Lucky Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 363 Accepted Submission(s): 105
Problem Description “Ladies and Gentlemen, It’s show time! ”
“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.
Input There are multiply test cases.
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.
Output For each test case, output “Case #k: ”first, k is the case number, from 1 to T , then, output a line with one integer, the answer to the query.
Sample Input
2
10
19
Sample Output
Case #1: 0
Case #2: 1
Hint
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.
Author UESTC
Source 2014 Multi-University Training Contest 7
思路:論科學的暴力的重要性。首先對于inf這種情況,一定是n=3,4,5,6,因為對于大于n的base,化為進制數還是由3,4,5,6組成的。
對于a1*x+a0這種形式的,枚舉a1,a0,共16種情況。
對于a2*x^2+a1*x+a0這種形式,枚舉a2,a1,a0,g共64種情況,解二次方程判斷是否存在符合進制。
對于含有x三次方及其以上的,直接從4暴力枚舉到1e4,看是否有可行的進制,詳見代碼:
// file name: 3.cpp //
// author: kereo //
// create time: 2014年08月12日 星期二 19時43分48秒 //
//***********************************//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=10000+100;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
ll n;
int main()
{
int T,kase=0;
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
printf("Case #%d: ",++kase);
if(n>=3 && n<=6){
printf("-1\n");
continue;
}
ll ans=0;
for(ll i=3;i<=6;i++)
for(ll j=3;j<=6;j++){
if((n-i)%j == 0 && (n-i)/j >max(i,j))
ans++;
}
for(ll i=3;i<=6;i++)
for(ll j=3;j<=6;j++)
for(ll k=3;k<=6;k++){
ll a=i,b=j,c=k-n;
ll d=(ll)sqrt(b*b-4*a*c+0.5);
if(d*d!=b*b-4*a*c)
continue;
if((d-b)%(2*a))
continue;
ll x=(d-b)/(2*a);
if(x>max(i,max(j,k)))
++ans;
}
for(ll i=4;i*i*i<=n;i++){
ll x=n;
while(x){
if(x%i<3 || x%i >6)
break;
x/=i;
}
if(!x)
ans++;
}
printf("%I64d\n",ans);
}
return 0;
}