A. Permutation (规律 / 思维) 2020牛客暑期多校训练营(第十场)

传送门

在这里插入图片描述
在这里插入图片描述
思路:

  • 题意: 让你构造1~p-1的一种排列(p是素数),使得a[i+1] == a[i]*2%p 或者 a[i+1] == a[i]*3%p。若无解输出 “-1”。
  • 刚开始一直不知道怎么写,比赛后半段快找出规律了,再经过队友提点还是相通了。
  • 每次肯定都是1 2 4开始,对于 i 位置若 a[i-1]*2没有使用过就使用,否则就使用 a[i-1]*3,若两个都已被使用,说明必定有冲突。(详细见代码!)

代码实现:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 1e6 + 5;

int t, p, vis[N], a[N];

signed main()
{
    IOS;

    cin >> t;
    while(t --){
        cin >> p;
        me(vis);
        a[1] = 1; vis[1] = 1;
        bool flag = 0;
        for(int i = 2; i < p; i ++){
            int a2 = a[i-1]*2%p, a3 = a[i-1]*3%p;
            if(a2 && !vis[a2]){
                vis[a2] = 1;
                a[i] = a2;
            }
            else if(a3 && !vis[a3]){
                vis[a3] = 1;
                a[i] = a3;
            }
            else {flag = 1; break;}
        }
        if(flag) cout << -1 << endl;
        else for(int i = 1; i < p; i ++) cout << a[i] << " \n"[i==p-1];
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Satur9/article/details/107919628