首页 > 其他分享 >POJ2487 Farey Sequence 欧拉函数模板题

POJ2487 Farey Sequence 欧拉函数模板题

时间:2023-02-07 12:36:29浏览次数:94  
标签:prime phi const Sequence int long Farey POJ2487 include


Farey Sequence

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

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 18838

 

Accepted: 7581

算法分析:

裸的欧拉函数求和问题,最后用long long 保存

代码实现

#include<cstdio>  
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
using namespace std;
const double eps = 1e-8;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN=5010;
const int MAXM=100010;
using namespace std;
const int M=1000011;
int prime[M],mark[M];//prime是素数数组,mark为标记不是素数的数组
int tot,phi[M];//phi为φ(),tot为1~i现求出的素数个数
void getphi(int N){
phi[1]=1;//φ(1)=1
for(int i=2;i<=N;i++){//从2枚举到N
if(!mark[i]){//如果是素数
prime[++tot]=i;//那么进素数数组,指针加1
phi[i]=i-1;//根据性质1所得
}
for(int j=1;j<=tot;j++){//从现求出素数枚举
if(i*prime[j]>N) break;//如果超出了所求范围就没有意义了
mark[i*prime[j]]=1;//标记i*prime[j]不是素数
if(i%prime[j]==0){//应用性质2
phi[i*prime[j]]=phi[i]*prime[j];break;
}
else phi[i*prime[j]]=phi[i]*phi[prime[j]];//应用性质3
}
}
}


int main()
{

int n;
getphi(M);
while(scanf("%d",&n)!=EOF)
{
if(n==0) break;
long long sum=0;
for(int i=2;i<=n;i++)
{
sum+=phi[i];
}
cout<<sum<<endl;
}
return 0;
}

 

 

标签:prime,phi,const,Sequence,int,long,Farey,POJ2487,include
From: https://blog.51cto.com/u_14932227/6041966

相关文章

  • 2019-ACM-ICPC 徐州网络赛 M. Longest subsequence 序列自动机
    Stringisaveryusefulthingandasubsequenceofthesamestringisequallyimportant.Nowyouhaveastring ss withlength nn andastring tt withlen......
  • 2019南昌大学邀请赛 M. Subsequence 序列自动机
     Giveastring SS and NN string T_iTi ,determinewhether T_iTi isasubsequenceof SS.Iftiissubsequenceof SS,print ​​YES​​​,elseprint ......
  • PostgreSQL数据库-Sequence的作用和用法
    PostgreSQL中的序列是一个数据库对象,本质上是一个自增器。所以,Sequence也可以通过在每个属性后加上autoincrment的值的形式存在。sequence的作用有两个方面:作为表的唯一......
  • Sequence Decoding (DFS)
    给出一个字符串,只含【,】,H,P,和数字,要把这些数字和中括号里的字母展开复制多少遍根据中括号外面的数字决定。用深搜来做,遇到数字就记录数字多少,遇到【就进深一度的搜索,如果数H......
  • [LeetCode]Permutation Sequence
    QuestionTheset[1,2,3,…,n]containsatotalofn!uniquepermutations.Bylistingandlabelingallofthepermutationsinorder,Wegetthefollowingsequen......
  • HDU-1159-Common Subsequence
    ​​题目链接​​​题目大意:给出两个字符串,求两个字符串的最长公共字串。思路:慢慢重心开始有贪心转向动态规划了,这题就是简单的动态规划题。以题目的第一组测试数据为例......
  • 【题解】CF1770F Koxia and Sequence
    有没有觉得其他题解的模二Lucas逆用太智慧了,有没有觉得这题的第一思路是直接拆位算每一位是否有贡献,而不是先满足和的限制列式?这里提供另外一个做法。方向不同,结果一样......
  • poj-1458-Common Subsequence
    CommonSubsequenceTimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:43207Accepted:17522DescriptionAsubsequenceofagivensequenceisthegivenseq......
  • Codeforces Round #661 (Div. 3) D. Binary String To Subsequences(贪心/思维)
    https://codeforces.com/contest/1399/problem/D题目大意:长度为n的字符串s只由0和1组成。我们要把s分成最小数量的子序列,使得每一个子序列看起来像“010101...”或......
  • 摘抄重要内容 Minimap2 On the definition of sequence identity
     HengLi'sblogArchiveCategoriesPagesTagsOnthedefinitionofsequenceidentity25November2018Sequenceidentityisawaytomeasurethe......