天天看点

CCF历程 201312-4 有趣的数 -Java CCF历程 201312-4 有趣的数 -Java

CCF历程 201312-4 有趣的数 -Java

试题编号: 201312-4
试题名称: 有趣的数
时间限制: 1.0s
内存限制: 256.0MB
问题简述:

问题描述:  我们把一个数称为有趣的,当且仅当:

1、它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次。

2、所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前。

3、最高位数字不为0。

因此,符合我们定义的最小的有趣的数是2013。除此以外,4位的有趣的数还有两个:2031和2301。请计算恰好有n位的有趣的数的个数。由于答案可能非常大,只需要输出答案除以1000000007的余数。输入格式输入只有一行,包括恰好一个正整数n (4 ≤ n ≤ 1000)输出格式输出只有一行,包括恰好n 位的整数中有趣的数的个数除以1000000007的余数。样例输入4样例输出3

java参考代码如下:

import java.util.Scanner;

public class NumberOfInteresting {
    public static void main(String[] args) {
        new NumberOfInteresting().run();
    }
    public void run() {
        Scanner fin = new Scanner(System.in);
        int N = fin.nextInt();
        long[] count = new long[8];
        count[6] = 0;
        count[7] = 1;
        long mod = 1000000007;
        for (int i = 2; i <= N; ++i) {
            long[] newCount = new long[8];
            newCount[0] = (count[0] * 2 + count[1] + count[3]) % mod;
            newCount[1] = (count[1] * 2 + count[2] + count[5]) % mod;
            newCount[2] = (count[2] + count[6]) % mod;
            newCount[3] = (count[3] * 2 + count[4] + count[5]) % mod;
            newCount[4] = (count[4] + count[7]) % mod;
            newCount[5] = (count[5] * 2 + count[6] + count[7]) % mod;
            newCount[6] = 0;
            newCount[7] = 1;
            count = newCount;
        }
        System.out.println(count[0]);
    }
}