Pagini recente » Cod sursa (job #787752) | Cod sursa (job #431219)
Cod sursa(job #431219)
#include <stdio.h>
int a, b, c, k;
int d[15][15][15];
void calc_din() {
if (c) {
for (int i = 1; i <= 10; i++)
if (i - 1 != c) d[i][0][0] = d[i - 1][0][0] + 1;
else d[i][0][0] = d[i - 1][0][0];
for (int i = c + 1; i <= 10; i++)
d[i][0][1] = 1;
}
else {
for (int i = 1; i <= 9; i++)
d[i][0][0] = 1;
d[0][0][1] = 1;
}
for (int i = 1; i < 9; i++)
for (int j = 0 + (c != 0); j <= 9; j++)
for (int t = 0; t <= 9; t++)
if (c)
if (j == 1) {
int x = 0;
for (int L = i - 1; L >= 0; L--) {
d[j][i][t] += d[9][L][t - x];
if (c == 9) x++;
if (x > t) break;
}
if (c != 9 && t == 0) d[j][i][t]++;
if (c == 9 && t == i) d[j][i][t]++;
}
else {
if (j == c + 1 && t == i + 1) d[j][i][t]++; //adaug numarul "ccccc" de i + 1 ori
d[j][i][t] = d[j - 1][i][t];
if (t - (j - 1 == c) >= 0) d[j][i][t] += d[1][i][t - (j - 1 == c)];
}
else
if (t - (j == 0) >= 0)
for (int cif = 0; cif < 10; cif++)
d[j][i][t] += d[cif][i - 1][t - (j == 0)];
}
int solve(int x) {
if (x < 0) return 0;
if (x == 0) return d[1][0][k];
int cif[12] = {0,0,0,0,0,0,0,0,0,0,0}, sol = 0, nr = k;
while (x) {
cif[++cif[0]] = x % 10;
x /= 10;
}
if (c)
for (int i = cif[0]; i > 0; i--)
if (i == 1) {
if (cif[i] > 0)
for (int j = nr; j <= 9; j++)
sol = sol + d[cif[i] + 1][0][j];
}
else {
for (int j = nr; j <= 9; j++)
sol = sol + d[cif[i]][i - 1][j];
if (cif[i] == c) nr--;
}
else
for (int i = cif[0]; i > 0; i--) {
for (int j = 0 + (i == cif[0]); j < cif[i]; j++)
for (int t = nr; t <= 9; t++)
sol = sol + d[j][i - 1][t];
if (cif[i] == 0) nr--;
if (i < cif[0])
for (int j = 1; j < 10; j++)
for (int t = k; t <= 9; t++)
sol = sol + d[j][i - 1][t];
}
if (!c && k <= 1) sol++;
return sol;
}
int main() {
freopen("cifre.in", "r", stdin);
freopen("cifre.out", "w", stdout);
scanf("%d %d %d %d", &a, &b, &c, &k);
calc_din();
printf("%.4lf\n", 1.0 * (solve(b) - solve(a - 1)) / (b - a + 1));
return 0;
}