数学知识-欧拉函数&快速幂
欧拉函数
定义
对于正整数n,欧拉函数是小于或等于n的正整数中与n互质的数的数目,记作φ(n).

算法思路
既然求解每个数的欧拉函数,都需要知道他的质因子,而不需要个数
因此,我们只需求出他的质因子,然后根据公式:N*(p1-1)/p1*(p2-1)/p2......即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n; int main()
{
ll i,j;
cin>>n;
for(i=0;i<n;i++)
{
ll x;
cin>>x;
ll ans=x;
for(j=2;j<=x/j;j++)
{
if(x%j==0)
{
ans=ans*(j-1)/j;
while(x%j==0)
{
x/=j;
}
}
}
if(x!=1)
ans=ans*(x-1)/x;
cout<<ans<<endl;
}
return 0;
}
筛法求欧拉函数
问题
给定一个正整数n,求1~n中每个数的欧拉函数之和。
算法思路
使用线性筛的方法,来求每个数的欧拉函数

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int n,cnt;
bool st[N];
int primes[N];
int phi[N]; void get_eulers()
{
int i,j;
phi[1]=1;
for(i=2;i<=n;i++)
{
if(!st[i])
{
primes[cnt++]=i;
phi[i]=i-1;
for(j=0;primes[j]<=n/i;j++)
{
st[primes[j]*i]=true;
if(i%primes[j]==0)
phi[primes[j]*i]=primes[j]*phi[i];
else
phi[primes[j]*i]=(primes[j]-1)*phi[i];
}
}
}
int ans=0;
for(i=1;i<=n;i++)
ans+=phi[i];
cout<<ans<<endl;
} int main()
{
int i,j;
cin>>n; get_eulers(); return 0;
}
快速幂
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; void suppow(ll a,ll b,ll p)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%p;
a=(a*a)%p;
b>>=1;
}
cout<<ans%p<<endl;
} int main()
{
ll n,a,b,p;
cin>>n;
while(n--)
{
cin>>a>>b>>p;
suppow(a,b,p);
} return 0;
}
快速幂求逆元
前提

代码
快速幂求解:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10; ll supow(ll a,ll b,ll p)
{
ll ans=1;
while(b)
{
if(b&1)
ans=ans*a%p;
a=a*a%p;
b>>=1;
}
return ans;
} int main()
{
ll a,p,n;
cin>>n;
while(n--)
{
cin>>a>>p;
if(a%p!=0)
{
cout<<supow(a,p-2,p)<<endl;
}
else
puts("impossible");
}
return 0;
}
扩展欧几里得求逆元
#include <iostream>
using namespace std;
typedef long long LL;
int n; int exgcd(int a, int b, int &x, int &y)
{
if (!b) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
} int main()
{
cin >> n;
while (n --)
{
int a, p, x, y;
// if (a < p) swap(a, p);
cin >> a >> p;
int d = exgcd(a, p, x, y);
if (d == 1) cout << ((LL)x + p) % p << endl;//保证x是正数
else puts("impossible"); }
return 0;
}
数学知识-欧拉函数&快速幂的更多相关文章
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】
1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 45 Solved: 8[Submit][Status][W ...
- 牛客训练:小a与黄金街道(欧拉函数+快速幂)
题目链接:传送门 思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果. 参考文章:传送门 #include<iostream> #include<cmath ...
- Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...
- 小a与黄金街道(欧拉函数+快速幂)
链接:https://ac.nowcoder.com/acm/contest/317/D 来源:牛客网 题目描述 小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们 ...
- 【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)
读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用 ...
- CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)
Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...
- UVaLive 7362 Farey (数学,欧拉函数)
题意:给定一个数 n,问你0<= a <=n, 0 <= b <= n,有多少个不同的最简分数. 析:这是一个欧拉函数题,由于当时背不过模板,又不让看书,我就暴力了一下,竟然A ...
- 数学之欧拉函数 &几道poj欧拉题
欧拉函数总结+证明 欧拉函数总结2 POJ 1284 原根 #include<iostream> #include<cstdio> #include<cstring> ...
随机推荐
- kubernets之控制器之间的协作以及网络
一 创建一个deployment的时候整个kubernets集群的资源和事件的调用链 1.1 创建一个deployment的资源,在提交的时候,集群中的调度器,控制器以及node节点上kubele ...
- CTFHub - Web(二)
目录遍历: 法一: 依次查看目录即可: 法二: 利用脚本: #!/usr/bin/python3 # -*- coding: utf-8 -*- # --author:valecalida-- ...
- Nginx和Tomcat配置SSL实现https访问
环境:CentOS 7 Nginx版本: nginx/1.18.0 1. 安装nginx 详细步骤可以参考如下官网:http://nginx.org/en/linux_packages.html#RH ...
- Nginx架构赏析
淘宝的某位大佬曾经做过测试,在一台24G内存的机器上,Nginx的最大并发连接数达到了200万.同学们听到这个结论后,是不是被Nginx的超高性能深深折服了,它内部的架构设计究竟是怎么样的呢?这篇文章 ...
- 转 Fiddler2 下断点修改HTTP报文
文章转自:https://www.cnblogs.com/zhengna/p/10861893.html 一 Fiddler中设置断点修改HTTP请求 方法1:全局断点.Rules-->Auto ...
- MySQL高可用HA——keepalived配置
0. Keepalived介绍 Keepalived是基于VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)协议的一款高可用软件.Keepaili ...
- SQL解析工具
面对复杂的SQL可用这个SQL解析工具,分析出用到了哪些表哪些字段: http://107.170.101.241:8080/getTableColumn/
- Go for Pythonistas Go and the Zen of Python 禅
Go for Pythonistas https://talks.golang.org/2013/go4python.slide#1 Things I don't like about Python ...
- v-show和v-if指令的共同点和不同点?
共同点:都能控制元素的显示和隐藏:不同点:实现本质方法不同,v-show本质就是通过控制css中的display设置为none,控制隐藏,只会编译一次:v-if是动态的向DOM树内添加或者删除DOM元 ...
- Fiddler扩展——自定义列数据&Tunnel to 443解决办法
在平时日常工作中,使用Fiddler的占比还是蛮大的.使用过程,也会遇到一些小问题,问题虽小,但抓不到包,分析不了问题与数据,那也是件麻烦的事情. 以前也分享过一些小技巧,可以找以前的博文查看,具体地 ...