Project Euler 778
Project Euler 778
题目
Freshman’s Product
If $a,b$ are two nonnegative integers with decimal representations $a=(\dots a_2a_1a_0)$ and $b=(\dots b_2b_1b_0)$ respectively, then the freshman’s product of $a$ and $b$, denoted $a\boxtimes b$, is the integer $c$ with decimal representation $c=(\dots c_2c_1c_0)$ such that $c_i$ is the last digit of $a_i\cdot b_i$.
For example, $234 \boxtimes 765 = 480$.
Let $F(R,M)$ be the sum of $x_1 \boxtimes \dots \boxtimes x_R$ for all sequences of integers $(x_1,\dots,x_R)$ with $0\leq x_i \leq M$.
For example, $F(2, 7) = 204$, and $F(23, 76) \equiv 5870548 \pmod{ 1\,000\,000\,009}$.
Find $F(234567,765432)$, give your answer modulo $1\,000\,000\,009$
解决方案
需要注意的是,本题中的每一个数位都是独立的,因此我们考虑计算出每个数位。
假设$M$是一个$l$位数$m{l-1}m{l-2}\dots m_0$。我们考虑单独计算$0\sim n$这些数中有多少个有前导$0$的数的第$k$个数位为$d$,记为$g(n,k,d)$。那么通过三部分考虑,可以写出$g$为:
单独考虑序列$x$中所有数的第$k$位。令状态$f_k(i,j)(0\le i\le R,0< j <10)$序列$x_1,x_2,\dots,x_i$的第$k$位有多少种填法,使得$x_1\boxtimes x_2\boxtimes\dots\boxtimes x_i$的第$k$位为$j$。那么不难写出状态转移方程为
对于方程最后一行,序列$x_i$的第$k$位有$g(M,k,d)种填法$,从而将$f_k(i-1,j)$转移到$f_k(i,j)$。
使用矩阵快速幂,可以将$f_k(R,\cdot)$以$O(\log R)$的时间复杂度计算出来。
因此最终答案为:
代码
1 | from itertools import count |