UVALive 6176 Faulhaber's Triangle

题目链接 http://acm.sdibt.edu.cn/vjudge/ojFiles/uvalive/pdf/61/6177.pdf

题意是  给定一个数n,代表着一共有n个人,且他们的身高从1到n。 要求让这n个人站成一行,使得身高的排列呈波浪形,比如低高低或者高低高。

注意:n = 1 ,  ans = 1;

     n = 2 ,  ans = 2;

动态规划。

解题思路: 每次新加入的点k,可以看成将之前的序列分成前后两部分,并且因为 k是最大的,所以要求k前面数的趋势应该是高低,k后面的趋势应该是底高。这样加入k后的排列数,就是前后可行排列方法数的

乘积。 枚举k插入的位置i,以及乘上c[k][i],表示从k个数里面取i个数的取法。

那么怎么计算前后可行排列的方法数呢? 经过推导后,可以证明,前面和后面的方法数是相同的,所以假设用dp[n][0]表示前n个数中以高低为结尾的方法数,dp[n][1]表示前n个数中以底高为开始的方法数,ans[n]表示n个人的时候的方法数,可得 dp[n][0] = dp[n][1] = ans[n] / 2;

代码:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<string.h>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<stack>
 9 #include<deque>
10 #include<map>
11 #include<iostream>
12 using namespace std;
13 typedef long long  LL;
14 const double pi=acos(-1.0);
15 const double e=exp(1);
16 const int N = 100009;
17
18 LL a[25];
19 LL c[25][25];
20 LL sta[25][3],ans[25];
21
22 void init_c()
23 {
24     LL i,p,j;
25     LL x = 1;
26     for(i = 1; i <= 20; i++)
27     {
28         x *= i;
29         a[i] = x;
30     }
31     a[0] = 1;
32
33     for(i = 1; i <= 20; i++)
34     {
35         for(j = 0; j <= i; j++)
36         {
37             c[i][j] = a[i] / a[j] / a[i - j];
38         }
39     }
40 }
41
42
43 void init_tab()
44 {
45     LL i,p,j;
46
47     sta[0][0] = sta[0][1] = 1;  //特判 k 插在第一位和最后一位的情况
48     ans[1] = 1;
49     sta[1][0] = sta[1][1] = 1;  //特判 n = 1时,既可以看成是开始为底高的方法数也可以看成是高低的方法数。
50
51     for(i = 2; i <= 20; i++)
52     {
53         for(j = 0; j <= i - 1; j++)
54         {
55             ans[i] += sta[j][0] * sta[i - j - 1][1] * c[i - 1][j];
56         }
57         sta[i][0] = sta[i][1] = ans[i] / 2;
58     }
59 }
60
61 int main()
62 {
63     LL i,p,j,n,t;
64     LL w;
65
66     init_c();
67     init_tab();
68
69     scanf("%lld",&t);
70     while(t--)
71     {
72         scanf("%lld%lld",&w,&n);
73         printf("%lld %lld\n",w,ans[n]);
74
75     }
76     return 0;
77 }

UVALive 6176 Faulhaber's Triangle

原文地址:https://www.cnblogs.com/daybreaking/p/10625268.html

时间: 2024-08-13 22:28:45

UVALive 6176 Faulhaber's Triangle的相关文章

Contest1539 - 2019年我能变强组队训练赛第十一场

Greater New York 2012 Hailstone HOTPO #pragma GCC optimize(3,"Ofast","inline") #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n; int T; cin>>T; while(T--) { ll k; scanf("%lld %lld&q

LA UVaLive 7371 Triangle (水题,判矩形)

题意:给定两个三角形,问你能不能拼成矩形. 析:很明显,要想是矩形,必须是四个角是直角,那么三角形必须是直角三角形,然后就是只能斜边相对,然后呢?就没了. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <functional> #include <cstdlib> #inc

UVALive - 8295 Triangle to Hexagon

题意:就是求里面的六边形的每条边的距离 思路:直接求就好了(把题目标号的顺序读反了,保佑队友不杀之恩) 代码: #include <set> #include <map> #include <queue> #include <stack> #include <math.h> #include <vector> #include <string> #include <stdio.h> #include <s

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

Lab 1: Write a java program for the triangle problem and test the program with Junit.

Tasks: 1. Install Junit(4.12), Hamcrest(1.3) with Eclipse 将两个jar包添加到工程中 2. Install Eclemma with Eclipse 3. Write a java program for the triangle problem and test the program with Junit. [Description of triangle problem]Function triangle takes three i

Solution to Triangle by Codility

question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one hand, there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A i

LeetCode (13) Pascal&#39;s Triangle (杨辉三角 )

题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return 从第三行开始,每行除了最左边和最右边两个数为1,其他数字都是上一行中相邻两个数字之和.根据上述规则可以写出下面的代码: class Solution { public: vector<vector<int> > generateRow1() { vector<in

UVA - 11437 - Triangle Fun (计算几何~)

UVA - 11437 Triangle Fun Time Limit: 1000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem A Triangle Fun Input: Standard Input Output: Standard Output In the picture below you can see a triangle ABC. Point D, E

POJ 1163 The Triangle

题目链接:http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39022   Accepted: 23430 Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a program that calculat