D. Magic Numbers
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 is0-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 6
10
99
output
8
input
2 0
1
9
output
4
input
19 7
1000
9999
output
6
Note

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.

题意:
定义一个数字为d-magic当且仅当数字d仅在数字偶数位出现。
问在[a,b]之间的被m整除的d-magic数字有多少个。

 

题解:
数位dp味道很浓,用十分经典的模型就行了。
不难想到f[N][2][2][M](f[i][0..1][0..1][j])。
代表已经确定了前i位数字的选择(不一定要i位都填,可能前面不足i位),
当前位为奇数位或偶数位,
当前是否达到前i位数字的上限,
MOD m后余数为j的方案数。
转移是很显然的。

  

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std; const int N = , MOD = ;
int m, d, lenLow, lenHigh;
char low[N], high[N];
int dp[N][][][N]; int add(int a, int b) {
return (((a + b) % MOD) + MOD) % MOD;
} int work(char *str) {
int n = strlen(str);
for(int full = ; full < ; ++full)
for(int odd = ; odd < ; ++odd)
for(int mod = ; mod < m; ++mod)
dp[][full][odd][mod] = ;
for(int i = ; i <= str[] - ''; ++i) {
if(i == d) continue;
int &nex = dp[][i == str[] - ''][][i % m];
nex = add(nex, );
}
for(int digit = ; digit < n - ; ++digit) {
for(int full = ; full < ; ++full)
for(int odd = ; odd < ; ++odd)
for(int mod = ; mod < m; ++mod)
dp[digit + ][full][odd][mod] = ; int lim = str[digit + ] - '';
for(int full = ; full < ; ++full)
for(int odd = ; odd < ; ++odd)
for(int mod = ; mod < m; ++mod) {
if(!dp[digit][full][odd][mod]) continue;
int now = dp[digit][full][odd][mod];
if(odd) {
if(!full || (full && lim >= d)) {
int &nex = dp[digit + ][full && d == lim][odd ^ ][(mod * + d) % m];
nex = add(nex, now);
}
} else {
if(full) {
for(int i = ; i <= lim; ++i) {
if(i == d) continue;
int &nex = dp[digit + ][i == lim][odd ^ ][(mod * + i) % m];
nex = add(nex, now);
}
} else {
for(int i = ; i < ; ++i) {
if(i == d) continue;
int &nex = dp[digit + ][][odd ^ ][(mod * + i) % m];
nex = add(nex, now);
}
}
}
} for(int i = ; i < ; ++i) {
if(i == d) continue;
int &nex = dp[digit + ][][][i % m];
nex = add(nex, );
}
} int ret = ;
for(int full = ; full < ; ++full)
for(int odd = ; odd < ; ++odd)
ret = add(ret, dp[n - ][full][odd][]);
return ret;
} void solve() {
bool flag = true;
for(int i = ; i < lenLow; i += )
if(low[i] != d + '') flag = false;
for(int i = ; i < lenLow; i += )
if(low[i] == d + '') flag = false;
int sum = ;
for(int i = ; i < lenLow; ++i)
sum = (sum * + low[i] - '') % m;
flag &= sum == ; int ansb = work(high);
int ansa = work(low); printf("%d\n", add(flag, add(ansb, -ansa)));
} int main() {
scanf("%d%d", &m, &d);
scanf("%s", low);
lenLow = strlen(low);
scanf("%s", high);
lenHigh = strlen(high);
solve();
return ;
}

Codeforces CF#628 Education 8 D. Magic Numbers的更多相关文章

  1. Codeforces CF#628 Education 8 F. Bear and Fair Set

    F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces CF#628 Education 8 E. Zbazi in Zeydabad

    E. Zbazi in Zeydabad time limit per test 5 seconds memory limit per test 512 megabytes input standar ...

  3. Codeforces CF#628 Education 8 C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  4. Codeforces CF#628 Education 8 B. New Skateboard

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. Codeforces CF#628 Education 8 A. Tennis Tournament

    A. Tennis Tournament time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #189 (Div. 2) A. Magic Numbers【正难则反/给出一个数字串判断是否只由1,14和144组成】

    A. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. Codeforces Round #189 (Div. 2) A. Magic Numbers

    #include <iostream> #include <vector> #include <algorithm> #include <string> ...

  8. CodeForces 628 D Magic Numbers 数位DP

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

  9. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

随机推荐

  1. DEV MessageBox

    DialogResult dr = DevExpress.XtraEditors.XtraMessageBox.Show("确定要删除所有错误映射数据吗?", "提示&q ...

  2. 基于.net搭建热插拔式web框架(实现原理)

    第一节:我们为什么需要一个热插拔式的web框架? 模块之间独立开发 假设我们要做一个后台管理系统,其中包括“用户活跃度”.“产品管理”."账单管理"等模块.每个模块中有自己的业务特 ...

  3. N皇后问题—初级回溯

    N皇后问题,最基础的回溯问题之一,题意简单N*N的正方形格子上放置N个皇后,任意两个皇后不能出现在同一条直线或者斜线上,求不同N对应的解. 提要:N>13时,数量庞大,初级回溯只能保证在N< ...

  4. c/c++中关于sizeof、strlen的使用说明

    sizeof: 一般指类型.变量等占用的内存大小(由于在编译时计算,因此sizeof不能用来返回动态分配的内存空间的大小) strlen: c字符串的长度(参数必须是字符型指针 char*,当数组名作 ...

  5. Lr中关于字符串的截取

    Action() { char separators[] = "\"processId\":\"";//截取条件 char * token; char ...

  6. 面向对象Part1对象的创建和Static!

    面向对象的特征: 1)封装 2)继承 3)多台 4)抽象 创建的是什么类型的对象 变量的声明就是什么类型. class Servant{ void xxx (){} } Servant s1 = ne ...

  7. NodeJS Addon 多线程

    Mac版本客户端准备使用electron实现,需要对现有的C API的IM SDK 做NodeJS封装,提供Javascript接口. 使用Nan,遇到的问题主要是NodeJS是libuv defal ...

  8. 在Web Api中集成protobuf

    安装WebApiContrib.Formatting.ProtoBuf Install-Package WebApiContrib.Formatting.ProtoBuf 注册ProtoBufForm ...

  9. Angular2 组件生命周期

    1. 说明 Angular每个组件都存在一个生命周期,从创建,变更到销毁.Angular提供组件生命周期钩子,把这些关键时刻暴露出来,赋予在这些关键结点和组件进行交互的能力. 2. 接口 按照生命周期 ...

  10. urlencode遇到中文编码问题

    urlencode并不会改变输入的编码格式, 默认会将中文输出为 gbk 编码, 类似的, quote 会对中文进行 gbk 编码 不过, 当遇到嵌套多层的字典时, 问题就来了, 中文会被 utf8 ...