1. 程式人生 > >51Nod - 1079 中國剩余定理

51Nod - 1079 中國剩余定理

() cst ++ names string return print math tor

直接就是板子了,但是呢,我去了個重。

#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\n",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 中國剩余定理