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~)/

Codeforces 932 C.Permutation Cycle-数学 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))的更多相关文章

  1. Codeforces 932.C Permutation Cycle

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

  2. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined)

    靠这把上了蓝 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 megabyte ...

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

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大 ...

  4. Codeforces 932 B.Recursive Queries-前缀和 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    B. Recursive Queries   time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  5. Codeforces 932 A.Palindromic Supersequence (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))

    占坑,明天写,想把D补出来一起写.2/20/2018 11:17:00 PM ----------------------------------------------------------我是分 ...

  6. ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A

    2018-02-19 A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 mega ...

  7. 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) D】Tree

    [链接] 我是链接,点我呀:) [题意] 让你在树上找一个序列. 这个序列中a[1]=R 然后a[2],a[3]..a[d]它们满足a[2]是a[1]的祖先,a[3]是a[2]的祖先... 且w[a[ ...

  8. 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) B】Recursive Queries

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个记忆化搜索. 接近O(n)的复杂度吧 [代码] #include <bits/stdc++.h> using nam ...

  9. 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A】 Palindromic Supersequence

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 字符串倒着加到原串右边就好 [代码] #include <bits/stdc++.h> using namespace ...

随机推荐

  1. 【netbeans】【ubuntu】ubuntu netbeans 抗锯齿化修复

    每一个在ubuntu下用netbeans的,都会对它的字体怎么会显示的那么难看表示很不理解.我就是因此几乎没有用netbeans的.   不过今天终于解决问题了,虽然没有eclipse显示的那么漂亮, ...

  2. python3 兔子繁殖问题

    题目 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 代码: month = int(input("繁殖 ...

  3. python文件打包为exe可执行文件的方法

    我自己常用Pyinstaller库打包 第一步: 安装pyinstaller库   pip install pyinstaller 第二步: 在py文件所在目录输入 mydemo.py是自己写的py文 ...

  4. Python9-网络编程-day30

    # 由于不同机器上的程序要通信,才产生了网络# server# client# 端口 找到的程序# 在计算机上,每一个需要网络通信的程序,都会开一个端口# 在同一时间只会有一个程序占用一个端口# 不可 ...

  5. LeetCode(164)Maximum Gap

    题目 Given an unsorted array, find the maximum difference between the successive elements in its sorte ...

  6. Linux学习-systemctl 针对 timer 的配置文件

    如何使用 systemd 内建的 time 来处理各种任务呢? systemd.timer 的优势 在 archlinux 的官网 wiki 上面有提到,为啥要使用 systemd.timer 呢? ...

  7. HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树

    题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...

  8. Django Model two

    Django_model: eg: class XXXX(models.Model): nid = models.AutoField(primary_Key=True) name = models.C ...

  9. 如何解决自定义404页面在IE等浏览器中无法显示问题

    网站设置自定义404页面之后(如何在IIS下正确设置404页面?),如无法在浏览器中正常显示,可能是以下原因: 1.404页面文件权限设置错误 我们需要为404页面文件添加上用户everyone的可读 ...

  10. MAC OS X 终端命令入门

    在这里记下..防止丢失 pwd 当前工作目录 cd(不加参数) 进root cd(folder) 进入文件夹 cd .. 上级目录 cd ~ 返回root cd - 返回上一个访问的目录 rm 文件名 ...