Magic Numbers

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

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

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define inf 0x3f3f3f3f
  4. #define MS1(a) memset(a,-1,sizeof(a))
  5. const int N = ,MOD = 1e9+;
  6. char a[N],b[N];
  7. int f[N][N][],n,m,d;
  8. int dfs(char* s,int p,int mod,int on = )
  9. {
  10. if(p == n) return mod == ;
  11. int& ans = f[p][mod][on];
  12. if(ans != -) return ans;
  13. ans = ;
  14. for(int i = ;i <= (on?s[p] - '':);i++){
  15. if(p% && i != d) continue;
  16. if(p% == && i == d) continue;
  17. (ans += dfs(s,p+,(mod*+i)%m,on && i == s[p] - '')) %= MOD;
  18. }
  19. return ans;
  20. }
  21. int calc(char* s)
  22. {
  23. MS1(f);
  24. return dfs(s,,);
  25. }
  26. int main()
  27. {
  28. scanf("%d%d",&m,&d);
  29. scanf("%s%s",a,b);
  30. n = strlen(a);
  31. for(int i = n-;i >= ;i--){ // a - 1;
  32. if(a[i] == '') a[i] = '';
  33. else{a[i]--;break;}
  34. }
  35. printf("%d",(calc(b)-calc(a)+MOD)%MOD);
  36. }

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. python--while循环

    1.最简单的while True循环 count = while True : : print('hello',count) break count += hello 2.利用while循环写一个猜年 ...

  2. CSS——选择器

    css选择器 css选择器可分为:标签(元素)选择器,ID选择器,类选择器,属性选择器,后代选择器,子代选择器,相邻兄弟选择器和兄弟选择器.... 标签选择器: //E{attr:value;attr ...

  3. Android_Dialog

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

  4. Fortify规则与CERT JAVA 安全编程规范的对照表

    Fortify规则与CERT JAVA 安全编程规范的对照表http://www.automationqa.com/forum.php?mod=viewthread&tid=4353& ...

  5. [原]unity中WWW isDone方法只能在主线程中调用

    项目中要使用动态加载,原计划是生成WWW对象后,放到一个容器里.由一个独立线程轮询容器里的对象,如果www.isDone为true时,回调一个接口把结果交给请求方. new Thread( new T ...

  6. mstsc 终端服务器超出了最大允许连接的解决办法

    终端服务器超出了最大允许连接的解决办法   win7系统:运行,输入mstsc /v xxx.xxx.xxx.xxx /admin win2003系统:运行,输入mstsc /v xxx.xxx.xx ...

  7. 多个DIV让float:left属性,最后一个DIV填满剩余的部分

    <DIV style="border:1px solid red; overflow:hidden;zoom:1;">      <DIV style='floa ...

  8. 20160506-hibernate入门

    HQL和Criteria HQL(Hibernate Query Language) 面向对象的查询语言,与SQL不同,HQL中的对象名是区分大小写的(除了JAVA类和属性其他部分不区分大小写):HQ ...

  9. C++实现元组

    一般我们使用struct时需要在头文件中定义,例如 struct Example { int a; char b; ... }; 这样将数据打包好必须在程序运行之前将其定义好,如果有需求在程序运行时添 ...

  10. System.Data.SqlClient.SqlError: 对文件……的目录查找失败[转]

    System.Data.SqlClient.SqlError: 对文件……的目录查找失败,出现操作系统错误 3 的处理办法 在还原SQL SERVER数据库时出现了查找目录失败的原因,困扰了我一个多小 ...