Farey Sequence POJ - 2478 (欧拉函数 前缀和)

Farey Sequence POJ - 2478

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

题目:

法理序列Fn是指对于任意整数n( n >= 2),由不可约的分数a/b(0 < a < b <= n),gcd(a,b) = 1升序排列构成的序列,最开始的几个如下
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中的元素个数。

Input

输入包含多组样例. 每组样例仅一行, 有一个正整数n (2 <= n <= 10
6). 两组样例间无空行. 0代表输入结束.

Output

对于每种情况,你需要输出法理序列Fn中包含元素的个数

Sample Input

2
3
4
5
0

Sample Output

1
3
5
9思路:1=13=1+25=1+2+29=1+2+2+4...由于1 2 2 4 4 ... 是欧拉值,故很容易想到是求欧拉值的前缀和,所以第一步将欧拉值打表出来,后来我一开始遍历用for循环求和,超时了,所以这里还是要将前缀和存入表内,由于是从下标为2开始的,故求前缀和从下标为3开始

//
// Created by hanyu on 2019/8/9.
//
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include<math.h>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=2e6+7;
ll euler[maxn];
void value()
{
    memset(euler,0,sizeof(euler));
    euler[1]=1;
    for(int i=2;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-1);
            }
        }
    }
    for(int i=3;i<=maxn;i++)
        euler[i]+=euler[i-1];
}
int main()
{
    int n;
    value();
    while(~scanf("%d",&n)&&n)
    {
        printf("%lld\n",euler[n]);
    }
    return 0;
}


原文地址:https://www.cnblogs.com/Vampire6/p/11329981.html

时间: 2024-07-30 13:20:33

Farey Sequence POJ - 2478 (欧拉函数 前缀和)的相关文章

POJ 2478 欧拉函数打表的运用

http://poj.org/problem?id=2478 此题只是用简单的欧拉函数求每一个数的互质数的值会超时,因为要求很多数据的欧拉函数值,所以选用欧拉函数打表法. PS:因为最后得到的结果会很大,所以结果数据类型不要用int,改为long long就没问题了 #include <iostream> #include <stdio.h> using namespace std; #define LL long long LL F[1000100]; int phi[10001

POJ 2478 欧拉函数(欧拉筛法) HDU 1576 逆元求法

相关逆元求法,我之前有写过,还有欧拉函数的求法,欧拉函数与逆元的关系  点击 POJ 2478 又是一个打表的题目,一眼看出结果就是前n个欧拉函数值的和. 这里直接计算欧拉函数值求和会超时,看见多组数据. 然后就是计算欧拉函数,打表就好了. #include <stdio.h> #include <string.h> #include <iostream> using namespace std; typedef long long LL; const int N =

【欧拉函数】欧拉函数前缀和

转自http://www.cnblogs.com/chanme/p/4457200.html H.   Game Alice likes to play games. One day she meets such a game. There are N * N switches arranged in an N * N array. Pressing a switch would change its state, from off to onor from on to off. In addi

poj 3090 (欧拉函数,找规律)

poj 3090 (欧拉函数,找规律) 题目: 给出一个n*n的点阵,求从(0,0)出发斜率不相等的直线有多少条. 限制: 1 <= n <= 1000 思路: 先定义sum[i] sum[i] = 0, if(i == 1) sum[i] = sum[i-1] + phi[i], if(i >= 2) ans = sum[n] * 2 + 3 /*poj 3090 题目: 给出一个n*n的点阵,求从(0,0)出发斜率不相等的直线有多少条. 限制: 1 <= n <= 100

poj 2480 欧拉函数+积性函数+GCD

题目:http://poj.org/problem?id=2480 首先要会欧拉函数:先贴欧拉函数的模板,来源于吉林大学的模板: //欧拉函数PHI(n)表示的是比n小,并且与n互质的正整数的个数(包括1). unsigned euler(unsignedx) {// 就是公式 unsigned i, res=x; for(i = 2; i < (int)sqrt(x * 1.0) + 1; i++) if(x%i==0) { res = res / i * (i - 1); while(x %

欧拉函数前缀和

今天软院校赛,有一道H题非常的神,所以记下来.题意转化了之后就是求欧拉函数的前缀和.自然的想法是O(n)的线性预处理可以求出前n个数的欧拉函数,又或者是O(sqrt(n))的预处理求出单个数的欧拉函数.但是题目要求的是前n(n<=10^9)个数欧拉函数的前缀和.于是我就觉得这是没法做的了,赛后问了出题人,出题人非常好的给了下面的这个链接. http://wzimpha.sinaapp.com/archives/596 然后大体的思路就在里面,这里也不重复了.主要是觉得这种方法好神,所以特别的在这

ligtoj 1007 - Mathematically Hard(欧拉函数+前缀和)

1007 - Mathematically Hard PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 64 MBMathematically some problems look hard. But with the help of the computer, some problems can be easily solvable. In this problem, you will be given tw

POJ 3090 欧拉函数

求一个平面内可见的点,其实就是坐标互质即可,很容易看出来或者证明 所以求对应的欧拉函数即可 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int phi[1010]; int n; void calc(int x) { for (int i=2;i<=x;i++) phi[i]=0; phi[1]=1;

poj 3696 欧拉函数

poj 3696 题意: 给出一个数字L,求出最短的888...8能被L整除,输出最短的长度. 限制: 1 <= L <= 2*10^9 思路: 设x为最小长度 888...8=(10^x-1)/9*8 由题意得: (10^x-1)/9*8 % L=0 -> (10^x-1)*8 % (9L) = 0 -> (10^x-1) % (9L/gcd(L,8)) = 0 -> 10^x % (9L/gcd(L,8)) = 1 这个是一个离散对数的问题,第一个想到的是用拓展BSGS做