牛客竞赛 生涯回忆录

原题:https://ac.nowcoder.com/acm/contest/9033/C

题解:在子集中中找到,最小的正整数x使得 集合所包含的值中,没有权值ai使得ai=x。对于一个长度为n的序列ai,x最大为n+1,最小为1,不妨枚举x,考虑如何计算子集个数令cnt[i]为[1,x-1]中每个i出现的个数,那么选出一个连续的子集个数为\prod (2^{cnt[i]}-1),对于大于x的子集为2^{n-\sum_{i=1}^{x} cnt[i]}很显然,复杂度是O(n)的。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e5+10;
const int MOD=20000311; 
int a[N],cnt[N];
int n; 
ll pow_mod(ll a,int b){
	ll ans=1;
	while(b){
		if(b&1) ans=ans*a%MOD;
		b>>=1;a=a*a%MOD;
	}
	return ans;
} 

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=n;i++){
		if(a[i]<=n) cnt[a[i]]++;
	}
	int tot=n;
	ll ans=0;
	ll pre=1;
	for(int i=1;i<=n+1;i++){
		tot-=cnt[i];
		ans+=pre*(pow_mod(2,tot))%MOD*i%MOD;
		ans%=MOD;
		pre*=(pow_mod(2,cnt[i])-1)%MOD; pre%=MOD; 
	
	}
	printf("%lld\n",ans);
	
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值