hdu5974数学

Given two positive integers a and b,find suitable X and Y to meet the conditions:
                                                        X+Y=a
                                              Least Common Multiple (X, Y) =b

x+y=a
lcm(x,y)=b



x=nd
y=md



gcd(n,m)=1 
(n+m)d=a
n*m*d=b
=>
n+m=a/d
n*m=b/d
枚举a的因子d,然后就是韦达定理 


#include <bits/stdc++.h>

using namespace std;

typedef long long LL ;
const int N = 20000 ;
std::vector<int> fac[N+8] ;


void init(){
    for(int i = 1 ; i <= N ; i++){
        for(int j = i ; j <= N ; j += i) fac[j].push_back(i) ;
    }
}

void gao(LL a , LL b){
     std::vector<std::pair<LL , LL> > res ;
     for(std::vector<int>::iterator it = fac[a].begin() ; it != fac[a].end() ; it++){
        int d = *it ;
        if((b%d) != 0) continue ;
        LL A = 1;
        LL B = -a/d ;
        LL C = b/d ;

        LL delt = B*B - A*C*4 ;
        LL de = (LL)std::sqrt(delt + 0.5) ;
        if(de*de != delt) continue ;

        if((-B + de) % (A*2) == 0){
            LL m = (-B + de) / (A*2) ;
            LL n = a/d - m ;
            if(std::__gcd(n,m) == 1 && n*m == b/d){
                if(n > m) std::swap(n , m) ;
                res.push_back(std::make_pair(n*d , m*d)) ;
            }
        }

        if((-B - de) % (A*2) == 0){
            LL m = (-B - de) / (A*2) ;
            LL n = a/d - m ;
            if(std::__gcd(n,m) == 1 && n*m == b/d){
                if(n > m) std::swap(n , m) ;
                res.push_back(std::make_pair(n*d , m*d)) ;
            }
        }
     }

     if(res.empty()) puts("No Solution") ;
     else{
        std::sort(res.begin() , res.end()) ;
        printf("%I64d %I64d\n" , res[0].first , res[0].second) ;
     }
}


int main() {
    init() ;
    LL a , b ;
    while(scanf("%I64d%I64d" , &a , &b) != EOF){
       gao(a , b) ;
    }
    return 0 ;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值