[NOIP simulation test]: with (fast power + math)

Title Description

$ $ N-given positive integers $ a_1, a_2 ... a_n $ and a prime number mod. A variable $ x $ for the initial $ 1 $. $ M $ operations performed each time randomly selected in a $ a_i $ $ $ n-number, and then $ x = x \ times a_i $. M Q a desired value of $ X $ $ $ subsequent operations. The answer must be represented as $ \ frac {a} {b} $ of fractional accuracy. A $ $ $ b $ and may be large, it needs to output only $ a \ times b ^ {{} ^ 9 + 5 10} $ {$ result mold 10 ^ 9 + 7} of $.


Input Format

The first row of three integers $ n, m, mod $.
The next line $ n $ spaces separated positive integers $ a_1, a_2 ... a_n $.


Output Format

A row of integer answer.


Sample

Sample input

2 1 3
1 2

Sample Output

500000005


Data range and tips

The first test point $ 1 $: $ mod = 2 $.
$ 2 $ test points: $ n = 1 $.
The first $ 3,4,5 $ test points: $ m \ leqslant 1,000,1 \ leqslant mod \ leqslant 300 $.
The first $ 6,7,8 $ test points: $ 1 \ leqslant mod \ leqslant 300 $.
For all the test points: $ 1 \ leqslant a_i <mod , mod $ prime numbers $ 1 \ leqslant mod \ leqslant 1,000,1 \ leqslant n \ leqslant {10} ^ 5,1 \ leqslant m \ leqslant {10} ^ 9 $.


Lu teacher to teach you mathematics
prime number $ P $ primitive root $ g $ satisfies $ 1 \ leqslant rt <P $ , and $ rt $ $ 1 $ th, $ 2 $ th $ ... (P-1) $ times party $ P $ significance of the lower mold may be taken over $ 1 to $ $ (P-1) $ all integers.
Euler's theorem: For a prime number $ P, 1 \ leqslant x < P $ $ X $ is any $ (P-1) th power in a mold under $ P $ $ $ 1 $ are sense.
Apparently, the original root of the power of $ 1 $, $ 2 $ th $ ... (P-2) in the power mode under $ $ P $ is $ 1 $ meaning not only the $ (P-1) $ in power lower mold $ P $ significance for the $ 1 $.
It is also a number of necessary and sufficient conditions become primitive roots.


answer

The first $ 1 $ test points:

$ Mod = 2 $, do not panic, do not consider $ mod done $ will be $ 0 cases $, because $ 1 \ leqslant a_i <mod $, then $ a_i $ must be $ 1 $, so $ x $ is $ 1 $ directly $ puts ( "1"); return 0; $ enough.

The first $ 2 $ test points:

$ N = 1 $, because you can only choose the number, so fast power directly out just fine.

The first $ 3,4,5 $ test points:

Consider $ DP $, defined $ dp [i] [j] $ $ I $ after showing operations of $ X $ $ $ J probability.

Then we can write the DP $ $ formula: $ dp [i] [j \ times a [k] \% mod] = \ sum \ limits_ {k = 1} ^ ndp [i-1] [[j] $ .

Time complexity: $ \ Theta (n \ times m \ times mod) $.

But he noted that $ 3,4,5 $ three test points and did not limit the scope of the $ n $, so obviously $ T $ dancing, then we consider to optimize this $ DP $.

Then note that $ 1 \ leqslant a_i <MOD $ , so there will be a total of up to $ mod-1 $ different $ a_i $, trying to start from here.

Opening an array $ sum [i] $ $ I $ recording times this number appears.

那么$DP$式子即可转化为:$dp[i][j\times k\%mod]=\sum \limits_{k=0}^{mod-1}dp[i-1][j]\times sum[k]$。

Guess you like

Origin www.cnblogs.com/wzc521/p/11256711.html