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 ...
随机推荐
- spring boot Configuration Annotation Proessor not found in classpath
出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationPropertie ...
- css的三个特性 背景透明设置
关于行内元素(补充一点) 行内元素只能容纳文本或其他行内元素.(a特殊a里面可以放块级元素) 例子: 关于行高tip: 选择器的嵌套层级不应大于3级,位置靠后的限定条件应尽可能的精确. 属性定义必须另 ...
- Spring Cloud入门程序
本文手把手教你,做出第一个Spring Cloud程序,Eureka的简单入门使用 1.创建Spring Starter Project工程 点击next,添加项目名 2.引入Spring Cloud ...
- 使用 Satis 搭建私有的 Composer 包仓库
简述 iBrand 产品立项时是商业性质的项目,但是在搭建架构时考虑后续的通用性,因此每个模块都设计成一个 Package,作为公司内部用,因此这些包并不能提交到 packagist.org 上去. ...
- Element(Vue)+Express(Node)模拟服务器获取本地json数据
网上很多教程说需要在build目录下的dev-server.js文件中配置,但目前最新的vue-cli是没有dev-server.js这个文件的,因为已经被合并到webpack.dev.conf.js ...
- nginx配置vhost配置文件详解
//千锋PHP-PHP培训的实力派server { listen 80; server_name www.sina.com; root /data/www/sina; index index.php; ...
- COGS 182. [USACO Jan07] 均衡队形
★★ 输入文件:lineup.in 输出文件:lineup.out 简单对比时间限制:4 s 内存限制:128 MB 题目描述 农夫约翰的 N (1 ≤ N ≤ 50,000) 头奶牛 ...
- 二维码生成的WEB api方法
/// <summary> /// 获取二维码 /// </summary> /// <param name="size">编码测量度,值越大生 ...
- 深搜,四色定理,(POJ1129)
题目链接:http://poj.org/problem?id=1129 解题报告: #include<iostream> #include<cstdio> #include&l ...
- python web应用--web框架(三)
了解了WSGI框架,我们发现:其实一个Web App,就是写一个WSGI的处理函数,针对每个HTTP请求进行响应. 但是如何处理HTTP请求不是问题,问题是如何处理100个不同的URL. 每一个URL ...