solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 107;
int n, m;
char s[N];
int main() {
long long ans = 0;
scanf("%d %d", &n, &m);
scanf("%s", s + 1);
m--;
ans += n * m;
long long p = 0;
for (int i = 2; i <= n; i++) {
if (s[i] != s[i - 1]) p++;
ans += p * m;
}
p = 0;
for (int i = n - 1; i >= 1; i--) {
if (s[i] != s[i + 1]) p++;
ans += p * m;
}
int r = 1;
for (int l = 1; l < n; l++) {
if (s[l] == s[l + 1]) continue;
r = max(r, l + 2);
while (r <= n && s[r] == s[r - 2]) r++;
ans -= r - l - 1;
}
printf("%I64d\n", ans);
}
| 9 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
static const int _ = []() {
ios::sync_with_stdio(false);
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}();
template <typename T, typename U>
struct par {
T x;
U y;
par() : x(-1), y(-1) {}
par(const T& x, const U& y) : x(x), y(y) {}
bool operator<(const par& other) const {
if (x == other.x) return y < other.y;
return x < other.x;
}
bool operator>(const par& other) const {
if (x == other.x) return y > other.y;
return x > other.x;
}
bool operator==(const par& other) const {
return x == other.x && y == other.y;
}
par operator-(const par& b) const { return {x - b.x, y - b.y}; }
};
template <class T>
inline vector<T> rv(long long n) {
vector<T> v(n);
for (long long i = 0; i < (n); i++) cin >> v[i];
return v;
}
template <class T>
inline void pv(const vector<T>& v) {
for (long long i = 0; i < (v.size()); i++) cout << v[i] << " ";
cout << endl;
}
template <class T, class U>
inline void amax(T& a, const U& b) {
if (a < b) a = b;
}
template <class T, class U>
inline void amin(T& a, const U& b) {
if (a > b) a = b;
}
template <class T, class U>
inline T min(T& a, const U& b) {
return a > b ? b : a;
}
template <class T, class U>
inline T max(T& a, const U& b) {
return a < b ? b : a;
}
const long long MOD = 1e9 + 7;
const long long MOD1 = 998244353;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
long long GCD(long long a, long long b) {
if (a < b) swap(a, b);
while (b) {
long long r = a % b;
a = b;
b = r;
}
return a;
}
int main(int argc, char** argv) {
long long a, b, c;
cin >> a >> b >> c;
long long x = 2 * c;
if (a > b) {
x += 2 * b + 1;
} else if (a < b) {
x += 2 * a + 1;
} else {
x += 2 * a;
}
cout << x << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#define mod 1000000007
//#define mod 998244353
#define INF LLONG_MAX
#define NINF LLONG_MIN
#define ll long long int
#define pr(a) pair<a, a>
#define ps pair<int, string>
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define f(i, a, b) for (int i = a; i <= b; i++)
#define r(i, a, b) for (int i = a; i >= b; i--)
#define ms(a, b) memset(a, b, sizeof(a))
#define line cout << '\n'
#define all(v) v.begin(), v.end()
#define rev(s) reverse(all(s))
using namespace std;
void Precompute()
{
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Precompute();
int tt = 1;
cin >> tt;
f(t, 1, tt)
{
string a, b;
cin >> a >> b;
int m = a.size(), n = b.size();
int dp[m + 1][n + 1], mx = 0;
ms(dp, 0);
f(i, 1, m)
{
f(j, 1, n)
{
if(a[i - 1] == b[j - 1])
{
dp[i][j] = dp[i - 1][j - 1] + 1;
}
else
{
dp[i][j] = 0;
}
mx = max(mx, dp[i][j]);
}
}
int ans = m + n - 2 * mx;
cout << ans;
line;
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
int d[100007];
int l[100007];
int num[207];
int mark[100007];
int sum[100007];
int total[100007];
int n;
struct Node {
int d;
int l;
bool operator<(const Node& a) const { return l < a.l; }
} b[100007];
int main() {
while (~scanf("%d", &n)) {
memset(num, 0, sizeof(num));
memset(sum, 0, sizeof(sum));
memset(total, 0, sizeof(total));
memset(mark, 0, sizeof(mark));
int low = 100007, up = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &l[i]);
low = min(low, l[i]);
up = max(up, l[i]);
}
for (int i = 0; i < n; i++) scanf("%d", &d[i]);
for (int i = 0; i < n; i++) {
mark[l[i]]++;
total[l[i]] += d[i];
}
for (int i = up; i >= low; i--) {
sum[i] = sum[i + 1] + mark[i];
total[i] += total[i + 1];
}
for (int i = 0; i < n; i++) {
b[i].d = d[i];
b[i].l = l[i];
}
sort(b, b + n);
int cnt = 0;
int ans = 1e9;
int temp;
for (int i = low; i <= up; i++) {
if (!mark[i]) continue;
while (cnt < n && b[cnt].l < i) num[b[cnt++].d]++;
temp = total[i + 1];
int p = n - sum[i];
for (int j = 1; j <= 200; j++) {
if (p < mark[i]) break;
if (p - mark[i] + 1 < num[j]) {
temp += (p - mark[i] + 1) * j;
p -= mark[i] - 1;
break;
}
temp += num[j] * j;
p -= num[j];
}
ans = min(ans, temp);
}
printf("%d\n", ans);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
struct edge {
int from;
int to;
} a[300000 + 5];
vector<int> E[300000 + 5];
void add_edge(int u, int v) {
E[u].push_back(v);
E[v].push_back(u);
}
int ans[300000 + 5];
int mk[300000 + 5];
int cnt[300000 + 5];
void failed() {
printf("-1\n");
exit(0);
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &a[i].from, &a[i].to);
if (a[i].from == 1) mk[a[i].to] = 1;
if (a[i].to == 1) mk[a[i].from] = 1;
add_edge(a[i].from, a[i].to);
}
for (int i = 1; i <= m; i++)
if (!mk[i]) ans[i] = 1;
for (int i = 1; i <= m; i++) mk[i] = 0;
if (E[1].size() == 0) failed();
for (int i = 1; i <= m; i++) {
if (a[i].from == E[1][0]) mk[a[i].to] = 1;
if (a[i].to == E[1][0]) mk[a[i].from] = 1;
}
for (int i = 1; i <= n; i++) {
if (!mk[i]) {
if (ans[i] != 1)
ans[i] = 2;
else
failed();
}
}
for (int i = 1; i <= m; i++) {
if (ans[a[i].from] == ans[a[i].to]) failed();
}
for (int i = 1; i <= n; i++) {
if (!ans[i]) ans[i] = 3;
cnt[ans[i]]++;
}
if (cnt[1] == 0 || cnt[2] == 0 || cnt[3] == 0) failed();
if (n * (n - 1) / 2 - cnt[1] * (cnt[1] - 1) / 2 - cnt[2] * (cnt[2] - 1) / 2 -
cnt[3] * (cnt[3] - 1) / 2 !=
m)
failed();
for (int i = 1; i <= n; i++) {
printf("%d ", ans[i]);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const int64_t LINF = 4611686018427387903;
const int MOD = 1e9 + 7;
void solve(int64_t x, vector<int64_t> &arr) {
for (int64_t i = 1; i * i <= x; i++)
if (x % i == 0) arr.push_back(i), arr.push_back(x / i);
sort(arr.begin(), arr.end());
arr.erase(unique(arr.begin(), arr.end()), arr.end());
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
int64_t a, b, ans = LINF;
vector<int64_t> fa, fb, fab;
cin >> a >> b;
solve(a, fa);
solve(b, fb);
solve(a + b, fab);
for (int64_t &x : fab) {
int64_t an = (a + b) / x;
int64_t w = a / *(--upper_bound(fa.begin(), fa.end(), x));
int64_t q = b / *(--upper_bound(fb.begin(), fb.end(), x));
if (w <= an || q <= an) ans = min(ans, 2 * (an + x));
}
cout << ans << '\n';
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void lose() {
cout << "cslnb";
exit(0);
}
void win() {
cout << "sjfnb";
exit(0);
}
bool solve(vector<int> &v) {
if (v.size() == 1) {
return v[0] & 1;
}
sort(v.begin(), v.end());
long long value = 0;
long long prev = -1;
long long rest = v.size() + 1;
for (long long x : v) {
rest--;
if (x == prev + 1) {
prev = x;
continue;
}
value += (x - prev - 1) * rest;
value %= 2;
prev = x;
}
return value & 1;
}
int main() {
int n;
cin >> n;
vector<int> v(n);
map<int, int> cnt;
for (int i = 0; i < n; i++) {
cin >> v[i];
cnt[v[i]]++;
}
if (n == 1) {
if (v[0] & 1) {
win();
}
lose();
}
int pos = -1;
for (auto x : cnt) {
if (x.second > 2) {
lose();
}
if (x.second == 2) {
if (pos != -1 || x.first == 0 || cnt[x.first - 1] != 0) {
lose();
}
pos = x.first;
}
}
if (pos == -1) {
if (solve(v)) {
win();
}
lose();
} else {
for (int i = 0; i < n; i++) {
if (v[i] == pos) {
v[i]--;
break;
}
}
if (solve(v)) {
lose();
}
win();
}
}
| 5 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
int array[n];
int i, j, x;
for (i = 0; i < n; i++) scanf("%d", &array[i]);
for (i = 0; i < n; i++) {
x = array[i];
j = i - 1;
while (j >= 0 && array[j] > x) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = x;
}
int count1 = 0, count2 = 0, t = 1;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j]) {
if (array[i] != 0) {
count1++;
count2++;
if (count2 > 1) {
t = 0;
count1 = -1;
break;
}
}
} else
break;
}
count2 = 0;
if (t == 0) break;
}
printf("%d\n", count1);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T abs(T a) {
return a < 0 ? -a : a;
}
template <typename T>
T sqr(T a) {
return a * a;
}
const int INF = (int)1e9;
const long double EPS = 1e-9;
const long double PI = 3.1415926535897932384626433832795;
const int N = 200;
inline void makeLine(const pair<long double, long double>& a,
const pair<long double, long double>& b, long double& A,
long double& B, long double& C) {
A = b.second - a.second;
B = a.first - b.first;
C = -A * a.first - B * a.second;
}
inline long double det(const long double& a, const long double& b,
const long double& c, const long double& d) {
return a * d - b * c;
}
inline long double mid(const pair<long double, long double>& a,
const pair<long double, long double>& b,
const pair<long double, long double>& c) {
return min(a.first, b.first) <= c.first + EPS &&
c.first <= max(a.first, b.first) + EPS &&
min(a.second, b.second) <= c.second + EPS &&
c.second <= max(a.second, b.second) + EPS;
}
pair<long double, long double> operator-(
const pair<long double, long double>& a,
const pair<long double, long double>& b) {
return pair<long double, long double>(a.first - b.first, a.second - b.second);
}
inline int vmulS(const pair<long double, long double>& a,
const pair<long double, long double>& b) {
long double res = a.first * b.second - a.second * b.first;
if (abs(res) < EPS) return 0;
return (res < 0) ? -1 : 1;
}
inline bool intersect(const pair<long double, long double>& a1,
const pair<long double, long double>& b1,
const pair<long double, long double>& a2,
const pair<long double, long double>& b2) {
if (max(a1.first, b1.first) + EPS < min(a2.first, b2.first) ||
max(a1.second, b1.second) + EPS < min(a2.second, b2.second) ||
max(a2.first, b2.first) + EPS < min(a1.first, b1.first) ||
max(a2.second, b2.second) + EPS < min(a1.second, b1.second))
return false;
return vmulS(b2 - a1, b1 - a1) * vmulS(a2 - a1, b1 - a1) <= 0 &&
vmulS(b1 - a2, b2 - a2) * vmulS(a1 - a2, b2 - a2) <= 0;
}
int n, hlf, hrg;
int t[N], lf[N], rg[N], v[N];
bool used[N];
vector<pair<pair<long double, long double>, int> > fns;
int main() {
cin >> hlf >> hrg >> n;
for (int i = 0; i < int(n); ++i) {
cin >> v[i];
char tp;
scanf(" %c", &tp);
if (tp == 'T') t[i] = 1;
cin >> lf[i] >> rg[i];
}
{
pair<long double, long double> cnf(100000, hrg);
int cnt = 0;
for (int lvl = 0; lvl >= -100; --lvl) {
int ln = lvl * 100;
cnf.second += 2 * (ln - cnf.second);
cnt++;
fns.push_back(make_pair(cnf, cnt));
}
}
{
pair<long double, long double> cnf(100000, hrg);
int cnt = 0;
for (int lvl = 1; lvl <= 100; ++lvl) {
int ln = lvl * 100;
cnf.second += 2 * (ln - cnf.second);
cnt++;
fns.push_back(make_pair(cnf, cnt));
}
}
int ans = 0;
for (int j = 0; j < int(int((fns).size())); ++j) {
pair<long double, long double> st(0, hlf);
pair<long double, long double> fn = fns[j].first;
int cnt = 0;
int cur = 0;
bool good = true;
memset(used, 0, sizeof(used));
for (int lvl = -101; lvl <= 101; ++lvl) {
for (int i = 0; i < int(n); ++i) {
if ((lvl & 1) == t[i]) {
pair<long double, long double> a(lf[i], 100 * lvl);
pair<long double, long double> b(rg[i], 100 * lvl);
if (intersect(a, b, st, fn)) {
cnt++;
cur += v[i];
if (used[i]) {
good = false;
break;
}
used[i] = true;
}
}
}
if (!good) break;
}
if (cnt != fns[j].second || !good) continue;
ans = max(ans, cur);
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, p = 1;
cin >> n;
for (int i = n; i - 1; i >>= 1, p <<= 1)
for (int j = 0; j < (i + 1) / 2; j++) cout << p << " ";
cout << n - n % (p >> (n != 1));
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string line;
vector<string> mine;
int m, n;
void creat() {
int i;
getline(cin, line);
line.clear();
for (i = 1; i <= m; i++) {
line.clear();
getline(cin, line);
mine.push_back(line);
}
}
void check() {
int i, j, x, y, cnt;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
for (x = -1, cnt = 0; x <= 1; x++) {
for (y = -1; y <= 1; y++) {
if (x == 0 && y == 0) continue;
if (-1 < i + x && i + x < m && -1 < j + y && j + y < n) {
if (mine[i + x][j + y] == '*') cnt++;
} else
continue;
}
}
if (mine[i][j] >= 49 && mine[i][j] <= 57) {
if (cnt == mine[i][j] - 48)
continue;
else {
cout << "No";
return;
}
}
if (mine[i][j] == '.') {
if (cnt == 0) {
continue;
} else {
cout << "No";
return;
}
}
if (mine[i][j] == '*') {
continue;
}
}
}
if (i == m && j == n) {
cout << "Yes";
}
}
int main() {
cin >> m >> n;
creat();
check();
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int a[2010];
int main() {
int T = read();
while (T--) {
int ji = 0, ou = 0;
int n = read();
for (int i = 1; i <= n; i++) {
a[i] = read();
if (a[i] & 1)
ji++;
else
ou++;
}
if (ou == n || (ji == n && ji % 2 == 0))
printf("NO\n");
else if (ji == n && (ji & 1))
printf("YES\n");
else
printf("YES\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
while (k--) {
if (n % 10 == 0)
n /= 10;
else
n -= 1;
}
cout << n << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << " ";
err(++it, args...);
}
template <typename T>
inline void read(T& x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) f = (ch == '-') ? -1 : 1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
x *= f;
}
template <typename T, typename... Args>
inline void read(T& x, Args&... args) {
read(x), read(args...);
}
constexpr int MAXN = 3e5 + 5, MOD = 1e9 + 7;
template <int MAXN, int LOG>
struct Trie {
struct TrieNode {
TrieNode() { son[0] = son[1] = cnt = 0; }
int son[2], cnt;
} trie[MAXN * LOG];
int root = 0, tot = 0, Xor = 0;
void insert(int x) {
if (find(x)) return;
int cur = root;
for (int i = LOG - 1; ~i; i--) {
int tmp = (x >> i) & 1;
if (!trie[cur].son[tmp]) trie[cur].son[tmp] = ++tot;
cur = trie[cur].son[tmp];
trie[cur].cnt++;
}
}
bool find(int x) {
int cur = root;
for (int i = LOG - 1; ~i; i--) {
int tmp = (x >> i) & 1;
if (!trie[cur].son[tmp]) return false;
cur = trie[cur].son[tmp];
}
return true;
}
void updateAll(int x) { Xor ^= x; }
int mex() {
int cur = root, res = 0;
for (int i = LOG - 1; ~i; i--) {
int tmp = (Xor >> i) & 1;
if (!trie[cur].son[tmp]) return res;
if (trie[trie[cur].son[tmp]].cnt == (1 << i)) tmp ^= 1, res += 1 << i;
if (!trie[cur].son[tmp]) return res;
cur = trie[cur].son[tmp];
}
return res;
}
};
Trie<MAXN, 20> trie;
int n, m;
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
cin >> n >> m;
for (int i = 1, tmp; i <= n; i++) {
cin >> tmp;
trie.insert(tmp);
}
for (int i = 1, x; i <= m; i++) {
cin >> x;
trie.updateAll(x);
cout << trie.mex() << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
double l, r = 1e14, mid;
int n, p, a[100001], b[100001];
bool can(double x) {
double sum = 0, num;
for (int i = 0; i < n; i++) {
num = x * a[i] - b[i];
if (num >= 0) sum += num / p;
}
return sum <= x;
}
int main() {
cin >> n >> p;
for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
if (can(r + 1)) return cout << -1, 0;
for (int i = 0; i < 1000; i++) {
mid = (l + r) / 2;
if (can(mid))
l = mid;
else
r = mid;
}
cout << fixed << setprecision(10) << l;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
if (n == 1 || m == 1) {
cout << "YES" << endl;
} else if (n == 2 && m == 2) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
int from[10];
int to[10];
double coef1 = 1.0;
double coef2 = 1.0;
bool isBetween(int k, int mmin, int mmax) { return k >= mmin && k <= mmax; }
long long dp[10][11000][2][2][2];
long long count(int pos, int k, bool ensured, bool hask, bool hasbigger) {
if (pos == n) {
return ensured && hask;
}
if (dp[pos][k][ensured][hask][hasbigger] != -1) {
return dp[pos][k][ensured][hask][hasbigger];
}
long long ret = 0;
ret += (k < to[pos]) * (to[pos] - max(k + 1, from[pos]) + 1) * (!hasbigger) *
count(pos + 1, k, true, hask, true);
ret += isBetween(k, from[pos], to[pos]) *
count(pos + 1, k, ensured || hask, true, hasbigger);
ret += (from[pos] < k) * (min(to[pos], k - 1) - from[pos] + 1) *
count(pos + 1, k, ensured, hask, hasbigger);
return dp[pos][k][ensured][hask][hasbigger] = ret;
}
void solve() {
memset(dp, -1, sizeof(dp));
double ret = 0;
int i = 0;
for (i = 1; i <= 10000; i++) {
ret += ((double)i) * count(0, i, false, false, false) / coef2;
}
printf("%.9lf\n", ret);
}
int main() {
cin >> n;
int i;
for (i = 0; i < n; i++) {
cin >> from[i] >> to[i];
coef1 *= 1.0 / (to[i] - from[i] + 1);
coef2 *= (to[i] - from[i] + 1);
}
solve();
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
map<pair<long long int, long long int>, long long int> u;
vector<pair<long long int, long long int> > v;
void pp(long long int n) {
map<long long int, long long int> vv;
long long int i, j;
for (i = 2; i <= sqrt(n); i++) {
if (vv[i] == 0) {
for (j = i * i; j <= n; j += i) {
vv[j] = 1;
}
}
}
vv[1] = 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m, i, j, k, p, q, o, l, s, t;
cin >> n >> m;
for (i = 0; i < n; i++) {
cin >> p >> q;
u[{p, q}]++;
v.push_back({p, q});
}
sort(v.begin(), v.end(), greater<pair<long long int, long long int> >());
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (v[j].first == v[j + 1].first && v[j].second > v[j + 1].second) {
swap(v[j], v[j + 1]);
}
}
}
cout << u[v[m - 1]] << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
cout << 3 * (n + 1) + 1 << '\n';
cout << 0 << " " << 0 << '\n';
for (int i = 1; i <= n + 1; ++i) {
cout << i - 1 << " " << i << '\n';
cout << i << " " << i - 1 << '\n';
cout << i << " " << i << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tcs = 1;
while (tcs-- > 0) solve();
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int exp(long long int t, long long int x) {
if (x == 0) return 1;
if (x == 1) return t;
if (x % 2 == 1) return (t * exp((t * t) % 1000000007, x / 2)) % 1000000007;
if (x % 2 == 0) return exp((t * t) % 1000000007, x / 2);
}
long long int gcd(long long int x, long long int y) {
return x % y == 0 ? y : gcd(y, x % y);
}
long long int lcm(long long int x, long long int y) {
return x * (y / gcd(x, y));
}
bool isprime(long long int x) {
for (long long int i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
void multiply(int n, long long int A[101][101], long long int B[101][101],
long long int C[101][101]) {
long long int sm = 0;
for (int(i) = (0); (i) <= (n - 1); (i)++)
for (int(j) = (0); (j) <= (n - 1); (j)++) {
sm = 0;
for (int(k) = (0); (k) <= (n - 1); (k)++) {
sm += A[i][k] * B[k][j];
sm %= 1000000007;
}
C[i][j] = sm;
}
}
void copy(int n, long long int A[101][101], long long int C[101][101]) {
for (int(i) = (0); (i) <= (n - 1); (i)++)
for (int(j) = (0); (j) <= (n - 1); (j)++) C[i][j] = A[i][j];
}
void getIdentity(int n, long long int C[101][101]) {
for (int(i) = (0); (i) <= (n - 1); (i)++)
for (int(j) = (0); (j) <= (n - 1); (j)++)
if (i == j)
C[i][j] = 1;
else
C[i][j] = 0;
}
void display(int n, long long int A[101][101]) {
for (int(i) = (0); (i) <= (n - 1); (i)++) {
for (int(j) = (0); (j) <= (n - 1); (j)++) cout << A[i][j] << ' ';
cout << endl;
}
}
void power(int n, int k, long long int A[101][101], long long int C[101][101]) {
long long int temp[101][101];
copy(n, A, temp);
getIdentity(n, C);
for (int(i) = (0); (i) <= (30); (i)++) {
if (k & (1 << i)) {
multiply(n, C, temp, A);
copy(n, A, C);
}
multiply(n, temp, temp, A);
copy(n, A, temp);
}
}
int main() {
long long int n, b, k, x, num, newn;
scanf("%lld %lld %lld %lld", &n, &b, &k, &x);
long long int divs[101][101];
memset(divs, 0, sizeof divs);
for (int(i) = (0); (i) <= (n - 1); (i)++) {
scanf("%lld", &num);
for (int(j) = (0); (j) <= (x - 1); (j)++) {
newn = j * 10 + num;
newn = newn % x;
++divs[j][newn];
}
}
long long int C[101][101];
power(x, b, divs, C);
printf("%lld\n", C[0][k]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << min((a - b), (c + 1)) << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template <class A, class B>
bool smin(A &x, B &&y) {
if (y < x) {
x = y;
return true;
}
return false;
}
template <class A, class B>
bool smax(A &x, B &&y) {
if (x < y) {
x = y;
return true;
}
return false;
}
struct Edmonds {
int n, T{};
vector<vector<int>> graph;
vector<int> pa, p, used, base;
vector<int> toJoin;
explicit Edmonds(int n) : n(n), graph(n), pa(n, -1), p(n), used(n), base(n) {}
void add_edge(int v, int u) {
graph[v].push_back(u);
graph[u].push_back(v);
}
int getBase(int i) { return base[i] == i ? i : base[i] = getBase(base[i]); }
void mark_path(int v, int x, int b, vector<int> &path) {
for (; getBase(v) != b; v = p[x]) {
p[v] = x;
x = pa[v];
toJoin.push_back(v);
toJoin.push_back(x);
if (!used[x]) {
used[x] = ++T;
path.push_back(x);
}
}
}
bool go(int v) {
for (int x : graph[v]) {
int b;
int bv = getBase(v);
int bx = getBase(x);
if (bv == bx) {
continue;
}
if (used[x]) {
vector<int> path;
toJoin.clear();
if (used[bx] < used[bv]) {
mark_path(v, x, b = bx, path);
} else {
mark_path(x, v, b = bv, path);
}
for (int z : toJoin) {
base[getBase(z)] = b;
}
for (int z : path) {
if (go(z)) {
return true;
}
}
} else if (p[x] < 0) {
p[x] = v;
if (pa[x] < 0) {
for (int y; x >= 0; x = v) {
y = p[x];
v = pa[y];
pa[x] = y;
pa[y] = x;
}
return true;
}
if (!used[pa[x]]) {
used[pa[x]] = ++T;
if (go(pa[x])) {
return true;
}
}
}
}
return false;
}
void init_dfs() {
used.assign(n, 0);
p.assign(n, -1);
iota(base.begin(), base.end(), 0);
}
bool dfs(int root) {
used[root] = ++T;
return go(root);
}
int matching() {
int ans = 0;
init_dfs();
for (int i = 0; i < n; ++i) {
if (pa[i] < 0 && dfs(i)) {
ans++;
init_dfs();
}
}
return ans;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (auto &x : a) cin >> x;
vector<bool> have(n);
for (int i = 0; i + 1 < n; ++i) {
if (a[i] > 0 && a[i + 1] > 0 && a[i] == a[i + 1]) {
have[a[i] - 1] = true;
}
}
vector<pair<int, int>> gaps;
for (int i = 0; i < n; ++i) {
if (a[i] != 0) {
continue;
}
int j = i;
while (j < n && a[j] == 0) {
++j;
}
gaps.emplace_back(i - 1, j);
i = j;
}
vector<int> vid(n, -1);
int id = 0;
for (int i = 0; i < n; ++i) {
if (!have[i]) {
vid[i] = id++;
}
}
int ngap = int(gaps.size());
int nvs = id + ngap + 1;
Edmonds graph(nvs);
auto addEdge = [&](int u, int v) {
assert(0 <= u && u < nvs && 0 <= v && v < nvs && u != v);
graph.add_edge(u, v);
};
int cid = id;
for (auto [l, r] : gaps) {
int len = r - l - 1;
if (len % 2 == 0) {
if (l >= 0 && r < n) {
int x = vid[a[l] - 1];
int y = vid[a[r] - 1];
if (x >= 0 && y >= 0 && x != y) {
addEdge(x, y);
}
}
} else {
if (l >= 0) {
int x = vid[a[l] - 1];
if (x >= 0) {
addEdge(cid, x);
}
}
if (r < n) {
int y = vid[a[r] - 1];
if (y >= 0) {
addEdge(cid, y);
}
}
}
cid++;
}
graph.matching();
cid = id;
for (auto [l, r] : gaps) {
int len = r - l - 1;
if (len % 2 == 0) {
if (l >= 0 && r < n) {
int x = vid[a[l] - 1];
int y = vid[a[r] - 1];
if (x >= 0 && y >= 0 && x != y) {
if (!have[a[l] - 1] && !have[a[r] - 1]) {
int mt = graph.pa[x];
if (mt == y) {
a[l + 1] = a[l];
a[r - 1] = a[r];
have[a[l] - 1] = true;
have[a[r] - 1] = true;
}
}
}
}
} else {
if (l >= 0) {
int x = vid[a[l] - 1];
if (x >= 0) {
int mt = graph.pa[cid];
if (mt == x && !have[a[l] - 1]) {
a[l + 1] = a[l];
have[a[l] - 1] = true;
}
}
}
if (r < n) {
int y = vid[a[r] - 1];
if (y >= 0) {
int mt = graph.pa[cid];
if (mt == y && !have[a[r] - 1]) {
a[r - 1] = a[r];
have[a[r] - 1] = true;
}
}
}
}
cid++;
}
int ptr = 0;
for (int i = 0; i < n; ++i) {
if (a[i] != 0) {
continue;
}
int j = i;
while (j < n && a[j] == 0) {
++j;
}
int len = j - i;
int fr = i;
if (len % 2 == 1) {
a[fr++] = 1;
}
for (int cur = fr; cur < j; cur += 2) {
while (ptr < n && have[ptr]) {
++ptr;
}
if (ptr == n) {
a[cur] = a[cur + 1] = 1;
} else {
a[cur] = a[cur + 1] = ptr + 1;
have[ptr] = true;
}
}
i = j;
}
for (auto x : a) cout << x << ' ';
cout << '\n';
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e9 + 1;
int t;
void solve() {
long long n, ch = 1;
long long ans = 0;
cin >> n;
ans += n * (n + 1) / 2;
while (ch <= n) {
ans -= 2 * ch;
ch *= 2;
}
cout << ans << "\n";
}
int main() {
cin >> t;
while (t--) solve();
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, ans, k, p, l;
cin >> n >> k;
p = k;
long long int a[n + 2];
for (i = 0; i < n; i++) {
cin >> a[i];
}
k = 2 * k;
k = sqrt(k);
ans = (k * (k + 1)) / 2;
if (ans < p) {
k++;
ans = (k * (k + 1)) / 2;
}
l = ans - p;
cout << a[k - l - 1];
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
long long dp[(1 << 19)][20];
int lowbit[(1 << 19)];
bool G[20][20];
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i < (1 << 19); ++i) {
int j;
for (j = 0;; ++j) {
if ((i & (1 << j))) {
break;
}
}
lowbit[i] = j;
}
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
G[--a][--b] = 1, G[b][a] = 1;
}
for (int i = 0; i < n; ++i) {
dp[(1 << i)][i] = 1;
}
for (int mask = 1; mask < (1 << n); ++mask) {
for (int i = 0; i < n; ++i) {
if (dp[mask][i] > 0) {
for (int j = lowbit[mask] + 1; j < n; ++j) {
if ((!(mask & (1 << j))) && (G[i][j])) {
dp[mask | (1 << j)][j] += dp[mask][i];
}
}
}
}
}
long long ans = 0;
for (int mask = 1; mask < (1 << n); ++mask) {
if (__builtin_popcount(mask) >= 3) {
for (int i = 0; i < n; ++i) {
if (G[i][lowbit[mask]]) {
ans += dp[mask][i];
}
}
}
}
cout << ans / 2 << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
while (cin >> n) {
string s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
long long cnt1 = 0, cnt2 = 0, ans = 0;
for (int i = 0; i < n; i++) {
cnt1 = cnt2 = 0;
for (int j = 0; j < n; j++) {
if (s[i][j] == 'C') cnt1++;
if (s[j][i] == 'C') cnt2++;
}
ans += ((cnt1 * (cnt1 - 1)) / 2) + ((cnt2 * (cnt2 - 1)) / 2);
}
cout << ans << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w_c, b_c;
int cost = 0;
cin >> n >> w_c >> b_c;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n / 2; i++) {
if ((a[i] != a[n - 1 - i]) and (a[i] != 2 and a[n - 1 - i] != 2)) {
cout << -1;
return 0;
}
}
for (int i = 0; i < n / 2; i++) {
if (a[i] == 2 and a[n - 1 - i] == 2) {
cost += 2 * min(w_c, b_c);
continue;
}
if ((a[i] == 0 or a[n - 1 - i] == 0) and (a[i] == 2 or a[n - 1 - i] == 2)) {
cost += w_c;
continue;
}
if ((a[i] == 1 or a[n - 1 - i] == 1) and (a[i] == 2 or a[n - 1 - i] == 2)) {
cost += b_c;
}
}
if (n % 2 != 0 and a[n / 2] == 2) {
cost += min(w_c, b_c);
}
cout << cost;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MOD = 1e9 + 7;
const int N = 1 << 21;
int ct[N];
bool mark[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
for (int i = 0; i < n; ++i) {
int mask = 0;
for (int j = i; j >= 0; --j) {
int x = s[j] - 'a';
x = (1 << x);
if (mask & x) break;
mask |= x;
ct[mask] = __builtin_popcount(mask);
mark[mask] = 1;
}
}
for (int mask = 0; mask < (1 << 20); ++mask) {
for (int bit = 0; bit < 20; ++bit) {
if ((mask ^ (1 << bit)) < mask)
ct[mask] = max(ct[mask], ct[mask ^ (1 << bit)]);
}
}
int ans = 0;
for (int mask = 0; mask < (1 << 20); ++mask)
if (mark[mask]) ans = max(ans, ct[mask] + ct[mask ^ ((1 << 20) - 1)]);
cout << ans;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool isLittleCircleinBigCircle(double x, double y, double r, double R,
double d) {
return sqrt(x * x + y * y) + r <= R && sqrt(x * x + y * y) >= R - d + r;
}
int main() {
int R, d, n, ans = 0;
cin >> R >> d;
cin >> n;
int x, y, r;
for (int i = 0; i < n; i++) {
cin >> x >> y >> r;
if (isLittleCircleinBigCircle(x, y, r, R, d) == 1) {
ans++;
}
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
map<string, string> m_ip;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; ++i) {
string name, ip;
cin >> name >> ip;
m_ip[ip] = name;
}
for (int j = 0; j < m; ++j) {
string name, ip;
cin >> name >> ip;
cout << name << " " << ip << " #" << m_ip[ip.substr(0, ip.size() - 1)]
<< endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
vector<bool> used(n);
function<pair<int, int>(int, int)> dfs = [&](int u, int d) {
used[u] = true;
pair<int, int> best = {d, u};
for (int v : g[u]) {
if (used[v]) continue;
best = max(best, dfs(v, d + 1));
}
return best;
};
auto r = dfs(0, 0);
for (int i = 0; i < n; i++) used[i] = false;
auto ans = dfs(r.second, 0);
cout << ans.first << '\n';
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int opr(string s) {
int l, k, ind, i;
l = 0;
k = 0;
ind = 0;
for (i = 1; i < s.length(); i++)
if (s[i] != '-')
if (s[i] != s[ind]) {
l = -100500;
break;
} else
ind = i;
for (i = 1; i < s.length(); i++)
if (s[i] != '-')
if (s[i] >= s[ind]) {
k = -100500;
break;
} else
ind = i;
if (l == 0)
return (1);
else if (k == 0)
return (2);
else
return (0);
}
string s[111];
int a[111], b[111], c[111];
int main() {
string ss;
int n, pizza, taxi, girl, pi, gi, ta, i, j, m, indta, indpi, indgi, flag,
inda, indb, indc;
cin >> n;
pizza = -1;
girl = -1;
taxi = -1;
indpi = -1;
indgi = -1;
indta = -1;
a[0] = -1;
b[0] = -1;
c[0] = -1;
for (i = 1; i <= n; i++) {
cin >> m >> s[i];
pi = 0;
gi = 0;
ta = 0;
for (j = 1; j <= m; j++) {
cin >> ss;
flag = opr(ss);
if (flag == 1) ta++;
if (flag == 2) pi++;
if (flag == 0) gi++;
}
if (ta == taxi) {
inda++;
a[inda] = i;
}
if (pi == pizza) {
indc++;
c[indc] = i;
}
if (gi == girl) {
indb++;
b[indb] = i;
}
if (ta > taxi) {
indta = i;
taxi = ta;
inda = 1;
a[1] = i;
}
if (gi > girl) {
indgi = i;
girl = gi;
indb = 1;
b[1] = i;
}
if (pi > pizza) {
indpi = i;
pizza = pi;
indc = 1;
c[1] = i;
}
}
cout << "If you want to call a taxi, you should call: ";
for (i = 1; i <= inda; i++)
if (i != inda)
cout << s[a[i]] << ", ";
else
cout << s[a[i]] << "." << endl;
cout << "If you want to order a pizza, you should call: ";
for (i = 1; i <= indc; i++)
if (i != indc)
cout << s[c[i]] << ", ";
else
cout << s[c[i]] << "." << endl;
cout
<< "If you want to go to a cafe with a wonderful girl, you should call: ";
for (i = 1; i <= indb; i++)
if (i != indb)
cout << s[b[i]] << ", ";
else
cout << s[b[i]] << "." << endl;
cin.get();
cin.get();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> ans;
long long gcd(long long x, long long y) {
while (true) {
if (x == y) return x;
if (x < y) {
long long k = (y - 1) / x;
ans.push_back(-k);
y -= k * x;
} else {
long long k = (x - 1) / y;
ans.push_back(k);
x -= k * y;
}
}
}
int main() {
long long x, y;
cin >> x >> y;
if (gcd(x, y) != 1) {
puts("Impossible");
return 0;
}
for (int i = 0; i < ans.size(); i++) {
printf("%I64d%c", abs(ans[i]), ans[i] > 0 ? 'A' : 'B');
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
const double PI = 3.141592653589793238460;
using namespace std;
long long pows(long long a, long long n, long long m) {
long long res = 1;
while (n) {
if (n % 2 != 0) {
res = (res * a) % m;
n--;
} else {
a = (a * a) % m;
n = n / 2;
}
}
return res % m;
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
bool isprime(long long n) {
if (n == 1) {
return false;
}
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
bool istrue(string s) {
int i = 0;
int j = s.size() - 1;
while (i < j) {
if (s[i] == s[j]) {
i++;
j--;
} else {
return false;
}
}
return true;
}
long long n, d;
long double a[100001];
long double f(long double t) {
long double curr1 = 0;
long long currspeed1 = 1;
long double time1 = 0;
int target;
for (int i = 0; i <= n; i++) {
if (i == n) {
target = d;
} else
target = a[i];
long double req = (target - curr1) / currspeed1;
curr1 = min(curr1 + (min(req, t - time1)) * currspeed1, (long double)d);
time1 = time1 + min(req, t - time1);
if (curr1 < target) {
break;
}
currspeed1++;
}
long double curr2 = d;
long long currspeed2 = 1;
long double time2 = 0;
for (int i = n - 1; i >= -1; i--) {
if (i == -1)
target = 0;
else
target = a[i];
long double req = (curr2 - target) / currspeed2;
curr2 = max((long double)0, curr2 - min(req, t - time2) * currspeed2);
time2 = time2 + min(req, t - time2);
if (curr2 > target) {
break;
}
currspeed2++;
}
return curr1 >= curr2;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n >> d;
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
long double l = -1;
long double r = 1e9 + 7;
for (int i = 0; i <= 60; i++) {
long double m = (l + r) / 2;
if (f(m) == true) {
r = m;
} else {
l = m;
}
}
cout << setprecision(10) << r << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)(1e9 + 7);
const int MAXN = 1e6 + 5;
pair<long long, long long> p[MAXN << 2];
long long dp[MAXN << 2];
vector<long long> a[MAXN << 2];
long long sum[MAXN << 2];
int main() {
long long m, n, i, j, k;
vector<long long> v;
ios::sync_with_stdio(false);
;
cin >> m >> n;
map<long long, bool> mp;
if (n == 0) {
printf("%d\n", m == 0 ? 1 : 0);
return 0;
}
for (i = 1; i <= n; i++) {
cin >> p[i].first >> p[i].second;
if (!mp[p[i].first]) v.push_back(p[i].first);
mp[p[i].first] = 1;
if (!mp[p[i].second]) v.push_back(p[i].second);
mp[p[i].second] = 1;
}
sort((v).begin(), (v).end());
if (v[v.size() - 1] != m || v[0] != 0) {
cout << 0 << endl;
return 0;
}
for (i = 1; i <= n; i++) {
p[i].first =
lower_bound((v).begin(), (v).end(), p[i].first) - v.begin() + 1;
p[i].second =
lower_bound((v).begin(), (v).end(), p[i].second) - v.begin() + 1;
a[p[i].second].push_back(p[i].first);
}
m = v.size();
dp[1] = sum[1] = 1;
for (i = 1; i <= m; i++) {
for (j = 0; j < a[i].size(); j++) {
long long l = a[i][j], r = i - 1;
if (l > r)
continue;
else {
dp[i] += (sum[r] - sum[l - 1] + mod) % mod;
dp[i] %= mod;
}
}
sum[i] = (sum[i - 1] + dp[i]) % mod;
}
cout << dp[m] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
struct node {
int p, t;
} a[N];
bool operator<(const node &a, const node &b) { return a.p > b.p; }
bool cmp(const node &a, const node &b) {
if (a.t != b.t)
return a.t < b.t;
else
return a.p < b.p;
}
set<int> st;
priority_queue<node> pq;
queue<node> q;
long long now, ans[N];
int n, ps, m;
int main() {
scanf("%d %d", &n, &ps);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i].t);
a[i].p = i;
}
sort(a, a + n, cmp);
st.insert(n);
while (m < n || !q.empty() || !pq.empty()) {
if (!q.empty()) {
node tp = q.front();
q.pop();
now += ps;
ans[tp.p] = now;
while (m < n && a[m].t < now) {
if (a[m].p < *st.begin()) {
q.push(a[m]);
st.insert(a[m].p);
m++;
} else {
pq.push(a[m]);
m++;
}
}
st.erase(tp.p);
}
if (q.empty() && pq.empty()) now = max(now, (long long)a[m].t);
while (m < n && a[m].t <= now) pq.push(a[m++]);
if (!pq.empty() && pq.top().p < *st.begin()) {
q.push(pq.top());
st.insert(pq.top().p);
pq.pop();
}
}
for (int i = 0; i < n; ++i) printf("%lld ", ans[i]);
printf("\n");
}
| 7 |
#include <bits/stdc++.h>
const int N = 100100;
const long long mod = 1e9 + 7;
using namespace std;
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
long long int dp[2][200009];
long long int n, a, b;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> a >> b;
long long int l = 1, r = (a + b);
long long int mid;
while (l < r) {
mid = (l + r + 1) / 2LL;
if ((mid * (mid + 1)) <= ((a + b) * 2LL))
l = mid;
else
r = mid - 1;
}
n = l;
if (a > b) swap(a, b);
for (long long int i = 0; i <= a; i++) {
if (((n * (n + 1)) / 2LL) - (a - i) <= b)
dp[0][i] = 1;
else
dp[0][i] = 0;
}
long long int j = 1;
for (long long int i = n; i >= 1; i--) {
for (long long int k = 0; k <= a; k++) {
dp[j % 2][k] = dp[(j - 1) % 2][k];
if (k >= i) dp[j % 2][k] = (dp[j % 2][k] + dp[(j - 1) % 2][k - i]) % mod;
}
j++;
}
cout << dp[(j - 1) % 2][a] << "\n";
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f first
#define s second
#define for1(i,a,b) for (int i = a; i < b; i++)
#define for2(i,a,b) for (int i = a; i > b; i--)
#define arrin(a,n) for (int i = 0; i < n; i++) cin >> a[i];
#define arrout(a,n) for (int i = 0; i < n; i++) cout << a[i] << ' '; cout << '\n';
#define arrin2d(a,m,n) for (int i = 0; i < m; i++){for (int j = 0; j < n; j++) cin >> a[i][j];}
#define arrout2d(a,m,n) for (int i = 0; i < m; i++){for (int j = 0; j < n; j++) cout << a[i][j] << " "; cout << '\n';}
#define w(t) int t; cin >> t; while(t--)
#define strin(s) getline(cin,s)
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define mp make_pair
#define lower_bound lb
#define upper_bound ub
#define yes cout << "YES\n"
#define no cout << "NO\n"
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int,int>> vp;
typedef vector<char> vch;
typedef vector<string> vstr;
typedef set<ll> sll;
typedef pair<int,int> pi;
typedef map<int,int> mi;
typedef map<int,char> mch;
//typedef __int128_t vvlint;
const int inf = 1e9 + 7;
/*
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
*/
void tc()
{
int a1,a2,a3,a4,a5,c1,c2,c3;
cin >> c1 >> c2 >> c3;
cin >> a1 >> a2 >> a3 >> a4 >> a5;
if (a1 <= c1 && a2 <= c2 && a3 <= c3)
{
c1 -= a1;
c2 -= a2;
c3 -= a3;
a4 -= min(a4,c1);
a5 -= min(a5,c2);
if (a4 + a5 <= c3)
yes;
else
no;
}
else
no;
}
int main()
{
fastio;
w(t)
{
tc();
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int a[1005];
int main() {
int n;
scanf("%d", &n);
int s = 0;
int l = 0;
int r = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
if (s == 0 && a[i] != i) {
s++;
l = i;
} else if (s == 1 && a[i] != i && a[i] != a[i - 1] - 1) {
printf("0 0");
return 0;
} else if (s == 1 && a[i] != a[i - 1] - 1 && a[i] == i) {
r = i - 1;
s++;
} else if (s == 1 && a[i] == a[i - 1] - 1 && i == n) {
r = i;
s++;
} else if (s == 2 && a[i] != i) {
printf("0 0");
return 0;
}
}
printf("%d %d", l, r);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
double l[500], r[500];
int sign(double x) {
if (fabs(x) < eps) return 0;
return (x > 0) ? 1 : -1;
}
double calc(double a, double b, double h, double f) {
if (sign(a) == 0 || sign(b) == 0 || sign(a) * sign(b) == -1) return 0;
if (sign(a) == -1) {
a *= -1;
b *= -1;
}
double k1 = (h - f) / a, b1 = f;
double k2 = (f - h) / b, b2 = -f;
double x = (b2 - b1) / (k1 - k2);
double y = k1 * x + b1;
if (sign(y - h) > 0) return (b + b / (f - h) * (f + h)) * h;
if (sign(y + h) < 0) return (a + a / (f - h) * (f + h)) * h;
return (a + x) * (h - y) / 2 + (b + x) * (y + h) / 2;
}
int main() {
int n;
double h, f;
scanf("%d%lf%lf", &n, &h, &f);
for (int i = 0; i < n; i++) scanf("%lf%lf", &l[i], &r[i]);
double ans = 0;
for (int i = 0; i < n; i++) {
double x = r[i] - l[i];
double y = x / (f - h) * (f + h);
ans += (x + y) * h * 2;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
double tmp = 0;
tmp += calc(l[i], l[j], h, f);
tmp += calc(r[i], r[j], h, f);
tmp -= calc(l[i], r[j], h, f);
tmp -= calc(r[i], l[j], h, f);
ans -= tmp;
}
printf("%.10lf\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
long long dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
long long dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
using namespace std;
class pa3 {
public:
long long x;
long long y, z;
pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {}
bool operator<(const pa3 &p) const {
if (x != p.x) return x < p.x;
if (y != p.y) return y < p.y;
return z < p.z;
}
bool operator>(const pa3 &p) const {
if (x != p.x) return x > p.x;
if (y != p.y) return y > p.y;
return z > p.z;
}
bool operator==(const pa3 &p) const {
return x == p.x && y == p.y && z == p.z;
}
bool operator!=(const pa3 &p) const {
return !(x == p.x && y == p.y && z == p.z);
}
};
class pa4 {
public:
long long x;
long long y, z, w;
pa4(long long x = 0, long long y = 0, long long z = 0, long long w = 0)
: x(x), y(y), z(z), w(w) {}
bool operator<(const pa4 &p) const {
if (x != p.x) return x < p.x;
if (y != p.y) return y < p.y;
if (z != p.z) return z < p.z;
return w < p.w;
}
bool operator>(const pa4 &p) const {
if (x != p.x) return x > p.x;
if (y != p.y) return y > p.y;
if (z != p.z) return z > p.z;
return w > p.w;
}
bool operator==(const pa4 &p) const {
return x == p.x && y == p.y && z == p.z && w == p.w;
}
};
class pa2 {
public:
long long x, y;
pa2(long long x = 0, long long y = 0) : x(x), y(y) {}
pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); }
pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); }
bool operator<(const pa2 &p) const { return y != p.y ? y < p.y : x < p.x; }
bool operator>(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; }
bool operator==(const pa2 &p) const {
return abs(x - p.x) == 0 && abs(y - p.y) == 0;
}
bool operator!=(const pa2 &p) const {
return !(abs(x - p.x) == 0 && abs(y - p.y) == 0);
}
};
string itos(long long i) {
ostringstream s;
s << i;
return s.str();
}
long long gcd(long long v, long long b) {
if (v > b) return gcd(b, v);
if (v == b) return b;
if (b % v == 0) return v;
return gcd(v, b % v);
}
long long mod;
long long extgcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
pair<long long, long long> operator+(const pair<long long, long long> &l,
const pair<long long, long long> &r) {
return {l.first + r.first, l.second + r.second};
}
pair<long long, long long> operator-(const pair<long long, long long> &l,
const pair<long long, long long> &r) {
return {l.first - r.first, l.second - r.second};
}
long long pr[1001010];
long long inv[1001010];
long long beki(long long wa, long long rr, long long warukazu) {
if (rr == 0) return 1 % warukazu;
if (rr == 1) return wa % warukazu;
wa %= warukazu;
if (rr % 2 == 1)
return ((long long)beki(wa, rr - 1, warukazu) * (long long)wa) % warukazu;
long long zx = beki(wa, rr / 2, warukazu);
return (zx * zx) % warukazu;
}
long long comb(long long nn, long long rr) {
if (rr < 0 || rr > nn || nn < 0) return 0;
long long r = pr[nn] * inv[rr];
r %= mod;
r *= inv[nn - rr];
r %= mod;
return r;
}
void gya(long long ert) {
pr[0] = 1;
for (long long i = 1; i <= ert; i++) {
pr[i] = (pr[i - 1] * i) % mod;
}
inv[ert] = beki(pr[ert], mod - 2, mod);
for (long long i = ert - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
struct edge {
long long to, cap, num, moto;
};
vector<edge> ZANYO[100020];
long long label[100020];
queue<pair<long long, long long> > qu_bfs;
long long dokomade[100020];
void add_edge(long long s_point, long long t_point, long long capa) {
ZANYO[s_point].push_back(
(edge){t_point, capa, (long long)ZANYO[t_point].size(), 0});
ZANYO[t_point].push_back(
(edge){s_point, 0, (long long)ZANYO[s_point].size() - 1, 1});
}
void bfs_dinic(long long s_point, long long N) {
for (long long i = 0; i < N; i++) label[i] = -1;
while (qu_bfs.size()) qu_bfs.pop();
qu_bfs.push(make_pair(s_point, 0));
while (qu_bfs.size()) {
pair<long long, long long> z = qu_bfs.front();
qu_bfs.pop();
if (label[z.first] >= 0) continue;
label[z.first] = z.second;
for (auto &v : ZANYO[z.first]) {
if (v.cap == 0) continue;
if (v.cap < 0) {
cout << "error" << endl;
exit(0);
}
if (label[v.to] >= 0) continue;
qu_bfs.push(make_pair(v.to, z.second + 1));
}
}
return;
}
long long dfs_dinic(long long s_point, long long t_point, long long ima_flow) {
if (s_point == t_point) return ima_flow;
while (1) {
if (dokomade[s_point] >= ZANYO[s_point].size()) break;
long long edanum = dokomade[s_point];
if (ZANYO[s_point][edanum].cap < 0) {
cout << "minus" << endl;
exit(0);
}
if (ZANYO[s_point][edanum].cap <= 0 ||
label[s_point] + 1 != label[ZANYO[s_point][edanum].to]) {
dokomade[s_point]++;
continue;
}
long long W = dfs_dinic(ZANYO[s_point][edanum].to, t_point,
min(ZANYO[s_point][edanum].cap, ima_flow));
if (W > 0) {
ZANYO[s_point][edanum].cap -= W;
ZANYO[ZANYO[s_point][edanum].to][ZANYO[s_point][edanum].num].cap += W;
return W;
}
dokomade[s_point]++;
}
return 0;
}
long long max_flow_dinic(long long s_point, long long t_point, long long N) {
long long jougen = 1000000007 * 1000000000ll;
long long ans_flow = 0;
while (1) {
bfs_dinic(s_point, N);
if (label[t_point] == -1) break;
for (long long i = 0; i < N; i++) dokomade[i] = 0;
while (1) {
long long ima_flow = dfs_dinic(s_point, t_point, jougen + 1);
if (ima_flow == 0) break;
ans_flow += ima_flow;
}
}
return ans_flow;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
vector<pa4> ve;
long long n, q;
cin >> n >> q;
set<long long> sex, sey;
sex.insert(0);
sex.insert(n);
sey.insert(0);
sey.insert(n);
for (long long i = 0; i < q; i++) {
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
x1--, y1--;
sex.insert(x1);
sex.insert(x2);
sey.insert(y1);
sey.insert(y2);
ve.push_back((pa4){x1, x2, y1, y2});
}
vector<long long> vx, vy;
for (auto it = sex.begin(); it != sex.end(); it++) vx.push_back(*it);
for (auto it = sey.begin(); it != sey.end(); it++) vy.push_back(*it);
long long a = vx.size() - 1;
long long b = vy.size() - 1;
set<pair<long long, long long> > se;
for (long long i = 0; i < a; i++)
for (long long j = 0; j < b; j++) {
long long sx = vx[i], sy = vx[i + 1];
long long tx = vy[j], ty = vy[j + 1];
bool bo = 0;
for (auto v : ve) {
if (sx >= v.x && v.y >= sy && tx >= v.z && v.w >= ty) bo = 1;
}
if (bo) se.insert(make_pair(i, j));
}
long long A[200], B[200];
for (long long i = 0; i < a; i++) {
long long sx = vx[i], sy = vx[i + 1];
add_edge(0, 1 + i, sy - sx);
A[i] = sy - sx;
}
for (long long j = 0; j < b; j++) {
long long tx = vy[j], ty = vy[j + 1];
add_edge(a + 1 + j, a + b + 1, ty - tx);
B[j] = ty - tx;
}
for (auto it = se.begin(); it != se.end(); it++) {
pair<long long, long long> z = *it;
add_edge(1 + z.first, a + 1 + z.second, min(A[z.first], B[z.second]));
}
cout << max_flow_dinic(0, a + b + 1, a + b + 5) << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, ii, k, jj, t1, mp[27][27], a[51][51][27], b[51][51][27],
f[51][51], aa[51][3];
char s1[101], s2[101], ch[101];
int main() {
scanf("%s", s1 + 1);
scanf("%s", s2 + 1);
n = strlen(s1 + 1);
m = strlen(s2 + 1);
scanf("%d", &t1);
for (i = 1; i <= t1; i++) {
scanf("%s", ch);
aa[i][0] = ch[3] - 'a' + 1;
aa[i][1] = ch[4] - 'a' + 1;
aa[i][2] = ch[0] - 'a' + 1;
}
for (i = 1; i <= n; i++) a[i][i][s1[i] - 'a' + 1] = 1;
for (i = 1; i <= m; i++) b[i][i][s2[i] - 'a' + 1] = 1;
for (i = n; i; i--)
for (j = i + 1; j <= n; j++)
for (k = i; k < j; k++)
for (ii = 1; ii <= t1; ii++)
if (a[i][k][aa[ii][0]] && a[k + 1][j][aa[ii][1]])
a[i][j][aa[ii][2]] = 1;
for (i = m; i; i--)
for (j = i + 1; j <= m; j++)
for (k = i; k < j; k++)
for (ii = 1; ii <= t1; ii++)
if (b[i][k][aa[ii][0]] && b[k + 1][j][aa[ii][1]])
b[i][j][aa[ii][2]] = 1;
memset(f, 44, sizeof(f));
f[0][0] = 0;
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) {
for (ii = 0; ii < i; ii++)
for (jj = 0; jj < j; jj++)
for (k = 1; k <= 26; k++)
if (a[ii + 1][i][k] && b[jj + 1][j][k]) {
f[i][j] = min(f[i][j], f[ii][jj] + 1);
}
}
if (f[n][m] > 1e8) f[n][m] = -1;
printf("%d", f[n][m]);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf64 = 1e18;
const int mod = inf + 7;
const int N = 1e5 + 10;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int a, b, x;
cin >> a >> b >> x;
if (x % 2 == 0) {
if (a > b) {
for (int i = 0; i < x / 2; ++i) cout << "01";
cout << string(b - x / 2, '1');
cout << string(a - x / 2, '0');
} else {
for (int i = 0; i < x / 2; ++i) cout << "10";
cout << string(a - x / 2, '0');
cout << string(b - x / 2, '1');
}
} else if (a > b) {
for (int i = 0; i < x / 2; ++i) cout << "01";
cout << string(a - x / 2, '0');
cout << string(b - x / 2, '1');
} else {
for (int i = 0; i < x / 2; ++i) cout << "10";
cout << string(b - x / 2, '1');
cout << string(a - x / 2, '0');
}
cout << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n;
long long cnt[1000001];
long long ans = 0;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
vector<int> nums;
for (int i = 1; i <= n; i++) {
int u;
cin >> u;
if (cnt[u] == 0) {
ans += n * n;
nums.push_back(u);
}
ans -= ((i - cnt[u] - 1) * (i - cnt[u] - 1));
cnt[u] = i;
}
for (auto u : nums) {
ans -= (n - cnt[u]) * (n - cnt[u]);
}
cout << fixed << setprecision(6) << ans / double(n * n);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const int MAXN = 100007;
const int MOD = 30021;
const double eps = 1e-8;
const double PI = acos(-1.0);
map<int, int> SG, num;
int getSG(int x) {
if (x == 0) return 0;
if (SG.count(x)) return SG[x];
map<int, int> vis;
int p = x;
int t = 0;
while (p) p /= 2, t++;
for (int k = 1; k <= t; k++)
vis[getSG((x >> k) | (x & ((1 << k - 1) - 1)))] = 1;
for (int i = 0;; i++)
if (!vis[i]) return SG[x] = i;
}
int prime[MAXN], primesize;
bool isprime[MAXN];
void getlist(int listsize) {
memset(isprime, 1, sizeof(isprime));
isprime[1] = false;
for (int i = 2; i <= listsize; i++) {
if (isprime[i]) prime[++primesize] = i;
for (int j = 1; j <= primesize && i * prime[j] <= listsize; j++) {
isprime[i * prime[j]] = false;
if (i % prime[j] == 0) break;
}
}
}
void solve() {
int t, x;
scanf("%d", &x);
for (int i = 1; prime[i] * prime[i] <= x; i++) {
t = 0;
while (x % prime[i] == 0) x /= prime[i], t++;
if (t != 0) num[prime[i]] = num[prime[i]] | 1 << (t - 1);
}
if (x != 1) num[x] = num[x] | 1;
}
int main() {
getlist(100005);
int n;
int x;
int ans = 0;
scanf("%d", &n);
while (n--) solve();
map<int, int>::iterator it;
for (it = num.begin(); it != num.end(); it++) ans ^= getSG(it->second);
if (ans == 0)
printf("Arpa\n");
else
printf("Mojtaba\n");
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
if (k < n) {
if ((n / k) < (k - 1) || (k * (k - 1) == n)) {
cout << "YES" << endl;
long long s = 1;
for (long long i = 1; i <= n; i++) {
if (i % k != 0 && (i + s) % k != 0)
cout << i % k << " " << (i + s) % k << endl;
else if (i % k != 0 && (i + s) % k == 0)
cout << i % k << " " << k << endl;
else if (i % k == 0 && (i + s) % k != 0) {
cout << k << " " << (i + s) % k << endl;
s++;
}
}
} else
cout << "NO" << endl;
} else {
cout << "YES" << endl;
if (k == n) {
for (long long i = 1; i <= n; i++)
if (i % n != 0)
cout << i << " " << i + 1 << endl;
else
cout << i << " " << 1 << endl;
} else {
for (long long i = 1; i <= n; i++) cout << i << " " << i + 1 << endl;
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t;
cin >> t;
while (t--) {
int n;
vector<int> vi;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
vi.push_back(x);
}
sort(vi.begin(), vi.end());
long long int total = 1;
for (int i = n - 1; i >= 0; i--) {
if (vi[i] <= i + 1) {
cout << i + 2 << endl;
total = 2;
break;
}
}
if (total == 1) cout << total << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int md = 1e9 + 7;
const long long inf = 2e18;
const int OO = 0;
const int OOO = 1;
using namespace std;
struct edge {
int u, v, i, vis;
edge() {}
edge(int uu, int vv, int ii) {
u = uu;
v = vv;
i = ii;
vis = 0;
}
int to(int source) { return source ^ u ^ v; }
};
const int N = 1 << 20;
int n;
vector<pair<int, int>> a;
vector<int> ans;
vector<pair<int, int>> ans2;
vector<edge> e;
vector<int> g[N];
void dfs(int v, vector<int> &build, vector<pair<int, int>> &build2) {
while (g[v].size()) {
edge &x = e[g[v].back()];
g[v].pop_back();
if (x.vis) continue;
x.vis = 1;
dfs(x.to(v), build, build2);
build.push_back(x.i);
build2.push_back({v, x.to(v)});
}
}
bool work(int d) {
if (OO) cout << "start work " << d << endl;
e.clear();
for (int i = 0; i < d; i++) g[i].clear();
for (int i = 0; i < n; i++) {
int u = a[i].first % d;
int v = a[i].second % d;
g[u].push_back(e.size());
g[v].push_back(e.size());
e.push_back(edge(u, v, i));
}
for (int i = 0; i < d; i++) {
if (g[i].size() % 2 == 1) {
return false;
}
}
int at = -1;
for (int i = 0; i < d; i++) {
if (g[i].size()) {
at = i;
break;
}
}
if (at == -1) {
cout << "wtf\n";
}
if (OO) {
cout << "found at " << at << endl;
}
vector<int> build;
vector<pair<int, int>> build2;
dfs(at, build, build2);
if (build.size() != n) return false;
ans = build;
ans2 = build2;
return true;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
a.resize(n);
for (auto &i : a) cin >> i.first >> i.second;
int d = -1;
for (int i = 20; i >= 0; i--) {
if (work(1 << i)) {
cout << i << endl;
d = 1 << i;
break;
}
}
for (int i = 0; i < n; i++) {
if (ans2[i].first == ans2[i].second) {
cout << 2 * ans[i] + 1 << " " << 2 * ans[i] + 2 << " ";
} else {
if (a[ans[i]].first % d == ans2[i].second) {
cout << 2 * ans[i] + 1 << " " << 2 * ans[i] + 2 << " ";
} else {
cout << 2 * ans[i] + 2 << " " << 2 * ans[i] + 1 << " ";
}
}
}
}
| 8 |
#include <bits/stdc++.h>
const long long int MOD = 1000000007;
long long int dp[255][255];
long long int fastK[255];
long long int fac[255], ifac[255];
long long int k;
int n;
long long int fexpo(long long int base, long long int p) {
long long int ret = 1;
while (p) {
if (p % 2) {
ret = ret * base % MOD;
}
base = base * base % MOD;
p /= 2;
}
return ret;
}
long long int comb(int a, int b) {
return fac[a] * ifac[b] % MOD * ifac[a - b] % MOD;
}
long long int serc(int rem, int row) {
if (row == n) {
return rem == 0;
}
if (dp[rem][row] != -1) return dp[rem][row];
long long int &ret = dp[rem][row];
ret = 0;
long long int rest = 0;
long long int extra = fastK[n - rem];
for (int i = 1; i <= n - rem; i++) {
rest = (rest + comb(n - rem, i) * fastK[n - rem - i] % MOD) % MOD;
}
for (int i = 0; i <= rem; i++) {
long long int rec = serc(rem - i, row + 1);
long long int way = comb(rem, i) * fastK[rem - i] % MOD;
ret =
(ret + rec * way % MOD * ((rest + (i > 0) * extra) % MOD) % MOD) % MOD;
}
return ret;
}
int main() {
scanf("%d %lld", &n, &k);
for (int i = 0; i <= n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = -1;
}
}
fastK[0] = 1;
for (int i = 1; i <= n; i++) {
fastK[i] = fastK[i - 1] * (k - 1) % MOD;
}
fac[0] = ifac[0] = 1;
for (int i = 1; i <= 250; i++) {
fac[i] = fac[i - 1] * i % MOD;
ifac[i] = fexpo(fac[i], MOD - 2);
}
printf("%lld\n", serc(n, 0));
return 0;
}
| 7 |
#include <bits/stdc++.h>
template <class T>
inline T max(const T &x, const T &y) {
return x > y ? x : y;
}
template <class T>
inline T min(const T &x, const T &y) {
return x < y ? x : y;
}
template <class T>
inline bool chkmax(T &x, const T &y) {
return x < y ? x = y, true : false;
}
template <class T>
inline bool chkmin(T &x, const T &y) {
return x > y ? x = y, true : false;
}
inline long long read() {
register long long x = 0, v = 1;
register char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') v = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * v;
}
const long long MAXN = 2e5;
long long n, ans;
long long X[MAXN | 1], Y[MAXN | 1];
std::map<long long, long long> cnt1, cnt2;
std::map<std::pair<long long, long long>, long long> cnt3;
signed main() {
n = read();
for (long long i = 1; i <= n; ++i) {
X[i] = read();
Y[i] = read();
ans += cnt1[X[i]] + cnt2[Y[i]] - cnt3[std::make_pair(X[i], Y[i])];
++cnt1[X[i]];
++cnt2[Y[i]];
++cnt3[std::make_pair(X[i], Y[i])];
}
printf("%lld\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int base = 19260817;
const long long INF = 1e18;
const int maxn = 1e6 + 10;
const double eps = 1e-8;
int add(int x, int y) {
x += y;
return x >= mod ? x - mod : x;
}
int sub(int x, int y) {
x -= y;
return x < 0 ? x + mod : x;
}
int mul(int x, int y) { return 1LL * x * y % mod; }
int p[maxn], L[maxn], R[maxn];
long long c[maxn], d[maxn], ans[maxn];
pair<int, int> que[maxn];
stack<int> s;
vector<int> st[maxn], ed[maxn], qs[maxn], qe[maxn];
int lowbit(int o) { return o & (-o); }
void update(long long *tr, int o, int v) {
while (o < maxn) {
tr[o] += v;
o += lowbit(o);
}
}
long long query(long long *tr, int o) {
long long res = 0;
while (o) {
res += tr[o];
o -= lowbit(o);
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i <= q; i++) cin >> que[i].first;
for (int i = 1; i <= q; i++) cin >> que[i].second;
for (int i = 1; i <= n; i++) {
while (!s.empty() && p[s.top()] < p[i]) R[s.top()] = i, s.pop();
s.push(i);
}
while (!s.empty()) R[s.top()] = n + 1, s.pop();
for (int i = n; i >= 1; i--) {
while (!s.empty() && p[s.top()] < p[i]) L[s.top()] = i, s.pop();
s.push(i);
}
while (!s.empty()) L[s.top()] = 0, s.pop();
for (int i = 1; i <= n; i++) st[L[i]].push_back(i), ed[R[i]].push_back(i);
for (int i = 1; i <= q; i++)
qs[que[i].first].push_back(i), qe[que[i].second].push_back(i);
for (int i = 1; i <= n; i++) update(c, i, L[i]);
for (int i = 1; i <= n; i++) {
for (int j = 0; j < st[i - 1].size(); j++) {
int id = st[i - 1][j];
update(c, id, -L[id]);
update(d, id, 1);
}
for (int j = 0; j < qs[i].size(); j++) {
int id = qs[i][j];
ans[id] -= query(c, que[id].second) - query(c, que[id].first - 1);
ans[id] -=
(query(d, que[id].second) - query(d, que[id].first - 1)) * (i - 1);
}
}
memset(c, 0, sizeof(c)), memset(d, 0, sizeof(d));
for (int i = 1; i <= n; i++) update(c, i, R[i] - 1);
for (int i = n; i >= 1; i--) {
for (int j = 0; j < ed[i + 1].size(); j++) {
int id = ed[i + 1][j];
update(c, id, -R[id] + 1);
update(d, id, 1);
}
for (int j = 0; j < qe[i].size(); j++) {
int id = qe[i][j];
ans[id] += query(c, que[id].second) - query(c, que[id].first - 1);
ans[id] += (query(d, que[id].second) - query(d, que[id].first - 1)) * i;
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << " ";
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> primes;
void Sieve(long long int n) {
vector<bool> prime(n + 1, true);
for (long long int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long int i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (long long int p = 2; p <= n; p++)
if (prime[p]) primes.push_back(p);
}
vector<pair<long long int, long long int>> vec1;
vector<pair<long long int, long long int>> vec2;
vector<vector<pair<long long int, pair<long long int, long long int>>>> adj;
vector<bool> visited;
long long int dfs(long long int x, long long int w, long long int w1) {
visited[x] = true;
long long int c = 0;
for (auto it : adj[x]) {
if (!visited[it.first])
c += dfs(it.first, it.second.first, it.second.second);
}
if (adj[x].size() == 1 && w != 0) {
if (w1 == 1)
vec1.push_back({w, 1});
else if (w1 == 2)
vec2.push_back({w, 1});
return 1;
}
if (adj[x].size() != 1 && w != 0) {
if (w1 == 1)
vec1.push_back({w, c});
else if (w1 == 2)
vec2.push_back({w, c});
return c;
}
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, m;
cin >> n;
cin >> m;
adj =
vector<vector<pair<long long int, pair<long long int, long long int>>>>(
n + 1);
for (long long int i = 0; i < n - 1; i++) {
long long int x, y, z, z1;
cin >> x;
cin >> y;
cin >> z;
cin >> z1;
adj[x].push_back({y, {z, z1}});
adj[y].push_back({x, {z, z1}});
}
visited = vector<bool>(n + 1, 0);
adj[0].push_back({1, {0, -1}});
dfs(0, 0, -1);
vector<long long int> v1;
vector<long long int> v2;
long long int total = 0;
for (auto it : vec1) {
long long int x = it.first;
long long int y = it.second;
total += (x * y);
while (x != 0) {
v1.push_back((x - (x / 2)) * y);
x = x / 2;
}
}
for (auto it : vec2) {
long long int x = it.first;
long long int y = it.second;
total += (x * y);
while (x != 0) {
v2.push_back((x - (x / 2)) * y);
x = x / 2;
}
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
reverse(v1.begin(), v1.end());
reverse(v2.begin(), v2.end());
long long int cost = 0;
long long int sum = 0;
vector<pair<long long int, long long int>> cost1;
vector<pair<long long int, long long int>> cost2;
cost1.push_back({0, 0});
cost2.push_back({0, 0});
for (auto it : v1) {
sum += it;
cost1.push_back({sum, ++cost});
}
cost = 0;
sum = 0;
for (auto it : v2) {
sum += it;
cost += 2;
cost2.push_back({sum, cost});
}
long long int mini = 9999999999999999;
for (auto it : cost1) {
long long int x = it.first;
long long int y = it.second;
long long int temp = total - x;
temp = temp - m;
long long int ans = y;
pair<long long int, long long int> p = {temp, -1};
auto it1 = std::lower_bound(cost2.begin(), cost2.end(), p);
if (it1 != cost2.end()) {
ans += ((*it1).second);
mini = min(mini, ans);
}
}
visited.clear();
adj.clear();
vec1.clear();
vec2.clear();
cout << mini << endl;
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long res = 0, flag = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') flag = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
res = (res << 3) + (res << 1) + (ch ^ 48);
ch = getchar();
}
return res * flag;
}
const long long maxn = 1e6 + 5;
const long long mod = 1e9 + 7;
const double pi = acos(-1);
const double eps = 1e-8;
long long n, fa1[maxn], fa2[maxn], m1, m2;
long long find(long long x, long long fa[]) {
return fa[x] == x ? x : fa[x] = find(fa[x], fa);
}
vector<pair<long long, long long>> v;
signed main() {
long long t = 1;
while (t--) {
n = read(), m1 = read(), m2 = read();
for (long long i = 1; i <= n; i++) fa1[i] = fa2[i] = i;
for (long long i = 1; i <= m1; i++) {
long long u = read(), v = read();
long long fax = find(u, fa1), fay = find(v, fa1);
if (fax != fay) {
fa1[fax] = fay;
}
}
for (long long i = 1; i <= m2; i++) {
long long u = read(), v = read();
long long fax = find(u, fa2), fay = find(v, fa2);
if (fax != fay) {
fa2[fax] = fay;
}
}
long long ans = 0;
for (long long i = 1; i <= n; i++) {
for (long long j = i + 1; j <= n; j++) {
long long fax = find(i, fa1), fay = find(j, fa1);
long long faxx = find(i, fa2), fayy = find(j, fa2);
if (fax != fay && faxx != fayy) {
ans++;
fa1[fax] = fay;
fa2[faxx] = fayy;
v.push_back({i, j});
}
}
}
cout << ans << '\n';
for (auto p : v) cout << p.first << " " << p.second << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
namespace ufs {
int fa[100005];
void makeset(int n) {
for (int i = 1; i <= n; i++) fa[i] = i;
}
int findset(int u) {
if (fa[u] == u) return fa[u];
return fa[u] = findset(fa[u]);
}
void unionset(int a, int b) {
int u = findset(a);
int v = findset(b);
fa[u] = v;
}
} // namespace ufs
using namespace ufs;
struct node {
int e;
int c;
};
int n, m;
int ans1, ans2;
int mem1[100005], mem2[100005], used[100005];
bool vis[100005];
vector<int> bel[100005];
vector<node> g[100005];
void init(int st) {
for (int i = 0; i < bel[st].size(); i++) used[bel[st][i]] = -1;
}
bool dfs(int u, int col) {
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i].e;
int edge_col = (g[u][i].c ^ used[u]);
if (edge_col == col) {
if (used[v] == 1) return 0;
if (used[v] == -1) {
used[v] = 0;
if (dfs(v, col) == 0) return 0;
}
} else {
if (used[v] == 0) return 0;
if (used[v] == -1) {
used[v] = 1;
if (dfs(v, col) == 0) return 0;
}
}
}
return 1;
}
void update1(int st, int &ans) {
int tot = 0;
for (int i = 0; i < bel[st].size(); i++)
if (used[bel[st][i]] == 1) tot++;
if (tot < ans) {
ans = tot;
for (int i = 0; i < bel[st].size(); i++)
mem1[bel[st][i]] = used[bel[st][i]];
}
}
void update2(int st, int &ans) {
int tot = 0;
for (int i = 0; i < bel[st].size(); i++)
if (used[bel[st][i]] == 1) tot++;
if (tot < ans) {
ans = tot;
for (int i = 0; i < bel[st].size(); i++)
mem2[bel[st][i]] = used[bel[st][i]];
}
}
bool check1(int st) {
int ans = (1 << 30);
init(st);
used[st] = 1;
if (dfs(st, 1)) update1(st, ans);
init(st);
used[st] = 0;
if (dfs(st, 1)) update1(st, ans);
if (ans == (1 << 30)) return 1;
ans1 += ans;
return 0;
}
bool check2(int st) {
int ans = (1 << 30);
init(st);
used[st] = 1;
if (dfs(st, 0)) update2(st, ans);
init(st);
used[st] = 0;
if (dfs(st, 0)) update2(st, ans);
if (ans == (1 << 30)) return 1;
ans2 += ans;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
makeset(n);
for (int i = 1; i <= m; i++) {
int u, v;
char c;
cin >> u >> v >> c;
g[u].push_back((node){v, c == 'B' ? 1 : 0});
g[v].push_back((node){u, c == 'B' ? 1 : 0});
unionset(u, v);
}
for (int i = 1; i <= n; i++) bel[findset(i)].push_back(i);
for (int i = 1; i <= n; i++)
if (vis[findset(i)] == 0) {
vis[findset(i)] = 1;
if (check1(findset(i))) {
ans1 = (1 << 30);
break;
}
}
for (int i = 1; i <= n; i++) vis[i] = 0;
for (int i = 1; i <= n; i++)
if (vis[findset(i)] == 0) {
vis[findset(i)] = 1;
if (check2(findset(i))) {
ans2 = (1 << 30);
break;
}
}
if (ans1 == (1 << 30) && ans2 == (1 << 30))
cout << "-1";
else {
if (ans1 > ans2) {
ans1 = ans2;
for (int i = 1; i <= n; i++) mem1[i] = mem2[i];
}
cout << ans1 << endl;
for (int i = 1; i <= n; i++)
if (mem1[i] == 1) cout << i << ' ';
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch;
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return f * x;
}
inline char readch() {
char ch = getchar();
while (ch == ' ' || ch == '\n') ch = getchar();
return ch;
}
inline string readstr() {
char ch = getchar();
string ans = "";
while (ch == ' ' || ch == '\n') ch = getchar();
while (ch != ' ' && ch != '\n') {
ans += ch;
ch = getchar();
}
return ans;
}
const int MAXN = 100;
int n;
vector<int> V = {0}, K = {0}, X = {0};
int dp[MAXN][MAXN][MAXN][3];
inline int calc(int x, int y, int z, int pos) {
int ans = pos - (x + y + z + 1);
for (int i = 1; i <= x; i++)
if (V[i] >= pos) ans++;
for (int i = 1; i <= y; i++)
if (K[i] >= pos) ans++;
for (int i = 1; i <= z; i++)
if (X[i] >= pos) ans++;
return ans;
}
signed main() {
memset(dp, 0x3f3f3f3f, sizeof(dp));
n = read();
for (int i = 1; i <= n; i++) {
char tmp = readch();
if (tmp == 'V')
V.push_back(i);
else if (tmp == 'K')
K.push_back(i);
else
X.push_back(i);
}
dp[0][0][0][0] = dp[0][0][0][1] = 0;
for (int i = 0; i < V.size(); i++) {
for (int j = 0; j < K.size(); j++) {
for (int k = 0; k < X.size(); k++) {
if (i != 0)
dp[i][j][k][1] =
min(dp[i][j][k][1], min(dp[i - 1][j][k][0], dp[i - 1][j][k][1]) +
calc(i - 1, j, k, V[i]));
if (k != 0)
dp[i][j][k][0] =
min(dp[i][j][k][0], min(dp[i][j][k - 1][0], dp[i][j][k - 1][1]) +
calc(i, j, k - 1, X[k]));
if (j != 0)
dp[i][j][k][0] =
min(dp[i][j][k][0], dp[i][j - 1][k][0] + calc(i, j - 1, k, K[j]));
}
}
}
cout << min(dp[V.size() - 1][K.size() - 1][X.size() - 1][0],
dp[V.size() - 1][K.size() - 1][X.size() - 1][1])
<< endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
template <typename T>
inline T max(T a, T b) {
return a > b ? a : b;
}
template <typename T>
inline T min(T a, T b) {
return a < b ? a : b;
}
template <typename T>
inline T abs(T a) {
return a > 0 ? a : -a;
}
template <typename T>
inline bool repr(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool repl(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline T gcd(T a, T b) {
T t;
if (a < b) {
while (a) {
t = a;
a = b % a;
b = t;
}
return b;
} else {
while (b) {
t = b;
b = a % b;
a = t;
}
return a;
}
}
template <typename T>
inline T sqr(T x) {
return x * x;
}
struct Cg {
inline char operator()() { return getchar(); }
};
struct Cp {
inline void operator()(char x) { putchar(x); }
};
inline bool IS(char x) { return x == 10 || x == 13 || x == ' '; }
template <typename T>
struct Fr {
T P;
inline Fr &operator,(int &x) {
x = 0;
char t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
bool f = 0;
if (t == '-') t = P(), f = 1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (f) x = -x;
return *this;
}
inline operator int() {
int x;
*this, x;
return x;
}
inline Fr &operator,(long long &x) {
x = 0;
char t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
bool f = 0;
if (t == '-') t = P(), f = 1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (f) x = -x;
return *this;
}
inline operator long long() {
long long x;
*this, x;
return x;
}
inline Fr &operator,(char &x) {
for (x = P(); IS(x); x = P())
;
return *this;
}
inline operator char() {
char x;
*this, x;
return x;
}
inline Fr &operator,(char *x) {
char t = P();
for (; IS(t); t = P())
;
if (~t) {
for (; !IS(t) && ~t; t = P()) *x++ = t;
}
*x++ = 0;
return *this;
}
inline Fr &operator,(double &x) {
x = 0;
char t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
bool f = 0;
if (t == '-') t = P(), f = 1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (t == '.') {
double u = 0.1;
for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1) x += u * (t - '0');
}
if (f) x = -x;
return *this;
}
inline operator double() {
double x;
*this, x;
return x;
}
inline Fr &operator,(long double &x) {
x = 0;
char t = P();
while ((t < '0' || t > '9') && t != '-') t = P();
bool f = 0;
if (t == '-') t = P(), f = 1;
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
if (t == '.') {
double u = 0.1;
for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1) x += u * (t - '0');
}
if (f) x = -x;
return *this;
}
inline operator long double() {
long double x;
*this, x;
return x;
}
inline Fr &operator,(unsigned int &x) {
x = 0;
char t = P();
while (t < '0' || t > '9') t = P();
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
return *this;
}
inline operator unsigned int() {
unsigned int x;
*this, x;
return x;
}
inline Fr &operator,(unsigned long long &x) {
x = 0;
char t = P();
while (t < '0' || t > '9') t = P();
x = t - '0';
for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
return *this;
}
inline operator unsigned long long() {
unsigned long long x;
*this, x;
return x;
}
};
Fr<Cg> in;
template <typename T>
struct Fw {
T P;
inline Fw &operator,(int x) {
if (x) {
if (x < 0) P('-'), x = -x;
char s[10], c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(int x) {
if (x) {
if (x < 0) P('-'), x = -x;
char s[10], c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(unsigned int x) {
if (x) {
char s[10], c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(unsigned int x) {
if (x) {
char s[10], c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(long long x) {
if (x) {
if (x < 0) P('-'), x = -x;
char s[19], c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(long long x) {
if (x) {
if (x < 0) P('-'), x = -x;
char s[19], c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(unsigned long long x) {
if (x) {
char s[20], c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator()(unsigned long long x) {
if (x) {
char s[20], c = 0;
while (x) s[c++] = x % 10 + '0', x /= 10;
while (c--) P(s[c]);
} else
P('0');
return *this;
}
inline Fw &operator,(char x) {
P(x);
return *this;
}
inline Fw &operator()(char x) {
P(x);
return *this;
}
inline Fw &operator,(const char *x) {
while (*x) P(*x++);
return *this;
}
inline Fw &operator()(const char *x) {
while (*x) P(*x++);
return *this;
}
inline Fw &operator()(double x, int y) {
if (y) {
double t = 0.5;
for (int i = y; i--;) t *= 0.1;
if (x >= 0)
x += t;
else
x -= t, P('-');
*this, (long long)(abs(x));
P('.');
if (x < 0) x = -x;
while (y--) {
x *= 10;
x -= floor(x * 0.1) * 10;
P(((int)x) % 10 + '0');
}
} else if (x >= 0)
*this, (long long)(x + 0.5);
else
*this, (long long)(x - 0.5);
;
return *this;
}
inline Fw &operator()(long double x, int y) {
if (y) {
double t = 0.5;
for (int i = y; i--;) t *= 0.1;
if (x >= 0)
x += t;
else
x -= t, P('-');
*this, (long long)(abs(x));
P('.');
if (x < 0) x = -x;
while (y--) {
x *= 10;
x -= floor(x * 0.1) * 10;
P(((int)x) % 10 + '0');
}
} else if (x >= 0)
*this, (long long)(x + 0.5);
else
*this, (long long)(x - 0.5);
;
return *this;
}
};
Fw<Cp> out;
const int N = 107;
int n;
std::vector<std::pair<int, int> > p[N];
inline void dfs(int x, int fa, double t) {
double v = 2.0 / p[x].size();
for (__typeof(p[x].end()) i = p[x].begin(); i != p[x].end(); ++i)
if (i->first != fa) {
t += v;
if (t >= 2) t -= 2;
if (t < 1)
printf("1 %d %d %d %.10lf\n", i->second, i->first, x, 1 - t);
else
printf("1 %d %d %d %.10lf\n", i->second, x, i->first, 2 - t);
dfs(i->first, x, t >= 1 ? t - 1 : t + 1);
}
}
int main() {
in, n;
for (int i = 1, iend = n - 1; i <= iend; i++) {
int x, y;
in, x, y;
p[x].push_back(std::make_pair(y, i));
p[y].push_back(std::make_pair(x, i));
}
out, n - 1, '\n';
dfs(1, 0, 0);
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
bool debug = true;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int dy[] = {1, 0, -1, 0, -1, 1, 1, -1, 0};
template <typename X>
inline X square(const X& a) {
return a * a;
}
int scan_d() {
bool minus = false;
int result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = (result << 3) + (result << 1) + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
long scan_ld() {
bool minus = false;
long result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = (result << 3) + (result << 1) + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
long long scan_lld() {
bool minus = false;
long long result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
if (ch == '-')
minus = true;
else
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = (result << 3) + (result << 1) + (ch - '0');
}
if (minus)
return -result;
else
return result;
}
long long unsigned scan_llu() {
long long unsigned result = 0;
char ch;
ch = getchar();
while (true) {
if (ch == '-') break;
if (ch >= '0' && ch <= '9') break;
ch = getchar();
}
result = ch - '0';
while (true) {
ch = getchar();
if (ch < '0' || ch > '9') break;
result = (result << 3) + (result << 1) + (ch - '0');
}
return result;
}
void print_d(int n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 10;
char output_buffer[10];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 10);
}
void print_ld(long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 11;
char output_buffer[11];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 11);
}
void print_lld(long long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 21;
char output_buffer[21];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 21);
}
void print_llu(long long unsigned n) {
int i = 21;
char output_buffer[21];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 21);
}
void println_d(int n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 10;
char output_buffer[11];
output_buffer[10] = '\n';
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 11);
}
void println_ld(long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 11;
char output_buffer[12];
output_buffer[11] = '\n';
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 12);
}
void println_lld(long long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 21;
char output_buffer[22];
output_buffer[21] = '\n';
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 22);
}
void println_llu(long long unsigned n) {
int i = 21;
char output_buffer[22];
output_buffer[21] = '\n';
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 22);
}
char sp;
void printsp_d(int n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 10;
char output_buffer[11];
output_buffer[10] = sp;
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 11);
}
void printsp_ld(long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 11;
char output_buffer[12];
output_buffer[11] = sp;
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 12);
}
void printsp_lld(long long n) {
if (n < 0) {
n = -n;
putchar('-');
}
int i = 21;
char output_buffer[22];
output_buffer[21] = sp;
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 22);
}
void printsp_llu(long long unsigned n) {
int i = 21;
char output_buffer[22];
output_buffer[21] = sp;
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar(output_buffer[i]);
} while (++i < 22);
}
int main() {
int n, a, b, ans, temp;
ans = temp = 0;
n = scan_d();
a = scan_d();
b = scan_d();
while (n--) {
int x = scan_d();
if (x == 1) {
if (a >= 1) {
a--;
} else if (b >= 1) {
b--;
temp++;
} else if (temp >= 1) {
temp--;
} else
ans++;
} else if (x == 2) {
if (b >= 1) {
b--;
} else
ans += 2;
}
}
println_d(ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, q;
cin >> n >> q;
vector<int> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
arr.emplace_back(0);
long long up = 0;
long long down = 0;
vector<bool> valid(n + 1);
for (int i = 1; i <= n; i++) {
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
up += arr[i];
valid[i] = true;
}
if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) {
down += arr[i];
valid[i] = true;
}
}
cout << up - down << '\n';
auto remover = [&](int i) {
if (!valid[i]) {
return;
}
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
up -= arr[i];
valid[i] = false;
}
if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) {
down -= arr[i];
valid[i] = false;
}
};
auto adicionar = [&](int i) {
if (valid[i]) {
return;
}
if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) {
up += arr[i];
valid[i] = true;
}
if (arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) {
down += arr[i];
valid[i] = true;
}
};
while (q--) {
int l, r;
cin >> l >> r;
for (int i = max(1, l - 1); i <= min(n, l + 1); i++) {
remover(i);
}
for (int i = max(1, r - 1); i <= min(n, r + 1); i++) {
remover(i);
}
swap(arr[l], arr[r]);
for (int i = max(1, l - 1); i <= min(n, l + 1); i++) {
adicionar(i);
}
for (int i = max(1, r - 1); i <= min(n, r + 1); i++) {
adicionar(i);
}
cout << up - down << '\n';
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int n = s.length();
int cnt = 1;
int ans = 0;
bool b = true;
for (int i = 1; i < n; ++i) {
if (s[i] == s[i - 1]) {
b = false;
break;
}
}
if (b) {
cout << n << endl;
return 0;
}
s += s;
n *= 2;
for (int i = 1; i < n; ++i) {
if (s[i] == s[i - 1]) {
ans = max(ans, cnt);
cnt = 1;
} else
++cnt;
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b, x[200000], y[200000], l[200000];
int main() {
scanf("%d%d%d%d", &n, &m, &a, &b);
for (int i = 0; i < n; ++i) scanf("%d", &x[i]);
for (int i = 0; i < m; ++i) scanf("%d", &y[i]);
for (int i = 0; i < m; ++i) scanf("%d", &l[i]);
double Min = 1e18, nn = -1, mm = -1;
for (int j = 0; j < m; ++j) {
double t = l[j];
double yy = 1.0 * y[j] * a / b;
int ind = lower_bound(x, x + n, yy) - x;
for (int i = ind - 1; i <= ind; ++i)
if (i >= 0 && i < n) {
double second =
sqrt(1.0 * a * a + 1.0 * x[i] * x[i]) +
sqrt(1.0 * (a - b) * (a - b) + 1.0 * (x[i] - y[j]) * (x[i] - y[j]));
if (Min > second + t + 1e-8) {
Min = second + t;
nn = i + 1, mm = j + 1;
}
}
}
cout << nn << ' ' << mm;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300000, M = 500000;
int xx[N], yy[N], n;
int x_, y_;
bool check(int d) {
int xl = 0, xr = M;
int yl = 0, yr = M;
int xyl = -M, xyr = M;
for (int i = 0; i < n; i++) {
int x = xx[i], y = yy[i];
xl = max(xl, x - d);
xr = min(xr, x + d);
yl = max(yl, y - d);
yr = min(yr, y + d);
xyl = max(xyl, x - y - d);
xyr = min(xyr, x - y + d);
}
if (xl > xr || yl > yr || xyl > xyr) return false;
int l = max(xl - yr, xyl), r = min(xr - yl, xyr);
if (l > r) return false;
if (yl <= xl - l)
x_ = xl, y_ = xl - l;
else
x_ = yl + l, y_ = yl;
return true;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
int m = s.length();
for (int j = 0; j < m; j++) (s[j] == 'B' ? xx[i] : yy[i])++;
}
int lower = -1, upper = M;
while (upper - lower > 1) {
int d = (lower + upper) / 2;
if (check(d))
upper = d;
else
lower = d;
}
check(upper);
if (x_ == 0 && y_ == 0) x_ = y_ = 1;
cout << upper << '\n';
cout << string(x_, 'B') + string(y_, 'N') << '\n';
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
int m, s;
cin >> m >> s;
v[i] = make_pair(m, s);
}
sort(v.begin(), v.end());
int l = 0;
unsigned long long res = 0;
unsigned long long sum = 0;
for (int r = 0; r < v.size(); r++) {
sum += v[r].second;
while (v[l].first + d <= v[r].first) {
sum -= v[l].second;
l++;
}
res = max(sum, res);
}
cout << res;
return 0;
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
template <class T>
inline void in(T &);
template <class T>
inline void out(const T &);
using std::abs;
using std::max;
using std::min;
using std::sort;
using namespace std;
int t, n, s1, s2;
int main() {
in(t);
while (t--) {
in(n);
s1 = s2 = 0;
while (n % 2 == 0) n /= 2, s1++;
while (n % 3 == 0) n /= 3, s2++;
if (n == 1 && s1 <= s2)
out(2 * s2 - s1);
else
out(-1);
putchar('\n');
}
return 0;
}
template <class T>
void in(T &Re) {
T k = 0;
char ch = getchar();
int flag = 1;
while (!(ch >= '0' && ch <= '9')) {
if (ch == '-') {
flag = -1;
}
ch = getchar();
}
while ((ch >= '0' && ch <= '9')) {
k = (k << 1) + (k << 3) + ch - '0';
ch = getchar();
}
Re = flag * k;
}
template <class T>
void out(const T &Wr) {
if (Wr < 0) {
putchar('-');
out(-Wr);
} else {
if (Wr < 10) {
putchar(Wr + '0');
} else {
out(Wr / 10);
putchar((Wr % 10) + '0');
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
map<string, set<string> > phoneBook;
cin >> n;
for (int i = 0; i < n; i++) {
string name;
int m;
string phone;
cin >> name >> m;
for (int j = 0; j < m; j++) {
cin >> phone;
phoneBook[name].insert(phone);
}
}
for (map<string, set<string> >::iterator iter = phoneBook.begin();
iter != phoneBook.end(); iter++) {
vector<string> toRemove;
for (set<string>::iterator phoneIter = iter->second.begin();
phoneIter != iter->second.end(); phoneIter++) {
for (set<string>::iterator phoneIter2 = iter->second.begin();
phoneIter2 != iter->second.end(); phoneIter2++) {
if ((*phoneIter) != (*phoneIter2)) {
string phone1 = (*phoneIter);
string phone2 = (*phoneIter2);
if (phone1.length() <= phone2.length()) {
if (phone2.substr(phone2.length() - phone1.length()) == phone1) {
toRemove.push_back(phone1);
}
}
}
}
}
for (int i = 0; i < toRemove.size(); i++) {
iter->second.erase(toRemove[i]);
}
}
cout << phoneBook.size() << endl;
for (map<string, set<string> >::iterator iter = phoneBook.begin();
iter != phoneBook.end(); iter++) {
cout << iter->first << " " << iter->second.size() << " ";
for (set<string>::iterator iter2 = iter->second.begin();
iter2 != iter->second.end(); iter2++) {
cout << (*iter2) << " ";
}
cout << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
long long ans = 1000001;
for (long long i = 0; i < n; i++) {
cout << ans << " ";
ans++;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
if (a < b) a = b;
}
template <class T>
inline void gn(T &first) {
char c, sg = 0;
while (c = getchar(), (c > '9' || c < '0') && c != '-')
;
for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
c = getchar())
first = (first << 1) + (first << 3) + c - '0';
if (sg) first = -first;
}
template <class T1, class T2>
inline void gn(T1 &x1, T2 &x2) {
gn(x1), gn(x2);
}
int power(int a, int b, int m, int ans = 1) {
for (; b; b >>= 1, a = 1LL * a * a % m)
if (b & 1) ans = 1LL * ans * a % m;
return ans;
}
unsigned long long dp[100100];
vector<int> adj[100100];
int sz[100100];
void dfs(int u, int f = 0) {
sz[u] = 1;
for (int &v : adj[u]) {
if (v == f) {
swap(v, adj[u].back());
adj[u].pop_back();
break;
}
}
vector<unsigned long long> vec;
for (int v : adj[u]) {
dfs(v, u);
vec.push_back(dp[v]);
sz[u] += sz[v];
}
sort(vec.begin(), vec.end());
vec.push_back(sz[u]);
vec.push_back(adj[u].size());
dp[u] = 0;
for (unsigned long long first : vec) dp[u] = dp[u] * 100102021 + first;
}
set<unsigned long long> s;
void redfs(int u, unsigned long long pre = 0, int psz = 0) {
vector<unsigned long long> vec;
vec.push_back(pre);
for (int v : adj[u]) vec.push_back(dp[v]);
sort(vec.begin(), vec.end());
vec.push_back(sz[u] + psz);
vec.push_back(adj[u].size() + (psz > 0));
unsigned long long ans = 0;
for (unsigned long long first : vec) ans = ans * 100102021 + first;
if (adj[u].size() + (psz > 0) < 4) s.insert(ans);
for (int v : adj[u]) {
vec.clear();
vec.push_back(pre);
int ppsz = psz + 1;
for (int w : adj[u])
if (v != w) vec.push_back(dp[w]), ppsz += sz[w];
sort(vec.begin(), vec.end());
vec.push_back(ppsz);
vec.push_back(adj[u].size() - (!psz));
unsigned long long ppre = 0;
for (unsigned long long first : vec) ppre = ppre * 100102021 + first;
redfs(v, ppre, ppsz);
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int u, v;
gn(u, v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1);
redfs(1);
cout << s.size() << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int a, b;
void precalc() {}
bool read() {
if (scanf("%d%d", &a, &b) < 2) {
return false;
}
return true;
}
void solve() {
int even = 0, odd = 0;
if (!a && !b) {
printf("NO\n");
return;
}
for (int i = 1;; i++) {
if (i % 2 == 1)
odd++;
else
even++;
if (even == a && odd == b) {
printf("YES\n");
return;
}
if (even > a || odd > b) break;
}
even = 0;
odd = 0;
for (int i = 2;; i++) {
if (i % 2 == 1)
odd++;
else
even++;
if (even == a && odd == b) {
printf("YES\n");
return;
}
if (even > a || odd > b) break;
}
printf("NO\n");
}
int main() {
precalc();
while (read()) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[5], cnt = 0;
for (int i = 0; i < 4; i++) cin >> a[i];
sort(a, a + 4);
for (int i = 0; i < 3; i++) {
if (a[i] == a[i + 1]) cnt++;
}
cout << cnt << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
map<long long int, long long int> mp;
map<long long int, long long int> cnt;
long long int arr[120005];
long long int vis[120005];
int main() {
ios_base::sync_with_stdio(0);
long long int i, j, k, m, n, c = 0, x, y;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> arr[i];
mp[arr[i]] = 1;
cnt[arr[i]]++;
}
fill(vis, vis + n + 1, 0);
for (i = 1; i <= n; i++) {
for (j = 0; j <= 30; j++) {
if ((1 << j) < arr[i])
continue;
else if ((1 << j) == arr[i]) {
if (cnt[arr[i]] > 1) {
vis[i] = 1;
}
} else if (mp[(1 << j) - arr[i]] == 1) {
if ((1 << j) - arr[i] != arr[i])
vis[i] = 1;
else if (cnt[arr[i]] > 1) {
vis[i] = 1;
}
}
}
}
for (i = 1; i <= n; i++) {
if (vis[i] == 0) c++;
}
cout << c;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
while (b > 0) {
int t = a % b;
a = b;
b = t;
}
return a;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
if (m < n - 1) {
cout << "Impossible\n";
return 0;
}
vector<pair<int, int>> edges;
for (int i = 2; i <= n; i++) edges.emplace_back(1, i);
m -= n - 1;
for (int u = 2; m > 0 && u < n; u++) {
for (int v = u + 1; v <= n; v++) {
if (gcd(u, v) == 1) {
edges.emplace_back(u, v);
if (--m == 0) break;
}
}
}
if (m == 0) {
cout << "Possible\n";
for (auto e : edges) cout << e.first << ' ' << e.second << '\n';
} else {
cout << "Impossible\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500;
int XR[N], YR[N], XB[N], YB[N], arr[N][N];
bool ccw(int i, int j, int k) {
int x1 = XR[j] - XR[i], y1 = YR[j] - YR[i], x2 = XB[k] - XR[i],
y2 = YB[k] - YR[i];
return 1LL * x1 * y2 - 1LL * x2 * y1 > 0;
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d %d", &XR[i], &YR[i]);
for (int i = 0; i < m; i++) scanf("%d %d", &XB[i], &YB[i]);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (XR[i] < XR[j]) {
for (int k = 0; k < m; k++)
if (XB[k] >= XR[i] and XB[k] < XR[j] and ccw(i, j, k)) arr[i][j]++;
arr[j][i] = -arr[i][j];
}
int ans = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
for (int k = j + 1; k < n; k++)
if (arr[i][j] + arr[j][k] + arr[k][i] == 0) ans++;
printf("%d\n", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool chmax(T& a, const T& b) {
return a < b ? (a = b, true) : false;
}
template <typename T>
bool chmin(T& a, const T& b) {
return b < a ? (a = b, true) : false;
}
int N;
const int MAX_N = 100000;
int T[MAX_N];
long long L[MAX_N], R[MAX_N];
vector<long long> VP;
unsigned long long dat[MAX_N * 3 / 64];
int main() {
scanf("%d", &N);
for (int i = (int)(0); i < (int)(N); i++) {
scanf("%d%lld%lld", &T[i], &L[i], &R[i]);
VP.push_back(L[i]);
VP.push_back(R[i] + 1);
}
sort(VP.begin(), VP.end());
VP.erase(unique(VP.begin(), VP.end()), VP.end());
if (VP.front() != 1) {
for (int i = (int)(0); i < (int)(N); i++) {
puts("1");
}
return 0;
}
for (int i = (int)(0); i < (int)(N); i++) {
int l = lower_bound(VP.begin(), VP.end(), L[i]) - VP.begin();
int r = lower_bound(VP.begin(), VP.end(), R[i] + 1) - VP.begin();
while ((l & 63) && l < r) {
int x = l / 64, y = l % 64;
if (T[i] == 1) {
dat[x] |= 1llu << y;
} else if (T[i] == 2) {
dat[x] &= ~(1llu << y);
} else {
dat[x] ^= 1llu << y;
}
l++;
}
while ((r & 63) && l < r) {
r--;
int x = r / 64, y = r % 64;
if (T[i] == 1) {
dat[x] |= 1llu << y;
} else if (T[i] == 2) {
dat[x] &= ~(1llu << y);
} else {
dat[x] ^= 1llu << y;
}
}
while (l < r) {
if (T[i] == 1) {
dat[l / 64] = ~0llu;
} else if (T[i] == 2) {
dat[l / 64] = 0;
} else {
dat[l / 64] ^= ~0llu;
}
l += 64;
}
for (int j = (int)(0); j < (int)(N); j++)
if (dat[j] != ~0llu) {
for (int k = (int)(0); k < (int)(64); k++)
if ((dat[j] >> k & 1) == 0) {
printf("%lld\n", VP[j * 64 + k]);
break;
}
break;
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
vector<int> ed[N], child[N], cc_ed[N];
bool bridge[N][N];
int tin[N], low[N], id[N], cc_sz[N], sub_sz[N];
void dfs_bridge(int u, int p, int& tm) {
low[u] = tin[u] = ++tm;
for (int i = 0; i < ed[u].size(); ++i) {
int v = ed[u][i];
if (v == p) continue;
if (tin[v] > 0) {
low[u] = min(low[u], tin[v]);
} else {
dfs_bridge(v, u, tm);
child[u].push_back(v);
low[u] = min(low[u], low[v]);
}
}
}
void dfs_cc(int u, int k) {
if (id[u]) return;
++cc_sz[k];
id[u] = k;
for (int i = 0; i < ed[u].size(); ++i) {
int v = ed[u][i];
if (!bridge[u][v]) {
dfs_cc(v, k);
}
}
}
pair<int, int> dfs(int u, int p) {
int sz = cc_sz[u];
int r = sz * sz;
for (int i = 0; i < cc_ed[u].size(); ++i) {
int v = cc_ed[u][i];
if (v == p) continue;
pair<int, int> x = dfs(v, u);
sz = sz + x.first;
r = r + cc_sz[u] * x.first + x.second;
}
return make_pair(sz, r);
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d %d", &u, &v);
ed[u].push_back(v);
ed[v].push_back(u);
}
int tm = 0;
dfs_bridge(1, 0, tm);
for (int u = 1; u <= n; ++u) {
for (int i = 0; i < child[u].size(); ++i) {
int v = child[u][i];
if (low[v] > tin[u]) {
bridge[v][u] = bridge[u][v] = 1;
}
}
}
int cc_cnt = 0;
for (int u = 1; u <= n; ++u)
if (!id[u]) {
dfs_cc(u, ++cc_cnt);
}
for (int u = 1; u <= n; ++u) {
int uid = id[u];
for (int i = 0; i < ed[u].size(); ++i) {
int v = ed[u][i];
int vid = id[v];
if (vid == uid) continue;
cc_ed[uid].push_back(vid);
cc_ed[vid].push_back(uid);
}
}
for (int u = 1; u <= cc_cnt; ++u) {
sort(cc_ed[u].begin(), cc_ed[u].end());
cc_ed[u].erase(unique(cc_ed[u].begin(), cc_ed[u].end()), cc_ed[u].end());
}
int ans = 0;
for (int root = 1; root <= cc_cnt; ++root) {
bool can[N] = {0};
can[0] = 1;
int sz = cc_sz[root];
int r = sz * sz, sum = 0;
for (int i = 0; i < cc_ed[root].size(); ++i) {
int u = cc_ed[root][i];
pair<int, int> x = dfs(u, root);
r = r + x.second;
for (int j = sum; j >= 0; --j)
if (can[j]) {
can[j + x.first] = 1;
}
sum = sum + x.first;
}
int add = 0;
for (int i = 0; i <= sum; ++i)
if (can[i]) {
int val = i * sz + i * (sum - i) + sz * (sum - i);
add = max(add, val);
}
ans = max(ans, r + add);
}
printf("%d", ans);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int str[200010];
int main() {
int n, k, nn, i, t;
while (~scanf("%d%d", &n, &k)) {
nn = n * 2;
for (i = 1; i <= nn; i++) {
str[i] = i;
}
swap(str[2], str[k + 1]);
if (k > 1 && (k + 1) % 2 == 0) {
swap(str[k], str[k + 1]);
}
swap(str[1], str[2]);
for (i = 1; i < nn; i++) {
printf("%d ", str[i]);
}
printf("%d\n", str[nn]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long modulo = 1e9 + 7;
long long mod1 = 998244353;
int main() {
long long i, j, k = 0, a, b, c, x1, y1, x2, y2, p, q, r;
cin >> x1 >> y1 >> x2 >> y2 >> j;
for (i = 0; i < j; i++) {
cin >> a >> b >> c;
p = a * x1 + b * y1 + c;
if (p > 0)
p = 1;
else
p = -1;
q = p * (a * x2 + b * y2 + c);
if (q < 0) k++;
}
cout << k;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int P = 998244353, INF = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long qpow(long long a, long long n) {
long long r = 1 % P;
for (a %= P; n; a = a * a % P, n >>= 1)
if (n & 1) r = r * a % P;
return r;
}
inline int rd() {
int first = 0;
char p = getchar();
while (p < '0' || p > '9') p = getchar();
while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar();
return first;
}
const int N = 1e6 + 50;
int n, a[N], inv[N], dp[N];
int main() {
inv[1] = 1;
for (int i = 2; i <= N - 1; ++i)
inv[i] = (long long)inv[P % i] * (P - P / i) % P;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
int s = accumulate(a + 1, a + 1 + n, 0);
dp[0] = n - 1;
for (int i = 1; i <= s - 1; ++i)
dp[i] = (n - 1) * (s + (long long)i * dp[i - 1] % P) % P * inv[s - i] % P;
for (int i = s - 1; i >= 0; --i) dp[i] = (dp[i] + dp[i + 1]) % P;
int ans = 0;
for (int i = 1; i <= n; ++i) ans = (ans + dp[a[i]]) % P;
ans = (ans - (long long)(n - 1) * dp[0]) % P;
ans = (long long)ans * inv[n] % P;
if (ans < 0) ans += P;
printf("%d\n", ans);
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, R, r;
cin >> n >> R >> r;
if (r > R || (n > 1 && (R - r) * sin(acos(-1) / n) < r - 1e-8))
printf("NO\n");
else
printf("YES\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
while (t--) {
long long int n, m, x, y, z, k, sum = 0, c1 = 0, c2 = 0, ans = 0;
cin >> n;
if (n % 2) {
cout << "YES" << endl;
cout << 1 << " ";
x = 1;
for (int i = 1; i < ((2 * n) / 2); i += 2) {
cout << x + 3 << " " << x + 4 << " ";
x += 4;
}
x = 2;
for (int i = 1; i < ((2 * n) / 2); i += 2) {
cout << x << " " << x + 1 << " ";
x += 4;
}
cout << (2 * n);
cout << endl;
} else {
cout << "NO" << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dp[110000][3];
long long dist[110000], ht[110000];
int main() {
int n;
cin >> n;
dp[0][0] = dp[0][1] = dp[0][2] = 0;
dist[0] = -1e10;
ht[0] = 0;
dist[n + 1] = 1e10;
ht[n + 1] = 0;
for (int i = 1; i <= n; i++) cin >> dist[i] >> ht[i];
for (int i = 1; i <= n; i++) {
int mx = max(dp[i - 1][0], dp[i - 1][1]);
mx = max(mx, dp[i - 1][2]);
if (dist[i] + ht[i] < dist[i + 1])
dp[i][2] = mx + 1;
else if (dist[i] + ht[i] >= dist[i + 1])
dp[i][2] = 0;
dp[i][1] = mx;
if (dist[i] - ht[i] > dist[i - 1])
dp[i][0] = max(dp[i - 1][1], dp[i - 1][0]) + 1;
if (dist[i] - ht[i] > dist[i - 1] + ht[i - 1])
dp[i][0] = max(dp[i][0], dp[i - 1][2] + 1);
}
int ans = max(dp[n][0], dp[n][1]);
cout << max(ans, dp[n][2]);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 5;
map<int, int> s[N];
int n, a[N], t[N], x[N], pool[N], mx, m;
void add(int u, int x, int val) {
while (u <= n) {
s[u][x] += val;
u += (u & -u);
}
}
int cnt(int u, int x) {
int ans = 0;
while (u) {
ans += s[u][x];
u -= (u & -u);
}
return ans;
}
int main() {
scanf("%d", &n);
for (register int i = 1; i <= n; i++)
scanf("%d%d%d", &a[i], &t[i], &x[i]), pool[i] = t[i];
sort(pool + 1, pool + n + 1);
int m = unique(pool + 1, pool + n + 1) - (pool + 1);
mx = *max_element(pool + 1, pool + m + 1);
for (register int i = 1; i <= n; i++) {
t[i] = lower_bound(pool + 1, pool + m + 1, t[i]) - pool;
if (a[i] == 1) {
add(t[i], x[i], 1);
} else if (a[i] == 2) {
add(t[i], x[i], -1);
} else if (a[i] == 3) {
printf("%d\n", cnt(t[i], x[i]));
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n, a[N];
int cnt[N];
long long sc[N];
long long sum;
long long divx(long long x, long long y) { return x / y + 1; }
long long ans;
int main() {
scanf("%d", &n);
for (int i = 1, B = n; i <= B; i++) scanf("%d", &a[i]), cnt[a[i]]++;
sort(a + 1, a + n + 1);
for (int i = 1, B = 1e6; i <= B; i++) sc[i] = sc[i - 1] + cnt[i];
for (int y = 2, B = 5e5; y <= B; y++) {
sum = 0;
for (int i = 1, B = 5e5 / y; i <= B; i++)
sum += (sc[i * y + y - 1] - (i == 0 ? 0 : sc[i * y - 1])) * i;
if (sum == 0) continue;
long long x = 0;
for (long long l = 1, r = 5e5; l <= r;) {
long long m = l + r >> 1;
if (m <= sum - 2 * divx(m, y) + 2)
x = m, l = m + 1;
else
r = m - 1;
}
x = min(x, (long long)a[n - 1]);
for (int i = 0, B = 2; i <= B; i++) {
if (x <= 1) break;
int cnt1 = 0;
for (int j = x / y, B = 5e5 / y; j <= B; j++)
cnt1 += sc[j * y + y - 1] - sc[j * y + x % y - 1];
long long t = sum - 2 * divx(x, y) + min(2, cnt1);
if (x <= t) ans = max(ans, (long long)y * x);
x--;
}
}
for (int y = 2, B = 5e5; y <= B; y++) {
sum = 0;
for (int i = 1, B = 5e5 / y; i <= B; i++)
sum += (sc[i * y + y - 1] - (i == 0 ? 0 : sc[i * y - 1])) * i;
if (sum == 0) continue;
long long x = 0;
for (long long l = 1, r = 5e5; l <= r;) {
long long m = l + r >> 1;
if (m <= sum - divx(2 * m, y) + 1)
x = m, l = m + 1;
else
r = m - 1;
}
x = min(x, (long long)a[n] / 2);
for (int i = 0, B = 2; i <= B; i++) {
if (x <= 1) break;
int cnt1 = 0;
long long c = 2 * x % y;
for (int j = 2 * x / y, B = 5e5 / y; j <= B; j++)
cnt1 += sc[j * y + y - 1] - sc[j * y + c - 1];
long long t = sum - divx(2 * x, y) + min(1, cnt1);
if (x <= t) ans = max(ans, (long long)y * x);
x--;
}
}
printf("%I64d\n", ans);
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const double Pi = acos(-1);
bool inline equ(double a, double b) { return fabs(a - b) < EPS; }
int _R(int &x) { return scanf("%d", &x); }
int _R(long long &x) { return scanf("%" PRId64, &x); }
int _R(double &x) { return scanf("%lf", &x); }
int _R(char *s) { return scanf("%s", s); }
int _R(char c) { return scanf("%c", &c); }
int R() { return 0; }
template <typename T, typename... U>
int R(T &x, U &...tail) {
return _R(x) + R(tail...);
}
template <class T>
void _W(const T &x) {
cout << x;
}
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%" PRId64, x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T>
void _W(const vector<T> &x) {
for (auto i = x.begin(); i != x.end(); _W(*i++))
if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &...tail) {
_W(head);
putchar(sizeof...(tail) ? ' ' : '\n');
W(tail...);
}
const int L = 111;
const int K = 66;
int r, c, k, cntR;
char ip[L][L];
int ans[L][L];
inline int need(int i) { return cntR / k + (i <= (cntR % k) ? 1 : 0); }
inline char sym(int i) {
if (i <= 26) return 'A' + i - 1;
if (i <= 52) return 'a' + i - 27;
return '0' + i - 53;
}
void main2() {
R(r, c, k);
cntR = 0;
for (int i = (1); i <= (r); i++) {
scanf("%s", ip[i] + 1);
for (int j = (1); j <= (c); j++) {
if (ip[i][j] == 'R') cntR++;
}
}
int cur = 1, cnt = 0;
for (int i = (1); i <= (r); i++) {
if (i % 2) {
for (int j = (1); j <= (c); j++) {
if (ip[i][j] == 'R') ++cnt;
ans[i][j] = cur;
if (cnt == need(cur) && cur < k) {
++cur;
cnt = 0;
}
}
} else {
for (int j = (c); j >= (1); j--) {
if (ip[i][j] == 'R') ++cnt;
ans[i][j] = cur;
if (cnt == need(cur) && cur < k) {
++cur;
cnt = 0;
}
}
}
}
for (int i = (1); i <= (r); i++) {
for (int j = (1); j <= (c); j++) {
putchar(sym(ans[i][j]));
}
putchar('\n');
}
}
int main() {
int tc;
R(tc);
for (int _ = 0; _ < (tc); _++) main2();
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline bool SR(int &x) { return scanf("%d", &x) == 1; }
inline bool SR(long long &x) { return scanf("%lld", &x) == 1; }
inline bool SR(double &x) { return scanf("%lf", &x); }
inline bool SR(char *s) { return scanf("%s", s); }
inline bool RI() { return 1; }
template <typename I, typename... T>
bool RI(I &x, T &...tail) {
return SR(x) && RI(tail...);
}
inline void SP(const int x) { printf("%d", x); }
inline void SP(const long long x) { printf("%lld", x); }
inline void SP(const double x) { printf("%.10f", x); }
inline void SP(const char *s) { printf("%s", s); }
inline void PL() { puts(""); }
template <typename I, typename... T>
void PL(const I x, const T... tail) {
SP(x);
if (sizeof...(tail)) putchar(' ');
PL(tail...);
}
template <typename I>
inline void _DOING(const char *s, I &&x) {
cerr << s << " = " << x << '\n';
}
template <typename I, typename... T>
void _DOING(const char *s, I &&x, T &&...tail) {
int c = 0;
while (*s != ',' || c != 0) {
if (*s == '(' || *s == '[' || *s == '{') c++;
if (*s == ')' || *s == ']' || *s == '}') c--;
cerr << *s++;
}
cerr << " = " << x << " , ";
_DOING(s + 1, tail...);
}
const int INF = 0x3f3f3f3f;
const long long INF64 = 0x3f3f3f3f3f3f3f3fll;
inline int RAND() {
static int x = 880301;
return x = (x * 0xdefacedll + 1ll) % 0x7fffffffll;
}
const int MAX_N = 100 + 7;
int main() {
int n;
RI(n);
if (n & 1) {
PL(-1);
return 0;
}
int cnt = 0, res = 0;
for (int i = 0; i < int(n); i++) {
for (int j = 0; j < int(n); j++) {
for (int k = 0; k < int(n >> 1); k++) {
if (cnt & 1)
cout << "bb";
else
cout << "ww";
cnt++;
}
if ((n >> 1) % 2 == 0) cnt++;
cout << '\n';
}
res++;
if (res % 2 == 0) cnt++;
cout << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<string> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
arr[i] = "#" + arr[i];
}
int dp[n + 1][m + 1];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
char x = arr[i][j];
if (i > 1 && x == arr[i - 1][j - 1] && x == arr[i - 1][j] &&
x == arr[i - 1][j + 1] && x == arr[i - 2][j])
dp[i][j] =
min(dp[i - 1][j - 1], min(dp[i - 2][j], dp[i - 1][j + 1])) + 1;
else
dp[i][j] = 1;
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
ans += dp[i][j];
}
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
int ans, n, i, r[22], b[22], j, k, dp[65555][130], sumr, sumb;
char ss[22][22];
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
int main() {
scanf("%d\n", &n);
for (i = 0; i < n; i++) {
scanf("%s", ss[i]);
scanf("%d%d\n", &r[i], &b[i]);
sumr += r[i];
sumb += b[i];
}
ans = 1111111111;
int s = 1 << n;
memset(dp, -1, sizeof(dp));
dp[0][0] = 0;
for (i = 0; i < s; i++)
for (j = 0; j <= n * n / 2; j++)
if (dp[i][j] != -1) {
int A = 0, B = 0;
for (k = 0; k < n; k++)
if (i & (1 << k)) {
if (ss[k][0] >= 'R')
A++;
else
B++;
}
for (k = 0; k < n; k++)
if ((i & (1 << k)) == 0) {
int costr = min(r[k], A);
int costb = min(b[k], B);
dp[i + (1 << k)][j + costr] =
max(dp[i + (1 << k)][j + costr], dp[i][j] + costb);
}
if (i == s - 1) {
ans = min(ans, max(sumr - j, sumb - dp[i][j]));
}
}
printf("%d\n", n + ans);
}
| 8 |
#include <iostream>
using namespace std;
int n, k;
string s;
int dp[(1 << 18) - 1];
int pos[200010][18], tr[200010][18];
bool check(int x) {
//cout << "X = " << x << endl;
for (int i = 0; i < (1 << k); ++i) dp[i] = n;
for (int i = n - 1; i >= 0; --i) {
for (int j = 0; j < k; ++j) {
if (s[i] == '?') {
pos[i][j] = (i + 1 >= n) ? 1: (pos[i + 1][j] + 1);
} else {
pos[i][s[i] - 'a'] = (i + 1 >= n) ? 1: (pos[i + 1][s[i] - 'a'] + 1);
}
}
}
/*for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
//cout << pos[i][j] << " ";
}
cout << endl;
}*/
for (int i = n - 1; i >= 0; --i) for (int j = 0; j < k; ++j) tr[i][j] = n;
for (int i = n - 1; i >= 0; --i) {
for (int j = 0; j < k; ++j) {
if (pos[i][j] >= x) {
tr[i][j] = i + x - 1;
} else {
if (i + 1 < n) tr[i][j] = tr[i + 1][j];
}
}
}
/*for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
cout << tr[i][j] << " ";
}
cout << endl;
}*/
dp[0] = -1;
for (int j = 0; j < (1 << k); ++j) {
for (int i = 0; i < k; ++i) {
int cu = j;
int ne = (j | (1 << i));
if (dp[cu] + 1 >= n) continue;
int y = tr[dp[cu] + 1][i];
dp[ne] = min(dp[ne], y);
//cout << cu << " " << ne << " " << dp[ne] << endl;
}
}
/*for (int i = 0; i < (1 << k); ++i) {
cout << dp[i] << " ";
}
cout << endl;*/
return (dp[(1 << k) - 1] < n);
}
int main() {
cin >> n >> k;
cin >> s;
int hi = n, lo = 1;
while (lo <= hi) {
//cout << "HI = " << hi << " " << "LO = " << lo << endl;
int mi = (hi + lo) / 2;
if (check(mi)) {
lo = mi + 1;
} else {
hi = mi - 1;
}
}
cout << (lo - 1) << endl;
} | 8 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& output, vector<T>& v) {
output << "[ ";
if (int(v.size())) {
output << v[0];
}
for (int i = 1; i < int(v.size()); i++) {
output << ", " << v[i];
}
output << " ]";
return output;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& output, pair<T1, T2>& p) {
output << "( " << p.first << ", " << p.second << " )";
return output;
}
template <typename T>
ostream& operator,(ostream& output, T x) {
output << x << " ";
return output;
}
int bitMat[32][100007];
int comuZeroBit[32][100007];
vector<pair<int, int> > v[32];
int main() {
int n, m;
int l, r, q;
cin >> n >> m;
for (int tmp = 0; tmp < m; tmp++) {
cin >> l >> r >> q;
for (int i = 0; i < 32; i++) {
if ((q & (1 << i))) {
bitMat[i][l]++;
bitMat[i][r + 1]--;
} else {
v[i].push_back(make_pair(l, r));
}
}
}
for (int i = 0; i < 32; i++) {
int sum = 0;
for (int j = 1; j <= n; j++) {
sum += bitMat[i][j];
if (sum > 0) {
bitMat[i][j] = 1;
} else {
comuZeroBit[i][j] = 1;
}
comuZeroBit[i][j] += comuZeroBit[i][j - 1];
}
}
bool pos = 1;
for (int i = 0; i < 32; i++) {
for (int j = 0; j < int(v[i].size()); j++) {
l = v[i][j].first;
r = v[i][j].second;
if ((comuZeroBit[i][r] - comuZeroBit[i][l - 1]) == 0) {
pos = false;
}
}
}
if (pos) {
cout << "YES" << endl;
for (int i = 1; i <= n; i++) {
int num = 0;
for (int j = 0; j < 32; j++) {
if (bitMat[j][i] == 1) {
num = num | (1 << j);
}
}
cout << num << ' ';
}
} else {
cout << "NO" << endl;
}
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
enum { WHITE, GREY, BLACK };
using namespace std;
long long int inf = 1e18;
template <class T>
T gcd(T a, T b) {
return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <typename T>
T last_bit(T n) {
return n & 1;
}
template <class T>
T big_mod(T n, T p, T m) {
if (p == 0) return (T)1;
T x = big_mod(n, p / 2, m);
x = (x * x) % m;
if (p & 1) x = (x * n) % m;
return x;
}
template <class T>
T modInv(T a, T m) {
T x, y;
extgcd(a, m, x, y);
x %= m;
while (x < 0) {
x += m;
}
return x;
}
template <class T>
T extgcd(T a, T b, T& x, T& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
int g = extgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
}
template <class T>
T multiplication(T n, T p, T m) {
if (p == 0) return (T)0;
T x = multiplication(n, p / 2, m);
x = (x + x) % m;
if (p & 1) x = (x + n) % m;
return x;
}
template <class T>
T my_pow(T n, T p) {
if (p == 0) return 1;
T x = my_pow(n, p / 2);
x = (x * x);
if (p & 1) x = (x * n);
return x;
}
template <class T>
double getdist(T a, T b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
template <class T>
T extract(string s, T ret) {
stringstream ss(s);
ss >> ret;
return ret;
}
template <class T>
string tostring(T n) {
stringstream ss;
ss << n;
return ss.str();
}
template <class T>
T Mod(T n, T m) {
return (n % m + m) % m;
}
template <class T>
T MIN3(T a, T b, T c) {
return min(a, min(b, c));
}
template <class T>
T MAX3(T a, T b, T c) {
return max(a, max(b, c));
}
template <class T>
void print_vector(T& v) {
int sz = v.size();
if (sz) cout << v[0];
for (int i = 1; i < sz; i++) cout << ' ' << v[i];
cout << "\n";
}
bool isVowel(char ch) {
ch = toupper(ch);
if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E')
return true;
return false;
}
bool isConsonant(char ch) {
if (isalpha(ch) && !isVowel(ch)) return true;
return false;
}
template <class R>
R Josephus(R n, R k) {
R ans = 1;
for (R i = 2; i <= n; i++) ans = (ans + k - 1) % i + 1;
return ans;
}
template <class R>
R toitent_Phi2(R a) {
R result = a;
for (R i = 2; i * i <= a; i++) {
if (a % i == 0) result = result - result / i;
while (a % i == 0) a = a / i;
}
if (a > 1) result = result - result / a;
return result;
}
template <typename T>
T Angle(T x1, T y1, T x2, T y2) {
return atan(double(y1 - y2) / double(x1 - x2));
}
long long int track1[1009];
long long int track2[1009];
int main() {
cin.sync_with_stdio(false);
cin.tie();
;
long long int T, t;
long long int i, j, k;
long long int n, m, q;
scanf("%lld", &n);
scanf("%lld", &q);
long long int revQ = n - q + 1;
for (i = 1; i <= n; i++) {
scanf("%lld", &track1[i]);
}
for (i = 1; i <= n; i++) {
scanf("%lld", &track2[i]);
}
if (track1[1] == 0) {
printf("NO\n");
return 0;
} else if (track1[q] == 1) {
printf("YES\n");
return 0;
} else if (track1[q] == 0 && track2[q] == 0) {
printf("NO\n");
return 0;
} else {
for (i = q + 1; i <= n; i++) {
if (track1[i] == 1 && track2[i] == 1) {
printf("YES\n");
return 0;
}
}
printf("NO\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1500;
vector<int> ans;
int n, a[N][6];
int mul(int i, int j, int k) {
int tmp = 0;
for (int x = 0; x < 5; x++)
tmp = tmp + (a[j][x] - a[i][x]) * (a[k][x] - a[i][x]);
return tmp;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < 5; j++) scanf("%d", &a[i][j]);
for (int i = 0; i < n; i++) {
bool flag = false;
for (int j = 0; j < n; j++)
if (i != j) {
for (int k = j + 1; k < n; k++)
if (i != k) {
int tmp = mul(i, j, k);
if (tmp > 0) {
flag = true;
break;
}
}
if (flag) break;
}
if (!flag) ans.push_back(i + 1);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) printf("%d\n", ans[i]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, hh, mm;
int h, m, c = 0;
cin >> s;
hh = s[0];
hh += s[1];
mm = s[3];
mm += s[4];
h = stoi(hh, nullptr, 10);
m = stoi(mm, nullptr, 10);
while (hh[0] != mm[1] || hh[1] != mm[0]) {
c++;
if (m < 59)
m++;
else {
m = 0;
if (h < 23)
h++;
else
h = 0;
}
if (h > 9)
hh = to_string(h);
else
hh = "0" + to_string(h);
if (m > 9)
mm = to_string(m);
else
mm = "0" + to_string(m);
}
printf("%d", c);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(std::chrono::system_clock::now().time_since_epoch().count());
const int N = 1000;
int n, m, dis[N][N], rem, s, b, k, h, x[N], a[N], f[N], x1[N], d[N],
last[2 * N + 2], last1[2 * N + 2], cost[2 * N + 2], q[2 * N + 2];
struct edge {
int u, v, c, last;
} e[3 * N * N];
void addEdge(int u, int v) {
e[rem].u = u;
e[rem].v = v;
e[rem].c = 1;
e[rem].last = last[u];
last[u] = rem++;
e[rem].u = v;
e[rem].v = u;
e[rem].c = 0;
e[rem].last = last[v];
last[v] = rem++;
}
bool getPath() {
memset(cost, -1, sizeof cost);
cost[0] = 0;
int l = 0, r = 0;
while (l <= r) {
int x = q[l++];
for (int cur = last[x]; cur != -1; cur = e[cur].last)
if (e[cur].c && cost[e[cur].v] == -1) {
cost[e[cur].v] = cost[x] + 1;
q[++r] = e[cur].v;
}
}
return cost[s + b + 1] != -1;
}
bool dfs(int u) {
if (u == s + b + 1) return 1;
for (int &cur = last1[u]; cur != -1; cur = e[cur].last) {
if (e[cur].c && cost[e[cur].v] == cost[u] + 1 && dfs(e[cur].v)) {
e[cur].c = 0;
e[cur ^ 1].c = 1;
return 1;
}
}
return 0;
}
int Maxflow() {
int flow = 0;
while (getPath()) {
for (int i = 0; i < 2 * N + 2; i++) last1[i] = last[i];
while (dfs(0)) flow++;
}
return flow;
}
int main() {
scanf("%d%d", &n, &m);
memset(last, -1, sizeof last);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (i != j) dis[i][j] = 1e9;
for (int i = 0, a, b; i < m; i++) {
scanf("%d%d", &a, &b);
dis[a - 1][b - 1] = dis[b - 1][a - 1] = 1;
}
for (int k = 0; k < n; k++)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
scanf("%d%d%d%d", &s, &b, &k, &h);
for (int i = 0; i < s; i++) {
addEdge(0, i + 1);
scanf("%d%d%d", x + i, a + i, f + i);
}
for (int i = 0; i < b; i++) {
addEdge(s + i + 1, s + b + 1);
scanf("%d%d", x1 + i, d + i);
for (int j = 0; j < s; j++)
if (dis[x[j] - 1][x1[i] - 1] <= f[j] && a[j] >= d[i])
addEdge(j + 1, i + s + 1);
}
printf("%lld\n", min(Maxflow() * 1LL * k, h * 1LL * s));
}
| 7 |
#include <bits/stdc++.h>
int a[10005];
int ck[10005];
int main() {
int N, cnt, idx, max;
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d", &a[i]);
ck[i] = 1;
}
idx = 1;
cnt = 0;
max = 1;
while (idx <= N) {
int tmp = idx;
while (ck[tmp]) {
ck[tmp] = 0;
tmp = a[tmp];
if (tmp > max) max = tmp;
}
while (!ck[idx] && idx <= N) idx++;
if (idx == max + 1) cnt++;
}
printf("%d\n", cnt);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long ipow(long long a, long long n) {
long long k = 1;
for (long long i = 0; i < n; i++) k *= a;
return k;
}
int main() {
long long n, k;
cin >> n >> k;
long long d2 = 0, d5 = 0;
while (n % 2 == 0) {
n /= 2;
d2++;
}
while (n % 5 == 0) {
n /= 5;
d5++;
}
if (d2 < k) d2 = k;
if (d5 < k) d5 = k;
cout << n * ipow(5, d5) * ipow(2, d2);
getchar();
getchar();
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const string basicString =
"What are you doing at the end of the world? Are you busy? Will you save "
"us?";
const vector<string> queryString = {
string("What are you doing while sending \""),
string("\"? Are you busy? Will you send \""), string("\"?")};
const auto allForCheck = queryString[0] + basicString + queryString[1] +
basicString + queryString[2];
const std::uint64_t toAdd =
queryString[0].size() + queryString[1].size() + queryString[2].size();
const int SIZE = 56;
const int MAX_SIZE = 100001;
long long int sCount[MAX_SIZE];
char GetChar(const std::uint64_t strNum, const std::uint64_t sNum) {
if (strNum == 0) return basicString[sNum - 1];
if (queryString[0].size() >= sNum) {
return queryString[0][sNum - 1];
}
long long int c = (long long int)(queryString[0].size()) + sCount[strNum - 1];
long long int c1 = c + queryString[1].size();
if (c < sNum && sNum <= c1) return queryString[1][sNum - 1 - c];
if (sCount[strNum] == sNum) return '?';
if (sCount[strNum] - 1 == sNum) return '"';
if (queryString[0].size() < sNum && c >= sNum) {
return GetChar(strNum - 1, sNum - queryString[0].size());
}
return GetChar(strNum - 1, sNum - c1);
}
int main() {
std::uint64_t querySize = 0;
cin >> querySize;
sCount[0] = basicString.size();
for (std::uint64_t i = 1; i < SIZE; ++i) {
sCount[i] = 2 * sCount[i - 1] + toAdd;
}
for (std::uint64_t i = SIZE; i < MAX_SIZE; ++i) {
sCount[i] = std::numeric_limits<long long int>::max() - 1000;
}
for (std::uint64_t i = 0; i < querySize; ++i) {
long long int sringNum, symbolNum;
cin >> sringNum >> symbolNum;
if (symbolNum > sCount[sringNum]) {
cout << ".";
continue;
}
cout << GetChar(sringNum, symbolNum);
}
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
int *arr = new int[n];
for (int i = 0; i < n; i++) cin >> arr[i];
int minDist = INT_MAX;
for (int i = 0; i < n; i++) {
if (arr[i] > 0 && k >= arr[i] && abs(m - i - 1) * 10 < minDist &&
m != i + 1) {
minDist = abs(m - i - 1) * 10;
}
}
cout << minDist << "\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int m, n, ans, x[1010101], y[1010101], s;
void solve(long long int a, long long int b) {
map<long long int, bool> mp;
vector<long long int> v;
long long int m1, m2;
m1 = (x[a] - x[b]);
m2 = (y[a] - y[b]);
long long int c = m1 * y[a] - m2 * x[a];
for (long long int i = 1; i <= n; i++) {
if (m2 * x[i] + c != m1 * y[i]) {
v.push_back(i);
}
}
if (v.size() <= 2) {
cout << "YES" << endl;
exit(0);
}
m2 = (y[v[0]] - y[v[1]]);
m1 = (x[v[0]] - x[v[1]]);
c = m1 * y[v[0]] - m2 * x[v[0]];
s = v[0];
for (auto i : v) {
if (m2 * x[i] + c != m1 * y[i]) {
return;
}
}
cout << "YES" << endl;
exit(0);
}
int main() {
cin >> n;
for (long long int i = 1; i <= n; i++) {
cin >> x[i] >> y[i];
}
solve(1ll, 2ll);
solve(1ll, 3ll);
solve(2ll, 3ll);
cout << "NO" << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int x, n, ans = INT_MAX;
struct node {
int nxt, eid;
};
vector<node> edges[maxn];
map<pair<int, int>, int> mp;
bool vis[maxn];
int dis[maxn];
queue<int> q;
void Div(int x, int id) {
int p = 1, q = 1;
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
int num = 0;
while (x % i == 0) num++, x /= i;
if (num % 2) {
if (p == 1)
p = i;
else
q = i;
}
}
}
if (x != 1) {
if (p == 1)
p = x;
else
q = x;
}
if (p == 1 && q == 1) {
ans = min(ans, 1);
return;
}
mp[{p, q}]++;
if (mp[{p, q}] >= 2) {
ans = min(ans, 2);
return;
}
edges[q].push_back({p, id});
edges[p].push_back({q, id});
}
int bfs(int s) {
if (edges[s].empty()) return INT_MAX;
memset(vis, false, sizeof vis);
memset(dis, 0, sizeof dis);
while (!q.empty()) q.pop();
q.push(s);
dis[s] = 1;
while (!q.empty()) {
int now = q.front();
q.pop();
for (int i = 0; i < edges[now].size(); i++) {
int nxt = edges[now][i].nxt, eid = edges[now][i].eid;
if (vis[eid]) continue;
if (dis[nxt]) return dis[now] + dis[nxt] - 1;
vis[eid] = true;
q.push(nxt);
dis[nxt] = dis[now] + 1;
}
}
return INT_MAX;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
Div(x, i);
}
if (ans != INT_MAX)
printf("%d\n", ans);
else {
for (int i = 1; i <= 1000; i++) ans = min(ans, bfs(i));
if (ans == INT_MAX) ans = -1;
printf("%d\n", ans);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));
using namespace std;
struct mint {
long long x;
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(long long t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
mint inv() const { return pow(mod - 2); }
mint& operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
mint dp[2][4001];
void init(int x) {
for (int i = 0; i <= 4000; i++) dp[x][i] = 0;
}
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
mint COM(int n, int k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return mint(fac[n] * (finv[k] * finv[n - k] % mod) % mod);
}
int main() {
COMinit();
long long n, k;
scanf("%lld %lld", &n, &k);
vector<long long> h(n);
for (int i = 0; i < n; i++) scanf("%lld", &h[i]);
int cnt = 0;
for (int i = 0; i < n; i++) {
if (h[i] != h[(i + 1) % n]) {
cnt++;
}
}
mint res = 0;
vector<mint> k2(n + 1);
k2[0] = 1;
for (int i = 1; i <= n; i++) k2[i] = k2[i - 1] * (k - 2);
vector<mint> tw(n + 1);
tw[0] = 1;
for (int i = 1; i <= n; i++) tw[i] = tw[i - 1] * 2;
mint tinv = mint(2).inv();
for (int i = 0; i < cnt; i++) {
int r = cnt - i;
if (r & 1)
res += COM(cnt, i) * k2[i] * tw[r - 1];
else
res += COM(cnt, i) * k2[i] * (tw[r] - COM(r, r / 2)) * tinv;
}
res *= mint(k).pow(n - cnt);
cout << res.x << "\n";
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[100001];
int visited[100001] = {0}, parent[100001] = {0};
int dfs(int s, int p) {
visited[s] = 1;
int f = 1;
for (int j = 0; j < graph[s].size(); j++)
if (!visited[graph[s][j]]) {
parent[graph[s][j]] = s;
f = min(f, dfs(graph[s][j], p));
} else if (parent[s] != graph[s][j]) {
f = 0;
}
return f;
}
int main() {
int n, m, ans = 0, i, x, y;
cin >> n >> m;
for (i = 0; i < m; i++) {
cin >> x >> y;
graph[x].push_back(y);
graph[y].push_back(x);
}
for (i = 1; i <= n; i++)
if (!visited[i]) {
parent[i] = -1;
ans += dfs(i, i);
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a[2000005], ps = 0;
pair<int, int> s[1000005];
int main() {
scanf("%d", &n);
int mx = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
if (a[i] > a[mx]) mx = i;
}
for (int i = 1; i <= mx; i++) a[n + i] = a[i];
s[ps] = make_pair(a[mx], 1);
long long ans = 0;
for (int i = mx + 1; i < mx + n; i++) {
while (a[i] > s[ps].first) {
ans +=
(long long)s[ps].second * (s[ps].second - 1) / 2 + s[ps].second * 2;
ps--;
}
if (a[i] == s[ps].first)
s[ps].second++;
else {
s[++ps] = make_pair(a[i], 1);
}
}
for (; ps; ps--) {
ans += (long long)s[ps].second * (s[ps].second - 1) / 2 +
s[ps].second * (2 - (ps == 1 && s[0].second == 1));
}
ans += (long long)s[0].second * (s[0].second - 1) / 2;
cout << ans << endl;
return 0;
}
| 8 |