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

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. 仿迅雷播放器教程 -- duilib界面(13)

    经过了这么多篇文章的讲解,相信大家也对界面库有一定了解了,用一个新的界面库,肯定要对它进行全方位考察.鉴于公司目前所有的产品都是MFC做的,全部转换成duilib肯定不现实,并且公司的很多项目逻辑和界 ...

  2. Ubuntu中apt与apt-get命令的区别

    https://blog.csdn.net/taotongning/article/details/82320472

  3. [IR] Suffix Trees and Suffix Arrays

    前缀树 匹配前缀字符串是不言自明的道理. 1. 字符串的快速检索 2. 最长公共前缀(LCP) 等等 树的压缩 后缀树 Let s=abab, a suffix tree of s is a comp ...

  4. 05原型模式Prototype

    一.什么是原型模式 Prototype模式是一种对象创建型模式,它采 取复制原型对象的方法来创建对象的实例.使用 Prototype模式创建的实例,具有与原型一样的 数据. 二.原型模式的特点 1. ...

  5. ng4.0 生命周期

    名称 时机 接口 范围 ngOnChanges 当被绑定的输入属性的值发生变化时调用,首次调用一定会发生在 ngOnInit之前. OnChanges 指令和组件 ngOnInit 在第一轮 ngOn ...

  6. Unity3D 批处理场景的工具

    //场景的批量处理器 public static class OperateScene { public const string SceneDir = "Assets/Scene/&quo ...

  7. A - Cable master

    Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Commi ...

  8. css学习_文本有关的样式属性、sublime快捷生成标签

    1.css中color定义文本的颜色 写法:(最常用的是16进制的) 2.行间距    line-height 3.水平对齐方式   text-align left right  center 4.首 ...

  9. 用C# 7.0的switch...case模式匹配取代一堆if语句

    今天在重构代码时对下面的一堆if语句实在看着不顺眼. if(activation == null) { _logger.LogError("x1"); return Boolean ...

  10. 游标SQL Cursor 基本用法

    http://www.cnblogs.com/Gavinzhao/archive/2010/07/14/1777644.html 1 table1结构如下 2 id    int 3 name  va ...