题目:

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
21 2

output
4

input
41 1 2 3

output
20

input
51 1 1 1 1

output
62

题意:如果当前到达点i的进入次数cnt为奇数 则可以到达p[i]位置 如果是偶数 可以到达i+1位置 求从1走到n+1需要的总步数 

思路:第一次走进i房间的进入次数cnt为1 是奇数 会往后退回p[i]位置 但是此刻p[i]位置的cnt也变成了奇数 因为当时只有为偶数才能向前走 那么会退回到p[p[i]]位置 不断递归因此 dp[i]记为走到当前位置需要的总步数 状态转移方程为dp[i+1]=dp[i]+1+(dp[i]-dp[p[i]])+1 即为dp[i+1]=2*dp[i]-dp[p[i]]+2

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=1e3+10;
const int mod=1e9+7;
int n;
int p[maxn];
ll dp[maxn];int main(){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&p[i]);}dp[1]=0;for(int i=1;i<=n;i++){dp[i+1]=(2*dp[i]-dp[p[i]]+2+mod)%mod;}printf("%lld\n",(dp[n+1]+mod)%mod);return 0;
}

转载于:https://www.cnblogs.com/whdsunny/p/10514150.html

Codeforces 408D Long Path (DP)相关推荐

  1. Codeforces 919D Substring (拓扑图DP)

    Codeforces 919D Substring (拓扑图DP) 手动博客搬家: 本文发表于20180716 10:53:12, 原地址https://blog.csdn.net/suncongbo ...

  2. [CodeForces 332B]Maximum Absurdity[DP]

    题目链接: [CodeForces 332B]Maximum Absurdity[DP] 题意分析: 寻找两个不重叠的长度为k的子串,使得它们之和最大. 解题思路: 第一想法是,处理出从这个点开始,长 ...

  3. 【CodeForces 1042B --- Vitamins】DP+位运算

    [CodeForces 1042B --- Vitamins]DP+位运算 题目来源:点击进入[CodeForces 1042B - Vitamins] Description Berland sho ...

  4. [CodeForces 300D Painting Square]DP

    http://codeforces.com/problemset/problem/300/D 题意:每一次操作可以选一个正方形,令边长为n,如果n为奇数那么可以从中间画一个十字,分成4个大小相等的边长 ...

  5. Codeforces 1322D Reality Show (DP)

    题目链接 https://codeforces.com/contest/1322/problem/D 题面写得非常模糊,很容易读错题,建议参考翻译:https://www.luogu.com.cn/p ...

  6. CodeForces - 1579G Minimal Coverage(dp)

    题目链接:点击查看 题目大意:给出 nnn 个长度不同的木棍.设第 i−1i-1i−1 次放置木棍后的终点为 xxx,那么第 iii 个木棍有且仅有两种放置方法: 放到 [x+1,x+a[i]][x+ ...

  7. CodeForces - 1562E Rescue Niwen!(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的字符串 sss,将其子串按顺序展开成序列,即 {s1,s1s2,⋯,s1s2-sn,s2,s2s3,s2s3-sn,s3,s3s4,⋯,sn−1 ...

  8. CodeForces - 1551E Fixed Points(dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,需要求出删掉最少的数字,使得剩下的数字至少有 kkk 个位置满足 a[i]=ia[i]=ia[i]=i 成立 题目分析:看完数据范围不难想 ...

  9. CodeForces - 1484E Skyline Photo(dp+单调栈)

    题目链接:点击查看 题目大意:给出 nnn 个建筑,每个建筑有一个高度和一个美丽值,现在要求划分为数个连续的区间,使得所有区间的贡献之和最大,其中每个区间的贡献值为,区间中高度最低的建筑物的美丽值 题 ...

最新文章

  1. JAVA IO操作中的IN和OUT问题
  2. 机械爪的带有压力反馈的控制实验
  3. SecureCRT中文绿色免安装版修改字体颜色
  4. matlab 低秩矩阵分解,低秩分解的matlab代码看不懂,分解的两个矩阵在哪呀??...
  5. 将 ASP.NET Core 2.0 项目升级至 ASP.NET Core 2.1 RC 1
  6. 电话光端机应用范围解析!电话光端机主要应用在哪些领域?
  7. Doc2Vec训练相似文章识别模型
  8. 腾讯阿里都在用!机器学习最热研究方向入门,附学习路线图
  9. Microsoft .Net Remoting系列专题之二
  10. What we learn before born?
  11. oracle--索引的使用
  12. paip. dsl 编程语言优点以及 常见的dsl
  13. 华为的人才体系:任正非这样管理19万员工
  14. 华为digix算法大赛2020机器学习赛道-搜索相关性初赛A/B榜rank1
  15. linux 内核rps,Linux内核软RPS实现网络接收软中断的负载均衡分发
  16. 程序员30+后的困惑焦虑,出路在哪里?
  17. 告别360全家桶,安利一波电脑必备软件
  18. 让星星⭐月亮告诉你,打印完全格式的日期格式 包含 年月日时分秒毫秒
  19. 当女友让程序员去买西瓜...... | 每日趣闻
  20. new BigDecimal比较大小

热门文章

  1. ADO.NET中在C/S模式中使用的连接池
  2. php原生sql语法,thinkphp执行原生SQL语句的实现方法
  3. java单击切换div_[Java教程]点击同一按钮实现div的隐藏与现实切换
  4. css类选择器或逻辑,深入理解CSS中选择器的逻辑处理
  5. CSDN挑战编程——《数学问题》
  6. 操作系统上机题目(多线程2)
  7. 160 - 7 aLoNg3x.2
  8. 颜色缩减 -利用指针、迭代器、动态地址实现访问像素
  9. 形参与实参在函数中的传递
  10. java中Scanner类中 next()与nextLine()的区别