J - The Hell Boy
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
use MathJax to parse formulas
Description
Since the problem set was hard, here is an easy task for you to solve.
You are given an array a consisting of n integers, and your task is to calculate the summation of the multiplication of all subsets of array a. (See the note for more clarifications)
A subset of an array a is defined as a set of elements that can be obtained by deleting zero or more elements from the original array a.
Input
The first line contains an integer T, where T is the number of test cases.
The first line of each test case contains an integer n(1 ≤ n ≤ 105), where n is the size of array a.
The second line of each test case contains n integers a1, a2, ..., an(1 ≤ ai ≤ 106), giving the array a.
Output
For each test case, print a single line containing the summation of the multiplication of all subsets of array a. Since this number may be too large, print the answer modulo 109 + 7.
Sample Input
Input
3 3 1 2 3 2 3 5 1 4512
Output
23 23 4512
Hint
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java.
In the first test case, the array a has 6 subsets, and the answer is calculated as follow:
(1) + (2) + (3) + (1 × 2) + (1 × 3) + (2 × 3) + (1 × 2 × 3) = 23
思路:递推
例如 序列长度为2 ,即 A,B 所有子集的乘积的和为 AB+A+B,则序列长度为3的时候,即A,B,C ,所有子集的乘积和为
ABC+AB+BC+AC+A+B+C == C+(AB+A+B)*C+AB+A+B,所以递推关系式为DP[ i ] = a[ i ] + DP[ i - 1 ] * a[ i ] + DP [ i - 1 ];
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
#define mod 1000000007
ll a[100100],dp[100100];
int main()
{
ll T;
cin>>T;
while(T--)
{
ll n;
cin>>n;
dp[0]=0;
for(ll i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
dp[i]=(dp[i-1]+a[i]+dp[i-1]*a[i])%mod;
}
cout<<dp[n]<<endl;
}
return 0;
}