Cod sursa(job #487422)

Utilizator cont_de_testeCont Teste cont_de_teste Data 25 septembrie 2010 10:34:02
Problema Ciurul lui Eratosthenes Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
# include <algorithm>
using namespace std ;

const char FIN[] = "scmax.in", FOU[] = "scmax.out" ;
const int MAX = 100005 ;

int N, sol = 1 ;

int modulo(int a,int b,int c){
    long long x=1,y=a; // long long is taken to avoid overflow of intermediate results
    while(b > 0){
        if(b%2 == 1){
            x=(x*y)%c;
        }
        y = (y*y)%c; // squaring the base
        b /= 2;
    }
    return x%c;
}
long long mulmod(long long a,long long b,long long c){
    long long x = 0,y=a%c;
    while(b > 0){
        if(b%2 == 1){
            x = (x+y)%c;
        }
        y = (y*2)%c;
        b /= 2;
    }
    return x%c;
}
bool Miller(long long p,int iteration){
    if(p<2){
        return false;
    }
    if(p!=2 && p%2==0){
        return false;
    }
    long long s=p-1;
    while(s%2==0){
        s/=2;
    }
    for(int i=0;i<iteration;i++){
        long long a=rand()%(p-1)+1,temp=s;
        long long mod=modulo(a,temp,p);
        while(temp!=p-1 && mod!=1 && mod!=p-1){
            mod=mulmod(mod,mod,p);
            temp *= 2;
        }
        if(mod!=p-1 && temp%2==0){
            return false;
        }
    }
    return true;
}

int main ( void ) {
      freopen ( "ciur.in", "r", stdin ) ;
      freopen ( "ciur.out", "w", stdout ) ;
      scanf ( "%d", &N ) ;
      for ( int i = 3; i <= N; i += 2 ) {
          if ( Miller ( i, 5 ) ) {
              ++sol ;
          }
      }

      printf ( "%d", sol ) ;

}