Wednesday, 08 December, 2021

Rifat's Blog

Competitive Programming Notebook

single post

  • Home
  • HackerEarth-Every digit
DP, Solve By OJ, Solve By Topic

HackerEarth-Every digit

Problem NameEvery digit
JudgeHackerEarth
Problem Linkhttps://www.hackerearth.com/practice/algorithms/dynamic-programming/bit-masking/practice-problems/algorithm/every-digit/
Algorithms & DSDynamic Programming , BitMasking
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define PB push_back
#define VST(V) sort(V.begin(),V.end())
#define VSTrev(V) sort(V.begin(),V.end(),greater<long long int>())
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll mod=1e9+7;
ll SET(ll NUM,ll POS)
{
    return NUM | (1<<POS);
}
bool isOn(ll NUM,ll POS)
{
    return (bool)(NUM & (1<<POS));
}
ll mem[1010][(1<<10)+10],n;
vector<ll>V;
ll dp(ll i,ll mask)
{
    if(i>=n)
    {
        if(mask==(1<<10)-1)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    if(mem[i][mask]!=-1)
    {
        return mem[i][mask];
    }
    ll ans=0;
    ans+=dp(i+1,mask);
    ans%=mod;
    ll tm_mask=mask|V[i];
    ans+=dp(i+1,tm_mask);
    ans%=mod;
    return mem[i][mask]=ans;
}
int main()
{
    //freopen("1input.txt","r",stdin);
    fast;
    ll tcase=1;
    cin>>tcase;
    for(ll test=1; test<=tcase; test++)
    {
        V.clear();
        cin>>n;
        string s;
        for(ll i=0; i<n; i++)
        {
            cin>>s;
            ll siz=s.size();
            ll mask=0;
            for(ll j=0; j<siz; j++)
            {
                ll val=s[j]-'0';
                mask=SET(mask,val);
            }
            V.PB(mask);
        }
        for(ll i=0; i<=n; i++)
        {
            for(ll j=0; j<=(1<<10)+1; j++)
            {
                mem[i][j]=-1;
            }
        }
        ll ans=dp(0,0);
        cout<<ans<<"\n";
    }
    return 0;
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Created by Abu Rifat Muhammed Al Hasib
2021 © All rights reserved


error: Alert: Content is protected !!