Enlarge GCD - codeforce
题解:这类题一般都可以考虑枚举约数(gcd),令G = gcd(a1,a2,a3,········,an),M = max(a1,a2,········,an)。有个很显然的想法,就是算出每个数的约数,然后枚举大于G的约数。然而算约数的复杂度如果是sqrt(M),就T飞了(亲测)。考虑模仿筛素数的过程,可以做到Mlog(M)的复杂度。(orz,找n个数的约数,只会O(nsqrt(n)))!
const int N = 300005; const int M = 15000005; int n; int a[N], b[M]; bool use[M]; int gcd(int a, int b) { return (!b ? a : gcd(b, a % b)); } int main() { sc(n); int g = 0, ma = 0; Rep(i, 1, n) sc(a[i]), g = gcd(g, a[i]), ma = max(ma, a[i]); Rep(i, 1, n) b[a[i]]++; int res = INF32; Rep(i, g + 1, ma) if (!use[i]) { int t = 0; for (int j = i; j <= ma; j += i) t += b[j], use[j] = 1; if (t) res = min(res, n - t); } cout << (res == INF32 ? -1 : res) << endl; return 0; }