http://paste.ubuntu.com/7902091/
#include <stdio.h>
#include <ctime>
#include <cstdlib>
#define ll long long
ll pow(ll b,ll m,ll n)
{
ll sum=b,ans=1;
while (m)
{
if (m%2)
ans=(ans*sum)%n;
sum=(sum*sum)%n;
m/=2;
}
return ans;
}
int M_R(int x)
{
if(x== 2 ||x== 3 || x== 5 ||x == 7 || x== 11) return 1;
if(x== 1 || !(x%2) || !(x%3) || !(x%5) || !(x%7) || !(x%11)) return 0;
ll j=0,m,b,v,n=x--,i=0;
while (x%2==0)
{
j++;
x/=2;
}
m=x;
b=rand()%n+2;
v=pow(b,m,n);
if (v==1)
return 1;
while (1)
{
if (v==n-1)
return 1;
else if (i==j)
return 0;
v=(v*v)%n;
i++;
}
return 1;
}
int main()
{
int i,n,ans;
ll x;
srand(time(0));
while (scanf("%d",&n)!=EOF)
{
ans=0;
for (i=0;i<n;i++)
{
scanf("%I64d",&x);
if (M_R(x))
ans++;
}
printf("%d\n",ans);
}
}