Luogu2481 SDOI2010 Code auction DP, a combination of

Portal


DP gods

Noting \ (N \ Leq 18 is 10 ^ {} \) , the number of bits can not be directly DP, thus forming considered \ (N \) Nature bits.

Because certainly not smaller than the lower high, all satisfying the condition \ (N \) bits must be not more than \ (9 \) a \ (f (x) (x \ in [1, N]) \) and wherein \ (F (X) = \ SUM \ limits_ I {X = 0} ^ {10}. 1-I ^ \) , and there must be a \ (F (N) \) .

Consider a \ (f (x) \ \ bmod \ P \) the number of columns is formed, because \ (F (X) = 1OF (X-. 1) +. 1 \) , so that the series will present a no more than \ (P \) loop section. Then we can use this pre-out \ (cnt_i = \ SUM \ limits_. 1} ^ {N = X [F (X) \ MOD P = I] \) , obtained simultaneously \ (f (N) \ \ bmod \ P \) values.

Next, the DP can be: Let \ (f_ {i, j, k} \) represents considered \ (cnt_0 \ I-SIM cnt_. 1} {\) , select the \ (K \) a \ (f (x ) \) , and their \ (\ bmod \ P = j \) in the number of programs. Consider transferring enumeration \ (cnt_i \) , select how many, this is a flapper, transfer coefficient is a number of combinations.

The final answer is \ (\ SUM \ limits_ {I = 0} ^ 8 F_ {P, (P - F (N)) \ \ BMOD P, I} \) , \ (I \) max \ (8 \ ) the reason is that we must choose a \ (f (N) \) .

#include<bits/stdc++.h>
using namespace std;

#define int long long
const int MOD = 999911659;
int dp[503][503][9] , Cnt[503] , dir[503] , N , P;

int poww(int a , int b){
    int times = 1;
    while(b){
        if(b & 1) times = times * a % MOD;
        a = a * a % MOD; b >>= 1;
    }
    return times;
}

int binom(int a , int b){
    int times = 1;
    for(int i = a ; i > a - b ; --i)
        times = times * i % MOD * poww(a - i + 1 , MOD - 2) % MOD;
    return times;
}

signed main(){
    cin >> N >> P;
    int cur = 1 % P , cnt = 1 , tmp = 1 % P , ed;
    do{dir[cur] = cnt; ++cnt; cur = (cur * 10 + 1) % P;}while(!dir[cur]);
    for(int i = 1 ; i < dir[cur] && i <= N ; ++i , tmp = (tmp * 10 + 1) % P) ++Cnt[ed = tmp];
    if(dir[cur] <= N){
        for(int i = dir[cur] ; i < cnt ; ++i , tmp = (tmp * 10 + 1) % P) Cnt[ed = tmp] = (N - dir[cur] + 1) / (cnt - dir[cur]) % MOD;
        for(int i = 1 ; i <= (N - dir[cur] + 1) % (cnt - dir[cur]) ; ++i , tmp = (tmp * 10 + 1) % P) ++Cnt[ed = tmp];
    }
    dp[0][0][0] = 1;
    for(int i = 0 ; i < P ; ++i)
        for(int j = 0 ; j <= 8 ; ++j){
            int val = binom(Cnt[i] + j - 1 , j);
            if(!val) continue;
            for(int k = 0 ; k < P ; ++k)
                for(int l = 0 ; l + j <= 8 ; ++l)
                    dp[i + 1][(k + i * j) % P][l + j] = (dp[i + 1][(k + i * j) % P][l + j] + val * dp[i][k][l]) % MOD;
        }
    int sum = 0;
    for(int i = 0 ; i <= 8 ; ++i)
        sum = (sum + dp[P][(P - ed) % P][i]) % MOD;
    cout << sum;
    return 0;
}

Guess you like

Origin www.cnblogs.com/Itst/p/11295096.html