Uva 686 Solution

#include<bits/stdc++.h>
using namespace std;
#define N 40000
int p[N];
vector<int>a;

void prime()
{
    int i, j, l;
    l =sqrt(N);

    p[0] = p[1] = 1;
    a.push_back(2);

    for(i=4;i<N;i+=2)
        p[i] = 1;

    for(i=3;i<N;i+=2)
    {
        if(p[i] == 0)
        {
            a.push_back(i);
            if(i<=l)
            {
                for(j =i*i;j<N;j+=i*2)
                    p[j] = 1;
            }
        }
    }

}

int anik(int n)
{
    int c = 0, l = 0, t = 0;

    while(a[t] < n)
        t++;

    while(l <= t)
    {
        if(a[l] + a[t] == n)
        {
            c++;
            l++;
            t--;
        }

        else if(a[l] + a[t] < n)
            l++;

        else
            t--;
    }
    return c;
}

int main()
{
    prime();
    int n, m;
    while(cin >> n)
    {
        if(n == 0)
            break;

        m = anik(n);
        cout << m << endl;
    }

    return 0;
}

Comments