結果

問題 No.234 めぐるはめぐる (4)
ユーザー heno239heno239
提出日時 2020-08-19 20:51:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 1,000 ms
コード長 9,183 bytes
コンパイル時間 2,267 ms
コンパイル使用メモリ 148,400 KB
実行使用メモリ 68,936 KB
最終ジャッジ日時 2023-08-02 10:27:19
合計ジャッジ時間 2,822 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
68,936 KB
testcase_01 AC 20 ms
68,936 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:29:20: 警告: integer overflow in expression of type ‘long long int’ results in ‘5054944270305263737’ [-Woverflow]
   29 | const ll INF = mod * mod / 300;
      |                ~~~~^~~~~

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 999999999999989;
const ll INF = mod * mod / 300;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);

ll mod_pow(ll x, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 22;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}

struct uf {
private:
	vector<int> par, ran;
public:
	uf(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

void debug(vector<int> v) {
	rep(i, v.size()) {
		if (i > 0)cout << " ";
		cout << v[i];
	}
	cout << "\n";
}
vector<int> ch[1 << 10];
void init() {
	rep(i, (1 << 10)) {
		vector<int> v;
		rep(j, 10)if (i & (1 << j)) {
			v.push_back(j);
		}
		int len = v.size();
		rep(j, (1 << len)) {
			int s = 0;
			rep(k, len) {
				if (j & (1 << k)) {
					s |= (1 << v[k]);
				}
			}
			ch[i].push_back(s);
		}
	}
}

void solve() {
	init();
	vector<ll>memo(13);
	memo[1] = 1;
	cout << memo[1] << "\n";
	map<pair<vector<int>, int>, ll> dp;
	dp[{ {3}, 0}] = 2;
	for (int n = 2; n <= 12; n++) {
		dp[{ {}, 0 }] = 1;
		map<pair<vector<int>, int>, ll> nex;
		vector<int> ss(2 * n + 1);
		vector<int> to, nexs;
		vector<int> cnt(n + 1);
		vector<bool> exi(n + 1);
		vector<int> v;
		for (pair<pair<vector<int>, int>, ll> p : dp) {
			v = p.first.first;
			int bans = p.first.second;
			ll val = p.second;
			fill(all(exi), false);
			for (int s : v)rep(i, n)if (s & (1 << i))exi[i] = true;
			rep(i, (1 << n)) {
				bool valid = true;
				rep(j, n) {
					if (i & (1 << j))if (bans & (1 << j))valid = false;
				}
				if (!valid)continue;
				uf u(2 * n + 1);
				for (int s : v) {
					int pre = -1;
					rep(j, n) {
						if (s & (1 << j)) {
							if (pre < 0)pre = j;
							u.unite(pre + n + 1, j + n + 1);
						}
					}
				}
				bool bf = false;
				fill(all(cnt), 0);
				rep(j, n) {
					if (exi[j]) {
						if (i & (1 << j)) {
							if (u.same(j + 1, j + n + 1))bf = true;
							u.unite(j + 1, j + n + 1);
							cnt[j + 1]++;
						}
						else {
							if (u.same(j, j + n + 1))bf = true;
							u.unite(j, j + n + 1);
							cnt[j]++;
						}
					}
					else {
						if (i & (1 << j)) {
							if (u.same(j, j + n + 1))bf = true;
							u.unite(j, j + n + 1);
							cnt[j]++;
							if (u.same(j + 1, j + n + 1))bf = true;
							u.unite(j + 1, j + n + 1);
							cnt[j + 1]++;
						}
					}
				}
				fill(all(ss), 0);
				rep(j, n + 1)if (cnt[j] > 0) {
					ss[u.find(j)] |= (1 << j);
				}
				to.clear();
				rep(j, 2 * n + 1)if (ss[j] > 0)to.push_back(ss[j]);

				int bs = 0, cs = 0;
				rep(j, n + 1) {
					if (cnt[j] == 1)cs |= (1 << j);
					else if (cnt[j] == 2)bs |= (1 << j);
				}
				if (to.size() == 1) {
					if (bs == to[0]) {
						/*cout << "from\n";
						debug(p.first.first);
						cout << i << " " << bs << " " << val << " " << bans << "\n";*/

						auto& loc = memo[n];
						loc += val;
						if (loc >= mod)loc -= mod;
					}
				}
				if (!bf) {

					nexs.clear();
					for (int s : to) {
						int ns = s & cs;
						if (ns > 0)nexs.push_back(ns);
					}
					sort(all(nexs));
					if (nexs.size()) {
						/*cout << "hoge\n";
						debug(p.first.first);
						debug(nexs);
						cout << i << " " << bs << " " << val << " " << bans << "\n";*/
						auto& loc = nex[{nexs, bs}];
						loc += val;
						if (loc >= mod)loc -= mod;
					}
				}
			}
		}
		//cout << memo[n] << "\n";
		vector<bool> al(n + 1);
		nex[{ {}, 0}] = 1;
		dp.clear();
		for (pair < pair<vector<int>, int>, ll> p : nex) {
			v = p.first.first;
			int bans = p.first.second;
			ll val = p.second;
			rep(i, (1 << n)) {
				uf u(2 * n + 1);
				fill(all(exi), false);
				for (int s : v) {
					int pre = -1;
					rep(j, n + 1)if (s & (1 << j)) {
						if (pre < 0)pre = j;
						u.unite(pre + n, j + n);
						exi[j] = true;
					}
				}
				bool valid = true;
				rep(j, n) {
					if (i & (1 << j)) {
						if (bans & (1 << j))valid = false;
						if (bans & (1 << j + 1))valid = false;
					}
				}
				rep1(j, n - 1) {
					if (exi[j]) {
						if (i & (1 << j - 1))if (i & (1 << j))valid = false;
					}
				}

				if (!valid)continue;
				fill(all(al), false);
				rep(j, n)if (i & (1 << j)) {
					al[j] = al[j + 1] = true;
				}

				rep(j, n + 1)if (exi[j])al[j] = true;
				bool bf = false;
				rep(j, n)if (i & (1 << j)) {
					if (u.same(j, j + n))bf = true;
					u.unite(j, j + n);
					if (u.same(j, j + 1 + n))bf = true;
					u.unite(j, j + 1 + n);
				}
				fill(all(ss), 0);

				int bs = bans;
				rep1(j, n - 1) {
					if (i & (1 << j - 1))if (i & (1 << j))bs |= (1 << j);
				}
				rep(j, n)if (i & (1 << j)) {
					if (exi[j])bs |= (1 << j);
					if (exi[j + 1])bs |= (1 << j + 1);
				}
				to.clear();
				rep(j, n + 1) {
					if (al[j])if (!(bs & (1 << j))) {
						ss[u.find(j + n)] |= (1 << j);
					}
				}
				for (int s : ss)if (s > 0)to.push_back(s);

				/*cout << "from\n";
				debug(p.first.first);
				debug(to);
				cout << i << " " << bs << "\n";*/
				if (to.empty()) {
					int cnt = 0;
					int chk = -1;
					rep(j, n + 1)if (al[j]) {
						int p = u.find(j + n);
						if (chk != p) {
							chk = p; cnt++;
						}
					}
					if (cnt == 1) {
						/*cout << "from\n";
						debug(p.first.first);
						cout << i << " " << bs << " " << val << " " << bans << "\n";*/
						memo[n] += val;
						if (memo[n] >= mod)memo[n] -= mod;
					}
				}
				if (!bf) {
					/*cout << "from\n";
					debug(p.first.first);
					debug(to);
					cout << i << " "<<bans<<" "<<bs<<"\n";*/
					if (!to.empty()) {
						sort(all(to));
						auto& loc = dp[{to, bs}];
						loc += val;
						if (loc >= mod)loc -= mod;
					}
				}
			}
		}
		/*for (pair<pair<vector<int>, int>, ll> p : nex) {
			cout << "start\n";
			debug(p.first.first); cout << p.first.second << " " << p.second << "\n";
		}*/
		cout << memo[n] << "\n";
	}
	ll sum = 0;
	cout << "{";
	rep1(i, 12) {
		sum += memo[i];
		while (sum >= mod)sum -= mod;
		if (i > 1)cout << ",";
		cout << sum;
	}
	cout << "}\n";
}
vector<ll> ans = { 1,11,110,2402,128967,16767653,5436906668,4406952731948,8819634719356421,6435859857508631015,7943455629954469858 };
void solve1() {
	int n; cin >> n;
	if (n == 11) {
		cout << "522235268182347360718818\n";
	}
	else if (n == 12) {
		cout << "15436131339319739257518081878\n";
	}
	else {
		cout << ans[n - 1] << "\n";
	}
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//expr();
	//int t; cin >> t; rep(i, t)
	//while (cin >> n >> m>>s1>>s2>>t, n)
	solve1();
	return 0;
}
0