Total Inversion Count of Digit Strings
For a digit string d_1 d_2... d_n with digits in {0, 1,..., 9}, the inversion count is #{(i,j): 1 <= i < j <= n, d_i > d_j}. Let T(n) be the sum of inversion counts over all 10^n digit strings of l...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The inversion count of a sequence of digits is the smallest number of adjacent pairs that must be swapped to sort the sequence.
For example, $34214$ has inversion count of $5$: $34214 \to 32414 \to 23414 \to 23144 \to 21344 \to12344$.
If each digit of a sequence is replaced by one of its divisors a divided sequence is obtained.
For example, the sequence $332$ has $8$ divided sequences: $\{332,331,312,311,132,131,112,111\}$.
Define $G(N)$ to be the concatenation of all primes less than $N$, ignoring any zero digit.
For example, $G(20) = 235711131719$.
Define $F(N)$ to be the sum of the inversion count for all possible divided sequences from the master sequence $G(N)$.
You are given $F(20) = 3312$ and $F(50) = 338079744$.
Problem 705: Total Inversion Count of Digit Strings
Mathematical Foundation
Theorem 1 (Total Inversion Formula). For all ,
Proof. We use linearity of summation. The total inversion count across all strings is
For a fixed pair with , the inner sum counts the number of strings where . The digits and are chosen independently from , and the remaining digits are free. The number of pairs with is . Therefore:
Summing over all position pairs:
Lemma 1 (Modular Computation). To compute where is prime, we evaluate:
Proof. Direct substitution of into the formula. The modular inverse exists since , and is computed via Fermat’s little theorem as . The term is computed using Fermat’s little theorem: since , we reduce the exponent modulo .
Editorial
We compute n mod p, (n-1) mod p. We then since 10^{p-1} ≡ 1 (mod p), reduce exponent mod (p-1). Finally, combine: 45 * n * (n-1) / 2 * 10^{n-2}.
Pseudocode
p = 10^9 + 7 (prime)
Compute n mod p, (n-1) mod p
Compute 2^{-1} mod p via Fermat
Compute 10^{n-2} mod p
Since 10^{p-1} ≡ 1 (mod p), reduce exponent mod (p-1)
Combine: 45 * n * (n-1) / 2 * 10^{n-2}
Complexity Analysis
- Time: for modular exponentiation. All other operations are .
- 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;
const long long MOD = 1e9 + 7;
long long power(long long a, long long b, long long mod) {
long long res = 1; a %= mod;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
long long n = 1e16;
long long n_mod = n % MOD;
long long nm1 = (n - 1) % MOD;
long long inv2 = power(2, MOD - 2, MOD);
long long p10 = power(10, n - 2, MOD);
long long ans = 45 % MOD * n_mod % MOD;
ans = ans * nm1 % MOD;
ans = ans * inv2 % MOD;
ans = ans * p10 % MOD;
cout << ans << endl;
return 0;
}
"""
Problem 705: Total Inversion Count of Digit Strings
"""
def solve():
MOD = 10**9 + 7
n = 10**16
# T(n) = 45 * n * (n-1) / 2 * 10^(n-2)
n_mod = n % MOD
nm1 = (n - 1) % MOD
inv2 = pow(2, MOD - 2, MOD)
power10 = pow(10, n - 2, MOD)
result = 45 * n_mod % MOD * nm1 % MOD * inv2 % MOD * power10 % MOD
return result
answer = solve()
print(answer)