B. 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 numberpi, 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 pidenotes 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).

Sample test(s)
input
2
1 2
output
4
input
4
1 1 2 3
output
20
input
5
1 1 1 1 1
output
62
题意:Vasya在一个由各种房间组成的迷宫里,房间的编号为(1~n),开始Vasya在1号房间,每个房间都有两个门,第一扇门通往下一个房间,第二扇门通往任意一个房间,Vasya决定每到一个房间就在该房间标记一次,如果标记数为奇数就打开第二扇门,否则打开第一扇门,问Vasya走出迷宫需要的步数。
思路:dp[i]表示偶数次到第i个房间的步数,则状态转移方程为dp[i] = dp[i-1]+1+dp[i-1]-dp[a[i]-1]+1;
 #include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int N=;
const int MOD=;
long long dp[N],a[N];
int main()
{
int n;
while(~scanf("%d",&n))
{
for (int i = ; i <= n; i++)
cin>>a[i];
dp[] = -;
dp[] = ;
for (int i = ; i <= n; i++)
{
dp[i] = (dp[i-]++dp[i-]-dp[a[i]-]+)%MOD;
}
cout<<((dp[n]+)%MOD+MOD)%MOD<<endl;
}
return ;
}

Codeforces Round #239 (Div. 1)的更多相关文章

  1. Codeforces Round #239 (Div. 1)C, 407C

    题目链接:http://codeforces.com/contest/407/problem/C 题目大意:给一个长度为n的数列,m次操作,每次操作由(li, ri, ki)描述,表示在数列li到ri ...

  2. Codeforces Round #239 (Div. 2)

    做了三个题,先贴一下代码...终于涨分了 A. Line to Cashier 水题 #include <iostream> #include <cstdio> #includ ...

  3. Codeforces Round #239 (Div. 2) C. Triangle

    time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:standard o ...

  4. Codeforces Round #239 (Div. 1) 二项式差分

    C - Curious Array 思路:对于区间[l, r]每个数加上C(i - l + k, k), 可以在l处+1, 在r+1处-1, 然后做k+1次求前缀和操作,然后就可以写啦. 然后逐层求前 ...

  5. Codeforces Round #239(Div. 2) 做后扯淡玩

    今天补了下 cf 239div2 顿时信心再度受挫 老子几乎已经木有时间了啊 坐着等死的命.哎!!! 到现在还只能做大众题,打铁都不行. 每次D题都是有思路敲错,尼玛不带这么坑爹的. 哎!不写了,写这 ...

  6. 【Codeforces Round #239 (Div. 1) B】 Long Path

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] DP,设f[i]表示第一次到i这个房间的时候传送的次数. f[1] = 0,f[2] = 2 考虑第i个位置的情况. 它肯定是从i- ...

  7. 【Codeforces Round #239 (Div. 1) A】Triangle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最后的直角三角形可以通过平移,将直角顶点移动到坐标原点. 然后我们只要枚举另外两个点其中一个点的坐标就好了. x坐标的范围是[1.. ...

  8. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. python_ 学习笔记(hello world)

    python中的循环语句 循环语句均可以尾随一个else语句块,该块再条件为false后执行一次 如果使用break跳出则不执行. for it in [1,2,3,4]: print(it,end= ...

  2. Oracle 控制文件(CONTROLFILE)

    一.Oracle 控制文件 为二进制文件,初始化大小由CREATE DATABASE指定,可以使用RMAN备份 记录了当前数据库的结构信息,同时也包含数据文件及日志文件的信息以及相关的状态,归档信息等 ...

  3. 截取命令cut命令、awk命令、sed命令

    cut命令 截取以制表符tab为分隔符的第一列 cut -f 1test.txt 截取以":"为分隔符的第一列,如果比较规律的文件,可以自己设定分隔符 cut -f 1 -d ': ...

  4. python virtualenv 虚拟环境的应用

    为什么要使用python的虚拟环境呢?: 首先我们来说不实用虚拟环境的情况: 在Python应用程序开发的过程中,系统安装的Python3只有一个版本:3.7.所有第三方的包都会被pip3安装到   ...

  5. 【Codeforces 466B】Wonder Room

    [链接] 我是链接,点我呀:) [题意] 让你把长为a,宽为b的房间扩大(长和宽都能扩大). 使得它的面积达到6*n 问你最小的能满足要求的面积是多少 输出对应的a和b [题解] 假设a< b ...

  6. 猎豹CEO傅盛:与周鸿祎、雷军、马化腾、马云的的相爱相杀

    百度百科:傅盛,男,1978年3月6日出生在江西景德镇,毕业于山东工商学院信息管理与信息系统专业. 2003年加入3721公司.2005年加入奇虎360,带领团队打造了安全类软件360安全卫士.200 ...

  7. [K/3Cloud]关于K/3 Cloud v2.0升级补丁的疑问

    现在除了K/3 Cloud v2.0的正式版之外,后续又发布了SP1和SP2补丁包.改善了一些问题和BUG,有些人之前可能之前装了SP1,有的可能没装过.在这里我说明一下: 不管装没装过补丁直接打SP ...

  8. HDU5266 LCA 树链剖分LCA 线段树

    HDU5266 LCA Description 给一棵 n 个点的树,Q 个询问 [L,R] : 求点 L , 点 L+1 , 点 L+2 -- 点 R 的 LCA. Input 多组数据. The ...

  9. Drools等规则引擎技术对比分析

    项目中需要设计开发一个规则引擎服务,于是调研了业界常用的规则引擎. 常见的规则引擎如下: Ilog JRules 是最有名的商用BRMS: Drools 是最活跃的开源规则引擎: Jess 是Clip ...

  10. Oracle GV$VIEW

    The catclustdb.sql script creates the GV$ views. Run this script if you do not create your database ...