D. Magic Numbers

题目连接:

http://www.codeforces.com/contest/628/problem/D

Description

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).

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.

Sample Input

2 6

10

99

Sample Output

8

Hint

题意

现在定义d-magic数字,就是一个没有前导0的数,d恰好仅出现在这个数的偶数位置。

然后现在给你m,d,a,b。问你在[a,b]内,是m的倍数,且是d-magic的数字有多少个

答案需要 mod 1e9+7

题解:

比较显然的数位dp

dp[len][mod][flag]表示现在长度是多少,现在的余数是多少,现在是否达到上界的方案数是多少

然后直接转移就好了

这个让L--很麻烦,所以我直接就判断L这个位置合不合法就好了,如果合法,我就直接让答案++就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3+5;
const int mod = 1e9+7;
int dp[maxn][maxn][2];
int vis[maxn][maxn][2];
char s[maxn];
int m,d,len;
int check()
{
int mm = 0;
for(int i=1;i<=len;i++)
{
mm = (mm+s[i]-'0')%m;
if(i%2==1&&(s[i]-'0')==d)
return 0;
if(i%2==0&&(s[i]-'0')!=d)
return 0;
}
if(mm!=0)return 0;
return 1;
}
void update(int &a,int b)
{
a = (a+b)%mod;
}
int solve(int Len,int Mod,int Flag)
{
if(Len==len+1)return Mod==0?1:0;
if(vis[Len][Mod][Flag])return dp[Len][Mod][Flag];
vis[Len][Mod][Flag]=1;
int st=0,ed=0;
if(Flag!=0)ed=9;else ed=s[Len]-'0';
if(Len==1)st=1;else st=0;
if(Len%2==0)
{
if(ed>=d)
{
int Flag2 = Flag|(d<(s[Len]-'0'));
update(dp[Len][Mod][Flag],solve(Len+1,(Mod*10+d)%m,Flag2));
}
}
else
{
for(int i=st;i<=ed;i++)
{
if(i==d)continue;
int Flag2 = Flag|(i<(s[Len]-'0'));
update(dp[Len][Mod][Flag],solve(Len+1,(Mod*10+i)%m,Flag2));
}
}
return dp[Len][Mod][Flag];
} int main()
{
scanf("%d%d",&m,&d);
scanf("%s",s+1);
len = strlen(s+1);
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
int ans1 = solve(1,0,0),ans2=0;
if(check())ans2++;
scanf("%s",s+1);
len = strlen(s+1);
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
ans2 += solve(1,0,0);
int ans=(ans2-ans1)%mod;
if(ans<0)ans+=mod;
cout<<ans<<endl;
}

Educational Codeforces Round 8 D. Magic Numbers 数位DP的更多相关文章

  1. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  2. Educational Codeforces Round 8 D. Magic Numbers

    Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...

  3. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  4. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

  5. CodeForces 628 D Magic Numbers 数位DP

    Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...

  6. 【CF628D】Magic Numbers 数位DP

    [CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...

  7. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  8. Educational Codeforces Round 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  9. CodeForces 628D Magic Numbers (数位dp)

    题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...

随机推荐

  1. 【Python学习笔记】colormap的参数及其对应的色条

  2. 深入分析_linux_spinlock_实现机制【转】

    转自:http://blog.csdn.net/electrombile/article/details/51289813 在 x86 平台上,spinlock 主要通过处理器的 lock 指令前缀实 ...

  3. libsensor

    https://github.com/easyiot-china/libsensor https://github.com/adafruit/DHT-sensor-library https://gi ...

  4. api文档工具

    平台选型         Apidoc         文档参考:http://apidocjs.com        优点      文档齐全,操作简单,ui清晰,代码注解查询性强,语言支持多元化, ...

  5. 【LA3882】And then there was one

    做sb题也是一种乐趣,是吧…… #include<bits/stdc++.h> #define N 10005 using namespace std; int f[N],m,n,k; i ...

  6. 在Caffe中使用 DIGITS(Deep Learning GPU Training System)自定义Python层

    注意:包含Python层的网络只支持单个GPU训练!!!!! Caffe 使得我们有了使用Python自定义层的能力,而不是通常的C++/CUDA.这是一个非常有用的特性,但它的文档记录不足,难以正确 ...

  7. App推广

    一行业常见名词解释 开发商:也叫CP,即Content Provider内容提供商的英文首字母缩写. 发行商(运营商):即代理CP开发出来的产品. 渠道:拥有用户,能够进行流量分发的公司,即可成为渠道 ...

  8. mac下安装golang

    1.安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...

  9. [转载] Python itertools模块详解

    原文在这里,写的很详细,感谢原作者,以下摘录要点. itertools用于高效循环的迭代函数集合. 无限迭代器 迭代器 参数 结果 例子 count() start, [step] start, st ...

  10. 访问Github慢的解决办法

    http://blog.csdn.net/sunsteam/article/details/63253933 http://tool.chinaz.com/dns 151.101.196.249  g ...