uva 10673 Play with Floor and Ceil

解题思路:扩展 gcd 或者 直接判断 x%k 是否等于 0, 若是,可取 p=0, q=k; 否则,可取 p=-x, q=x.

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: uva 10673
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 #pragma comment(linker,"/STACK:1024000000,1024000000")
26 
27 #define lson l,m,rt<<1
28 #define rson m+1,r,rt<<1|1
29 ///////////////////////////////////////////////////////////////////////////
30 
31 ///////////////////////////////////////////////////////////////////////////
32 const double EPS=1e-9;
33 const double PI=acos(-1.0);
34 const double E=2.7182818284590452353602874713526;
35 
36 const int x4[]={-1,0,1,0};
37 const int y4[]={0,1,0,-1};
38 const int x8[]={-1,-1,0,1,1,1,0,-1};
39 const int y8[]={0,1,1,1,0,-1,-1,-1};
40 ///////////////////////////////////////////////////////////////////////////
41 
42 ///////////////////////////////////////////////////////////////////////////
43 typedef long long LL;
44 
45 typedef int T;
46 T max(T a,T b){ return a>b? a:b; }
47 T min(T a,T b){ return a<b? a:b; }
48 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
49 T lcm(T a,T b){ return a/gcd(a,b)*b; }
50 ///////////////////////////////////////////////////////////////////////////
51 
52 ///////////////////////////////////////////////////////////////////////////
53 //Add Code:
54 LL exgcd(LL a,LL b,LL &d,LL &x,LL &y){
55     if(b==0) d=a,x=1,y=0;
56     else{
57         exgcd(b,a%b,d,y,x);
58         y-=x*(a/b);
59     }
60 }
61 ///////////////////////////////////////////////////////////////////////////
62 
63 int main(){
64     ///////////////////////////////////////////////////////////////////////
65     //Add Code:
66     int Case;
67     scanf("%d",&Case);
68     while(Case--){
69         LL x,k,a,b,d,p,q;
70         scanf("%lld%lld",&x,&k);
71         if(x%k==0) a=x/k,b=a;
72         else a=x/k,b=a+1;
73         exgcd(a,b,d,p,q);
74         printf("%lld %lld
",x/d*p,x/d*q);
75     }
76     ///////////////////////////////////////////////////////////////////////
77     return 0;
78 }
79 
80 ///////////////////////////////////////////////////////////////////////////
81 /*
82 Testcase:
83 Input:
84 3
85 5 2
86 40 2
87 24444 6
88 Output:
89 1 1
90 1 1
91 0 6
92 */
93 ///////////////////////////////////////////////////////////////////////////

 1 ///////////////////////////////////////////////////////////////////////////
 2 //problem_id: uva 10673
 3 //user_id: SCNU20102200088
 4 ///////////////////////////////////////////////////////////////////////////
 5 
 6 #include <algorithm>
 7 #include <iostream>
 8 #include <iterator>
 9 #include <iomanip>
10 #include <cstring>
11 #include <cstdlib>
12 #include <string>
13 #include <vector>
14 #include <cstdio>
15 #include <cctype>
16 #include <cmath>
17 #include <queue>
18 #include <stack>
19 #include <list>
20 #include <set>
21 #include <map>
22 using namespace std;
23 
24 ///////////////////////////////////////////////////////////////////////////
25 #pragma comment(linker,"/STACK:1024000000,1024000000")
26 
27 #define lson l,m,rt<<1
28 #define rson m+1,r,rt<<1|1
29 ///////////////////////////////////////////////////////////////////////////
30 
31 ///////////////////////////////////////////////////////////////////////////
32 const double EPS=1e-9;
33 const double PI=acos(-1.0);
34 const double E=2.7182818284590452353602874713526;
35 
36 const int x4[]={-1,0,1,0};
37 const int y4[]={0,1,0,-1};
38 const int x8[]={-1,-1,0,1,1,1,0,-1};
39 const int y8[]={0,1,1,1,0,-1,-1,-1};
40 ///////////////////////////////////////////////////////////////////////////
41 
42 ///////////////////////////////////////////////////////////////////////////
43 typedef long long LL;
44 
45 typedef int T;
46 T max(T a,T b){ return a>b? a:b; }
47 T min(T a,T b){ return a<b? a:b; }
48 T gcd(T a,T b){ return b==0? a:gcd(b,a%b); }
49 T lcm(T a,T b){ return a/gcd(a,b)*b; }
50 ///////////////////////////////////////////////////////////////////////////
51 
52 ///////////////////////////////////////////////////////////////////////////
53 //Add Code:
54 ///////////////////////////////////////////////////////////////////////////
55 
56 int main(){
57     ///////////////////////////////////////////////////////////////////////
58     //Add Code:
59     int Case;
60     scanf("%d",&Case);
61     while(Case--){
62         LL x,k,p,q;
63         scanf("%lld%lld",&x,&k);
64         if(x%k==0) p=0,q=k;
65         else p=-x,q=x;
66         printf("%lld %lld
",p,q);
67     }
68     ///////////////////////////////////////////////////////////////////////
69     return 0;
70 }
71 
72 ///////////////////////////////////////////////////////////////////////////
73 /*
74 Testcase:
75 Input:
76 3
77 5 2
78 40 2
79 24444 6
80 Output:
81 1 1
82 1 1
83 0 6
84 */
85 ///////////////////////////////////////////////////////////////////////////
原文地址:https://www.cnblogs.com/linqiuwei/p/3325349.html