2023-ACM-week3

饿饿 饭饭之暑假大狂欢

  1. 对于每一个玩家,我们可以将他们的卡片上的数字进行排序,从小到大排列。这样做的目的是为了让玩家在选择划掉数字的顺序时,可以优先划掉小的数字,从而更有可能在最短的时间内划掉所有的数字。

  2. 然后我们可以根据每个玩家的卡片上的数字,计算出在最有利的情况下,每一步应该划掉哪个数字。具体的计算方法是,我们首先划掉当前卡片上最小的数字,然后在其它玩家卡片中找到这个数字,并将这个数字从这些卡片上划掉。然后再找到卡片上最小的未被划掉的数字,依次类推。

  3. 最后我们可以判断每个玩家的卡片是否被划掉了所有的数字,如果是,则输出YES,否则输出NO。

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> nums(n);
        unordered_map<int, int> cnt;
        for (int i = 0; i < n; ++i) {
            cin >> nums[i];
            ++cnt[nums[i]];
        }
        bool flag = true;
        for (int i = 1; i <= 100; ++i) {
            if (cnt[i] > 1) {
                flag = false;
                break;
            }
        }
        if (!flag) cout << "NO" << endl;
        else {
            vector<bool> used(n, false);
            for (int i = 1; i <= 100; ++i) {
                bool ok = false;
                for (int j = 0; j < n; ++j) {
                    if (!used[j] && nums[j] == i) {
                        used[j] = true;
                        ok = true;
                        break;
                    }
                }
                if (!ok) break;
            }
            if (used.back()) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}

RSA

  1. 首先判断A和B是否相等,如果相等直接判定为no credit。

  2. 判断A和B是否是质数,可以用质数筛选或者试除法来判断。

  3. 如果A和B是质数且不相等,判断A×B是否为整数的平方的整数倍,可以先将A和B的最大公约数求出,然后判断A和B的积能否被最大公约数的平方整除,如果能,则为full credit,否则为partial credit。

#include<bits/stdc++.h>

using namespace std;

bool isPrime(long long x) {
    if (x < 2) return false;
    for (long long i = 2; i <= sqrt(x); ++i) {
        if (x % i == 0) return false;
    }
    return true;
}

int main() {
    long long A, B;
    cin >> A >> B;

    if (!isPrime(A) || !isPrime(B)) {
        cout << "no credit" << endl;
        return 0;
    }

    long long C = A * B;
    long long D = sqrt(C);
    if (D * D != C) {
        cout << "partial credit" << endl;
    } else {
        cout << "full credit" << endl;
    }

    return 0;
}

数组操作

首先找到数组中的最大值和最小值,若二者不相等,则至少需要进行一次操作,因为最大值和最小值需要通过某一操作进行改变,使得它们相等。假设数组中所有元素都相等,那么每次操作都是无意义的。考虑从最大值和最小值入手,设最大值为 M,最小值为 m,假设 M 和 m 差距为 d,那么可以通过一次操作将一个偶数大小的子数组中的 M 值向 m 值逐一变换,同时改变其它位置的值。当然,因为每次操作只能操作偶数大小的子数组,因此需要通过多次操作,每次操作后才能保证 m 值逐渐逼近 M 值。

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

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        int res = 0;
        for (int i = 0; i < n - 1; i++) {
            res += abs(a[i] - a[i + 1]);
        }
        int ans = res;
        for (int i = 0; i < n; i++) {
            if (i == 0 || i == n - 1) {
                ans = min(ans, res - abs(a[i] - a[i + 1]));
            } else {
                int d = min(abs(a[i] - a[i - 1]), abs(a[i] - a[i + 1]));
                ans = min(ans, res - abs(a[i] - a[i - 1]) - abs(a[i] - a[i + 1]) + d);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

A-B 数对

  1. 遍历数组,对于每个数 a[i],在哈希表中查找 a[i]-C 的值的数量 cnt,将 cnt 加入答案。

  2. 将当前数 a[i] 加入哈希表中。

  3. 遍历结束后,返回答案。

#include <iostream>
#include <unordered_map>

using namespace std;

int main()
{
    int n, c;
    cin >> n >> c;

     map<int, int> m;
    int res = 0;
    for (int i = 0; i < n; i ++ )
    {
        int x;
        cin >> x;

        res += m[x + c];
        m[x] ++;
    }

    cout << res << endl;

    return 0;
}

数位计算

  1. 设 $dp[i]$ 表示数字位数不超过 $i$ 时的答案。

  2. 以数字 $i$ 为结尾的数中,位数不超过 $i$ 的个数为 $i - 10^{len_i - 1} + 1$。

  3. 遍历 $i$ 的所有数字位数 $len_i$,则 $dp[i] = \sum_{j=1}^{len_i-1}(9 \times 10^{j-1}) + i - 10^{len_i - 1} + 1$。

  4. 最后答案为 $\sum_{i=1}^n dp[i]$。

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

typedef long long LL;

const int MOD = 998244353;

LL n;
int cnt, digits[20], f[20][2][2], ten[20], sum[20][2];

int dfs(int pos, int state, bool limit, bool zero) {
    if (pos == -1) return state == 1;
    if (!limit && !zero && f[pos][state][cnt] != -1) return f[pos][state][cnt];

    int ans = 0;
    int maxv = limit ? digits[pos] : 9;
    for (int i = 0; i <= maxv; ++i) {
        int newcnt = zero ? 0 : cnt + (i == 1 ? 1 : 0);
        ans = (ans + dfs(pos - 1, state || (cnt > 0 && i > 0), limit && (i == maxv), zero && (i == 0))) % MOD;
    }
    if (!limit && !zero) f[pos][state][cnt] = ans;
    return ans;
}

int main() {
    cin >> n;

    ten[0] = 1;
    for (int i = 1; i <= 18; ++i) {
        ten[i] = ten[i - 1] * 10;
        if (ten[i] > n) {
            ten[i] = n + 1;
            break;
        }
    }

    int ans = 0;
    for (int i = 1; i <= 18; ++i) {
        memset(f, -1, sizeof(f));
        for (int j = 1; j < ten[i]; ++j) {
            cnt = 0;
            int x = j;
            while (x) {
                digits[cnt++] = x % 10;
                x /= 10;
            }
            ans = (ans + dfs(cnt - 1, 0, true, true)) % MOD;
        }
    }

    cout << ans << endl;

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值