解题报告 之 POJ2891 Strange Way to Express Integers

解题报告 之 POJ2891 Strange Way to Express Integers

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 remainderri. 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

题目大意:现在将数表示成一种新的形式,即用一个数去除多个数mk,分别得到余数rk,用这些(除数,余数)对来唯一确定本来的数字。有了数num和m1~mn很容易表示成这种形式,但是现在反过来,给你n个(mk,rk)对,让你确定这个数num是多少?不存在输出-1.

分析:裸的解线性同余方程组,说实话我看书没看懂这个代码的原理,特别是要用m2/gcd那里,所以先把代码当接口来用吧。有一个讲解的比较好的博文大家可以看看原理(http://blog.csdn.net/orpinex/article/details/6972654),但是我同样看不懂,等学了中国剩余再返回来看看吧。那么这道题等价于解方程:

x ≡ r1(mod m1)

x ≡ r2(mod
m2)

……

x ≡ rn(mod
mn)

是线性同余方程组,用模版过吧,虽然原理还不懂。。。Orz。。如果有好心的大神愿意给我讲讲麻烦加我QQ吧 452884244,叩谢。

上代码:

#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;

ll exgcd( ll a, ll b, ll& x, ll& y )  //扩展gcd
{
	if(b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	ll gcd = exgcd( b, a%b, x, y );
	ll tem = x;
	x = y;
	y = tem - a / b*y;
	return gcd;
}

int main()
{
	int n;
	ll r1, m1, r2, m2, x0, y0;
	while(cin >> n)
	{
		bool flag=1;
		cin >> m1 >> r1;  //以第一个同余方程作为基础
		for(int i = 1; i < n; i++)
		{
			cin >> m2 >> r2;    //处理剩下的同余方程
			ll a = m1, b = m2, c = r2 - r1;
			ll d=exgcd( a, b, x0, y0 );   //求基础解X0和d=GCD

			if(c%d != 0) flag = false;  //无解条件

			ll t = b / d;                                        //看不懂,似乎是在更新一些系数。
			x0 = (x0*(c / d) % t + t) % t;			//看不懂,似乎是在更新一些系数。
			r1 = m1*x0 + r1;								//看不懂,似乎是在更新一些系数。
			m1 = m1*(m2 / d);							//看不懂,似乎是在更新一些系数。
		}
		if(!flag)
		{
			cout << -1 << endl;
			continue;
		}
		cout << r1 << endl;
	}
	return 0;
}

数论难起来真是理解无力。。。。

时间: 2024-10-14 06:47:48

解题报告 之 POJ2891 Strange Way to Express Integers的相关文章

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

Strange Way to Express Integers DescriptionElina 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 n

POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x,那么输出-1.不满足所有的ai互质. 题解 互质就简单,但是不互质就有些麻烦,到现在我还是不大懂. 具体证明可以求教大佬,如果我懂了,会更新的. 代码 #include <cstring> #include <cstdio> #include <algorithm> #in

POJ2891 Strange Way to Express Integers【一元线性同余方程组】

题目链接: http://poj.org/problem?id=2891 题目大意: 选择k个不同的正整数a1.a2.-.ak,对于某个整数m分别对ai求余对应整数ri,如果 适当选择a1.a2.-.ak,那么整数m可由整数对组合(ai,ri)唯一确定. 若已知a1.a2.-.ak以及m,很容易确定所有的整数对(ai,ri),但是题目是已知a1. a2.-.ak以及所有的整数对(ai,ri),求出对应的非负整数m的值. 思路: 题目可以转换为给定一系列的一元线性方程 x ≡ r1( mod a1

poj2891 Strange Way to Express Integers

扩展欧几里得,注意防溢出. http://poj.org/problem?id=2891 1 #include <cstdio> 2 using namespace std; 3 typedef __int64 LL; 4 const int maxn = 1e5 + 10; 5 6 LL a[maxn], r[maxn]; 7 int n; 8 LL egcd(LL a, LL b, LL& x, LL& y){ 9 if(!b){ 10 x = 1, y = 0; 11 r

【POJ2891】Strange Way to Express Integers(拓展CRT)

[POJ2891]Strange Way to Express Integers(拓展CRT) 题面 Vjudge 板子题. 题解 拓展\(CRT\)模板题. #include<iostream> #include<cstdio> using namespace std; #define ll long long #define MAX 111111 ll exgcd(ll a,ll b,ll &x,ll &y) { if(!b){x=1,y=0;return a;

【EXCRT模板】POJ2891/LuoGu4777Strange Way to Express Integers拓展中国剩余定理

这道题需要exgcd的基础 POJ的题干描述十分恶心 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 21217 Accepted: 7120 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negati

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

F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers.

poj 2891 Strange Way to Express Integers

http://poj.org/problem?id=2891 这道题的题意是:给你多个模性方程组:m mod ai=ri 求最小的m: 中国剩余定理 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define ll long long 5 using namespace std; 6 7 ll gcd(ll a,ll b,ll &x,ll &y) 8 { 9 if(!

poj——2891 Strange Way to Express Integers

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839   Accepted: 5625 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is