var | desc |
---|---|
Length of bit string |
00
01
10
11
Possible combinations =
000
001
010
011
100
101
110
111
Possible combinations =
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
Possible combinations =
The formula would be:
However this algorithm would cause an overflow with u64
and even with u128
.
So instead we iterate over range
The formula would look like this:
We multiply by 2 because for each 0
and 1
.
Also on each iteration we apply
For example if res = 1
:
So after iterating three times res = 8
. This would be the same as
This is possible because of the following rule in modulo arithmetic:
In Rust 🦀 code:
fn main() {
const MOD: u64 = 1_000_000_007; // 10 ^ 9 + 7
let n: u64 = std::io::read_to_string(std::io::stdin())
.unwrap()
.trim()
.parse()
.unwrap();
let res = (0..n).fold(1, |res, _| 2 * res % MOD);
println!("{res}");
}