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 ...
随机推荐
- JavaWeb监听器的使用(一)监听上下文和会话信息
1.监听上下文的类 package com.examp.ch9; import java.io.FileOutputStream; import java.io.PrintWriter; import ...
- 深入理解 JavaScript 变量的作用域和作用域链
一个变量的作用域(scope)是程序源代码中定义这个变量的区域.简单的说,作用域就是变量与函数的可访问范围.全局变量拥有全局作用域,在JavaScript代码中的任何地方都有定义.局部变量是在函数体内 ...
- Makefile文件学习总结
Makefile文件相当于是一种脚本编程语言,目的是实现自动化编译.编写makefile文件的过程中可以使用变量.控制结构和函数等一般编程语言的特性. Makefile文件的组成内容.makefile ...
- 本地mysql打不开的解决方法
今天打开本地mysql的数据库,却一直打开报错.错误代码是10061. 让同事过来一看,发现是自己的mysql服务器并未启动.右下角任务管理器的mysql服务器为红色未启动状态.
- WAMP错误提示:HTTP Error 404.The requested resource is not found
原因: 本地80端口被占用,需要修改WAMP的默认端口 修改设置: 找到 bin/apache/apache***/conf/httpd.conf文件 将文件中的80修改为8088 (注:修改三个位置 ...
- NSIS对话框单位造成的控件移位问题
在使用NSIS脚本开发安装卸载程序,使用自定义的nsdialog控件.发现在小部分系统上安装时,一些控件会消失,或者挪位.于是排除问题,看看这些控件的为位置和坐标,发现基本上是使用了对话框单位的控件, ...
- selenium 常见面试题以及答案(Java版)
1.怎么 判断元素是否存在? 判断元素是否存在和是否出现不同, 判断是否存在意味着如果这个元素压根就不存在, 就会抛出NoSuchElementException 这样就可以使用try catch,如 ...
- Sublime Text:Windows下配置C 编译环境和GDB调试环境
写此文解决两个问题: 1.在Sublime Text中实现编译运行含有外部输入的C程序(如含有scanf的程序); 2.在程序运行完毕后不退出cmd,能继续用gdb调试程序. 一.MinGW 下载地址 ...
- windows内网渗透技巧
1.(windows)无扫描器情况下内网存活主机探测: for /l %i in (1,1,255) do @ping 192.168.1.%i -w 1 -n 1 | find /i "t ...
- C#后台调用公网接口(GET, POST)
//get方法调用接口获取json文件内容 public void GetFunction() { string service ...