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 <= 10 9).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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值