• POJ 3187 Backward Digit Sums


    Backward Digit Sums
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6338   Accepted: 3667

    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.

    Source

     
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <map>
     4 #include <vector>
     5 #include <functional>
     6 #include <string>
     7 #include <cstring>
     8 #include <queue>
     9 #include <set>
    10 #include <cmath>
    11 #include <cstdio>
    12 using namespace std;
    13 #define IOS ios_base::sync_with_stdio(false)
    14 #define TIE std::cin.tie(0)
    15 #define MIN2(a,b) (a<b?a:b)
    16 #define MIN3(a,b) (a<b?(a<c?a:c):(b<c?b:c))
    17 #define MAX2(a,b) (a>b?a:b)
    18 #define MAX3(a,b,c)  (a>b?(a>c?a:c):(b>c?b:c))
    19 typedef long long LL;
    20 typedef unsigned long long ULL;
    21 const int INF = 0x3f3f3f3f;
    22 const double PI = 4.0*atan(1.0);
    23 
    24 int n, m, a[15],b[15];
    25 void solve()
    26 {
    27     for (int i = 1; i <= n; i++)
    28         a[i] = i;
    29     do{
    30         for (int i = 1; i <= n; i++)
    31             b[i] = a[i];
    32         for (int i = n-1; i > 0; i--)
    33             for (int j = 1; j <= i; j++)
    34                 b[j] = b[j] + b[j + 1];
    35         if (b[1] == m){
    36             for (int i = 1; i < n; i++)
    37                 printf("%d ", a[i]);
    38             printf("%d
    ", a[n]);
    39             return;
    40         }
    41     } while (next_permutation(a + 1, a + n + 1));
    42 }
    43 int main()
    44 {
    45     while (scanf("%d%d", &n, &m) == 2){
    46         solve();
    47     }
    48 }
  • 相关阅读:
    ObjectiveC 日记①
    C# WPF vs WinForm
    Ext.Net之 GridPanel Excel导出方法实现
    C 温故知新 之 指针:函数指针变量、指针型函数
    C 温故知新 之 指针:基本概念&变量的指针和指向变量的指针
    C 温故知新 之 指针:数组指针、字符串指针、函数指针
    Linq之 推迟查询 VS 立即查询
    Windows 下搭建ObjectiveC 开发环境
    (转) ObjectiveC 日记② 关于self用法
    windows phone 8 开发环境详细图解
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5855450.html
Copyright © 2020-2023  润新知