• poj3187(Backward Digit Sums)


    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

    Hint

    Explanation of the sample: 

    There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.

     

    无脑枚举全排列,竟然都过了,状态总数只有10!

    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const double PI = acos(-1.0);
    int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
    const int maxn = 10000010;
    
    int main()
    {
        int n, sum;
        cin >> n >> sum;
        int s[15];
        for (int i = 0; i < n; ++i)
            s[i] = i + 1;
        do
        {
            int t[15];
            for (int i = 0; i < n; ++i)
                t[i] = s[i];
            int tmp = n;
            while (tmp-- > 1)
                for (int i = 0; i < tmp; ++i)
                    t[i] += t[i+1];
            if (t[0] == sum)
            {
                for (int i = 0; i < n-1; ++i)
                    printf("%d ", s[i]);
                printf("%d
    ", s[n-1]);
                break;
            }
        } while (next_permutation(s, s+n));
        return 0;
    }


     

  • 相关阅读:
    CentOS7-samba文件共享服务
    centos7-vsftpd文件服务器
    linux用户和权限管理
    linux程序安装及包管理
    linux文件查找-find命令
    linux文本编辑器-VIM基本使用方法
    linux文本处理工具及正则表达式
    linux目录结构及文件管理
    linux基本命令及使用方法
    巴什博奕----取完者负的分析
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203987.html
Copyright © 2020-2023  润新知