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. OwinStartupAttribute出错

    尝试加载应用时出现了以下错误.- 找不到包含 OwinStartupAttribute 的程序集.- 找不到包含 Startup 或 [AssemblyName].Startup 类的程序集.若要禁用 ...

  2. thinkcmf5更新模板代码分析,解决模板配置json出错导致数据库保存的配置项内容丢失问题

    private function updateThemeFiles($theme, $suffix = 'html') { $dir = 'themes/' . $theme; $themeDir = ...

  3. SEO 优化

    1.什么是SEO优化: 简单的来说就是了解搜索引擎的排名规则,投机所好,让我们的网站在搜索引擎上得到靠前的排名,获取更多流量的一种方式. 2.SEO优化-衡量标准 关键词的排名--核心关键词的效果 收 ...

  4. Spring Cloud构建微服务架构(二)分布式配置中心

     注:此文不适合0基础学习者直接阅读,请先完整的将作者关于微服务的博文全部阅读一遍,如果还有疑问,可以再来阅读此文,地址:http://blog.csdn.net/sosfnima/article/d ...

  5. PAT Basic 1076

    1076 Wifi密码 下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请 ...

  6. 爬取豆瓣Top250_Ajax动态页面

    爬取网址: 完整代码: import sys from urllib import request, parse import ssl ssl._create_default_https_contex ...

  7. 安装repo

    $ sudo apt-get install curl -y$ curl "http://android.git.linaro.org/gitweb?p=tools/repo.git;a=b ...

  8. InnoDB Redo Flush及脏页刷新机制深入分析

    概要: 我们知道InnoDB采用Write Ahead Log策略来防止宕机数据丢失,即事务提交时,先写重做日志,再修改内存数据页,这样就产生了脏页.既然有重做日志保证数据持久性,查询时也可以直接从缓 ...

  9. Mysql 安装及MySQL-python 问题

    今天遇到了个低级问题: EnvironmentError:mysql_config not found 网上谷歌了一圈发现没用,静下来想的时候才发现新电脑没安装Mysql,吐血 后面再去官网上下载My ...

  10. Flowerpot(单调队列)

    描述 Farmer John has been having trouble making his plants grow, and needs your help to water them pro ...