Atcoder Regular Contest 148

By xiaruize

A - mod M

很明显,答案仅有可能为1/2,因为当 M=2M=2 时,任何序列的结果均不可能超过 22

那么,可以考虑什么情况下答案为11

那么,显然我们需要找一个数,使得

{A1A2(mod x)A2A3(mod x)An1An(mod x)\begin{cases} A_1 \equiv A_2 (mod\ x)\\ A_2 \equiv A_3 (mod\ x)\\ \cdots \\ A_{n-1} \equiv A_n (mod\ x) \end{cases}

显然,当 x=gcd(A1A2,A2A3,,An1An)x=gcd(|A_1-A_2|,|A_2-A_3|,\cdots , |A_{n-1}-A_n|) 时,上述条件成立,且不存在一个更大的 xx 使得条件成立

此时,若 x=1x=1 ,则答案为 22, 否则答案为 11

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
/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int n;
int g;
int a[N];
// bool en;

int gcd(int x, int y)
{
if (x == 0)
return y;
return gcd(y % x, x);
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> n;
cin >> a[1];
for (int i = 2; i <= n; i++)
{
cin >> a[i];
if (i != 2)
g = gcd(g, abs(a[i] - a[i - 1]));
else
g = abs(a[i] - a[i - 1]);
}
if (g != 1)
{
cout << "1" << endl;
}
else
cout << "2" << endl;
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

B - dp

题目标题很诈骗qwq

暴力枚举是 O(n3)O(n^3) 的,会获得 TLE\color{orange}{TLE} 的好成绩

那么,我们考虑这三维中哪一个是可以不需要的

可以发现,被操作序列的起点是一定的,即第一个 pp . 所以,只需要 O(n2)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
/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int n;
string s;
// bool en;

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> n;
cin >> s;
string res = s;
for (int i = 0; i < n; i++)
{
if (s[i] == 'p')
{
for (int j = i; j < n; j++)
{
string tmp = s;
for (int k = i; k <= j; k++)
{
tmp[k] = (s[j - k + i] == 'd' ? 'p' : 'd');
}
// cerr << tmp << endl;
if (tmp < res)
res = tmp;
}
break;
}
}
cout << res << endl;
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

C - Lights Out on Tree

观察样例的第2个query的操作过程,我们发现,对于任意一个连通的部分,答案为 与当前连通块相连的节点个数

实现时需要注意复杂度与常数,遍历每个节点的儿子会导致超时,使用memset也有可能导致TLE

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
/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
int n, q;
int p[N];
int m;
int v[N];
int deg[N];
bool st[N];
int cnt[N];
// bool en;

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
cin >> n >> q;
for (int i = 2; i <= n; i++)
{
cin >> p[i];
deg[p[i]]++;
}
while (q--)
{
cin >> m;
int res = 0;
for (int i = 1; i <= m; i++)
{
cin >> v[i];
st[v[i]] = true;
cnt[p[v[i]]]++;
}
for (int i = 1; i <= m; i++)
{
res += deg[v[i]] - cnt[v[i]];
if (!st[p[v[i]]])
res++;
}
for (int i = 1; i <= m; i++)
{
st[v[i]] = false;
cnt[p[v[i]]] = 0;
}
cout << res << endl;
}
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

D - mod M Game

考场第一想法是,每次Alice取什么,Bob就取什么,才能保证Bob,否则Alice

这个想法如果没有 MM 是正确的,那么什么情况下会非法呢?

1
2
2 8
1 3 5 7
1
Bob

那么,为什么会出现这种情况呢?

显然是因为原数组在模 m2\frac{m}{2} 后可以一一对应(m和n均为偶数)

所以可以特判该情况,并 AC\color{green}{AC} 本题

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
/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
const int N = 4e5 + 10;

int n, m;
int a[N];
map<int, int> mp;
vector<int> x, y;

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> n >> m;
for (int i = 1; i <= n + n; i++)
{
cin >> a[i];
mp[a[i]]++;
}
bool flag = true;
for (auto v : mp)
{
if (v.sec % 2 == 1)
{
flag = false;
if (m % 2 == 1)
{
cout << "Alice" << endl;
return 0;
}
else
{
if (v.fir >= m / 2)
y.pb(v.fir - m / 2);
else
x.pb(v.fir);
}
}
}
if (flag || (x == y && x.size() % 2 == 0))
cout << "Bob" << endl;
else
cout << "Alice" << endl;
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}

待补