HDU 5496(Beauty of Sequence-DP)

统计一个序列所有非空子序列的Beauty值,
一个序列的Beauty值为unique后的数列的和

算出每个数出现在连续相同元素中第一个时的贡献,求和

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<map>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p]) 
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007LL)
#define mp make_pair
#define fi first
#define se second
#define MAXN (100000+10)
typedef __int64 ll;
int n;
ll a[MAXN];
ll pow2(ll a,ll b)  
{  
    ll c=1;
    static bool a2[MAXN];  
    int n=0;while (b) a2[++n]=b&1,b>>=1;  
    For(i,n)  
    {  
        if (a2[i]) c=c*a%F;  
        a=a*a%F;  
    }  
    return c;  
}  
map<ll,ll> d;
int main()
{
// freopen("c.in","r",stdin);
// freopen(".out","w",stdout);

    int T;cin>>T;

    while(T--) {
        d.clear();
        cin>>n;
        For(i,n) scanf("%I64d",&a[i]);

        ll ans=0;
        ll now = 1;

        For(i,n) {
            if (d.find(a[i])==d.end()) d[a[i]]=0;

            ans+=a[i]*pow2(2,n-i)%F*((now-d[a[i]]+F ) %F )%F;
            ans%=F;

            ll p=pow2(2,i-1);
            d[a[i]]=(d[a[i]] + p )%F; 
            now=(now + p)%F;
        }       
        printf("%I64d\n",ans%F);
    }

    return 0;
}
相关文章
相关标签/搜索