Solve Equation gcd(x,y)=gcd(x+y,lcm(x,y)) gcd(x,y)=1 => gcd(x*y,x+y)=1

/**
题目:Solve Equation
链接:http://acm.hnust.edu.cn/JudgeOnline/problem.php?id=1643   //最终来源neu oj 2014新生选拔赛题
题意:给定两个数的和以及他们的最小公倍数,求这两个数。
思路:
x+y=A
lcm(x,y)=B => x*y/gcd(x,y)=B
要把这两个公式联立,那么必须消掉gcd;
设:d = gcd(x,y), x = kx*d, y = ky*d; kx与ky互质;

x+y=A => d(kx+ky)=A
x*y/gcd(x,y)=B => d*kx*ky=B

由于kx与ky互质,所以kx*ky与(kx+ky)互质,那么d的大小为gcd(A,B);

那么:
x+y=A
x*y/gcd(A,B)=B; 联立两个式子判断是否可以获得整数解再解方程即可。

得出结论:gcd(x,y) = gcd(A,B) = gcd(x+y,lcm(x,y));

一元二次方程解
b*b-4*a*c>=0有解。
x1 = (-b+sqrt(b*b-4*a*c))/(2*a);
x2 = (-b-sqrt(b*b-4*a*c))/(2*a);
要判断是否是整数解,则要对分母对分子取余为0,以及开根号后为整数值。
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 1000100;
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    ll A, B;
    while(scanf("%lld%lld",&A,&B)==2)
    {
        ll d = gcd(A,B);
        ll deta = A*A-4*d*B;
        ll p = sqrt(deta+0.00001);
        if(p*p!=deta){
            printf("No answer\n");
        }else
        {
            if((A+p)%2!=0){
                printf("No answer\n");
            }else
            {
                ll x = (A-p)/2;
                ll y = (A+p)/2;
                printf("%lld %lld\n",x,y);
            }
        }
    }
    return 0;
}
时间: 2024-08-24 06:21:55

Solve Equation gcd(x,y)=gcd(x+y,lcm(x,y)) gcd(x,y)=1 => gcd(x*y,x+y)=1的相关文章

ACM: FZU 2102 Solve equation - 手速题

FZU 2102   Solve equation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Practice Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-negati

FZOJ 2102 Solve equation

                                                                                                                                     Problem 2102 Solve equation Accept: 1097    Submit: 2608Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Desc

FZU 2102 Solve equation 多进制计算

Solve equation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u FZU 2102 Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-negative pairs (k

FZU Problem 2102 Solve equation (数学啊 )

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2102 Problem Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-negative pairs (k, d) that satisfy the equation above. Now in

Solve equation

Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-negative pairs (k, d) that satisfy the equation above. Now in this problem, we want to maximize k. For example, A="123

FZU 2102 Solve equation(水,进制转化)&amp;&amp; FZU 2111(贪心,交换使数字最小)

C Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice FZU 2102 Description You are given two positive integers A and B in Base C. For the equation: A=k*B+d We know there always existing many non-negativ

GCD与LCM

本篇将讲述一下辗转相除法 GCD(欧几里得)算法求的是两数的最大公约数 LCM算法则是在GCD的基础上算出两数的最小公倍数 代码如下: inline int gcd(int a,int b) { return !b? a:gcd(b,a%b); } inline int lcm(int a,int b) { return a/gcd(a,b)*b; } 原文地址:https://www.cnblogs.com/guxingdetiankong/p/10714361.html

Codeforces Round #554 (Div. 2) C.Neko does Maths (gcd的运用)

题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给定两个正整数a,b,其中(1<=a,b<=1e9),求一个正整数k(0<=k),使得a+k与b+k的最小公倍数最小. 解题思路:首先我们需要知道gcd(a,b)=gcd(a,b-a)=gcd(b,b-a)(b>a)的 我们要求的是lcm(a+k,b+k)=(a+k)(b+k)/gcd(a+k,b+k)=(a+k)(b+k)/gcd(a+k,b-a) 因为b-a是定值,所

2015ACM/ICPC亚洲区沈阳站

5510 Bazinga 题意:给出n个字符串,求满足条件的最大下标值或层数 条件:该字符串之前存在不是 它的子串 的字符串 求解si是不是sj的子串,可以用kmp算法之类的. strstr是黑科技,比手写的kmp快.if(strstr(s[i], s[j]) == NULL),则Si不是Sj的子串. 还有一个重要的剪枝:对于一个串,如果当前找到的串是它的母串,则下一次这个串不用遍历. 1 #include <set> 2 #include <queue> 3 #include &