其他分享
首页 > 其他分享> > AtCoder Beginner Contest 220 A-F

AtCoder Beginner Contest 220 A-F

作者:互联网

A

#include <iostream>

using namespace std;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    for(int i = 1; i <= 1000; i ++ ) {
        if(c * i >= a && c * i <= b) {
            cout << c * i ;
            return 0;
        }
        if(c * i > b) break;
    }
    cout << "-1";
    return 0;
    
    return 0;
}

B

#include <iostream>
#include <algorithm>
#include <cstring>

#define LL long long
using namespace std;

int main() {
    string a, b;
    int k; cin >> k >> a >> b;
    LL cnt1 = 0, cnt2 = 0;
    // cout << a << b << endl;
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    LL res = 1;
    for(int i = 0; a[i]; i ++ ) {
        int p = a[i] - '0';
        cnt1 += p * res;
        res *= k;
    }
    res = 1;
    for(int i = 0; b[i]; i ++ ) {
        int p = b[i] - '0';
        cnt2 += p * res;
        res *= k;
    }
    // cout << cnt1 << ' ' << cnt2 << endl;
    cout << cnt1 * cnt2;
    
    
    
    return 0;
}

C

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>

#define LL long long
using namespace std;

int main() {
    int n; cin >> n;
    vector<int> a(n);
    LL ans = 0;
    for(int &x: a) {
        cin >> x;
        ans += x;
    }
    LL p; cin >> p; 

    LL res = p / ans * n;
    LL pp = 0;
    p %= ans;

    for(int i = 0; i < n; i ++ ) {
        pp += a[i];
        if(pp > p) {
            cout << res + i + 1 << endl;
            return 0;
        } 
    }

    
    
    
    return 0;
}

D

线性DP

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#define pb push_back

#define mod(x) (x) % MOD
#define LL long long

using namespace std;

const int MOD = 998244353, N = 1e5 + 10;

LL cnt[10];
int n, a[N];


int main() {
    cin >> n;
    for(int i = 0; i < n; i ++ ) cin >> a[i];
    vector<LL> p(10, 0), q(10, 0);
    q[a[0]] = 1;
    for(int i = 1; i < n; i ++ ) {
        // p = q;
        for(int j = 0; j < 10; j ++ ) {
            p[j] = q[j] % MOD;
            q[j] = 0;
        }
        for(int j = 0; j < 10; j ++ ) {
            q[(j + a[i]) % 10] =  mod(q[(j + a[i]) % 10] + p[j]);
            q[(j * a[i]) % 10] = mod(q[(j * a[i]) % 10] + p[j]);
        }

    }
    for(int i = 0; i < 10; i ++ ) {
        cout << q[i] % MOD << endl;
    }
    
    
    return 0;
}

E

对于每个节点形成的链有两种可能

这样枚举可以不重不漏的找出所有的长度固定的链
而且,对于每个节点第一种有\(2^{n+1}\)个(正向逆向要重复计数),第二种有\((d-1)*{2^{d-1}}\)个
当然有的节点可能不存在那么多,也就是当剩余的层数不足d的时候,我们就需要一层一层的拓展
比如说往下扩展一层就多了 $2 * {2^{d-2}} $ 即 左边的层数所具有的节点 * 右边层数所具有的节点 永远都是 \(2^{d-1}\)
因此还剩多少层我就就会加多少个 \(2^{d-1}\)

AC_CODE

#include <bits/stdc++.h>
#define mod(x) ((x) % MOD)
#define LL long long

using namespace std;

const int N = 2e6 + 10, MOD = 998244353;

LL num[N];


int main() {
    // pre
    num[0] = 1;
    for(int i = 1; i < N; i ++ ) 
        num[i] = mod(num[i - 1] * 2);
    LL ans = 0;
    LL n, d; cin >> n >> d;

    for(int i = 0; i < n; i ++ ) {
        LL cnt = num[i];
        LL mx = n - i - 1;
        if(mx >= d) {
            ans = mod(ans + mod(num[d + 1] * cnt)); // 求出以第i层的节点为 链的端点的所有个数
            ans = mod(ans + mod(num[d - 1] * mod((d - 1) * cnt)));// 求出以第i层的节点为 链的转折点的所有个数
            //转折过后必须向下转折 可以不重不漏
        } else if(2 * mx >= d) {
            ans = mod(ans + mod(mod(num[d - 1] * cnt) * (2 * mx - d + 1)));
            //求出以第i层的节点为 链的转折点的所有个数 因为不可以为 链的端点
        } else break;
    }
    cout << mod(ans) << endl;


    return 0;
}

F

思路

先求出以1号节点为起点,其他的点到1号点的距离,然后对于1号点子节点,我们再继续求以这个点为起点的路径长度的时候
我们可以把其他的点分为两种

对于第一种,每条路径长度减去1就是我们想知道的路径长度,第二种则需要加上1
整理一下会发现就是 ans[j] = ans[u] + (n - size(j)) - size(j); (u是j的根节点)
因此可以用树形DP解决这个问题

AC_CODE

#include <iostream>
#include <cstring>
#define LL long long

using namespace std;

const int N = 2e5 + 10, M = N << 1;

int h[N], e[M], ne[M], idx;
int n;
LL ans[N], s[N];

void add(int a, int b) {
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx ++;
}

int dfs(int u, int fa, int cnt) {
    ans[1] += cnt;
    int res = 1;
    for(int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if(j == fa)
            continue;
        res += dfs(j, u, cnt + 1);
    } 
    s[u] = res;
    return res;
}

void dfs1(int u, int fa) {
    for(int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if(j == fa)
            continue;
        ans[j] = ans[u] + n - 2 * s[j];
        dfs1(j, u);
    }
}

int main() {
    memset(h, -1, sizeof h);
    scanf("%d", &n);
    for(int i = 1; i < n; i ++ ) {
        int u, v;
        scanf("%d%d", &u, &v);
        add(u, v); add(v, u);
    }
    dfs(1, -1, 0);
    dfs1(1, -1);
    for(int i = 1; i <= n; i ++ )
        printf("%lld\n", ans[i]);
    return 0;
}

标签:AtCoder,Beginner,10,int,LL,ans,mod,include,220
来源: https://www.cnblogs.com/c972937/p/15365787.html