// Karol Kosinski 2025 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define X first #define Y second #define endl '\n' #define NAM(x) #x,'=',x," " using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; template<class...T> void _cout(T...a){(cout<<...<<a);} #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);} #endif #define DEBF(f,x...) DEB(1,1,f,x) #define DEBL DEBF(void,0) #define DEBC(p,x...) DEB(0,p,_cout,x) #define DEBUG(x...) DEBC(1,x) constexpr ULL NX = 1'000'000'000'000'000'000; // constexpr ULL NX = 100; constexpr int DX = 10; constexpr int N2 = 60; constexpr int N3 = 38; constexpr int N5 = 26; constexpr int N7 = 22; map<ULL, int> M; map<ULL, tuple<int, int, int, int, int>> FM; ULL Newt[21][21]; // int MC[10]; // int cfact; ULL get_prod(ULL x) { ULL prod = 1; while ( x > 0 ) { prod *= x % 10; x /= 10; } return prod; } int add_to_map(ULL num) { ULL prod = get_prod(num); auto it = M.find(prod); int r = ( it == M.end() ) ? add_to_map(prod) : it->Y; M[num] = r; return r; } // void digits(int v, int k2, int k3, int k4, int k5, int k6, int k7, int k8, int k9) // { // int k = k2 + k3 + k4 + k5 + k6 + k7 + k8 + k9; // if ( k <= 18 ) // { // ++ cfact; // DEBUG( "-- ", NAM(k2), NAM(k3), NAM(k4), NAM(k5), NAM(k6), NAM(k7), NAM(k8), NAM(k9), endl ); // } // if ( v <= 4 and k2 >= 2 ) // digits( 4, k2 - 2, k3, k4 + 1, k5, k6, k7, k8, k9 ); // if ( v <= 6 and k2 >= 1 and k3 >= 1 ) // digits( 6, k2 - 1, k3 - 1, k4, k5, k6 + 1, k7, k8, k9 ); // if ( v <= 8 and k2 >= 3 ) // digits( 8, k2 - 3, k3, k4, k5, k6, k7, k8 + 1, k9 ); // if ( v <= 9 and k3 >= 2 ) // digits( 9, k2, k3 - 2, k4, k5, k6, k7, k8, k9 + 1 ); // } void preproc() { ULL p2 = 1, p3 = 1, p5 = 1, p7 = 1; FOR(i,0,10) M[i] = i; FOR(i2,0,N2) { p3 = 1; if ( p2 > NX ) break; FOR(i3,0,N3) { p5 = 1; if ( p2 * p3 > NX ) break; FOR(i5,0,N5) { p7 = 1; if ( p2 * p3 * p5 > NX ) break; FOR(i7,0,N7) { ULL num = p2 * p3 * p5 * p7; if ( num > NX ) break; add_to_map( num ); int val = M[num]; if (val) { FM[num] = tuple( val, i2, i3, i5, i7 ); } p7 *= 7; } p5 *= 5; } p3 *= 3; } p2 *= 2; } auto it = M.begin(); while ( it != M.end() ) { if ( it->Y == 0 ) it = M.erase(it); else ++ it; } FOR(i,0,20) Newt[i][0] = 1; FOR(i,1,19) { FR_(j,1,i) { Newt[i][j] = Newt[i-1][j] + Newt[i-1][j-1]; DEBUG( Newt[i][j], " " ); } DEBUG( endl ); } DEBL; } // vector<ULL> non_zero[10]; // for ( const auto& elem : M ) // { // ++ MC[ elem.Y ]; // if ( elem.Y ) non_zero[ elem.Y ].push_back( elem.X ); // DEBUG( "ILO(", setw(18), elem.X , ") = ", elem.Y , endl ); // } // DEBL; // FOR(i,0,10) // { // DEBUG( "MC[", i ,"] = ", MC[i], endl ); // } // DEBL; // FOR(i,1,10) // { // DEBUG( "-------- ", NAM(i), endl ); // for ( auto k : non_zero[i] ) // { // DEBUG( "* ", k, endl ); // } // } // DEBL; // for ( const auto& elem : FM ) // { // auto [i2, i3, i5, i7] = elem.Y; // DEBUG( "FACT(", setw(18), elem.X , ") = { ", NAM(i2), NAM(i3), NAM(i5), NAM(i7), "}", endl ); // digits( 0, i2, i3, 0, i5, 0, i7, 0, 0 ); // } // DEBL; // DEBUG( "** ", NAM(cfact), endl ); ULL CR[DX]; int KK, cur_prod; int T[20]; int set_T(ULL x) { int k = 0; while ( x > 0 ) { T[ k ++ ] = int( x % 10 ); x /= 10; } return k; } ULL part_prod(int ALL, int* S) { ULL x = 1; FOR(j,2,DX) { x *= Newt[ALL][ S[j] ]; ALL -= S[j]; } return x; } void run(int ksum, int k2, int k3, int k4, int k5, int k6, int k7, int k8, int k9) { ULL res = 0; int ones = KK - ksum; int S[DX] = { 0, ones, k2, k3, k4, k5, k6, k7, k8, k9 }; FOR(i,0,ones) { res += part_prod( ksum + i, S ); } FOR(i,0,KK) { int dg = T[i]; FOR(j,1,dg) { if ( S[j] ) { -- S[j]; res += part_prod( KK - i - 1, S ); ++ S[j]; } } if ( S[dg] == 0 ) break; -- S[dg]; if ( i == KK - 1 ) res += 1; } DEBUG( "++ ", NAM(res), endl ); CR[cur_prod] += res; } void fact(int v, int k2, int k3, int k4, int k5, int k6, int k7, int k8, int k9) { int k = k2 + k3 + k4 + k5 + k6 + k7 + k8 + k9; if ( k <= KK ) { DEBUG( "-- ", NAM(k2), NAM(k3), NAM(k4), NAM(k5), NAM(k6), NAM(k7), NAM(k8), NAM(k9), endl ); run( k, k2, k3, k4, k5, k6, k7, k8, k9 ); } if ( v <= 4 and k2 >= 2 ) fact( 4, k2 - 2, k3, k4 + 1, k5, k6, k7, k8, k9 ); if ( v <= 6 and k2 >= 1 and k3 >= 1 ) fact( 6, k2 - 1, k3 - 1, k4, k5, k6 + 1, k7, k8, k9 ); if ( v <= 8 and k2 >= 3 ) fact( 8, k2 - 3, k3, k4, k5, k6, k7, k8 + 1, k9 ); if ( v <= 9 and k3 >= 2 ) fact( 9, k2, k3 - 2, k4, k5, k6, k7, k8, k9 + 1 ); } void solve() { ULL n; cin >> n; if ( n == NX ) { KK = 18; FOR(i,0,KK) T[i] = 9; } else { KK = set_T(n); reverse( T, T + KK ); } FOR(i,0,DX) CR[i] = 0; for ( const auto& elem : FM ) { auto [ val, i2, i3, i5, i7 ] = elem.Y; cur_prod = val; DEBUG( "FACT(", setw(18), elem.X , ") = { ", NAM(i2), NAM(i3), NAM(i5), NAM(i7), "}", endl ); fact( 0, i2, i3, 0, i5, 0, i7, 0, 0 ); } CR[1] -= 1; FOR(i,1,DX) CR[0] += CR[i]; CR[0] = n - CR[0]; FOR(i,0,DX) cout << CR[i] << ' '; cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); preproc(); int z; cin >> z; while ( z -- ) solve(); return 0; }
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | // Karol Kosinski 2025 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define X first #define Y second #define endl '\n' #define NAM(x) #x,'=',x," " using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; template<class...T> void _cout(T...a){(cout<<...<<a);} #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);} #endif #define DEBF(f,x...) DEB(1,1,f,x) #define DEBL DEBF(void,0) #define DEBC(p,x...) DEB(0,p,_cout,x) #define DEBUG(x...) DEBC(1,x) constexpr ULL NX = 1'000'000'000'000'000'000; // constexpr ULL NX = 100; constexpr int DX = 10; constexpr int N2 = 60; constexpr int N3 = 38; constexpr int N5 = 26; constexpr int N7 = 22; map<ULL, int> M; map<ULL, tuple<int, int, int, int, int>> FM; ULL Newt[21][21]; // int MC[10]; // int cfact; ULL get_prod(ULL x) { ULL prod = 1; while ( x > 0 ) { prod *= x % 10; x /= 10; } return prod; } int add_to_map(ULL num) { ULL prod = get_prod(num); auto it = M.find(prod); int r = ( it == M.end() ) ? add_to_map(prod) : it->Y; M[num] = r; return r; } // void digits(int v, int k2, int k3, int k4, int k5, int k6, int k7, int k8, int k9) // { // int k = k2 + k3 + k4 + k5 + k6 + k7 + k8 + k9; // if ( k <= 18 ) // { // ++ cfact; // DEBUG( "-- ", NAM(k2), NAM(k3), NAM(k4), NAM(k5), NAM(k6), NAM(k7), NAM(k8), NAM(k9), endl ); // } // if ( v <= 4 and k2 >= 2 ) // digits( 4, k2 - 2, k3, k4 + 1, k5, k6, k7, k8, k9 ); // if ( v <= 6 and k2 >= 1 and k3 >= 1 ) // digits( 6, k2 - 1, k3 - 1, k4, k5, k6 + 1, k7, k8, k9 ); // if ( v <= 8 and k2 >= 3 ) // digits( 8, k2 - 3, k3, k4, k5, k6, k7, k8 + 1, k9 ); // if ( v <= 9 and k3 >= 2 ) // digits( 9, k2, k3 - 2, k4, k5, k6, k7, k8, k9 + 1 ); // } void preproc() { ULL p2 = 1, p3 = 1, p5 = 1, p7 = 1; FOR(i,0,10) M[i] = i; FOR(i2,0,N2) { p3 = 1; if ( p2 > NX ) break; FOR(i3,0,N3) { p5 = 1; if ( p2 * p3 > NX ) break; FOR(i5,0,N5) { p7 = 1; if ( p2 * p3 * p5 > NX ) break; FOR(i7,0,N7) { ULL num = p2 * p3 * p5 * p7; if ( num > NX ) break; add_to_map( num ); int val = M[num]; if (val) { FM[num] = tuple( val, i2, i3, i5, i7 ); } p7 *= 7; } p5 *= 5; } p3 *= 3; } p2 *= 2; } auto it = M.begin(); while ( it != M.end() ) { if ( it->Y == 0 ) it = M.erase(it); else ++ it; } FOR(i,0,20) Newt[i][0] = 1; FOR(i,1,19) { FR_(j,1,i) { Newt[i][j] = Newt[i-1][j] + Newt[i-1][j-1]; DEBUG( Newt[i][j], " " ); } DEBUG( endl ); } DEBL; } // vector<ULL> non_zero[10]; // for ( const auto& elem : M ) // { // ++ MC[ elem.Y ]; // if ( elem.Y ) non_zero[ elem.Y ].push_back( elem.X ); // DEBUG( "ILO(", setw(18), elem.X , ") = ", elem.Y , endl ); // } // DEBL; // FOR(i,0,10) // { // DEBUG( "MC[", i ,"] = ", MC[i], endl ); // } // DEBL; // FOR(i,1,10) // { // DEBUG( "-------- ", NAM(i), endl ); // for ( auto k : non_zero[i] ) // { // DEBUG( "* ", k, endl ); // } // } // DEBL; // for ( const auto& elem : FM ) // { // auto [i2, i3, i5, i7] = elem.Y; // DEBUG( "FACT(", setw(18), elem.X , ") = { ", NAM(i2), NAM(i3), NAM(i5), NAM(i7), "}", endl ); // digits( 0, i2, i3, 0, i5, 0, i7, 0, 0 ); // } // DEBL; // DEBUG( "** ", NAM(cfact), endl ); ULL CR[DX]; int KK, cur_prod; int T[20]; int set_T(ULL x) { int k = 0; while ( x > 0 ) { T[ k ++ ] = int( x % 10 ); x /= 10; } return k; } ULL part_prod(int ALL, int* S) { ULL x = 1; FOR(j,2,DX) { x *= Newt[ALL][ S[j] ]; ALL -= S[j]; } return x; } void run(int ksum, int k2, int k3, int k4, int k5, int k6, int k7, int k8, int k9) { ULL res = 0; int ones = KK - ksum; int S[DX] = { 0, ones, k2, k3, k4, k5, k6, k7, k8, k9 }; FOR(i,0,ones) { res += part_prod( ksum + i, S ); } FOR(i,0,KK) { int dg = T[i]; FOR(j,1,dg) { if ( S[j] ) { -- S[j]; res += part_prod( KK - i - 1, S ); ++ S[j]; } } if ( S[dg] == 0 ) break; -- S[dg]; if ( i == KK - 1 ) res += 1; } DEBUG( "++ ", NAM(res), endl ); CR[cur_prod] += res; } void fact(int v, int k2, int k3, int k4, int k5, int k6, int k7, int k8, int k9) { int k = k2 + k3 + k4 + k5 + k6 + k7 + k8 + k9; if ( k <= KK ) { DEBUG( "-- ", NAM(k2), NAM(k3), NAM(k4), NAM(k5), NAM(k6), NAM(k7), NAM(k8), NAM(k9), endl ); run( k, k2, k3, k4, k5, k6, k7, k8, k9 ); } if ( v <= 4 and k2 >= 2 ) fact( 4, k2 - 2, k3, k4 + 1, k5, k6, k7, k8, k9 ); if ( v <= 6 and k2 >= 1 and k3 >= 1 ) fact( 6, k2 - 1, k3 - 1, k4, k5, k6 + 1, k7, k8, k9 ); if ( v <= 8 and k2 >= 3 ) fact( 8, k2 - 3, k3, k4, k5, k6, k7, k8 + 1, k9 ); if ( v <= 9 and k3 >= 2 ) fact( 9, k2, k3 - 2, k4, k5, k6, k7, k8, k9 + 1 ); } void solve() { ULL n; cin >> n; if ( n == NX ) { KK = 18; FOR(i,0,KK) T[i] = 9; } else { KK = set_T(n); reverse( T, T + KK ); } FOR(i,0,DX) CR[i] = 0; for ( const auto& elem : FM ) { auto [ val, i2, i3, i5, i7 ] = elem.Y; cur_prod = val; DEBUG( "FACT(", setw(18), elem.X , ") = { ", NAM(i2), NAM(i3), NAM(i5), NAM(i7), "}", endl ); fact( 0, i2, i3, 0, i5, 0, i7, 0, 0 ); } CR[1] -= 1; FOR(i,1,DX) CR[0] += CR[i]; CR[0] = n - CR[0]; FOR(i,0,DX) cout << CR[i] << ' '; cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); preproc(); int z; cin >> z; while ( z -- ) solve(); return 0; } |