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大学生程序设计竞赛

分析

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

示例程序

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <cstring>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int T;
11     scanf("%d",&T);//测试组数
12     while(T--)
13     {
14         int a[55];//存储系数
15         memset(a,0,sizeof(a));//全部初始化为零
16         int n,q;
17         scanf("%d",&n);
18         for(int i=0;i<n;i++)
19             scanf("%d",&a[i]);
20         scanf("%d",&q);//查询次数
21         while(q--)
22         {
23             long long p;
24             scanf("%lld",&p);
25             long long sum=1,i=0;
26             while(p)//化为二进制
27             {
28                 if(p%2==1)
29                     sum*=a[i];
30                 if(sum>2012)
31                     sum%=2012;
32                 i++;
33                 p/=2;
34             }
35             printf("%lld
",sum);
36         }
37     }
38     return 0;
39 }
原文地址:https://www.cnblogs.com/vivider/p/3707901.html