This page is still under construction.

Parts of this page are still being built. What you see may change.

Average Value Sequence

Time limit5sMemory limit256 MB

Summary
Given a non-decreasing average sequence m of length n, count the integer sequences s of length n+1 whose adjacent averages equal m.
Level

Medium6 of 10

Topics
Math, Combinatorics, Dynamic programming, Implementation
Solved
No attempts yet

Problem

Consider a non-decreasing integer sequence s1,s2,…,sn+1s_1, s_2, \ldots, s_{n+1} of length n+1n+1 (that is, si≤si+1s_i \le s_{i+1} for every 1≤i≤n1 \le i \le n).

Define the average sequence of ss as follows: for 1≤i≤n1 \le i \le n, mi=si+si+12.m_i = \frac{s_i + s_{i+1}}{2}.

For example, if S=(1,2,2,4)S = (1, 2, 2, 4) then its average sequence is M=(32, 2, 3)M = \left(\frac{3}{2},\ 2,\ 3\right). The elements of an average sequence need not be integers, but in this problem we only consider cases where every element of the average sequence is an integer.

You are given a non-decreasing average sequence m1,m2,…,mnm_1, m_2, \ldots, m_n of length nn. Count the number of integer sequences s1,s2,…,sn+1s_1, s_2, \ldots, s_{n+1} whose average sequence equals it.

Input

The first line contains the length nn of the average sequence. (2≤n≤5 000 0002 \le n \le 5\,000\,000)

Each of the next nn lines contains one element mim_i of the average sequence, in order. (0≤mi≤1 000 000 0000 \le m_i \le 1\,000\,000\,000; the sequence is non-decreasing.)

Output

Print the number of integer sequences ss whose average sequence equals the given one.

Hint

For the first example (m=(2,5,9)m = (2, 5, 9)), exactly the following four sequences exist: (2,2,8,10),(1,3,7,11),(0,4,6,12),(−1,5,5,13).(2, 2, 8, 10),\quad (1, 3, 7, 11),\quad (0, 4, 6, 12),\quad (-1, 5, 5, 13).

Examples1

  1. Example 1

    Input
    3
    2
    5
    9
    
    Expected output
    4