【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
则相当于问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;
}

转载于:https://www.cnblogs.com/AWCXV/p/8453131.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值