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. iOS开发-简单的循环结构分析

    1.while循环 while (循环条件) {         循环体: } // 1.定义循环变量 ; // 2.循环条件 ) { // 3.循环体 printf("%d\n" ...

  2. python学习笔记-问题

    1.字典按照值进行排序输出 2.返回函数-闭包的使用

  3. Python randrange() 函数

    Python randrange() 函数  Python 数字 描述 randrange() 方法返回指定递增基数集合中的一个随机数,基数缺省值为1. 语法 以下是 randrange() 方法的语 ...

  4. JAVA_SE基础——62.String类的构造方法

    下面我先列出初学者目前用到的构造方法 String 的构造方法:     String()  创建一个空内容 的字符串对象.   String(byte[] bytes)  使用一个字节数组构建一个字 ...

  5. JAVA_SE基础——27.匿名对象

    黑马程序员入学blog... 匿名对象:没有引用类型变量指向的对象称作为匿名对象. 匿名对象要注意的事项:1. 我们一般不会给匿名对象赋予属性值,因为永远无法获取到.2. 两个匿名对象永远都不可能是同 ...

  6. JAVA_SE基础——12.运算符的优先级

    优先级 操作符 含义 关联性 用法 ---------------------------------------------------------------- 1 [ ] 数组下标 左 arra ...

  7. 码农、黑客和2B程序员之间的区别

    码农: 黑客: 2B程序员: 求2的32次方: 码农: System.out.println(Math.pow(2, 32)); 黑客: System.out.println(1L<<32 ...

  8. 第四章 Ajax与jQuery

    第四章   Ajax与jQuery 一.Ajax简介 在传统的Web应用中,每次请求服务器都会生成新的页面,用户在提交请求后,总是要等待服务器的响应.如果前一个请求没有响应,则后一个请求就不能发送,在 ...

  9. C# Bootstrap table之 分页

    效果如图: 一.声明talbe <div class="container"> <table id="table" class="t ...

  10. GridControl的常用操作

    1.GridView的回车跳转单元格和换行 private void gridView1_KeyPress(object sender, KeyPressEventArgs e)        {   ...