2025 暑期集训A班 模拟赛#5 总结

· · 个人记录

A.余数游戏(mod)

题意

给定正整数 n ,请找到一正整数 k 满足 1\le k\le n 且使得 k \times (n \bmod k) 的值尽可能大。输出 k \times (n \bmod k) 的最大值。

思路

## 代码 ```cpp #include<bits/stdc++.h> #define int long long using ull = unsigned long long; using i128 = __int128; using namespace std; i128 n; void write(i128 x){ if(x == 0){ cout << 0 << "\n"; return ; } vector<int> ans; while(x){ ans.push_back(x % 10); x /= 10; } reverse(ans.begin(), ans.end()); for(int i : ans){ cout << i; } cout << "\n"; } i128 read(){ i128 sum = 0; char c; while(cin >> c){ sum = sum * 10 + (c - '0'); } return sum; } signed main(){ ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); freopen("mod.in", "r", stdin); freopen("mod.out", "w", stdout); n = read(); i128 x = n / 2; write((x + 1) * ((n - x - 1) % ((x + 1) * (x + 1)))); return 0; } ``` # B.中位数(med) ## 题意 你有五个长度为 $N$ 的数组 $\{A_1, A_2,\dots , A_N \}$,$\{B_1, B_2,\dots , B_N \}$,$\{C_1, C_2,\dots , C_N \}$,$\{D_1, D_2,\dots , D_N \}$,$\{E_1, E_2,\dots , E_N \}$,请求出: $$\sum \limits_{a=1}^N \sum \limits_{b=1}^N \sum \limits_{c=1}^N \sum \limits_{d=1}^N \sum \limits_{e=1}^N med(A_a, B_b, C_c, D_d, E_e)$$ 其中 $med(A_a, B_b, C_c, D_d, E_e)$ 是 $\{A_a, B_b, C_c, D_d, E_e\}$ 的中位数。 该结果可能很大,请输出其对 $998244353$ 取模后的值。 ## 思路 可以枚举中位数,接着枚举两个数组,看这两个数组中分别有几个小于中位数的,再枚举两个数组,看这两个数组中分别有几个大于中位数的,最后将得到的四个数乘起来并乘上中位数,就是当前中位数对答案的贡献。 因为可能有重复的数字,所以记录所属数组编号,避免重复计算,接着直接二分处理数量计算答案即可。 ## 代码 ```cpp #include<bits/stdc++.h> using namespace std; #define int long long const int N = 1e5 + 10, p = 998244353; int n; long long ans; struct P{ int v, id; bool operator < (const P &b) const{ if(v != b.v){ return v < b.v; } return id < b.id; } }h[10][N]; signed main(){ ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); freopen("med.in", "r", stdin); freopen("med.out", "w", stdout); cin >> n; for(int i = 1; i <= 5; i++){ for(int j = 1; j <= n; j++){ cin >> h[i][j].v; h[i][j].id = i; } sort(h[i] + 1, h[i] + 1 + n); } for(int o = 1; o <= 5; o++){ for(int k = 1; k <= n; k++){ for(int i = 1; i <= 5; i++){ for(int j = i + 1; j <= 5; j++){ for(int x = 1; x <= 5; x++){ for(int y = x + 1; y <= 5; y++){ if(i == o || j == o || x == o || y == o || j == x || y == i || i == x || j == y) continue; int p1 = lower_bound(h[i] + 1, h[i] + 1 + n, h[o][k]) - h[i] - 1; int p2 = lower_bound(h[j] + 1, h[j] + 1 + n, h[o][k]) - h[j] - 1; int p3 = upper_bound(h[x] + 1, h[x] + 1 + n, h[o][k]) - h[x] - 1; int p4 = upper_bound(h[y] + 1, h[y] + 1 + n, h[o][k]) - h[y] - 1; ans += h[o][k].v * (n - p4) % p * p1 % p * (n - p3) % p * p2 % p; ans %= p; } } } } } } cout << ans; return 0; } ``` # C.丝血反杀(counter) ## 题意 给定一个长度为 $n$ 的数组 $a$,请求出最小的 $x$ 满足以下条件: - 对于 $\forall a_i,(a_i - 1) \bmod x \neq 0

思路

可以发现,如果 x 是 a_i - 1 的因数,则不合法。所以对于输入的 a_i,可以记录 a_i-1 出现。接着从小到大枚举 x,用调和级数枚举 x 在 10^6 以内的倍数。如果枚举倍数被标记,则说明当前 x 不合法;否则输出最小答案。

代码

#include<bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10;

int n, a[N], cnt[N];

int main(){
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  freopen("counter.in", "r", stdin);
  freopen("counter.out", "w", stdout);
  cin >> n;
  bool is_1 = 0;
  for(int i = 1; i <= n; i++){
    cin >> a[i];
    cnt[a[i] - 1]++;
    if(a[i] == 1){
      is_1 = 1;
    }
  }
  if(is_1){
    cout << -1;
    return 0;
  }
  for(int i = 1; i <= 1e6; i++){
    bool f = 1;
    for(int j = 1; j * i * 1ll <= 1e6; j++){
      if(cnt[i * j] >= 1){
        f = 0;
        break;
      }
    }
    if(f){
      cout << i;
      return 0;
    }
  }
  return 0;
}

未尝一负(neg)

题意

请统计,存在多少长度为n 的整数数列 a_1, a_2,\dots , a_n,满足:

满足上述条件的数列可能非常多,请输出答案对 998244353 取模后的结果。

思路

因为状态复杂,所以考虑 dp。

设计状态 dp_{i,j} 表示处理到第 i 个点后缀最小值为 j 时的方案数。

所以转移为:

dp_{i,j} \Longleftarrow dp_{i-1,min(j-k,-k)}(-m \le k \le j)

可以对 dp 数组做差分辅助转移。

最终答案为:

\sum \limits _{i=-m}^m dp_{n,i}

即终止状态求和。

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long

const int p = 998244353, base = 5141, N = 5000 + 10;

int n, m, dp[N][N + base], ans;

signed main(){
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  freopen("neg.in", "r", stdin);
  freopen("neg.out", "w", stdout);
  cin >> n >> m;
  for(int i = -m + base; i <= m + base; i++){
    dp[1][i] = 1;
  }
  for(int i = 2; i <= n; i++){
    for(int j = -m; j <= 0; j++){
      dp[i][base] = (dp[i][base] + dp[i - 1][j + base]) % p;
      dp[i][j + m + 1 + base] = (dp[i][j + m + 1 + base] - dp[i - 1][j + base] + p) % p;
    }
    for(int j = 1; j <= m; j++){
      dp[i][-j + base] = (dp[i][-j + base] + dp[i - 1][j + base]) % p;
      dp[i][m + 1 + base] = (dp[i][m + 1 + base] - dp[i - 1][j + base] + p) % p;
    }
    for(int j = -m + base; j <= m + base; j++){
      dp[i][j] = (dp[i][j] + dp[i][j - 1]) % p;
    }
  }
  for(int i = -m + base; i <= m + base; i++){
    ans += dp[n][i] % p;
    ans %= p;
  }
  cout << ans;
  return 0;
}