We need to parse the problem carefully. It says: "Golu hates odd numbers. When he learns about binary strings he wants to find out how many binary strings of given length N exist without an odd number of consecutive 1's." Then example: For N=4 1001,1010,1110,0001 are strings which contain odd number of 1's where 1100,1111 are not. Leading zeros are allowed.
Wait: The example is confusing: It says "1001,1010,1110,0001 are strings which contain odd number of 1's where 1100,1111 are not." Actually, it's saying which strings contain odd number of consecutive 1's? Let's read: "For N=4 1001,1010,1110,0001 are strings which contain odd number of 1's where 1100,1111 are not." That seems contradictory: 1001 contains groups of 1s: "1", then "1" at the end – each group length is 1, which is odd. So it contains odd number of consecutive 1's? Actually, the phrase "odd number of consecutive 1's" might mean that the number of consecutive 1's in any block is odd? Or does it mean that the count of consecutive 1's (the length of each run) is odd? Or does it mean that the total number of ones that are consecutive? The wording is ambiguous: "without an odd number of consecutive 1's." Typically, "consecutive 1's" refers to a block of 1's. So "odd number of consecutive 1's" would mean a block whose length is odd. So the condition is that the binary string should NOT contain any block of consecutive 1's that has an odd length. In other words, every run of 1's must have even length (including possibly length 0, i.e., no 1's). So the allowed strings are those where all consecutive 1's runs are of even length.
But wait, the example: For N=4, they list 1001,1010,1110,0001 as strings which contain odd number of 1's where 1100,1111 are not. Let's examine:
- 1001: runs: "1", "00", "1" – each run of 1's is length 1 (odd) => contains odd-length run, so it is a string that contains an odd number of consecutive 1's (i.e., it violates the condition). So they are examples of strings that contain odd number of consecutive 1's? They say "which contain odd number of 1's" but they probably mean "odd number of consecutive 1's"? Actually they wrote: "1001,1010,1110,0001 are strings which contain odd number of 1's where 1100,1111 are not." That is ambiguous: They might be saying these strings contain odd number of 1's (total count of ones) or odd number of consecutive 1's? Let's check: 1001 has two ones, even total count, but they say it contains odd number of 1's? That would be false. So they must mean odd number of consecutive 1's (i.e., an odd-length run). Similarly, 1010 has two ones, but each run is length 1 (odd) => contains odd runs. 1110 has three consecutive ones (odd) => odd run. 0001 has a run of one 1 (odd) => odd run. So these are violating strings. Then 1100: runs: "11" (even length) and then zeros, so all runs of 1's are even length => no odd run. 1111: "1111" even length => no odd run. So these are valid strings. So indeed, condition: strings without any odd-length run of consecutive 1's.
Thus problem: Count binary strings of length N where every maximal consecutive segment of 1's has even length (i.e., lengths are 0,2,4,6,...). Note: string can be all zeros, that's okay (run length 0 which is even? Actually no 1's at all means there is no run of ones, vacuously satisfying condition). So we need to count such strings.
We need to compute modulo M=1e9+7. N up to 1e6, T up to 100. So precompute up to max N.
This is a known combinatorial problem: strings with no odd runs of ones. Equivalent to: any 1's appear in blocks of even length. This can be modeled as a linear recurrence. Let's derive.
We can think of constructing strings of length n. Let a_n = number of valid strings of length n.
Consider the start: either first character is 0, then the remainder is a valid string of length n-1, contribution: a_{n-1} (since after 0, the rest must be valid). Or first character is 1, but then we must have a block of 1's of even length. So if we start with a 1, the block must be of length at least 2. So the string begins with "11..." and we need to have an even number of leading 1's before a 0 or end. So we can think of placing a block of 1's of length 2k, where k>=1, so block length 2,4,6,... up to n. After this block, either the string ends (if 2k = n) or the next character must be 0 (since block ends, next character cannot be 1 because then they'd merge into a longer block, which would still be even if total length remains even? Actually if we have consecutive ones, they form a single block. So if we have a block of length 2k, then the next character (if any) must be 0 to separate blocks; otherwise if it's 1, they'd merge and the block length would become 2k+1, odd? Not necessarily odd: if you merge, the length becomes 2k+1 (if you add one more 1) which is odd if 2k is even. So that would be invalid. So indeed after a block of even length 1's, we must have a 0 (unless it's the end). So structure: a valid string can be: either empty; or it starts with 0, then a valid string; or it starts with a block "11" (at least 2) then a 0 then a valid string; but also we can have just a block of even length at the end.
We can formulate recurrence using states. Another approach: Consider the generating function or recurrence. Let's define a_n directly.
We can consider the last character. But careful because block length constraints.
Let’s try to derive recurrence: For n >= 1, a valid string ends either with 0 or with a 1 that is part of an even-length block. If it ends with 0, then the preceding n-1 must be valid: a_{n-1}. If it ends with 1, then because the block must be even-length, the last two characters must be "11" and the character just before the start of this block must be 0 (or the block starts at position 1). So basically if the string ends with 1, then the block of trailing ones has even length. So consider the last occurrence of 0. Suppose the string ends with 1, and the block of trailing ones length is 2k (k>=1). Then the string is: (prefix of length n-2k) (which must be valid and ends with 0 or is empty) then 0? Actually if the block length is 2k, then the character immediately before the block (if any) must be 0, otherwise the block would be longer. So the string is: X 0 (1)^{2k}, where X is a valid string (could be empty) and also X by itself is valid. But note: X can be any valid string, and after that valid string we append "0" and then 2k ones. However, is the string formed as X + "0" + 2k ones always valid? Let's verify: X is valid. After X we add a 0, then 2k ones. Since the 0 separates, the block of ones is even, so overall valid. Also X can be empty, giving string "0"+"1"^{2k} which is valid as long as 2k>0. But note that the case where the string starts with ones: that would correspond to X empty and no leading 0? Wait, if we have no 0 before the block, that means the string begins with ones. In our construction X 0 (1)^{2k}, we require a 0 before the block. To include strings that start with a block of ones (i.e., no preceding 0), we can treat X as empty and also allow the possibility that there is no preceding 0? Actually if there is no 0 before the block, then the string is simply (1)^{2k}. This is of the form: (empty) (1)^{2k}. But in our construction X 0 (1)^{2k}, X empty would give "0"+"1"^{2k}, which has a leading 0, not the same. So we need to include strings that start with a block of ones separately. So maybe we should treat two states: strings ending with 0, and strings ending with 1 but with even block length.
Alternate approach: Use state machine with memory of parity of current run length? Let's define DP[n][p] where p = parity (0 for even length run of 1's so far, or maybe the state of the last run?). However, since we require that any run of ones must be even, we must ensure that when a run ends, its length is even. So we can model as we scan left to right, we track whether we are currently in a run of 1's and its length modulo 2. But we also need to know if the run is ongoing, because we cannot end it on odd length.
Simpler: Let’s define f_n = number of valid strings of length n. We can derive recurrence by considering the first block (if any) and using concatenation decomposition.
Another known combinatorial interpretation: Strings where ones occur in pairs. But careful: They can be separated by zeros, but if you have two consecutive 1's, that's a pair, but you could have four consecutive 1's (two pairs) etc. So essentially, the string can be seen as a sequence of "0" and "11" (a double-1) but note that you cannot have a single 1. However, what about runs of length 4? That could be represented as "11" followed by another "11" but they would merge into "1111". But if we treat them as concatenation of blocks "11", we need to allow merging of adjacent "11" blocks without a zero separator. So it's like we have tokens: either "0" or "11", and these tokens can be concatenated, but note that concatenating two "11" tokens yields "1111", which is allowed. So the set of valid strings is exactly the set of strings over alphabet {0, 2} where "2" represents a pair of ones. However, we must consider that a single "0" is fine. So any string can be generated by a sequence of symbols: either a single zero, or a double-one (i.e., "11"). But careful: The generated string length: each zero contributes 1, each double-one contributes 2. So if we want to generate strings of length n, we need to consider compositions of n using parts of size 1 (zero) and 2 (double-one). But also, we need to consider that zeros and double-ones are placed in order. However, there is a nuance: what about strings like "0110"? This would be zero, then double-one, then zero? Actually "0110": positions: 0,1,1,0. This can be parsed as: first char 0 => zero token. Next two chars are "11" => double-one token. Last char 0 => zero token. So yes. What about "1100"? double-one then double-zero? Actually "1100" is double-one then two zeros: "11","0","0". So that's tokens: double-one, zero, zero. That matches. What about "0011"? zeros then double-one. So indeed any valid string can be uniquely parsed as a sequence of tokens: "0" and "11". But is the parsing always unique? For a string that has runs of ones of even length, say length 4: "1111". This can be parsed as "11"+"11" (two double-ones) or as a single token? There's no token of length 4; we only have "11". So the natural parsing is to break the run of ones into consecutive "11" tokens. Since the run length is even, it can be partitioned into pairs in exactly one way if we require the partition to be into contiguous "11" blocks? Actually, a run of length 4 can be partitioned as "11" then "11" (starting at positions 1-2 and 3-4) uniquely because the pairs are non-overlapping and cover the run. Could we also consider it as a single token of length 2? That would not cover the full run. So to parse the whole string, we break runs of ones into consecutive pairs. That yields a unique decomposition into tokens of "0" and "11". So the mapping from valid strings to sequences of tokens is bijective: given a valid string, replace each zero by token "0", and replace each maximal run of ones of even length 2k by k consecutive "11" tokens. Conversely, given a sequence of tokens "0" and "11", concatenating them yields a valid string: zeros remain zeros, and "11" tokens produce runs of ones; if two "11" tokens appear consecutively, they merge into a longer run of even length (since 2+2=4, etc.) which is allowed. So the count of valid strings of length n is equal to the number of sequences of tokens (each either length 1 or 2) that sum to n. This is exactly the number of ways to tile a board of length n with tiles of size 1 (zero) and size 2 (pair of ones). So a_n = number of ways to write n as ordered sum of 1's and 2's. That's simply the Fibonacci numbers: a_n = F_{n+1} where F_1=1, F_2=1? Let's check: For n=1: valid strings length 1. According to condition, any run of ones must be even length. The only possible strings: "0" (valid because no ones) and "1" (invalid because run length 1 odd). So a_1=1. The number of ways to tile length 1 with 1 and 2 tiles: only one way: a single 1-length tile. So a_1=1. For n=2: valid strings: "00","11"? Check "11": run length 2 even => valid. "01": run length 1 odd? "01" has a run of one 1 at position2, length1 odd => invalid. "10": run length1 odd => invalid. So valid: "00" and "11". That's 2. Tiling of length 2: ways: two 1-tiles (1+1), one 2-tile. That's 2. So a_2=2. For n=3: valid strings? Let's list: 000 (valid), 001? ends with one 1 -> run length1 odd invalid. 010? has two runs of length1 each, invalid. 011? run of two ones at end length2 even, but also a single one at position2? Actually 011: positions: 0,1,1: runs: "0", then "11". The run of ones length2 even, and the single 0. No odd runs. So 011 is valid? Wait check: "011" -> it has a run of two consecutive ones (positions2-3). That's even length, so valid. However, does it contain any odd-length run? The only run of ones is length2, even. So it's valid. But careful: The run of ones is at positions 2 and 3? Actually indices 1-based: 0 1 1? Let's write binary string "011": characters: first '0', second '1', third '1'. So run of ones from position2 to 3, length2, even. So it's valid. But earlier we thought "011" might be invalid because there's a single 1? Actually it's two consecutive ones, no single 1. So valid. Next "100": run of ones length1 at start? "100": first char '1', then '0','0' -> run length1 odd -> invalid. "101": runs: '1', '0', '1' -> each run of ones length1 odd -> invalid. "110": run of ones length2 even -> valid. "111": run of ones length3 odd -> invalid. So valid: 000, 011, 110. That's 3 strings. Tiling of length3: ways: 1+1+1; 1+2; 2+1. That's 3. So matches. So indeed a_n = Fib(n+1) if we define Fib(1)=1, Fib(2)=1, then Fib(2)=1? Wait compute: n=1 => a_1=1. Fib(2)=1? Actually Fibonacci numbers: F(0)=0, F(1)=1, F(2)=1, F(3)=2, F(4)=3, F(5)=5,... Then a_n = F_{n+2}? Check: n=1 => F_3=2, but a_1=1, so not that. Using F(0)=0, F(1)=1, then number of ways to tile n with 1 and 2 is F_{n+1} (if F_1=1, F_2=2? Let's standard: often combinatorial: let f(0)=1 (empty tiling), f(1)=1, f(2)=2, f(3)=3, f(4)=5. This is Fibonacci shifted: f(n) = F_{n+1} where F_0=0,F_1=1 => F_2=1,F_3=2,F_4=3,F_5=5. So f(1)=1 = F_2, f(2)=2 = F_3, f(3)=3 = F_4, f(4)=5 = F_5. So indeed a_n = F_{n+1} if F_1=1, F_2=1? Need to be consistent. For our purposes, recurrence: a_n = a_{n-1} + a_{n-2}, with a_1 = 1, a_2 = 2? Wait we found a_3=3, so a_3 = a_2 + a_1? 2+1=3 yes. So recurrence a_n = a_{n-1} + a_{n-2} for n >= 3. And a_1=1, a_2=2. So this is like Fibonacci starting with 1,2: a_1=1, a_2=2, a_3=3, a_4=5, a_5=8, etc. So a_n = F_{n+1} if F_1=1, F_2=1? Let's compute: F_1=1, F_2=1, F_3=2, F_4=3, F_5=5, F_6=8. Then a_1 = F_2 =1, a_2 = F_3=2, a_3 = F_4=3, a_4 = F_5=5. So a_n = F_{n+1} where F_1=1,F_2=1. Alternatively, define Fib[0]=0, Fib[1]=1, then a_n = Fib[n+2]? Because Fib[2]=1, Fib[3]=2, Fib[4]=3, Fib[5]=5. For n=1: Fib[3]=2, not 1. So that's off. So better to use a_n = F_{n+1} with F_1=1,F_2=1.
But wait: is there any nuance about leading zeros? Allowed. So the tiling interpretation is correct.
Thus the answer for each N is the Nth number in the sequence defined by:
a[0] = 1? For n=0, empty string, would count as 1? But N>=1, so not needed. But we might need base for recurrence.
But let's double-check with N=2 sample: they say output 2, matches.
Now the problem statement says: "Now your task is to find how many strings of given length exist without any consecutive 1's." Huh? It says "without any consecutive 1's"? Wait reread: After the description, it says: "Now your task is to find how many strings of given length exist without any consecutive 1's." That seems contradictory to earlier description. Let's read the original text: "Golu hates odd numbers. When he learns about binary strings he wants to find out how many binary strings of given length N exist without an odd number of consecutive 1's. ... Now your task is to find how many strings of given length exist without any consecutive 1's." The last sentence might be a misstatement? It says "without any consecutive 1's" but earlier "without an odd number of consecutive 1's". Which one is correct? The sample: N=2, output 2. Let's test: strings without any consecutive 1's (i.e., no two adjacent 1's) for N=2: valid are 00,01,10 => 3, not 2. So it's not that. So the last sentence is a mistake; the correct problem is the one described earlier: without an odd number of consecutive 1's (i.e., all runs of 1's have even length). So we are correct.
Now we need to compute a_n modulo 1e9+7 for N up to 1e6, multiple test cases. So we can precompute a array up to 1e6 using recurrence a_n = a_{n-1} + a_{n-2} mod M, with a_1=1, a_2=2.
But wait: Let's confirm for N=1: answer should be 1. But the problem constraints 1 ≤ N ≤ 1e6, and they might ask for number of strings without odd consecutive 1's. For N=1, only "0" is valid, so 1. They didn't provide sample for N=1, but it's fine.
Check N=4: According to recurrence a_4 = a_3 + a_2 = 3+2=5. Let's list all 16 binary strings of length 4, and count those with all runs of 1's even. We listed earlier: valid: 0000, 0011, 0110, 1100, 1111? 1111 has run length 4 even => valid. Also 1001? That has runs of length 1 each -> invalid. 1010 invalid, 0101 invalid, 0111? 0111 has run of three ones -> invalid. 1011 invalid, 1101? 1101: run of two ones then a single one at end? Actually 1101: runs: "11" then "0" then "1" (single) -> invalid. 1110 valid? 1110 has three ones -> invalid. 0000 valid. 0011 valid. 0100? 0100: runs: "0","1","00" -> single 1 -> invalid. 1000 invalid. So valid strings: 0000, 0011, 0110, 1100, 1111. Also maybe 1001? No. That's 5. So matches a_4=5.
Thus recurrence is correct.
So answer = a[N] where a[1]=1, a[2]=2, a[i] = a[i-1] + a[i-2] mod M.
But what about N=0? Not needed.
Now the problem might have large N up to 1e6, so we can precompute up to 1e6 easily.
But wait, the input constraints: 1 ≤ T ≤ 100, 1 ≤ N ≤ 1000000. So O(maxN) precomputation and then O(1) per query.
We must be careful about modulo: 1000000007.
Thus solution: For each test case, read N, output dp[N] where dp[1]=1, dp[2]=2, and for i=3..maxN: dp[i] = (dp[i-1] + dp[i-2]) % MOD.
Now we should test with sample: N=2 -> 2, correct.
But wait: Let's check N=3: should be 3. Could sample have been something else? The sample shows N=2 output 2, consistent.
But the problem statement also includes: "Print number of strings without consecutive 1's." That is a misprint. Many online problems have similar "Golu and odd one" problem. Actually, I recall a problem from HackerEarth or something: "Golu and odd one" where the answer is Fibonacci. So it's correct.
However, let's double-check: There is known problem "Count binary strings without consecutive 1's" which is standard Fibonacci (F_{n+2}), but that yields for N=2: count=3 (00,01,10). But sample says 2, so it's different. So it's indeed the even-run condition.
Thus solution is straightforward.
But wait: Could there be any nuance about "odd number of consecutive 1's" meaning "the total number of consecutive 1's in the string is odd"? That would be total count of 1's that are consecutive? That phrasing could be interpreted as "the string contains an odd count of consecutive 1's" meaning there is some run of consecutive 1's whose length is odd. Our interpretation matches the example. Good.
Thus answer: Fibonacci-like numbers.
Now we need to output modulo 1e9+7.
Now, can N be as large as 10^6? That's fine; compute iteratively.
Edge Cases: N=1 => output 1. N=2 => 2.
Implementation: Precompute array size 1000000+1 (index 1..1000000). Use long long int for mod.
Pseudo:
#include <stdio.h>
#define MOD 1000000007
int main() {
int t, n, max_n = 0;
read t;
store queries maybe to compute max.
// Or precompute up to 1e6 anyway.
const int MAXN = 1000000;
long long dp[MAXN+1];
dp[1] = 1; dp[2] = 2;
for(int i=3; i<=MAXN; i++) dp[i] = (dp[i-1] + dp[i-2]) % MOD;
while(t--) {
read n;
print dp[n];
}
return 0;
}
But note: For n=1, we access dp[1], set. For n=2, dp[2]. Good.
Potential overflow: Using long long, but mod is < 2^31, so int is fine but sum may exceed int before mod, so use long long or int with casting.
Time: O(MAXN) ~ 1e6 operations, trivial.
Now, confirm that dp[1000000] will be within mod.
Thus solution done.
But maybe there is alternative interpretation: Some might think it's strings without any consecutive 1's that are odd? That is exactly our interpretation.
Thus final answer: precomputed Fibonacci with base 1,2.
Wait, let's read the problem again as provided: "Now your task is to find how many strings of given length exist without any consecutive 1's." That is a contradictory statement. Possibly they mistakenly wrote "without any consecutive 1's" but they meant "without any odd consecutive 1's". But the sample clarifies it's not the standard no-consecutive-1s. So follow sample.
Thus we can answer.
Now, also we need to consider that the string may be empty? N>=1 so no.
Thus solution is simple.
But wait, the problem might be from some contest where they asked to count strings without any odd-length run of consecutive 1's. There's a known trick: the answer is Fibonacci. Indeed.
Now for completeness, let's derive formally: Let f(n) be number of valid strings of length n. Consider first character. If first is 0, then remaining f(n-1) possibilities. If first is 1, then because runs must be even, the next character must also be 1 (since block length must be at least 2). So the first two are "11". After that, the third character can be either 0 or if there are more 1's we need to keep even. However, after "11", we can either stop (if n=2) or continue. But more systematically: If first is 1, then second must be 1. So we have "11" then the rest of string from position 3 must be a valid string, but careful: Because after "11", we can either have a 0, or if we have another 1, then that extends the block. However, if we have another 1, then the block length becomes 3 which is odd, which is not allowed. So after "11", we cannot have a 1 immediately? Wait, we can have a 1 if it continues? Let's check: For a valid string, runs of ones must be even. So if we start with "11", that's even length 2. If we then add another 1, the run becomes "111", length 3 odd -> invalid. So you cannot have a third consecutive 1 after two 1's? Actually, you can if you add a fourth 1 as well: "1111" is even length. But can you have "111"? That's invalid because the run length is 3 odd. So to have a longer even run, you need to add an even number of extra ones. So after "11", you could add another "11" to get "1111". That is allowed. So the third character could be 1 if the fourth is also 1, making the block of length 4. But then the block continues. So we need to consider that.
Thus the simpler tiling approach is better.
Thus answer: f(n) = f(n-1) + f(n-2) with f(1)=1, f(2)=2.
Now confirm with N=0? Not needed.
Now, the problem might also ask for huge N up to 10^18? But constraints say up to 1e6, so linear precomputation is fine.
But we can also use matrix exponentiation if needed.
Thus code is straightforward.
I will produce a solution in C++.
Now, double-check sample input:
1
2
Output: 2
Yes.
Now I'll produce final answer.
But wait: The problem description includes "without any consecutive 1's" but the sample matches our interpretation. Could it be that they actually want strings that do not have consecutive 1's at all? That would give 3 for N=2, but they output 2. So definitely they mean something else.
Thus proceed.
Edge: For N=1, answer = 1.
Now implement.
Now think about memory: dp array of size 1e6+1 of long long => ~8 MB, fine.
Alternatively, we could compute on the fly with two variables but since multiple queries we precompute.
Now code.
But I'll also include explanation.
Now one more nuance: The problem statement: "without an odd number of consecutive 1's." Could also be interpreted as "without having an odd number of 1's that are consecutive"? That is the same as we used.
Now confirm with N=5: According to recurrence, f(5)=f(4)+f(3)=5+3=8. Let's quickly check manually? Possibly correct. For sanity, number of strings of length 5 = 32, so 8 is plausible.
Now we can finalize.
Thus final answer: Print dp[N] mod 1e9+7.
Now produce final code.
Let me write in C++:
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MAXN = 1000000;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<long long> dp(MAXN+1);
dp[1] = 1;
dp[2] = 2;
for (int i=3; i<=MAXN; ++i) {
dp[i] = (dp[i-1] + dp[i-2]) % MOD;
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << dp[n] << "\n";
}
return 0;
}
That should work.
But the problem might have T up to 100, N up to 10^6, so okay.
Now answer.
But ensure to handle case N=0? Not needed.
Now final. The number of binary strings of length (N) where every run of consecutive 1's has even length follows the recurrence
(a_1 = 1), (a_2 = 2), and for (n \ge 3): (a_n = a_{n-1} + a_{n-2} \pmod{10^9+7}).
This is equivalent to the number of ways to tile a board of length (N) with tiles of size (1) (representing '0') and size (2) (representing '11').
We precompute (a_i) for all (i) up to (10^6) and answer each test case in constant time.
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int MAXN = 1000000;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<long long> dp(MAXN + 1);
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= MAXN; ++i) {
dp[i] = (dp[i-1] + dp[i-2]) % MOD;
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
cout << dp[n] << "\n";
}
return 0;
}