数论 UVALive-7726 A Simple Math Problem

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5748

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 ∗ 104 ), b (1 ≤ b ≤ 109 ), and their meanings are shown in the description.

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

 

题目大意:

给定a,b,找到一组x,y满足

x + y = a;

lcm(x,y)=b;

 

化简式子

y = x - a;

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

设k=gcd(x,y) 可得 x * (x-a) = k * b;

那么,我们可以通过枚举k得到x,并验证k是否等于gcd(x,y)。

枚举k时嘤判断求得x是否为整数。

 

#include 
#include 
#include 
using namespace std;

long long gcd(long long x,long long y)
{
     return y?gcd(y,x%y):x;
}

int main()
{
     long long a,b;
     long long x,y;
     bool found;

     while(~scanf("%lld%lld",&a,&b)){
          found=0;
          long long sz=min((a*a)/4/b+1,a);
          for(long long k=1;k

 

你可能感兴趣的:(ACM,数论)