Cao Chong Pig (Chinese Remainder Theorem template title + exgcd inversion yuan)

Cao Chong Pig (Chinese Remainder Theorem template title + exgcd inversion yuan)

source: Luo Gu P1495
time limit: 1.00s
memory limit: 125.00MB

Title Description

Since Cao Chong to get the elephant after Cao Cao began to fathom her son to do some business, then sent him to the Central Plains pig farms, but Cao Chong full of happy, so so-so at work, once Cao Cao wanted to know the number of sows , so Cao Cao Chong would like to play a fiercely. For example, if there are 16 sows, if built three pigsty, there is no place to rest one pig settled down. If built five pigsty, but there are still a pig no place to go, and then if the construction of seven pigsty, there are two no place to go. You give Cao, how can you do as a private secretary general Cao of course you want to accurately report the number of pigs?

Input Format

The first row contains an integer n (n <= 10) - to establish the number of the pigsty, the solution down n lines of two integers ai, bi (bi <= ai <= 1000), ai represents the establishment of a pigsty there is no place for bi pigs. You can assume that ai, aj relatively prime.

Output Format

Output includes a positive integer, i.e. the number of sows to raise at least Cao Chong.

Input 1

3
3 1
5 1
7 2

Output 1

16

answer

First look at the Chinese Remainder Theorem ( giant guys please skip )
Pictures from Baidu Encyclopedia
Well, then you already know the answer is obviously
Here Insert Picture Description
where
Here Insert Picture Description
and t i t_i Can use e x g c d exgcd determined,see the question here which suddenly becomes dull

This question is there are a few pit, first with rapid power inversion yuan would WA, suspected burst LL, the following should be placed on the ans modulo Secondly, if coupled with mod directly in the brackets (46 lines of code) will be WA, the suspect also burst LL ......

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#define m_p make_pair
#define p_i pair<int, int> 
#define maxn 12
#define maxm 505
#define _for(i, a) for(LL i = 0; i < (a); i++)
#define _rep(i, a, b) for(LL i = (a); i <= (b); i++)
#define outval(a) cout<<#a<<": "<<a<<"\n"
using namespace std;
typedef long long LL;
const LL MAXN = 110;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
typedef vector< double > vec;
typedef vector< vec > mat;

const double eps = 1e-8;

vector<LL> B, M;
LL mod;

void init() {
	mod = 1;
}

void exgcd(LL a, LL b, LL&x, LL&y) {//用快速幂求逆元会WA,怀疑数据溢出了
	if (!b) { x = 1; y = 0; }
	else {
		exgcd(b, a % b, y, x);
		y -= a / b * x;
	}
}

void Crt(vector<LL>&B, vector<LL>M) {
	LL ans = 0, x, y;
	_for(i, B.size()) {
		LL m = mod / M[i];
		exgcd(m, M[i], x, y);
		ans = (ans + m * x*B[i]) % mod;
		ans = (ans + mod) % mod;//放在下面以防止溢出(这道题真的会溢出)
	}
	cout << ans << "\n";
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//freopen("in.txt", "r", stdin);

	init();
	int n;
	cin >> n;
	B.resize(n);
	M.resize(n);
	_for(i, n) cin >> M[i] >> B[i], mod *= M[i];
	Crt(B, M);
	return 0;
}

/*

*/

Published 163 original articles · won praise 54 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_42856843/article/details/102636953