Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers
题目连接:
http://www.codeforces.com/contest/628/problem/D
Description
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).
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.
Sample Input
2 6
10
99
Sample Output
8
Hint
题意
现在定义d-magic数字,就是一个没有前导0的数,d恰好仅出现在这个数的偶数位置。
然后现在给你m,d,a,b。问你在[a,b]内,是m的倍数,且是d-magic的数字有多少个
答案需要 mod 1e9+7
题解:
比较显然的数位dp
dp[len][mod][flag]表示现在长度是多少,现在的余数是多少,现在是否达到上界的方案数是多少
然后直接转移就好了
这个让L--很麻烦,所以我直接就判断L这个位置合不合法就好了,如果合法,我就直接让答案++就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e3+5;
const int mod = 1e9+7;
int dp[maxn][maxn][2];
int vis[maxn][maxn][2];
char s[maxn];
int m,d,len;
int check()
{
int mm = 0;
for(int i=1;i<=len;i++)
{
mm = (mm+s[i]-'0')%m;
if(i%2==1&&(s[i]-'0')==d)
return 0;
if(i%2==0&&(s[i]-'0')!=d)
return 0;
}
if(mm!=0)return 0;
return 1;
}
void update(int &a,int b)
{
a = (a+b)%mod;
}
int solve(int Len,int Mod,int Flag)
{
if(Len==len+1)return Mod==0?1:0;
if(vis[Len][Mod][Flag])return dp[Len][Mod][Flag];
vis[Len][Mod][Flag]=1;
int st=0,ed=0;
if(Flag!=0)ed=9;else ed=s[Len]-'0';
if(Len==1)st=1;else st=0;
if(Len%2==0)
{
if(ed>=d)
{
int Flag2 = Flag|(d<(s[Len]-'0'));
update(dp[Len][Mod][Flag],solve(Len+1,(Mod*10+d)%m,Flag2));
}
}
else
{
for(int i=st;i<=ed;i++)
{
if(i==d)continue;
int Flag2 = Flag|(i<(s[Len]-'0'));
update(dp[Len][Mod][Flag],solve(Len+1,(Mod*10+i)%m,Flag2));
}
}
return dp[Len][Mod][Flag];
}
int main()
{
scanf("%d%d",&m,&d);
scanf("%s",s+1);
len = strlen(s+1);
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
int ans1 = solve(1,0,0),ans2=0;
if(check())ans2++;
scanf("%s",s+1);
len = strlen(s+1);
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
ans2 += solve(1,0,0);
int ans=(ans2-ans1)%mod;
if(ans<0)ans+=mod;
cout<<ans<<endl;
}
Educational Codeforces Round 8 D. Magic Numbers 数位DP的更多相关文章
- Educational Codeforces Round 53 E. Segment Sum(数位DP)
Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...
- Educational Codeforces Round 8 D. Magic Numbers
Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- 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 ...
- 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 ...
- Educational Codeforces Round 2 A. Extract Numbers 模拟题
A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- Educational Codeforces Round 9 F. Magic Matrix 最小生成树
F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...
- CodeForces 628D Magic Numbers (数位dp)
题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...
随机推荐
- ubuntu安装wifi
因为需要测试app,但是自己本地是deepin.如果win下的话直接安装一个wifi万能管家啥的就完事儿了.可是这个是linux 有点不同.所以就进行百度学习了一波.特此分享. 需要用到的工具 hos ...
- Django rest framework 的认证流程(源码分析)
一.基本流程举例: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^users/', views.HostView.as_view() ...
- 阿里云ECS安装Docker
阿里云ESC系统信息,官方说2.6内核运行docker服务可能会不稳定: $ uname -a Linux iZ259dixwg8Z -.el6.x86_64 # SMP Thu Jul :: UTC ...
- MVC 从控制器将数据对象赋值给前端JS对象
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- mysql连接池优化笔记
中间件mycat是一个高性能的分表分库读写分离的中间件,但配置不好的情况会出现很多性能问题. 1.mycat-web的监控的准确性有问题,1.6-RELEASE ,1.0-SNAPSHOT (web ...
- Intellij idea的maven依赖图
Intellij idea下查看maven的依赖图与eclipse有所不同.下面简单介绍一下Intellij下maven的查看使用. 使用场景 当你想查看maven依赖的jar都有哪些,是否有冲突,冲 ...
- 机器学习方法(四):决策树Decision Tree原理与实现技巧
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 前面三篇写了线性回归,lass ...
- opencv python实用操作
画多边形 fillConvexPloy与fillConvexPloy的区别 fillConvexPloy 用来画单个凸多边形: 如果点集的连线不是凹多边形,则会找一个最小的凸多边形把该凹多边形包住画出 ...
- 前端读者 | 百度前端编码规范(JS)
本文来自:百度FEX 1 前言 JavaScript在百度一直有着广泛的应用,特别是在浏览器端的行为管理.本文档的目标是使JavaScript代码风格保持一致,容易被理解和被维护. 虽然本文档是针对J ...
- nodejs里的express自动刷新gulp-express使用【转载】
搬运自[http://blog.csdn.net/zhu_free/article/details/51476525] gulp-express实现实时刷新 本来使用gulp-connect可以创建本 ...