Topic Link: Click to open the link
Test instructions: Gives a solution of X, K for equation P*floor (x/k) +q*ceil (x/k) =x. Floor () is rounded up and ceil () is rounded down.
Naked extension gcd, the title does not say the case of no solution, should be the default X%GCD (Floor (x/k), Ceil (x/k)) ==0
For extended Gcd,ax+by=d①, when D is a multiple of G=GCD (A, b), the equation ① has a solution that translates to the solution of Ax+by=g②, assuming that the solution of the equation ② is (x0,y0)
The equation ② around the same multiply d/g, get a A * (x0*d/g) +b* (y0*d/g) =g*d/g, so the solution of the equation ① is (x0*d/g,y0*d/g)
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 1002#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 10000007#define pp pair<int,int> #define ull unsigned long longusing names Pace Std;ll X,k;ll gcd (ll A,ll b) {return b==0?a:gcd (b,a%b);} ll EX_GCD (ll a,ll b,ll& d,ll& x,ll& y) {if (!b) {d=a;x=1;y=0;} ELSE{EX_GCD (b,a%b,d,y,x); y-=x* (A/b);}} void Solve () {ll a=floor (double) x/(double) k); ll B=ceil (double) x/(double) k); ll G=GCD (A, b); ll D=g,tx,ty;a/=g;b/=g;ex _GCD (a,b,d,tx,ty);p rintf ("%lld%lld\n", tx*x/g,ty*x/g);} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%lld%lld", &x,&k); solve ();} return 0;}
Uva 10673-play with Floor and ceil (extended Euclidean)