C. Permutation Cycle
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
这题就是求 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的更多相关文章
- Codeforces 932.C Permutation Cycle
C. Permutation Cycle time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CF932C Permutation Cycle
思路: 构造. 实现: #include <bits/stdc++.h> using namespace std; ]; int main() { int n, a, b; while ( ...
- 【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 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 ...
- Codeforces Round #463
A - Palindromic Supersequence /* 题目大意:给出一个串,构造一个包含该串的回文串 */ #include <cstdio> #include <alg ...
- 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 ...
- 置换及Pólya定理
听大佬们说了这么久Pólya定理,终于有时间把这个定理学习一下了. 置换(permutation)简单来说就是一个(全)排列,比如 \(1,2,3,4\) 的一个置换为 \(3,1,2,4\).一般地 ...
- (转)排列算法 Permutation Generation
转自:http://www.cnblogs.com/dragonpig/archive/2010/01/21/1653680.html http://www.notesandreviews.com/p ...
- 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/ ...
随机推荐
- 学习html/css基础的重点笔记
1.权重问题 内联样式表 > id选择符 > class选择符 > 类型选择符(所有html标签名称) 内联样式表 > 内部样式表.外部样式表 内部样式表.外部样式表的权重与书 ...
- ViewPager实现Recycle机制和响应notifyDataSetChanged
1.目标 主界面要求水平移动翻页效果,每次只能翻一页,可以翻无数页. 2.实现思路 针对"每次只能翻一页"这个要求,简单使用SDK的话只有用ViewPager.ViewPager的 ...
- 学习笔记(4)——实验室集群管理结点IP配置
经过验证,集群管理结点mgt的IP配置应为如下所示: [root@mgt zmq]# ifconfig//外部网卡 eth0 Link encap:Ethernet HWaddr 5C:F3:FC:E ...
- IDEA报错:Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. ('crmWatcherService'错误)
单表插入项目,插入前正常,插入后运行webapplication报错: run: debug: 于webapplication报错: Injection of autowired dependenci ...
- Android 编程下的 TraceView 简介及其案例实战
TraceView 是 Android 平台配备一个很好的性能分析的工具.它可以通过图形化的方式让我们了解我们要跟踪的程序的性能,并且能具体到 method.详细内容参考:Profiling with ...
- 常用的第三方模块 Pillow url
Pillow PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 由于PIL仅支持到Python 2.7 ...
- Linux find查找指定文件 按照名称 然后cp拷贝到指定目录且指定文件名
最近有一个需求,需要将指定目录下的文件(已知文件名)复制到另一个指定的目录且重命名文件. 要求: 在var目录下会定义系统的启动日志相关信息,请查找对应的boot.log文件,并把它备份一份到var/ ...
- MySQL 性能监控4大指标——第二部分
[编者按]本文作者为 John Matson,主要介绍 mysql 性能监控应该关注的4大指标. 第一部分介绍了前两个指标:查询吞吐量与查询执行性能.本文将继续介绍另两个指标:MySQL 连接与缓冲池 ...
- SecureCRT使用问题记录
1.破解版下载&安装 参考:https://bbs.feng.com/read-htm-tid-6939481.html 2.session导入 查看 SecureCRT-Preference ...
- ABP(ASP.NET Boilerplate Project)框架探讨
从官网上下载下来带Module-Zero的abp框架. vs2015打开解决方案. 首先让系统run起来.把webconfig数据库连接改一下.启动程序. 发现报错:“本地语言指定”的错误,之后运行n ...