题意:

起点为1,终点为n+1,对应第i个各点,如果我奇数次到达i点,那么下一步走到a【i】的位子,如果是偶数次到达,那么下一步走到i+1的位子。
问从1走到n+1一共需要走多少步?结果对1e9+7取模。

题目

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

2
1 2

Output

4

Input

4
1 1 2 3

Output

20

Input

5
1 1 1 1 1

Output

62

分析:

1.定义状态dp[x][y]为从x走到y需要走多少次。
2.如果我们要到达某个点y,则要到达y-1的点,第一次到达y-1,按照题目要求,奇数次到达y-1点,会到达s【y-1】,所以第二次到达y-1,需从s【y-1】到达y-1:故推出公式,

dp【x】【y】=dp【x】【y-1】+【s【y-1】】【y-1】+1;

AC代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int M=1e3+10;
const int mod=1e9+7;
int dp[M][M],s[M];//设定dp【i】【j】表示从i走到j一共需要多少步。
int n;
void dfs(int x,int y)
{if(x==y){dp[x][y]=1;return ;}if(dp[x][y])return ;/**如果要到达y,有两种情况,第一次到达y-1,回到s[i-1],在偶数次到达y-1时,可直接到达y*/dfs(x,y-1);dfs(s[y-1],y-1);///考虑到s【i】<=i,那么要想到i+1这个格点去,那么一定是从第i个格点走过去的。所以推出普遍公式dp[x][y]=dp[x][y-1]+dp[s[y-1]][y-1]+1;/**那么x-->y,需要先第一次从x-->y-1,由规则到达【y-1】,再从是【y-1】->y-1*/dp[x][y]%=mod;
}
int main()
{while(~scanf("%d",&n)){memset(dp,0,sizeof(dp));for(int i=1; i<=n; i++)scanf("%d",&s[i]);dfs(1,n+1);printf("%d\n",dp[1][n+1]-1);}return 0;
}

Long Path CodeForces - 407B(动态规划+思维+公式推导)相关推荐

  1. 采用动态规划思维求解数塔问题,c++实现

    采用动态规划思维求解数塔问题,c++实现 数塔问题 算法实现 结果 数塔问题 问题描述: 加粗样式从塔顶往下走,如何走过的步数最大 问题分析: 数塔游戏(5层) 从塔顶到塔底,走过的值最大 从第一层开 ...

  2. [codeforces 1379B] Dubious Cyrpto 公式推导

    Codeforces Round #657 (Div. 2)   参与排名人数8684   错过了难得17:00比赛,可惜 [codeforces 1379B]   Dubious Cyrpto  公 ...

  3. codeforces数学1600day4[贪心数学公式推导CodeForces - 1151D ,思维CodeForces - 1085C,数论同余+组合计数 CodeForces - 1056B]

    A - Stas and the Queue at the Buffet CodeForces - 1151D 题目大意:就是给你n个人在排队,每个人都有一个ai值和bi值,每个人的不满意度就是f(i ...

  4. Codeforces Round #772 (Div. 2) D. Infinite Set (动态规划+思维)

    题目链接 https://codeforces.com/contest/1635/problem/D 题面 题意 输入一个n表示数组 aaa 的长度,然后输入一个 p,然后输入n个不同的元素,问在 [ ...

  5. Ehab and Path-etic MEXs CodeForces - 1325C(思维+贪心)

    You are given a tree consisting of n nodes. You want to write some labels on the tree's edges such t ...

  6. codeforces 有意思的思维题 1 ~ 15

    codeforces 思维题 1.给定数组,求满足i < j and ai * aj = i + j的数对数量 2.第 i 步向前跳 i 步或后退 1 步 3.给两个点,求正方形的另两个点 4. ...

  7. CodeForces - 364A Matrix(思维+数学)

    题目链接:点击查看 题目大意:给出一个长度为 n 的,只由十进制数字组成的字符串 s,可以构造出一个大小为 n * n 的矩阵,构造方法如下:b[ i ][ j ] = s[ i ] * s[ j ] ...

  8. 洛谷 - P2766 最长不下降子序列问题(最大流+动态规划+思维建边)

    题目链接:点击查看 题目大意:给出一个由n个数组成的序列,有三个子问题: 求出当前数列的最长不下降子序列的长度len 如果每个数最多只能使用一次,问最多可以组成多少个长度为len的最长不下降子序列 如 ...

  9. Task On The Board CodeForces - 1367D(思维)

    Polycarp wrote on the board a string s containing only lowercase Latin letters ('a'-'z'). This strin ...

最新文章

  1. 自助收银导致货损增加,阿里安全如何用视频识别AI应对?
  2. python3 导入自定义函数
  3. 开发日记-20190504 关键词 汇编语言(三)
  4. 【机器学习】孤立森林-一个通过瞎胡乱分进行异常检测的算法
  5. c mysql 设置字符集_MYSQL字符集设置的方法详解(终端的字符集)
  6. Linux创建文件【命令】
  7. linux下修改当前目录下图像文件并删除
  8. 为什么使用累积分布函数处理后像素值会均匀分布_亿万级数据处理的高效解决方案
  9. 常用php操作redis命令整理(五)ZSET类型
  10. 给iOS开发新手送点福利,简述UIPageControl的属性和用法
  11. golang 腾讯云直播开发日记(一)
  12. python爬虫小项目价格_爬虫项目怎么收费?
  13. 《2017中国云计算评测报告》
  14. 阿里职位层级(附P级详细要求)
  15. 树莓派GPIO远程控制继电器
  16. 谭波 oracle,行动营复盘
  17. 用决策树预测获胜球队
  18. 控制搜索引擎蜘蛛的爬行收录
  19. QT与MATLAB混合编程
  20. 《The Art Of Effective Rigging In Blender》笔记:手臂部分的IK/FK切换

热门文章

  1. Android之提示Unable to instantiate fragment***MyLikeFragment .could not find Fragment constructor
  2. IOS之学习笔记二(块)
  3. C和指针之多维数组一行存满后会轮到下一行
  4. LeetCode之Add Digits
  5. 《零基础看得懂的C语言入门教程 》——(六)轻轻松松了解C语言的逻辑运算
  6. pythonweb服务器怎么让别人访问_Django配置让其他电脑访问网站
  7. 软件项目组织管理(二、三)项目管理与信息技术环境、项目管理过程组
  8. mysql如何快速插入一千万条数据_如何快速安全的插入千万条数据?
  9. python长度多少_用Python求最长子串长度快速版
  10. 一个人动情之后的表现......