Official

D - FG operation Editorial by en_translator


We solve it in distributing DP (Dynamic Programming).

Let \(dp[i][j]:\) the number of ways of operations for the first \(i\) elements, after which the first element of the sequence becomes \(j\).

The initial values are

  • \(dp[1][j] = 1 (j = A_1)\),
  • \(dp[1][j] = 0 (j \neq A_1)\).

The transitions from \(dp[i][j]\) are

  • \(dp[i+1][(j+A_{i+1}) \%10]\), and
  • \(dp[i+1][(j\times A_{i+1}) \%10]\).

The final answer is \(dp[N][K]\).

The following is a sample code in C++.

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
using mint = modint998244353;

signed main(){
    ll n;cin>>n;
    vector<ll>a(n+1);
    for(ll i=1;i<=n;i++)cin>>a[i];
    mint dp[n+1][10];memset(dp,0,sizeof(dp));
    dp[1][a[1]] = 1;

    for(ll i=1;i<=n-1;i++){
        for(ll j=0;j<=9;j++){
            dp[i+1][(j+a[i+1])%10] += dp[i][j];
            dp[i+1][(j*a[i+1])%10] += dp[i][j];
        }
    }
    for(ll K=0;K<=9;K++){
        cout<<dp[n][K].val()<<endl;
    }
    return 0;
}

posted:
last update: