Magic Numbers
Test instructions: Given a length of not more than 2000 of A, a, ask how many X (a<=x<=b) makes X's even digit is d, the odd digit is not D; result MoD 1e9+7;
Direct digital DP; Note that before judging whether f[pos][mod]! =-1, it is possible to determine whether it is a boundary, or the duplicate calculation will occur;
#include <bits/stdc++.h>using namespacestd;#defineINF 0x3f3f3f3f#defineMS1 (a) memset (A,-1,sizeof (a))Const intN =2005, MOD = 1e9+7;CharA[n],b[n];intf[n][n][2],n,m,d;intDfsChar* S,intPintMoDintOn =1){ if(p = = n)returnMoD = =0; int& ans =F[p][mod][on]; if(ans! =-1)returnans; Ans=0; for(inti =0; I <= (On?s[p]-'0':9); i++){ if(p%2&& i! = d)Continue; if(p%2==0&& i = = d)Continue; (Ans+ = DFS (s,p+1, (mod*Ten+i)%m,on && i = = s[p]-'0')) %=MOD; } returnans;}intCalcChar*s) {MS1 (f); returnDFS (S,0,0);}intMain () {scanf ("%d%d",&m,&d); scanf ("%s%s", A, b); N=strlen (a); for(inti = n1; I >=0; i--){ if(A[i] = ='0') A[i] ='9'; Else{a[i]--; Break;} } printf ("%d", (Calc (b)-calc (a) +mod)%MOD);}
View Code
Another is the addition of one dimension [2], which is used to store whether it is a boundary;
#include <bits/stdc++.h>using namespacestd;#defineINF 0x3f3f3f3f#defineMS1 (a) memset (A,-1,sizeof (a))Const intN =2005, MOD = 1e9+7;CharA[n],b[n];intf[n][n][2],n,m,d;intDfsChar* S,intPintMoDintOn =1){ if(p = = n)returnMoD = =0; int& ans =F[p][mod][on]; if(ans! =-1)returnans; Ans=0; for(inti =0; I <= (On?s[p]-'0':9); i++){ if(p%2&& i! = d)Continue; if(p%2==0&& i = = d)Continue; (Ans+ = DFS (s,p+1, (mod*Ten+i)%m,on && i = = s[p]-'0')) %=MOD; } returnans;}intCalcChar*s) {MS1 (f); returnDFS (S,0,0);}intMain () {scanf ("%d%d",&m,&d); scanf ("%s%s", A, b); N=strlen (a); for(inti = n1; I >=0; i--){ if(A[i] = ='0') A[i] ='9'; Else{a[i]--; Break;} } printf ("%d", (Calc (b)-calc (a) +mod)%MOD);}
View Code
Educational codeforces Round 8 D. Magic Numbers