Princess Rupsa saw one of her friends playing a special game. The game goes as follows:
1.N+1 numbers occur sequentially (one at a time) from A0 to AN.
2.You must write the numbers on a sheet of paper, such that A0 is written first. The other numbers are
written according to an inductive rule — after A i-1
numbers have been written in a row, then Ai can be
written at either end of the row. That is, you first write A0, and then A1 can be written on its left or right to
make A0A1 or A1A0, and so on.
3.Ai must be written before writing Aj, for every i < j.
4.For a move in which you write a number Ai (i>0), your points increase by the product of Ai and its neighbour. (Note that for any move it will have only one neighbour as you write the number at an end).
5.Total score of a game is the score you attain after placing all the N + 1 numbers.
Princess Rupsa wants to find out the sum of scores obtained by all possible different gameplays. Two gameplays are different, if after writing down all N + 1 numbers, when we read from left to right, there exists some position i such that the number written at ith position is different for both gameplays.
Since the answer can be very large, print the answer modulo 10^9 + 7.
Input
The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains a single integer N.
The second line contains N + 1 space separated integers denoting A0 to AN.
Output
For each test case, output a single line containing an integer denoting the answer.
Constraints
1 ≤ T ≤ 10
1 ≤ N ≤ 10^5
1 ≤ Ai ≤ 10^9
Time limit=1 sec
I am getting wrong answer for the below code..and my time limit is also exceeding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
long long a[n+1];
long long total;
long long m=1000000000+7;
for(int i=0;i<=n;i++)
{
cin>>a[i];
}
long long s=0;
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
s=s+(((a[i]%m)*(a[j]%m))%m*((int)pow(2,n-j+i-1)%m))%m;
}
}
long long s1=0;
for(int j=1; j<=n;j++)
{
s1=s1+(((a[0]%m)*(a[j]%m))%m*((int)pow(2,n-j)%m))%m;
}
total=2*(s+s1);
cout<<total<<"\n";
}
}
|