扩展欧几里得以及欧拉

来源:互联网 发布:正规网络兼职赚钱 编辑:程序博客网 时间:2024/06/09 04:17

#include<iostream>

#include<algorithm>#include<string>#include<queue>#include<cmath>#include<vector>#include<stdlib.h>#include<iomanip>#include<map>#include<stack>#include<memory.h>using namespace std;typedef long long ll;//解决 ax+by=gcd(a,b)int extend_Euclid(ll a, ll b, int &x, int &y) {int d, int temp;if (b == 0) {x = 1;y = 0;return a;}d = extend_Euclid(b, a%b, x, y);temp = x;x = y;y = temp - a / b*y;return d;}//ax=b(mod n)>>ax-b=ny>>ax+nt=b->d=gcd(a,n)->k=b/d->x=x0*k 一个解int liner_Euclid(ll a, ll b, int n) {int d, x, y;d = extend_Euclid(a, n, x, y);if (b%d != 0) {return -1;}x = x*b / d;x = (x%n + n) % (n / d);return x;}/*算法导论里说:(还没理解) 方程axºb(mod n)有解(即存在d|b,其中d=gcd(a,n)),x0是该方程的任意一个解,则该方程对模n恰有d个不同的 解,分别为 x(i)=x(0)+i(n/d)(i=1,2,...d). 特别的设e=x0+n,方程ax=b(mod n)的最小整数解x1=e mod (n/d),最大整数解x2=x1+(d-1)*(n/d)。*/bool mod_lineEuclid(ll a, ll b, ll n) {int d, x, y;d = extend_Euclid(a, n, x, y);if (b%d != 0) {return false;;}x = x*(b/d)%n;for (int i = 0; i <= d; i++) {ll x0 = (x + i*(n / d)) % n;cout << x0 << endl;}ll x1 = (x + n) % (n / d);ll x2 = x1 + (d - 1)*(n / d);cout << x1 << '\t' << x2 << endl;return true;}/*欧拉公式:求一个n 的小于他的质数个数 对于质数p:f(p)=p-1 */int oular(ll n) {ll ans = n;ll a = n;for (ll i = 2; i*i < a; i++) {if (n%i == 0) {ans = ans / i*(i - 1);while (a%i == 0) {a /= i;}}}if (a > 1)ans = ans / a*(a - 1);return ans;}int main() {}

阅读全文
0 0
原创粉丝点击