杨辉三角的一些性质和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的个数)*

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
杨辉三角是一个数学问题,可以用Java编程语言来实现。以下是一个简单的Java程序,用于读取和打印杨辉三角: ``` import java.util.Scanner; public class YangHuiTriangle { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入杨辉三角的行数:"); int n = input.nextInt(); int[][] triangle = new int[n][n]; for (int i = 0; i < n; i++) { triangle[i][0] = 1; triangle[i][i] = 1; } for (int i = 2; i < n; i++) { for (int j = 1; j < i; j++) { triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { System.out.print(triangle[i][j] + " "); } System.out.println(); } } } ``` 该程序首先要求用户输入杨辉三角的行数,然后创建一个二维数组来保存杨辉三角。在第一个for循环中,将第一列和对角线上的元素设为1。在第二个for循环中,计算所有其他元素的值。最后,在第三个for循环中,输出杨辉三角的每一行。 如果您想将杨辉三角写入文件,可以使用Java的文件输入/输出操作。以下是一个简单的Java程序,用于将杨辉三角写入文本文件: ``` import java.io.File; import java.io.FileWriter; import java.io.IOException; public class WriteYangHuiTriangleToFile { public static void main(String[] args) { int n = 10; // 生成10行杨辉三角 int[][] triangle = new int[n][n]; for (int i = 0; i < n; i++) { triangle[i][0] = 1; triangle[i][i] = 1; } for (int i = 2; i < n; i++) { for (int j = 1; j < i; j++) { triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]; } } try { File file = new File("yanghui.txt"); FileWriter writer = new FileWriter(file); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { writer.write(triangle[i][j] + " "); } writer.write("\n"); } writer.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 该程序生成10行杨辉三角,然后将其写入名为"yanghui.txt"的文本文件中。程序使用Java的FileWriter类来打开文件并写入数据。注意要处理IOException异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值