欧拉函数

定义

对于正整数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;
}

数学知识-欧拉函数&快速幂的更多相关文章

  1. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  2. XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

    1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][W ...

  3. 牛客训练:小a与黄金街道(欧拉函数+快速幂)

    题目链接:传送门 思路:欧拉函数的性质:前n个数的欧拉函数之和为φ(n)*n/2,由此求出结果. 参考文章:传送门 #include<iostream> #include<cmath ...

  4. Exponial (欧拉定理+指数循环定理+欧拉函数+快速幂)

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2021 Description Everybody loves big numbers ...

  5. 小a与黄金街道(欧拉函数+快速幂)

    链接:https://ac.nowcoder.com/acm/contest/317/D 来源:牛客网 题目描述 小a和小b来到了一条布满了黄金的街道上.它们想要带几块黄金回去,然而这里的城管担心他们 ...

  6. 【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)

    读了一下题就会很愉快的发现,这个数列是关于p的幂次的斐波那契数列,很愉快,然后就很愉快的发现可以矩阵快速幂一波,然后再一看数据范围就......然后由于上帝与集合对我的正确启示,我就发现这个东西可以用 ...

  7. 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 ...

  8. UVaLive 7362 Farey (数学,欧拉函数)

    题意:给定一个数 n,问你0<= a <=n, 0 <= b <= n,有多少个不同的最简分数. 析:这是一个欧拉函数题,由于当时背不过模板,又不让看书,我就暴力了一下,竟然A ...

  9. 数学之欧拉函数 &几道poj欧拉题

    欧拉函数总结+证明 欧拉函数总结2 POJ 1284 原根 #include<iostream> #include<cstdio> #include<cstring> ...

随机推荐

  1. oralce move和shrink释放高水位

    转自:https://blog.51cto.com/fengfeng688/1955137 move和shrink的共同点: 收缩段,消除部分行迁移,消除空间碎片,使数据更紧密 shrink用法: 语 ...

  2. 鸿蒙的远程交互组件应用及微信小程序的远程交互组件应用

    注:鸿蒙的远程交互组件应用相对复杂 ,访问网络时,首先要配置网络权限,华为官方文档有问题,在此引用我老师配置的模板,见附件 过程:1.导入鸿蒙的网络请求模块fetch 2.发起对服务器的请求(在这过程 ...

  3. 安装macosx10.13high serria

    本教程所需资源下载链接: 链接:https://pan.baidu.com/s/1wGTezXz6zGvtlwpv6mMoSg 提取码:r6n9 安装VMware workstation 16.0,安 ...

  4. EntityFramework Core如何映射动态模型?

    前言 本文我们来探讨下映射动态模型的几种方式,相信一部分童鞋项目有这样的需求,比如每天/每小时等生成一张表,此种动态模型映射非常常见,经我摸索,这里给出每一步详细思路,希望能帮助到没有任何头绪的童鞋, ...

  5. MySQL如何加锁控制并发

    目录 前言 一.乐观锁 添加version字段 二.悲观锁 读锁 全表锁(LOCK TABLE 表 READ) 行锁(SELECT ... LOCK IN SHARE MODE) 写锁 全表锁(LOC ...

  6. 解决window10 和 ubuntu 双系统安装没有启动选项问题

    win10 和Ubuntu 双系统安装在网上已经有很多例子了,这里就不在赘述了. 今天新买的笔记本,想安装双系统.正常安装完ubuntu 重启后没有选项. 解决方法一 下载和解压以后,按照以下的步骤安 ...

  7. 架构风格 vs. 架构模式 vs. 设计模式(译)

    4.架构风格 vs. 架构模式 vs. 设计模式(译) - 简书 https://www.jianshu.com/p/d8dce27f279f

  8. mysqldump 内存消耗

    MySQL :: MySQL 8.0 Reference Manual :: 4.5.4 mysqldump - A Database Backup Program https://dev.mysql ...

  9. 《进击吧!Blazor!》第一章 3.页面制作

    作者介绍 陈超超Ant Design Blazor 项目贡献者拥有十多年从业经验,长期基于.Net技术栈进行架构与开发产品的工作,Ant Design Blazor 项目贡献者,现就职于正泰集团 写专 ...

  10. ResponseEntity和@ResponseBody以及@ResponseStatus区别

    看的迷迷糊糊的 https://www.jdon.com/springboot/responseentity.html