CF 174 (div2) D

Source: Internet
Author: User
Tags terminates
D. Cow Programtime limit per test

2 seconds

Memory limit per test

256 megabytes

Input

Standard input

Output

Standard output

Farmer John has just given the cows a program to play! The program contains two integer variables,XAnd
Y, And performs the following operations on a sequenceA1, bytes,A2, middle..., middle ,...,AN
Of positive integers:

  1. Initially,XBetween = between 1 and
    YWhen = then 0. If, after any step,XLimit ≤ limit 0 orXLatency> latencyN, The program immediately terminates.
  2. The program increases bothXAnd
    YBy a value equalAXSimultaneously.
  3. The program now increasesYBy
    AXWhile decreasing
    XByAX.
  4. The program executes steps 2 and 3 (first step 2, then step 3) repeatedly until it terminates (it may never terminate ). so, the sequence of executed steps may start with: step 2, step 3, step 2, step 3, step 2 and so on.

The cows are not very good at arithmetic though, and they want to see how the program works. Please help them!

You are given the sequenceA2, bytes,A3, middle..., middle ,...,AN. Suppose for eachI
(1 digit ≤ DigitILimit ≤ limitNUpload-sequence 1) we run the program on the sequenceI, Bytes,A2, bytes,A3, middle..., middle ,...,AN.
For each such run output the final valueYIf the program terminates or
-1 if it does not terminate.

Input

The first line contains a single integer,N(2 cores ≤ CoresNLimit ≤ limit 2 · 105). The next line containsNEncryption-rule 1 space separated integers,
A2, bytes,A3, middle..., middle ,...,AN(1 digit ≤ DigitAILimit ≤ limit 109 ).

Output

OutputNLimits-limits 1 lines. On
I-Th line, print the requested value when the program is run on the sequenceI, Bytes,A2, bytes,A3, large ,...AN.

Please do not use the % lld specifier to read or write 64-bit integers in bytes ++. It is preferred to use thecin,
Cout streams or the % I64d specifier.

Sample test (s) Input
42 4 1
Output
368
Input
31 2
Output
-1-1
Note

In the first sample

  1. ForIPriority = Priority 1, priority,XBecomes andY
    Becomes 1 response + response 2 response = Response 3.
  2. ForILimit = Limit 2, limit,XBecomes andY
    Becomes 2 centers + limit 4 centers = Limit 6.
  3. ForILatency = latency 3, latency,XBecomes andY
    Becomes 3 cores + cores 1 cores + cores 4 cores = cores 8.

If you do this directly, you need to use the memory to search for the cumulative increment of y until the end of each x. Note that the memory status here is two-dimensional, because operation 1 is performed when the result reaches x, or 2 has different effects on the results!

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <stack> # include <queue> # define LL long longusing namespace std; const int maxn = 200000 + 5; int n; LL num [maxn]; int vis [maxn] [2]; LL go [maxn] [2]; LL find (int x, int kind) {if (go [x] [kind]) return go [x] [kind]; if (vis [x] [kind] = 1) {return go [x] [kind] =-1;} vis [x] [kind] = 1; if (kind = 1 & x + num [x]> N) {return go [x] [kind] = num [x];} else if (kind = 0 & x-num [x] <= 0) {return go [x] [kind] = num [x];} LL tem = find (x + (kind = 1? Num [x]:-num [x]), (kind + 1) % 2); // This is discussed in kind. 1 indicates that the current operation is 1, 0 indicates the operation 2 if (tem! =-1) return go [x] [kind] = tem + num [x]; else return go [x] [kind] =-1;} int main () {while (cin> n) {memset (vis, 0, sizeof (vis); memset (go, 0, sizeof (go); for (int I = 2; I <= n; I ++) cin> num [I]; LL x, y; for (int I = 2; I <= n; I ++) {y = I-1; LL tem = find (I, 0); if (tem =-1) {cout <-1 <endl; continue ;} y + = tem; cout <y <endl ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.