Backward Digit Sums(POJ-3187)

本来以为会超时,看来是想多了,63ms,看来对时间复杂度的判断能力还是不行啊。这个题正是next_permutation()函数的用武之地。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
int n,sum,a[100],A[100];
int main() {
    scanf("%d%d",&n,&sum);
    for(int i=0;i<n;i++) a[i] = i+1;
    do {
        for(int i=0;i<n;i++) A[i] = a[i];
        int cur = n;
        bool ok = false;
        if(n==1) { printf("1\n"); return 0; }
        while(true) {
            for(int i=0;i<cur-1;i++) A[i] = A[i]+A[i+1];
            if(cur==2) {
                if(A[0]==sum) ok = true;
                break;
            }
            cur--;
        }
        if(ok) {
            printf("%d",a[0]);
            for(int i=1;i<n;i++) printf(" %d",a[i]);
            printf("\n");
            break;
        }
    } while(next_permutation(a,a+n));
    return 0;
}
时间: 2024-10-15 08:42:46

Backward Digit Sums(POJ-3187)的相关文章

Enum:Backward Digit Sums(POJ 3187)

反过来推 题目大意:就是农夫和这只牛又杠上了(怎么老是牛啊,能换点花样吗),给出一行数(从1到N),按杨辉三角的形式叠加到最后,可以得到一个数,现在反过来问你,如果我给你这个数,你找出一开始的序列(可能存在多个序列,输出字典序最小的那个). 这一题首先你要看懂原文的那个1到N是什么意思,就是那一行数只能是1到N,而不是1到10(我一开始犯了这个愚蠢的错误,导致枚举到风扇呼呼的转),如果是这样给你,那么这道题就很简单啦,就直接是用next_permutation枚举所有的序列就可以了,然后找出字典

POJ 3187 Backward Digit Sums(next_permutation)

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 adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number

【POJ - 3187】Backward Digit Sums(搜索)

-->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 满足

POJ 题目Backward Digit Sums(next_permutation)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4807   Accepted: 2772 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

bzoj1653:Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 318  Solved: 239[Submit][Status][Discuss] Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a cer

BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved: 161[Submit][Status][Discuss] Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a cer

Backward Digit Sums(暴力)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 3280 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

POJ3187 Backward Digit Sums 【暴搜】

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 2575 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

POJ 3187 Backward Digit Sums 题解 《挑战程序设计竞赛》

题目:POJ 3187 思路: 这道题很简单,用next_permutation枚举1~N的所有排列,然后依次相加,判断最后的和是否等于sum,是的话则break,即为字典序最前的. 1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int n; 7 int sum; 8 int mycase[11][11]; 9 10 int main() { 11 cin >> n &

Backward Digit Sums

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