In this problem \(\oplus\) is used
to represent the bitwise exclusive or of two
numbers.
Starting with blank paper repeatedly do the following:
Write down the smallest positive integer \(a\) which is currently not on the
paper;
Find the smallest positive integer \(b\) such that neither \(b\) nor \((a
\oplus b)\) is currently on the paper. Then write down both \(b\) and \((a
\oplus b)\).
After the first round \(\{1,2,3\}\)
will be written on the paper. In the second round \(a=4\) and because \((4 \oplus 5),(4 \oplus 6)\) and \((4 \oplus 7)\) are all already written
\(b\) must be \(8\).
After \(n\) rounds there will be
\(3n\) numbers on the paper. Their sum
is denoted by \(M(n)\).
For example, \(M(10) = 642\) and
\(M(1000) = 5432148\).
Find \(M(10^{18})\). Give your
answer modulo \(1\,000\,000\,007\).
解决方案
使用以下程序暴力打印前面几行的规律,并得到结果:
1 2 3 4 5 6 7 8 9 10 11 12
st = set() for i inrange(100): x = 1 while x in st: x += 1 y = x + 1 while y in st or x ^ y in st: y += 1 w = x + y + (x ^ y) print(i, x, y, x ^ y, w) st |= {x, y, x ^ y}
deff(n, a, b, c, m): if n <= 0: return0 pw = 1 << (2 * m) if n > pw: return (f(pw, a, b, c, m) + f(n - pw, a << 2, b << 2, c << 2, m + 1)) % mod elif n == pw: returnsum((x + x + pw - 1) * pw // 2for x in [a, b, c]) %mod else: pw2 = pw >> 2 ans = f(min(n, pw2), a, b, c, m - 1) ans += f(min(n - 1 * pw2, pw2), a + 1 * pw2, b + 2 * pw2, c + 3 * pw2, m - 1) ans += f(min(n - 2 * pw2, pw2), a + 2 * pw2, b + 3 * pw2, c + 1 * pw2, m - 1) ans += f(min(n - 3 * pw2, pw2), a + 3 * pw2, b + 1 * pw2, c + 2 * pw2, m - 1) return ans % mod