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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <bits/stdc++.h>
using namespace std;

struct number {
    long long int val;
    long long int pref;
    int index;          //in days
    int split[3];
    vector<long long int> count;
};

int multiplyDigits(long long int a) {
    long long int result = 1;
    while(a != 0) {
        result *= (a%10);
        a /= 10;
    }
    return (int)result;
}

void output(int t, vector<number> &days) {
    sort(days.begin(), days.end(), [](number &a, number &b)
                {return a.index < b.index;});
    for(int j=0; j<t; j++) {
        number &n = days[j];
        for(auto i : n.count) {
            cout << i << " ";
        }
        cout << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t;          //days
    cin >> t;
    vector<number> days(t+1);
    days[t].val = LONG_LONG_MAX;
    days[t].pref = LONG_LONG_MAX;
    days[t].split[0] = INT_MAX;
    days[t].index = INT_MAX;
    int mil = 1000000;
    for(int i=0; i<t; i++) {
        cin >> days[i].val;
        days[i].index = i;
        long long int temp = days[i].val;
        days[i].split[2] = (int)(temp%mil);
        temp /= mil;
        days[i].pref = temp;
        days[i].split[1] = (int)(temp%mil);
        days[i].split[0] = (int)(temp/mil);
        days[i].count.assign(10, 0);
    }
    sort(days.begin(), days.end(), [](number &a, number &b)
                {return a.val < b.val;});

    vector<int> roots(mil);
    for(int i=1; i<mil; i++) {
        roots[i] = multiplyDigits(i);
    }

    vector<long long int> counter(10, 0);
    int index = 0;          //which number are we looking for
    for(int i=1; i<mil; i++) {
        int val = i;
        while(val > 9) {
            val = roots[val];
        }
        counter[val]++;
        while(days[index].val == i) {
            days[index].count = counter;
            index++;
        }
    }
    if(index == t) {
        output(t, days);
        return 0;
    }
    //else -> something greater than a mil

    for(int j=1; j<mil; j++) {
        int cur = roots[j];
        if(cur == 0 && days[index].pref != j) {
            counter[0] += mil;
            continue;
        }
        //else -> need to check
        for(int i=0; i<mil; i++) {
            long long int val;
            if(i < 100000) {
                val = 0;
            }
            else {
                val = cur*roots[i];
                while(val >= mil)
                    val = multiplyDigits(val);
                while(val > 9) {
                    val = roots[val];
                }
            }
            counter[val]++;
            while(i == days[index].split[2] && j == days[index].pref) {
                days[index].count = counter;
                index++;
                if(index == t) {
                    output(t, days);
                    return 0;
                }
            }
        }
    }

    int rest = mil*mil;         //brute from now on, too slow anyway
    for(;;rest++) {
        counter[multiplyDigits(rest)]++;
        while(rest == days[index].val) {
            days[index].count = counter;
            index++;
            if(index == t) {
                output(t, days);
                return 0;
            }
        }
    }

    return 0;
}
/*
 https://math.stackexchange.com/questions/4731838/patterns-after-recursive-multiplication-of-the-digits-of-a-number-until-one-digi
 recursive digit product
 A031347 - multiplicative digital root
 */

/*
If at the beginning (in mil)
0: 902609
1: 6
2: 16673
3: 21
4: 2345
5: 2073
6: 43538
7: 21
8: 32658
9: 56

Otherwise
0: 920207
1: 1
2: 13460
3: 6
4: 1451
5: 1466
6: 36695
7: 6
8: 26687
9: 21
 */