voidUnion(int x, int y){ x = GetFather(x); y = GetFather(y); fa[x] = y; }
intmain(){ int T; cin >> T; for (int test = 1; test <= T; test++) { int N, M; cin >> N >> M; for (int i = 1; i <= N; i++) fa[i] = i; for (int i = 1; i <= M; i++) { int x, y; cin >> x >> y; Union(x, y); } map<int, bool> go; for (int i = 1; i <= N; i++) go[GetFather(i)] = true; int num = go.size(); int ans = N - num + (num - 1) * 2; cout << "Case #" << test << ": " << ans << endl; } return0; }
voidPrepare(){ vector<bool> is_prime(40000); is_prime[2] = true; for (int i = 3; i < 40000; i++) is_prime[i] = true; for (int i = 2; i < 40000; i++) { if (is_prime[i]) { for (int j = i + i; j < 40000; j += i) { is_prime[j] = false; } } } for (int i = 3; i < 40000; i++) { if (is_prime[i]) primes.push_back(i); } }
voidFactorize(int x, int& a, int& M){ a = 0; while ((x & 1) == 0) { x >>= 1; a++; } int now = x; M = 1; for (int prime : primes) { if (prime * prime > x) break; if (now % prime == 0) { int cnt = 0; while (now % prime == 0) { now /= prime; cnt++; } M *= (cnt + 1); if (M > 2) break; } } if (now > 1) M *= 2; }
boolCheck(int x){ int a, M; Factorize(x, a, M); if (a == 0) { return M <= 2; } elseif (a == 1) { returntrue; } else { return (a - 1) * M <= 2; } }
intWork(int L, int R){ int ans = 0; for (int i = L; i <= R; i++) { if (Check(i)) ans++; } return ans; }
intmain(){ Prepare(); int T; cin >> T; for (int test = 1; test <= T; test++) { int L, R; cin >> L >> R; int ans = Work(L, R); cout << "Case #" << test << ": " << ans << endl; } return0; }