Advertisement
yuawn

algo2017_week1_euler

Oct 3rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int Pow(long long x, int n, long long mod) {
  5.     long long Ans = 1, t = x;
  6.     while(n) {
  7.         if(n&1)
  8.             Ans *= t, Ans %= mod;
  9.         t *= t, t %= mod, n >>= 1;
  10.     }
  11.     return (int)Ans;
  12. }
  13.  
  14. int isPrime(int n) {
  15.     if(n == 2 || n == 3)    return 1;
  16.     if(n == 1)    return 0;
  17.     if(!(n & 1))    return 0;
  18.     int a, x, flag = 1, t;
  19.     for(a = 0; a < 2; a++) {
  20.         x = rand() % (n - 4) + 2;
  21.         t = Pow( x , n-1 , n );
  22.         if(t != 1)    return 0;
  23.     }
  24.     return 1;
  25. }
  26.  
  27. int gcd(int a,int b){
  28.     while((a%=b)&&(b%=a));
  29.     return a+b;
  30. }
  31.  
  32. int euler( int n ){
  33.     int t = 0;
  34.     if( n == 1 ) return 1;
  35.     if( isPrime( n ) ) return n - 1;
  36.     for( int i = 1 ; i <= n ; i++ ){
  37.         if( gcd( i , n ) == 1 ) ++t;
  38.     }
  39.     return t;
  40. }
  41.  
  42. int main(){
  43.     int n , t;
  44.     scanf("%d",&t);
  45.     while( t-- ){
  46.         scanf("%d",&n);
  47.         printf("%d\n" , euler(n));
  48.     }
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement