Magic Numbers

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

直接数位DP;前两维的大小就是mod m的大小,注意在判断是否f[pos][mod] != -1之前,要判断是否为边界,否则会出现重复计算;

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define MS1(a) memset(a,-1,sizeof(a))
const int N = ,MOD = 1e9+;
char a[N],b[N];
int f[N][N][],n,m,d;
int dfs(char* s,int p,int mod,int on = )
{
if(p == n) return mod == ;
int& ans = f[p][mod][on];
if(ans != -) return ans;
ans = ;
for(int i = ;i <= (on?s[p] - '':);i++){
if(p% && i != d) continue;
if(p% == && i == d) continue;
(ans += dfs(s,p+,(mod*+i)%m,on && i == s[p] - '')) %= MOD;
}
return ans;
}
int calc(char* s)
{
MS1(f);
return dfs(s,,);
}
int main()
{
scanf("%d%d",&m,&d);
scanf("%s%s",a,b);
n = strlen(a);
for(int i = n-;i >= ;i--){ // a - 1;
if(a[i] == '') a[i] = '';
else{a[i]--;break;}
}
printf("%d",(calc(b)-calc(a)+MOD)%MOD);
}

Educational Codeforces Round 8 D. Magic Numbers的更多相关文章

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

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

  2. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  3. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  4. Educational Codeforces Round 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  5. Educational Codeforces Round 2 A. Extract Numbers

    打开题目链接 题意:输入一个字符串,用,或:分隔输出字符串和整数(不含前导0和浮点数) ACcode: #include <iostream> #include <cstdio> ...

  6. Educational Codeforces Round 60 D. Magic Gems

    易得递推式为f[i]=f[i-1]+f[i-M] 最终答案即为f[N]. 由于N很大,用矩阵快速幂求解. code: #include<bits/stdc++.h> using names ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  8. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

随机推荐

  1. C#_datatable 写入大量数据_BulkCopy

    using Microsoft.Win32; using System; using System.Collections.Generic; using System.Configuration; u ...

  2. error “base class has incomplete type”

    error "base class has incomplete type" 如果base.h是你的基类,那么在子类derive中,写成如下形式: class base; clas ...

  3. 一个响应式数据库框架SQLBrite,完美解决数据库和UI的同步更新!

    相信小伙伴们在开发中或多或少都可能遇到过这样的问题:打开一个应用后,为了快速响应,先将数据库中的数据呈现给用户,然后再去网络上请求数据,请求成功之后将数据缓存至数据库,同时更新UI,但是我们经常会这样 ...

  4. 在IOS中 NSRange类详解

    NSRange的定义 typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; NSRange是一个结构 ...

  5. hadoop学习记录(零)

    这个博客开通快一年了,但是由于种种原因,始终没有能够养成定期更新的习惯. 最近完成了比赛的项目,向除了android开发以外再拓宽一下自己的技能树. 最近购买了<java8函数式编程>和& ...

  6. Android_Intent_data_type

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  7. Struts2的工作流程

    Struts2如何实现MVC Struts2的参数封装: Struts2的运行原理图:

  8. 浅谈用java解析xml文档(三)

    接上一篇,本文介绍使用JDOM解析xml文档, 首先我们还是应该知道JDOM从何而来,是Breet Mclaughlin和Jason Hunter两大Java高手的创作成果,2000年初, JDOM作 ...

  9. 动态上传多个文件(asp)

    CreateElements.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...

  10. GestureDetector类及其用法

    转载子:http://www.cnblogs.com/rayray/p/3422734.html 项目中有需求:针对一个imageview,点击需要查看大图,左右滑动能执行翻页. 自己对手势这一块并不 ...