HDU 5974 A Simple Math Problem(简单数学)
来源:互联网 发布:淘宝小二人工服务电话 编辑:程序博客网 时间:2024/06/05 18:55
Description
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
Input
Input includes multiple sets of test data.Each test data occupies one line,including two positive integers a(1≤a≤2*10^4),b(1≤b≤10^9),and their meanings are shown in the description.Contains most of the 12W test cases.
Output
For each set of input data,output a line of two integers,representing X, Y.If you cannot find such X and Y,output one line of “No Solution”(without quotation).
Sample Input
6 8
798 10780
Sample Output
No Solution
308 490
题目大意
给定两个值 a,b,判断是否存在两个数 x,y 满足 x+y=a,lcm(x,y)=b,
若存在,输出x,y,否则输出“No Solution”。
注意:输出有顺序要求,必须是小值在前。
解题思路
某童鞋厉害得不行的直接看出了规律还是定理?
如果这两个值存在的话,就满足 gcd(x,y)=gcd(a,b),然后可得 x*y = lcm(x,y)*gcd(x,y)=gcd(x,y)*b=gcd(a,b)*b,再由完全平方公式可以得到 x 与 y 的差值,再与 x+y 相加减就可以得到 x,y,最后记得验证求得的值x+y=a,lcm(x,y)=b是否成立。
最后推一下这个规律还是定理?
设x=m*gcd(x,y),y=n*gcd(x,y);
gcd(a,b)=gcd(x+y,lcm(x,y))=gcd(m*gcd(x,y)+n*gcd(x,y),m*n*gcd(x,y))
因为 m,n 是互质的,所以 m+n 与 m*n 也是互质的,即可得gcd(x,y)=gcd(a,b)。
代码实现
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;#define ll long longll a,b;ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);}int main(){ while(~scanf("%lld %lld",&a,&b)) { ll g=gcd(a,b); ll xy=b*g; ll z=a*a-4*xy; if(z<0) { printf("No Solution\n"); continue; } else z=sqrt(z); ll x=(z+a)/2; ll y=a-x; if(x*y/gcd(x,y)!=b||x+y!=a) printf("No Solution\n"); else printf("%lld %lld\n",y,x); } return 0;}
- HDU 5974 A Simple Math Problem(简单数学)
- HDU 5974 A Simple Math Problem 数学
- HDU 5974 A Simple Math Problem (数学)
- HDU-5974 A Simple Math Problem(数学)
- HDU 5974 A Simple Math Problem (数学)
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- HDU 5974 A Simple Math Problem(数论)
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- HDU 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- HDU 5974 A Simple Math Problem
- HDU-5974-A Simple Math Problem
- 在主窗口显示前先显示登录窗口
- 洛谷 P3375 【模板】KMP字符串匹配
- dhclient命令
- Linux基本命令
- Android xUtils3完全解析 是时候来了解xUtils3了
- HDU 5974 A Simple Math Problem(简单数学)
- Android Apk包安装应用闪退,出现 java.lang.RuntimeException Unable to instantiate application Caused by: java
- 移植DM9000C驱动程序之确定相异性
- [莫比乌斯反演] BZOJ 4816 [Sdoi2017]数字表格
- RMQ问题
- 【转载】零基础入门深度学习(1)
- 获取对话框句柄 对话框指针
- 什么是对象?
- Iterative Reweighted Least Squares