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,其 ...
随机推荐
- 洛谷 P3958 奶酪
谨以此题来纪念我爆炸的NOIp2017 这个题虽然很多人说是并查集,但是搜索也是毫无压力的,考场搜索细节写挂,爆了个不上不下的80分.今天无意看到这道题,终于AC 首先这道题要考虑一下精度问题,虽然出 ...
- jquery操作滚动条滚动到指定元素位置 scrollTop
$('.brand_t a').bind('click',function(){ if($(this).attr('title1')){ var toChar = $(this).attr('titl ...
- 004 html常用标签
html常用标签 1.无语义标签 <div></div> <span></span> 2.常用语义标签 <hn></hn> 标题 ...
- uboot顶层mkconfig分析
GNU make:http://www.gnu.org/software/make/manual/make.html#Rules 为了便于理解把uboot中的Makefile配置部分弄出来便于理解,这 ...
- BFS:UVa220 ACM/ICPC 1992-Othello(黑白棋)
Othello Othello is a game played by two people on an 8 x 8 board, using disks that are white on one ...
- 使用Lucene的api将索引创建到索引库中
import org.apache.commons.io.FileUtils; import org.apache.lucene.document.Document; import org.apach ...
- 【转】VS2017的VSIX插件开发
最近从头开发了一遍一个VSIX的插件,用于调测的一个工具: 特此把相关的过程经验记录下来: 第一步:建立工程 1. 首先是安装上: 需要安装Visual Studio SDK,这个在安装VS ...
- kendo Grid 列添加自定义模板
columns: [ {field: "行为",template: "<a href='#= 行为#'>#= 行为#</a>"}, {f ...
- Linux暂停和恢复进程
Linux暂停和恢复进程 kill -STOP 1234 将该进程暂停. 如果要让它恢复到后台,用kill -CONT 1234 (很多在前台运行的程序这样是不行的) 如果要恢复到前台,请在当时运行该 ...
- Quartz与Terracotta的集成
Quartz与Terracotta的集成 博客分类: Java&Socket 企业应用面临的问题 开源组件的应用 quartz任务集群数据库任务调度Terracotta Quartz与Ter ...