Codeforces Round 362 (Div 2) e formula derivation + fast Power + Fermat theorem Please a[i]= (1-a[i-1]) except 2 n times n is a continuous multiplier

Source: Internet
Author: User
Tags greatest common divisor mul printf time limit

E. Please-time limit per test 1 second memory limit per test megabytes input standard input output standard output

As we all know Barney's job is the "please" and he have not much to does at work. That's why he started playing "cups and key". In this game there is three identical cups arranged in a line from left to right. Initially key to Barney ' s heart is under the Middle Cup.

Then at one turn Barney swaps the cup in the middle with any of the other and the cups randomly (he choses each with equal Probabi lity), so the chosen Cup becomes the middle one. Game lasts n turns and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it is at the start.

After n-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney were distracted while making turns and doesn ' t know if the key is under the MI Ddle Cup. That's why he asked the him the probability, that girl guessed right.

Number N of the game turns can be extremely large, that's why Barney didn't not give it to you. Instead He gave you an array a1, A2, ..., Aksuch that

In other words, n was multiplication of all elements of the given array.

Because of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In all words need to find it as a fraction p/q such that, where is the greatest common divisor. Since p and Q can be extremely large, you have need to find the remainders of dividing each of the them by 109 + 7.

Please note that we want the P and Q to is 1, not the their remainders after dividing by 109 + 7. Input

The first line of input contains a single integer k (1≤k≤105)-the number of elements in array Barney gave.

The second line contains k integers a1, A2, ..., AK (1≤ai≤1018)-the elements of the array. Output

In the-line of output print a single string x/y where x is the remainder of dividing p by 109 + 7 and Y are the Rema Inder of dividing Qby 109 + 7. Examples input

1
2
Output
1/2
Input
3
1 1 1
Output
0/1

#include <stdio.h> #include <iostream> #include <string.h> #include <string> #include < ctype.h> #include <math.h> #include <set> #include <map> #include <vector> #include <queue
> #include <bitset> #include <algorithm> #include <time.h> using namespace std; void Fre () {freopen ("c://test//input.in", "R", stdin); Freopen ("C://test//output.out", "w", stdout);} #define MS (x, y) Me  Mset (x,y,sizeof (x)) #define MC (x, y) memcpy (x,y,sizeof (×)) #define MP (y) make_pair (x, y) #define LS o<<1 #define RS
o<<1|1 typedef long Long LL;
typedef unsigned long long UL;
typedef unsigned int UI; Template <class T1, class t2>inline void Gmax (T1 &a, T2 b) {if (b>a) a = b;} template <class T1, class T2
>inline void Gmin (T1 &a, T2 b) {if (b<a) a = b;} const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n;
	const int G = 2;//Matrix size struct MX {int v[g][g];
	void O () {MS (V, 0);} void E () {MS (V, 0); for (int i = 0; i<g; i++) v[i][i] = 1;
		} MX operator * (const MX &b) const {MX C; C.O (); for (int i = 0; i<g; i++) {for (int j = 0; j<g; j + +) {for (int k = 0; k<g; k++) {c.v[i][j
				] = (C.v[i][j] + (LL) v[i][k] * b.v[k][j])% Z;
	}}} return C;
		} MX operator + (const MX &b) const {MX C; C.O ();
			for (int i = 0; i<g; i++) {for (int j = 0; j<g; j + +) {C.v[i][j] = (V[i][j] + b.v[i][j])% Z;
	}} return C;
		} MX operator ^ (LL p) const {MX y; y.e (); MX x;
		MC (X.V, v);
			while (p) {if (P & 1) y = y*x;
			x = x*x;
		P >>= 1;
	} return y;
}}a,b,c;
	void Tryit (int p) {b.o ();
	B.V[0][0] = 4;
	B.v[1][0] = b.v[1][1] = 1;
	A.O ();
	A.v[0][0] = 0;
	A.V[0][1] = 1;
	c = A * (b ^ (P/2));
	int ans = c.v[0][0];
	if (P & 1) ans = (ans * 2 + 1)% Z;
printf ("%d", ans);
	} int Mul (ll x, int p) {ll y = 1;
		while (p) {if (P & 1) y = y*x%z;
		x = x*x%z;
	P >>= 1;
} return y; } VOID Fast (int tim) {int top = Mul (2, Tim);
	if (Tim & 1) {top + = 1; if (top >= z) top = z;}
	else {top-= 1; if (top < 0) Top + = Z;}
	top = (LL) Top * MUL (3, Z-2)% Z;
printf ("%d", top);
		} int main () {while (~SCANF ("%d", &n)) {LL Tim = 1;
			for (int i = 1; I <= n; ++i) {LL x; scanf ("%lld", &x);
			x%= (Z-1);
			Tim *= x;
		Tim%= (Z-1);
		} Tim = (Tim + Z-2)% (Z-1);
		Tryit (Tim);//matrix Fast Power Procedure fast (TIM);//Fast Power Procedure printf ("/");
	printf ("%d\n", Mul (2, Tim));
} return 0;
}/* "Test instructions" >.< do not say the original test instructions a little conversion, the problem becomes--a[0]=1;
A[i]= (1-a[i-1])/2 in the form of fractions output A[N] (numerator denominator numerator, then% (z=1e9+7)) where n gives the "type" Fermat theorem in the form of a number of multiples (fast power of matrix or fast exponentiation) "Analysis" first n so big, what shall we do.
It can be iterated by the fast power of Nlogn, or by Fermat theorem simplification.
The so-called fee Ma Xiao theorem, is said, (x^ (Z-1))%z==1 (Z is the prime number), that is, for the power of X, each Z-1 to complete a cycle.

So, we multiply the power to do% (Z-1), and the resulting Tim is used for subsequent computations.
Then how to count you.
A[i]= (1-a[i-1])/2 We look for the law, that's it.--1 0 1/2 1/4 3/8 5/16 We found that the denominator has been *=2, the numerator is in (*2+1), (*2-1) the wrong alternation of the way.

The denominator is the power of 2, the numerator is an odd number does not contain 2, so the denominator and molecule are not numerator necessary. In the denominator, a fast power can be done with molecules, and we can combine (*2+1) and (*2-1) into (*4+1) But my approach is a bit rough, the problem can be directly with the quick power. First to push the formula--a[i]= (1-a[i-1])/2 get 2a[i] = (1-a[i-1]) to get 2 (A[I]-1/3) =-(A[I-1]-1/3) so (A[I]-1/3) is a (A[0]-1/3) to the head ( -1/2)

For the geometric series of the male ratio.

So we find--(A[N]-1/3) = (2/3) * ( -1/2) ^n so that if n is even, then the result is (1+2^ (n-1))/3*2^ (n-1) if n is odd, then the result is ( -1+2^ (n-1))/3*2^ (n-1)

So we can find the solution quickly. "Time Complexity && Optimization" O (n) */


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.