代码源挑战赛R12
2025年5月18日大约 3 分钟
R12A
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
cout << " ";
for (int j = 1; j <= i * 2 - 1; j++)
cout << "#";
cout << "\n";
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - 1; j++)
cout << " ";
cout << "#\n";
}
return 0;
}
R12B
辗转相减
时间复杂度
#include <bits/stdc++.h>
using namespace std;
int a, b, x, y;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b;
x = y = 0;
while (a != b)
{
if (a > b)
x++, a -= b;
else
y++, b -= a;
}
cout << x << " " << y << "\n";
return 0;
}
辗转相除
时间复杂度
#include <bits/stdc++.h>
using namespace std;
int a, b, x, y;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> a >> b;
x = y = 0;
while (a != b && a && b)
{
if (a > b)
{
x += a / b;
a %= b;
}
else
{
y += b / a;
b %= a;
}
}
if (a == 0)
x--;
if (b == 0)
y--;
cout << x << " " << y << "\n";
return 0;
}
R12C
链表
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000;
const int MAXM = 300000;
int n, m;
int pre[MAXN + 5];
int nxt[MAXN + 5];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
pre[i] = i - 1;
nxt[i] = i + 1;
}
pre[1] = 0;
nxt[n] = 0;
int head = 1;
for (int i = 1; i <= m; i++)
{
int xi;
cin >> xi;
if (xi == head)
continue;
// xi 从链表中删除
nxt[pre[xi]] = nxt[xi];
pre[nxt[xi]] = pre[xi];
// xi 放到链表头
pre[head] = xi;
nxt[xi] = head;
pre[xi] = 0;
head = xi;
}
for (int i = head; i != 0; i = nxt[i])
cout << i << " ";
return 0;
}
标记权值重新排序
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000;
const int MAXM = 300000;
int n, m;
pair<int, int> num[MAXN + 5];
int x[MAXM + 5];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
num[i] = {i, i};
int now = 1;
for (int i = 1; i <= m; i++)
{
cin >> x[i];
num[x[i]].first = --now;
}
sort(num + 1, num + n + 1);
for (int i = 1; i <= n; i++)
cout << num[i].second << " ";
return 0;
}
标记每个数还在不在
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000;
const int MAXM = 300000;
int n, m;
int a[MAXN + MAXM + 5];
int pos[MAXN + MAXM + 5];
bool ok[MAXN + MAXM + 5];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int now = 1 + m;
for (int i = 1; i <= n; i++)
{
a[i + m] = i;
ok[i + m] = true;
pos[i] = i + m;
}
for (int i = 1; i <= m; i++)
{
int xi;
cin >> xi;
ok[pos[xi]] = false;
a[--now] = xi;
ok[now] = true;
pos[xi] = now;
}
for (int i = now; i <= n + m; i++)
if (ok[i])
cout << a[i] << " ";
return 0;
}
离线处理
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300000;
const int MAXM = 300000;
int n, m;
int x[MAXM + 5];
bool flag[MAXN + 5]; // 每个数有没有输出
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> x[i];
for (int i = m; i >= 1; i--)
{
if (!flag[x[i]])
{
cout << x[i] << " ";
flag[x[i]] = true;
}
}
for (int i = 1; i <= n; i++)
if (!flag[i])
cout << i << " ";
return 0;
}
R12D
枚举每种答案组合
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 998244353;
const int MAXN = 3'000'000;
int n;
vector<int> d;
int cnt[MAXN + 5];
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
{
int now = gcd(i, n);
cnt[now]++;
if (cnt[now] == 1)
d.push_back(now);
}
int ans = 0;
for (int i = 0; i < d.size(); i++)
{
int x = cnt[d[i]];
for (int j = 0; j < d.size(); j++)
{
int y = cnt[d[j]];
ans += x * y * gcd(d[i], d[j]);
ans %= MOD;
}
}
cout << ans;
return 0;
}