哥德巴赫猜想:

任一大于2的偶数,都可表示成两个素数之和。

任一大于5的整数都可写成三个质数之和。

贪心取尽可能大的素数.....

C. Prime Swaps
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 "i j"
(1 ≤ i < j ≤ n; (j - i + 1) is
a prime).

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

Sample test(s)
input
  1. 3
  2. 3 2 1
output
  1. 1
  2. 1 3
input
  1. 2
  2. 1 2
output
  1. 0
input
  1. 4
  2. 4 2 3 1
output
  1. 3
  2. 2 4
  3. 1 2
  4. 2 4

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int maxn=100100;
  9.  
  10. bool vis[maxn];
  11. int prime[maxn/10],pn;
  12. int n,a[maxn],b[maxn];
  13. int Left[maxn*5],Right[maxn*5],nu;
  14.  
  15. void get_prime()
  16. {
  17. for(int i=2;i*i<maxn;i++)
  18. {
  19. for(int j=i*2;j<maxn;j+=i)
  20. {
  21. vis[j]=1;
  22. }
  23. }
  24. for(int i=2;i<maxn;i++)
  25. {
  26. if(vis[i]==0)
  27. prime[pn++]=i;
  28. }
  29. }
  30.  
  31. int Bin(int x)
  32. {
  33. int low=0,high=pn-1,mid,ans=-1;
  34. while(low<=high)
  35. {
  36. mid=(low+high)/2;
  37. if(prime[mid]<=x)
  38. {
  39. ans=prime[mid],low=mid+1;
  40. }
  41. else high=mid-1;
  42. }
  43. return ans;
  44. }
  45.  
  46. void debug()
  47. {
  48. cout<<"a....\n"; for(int i=1;i<=n;i++) cout<<a[i]<<","; cout<<endl;
  49. cout<<"b....\n"; for(int i=1;i<=n;i++) cout<<b[i]<<","; cout<<endl;
  50. }
  51.  
  52. int main()
  53. {
  54. get_prime();
  55. scanf("%d",&n);
  56. for(int i=1;i<=n;i++)
  57. {
  58. scanf("%d",a+i);
  59. b[a[i]]=i;
  60. }
  61. for(int i=1;i<=n;i++)
  62. {
  63. /// from b[i] to i greedy!!!
  64. int len=b[i]-i+1;
  65. while(len!=1)
  66. {
  67. int bin=Bin(len);
  68. int cp=b[i]-bin+1; /// changepos
  69. Left[nu]=cp,Right[nu]=b[i]; nu++;
  70. int t1=b[i],t2=b[a[cp]];
  71. swap(a[cp],a[b[i]]);
  72. b[a[b[i]]]=t1;b[i]=t2;
  73. // debug(); getchar();
  74. len=b[i]-i+1;
  75. }
  76. }
  77. printf("%d\n",nu);
  78. for(int i=0;i<nu;i++)
  79. {
  80. printf("%d %d\n",Left[i],Right[i]);
  81. }
  82. return 0;
  83. }

版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss

Codefoces 432 C. Prime Swaps的更多相关文章

  1. Codefoces 432 C. Prime Swaps(水)

    思路:从前往后想将1调整好,在调整2....这样平均每次有五次机会调整,并且有相当一部分可能都用不到五次,能够一试.ac 代码: #include<iostream> #include&l ...

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

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

  3. CodeForces 432C Prime Swaps

    Description You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your ...

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

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

  5. codeforces C. Prime Swaps

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

  6. Prime Factory

    Your task is simple:Find the first two primes above 1 million, whose separate digit sums are also pr ...

  7. Codeforces #432 Div2 D

    #432 Div2 D 题意 给出一些数字,如果这些数字的的 \(gcd\) 不为1则称这些数字 \(good\). 可以有两种操作: 花费 x 删掉一个数 花费 y 将一个数加 1 问使这些数 \( ...

  8. POJ 3126:Prime Path

    Prime Path Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit St ...

  9. Java 素数 prime numbers-LeetCode 204

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

随机推荐

  1. Cocos2d-x3.1 粒子效果演示样例

    这里把粒子的几种效果粘出来,以便以后使用 原文地址:http://blog.csdn.net/qqmcy/article/details/37511259 // // IntervalLayer.cp ...

  2. OCP读书笔记(20) - 复制数据库

    没有连接到target的复制 将orcl数据库的备份复制为orcl1 一.创建orcl的备份: run{ backup database plus archivelog;} 二.复制数据库为orcl1 ...

  3. 《炉石传说》建筑设计欣赏(7):采用Google.ProtocolBuffers处理网络消息

    这一次,琢磨了一下Unity3D网络游戏发展的网络信息处理.服务器的网络游戏一般都是自主研发,因此,相应的网络消息处理应该培养自己.client/现在使用的邮件服务器之间的价差JSON和Google. ...

  4. JSP生成word文件

    1.jsp生成word文件,直接改动jsp格式: <%@ page contentType="application/vnd.ms-word;charset=GB2312"% ...

  5. 设计模式——辛格尔顿(Singleton)

    要想正确理解设计模式,首先必须明白它是为了解决什么问题而提出来的. 设计模式学习笔记 --Shulin 转载请注明出处:http://blog.csdn.net/zhshulin 单例模式属于设计模式 ...

  6. Type mismatch: cannot convert from Enumeration&lt;String&gt; to Enumeration&lt;Object&gt;

    完整的错误信息: Description Resource Path Location TypeType mismatch: cannot convert from Enumeration<St ...

  7. centos6.4设备hadoop-2.5.1(完全分布式)

    环境介绍: 在这两种装备centos6.4(32位置)的server安装Hadoop-2.5.1分布式集群(2台机器,主要试验用.哈哈). 1.改动主机名和/etc/hosts文件 1)改动主机名(非 ...

  8. 高级项目 它 集群环境建设(两)MySQL簇

    最后博文我们介绍一下相关概念集群,今天我们要介绍的博文MySQL相关内容集群. 1.MySQL集群简单介绍 MySQL群集技术在分布式系统中为MySQL数据提供了冗余特性,增强了安全性,使得单个MyS ...

  9. ajax j跨域请求sonp

    需求 遇到的问题 解决方案 需求 如今,该项目需要获得数据访问外部链接.它是跨域.使用ajax 直提示: 遇到的问题 1. 怎样使用ajax 跨域请求数据 2. 能不能post请求 解决的方法 经过网 ...

  10. 转: 第二章 IoC Annotation注入

    http://blog.csdn.net/p_3er/article/details/9231307 1.命名空间 使用Annotation的方式,需要在spring的配置文件中配置命名空间.命名空间 ...