All Euler problems
Project Euler

Conjunctive Sequences

A sequence (x_1, x_2,..., x_n) of non-negative integers is called conjunctive if for every consecutive pair, x_i mathbin& x_(i+1) ne 0 (where & denotes bitwise AND). Let c(n, b) denote the number o...

Source sync Apr 19, 2026
Problem #0774
Level Level 37
Solved By 170
Languages C++, Python
Answer 459155763
Length 489 words
linear_algebradigit_dpalgebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let ’\(\& \)’ denote the bitwise AND operation. For example, \(10\,\& \, 12 = 1010_2\,\& \, 1100_2 = 1000_2 = 8\).

We shall call a finite sequence of non-negative integers \((a_1, a_2, \ldots , a_n)\) conjunctive if \(a_i\,\& \, a_{i+1} \neq 0\) for all \(i=1\ldots n-1\).

Define \(c(n,b)\) to be the number of conjunctive sequences of length \(n\) in which all terms are \(\le b\).

You are given that \(c(3,4)=18\), \(c(10,6)=2496120\), and \(c(100,200) \equiv 268159379 \pmod {998244353}\).

Find \(c(123,123456789)\). Give your answer modulo \(998244353\).

Problem 774: Conjunctive Sequences

Mathematical Foundation

Definition. Let B=log2b+1B = \lfloor \log_2 b \rfloor + 1 be the number of bits needed to represent integers up to bb. For a non-negative integer xbx \le b, define its bit-set bits(x)={j:bit j of x is 1}{0,1,,B1}\text{bits}(x) = \{j : \text{bit } j \text{ of } x \text{ is } 1\} \subseteq \{0, 1, \ldots, B-1\}.

Lemma 1 (AND Condition via Bit-sets). x&y0x \mathbin{\&} y \ne 0 if and only if bits(x)bits(y)\text{bits}(x) \cap \text{bits}(y) \ne \emptyset.

Proof. x&yx \mathbin{\&} y has a 11 in bit position jj iff both xx and yy have a 11 in position jj. So x&y0x \mathbin{\&} y \ne 0 iff some bit position is shared, i.e., bits(x)bits(y)\text{bits}(x) \cap \text{bits}(y) \ne \emptyset. \square

Definition. For a subset T{0,1,,B1}T \subseteq \{0, 1, \ldots, B-1\}, define w(T)={x:0xb,bits(x)T}w(T) = |\{x : 0 \le x \le b, \text{bits}(x) \supseteq T\}| — the count of integers up to bb that have all bits in TT set.

Lemma 2 (Inclusion-Exclusion for Compatible Counts). For a given xx with bits(x)=S\text{bits}(x) = S, the number of integers yby \le b with x&y0x \mathbin{\&} y \ne 0 is

A(S)=TS(1)T+1w(T).A(S) = \sum_{\emptyset \ne T \subseteq S} (-1)^{|T|+1} w(T).

Proof. By inclusion-exclusion on the events “bit jj of yy is set” for jSj \in S:

A(S)=jS{yb:jbits(y)}=TS(1)T+1{yb:Tbits(y)}.A(S) = \left|\bigcup_{j \in S} \{y \le b : j \in \text{bits}(y)\}\right| = \sum_{\emptyset \ne T \subseteq S} (-1)^{|T|+1} |\{y \le b : T \subseteq \text{bits}(y)\}|. \quad\square

Theorem 1 (Subset-based Transfer Matrix). Define for each non-empty subset S{0,,B1}S \subseteq \{0, \ldots, B-1\} the count α(S)={x:0xb,bits(x)=S}\alpha(S) = |\{x : 0 \le x \le b, \text{bits}(x) = S\}|. The number of conjunctive sequences of length nn is

c(n,b)=S1,,Sni=1nα(Si)i=1n11[SiSi+1].c(n, b) = \sum_{S_1, \ldots, S_n \ne \emptyset} \prod_{i=1}^{n} \alpha(S_i) \cdot \prod_{i=1}^{n-1} \mathbf{1}[S_i \cap S_{i+1} \ne \emptyset].

This is equivalent to computing the (n1)(n-1)-th power of a transfer matrix MM indexed by non-empty subsets of {0,,B1}\{0, \ldots, B-1\}, where M[S,S]=1[SS]M[S, S'] = \mathbf{1}[S \cap S' \ne \emptyset].

Proof. Each term xix_i with bits(xi)=Si\text{bits}(x_i) = S_i contributes α(Si)\alpha(S_i) choices. The conjunctive condition xi&xi+10x_i \mathbin{\&} x_{i+1} \ne 0 is equivalent to SiSi+1S_i \cap S_{i+1} \ne \emptyset by Lemma 1. The matrix product formulation follows from the standard transfer matrix method. \square

Theorem 2 (Mobius Inversion on the Boolean Lattice). The transfer matrix MM can be diagonalized using the Mobius function of the Boolean lattice (i.e., the zeta/Mobius transforms). Specifically, M[S,S]=11[SS=]M[S, S'] = 1 - \mathbf{1}[S \cap S' = \emptyset], so

M=JDM = J - D

where J[S,S]=1J[S, S'] = 1 for all S,SS, S' and D[S,S]=1[SS=]D[S, S'] = \mathbf{1}[S \cap S' = \emptyset]. The matrix DD factors over bits: D[S,S]=j=0B1dj(S,S)D[S, S'] = \prod_{j=0}^{B-1} d_j(S, S') where dj=1d_j = 1 if jSj \notin S or jSj \notin S', i.e., dj=11[jS]1[jS]d_j = 1 - \mathbf{1}[j \in S]\mathbf{1}[j \in S']. This tensor product structure enables efficient computation via subset convolution and the “sum over supersets” (SOS) transform.

Proof. SS=S \cap S' = \emptyset iff for every bit jj, at least one of S,SS, S' does not contain jj. This gives the product decomposition. The SOS transform diagonalizes each factor, reducing the matrix power to pointwise exponentiation in the transform domain. \square

Lemma 3 (Efficient Computation via Ranked Mobius Transform). Using the ranked Mobius transform (subset convolution), the matrix power Mn1M^{n-1} applied to the initial vector α\alpha can be computed in O(2BB2)O(2^B \cdot B^2) time per step and O(B)O(B) matrix power steps (via polynomial multiplication in the rank variable), giving total time O(2BB2logn)O(2^B \cdot B^2 \cdot \log n) with repeated squaring.

Proof. The subset convolution framework of Bjorklund et al. (2007) allows computing the “OR-convolution restricted to disjoint sets” in O(2BB2)O(2^B \cdot B^2) time. The transfer matrix multiplication in the transform domain becomes pointwise polynomial multiplication (polynomials in the rank variable of degree at most BB). Matrix exponentiation via repeated squaring requires O(logn)O(\log n) multiplications. \square

Editorial

Sequence of non-negative integers where consecutive terms have nonzero bitwise AND. c(n,b)c(n,b) = count of length-nn sequences with terms b\le b. Find c(123,123456789)mod998244353c(123, 123456789) \bmod 998244353. We compute alpha(S) for all non-empty S via digit DP. We then alpha(S) = count of x in [0, b] with bits(x) = S. Finally, where w(T) = floor(b / 2^{bits in T positions}) … (digit DP for upper bound).

Pseudocode

Compute alpha(S) for all non-empty S via digit DP
alpha(S) = count of x in [0, b] with bits(x) = S
Use inclusion-exclusion: alpha(S) = sum_{T supseteq S} (-1)^{|T|-|S|} w(T)
where w(T) = floor(b / 2^{bits in T positions}) ... (digit DP for upper bound)
Actually, compute w(T) = |{x <= b : T subseteq bits(x)}| via digit DP
Then Mobius invert: alpha(S) = sum_{T supseteq S} (-1)^{|T\S|} w(T)
Set up the transfer matrix in subset convolution domain
Transform alpha into the ranked Mobius domain
f_hat[S][rank] = ranked zeta transform of alpha
Compute the n-th power
In the transform domain, perform pointwise polynomial exponentiation
Each point has a polynomial of degree <= B, raise to power (n-1)
Use matrix exponentiation or polynomial repeated squaring
Inverse transform to get c(n, b)
Sum over all S of the result vector
Detailed subset convolution approach:
Ranked zeta transform
In transform domain: g_hat[S] = polynomial, raise to power n-1
Then multiply by f_hat to get the convolution
This requires O(2^B * B^2 * log(n)) operations
Count x in [0, b] such that T is a subset of bits(x)
Standard digit DP on the binary representation of b

Complexity Analysis

  • Time: O(2BB2logn)O(2^B \cdot B^2 \cdot \log n) where B=27B = 27 (since log2(123456789)26.9\log_2(123456789) \approx 26.9). This gives approximately 22727276.8×10102^{27} \cdot 27^2 \cdot 7 \approx 6.8 \times 10^{10}, which is tight but feasible with constant-factor optimizations. Alternatively, an eigenvalue-based approach exploiting the symmetric structure reduces this further.
  • Space: O(2BB)227273.6×109O(2^B \cdot B) \approx 2^{27} \cdot 27 \approx 3.6 \times 10^9 bits 450\approx 450 MB, which is tight. Optimizations (e.g., working in compressed representations) may be needed.

Answer

459155763\boxed{459155763}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_774/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 774: Conjunctive Sequences
 *
 * Sequence of non-negative integers where consecutive terms have nonzero bitwise AND. $c(n,b)$ = count of length-$n$ sequences with terms $\le b$. Find 
 */

int main() { printf("Problem 774: Conjunctive Sequences\n"); return 0; }