\(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. mybatis 框架 的应用之四(一对一 与 一对多)

    lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&u ...

  2. Luogu 3899 [湖南集训]谈笑风生

    BZOJ 3653权限题. 这题方法很多,但我会的不多…… 给定了$a$,我们考虑讨论$b$的位置: 1.$b$在$a$到根的链上,那么这样子$a$的子树中的每一个结点(除了$a$之外)都是可以成为$ ...

  3. c# 多维数组、交错数组(转化为DataTable)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. CLR 显示实现事件 EventSet内部管理一个字典

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  5. 浅谈android代码保护技术_加固

    可看原文: http://www.cnblogs.com/jiaoxiake/p/6536824.html 导语 我们知道Android中的反编译工作越来越让人操作熟练,我们辛苦的开发出一个apk,结 ...

  6. Appium移动端自动化测试之应用操作详解(四)

    应用操作篇 1.1).安装应用 desired_caps = { 'platformName': 'Android', 'platformVersion': '5.0.0.0', 'deviceNam ...

  7. Controlling Session Behavior in Asp.Net MVC4

    Posted By : Shailendra Chauhan, 06 Jan 2013 Updated On : 11 Jun 2014 By default, Asp.Net MVC support ...

  8. Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)

    题目链接 Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occa ...

  9. Spring Websocket与sockJS结合实现

    首先了解Stomp协议:简单的消息文本协议 采用广播机制与队列,可以和Activemq结合使用 Stomp协议学习与实战 http://diaocow.iteye.com/blog/1725186 S ...

  10. 在sql server数据库可以插入在回车的数据

    insert into t_FamilyClass (id,ParentId,Name) values(111,111,'111')可以在编辑模式下copy到editplus中,设置 显示 空白字符: ...