Cod sursa(job #1468208)

Utilizator killer301Ioan Andrei Nicolae killer301 Data 5 august 2015 14:20:34
Problema Cifre Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int cifmax = 9;

int c, k;
int dp[cifmax+5][cifmax+5][2][2];
int a[cifmax+5];

int ans(int x)
{
	memset(dp, 0, sizeof(dp));
	int n = 0;
	do
	{
		a[n++] = x%10;
		x = x/10;
	}
	while(x);
	reverse(a, a+n);
	dp[0][0][0][0] = 1;
	for(int i=0; i<n; i++)
		for(int j=0; j<=k; j++)
			for(int zero=0; zero<=1; zero++)
				for(int prefix=0; prefix<=1; prefix++)
					for(int digit=0; digit<=9; digit++)
					{
						int nrcif = j;
						if(digit==c)
						{
							nrcif++;
							if (digit == 0 && zero == 0)
								nrcif--;
						}
						if(nrcif>k)nrcif=k;
						if(prefix == 0 && digit > a[i])
							continue;
						int _prefix = prefix;
						if(digit<a[i])_prefix = 1;
						int _zero=zero;
						if(digit)_zero=1;
						dp[i+1][nrcif][_zero][_prefix] += dp[i][j][zero][prefix];
					}
	return dp[n][k][1][0] + dp[n][k][1][1];
}

int main()
{
    freopen("cifre.in", "r", stdin);
    freopen("cifre.out", "w", stdout);
    int a, b;
    scanf("%d%d%d%d", &a, &b, &c, &k);
    printf("%lf\n", 1.0*(ans(b) - ans(a-1))/(b-a+1));
    return 0;
}