Bubble Cup V8 A Fibonotci

View as PDF

Submit solution


Points: 25
Time limit: 1.4s
Memory limit: 64M

Problem type

Fibonotci sequence is an integer recursive sequence defined by the recurrence relation

Fn=cn1Fn1+cn2Fn2

with

F0=0,F1=1.

Sequence c is infinite and almost cyclic sequence with a cycle of length N. A sequence s is almost cyclic with a cycle of length N iff si=simodN, for iN, except for a finite number of values si, for which sisimodN (iN).

Following is an example of an almost cyclic sequence with a cycle of length 4.

s=(5,3,8,11,5,3,7,11,5,3,8,11,)

Notice that the only value of s for which the equality si=simod4 does not hold is s6 (s6=7 and s2=8).

You are given c0,c1,,cN1 and all the values of sequence c for which cicimodN (iN).

Find FKmodP.

Input Specification

The first line contains two numbers K and P. The second line contains a single number N. The third line contains N numbers separated by spaces, that represent the first N numbers of the sequence c. The fourth line contains a single number M, the number of values of sequence c for which cicimodN. Each of the following M lines contains two integers j and v, indicating that cjcjmodN and cj=v.

Output Specification

Output should contain a single integer equal to FKmodP.

Constraints

  • 1N,M50000
  • 0K1018
  • 1P109
  • 1ci109, for i=0,1,,N1
  • Nj1018
  • 1v109
  • All values are integers

Sample Input

Copy
10 8
3
1 2 1
2
7 3
5 4

Sample Output

Copy
4

Comments

There are no comments at the moment.