Problem F: Alice and Bob

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

  1. 1
  2. 2
  3. 2 1
  4. 2
  5. 3
  6. 4

Sample Output

  1. 2
  2. 0

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

解题思路:完全靠位运算即可,可记住这个规律。

求多项式相乘展开式中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

再举例说明:

  1. #include <iostream>
  2. #include <stack>
  3. #include <string.h>
  4. using namespace std;
  5. int a[];
  6. int t,n,q;
  7. long long p;
  8.  
  9. int main()
  10. {
  11. cin>>t;while(t--)
  12. {
  13. cin>>n;
  14. for(int i=;i<n;i++)
  15. cin>>a[i];
  16. cin>>q;
  17. while(q--)
  18. {
  19. stack<int>s;
  20. cin>>p;
  21. int result=;
  22. int cnt=-; //这里使用了-1,为了方便,因为a数组是从0开始的
  23. int yu;
  24. while(p) //把数化为二进制存到栈中
  25. {
  26. cnt++;
  27. yu=p%;
  28. s.push(yu);
  29. p/=;
  30. }
  31. if(cnt>n-) //当数的二进制位数大于n时,不存在直接输出0
  32. {
  33. cout<<<<endl;
  34. continue;
  35. }
  36. else
  37. {
  38. while(!s.empty())
  39. {
  40. if(s.top()==)
  41. {
  42. result*=a[cnt];//取数相乘
  43. if(result>)
  44. result%=;
  45. }
  46. s.pop();
  47. cnt--;
  48. }
  49. }
  50. cout<<result<<endl;
  51. }
  52. }
  53. return ;
  54. }

山东13年省赛 Aliceand Bob的更多相关文章

  1. 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec   ...

  2. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  3. 13年省赛-B题-连通分量

    题意:求从1到N是否存在一条路,可以遍历每个节点. 思路:求任意两点之间是否通畅即可: 疑惑:完全暴力,bfs但是TLE,问题在于求连通分量(PS:不会)贴别人代码,先保存着. #include &l ...

  4. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  5. 13年山东省赛 Mountain Subsequences(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mountain Subsequences Time Limit: 1 Sec   ...

  6. EZ 2018 05 13 NOIP2018 模拟赛(十三)

    这次的比赛真心水,考时估分240,然后各种悠闲乱逛 然后测完T1数组开小了炸成40,T2,T3都没开long long,T2炸成20,T3爆0 掉回1600+的深渊,但是还有CJJ dalao比我更惨 ...

  7. EZ 2018 04 13 NOIP2018 模拟赛(八)

    这次的题目都是什么鬼? 玄学乱搞+肉眼看CODE+倒着搜索? 好吧是我ZZ了 链接在此 T1 玄学乱搞 由于考场上写的部分分做法忘记讨论n<=2000时的情况,少得了30pts 很容易得到一个基 ...

  8. HDU 4802 && HDU 4803 贪心,高精 && HDU 4804 轮廓线dp && HDU 4805 计算几何 && HDU 4811 (13南京区域赛现场赛 题目重演A,B,C,D,J)

    A.GPA(HDU4802): 给你一些字符串对应的权重,求加权平均,如果是N,P不计入统计 GPA Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  9. 2019.03.13 ZJOI2019模拟赛 解题报告

    得分: \(55+12+10=77\)(\(T1\)误认为有可二分性,\(T2\)不小心把\(n\)开了\(char\),\(T3\)直接\(puts("0")\)水\(10\)分 ...

随机推荐

  1. mysql alter 添加索引

    1.添加主键索引 ALTER TABLE `table_name` ADD PRIMARY KEY (`column`) 2.添加唯一索引 ALTER TABLE `table_name` ADD U ...

  2. 网络协议学习(2)---IP地址

    一.IPv4地址 IPv4地址为32bit地址,分为5类(ABCDE,这里不讨论特殊用途的D和E类). 通常我们八位一看,写成4个部分,例如:00000000 00000000 00000000 00 ...

  3. Atitit 创业好处 Atitit 为什么我们要创业

    Atitit 创业好处 Atitit 为什么我们要创业 1.1. 提升学历 1 1.2. 提升自己的能力 1 1.3. 拓展视野 站在高层ceo 才能掌握全局.站在产业链高层,才可看到趋势. 1 1. ...

  4. django项目添加utf-8编码支持中文

    代码中出现中文会报错: Non-ASCII character '...' in file ......models.py on line ......., but no encoding decla ...

  5. C语言 · 积分之迷

    标题:积分之迷 小明开了个网上商店,卖风铃.共有3个品牌:A,B,C. 为了促销,每件商品都会返固定的积分. 小明开业第一天收到了三笔订单: 第一笔:3个A + 7个B + 1个C,共返积分:315 ...

  6. Linux DMA Engine framework(3)_dma controller驱动

    http://www.wowotech.net/linux_kenrel/dma_controller_driver.html

  7. 机器学习&深度学习基础(机器学习基础的算法概述及代码)

    参考:机器学习&深度学习算法及代码实现 Python3机器学习 传统机器学习算法 决策树.K邻近算法.支持向量机.朴素贝叶斯.神经网络.Logistic回归算法,聚类等. 一.机器学习算法及代 ...

  8. find ctime 加减n时间范围

    看下atime的时间解释:-atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour ...

  9. 【ML入门系列】(三)监督学习和无监督学习

    概述 在机器学习领域,主要有三类不同的学习方法: 监督学习(Supervised learning) 非监督学习(Unsupervised learning) 半监督学习(Semi-supervise ...

  10. 网络I/O模型---同步异步阻塞非阻塞之惑

    网络I/O模型 人多了,就会有问题.web刚出现的时候,光顾的人很少.近年来网络应用规模逐渐扩大,应用的架构也需要随之改变.C10k的问题,让工程师们需要思考服务的性能与应用的并发能力. 网络应用需要 ...