51Nod - 1079 中国剩余定理
Posted zhuiyicc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了51Nod - 1079 中国剩余定理相关的知识,希望对你有一定的参考价值。
直接就是板子了,但是呢,我去了个重。
#include<cstdio> #include<cstdlib> #include<iostream> #include<string> #include<set> #include<algorithm> #include<vector> #include<queue> #include<list> #include<cmath> #include<cstring> #include<map> #include<stack> using namespace std; #define INF 0x3f3f3f3f #define maxn 1005 #define ull unsigned long long #define ll long long #define hashmod 99999839 #define mod 9997 struct func{ ll p; ll y; }a[maxn]; int n; ll m[maxn],y[maxn]; bool cmp(const func& a,const func& b){ if(a.p != b.p) return a.p < b.p; return a.y < b.y; } ll excgcd(ll a,ll b,ll& x,ll& y){ if(!b){x = 1,y = 0;return a;} ll g = excgcd(b,a % b,x,y); ll t = x; x = y; y = t - a/b * y; return g; } void check(){ sort(a + 1,a + n + 1,cmp); int j = 2; for(int i = 2;i <= n;++i){ if(a[i].p == a[i-1].p && a[i].y == a[i-1].y) continue; a[j] = a[i]; ++j; } n = j - 1; } void solve(){ ll M = 1,x = 0,y = 0,ans = 0;//[m_1,m_2,...,m_n] for(int i = 1;i <= n;++i) M *= a[i].p; for(int i = 1;i <= n;++i){ m[i] = M / a[i].p; ll g = excgcd(m[i],a[i].p,x,y); x = x + (abs(x)/(a[i].p/g) + 1) * (a[i].p/g); x = x % (a[i].p/g);//将x扩展为正数 ans = ans + x * m[i] * a[i].y; if(ans >= M) ans %= M; } printf("%lld ",ans); } int main(){ // freopen("a.in","r",stdin); // freopen("b.out","w",stdout); while(~scanf("%d",&n)){ for(int i = 1;i <= n;++i) scanf("%lld%lld",&a[i].p,&a[i].y); check(); solve(); } return 0; }
以上是关于51Nod - 1079 中国剩余定理的主要内容,如果未能解决你的问题,请参考以下文章