#include <iostream>
#include <algorithm>
//ZMIEŃ
const int MAX_LEN = 19; /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!ZMIEŃ
long long count[MAX_LEN * 3 + 3][MAX_LEN * 2 + 2][MAX_LEN + 1][MAX_LEN + 1][MAX_LEN][10]; // (already set) factors 2, factors 3, factors 5, factors 7, // number of unknown digits // final digit,
int dest[MAX_LEN * 3+3][MAX_LEN * 2+2][MAX_LEN+1][MAX_LEN+1];
long long POW_digits[11][3*MAX_LEN+1];
long long calc_res(long long number) {
int cur_digits[10] = {0,0,0,0,0 ,0,0,0,0,0}; // 2,3,5,7
bool is_zero = false;
long long k = number;
while (k > 0LL) {
int digit = k % 10LL;
k = k/10LL;
switch (digit) {
case 0:
is_zero = true;
break;
case 1:
break;
case 2:
cur_digits[2] += 1; break;
case 3:
cur_digits[3] += 1; break;
case 4:
cur_digits[2] += 2; break;
case 5:
cur_digits[5] += 1; break;
case 6:
cur_digits[2] += 1;
cur_digits[3] += 1; break;
case 7:
cur_digits[7] += 1; break;
case 8:
cur_digits[2] += 3; break;
case 9:
cur_digits[3] += 2; break;
}
}
if (is_zero) return 0;
long long result = dest[cur_digits[2]][cur_digits[3]][cur_digits[5]][cur_digits[7]];
if (result == -1LL) {
result = calc_res(POW_digits[2][cur_digits[2]] *
POW_digits[3][cur_digits[3]] *
POW_digits[5][cur_digits[5]] *
POW_digits[7][cur_digits[7]]);
dest[cur_digits[2]][cur_digits[3]][cur_digits[5]][cur_digits[7]] = result;
}
return result;
}
int main()
{
for (long long i = 0LL; i < MAX_LEN*3+1; i++) { //OBLICZAMY POTĘGI
long long prod = 1;
POW_digits[i][0] = prod;
for (int k = 1; k <= MAX_LEN; k++) {
prod *= i;
POW_digits[i][k] = prod;
}
}
for (int i2=0; i2<3*MAX_LEN;i2++) // INICJALIZUJE TABLICE
for (int i3=0; i3<2*MAX_LEN;i3++)
for (int i5 = 0; i5 < MAX_LEN; i5++)
for (int i7 = 0; i7 < MAX_LEN; i7++)
{ dest[i2][i3][i5][i7] = -1LL;}
dest[0][0][0][0] = 1LL;
dest[1][0][0][0] = 2LL;
dest[0][1][0][0] = 3LL;
dest[2][0][0][0] = 4LL;
dest[0][0][1][0] = 5LL;
dest[1][1][0][0] = 6LL;
dest[0][0][0][1] = 7LL;
dest[3][0][0][0] = 8LL;
dest[0][2][0][0] = 9LL;
for (int i2 = 0; i2 <= MAX_LEN*3; i2++)
for (int i3 = 0; i3 <= MAX_LEN*2; i3++)
for (int i5 = 0; i5 <= MAX_LEN; i5++)
for (int i7 = 0; i7 <= MAX_LEN; i7++) {
long long number = POW_digits[2][i2] * POW_digits[3][i3] * POW_digits[5][i5] * POW_digits[7][i7];
dest[i2][i3][i5][i7] = calc_res(number);
}
int count_2 = 0;
for (int i2 = 0; i2 <= MAX_LEN * 3; i2++)
for (int i3 = 0; i3 <= MAX_LEN * 2; i3++)
for (int i5 = 0; i5 <= MAX_LEN; i5++)
for (int i7 = 0; i7 <= MAX_LEN; i7++) {
if (dest[i2][i3][i5][i7] == 2 && i2 + i3 + i5 + i7 < MAX_LEN*3) {
/*long long number = POW_digits[2][i2] * POW_digits[3][i3] * POW_digits[5][i5] * POW_digits[7][i7];
if (number <= 123456789LL) {
count_2++;
std::cout << "[" << number << " <- " << i2 << " " << i3 << " " << i5 << " " << i7 << "]\n";
}*/
}
for (int d = 0; d < 10; d++)
count[i2][i3][i5][i7][0][d] = 0LL;
count[i2][i3][i5][i7][0][dest[i2][i3][i5][i7]] = 1LL;
}
//std::cout << " count of 2 " << count_2 << "\n";
for (int k = 1; k < MAX_LEN; k++) {
for (int i2 = 0; i2 <= MAX_LEN * 3; i2++) {
for (int i3 = 0; i3 <= MAX_LEN * 2; i3++) {
if (i2 + i3 > MAX_LEN * 3) continue;
for (int i5 = 0; i5 <= MAX_LEN; i5++) {
if (i2 + i3 + i5 > MAX_LEN * 3) continue;
for (int i7 = 0; i7 <= MAX_LEN; i7++) {
if (i2 + i3 + i5 + i7 > MAX_LEN * 3) continue;
/*if (i2 > 0 && i5 > 0) {
count[i2][i3][i5][i7][k][0] += POW_digits[10][k - 1];
continue;
}*/
for (int d = 0; d < 10; d++) {
count[i2][i3][i5][i7][k][d] = count[i2][i3][i5][i7][k - 1][d]; // CYFRA 1
if (i2 + 1 < MAX_LEN*3) { count[i2][i3][i5][i7][k][d] += count[i2 + 1][i3][i5][i7][k - 1][d]; }// CYFRA 2
if (i3 + 1 < MAX_LEN*2) { count[i2][i3][i5][i7][k][d] += count[i2][i3 + 1][i5][i7][k - 1][d]; }// CYFRA 3
if (i2 + 2 < MAX_LEN*3) { count[i2][i3][i5][i7][k][d] += count[i2 + 2][i3][i5][i7][k - 1][d]; }// CYFRA 4
if (i5 + 1 < MAX_LEN) { count[i2][i3][i5][i7][k][d] += count[i2][i3][i5 + 1][i7][k - 1][d]; }// CYFRA 5
if (i2 + 1 < MAX_LEN*3 && i3 + 1 < MAX_LEN*2) { count[i2][i3][i5][i7][k][d] += count[i2 + 1][i3 + 1][i5][i7][k - 1][d]; }// CYFRA 6
if (i7 + 1 < MAX_LEN) { count[i2][i3][i5][i7][k][d] += count[i2][i3][i5][i7 + 1][k - 1][d]; }// CYFRA 7
if (i2 + 3 < MAX_LEN*3) { count[i2][i3][i5][i7][k][d] += count[i2 + 3][i3][i5][i7][k - 1][d]; }// CYFRA 8
if (i3 + 2 < MAX_LEN*2) { count[i2][i3][i5][i7][k][d] += count[i2][i3 + 2][i5][i7][k - 1][d]; }// CYFRA 9
}
count[i2][i3][i5][i7][k][0] += POW_digits[10][k-1]; // CYFRA 0
}
}
}
}
}
long long cur_count[10];
int digits[MAX_LEN+1];
long long count_no_leading_zeros[MAX_LEN+2][10];
for (int d = 0; d < 10; d++) count_no_leading_zeros[0][d] = 0;
for (int i = 1; i < MAX_LEN+2; i++) {
for (int d = 0; d < 10; d++) {
count_no_leading_zeros[i][d] = 0;
for (int pos = 1; pos <= i; pos++)
count_no_leading_zeros[i][d] += count[0][0][0][0][pos][d];
}
for (int pos = 0; pos < i; pos++)
count_no_leading_zeros[i][0] -= POW_digits[10][pos];
}
int k;
std::cin >> k;
for (int testy = 0; testy < k; testy++) {
long long n;
std::cin >> n;
bool has_zero = false;
for (int d = 0; d < 10; d++) cur_count[d] = 0;
for (int i = 0; i < MAX_LEN+1; i++) digits[i] = 0;
int cur_digits[10] = { 0,0,0,0,0,0,0,0,0,0 };
int dlen = 0;
while (n > 0LL) {
digits[dlen] = n % 10LL;
dlen++;
n /= 10LL;
}
if (dlen != 1) {
for (int k = dlen - 1; k >= 0; k--) {
int a = digits[k];
if (a == 0) { //WSZYSTKIE LICZBY Z TYM POCZĄTKIEM STAJĄ SIĘ ZEREM
has_zero = true;
int rest = 0;
for (int i = (k - 1); i >= 0; i--) { rest = 10 * rest + digits[i]; }
cur_count[0] += rest + (k != 0) - (rest != 0); //+1 BO DOLICZAM WARIANT SAMYCH ZER
break;
}
for (int b = 1; b < a; b++) {
int fact2 = cur_digits[2] + (b % 2 == 0) + (b % 4 == 0) + (b == 8);
int fact3 = cur_digits[3] + (b % 3 == 0) + (b == 9);
int fact5 = cur_digits[5] + (b == 5);
int fact7 = cur_digits[7] + (b == 7);
for (int d = 0; d < 10; d++) {
cur_count[d] += count[fact2][fact3][fact5][fact7][k][d];
}
std::cout << "";
}
//PRZYPADEK b=0 // ZAWSZE ZACHODZI BO JUŻ ODRZUCIŁEM a=0
if (k != dlen - 1) {
cur_count[0] += POW_digits[10][k]; //ZERO JEST W ŚRODKU LICZBY
}
else {
for (int d = 0; d < 10; d++) cur_count[d] += count_no_leading_zeros[k][d];
}
cur_digits[2] += (a % 2 == 0) + (a % 4 == 0) + (a == 8);
cur_digits[3] += (a % 3 == 0) + (a == 9);
cur_digits[5] += (a == 5);
cur_digits[7] += (a == 7);
}
if (has_zero) cur_count[0] += 1;
else cur_count[dest[cur_digits[2]][cur_digits[3]][cur_digits[5]][cur_digits[7]]] += 1;
}
else {
for (int d = 1; d <= digits[0]; d++) {
cur_count[d] = 1;
}
}
for (int d = 0; d < 10; d++) { std::cout << cur_count[d] << " "; }
std::cout << "\n";
}
//std::cout << "DONE " << dest[6][5][0][8];//calc_res(POW_digits[2][6] * POW_digits[3][3] * POW_digits[5][0] * POW_digits[7][10]);//dest[6][3][0][10];
}
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 | #include <iostream> #include <algorithm> //ZMIEŃ const int MAX_LEN = 19; /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!ZMIEŃ long long count[MAX_LEN * 3 + 3][MAX_LEN * 2 + 2][MAX_LEN + 1][MAX_LEN + 1][MAX_LEN][10]; // (already set) factors 2, factors 3, factors 5, factors 7, // number of unknown digits // final digit, int dest[MAX_LEN * 3+3][MAX_LEN * 2+2][MAX_LEN+1][MAX_LEN+1]; long long POW_digits[11][3*MAX_LEN+1]; long long calc_res(long long number) { int cur_digits[10] = {0,0,0,0,0 ,0,0,0,0,0}; // 2,3,5,7 bool is_zero = false; long long k = number; while (k > 0LL) { int digit = k % 10LL; k = k/10LL; switch (digit) { case 0: is_zero = true; break; case 1: break; case 2: cur_digits[2] += 1; break; case 3: cur_digits[3] += 1; break; case 4: cur_digits[2] += 2; break; case 5: cur_digits[5] += 1; break; case 6: cur_digits[2] += 1; cur_digits[3] += 1; break; case 7: cur_digits[7] += 1; break; case 8: cur_digits[2] += 3; break; case 9: cur_digits[3] += 2; break; } } if (is_zero) return 0; long long result = dest[cur_digits[2]][cur_digits[3]][cur_digits[5]][cur_digits[7]]; if (result == -1LL) { result = calc_res(POW_digits[2][cur_digits[2]] * POW_digits[3][cur_digits[3]] * POW_digits[5][cur_digits[5]] * POW_digits[7][cur_digits[7]]); dest[cur_digits[2]][cur_digits[3]][cur_digits[5]][cur_digits[7]] = result; } return result; } int main() { for (long long i = 0LL; i < MAX_LEN*3+1; i++) { //OBLICZAMY POTĘGI long long prod = 1; POW_digits[i][0] = prod; for (int k = 1; k <= MAX_LEN; k++) { prod *= i; POW_digits[i][k] = prod; } } for (int i2=0; i2<3*MAX_LEN;i2++) // INICJALIZUJE TABLICE for (int i3=0; i3<2*MAX_LEN;i3++) for (int i5 = 0; i5 < MAX_LEN; i5++) for (int i7 = 0; i7 < MAX_LEN; i7++) { dest[i2][i3][i5][i7] = -1LL;} dest[0][0][0][0] = 1LL; dest[1][0][0][0] = 2LL; dest[0][1][0][0] = 3LL; dest[2][0][0][0] = 4LL; dest[0][0][1][0] = 5LL; dest[1][1][0][0] = 6LL; dest[0][0][0][1] = 7LL; dest[3][0][0][0] = 8LL; dest[0][2][0][0] = 9LL; for (int i2 = 0; i2 <= MAX_LEN*3; i2++) for (int i3 = 0; i3 <= MAX_LEN*2; i3++) for (int i5 = 0; i5 <= MAX_LEN; i5++) for (int i7 = 0; i7 <= MAX_LEN; i7++) { long long number = POW_digits[2][i2] * POW_digits[3][i3] * POW_digits[5][i5] * POW_digits[7][i7]; dest[i2][i3][i5][i7] = calc_res(number); } int count_2 = 0; for (int i2 = 0; i2 <= MAX_LEN * 3; i2++) for (int i3 = 0; i3 <= MAX_LEN * 2; i3++) for (int i5 = 0; i5 <= MAX_LEN; i5++) for (int i7 = 0; i7 <= MAX_LEN; i7++) { if (dest[i2][i3][i5][i7] == 2 && i2 + i3 + i5 + i7 < MAX_LEN*3) { /*long long number = POW_digits[2][i2] * POW_digits[3][i3] * POW_digits[5][i5] * POW_digits[7][i7]; if (number <= 123456789LL) { count_2++; std::cout << "[" << number << " <- " << i2 << " " << i3 << " " << i5 << " " << i7 << "]\n"; }*/ } for (int d = 0; d < 10; d++) count[i2][i3][i5][i7][0][d] = 0LL; count[i2][i3][i5][i7][0][dest[i2][i3][i5][i7]] = 1LL; } //std::cout << " count of 2 " << count_2 << "\n"; for (int k = 1; k < MAX_LEN; k++) { for (int i2 = 0; i2 <= MAX_LEN * 3; i2++) { for (int i3 = 0; i3 <= MAX_LEN * 2; i3++) { if (i2 + i3 > MAX_LEN * 3) continue; for (int i5 = 0; i5 <= MAX_LEN; i5++) { if (i2 + i3 + i5 > MAX_LEN * 3) continue; for (int i7 = 0; i7 <= MAX_LEN; i7++) { if (i2 + i3 + i5 + i7 > MAX_LEN * 3) continue; /*if (i2 > 0 && i5 > 0) { count[i2][i3][i5][i7][k][0] += POW_digits[10][k - 1]; continue; }*/ for (int d = 0; d < 10; d++) { count[i2][i3][i5][i7][k][d] = count[i2][i3][i5][i7][k - 1][d]; // CYFRA 1 if (i2 + 1 < MAX_LEN*3) { count[i2][i3][i5][i7][k][d] += count[i2 + 1][i3][i5][i7][k - 1][d]; }// CYFRA 2 if (i3 + 1 < MAX_LEN*2) { count[i2][i3][i5][i7][k][d] += count[i2][i3 + 1][i5][i7][k - 1][d]; }// CYFRA 3 if (i2 + 2 < MAX_LEN*3) { count[i2][i3][i5][i7][k][d] += count[i2 + 2][i3][i5][i7][k - 1][d]; }// CYFRA 4 if (i5 + 1 < MAX_LEN) { count[i2][i3][i5][i7][k][d] += count[i2][i3][i5 + 1][i7][k - 1][d]; }// CYFRA 5 if (i2 + 1 < MAX_LEN*3 && i3 + 1 < MAX_LEN*2) { count[i2][i3][i5][i7][k][d] += count[i2 + 1][i3 + 1][i5][i7][k - 1][d]; }// CYFRA 6 if (i7 + 1 < MAX_LEN) { count[i2][i3][i5][i7][k][d] += count[i2][i3][i5][i7 + 1][k - 1][d]; }// CYFRA 7 if (i2 + 3 < MAX_LEN*3) { count[i2][i3][i5][i7][k][d] += count[i2 + 3][i3][i5][i7][k - 1][d]; }// CYFRA 8 if (i3 + 2 < MAX_LEN*2) { count[i2][i3][i5][i7][k][d] += count[i2][i3 + 2][i5][i7][k - 1][d]; }// CYFRA 9 } count[i2][i3][i5][i7][k][0] += POW_digits[10][k-1]; // CYFRA 0 } } } } } long long cur_count[10]; int digits[MAX_LEN+1]; long long count_no_leading_zeros[MAX_LEN+2][10]; for (int d = 0; d < 10; d++) count_no_leading_zeros[0][d] = 0; for (int i = 1; i < MAX_LEN+2; i++) { for (int d = 0; d < 10; d++) { count_no_leading_zeros[i][d] = 0; for (int pos = 1; pos <= i; pos++) count_no_leading_zeros[i][d] += count[0][0][0][0][pos][d]; } for (int pos = 0; pos < i; pos++) count_no_leading_zeros[i][0] -= POW_digits[10][pos]; } int k; std::cin >> k; for (int testy = 0; testy < k; testy++) { long long n; std::cin >> n; bool has_zero = false; for (int d = 0; d < 10; d++) cur_count[d] = 0; for (int i = 0; i < MAX_LEN+1; i++) digits[i] = 0; int cur_digits[10] = { 0,0,0,0,0,0,0,0,0,0 }; int dlen = 0; while (n > 0LL) { digits[dlen] = n % 10LL; dlen++; n /= 10LL; } if (dlen != 1) { for (int k = dlen - 1; k >= 0; k--) { int a = digits[k]; if (a == 0) { //WSZYSTKIE LICZBY Z TYM POCZĄTKIEM STAJĄ SIĘ ZEREM has_zero = true; int rest = 0; for (int i = (k - 1); i >= 0; i--) { rest = 10 * rest + digits[i]; } cur_count[0] += rest + (k != 0) - (rest != 0); //+1 BO DOLICZAM WARIANT SAMYCH ZER break; } for (int b = 1; b < a; b++) { int fact2 = cur_digits[2] + (b % 2 == 0) + (b % 4 == 0) + (b == 8); int fact3 = cur_digits[3] + (b % 3 == 0) + (b == 9); int fact5 = cur_digits[5] + (b == 5); int fact7 = cur_digits[7] + (b == 7); for (int d = 0; d < 10; d++) { cur_count[d] += count[fact2][fact3][fact5][fact7][k][d]; } std::cout << ""; } //PRZYPADEK b=0 // ZAWSZE ZACHODZI BO JUŻ ODRZUCIŁEM a=0 if (k != dlen - 1) { cur_count[0] += POW_digits[10][k]; //ZERO JEST W ŚRODKU LICZBY } else { for (int d = 0; d < 10; d++) cur_count[d] += count_no_leading_zeros[k][d]; } cur_digits[2] += (a % 2 == 0) + (a % 4 == 0) + (a == 8); cur_digits[3] += (a % 3 == 0) + (a == 9); cur_digits[5] += (a == 5); cur_digits[7] += (a == 7); } if (has_zero) cur_count[0] += 1; else cur_count[dest[cur_digits[2]][cur_digits[3]][cur_digits[5]][cur_digits[7]]] += 1; } else { for (int d = 1; d <= digits[0]; d++) { cur_count[d] = 1; } } for (int d = 0; d < 10; d++) { std::cout << cur_count[d] << " "; } std::cout << "\n"; } //std::cout << "DONE " << dest[6][5][0][8];//calc_res(POW_digits[2][6] * POW_digits[3][3] * POW_digits[5][0] * POW_digits[7][10]);//dest[6][3][0][10]; } |
English