赛时想了一下就过了
一个显然的结论:给定一个正整数 n,很多个与 n 互质的正整数的积与 n 互质。
由于这题要求的数的乘积被 n 除余 1,所以这些数都是与 n 互质的。
所以长度 ≤ n 的既约剩余系长度(指的就是 1∼n 与 n 互质的数的个数)。
由于这些数的乘积 modn 不一定为 1,所以要考虑去掉一些最少的数使得剩下的数的积满足条件。然后很明显去掉这个乘积 modn 即为最多的情况。
代码:
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; }
|