Codeforces D. Intercity Travelling

Subject description:

D. Intercity Travelling

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Leha is planning his journey from Moscow to Saratov. He hates trains, so he has decided to get from one city to another by car.

The path from Moscow to Saratov can be represented as a straight line (well, it's not that straight in reality, but in this problem we will consider it to be straight), and the distance between Moscow and Saratov is n

km. Let's say that Moscow is situated at the point with coordinate km, and Saratov — at coordinate n

Driving for a long time may be really difficult. Formally, if Leha has already covered i

kilometers since he stopped to have a rest, he considers the difficulty of covering-th kilometer as *a**i*+1

. The difficulty of the journey is denoted as the sum of difficulties of each kilometer in the journey.

Fortunately, there may be some rest sites between Moscow and Saratov. Every integer point from 1

to may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a1

, and so on.

For example, if n=5

and there is a rest site in coordinate , the difficulty of journey will be 2a1+2a2+a3, the second one — a2, the fourth — a2. Another example: if n=7 and 5

.

Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2n−1

different distributions of rest sites (two distributions are different if there exists some point such that it contains a rest site in exactly one of these distributions). Leha considers all these distributions to be equiprobable. He wants to calculate p

Obviously, p⋅2n−1

is an integer number. You have to calculate it modulo

.

Input

The first line contains one number n

(

) — the distance from Moscow to Saratov.

The second line contains n

integer numbers , a2 (1≤a1≤a2≤⋯≤*a**n≤106 is the difficulty of i*

Output

Print one number — p⋅2n−1

, taken modulo

.

Examples

Input

Copy

21 2

Output

Copy

5

Input

Copy

41 3 3 7

Output

Copy

60

Ideas:

Do vomit blood (Food is the essence. Said request is entitled \ (P *. 1-n-2 ^ {} \) , and because a total of possible cases is \ (. 1-n-2 ^ {} \) , in such a case is actually required to establish all the rest stop several case a1 + a2 + ... + an several several, i.e., the degree of difficulty and.

On reflection we can find such a fact: a1 is most likely to occur and an is the least prone to, in fact, may occur on an a: no rest stop. To appear a1, it is necessary to stand back rest has a unit length, to appear a2, will have to have two back rest stop unit length, and so on. So that we can take an example of n == 4:

A1: 0 ---- 1 ---- 2 ---- 3 ---- 4

a1: | ---- |, this segment represents a unit interval length. In front of the break point range segment must have a rest stop to appear a1, but not behind the establishment of the station can be built, a point behind the establishment of the station can have 1,2,3,2 ^ 3 possible

----------- | ---- |, segment is moved into this section, the same front end construction must ensure a station appears a1, followed by 2 ^ 2 2,3 Total possible

---------------- | ---- |, here are some differences, there are circumstances behind the establishment of the station under the preceding paragraph may station 3, 1 can appear in front of a station, two kinds of 2 ^ may

--------------------- | ---- |, where 1, 2 may be the front station, a total of 2 ^ 2 possible

To appear for a1, there are 2 ^ 3 + 3 * 2 ^ 2 ^ 2 = (4-1) + (4-1) * 2 ^ (4-1-1) in the case of possible

a2: 0 ---- 1 ---- 2 ---- 3 ---- 4

a2: | ---------- |, where the interval when the front-end to ensure a station, it may appear a2, the rear end has a total of 2 ^ 2,3 2 possible

a2: ------ | ---------- |, where the front end station, behind the station 3 may, from time to build a total of 2 ^ 1 possible

a2: ----------- | ----------- |, where 1 time, can a station, a total of 2 ^ 1 possible

To appear for a2, there \ (2 ^ 2 ^ 2 + 2 * 1 = ^ 2} + {4-2 (4-2) 4-2-1 * 2 ^ {} \) possible cases

Similarly, there are

ai, there \ (2 ^ {ni} + (ni) * 2 ^ {ni-1} \) possible

Final answer requires \ (\ sum_ {i = 1 } ^ {n} a [i] * s [i] \) values. s [i] is the corresponding a [i] in the case where the number of possible

Note may burst longlong (certain), always careful to take over, I'll write a final sum + = ... forget to take it over, and you miserable

As well as read data big? Like, plus a quick read into it without understanding? Look at the blog post (hee hee)

This problem can in fact be considered in time, not a power of two fast power per operator, direct O (n) into an array of pre-treatment, followed by O (1) used on the line

Code:

#include <iostream>
#include <cstdio>
#define max_n 1000005
#define mod 998244353
using namespace std;
int n;
long long a[max_n];
long long s[max_n];
long long mi[max_n] = {1,2,4,8,16,32};
void generator(int n)
{
    for(int i = 6;i<=n;i++)
    {
        mi[i] = (mi[i-1]*2)%mod;
    }
}
inline int read()//整数输入模板
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
#pragma optimize(2)
int main()
{
    n = read();
    for(int i = 1;i<=n;i++)
    {
        a[i] = read();
    }
    //s[1] = 1;
    //s[2] = 3;
    //s[3] = 8;
    //s[4] = 20;
    generator(n);
    long long sum = 0;
    for(int i = 1;i<=n;i++)
    {
        s[i] = (mi[n-i]+(mi[n-i-1]*(n-i)))%mod;
        sum = (sum%mod+(s[i]%mod*a[i]%mod)%mod)%mod;
    }
    //cout << "len " << endl;
    /*for(int i = 1;i<=n;i++)
    {
        cout << s[i] << " ";
    }
    cout << endl;*/
    printf("%I64d\n",sum);
    return 0;
}

Reference article:

The idea is to own, under code reference, because the timeout chat (qwq)

luyehao1, Intercity Travelling (mathematical formula to derive div2 E cf), https://blog.csdn.net/luyehao1/article/details/81080860

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11285549.html