solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
bool a[501][501];
bool hasEdge(char x, char y) { return (x == 'b') || (y == 'b') || (x == y); }
int main() {
int n, m, x, y;
cin >> n >> m;
while (m--) {
cin >> x >> y;
a[x][y] = a[y][x] = true;
}
vector<char> s(n + 1, 'b');
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j) {
continue;
}
if (a[i][j] != hasEdge(s[i], s[j])) {
if (s[i] != 'b' && s[j] != 'b') {
cout << "No";
return 0;
}
if (s[i] == 'b') {
if (s[j] == 'a') {
s[i] = 'c';
} else {
s[i] = 'a';
}
}
if (s[j] == 'b') {
if (s[i] == 'a') {
s[j] = 'c';
} else {
s[j] = 'a';
}
}
}
}
}
cout << "Yes" << endl;
for (int i = 1; i <= n; i++) {
cout << s[i];
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
if (n < 6) {
printf("-1\n");
} else {
printf("1 2\n");
printf("2 3\n");
printf("2 4\n");
for (int i = 5; i <= n; i++) {
printf("3 %d\n", i);
}
}
for (int i = 1; i < n; i++) {
printf("%d %d\n", i, i + 1);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7f7f7f7f;
int BigMod(long long int B, long long int P, long long int M) {
long long int R = 1;
while (P > 0) {
if (P % 2 == 1) R = (R * B) % M;
P /= 2;
B = (B * B) % M;
}
return (int)R;
}
long long int maxDivide(long long int a, long long int b) {
while (a % b == 0) a = a / b;
return a;
}
long long int isUgly(long long int no) {
no = maxDivide(no, 2);
no = maxDivide(no, 3);
no = maxDivide(no, 5);
return (no == 1) ? 1 : 0;
}
long long int myXOR(long long int x, long long int y) {
long long int res = 0;
for (long long int i = 31; i >= 0; i--) {
bool b1 = x & (1 << i);
bool b2 = y & (1 << i);
bool xoredBit = (b1 & b2) ? 0 : (b1 | b2);
res <<= 1;
res |= xoredBit;
}
return res;
}
long long int ar[100005];
int main() {
long long int x, n, m;
cin >> n;
for (long long int i = 0; i <= n - 1; i++) {
cin >> x;
ar[i] = x;
}
sort(ar, ar + n);
m = 0;
for (long long int i = 0; i <= n - 3; i++) {
if (ar[i] + ar[i + 1] > ar[i + 2]) {
m = 1;
break;
}
}
if (m == 1)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > adj;
int mul(int a, int b) { return 1ll * a * b % 998244353; }
int add(int a, int b) {
return a + b >= 998244353 ? (a + b - 998244353) : (a + b);
}
int sub(int a, int b) { return a >= b ? a - b : (a + 998244353 - b); }
int mem[200010][3];
int dp(int u, int type) {
if (mem[u][type] != -1) return mem[u][type];
vector<int> ALL;
vector<int> ONE;
vector<int> TWO;
int S;
for (int v : adj[u]) {
ALL.push_back(dp(v, 0));
ONE.push_back(dp(v, 1));
}
S = ALL.size();
if (S == 0) {
return mem[u][type] = 1;
}
int zero = 1;
for (int i = 0; i < S; ++i) {
zero = mul(zero, ALL[i]);
}
int all = 1;
for (int i = 0; i < ONE.size(); ++i) {
all = add(mul(all, ONE[i]), mul(all, ALL[i]));
}
int one = 0;
{
vector<int> C(S);
for (int i = S - 1; i >= 0; --i) {
if (i + 1 == S) {
C[i] = ALL[i];
} else {
C[i] = mul(ALL[i], C[i + 1]);
}
}
int f = 1;
for (int i = 0; i < S; ++i) {
int t = f;
if (i + 1 < S) {
t = mul(t, C[i + 1]);
}
one = add(one, mul(ONE[i], t));
f = mul(f, ALL[i]);
}
}
if (type == 1) {
return mem[u][type] = sub(all, zero);
} else if (type == 0) {
return mem[u][type] = sub(all, one);
}
}
int main() {
memset(mem, -1, sizeof(mem));
int N;
scanf("%d", &N);
adj = vector<vector<int> >(N);
for (int i = 1; i < N; ++i) {
int p;
scanf("%d", &p);
p--;
adj[p].push_back(i);
}
cout << dp(0, 0) << endl;
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a[6] = {4, 5, 6, 3, 2, 1};
int main() {
ios::sync_with_stdio(false);
long long n;
long long ans = 0;
char c;
cin >> n >> c;
n--;
ans += (n / 4) * 16;
n %= 4;
ans += (n % 2) * 7;
ans += a[c - 'a'];
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
namespace modular {
template <typename>
struct is_modular : std::false_type {};
template <int M>
struct static_mint {
static_assert(0 < M, "Module must be positive");
using mint = static_mint;
int val;
static_mint() : val() {}
static_mint(long long x) : val(x % M) {
if (val < 0) val += M;
}
mint pow(long long n) const {
mint ans = 1, x(*this);
while (n) {
if (n & 1) ans *= x;
x *= x;
n /= 2;
}
return ans;
}
mint inv() const { return pow(M - 2); }
friend mint pow(const mint& m, long long n) { return m.pow(n); }
friend mint inv(const mint& m) { return m.inv(); }
mint operator+() const {
mint m;
m.val = val;
return m;
}
mint operator-() const {
mint m;
m.val = M - val;
return m;
}
mint& operator+=(const mint& m) {
if ((val += m.val) >= M) val -= M;
return *this;
}
mint& operator-=(const mint& m) {
if ((val -= m.val) < 0) val += M;
return *this;
}
mint& operator*=(const mint& m) {
val = (long long)val * m.val % M;
return *this;
}
mint& operator/=(const mint& m) {
val = (long long)val * m.inv().val % M;
return *this;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs.val == rhs.val;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs.val != rhs.val;
}
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
mint operator++(int) {
mint result(*this);
*this += 1;
return result;
}
mint operator--(int) {
mint result(*this);
*this -= 1;
return result;
}
template <typename T>
explicit operator T() const {
return T(val);
}
friend std::ostream& operator<<(std::ostream& os, const mint& m) {
return os << m.val;
}
friend std::istream& operator>>(std::istream& is, mint& m) {
long long x;
is >> x;
m = x;
return is;
}
};
template <int M>
struct is_modular<static_mint<M>> : std::true_type {};
template <int& _M>
struct dynamic_mint {
static constexpr int& M = _M;
using mint = dynamic_mint;
int val;
dynamic_mint() : val() {}
dynamic_mint(long long x) : val(x % M) {
if (val < 0) {
val += M;
}
}
mint pow(long long n) const {
mint ans = 1, x(*this);
while (n) {
if (n & 1) ans *= x;
x *= x;
n /= 2;
}
return ans;
}
mint inv() const { return pow(M - 2); }
friend mint pow(const mint& m, long long n) { return m.pow(n); }
friend mint inv(const mint& m) { return m.inv(); }
mint operator+() const {
mint m;
m.val = val;
return m;
}
mint operator-() const {
mint m;
m.val = M - val;
return m;
}
mint& operator+=(const mint& m) {
if ((val += m.val) >= M) val -= M;
return *this;
}
mint& operator-=(const mint& m) {
if ((val -= m.val) < 0) val += M;
return *this;
}
mint& operator*=(const mint& m) {
val = (long long)val * m.val % M;
return *this;
}
mint& operator/=(const mint& m) {
val = (long long)val * m.inv().val % M;
return *this;
}
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint& lhs, const mint& rhs) {
return lhs.val == rhs.val;
}
friend bool operator!=(const mint& lhs, const mint& rhs) {
return lhs.val != rhs.val;
}
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
mint operator++(int) {
mint result(*this);
*this += 1;
return result;
}
mint operator--(int) {
mint result(*this);
*this -= 1;
return result;
}
template <typename T>
explicit operator T() const {
return T(val);
}
friend std::ostream& operator<<(std::ostream& os, const mint& m) {
return os << m.val;
}
friend std::istream& operator>>(std::istream& is, mint& m) {
long long x;
is >> x;
m = x;
return is;
}
};
} // namespace modular
using im = modular::static_mint<998244353>;
im p[2][100001];
im get(int i, int l, int r) {
if (r < 0) return im(0);
l = max(l, 0);
return p[i][r] - (l == 0 ? 0 : p[i][l - 1]);
}
void solution() {
int n, m;
cin >> n >> m;
vector<im> f(m + 1);
vector<int> l(n), r(n);
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
}
for (int d = 1; d <= m; d++) {
vector<int> L(n), R(n);
bool glitch = false;
for (int i = 0; i < n; i++) {
L[i] = (l[i] + d - 1) / d;
R[i] = r[i] / d;
if (R[i] < L[i]) glitch = true;
}
if (glitch) continue;
int bound = m / d;
for (int i = 0; i <= 1; i++)
for (int j = 0; j <= bound; j++) p[i][j] = 0;
p[0][0] = 1;
for (int i = 1; i <= bound; i++) p[0][i] = 1;
int cur = 0, nxt = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= bound; j++)
p[nxt][j] += get(cur, j - R[i], j - L[i]);
for (int j = 1; j <= bound; j++) p[nxt][j] += p[nxt][j - 1];
for (int j = 0; j <= bound; j++) p[cur][j] = 0;
swap(cur, nxt);
}
f[d] = p[cur][bound];
}
for (int i = m; i >= 1; i--) {
for (int j = i + i; j <= m; j += i) f[i] -= f[j];
}
cout << f[1];
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
while (tc--) {
solution();
cout << "\n";
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
char str[N];
int n;
int main() {
scanf("%s", str);
n = strlen(str);
str[n] = 'b';
for (int i = 0; i < n; ++i) {
printf("%d%c", str[i] != str[i + 1], " \n"[i + 1 == n]);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<long long, long long> M;
long long dfs(long long x) {
map<long long, long long>::iterator IT = M.find(x);
if (IT != M.end()) return M[x];
long long k = sqrt(x);
long long tot = x - 1;
for (long long i = 2; i <= k; i++) {
if (x % i == 0)
tot -= dfs(x / i);
else
continue;
if (i != x / i) tot -= dfs(i);
}
M[x] = tot;
return tot;
}
int main() {
long long n, k;
cin >> n >> k;
M[1] = 1;
M[2] = 1;
long long ans = n;
for (long long i = 1; i <= k; i++) {
if (i == 1) {
ans = dfs(ans);
continue;
}
if (i % 2 == 1) {
ans = dfs(ans);
}
if (ans == 1) break;
}
cout << ans % 1000000007;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1;
vector<long long> gaus;
int ans, n;
long long temp, a, b;
void add(long long val) {
int bit = 60, lol = 0;
for (auto i : gaus) {
while (bit && !(val & (1LL << bit)) && !(i & (1LL << bit))) bit--;
if (!(i & (1LL << bit)) && (val & (1LL << bit))) {
gaus.insert(gaus.begin() + lol, val);
return;
}
if ((i & (1LL << bit)) && (val & (1LL << bit))) val ^= i;
lol++;
}
if (val)
gaus.push_back(val);
else
ans++;
}
bool check(long long val) {
int bit = 60;
for (auto i : gaus) {
while (bit && !(val & (1LL << bit)) && !(i & (1LL << bit))) bit--;
if ((i & (1LL << bit)) && (val & (1LL << bit))) val ^= i;
}
return val;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld%lld", &a, &b);
temp ^= a;
add(a ^ b);
}
if (check(temp)) return puts("1/1"), 0;
n -= ans;
printf("%lld/%lld\n", (1LL << n) - 1, (1LL << n));
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
map<int, int> mp;
int x, y;
cin >> x >> y;
for (int i = 1; i <= y; ++i) {
int cur;
cin >> cur;
if (!mp[cur % x])
++mp[cur % x];
else {
cout << i << endl;
return;
}
}
cout << -1 << endl;
return;
}
int main() {
int t = 1;
while (t--) solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int len, i, b[100500], n, mx = -1, j;
char a[100500];
bool was[100500];
int main() {
scanf("%s", a + 1);
a[0] = '@';
len = strlen(a) - 1;
for (i = 1; i <= len; i++) b[i] = i;
if (len <= 0) {
printf("0");
exit(0);
}
for (i = 1; i <= len; i++) {
if (a[i] == ')' && a[b[i - 1]] == '(') {
was[b[i - 1]] = true;
was[i] = was[b[i - 1]] = true;
b[b[i - 1]] = b[b[i - 1] - 1];
b[i] = b[b[i - 1]];
}
if (a[i] == ']' && a[b[i - 1]] == '[') {
was[b[i - 1]] = true;
was[i] = was[b[i - 1]] = true;
b[b[i - 1]] = b[b[i - 1] - 1];
b[i] = b[b[i - 1]];
}
}
n = 0;
for (i = 1; i <= len; i++) {
while (was[i] == true) {
if (a[i] == '[') n++;
i++;
}
if (n > mx) mx = n;
n = 0;
}
for (i = 1; i <= len; i++) {
while (was[i] == true) {
if (a[i] == '[') n++;
i++;
}
if (n == mx) {
printf("%d\n", mx);
i--;
while (was[i] == true) i--;
i++;
while (was[i] == true) {
printf("%c", a[i]);
i++;
}
exit(0);
}
n = 0;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void spit(vector<T> a) {
for (auto i : a) cout << i << " ";
cout << "\n";
}
template <typename T>
void maxa(T& a, T b) {
a = max(a, b);
}
template <typename T>
void mina(T& a, T b) {
a = min(a, b);
}
int nxt() {
int a;
cin >> a;
return a;
}
long long int nxtll() {
long long int a;
cin >> a;
return a;
}
vector<pair<long long int, long long int> > tempa, result;
vector<pair<long long int, long long int> > factorize(long long int a) {
vector<pair<long long int, long long int> > answer;
for (long long int i = 2; i * i <= a; i++) {
if (a % i == 0) {
long long int count = 0;
while (a % i == 0) {
count++;
a /= i;
}
answer.push_back(make_pair(i, count));
}
}
if (a != 1) {
answer.push_back(make_pair(a, 1));
}
return answer;
}
long long int analyze(long long int a) {
long long int x = a;
result.clear();
for (auto i : tempa) {
if (x % i.first == 0) {
long long int count = 0;
while (x % i.first == 0) {
count++;
x /= i.first;
}
result.push_back(make_pair(i.first, count));
} else {
result.push_back(make_pair(i.first, 0));
}
}
return x;
}
long long int bahu(long long int a, long long int b) {
long long int answer = 1LL;
long long int base = a;
while (b) {
if (b % 2 == 0) {
base = base * base;
b /= 2LL;
} else {
answer *= base;
b--;
}
}
return answer;
}
long long int sheldon(vector<pair<long long int, long long int> >& a) {
long long int answer = bahu(a[0].first, a[0].second);
for (auto i : a) {
answer = min(answer, bahu(i.first, i.second));
}
return answer;
}
void solve() {
tempa.clear();
result.clear();
long long int a, b;
cin >> a >> b;
if (b > a) {
cout << a << endl;
return;
}
tempa = factorize(b);
long long int answer = analyze(a);
vector<pair<long long int, long long int> > pika;
for (int(i) = 0; (i) < (tempa.size()); (i)++) {
if (tempa[i].second > result[i].second) {
cout << a << endl;
return;
}
pika.push_back(
make_pair(tempa[i].first, result[i].second - tempa[i].second + 1LL));
}
cout << a / sheldon(pika) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
bool multi_run = 1;
if (multi_run) {
int t;
cin >> t;
while (t--) solve();
} else {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
bool sortby(const pair<int, int>& a, const pair<int, int>& b) {
if (a.first >= b.first && a.second >= b.second) return true;
return false;
}
double area(int x1, int y1, int x2, int y2, int x3, int y3) {
return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0);
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
fast();
int a, b, c, ans = 0;
cin >> a >> b >> c;
while (1) {
int x;
if (ans % 2 == 0) {
x = gcd(max(a, c), min(a, c));
} else {
x = gcd(max(b, c), min(b, c));
}
if (c >= x) {
c -= x;
ans++;
} else {
break;
}
}
cout << (ans + 1) % 2 << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
long long int pow(long long int a, long long int b) {
long long int ans = 1, i;
for (i = 0; i < b; i++) ans = (ans * a) % mod;
return ans;
}
int main() {
long long int n, i, temp, var = 7;
temp = 27;
scanf("%lld", &n);
long long int ans =
((long long int)pow(temp, n) - (long long int)pow(var, n));
if (ans < 0) ans += mod;
printf("%lld ", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2005, maxm = 500005;
inline int gi() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
int n, m, eu[maxm], ev[maxm];
bitset<maxn << 1> f[maxn];
int main() {
n = gi();
m = gi();
for (int i = 1; i <= m; ++i) eu[i] = gi(), ev[i] = gi(), f[eu[i]][ev[i]] = 1;
for (int i = 1; i <= n; ++i) f[i][i + n] = 1;
for (int i = 1; i <= n; ++i) {
int p = i;
while (!f[p][i]) ++p;
if (p != i) swap(f[p], f[i]);
for (int j = 1; j <= n; ++j)
if (j != i && f[j][i]) f[j] ^= f[i];
}
for (int i = 1; i <= m; ++i) puts(f[ev[i]][eu[i] + n] ? "NO" : "YES");
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int arr[2005][2005];
int dp[2005][2005];
int c1[2005][2005];
int r1[2005][2005];
void f(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = arr[i][j] + dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1];
}
}
}
int col[2005];
int col2[2005];
int row[2005];
int row1[2005];
int row2[2005];
void horiz(int n, int k) {
for (int i = 1; i <= n - k + 1; i++) {
for (int j = 1; j <= n; j++) {
col[j] = dp[n][j] - dp[n][j - 1] - dp[i + k - 1][j] +
dp[i + k - 1][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1];
col2[j] = dp[n][j] - dp[n][j - 1];
}
int sum = 0;
for (int j = k + 1; j <= n; j++) {
if (col2[j] == n) sum += 1;
}
for (int j = 1; j <= k; j++) {
if (col[j] == n - k) sum += 1;
}
c1[i][1] = sum;
for (int j = 2; j <= n - k + 1; j++) {
if (col2[j - 1] != n && col[j - 1] == n - k) {
sum--;
}
if (col[j + k - 1] == n - k && col2[j + k - 1] != n) {
sum++;
}
c1[i][j] = sum;
}
}
}
void vert(int n, int k) {
for (int i = 1; i <= n - k + 1; i++) {
for (int j = 1; j <= n; j++) {
row[j] = dp[j][n] - dp[j - 1][n] - dp[j][i + k - 1] +
dp[j - 1][i + k - 1] + dp[j][i - 1] - dp[j - 1][i - 1];
row2[j] = dp[j][n] - dp[j - 1][n];
}
int sum = 0;
for (int j = k + 1; j <= n; j++) {
if (row2[j] == n) sum += 1;
}
for (int j = 1; j <= k; j++) {
if (row[j] == n - k) sum += 1;
}
r1[1][i] = sum;
for (int j = 2; j <= n - k + 1; j++) {
if (row2[j - 1] != n && row[j - 1] == n - k) {
sum--;
}
if (row[j + k - 1] == n - k && row2[j + k - 1] != n) {
sum++;
}
r1[j][i] = sum;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
char c;
cin >> c;
if (c == 'W')
arr[i][j] = 1;
else
arr[i][j] = 0;
}
}
f(n);
horiz(n, k);
vert(n, k);
int mmmm = 0;
for (int i = 1; i <= n - k + 1; i++) {
for (int j = 1; j <= n - k + 1; j++) {
mmmm = max(mmmm, c1[i][j] + r1[i][j]);
}
}
cout << mmmm << '\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 5;
vector<long long int> h(101, 0);
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
vector<long long int> v1(n), v2;
for (int i = 0; i < n; i++) {
cin >> v1[i];
}
sort(v1.begin(), v1.end());
long long int ans = v1[0] * v1[n - 1];
for (int i = 2; i * 1ll * i <= ans; i++) {
if (ans % i == 0) {
v2.push_back(i);
if (ans / i != i) v2.push_back(ans / i);
}
}
sort(v2.begin(), v2.end());
if (v1 == v2)
cout << ans << endl;
else
cout << -1 << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
const int Mod = 1000000007;
using namespace std;
int res[1234567];
long long x[555], y[555];
int main(void) {
int i, j, n, m, k, s;
long long t;
x[0] = y[0] = 0;
while (scanf("%d %d", &n, &m) != EOF) {
s = 0;
for (i = 1; i <= n; i++) res[i] = 1;
for (i = 1; i <= m; i++) scanf("%I64d %I64d", &x[i], &y[i]);
for (i = 1; i <= m; i++)
for (j = i + 1; j <= m; j++) {
if (y[i] == y[j]) continue;
x[0] = x[i] - (x[i] - x[j]) * (y[i] - y[0]) / (y[i] - y[j]);
if (x[0] > n || x[0] <= 0 ||
(x[i] - x[j]) * (y[i] - y[0]) != (x[i] - x[0]) * (y[i] - y[j]))
continue;
t = 2;
for (k = j + 1; k <= m; k++) {
if (y[k] == y[i] || y[k] == y[j]) continue;
if ((x[i] - x[j]) * (y[i] - y[k]) == (x[i] - x[k]) * (y[i] - y[j]))
t++;
}
if (res[x[0]] < t) res[x[0]] = t;
}
for (i = 1; i <= n; i++) s += res[i];
printf("%d\n", s);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,fma,avx,avx2")
using namespace std;
using namespace std::chrono;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int fastscan() {
bool negative = false;
int c, number = 0;
for (c = getchar(); !(c > 47 && c < 58) && c != '-'; c = getchar())
;
if (c == '-') c = getchar(), negative = true;
for (; (c > 47 && c < 58); c = getchar())
number = (number << 1) + (number << 3) + (c ^ 48);
if (negative) return -number;
return number;
}
void write(int x) {
if (x < 0) {
putchar('-');
write(-x);
return;
}
if (x > 9) write(x / 10);
putchar(x % 10 ^ 48);
}
int fact[200001], ifact[200001];
void Cinit() {
fact[0] = 1;
for (int i = 1; i < 200001; ++i) fact[i] = 1LL * fact[i - 1] * i % 1000000007;
ifact[200001 - 1] = power(fact[200001 - 1], 1000000007 - 2, 1000000007);
for (int i = 200001 - 2; i >= 0; --i)
ifact[i] = 1LL * ifact[i + 1] * (i + 1) % 1000000007;
}
int nCr(int n, int r) {
if (r < 0 || r > n) return 0;
return (1LL * fact[n] * ifact[r] % 1000000007 * ifact[n - r] % 1000000007) %
1000000007;
}
struct segtree {
vector<long long> t = vector<long long>(200001),
lazy = vector<long long>(200001);
void push(int v) {
t[v * 2] += lazy[v];
lazy[v * 2] += lazy[v];
t[v * 2 + 1] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
lazy[v] = 0;
}
void update(int v, int tl, int tr, int l, int r, int addend) {
if (l > r) return;
if (l == tl && tr == r) {
t[v] += addend;
lazy[v] += addend;
} else {
push(v);
int tm = (tl + tr) / 2;
update(v * 2, tl, tm, l, min(r, tm), addend);
update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, addend);
t[v] = max(t[v * 2], t[v * 2 + 1]);
}
}
int query(int v, int tl, int tr, int l, int r) {
if (l > r) return -1;
if (l <= tl && tr <= r) return t[v];
push(v);
int tm = (tl + tr) / 2;
return max(query(v * 2, tl, tm, l, min(r, tm)),
query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r));
}
};
int spf[5000001];
void sieve() {
spf[1] = 1;
for (int i = 2; i < 200001; i++) spf[i] = i;
for (int i = 4; i < 200001; i += 2) spf[i] = 2;
for (int i = 3; i * i < 200001; i++)
if (spf[i] == i)
for (int j = i * i; j < 200001; j += i)
if (spf[j] == j) spf[j] = i;
}
vector<int> getFactorization(int x) {
vector<int> ret;
while (x != 1) ret.push_back(spf[x]), x = x / spf[x];
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(10);
int _, n, m, k, l = 0, r = 0, i, j, tt = 1, x, y, z = 1;
bool flag = 0;
string s, second;
cin >> tt;
for (_ = 1; _ <= tt; ++_) {
int q;
long long w, h;
cin >> w >> h;
vector<int> a, b, c, d;
cin >> k;
while (k--) {
cin >> n;
a.push_back(n);
}
sort(a.begin(), a.end());
cin >> k;
while (k--) {
cin >> n;
b.push_back(n);
}
sort(b.begin(), b.end());
cin >> k;
while (k--) {
cin >> n;
c.push_back(n);
}
sort(c.begin(), c.end());
cin >> k;
while (k--) {
cin >> n;
d.push_back(n);
}
sort(d.begin(), d.end());
long long ans = 0;
long long dx = max(a.back() - a[0], b.back() - b[0]);
long long dy = max(c.back() - c[0], d.back() - d[0]);
cout << max(dx * h, w * dy);
cout << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
int len, flag = 0;
string s;
long long solve(char c1, char c2) {
int Min = 0, Max = 0, sum = 0;
int p_lastMax = -1, p_firstMax = -1, p_lastMin = -1, p_firstMin = -1;
int c = 0;
for (int i = 0; i < len; i++) {
bool ys = 0;
if (s[i] == c1) sum--, ys = 1;
if (s[i] == c2) sum++, ys = 1;
if (ys) {
c++;
if (sum == Max) {
p_lastMax = i;
}
if (sum > Max) {
p_firstMax = p_lastMax = i;
Max = sum;
}
if (sum == Min) {
p_lastMin = i;
}
if (sum < Min) {
p_firstMin = p_lastMin = i;
Min = sum;
}
}
}
long long tmp = Max - Min + 1;
if ((c > 1) && (p_lastMax < p_firstMin || p_firstMax > p_lastMin)) {
tmp--;
flag++;
}
return tmp;
}
int main() {
int T;
cin >> T;
while (T--) {
flag = 0;
cin >> s;
len = s.size();
long long W = solve('W', 'S');
long long H = solve('A', 'D');
long long ans;
if (flag == 2)
ans = min(W * (H + 1), (W + 1) * H);
else
ans = W * H;
cout << ans << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
int main() {
int a, b, c, d, ans, ans1, ans2, ans3, fin, fin1;
scanf("%d %d %d %d", &a, &b, &c, &d);
ans = ((3 * a) / 10);
ans1 = (a - (a / 250) * c);
if (ans > ans1) {
fin = ans;
} else {
fin = ans1;
}
ans2 = ((3 * b) / 10);
ans3 = (b - (b / 250) * d);
if (ans2 > ans3) {
fin1 = ans2;
} else {
fin1 = ans3;
}
if (fin > fin1) {
printf("Misha");
} else if (fin < fin1) {
printf("Vasya");
} else if (fin == fin1) {
printf("Tie");
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void read(int& x) {
bool fu = 0;
char c;
for (c = getchar(); c <= 32; c = getchar())
;
if (c == '-') fu = 1, c = getchar();
for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0';
if (fu) x = -x;
};
char getc() {
char c;
for (c = getchar(); c <= 32; c = getchar())
;
return c;
}
bool pd(int x) {
while (x) {
if (x % 10 != 4 && x % 10 != 7) return 0;
x /= 10;
}
return 1;
}
int num(int x) {
int u = 1;
while (x) {
if (x % 10 == 4)
u = u * 2;
else
u = u * 2 + 1;
x /= 10;
}
return u;
}
int n, i, j, t, a[1000010], tot, L, R, rig, ttt;
long long firL, res, ans;
struct node {
int wei, key;
};
node b[2010];
int V[2010][2010];
bool vis[2010];
set<int> S;
void jia(int x) {
ttt = min(ttt, x - rig);
set<int>::iterator pos = S.upper_bound(x);
int p2 = *pos, p1 = *(--pos);
res -= 1LL * (p2 - p1) * (p2 - p1 - 1);
res += 1LL * (x - p1) * (x - p1 - 1);
res += 1LL * (p2 - x) * (p2 - x - 1);
S.insert(x);
}
void ins(int x) {
if (!vis[x]) {
vis[x] = 1;
int i;
for (i = 1; i <= V[x][0]; i++)
if (V[x][i] >= rig) jia(V[x][i]);
}
}
int main() {
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
b[0] = (node){1, 0};
for (i = 1; i <= n; i++)
if (pd(a[i])) {
t = num(a[i]);
b[++tot] = (node){i, t};
V[t][++V[t][0]] = i;
}
b[tot + 1] = (node){n + 1, 0};
int ls, rs;
for (R = 1; R <= tot + 1; R++) {
rig = b[R].wei;
memset(vis, 0, sizeof(vis));
S.clear();
S.insert(rig - 1);
S.insert(n + 1);
res = 1LL * (n - b[R].wei + 1) * (n - b[R].wei + 2);
ttt = n - rig + 1;
ls = b[R].wei - b[R - 1].wei - 1;
for (i = b[R - 1].wei + 1; i <= b[R].wei - 1; i++) {
ans += 1LL * (i - b[R - 1].wei) *
(1LL * (n - i + 1) * ((n - i + 1) - 1) / 2);
}
for (L = R - 1; L >= 0; L--) {
ins(b[L].key);
ls = b[L].wei - b[L - 1].wei;
rs = b[R].wei - b[R - 1].wei;
ans += 1LL * ls * rs * (res / 2);
ans += 1LL * ls * (1LL * (rs) * ((rs)-1) / 2) * ttt;
ans += 1LL * ls * (1LL * (rs + 1) * ((rs + 1) - 1) * ((rs + 1) - 2) / 6);
}
}
cout << ans << endl;
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int a[101][101];
int u[101];
int uu[101];
int d[101];
int b[101];
int t[101];
int main() {
int n, m, k;
cin >> n >> m >> k;
for (long long i = 1; i <= n; i++)
for (long long j = 1; j <= m; j++) cin >> a[i][j];
for (long long j = 1; j <= m; j++) {
for (long long y = 1; y <= k; y++) u[y] = 0;
for (long long i = 1; i <= n; i++)
if (!t[i] && a[i][j]) {
if (u[a[i][j]])
t[u[a[i][j]]] = j, t[i] = j, d[a[i][j]] = 1;
else if (d[a[i][j]])
t[i] = j;
else
u[a[i][j]] = i;
}
}
for (long long i = 1; i <= n; i++) cout << t[i] << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, k;
cin >> n >> m >> k;
vector<pair<int, int> > p;
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
for (int j = m; j >= 1; j--) p.push_back(make_pair(i, j));
} else {
for (int j = 1; j <= m; j++) p.push_back(make_pair(i, j));
}
}
for (int i = 1; i <= k - 1; i++) {
cout << "2 " << p[2 * i - 2].first << " " << p[2 * i - 2].second << " "
<< p[2 * i - 1].first << " " << p[2 * i - 1].second << endl;
}
cout << p.size() - 2 * (k - 1) << " ";
for (int i = 2 * k - 2; i < p.size(); i++) {
cout << p[i].first << " " << p[i].second << " ";
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
namespace INIT {
char buf[1 << 15], *fs, *ft;
inline char getc() {
return (fs == ft &&
(ft = (fs = buf) + fread(buf, 1, 1 << 15, stdin), fs == ft))
? 0
: *fs++;
}
inline int read() {
int x = 0, f = 1;
char ch = getc();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getc();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getc();
}
return x * f;
}
} // namespace INIT
using namespace INIT;
int n, p[1000010], vis[1000010], ans[1000010];
vector<int> a[1000010], b[1000010];
int main() {
n = read();
for (int i = 1; i <= n; ++i) p[i] = read();
for (int i = 1; i <= n; ++i) {
int num = 0;
for (int j = i; !vis[j]; j = p[j]) vis[j] = 1, ++num, a[i].push_back(j);
b[num].push_back(i);
}
for (int i = 1; i <= n; ++i)
if (i % 2 == 0 && (b[i].size() & 1)) {
puts("-1");
return 0;
}
memset(vis, 0, sizeof(vis));
for (int i = 0; i <= (int)b[1].size() - 1; ++i) ans[b[1][i]] = b[1][i];
for (int i = 2; i <= n; ++i)
for (int j = 0; j <= (int)b[i].size() - 1; ++j) {
if (i & 1) {
int x = a[b[i][j]][0], y = a[b[i][j]][i / 2 + 1];
while (!vis[x]) {
vis[x] = vis[y] = 1;
ans[x] = y;
x = p[x];
ans[y] = x;
y = p[y];
}
} else {
int x = a[b[i][j]][0], y = a[b[i][j + 1]][0];
while (!vis[x] && !vis[y]) {
vis[x] = vis[y] = 1;
ans[x] = y;
x = p[x];
ans[y] = x;
y = p[y];
}
j++;
}
}
for (int i = 1; i <= n; ++i) printf("%d ", ans[i]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, c, t, flag;
cin >> n >> m;
int a[1001] = {0};
for (int i = 0; i < m; i++) {
cin >> c >> t;
a[c]++;
a[t]++;
}
for (int i = 1; i < 1001; i++) {
if (a[i] == 0) {
flag = i;
break;
}
}
cout << n - 1 << endl;
for (int i = 1; i <= n; i++) {
if (i != flag) cout << flag << " " << i << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void splitstr(const string &s, vector<T> &out) {
istringstream in(s);
out.clear();
copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
template <class T>
T read_value(string s) {
T result;
istringstream sin(s);
sin >> result;
return result;
}
string read_line() {
string ret_val;
getline(std::cin, ret_val);
return ret_val;
}
template <class T>
void init_array(T *arr, int size, T value) {
for (int i = 0; i < size; i++) arr[i] = value;
}
template <class T>
inline T gcd(T a, T b) {
return (!a) ? b : gcd(b % a, a);
}
template <class T>
inline T mod(T a, T p) {
a %= p;
return (a < 0) ? a + p : a;
}
template <class T>
inline int numbits(T n) {
return (!n) ? 0 : 1 + numbits(n & (n - 1));
}
template <class T>
inline T inverse(T a, T m) {
a = mod<T>(a, m);
return (a == 1) ? 1 : mod((1 - m * inverse(m % a, a)) / a, m);
}
template <class T>
inline bool is_prime(T a) {
T lim = (T)sqrt((double)a);
for (T i = 2; i <= T(lim + 1E-9); i++)
if (a % i == 0) return false;
return true;
}
template <class T>
inline T power(T a, T p, T mod) {
if (!p) return 1;
T temp = power(a, p >> 1, mod);
temp = (temp * temp) % mod;
if (p & 1) temp = (temp * a) % mod;
return temp;
}
void get_primes(int start, int end, vector<int> &vi) {
bool *p = new bool[end + 1];
init_array<bool>(p, end + 1, 0);
p[1] = 1;
for (int i = 2; i <= end; i++) {
if (!p[i]) {
if (i >= start) vi.push_back(i);
for (int j = 2 * i; j <= end; j += i) p[j] = 1;
}
}
delete[] p;
}
bool dfs(int current, int final, int total, vector<int> &visited,
vector<vector<bool> > &edges, bool flow) {
if (current == final) return true;
if (visited[current]) return false;
visited[current] = true;
for (int i = 0; i < total; i++)
if (edges[current][i] && dfs(i, final, total, visited, edges, flow)) {
if (flow) {
edges[current][i] = false;
edges[i][current] = true;
}
return true;
}
return false;
}
int flow(int in, int out, int total, vector<vector<bool> > &edges) {
int result = 0;
for (vector<int> visited(total, 0); dfs(in, out, total, visited, edges, true);
++result)
visited = vector<int>(total, 0);
return result;
}
void create_set(int x, int *P, int *rank) {
P[x] = x;
rank[x] = 0;
}
int find_set(int x, int *P) {
if (x != P[x]) P[x] = find_set(P[x], P);
return P[x];
}
bool merge_sets(int x, int y, int *P, int *rank) {
int Px = find_set(x, P);
int Py = find_set(y, P);
if (Px == Py) return false;
if (rank[Px] > rank[Py])
P[Py] = Px;
else
P[Px] = Py;
if (rank[Px] == rank[Py]) rank[Py]++;
return true;
}
template <typename T>
int read_cum_freq(int index, T *tree) {
int sum = 0;
while (index) {
sum += tree[index];
index -= (index & -index);
}
return sum;
}
template <typename T>
void upd_freq(int index, int mxIndex, int value, T *tree) {
while (index <= mxIndex) {
tree[index] += value;
index += (index & -index);
}
}
template <typename T>
int read_freq(int index, T *tree) {
return read_cum_freq(index, tree) - read_cum_freq(index - 1, tree);
}
void build_failure_function(const string &str, int *temp_arr) {
temp_arr[0] = temp_arr[1] = 0;
for (int i = 2; i <= ((int)(str).size()); i++) {
temp_arr[i] = -1;
for (int ind = temp_arr[i - 1]; temp_arr[i] == -1; ind = temp_arr[ind]) {
if (str[ind] == str[i - 1]) {
temp_arr[i] = ind + 1;
} else if (ind == 0) {
temp_arr[i] = 0;
}
}
}
}
void KMP(const string &text, const string &pattern, int *res) {
int *temp_arr = new int[((int)(pattern).size()) + 1];
build_failure_function(pattern, temp_arr);
int i = 0;
int ind = 0;
while (i < ((int)(text).size())) {
if (text[i] == pattern[ind]) {
res[i] = ++ind;
if (res[i] == ((int)(pattern).size())) {
ind = temp_arr[ind];
}
i++;
} else if (ind == 0) {
res[i++] = 0;
} else {
ind = temp_arr[ind];
}
}
delete[] temp_arr;
}
template <typename T, int N>
struct matrix {
T m[N][N];
T md;
matrix(T md, bool unary = false) {
this->md = md;
memset(m, 0, sizeof(m));
if (unary) {
for (int i = 0; i < N; i++) {
m[i][i] = 1;
}
}
}
matrix operator*(const matrix &that) const {
matrix ret_val(md, false);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
ret_val.m[i][j] += this->m[i][k] * that.m[k][j];
ret_val.m[i][j] %= md;
}
}
}
return ret_val;
}
vector<T> operator*(const vector<T> &vec) const {
vector<T> ret_val(vec.size(), 0);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
ret_val[i] += m[i][j] * vec[j];
ret_val[i] %= md;
}
}
return ret_val;
}
matrix power(int power) const {
matrix temporary = *this;
matrix ret_val(md, true);
while (power) {
if (power & 1) ret_val = ret_val * temporary;
temporary = temporary * temporary;
power >>= 1;
}
return ret_val;
}
};
void setup(int value, string name) {
string name_in = name + ".in";
string name_out = name + ".out";
freopen(name_in.c_str(), "r", stdin);
if (value) freopen(name_out.c_str(), "w", stdout);
}
void run() {
string str;
std::cin >> str;
string res;
int ind = 0;
for (char ch = 'z'; ch >= 'a'; --ch) {
int num = std::count(str.begin() + ind, str.end(), ch);
if (!num) continue;
res += string(num, ch);
for (ind = str.size() - 1; str[ind] != ch; --ind)
;
}
std::cout << res << std::endl;
}
int main() {
run();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 5e3 + 5;
vector<pair<long long, pair<long long, long long>>> H;
vector<pair<pair<long long, long long>, long long>> V;
vector<pair<long long, long long>> s[2 * N];
pair<pair<long long, long long>, pair<long long, long long>> a[N];
long long T[2 * N];
long long f[2 * N];
long long g[2 * N];
long long sg[2 * N];
long long n, ans;
void update(long long i, long long val) {
while (i <= 10001) {
T[i] += val;
i += i & -i;
}
}
long long get(long long i) {
long long res = 0;
while (i) {
res += T[i];
i -= i & -i;
}
return res;
}
void solve() {
for (long long i = 0, k = 0; i < H.size(); i++) {
while (k < V.size() && V[k].first.first <= H[i].first) {
s[i].push_back(make_pair(V[k].second, 1));
long long l = i;
long long r = H.size();
while (r - l > 1) {
long long mid = (l + r) >> 1;
if (V[k].first.second >= H[mid].first)
l = mid;
else
r = mid;
}
s[r].push_back(make_pair(V[k].second, -1));
k++;
}
for (pair<long long, long long> x : s[i]) {
update(x.first, x.second);
}
for (long long j = i + 1; j <= H.size(); j++) {
for (pair<long long, long long> x : s[j]) {
update(x.first, x.second);
}
long long l = max(H[i].second.first, H[j].second.first);
long long r = min(H[i].second.second, H[j].second.second);
if (l <= r) {
long long num = get(r) - get(l - 1);
ans += num * (num - 1) / 2;
}
}
for (long long j = i + 1; j <= H.size(); j++) {
for (pair<long long, long long> x : s[j]) {
update(x.first, -x.second);
}
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> a[i].first.first >> a[i].second.first >> a[i].first.second >>
a[i].second.second;
a[i].first.first += 5001;
a[i].first.second += 5001;
a[i].second.first += 5001;
a[i].second.second += 5001;
if (a[i].first.first > a[i].first.second)
swap(a[i].first.first, a[i].first.second);
if (a[i].second.first > a[i].second.second)
swap(a[i].second.first, a[i].second.second);
if (a[i].first.first == a[i].first.second) {
V.push_back(make_pair(a[i].second, a[i].first.first));
} else
H.push_back(make_pair(a[i].second.first, a[i].first));
}
sort(V.begin(), V.end());
sort(H.begin(), H.end());
solve();
cout << ans;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int num[1000001];
int L[1000001];
int sum_tree[1000001 * 3];
int max_tree[1000001 * 3];
void update(int a, int b, int i = 1, int start = 1, int end = n + 1) {
if (start == a && end == b) {
max_tree[i] += 1;
sum_tree[i] += 1;
return;
}
int mid = (start + end) / 2;
if (a < mid) update(a, min(b, mid), i * 2, start, mid);
if (b > mid) update(max(a, mid), b, i * 2 + 1, mid, end);
max_tree[i] = max(max_tree[i * 2], max_tree[i * 2 + 1]) + sum_tree[i];
}
int query(int a, int b, int i = 1, int start = 1, int end = n + 1) {
if (start == a && end == b) return max_tree[i];
int mid = (start + end) / 2;
int ans = 0;
if (a < mid) ans = max(ans, query(a, min(b, mid), i * 2, start, mid));
if (b > mid) ans = max(ans, query(max(a, mid), b, i * 2 + 1, mid, end));
return ans + sum_tree[i];
}
int main() {
cin >> n >> k;
num[0] = 1 << 30;
for (int i = 1; i <= n; ++i) {
cin >> num[i];
}
for (int i = 1; i <= n; ++i) {
int idx = i - 1;
while (num[i] > num[idx]) {
idx = L[idx];
}
L[i] = idx;
}
for (int i = 1; i <= n; ++i) {
update(L[i] + 1, i + 1);
if (i >= k) {
cout << query(i - k + 1, i + 1) << ' ';
}
}
cout << '\n';
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
scanf("%d", &n);
int t;
int prev = 0;
int res = 0;
for (int i = 0; i < n; ++i) {
scanf("%d", &t);
if (t - prev <= 15) {
res = prev = t;
}
}
cout << min(90, res + 15);
}
int main() {
solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int X1, Y1, X2, Y2;
int x, y;
int main() {
cin >> X1 >> Y1 >> X2 >> Y2;
cin >> x >> y;
int xx = abs(X2 - X1);
int yy = abs(Y2 - Y1);
int cx = xx / x;
int cy = yy / y;
if (xx % x || yy % y) {
cout << "NO";
return 0;
}
if (cx % 2 != cy % 2) {
cout << "NO";
return 0;
}
cout << "YES";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int N, p[1000000];
long long X[1000000], Y[1000000], A[1000000], dp[1000000];
struct L {
long long m, c;
};
long long eval(L f, long long x) { return f.m * x + f.c; }
L q[1000000];
int tail = 0, head = 0;
long long best_line(long long x) {
while (head - tail >= 2 && eval(q[tail + 1], x) >= eval(q[tail], x)) tail++;
return eval(q[tail], x);
}
long long intersect(L a, L b) {
long long num = b.c - a.c;
long long dem = a.m - b.m;
return num / dem - ((num ^ dem) < 0 && num % dem);
}
void insert_line(L f) {
while (head - tail >= 2 &&
intersect(q[head - 1], f) <= intersect(q[head - 2], q[head - 1]))
head--;
q[head++] = f;
}
char buffer[42000100];
char *it;
template <typename T>
void read(T &x) {
x = 0;
while (!isdigit(*it)) it++;
do x = x * 10 + *it - '0';
while (isdigit(*++it));
}
int main() {
fread(buffer, 1, 42000100, stdin);
it = buffer;
read(N);
for (int i = 0; i < N; ++i) {
read(X[i]);
read(Y[i]);
read(A[i]);
}
iota(p, p + N, 0);
sort(p, p + N,
[&](int i, int j) { return X[i] != X[j] ? X[i] < X[j] : Y[i] > Y[j]; });
q[head++] = {0, 0};
for (int it = 0, i = p[it]; it < N; ++it, i = p[it]) {
dp[i] = X[i] * Y[i] - A[i] + best_line(-Y[i]);
insert_line({X[i], dp[i]});
}
printf("%lld\n", *max_element(dp, dp + N));
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, l;
long long calcCombine(long long n, long long m) {
long long ret = 1;
for (long long i = n - m + 1; i <= n; i++) {
ret *= i;
}
for (long long i = 1; i <= m; i++) {
ret /= i;
}
return ret;
}
long long calc(long long a, long long b, long long c, long long l) {
long long ret = 0;
for (long long i = 0; i <= l; i++) {
long long first = min(l - i, a + i - b - c);
if (first >= 0) {
ret += (first + 1) * (first + 2) / 2;
}
}
return ret;
}
void work() {
cin >> a >> b >> c >> l;
long long ans = calcCombine(l + 3, 3);
ans -= calc(a, b, c, l);
ans -= calc(b, a, c, l);
ans -= calc(c, a, b, l);
cout << ans << endl;
}
int main() {
work();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long max_n = 1000000000;
vector<int> serie;
long long ans;
void funct(long long n) {
long long r = serie.size() - 1;
long long mid;
while (n >= 2) {
long long temp = 0;
long long l = 0;
while (l <= r) {
mid = (l + r) / 2;
if (serie[mid] > n) {
r = mid - 1;
} else {
l = mid + 1;
temp = mid;
}
}
ans++;
n = n - serie[temp];
r = mid;
}
}
int main() {
int t;
cin >> t;
serie.clear();
serie.push_back(2);
long long a = 5;
while (serie[serie.size() - 1] <= max_n) {
long long temp = serie[serie.size() - 1] + a;
serie.push_back(temp);
a += 3;
}
for (int i = 0; i < t; i++) {
ans = 0;
long long n;
cin >> n;
funct(n);
cout << ans << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> vv[100];
long long arr[3];
long long m[8][8];
map<string, int> ma;
string r[] = {"Anka", "Chapay", "Cleo", "Troll",
"Dracul", "Snowy", "Hexadecimal"};
int main() {
long long n;
cin >> n;
vector<string> v;
long long idx = 0;
for (long long i = 0; i < 7; i++) ma[r[i]] = i;
for (long long i = 0; i < n; i++) {
string s, second, sss;
cin >> s >> second >> sss;
m[ma[s]][ma[sss]] = 1;
}
long long mm = 1e10;
for (long long i = 0; i < 3; i++) cin >> arr[i];
vector<int> diff[100];
for (long long i = 1; i <= 7; i++) {
for (long long j = 1; j <= 7; j++) {
if (i + j >= 7) break;
long long k = 7 - (i + j);
vector<int> q;
q.push_back(arr[0] / i);
q.push_back(arr[1] / j);
q.push_back(arr[2] / k);
sort(q.begin(), q.end());
if (mm > q[2] - q[0]) {
mm = q[2] - q[0];
}
}
}
cout << mm;
int g = 0;
for (long long i = 1; i <= 7; i++) {
for (long long j = 1; j <= 7; j++) {
if (i + j >= 7) break;
long long k = 7 - (i + j);
vector<int> q;
q.push_back(arr[0] / i);
q.push_back(arr[1] / j);
q.push_back(arr[2] / k);
sort(q.begin(), q.end());
if (mm == q[2] - q[0]) {
diff[g].push_back(i);
diff[g].push_back(j);
diff[g++].push_back(k);
}
}
}
long long ans = 0;
for (long long z = 0; z < g; z++) {
vector<int> c;
for (long long i = 0; i < 7; i++) c.push_back(i);
do {
long long k = 0;
for (long long i = 0; i < diff[z][0]; i++) {
for (long long j = i + 1; j < diff[z][0]; j++) {
k += (m[c[i]][c[j]] + m[c[j]][c[i]]);
}
}
for (long long i = diff[z][0]; i < diff[z][0] + diff[z][1]; i++) {
for (long long j = i + 1; j < diff[z][0] + diff[z][1]; j++) {
k += (m[c[i]][c[j]] + m[c[j]][c[i]]);
}
}
for (long long i = diff[z][0] + diff[z][1];
i < diff[z][0] + diff[z][1] + diff[z][2]; i++) {
for (long long j = i + 1; j < diff[z][0] + diff[z][1] + diff[z][2];
j++) {
k += (m[c[i]][c[j]] + m[c[j]][c[i]]);
}
}
ans = max(ans, k);
} while (next_permutation(c.begin(), c.end()));
}
cout << " " << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
set<pair<long long, long long>> puntos;
vector<pair<long long, long long>> v;
long long cont = 0;
for (long long i = 0; i < n; ++i) {
long long x, y;
cin >> x >> y;
v.push_back({x, y});
puntos.insert({x, y});
}
sort(v.begin(), v.end());
for (int i = 0; i < n - 1; ++i)
for (int j = i + 1; j < n; ++j) {
if ((v[i].first + v[j].first) % 2 == 0 &&
(v[i].second + v[j].second) % 2 == 0) {
long long x1, y1;
x1 = (v[i].first + v[j].first) / 2;
y1 = (v[i].second + v[j].second) / 2;
if (puntos.count({x1, y1})) cont++;
}
}
cout << cont;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 233;
priority_queue<int> q;
vector<int> s[maxn];
int a[maxn], b[maxn];
int main() {
int n, k;
cin >> n >> k;
long long ans = 0;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
for (int i = 1; i <= n; i++) s[a[i]].push_back(b[i]);
int t = 0;
for (int i = 1; i <= k; i++) {
if (!s[i].size()) {
t++;
continue;
}
sort(s[i].begin(), s[i].end());
for (int j = 0; j < s[i].size() - 1; j++) q.push(-s[i][j]);
}
while (t--) {
ans -= q.top();
q.pop();
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a, b;
long long res = 0LL;
int mod = 1000000007;
int main() {
cin >> a >> b;
cout << (((((((((a * (a + 1)) / 2 % mod) * (b % mod)) % mod) + a) % mod) %
mod) *
((b * (b - 1)) / 2 % mod)) %
mod)
<< endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 5000 + 100;
const long long maxk = 5000 + 100;
struct pii {
long long x, v, y;
pii(long long xx = 0, long long vv = 0, long long yy = 0) {
x = xx;
v = vv;
y = yy;
}
bool operator<(const pii& b) const {
if (x == b.x) {
if (v == b.v) {
return y < b.y;
}
return v < b.v;
}
return x < b.x;
}
};
vector<pii> v;
long long n;
int main() {
long long k, x, y, m, ans = 0, a, b;
scanf("%I64d", &n);
long long tot = 0;
for (long long i = 1; i <= n; i++) {
long long cur = 0;
scanf("%I64d%I64d%I64d%I64d%I64d", &k, &a, &x, &y, &m);
v.push_back(pii(cur, a, i));
tot++;
for (long long j = 2; j <= k; j++) {
b = (a * x + y) % m;
if (b < a) {
cur++;
}
if (tot < 200000) v.push_back(pii(cur, b, i));
tot++;
a = b;
}
ans = max(ans, cur);
}
printf("%I64d\n", ans);
sort(v.begin(), v.end());
if (tot <= 200000)
for (long long i = 0; i < v.size(); i++) {
printf("%I64d %I64d\n", v[i].v, v[i].y);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> v;
for (long long i = 1; i <= n; i *= (long long)2) {
v.push_back(i);
n -= i;
}
if (n > 0) v.push_back(n);
sort(v.begin(), v.end());
long long ans = (long long)v.size() - 1;
cout << ans << "\n";
for (long long i = 0; i < ans; i++) cout << v[i + 1] - v[i] << " ";
cout << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
const long long mod = 1e9 + 7;
void solve_test() {
long long m, n;
cin >> m >> n;
long long a[m][n];
for (long long i = 0; i < m; i++)
for (long long j = 0; j < n; j++) cin >> a[i][j];
long long l = 0, r = 1e10, ans = 0;
while (l <= r) {
long long mid = l + (r - l) / 2;
bool ok = 1;
for (long long i = 0; i < n; i++) {
bool first = 0;
for (long long j = 0; j < m; j++)
if (a[j][i] >= mid) first = 1;
ok &= first;
}
bool ok2 = 0;
for (long long i = 0; i < m; i++) {
long long cnt = 0;
for (long long j = 0; j < n; j++)
if (a[i][j] >= mid) cnt++;
if (cnt > 1) ok2 = 1;
}
if (ok and ok2) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
cin >> t;
for (long long i = 1; i <= t; i++) {
solve_test();
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T1>
inline void debug(T1 _x) {
cout << _x << '\n';
}
template <class T1, class T2>
inline void debug(T1 _x, T2 _y) {
cout << _x << ' ' << _y << '\n';
}
template <class T1, class T2, class T3>
inline void debug(T1 _x, T2 _y, T3 _z) {
cout << _x << ' ' << _y << ' ' << _z << '\n';
}
template <class T1, class T2, class T3, class T4>
inline void debug(T1 _x, T2 _y, T3 _z, T4 _zz) {
cout << _x << ' ' << _y << ' ' << _z << ' ' << _zz << '\n';
}
template <class T1>
inline void debug(T1 _array, int _size) {
cout << "[";
for (int i = 0; i < _size; ++i) {
cout << ' ' << _array[i];
}
puts(" ]");
}
inline bool CI(int &_x) { return scanf("%d", &_x) == 1; }
inline bool CI(int &_x, int &_y) { return CI(_x) && CI(_y); }
inline bool CI(int &_x, int &_y, int &_z) { return CI(_x) && CI(_y) && CI(_z); }
inline bool CI(int &_x, int &_y, int &_z, int &_zz) {
return CI(_x) && CI(_y) && CI(_z) && CI(_zz);
}
inline void wait(double seconds) {
double endtime = clock() + (seconds * CLOCKS_PER_SEC);
while (clock() < endtime) {
;
}
}
const double eps = 1e-9;
int n, x, y;
inline void Read() { CI(n, x, y); }
inline string toS(double var) {
char _buff[50];
sprintf(_buff, "%.9lf", var);
return (string)_buff;
}
inline void Proc() {
long long xtra;
for (xtra = 0;; ++xtra) {
long long tmp = (xtra + x) * 100.;
if (tmp >= ((long long)(y)*n)) break;
}
cout << xtra;
puts("");
}
int main() {
int tt = 1;
for (int i = 1, j1 = tt + 1; i < j1; ++i) {
Read();
Proc();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e7 + 10;
long long prime[M];
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
vector<int> graph[200001];
void precompute() {
for (long long i = 2; i < M; i++) {
if (!prime[i]) {
for (long long j = 1; i * j < M; j++) prime[i * j] = i;
}
}
}
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t, i, j, n, m, k, temp, x = 1, y = 2, z;
cin >> n;
vector<long long> dp(n + 1);
dp[0] = 0;
dp[1] = 1;
if (n >= 2) dp[2] = 2;
for (i = 3; i <= n; i++) {
if (i % 2 == 0) {
dp[i] = (x + 1) % 1000000007;
y += dp[i] % 1000000007;
y %= 1000000007;
} else {
dp[i] = (y + 1) % 1000000007;
x += dp[i] % 1000000007;
x %= 1000000007;
}
}
long long ans = 0;
for (i = 0; i <= n; i++) {
ans += dp[i];
ans %= 1000000007;
}
cout << ans << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int TESTS = 1;
while (TESTS--) {
long long i, j, k, l, m, n, c, d, v[31] = {0}, a, b;
c = pow(2, 30) - 1;
d = pow(2, 30) - 1;
cout << "? " << c << " " << d << '\n';
fflush(stdout);
cin >> k;
j = k * (-1);
a = 0;
b = 0;
c = 0;
d = 0;
for (m = 29; m >= 0; m--) {
c = c + pow(2, m);
d = d + pow(2, m);
cout << "? " << c << " " << d << '\n';
fflush(stdout);
cin >> k;
if (k == 0) break;
if (k != j && k != 0) {
v[m] = 1;
if (j == 1) {
a = a + pow(2, m);
c = c - pow(2, m);
} else {
b = b + pow(2, m);
d -= pow(2, m);
}
cout << "? " << c << " " << d << '\n';
fflush(stdout);
cin >> k;
j = k;
}
}
c = a;
d = b;
for (i = 0; i <= 29; i++) {
if (v[i] == 1) continue;
c += pow(2, i);
cout << "? " << c << " " << d << '\n';
fflush(stdout);
cin >> k;
if (k == -1) {
a += pow(2, i);
b += pow(2, i);
}
c -= pow(2, i);
}
cout << "! " << a << " " << b << '\n';
fflush(stdout);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
template <typename T>
using Array = vector<T>;
template <typename T>
using Matrix = Array<Array<T>>;
using Graph = Matrix<int>;
template <typename T>
bool relax(T& r, const T& first) {
if (first < r) {
r = first;
return true;
}
return false;
}
template <typename T>
bool heavy(T& h, const T& first) {
if (h < first) {
h = first;
return true;
}
return false;
}
const int DX[8] = {-1, +1, +0, +0, +1, -1, +1, -1};
const int DY[8] = {+0, +0, -1, +1, +1, -1, -1, +1};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll n, s;
cin >> n >> s;
int ans = s / n;
s %= n;
if (s) ans++;
cout << ans;
return false;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
char m[2005][2005];
int cnt[2005][2005];
int row, col;
struct Node {
int x, y;
};
queue<Node> q;
bool check(int a, int b) {
if (a < 0 || b < 0 || a >= row || b >= col) return false;
return true;
}
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
char s[2][4];
int main() {
memset(cnt, 0, sizeof(cnt));
s[0][0] = 'v';
s[1][0] = '^';
s[0][1] = '<';
s[1][1] = '>';
s[0][2] = '^';
s[1][2] = 'v';
s[0][3] = '>';
s[1][3] = '<';
scanf("%d%d", &row, &col);
for (int i = 0; i < row; ++i) scanf("%s", m[i]);
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j) {
if (m[i][j] == '.') {
for (int d = 0; d < 4; ++d) {
int nextx = i + dx[d];
int nexty = j + dy[d];
if (check(nextx, nexty) && m[nextx][nexty] == '.') cnt[i][j]++;
}
if (cnt[i][j] == 1) {
Node node;
node.x = i;
node.y = j;
q.push(node);
}
}
}
bool ok = true;
while (!q.empty() && ok) {
Node cur = q.front();
q.pop();
int curx = cur.x;
int cury = cur.y;
if (m[curx][cury] != '.') continue;
for (int i = 0; i < 4; ++i) {
int nextx = curx + dx[i];
int nexty = cury + dy[i];
if (check(nextx, nexty) && m[nextx][nexty] == '.') {
m[curx][cury] = s[0][i];
m[nextx][nexty] = s[1][i];
for (int j = 0; j < 4; ++j) {
int nnx = nextx + dx[j];
int nny = nexty + dy[j];
if (check(nnx, nny) && m[nnx][nny] == '.') {
cnt[nnx][nny]--;
if (cnt[nnx][nny] == 0) {
ok = false;
}
if (cnt[nnx][nny] == 1) {
Node node;
node.x = nnx;
node.y = nny;
q.push(node);
}
}
}
}
}
}
for (int i = 0; i < row; ++i)
for (int j = 0; j < col; ++j)
if (m[i][j] == '.') ok = false;
if (ok) {
for (int i = 0; i < row; ++i) {
printf("%s\n", m[i]);
}
} else {
printf("Not unique\n");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int i, x, y, n, iCount, s, a[200009], b[200009], result1[200009],
result2[200009];
struct compare {
bool operator()(int x, int y) {
if (a[x] != a[y]) return a[x] > a[y];
if (x != y) return x > y;
return false;
}
};
set<int, compare> q;
int main() {
scanf("%d%d", &n, &s);
for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (i = 1; i <= n; ++i)
if (a[i] != 0) q.insert(i);
iCount = 0;
while (!q.empty()) {
x = *q.begin();
q.erase(q.begin());
b[0] = 0;
for (i = 0; i < a[x]; ++i)
if (q.empty()) {
iCount = -1;
break;
} else {
y = *q.begin();
q.erase(q.begin());
b[++b[0]] = y;
--a[y];
result1[++iCount] = x;
result2[iCount] = y;
}
if (iCount == -1) break;
for (i = 1; i <= b[0]; ++i)
if (a[b[i]] != 0) q.insert(b[i]);
}
if (iCount == -1)
cout << "No" << endl;
else {
cout << "Yes" << endl;
printf("%d\n", iCount);
for (i = 1; i <= iCount; ++i) printf("%d %d\n", result1[i], result2[i]);
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
void lucky() {
int n, m;
cin >> n >> m;
pair<int, vector<int>> p[m];
for (int i = 0; i < m; i++) {
p[i].first = 0;
}
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int j = 0; j < s.size(); j++) {
int a = s[j] - '0';
if (p[j].first < a) {
p[j].first = a;
p[j].second.clear();
p[j].second.push_back(i);
} else if (p[j].first == a) {
p[j].second.push_back(i);
}
}
}
set<int> s;
for (int i = 0; i < m; i++) {
for (auto j : p[i].second) s.insert(j);
}
cout << s.size();
}
int main() {
lucky();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool g[2001][2001];
bool visit[2001] = {false}, test[2001] = {false};
int n, m = 0;
int DFS(int x, int depth) {
visit[x] = true;
m = max(m, depth);
bool tag = false;
for (int i = 1; i <= n; i++) {
if (g[x][i]) {
DFS(i, depth + 1);
tag = true;
}
}
if (!tag) return depth;
}
int main() {
int i, j, x;
scanf("%d", &n);
for (i = 0; i <= n; i++) g[i][i] = false;
for (i = 1; i <= n; i++) {
scanf("%d", &x);
if (x > 0)
g[x][i] = true;
else
test[i] = true;
}
for (i = 1; i <= n; i++) {
if (!visit[i] && test[i]) DFS(i, 0);
}
cout << m + 1 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 105, maxs = 10005;
inline int gi() {
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int sum = 0;
while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
return sum;
}
int n, x, a[maxn];
double C[maxn][maxn], f[maxn][maxs];
int main() {
n = gi();
x = gi();
for (int i = 0; i <= n; ++i)
for (int j = C[i][0] = 1; j <= i; ++j)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
f[0][0] = 1;
int sum = 0;
for (int a, i = 1; i <= n; ++i) {
a = gi();
for (int j = i - 1; j >= 0; --j)
for (int k = sum; k >= 0; --k) f[j + 1][k + a] += f[j][k];
sum += a;
}
double ans = sum;
for (int i = 1; i <= n; ++i)
for (int j = 0; j <= sum; ++j) {
double val = (1. * n / i + 1) * x / 2, p = 1. * j / i;
if (val <= p) ans -= (p - val) / C[n][i] * f[i][j];
}
printf("%.9lf\n", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int a, k;
string t;
int main() {
cin.tie(0);
cout.tie(0);
cin.sync_with_stdio(0);
cout.sync_with_stdio(0);
;
int z;
cin >> z;
for (; z; z--) {
cin >> a;
set<int> s;
for (int q = 0; q < a; q++) {
int x;
cin >> x;
s.insert(x);
}
a = s.size();
vector<int> m(a);
int u = 0, o = 0, c = 1;
for (int i : s) m[u++] = i;
sort(m.begin(), m.end());
o = m.back();
for (int q = a - 1; q >= 0; q--) {
for (int w = q - 1; w >= 0; w--) {
if (m[q] + m[w] + (w == 0 ? 0 : m[w - 1]) <= o) break;
if (m[q] % m[w] == 0) continue;
o = max(m[q] + m[w], o);
for (int e = w - 1; e >= 0; e--) {
if (m[q] + m[w] + m[e] <= o) break;
if (m[q] % m[e] == 0 || m[w] % m[e] == 0) continue;
if (m[q] + m[w] + m[e] > o) {
o = m[q] + m[w] + m[e];
break;
}
}
}
}
cout << o << "\n";
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using dd = double;
using ldd = long double;
using si = short int;
using usi = unsigned short int;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
ll ans = 0;
ll x, f;
vector<ll> arr(n);
for (int i = 0; i < n; ++i) cin >> arr[i];
cin >> x >> f;
x += f;
for (int i = 0; i < n; ++i) {
if (arr[i] % x == 0) {
ans += arr[i] / x;
} else {
ans += arr[i] / x;
arr[i] %= x;
if (arr[i] > x - f) ++ans;
}
0;
0;
}
cout << ans * f;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool f = 1;
int main() {
int n, m, t, k;
cin >> n >> m;
set<int> s;
for (int i = 1; i <= m; i++) {
cin >> t;
s.clear();
for (int j = 1; j <= t; j++) {
cin >> k;
s.insert(k);
if (s.find(-k) != s.end()) {
f = 0;
}
}
if (f) {
cout << "YES";
return 0;
} else
f = 1;
}
cout << "NO";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int t[5005], n, used[5005], ret[5005];
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> t[i];
}
for (int i = 0; i < n; ++i) {
memset(used, 0, sizeof used);
used[t[i]]++;
int mx = t[i];
ret[t[i]]++;
for (int j = i + 1; j < n; ++j) {
used[t[j]]++;
if (used[t[j]] > used[mx] || (used[t[j]] == used[mx] && t[j] < mx))
mx = t[j];
ret[mx]++;
}
}
for (int i = 1; i <= n; ++i) cout << ret[i] << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m, ans = 0;
string s;
void go(vector<char> k, pair<int, int> start, vector<string> a) {
for (int i = 0; i < s.size(); i++) {
if (s[i] == k[0]) {
start.first--;
}
if (s[i] == k[1]) {
start.first++;
}
if (s[i] == k[2]) {
start.second--;
}
if (s[i] == k[3]) {
start.second++;
}
if (start.first >= n || start.first < 0) {
return;
}
if (start.second >= m || start.second < 0) {
return;
}
if (a[start.first][start.second] == '#') {
return;
}
if (a[start.first][start.second] == 'E') {
ans++;
return;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
pair<int, int> start;
vector<string> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
for (int j = 0; j < m; j++) {
if (a[i][j] == 'S') {
start.first = i;
start.second = j;
}
}
}
cin >> s;
vector<char> k = {'0', '1', '2', '3'};
do {
go(k, start, a);
} while (next_permutation(k.begin(), k.end()));
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
int n, m;
long long fact[1000060];
long long l25[100050];
char X[100050];
long long inv[100050];
int ans[450][100001];
long long calc(long long x, long long k) {
if (k == 0) return 1;
long long ans = 1;
while (k > 0) {
if (k & 1) {
ans *= x;
ans %= mod;
}
x *= x;
x %= mod;
k >>= 1;
}
return ans;
}
int op;
int cnt = 0;
bool calcd[100050];
int pointx[100050];
void calcx(int x) {
pointx[x] = ++cnt;
for (int i = 1; i <= 100000; ++i) {
if (i < x)
ans[cnt][i] = 0;
else {
ans[cnt][i] = ((long long)ans[cnt][i - 1] * 26 +
l25[i - x] % mod * fact[i - 1] % mod * inv[x - 1] % mod *
inv[(i - 1) - (x - 1)] % mod) %
mod;
}
}
}
int main() {
memset(ans, 0, sizeof(ans));
scanf("%d", &m);
fact[0] = 1;
inv[0] = 1;
l25[0] = 1;
scanf("%s", X + 1);
for (int i = 1; i <= 100005; ++i) {
l25[i] = l25[i - 1] * 25 % mod;
fact[i] = fact[i - 1] * i % mod;
inv[i] = calc(fact[i], mod - 2);
}
int len = strlen(X + 1);
calcx(len);
for (int i = 1; i <= m; ++i) {
scanf("%d", &op);
if (op == 1) {
scanf(" %s", X + 1);
len = strlen(X + 1);
if (!calcd[len]) {
calcx(len);
calcd[len] = 1;
}
} else {
scanf("%d", &op);
if (op < len)
printf("%d\n", 0);
else
printf("%d\n", ans[pointx[len]][op]);
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = static_cast<long long>(1e5) + 100;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long oo = 0x3f3f3f3f;
long long read() {
long long s = 0, f = 0;
char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
static vector<pair<long long, long long> > edge[MAXN * 10];
static long long n, q, s, cnt, treeOut[MAXN << 2], treeIn[MAXN << 2];
inline void build(long long k, long long l, long long r) {
if (l == r) {
treeOut[k] = l;
treeIn[k] = l;
return;
}
long long mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
treeOut[k] = ++cnt;
treeIn[k] = ++cnt;
edge[treeOut[k << 1]].push_back(make_pair(treeOut[k], 0));
edge[treeOut[k << 1 | 1]].push_back(make_pair(treeOut[k], 0));
edge[treeIn[k]].push_back(make_pair(treeIn[k << 1], 0));
edge[treeIn[k]].push_back(make_pair(treeIn[k << 1 | 1], 0));
}
inline void updateIn(long long k, long long l, long long r, long long L,
long long R, long long from, long long cost) {
if (L <= l && r <= R) {
edge[from].push_back((make_pair(treeIn[k], cost)));
return;
}
long long mid = (l + r) >> 1;
if (L <= mid) updateIn(k << 1, l, mid, L, R, from, cost);
if (mid < R) updateIn(k << 1 | 1, mid + 1, r, L, R, from, cost);
}
inline void updateOut(long long k, long long l, long long r, long long L,
long long R, long long from, long long cost) {
if (L <= l && r <= R) {
edge[treeOut[k]].push_back((make_pair(from, cost)));
return;
}
long long mid = (l + r) >> 1;
if (L <= mid) updateOut(k << 1, l, mid, L, R, from, cost);
if (mid < R) updateOut(k << 1 | 1, mid + 1, r, L, R, from, cost);
}
struct heapnode {
long long pos, dis;
bool operator<(heapnode right) const { return dis > right.dis; }
};
static long long dis[MAXN * 10];
static priority_queue<heapnode> heap;
inline void dij() {
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
heapnode tmp;
tmp.dis = 0, tmp.pos = s;
heap.push(tmp);
while (!heap.empty()) {
heapnode now = heap.top();
heap.pop();
if (dis[now.pos] != now.dis) continue;
for (auto &i : edge[now.pos]) {
if (i.first == 0) continue;
if (i.second + now.dis < dis[i.first]) {
dis[i.first] = i.second + now.dis;
tmp.pos = i.first, tmp.dis = dis[i.first];
heap.push(tmp);
}
}
}
}
signed main() {
n = read(), q = read(), s = read();
cnt = n;
build(1, 1, n);
for (long long i = 1; i <= q; ++i) {
long long com = read();
if (com == 1) {
long long from = read(), first = read(), second = read();
edge[from].push_back((make_pair(first, second)));
} else {
if (com == 2) {
long long from = read(), l = read(), r = read(), second = read();
updateIn(1, 1, n, l, r, from, second);
} else {
long long from = read(), l = read(), r = read(), second = read();
updateOut(1, 1, n, l, r, from, second);
}
}
}
dij();
for (long long i = 1; i <= n; ++i)
printf("%lld ", (dis[i] == INF) ? -1 : dis[i]);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int str[1005], cnt = 0, zero = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &str[i]);
if (str[i] == 5) {
cnt++;
} else {
zero++;
}
}
if (cnt > 9) {
cnt = cnt - cnt % 9;
}
if (cnt <= 8) {
if (zero == 0) {
printf("-1\n");
} else
printf("0\n");
} else if (zero == 0) {
printf("-1\n");
} else {
for (int i = 0; i < cnt; i++) printf("5");
for (int i = 0; i < zero; i++) printf("0");
printf("\n");
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int max_base = 32;
long long val[32 * maxn];
int ch[32 * maxn][2];
int tol = 0;
int a[maxn];
void init() {
tol = 1;
ch[0][0] = ch[1][0] = 0;
}
void insert(long long x) {
int u = 0;
for (int i = max_base; i >= 0; i--) {
int v = (x >> i & 1);
if (!ch[u][v]) {
ch[tol][1] = ch[tol][0] = 0;
val[tol] = 0;
ch[u][v] = tol++;
}
u = ch[u][v];
}
val[u] = x;
}
int query(long long x) {
int u = 0;
int ans = 0;
for (int i = max_base; i >= 0; i--) {
int v = (x >> i) & 1;
if (ch[u][0] && ch[u][1]) ans += (1 << i);
u = ch[u][v];
}
return ans;
}
int main() {
init();
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
insert(a[i]);
}
int ans = INT_MAX;
for (int i = 0; i < n; i++) ans = min(ans, query(a[i]));
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long h[100005], a[100005], home[100005], away[100005], tmp;
int main() {
long long n, i;
map<long long, long long> mp;
while (cin >> n) {
mp.clear();
for (i = 0; i < n; i++) {
cin >> h[i] >> a[i];
home[i] = (n * (n - 1)) / n;
away[i] = 0;
mp[h[i]]++;
}
for (i = 0; i < n; i++) {
tmp = mp[a[i]];
home[i] += tmp;
away[i] = (n - tmp) - 1;
cout << home[i] << " " << away[i] << "\n";
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
string a[10010];
bool vis[10010];
string p;
bool cmp(string x, string y) { return x + p + y < y + p + x; }
int main() {
int n;
cin >> n;
int l = 0;
for (int i = 0; i < n; i++) cin >> a[i], l += a[i].length();
cin >> p;
sort(a, a + n, cmp);
l = l / (n / 2);
for (int i = 0; i < n; i++) {
if (!vis[i]) {
for (int j = i + 1; j < n; j++) {
if (!vis[j] && a[i].length() + a[j].length() == l) {
cout << a[i] << p << a[j] << endl;
vis[j] = 1;
break;
}
}
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int solve(int a, int b) {
if (a - b == 0) return printf("infinity\n");
if (a - b < 0 || b > a - b) return printf("0\n");
int i = 2, n = a - b, res = 1;
while (i < n && i * i <= (a - b) && n > 1) {
int rr = 0;
while (n % i == 0) {
rr++;
n /= i;
}
res *= (rr + 1);
i++;
}
if (n > 1) res *= 2;
if (b > 0) res--;
i = 2;
n = a - b;
while (i * i <= n && i <= b) {
if (n % i == 0) {
res--;
res -= (n / i) <= b && n / i != i;
}
i++;
}
cout << res << endl;
}
int main() {
int a, b;
cin >> a >> b;
solve(a, b);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[1001], i;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
for (i = 1; i <= n; i++) {
bool f[1001] = {};
f[i] = 1;
int j = i;
while (true) {
j = a[j];
if (f[j] == 1) {
cout << j << " ";
break;
} else
f[j] = 1;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i] &= 1;
}
stack<int> st;
for (int i = 0; i < n; i++) {
if (!st.empty() && st.top() == a[i])
st.pop();
else
st.push(a[i]);
}
if (st.size() <= 1)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("03")
const int N = 2e5 + 5;
const long long INF = 1e18;
int n, u, v, check[N], child[N];
long long a[N], ans, dist[N], sum[N], prefSum[N], sufSum[N];
vector<int> adj[N];
struct line {
long long a, b;
mutable long long p;
bool type;
bool operator<(const line& tmp) const {
if (tmp.type == 0) {
return a < tmp.a || (a == tmp.a && b < tmp.b);
} else {
return p < tmp.p;
}
}
};
struct Convexhull : multiset<line> {
long long div(long long x, long long y) {
return x / y - ((x ^ y) < 0 && x % y);
}
bool inters(iterator x, iterator y) {
if (y == end()) {
x->p = INF;
return 0;
}
if (x->a == y->a) {
x->p = (x->b > y->b ? INF : -INF);
} else {
x->p = div(y->b - x->b, x->a - y->a);
}
return x->p >= y->p;
}
void add(long long s, long long c) {
auto it = insert({s, c, 0, 0});
auto it0 = it, it1 = it;
it1++;
while (inters(it, it1)) {
it1 = erase(it1);
}
if (it0 != begin() && inters(--it0, it)) {
inters(it0, it = erase(it));
}
while ((it = it0) != begin() && (--it0)->p >= it->p) {
inters(it0, erase(it));
}
}
long long get(long long x) {
if (empty()) return -INF;
auto tmp = *lower_bound({0, 0, x, 1});
return tmp.a * x + tmp.b;
}
} convex;
void build(int u, int p) {
child[u] = 1;
for (int v : adj[u]) {
if (v != p && check[v] == 0) {
build(v, u);
child[u] += child[v];
}
}
}
int findCen(int u, int p, int sz) {
for (int v : adj[u]) {
if (v != p && check[v] == 0 && child[v] * 2 > sz) {
return findCen(v, u, sz);
}
}
return u;
}
void dfs1(int u, int p) {
dist[u] = dist[p] + 1;
sum[u] = sum[p] + a[u];
sufSum[u] = sufSum[p] + (dist[u] + 1) * a[u];
ans = max(ans, sufSum[u]);
ans = max(ans, sufSum[u] + convex.get(sum[u]));
for (int v : adj[u]) {
if (v != p && check[v] == 0) {
dfs1(v, u);
}
}
}
void dfs2(int u, int p) {
if (u != p) {
sum[u] = sum[p] + a[u];
prefSum[u] = prefSum[p] + sum[u];
}
ans = max(ans, prefSum[u] + a[u] * (dist[u] + 1));
convex.add(dist[u], prefSum[u]);
for (int v : adj[u]) {
if (v != p && check[v] == 0) {
dfs2(v, u);
}
}
}
void solve(int u) {
convex.clear();
sum[u] = prefSum[u] = sufSum[u] = a[u];
dist[u] = 0;
for (int v : adj[u]) {
if (check[v] == 0) {
dfs1(v, u);
sum[v] = prefSum[v] = a[v];
dfs2(v, v);
}
}
}
void centroid(int root) {
build(root, root);
int u = findCen(root, root, child[root]);
check[u] = 1;
ans = max(ans, a[u]);
solve(u);
reverse(adj[u].begin(), adj[u].end());
solve(u);
for (int v : adj[u]) {
if (check[v] == 0) {
centroid(v);
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) cin >> a[i];
centroid(1);
cout << ans;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int w, l;
int a[100005];
int sum[100005];
int main() {
scanf("%d%d", &w, &l);
for (int i = 1; i <= w - 1; i++) {
scanf("%d", &a[i]);
sum[i] = sum[i - 1] + a[i];
}
int ans = 0x3f3f3f3f;
for (int i = 1; i <= w - l; i++) ans = min(ans, sum[i + l - 1] - sum[i - 1]);
printf("%d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
string s;
void solve() {
string a, b;
for (int i = 0; i < s.length(); i++) {
if (s[i] & 1)
a += s[i];
else
b += s[i];
}
for (int i = 0, pa = 0, pb = 0; i < s.length(); i++) {
if (pa == a.length() || pb != b.length() && b[pb] < a[pa])
cout << b[pb++];
else
cout << a[pa++];
}
cout << endl;
}
int main() {
int Q;
cin >> Q;
while (Q--) {
cin >> s;
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a;
cin >> a;
cout << a;
reverse(a.begin(), a.end());
cout << a;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, d, l;
void calc(vector<int>& v, int x, int c) {
v.assign(c, 1);
int sum = c;
int pos = 0;
while (sum < x) {
if (x - sum < l) {
v[pos] += x - sum;
return;
} else {
v[pos++] = l;
sum += l - 1;
}
}
}
int main() {
cin >> n >> d >> l;
int c1 = n - n / 2;
int c2 = n / 2;
if (!(c1 - c2 * l <= d && d <= c1 * l - c2)) {
cout << -1;
return 0;
}
int s1, s2;
for (int i = c1; i <= c1 * l; i++) {
int cur = -(d - i);
if (c2 <= cur && cur <= c2 * l) {
s1 = i;
s2 = s1 - d;
break;
}
}
vector<int> v1, v2;
calc(v1, s1, c1);
calc(v2, s2, c2);
int pos1 = 0, pos2 = 0;
for (int i = 1; i <= n; i++)
if (i & 1)
cout << v1[pos1++] << ' ';
else
cout << v2[pos2++] << ' ';
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long int n;
cin >> n;
int ans = n % 2 == 0 ? (n / 2) - 1 : (n - 1) / 2;
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int read() {
static int ch, x;
while ((ch = getchar()) < 48) {
}
x = ch ^ 48;
while ((ch = getchar()) >= 48) x = (((x << 2) + x) << 1) + (ch ^ 48);
return x;
}
const int SIG = 27;
char s[100010];
int a[100010], sum[100010];
int pre[100010][SIG], suf[100010][SIG], pans[100010][SIG], sans[100010][SIG];
bool vis[SIG];
std::pair<int, int> st[SIG + 10];
int main() {
scanf("%s", s + 2);
const int n = strlen(s + 2) + 2;
s[1] = s[n] = 26 + 'a';
for (int i = 1; i <= n; ++i) {
a[i] = s[i] - 'a';
for (int j = 0; j < SIG; ++j) pre[i][j] = pre[i - 1][j];
pre[i][a[i]] = i;
}
for (int i = n; i >= 1; --i) {
for (int j = 0; j < SIG; ++j) suf[i][j] = suf[i + 1][j];
suf[i][a[i]] = i;
}
for (int i = n; i >= 1; --i) {
int top = 0;
for (int j = 0; j < SIG; ++j) {
if (const int t = suf[i][j]) st[++top] = std::make_pair(t, j);
}
std::sort(st + 1, st + top + 1);
const int t = suf[i + 1][a[i]];
if (t && t < n) {
sum[i] = pans[i + 1][a[i]];
sum[i] ^= sum[t];
}
for (int j = 1; j <= top; ++j) {
const int pos = st[j].first, col = st[j].second;
for (int k = 0; k < SIG; ++k) vis[k] = false;
for (int k = 1; k < j; ++k) {
const int p = st[k].second, u = pre[pos][p];
int res = pans[i][p] ^ sum[suf[i][p]] ^ sum[u];
if (u) res ^= pans[u + 1][col];
if (res < SIG) vis[res] = true;
}
int &res = pans[i][col];
while (res < SIG && vis[res]) ++res;
}
for (int j = 0; j < SIG; ++j)
if (!suf[i][j]) pans[i][j] = pans[i][SIG - 1];
}
for (int i = 1; i <= n; ++i) {
int top = 0;
for (int j = 0; j < SIG; ++j) {
if (const int t = pre[i][j]) st[++top] = std::make_pair(t, j);
}
std::sort(st + 1, st + top + 1, std::greater<std::pair<int, int>>());
for (int j = 1; j <= top; ++j) {
const int pos = st[j].first, col = st[j].second;
for (int k = 0; k < SIG; ++k) vis[k] = false;
for (int k = 1; k < j; ++k) {
const int p = st[k].second, u = suf[pos][p];
int res = sans[i][p] ^ sum[pre[i][p]] ^ sum[u];
if (u <= n) res ^= sans[u - 1][col];
if (res < SIG) vis[res] = true;
}
int &res = sans[i][col];
while (res < SIG && vis[res]) ++res;
}
for (int j = 0; j < SIG; ++j)
if (!pre[i][j]) sans[i][j] = sans[i][SIG - 1];
}
for (int m = read(); m--;) {
const int l = read() + 1, r = read() + 1;
bool flag = false;
for (int i = 0; i < SIG; ++i) {
const int t = suf[l][i];
if (t && t <= r) {
const int u = pre[r][i];
int res = pans[l][i] ^ sum[t] ^ sum[u];
if (u) res ^= sans[r][i];
if (!res) {
flag = true;
break;
}
}
}
puts(flag ? "Alice" : "Bob");
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
const int N = 3e5 + 100;
set<pair<int, int> > s[N];
int a[N];
int nxt[N];
int dp[N];
int mol[N];
int n;
void add(long long int v, long long int u) {
long long int m = mol[u];
if (m != n + 2) s[m].insert({a[v], v});
mol[v] = mol[u];
}
int tt;
void Main(long long int oo) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
nxt[i] = n + 2;
s[i].clear();
dp[i] = 0;
}
s[n + 1].clear();
s[n + 2].clear();
a[n + 1] = -1;
a[n + 2] = -1;
dp[n + 2] = 0;
dp[n + 1] = 0;
nxt[n + 2] = n + 2;
mol[n + 2] = n + 2;
nxt[n + 1] = n + 1;
for (int i = 1; i <= n + 1; i++) {
mol[i] = n + 1;
}
vector<int> pashm;
for (int i = n; i; i--) {
if (a[i] == a[i + 1]) {
nxt[i] = i + 2;
add(i, i + 2);
} else {
long long int z = mol[nxt[i + 1]];
if (z == n + 3) z = n + 2;
auto y = s[z].lower_bound({a[i], 0});
if (y != s[z].end() && y->first == a[i]) {
long long int p = y->second;
nxt[i] = p + 1;
add(i, p + 1);
} else {
mol[i] = i;
nxt[i] = n + 2;
add(i, i);
}
}
}
long long int ans = 0;
for (int i = n; i; i--) {
if (nxt[i] != n + 2) {
dp[i] = 1 + dp[nxt[i]];
}
ans += dp[i];
}
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> tt;
for (int i = 1; i <= tt; i++) Main(i);
}
| 9 |
#include <bits/stdc++.h>
char r[2000 + 1];
int v[2000 + 1];
int main(void) {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < n; i++) {
scanf("%s", r);
for (int j = 0; j < m; j++)
if (r[j] == 'U' && i % 2 == 0)
v[j]++;
else if (r[j] == 'L' && j >= i)
v[j - i]++;
else if (r[j] == 'R' && j + i < m)
v[j + i]++;
}
for (int j = 0; j < m; j++) printf("%d ", v[j]);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, hh, p = 1e9 + 7;
long long ksm(long long x, long long y) {
int xlh = 1;
while (y) {
if (y & 1) xlh = xlh * x % p;
x = x * x % p;
y /= 2;
}
return xlh;
}
int main() {
scanf("%lld%lld", &n, &m);
hh = ksm(2 * (n + 1), m);
hh = hh * (n + 1 - m) % p;
hh = hh * ksm(n + 1, p - 2) % p;
printf("%lld", hh);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long MXN = 2e5 + 7;
const long long MOD = 1e9 + 7;
void allday() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void relax() {
freopen("txt.in", "w", stdin);
freopen("txt,out", "r", stdout);
}
int n, a[MXN], f[MXN], ans[MXN];
vector<pair<int, int>> v;
void add(int id, int val) {
while (id <= n) {
f[id] += val;
id = (id | (id + 1));
}
}
long long sum(int id) {
long long sum = 0;
while (id > 0) {
sum += f[id];
id = (id & (id + 1)) - 1;
}
return sum;
}
int main() {
allday();
cin >> n;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
v.push_back({a, i});
}
sort(v.begin(), v.end());
for (pair<int, int> p : v) {
int i = p.second;
for (int k = 1; k * (i - 1) + 2 <= n && k <= n; k++) {
int L = (k * (i - 1) + 2);
int R = min((k * i + 1), n);
ans[k] += sum(R) - sum(L - 1);
}
add(i, 1);
}
for (int i = 1; i < n; i++) {
cout << ans[i] << ' ';
}
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
void chkmin(T1 &x, T2 y) {
if (x > y) x = y;
}
template <typename T1, typename T2>
void chkmax(T1 &x, T2 y) {
if (x < y) x = y;
}
namespace fastio {
char rbuf[1 << 23], *p1 = rbuf, *p2 = rbuf, wbuf[1 << 23], *p3 = wbuf;
inline char getc() {
return p1 == p2 &&
(p2 = (p1 = rbuf) + fread(rbuf, 1, 1 << 23, stdin), p1 == p2)
? -1
: *p1++;
}
inline void putc(char x) { (*p3++ = x); }
template <typename T>
void read(T &x) {
x = 0;
char c = getchar();
T neg = 0;
while (!isdigit(c)) neg |= !(c ^ '-'), c = getchar();
while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
if (neg) x = (~x) + 1;
}
template <typename T>
void recursive_print(T x) {
if (!x) return;
recursive_print(x / 10);
putc(x % 10 ^ 48);
}
template <typename T>
void print(T x) {
if (!x) putc('0');
if (x < 0) putc('-'), x = ~x + 1;
recursive_print(x);
}
template <typename T>
void print(T x, char c) {
if (!x) putc('0');
if (x < 0) putc('-'), x = ~x + 1;
recursive_print(x);
putc(c);
}
void print_final() { fwrite(wbuf, 1, p3 - wbuf, stdout); }
} // namespace fastio
const int MAXN = 5e4;
const int LOG_V = 30;
const int MAXP = MAXN * 31;
const int MOD = 1e9 + 7;
const int INV2 = 5e8 + 4;
int n, a[MAXN + 5];
long long m;
struct node {
int ch[2], siz;
short cnt[31][2];
} s[MAXP + 5];
int ncnt, rt;
void insert(int &k, int d, int v) {
if (!k) k = ++ncnt;
s[k].siz++;
for (int i = 0; i <= LOG_V; i++) s[k].cnt[i][v >> i & 1]++;
if (!~d) return;
insert(s[k].ch[v >> d & 1], d - 1, v);
}
int query(int k, int d, int x, int y) {
if (!k) return 0;
if (!~d) return s[k].siz;
int dx = x >> d & 1, dy = y >> d & 1;
if (dy)
return query(s[k].ch[dx ^ 1], d - 1, x, y);
else
return s[s[k].ch[dx ^ 1]].siz + query(s[k].ch[dx], d - 1, x, y);
}
bool check(int mid) {
long long sum = 0;
for (int i = 1; i <= n; i++) sum += query(rt, LOG_V, a[i], mid);
return sum >= m;
}
int getsum(int k, int d, int x, int y) {
if (!k) return 0;
if (!~d) return 1ll * y * s[k].siz % MOD;
int dx = x >> d & 1, dy = y >> d & 1;
if (dy)
return getsum(s[k].ch[dx ^ 1], d - 1, x, y);
else {
int sum = getsum(s[k].ch[dx], d - 1, x, y);
sum =
(sum + 1ll * (y & ~((1 << d + 1) - 1)) * s[s[k].ch[dx ^ 1]].siz) % MOD;
sum = (sum + 1ll * (1 << d) * s[s[k].ch[dx ^ 1]].siz) % MOD;
for (int i = d - 1; ~i; i--)
sum = (sum + 1ll * (1 << i) * (s[s[k].ch[dx ^ 1]].cnt[i][~x >> i & 1])) %
MOD;
return sum;
}
}
int main() {
scanf("%d%lld", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) insert(rt, LOG_V, a[i]);
m <<= 1;
int l = 0, r = (1 << 30) - 1, p = 0;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid))
p = mid, l = mid + 1;
else
r = mid - 1;
}
int ans = 0;
long long sum = 0;
for (int i = 1; i <= n; i++) ans = (ans + getsum(rt, LOG_V, a[i], p)) % MOD;
for (int i = 1; i <= n; i++) sum += query(rt, LOG_V, a[i], p);
ans = (ans - 1ll * (sum - m) * p % MOD + MOD) % MOD;
printf("%d\n", 1ll * ans * INV2 % MOD);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
struct Vote {
int a, b, rk;
bool operator<(const Vote& x) const { return b < x.b; }
} vote[MAXN];
int n, m;
vector<int> v[MAXN];
int check(int k) {
int res = 0, j = 0;
for (int i = 1; i <= m; i++)
if (v[i].size() >= k) {
j += v[i].size() - k + 1;
res += v[i][v[i].size() - k];
}
for (int i = 1; i <= n && j + v[0].size() < k; i++)
if (vote[i].a &&
(v[vote[i].a].size() < k || vote[i].rk > v[vote[i].a].size() - k)) {
j++;
res += vote[i].b;
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &vote[i].a, &vote[i].b);
m = max(m, vote[i].a);
}
sort(vote + 1, vote + n + 1);
for (int i = 1; i <= n; i++) {
int tar = vote[i].a;
vote[i].rk = v[tar].size();
v[tar].push_back(vote[i].rk ? v[tar][vote[i].rk - 1] + vote[i].b
: vote[i].b);
}
int l = 1, r = n;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid) < check(mid + 1))
r = mid - 1;
else
l = mid + 1;
}
printf("%d", check(l));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-9, PI = acos(-1.);
const long long LINF = 0x3f3f3f3f3f3f3f3f, LMOD = (1ll << 60);
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int N = 1e5 + 5;
long long lvl[N];
long long getlvl(long long x) {
if (x <= 0) return 0;
long long ans = 1;
long long k = 1;
while ((2 * k) <= x) {
k <<= 1;
ans++;
}
return ans;
}
void add(long long l, long long k) {
long long v = k;
for (; l <= 60; l++) {
lvl[l] += v;
lvl[l] %= LMOD;
v *= 2;
}
}
long long q, t, x, k;
int main() {
scanf("%lld", &q);
while (q--) {
scanf("%lld%lld", &t, &x);
long long l = getlvl(x);
if (t == 1) {
scanf("%lld", &k);
lvl[l] += k;
lvl[l] %= LMOD;
} else if (t == 2) {
scanf("%lld", &k);
add(l, k);
} else if (t == 3) {
long long re = x + lvl[l];
re %= (1ll << l);
while (getlvl(re) > l) re -= (1ll << (l - 1));
while (getlvl(re) < l) re += (1ll << (l - 1));
for (int i = l; i >= 1; i--) {
x = re - lvl[i];
x %= (1ll << i);
while (getlvl(x) > i) x -= (1ll << (i - 1));
while (getlvl(x) < i) x += (1ll << (i - 1));
printf("%lld ", x);
re /= 2;
}
printf("\n");
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
bool ch(int a1, int a2, int a3, int a4) {
if (a1 > a2) swap(a1, a2);
if (a3 > a4) swap(a3, a4);
return (a1 <= a3 && a2 >= a3) || (a3 <= a2 && a4 >= a2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> vv(n), h, v;
for (int i = 0; i < n; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
vv[i] = {x1, y1, x2, y2, i};
if (x1 == x2)
v.push_back(vv[i]);
else
h.push_back(vv[i]);
}
if (h.size() > v.size()) swap(h, v);
vector<bitset<5000>> vb(h.size());
for (int i = 0; i < h.size(); i++) {
for (auto v1 : v) {
if (ch(h[i][0], h[i][2], v1[0], v1[2]) &&
ch(h[i][1], h[i][3], v1[1], v1[3])) {
vb[i][v1[4]] = 1;
}
}
}
long long ans = 0;
for (int i = 0; i < h.size(); i++) {
for (int j = i + 1; j < h.size(); j++) {
long long val = (vb[i] & vb[j]).count();
ans += val * (val - 1) / 2;
}
}
cout << ans;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int MN = 1e5 + 44;
int cou[MN];
vector<int> graph[MN];
long long a[MN], b[MN], c[MN], d[MN];
const int L = 3;
void getmax2(int x, int y, long long t[], pair<long long, int> res[]) {
for (int i = 0; i < L; ++i) res[i] = make_pair(0, -1);
for (auto v : graph[x])
if (y != v) {
auto add = make_pair(t[v], v);
for (int i = 0; i < L; ++i)
if (add > res[i]) swap(add, res[i]);
}
}
long long compose(pair<long long, int> A[], pair<long long, int> B[]) {
long long ret = 0;
for (int i = 0; i < L; ++i)
for (int k = 0; k < L; ++k)
if (A[i].second != B[k].second) ret = max(ret, A[i].first + B[k].first);
return ret;
}
long long compose3(pair<long long, int> A[], pair<long long, int> B[],
pair<long long, int> C[]) {
long long ret = 0;
for (int i = 0; i < L; ++i)
for (int k = 0; k < L; ++k)
for (int l = 0; l < L; ++l)
if (A[i].second != B[k].second && A[i].second != C[l].second &&
B[k].second != C[l].second)
ret = max(ret, A[i].first + B[k].first + C[l].first);
return ret;
}
void dfs(int x, int y = -1) {
for (auto v : graph[x])
if (y != v) dfs(v, x);
pair<long long, int> A[L], B[L], C[L], D[L];
getmax2(x, y, a, A);
getmax2(x, y, b, B);
getmax2(x, y, c, C);
getmax2(x, y, d, D);
a[x] = cou[x] + A[0].first;
b[x] = max(B[0].first, A[0].first + A[1].first + cou[x]);
c[x] = max(C[0].first + cou[x], compose(A, B) + cou[x]);
d[x] = max(max(D[0].first, B[0].first + B[1].first),
max(compose(A, C) + cou[x], compose3(A, A, B) + cou[x]));
}
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", cou + i);
for (int i = 0; i < n - 1; ++i) {
int a, b;
scanf("%d%d", &a, &b);
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(1);
printf("%I64d\n", d[1]);
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
vector<vector<int>> a(n, vector<int>(m));
int ans = 1e9;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> a[i][j];
if (n <= k) {
for (int msk = 0; msk < (1 << n); msk++) {
vector<int> x;
for (int i = 0; i < n; i++) {
int b = (msk >> i) & 1;
x.push_back(b);
}
vector<int> y(m);
for (int j = 0; j < m; j++) {
int cur = 0;
for (int i = 0; i < n; i++) {
if (a[i][j] == x[i]) cur++;
}
if (cur > n - cur)
y[j] = 0;
else
y[j] = 1;
}
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int b = x[i] ^ y[j];
if (a[i][j] != b) sum++;
}
}
if (sum <= k) ans = min(ans, sum);
}
} else {
for (int h = 0; h < m; h++) {
vector<int> x;
for (int j = 0; j < n; j++) x.push_back(a[j][h]);
vector<int> y(m);
for (int j = 0; j < m; j++) {
int cur = 0;
for (int i = 0; i < n; i++) {
if (a[i][j] == x[i]) cur++;
}
if (cur > n - cur)
y[j] = 0;
else
y[j] = 1;
}
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int b = x[i] ^ y[j];
if (a[i][j] != b) sum++;
}
}
if (sum <= k) ans = min(ans, sum);
}
}
if (ans == 1e9)
cout << -1;
else
cout << ans;
return 0;
}
| 7 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
using namespace std;
template <typename T>
void uin(T &a, T b) {
if (b < a) {
a = b;
}
}
template <typename T>
void uax(T &a, T b) {
if (b > a) {
a = b;
}
}
const int M = 1001;
bitset<2 * M> dp, nl, kek, out;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
nl[i] = 1;
}
for (int i = m; i < 2 * m; ++i) {
out[i] = 1;
}
for (int i = 1; i <= n; ++i) {
int a;
cin >> a;
a %= m;
kek = (dp << a);
kek &= out;
kek >>= m;
dp = dp | ((dp << a) & nl);
dp = dp | kek;
dp[a] = 1;
}
if (dp[0])
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string a, b;
long long int N;
cin >> N;
long long int s = 0, h = 0;
for (long long int i = (0); i < (long long int)(N); ++i) {
cin >> a >> b;
if (b == "soft")
s++;
else
h++;
}
for (long long int i = (0); i < (long long int)(100); ++i) {
long long int lh = i * i / 2;
long long int ls = i * i / 2 + (i % 2);
if (s <= ls && h <= lh || s <= lh && h <= ls) {
cout << i << endl;
break;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, S;
int main() {
cin >> n >> S;
cout << (S - 1) / n + 1 << '\n';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
long long int res = 1;
a %= 1000000007;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007;
a = a * a % 1000000007;
}
return res;
}
long long int modInverse(long long int a) { return power(a, 1000000007 - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
void solve() {
long long int n;
cin >> n;
vector<long long int> v;
for (long long int i = 0; i < n / 2; i++) {
long long int c;
cin >> c;
v.push_back(c);
}
sort((v).begin(), (v).end());
long long int ans1 = 0;
long long int ans2 = 0;
int pos = 1;
for (int i = 0; i < v.size(); i++) {
ans1 += ((abs(v[i] - pos)));
pos += 2;
}
pos = 2;
for (int i = 0; i < v.size(); i++) {
ans2 += ((abs(v[i] - pos)));
pos += 2;
}
cout << min(ans1, ans2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
int t = 0;
while (t++ < T) {
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 210;
int n, vis[maxn][maxn], Q;
vector<int> belong[maxn];
vector<vector<int> > lines;
double val[maxn], ans;
struct point {
int x, y;
} e[maxn];
struct Mat {
double a[maxn][maxn];
void clear() {
memset(a, 0, sizeof(a));
for (int i = 1; i <= n; i++) a[i][i] = 1;
}
void init() { memset(a, 0, sizeof(a)); }
} a[maxn], b[maxn];
Mat operator*(Mat a, Mat b) {
Mat c;
c.init();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 1; k <= n; k++) c.a[i][j] += a.a[i][k] * b.a[k][j];
return c;
}
point operator-(point a, point b) {
point c;
c.x = a.x - b.x;
c.y = a.y - b.y;
return c;
}
int operator*(point a, point b) { return a.x * b.y - a.y * b.x; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d%d", &e[i].x, &e[i].y);
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
if (!vis[i][j]) {
vector<int> line;
for (int k = 1; k <= n; k++)
if ((e[k] - e[i]) * (e[k] - e[j]) == 0) line.push_back(k);
for (int k = 0; k < line.size(); k++)
belong[line[k]].push_back((int)lines.size());
for (int p = 0; p < line.size(); p++)
for (int q = 0; q < line.size(); q++) {
int y1 = line[p], y2 = line[q];
vis[y1][y2] = 1;
}
lines.push_back(line);
}
a[0].clear();
for (int i = 1; i <= n; i++)
for (int j = 0; j < belong[i].size(); j++) {
int t = belong[i][j];
double temp = 1.0 / belong[i].size() / lines[t].size();
for (int k = 0; k < lines[t].size(); k++) {
int v = lines[t][k];
a[1].a[i][v] += temp;
}
}
for (int i = 2; i <= 100; i++) a[i] = a[i - 1] * a[1];
b[0].clear();
b[1] = a[100];
for (int i = 2; i <= 100; i++) b[i] = b[i - 1] * b[1];
scanf("%d", &Q);
while (Q--) {
ans = 0.0;
int t, m;
scanf("%d%d", &t, &m);
m--;
memset(val, 0, sizeof(val));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
val[i] += b[m / 100].a[i][j] * a[m % 100].a[j][t];
for (int i = 0; i < lines.size(); i++) {
vector<int> t = lines[i];
double temp = 0.0;
for (int j = 0; j < t.size(); j++) temp += val[t[j]];
temp /= t.size();
ans = max(ans, temp);
}
printf("%.15lf\n", ans);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int f = 0, ans = 0;
for (int i = 0; i < n - 1; i++) {
if (a[i] == 1) {
if (a[i + 1] == 2)
ans += 3;
else
ans += 4;
} else if (a[i] == 2) {
if (a[i + 1] == 3)
f++;
else
ans += 3;
} else {
if (a[i + 1] == 2)
f++;
else {
ans += 4;
}
}
}
int cnt = 0;
for (int i = 0; i < n - 2; i++) {
if (a[i] == 3 and a[i + 1] == 1 and a[i + 2] == 2) cnt++;
}
if (f > 0)
cout << "Infinite";
else {
cout << "Finite" << '\n';
cout << ans - cnt;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, trs[4000005], trmn[4000005], trmx[4000005], id = 1, mxid = 1;
string second;
void upd(long long x, long long v, long long nd, long long nl, long long nr) {
if (nl > x || nr < x) return;
if (nl == nr) {
trmn[nd] = min(v, (long long)0);
trmx[nd] = max(v, (long long)0);
trs[nd] = v;
return;
}
long long md = (nl + nr) / 2;
upd(x, v, nd * 2, nl, md), upd(x, v, nd * 2 + 1, md + 1, nr);
trs[nd] = trs[nd * 2] + trs[nd * 2 + 1];
trmn[nd] = min(trmn[nd * 2], trs[nd * 2] + trmn[nd * 2 + 1]);
trmx[nd] = max(trmx[nd * 2], trs[nd * 2] + trmx[nd * 2 + 1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> second;
for (int i = 0; i < second.size(); i++) {
if (second[i] == 'L') {
if (id > 1) id -= 1;
} else if (second[i] == 'R')
id += 1;
else if (second[i] == '(')
upd(id, 1, 1, 1, n);
else if (second[i] == ')')
upd(id, -1, 1, 1, n);
else
upd(id, 0, 1, 1, n);
if (trs[1] == 0) {
if (trmn[1] < 0)
cout << -1 << " ";
else
cout << trmx[1] << " ";
} else
cout << -1 << " ";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m, maxR, fa[1010], ans;
struct data {
int u, v, l, r;
} e[3030];
inline bool cmp(data a, data b) { return a.l < b.l; }
inline int find(int k) { return fa[k] == k ? k : fa[k] = find(fa[k]); }
inline bool Judge(int MaxL, int MinR) {
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= MaxL; i++)
if (e[i].r >= MinR) {
fa[find(e[i].u)] = find(e[i].v);
if (find(1) == find(n)) return 1;
}
return 0;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d%d", &e[i].u, &e[i].v, &e[i].l, &e[i].r);
maxR = max(maxR, e[i].r);
}
sort(e + 1, e + m + 1, cmp);
for (int i = 1; i <= m; i++) {
int L = 0, R = maxR;
while (L <= R) {
int mid = (L + R) >> 1;
if (Judge(i, mid))
ans = max(ans, mid - e[i].l + 1), L = mid + 1;
else
R = mid - 1;
}
}
if (!ans)
printf("Nice work, Dima!");
else
printf("%d\n", ans);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long maxx = 1e9 + 10;
const long long mod = 1e9 + 7;
long long x;
long long y;
long long res;
map<long long, long long> f;
long long fp(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) {
res = res * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return res;
}
void calf(long long d) {
if (f.count(d)) return;
long long res = (fp(2, d - 1) + mod - f[1]) % mod;
for (long long i = 2; i * i <= d; i++) {
if (d % i == 0) {
calf(d / i);
res = (res + mod - f[d / i]) % mod;
if (d / i != i) {
calf(i);
res = (res + mod - f[i]) % mod;
}
}
}
f[d] = res;
}
int main() {
scanf("%I64d%I64d", &x, &y);
res = 0;
if (y % x == 0) {
long long d = y / x;
f[1] = 1;
calf(d);
res = f[d];
}
printf("%I64d\n", res);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10;
long long a[N];
int x[N];
int n, sum, ans;
void dfs(int k, int cnt, int groups) {
if (k == n) {
ans = min(ans, sum - cnt - (groups == 1));
return;
}
dfs(k + 1, cnt, groups);
for (int i = 0; i < k; i++) {
if (a[i] % a[k] == 0) {
a[i] /= a[k];
dfs(k + 1, cnt + x[k] + (!x[k]), groups - 1);
a[i] *= a[k];
}
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%lld", a + i);
sort(a, a + n);
reverse(a, a + n);
sum = n + 1;
for (int i = 0; i < n; i++) {
int cnt = 0;
long long tmp = a[i];
for (long long j = 2; j * j <= a[i] && tmp > 1; j++) {
while (tmp > 1 && tmp % j == 0) {
tmp /= j;
cnt++;
}
}
if (tmp > 1 && tmp != a[i]) cnt++;
x[i] = cnt;
sum += cnt;
}
ans = 123456789;
dfs(0, 0, n);
printf("%d\n", ans);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
bool check(int n, int pos) { return n & (1LL << pos); }
int Set(int n, int pos) { return n = n | (1LL << pos); }
struct scc {
int n;
int comp;
vector<vector<int> > g, rg;
vector<int> compno;
vector<bool> vis1;
vector<int> nodes;
void init(int _n) {
n = _n;
g.assign(n + 10, {});
rg.assign(n + 10, {});
comp = 0;
compno.assign(n + 10, 0);
vis1.assign(n + 10, 0);
nodes.clear();
}
void dfs1(int node) {
vis1[node] = 1;
for (auto it : g[node]) {
if (vis1[it] == 0) dfs1(it);
}
nodes.push_back(node);
}
void dfs2(int node) {
compno[node] = comp;
for (auto it : rg[node]) {
if (compno[it] == 0) dfs2(it);
}
}
void addedge(int u, int v) {
g[u].push_back(v);
rg[v].push_back(u);
}
void start() {
for (int i = 1; i <= n; i++) {
if (vis1[i] == 0) dfs1(i);
}
reverse(nodes.begin(), nodes.end());
for (auto it : nodes) {
if (compno[it] == 0) {
comp++;
dfs2(it);
}
}
}
};
int main() {
int i, j, k, l, m, n, o, r, q;
int testcase;
int input, flag, tag, ans;
scanf("%d", &testcase);
for (int cs = 1; cs <= testcase; cs++) {
scanf("%d", &n);
scanf("%d", &m);
scc Scc;
Scc.init(n);
for (i = 1; i <= m; i++) {
int u, v;
scanf("%d", &u);
scanf("%d", &v);
Scc.addedge(u, v);
}
vector<int> a, b;
Scc.start();
if (Scc.comp == 1)
printf("No\n");
else {
printf("Yes\n");
for (int i = 1; i <= n; i++) {
if (Scc.compno[i] == Scc.comp)
a.push_back(i);
else
b.push_back(i);
}
cout << a.size() << " " << b.size() << "\n";
for (auto it : a) cout << it << " ";
cout << "\n";
for (auto it : b) cout << it << " ";
cout << "\n";
}
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
int exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1, y = 0;
return a;
}
int g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
int n, m, cnta, cntb, a[100010], b[100010], cnt, cnt1;
long long ans;
vector<int> r[200020];
pair<int, int> q[200020];
map<int, bool> mpa, mpb;
int main() {
scanf("%d %d", &n, &m);
scanf("%d", &cnta);
for (int i = 1; i <= cnta; ++i) scanf("%d", &a[i]), mpa[a[i]] = 1;
scanf("%d", &cntb);
for (int i = 1; i <= cntb; ++i) scanf("%d", &b[i]), mpb[b[i]] = 1;
int g = gcd(n, m);
if (g > cnta + cntb) return printf("-1"), 0;
sort(a + 1, a + 1 + cnta);
sort(b + 1, b + 1 + cntb);
for (int i = 1; i <= cnta; ++i)
if (!mpb[a[i] % m]) ans = max(ans, 1ll * a[i]), mpb[a[i] % m] = 1;
for (int i = 1; i <= cntb; ++i)
if (!mpa[b[i] % n]) ans = max(ans, 1ll * b[i]), mpa[b[i] % n] = 1;
for (int i = 1; i <= cnta; ++i) r[a[i] % g].push_back(a[i]);
for (int i = 1; i <= cntb; ++i) r[b[i] % g].push_back(b[i]);
n /= g, m /= g;
int x, y;
exgcd(n, m, x, y);
(x -= x / m * m) < 0 ? x += m : 0;
(y -= y / n * n) < 0 ? y += n : 0;
for (int i = 0; i < g; ++i) {
if (!r[i].size()) return printf("-1"), 0;
cnt = 0;
cnt1 = 0;
for (auto p : r[i]) q[++cnt] = make_pair(1ll * p / g * x % m, p);
sort(q + 1, q + 1 + cnt);
for (int j = 1; j <= cnt; ++j)
if (j == 1 || q[j].first != q[cnt1].first) q[++cnt1] = q[j];
cnt = cnt1;
q[cnt + 1] = q[1];
q[cnt + 1].first += m;
for (int j = 1; j <= cnt; ++j)
if (q[j + 1].first - q[j].first > 1)
ans = max(
ans, 1ll * n * g * (q[j + 1].first - q[j].first - 1) + q[j].second);
cnt = 0;
cnt1 = 0;
for (auto p : r[i]) q[++cnt] = make_pair(1ll * p / g * y % n, p);
sort(q + 1, q + 1 + cnt);
for (int j = 1; j <= cnt; ++j)
if (j == 1 || q[j].first != q[cnt1].first) q[++cnt1] = q[j];
cnt = cnt1;
q[cnt + 1] = q[1];
q[cnt + 1].first += n;
for (int j = 1; j <= cnt; ++j)
if (q[j + 1].first - q[j].first > 1)
ans = max(
ans, 1ll * m * g * (q[j + 1].first - q[j].first - 1) + q[j].second);
}
printf("%lld", ans);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
char S[1005][55];
int M, N;
int DP[1005][1006];
int C[1005][1005];
int Sum[1005];
const int MOD = 1000000007;
int Ord[1005];
inline void Add(int& x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
void precalcC() {
C[0][0] = 1;
for (int i = 1; i <= 1000; i++)
for (int j = 1; j <= i; j++) {
C[i][j] = C[i - 1][j];
Add(C[i][j], C[i - 1][j - 1]);
}
}
void precalcDP() {
for (int i = 1; i <= M; i++) {
DP[i][1] = 1;
Add(Sum[i], 1);
for (int j = 2; j <= i; j++) {
DP[i][j] = DP[i - 1][j - 1];
Add(DP[i][j], (1LL * DP[i - 1][j] * j) % MOD);
Add(Sum[i], DP[i][j]);
}
}
}
inline bool cmp(int a, int b) { return strcmp(S[a] + 1, S[b] + 1) < 0; }
void Read() {
cin >> M >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) cin >> S[j][i];
}
for (int i = 1; i <= M; i++) Ord[i] = i;
sort(Ord + 1, Ord + M + 1, cmp);
}
void Solve() {
int curr = 1;
int ans = 1;
for (int i = 2; i <= M; i++) {
if (strcmp(S[Ord[i]] + 1, S[Ord[i - 1]] + 1) == 0)
++curr;
else {
ans = (1LL * ans * Sum[curr]) % MOD;
curr = 1;
}
}
ans = (1LL * ans * Sum[curr]) % MOD;
cout << ans << "\n";
}
int main() {
Read();
precalcC();
precalcDP();
Solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500000, S = 4000, B = 125;
int a[N];
int bmx[B][S], bmn[B][S];
int all_mx[B], all_mn[B];
int sz[B];
int rmx[S];
int n, q;
int tot_b;
int RMX[B + 1];
void recal(int b) {
int L = b * S, R = min(n - 1, L + S - 1);
int mx = 0;
for (int i = R, j = R - L; i >= L; i--, j--) {
mx = max(mx, a[i]);
rmx[j] = mx;
}
int mn = 1e9;
sz[b] = 0;
for (int i = L, j = 0; i <= R; i++, j++) {
if (mn > rmx[j]) {
bmx[b][sz[b]] = rmx[j];
bmn[b][sz[b]] = mn;
sz[b]++;
}
mn = min(mn, a[i]);
}
all_mn[b] = mn;
all_mx[b] = mx;
}
int query() {
for (int i = tot_b - 1; i >= 0; i--) {
RMX[i] = max(RMX[i + 1], all_mx[i]);
}
int LMN = 1e9 + 100;
int ans = 0;
for (int i = 0; i < tot_b; i++) {
if (LMN > RMX[i + 1]) {
int lo = 0, hi = sz[i] - 1;
while (hi > lo) {
int mid = (lo + hi) / 2;
if (bmx[i][mid] < LMN)
hi = mid;
else
lo = mid + 1;
}
if (bmx[i][lo] < LMN) {
int idx = lo;
lo = 0, hi = sz[i] - 1;
while (hi > lo) {
int mid = (lo + hi + 1) / 2;
if (bmn[i][mid] > RMX[i + 1])
lo = mid;
else
hi = mid - 1;
}
if (bmn[i][lo] > RMX[i + 1]) {
ans += max(0, lo - idx + 1);
}
}
}
LMN = min(all_mn[i], LMN);
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
tot_b = (n - 1) / S + 1;
for (int i = 0; i < tot_b; i++) recal(i);
while (q--) {
int pos, x;
cin >> pos >> x;
pos--;
a[pos] = x;
recal(pos / S);
cout << query() << "\n";
}
return 0;
}
| 12 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL base = 1e9 + 7;
int main() {
LL a, b;
cin >> a >> b;
LL res = 0;
LL x = ((a * (a + 1) / 2) % base * b) % base;
LL y = (b * (b - 1) / 2) % base;
res = (((x + a) % base) * y) % base;
cout << res << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long moeny, a, b;
cin >> moeny >> a >> b;
for (int i = 0; i <= moeny; i += a) {
if ((moeny - i) % b == 0 && (moeny - i) / b >= 0) {
cout << "YES\n";
cout << i / a << ' ' << (moeny - i) / b << endl;
return 0;
}
}
cout << "NO\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> mx[105][105];
void solve() {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i(0), _l((int)(n)-1); i <= _l; ++i)
for (int j(0), _l((int)(m)-1); j <= _l; ++j) mx[i][j] = make_pair(i, j);
map<pair<int, int>, int> values;
for (int i(0), _l((int)(q)-1); i <= _l; ++i) {
int t, x;
scanf("%d%d", &t, &x);
if (t == 1) {
--x;
for (int i(0), _l((int)(m - 1) - 1); i <= _l; ++i)
swap(mx[x][i], mx[x][i + 1]);
} else if (t == 2) {
--x;
for (int i(0), _l((int)(n - 1) - 1); i <= _l; ++i)
swap(mx[i][x], mx[i + 1][x]);
} else if (t == 3) {
int y, v;
scanf("%d%d", &y, &v);
--x, --y;
values[mx[x][y]] = v;
}
}
for (int i(0), _l((int)(n)-1); i <= _l; ++i) {
for (int j(0), _l((int)(m)-1); j <= _l; ++j) {
int v(values.count(make_pair(i, j)) ? values[make_pair(i, j)] : 0);
printf("%d%c", v, j + 1 == m ? '\n' : ' ');
}
}
}
int main() {
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<char> v(n);
int fd, fr;
fr = fd = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
if (v[i] == 'D')
fd++;
else
fr++;
}
int d, r;
r = d = 0;
vector<int> dd(n, -1);
vector<int> rr(n, -1);
int ddd = 0;
int rrr = 0;
for (int i = 0; i < n; i++) {
if (rrr >= fr) {
cout << 'D';
break;
}
if (ddd >= fd) {
cout << 'R';
break;
}
if (v[i] == 'D') {
if (d > 0) {
if (dd[i] == 0) {
if (i == n - 1) i = -1;
continue;
} else {
if (dd[i] == 0) {
if (i == n - 1) i = -1;
continue;
}
dd[i] = 0;
d--;
}
} else {
if (dd[i] == 0) {
if (i == n - 1) i = -1;
continue;
}
r++;
rrr++;
}
} else {
if (r > 0) {
if (rr[i] == 0) {
if (i == n - 1) i = -1;
continue;
} else {
if (rr[i] == 0) {
if (i == n - 1) i = -1;
continue;
}
rr[i] = 0;
r--;
}
} else {
if (rr[i] == 0) {
if (i == n - 1) i = -1;
continue;
}
d++;
ddd++;
}
}
if (i == n - 1) i = -1;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, a;
cin >> n >> a;
if (a & 1) {
cout << ceil(a / 2.0) << endl;
} else {
long long o = n - a + 1;
cout << ceil(o / 2.0) << endl;
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
| 1 |
Subsets and Splits