1653: [Usaco2006 Feb]Backward Digit Sums

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 207  Solved: 161
[Submit][Status][Discuss]

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

OUTPUT DETAILS:

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

HINT

 

Source

Silver

题解:

暴力枚举排列是必须的,那我们看一下能否在O(N)的时间内检验一个排列按题意操作之后和是否为final number

根据直觉每一个数被使用的次数应该有某种关系,从样例来看:

16

7 9

4 3 6

3 1 2 4

他们的次数三角形为:

1

1 1

1 2 1

1 3 3 1

这下应该就明显了吧,下面的数的使用次数等于它们斜上角的两个数的使用之和,这不就是喜闻乐见的杨辉三角吗,组合数搞定。

代码:

待UPD

 #include<cstdio>

 #include<cstdlib>

 #include<cmath>

 #include<cstring>

 #include<algorithm>

 #include<iostream>

 #include<vector>

 #include<map>

 #include<set>

 #include<queue>

 #include<string>

 #define inf 1000000000

 #define maxn 15

 #define maxm 500+100

 #define eps 1e-10

 #define ll long long

 #define pa pair<int,int>

 #define for0(i,n) for(int i=0;i<=(n);i++)

 #define for1(i,n) for(int i=1;i<=(n);i++)

 #define for2(i,x,y) for(int i=(x);i<=(y);i++)

 #define for3(i,x,y) for(int i=(x);i>=(y);i--)

 #define mod 1000000007

 using namespace std;

 inline int read()

 {

     int x=,f=;char ch=getchar();

     while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}

     while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}

     return x*f;

 }

 int a[maxn],n,m,c[maxn][maxn];

 int main()

 {

     freopen("input.txt","r",stdin);

     freopen("output.txt","w",stdout);

     n=read();m=read();

     for1(i,n)

      {

          c[i][]=c[i][i]=;

         for1(j,n-)c[i][j]=c[i-][j]+c[i-][j-];

      }

     for1(i,n)a[i]=i; 

     while()

     {

         int tmp=;

         for1(i,n)tmp+=a[i]*c[n][i-];

         if(tmp==m)break;

         next_permutation(a+,a+n+);

     } 

     printf("%d",a[]);

     for2(i,,n)printf(" %d",a[i]);

     return ;

 }

BZOJ1653: [Usaco2006 Feb]Backward Digit Sums的更多相关文章

  1. 1653: [Usaco2006 Feb]Backward Digit Sums

    1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 285  Solved:  ...

  2. BZOJ 1653 [Usaco2006 Feb]Backward Digit Sums ——搜索

    [题目分析] 劳逸结合好了. 杨辉三角+暴搜. [代码] #include <cstdio> #include <cstring> #include <cmath> ...

  3. 【BZOJ】1653: [Usaco2006 Feb]Backward Digit Sums(暴力)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1653 看了题解才会的..T_T 我们直接枚举每一种情况(这里用next_permutation,全排 ...

  4. bzoj 1653: [Usaco2006 Feb]Backward Digit Sums【dfs】

    每个ai在最后sum中的值是本身值乘上组合数,按这个dfs一下即可 #include<iostream> #include<cstdio> using namespace st ...

  5. Backward Digit Sums(POJ 3187)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5495   Accepted: 31 ...

  6. Backward Digit Sums(暴力)

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5664   Accepted: 32 ...

  7. POJ3187 Backward Digit Sums 【暴搜】

    Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4487   Accepted: 25 ...

  8. POJ 3187 Backward Digit Sums 枚举水~

    POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻 ...

  9. 【POJ - 3187】Backward Digit Sums(搜索)

    -->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...

随机推荐

  1. CF-599B - Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. 大型分布式C++框架《一:框架简介》

    首先名字要取得霸气才能吸引人气,哈哈~~ 下面简单介绍下情况.框架是腾讯电商平台的分布式框架.虽然腾讯拍拍已经玩完了.但是这套框架还是很不错的.而且据原腾讯同事说微信也是用的这套框架.源码肯定是不能说 ...

  3. JS(六)

    没有视频看了,心里有点慌啊,像这种每天都会灌输很多知识的学习方式,我觉得提前预习真的是有奇效,不然不容易跟上老师的思维. 1.发送验证码:简单 <!DOCTYPE html> <ht ...

  4. nexus5 root教程

    转载自: http://www.inexus.co/article-1280-1.html http://www.pc6.com/edu/71016.html https://download.cha ...

  5. JSTL配合正则表达式在JSP中的应用

    <%@ page language="java" import="java.util.*,cn.com.Person" pageEncoding=&quo ...

  6. [SVG] Simple introduce for SVG

    Just like create html page, you can create a svg tag by: <?xml version="1.0" encoding=& ...

  7. IOS成长之路-Nsstring中搜索方法rangeOfString

    NSString *str1 = @"can you \n speak English"; NSString *str = @"\n"; //在str1这个字符 ...

  8. license文件生成原理

      byte解密weblogic加密oraclehex 现在很多J2EE应用都采用一个license文件来授权系统的使用,特别是在系统购买的早期,会提供有限制的license文件对系统进行限制,比如试 ...

  9. 在VMware中为Linux系统安装VM-Tools的详解教程

    在安装Linux的虚拟机中,单击“虚拟机”菜单下的“安装Vmware-Tools”. 先介绍一下下面安装该工具时要用到的几个目录: /mnt 挂载目录,用来临时挂载别的文件系统,硬件设备 /tmp临时 ...

  10. Java初转型-Tomcat安装和配置

    1.http://www.cnblogs.com/diegodu/p/5915358.html tomcat直接解压,启动然后测试是否正常. 2.http://www.cnblogs.com/mq00 ...