Palindrome Function(数位dp模板)

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)∑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.
(1≤T≤105,1≤L≤R≤109,2≤l≤r≤361≤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*

题意:t组输入,每组给你四个数,L R表示从L到R的十进制数,l r表示范围从l到r的k进制,根据题意求

i=LRj=lrf(i,j) ∑ i = L R ∑ j = l r f ( i , j )
的值,若i为j进制下回文数则f(i,j)=k,否则f(i,j)=1。
思路:考虑到多组输入和范围比较大,直接暴力循环一般是要超时的,再考虑题意,数字在某一进制下是否回文为数字本身的性质,不会随着输入而改变,而我们又是判断一个区间上一部分数是否回文,可以考虑数位dp的方法,对区间的每一位的各个取值进行判断。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
using namespace std;
int dp[110][2][110][110];  //dp数组
int x[110],temp[110];
int dfs(int pos//第pos位,int sta//是否回文,bool limit//是否到达上限,int k,int len//数字长度)
{
    if(pos<0)
    {
        if(sta)  return k;
        else     return 1;  
    }
    if(!limit&&dp[pos][sta][len][k]!=-1)   return dp[pos][sta][len][k];
    long long sum=0,up;
    up=limit?x[pos]:k-1;
    for(int i=0;i<=up;i++)
    {
        temp[pos]=i;
        if(i==0&&len==pos)  //递归遍历,从最小的开始
           sum+=dfs(pos-1,sta,limit&&(i==up),k,len-1);
        else if(sta&&pos<(len+1)/2)  //判断是否回文
           sum+=dfs(pos-1,sta&&i==temp[len-pos],limit&&(i==up),k,len);
        else
           sum+=dfs(pos-1,sta,limit&&(i==up),k,len);
    }
    if(!limit)   dp[pos][sta][len][k]=sum;  //记忆化
    return sum;
}

int f(int a,int k)
{
    int pos=0;
    while(a)   //k进制下提取每一位
    {
        x[pos++]=a%k;
        a/=k;
    }
    return dfs(pos-1,1,true,k,pos-1);
}
int main()
{
    memset(dp,-1,sizeof(dp));
    int l,r,l1,r1,n;
    long long ans;
    cin>>n;
    for(int k=0;k<n;k++)
    {
        ans=0;
        cin>>l>>r>>l1>>r1;
        for(int i=l1;i<=r1;i++)
        {
             ans+=(f(r,i)-f(l-1,i));
        }
        printf("Case #%d: %lld\n",k+1,ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值