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

貌似用了挺复杂的dp ,应该是写丑了
把每个点分成两个状态,一个奇数一个偶数,边往下走边保存第一次遇到某个状态的步数,当下一个状态是前面出现过的状态可以直接计算再一次回到目前的状态,否则往下一个状态走。妥妥写丑了... 刚刚突然发现原来我忽略了 pi <= i ,好吧,我自己把题目变难了,我貌似解决了这样的问题Can you solve this task without statement pi ≤ i? I don't know the solution, it seems difficult.
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long ll; const int MAX_N = 1e3 + ;
const int MOD = 1e9 + ;
int N;
int p[MAX_N],sta[MAX_N],dp[ * MAX_N]; int cal(int x) {
return (!sta[x]) * (N + ) + x;
} int main()
{
scanf("%d",&N);
fill(dp + ,dp + * (N + ) + ,-);
for(int i = ; i <= N; ++i) {
scanf("%d",&p[i]);
} sta[] = ;
int now = ,ans = ,next;
while(now != N + ) {
if(dp[cal(now)] == -)
dp[ cal(now)] = ans;
if(sta[now] % ) {
next = p[now];
} else {
next = now + ;
} int nowid = cal(now),nextid;
sta[next] = !sta[next];
nextid = cal(next);
if( dp[ cal(next) ] != -) {
ans = ( (ll)ans + (dp[nowid] - dp[nextid]) + MOD + ) % MOD;
sta[now] = (sta[now] + ) % ;
sta[next] = !sta[next];
} else {
now = next;
ans = (ans + ) % MOD;
}
} printf("%d\n",ans);
return ;
}

cf div2 239 D的更多相关文章

  1. cf div2 234 D

    D. Dima and Bacteria time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. 离线dfs CF div2 707 D

    http://codeforces.com/contest/707/problem/D 先说一下离线和在线:在线的意思就是每一个询问单独处理复杂度O(多少多少),离线是指将所有的可能的询问先一次都处理 ...

  3. cf div2 236 D

    D. Upgrading Array time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. cf div2 237 D

    D. Minesweeper 1D time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  5. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. cf div2 238 c

    C. Unusual Product time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. cf div2 235 D

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  8. cf div2 234 E

    E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...

  9. CF div2 D BFS

    http://codeforces.com/contest/676/problem/D 题目大意: 勇者去迷宫杀恶龙.迷宫是有n*m的方格子组成的.迷宫上有各种记号,这些记号表达着能走的方向.当且仅当 ...

随机推荐

  1. [php]如何更改wamp默认的mysq空密码?

    最近刚开始学php,爬坑中.上午看了会儿书,下了个源码,把文件部署到www下之后,也导入了sql文件,但是进入之后显示 = =  一开始不知道是什么.后来百度之后发现是mysql的问题,应该是源码中p ...

  2. JS中的函数,Array对象,for-in语句,with语句,自定义对象,Prototype

    一)函数 A)JS中的函数的定义格式: function add(a,b) { var sum = a+b; document.write("两个数的和是:" + sum); // ...

  3. 金融系列14《QPBOC交易流程》

    中国银行金融IC卡qPBOC交易时间(不含终端处理时间)要求:IC卡私钥长度1024位,交易时间 <= 500ms, 1152 <=600 ATR=3B8D800100814D220886 ...

  4. IIS 配置错误解决方法集合

    问题:405 - 不允许用于访问此页的 HTTP 谓词 解决:IIS处理程序映射中添加模块映射,模块选择:ServerSideIncludeModule,名称:SSINC-HTML

  5. nginx 配置 ThinkPHP Rewrite

    server { listen 80; server_name www.funsion.com; root /www/web/funsion; index index.php; location / ...

  6. [原创]pg_shard使用场景及功能测试

    pg_shard是一个PostgreSQL的sharding extension.可以用于Shards.Replicates tables和高可用.它可以在不修改Applications的情况下无缝分 ...

  7. 关于Swift中实现Lazy initialize的方式

    在oc中我们通过 -(CardMatchingGame *)game { if(!_game) _game=[[CardMatchingGame alloc] initWithCardCount:[s ...

  8. MVC4.0 WebApi如何设置api支持namespace

    1.自定义HttpControllerSelector /// <summary> /// 设置api支持namespace /// </summary> public cla ...

  9. [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法

    [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法 http://blog.csdn.net/sahuso ...

  10. run.do 文件编写说明

    #停止仿真quit -sim#清除信息.main clear #建立物理路径libvlib ./lib/#建立物理路径work_avlib ./lib/work_a/#映射逻辑库至物理路径vmap b ...