THICC '17 P2 - Molly and Product

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 16M
Python 128M

Author:
Problem type

Molly has a math addiction. For her birthday, she receives a sequence of length N defined by Ai=(Ai1×B)modM. Given the value of A0, help Molly find the sum of all pairwise products, mod 109+7.

Input Specification

The first and only line of input will contain N, A0, B, and M, each space-separated.

Output Specification

The output should contain a single integer, the sum of all pairwise products, mod 109+7.

Constraints

For all subtasks:

1A0,B,M109

Subtask 1 [40%]

1N103

Subtask 2 [40%]

1N105

Subtask 3 [20%]

1N107

Sample Input

Copy
3 6 3 100

Sample Output

Copy
2808

Explanation for Sample Output

The three numbers are 6, 18, and 54. Their pairwise products are 6×18=108, 6×54=324, 18×6=108, 18×54=972, 54×6=324 and 54×18=972 and their sum is 2808.


Comments

There are no comments at the moment.