博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 6156 回文 数位DP(2017CCPC)
阅读量:6291 次
发布时间:2019-06-22

本文共 2926 字,大约阅读时间需要 9 分钟。

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=Lrj=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.
(
1T105,1LR109,2lr36)
 

 

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
思路:枚举进制计算结果即可。
代码:
1 #include 
2 using namespace std; 3 4 typedef long long LL; 5 const int maxn = 1010; 6 const int maxm = 55; 7 const LL mod = 1e9+7; 8 int digit[maxn], revert[maxn]; 9 LL L, R, l, r;10 LL dp[maxm][maxm][maxn][2];11 12 LL dfs(int k, int s, int l, bool ok, bool lim) {13 if(l < 0) {14 if(ok) return k;15 return 1;16 }17 if(!lim && ~dp[k][s][l][ok]) return dp[k][s][l][ok];18 int pos = lim ? digit[l] : k - 1;19 LL ret = 0;20 for(int i = 0; i <= pos; i++) {21 revert[l] = i;22 if(i == 0 && s == l) {23 ret += dfs(k, s-1, l-1, ok, lim&&(i==pos));24 }25 else if(ok && l < (s + 1) / 2) {26 ret += dfs(k, s, l-1, i==revert[s-l], lim&&(i==pos));27 }28 else {29 ret += dfs(k, s, l-1, ok, lim&&(i==pos));30 }31 }32 if(!lim) dp[k][s][l][ok] = ret;33 return ret;34 }35 36 LL f(LL n, LL k) {37 if(n == 0) return k;38 int pos = 0;39 while(n) {40 digit[pos++] = n % k;41 n /= k;42 }43 return dfs(k, pos-1, pos-1, 1, 1);44 }45 46 47 signed main() {48 int T, tt = 1;49 scanf("%d", &T);50 memset(dp, -1, sizeof(dp));51 while(T--) {52 scanf("%lld%lld%lld%lld",&L,&R,&l,&r);53 LL ret = 0;54 for(int i = l; i <= r; i++) {55 ret += f(R, i) - f(L-1, i);56 }57 printf("Case #%d: %lld\n", tt++, ret);58 }59 return 0;60 }

 

 

转载于:https://www.cnblogs.com/mj-liylho/p/7400480.html

你可能感兴趣的文章
random模块
查看>>
在二元树中找出和为某一值的所有路径
查看>>
使用PowerDesigner 15对现有数据库进行反向工程(转)
查看>>
怎样去思考问题 解决问题 zkc学长的福利
查看>>
[python]python2与python3版本的区别
查看>>
关于任务分配方式的一种设想
查看>>
11个很棒的 jQuery 图表库
查看>>
Android线程处理
查看>>
更新 TeX Live 软件包
查看>>
Exp3 免杀原理与实践 Week5 - 20165201
查看>>
嵌套查询
查看>>
关不掉.vbs
查看>>
算法11---红黑树的实现
查看>>
本地系统服务例程:Nt和Zw系列函数
查看>>
mysql 案例 ~ 常见案例汇总
查看>>
jmeter if 控制器
查看>>
Spring定时器时间设置规则
查看>>
算法のLowLow三人行
查看>>
appcompat_v7出现红叉解决方法
查看>>
javascript事件之:jQuery事件接口概述
查看>>