[LOJ10213] 「一本通 6.4 例 5」Strange Way to Express Integers [ExCRT]

Link
Luogu - https://www.luogu.org/problemnew/show/P4777
POJ - http://poj.org/problem?id=2891
LOJ - https://loj.ac/problem/10213


板子


#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 114514;
long long n, m[MAXN], a[MAXN];
void exgcd(long long a, long long b, long long& d, long long& x, long long& y)
{
	!b?(d=a,x=1,y=0):(exgcd(b,a%b,d,y,x),y-=x*(a/b));
}
void Mul(long long& a, const long long& b, const long long& p)
{
	static long long t;
	
	t = (long double) a * b / p;
	a = (a * b - t * p) % p + p;
	if (a >= p) a -= p;
}
long long Adjust(long long x, const long long& p)
{
	x = x % p + p;
	return (x >= p) ? (x - p) : x;
}
int main()
{
	register long long d, x, y, A, M, t;
	while(~scanf("%d", &n))
	{
		for (register int i = 1; i <= n; ++i) scanf("%lld%lld", &m[i], &a[i]);
		A = a[1], M = m[1];
		for (register int i = 2; i <= n; ++i)
		{
			exgcd(m[i], M, d, x, y);
			t = A - a[i];
			if (t%d) goto FAIL;
			Mul(y, (t / d), m[i]);
			t = M;
			M *= m[i] / d;
			Mul(y, t, M);
			A = Adjust(A - y, M);
		}
		printf("%lld\n", Adjust(A, M));
		continue;
FAIL:	printf("-1\n");
	}
	return 0;
}

你可能感兴趣的:([LOJ10213] 「一本通 6.4 例 5」Strange Way to Express Integers [ExCRT])