标签:UI ase int its 二分 condition size ane mat
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
还是大字看着舒服啊,可能是我眼睛不行了,现在迷之老年人生活态度,等我先去泡杯茶。
好的我泡茶回来了,刚学刷茶杯路过隔壁实验室的时候发现一个小姐姐正在看琅琊榜……我也想看琅琊榜……(QAQ)
好的回到这道题,一开始我想到了X,Y的最大公约数就是a,b的最大公约数,但是之后解方程我不会解了,十分忏愧,用的二分来查找解:
l=1;r=a/2+1;
mid=(l+r)/2;
if(mid*(a-mid)>b) r=mid-1;
if(mid*(a-mid)<b) l=mid+1;
我自认为对的很,但是不知道为何过不了,可能是我二分写的也太菜了吧,希望哪个有识之士能帮我实现这个方法
下面是中规中矩解方程的代码,我现在连这种方程都不会解了,也是废了
#include<bits/stdc++.h>
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)
{
int ok=1;
ll c=gcd(a,b);
ll x,y;
a=a/c,b=b/c;
ll g1=a*a-4*b;
if(g1<0)ok=0;
else
{
ll g2=sqrt(g1);
if(g2*g2!=g1)ok=0;
else
{
if((a-g2)%2!=0||(a+g2)%2!=0)
ok=0;
else
{
x=(g2+a)/2*c;
y=(a-g2)/2*c;
if(x>y)
{ int temp=x; x=y; y=temp; }
}
}
}
if(ok)
printf("%d %d\n",x,y);
else
printf("No Solution\n");
}
return 0;
}
A Simple Math Problem HDU - 5974
标签:UI ase int its 二分 condition size ane mat
原文地址:http://www.cnblogs.com/xiachongyubing/p/7384117.html