题目
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5496
题目来源:bc#58 div1 1002
简要题意:一个序列的美丽值为连续相同数去重后的总和,求一个序列所有子序列的美丽值之和。
数据范围: ∑n⩽2000000;1⩽n⩽105;1⩽ai⩽109
题解
首先可以考虑先加上所有的,然后再去去重。
首先不去除重复的话总数是 2n−1∑i=1nai 。
接着来计算重复的个数,这个是这题的难点,不妨令一段相同的中的第一个作出贡献。
考虑当前为 ai ,则对于后面来说不影响 ai 贡献,因为就算重复也只算最前面的,方案 2n−i 。
对前面枚举所有 j,ai=aj ,方案数为 2j−1 ,即 aj 连接 ai 前面任取的方案。
这些贡献多算了,因为此时 ai 不产生贡献,贡献是 aj 产生的。
对于 ai 需要去除的贡献为 ai2n−i∑j<i,aj=ai2j−1
实现
ai 较大需要离散化,扫的同时可以直接处理出 ∑j<i,aj=ai2j−1 。
想通之后代码实现难度不大。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 1E5+5;
const LL MOD = 1e9+7;
int a[N];
int b[N];
LL cnt[N];
int dis[N];
int main()
{
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
LL ans = 0;
for (int i = 0; i < n; i++) {
scanf("%d", a+i);
dis[i] = a[i];
ans += a[i];
}
sort(dis, dis+n);
int len = unique(dis, dis+n)-dis;
ans = ans%MOD*powmod(2, n-1, MOD)%MOD;
for (int i = 0; i < n; i++) {
b[i] = lower_bound(dis, dis+len, a[i])-dis;
ans = (MOD + ans - powmod(2, n-1-i, MOD)*cnt[b[i]]%MOD*a[i]%MOD) % MOD;
cnt[b[i]] = (cnt[b[i]] + powmod(2, i, MOD))%MOD;
}
printf("%I64d\n", ans);
memset(cnt, 0, sizeof cnt);
}
return 0;
}