Problem F:Alice and Bob
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 48 Solved: 18
[ Submit][ Status][ Discuss]
Description
Alice and Bob like playing games very much.Today, they introduce a new game.
There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1). Then Alice ask Bob Q questions. In the expansion of the Polynomial, Given an integer P, please tell the coefficient of the x^P.
Can you help Bob answer these questions?
Input
The first line of the input is a number T, which means the number of the test cases.
For each case, the first line contains a number n, then n numbers a0, a1, .... an-1 followed in the next line. In the third line is a number Q, and then following Q numbers P.
1 <= T <= 20
1 <= n <= 50
0 <= ai <= 100
Q <= 1000
0 <= P <= 1234567898765432
Output
For each question of each test case, please output the answer module 2012.
Sample Input
122 1234
Sample Output
20
HINT
The expansion of the (2*x^(2^0) + 1) * (1*x^(2^1) + 1) is 1 + 2*x^1 + 1*x^2 + 2*x^3
Source
AC CODE:
#include <iostream>
#include <cmath>
using namespace std;
int a[51];
int main()
{
int t,n,m,i;
long long int q,sum;
cin>>t;
while ( t-- )
{
cin>>n;
for(i=0; i<n; ++i)
cin>>a[i];
cin>>m;
while( m-- )
{
cin>>q;
i=0;
sum=1;
while( q!=0 )
{
if( i>=n )
{
sum=0;
break;
}
if( q%2 ) sum*=a[i];
++i;
q/=2;
sum=sum%2012;
}
cout<<sum<<endl;
}
}
return 0;
}