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不为偶数的情况我们可以先算出下一行然后做同样的步骤即可

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cmath>
  7. #define RG register
  8. #define il inline
  9. #define iter iterator
  10. #define Max(a,b) ((a)>(b)?(a):(b))
  11. #define Min(a,b) ((a)<(b)?(a):(b))
  12. using namespace std;
  13. typedef long long ll;
  14. const int N=200005,mod=1e9+7;
  15. int a[N],n;ll w[N],mul[N],ni[N];
  16. ll qm(ll x,ll k){
  17. ll sum=1;
  18. while(k){
  19. if(k&1)sum*=x,sum%=mod;
  20. x*=x;x%=mod;k>>=1;
  21. }
  22. return sum;
  23. }
  24. ll C(int n,int k){return mul[n]*ni[k]%mod*ni[n-k]%mod;}
  25. void work()
  26. {
  27. scanf("%d",&n);
  28. for(int i=1;i<=n;i++)scanf("%d",&a[i]);
  29. if(n<=2){printf("%d\n",(a[1]+a[2])%mod);return ;}
  30. if(n%2){
  31. n--;
  32. for(int i=1;i<=n;i++)
  33. w[i]=(i%2?a[i]+a[i+1]:a[i]-a[i+1]),
  34. w[i]=(w[i]%mod+mod)%mod;
  35. }
  36. else for(int i=1;i<=n;i++)w[i]=a[i];
  37. mul[0]=1;ni[0]=1;
  38. for(int i=1;i<=n;i++)
  39. mul[i]=mul[i-1]*i%mod,ni[i]=qm(mul[i],mod-2);
  40. ll ans=0;
  41. int hx=(n%4?1:-1);
  42. for(int i=1;i<=n;i+=2){
  43. ans+=C((n>>1)-1,i>>1)*(w[i]+hx*w[i+1]%mod)%mod;
  44. if(ans>=mod)ans-=mod;
  45. }
  46. ans=((ans%mod)+mod)%mod;
  47. printf("%lld\n",ans);
  48. }
  49. int main()
  50. {
  51. work();
  52. return 0;
  53. }

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. SOAP不同版本引起的问题

     曾经遇到这样一个问题,在组织soap字符串时报这个错误: 2013-5-29 17:25:56 org.apache.cxf.phase.PhaseInterceptorChain doDefaul ...

  2. Django之ORM字段和参数

    字段 常用字段 AutoField                                                                                    ...

  3. javascript中数组的深拷贝的方法

    一.什么是浅拷贝 在js当中,我们常常遇到数组复制的的情况,许多人一般都会使用"="来直接把一个数组赋值给一个变量,如 var a=[1,2,3]; var b=a; consol ...

  4. JavaScript Cookie使用实例

    # Session-Cookie //  利用Cookie防止在1分钟内多次提交: function SetCookie (name, value) { var Days = 30; var exp ...

  5. Gitlab的安装及项目新建

    1. Gitlab的安装及仓库创建 1.1下载gitlab安装包 1).官网下载速度较慢 建议先行下载 国内的源里面可以找到最新的版本https://mirrors.tuna.tsinghua.edu ...

  6. JavaScript 原型中的哲学思想

    学习JavaScript过程中,原型问题一直让我疑惑许久,那时候捧着那本著名的红皮书,看到有关原型的讲解时,总是心存疑虑.当在JavaScript世界中走过不少旅程之后,再次萌发起研究这部分知识的欲望 ...

  7. kubernetes进阶(02)kubernetes的node

    一.Node概念 Node是Pod真正运行的主机,可以物理机,也可以是虚拟机. 为了管理Pod,每个Node节点上至少要运行container runtime(比如docker或者rkt). kube ...

  8. Spring Security 入门(1-6-2)Spring Security - 内置的filter顺序、自定义filter、http元素和对应的filterChain

    Spring Security 的底层是通过一系列的 Filter 来管理的,每个 Filter 都有其自身的功能,而且各个 Filter 在功能上还有关联关系,所以它们的顺序也是非常重要的. 1.S ...

  9. Extensions in UWP Community Toolkit - Mouse Cursor

    概述 UWP Community Toolkit Extensions 中有一个为 Mouse 提供的扩展 - Mouse Cursor Extensions,本篇我们结合代码详细讲解 Mouse C ...

  10. ribbon 详解

    ribbon 详解 1. 顶层架构 2. 简单的示例:使用ResourceTemplate方式 @Test public void testGroup(){ HttpResourceGroup htt ...