2013年山東省賽題目 Alice and Bob

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.

示例輸入

122 1234

示例輸出

20

提示

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<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int a[70];
int b[70];

int main()
{
    int i,j,k,q,n;
    long long p;
    int T;
    scanf("%d",&T);
    while(T--)
    {

     scanf("%d",&n);
     memset(a,0,sizeof(a));//很重要
     for(i=0;i<n;i++)
     {
         scanf("%d",&a[i]);
     }
     scanf("%d",&q);
     int na=0;
     int nb=0;
     while(q--)
     {
         scanf("%lld",&p);
         if(p==0)
         {
             printf("1\n");
             continue;

         }
         na=0;
         nb=1;
         while(p)
         {
             b[na]=p%2;
             na++;
             p=p/2;

         }
         /*for(i=0;i<na;i++)
         {
             printf("bb  %d\n",b[i]);
         }*/
         for(i=0;i<na;i++)
         {
            if(b[i])
            {
                nb=nb*a[i]%2012;

            }
         }




       printf("%d\n",nb);



     }


    }
    return 0;
}







發佈了312 篇原創文章 · 獲贊 10 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章