题目描述:

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

思路:

做到吐血(菜是本质。题目是说求\(P*2^{n-1}\) ,又因为总共的可能情况就是\(2^{n-1}\),这样实际上就是求在所有的建立休息站的情况下a1有几种+a2有几种+...+an有几种,即困难度的和。

经过思考我们可以发现这样一个事实:a1是最容易出现的而an是最不容易出现的,实际上,an出现的可能就一个:无休息站。要出现a1,则需要休息站后面得有1个单位长度,要出现a2,则要休息站后面有2个单位长度,以此类推。这样我们可以来举一个n==4的例子:

a1: 0----1----2----3----4

a1: |----|,这个区间段表示一个单位长度。区间段的前面断点处必须有一个休息站才能出现a1,而后面建不建站都可以,后面可以建站的点有1,2,3,2^3种可能

-----------|----|,区间段移动到这里,同样,前端的必须建一个站保证出现a1,后面有2,3共2^2种可能

----------------|----|,到这里是有些区别,前段建站情况下后面有3可建站,前面出现1可建站,共2^2种可能

---------------------|----|,这里前方有1,2可建站,共2^2种可能

对于要出现a1,有2^3+3* 2^2=2 ^(4-1)+(4-1)* 2^(4-1-1)种可能的情况

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

a2:|----------|,区间在这里时,前端保证建站,才可能出现a2,后端有2,3共2^2种可能

a2:------|----------|,这里前端建站,后面3可建站,可不建共2^1种可能

a2: -----------|-----------|,这里时1,可建站,共2^1种可能

对于要出现a2,有\(2^2+2* 2^1= 2^{4-2}+(4-2)* 2^{4-2-1}\)种可能的情况

类似的,有

ai,有\(2^{n-i}+(n-i)* 2^{n-i-1}\)种可能

最后答案要求\(\sum_{i=1}^{n}a[i]*s[i]\)的值。s[i]就是对应a[i]可能的情况数

注意可能会爆longlong(一定),时刻小心取余,我就在最后写了个sum+=...这忘记取余了,就惨兮兮

还有读入数据挺大的?好像,加个快速读入吧,如果不了解?看一下上篇博客(嘻嘻)

这题其实可以及时算,不用快速幂每次算2的幂,直接O(n)预处理进数组,后面O(1)用就行

代码:

#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;
}

参考文章:

思路是自己的,代码参考了下,因为超时聊(qwq)

luyehao1,Intercity Travelling(数学公式推导 cf div2 E),https://blog.csdn.net/luyehao1/article/details/81080860

Codeforces D. Intercity Travelling(区间组合)的更多相关文章

  1. Codeforces 1009E Intercity Travelling | 概率与期望

    题目链接 题目大意: 一个人要从$A$地前往$B$地,两地相距$N$千米,$A$地在第$0$千米处,$B$地在第$N$千米处. 从$A$地开始,每隔$1$千米都有$\dfrac{1}{2}$的概率拥有 ...

  2. CodeForces - 1009E Intercity Travelling

    题面在这里! 可以发现全是求和,直接拆开算贡献就好了 #include<bits/stdc++.h> #define ll long long using namespace std; c ...

  3. Codeforces 1009 E. Intercity Travelling(计数)

    1009 E. Intercity Travelling 题意:一段路n个点,走i千米有对应的a[i]疲劳值.但是可以选择在除终点外的其余n-1个点休息,则下一个点开始,疲劳值从a[1]开始累加.休息 ...

  4. E. Intercity Travelling

    E. Intercity Travelling time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...

  5. Intercity Travelling CodeForces - 1009E (组合计数)

    大意: 有一段$n$千米的路, 每一次走$1$千米, 每走完一次可以休息一次, 每连续走$x$次, 消耗$a[1]+...+a[x]$的能量. 休息随机, 求消耗能量的期望$\times 2^{n-1 ...

  6. Educational Codeforces Round 47 (Rated for Div. 2) :E. Intercity Travelling

    题目链接:http://codeforces.com/contest/1009/problem/E 解题心得: 一个比较简单的组合数学,还需要找一些规律,自己把方向想得差不多了但是硬是找不到规律,还是 ...

  7. Educational Codeforces Round 47 (Rated for Div. 2)E.Intercity Travelling

    题目链接 大意:一段旅途长度N,中间可能存在N-1个休息站,连续走k长度时,疲劳值为a1+a2+...+aka_1+a_2+...+a_ka1​+a2​+...+ak​,休息后a1a_1a1​开始计, ...

  8. CodeForces 149D Coloring Brackets 区间DP

    http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...

  9. Codeforces - 149D 不错的区间DP

    题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...

随机推荐

  1. 将博客转成pdf

    前些天无意间看到了“birdben”的博客,写的比较详细,但是最新的文章更新时间是“2017-05-07”,时间很是久远,本打算有时间认真学习一下博主所写的文章,但是担心网站会因为某些原因停止服务,于 ...

  2. excel 去掉 空单元格

    Excel 2003 选中这一列,定位(CTRL+G)--定位条件--空值--确定--右键--删除. 1. 然后进行全选已输入的内容,可以使用鼠标拖动已输入的内容,也可以使用快捷键全选内容,按住ctr ...

  3. UE4 移动物体的几种方法

    转自:https://dawnarc.com/2016/06/ue4%E7%A7%BB%E5%8A%A8%E7%89%A9%E4%BD%93%E7%9A%84%E5%87%A0%E7%A7%8D%E6 ...

  4. 1.2 lvm镜像卷

    镜像能够分配物理分区的多个副本,从而提高数据的可用性.当某个磁盘发生故障并且其物理分区变为不可用时,您仍然可以访问可用磁盘上的镜像数据.LVM 在逻辑卷内执行镜像.  系统版本: # cat /etc ...

  5. nginx+upsync+consul 构建动态nginx配置系统

    参考: http://www.php230.com/weixin1456193048.html  [upsync模块说明.性能评测] https://www.jianshu.com/p/76352ef ...

  6. 长乐国庆集训Day5-2

    T1 彩虹 题目 [题目描述] Mr.Raju和他的一个大家庭外出度假,他们想要乘着彩虹欣赏周围的景色,但是这样最会有一些问题. 在他们家族中,如果一个人想要骑上彩虹,那么他喜欢的所有人和喜欢他的所有 ...

  7. 关于 exynos 4412 按键中断 异步通知

    以下是驱动测试代码: //内核的驱动代码 #include <linux/init.h> #include <linux/module.h> //for module_init ...

  8. Android 系统架构 和 各个版本代号介绍

    一.Android 系统架构: 1. linux内核层Android 基于Linux内核,为Android设备的各种硬件提供底层驱动 比如: 显示驱动.音频.照相机.蓝牙.Wi-Fi驱动,电源管理等 ...

  9. .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?

    原文:.NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用? 都知道可以在任务管理器中查看进程的 CPU 和内存占用,那么如何通过 .NET 编写代码的方式来获取到 ...

  10. 阿里巴巴 Java 开发手册 (十)MySQL 数据库

    (一) 建表规约 1. [强制]表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint ( 1 表示是,0 表示否). 说明:任何字段如果为非负数,必须 ...