其他分享
首页 > 其他分享> > 晚间测试6

晚间测试6

作者:互联网

T1 : 山洞

#include <bits/stdc++.h>
using namespace std;
inline int read() {
    int k = 0, f = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    for (; isdigit(ch); ch = getchar()) k = k * 10 + ch - '0';
    return k * f;
}
#define int long long
int f[5][1005];
const int mod = 1e9 + 7;    
signed main() {
    freopen("cave.in", "r", stdin);
    freopen("cave.out", "w", stdout);
    int n = read(), m = read();
    if (m > 1000) return !puts("0");
    int add = (m / n + 1) * n;
    f[0][0] = 1;
    for (int i = 1; i <= m; i++) {
        for (int j = 0; j < n; j++) {
            int last = (i - 1) & 1, now = i & 1;
            f[now][j] = (f[last][(1ll * j - i + 1ll * n * m) % n] % mod + f[last][(j + i) % n]) % mod;
        }
    }
    cout << f[m & 1][0] % mod << endl;
    
}

T2 : beauty

#include <bits/stdc++.h>
using namespace std;
inline int read() {
    int k = 0, f = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    for (; isdigit(ch); ch = getchar()) k = k * 10 + ch - '0';
    return k * f;
}
const int mod = 1e9 + 7;
const int maxn = 100000 * 4 + 100;
struct node { int to, next; } e[maxn];
int important[maxn], head[maxn], ecnt;
void add(int u, int v) { e[++ecnt] = (node){v, head[u]}; head[u] = ecnt; }
int size[maxn], dp[maxn], vis[maxn];
int n, k, a;
void dfs(int u, int f) {
    if (important[u]) size[u] = 1;
//  size[u] = 1;
    for (int i = head[u]; i; i = e[i].next) {
        int v = e[i].to;
        if (v == f) continue;
        dfs(v, u);
        size[u] += size[v];
    }
}

int main() {
    freopen("beauty.in", "r", stdin);
    freopen("beauty.out", "w", stdout);
    n = read(), k = read() * 2, a = read();
    for (int i = 1; i <= k; i++) { int x = read(); important[x] = 1; }
    for (int i = 1; i < n; i++) {
        int u = read(), v = read();
        add(u, v), add(v, u);
    }
    dfs(1, 0);
    int ans = 0; 
    for (int i = 1; i <= n; i++) ans += min(size[i], k - size[i]);
    cout << ans << endl;
}

标签:ch,int,read,maxn,测试,晚间,size,getchar
来源: https://www.cnblogs.com/hellohhy/p/13832256.html