POJ2478 - Farey Sequence(法雷级数&&欧拉函数)
题目大意
直接看原文吧。。。。
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.
题解
欧拉函数的一个应用~~对于输入n,答案就等于phi[1]+phi[2]+……phi[n]
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#define MAXN 1000005
using namespace std;
int phi[MAXN],prime[MAXN],check[MAXN];
void euler_phi()
{
int cnt=0;
memset(check,false,sizeof(check));
phi[1]=1;
for(int i=2; i<MAXN; i++)
{
if(!check[i])
{
prime[cnt++]=i;
phi[i]=i-1;
}
for(int j=0; j<cnt&&i*prime[j]<MAXN; j++)
{
check[i*prime[j]]=true;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
int main()
{
int n;
euler_phi();
while(cin>>n&&n)
{
long long sum=0;
for(int i=2; i<=n; i++)
sum+=phi[i];
cout<<sum<<endl;
}
return 0;
}
POJ2478 - Farey Sequence(法雷级数&&欧拉函数)的更多相关文章
- poj2478——Farey Sequence(欧拉函数)
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18507 Accepted: 7429 D ...
- poj-2478 Farey Sequence(dp,欧拉函数)
题目链接: Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14230 Accepted: ...
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- Farey Sequence (欧拉函数+前缀和)
题目链接:http://poj.org/problem?id=2478 Description The Farey Sequence Fn for any integer n with n >= ...
- POJ_2478 Farey Sequence 【欧拉函数+简单递推】
一.题目 The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbe ...
- 洛谷UVA12995 Farey Sequence(欧拉函数,线性筛)
洛谷题目传送门 分数其实就是一个幌子,实际上就是求互质数对的个数(除开一个特例\((1,1)\)).因为保证了\(a<b\),所以我们把要求的东西拆开看,不就是\(\sum_{i=2}^n\ph ...
- Farey Sequence(欧拉函数板子题)
题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total S ...
- Farey Sequence(欧拉函数)
题意:给出式子F F中分子分母互质,且分子小于分母 例: F2 = {1/2} F3 = {1/3, 1/2, 2/3} F4 = {1/4, 1/3, 1/2, 2/3, 3/4} F5 = {1/ ...
- UVaLive 7362 Farey (数学,欧拉函数)
题意:给定一个数 n,问你0<= a <=n, 0 <= b <= n,有多少个不同的最简分数. 析:这是一个欧拉函数题,由于当时背不过模板,又不让看书,我就暴力了一下,竟然A ...
随机推荐
- Django环境搭建和项目创建
1.下载安装python 2.打开shell(windows下cmd),安装虚拟环境工具: "pip install virtualenv".(可以通过“python -m pi ...
- uCOS-II任务的挂起和恢复
函数描述 OSTaskSuspend() 功能描述:无条件挂起一个任务.调用此函数的任务也可以传递参数OS_PRIO_SELF,挂起调用任务本身.函数原型:INT8U OSTaskSuspend ( ...
- 关于博客名“大话济公”的说明
其实本来没打算起这个名字的,换了几个名字都被占用了(无语啊...).最近呢,我在研究<济公传>,对于济公的传说比较喜欢,尤其是这个任务,诙谐幽默,同时有时时刻刻在帮助有困难的群众,虽然是个 ...
- ASP.NET MVC 3 Razor Views in SharePoint
http://tqcblog.com/2011/01/22/asp-net-mvc-3-razor-views-in-sharepoint/ ASP.NET MVC 3 has just been r ...
- Python之添加新元素
现在,班里有3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 今天,班里转来一名新同学 Paul,如何把新同学添加到现有的 list 中呢? 第一个办法是 ...
- Android openGL ES 2.0里Surfaceview背景透明
surfaceview的黑色背景会挡住其父的背景,现在把surfaceview的背景设为透明,既可以看到所绘的3D物体,又可以看到背景. 在onSurfaceCreated里,调用GLES20.glC ...
- <三> SQL 基础
SQL查询的一般形式,以及被逻辑处理的顺序 (8) select (9) distinct (11) <TOP_specification> <select_list> (1) ...
- JavaScript的OOP编程2
我做了一个observer的设计模式实现 version1 // -------------------------------------------------- function Subject ...
- js高手
http://kb.cnblogs.com/page/173798/ http://kb.cnblogs.com/page/121539/ http://blog.jobbole.com/9648/ ...
- 几种 Docker 监控工具对比
轻量级虚拟化容器 Docker,自发布以来便广受业界关注,在开源界和企业界掀起了一阵风.Docker 容器相对于 VM 有以下几个优势:启动速度快:资源利用率高:性能开销小. 从图中可以看出 Dock ...