Atcoder 做题记录
ABC297G
nim 板子题,比 F 简单,但是不会结论,爬了。
有
解为
#include <bits/stdc++.h>
using namespace std;
int f(int x, int l, int r) {
return (x % (l + r)) / l;
}
bool solve(vector<int>& v, int l, int r) {
int fold = 0;
for(const auto u : v) fold ^= f(u, l, r);
return fold != 0;
}
int main() {
int n, l, r;
cin >> n >> l >> r;
vector<int> a(n);
for(auto& v : a) cin >> v;
cout << (solve(a, l, r) ? "First\n" : "Second\n");
}
ABC297F
容斥。场上做出来了。上分。 用了有点像 01 背包的写法。
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int p[1001][1001], inv[1000010], fac[1000010];
int main()
{
inv[0] = fac[0] = inv[1] = fac[1] = 1;
for(int i = 2; i <= 1000000; ++i) fac[i] = 1ll*fac[i-1]*i%mod;
for(int i = 2; i <= 1000000; ++i) inv[i] = 1ll*(mod-mod/i)*inv[mod%i]%mod;
for(int i = 2; i <= 1000000; ++i) inv[i] = 1ll*inv[i-1]*inv[i]%mod;
int h, w, k; cin >> h >> w >> k;
for(int i = 1; i <= h; ++i)
for(int j = 1; j <= w; ++j)
{
if(k <= i*j) p[i][j] = 1ll*fac[i*j]*inv[i*j-k]%mod*inv[h*w]%mod*fac[h*w-k]%mod;
}
for(int i = h; i >= 1; --i)
for(int j = w; j >= 1; --j)
{
p[i][j] = (p[i][j]+4ll*p[i-1][j-1]%mod)%mod;
(p[i][j] -= 2*p[i-1][j]%mod) %= mod;
(p[i][j] -= 2*p[i][j-1]%mod) %= mod;
if(i >= 2) p[i][j] = (p[i][j]-2ll*p[i-2][j-1]%mod+p[i-2][j])%mod;
if(j >= 2) p[i][j] = (p[i][j]-2ll*p[i-1][j-2]%mod+p[i][j-2])%mod;
if(i >= 2 && j >= 2) p[i][j] = (p[i][j]+p[i-2][j-2])%mod;
}
int ans = 0;
for(int i = 1; i <= h; ++i)
for(int j = 1; j <= w; ++j)
ans = (ans+1ll*i*j%mod*p[i][j]%mod*(h-i+1)%mod*(w-j+1)%mod)%mod;
cout << (ans+mod)%mod << '\n';
return 0;
}
ABC248F
设
Code:
#include <iostream>
using namespace std;
int f[3001][3001], g[3001][3001];
int main()
{
ios::sync_with_stdio(0);
int n, p; cin >> n >> p;
f[1][0] = 1; g[1][0] = g[1][1] = 1;
for(int i = 2; i <= n; ++i)
{
f[i][0] = g[i][0] = 1;
f[i][1] = g[i][1] = 2*i+1;
for(int j = 1; j <= n; ++j)
{
f[i][j] = (g[i-1][j]+3ll*f[i-1][j-1])%p;
g[i][j] = (g[i-1][j]+g[i-1][j-1]+2ll*(f[i-1][j-1]+f[i-1][j-2]))%p;
}
}
for(int i = 1; i < n; ++i)
cout << f[n][i] << ' ';
return 0;
}
/*
f(n, i) = f(n-1, i-1)*3+g(n-1, i)
g(n, i) = g(n-1, i)+g(n-1, i-1)+f(n-1,i-1)*2+f(n-1,i-2)*2;
f(n, 0) = 1;
f(1, x>0) = 0;
*/
ABC249F
枚举最后一次操作一的位置,维护后面的答案。
细节写挂,WA 了好几发还看了题解(
Code:
#include <iostream>
#include <queue>
using namespace std;
priority_queue <int> q;
long long s, ans = -1e18;
int n, t[200010], y[200010], k;
int main()
{
ios::sync_with_stdio(0);
cin >> n >> k;
for(int i = 0; i < n; ++i) cin >> t[i] >> y[i];
for(int i = n-1; i >= 0; --i)
{
if(t[i] == 2)
{
if(y[i] >= 0) {s += y[i]; continue;}
q.push(y[i]);
if(q.size() > k) {s += q.top(); q.pop();}
continue;
}
ans = max(ans, y[i]+s); if(--k < 0) return cout << ans << '\n', 0;
if(q.size() > k) {s += q.top(); q.pop();}
}
ans = max(ans, s);
cout << ans << '\n';
return 0;
}
ABC250F
这道卡了好久,只想到 n 方
然后想到双指针,就切了。
记得破环为链空间要翻倍!
#include <iostream>
using namespace std;
struct Point {
long long x, y;
Point(int x=0,int y=0):x(x),y(y){}
} a[200010];
Point operator -(const Point &a, const Point &b) {
return Point(a.x-b.x,a.y-b.y);
}
long long Cross(Point a, Point b)
{
return a.x*b.y-a.y*b.x;
}
int main()
{
ios::sync_with_stdio(0);
int n; cin >> n;
for(int i = 0; i < n; ++i) cin >> a[i].x >> a[i].y, a[i+n] = a[i];
int r = 1;
long long s1 = 0, s2 = 0, ans = 0x7fffffffffffffff; // 阴间题,inf 要到 2.56e18
for(int i = 0; i < n; ++i)
s1 += Cross(a[i], a[i+1]);
for(int l = 0; l < n; ++l)
{
while(s2 < s1) s2 += 4*Cross(a[r]-a[l], a[r+1]-a[l]), ++r;
ans = min(ans, min(s2-s1, s1-(s2-4*Cross(a[r-1]-a[l], a[r]-a[l]))));
s2 -= 4*Cross(a[r]-a[l], a[r]-a[l+1]);
}
cout << ans << '\n';
return 0;
}
ABC251F
T1 需要满足的条件英文差点没读懂
结论:可构造 T1 为 dfs 树,T2 为 bfs 树
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
int n, m, dt[200000][2], cnt, bt[200000][2];
bool vis[200001];
vector <int> G[200001];
void dfs(int u)
{
vis[u] = true;
for(auto it : G[u])
if(!vis[it])
{
dt[cnt][0] = u;
dt[cnt++][1] = it;
dfs(it);
}
}
int main()
{
ios::sync_with_stdio(0);
int n, m; cin >> n >> m;
while(m--)
{
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1);
memset(vis, false, sizeof vis);
queue <int> q;
q.push(1);
cnt = 0; vis[1] = true;
while(!q.empty())
{
int u = q.front(); q.pop();
for(auto it : G[u])
if(!vis[it])
{
bt[cnt][0] = u;
bt[cnt++][1] = it;
vis[it] = true;
q.push(it);
}
}
for(int i = 0; i < n-1; ++i) cout << dt[i][0] << ' ' << dt[i][1] << '\n';
for(int i = 0; i < n-1; ++i) cout << bt[i][0] << ' ' << bt[i][1] << '\n';
return 0;
}
ABC256F
然后用树状数组维护即可。
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int a[200010], s1[200010], s2[200010], s3[200010], c1[200010], c2[200010], c3[200010], n, q;
void add(int *c, int k, int v)
{
for(; k <= n; k += k&-k)
c[k] = (c[k]+v)%mod;
}
int ask(int *c, int k)
{
int ans = 0;
for(; k; k &= k-1)
ans = (ans+c[k])%mod;
return ans;
}
int main()
{
ios::sync_with_stdio(0); cin >> n >> q;
for(int i = 1; i <= n; ++i) cin >> a[i], a[i] %= mod;
for(int i = 1; i <= n; ++i)
{
s1[i] = (s1[i-1]+a[i])%mod;
s2[i] = (s2[i-1]+1ll*a[i]*i%mod)%mod;
s3[i] = (s3[i-1]+1ll*a[i]*i%mod*i%mod)%mod;
}
for(int i = 1; i <= n; ++i)
{
c1[i] = ((s1[i]-s1[i&(i-1)])%mod+mod)%mod;
c2[i] = ((s2[i]-s2[i&(i-1)])%mod+mod)%mod;
c3[i] = ((s3[i]-s3[i&(i-1)])%mod+mod)%mod;
}
while(q--)
{
int op; cin >> op;
if(op == 1)
{
int x, v; cin >> x >> v;
add(c1, x, ((v-a[x])%mod+mod)%mod);
add(c2, x, 1ll*((v-a[x])%mod+mod)%mod*x%mod);
add(c3, x, 1ll*((v-a[x])%mod+mod)%mod*x%mod*x%mod);
a[x] = v; // 这行没加调了半天/ll
}
else
{
int x; cin >> x;
cout << ((1ll*(x+1)*(x+2)%mod*ask(c1,x)%mod+1ll*(mod-2*x-3)*ask(c2,x)%mod)%mod+1ll*ask(c3,x)%mod)%mod*499122177%mod << '\n';
}
}
return 0;
}
ABC258F
思路简单,但是大分讨
手写 floor ceil。
WA了一发
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int T;
long long a[5][2], b[5][2], B, k, i, j;
ll floor(ll a, ll b)
{
if(b < 0) a = -a, b = -b;
if(a > 0) return a/b;
return -((-a+b-1)/b);
}
ll ceil(ll a, ll b)
{
return -floor(a,-b);
}
ll dis1(ll *a, ll *b)
{
return abs(a[0]-b[0])*k+abs(a[1]-b[1])*k;
}
ll dis2(ll *a, ll *b)
{
if(a[0]%B==0&&b[1]%B==0||a[1]%B==0&&b[0]%B==0) return abs(a[0]-b[0])+abs(a[1]-b[1]);
if(a[0]/B==b[0]/B)
return min(abs(a[0]-b[0])+abs(a[1]-b[1])*k,abs(a[1]-b[1])+(ll)
min(a[0]+b[0]-floor(a[0],B)*B-floor(b[0],B)*B,ceil(a[0],B)*B+ceil(b[0],B)*B-a[0]-b[0]));
if(a[1]/B==b[1]/B)
return min(abs(a[1]-b[1])+abs(a[0]-b[0])*k,abs(a[0]-b[0])+(ll)
min(a[1]+b[1]-floor(a[1],B)*B-floor(b[1],B)*B,ceil(a[1],B)*B+ceil(b[1],B)*B-a[1]-b[1]));
return abs(a[0]-b[0])+abs(a[1]-b[1]);
}
int main()
{
ios::sync_with_stdio(0);
cin >> T;
while(T--)
{
cin >> B >> k >> a[0][0] >> a[0][1] >> b[0][0] >> b[0][1];
long long ans = dis1(a[0],b[0]);
a[1][0] = a[2][0] = a[0][0];
a[3][0] = floor(a[0][0],B)*B;
a[4][0] = ceil(a[0][0],B)*B;
a[3][1] = a[4][1] = a[0][1];
a[1][1] = floor(a[0][1],B)*B;
a[2][1] = ceil(a[0][1],B)*B;
b[1][0] = b[2][0] = b[0][0];
b[3][0] = floor(b[0][0],B)*B;
b[4][0] = ceil(b[0][0],B)*B;
b[3][1] = b[4][1] = b[0][1];
b[1][1] = floor(b[0][1],B)*B;
b[2][1] = ceil(b[0][1],B)*B;
for(i = 1; i <= 4; ++i)
for(j = 1; j <= 4; ++j)
ans = min(ans, dis1(a[0],a[i])+dis1(b[0],b[j])+dis2(a[i],b[j]));
cout << ans << '\n';
}
return 0;
}
ABC258G
小清新 bitset 题。比 F 简单!
#include <bits/stdc++.h>
using namespace std;
int n;
long long ans;
bitset <3000> a[3000];
int main()
{
ios::sync_with_stdio(0);
string s; cin >> n;
for(int i = n-1; i >= 0; --i)
cin >> s, a[i] = bitset<3000>(s);
for(int i = 0; i < n; ++i)
for(int j = i+1; j < n; ++j)
if(a[i][j])
ans += (a[i]&a[j]).count();
cout << ans/3 << '\n';
return 0;
}
ABC258H
若
可以递推,矩阵快速幂加速到
学了下返回指向数组的指针的函数写法。
#include <bits/stdc++.h>
using namespace std;
int n, a[2][2] = {{1,1},{1,0}}, b[2] = {1,0};
long long S;
const int mod = 998244353;
void mul(int b[2], int c[2][2])
{
int d[2];
d[0] = 1ll*b[0]*c[0][0]%mod+1ll*b[1]*c[1][0]%mod;
d[1] = 1ll*b[0]*c[0][1]%mod+1ll*b[1]*c[1][1]%mod;
b[0] = d[0] % mod; b[1] = d[1] % mod;
}
void mul2(int a[2][2], int b[2][2])
{
int c[2][2];
c[0][0] = 1ll*a[0][0]*b[0][0]%mod+1ll*a[0][1]*b[1][0]%mod;
c[0][1] = 1ll*a[0][0]*b[0][1]%mod+1ll*a[0][1]*b[1][1]%mod;
c[1][0] = 1ll*a[1][0]*b[0][0]%mod+1ll*a[1][1]*b[1][0]%mod;
c[1][1] = 1ll*a[1][0]*b[0][1]%mod+1ll*a[1][1]*b[1][1]%mod;
a[0][0] = c[0][0]%mod;
a[0][1] = c[0][1]%mod;
a[1][0] = c[1][0]%mod;
a[1][1] = c[1][1]%mod;
}
int (*qpow(long long p))[2]
{
int d[2][2] = {{1,1},{1,0}};
static int c[2][2];
c[0][0] = c[1][1] = 1;
c[1][0] = c[0][1] = 0;
for(; p; p >>= 1)
{
if(p & 1) mul2(c, d);
mul2(d, d);
}
return c;
}
int main()
{
ios::sync_with_stdio(0);
cin >> n >> S;
long long no = 0;
for(int i = 0; i < n; ++i)
{
long long x; cin >> x;
mul(b, qpow(x-no-1));
swap(b[0],b[1]); no = x;
}
mul(b, qpow(S-no-1));
cout << b[0] << '\n';
return 0;
}
ABC259C
简单贪心。
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s, t; cin >> s >> t;
for(;;)
{
char c = s.back();
int c1 = 0, c2 = 0;
while(!s.empty() && s.back() == c) s.pop_back(), ++c1;
while(!t.empty() && t.back() == c) t.pop_back(), ++c2;
if(c1 > c2 || (c1 == 1 && c2 > 1)) {cout << "No\n"; exit(0);}
if(s.empty() && !t.empty()) {cout << "No\n"; exit(0);}
if(s.empty()) {cout << "Yes\n"; exit(0);}
}
return 0;
}