HDU 6156 Palindrome Function



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) ∑i=LR∑j=lrf(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 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


有一个函数f(i,j),如果i在j进制下是回文数字则函数值为j否则为1;

i的范围是l-r,j的范围是L-R,求f(i,j)的函数值之和

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

ll l,r,L,R;
ll dp[35][35][2][38];
ll a[35];
ll num[35];

ll dfs(ll len,ll pos,ll sta,ll lim,ll k)//当前数字的长度,当前数位,是否是回文串,前一位是否达到了最大值,当前是k进制
{
    if(pos<0)
        if(sta)
            return k;
        else
            return 1;
    if(!lim&&dp[len][pos][sta][k]!=-1)
        return dp[len][pos][sta][k];
    int up=lim?a[pos]:k-1;
    ll ans=0;
    for(int i=0;i<=up;i++)
    {
        num[pos]=i;//记录第pos位的数值
        if(len==pos&&i==0)//如果枚举的数位是当前数字位的最高位,且当前的数字位是0,则数字舍掉最高位0;
            ans+=dfs(len-1,pos-1,sta,lim&&i==up,k);
        else if(sta&&pos<(len+1)/2)//枚举到该数字的后半部分,开始判断是否是回文串
            ans+=dfs(len,pos-1,sta&&i==num[len-pos],lim&&i==up,k);
        else//前半部分枚举
            ans+=dfs(len,pos-1,sta,lim&&i==up,k);
    }
    if(!lim) dp[len][pos][sta][k]=ans;
    return ans;
}
ll solve(ll n)
{
    ll ans=0;
    for(int i=L;i<=R;i++)
    {
        int pos=0;
        int tmp=n;
        while(tmp)
        {
            a[pos++]=tmp%i;
            tmp/=i;
        }
        ans+=dfs(pos-1,pos-1,1,1,i);
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    int kase=1;
    memset(dp,-1,sizeof(dp));
    while(t--)
    {

        scanf("%I64d%I64d%I64d%I64d",&l,&r,&L,&R);
        printf("Case #%d: %I64d\n",kase++,solve(r)-solve(l-1));
    }
    return 0;
}



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) ∑i=LR∑j=lrf(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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值