中国剩余定理
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 = 100; 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 a[maxn + 5], b[maxn + 5], d, res; 59 ll gcd(ll a, ll b) { 60 return b ? gcd(b, a % b) : a; 61 } 62 ll lcm(ll a, ll b) { 63 return a / gcd(a, b) * b; 64 } 65 void ex_gcd(ll a, ll b, ll &x, ll &y, ll &d) { 66 if (!b) { d = a, x = 1, y = 0; } 67 else { 68 ex_gcd(b, a % b, y, x, d); 69 y -= x * (a / b); 70 } 71 } 72 ll inv(ll t, ll p) {//如果不存在,返回-1 73 ll d, x, y; 74 ex_gcd(t, p, x, y, d); 75 return d == 1 ? (x % p + p) % p : -1; 76 } 77 ll Quick_mul(ll a, ll b, ll mod) { 78 ll res = 0; 79 while (b) { 80 if (b & 1)res = (res + a) % mod; 81 a = 2 * a % mod; 82 b >>= 1; 83 } 84 return res; 85 } 86 int n; 87 int main() 88 { 89 n = read_int(); 90 for (int i = 1; i <= n; ++i)a[i] = read_int(); 91 for (int i = 1; i <= n; ++i)b[i] = read_int(); 92 for (int i = 1; i <= n; ++i)a[i] = (a[i] % b[i] + b[i]) % b[i]; 93 d = b[1]; 94 for (int i = 2; i <= n; ++i)d = lcm(d, b[i]); 95 for (int i = 1; i <= n; ++i) { 96 ll p = d / b[i]; 97 ll g = inv(p, b[i]); 98 res = (res + Quick_mul(Quick_mul(a[i], p, d), g, d)) % d; 99 } 100 res = (res % d + d) % d; 101 cout << res << endl; 102 //system("pause"); 103 return 0; 104 }
以上是关于中国剩余定理的主要内容,如果未能解决你的问题,请参考以下文章