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 is left. For example,
one instance of the game (when N=4) might go like this:

    3   1   2   4

      4   3   6

        7   9

         16

Behind FJ‘s back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ‘s mental arithmetic capabilities.

Write a program to help FJ play the game and keep up with the cows.

Input

Line 1: Two space-separated integers: N and the final sum.

Output

Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

Sample Input

4 16

Sample Output

3 1 2 4

强大的STL,我比赛的时候是DFS做的。

题意:1~n排列操作的值为sum.

next_permutation的用法:按字典序输出全排列,没有返回false。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
int a[15],f[15];
int n,sum;
int solve()
{
    int s=0;
    for(int i=0;i<n;i++)
        s+=f[i]*a[i];
    return s;
}
int main()
{
   while(~scanf("%d%d",&n,&sum))
   {
       for(int i=0;i<n;i++)
          a[i]=i+1;
       memset(f,0,sizeof(f));
       f[0]=1;
       for(int i=1;i<n;i++)
       {
           for(int j=i;j>=1;j--)
              f[j]+=f[j-1];//杨辉三角
       }
       while(solve()!=sum)
          next_permutation(a,a+n);
       for(int i=0;i<n;i++)
           printf(i==n-1?"%d\n":"%d ",a[i]);
   }
   return 0;
}

DFS版:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
#include<cmath>
typedef long long LL;
using namespace std;
int num[15],a[15];
int visit[15],p[15];
int n,sum,flag;
int solve(int k)
{
     for(int i=1;i<=n;i++)
        p[i]=num[i];
     while(k>1)
     {
         for(int i=1;i<k;i++)
            p[i]=p[i]+p[i+1];
        k--;
     }
     return p[1];
}
void dfs(int k,int s)
{
    if(s==n+1)
    {
        if(solve(n)==sum)
        {
            for(int i=1;i<=n;i++)
                printf(i==n?"%d\n":"%d ",num[i]);
            flag=1;
            return ;
        }
    }
    if(flag)
        return ;
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
        {
            visit[i]=1;
            num[s]=a[i];
            dfs(i,s+1);
            visit[i]=0;
        }
    }
}
int main()
{
    while(~scanf("%d%d",&n,&sum))
    {
        for(int i=1;i<=n;i++)
              a[i]=i;
        memset(visit,0,sizeof(visit));
        memset(num,0,sizeof(num));
        flag=0;
        dfs(1,1);
    }
    return 0;
}

next_permutation:

POJ 1146:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
int main()
{
 int len;
 char s[51];
 while(scanf("%s",s)!=EOF)
 {
  if(s[0]=='#')
   break;
  len=strlen(s);
  if(next_permutation(s,s+len))
   printf("%s\n",s);
  else
   printf("No Successor\n");
 }
 return 0;
}

POJ 1731 :

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
char str[220];

int main()
{
    while(~scanf("%s",str))
    {
        int len=strlen(str);
        sort(str,str+len);
        do{
            printf("%s\n",str);
        }while(next_permutation(str,str+len));
    }
    return 0;
}

POJ 3187 Backward Digit Sums(next_permutation)

时间: 2024-10-27 06:53:03

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

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

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 &

POJ 3187 Backward Digit Sums

http://poj.org/problem?id=3187 穷竭搜索 全排列 然后按规则求和 排列之前先按升序排序 这样可以保证第一个和为k的就是符合最小序列的结果 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 int sum(int a[], int n) 8 { 9 int s[11]; 10 for (int i = 0

POJ 3187 Backward Digit Sums (杨辉三角,穷竭搜索,组合数,DFS)

http://poj.org/problem?id=3187 将一行数按杨辉三角的规则计算为一个数,已知最后那个数和三角形的高度,求最初的那行数. 杨辉三角前10行:                   1                                   1   1                               1   2   1                           1   3   3   1                       1  

poj 3187 Backward Digit Sums(穷竭搜索dfs)

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 (dfs,杨辉三角形性质)

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

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

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