Sequence Evaluation

시간 제한5초메모리 제한1024 MB

요약
점화식 a_n = X * sum(a_i/(n-i))으로 정의된 수열에서 a_{P-K}를 소수 P로 나눈 나머지를 구한다. K는 8 이하다.
난이도

어려움10점 중 8점

유형
수학, 정수론, 조합론, 누적 합
정답자
아직 제출이 없습니다

문제

Let KK, PP, and XX be integers, where PP is prime and 0≤K≤min⁡(P−1,8)0 \le K \le \min(P-1, 8).

Define a sequence aa of rational numbers as follows:

  • a_1=1a\_1 = 1.
  • For all n≥2n \ge 2, a_n=X∑_i=1n−1a_in−ia\_n = X \sum\_{i=1}^{n-1} \frac{a\_i}{n-i}.

Output a_P−Ka\_{P-K} modulo PP (note the unusual modulo). Formally, let a_P−K=xya\_{P-K} = \frac{x}{y} in lowest terms, and output an integer 0≤b<P0 \le b < P such that by≡x(modP)by \equiv x \pmod{P}. We can show that such a bb exists and is unique under the constraints of this problem.

We recommend that C++ users use the following code, from KACTL, to perform modulo operations faster. Note that creating FastMod instances is a relatively slow operation, so avoid repeatedly doing so for the same modulo.

typedef unsigned long long ull;
struct FastMod {
    ull b, m;
    FastMod(ull b) : b(b), m(-1ULL / b) {}
    ull reduce(ull a) {
        ull q = (ull)((__uint128_t(m) * a) >> 64), r = a - q * b;
        return r - (r >= b) * b;
    }
};

입력

Each test contains multiple test cases. The first line of input contains a single integer TT (1≤T≤200)(1 \leq T \leq 200), the number of test cases. The description of each test case follows.

Each test case consists of one line of input with three integers KK, PP, and XX (0≤K≤min⁡(P−1,8)\mathbf{0 \le K \le \min(P-1, 8)}, 3≤P<2⋅1073 \le P < 2 \cdot 10^7, 1≤X≤1091 \le X \le 10^9, PP is prime).

It is guaranteed that the sum of PP over all test cases does not exceed 2⋅1072 \cdot 10^7.

출력

For each test case, output a line with a single integer: a_P−Ka\_{P-K} modulo PP.

힌트

In the first test case, we have K=2K = 2, P=5P = 5, X=3X = 3, and we want to find a_P−K=a_3a\_{P-K} = a\_3.

We may evaluate the initial elements of aa as follows:

  • a_1=1a\_1 = 1.
  • a_2=Xa_1=3a\_2 = Xa\_1 = 3.
  • a_3=X(a_1/2+a_2)=3(7/2)=21/2a\_3 = X(a\_1/2 + a\_2) = 3(7/2) = 21/2.

Since 2⋅3≡21(mod5)2 \cdot 3 \equiv 21 \pmod{5}, the answer is 3(mod5)3 \pmod 5.

예제1

  1. 예제 1

    입력
    2
    2 5 3
    8 199999 123456789
    
    예상 출력
    3
    42180