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/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5}
求解 fn的元素个数、
分析:本题就是求解欧拉函数值的前n项和,直接求解欧拉函数值的方法不行,由于用此法就是O(n^2)复杂度。採用递推式求解是O(nlogn)复杂度
代码例如以下:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <utility>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <functional> using namespace std;
long long euler(long long n){ //返回euler(n)
long long res=n,a=n;
for(long long i=2;i*i<=a;i++){
if(a%i==0){
res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
//欧拉函数值直接求法,没用上,超时
long long a[1000010];
int main(){
for(long long i=1;i<=1000005;i++)a[i]=i;
for(long long i=2;i<=1000005;i+=2)a[i]/=2;
for(long long i=3;i<=1000005;i++)if(a[i]==i){
for(long long j=i;j<=1000005;j+=i)
a[j]=a[j]/i*(i-1);
}//递推公式 打表时非常好用
for(long long i=3;i<=1000005;i++){
a[i]+=a[i-1];
}
long long n;
while(scanf("%I64d",&n)!=EOF){
if(n==0)break;
printf("%I64d\n",a[n]);
}
return 0;
}
Farey Sequence(欧拉函数)的更多相关文章
- poj2478 Farey Sequence (欧拉函数)
Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...
- POJ2478 Farey Sequence —— 欧拉函数
题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K To ...
- poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)
http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...
- poj2478 Farey Sequence 欧拉函数的应用
仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值 的总和,为什么呢,因为要构成 a/b 然后不能约分 所以 gcd(a,b)==1,所以 分母 b的 欧拉函数值 ...
- hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数
hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...
- UVA12995 Farey Sequence [欧拉函数,欧拉筛]
洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...
- poj 2478 Farey Sequence 欧拉函数前缀和
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Description The Farey Sequence Fn for ...
- Poj 2478-Farey Sequence 欧拉函数,素数,线性筛
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 5647 D ...
- POJ-2478-Farey Sequence(欧拉函数)
链接: https://vjudge.net/problem/POJ-2478 题意: The Farey Sequence Fn for any integer n with n >= 2 i ...
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- js冒泡法和数组转换成字符串
js代码: window.onload = function(){ var mian = document.getElementById( "mian" ); var mian1 ...
- One or more files are in a conflicted state
http://blog.csdn.net/caiwenfeng_for_23/article/details/37501249 解决代码冲突 如果commit时出现“You have to updat ...
- 算法笔记_009:字符串匹配(Java)
1 问题描述 给定一个n个字符组成的串(称为文本),一个m(m <= n)的串(称为模式),从文本中寻找匹配模式的子串. 2 解决方案 2.1 蛮力法 package com.liuzhen.c ...
- Mysql Field * doesn't have a default value解决方法
Mysql Field * doesn't have a default value解决方法 MySQL 5中,出现错误提示: Field 'id' doesn't have a default va ...
- Creating a Fragment: constructor vs newInstance()
from stack overflow and another chapter I recently grew tired of constantly having to know String ke ...
- 【Oracle】表空间相关集合
创建表空间 1.语法 CREATE [SMALLFILE/BIGFILE] TABLESPACE tablespace_nameDATAFILE '/path/filename' SIZE num[k ...
- django 实现linux运维管理平台
概述 使用django实现一个linux运维管理平台,可以实现注册登录,机器管理 ,服务器批量操作,服务器性能监控. 详细 代码下载:http://www.demodashi.com/demo/112 ...
- springmvc sitemesh json问题
参考: 解决方法: <sitemesh> <mapping path="/*" decorator="/WEB-INF/views/template/t ...
- 神经网络中 BP 算法的原理与 Python 实现源码解析
最近这段时间系统性的学习了 BP 算法后写下了这篇学习笔记,因为能力有限,若有明显错误,还请指正. 什么是梯度下降和链式求导法则 假设我们有一个函数 J(w),如下图所示. 梯度下降示意图 现在,我们 ...
- List多个字段标识过滤
class Program { public static void Main(string[] args) { List<T> list = new List<T>(); ...