Hdu 6156 Palindrome Function【数位Dp】

来源:互联网 发布:mutlab定一个数组 编辑:程序博客网 时间:2024/06/06 01:45

Palindrome Function

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


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

题目大意:


设定F(n,k),表示K进制数n是回文数的话,值为k,否则为1.

Ri=Lrj=lf(i,j) .


思路:


设定Dp【i】【j】【k】【2】:

①Dp【i】【j】【k】【0】表示起点为i,当前位为j,k进制下的数字不是回文数的方案数。

②Dp【i】【j】【k】【1】表示起点为i,当前位为j,k进制下的数字是回文数的方案数。


那么过程记忆化搜索一波即可。


Ac代码:


#include<stdio.h>#include<string.h>using namespace std;#define ll long long intll now[60];ll num[60];ll dp[60][60][50][2];ll Dfs(ll start,ll cur,ll limit,ll base,ll state)//标记起点,当前位子,是否达到临界点,进制数,以及是否为回文数{    if(cur<0)return state;    if(limit==0&&dp[start][cur][base][state]>-1)return dp[start][cur][base][state];    ll end=base-1;    if(limit==1)end=num[cur];    ll ans=0;    for(ll i=0;i<=end;i++)    {        now[cur]=i;        if(start==cur&&i==0)        {            ans=ans+Dfs(start-1,cur-1,limit&&(i==end),base,state);        }        else if((start-1)/2>=cur&&state==1)        {           ans=ans+Dfs(start,cur-1,limit&&(i==end),base,now[start-cur]==i);        }        else        {            ans=ans+Dfs(start,cur-1,limit&&(i==end),base,state);        }    }    if(limit==0)    dp[start][cur][base][state]=ans;    return ans;}ll Slove(ll X,ll base){    memset(now,-1,sizeof(now));    ll len=0;    while(X)    {        num[len++]=X%base;        X/=base;    }    num[len]=0;    return Dfs(len-1,len-1,1,base,1);}int main(){    memset(dp,-1,sizeof(dp));    ll t;    int kase=0;    scanf("%lld",&t);    while(t--)    {        ll L,R,l,r;        scanf("%lld%lld%lld%lld",&L,&R,&l,&r);        ll ans=0;        for(ll i=l;i<=r;i++)        {            ll ans1=Slove(R,i);            ans1-=Slove(L-1,i);            ans+=ans1*i+(R-L+1-ans1);        }        printf("Case #%d: ",++kase);        printf("%lld\n",ans);    }}







原创粉丝点击