题目链接http://poj.org/problem?id=2117

题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除。范围2^64。

解题思路

一开始SB地开了10维数组记录情况。

首先要求能被各位上的数整除,可以转化为被一个数整除问题。

这个数就是各位上数的最小公倍数LCM(不是GCD)。

其次,处理整除问题,得转化成数位DP的余数模板。1~9的LCM最大是2520, 那么%2520,让其可以开数组进行记忆化搜索。

最后, 对于不能%2520最后结果,再%各个数位累计过来的LCM。

这样下来,需要开20*2520*2520的数组,往CF上一交你会发现MLE。

仔细观察每次的LCM,其范围是1~2520没错,但是都是整除gcd的结果(LCM=a*b/gcd(a,b) ),也就是说所有LCM都是某个数的约数。

这个数其实就是2520。所以DP之前,为2520打个表,把LCM给离散化Hash。这样其实只有48个LCM了。数组开20*2520*50即可。

注意结果是int64。

  1. #include "cstdio"
  2. #include "cstring"
  3. using namespace std;
  4. #define LL long long
  5. LL dp[][][],digit[],Hash[];
  6. int gcd(int a,int b) {return b==?a:gcd(b,a%b);}
  7. int lcm(int a,int b) {return a*b/gcd(a,b);}
  8. LL dfs(int len,int Remain,int Lcm,bool fp)
  9. {
  10. if(!len) return Remain%Lcm?:;
  11. printf("%d\n",Lcm);
  12. if(!fp&&dp[len][Remain][Hash[Lcm]]!=-) return dp[len][Remain][Hash[Lcm]];
  13. LL ret=,fpmax=fp?digit[len]:;
  14. for(int i=;i<=fpmax;i++)
  15. ret+=dfs(len-,(Remain*+i)%,i==?Lcm:lcm(Lcm,i),fp&&i==fpmax);
  16. if(!fp) dp[len][Remain][Hash[Lcm]]=ret;
  17. return ret;
  18. }
  19. LL f(long long x)
  20. {
  21. int len=;
  22. while(x)
  23. {
  24. digit[++len]=x%;
  25. x/=;
  26. }
  27. return dfs(len,,,true);
  28. }
  29. int main()
  30. {
  31. //freopen("in.txt","r",stdin);
  32. int T;
  33. LL l,r,cnt=;
  34. scanf("%d",&T);
  35. memset(dp,-,sizeof(dp));
  36. for(int i=;i*i<=;i++)
  37. {
  38. if(%i==)
  39. {
  40. Hash[i]=cnt++;
  41. if(i*i!=) Hash[/i]=cnt++;
  42. }
  43. }
  44. while(T--)
  45. {
  46. scanf("%I64d%I64d",&l,&r);
  47. LL res=f(r)-f(l-);
  48. printf("%I64d\n",res);
  49. }
  50. }
2908091(#) neopenx CodeForces 55D Accepted 19800 780 GNU C++ 4.6 1121
2014-10-30 19:41:38

Codeforces 55D (数位DP+离散化+数论)的更多相关文章

  1. codeforces 55D 数位dp

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  3. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  4. Codeforces 628D 数位dp

    题意:d magic number(0<=d<9)的意思就是一个数,从最高位开始奇数位不是d,偶数位是d 题目问,给a,b,m,d(a<=b,m<2000)问,a,b之间有多少 ...

  5. codeforces 401D (数位DP)

    思路:很明显的数位dp,设dp[i][j] 表示选取数字的状态为i,模m等于j的数的个数,那么最后的答案就是dp[(1<<n)-1][0].状态转移方程就是,dp[i|(1<< ...

  6. Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)

    大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...

  7. Codeforces - 914C 数位DP

    题意有点难以描述,简略的就是给定一个二进制\(n\),每一步操作能使\(n\)的位为1的数的和转化为一个十进制,然后转化为该数的二进制再进行相同的操作 查询\([0,n]\)中操作数恰好为\(k\)的 ...

  8. Codeforces #55D (数位dp+离散化)

    Description Volodya is an odd boy and his taste is strange as well. It seems to him that a positive ...

  9. Codeforces 13C Sequence --DP+离散化

    题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为 ...

随机推荐

  1. Java 23种设计模式

    转自: http://zz563143188.iteye.com/blog/1847029 ; i<count; i++){ list.add(new MailSender()); } } pu ...

  2. HDOJ 1878 欧拉回路 nyoj 42一笔画问题

    #include<cstdio> #include<cstring> ]; int find(int x) { if(visited[x]!=x) return find(vi ...

  3. 【Linux】为啥查某个进程的线程,查出来的所有线程的pid不一样啊

    楼上说的linux线程和进程是一样的,这个说法是错误的. 看了楼主的问题,感觉楼主是被PID给弄混了,线程进程都会有自己的ID,这个ID就叫做PID,PID是不特指进程ID,线程ID也可以叫做PID. ...

  4. Master-Worker模式

    并行程序设计模式--Master-Worker模式 简介 Master-Worker模式是常用的并行设计模式.它的核心思想是,系统有两个进程协议工作:Master进程和Worker进程.Master进 ...

  5. ext树表

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2UAAAHwCAIAAACpIFDdAAAgAElEQVR4nOy9f5Qb5ZnvWWQZlnO5Oc ...

  6. LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件

    //file_op.c #include <string.h> #include <stdio.h> #include <stdlib.h> struct info ...

  7. Android与后台数据交互学习

    摘要 任何系统在没有用户登录就可操作数据是非常危险的,链接客户端与服务器的是用户登录后的session,用户登录系 统后存在一个sessionid,这个sessionid就是客户端的cookie,客户 ...

  8. .pro配置选项

    在Qt Creator的项目中添加头文件和库   在Qt Creator中的工程中,工程通过.pro文件管理. 额外需要连接的连接库 unix:LIBS += -L your_lib_path -ly ...

  9. hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  10. STL —— STL六大组件

    注:以下内容摘自 http://blog.csdn.net/byxdaz/article/details/4633826 STL六大组件 容器(Container) 算法(Algorithm) 迭代器 ...