HDU5974 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): 486    Accepted Submission(s): 196


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

Sample Input
  
  
6 8 798 10780
 

Sample Output
  
  
No Solution 308 490
 

Source

2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)  

http://acm.split.hdu.edu.cn/showproblem.php?pid=5974

一个数 拆成两个正整数和相加为a,最小公倍数为b

 x+y=a;

xy=b;

g=gcd(x,y);    设ng=x,mg=y,

 则m,n都为质数  m+n=a/g;   m*n=b/g;

这时有个结论 n,m互质则 n*m与n+m互质 证明略 求证好多人都无果(也可以不用这个结论 直接试a的因子就OK了a最大就2W)

所以 gcd(x,y)=gcd(a,b); 只要解一个一元二次方程就行了

所以O(1) 注意有多组输出 x小的

#include<bits/stdc++.h>
using namespace std;

int gcd(int a,int b)
{
    if(!b) return a;
    else return gcd(b,a%b);
}

int main ()
{
    int a,b,g,w,p;
    while(~scanf("%d%d",&a,&b))
    {
     g=gcd(b,a);
     w=a/g;
     p=b/g;
     double dt=w*w-4*p;
     //printf("%lf\n",dt);
     double n1,n2,m1,m2;
     if(dt<0) {printf("No Solution\n");continue;}
     n1=(w-sqrt(dt))/2.0;m1=(w*1.0-n1);
     n2=(w+sqrt(dt))/2.0;m2=(w*1.0-n2);
     int flag=0;
     if(m1<n1) swap(n1,m1);
     if(m2<n2) swap(n2,m2);
     if(n1>n2) {swap(n1,n2);swap(m1,m2);}
    // printf("%lf %lf %lf %lf\n",n1,m1,n2,m2);
     int  n3=n1;int m3=m1;
     int  n4=n2;int m4=m2;
     if(n3==n1)
     if(n1>0&&m1>0)
     {   flag=1;
         printf("%d %d\n",n3*g,m3*g);
     }

     if(!flag)
     if(n4==n2)
     if(n2>0&&m2>0)
     {
         flag=1;
         printf("%d %d\n",n4*g,m4*g);
     }

     if(!flag)
     printf("No Solution\n");

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值