hdu-5496 Beauty of Sequence(递推)

题目链接:

Beauty of Sequence

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 813    Accepted Submission(s): 379

Problem Description

Sequence is beautiful and the beauty of an integer sequence is defined as follows: removes all but the first element from every consecutive group of equivalent elements of the sequence (i.e. unique function in C++ STL) and the summation of rest integers is the beauty of the sequence.

Now you are given a sequence A of n integers {a1,a2,...,an}. You need find the summation of the beauty of all the sub-sequence of A. As the answer may be very large, print it modulo 109+7.

Note: In mathematics, a sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example {1,3,2} is a sub-sequence of {1,4,3,5,2,1}.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤105), indicating the size of the sequence. The following line contains n integers a1,a2,...,an, denoting the sequence (1≤ai≤109).

The sum of values n for all the test cases does not exceed 2000000.

Output

For each test case, print the answer modulo 109+7 in a single line.

Sample Input

3

5

1 2 3 4 5

4

1 2 1 3

5

3 3 2 1 2

Sample Output

240

54

144

题意:

一个数列的美丽值为这个数列合并相邻的且相同的数后这个序列的和,现在给一个序列,求所有子序列的美丽值得和;

思路:

套路题,dp[i]表示以第i个为结尾的所有的序列的美丽值得和,那么就可以递推了

if a[j]==a[i],dp[i]+=dp[j];

else dp[i]+=dp[j]+num[j]*a[i];其中j属于[1,i-1],num[j]表示以j结尾的序列的个数;

这样会超时啊,用前缀和优化,然后开个map记录每个值结尾的序列的个数;

AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
const LL mod=1e9+7;
LL dp[maxn],num,sum[maxn];
map<int,LL>mp;
int n,a[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        mp.clear();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        sum[1]=a[1];num=1;mp[a[1]]=1;
        for(int i=2;i<=n;i++)
        {
            dp[i]=(a[i]+sum[i-1])%mod;
            dp[i]=(dp[i]+(num-mp[a[i]]+mod)%mod*a[i])%mod;
            mp[a[i]]=(mp[a[i]]+num+1)%mod;
            num=(num*2+1)%mod;
            sum[i]=(sum[i-1]+dp[i])%mod;
        }
        printf("%lld\n",sum[n]);
    }
    return 0;
}

  

时间: 2024-10-20 08:19:27

hdu-5496 Beauty of Sequence(递推)的相关文章

HDU 5496 Beauty of Sequence

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5496 Beauty of Sequence Problem Description Sequence is beautiful and the beauty of an integer sequence is defined as follows: removes all but the first element from every consecutive group of equivalen

HDU 5496——Beauty of Sequence——————【考虑局部】

Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 384    Accepted Submission(s): 168 Problem Description Sequence is beautiful and the beauty of an integer sequence is defined a

hdu 4869 Turn the pokers(递推&amp;组合数学&amp;逆元)

Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1279    Accepted Submission(s): 466 Problem Description During summer vacation,Alice stay at home for a long time, with nothing t

hdu 1284 钱币兑换问题 (递推 || DP || 母函数)

钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5069    Accepted Submission(s): 2868 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有一个正整数N,N小于32768. Outpu

HDU Tickets(简单的dp递推)

Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 972    Accepted Submission(s): 495 Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However,

hdu 2050 折线分割平面 (递推)

折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 15709    Accepted Submission(s): 10836 Problem Description 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面

HDU 3485 Count 101(递推)

C - Count 101 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3485 Description You know YaoYao is fond of his chains. He has a lot of chains and each chain has n diamonds on it. There are two ki

HDU 3899 树形DP||树上递推

JLUCPC 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3899 Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1375    Accepted Submission(s): 413 Problem Description Dr. Skywind and Dr. Walkonclo

HDU 2046 骨牌铺方格(递推)

骨牌铺方格 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42774    Accepted Submission(s): 20735 Problem Description 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数. 例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图: Inpu