zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    【20171030早】sqli-libs Less7-15 练习
    【20171029中】sqli-libs 注入的过程 less1-4
    【20171028早】ubuntu 16.04 LTS 安装php遇到的问题
    【20171027早】alert(1) to win 第9,10,11,12题
    【20171026早】alert(1) to win
    【20171025晚】alert(1) to win 第五题 正则表达式过滤
    【20171025中】alert(1) to win 脚本渲染自建
    【20171025早】alert(1) to win 练习
    mysql 导入 excel 数据
    我的书单
  • 原文地址:https://www.cnblogs.com/cumulonimbus/p/5855450.html
Copyright © 2011-2022 走看看