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.
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.
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 )
3 1 1 2 36 1 982180 10 10 496690841 524639270 5 20
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=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.
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.
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 )
3 1 1 2 36 1 982180 10 10 496690841 524639270 5 20
Case #1: 665 Case #2: 1000000 Case #3: 447525746