Palindrome Function

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 863    Accepted Submission(s): 476

Problem Description
As we all know,a palindrome number is the number which reads the same backward as forward,such as 666 or 747.Some numbers are not the palindrome numbers in decimal form,but in other base,they may become the palindrome number.Like 288,it’s not a palindrome number under 10-base.But if we convert it to 17-base number,it’s GG,which becomes a palindrome number.So we define an interesting function f(n,k) as follow:
f(n,k)=k if n is a palindrome number under k-base.
Otherwise f(n,k)=1.
Now given you 4 integers L,R,l,r,you need to caluclate the mathematics expression ∑Ri=L∑rj=lf(i,j) .
When representing the k-base(k>10) number,we need to use A to represent 10,B to represent 11,C to repesent 12 and so on.The biggest number is Z(35),so we only discuss about the situation at most 36-base number.
 
Input
The first line consists of an integer T,which denotes the number of test cases.
In the following T lines,each line consists of 4 integers L,R,l,r.
(1≤T≤105,1≤L≤R≤109,2≤l≤r≤36)
 
Output
For each test case, output the answer in the form of “Case #i: ans” in a seperate line.
 
Sample Input
3
1 1 2 36
1 982180 10 10
496690841 524639270 5 20
 
Sample Output
Case #1: 665
Case #2: 1000000
Case #3: 447525746
 
Source
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <set>
  7. #include <map>
  8. #include <queue>
  9. #include <stack>
  10. #include <vector>
  11. using namespace std;
  12. #define mod 1000000007
  13. typedef long long ll;
  14. int t;
  15. int bit[];
  16. int ans[];
  17. ll dp[][][];
  18. ll dfs(int pos,int zero,int jinzhi,int flag,int beg)
  19. {
  20. if(pos<) return zero==;
  21. if(dp[pos][jinzhi][beg]!=-&&!flag&&!zero)
  22. return dp[pos][jinzhi][beg];
  23. ll sum=;
  24. int up=flag?bit[pos]:jinzhi-;
  25. for(int i=; i<=up; i++){
  26. if(zero&&i==)
  27. sum+=dfs(pos-,zero,jinzhi,flag&&i==up,beg);
  28. else{
  29. if(zero){
  30. ans[pos]=i;
  31. sum+=dfs(pos-,,jinzhi,flag&&i==up,pos);
  32. }
  33. else if(pos<(beg+)/){
  34. if(i==ans[beg-pos])
  35. sum+=dfs(pos-,,jinzhi,flag&&i==up,beg);
  36. }
  37. else{
  38. ans[pos]=i;
  39. sum+=dfs(pos-,,jinzhi,flag&&i==up,beg);
  40. }
  41. }
  42. }
  43. ans[pos]=-;
  44. if(!flag&&!zero)
  45. dp[pos][jinzhi][beg]=sum;
  46. return sum;
  47. }
  48. ll slove (int x,int jinzhi){
  49. int len=;
  50. while(x)
  51. {
  52. bit[len++]=x%jinzhi;
  53. x/=jinzhi;
  54. }
  55. return dfs(len-,,jinzhi,,);
  56. }
  57. int main()
  58. {
  59. scanf("%d",&t);
  60. memset(dp,-,sizeof(dp));
  61. int ce=;
  62. while(t--){
  63. int L,R,l,r;
  64. scanf("%d %d %d %d",&L,&R,&l,&r);
  65. ll ans=;
  66. for(int i=l; i<=r; i++){
  67. ll sum=slove(R,i)-slove(L-,i);
  68. ans=ans+sum*i+(R-L+-sum);
  69. }
  70. printf("Case #%d: %lld\n",ce++,ans);
  71. }
  72. return ;
  73. }

HDU 6156 数位dp的更多相关文章

  1. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做 ...

  2. hdu 4507 数位dp(求和,求平方和)

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...

  3. hdu 4352 数位dp + 状态压缩

    XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. hdu:2089 ( 数位dp入门+模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 数位dp的模板题,统计一个区间内不含62的数字个数和不含4的数字个数,直接拿数位dp的板子敲就行 ...

  5. HDU 4352 XHXJ's LIS HDU(数位DP)

    HDU 4352 XHXJ's LIS HDU 题目大意 给你L到R区间,和一个数字K,然后让你求L到R区间之内满足最长上升子序列长度为K的数字有多少个 solution 简洁明了的题意总是让人无从下 ...

  6. hdu 3709 数位dp

    数位dp,有了进一步的了解,模板也可以优化一下了 题意:找出区间内平衡数的个数,所谓的平衡数,就是以这个数字的某一位为支点,另外两边的数字大小乘以力矩之和相等,即为平衡数例如4139,以3为支点4*2 ...

  7. HDU 2089 数位dp入门

    开始学习数位dp...一道昨天看过代码思想的题今天打了近两个小时..最后还是看了别人的代码找bug...(丢丢) 传说院赛要取消 ? ... 这么菜不出去丢人也好吧~ #include<stdi ...

  8. HDU 2089 数位dp/字符串处理 两种方法

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. HDU 5808[数位dp]

    /* 题意: 给你l和r,范围9e18,求l到r闭区间有多少个数字满足,连续的奇数的个数都为偶数,连续的偶数的个数都为奇数. 例如33433符合要求,44不符合要求.不能含有前导零. 思路: 队友说是 ...

随机推荐

  1. JavaEE笔记(十三)

    #单一职责原则 一个类只做一件事 #开闭原则 拓展开,修改源码闭 #动态代理 1 基于接口的方式 jdk的动动代理2 基于类的方式 cglib的代理 #SSH整合 1.spring(容器)    1& ...

  2. Windows下TeX Live + Sublime Text 3 + Sumatra PDF配置

    本文写给我的师弟们,如何自己动手配置LaTeX环境(通过LeX Live + Sublime Text 3 + Sumatra PDF). 1.TeX Live 配置 首先从TeX Live 下载IS ...

  3. PHP 练习(租房子)

    一.题目要求 二.题目做法 1.建立数据库 2.封装类文件 <?php class DBDA { public $fuwuqi="localhost"; //服务器地址 pu ...

  4. 基于Boost库的HTTP Post函数

    两个函数的区别: 提交表单数据和提交文本数据 表单数据: request_stream << "Content-Type: application/x-www-form-urle ...

  5. [CF1007D]Ants[2-SAT+树剖+线段树优化建图]

    题意 我们用路径 \((u, v)\) 表示一棵树上从结点 \(u\) 到结点 \(v\) 的最短路径. 给定一棵由 \(n\) 个结点构成的树.你需要用 \(m\) 种不同的颜色为这棵树的树边染色, ...

  6. onSaveInstanceState和onRestoreInstanceState触发的时机

    先看Application Fundamentals上的一段话: Android calls onSaveInstanceState() before the activity becomes vul ...

  7. pie的绕过方式

    目标程序下载 提取码:qk1y 1.检查程序开启了哪些安全保护机制 pie机制简介 PIE(position-independent executable) 是一个针对代码段.text, 数据段.*d ...

  8. raft--分布式一致性协议

    0. 写在前面的话 一直从事分布式对象存储工作,在分布式对象存储的运营,开发等工作中,数据一致性是至关重要的.因此想写一篇关于分布式一致性的文章.一来,可以和大家分享.二来,可以提高自己的文字提炼能力 ...

  9. 微软职位内部推荐-Senior Software Engineer-Eco

    微软近期Open的职位: The MOD Ecosystem team is dedicated to expanding the reach and value of Office by enabl ...

  10. Vim操作指南

    vim具有6种基本模式和5种派生模式. 基本模式 普通模式 插入模式 可视模式 选择模式 命令行模式 Ex模式 派生模式 操作符等待模式 插入普通模式 插入可视模式 插入选择模式 替换模式 1.移动光 ...