HDU 4937 Lucky Number(列舉進位制)

畫船聽雨發表於2014-08-26

題目大意:給你一個十進位制的數字n,然後問你轉化為某一進位制後它的每一位的數字只可能為3,4,5,6.求這種符合條件的進位制有多少種。

解題思路:這題雖然沒說進位制有多大但是我們可以簡單的分析一下,n的上限是10^12,如果有四位數字的話,那至少要出現三次方,所以進位制最大為10000。

所以我們列舉一下,一位的時候3,4,5,6顯然為-1.

兩位的時候解一下a*x+b = n。

三位時解一下:a*x^2+b*x+c = n。

四位的時候看每一位取餘後的結果是否落在3-6之間就行了。

Lucky Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1201    Accepted Submission(s): 358


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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

using namespace std;

const int maxn = 510;

int main()
{
    int T;
    cin >>T;
    int Case = 1;
    while(T--)
    {
        LL n;
        scanf("%I64d",&n);
        cout<<"Case #"<<Case++<<": ";
        if(n == 3LL || n == 4LL || n == 5LL || n == 6LL)
        {
            cout<<-1<<endl;
            continue;
        }
        int 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;
                    LL b = j;
                    LL c = k-n;
                    LL tmp = sqrt(b*b-4*a*c);
                    if(tmp*tmp != b*b-4*a*c) continue;
                    if((tmp-b)%(2*a)) continue;
                    if((tmp-b)/(2*a) > max(a, max(b, c))) ans++;
                }
            }
        }
        for(LL i = 4; i*i*i <= n; i++)
        {
            LL tmp = n;
            while(tmp)
            {
                int x = tmp%i;
                if(x < 3 || x > 6) break;
                tmp /= i;
            }
            if(!tmp) ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


相關文章