POJ 2478Farey Sequence
escription
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=3*1e6+10;
LL prime[MAXN],tot=0,vis[MAXN],phi[MAXN],N;
void GetPhi()
{
for(LL i=2;i<=N;i++)
{
if(!vis[i])
{
prime[++tot]=i;
phi[i]=i-1;
}
for(LL j=1;j<=tot&&prime[j]*i<=N;j++)
{
vis[ i*prime[j] ] = 1;
if(i%prime[j]==0)
{
phi[ i*prime[j] ]=phi[i]*prime[j];
break;
}
else phi[ i*prime[j] ]=phi[i]*(prime[j]-1);
}
}
for(LL i=1;i<=N;i++)
phi[i]=phi[i]+phi[i-1];
}
int main()
{
N=2*1e6+10;
GetPhi();
while(cin>>N&&N!=0)
printf("%lldn",phi[N]);
return 0;
}
- 简易的深度学习框架Keras代码解析与应用
- Java并发编程的艺术(六)——线程间的通信
- 轻量级线程池的实现
- python根据BM25实现文本检索
- 稳扎稳打JavaScript(一)——作用域链内存模型
- 稳扎稳打JavaScript(二)——图解对象内存模型
- Swift学习资源
- 稳扎稳打JavaScript(三)——创建对象的几种方式
- 快速教程:使用Cython来扩展Python/NumPy库
- 稳扎稳打JavaScript(四)——闭包
- JavaScript奇淫技巧(一)
- 动态规划法(四)——0/1背包问题
- 动态规划法(三)——最长公共子序列
- Python数据分析模块 | pandas做数据分析(三):统计相关函数
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Apache IoTDB 系列教程-4:客户端接口
- 数据结构与算法系列2 线性表 使用java实现动态数组+ArrayList源码详解
- Kali中密码暴力破解工具hydra的使用
- Apache IoTDB 系列教程-8:文件同步工具
- 树莓派基础实验25:DS18B20温度传感器实验
- 迈出加入 Apache IoTDB 社区的第一步!(订阅邮件、调试代码)
- kali下一些代理工具的简单描述
- 树莓派基础实验26:旋转编码器实验
- java web Session会话技术(原理图解+功能+与Cookie的区别+基本使用)
- JAVA创建对象有哪几种方式
- Apache IoTDB 系列教程-7:时序数据文件格式 TsFile
- XSS原理及代码分析
- 树莓派基础实验27:温湿度传感器DHT11 实验
- org.apache.jasper.JasperException: Unable to compile class for JSP
- 树莓派基础实验28:红外避障传感器实验