版权声明:本文为博主原创文章,未经博主同意不得转载。

https://blog.csdn.net/u011328934/article/details/26094917

题目连接:Codefoces 432C Prime Swaps

题目大意:给出一个序列。长度为n,要求用5n以内的交换次数使得序列有序。而且交换的i,j两个位置的数时要满足,j−i+1为素数。

解题思路:a数组为相应的序列,b数组为相应的有序序列,p为相应数的位置。每次从有序序列最小的位置開始,该为必须放b[i]才对。所以p[b[i]]=i,否则就要将b[i]尽量往前换,直到换到i的位置为止。

哥德巴赫猜想:不论什么一个大于5的数都能够写成三个质数之和。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 1e5+5; int n, a[N], b[N], p[N], v[N], r[5*N][2]; void init () {
memset(v, 0, sizeof(v)); for (int i = 2; i <= n; i++) {
if (v[i])
continue; for (int j = i * 2; j <= n; j += i)
v[j] = 1;
} for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
p[a[i]] = i;
}
sort(b+1, b+n+1);
} int solve () {
int c = 0; for (int i = 1; i <= n; i++) {
while (p[b[i]] != i) {
for (int j = i; j < p[b[i]]; j++) {
if (!v[p[b[i]] - j + 1]) {
r[c][1] = p[b[i]];
r[c++][0] = j; int t = p[b[i]];
p[b[i]] = j;
p[a[j]] = t;
swap(a[j], a[t]);
break;
}
}
}
}
return c;
} int main () {
scanf("%d", &n);
init();
int c = solve(); printf("%d\n", c);
for (int i = 0; i < c; i++)
printf("%d %d\n", r[i][0], r[i][1]);
return 0;
}

Codefoces 432C Prime Swaps(数论+贪心)的更多相关文章

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

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

  2. CodeForces 432C Prime Swaps

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

  3. CodeForces 124C Prime Permutation (数论+贪心)

    题意:给定一个字符串,问你能不能通过重排,使得任意一个素数p <= 字符串长度n,并且 任意的 i <= 长度n/素数p,满足s[p] == s[p*i]. 析:很容易能够看出来,只要是某 ...

  4. Codefoces 432 C. Prime Swaps

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

  5. Tsinsen A1504. Book(王迪) 数论,贪心

    题目:http://www.tsinsen.com/A1504 A1504. Book(王迪) 时间限制:1.0s   内存限制:256.0MB   Special Judge 总提交次数:359   ...

  6. Codefoces 432 C. Prime Swaps(水)

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

  7. 【HDU】2866:Special Prime【数论】

    Special Prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. codeforces 680C C. Bear and Prime 100(数论)

    题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...

  9. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

随机推荐

  1. plsql与64位的Oracle关联方法

    在这里吐槽一下,plsql居然木有64位的,以前居然不知道,好久没用Oracle了,想练习一下,方法如下: 1.安装Oracle,官网都有,这里不细说了,我选的是64的Oracle安装的. 2.下载p ...

  2. Eclipse 中修改tomcat设置内存大小

    修改1: 在Eclipse中下面Servers双击Tomcat Server... 然后点击General InformAtion 下的Open launch configuration: 会弹出Ed ...

  3. python 守护进程,监控进程

    守护进程代码: import time, os import subprocess def run(): while True: taskList = os.popen('tasklist').rea ...

  4. python-docx 设置标题heading的中文字体类型+设置正文的中文字体类型

    依赖包: from docx import Document from docx.shared import Pt from docx.shared import Inches from docx.o ...

  5. php info

    http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/08/12/1797753.html  php pdo 相关 http://blog.cs ...

  6. [原]openstack-kilo--issue(二十一) instance can't get ip 虚拟机不能得到ip(2)

    ===问题点==== 在使用vlan模式部署compute节点的时候出现了下面的错误:在controller节点的dhcp-agent.log中 2017-01-22 20:19:34.178 241 ...

  7. Unity Editor工具-代码里复制Component

    //CopyComponent ublic static T CopyComponent<T>(T original, GameObject destination) where T : ...

  8. Flask web开发之路七

    今天写SQLAlchemy数据库 首先介绍ORM的概念: ORM,Object类,Relationship:关系,Mapping:映射,也就是模型关系映射 flask-sqlalchemy是一套ORM ...

  9. acm入门练习心得

    A题  两数相加等于n相乘等于m 问是否存在两数 写了超级久没有写出来,因为我的思路是 把m的因子都找出来,加加看,看等不等于n.后来发现m的因子还要考虑负数的情况,还要考虑m是不是负数的情况.太过麻 ...

  10. CodeForces 1099F - Cookies - [DFS+博弈+线段树]

    题目链接:https://codeforces.com/problemset/problem/1099/F Mitya and Vasya are playing an interesting gam ...