A.余数游戏(mod)
题意
给定正整数 ,请找到一正整数 满足 且使得 的值尽可能大。输出 的最大值。
思路
可化为 。因为 ,所以 ,进一步化为 。可以发现当 时,两数相乘达到最大值,输出即可。
代码
#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)
题意
你有五个长度为 的数组 ,,,,,请求出:
其中 是 的中位数。 该结果可能很大,请输出其对 取模后的值。
思路
可以枚举中位数,接着枚举两个数组,看这两个数组中分别有几个小于中位数的,再枚举两个数组,看这两个数组中分别有几个大于中位数的,最后将得到的四个数乘起来并乘上中位数,就是当前中位数对答案的贡献。
因为可能有重复的数字,所以记录所属数组编号,避免重复计算,接着直接二分处理数量计算答案即可。
代码
#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)
题意
给定一个长度为 的数组 ,请求出最小的 满足以下条件:
- 对于
思路
可以发现,如果 是 的因数,则不合法。所以对于输入的 ,可以记录 出现。接着从小到大枚举 ,用调和级数枚举 在 以内的倍数。如果枚举倍数被标记,则说明当前 不合法;否则输出最小答案。
代码
#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)
题意
请统计,存在多少长度为 的整数数列 ,满足:
- ;
- 数列中任意一个长度大于 的区间的区间和均大于等于 。即,对于任何 , 都有 。
满足上述条件的数列可能非常多,请输出答案对 998244353 取模后的结果。
思路
因为状态复杂,所以考虑 dp。
设计状态 表示处理到第 个点后缀最小值为 时的方案数。
所以转移为:
可以对 dp 数组做差分辅助转移。
最终答案为:
即终止状态求和。
代码
#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;
}