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. 【2013南京区域赛】部分题解 hdu4802—4812

    上周末打了一场训练赛,题目是13年南京区域赛的 这场题目有好几个本来应该是我擅长的,但是可能是太久没做比赛了各种小错误代码写的也丑各种warusn trush搞得人很不爽 全场题之一的1002也没有想 ...

  2. HDU5125--magic balls(LIS)

    题意:求a数组的LIS,但是加了一个条件,为了LIS最大 b[i] a[i]可以交换.最多交换mci: 赤果果的dp啊,可是这个题用线段树的话却会TLE,,由于查询的只是1-x的最大值 因此我们可以用 ...

  3. 《Node.js开发指南》知识整理

    Node.js简介 Node是一个可以让JavaScript运行在服务器端的平台,抛弃了传统平台依靠多线程来实现高并发的设计思路,而采用单线程.异步式I/O.事件驱动式的程序设计模型. 安装和配置No ...

  4. 从此走上一条iOS程序猿不归路。。。

    新的城市,新的生活!前不久刚刚结束了苦逼的面试找工作之旅,期间也小有收货,如今正处年底工作闲暇之余,将前一阵子陆陆续续的总结整理了一下,本人菜鸟程序猿一只,水平有限,本文总结的知识不算深入,比较浅显, ...

  5. 鼠标点击DIV后,DIV的背景变色(js)

    <!DOCTYPE html> <html> <head> <script> window.onload = function(){ var divs ...

  6. Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现

    UI系列教程第八课:Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现 今天蓝老师要讲的是关于新浪新闻侧滑界面的实现.先看看原图: 如图所示,这种侧滑效果以另一种方式替 ...

  7. [置顶] .net技术类面试、笔试题汇总1

    1.简述 private. protected. public. internal 修饰符的访问权限. private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类内 ...

  8. linux查看系统版本

       RHEL7.0以下,查看系统版本的方式: [rusky@rheltest1 ~]$ cat /proc/version Linux version -.el6.x86_64 (mockbuild ...

  9. 今天工作中遇到的根据用户id取得产品大类和相关小类的问题

    今天做了一个项目,需求是客户登陆后,可以从会员中心发布详细信息(包括联系信息和公司信息),插入到数据库后在将来生成一个公司页面模板,一般的产品大类+小类 用repeater嵌套就可以了,但是这个涉及到 ...

  10. IP地址的存储和使用

    ip地址使用int类型存储,用INET_NTOA()和INET_ATON()转换 mysql'),inet_aton('127.0.0.1'); +-------------------------+ ...