Problem Solving OJ-2019.03.10

博客探讨了解决在线判题中的一个问题,涉及计算n的奇数二项式系数。通过数学归纳法证明了二项式系数C(n,k)为奇数当且仅当n与k按位与等于k,并利用二进制特性简化计算。此外,还提到了Lucas定理作为另一种可能的解决方案。" 132784890,19671617,使用MATLAB模拟城市增长的元胞自动机模型,"['MATLAB', '算法', '开发语言', '城市规划']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读入n,输出  \sum_{i=1}^n\sum_{j=0}^i(\binom{i}{j}mod 2)

 样例输入:10

样例输出: 36

数据范围: 0<n<10^7

Analysis: We only need to calculate the number of odd binomial coefficients.

Points:

1. C(n,k) is odd if and only if n&k==k(0<=k<=n);

2. Fix one i, we only need to count how many "1"s in its binary form and the odd binomial coefficients in this equivalence class(i) is clearly 2^k;(Use the quick calculation of "1"s in a binary number)

As for the first point, we prove it by using the method of mathematical induction on the level n of the Pascal Triangle.

First we check the base step. When the level is 1,2,3, it is quite obvious that the statement is true.

Then we check the induction step. We assuem that for the (n-1)-th level, the statement is true. That is C(n-1,k) is odd if and only if (n-1)&k==k(0<=k<=n-1). Now we consider C(n,p).

When p=0 or p=n, the statement is apparently true, and next we will talk about the situation where 0<p<n.

Since C(n,p)=C(n-1,p-1)+C(n-1,p), we prove the statement in cases.

(1)When C(n-1,p-1) and C(n-1,p) are both odd numbers, we have to prove that n&p!=p. Given the fact that (n-1)&(p-1)==p-1, (n-1)&p==p, and the fact that the least siginificant bit of p and p-1 must differ(0 or 1), the LSB of n-1 should be 1. And therefore, the LSB of n should be 0. We assume that n&p==p. With the knowledge that (n-1)&p==p, again, since the LSB of n and n-1 must differ, the LSB of p must be 1. LSB(p)==1, LSB(n)==0, n&p!=p, which contradicts with our assumptions. Therefore, n&p!=p.

2. When C(n-1,p-1) and C(n-1,p) are both even numbers, we need to prove that n&p!=p. First we know the fact that (n-1)&(p-1)!=p-1, (n-1)&p!=p. We assume that n&p==p. If LSB(p)==1, then LSB(n)==1 and (n-1)&(p-1)==p-1, which is a contradiction. Therefore n&p!=p.

3.When C(n-1,p-1) is odd and C(n-1,p) is even, we need to prove that n&p==p. First we know the fact that (n-1)&(p-1)==p-1, (n-1)&p!=p. We assume that n&p!=p. The only chance that the assumption would happen us when LSB(p)==1 and LSB(n)==0. Then (n-1)&(p-1)!=p-1 for at least their LSB is different, which is a contradiction. 

4.When C(n-1,p-1) is even and C(n-1,p) is odd, we need to prove that n&p==p. First we know the fact that (n-1)&(p-1)!=p-1, (n-1)&p==p. We assume that n&p!=p. The only chance that the assumption would happen us when LSB(p)==1 and LSB(n)==0. Then LSB(n-1)==1. Since (n-1)&p==p, we know that LSB(p)==1 and LSB(p-1)==0 with all other bits of p-1 the same as p. Therefore (n-1)&(p-1)==p-1, which is a contradiction. Therefore n&p==p.

Conclusion: The statement is true.

 

 

 

Also, this problem can be solved by Lucas' theorem.(Can it?......well.....)

 

I will put it any way and feel free to use that. C(n,m)%p=C(n/p,m/p)*C(n%p,m%p)%p

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值