Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18507   Accepted: 7429

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 题意:给出一个数n,要你求出在1-n中,互质的数有多少对; 思路:这题属于欧拉函数的应用。1-n中互质数的对数就是等于与2互质的数的个数+与3互质的数的个数+与4互质的数的个数+....+与n互质的数的个数。(为什么没有1,因为一个数无法成对)
所以这题就演变成了求2-n中所有数的欧拉函数的和,所以要用到欧拉函数的变形写法。
因为是多组输入,所以要预处理一下,提前记录下n为1-1000000时的值。 代码:
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#define eps 1e-7
#define ll long long
#define inf 0x3f3f3f3f
#define pi 3.141592653589793238462643383279
using namespace std;
ll n,euler[],sum[];
int main()
{
for(int i=; i<=; ++i) //初始化所有数的欧拉函数值为自己
euler[i] = i;
for(int i=; i<=; ++i) //求所有数的欧拉函数值
{
if(euler[i] == i) //根据公式 欧拉值(n) = n*(1-1/p1)(1-1/p2)...(1-1/pi)其中p为n的素因子
//可知素数只有自己是自己的素因子,所以在遇到自己以前euler值没变,所以可以通过上面的语句得知euler[i]==i的为素数
{
for(int j=i; j<=; j+=i) //所有i的倍数乘上(1-1/i)
euler[j] = euler[j]/i*(i-);
}
}
sum[] = ;
for(int i=; i<=; ++i) //提前记录下每个数的答案
{
sum[i] = sum[i-] + euler[i];
}
while(scanf("%lld",&n)!=EOF)
{
if(!n) break;
printf("%lld\n",sum[n]);
}
return ;
}
 

poj2478——Farey Sequence(欧拉函数)的更多相关文章

  1. POJ2478 Farey Sequence —— 欧拉函数

    题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K To ...

  2. poj2478 Farey Sequence (欧拉函数)

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

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

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

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

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

  5. poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)

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

  6. UVA12995 Farey Sequence [欧拉函数,欧拉筛]

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

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

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

  8. POJ-2478-Farey Sequence(欧拉函数)

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

  9. Poj 2478-Farey Sequence 欧拉函数,素数,线性筛

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

  10. POJ2478 - Farey Sequence(法雷级数&&欧拉函数)

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

随机推荐

  1. react组件生命

    组件的生命周期主要由三个部分组成: Mounting:组件正在被插入DOM中 Updating:如果DOM需要更新,组件正在被重新渲染 Unmounting:组件从DOM中移除 React提供了方法, ...

  2. thymeleaf switch在表格中的使用,遇到的空行问题

    switch在表格中的使用时 如果把<td>写在<div th:switch="${data.isShow}"> 里面导致外面出现很多空的<div&g ...

  3. 大端模式、小端模式和C#反转

    A.C#大端模式和小端模式. 小端(little-endian)模式:低地址上存放低字节,高地址上存放高字节. 如0x11223344→ byte[] numBytes = new byte[]{ 0 ...

  4. 关于where和having的直观理解

    一,查询区别 where是对前面select的字段没有要求,直接查询库表的 having是对前面的select的字段有要求,字段已经select出来的 可以用having进行处理 select id, ...

  5. Shiro异常处理总结

    出自:https://blog.csdn.net/goodyuedandan/article/details/62420120 一.Spring MVC处理异常有3种方式: (1)使用Spring-M ...

  6. rails 网站字体

    方法1,在rubymine下查找所有css,scss,sass,less,修改所有带font-family的内容,删除public文件夹下面的缓存css,查看效果.如 body { backgroun ...

  7. sitemap和sitemapindex

    介绍: https://support.google.com/webmasters/answer/75712?hl=zh-Hans 实例: http://www.thepaper.cn/sitemap ...

  8. PHP - 请求阻塞,Session写阻塞

    之前写某些代码的时候,发现用户莫名奇妙地阻塞了,而且这种阻塞的情况还比较难以形容: 使用session过程中,在开启session后,同一浏览器,执行同一程序,不同页面会被锁.不同浏览器不会出现这种情 ...

  9. PHP笔试题及答案

    1.表单提交get和post有何区别? 答:get的方式是把数据在地址栏中发送,get传送的数据量较小,不能大于2KB.post传送的数据量较大,一般被默认为不受限制.但理论上,IIS4中最大量为80 ...

  10. ASP.NET Web API(C#)学习01

    Web Api 记得去年公司有个分享会分享了这个,最近留意招聘信息的时候,发现有个招聘信息的要求是会用WebApi,然后花了半个小时不到,根据下面这篇文章了解了一下,觉得这个东西也不难啊. 突然发现在 ...