扩展中国剩余定理

Posted eterna-king

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扩展中国剩余定理相关的知识,希望对你有一定的参考价值。

技术图片
 1 //author Eterna
 2 #define Hello the_cruel_world!
 3 #pragma GCC optimize(2)
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<cstdio>
 7 #include<string>
 8 #include<cstring>
 9 #include<vector>
10 #include<map>
11 #include<set>
12 #include<queue>
13 #include<stack>
14 #include<utility>
15 #include<cmath>
16 #include<climits>
17 #include<deque>
18 #include<functional>
19 #include<complex>
20 #include<numeric>
21 #include<unordered_map>
22 #define max(x,y) ((x)>(y)?(x):(y))
23 #define min(x,y) ((x)<(y)?(x):(y))
24 #define Pi acos(-1.0)
25 #define ABS(x) ((x) >= 0 ? (x) : (-(x)))
26 #define pb(x) push_back(x)
27 #define lowbit(x) (x & -x)
28 #define FRIN freopen("C:\Users\Administrator.MACHENI-KA32LTP\Desktop\in.txt", "r", stdin)
29 #define FROUT freopen("C:\Users\Administrator.MACHENI-KA32LTP\Desktop\out.txt", "w", stdout)
30 #define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
31 #define outd(x) printf("%d
", x)
32 #define outld(x) printf("lld
", x)
33 #define il inline
34 using namespace std;
35 typedef long long ll;
36 typedef unsigned long long ull;
37 typedef pair<int, int> pii;
38 const int maxn = 1e5;
39 const int INF = 0x7fffffff;
40 const int mod = 1e9 + 7;
41 const double eps = 1e-7;
42 inline int read_int() {
43     char c;
44     int ret = 0, sgn = 1;
45     do { c = getchar(); } while ((c < 0 || c > 9) && c != -);
46     if (c == -) sgn = -1; else ret = c - 0;
47     while ((c = getchar()) >= 0 && c <= 9) ret = ret * 10 + (c - 0);
48     return sgn * ret;
49 }
50 inline ll read_ll() {
51     char c;
52     ll ret = 0, sgn = 1;
53     do { c = getchar(); } while ((c < 0 || c > 9) && c != -);
54     if (c == -) sgn = -1; else ret = c - 0;
55     while ((c = getchar()) >= 0 && c <= 9) ret = ret * 10 + (c - 0);
56     return sgn * ret;
57 }
58 ll Quick_mul(ll x, ll y, ll p) {
59     ll res = 0;
60     while (y) {
61         if (y & 1)res = (res + x) % p;
62         x = 2 * x % p;
63         y >>= 1;
64     }
65     return res;
66 }
67 ll extend_gcd(ll a, ll b, ll& x, ll& y) {
68     ll d = a;
69     if (b != 0) {
70         d = extend_gcd(b, a % b, y, x);
71         y -= (a / b) * x;
72     }
73     else x = 1, y = 0;
74     return d;
75 }
76 ll a[maxn + 5], b[maxn + 5];
77 int n;
78 ll ex_crt() {
79     ll x, y;
80     ll m = b[1], res = a[1];
81     for (int i = 2; i <= n; ++i) {
82         ll g = extend_gcd(m, b[i], x, y), l = b[i] / g, c = (a[i] - res % b[i] + b[i]) % b[i];
83         if (c % g) return -1;
84         x = Quick_mul(x, c / g, l);
85         res += x * m;
86         m *= l;
87         res = (res % m + m) % m;
88     }
89     return res;
90 }
91 int main()
92 {
93     n = read_int();
94     for (int i = 1; i <= n; ++i)b[i] = read_ll(), a[i] = read_ll();
95     cout << ex_crt() << endl;
96     //system("pause");
97     return 0;
98 }
View Code

 

以上是关于扩展中国剩余定理的主要内容,如果未能解决你的问题,请参考以下文章

中国剩余定理与扩展中国剩余定理

中国剩余定理&&扩展中国剩余定理

中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结

欧几里得(辗转相除gcd)扩欧(exgcd)中国剩余定理(crt)扩展中国剩余定理(excrt)简要介绍

Acwing-204-表达整数的奇怪方式(扩展中国剩余定理)

中国剩余定理讲解