Codeforces 432C (ingenious application of Goldbach conjecture) __ Algorithm design

Source: Internet
Author: User

C.prime swps time limit/test 2 seconds memory limit per test 256 megabytes input standard input output standard output

You are 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 the following operation (your 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, your are allowed to apply the following sequence of assignments:tmp = A[i], a[i] = A[j], a[j] = TMP (TMP i s a temporary variable).

You don't need to minimize the number of used operations. However, you are need to make sure this there are at most 5n operations. Input

The The 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, print integer k (0≤k≤5n)-the number of used operations. Next, print the operations. Each operation must is printed as "I J" (1≤i < j≤n; (J-i + 1) is a prime).

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

Sample input

3

3 2 1

Sample output

1

1 3


Sample input

2

1 2

Sample output

0


Sample input

4

4 2 3 1

Sample output

3

2 4

1 2

2 4

Address: Http://codeforces.com/problemset/problem/432/C


Solution:


The subject is roughly, set series A,ai=i (1=<i<=n), namely 1,2,3......N. For a, the author is very bad, the order of the sequence is disturbed, but did not change the number of numbers in the series, but only a number of times and the exchange of numbers, now requires you in the limited conditions to make this series of a.
The qualified condition is: each can choose two position I J, the request j-i+1 must be the prime number, will be in the series I j position the quantity to exchange, can only carry on the 5*n time, n is this number of elements of this sequence.
Not required to make the minimum number of operations, only to find a procedure, so that the series can be reduced to a.


It's obvious to us that the numbers in this series are from 1 to N, and that each element is unique, we start with 1, all the way to N, we do a number of operations each time, and then we restore that number to where it should be. That is, to restore each number I to this position


And obviously, if I was already in the position of I, there would be no need to restore it. At the same time, since we are restoring from 1 to N, each time this number i is either in the position of I or in the position behind I. It is not possible to position in front of I because I have guaranteed that the number in front of I has been restored to its rightful place.

For this number I, if it is not in the position of I, then it must be in the position behind I. So, can we find a few positions, as the intermediate springboard, to restore I to the springboard and then back to where it should be? In fact, it is possible, but how to do it. How do you choose these positions?

According to the requirements of the topic, if we select the number of springboard each time does not exceed 4, then each number I restore to I this position requires no more than 5 steps, the total step is not more than 5*n

First give an incorrect idea, if we find that the number I is not in this position, and can not be directly exchanged, I and the number of the front to exchange, and then loop to exchange. This is a big, messy sequence, and the procedure is likely to be more than 5*n.



So, here we can use the Goldbach conjecture.

Goldbach conjecture that the most famous expression of modern times is the following two points:
1. For any integer greater than 5, it can be expressed as a number of 3 primes.
2. For any even number greater than 2, it can be expressed as a number of 2 primes.


Set P1,p3,p4,p2,d1,d2,d3,x

P1 represents the position to which the number of x is needed, p2 represents the current position of X, P3,P4 is the position of two springboard.
D1 is P1 to P3 distance +1,d2 is p3 to P4 distance +1,d3 is P4 to P2 distance +1
Guarantee 1<=p1<p3<p4<p2<=n, then

D1=p3-p1+1
D2=p4-p3+1
D3=p2-p4+1
Sum=d1+d2+d3=p2-p1+3

Now for an integer sum, we want to find 3 prime d1,d2,d3 so that sum=d1+d2+d3 can
Note that if p2-p1=1 or p2-p1=2, then p2-p1+1=2 is prime, and p3-p2+1=3 is prime, it can be exchanged directly.
So p2-p1>=3, sum>=6, just satisfies the Goldbach conjecture that the condition of the first expression is greater than 5
At the same time, sum we can find by known p1,p2, for this sum, if an even number, then can be split into sum=2+d, if it is odd, that is sum=3+d

This d must be an even number and d>2, which satisfies the condition that the Goldbach conjecture is greater than 2 in the second statement.
Then we simply require that all even numbers in 1-100000 can be represented by two d1,d2, while d3=2 or 3
This way, through

D1=p3-p1+1
D2=p4-p3+1
D3=p2-p4+1

We can find the P3,P4, we can know the exchange operation.
The number of exchanges here is otherwise equal to 0, or equal to 3, to meet the total operating procedures <=5*n



Finally mention, sometimes we might as well stand in the perspective of the author to think about the problem.
This way you can guess whether the nominal results are correct function, help to think.

Obviously, this topic requires the output of an operating procedure, the author must have a function, this function is used to simulate the output of your program operation steps. That is, according to your operating procedures, the input of a given number of sequences to restore, and finally determine whether the number of restored sequence a,ai=i (1=<i<=n) This condition can be.


The code is as follows:


Statement: The code contains personal style, if there is ambiguity, please understand, in addition, please respect the personal copyright.

Hello.
I ' m Peter. #include <cstdio> #include <iostream> #include <sstream> #include <iomanip> #include < cstring> #include <string> #include <cmath> #include <cstdlib> #include <algorithm> #include <functional> #include <cctype> #include <ctime> #include <stack> #include <queue> # include<deque> #include <vector> #include <set> #include <map> #include <limits> using
namespace Std; #define ILOVEU cout<< "I love u" <<endl #define INPUT freopen ("Data.txt", "R", stdin) #define OUTPUT freopen (" Output.txt "," w ", stdout) #define INT (0x3f3f3f3f) *2 #define LL (0x3f3f3f3f3f3f3f3f) *2 #define LEN (a) (INT) strlen (a) # Define CLR (a) memset (A,0,sizeof (a)) #define CLR_MINUS1 (a) memset (A,-1,sizeof (a)) #define CLR_INT (a) memset (A,int, sizeof (a)) #define CLR_TRUE (a) memset (A,true,sizeof (a)) #define CLR_FALSE (a) memset (A,false,sizeof (a)) #define Clr_ Queue (q) while (!q.empty ()) Q.pop () #define CLR_STACK (s) WHIle (!s.empty ()) S.pop () #define REP (I, A, b) for (int i = A; I < b; i++) #define DEP (I, A, b) for (int i = A; i > b; i--) #define Repin (I, A, b) for (int i = A; I <= b; i++) #define Depin (I, A, b) for (int i = A; I >= b; i--) #defin
e ll long long #define EPS 1e-9 #define MOD #define MAXN 100100 #define N #define M-BOOL isprime[maxn+m];//Determine whether a number is a prime
int prime[maxn+m];//The first one is which primes int num_prime;
    void Get_prime ()//sieve method to calculate prime number {clr_true (isprime);
    Isprime[0]=isprime[1]=false;
    num_prime=0;
            Repin (I,2,MAXN) {if (Isprime[i]) {num_prime++;
            Prime[num_prime]=i;
                Repin (J,2,MAXN) {if (I*J&GT;MAXN) break;
            Isprime[i*j]=false; struct twopirmes//each even number greater than 2 can be represented as two primes and {int a,b;}
TWOPRIME[MAXN+M];
    void Get_twoprimes ()//finds the sum of each even number greater than 2, and {int i,t1,t2,t,tail;
        for (i=4;i<=maxn;i+=2) {T=I/2; tail= (int) (Lower_bound prime+1,prime+1+num_prime,t)-prime);
            while (1) {T1=prime[tail];
            T2=I-T1;
                if (Isprime[t1] && isprime[t2]) {twoprime[i].a=t1;
                Twoprime[i].b=t2;
            Break
        } tail--; and the int a[maxn+m],pos[maxn+m];//a represents the sequence, and the POS indicates where the number is now struct answer//storage result {int i,j;}
ANS[5*MAXN];
int Num_ans;
    void Swapswap (int p1,int p2)//to exchange the factor of two numbers in the number of P1 and P2 in the sequence, by the way {int T1=A[P1];
    int T2=A[P2];
    Swap (A[P1],A[P2]);
    Swap (pos[t1],pos[t2]);
    num_ans++;
    ANS[NUM_ANS].I=P1;
    ANS[NUM_ANS].J=P2;
if (ANS[NUM_ANS].I&GT;ANS[NUM_ANS].J) swap (ANS[NUM_ANS].I,ANS[NUM_ANS].J);
    void resume (int x)//restores the given number of columns to {int p1=x;
    int p2=pos[x];
    if (P1==P2) return;
    P1&LT;P2 int d=p2-p1+1;
    if (Isprime[d]) Swapswap (P1,P2);
        else {//can not be directly exchanged, using Goldbach conjecture to find the springboard int sum=p2-p1+3;
        int d1,d2,d3,p3,p4,t; if (sum%2) d3=3,t=sum-3;
        else d3=2,t=sum-2;
        P4=P2+1-D3;
        D1=TWOPRIME[T].A;
        d2=twoprime[t].b;
        P3=d1+p1-1;
        Swapswap (P2,P4);
        Swapswap (P4,P3);
    Swapswap (P3,P1);
    int main () {get_prime ()};
    Get_twoprimes ();
    int n,t;
    cin>>n;
        Repin (i,1,n) {scanf ("%d", &t);
        a[i]=t;
    Pos[t]=i;
    } num_ans=0;
    Repin (i,1,n) {resume (i);
    } cout<<num_ans<<endl;
        Repin (I,1,num_ans) {if (ANS[I].I&GT;ANS[I].J) swap (ANS[I].I,ANS[I].J);
    printf ("%d%d\n", ANS[I].I,ANS[I].J); }
}

Thanks for reading.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.