Hello 2019 LeeRinji

Gennady and a Card Game

#include <bits/stdc++.h>
using namespace std;
char t[9], s[9];
int main()
{
	for (scanf("%s", t); ~scanf("%s", s);)
		if (s[0] == t[0] || s[1] == t[1])
			return printf("YES"), 0;
	printf("NO");
}

Petr and a Combination Lock

#include <bits/stdc++.h>
using namespace std;
int n, a[15];
int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int i = (1 << n) - 1, sum; ~i; --i)
	{
		for (int j = sum = 0; j < n; ++j)
			sum += (i & 1 << j) ? -a[j] : a[j];
		if (sum % 360 == 0)
			return printf("YES"), 0;
	}
	printf("NO");
}

Yuhao and a Parenthesis

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
char s[N], t[N];
int n, cnt[N << 1];
int main()
{
	scanf("%d", &n);
	for (int i = 0, c[2]; i < n; ++i)
	{
		scanf("%s", s);
		for (int i = c[0] = c[1] = 0, siz = 0; s[i]; ++i)
		{
			if (siz && t[siz - 1] == '(' && s[i] == ')')
				--c[0], --siz;
			else
				++c[s[i] == ')'], t[siz++] = s[i];
		}
		if (!c[0] || !c[1])
			++cnt[N - c[0] + c[1]];
	}
	for (int i = n = 0; i < N; ++i)
		n += min(cnt[N - i], cnt[N + i]);
	printf("%d", n + cnt[N] / 2 - cnt[N]);
}

Makoto and a Blackboard

#include <bits/stdc++.h>
#define FI first
#define SE second
using namespace std;
typedef long long ll;
struct Mod
{
	const ll M;
	Mod(ll M) : M(M) {}
	ll add(ll a, ll b) const { return ((a + b) % M + M) % M; }
	ll mul(ll a, ll b) const { return a * b % M; }
	ll pow(ll a, ll b) const
	{
		ll r = 1;
		for (a %= M; b; b >>= 1, a = mul(a, a))
			if (b & 1)
				r = mul(r, a);
		return r;
	}
	ll inv(ll a) const { return pow(a, M - 2); }
} M(1e9 + 7);
ll n, k, ans;
int main()
{
	scanf("%lld%lld", &n, &k);
	vector<pair<ll, ll>> v;
	ans = n;
	for (ll i = 2; i * i <= n; ++i)
		if (ans % i == 0)
			for (v.push_back({i, 0}); ans % i == 0; ans /= i)
				++v.back().SE;
	if (ans > 1)
		v.push_back({ans, 1}), ans = 1;
	for (auto it : v)
	{
		vector<ll> sum(it.SE + 1, 0);
		sum[it.SE] = 1;
		for (ll j = 0; j < k; ++j)
		{
			vector<ll> f(it.SE + 1, 0);
			for (ll i = 1; i <= it.SE; ++i)
				f[0] = M.add(f[0], f[i] = M.mul(sum[i - 1], M.inv(i)));
			sum[0] = M.add(f[0], M.mul(sum[it.SE], M.inv(it.SE + 1)));
			for (ll i = 1; i <= it.SE; ++i)
				sum[i] = M.add(sum[i - 1], -f[i]);
		}
		ll res = sum[0];
		for (ll i = 1, p = 1; i <= it.SE; ++i)
			res = M.add(res, M.mul(p = M.mul(p, it.FI), sum[i]));
		ans = M.mul(ans, res);
	}
	printf("%lld", ans);
}