山东13年省赛 Aliceand Bob
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
2
2 1
2
3
4
Sample Output
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
再举例说明:
#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int a[];
int t,n,q;
long long p; int main()
{
cin>>t;while(t--)
{
cin>>n;
for(int i=;i<n;i++)
cin>>a[i];
cin>>q;
while(q--)
{
stack<int>s;
cin>>p;
int result=;
int cnt=-; //这里使用了-1,为了方便,因为a数组是从0开始的
int yu;
while(p) //把数化为二进制存到栈中
{
cnt++;
yu=p%;
s.push(yu);
p/=;
}
if(cnt>n-) //当数的二进制位数大于n时,不存在直接输出0
{
cout<<<<endl;
continue;
}
else
{
while(!s.empty())
{
if(s.top()==)
{
result*=a[cnt];//取数相乘
if(result>)
result%=;
}
s.pop();
cnt--;
}
}
cout<<result<<endl;
}
}
return ;
}
山东13年省赛 Aliceand Bob的更多相关文章
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- 山东第一届省赛1001 Phone Number(字典树)
Phone Number Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 We know that if a phone numb ...
- 13年省赛-B题-连通分量
题意:求从1到N是否存在一条路,可以遍历每个节点. 思路:求任意两点之间是否通畅即可: 疑惑:完全暴力,bfs但是TLE,问题在于求连通分量(PS:不会)贴别人代码,先保存着. #include &l ...
- 13年山东省赛 The number of steps(概率dp水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud The number of steps Time Limit: 1 Sec Me ...
- 13年山东省赛 Mountain Subsequences(dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mountain Subsequences Time Limit: 1 Sec ...
- EZ 2018 05 13 NOIP2018 模拟赛(十三)
这次的比赛真心水,考时估分240,然后各种悠闲乱逛 然后测完T1数组开小了炸成40,T2,T3都没开long long,T2炸成20,T3爆0 掉回1600+的深渊,但是还有CJJ dalao比我更惨 ...
- EZ 2018 04 13 NOIP2018 模拟赛(八)
这次的题目都是什么鬼? 玄学乱搞+肉眼看CODE+倒着搜索? 好吧是我ZZ了 链接在此 T1 玄学乱搞 由于考场上写的部分分做法忘记讨论n<=2000时的情况,少得了30pts 很容易得到一个基 ...
- 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 ...
- 2019.03.13 ZJOI2019模拟赛 解题报告
得分: \(55+12+10=77\)(\(T1\)误认为有可二分性,\(T2\)不小心把\(n\)开了\(char\),\(T3\)直接\(puts("0")\)水\(10\)分 ...
随机推荐
- Redis具体解释
redis 学习指南 一.介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.一个高性能的key-value数据库.并提供多种语言的API.说到Key-Val ...
- C#读取Excel文件的简单方法
一.简述 本文讲C#通过第三方库读取Excel的最简单的方法,下文给一个读取行数的例子. 二.依赖 引入nuget.org包如下: <?xml version="1.0" e ...
- 一步步教你轻松学支持向量机SVM算法之案例篇2
一步步教你轻松学支持向量机SVM算法之案例篇2 (白宁超 2018年10月22日10:09:07) 摘要:支持向量机即SVM(Support Vector Machine) ,是一种监督学习算法,属于 ...
- System.IO.File.WriteAllText("log.txt", "dddd");
System.IO.File.WriteAllText("log.txt", "dddd");
- 巧用Openlayers4的Style
原文:https://blog.csdn.net/gisshixisheng/article/details/80149087 概述 非常细化Openlayers4中的StyleFunction,因为 ...
- MySQL server has gone away 的两个最常见的可能性
[背景] 今天测试同学反馈他们docker中的测试库时不时就就报“MySQL server has gone away”,事态之紧急搞的我都有点怕了(像我这么成熟稳重 的DBA怎么有可能怕呢): 第一 ...
- system generator学习笔记【02】
作者:桂. 时间:2018-05-20 23:28:04 链接:https://www.cnblogs.com/xingshansi/p/9059668.html 前言 继续学习sysgen.接触s ...
- 为啥百度、网易、小米都用Python?Python的用途是什么?
Python是一门脚本语言.由于能将其他各种编程语言写的模块粘接在一起,也被称作胶水语言.强大的包容性.强悍的功能和应用的广泛性使其受到越来越多的关注,想起一句老话:你若盛开.蝴蝶自来. 假设你感 ...
- 如何在Mac的Finder中显示/usr,/tmp,/var等隐藏目录
在终端里输入下面命令即可让它们显示出来. defaults write com.apple.finder AppleShowAllFiles -bool true 如果想恢复隐藏,可以用这个命令: d ...
- Android Studio系列-签名打包
本篇博客纪录使用Android Studio对项目进行签名打包,跟Eclipse大同小异,读者朋友注意其中到差别. 第一步 创建签名文件 第二步 填写签名参数 第三步 选择构建类型 第四步 查看生成第 ...