有趣的数

问题描述

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

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

 

solution : 数位dp搞起来,dp【i】【a】【b】【c】【d】 表示到了第i位 0 ,1, 2,3的出现情况,1代表出现,0代表未出现。

 

  1 #include <cstdio>

 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 #include <iostream>
 6 using namespace std;
 7 typedef long long LL;
 8 const int MAX = 1e3+10;
 9 const int MOD = 1e9+7;
10 LL dp[MAX][2][2][2][2];
11 LL dfs(int pos,int a,int b,int c,int d) {
12     if(!pos&&a&&b&&c&&d) return 1;
13     if(dp[pos][a][b][c][d]!=-1) {
14         return dp[pos][a][b][c][d];
15     }
16     LL ans=0;
17     if(b==0) {
18         ans=(ans+dfs(pos-1,1,b,c,d))%MOD;
19     }
20     if(a==1) {
21         ans=(ans+dfs(pos-1,a,1,c,d))%MOD;
22     }
23     if(d==0) {
24         ans=(ans+dfs(pos-1,a,b,1,d))%MOD;
25     }
26     if(c==1) {
27         ans=(ans+dfs(pos-1,a,b,c,1))%MOD;
28     }
29     return dp[pos][a][b][c][d]=ans;
30 }
31 int main() {
32     int n;
33     scanf("%d",&n);
34     memset(dp,-1,sizeof(dp));
35     LL ans=dfs(n-1,false,false,true,false);
36     printf("%lld\n",ans);
37 
38 }

 

posted @ 2015-09-11 12:15  acvc  阅读(258)  评论(0编辑  收藏  举报