CF 407B Long Path[观察性质 DP]
1 second
256 megabytes
standard input
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.
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.
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).
2
1 2
4
4
1 1 2 3
20
5
1 1 1 1 1
62
官方题解
In this problem you had to simulate route of character in graph.
Note that if you are in vertice i, then edges in all vertices with numbers less than i are turned to pi. It gives us opportunity to see a recurrence formula: let dpi be number of steps, needed to get from vertice to vertice i, if all edges are rotated back, into pi. Then dpi + = 2dpi + - dppi. Answer will be dpn + .
很明显要用到DP
考虑d+1,只可能从i走到,但是第一次到达i,下一步会走到pi,然后又到达i
可以发现到i时i之前的全是偶数个叉,所以这两次的传送门数是一样的,第二次只是少了到p[i]到次数而已
d[i]表示第一次到达i的传送门次数,d[i]=d[i]+1+(d[i]-d[p[i]])+1
//
// main.cpp
// cf407b
//
// Created by Candy on 9/15/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const ll N=,MOD=1e9+;
int n,p[N];
ll d[N];
void dp(){
for(int i=;i<=n;i++)
d[i+]=(d[i]++(d[i]-d[p[i]])++MOD)%MOD;
}
int main(int argc, const char * argv[]) {
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&p[i]);
dp();
cout<<d[n+];
return ;
}
CF 407B Long Path[观察性质 DP]的更多相关文章
- CodeForces 407B Long Path (DP)
题目链接 题意:一共n+1个房间,一个人从1走到n+1,如果第奇数次走到房间i,会退回到房间Pi,如果偶数次走到房间i,则走到房间i+1,问走到n+1需要多少步,结果对1e9+7取模. 题解:设dp[ ...
- Codeforces 407B Long Path(好题 DP+思维)
题目链接:http://codeforces.com/problemset/problem/407/B 题目大意:一共n+1个房间,一个人从1走到n+1,每次经过房间都会留下一个标记,每个房间有两扇门 ...
- Codeforces 1461F - Mathematical Expression(分类讨论+找性质+dp)
现场 1 小时 44 分钟过掉此题,祭之 大力分类讨论. 如果 \(|s|=1\),那么显然所有位置都只能填上这个字符,因为你只能这么填. scanf("%d",&n);m ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
- CF 337D Book of Evil 树形DP 好题
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...
- 【gdoi2018 day2】第二题 滑稽子图(subgraph)(性质DP+多项式)
题目大意 [gdoi2018 day2]第二题 滑稽子图(subgraph) 给你一颗树\(T\),以及一个常数\(K\),对于\(T\)的点集\(V\)的子集\(S\). 定义\(f(S)\)为点集 ...
- 64. Minimum Path Sum (Graph; DP)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Codeforces 954H Path Counting 【DP计数】*
Codeforces 954H Path Counting LINK 题目大意:给你一棵n层的树,第i层的每个节点有a[i]个儿子节点,然后问你树上的简单路径中长度在1~n*2-2之间的每个有多少条 ...
- Codeforces 954H Path Counting(DP)
题目链接 Path Counting 题意 给定一棵高度为$n$的树,给出每一层的每个点的儿子个数(某一层的所有点儿子个数相同). 令$f_{k}$为长度为$k$的路径条数,求$f_{1}, ...
随机推荐
- sharepoint2010问卷调查(2)-实现问卷的图片调查(采用自定义字段类型)
1. 首先建立个图片库上传图片 并建立文件夹1和2,1下有1.1文件夹,2下2.1文件夹,2.1下有文件夹2.1.1. 在1文件夹下放如下图片: 2.建立自定义字段类型,如下图: 3.部署后建立栏目的 ...
- SVN源码泄露漏洞
SVN(subversion)是源代码版本管理软件,造成SVN源代码漏洞的主要原因是管理员操作不规范.“在使用SVN管理本地代码过程中,会自动生成一个名为.svn的隐藏文件夹,其中包含重要的源代码信息 ...
- 对tableView三种计算动态行高方法的分析
tableView是一个神奇的东西,可以这么说,就算是一个初学者如果能把tableView玩的很6,那编一般的iOS的需求都问题不大了.tableView是日常开发中用烂了的控件,但是关于tableV ...
- Android 手机卫士--打包生成apk维护到服务器
项目打包生成apk过程: 1.生成签名文件,并且指定所在位置 2.使用生成的签名文件,给工程打包生成一个apk 本文地址:http://www.cnblogs.com/wuyudong/p/59033 ...
- android 最简单的自定义圆点view
首先创建一个选择器,用来判断圆点状态,可以根本自己的需求改 <selector xmlns:android="http://schemas.android.com/apk/res/an ...
- OC 动态类型,动态绑定,动态加载
OC 动态类型,动态绑定,动态加载 Objective-C具有相当多的动态特性,基本的,也是经常被提到和用到的有 动态类型(Dynamic typing) 动态绑定(Dynamic binding) ...
- SQL并行与否的性能差异
比较两种代码,核心代码相同,其中一个使用变量保存查询出的结果,另一个直接输出.使用同一变量时,强迫SQL放弃了并行,使用了循环. 测试结果 表'#1699586C'.扫描计数1,逻辑读取186 次 ...
- samba服务配置
使用yum或者apt-get安装 # yum install samba samba-client samba-swat Samba开发环境配置 Acl权限设置 [不是必须.只要保证web目录的所有 ...
- Web API与国际化
软件国际化是在软件设计和文档开发过程中,使得功能和代码设计能处理多种语言和文化习俗,在创建不同语言版本时,不需要重新设计源程序代码的软件工程方法.这在很多成熟的软件开发平台中非常常见.对于.net开发 ...
- NGINX高性能Web服务器详解(读书笔记)
原文地址:NGINX高性能Web服务器详解(读书笔记) 作者:夏寥寥 第4章 Nginx服务器的高级配置 4.1 针对IPv4的内核7个参数的配置优化 说明:我们可以将这些内核参数的值追加到Linu ...