First question: series
Submit File: Sequence.pas/c/cpp
input File: sequence.in
Output File: Sequence.out
Problem Description:
Divides a positive integer into a sequence of consecutive positive integers. This sequence must contain at least two positive integers. You need to ask for the minimum length of this sequence. If this sequence does not exist then output-1.
Input Format:
Each row consists of a positive integer n.
Each file contains multiple lines, which are read in until the end of the file.
output Format:
For each n, the output line is the minimum length of the sequence.
Input Sample: |
Sample output: |
9 2 |
2 -1 |
Data range:
For all data, n≤263.
Problem Solving Report:
Enumeration of data for analysis can be found that the continuous arithmetic progression has a monotonically increasing symmetry, the sequence can be expressed as Ai...a2a1xb1b2...bi, where x is the number of symmetric points, X can be a count, its value is x= (A1+B1)/2, if x is not a number, then b1-a1=1, At the same time, further analysis can be found that the data itself can be decomposed according to the A1+b1=a2+b2=...=ai+bi: when n is odd, the solution is 2, when n is even the factor decomposition, to find the smallest odd factor, the smallest odd factor is the problem solution, but there are special cases, such as 0, 1,2,3,4 Series it has a length of 5, and the actual length is 4, so the odd sequence will need to be adjusted if a series of numbers extends to 0 and a negative range.
On the basis of the above analysis, using the symmetry of the series, special handling n is odd and n=1 case, n is an even number of direct enumeration sequence length, the sequence length itself also has parity, encountered odd sequence length is judged: n can divide the length of the series if it is the solution The length of an even sequence is judged by whether the remainder of the length of the sequence is evenly divided by half of the series length, and if it is the solution.
So complicated. The exam is absolutely unexpected.
This would like to cheat a few, unexpectedly each group of data has a value of "1139300094624923648", just can't live, a point did not.
var I,j,n,m,ans:int64;
Begin
Assign (input, ' sequence.in ');
Reset (input);
Assign (output, ' sequence.out ');
Rewrite (output);
While don't EOF do
Begin
READLN (n);
Ans:=-1;
If N=1 then Ans:=-1
Else
If n mod 2<>0 then ans:=2
Else
Begin
I:=n;
While I mod 2=0 do i:=i Div 2;
if (i+1)/2-n/i>0then
Begin
M:=n Div i*2;
Ans:=m;
End
m:=3;
while (M*m<=i) and ((Ans=-1) or (M<ans)) do
Begin
If I mod m=0 then
If n/m-(m-1)/2>0then
Begin
Ans:=m;
Break
End
m:=m+2;
End
M:=i;
if (m<>1) and (n/m-(m-1)/2>0) and ((Ans=-1) or (M<ans)) Then
Ans:=m;
End
Writeln (ANS);
End
Close (input); close (output);
End.