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是 ...
 
随机推荐
- 【C++】《C++ Primer 》第四章
			
第四章 表达式 一.基础 重载运算符:当运算符作用在类类型的运算对象时,用户可以自行定义其含义. 左值和右值: C中:左值可以在表达式左边,右值不能. C++中:当一个对象被用作右值的时候,用的是对象 ...
 - paramunittest参数化测试基础
			
samples: import paramunittestimport unittest@paramunittest.parametrized( (10,20), (30,40), # (100,20 ...
 - 【Linux】用find删除大于30天的文件
			
1.删除文件命令: find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; 实例命令:find /opt/soft/log/ -mt ...
 - 同步alv的前端显示和输出内表中的数据
			
在使用CL_GUI_ALV_GRID显示报表的时候,当我们使用了checkbox的时候,或者是有可编辑的字段,当我们 在前段修改了单元格内容的时候,后台的内表并不会自动的更新,此时需要我们调用一个方法 ...
 - 输入12V,输出12V的限流芯片
			
随着手机充电电流的提升,和设备的多样化,USB限流芯片就随着需求的增加而越来越多,同时为了更好的保护电子设备,需要进行一路或者多路的负载进行限流. USB限流芯片,5V输入 1, PW1502,常使用 ...
 - 阿里云OSS对象存储服务(二)
			
一.使用SDK 在OSS的概览页右下角找到"Bucket管理",点击"OSS学习路径" 点击"Java SDK"进入SDK开发文档 二.创建 ...
 - scrapy框架的中间件
			
中间件的使用 作用:拦截所有的请求和响应 拦截请求:process_request拦截正常的请求,process_exception拦截异常的请求 篡改请求的头信息 def process_reque ...
 - uni-app开发经验分享五: 解决三端页面兼容问题的方法
			
在做uni-app开发的过程中,我们最头疼可能不是开发的过程中的逻辑,而是最后要做的三端兼容测试和修改,在我开发的项目中,这一步都是最头疼和令人头秃的过程,这里总结一些个人开发遇到的问题,希望对大家有 ...
 - Kubernetes 存储简介
			
存储分类结构图 半持久化存储 1.EmptyDir EmptyDir是一个空目录,生命周期和所属的 Pod 是完全一致的,EmptyDir的用处是,可以在同一 Pod 内的不同容器之间共享工作过程中产 ...
 - Nifi组件脚本开发—ExecuteScript 使用指南(一)
			
Part 1 - 介绍 NiFi API 和 FlowFiles ExecuteScript 是一个万能的处理器,允许用户使用编程语言定义自己的数据处理功能, 在每一次 ExecuteScript p ...