Problem 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).
 
题意:给你两个数a,b如果能找到两个数x,y使得x+y=a而且x,y的最小公倍数为b。
 
由于题目给出数据组数下、较多有12w组所以遍历会超时,所以要想办法直接求值。
根据题意能得到两个公式:
(1)x+y=a;
(2)x*y=b*k;(k为x,y的最大公约数)
显然有一个k在这里不好求答案所以想办法把k除掉。
将(1)左右都除k得到
(3)x/k+y/k=a/k;
再将(2)左右都除k得到
(4)(x/k)*(y/k)=b/k;
由于k是x,y的最大公约数,所以x/k,与y/k互质。所以a/k,与b/k也是互质。
所以问题就简化到求
i+j=a/gcd(a,b),i*j=b/gcd(a,b);
i和j的解。
 
#include <iostream>
#include <cmath>
using namespace std;
int X , Y;
int gcd(int a , int b) {
return b > 0 ? gcd(b , a % b) : a;
}
int cal(int a , int b , int c) {
if(a * a - 4 * b < 0)
return 0;
int gg = a * a - 4 * b;
int fff = sqrt(gg);
if(gg != fff * fff) {
Y = -1 , X = -1;
return 0;
}
X = (a + fff);
Y = (a - fff);
if(X % 2 == 0 && Y % 2 == 0) {
X /= 2;
Y /= 2;
return 1;
}
else {
X = -1;
Y = -1;
return 0;
}
}
int main()
{
int a , b;
while(cin >> a >> b) {
int c = gcd(a , b);
X = -1 , Y = -1;
if(cal(a / c , b / c , c) && X != -1 && Y != -1) {
cout << min(X , Y) * c << ' ' << max(X , Y) * c << endl;
}
else {
cout << "No Solution" << endl;
}
}
return 0;
}

hdu 5974 A Simple Math Problem(数学题)的更多相关文章

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

  2. hdu 5974 A Simple Math Problem

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

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

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

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

  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 (数论 GCD)

    题目描述: Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least ...

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

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

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

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

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

随机推荐

  1. Mysql架构简要

    1. MySql 最上层是一些客户端和连接服务,包含本地sock通信和大多数基于客户端/服务端工具实现的类似于tcp/ip的通信. 主要完成一些类似于连接处理.授权认证.及相关的安全方案.在该层上引入 ...

  2. FormLayout and FormData

    FormLayout通过为小窗口部件创建四边的Form附加值(attachment)来进行工作,并且把这些Form附加值存储在布局数据中.一个附加值让一个小窗口部件指定的一边粘贴(attach)到父C ...

  3. 今天代码中接触到了一个新的东西。js的上下自动滚动,无缝对接。

    js的上下自动滚动,无缝对接.为什么会用到这个东西呢?因为我在做公司的官网项目的修改的时候.有一个产品介绍的页面,会有很多的产品出现在,中间部分的列表里.但是又不能够使用分页.所以我就在想如果,列表数 ...

  4. Axis1.4 配置数组类型复杂对象

    最近 项目对接 webservice,要求SOAP 标准是1.1,然后在axis 和 spring ws 和 cxf 之间进行选择,然后axis 可以自定义服务,然后随tomcat启动发布,sprin ...

  5. HTML/CSS:display:flex 布局教程

    网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂 ...

  6. ES6中比较实用的几个特性

    1.Default Parameters(默认参数) in ES6 es6之前,定义默认参数的方法是在一个方法内部定义 var link = function (height, color, url) ...

  7. Git 实用技巧:git stash

    我们经常会遇到这样的情况: 正在dev分支开发新功能,做到一半时有人过来反馈一个bug,让马上解决,但是新功能做到了一半你又不想提交,这时就可以使用git stash命令先把当前进度保存起来.然后切换 ...

  8. 快速了解Python并发编程的工程实现(上)

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...

  9. spark读取pg数据库报错操作符不存在

    代码: Properties connectionProperties = new Properties(); connectionProperties.put("user", C ...

  10. NOIP退役记

    10.10 想着自己再过一个月就要退役了,真叫人心酸.想到徐志摩的诗: "悄悄地,我走了,正如我悄悄的来,我挥一挥衣袖,不带走一片云彩." 学了这么久的OI,感觉真的就像诗里讲的一 ...