Cod sursa(job #2091275)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 19 decembrie 2017 14:29:20
Problema Cifre Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.26 kb
#include <bits/stdc++.h>

inline int cf(int a){
    int ncif = 0;
    while(a > 0){
        ncif++;
        a /= 10;
    }
    return ncif;
}

int D2(int n, int ncif, int c, int k){
    if(n < 10){
        if(k > ncif) return 0;
        else if(k == ncif)
            return 1;
        else return n + 1;
    }
    int cn = n, p10 = 1;
    while(cn > 0){
        p10 *= 10;
        cn /= 10;
    }
    p10 /= 10;
    int lastcif = n / p10;
    int ans = 0;
    cn = n;
    int ncif1 = 0;
    while(cn > 0){
        ncif1++;
        cn /= 10;
    }
    cn = n % p10;
    int ncif2 = 0;
    while(cn > 0){
        ncif2++;
        cn /= 10;
    }

    ans += D2(n % p10, cf(n % p10), c, k - (ncif1 - ncif2 - 1));
    ans += (lastcif - 1) * D2(p10 - 1, ncif - 1, c, k) + D2(p10 - 1, ncif - 1, c, k - 1);
    return ans;
}

int D(int n, int c, int k){
    if(n < 10){
        if(k > 1) return 0;
        else if(k == 1){
            if(n >= c) return 1;
            return 0;
        }
        else return n + 1;
    }
    int cn = n, p10 = 1;
    while(cn > 0){
        p10 *= 10;
        cn /= 10;
    }
    p10 /= 10;
    int lastcif = n / p10;
    int ans = 0;
    if(c != 0){
        if(lastcif == c)
            ans += D(n % p10, c, k - 1);
        else
            ans += D(n % p10, c, k);
        lastcif--;
        if(lastcif < c)
            ans += (lastcif + 1) * D(p10 - 1, c, k);
        else
            ans += lastcif * D(p10 - 1, c, k) + D(p10 - 1, c, k - 1);
        return ans;
    }
    else{
        cn = n;
        int ncif1 = 0;
        while(cn > 0){
            ncif1++;
            cn /= 10;
        }
        cn = n % p10;
        int ncif2 = 0;
        while(cn > 0){
            ncif2++;
            cn /= 10;
        }
        ans += D2(n % p10, cf(n % p10), c, k - (ncif1 - ncif2 - 1));
        ans += lastcif * D(p10 - 1, c, k);
        return ans;
    }
}
int main(){
    FILE*fi,*fo;
    fi=fopen("cifre.in","r");
    fo=fopen("cifre.out","w");

    int a, b, c, k;
    fscanf(fi,"%d%d%d%d", &a, &b, &c, &k);
    //printf("%d", D(b, c, k));
    fprintf(fo,"%lf", (1.0 * D(b, c, k) - 1.0 * D(a - 1, c, k)) / (1.0 * b - 1.0 * a + 1));
    fclose(fi);
    fclose(fo);
    return 0;
}