數論 UVALive-7726 A Simple Math Problem
阿新 • • 發佈:2018-12-09
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 <cstdio> #include <cmath> #include <algorithm> 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<sz;++k){ double c=sqrt(a*a-4*k*b); if(!(c-(int(c)))&&!((a+(int)c)&1)){ x=(a-c)/2;y=(a+c)/2; if(gcd(x,y)==k){ found=1; break; } } } if(found) printf("%lld %lld\n",x,y); else printf("No Solution\n"); } return 0; }