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之计算器

    开发一个简单的python计算器 1.实现加减乘除及拓号优先级解析 2.用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * ...

  2. 简谈const限定符

    const修饰的数据类型是常量类型,常量类型的对象和变量在定义初始化后是不能被更新的.其实只用记住这一个概念,就可以明白const操作对象的方法. 1)定义const常量 最简单的: const in ...

  3. qt-creator

    https://github.com/qt-creator/qt-creator https://github.com/qt-creator

  4. 002 Lock和synchronized的区别和使用

    转自 https://www.cnblogs.com/baizhanshi/p/6419268.html 今天看了并发实践这本书的ReentantLock这章,感觉对ReentantLock还是不够熟 ...

  5. API(选项/数据 选项/dom)

    选项/数据 data 类型: Object | Function 限制: 组件的定义只接受function var data = { a: 1 } // 直接创建一个实例 var vm = new V ...

  6. edittext 的一个案例

        <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android= ...

  7. C# 笔记——索引器

    索引器允许类或者结构的实例按照与数组相同的方式进行索引取值,索引器与属性类似,不同的是索引器的访问是带参的. 索引器和数组比较: (1)索引器的索引值(Index)类型不受限制 (2)索引器允许重载 ...

  8. 从一个R语言案例学线性回归

    线性回归简介 如下图所示,如果把自变量(也叫independent variable)和因变量(也叫dependent variable)画在二维坐标上,则每条记录对应一个点.线性回规最常见的应用场景 ...

  9. finally

     finally 我们都知道无论try语句中是否抛出异常,finally中的语句一定会被执行.我们来看下面的例子: try: f = open("/tmp/output", &qu ...

  10. 赤峰项目目前的mysql配置项目

    #BEGIN CONFIG INFO #DESCR: 4GB RAM, InnoDB only, ACID, few connections, heavy queries #TYPE: SYSTEM ...