Lucky Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 294 Accepted Submission(s): 49
“A thief is a creative artist who takes his prey in style... But a detective is nothing more than a critic, who follows our footsteps...”
Love_Kid is crazy about Kaito Kid , he think 3(because 3 is the sum of 1 and 2), 4, 5, 6 are his lucky numbers and all others are not.
Now he finds out a way that he can represent a number through decimal representation in another numeral system to get a number only contain 3, 4, 5, 6.
For example, given a number 19, you can represent it as 34 with base 5, so we can call 5 is a lucky base for number 19.
Now he will give you a long number n(1<=n<=1e12), please help him to find out how many lucky bases for that number.
If there are infinite such base, just print out -1.
The first line contains an integer T(T<=200), indicates the number of cases.
For every test case, there is a number n indicates the number.
10
19
Case #2: 1
10 shown in hexadecimal number system is another letter different from ‘0’-‘9’, we can represent it as ‘A’, and you can extend to other cases.
Mean:
给你一个10进制数n,现在要你找一个x,使得这个十进制数在x进制表示下的数字中只包含3,4,5,6这四个数字,问你这样的x有多少个。
analyse:
我们将n这个数在x进制下的表示记为:n=a0+a1*x+a2*x^2+a3*x^3+.....
我们采取枚举a0、a1、a2...,然后判断这个式子是否等于n的做法。
讨论一下几种情况:
1)a0:即a0==n,只有当a0等于3,4,5,6中其中一个的时候,才可能满足要求,而这种情况下,x取任何值都满足要求(当然最基本的条件x>a0要满足),所以该种情况下就输出-1;
2)a0+a1*x:此时要枚举的量有a0和a1,我们枚举在3,4,5,6中枚举a0和a1,那么如果方程:(n-a0)%a1==0成立(上面的基本条件不再重复),此时也是成立的;
3)a0+a1*x+a2*x^2:此时相当于求解方程a0+a1*x+a2*x^2=n这样的2次方程,但是要怎么解呢?利用求根公式:x=(-b±根号(b^2-4ac))/2a,然后判断这个值是否为整数就可以了。
这样一来三位以内的x就被我们用枚举a0,a1,a2,a3的方式来枚举完了。
我们可以证明a0+a1*x+a2*x^2+a3*x^3是可以将1e12以内的数表示出来的,以上三个步骤枚举完后,剩下的就是a*x^3这种情况,然后在x^3<n的范围内枚举进制就可以了、枚举x进制的就可以了。
Time complexity:不会超过O(n)开3次方次
Source code:
//Memory Time
// 1347K 0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL __int64
using namespace std; int main()
{
#ifndef ONLINE_JUDGE
freopen("cin.txt","r",stdin);
#endif
int T,kase=0;
cin>>T;
while(T--){
LL n,t,ans=0;
LL i,j,k;
LL a,b,c,d,x;
scanf("%I64d",&n);
if(n>=3&&n<=6){
printf("Case #%d: -1\n",++kase);
continue;
}
for(i=3;i<=6;i++){
for(j=3;j<=6;j++){
if((n-i)%j==0&&(n-i)/j>max(i,j))
ans++;
}
}
for(i=3;i<=6;i++){
for(j=3;j<=6;j++){
for(k=3;k<=6;k++){
a=i;b=j;c=k-n;
d=(LL)sqrt(b*b-a*c*4+0.5);
if(d*d!=b*b-a*c*4)continue;
if((d-b)%(a*2))continue;
x=(d-b)/(a*2);
if(x>max(max(i,j),k)){
ans++;
}
}
}
}
for(i=2;i*i*i<=n;i++){
t=n;
while(t){
if(t%i<3||t%i>6)
break;
t=t/i;
}
if(!t){
ans++;
}
}
printf("Case #%d: %I64d\n",++kase,ans);
}
return 0;
}