CF1514C Product 1 Modulo N 题解

赛时想了一下就过了

一个显然的结论:给定一个正整数 nn,很多个与 nn 互质的正整数的积与 nn 互质。

由于这题要求的数的乘积被 nn 除余 11,所以这些数都是与 nn 互质的。

所以长度 \leq nn 的既约剩余系长度(指的就是 1n1\sim nnn 互质的数的个数)。

由于这些数的乘积 modn\bmod n 不一定为 11,所以要考虑去掉一些最少的数使得剩下的数的积满足条件。然后很明显去掉这个乘积 modn\bmod 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
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;

int n, tot, a[N];

int gcd (int m, int n) {
if (m < n) swap(m, n);
if (n == 0) return m;
return gcd(n, m % n);
}

int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
if (gcd(i, n) == 1) a[++tot] = i;
}
int times = 1;
for (int i = 1; i <= tot; ++i) {
times = 1ll * times * a[i] % n;
}
if (times == 1) {
cout << tot << endl;
for (int i = 1; i <= tot; ++i) cout << a[i] << " ";
cout << endl;
}
if (times > 1) {
cout << tot - 1 << endl;
for (int i = 1; i <= tot; ++i)
if (a[i] != times) cout << a[i] << " ";
cout << endl;
}
return 0;
}

CF1514C Product 1 Modulo N 题解
https://sobaliuziao.github.io/2021/05/08/post/9c167519.html
作者
Egg_laying_master
发布于
2021年5月8日
许可协议