Dear Iffat
you used 2 big size long long array,which certainly caused MLE.here the thing you can do,
1. for prime generating use seive algo with a bool array of max 20000000 size.by taking bool type array you can save enough memory.
2.you can then take your 2nd array to store the twin primes of long type(no need for long long) and for this the size 100000 is enough. hope u'll get AC soon.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int l=1,u=20000000,d,j;
d=u-l+1;
bool *flag=new bool[d];
for (int i=0;i<d;i++)
flag[i]=true;
for (int i=(l%2!=0);i<d;i+=2)
flag[i]=false;
for (int i=3;i<=sqrt(u);i+=2)
{
j=l;
while (j%i!=0)
j++;
for (j=j+i;j<u;j+=i)
flag[j-l]=false;
}
if (l<=1) flag[1-l]=false;
if (l<=2) flag[2-l]=true;
int c=1;
int b[110002];
for (int i=0;i<d;i++) if (flag[i] && flag[i+2])
{
b[c-1]=i+l;
c++;
}
int n;
while (cin>>n)
{
int t=b[n-1];
cout<<"("<<t<<", "<<t+2<<")"<<endl;
}
return 0;
}
friends i got AC but it's too slow 9.57 s and memory used is also high 1900
plz suggest ways to better it............
Hi!! everyone...
I am fed up with this question...
changed my sieve method lot of time.
my present sieve method takes around 1.1-1.2 seconds.I also used the concept of twin primes around multiples of 6...
then also getting TLE...
PLZ Help..here is my code...
Thanks Jan for your reply.
As far as system("pause") is concerned,it does not make any kind of problem.
I have submitted many problems in UVa with system("pause").
or Does using system("pause") increases runtime??
Plz Can u explain how Bit-wise sieve is done?
I haven't find any reason to use 'system("pause")'. However, its up to you. About bitwise sieve, bool takes 8 bits. So, when you assign something in prime, it will change all the 8 bits. You can use integer array, and imagine that each bit of the array maps an integer. Say a[2], a[0] maps to 0 to 31, a[1] maps to 32 to 63 (cause an integer has 32 bits). Now you can use bitwise operations to use the array.