[2013山东ACM省赛] 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大学生程序设计竞赛

解题思路:

求多项式相乘展开式中x的某一指数的系数。

(a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1)给定这个式子,观察x的指数,2^0   2^1  2^2  。。很容易联想到二进制。

后来发现有规律,展开式中没有指数相同的两项,也就是说不能合并公因式。而某一x指数的系数化为二进制以后就可以找到规律了。

比如 求指数为13的系数,把13化为二进制    1  1   0  1    从右到左分别对应  a0   a1   a 2   a3  ,那么所求系数就是  a0  *   a2   *  a 3

再举例说明:

代码:

#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int a[51];
int t,n,q;
long long p;  

int main()
{
    cin>>t;while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        cin>>q;
        while(q--)
        {
            stack<int>s;
            cin>>p;
            int result=1;
            int cnt=-1; //这里使用了-1,为了方便,因为a数组是从0开始的
            int yu;
            while(p) //把数化为二进制存到栈中
            {
                cnt++;
                yu=p%2;
                s.push(yu);
                p/=2;
            }
            if(cnt>n-1) //当数的二进制位数大于n时,不存在直接输出0
            {
                cout<<0<<endl;
                continue;
            }
            else
            {
                while(!s.empty())
                {
                    if(s.top()==1)
                    {
                        result*=a[cnt];//取数相乘
                        if(result>2012)
                        result%=2012;
                    }
                    s.pop();
                    cnt--;
                }
            }
            cout<<result<<endl;
        }
    }
    return 0;
}  

[2013山东ACM省赛] Alice and Bob

时间: 2024-10-14 01:27:07

[2013山东ACM省赛] Alice and Bob的相关文章

[2013山东ACM省赛] The number of steps (概率DP,数学期望)

The number of steps Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Mary stands in a strange maze, the maze looks like a triangle(the first layer have one room,the second layer have two rooms,the third layer have three rooms -). Now she st

[2013山东ACM]省赛 The number of steps (可能DP,数学期望)

The number of steps nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; color:rgb(83,113,197); text-decoration:none; padding-top:0px"> Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 Mary

[2012山东ACM省赛] n a^o7 !(模拟,字符替换)

n a^o7 ! Time Limit: 1000MS Memory limit: 65536K 题目描述 All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my inst

[2012山东ACM省赛] Pick apples (贪心,全然背包,枚举)

Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描写叙述 Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because s

[2012山东ACM省赛] Pick apples (贪心,完全背包,枚举)

Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which only produces three kinds of apples. The number of each kind is infinite. A girl carrying a big bag comes into the yard. She is so surprised because she

[2011山东ACM省赛] Binomial Coeffcients(求组合数)

Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; color:rgb(83,113,197); text-decoration:none; padding-top:0px"> Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 输入

第六届山东ACM省赛总结

省赛总结 省赛打完了,结果在意料之中,教主的判断还是很准的,我们的水平在银牌区中段,运气好可以进金牌区.感觉队伍打的还不错,感谢给力的队友,但感觉我个人还是打的有点乱. 开场我先简单整体的翻了一下习题册,依旧abc一人一题,我开b,李睿易a王成瑞c,b读读感觉是个模拟,尽管感觉不难,但是之前每逢模拟必卡题,就先跳过了,和王成瑞去看c,是个博弈,有点小自信,因为省赛前一段时间做过几道博弈,又看到有一队5min C题1Y,自信的推c了,第一次直接对3取余wa一发,李睿易看出a水,喊王成瑞去写a,我继

[2012山东ACM省赛] Fruit Ninja II (三重积分,椭球体积)

大概去年夏天的时候,在<C和指针>里面一个练习题要求实现一个很简单的不包含打印浮点数功能的printf函数.但是很好奇,于是一直纠结下去,结果就是知道了printf的实现,自己也写了一个简单的.或许是夏天的原因吧,那时候暑假没回去,凌晨四点兴奋到不能睡觉.那时候刚开始写blog.没想整理一下,只是简单的把最重要的实现"工具"贴了一个blog在 http://blog.csdn.net/cinmyheart/article/details/9804189 那时候第一次看lin

第八届山东ACM省赛F题-quadratic equation

这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦 要注意的有以下几点: 1.a=b=c=0时 因为x有无穷种答案,所以不对 2.注意精度问题 3.b^2-4ac<0时也算对 Problem Description With given integers a,b,c, you are asked to judge whether the following statement is true: "For any x, if a?+b?x+c=0, then x is an inte