Educational Codeforces Round 106 (Rated for Div. 2)
Educational Codeforces Round 106 (Rated for Div. 2)
A - Domino on Windowsill
int main() {
IOS;
for (cin >> _; _; --_) {
int a, b; cin >> n >> m >> k >> a >> b;
if ((m + k >> 1) >= a && (n * 2 - m - k >> 1) >= b) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
B - Binary Removals
出现了连续的 1, 在两个连续 0 的前面, 必定无解
int main() {
IOS; s[0] = 'a';
for (cin >> _; _; --_) {
cin >> s + 1; n = 1, m = strlen(s + 1);
for (; n <= m; ++n) if (s[n - 1] == s[n] && s[n] == '1') break;
for (; m; --m) if (s[m] == s[m + 1] && s[m] == '0') break;
cout << (n < m ? "NO\n" : "YES\n");
}
return 0;
}
C - Minimum Grid Path
记录当前水平和垂直最小花费, 贪心取min
ll s, mi[3];
int main() {
IOS;
for (cin >> _; _; --_) {
cin >> n; memset(mi, 0x3f, sizeof mi); s = 0;
rep (i, 1, n) {
cin >> m; umin(mi[i & 1], m); s += m;
if (i > 1) umin(mi[2], s + mi[1] * (n - (i + 1 >> 1)) + mi[0] * (n - (i >> 1)));
}
cout << mi[2] << '\n';
}
return 0;
}
D - The Number of Pairs
设 \(a=k \times x+n, b=k \times y + m, gcd(n, m) = 1\) 则
\(c \times lcm(a,b) - d \times gcd(a, b) = c \times k \times n \ times m - d \times k = x\)
则, \(n \times m = \frac {\frac{x}{k}+d}{c}\) 其中 \(k|x, (\frac{x}{k}+d) | c\)
则枚举 x 的因子k, 在保证\((\frac{x}{k}+d) | c\) 的前提下, 求互质的 \(n, m\),
设 \(y = \frac {\frac{x}{k}+d}{c}\) 则直接分解质因数 \(y\),
对于每一个不同的质因子, 要么属于n, 要么属于m, 也就是\(2^{cntprime}, cntpriem <= 9\),
可以预处理\(2^{cntprime}, cntpriem <= 9\), 复杂度为\(O(\sqrt{n}^2)\), 远远达不到
const int N = 2e7 + 5;
int n, m, _, k, cas;
int pri[N], tot, v[N], c, d, x, qp[10];
void init(int n) {
qp[0] = 1;
rep (i, 1, 9) qp[i] = qp[i - 1] << 1;
rep (i, 2, n) {
if (!v[i]) pri[++tot] = i, v[i] = 1;
for (int j = 1; j <= tot && pri[j] <= n / i; ++j) {
if (i % pri[j] == 0) { v[i * pri[j]] = v[i]; break; }
v[i * pri[j]] = v[i] + 1;
}
}
}
void work(int m) {
if ((x / m + d) % c) return;
n += qp[v[(x / m + d) / c]];
}
int main() {
IOS; init(2e7);
for (cin >> _; _; --_) {
cin >> c >> d >> x; n = 0;
rep (i, 1, x / i) if (x % i == 0) {
work(i);
if (x / i != i) work(x / i);
}
cout << n << '\n';
}
return 0;
}
E - Chaotic Merge
线性dp, 设\(a(i, j)\) 表示以i结尾 \(\sum_{\begin{matrix} 1 \leqslant l_1 \leqslant i\\ 1 \leqslant l_2 \leqslant j\end{matrix}} f(l_1, i, l_2, j)\)
设\(b(i, j)\) 表示表示以j结尾 \(\sum_{\begin{matrix} 1 \leqslant l_1 \leqslant i\\ 1 \leqslant l_2 \leqslant j\end{matrix}} f(l_1, i, l_2, j)\)
初始化
for (int i = 1; s[i]; ++i) a[i][0] = (s[i] == s[i - 1] ? 0 : a[i - 1][0]) + 1;
for (int i = 1; t[i]; ++i) b[0][i] = (t[i] == t[i - 1] ? 0 : b[0][i - 1]) + 1;
转移方程
if (s[i] ^ s[i - 1]) a[i][j] = a[i - 1][j];
if (t[j] ^ t[j - 1]) b[i][j] = b[i][j - 1];
if (s[i] ^ t[j]) {
a[i][j] = (a[i][j] + b[i - 1][j]) % mod;
b[i][j] = (b[i][j] + a[i][j - 1]) % mod;
if (i - 1) a[i][j] = (a[i][j] + b[0][j]) % mod;
if (j - 1) b[i][j] = (b[i][j] + a[i][0]) % mod;
}
ll a[N][N], b[N][N], ans;
char s[N], t[N];
int main() {
IOS; cin >> s + 1 >> t + 1;
for (int i = 1; s[i]; ++i) a[i][0] = (s[i] == s[i - 1] ? 0 : a[i - 1][0]) + 1;
for (int i = 1; t[i]; ++i) b[0][i] = (t[i] == t[i - 1] ? 0 : b[0][i - 1]) + 1;
for (int i = 1; s[i]; ++i) for(int j = 1; t[j]; ++j) {
if (s[i] ^ s[i - 1]) a[i][j] = a[i - 1][j];
if (t[j] ^ t[j - 1]) b[i][j] = b[i][j - 1];
if (s[i] ^ t[j]) {
a[i][j] = (a[i][j] + b[i - 1][j]) % mod;
if (i - 1) a[i][j] = (a[i][j] + b[0][j]) % mod;
b[i][j] = (b[i][j] + a[i][j - 1]) % mod;
if (j - 1) b[i][j] = (b[i][j] + a[i][0]) % mod;
}
ans = (ans + a[i][j] + b[i][j]) % mod;
}
cout << ans;
return 0;
}