Candies for Friends
Limits: 4 sec., 1024 MiB
This statement was translated automatically from Ukrainian.
Zenyk and Marichka decided to invite their friends over after an exhausting Olympiad to their apartment and treat them to candies.
They have \(n\) algorithmic friends. Some of them are so dedicated that they do not stop solving problems neither day nor night, and will be writing the AtCoder Regular Contest today. They do not know the exact number of friends who plan to write this contest (and will not come to visit because of this). However, they know that there will be at least one such friend, but not more than \(k\). That is, \(n-1\), \(n-2\), ..., or \(n-k\) friends can accept Zenyk and Marichka’s invitation.
Zenyk and Marichka must make the best possible impression on their guests. They want to buy candies so that they can divide them equally among the friends, no matter how many of them gather. Note that they cannot keep some candies for themselves or distribute them unequally, as this would violate the rules of etiquette. Therefore, they will buy exactly \(\text{LCM}(n-1, n-2, \dots, n-k)\) candies.
After Zenyk and Marichka bought the candies, they noticed that they can divide this amount among all \(n\) friends.
Makeshift value of \(k\) is known to you, but you do not know the number of friends \(n\). Count how many such values of \(n\) satisfy the condition. Formally, you need to count the number of natural numbers \(n > k\) such that \(\text{LCM}(n-1, n-2, \dots, n-k)\) is divisible by \(n\). Since this count can be very large, you should calculate the remainder of its division by the prime number 998244353.
Zenyk and Marichka want everything to go perfectly, so they consider \(t\) different values of \(k\), for each of which they ask you to solve the problem independently.
Input
The first line contains a single integer \(t\) — the number of queries.
The next line contains \(t\) integers \(k\), for each of which you need to solve the problem.
Output
In a single line, output \(t\) integers — the answers to the corresponding queries, that is, the number of integers \(n\) for which \(\text{LCM}(n-1, n-2, \dots, n-k)\) is divisible by \(n\) modulo 998244353.
Constraints
\(1 \le t \le 10^5\),
\(1 \le k \le 10^7\).
Problem evaluation consists of the following subtasks:
1 point — sample from the statement,
4 points — \(t = 1, k \le 20\),
10 points — \(t = 1, k \le 10^7\),
10 points — without additional constraints.
Points for a subtask will be awarded only if you give the correct answer to all tests in the subtask.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 2 4 101 | 2 322961306 |
Notes
\(\text{LCM}(n-1, n-2, \dots, n-k)\) is the least common multiple, i.e., the smallest natural number that is divisible by \(n-1, n-2, \dots, n-k\).
For \(k=4\), the condition is satisfied by \(n=6\) and \(n=12\).
\(\text{LCM}(5, 4, 3, 2) = 60\) is divisible by 6.
\(\text{LCM}(11, 10, 9, 8) = 3960\) is divisible by 12.
For \(k=101\), the answer is \(322961306 \equiv 1321205659\pmod{998244353}\).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Candies for Friends - Editorial
Consider a prime divisor \(p\) of \(n\), and let \(b\) be the exponent of \(p\) in the factorization of \(n\), that is, the largest integer such that \(n\) is divisible by \(p^b\). If LCM(\(n-1, n-2, \dots, n-k\)) is divisible by \(n\), it is also divisible by \(p^b\). One can also notice that for this, one of the numbers \(n - 1, n - 2, ..., n - k\) must be divisible by \(p^b\). Let this number be \(n - x\). Since \(n\) is also divisible by \(p^b\), it follows that \(x\) is divisible by \(p^b\) too. We can also say that \(p^b \le x\), so the number \(p^b\) is among the numbers 1, 2, ..., \(k\). So LCM(\(n - 1, n - 2, ..., n - k\)) is divisible by \(n\) if and only if LCM(1, 2, ..., \(k\)) is divisible by \(n\). This means we need to count the divisors of LCM(1, 2, ..., \(k\)). For the first test group, we could compute this number, go over all its divisors \(n\) and count how many of them are greater than \(k\). As \(k\) grows, this value grows very fast, so this solution cannot be used for the whole problem.
For the group with a single \(k\), note that if we can write LCM(1, 2, ..., \(k\)) as \(p_1^{b_1} \cdot p_2^{b_2} \cdot ... \cdot p_m^{b_m}\), then the number of its divisors is \((b_1 + 1) \cdot (b_2 + 1) \cdot ... \cdot (b_m + 1)\). Also, all numbers from 1 to \(k\) are divisors of the LCM, so we can subtract \(k\) from the total number of divisors. In the previous paragraph we showed that the LCM can be divisible by some \(p^b\) only if \(p^b \le k\). So for each prime \(p \le k\) it is enough to find the largest \(b\) with \(p^b \le k\) and compute the product of \(b + 1\) modulo the modulus.
To solve the whole problem, one needs to realize that as \(k\) increases, \(b\) can increase for some \(p\). So we process the queries in sorted order, and sort all numbers of the form \(p^b\) not exceeding \(10^7\). We use two pointers. When we go from one \(k\) to another, some values of \(b\) change, so we need to update the answer: divide by the old value of \(b\) + 1 and multiply by the new one. Division modulo \(M = 998244353\) by a number \(x\) coprime with the modulus is the same as multiplication by the number \(y\) such that \(x \cdot y \mod{M} = 1\). For a prime modulus, Fermat’s little theorem gives \(x^{M - 1} \mod{M} = 1\), so \(y = x^{M - 2} \mod{M}\), which can be computed with fast exponentiation. Also, since the values of \(b\) are at most 23, we could compute the inverse of each number from 1 to 23 and store them in an array.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|