题目链接:http://poj.org/problem?id=2891

题目大意:

求解同余方程组,不保证模数互质

题解:

扩展中国剩余定理板子题

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll; const int N=+;
int k;
ll m[N],a[N];
inline ll read()
{
char ch=getchar();
ll s=,f=;
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<='') {s=(s<<)+(s<<)+ch-'';ch=getchar();}
return s*f;
}
ll mul(ll a,ll b,ll mod)
{
ll re=;
for (;b;b>>=,a=(a+a)%mod) if (b&) re=(re+a)%mod;
return re;
}
ll exgcd(ll a,ll b,ll &x0,ll &y0)
{
if (!b)
{
x0=;y0=;
return a;
}
ll gcd=exgcd(b,a%b,y0,x0);
y0-=a/b*x0;
return gcd;
}
ll excrt()
{
ll M=m[],x=a[];
for (int i=;i<=k;i++)
{
ll mi=m[i],ai=a[i];
ll x0,y0;
ll gcd=exgcd(M,mi,x0,y0);
ll c=(ai-x%mi+mi)%mi;
if (c%gcd) return -;
x0=mul(x0,c/gcd,mi/gcd);
//x0=x0*c/gcd%(mi/gcd);
x+=x0*M;
M=M/gcd*mi;
x%=M;
}
return (x%M+M)%M;
}
int main()
{
//freopen("excrt.in","r",stdin);
//freopen("excrt.out","w",stdout);
//scanf("%d",&k);
while (~scanf("%d",&k))
{
for (int i=;i<=k;i++) m[i]=read(),a[i]=read();
printf("%lld\n",excrt());
}
return ;
}

[poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)的更多相关文章

  1. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  2. 「POJ2891」Strange Way to Express Integers【数学归纳法,扩展中国剩余定理】

    题目链接 [VJ传送门] 题目描述 给你\(a_1...a_n\)和\(m_1...m_n\),求一个最小的正整数\(x\),满足\(\forall i\in[1,n] \equiv a_i(mod ...

  3. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  4. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

  5. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  6. poj 2891 Strange Way to Express Integers(中国剩余定理)

    http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...

  7. POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd

    http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...

  8. POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法

    http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...

  9. POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】

    求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...

随机推荐

  1. ZOJ 2836

    求不比M大的可以被集合任一个数整除的数的个数.(容斥原理) #include <iostream> #include <cstdio> #include <algorit ...

  2. 【剑指offer】Q31:连续子数组的组大和

    简短的分析见:http://blog.csdn.net/shiquxinkong/article/details/17934747 def FindGreatestSumOfSubArray(arra ...

  3. Unsupported major.minor version 51.0问题的解决

    在java编程的过程中,当用myeclipse软件打开别人写的代码时,遇到Unsupported major.minor version 51.0此类问题,实在是令人痛苦不堪.弄了整整一晚才搞清楚,我 ...

  4. jms及active(jdk api)的实现

    在企业中,分布式的消息队列需要实现的问题: 1.不同的业务系统分别处理同一个消息(订阅发布),同一个业务系统负载处理同一类消息(队列模式) 2.消息的一致性问题,在互联网公司中一般不要求强一致性,一般 ...

  5. http://www.secrepo.com 安全相关的数据获取源

    来自:http://www.secrepo.com Network MACCDC2012 - Generated with Bro from the 2012 dataset A nice datas ...

  6. 移动端fixed后 横竖屏切换时上部或下部出现空隙问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 爬取xml数据之R

    生物信息很多时候要爬数据.最近也看了一些这些方面的. url<-"要爬取的网址" url.html<-htmlParse(url,encoding="UTF- ...

  8. 【原创】RPM安装软件时解决依赖性问题(自动解决依赖型)

    满足以下3个条件才能自动解决依赖性: 1.使用rpmdb -redhat(在安装时会自动弹出依赖性错误) 2.所有互相依赖的软件都必须在同一个目录下面. 3.调用-aid参数.

  9. PC比价软件

    方法:从百度中搜索比较软件排行,并且根据将搜索时间缩小在一年内.统计前面一页结果网站中的比价软件. 名称 备注 统计 淘淘搜购物助手 淘宝购物比价神器 1 省省比价软件 主流网购商城进行横向比较的智能 ...

  10. ZBrush中功能强大的插件PaintStop

    PaintStop是ZBrush®3.1的手绘插件,可以比较真实的模拟手绘风格,尤其是用水彩笔刷画水墨风格画.PaintStop插件可供用户免费使用. PaintStop是一款功能强大的插件,已经被添 ...