LightOJ1245 Harmonic Number (II) 整除分块

LightOJ1245 Harmonic Number (II)

标签

  • 整除分块

前言

简明题意

  • 求(n<=max_int)

\[\sum_{i=1}^n[\frac ni] \]

思路

  • 。。。整出分块的板子题。不会的去学一下整除分块

注意事项

总结

AC代码

#include<cstdio>

void solve()
{
	int t;
	scanf("%d", &t);
	for (int i = 1; i <= t; i++)
	{
		int n;
		scanf("%d", &n);

		long long ans = 0;
		int l = 1;
		while (l <= n)
		{
			int r = n / (n / l);
			ans += (r - l + 1) * (n / l);
			if (l == 1ll << 31 - 1)
				break;
			l = r + 1;
		}

		printf("Case %d: %lld\n", i, ans);
	}
}

int main()
{
	freopen("Testin.txt", "r", stdin);
	solve();
	return 0;
}
posted @ 2019-08-27 17:06  danzh  阅读(116)  评论(0编辑  收藏  举报