JB Math Club 26-27 · Problem set
Modular arithmetic · Problem set
What is the remainder when \(70!\) is divided by \(71\)?
Since \(71\) is prime, Wilson's theorem gives
\[70!=(71-1)!\equiv-1\pmod{71}.\]
The nonnegative representative of \(-1\) modulo \(71\) is \(70\).
\[\boxed{70}\]
What is the remainder when \(80!\) is divided by \(83\)?
The number \(83\) is prime. Wilson's theorem applies to \(82!\), which differs from \(80!\) by two factors:
\[82!=80!\cdot81\cdot82\equiv80!\cdot(-2)(-1)=2\cdot80!\pmod{83}.\]
Consequently, \(2\cdot80!\equiv-1\equiv82\pmod{83}\). Since \(2\) is coprime to \(83\), this congruence has a unique solution for \(80!\) modulo \(83\). The number \(41\) works because \(2\cdot41=82\).
\[\boxed{41}\]
What is the remainder when \(201!\) is divided by \(10201\)?
First observe that \(10201=101^2\). Among \(1,2,\ldots,201\), the only multiple of \(101\) is \(101\) itself. Write
\[201!=101A,\qquad A=100!\prod_{r=1}^{100}(101+r).\]
We only need \(A\) modulo \(101\): multiplying a congruence modulo \(101\) by \(101\) gives a congruence modulo \(101^2\). Reducing the factors of the second block gives
\[A\equiv100!\prod_{r=1}^{100}r=(100!)^2\equiv(-1)^2=1\pmod{101},\]
where the last congruence is Wilson's theorem for the prime \(101\). Thus \(A=1+101t\) for some integer \(t\), and
\[201!=101+10201t.\]
\[\boxed{101}\]
Wilson's theorem was used modulo the prime \(101\). It does not apply directly to the composite modulus \(10201\).
What is the remainder when \(200^{10001}\) is divided by \(101\)?
We have \(200\equiv-2\pmod{101}\). Since \(101\) is prime and does not divide \(2\), Fermat's little theorem gives \((-2)^{100}\equiv1\pmod{101}\). Now \(10001=100\cdot100+1\), so
\[200^{10001}\equiv(-2)^{10001}=\bigl((-2)^{100}\bigr)^{100}(-2)\equiv-2\equiv99\pmod{101}.\]
\[\boxed{99}\]
The base is reduced modulo \(101\); the exponent is reduced modulo \(100\).
What is the remainder when \(9^{1010}\) is divided by \(2017\)?
The number \(2017\) is prime, and \(3\) is coprime to it. Fermat's little theorem gives \(3^{2016}\equiv1\pmod{2017}\). Rewriting \(9=3^2\) makes the useful exponent visible:
\[9^{1010}=3^{2020}=3^{2016}\cdot3^4\equiv3^4=81\pmod{2017}.\]
\[\boxed{81}\]
Applying Fermat directly to base \(9\) would give exponent \(2016\), which is larger than \(1010\). Changing the base avoids a long calculation.
What is the remainder when \(3^{125}\) is divided by \(127\)?
Since \(127\) is prime and does not divide \(3\), Fermat's little theorem gives \(3^{126}\equiv1\pmod{127}\). Thus
\[3\cdot3^{125}\equiv1\pmod{127}.\]
We need the inverse of \(3\). It is \(85\), because
\[3\cdot85=255=2\cdot127+1.\]
The inverse is unique modulo \(127\), so \(3^{125}\equiv85\pmod{127}\).
\[\boxed{85}\]
Calculate \(\varphi(1)+\varphi(101)+\varphi(450)\).
By definition, \(\varphi(1)=1\): the integer \(1\) is coprime to \(1\). Since \(101\) is prime, \(\varphi(101)=100\).
Next, \(450=2\cdot3^2\cdot5^2\), so the totient formula gives
\[\varphi(450)=450\left(1-\frac12\right)\left(1-\frac13\right)\left(1-\frac15\right)=120.\]
Each distinct prime contributes one factor, regardless of its exponent in the factorization.
\[\boxed{\varphi(1)+\varphi(101)+\varphi(450)=1+100+120=221}\]
What is the remainder when \(7^{963}\) is divided by \(450\)?
We have \(\gcd(7,450)=1\), and Problem 7 gives \(\varphi(450)=120\). Euler's theorem therefore gives \(7^{120}\equiv1\pmod{450}\).
Since \(963=8\cdot120+3\),
\[7^{963}=(7^{120})^8\cdot7^3\equiv7^3=343\pmod{450}.\]
As \(0\le343<450\), this is already the required remainder.
\[\boxed{343}\]
A positive integer \(n\) is called remarkable if \(2^{n!}-1\) is divisible by \(n\). How many remarkable numbers not exceeding \(1000\) exist?
No even integer works. The number \(2^{n!}-1\) is odd, so it cannot be divisible by an even \(n\).
Every odd integer works. First, \(n=1\) works because every integer is divisible by \(1\). Now let \(n>1\) be odd. Then \(\gcd(2,n)=1\), so Euler's theorem gives
\[2^{\varphi(n)}\equiv1\pmod n.\]
The positive integer \(\varphi(n)\) is at most \(n-1\), so it occurs among the factors of \(n!\). In particular, \(\varphi(n)\mid n!\). Write \(n!=t\varphi(n)\). Then
\[2^{n!}=\bigl(2^{\varphi(n)}\bigr)^t\equiv1\pmod n.\]
Thus the remarkable integers are exactly the odd positive integers. There are \(500\) of these in \(1,2,\ldots,1000\).
\[\boxed{500}\]
Here is another proof that every odd \(n>1\) works, using only the pigeonhole principle and cancellation. Consider the \(n+1\) numbers
\[2^0,2^1,2^2,\ldots,2^n.\]
There are only \(n\) possible remainders modulo \(n\), so \(2^i\equiv2^j\pmod n\) for some \(0\le i<j\le n\). Since \(n\) is odd, \(2^i\) is coprime to \(n\), and we may cancel it:
\[2^{j-i}\equiv1\pmod n.\]
Put \(d=j-i\). Since \(1\le d\le n\), we have \(d\mid n!\). Raising the last congruence to the integer power \(n!/d\) gives \(2^{n!}\equiv1\pmod n\). Together with the even case and \(n=1\), this again gives exactly \(500\) remarkable integers.
What three-digit number is formed by the last three digits of \(7^{999999}\)?
The last three digits are determined by the remainder modulo \(1000\). Since \(\gcd(7,1000)=1\) and
\[\varphi(1000)=1000\left(1-\frac12\right)\left(1-\frac15\right)=400,\]
Euler's theorem gives \(7^{400}\equiv1\pmod{1000}\). The exponent \(1000000=2500\cdot400\) is especially convenient:
\[7\cdot7^{999999}=7^{1000000}=(7^{400})^{2500}\equiv1\pmod{1000}.\]
Thus the desired remainder is the inverse of \(7\) modulo \(1000\). Since \(7\cdot143=1001\equiv1\pmod{1000}\), we obtain
\[\boxed{143}\]
Modulo \(8\), the base is \(7\equiv-1\), and the exponent is odd. Therefore
\[7^{999999}\equiv7\pmod8.\]
Modulo \(125\), we have \(\varphi(125)=100\) and \(\gcd(7,125)=1\). Euler gives \(7^{100}\equiv1\pmod{125}\). Since \(100\mid1000000\), the desired power is the inverse of \(7\) modulo \(125\). That inverse is \(18\), because \(7\cdot18=126\).
The number \(143\) satisfies both required congruences:
\[143\equiv7\pmod8,\qquad143\equiv18\pmod{125}.\]
Hence \(7^{999999}-143\) is divisible by both \(8\) and \(125\). These numbers are coprime, so their product \(1000\) also divides the difference. Since \(143\) lies between \(0\) and \(999\), the last three digits are again \(143\).
Given a positive integer \(m>1000\), what is the remainder of \(2^{3^m}\) when divided by \(3^{m+1}\)? Express the answer in terms of \(m\).
We will prove the following stronger statement by induction:
\[3^{k+1}\mid2^{3^k}+1\qquad\text{for every integer }k\ge0.\]
For \(k=0\), this is \(3\mid2+1\), which is true. Suppose it holds for some \(k\ge0\), and put \(A=2^{3^k}\). Then \(3^{k+1}\mid A+1\), so in particular \(A\equiv-1\pmod3\). Now factor:
\[2^{3^{k+1}}+1=A^3+1=(A+1)(A^2-A+1).\]
The first factor is divisible by \(3^{k+1}\), and the second is divisible by \(3\), because
\[A^2-A+1\equiv(-1)^2-(-1)+1=3\equiv0\pmod3.\]
The product is therefore divisible by \(3^{k+2}\), completing the induction. Taking \(k=m\) gives \(2^{3^m}\equiv-1\pmod{3^{m+1}}\). The required nonnegative remainder is
\[\boxed{3^{m+1}-1}\]
Put \(X=2^{3^m}\). Since \(2\) is coprime to \(3^{m+1}\) and \(\varphi(3^{m+1})=2\cdot3^m\), Euler's theorem gives
\[X^2=2^{2\cdot3^m}\equiv1\pmod{3^{m+1}}.\]
Thus \(3^{m+1}\mid(X-1)(X+1)\). The two factors differ by \(2\), so they cannot both be divisible by \(3\). Consequently, all \(m+1\) factors of \(3\) must come from one of them.
Because \(3^m\) is odd, we have \(X\equiv(-1)^{3^m}=-1\pmod3\). Thus \(X-1\) is not divisible by \(3\); the factor divisible by \(3^{m+1}\) must be \(X+1\). This again gives remainder \(3^{m+1}-1\).
The argument actually works for every integer \(m\ge0\). The large lower bound in the problem does not change the answer. In the second solution, Euler gives the square congruence; the argument about the two factors is needed to determine the sign.
Given a prime number \(p>1000\), Alice has \(p-1\) cards, each labeled with a number from \(1\) to \(p-1\), with each label used once. She wants to take as many cards as possible such that the product of their labels leaves remainder \(123\) when divided by \(p\). How many cards should she take? Express the answer in terms of \(p\).
She cannot take all the cards. By Wilson's theorem, their product is
\[(p-1)!\equiv-1\pmod p.\]
This is not congruent to \(123\): otherwise \(p\mid124\), impossible for \(p>1000\). Thus she can take at most \(p-2\) cards.
She can take exactly that many. Since \(p\) is prime and greater than \(123\), the number \(123\) has an inverse modulo \(p\). Choose the unique \(b\in\{1,2,\ldots,p-1\}\) satisfying
\[123b\equiv-1\pmod p\qquad\bigl(b\equiv-123^{-1}\pmod p\bigr).\]
Omit the card labeled \(b\), and let \(P\) be the product of the remaining labels. Then
\[bP=(p-1)!\equiv-1\equiv123b\pmod p.\]
Since \(b\) is coprime to \(p\), cancellation gives \(P\equiv123\pmod p\). This construction uses \(p-2\) cards and attains the upper bound.
\[\boxed{p-2}\]
Given distinct prime numbers \(p\) and \(q\), both greater than \(1000\), what is the remainder of \(p^q+q^p\) when divided by \(pq\)? Express the answer in terms of \(p\) and \(q\).
Since \(p\) and \(q\) are distinct primes, Fermat's little theorem gives
\[p^{q-1}\equiv1\pmod q,\qquad q^{p-1}\equiv1\pmod p.\]
Now subtract the candidate remainder \(p+q\):
\[p^q+q^p-(p+q)=p\bigl(p^{q-1}-1\bigr)+q\bigl(q^{p-1}-1\bigr).\]
The first summand is divisible by \(pq\), because its bracket is divisible by \(q\). The second is also divisible by \(pq\), because its bracket is divisible by \(p\). Hence \(p^q+q^p\equiv p+q\pmod{pq}\).
It remains to check the range. We have \(p+q>0\), and
\[pq-p-q=(p-1)(q-1)-1>0.\]
Thus \(p+q\) is already the least nonnegative remainder.
\[\boxed{p+q}\]
Let \(N=p^q+q^p\). Modulo \(p\), the first term vanishes and Fermat gives \(q^p\equiv q\). Modulo \(q\), the second term vanishes and Fermat gives \(p^q\equiv p\). Therefore
\[N\equiv q\pmod p,\qquad N\equiv p\pmod q.\]
The number \(p+q\) has exactly these same two remainders. Hence both \(p\) and \(q\) divide \(N-(p+q)\). Since they are coprime, \(pq\) divides this difference. The range check above again shows that the remainder is \(p+q\).
If \(p\) is prime and \(a\not\equiv0\pmod p\), Fermat's little theorem gives
\[a^{p-1}\equiv1\pmod p\quad\Rightarrow\quad a^{p-2}\equiv a^{-1}\pmod p.\]
Compute the modular inverse of \(a\) modulo \(p\) by evaluating \(a^{p-2}\bmod p\) using binary exponentiation, also called exponentiation by squaring.
Two integers \(a\) and \(p\) separated by a space.
A single integer: the modular inverse of \(a\) modulo \(p\).
| Example | Input | Output |
|---|---|---|
| 1 | 3 7 | 5 |
| 2 | 2 5 | 3 |
| 3 | 10 17 | 12 |
In Example 1, \(3\cdot5\equiv1\pmod7\).
stdin and write to stdout.Maintain a running product, a current base, and the exponent still to be processed. An odd exponent contributes one copy of the current base to the product. Squaring the base then lets us halve the remaining exponent.
import sys
def inverse_mod(a, p):
result = 1
base = a % p
exponent = p - 2
while exponent > 0:
if exponent % 2 == 1:
result = (result * base) % p
base = (base * base) % p
exponent //= 2
return result
def main():
a, p = map(int, sys.stdin.buffer.read().split())
print(inverse_mod(a, p))
if __name__ == "__main__":
main()
The code performs the exponentiation explicitly. It does not use a built-in power or inverse function. Reducing \(a\) initially also handles negative inputs and inputs larger than \(p\).
For \(a=3\) and \(p=7\), the exponent is \(p-2=5\). Each row shows the state at a loop test.
| Stage | exponent | base | result |
|---|---|---|---|
| Initially | 5 | 3 | 1 |
| After iteration 1 | 2 | 2 | 3 |
| After iteration 2 | 1 | 4 | 3 |
| After iteration 3 | 0 | 2 | 5 |
At every loop test, the stored values \(r\), \(b\), and \(e\), standing for result, base, and exponent, satisfy
\[r\,b^e\equiv a^{p-2}\pmod p.\]
This relation holds initially because \(r=1\), \(b\equiv a\pmod p\), and \(e=p-2\). To see why an iteration preserves it, consider the two possible forms of \(e\):
Taking remainders after multiplication does not change these congruences. Repeated halving eventually gives \(e=0\), so the final result satisfies \(r\equiv a^{p-2}\pmod p\). Fermat's little theorem now gives
\[\boxed{ar\equiv a^{p-1}\equiv1\pmod p.}\]
The program keeps \(0\le r<p\), so it prints the required representative of the inverse.
Each iteration halves the exponent and uses at most two modular multiplications. Hence the algorithm uses \(O(\log p)\) modular operations and \(O(1)\) extra integer variables, as required in the standard arithmetic-operation model.
For \(p=2\), the exponent is \(0\), so the loop is skipped and the output is \(1\). This is correct: the only nonzero residue modulo \(2\) is \(1\), whose inverse is itself.