zoukankan      html  css  js  c++  java
  • P1118 [USACO06FEB]数字三角形Backward Digit Su…

    P1118 [USACO06FEB]数字三角形Backward Digit Su…

    题目描述

    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.

    有这么一个游戏:

    写出一个1~N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置。下面是一个例子:

    3 1 2 4

    4 3 6

    7 9 16 最后得到16这样一个数字。

    现在想要倒着玩这样一个游戏,如果知道N,知道最后得到的数字的大小sum,请你求出最初序列a[i],为1~N的一个排列。若答案有多种可能,则输出字典序最小的那一个。

    [color=red]管理员注:本题描述有误,这里字典序指的是1,2,3,4,5,6,7,8,9,10,11,12

    而不是1,10,11,12,2,3,4,5,6,7,8,9[/color]

    输入输出格式

    输入格式:

     

    两个正整数n,sum。

     

    输出格式:

     

    输出包括1行,为字典序最小的那个答案。

    当无解的时候,请什么也不输出。(好奇葩啊)

     

    输入输出样例

    输入样例#1:
    4 16
    输出样例#1:
    3 1 2 4

    说明

    对于40%的数据,n≤7;

    对于80%的数据,n≤10;

    对于100%的数据,n≤12,sum≤12345。

    对于字典序最小的 排列 有一个神奇的性质 

    对于这个排列 AA2 ....A

    我们可以发现 A1*C(n-1,0)+A2*C(n-1,1)+....+An*C(n-1,n-1)==sum // sum即为题目中给的和

    看样例 3*1+1*3+2*3+1*4==16 

    数据n<=12  所以我们可以DFS枚举全排列 进行判断 

    当然 还要加个剪枝

     1 #include <cctype>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 
     5 const int MAXN=110;
     6 
     7 int n,sum;
     8 
     9 int C[MAXN][MAXN],ans[MAXN];
    10 
    11 bool vis[MAXN];
    12 
    13 inline void read(int&x) {
    14     int f=1;register char c=getchar();
    15     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
    16     for(;isdigit(c);x=x*10+c-48,c=getchar());
    17     x=x*f;
    18 }
    19 
    20 inline void DFS(int num,int tot) {
    21     if(tot>sum) return;
    22     if(num>n) {
    23         if(sum==tot) {
    24             for(int i=1;i<=n;++i) printf("%d ",ans[i]);
    25             exit(0);
    26         }
    27     }
    28     for(int i=1;i<=n;++i) {
    29         if(!vis[i]) {
    30             vis[i]=true;
    31             ans[num]=i;
    32             DFS(num+1,tot+C[n][num]*i);
    33             vis[i]=false;
    34         }
    35     }
    36     
    37 }
    38 
    39 int hh() {
    40     read(n);read(sum);
    41     C[0][0]=1;
    42     for(int i=1;i<=n;++i) 
    43       for(int j=1;j<=i;++j)
    44         C[i][j]=C[i-1][j-1]+C[i-1][j];
    45     DFS(1,0);
    46     return 0;
    47 }
    48 
    49 int sb=hh();
    50 int main(int argc,char**argv) {;}
    代码

     

     

  • 相关阅读:
    杭电 Problem
    杭电Problem 5053 the sum of cube 【数学公式】
    杭电 Problem 2089 不要62 【打表】
    杭电 Problem 4548 美素数【打表】
    杭电 Problem 2008 分拆素数和 【打表】
    杭电 Problem 1722 Cake 【gcd】
    杭电 Problem 2187 悼念512汶川大地震遇难同胞——老人是真饿了【贪心】
    杭电Problem 1872 稳定排序
    杭电 Problem 1753 大明A+B
    东北林业大 564 汉诺塔
  • 原文地址:https://www.cnblogs.com/whistle13326/p/7587965.html
Copyright © 2011-2022 走看看