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. 实施软件测试风险分析&回归用例刷选

    [一两年前收集整理的资料,感觉不错,放到博客上来] 作为软件测试计划的一部分,软件测试风险的分析与控制是其中重要的环节.如果前期风险分析与控制比较充分,那么会使软件的测试成功性大大增加,且可将由风险异 ...

  2. jenkins - jsp或其他web样式无法展示

    背景 因为jenkins安全性的问题,默认加载出来的报告是无css的,通过以下配置解决:HTML Publisher Plugin 插件在新的Jenkins版本中会导致打开的网页中无法加载CSS以及无 ...

  3. 聊一聊FE面试那些事

    聊一聊FE面试那些事 最近公司由于业务的扩展.技术的延伸需要招一批有能力的小伙伴加入,而我有幸担任"技术面试官"的角色前前后后面试了不下50多位候选人,如同见证了50多位前端开发者 ...

  4. (转)log4j(四)——如何控制不同风格的日志信息的输出?

    一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 1 老规矩,先来个栗子,然后再聊聊感受 import org.apache.log4j.*; //by godtrue p ...

  5. (转)Java线程:线程的同步与锁

      Java线程:线程的同步与锁       一.同步问题提出   线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏. 例如:两个线程ThreadA.ThreadB都操作同一个对象Fo ...

  6. Linux安装搜狗输入法教程

    最近开始学习linux 在安装输入法中遇到的一些问题,最终成功安装,也得益于网络上的前辈写的文章,现在将全部安装步骤以及遇到的一些问题总结如下:   基本上分三步走 1,添加fcitx的键盘输入法系统 ...

  7. ubuntu解压乱码

    乱码原因 问题一般出现在windows下压缩的在ubuntu中会出现这种情况. 其实就是windows和ubuntu下压缩的编码格式不同.windows下的编码格式为GBK,Ubuntu下的为UTF- ...

  8. 12. leetcode 455.Assign Cookies

    Assume you are an awesome parent and want to give your children some cookies. But, you should give e ...

  9. C#字符串格式化(摘抄的,留下来用用)

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...

  10. idea如何添加外部jar包

    假设我们要将G:\ModuleAPI_Java_2.2.0.0 .jar导入工程中: 首先,在mvn命令行执行下面命令: mvn install:install-file -Dfile=G:\Modu ...