(数论)51NOD 1136 欧拉函数
输入一个数N。(2 <= N <= 10^9)
输出Phi(n)。
8
4
解:简单说明一下基本情况,复杂情况可类推。若n=a^p*b^q(a,b为质数或1),则phi(n)=n-n/a-n/b+n/(a*b)=n*(1-1/a-1/b+1/(a*b))=n*(1-1/a)*(1-1/b)=n*(a-1)*(b-1)/(a*b);
所以我们可以得到如下公式phi(n)=n*(a-1)*(b-1)*(c-1).../(a*b*c...)=(n/(a*b*c...))*(a-1)*(b-1)*(c-1)...(避免大数相乘爆int);
#include <stdio.h>
#include <math.h>
#include <string.h>
#define CLR(x) memset(x,0,sizeof x) int num[]; void deco(int a)
{
int j = ;
for (int i = ; i <= sqrt((double)a) && a != ; i++)
{
if ( == a%i)
{
num[j++] = i;
do a /= i;
while (a%i == );
}
}
if (a != ) num[j++] = a;
return ;
} int main()
{
int n;
while (scanf_s("%d", &n) != EOF)
{
int temp = , mult = ;
CLR(num);
deco(n);
for (int i = ; num[i]; i++)
{
temp *= (num[i] - );
mult *= num[i];
}
printf("%d\n", n / mult * temp);
}
}
改进一下,写成函数
#include <stdio.h> int phi(int n)
{
int ans = n;
for (int i = ; i * i <= n && n != ; i++)
{
if ( == n%i)
{
ans = ans / i * (i - );
do n /= i;
while (n % i == );
}
}
if (n != ) ans = ans / n * (n - );
return ans;
} int main()
{
int n;
while (scanf_s("%d", &n) != EOF) printf("%d\n", phi(n));
return ;
}
(数论)51NOD 1136 欧拉函数的更多相关文章
- 51Nod 1136 欧拉函数 Label:数论
对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...
- [51Nod 1244] - 莫比乌斯函数之和 & [51Nod 1239] - 欧拉函数之和 (杜教筛板题)
[51Nod 1244] - 莫比乌斯函数之和 求∑i=1Nμ(i)\sum_{i=1}^Nμ(i)∑i=1Nμ(i) 开推 ∑d∣nμ(d)=[n==1]\sum_{d|n}\mu(d)=[n== ...
- 51nod 1239 欧拉函数之和(杜教筛)
[题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 [题目大意] 计算欧拉函数的前缀和 [题解] 我们 ...
- 陕西师范大学第七届程序设计竞赛网络同步赛 J 黑猫的小老弟【数论/法拉数列/欧拉函数】
链接:https://www.nowcoder.com/acm/contest/121/J来源:牛客网 题目描述 大家知道,黑猫有很多的迷弟迷妹,当然也有相亲相爱的基友,这其中就有一些二五仔是黑猫的小 ...
- 1370 - Bi-shoe and Phi-shoe(LightOJ1370)(数论基础,欧拉函数)
http://lightoj.com/volume_showproblem.php?problem=1370 欧拉函数: 在数论,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. φ(n) ...
- acm数论之旅--欧拉函数的证明
随笔 - 20 文章 - 0 评论 - 73 ACM数论之旅7---欧拉函数的证明及代码实现(我会证明都是骗人的╮( ̄▽ ̄)╭) https://blog.csdn.net/chen_ze_hua ...
- 【bzoj3813】: 奇数国 数论-线段树-欧拉函数
[bzoj3813]: 奇数国 题意:给定一个序列,每个元素可以分解为最小的60个素数的形式.(x=p1^k1*p2^k2*......p60^k60)(p1=2,p2=3,…,p60=281) 支持 ...
- 51Nod 1239 欧拉函数前n项和 杜教筛
http://www.51nod.com/Challenge/Problem.html#!#problemId=1239 AC代码 #include <bits/stdc++.h> #de ...
- 51nod 1040 欧拉函数
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1040 1040 最大公约数之和 题目来源: rihkddd 基准时间限制 ...
随机推荐
- c#读取MySQL数据表中的内容
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- xtu summer individual 3 F - Opening Portals
Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- fread了解一下
神奇读入挂^_^ 记得加头文件#include const int BufferSize=100*1000; char buffer[BufferSize],*head,*tail; bool not ...
- 七牛云上传图片到cdn,cdn返回的是一个只有图片的url
如:
- ppc_85xx-gcc -shared -fPIC liberr.c -o liberr.so
fPIC作用于编译阶段,告诉编译器产生与位置无关代码(Position-Independent Code), 则产生的代码中,没有绝对地址,所有使用相对地址.故而代码能够被载入器载入到内存的随意 ...
- win7开启超级管理员账户(Administrator)
win7开启超级管理员账户(Administrator) 不同于XP系统,Windows7系统据说出于安全的考虑,将超级管理员帐户"Administrator"在登陆界面给隐藏了, ...
- 使用OpenCV读、操作、写图像并与bash合作对某个文件夹下全部图像进行相似处理
我门要对某个文件夹下全部图像文件进行统一处理,假设图像的数量过多.那么手动地一张张处理就会显得有些麻烦.本文使用OpenCV和bash来完毕我们指定的任务. 任务 将文件夹A下的全部统一格式的jpg图 ...
- Akka并发编程——第五节:Actor模型(四)
本节主要内容: 1. 停止Actor 1. 停止Actor (1)通过ActorSystem.shutdown方法停止全部 Actor的执行 /* *停止Actor:ActorSystem.shutd ...
- OCP-1Z0-051-题目解析-第21题
21. Examine the description of the EMP_DETAILS table given below: name NULL ...
- linux 线程同步(二)
信号量 信号量是相互排斥锁的升级版把相互排斥锁中1变成了n.举个简单的样例:如果如今有10个人,有一部手机.这10个人都竞争来使用手机打电话这就是相互排斥锁.对于信号量,如今可能是有4部手机,这10个 ...