Project Euler_Problem 172_Few Repeated Digits_动态规划

原题目:

题目大意:18位数里头,有多少个数,对于每个数字0-9,在这18位里面出现均不超过3次

111222333444555666 布星~~
112233445566778899 可以~~
 

解题思路:

动态规划

代码:

ll F[19][3000000];


void solve() {
    ll i, j,k,x,y,z,p,q,u,v;
    ll N = 18,NN=4;
    double a, b, c,d;

    

    F[0][0] = 1;

    for (i = 1; i <= N; i++) {

        for (j = 0; j <= 9; j++) {
            if (j == 0 && i == N)continue;
            for (k = 0; k <= (1 << 21); k++) {
                p = k;
                u = 0;
                while (p) {
                    z = (3 & p);
                    u = u + z;
                    p = p >> 2;
                    if (u > i - 1)break;
                }
                if (u == i - 1) {
                    p = k;
                    v = 3 & (p >> (2 * j));
                    if (v+1 >= NN)continue;
                    F[i][k + ((ll)1 << (2 * j))] += F[i-1][k];

                }
            }
        }
    }

    for (i = 1; i <= (1 << 21); i++) {
        p = i; u = 0; flag = 1;
        while (p) {
            z = (3 & p);
            u = u + z;
            if (z > NN) { flag = 0; break; }
            p = p >> 2;
        }
        if (u == N&&flag==1) {
            printf("%lld %lld\n", i,F[N][i]);
            ans1 = ans1 + F[N][i];
        }
    }


    
    printf("%lld\n",ans1);
}

上一篇:深度学习500问——Chapter07:生成对抗网络(GAN)(2)


下一篇:VRRP——虚拟路由冗余协议