
题解:快速幂,有人可能觉的水题没必要用快速幂,但是我认为写快速幂能更好的记住模板。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans,n;
ll quickmi(ll a, ll n) // a:底数 n:指数
{
ll res = 1, t = a;
while(n)
{
if(n&1) res = res*t;
t = t*t;
n >>= 1;
}
return res;
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
cout<<"2"<<"^"<<n<<" = "<<quickmi(2,n)<<‘\n‘;
return 0;
}
原文:https://www.cnblogs.com/Edviv/p/12254384.html