Codeforces Round 856 (Div. 2)(A~D)

A. Prefix and Suffix Array

给出一个字符串的所有前缀和后缀,判断这个字符串是否是回文串。给出的前后缀都不是整个字符串。

思路:如果是回文的话,那每一个等长的字符串也都是相同的。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 2e5 + 5;
int t, n;
std::string s;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin >> t;
    while(t --) {
        std::cin >> n;
        std::map<int, std::string> mp;
        bool flag = true;
        for(int i = 1; i <= 2 * n - 2; i ++) {
            std::cin >> s;
            int len = s.length();
            if(mp[len].empty())
                mp[len] = s;
            else {
                reverse(s.begin(), s.end());
                if(s != mp[len]) flag = false;
            }
        }
        std::cout << (flag ? "YES" : "NO") << '\n';
    }
    return 0;
}

B. Not Dividing

给出一个数组,每次可以对任意一个数字加一,在2n次操作内,使得所有的数满足后一个数不能整除前一个数。

思路:如果数组的前n-1个数中没有1的时候,若a[i + 1] % a[i] == 0,则一定存在(a[i + 1] + 1) % a[i] != 0。直接贪心去做就可以,注意遇到1必须变成2。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e4 + 5;
int t, n;
ll a[N];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin >> t;
    while(t --) {
        std::cin >> n;
        for(int i = 1; i <= n; i ++) {
            std::cin >> a[i];
        }
        if(a[1] == 1) a[1] ++;
        for(int i = 2; i <= n; i ++) {
            if(a[i] == 1) a[i] ++;
            while(a[i] % a[i - 1] == 0)
                a[i] ++;
        }
        for(int i = 1; i <= n; i ++) {
            std::cout << a[i] << " \n"[i == n];
        }
    }
    return 0;
}

os:感觉很抽象QWQ

C. Scoring Subsequences

给出一个数组,从头开始,对于1~n长度的数组输出满足分数最大的最长序列的长度。

思路:很显然,因为数组是不减的,所以每次能取得的满足条件的数组一定是从第i个数向前取若干位。这样每加入一个数字,就向答案乘了a[i - k] / k,假设k是取到的数组长度。这样就可以得知每次这个值若是大于等于1,则对答案有正向贡献,小于1则反向贡献,所以就按照顺序,每次去掉小于set内个数的数字。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
int t, n, x;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin >> t;
    while(t --) {
        std::cin >> n;
        std::multiset<int> s;
        for(int i = 1; i <= n; i ++) {
            std::cin >> x;
            s.insert(x);
            while(s.size() > 1 && *s.begin() < s.size())
                s.erase(s.begin());
            std::cout << s.size() << " \n"[i == n];
        }
    }
    return 0;
}

D. Counting Factorizations

给出一个长为2n的数组,可以将这些数组组成唯一分解定理的格式,求通过不同的顺序组合,能组合出多少不同的数字。

思路:组合数学。思路来自cup-pyy佬

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e6 + 5;
int t, n, x, tot;
int prime[N];
bool mark[N];

template<const int T>
struct ModInt {
    const static int mod = T;
    int x;
    ModInt(int x = 0) : x(x % mod) {}
    ModInt(ll x) : x(int(x % mod)) {} 
    int val() { return x; }
    ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < mod ? x0 : x0 - mod); }
    ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < 0 ? x0 + mod : x0); }
    ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % mod); }
    ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
    void operator += (const ModInt &a) { x += a.x; if (x >= mod) x -= mod; }
    void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += mod; }
    void operator *= (const ModInt &a) { x = 1LL * x * a.x % mod; }
    void operator /= (const ModInt &a) { *this = *this / a; }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x;}
    
    ModInt pow(int64_t n) const {
        ModInt res(1), mul(x);
        while(n){
            if (n & 1) res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    
    ModInt inv() const {
        int a = x, b = mod, u = 1, v = 0;
        while (b) {
            int t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        if (u < 0) u += mod;
        return u;
    }
    
};
typedef ModInt<998244353> mint;
 
mint fact[N], invfact[N];
 
void init(){
    fact[0] = invfact[0] = 1;
    for(int i = 1; i < N; i++) fact[i] = fact[i - 1] * i;
    invfact[N - 1] = fact[N - 1].inv();
    for(int i = N - 2; i; i--)
        invfact[i] = invfact[i + 1] * (i + 1); 

    mark[1] = true;
    for(int i = 2; i <= N - 3; i ++) {
        if(!mark[i]) prime[++ tot] = i;
        for(int j = 1; prime[j] <= N / i; j ++) {
            mark[i * prime[j]] = true;
            if(i % prime[j] == 0) break;
        }
    } 
}
 
inline mint C(int a, int b){
    if (a < 0 || b < 0 || a < b) return 0;
    return fact[a] * invfact[b] * invfact[a - b];
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    init();
    std::cin >> n;
    std::map<int, int> mp;
    for(int i = 1; i <= n * 2; i ++) {
        std::cin >> x;
        mp[x] ++;
    }
    int sum = 0;
    std::vector<mint> f(n + 1, 0);
    f[0] = 1;
    for(auto [x, y] : mp) {
        std::vector<mint> ff(n + 1, 0);
        for(int i = 0; i <= n; i ++) {
            ff[i] += f[i] * C(n - (sum - i), y);
            if(i >= 1 && !mark[x])
                ff[i] += f[i - 1] * C(n - (sum - i + 1), y - 1);
        }
        f.swap(ff);
        sum += y;
    }
    std::cout << f[n] << '\n';
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值