Tidal Lighthouse
2026-08-07
This is a fun OI problem regarding sequences that I came up with. You can submit your solution here.
Time limit: 1.0 s · Memory limit: 128 MB · Modulus: 998244353
Background
On the cliffs of Kaer stands a lighthouse. Its beacon sits at the centre of the tower, and every year the masons add one lamp niche above it and one below, so the tower is always symmetric about the beacon: in year it holds niches.
The lamps of Kaer burn with a cold, greedy flame. If two adjacent niches are lit on the same night their beams interfere and the whole tower goes dark, so the keeper must choose his pattern with care — any subset of niches, the empty one included, so long as no two chosen niches are neighbours.
The keeper is old now. His logbook devotes one page to each year of his service, and on that page he wrote down every pattern that would have been legal that year. He wants to know how many patterns are written in the whole book.
Statement
In year the tower has niches in a vertical line, numbered to . A pattern for year is a subset containing no two consecutive integers. Let be the number of such patterns.
Given , compute
There are independent queries.
Input
Line 1: integer . Next lines: one integer each.
Output
lines, .
Sample 1
5
1
2
3
5
10
2
7
20
143
17710
Explanation. Year 1: one niche, patterns , so . Year 2: three niches, , running total . Year 3: five niches, , total .
Sample 2
3
1000000
1000000000000000000
123456789012345678
140464712
962211619
66528294
Constraints
For all data: , .
| Subtask | Points | ||
|---|---|---|---|
| 1 | 8 | ||
| 2 | 12 | ||
| 3 | 15 | ||
| 4 | 20 | ||
| 5 | 45 |
Click to reveal solution
Step 1 — identify
counts independent sets on a path of vertices. Conditioning on the last niche gives the Fibonacci recurrence, so a path of vertices has independent sets (), hence
Only odd-indexed Fibonacci numbers appear. The familiar does not apply.
Step 2 — the odd-index identity
Write each term as a difference and telescope:
Check: . ✓ . ✓
The two companion identities worth knowing:
Step 3 — one Fibonacci number, fast
Fast doubling (, ) or the matrix power gives in . Total .
Overflow trap: the index is , up to . It fits in a signed 64-bit integer, but only just — and it will silently break any 32-bit index.
Reference solution
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long u64;
const u64 MOD = 998244353;
pair<u64,u64> fib(u64 n){ // {F(n), F(n+1)}, F(0)=0, F(1)=1
if(!n) return {0,1};
auto [a,b] = fib(n>>1);
u64 c = a * ((2*b % MOD + MOD - a) % MOD) % MOD;
u64 d = (a*a + b*b) % MOD;
if(n & 1) return {d, (c+d)%MOD};
return {c, d};
}
int main(){
int T; scanf("%d",&T);
while(T--){
u64 n; scanf("%llu",&n);
printf("%llu\n", (fib(2*n+2).first + MOD - 1) % MOD);
}
}
Tags:OI