hdu5974数学
来源:互联网 发布:贵州大数据如何登录 编辑:程序博客网 时间:2024/04/28 00:42
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
- hdu5974数学
- hdu5974
- HDU5974
- hdu5974——A Simple Math Problem(gcd推导+数学+解方程组)
- 数论-HDU5974
- HDU5974 Simple Math Problem
- HDU5974 数论+解方程
- HDU5974(数学题)
- HDU5974 A Simple Math Problem
- HDU5974 A Simple Math Problem
- HDU5974 A Simple Math Problem (韦达定理)
- hdu5974————A Simple Math Problem(GCD)
- 数学
- 数学
- 数学
- 数学
- 数学
- 数学
- python4
- 携程移动端架构演进与优化之路
- 10.30 NOIP模拟赛(afternoon)
- Android C/C++开发指南
- 规则引擎简介
- hdu5974数学
- 11.06 周末联考总结
- 安卓 Volley+OkHttp3+Gson(Jackson)开源库的封装过程
- java中重载与重写的区别
- js中对象的拷贝和比较
- 第一篇博客不知道写什么好 就当记录我自己的学习点滴吧,当记笔记的地方
- 字符串Hash (BKDRHash)
- php上传文件
- Python正则表达式的七个使用范例