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是 ...
随机推荐
- MVC和MVVM的差别
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码 ...
- Linux find 命令的初步实现(C++)
Implement a myfind command following the find command in UNIX operating system. The myfind command s ...
- windows下使用mingw和msvc静态编译Qt5.15.xx
windows下使用mingw和msvc静态编译Qt5.15.xx 下载并安装相关依赖软件 Python version 2.7 https://www.python.org/downloads/ ( ...
- 没搞清楚网络I/O模型?那怎么入门Netty
微信搜索[阿丸笔记],关注Java/MySQL/中间件各系列原创实战笔记,干货满满. 本文是Netty系列笔记第二篇 Netty是网络应用框架,所以从最本质的角度来看,是对网络I/O模型的封装使用. ...
- dmp文件导入抽取方法
一.确认dmp文件.oracle客户端和服务端的字符集 (1)dmp文件字符集确认: 使用UE打开dmp文件查看文件第2个和第3个字节内容,这两个字节记录了dmp文件的字符集.如0354,然后用以下s ...
- PHP反序列化 - Pikachu
概述 序列化serialize()序列化说通俗点就是把一个对象变成可以传输的字符串,比如下面是一个对象: class S{ public $test="pikachu"; } $s ...
- bat批处理积累
1 ::所有命令不回显,包含echo off自身也不回显 2 @echo off 3 4 ::rem或双冒号都为注释行 5 6 rem 变量赋值,注意变量和等号之间不能有空格,等号后的空格会作为变量值 ...
- SparkStreaming和Kafka基于Direct Approach如何管理offset实现exactly once
在之前的文章<解析SparkStreaming和Kafka集成的两种方式>中已详细介绍SparkStreaming和Kafka集成主要有Receiver based Approach和Di ...
- 用CSS制做一个三角形!
用CSS制做一个三角形! <style> .outer { width: 0; height: 0; border-left: 10px solid transparent; border ...
- Maven 本地仓库
概述 Maven 的本地资源库是用来存储所有项目的依赖关系(插件 Jar 和其他文件,这些文件被 Maven 下载)到本地文件夹.很简单,当你建立一个 Maven 项目,所有相关文件将被存储在你的 M ...