Cod sursa(job #2194204)

Utilizator bogdan10bosBogdan Sitaru bogdan10bos Data 12 aprilie 2018 16:39:14
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.43 kb
#include <bits/stdc++.h>

using namespace std;

/*double ans[15][15][2];

int N, B;

int nxt[1000005][10];
int pw[2][15];
double e[1000005], prb[1000005];
double prbb[1000005];
vector<int> nmr[15];
map<int, int> mp;

void bck(int pas, int nr, int id, int cnt)
{
    if(pas >= N)
    {
        prb[id] = e[id] = 0.0;
        nmr[cnt].push_back(id);
        mp[id] = nr;
        return;
    }
    bck(pas + 1, nr, id, cnt);
    for(int i = 1; i <= B; i++)
        bck(pas + 1, nr + pw[0][pas] * (i - 1), id + pw[1][pas] * i, cnt + 1);
}

double solveprob(int oth)
{
    for(auto x: nmr[N])
    {
        if(x >= oth)    prbb[x] = 1.0;
        else    prbb[x] = 0.0;
    }
    for(int i = N - 1; i >= 0; i--)
        for(auto x: nmr[i])
        {
            prbb[x] = 0.0;
            for(int c = 0; c < B; c++)
            {
                double bst = -1.0;
                for(int i = 0; i < N; i++)
                    if((x / pw[1][i]) % (B + 1) == 0)
                    {
                        int nx = x + (c + 1) * pw[1][i];
                        if(bst < prbb[nx])
                            bst = prbb[nx];
                    }
                prbb[x] += bst;
            }
            prbb[x] /= (double)B;
        }
    return prbb[0];
}

void solve(int n, int b)
{
    N = n, B = b;
    for(int i = 0; i <= N; i++) nmr[i].clear();

    pw[0][0] = 1;
    pw[1][0] = 1;
    for(int i = 1; i <= N; i++)
    {
        pw[0][i] = pw[0][i - 1] * B;
        pw[1][i] = pw[1][i - 1] * (B + 1);
    }

    mp.clear();
    bck(0, 0, 0, 0);

    for(auto x: nmr[N])
        e[x] = mp[x];
    for(int i = N - 1; i >= 0; i--)
        for(auto x: nmr[i])
        {
            e[x] = 0.0;
            for(int c = 0; c < B; c++)
            {
                double bst = -1.0;
                for(int i = 0; i < N; i++)
                    if((x / pw[1][i]) % (B + 1) == 0)
                    {
                        int nx = x + (c + 1) * pw[1][i];
                        if(bst < e[nx])
                        {
                            bst = e[nx];
                            nxt[x][c] = nx;
                        }
                    }
                e[x] += bst;
            }
            e[x] /= (double)(B);
        }

    prb[0] = 1.0;
    for(int i = 0; i < N; i++)
        for(auto x: nmr[i])
        {
            double coef = 1.0 / (double)B;
            for(int c = 0; c < B; c++)
                prb[ nxt[x][c] ] += coef * prb[x];
        }

    double anss = 0.0;
    double sum = 0.0;
    sort(nmr[N].begin(), nmr[N].end());
    for(auto x: nmr[N])
    {
        anss += prb[x] * sum;
        sum += prb[x];
    }
    ans[N][B][0] = anss;

    anss = 0.0;
    for(auto x: nmr[N])
    {
        double val = solveprob(x);
        anss += prb[x] * (1.0 - val);
    }
    ans[N][B][1] = anss;
}*/

typedef pair<int, int> pii;
typedef pair<pii, pii> p4i;
map<pii, int> dp;

int N, B;
int pw[2][15];

void pre()
{
    pw[0][0] = 1;
    pw[1][0] = 1;
    for(int i = 1; i <= N; i++)
    {
        pw[0][i] = pw[0][i - 1] * B;
        pw[1][i] = pw[1][i - 1] * (B + 1);
    }
}

double solve(int nr1, int nr2, int rem1, int rem2)
{
    if(rem1 == 0 && rem2 == 0)
    {
        if(nr1 > nr2)   return 1.0;
        return 0.0;
    }

    pii state = {nr1, nr2};
    if(dp.count(state)) return dp[state];

    if(rem1 >= rem2)
    {
        double val = 0.0;
        for(int c = 0; c < B; c++)
        {
            double bst = 0.0;
            for(int i = 0; i < N; i++)
                if( (nr1 / pw[1][i]) % (B + 1) == 0 )
                {
                    int nx = nr1 + (c + 1) * pw[1][i];
                    double ans = solve(nx, nr2, rem1 - 1, rem2);
                    bst = max(bst, ans);
                }
            val += bst;
        }
        val /= (double)B;
        dp[state] = val;
        return val;
    }
    else
    {
        double val = 0.0;
        for(int c = 0; c < B; c++)
        {
            double bst = 1.0;
            for(int i = 0; i < N; i++)
                if( (nr2 / pw[1][i]) % (B + 1) == 0 )
                {
                    int nx = nr2 + (c + 1) * pw[1][i];
                    double ans = solve(nr1, nx, rem1, rem2 - 1);
                    bst = min(bst, ans);
                }
            val += bst;
        }
        val /= (double)B;
        dp[state] = val;
        return val;
    }
    return 0.0;
}

/*void solve_test()
{
    scanf("%d%d", &N, &B);
    if(N == 0)  exit(0);
    cout << fixed << setprecision(15) << ans[N][B][0] << " " << ans[N][B][1] << '\n';
}*/

int main()
{
    freopen("greater.in", "r", stdin);
    freopen("greater.out", "w", stdout);

    /*N = 2;
    B = 2;
    pre();
    dp.clear();
    double ans = solve(0, 0, N, N);
    cout << fixed << setprecision(15) << ans << '\n';
    return 0;*/

    for(int b = 2; b <= 10; b++)
    {
        int pw = 1;
        for(int n = 1; n <= 10; n++)
        {
            pw *= (b + 1);
            if(pw > 3000)   break;
            //printf("%d %d\n", n, b);
            N = n;
            B = b;
            dp.clear();
            pre();
            double ans = solve(0, 0, N, N);
            printf("ans[%d][%d] = ", N, B);
            cout << fixed << setprecision(15) << ans << '\n';
        }
    }

    //while(1)
    //    solve_test();

    return 0;
}