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;
}


全排列算法解析(完整版) 基于全排列算法的完整代码解析,可供理解搜索的技巧,有很高的使用价值 立即下载

相关推荐

Verilog实现FIR滤波器

1、FIR滤波器简介 不追究FIR滤波器的深层含义,我们只关注如何实现;可以看出,FIR滤波器的本质就是延迟、系数相乘与求和,如下图: 2、设计 基于以上分析,按照三级流水实现FIR滤波器设计:信号延迟-系数相乘-求和 `timescale 1ns / 1ps module fir( input clk, input rst_n, input [3:0]din, output reg [9:0]dout ); // data reg reg [3:0]din1; reg [3:.

CLL_caicai的博客 1万+

全排列 - 三种形式

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

Lucius_的博客 668

Python学习24:中国古代数学问题——物不知数(Python123)

意思是说有一些物品,不知道有多少个,3个3个数的话,还多出2个;5个5个数则多出3个;7个7个数也会多出2个。题目为‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬:今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?现假设物品总数不超过n (n

weixin_60530224的博客 2万+

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

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

顾得泉的博客 3576

随机森林模型及案例(Python)

1 集成模型简介 集成学习模型使用一系列弱学习器(也称为基础模型或基模型)进行学习,并将各个弱学习器的结果进行整合,从而获得比单个学习器更好的学习效果。 集成学习模型的常见算法有Bagging算法和Boosting算法两种。 Bagging算法的典型机器学习模型为随机森林模型,而Boosting算法的典型机器学习模型则为AdaBoost、GBDT、XGBoost和LightGBM模型。 1.1 Bagging算法简介 Bagging算法的原理类似投票,每个弱学习器都有一票,最终根据所有弱学习器的投

qq_42433311的博客 4万+

OpenCV实战:5分钟搞定图像拼接(附Python代码)

本文通过OpenCV实战教程,详细讲解如何使用Python在5分钟内完成图像拼接。重点介绍`findHomography`函数和单应性矩阵的应用,提供完整的代码示例和常见问题解决方案,帮助开发者快速掌握图像拼接技术。

weixin_26833503的博客 120

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

省洗发水钱买书 333

K-L展开 (Karhunen-Loeve expansion)

K-L变换

susan_wang1的博客 2万+

普及练习场-带有技巧的搜索-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的博客 353

深度强化学习中的动作屏蔽(Action Masking)

在复杂环境中使用动作屏蔽,实现更好的训练效果

Kiek17的博客 5875

内存优化--LOGBACK导致内存告警

一.故事背景 由于开辟新市场需要,开发了新的项部署在海外平台,其中最消耗内存的功能是消费kafka消息(最开始预计一天有1千万的量),部署完之后运行一段时间发现出现内存告警问题。 二.分析问题 出现内存告警,(新项目是低配置机器 2C4G * 2台)第一个念头是去查看堆空间设置的情况,看到jvm启动命令中堆空间都是直接指定大小,说明问题不是出在堆空间 -Xmx3g -Xms3g 先不管这些,去查看总的内存使用情况,发现总的内存空间使用将近有92% = 3.68G,那么这多出来使用的0.6

qq_33199919的博客 6051

洛谷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

HTB_Bike靶机之模板注入

船到桥头自然直 文章目录信息收集模板引擎SSIT 模板注入后放答案 信息收集 日常 A 扫描,两个端口 22/tcp open ssh OpenSSH 8.2p1 Ubuntu 4ubuntu0.4 (Ubuntu Linux; protocol 2.0) | ssh-hostkey: | 3072 48:ad:d5:b8:3a:9f:bc:be:f7:e8:20:1e:f6:bf:de:ae (RSA) | 256 b7:89:6c:0b:20:ed:49:b2:c1:86:7c:.

网络安全学习 797

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

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

zaiyang遇见 987

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

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

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

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)

梧桐下的四叶草 564

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的博客 377

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的博客 259

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、付费专栏及课程。

余额充值