考虑线性dp

状态设计为dp[i][j],表示考虑了后i个元素,结果为j的所有方案数

我们发现dp[i+1][(a[i+1]+j)%10]和dp[i+1][(1LL*a[i+1]*j)%10]可以从dp[i][j]转移过来,转移方式就是前者直接加上后者的值,估转移方程为

dp[i+1][(a[i+1]+j)%10] += dp[i][j]

dp[i+1][(1LL*a[i+1]*j)%10] += dp[i][j]

那么此时再枚举0~9,就是对应0~9所有情况的方案数了,而dp[n]数组就是我们要的答案。

实现:虽然dp的题下标一般从1开始,但是此题从0开始影响也不大,我此处的代码是0base;从后往前考虑不太符合直觉,我们把数组a翻转一下,变成从前往后考虑;考虑对1E9+7取模,可以自己在计算时取模,但是我为了图方便,直接套用jiangly老师的自动取模板子;测试点有n = 1的情况,且该元素大于9,由于一个元素无法进行操作,那么此时是不能对该元素模10的,此时所有方案均无解,我们在dp初始化的时候需要考虑到该情况。

正好最近也在学习Python,贴一份C++代码,再贴一份Python代码

#include <iostream>
#include <vector>
#include <cassert>
#include <algorithm>

using i64 = long long;

template<class T>
constexpr T power(T a, i64 b) {
  T res = 1;
  for (; b; b /= 2, a *= a) {
    if (b % 2) {
      res *= a;
    }
  }
  return res;
}

template<int P>
struct MInt {
  int x;
  constexpr MInt() : x{} {}
  constexpr MInt(i64 x) : x{norm(x % getMod())} {}

  static int Mod;
  constexpr static int getMod() {
    if (P > 0) {
      return P;
    } else {
      return Mod;
    }
  }
  constexpr static void setMod(int Mod_) {
    Mod = Mod_;
  }
  constexpr int norm(int x) const {
    if (x < 0) {
      x += getMod();
    }
    if (x >= getMod()) {
      x -= getMod();
    }
    return x;
  }
  constexpr int val() const {
    return x;
  }
  explicit constexpr operator int() const {
    return x;
  }
  constexpr MInt operator-() const {
    MInt res;
    res.x = norm(getMod() - x);
    return res;
  }
  constexpr MInt inv() const {
    assert(x != 0);
    return power(*this, getMod() - 2);
  }
  constexpr MInt& operator*=(MInt rhs)& {
    x = 1LL * x * rhs.x % getMod();
    return *this;
  }
  constexpr MInt& operator+=(MInt rhs)& {
    x = norm(x + rhs.x);
    return *this;
  }
  constexpr MInt& operator-=(MInt rhs)& {
    x = norm(x - rhs.x);
    return *this;
  }
  constexpr MInt& operator/=(MInt rhs)& {
    return *this *= rhs.inv();
  }
  friend constexpr MInt operator*(MInt lhs, MInt rhs) {
    MInt res = lhs;
    res *= rhs;
    return res;
  }
  friend constexpr MInt operator+(MInt lhs, MInt rhs) {
    MInt res = lhs;
    res += rhs;
    return res;
  }
  friend constexpr MInt operator-(MInt lhs, MInt rhs) {
    MInt res = lhs;
    res -= rhs;
    return res;
  }
  friend constexpr MInt operator/(MInt lhs, MInt rhs) {
    MInt res = lhs;
    res /= rhs;
    return res;
  }
  friend constexpr std::istream& operator>>(std::istream& is, MInt& a) {
    i64 v;
    is >> v;
    a = MInt(v);
    return is;
  }
  friend constexpr std::ostream& operator<<(std::ostream& os, const MInt& a) {
    return os << a.val();
  }
  friend constexpr bool operator==(MInt lhs, MInt rhs) {
    return lhs.val() == rhs.val();
  }
  friend constexpr bool operator!=(MInt lhs, MInt rhs) {
    return lhs.val() != rhs.val();
  }
};

template<>
int MInt<0>::Mod = 998244353;

template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

constexpr int P = 1000000007;
using Z = MInt<P>;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);

  int n;
  std::cin >> n;

  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }

  std::reverse(a.begin(),a.end());

  auto dp = std::vector(n, std::vector<Z>(10));

  if(n > 1 || (n == 1 && a[0] < 10)){
    dp[0][a[0]%10] = 1;
  }

  for(int i = 1; i < n; i++){
    for(int j = 0; j < 10; j++){
      dp[i][(a[i]+j)%10] += dp[i-1][j];
      dp[i][(1LL*a[i]*j)%10] += dp[i-1][j];
    }
  }

  for(int i = 0; i < 10; i++){
    std::cout << dp[n-1][i] << " \n"[i==9];
  }
}
import sys

input = lambda : sys.stdin.readline().strip()

P = 10**9+7

n = int(input())
a = list(map(int,input().split()))[::-1]

dp = [[0]*10 for i in range(n)]

if n > 1 or n == 1 and a[0] < 10:
  dp[0][a[0]%10] = 1

for i in range(1,n):
  for j in range(10):
    dp[i][(j+a[i])%10] += dp[i-1][j]
    dp[i][(j+a[i])%10] %= P
    dp[i][(j*a[i])%10] += dp[i-1][j]
    dp[i][(j*a[i])%10] %= P

print(' '.join(str(x) for x in dp[n-1]))