Pagini recente » Cod sursa (job #3275324) | Cod sursa (job #2779892) | Cod sursa (job #176104) | Cod sursa (job #1633295) | Cod sursa (job #2914843)
/// Preset de infoarena
#include <fstream>
#include <iomanip>
using namespace std;
ifstream fin("cifre.in");
ofstream fout("cifre.out");
int cif, frecv, v[12], dp[15][15];
int calc(int nr)
{
int aux = nr, nrc = 0, ans = 0, ramas = frecv;
bool flag = 0;
while(aux)
{
v[++nrc] = aux % 10;
aux /= 10;
}
if(cif == 0)
{
for(int i = nrc - 1; i >= 1; i--)
ans += 9 * dp[i - 1][frecv];
flag = 1;
}
for(int i = nrc; i >= 1; i--)
{
if(v[i] > cif && ramas && !flag)
{
ans += dp[i - 1][ramas - 1];
v[i]--;
}
ans += (v[i] - flag) * dp[i - 1][ramas];
if(v[i] == cif)
ramas = max(ramas - 1, 0);
flag = 0;
}
if(ramas == 0)
ans++;
return ans;
}
int main()
{
int x, y;
double ans;
fin >> x >> y >> cif >> frecv;
dp[0][0] = 1;
for(int i = 1; i <= 9; i++)
{
dp[i][0] = 9 * dp[i - 1][0];
for(int j = 1; j <= i; j++)
dp[i][j] = 9 * dp[i - 1][j] + dp[i - 1][j - 1];
}
for(int i = 1; i <= 9; i++)
for(int j = 8; j >= 0; j--)
dp[i][j] += dp[i][j + 1];
ans = 1.0 * (calc(y) - calc(x - 1)) / (y - x + 1);
fout << fixed << setprecision(4) << ans;
return 0;
}