题目链接: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. Eclipse下的java工程目录问题和路径问题理解

    1.Eclipse下的java工程都有哪些文件夹? 答:new java project时,会默认创建SRC源代码目录,并默认创建一个bin目录作为输出目录,输出目录是指生成的class文件和配置文件 ...

  2. Calender

    public static void main(String[] args) { // TODO 自动生成的方法存根 Calendar c = new GregorianCalendar(); c., ...

  3. flex布局下img变形的问题

    flex-shrink  加上:flex-shrink:0:定义了缩小比例,默认为1,即如果空间不足,项目将会缩小所有项目为1时,空间不足,都会缩小,如果你不希望某个容器在任何时候都不被压缩,那设置f ...

  4. 中文版 ImageNet Classification with Deep Convolutional Neural Networks

    ImageNet Classification with Deep Convolutional Neural Networks 摘要 我们训练了一个大型深度卷积神经网络来将ImageNet LSVRC ...

  5. 新疆大学OJ(ACM) 1099: 数列有序!

    1099: 数列有序! 时间限制: 1 Sec  内存限制: 128 MB 题目描述 有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的 ...

  6. MySQL用户添加和分配权限

    mysql数据库insertdelete服务器file mysql> grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口 ...

  7. 利用SignalR来同步更新Winfrom

    之前写了个用Socket来更新多个Winfrom的试例,这两天看了下SignalR,也用这个来试一下 SignalR 地址:https://www.asp.net/signalr 我这个也是基于 ht ...

  8. FCC高级编程篇之Make a Person

    Make a Person Fill in the object constructor with the following methods below: getfirstname() getLas ...

  9. 史上最简单的在 Yii2.0 中将数据导出成 Excel

    在 vendor/yiisoft/yii2/helpers/ 创建一个 Excel.php <?php namespace yii\helpers;   class Excel{         ...

  10. How Javascript works (Javascript工作原理) (九) 网页消息推送通知机制

    个人总结: 1.介绍了网页消息推送通知机制 全文地址:https://github.com/Troland/how-javascript-works 这是 JavaScript 工作原理的第九章. 现 ...