• 牛客多校(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
    ";
    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 }
  • 相关阅读:
    在Myeclipse中配置tomcat
    tomcat用startup.bat启动时,界面一闪消失
    单向链表的基本操作
    简单排序的对比
    关于string的排序 选择插入排序
    insertsort
    selectsort
    理解函数式编程
    vue中使用$nextTick后任然无法正确计算出元素高度
    使用vuex+vue-i18n方式国际化
  • 原文地址:https://www.cnblogs.com/mr-wei977955490/p/15367577.html
Copyright © 2020-2023  润新知