Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

Alice and Bob

Time Limit: 1000ms   Memory limit: 65536K

题目描述

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?

输入

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

输出

For each question of each test case, please output the answer module 2012.

示例输入

1
2
2 1
2
3
4

示例输出

2
0

提示

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

来源

2013年山东省第四届ACM大学生程序设计竞赛

分析

受到公式的影响,刚开始当成母函数做的,母函数都写好了,发现开不了最大的数组。郁闷啊!!!后来用一片面包换来的情报,与二进制有关。
恍然大悟,大难题秒变水题。
 
 

示例程序

 #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cstring>
using namespace std; int main()
{
int T;
scanf("%d",&T);//测试组数
while(T--)
{
int a[];//存储系数
memset(a,,sizeof(a));//全部初始化为零
int n,q;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&q);//查询次数
while(q--)
{
long long p;
scanf("%lld",&p);
long long sum=,i=;
while(p)//化为二进制
{
if(p%==)
sum*=a[i];
if(sum>)
sum%=;
i++;
p/=;
}
printf("%lld\n",sum);
}
}
return ;
}
上一篇:VUE自学日志02-应用与组件实例


下一篇:通过PowerShell发送TCP请求