Solution -「HDU 1788」CRT again

\(\mathcal{Description}\)

  Link.

  解同余方程组:

\[x\equiv m_i-a\pmod{m_i} \]

  其中 \(i=1,2,\dots,n\)

  \(n\le10\)\(a,多测(假设常规 CRT 不可过)。

\(\mathcal{Solution}\)

  显:

\[x=\operatorname{lcm}(m_1,m_2,\cdots,m_n)-a \]

  复杂度 \(\mathcal O(n\log\max\{m_i\})\)(只是优化了常数 www)。

\(\mathcal{Code}\)

#include 

typedef unsigned long long ULL;

inline ULL gcd ( const ULL a, const ULL b ) { return b ? gcd ( b, a % b ) : a; }

int main () {
	int n, a;
	while ( ~ scanf ( "%d %d", &n, &a ) && n | a ) {
		ULL ans = 1;
		for ( int i = 1, m; i <= n; ++ i ) {
			scanf ( "%d", &m );
			ans *= m / gcd ( ans, m );
		}
		printf ( "%llu\n", ans - a );
	}
	return 0;
}

你可能感兴趣的:(Solution -「HDU 1788」CRT again)