Sdut 2108 Alice and Bob(数学题)(山东省ACM第四届省赛D题)

题目地址:sdut 2608 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 http://

输出

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

/****************************

题意很简单求一个多项式展开式中 次数为p的项 的系数

算是一道数学题吧,小锐锐觉得是找规律的,模拟赛之后再看这道题,我觉得可以这样理解:

多项式是这样的一个形式(未展开时):

(a0x+1)+(a1x2+1)+ (a2x4  +1)+ (a3x8 + +1)……+ (an-1x2^(n-1) +1) + (anx2^n +1)

这样的项一共n+1项,求展开式中 次数为p的项的系数, 我们可以理解为从上述式子中抽取不定项,使得其次幂之和为p,然后系数呢,就是对应抽取的项的乘积,

然后就是小锐锐说的了,二进制了   

举个例子:比如说3,二进制表示为011,则他就是a0*a1 = 2*1 = 2.

**************************/

Code:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long ll;
int main()
{
    int  t,n,i,j,q,num[100],ans;
    ll p;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(num,0,sizeof(num));
        for(i = 0;i<n;i++)
            scanf("%d",&num[i]);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%lld",&p);  // 这点要注意,一定要是 long long ,不能写 __int64 ,__int64是MFC的,不是ANSC C标准的,貌似sdut 不支持__int64
            j = 0;ans = 1;
            while(p>0)
            {
                if(j>n)
                {
                    ans = 0;break;
                }
                if(p%2)
                    ans*=num[j];
                j++;p/=2;
                ans=ans%2012;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值