等差数列求和
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); LL n; cin >> n; cout << n * (n + 1) / 2 % 4399 << '\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; cin >> n; int ans = 0; vector<vector<int>> a(n + 1, vector<int> (n + 1)); vector<vector<bool>> vis(n + 1, vector<bool> (n + 1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } for (int i = 1; i <= n; i++) { if (!vis[1][i]) { vis[1][i] = true; ans += a[1][i]; } } for (int i = 1; i <= n; i++) { if (!vis[i][1]) { vis[i][1] = true; ans += a[i][1]; } } for (int i = 1; i <= n; i++) { if (!vis[i][n]) { vis[i][n] = true; ans += a[i][n]; } } for (int i = 1; i <= n; i++) { if (!vis[n][i]) { vis[n][i] = true; ans += a[n][i]; } } for (int i = 1; i <= n; i++) { if (!vis[i][i]) { vis[i][i] = true; ans += a[i][i]; } } for (int i = 1; i <= n; i++) { if (!vis[i][n - i + 1]) { vis[i][n - i + 1] = true; ans += a[i][n - i + 1]; } } 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 n; cin >> n; LL ans = 0; vector<vector<int>> a(n + 1, vector<int> (n + 1)); vector<vector<bool>> vis(n + 1, vector<bool> (n + 1)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } for (int k = 0; k <= (n + 1) / 2; k++) { LL sum = 0; for (int i = 1 + k; i <= n - k; i++) { if (!vis[1 + k][i]) { vis[1 + k][i] = true; sum += a[1 + k][i]; } } for (int i = 1 + k; i <= n - k; i++) { if (!vis[i][1 + k]) { vis[i][1 + k] = true; sum += a[i][1 + k]; } } for (int i = 1 + k; i <= n - k; i++) { if (!vis[i][n - k]) { vis[i][n - k] = true; sum += a[i][n - k]; } } for (int i = 1; i <= n - k; i++) { if (!vis[n - k][i]) { vis[n - k][i] = true; sum += a[n - k][i]; } } ans = max(ans, sum); } 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 n, m; cin >> n >> m; map<string, LL> mp; LL ans = 0; for (int i = 0; i < n; i++) { int z; cin >> z; for (int j = 0; j < z; j++) { string s; int w; cin >> s >> w; mp[s] += w; } } for (int i = 0; i < m; i++) { string s; int w; cin >> s >> w; ans = ans + 1LL * mp[s] * w; } cout << ans << '\n'; return 0; }
不要被题意平均头发数误导,有一个光头乘积全为0
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << "0\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; cout << min(n, m) << '\n'; LL res = 1; for (int i = 0; i < min(n, m); i++) { res = res * (max(n, m) - i); } cout << res << '\n'; return 0; }
DP
相当于有区别的n的m个划分,循环的时候把n和m遍历互换即可
相当于第m个划分用第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); int n, m; cin >> m >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } vector<int> dp(m + 1); dp[0] = 1; for (int i = 0; i <= m; i++) { for (int j = 1; j <= n; j++) { if (i + a[j] <= m) { dp[i + a[j]] = (dp[i + a[j]] + dp[i]) % mod; } } } cout << dp[m] << '\n'; return 0; }
取模运算性质
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; const int mod = 10000; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; LL res = 1; for (int i = 0; i < n; i++) { string s; cin >> s; int len = s.size(); string t = ""; for (int i = len - 1; i > len - 1 - 4 and i >= 0; i--) { t += s[i]; } reverse(t.begin(), t.end()); LL z = stoll(t); res = res * z % mod; } cout << res << '\n'; return 0; }
AC代码:
14
除了第一个操作,可以一直等到他出J就能赢,否则平
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; string s; cin >> s; for (int i = 1; i < n; i++) { if (s[i] == 'J') { cout << "win\n"; return 0; } } cout << "draw\n"; return 0; }
模拟,细节处理,特殊情况较多,原式可能更优,在某一位置减1后都有可能更优
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); LL n, k; cin >> n >> k; vector<LL> z; LL x = n; while (x) { z.push_back(x % k); x /= k; } LL ans = 0; int len = z.size(); bool ok = true; for (int i = 0; i < len - 1; i++) { if (z[i] != k - 1) { ok = false; break; } } if (ok) { ans = len; for (int i = 0; i < len; i++) { ans += z[i]; } } else { ans = len; LL p = 0; LL sum = len; for (int i = 0; i < len; i++) { sum += z[i]; } for (int i = len - 1; i >= 0; i--) { if (z[i] != 0) { if (i == len - 1 and z[i] == 1) { ans = max(ans, len - 1 + (len - 1) * (k - 1)); } else { ans = max(ans, z[i] - 1 + i * (k - 1) + len + p); } } p += z[i]; } ans = max(ans, sum); } cout << ans << '\n'; return 0; }
湖南理工学院2022年程序设计竞赛新生赛(12/14)
最新推荐文章于 2024-07-04 16:16:29 发布