Codeforces CF#628 Education 8 D. Magic Numbers
2 seconds
256 megabytes
standard input
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).
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.
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.
2 6
10
99
8
2 0
1
9
4
19 7
1000
9999
6
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #189 (Div. 2) A. Magic Numbers
#include <iostream> #include <vector> #include <algorithm> #include <string> ...
- CodeForces 628 D Magic Numbers 数位DP
Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
随机推荐
- 使用ICSharpCode.SharpZipLib.Zip类库解压zip文件的方法
public static bool ZipExtractFile(string zipFilePath,string targetPath) { FastZip fastZip = new Fast ...
- Delphi XE7中各种字符串与字符类型的内存结构
1. ShortString 类型 定义:type ShortString = string[255]; 内存结构与大小:ShortString 是每个字符为单字节的字符串.ShortString 的 ...
- Ubuntu安装SSH服务器故障分析及解决办法(错误1:E:软件包 openssh-server 还没有可供安装的候选者,错误2:E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系)
• 微博: 小样儿老师2015 Windows下做Linux开发需要SSH强大功能的支持.安装SSH的过程会出现了很多问题,看完这篇文章可以让你少走些弯路,PS:折腾一下午的成果. Ubuntu ...
- crontab详解
搜索 纠正错误 添加实例 crontab 提交和管理用户的需要周期性执行的任务 补充说明 crontab命令 被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成 ...
- java IO输入输出流中的各种字节流,字符流类
字节流字节流主要是操作byte类型数据,也byte数组为准,主要操作类就是·字节输出流:OutputStream·字节输入流:InputStream字符流在程序中一个字符等于2个字节,那么java提供 ...
- 使select文本框可编辑可选择(jQuery插件)
最近做项目中用到了这个插件,正好分享下. 1. 需要用的js包点击下载,在项目中引入该js. <script src="${pageContext.request.contextPa ...
- c/c++ string
string类的定义.操作. #include<iostream> #include<string> using namespace std; int main() { // ...
- iOS常用第三方开源框架和优秀开发者博客等
博客收藏iOS开发过程好的开源框架.开源项目.Xcode工具插件.Mac软件.文章等,会不断更新维护,希望对你们有帮助.如果有推荐或者建议,请到此处提交推荐或者联系我. 该文档已提交GitHub,点击 ...
- PHP新手常见的一些不好习惯(抄的 有待理解)
1.不写注释(是个好习惯,不过也没必要每个语句都要写) 2.不使用可以提高生产效率的IDE工具 3.不使用版本控制 4.不按照编程规范写代码 5.不使用统一的方法 6.编码前不去思考和计划 7.在执行 ...
- 机器学习caffe环境搭建——redhat7.1和caffe的python接口编译
相信看这篇文章的都知道caffe是干嘛的了,无非就是深度学习.神经网络.计算机视觉.人工智能这些,这个我就不多介绍了,下面说说我的安装过程即遇到的问题,当然还有解决方法. 说下我的环境:1>虚拟 ...