Let L be the length of X (number of digits).

Then, $f(X,K) = \displaystyle\sum_{i=0}^{K-1}{10^{iL}}X = X\displaystyle\sum_{i=0}^{K-1}{10^{iL}} = X\frac{10^{KL}-1}{10^L-1}$

Now, we need to find the minimum K such that the above expression is divisible by P. That means $\frac{X(10^{KL}-1)}{P(10^L-1)}$ needs to be an integer.

Let G be the GCD of X and P (10L-1).

We can easily find $P' = \frac{P(10^L-1)}{G}$ with prime factorization.

At this point, we basically needs to find whether $\frac{10^{KL}-1}{P'}$ is an integer i.e. we need to find the minimum K such that

$10^{KL}-1 \equiv 0 \: \textrm{modulo} \: P'$

$10^{KL} \equiv 1 \: \textrm{modulo} \: P'$

Now, let's try to solve a simpler problem: finding minimum value of Z such that $10^{Z} \equiv 1 \: \textrm{modulo} \: N$.

When 10 and are not co-prime, it can be proven that can not exist.

Let's take a look at Euler's theorem now. It states that if and are coprime positive integers, then

$a^{\varphi (n)}\equiv 1\pmod n$

So, for $10^Z \equiv 1 \pmod N$, one possible solution is $Z=\varphi(N)$. But can't a smaller value for exist? Yet, it can. But even if it does, it must be a divisor of $\varphi(N)$. The proof of that is being left for the reader to figure out.

Let's return to our original equation to solve, $10^{KL} \equiv 1 \pmod P'$.

Within the constraints of this problem, we can simply traverse on all the divisors D of $\varphi(P')$ and find the optimal K for that divisor if $10^D \equiv 1 \pmod P'$. Needless to say, we choose the minimum value of K as output.

Statistics

23% Solution Ratio
EgorKulikovEarliest, Jan '20
Kuddus.6068Fastest, 0.0s
EgorKulikovLightest, 131 kB
rebornShortest, 1076B
Toph uses cookies. By continuing you agree to our Cookie Policy.