Backward Digit Sums(序列生成 + 模拟)

19 篇文章 0 订阅

 

C - Backward Digit Sums

  Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  

Submit Status Practice POJ 3187 

 

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与总数M,N指从1到N一共有N个数,通过之间任意排序,相邻两个数两两加和最后得出一个总数,当这个总数等于M时,则输出该序列,这个序列可能不止一种,输出字典序最小的一组数据。

   思路:

   先用二维数组将这N个数放在第一行位置上,然后通过调用algorithm函数中的next_permutation来对刚行数据不停生成新排序序列来进行两两加和处理,当一遇到和等于M时,输出该序列。

 AC and test:

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
	int N,M,i,j;
	int a[15][15];
	memset(a,0,sizeof(a));
	scanf("%d%d",&N,&M);
	for(i=0;i<N;i++)
	   a[0][i]=i+1;
	
//	for(i=0;i<N;i++)
//	 printf("%d ",a[0][i]);
//	printf("\n");
	
	
//	sort(a[0],a[0]+N);
	
//	for(i=0;i<N;i++)
//	 printf("%d ",a[0][i]);
//	printf("\n");
	
    do
    {
      for(i=1;i<N;i++)
	   for(j=0;j<N-i;j++)
	  	a[i][j]=a[i-1][j]+a[i-1][j+1];
	  	
//	  for(i=0;i<N;i++)
//	   {
//	    for(j=0;j<N;j++)
//	   	printf("%d ",a[i][j]);
//	   	printf("\n");
//	   }
//printf("%d\n",a[N-1][0]);
//	   system("pause");
	   
	  if(a[N-1][0]==M) 
	  {
	  	for(i=0;i<N;i++)
	  	{
	  	  printf("%d",a[0][i]);
	  	  i==N?printf("\n"):printf(" ");
	    }
	    break;
	  }
    }while(next_permutation(a[0],a[0]+N));  //不断的生成新序列
}

   总结:

   好好看书非常重要……先把书本的学会了再说……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现RNN+MCTS来进行序列生成任务的方法如下: 1. 准备数据集:准备一个合适的数据集,包含输入序列和对应的输出序列。 2. 实现RNN模型:使用PyTorch实现一个RNN模型,作为序列生成任务的基础模型。可以使用LSTM或GRU,这两种模型在序列建模领域中表现较好。 3. 实现MCTS算法:实现一个基于MCTS算法的序列生成器。在每个时间步,该生成器会基于当前的序列状态进行一定程度的探索,并返回一个新的序列状态,以及一个评估值。 4. 训练RNN模型:使用准备好的数据集对RNN模型进行训练。在每个时间步,将当前的RNN模型状态作为输入,运行MCTS算法得到一个新的序列状态,将其作为下一个时间步的输入,直到生成整个序列。使用生成序列与目标序列之间的误差作为损失函数,使用反向传播算法进行训练。 5. 生成序列:使用训练好的RNN模型和MCTS算法,可以生成新的序列。在每个时间步,将当前的RNN模型状态作为输入,运行MCTS算法得到一个新的序列状态,将其作为下一个时间步的输入,直到生成整个序列。 代码实现: 以下是一个简单的RNN+MCTS的代码实现。假设我们要生成一个长度为10的二进制序列,其中第i个位上的数字是前i-1个位上数字的和的模2。例如,前3位应该是:0, 1, 1。 ```python import torch import torch.nn as nn import torch.optim as optim import numpy as np # 定义RNN模型 class RNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(RNN, self).__init__() self.hidden_size = hidden_size self.i2h = nn.Linear(input_size + hidden_size, hidden_size) self.i2o = nn.Linear(input_size + hidden_size, output_size) self.softmax = nn.LogSoftmax(dim=1) def forward(self, input, hidden): combined = torch.cat((input, hidden), 1) hidden = self.i2h(combined) output = self.i2o(combined) output = self.softmax(output) return output, hidden def initHidden(self): return torch.zeros(1, self.hidden_size) # 定义MCTS算法 class MCTS: def __init__(self, rnn_model): self.rnn_model = rnn_model def evaluate(self, sequence): # 将输入序列转换为张量 input_tensor = torch.tensor(sequence, dtype=torch.float).view(-1, 1, 1) hidden = self.rnn_model.initHidden() # 运行RNN模型,得到预测值和新的隐藏状态 for i in range(input_tensor.size()[0]): output, hidden = self.rnn_model(input_tensor[i], hidden) prediction = torch.argmax(output) # 计算评估值 error = torch.abs(prediction - sequence[-1]) value = 1 / (1 + error.item()) return value def search(self, sequence, n_iter=1000): for i in range(n_iter): # 复制当前序列 new_sequence = sequence.copy() # 随机选择一个位置进行翻转 index = np.random.randint(len(new_sequence)) new_sequence[index] = 1 - new_sequence[index] # 计算评估值 value = self.evaluate(new_sequence) # 更新序列 if value > sequence[-1]: sequence = new_sequence + [value] return sequence[:-1] # 训练RNN模型 input_size = 1 hidden_size = 128 output_size = 2 learning_rate = 0.01 rnn_model = RNN(input_size, hidden_size, output_size) optimizer = optim.Adam(rnn_model.parameters(), lr=learning_rate) n_epochs = 1000 for epoch in range(n_epochs): sequence = [0, 1, 1, 0, 1, 0, 1, 1, 0, 1] hidden = rnn_model.initHidden() optimizer.zero_grad() # 运行MCTS算法,得到新的序列状态 mcts = MCTS(rnn_model) sequence = mcts.search(sequence) # 计算损失函数并进行反向传播 input_tensor = torch.tensor(sequence[:-1], dtype=torch.float).view(-1, 1, 1) target_tensor = torch.tensor(sequence[1:], dtype=torch.long) loss = nn.NLLLoss()(rnn_model(input_tensor, hidden)[0].squeeze(), target_tensor) loss.backward() optimizer.step() print('Epoch: {}, Loss: {}'.format(epoch, loss.item())) # 生成序列 sequence = [0, 1, 1, 0, 1, 0, 1, 1, 0] hidden = rnn_model.initHidden() for i in range(10): input_tensor = torch.tensor(sequence[-1], dtype=torch.float).view(-1, 1, 1) output, hidden = rnn_model(input_tensor, hidden) prediction = torch.argmax(output) mcts = MCTS(rnn_model) sequence = sequence + [mcts.search(sequence + [prediction.item()])[i+1]] print(sequence) ``` 在上述代码中,我们首先定义了一个`RNN`类,表示我们要使用的RNN模型。该模型包含一个输入层,一个隐藏层和一个输出层。在每个时间步,模型将当前输入和隐藏状态作为输入,输出一个预测值和新的隐藏状态。 接下来,我们定义了一个`MCTS`类,表示我们要使用的MCTS算法。该算法基于当前序列状态进行一定程度的探索,并返回一个新的序列状态,以及一个评估值。在这个例子中,我们使用了一个非常简单的评估函数,即计算预测值与目标值之间的误差,然后将其倒数作为评估值。 在训练过程中,我们首先生成一个起始序列。然后,我们运行MCTS算法,得到一个新的序列状态。使用该状态作为输入,我们运行RNN模型,得到一个预测值和新的隐藏状态。我们将预测值与目标值之间的误差作为损失函数,使用反向传播算法进行训练。 最后,我们使用训练好的RNN模型和MCTS算法,生成一个新的序列。在每个时间步,我们将当前的RNN模型状态作为输入,运行MCTS算法得到一个新的序列状态,将其作为下一个时间步的输入,直到生成整个序列
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值