SUB7NUM-Editorial

PROBLEM LINK:

Contest Division 1
Contest Division 2
Contest Division 3
Contest Division 4

Setter: Yash Kulkarni
Tester: Manan Grover, Lavish Gupta
Editorialist: Devendra Singh

DIFFICULTY:

1943

PREREQUISITES:

Dynamic programming

PROBLEM:

Kulyash has given you an array A of size N.

He defines the subsequence-number of a non-empty subsequence
S of array A as the number formed by the concatenation of all the elements of the subsequence S.

Find the count of non-empty subsequences of A having their subsequence-numbers divisible by 7. Since the answer can be huge, output it modulo 10^9 + 7.

For example: Consider A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. A subsequence S of A is [2, 5, 7, 10]. The subsequence-number of this subsequence is 25710.

QUICK EXPLANATION:

Let dp[i][j] represent the number of subsequences in the prefix P_i of the array A which leave a remainder j when there subsequence-number is divided by 7.
Let D be the number of digits in the i^{th} element.
Initialize dp[0][0]=1 and rest as 0 (the empty subsequence, this will be subtracted from the answer later).
The dp transition is : for each j from 0 to 6
dp[i][j*10^{D} + A_i] = dp[i-1][j] + dp[i-1][j*10^{D}+A_i]

The answer is dp[N][0]-1

EXPLANATION:

For each value of remainder, rem = 0\: to\: 6, let us calculate the number of subsequences in the prefix P_i (first i elements of the array A) which leave a remainder rem when their subsequence-number is divided by 7 . The subsequences till a particular index i leaving remainder rem include the subsequences calculated for prefix P_{i-1} for the same remainder rem.
Let D be the number of digits in the i^{th} element. Let the number of subsequences in P_{i-1} which leave a remainder R when their subsequence-number is divided by 7 be X, Then the remainder when the subsequence-number formed by combining the i^{th} element with an earlier subsequence from these X subsequences is divided by 7 is (R*10^D+A_i) %7. Hence, we need to add X to the number of subsequences in P_i which leave a remainder (R*10^D+A_i)%7 when their subsequence-number is divided by 7.

This problem has both optimal substructure property and overlapping subproblems. These kind of problems can be solved using dynamic programming.

Let dp[i][j] represent the number of subsequences in the prefix P_i of the array A which leave a remainder j when there subsequence-number is divided by 7.
Initialize dp[0][0]=1 and rest as 0 (the empty subsequence, this will be subtracted from the answer later).
The dp transition is : for each j from 0 to 6
dp[i][j*10^{D} + A_i] = dp[i-1][j] + dp[i-1][j*10^{D}+A_i]

The answer is dp[N][0]-1

TIME COMPLEXITY:

O(N) for each test case.

SOLUTION:

Setter's solution
#include<bits/stdc++.h>
#define ll long long
#define MOD 1000000007
using namespace std;
int main(){
    ll T;
    cin >> T;
    while(T--){
        ll n;
        cin >> n;
        vector<ll>v(n);
        for(ll i=0;i<n;i++)cin >> v[i];
        vector<vector<ll>>dp(n,vector<ll>(7,0));
        vector<ll>mul(n,1);
        for(ll i=0;i<n;i++){
            ll x=v[i];
            while(x){
                mul[i]*=10;
                x/=10;
            }
        }
        dp[0][v[0]%7]=1;
        for(ll i=1;i<n;i++){
            for(ll k=0;k<7;k++)dp[i][k]=dp[i-1][k];
            for(ll k=0;k<7;k++)dp[i][(mul[i]*k+v[i])%7]=(dp[i][(mul[i]*k+v[i])%7]+dp[i-1][k])%MOD;
            dp[i][v[i]%7]=(dp[i][v[i]%7]+1)%MOD;
        }
        cout << dp[n-1][0] << endl;
    }
    return 0;
}
Editorialist's Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(), _obj.end()
#define F first
#define S second
#define pll pair<ll, ll>
#define vll vector<ll>
const int N = 1e5 + 11, mod = 1e9 + 7;
ll max(ll a, ll b) { return ((a > b) ? a : b); }
ll min(ll a, ll b) { return ((a > b) ? b : a); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dig(int n)
{
    int cnt = 0;
    while (n)
    {
        cnt++;
        n /= 10;
    }
    return cnt;
}
void sol(void)
{
    int n;
    cin >> n;
    vll v(n);
    for (int i = 0; i < n; i++)
        cin >> v[i];
    ll dp[n + 1][7], ans = 0;
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for (int i = 0; i < n; i++)
    {
        int digits = dig(v[i]), tenpower = pow(10, digits);
        for (int j = 0; j < 7; j++)
        {
            dp[i + 1][(j * tenpower + v[i]) % 7] = dp[i][j] + dp[i][(j * tenpower + v[i]) % 7];
            dp[i + 1][j] %= mod;
        }
    }
    cout << (dp[n][0] - 1 + 2 * mod) % mod << '\n';
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
    int test = 1;
    cin >> test;
    while (test--)
        sol();
}


3 Likes
lli h(lli arr[],int i,int n,int rem){
    if(i==n){
        return 0;
    }
    // if((((rem*10)%7+arr[i])%7)==0)cout<<rem<<" "<<i<<endl;
    if(dp[rem][i]!=-1){
        return (dp[rem][i])%MOD;
    }
    lli a=h(arr,i+1,n,rem)%MOD;
    int num=woper(10,lennum(arr[i]));
    lli b=(h(arr,i+1,n,(((rem*num)%7+arr[i])%7))%MOD+(((((rem*num)%7+arr[i])%7)==0)?1:0))%MOD;
    dp[rem][i]=(a+b)%MOD;
    return dp[rem][i];
}

one with recursion

2 Likes

Thanks for the recursion part

nice problem @kulyash

1 Like

https://www.codechef.com/viewsolution/65695392
soln using memoisation

Thanks!!

In the transition: dp[i][j*10^{D} + A_i] = dp[i-1][j] + dp[i-1][j*10^{D}+A_i], why is this term(dp[i-1][j*10^{D}+A_i]) added?