Palindrome Function(回文数)

Palindrome Function
Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 1141    Accepted Submission(s): 390


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  


题意:求k进制数某区间内的回文个数

由于范围十分大, 直接求肯定是不行的, 故用数位来考虑

要分是否用0补全来考虑


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 1e9;

int head(int x, int k){//求首位
	while(x/k){
		x /= k;
	}
	return x;
}
int tail(int x, int k){//求末位
	return x%k;
}
int bits(int x, int k){//求位数
	int ans = 0;
	while(x){
		x /= k;
		ans++;
	}
	return ans;
}
int body(int x, int k){//求除去首、末位的数
	int ans = x - tail(x,k) - head(x,k)*pow(k, bits(x, k)-1);
	if(ans < 0) ans = 0;
	return ans/k;
}
int calc(int n,int k){//计算n位数的回文个数
	if(n == 0) return 0;
	return pow(k, (n+1)/2) * (k-1) / k;
}
int calc0(int n,int k){//计算n位数的回文个数, 用0补全
	if(n == 0) return 0;
	return pow(k, (n+1)/2);
}
int fun0(int n, int k, int b){//不大于n的数中k进制为回文的个数, 不足b位用0补全
	if(n<0) return 0;
	int ans = 0;
	if(bits(n,k)*2<=b) return 1;
	int tmp = n / (int)pow(k, b-bits(n,k));
	if(bits(tmp, k)==1) return tmp+1;
	if(bits(tmp, k)==2) {
		if(head(tmp,k)<=tail(tmp,k)){
			return head(n,k) + 1;
		}
		else return head(n,k);
	}
	ans += head(tmp, k) * calc0(bits(tmp, k)-2, k);
	int p2 = body(tmp,k);
	if(head(tmp,k)>tail(tmp,k)) p2--;
	ans += fun0(p2, k, bits(tmp,k)-2);
	return ans;
} 
int fun(int n, int k){	//不大于n的数中k进制为回文的个数
	if(n<=0) return 0;
	if(bits(n, k)==1) return n;
	if(bits(n, k)==2) {
		if(head(n,k)<=tail(n,k)){
			return k-1 + head(n,k);
		}
		else return k-1 + head(n,k)-1;
	}
	int ans = 0;
	for(int i=bits(n, k)-1; i>=1; i--)
		ans += calc(i, k);
	ans += (head(n,k)-1)*calc0(bits(n,k)-2, k);
	int tmp = body(n,k);
	if(head(n,k)>tail(n,k)) tmp--;
	ans += fun0(tmp, k, bits(n,k)-2);
	return ans;
} 

int main(){
	std::ios::sync_with_stdio(false);
	int T;
	cin>>T;
	int caset = 0;
	while(T--){
		int L,R,l,r;
		cin>>L>>R>>l>>r;
		ll ans = 0;
		for(int k=l; k<=r; k++)
			ans += ( fun(R, k) - fun(L-1, k) ) * (k-1) + R-L+1;
		cout<<"Case #"<<++caset<<": "<<ans<<endl;
	}
}


猜你喜欢

转载自blog.csdn.net/outp0st/article/details/77448511