第四届 Alice and Bob

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 theexpansion 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

我们展开多项式,以下面的为例


就此来看我们可以把P化成二进制形式然后对应的来求


#include
#include
#include
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {


        long long n,Q,p,A[55];
        memset(A,0,sizeof(A));
        cin>>n;
        for(int i=0;i             cin>>A[i];
        cin>>Q;
        while(Q--)
        {
            cin>>p;
            int b[10005],c=0,ans=1;
            memset(b,0,sizeof(b));
            while(p!=0)
            {
                b[c++]=p%2;
                p/=2;
            }
            for(int i=c-1;i>=0;i--)
            {
                if(b[i]==1)
                {
                    ans=((ans%2012)*(A[i]%2012))%2012;
                }
            }
            cout<         }
    }
    return 0;
}


转载于:https://www.cnblogs.com/MisdomTianYa/p/6581748.html

你可能感兴趣的:(第四届 Alice and Bob)