Karen has just arrived at school, and she has a math test today!

The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points.

There are n integers written on a row. Karen must alternately add and subtract each pair of adjacent integers, and write down the sums or differences on the next row. She must repeat this process on the values on the next row, and so on, until only one integer remains. The first operation should be addition.

Note that, if she ended the previous row by adding the integers, she should start the next row by subtracting, and vice versa.

The teachers will simply look at the last integer, and then if it is correct, Karen gets a perfect score, otherwise, she gets a zero for the test.

Karen has studied well for this test, but she is scared that she might make a mistake somewhere and it will cause her final answer to be wrong. If the process is followed, what number can she expect to be written on the last row?

Since this number can be quite large, output only the non-negative remainder after dividing it by \(10^9+7\).


题目大意:

给定N个数,每一次交错填写+-号,然后将结果放到下一行,然后继续交错填写,求最后一行的答案

解题报告:

好久以前的坑.....

手玩N=6和样例发现:



最后的结果可以表示为\(x1*a1+x2*a2+..+xn*an\),\(x\)是\(ai\)的系数,然后发现如果N为偶数就可以分偶数项和奇数项讨论,两者计算的方式是一样的.

再进一步又发现单独看奇偶项满足二项式定理,所以直接用组合数计算系数即可,但是对于N为4的倍数的情况答案是奇数项-偶数项,讨论一下即可

对于N不为偶数的情况我们可以先算出下一行然后做同样的步骤即可

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=200005,mod=1e9+7;
int a[N],n;ll w[N],mul[N],ni[N];
ll qm(ll x,ll k){
ll sum=1;
while(k){
if(k&1)sum*=x,sum%=mod;
x*=x;x%=mod;k>>=1;
}
return sum;
}
ll C(int n,int k){return mul[n]*ni[k]%mod*ni[n-k]%mod;}
void work()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
if(n<=2){printf("%d\n",(a[1]+a[2])%mod);return ;}
if(n%2){
n--;
for(int i=1;i<=n;i++)
w[i]=(i%2?a[i]+a[i+1]:a[i]-a[i+1]),
w[i]=(w[i]%mod+mod)%mod;
}
else for(int i=1;i<=n;i++)w[i]=a[i];
mul[0]=1;ni[0]=1;
for(int i=1;i<=n;i++)
mul[i]=mul[i-1]*i%mod,ni[i]=qm(mul[i],mod-2);
ll ans=0;
int hx=(n%4?1:-1);
for(int i=1;i<=n;i+=2){
ans+=C((n>>1)-1,i>>1)*(w[i]+hx*w[i+1]%mod)%mod;
if(ans>=mod)ans-=mod;
}
ans=((ans%mod)+mod)%mod;
printf("%lld\n",ans);
} int main()
{
work();
return 0;
}

Codeforces Round #419 D. Karen and Test的更多相关文章

  1. codeforces round #419 E. Karen and Supermarket

    On the way home, Karen decided to stop by the supermarket to buy some groceries. She needs to buy a ...

  2. codeforces round #419 C. Karen and Game

    C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  3. codeforces round #419 B. Karen and Coffee

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  4. codeforces round #419 A. Karen and Morning

    Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As yo ...

  5. Codeforces Round #419 (Div. 2) B. Karen and Coffee(经典前缀和)

    http://codeforces.com/contest/816/problem/B To stay woke and attentive during classes, Karen needs s ...

  6. Codeforces Round #419 (Div. 2) C. Karen and Game

    C. Karen and Game time limit per test 2 seconds memory limit per test 512 megabytes input standard i ...

  7. Codeforces Round #419 (Div. 2) B. Karen and Coffee

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  8. Codeforces Round #419 (Div. 2) E. Karen and Supermarket(树形dp)

    http://codeforces.com/contest/816/problem/E 题意: 去超市买东西,共有m块钱,每件商品有优惠卷可用,前提是xi商品的优惠券被用.问最多能买多少件商品? 思路 ...

  9. Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)

    http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...

随机推荐

  1. 用greenlet实现Python中的并发

    from greenlet import greenlet def test1(): print 12 gr2.switch() print 34 def test2(): print 56 gr1. ...

  2. Flask 学习 九 用户资料

    资料信息 app/models.py class User(UserMixin,db.Model): #...... name = db.Column(db.String(64)) location ...

  3. XFTP连接主机文件名显示中文乱码且不能下载的解决方法

    Xftp连接主机文件名显示中文乱码且不能下载的本地解决方法 原因:Xftp编码格式问题 解决方法:把Xftp的编码格式增加UTF-8 具体步骤:打开Xftp,文件-属性,在打开的属性界面中打开&quo ...

  4. Mego开发文档 - 索引

    Mego 开发文档 Mego 快速概述 主要特性 获取Mego 使用流程 模型 查询 保存数据 入门 Mego 快速开始 创建项目 安装Nuget包 创建连接字符串 创建模型及数据上下文(添加引用) ...

  5. api-gateway实践(13)新服务网关 - 断路保护/熔断机制

    参考链接:SpringCloud的Hystrix(五) Hystrix机制 新需求列表 1.在线测试 根据定义,生成输入界面, 点击测试, 验证参数,发起调用,返回执行结果 2.熔断保护 两个实现类: ...

  6. EasyUI 数据网格行过滤

    前段时间发现一个GridView很有用的功能,可以筛选数据,实现起来很简单 一.添加一个引用,这个可以自己去网上下载 <script type="text/javascript&quo ...

  7. 命名参数名(含*args , * *kw的区别)

    要限制关键字参数的名字,就可以用命名关键字参数 # coding=utf-8 # 命名关键字参数需要一个特殊分隔符*,*后面的参数被视为命名关键字参数.调用方式如下 def person(name, ...

  8. Beautifulsoup4

    kindEditor 1 官网:http://kindeditor.net/doc.php 2 文件夹说明: ├── asp asp示例 ├── asp.net asp.net示例 ├── attac ...

  9. python基础—函数嵌套与闭包

    python基础-函数嵌套与闭包 1.名称空间与作用域 1 名称空间分为: 1 内置名称空间   内置在解释器中的名称 2 全局名称空间   顶头写的名称 3 局部名称空间 2 找一个名称的查找顺序: ...

  10. MongoDB的安装和使用指南

    什么是MongoDB   MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系 ...