快速幂浅谈
快速幂——>Miller_Rabin方法
1.初步认知:2
// 11 == 1011 == 2^0+2^1+2^3 #include<bits/stdc++.h> using namespace std; int pow(int a,int b) { int ans=1,base=a; while(b!=0) { if(b&1!=0) ans*=base; base*=base; b>>=1; } return ans; } int main() { int a,b; while(~scanf("%d%d",&a,&b))//scanf("%d%d",&a,&b); { printf("%d\n",pow(a,b)); } return 0; }
2.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll qmod(ll a,ll b)
{
ll ans=1,base=a;
while(b)
{
if(b&1) ans*=base;
base*=base;
b>>=1;
}
return ans;
}
int main()
{
ll a,b;
while(~scanf("%lld%lld",&a,&b))
{
printf("%lld\n",pow(a,b));
}
return 0;
}
3.从知乎上借鉴的:
#include<bits/stdc++.h>
using namespace std;
int pow(int a,int b,int p)
{
int ans=1;
while(b)
{
if(b&1) ans=(long long) ans*a%p;
a=(long long)a*a%p;
b>>=1;
}
return ans;
}
int main()
{
int a,b,p;
while(cin>>a>>b>>p) cout<<pow(a,b,p)<<endl;
return 0;
}
4.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll qmod(ll a,ll n,ll m)//calculate the value of (a^n MOD m).
{
ll ans=1;
while(b)
{
if(b&1) ans=ans*a%m;
a=a*a%m;
b>>=1;
}
return ans;
}
int main()
{
ll a,b,m;
while(cin>>a>>b>>m) cout<<qmod(a,b,m)<<endl;
return 0;
}
5.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll prime[65000];
void f()
{
prime[0]=prime[1]=0;
for(int i=2;i<65000;i++) prime[i]=1;
for(int i=2;i<65000;i++)
if(prime[i])
for(int j=2*i;j<65000;j+=i) prime[j]=0;
}//素数提前打表
ll powmod(ll a,ll n,ll m)//a^n MOD m;
{
ll ans=1;
while(n)
{
if(n&1) ans=ans*a%m;
a=a*a%m;
n>>=1;
}
return ans;
}
int tests(int n)
{
for(int i=2;i<n;i++)
{
if(powmod(i,n,n)!=i) return 0;
}
return 1;
}
int main()
{
f();
int n;
while(cin>>n && n)
{
if(!prime[n] && tests(n))
cout<<"The number "<<n<<" is a Carmichael number.\n";
else
cout<<n<<" is normal.\n";
}
return 0;
}
——————————————————————————————https://blog.csdn.net/qq_41785863/article/details/81266531
浙公网安备 33010602011771号