Palindrome Function
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 930 Accepted Submission(s): 516
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 .
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 .
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.
( )
In the following T lines,each line consists of 4 integers L,R,l,r.
( )
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
解题思路:直接数位dp, dp[start[[now][carry]表示start位数,当前到了now位,前面都是回文状态,进制为carry时的所有回文数的个数
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100;
LL dp[maxn][maxn][50];//dp[i][j][k] 表示i位数,当前为j位前面都回文,进制位k的回文数个数
LL L, R;
int l, r;
int carry;
int num[maxn];
int term[maxn];
LL dfs(int start, int now, int c, int limit, int zero)
{
if(dp[start][now][c] != -1 && !limit) return dp[start][now][c];
int up = limit?num[now]:c - 1;
if(now < 1) return 1;
LL sum = 0;
for(int i = 0; i <= up; i++)
{
term[now] = i;
if(zero && i == 0) sum += dfs(start - 1, now - 1, c, (i == up) && limit, 1);
else if(now > start / 2) sum += dfs(start, now - 1, c, (i == up) && limit, 0);
else if(i == term[start - now + 1]) sum += dfs(start, now - 1, c, (i == up) && limit, 0);
}
if(!limit) dp[start][now][c] = sum;
return sum;
}
LL solve(LL x)
{
int len = 0;
while(x)
{
num[++len] = x % carry;
x /= carry;
}
return dfs(len, len, carry, 1, 1);
}
int main()
{
int T;
scanf("%d", &T);
int Case = 1;
memset(dp, -1, sizeof(dp));
while(T--)
{
scanf("%lld%lld%d%d", &L, &R, &l, &r);
LL ans = 0;
for(int i = l; i <= r; i++)
{
carry = i;
LL res = solve(R) - solve(L - 1);
ans += res * (LL)i;
ans += (R - L + 1 - res);
}
printf("Case #%d: %lld\n", Case++, ans);
}
return 0;
}