CodeForces GYM 101981 2018-2019 ACM-ICPC, Asia Nanjing Regional Contest
题目链接
Problem:
Problem A. Adrien and Austin:
一堆岩石按照1~n编号顺序放置,没人每次只能拿不超过k个连续的一串岩石,最后拿走岩石的人获胜,求胜方。
考虑若岩石数量为0,则先手必败;
若k>n,则后手必败;
若n为奇数,则先手方可以拿中间段奇数个岩石将剩下的岩石分为对称的两部分,之后每次与后手方操作相同即可必胜;
若n为偶数k>1,则先手方可以拿中间段偶数个岩石将剩下的岩石分为对称的两部分,之后每次与后受访操作相同即可必胜;
若n为偶数但k=1,则先手方无法将岩石分为对称的两部分,则必败。
AC代码:
#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代码:
#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代码:
#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代码:
#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个数的数列 aI,求 ∑i=1n∑j=infac(i,j),其中fac(i,j)为 ∏k=ijak的质因子数量。
a质因子与b的质因子去重即为 a×b的质因子,所以需要求数列中所有数的质因子,算其在结果中计次贡献。
质因子通过素数筛打表求出。
每个质因子贡献为 (pos−pre+1)×(n−pos+1),其中pos是此质因子在数列 ai中被分解出现的位置,pre是上一次出现位置。
AC代码:
#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×m的地图,0表示不可行,1表示可行且初始有一个袋鼠,可操作所有袋鼠同时向四个方向移动(若移动方向不可行则袋鼠不动),求在50000次操作内可使袋鼠抵达同一位置的操作过程。
由于题目数据范围很小,所以随机输出50000次操作最后使所有袋鼠走到同一位置的概率很大。
AC代码:
#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;
}