题目描述:

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)的更多相关文章

  1. [数论] hdu 5974 A Simple Math Problem (数论gcd)

    传送门 •题意 一直整数$a,b$,有 $\left\{\begin{matrix}x+y=a\\ LCM(x*y)=b \end{matrix}\right.$ 求$x,y$ •思路 解题重点:若$ ...

  2. HDU 5974 A Simple Math Problem(数论+结论)

    Problem Description Given two positive integers a and b,find suitable X and Y to meet the conditions ...

  3. 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$ 为正整 ...

  4. hdu 5974 A Simple Math Problem

    A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Ot ...

  5. 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)= ...

  6. HDU 5974 A Simple Math Problem ——(数论,大连区域赛)

    给大一的排位赛中数论的一题.好吧不会做...提供一个题解吧:http://blog.csdn.net/aozil_yang/article/details/53538854. 又学了一个新的公式..如 ...

  7. 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 ...

  8. hdu 5974 A Simple Math Problem(数学题)

    Problem Description Given two positive integers a and b,find suitable X and Y to meet the conditions ...

  9. 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,其 ...

随机推荐

  1. Bootstrap CSS概览

    HTML5文档类型(<!DOCTYPE html>) Bootstrap前端框架使用了HTML5和CSS属性,为了让这些能正常工作,您需要使用HTML5文档类型(<!DOCTYPE ...

  2. POJ-3669-流星雨

    这题的话,卡了有两个小时左右,首先更新地图的时候越界了,我们进行更新的时候,要判断一下是不是小于零了,越界就会Runtime Error. 然后bfs 的时候,我没有允许它搜出300以外的范围,然后就 ...

  3. java之 List、Set、ArraylIst、 LinkList

    LIst与set概述 List Set 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.      2.对于随机访问get和set,ArrayList优于 ...

  4. LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal

    题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...

  5. Verilog学习笔记基本语法篇(二)·········运算符

    Verilog HDL的语言的运算符的范围很广,按照其功能大概可以分为以下几类: (1)算术运算符 +,-,*,/,% 优先顺序 !~ *  /   % +    - <<    > ...

  6. sublime_win配置

    让你用sublime写出最完美的python代码--windows环境 点击上方标题查看原文链接, 感谢大佬 至少很长一段时间内,我个人用的一直是pycharm,也感觉挺好用的,也没啥大毛病 但是py ...

  7. bash中的算术运算

    bash中的算术运算     +, -, *, /, %     实现算术运算:         (1) let var=算术表达式          (2) var=$[算术表达式]         ...

  8. luogu3698 [CQOI2017]小Q的棋盘

    最长链是根节点到深度最深的结点的路径. 显然,要么直接走最长链,要么兜兜转转几个圈圈再走最长链,而最长链以外的结点因为要"兜圈",所以要经过两次. #include <ios ...

  9. Charles-安装和配置

    一. 安装.破解charles工具 1. 安装压缩包中的charles_setup.exe,安装完成后先不启动charles. 2. 在安装文件中找到crack文件,将文件中的charles.jar拷 ...

  10. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!

    Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...