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

这题就是求 Ax+By=N 是否存在非负解,且 x,y解就是A,B的组数,每组将最前面的数丢到最后即可

扩展欧几里得求不定方程

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c)
{
return min(min(a, b), c);
}
template <class T> inline T max(T a, T b, T c)
{
return max(max(a, b), c);
}
template <class T> inline T min(T a, T b, T c, T d)
{
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d)
{
return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int N = ;
// name*******************************
ll n,a,b;
ll x0,y0; // function******************************
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=;
y=;
return a;
}
ll g=exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-(a/b)*y;
return g;
} //***************************************
int main()
{
// ios::sync_with_stdio(0);
// cin.tie(0);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin>>n>>a>>b;
ll g=exgcd(a,b,x0,y0);
ll k1=x0*n/g,k2=y0*n/g;
ll t=;
ll a1=a,b1=b;
a/=g; //求通解时这里的a,b一定要除以最大公约数!!!
b/=g;
if(n%g)
{
cout<<-;
return ;
} if(k1<)
{
t=(-k1)/b;
if((-k1)%b)t++;
if(k2-a*t<)
{
cout<<-;
return ;
}
k1+=b*t;
k2-=a*t;
}
if(k2<)
{
t=(-k2)/a;
if((-k2)%a)t++;
if(k1-b*t<)
{
cout<<-;
return ;
}
k2+=a*t;
k1-=b*t;
} queue<ll>q;
For(i,,n)
{
q.push(i);
}
For(i,,k1)
{
ll x=q.front();
q.pop();
For(j,,a1-)
{
cout<<q.front()<<" ";
q.pop();
}
cout<<x<<" ";
}
For(i,,k2)
{
ll x=q.front();
q.pop();
For(j,,b1-)
{
cout<<q.front()<<" ";
q.pop();
}
cout<<x<<" ";
} return ;
}

C. Permutation Cycle的更多相关文章

  1. Codeforces 932.C Permutation Cycle

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

  2. CF932C Permutation Cycle

    思路: 构造. 实现: #include <bits/stdc++.h> using namespace std; ]; int main() { int n, a, b; while ( ...

  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 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 stand ...

  5. Codeforces Round #463

    A - Palindromic Supersequence /* 题目大意:给出一个串,构造一个包含该串的回文串 */ #include <cstdio> #include <alg ...

  6. 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 ...

  7. 置换及Pólya定理

    听大佬们说了这么久Pólya定理,终于有时间把这个定理学习一下了. 置换(permutation)简单来说就是一个(全)排列,比如 \(1,2,3,4\) 的一个置换为 \(3,1,2,4\).一般地 ...

  8. (转)排列算法 Permutation Generation

    转自:http://www.cnblogs.com/dragonpig/archive/2010/01/21/1653680.html http://www.notesandreviews.com/p ...

  9. Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造

    B. Kyoya and Permutation Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

随机推荐

  1. SVN查看所有日志提交记录

    1. svn默认显示最近一周的文件提交和修改记录,怎么查看更长时间的日志记录呢? 2. TortoiseSVN 3. 点击show all 或者NEXT 100,就可显示更长时间的文件提交记录.

  2. 设计模式原则(4)--Interface Segregation Principle(ISP)--接口隔离原则

    作者QQ:1095737364    QQ群:123300273     欢迎加入! 1.定义: 使用多个专门的接口,而不使用单一的总接口,即客户端不应该依赖那些它不需要的接口. 2.使用场景: 类A ...

  3. phpadmin登录报错:#1045 - Access denied for user 'root'@'localhost' (using password: yes)

    原因:phpmyadmin无法通过root+密码联系mysql; 解决方法:重置mysql密码. 步骤: 1.cmd 2.登录MySQL:mysql -uroot -p    ->root是用户 ...

  4. css3 content 特殊字符和符号

    基本形状 ▲ 9650 25B2 ► 9658 25BA ► 9658 25BA ▼ 9660 25BC◄ 9668 25C4 ❤ 10084 2764 ✈ 9992 2708 ★ 9733 2605 ...

  5. 图像阈值化-threshold、adaptivethreshold

    在图像处理中阈值化操作,从一副图像中利用阈值分割出我们需要的物体部分(当然这里的物体可以是一部分或者整体).这样的图像分割方法是基于图像中物体与背景之间的灰度差异,而且此分割属于像素级的分割.open ...

  6. Android 借助Stetho在Chrome上调试Android网络、数据库、Sharedpreferences

    Android 借助Stetho在Chrome上调试Android网络.数据库.Sharedpreferences 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/a ...

  7. oracle EBS rtf报表不能输出模板样式

    1.需要定义中文的数据定义 2.缺少文件 cd $ADMIN_SCRIPTS_HOME prefs.ora 3.查看文档  文档 ID 1059712.1 (1)请求模版显示不出来 解决:模版定义中模 ...

  8. 一个服务器多个tomcat的配置

    下面我们把配置的详细过程写在下面,以供参考:(此例以配置三个Tomcat为例)1. 下载apache-tomcat-7.0.63,下载下来的文件为apache-tomcat-7.0.63.zip.2. ...

  9. sql点滴44—mysql忘记root密码

    1. 首先检查mysql服务是否启动,若已启动则先将其停止服务,可在开始菜单的运行,使用命令: net stop mysql 打开第一个cmd1窗口,切换到mysql的bin目录,运行命令: mysq ...

  10. plsqldev、oracle插入中文乱码问题解决

    最近在开发项目时用到Oracle数据库,需要手动插入一些数据,但插入中文的sql值时出现了乱码. 中文乱码就是编码不统一所导致的了,解决办法只需要把编码统一下即可解决了. 此种情况最大的可能就是客户端 ...