P3868 [TJOI2009]猜数字

(color{#0066ff}{ 题目描述 })

现有两组数字,每组k个,第一组中的数字分别为:a1,a2,...,ak表示,第二组中的数字分别用b1,b2,...,bk表示。其中第二组中的数字是两两互素的。求最小的非负整数n,满足对于任意的i,n - ai能被bi整除。

(color{#0066ff}{输入格式})

输入数据的第一行是一个整数k,(1 ≤ k ≤ 10)。接下来有两行,第一行是:a1,a2,...,ak,第二行是b1,b2,...,bk

(color{#0066ff}{输出格式})

输出所求的整数n。

(color{#0066ff}{输入样例})

3
1 2 3
2 3 5

(color{#0066ff}{输出样例})

23

(color{#0066ff}{数据范围与提示})

所有数据中,第一组数字的绝对值不超过(10^9)(可能为负数),第二组数字均为不超过6000的正整数,且第二组里所有数的乘积不超过(10^{18})

每个测试点时限1秒

(color{#0066ff}{ 题解 })

根据题目,你得出了这个式子。。

(n-a_i equiv 0 mod b_i)

然后你移个项,发现就TM是个裸的CRT

注意数据范围,要写龟乘

#include<bits/stdc++.h>
#define LL long long
LL in() {
	char ch; LL x = 0, f = 1;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
	return x * f;
}
LL A[15], B[15];
int k;
LL msc(LL x, LL y, LL N) {
	LL re = 0;
	while(y) {
		if(y & 1) re = (re + x) % N;
		x = (x + x) % N;
		y >>= 1;
	}
	return re;
}
void exgcd(LL a, LL b, LL &x, LL &y) {
	if(!b) return(void)(x = 1, y = 0);
	exgcd(b, a % b, x, y);
	LL t = x - a / b * y;
	x = y, y = t;
}
LL work(int p) {
	LL N = 1;
	for(int i = 1; i <= k; i++) N *= B[i];
	LL x, y;
	exgcd(N / B[p], B[p], y, x);
	return ((y * (N / B[p])) % N + N) % N;
}


int main() {
	k = in();
	for(int i = 1; i <= k; i++) A[i] = in(); 
	for(int i = 1; i <= k; i++) B[i] = in();
	LL N = 1;
	for(int i = 1; i <= k; i++) N *= B[i];
	LL ans = 0;
	for(int i = 1; i <= k; i++) ans = (ans + msc(A[i], work(i), N)) % N;
	printf("%lld
", (ans % N) + N % N);
	return 0;
}
原文地址:https://www.cnblogs.com/olinr/p/10301939.html