The article was stolen or very serious, plus copyright information
Reprint please specify the source [ametake All rights reserved]http://blog.csdn.net/ametake Welcome to see
Topic:
Title Description
Description
There is an infinite sequence as follows: 110100100010000100000 ... Please find out the number at the specified position in this infinite sequence.
Enter a description input
Description
The first line is a positive integer n, which indicates the number of queries, and the next n rows, one positive integer per line, ai,ai the position in the sequence.
outputs description output
Description
n rows, 0 or 1 per action, representing the number on the AI bit of the sequence.
sample input to
sample
4
3
14
7
6
Sample output Sample
outputs
0
0
1
0
data
size & Hint
"Data range" has n≤1500000,ai≤10^9 for 100% of data
We observed the sequence and found that the sequence could be divided into groups of 1 and many 0 0 depending on the number of 1 in front of him 1
This topic has a lot of practice, look at the size of the data, a single query must be O (1) time, but the ontology data is rather weak =
First put on my own practice, and the solution of a classmate is similar to the simulation.
The first thought was to store the numbers in each location directly to the table, but obviously not enough space. 256M can open an array of 100 million, but the subject needs 1 billion = =
So instead of using the array f[i] to represent the position of the I 1, so only 50,000 or so 50,000 square is 2.5 billion.
Then for each query lookup there is no I make f[i]==x can
Find can be optimized to optimize for two points, there are faster, take advantage of each 1 position is n (n+1)/2 to narrow the look-up to several numbers
But my optimization failed = = Fortunately the data is very weak, from 1 to MAXN lookup can be
On the Code
Obviously there are more and better ways to do this, let me summarize:
1. Leo's practice, direct solution Equation N (n+1)/2=x integer Solution If there is 1 otherwise 0
2. I don't quite understand this approach.
"Determine if the 8*a-7 is a full square number
i.e. whether trunc (sqrt (8*a-7)) =sqrt (8*a-7) "
3. A method of number theory:
Only the nearest 1 of the distance k is required.
It is not difficult to find that each occurrence 1 positions are in accordance with N (n+1)/2+1, so the following deduction:
Set k=n* (n+1)/2 (n is the nearest 1 location, K is the number of digits questioned
Then there are 2*k=n* (n+1)
Visible N=trunc (sqrt (n. n+1)) =trunc (sqrt (2*k))
After finding N, just determine if K equals n (n+1)/2+1 can
T:=trunc (sqrt (2*k));
If k<> (t*t+t) Div 2+1 Then
Ans[i]:=0
Else
Ans[i]:=1;
Some places do not understand, especially take the whole here trunc is to end
4. Another number theory is similar to the above, but there is a little understanding
Suppose we encounter an input n and assume that it is in the group M.
Thereupon: 0.5* (m-1) *m<n<=0.5*m* (m+1)
2, Get: m* (m-1) <2n<=m* (m+1)
To shrink, get: (m-1) * (m-1) <2n< (m+1) * (m+1)
In this way, we just need to take the 2n root and then the whole, then the resulting number is either m, or m-1, make a judgment can easily solve the M
And M is just the number of n in the group!
In this way, we use the O (1) method to find the number of N in the group. Seconds to kill the problem.
All right, worship the great gods above, more than Codevs.
So this is the end of the problem, the afternoon or to refuel AH
--the first day of the creek cloud, hinting.
The article was stolen or very serious, plus copyright information
Reprint please specify the source [ametake All rights reserved]http://blog.csdn.net/ametake Welcome to see
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Basic Exercise" "Number theory/simulation" codevs1670 Infinite sequence Solving