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 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, 如果找不到则输出No solution.
题解:由于test case 和 a,b规模都很大,不能使用暴力,必然是通过数学方法直接求解。
不妨设x = ki, y = kj; gcd(x,y) = k
易知 i,j互质 (如果不互质则gcd必然大于k)
gcd(a,b) = gcd( k*(i+j) , k*(i*j) )
由于i,j互质,则(i+j)和 (i*j)必然互质,证明如下:
对于i的任意因子p(1除外),i % p = 0, (i*j) % p = 0
(i+j) % p = (i%p + j%p) % p = j%p, 由于i,j互质则p必然不是j的因子,所以 p 不是 (i+j) 的因子
所以对于i的所有因子(1除外)i+j都没有,但i*j都有;同理对于j的所有因子(1除外),i+j也没有,但i*j都有
所以i*j的所有因子(1除外),i+j都没有 即 (i+j) , (i*j) 互质
我们可以得出以下结论:
(1)如果 i,j互质,那么i 和(i+j) 互质,j和(i+j)互质
(2)如果 i,j互质,那么(i+j) 和(i*j)互质
对于此题我们推出了gcd(a,b) = gcd(x,y) = k
原方程:LCM(x,y) = x*y / gcd(x,y) = b xy = bk = b*gcd(a,b)
又有x + y = a , a,b已知
可以把y表示成x带入解一元二次方程;
也可以用(x-y)2 = (x + y)2 - 4xy求出x - y进而求出x和y
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdio> using namespace std; long long gcd(long long a,long long b)
{
return a == ? b : gcd(b % a, a);
}
int main()
{
long long a,b;
ios::sync_with_stdio(false);
while(cin>>a>>b)
{
long long c = gcd(a,b);
long long xy = c*b;
long long t = a*a-*xy;
long long t1 = sqrt(t);
long long x = (t1+a)/;
long long y = (a-x);
if((x/gcd(x,y)*y!=b))
{
cout<<"No Solution"<<endl;
continue;
}
if(x<y)
{
cout<<x<<" "<<y<<endl;
}
else
{
cout<<y<<" "<<x<<endl;
}
}
return ;
}
HDU - 5974 A Simple Math Problem (数论 GCD)的更多相关文章
- [数论] hdu 5974 A Simple Math Problem (数论gcd)
传送门 •题意 一直整数$a,b$,有 $\left\{\begin{matrix}x+y=a\\ LCM(x*y)=b \end{matrix}\right.$ 求$x,y$ •思路 解题重点:若$ ...
- 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"(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
A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 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 ——(数论,大连区域赛)
给大一的排位赛中数论的一题.好吧不会做...提供一个题解吧:http://blog.csdn.net/aozil_yang/article/details/53538854. 又学了一个新的公式..如 ...
- 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 ...
- 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,其 ...
随机推荐
- 搭建SSI开发框架原理
Spring2.5.Struts2.Ibatis开发框架搭建(一) ssi, ibatis 一.框架下载 1.1 Struts2框架 Struts2框架发展于WebWork,现在捐献给了Apach ...
- Bootstrap 原始按钮
Bootstrap 原始按钮 <!DOCTYPE html><html><head><meta http-equiv="Content-Type&q ...
- shell脚本,计算输入给定的数,判断最大值,最小值,总和?
[root@localhost ~]# cat five.sh #!/bin/bash #任意输入5个数,判断最大值,最小值,总和 s= read -p "please input:&quo ...
- netfilter 和 iptables
http://blog.chinaunix.net/uid/23069658/cid--1-list-4.html 洞悉linux下的Netfilter&iptables 系列,有一到十六, ...
- shell脚本中使用echo显示带颜色的内容
shell脚本中使用echo显示带颜色的内容,需要使用参数-e 格式如下: echo -e "\033[字背景颜色;文字颜色m字符串\033[0m" 例如: echo -e &qu ...
- 五分钟入门 Dingo API
基于 https://laravel-china.org/doc... 文档更简洁的描述Dingo,直戳重点,注重实践 Django-Book 概述 Dingo API帮助您轻松快速地构建自己的API ...
- perl学习之子例程
1.system function && user function system fucntion:chomp reverse print... user function: & ...
- git 常用命令及虚拟机服务器仓库搭建
$ git config --global user.email "you@example.com" $ git config --global user.name "Y ...
- (转)UILabel常用属性
Java代码 收藏代码 #import "ViewController.h" #import <CoreText/CoreText.h> @interface View ...
- HttpServlet RequestDispatcher sendredirect和forward
Servlet的框架是由两个Java包组成:javax.servlet和javax.servlet.http. 在javax.servlet包中定义了所有的Servlet类都必须实现或扩展的的通用接口 ...