列挙+進数変換---hdu 4937 Lucky Number

4816 ワード

Lucky Number
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 294    Accepted Submission(s): 49
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
 
 
Mean:
 
10進数nをあげます.今、xを探して、この10進数がx進数で表される数字の中に3、4、5、6の4つの数字しか含まれていないようにしてください.このようなxはいくつありますか.
 
 
analyse:
 
nという数のx進法での表現をn=a 0+a 1*x+a 2*x^2+a 3*x^3+.....a 0,a 1,a 2...と列挙し,この式がnに等しいか否かを判断する.いくつかのケースについて議論する:1)a 0:すなわちa 0=nは、a 0が3,4,5,6のいずれかに等しい場合にのみ要求を満たすことができるが、この場合、xは任意の値を取って要求を満たす(もちろん最も基本的な条件x>a 0が満たす)ので、この場合は-1を出力する.2)a 0+a 1*x:このとき列挙する量はa 0とa 1であり、我々は3,4,5,6の中でa 0とa 1を列挙する.3)a 0+a 1*x+a 2*x^2:この場合は方程式a 0+a 1*x+a 2*x^2=nを解く2次方程式に相当しますが、どうやって解くのでしょうか.求根式:x=(-b±ルート番号(b^2-4 ac)/2 aを用いて,この値が整数であるか否かを判断すればよい.このように3位以内のxはa 0,a 1,a 2,a 3を列挙することで列挙された.a 0+a 1*x+a 2*x^2+a 3*x^3は1 e 12以内の数を表すことができることを証明することができ、以上の3つのステップを列挙した後、残りはa*x^3の場合であり、x^3 
 
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
",++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
",++kase,ans); } return 0; }