I am working on a problem whose solution am unable to understand. I came up with my own solution but that isn't accepted:
N+1 numbers occur sequentially (one at a time) from A0 to AN. Each number can be placed either side of the last sequence. The score at this point would be the product of this number and its neighbor, e.g.: A0.A1.A2 or A2.A0.A1 (A2 could be placed either side of A0.A1 so scores could be A1.A2 or A2.A0; there could also be a possibility of A1.A0 before A2 came up). We need to sum up the all the possible scores in across all the possible combinations; i.e. sum of score of first sequence over N+1 numbers, then sum over some other sequence and so on and finally sum of all these sums.
Here is the logic which was found to be acceptable:
int pwr[0] = 1;
for (int i = 1; i < 100000; i++)
pwr[i] = (pwr[i - 1] << 1) % MOD;
extra = A0 * 2;
sum = 0;
for (int i = 1; i <= N; i++){
Ai = scan();
sum = (sum * 2 + Ai * extra) % MOD; //Mod is a scaling factor
extra = (extra + pwr[i] * Ai) % MOD;
}
could somebody explain how this works?
Here is my logic (solution) to the same problem, but didn't accept:
#include <iostream>
#include <cmath>
int main()
{
int T;
std::cin>>T;
long long int output[T];
for (int i = 0; i < T; ++i)
{
int N;
std::cin>>N;
long long int inp[N+1];
for (int j = 0; j <= N; ++j)
{
std::cin>>inp[j];
}
long long int tot = 0;
for (int j = 0; j < N; ++j)
{
for (int k = j+1; k <= N; ++k)
{
tot += (inp[j] * inp[k] * pow(2,N-k+1));
}
}
long long int num = pow(10,9) + 7;
output[i] = tot % num;
}
for (int i = 0; i < T; ++i)
{
std::cout<<output[i]<<std::endl;
}
return 0;
}