题意:

求在[a,b](a,b不含前导0)中的d−magic数中有多少个是m的倍数。

分析:

计数dp

Let’s call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else.

仔细读题并观察例子就可以明确d−magic数从左到右所有偶数位置上都是d,奇数位上不能是d

求[a,b],即可转化为求[1,a],[1,b]中的满足条件的数,最后相减,注意判断a是否满足条件。

设dp[i][j][k]表示前缀为i位,余数为j,等于(1)或者小于(0)上限的方案数。容易想到状态转移的过程,注意分填入的数字小于和等于上限对应元素两种情况处理。

代码:

#include<cstdio>
#include<cstring>
const int mod = 1e9+7, maxn = 2005;
int m, d;
int dp[maxn][maxn][2];
char a[maxn], b[maxn];
int num[maxn];
int solve(char* A)
{
memset(dp, 0, sizeof(dp));
memset(num, 0, sizeof(num));
int cnt = strlen(A);
for(int i = 0; i < cnt; i++){
num[i+1] = A[i] - '0';
}
for(int i = 1; i<=num[1];i++){
if(i == d)continue;
if(i < num[1]){
dp[1][i%m][0]++;
}else{
dp[1][i%m][1]++;
}
} for(int i = 2; i <= cnt; i++){
for(int j = 0; j < m; j++){
if(i%2==0){
dp[i][(j * 10 + d)%m][0] = (dp[i][(j * 10 + d)%m][0] + dp[i - 1][j][0])%mod;
if(d < num[i])
dp[i][(j * 10 + d)%m][0] = ( dp[i][(j * 10 + d)%m][0] + dp[i - 1][j][1])%mod;
else if(d == num[i])
dp[i][(j * 10 + d)%m][1] = ( dp[i][(j * 10 + d)%m][1] + dp[i-1][j][1])%mod;
}else{
for(int k = 0; k < 10; k++){
if(k == d) continue;
dp[i][(j * 10 + k)%m][0] = (dp[i][(j * 10 + k)%m][0] + dp[i - 1][j][0])%mod;
if(k < num[i])
dp[i][(j * 10 + k)%m][0] = ( dp[i][(j * 10 + k)%m][0] + dp[i - 1][j][1])%mod;
else if(k == num[i])
dp[i][(j * 10 + k)%m][1] = ( dp[i][(j * 10 + k)%m][1] + dp[i-1][j][1])%mod;
}
}
}
} return (dp[cnt][0][0] + dp[cnt][0][1])%mod; }
int is(char* a)
{
int res = 0;
for(int i = 0; i < strlen(a); i++){
int a1 = a[i] - '0';
if(i%2 == 0){
if(a1 == d) return 0;
}
if(i%2==1){
if(a1 != d) return 0;
}
res = (res*10 +a1)%m;
}
return res == 0;
}
int main (void)
{
scanf("%d%d",&m,&d);
scanf("%s%s", a, b);
printf("%d\n", (solve(b) - solve(a) +is(a)+mod)%mod) ;
return 0;
}

Codeforces 628D Magic Numbers的更多相关文章

  1. CodeForces 628D Magic Numbers (数位dp)

    题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...

  2. Codeforces 320A Magic Numbers

    因为晚上有一个cf的比赛,而自己从来没有在cf上做过题,就找了道题熟悉一下. 题目大意:给一个数,判断是否能由1,14,144三个数连接得到. 代码如下: #include <stdio.h&g ...

  3. 628D Magic Numbers

    传送门 题目大意 定义n-magic为从左往右,偶数位置均为n,奇数位置不为n的一类数.求出[a,b]内所有可被m整除的d-magic个数. 分析 显然是数位dp,我们用dp[i][j][k]表示考虑 ...

  4. Magic Numbers CodeForces - 628D

    Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...

  5. Educational Codeforces Round 8 D. Magic Numbers 数位DP

    D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...

  6. Codeforces CF#628 Education 8 D. Magic Numbers

    D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. Educational Codeforces Round 8 D. Magic Numbers

    Magic Numbers 题意:给定长度不超过2000的a,b;问有多少个x(a<=x<=b)使得x的偶数位为d,奇数位不为d;且要是m的倍数,结果mod 1e9+7; 直接数位DP;前 ...

  8. CodeForces 628 D Magic Numbers 数位DP

    Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...

  9. 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 ...

随机推荐

  1. Jauery 中Ajax的几种异步请求

       以下介绍Jquery中  Post   Get   Ajax几种异步请求的使用方法  <%@ Page Language="C#" AutoEventWireup=&q ...

  2. 仿微信右滑关闭Activity

    SwipeBackLayout 1.AS添加依赖 compile 'me.imid.swipebacklayout.lib:library:1.0.0' eclipse 想办法下载库工程,以库工程形式 ...

  3. 解决VS2010提示warning C4068: 未知的杂注

    出现原因是#pragma声明问题,加上#pragma warning(disable:4068)即可 #pragma warning(disable:4068)#pragma execution_ch ...

  4. SCCM大致安装过程,参考前辈教程完成部署

    本安装sccm主站点服务器准备 参考:http://stephen1991.blog.51cto.com/8959108/1529864 1.  准备三台服务器 ,注:所有服务器需要安装 .net3. ...

  5. Proc datasets

    作用:控制数据集.Datasets 过程运行结果不输出,结果只有在日志里才能看到. 基本语法: proc datasets lib=work; quit; 用法: 1. 更改数据集 proc data ...

  6. 深入理解python对象及属性

    类属性和实例属性首先来看看类属性和类实例的属性在python中如何存储,通过__dir__方法来查看对象的属性 >>> class Test(object): pass>> ...

  7. date - 打印或设置系统日期和时间

    总览 date [选项]... [+格式] date [选项] [MMDDhhmm[[CC]YY][.ss]] 描述 根据指定格式显示当前时间或设置系统时间. -d, --date=STRING 显示 ...

  8. ALTER SCHEMA - 修改一个模式的定义

    SYNOPSIS ALTER SCHEMA name RENAME TO newname DESCRIPTION 描述 ALTER SCHEMA 修改一个模式的定义. 现在它唯一的功能就是重命名模式. ...

  9. 解决webstorm中vue语法没有提示

    首先看看webstrom内置的vue插件,打上勾,没有这个选项就要自己去下载插件了 如果插件还是没有语法提示,可以用下面的方法,自己添加语法进去搜索 unknown HTML tag attribut ...

  10. 【原】jq简易教程

    https://www.jianshu.com/p/3522fe70de19 https://www.jianshu.com/p/6de3cfdbdb0e 1. jq简介 jq可以对json数据进行分 ...