題目鏈接:點擊查看
題目大意:給出 a 和 b ,求解滿足條件的 c,d,e,f ,使得:
d < bf < bc,e 均為小于等于 4e12 的正整數
題目分析:分情況討論一下,首先如果 a 和 b 可以約分的話,那么直接約分后,輸出 a+1 , b , 1 , b 顯然就是答案了,如果不能約分的話,且 b 的相異質因子的個數不超過 1 個的話,那么是無解的,證明如下:(來自官方題解)
最后一種情況就是 b 的相異質因子個數超過一個,對于這種情況可以將條件 1 的公式轉換一下:?
這樣一來,d 和 f 顯然就是 b 的兩個相異質因子了,而 cf - de = a ,在已知 d 和 f 的前提下,一定有解,且可以用擴展歐幾里得來解決,注意最后求出來的答案需要處理一下,因為需要保證 c 和 e 為正整數
在找 d 和 f 的時候也有技巧,如果 sqrt( n ) 暴力去找的話,會超時,所以可以用埃氏篩預處理一下,保存一下每個數字的最大質因子,這樣就可以令 d 為該質因子的冪,然后 f = b / d 了
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e6+100;int pri[N];//pri[i]:i的最大質因子為pri[i]template<class T> void exgcd(T a,T b,T &d,T &x,T &y){if(!b) {d=a;x=1;y=0;}else {exgcd(b,a%b,d,y,x);y-=x*(a/b);}
}
//求解二元一次方程 a*x+b*y=c,一組解為x,y,無解則返回false
template<class T> bool Solve_equation(T a,T b,T c,T &x,T& y){T gcd;exgcd(a,b,gcd,x,y);if(c%gcd) return false; //無解T k=c/gcd;x*=k;y*=k;T xplus=b/gcd,yplus=a/gcd; if(xplus<0) xplus*=-1;if(yplus<0) yplus*=-1;//此時求出的x,y即為一組解,該方程的通解形式為X=x+t*(b/gcd),Y=y-t*(a/gcd) t為任意正整數//根據題目要求我們需要構造特殊解//x=(x%xplus+xplus)%xplus;y=c-a*x; //x的最小正整數解//y=(y%yplus+yplus)%yplus;x=c-b*y; //y的最小正整數解return true;
}void init()
{pri[1]=1;for(int i=2;i<N;i++)if(!pri[i])//當前的數為質數for(int j=i;j<N;j+=i)pri[j]=i;
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);init();int w;cin>>w;while(w--){LL a,b;scanf("%lld%lld",&a,&b);LL gcd=__gcd(a,b);a/=gcd,b/=gcd;if(gcd!=1){printf("%lld %lld %lld %lld\n",a+1,b,1,b);continue;}LL d=1,f=b;while(pri[b]!=1&&f%pri[b]==0){f/=pri[b];d*=pri[b];}if(f==1)//只有一個質因子 {printf("-1 -1 -1 -1\n");continue;}LL c,e;Solve_equation(f,-d,a,c,e);LL k=abs(min(c/d,e/f))+1;c+=d*k,e+=f*k;printf("%lld %lld %lld %lld\n",c,d,e,f);}return 0;
}
?
總結
以上是生活随笔為你收集整理的牛客多校3 - Fraction Construction Problem(扩展欧几里得)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。