codeforces628D. Magic Numbers (数位dp)
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).
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.
题意:给你一个区间[a,b],让你找到这个区间内满足没有前导零且偶数位都是d,奇数位不出现d,并且这个数能被m整除的数的个数。
思路:用dp[pos][yushu][oushu]表示pos位前面的位形成的数modm后余数为yushu,且当前位是否是偶数的方案数,要注意前导零。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define MOD 1000000007
char s1[2005],s2[2005];
int wei[2005];
ll dp[2005][2005][2];
int m,d;
void add(ll& x,ll y) {
x += y;
if(x>=MOD) x-=MOD;
}
ll dfs(int pos,int yushu,int oushu,int flag,int zero)
{
int i,j;
if(pos==-1){
if(zero==1)return 0;
if(yushu==0)return 1;
else return 0;
}
if(!flag && !zero && dp[pos][yushu][oushu]!=-1){
return dp[pos][yushu][oushu];
}
int ed=flag?wei[pos]:9;
ll ans=0;
if(zero==1){
add(ans,dfs(pos-1,yushu,oushu,0,1));
for(i=1;i<=ed;i++){
if(i!=d)add(ans,dfs(pos-1,(yushu*10+i)%m,1^oushu,flag&&wei[pos]==i,0) );
}
}
else{
if(oushu){
if(d<=ed)add(ans,dfs(pos-1,(yushu*10+d)%m,1^oushu,flag&&wei[pos]==d,0) );
}
else{
for(i=0;i<=ed;i++){
if(i!=d)add(ans,dfs(pos-1,(yushu*10+i)%m,1^oushu,flag&&wei[pos]==i,0) );
}
}
}
if(!flag && !zero){
dp[pos][yushu][oushu]=ans;
}
return ans;
}
ll solve(char s[])
{
int len,i,j;
len=strlen(s);
for(i=len-1;i>=0;i--){
wei[i]=s[i]-'0';
}
return dfs(len-1,0,0,1,1);
}
int main()
{
int n,i,j,len1,len2;
while(scanf("%d%d",&m,&d)!=EOF)
{
scanf("%s%s",s1,s2);
len1=strlen(s1);
reverse(s1,s1+len1);
for(i=0;i<len1;i++){
if(s1[i]=='0'){
s1[i]='9';
}
else{
s1[i]--;break;
}
}
if(s1[len1-1]=='0'){
s1[len1-1]='\0';
len1--;
}
len2=strlen(s2);
reverse(s2,s2+len2);
memset(dp,-1,sizeof(dp));
ll num1=solve(s1);
ll num2=solve(s2);
printf("%I64d\n",((num2-num1)%MOD+MOD)%MOD );
}
return 0;
}
codeforces628D. Magic Numbers (数位dp)的更多相关文章
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- CodeForces 628 D Magic Numbers 数位DP
Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...
- 【CF628D】Magic Numbers 数位DP
[CF628D]Magic Numbers 题意:求[a,b]中,偶数位的数字都是d,其余为数字都不是d,且能被m整除的数的个数(这里的偶数位是的是从高位往低位数的偶数位).$a,b<10^{2 ...
- CodeForces 628D Magic Numbers (数位dp)
题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- poj 3252 Round Numbers(数位dp 处理前导零)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- uva 10712 - Count the Numbers(数位dp)
题目链接:uva 10712 - Count the Numbers 题目大意:给出n,a.b.问说在a到b之间有多少个n. 解题思路:数位dp.dp[i][j][x][y]表示第i位为j的时候.x是 ...
随机推荐
- spring cloud config —— git配置管理
目录 talk is cheep, show your the code Server端 pom.xml server的application.yml 配置文件 测试Server client端 po ...
- 【二分搜索树】1、二分查找法的实现 - Binary Search
简单记录 - bobo老师的玩转算法系列–玩转算法 - 二分搜索树 二叉搜索树 Binary Search Tree 查找问题 Searching Problem 查找问题是计算机中非常重要的基础问题 ...
- linux线程库
linux 提供两个线程库,Linux Threads 和新的原生的POSIX线程库(NPTL),linux threads在某些情况下仍然使用,但现在的发行版已经切换到NPTL,并且大部分应用已经不 ...
- 【函数分享】每日PHP函数分享(2021-1-19)
substr 函数返回字符串的一部分.注释:如果 start 参数是负数且 length 小于或等于 start,则 length 为 0. string substr (string $string ...
- 【ORA】ORA-00257 archiver error. 错误的处理方法
今天连接数据库,结果报错,ora-00257查看 [oracle@exam oracle]$ oerr ora 00257 00257, 00000, "archiver error. Co ...
- i春秋新春战疫—web—简单的招聘系统
打开靶机 打开后看到登录界面 利用万能密码,以admin身份登录 登录成功后看到如下界面 在Blank Page界面内发现注入点,抓包 保存在sqlmap目录下test.txt文件夹,使用sqlmap ...
- 远程部署项目,修改catalina.bat文件 完美解决在代理服务器上HttpURLConnection 调接口超时的问题
远程给客户部署项目,运行时程序调外部接口时总是出不去,经过不懈努力,后来发现客户那边的网络走的是代理,于是在代码中加下面代码: //设置代理 System.setProperty("http ...
- RecyclerView 源码分析(二) —— 缓存机制
在前一篇文章 RecyclerView 源码分析(一) -- 绘制流程解析 介绍了 RecyclerView 的绘制流程,RecyclerView 通过将绘制流程从 View 中抽取出来,放到 Lay ...
- python Mysql 多条件查询
做项目时,遇到一场景,前端至少传入一个参数,最多传入四个参数,根据单参数或者组合参数,从数据库筛选数据. 作为一个小白,思考之,从数学的角度,\(C_4^1 + C_4^2+C_4^3+C_4^4=1 ...
- uni-app 微信小程序 picker 三级联动
之前做过一个picker的三级联动功能,这里分享代码给大家 具体代码: // An highlighted block <template> <view> <picker ...