数论(米勒测试)

a^(p-1)%p==1

 

 

#include <iostream>
#include 
<time.h>
using namespace std;


inline __int64 GetMod(__int64 
base,__int64 power,__int64 Mod)
{
    __int64 t
=1;
    __int64 res
=base;
    
    
while (power>1)
    {
        
if (power&1)
        {
            t
=(t*res)%Mod;
        }
        
        res
=(res*res)%Mod;
        power
=power>>1;
        
    }
    
    
return (res*t)%Mod;
}

inline 
bool slove(__int64 n)
{
    
if (n==1)
    {
        
return false;
    }
    
    
if (n==2)
    {
        
return true;
    }
    
    
if ((n&1)==0)
    {
        
return false;
    }
    
    
    
long a;
    
    a
=2;
    if(n%a==0) return false;
    
if (n>a&&GetMod(a,n-1,n)!=1)
    {
        
return false;
    }
    
    a
=3;
    if(n%a==0) return false;
    
if (n>a&&GetMod(a,n-1,n)!=1)
    {
        
return false;
    }
    
    
    a
=5;
    if(n%a==0) return false;
    
if (n>a&&GetMod(a,n-1,n)!=1)
    {
        
return false;
    }
    
    a
=7;
    if(n%a==0) return false;
    
if (n>a&&GetMod(a,n-1,n)!=1)
    {
        
return false;
    }
    
    
return true;
}

int main()
{
    __int64 n;
    
long T;
    scanf(
"%ld",&T);
    
while (T--)
    {
        scanf(
"%I64d",&n);
        
if(slove(n))
        {
            puts(
"yes");
        }
        
else
        {
            puts(
"no");
        }    
    }
    
    
return 0;
}
posted @ 2008-10-01 18:01  Hdu-Lost  阅读(591)  评论(0编辑  收藏  举报