HDU - 5974 A Simple Math Problem(简单数论)

题意:求x,y满足x+y=a,lcm(x, y)= b。(a <= 20000, b <= 1000000000)有十二万组样例。

思路:这道题现场赛数据很水能用O(n)的复杂度爆过去。后来加了数据就过不了了。只能用O(1)的复杂度。

首先知道 x+y=a,lcm(x, y)= b。令g = gcd(x, y),、那么x = i * g, y = j * g (i,j互素,因为如果i,j不互素那么gcd(x,y)就不等于g了,而是等于g乘以i和j的最大公约数了,矛盾),i * g + j * g = a, i * j * g = b. (i + j) * g = a, i * j * g = b,因为i,j互质,所以i + j和i*j互质,故gcd(a,b) = g = gcd(x, y).

s所以就有 x + y = a, x * y = b * gcd(a, b),解一个二元一次方程组,即为答案。


#include<cstdio>
#include<set>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<map>
#include<queue>
#include<vector>
#include<string>
#include<sstream>
#include<cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 100 + 20;
const double EPS = 1e-5;
const int mod = 1e8 + 7;
typedef unsigned long long ull;
typedef long long ll;
int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, -1, 1};
int a, b;
int gcd(int x, int y){
    return !y ? x : gcd(y, x % y);
}
int main(){
    while(scanf("%d%d", &a, &b) == 2){
        int g = gcd(a, b);
        ll det = a * a - 4LL * g * b;
        ll d = sqrt(det);
        if(d * d == det){
            ll c = a - d;
            if(c % 2 == 0) {
                printf("%lld %lld\n", c / 2, a - c / 2);
                continue;
            }
        }
        puts("No Solution");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值