#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,a,lcm,now;
bool flag;
void exgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(b==)
{
d=a;
x=;
y=;
}
else
{
exgcd(b,a%b,d,x,y);
ll t=x;
x=y;
y=t-a/b*y;
}
}
int main()
{
ll d,x,y,k;
while(~scanf("%lld",&n))
{
scanf("%lld%lld",&m,&a);
lcm=m;now=a;flag=true;
for(int i=;i<n;i++)
{
scanf("%lld%lld",&m,&a);
a=((a%m)-(now%m)+m)%m;
exgcd(lcm,m,d,x,y);
if(a%d==)
k=(x*(a/d)%(m/d)+(m/d))%(m/d);
else flag=false;
now+=k*lcm;
lcm=lcm*m/d;
now=((now%lcm)+lcm)%lcm;
}
if(!flag) printf("-1");
else printf("%lld",now);
puts("");
}
return ;
}

1635:【例 5】Strange Way to Express Integers的更多相关文章

  1. 一本通1635【例 5】Strange Way to Express Integers

    1635:[例 5]Strange Way to Express Integers sol:貌似就是曹冲养猪的加强版,初看感觉非常没有思路,经过一番艰辛的***,得到以下的结果 随便解释下给以后的自己 ...

  2. Strange Way to Express Integers

    I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na​ ...

  3. 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)

    0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...

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

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

  5. POJ2891——Strange Way to Express Integers(模线性方程组)

    Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...

  6. [POJ 2891] Strange Way to Express Integers

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

  7. 数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

    F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format: ...

  8. Strange Way to Express Integers(中国剩余定理+不互质)

    Strange Way to Express Integers Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...

  9. POJ2891 Strange Way to Express Integers

    题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...

  10. poj 2981 Strange Way to Express Integers (中国剩余定理不互质)

    http://poj.org/problem?id=2891 Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 13 ...

随机推荐

  1. (三十)JSP标签之自定义标签

    创建一个类,引入外部jsp-api.jar包(在tomcat 下lib包里有),这个类继承SimpleTagSupport 重写doTag()方法. jspprojec包下的helloTag类: 1 ...

  2. centos从安装到环境配置

    1.安装centos6.5    http://jingyan.baidu.com/article/25648fc1a235c99191fd0008.html 2.配置网络ip   http://ji ...

  3. mpeg1、mpeg2和mpeg4标准对比分析和总结

    mpeg1.mpeg2和mpeg4标准对比分析和总结 来源 https://blog.csdn.net/SoaringLee_fighting/article/details/83627824 mpe ...

  4. JS 学习书籍电子版PDF下载

    JavaScript权威指南(第6版)(中文版) 链接:https://pan.baidu.com/s/1H1v77UY-yh7oDxonRjd0GA 提取码:r3pu JavaScript DOM编 ...

  5. 解决ios8下coreData没有NSPersistentContainer的问题

    用Xcode8.1默认创建ios app的时候,使用coreData的话,要10.0以上的版本才行.因为NSPersistentContainer只有10.0以上的版本才有,10.0以下的版本是没有的 ...

  6. 【python】python3连接mysql数据库

    一.安装pymysql 详见http://www.runoob.com/python3/python3-mysql.html 二.连接mysql数据库 db = pymysql.connect( #连 ...

  7. 05 Windows编程——Windows程序框架

    源码 #include<Windows.h> LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARA ...

  8. Linux行编辑器——ed

    实验文件test.txt内容 root:x:::root:/root:/bin/bash bin:x:::bin:/bin:/sbin/nologin daemon:x:::daemon:/sbin: ...

  9. GNS3

    什么是GNS? GNS Graphical Network Simulator Simulator or Emulator? 尽管GNS全拼包含simulator,但实际上是emulator.我们说其 ...

  10. Nginx 的关键组件的介绍

    本文章主要介绍Nginx本身功能,不依赖第三方的任何模块.介绍常用的组件功能:反向代理.负载均衡.Http服务器.正向代理 反向代理(Reverse Proxy): 什么是反向代理呢?本人最直接的理解 ...