输入输出
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; cin >> s; cout << "| _ ____ _____ _____ |\n"; cout << "| | | _ \\ / ____|/ ____| |\n"; cout << "| | | |_) | | __| | __ |\n"; cout << "| _ | | _ <| | |_ | | |_ | |\n"; cout << "| | |__| | |_) | |__| | |__| | |\n"; cout << "| \\____/|____/ \\_____|\\_____| |\n"; return 0; }
输入输出
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<string> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << a[i][j] << a[i][j]; } cout << '\n'; for (int j = 0; j < m; j++) { cout << a[i][j] << a[i][j]; } cout << '\n'; } return 0; }
模拟
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; LL read() { LL x = 0, f = 1, ch = getchar(); for (; !isdigit(ch); ch = getchar()) { if (ch == '-') { f = -f; } } for (; isdigit(ch); ch = getchar()) { x = x * 10 + ch - '0'; } return x * f; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; T = read(); while (T--) { int t1 = read(), t2 = read(), t3 = read(), t4 = read(); int n = read(); vector<int> a(n); bool ok = false; for (int i = 0; i < n; i++) { a[i] = read(); if (a[i] >= t1 and a[i] <= t2) { ok = true; } if (a[i] >= t3 and a[i] <= t4) { ok = true; } } if (ok) { cout << "Y\n"; } else { cout << "N\n"; } } return 0; }
博弈dp,根据数据范围,可以直接通过判断怪兽血量和先手次序进行dp,暴力判断怪兽在i血量下不同先手的胜负
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n, m, x; cin >> n >> m >> x; vector<int> a(n + 1), b(m + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= m; i++) { cin >> b[i]; } vector<vector<int>> dp(x + 1, vector<int> (2)); for (int i = 1; i <= x; i++) { for (int j = 1; j <= n; j++) { if (a[j] >= i) { dp[i][1] = 1; } else { dp[i][1] = max(dp[i][1], dp[i - a[j]][0] ^ 1); } } for (int j = 1; j <= m; j++) { if (b[j] >= i) { dp[i][0] = 1; } else { dp[i][0] = max(dp[i][0], dp[i - b[j]][1] ^ 1); } } } if (dp[x][1]) { cout << "AsindE\n"; } else { cout << "slwang\n"; } } return 0; }
倒序还原数列,调和级数O(nlogn)
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<LL> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } LL ans = a[1]; for (int i = n; i >= 1; i--) { for (int j = 2; j <= n / i; j++) { a[i] -= a[i * j]; } } cout << a[1] << '\n'; return 0; }
模拟
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { string s; cin >> s; int len = s.size(); for (int i = 0; i < len;) { while (i < len and s[i] >= 'A' and s[i] <= 'Z') { i++; } if (i == len) { break; } s[i - 1] += 32; while (i < len and s[i] >= 'a' and s[i] <= 'z') { i++; } if (i == len) { s[i - 1] -= 32; break; } s[i] -= 32; } cout << s << '\n'; } return 0; }
dfs,特判n=1
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; LL ans = 0; cin >> n; vector<LL> a(n + 1); vector<int> in(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } if (n == 1) { cout << a[1] << '\n'; exit(0); } vector<vector<pair<int, int>>> G(n + 1); for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; in[u]++; in[v]++; G[u].push_back(make_pair(v, w)); G[v].push_back(make_pair(u, w)); } vector<int> z; for (int i = 1; i <= n; i++) { if (in[i] == 1) { z.push_back(i); } } int len = z.size(); for (int i = 0; i < len; i++) { vector<LL> calc(n + 1); function<void(int, int)> dfs = [&](int u, int fa) { for (auto it : G[u]) { if (it.first != fa) { calc[it.first] = calc[u] + it.second + a[it.first]; dfs(it.first, u); } } }; calc[z[i]] = a[z[i]]; dfs(z[i], 0); ans = max(ans, *max_element(calc.begin(), calc.end())); } cout << ans << '\n'; return 0; }
构造,相邻的数互为相反数即可
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n, m; cin >> n >> m; vector<vector<int>> a(n + 1, vector<int> (m + 1)); for (int i = 1; i <= n; i++) { if (i & 1) { a[i][1] = 114514; } else { a[i][1] = -114514; } } for (int i = 1; i <= n; i++) { for (int j = 2; j <= m; j++) { if (a[i][j - 1] == 114514) { a[i][j] = -114514; } else { a[i][j] = 114514; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cout << a[i][j] << " \n"[j == m]; } } } return 0; }
位运算,组合数学,把a[i]二进制拆分,分别讨论每一位选与不选,根据异或的性质,异或奇数次为1,偶数次为0,只需记录a[i]中每一位为1的个数,当大于0的时候只选奇数次,根据二项式定理,C(n,m)中m为奇数的和等于m为偶数的和,C(n,m)从0到m的和为2的n次方,所以只算奇数时为2的n-1次方,再乘在这一位为0的数中选择任意个就是C(sum-n,x),x从0到sum-n,可知二项式定理,C(sum-n,x)=2的(sum-n)次方,然后再乘这一位代表多大
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; const int mod = 998244353; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); map<int, int> mp; for (int i = 0; i < 22; i++) { mp[(1 << i)] = i + 1; } int n; cin >> n; vector<int> a(n + 1); vector<int> cnt(25); for (int i = 1; i <= n; i++) { cin >> a[i]; int x = a[i]; for (int j = 0; j < 22; j++) { if ((x >> j) & 1) { cnt[j + 1]++; } } } function<LL(LL, LL)> qp = [&](LL a, LL b) { LL res = 1; for (; b; b >>= 1, a = a * a % mod) { if (b & 1) { res = res * a % mod; } } return res; }; LL ans = 0; for (int i = 1; i <= 22; i++) { if (cnt[i] > 0) { ans = (ans + qp(2, n - cnt[i]) * (1 << (i - 1)) % mod * qp(2, cnt[i] - 1) % mod) % mod; } } cout << ans << '\n'; return 0; }
并查集
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int fa[100010]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i <= 100000; i++) { fa[i] = i; } function<int(int)> getfa = [&](int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; int aa = getfa(x), bb = getfa(y); if (aa != bb) { fa[aa] = bb; } } for (int i = 0; i < n; i++) { cout << getfa(a[i]) << " \n"[i == n - 1]; } return 0; }
数学,x范围[1,1e7],只需要找大于1e7的素数即可
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; LL read() { LL x = 0, f = 1, ch = getchar(); for (; !isdigit(ch); ch = getchar()) { if (ch == '-') { f = -f; } } for (; isdigit(ch); ch = getchar()) { x = x * 10 + ch - '0'; } return x * f; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; T = read(); while (T--) { int x = read(); cout << 1000000007 - x << " " << 1000000007 << '\n'; } return 0; }
数学,可知p的范围(5,1e6)中的一个素数,循环1e6次至少可以得到一个重复的数,即找到循环点,输出循环中最大即可
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string n; cin >> n; LL m, p, sum = 0; cin >> m >> p; int len = n.size(); set<int> se; for (int i = 0; i < m; i++) { for (int k = 0; k < len; k++) { sum = (sum * 10 + n[k] - '0') % p; } if (se.count(sum)) { goto aaa; } se.insert(sum); } aaa:; cout << *se.rbegin() << '\n'; return 0; }
浙江农林大学2022年新生杯程序设计竞赛(12/13)
于 2022-12-12 09:44:23 首次发布