poj3030(欧拉函数2 O(n))

用O(n),算法优化

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=2000;

int phi[maxn],totans[maxn],prime[maxn],v[maxn];

int getphi(int n){
    int top=0;
    for (int i=2;i<=n;i++){
        if(v[i]==0){
            prime[++top]=i;
            v[i]=i;
            phi[i]=i-1;
        }
        for (int j=1;j<=top;j++){
            if(prime[j]>v[i]||prime[j]>n/i) break;
            v[i*prime[j]]=prime[j];
            phi[i*prime[j]]=phi[i]*(i%prime[j]?prime[j]-1:prime[j]);//if prime[i]整除n,且prime[i]*prime[i]整除n,那么phi[n]=phi[n/prime[i]]*prime[i]
            //else phi[n]=phi[n/prime[i]]*prime[i]-1;
        }
    }
}

int main(){
    int n;
    scanf("%d",&n);
    int v;
    phi[1]=1;
    getphi(1010);
    for (int i=2;i<=1010;i++){
        totans[i]=phi[i]+totans[i-1];
    }
    for (int i=1;i<=n;i++){
        scanf("%d",&v);
        printf("%d %d %d\n",i,v,totans[v]*2+3);
    }
return 0;
}

原文地址:https://www.cnblogs.com/lmjer/p/9090392.html

时间: 2024-11-13 10:14:37

poj3030(欧拉函数2 O(n))的相关文章

poj3030(欧拉函数)

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8101   Accepted: 4963 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

欧拉函数

void Euler_Sieve_Method(int * euler, int n) { euler[1] = 1; for (int i = 2; i < n; i++) { euler[i] = i; } for (int i = 2; i < n; i++) { if (euler[i] == i) { for (int j = i; j < n; j += i) { euler[j] = euler[j] / i * (i - 1); } } } } void Euler_Si

hdu1695(莫比乌斯)或欧拉函数+容斥

题意:求1-b和1-d之内各选一个数组成数对,问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个可以简化成1-b/k 和1-d/k 的互质有序数对的个数.假设b=b/k,d=d/k,b<=d.欧拉函数可以算出1-b与1-b之内的互质对数,然后在b+1到d的数i,求每个i在1-b之间有多少互质的数.解法是容斥,getans函数参数的意义:1-tool中含有rem位置之后的i的质因子的数的个数. 在 for(int j=rem;j<=factor[i

欧拉函数常用性质

欧拉函数定义:设n 为正整数,则1,2......,n中与n互质的整数个数记作f(n). 1.1 若n为素数,f(n)=n-1; 1.2 整数n=p*q,p,q为不同素数,则f(n)=f(p)*f(q)=(p-1)*(q-1) 1.3 n=p^a*q^b,f(n)=f(p^a)*f(q^b)=n*(1-1/p)*(1-1/q) 1.4 分解质因子相乘,f(n)=n*(1-1/p1)*(1-1/p2)*.......*(1-1/pk). f(100)=f(2^2*5^2)=100*1/2*4/5=

POJ2478(SummerTrainingDay04-E 欧拉函数)

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

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 =

算法复习——欧拉函数(poj3090)

题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For exa

欧拉函数之和(51nod 1239)

对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi(8) = 4),因为1,3,5,7均和8互质. S(n) = Phi(1) + Phi(2) + ...... Phi(n),给出n,求S(n),例如:n = 5,S(n) = 1 + 1 + 2 + 2 + 4 = 10,定义Phi(1) = 1.由于结果很大,输出Mod 1000000007的结

FZU 1759 欧拉函数 降幂公式

Description Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000). Input There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a singl