/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
ll prime[1000000] ;
ll data[1000000] ;
ll pl ;
void isPrime()
{
ll l,k ;
prime[0] = prime[1] =1 ;
for(l=2 ; l<=1000000 ; l++)
{
if(!prime[l])
{
data[pl++] = l ;
for(k =l*2 ; k<=1000000 ; k+=l)
{
prime[k] =1 ;
}
}
}
}
bool primeCheck(ll n)
{
if(n<=1000000)
{
if(prime[n]==0)
return true ;
else
return false ;
}
else
{
int k ;
for(k=0 ; k<pl && data[k]<=sqrt(n) ; k++)
{
if(n%data[k]==0)
{
return false ;
}
}
return true ;
}
}
int main()
{
ll n ,m ,pr1,pr2,pr3,pr4,i,p1,p2,dis;
isPrime() ;
while(sc("%lld %lld",&n,&m)==2)
{
ll flag =0 ,mn =100000000 , mx =-1 ;
ll cnt =0 ;
for(i = n ; i<=m ; i++)
{
if(primeCheck(i))
{
if(!flag)
{
p1 = i ;
flag =1 ;
}
else
{
p2 = i ;
dis = p2 -p1 ;
if(dis<mn)
{
mn =dis ;
pr1 = p1 ;
pr2 = p2 ;
cnt++ ;
}
if(dis>mx)
{
mx =dis ;
pr3 =p1 ;
pr4 = p2 ;
cnt++ ;
}
p1 =p2 ;
}
}
}
if(cnt==0)
{
pf("There are no adjacent primes.\n") ;
}
else
pf("%lld,%lld are closest, %d,%d are most distant.\n",pr1,pr2,pr3,pr4) ;
}
}
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
ll prime[1000000] ;
ll data[1000000] ;
ll pl ;
void isPrime()
{
ll l,k ;
prime[0] = prime[1] =1 ;
for(l=2 ; l<=1000000 ; l++)
{
if(!prime[l])
{
data[pl++] = l ;
for(k =l*2 ; k<=1000000 ; k+=l)
{
prime[k] =1 ;
}
}
}
}
bool primeCheck(ll n)
{
if(n<=1000000)
{
if(prime[n]==0)
return true ;
else
return false ;
}
else
{
int k ;
for(k=0 ; k<pl && data[k]<=sqrt(n) ; k++)
{
if(n%data[k]==0)
{
return false ;
}
}
return true ;
}
}
int main()
{
ll n ,m ,pr1,pr2,pr3,pr4,i,p1,p2,dis;
isPrime() ;
while(sc("%lld %lld",&n,&m)==2)
{
ll flag =0 ,mn =100000000 , mx =-1 ;
ll cnt =0 ;
for(i = n ; i<=m ; i++)
{
if(primeCheck(i))
{
if(!flag)
{
p1 = i ;
flag =1 ;
}
else
{
p2 = i ;
dis = p2 -p1 ;
if(dis<mn)
{
mn =dis ;
pr1 = p1 ;
pr2 = p2 ;
cnt++ ;
}
if(dis>mx)
{
mx =dis ;
pr3 =p1 ;
pr4 = p2 ;
cnt++ ;
}
p1 =p2 ;
}
}
}
if(cnt==0)
{
pf("There are no adjacent primes.\n") ;
}
else
pf("%lld,%lld are closest, %d,%d are most distant.\n",pr1,pr2,pr3,pr4) ;
}
}
No comments:
Post a Comment