Submission #466261

# Submission time Handle Problem Language Result Execution time Memory
466261 2021-08-18T12:23:15 Z the_visitor2341 Beautiful row (IZhO12_beauty) C++14
100 / 100
2686 ms 164568 KB
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define repp(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend() 
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define eb emplace_back
#define pb push_back
#define ins insert 
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back 
#define pf push_front
#define rtn return
#define trav(it,v) for(auto&it:(v))
#define rsz resize
#define tcT template<class T
#define tcTU tcT,class U
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef vector<pii> vpii;

tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); 

tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b) - begin(a)); }

const int MOD = 1e9 + 7;
const ll INF = 1e18;
const db PI = acos((db)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};

constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { // assert(x >= 0);
	return x == 0 ? 0 : 31-__builtin_clz(x); }
constexpr int p2(int x) { return 1<<x; }
constexpr int msk2(int x) { return p2(x)-1; }

tcT> bool ckmin(T& a, const T& b) {
	return b < a ? a = b, 1 : 0; }
tcT> bool ckmax(T& a, const T& b) {
	return b > a ? a = b, 1 : 0; }

ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); }
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); }

tcTU> T fstTrue(T lo ,T hi, U f) {
	hi++ ; assert(lo <= hi);
	while(lo < hi) {
		T mid = lo + (hi - lo)/2;
		f(mid) ? hi = mid : lo = mid+1;
	}
	return lo;
}
tcTU> T lstTrue(T lo, T hi, U f) {
	lo--; assert(lo <= hi);
	while(lo < hi) {
		T mid = lo + (hi-lo+1)/2;
		f(mid) ? lo = mid : hi = mid-1;
	}
	return lo;
}
tcT> void remDup(vector<T>& v) {
	sort(all(v)); v.erase(unique(all(v)),end(v)); }

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

void setPrec() { cout << fixed << setprecision(16); }

void setIO(string name = "") {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
	setPrec();
	if(sz(name)) {
		freopen((name + ".in").c_str(),"r",stdin);
		freopen((name + ".out").c_str(),"w",stdout);
	}
}

const int MASK = 1<<20, mxN = 20;
ll DP[MASK][mxN],A[mxN];

int cnt1(ll x) {
	int ans = 0;
	while(x>0) {
		ans += x%3 == 1;
		x/=3;
	}
	return ans;
}

bool ok(ll a, ll b) {
	if(__builtin_popcountll(a)==__builtin_popcountll(b)) return true;
	if(cnt1(a) == cnt1(b)) return true;
	return false;
}

void solve() {
	int N;
	cin>>N;
	F0R(i,N) cin>>A[i],DP[1<<i][i]=1;
	FOR(msk,1,1<<N) {
		F0R(i,N) {
			if(msk>>i&1) {
				F0R(j,N) {
					if(!(msk>>j&1)) {
						if(ok(A[i],A[j])) {
							DP[msk^(1<<j)][j]+=DP[msk][i];
						}
					}
				}
			}
		}
	}
	ll ans = 0;
	F0R(i,N) {
		ans += DP[(1<<N)-1][i];
	}
	cout << ans << "\n";
}

//#define TC

int main() {
	setIO();

	int t=1;
#ifdef TC
	cin>>t;
#endif
	while(t-- > 0) {
		solve();
	}

	return 0;
}

Compilation message

beauty.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    2 | #pragma GCC optimization ("O3")
      | 
beauty.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      | 
beauty.cpp: In function 'void setIO(std::string)':
beauty.cpp:124:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |   freopen((name + ".in").c_str(),"r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
beauty.cpp:125:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  125 |   freopen((name + ".out").c_str(),"w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 456 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 15 ms 2892 KB Output is correct
12 Correct 19 ms 2892 KB Output is correct
13 Correct 83 ms 10564 KB Output is correct
14 Correct 496 ms 41396 KB Output is correct
15 Correct 173 ms 41344 KB Output is correct
16 Correct 627 ms 41340 KB Output is correct
17 Correct 597 ms 41524 KB Output is correct
18 Correct 786 ms 41340 KB Output is correct
19 Correct 2686 ms 164452 KB Output is correct
20 Correct 821 ms 164568 KB Output is correct