读入n,输出
样例输入: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