Codeforces 932.C Permutation Cycle

C. Permutation Cycle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

For a permutation P[1... N] of integers from 1 to N, function f is defined as follows:

Let g(i) be the minimum positive integer j such that f(i, j) = i. We can show such j always exists.

For given N, A, B, find a permutation P of integers from 1 to N such that for 1 ≤ i ≤ Ng(i) equals either A or B.

Input

The only line contains three integers N, A, B (1 ≤ N ≤ 106, 1 ≤ A, B ≤ N).

Output

If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.

Examples
input
9 2 5
output
6 5 8 3 4 1 9 2 7
input
3 2 1
output
1 2 3 
Note

In the first example, g(1) = g(6) = g(7) = g(9) = 2 and g(2) = g(3) = g(4) = g(5) = g(8) = 5

In the second example, g(1) = g(2) = g(3) = 1

题目大意:p是一个置换,要求将一个序列分成若干个环,大小为a或b.(不是很好说......),给出p.

分析:枚举一下a的系数,可以推测出b的系数,如果ax + by = n没有非负整数解,那么就输出-1,否则一个一个环直接构造就好了.

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>

int n,a,b;
int cnta,cntb;
int flasttg=false;
int main()
{
    scanf("%d%d%d",&n,&a,&b);
    for (int i = 0; i * a <= n; i++)
    {
        int t = n - a * i;
        if (t % b == 0)
        {
            cnta = i;
            cntb = t / b;
            flasttg = true;
            break;
        }
    }
    int pos=1;
    int lastt=pos;
    if(!flasttg)
        printf("-1\n");
    else
    {
        for(int i = 1;i <= cnta;i++)
        {
            for(int k = 1;k < a;k++)
            printf("%d ",++pos);
            printf("%d ",lastt);
            pos++;
            lastt=pos;
        }
        for(int i = 1;i <= cntb;i++)
        {
            for(int k = 1;k < b;k++)
            printf("%d ",++pos);
            printf("%d ",lastt);
            pos++;
            lastt=pos;
        }
    }
    return 0;
}

 

 

 

转载于:https://www.cnblogs.com/zbtrs/p/8450161.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值