http://acm.hdu.edu.cn/showproblem.php?pid=5974

遇到数学题真的跪。。

题目要求

X + Y = a

lcm(X, Y) = b

设c = gcd(x, y);

那么可以表达出x和y了,就是x = i * c; y = j * c;

其中i和j是互质的。

所以lcm(x, y) = i * j * c = b

那么就得到两个方程了。

i * c + j * c = a;

i * j * c = b;

但是有一个c,三个未知数。

因为i和j互质,所以(i + j) 和 i * j 互质。

假设他们不互质,那么设那个数是x,有,i + j = m * x;  i * j = k * x;

那么有i和j都能整除x,(由第一条可以得到了),这和i和j互质矛盾。

gcd(a, b) = c了,

然后就能解一个一元二次方程。有解的时候要输出最小解,因为这样确保不会一个取了较大的解,另一个变了负数。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
LL a, b;
void work() {
LL c = __gcd(a, b);
LL B = a / c;
LL C = b / c;
LL diaota = B * B - * C;
LL t = inf / ;
if (diaota >= )
t = (LL)sqrt(diaota);
LL ans1 = B + t;
LL ans2 = B - t;
if (diaota < || t * t != diaota || ((ans1 & ) && (ans2 & ))) {
cout << "No Solution" << endl;
} else {
LL x, y;
if (ans1 % == && ans2 % == ) { //优先最小解
LL tans = min(ans1, ans2);
tans /= ;
cout << tans * c << " " << (B - tans) * c << endl; } else if (ans1 & ) {
ans2 /= ;
x = ans2 * c;
y = (B - ans2) * c;
cout << ans2 * c << " " << (B - ans2) * c << endl;
} else {
ans1 /= ;
x = ans1 * c;
y = (B - ans1) * c;
cout << ans1 * c << " " << (B - ans1) * c << endl;
}
// if (x + y != a) while (1);
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
IOS;
while (cin >> a >> b) work();
return ;
}

HDU 5974 A Simple Math Problem 数学题的更多相关文章

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

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

  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. 使用XMLHttpRequest

    请求种类 通过XMLHttpRequest的请求可以通过同步和异步的方式获取数据,请求的种类在XMLHttpRequest的open()方法的第三三个可选参数async设置.如果这个参数是true或者 ...

  2. Android Jni(Java Native Interface)笔记

    首先记录一个问题,关于如何用javah生成头文件. 为什么要生成头文件?在含有 static{ System.loadLibrary("hellojni"); } 这样代码的类下面 ...

  3. 属性(@property)的修饰词有哪些,各自是什么作用,在哪种情况下用?

       之前面试了几家公司,都会问到这个基础的问题,以前,没有怎么注意,所以答的很混乱,所以查了查网上的资料,特意整理了一份.   常见修饰词有:assign.weak.strong.retain.co ...

  4. luogu 3812 【模板】 线性基

    线性基是一个支持在集合里插入数并查询最大子集异或值 #include<iostream> #include<cstdio> #include<cstring> #i ...

  5. 洛谷P4719 动态DP —— 动态DP(树剖+矩乘)

    题目:https://www.luogu.org/problemnew/show/P4719 感觉这篇博客写得挺好:https://blog.csdn.net/litble/article/detai ...

  6. R文件报错

    res中drawable中的资源文件进行命名时只能用0-9或者a-z或者-,其他的东西不能使用,也禁止在重命名时开头使用大写字母

  7. 小程序rpx

    rpx是微信小程序解决自适应屏幕尺寸的尺寸单位.微信小程序规定屏幕的宽度是750rpx, 微信小程序也支持rem尺寸单位,rem规定屏幕的宽度是20rem vw vh适配 vw和vh是css3中的新单 ...

  8. 二、mysql安装详解

    step1:打开下载的mysql安装文件(mysql-5.5.36-win32.msi),双击运行,如下图: step2:点击“Next”按钮继续,如下图: step3:点击“勾选”,点击“Next” ...

  9. unity3D +php +数据库

    本文只是自己学习 摘录网上前人的学习资料,并非自己写的!!!!!!最近有项目需要用到数据库,网上有不少资料,但是整理后发现,那些资料对于完全没有PHP+MySQL经验的人来说还是很难理解,所以分享一下 ...

  10. APP开发过程中需求变更流程

    在APP开发过程中,不可避免的会有需求变更,从以往项目开发过程总结发现,需求变更太频繁,产品一句话需求,没有形成良好的版本迭代概念,频繁的变动影响开发交付日期,但是交付日期又是定死的,严重拖累了开发及 ...