1292F - Nora's Toy Boxes - CodeForces Solution


bitmasks combinatorics dp *3500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
 using namespace std;
typedef long long ll;
const int N=65, K=16, mod=1000000007;
 ll n, m, k, u, v, x, y, t, a, b, ans=1, ted;
int A[N], B[N], src[N], vec[N], adj[N];
ll dp[N][1<<K], C[N][N];
 void dfs(int v){
 A[v]=0;
 if (B[v]) vec[m++]=v;
 else src[k++]=v;
 for (int u=1; u<N; u++) if (A[u] && (u%v==0 || v%u==0)) dfs(u);
}
 int main(){
 for (int i=0; i<N; i++){
  C[i][0]=C[i][i]=1;
  for (int j=1; j<i; j++) C[i][j]=(C[i-1][j] + C[i-1][j-1])%mod;
 }
 cin>>n;
 while (n--) cin>>x, A[x]=1;
 for (int i=1; i<N; i++) if (A[i]) for (int j=i+i; j<N; j+=i) B[j]=1;
 for (int i=1; i<N; i++) if (A[i]){
  m=k=0;
  dfs(i);
  if (!m) continue ;
  m--;
  for (int t=0; t<=m; t++) for (int mask=0; mask<(1<<k); mask++) dp[t][mask]=0;
  for (int j=0; j<=m; j++){
   adj[j]=0;
   for (int a=0; a<k; a++) if (vec[j]%src[a]==0) adj[j]|=(1<<a);
   dp[0][adj[j]]++;
  }
  for (int j=0; j<m; j++) for (int mask=1; mask<(1<<k); mask++){
   dp[j][mask]%=mod;
   for (int x=0; x<=m; x++) if (mask&adj[x]) dp[j+1][mask|adj[x]]+=dp[j][mask];
   dp[j+1][mask]-=dp[j][mask]*(j+1);
  }
  ans=ans*dp[m][(1<<k)-1]%mod*C[ted+=m][m]%mod;
 }
 cout<<ans<<"\n";
  return 0;
}
 


Comments

Submit
0 Comments
More Questions

983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III