牛客多校(2020第十场)A Permutation
输入
2 3 5
输出
1 2 1 2 4 3
题解:
每次能*2就*2,不行就*3
1 #include<unordered_map>
2 #include<vector>
3 #include<iostream>
4 #include<cstring>
5 #include<algorithm>
6 #include<queue>
7 #include<cstring>
8
9 using namespace std;
10 #define ll long long
11 const ll N = 1e6 + 5;
12
13 inline ll read() {
14 ll x = 0, f = 1;
15 char ch = getchar();
16 while(ch<'0'||ch>'9'){
17 if(ch=='-')
18 f=-1;
19 ch=getchar();
20 }
21 while(ch>='0'&&ch<='9'){
22 x = x * 10 + ch - '0';
23 ch = getchar();
24 }
25 return x * f;
26 }
27
28 bool visit[N];
29 vector <int> res;
30
31 void solve() {
32 res.clear();
33 fill (visit, visit+N, false);
34 int p = read();
35 int cnt = 1, num = 1;
36 visit[num] = true;
37 res.push_back(num);
38 while (cnt < p-1) {
39 int first = 2 * num % p, second = 3 * num % p;
40 if (visit[first] == false && first) {
41 res.push_back(first);
42 visit[first] = true;
43 num = first;
44 cnt++;
45 }
46 else if (visit[second] == false && second) {
47 res.push_back(second);
48 visit[second] = true;
49 num = second;
50 cnt++;
51 }
52 else {
53 cout << "-1\n";
54 return;
55 }
56 }
57 for (int i = 0; i < res.size(); i++) {
58 printf("%d ", res[i]);
59 }
60 puts("");
61 }
62
63 int main() {
64 int t = read();
65 while (t--) {
66 solve();
67 }
68 return 0;
69 }

浙公网安备 33010602011771号