Lucky Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit:
131072/131072 K (Java/Others)
“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.
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.
2 10 19
Case #1: 0 Case #2: 1Hint10 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.
x1 + x2 * base + x3 * base * base + x4 * base *base *base + ……>= 3*7000+3 *7000 ^2 +3 * 7000 ^3 = 1.029e12 > max(n).
#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; }