【ZOJ 2996】(1+x)^n(二项式定理)

Please calculate the coefficient modulo 2 of x^i in (1+x)^n.

Input

For each case, there are two integers n, i (0<=i<=n<=2^31-1)

Output

For each case, print the coefficient modulo 2 of x^i in (1+x)^n on a single line.

Sample Input

3 1
4 2

Sample Output

1
0

题意:

已知n和i,让你判断(1+x)^n中x^i系数的奇偶性。

题解:

由题意易得知是一道二项式展开的题目,先给出二项式定理的相关公式:

由二项式定理可知x^i的系数为C(n,i),然而C(n,i)的奇偶性有一个性质:如果C(n,i)为奇数,(n&i)=i,由此可以简化代码。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i;
    while(cin>>n>>i)
    {
        if((n&i)==i)cout<<1<<endl;
        else cout<<0<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/kannyi/p/10180907.html
ZOJ