POJ 2891 中国剩余定理(不互素)
| 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 a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) 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 ai, ri (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
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 中国剩余定理(不互素)的更多相关文章
- POJ 2891 中国剩余定理的非互质形式
中国剩余定理的非互质形式 任意n个表达式一对对处理,故只需处理两个表达式. x = a(mod m) x = b(mod n) km+a = b (mod n) km = (a-b)(mod n) 利 ...
- poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- 中国剩余定理模数不互质的情况(poj 2891
中国剩余定理模数不互质的情况主要有一个ax+by==k*gcd(a,b),注意一下倍数情况和最小 https://vjudge.net/problem/POJ-2891 #include <io ...
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
题目链接: http://poj.org/problem?id=1006 http://acm.hdu.edu.cn/showproblem.php?pid=1370 题目大意: (X+d)%23=a ...
随机推荐
- Hibernate课程 初探多对多映射2-2 创建持久化类和映射文件
生成实体类 和 cfg.xml配置 cfg.xml 参照一对多映射 实体类如下: Project 类 package com.ddwei.entity; import java.util.HashSe ...
- nopCommerce如何支持MySQL
此方法支持nopCommerce2.4以上版本(缺少的代码,可参照nopCommerce2.6源码) nopCommerce 4.1 如何支持Mysql 请看 Url: http://www.nop ...
- 部分易被忽视的css3属性
1.-webkit-tap-highlight-color 移动端页面点击按钮时会发现按钮上会出现一块阴影,设置-webkit-tap-highlight-color:rgba(0,0,0,0);就可 ...
- JSON中不能加注释
今天犯了一个白痴级的错误,那就是向JSON数据文件中,很多行后面添加注释(Comment,//). 导致Node.js程序不能读取JSON文件,Server启动失败. Debug时间蛮久,经同事提醒才 ...
- tomcat的相关使用
tomcat服务器是apache下非常优秀的一款web服务器,当今的互联网企业中90%左右的中小型企业使用的都是tomcat.tomcat在部署项目时有很多很多的解决方案,这些你都清楚吗? 1.同一个 ...
- [SVN]TortoiseSVN工具培训3─使用基本流程和图标说明
1.SVN的使用基本流程 注意:对于文件编辑方面,上图的编辑副本操作前建议进行Get lock操作,以防出现后续的冲突等异常报错. 2.SVN的基本图标说明
- js中字符串怎么转化为日期
var str = "2010-08-01"; // 转换日期格式 str = str.replace(/-/g, '/'); // "2010/08/01"; ...
- jmter安装配置
一 JMeter 简介 JMeter 它是Apache组织的开放源代码项目,它是现在比较流行的功能和性能测试的工具.JMeter requires a fully compliant JVM 7 or ...
- IOS 制作版本新特性
创建版本新特性 页面(存放图片) HMNewfeatureViewController.m #define HMNewfeatureImageCount 4 #import "HMNewfe ...
- Robin-Karp algorithm 字符串的匹配
有关字符串的匹配问题,有很好的算法,即KMP算法,但是还有一种其实经常使用到的算法是Rabin-Karp算法,它是使用hash的原理来进行字符串匹配的.具体的做法如下. Rabin-Karp算法是由R ...