All Euler problems
Project Euler

Double Pandigital Number Divisible by 11

We call a positive integer double pandigital if it uses all the digits 0 to 9 exactly twice (with no leading zero). How many double pandigital numbers are divisible by 11?

Source sync Apr 19, 2026
Problem #0491
Level Level 09
Solved By 2,478
Languages C++, Python
Answer 194505988824000
Length 450 words
modular_arithmeticlinear_algebranumber_theory

Problem Statement

This archive keeps the full statement, math, and original media on the page.

We call a positive integer double pandigital if it uses all the digits \(0\) to \(9\) exactly twice (with no leading zero). For example, \(40561817703823564929\) is one such number.

How many double pandigital numbers are divisible by \(11\)?

Problem 491: Double Pandigital Number Divisible by 11

Notation

Throughout, a double pandigital number is a 20-digit string d1d2d20d_1 d_2 \cdots d_{20} (with d10d_1 \neq 0) in which every digit 0,1,,90, 1, \ldots, 9 appears exactly twice. Positions 1,3,5,,191, 3, 5, \ldots, 19 are called odd positions and positions 2,4,6,,202, 4, 6, \ldots, 20 are called even positions. We write O=i odddiO = \sum_{i \text{ odd}} d_i and E=i evendiE = \sum_{i \text{ even}} d_i.

Mathematical Foundation

Theorem 1 (Divisibility rule for 11). An integer NN with decimal representation d1d2dnd_1 d_2 \cdots d_n satisfies 11N11 \mid N if and only if i=1n(1)i+1di0(mod11)\sum_{i=1}^{n}(-1)^{i+1} d_i \equiv 0 \pmod{11}.

Proof. Since 101(mod11)10 \equiv -1 \pmod{11}, we have 10k(1)k(mod11)10^k \equiv (-1)^k \pmod{11} for all k0k \geq 0. Therefore

N=i=1ndi10nii=1ndi(1)ni(mod11).N = \sum_{i=1}^{n} d_i \cdot 10^{n-i} \equiv \sum_{i=1}^{n} d_i \cdot (-1)^{n-i} \pmod{11}.

For n=20n = 20 (even), (1)ni=(1)i=(1)i(-1)^{n-i} = (-1)^{-i} = (-1)^i, so Ni=120(1)idi=EO(mod11)N \equiv \sum_{i=1}^{20} (-1)^i d_i = E - O \pmod{11}. Hence 11N11 \mid N if and only if OE(mod11)O \equiv E \pmod{11}. \square

Lemma 1 (Reduction to a single congruence). The divisibility condition OE(mod11)O \equiv E \pmod{11} is equivalent to O1(mod11)O \equiv 1 \pmod{11}.

Proof. Since every digit 0,1,,90, 1, \ldots, 9 appears exactly twice, the total digit sum is 2(0+1++9)=902(0 + 1 + \cdots + 9) = 90. Thus O+E=90O + E = 90, giving E=90OE = 90 - O. The condition OE(mod11)O \equiv E \pmod{11} becomes O90O(mod11)O \equiv 90 - O \pmod{11}, i.e., 2O90(mod11)2O \equiv 90 \pmod{11}. Now 90=811+290 = 8 \cdot 11 + 2, so 902(mod11)90 \equiv 2 \pmod{11}. Hence 2O2(mod11)2O \equiv 2 \pmod{11}. Since gcd(2,11)=1\gcd(2, 11) = 1, we may divide both sides by 22 (equivalently, multiply by 216(mod11)2^{-1} \equiv 6 \pmod{11}) to obtain O1(mod11)O \equiv 1 \pmod{11}. \square

Definition (Assignment vector). An assignment vector is a tuple c=(c0,c1,,c9){0,1,2}10\mathbf{c} = (c_0, c_1, \ldots, c_9) \in \{0, 1, 2\}^{10} satisfying d=09cd=10\sum_{d=0}^{9} c_d = 10, where cdc_d denotes the number of copies of digit dd placed at odd positions. The complementary count at even positions is 2cd2 - c_d.

Lemma 2 (Feasible values of OO). Under any assignment vector c\mathbf{c}, the odd-position sum is O(c)=d=09dcdO(\mathbf{c}) = \sum_{d=0}^{9} d \cdot c_d. Since 0cd20 \leq c_d \leq 2 and cd=10\sum c_d = 10, we have 0O900 \leq O \leq 90. The values satisfying O1(mod11)O \equiv 1 \pmod{11} in [0,90][0, 90] are exactly

O{1,12,23,34,45,56,67,78,89}.O \in \{1, 12, 23, 34, 45, 56, 67, 78, 89\}.

Proof. The minimum of OO is 00 (attained when cd=2c_d = 2 for d=0,1,2,3,4d = 0, 1, 2, 3, 4 and cd=0c_d = 0 otherwise) and the maximum is 9090 (attained when cd=2c_d = 2 for d=5,6,7,8,9d = 5, 6, 7, 8, 9). The integers congruent to 1(mod11)1 \pmod{11} in {0,1,,90}\{0, 1, \ldots, 90\} are 1,12,23,34,45,56,67,78,891, 12, 23, 34, 45, 56, 67, 78, 89. The value O=0O = 0 does not satisfy the congruence, nor does O=90O = 90. \square

Theorem 2 (Counting formula). For a valid assignment vector c\mathbf{c} (i.e., cd=10\sum c_d = 10 and dcd1(mod11)\sum d \cdot c_d \equiv 1 \pmod{11}), the number of double pandigital numbers with no leading zero is

N(c)=10!d=09cd!odd-position permutations10!d=09(2cd)!even-position permutations    1c019!(c01)!d=19cd!10!d=09(2cd)!.N(\mathbf{c}) = \underbrace{\frac{10!}{\prod_{d=0}^{9} c_d!}}_{\text{odd-position permutations}} \cdot \underbrace{\frac{10!}{\prod_{d=0}^{9} (2-c_d)!}}_{\text{even-position permutations}} \;-\; \mathbf{1}_{c_0 \geq 1} \cdot \frac{9!}{(c_0 - 1)!\, \prod_{d=1}^{9} c_d!} \cdot \frac{10!}{\prod_{d=0}^{9} (2-c_d)!}.

Proof. Given c\mathbf{c}, the 10 odd-position slots receive cdc_d copies of digit dd for each d{0,,9}d \in \{0, \ldots, 9\}. The number of distinct permutations of these 10 items is the multinomial coefficient (10c0,c1,,c9)=10!/cd!\binom{10}{c_0, c_1, \ldots, c_9} = 10!/\prod c_d!. Similarly, the 10 even-position slots receive 2cd2 - c_d copies of each digit dd, yielding 10!/(2cd)!10!/\prod (2 - c_d)! permutations. Since the choices for odd and even positions are independent, the total number of 20-digit strings (including those with a leading zero) is the product of these two multinomials.

To exclude leading zeros: position 1 is an odd-position slot. A leading zero occurs precisely when digit 00 occupies position 1. If c01c_0 \geq 1, we can fix one zero at position 1 and permute the remaining 99 odd-position digits (consisting of c01c_0 - 1 zeros and cdc_d copies of each d1d \geq 1) in 9!/((c01)!d1cd!)9!/((c_0-1)! \prod_{d \geq 1} c_d!) ways. Multiplying by the even-position count and subtracting yields the formula. If c0=0c_0 = 0, no odd-position slot contains a zero, so the leading digit is never zero and no subtraction is needed. \square

Corollary. The answer is c validN(c)\displaystyle \sum_{\mathbf{c} \text{ valid}} N(\mathbf{c}).

Editorial

We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

answer = 0
For each each (c_0, c_1, ..., c_9) in {0,1,2}^10:
    If sum(c_d) != 10 then continue
    O = sum(d * c_d for d = 0..9)
    If O mod 11 != 1 then continue
    odd_perms = 10! / prod(c_d!)
    even_perms = 10! / prod((2 - c_d)!)
    total = odd_perms * even_perms
    If c_0 >= 1 then
        leading_zero = 9! / ((c_0 - 1)! * prod(c_d! for d=1..9)) * even_perms
        total -= leading_zero
    answer += total
Return answer

Complexity Analysis

  • Time: O(31010)=O(590490)O(3^{10} \cdot 10) = O(590490). There are 310=590493^{10} = 59049 candidate vectors, each processed in O(10)O(10) arithmetic operations.
  • Space: O(1)O(1) auxiliary (beyond the precomputed table of 10!,9!,,0!10! , 9!, \ldots, 0!).

Answer

194505988824000\boxed{194505988824000}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_491/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main(){
    // Each digit 0-9 appears exactly twice in a double pandigital number (20 digits).
    // We choose c[d] in {0,1,2} copies of digit d to place at odd positions (10 positions).
    // Sum of c[d] must be 10.
    // Divisibility by 11: O - E ≡ 0 (mod 11) where O = sum of odd-position digits.
    // O + E = 90, so O ≡ 1 (mod 11).

    long long fact[11];
    fact[0] = 1;
    for(int i = 1; i <= 10; i++) fact[i] = fact[i-1] * i;

    // We enumerate choices c[0..9] in {0,1,2}, sum = 10
    // For each valid choice, count = (10! / prod c[d]!) * (10! / prod (2-c[d])!)
    // Then subtract leading-zero cases.

    long long total = 0;
    long long leading_zero = 0;

    // Use recursion or iterative enumeration
    // c[d] in {0,1,2}, 10 digits -> 3^10 = 59049 combos

    int c[10];
    // iterate over all 3^10 combinations
    for(int mask = 0; mask < 59049; mask++){
        int tmp = mask;
        int sum_c = 0;
        int sum_val = 0;
        bool valid = true;
        for(int d = 0; d < 10; d++){
            c[d] = tmp % 3;
            tmp /= 3;
            sum_c += c[d];
            sum_val += d * c[d];
        }
        if(sum_c != 10) continue;
        if(sum_val % 11 != 1) continue;

        // Count permutations for odd positions: 10! / prod(c[d]!)
        long long odd_perms = fact[10];
        for(int d = 0; d < 10; d++) odd_perms /= fact[c[d]];

        // Count permutations for even positions: 10! / prod((2-c[d])!)
        long long even_perms = fact[10];
        for(int d = 0; d < 10; d++) even_perms /= fact[2 - c[d]];

        long long ways = odd_perms * even_perms;
        total += ways;

        // Leading zero: digit 0 is at position 1 (odd position).
        // If c[0] >= 1, fix one 0 at position 1, remaining 9 odd positions
        // have the other digits. The count of such arrangements:
        // (c[0]/10) * odd_perms * even_perms ... but more precisely:
        // Among odd_perms arrangements, fraction c[0]/10 have 0 in the first spot.
        if(c[0] >= 1){
            // Number of odd arrangements with 0 in first position:
            // Fix 0 there. Remaining 9 positions: (9! / ((c[0]-1)! * prod_{d>0} c[d]!))
            long long odd_lz = fact[9];
            odd_lz /= fact[c[0] - 1];
            for(int d = 1; d < 10; d++) odd_lz /= fact[c[d]];
            leading_zero += odd_lz * even_perms;
        }
    }

    cout << total - leading_zero << endl;
    return 0;
}