luogu1920 成功密码

题目大意:给出x∈(0,1)以及n∈(0,1e18),求sum foreach i(1<=i<=n) (x^i/i)保留四位小数的值。

用快速幂暴力求。考虑到题目只要求保留四位小数,而随着i的增大,x^1就越来越小,变化量被隐藏到四位小数后面去了。所以我们可以在适当的时候提前退出。

#include <cstdio>
#include <algorithm>
using namespace std;

#define ll long long
const ll M = 1000000;

double Power(double a, ll n)
{
	double ans = 1;
	while (n)
	{
		if (n & 1)
			ans *= a;
		a *= a;
		n >>= 1;
	}
	return ans;
}

int main()
{
	ll n;
	double x;
	scanf("%lf%lld", &x, &n);
	double ans = 0;
	for (int i = 1; i <= min(n, M); i++)
		ans += Power(x, i) / (double)i;
	printf("%.4f
",ans);
}

  

原文地址:https://www.cnblogs.com/headboy2002/p/8894116.html