数学知识-欧拉函数&快速幂
欧拉函数
定义
对于正整数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> ...
随机推荐
- 【EXP】Oracle多表导出问题
有些时候,需要导入某个用户的一些相关表.但是不知道用户的用户名和密码.这样就很尴尬 但是如果手上有dba权限的用户的话,就很方便的能导出了 先要知道多表导出的语句 exp system/123456 ...
- Databricks 第7篇:管理Secret
有时,访问数据要求您通过JDBC对外部数据源进行身份验证,可以使用Azure Databricks Secret来存储凭据,并在notebook和job中引用它们,而不是直接在notebook中输入凭 ...
- 解决ROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'creat table study_record( id int(11) not null
之前一直用的好好的,突然就出现了这个错误: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tha ...
- Python3.9的http.client.py下的HTTPMessage类中的方法getallmatchingheaders的bug修复建议
在官方网站已经提交相关issue,不过目前看好像还没有修复.具体的bug位置为: http文件夹下的client.py文件,代码位置为:类HTTPMessage下的方法getallmatchinghe ...
- Vue基础之生命周期函数[残缺版]!
Vue基础之生命周期函数[残缺版]! 为什么说是残缺版呢?! 因为有一些周期函数我并没有学到!所以是残缺版! 01 beforeCreate //在实例初始化之后,数据观测 (data observe ...
- U盘制作系统启动盘方法
1.下载一个UltralSO用来把CentOS系统镜像写入U盘作为启动安装盘 U盘用一个空U盘,会格式化的. 下载下来,使用试用版就行 刻录完成.
- idea maven package报错"不再支持源选项 5 请使用 6 或更高版本。不支持发行版本 5"
解决办法: 1.确保java compile以及project和module的java字节码版本是所用的java版本:
- aio 系列函数是由 POSIX 定义的异步操作接口,可惜的是,Linux 下的 aio 操作,不是真正的操作系统级别支持的,它只是由 GNU libc 库函数在用户空间借由 pthread 方式实现的,而且仅仅针对磁盘类 I/O,套接字 I/O 不支持。
30 | 真正的大杀器:异步I/O探索 https://time.geekbang.org/column/article/150780
- Qedis实现
对比redis的Qedis 实现在github 和 实验楼都有资料
- TCMalloc源码学习(四)(小内存块释放)
pagemap_和pagemap_cache_ PageHeap有两个map,pagemap_记录某一内存页对应哪一个span,显然可能多页对应一个span,pagemap_cache_记录某一内存页 ...