立志用最少的代码做最高效的表达
Alice and Bob like playing games very much.Today, they introduce a new game.
There is a polynomial like this: (a0x(20)+1) * (a1 * x(21)+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
解析
题意:输入 a 1 − a n a1-an a1−an,输入 p p p,问输出 x p x^p xp次方前面的系数是多少
解析:不难看出是一道规律推导题。
写出前三项相乘的结果:
(
a
0
∗
x
+
1
)
∗
(
a
1
∗
x
2
+
1
)
∗
(
a
2
∗
x
4
+
1
)
=
a
0
a
1
a
2
∗
x
7
+
a
1
a
2
∗
x
6
+
a
0
a
2
∗
x
5
+
a
2
∗
x
4
+
.
.
.
(a_0*x+1)*(a_1*x^2+1)*(a_2*x^4+1)=a_0a_1a_2*x^7+a_1a_2*x^6+a_0a_2*x^5+a_2*x^4+...
(a0∗x+1)∗(a1∗x2+1)∗(a2∗x4+1)=a0a1a2∗x7+a1a2∗x6+a0a2∗x5+a2∗x4+...
进而推导出: x p x^p xp的系数,就是按二进制位数取1的位子上相乘的值。
#include<bits/stdc++.h>
using namespace std;
typedef long long gg;
gg a[100];
int main() {
int T; cin >> T; while(T--) {
memset(a, 0, sizeof(a));
int n; cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
int q; cin >> q;
for(int i = 0; i < q; i++) {
gg sum = 1, k = 0; //存放结果
gg p; cin >> p;
while(p) {
if(p%2 == 1)
sum = (sum*a[k])%2012;
k++;
p /= 2;
}
cout << sum << '\n';
}
}
return 0;
}