[Nowcoder 218396] Little G's sum

Small G's sum

Title link: nowcoder 218396

To Niu Ke:

——>Click me to jump<——

General idea

Find the sum of the smallest divisor and the largest divisor of each number in 1~n.

Ideas

Everyone knows that the smallest divisor of a number is 1 11. The largest divisor is itself.

That is equal to ∑ i = 1 n (1 + i) \sum\limits_{i=1}^{n}(1+i)i=1n(1+i ) , and then simplify:
n + (1 + n) × n / 2 = 2 × n / 2 + (1 + n) × n / 2 = (3 + n) × n / 2 n+(1+n )\times n/2=2\times n/2+(1+n)\times n/2=(3+n)\times n/2n+(1+n)×n/2=2×n/2+(1+n)×n/2=(3+n)×n/2

Then you can O (1) O (1)O ( 1 ) asked.

Code

#include<cstdio>

using namespace std;

int n;

int main() {
    
    
	scanf("%d", &n);
	
	printf("%lld", 1ll * n * (n + 3) >> 1ll);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43346722/article/details/114551682