#P10663. BZOJ4833 最小公倍佩尔数

    ID: 12163 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>数论O2优化莫比乌斯反演容斥原理

BZOJ4833 最小公倍佩尔数

Background

This problem comes from the BZOJ April 2017 monthly contest.

Problem Description

Let (1+2)n=e(n)+2f(n)(1+\sqrt{2})^n=e(n)+\sqrt{2}f(n), where e(n),f(n)e(n), f(n) are both integers. It is clear that (12)n=e(n)2f(n)(1-\sqrt{2})^n=e(n)-\sqrt{2}f(n). Let g(n)=lcm(f(1),f(2),,f(n))g(n)=\operatorname{lcm}(f(1),f(2),\dots,f(n)).

Given two positive integers n,pn, p, where pp is a prime number, and it is guaranteed that f(1),f(2),,f(n)f(1), f(2), \dots, f(n) are all non-zero modulo pp, compute the value of i=1ni×g(i)\sum \limits_{i=1}^n i\times g(i) modulo pp.

Input Format

The first line contains a positive integer TT, meaning there are TT test cases.

The following lines are the testdata. Each test case occupies one line and contains two positive integers nn and pp.

Output Format

For each test case, output one line containing one non-negative integer, which is the answer for this test case.

5
1 233
2 233
3 233
4 233
5 233
1
5
35
42
121

Hint

Constraints: for 100%100\% of the testdata, 1T2101\leq T\leq 210, 1n1061\leq n\leq 10^6, 2p109+72\leq p\leq 10^9+7, n3×106\sum n\leq 3\times 10^6.

Translated by ChatGPT 5