题目描述

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 916最后得到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。

10分写法:啥也不输出(雾

60分解法:直接搜索,爆掉时间

100分解法:

我们可以采取一定的方法进行剪枝。显然可以发现,每一个数对于最终结果的贡献是一定的(通俗的说,譬如有序列a,b,c,d,那么最终结果为a*q+b*w+c*r+d*t,其中q、w、e、r是与层数有关的值,当层数一定时,q、w、e、r一定)。

对于原题,我们可以得到一个递推式:(我这里采用文字描述,严谨的数学语言的话会很麻烦)

当前点对结果的贡献系数=左下角的贡献系数+右下角的贡献系数

原因很简单,请大家先仔细思考,然后再往下看。

我们设当前点贡献系数为k,已知当前点值为w,左下角点值为w1+w,贡献系数为k1,右下角点值为w2+w,贡献系数为k2。

可以得到:w1*k1+w2*k2+w*(k1+k2)=w总

可以看出,w的系数为k1+k2,即当前点的贡献系数等于左下角贡献系数+右上角贡献系数。

但要想得到贡献系数必须倒着推,原题三角形倒过来,我们不难想到杨辉三角。



下面是程序,希望大家仔细研读,以便对杨辉三角这个神奇的东西有更深刻的理解!



#include<iostream>

#include<cstdio>

#include<string>

#include<cstring>



const int MAXX=15;



int num[MAXX];

int delta[MAXX][MAXX];



bool b[MAXX];



int n,sum;

bool ok,zhi;

void dfs(int step,int summ)

{

    if(step-1==n)

    {

        if(summ==sum)

        {

            for(int i=1;i<=n;i++)

            {

                printf("%d ",num[i]);

            }

            ok=true;

        }

        return;

    }

    if(summ>sum)return;

     if(ok)return;

    for(int i=1;i<=n;i++)

    {

        if(!b[i])

        {

            num[step]=i;

            b[i]=true;

            dfs(step+1,summ+i*delta[n][step]);

            b[i]=false;

        }

    }

}

int main()

{

    scanf("%d%d",&n,&sum);

    for(int i=1;i<=n+1;i++)delta[i][1]=1;

    for(int i=2;i<=n;i++)

    {

        for(int j=2;j<=n;j++)

        {

            delta[i][j]=delta[i-1][j]+delta[i-1][j-1];

        }

    }

    dfs(1,0);

    return 0;

}

另:可以将dfs的第二个参数sum设为全局变量,并将dfs递归部分改为:

for(int i=1;i<=n;i++)

    {

        if(!b[i])

        {

            num[step]=i;

            b[i]=true;

            summ+=i*delta[n][step];

            dfs(step+1);

            summ-=i*delta[n][step];

            b[i]=false;

        }

    }

}

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring> const int MAXX=15; int num[MAXX];
int delta[MAXX][MAXX]; bool b[MAXX]; int n,sum;
bool ok,zhi;
void dfs(int step,int summ)
{
if(step-1==n)
{
if(summ==sum)
{
for(int i=1;i<=n;i++)
{
printf("%d ",num[i]);
}
ok=true;
}
return;
}
if(summ>sum)return;
if(ok)return;
for(int i=1;i<=n;i++)
{
if(!b[i])
{
num[step]=i;
b[i]=true;
dfs(step+1,summ+i*delta[n][step]);
b[i]=false;
}
}
}
int main()
{
scanf("%d%d",&n,&sum);
for(int i=1;i<=n+1;i++)delta[i][1]=1;
for(int i=2;i<=n;i++)
{
for(int j=2;j<=n;j++)
{
delta[i][j]=delta[i-1][j]+delta[i-1][j-1];
}
}
dfs(1,0);
return 0;
}
另:可以将dfs的第二个参数sum设为全局变量,并将dfs递归部分改为:
for(int i=1;i<=n;i++)
{
if(!b[i])
{
num[step]=i;
b[i]=true;
summ+=i*delta[n][step];
dfs(step+1);
summ-=i*delta[n][step];
b[i]=false;
}
}
}

附加暴力写法(60分):

#include<cstdio>
const int MAXX=15;
int delta[MAXX][MAXX];
bool b[MAXX];
int n,sum;
bool ok;
void dfs(int step)
{
if(step-1==n)
{
for(int i=2;i<=n;i++)
{
for(int j=1;j<=n+1-i;j++)
{
delta[i][j]=delta[i-1][j]+delta[i-1][j+1];
if(delta[i][j]>sum)return;
}
}
if(delta[n][1]==sum)
{
for(int i=1;i<=n;i++)
{
printf("%d ",delta[1][i]);
}
ok=true;
}
return;
}
if(ok)return;
for(int i=1;i<=n;i++)
{
if(!b[i])
{
delta[1][step]=i;
b[i]=true;
dfs(step+1);
b[i]=false;
}
} }
int main()
{
scanf("%d%d",&n,&sum);
for(int i=1 ; i <= n ; i++)
delta[1][i] = 1;
dfs(1);
return 0;
}

【洛谷】【USACO】P1118 数字三角形的更多相关文章

  1. 「区间DP」「洛谷P1043」数字游戏

    「洛谷P1043」数字游戏 日后再写 代码 /*#!/bin/sh dir=$GEDIT_CURRENT_DOCUMENT_DIR name=$GEDIT_CURRENT_DOCUMENT_NAME ...

  2. 洛谷P1118 数字三角形游戏

    洛谷1118 数字三角形游戏 题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直 ...

  3. 洛谷 P2602 [ZJOI2010]数字计数

    洛谷 第一次找规律A了一道紫题,写篇博客纪念一下. 这题很明显是数位dp,但是身为蒟蒻我不会呀,于是就像分块打表水过去. 数据范围是\(10^{12}\),我就\(10^6\)一百万一百万的打表. 于 ...

  4. BZOJ1833或洛谷2602 [ZJOI2010]数字计数

    BZOJ原题链接 洛谷原题链接 又是套记搜模板的时候.. 对\(0\sim 9\)单独统计. 定义\(f[pos][sum]\),即枚举到第\(pos\)位,前面枚举的所有位上是当前要统计的数的个数之 ...

  5. bzoj 4816: 洛谷 P3704: [SDOI2017]数字表格

    洛谷很早以前就写过了,今天交到bzoj发现TLE了. 检查了一下发现自己复杂度是错的. 题目传送门:洛谷P3704. 题意简述: 求 \(\prod_{i=1}^{N}\prod_{j=1}^{M}F ...

  6. 【题解】洛谷P3166 [CQOI2014] 数三角形(组合+枚举)

    洛谷P3166:https://www.luogu.org/problemnew/show/P3166 思路 用组合数求出所有的3个点组合(包含不合法的) 把横竖的3个点共线的去掉 把斜的3个点共线的 ...

  7. 洛谷 USACO P2207 Photo

    P2207 Photo 题目描述 Framer Jhon 打算给他的N头奶牛照相,( 2 <= N <= 1 000 000 000) . 他们排成一条线,并且依次取1~N作为编号. 每一 ...

  8. 洛谷P1118 数字三角形【dfs】【STL】

    题目链接:https://www.luogu.org/problemnew/show/P1118 题意: 1~n的一个排列,相邻的两项加起来得到下一行. 现在给定最后一行的数字,问最初的1~n的排列是 ...

  9. 洛谷 - P1118 - 数字三角形 - next_permutation

    https://www.luogu.org/problemnew/show/P1118 next_permutation的第二个参数是最后一个元素的下一个元素,sort也是一样!有毒!这么低级的错误. ...

随机推荐

  1. LintCode_13 字符串查找

    题目 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始).如果不存在,则返回 -1. 您在真实的面 ...

  2. Android基础控件ToggleButton和Switch开关按钮

    1.简介 ToggleButton和Switch都是开关按钮,只不过Switch要Android4.0之后才能使用! ToggleButton <!--checked 是否选择--> &l ...

  3. python用reduce和map把字符串转为数字的方法

    python用reduce和map把字符串转为数字的方法 最近在复习高阶函数的时候,有一道题想了半天解不出来.于是上午搜索资料,看了下别人的解法,发现学习编程,思维真的很重要.下面这篇文章就来给大家介 ...

  4. PAT甲级——A1081 Rational Sum

    Given N rational numbers in the form numerator/denominator, you are supposed to calculate their sum. ...

  5. Cooki and Session

    目录 Cookie Cookie的由来 什么是Cookie Cookie的原理 查看Cookie Django中操作Cookie 获取Cookie 设置Cookie 删除Cookie Session ...

  6. 运行第一个python程序,python 变量,常量,注释

    一.运行第一个python程序: print('Hello,world') 保存为.py文件 在cmd窗口: python3x:python  py文件路径 回车 python2x:python  p ...

  7. linux中断处理-顶半部(top half)和底半部(bottom half) -转

    原文:http://rensanshi.blog.163.com/blog/static/21395510820136282224877/ 设备的中断会打断内核中进程的正常调度和运行,系统对更高吞吐率 ...

  8. ssh连接Ubuntu之access denied

    解决方法是: 修改/etc/ssh/ssh_config文件, #PermitRootLogin prohibit-password改为PermitRootLogin yes 然后 PasswordA ...

  9. 使用 QuickBI 搭建酷炫可视化分析

    随着各行各业大数据的渗透,BI 类数据分析需求与日俱增,如何让可视化更好的展现数据的价值,是 BI 类产品一直努力的方向.对此国内外的BI产品都有自己的方法,如国外大牌的 PowerBI.Tablea ...

  10. HZOI20190818模拟25题解

    题面:https://www.cnblogs.com/Juve/articles/11372379.html A:字符串 其实是CATALAN数水题... 和网格一毛一样:https://www.cn ...