1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
    Potyczki Algorytmiczne 2019 (zad.: APB)
    Copyright 2019 by Michał Gibas
*/
#include <cstdio>
#include <utility>
#include <string>

using ull = unsigned long long;

ull possiblePairs(const ull& number);

ull tenPwr(ull n);

ull specialSum(ull a, ull b);

size_t charCount(const ull& x);

int main(void) {
    ull x;
    scanf("%llu", &x);
    printf("%llu \n", possiblePairs(x));
    return 0;
}

//----
ull possiblePairs(const ull& number) {
    if(number == 0)
        return 1;
    
    ull pairCount = 2;

    for(size_t i = 1; i < number; ++i) {
        for(size_t j = 1; j < number; ++j) {
            if(specialSum(i, j) == number)
                ++pairCount;
        }
    }

    return pairCount;
}
//----

ull specialSum(ull a, ull b) {
    if(a < b)
        std::swap(a, b);
    
    ull sum = 0;

    size_t size = charCount(a);

    size_t offset = 0;
    for(size_t i = 0; i < size; ++i) {
        unsigned int cA = (a % tenPwr(i+1) / tenPwr(i));
        unsigned int cB = (b % tenPwr(i+1) / tenPwr(i));

        sum += (cA + cB) * tenPwr(i+offset);
        
        if( (cA + cB) >= 10 )
            ++offset;
    }

    return sum;
}

ull tenPwr(ull n) {
    ull result = 1;
    for(ull i = 0; i < n; ++i)
        result *= 10;
    return result;
}

size_t charCount(const ull& x) {
    for(size_t i=18; i >= 0; --i) {
        if( (x % tenPwr(i) / tenPwr(i-1) ) != 0)
            return i;     
    }

    return 1;
}