标签:with int vector min algorithm img logs scan 16px
求不定方程 $$x = p \lfloor {x \over k} \rfloor + q \lceil {x \over k} \rceil$$ 的一组整数解,$x$,$k$给出。
我们发现 $\lfloor {x \over k} \rfloor$ 和 $\lceil {x \over k} \rceil$ 只有两种情况:
1. 相差$1$,显然$gcd(\lfloor {x \over k} \rfloor,\lceil {x \over k} \rceil)=1$。显然有整数解。
2. 相等,则$k|x$,所以$gcd|x$,也有整数解。
用扩欧求出方程$$gcd(\lfloor {x \over k} \rfloor,\lceil {x \over k} \rceil) = p \lfloor {x \over k} \rfloor + q \lceil {x \over k} \rceil$$一组解,
最后将答案乘上$x \over gcd$即可。
1 #include <set> 2 #include <map> 3 #include <ctime> 4 #include <cmath> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <cstdio> 9 #include <string> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 #define LL long long 15 #define Max(a, b) ((a) > (b) ? (a) : (b)) 16 #define Min(a, b) ((a) < (b) ? (a) : (b)) 17 #define sqr(x) ((x)*(x)) 18 using namespace std; 19 20 LL t, x, k; 21 22 LL exgcd(LL a, LL b, LL &x, LL &y) { 23 if (b == 0) { 24 x = 1, y = 0; 25 return a; 26 } 27 LL c = exgcd(b, a%b, x, y); 28 LL t = x; 29 x = y; 30 y = t-y*(a/b); 31 return c; 32 } 33 34 int main() { 35 scanf("%lld", &t); 36 while (t--) { 37 scanf("%lld%lld", &x, &k); 38 LL p, q; 39 LL tmp = exgcd(x/k, x/k+(bool)(x%k), p, q); 40 printf("%lld %lld\n", p*x/tmp, q*x/tmp); 41 } 42 return 0; 43 }
[UVa 10673]Play with Floor and Ceil
标签:with int vector min algorithm img logs scan 16px
原文地址:http://www.cnblogs.com/NaVi-Awson/p/7494118.html