hdu 4627 The Unsolvable Problem(暴力的搜索)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 4627 The Unsolvable Problem(暴力的搜索)相关的知识,希望对你有一定的参考价值。

Problem Description
There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger number. Given an integer n(2 <= n <= 109).We should find a pair of positive integer a, b so that a + b = n and [a, b] is as large as possible. [a, b] denote the least common multiplier of a, b.
 

 

Input
The first line contains integer T(1<= T<= 10000),denote the number of the test cases. For each test cases,the first line contains an integer n.
 

 

Output
For each test cases,print the maximum [a,b] in a line.
 

 

Sample Input
3 2 3 4
 

 

Sample Output
1 2 3
 

 

Author
WJMZBMR
 

 

Source
 


题意:给定一个数n,要求是找到一对数a、b,使得a+b=n,且a和b的最小公倍数要最大,求最大的最小公倍数。

思路:直接暴力查询,找出最大的值。具体看代码。时间复杂度是O(n),并不会超时。注意要用long long,会爆int。

技术分享
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 using namespace std;
15 #define ll long long
16 #define eps 1e-10
17 #define MOD 1000000007
18 #define N 1000000
19 #define inf 1e12
20 ll n;
21 ll gcd(ll a,ll b){
22     return b==0?a:gcd(b,a%b);
23 }
24 ll lcm(ll a,ll b){
25     return a*b/gcd(a,b);
26 }
27 int main()
28 {
29     ll t;
30     scanf("%I64d",&t);
31     while(t--){
32         scanf("%I64d",&n);
33         
34         ll x=n/2;
35         ll r=gcd(x,n-x);
36         ll ans=x*(n-x)/r;
37         while(r!=1 && x>0){
38             x--;
39             r=gcd(x,n-x);
40             ans=max(ans,x*(n-x)/r);
41         }
42         printf("%I64d\n",ans);
43     }
44     return 0;
45 }
View Code

 

以上是关于hdu 4627 The Unsolvable Problem(暴力的搜索)的主要内容,如果未能解决你的问题,请参考以下文章

(The application/json Media Type for JavaScript Object Notation (JSON))RFC4627-JSON格式定义

HDU2824 The Euler function(欧拉函数)

HDU3193 Find the hotel

HDU 6051 If the starlight never fade(原根+推式子)

[HDU3336]Count the string(KMP+DP)

The Luckiest number(hdu 2462)