Submission #669673

# Submission time Handle Problem Language Result Execution time Memory
669673 2022-12-07T04:45:09 Z sudheerays123 Beautiful row (IZhO12_beauty) C++17
0 / 100
106 ms 262144 KB
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 20+2 , INF = 1e18 , MOD = 1e9+7;

ll n;
vector<ll> a(N) , fact(N);
ll dp[(1ll<<N)][N];

ll go(ll mask  , ll last){

	if(mask == ((1ll<<n)-1)) return 0;
	if(dp[mask][last] != -1) return dp[mask][last];

	ll ans = 0;

	for(ll i = 0; i < n; i++){
		if(mask&(1ll<<i)) continue;
		if(last != 0 && __builtin_popcountll(last) == __builtin_popcountll(a[i])) ans += fact[n-__builtin_popcount(mask)-1];
		else ans += go(mask|(1ll<<i),i);
	}

	return dp[mask][last] = ans;
}

void solve(){

	cin >> n;
	for(ll i = 0; i < n; i++) cin >> a[i];

	fact[0] = 1;
	for(ll i = 1; i <= n; i++) fact[i] = i*fact[i-1];

	memset(dp,-1,sizeof dp);

	cout << go(0,0);
}

int main(){

	fast;

	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif

	ll tc = 1;
	// cin >> tc;
	while(tc--) solve();

	return 0;
}

Compilation message

beauty.cpp: In function 'int main()':
beauty.cpp:45:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |   freopen("input.txt","r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
beauty.cpp:46:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |   freopen("output.txt","w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 106 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -