2023杭电多校05


题目来自: 2023“钉耙编程”中国大学生算法设计超级联赛(5)

Solved Rank A B C D E F G H I J K L
8 / 12 98 / 1200 O - O O O O O - O - - O
  • Ø 赛后通过
  • O 在比赛中通过
  • ! 尝试了但是失败了
  • - 没有尝试

A - Typhoon

SOLUTION

判断点到线段距离,然后卡卡常

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e6 + 50;
int n, m;

struct Point {
int x, y;

Point(int _x = 0, int _y = 0) : x(_x), y(_y) {}

Point operator-(const Point &b) const {
return Point(x - b.x, y - b.y);
}

ll operator^(const Point &b) const {
return 1ll * x * b.y - 1ll * y * b.x;
}

ll operator*(const Point &b) const {
return 1ll * x * b.x + 1ll * y * b.y;
}

ll dis(Point &b) {
return 1ll * (x - b.x) * (x - b.x) + 1ll * (y - b.y) * (y - b.y);
}
} A[10004], B[10004];

struct Line {
Point s, e;

Line() {

}

Line(Point _s, Point _e) : s(_s), e(_e) {

}

ll length() {
return s.dis(e);
}

double dis(Point &p) {
if ((p - s) * (e - s) < 0 || (p - e) * (s - e) < 0) return sqrt(min(p.dis(s), p.dis(e)));
return abs((p - s) ^ (e - s)) / sqrt(length());
}
} C[10004];

signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> m >> n;
for (int i = 1; i <= m; ++i) {
int xx, yy;
cin >> xx >> yy;
B[i] = Point(xx, yy);
C[i - 1] = Line(B[i], B[i - 1]);
}
m--;
for (int i = 1; i <= n; ++i) {
int xx, yy;
cin >> xx >> yy;
A[i] = Point(xx, yy);
double ans = 2000000000;
for (int j = 1; j <= m; ++j) {
ans = min(ans, C[j].dis(A[i]));
}
printf("%.4lf\n", ans);
}
}

C - String Magic (Easy Version)

SOLUTION

首先需要知道一个长度为 $n$ 的字符串中本质不同的字符串个数是 $O(n)$ 级别的。

那么对于这个题就可以先用马拉车处理出每个点作为回文中心所能得到的最长回文串,接下来枚举回文中心,再从长到短枚举回文串依次用hash判断合法性,但是每次暴力判断的总体复杂度是 $O(n^2)$ 的,不过因为不同的回文串很少,所以记忆化一下即可。

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const pii mod = {1e9 + 7, 1e9 + 9}, base = {131, 251};
const int N = 2e5 + 10;

pll operator + (const pll &a, const pll &b){
return {(a.first + b.first) % mod.first, (a.second + b.second) % mod.second};
}

pll operator - (const pll &a, const pll &b){
return {(a.first - b.first + mod.first) % mod.first, (a.second - b.second + mod.second) % mod.second};
}

pll operator * (const pll &a, const pll &b){
return {a.first * b.first % mod.first, a.second * b.second % mod.second};
}

pll hs[N], pw[N];
int n, f[N], m;
string a;
char s[N];

void init(){
hs[0] = {0, 0};
for(int i = 1; i <= n; i++){
pll ch = pll{a[i] - 'a' + 1, a[i] - 'a' + 1};
hs[i] = hs[i - 1] * base + ch;
}
for(int i = 1; i <= n; i++){
s[i << 1] = a[i];
s[i << 1 | 1] = '#';
f[i << 1] = f[i << 1 | 1] = 0;
}
f[0] = f[1] = 0;
int r, p, i;
s[0] = '$', s[1] = '#', s[m = (n + 1) << 1] = '@';
for(r = p = 0, f[1] = 1, i = 2; i < m; i += 1){
f[i] = r > i ? min(r - i, f[p * 2 - i]) : 1;
for(; s[i - f[i]] == s[i + f[i]]; f[i]++);
if(i + f[i] > r) r = i + f[i], p = i;
}

}

pll get(int l, int r){
return hs[r] - hs[l - 1] * pw[r - l + 1];
}

map<pll, int> mp;

int dfs(int l, int r){
if(l > r) return 0;
pll hash_ = get(l, r);
if(mp.count(hash_)){
return mp[hash_];
}
int ans = 0, mid = (l + r) >> 1;
if(get(l, mid) == get(mid + 1, r)) ans = 1;
ans += dfs(l + 1, r - 1);
mp[hash_] = ans;
return ans;
}

void solve(){
mp.clear();
cin >> a;
n = (int)a.size();
a = " " + a;
init();
ll ans = 0;
for(int i = 3; i + 2 < m; i += 2){
int l = (i - f[i] + 2) / 2, r = (i + f[i] - 2) / 2;
ans += dfs(l, r);
}
cout << ans << '\n';
}

signed main(){
int T = 1;
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
pw[0] = {1, 1};
for(int i = 1; i <= N - 1; i++){
pw[i] = pw[i - 1] * base;
}
cin >> T;
while(T--) solve();
return 0;
}

D - String Magic (Hard Version)

SOLUTION

延续C题的思路,在这题中需要求出每个前缀字符串的满足条件的回文串个数。

不妨考虑枚举右端点 $r$,由于本质不同回文串个数是 $O(n)$ 的,那么只要从长到短找出所有以 $r$ 结尾的回文串然后按C题的做法记忆化记录就能做完,接下来问题就在于如何快速找到以 $r$ 结尾的回文串。

考虑储存下所有回文中心,以及以该回文中心的最长回文串长度,那么在枚举右端点时,从左到右依次枚举当前的所有回文中心,若枚举到的回文中心的最长回文串不包含当前右端点,就可以把这个回文中心删除(因为后续的所有右端点也用不到这个回文中心),否则就可以用这个回文中心以及当前的右端点找出一个回文串,并且由于是从左往右枚举的回文中心,找到的回文串长度一定是从长到短的,接下来再记忆化一下就可以保证时间复杂度是正确的。

找回文串的复杂度是 $O(n)$,瓶颈在于如何记忆化以及存储回文中心,以下代码时间复杂度为 $O(nlogn)$。

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const pii mod = {1e9 + 7, 1e9 + 9}, base = {131, 251};
const int N = 2e5 + 10;

pll operator + (const pll &a, const pll &b){
return {(a.first + b.first) % mod.first, (a.second + b.second) % mod.second};
}

pll operator - (const pll &a, const pll &b){
return {(a.first - b.first + mod.first) % mod.first, (a.second - b.second + mod.second) % mod.second};
}

pll operator * (const pll &a, const pll &b){
return {a.first * b.first % mod.first, a.second * b.second % mod.second};
}

pll hs[N], pw[N];
int n, f[N], m;
string a;
char s[N];

void init(){
hs[0] = {0, 0};
for(int i = 1; i <= n; i++){
pll ch = pll{a[i] - 'a' + 1, a[i] - 'a' + 1};
hs[i] = hs[i - 1] * base + ch;
}
for(int i = 1; i <= n; i++){
s[i << 1] = a[i];
s[i << 1 | 1] = '#';
f[i << 1] = f[i << 1 | 1] = 0;
}
f[0] = f[1] = 0;
int r, p, i;
s[0] = '$', s[1] = '#', s[m = (n + 1) << 1] = '@';
for(r = p = 0, f[1] = 1, i = 2; i < m; i += 1){
f[i] = r > i ? min(r - i, f[p * 2 - i]) : 1;
for(; s[i - f[i]] == s[i + f[i]]; f[i]++);
if(i + f[i] > r) r = i + f[i], p = i;
}
}

pll get(int l, int r){
return hs[r] - hs[l - 1] * pw[r - l + 1];
}

map<pll, int> mp;

struct node{
pll hash;
int cnt;
};

ll ans[N];

void solve(){
mp.clear();
cin >> a;
n = (int)a.size();
a = " " + a;
init();
for(int i = 1; i <= n; i++) ans[i] = 0;
set<int> st;
for(int i = 3; i + 2 < m; i += 2){
st.insert(i);
}
for(int i = 2; i < m; i += 2){
vector<int> del;
vector<node> res;
for(auto j : st){
int rt = j + f[j] - 1;
if(j > i) break;
if(i > rt){
del.push_back(j);
}else{
int l = (2 * j - i) / 2, r = i / 2;
pll hash_ = get(l, r);
if(mp.count(hash_)){
res.push_back({hash_, mp[hash_]});
ans[r] += mp[hash_];
break;
}
int ok = 0, mid = (l + r) >> 1;
if(get(l, mid) == get(mid + 1, r)) ok = 1;
ans[r] += ok;
res.push_back({hash_, ok});
}
}
for(int j : del){
st.erase(j);
}
reverse(res.begin(), res.end());
int pre = 0;
for(auto j : res){
pll ha = j.hash;
int cnt = j.cnt;
pre += cnt;
if(mp.count(ha)) continue;
mp[ha] = pre;
}
}
for(int i = 1; i <= n; i++){
ans[i] += ans[i - 1];
cout << ans[i] << " \n"[i == n];
}
}

int main(){
int T = 1;
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
pw[0] = {1, 1};
for(int i = 1; i <= N - 1; i++){
pw[i] = pw[i - 1] * base;
}
cin >> T;
while(T--) solve();
return 0;
}

E - Snake

SOLUTION

容斥,333lfy秒了。

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=1e6+10;
const int mod=998244353;
ll qpow(ll a, ll b){
ll res = 1;
while(b){
if(b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}

ll f[N], inv[N];

void init(int M){
f[0] = 1;
for(int i = 1; i <= M; i++) f[i] = f[i - 1] * i % mod;
inv[M] = qpow(f[M], mod - 2);
for(int i = M - 1; i >= 0; i--){
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}

ll C(ll n, ll m){
if(n<0||m<0||n<m) return 0;
return f[n] * inv[m] % mod * inv[n - m] % mod;
}

void solve(){
ll n, m,k,i,ans=0;
cin >>n>>m>>k;
ans=C(n-1,m-1);
int fl=1;
for(i=1;i<=m;i++){
fl=-fl;
ans=(fl*C(m,i)*C(n-1-i*k,m-1)%mod+mod+ans)%mod;
}
printf("%lld\n",ans*f[n]%mod*inv[m]%mod);
}

int main(){
int T = 1;
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
init(1000000);
cin >> T;
while(T--) solve();
return 0;
}

F - Touhou Red Red Blue

SOLUTION

$dp_{ijk}$ 表示到第 $i$ 个物品时,背包状况为 $j, k$ 时的最大答案,然后转移即可。

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 1e5 + 50, inf = 0x3f3f3f3f;

int dp[N][4][4];// 0 1R 2G 3B

void solve(){
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
dp[0][i][j] = -inf;
}
}
string s;
cin >> s;
int n = (int)s.size();
dp[0][0][0] = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < 4; j++){
for(int k = 0; k < 4; k++){
dp[i][j][k] = dp[i - 1][j][k];
}
}
int c;
if(s[i - 1] == 'R') c = 1;
else if(s[i - 1] == 'G') c = 2;
else if(s[i - 1] == 'B') c = 3;
for(int j = 0; j < 4; j++){
for(int k = 0; k < 4; k++){
if(!j && k) continue;
if(!j){
dp[i][c][k] = max(dp[i][c][k], dp[i - 1][j][k]);
}else if(!k){
dp[i][j][c] = max(dp[i][j][c], dp[i - 1][j][k]);
}else{
if(j == k && j == c){
for(int p = 1; p <= 3; p++){
dp[i][p][0] = max(dp[i][p][0], dp[i - 1][j][k] + 1);
}
}else if(j != k && j != c && k != c){
for(int p1 = 1; p1 <= 3; p1++){
for(int p2 = 1; p2 <= 3; p2++){
dp[i][p1][p2] = max(dp[i][p1][p2], dp[i - 1][j][k]);
}
}
}else{
dp[i][k][c] = max(dp[i][k][c], dp[i - 1][j][k]);
}
}
}
}
}
int ans = 0;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
ans = max(ans, dp[n][i][j]);
}
}
cout << ans << '\n';
}

signed main(){
int T = 1;
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> T;
while(T--) solve();
return 0;
}

G - Expectation (Easy Version)

SOLUTION

签到题。

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int mod = 998244353, N = 1e6 + 10;

ll qpow(ll a, ll b){
ll res = 1;
while(b){
if(b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}

ll f[N], inv[N];

void init(int M){
f[0] = 1;
for(int i = 1; i <= M; i++) f[i] = f[i - 1] * i % mod;
inv[M] = qpow(f[M], mod - 2);
for(int i = M - 1; i >= 0; i--){
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}

ll C(ll n, ll m){
return f[n] * inv[m] % mod * inv[n - m] % mod;
}

ll pi[N], qi[N], pre[N];

void solve(){
int n, m, a, b;
cin >> n >> m >> a >> b;
ll p = a * qpow(b, mod - 2) % mod, q = 1 - p;
if(q < 0) q += mod;
pi[0] = qi[0] = 1;
for(int i = 1; i <= n; i++){
pi[i] = pi[i - 1] * p % mod;
qi[i] = qi[i - 1] * q % mod;
pre[i] = pre[i - 1] + qpow(i, m);
if(pre[i] >= mod) pre[i] -= mod;
}
ll ans = 0;
for(int i = 1; i <= n; i++){
ans += pi[i] * qi[n - i] % mod * pre[i] % mod * C(n, i) % mod;
if(ans >= mod) ans -= mod;
}
cout << ans << '\n';
}

int main(){
int T = 1;
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
init(1000000);
cin >> T;
while(T--) solve();
return 0;
}

I - Tree

SOLUTION

模拟题,333lfy秒了

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;

#define gc()(is==it?it=(is=in)+fread(in,1,Q,stdin),(is==it?EOF:*is++):*is++)
const int Q = (1 << 24) + 1, mod = 1000000007, N = 1e6 + 50;
char in[Q], *is = in, *it = in, c;
void read(int &n){
for(n = 0; (c = gc()) < '0' || c > '9';);
for(; c <= '9' && c >= '0'; c = gc()) n = n * 10 + c - 48;
}
int dep[N];
int lg[N*2];
int son[N];
int fa[N];
int sz[N];
int vis[N];
vector<int>e[N];
void init(){
lg[0]=0;
int x=1,y=1;
for(int i=1;i<=2e6;i++){
if(x<i) x*=2,y++;
lg[i]=y;
}
}
void dfs(int x,int y){
if(fa[x]==x){
dep[x]=dep[y]+lg[sz[x]];
}
else if(fa[x]){
dep[x]=dep[fa[x]];
}
else{
dep[x]=dep[y]+1;
}
for(auto i:e[x]){
dfs(i,x);
}
}
void solve(){
int n,i,j,x,y,ans=0;
read(n);
for(i=1;i<=n;i++){
vis[i]=0;
e[i].clear();
sz[i]=0;
fa[i]=0;
}
for(i=1;i<=n;i++){
read(x);
// scanf("%d",&x);
e[x].push_back(i);
}
for(i=1;i<=n;i++){
read(son[i]);
// scanf("%d",son+i);
}
for(i=1;i<=n;i++){
int p=i;
int cnt=1;
if(!vis[i]&&son[i]){
vis[i]=1;
fa[i]=i;
do{
p=son[p];
fa[p]=i;
vis[p]=1;
cnt++;
}while(son[p]);
sz[i]=cnt;
}
}
dfs(1,0);
int mx=0;
for(i=1;i<=n;i++) mx=max(mx,dep[i]);
cout<<mx<<'\n';
}

signed main(){
int size(512<<20); // 512M
__asm__ ( "movq %0, %%rsp\n"::"r"((char*)malloc(size)+size)); // YOUR CODE
int T = 1;
init();
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
init();
read(T);
while(T--) solve();
exit(0);
}

L - Counting Stars

SOLUTION

签到题

CODE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
typedef long long ll;

#define gc()(is==it?it=(is=in)+fread(in,1,Q,stdin),(is==it?EOF:*is++):*is++)
const int Q = (1 << 24) + 1, mod = 1000000007, N = 1e6 + 50;
char in[Q], *is = in, *it = in, c;
void read(int &n){
for(n = 0; (c = gc()) < '0' || c > '9';);
for(; c <= '9' && c >= '0'; c = gc()) n = n * 10 + c - 48;
}

ll qpow(ll a, ll b){
ll res = 1;
while(b){
if(b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}

ll f[N], inv[N];

void init(int M){
f[0] = 1;
for(int i = 1; i <= M; i++) f[i] = f[i - 1] * i % mod;
inv[M] = qpow(f[M], mod - 2);
for(int i = M - 1; i >= 0; i--){
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}

ll C(ll n, ll m){
return f[n] * inv[m] % mod * inv[n - m] % mod;
}

int n,m,a[1000006],b[1000006],ans;
void solve(){
read(n);
read(m);
for (int i=1;i<=n;++i) a[i]=b[i]=0;
ans=0;
for (int i=1,u,v;i<=m;++i) {
read(u);
read(v);
a[u]++,a[v]++;
}
for (int i=1;i<=n;++i) {
for (int j=2;j<=min(a[i],n-1);++j) {
b[j]+=C(a[i],j);
b[j]%=mod;
}
}
for (int i=2;i<=n-1;++i) ans=ans^b[i];
cout << ans << endl;
}

signed main(){
int T = 1;
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
init(1000000);
read(T);
while(T--) solve();
return 0;
}