HDU 5496 Beauty of Sequence

Source: Internet
Author: User


Topic Links:


Beauty of Sequenceproblem descriptionsequence is beautiful and the Beauty of a integer sequence is defined as Follows:re Moves all and 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 is given a sequenceAOfNIntegers{A1,a2,.. . ,an} . You need find the summation of the beauty of the sub-sequence ofA. As the answer may very large, print it modulo9+7 .

Note:in Mathematics, a sub-sequence is a sequence so can be derived from another sequence by deleting some elements wit Hout changing the order of the remaining elements. For example{1,3,2} is a sub-sequence of {1,4,3,5,2,1} .Inputthere is multiple test cases. The first line of input contains an integerT, indicating the number of test cases. For each test case:

The first line contains an integerN (1≤n≤5) , indicating the size of the sequence. The following line containsNIntegersa1,a2,.. . ,an , denoting the sequence(1≤ai≤9) .

The sum of values n for all the test cases does not exceed 2000000. Outputfor each test case, print the answer modulo9+7 in a.
Sample Input351 2 3 4 541 2 1 353 3 2 1 2 sample Output24054144

Test Instructions Analysis:

  The title is to ask for the sum of all the sub-orders, and the number of contiguous and equal numbers in a subsequence is not repeated (equivalent to a number).


When you can't think of a problem, you can think in a different way.

First directly want to statistical results, but the obvious statistics is astronomical, so that there is no law, and did not want to come out, so I want to think from the opposite side of the question, since can not directly sum, then can not go to beg each point to the last ans contribution it. A little try, found feasible.

Another problem is that the adjacent same points in a subsequence are considered only once. You can choose to only calculate the contribution value of the first point in such a subsequence. That is, considering the contribution value of a point, just consider all the sub-sequences that contain it and have no point equal to it in front of it, we can find the number of such subsequence in a different angle, count all the sub-sequences that contain the modified node, and subtract the sub-sequences that do not meet the criteria (see code comment)

AC Code:

1#include <iostream>2#include <cstdio>3#include <map>4 using namespacestd;5typedefLong LongLL;6 7 Const intMAXN = 1e5 +Ten;8 Const intMoD = 1e9 +7;9 Ten intA[MAXN]; One intN; A  - intBIN[MAXN]; -map<int,int>Mymap; the  - voidtable () { -bin[0] =1; -      for(inti =1; i < MAXN; i++) Bin[i] = bin[i-1] *2%MoD; + } -  + voidinit () { A mymap.clear (); at } -  - intMain () { -     intTC; - table (); -scanf"%d", &TC); in      while(tc--) { - init (); toscanf"%d", &n); +LL ans =0; -          for(inti =1; I <= N; i++) { thescanf"%d", A +i); *             //Mymap[a[i]] indicates the number of all sub-sequences ending with a[i] before I $             //Bin[n-1] denotes all sub-sequences that contain I, while Mymap[a[i]]*bin[n-i] represents a sub-sequence that does not meet the criteria. Panax NotoginsengLL tmp = ((Bin[n-1]-(LL) bin[n-i] * Mymap[a[i]])%mod +mod)%MoD; -Ans = (ans + a[i] * tmp)%MoD; theMymap[a[i]] + = bin[i-1]; +Mymap[a[i]]%=MoD; A         } theprintf"%lld\n", ans); +     } -     return 0; $}
View Code

HDU 5496 Beauty of Sequence

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: