哥德巴赫猜想:

任一大于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
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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=100100; bool vis[maxn];
int prime[maxn/10],pn;
int n,a[maxn],b[maxn];
int Left[maxn*5],Right[maxn*5],nu; void get_prime()
{
for(int i=2;i*i<maxn;i++)
{
for(int j=i*2;j<maxn;j+=i)
{
vis[j]=1;
}
}
for(int i=2;i<maxn;i++)
{
if(vis[i]==0)
prime[pn++]=i;
}
} int Bin(int x)
{
int low=0,high=pn-1,mid,ans=-1;
while(low<=high)
{
mid=(low+high)/2;
if(prime[mid]<=x)
{
ans=prime[mid],low=mid+1;
}
else high=mid-1;
}
return ans;
} void debug()
{
cout<<"a....\n"; for(int i=1;i<=n;i++) cout<<a[i]<<","; cout<<endl;
cout<<"b....\n"; for(int i=1;i<=n;i++) cout<<b[i]<<","; cout<<endl;
} int main()
{
get_prime();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
b[a[i]]=i;
}
for(int i=1;i<=n;i++)
{
/// from b[i] to i greedy!!!
int len=b[i]-i+1;
while(len!=1)
{
int bin=Bin(len);
int cp=b[i]-bin+1; /// changepos
Left[nu]=cp,Right[nu]=b[i]; nu++;
int t1=b[i],t2=b[a[cp]];
swap(a[cp],a[b[i]]);
b[a[b[i]]]=t1;b[i]=t2;
// debug(); getchar();
len=b[i]-i+1;
}
}
printf("%d\n",nu);
for(int i=0;i<nu;i++)
{
printf("%d %d\n",Left[i],Right[i]);
}
return 0;
}

版权声明:来自: 代码代码猿猿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. 再探vim经常使用命令

     最開始学习过vim,见 http://blog.csdn.net/u011848617/article/details/12837873 之后以前不了了之,当再次学习后,发现经常使用命令的掌握还 ...

  2. 分布式消息系统Jafka入门指南之二

    分布式消息系统Jafka入门指南之二 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 三.Jafka的文件夹结构 1.安装tree命令 $ sudo yu ...

  3. JS 数字 、中文、 英文、判断

    <pre name="code" class="html">单独的验证: 利用正则表达式限制网页表单里的文本框输入内容: 用正则表达式限制只能输入中 ...

  4. spring4.1+springmvc4.1+mybatis3.2.8+spring-security3.2.5集成环境建设

    在最近使用的项目ssi+spring-security 结构体.建立你自己的家,这是什么环境. 只有记录的目的. 项目结构: 类文件:                                  ...

  5. 64地点 Windows 8/7 根据系统 32地点PLSQL 耦合 64 地点 Oracle 11g

    64地点 Windows 8/7 根据系统 32地点PL/SQL 耦合 64 地点 Oracle 11g     说明:安装后Oracle的 oci.dll 是64位的,而32位应用程序 PL/SQL ...

  6. 第五章_JSTL

    5.1.下载JSTL http://jstl.java.net 5.2.JSTL类库 类别 下属功能 URI 前缀 Core 变量支持 http://java.sun.com/jsp/jstl/cor ...

  7. 判断DAG图

    拓扑排序O(E), bellman O(VE)   , 使用邻接表的dfs O(V+E) ,floyd O(N*N*N) bellman算法只能判断是否存在负环. 所以可以先把权值全部设为-1 #in ...

  8. JS多语种方式

    方案: 在不同的移动平台(IOS.Android)上,并建立了HTML页面通信框架.主要业务逻辑HTML发展:我要支持多语言开发. 动机: 通过积极主动的信息方式,前一页完成初始化,获取当前语言选项. ...

  9. [C++] WinAES问题

    WinAES这是一个很好windows CAPI包. 假设C++项目需求和java程序aes加密和通信的解密,然后WinAES代码是有问题. java的aes默认代码未设置IV和用途ECB模式. 因此 ...

  10. 【原创】shadowebdict开发日记:基于linux的简明英汉字典(三)

    全系列目录: [原创]shadowebdict开发日记:基于linux的简明英汉字典(一) [原创]shadowebdict开发日记:基于linux的简明英汉字典(二) [原创]shadowebdic ...