361B - Levko and Permutation - CodeForces Solution


constructive algorithms math number theory *1200

Please click on ads to support us..

C++ Code:

                                                     /*JODD or WOTT!*/
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i,k,n) for(int i=k;i<=n;i++)
#define repr(i,k) for(int i=k;i>=0;i--)
#define PI 3.1415926535897932384626433832795
#define EPS 1e-9
#define PB push_back
#define MOD 100000007
#define MAX INT_MAX
#define MIN INT_MIN
const int MAXL= (int)1e5+10;
class Graph
{
    public:
    map<int, bool> visited;
    map<int, list<int> > adj;
    void addEdge(int v, int w);
    
    void DFS(int v,vector<int> &ans);
    
};
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
}
 
void Graph::DFS(int v, vector<int> &ans)
{
    visited[v] = true;
    
    ans.push_back(v);
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
    {   
        if (!visited[*i])
            DFS(*i,ans);
    }
        
}
int fact[MAXL];
class factorial
{
    public:
     
    int factL(int x);
};
int factL(int x)
{   
    fact[0]=fact[1]=1;
    if(fact[x]==0)
    {
        rep(i,2,MAXL)
      fact[i]=(fact[i-1]*i)%MOD;
    }
    return fact[x];
}
int gcd(int a, int b)
{
    if (a == 0)
       return b;
    if (b == 0)
       return a;
    if (a == b)
        return a;
    if (a > b)
        return gcd(a-b, b);
    return gcd(a, b-a);
}

int lcm(int x,int y)
{   
    return x*y/(gcd(x,y));
}

void run_case()
{
   int n,k;
   cin>>n>>k;
   vector<int>a(n);
   if(k<0 || k>=n){cout<<"-1"<<endl;return;}

    if(k==0)
    {
       a[0]=n;
       rep(i,1,n-1)a[i]=i;
    }
   else if(k==1 )
   {if(n%2){cout<<"-1"<<endl;return;}
   
       a[0]=1;
      if(n>1) a[n-1]=2;
      rep(i,1,n-2)
        a[i]=(i+2);
 
   }
   else if(k==n-1)
   { 
      rep(i,0,n-1)
        a[i]=i+1;
   }
   else
   { 
       a[0]=2+k;
       a[n-1]=1;
      rep(i,1,k)
         a[i]=i+1;
      rep(i,1+k,n-2)
         a[i]=i+2;
   }
   rep(i,0,n-1)
   cout<<a[i]<<" ";
 
}


signed main()
{

ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t=1;//cin>>t;

   while(t--)
   {
       run_case();
   }
return 0;
}


Comments

Submit
0 Comments
More Questions

1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes