A

提交情况

1发过。

题目大意

给定 $n$ 个选手,每个选手通过的题目由一个字符串表示(字符代表题目编号)。选手排名首先按通过题目数降序,题目数相同则按罚时升序(罚时各不相同)。已知题目数但不知道罚时的情况下,问可能的最终排名情况有多少种(模 $10^9 + 7$)。关键在于对题目数相同的选手组进行全排列,然后将各组的结果相乘。

思路

按照通过个数进行记录,用排列组合公式求解出答案。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int mod = 1e9 + 7;

int n, cnt[30], ans = 1;

signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
cnt[s.size()]++;
}
for (int i = 1; i <= 26; i++) {
int res = 1;
for (int j = 1; j <= cnt[i]; j++) {
res *= j;
res %= mod;
}
ans *= res;
ans %= mod;
}
cout << ans;
return 0;
}

B

提交情况

pVY9EGQ.png

题目大意

题目要求找到一个正整数 $x$($1 \leq x \leq 10^{18}$),满足:

  1. $\gcd(a, x) = 1$($x$ 与 $a$ 互质),

  2. $\gcd(b, x) > 1$($x$ 与 $b$ 不互质)。

如果不存在这样的 $x$,输出 -1。

思路

题目中要我们求最大的 $x$,我们从第 2 个条件开始入手。

第 2 个条件要去 $gcd(b, x) > 1$,那么 $x$ 就有可能是 $b$ 的倍数,或着 $b$ 的约数。显而易见,$x$ 不可能是 $b$ 的倍数,只有可能是 $x$ 的因数。

接着在考虑条件 1。条件 1 要求 $gcd(a, x) = 1$,这也就要求 $x$ 不能包含 $a$ 的因子,由于要最大,于是我们设 $x = b$ ,然后不停循环,只要 $gcd(a, x)$ 不等于 $1$,就让 $x$ 处以 $gcd(x, a)$。

注意这个操作不止进行一次,要进行多次。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int mod = 1e9 + 7;

int n, cnt[30], ans = 1;

signed main() {
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
cnt[s.size()]++;
}
for (int i = 1; i <= 26; i++) {
int res = 1;
for (int j = 1; j <= cnt[i]; j++) {
res *= j;
res %= mod;
}
ans *= res;
ans %= mod;
}
cout << ans;
return 0;
}

C

提交情况

pVY9Zxs.png

题目大意

给定两个正整数 $x$ 和 $y$,找到一个正整数 $z$ 满足以下条件:

  1. $x < z < y$;

  2. $\gcd(z, y) = 1$;

  3. $\gcd(z, x) = 1$。

如果存在多个解,输出任意一个即可;如果无解,输出 $-1$。

思路

其实这道题就直接枚举 $(x, y)$ 内的所有 $z$ 就行了,只要枚举到一个 $z$ 满足条件,就直接输出,如果没有枚举到就输出 -1。

为啥这样做是正确的呢?

素数的密度是比较高的,大概 $22$ 数字内就会有一个素数,素数是和任意数字互质的,所以大概我们枚举 $20$ 多个数字后我们就会发现有一个素数,就算无解,那么 $y - x$ 这个值也不会超过 $22$,不然就有质数出现,就可以满足条件了。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int T;

ll gcd(ll a, ll b) {
return !b ? a : gcd(b, a % b);
}

void solve() {
ll x, y;
cin >> x >> y;
for (ll z = x + 1; z < y; z++) {
if (gcd(x, z) == 1 && gcd(z, y) == 1) {
cout << z << '\n';
return;
}
}
cout << "-1\n";
}

int main() {
cin >> T;
while (T--) {
solve();
}
return 0;
}

D

提交情况

pVY9V2j.png

题目大意

我们有一个函数 $f(a, b)$ 定义如下:

1
2
3
4
5
6
7
8
9
10
function F(a, b)
i ← b
while i ≥ 2 do
if a mod i = 0 then
return a / i
end if
i ← i - 1
end while
return a
end function

然后,我们需要计算:
$$
S(n) = \sum_{i=1}^n f(n, i)
$$

其中,$n$ 的取值范围是 $1 \leq n \leq 10^9$,并且有 $T$($1 \leq T \leq 10^3$)组测试用例。

思路

可以发现 $f(a, b)$ 中循环变量 $i$ 为小于等于 $b$ 的整数里能整除 $a$ 的最大整数。

因此考虑 $i$ 一定是 $a$ 的因子。对于 $f(n, p)$($1 ≤ p < n$),记 $n$ 的因子从小到大排列为 $d_1, d_2, . . . , d_m$,如果 $d_i ≤ p < d_i+1$($1 ≤ i < m$),则求 $f(n, p)$ 中的循环变量将停在 $d_i$ 位置,返回值为 $\frac{n}{d_i}$ 。考虑 $p = n$,则 $f(n, n) = 1$。

综上,答案为:
$$
1 + \sum_{i = 1}^{m - 1} (d_{i + 1} - d_i) \frac{n}{d_i}
$$
对于 $n$ 质因数分解即可。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
#define int long long

using namespace std;

int T, n;

void solve() {
cin >> n;
if (n == 1) {
cout << 1 << '\n';
return;
}
int t = n;
vector<int> v;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
v.push_back(i);
if (i * i != n) {
v.push_back(n / i);
}
}
}
sort(v.begin(), v.end());
int ans = 0;
ans += (v[1] - v[0]) * (n / v[0]);
for (int i = 1; i < v.size() - 1; i++) {
ans += (v[i + 1] - v[i]) * (n / v[i]);
}
ans += 1;
cout << ans << '\n';
}

signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> T;
while (T--) {
solve();
}
return 0;
}