• POJ-2478 Farey Sequence(欧拉函数)


    Farey Sequence
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 15449   Accepted: 6149

    Description

    The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b) = 1 arranged in increasing order. The first few are 
    F2 = {1/2} 
    F3 = {1/3, 1/2, 2/3} 
    F4 = {1/4, 1/3, 1/2, 2/3, 3/4} 
    F5 = {1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5} 

    You task is to calculate the number of terms in the Farey sequence Fn.

    Input

    There are several test cases. Each test case has only one line, which contains a positive integer n (2 <= n <= 106). There are no blank lines between cases. A line with a single 0 terminates the input.

    Output

    For each test case, you should output one line, which contains N(n) ---- the number of terms in the Farey sequence Fn. 

    Sample Input

    2
    3
    4
    5
    0

    Sample Output

    1
    3
    5
    9

    Source

    POJ Contest,Author:Mathematica@ZSU
     
    欧拉函数φ( )是指不超过n且与n互素的正整数的个数

    φ函数的值 通式:φ(n)=n(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pk),其中p1, p2……pk为n的所有质因数

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <queue>
     6 #include <stack>
     7 #include <vector>
     8 #include <iostream>
     9 #include "algorithm"
    10 using namespace std;
    11 typedef long long LL;
    12 const int MAX=1000005;
    13 int n,m;
    14 int phi[MAX];
    15 LL sum[MAX];
    16 void init(){
    17     int i,j;
    18     for (i=1;i<MAX;i++) phi[i]=i;
    19     for (i=2;i<MAX;i+=2) phi[i]/=2;
    20     for (i=3;i<MAX;i+=2) 
    21      if (phi[i]==i){
    22          for (j=i;j<MAX;j+=i)
    23           phi[j]=phi[j]/i*(i-1);
    24      }
    25     sum[1]=0;
    26     sum[2]=phi[2];
    27     for (i=3;i<MAX;i++){
    28         sum[i]=sum[i-1]+(LL)phi[i];
    29     }
    30 }
    31 int main(){
    32     freopen ("sequence.in","r",stdin);
    33     freopen ("sequence.out","w",stdout);
    34     int i,j;
    35     init();
    36     while (1){
    37         scanf("%d",&j);
    38         if (j==0)
    39          break;
    40         printf("%lld
    ",sum[j]);
    41     }
    42     return 0;
    43 }
    如果你不会,那你最好看看
     
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    原型模式&原型链
    [四种方法]检测数据类型
    JSON
    PHP基础 mysqli的事务处理
    PHP的扩展类 mysqli_stmt:预处理类
    PHP的 Mysqli扩展库的多语句执行
    PHP基础文件下载类的简单封装
    PHP基础封装简单的MysqliHelper类
    Spring深入浅出(四)AOP面向切面
    Spring深入浅出(二)IOC的单例 ,继承,依赖,JDBC,工厂模式以及自动装载
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/6063371.html
Copyright © 2020-2023  润新知