Problem A Play with Floor and Ceil http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=18&page=show_problem&problem=1614

扩展gcd求解2元不定式 ax+by=c    d=gcd(a,b)=ax+by;   x=x*c/d  y=y*c/d

 1 #include<cstdio>
 2 typedef long long LL;
 3 LL ext_gcd(LL a,LL b,LL &x,LL &y) { //扩展gcd d=gcd(a,b)=a*x+b*y; return d,x,y;
 4     LL t,ret;
 5     if(!b) {
 6         x=1;
 7         y=0;
 8         return a;
 9     }
10     ret=ext_gcd(b,a%b,x,y);
11     t=x;
12     x=y;
13     y=t-a/b*y;
14     return ret;
15 }
16 int main(){
17     int t,x,k;
18     while(~scanf("%d",&t)){
19         while(t--){
20             scanf("%d%d",&x,&k);
21             LL a=x/k;
22             LL b=a;
23             if(x%k) b++;
24             LL tx,ty;
25             LL d=ext_gcd(a,b,tx,ty);
26             printf("%lld %lld\n",tx*x/d,ty*x/d);
27         }
28     }
29     return 0;
30 }
View Code

相关文章: