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);
那么可以表达出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 数学题的更多相关文章
- 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 Simple Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 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,其 ...
- 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 (数论 GCD)
题目描述: Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least ...
- HDU 5974 A Simple Math Problem ——(数论,大连区域赛)
给大一的排位赛中数论的一题.好吧不会做...提供一个题解吧:http://blog.csdn.net/aozil_yang/article/details/53538854. 又学了一个新的公式..如 ...
- 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 (数论gcd)
传送门 •题意 一直整数$a,b$,有 $\left\{\begin{matrix}x+y=a\\ LCM(x*y)=b \end{matrix}\right.$ 求$x,y$ •思路 解题重点:若$ ...
随机推荐
- C#入门---1、C#装备知识(C#如何学习)
C#入门---1.C#装备知识(C#如何学习) 一.总结 一句话总结: 主视频,辅助书和教程:还是得看视频,直接看书或者看教程效率不高 1.C#和.NET的关系和区别? .net是一个平台,核心是.n ...
- druid.io 海量实时OLAP数据仓库 (翻译+总结) (1)——分析框架如hive或者redshift(MPPDB)、ES等
介绍 我是NDPmedia公司的大数据OLAP的资深高级工程师, 专注于OLAP领域, 现将一个成熟的可靠的高性能的海量实时OLAP数据仓库介绍给大家: druid.io NDPmedia在2014年 ...
- 谈谈java中成员变量与成员方法继承的问题
谈谈java中成员变量与成员方法继承的问题 关于成员变量和成员方法的的继承问题,我也可以做一个小测试,来看看结果. 首先我们先创建一个父类:
- hadoop应用场景
大数据量存储:分布式存储 日志处理: Hadoop擅长这个 海量计算: 并行计算 ETL:数据抽取到oracle.mysql.DB2.mongdb及主流数据库 使用HBase做数据分析: 用扩展性应对 ...
- TModJS:使用tmodjs
ylbtech-TModJS:使用tmodjs 1.返回顶部 1. 1.安装 npm install -g tmodjs 2.配置 我的模板都放在tpl文件夹中,htmls用于存放模板页面,每一个后缀 ...
- Redis简介,安装和配置,停止,卸载(图解方式)
Redis是一个Key-value的数据结构存储系统,可以已数据库的形式,缓存系统,消息处理器使用,它支持的存储类型很多,例如,String(字符串),list(列表),set(集合),zset(有序 ...
- 如何正确遍历删除List中的元素,你会吗?
遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题.下面主要看看以下几种遍历删除List中元素的形式: 1.通过增强的for循环删除符合条件的多个元素 2.通过增强的for循环删除符合 ...
- HDOJ-1263
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- PHP实用小程序(五)
<HTML> <HEAD> <TITLE>图像函数</TITLE> </HEAD> <BODY> <img src=&qu ...
- 洛谷 - P4997 - 不围棋 - 并查集 - 模拟
https://www.luogu.org/problemnew/show/P4997 首先是改变气的定义,使得容易计算,这个很好理解. 然后使用并查集,因为要维护整个连通块的性质. 最后的难点在于, ...