当前位置:网站首页>数学知识(欧拉函数)
数学知识(欧拉函数)
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;
}
边栏推荐
- 2022-003arts: recursive routine of binary tree
- C - derived classes and constructors
- VMware installation win10 reports an error: operating system not found
- Homework of the 16th week
- Record the bug of unity 2020.3.31f1 once
- Markdown edit syntax
- DJB Hash
- Mysql database learning
- Ognl和EL表达式以及内存马的安全研究
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
猜你喜欢
Websites that it people often visit
VMware installation win10 reports an error: operating system not found
解析少儿编程中的动手搭建教程
深圳打造全球“鸿蒙欧拉之城”将加快培育生态,优秀项目最高资助 1000 万元
[C language] basic learning notes
万卷共知,一书一页总关情,TVP读书会带你突围阅读迷障!
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Leetcode- insert and sort the linked list
Idea autoguide package and autodelete package Settings
How to write a client-side technical solution
随机推荐
DJB Hash
千亿市场规模医疗美容行业的水究竟有多浑?
【毕业季·进击的技术er】年少有梦,何惧彷徨
Federal learning: dividing non IID samples according to Dirichlet distribution
培养中小学生对教育机器人的热爱之心
Mapping location after kotlin confusion
Summary of main account information of zhengdaliu 4
My first experience of shadowless cloud computer
Cache consistency solution - how to ensure the consistency between the cache and the data in the database when changing data
解析少儿编程中的动手搭建教程
Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)
解决:代理抛出异常错误
Hcip day 17
Leetcode- insert and sort the linked list
Major domestic quantitative trading platforms
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Pytorch---使用Pytorch进行鸟类的预测
Steam教育的实际问题解决能力
Keil compilation code of CY7C68013A
idea自動導包和自動删包設置