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,其 ...
随机推荐
- lucene测试类
package test.lucene; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStrea ...
- Shell脚本中时间处理
Shell脚本中时间处理 1.脚本内容 #!/bin/bash #环境变量 #设置环境变量和sql文件格式相符 source /etc/profileexport LD_LIBRARY_PATH=&q ...
- ios之UIProgressView
UIProgressView和UIActivityIndicator有些类似 但是不同之处在于, UIProgressView能够更加精确的反应进度 UIActivityIndicator则只能表 ...
- windows下pycharm使用Anaconda安装包环境
转自: https://www.cnblogs.com/heitaoq/p/8632315.html
- python:post请求业务、调用微信api监控业务
vim post.py #!/usr/bin/env python # -*- coding: utf-8 -*- import json import os import datetime impo ...
- shell中test的使用
#/secondin/secondfirstshecho “please enter two numseconder”read firstread secondif test $first -eq $ ...
- PAT Basic 1071
1071 小赌怡情 常言道“小赌怡情”.这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注 t 个筹码后,计算机给出第二个数.若玩家猜对了,则 ...
- .NET:权限管理
题外话: 临近大四,编写各种简历的时候发现,很多电子简历上是可以链上自己在各大论坛上留下的足迹.关于这点,学习网络,拥抱开源,具有互联网思维的博主很后悔,后悔当年只会在网上查资料,不会留资料,空有才能 ...
- 算法学习记录-图——最小路径之Floyd算法
floyd算法: 解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权的最短路径问题,同时也被用于计算有向图的传递闭包. 设为从到的只以集合中的节点为中间节点的最短路径的长度. 若最短路径经过 ...
- AWK原理及命令和文件输入
一.awk简介 1.awk是3个姓氏的首字母,代表该语言的3个作者,awk的版本有很多,包括:旧版awk,新版awk(nawk),GNU awk(gawk)等. awk程序有awk命令,括在引 ...