Codeforces Round 873 (Div. 2) A~C

A. Divisible Array

当首项为2,公差为2的前n项和公式:n * (n + 1)

直接输出i * 2(1 <= i <= n)即可。

#include<bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >>T;
    while (T--){
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) cout << i * 2 << ' ';
        cout << '\n';
    }
    return 0;
}

B. Permutation Swap

求出每个数减自身下标的绝对值,求GCD。

#include<bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--){
        int n;
        cin >> n;
        int GCD = 0;
        for (int i = 1; i <= n; i++){
            int x;
            cin >> x;
            GCD = __gcd(GCD, abs(x - i));
        }
        cout << GCD << '\n';
    }
    return 0;
}

C. Counting Orders

先将a、b两个数组从小到大排序。

如果从b数组的开头往后遍历每个数有多少种可能,后面的数的选择可能会对前面的数产生影响。

反过来从后往前遍历的话,后面的数不会对前面的数产生影响,直接二分找到a数组中大于b[i]的下标(即对于b[i]有多少种选择),再减去已经用过的数,相乘取模。

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int mod = 1e9 + 7;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--){
        int n;
        cin >> n;
        vector<int> a(n), b(n);
        for (int i = 0; i < n; i++) cin >> a[i];
        for (int j = 0; j < n; j++) cin >> b[j];
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        ll res = 1;
        for (int i = n - 1; i >= 0; i--){
            int x = a.end() - upper_bound(a.begin(), a.end(), b[i]);
            res = (res * (x - (n - i - 1)) % mod) % mod;
        }
        cout << res << '\n';
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值