【AGC009E】Eternal Average

【AGC009E】Eternal Average

Face questions

Luo Valley

answer

.Jpg immortal title

We operate as a \ (K \) tree, where each node is the right value, all leaf nodes (co \ (n + m \) a) is \ (0 \) or \ (1 \) .

Outside the leaf nodes of all nodes on behalf of a merger, the weight is their average.

Set a start \ (0 \) depth point are respectively \ (x_1, x_2 ... x_n \) , \ (. 1 \) depth of \ (Y_1, Y_M Y_2 ... \) .

Then a weight of root \ (\ SUM (\ FRAC 1K) y_i ^ {} \) , and if we will change the value to the right of all points \ (1 \) , the root weight was also \ (1 \) , then there is \ (\ SUM (\ FRAC 1K) x_i ^} + {\ SUM (\ FRAC 1K)} = ^ {y_i. 1 \) , and if this condition is met, will be able to construct a solution.

Then the problem is converted into the number of \ (Z \) can be written as \ (n-\) a \ ((\ FRAC 1K) ^ X \) , \ (. 1-Z \) can be written as \ (m \) a \ (( \ frac 1k) ^ y \) added form.

We \ (Z \) is expressed as \ ((0.c_1c_2 ...) _ K \) , then if the carry \ (\ C = SUM m \) , and then carry on subtracting a carry \ (k-1 \) , then the \ (\ C = SUM m (\ BMOD \;. 1-K) \) .
Suppose there are decimal \ (len \) bits, the \ (1-z \) and should be \ ((len-1) ( k-1) + k- \ sum c = len (k-1) - \ sum . 1 + C \) .

Then set \ (f_ {i, j} \) represents the section \ (I \) bits, and for the current \ (J \) program number can, because the end is not (0 \) \ , so to open remember what the last one-dimensional whether \ (0 \) .
(A large number of problem solution reference litble)

Code

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cstring> 
#include <cmath> 
#include <algorithm> 
using namespace std;
const int Mod = 1e9 + 7; 
const int MAX_N = 2e3 + 5; 
int N, M, K, ans; 
int f[MAX_N << 1][MAX_N][2], s[MAX_N]; 

int main () { 
    cin >> N >> M >> K; 
    f[0][0][0] = 1; 
    for (int i = 1; i <= N + M; i++) { 
        s[0] = (f[i - 1][0][0] + f[i - 1][0][1]) % Mod; 
        for (int j = 1; j <= N; j++) 
            s[j] = (s[j - 1] + (f[i - 1][j][0] + f[i - 1][j][1]) % Mod) % Mod; 
        for (int j = 0; j <= N; j++) { 
            f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % Mod; 
            if (j) f[i][j][1] = (s[j - 1] - (j - K >= 0 ? s[j - K] : 0) + Mod) % Mod; 
        } 
        for (int j = 0; j <= N; j++) 
            if (j % (K - 1) == N % (K - 1) && 
                (i * (K - 1) - j + 1) % (K - 1) == M % (K - 1) &&
                i * (K - 1) - j + 1 <= M)
                ans = (ans + f[i][j][1]) % Mod; 
    } 
    printf("%d\n", ans); 
    return 0; 
} 

Guess you like

Origin www.cnblogs.com/heyujun/p/11728891.html