CodeForces GYM 101981 2018-2019 ACM-ICPC, Asia Nanjing Regional Contest

题目链接

Problem:

https://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjing-regional-contest-en.pdf

Problem A. Adrien and Austin:

一堆岩石按照1~n编号顺序放置,没人每次只能拿不超过k个连续的一串岩石,最后拿走岩石的人获胜,求胜方。

考虑若岩石数量为0,则先手必败;

若k>n,则后手必败;

若n为奇数,则先手方可以拿中间段奇数个岩石将剩下的岩石分为对称的两部分,之后每次与后手方操作相同即可必胜;

若n为偶数k>1,则先手方可以拿中间段偶数个岩石将剩下的岩石分为对称的两部分,之后每次与后受访操作相同即可必胜;

若n为偶数但k=1,则先手方无法将岩石分为对称的两部分,则必败。

AC代码:

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
#include <bits/stdc++.h>
using namespace std;

int main(int argc, char *argv[]) {
int N, K;
scanf("%d%d", &N, &K);
if (N == 0) {
printf("Austin\n");
}
else if (K >= N) {
printf("Adrien\n");
}
else {
if (N & 1) {
printf("Adrien\n");
}
else {
if (K == 1) {
printf("Austin\n");
}
else {
printf("Adrien\n");
}
}
}
return 0;
}

Problem D. Country Meow:

最小球覆盖裸题,模拟退火的时候初始概率适当大一点。

AC代码:

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
#include <bits/stdc++.h>
using namespace std;

const double INF = 1e20;
const int maxn = 1e2 + 5;
const double eps = 1e-8;
const double delta = 0.98;

struct Point {
double X, Y, Z;

void Input() {
scanf("%lf%lf%lf", &X, &Y, &Z);
}
};

double Distance(Point A, Point B) {
return sqrt((A.X - B.X) * (A.X - B.X) + (A.Y - B.Y) * (A.Y - B.Y) + (A.Z - B.Z) * (A.Z - B.Z));
}

int N;
Point points[maxn];

double MinimumSphereCoverage() {
Point Cur = points[0];
double Probability = 10000, Ans = INF;
while (Probability > eps) {
int Book = 0;
for (int i = 0; i < N; ++i) {
if (Distance(Cur, points[i]) > Distance(Cur, points[Book])) {
Book = i;
}
}
double Redius = Distance(Cur, points[Book]);
Ans = min(Ans, Redius);
Cur.X += (points[Book].X - Cur.X) / Redius * Probability;
Cur.Y += (points[Book].Y - Cur.Y) / Redius * Probability;
Cur.Z += (points[Book].Z - Cur.Z) / Redius * Probability;
Probability *= delta;
}
return Ans;
}

int main(int argc, char *argv[]) {
scanf("%d", &N);
for (int i = 0; i < N; ++i) {
points[i].Input();
}
printf("%.15lf\n", MinimumSphereCoverage());
return 0;
}

Problem G. Pyramid:

数正三角形数量。

通过n<=4(1:1,2:5,3:15,4:35)找规律可观察得结果为C(n+4,4),题目要求取模需要求出4!=24的逆元进行计算。

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;

int main(int argc, char *argv[]) {
long long T;
scanf("%lld", &T);
for (long long Case = 1, N, Ans; Case <= T; ++Case) {
scanf("%lld", &N);
Ans = (((N + 3) * (N + 2) % mod) * (N + 1) % mod) * N % mod;
Ans = Ans * 41666667 % mod;
printf("%lld\n", Ans);
}
return 0;
}

Problem I. Magic Potion:

n个英雄m个怪兽,每个英雄可以击杀一个怪兽,另外有k瓶药水,每瓶药水可以使一个英雄多击杀一个怪兽,求最多可以击杀多少怪兽。

最大流问题,建图时源点与每个英雄建容量为1的边,英雄与其可以击杀的怪兽建容量为1的边,每个怪兽与汇点建容量为1的边,因为另外有k瓶药水,所以可以最多增加k次击杀,可以建立一个补充点,源点与补充点建容量为k的边(相当于多给k此机会),补充点与每个英雄建容量为1的边,源点到汇点的最大流即为结果。

AC代码:

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
#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 1e4 + 5;

struct Edge {
int V, Weight, Next;
};

Edge edges[maxn << 1];
int Head[maxn];
int Tot;
int Depth[maxn];
int Current[maxn];

void Init() {
Tot = 0;
memset(Head, -1, sizeof(Head));
}

void AddEdge(int U, int V, int Weight, int ReverseWeight = 0) {
edges[Tot] = Edge {V, Weight, Head[U]};
Head[U] = Tot++;

edges[Tot] = Edge {U, ReverseWeight, Head[V]};
Head[V] = Tot++;
}

bool Bfs(int Start, int End) {
memset(Depth, -1, sizeof(Depth));
queue<int> Que;
Depth[Start] = 0;
Que.push(Start);
while (!Que.empty()) {
int Vertex = Que.front(); Que.pop();
for (int i = Head[Vertex]; ~i; i = edges[i].Next) {
if (Depth[edges[i].V] == -1 && edges[i].Weight > 0) {
Depth[edges[i].V] = Depth[Vertex] + 1;
Que.push(edges[i].V);
}
}
}
return Depth[End] != -1;
}

int Dfs(int Vertex, int End, int NowFlow) {
if (Vertex == End || NowFlow == 0) {
return NowFlow;
}
int UsableFlow = 0, FindFlow;
for (int &i = Current[Vertex]; ~i; i = edges[i].Next) {
if (edges[i].Weight > 0 && Depth[edges[i].V] == Depth[Vertex] + 1) {
FindFlow = Dfs(edges[i].V, End, min(NowFlow - UsableFlow, edges[i].Weight));
if (FindFlow > 0) {
edges[i].Weight -= FindFlow;
edges[i ^ 1].Weight += FindFlow;
UsableFlow += FindFlow;
if (UsableFlow == NowFlow) {
return NowFlow;
}
}
}
}
if (!UsableFlow) {
Depth[Vertex] = -2;
}
return UsableFlow;
}

int N, M, K;
int Ans;

int Dinic(int Start, int End) {
int MaxFlow = 0;
while (Bfs(Start, End)) {
for (int i = 0; i <= N + M + 2; ++i) {
Current[i] = Head[i];
}
MaxFlow += Dfs(Start, End, INF);
}
return MaxFlow;
}

int main(int argc, char *argv[]) {
Init();
scanf("%d%d%d", &N, &M, &K);
AddEdge(0, N + M + 1, K);
for (int i = 1, T; i <= N; ++i) {
scanf("%d", &T);
for (int j = 0, Tar; j < T; ++j) {
scanf("%d", &Tar);
AddEdge(i, N + Tar, 1);
}
AddEdge(0, i, 1);
AddEdge(N + M + 1, i, 1);
}
for (int i = 1; i <= M; ++i) {
AddEdge(N + i, N + M + 2, 1);
}
Ans = Dinic(0, N + M + 2);
printf("%d\n", Ans);
return 0;
}

Problem J. Prime Game:

n个数的数列$a_I$,求$\sum_{i=1}^{n}\sum_{j=i}^{n}fac(i,j)$,其中fac(i,j)为$\prod_{k=i}^{j}a_k$的质因子数量。

a质因子与b的质因子去重即为$a\times b$的质因子,所以需要求数列中所有数的质因子,算其在结果中计次贡献。

质因子通过素数筛打表求出。

每个质因子贡献为$(pos- pre + 1)\times(n-pos + 1)$,其中pos是此质因子在数列$a_i$中被分解出现的位置,pre是上一次出现位置。

AC代码:

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
#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 5;

bool IsPrime[maxn];
vector<int> PrimeFactor[maxn];

void Init() {
memset(IsPrime, true, sizeof(IsPrime));
for (long long i = 2; i < maxn; ++i) {
if (IsPrime[i]) {
PrimeFactor[i].push_back(i);
for (long long j = i + i; j < maxn; j += i) {
IsPrime[j] = false;
PrimeFactor[j].push_back(i);
}
}
}
IsPrime[1] = false;
}

long long N;
int Cnt;
long long Ans;
bool Vis[maxn];
long long A[maxn], Factor[maxn];
vector<int> Pos[maxn];

int main(int argc, char *argv[]) {
Init();
scanf("%lld", &N);
for (int i = 1; i <= N; ++i) {
scanf("%lld", &A[i]);
}
Ans = 0; Cnt = 1;
memset(Vis, false, sizeof(Vis));
for (int i = 1; i <= N; ++i) {
for (int f : PrimeFactor[A[i]]) {
if (!Vis[f]) {
Vis[f] = true;
Factor[Cnt++] = f;
}
Pos[f].push_back(i);
}
}
for (int i = 1; i < Cnt; ++i) {
long long Pre = 1;
for (auto p : Pos[Factor[i]]) {
Ans += (p - Pre + 1) * (N - p + 1);
Pre = p + 1;
}
}
printf("%lld\n", Ans);
return 0;
}

Problem K. Kangaroo Puzzle:

一幅$n\times m$的地图,0表示不可行,1表示可行且初始有一个袋鼠,可操作所有袋鼠同时向四个方向移动(若移动方向不可行则袋鼠不动),求在50000次操作内可使袋鼠抵达同一位置的操作过程。

由于题目数据范围很小,所以随机输出50000次操作最后使所有袋鼠走到同一位置的概率很大。

AC代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;

int N, M;

int main(int argc, char *argv[]) {
srand(time(NULL));
scanf("%d%d", &N, &M);
for (int i = 0, X; i < N; ++i) {
for (int j = 0;j < M; ++j) {
scanf("%d", &X);
}
}
char Operate[4] = {'U', 'D', 'L', 'R'};
for (int i = 0; i < 5e4; ++i) {
printf("%c", Operate[rand() % 4]);
}
return 0;
}
文章目录
  1. 1. 题目链接
  2. 2. Problem:
  3. 3. Problem A. Adrien and Austin:
    1. 3.1. AC代码:
  4. 4. Problem D. Country Meow:
    1. 4.1. AC代码:
  5. 5. Problem G. Pyramid:
    1. 5.1. AC代码:
  6. 6. Problem I. Magic Potion:
    1. 6.1. AC代码:
  7. 7. Problem J. Prime Game:
    1. 7.1. AC代码:
  8. 8. Problem K. Kangaroo Puzzle:
    1. 8.1. AC代码:
|