ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1086. Cryptography

Time Limit Exceed
Posted by AquibMujtaba 6 Sep 2015 20:20
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 16500

void prime(int n)
{
    int i,j,temp,count=0;
    for(i=2;i<=MAX;i++)
    {
        temp=0;
        for(j=2;j*j<=i;j++)
        {
            if(i%j==0)
            {
                temp=1;
                break;
            }
        }
        if(temp==0)
        {
            count++;
        }
        if(count==n)
        {
            printf("%d\n",i);
            break;
        }
    }
}

int main()
{
   int n,t,i;
   scanf("%d",&t);
      while(t--)
      {
           scanf("%d",&n);
            prime(n);
      }
   return 0;
}

Why i got this TLE. I think my idea is not wrong but why its occur?
I don't understand. Pleas help me.....

Edited by author 06.09.2015 20:29
Re: Time Limit Exceed
Posted by Cabbage 13 Sep 2015 17:10
Your algorithm is not wrong, but it cost too much time. Try "sieve of Eratosthenes". You can search it on wiki.
Re: Time Limit Exceed
Posted by Aleksandr 8 Jan 2016 17:22
Правильное решение (Accepted):
using System;
namespace _1086_Криптография
{
    class Program
    {

        static void Main(string[] args)
        {
            // считываем количество выводимых чисел
            int n = int.Parse(Console.ReadLine());
            // создаем массив и считуем в него порядочные номера простых чисел
            int[] hip = new int[n];
            for (int i = 0; i < n; i++)
            {
                hip[i] = int.Parse(Console.ReadLine());

            }
            // определим максимальный элемент масива
            // можно исользовать цикл
            int[] hip1 = new int[n];
            Array.Copy(hip, hip1, n);
            Array.Sort(hip1);
            // зададим массив упорядоченых простых чисео с запасом
            int[] val = new int[hip1[hip1.Length - 1]+2];
            // зададим начальный ряд простых чисел
            val[0] = 2;
            val[1] = 3;
            val[2] = 5;
            val[3] = 7;
            // определяем простые числа делением на предыдущие простые числа
            // метод итераций построен на do while
                int k = 4; int i1 = 8;
                do
                {
                    int reid = 0;
                    for (int i = 0; i < k; i++)
                    {
                        if (i1 % val[i] == 0)
                        {reid = 1; break; }
                    }
                    if (reid == 0)
                    { val[k] = i1; k++; }
                    i1++;

                }
                while (k < hip1[hip1.Length - 1]+1);
            // вывод заданых простых чисел
                for (int i = 0; i < hip.Length; i++)
                {
                    Console.WriteLine(val[hip[i]-1]);
                }
        }
    }
}

Edited by author 08.01.2016 17:23

Edited by author 08.01.2016 17:23