POJ 2407 Relatives】的更多相关文章

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…
裸欧拉函数. #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]<…
Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13920   Accepted: 6965 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 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 , y > , z > such that a = xy and b = xz. Input There are several test cases. For each test ,,,. A li…
题目链接 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的全部质因数   就是要求这种式子啦,只是求这条式子.相信有非…
题目链接:http://poj.org/problem?id=2407 Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13599   Accepted: 6772 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers…
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 <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #incl…
题意就是求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,问你在区间[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]=;…
欧拉函数: 对正整数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本身). …
#include <cstdio> #include <iostream> using namespace std; int phi(int x) { int t=x; ;i<=x;i++) { ) { t=t/i*(i-); x=x/i; } ) { x/=i; } } return t; } int main() { //freopen("in.txt","r",stdin); int n; ) { cout << ph…
裸 的求欧拉函数 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int main(){ int n; while(scanf("%d",&n),n){ int res=n; int L=(int)sqrt(n*1.0); // cout…
题目:给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内).(N<=10000000,M<=100000) 解法:1.欧拉筛O(n),数组近乎100KB:2.(我这题copy了数据范围肯定是有原因滴......)欧拉函数判断素数O(m log n),m 比 n 小啊,可以分解质因数求欧拉函数. 2种写法我在这篇博文里都有写:[poj 2407]Relatives(数论--欧拉函数 模版题) 下面的代码是第一种方法的, 1 #include<cstdio>…
题意: 求出来区间[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;…
POJ 2891 x=r1 (mod a1) x=r2 (mod a2) x=a1*x+r1,x=a2*y+r2; a1*x-a2*y=r2-r1; 用Extend_Gcd求出m1*x+m2*y=d; d=Gcd(x,y); 那么就可以解出原来的x=(x*(r2-r1)/d) 那么代入原式r1=a1*x+r1 新的a1=lcm(a1,a2); #include <cstdio> #define LL long long LL a1,a2,r1,r2,x,y,n; LL Extend_Gcd(L…
欧拉函数: 对正整数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; }…
[SinGuLaRiTy-1020] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [CQBZOJ 1464] Hankson 题目描述 Hanks博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson正在思考一个有趣的问题. 今天在课堂上,老师讲解了如何求两个正整数c1和c2的最大公约数和最小公倍数.现在Hankson认为自己已经熟练地掌握了这些知识,他开始思考一个“…
素数筛 朴素算法 一般来说,可以用试除法判断某一个数是不是素数: bool isPrime(int n) { if(n < 2) return false; for(int i = 2; i < n; i++) if(n % i == 0) return false; return true; } 但其实我们只需要试除到根号n即可,因为对于任意的n,假设存在一个大于根号n的因数,那么肯定存在一个小于根号n的因数与之对应.那么有: bool isPrime(int n) { if(n <…
题目链接:http://poj.org/problem?id=2021 思路分析:由于数据较小,采用O(N^2)的暴力算法,算出所有后代的年龄,再排序输出. 代码分析: #include <iostream> #include <string.h> #include <algorithm> using namespace std; #define MAX_N ( 100 + 10 ) typedef struct Descendant { ]; int Age; }De…
Relative Relatives Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3339   Accepted: 1462 Description Today is Ted's 100th birthday. A few weeks ago, you were selected by the family to contact all of Ted's descendants and organize a surpr…
题意: 今天是Ted的100岁生日.凑巧的是,他家族里面每个人都跟他同一天生日,但是年份不同. 现在只给出一些 父亲的名字,孩子的名字,以及孩子出生时父亲的年龄, 要求将Ted以外的家族成员按年龄降序排序,如果年龄相同,则按字母排序. 思路:遍历树. 根据题意,可通过父子关系建立一个有权无向树,Ted作为树的根节点.通过从Ted所在节点开始遍历树,给每个节点赋值. 最后根据每个成员的年龄排序即可. 这里用到map,建立名字到编号的映射,从而获取一个人在树中的节点编号. #include <ios…
此文来自北京邮电大学ACM-ICPC集训队 此50题在本博客均有代码,可以在左侧的搜索框中搜索题号查看代码. 以下是原文: POJ推荐50题1.标记“难”和“稍难”的题目可以看看,思考一下,不做要求,当然有能力的同学可以直接切掉.2.标记为A and B的题目是比较相似的题目,建议大家两个一起做,可以对比总结,且二者算作一个题目.3.列表中大约有70个题目.大家选做其中的50道,且每类题目有最低数量限制.4.这里不少题目在BUPT ACM FTP上面都有代码,请大家合理利用资源.5.50个题目要…