牛客春招刷题训练营-2025.4.15题解
活动地址: 牛客春招刷题训练营 - 编程打卡活动
简单题 构造A+B
因为 都为正整数,所以共有
个不同的
。
的取值为
,特别地如果
则不存在
。
本题即判断
是否小于等于
。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
if (k > n - 1)cout << "NO\n";
else cout << "YES\n";
return 0;
}
中等题 小红的双生排列
双生排列即排列是奇偶交替的。
为奇数,则排列开头只能为奇数,答案为
。
为偶数,则奇-偶-……-奇-偶或偶-奇-……-偶-奇均可,答案为
。
#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 1000000007;
long long frac(int n) {
long long res = 1;
for (int i = 2; i <= n; i++) {
res *= i;
res %= mod;
}
return res;
}
int main() {
int n;
cin >> n;
long long ans = 1;
if (n % 2 == 1) {
ans *= frac(n / 2 + 1);
ans %= mod;
ans *= frac(n / 2);
ans %= mod;
} else {
ans *= frac(n / 2);
ans %= mod;
ans *= frac(n / 2);
ans %= mod;
ans *= 2;
ans %= mod;
}
cout << ans << '\n';
return 0;
}
困难题 【模板】前缀和
记 为前
项的和,即
。
特别地 。
我们求 可以转换成求
。
只需 预处理
数组即可
查询区间和。
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector<int> a(n + 1);
vector<long long> pre(n + 1);
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++)pre[i] = pre[i - 1] + a[i];
while (q--) {
int l, r;
cin >> l >> r;
cout << pre[r] - pre[l - 1] << '\n';
}
return 0;
}
#牛客春招刷题训练营#