Cod sursa(job #2648602)

Utilizator OvidRata Ovidiu Ovid Data 11 septembrie 2020 17:54:04
Problema Cifre Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.29 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll


ifstream fin("cifre.in"); ofstream fout("cifre.out");
#define cin fin
#define cout fout


int t, n, m, a, b, k, c;
int dp[11][11];




int ln(int x){
int y=x; int res=0;
while(y>0){
    y/=10; res++;
}
return res;
}

string to_str(int a){
string res; int x=a;
while(x>0){
    res="0"+res;
    res[0]+=(x%10); x/=10;
}
if(a==0){res="0";}
return res;
}

int less_than(int x){
string s=to_str(x); int l=s.length();
int res=0;
int cnt=0;
if(c==0){if(l>=2){res+=dp[l-2][k]*9;}else{if(k==1){res=1;} } }
else{res+=dp[l-1][k]; if(l>=2){res-=dp[l-2][k];} }

for(int cf=1; cf<(s[0]-'0'); cf++ ){
    if(cf==c){
        res+=dp[l-1][k-1];
    }
    else{
        res+=dp[l-1][k];
    }
}
if(s.length()==1){return res;}

//if( (c<(s[0]-'0')) && (c>0) ){res+=dp[l-1][k-1]-dp[l-1][k];}
if(c==(s[0]-'0')){cnt++;}
//cout<<res<<"\n";
for(int i=1; i<(l-1); i++){

for(int cf=0; cf<(s[i]-'0'); cf++ ){
    if(cf==c){
        res+=dp[l-i-1][max(k-1-cnt, 0ll)];
    }
    else{
        res+=dp[l-i-1][max(k-cnt, 0ll)];
    }
}
   // if( (c<(s[i]-'0'))&& (c>0) ){res+=dp[l-i-1][max(k-cnt-1, 0ll)]-dp[l-i-1][max(k-cnt, 0ll)];}
    if(c==(s[i]-'0')){cnt++;}
}

for(int cf=0; cf<=(s[s.length()-1]-'0');cf++ ){
    if(cf==c){
        if( (cnt+1)>=k ){res++;}
    }
    else{
        if(cnt>=k){res++;}
    }
}

return res;
}






int32_t main(){
INIT
cin>>a>>b>>c>>k;
dp[0][0]=1;
for(int l=1; l<=10; l++){
    for(int i=0; i<=10; i++){
        dp[l][i]+=dp[l-1][i]*(9);
        if(i!=0){
            dp[l][i]+=dp[l-1][i-1];
        }
        //cout<<dp[l][i]<<" ";
    }
    //cout<<"\n";
}
for(int l=1; l<=10; l++){
for(int i=9; i>=0; i--){
    dp[l][i]+=dp[l][i+1];
}
}
int ca=less_than(a-1), cb=less_than(b);
int cnt=cb-ca;
//cout<<ca<<" "<<cb<<"\n";
long double res=((long double)(cnt)/((long double) (b-a+1) ));
cout<<fixed<<setprecision(5)<<res;


return 0;
}