[SHOI2002] N consecutive number split solution

A relatively simple math problem.
The question asks us how many consecutive positive integers a positive integer \ (n \) can be split. Then these consecutive positive integers will form an arithmetic sequence with tolerance \ (1 \) .
If the first item of the sequence is \ (a \) and the last item is \ (b \) , then the sum of the difference sequence is \ (\ dfrac {(a + b) (b-a + 1)} {2} \) .
Then come to push a wave of persimmons:

\[\because \dfrac{(a+b)(b-a+1)}{2}=n \]

\[\therefore (a+b)(b-a+1)=2n \]

\[\therefore (a+b)(b-a+1)\bmod 2=0 \]

\ [\ because \ text {The parity of the sum and difference of the two numbers is the same} \]

\ [\ therefore (a + b) \ text {and} (ba) \ text {have the same parity} \]

\ [\ therefore (a + b) \ text {and} (b-a + 1) \ text {have different parity} \]

Therefore, we only need to enumerate \ ((a + b) \) to determine whether it is a factor of \ (2n \) . If yes, find \ ((b-a + 1) \) . If the parity of \ ((a + b) \) and \ ((b-a + 1) \) are different, the number of schemes increases.
The time complexity is \ (O (\ sqrt {n}) \) and the constant is \ (\ sqrt {2} \) .
\ (11 \) line code (unpressed line):

#include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
	scanf("%lld",&n);
	n<<=1; // 之后不需要使用 n 了,只需要用 2n,故直接将 n 乘上 2
	int ans=0,m=sqrt(n);
	for (register int i=1,j;i<=m;i++) ans+=!(n%i)&&(i&1)^(n/i&1); // 位运算。等价于 if (n%i==0&&i%2!=n/i%2) ans++;
	printf("%d",ans);
	return 0;
}

Guess you like

Origin www.cnblogs.com/Xray-luogu/p/12679256.html