Relatives POJ - 2407 欧拉函数】的更多相关文章

题意: 给你一个正整数n,问你在区间[1,n)中有多少数与n互质 题解: 1既不是合数也不是质数(1不是素数) 互质是公约数只有1的两个整数,叫做互质整数.公约数只有1的两个自然数,叫做互质自然数 所以1与任何整数都互质 根据欧拉函数求解 欧拉函数是少于或等于n的数中与n互质的数的数目. 欧拉函数的性质:它在整数n上的值等于对n进行素因子分解后,所有的素数幂上的欧拉函数之积. 欧拉函数的值 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)-..(1-1/pn),…
题目链接: http://poj.org/problem?id=2407 题目大意:求小于n且与n互质的正整数个数. 解题思路: 欧拉函数=小于n且与n互质的正整数个数. 公式=n*(1-1/P1)*(1-1/P2)....*(1-1/Pn),其中Pn为不同的质因数. 欧拉函数的求法有好多. 最简单的是手艹质因数分解,然后套公式计算. 注意特判1的时候ans=0. #include "cstdio" #include "map" using namespace st…
http://poj.org/problem?id=2407 题意:多组数据,每次输入一个数 ,求这个数的欧拉函数 int euler_phi(int n){//单个欧拉函数 int m=(int)sqrt(n+0.5); int ans=n; ;i<=m;i++)){ ans=ans/i*(i-); )n/=i; } )ans=ans/n*(n-); } 单个欧拉函数 int phi[maxn]; void phi_table(int n){//函数表 ;i<=n;i++)phi[i]=;…
题意就是求10^9以内的正整数的欧拉函数(Φ(n)表示<=n的与n互质的正整数个数). 解法:用欧拉筛和欧拉函数的一些性质:    1.若p是质数,Φ(p)=p-1:    2.欧拉函数是积性函数,即若a,b互质,则Φ(ab)=Φ(a)*Φ(b):    3.若a,b不互质,则Φ(ab)=Φ(a)*b. 若 n≤10^6,可以通过欧拉筛用数组预处理得出:若不是,再分解质因数,利用Φ(n)=n*(1-1/p1)*(1-1/p2)*...*(1-1/pk) {除去各质因数的 n 以内的倍数}求出.…
欧拉函数: 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 对于一个正整数N的素数幂分解N=P1^q1*P2^q2*...*Pn^qn. Euler函数表达通式:euler(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…(1-1/pn),或者φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn), 其中p1,p2……pn为x的所有素因数,x是不为0的整数. euler(1)=1(唯一和1互质的数就是1本身). …
http://poj.org/problem?id=2478 此题只是用简单的欧拉函数求每一个数的互质数的值会超时,因为要求很多数据的欧拉函数值,所以选用欧拉函数打表法. PS:因为最后得到的结果会很大,所以结果数据类型不要用int,改为long long就没问题了 #include <iostream> #include <stdio.h> using namespace std; #define LL long long LL F[]; ]; void phi_table(in…
求一个平面内可见的点,其实就是坐标互质即可,很容易看出来或者证明 所以求对应的欧拉函数即可 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; ]; int n; void calc(int x) { ;i<=x;i++) phi[i]=; phi[]=; ;i<=x;i++){ if (!phi[i]…
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 , y > , z > such that a = xy and b = xz. Input There are several test cases. For each test ,,,. A li…
题意: UVa 10820 这两个题是同一道题目,只是公式有点区别. 给出范围为(0, 0)到(n, n)的整点,你站在原点处,问有多少个整点可见. 对于点(x, y), 若g = gcd(x, y) > 1,则该点必被点(x/g, y/g)所挡住. 因此所见点除了(1, 0)和(0, 1)满足横纵坐标互素. 最终答案为,其中的+3对应(1, 1) (1, 0) (0, 1)三个点 #include <cstdio> ; ]; void get_table() { ; i <= m…
题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostream> 4 #include<algorithm> 5 #include<math.h> 6 using namespace std; 7 typedef long long ll;…
欧拉函数: 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. POJ 2407.Relatives-欧拉函数 代码O(sqrt(n)): ll euler(ll n){ ll ans=n; ;i*i<=n;i++){ //这里i*i只是为了减少运算次数,直接i<=n也没错, ){ //因为只有素因子才会加入公式运算.仔细想一下可以明白i*i的用意. ans=ans/i*(i-); ) n/=i; //去掉倍数 } } ) ans=ans/n*(n-); return ans; }…
http://poj.org/problem?id=2407 题意: 给出一个n,求小于等于的n的数中与n互质的数有几个. 思路: 欧拉函数的作用就是用来求这个的. #include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cmath> using namespace std; int n; int main() { //freopen(&…
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…
Relatives 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 there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. Input There are several t…
Relatives AC代码 Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16186   Accepted: 8208 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relativel…
裸欧拉函数. #include<stdio.h> #include<string.h> ; int p[N],pr[N],cnt; void init(){ ;i<N;i++){ if(!p[i])pr[++cnt]=i; ;j<=cnt&&i*pr[j]<N;j++){ p[i*pr[j]]=; ) break; } } } int er(int n){ int ans=n; ;i<=cnt&&pr[i]*pr[i]<…
题目链接 题意 : 求小于等于n中与n互质的数的个数. 思路 : 看数学的时候有一部分是将欧拉函数的,虽然我没怎么看懂,但是模板我记得了,所以直接套了一下模板. 这里是欧拉函数的简介. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; int main() { int x…
题目链接 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 there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. Input There are se…
<题目链接> 题目大意: 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 there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz. 解题分析: 其实只要看懂题目就会…
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article/details/35774889 最主要的欧拉函数: 欧拉函数:求小于n的与n互质的个数   欧兰函数公式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)-..(1-1/pn),当中p1, p2--pn为x的全部质因数   就是要求这种式子啦,只是求这条式子.相信有非…
通式: $\phi(x)=x(1-\frac{1}{p_1})(1-\frac{1}{p_2})(1-\frac{1}{p_3}) \cdots (1-\frac{1}{p_n})$ 若n是质数p的k次幂:$\phi(n)=p^k-p^{k-1}=(p-1)p^{k-1}$,因为除了p的倍数外,其他数都跟n互质. 设n为正整数,以$\phi(n)$表示不超过n且与n互素的正整数的个数,称为n的欧拉函数值,这里函数φ:N→N,n→φ(n)称为欧拉函数. 欧拉函数是积性函数——若m,n互质, $\p…
题目链接: http://poj.org/problem?id=2480 题目大意:求Σgcd(i,n). 解题思路: 如果i与n互质,gcd(i,n)=1,且总和=欧拉函数phi(n). 如果i与n不互质,那么只要枚举n的全部约数,对于一个约数d,若使gcd(i/d,n/d)互质,这部分的gcd和=d*欧拉函数phi(n/d). 不断暴力从小到大枚举约数,这样就把gcd和切成好多个部分,累加起来就行了. 其实还可以公式化简,不过实在太繁琐了.可以参考金海峰神的解释. 由于要求好多欧拉函数,每次…
A - 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 numbers a/b with 0 &l…
Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 5647 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)…
http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2.欧拉定理:若a与n互质.那么有a^φ(n) ≡ 1(mod n),经经常使用于求幂的模. 3.若p是一个质数,那么φ(p) = p-1.注意φ(1) = 1. 4.欧拉函数是积性函数: 若m与n互质,那么φ(nm) = φ(n) * φ(m). 若n = p^k且p为质数,那么φ(n) = p^k…
欧拉函数总结+证明 欧拉函数总结2 POJ 1284 原根 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int Euler(int n) { int res=n; ;i*i<=n;i++) { ) { n/=i; res-=(res/i); ) n/=i; } }…
Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8359   Accepted: 2737 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are al…
http://poj.org/problem?id=2478 http://acm.hdu.edu.cn/showproblem.php?pid=2824 欧拉函数模板裸题,有两种方法求出所有的欧拉函数,一是筛法,而是白书上的筛法. 首先看欧拉函数的性质: 欧拉函数是求小于n且和n互质(包括1)的正整数的个数.记为φ(n). 欧拉定理:若a与n互质,那么有a^φ(n) ≡ 1(mod n),经常用于求乘法逆元. 若p是一个质数,那么φ(p) = p-1,注意φ(1) = 1. 欧拉函数是积性函数…
传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2416 Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms.…
http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2*f[i]-1. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <stack> #include <vector> #inclu…