Nth Digit of Reciprocals
Let d_n(x) be the n -th decimal digit of the fractional part of x (or 0 if the fractional part has fewer than n digits). Examples: d_7(1/3) = 3 since 1/3 = 0.3333333333... d_7(1/6) = 6 since 1/6 =...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let \(d(n)\) be the \(n^{th}\) decimal digit of the fractional part of \(x\), or \(0\) if the fractional part has fewer than \(n\) digits.
For example:
-
\(d_7(1) = d_7(\frac {1}{2}) = d_7(\frac {1}{4}) = d_7(\frac {1}{5}) = 0\)
-
\(d_7(\frac {1}{3}) = 3\) since \(\frac {1}{3} = 0.333333\textbf {\textcolor {red}{3}}333\ldots \)
-
\(d_7(\frac {1}{6}) = 6\) since \(\frac {1}{6} = 0.666666\textbf {\textcolor {red}{6}}666\ldots \)
-
\(d_7(\frac {1}{7}) = 1\) since \(\frac {1}{7} = 0.142857\textbf {\textcolor {red}{1}}428\ldots \)
Let \(S(n) = \sum _{k = 1}^{n} d_n(\frac {1}{k})\).
You are given:
-
\(S(7) = 0 + 0 + 3 + 0 + 0 + 6 + 1 = 10\)
-
\(S(100) = 418\).
Find \(S(10^7)\).
Problem 820: Nth Digit of Reciprocals
Mathematical Analysis
Key Observation
The -th decimal digit of can be computed via long division. After steps of long division, the remainder is . Then:
Proof
We have
The standard long division algorithm maintains a remainder :
By induction, , so:
Computation
For each from to :
- Compute using binary exponentiation in .
- Compute the digit as .
- Sum all digits.
Editorial
We use dynamic programming over the state space implied by the derivation, apply each admissible transition, and read the answer from the final table entry.
Pseudocode
total = 0
For k from 2 to N:
r = modpow(10, N-1, k)
digit = (10 * r) / k
total += digit
Return total
Correctness
Theorem. The method described above computes exactly the quantity requested in the problem statement.
Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer.
Complexity Analysis
- Time: — for each of values of , binary exponentiation takes .
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
// S(n) = sum_{k=1}^{n} d_n(1/k)
// d_n(1/k) = floor(10^n / k) mod 10
// We need d_n(1/k): the n-th decimal digit of 1/k
// 1/k = 0.d1 d2 d3 ... dn ...
// d_n(1/k) = floor(10^n * (1/k)) mod 10 = floor(10^n / k) mod 10
// But 10^n is huge for n=10^7. We use modular arithmetic:
// 10^n mod k gives us the remainder, then floor(10^n/k) mod 10 = (10 * (10^{n-1} mod k)) / k ...
// Actually: d_n(1/k) = floor(10 * r / k) where r = 10^{n-1} mod k
// Because: 1/k has digits determined by long division.
// After n-1 steps, the remainder is 10^{n-1} mod k, then d_n = floor(10*r/k)
const int N = 10000000;
// We need 10^{N-1} mod k for each k from 1 to N.
// 10^{N-1} mod k can be computed using modular exponentiation for each k,
// but that's O(N log N) total which should be feasible.
// Alternative: use the fact that for the n-th digit of 1/k,
// we need r = 10^{n-1} mod k, then digit = (10*r) / k.
long long total = 0;
for(int k = 1; k <= N; k++){
if(k == 1){
// 1/1 = 1.000..., fractional part = 0, so d_n = 0
continue;
}
// Compute 10^{N-1} mod k using fast exponentiation
long long exp = (long long)N - 1;
long long base = 10 % k;
long long r = 1;
long long b = base;
long long e = exp;
while(e > 0){
if(e & 1) r = r * b % k;
b = b * b % k;
e >>= 1;
}
// digit = (10 * r) / k
int digit = (int)(10LL * r / k);
total += digit;
}
cout << total << endl;
return 0;
}
def solve():
N = 10**7
total = 0
for k in range(2, N + 1):
r = pow(10, N - 1, k)
digit = (10 * r) // k
total += digit
print(total)
if __name__ == "__main__":
solve()