您的位置:首页 > 其它

HDU-5974 A Simple Math Problem(数论推导)

2016-11-09 21:56 423 查看

A Simple Math Problem

[b]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 308    Accepted Submission(s): 131
[/b]

[align=left]Problem Description[/align]

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


 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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).
 

[align=left]Sample Input[/align]

6 8
798 10780

 

[align=left]Sample Output[/align]

No Solution
308 490

 

[align=left]Source[/align]
2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)

题意:X+Y = a,   lcm(X,Y) = b  ,求X和Y,12W 数据。

思路:参考貌似队友推出来了,我是看了题解才会的

X+Y = a
lcm(X,Y) = b
X = ns,Y = ms
(n+m)*s = a
lcm(ns,ms) = b
gcd(X,Y) = gcd(a,b) = s
X*Y = gcd(X,Y)*lcm(X,Y) = gcd(X,Y)*b = gcd(a,b)*b

X*Y = gcd(a,b)*b
X+Y = a
联立求解
即 X^2 - a*x + b*c = 0
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
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))
{
int x,y;
int c = gcd(a,b);
int temp = a*a - 4*b*c;
int delta = sqrt(temp);
bool flag = true;
if(temp < 0)
flag = false;
if(delta*delta != temp || (a-delta)%2 != 0)
flag = false;
if(flag)
{
x = (a-delta) / 2;
y = a - x;
printf("%d %d\n",x,y);
}
else
printf("No Solution\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: