2021 ECNU Campus Invitational Contest

本文主要介绍了五道算法竞赛题目,涉及抽象代数、计数、整数除法、图论和字符串匹配问题。每道题都给出了简洁的解决方案,并通过实例演示了如何求解。通过对这些题目和算法的理解,读者可以提升在算法竞赛中的解题能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Link

思路没写,有空就写思路

A. Abstract Algebra

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        if (a == 0) {
            if (b == 1) {
                printf("2\nB 1\nA %d\n", -d);
            } else {
                printf("2\nB 3\nA %d\n", d);
            }
        } else if (b == 0) {
            if (a == -1) {
                printf("3\nB 1\nA %d\nB 1\n", c);
            } else {
                printf("3\nB 1\nA %d\nB 3\n", -c);
            }
        } else if (c == 0) {
            if (a == 1) {
                printf("1\nA %d\n", b);
            } else {
                printf("2\nA %d\nB 2\n", -b);
            }
        } else {
            if (b == 1) {
                printf("2\nA %d\nB 1\n", -a);
            } else {
                printf("2\nA %d\nB 3\n", a);
            }
        }
    }
	return 0;
}

C. Countdown

#include <bits/stdc++.h>
using namespace std;
int main() {
    cout << 189<< endl;
	return 0;
}

D. Divide

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e7 + 10;
int v[N], prime[N];
int m;
void init() {
    int n = 1e7;
    for (int i = 2; i <= n; i ++ ) {
        if (v[i] == 0) {
            v[i] = i;
            prime[++ m] = i;
        }
        for (int j = 1; j <= m; j ++ ) {
            if (prime[j] > v[i] || prime[j] > n / i) break;
            v[i * prime[j]] = prime[j];
        }
    }
}
int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    init();
    while (t -- ) {
        int l, r, a, b;
        cin >> l >> r >> a >> b;
        bool ok = 1;
        for (int i = 1; i <= m; i ++ ) {
            if (prime[i] > r) break;
            LL cnt1 = 0, cnt2 = 0;
            for (LL j = prime[i]; j <= max(r, b); j *= prime[i]) {
                cnt1 += r / j - (l - 1) / j;
                cnt2 += b / j - (a - 1) / j;
            }
            if (cnt1 > cnt2) {
                ok = 0;
                break;
            }
        }
        if (ok) puts("Yes");
        else puts("No");
    }
	return 0;
}

E. Edge Game

#include <bits/stdc++.h>
using namespace std;
#define PB push_back
typedef vector<int> VI;
const int N = 1e5 + 10;
bool st[N];
int ans;
VI edg[N];
void bfs(int a, int b) {
    queue<PII> q;
    q.push({a, 0});
    st[a] = 1;
    while (q.size()) {
        auto t = q.front(); q.pop();
        for (auto v : edg[t.FI]) {
            if (st[v]) continue;
            if (v == b) {
                ans = t.SE + 1;
                return;
            }
            st[v] = 1;
            q.push({v, t.SE + 1});
        }
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    for (int i = 1; i < n; i ++ ) {
        int u, v;
        cin >> u >> v;
        edg[u].PB(v), edg[v].PB(u);
    }
    int a, b;
    cin >> a >> b;
    bfs(a, b);
    if (ans & 1) cout << "Yes" << endl;
    else cout << "No" << endl;
	return 0;
}

G. Group QQ Speed

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t -- ) {
        int n, m;
        cin >> n >> m;
        if (1 == m) cout << n + 1 << endl;
        else if (m == n) cout << 2 << endl;
        else cout << 3 << endl;
    }
	return 0;
}

I. I Love You

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s, t;
    cin >> s >> t;
    int i = 0, j = 0;
    for (; i < s.size(); i ++ ) {
        if (s[i] == t[j]) j ++;
    }
    if (j == t.size()) cout << "Yes" << endl;
    else cout << "No" << endl;
	return 0;
}

J. Just the Chosen One

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
long double f[N];
void init() {
    f[0] = f[1] = 0;
    for (int i = 2; i < N; i ++ )
        f[i] = f[i - 1] + 1.0 / i;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    init();
    int n, m, k;
    cin >> n >> m >> k;
    double ans = 0;
    if (m >= k) {
        ans += m - k;
        k = m;
    }
    if (n < N && k < N) 
        ans += (double) m * (f[n] - f[k - 1]);
    else 
        ans += (double) m * (log(n) - log(k - 1));
    printf("%9lf", ans);
	return 0;
}

K. K-Primes

#include <bits/stdc++.h>
using namespace std;
int main() {
    int l, k;
    cin >> l >> k;
    if (l == 2 && k <= 3) cout << "Yes" << endl;
    else cout << "No" << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值