hdu 6298 Maximum Multiple (簡單數論)
阿新 • • 發佈:2018-09-16
sam -c limit miss hide hid 不存在 review print
The first line contains an integer n (1≤n≤106).
instead.
Maximum Multiple
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3313 Accepted Submission(s): 1382
Input There are multiple test cases. The first line of input contains an integer T (1≤T≤106), indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤106).
Output For each test case, output an integer denoting the maximum xyz. If there no such integers, output ?1
Sample Input 3 1 2 3
Sample Output -1 -1 1
題目大意:
給你一個正整數n,要你找到三個整數x,y,z滿足:n=x+y+z, x∣n, y∣n, z∣n,並使xyz最大。求最大的xyz,若不存在,則輸出-1。
簡單數論題。
1可以分解成這樣幾個形式:
1=1/3+1/3+1/3 1=1/2+1/4+1/4 1=1/2+1/3+1/6
而他們對應的乘積分別是1/27 > 1/32 > 1/36。
所以應當優先考慮n被3整除的情況,然後是被2整除,最後是被6整除。由於被3整除包含被6整除,故只需考慮前兩種情況即可。註意輸出用long long。
#include<cstdio> using namespace std; int main() { int t; scanf("%d",&t); while(t--) { long long n; scanf("%lld",&n); if(n%3==0) { printf("%lld\n",(n/3)*(n/3)*(n/3)); } else if(n%4==0) { printf("%lld\n",(n/2)*(n/4)*(n/4)); } else { printf("-1\n"); } } return 0; }View Code
hdu 6298 Maximum Multiple (簡單數論)