2020牛客暑期多校训练营(第十场)A Permutation

题目链接

思路:

构造,优先乘二,如果能够构造出则输出,不能就输出-1.

代码:

#include
//#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=2e6+7;
const int M=2e4+5;
const double eps=1e-8;
const int mod=998244353;
const int inf=0x7fffffff;
const double pi=3.1415926;
using namespace std;
int flag[N];
signed main()
{
    IOS;
    int t;
    cin>>t;
    while(t--)
    {
       int n,sum=1;
       memset(flag,0,sizeof flag);
       cin>>n;
       vector<int>q;
       q.push_back(1);
       flag[1]=1;
       for(int i=2;i<n;i++)
       {
           if(flag[2*sum%n]==0)
           {
               flag[2*sum%n]=1;
               sum=2*sum%n;
               q.push_back(sum);
           }
           else if(flag[3*sum%n]==0)
           {
               flag[3*sum%n]=1;
               sum=3*sum%n;
               q.push_back(sum);
           }
       }
       if(q.size()!=n-1)
       {
           cout<<-1<<endl;
       }
       else
       {
           for(int i=0;i<n-1;i++)
           {
               cout<<q[i]<<" ";
           }
           cout<<endl;
       }
    }
    return 0;
}

你可能感兴趣的:(思维)