HDU 5974 A Simple Math Problem 数学题
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 ;
}
HDU 5974 A Simple Math Problem 数学题的更多相关文章
- hdu 5974 A Simple Math Problem(数学题)
Problem Description Given two positive integers a and b,find suitable X and Y to meet the conditions ...
- hdu 5974 A Simple Math Problem
A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- HDU 5974 A Simple Math Problem(数论+结论)
Problem Description Given two positive integers a and b,find suitable X and Y to meet the conditions ...
- HDU 5974 A Simple Math Problem (解方程)
题意:给定a和b,求一组满足x+y=a && lcm(x, y)=b. 析:x+y = a, lcm(x, y) = b,=>x + y = a, x * y = b * k,其 ...
- hdu 5974 A Simple Math Problem gcd(x,y)=gcd((x+y),lcm(x,y))
题目链接 题意 现有\[x+y=a\\lcm(x,y)=b\]找出满足条件的正整数\(x,y\). \(a\leq 2e5,b\leq 1e9,数据组数12W\). 思路 结论 \(gcd(x,y)= ...
- HDU - 5974 A Simple Math Problem (数论 GCD)
题目描述: Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least ...
- HDU 5974 A Simple Math Problem ——(数论,大连区域赛)
给大一的排位赛中数论的一题.好吧不会做...提供一个题解吧:http://blog.csdn.net/aozil_yang/article/details/53538854. 又学了一个新的公式..如 ...
- HDU 5974"A Simple Math Problem"(GCD(a,b) = GCD(a+b,ab) = 1)
传送门 •题意 已知 $a,b$,求满足 $x+y=a\ ,\ LCM(x,y)=b$ 条件的 $x,y$: 其中,$a,b$ 为正整数,$x,y$ 为整数: •题解 关键式子:设 $a,b$ 为正整 ...
- [数论] hdu 5974 A Simple Math Problem (数论gcd)
传送门 •题意 一直整数$a,b$,有 $\left\{\begin{matrix}x+y=a\\ LCM(x*y)=b \end{matrix}\right.$ 求$x,y$ •思路 解题重点:若$ ...
随机推荐
- 如何查看ffmpeg支持的编码器和封装格式
查看支持的编码器(也就是-vcodec后面可以接的参数):ffmpeg -codecs 查看支持的封装格式(也就是-f后面可以接的参数):ffmpeg -formats 查看支持的滤镜(也就是-vf后 ...
- C++中指针和指针变量
指针和指针变量的理解: #include<iostream> using namespace std; int main() { int n; int * m; m = &n; n ...
- [SHOI 2017] 组合数问题
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4870 [算法] 回顾组合数的定义 : C(N , M)表示将N个小球放入M个盒子里的 ...
- http 和 ajax 的关系
经常听到有人混淆两者之间的基本概念,认为只是毫无关系的两种东西. 其实ajax也属于http请求,只不过是http一共有两种请求,一种是同步,一种是异步当然也可以同步那就是ajax. 总所周知hhtp ...
- bzoj1012最大数maxnumber——单调栈
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1012 单调栈水题:用了一下lower_bound二分. 代码如下: #include< ...
- phpStorm的远端部署
首先远端服务器的路径: /var/www -rwxrwxrwx jiangzhaowei jiangzhaowei 6月 index.html* lr-xr-xr-x root root 2月 php ...
- 【旧文章搬运】加载PE文件时IAT的填充时机
原文发表于百度空间,2011-06-20========================================================================== 大致过程如 ...
- CF-832B
B. Petya and Exam time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 【Linux学习】Vi / Vim编辑器—编辑器工作模式、vi编辑操作
Vi / Vim编辑器-编辑器工作模式.vi编辑操作 推荐一个很好的学习指南:http://www.oschina.net/translate/learn-vim-progressively 一.编辑 ...
- 技术胖Flutter第四季-22页面跳转并返回数据
视频地址: https://www.bilibili.com/video/av35800108/?p=23 博客地址: https://jspang.com/post/flutter4.html#to ...