Palindrome Function

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

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 <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = ;
  6. const int maxm = ;
  7. const LL mod = 1e9+;
  8. int digit[maxn], revert[maxn];
  9. LL L, R, l, r;
  10. LL dp[maxm][maxm][maxn][];
  11.  
  12. LL dfs(int k, int s, int l, bool ok, bool lim) {
  13. if(l < ) {
  14. if(ok) return k;
  15. return ;
  16. }
  17. if(!lim && ~dp[k][s][l][ok]) return dp[k][s][l][ok];
  18. int pos = lim ? digit[l] : k - ;
  19. LL ret = ;
  20. for(int i = ; i <= pos; i++) {
  21. revert[l] = i;
  22. if(i == && s == l) {
  23. ret += dfs(k, s-, l-, ok, lim&&(i==pos));
  24. }
  25. else if(ok && l < (s + ) / ) {
  26. ret += dfs(k, s, l-, i==revert[s-l], lim&&(i==pos));
  27. }
  28. else {
  29. ret += dfs(k, s, l-, ok, lim&&(i==pos));
  30. }
  31. }
  32. if(!lim) dp[k][s][l][ok] = ret;
  33. return ret;
  34. }
  35.  
  36. LL f(LL n, LL k) {
  37. if(n == ) return k;
  38. int pos = ;
  39. while(n) {
  40. digit[pos++] = n % k;
  41. n /= k;
  42. }
  43. return dfs(k, pos-, pos-, , );
  44. }
  45.  
  46. signed main() {
  47. int T, tt = ;
  48. scanf("%d", &T);
  49. memset(dp, -, sizeof(dp));
  50. while(T--) {
  51. scanf("%lld%lld%lld%lld",&L,&R,&l,&r);
  52. LL ret = ;
  53. for(int i = l; i <= r; i++) {
  54. ret += f(R, i) - f(L-, i);
  55. }
  56. printf("Case #%d: %lld\n", tt++, ret);
  57. }
  58. return ;
  59. }
 

HDU 6156 回文 数位DP(2017CCPC)的更多相关文章

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

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  2. bzoj2084/luoguP3501 [Poi2010]Antisymmetry(回文自动机+dp)

    bzoj2084/luoguP3501 [Poi2010]Antisymmetry(回文自动机+dp) bzoj Luogu 对于一个01字符串,如果将这个字符串0和1取反后,再将整个串反过来和原串一 ...

  3. bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp)

    bzoj4044/luoguP4762 [Cerc2014]Virus synthesis(回文自动机+dp) bzoj Luogu 你要用ATGC四个字母用两种操作拼出给定的串: 1.将其中一个字符 ...

  4. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  5. HDU2205 又见回文(区间DP)

    题意:给定两个字符串(可能为空串),求这两个串交叉组成新串的子串中的回文串的最大长度. 布尔型变量dp[i][j][k][l]表示串a从i到j,b从k到l能否组成新串,初始化为false,则采取区间动 ...

  6. 51nod 1092 回文字符串 (dp)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i ...

  7. bzoj 1138: [POI2009]Baj 最短回文路 dp优化

    1138: [POI2009]Baj 最短回文路 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 161  Solved: 48[Submit][Sta ...

  8. 还是回文(dp)

    还是回文 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 判断回文串很简单,把字符串变成回文串也不难.现在我们增加点难度,给出一串字符(全部是小写字母),添加或删除一个字 ...

  9. hdu 1282回文数猜想

    http://acm.hdu.edu.cn/showproblem.php?pid=1282 Problem Description 一个正整数,如果从左向右读(称之为正序数)和从右向左读(称之为倒序 ...

随机推荐

  1. PHP 中获取文件名及路径

    1. basename("/mnt/img/image01.jpg")函数:得到文件名;输出结果为:image01.jpg. 使用 basename($uriString) 我们可 ...

  2. CCflow6 的使用

    sELECT * FROM WF_GENERWORKFLOW        WHERE WorkID=00000 -- 查看流程状态 wf_selectaccpter --查看流程某个节点下的人员 M ...

  3. LitePal——安卓数据库library

    简介:一个让开发者使用SQLite数据库更加容易的库文件 LitePal for Android,项目地址:点击打开 LitePal是一个开源的android库,它让开发者使用SQLite数据变得容易 ...

  4. Ngnix服务器详解(Windows版本)(非原创)

    文章大纲 一.Ngnix简介二.Ngnix安装三.Ngnix之静态资源访问四.Ngnix正向代理与反向代理五.Ngnix之虚拟主机配置六.Ngnix之负载均衡七.Ngnix之访问控制八.Ngnix日志 ...

  5. jar 压缩 解压 war包

    Win+R 输入cmd进入命令行,进入到源码所在目录.所用工具,jdk自带的jar.exe 打包命令:jar -cvf xxx.war * 解包命令: jar -xvf xxx.war * 参数 说明 ...

  6. #include stdio.h(1)

    #include <stdio.h> int main() { //************一.运算符********** //1.赋值运算符 = ; //赋值运算符表示的是将等号右边的赋 ...

  7. Vue系列(2):Vue 安装

    前言:关于页面上的知识点,如有侵权,请看 这里 . 关键词:小白.Vue 安装.Vue目录结构.Vue 构建页面流程 ? 初学者安装 vue 用什么好 大家都知道,学 Vue 最好还是去官网学,官网写 ...

  8. UOJ#122【NOI2013】树的计数

    [NOI2013]树的计数 链接:http://uoj.ac/problem/122 按BFS序来,如果$B_i$与$B_{i-1}$必须在同一层,那么贡献为0,必须在不同层那么贡献为1,都可以贡献为 ...

  9. TFS看板的设计

    列 产品开发的整个流程如下图,将流程配置到看板的列: 需求池-->就绪-->开发-->测试-->待验收 -->待发布 -->已关闭 一般将Bug和需求放在一块看版上 ...

  10. hangfire使用

    1 . NuGet 命令行执行 Install-Package Hangfire2.首先在ConfigureServices 方法中注册服务: services.AddHangfire(r=>r ...