Codeforces Round #419 (Div. 2) (Codeforces 815B) D. Karen and Test Combinatorial math

Source: Internet
Author: User
Tags first row time limit

D. Karen and test time limit per test 2 seconds memory limit per test megabytes input standard input output standard o Utput

Karen had just arrived at school, and she had 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, which is worth all the points.

There is 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 is on the values of 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 ve Rsa.

The teachers'll simply look at the last integer, and then if it's correct, Karen gets a perfect score, otherwise, she g ETS a zero for the test.

Karen had studied well for this test, but she was scared that she might make a mistake somewhere and it'll cause her FINA L answer to be wrong. If the process is followed, what number can she expect to being written on the last row?

Since This number can is quite large, output only the non-negative remainder after dividing it by 109 + 7. Input

The first line of input contains a is a single integer n (1≤n≤200000) and 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 is quite large, print only the non-negative remainder after dividing it by 109 + 7. Examples input

5
3 6 9 12 15
Output
36
Input
4
3 7) 5 2
Output
1000000006
Note

In the first test case, the numbers written on the first row is 3, 6, 9, and 15.

Karen performs the operations as follows:

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

The second test case, the numbers written on the first row is 3, 7, 5 and 2.

Karen performs the operations as follows:

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



A column n number, put these number 22 according to +,-,+,-... Computation, each time the number of N merged into N-1, ask the last number of the remaining mod 1e9+7 is how much.


After a rigorous XJB guess, found that according to the results of the n%4 four, you can find each number in the final answer to contribute to the law of the coefficient. These coefficients can be expressed in combination numbers. Detailed rules are as follows: (from the official CF)

When, the pattern is:

When, the pattern is:

When, the pattern is:

When, the pattern is:

Then just count the number of combinations you can. The number of combinations is too large to calculate the inverse.

A magical law about factorial inverse:

Inv[n!] =inv[(n+1)!] * (n+1)

This makes it easy to figure out the large number of combinations.


#include <cstdio> #include <iostream> #include <string.h> #include <string> #include <map > #include <queue> #include <vector> #include <set> #include <algorithm> #include <math.h&
Gt #include <cmath> #include <stack> #define MEM0 (a) memset (A,0,sizeof (a)) #define Meminf (a) memset (a,0x3f,
sizeof (a)) using namespace Std;
typedef long Long LL;
typedef long double LD;  
const int maxn=200005,inf=0x3f3f3f3f;   
const LL LLINF=0X3F3F3F3F3F3F3F3F,MOD=1E9+7;  
Const LD Pi=acos ( -1.0L);

ll A[MAXN],INV[MAXN];
	ll Fastpow (ll Base,ll index) {ll sum=base,an=1;
    ll I=index;
    	while (i) {if (i%2) an= (an*sum)%mod;
    	Sum*=sum;
    	Sum=sum%mod;
    i/=2;
} return an;
	} int main () {int n,i,j;
	ll Ans=0,fac=1,g;
	scanf ("%d", &n);
	for (i=1;i<=n;i++) scanf ("%i64d", &a[i]); if (n%2==0) g= (n-2)/2;
	else g= (n-n%4)/2;
	for (i=1;i<=g;i++) fac= (fac* ((ll) i))%mod;
	Inv[g]=fastpow (fac,mod-2); for (I=g-1;i> =0;i--) {inv[i]=inv[i+1]* (i+1);
	Inv[i]%=mod;
		} if (n%2==0) {ll t=1;
			for (i=1;i<=n;i++) {if (i%2==0&&n%4==0) ans-=t*a[i]; else ans+=t*a[i];
			Ans%=mod;
			if (ans<0) Ans+=mod;
			if (i%2==0) {t= (((FAC*INV[I/2])%mod) *INV[G-I/2])%mod;
		}}} and else if (n%4==1) {ll t=1;
			for (i=1;i<=n;i++) {if (i%2!=0) {ans+=t*a[i];ans%=mod;
			} else {t= ((FAC*INV[I/2)%mod) *INV[G-I/2])%mod;
		}}} and else if (n%4==3) {ll t=1,last=0;
			for (i=1;i<=n;i++) {if (i%2==0) ans+=2*t*a[i]; else ans+= (T-last) *a[i];
			Ans%=mod;
			if (ans<0) Ans+=mod;
				if (i%2==0) {last=t;
			T= (((FAC*INV[I/2)%mod) *INV[G-I/2])%mod;
	}}} printf ("%i64d\n", ans);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.