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. mysql数据库忘记密码时如何修改(一)

    方法/步骤 打开mysql.exe和mysqld.exe所在的文件夹,复制路径地址 打开cmd命令提示符,进入上一步mysql.exe所在的文件夹. 输入命令  mysqld --skip-grant ...

  2. Dos窗口一闪而过,如何查看错误?

    问:Dos窗口一闪而过,如何查看错误? 答:在执行程序最后追加pause或者read(,),即可查看错误信息.

  3. Ini文件格式说明

    http://www.cnblogs.com/CUIT-DX037/ 百度百科介绍:ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储 ...

  4. Web 前端安装依赖的时候遇到的问题

  5. [转]Tomcat日志详解

    Tomcat下相关的日志文件: 1.Cataline引擎的日志文件,文件名为catalina.{date}.log 2.Tomcat下内部代码丢出的日志,文件名为localhost.{date}.lo ...

  6. 跨平台移动开发phonegap/cordova 3.3全系列教程-结合asp.net/jqmboile

    遠程app配置 把編譯後的www資料夾,復制到遠程地址(目錄結構不要改變), 例如:建議使用app-framework 1.加入jquery mobile1.4点击打开链接 2.加入app-frame ...

  7. 在数据绑定控件(如:Repeater)中使用if判断

    方法: target="<%# DataBinder.Eval(Container.DataItem, "数据库字段").ToString() == "t ...

  8. 测试MS题

    购物车测试点:  1.界面测试        界面布局.排版是否合理:文字是否显示清晰:不同卖家的商品是否区分明显. 2.功能测试 未登录时: 将商品加入购物车,页面跳转到登录页面,登录成功后购物车数 ...

  9. 有些其他程序设置为从 Outlook 下载并删除邮件。为防止发生此意外情况,我们将这些邮件放入一个特殊的 POP 文件夹中

    最近使用FOXMAIL接收MSN邮件时,发现有一些邮件收取不到,进到WEB页面,页面下方提示“你的邮件位于 POP 文件夹中!有些其他程序设置为从 Outlook 下载并删除邮件.为防止发生此意外情况 ...

  10. JavaScript getMonth() 方法

    应该特别注意的是Js中getMonth()这个方法的返回值: 定义和用法: getMonth() 方法可返回表示月份的数字. 返回值: dateObject 的月份字段,使用本地时间.返回值是 0(一 ...