T1
推公式。
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, Fz, Fm;
bool is_prime(int s) {
for (int i = 2; i * i <= s; i++) {
if (s % i == 0) return 0;
} return 1;
}
int not_big(int x) {
while (!is_prime(x)) x--;
return x;
}
int big(int x) {
while (!is_prime(x)) x++;
return x;
}
signed main() {
int t; cin >> t;
while (t--) {
cin >> n;
int U = not_big(n), V = big(n + 1);
Fz = U * V - 2 * V + 2 * (n - U + 1);
Fm = 2 * U * V;
int Gcd = __gcd(Fz, Fm);
Fz /= Gcd; Fm /= Gcd;
cout << Fz << "/" << Fm << "\n";
}
return 0;
}T2
wssb。
不一定靠墙才能开枪。
简单bfs。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
struct node {
int x, y, w;
bool operator < (const node &i) const { return w > i.w; }
};
int n, m, Sx, Sy, Ex, Ey, f[505][505], dp[4][505][505], v[505][505];
char c[505][505];
queue<pair<int, int>> q;
priority_queue<node> q1;
signed main() {
// freopen("portal.in", "r", stdin);
// freopen("portal.out", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> c[i][j];
if (c[i][j] == 'C') Sx = i, Sy = j;
if (c[i][j] == 'F') Ex = i, Ey = j;
if (c[i][j] == '#') q.push({i, j});
}
}
while (!q.empty()) {
int x = q.front().first, y = q.front().second; q.pop();
for (int i = 0; i < 4; i++) {
int X = x + dx[i], Y = y + dy[i];
if (X && Y && X <= n && Y <= m && c[X][Y] != '#' && !f[X][Y]) {
f[X][Y] = f[x][y] + 1, q.push({X, Y});
}
}
}
for (int i = 1; i <= n; i++) {
int A;
for (int j = 1; j <= m; j++) {
if (c[i][j] == '#') A = j;
dp[0][i][j] = A + 1;
}
}
for (int i = 1; i <= n; i++) {
int A;
for (int j = m; j >= 1; j--) {
if (c[i][j] == '#') A = j;
dp[1][i][j] = A - 1;
}
}
for (int i = 1; i <= m; i++) {
int A;
for (int j = 1; j <= n; j++) {
if (c[j][i] == '#') A = j;
dp[2][j][i] = A + 1;
}
}
for (int i = 1; i <= m; i++) {
int A;
for (int j = n; j >= 1; j--) {
if (c[j][i] == '#') A = j;
dp[3][j][i] = A - 1;
}
}
q1.push({Sx, Sy, 1});
while (!q1.empty()) {
int x = q1.top().x, y = q1.top().y, w = q1.top().w; q1.pop();
if (v[x][y]) continue;
v[x][y] = w;
for (int i = 0; i < 4; i++) {
int X = x + dx[i], Y = y + dy[i];
if (X && Y && X <= n && Y <= m && c[X][Y] != '#') q1.push({X, Y, w + 1});
}
for (int i = 0; i < 4; i++) {
int X = (i > 1 ? dp[i][x][y] : x), Y = (i < 2 ? dp[i][x][y] : y);
q1.push({X, Y, w + f[x][y]});
}
}
if (!v[Ex][Ey]) cout << "nemoguce";
else cout << v[Ex][Ey] - 1;
return 0;
}T3
起点放一只宝可梦,区间DP,会做但是写挂了,我是祝睿融。
#include <bits/stdc++.h>
using namespace std;
const int N = 105, T = 2005, inf = 1e9;
int n, k, m, dp[2][N][T][2], ans, mxt;
struct node {
int a, b, t;
} a[N];
bool cmp(node x, node y) {
return x.a < y.a;
}
int main() {
cin >> m >> k >> n;
int flag = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i].a >> a[i].b >> a[i].t;
mxt = max(mxt, a[i].t);
if (a[i].a == k) flag = 1;
a[i].t--;
}
if (!flag) {
a[++n] = {k, 0, 0};
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++) {
if (a[i].a == k) {
k = i; break;
}
}
for (int l = k; l >= 1; l--) {
for (int r = k; r <= n; r++) {
for (int i = 0; i < a[r].a - a[l].a + min(a[r].a - a[k].a, a[k].a - a[l].a); i++) {
dp[l % 2][r][i][0] = dp[l % 2][r][i][1] = -inf;
}
for (int i = a[r].a - a[l].a + min(a[r].a - a[k].a, a[k].a - a[l].a); i <= mxt; i++) {
if (l == r) {
dp[l & 1][r][i][0] = dp[l & 1][r][i][1] = a[l].b;
continue;
}
if (l != k) {
dp[l & 1][r][i][0] = max(i - (a[l + 1].a - a[l].a) >= 0 ? dp[l + 1 & 1][r][i - (a[l + 1].a - a[l].a)][0] : -inf, i - (a[r].a - a[l].a) >= 0 ? dp[l + 1 & 1][r][i - (a[r].a - a[l].a)][1] : -inf) + (i <= a[l].t ? a[l].b : 0);
} else {
dp[l & 1][r][i][0] = -inf;
}
if (r != k) {
dp[l & 1][r][i][1] = max(i - (a[r].a - a[r - 1].a) >= 0 ? dp[l & 1][r - 1][i - (a[r].a - a[r - 1].a)][1] : -inf, i - (a[r].a - a[l].a) >= 0 ? dp[l & 1][r - 1][i - (a[r].a - a[l].a)][0] : -inf) + (i <= a[r].t ? a[r].b : 0);
} else {
dp[l & 1][r][i][1] = -inf;
}
ans = max({ans, dp[l & 1][r][i][0], dp[l & 1][r][i][1]});
}
}
}
cout << ans;
return 0;
}T4
换根 DP。祝睿融题目。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
struct node { int x, w; };
int n, a[N], d[N], S[N], dp[N];
vector<node> v[N];
void dfs(int x, int fu) {
S[x] = 1;
for (node p : v[x]) {
int i = p.x, w = p.w;
if (i != fu) {
d[i] = d[x] + w - a[x];
dfs(i, x); S[x] += S[i];
}
}
}
void dfs2(int x, int fu) {
for (node p : v[x]) {
int i = p.x, w = p.w;
if (i != fu) {
dp[i] = dp[x] + (n - S[i]) * (w - a[i]) - S[i] * (w - a[x]);
dfs2(i, x);
}
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int x, y, w; cin >> x >> y >> w;
v[x].push_back({y, w});
v[y].push_back({x, w});
}
dfs(1, 0);
for (int i = 1; i <= n; i++) dp[1] += d[i];
dfs2(1, 0);
int minn = 1e18, ans = 0;
for (int i = 1; i <= n; i++) {
if (minn > dp[i]) {
minn = dp[i]; ans = i;
}
}
cout << ans << "\n" << minn;
return 0;
}