1. 程式人生 > >hdu5974Math Problem(數學,思維,公式)

hdu5974Math Problem(數學,思維,公式)

cst tac 發現 mat code 解決 pac cstring queue

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5974

分析和思路:

數學題,推公式。這種大量的多組輸入暴力解決是不可能的,一般是規律或者推公式。

根據 最大公約*最小公倍=x*y 推出如下解題公式

x*x-a*x+最大公約數*b=0

到了這裏發現還多一個未知數最大公約(當然這也是本題最難最關鍵的部分),其實可以取巧,充分利用給出的條件a,b和樣例2結果猜想驗證一下,可以很驚喜的發現gcd(a,b)=gcd(x,y)!程序可以完美解方程解決。

至於真正的推導過程有些不太好想,

x+y=a, x*y/gcd(x,y)=b

令 gcd(x,y)=c 、x=i*c y=j*c

i*c+j*c=a ,c*i*j=b c*(i+j)=a ,

c*i*j=b因為 i j 互質 所以 gcd(a,b)=c=gcd(x,y)!最重要的結論!

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <iomanip>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <stack>
 9
#include <queue> 10 #include <cstdio> 11 #include <cstring> 12 #include <cmath> 13 using namespace std; 14 typedef long long ll; 15 typedef unsigned long long ull; 16 const int maxn=1e4+5; 17 int vis[maxn]; 18 19 ll gcd(ll a,ll b) 20 { 21 return b?gcd(b,a%b):a; 22 } 23 24
int main() 25 { 26 ios::sync_with_stdio(false); cin.tie(0); 27 28 ll a,b; 29 while(cin>>a>>b) 30 { 31 ll c=gcd(a,b); 32 c*=b; 33 ll dt=a*a-4*c; 34 35 if(dt>=0)//根號下>=0才有解 36 { 37 ll gdt=sqrt(dt); 38 if(gdt*gdt==dt)//開方是整數 39 { 40 ll fz1=a+gdt,fz2=a-gdt; 41 if(fz1%2==0 && fz2%2==0)//兩個分子/2是整數 42 { 43 cout<<min(fz1/2,fz2/2)<< <<max(fz1/2,fz2/2)<<endl; 44 } 45 else 46 { 47 cout<<"No Solution"<<endl; 48 } 49 } 50 else 51 { 52 cout<<"No Solution"<<endl; 53 } 54 } 55 else 56 { 57 cout<<"No Solution"<<endl; 58 } 59 } 60 61 return 0; 62 }

完。

hdu5974Math Problem(數學,思維,公式)