zoukankan      html  css  js  c++  java
  • poj 2478: Farey Sequence

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13984   Accepted: 5526

    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
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<cstring>
     7 #include<queue>
     8 #include<vector>
     9 using namespace std;
    10 typedef long long LL;
    11 const LL maxn=1e6+5;
    12 LL N,tot,ANS;
    13 LL prime[maxn],phi[maxn];
    14 bool not_p[maxn];
    15 void shai(){
    16     not_p[1]=1;
    17     for(LL i=1;i<maxn;i++){
    18         if(not_p[i]==false){
    19             prime[++tot]=i;
    20             phi[i]=i-1;
    21         }
    22         for(LL j=1;j<=tot;j++){
    23             LL k=prime[j]*i;
    24             if(k>maxn) break;
    25             not_p[k]=true;
    26             if(i%prime[j]!=0){
    27                 phi[k]=phi[i]*phi[prime[j]];
    28             }
    29             else{
    30                 phi[k]=phi[i]*prime[j];
    31                 break;
    32             }
    33         }
    34     }
    35 }
    36 int main(){
    37     shai();
    38     while(scanf("%lld",&N)&&N){
    39         ANS=0;
    40         for(LL i=2;i<=N;i++){
    41             ANS+=phi[i];
    42         }
    43         printf("%lld
    ",ANS);
    44     }
    45     return 0;
    46 } 
     
  • 相关阅读:
    Oc中UIKit框架的继承结构
    iOS中的事件传递和响应者链
    音频视频,音乐,录音参考资料
    对象序列化(对对象的归档NSKeyedArchiver、接档NSKeyedUnarchiver)的理解
    iOS--UI篇——UIToolbar的简单使用
    iOS--内购的使用方法
    协调-分布式锁
    协调-协调架构原理
    协调-分布式配置
    队列-生产者消费者模式
  • 原文地址:https://www.cnblogs.com/CXCXCXC/p/5226513.html
Copyright © 2011-2022 走看看