http://acm.hdu.edu.cn/showproblem.php?pid=5974

遇到数学题真的跪。。

题目要求

X + Y = a

lcm(X, Y) = b

设c = gcd(x, y);

那么可以表达出x和y了,就是x = i * c; y = j * c;

其中i和j是互质的。

所以lcm(x, y) = i * j * c = b

那么就得到两个方程了。

i * c + j * c = a;

i * j * c = b;

但是有一个c,三个未知数。

因为i和j互质,所以(i + j) 和 i * j 互质。

假设他们不互质,那么设那个数是x,有,i + j = m * x;  i * j = k * x;

那么有i和j都能整除x,(由第一条可以得到了),这和i和j互质矛盾。

gcd(a, b) = c了,

然后就能解一个一元二次方程。有解的时候要输出最小解,因为这样确保不会一个取了较大的解,另一个变了负数。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
LL a, b;
void work() {
LL c = __gcd(a, b);
LL B = a / c;
LL C = b / c;
LL diaota = B * B - * C;
LL t = inf / ;
if (diaota >= )
t = (LL)sqrt(diaota);
LL ans1 = B + t;
LL ans2 = B - t;
if (diaota < || t * t != diaota || ((ans1 & ) && (ans2 & ))) {
cout << "No Solution" << endl;
} else {
LL x, y;
if (ans1 % == && ans2 % == ) { //优先最小解
LL tans = min(ans1, ans2);
tans /= ;
cout << tans * c << " " << (B - tans) * c << endl; } else if (ans1 & ) {
ans2 /= ;
x = ans2 * c;
y = (B - ans2) * c;
cout << ans2 * c << " " << (B - ans2) * c << endl;
} else {
ans1 /= ;
x = ans1 * c;
y = (B - ans1) * c;
cout << ans1 * c << " " << (B - ans1) * c << endl;
}
// if (x + y != a) while (1);
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
IOS;
while (cin >> a >> b) work();
return ;
}

最新文章

  1. iOS优秀博客收录
  2. 屌丝程序员的梦想 (六) 我也写个开源CMS
  3. OC中.pch文件的解释
  4. 【SQL】区分新来顾客和再访顾客
  5. android自定义viewgroup实现等分格子布局
  6. html embed用法
  7. FastDFS安装和配置,整合Nginx-1.13.3
  8. Java并发编程:synchronized和锁优化
  9. GC真正的垃圾:强、软、弱、和虚 对象
  10. asp.net动态添加控件学习
  11. docker+ubuntu14.04+cuda7.0
  12. oracle之触发器
  13. Python人工智能第二篇
  14. Direct3D 11 Tutorial 3: Shaders and Effect System_Direct3D 11 教程3:着色器和效果系统
  15. 在notepad++中使用正则匹配功能(一-龥!-~) 中文[利刃篇]
  16. 【iCore1S 双核心板_ARM】例程十三:SDIO实验——读取SD卡信息
  17. Hyper-v虚拟机
  18. 【转】netstat 的10个基本用法
  19. html5 canvas 径向渐变
  20. 如果要写php扩展啥的, 要看什么?

热门文章

  1. Oracle在Java中事物管理
  2. the art of seo(chapter four)
  3. hdu 1236 排名(排序)
  4. 如何应用 AutoIt 修改本机的防火墙配置?(开启,关闭防火墙,添加程序信任到防火墙)
  5. 洛谷 2668&amp;2540 斗地主——搜索+贪心+dp
  6. 【转】Cache Buffer Chain 第三篇
  7. windows设置远程连接
  8. TimeUtils(没试过)
  9. PHP中的常用正则表达式集锦
  10. Java字节码指令收集大全