数学知识-欧拉函数&快速幂
欧拉函数
定义
对于正整数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> ...
随机推荐
- 【高级排序算法】2、归并排序法的实现-Merge Sort
简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...
- kubernets集群的安全防护(上)
一 了解认证机制 1.1 API的服务器在接收来自客户端的请求的时候会对发起的用户进行几个步骤 认证插件进行认证,确认发起的用户是外部用户,还是集群中的某个命名空间里面的pod 确认用户属于哪个 ...
- Go - httpclient 常用操作
httpclient 模块介绍 httpclient 是基于 net/http 封装的 Go HTTP 客户端请求包,支持常用的请求方式.常用设置,比如: 支持设置 Mock 信息 支持设置失败时告 ...
- CMOS 摄像头的Skipping 和 Binning 模式
在通常的摄像头中,不同的resolution对应不同的帧率.想要提高帧率就要考虑是否需要缩小视野(FOV).若不希望视野缩小,就需要减少resolution. 常用的减少resolution的两种方式 ...
- 开源AwaitableCompletionSource,用于取代TaskCompletionSource
1 TaskCompletionSource介绍 TaskCompletionSource提供创建未绑定到委托的任务,任务的状态由TaskCompletionSource上的方法显式控制,以支持未来的 ...
- 【python刷题】LRU
什么是LRU? LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰.该算法赋予每个页面一个访问字段,用来记录一个页面自上次 ...
- VirtualBox Guest Additions 下载地址以及简介
下载者可将以下链接粘贴到浏览器上,根据Vbox的版本找到自己对应的增强. http://download.virtualbox.org/virtualbox/5.0.10/ 虚拟机安装VBoxAddi ...
- SumatraPDF设置护眼背景
高级选项中: 1 FixedPageUI [ 2 TextColor = #000000 3 BackgroundColor = #C7EDCC 4 SelectionColor = #f5fc0c ...
- 基于Vue+ElementUI架构的前端国际化解决方案
1.项目目录结构 ├── build 构建相关配置文件 | |── index.js webpack的基础配置入口 ├── m ...
- (五)SpringBoot面试题
SpringBoot面试题 1.Spring Boot 的自动配置是如何实现的? 2.shiro和oauth还有cas他们之间的关系是什么?问下您公司权限是如何设计,还有就是这几个概念的区别. 2.1 ...