HDU5974 A Simple Math Problem---数论--转化解方程

感谢:http://blog.csdn.net/mirror58229/article/details/63685884

题意:x+y=a lcm(x,y)=b  求x,y

12WCases +  b 10^9 + a 10^4

所以肯定不是枚举……肯定是公式题

接下来就是转化

x+y=a

x*y/gcd(x,y)=b

令gcd(x,y)=c

x=i*c,y=j*c

i*c+j*c=a

c*i*j=b

c*(i+j)=a

c*i*j=b

因为i j互质 所以gcd(a,b)=c=gcd(x,y) 【最重要的条件】

所以一开始就能得到c值,剩下就是求根

i+j=a/c

i*j=b/c

i+b/(c*i)=a/c

c*i2-a*i+b=0

a.b.c已知二元一次方程组求根 小的那个为i 大的为j

最后因为求根时整除可能会出现小数,这是不满足题意的 ,丢掉这种情况即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
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)!=EOF)
    {
        ll c=gcd(a,b);
        ll d=a*a-4*b*c;
        if(d<0)
        {
            printf("No Solution\n");
            continue;
        }
        ll i=(a-sqrt(d))/2/c;
        ll j=a/c-i;
        ll x=c*i;
        ll y=c*j;
        if(x*y/c==b)
            printf("%lld %lld\n",x,y);
        else
            printf("No Solution\n");
    }
}
时间: 2024-11-05 04:33:52

HDU5974 A Simple Math Problem---数论--转化解方程的相关文章

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

hdu1757-- A Simple Math Problem(矩阵快速幂优化)

A Simple Math Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2

HDU 1757 A Simple Math Problem

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 108 Accepted Submission(s): 77   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

hdu------(1757)A Simple Math Problem(简单矩阵快速幂)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2791    Accepted Submission(s): 1659 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) =

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

hdu 5974 A Simple Math Problem

A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1645    Accepted Submission(s): 468 Problem Description Given two positive integers a and b,find suitable X and Y to meet th

hdoj 1757 A Simple Math Problem

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(

hdu1757 A Simple Math Problem(矩阵快速幂)

题目: A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3522    Accepted Submission(s): 2130 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f

FZYZ-2071 A Simple Math Problem IX

P2071 -- A Simple Math Problem IX 时间限制:1000MS      内存限制:262144KB 状态:Accepted      标签:    数学问题-博弈论   无   无 Description 给定a,b,n,保证a≥2,b≥1,a^b≤n.两个人在玩游戏,每个人每次可以把a加1,或者把b加1,但是不能违反a^b<=n,无法再进行操作的人就输掉了这一场游戏. 假设两个人都足够聪明,按照最优策略进行游戏,问先手是否有必胜策略. Input Format 第