现在的位置: 首页 > 综合 > 正文

hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场

2018年01月19日 ⁄ 综合 ⁄ 共 2380字 ⁄ 字号 评论关闭

Lucky Number

                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit:
131072/131072 K (Java/Others)


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.
 
题意:Love_Kid将3,4,5,6认为是幸运数字。给定一个十进制数n。现在可以讲起任意转换成其他进制,但转换后的数必须是由3,4,5,6构成的,而这个进制称为幸运进制。问有多少个幸运进制。若有无数个,则输出-1。例如19在5进制下是34,所以5是幸运进制。
分析:对于只有一位数的情况,显然3、4、5、6都应该输出-1.
  如果有2位数,假设这2位中高位为a,低位为b,进制为base,则 n = a * base + b,解一元一次方程即可。
  如果有3位数,假设这3为从高到低分别为a、b、c,进制为base,则 n = a * base * base + b * base + c,即一元二次方程即可。
  如果位数>= 4,可以暴力枚举进制数。base>min(3,4,5,6),所以从base=4开始枚举。又因为
x1 + x2 * base + x3 * base * base + x4 * base *base *base + ……>= 3*7000+3 *7000 ^2 +3 * 7000 ^3 = 1.029e12 > max(n).
所以枚举4到7000就可以了。
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef __int64 LL;
LL Max(LL a, LL b, LL c)
{
    return max(a, max(b, c));
}
int main()
{
    int T, cas = 0;
    LL n, i, j, k, a, b, c, delta, x, t;
    scanf("%d",&T);
    while(T--) {
        LL ans = 0;
        scanf("%I64d",&n);
        printf("Case #%d: ", ++cas);
        if(n >= 3 && n <= 6) {
            printf("-1\n");
            continue;
        }

        // n = j * x + i, x > max(i, j), 转化后只有2位数
        for(i = 3; i <= 6; i++) {
            for(j = 3; j <= 6; j++) {
                if((n - i) % j == 0 && (n - i) / j > max(i, j))
                    ans++;
            }
        }

        // n = i * x * x + j * x + k, 转化后只有3位数
        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;
                    delta = (LL)sqrt(b * b - 4 * a * c + 0.5);
                    if(delta * delta != (b * b - 4 * a * c)) continue;
                    if((delta - b) % (2 * a)) continue;  //负根直接舍弃
                    x = (delta - b) / (2 * a); 
                    if(x > Max(i, j, k))
                        ans++;
                }
            }
        }

        //其他情况
        for(i = 4; i * i * i <= n; i++) {
            t = n;
            while(t) {
                if(t % i < 3 || t % i > 6)
                    break;
                t = t / i;
            }
            if(!t) ans++;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

抱歉!评论已关闭.