int main() { int x(0); int y(0); int d = extgcd(A, B, x, y); if (d == 1) { if (0 <= x) { std::printf("%d * %d + ", A, x); } else { std::printf("%d * (%d) + ", A, x); } if (0 <= y) { std::printf("%d * %d = 1\n", B, y); } else { std::printf("%d * (%d) = 1\n", B, y); } } return 0; }
pii extgcd(int a, int b){ if(b == 0) return make_pair(1, 0); else{ int p = a / b; pii q = extgcd(b, a % b); return make_pair(q.second, q.first - q.second * p); } }
long long extgcd( long long a,long long b,long long &x,long long &y ) { long long ret=a; if ( b ) { ret=extgcd( b,a%b,y,x ); y-=( a/b )*x; } else x=1LL,y=0LL; return ret; }
// @desc mod mでaの逆元を求める template <class T> T mod_inverse( T a, T m ) { T x, y; extgcd( a, m, x, y ); x %= m; while ( x < 0 ) x += m; return x; }
// 拓展欧几里得算法 // 有如下事实 // 1. a*x+b*y=gcd(a, b) ab不同为0,ab为整数,一定存在整数解 // 2. gcd(a, b) = gcd(b, a mod b) // // 那么 a*x+b*y=gcd(a, b)=gcd(b, a mod b)=b*x'+(a mod b)*y' // 即 a*x+b*y = b*x'+(a-(a/b)*b)*y' // a*x+b*y = a*y'+b*(x'-(a/b)*y') // => x <- y', y <- (x'-(a/b)*y') int extgcd(int a, int b, int& x, int& y) { if (b == 0) { x = 1; y = 0; return a; }; int gcd = extgcd(b, a % b, x, y); int t = y; y = x - (a / b)*y; x = t; return gcd; }
int extgcd(int a, int b, int& x, int& y){ int d = a; if (b != 0){ d = extgcd(b, a%b, y, x); y -= (a/b) * x; } else { x = 1; y=0; } return d; }
void extgcd(int a,int b){ if(!b){ if(c%a) ok=false; else ok=true,x=c/a; y=0; g=a>0?a:-a; return; } extgcd(b,a%b); if(!ok) return; int t=x; x=y; y=t-a/b*y; }
int main() { GEN x, y, d, u, v; pari_init(1000000,2); printf("x = "); x = gp_read_stream(stdin); printf("y = "); y = gp_read_stream(stdin); d = extgcd(x, y, &u, &v); pari_printf("gcd = %Ps\nu = %Ps\nv = %Ps\n", d, u, v); pari_close(); return 0; }
//(x+kb,y-ka),(x,y) void extgcd(long long a,long long b,long long *d,long long *x,long long *y) { if(!b) { *d=a; *x=1; *y=0; } else { extgcd(b,a%b,d,y,x); *y-=(*x)*(a/b); } }
//https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a % b, x, y); int t = x; x = y; y = t - a / b * y; return d; }
Int divide(Int a, Int b, Int m) { Int g = gcd(a, m); if (b%g != 0) throw no_solution(); Int x, y; extgcd(a, m, x, y); assert(a*x+m*y == gcd(a,m)); Int n = x*b/g; Int dn = m/g; n -= n/dn*dn; if (n < 0) n += dn; return n; }
int main() { long long a,b,c,k; while ( ~scanf( "%I64d%I64d%I64d%I64d",&a,&b,&c,&k ) ) { if ( !a&&!b&&!c&&!k )break; long long x,y; long long z=extgcd( c,1LL<<k,x,y ); long long tp=( b-a )/z; if ( tp*z==( b-a ) ) { long long t=x; t*=tp; long long mod = ( 1LL<<k )/z; t%=mod; t+=mod; t%=mod; printf( "%I64d\n",t ); } else printf( "FOREVER\n" ); } return 0; }
int main (void) { int x,y,m,n; int x1,y1; int a,b,c; while(scanf("%d %d %d %d %d",&x,&y,&m,&n,&b)!=EOF) { a=m-n; c=y-x; int r=extgcd(a,b,x1,y1); if(c%r) printf("Impossible\n"); else { x1=(c*x1/r)%b; if(x1<0)x1+=b; printf("%d\n",x1); } } return 0; }
int extoclean(int x, int y, int z) { int ret = extgcd(x, y); int ans; if (z%ret) return -1; ans = ((long long)(z/ret)*k1%(y/ret)); if (ans < 0) ans += y/ret; return ans; }
int mod_inverse(int a, int m) { int x, y; extgcd(a, m, x, y); return (m + x % m) % m; }
T extgcd(T a, T b, T& x, T& y) { T g = 1; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; }
int main() { long long p1, v1, p2, v2, l; long long t, disv, dist, td; long long temp; long long a, b, d; long long x, y; while(scanf("%lld %lld %lld %lld %lld", &p1, &p2, &v1, &v2, &l) != EOF) { while(p1 >= l) p1 -= l; while(p2 >= l) p2 -= l; if (v1 < v2) { temp = v1; v1 = v2; v2 = temp; temp = p1; p1 = p2; p2 = temp; } if (p1 > p2) dist = l - p1 + p2; else dist = p2 - p1; disv = v1 - v2; if (disv == 0) { if (dist != 0) printf("Impossible\n"); else printf("0\n"); continue; } // printf("%lld %lld %lld %lld\n", p1, p2, l, dist); // disv * x + (-y) * l = dist if (dist % gcd(disv, l) != 0) { printf("Impossible\n"); continue; } extgcd(disv, l, &d, &a, &b); // printf("%lld\n", disv * a - l * b); // printf("%lld %lld %lld %lld %lld %lld\n",disv, l, a, b, disv * a + l * b, d); x = a; y = b; x *= dist / d; y *= dist / d; x -= (x / (l / d)) * (l / d); while (x <= 0) x += l / d; printf("%lld\n", x); } return 0; }
T invMod(T a, T mod) { T x, y; if (extgcd(a, mod, x, y) == -1) return (x + mod) % mod; return 0; }
int mod_inv(int a, int m) { int x, y; extgcd(a, m, x, y); return (m + x) % m; }
Int invMod(Int a, Int m) { Int x, y; if (extgcd(a, m, x, y) == 1) return (x + m) % m; else return 0; // unsolvable }
// a x + b y = gcd(a, b) Int extgcd(Int a, Int b, Int &x, Int &y) { Int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; }
int extgcd(int x, int y) { int ret, tmp; if (!y) { k1 = 1; k2 = 0; return x; } ret = extgcd(y, x%y); tmp = k1-x/y*k2; k1 = k2; k2 = tmp; return ret; }