poj2478--欧拉函数打表

此题中对时间有要求,如直接使用欧拉函数求解,每输入一个n,就得进行循环求出<n的每个数的欧拉函数,

这样会超时,

于是我们可预先将欧拉函数打表,

再进行一个循环加法运算,便可不超时得解。

#include<iostream>
#define Max 1000001
using namespace std;
//欧拉函数打表
long long euler[Max];
void Init(){
    euler[1]=1;
    for(int i=2;i<Max;i++)
    euler[i]=i;
    for(int i=2;i<Max;i++)
    if(euler[i]==i)
    for(int j=i;j<Max;j+=i)
    euler[j]=euler[j]/i*(i-1);
}

int main(){
    long long n,sum;
    Init();
    while(cin>>n,n){
        sum=0;
        for(int i=2;i<=n;i++){
            sum+=euler[i];
        }
        cout<<sum<<endl;
    }
    return 0;
}
时间: 2024-08-03 21:43:32

poj2478--欧拉函数打表的相关文章

poj2478 欧拉函数水题

poj2478 欧拉函数水题 Y - Farey Sequence Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2478 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational number

A - Bi-shoe and Phi-shoe (欧拉函数打表)

Description Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular coach for his success. He needs some bamboos for his students, so he asked his assistant Bi-Shoe to go to the market and buy them. Plenty of

hdu 2824 The Euler function 欧拉函数打表

The Euler function Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are sm

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

poj2478欧拉函数

打表欧拉函数,求2到n的欧拉函数和 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cassert> #include<iomanip> #include<cstdlib> #include<c

poj2478 (欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17894   Accepted: 7179 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)

LightOJ - 1370 Bi-shoe and Phi-shoe (欧拉函数打表)

题意:给N个数,求对每个数ai都满足最小的phi[x]>=ai的x之和. 分析:先预处理出每个数的欧拉函数值phi[x].对于每个数ai对应的最小x值,既可以二分逼近求出,也可以预处理打表求. #include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 1100005; int phi[maxn]; int res[maxn]; bool isprime[maxn]; void E

LightOJ 1370 Bi-shoe and Phi-shoe(欧拉函数+打表)

/* 题意: 对于每个数字a[i]找到一个数num[i],num[i]的欧拉函数值大于等于a[i], 求找到的所有数的最小和. */ #include <algorithm> #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <map> using namespace std; typedef long long LL;

欧拉函数与欧拉打表解决实际问题

1.欧拉函数的定义: 欧拉函数phi(x)等于不超过x且与x互素的整数的个数. 2.欧拉函数的求法:推导过程见随笔<欧拉函数与容斥原理>. 3.代码实现欧拉函数: 1 int euler_phi(int n) 2 { 3 int m=(int)sqrt(n+0.5);//取一半就行,简化计算 4 int ans=n; 5 for(int i=2;i<=m;i++) 6 if(n%i==0)//找素因子 7 { 8 ans=ans/i*(i-1);//公式的运用 9 while(n%i==

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

链接: https://vjudge.net/problem/POJ-2478 题意: 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 =