05 November 2017

UVA 914 Jumping Champion

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 1000001
#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

const int BIGGEST_DIFF = 115;

int FindPos(const vector<int>& primes, int valueWanted)
{
    int min(0), max(primes.size() - 1), mid;

    while (min < max)
    {
        mid = (min + max) / 2;

        if (primes[mid] > valueWanted)
            max = mid;
        else if (primes[mid] < valueWanted)
            min = mid + 1;
        else
            return mid;
    }

    return min;
}

int main()
{
    vector<bool> isPrime(1000000, true);
    vector<int> primes;
    primes.push_back(2);

    for (long long i = 3; i < 1000000; i += 2)
    {
        if (isPrime[i])
        {
            primes.push_back(i);

            long long doubleI = i * 2;

            for (long long j = i * i; j < 1000000; j += doubleI)
                isPrime[j] = false;

        }
    }

    int T;
    scanf("%d", &T);

    while (T--)
    {
        int l, u;
        scanf("%d%d", &l, &u);

        int min = FindPos(primes, l), max = FindPos(primes, u);

        if (primes[min] < l)
            ++min;

        if (primes[max] > u)
            --max;

        vector<int> diffCount(BIGGEST_DIFF, 0);

        for (int i = min + 1; i <= max; ++i)
        {
            ++diffCount[primes[i] - primes[i - 1]];
        }

        int highest = 0, number = -1;

        for (int i = 0; i < BIGGEST_DIFF; ++i)
        {
            if (diffCount[i] > highest)
            {
                highest = diffCount[i];
                number = i;
            }

            else if (diffCount[i] == highest)
                number = -1;
        }

        if (number == -1)
            printf("No jumping champion\n");
        else
            printf("The jumping champion is %d\n", number);
    }
}

03 November 2017

UVA 10948 The primary problem

/***
Md. Nazmul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<iostream>
#include<cstdio>
#include<stack>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 1000001
#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
bool isPrime[maxi];
vector<int> data;
int x,n;
void sieve(){
memset(isPrime,true , sizeof(isPrime));
isPrime[0] = isPrime[1] = false;
for(int i=4 ; i<=maxi ; i+=2){
isPrime[i] = false;
}
for(int i=3 ; i*i<maxi ; i+=2){
if(isPrime[i]){
for(int j=i*i ; j<maxi ; j+=i+i)
isPrime[j] = false;
}
}
data.pb(2);
for(int i=3 ; i<maxi ; i+=2)
if(isPrime[i])
data.pb(i);
}
bool check(){
if(n%2 || n==4){
x =2 ;
return isPrime[n-x];
}
for(int i=1 ; i<data.size() && data[i]<=n/2 ; i++){
if(isPrime[n-data[i]]){
x = data[i];
return true;
}
}
return false;
}
int main()
{
sieve();
while(sc("%d",&n) && n){
pf("%d:\n",n);
if(check()){
pf("%d+%d\n",x,n-x);
}else{
pf("NO WAY!\n");
}
}
return 0;
}

UVA 10679 - I Love Strings!!