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
设 为答案, 表示最右边两个点用另画的一条边连通时满足题意的方案数,就可以 dp(递推?)了。
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;
}ABC259D
做法,用并查集维护。
有老哥被卡精度了。
#include <bits/stdc++.h>
using namespace std;
int n, sx, sy, tx, ty, x[3000], y[3000], r[3000], fa[3000];
bool xj(int i, int j)
{
long long dis = 1ll*(x[i]-x[j])*(x[i]-x[j])+1ll*(y[i]-y[j])*(y[i]-y[j]);
return 1ll*(r[i]-r[j])*(r[i]-r[j])<=dis&&dis<=1ll*(r[i]+r[j])*(r[i]+r[j]);
}
int find(int k){return fa[k]==k?k:fa[k]=find(fa[k]);}
int main()
{
ios::sync_with_stdio(0);
cin >> n >> sx >> sy >> tx >> ty;
int s = -1, t = -1;
for(int i = 0; i < n; ++i)
{
fa[i] = i;
cin >> x[i] >> y[i] >> r[i];
if(s==-1&&1ll*(sx-x[i])*(sx-x[i])+1ll*(sy-y[i])*(sy-y[i])==1ll*r[i]*r[i])
s = i;
if(t==-1&&1ll*(tx-x[i])*(tx-x[i])+1ll*(ty-y[i])*(ty-y[i])==1ll*r[i]*r[i])
t = i;
}
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(xj(i, j))
fa[find(i)] = find(j);
cout << (find(s)==find(t)?"Yes":"No") << '\n';
return 0;
}ABC259E
如果一个数不被其他数的 整除,则这个数对答案有 的贡献。
#include <bits/stdc++.h>
using namespace std;
unordered_map <int, pair <int, int> > Map;
bool vis[200001];
int main()
{
ios::sync_with_stdio(0);
int n; cin >> n;
for(int i = 1; i <= n; ++i)
{
int m; cin >> m;
for(int j = 1; j <= m; ++j)
{
int p, e; cin >> p >> e;
if(!Map.count(p))
Map[p] = make_pair(e, i);
else
if(e > Map[p].first)
Map[p] = make_pair(e, i);
else if(e == Map[p].first)
Map[p].second = -1;
}
}
for(auto it : Map)
{
//cout << it.first << ' ' << it.second.first << ' ' << it.second.second<<endl;
if(it.second.second != -1)
vis[it.second.second] = true;
}
int ans = 0;
for(int i = 1; i <= n; ++i)
if(vis[i]) ++ans;
cout << min(n, ans+1) << '\n';
return 0;
}ABC293F
指数小时一种做法,指数大时一种做法。和春测 T2 有异曲同工之妙。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
int T; cin >> T;
while(T--)
{
ll n; cin >> n;
ll ans = 1;
if(n > 2) ++ans;
for(int i = 4; i < 16; ++i)
{
int L = 1, R = 1000000000;
while(L < R)
{
int mid = (L+R+1)>>1;
__int128 sm = 0, pw = 1;
for(int j = 0; j < 4; ++j)
{
if((i>>j)&1) sm += pw;
pw = pw*mid;
}
if(sm <= n) L = mid;
else R = mid-1;
}
__int128 sm = 0, pw = 1;
for(int j = 0; j < 4; ++j)
{
if((i>>j)&1) sm += pw;
pw = pw*L;
}
if(L > 1 && sm == n) ++ans;
}
for(int i = 2; 1ll*i*i <= n/i/i; ++i)
{
bool ok = true;
for(ll x = n; x; x /= i)
if(x % i > 1) {ok = false; break;}
if(ok) ++ans;
}
cout << ans << '\n';
}
return 0;
}ABC293G
莫队板题。收藏。
#include <bits/stdc++.h>
using namespace std;
struct Query {
int l, r, id;
long long ans;
} q[200010];
bool cmpl(const Query &a, const Query &b) {
return a.l < b.l;
}
bool cmpr(const Query &a, const Query &b) {
return a.r < b.r;
}
bool cmpid(const Query &a, const Query &b) {
return a.id < b.id;
}
int n, Q, a[200010], cnt[200010];
int main()
{
ios::sync_with_stdio(0);
cin >> n >> Q;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= Q; ++i)
{
cin >> q[i].l >> q[i].r;
q[i].id = i;
}
sort(q+1, q+Q+1, cmpl);
int len = sqrt(n);
int T = 0, lstr = 0;
while(lstr < Q)
{
++T; int tmp = ++lstr;
while(lstr < Q && q[lstr+1].l <= len*T) ++lstr;
sort(q+tmp, q+lstr+1, cmpr);
}
int l = 1, r = 0;
long long mt = 0;
for(int i = 1; i <= Q; ++i)
{
while(l < q[i].l) mt -= 1ll*(cnt[a[l]]-1)*(cnt[a[l]]-2)/2, --cnt[a[l++]];
while(l > q[i].l) ++cnt[a[--l]], mt += 1ll*(cnt[a[l]]-2)*(cnt[a[l]]-1)/2;
while(r < q[i].r) ++cnt[a[++r]], mt += 1ll*(cnt[a[r]]-2)*(cnt[a[r]]-1)/2;
while(r > q[i].r) mt -= 1ll*(cnt[a[r]]-1)*(cnt[a[r]]-2)/2, --cnt[a[r--]];
q[i].ans = mt;
}
sort(q+1, q+Q+1, cmpid);
for(int i = 1; i <= Q; ++i)
cout << q[i].ans << '\n';
return 0;
}ARC158A
有点猜结论的感觉,不过题解思路其实挺奇妙。https://atcoder.jp/contests/arc158/editorial/5952
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
int T; cin >> T;
while(T--)
{
int a, b, c; cin >> a >> b >> c;
if(a>c)swap(a,c);
if(a>b)swap(a,b);
if(b>c)swap(b,c);
int u = b-a, v = c-b;
if((u&1)||(v&1)) cout << -1 << '\n';
else
{
int ans = min(u,v)/2;
u-=2*ans;v-=2*ans;
if(u%6||v%6) cout << -1 << '\n';
else cout << ans+(u+v)/3 << '\n';
}
}
return 0;
}