HDU 6156 Palindrome Function(数位DP)

来源:互联网 发布:qq管家软件管理 编辑:程序博客网 时间:2024/05/16 19:45

Palindrome Function

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

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.

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)

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(n,k)表示十进制下n在变成k进制后,如果是回文数。则f(n,k)=k,否则=1.问[L,R]在[l,r]进制下的回文数个数。
题解:比赛时试图找一下规律,完全没想到数位dp。赛后看了别人代码才想到。 设dp[p][len][pos]表示在p进制下,长度为len,当前位为pos的方案数。
代码:

#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<vector>#include<queue>#include<set>#include<algorithm>#include<map>#include<math.h>using namespace std;typedef long long int ll;typedef pair<int,int>pa;const int N=1e6+100;const int mod=1e9+7;const ll INF=1e18;int read(){    int x=0;    char ch = getchar();    while('0'>ch||ch>'9')ch=getchar();    while('0'<=ch&&ch<='9')    {        x=(x<<3)+(x<<1)+ch-'0';        ch=getchar();    }    return x;}/***********************************************************/int t,p,x,y,l,r;ll op;ll dp[40][40][40];int temp[40];int a[40];ll dfs(int len,int pos,bool limit,bool zero){    if(pos<0) return 1;    if(!limit&&!zero&&dp[p][len][pos]!=-1) return dp[p][len][pos];    int up=limit?a[pos]:p-1;    ll ans=0;    for(int i=0; i<=up; i++)    {        temp[pos]=i;        if(zero) ans+=dfs(len-(i==0),pos-1,limit&&i==a[pos],i==0);        else if(pos<(len+1)/2&&i==temp[len-pos]||pos>=(len+1)/2)            ans+=dfs(len,pos-1,limit&&i==a[pos],false);    }    if(!limit&&!zero)        dp[p][len][pos]=ans;    return ans;}ll solve(ll x){    int pos=0;    while(x)    {        a[pos++]=x%p;        x/=p;    }    return dfs(pos-1,pos-1,true,true);}int main(){    scanf("%d",&t);    memset(dp,-1,sizeof(dp));    for(int cas=1; cas<=t; cas++)    {        op=0;        scanf("%d%d%d%d",&l,&r,&x,&y);        for(int i=x; i<=y; i++)        {            p=i;            op+=(solve(r)-solve(l-1))*(i-1);        }        printf("Case #%d: %lld\n", cas, op+(ll)(r-l+1)*(y-x+1));    }    return 0;}
原创粉丝点击