\(mod\)存在不互素情况下的CRT

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e6+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1;y=0;
return a;
}else{
ll g=exgcd(b,a%b,x,y);
ll tmp=x;
x=y;y=tmp-a/b*x;
return g;
}
}
ll inv(ll n,ll m){
ll g=gcd(n,m);
if(g!=1) return -1;
ll x,y;
exgcd(n,m,x,y);
return (x%m+m)%m;
}
bool merge(ll a1,ll mod1,ll a2,ll mod2,ll &a3,ll &mod3){
ll d=gcd(mod1,mod2);
ll c=a2-a1;
if(c%d!=0)return 0;
mod1/=d; mod2/=d; c/=d;
c*=inv(mod1,mod2);
c%=mod2;
c*=mod1*d;
c+=a1;
mod3=mod1*mod2*d;
a3=(c%mod3+mod3)%mod3;
return 1;
}
ll CRT(ll a[],ll mod[],ll n){
ll a1=a[1];
ll mod1=mod[1];
rep(i,2,n){
ll a2=a[i];
ll mod2=mod[i];
ll mod3,a3;
if(!merge(a1,mod1,a2,mod2,a3,mod3))return -1;
a1=a3;
mod1=mod3;
}
return (a1%mod1+mod1)%mod1;
}
int n;
ll mod[maxn],a[maxn];
int main(){
while(cin>>n){
rep(i,1,n){
mod[i]=read();
a[i]=read();
}
ll ans=CRT(a,mod,n);
println(ans);
}
return 0;
}

POJ - 2891 中国剩余定理的更多相关文章

  1. POJ 1006 中国剩余定理

    #include <cstdio> int main() { // freopen("in.txt","r",stdin); ; while(sca ...

  2. Biorhythms POJ - 1006 中国剩余定理

    定理证明:https://blog.csdn.net/d_x_d/article/details/48466957 https://blog.csdn.net/lyy289065406/article ...

  3. hdu 2891 中国剩余定理

    从6点看到10点,硬是没算出来,早知道玩游戏去了,艹,明天继续看 不爽,起来再看,终于算是弄懂了,以后超过一个小时的题不会再看了,不是题目看不懂,是水平不够 #include<cstdio> ...

  4. POJ 1006-Biorhythms,中国剩余定理,学信安的路过!

                                                       Biorhythms 我竟然1A了, 终于从一天的浑噩中找回点自信了.人生第一次做中国剩余定理的题 ...

  5. POJ 1006:Biorhythms 中国剩余定理

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 Des ...

  6. acm数论之旅--中国剩余定理

    ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*°▽°*)╯)   中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 ...

  7. POJ1006——中国剩余定理

    题目:http://poj.org/problem?id=1006 中国剩余定理:x= m/mj + bj + aj 讲解:http://www.cnblogs.com/MashiroSky/p/59 ...

  8. 《孙子算经》之"物不知数"题:中国剩余定理

    1.<孙子算经>之"物不知数"题 今有物不知其数,三三数之剩二,五五数之剩七,七七数之剩二,问物几何? 2.中国剩余定理 定义: 设 a,b,m 都是整数.  如果 m ...

  9. [TCO 2012 Round 3A Level3] CowsMooing (数论,中国剩余定理,同余方程)

    题目:http://community.topcoder.com/stat?c=problem_statement&pm=12083 这道题还是挺耐想的(至少对我来说是这样).开始时我只会60 ...

随机推荐

  1. 【转】request的cache-control和response cache-control不同点

    原文地址:http://www.cnblogs.com/lwhkdash/archive/2012/11/04/2748291.html HTTP协议中,关于一些头域的解释很模糊,网上的解释有些甚至是 ...

  2. Solidity notes

    1. 查询transaction历史记录 https://forum.ethereum.org/discussion/2116/in-what-ways-can-storage-history-be- ...

  3. jqentitydetail

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  4. codefirst 关系处理

    1.http://www.cnblogs.com/libingql/archive/2013/01/31/2888201.html 2.多对多 protected override void OnMo ...

  5. dev初识 拖动分组

    1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm ...

  6. 基本滤波算法比较 (转载http://blog.sina.com.cn/s/blog_69f2aa5a01014du5.html)

    最近在做关于数据采集方面的东西,这就不免涉及到了滤波的算法,在网上找到了关于几种算法的比较. 数字滤波方法有很多种,每种方法有其不同的特点和使用范围.从大的范围可分为3类. 1.克服大脉冲干扰的数字滤 ...

  7. 在使用webstorm打开本地项目文件夹的html文件时,浏览器提示404错误

    错误原因:在使用webstorm打开本地项目文件夹的html文件时,浏览器提示404错误. 错误分析:文件夹命名内包含“+”,此特殊符号导致浏览器解析错误. 改正方案:去掉特殊符号“+”

  8. 【2008nmj】GDA二元分类.docx

  9. angular 第二种依赖注入

    import { Injectable } from '@angular/core'; import { ProductServiceService, Product } from './produc ...

  10. SSM项目配置文件及其各项使用

    $说明: ·Spring 5  + Mybatis 3.4.5 +SpringMVC  ·使用druid数据库 ·使用log4j输出日志 $Spring 及其配置文件(部分) Spring官方网站:h ...