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

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

2017中国大学生程序设计竞赛 - 网络选拔赛

//题意:给出四个整数,L,R,l,r, ∑(L<=i<=R)∑(l<=j<=r) f(i,j) , f(i,j)代表 i 在 j 进制下是否为回文数,是为 k,否则值为 1。

//题解:枚举进制,那么就变为 cal(R,k) - cal(L-1,k) { cal(i,j)表1--i,在k进制下的回文数个数*k+其余数的个数 } 的累加了

计算小于等于一个数的回文数个数并不难,想清楚就好,如果某数前半部分小于该数的前半部分,那一定能构造出来,所以等于时特判一下即可

  1. # include <cstdio>
  2. # include <cstring>
  3. # include <cstdlib>
  4. # include <iostream>
  5. # include <vector>
  6. # include <queue>
  7. # include <stack>
  8. # include <map>
  9. # include <bitset>
  10. # include <sstream>
  11. # include <set>
  12. # include <cmath>
  13. # include <algorithm>
  14. # pragma comment(linker,"/STACK:102400000,102400000")
  15. using namespace std;
  16. # define LL long long
  17. # define pr pair
  18. # define mkp make_pair
  19. # define lowbit(x) ((x)&(-x))
  20. # define PI acos(-1.0)
  21. # define INF 0x3f3f3f3f3f3f3f3f
  22. # define eps 1e-
  23. # define MOD
  24.  
  25. inline int scan() {
  26. int x=,f=; char ch=getchar();
  27. while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
  28. while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
  29. return x*f;
  30. }
  31. inline void Out(int a) {
  32. if(a<) {putchar('-'); a=-a;}
  33. if(a>=) Out(a/);
  34. putchar(a%+'');
  35. }
  36. #define MX 500
  37. /**************************/
  38.  
  39. LL cal(LL x,LL k)
  40. {
  41. int len=;
  42. LL temp=x;
  43. int dat[];
  44. while (temp)
  45. {
  46. dat[len++]=temp%k;
  47. temp/=k;
  48. }
  49.  
  50. LL ans = ,sum = k-,base=;
  51. for (int i=;i<len;i++)
  52. {
  53. ans+=sum-base;
  54. if (i%==)
  55. {
  56. base=sum;
  57. sum=sum*k+k-;
  58. }
  59. }
  60. LL sb=;
  61. for (int i=len-;i>=len/;i--)
  62. sb = sb*k+dat[i];
  63. LL now=sb;
  64. int wei;
  65. if (len%) wei=len/+;
  66. else wei = len/;
  67. for (int i=wei;i<len;i++)
  68. sb = sb*k+dat[i];
  69.  
  70. if (sb<=x)
  71. ans+=now-base;
  72. else
  73. ans+=now-base-;
  74. return ans*(k-)+x;
  75. }
  76.  
  77. int main()
  78. {
  79. int T = scan();
  80. for (int cnt=;cnt<=T;cnt++)
  81. {
  82. int L,R,l,r;
  83. scanf("%d%d%d%d",&L,&R,&l,&r);
  84. LL ans = ;
  85. for (int i=l;i<=r;i++)
  86. {
  87. ans +=cal(R,i)-cal(L-,i);
  88. }
  89. printf("Case #%d: %lld\n",cnt,ans);
  90. }
  91. return ;
  92. }

Palindrome Function的更多相关文章

  1. HDU - 6156 2017CCPC网络赛 Palindrome Function(数位dp找回文串)

    Palindrome Function As we all know,a palindrome number is the number which reads the same backward a ...

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

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

  3. Palindrome Function HDU - 6156(数位dp)

    要求m-n内在l-r进制下的是回文数的总个数. dp[进制][从第j为开始][目前到达第k位] = 总的方案数 dfs枚举目前的到达的位置,这个数开始的位置,进制,前导零,限制条件,然后枚举的时候如果 ...

  4. HDU 6156 Palindrome Function

    http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:$f(n,k)$表示判断n在k进制下是否是回文串,如果是,则返回k,如果不是,则返回1.现在要计算$ ...

  5. HDU 6156 Palindrome Function(数位DP)题解

    思路: 数位dp的操作是dfs+记忆化,我们dp开四维:位置,长度,进制,是否回文.然后每次暴搜记录下每个位置的数字是什么,搜到对称轴另一边需要检查是否符合回文. 终于把友谊赛的题目都补完了...没做 ...

  6. HDU-6156 Palindrome Function(数位DP)

    一.题目 二.思路 1.这是很明显的数位DP: 2.和以往数位DP不同的是,这里带了个进制进来,而以往做是纯十进制下或者纯二进制下做操作.但是,不管多少进制,原理都是一样的: 3.这里有个小坑,题目中 ...

  7. 【数位DP】HDU 6156 Palindrome Function

    http://acm.hdu.edu.cn/showproblem.php?pid=6156 [AC] #include<bits/stdc++.h> using namespace st ...

  8. 【2017中国大学生程序设计竞赛 - 网络选拔赛】Palindrome Function

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6156 [题意] 已知函数f(x, k),如果10进制数x在k进制下是个回文数,那么f(x, k)值为k, ...

  9. PPAPI插件开发指南

    转载请注明出处:http://www.cnblogs.com/fangkm/p/4401075.html 前言 插件一直是浏览器的重要组成部分,丰富浏览器的运行能力,实现一些HTML+JS实现不了本地 ...

随机推荐

  1. 炫酷的sublimeText开发工具 快捷键总结

     sublimeText3绝对是写前端程序的利器.占内存小.启动快.代码提示功能齐全,界面酷炫并且再也不怕断电.了 网上搜罗一些快捷键总结一下.先来张图: (事实上sublimeText3也能够写 ...

  2. 普通用户 crontab 任务不运行

    今天发如今linux下,普通用户的crontab任务不运行.网上搜了好多.好多说要在运行的脚本前面加上例如以下内容 if [ -f ~/.bash_profile ]; then   . ~/.bas ...

  3. windows盘符操作

    subst subst x: /d subst y: e:\

  4. The fundamental differences between "GET" and "POST"

    The HTML specifications technically define the difference between "GET" and "POST&quo ...

  5. 《深入PHP:面向对象、模式与实践》(一)

    第1章  PHP:设计与管理 本章主要介绍了本书有哪些内容. 第2章  PHP与对象 本章总结了PHP面向对象特性的发展过程,逐步介绍对象的概念. PHP/FI:支持变量.关联数组和函数.没有对象. ...

  6. js 页面离开前触发事件

    当前窗口载入新的dom文档前发生 window.onbeforeunload = function(event) { return confirm("确定离开此页面吗?");

  7. php模板原理PHP模板引擎smarty模板原理浅谈

    mvc是开发中的一个伟大的思想,使得开发代码有了更加清晰的层次,让代码分为了三层各施其职.无论是对代码的编写以及后期的阅读和维护,都提供了很大的便利. 我们在php开发中,视图层view是不允许有ph ...

  8. APACHE KYLIN™ 概览

    APACHE KYLIN™ 概览 Apache Kylin™是一个开源的分布式分析引擎,提供Hadoop之上的SQL查询接口及多维分析(OLAP)能力以支持超大规模数据,最初由eBay Inc. 开发 ...

  9. std::vector

    Vector Vectors are sequence containers representing arrays that can change in size. Just like arrays ...

  10. IDEA中maven打包跳过Junit Test

    运行MVN install时需要跳过Junit的test cases,可以采用下面的方法: mvn install -DskipTests 或者mvn install -Dmaven.test.ski ...