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 ...
随机推荐
- nuget的搭建及多源冲突
为什么使用nuget来管理类库引用就不再阐述,好处真的一抓一把.在使用nuget的时候,我们如果总去访问别人的nuget源,受限于网络情况的好坏,速度真的没法保证,更别说访问国外的源了.那好,我们来自 ...
- JavaScript 常用正则表达式
==========================正则表达式=========================== 常用元字符 代码 说明 . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划 ...
- Spring框架学习(一)
一. spring概述 Spring 框架是一个分层架构,由 7 个定义良好的模块组成.Spring 模块构建在核心容器之上,核心容器定义了创建.配置和管理 bean 的方式,如图 1 所示. 图 1 ...
- Go简介
Go是Google开发的一种编译型,並發型,并具有垃圾回收功能的编程语言. 罗伯特·格瑞史莫(Robert Griesemer),罗勃·派克(Rob Pike)及肯·汤普逊于2007年9月开始设计Go ...
- [PHP][REDIS]phpredis 'RedisException' with message 'read error on connection'
最近一个后台常驻job通过redis的brpop阻塞读取消息时,设置了永不超时 while( $re=$redis->brPop($queue_name,0) ){ } 但是在实际的使用中发现很 ...
- jdbcTemplate批量插入(添加)
public void addSubscibe(List<PermedipUserSubscribeVo> list) { final List<PermedipUserSubscr ...
- MVC 导出Excel 的其中一方法(View导出excel)
场景:mvc下导出excel 思路:使用View导出excel 步骤: 1.导出标签添加事件 $("#export_A").click(function(){ //省略代码.... ...
- 离线安装 Python 2.7, paramiko 和 tornado
无非就是离线安装, 步骤比较繁琐, 记录一下. 需求很简单, 一个离线安装的 Python, 能跑 tornado 和 paramiko 1. 离线安装 Python 2.7 .tgz cd Pyth ...
- printf对齐
C语言中,将printf函数打印出的字符像表格一样分类对齐.%-10d表示这个字符型占10个字节,负号表示左对齐.即下面表格中的x1位置开始填充.如果是%10d,表示右对齐,即在x10位置对齐. x1 ...
- iOS版本更新在APP中直接访问AppStore
1.导入框架 #import <StoreKit/StoreKit.h> 2.添加代理 <SKStoreProductViewControllerDelegate> 3.设置跳 ...