National Contest for Private Universities (NCPU), 2019 E. Generalized Pascal's Triangle

本文主要是介绍National Contest for Private Universities (NCPU), 2019 E. Generalized Pascal's Triangle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

编辑代码
  • 2000ms
  • 262144K

Pascal's triangle is a triangular array in which each number can be calculated by the sum of the two numbers directly above that number as shown in Figure 1. One of its prominent applications is to determine the coefficients which arise in binomial expansion (or binomial theorem), say the coefficients of (x+y)n(x+y)^n(x+y)n. Figure 1 also illustrates how to derive the elements layer by layer and shows that layer kkk gives the coefficients of the expansion of (x+y)k(x+y)^k(x+y)k. We now want to generalize Pascal's triangle in higher dimension and consider the three-dimensional version. This three-dimensional version can be associated with the coefficients of the trinomial expansion, (x+y+z)n(x+y+z)^n(x+y+z)n, and its shape is a tetrahedron as shown in Figure 2(a) instead of a triangle. Figure 2(b)-(e) present the elements on layer 0, 1, 2, 3, and 4 (center) as well as demonstrate the relation between layer iii and layer i+1i+1i+1 (right). The element on layer iii can be derived from two or three elements on layer i−1i-1i1. This is the same as the coefficients in the expansion of trinomial (x+y+z)i(x+y+z)^i(x+y+z)i (on layer iii) that can be calculated from the coefficients of (x+y+z)i−1(x+y+z)^{i-1}(x+y+z)i1(on layer i−1i-1i1) with the sum of two or three numbers. Now, given an integer nnn as the layer number, please list all the elements on layer nnn in a triangular array.

E1.png

Figure 1. Pascal's triangle

E21.png

Figure 2. Three-dimensional version of Pascal's triangle: (a) The shape and (b)-(e) presenting the elements on layer 0,1,2,3,0, 1, 2, 3,0,1,2,3, and 444 (center) as well as demonstrating the relation between layer iii and layer i+1i+1i+1 (right) with associated trinomial expansion

Input Format

The input contains several test cases and is terminated by End-Of-File (EOF). Each test case is an integer nnn.

Output Format

For each test case, the output is like a triangular array and shall be denoted in n+1n+1n+1 lines depending on the input nnn. The first line has one element (as the first coefficient in the expansion of (x+y+z)n(x+y+z)^n(x+y+z)n) and the second line has two elements and etc. The last line has n+1n+1n+1 elements that are the coefficients of the binomial expansion of (x+y)n(x+y)^n(x+y)n. The first column of the output array thus has n+1n+1n+1 elements and those elements are also the coefficients of the binomial expansion of (x+y)n(x+y)^n(x+y)n. In each line of the output, all elements are separated by space key as delimiter.

Technical Specification

0≤n≤200 ≤ n ≤ 200n20

样例输入 复制
2
3
4
样例输出 复制
1
2 2
1 2 1
1
3 3
3 6 3
1 3 3 1
1
4 4
6 12 6
4 12 12 4
1 4 6 4 1

杨辉三角变形,杨辉三角是从上往下推这个是每个点也对上面有影响。数据量也不是很大直接滚动数组暴力了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 0x3f3f3f3f;
int i,j,k;
int n;
const int N=21;
ll ans[N]= {1, 1};
long long v[2][60][60];
int main()
{while(~scanf("%d", &n)){memset(v,0,sizeof(v));v[0][1][1]=1;for(int k=1;k<=n;k++)for(int i=1;i<=k+1;i++){for(int j=1;j<=i;j++){v[k%2][i][j]=v[(k+1)%2][i][j]+v[(k+1)%2][i-1][j]+v[(k+1)%2][i-1][j-1];}}for(int i=1;i<=n+1;i++){for(int j=1;j<=i;j++){cout<<v[(n)%2][i][j];if(j<i)cout<<' ';}cout<<endl;}}return 0;
}

这篇关于National Contest for Private Universities (NCPU), 2019 E. Generalized Pascal's Triangle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137289

相关文章

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码

2014 Multi-University Training Contest 8小记

1002 计算几何 最大的速度才可能拥有无限的面积。 最大的速度的点 求凸包, 凸包上的点( 注意不是端点 ) 才拥有无限的面积 注意 :  凸包上如果有重点则不满足。 另外最大的速度为0也不行的。 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct poin

2014 Multi-University Training Contest 7小记

1003   数学 , 先暴力再解方程。 在b进制下是个2 , 3 位数的 大概是10000进制以上 。这部分解方程 2-10000 直接暴力 typedef long long LL ;LL n ;int ok(int b){LL m = n ;int c ;while(m){c = m % b ;if(c == 3 || c == 4 || c == 5 ||

2014 Multi-University Training Contest 6小记

1003  贪心 对于111...10....000 这样的序列,  a 为1的个数,b为0的个数,易得当 x= a / (a + b) 时 f最小。 讲串分成若干段  1..10..0   ,  1..10..0 ,  要满足x非递减 。  对于 xi > xi+1  这样的合并 即可。 const int maxn = 100008 ;struct Node{int

AtCoder Beginner Contest 370 Solution

A void solve() {int a, b;qr(a, b);if(a + b != 1) cout << "Invalid\n";else Yes(a);} B 模拟 void solve() {qr(n);int x = 1;FOR(i, n) FOR(j, i) qr(a[i][j]);FOR(i, n) x = x >= i ? a[x][i]: a[i][x];pr2(

leetCode#119. Pascal's Triangle II

Description Given an index k, return the kth row of the Pascal’s triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Code

CF Bayan 2015 Contest Warm Up B.(dfs+暴力)

B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/475/probl

CF Bayan 2015 Contest Warm Up A.(模拟+预处理)

A. Bayan Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/475/problem/A The fi

2019学习计划

工作三年了,第一年感觉是荒废的,第二年开始学习python,第三年开始自动化 感觉自己会的东西比较少,而且不够深入,流于表面 现制定一下今年大概的学习计划 需持续巩固加强:python、ui自动化、接口自动化、sql等 代码量需提升,敲的不够(重点) 学习: 1.移动端测试,appium等 2.前端知识系统整理学习  3.性能测试 4.docker入门,环境搭建 5.shell

c++ public、protected 、 private访问修饰符详解

在 C++ 中,访问修饰符用于控制类的成员(数据成员和成员函数)的访问权限。主要的访问修饰符有三个:public、protected 和 private。每种修饰符的访问规则如下: 1. public 定义:public 修饰符表示该成员对所有代码都是可见的,任何对象都可以访问和修改。作用:允许类外部的代码访问这些成员。 class Example {public:int publicVa