今天下午的测试题,求C(n,k) 的奇偶性,拼死没想出来,丢了100分啊qwq
在网上扒题,发现Soj上有
其实很简单,因为C(n,k)=n! / (k! * (n-k)!),所以只需要找到 n、k、n-k中质因数2的个数就行了
当然有更好的数学方法,参见:C(n,k)奇偶性
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define exp 1e-7
using namespace std;
int T,n,k;
inline int log2(int x)
{
return (int)(log(x)/log(2)+exp);
}
int cnt(int x)
{
int cnt=0;
for (int i=1;i<=log2(x);i++)
{
cnt+=x/(1<<i);
}
return cnt;
}
int main()
{
while (1)
{
scanf("%d%d",&n,&k);
if (n==0 && k==0) break;
int zi=cnt(n);
int mu=cnt(k)+cnt(n-k);
if (zi==mu) printf("1\n");
else printf("0\n");
}
return 0;
}