hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场
生活随笔
收集整理的這篇文章主要介紹了
hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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: 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. 題意: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; }
總結
以上是生活随笔為你收集整理的hdu 4937 Lucky Number(数学题 进制转换)2014多校训练第7场的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NoSQL架构实践(一)——以NoSQL
- 下一篇: 花了一个月时间梳理了一下公司的微服务核心