Codeforces Round #729 (Div. 2) 补题 A B C

A. Odd Set

Codeforces Round #729 (Div. 2) 补题 A B C

分析: 构造每两个数之和为奇数,那么必须奇数的个数为 n n n,若个数 > n >n >n,那么必然存在两个奇数一组,成为偶数,若个数 < n <n <n,那么必然存在两个偶数一组,成为偶数。

代码:

#include <bits/stdc++.h>
using namespace std;
int T, n, x;
signed main() {
    cin >> T;
    while (T --) {
        int cnt = 0;
        cin >> n;
        for (int i = 1; i <= n << 1; i ++) {
            cin >> x;
            if (x & 1) cnt ++;
        }
        if (cnt == n) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
}

B. Plus and Multiply

Codeforces Round #729 (Div. 2) 补题 A B C
分析: 给定的 n n n可以表示为 n = a p + b ∗ q n=a^p+b*q n=ap+b∗q,所以只需要枚举 a a a的幂,假设为 a k a^k ak,再判断 n − a k m o d    b = 0 n-a^k \mod b =0 n−akmodb=0 即可。

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int T, n, a, b;
signed main() {
    cin >> T;
    while (T --) {
        int flag = 0, x = 1;
        cin >> n >> a >> b;
        while (x <= n) {
            if ((n - x) % b == 0) flag = 1;
            x *= a;
            if (a == 1) break;
        }
        if (flag) {
            cout << "Yes" << endl;
        } else {
            cout << "No" << endl;
        }
    }
}

C. Strange Function

Codeforces Round #729 (Div. 2) 补题 A B C

分析:

在 1 ∼ n 1 \sim n 1∼n中的数 k k k,满足 f ( k ) = i f(k)=i f(k)=i的个数为 ⌊ n LCM ( 1 , 2 , ⋯   , i − 1 ) ⌋ − ⌊ n LCM ( 1 , 2 , ⋯   , i ) ⌋ \left \lfloor \frac{n}{\text{LCM}(1,2,\cdots,i-1)} \right \rfloor -\left \lfloor \frac{n}{\text{LCM}(1,2,\cdots,i)} \right \rfloor ⌊LCM(1,2,⋯,i−1)n​⌋−⌊LCM(1,2,⋯,i)n​⌋

那么对总答案的贡献就为 ∑ i = 2 LCM ( 1 , 2 , ⋯   , i ) < n i ∗ ( ⌊ n LCM ( 1 , 2 , ⋯   , i − 1 ) ⌋ − ⌊ n LCM ( 1 , 2 , ⋯   , i ) ⌋ ) \sum _{i=2} ^{\text{LCM}(1,2,\cdots,i)<n}i*(\left \lfloor \frac{n}{\text{LCM}(1,2,\cdots,i-1)} \right \rfloor -\left \lfloor \frac{n}{\text{LCM}(1,2,\cdots,i)} \right \rfloor) i=2∑LCM(1,2,⋯,i)<n​i∗(⌊LCM(1,2,⋯,i−1)n​⌋−⌊LCM(1,2,⋯,i)n​⌋)

整理得 ∑ i = 1 LCM ( 1 , 2 , ⋯   , i ) < n ⌊ n LCM ( 1 , 2 , ⋯   , i ) ⌋ + n \sum_{i=1} ^{\text{LCM}(1,2,\cdots,i)<n}\left \lfloor \frac{n}{\text{LCM}(1,2,\cdots,i)} \right \rfloor +n i=1∑LCM(1,2,⋯,i)<n​⌊LCM(1,2,⋯,i)n​⌋+n

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int T, n;
signed main() {
    cin >> T;
    while (T --) {
        int res = 0;
        cin >> n;
        for (int x = 1, i = 1; x <= n; x = lcm(i, x), i ++) res = (res + n / x) % mod;
        cout << res << endl;
    }
}
上一篇:HDU1573 X问题【扩展欧几里得算法】


下一篇:Codeforces Round #641 (Div. 2)-C. Orac and LCM(数论,gcd,lcm)