杨辉三角的一些性质和lucas定理

前提:每行端点与结尾的数为1.
每个数等于它上方两数之和。
每行数字左右对称,由1开始逐渐变大。
第n行的数字有n项。
第n行数字和为2n-1。
第n行的m个数可表示为 C(n-1,m-1),即为从n-1个不同元素中取m-1个元素的组合数。
第n行的第m个数和第n-m+1个数相等 ,为组合数性质之一。
每个数字等于上一行的左右两个数字之和。可用此性质写出整个杨辉三角。即第n+1行的第i个数等于第n行的第i-1个数和第i个数之和,这也是组合数的性质之一。即 C(n+1,i)=C(n,i)+C(n,i-1)。
(a+b)n的展开式中的各项系数依次对应杨辉三角的第(n+1)行中的每一项。
将第2n+1行第1个数,跟第2n+2行第3个数、第2n+3行第5个数……连成一线,这些数的和是第4n+1个斐波那契数;将第2n行第2个数(n>1),跟第2n-1行第4个数、第2n-2行第6个数……这些数之和是第4n-2个斐波那契数。
将各行数字相排列,可得11的n-1(n为行数)次方:1=11^0; 11=11^1; 121=11^2……当n>5时会不符合这一条性质,此时应把第n行的最右面的数字”1”放在个位,然后把左面的一个数字的个位对齐到十位… …,以此类推,把空位用“0”补齐,然后把所有的数加起来,得到的数正好是11的n-1次方。以n=11为例,第十一行的数为:1,10,45,120,210,252,210,120,45,10,1,结果为 25937424601=1110。

杨辉三角第n行m列元素是?
代码:

int main()
{
    int n,m;
    long long f[105][105];
    cin>>n>>m;
    f[1][1]=1;
    f[n][1]=1;
    f[n][n]=1;
    for(int i=2;i<=50;i++)
        for(int j=1;j<=i;j++)
        {
            f[i][j]=f[i-1][j-1]+f[i-1][j];
        }
    cout<<f[n][m];

   return 0;
}






杨辉三角的打印代码:

void YangHui() {
    memset(Triangle, 0, sizeof(Triangle));
    for (int i=0; i<n; ++i) {
        Triangle[i][0]=1;
        for (int j=1; j<n; ++j) Triangle[i][j]=Triangle[i-1][j-1]+Triangle[i-1][j];
    }
}

Xiao Ming’s Hope
Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2011.11.11 comes. Seeing classmates walking with their girl friends, he coundn’t help running into his classroom, and then opened his maths book preparing to count odd numbers. He looked at his book, then he found a question “C (n,0)+C (n,1)+C (n,2)+…+C (n,n)=?”. Of course, Xiao Ming knew the answer, but he didn’t care about that , What he wanted to know was that how many odd numbers there were? Then he began to count odd numbers. When n is equal to 1, C (1,0)=C (1,1)=1, there are 2 odd numbers. When n is equal to 2, C (2,0)=C (2,2)=1, there are 2 odd numbers…… Suddenly, he found a girl was watching him counting odd numbers. In order to show his gifts on maths, he wrote several big numbers what n would be equal to, but he found it was impossible to finished his tasks, then he sent a piece of information to you, and wanted you a excellent programmer to help him, he really didn’t want to let her down. Can you help him?
Input
Each line contains a integer n(1<=n<=10 8)
Output
A single line with the number of odd numbers of C (n,0),C (n,1),C (n,2)…C (n,n).
Sample Input
1
2
11
Sample Output
2
2
8

题意:求C(n,0),C(n,1),C(n,2)…C(n,n).当中有多少个奇数。
Lucas定理:

A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]…a[0],B=b[n]b[n-1]…b[0]。
则组合数C(A,B)与C(a[n],b[n])C(a[n-1],b[n-1])…*C(a[0],b[0]) modp同余

即:Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)
这里写图片描述
证明:
http://en.wikipedia.org/wiki/Lucas%27_theorem
维基的意思是说在m中里面取n个%p来说,这样考虑:
将m划分成m0个1,m1个p,m2个p^2,……
将n划分成n0个1,n1个p,n2个p^2……
那是不是C(m,n)的总共的取法就是( 在m0个里面取n0个的方案数 * m1个里面取n1个的方案数 * m2个里面取n2个的方案数 ……)
证明就是这样。
**由数论Lucas定理:我们可以写
成二进制的形式观察,比如 n=1001101,m是从000000到1001101的枚举,我们知道在该定理中 C(0,1)=0,因此如果n=1001101的0对应位置的m二进制位为1那么C(n,m) % 2==0,因此m对应n为0的 位置只能填0,而1的位置填0,填1都是1(C(1,0)=C(1,1)=1),不影响结果为奇数,并且保证不会超出n的范围,因此所有的情况即是n中1位置对应m位置0,1的枚举,那么结果很明显就是:2^(n中1的个数)*

猜你喜欢

转载自blog.csdn.net/qq_42825221/article/details/81953577