問題一覧 > 教育的問題

No.2993 Power of Power mod Power

レベル : / AI推定難易度 : 2700 / 実行時間制限 : 1ケース 10.000秒 / メモリ制限 : 512 MB / スペシャルジャッジ問題 (複数の解が存在する可能性があります)
タグ : (AC するまで非表示) / 解いたユーザー数 2
作問者 : 👑 p-adic / テスター : testestest
お気に入りにしたユーザー ProblemId : 9132 / 自分の提出
問題文最終更新日: 2023-12-12 07:51:09
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2024 (順位表) の他の問題:

Problem Statement

The time limit for this problem is 10000[ms]. The writer's solution runs in 415[ms] in C++ and 5293[ms] in PyPy3, and gets TLE in Python3, so using a fast language and implementation is recommended.

Problem Statement

Consider the following problem:

You are given as input an integer $B$ greater than or equal to $2$ and $2$ non-negative integers $N,M$.

Determine whether there exists an integer $a$ satisfying all of the following $2$ conditions, and if one exists, find one such $a$:

  1. $0 \leq a < B^N$.
  2. There exists a non-negative integer $n_0$ satisfying the following condition:
    • For any non-negative integer $n$ greater than or equal to $n_0$, $M^{B^n} \equiv 1 + B^n a \pmod{B^{n+N}}$.

Hereafter, the above $2$ conditions are called the solution conditions for this problem.

A positive integer $T$ is given at the beginning of the input. Answer $T$ problems.

Input

For each positive integer $t$ less than or equal to $T$, let $B_t,N_t,M_t$ denote the input $B,N,M$ for the $t$-th problem.

Then the input is given from Standard Input in $1 + T$ lines in the following format:

  • Line $1$ contains $T$.
  • For each positive integer $t$ less than or equal to $T$, line $1 + t$ contains the input $B_t, N_t, M_t$ for the $t$-th problem, separated by single-byte spaces.
$T$
$B_1$ $N_1$ $M_1$
$\vdots$
$B_T$ $N_T$ $M_T$

Constraints

The input satisfies the following constraints:

  • $T$ is an integer satisfying $1 \leq T \leq 10^5$
  • For any positive integer $t$ less than or equal to $T$,
    • $B_t$ is an integer satisfying $2 \leq B_t \leq 10^6$
    • $N_t$ is an integer satisfying $0 \leq N_t$ and $B_t^{N_t} \leq 10^6$
    • $M_t$ is an integer satisfying $0 \leq M_t \leq 10^{18}$

Output

For each positive integer $t$ less than or equal to $T$, if there exists an integer $a$ satisfying the solution conditions for the $t$-th problem, print such an $a$ on line $t$; otherwise, print -1 on line $t$.

Print a newline at the end.

 

This is a special judge problem. If there are multiple correct answers, you may print any of them.

However, strictly follow the format described above for the output. For example, the behavior of the judge is not guaranteed if there is extra whitespace.

Sample

Sample 1
Input
1
2 0 0
Output
-1

An integer $a$ satisfies the solution conditions for the test case $(B,N,M) = (2,0,0)$ if and only if it satisfies the following $2$ conditions:

  1. $0 \leq a < 1$.
  2. There exists a non-negative integer $n_0$ satisfying the following condition:
    • For any non-negative integer $n$ greater than or equal to $n_0$, $0 \equiv 1 + 2^n a \pmod{2^n}$.

By condition 1, $a$ must be $0$. However, if $a = 0$, then no matter how the non-negative integer $n_0$ is chosen, $0 \equiv 1 + 2^n a \pmod{2^n}$ does not hold when $n = \max \{n_0,1\}$, so condition 2 does not hold. Therefore, no integer $a$ satisfying the solution conditions exists.

Sample 2
Input
1
2 0 1
Output
0

An integer $a$ satisfies the solution conditions for the test case $(B,N,M) = (2,0,1)$ if and only if it satisfies the following $2$ conditions:

  1. $0 \leq a < 1$.
  2. There exists a non-negative integer $n_0$ satisfying the following condition:
    • For any non-negative integer $n$ greater than or equal to $n_0$, $1 \equiv 1 + 2^n a \pmod{2^n}$.

If we set $a = 0$, then $a$ satisfies condition 1. If we set $n_0 = 0$, then for any non-negative integer $n$ greater than or equal to $n_0$, $1 \equiv 1 + 2^n a \pmod{2^n}$ also holds, so condition 2 is satisfied as well. Therefore, $a$ satisfies the solution conditions.

Sample 3
Input
1
2 1 1
Output
0

An integer $a$ satisfies the solution conditions for the test case $(B,N,M) = (2,1,1)$ if and only if it satisfies the following $2$ conditions:

  1. $0 \leq a < 2$.
  2. There exists a non-negative integer $n_0$ satisfying the following condition:
    • For any non-negative integer $n$ greater than or equal to $n_0$, $1 \equiv 1 + 2^n a \pmod{2^{n+1}}$.

If we set $a = 0$, then $a$ satisfies condition 1. If we set $n_0 = 0$, then for any non-negative integer $n$ greater than or equal to $n_0$, $1 \equiv 1 + 2^n a \pmod{2^{n+1}}$ also holds, so condition 2 is satisfied as well. Therefore, $a$ satisfies the solution conditions.

Sample 4
Input
6
3 0 0
3 0 1
3 0 2
3 1 0
3 1 1
3 1 2
Output
-1
0
-1
-1
0
-1

Answer $T = 6$ problems.

Sample 5
Input
18
4 0 0
4 0 1
4 0 2
4 0 3
4 0 4
4 0 5
4 1 0
4 1 1
4 1 2
4 1 3
4 1 4
4 1 5
4 2 0
4 2 1
4 2 2
4 2 3
4 2 4
4 2 5
Output
-1
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
4
-1
12

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。