Farey Sequence
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17744   Accepted: 7109

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
 
 
std的玄学做法没看懂
给n,求ans[n]。其中$ans[n]=ans[n-1]+phi[n]$,且n的范围比较大,在10的6次以内。则考虑打表解决。 
先得到能整除i的最小正整数$md[i]$(一定是个素数),再利用性质3,得到$phi[i]$
不过我用线性筛水过去啦。
 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;
const LL MAXN=*1e6+;
LL prime[MAXN],tot=,vis[MAXN],phi[MAXN],N;
void GetPhi()
{
for(LL i=;i<=N;i++)
{
if(!vis[i])
{
prime[++tot]=i;
phi[i]=i-;
}
for(LL j=;j<=tot&&prime[j]*i<=N;j++)
{
vis[ i*prime[j] ] = ;
if(i%prime[j]==)
{
phi[ i*prime[j] ]=phi[i]*prime[j];
break;
}
else phi[ i*prime[j] ]=phi[i]*(prime[j]-);
}
}
for(LL i=;i<=N;i++)
phi[i]=phi[i]+phi[i-];
}
int main()
{
N=*1e6+;
GetPhi();
while(cin>>N&&N!=)
printf("%lld\n",phi[N]);
return ;
}

POJ 2478Farey Sequence的更多相关文章

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

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

  2. [POJ 3581]Sequence

    [POJ 3581]Sequence 标签: 后缀数组 题目链接 题意 给你一串序列\(A_i\),保证对于$ \forall i \in [2,n],都有A_1 >A_i$. 现在需要把这个序 ...

  3. POJ 2442 Sequence

    Pro. 1 给定k个有序表,取其中前n小的数字.组成一个新表,求该表? 算法: 由于  a1[1] < a1[2] < a1[3] ... <a1[n] a2[1] < a2 ...

  4. POJ 3581 Sequence [后缀数组]

    Sequence Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6911   Accepted: 1543 Case Tim ...

  5. POJ 2442 - Sequence - [小顶堆][优先队列]

    题目链接:http://poj.org/problem?id=2442 Time Limit: 6000MS Memory Limit: 65536K Description Given m sequ ...

  6. POJ 2853 Sequence Sum Possibilities

    Sequence Sum Possibilities Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5537   Accep ...

  7. poj 2442 Sequence(优先队列)

    题目:http://poj.org/problem?id=2442 题意:给你n*m的矩阵,然后每行取一个元素,组成一个包含n个元素的序列,一共有n^m种序列, 让你求出序列和最小的前n个序列的序列和 ...

  8. POJ 2442 Sequence 优先队列

    题目: http://poj.org/problem?id=2442 #include <stdio.h> #include <string.h> #include <q ...

  9. POJ 3581 Sequence(后缀数组)

    [题目链接] http://poj.org/problem?id=3581 [题目大意] 给出一个数列,将这个数列分成三段,每段分别翻转,使得其字典序最小,输出翻转后的数列. [题解] 首先,第一个翻 ...

随机推荐

  1. HTML学习一_网页的基本结构及HTML简介

    HTML网页的基本结构 ```angular2html<!DOCTYPE html> 声明为 HTML5 文档<html> 元素是 HTML 页面的根元素<head> ...

  2. Nginx+tomcat集群使用redis共享session

    一 :nginx负载均衡 当Tomcat当做独立的Servlet容器来运行时,可看做是能运行Java Servlet的独立Web服务器. 此外 Tomcat还可以作为其他Web服务器进程内或者进程外的 ...

  3. 第83节:Java中的学生管理系统分页功能

    第83节:Java中的学生管理系统分页功能 分页功能一般可以做成两种,一种是物理分页,另一种是逻辑分页.这两种功能是有各自的特点的,物理分页是查询的时候,对数据库进行访问,只是查一页数据就进行返回,其 ...

  4. Robot Framework - 4 - 创建和扩展测试库的示例

    创建和扩展Library的示例 示例:Check status on Linux OS 创建与使用library的基本步骤:           1--- library实现的内容和实现的方式     ...

  5. 关于I/O编程

    IO在计算机中指Input/Output,也就是输入和输出 由于程序在运行时,数据是驻留在内存中的,并由CPU这个超快的计算核心来执行,涉及到数据交换的地方,通常是磁盘.网络等,就需要IO接口 IO编 ...

  6. 一道题引出对LinkedList源码的研究

    题目:打开一个文本文件,每次读取一行内容,将每行作为一个String读入,并将那个String对象置入一个LinkedList中,按照相反的顺序打印出LinkedList中的所有行. 解题代码: pu ...

  7. [工具]K8tools github/K8工具合集/K8网盘

    K8tools 20190428 声明: 工具仅供安全研究或授权渗透,非法用途后果自负. 博客: https://www.cnblogs.com/k8gege 下载: https://github.c ...

  8. CSS box-sizing属性

    全文摘抄自 https://developer.mozilla.org/zh-CN/docs/Web/CSS/box-sizing 专门抄一遍是因为,我想当然的以为标准盒子模型设置的宽高是包括padd ...

  9. java字符流操作flush()方法及其注意事项

    java字符流操作flush()方法及其注意事项   flush()方法介绍 查阅文档可以发现,IO流中每一个类都实现了Closeable接口,它们进行资源操作之后都需要执行close()方法将流关闭 ...

  10. 转载:浏览器开发系列第一篇:如何获取最新chromium源码

    背景:     最近摊上一个事儿,领导非要让写一篇技术文章,思来想去,自己接触chrome浏览器时间也不短了,干脆就总结一下吧.于是乎,本文顺理成章.由于有些细节必需描述清楚,所以这次先讲如何拿到ch ...