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. Oracle权限管理

    一)用户Oracle中的用户分为二大类1)Oracle数据库服务器创建时,由系统自动创建的用户,叫系统用户,如sys.2)利用系统用户创建的用户,叫普通用户,如scott,hr,c##tiger,zh ...

  2. sql server 分析

    查询指令,查询数据库的版本  SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPE ...

  3. 虚拟机安装CentOS7 Minimal、jdk和hadoop

    虚拟机安装CentOS7 Minimal.jdk和hadoop Table of Contents 1. 安装版本 2. PD安装 3. vim安装和配置 4. 主机名变为bogon的解决办法 5. ...

  4. 添加SAP_ALL权限

    更新usr04,ust04,usrbf2这三张表 REPORT ZTESTCREATEUSER. data: l_USR04 LIKE USR04 , l_UST04 LIKE UST04 , l_P ...

  5. Eucalyptus常用查询命令

    前言: Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus)  ...

  6. 9.26<立方网>技术笔试题

    该公司题目感觉不难,算法设计有三道大题. 1.设有m和n两个整数,求它们的最大公约数和最小公倍数. 2.猴子分桃问题,每次分桃多一个,共有五个猴子,问最少有多少个桃子. 3.关于java的题目,有A, ...

  7. log4j-初识

    1.配置文件介绍: 1.1. 控制台输出:log4j.rootLogger=DEBUG, Console ,File #Console log4j.appender.Console=org.apach ...

  8. mybatis-注解实现crud

    1.在接口上注解sql package com.java1234.mappers; import java.util.List; import org.apache.ibatis.annotation ...

  9. LeetCode Unique Binary Search Trees (DP)

    题意: 一棵BST有n个节点,每个节点的key刚好为1-n.问此树有多少种不同形态? 思路: 提示是动态规划. 考虑一颗有n个节点的BST和有n-1个节点的BST.从n-1到n只是增加了一个点n,那么 ...

  10. LeetCode OJ Container With Most Water 容器的最大装水量

    题意:在坐标轴的x轴上的0,1,2,3,4....n处有n+1块木板,长度不一,任两块加上x轴即可构成一个容器,其装水面积为两板的间距与较短板长之积,以vector容器给出一系列值,分别代表在0,1, ...