D. Long Path

D. Long Path
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day, little Vasya found himself in a maze consisting of (n + 1) rooms, numbered from 1 to (n + 1). Initially, Vasya is at the first room and to get out of the maze, he needs to get to the (n + 1)-th one.

The maze is organized as follows. Each room of the maze has two one-way portals. Let's consider room number i (1 ≤ i ≤ n), someone can use the first portal to move from it to room number (i + 1), also someone can use the second portal to move from it to room number pi, where 1 ≤ pi ≤ i.

In order not to get lost, Vasya decided to act as follows.

  • Each time Vasya enters some room, he paints a cross on its ceiling. Initially, Vasya paints a cross at the ceiling of room 1.
  • Let's assume that Vasya is in room i and has already painted a cross on its ceiling. Then, if the ceiling now contains an odd number of crosses, Vasya uses the second portal (it leads to room pi), otherwise Vasya uses the first portal.

Help Vasya determine the number of times he needs to use portals to get to room (n + 1) in the end.

Input

The first line contains integer n (1 ≤ n ≤ 103) — the number of rooms. The second line contains n integers pi (1 ≤ pi ≤ i). Each pi denotes the number of the room, that someone can reach, if he will use the second portal in the i-th room.

Output

Print a single number — the number of portal moves the boy needs to go out of the maze. As the number can be rather large, print it modulo 1000000007 (109 + 7).

Examples
input
Copy
2
1 2
output
Copy
4
input
Copy
4
1 1 2 3
output
Copy
20
input
Copy
5
1 1 1 1 1
output
Copy
62

题目的意思是叫我们求出从1号房间到达n+1号房间需要借用几次入口,入口有两种类型,一种是到达1和当前所在房间之间的房间,另外一种是到达当前所在房间的上一个房间,每到达一个房间都要在该房间的天花板上画一个十字,画完后当该房间的十字数量为奇数时,使用上面说述的第一种,否则就使用上面所述的第二种入口

我们会发现,当我们第一次进入一个房间时,接下来肯定会往下降,进入房间p[i],我们设dp[i](i为当前我们所在的房间号)为从第一次从i到进入i+1号房间所需要的入口次数,所以dp[i]就为

for(int j = p[i]; j < i; j ++) dp[i] =  (dp[i] + dp[j]) % inf;

不过这里我们要初始化dp[i]为2,因为上述计算少加了两次,一次是第一次进入i号房间,另为一次时重新回到i+1号后进入i+1号房间,最后的答案就是dp[i]的累和

#include <bits/stdc++.h>

using namespace std;
typedef __int64 ll;
const int inf = 1e9 + 7;
ll p[1010];
ll dp[1010];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++){
        scanf("%I64d", &p[i]);
    }
    dp[1] = 2;
    ll ans = 2;
    for(int i = 2; i <= n; i ++){
        dp[i] = 2;
        for(int j = p[i]; j < i; j ++){
            dp[i] = (dp[i] + dp[j]) % inf;
        }
        ans = (ans + dp[i]) % inf;
    }
    cout << ans << endl;
    return 0;
}

import torch import os import torch.nn as nn import torch.optim as optim import numpy as np import random class Net(nn.Module): def init(self): super(Net, self).init() self.conv1 = nn.Conv2d(1, 16, kernel_size=3,stride=1) self.pool = nn.MaxPool2d(kernel_size=2,stride=2) self.conv2 = nn.Conv2d(16, 32, kernel_size=3,stride=1) self.fc1 = nn.Linear(32 * 9 * 9, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 2) def forward(self, x): x = self.pool(nn.functional.relu(self.conv1(x))) x = self.pool(nn.functional.relu(self.conv2(x))) x = x.view(-1, 32 * 9 * 9) x = nn.functional.relu(self.fc1(x)) x = nn.functional.relu(self.fc2(x)) x = self.fc3(x) return x net = Net() criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9) folder_path = 'random_matrices2' # 创建空的tensor x = torch.empty((40, 1, 42, 42)) # 遍历文件夹内的文件,将每个矩阵转化为tensor并存储 for j in range(40): for j in range(40): file_name = 'matrix_{}.npy'.format(j) file_path = os.path.join(folder_path, file_name) matrix = np.load(file_path) x[j] = torch.from_numpy(matrix).unsqueeze(0) #y = torch.cat((torch.zeros(20), torch.ones(20))) #y = torch.cat((torch.zeros(20, dtype=torch.long), torch.ones(20, dtype=torch.long))) y = torch.cat((torch.zeros(20, dtype=torch.long), torch.ones(20, dtype=torch.long)), dim=0) for epoch in range(10): running_loss = 0.0 for i in range(40): inputs = x[i] labels = y[i].unsqueeze(0) labels = labels.long() optimizer.zero_grad() outputs = net(inputs) #loss = criterion(outputs, labels) loss = criterion(outputs.unsqueeze(0), labels.float()) loss.backward() optimizer.step() running_loss += loss.item() print('[%d] loss: %.3f' % (epoch + 1, running_loss / 40)) print('Finished Training') 报错:RuntimeError: Expected target size [1, 2], got [1],怎么修改?
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值