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. js数组内置方法

    var arr = ['A','B','C','D']; length 计算数组的长度 arr.length//4   indexOf() 搜索一个指定的元素的位置 arr.indexOf('C'); ...

  2. 马上搞定Android平台的Wi-Fi Direct开发

    导语 移动互联网时代,很多用户趋向于将大量的资料保存在移动设备上.但在给用户带来便利的同时引发了一个新的问题——保存在移动设备上的资料该怎样共享出去?到了思考时间,普通青年这样想:折腾什么劲啊,直接用 ...

  3. 如何在C#中循环一个枚举

      在C#中要想迭代循环一个枚举,最容易想到的办法是直接进行循环,如下代码所示:   public enum Suit { Spades, Hearts, Clubs, Diamonds } publ ...

  4. PHP算法--将数字金额转换成大写金额

    最近在看一些PHP算法题,遇到一个将数字金额转换成大写金额的小算法题,这里贴出自己的一个例子. 注:这个小算法适用于10万以内的金额. <?php //$num = 12345.67; func ...

  5. Java优先级队列实现

    优先级队列数组实现: public class PriorityQueue { private int[] data; private int size; public PriorityQueue(i ...

  6. SharePoint移动客户端对比 ---Rshare 无疑是最好用的

    目前市面上SharePoint移动客户端确实不少,但经过使用后的对比,Rshare无论在界面上还是在操作性上都占据了优势.大家可以下载进行尝试.

  7. 20160510--hibernate懒加载问题

    懒加载 通过asm和cglib二个包实现:Domain是非final的. 1.session.load懒加载. 2.one-to-one(元素)懒加载: 必需同时满足下面三个条件时才能实现懒加载 (主 ...

  8. 20160505-hibernate入门2

    基本概念和CURD 开发流程 1由Domain object -> mapping->db.(官方推荐) 2由DB开始,用工具生成mapping和Domain object.(使用较多) ...

  9. cookie管理中的一些细节,转的

    1.domain表示的是cookie所在的域,默认为请求的地址,如网址为www.jb51.net/test/test.aspx,那么domain默认为www.jb51.net.而跨域访问,如域A为t1 ...

  10. oc语言学习之基础知识点介绍(二):类和对象的进一步介绍

    一.类.对象在内存中的存储 /* 内存分区: 栈:局部变量 堆:程序员自己写代码申请开辟的 程序员自己维护,编译器现在帮我们自动优化了,它在合适的给我们加上了释放空间的语句,所以我们现在写的对象不会造 ...