【poj 2407】Relatives(数论--欧拉函数 模版题)
题意就是求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 以内的倍数}求出。
P.S. n 不是10^6以内分解质因数并求解时要注意啊,我WA了7次! %>_<%
P.P.S. 这题数据弱,不用欧拉筛更快......不写mn_prim,而用标记数组也是可以的。
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<cmath>
5 #include<iostream>
6 using namespace std;
7 #define N (int)1e9
8 #define M (int)1e6
9 typedef long long LL;
10
11 int phi[M+10],prim[M+10],mn_prim[M+10];
12 int pr=0;
13
14 void get_prime()
15 {
16 memset(mn_prim,0,sizeof(mn_prim));
17 for (int i=2;i<=M;i++)
18 {
19 if (!mn_prim[i])
20 {
21 prim[++pr]=i;
22 phi[i]=i-1;
23 }
24 for (int j=1;j<=pr,i*prim[j]<=M;j++)
25 {
26 mn_prim[i*prim[j]]=prim[j];
27 if (i%prim[j]!=0)
28 phi[i*prim[j]]=phi[i]*phi[prim[j]];
29 else
30 {
31 phi[i*prim[j]]=phi[i]*prim[j];
32 break;
33 }
34 }
35 }
36 }
37 int main()
38 {
39 get_prime();
40 int n;
41 while (1)
42 {
43 scanf("%d",&n);
44 if (!n) break;
45 if (n<=M-10) printf("%d\n",phi[n]);
46 else
47 {
48 int cnt=n,t=n;
49 for (int i=2;i<=t;i++)//t
50 {
51 if (t%i==0)
52 {
53 cnt-=cnt/i;// cnt*(1-1/i);
54 while (t%i==0) t/=i;
55 }
56 if (t==1) break;
57 }
58 printf("%d\n",cnt);
59 }
60 }
61 return 0;
62 }
【poj 2407】Relatives(数论--欧拉函数 模版题)的更多相关文章
- POJ 2407 Relatives(欧拉函数入门题)
Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...
- POJ 2407 Relatives(欧拉函数)
http://poj.org/problem?id=2407 题意: 给出一个n,求小于等于的n的数中与n互质的数有几个. 思路: 欧拉函数的作用就是用来求这个的. #include<iostr ...
- POJ 2407 Relatives 【欧拉函数】
裸欧拉函数. #include<stdio.h> #include<string.h> ; int p[N],pr[N],cnt; void init(){ ;i<N;i ...
- POJ 2407 Relatives (欧拉函数)
题目链接 Description Given n, a positive integer, how many positive integers less than n are relatively ...
- POJ 2407 Relatives【欧拉函数】
<题目链接> 题目大意: Given n, a positive integer, how many positive integers less than n are relativel ...
- hdu 1286 找新朋友 欧拉函数模版题
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- 数论 - 欧拉函数模板题 --- poj 2407 : Relatives
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11372 Accepted: 5544 Descri ...
- hdu1286(找新朋友)&&POJ2407Relatives(欧拉函数模版题)
http://acm.hdu.edu.cn/showproblem.php?pid=1286 没什么好说的,模板题,主要是弄懂欧拉函数的思想. #include <iostream> #i ...
- POJ_2407 Relatives 【欧拉函数裸题】
一.题目 Given n, a positive integer, how many positive integers less than n are relatively prime to n? ...
随机推荐
- 一文彻底理解IO多路复用
在讲解IO多路复用之前,我们需要预习一下文件以及文件描述符. 什么是文件 程序员使用I/O最终都逃不过文件. 因为这篇同属于高性能.高并发系列,讲到高性能.高并发就离不开Linux/Unix,因此这里 ...
- o_direct刷新方式和文件系统支持Direct i/o
若让innodb使用o_direct刷新方式,文件系统支持Direct i/o 是非常重要的.为啥
- 如何构建一个多人(.io) Web 游戏,第 2 部分
原文:How to Build a Multiplayer (.io) Web Game, Part 2 探索 .io 游戏背后的后端服务器. 上篇:如何构建一个多人(.io) Web 游戏,第 1 ...
- SAP FTP FOR ABAP programing
近来忙的不可开交,忙的一塌糊涂,呵呵,今天怀揣愧疚之心,上来分享博文一篇,算是对自己的一点安慰. 首先在SAP系统中提供了很多的FTP示例程序,如下: RSFTP001 SAPFT ...
- libuv中实现tcp服务器
目录 1.说明 2.libuv的tcp server 3.API简介 3.1.uv_tcp_init 3.2.uv_ip4_addr 3.3.uv_tcp_bind 3.4.uv_listen 3.5 ...
- Vue基础之插值表达式的另一种用法!附加变量的监听!
Vue基础之插值表达式的另一种用法!附加变量的监听! 讲这个之前我们先回顾一下原来的用法! <body> <!-- Vue.js的应用可以分为两个重要的组成部分 一个是视图! 另一个 ...
- linux通过ntp同步时间
1.安装服务 yum install ntp ##安装ntp服务,这个和ntpdate不一样哦,用这个比较好 systemctl start ntpd.service ###启动服务 systemct ...
- 让绝对定位的div居中
最近看到一个问题就是让绝对定位的div居中,在尝试了top:50%:left:50%:后发现,居中是有问题的并不是想象中的样子 需要再加两句margin-top:-盒子高度的一般px margin- ...
- Slack 的想法很好啊,很有创新,牛。
[原]https://www.leiphone.com/news/201411/aXHUpe4ZFI2sSwpb.html 由于以往一些用于办公的应用反响平平,因此对迅速崛起的办公交流应用Slack, ...
- GRPC Health Checking Protocol Unavailable 14
https://github.com/grpc/grpc/blob/master/doc/health-checking.md GRPC Health Checking Protocol Health ...