#P5702. 调和级数求和

    ID: 5030 远端评测题 8000ms 256MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>数学倍增O2优化快速傅里叶变换 FFT

调和级数求和

Description

Given n,pn, p, find the value of:

i=1n1i\sum_{i=1}^n \frac 1i

modulo pp.

If you do not know how to take a fraction modulo a number, you can refer to this problem.
It is guaranteed that the answer exists modulo pp.

To make your computation easier, the smallest primitive root gg of pp will be provided.

Input Format

The first line contains a positive integer TT, the number of test cases.
The next TT lines each contain three positive integers n,p,gn, p, g.

Output Format

Output TT lines. Each line contains one integer, the answer.

5
998007 998244353 3
19260817 998244353 3
274829164 998244353 3
792846153 998244353 3
1924762 899678209 7
429767635
632288905
445668022
128133635
3097708

Hint

Constraints
For 30%30\% of the testdata, 1n1061\le n \le 10^6.
For 100%100\% of the testdata, 1n<p<2301 \le n < p < 2^{30}, 1T201\le T \le 20.
It is guaranteed that pp is prime, and p1p-1 is divisible by 2192^{19}.

Note: The time limit is three times that of std. If you cannot pass, please check that your time complexity is correct and optimize the constant factors.

Translated by ChatGPT 5