Description

You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times):

  • choose two indexes, i and j (1 ≤ i < j ≤ n; (j - i + 1) is a prime number);
  • swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable).

You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations.

Input

The first line contains integer n(1 ≤ n ≤ 105). The next line contains n distinct integers a[1], a[2], ..., a[n](1 ≤ a[i] ≤ n).

Output

In the first line, print integer k(0 ≤ k ≤ 5n) — the number of used operations. Next, print the operations. Each operation must be printed as "ij" (1 ≤ i < j ≤ n; (j - i + 1) is a prime).

If there are multiple answers, you can print any of them.

Sample Input

Input
3
3 2 1
Output
1
1 3
Input
2
1 2
Output
0
Input
4
4 2 3 1
Output
3
2 4
1 2
2 4 题目大意:有n个数的序列,通过交换使其变得有序,交换的原则是每次交换的数字ai和aj,(j-i+1)必须是质数,要求在5n步内完成。
思路:很容易考虑到歌德巴赫猜想。该猜想虽未证明,不过科学家目前还未找出反例,在本题数据范围有限大的情况下是适用的。由猜想可得,每个大于等于5的数都可以有三个质数相加获得,而2,3都是质数,4=2+2,所以所有大于等于2的数都可以用质数表示。所以无论i,j多少,每次交换i,j都可以在三步之内获得。已知把一个无序数列变成有序数列最多需要交换n-1次,所以答案小于等于3(n-1),小于等于5n。
/*
* Author: Joshua
* Created Time: 2014年07月20日 星期日 20时16分13秒
* File Name: c.cpp
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100005
int a[maxn],l[maxn<<],r[maxn<<];
bool f[maxn];
int n,ans;
void primeNumber()
{
memset(f,true,sizeof(f));
f[]=f[]=false;
for (int i=;i<maxn;++i)
if (f[i])
for (int j=i+i;j<maxn;j+=i)
f[j]=false;
} void change(int x,int y)
{
if (x==y) return;
if (x>y) swap(x,y);
for (int i=y;i>x;i--)
if (f[i-x+])
{
swap(a[i],a[x]);
l[++ans]=x;
r[ans]=i;
change(i,y);
break;
}
} void solve()
{
ans=;
for (int i=;i<=n;++i)
scanf("%d",&a[i]);
for (int i=;i<=n;++i)
while (a[i]!=i) change(i,a[i]);
printf("%d\n",ans);
for (int i=;i<=ans;++i)
printf("%d %d\n",l[i],r[i]);
}
int main()
{ primeNumber();
while (scanf("%d",&n)==)
solve(); return ;
}

CodeForces 432C Prime Swaps的更多相关文章

  1. Codefoces 432C Prime Swaps(数论+贪心)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...

  2. codeforces C. Prime Swaps

    题意:给你n个数,然后在交换次数小于等于5×n的情况下使得这个序列变成升序,输出次数; 思路:哥德巴赫猜想:任何一个大于5的数都可以写成三个质数之和.尽可能的找大的素数,从1的位置向右逐步的调整,每一 ...

  3. Codefoces 432 C. Prime Swaps

    哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...

  4. Codeforces H. Prime Gift(折半枚举二分)

    题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces Round #246 (Div. 2) C. Prime Swaps(贪心,数论)

    题目链接:http://codeforces.com/contest/432/problem/C 首先由题意分析出:这些数是从1到n且各不相同,所以最后结果肯定是第i位的数就是i. 采用这样一种贪心策 ...

  6. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  7. [Codeforces 1178D]Prime Graph (思维+数学)

    Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是 ...

  8. Codeforces 912E - Prime Gift

    912E - Prime Gift 思路: 折半枚举+二分check 将素数分成两个集合(最好按奇偶位置来,保证两集合个数相近),这样每个集合枚举出来的小于1e18的积个数小于1e6. 然后二分答案, ...

  9. Codeforces 912E Prime Gift(预处理 + 双指针 + 二分答案)

    题目链接 Prime Gift 题意  给定一个素数集合,求第k小的数,满足这个数的所有质因子集合为给定的集合的子集. 保证答案不超过$10^{18}$ 考虑二分答案. 根据折半的思想,首先我们把这个 ...

随机推荐

  1. 使用xcrun打包iOS应用

    使用xcrun打包iOS应用 通常打包采用xcodebuild和xcrun两个命令,xcodebuild负责编译,xcrun负责将app打成ipa.   XCode 默认编译出来的是appName.a ...

  2. jmeter 单接口测试方案(接口无业务关联)

    前言 前面开了一篇讲了Jenkins+jmeter+ant的使用,但没有说到具体怎么投入到项目使用,主要介绍了接口测试定义,流程和环境部署,所以我今天要说的就是我是怎么将这个方案投入到实际中使用的.这 ...

  3. (转)面试大总结之一:Java搞定面试中的链表题目

    面试大总结之一:Java搞定面试中的链表题目 分类: Algorithm Interview2013-11-16 05:53 11628人阅读 评论(40) 收藏 举报 链表是面试中常出现的一类题目, ...

  4. (转)搬瓦工(bandwagonhost)后台管理VPS

    1. Bandwagonghost使用建议 购买了搬瓦工(bandwagonhost)的VPS,如何使用呢? 首先插几句使用建议,老高认为十分重要,为什么呢?搬瓦工如果监控到有大量的垃圾信息从我们的主 ...

  5. CSS样式----盒子模型(图文详解)

    盒子模型 盒子中的区域 一个盒子中主要的属性就5个:width.height.padding.border.margin.如下: width:内容的宽度.CSS中 width 指的是内容的宽度,而不是 ...

  6. spring boot 整合mybatis + swagger2

    之前使用springMVC+spring+mybatis,总是被一些繁琐的xml配置,有时候如果配置出错,还要检查各种xml配置,偶然接触到了spring boot 后发现搭建一个web项目真的是1分 ...

  7. 简易 HTTP Server 实现(JAVA)

    该简易的J2EE WEB容器缺失很多功能,却可以提供给大家学习HTTP容器大致流程. 注:容器功能很少,只供学习. 1. 支持静态内容与Servlet,不支持JSP 2. 仅支持304/404 3. ...

  8. python-冒泡排序,升序、降序

    冒泡排序 这个算法的名字由来是因为越大的元素会经交换慢慢浮'到数列的顶端. 冒泡排序的基本思想:重复走访要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来,直到没有再需要交换,完成排序 ...

  9. python 密码学编程

    最近在看一本书.名字是 python密码学编程.在此做一些笔记,同时也为有需要的人提供一些参考. *************************************************** ...

  10. 到底什么样的企业才适合实施SAP系统?

    SAP系统作为全宇宙第一的ERP,号称世界500强里面有80%的企业部署了SAP系统,总部位于德国沃尔多夫市,在全球拥有6万多名员工,遍布全球130个国家,并拥有覆盖全球11,500家企业的合作伙伴网 ...