Backward Digit Sums - POJ 3187 DFS

Backward Digit Sums
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4860 Accepted: 2808

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.


题意:某1~N的序列,相邻两个相加得到新的序列,一直加到最后只剩一个数,给出N和这种方法得到的最后一个数,求出字典序最小的原序列。

思路:水题,简单思考后每一项的系数为二项式系数,数据范围很小直接DFS即可


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<set>
#define MAXN 20
#define LL long long
using namespace std;
int a[MAXN],len=0;
bool used[20];
int n,sum;
int yh[20][20];
bool dfs(int pos,int now)
{
	for(int i=1;i<=n;i++)
	{
		if(used[i]) continue;
		if(pos==n)
		{
			if(now+i*yh[n][pos]==sum)
			{
				used[i]=1;
				a[len++]=i;
				return 1;
			}
			else
				continue;
		}
		if(now+i*yh[n][pos]>=sum)
			continue;
		else
		{
			a[len++]=i;
			used[i]=1;
			if(dfs(pos+1,now+i*yh[n][pos]))
				return 1;
			else
			{
				len--;
				used[i]=0;
			}
		}
	}
	return 0;
}
int main()
{
//	freopen("in.txt","r",stdin);
	yh[1][1]=1;
	for(int i=2;i<=11;i++)
	{
		for(int j=1;j<=i;j++)
		{
			yh[i][j]=yh[i-1][j]+yh[i-1][j-1];
		}
	}
	while(~scanf("%d%d",&n,&sum))
	{
		len=0;
		memset(used,0,sizeof(used));
		dfs(1,0);
		int temp;
		for(int i=0;i<len;i++)
		{
			printf("%d",a[i]);
			if(i==len-1)
				printf("\n");
			else printf(" ");
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
forward-loss-backward-update 是一种在神经网络中常用的训练方法。它由以下四个步骤组成: 首先是"forward"(前向传播)步骤。在该步骤中,输入数据会通过网络的各个层级进行计算和传递。每一层都会执行一些权重计算和激活函数的操作,将结果传递给下一层。最终,前向传播会生成网络的输出。 接下来是"loss"(损失计算)步骤。该步骤主要用于计算网络的预测结果与真实标签之间的差距,即损失函数。损失函数可以是交叉熵、平方误差等。通过计算损失,我们可以得到一个衡量网络预测准确性的值。 然后是"backward"(反向传播)步骤。在该步骤中,反向传播算法会利用损失函数的梯度信息来计算每个参数的梯度。这些梯度指示了网络中各个参数对最终损失函数的贡献程度。通过反向传播,我们可以得到网络中各个参数的梯度值。 最后是"update"(参数更新)步骤。在该步骤中,我们使用梯度下降法或其他优化算法来更新网络中的参数。通过使用计算得到的梯度信息,我们可以调整网络中的参数,使其向着最小化损失函数的方向更新。 总结起来,forward-loss-backward-update 是一种有效的神经网络训练方法。通过前向传播计算网络输出,计算损失函数评估网络性能,利用反向传播计算参数梯度,最后使用参数更新方法来优化网络性能。这个过程迭代进行,使得网络能够逐渐学习到更好的表示和预测能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值