A - A Math Problem HDU - 6182

签到题

先预算一下,15^15是一个18位的数,而16^16则超越了unsigned long long 所表示的范围

根据题意我们算出15^15次就够了,它给的数只要超过了15^15,就说明15就是满足k^k<=n的最大整数,因为16^16次超越了任何它给的数,

也可以直接用大数,这样比较简单,不需要先预算估计,大数模板直接CV

下面是不用大数的方法


#include<iostream> 
#include<cstring> 
#include<string> 
#include<iomanip> 
#include<algorithm> 
using namespace std;
typedef unsigned long long ULL;
ULL Pow(ULL a, ULL b)
{
	ULL ans = 1;
	while (b)
	{
		if (b & 1) ans *= a;
		b >>= 1;
		a *= a;
	}
	return ans;
}

int main()
{
	ULL a[20], i, n;
	for (i = 1; i <= 15; i++) 
		a[i] = Pow(i, i);
	while (cin >> n)
	{
		int cnt = 0;
		for (i = 1; i <= 15; i++)
			if (a[i] <= n) cnt++;
		cout << cnt << endl;
	}
	//system("pause");
	
}

猜你喜欢

转载自blog.csdn.net/qq_41776911/article/details/81226448