HackerEarth-Every digit

H
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; }
Code language: PHP (php)

About the author

Sheikh Arman Hossain
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

আবু রিফাত মুহাম্মদ

সখের বশে প্রোগ্রামিং করি। নতুন নতুন জিনিস শিখতে এবং শেখাতে ভালবাসি।

যোগাযোগ করুন

error: Alert: Content is protected !!