zoukankan      html  css  js  c++  java
  • 快速切题 sgu102.Coprimes 欧拉函数 模板程度 难度:0

    102. Coprimes

    time limit per test: 0.25 sec. 
    memory limit per test: 4096 KB

     

    For given integer N (1<=N<=104) find amount of positive numbers not greater than N that coprime with N. Let us call two positive integers (say, A and B, for example) coprime if (and only if) their greatest common divisor is 1. (i.e. A and B are coprime iff gcd(A,B) = 1).

     

    Input

    Input file contains integer N.

     

    Output

    Write answer in output file.

     

    Sample Input

    9
    

    Sample Output

    6

    注意欧拉函数只需要统计质因子

    #include <cstdio>
    #include <cmath>
    using namespace std;
    const int maxn=100;
    int n;
    int prim[maxn],len;
    void divide(){
        len=0;
        int tn=n;
        int r=sqrt(n+0.5)+1;
        for(int i=2;i<r;i++){
            if(tn%i==0){
                prim[len++]=i;
                while(tn%i==0)tn/=i;
            }
        }
        if(tn!=1)prim[len++]=tn;
    }
    int phi(){
        int ans=n;
        for(int i=0;i<len;i++){
            ans/=prim[i];
            ans*=prim[i]-1;
        }
        return ans;
    }
    int main(){
        while(scanf("%d",&n)==1){
            divide();
            int amount=phi();
            printf("%d\n",amount);
        }
        return 0;
    }
    

      

  • 相关阅读:
    第几天
    计算一个歌手的平均分
    将单词的首字母改为大写
    两数求和
    做一板1寸照片
    Filter过滤器
    AJAX
    EL表达式
    session存取
    默认的前进,刷新,后退,代码
  • 原文地址:https://www.cnblogs.com/xuesu/p/3995905.html
Copyright © 2011-2022 走看看