大数欧拉函数 + 降幂 —— 模板题

2023-11-02 10:48

本文主要是介绍大数欧拉函数 + 降幂 —— 模板题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:点我啊!!~~~

P S : PS: PS注意要判断扩展欧拉降幂,并用快速乘

O ( s q r t ( n ) ) O(sqrt(n)) O(sqrt(n))求欧拉函数:

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
typedef pair <int,int> pii;
const int maxn = 2e7 + 5;
ll a, p;
char b[maxn];ll qmul(ll a, ll b) {return (a*b - (ll)((long double)a/p*b)*p+p)%p;
}ll qpow(ll a, ll b){ll ret = 1;while(b){if(b & 1) ret = qmul(ret, a);a = qmul(a, a);b >>= 1;}return ret;
}ll getphi(ll x){ll ret = x;for(ll i=2; i*i<=x; i++)if(x%i==0){ret -= ret / i;while(x%i==0) x /= i;}if(x > 1) ret -= ret / x;return ret;
}int main() {scanf("%lld%lld%s", &a, &p, &b);ll ans = 0, phi = getphi(p), f = 0;for(int i=0; b[i]; i++){ans = ans * 10 + b[i] - '0';if(ans >= phi) f = 1;ans %= phi;}if(f) ans = qpow(a, ans+phi);else ans = qpow(a, ans);printf("%lld\n", ans);
}

利用 M i l l e r − R a b i n Miller-Rabin MillerRabin 求质因子后求得:

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
typedef pair <int,int> pii;
const int maxn = 2e7 + 5;namespace Factor {
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define PLL pair<ll,ll>
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)const int N=1010000;ll C,fac[10010],n,mut,a[1001000];int T,cnt,i,l,prime[N],p[N],psize,_cnt;ll _e[100],_pr[100];vector<ll> d;inline ll mul(ll a,ll b,ll p) {if (p<=1000000000) return a*b%p;else if (p<=1000000000000ll) return (((a*(b>>20)%p)<<20)+(a*(b&((1<<20)-1))))%p;else {ll d=(ll)floor(a*(long double)b/p+0.5);ll ret=(a*b-d*p)%p;if (ret<0) ret+=p;return ret;}}void prime_table() {int i,j,tot,t1;for (i=1; i<=psize; i++) p[i]=i;for (i=2,tot=0; i<=psize; i++) {if (p[i]==i) prime[++tot]=i;for (j=1; j<=tot && (t1=prime[j]*i)<=psize; j++) {p[t1]=prime[j];if (i%prime[j]==0) break;}}}void init(int ps) {psize=ps;prime_table();}ll powl(ll a,ll n,ll p) {ll ans=1;for (; n; n>>=1) {if (n&1) ans=mul(ans,a,p);a=mul(a,a,p);}return ans;}bool witness(ll a,ll n) {int t=0;ll u=n-1;for (; ~u&1; u>>=1) t++;ll x=powl(a,u,n),_x=0;for (; t; t--) {_x=mul(x,x,n);if (_x==1 && x!=1 && x!=n-1) return 1;x=_x;}return _x!=1;}bool miller(ll n) {if (n<2) return 0;if (n<=psize) return p[n]==n;if (~n&1) return 0;for (int j=0; j<=7; j++) if (witness(rand()%(n-1)+1,n)) return 0;return 1;}ll gcd(ll a,ll b) {ll ret=1;while (a!=0) {if ((~a&1) && (~b&1)) ret<<=1,a>>=1,b>>=1;else if (~a&1) a>>=1;else if (~b&1) b>>=1;else {if (a<b) swap(a,b);a-=b;}}return ret*b;}ll rho(ll n) {for (;;) {ll X=rand()%n,Y,Z,T=1,*lY=a,*lX=lY;int tmp=20;C=rand()%10+3;X=mul(X,X,n)+C;*(lY++)=X;lX++;Y=mul(X,X,n)+C;*(lY++)=Y;for(; X!=Y;) {ll t=X-Y+n;Z=mul(T,t,n);if(Z==0) return gcd(T,n);tmp--;if (tmp==0) {tmp=20;Z=gcd(Z,n);if (Z!=1 && Z!=n) return Z;}T=Z;Y=*(lY++)=mul(Y,Y,n)+C;Y=*(lY++)=mul(Y,Y,n)+C;X=*(lX++);}}}void _factor(ll n) {for (int i=0; i<cnt; i++) {if (n%fac[i]==0) n/=fac[i],fac[cnt++]=fac[i];}if (n<=psize) {for (; n!=1; n/=p[n]) fac[cnt++]=p[n];return;}if (miller(n)) fac[cnt++]=n;else {ll x=rho(n);_factor(x);_factor(n/x);}}void dfs(ll x,int dep) {if (dep==_cnt) d.pb(x);else {dfs(x,dep+1);for (int i=1; i<=_e[dep]; i++) dfs(x*=_pr[dep],dep+1);}}void norm() {sort(fac,fac+cnt);_cnt=0;rep(i,0,cnt) if (i==0||fac[i]!=fac[i-1]) _pr[_cnt]=fac[i],_e[_cnt++]=1;else _e[_cnt-1]++;}vector<ll> getd() {d.clear();dfs(1,0);return d;}vector<ll> factor(ll n) {cnt=0;_factor(n);norm();return getd();}vector<PLL> factorG(ll n) {cnt=0;_factor(n);norm();vector<PLL> d;rep(i,0,_cnt) d.pb(mp(_pr[i],_e[i]));return d;}ll getphi(ll x) { // 2019/9/17vector <PLL> G;G = factorG(x);double ret = x;rep(i,0,G.size())ret *= (1.0 - 1.0/(double)G[i].first);return ret;}bool is_primitive(ll a,ll p) {assert(miller(p));vector<PLL> D=factorG(p-1);rep(i,0,SZ(D)) if (powl(a,(p-1)/D[i].fi,p)==1) return 0;return 1;}
#undef fi
#undef pb
#undef se
#undef mp
#undef PLL
#undef SZ
#undef all
#undef rep
#undef per
};ll qmul(ll a, ll b, ll p) {return (a*b - (ll)((long double)a/p*b)*p+p)%p;
}ll qpow(ll a, ll b, ll p){ll ret = 1;while(b){if(b & 1) ret = qmul(ret, a, p);a = qmul(a, a, p);b >>= 1;}return ret;
}char b[maxn];
int main() {ll a, p;Factor::init(1e4 + 5);scanf("%lld%lld%s", &a, &p, &b);ll ans = 0, phi = Factor::getphi(p), f = 0;for(int i=0; b[i]; i++){ans = ans * 10 + b[i] - '0';if(ans >= phi) f = 1;ans %= phi;}if(f) ans = qpow(a, ans+phi, p);else ans = qpow(a, ans, p);printf("%lld\n", ans);
}

这篇关于大数欧拉函数 + 降幂 —— 模板题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/330080

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

uva 10069 DP + 大数加法

代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <cl

poj 2104 and hdu 2665 划分树模板入门题

题意: 给一个数组n(1e5)个数,给一个范围(fr, to, k),求这个范围中第k大的数。 解析: 划分树入门。 bing神的模板。 坑爹的地方是把-l 看成了-1........ 一直re。 代码: poj 2104: #include <iostream>#include <cstdio>#include <cstdlib>#include <al

最大流、 最小费用最大流终极版模板

最大流  const int inf = 1000000000 ;const int maxn = 20000 , maxm = 500000 ;struct Edge{int v , f ,next ;Edge(){}Edge(int _v , int _f , int _next):v(_v) ,f(_f),next(_next){}};int sourse , mee

C++操作符重载实例(独立函数)

C++操作符重载实例,我们把坐标值CVector的加法进行重载,计算c3=c1+c2时,也就是计算x3=x1+x2,y3=y1+y2,今天我们以独立函数的方式重载操作符+(加号),以下是C++代码: c1802.cpp源代码: D:\YcjWork\CppTour>vim c1802.cpp #include <iostream>using namespace std;/*** 以独立函数