LightOJ1245 Harmonic Number (II)

题意

(求Sigma lfloor frac{n}{i} floor)
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n < 2^31).

Sol

数论分块

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;

IL ll Read(){
	char c = '%'; ll x = 0, z = 1;
	for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
	for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
	return x * z;
}

ll n, ans;

int main(RG int argc, RG char *argv[]){
	for(RG int T = Read(), Case = 1; Case <= T; ++Case){
		n = Read(); ans = 0;
		for(RG ll i = 1, j; i <= n; i = j + 1){
			j = min(n, n / (n / i));
			ans += 1LL * (n / i) * (j - i + 1);
		}
		printf("Case %d: %lld
", Case, ans);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/cjoieryl/p/8250517.html