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?
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://
122 1234
20
/****************************
题意很简单求一个多项式展开式中 次数为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; }
Sdut 2108 Alice and Bob(数学题)(山东省ACM第四届省赛D题),布布扣,bubuko.com
Sdut 2108 Alice and Bob(数学题)(山东省ACM第四届省赛D题)
原文:http://blog.csdn.net/gray_1566/article/details/25226119