标签:style blog http color io os ar sp div
原题链接:hdu 1576 A/B
同样是用扩展的欧几里得算法。$A = 9973k+n = xB$从而转化为:$xB-9973k=n$求解$x$即可。
具体扩展欧几里得算法请参考:hdu 2669 Romantic
代码如下:
1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <cstring> 5 using namespace std; 6 typedef long long LL; 7 void exgcd(LL a, LL b, LL &d, LL &x, LL&y) 8 { 9 if( b == 0 ) 10 { 11 d = a; 12 x = 1; 13 y = 0; 14 } 15 else 16 { 17 exgcd(b, a%b, d, x, y); 18 int t = x; 19 x = y; 20 y = t - (a/b)*y; 21 } 22 } 23 int main(int argc, char *argv[]) 24 { 25 int T; 26 scanf ( "%d", &T ); 27 int n, a, b; 28 LL d, x, y; 29 while( T-- ) 30 { 31 scanf("%d%d", &n, &b); 32 a = 9973; 33 exgcd(a, b, d, x, y); 34 y = (y%9973)*(n/d); 35 y %= 9973; 36 if( y < 0 ) 37 { 38 y += 9973; 39 } 40 cout<<y<<endl; 41 } 42 }
标签:style blog http color io os ar sp div
原文地址:http://www.cnblogs.com/jostree/p/4008221.html