位运算 2013年山东省赛 F Alice and Bob

时间:2023-03-08 22:49:12
位运算 2013年山东省赛 F Alice and Bob

题目传送门

 /*
题意: 求(a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*.......*(an-1 * x^(2^(n-1))+1) 式子中,x的p次方的系数
二进制位运算:p = 2 ^ i + 2 ^ j + 2 ^ k + ...,在二进制表示下就是1的出现
例如:10 的二进制 为1010,10 = 2^3 + 2^1 = 8 + 2,而且每一个二进制数都有相关的a[i],对p移位运算,累计取模就行了
*/
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f; int main(void) //F Alice and Bob
{
//freopen ("F.txt", "r", stdin); int t;
while (scanf ("%d", &t) == )
{
while (t--)
{
int n, a[], q;
long long p;
scanf ("%d", &n);
for (int i=; i<n; ++i)
{
scanf ("%d", &a[i]);
}
scanf ("%d", &q); while (q--)
{
scanf ("%lld", &p);
int tmp = ; int i = ;
while (p)
{
if (i >= n)
{
tmp = ; break;
}
if (p & ) tmp *= a[i];
++i;
p >>= ;
tmp %= ;
}
printf ("%d\n", tmp);
}
}
} return ;
}