28 条题解
-
0
使用分治思想,每次将指数 减半,再平方。但如果 是技术,则要再乘
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int INF=0x3f3f3f3f; inline ll read() { ll x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();} return x*f; } ll quickpow(ll a,ll b,ll m){ if(b==0) return 1; ll tmp=quickpow(a,b/2,m)%m; ll ans=tmp*tmp%m; if(b&1) ans=ans*a%m; return ans; } int main(){ ll a,b,p; a=read(),b=read(),p=read(); cout<<quickpow(a,b,p); return 0; }
-
-1
注释了啊
#include <bits/stdc++.h> using namespace std; /* (a+b)%k = (a%k+b%k)%k (a*b)%k = ((a%k)*(b%k))%k */ //快速幂 (a^x)%p // a=1231321313 // x=5464646546 // p=12313 long long a,x,p; long long f(int k){//a的k次方 %p的结果 if(k==0) return 1; if(k==1) return a%p; long long tmp=f(k/2); if(k%2==0){ return tmp*tmp%p; }else{ return (tmp*tmp%p)*a%p; } } int main(){ cin>>a>>x>>p; cout<<f(x); return 0; }
-
-1
位运算 code
#include<stdio.h> using namespace std; unsigned long long a,b,mod; unsigned long long pow(unsigned long long a,unsigned long long b){ unsigned long long ret = 1; while(b){ if(b&1) ret=(ret*a)%mod; a=(a*a)%mod; b>>=1; } return ret; } int main(){ scanf("%lld%lld%lld",&a,&b,&mod); printf("%lld",pow(a,b)%mod); return 0; }
-
-1
#include<bits/stdc++.h> #define int long long using namespace std; int a,b,p; int qp(int a,int b,int p) { int res=1; while(b) { if(b&1) { res*=a; res%=p; } a*=a; a%=p; b>>=1; } return res; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>a>>b>>p; cout<<qp(a,b,p); cout.flush(); return 0; }
板子,就不说了,
直接背
信息
- ID
- 171
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 2
- 标签
- 递交数
- 1240
- 已通过
- 394
- 上传者