【Lucas定理】【模板】Day8-A

链接

A

题目描述

给出多组n,m,求C(n,m)的奇偶性

思路

其实就是求 C n m (   m o d     2 ) C_n^m(\bmod\ 2) Cnm(mod 2)
Lucas定理
C n m ≡ C n   m o d   2 m   m o d   2 ∗ C ⌊ n 2 ⌋ ⌊ m 2 ⌋ (   m o d     2 ) C_n^m \equiv C_{n \bmod 2} ^ {m \bmod 2}*C_{\left\lfloor \frac{n}{2} \right\rfloor}^{\left\lfloor \frac{m}{2} \right\rfloor}(\bmod\ 2) CnmCnmod2mmod2C2n2m(mod 2)
递归实现

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int n, m, T;

int C(int x, int y)
{
    
    
	if(x < y) return 0;
	if(y == 0 || x == y) return 1;
	int p = C(x % 2, y % 2) % 2;
	int q = C(x / 2, y / 2) % 2;
	return (p * q) % 2;
}

int read()
{
    
    
	int re = 0;
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	while('0' <= c && c <= '9') {
    
    
		re = re * 10 + c - 48;
		c = getchar();
	}
	return re;
}

void write(int x)
{
    
    
    if (x < 0) {
    
     
		x = -x; 
		putchar('-');
	}
	if (x > 9) write(x / 10);
	putchar(x % 10 + 48);
	return;
}

int main()
{
    
    
	scanf("%d", &T);
	for(int times = 1; times <= T; ++times)
	{
    
    
		n = read(); m = read();
		write((C(n % 2, m % 2) * C(n / 2, m / 2)) % 2);
		putchar('\n');
	}
	
} 

Supongo que te gusta

Origin blog.csdn.net/LTH060226/article/details/119736126
Recomendado
Clasificación