Cod sursa(job #1774814)

Utilizator lflorin29Florin Laiu lflorin29 Data 9 octombrie 2016 14:46:14
Problema Cifre Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <bits/stdc++.h>

using namespace std;

int a, b, cif, k;

int memo[12][2][2][12];

int dp(int len, bool lo, bool nenul, int cnt, const string &s) {
	if(len == s.size()) {
		return (cnt >= k && nenul);
	}

	if(memo[len][lo][nenul][cnt] != -1) return memo[len][lo][nenul][cnt];

	int lim = !lo ? s[len] : 9;

	int ans = 0;
	for(int c = 0; c <= lim; ++c) {
		ans += dp(len + 1, lo || (c < s[len]), nenul || (c > 0), cnt + ((c == cif && c != 0) || (c == cif && c == 0 && nenul)), s);
	}
	return memo[len][lo][nenul][cnt] = ans;
}

string tonum(int x) {
	if(x == 0) return string(1, 0);
	string rez;
	while(x) {
		 rez += (x % 10);
		 x /= 10;
	}
	reverse(begin(rez), end(rez));
	return rez;
}

int cnt(int x) {
	memset(memo, -1, sizeof memo);
	string s = tonum(x);
	return dp(0, 0, 0, 0, s);
}

int main() {
	ifstream cin("cifre.in");
	ofstream cout("cifre.out");

	cin >> a >> b >> cif >> k;

	int tmp = cnt(b);
	if(a > 0) tmp -= cnt(a - 1);
	else tmp ++;
	double supra = b - a + 1;
	cout << fixed << setprecision(4) << (1.0 * tmp) / supra;
}