题目描述

FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \le 10)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=4N=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 NN . 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.

有这么一个游戏:

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

3,1,2,43,1,2,4

4,3,64,3,6

7,97,9

1616

最后得到 1616 这样一个数字。

现在想要倒着玩这样一个游戏,如果知道 NN ,知道最后得到的数字的大小 sumsum ,请你求出最初序列 a_iai​ ,为 11 至 NN 的一个排列。若答案有多种可能,则输出字典序最小的那一个。

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

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

输入输出格式

输入格式:

两个正整数 n,sumn,sum 。

输出格式:

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

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

输入输出样例

输入样例#1:

4 16
输出样例#1:

3 1 2 4

说明

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

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

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

Solution:

  本题比较水(纯暴力就能过)。

  不难发现每次合并,每个位置的数所参与的贡献次数为杨辉三角的第$n$行所对应的数。

  举个例子:$a,b,c,d\rightarrow a+3b+3c+d$。最后一个数中$a,b,c,d$的系数就是杨辉三角第$4$行的数列。

  所以我们可以先递推出前$12$行杨辉三角的数,然后作为系数,因为要使得前面的数值尽可能小,所以就枚举每一位的取值,随便加一个可行性剪枝(记录当前的$tot$,若大于总和$sum$则减掉),然后记录一下每个数的取值范围,乱搞一下就好了。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
int n,sum,a[],c[][];
bool f=,vis[]; il int gi(){
int a=;char x=getchar();bool f=;
while((x<''||x>'')&&x!='-')x=getchar();
if(x=='-')x=getchar(),f=;
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return f?-a:a;
} il void init(){
c[][]=;
For(i,,) For(j,,i) c[i][j]=c[i-][j-]+c[i-][j];
} il void dfs(int now,int tot){
if(sum-tot<=)return;
if(now==n-)
if(!vis[sum-tot]&&sum-tot<=n) {
a[n]=sum-tot;
For(i,,n) cout<<a[i]<<' ';
exit();
}
int p=min(sum-tot,n);
For(i,,p)
if(!vis[i]) {
a[now+]=i;vis[i]=;
dfs(now+,tot+i*c[n][now+]);
vis[i]=;
}
} int main(){
ios::sync_with_stdio();
init();
n=gi(),sum=gi();
if(n==){cout<<;return ;}
For(i,,n) {
vis[i]=;
a[]=i,dfs(,i);
vis[i]=;
}
return ;
}

P1118 [USACO06FEB]数字三角形`Backward Digit Su`…的更多相关文章

  1. P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法

    有这么一个游戏: 写出一个11至NN的排列a_iai​,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置.下面是一 ...

  2. 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 ...

  3. 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…

    https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...

  4. P1118 [USACO06FEB]数字三角形`Backward Digit Su`… (dfs)

    https://www.luogu.org/problemnew/show/P1118 看的出来是个dfs 本来打算直接从下到上一顿搜索 但是不会 看了题解才知道系数是个杨辉三角....... 这样就 ...

  5. 洛谷P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    #include<iostream> using namespace std ; ; int y[N][N]; int n; int a[N]; bool st[N]; int sum; ...

  6. luoguP1118 [USACO06FEB]数字三角形`Backward Digit Su`… 题解

    一上午都在做有关搜索的题目,,, 看到这题之后就直接开始爆搜 结果只有70分, 其余的点硬生生的就是那么WA了. 我的天哪~ 70分代码: #include<iostream> #incl ...

  7. Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学

    题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...

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

    数字三角形 题目链接 4 16 3 1 2 4 3 1 2 4 (3+1) (1+2) (2+4)(3+1+1+2) (1+2+2+4) (3+1+1+1+2+2+2+4)16=1*3+3*1+3*2 ...

  9. [USACO06FEB]数字三角形

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...

随机推荐

  1. Firewalld共享上网及本地yum仓库搭建

    1.firewalld共享上网 1.服务端操作(有外网的服务器) 1.开启防火墙并加入开机自启动 [root@zeq ~]# systemctl start firewalld [root@zeq ~ ...

  2. 路由器基础设置之ospf

    我们将以上面的拓扑图来进行实验,要用ospf的协议达到全网互通的效果 router1: enable 进入特权模式 config t 进入全局配置模式 interface L0 ip address ...

  3. Windows环境下安装redis及PHP Redis扩展

    附带管理工具安装教程 安装环境 WNMP环境 参考教程:WIN10下WNMP开发环境部署 安装windows的redis服务 安装包下载 选择msi安装包下载并安装,下载可能会有点慢,请自行使用梯子. ...

  4. php 操作RabbitMQ

    本文摘抄自:https://www.cnblogs.com/alin-qu/p/8312874.html php 操作RabbitMQ   基本流程图 如果exchange 没有绑定queue,则消息 ...

  5. SAN---第二网的概念

    网络技术的优缺点:优点:连接能力,超强路由,管理能力,远距离缺点:低速以及高负载,强烈的软件需求,错误检测能力 SAN:storage area network(存储区域网络)--是一种基于光网的特殊 ...

  6. oracle杀死锁表的进程(转发+合并+自己实践)

    之一: Oracle数据库操作中,我们有时会用到锁表查询以及解锁和kill进程等操作 (1)锁表查询的代码有以下的形式:select count(*) from v$locked_object;sel ...

  7. Django中的select_related与prefetch_related

      Django是一个基于Python的网站开发框架,一个很重要的特点就是Battery Included,简单来说就是包含了常规开发中所需要的一切东西,包括但不限于完整的ORM模型.中间件.会话处理 ...

  8. LeetCode:22. Generate Parentheses(Medium)

    1. 原题链接 https://leetcode.com/problems/generate-parentheses/description/ 2. 题目要求 给出一个正整数n,请求出由n对合法的圆括 ...

  9. jar命令:打包、查看、更新等

    如何把写好的Java程序打包为jar文件呢?下面说的就是java使用命令行打包JAR的方法 1.命令行的方式:打包jar cf JAR文件名称 程序文件名称或者程序所在的文件夹举例:jar cf My ...

  10. quartz 使用总结

    quartz是一个任务调度框架,具体的用途比如说,我想我的程序在每天的3点干什么事,每隔多长时间做一件什么事.quartz框架就可以完美地解决这些. 1.xml配置方式 首先我是用spring来管理的 ...