Time Limits: 3000 MS Memory Limits: 65536 KB
64-bit interger IO format: %lld Java class name: Main
Description
There is a river, which contains n stones from left to right. These stones are magic, each
one has a magic number Ai which means if you stand on the ith stone, you can jump to (i +1)th stone, (i+2)th stone, ..., (i+Ai)th stone(when i+Ai > n, you can only reach as far as n), at first, you stand on 1th stone, you want to calculate the number of ways to reach the nth stone.
Notice: you can not jump from right to left!
Input
Input starts with an integer T(1 <= T <= 10), denoting the number of test cases. Each test case contains an integer n(1 <= n <= 1e5), denoting the number stones. Next line contains n integers Ai(1 <= Ai <= 1e8).
Output
For each test case, print the number of way to reach the nth stone module 1e9+7.
Sample Input
351 2 3 4 511022 1
Output for Sample Input
311
校赛那会儿的题目,主要操作就是区间更新、单点查询,树状数组和线段树都可以,树状数组的简单很多也好写很多,线段树嘛,线段树的模版题自行体会。
主要解题思路:刚开始肯定是1号石头初始化为1,其他的都是0,这个相信很好理解,然后就是往后覆盖区间,但却不是简单的覆盖,为了弄懂到底如何操作举个例子先,比如我到3号有a种路线,到4号有几种(假设4号只与3号连通)?当然跟3号一样,就是a种,如果3号可以跳跃的步数不止1即可以跳到5号上呢?此时会变成P5=P4+P3,即3号过来有三种,4号过来有1种(只能跳一步过来)。加起来就是4种,3->5一种,3->4>-5三种,推广出去就是不停地往后覆盖自己这个点的可到达情况数就好了,最后的答案当然就是Pn
树状数组代码:
#include #include #include #include #include #include #include #include #include #include #include #include #include
线段树代码:
#include #include #include #include #include #include #include #include #include #include #include #include #include