zoukankan      html  css  js  c++  java
  • [USACO06FEB]数字三角形

    题目描述

    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。

    一上来,直接打的大爆搜(70)。

     1 #include<cstdio>
     2 int n,m;
     3 int s[13],bf[13];
     4 bool v[13];
     5 void find(int x){
     6     if(x>n){
     7         s[0]=n;
     8         while(s[0]--){
     9             for(int i=1;i<=s[0];i++) s[i]+=s[i+1];
    10         }
    11         if(s[1]==m){
    12             for(int i=1;i<=n;i++) printf("%d ",bf[i]);
    13             n=0;
    14         }
    15         else{
    16             for(int i=1;i<=n;i++) s[i]=bf[i];
    17         }
    18         return;
    19     }
    20     for(int i=1;i<=n;i++) if(!v[i]){s[x]=bf[x]=i;v[i]=1;find(x+1);v[i]=0;}
    21 }
    22 int main(){
    23     scanf("%d%d",&n,&m);
    24     find(1);
    25     return 0;
    26 }
    View Code

    后来,看别人代码,找了一下每一位上对答案的影响。(80)

     1 #include<cstdio>
     2 int n,m;
     3 int s[15],js[15]={0,1};
     4 bool v[15];
     5 void find(int x,int y){
     6     if(x>n){
     7         if(y==m){
     8             for(int i=1;i<=n;i++) printf("%d ",s[i]);
     9             n=0;
    10         }
    11         return;
    12     }
    13     for(int i=1;i<=n;i++)
    14     if(!v[i]){
    15         s[x]=i;
    16         v[i]=1;
    17         find(x+1,y+js[x]*i);
    18         v[i]=0;
    19     }
    20 }
    21 int main(){
    22     scanf("%d%d",&n,&m);
    23     for(int i=1;i<n;i++)
    24     for(int j=i;j>0;j--)
    25     js[j+1]+=js[j];
    26     find(1,0);
    27     return 0;
    28 }
    View Code

    最后又加了一个

    if(y>m) return;

    的剪枝。终于过了。

    代码实现:

     1 #include<cstdio>
     2 int n,m;
     3 int s[15],js[15]={0,1};
     4 bool v[15];
     5 void find(int x,int y){
     6     if(y>m) return;
     7     if(x>n){
     8         if(y==m){
     9             for(int i=1;i<=n;i++) printf("%d ",s[i]);
    10             n=0;
    11         }
    12         return;
    13     }
    14     for(int i=1;i<=n;i++)
    15     if(!v[i]){
    16         s[x]=i;
    17         v[i]=1;
    18         find(x+1,y+js[x]*i);
    19         v[i]=0;
    20     }
    21 }
    22 int main(){
    23     scanf("%d%d",&n,&m);
    24     for(int i=1;i<n;i++)
    25     for(int j=i;j>0;j--)
    26     js[j+1]+=js[j];
    27     find(1,0);
    28     return 0;
    29 }

    。。。我去年八月份就会的题,现在。。。

    题目来源:洛谷

  • 相关阅读:
    MySql主从库配置
    Linux安装MySql5.6.43(亲测)
    Linux安装ffmpeg(亲测)
    Linux JDK安装(亲测)
    Linux磁盘挂载(亲测)
    Finance_CAPM&APT
    Python_QT_量化投资/量化交易 基础入门课
    581. Shortest Unsorted Continuous Subarray
    3. Longest Substring Without Repeating Characters
    239. Sliding Window Maximum
  • 原文地址:https://www.cnblogs.com/J-william/p/6362623.html
Copyright © 2011-2022 走看看