A、牛牛的mex
题解:
这题注意看a[i]的数值范围,处理一下前缀和后缀中的最小值即可,不在这个区间中的最小值即min(前缀中的最小值, 后缀中的最小值)
AC代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
const int N = 1e5 + 15;
int a[N];
int L[N], R[N];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i];
L[0] = R[n + 1] = n;
for (int i = 1; i <= n; i++)
L[i] = min(L[i - 1], a[i]);
for (int i = n; i >= 0; i--)
R[i] = min(R[i + 1], a[i]);
while (q--) {
int l, r;
cin >> l >> r;
cout << min(L[l - 1], R[r + 1]) << endl;
}
return 0;
}
B、牛牛的算术
题解:
遇见这种题最主要的就是化简式子,列举前几项,找规律化简
n = 1:res = 1 * 1 * 1
n = 2:res = (1 * 1 * 1) * (2 * 1 * 1 + 2 * 2 * 1 + 2 * 2 * 2)
n = 3:res = (1 * 1 * 1) * (2 * 1 * 1 + 2 * 2 * 1 + 2 * 2 * 2) * (3 * 1 * 1 + 3 * 2 * 1 + 3 * 2 * 2 + 3 * 3 * 1 + 3 * 3 * 2 + 3 * 3 * 3)
······
尝试化简:(对n = 3时)
1、对每一项化简,提出一个i的(1 * 2 * 3) * (1 * 1 + 2 * (1 + 2))* (1 * 1 + 2 * (1 + 2)+ 3 * (1 + 2 + 3))
2、得到:res = n! * (1 * sum[1]) * (1 * sum[1] + 2 * sum[2]) * (1 * sum[1] + 2 * sum[2] + 3 * sum[3])
因为第一项是n的阶乘,所以当n >= 199999时,取模之后res = 0;
复杂度 O(n)
AC代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<string>
using namespace std;
#define ll long long
const int mod = 199999;
ll nsum[mod + 15];
void init() {
ll mulsum = 0, sum = 0;
nsum[0] = 1;
for (int i = 1; i <= mod + 1; i++) {
sum = (sum + i) % mod;
mulsum = (mulsum + sum * i) % mod;
nsum[i] = i * ((nsum[i - 1] * (mulsum % mod)) % mod) % mod;
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
init();
int t; cin >> t;
while (t--) {
string n; cin >> n;
int res = 0;
int flag = 0;
for (int i = 0; i < n.length(); i++) {
res = res * 10 + (n[i] - '0');
if (res >= mod) {
flag = 1;
break;
}
}
if (flag)
cout << 0 << endl;
else {
cout << nsum[res] % mod << endl;
}
}
return 0;
}