A 余数游戏(mod)
本题总结:
- 思考时间:约 分钟。
- 代码调试时间:约 分钟。
- 问题:高精度模版不熟且不会用
__int128。 - 改进方案:学习使用并复习高精度加减乘除。
思路:
这里可以分为两种情况:
- 此时 , 原式 .
- 此时 , 原式 .
由于当乘数和一定时,两数相差越小积越大,可以证明,当 时,答案最大。
实现:
本题可用 __int128 实现。
使用方法:
vector<int> num;
long long n;
cin >> n; // __int128 不可使用 cin , cout 。
__int128 p = f(n); //范围:[-2^127 ~ 2^127 - 1]
while(p){
num.push_back(p % 10); //可参加运算。
p /= 10;
}
reverse(num.begin(), num.end());
for(int x : num){
cout << x;
}高精度:
#include<bits/stdc++.h>
using namespace std;
long long n;
string mul(const string &s, const string &t){
string ans = "";
vector<int> num(50);
for(int i = 0; i < t.size(); i++){
int x = (t[i] - '0');
for(int j = 0; j < s.size(); j++){
int y = (s[j] - '0');
num[i + j] += x * y;
num[i + j + 1] += num[i + j] / 10;
num[i + j] %= 10;
}
}
bool f = 0;
for(int i = num.size() - 1; i >= 0; i--){
int x = num[i];
if(x || f){
ans += (x + '0');
if(x) f = 1;
}
}
//reverse(ans.begin(), ans.end());
return ans;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
freopen("mod.in", "r", stdin);
freopen("mod.out", "w", stdout);
cin >> n;
long long k = (n / 2 + 1);
long long k2 = k;
k = (n % k);
string s = "", t = "";
while(k2){
s += (k2 % 10 + '0');
k2 /= 10;
}
while(k){
t += (k % 10 + '0');
k /= 10;
}
cout << mul(s, t);
return 0;
}
B 中位数(med)
本题总结:
- 思考时间:约 分钟。
- 代码调试时间:基本无。
- 问题:只会暴力,不知道更优解。
- 改进方案:写阶段只需写暴力即可,写完就“跑路”看下一题。
思路:
按题意模拟。 时间复杂度: ,两秒大约能过 级别。
实现:
暴力:
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1, mod = 998244353;
int n;
int a[N], b[N], c[N], d[N], e[N];
long long ans;
void input(int num[]){
for(int i = 1; i <= n; i++){
cin >> num[i];
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
freopen("med.in", "r", stdin);
freopen("med.out", "w", stdout);
cin >> n;
input(a), input(b), input(c), input(d), input(e);
vector<int> num(5), v;
for(int i = 1; i <= n; i++){
num[0] = a[i];
for(int j = 1; j <= n; j++){
num[1] = b[j];
for(int k = 1; k <= n; k++){
num[2] = c[k];
for(int l = 1; l <= n; l++){
num[3] = d[l];
for(int m = 1; m <= n; m++){
num[4] = e[m];
v = num;
sort(v.begin(), v.end());
ans = (ans + v[2]) % mod;
}
}
}
}
}
cout << ans;
return 0;
}
C 丝血反杀(counter)
本题总结:
- 思考时间:约 分钟。
- 代码调试时间:约 分钟。
- 问题:思路有 对(想到找约数,没想到找倍数更省时间),但实现有问题(代码没写对,只拿了特判分)。
- 改进方案:学会模版,写代码能力薄弱,训练代码实现能力。
思路:
转化题目可得:找到一个最小正整数 使得 ,即 ,其中对于任意 都有 ,否则直接输出 。
所以只需找每个 的约数 ,用标记数组标记 ,最后找到最小的未标记的 即可 ,倍数法时间更快。
实现:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e6 + 1;
int n, mxv;
int vis[N << 1];
int main(){
ios::sync_with_stdio(0);
freopen("counter.in", "r", stdin);
freopen("counter.out", "w", stdout);
cin >> n;
for(int i = 1, x; i <= n; i++){
cin >> x;
mxv = max(mxv, x);
vis[x - 1] = 1;
if(x == 1){
cout << -1;
return 0;
}
}
for(int k = 2; k <= mxv; k++){
bool f = 1;
for(int j = k; j <= mxv; j += k){
if(vis[j]){
f = 0;
break;
}
}
if(f){
cout << k;
return 0;
}
}
cout << -1;
return 0;
}
D 未尝一负(neg)
本题总结:
- 思考时间:暴力 分钟,用 分钟发现状态及转移。
- 代码调试时间:暴力 分钟,动规未实现。
- 问题:
freopen注释未消,导致爆 ,转移思考时间太慢。 - 改进方案:多加细心检查,大不了加一行注释提醒删,动规需加练。
思路:
首先需发现性质:判定整个数组所有区间和是否大于等于零,只需判断所有相邻两个数和相邻三个数之和是否大于等于零即可。
- 分:
- 暴搜。时间复杂度:
- 分:
- 设计状态 表示 时的合法方案数。
- 转移:
- 时间复杂度:
- 分:
- 在 基础上用前缀和与滚动数组优化时空。
- 满分:
-
仔细分析后可得结论:任意长度大于等于二的子段和大于等于零,等价于每个前缀(a1 到 ai)中,所有长度大于等于二的后缀和的最小值大于等于零。
-
重设状态: 表前 个数中后缀和最小为 时的方案数。
-
转移:
- 优化:用前缀和,时间复杂度从 。
实现:
每个 Subtask 都表示不同分数的写法。
#include<bits/stdc++.h>
using namespace std;
using vi = vector<int>;
const int N = 5e3 + 10, mod = 998244353;
int n, m;
namespace Subtask1_4{
const int INF = 1e9 + 1;
int ans = 0;
void dfs(int i, int lt, int llt){
if(i == n + 1){
(ans += 1) %= mod;
return ;
}
for(int now = -m; now <= m; now++){
if(now + lt + llt >= 0 && now + lt >= 0){
dfs(i + 1, now, lt);
}
}
}
void Solve(){
dfs(1, INF, INF);
cout << ans;
}
}
namespace Subtask5_10{
const int V = 10;
int dp[N][V][V], ans = 0;
void Solve(){
dp[0][2][2] = 1;
for(int i = 1; i <= n; i++){
for(int x = -m; x <= m; x++){
for(int y = -m; y <= m; y++){
if(x + y < 0) continue;
int cntx = x + 1, cnty = y + 1;
for(int z = -m; z <= m; z++){
if(x + y + z < 0 || y + z < 0) continue;
int cntz = z + 1;
(dp[i][cntx][cnty] += dp[i - 1][cnty][cntz]) %= mod;
}
}
}
}
for(int i = 0; i <= m << 1; i++){
for(int j = 0; j <= m << 1; j++){
(ans += dp[n][i][j]) %= mod;
}
}
cout << ans;
}
}
namespace Subtask11_16{
int cur[N][N], now[N][N];
int clac(int i, int l, int r){
return l <= 0 ? now[i][r] : (now[i][r] - now[i][l - 1] + mod) % mod;
}
void Solve(){
now[m << 1][m << 1] = 1;
for(int i = 1; i <= n; i++){
for(int x = -m; x <= m; x++){
for(int y = -m; y <= m; y++){
if(x + y < 0) continue;
int j = x + m, k = y + m;
cur[j][k] = clac(k, max({-m, -y, -x - y}) + m, m + m);
}
}
for(int x = -m; x <= m; x++){
for(int y = -m; y <= m; y++){
int j = x + m, k = y + m;
now[j][k] = !k ? cur[j][k] : (now[j][k - 1] + cur[j][k]) % mod;
cur[j][k] = 0;
}
}
}
int ans = 0;
for(int x = -m; x <= m; x++){
int j = x + m;
(ans += now[j][m << 1]) %= mod;
}
cout << ans;
}
}
namespace Task_all{
int dp[N][N << 1], pre[N << 1];
int calc(int l, int r){
return (pre[r] - pre[l - 1] + mod) % mod;
}
void Solve(){
pre[m + m] = 1;
for(int i = 1; i <= n; i++){
for(int j = -m; j < 0; j++){
dp[i][j + m] = calc(m - j, m + m);
}
for(int j = 0; j <= m; j++){
dp[i][j + m] = calc(j, m + m);
}
for(int j = -m; j <= m; j++){
pre[j + m] = (j == -m ? dp[i][j + m] : (pre[j + m - 1] + dp[i][j + m]) % mod);
}
}
cout << pre[m + m];
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
freopen("neg.in", "r", stdin);
freopen("neg.out", "w", stdout);
cin >> n >> m;
if(n <= 17 && m == 1) Subtask1_4::Solve(); //20分
else if(n <= 300 && m == 1) Subtask5_10::Solve(); // 50分
else if(n <= 300 && m <= 300) Subtask11_16::Solve(); // 80分
else Task_all::Solve(); // 100分
return 0;
}