题目链接:https://ac.nowcoder.com/acm/contest/9033/C
题意:给定n<=1e5个数字,求所有区间的mex值之和。
题解:很模板的一道题目了,我怎么就想了那么久??第一题都错了5发,看来我还是不够平常心对待比赛emmmm。唉什么时候才能懂得?
1.讨论不选1
选1(不选2 ,选2 ..........)
唉,递归就递归,怎么就盲目递归,明知道是哪里错了(讨论方式总数的地方)。仔细一思考就出来了
代码:
#include <bits/stdc++.h>
#define ll long long
#define ld double
#define pi acos(-1)
#define pb push_back
#define mst(a, i) memset(a, i, sizeof(a))
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define rep(i, a, n) for (ll i = a; i <= n; i++)
#define per(i, n, a) for (ll i = n; i >= a; i--)
#define dbg(x) cout << #x << "===" << x << endl
using namespace std;
template<class T>void read(T &x){T res=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}while(isdigit(c)){res=(res<<3)+(res<<1)+c-'0';c=getchar();}x=res*f;}
const ll maxn = 5e5 + 10;
const ll mod = 20000311;
ll n,a[maxn],b,sum[maxn],mul[maxn];
ll qpow(ll a, ll p, ll mod){if(p<=0) return 1;ll ans=1;a=a%mod;while(p){if(p&1)ans=(ans*a)%mod;p>>=1;a=(a*a)%mod;}return ans;}
ll dfs(ll x) {
if(a[x]==0) return x*mul[x-1]*qpow(2,n-sum[x],mod)%mod;
return x*mul[x-1]*qpow(2,n-sum[x],mod)%mod+dfs(x+1)%mod;
}
int main() {
ll _s = 1;
// read(_s);
// freopen("testdata.in","r",stdin);
// freopen("testout.out","w",stdout);
for (ll _ = 1; _ <= _s; _++) {
read(n);
rep(i, 1, n) {
read(b);
if (b <= n + 5) a[b]++;
}
rep(i,0,n+5) mul[i]=1;
rep(i, 1, n + 5) {
sum[i] = sum[i - 1] + a[i],
mul[i] = mul[i - 1] * (qpow(2, a[i], mod)-1) % mod;
}
ll ans = 0;
ans = dfs(1);
ans%=mod;
cout << ans << endl;
}
return 0;
}
/*
input:::
output:::
*/