Palindrome Function
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 733 Accepted Submission(s): 399
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.
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 )
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
也就是说 ans = 回文数个数 * 进制 + (R - L + 1 - 回文数个数)
思路 1 : 数位DP 思路 2:直接打印出2 到 36 进制下 1 到 1e9 以内的回文数,然后前后二分找答案
个人认为DP比较好,我数位DP写得烂,差不多就快超时的了,卡时间,下面只给出数位DP代码
DP[当前位][进制][开始位]
solve(x,i)表示 1 到 x 以内,进制位 i 的回文数个数
那么 L 到 R 的就是 solve(R,i) - solve(L - 1,i)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define ll long long
ll dp[40][40][40];
int ans[40],wei[40];
ll dfs(int pos,int zero,int jin,int flag,int beg){
if(pos < 0){
return zero == 0; // zero == 0 则表示不是前导零,计数返回 1
}
if(dp[pos][jin][beg] != -1 && !flag && !zero)
return dp[pos][jin][beg]; //如果这里已经有了值且无上界限制且不是前导零可直接返回值
ll sum = 0;
int up = flag ? wei[pos] : jin - 1; //up记录上界
for(int i = 0;i <= up;i++){
if(zero && i == 0) //是前导零则传递状态计数
sum += dfs(pos - 1,zero,jin,flag && i == up,beg);
else{
if(zero){ //是前导零,枚举到非零数时
ans[pos] = i; //ans数组记录当前位置数字,拿去与后面的对比
sum += dfs(pos - 1,0,jin,flag && i == up,pos);
}else if(pos < (beg + 1) / 2){ //位置到了一半之后要开始对比了
if(i == ans[beg - pos]) // 如果对比相等,形成回文 计数加
sum += dfs(pos - 1,0,jin,flag && i == up,beg);
}else{
ans[pos] = i; //未到一半 ans数组记录当前位置数字,拿去与后面的对比
sum += dfs(pos - 1,0,jin,flag && i == up,beg);
}
}
}
ans[pos] = -1; //该位对比结束,赋初始值-1
if(!flag && !zero){ //只有该位可以任意放值不被限制的时候还是可重复利用的值,才要记录
dp[pos][jin][beg] = sum;
}
return sum;
}
ll solve(int x,int jin){
int len = 0;
while(x){
wei[len++] = x % jin;
x /= jin;
}
return dfs(len - 1,1,jin,1,39);
}
int main(){
int t;
scanf("%d",&t);
memset(dp,-1,sizeof(dp));
int Case = 1;
while(t--){
int L,R,l,r;
scanf("%d %d %d %d",&L,&R,&l,&r);
ll tot = 0;
for(int i = l;i <= r;i++){
ll sum = solve(R,i) - solve(L - 1,i);
tot += sum * i + (R - L + 1 - sum);
}
printf("Case #%d: %lld\n",Case++,tot);
}
return 0;
}