signedmain() { ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); #if 0 freopen("in.txt", "r", stdin); #endif int T = scanner.nextInt(); while (T --) { int n = scanner.nextInt(), m = scanner.nextInt(); for (int i = 1; i <= n; ++ i) a[i] = scanner.nextInt(); memset(cnt, 0, sizeof(int) * m); for (int i = 1; i <= n; ++ i) ++ cnt[a[i] % m]; int ans = bool(cnt[0]), lim = m / 2; for (int i = 1; i <= lim; ++ i) { auto aa = cnt[i], bb = cnt[m - i]; if (!aa && !bb) continue; ans += max(abs(bb - aa), 1); } println(ans); } return0; }
voidC1(int n) { if (n % 2) print("1 ", n / 2, ' ', n / 2); elseif (n % 4) print("2 ", n / 2 - 1, ' ', n / 2 - 1); else print(n / 2, ' ', n / 4, ' ', n / 4); }
signedmain() { ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); #if 0 freopen("in.txt", "r", stdin); #endif int T = scanner.nextInt(); while (T --) { int n = scanner.nextInt(), k = scanner.nextInt(); int m = k - 3; C1(n - m); while (m --) print(" 1"); println(); } return0; }
首先肯定要对数组里每个数字进行分解质因数;因为相乘就是分解质因数得到的多项式的指数相加,而完全平方数分解得到的多项式的指数均为 2;那么我们可以得到结论:如果 x 和 y 的乘积是完全平方数,那么 x 和 y 分解质因数得到的多项式中关于每一个质数的指数都是 \(\mathrm{mod} \ 2\) 相等的。
constint N = 2e5 + 5, A = 1e7 + 7; int a[N], pattern[N];
// PrimeFactorizationSieve() namespace PFS {...}
signedmain() { ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); #if 0 freopen("in.txt", "r", stdin); #endif int T = scanner.nextInt(); using PFS::prime, PFS::fact; PFS::start(); while (T --) { int n = scanner.nextInt(), k = scanner.nextInt(); for (int i = 1; i <= n; ++ i) a[i] = scanner.nextInt(); fill(pattern, pattern + n + 1, 1); for (int i = 1; i <= n; ++ i) { intexp = 0, base = 0, tmp = a[i]; while (tmp > 1) { int p = fact[tmp]; if (base == p) ++ exp; else { if (exp % 2) pattern[i] *= base; base = p, exp = 1; } tmp /= p; } if (exp % 2) pattern[i] *= base; } int ans = 1; set<int> unique; for (int i = 1; i <= n; unique.insert(pattern[i ++])) if (unique.count(pattern[i])) ++ ans, unique.clear(); println(ans); } return0; }