173 字
1 分钟
大数翻倍法求(拓展)中国剩余定理
暴力
暴力合并每个数和另一个数,算一下要加上多少才能符合。
看着时间复杂度很迷惑,实际上模板题跑的还是很快的。
不过可能又被卡的风险,据zhx说n越小越容易卡,不过顶多卡一个点(
#include<bits/stdc++.h>#define int long longusing namespace std;int n;
void Merge(int &b1, int &a1, int b2, int a2){ if(a1 < a2) swap(b1, b2), swap(a1, a2); while(b1 % a2 != b2) { b1 += a1; } a1 = a1 / __gcd(a1, a2) * a2;}//a为模数,b为余数signed main(){ ios::sync_with_stdio(0), cin.tie(0);
int bstar = 0, astar = 1; int b, a; cin >> n; for(int i = 1;i <= n;i++) { cin >> a >> b; b%= a; Merge(bstar, astar, b, a); }
printf("%lld", bstar); return 0;}
大数翻倍法求(拓展)中国剩余定理
https://blog.histcat.top/posts/double-big-num/