1. 程式人生 > >Play with Floor and Ceil UVA - 10673(拓展歐幾裏得)

Play with Floor and Ceil UVA - 10673(拓展歐幾裏得)

拓展歐幾裏得 ace 。。 double algo img 分享圖片 滿足 end

因為我現在還不會用這個。。。emm。。。蒟蒻。。。只看了 從來沒用過。。。。所以切一道水題。。。練一下。。。

人家講的很好 https://blog.csdn.net/u012860428/article/details/41259377

題目大意:求出滿足要求的p和q,使得對於給定的x,k,技術分享圖片,輸出一組滿足要求的p,q即可;

下面對於x,k進行討論;

1、若x能被k整除,那麽只要p+q=k即可;

2、如果不能被其整除,則領技術分享圖片,那麽,x=p*a+q*(a+1);相當於對於不定方程求解,易知,(a,a+1)=1,所以可以先用擴展歐幾裏得算法求出一組滿足

ap + bq= d 的解 其中d = gcd(p,q)

然後 P = p * x/d Q = q * x/d 因為求的是 ap + bq= d 的解 而我們要求ap + bq = x 的解 x = d * x/d 所以 求出的p 和 q 都乘上 x/d即可

即為解

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define
ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 10010, INF = 0x7fffffff; void gcd(LL a, LL b, LL& d, LL& x, LL& y) { if
(!b) { d = a; x = 1; y = 0; } else { gcd(b, a%b, d, y, x); y -= x*(a/b); } } int main() { int T; cin>> T; while(T--) { LL x, y, d, a, b, k, c; cin>> c >> k; if(c % k == 0) { cout<< 1 << " " << k-1 <<endl; } else { a = floor(c/(double)k); b = ceil(c/(double)k); gcd(a, b, d, x, y); x*=c/d; y*=c/d; cout<< x << " " << y <<endl; } } return 0; }

Play with Floor and Ceil UVA - 10673(拓展歐幾裏得)