ZOJ-2996 (1+x)^n

来源:互联网 发布:炉石传说卡牌数据库 编辑:程序博客网 时间:2024/06/09 15:24
(1+x)^n

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 14 2

Sample Output

10
题目描述:给出n和i,(1+x)^n ,让你求x^i次的系数。
题目思路:很容易想到的是二项式展开,然后求x^i的系数。求C(n,i)的奇偶性偶数输出0,奇数则输出1,由此可以简化代码。C(n,i)的奇偶性有一个性质就是如果为奇数,(n & i) == i。
题目链接:ZOJ 2996
以下是代码:
#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;int main(){long long n,i;while (cin >> n >> i){if ((n & i) == i)cout << 1 << endl;elsecout << 0 << endl;}return 0;}

                                             
0 0