1. 程式人生 > >【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle

【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle

ech lac oid 位置 spa void emp blog post

【鏈接】 我是鏈接,點我呀:)
【題意】


在這裏輸入題意

【題解】


p[i] = p[p[i]]一直進行下去 在1..n的排列下肯定會回到原位置的。
即最後會形成若幹個環。
g[i]顯然等於那個環的大小。
即讓你形成若幹個環。
每個環的大小只能為A或B
則相當於問Ax+By=n是否有解。
可以枚舉x然後看看n-A*x能否被B整除。

構造x個長度為A的環,y個長度為B的環就好了

【代碼】

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6;

int aa[N+10],n,a,b;

void
dfs(int numa,int numb){ int now = 1; for (int i = 1;i <= numa;i++){ int prenow = now; for (int j = 1;j <= a;j++){ aa[now] = now+1; now++; } aa[now-1] =prenow; } while (numb--){ int prenow = now; for (int j = 1
;j <= b;j++){ aa[now] = now+1; now++; } aa[now-1] = prenow; } for (int i = 1;i <= n;i++) cout<<aa[i]<<' '; } int main(){ #ifdef LOCAL_DEFINE freopen("rush_in.txt", "r", stdin); #endif
ios::sync_with_stdio(0),cin.tie(0); cin >> n >> a >> b; for (int i = 0;i <= N;i++){ int temp = a*i; temp = n-temp; if (temp<0) break; if (temp%b==0){ dfs(i,temp/b); return 0; } } cout<<-1<<endl; return 0; }

【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle