Codeforces 932 C.Permutation Cycle-数学 (ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined))
2 seconds
256 megabytes
standard input
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 ≤ N, g(i) equals either A or B.
The only line contains three integers N, A, B (1 ≤ N ≤ 106, 1 ≤ A, B ≤ N).
If no such permutation exists, output -1. Otherwise, output a permutation of integers from 1 to N.
9 2 5
6 5 8 3 4 1 9 2 7
3 2 1
1 2 3
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))的更多相关文章
- Codeforces 932.C Permutation Cycle
C. Permutation Cycle time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大 ...
- 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 ...
- 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 ----------------------------------------------------------我是分 ...
- 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 ...
- 【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[ ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) B】Recursive Queries
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 写个记忆化搜索. 接近O(n)的复杂度吧 [代码] #include <bits/stdc++.h> using nam ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) A】 Palindromic Supersequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 字符串倒着加到原串右边就好 [代码] #include <bits/stdc++.h> using namespace ...
随机推荐
- oracle如何保证读一致性 第二弹
Oracle之数据库一致性读的原理 在Oracle数据库中,undo主要有三大作用:提供一致性读(Consistent Read).回滚事务(Rollback Transaction)以及实例恢复(I ...
- IIS发布网站Microsoft JET Database Engine 错误 '80004005'的解决办法,基于Access数据库
在网站发布后,访问网站会有80004005的错误提示. 项目环境 项目基于Access数据库,server2012,文件系统为NTFS格式. 错误信息 Microsoft JETDatabase En ...
- 迷宫问题&MakeFile
先看一个有意思的问题, 我们定义一个二维数组表示迷宫. 它表示一个迷宫, 其中的1表示墙壁,0表示可以走的路, 只能横着走或竖着走,不能斜着走, 我们要编程序找出从左上角到右下角的路线.其实这个问题可 ...
- 《Scrum实战》第1课【知易行难】全团课后任务汇总
1组 孟帅(班长) kecyru 2017-7-5 http://kecyru.blog.163.com/blog/static/27416617320176411513013 htt ...
- oracle游标遍历
--创建存储过程 CREATE OR REPLACE PROCEDURE xxxxxxxxxxx_p (--参数IN表示输入参数,OUT表示输入参数,类型可以使用任意Oracle中的合法类型. is_ ...
- 正则表达式 去除所有非ASCII字符
需求: 去除字符串中包含的所有外国字符 只能使用正则如下,找到包含非ASCII的记录 db=# select * from test where info ~ '[^(\x00-\x7f)]'; id ...
- deque 类
题外: 'A' +1='B' 1.deque被称为双端队列,它也是一种顺序容器.可通过迭代器存取元素 ,也可以通过下标顺序 存取元素 for(i=0;i<d1.size();i++) { cou ...
- 学习C++的第四天
1.头文件中的 #ifndef/#define/#endif 防止该头文件被重复引用” //文件路径名:el_1\hello.h #ifndef _HELLO /////如果没有定义 _HELLO文件 ...
- python操redis
Python操作redis python连接方式:点击 下面介绍详细使用 1.String 操作 redis中的String在在内存中按照一个name对应一个value来存储 set() #在Redi ...
- Phpstrom 书签应用
F11增加书签 书签 Ctrl + F11切换书签助记符 Ctrl +#[0-9]转到编号书签 Shift + F11显示书签