Codeforces 628D Magic Numbers 数位DP
2017-11-01 23:04
676 查看
Consider the decimal presentation of an integer. Let’s call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else.
For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic.
Find the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109 + 7 (so you should find the remainder after dividing by 109 + 7).
The second line contains positive integer a in decimal presentation (without leading zeroes).
The third line contains positive integer b in decimal presentation (without leading zeroes).
It is guaranteed that a ≤ b, the number of digits in a and b are the same and don’t exceed 2000.
10
99
1
9
1000
9999
The numbers from the answer of the second example are 2, 4, 6 and 8.
The numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747.
在[a,b]范围内求有几个数,满足:1.是m的倍数,2.从左往右数的偶数位一定是数字k,奇数位一定不是k。
取模运算可加,高位的余数*10+低位再取模不影响结果,所以数位DP递归到最后一位就可以得到这个数是否是m的整数倍,转移的状态是上一位取模剩下的余数。
注意数位DP一般是求(a,b],此题要额外检验a是否满足条件,注意变量开longlong,且a<1e9+7,b>1e9+7时由于取模b
For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic.
Find the number of d-magic numbers in the segment [a, b] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109 + 7 (so you should find the remainder after dividing by 109 + 7).
Input
The first line contains two integers m, d (1 ≤ m ≤ 2000, 0 ≤ d ≤ 9) — the parameters from the problem statement.The second line contains positive integer a in decimal presentation (without leading zeroes).
The third line contains positive integer b in decimal presentation (without leading zeroes).
It is guaranteed that a ≤ b, the number of digits in a and b are the same and don’t exceed 2000.
Output
Print the only integer a — the remainder after dividing by 109 + 7 of the number of d-magic numbers in segment [a, b] that are multiple of m.Examples
input
2 610
99
output
8input
2 01
9
output
4input
19 71000
9999
output
6Note
The numbers from the answer of the first example are 16, 26, 36, 46, 56, 76, 86 and 96.The numbers from the answer of the second example are 2, 4, 6 and 8.
The numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747.
在[a,b]范围内求有几个数,满足:1.是m的倍数,2.从左往右数的偶数位一定是数字k,奇数位一定不是k。
取模运算可加,高位的余数*10+低位再取模不影响结果,所以数位DP递归到最后一位就可以得到这个数是否是m的整数倍,转移的状态是上一位取模剩下的余数。
注意数位DP一般是求(a,b],此题要额外检验a是否满足条件,注意变量开longlong,且a<1e9+7,b>1e9+7时由于取模b
#include <iostream> #include <stdio.h> #include <map> #include <set> #include <queue> #include <algorithm> #include <math.h> #include <string.h> using namespace std; typedef long long ll; int mo[4][2]={0,1,1,0,0,-1,-1,0}; const int MAXN=1000000007; const int sz=2005; char s[sz]; ll dp[sz][sz][2],bit[sz]; int m,k,len; ll dfs(int pos,int rem,int up){ if(pos==len) return rem==0; if(dp[pos][rem][up]!=-1) return dp[pos][rem][up]; int end; if(up) end=bit[pos]; else end=9; ll ans=0; for(int i=0;i<=end;i++){ if(pos%2==0&&i==k) continue; if(pos%2==1&&i!=k) continue; ans+=dfs(pos+1,((rem*10)+i)%m,up&&i==end); ans%=MAXN; } dp[pos][rem][up]=ans; return ans; } ll get(){ len=strlen(s); for(int i=0;i<len;i++){ bit[i]=s[i]-'0'; } return dfs(0,0,1); } int check(){ int t=0; for(int i=0;i<len;i++){ if(i%2==0&&bit[i]==k) return 0; if(i%2==1&&bit[i]!=k) return 0; t=t*10+bit[i]; t%=m; } if(t==0) return 1; else return 0; } int main() { //freopen("r.txt","r",stdin); while(scanf("%d%d",&m,&k)!=EOF){ ll ans=0; memset(dp,-1,sizeof(dp)); scanf("%s",&s); ll a=get(); ans+=check(); memset(dp,-1,sizeof(dp)); scanf("%s",&s); ll b=get(); ans+=b-a; ans=(ans+MAXN)%MAXN; cout<<ans<<endl; } return 0; }
相关文章推荐
- CodeForces 628D Magic Numbers(数位DP)
- Codeforces 628D Magic Numbers 【数位dp】
- CodeForces 628D Magic Numbers (数位DP)
- Magic Numbers codeforces 628D 数位dp
- codeforces 628D Magic Numbers (数位dp)
- Magic Numbers CodeForces - 628D [数位DP]
- codeforces_628D. Magic Numbers(数位dp)
- Codeforces 628D 偶数位全是某个数 数位dp
- codeforces 628D. Magic Numbers 数位dp
- codeforces 628D 数位dp
- 【CF628D】Magic Numbers 数位DP
- Codeforces edu 8 D. Magic Numbers 数位dp
- Codeforces 628D 数位dp
- Codeforces 628D. Magic Numbers 数位DP
- Magic Numbers CodeForces - 628D (数位dp)
- CodeForces - 55D Beautiful numbers(数位dp 离散化)
- 【数位dp】【HDU 3555】【HDU 2089】【UESTC 1307】【CodeForces 258B】数位DP入门题
- codeforces 55D Beautiful numbers[数位dp]
- Codeforces 55D. Beautiful numbers (数位DP)
- CodeForces 401D 数位DP