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;
    }
    

      

  • 相关阅读:
    AWK
    正则表达式
    BASH
    C# 常用控件的一些属性及方法
    C# FTP
    C# Delegate
    DLL/EXE查看工具Dumpbin
    VBA 破解Excel工作表保护密码
    VB6 IP地址+网卡地址+网卡类型
    编程之路┊由C#风潮想起的——给初学编程者的忠告 ZT
  • 原文地址:https://www.cnblogs.com/xuesu/p/3995905.html
Copyright © 2011-2022 走看看