C++ Saved Program: serene-mountain

facebook share

Here's the online compiler accompanied with IDE at your service. This is a saved code by one of our users. Try it out or save your own from the IDE page and don't forget to leave a feedback. You can find that at the bottom of the page.


#include <bits/stdc++.h>
#define MAXN 200010
using namespace std;
const int mod=1e9+7;
int n,a[MAXN];

long long q_pow(long long x,long long n)
{
	long long res=1,tmp=x;
	while(n){
		if(n&1) res=res*tmp%mod;
		tmp=tmp*tmp%mod;
		n>>=1;
	}
	return res;
}

int main()
{
	int i,j,c=1;
	
	scanf("%d",&n);
	for(int i = 0; i < n; i++) scanf("%d",&a[i]);
	int sw=(n*(n-1)/2)&1?1:-1;
	
	if(n&1) {
		for(i=0;i<n;i++,c^=1)
			a[i] = (a[i] + (c ? a[i+1] : mod - a[i + 1])) % mod;
		n--;
	}
	
	for(int i = 0; i < n; i++) {
	    cout << a[i] << " " << flush;
	}
	cout << endl;
	
	long long C=1,ans=a[0];
	for(int i=1,k=1; i<n; i++){
		if(i%2==0) {
		    C=C*(n/2-k)%mod*q_pow(k++,mod-2)%mod;
		    ans+=C*a[i];
		} else
    		ans+=sw*C*a[i]+mod;
		cout<< "C: " << C << endl;
		
		ans%=mod;
	}
	
	cout<<"sw: " << sw<< endl;
	
	
	cout<<ans<<endl;
 return 0;
}
Execute

Output

Press the execute button or CTRL + enter to run code.

Inputs