当前位置:网站首页>数学知识(欧拉函数)
数学知识(欧拉函数)
2022-07-02 04:45:00 【吴雨4】
前提:欧拉函数Φ(n)表示1~n中与n互质的数的个数。
【若两个数只有1一个共同的质因子,称为互质】
eg:ϕ(6)=2。[1,2,3,4,5,6]
那怎么求1~n中与n互质的数的个数呢?
由算数基本定理可得,任何一个数n可以用如下式子表示,其中Pi是质因数,ai为指数。
这里还要介绍一下容斥原理:
既然要算互质的数的个数,则要将质因数的倍数全部删掉,操作如下:
1)从1~n中减去p1,p2,…,pk的倍数【-奇】
2)加上所有pi*pj的倍数 【+偶】
3)减去所有pi*pj*pk的倍数 【-奇】
如此有:
化简得:
补充步骤解释:
1
2
3
4
5
6
7
第一步:
删掉Pi的倍数
-1
-1
-1
-1
删掉Pj的倍数
-1
-1
-1
-1
删掉pk的倍数
-1
-1
-1
-1
第二步:
加上pi*pj的倍数
+1
+1
加上pi*pk的倍数
+1
+1
加上pj*pk的倍数
+1
+1
第三步:
减去pi*pj*pk的倍数
-1
总共
-1
-1
-1
-1
-1
-1
-1
题目链接:873. 欧拉函数 - AcWing题库
题面:

下面求解质因数的代码可以参考这个博客:
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll t,n;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
ll ans=n;
for(int i=2;i<=n/i;i++)
{
if(n%i==0)
{
ans=ans/i*(i-1);//等价于ans*(1-1/i)
while(n%i==0) n/=i;
}
}
if(n>1) ans=ans/n*(n-1);
cout<<ans<<endl;
}
return 0;
}
边栏推荐
- win10 磁盘管理 压缩卷 无法启动问题
- Embedded-c language-9-makefile/ structure / Consortium
- LeetCode-归并排序链表
- Shenzhen will speed up the cultivation of ecology to build a global "Hongmeng Oula city", with a maximum subsidy of 10million yuan for excellent projects
- Lm09 Fisher inverse transform inversion mesh strategy
- Design and implementation of general interface open platform - (44) log processing of API services
- UNET deployment based on deepstream
- Cannot activate CONDA virtual environment in vscode
- Research on the security of ognl and El expressions and memory horse
- Keil compilation code of CY7C68013A
猜你喜欢

2022-003arts: recursive routine of binary tree

idea自动导包和自动删包设置

正大美欧4的主账户关注什么数据?

Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial

Read "the way to clean code" - function names should express their behavior

One step implementation of yolox helmet detection (combined with oak intelligent depth camera)

缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性

Orthogonal test method and function diagram method for test case design

idea自動導包和自動删包設置

VMware installation win10 reports an error: operating system not found
随机推荐
Pytoch --- use pytoch to predict birds
Pytoch --- use pytoch to realize u-net semantic segmentation
Orthogonal test method and function diagram method for test case design
Deep understanding of lambda expressions
Research on the security of ognl and El expressions and memory horse
C language practice - number guessing game
oracle 存储过程与job任务设置
Binary tree problem solving (1)
【ClickHouse】How to create index for Map Type Column or one key of it?
Solution of DM database unable to open graphical interface
Pytorch-Yolov5從0運行Bug解决:
Lm09 Fisher inverse transform inversion mesh strategy
Arbre binaire pour résoudre le problème (2)
[C language] basic learning notes
Mysql表insert中文变?号的问题解决办法
Let正版短信测压开源源码
Landing guide for "prohibit using select * as query field list"
TypeScript函数详解
Free drawing software recommended - draw io
Virtual machine installation deepin system

化简得:

https://blog.csdn.net/WSY444/article/details/125496621?spm=1001.2014.3001.5502