407700: GYM102878 B Residue Problem

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Residue Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Let function $$$f(i,r,P)$$$ equal the number of pairs of $$$(a,b),0\le a,b \lt P$$$ that satisfy the equation $$$a^r{(b+b^2)}^i\equiv b^i(\mathrm{mod}\; P)$$$, where $$$P$$$ is a prime. Given $$$m$$$ queries, each with $$$Y,N,r,P$$$, anwser $$${{{\displaystyle \prod_{i=1}^{N} Y^{f(i,r,P)_{}}}}}\mod 10^9+7$$$.

Input

First, an integer $$$m, 1 \le m \le 10^5 $$$, indicating the number of queries.

The following $$$m$$$ lines, each with four integers $$$Y,N,r,P$$$,( $$$1e5\le Y \lt 10^9+7$$$, $$$1\le N\lt P$$$, $$$1\le r \le 3$$$, $$$3\le P\lt10^9+7$$$), indicating the input of this query.

Output

Output $$$m$$$ lines, each line with an integer standing for the answer to the corresponding query.

ExampleInput
4
100000 1234567 1 999999937
100000 7654321 2 999999929
100000 7777777 3 999999893
100000 7777777 3 999999757
Output
968518472
236321637
770733917
509589974

加入题单

算法标签: