E. Intercity Travelling

E. 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 nn km. Let's say that Moscow is situated at the point with coordinate 00 km, and Saratov — at coordinate nn km.

Driving for a long time may be really difficult. Formally, if Leha has already covered ii kilometers since he stopped to have a rest, he considers the difficulty of covering (i+1)(i+1) -th kilometer as ai+1ai+1 . It is guaranteed that for every i∈[1,n−1]i∈[1,n−1] ai≤ai+1ai≤ai+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 11 to n−1n−1 may contain a rest site. When Leha enters a rest site, he may have a rest, and the next kilometer will have difficulty a1a1 , the kilometer after it — difficulty a2a2 , and so on.

For example, if n=5n=5 and there is a rest site in coordinate 22 , the difficulty of journey will be 2a1+2a2+a32a1+2a2+a3 : the first kilometer will have difficulty a1a1 , the second one — a2a2 , then Leha will have a rest, and the third kilometer will have difficulty a1a1 , the fourth — a2a2 , and the last one — a3a3 . Another example: if n=7n=7 and there are rest sites in coordinates 11 and 55 , the difficulty of Leha's journey is 3a1+2a2+a3+a43a1+2a2+a3+a4 .

Leha doesn't know which integer points contain rest sites. So he has to consider every possible situation. Obviously, there are 2n−12n−1 different distributions of rest sites (two distributions are different if there exists some point xx 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 pp — the expected value of difficulty of his journey.

Obviously, p⋅2n−1p⋅2n−1 is an integer number. You have to calculate it modulo 998244353998244353 .

Input

The first line contains one number nn (1≤n≤1061≤n≤106 ) — the distance from Moscow to Saratov.

The second line contains nn integer numbers a1a1 , a2a2 , ..., anan (1≤a1≤a2≤⋯≤an≤1061≤a1≤a2≤⋯≤an≤106 ), where aiai is the difficulty of ii -th kilometer after Leha has rested.

Output

Print one number — p⋅2n−1p⋅2n−1 , taken modulo 998244353998244353 .

Examples

Input

Copy

2
1 2

Output

Copy

5

Input

Copy

4
1 3 3 7

Output

Copy

60

 

 

 

n千米,有n段路,共有n-1个可能可以休息的地方,则共有2^(n-1)种可能的休息方式;
要计算 p⋅2^(n−1),也就是计算所有的可能的休息方式下的消耗之和;
分解出来,就是要计算所有的可能的休息方式中所有的a[i]的和;
也就是求和 sum(a[i] * 消耗a[i]出现的总次数) (i->1,2,3,…n);

我们可以按照这种思路,统计a[1]出现的总次数,a[2]出现的次数,这个应该是有规律的;

例如n=4的情况

0  1  2  3  4
#--#--#--#--#
其中 1,2,3 这几个点既可以是休息,也可以是不休息两种状态
  • 1
  • 2
  • 3
  • 4
  • 5

先考虑简单的,要使a[1]出现:

若a[1]出现在 0-1 之间,显然这是必定的,和 1,2,3 休息与不休息都没关系,有所以在 0-1 出现a[1]的次数是 2^3 = 2^(n-1) 次
若a[1]出现在 1-2 之间,则必须是在 1 点处休息了,而 2,3 处则没关系,那么所有在 1-2 出现a[1]的次数是 2^2 = 2^0 * 2^(n-2) = 2^(n-2) 次
若a[1]出现在 2-3 之间,则必须是在 2 点休息了,和在 1,3 的状况没关系,那么所有在 2-3 出现a[1]的次数是 2^2 = 2^1 * 2^(n-3) = 2^(n-2) 次
同理可得a[1]出现在 3-4 之间的次数是 2^2 = 2^2 * 2^(n-4) = 2^(n-2) 次

则a[1]出现的总次数是 2^3 + 2^2 + 2^2 + 2^2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

再考虑a[2],a[3],a[4]的情况,
a[2]出现的总次数是 2^2 + 2^1 + 2^1
a[3]出现的总次数是 2^1 + 2^0
a[4]出现的总次数是 2^0

可以发现规律
a[i]出现的次数是 2^(n-i) + 2^(n-i-1) * (n-i)

 

 

AC代码

#include <iostream>
 
using namespace std;
#define N 1000003
#define mod 998244353
 
long long dp[N];
long long times[N];
long long sum[N];
long long num[N];
 
long long power_moudle(long long a,long long p)
{
    long long ans=1;
    while(p)
    {
        if(p&1)
            ans=ans*a%mod;
        a=a*a%mod;
        p=p/2;
    }
    return ans;
}
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>num[i];
    sum[0]=1;
    for(int i=1;i<=n;++i)
        sum[i]=(sum[i-1]*2)%mod;
    long long ans=0;
    for(int i=1;i<=n;++i)
    {
        ans=(ans+((sum[n-i]+(sum[n-i-1]*(n-i))%mod)*num[i])%mod)%mod;
    }
    cout<<ans<<endl;
    return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值