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. JMeter tomcat测试请求

    JMeter tomcat测试请求 Apache Jmeter是开源的压力测试工具,可以测试tomcat 的吞吐量等信息 下载地址: http://jmeter.apache.org/download ...

  2. Elasticsearch如何安装中文分词插件ik

    elasticsearch-analysis-ik 是一款中文的分词插件,支持自定义词库. 安装步骤: 1.到github网站下载源代码,网站地址为:https://github.com/medcl/ ...

  3. .net笔试题一(简答题)

    1. 简述 private. protected. public. internal 修饰符的访问权限答:private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类 ...

  4. xml 文件转化Dictionary

    下面是xml文件 <?xml version="1.0" encoding="utf-8" ?><nodes> <国土局> ...

  5. UITableViewCellStyle 四种样式

    四种样式如下:

  6. java网络访问指定出口ip

    java网络访问指定出口ip Table of Contents 1. socket 2. apache httpclient 1 socket 可以在Socket构造函数中指定使用的本地ip,如: ...

  7. linux nginx 404错误页面设置

    配置nginx 实现404错误 返回一个页面 1.配置nginx.conf 在http代码块 添加 fastcgi_intercept_errors on; 2.在网站的sever代码块 添加 err ...

  8. Localroast使用总结

    全手打原创,转载请标明出处: https://www.cnblogs.com/dreamsqin/p/10883248.html,多谢~=.= 什么是Localroast 一个根据 JSON 文件快速 ...

  9. Internationalization(i18n) support in SAP CRM,UI5 and Hybris

    i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是"国际化"的简称.对程序来说,在不修改内部代码的情况下,能根据不同语言及 ...

  10. libav(ffmpeg)简明教程(1)

    突然发现又有好久没有写技术blog了,主要原因是最近时间都用来研究libav去了(因为api极类似ffmpeg,虽然出自同一份代码的另外一个分支,因项目选用libav,故下文均用libav代替),其实 ...