山东13年省赛 Aliceand Bob

Problem F: 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 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

1
2
2 1
2
3
4

Sample Output

2
0

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

解题思路:完全靠位运算即可,可记住这个规律。

求多项式相乘展开式中x的某一指数的系数。

(a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1)给定这个式子,观察x的指数,2^0   2^1  2^2  。。很容易联想到二进制。

 

后来发现有规律,展开式中没有指数相同的两项,也就是说不能合并公因式。而某一x指数的系数化为二进制以后就可以找到规律了。

比如 求指数为13的系数,把13化为二进制    1  1   0  1    从右到左分别对应  a0   a1   a 2   a3  ,那么所求系数就是  a0  *   a2   *  a 3

再举例说明:

 1 #include <iostream>  
 2 #include <stack>  
 3 #include <string.h>  
 4 using namespace std;  
 5 int a[51];  
 6 int t,n,q;  
 7 long long p;  
 8   
 9 int main()  
10 {  
11     cin>>t;while(t--)  
12     {  
13         cin>>n;  
14         for(int i=0;i<n;i++)  
15             cin>>a[i];  
16         cin>>q;  
17         while(q--)  
18         {  
19             stack<int>s;  
20             cin>>p;  
21             int result=1;  
22             int cnt=-1; //这里使用了-1,为了方便,因为a数组是从0开始的 
23             int yu;  
24             while(p) //把数化为二进制存到栈中 
25             {  
26                 cnt++;  
27                 yu=p%2;  
28                 s.push(yu);  
29                 p/=2;  
30             }  
31             if(cnt>n-1) //当数的二进制位数大于n时,不存在直接输出0 
32             {  
33                 cout<<0<<endl;  
34                 continue;  
35             }  
36             else  
37             {  
38                 while(!s.empty())  
39                 {  
40                     if(s.top()==1)  
41                     {  
42                         result*=a[cnt];//取数相乘  
43                         if(result>2012)  
44                         result%=2012;  
45                     }  
46                     s.pop();  
47                     cnt--;  
48                 }  
49             }  
50             cout<<result<<endl;  
51         }  
52     }  
53     return 0;  
54 }  
View Code

 

posted @ 2015-04-09 22:59  一麻袋码的玛侬  阅读(115)  评论(0)    收藏  举报