poj 3187 Backward Digit Sums (全排列)

全排列算法解析(完整版) 基于全排列算法的完整代码解析,可供理解搜索的技巧,有很高的使用价值 立即下载
Backward Digit Sums
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4788 Accepted: 2759

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

Hint

Explanation of the sample: 

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

题目大意:n个数字1-n形成一种排列使累加和的倒三角最底端值为k,输出这种排列,如果有多种情况,输出字典序小的。

解题思路:用STL的next_permutation(a,a+n)函数生成全排列。之后步骤就简单了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[12],b[12];
int main()
{
    int i,n,k;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)  //排列为1-n递增的情况
	{
        a[i]=i+1;
		b[i]=a[i];
	}
    int m=n;
	while(m>1)
	{
		for(i=0;i<m-1;i++)
			b[i]+=b[i+1];
		m--;
	}
	if(b[0]==k)
	{
		for(i=0;i<n;i++)
		{
			if(i)
				printf(" ");
			printf("%d", a[i]);
		}
		printf("\n");
		return 0;
	}
    while(next_permutation(a,a+n))
	{
		for(i=0;i<n;i++)  //三角形累加过程
			b[i]=a[i];
        m=n;
		while(m>1)
		{
			for(i=0;i<m-1;i++)
				b[i]+=b[i+1];
			m--;
		}
		if(b[0]==k)break;
    }
    for(i=0;i<n;i++)
    {
        if(i)
            printf(" ");
        printf("%d", a[i]);
    }
	printf("\n");
    return 0;
}


全排列算法解析(完整版) 全排列算法详细解析(完整版) 立即下载

相关推荐

从按下开机键到进入系统:深入主板“后台”,揭秘Super I/O芯片的上电时序与BIOS交互

本文深入探讨了Super I/O芯片(SIO)在电脑启动过程中的关键作用,详细解析了其架构设计、上电时序与BIOS交互机制。通过分析SIO的多功能集成控制器特性,包括低速I/O管理、硬件监控和电源管理,揭示了从按下电源按钮到系统初始化的精密硬件协作过程。文章还提供了常见问题的调试方法,为硬件工程师和嵌入式开发者提供了实用参考。

weixin_27014595的博客 141

全排列 - 三种形式

全排列的三种形式:元素无重不可复选,元素可重不可复选,元素无重可复选。

Lucius_的博客 668

锐捷交换机堆叠配置举例

一、组网需求: 1)四台S2628G进行堆叠; 2)设备在堆叠前先设置好优先级; 3)使用光口堆叠的设备如RG-S2928G-E相关配置 二、组网拓扑 三、配置要点: 普通堆叠设备不需要配置,只要把堆叠模块和线缆连接好即可 堆叠模块不支持热插拔,即插在模块连上线缆后不能立即生效,需要重启设备才生效。 如果拔掉堆叠插槽后,想再插上模块堆叠,那么必须重启设备才生效 设备堆叠需要协商参数,时间会比较久,请耐心等待 堆叠时设备建议是...

dongsheng_9234的专栏 8299

一篇文章带你搞懂---全排列

全排列(Permutation)是指将一组元素按照一定的顺序进行排列的过程。在计算机科学中,全排序是一个经典的问题,常用于解决排列组合、搜索和优化等领域的算法设计。

顾得泉的博客 3579

在CoppeliaSim里实现一个最简单的强化学习

前言 上一篇文章中介绍了CoppeliaSim的一些基本知识和一个简单的用python去控制CoppeliaSim里面四旋翼无人机运动的一个例子。文章链接 :https://blog.csdn.net/qq_37051669/article/details/115324851 本文将继续介绍如何利用CoppeliaSim的物理环境去实现一个最简单的强化学习。 如何实现强化学习 强化学习(Reinforcementlearning)是机器学习中的一个领域,强调如何基于环境而行动,以取得最大化的预期利.

qq_37051669的博客 3767

大数据之数据抽取架构演变过程

而当时我们搭建的Flink集群是基于YARN进行资源管理和调度的,因为YARN中不仅仅包含着FLInk的资源,也有hive,zoozie,hdfs等等的资源,这样容易造成yarn资源的过度使用,容易引起整个底座的cpu以及内存的使用问题,经过排查,主要是由于FLink的使用资源引起的,所以后续就把Flink从整个集群中剥离出来,进行资源的隔离,最后Flink采用了StandAlone HA部署(但是这个目前还是存在问题,后续根据项目进行优化)'topic' = '表名',二、数据抽取架构的演变。

天道酬勤 869

普及练习场-带有技巧的搜索-P1118 [USACO06FEB]数字三角形

题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 ≤ N ≤ 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. The...

zqhf123的博客 358

软件设计师---计算机系统

软件设计师---计算机系统

我从不打没有准备的仗! 3万+

Backward Digit Sums(POJ-3187)题解

文章目录题目描述InputOutputSample InputSample OutputHint算法分析解题标程错题分析 题目描述 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 few

省洗发水钱买书 335

基于DINO自监督学习对YOLO11 Backbone进行预训练

这PoolHead将获取前一层的输出并应用卷积,然后应用自适应平均池化以将特征图的空间维度减小为固定且一致的大小(1x1)。这是必需的,因为否则空间维度会根据输入的大小而变化,这将使将 DINO 头部附加到主干变得困难,因为它需要固定的输入大小。在本指南中,我们将介绍如何使用 Lightly 使用 DINO 预训练 YOLO 11主干,以及如何将其重新加载到 Ultralytics 中进行微调。您可以使用不同的大小,例如,640这与 YOLO 中的默认图像大小更一致,但它也会在训练期间消耗更多 VRAM。

如果想成为中心,那么就到中心去吧。 1031

化工设计软件:AutoCAD Plant 3D二次开发_(1).AutoCADPlant3D概述

AutoCAD Plant 3D 是一款专为化工行业设计的三维工厂设计和建模软件。它基于AutoCAD平台,提供了强大的三维建模功能和专业的化工设计工具,帮助工程师和设计师在工厂设计过程中提高效率和准确性。AutoCAD Plant 3D不仅能够创建详细的三维模型,还可以进行管道设计、设备布置、支架设计等复杂任务。通过上述案例,我们可以看出 AutoCAD Plant 3D 在化工厂设计中的强大功能和优势。该软件不仅提供了丰富的建模工具和专业的设计功能,还支持二次开发,使用户能够根据具体需求进行定制化操作。

kkchenjj的博客 1132

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

题目描述FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 &lt;= N &lt;= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer numb...

weixin_38199770的博客 250

Debezium报错处理系列之第112篇:Cannot replicate because the master purged required binary logs. Replicate the

Debezium报错处理系列之第112篇:Cannot replicate because the master purged required binary logs.

zhengzaifeidelushang的博客 733

POJ3187-Backward Digit Sums(穷竭搜索:全排列)

题目要求根据N和最终总和找出字典序最小的1到N排列,使得逐层相邻相加后的结果等于给定总和。当N较小时(≤10),可以通过枚举所有排列并模拟相加过程来求解。优化方法是利用杨辉三角系数预处理,计算每个排列的加权和,从而避免重复模拟相加。代码首先生成1到N的所有排列,然后检查哪个排列的加权和等于目标值,找到后立即输出即可保证字典序最小。两种实现方式(直接模拟相加或杨辉三角加权)均可通过,后者效率更高。

zaiyang遇见 991

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

POJ 3187 Backward Digit Sums (DFS 全排列)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4531   Accepted: 2604 Description FJ and his cows enjoy playing a mental game. They write

EaShion1994的专栏 705

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

POJ 3187 Backward Digit Sums (全排列)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5727   Accepted: 3320 Description FJ and his cows enjoy playing a mental game. They write

滴水穿石,慢慢积累,总会有成果 433

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

POJ 3187- Backward Digit Sums(DFS+全排列)

E - Backward Digit Sums Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3187 Appoint description:  System Crawler  (2016-05-02)

梧桐下的四叶草 566

POJ3187 Backward Digit Sums【全排列】

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7633   Accepted: 4397 Description FJ and his cows enjoy playing a mental game. They write

海岛Blog 1073

Backward Digit Sums - poj3187 - 全排列

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9149   Accepted: 5206 Description FJ and his cows enjoy playing a mental game. They write down the nu...

m0_37579232的博客 438

POJ3187 Backward Digit Sums 全排列经典题目

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Description FJ and his cows enjoy playing a mental game. They write d

逝水流年轻染尘 427

poj 3187 Backward Digit Sums 【全排列变形题】

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5356   Accepted: 3093 Description FJ and his cows enjoy playing a mental game. They write

dxx_111的博客 562

POJ 3187 Backward Digit Sums (dfs全排列)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4705   Accepted: 2703 Description FJ and his cows enjoy playing a mental game. They wri

TaoSama's Microcosm 496

poj Backward Digit Sums 3187 (全排列)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5667   Accepted: 3281 Description FJ and his cows enjoy playing a mental game. They write d

yanghui07216的博客 378

Backward Digit Sums POJ - 3187 (枚举+全排列)

题目: 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 num...

titi2018815的博客 260

POJ 3187 Backward Digit Sums——————全排列暴力水题

Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10751 Accepted: 6056 Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (...

鸿杰 的博客 278

poj 3187 Backward Digit Sums(全排列变形)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5670   Accepted: 3284 Description FJ and his cows enjoy playing a mental game. They write

dxx_111的博客 576
上一篇: noj 2030 收购计划
下一篇: hdu 1754 I Hate It(线段树)
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址