您的位置:首页 > 产品设计 > UI/UE

Hdu 5496 Beauty of Sequence (组合数)

2015-10-12 15:55 344 查看
题目链接:

  Hdu 5496 Beauty of Sequence

题目描述:

  一个整数序列,除去连续的相同数字(保留一个)后,序列的和成为完美序列和。问:一个整数序列的所有子序列的完美序列和?

解题思路:

  考虑位于i位置数字x的贡献值,假设x是子序列中连续相同数字的第一个,那么x对于i后面的数有2(n-i)个贡献值,对前面的数,要么不选取,要么选取结尾不为x的方案数目。

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
const LL maxn = 100010;
const LL mod = 1000000007;
LL a[maxn];
map<LL, LL>M;

int main ()
{
LL t, n;
scanf ("%lld", &t);

while (t --)
{
scanf ("%lld", &n);
for (int i=0; i<n; i++)
scanf ("%lld", &a[i]);

LL sum, cnt, num;
sum = cnt = 0;
M.clear();

for (int i=0; i<n; i++)
{
num = (cnt + 1 - M[a[i]] + mod) % mod;
sum = (2 * sum % mod + num * a[i] % mod) % mod;
M[a[i]] = (M[a[i]] + cnt + 1) % mod;
cnt = (cnt * 2 + 1) % mod;
}

printf ("%lld\n", sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: