hdu 5974 A Simple Math Problem(数学题)

Problem Description

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).

题意:给你两个数a,b如果能找到两个数x,y使得x+y=a而且x,y的最小公倍数为b。

由于题目给出数据组数下、较多有12w组所以遍历会超时,所以要想办法直接求值。

根据题意能得到两个公式:

(1)x+y=a;

(2)x*y=b*k;(k为x,y的最大公约数)

显然有一个k在这里不好求答案所以想办法把k除掉。

将(1)左右都除k得到

(3)x/k+y/k=a/k;

再将(2)左右都除k得到

(4)(x/k)*(y/k)=b/k;

由于k是x,y的最大公约数,所以x/k,与y/k互质。所以a/k,与b/k也是互质。

所以问题就简化到求

i+j=a/gcd(a,b),i*j=b/gcd(a,b);

i和j的解。

#include <iostream>
#include <cmath>
using namespace std;
int X , Y;
int gcd(int a , int b) {
    return b > 0 ? gcd(b , a % b) : a;
}
int cal(int a , int b , int c) {
    if(a * a - 4 * b < 0)
        return 0;
    int gg = a * a - 4 * b;
    int fff = sqrt(gg);
    if(gg != fff * fff) {
        Y = -1 , X = -1;
        return 0;
    }
    X = (a + fff);
    Y = (a - fff);
    if(X % 2 == 0 && Y % 2 == 0) {
        X /= 2;
        Y /= 2;
        return 1;
    }
    else {
        X = -1;
        Y = -1;
        return 0;
    }
}
int main()
{
    int a , b;
    while(cin >> a >> b) {
        int c = gcd(a , b);
        X = -1 , Y = -1;
        if(cal(a / c , b / c , c) && X != -1 && Y != -1) {
            cout << min(X , Y) * c << ‘ ‘ << max(X , Y) * c << endl;
        }
        else {
            cout << "No Solution" << endl;
        }
    }
    return 0;
}
时间: 2024-10-11 22:02:38

hdu 5974 A Simple Math Problem(数学题)的相关文章

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

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

HDU 5974 A Simple Math Problem ——(数论,大连区域赛)

给大一的排位赛中数论的一题.好吧不会做...提供一个题解吧:http://blog.csdn.net/aozil_yang/article/details/53538854. 又学了一个新的公式..如果x和y互质,那么x+y和x*y互质.证明如下:随便找一个x中有的因子c,因为x,y互质,因此c不是y的因子.同时c是x*y的因子,由同余模方程知(x+y)% c = x % c + y % c = 0 + y % c.因为c不是y的因子,所以不等于0,所以c不是x+y的因子.同理可以证得x和y中的

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 1715 A Simple Math Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: If x < 10   ,则  f(x) = x. If x >= 10 ,则  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 给出k,m和a0~a9,求f(k)%m,  k<2*10^9 , m < 10^5 这是一个递推式,故可以用矩阵乘法来求 和上题类似,具体思路过程见上题

HDU - 1757 A Simple Math 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(x-3) + -- + a9 * f(x-10); And ai(0<=i<=9) can only be 0 or 1 . Now, I will give a0 ~ a9 and two positive in

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): 2831    Accepted Submission(s): 1693 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x)

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 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(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 .Now, I w