目录
A: 美丽的 2
解题思路:
参考代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
#define ll long long
int main() {
int ans = 0;
for (int i = 1; i <= 2020; i++) {
int x = i;
while (x) {
if (x % 10 == 2) {
ans++;
break;
}
x /= 10;
}
}
cout << ans;
return 0;
}
B: 扩散
解题思路:
参考代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
#define ll long long
bool is[N][N];
struct node {
int x, y, t;
} s[5];
int ans = 0;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void bfs() {
queue<node>q;
for (int i = 1; i <= 4; i++) {
q.push({s[i].x, s[i].y, s[i].t});
is[s[i].x][s[i].y] = 1;
}
while (!q.empty()) {
node now = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int tx = now.x + dx[i];
int ty = now.y + dy[i];
if (tx >= 1 && ty >= 1 && tx < N && ty < N) {
if (is[tx][ty] == 0 && now.t + 1 <= 2020) {
ans++;
is[tx][ty] = 1;
q.push({tx, ty, now.t + 1});
}
}
}
}
}
int main() {
int x = 3000;
s[1] = {0, 0, 0 }, s[2] = {2020, 11, 0 };
s[3] = {11, 14, 0 }, s[4] = {2000, 2000, 0 };
for (int i = 1; i <= 4; i++) {
s[i].x += x, s[i]. y += x;
}
bfs();
cout << ans + 4;
return 0;
}
C: 阶乘约数
解题思路:
唯一分解定理
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
int mp[N];
bool is[N];
vector<int>p(1);
ll ans = 1;
void init() {
for (int i = 2; i <= 100; i++) {
if (!is[i]) p.push_back(i);
for (int j = 1; j < p.size() && i * p[j] <= 100; j++) {
is[i * p[j]] = 1;
if (i % p[j] == 0) break;
}
}
}
int main() {
init();
for (int i = 2; i <= 100; i++) {
int x = i;
while (x != 1) {
for (int j = 1; j < p.size(); j++) {
if (x % p[j] == 0) {
mp[p[j]]++;
x /= p[j];
break;
}
}
}
}
for (int i = 2; i <= 100; i++) {
if (mp[i]) {
ans *= mp[i] + 1;
}
}
cout << ans;
return 0;
}
D: 本质上升序列
解题思路:
参考代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
string s = "?tocyjkdzcieoiodfpbgcncsrjbhmugdnojjddhllnofawllbhfiadgdcdjstemphmnjihecoapdjjrprrqnhgccevdarufmliqijgihhfgdcmxvicfauachlifhafpdccfseflcdgjncadfclvfmadvrnaaahahndsikzssoywakgnfjjaihtniptwoulxbaeqkqhfwl";
int n;
int f[205];
int ans;
int main() {
n = s.size();
for (int i = 1; i < n; i++) {
f[i] = 1;
for (int j = 1; j < i; j++) {
if (s[i] > s[j]) f[i] += f[j];
if (s[i] == s[j]) f[i] -= f[j];
}
ans += f[i];
}
cout << ans;
return 0;
}
E: 玩具蛇
解题思路:
参考代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e1 + 5;
#define ll long long
map<string, int>mp;
bool is[N][N];
int ans[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void dfs(int x, int y, int k) {
if (k == 16) {
string s;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
s.push_back(ans[i][j] + 'a');
}
}
mp[s]++;
return;
}
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx >= 1 && ty >= 1 && tx <= 4 && ty <= 4) {
if (is[tx][ty] == 0) {
ans[tx][ty] = k + 1;
is[tx][ty] = 1;
dfs(tx, ty, k + 1);
is[tx][ty] = 0;
}
}
}
}
int main() {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 4; j++) {
memset(is, 0, sizeof(is));
is[i][j] = 1;
ans[i][j] = 1;
dfs(i, j, 1);
}
}
cout << mp.size();
return 0;
}
试题F: 皮亚诺曲线距离
解题思路:
参考代码:
试题 G: 游园安排
解题思路:
参考代码:
试题 H: 答疑
解题思路:
贪心
参考代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
#define ll long long
int n;
struct node {
ll s, a, e;
} t[N];
ll ans;
bool cmp(node a, node b) {
return a.s + a.a + a.e < b.s + b.a + b.e;
}
ll sum[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> t[i].s >> t[i].a >> t[i].e;
}
sort(t, t + n + 1, cmp);
for (int i = 1; i <= n; i++) {
sum[i] = t[i].s + t[i].a + t[i - 1].e+sum[i - 1];
ans += sum[i];
}
cout << ans;
return 0;
}