Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 17877   Accepted: 6021

Description

Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

Choose k different positive integers a1a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) can be used to express m.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

Since Elina is new to programming, this problem is too difficult for her. Can you help her?

Input

The input contains multiple test cases. Each test cases consists of some lines.

  • Line 1: Contains the integer k.
  • Lines 2 ~ k + 1: Each contains a pair of integers airi (1 ≤ i ≤ k).

Output

Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

Sample Input

2
8 7
11 9

Sample Output

31

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

Source

题意:n模k个不同的数,得到相应的余数,求最小的n。 
PS:一般的CRT用来解决模数互素的情况,本题不一定模数互素,因此,一般的中国剩余定理模板是不够的。 
代码:
 #include "bits/stdc++.h"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define inf 0x3f3f3f3f
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
typedef pair<ll,ll> pll;
ll a[N],b[N],m[N];
ll gcd(ll x,ll y) {return y==?x:gcd(y,x%y);}
ll ex_gcd(ll a,ll b,ll& x,ll& y)
{
if(b==){
x = ,y = ;
return a;
}
ll d = ex_gcd(b,a%b,y,x);
y -= a/b*x;
return d;
} ll inv(ll a,ll p)
{
ll d,x,y;
d = ex_gcd(a,p,x,y);
return d==?(x%p+p)%p:-;
}
pll CRT(ll A[], ll B[], ll M[], int n) {//求解A[i]x = B[i] (mod M[i]),总共n个线性方程组
ll x = , m = ;
for(int i = ; i < n; i ++) {
ll a = A[i] * m, b = B[i] - A[i]*x, d = gcd(M[i], a);
if(b % d != ) return pll(, -);//答案不存在,返回-1
ll t = b/d * inv(a/d, M[i]/d)%(M[i]/d);
x = x + m*t;
m *= M[i]/d;
}
x = (x % m + m ) % m;
return pll(x, m);//返回的x就是答案,m是最后的lcm值
} int main()
{
int k;
while(~scanf("%d",&k))
{
for(int i=;i<k;i++)
{
a[i] = ;
scanf("%lld%lld",&m[i],&b[i]);
}
pll ans = CRT(a,b,m,k);
if(ans.second==-) puts("-1");
else pl(ans.first);
}
}

POJ 2891 中国剩余定理(不互素)的更多相关文章

  1. POJ 2891 中国剩余定理的非互质形式

    中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...

  2. poj 1006中国剩余定理模板

    中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...

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

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

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

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

  5. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  6. poj 2891 Strange Way to Express Integers【扩展中国剩余定理】

    扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...

  7. POJ 2891 Strange Way to Express Integers 中国剩余定理解法

    一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...

  8. 中国剩余定理模数不互质的情况(poj 2891

    中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <io ...

  9. 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms

    题目链接: http://poj.org/problem?id=1006 http://acm.hdu.edu.cn/showproblem.php?pid=1370 题目大意: (X+d)%23=a ...

随机推荐

  1. arcgis Flex QueryTask

    <esri:Map id="myMap" creationComplete="useMapServicePermaLink()" load="u ...

  2. python模块详解 YAML和configparser

    YAML模块 主要用来做配置文件用的. yaml格式: 详细参考官方文档:PyYAML configparser模块 同样是用于生成和修改配置文件用的.格式和mysql一样. 生成配置文件 impor ...

  3. centreon公司推出的check plugin pack

    文档 http://documentation.centreon.com/docs/centreon-plugins/en/latest/ (epel) # yum install nagios-pl ...

  4. shell脚本学习(1)列出一组IP内所有活动主机

    1.用ping命令编写脚本来查询一组IP地址同时检查他们是否处于活动状态 [zhi@centos7 ~]$ vi CheckHostStatus.sh [zhi@centos7 ~]$ ll 总用量 ...

  5. kubernetes组件helm

    1.安装helm Helm由客户端helm命令行工具和服务端tiller组成,Helm的安装十分简单. 下载helm命令行工具到master节点node1的/usr/local/bin下(只需要在其中 ...

  6. TP5.1:数据库的增删改查操作(基于数据库操作)

    1.在app/index/controller文件夹下创建一个文件,名为:Operation 注意:起名一定要避开关键字,例如:mysql,curd等等,如果使用关键字起名,会造成报错! 在Opera ...

  7. Maven一些零散的知识点

    Maven常用命令: 1. 创建Maven的普通java项目: mvn archetype:create -DgroupId=com.yida.framework -DartifactId=hello ...

  8. Android(java)学习笔记72:ProgressBar的使用

    1. ProgressBar使用 首先我们看例程如下: (1) main.xml文件如下: <?xml version="1.0" encoding="utf-8& ...

  9. bzoj3882 [Wc2015]K小割

    Description Input Output Sample Input 3 3 1 3 100 1 2 3 2 3 4 1 3 5 Sample Output 8 9 12 -1   正解:暴搜+ ...

  10. 贪心,二叉树搜索,ZOJ(2315)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1315 解题报告: #include <stdio.h> ...