Alice and Bob(二进制&&找规律)

Alice and Bob

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem 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

122 1234

Sample Output

20

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

题目意思:

给定(a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1)

这个式子的系数a,展开这个式子后,求x^q的系数。


先看下面这张表(q在题目中是x的指数)

q   二进制      x的指数

1  1         (20)

2  10       (21)

3  11       (2+20)

4  100     (22)

5  101     (2+20)

6  110     (2+21)

7  111     (2+21+20)

8  1000   (23)

9  1001   (23+20)


发现了什么规律?

对了,就是把指数转换成二进制数,再把二进制数中所有“1”的权值都加起来!!


举个栗子:

q=3时,x^3的系数=a[1]*a[0];

q=6时,x^6的系数=a[2]*a[1];

q=7时,x^7的系数=a[2]*a[1]*a[0]

q=10时,x^10的系数=a[3]*a[1]


这些得到的系数就是题目要求的答案呀~


代码:

#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    int n,a[105],q;
    long long p;
    while(t--)
    {
        scanf("%d",&n);
        for(int i = 0 ; i < n ;i++)
            scanf("%d",&a[i]);
        scanf("%d",&q);
        while(q--)
        {
            long long  ans = 1;
            int res =0;
            scanf("%lld",&p);
            while(p > 0)
            {
                if(res>=n)
                {
                    ans = 0;
                    break;
                }
                if(p&1)
                {
                    ans = (ans * a[res])%2012;
                }
                p = p >> 1;
                res++;

            }
            cout << ans << endl;
        }

    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/Xuedan_blog/article/details/80180360