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

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的。 即最后会形成若干个环。 g[i]显然等于那个环的大小。 即让你形成若干个环。 每个环的大小只能为A或B 则相当于问A*x+B*y=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;
}
原文地址:https://www.cnblogs.com/AWCXV/p/8453131.html