当前位置: 首页 > news >正文

辽宁网站推广的目的海南网上报名系统

辽宁网站推广的目的,海南网上报名系统,网站没快照,哪个网站是自己销售算法基础-数学知识-欧拉函数、快速幂、扩展欧几里德、中国剩余定理 欧拉函数AcWing 874. 筛法求欧拉函数 快速幂AcWing 875. 快速幂AcWing 876. 快速幂求逆元 扩展欧几里德(裴蜀定理)AcWing 877. 扩展欧几里得算法AcWing 878. 线性同余方程 中国剩余定理…

算法基础-数学知识-欧拉函数、快速幂、扩展欧几里德、中国剩余定理

  • 欧拉函数
    • AcWing 874. 筛法求欧拉函数
  • 快速幂
    • AcWing 875. 快速幂
    • AcWing 876. 快速幂求逆元
  • 扩展欧几里德(裴蜀定理)
    • AcWing 877. 扩展欧几里得算法
    • AcWing 878. 线性同余方程
  • 中国剩余定理

欧拉函数

在这里插入图片描述
在这里插入图片描述

互质就是两个数的最大公因数只有1,体现到代码里面就是a和b互质,则b mod a = 1 mod a (目前我不是很理解,但是可以这样理解:a和b的最大公因数是1,即1作为除数和b作为除数时,对于被除数a来说余数是一样的,即1/a的余数和b/a是一样的,即b mod a = 1 mod a)
欧拉函数的作用是求1-n与n互质的个数

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;void get_eura(int x)
{int res = x;for (int i = 2; i <= x / i; ++ i){if (x % i == 0){//res = res * (1 - 1/i);或者res = res * (i - 1) / i;都不行,前者是浮点数1 后者会溢出res = res / i * (i - 1);while (x % i == 0){x /= i;}}}if (x > 1) res = res / x * (x - 1);cout << res << endl;
}
void solve()
{int n;cin >> n;while (n -- ){int x;cin >> x;get_eura(x);}
}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;//cin >> T;while (T --) solve();return 0;
}

AcWing 874. 筛法求欧拉函数

线性筛 + 欧拉函数(有一点推公式)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;
const int N = 1e6 + 10;
int primes[N], st[N], eulers[N];
int cnt;
void get_eulers(int x)
{eulers[1] = 1;  for (int i = 2; i <= x; ++ i)//只是在线性筛的过程中顺便求了一下每个数的欧拉函数{if (!st[i])//1-n的质数{primes[cnt++] = i;eulers[i] = i - 1;}for (int j = 0; primes[j] <= x / i; ++ j)//1-n的合数//任何合数都含有质因数,4 = 1 * 2 * 1 * 2;{st[primes[j] * i] = 1;if (i % primes[j] == 0){eulers[i * primes[j]] = eulers[i] * primes[j];break;//其实也相当于一个else}//eulers[i * primes[j]] = eulers[i] * primes[j] / primes[j] * (primes[j] - 1);eulers[i * primes[j]] = eulers[i] * (primes[j] - 1);}}
}
void solve()
{int n;cin >> n;get_eulers(n);long long res = 0; for (int i = 1; i <= n; ++ i) res += eulers[i];cout << res;
}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;//cin >> T;while (T --) solve();return 0;
}

快速幂

1 2 4 8成指数倍增长 log的时间复杂度

AcWing 875. 快速幂

long long qmi(int a, int b, int p)
{long long res = 1;while (b){if (b & 1){res = res * a % p;}a = a * (long long)a % p;b >>= 1;}return res;
}

AcWing 876. 快速幂求逆元

在这里插入图片描述
欧拉函数 =>费马定理 =>快速幂实现费马定理计算结果

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;long long qmi(int a, int b, int p)
{long long res = 1;while (b){if (b & 1) res = res * a % p;a = (long long)a * a % p;b >>= 1;}return res;
}
void solve()
{int n;cin >> n;while (n --){int a, p;cin >> a >> p;if (a % p == 0) cout << "impossible" << endl;else cout << qmi(a, p - 2, p) << endl;//a需要与m互质,否则a不存在乘法逆元}
}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;//cin >> T;while (T --) solve();return 0;
}

扩展欧几里德(裴蜀定理)

AcWing 877. 扩展欧几里得算法

理解递归的本质:
在这里插入图片描述
裴蜀定理和线性同余方程的证明:
在这里插入图片描述

#include <cstdio>
#include <iostream>using namespace std;int exgcd(int a, int b, int &x, int &y)
{if (b == 0){x = 1, y = 0;return a;}//d就是最大公约数,本题其实用不到int d = exgcd(b, a % b, y, x);//本题的精髓/*只是为了方便更改x和y的值,如果用d = exgcd(b, a % b, x, y);最后就解得 新x = y 新y = x - a / b * y那么代码就得这么写int t = y;y = x - a / b * y;x = t;显然比只要写一句 新y -= a / b * x; 麻烦*/y -= a / b * x;return d;
}
void solve()
{int n;cin >> n;while (n -- ){int a, b, x, y;cin >> a >> b;exgcd(a, b, x, y);cout << x << " " << y << endl;}
}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;//cin >> T;while (T --) solve();return 0;
}

AcWing 878. 线性同余方程

线性同余方程用扩展欧几里德定理求解
本题推导过程在上面
为什么要% m
在这里插入图片描述

#include <cstdio>
#include <iostream>using namespace std;int exgcd(int a, int b, int &x, int &y)
{if (b == 0){x = 1, y = 0;return a;}else//其实不用else,上面满足直接return了,上面不满足也会走到下面 {int d = exgcd(b, a % b, y, x);y -= a / b * x;return d;}
}
void solve()
{int n;cin >> n;while (n -- ){int a, b, m, x, y;cin >> a >> b >> m;int d = exgcd(a, -m, x, y);if (b % d != 0) cout << "impossible" << endl;else cout << (long long)b / d * x % m << endl;}
}
int32_t main()
{ios::sync_with_stdio(0);cin.tie(0);int T = 1;//cin >> T;while (T --) solve();return 0;
}

中国剩余定理

http://www.yayakq.cn/news/986323/

相关文章:

  • 企业网站建设的基本原则有哪些?二个字最旺财的公司名字
  • 崇安区网站建设价格江门免费建站
  • 四川省住房和城乡建设厅网站怎么样查看网站开发语言
  • 网站如何防止恶意注册天津市武清区建设银行网站
  • 哪个网站有学做吃的在线制作图片生成器小程序
  • 优化网站 主题网站建设讠金手指 22
  • 学会了vue 能搭建一个网站平台微信自带小程序商城
  • 潮州哪里有做网站网网站制作开发
  • 专业做化妆品的网站有哪些网站引导图
  • 做网站的cnfg网站转app免费
  • 如何弄死一个网站电脑课程培训零基础
  • 快速搭建网站 数据存储湖南人文科技学院官网教务系统
  • 付费 视频 网站 怎么做医疗器械监督管理条例
  • 北京网站建设公司价格响应式网站布局实例
  • 前端手机网站网站自己的
  • 网站建设哪家公司好成都网站建设定制公司网站建设
  • 做响应式网站应该注意什么百度seo关键词排名价格
  • pc端网站自适应代码网站开发工程师岗位概要
  • 网站建设作业指导书手机网站被禁止访问怎么打开网页
  • 做网站自动赚钱网络营销课程设置
  • 深圳市南山区住房和建设局网站手机网站如何做才能兼容性各种手机
  • 青岛做网站公网络公司有哪些
  • 中国建设教育网站官方免费成品网站模板
  • 营销类wordpress主题成都搜狗seo
  • 做内贸现在一般都通过哪些网站网站哪家公司做的
  • 建站报价网站开发和小程序开发区别
  • js特效网站展示阿里免费版企业邮箱
  • 官方网站建设维护合作协议wordpress首页文章列表
  • 成都的企业网站建设公司吉祥物设计
  • 专业电子网站建设网站客户端怎么做的