• Codeforces 932 C.Permutation Cycle-数学 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))


    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
    Copy
    9 2 5
    output
    6 5 8 3 4 1 9 2 7
    input
    Copy
    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

    这道题真的是要吐槽啊,欺负人(;´д`)ゞ,欺负我读不懂公式题。本来感觉要用exgcd写,简直是浪费,直接for个循环就出来了。

    解释一下样例1,就是9个数,要求有周期为2的循环圈和周期为5的循环圈,这里g(i)就分别是2和5。这个题的公式里的j-1就是周期循环的时候用到的。

    首先怎么解决有几个圈呢?其实就是一个方程,Ax+By=N,直接for一下就可以得到x和y,这里样例1就是2x+5y=9,算出来x=2,y=1。

    所以有2个以2为周期的圈和1个以5为周期的圈。然后就是1~N的数就用一遍,所以直接自己瞎构造就可以。

    继续,解释一下样例为什么是6 5 8 3 4 1 9 2 7

    从6开始,这个数列里面第6个数为1,然后再从1开始,第1个数为6,这个周期为2,是一个圈,然后继续往后,。

    从5开始,第5个数是4,然后从4开始,第4个数是3,然后从3开始,第3个数是8,然后从8开始,第8个数是2,然后从2开始,第2个数是5,循环回来了,这个周期为5,是一个圈。继续。。。

    一直到9之前都是已经存在的圈了,从9开始,第9个数是7,从7开始,第7个数是9,循环成一个圈了,这个周期为2,就满足推出来的是2个2周期的圈,1个5周期的圈。

    答案不唯一,满足条件的还有其他的数列,2 1 4 3 6 7 8 9 5也满足条件。2 1是一个圈,4 3是一个圈,6 7 8 9 5是一个圈。

    对于怎么构造这个数列,你会发现,直接成圈这样的好理解,瞎写一下就可以了,我不知道样例上的是怎么构造出来的。

    代码:

     1 //C. Permutation Cycle-数学题,脑子不行-(本来感觉要用exgcd写)
     2 #include<iostream>
     3 #include<string.h>
     4 #include<stdio.h>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<cstdio>
     8 #include<algorithm>
     9 #include<queue>
    10 using namespace std;
    11 int main(){
    12     int n,a,b;
    13     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    14     cin>>n>>a>>b;
    15     if(a==1||b==1){
    16         cout<<"1";
    17         for(int i=2;i<=n;i++)
    18             cout<<" "<<i;
    19         cout<<endl;
    20         return 0;
    21     }
    22     int x=0,y=0;
    23     for(int i=0;i<=n/a;i++){
    24         if((n-a*i)%b==0){
    25             x=i;
    26             y=(n-a*i)/b;
    27         }
    28     }
    29     if(x==0&&y==0) {
    30         cout<<"-1"<<endl;
    31         return 0;
    32     }
    33     int i=1;
    34     for(int k=0;k<x;k++){
    35         for(int j=0;j<a-1;j++)
    36             cout<<i+j+1<<" ";
    37         cout<<i<<" ";
    38         i+=a;
    39     }
    40     for(int k=0;k<y;k++){
    41         for(int j=0;j<b-1;j++)
    42             cout<<i+j+1<<" ";
    43         cout<<i<<" ";
    44         i+=b;
    45     }
    46     return 0;
    47 }

    先这样,等我看会了LCA和RMQ,本咸鱼就去写D了。(本来今天可以写D的题解,但是我没看完。但是昨天说好了今天要来写题解的,就写A,B,C的题解强行掩饰一波)

    溜了溜了。。。(/~0~)/

  • 相关阅读:
    Android代码执行adb shell top命令读取结果
    Android8.0+跨应用发送广播和跨应用启动前台服务
    Android代码执行adb shell命令
    关于参数获取的那些事儿
    c# 如何处理自定义消息
    esp32 mqtt协议上报 dht11温湿度数据到onenet 指令下发控制开关灯
    swoole-simps自搭mqtt服务器,完美实现订阅,发布并存到mysql
    stm32f103c8t6+esp8266+dht11 mqtt上传温湿度到阿里云
    使用ST-Link下载程序出现Error:Flash Download Failed-“Cortex-M3“ 解决详细步骤(附图)
    stm32f407 oled iic例程,成功点亮oled屏
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9711344.html
Copyright © 2020-2023  润新知