Lucky NumberTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1487 Accepted Submission(s): 441
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
Sample Output
|
定义:若一个数n的k进制全部由3、4、5、6这些数组成,称k是n的幸运数。
题意:给你一个数n(1 <= n <= 1e12),问你它的幸运数有多少个。若不存在输出0,无穷多输出-1。
思路:n最多为1e12,就算一层for也会TLE。我们可以考虑把搜索范围尽可能的缩小。
首先,我们可以把n写成n = a0 + a1 * k^1 + a2 * k^2 + ...
情况一:只有a0组成,直接判断即可。
情况二:n = a0 + a1 * k^1,这时直接求解就可以了,k的范围 >= 1e7
情况三:n = a0 + a1 * k^1 + a2 * k^2,这时就是求解一元二次方程了,k的范围 >= 1e5
其余情况:会出现k^3,这时搜索范围k <= (n)^(1/3),时间复杂度可以承受,直接暴力即可。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define LL long long
using namespace std;
int kcase = 1;
bool judge(LL base, LL n)
{
while(n)
{
if(n % base < 3 || n % base > 6)
return false;
n /= base;
}
return true;
}
void solve()
{
LL n;
scanf("%lld", &n);
printf("Case #%d: ", kcase++);
if(n >= 3 && n <= 6)
{
printf("-1\n");
return ;
}
LL ans = 0;
for(LL a = 3; a <= 6; a++)
{
for(LL b = 3; b <= 6; b++)
{
if((n-a) % b) continue;
if((n-a) / b > max(a, b))
ans++;
}
}
for(LL c = 3; c <= 6; c++)
{
for(LL b = 3; b <= 6; b++)
{
for(LL a = 3; a <= 6; a++)
{
LL C = c - n;
double temp = sqrt(b*b - 4*a*C);
if(temp == (LL)temp)
{
LL d = (LL)temp;
if((d-b) % (2*a) == 0)
{
LL x = (d-b) / (2*a);
if(x > max(max(a, b), c))
ans++;
}
}
}
}
}
for(LL base = 2; base * base * base <= n; base++)
if(judge(base, n))
ans++;
printf("%lld\n", ans);
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
solve();
}
return 0;
}