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
思路:枚举进制计算结果即可。
代码:
 #include <bits/stdc++.h>
using namespace std; typedef long long LL;
const int maxn = ;
const int maxm = ;
const LL mod = 1e9+;
int digit[maxn], revert[maxn];
LL L, R, l, r;
LL dp[maxm][maxm][maxn][]; LL dfs(int k, int s, int l, bool ok, bool lim) {
if(l < ) {
if(ok) return k;
return ;
}
if(!lim && ~dp[k][s][l][ok]) return dp[k][s][l][ok];
int pos = lim ? digit[l] : k - ;
LL ret = ;
for(int i = ; i <= pos; i++) {
revert[l] = i;
if(i == && s == l) {
ret += dfs(k, s-, l-, ok, lim&&(i==pos));
}
else if(ok && l < (s + ) / ) {
ret += dfs(k, s, l-, i==revert[s-l], lim&&(i==pos));
}
else {
ret += dfs(k, s, l-, ok, lim&&(i==pos));
}
}
if(!lim) dp[k][s][l][ok] = ret;
return ret;
} LL f(LL n, LL k) {
if(n == ) return k;
int pos = ;
while(n) {
digit[pos++] = n % k;
n /= k;
}
return dfs(k, pos-, pos-, , );
} signed main() {
int T, tt = ;
scanf("%d", &T);
memset(dp, -, sizeof(dp));
while(T--) {
scanf("%lld%lld%lld%lld",&L,&R,&l,&r);
LL ret = ;
for(int i = l; i <= r; i++) {
ret += f(R, i) - f(L-, i);
}
printf("Case #%d: %lld\n", tt++, ret);
}
return ;
}
 

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. Entity Framewrok 7beta7中不同版本sql server自动生成分页sql语句的问题

    在EF中,使用linq进行分页是很方便的,假如我们有一个EMP表,结构如下: public class Emp { [Key] public Guid No { get; set; } public ...

  2. Linux常用命令汇总(渐更)

    后台启动jar nohup java -jar xxxxx.jar > xxxx.out 2>&1 & 封禁ip iptables -I INPUT -s 200.194. ...

  3. vue-elem-stylus 的mixin用法

    .tab border-1px{red} minxin border-1px(&color) position:relative &::after display:block posi ...

  4. 移动端真机调试工具--DebugGap (VIDE)

    越来越多的移动端开发工作,需要我们有一个好的调试工具,以解决各类真机才会遇到的BUG,最近使用了一款DebugGap 的工具,非常方便,在这里推荐给大家. 官网地址 DebugGap  . 按需求下载 ...

  5. Eucalyptus-利用镜像启动一个Centos实例

    1.前言 使用kvm制作Eucalyptus镜像(Centos6.5为例)——http://www.cnblogs.com/gis-luq/p/3990795.html 上一篇我们讲述了如何利用kvm ...

  6. TFS无法确定工作区解决方案

    当TFS遇到无法确定工作区时,删除掉SUO和VSSSCC文件,当然也有可能不是这两个,反正是SLN文件以外的两个文件,毕竟是昨天做的事,有点忘记了.所以,做掉文件之前,要做好备份O(∩_∩)O~

  7. Merge更新同步一个表

    merge T2   --目标表using T1    --源表 on T1.id=T2.id   --匹配条件 when matched then    --匹配update set [name]= ...

  8. ubuntu 自动启动程序

    首先打开终端ctrl + alt + t sudo  -i 输入密码:ubuntu chmod 777 /etc/rc.local 打开  vi   /etc/rc.local 按  i  键进入输入 ...

  9. java 串口通信实现流程

    1.下载64位rxtx for java 链接:http://fizzed.com/oss/rxtx-for-java 2.下载下来的包解压后按照说明放到JAVA_HOME即JAVA的安装路径下面去 ...

  10. hdu-1874 畅通工程续---模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 求起点到终点的最短距离 解题思路: 注意重边,其他的就是模板 #include&l ...