51nod 1004 n^n的末尾数字

快速幂取模

#include <iostream>

using namespace std;
typedef long long ll;
ll pow(ll a, ll b)
{
	ll r = 1;
	while (b)
	{
		if (b & 1)
			{
				r *= a;
				r %= 10;
			}
		a *= a;
		a %= 10;
		b >>= 1;
	}
	return r;
}

int main()
{
	ll n;
	cin >> n;
	ll res = pow(n,n);
	cout << res % 10 << endl;
	return 0;
}


你可能感兴趣的:(C++,数学思维)