您的位置:首页 > 其它

△2013山东省ACM竞赛-Alice and Bob

2014-04-27 19:51 423 查看

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

题目大意:多项式相乘,写几项就发现规律了。

(a0x+1)(a1x^2+1)(a2x^4+1)

=a0a1a2x^7+a1a2x^6+a0a2x^5+a2x^4+a0a1x^3+a1x^2+a0x+1 列出来后发现正好该数化为二进制(不用逆置),如果为1,则相乘

7:a0a1a2 即1+2+4

6:a1a2 即2+4

5:a0a2 即1+4

4:a2 即4

3:a0a1 即1+2

2:a1 即2

1:a0 即1

0:1

可以看出,对应为:

a3 a2 a1 a0

8 4 2 1

#include <iostream>
#include<cstdio>
#include<string.h>
using namespace std;

#define maxn 105

int a[maxn],pos[maxn];

int main()
{
int t,n,i,j,q,ans;
long long p;//因为0 <= p <= 1234567898765432
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));//这个很妙,比如n=3,则p>=8以后都要=0,就靠这一句
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&q);
while(q--)
{
scanf("%lld",&p);
if(p==0)
{
printf("1\n");//别忘了
continue;
}
memset(pos,0,sizeof(pos));
i=0;
while(p)
{
pos[i++]=p%2;//求p转化为二进制
p/=2;
}
ans=1;
for(j=0;j<i;j++)
{
if(pos[j])//如果那一位二进制=1
{
ans=ans*a[j]%2012;//则就是那几位相乘,一边乘一遍取余2012
}
}
printf("%d\n",ans);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: