题目大意:给出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\n",ans);
}

  

相关文章:

  • 2021-07-29
  • 2022-12-23
  • 2021-04-17
  • 2022-12-23
  • 2021-11-25
  • 2021-11-05
  • 2021-12-03
  • 2022-01-16
猜你喜欢
  • 2021-10-18
  • 2021-12-21
  • 2022-01-13
  • 2022-12-23
  • 2021-11-01
  • 2021-11-05
相关资源
相似解决方案