HDU-6156:Palindrome Function(数位DP)

Palindrome Function

                                                                     Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
                                                                                            Total Submission(s): 1164    Accepted Submission(s): 660


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
 
  
31 1 2 361 982180 10 10496690841 524639270 5 20
 

Sample Output
 
  
Case #1: 665Case #2: 1000000Case #3: 447525746

思路:回文数计数,如果知道如何统计10进制下[1,n]中的回文数个数,那么这题就应该没问题了。对于求[1,n]里的回文数个数,首先考虑求长度比n小的回文数个数,这个可以很轻松的算出来。然后就是算长度和n一样的回文数个数了。

对于n来说,要求出长度等于它的回文数,那么这个数高位的半部分是要小于等于n的,例如对于n=141,那么长度等于n的回文数x=101,111,121,131,141(x的百位和十位上的数字是小于或等于n的)。设n的长度为len,所以从最高位len向(len+1)/2枚举,枚举x的前i位,求出当x的前i位上的数字和n的相同时,后面的位的值小于n的回文数个数。判断完小于n的情况后,最后判断一下x的前半部分等于n的时候是否满足x<=n。

#include<bits/stdc++.h>
using namespace std;
const int MAX=5e5;
const int MOD=1e9+7;
const double PI=acos(-1.0);
typedef long long ll;
ll p[40],f[100];
int check(int n)
{
    int tag=0;
    for(int i=n,j=1;i>=j;i--,j++)
    {
        if(p[i]<p[j])tag=-1;
        if(p[i]>p[j])tag=1;
    }
    return tag!=1;
}
ll solve(int x,ll bas)
{
    if(x==0)return 0;
    int y=x;
    ll tot=0;
    int n=0;
    while(x)p[++n]=x%bas,x/=bas;
    f[0]=1;
    for(int i=1;i<=n;i++)f[i]=f[i-1]*bas;
    for(int i=1;i<n;i++)//长度小于n的情况
    {
        if(i==1)tot+=bas-1;
        else tot+=(bas-1)*f[(i-1)/2];
    }
    for(int i=n,j=1;i>=j;i--,j++)//长度等于n
    {
        if(i==n)
        {
            tot+=(p[i]-1)*f[(n-2+1)/2];
            continue;
        }
        if(i>j)tot+=p[i]*f[(n-j*2+1)/2];//长度为偶数
        else tot+=p[i];                 //长度为奇数
    }
    tot+=check(n);//最后判断n本身的状态
    return tot*(bas-1)+y;
}
int main()
{
    int T,cas=1;
    cin>>T;
    while(T--)
    {
        ll L,R,l,r;
        scanf("%lld%lld%lld%lld",&L,&R,&l,&r);
        ll ans=0;
        for(int i=l;i<=r;i++)ans+=solve(R,i)-solve(L-1,i);
        printf("Case #%d: %lld\n",cas++,ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/mitsuha_/article/details/80327543