NOIP '20 P4 - WeRun

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem type

C is at an empty field with k dimensions, each dimension has size of wi, meaning that all positions in the field should satisfy the condition 1aiwi (1ik).

C is planning to walk for the next P=w1×w2××wk days, starting from a new position every day. In other words, he will walk from every position in the field. Every day, he will walk in a planned route consisting of repeating n steps, denoted by ci and di meaning that he will walk from (a1,a2,,aci,,ak) to (a1,a2,,aci+di,,ak) (1cik,di{1,1}). C will repeat the route until he leaves the field. That is, if C is still in the field after n steps, he will go back to step 1 and start all over again.

Find the number of steps C took after P days.

Input Specification

The first line contains two integers n,k, denoting the number of steps in C's route and the number of dimensions of the field. The next line contains k integers wi, denoting the sizes of dimensions of the field. The next n lines contain two integers on every line, denoting ci,di, as mentioned above.

Output Specification

Output one integer denoting the answer modulo 109+7. If C will be stuck in the field forever on one day, output -1.

Sample Input 1

Copy
3 2
3 3
1 1
2 -1
1 1

Sample Output 1

Copy
21

Starting at (1,1) will take 2 steps, starting at (1,2) will take 4 steps, starting at (1,3) will take 4 steps.
Starting at (2,1) will take 2 steps, starting at (2,2) will take 3 steps, starting at (2,3) will take 3 steps.
Starting at (3,1) will take 1 steps, starting at (3,2) will take 1 steps, starting at (3,3) will take 1 steps.
The answer would be 21.

Sample Input 2

Copy
5 4
6 8 6 5
3 1
2 1
1 1
2 1
2 -1

Sample Output 2

Copy
10265

Constraints

For all test cases, 1n5×105, 1k10, 1wi109, di{1,1}.

For partials, refer to the table below.

Test Case n= k= wi
1~3 5 5 3
4~6 100 3 10
7~8 105 1 105
9~12 105 2 106
13~16 5×105 10 106
17~20 5×105 3 109

Comments

There are no comments at the moment.