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
题目大意:p是一个置换,要求将一个序列分成若干个环,大小为a或b.(不是很好说......),给出p.
分析:枚举一下a的系数,可以推测出b的系数,如果ax + by = n没有非负整数解,那么就输出-1,否则一个一个环直接构造就好了.
#include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cstdio> int n,a,b; int cnta,cntb; int flasttg=false; int main() { scanf("%d%d%d",&n,&a,&b); for (int i = 0; i * a <= n; i++) { int t = n - a * i; if (t % b == 0) { cnta = i; cntb = t / b; flasttg = true; break; } } int pos=1; int lastt=pos; if(!flasttg) printf("-1\n"); else { for(int i = 1;i <= cnta;i++) { for(int k = 1;k < a;k++) printf("%d ",++pos); printf("%d ",lastt); pos++; lastt=pos; } for(int i = 1;i <= cntb;i++) { for(int k = 1;k < b;k++) printf("%d ",++pos); printf("%d ",lastt); pos++; lastt=pos; } } return 0; }