Codeforces Round #419 (Div. 2) D

来源:互联网 发布:聚划算与淘宝的关系 编辑:程序博客网 时间:2024/05/21 09:06

传送门


D. Karen and Test
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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 109 + 7.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 200000), the number of numbers written on the first row.

The next line contains n integers. Specifically, the i-th one among these is ai (1 ≤ ai ≤ 109), the i-th number on the first row.

Output

Output a single integer on a line by itself, the number on the final row after performing the process above.

Since this number can be quite large, print only the non-negative remainder after dividing it by 109 + 7.

Examples
input
53 6 9 12 15
output
36
input
43 7 5 2
output
1000000006
Note

In the first test case, the numbers written on the first row are 36912 and 15.

Karen performs the operations as follows:

The non-negative remainder after dividing the final number by 109 + 7 is still 36, so this is the correct output.

In the second test case, the numbers written on the first row are 375 and 2.

Karen performs the operations as follows:

The non-negative remainder after dividing the final number by 109 + 7 is 109 + 6, so this is the correct output.


题意:看样例知题意。 明显跟杨辉三角有联系。

做法:打表找找规律呗。 看每个位置对最终答案的贡献度。+-+-的交替性。 4为一个界限。 把mod4==1 mod4==2 mod4==3 mod4==0 分别讨论。

杨辉三角与二项式定理有关。 往那方面思考一下。

比如:

3 6 9

  9 -3

  12 

其实也就3和9对答案有贡献

另外个例子:举样例吧就。 

3 6 9 12 15

3 c(0,2)  此处的c代表组合  为1嘛 

9 c(1,2)  为2

15 c(2,2) 为1 

最终的答案就是ans=3*1+9*2+15*1 =36

另外的就是推规律啦。

还有一点写c(n,m)的时候求一下逆元 毕竟mod1e9+7

#include <iostream>#include <stdio.h>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <stack>#include <cmath>#include <map>#include <bitset>#include <set>#include <vector>#include <functional>using namespace std;typedef long long ll;typedef long long LL;#define mod 1000000007int p[1000050],t[1000050],f[1000050],a[1000050];LL Pow(LL a, LL b){  LL ans = 1;  for (; b; b >>= 1, a = a * a % mod)    if (b & 1) ans = ans * a % mod;  return ans;}//Pow(x,mod-2)  x的逆元。  - - 又被喷了的嘛int  C(int n,int m){     int  ans=1ll*p[n]*Pow(p[m],mod-2)%mod*Pow(p[n-m],mod-2)%mod;     return ans;}int  main(){     int  n,i,ans;     for (p[0]=i=1;i<=200000;i++) p[i]=1ll*p[i-1]*i%mod;     scanf("%d",&n);     for (i=1;i<=n;i++) scanf("%d",&a[i]);     if  (n%4==0)     {         for (i=1;i<=n;i++)         if  (i&1) t[i]=C(n/2-1,(i-1)/2);         else t[i]=-C(n/2-1,(i-1)/2);     }     else if  (n%4==1)     {         for (i=1;i<=n;i++)         if  (i&1) t[i]=C(n/2,i/2);     }     else     {         for (i=1;i<=n;i++)         {             t[i]=C(n/2-1,(i-1)/2);           //  cout<<t[i]<<"    "<<i<<endl;         }         if  (n%4==3)         {             f[1]=1;             f[n]=-1;             for (i=2;i<n;i++)             if  (i&1) f[i]=t[i]-t[i-1];             else f[i]=t[i]+t[i-1];             memcpy(t,f,sizeof(t));         }     }   //  for(i=1;i<=n;i++)      //  cout<<t[i]<<endl;     ans=0;     for (i=1;i<=n;i++) ans=(1ll*t[i]*a[i]+ans)%mod;     printf("%d\n",(ans+mod)%mod);     return 0;}






原创粉丝点击