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$ •思路 解题重点:若$ ...
随机推荐
- socket.io emit 常见用法
io.on('connect', onConnect); function onConnect(socket){ // 只发给sender. sending to the client socket. ...
- 万亿级日志与行为数据存储查询技术剖析(续)——Tindex是改造的lucene和druid
五.Tindex 数果智能根据开源的方案自研了一套数据存储的解决方案,该方案的索引层通过改造Lucene实现,数据查询和索引写入框架通过扩展Druid实现.既保证了数据的实时性和指标自由定义的问题,又 ...
- Opencv— — Pinch Filter
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- Bone Collector(复习01背包)
传送门 题目大意:01背包裸题. 复习01背包: 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总 ...
- bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- HDMI 8193 配置
1, User space:ProjectConfig.mkMTK_HDMI_SUPPORT = yes MTK_MULTIBRIDGE_SUPPORT = yesMTK_INTERNAL_HDMI_ ...
- 20个jQuery分页插件和教程
1.客户端的jQuery 分页插件jPages jPages 是一个客户端的分页插件,但提供很多特性例如自动翻页.键盘和滚动浏览,延迟显示以及完全可定制的导航面板. Read More Demo 2. ...
- python学习笔记——Thread常用方法
Thread对象中的一些方法: 以前说过多线程,用到threading模块中的Thread对象,其中的start和run方法比较熟悉了,start()是重载了Thread对象中的run方法,其实作用还 ...
- [转载]Doxygen C++ 注释风格
转载自:http://luchenqun.com/?p=761 做一个C++方面的符合Doxygen的注释文档,备用. 1.头文件根原文件注释.这个我也不知道需要注释什么.能想到的是:谁写的,里面有些 ...
- 1.1-1.5 flume架构概述及安装使用
一.flume架构概述 1.flume简介 Flume是一种分布式,可靠且可用的服务,用于有效地收集,聚合和移动大量日志数据.它具有基于流数据流的简单灵活的架构.它具有可靠的可靠性机制和许多故障转移和 ...