hpu 1194 Sequence


HS(Handsome)的Ocean在纸上写下NN个整数,Ocean把它定义为OO序列。

Ocean认为一个序列的价值的是:序列中不同元素个数。

现在他想知道OO序列中所有子序列的价值之和。


比如说:序列(1,1,2,2)(1,1,2,2)价值为22,因为序列中有1122两个不同元素。
比如序列(1,1,1)(1,1,1),共有77个子序列,(1)(1)(1)(1,1)(1,1)(1,1)(1,1,1)(1)、(1)、(1)、(1,1)、(1,1)、(1,1)、(1,1,1)。价值之和为77

输入

第一行输入一个整数TT,代表有TT组测试数据。
每组数据占两行,第一行输入一个整数NN,代表序列元素个数。
接下来一行输入NN个整数aiai

注:1<=T<=100001<=N<=501<=ai<=101<=T<=10000,1<=N<=50,1<=ai<=10。

输出

对每组测试数据,输出一个结果代表所有子序列价值之和。由于结果会很大,请用longlonglonglong(%lld)。

样例输入

4
3
1 1 1
4
1 1 1 1
4
10 10 10 8
20
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10

样例输出

7
15
22
7864320
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define ll long long
using namespace std;

const int maxn=50+10;

ll c[20];

int a[maxn],b[20];

int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        ll ans=0,t=0,s=0;
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
            b[a[i]]++;//b数组表示某个数出现的次数
        }
        for(int i=1; i<=10; i++)
        {
            if(b[i]!=0)s++;//s表示一共有几个不同的数
        }
        for(int i=1; i<=10; i++)
        {
            if(b[i])
            {
                ll t=pow(2,b[i])-1;
                for(int j=s; j>=1; j--)
                {
                    if(j==1) c[j]+=t;
                    else
                    {
                        c[j]+=c[j-1]*t;
                    }
                }
            }
        }
        for(int i=1; i<=s; i++)
        {
            ans+=i*c[i];
        }
        printf("%lld
",ans);
    }
    return 0;
}















原文地址:https://www.cnblogs.com/nyist-xsk/p/7264891.html