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

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.
Example Input
122 1234
Example 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

思路:就是二进制而且,打表就行;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
#define LL long long
#define INF 0x7fffffff
#define MAX 200010
#define PI 3.1415926535897932
#define E 2.718281828459045
using namespace std;

long long d[55];
long long p;
int t,n,a[105],q;
long long sum;
int main()
{
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
            d[i]=pow(2,i);
        }
        cin>>q;
        for(int i=0;i<q;i++){
            sum=1;
            cin>>p;
            if(p>=2*d[n-1]){
                cout<<0<<endl;
                continue;
            }
            if(p==0){
                cout<<1<<endl;
                continue;
            }
            for(int j=n-1;j>=0;j--){
                if(p/d[j]==1) sum=(sum*a[j])%2012;
                p=p%d[j];
            }
            cout<<(sum%2012)<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值