Cod sursa(job #1385613)

Utilizator sebinechitasebi nechita sebinechita Data 12 martie 2015 09:44:09
Problema Numere 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.66 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("dk.in");
ofstream fout("dk.out");

unsigned const maxb = 8192;
char buf[maxb];
unsigned ptr = maxb - 1;

int getInt()
{
    int rez = 0;
    while(!(buf[ptr] >= '0' && buf[ptr] <= '9'))
    {
        if(++ptr>=maxb)
        {
            fin.read(buf, maxb);
            ptr = 0;
        }
    }
    while((buf[ptr] >= '0' && buf[ptr] <= '9'))
    {
        rez = rez * 10 + buf[ptr] - '0';
        if(++ptr>=maxb)
        {
            fin.read(buf, maxb);
            ptr = 0;
        }
    }
    return rez;
}

int x, d, s;

int pow(int a, int p)
{
    int rez = 1;
    while(p)
    {
        if(p & 1)
        {
            rez = (1LL * rez * a)%x;
        }
        p >>= 1;
        a = (1LL * a * a) % x;
    }
    return rez;
}

bool vf(int a)
{
    a = pow(a, d);
    if(a == 1 || a == x - 1)
        return 1;
    for(int i = 1 ; i < s ; i++)
    {
        a = (1LL * a * a) % x;
        if(a == x - 1)
            return 1;
    }
    return 0;
}

bool prime(int n)
{
    if(n < 2)
        return 0;
    if(n == 2)
        return 1;
    if(n % 2 == 0)
        return 0;
    for(int i = 3 ; i * i <= n ; i+=2)
        if(n%i == 0)
            return 0;
    return 1;
}

bool p(int p)
{
    if(p <= 61)
        return prime(p);
    p--;
    while(p%2 == 0)
    {
        s++;
        p>>=1;
    }
    d = p;
    return ((vf(2) & vf(7)) & vf(61));
}

int main()
{
    int n, s = 0;
    fin >> n;
    for(int i = 1 ; i <= n ; i++)
    {
        fin >> x;
        s+=p(x);
    }
    fout << s << "\n";
    return 0;
}