Hdu 6156 Palindrome Function【数位Dp】

本文介绍了一种基于回文数特性的函数f(n,k),并提供了解决特定数学表达式的算法实现。该函数用于判断n在k进制下是否为回文数,并通过动态规划方法高效计算区间内的总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
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),表示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 int
ll 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);
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值