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 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)的更多相关文章
- [数论] hdu 5974 A Simple Math Problem (数论gcd)
传送门 •题意 一直整数$a,b$,有 $\left\{\begin{matrix}x+y=a\\ LCM(x*y)=b \end{matrix}\right.$ 求$x,y$ •思路 解题重点:若$ ...
- 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"(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
A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- 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 ——(数论,大连区域赛)
给大一的排位赛中数论的一题.好吧不会做...提供一个题解吧:http://blog.csdn.net/aozil_yang/article/details/53538854. 又学了一个新的公式..如 ...
- 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 ...
- 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,其 ...
随机推荐
- SSH中的jar包讲解
我们在搭建SSH框架的时候,需要引入各自的一些jar包 首先,先来看一下我们使用的SSH的各自版本及引入的jar包. struts2.3.1.2: struts2-core-2.3.1.jar j ...
- shell脚本,计算1+3+5....100等于多少?
[root@localhost wyb]# cat unevenjia.sh #!/bin/bash #从1+++...100的结果 i= count=$1 $count` do sum=$(($su ...
- http常用状态吗以及分别代表什么意思?
http常用状态码: 状态码 定义 说明 1xx 信息 街道请求继续处理 2xx 成功 成功的收到.理解.接受 3xx 重定向 浏览器需要执行某些特殊处理一完成请求 4xx 客户端错误 请求的语法有问 ...
- 【Java_多线程并发编程】JUC原子类——4种原子类
根据修改的数据类型,可以将JUC包中的原子操作类可以分为4种,分别是: 1. 基本类型: AtomicInteger, AtomicLong, AtomicBoolean ;2. 数组类型: Atom ...
- CSS3-::selection
“::selection”只能设置两个属性,一个就是background,另一个就是color属性,设置其他属性是没有任何效果的. ::selection使用语法: /*Webkit,Opera9.5 ...
- Python-求解两个字符串的最长公共子序列
一.问题描述 给定两个字符串,求解这两个字符串的最长公共子序列(Longest Common Sequence).比如字符串1:BDCABA:字符串2:ABCBDAB.则这两个字符串的最长公共子序列长 ...
- cf 1016D
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- PAT Basic 1041
1041 考试座位号 每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码 ...
- pytest分布式执行(pytest-xdist)
前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟.如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候, 我们会用测试人力成本换取时间成本,这个时候多找 ...
- C/C++ 位操作
C/C++对位操作有如下方法: <1>位操作运算符(注意:下面几个运算符不改变原来的变量的值,只是获得运算的结果即一个新值) 按位取反:~ 位与:& 位或:| 位异或:^ 左移位运 ...