AtCoder Beginner Contest 132 problem-solving report

 

The first four questions are good water. Behind two very difficult questions.

C Divide the Problems

#include <cstdio>
#include <algorithm>
using namespace std;

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
    return x * f;
}

const int N = 1e5 + 10;
int a[N];

int main() {
    int n = read();
    for (int i = 0; i < n; i++) a[i] = read();
    sort(a, a + n);
    printf("%d\n", a[n / 2] - a[n / 2 - 1]);
    return 0;
}
View Code

 

D Blue and Red Balls

#include <cstdio>
using namespace std;

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
    return x * f;
}

const int MOD = 1e9 + 7;
const int N = 2010;
int C[N][N];

void init() {
    C[0][0] = 1;
    C[1][0] = C[1][1] = 1;
    for (int i = 2; i < N; i++) {
        C[i][0] = 1;
        for (int j = 1; j <= i; j++) 
            C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
    }
}

int main() {
    init();
    int n = read(), k = read();
    for (int i = 1; i <= k; i++) {
        printf("%d\n", 1LL * C[k - 1][i - 1] * C[n - k + 1][i] % MOD);
    }
    return 0;
}
View Code

 

E Hopscotch Addict

Question is intended: to a directed graph can ask from $ S $, take $ 3 ^ {x} $ $ x \ geq 1 $ $ X $ can be output, the output is not -1

Ideas: BFS. At first thought was for each point, enumerate it take three steps backward point, but the T. Positive solutions should $ dis \ left [i \ right] \ left [k \ right] $ denotes come $ i $, and take the number of steps equal to the mold 3 and $ k $ BFS OK.

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

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
    return x * f;
}

const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int n, m, s, t, dis[N][3];
vector<int> G[N];

void bfs() {
    memset(dis, -1, sizeof(dis));
    dis[s][0] = 0;
    queue< pair<int, int> > que;
    que.push({s, 0});
    while (!que.empty()) {
        pair<int, int> u = que.front(); que.pop();
        for (auto v : G[u.first]) {
            if (dis[v][(u.second + 1) % 3] < 0) {
                dis[v][(u.second + 1) % 3] = dis[u.first][u.second] + 1;
                que.push({v, (u.second + 1) % 3});
            }
        }
    }
}

int main() {
    n = read(), m = read();
    while (m--) {
        int u = read(), v = read();
        G[u].push_back(v);
    }
    s = read(), t = read();
    bfs();
    if (dis[t][0] == -1) puts("-1");
    else printf("%d\n", dis[t][0] / 3);
    return 0;
}
View Code

 

F Small Products

Meaning of the questions: Given a $ N $, $ K $, ask a length of $ K $, and the number of programs the product of two numbers do not exceed $ N $ neighbor.

Thinking: I can only $ O \ violence left (KN ^ {2} \ right) $ a. Read an instant solution to a problem that ... tql

The less the number of divided $ \ sqrt {N} $ greater than $ \ sqrt {N} $, denoted by $ s $ and $ b $

The $ b $ into $ \ sqrt {N} $ block of $ I $ block represents $ i \ cdot b \ leq N $ then this block there $ \ dfrac {N} {i} - \ dfrac {N} {i $ the number of + 1}

$ S \ left (i, j \ right) $ denotes the length of $ i $, last number $ j $ ($ j \ leq \ sqrt {N} $) the number of programs

$ B \ left (i, j \ right) $ denotes the length of $ i $, $ B number in the last block of $ ^ {j} where ($ j \ leq \ sqrt {N} $) scheme

A small number of any of the foregoing may be put in a small number, a large number may be put, the number of small numbers of this product is less than equal to $ N $

Then $ S $ recursive expression is $ S \ left (i, j \ right) = \ sum ^ {\ sqrt {N}} _ {k = 1} s \ left (i-1, k \ right) + \ sum ^ {\ sqrt {N}} _ {k = j} B \ left (i-1, k \ right) $

$ B $ recursive formula is $ B \ left (i, j \ right) = \ left (\ dfrac {N} {j} - \ dfrac {N} {j + 1} \ right) \ sum ^ { j} _ {k = 1} S \ left (i-1, k \ right) $

$ans=\sum ^{\sqrt {N}}_{i=1}\left( S\left( k,i\right) +B\left( k,i\right) \right)$

But this complexity is $ O \ left (k \ left (\ sqrt {N} \ right) ^ {2} \ right) $

After the calculation in the calculation of this layer of this layer can give a request and, on the reduced complexity $ O \ left (k \ sqrt {N} \ right) $

Good question.

#include <cstdio>
#include <cstring>
#include <cmath>
#define ll long long
using namespace std;

const ll MOD = 1e9 + 7;
const int N = 5e4 + 10;
ll S[110][N], B[110][N];

inline int read() {
    int x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); }
    return x * f;
}

int main() {
    int n = read(), k = read();
    int r = sqrt(n) + 1;
    S[0][1] = 1;
    for (int i = 0; i < k; i++) {
        for (int j = 1; j < r; j++) S[i][j] = (S[i][j] + S[i][j - 1]) % MOD;
        for (int j = r - 2; j > 0; j--) B[i][j] = (B[i][j] + B[i][j + 1]) % MOD;
        for (int j = 1; j < r; j++) {
            B[i + 1][j] = S[i][j] * (n / j - n / (j + 1));
            if (j == n / j) B[i + 1][j] = 0; //已被包含在S里
            B[i + 1][j] %= MOD;
        }
        for (int j = 1; j < r; j++) {
            S[i + 1][j] = B[i][j] + S[i][r - 1];
            S[i + 1][j] %= MOD;
        }
    }
    ll ans = 0;
    for (int i = 1; i <= r; i++) ans = (ans + B[k][i] + S[k][i]) % MOD;
    printf("%lld\n", ans);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/Mrzdtz220/p/11115001.html