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. 常见的生成全局唯一id有哪些?他们各有什么优缺点?

    分布式系统中全局唯一id是我们经常用到的,生成全局id方法由很多,我们选择的时候也比较纠结.每种方式都有各自的使用场景,如果我们熟悉各种方式及优缺点,使用的时候才会更方便.下面我们就一起来看一下常见的 ...

  2. weex 项目开发 weexpack 项目 打包、签名、发布

    一. weexpack build android  和  weexpack run android 的 区别. (1)单纯打包 weexpack build android (2)打包并运行 wee ...

  3. Javascript Number

    Number 对象 Number对象是原始值的包装对象 创建Number对象的语法: var myNum = new Number(value): var myNum = Number(value): ...

  4. ArcMap中提取影像数据边界

    1.前言 客户手里有一些经过裁剪的不规则多边形影像数据(如图例所示),希望能批量获取该类影像的边界信息,即影像对应的面信息,边界线信息.这里我们提供一种利用镶嵌数据集Footprint图层的方法来获取 ...

  5. tcpick

    tcpick 是一款基于文本的嗅探器,能追踪,重组和重排tcp流.

  6. F5-WAF-12.0

    平台: CentOS 类型: 虚拟机镜像 软件包: f5bigip basic software security waf 服务优惠价: 按服务商许可协议 云服务器费用:查看费用 立即部署 产品详情 ...

  7. 解决IE下面诡异地使用quickrIE5模式打开页面的有关问题

    解决IE下面诡异地使用quickrIE5模式打开页面的有关问题 <!doctype html public "-//w3c//dtd html 4.01 transitional//e ...

  8. pat乙级1049

    浮点型乘整型和整型乘浮点型结果不同,不知为什么. double sum = 0.0; ; i < n; i++) { cin >> a[i]; sum += a[i] * (i + ...

  9. hdu-1068&&POJ1466 Girls and Boys---最大独立集

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1068 题目大意: 有n个人,一些人认识另外一些人,选取一个集合,使得集合里的每个人都互相不认识,求该 ...

  10. 【转】android调试工具DDMS的使用详解

    具体可见http://developer.android.com/tools/debugging/ddms.html. DDMS为IDE和emultor.真正的android设备架起来了一座桥梁.开发 ...