zoukankan      html  css  js  c++  java
  • 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
    ");
        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;
    }

     

  • 相关阅读:
    udelay、mdelay、ndelay、msleep使用比较说明
    linux多线程驱动中调用udelay()对整个系统造成的影响(by liukun321咕唧咕唧)
    linux设备驱动那点事儿之平台设备理论篇
    misc_register、 register_chrdev 的区别总结
    platform_driver与file_operations两种方法开发led驱动
    rc522 ,pn544区别
    内核驱动中常见的miscdevice、platform_device、platform_driver
    file_operations结构2
    file_operations结构体解析 1
    android5.0问题
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8450161.html
Copyright © 2011-2022 走看看