ARC192
<p> <a class="difficulty-label" style="color: rgb(192, 192, 0);" href="https://atcoder.jp/contests/arc192/tasks" target="_blank">◉ ARC192</a> </p>
<p> <span class="difficulty-circle" style="border-color: rgb(0, 192, 192); background: linear-gradient(to top, rgb(0, 192, 192) 46.75%, rgba(0, 0, 0, 0) 16.75%);"></span> <a class="difficulty-label" style="color: rgb(0, 192, 192);" href="https://atcoder.jp/contests/arc192/tasks/arc192_a" target="_blank">ARC192A - ARC Arc</a> </p>
题意
给定一个长度为
若有一个长度为
- 选择
1 \le i \le N ,若S_{i}=A,S_{i+1}=R,S_{i+2}=C ,则将A_i 和A_{i+1} 设为1 。 - 选择
1 \le i \le N ,若S_{i+2}=A,S_{i+1}=R,S_{i}=C ,则将A_i 和A_{i+1} 设为1 。
求是否存在一个好串。
题解
不难发现当
当
还剩下
时间复杂度
<details> <summary>ケロシの代码</summary>
const int N = 4e5 + 5;
int n, a[N], b[5];
void solve() {
cin >> n;
FOR(i, 1, n) cin >> a[i];
if(n % 4 == 0) {
cout << "Yes" << endl;
return;
}
int c = 0;
FOR(i, 1, n) if(a[i]) c ++;
if(c == n) {
cout << "Yes" << endl;
return;
}
if(n % 2 == 1) {
cout << (c ? "Yes" : "No") << endl;
return;
}
FOR(i, 1, n) if(a[i]) b[i & 1] ++;
if(b[0] && b[1]) cout << "Yes" << endl;
else cout << "No" << endl;
}
</details>
<br>
<p> <span class="difficulty-circle" style="border-color: rgb(0, 0, 255); background: linear-gradient(to top, rgb(0, 0, 255) 45.75%, rgba(0, 0, 0, 0) 16.75%);"></span> <a class="difficulty-label" style="color: rgb(0, 0, 255);" href="https://atcoder.jp/contests/arc192/tasks/arc192_b" target="_blank">ARC192B - Fennec VS. Snuke 2</a> </p>
题意
给定一个长度为
两名玩家依次经行以下操作:
- 选择
i ,使得A_i \ge 1 ,并将A_i 减去1 。如果i \notin S ,则将i 加入到S 中。 - 如果操作后出现
S=\\{1,2,\cdots,N\\} ,则执行这步操作的玩家获胜。
判断先手是否能必胜。
题解
因为每次是将一个
观察
那么不难发现策略就是要让剩下
要维持这个,那么就要在后手选新的偶数
所以特判掉
时间复杂度
<details> <summary>ケロシの代码</summary>
const int N = 2e5 + 5;
int n, a[N], f[N];
void solve() {
cin >> n;
FOR(i, 1, n) cin >> a[i];
int c = 0;
FOR(i, 1, n) if(a[i] % 2) c ++;
if(n == 1) {
cout << "Fennec" << endl;
return;
}
if(n == 2) {
cout << "Snuke" << endl;
return;
}
if(n == 3) {
cout << (c ? "Fennec" : "Snuke") << endl;
return;
}
cout << (c % 2 ? "Fennec" : "Snuke") << endl;
}
</details>
<br>
<p> <span class="difficulty-circle" style="border-color: rgb(192, 192, 0); background: linear-gradient(to top, rgb(192, 192, 0) 39.5%, rgba(0, 0, 0, 0) 16.75%);"></span> <a class="difficulty-label" style="color: rgb(192, 192, 0);" href="https://atcoder.jp/contests/arc192/tasks/arc192_c" target="_blank">ARC192C - Range Sums 2</a> </p>
题意
交互题,给定
每次询问给出两个整数
最多询问
题解
考虑先询问出所有
接下来用这个
<details> <summary>ケロシの代码</summary>
const int N = 5e3 + 5;
int n, id[N], ans[N];
ll f[N], g[N];
void solve() {
cin >> n;
FOR(i, 2, n) {
cout << "? " << 1 << " " << i << endl << flush;
cin >> f[i];
}
int mx = 0;
FOR(i, 1, n) if(f[i] > f[mx]) mx = i;
FOR(i, 1, n) if(i != mx) {
cout << "? " << mx << " " << i << endl << flush;
cin >> g[i];
}
if(g[1] > g[2]) {
FOR(i, 1, n) id[i] = i;
sort(id + 1, id + n + 1, [&] (int x, int y) {
return g[x] > g[y];
});
ll x;
cout << "? " << id[1] << " " << id[n - 1] << endl << flush;
cin >> x;
FOR(i, 1, n) ans[id[i]] = i;
cout << "! ";
FOR(i, 1, n) cout << ans[i] << " ";
sort(g + 1, g + n + 1);
g[1] = g[n] - x;
ROF(i, n, 1) cout << g[i] - g[i - 1] << " ";
cout << endl << flush;
}
else {
FOR(i, 1, n) id[i] = i;
sort(id + 1, id + n + 1, [&] (int x, int y) {
return g[x] < g[y];
});
ll x;
cout << "? " << id[2] << " " << id[n] << endl << flush;
cin >> x;
FOR(i, 1, n) ans[id[i]] = i;
cout << "! ";
FOR(i, 1, n) cout << ans[i] << " ";
sort(g + 1, g + n + 1);
g[1] = g[n] - x;
FOR(i, 1, n) cout << g[i] - g[i - 1] << " ";
cout << endl << flush;
}
}
</details>
<br>
<p> <span class="difficulty-circle" style="border-color: rgb(255, 128, 0); background: linear-gradient(to top, rgb(255, 128, 0) 80.5%, rgba(0, 0, 0, 0) 16.75%);"></span> <a class="difficulty-label" style="color: rgb(255, 128, 0);" href="https://atcoder.jp/contests/arc192/tasks/arc192_d" target="_blank">ARC192D - Fraction Line</a> </p>
题意
给定一个长度为
若一个长度为
- 对于所有
1 \le i < N ,都有\frac{S_iS_{i+1}}{\gcd(S_i,S_{i+1})^2}=A_i -
\gcd(S_1,S_2,\cdots,S_n)=1
则
定义一个序列的分数为其所有元素的乘积,求所有好序列的分数之和,对
题解
观察限制,不难发现这个限制都只和
一个数
首先看第二个限制,这个就相当于
再来看第一个限制,那么相当于:
那么现在所有的限制都是对于
复杂度估计
<details> <summary>ケロシの代码</summary>
const int N = 1e5 + 5;
const int P = 998244353;
inline int add(int x, int y) { return (x + y < P ? x + y : x + y - P); }
inline void Add(int & x, int y) { x = (x + y < P ? x + y : x + y - P); }
inline int sub(int x, int y) { return (x < y ? x - y + P : x - y); }
inline void Sub(int & x, int y) { x = (x < y ? x - y + P : x - y); }
inline int mul(int x, int y) { return (1ll * x * y) % P; }
inline void Mul(int & x, int y) { x = (1ll * x * y) % P; }
int n, a[N], d[N], pw[N];
int p[N], ip[N], cnt;
int b[N], len;
int f[N][2], g[N][2];
void init(int n) {
ip[1] = 1;
FOR(i, 2, n) {
if(! ip[i]) {
p[++ cnt] = i;
}
for(int j = 1; j <= cnt && p[j] * i <= n; j ++) {
ip[p[j] * i] = 1;
if(i % p[j] == 0) break;
}
}
}
void solve() {
cin >> n;
FOR(i, 1, n - 1) cin >> a[i];
init(N - 1);
FOR(i, 1, n - 1)
FOR(j, 1, a[i]) if(! ip[j]) if(a[i] % j == 0)
b[++ len] = j;
sort(b + 1, b + len + 1);
len = unique(b + 1, b + len + 1) - b - 1;
int ans = 1;
FOR(i, 1, len) {
FOR(j, 1, n - 1) {
d[j] = 0;
for(int x = a[j]; x % b[i] == 0; x /= b[i]) d[j] ++;
}
int lim = 1;
FOR(j, 1, n - 1) lim += d[j];
pw[0] = 1;
FOR(j, 1, lim) pw[j] = mul(pw[j - 1], b[i]);
FOR(j, 0, lim) REP(o, 2) f[j][o] = 0;
FOR(j, 1, lim) f[j][0] = pw[j];
f[0][1] = 1;
FOR(j, 1, n - 1) {
FOR(k, 0, lim) REP(o, 2) g[k][o] = 0;
FOR(k, 0, lim) REP(o, 2) {
int u = k + d[j];
if(u <= lim) Add(g[u][o | ! u], mul(f[k][o], pw[u]));
u = k - d[j];
if(d[j] && u >= 0) Add(g[u][o | ! u], mul(f[k][o], pw[u]));
}
FOR(k, 0, lim) REP(o, 2) f[k][o] = g[k][o];
}
int res = 0;
FOR(j, 0, lim) Add(res, f[j][1]);
Mul(ans, res);
}
cout << ans << endl;
}
</details>
<br>
<p> <span class="difficulty-circle" style="border-color: rgb(255, 0, 0); background: linear-gradient(to top, rgb(255, 0, 0) 85.5%, rgba(0, 0, 0, 0) 16.75%);"></span> <a class="difficulty-label" style="color: rgb(255, 0, 0);" href="https://atcoder.jp/contests/arc192/tasks/arc192_e" target="_blank">ARC192E - Snuke's Kyoto Trip</a> </p>
题意
有一个横轴
现在矩形内有一个横轴占据
求从矩形内任意点开始,任意点结束,所走路径的方案数。
题解
先考虑没有障碍怎么做,就是在一个矩形内任意起点终点的路径方案数。
先考虑起点固定,终点不固定,那么可以枚举终点后组合数列加合并:
考虑组合意义的证明,考虑
有了组合意义,就能类似的推出任意起点到任意终点的方案就是
算完了这个,接下来考虑有障碍怎么做,不难想到可以容斥。
先用大矩形的所有方案减掉障碍矩形内部的方案,然后就只用减掉穿过连接障碍点和非障碍点的边的贡献即可,称这种边为边界。
一条路径可能两次穿过边界,但是我们可以先减掉钦定穿过了下面或左边的边界的方案,然后钦点穿过了上面或右边的边界的时候,还要钦定不能穿过下面或左边的边界,这样就能全部减掉全部不合法方案了。
时间复杂度
<details> <summary>ケロシの代码</summary>
const int N = 2e6 + 5;
const int P = 998244353;
inline int add(int x, int y) { return (x + y < P ? x + y : x + y - P); }
inline void Add(int & x, int y) { x = (x + y < P ? x + y : x + y - P); }
inline int sub(int x, int y) { return (x < y ? x - y + P : x - y); }
inline void Sub(int & x, int y) { x = (x < y ? x - y + P : x - y); }
inline int mul(int x, int y) { return (1ll * x * y) % P; }
inline void Mul(int & x, int y) { x = (1ll * x * y) % P; }
int fp(int x, int y) {
int res = 1;
for(; y; y >>= 1) {
if(y & 1) Mul(res, x);
Mul(x, x);
}
return res;
}
int n, m, L, R, D, U;
int fac[N], finv[N];
void init(int n) {
fac[0] = 1;
FOR(i, 1, n) fac[i] = mul(fac[i - 1], i);
finv[n] = fp(fac[n], P - 2);
ROF(i, n - 1, 0) finv[i] = mul(finv[i + 1], i + 1);
}
int C(int x, int y) {
if(x < y || y < 0) return 0;
return mul(mul(fac[x], finv[y]), finv[x - y]);
}
int F1(int n, int m) {
return sub(C(n + m, n), 1);
}
int F2(int n, int m) {
return sub(C(n + m + 2, n + 1), add(mul(n + 1, m + 1), 1));
}
void solve() {
cin >> n >> m >> L >> R >> D >> U;
init(N - 1);
int ans = F2(n + 1, m + 1);
Sub(ans, F2(R - L + 1, U - D + 1));
FOR(i, D, U) Sub(ans, mul(F1(L, i + 1), F1(n - L + 1, m - i + 1)));
FOR(i, L, R) Sub(ans, mul(F1(i + 1, D), F1(n - i + 1, m - D + 1)));
FOR(i, D, U) Sub(ans, mul(F1(R - L + 1, i - D + 1), F1(n - R, m - i + 1)));
FOR(i, L, R) Sub(ans, mul(F1(i - L + 1, U - D + 1), F1(n - i + 1, m - U)));
cout << ans << endl;
}
</details>
<br>