本文主要是介绍HDOJ 4937 Lucky Number,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
Lucky Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1034 Accepted Submission(s): 305
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.
“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.
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: 1Hint
传送门:点击打开链接
解题思路:
枚举转换后两位和三位的情况,解方程a*x+b=n(两位的情况),a*x*x+b*x+c=n(三位的情况)。对于4位即及以上的情况,枚举进制从4到7000(上限是这么找的3*x^3+3*x^2+3*x+3>=10^12)。对于3,4,5,6,有无数种情况,输出-1.
代码:
#include <cstdio>
#include <cmath>
#include <cstring>typedef long long lint;
int a[4] = {3, 4, 5, 6};int fun(lint n, int m)
{int cnt = 0;while(n){int t = n % m;if(3!=t && 4!=t && 5!=t && 6!=t)return false;n /= m;cnt++;}if(cnt < 4) return false;return true;
}int solve(lint n)
{if(3==n || 4==n || 5==n || 6==n)return -1;int ret = 0;for(int i=0; i<4; ++i){for(int j=0; j<4; ++j){lint x = (n - a[j]) / a[i];if(x>a[i] && x>a[j] && x*a[i]+a[j]==n)ret++;//, printf("%d%d %d\n", a[i], a[j], x);}}for(int i=0; i<4; ++i){for(int j=0; j<4; ++j){for(int k=0; k<4; ++k){lint x;if(a[j]*a[j] < 4*a[i]*(a[k]-n)) continue;x = (-a[j]+sqrt(a[j]*a[j]-4*a[i]*(a[k]-n))) / (2*a[i]);if(x>a[i] && x>a[j] && x>a[k] && a[i]*x*x+a[j]*x+a[k]==n)ret++;//, printf("%d%d%d %d\n", a[i], a[j], a[k], x);x = (-a[j]-sqrt(a[j]*a[j]-4*a[i]*(a[k]-n))) / (2*a[i]);if(x>a[i] && x>a[j] && x>a[k] && a[i]*x*x+a[j]*x+a[k]==n)ret++;//, printf("%d%d%d %d\n", a[i], a[j], a[k], x);}}}for(int i=4; i<=7000; ++i){if(fun(n, i)) ret++;}return ret;
}int main()
{lint n;int t, icase = 1;scanf("%d", &t);while(t--){scanf("%I64d", &n);int ans = solve(n);printf("Case #%d: %d\n", icase++, ans);}return 0;
}
这篇关于HDOJ 4937 Lucky Number的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!