hdu2824 The Euler function 筛选法求欧拉函数模板题

//求a , b范围内的所有的欧拉函数

//筛选法求欧拉函数模板题

#include<cstdio>

#include<cstring>

#include<iostream>

using namespace std ;

const int maxn = 3000010 ;

typedef __int64 ll ;

int e[maxn] ;

int a ,  b ;

void Euler()

{

int i,j;

for (i=1;i<maxn;i++) e[i]=i;

for (i=2;i<maxn;i++)

if (e[i]==i)  // 当e[i] == i时,可以发现i即为素数

{

e[i]=i-1;

for (j=i*2;j<maxn;j+=i) e[j]=e[j]/i*(i-1);

}

}

int main()

{

Euler() ;

while(~scanf("%d%d" , &a , &b))

{

ll ans = 0 ;

for(int i = a ;i <= b;i++)

ans += (ll)e[i] ;

printf("%I64d\n" , ans) ;

}

return 0 ;

}

时间: 2024-08-02 06:54:31

hdu2824 The Euler function 筛选法求欧拉函数模板题的相关文章

hdu2824 The Euler function O(n)求欧拉函数

hdu2824 The Euler function O(n)求欧拉函数 1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std ; 4 5 const int N = 3000011 ; 6 int l,r ; 7 int prime[ 300011 ],phi[N] ; 8 ll sum ; 9 bool f[N] ; 10 11 inline void Euler_init(int NN) 12 {

POJ 2478 Farey Sequence 筛选法求欧拉函数

题目来源:POJ 2478 Farey Sequence 题意:输入n 求 phi(2)+phi(3)+phi(4)+...+phi(n) 思路:用类似筛法的方式计算phi(1), phi(2), ..., phi(n) 再求前缀和 #include <cstdio> #include <cstring> #include <cmath> //欧拉phi函数 const int maxn = 1000010; typedef long long LL; int eule

(hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)

题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 166 Accepted Submission(s): 96   Problem Description The Euler function phi is an important kind of function in number theory

The Euler function(线性筛欧拉函数)

/* 题意:(n)表示小于n与n互质的数有多少个,给你两个数a,b让你计算a+(a+1)+(a+2)+......+b; 初步思路:暴力搞一下,打表 #放弃:打了十几分钟没打完 #改进:欧拉函数:具体证明看po主的博客 ^0^ #超时:这里直接用欧拉函数暴力搞还是不可以的,用到线性筛欧拉函数,这里总和爆int,要用long long */ #include<bits/stdc++.h> #define ll long long using namespace std; /***********

poj2407(欧拉函数模板题)

题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p2,...,pn,然后根据Φ(m)=m*∏(1-1/pi)计算即可. AC代码: #include<cstdio> using namespace std; int n,ans; int main(){ while(scanf("%d",&n),n){ ans=n; f

数论 - 欧拉函数模板题 --- poj 2407 : Relatives

Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if ther

hdu1286找新朋友 欧拉函数模板题

#include<cstdio> #include<cstring> #include<iostream> using namespace std ; int Euler(int n) { int rea = n ; for(int i = 2;i*i <= n;i++) { if(n%i == 0) rea -= rea/i ; while(n%i == 0) n/=i ; } if(n>1) rea-=rea/n ; return rea ; } int

POJ 1284 Primitive Roots 欧拉函数模板题

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <stack> #include <map> #include <ctime> #include <io

POJ2478_Farey Sequence【快速求欧拉函数】

Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12377 Accepted: 4808 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) = 1