UVA 914 Jumping Champion Solution
Solution in c++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include<set>
#include<queue>
#define efor(i,a,b) for(int i=a; i<=b; i++)
#define zfor(i,n) for (int i=0; i<n; i++)
#define mall(ar, val) memset(ar, val, sizeof(ar))
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define M 1000001
using namespace std;
bool isprime[M];
vector<int>prime;
int num[150];
void sieve()
{
mall(isprime,true);
isprime[0] = isprime[1] = false;
for (int i=4; i<M; i+=2)
isprime[i] = false;
for (int i=3; i*i<M; i+=2)
if (isprime[i]==true)
for (int j=i*i; j<M; j+=i+i)
isprime[j] = false;
prime.pb(2);
for (int i=3; i<M; i+=2)
if (isprime[i]==true)
prime.pb(i);
}
int main()
{
int m,r,f,i,t,n,low,up;
sieve( );
cin>>t;
while(t--)
{
int mx=0,ind=0,c=0,temp;
cin>>m>>n;
mall(num,0);
low=(lower_bound(all(prime),m)-prime.begin());
up = (upper_bound(all(prime), n)- prime.begin());
for (int i=low+1; i<up; i++)
{
int temp = prime[i] - prime[i-1];
if(++num[temp]>mx)
{
mx=num[temp];
ind=temp;
}
}
efor(i,1,114)
{
if(num[i]==mx)
{
if (++c>1)
break;
}
}
if(c>1||mx==0)
{
cout<<"No jumping champion"<<endl;
}
else
printf("The jumping champion is %d\n", ind);
}
}
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
#include<iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include<set>
#include<queue>
#define efor(i,a,b) for(int i=a; i<=b; i++)
#define zfor(i,n) for (int i=0; i<n; i++)
#define mall(ar, val) memset(ar, val, sizeof(ar))
#define PI 3.1415926535897932385
#define uint64 unsigned long long
#define int64 long long
#define all(ar) ar.begin(), ar.end()
#define pb push_back
#define M 1000001
using namespace std;
bool isprime[M];
vector<int>prime;
int num[150];
void sieve()
{
mall(isprime,true);
isprime[0] = isprime[1] = false;
for (int i=4; i<M; i+=2)
isprime[i] = false;
for (int i=3; i*i<M; i+=2)
if (isprime[i]==true)
for (int j=i*i; j<M; j+=i+i)
isprime[j] = false;
prime.pb(2);
for (int i=3; i<M; i+=2)
if (isprime[i]==true)
prime.pb(i);
}
int main()
{
int m,r,f,i,t,n,low,up;
sieve( );
cin>>t;
while(t--)
{
int mx=0,ind=0,c=0,temp;
cin>>m>>n;
mall(num,0);
low=(lower_bound(all(prime),m)-prime.begin());
up = (upper_bound(all(prime), n)- prime.begin());
for (int i=low+1; i<up; i++)
{
int temp = prime[i] - prime[i-1];
if(++num[temp]>mx)
{
mx=num[temp];
ind=temp;
}
}
efor(i,1,114)
{
if(num[i]==mx)
{
if (++c>1)
break;
}
}
if(c>1||mx==0)
{
cout<<"No jumping champion"<<endl;
}
else
printf("The jumping champion is %d\n", ind);
}
}
No comments