Sequence Evaluation
시간 제한5초메모리 제한1024 MB
점화식 a_n = X * sum(a_i/(n-i))으로 정의된 수열에서 a_{P-K}를 소수 P로 나눈 나머지를 구한다. K는 8 이하다.
문제
Let , , and be integers, where is prime and .
Define a sequence of rational numbers as follows:
- .
- For all , .
Output modulo (note the unusual modulo). Formally, let in lowest terms, and output an integer such that . We can show that such a 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 , the number of test cases. The description of each test case follows.
Each test case consists of one line of input with three integers , , and (, , , is prime).
It is guaranteed that the sum of over all test cases does not exceed .
출력
For each test case, output a line with a single integer: modulo .
힌트
In the first test case, we have , , , and we want to find .
We may evaluate the initial elements of as follows:
- .
- .
- .
Since , the answer is .