dimanche 12 juillet 2020

SPOJ MPRIME overtime

Link of question: https://vn.spoj.com/problems/MPRIME/

I have 3 arrays C D E that:

C contains prime number.

D contains pair of 2 two adjacent prime number (ex: 2 and 3 is two adjacent prime number (C[1] and C[2], so D[1] is 23, then, D[2] is 57, D[3] is 1113 and so on.

E contains numbers from D that is a prime number (ex: 23 is a prime number so E[1] = 23, E[2] = 3137, ...)

The question is to print the E[n] number with 0 <= n <= 500.

I tried to do it but when I want to print the answer, it doesn't print anything.

Here my code:

#include <bits/stdc++.h>

#define fast ios_base::sync_with_stdio(0); cin.tie(0);
#define endl "\n"

typedef long long ll;

using namespace std;

const int oo = 1500000;

int n;

ll C[oo], D[oo], E[oo];

bool Flag[oo];

void makePrime_and_create_C ()
{
    for ( int i = 2; i <= oo; i ++ ) 
        Flag[i] = true;

    for ( int i = 2; i <= oo; i ++ ) 
    {
        if ( Flag[i] == true ) 
        {
            for ( int j = 2 * i; j <= oo; j += i ) 
                Flag[j] = false;
        }
    }

    int cnt = 0;
    
    for ( int i = 2; i <= oo; i ++ )
        if ( Flag[i] == true )
            C[++cnt] = i;
}

int cnt_ele (int x)
{
    int cnt = 0;
    
    while ( x > 0 )
    {
        cnt ++;
        
        x/= 10;
    }
    
    return pow(10, cnt);
}

void pairing ()
{
    int cnt = 0;
    
    for ( int i = 1; i <= oo; i += 2 )
    {
        int tmp = C[i] * cnt_ele(C[i]) + C[i + 1];
        
        D[++cnt] = tmp;
    }
}

int main ()
{
    makePrime_and_create_C ();
    
    pairing ();
    
    for ( int i = 1; i <= oo; i ++ )
        if ( Flag[D[i]] == true )
                E[i] = D[i];
                
    int n;  cin >> n;
    
    cout << E[n];
    
    return 0;
}

Tks for help <3

Aucun commentaire:

Enregistrer un commentaire