【codeforces】New Year Transportation

ゝ一纸荒年。 2022-09-26 11:55 137阅读 0赞

New Year Transportation

Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Submit Status

Description

New Year is coming in Line World! In this world, there are n cells numbered by integers from 1 to n, as a 1 × n board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells.

So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of n - 1positive integers a1, a2, …, a**n - 1. For every integer i where 1 ≤ i ≤ n - 1 the condition 1 ≤ a**i ≤ n - i holds. Next, he made n - 1portals, numbered by integers from 1 to n - 1. The i-th (1 ≤ i ≤ n - 1) portal connects cell i and cell (i + a**i), and one can travel from cell i to cell (i + a**i) using the i-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell(i + a**i) to cell i using the i-th portal. It is easy to see that because of condition 1 ≤ a**i ≤ n - i one can’t leave the Line World using portals.

Currently, I am standing at cell 1, and I want to go to cell t. However, I don’t know whether it is possible to go there. Please determine whether I can go to cell t by only using the construted transportation system.

Input

The first line contains two space-separated integers n (3 ≤ n ≤ 3 × 104) and t (2 ≤ t ≤ n) — the number of cells, and the index of the cell which I want to go to.

The second line contains n - 1 space-separated integers a1, a2, …, a**n - 1 (1 ≤ a**i ≤ n - i). It is guaranteed, that using the given transportation system, one cannot leave the Line World.

Output

If I can go to cell t using the transportation system, print “YES”. Otherwise, print “NO”.

Sample Input

Input

  1. 8 4
  2. 1 2 1 2 1 2 1

Output

  1. YES

Input

  1. 8 5
  2. 1 2 1 2 1 1 1

Output

  1. NO

Hint

In the first sample, the visited cells are: 1, 2, 4; so we can successfully visit the cell 4.

In the second sample, the possible cells to visit are: 1, 2, 4, 6, 7, 8; so we can’t visit the cell 5, which we want to visit.

依次循环下去,看能不能找到t;

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. using namespace std;
  7. const int N = 3*1e4+10;
  8. int a[N];
  9. int main() {
  10. int n,k;
  11. while(scanf("%d%d",&n,&k)!=EOF) {
  12. for(int i=1; i<n; i++) {
  13. scanf("%d",&a[i]);
  14. }
  15. a[n]=1;
  16. int i=1;
  17. while(i<=n&&i!=k) {
  18. i=i+a[i];
  19. }
  20. if(i==k)
  21. printf("YES\n");
  22. else
  23. printf("NO\n");
  24. }
  25. return 0;
  26. }

发表评论

表情:
评论列表 (有 0 条评论,137人围观)

还没有评论,来说两句吧...

相关阅读

    相关 MySQL Transportable Tablespace

    将大的InnoDB表从一个实例,移动或者复制到另一个实例,有很多的方法,在5.6之前常用的是通过物理或者逻辑备份来实现。在5.6.6+的版本中,用到了一种基于表空间迁移的快速方

    相关 New Year, New Me

    新的一年,新的开始 2017年,是我的毕业年,也是我开始参加工作的一年,这一年,很忙碌,很充实,却也很累,但也在一一实现着17年的计划,直到今日,十之八九得到了一个圆满的