POJ2478 Farey Sequence —— 欧拉函数

时间:2022-12-28 19:13:01

题目链接:https://vjudge.net/problem/POJ-2478

Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17753   Accepted: 7112

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

题解:

单纯的欧拉函数。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; int euler[MAXN];
void getEuler()
{
memset(euler, , sizeof(euler));
euler[] = ;
for(int i = ; i<MAXN; i++) if(!euler[i]) {
for(int j = i; j<MAXN; j += i)
{
if(!euler[j]) euler[j] = j;
euler[j] = euler[j]/i*(i-);
}
}
} LL f[MAXN];
void init()
{
getEuler();
f[] = ;
for(int i = ; i<MAXN; i++)
f[i] = f[i-] + euler[i];
} int main()
{
int n;
while(scanf("%d", &n)&&n)
printf("%lld\n", f[n]);
}

POJ2478 Farey Sequence —— 欧拉函数的更多相关文章

  1. poj2478 Farey Sequence &lpar;欧拉函数&rpar;

    Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...

  2. poj2478 Farey Sequence 欧拉函数的应用

    仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值  的总和,为什么呢,因为要构成 a/b 然后不能约分  所以 gcd(a,b)==1,所以  分母 b的 欧拉函数值   ...

  3. hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数

    hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...

  4. poj 2478 Farey Sequence&lpar;欧拉函数是基于寻求筛法素数&rpar;

    http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...

  5. UVA12995 Farey Sequence &lbrack;欧拉函数,欧拉筛&rsqb;

    洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...

  6. poj 2478 Farey Sequence 欧拉函数前缀和

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K       Description The Farey Sequence Fn for ...

  7. POJ-2478-Farey Sequence&lpar;欧拉函数)

    链接: https://vjudge.net/problem/POJ-2478 题意: The Farey Sequence Fn for any integer n with n >= 2 i ...

  8. Poj 2478-Farey Sequence 欧拉函数&comma;素数&comma;线性筛

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 5647 D ...

  9. POJ2478 - Farey Sequence&lpar;法雷级数&amp&semi;&amp&semi;欧拉函数&rpar;

    题目大意 直接看原文吧.... The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rat ...

随机推荐

  1. Spring MVC学习笔记——登录和异常处理

    1.在WEN-INF文件夹下面,添加一个login.jsp文件 <%@ page language="java" contentType="text/html; c ...

  2. centos6 下安装xfce&plus;vnc

    CentOS 安装图形界面的过程,简单记录一下.这里提供了两种图形界面的安装,分别是CentOS自带的gnome桌面及轻巧的xfce.据测试,我的精简版CentOS 6 64位系统安装gnome需要下 ...

  3. Linux企业级项目实践之网络爬虫(7)——DNS解析

    DNS 是Domain Name Service的缩写.域名系统为Internet上的主机分配域名地址和IP地址.IP地址不易于记忆,然而域名地址相比较而言是方便于记忆的.用户如果使用域名地址,当想获 ...

  4. Sublime Text 3 修改插件安装位置【sublime text、插件路径、Data】

    直接切入正题,在享受Sublime 插件给我们带来开发效率的同时,有些插件的文件也是很大的,但是插件默认安装的位置是AppData的目录[C:\Users\用户名\AppData\Roaming\Su ...

  5. Oracle之 any 、some、all解析

    oracle之 any.some.all 解析 因为很少用到, 所以几乎忘记了这几个函数, 不过它们还是很有用的使用它们可以大大简化一些SQL文的语法, 至于效率问题, 如CCW所说它们和EXISTS ...

  6. JavaScript设计模式&lpar;8&rpar;-装饰者模式

    装饰者模式 1. 作用: 可用来透明地把对象包装在具有同样接口的另一对象之中,这样可以给一个方法添加一些行为,然后将方法调用传递给原始对象. 可用于为对象增加功能,用来代替大量子类. 装饰者对其组件进 ...

  7. RNN入门(一)识别MNIST数据集

    RNN介绍   在读本文之前,读者应该对全连接神经网络(Fully Connected Neural Network, FCNN)和卷积神经网络( Convolutional Neural Netwo ...

  8. hdu-2027题&amp&semi;&amp&semi;gets&sol;getchar的区别

    hdu-2027题(水题~~~) 统计每个元音字母在字符串中出现的次数. Input输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串. Output对于每个测试实例输 ...

  9. 【RSYSLOG】Log4x To Rsyslog Config

    Log4x To Rsyslog Config Log4net配置 <!--RemoteSyslogAppender--> <appender name="remoteSy ...

  10. CentOS7安装chrony替代ntp同步时间

    Chrony是一个开源的*软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确.它由两个程序组成:chronyd和chronyc:chronyd是一个后台运行的守护进程,用于调整内核中运 ...