标签:
新的一周^w^
2.22
cf 628d Magic Numbers
自己的状态定义错了
应该是 dp[i][j][0] 前 i 位 有一位是小于的,dp[i][j][1] 前 i 位都是相同的
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 const int mod = 1e9+7; 9 typedef long long LL; 10 const int maxn = 2005; 11 LL dp[maxn][maxn][2]; 12 int m,d,n,f[maxn][maxn]; 13 char s[maxn],p[maxn]; 14 15 LL solve(char* t){ 16 memset(dp,0,sizeof(dp)); 17 int v = t[1]-‘0‘; 18 for(int i = 1;i <= t[1]-‘0‘;i++){ 19 if(i == d) continue; 20 if(i == v) dp[1][v%m][1] += 1LL; 21 else dp[1][i%m][0] += 1LL; 22 // printf("dp[1][%d][0] = %d dp[1][%d][1] = %d\n",i%2,dp[1][i%m][0],i%2,dp[1][i%m][1]); 23 } 24 25 for(int i = 2;i <= n;i++){ 26 for(int j = 0;j < m;j++){ 27 if(i%2){ 28 for(int k = 0;k <= 9;k++){ 29 if(k == d) continue; 30 dp[i][(j*10+k)%m][0] += dp[i-1][j][0]; 31 if(k == t[i]-‘0‘) dp[i][(j*10+k)%m][1] += dp[i-1][j][1]; 32 if(k < t[i]-‘0‘) dp[i][(j*10+k)%m][0] += dp[i-1][j][1]; 33 } 34 } 35 else{ 36 dp[i][(j*10+d)%m][0] += dp[i-1][j][0]; 37 if(d == t[i]-‘0‘) dp[i][(j*10+d)%m][1] += dp[i-1][j][1]; 38 if(d < t[i]-‘0‘) dp[i][(j*10+d)%m][0] += dp[i-1][j][1]; 39 } 40 dp[i][j][0] %= mod; 41 dp[i][j][1] %= mod; 42 // printf(">_<dp[%d][%d][0] = %d ",i,j,dp[i][j][0]); 43 // printf("dp[%d][%d][1] = %d\n",i,j,dp[i][j][0]); 44 } 45 } 46 // printf("dp[%d][0][0] = %d",n,dp[n][0][0]); 47 // printf(" dp[%d][0][1] = %d\n",n,dp[n][0][1]); 48 return (dp[n][0][0]+dp[n][0][1])%mod; 49 } 50 51 int ok(char* s){ 52 for(int i = 1;i <= n;i++){ 53 if(i%2 == 1 && (s[i]-‘0‘) == d) return 0; 54 if(i%2 == 0 && (s[i]-‘0‘) != d) return 0; 55 } 56 LL res = 0; 57 for(int i = 1;i <= n;i++){ 58 res = (res*10+(s[i]-‘0‘))%m; 59 } 60 if(res == 0) return 1; 61 return 0; 62 } 63 64 int main(){ 65 while(scanf("%d %d",&m,&d) != EOF){ 66 scanf("%s",s+1); 67 scanf("%s",p+1); 68 n = strlen(s+1); 69 LL l = solve(s); 70 LL r = solve(p); 71 LL ans = (r-l+mod)%mod; 72 if(ok(s)) ans++; 73 printf("%I64d\n",ans); 74 } 75 return 0; 76 }
标签:
原文地址:http://www.cnblogs.com/wuyuewoniu/p/5206025.html