首页 > 其他分享 >POJ--3187 Backward Digit Sums(暴搜/减枝)

POJ--3187 Backward Digit Sums(暴搜/减枝)

时间:2023-03-25 19:45:08浏览次数:56  
标签:Digit temp -- Sums int game num uint result

记录
5:30 2023-3-25

http://poj.org/problem?id=3178

reference:《挑战程序设计竞赛(第2版)》第二章练习题索引 p135

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.

暴搜,用stl中的next_permutation生成组合数(自己写也可以 类似于dfs的思路 输出组合数)
减枝的思路:这里很简单,next_permutation生成的序列大小是从小到大的(前提:使用的序列是自然数序列1 2 3 4 5 ... n),自己写组合数生成的话应该也是生成从小到大的,所以生成的第一个满足条件的就是最小的那个(实测,如果生成全部的序列会TLE)

#include<cstdio>
#include<iostream>
#include<algorithm>
#define MAX_N 20
using namespace std;
typedef long long ll;
typedef unsigned int uint;
const int INF = 0x3f3f3f3f;

uint N, M;
uint num[MAX_N + 1] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
uint result[MAX_N + 1];

uint sum() {
    uint temp[N];
    uint temp_N = N;
    memcpy(temp, num, sizeof(temp));
    while (temp_N > 1) {
        for(uint i = 0; i < temp_N - 1; i++) {
            temp[i] = temp[i] + temp[i + 1];
        }
        temp_N--;
    }
    return temp[0];
}

void print() {
    if(N == M == 1) {
        result[0] = 1;
    }
    for(int i = 0 ; i < N; i++) {
        if(i != N-1) {
            printf("%d ", result[i]);
        } else {
            printf("%d\n", result[i]);
        }
    }
}

void solve() {
    fill(result, result + MAX_N + 1, INF);
    do {
        uint total = sum();
        if(total == M) {
            // for(int i = 0; i < N; i++) {
            //     if(num[i] > result[i]) {
            //         break;
            //     } else if(num[i] == result[i]){
            //         continue;
            //     } else {
            //         memcpy(result, num, sizeof(result));
            //         break;
            //     }
            // }
            memcpy(result, num, sizeof(result));
            break;
        }
    } while (next_permutation(num, num + N));
    print();
}

int main() {
    while (~scanf("%d%d", &N, &M)) {
        solve();
    }
}

标签:Digit,temp,--,Sums,int,game,num,uint,result
From: https://www.cnblogs.com/57one/p/17255437.html

相关文章

  • java——spring boot集成kafka——查看消费组以及信息
                                                       ......
  • 对系统理论的认识
    系统的定义是一个由一组相互连接的要素组成,能够实现某个目标的整体.任何一个系统都包括三种构成要素:要素、连接、功能或目标.系统的静态特性-----结构包括相关性和等级......
  • 面试高频问题之C++编译过程
    C++编译过程C++是一种高级编程语言,但是计算机并不能直接理解它。因此,需要将C++代码翻译成计算机可以理解的机器语言。这个过程就是编译过程,是C++程序从源代码到可执行文件......
  • 泰拉瑞亚EasyBuildMod便捷建造模组开发详细过程
    github地址:https://github.com/lxmghct/Terraria-EasyBuildMod如果觉得有帮助,记得在github上点个star哦~创意工坊搜索EasyBuildMod即可找到模组目录简介模组物品制......
  • java——spring boot集成kafka——单播与多播消息的实现
    单播消息的实现:   单播消息:⼀个消费组⾥只会有⼀个消费者能消费到某⼀个topic中的消息。于是可以创建多个消费者,这些消费者在同⼀个消费组中。  创建一个消费......
  • 力扣---剑指 Offer 32 - III. 从上到下打印二叉树 III
    请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推。例如:给定二叉树:......
  • 【入门】Go语言运算符详解
    目录一、算数运算符1.1案例一:算数运算符练习1.2案例二:求三门成绩的总和、平均分1.3计算商场买衣服总共消费多少元一、算数运算符+-*/%++--1.1案例一:算数......
  • AtCoder Educational DP Contest
    1.A没什么难度,直接算就可以了。点击查看代码#include<bits/stdc++.h>#defineintlonglong#defineYesprintf("Yes\n")#defineNoprintf("No\n")#defineYESpr......
  • pytorch gather b2 = a.gather(1, b.view(-1, 1))
    importtorcha=torch.randint(0,100,(6,3))b=torch.Tensor([0,1,1,2,0,2]).long()b=b.unsqueeze(1)b0=b.view(-1,1)b2=a.gather(1,b.view(-1,1......
  • mysql公共字段填充
    在实体类的属性上打@TableField注解,并在写明何时自动填充。 按照框架要求编写元数据对象处理器,在此类中统一为公共字段赋值,此类需要实现MetaObjectHandler接口1/**......