poj 3187 三角数问题】的更多相关文章

题意:给你两个数,一个n表示这个三角有多少层,一个sum表示总和 思路: 类似杨辉三角 1 1       1 1      2     1 第n行的第k个数 为 n!/k!(n-k)! 暴力枚举,因为杨辉三角每行的第一个数都是1,所以你需要每行都乘上一个系数 排列系数 解决问题的代码: #include <iostream> #include <cstdio> #include <string.h> #include <algorithm> using n…
POJ 3187 给定N值,从而确定了数据的范围及长度,暴力枚举数列,接下来类似杨辉三角的递推计算.注permutation从递增有序数列开始枚举,枚举到符合sum值时退出即可 #include <stdio.h> #include <algorithm> using namespace std; int n; int sum; int arr[10][10];//中间结果 int main() { while (scanf("%d %d", &n,&a…
POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻的元素相加得到 4 3 6 然后 7 9 最后得到16,现在给定序列的长度,还有最后的得数,求原始序列(多解则取最小) 思路: 直接枚举即可. 下面是next_permutation版本. #include<cstdio> #include<cstdlib> #include<c…
题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行的数字(由1~N组成).如果有多个解,按字典序升序,输出第一个解. 题解:水题,不多说 AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> usi…
题目链接:http://poj.org/problem?id=3187 解题报告: #include <stdio.h> #include <iostream> #include <algorithm> using namespace std; int main() { int n,sum; scanf("%d%d",&n,&sum); ],b[]; ; i<n; i++) a[i]=i+; do { ;i<n;i++)…
反过来推 题目大意:就是农夫和这只牛又杠上了(怎么老是牛啊,能换点花样吗),给出一行数(从1到N),按杨辉三角的形式叠加到最后,可以得到一个数,现在反过来问你,如果我给你这个数,你找出一开始的序列(可能存在多个序列,输出字典序最小的那个). 这一题首先你要看懂原文的那个1到N是什么意思,就是那一行数只能是1到N,而不是1到10(我一开始犯了这个愚蠢的错误,导致枚举到风扇呼呼的转),如果是这样给你,那么这道题就很简单啦,就直接是用next_permutation枚举所有的序列就可以了,然后找出字典…
如果给出一个由1~n组成的序列,我们可以每相邻2个数求和,得到一个新的序列,不断重复,最后得到一个数sum, 现在输入n,sum,要求输出一个这样的排列,如果有多种情况,输出字典序最小的那一个. 刚开始我是直接搜,tle了 然后就开始找最初的序列和最终的和有什么关系 因为最终的和sum一定是等于若干个a[1],若干个a[2],...,若干个a[n]的和 即sum=p1*a1+p2*a2+...+pn*an 所以我们只要求出数组a的系数,n个p即可. 然后发现,和杨辉三角有很大的关系. 如果杨辉三…
题意:已知有N个数分别为1-N,如下图为4个数.相邻两两相加直至只剩下一个数,下图的结果就是16. 3 1 2 4     4 3 6   7 9 16 现在反过来看,告诉你数的个数N和最终结果,问这N个数的初始序列是什么.求出字典序最小的初始序列.上图的初始序列也可以是3 2 1 4,但这不是字典序最小. 分析:这题用全排列的方式非常容易做.首先初始化数组为1-N,然后用STL提供的按字典序生成全排列的函数next_permutation即可枚举全排列.对于每一组数,通过计算可以知道它是否能得…
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N <= ) ) might go like Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from on…
暴力DFS+验证. 验证如果是暴力检验可能复杂度会太高,事实上可以o(1)进行,这个可以o(n*n)dp预处理. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; ; bool flag[maxn]; bool r; int a[maxn]; int n,sum; ][]; bool check(int s) { ; ; }…
第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream> #include <iterator> #include<string> #include<cstring> #include<queue> #include<vector> #include<stack> #include<…
题意:给一个三角形形状的数字,从上到下,要求数字和最大 思路 :dp dp[i+1][j]=max(dp[i+1][j],dp[i][j]+score[i+1][j]) dp[i+1][j+1]=max(dp[i+1][j],dp[i][j]+score[i+1][j+1] 在最后一行进行比较,找到最大值输出 对上面思路的解释: 对于每个位置都是由上面一个位置加当前位置的最大值组成,所以有了上面的递推公式 解决问题的代码: #include <iostream> #include <cs…
思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int n,C[11][11],sum,f[11]; int main(){ scanf("%d%d",&n,&sum); for(int i=1;i<=n;i++)C[i][i]=1,f[i]=i; for(int…
-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然后把相邻的数相加的到新的一行数.重复这一操作直至只剩一个数字.比如下面是N=4时的一种例子 3 1 2 4 4 3 6 7 9 16 在FJ回来之前,奶牛们开始了一个更难的游戏:他们尝试根据最后结果找到开始的序列.这已超过了FJ的思考极限. 写一个程序来帮助FJ吧 Input N和最后的和 Output 满足…
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. Fo…
POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 Ball POJ 3009 Curling 2.0 AOJ 0558 Cheese POJ 3669 Meteor Shower AOJ 0121 Seven Puzzle POJ 2718 Smallest Difference POJ 3187 Backward Digit Sums POJ 3…
A.Lake Counting(POJ 2386) 题意: 由于最近的降雨,农夫约翰田地的各个地方都有水汇聚,用N x M(1 <= N <= 100; 1 <= M <= 100)的矩形表示.每个方格包含水('W')或干燥土地('.').农夫约翰想弄清楚他的田地里形成了多少个池塘.池塘是一组相连的正方形,里面有水,其中一个正方形被认为与八个池塘相邻给定农夫约翰的田野图,确定他有多少个池塘. 思路: 染色法,移动可用(-1,0)类似数组,也可以用for循环,注意循环时递归可能会比在…
Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5495   Accepted: 3184 Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum ad…
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12226   Accepted: 3187 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores arou…
Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798   Special Judge Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets…
Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000…
The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286   Accepted: 8603   Special Judge Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a…
Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the…
Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm…
Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050   Accepted: 10989 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representatio…
Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital le…
Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17898   Accepted: 9197 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t…
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时则按照横坐标从小到大输出. (0 <= x, y <= 32000) 要求输出等级0到n-1之间各等级的星星个数. 分析: 这道题不难想到n平方的算法,即从纵坐标最小的开始搜,每次找它前面横坐标的值比它小的点的个数,两个for循环搞定,但是会超时. 所以需要用一些数据结构去优化,主要是优化找 横坐…
poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23507   Accepted: 11012 Description The Head Elder of the tropical island of Lagrishan has a problem. A b…
Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9567   Accepted: 3888 Description On an N × N chessboard with a non-negative number in each grid, Kaka starts his matrix travels with SUM = 0. For each travel, Kaka mo…