当前位置:网站首页>数学知识(欧拉函数)
数学知识(欧拉函数)
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;
}
边栏推荐
- Use of Baidu map
- Mysql表insert中文变?号的问题解决办法
- LeetCode-对链表进行插入排序
- Free drawing software recommended - draw io
- [C language] Dynamic Planning --- from entry to standing up
- Idea autoguide package and autodelete package Settings
- Win10 disk management compressed volume cannot be started
- Leetcode merge sort linked list
- Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
- Comp 250 parsing
猜你喜欢

Orthogonal test method and function diagram method for test case design

Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知

Promise all()

Lm09 Fisher inverse transform inversion mesh strategy

VMware installation win10 reports an error: operating system not found
![[C language] Dynamic Planning --- from entry to standing up](/img/7e/29482c8f3970bb1a40240e975ef97f.png)
[C language] Dynamic Planning --- from entry to standing up

CY7C68013A之keil编译代码

June book news | 9 new books are listed, with a strong lineup and eyes closed!

Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel

正大美欧4的主账户关注什么数据?
随机推荐
Unity particle Foundation
Common errors of dmrman offline backup
LeetCode-归并排序链表
idea自动导包和自动删包设置
CorelDRAW Graphics Suite2022免费图形设计软件
Markdown edit syntax
MySQL table insert Chinese change? Solution to the problem of No
Mapping location after kotlin confusion
Learn BeanShell before you dare to say you know JMeter
Federal learning: dividing non IID samples according to Dirichlet distribution
Promise all()
Lm09 Fisher inverse transform inversion mesh strategy
Mouse events in JS
Design and implementation of general interface open platform - (44) log processing of API services
Change deepin to Alibaba image source
解析少儿编程中的动手搭建教程
How to write a client-side technical solution
C language guessing numbers game
Introduction to Luogu 3 [circular structure] problem list solution
Realize the function of data uploading

化简得:

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