solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 107;
const int MAXT = 2007;
const int MAXE = 207;
const int INF = 0x3f3f3f3f;
int n;
struct Item {
int t, d, p, nu;
Item() {}
Item(int t, int d, int p) : t(t), d(d), p(p) {}
bool operator<(Item i) const { return d < i.d; }
} item[MAXN];
struct Dp {
vector<int> v;
int val;
} dp[MAXT][MAXN];
int main() {
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) {
scanf("%d%d%d", &item[i].t, &item[i].d, &item[i].p);
item[i].nu = i;
}
sort(item + 1, item + 1 + n);
for (int i = 0; i < MAXT; i++)
for (int j = 0; j <= n; j++) {
dp[i][j].v.clear();
dp[i][j].val = 0;
}
for (int i = 0; i < MAXT; i++) {
for (int j = 1; j <= n; j++) {
if (i - item[j].t >= 0 && i < item[j].d) {
if (dp[i][j].val < dp[i - item[j].t][j - 1].val + item[j].p) {
dp[i][j].val = dp[i - item[j].t][j - 1].val + item[j].p;
dp[i][j].v = dp[i - item[j].t][j - 1].v;
dp[i][j].v.push_back(item[j].nu);
}
}
if (dp[i][j].val < dp[i][j - 1].val) {
dp[i][j].val = dp[i][j - 1].val;
dp[i][j].v = dp[i][j - 1].v;
}
}
}
int ans = 0;
vector<int> v;
for (int i = 0; i < MAXT; i++) {
if (ans < dp[i][n].val) {
ans = dp[i][n].val;
v = dp[i][n].v;
}
}
printf("%d\n%d\n", ans, (int)v.size());
for (int i = 0; i < v.size(); i++)
if (i == 0)
printf("%d", v[i]);
else
printf(" %d", v[i]);
putchar('\n');
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long t, n, m;
long long cust[105][3];
void solve() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> cust[i][j];
}
}
long long mn = m, mx = m, cur = 0;
for (int c = 0; c < n; c++) {
long long t = cust[c][0], l = cust[c][1], r = cust[c][2];
if (mn - (t - cur) <= r && mx + (t - cur) >= l) {
mn = max(mn - (t - cur), l);
mx = min(mx + (t - cur), r);
cur = t;
} else {
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
}
int main() {
cin >> t;
while (t--) {
cin >> n >> m;
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, a, b, c, x, y;
long long HP, A, B, C, D, AA, BB, CC, DD;
inline long long dis(int x, int y, int xx, int yy) {
return abs(x - xx) + abs(y - yy);
}
inline void upd() {
A += dis(x, y, a, b);
B += dis(x, y, a, b + c - 1);
C += dis(x, y, a + c - 1, b);
D += dis(x, y, a + c - 1, b + c - 1);
}
inline void gao1() {
if (!(x < a && y < b)) return;
long long xx = (a - x) + (b - y), yy = xx * (xx + 1) / 2;
A += dis(x, y, a, b) * xx - yy;
B += dis(x, y, a, b + c - 1) * xx - yy;
C += dis(x, y, a + c - 1, b) * xx - yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx - yy;
x = a, y = b;
}
inline void gao2() {
if (!(x < a && y >= b && y < b + c)) return;
long long xx = a - x, yy = xx * (xx + 1) / 2;
A += dis(x, y, a, b) * xx - yy;
B += dis(x, y, a, b + c - 1) * xx - yy;
C += dis(x, y, a + c - 1, b) * xx - yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx - yy;
x = a;
}
inline void gao3() {
if (!(x < a && y >= b + c)) return;
long long xx = a - x, yy = xx * (xx + 1) / 2;
A += dis(x, y, a, b) * xx - yy;
B += dis(x, y, a, b + c - 1) * xx - yy;
C += dis(x, y, a + c - 1, b) * xx - yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx - yy;
x = a;
}
inline void gao4() {
if (!(x >= a && x < a + c && y < b)) return;
long long xx = b - y, yy = xx * (xx + 1) / 2;
A += dis(x, y, a, b) * xx - yy;
B += dis(x, y, a, b + c - 1) * xx - yy;
C += dis(x, y, a + c - 1, b) * xx - yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx - yy;
y = b;
}
inline long long suan1(int jia, int jian) {
long long ret = jia * (jia + 1ll) / 2;
return ret + 1ll * jia * jian - (jian + 1ll) * jian / 2;
}
inline long long suan2(int jia, int jian) {
long long ret = jian * (jian + 1ll) / 2;
return -ret - 1ll * jian * jia + (jia + 1ll) * jia / 2;
}
inline void gao5() {
if (!(x >= a && x < a + c && y >= b && y < b + c)) return;
long long xx = dis(x, y, a + c - 1, b + c - 1), yy = (xx + 1) * xx / 2;
A += dis(x, y, a, b) * xx + yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx - yy;
B += xx * dis(x, y, a, b + c - 1);
C += xx * dis(x, y, a + c - 1, b);
long long high = suan1(a + c - x - 1, b + c - y - 1);
long long low = suan2(a + c - x - 1, b + c - y - 1);
assert(!((high - low) & 1));
long long jj = n + n - (a + c - 1) - (b + c - 1);
jj = HP - (jj + 1) * jj / 2 - jj * (c - 1) - B;
long long aim;
if ((jj - low) & 1)
aim = jj - 1;
else
aim = jj;
if (aim >= low) {
B += min(aim, high);
C -= min(aim, high);
} else
A = HP + 1;
x = a + c - 1;
y = b + c - 1;
if (x < n)
++x, upd();
else if (y < n)
++y, upd();
}
inline void gao6() {
if (!(x >= a && x < a + c && y >= b + c)) return;
long long xx = a + c - 1 - x, yy = xx * (xx + 1) / 2;
A += dis(x, y, a, b) * xx + yy;
B += dis(x, y, a, b + c - 1) * xx + yy;
C += dis(x, y, a + c - 1, b) * xx - yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx - yy;
x = a + c - 1;
if (x < n) ++x, upd();
}
inline void gao7() {
if (!(x >= a + c && y < b)) return;
long long xx = b - y, yy = xx * (xx + 1) / 2;
A += dis(x, y, a, b) * xx - yy;
B += dis(x, y, a, b + c - 1) * xx - yy;
C += dis(x, y, a + c - 1, b) * xx - yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx - yy;
y = b;
}
inline void gao8() {
if (!(x >= a + c && y >= b && y < b + c)) return;
long long xx = b + c - 1 - y, yy = xx * (xx + 1) / 2;
A += dis(x, y, a, b) * xx + yy;
B += dis(x, y, a, b + c - 1) * xx - yy;
C += dis(x, y, a + c - 1, b) * xx + yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx - yy;
y = b + c - 1;
if (y < n) ++y, upd();
}
inline void gao9() {
long long xx = dis(x, y, n, n), yy = xx * (xx + 1) / 2;
A += dis(x, y, a, b) * xx + yy;
B += dis(x, y, a, b + c - 1) * xx + yy;
C += dis(x, y, a + c - 1, b) * xx + yy;
D += dis(x, y, a + c - 1, b + c - 1) * xx + yy;
x = y = n;
}
inline bool ok() {
if (x > n) return 0;
int xx = x, yy = y;
gao1();
gao2();
gao4();
gao3();
gao5();
gao7();
gao6();
gao8();
gao9();
x = xx;
y = yy;
return max(max(A, B), max(C, D)) <= HP;
}
int main() {
cin >> n >> HP >> a >> b >> c;
x = y = 1;
if (!ok()) return puts("Impossible"), 0;
for (A = B = C = D = 0; x ^ n || y ^ n;) {
AA = A;
BB = B;
CC = C;
DD = D;
++x, upd();
if (ok())
putchar('R');
else
--x, ++y, putchar('U');
A = AA, B = BB, C = CC, D = DD, upd();
}
puts("");
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
unsigned long long a, b, c, d;
cin >> a >> b >> c >> d;
if (b >= a)
cout << b << endl;
else if (c <= d)
cout << "-1" << endl;
else {
unsigned long long tbed = b;
unsigned long long k;
k = ((a - b) / (c - d));
if ((a - b) % (c - d) != 0) k++;
tbed += k * c;
cout << tbed << endl;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
long long n;
cin >> n;
long long d1 = n % 10;
long long d2 = n % 100 / 10;
long long x = n / 10;
long long y = n / 100 * 10 + d1;
cout << max({x, y, n});
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long bINF = 1e16;
const double EPS = 1e-9;
const double PI = acos(-1);
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int q;
scanf("%d", &q);
while (q--) {
int n;
scanf("%d", &n);
vector<pair<int, int>> a(n);
vector<pair<int, int>> b(n);
for (int i = 0; i < n; ++i) {
scanf("%d %d", &a[i].first, &a[i].second);
b[i] = a[i];
swap(b[i].first, b[i].second);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
printf("%d\n", max(0, a.back().first - b[0].first));
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> dp;
void pre() {
for (long long i = 0; i < 63; i++) {
dp.push_back(pow(2, i));
}
}
void solution() {
long long n;
cin >> n;
long long ans = INT_MAX;
string s = to_string(n);
for (auto x : dp) {
long long k = 0;
string p = to_string(x);
for (long long i = 0; i < s.size(); i++) {
if (k == p.size()) break;
if (s[i] == p[k]) k++;
}
if (ans > (p.size() + s.size() - k * 2))
ans = (p.size() + s.size() - k * 2);
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
pre();
long long t;
cin >> t;
while (t--) solution();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long a = 1;
long long n, k, ans;
long long v[100010];
vector<long long> V;
map<long long, int> M;
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> v[i], v[i] += v[i - 1];
V.push_back(1);
if (k != 1 && k != -1)
while (a < 1e15) a *= k, V.push_back(a);
if (k == -1) V.push_back(-1);
M[0]++;
for (int i = 1; i <= n; i++) {
for (int h = 0; h < V.size(); h++) ans += M[-1 * (V[h] - v[i])];
M[v[i]]++;
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<int> a;
cin >> n;
int flag = 0;
int sum = 0;
for (int i = 0; i < n; i++) {
int t;
cin >> t;
if (t < 0) flag++;
if (flag == 3) {
a.push_back(sum);
flag = 1;
sum = 0;
}
sum++;
if (i == n - 1) a.push_back(sum);
}
cout << a.size() << endl;
for (int i = 0; i < a.size(); i++) {
cout << a[i] << ' ';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace ::std;
const unsigned long long maxn = 310;
const unsigned long long inf = 1e9 + 500;
const unsigned long long mod = 1e9 + 7;
const unsigned long long maxm = 2e4 + 500;
unsigned long long a[maxn];
unsigned long long id[maxn];
vector<unsigned long long> ger[maxn];
bool npr[maxm];
bool vis[maxn];
unsigned long long match[maxn][2];
bool tt;
bool dfs(unsigned long long a) {
vis[a] = 1;
for (auto u : ger[a]) {
if (match[u][tt] == -1 || (!vis[match[u][tt]] && dfs(match[u][tt]))) {
match[a][tt] = u;
match[u][tt] = a;
return 1;
}
}
return 0;
}
vector<unsigned long long> vec[maxn];
unsigned long long cnr;
void dfss(unsigned long long a) {
vis[a] = 1;
vec[cnr].push_back(a + 1);
unsigned long long v = match[a][0];
unsigned long long u = match[a][1];
if (vis[v] == 0) {
dfss(v);
}
if (vis[u] == 0) {
dfss(u);
}
}
int main() {
for (unsigned long long i = 2; i <= maxm; i++) {
for (unsigned long long j = i * 2; j < maxm; j += i) {
npr[j] = 1;
}
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
unsigned long long n;
cin >> n;
unsigned long long bala = 0;
for (unsigned long long i = 0; i < n; i++) {
cin >> a[i];
if (a[i] & 1) {
bala++;
} else {
bala--;
}
}
if (bala != 0) {
cout << "Impossible";
return 0;
}
for (unsigned long long i = 0; i < n; i++) {
for (unsigned long long j = i + 1; j < n; j++) {
if (!npr[a[i] + a[j]]) {
ger[i].push_back(j);
ger[j].push_back(i);
}
}
}
tt = 0;
for (unsigned long long i = 0; i < maxn; i++) {
match[i][tt] = -1;
}
for (unsigned long long i = 0; i < n; i++) {
if (a[i] & 1) {
fill(vis, vis + maxn, 0);
dfs(i);
}
}
for (unsigned long long i = 0; i < n; i++) {
ger[i].clear();
if (match[i][tt] == -1) {
cout << "Impossible";
return 0;
}
}
for (unsigned long long i = 0; i < n; i++) {
for (unsigned long long j = i + 1; j < n; j++) {
if (!npr[a[i] + a[j]] && match[i][0] != j && match[j][0] != i) {
ger[i].push_back(j);
ger[j].push_back(i);
}
}
}
tt = 1;
for (unsigned long long i = 0; i < maxn; i++) {
match[i][tt] = -1;
}
for (unsigned long long i = 0; i < n; i++) {
if (a[i] & 1) {
fill(vis, vis + maxn, 0);
dfs(i);
}
}
for (unsigned long long i = 0; i < n; i++) {
ger[i].clear();
if (match[i][tt] == -1) {
cout << "Impossible";
return 0;
}
}
fill(vis, vis + maxn, 0);
for (unsigned long long i = 0; i < n; i++) {
if (vis[i] == 0) {
dfss(i);
cnr++;
}
}
cout << cnr << endl;
for (unsigned long long i = 0; i < cnr; i++) {
cout << vec[i].size() << ' ';
for (auto a : vec[i]) {
cout << a << ' ';
}
cout << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
struct tree {
vector<int> t;
tree() {}
tree(int n) : t(4 * n, 0) {}
void modify(int v, int l, int r, int pos, int val) {
if (l == r) {
t[v] = val;
return;
}
if (pos <= (l + r) / 2) {
modify(v * 2, l, (l + r) / 2, pos, val);
} else {
modify(v * 2 + 1, (l + r) / 2 + 1, r, pos, val);
}
t[v] = min(t[v * 2], t[v * 2 + 1]);
}
int get(int v, int l, int r, int tl, int tr) {
if (l < tl) {
l = tl;
}
if (r > tr) {
r = tr;
}
if (l > r) {
return inf;
}
if (l == tl && r == tr) {
return t[v];
}
return min(get(v * 2, l, r, tl, (tl + tr) / 2),
get(v * 2 + 1, l, r, (tl + tr) / 2 + 1, tr));
}
};
struct rect {
int x1, y1, x2, y2;
int number;
bool ans = 0;
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, k, q;
cin >> n >> m >> k >> q;
vector<pair<int, int> > rook(k);
for (int i = 0; i < k; ++i) {
cin >> rook[i].first >> rook[i].second;
}
vector<rect> a(q);
for (int i = 0; i < q; ++i) {
cin >> a[i].x1 >> a[i].y1 >> a[i].x2 >> a[i].y2;
a[i].number = i + 1;
}
sort(rook.begin(), rook.end(),
[](pair<int, int> a, pair<int, int> b) { return a.second < b.second; });
sort(a.begin(), a.end(), [](rect a, rect b) { return a.y2 < b.y2; });
tree t(n);
int x = 0, y = 0;
for (int i = 1; i <= m; ++i) {
while (x < rook.size() && rook[x].second == i) {
t.modify(1, 1, n, rook[x].first, i);
++x;
}
while (y < a.size() && a[y].y2 == i) {
if (t.get(1, a[y].x1, a[y].x2, 1, n) >= a[y].y1) {
a[y].ans = 1;
}
++y;
}
}
sort(rook.begin(), rook.end(),
[](pair<int, int> a, pair<int, int> b) { return a.first < b.first; });
sort(a.begin(), a.end(), [](rect a, rect b) { return a.x2 < b.x2; });
t = tree(m);
x = 0, y = 0;
for (int i = 1; i <= n; ++i) {
while (x < rook.size() && rook[x].first == i) {
t.modify(1, 1, m, rook[x].second, i);
++x;
}
while (y < a.size() && a[y].x2 == i) {
if (t.get(1, a[y].y1, a[y].y2, 1, m) >= a[y].x1) {
a[y].ans = 1;
}
++y;
}
}
sort(a.begin(), a.end(), [](rect a, rect b) { return a.number < b.number; });
for (int i = 0; i < a.size(); ++i) {
cout << (a[i].ans ? "YES" : "NO") << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct truck {
int a, b, c, d;
};
struct para {
int s, pop;
};
int main() {
ios_base::sync_with_stdio(0);
int n;
cin >> n;
vector<truck> t(n);
for (int i = 0; i < n; ++i) cin >> t[i].a >> t[i].b >> t[i].c >> t[i].d;
map<pair<int, int>, set<pair<int, int> > > S;
vector<para> dp(n, {-1, -1});
for (int i = 0; i < n; ++i) {
int X = t[i].c, Y = t[i].d + t[i].b;
if (S[make_pair(X, Y)].size() == 0) {
if (t[i].c == 0) {
dp[i] = {t[i].a, -1};
S[make_pair(t[i].c + t[i].b, t[i].d)].insert(make_pair(-t[i].a, i));
}
} else {
pair<int, int> w = *S[make_pair(X, Y)].begin();
w.first *= -1;
dp[i] = {w.first + t[i].a, w.second};
S[make_pair(t[i].c + t[i].b, t[i].d)].insert(make_pair(-dp[i].s, i));
}
}
int maxi = -1, nr = -1;
for (int i = 0; i < n; ++i) {
if (dp[i].s > maxi && t[i].d == 0) {
maxi = dp[i].s;
nr = i;
}
}
if (maxi == -1) {
cout << "0";
return 0;
}
vector<int> nry;
while (nr != -1) {
nry.push_back(nr);
nr = dp[nr].pop;
}
reverse(nry.begin(), nry.end());
cout << nry.size() << endl;
for (int i = 0; i < nry.size(); ++i) cout << nry[i] + 1 << " ";
return 0;
}
| 4 |
#include <bits/stdc++.h>
const int MAXN = 200086;
char s[MAXN];
int mx[MAXN], c[MAXN], v[MAXN];
inline bool isv(const char x) {
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') return true;
if (x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U') return true;
return false;
}
int main() {
int i;
int tot = -1, L = -1;
int left, right, mid, tt;
scanf("%s", s + 1);
for (i = 1; s[i]; ++i) {
v[i] = v[i - 1] + isv(s[i]);
c[i] = c[i - 1] - (!isv(s[i])) * 2;
mx[i] = ((mx[i - 1]) > (v[i] + c[i]) ? (mx[i - 1]) : (v[i] + c[i]));
}
int len = i - 1;
for (i = 1; i <= len; ++i) {
left = 0, right = i - 1, tt = -1;
while (left <= right) {
mid = (left + right) >> 1;
if (mx[mid] >= v[i] + c[i])
tt = mid, right = mid - 1;
else
left = mid + 1;
}
if (tt == -1) continue;
if (i - tt > L) {
tot = 1;
L = i - tt;
} else if (i - tt == L)
++tot;
}
if (L == -1)
puts("No solution");
else
printf("%d %d\n", L, tot);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void bober() {
int n;
cin >> n;
vector<int> r(n + 1, n + 1), ans(n + 1, 0), cnt(n + 1, 0);
r[1] = 0;
for (int i = 2; i <= n; i++) {
if (r[i] < n + 1) {
cnt[r[i]]++;
continue;
}
r[i] = i;
cnt[i]++;
for (int j = i + i; j <= n; j += i) {
r[j] = min(i, r[j]);
}
}
int siz = 0;
for (int i = 3; i <= n / 2; i++) {
if (r[i] == i && cnt[i] % 2 == 1) {
ans[i] = i + i;
ans[i + i] = i;
siz++;
}
}
for (int i = 2; i <= n / 2; i++) {
int x = -1, y = -1;
for (int j = i; j <= n; j += i) {
if (ans[j]) continue;
if (x == -1)
x = j;
else {
y = j;
ans[x] = y;
ans[y] = x;
siz++;
x = y = -1;
}
}
}
cout << siz << "\n";
for (int i = 2; i <= n; i++) {
if (ans[i] > i) cout << i << " " << ans[i] << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
q = 1;
while (q--) {
bober();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char in[1000], in1[1000];
vector<string> vs;
string s;
vector<string> shift(vector<string> vs) {
int i;
for (i = vs.size() - 1; i >= 1; i--) {
vs[i] = vs[i - 1];
}
vs[0] = "0000";
return vs;
}
int main() {
int T, n, i;
scanf("%d", &T);
gets(in);
while (T--) {
vs.clear();
gets(in);
strcpy(in1, in);
char* pch;
pch = strtok(in1, ":");
while (pch != NULL) {
s = (string)pch;
pch = strtok(NULL, ":");
while (s.length() != 4) {
s = "0" + s;
}
vs.push_back(s);
}
n = strlen(in);
int j;
vector<int> pos;
for (i = 0; i < n; i++) {
if (in[i] == ':') pos.push_back(i);
}
int baki = 8 - vs.size();
if (baki) {
if (pos[0] == 0) {
while (baki--) {
vs.push_back("0000");
vs = shift(vs);
}
} else if ((pos[pos.size() - 1]) == (n - 1)) {
while (baki--) vs.push_back("0000");
} else {
vector<string> ret;
for (i = 1; i < pos.size(); i++) {
if (pos[i] == (pos[i - 1] + 1)) {
break;
}
}
for (j = 0; j < i; j++) ret.push_back(vs[j]);
while (baki--) ret.push_back("0000");
while (ret.size() != 8) {
ret.push_back(vs[j++]);
}
vs = ret;
}
}
for (i = 0; i < 8; i++) {
if (i) printf(":");
cout << vs[i];
}
cout << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
vector<int> visited, active;
void dfs_visit(int root, const vector<vector<pair<int, int> > >& adj,
vector<int>& colors, int parent) {
visited[root] = 1;
active[root] = 1;
for (auto v : adj[root]) {
colors[v.second] = active[v.first] ? 2 : 1;
if (!visited[v.first]) dfs_visit(v.first, adj, colors, root);
}
active[root] = 0;
}
vector<int> dfs(int m, const vector<vector<pair<int, int> > >& adj) {
vector<int> colors(m + 1);
int n = adj.size() - 1;
visited.resize(n + 1);
active.resize(n + 1);
for (int i = 1; i <= n; i++) {
if (!visited[i]) dfs_visit(i, adj, colors, -1);
}
return colors;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
vector<vector<pair<int, int> > > adj(n + 1);
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back({v, i});
}
vector<int> colors = dfs(m, adj);
int k = 1;
for (auto& i : colors) k = max(k, i);
cout << k << "\n";
for (int i = 1; i <= m; i++) cout << colors[i] << " ";
cout << "\n";
return 0;
}
| 4 |
#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];
sort(a,a+n,greater<int>());
cout << a[0]-a[n-1] << endl;
} | 0 |
#include <iostream>
using namespace std;
int n;
int p[100];
int cnt[101];
int main() {
int i;
cin >> n;
for (i = 0; i < n; i++) { cin >> p[i]; cnt[p[i]]++; }
for (i = 99; i >= 0; i--) cnt[i] += cnt[i + 1];
for (i = 100; i >= 0; i--) if (cnt[i] >= i) break;
cout << i << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long int n, a, b, y;
cin >> n;
cin >> a;
cin >> b;
long long int money;
if (b - 2 * a >= 0) {
cout << n * a << endl;
} else {
y = (a * n) / (2 * a - b);
if (y < (n / 2))
cout << y * b + (n - 2 * y) * a << endl;
else {
y = n / 2;
cout << y * b + (n - 2 * y) * a << endl;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int X1, Y1, X2, Y2;
int a, b;
int main() {
long long int n, k, kk;
cin >> n >> k;
kk = k;
vector<long long int> v(n + 1, 0);
vector<long long int> b;
v[0] = 1;
v[1] = 1;
for (long long int i = 2; i <= n; i++) {
if (v[i] == 0) {
b.push_back(i);
for (long long int j = 1; j * i <= n; j++) {
v[i * j] = 1;
}
}
}
k--;
vector<long long int> c;
while (k) {
for (long long int i = 0; i < b.size() && k && n != 1; i++) {
while (n % b[i] == 0 && k && n != 1) {
c.push_back(b[i]);
n = n / b[i];
k--;
}
}
if (n == 1) break;
}
if (n != 1) c.push_back(n);
if (k > 0) {
cout << -1;
return 0;
}
if (c.size() != kk) {
cout << -1;
return 0;
}
for (long long int i = 0; i < c.size(); i++) {
cout << c[i] << " ";
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long LINF = 3e18;
const int maxN = 1e5 + 100;
const int maxA = 1e6 + 1;
int main() {
int n;
cin >> n;
string s;
cin >> s;
string curr = "ogo";
while (curr.size() < s.size()) {
curr += "go";
}
while (curr.size() >= 3) {
for (int i = 0; i <= (int)s.size() - (int)curr.size(); i++) {
if (s.substr(i, curr.size()) == curr) {
s.erase(i, curr.size());
s.insert(i, "***");
}
}
curr.pop_back();
curr.pop_back();
}
cout << s;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long long maxn = 1000005;
double arr[maxn];
void solve() {
long long n, x;
cin >> n >> x;
priority_queue<double> pq;
for (long long i = (long long)(0); i < (long long)(n); i++) {
cin >> arr[i];
pq.push(arr[i]);
}
double sum = 0;
long long people = 0;
while (!pq.empty()) {
double maxx = pq.top();
pq.pop();
double avg = (maxx + sum) / (people + 1.0);
if (avg >= x) {
sum += maxx;
people++;
} else {
break;
}
}
cout << people;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
for (long long i = (long long)(1); i < (long long)(t + 1); i++) {
solve();
cout << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
return out << "(" << a.first << "," << a.second << ")";
}
template <class A>
ostream &operator<<(ostream &out, const vector<A> &a) {
for (const A &it : a) out << it << " ";
return out;
}
template <class A, class B>
istream &operator>>(istream &in, pair<A, B> &a) {
return in >> a.first >> a.second;
}
template <class A>
istream &operator>>(istream &in, vector<A> &a) {
for (A &i : a) in >> i;
return in;
}
ifstream cinn("in.txt");
ofstream coutt("out.txt");
long long poww(const long long &a, long long b,
const long long &m = 1000000007) {
if (b == 0) return 1;
long long first = poww(a, b / 2, m);
first = first * first % m;
if (b & 1) first = first * a % m;
return first;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool xx(long long a, long long li) {
if (a >= 0 && a < li) return true;
return false;
}
long long numofways(long long n, long long m, vector<vector<char>> &v) {
if (n <= 0 || m <= 0) return 0;
if (n == 1 && m == 1) return 1;
if (v[n - 1][m - 1] == '*') return 0;
return (((numofways(n - 1, m, v)) % 1000000007) +
((numofways(n, m - 1, v)) % 1000000007)) %
1000000007;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long t, n, r, c, m, h, a, b, k, f1, f2, t1, t2, i, j, ans = 0, f;
cin >> n;
string s;
cin >> s;
f = 0;
for (i = 0; i < n; i++) {
if (s[i] != '4' && s[i] != '7') f = 1;
}
if (!f) {
long long s1 = 0, s2 = 0;
for (i = 0; i < n; i++) {
if (i < n / 2) {
s1 += s[i] - '0';
} else {
s2 += s[i] - '0';
}
}
if (s1 != s2) f = 1;
}
cout << (f == 0 ? "YES" : "NO") << '\n';
;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
pair<int, int> p[N];
int n;
vector<pair<int, int> > cur, nxt;
int main() {
int m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
p[i] = make_pair(x, y);
}
sort(p, p + m);
if (p[0].first == 1)
cur.push_back(make_pair(1, 1));
else
cur.push_back(make_pair(1, n));
int i = 0, j = 0;
while (i < m) {
nxt.clear();
if (cur[0].first < p[j].second)
nxt.push_back(make_pair(cur[0].first, p[j].second - 1));
while (j < m && p[j].first == p[i].first) {
int low = 0, up = cur.size();
while (low < up) {
int mid = (low + up) >> 1;
if (cur[mid].second > p[j].second)
up = mid;
else
low = mid + 1;
}
pair<int, int> tmp;
if (low < (int)cur.size())
tmp.first = max(cur[low].first, p[j].second + 1);
else
tmp.first = n + 1;
if (j + 1 < m && p[j + 1].first == p[j].first)
tmp.second = p[j + 1].second - 1;
else
tmp.second = n;
if (tmp.first <= tmp.second) nxt.push_back(tmp);
j++;
}
if ((int)nxt.size() == 0) {
puts("-1");
return 0;
}
if ((j < m && p[i].first < p[j].first - 1) || (j == m && p[i].first < n)) {
pair<int, int> tmp = make_pair(nxt[0].first, n);
nxt.clear();
nxt.push_back(tmp);
}
i = j;
swap(cur, nxt);
}
if (cur[cur.size() - 1].second == n)
printf("%d\n", n + n - 2);
else
puts("-1");
return 0;
}
| 2 |
#include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
int main(){
int n;
while(cin>>n){
if(n==0)break;
int team[n][n-1];
char name[n];
rep(i,n){
cin>>name[i];
rep(j,n-1){
cin>>team[i][j];
}
}
int win[n];
int lose[n];
rep(i,n){
win[i]=0;
lose[i]=0;
}
rep(i,n){
rep(j,n-1){
if(team[i][j]==0)win[i]++;
else if(team[i][j]==1)lose[i]++;
}
}
rep(i,n){
for(int j=n-2;j>=i;j--){
if(win[j+1]>win[j]){
int alt=win[j];
win[j]=win[j+1];
win[j+1]=alt;
alt=lose[j];
lose[j]=lose[j+1];
lose[j+1]=alt;
char c=name[j];
name[j]=name[j+1];
name[j+1]=c;
}else if(win[j+1]==win[j]){
if(lose[j+1]<lose[j]){
int alt=win[j];
win[j]=win[j+1];
win[j+1]=alt;
alt=lose[j];
lose[j]=lose[j+1];
lose[j+1]=alt;
char c=name[j];
name[j]=name[j+1];
name[j+1]=c;
}
}
}
}
rep(i,n){
cout<<name[i]<<endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int score[56];
int flag[56][56];
int main() {
int n;
cin >> n;
for (int i = 0; i < n * (n - 1) / 2 - 1; i++) {
int x, y;
cin >> x >> y;
score[x]++;
score[y]--;
flag[x][y] = 1;
flag[y][x] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (flag[i][j] == 0 && i != j) {
if (score[i] > score[j]) {
cout << i << " " << j << endl;
} else {
cout << j << " " << i << endl;
}
exit(0);
}
}
}
return 0;
}
| 2 |
//40
#include<iostream>
#include<utility>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int main(){
queue<pair<vector<int>,int> > que;
static const int g[]={0,1,2,3,4,5,6,7};
vector<int> ov(g,g+8);
que.push(make_pair(ov,0));
map<vector<int>,int> m;
while(!que.empty()){
vector<int> cv=que.front().first;
if(m.find(cv)!=m.end()){
que.pop();
continue;
}
m[cv]=que.front().second;
int z=find(cv.begin(),cv.end(),0)-cv.begin();
for(int i=0;i<4;i++){
static const int d[]={1,4,-1,-4};
int nz=z+d[i];
if(0<=nz&&nz<8&&!(z*nz==12&&z+nz==7)){
vector<int> nv=cv;
swap(nv[z],nv[nz]);
que.push(make_pair(nv,que.front().second+1));
}
}
que.pop();
}
for(;;){
vector<int> v(8);
for(int i=0;i<8;i++){
cin>>v[i];
if(!cin)return 0;
}
cout<<m[v]<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> pii[10];
double ans = 0.0;
double cal(int n, int p, int vote, int A, int B) {
if (n == p) {
if (vote > n / 2)
return 1.0;
else
return 1.0 * A / (A + B);
}
double T = (1.0 * pii[p].second / 100);
double sum = T * cal(n, p + 1, vote + 1, A, B);
sum += (1.0 - T) * cal(n, p + 1, vote, A, B + pii[p].first);
return sum;
}
void DFS(int n, int p, int k, int A) {
if (p == n) {
ans = max(ans, cal(n, 0, 0, A, 0));
return;
}
for (int i = 0; i <= k; ++i) {
if (pii[p].second + 10 * i > 100) {
int T = 100 - pii[p].second;
pii[p].second = 100;
DFS(n, p + 1, k - T / 10, A);
pii[p].second = 100 - T;
return;
} else {
pii[p].second += 10 * i;
DFS(n, p + 1, k - i, A);
pii[p].second -= 10 * i;
}
}
}
int main() {
int n, k, A;
cin >> n >> k >> A;
for (int i = 0; i < n; ++i) cin >> pii[i].first >> pii[i].second;
DFS(n, 0, k, A);
printf("%.10lf\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b;
bool mark[200];
for (int i = 0; i < 150; i++) mark[i] = 0;
int n, a1, b1;
cin >> n >> a1 >> b1;
while (a1--) {
cin >> a;
mark[a] = true;
}
while (b1--) cin >> b;
for (int i = 1; i <= n; i++) {
if (mark[i])
cout << 1 << " ";
else
cout << 2 << " ";
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7, N = 1e6;
int sum[2000010], s1[2000010], s2[2000010], c[2000010], p[2000010];
int ans, n, m;
inline int mo(int x) {
if (x >= MOD) return x - MOD;
return x;
}
void init() {
for (int i = 1; i <= N; i++) {
p[i] += i;
for (int j = i + i; j <= N; j += i) p[j] -= p[i];
}
for (int i = 1; i <= N; i++)
for (int j = i; j <= N; j += i)
sum[j] = mo(sum[j] + 1ll * j / i * p[i] % MOD);
for (int i = 1; i <= N; i++) sum[i] = mo(sum[i] - i + MOD);
int sums = 0;
for (int i = 1; i <= N; i++) {
sums = mo(sums + 1ll * i * (i - 1) % MOD);
c[i] = mo(mo(mo(1ll * i * i % MOD * i % MOD - sums + MOD) -
1ll * i * (i - 1) % MOD * 2 % MOD + MOD) +
mo(sum[i] + sum[i]));
}
s1[1] = s2[1] = 1;
for (int i = 2; i <= N; i++) {
s1[i] =
mo(mo(mo(1ll * mo(s1[i - 1] + s2[i - 1]) * 2 % MOD - s1[i - 2] + MOD) -
1ll * s2[i - 2] * 2 % MOD + MOD) +
c[i]);
s2[i] = mo(mo(s1[i] - s1[i - 1] + MOD) - s2[i - 1] + MOD);
}
}
int main() {
int T;
scanf("%d", &T);
init();
while (T--) {
scanf("%d%d", &n, &m);
if (n > m) swap(n, m);
ans = mo(1ll * s2[n] * (m - n) % MOD + s1[n]);
printf("%d\n", ans);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long dist(pair<long long, long long>& o, pair<long long, long long>& u) {
return abs(o.first - u.first) + abs(o.second - u.second);
}
int32_t main() {
long long a, b;
cin >> a >> b;
pair<long long, long long> tar = {a, b};
string s;
cin >> s;
pair<long long, long long> vec = {0, 0};
if (a == 0 && b == 0) {
cout << "Yes" << endl;
return 0;
}
for (long long i = 0; i < s.size(); i++) {
switch (s[i]) {
case 'U':
vec.second++;
break;
case 'D':
vec.second--;
break;
case 'L':
vec.first--;
break;
case 'R':
vec.first++;
break;
}
if (vec.first == a && vec.second == b) {
cout << "Yes" << endl;
return 0;
}
}
pair<long long, long long> orig = {0, 0};
pair<long long, long long> cur = {0, 0};
long long k = (long long)((a + b - cur.first - cur.second) /
(double)(vec.first + vec.second));
if (k >= 0 && k * vec.first + cur.first == a &&
k * vec.second + cur.second == b) {
cout << "Yes" << endl;
return 0;
}
for (long long i = 0; i < s.size(); i++) {
switch (s[i]) {
case 'U':
cur.second++;
break;
case 'D':
cur.second--;
break;
case 'L':
cur.first--;
break;
case 'R':
cur.first++;
break;
}
k = (a == vec.first + cur.first) ? (b - cur.second) / (double)vec.second
: (a - cur.first) / (double)vec.first;
if (k >= 0 && k * vec.first + cur.first == a &&
k * vec.second + cur.second == b) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
}
| 1 |
#include <iostream>
#include <math.h>
using namespace std;
void solve()
{
double n;
while(cin >> n, n)
{
int ans = 0;
int k = sqrt(n);
for(int a = 1; a <= k; ++a)
{
if(a * a > n)
{
break;
}
if(a * a == n)
{
++ans;
}
for(int b = a; b <= k; ++b)
{
if(a * a + b * b > n)
{
break;
}
if(a * a + b * b == n)
{
++ans;
}
for(int c = b; c <= k; ++c)
{
if(a * a + b * b + c * c > n)
{
break;
}
if(a * a + b * b + c * c == n)
{
++ans;
}
for(int d = c; d <= k; ++d)
{
if(a * a + b * b + c * c + d * d > n)
{
break;
}
if(a * a + b * b + c * c + d * d == n)
{
++ans;
}
}
}
}
}
cout << ans << endl;
}
}
int main()
{
solve();
return(0);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[100005];
long long dp[100005][2];
int main() {
long long res, n, k;
while (cin >> n) {
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
a[i] = abs(a[i] - a[i + 1]);
}
long long ans = 0;
for (int i = n - 2; i >= 0; i--) {
dp[i][0] = a[i] + dp[i + 1][1];
dp[i][1] = -a[i] + dp[i + 1][0];
dp[i][0] = max(dp[i][0], 0LL);
dp[i][1] = max(dp[i][1], 0LL);
ans = max(ans, dp[i][0]);
ans = max(ans, dp[i][1]);
}
cout << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long n, input[1200000];
vector<long> v;
map<long, long> mapp;
long calc;
long long span, tinv[200], linv[200];
long long p;
vector<pair<long, long> > ad;
long tests;
long lvl;
long long answ;
long vars[1200000];
class Fenwick {
long *m, *mt, N;
public:
Fenwick(long n);
Fenwick(long a[], long n);
void add_range(long l, long d);
void add_range(long l, long r, long d);
long sum(long r);
long sum(long l, long r);
};
Fenwick::Fenwick(long n) {
N = n;
m = new long[N];
mt = new long[N];
for (int i = 0; i < n; i++) m[i] = mt[i] = 0;
}
void Fenwick::add_range(long r, long d) {
if (r < 0) return;
for (int i = r; i >= 0; i = (i & (i + 1)) - 1) mt[i] += d;
for (int i = r | (r + 1); i < N; i |= i + 1)
m[i] += d * (r - (i & (i + 1)) + 1);
}
void Fenwick::add_range(long l, long r, long d) {
add_range(r, d);
add_range(l - 1, -d);
}
long Fenwick::sum(long r) {
if (r < 0) return 0;
long long res = 0;
for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
res += m[i] + mt[i] * (i - (i & (i + 1)) + 1);
for (int i = r | (r + 1); i < N; i |= i + 1)
res += mt[i] * (r - (i & (i + 1)) + 1);
return res;
}
long Fenwick::sum(long l, long r) { return sum(r) - sum(l - 1); }
int main() {
cin >> n;
for (int i = 0; i < (1 << n); i++) {
scanf("%d", &input[i]);
vars[i] = input[i];
v.push_back(input[i]);
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
if (i == 0 || v[i] != v[i - 1]) {
mapp[v[i]] = calc + 1;
calc++;
}
}
for (int i = 0; i < (1 << n); i++) {
input[i] = mapp[input[i]];
ad.push_back(make_pair(input[i], -i));
}
sort(ad.begin(), ad.end());
Fenwick inv(1100100);
for (int pp = 0; pp < (1 << n); pp++) {
long i = -ad[pp].second;
for (int lev = 1; lev <= n; lev++) {
span = i % (1 << lev);
p = i - span;
if (span < (1 << (lev - 1))) continue;
linv[lev] += inv.sum(p, p + (1 << (lev - 1)) - 1);
}
inv.add_range(-ad[pp].second, -ad[pp].second, 1);
}
for (int lev = 1; lev <= n; lev++) tinv[lev] = linv[lev];
Fenwick inv1(1100100);
ad.clear();
for (int i = 0; i < (1 << n); i++) {
input[i] = calc - input[i] + 1;
ad.push_back(make_pair(-input[i], i));
}
sort(ad.begin(), ad.end());
for (int pp = 0; pp < (1 << n); pp++) {
long i = ad[pp].second;
for (int lev = 1; lev <= n; lev++) {
span = i % (1 << lev);
p = i - span + (1 << (lev - 1));
if (span >= (1 << (lev - 1))) continue;
tinv[lev] += inv1.sum(p, p + (1 << (lev - 1)) - 1);
}
inv1.add_range(ad[pp].second, ad[pp].second, 1);
}
cin >> tests;
for (; tests; --tests) {
scanf("%d", &lvl);
for (int i = lvl; i > 0; --i) linv[i] = tinv[i] - linv[i];
answ = 0;
for (int i = 1; i <= n; i++) answ += linv[i];
printf("%I64d\n", answ);
}
cin.get();
cin.get();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const long long mod = 1000000007;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
vector<long long> v;
long long a[105];
int main() {
long long n, k;
scanf("%I64d%I64d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &a[i]);
for (long long j = 1; j * j <= a[i]; j++) {
v.push_back(j);
long long t = (a[i] + j - 1) / j;
v.push_back(t);
}
k += a[i];
}
v.push_back(INF);
long long ans = 1;
sort(v.begin(), v.end());
int m = unique(v.begin(), v.end()) - v.begin() - 1;
for (int i = 0; i < m; i++) {
long long l = v[i], r = v[i + 1];
long long tmp = 0;
for (int i = 1; i <= n; i++) {
tmp += (a[i] + l - 1) / l;
}
long long d = k / tmp;
if (d >= l && d < r) {
ans = max(d, ans);
}
}
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
int n;
int prime[maxn];
int cnt = 0;
bool flag[maxn * 2];
int ans[maxn];
void p() {
for (int i = 2; i <= n * 2; i++) {
if (!flag[i]) prime[cnt++] = i;
for (int j = 0; j < cnt; j++) {
if (i * prime[j] > maxn * 2) break;
flag[i * prime[j]] = 1;
if ((i % prime[j]) == 0) break;
}
}
}
int main() {
cin >> n;
int x;
p();
int num1 = 0, num2 = 0;
int sum = 0;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x == 1)
num1++;
else
num2++;
}
if (num2 > 0) {
cout << 2;
num2--;
}
int now = 1;
while (num1 > 0 && num2 > 0) {
int d = prime[now] - prime[now - 1];
while (d > 1 && num2 > 0) {
cout << " " << 2;
d -= 2;
num2--;
}
while (d > 0 && num1 > 0) {
d--;
cout << " " << 1;
num1--;
}
now++;
}
while (num2 > 0) {
cout << " " << 2;
num2--;
}
while (num1 > 0) {
cout << " " << 1;
num1--;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct team {
string nam;
int pnts, sc, mis;
};
int n;
struct team t[50];
void add(const string &s, int a, int b) {
for (int i = 0; i < n; ++i)
if (t[i].nam == s) {
if (a > b)
t[i].pnts += 3;
else if (a == b)
t[i].pnts += 1;
t[i].sc += a;
t[i].mis += b;
break;
}
}
bool l1(const team &a, const team &b) {
if (a.pnts != b.pnts)
return a.pnts > b.pnts;
else if (a.sc - a.mis != b.sc - b.mis)
return a.sc - a.mis > b.sc - b.mis;
else
return a.sc > b.sc;
}
bool l2(const team &a, const team &b) { return a.nam < b.nam; }
int main() {
string a, b;
int c, d;
cin >> n;
for (int i = 0; i < n; ++i) cin >> t[i].nam;
cin.ignore();
for (int i = 0; i < n * (n - 1) / 2; ++i) {
getline(cin, a, '-');
getline(cin, b, ' ');
scanf("%d:%d", &c, &d);
cin.ignore();
add(a, c, d);
add(b, d, c);
}
sort(t, t + n, l1);
sort(t, t + n / 2, l2);
for (int i = 0; i < n / 2; ++i) cout << t[i].nam << endl;
return 0;
}
| 1 |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX_N 100005
int par[MAX_N];
int ran[MAX_N];
void init(int n){
for(int i = 0; i < n; i++){
par[i] = i;
ran[i] = 0;
}
}
int find(int x){
if(par[x] == x)return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
if(ran[x] < ran[y]){
par[x] = y;
}else{
par[y] = x;
if(ran[x] == ran[y]) ran[x]++;
}
}
bool same(int x,int y){
return find(x) == find(y);
}
int n,m,p[100003],ans,x,y;
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){cin>>p[i];p[i]--;}
init(n);
for(int i=0;i<m;i++){
cin>>x>>y;x--;y--;unite(x,y);
}
for(int i=0;i<n;i++){
if(same(i,p[i])==true)ans++;
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin >> t;
while (t--)
{
int n, s, y, x, z;
cin >> n >> s >> y >> x >> z;
int q = 0, f = 0, ans = 1;
if (y == n)q++;
if (z == n)q++;
if (y == n - 1)f++;
if (z == n - 1)f++;
if (s < q || x < q)ans = 0;
if (s + x < f+2*q)ans = 0;
q = 0, f = 0;
if (s == n)q++;
if (x == n)q++;
if (s == n - 1)f++;
if (x == n - 1)f++;
if (y < q || z < q)ans = 0;
if (y + z < f+2*q)ans = 0;
if (ans)cout << "YES" << endl;
else cout << "NO" << endl;
}
} | 2 |
#include <bits/stdc++.h>
using namespace std;
int l;
char s[110000];
int main() {
while (~scanf("%s", s)) {
int l = strlen(s);
long long a1 = 0, a2 = 0, b1 = 0, b2 = 0, e = 0, o = 0;
for (int i = 0; i < l; i++) {
if (s[i] == 'a') {
if (i % 2) {
a1++;
o += a1;
e += a2;
} else {
a2++;
o += a2;
e += a1;
}
} else {
if (i % 2) {
b1++;
o += b1;
e += b2;
} else {
b2++;
o += b2;
e += b1;
}
}
}
cout << e << " " << o << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
istringstream test(str);
string word;
string num;
int number;
stack<pair<string, int>> s;
vector<vector<string>> v;
int depth = 0;
while (getline(test, word, ',')) {
getline(test, num, ',');
number = stoi(num);
if (depth == v.size()) {
vector<string> t;
v.push_back(t);
}
v[depth].push_back(word);
if (number == 0) {
depth--;
if (!s.empty()) s.top().second--;
while (!s.empty() && s.top().second == 0) {
depth--;
s.pop();
if (!s.empty()) s.top().second--;
}
} else {
s.push(make_pair(word, number));
}
depth++;
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++) {
cout << v[i][j] << " ";
}
cout << endl;
}
return 0;
}
| 5 |
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-10;
const double PI = acos(-1.0);
typedef vector<vvi> vvvi;
int main(){
int n,K;
cin>>n>>K;
vi x,y,d;
vi x1(n),y1(n),d1(n);
vi x2(n),y2(n),d2(n);
REP(i,n){
cin>>x1[i]>>y1[i]>>d1[i];
cin>>x2[i]>>y2[i]>>d2[i];
x.push_back(x1[i]);
x.push_back(x2[i]);
y.push_back(y1[i]);
y.push_back(y2[i]);
d.push_back(d1[i]);
d.push_back(d2[i]);
}
sort(ALL(x));
sort(ALL(y));
sort(ALL(d));
x.erase(unique(ALL(x)),x.end());
y.erase(unique(ALL(y)),y.end());
d.erase(unique(ALL(d)),d.end());
vvvi f(x.size(),vvi(y.size(),vi(d.size())));
REP(i,n){
REP(j,x.size()-1){
REP(k,y.size()-1){
REP(l,d.size()-1){
if(x1[i]<=x[j]&&x[j+1]<=x2[i]&&
y1[i]<=y[k]&&y[k+1]<=y2[i]&&
d1[i]<=d[l]&&d[l+1]<=d2[i]){
f[j][k][l]++;
}
}
}
}
}
ll ans=0;
REP(j,x.size()-1){
REP(k,y.size()-1){
REP(l,d.size()-1){
if(f[j][k][l]>=K){
ans+=(ll)(x[j+1]-x[j])*(y[k+1]-y[k])*(d[l+1]-d[l]);
//cerr<<"x"<<x[j]<<"-"<<x[j+1]<<endl;
//cerr<<"y"<<y[k]<<"-"<<y[k+1]<<endl;
//cerr<<"d"<<d[l]<<"-"<<d[l+1]<<endl;
}
}
}
}
cout<<ans<<endl;
} | 0 |
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
#include <ctime>
#include <numeric>
using namespace std;
static const double EPS = 1e-8;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define SZ(a) (int(a.size()))
#define F first
#define S second
const double pi=acos(-1);
int dx[]={0,1,0,-1,1,1,-1,-1},dy[]={1,0,-1,0,1,-1,1,-1};
int w,h;
vector<pair<int,PI > > G[3000];
int s2i(const string &in){
stringstream ss(in);
int ret;
ss>>ret;
return ret;
}
void solve(){
rep(i,3000)G[i].clear();
string tt;
PI go;
int maxv=0;
rep(i,h)rep(j,w){
cin>>tt;
if(tt=="S")G[0].pb(mp(0,mp(i,j)));
else if(tt==".")continue;
else if(tt!="G"){
int v=s2i(tt);
maxv=max(v,maxv);
G[v].pb(mp(1<<28,mp(i,j)));
}else{
go=mp(i,j);
}
}
G[maxv+1].pb(mp(1<<28,go));
for(int i=0;i<=maxv;++i){
FOR(it1,G[i]){
FOR(it2,G[i+1]){
it2->F=min(it2->F,
it1->F+abs(it1->S.F-it2->S.F)+abs(it1->S.S-it2->S.S));
}
}
}
cout<<G[maxv+1][0].F<<endl;
}
int main(){
while(cin>>w>>h,w)solve();
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
bool arr[26];
string ip;
string op;
cin >> ip;
op = "YES";
if (ip[0] != 'a') {
cout << "NO" << endl;
exit(0);
} else {
int maxi = 'a';
for (int i = 1; i < ip.length(); i++) {
if (ip[i] > maxi + 1) {
op = "NO";
} else {
maxi = max(maxi, (int)ip[i]);
}
}
cout << op << endl;
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b;
cin >> a >> b;
int A[101][101]={0};
for(int i=0;i<a;i++){
for(int j = 0; j <b;j++){
cin >> A[i][j];
A[i][b] += A[i][j];
A[a][j] += A[i][j];
A[a][b] += A[i][j];
}
}
for(int i = 0; i<=a;i++){
for(int j = 0; j<=b;j++){
cout <<A[i][j] ;
if(j != b)cout<< ' ';
}
cout << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000009LL;
long long large = 2000000000000000000LL;
const long double pi = acos(-1.0);
const long double eps = 1e-10;
struct Vector {
long double x, y;
Vector(long double x = 0, long double y = 0) : x(x), y(y) {}
void init(long double a, long double b) {
x = a;
y = b;
}
};
Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator*(Vector A, long double p) { return Vector(p * A.x, p * A.y); }
Vector operator/(Vector A, long double p) { return Vector(A.x / p, A.y / p); }
bool operator<(const Vector &A, const Vector &B) {
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
int dcmp(long double x) {
if (fabs(x) < eps)
return 0;
else
return x < 0 ? -1 : 1;
}
bool operator==(const Vector &A, const Vector &B) {
return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0;
}
long double dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
long double length(Vector A) { return sqrt(dot(A, A)); }
long double angle(Vector A) { return atan2(A.y, A.x); }
long double angle(Vector A, Vector B) {
long double r = dot(A, B) / length(A) / length(B);
r = min(r, (long double)1.0);
r = max(r, (long double)-1.0);
return acos(r);
}
long double cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
Vector rotate(Vector A, long double rad) {
return Vector(A.x * cos(rad) - A.y * sin(rad),
A.x * sin(rad) + A.y * cos(rad));
}
Vector rotate(Vector A, Vector P, long double rad) {
Vector temp = A - P;
temp = rotate(temp, rad);
return temp + P;
}
Vector normal(Vector A) {
long double L = length(A);
return Vector(-A.y / L, A.x / L);
}
int main() {
int n, q;
cin >> n >> q;
vector<Vector> p(n, Vector());
for (int i = 0; i < n; i++) cin >> p[i].x >> p[i].y;
long double wx = 0, wy = 0, w = 0;
for (int i = 0; i < n; i++) {
Vector c = p[i] - p[0] + p[(i + 1) % n] - p[0];
c = c / 3.0;
long double a = cross(p[i] - p[0], p[(i + 1) % n] - p[0]);
w += a;
wx += a * c.x;
wy += a * c.y;
}
Vector c(wx / w, wy / w);
c = c + p[0];
for (int i = 0; i < n; i++) p[i] = p[i] - c;
Vector ref = c;
vector<int> pin(2, -1);
pin[0] = 0;
pin[1] = 1;
int grav = -1;
Vector off;
long double ang = 0;
while (q--) {
int type;
scanf("%d", &type);
if (type == 1) {
int take, to;
scanf("%d%d", &take, &to);
take--;
to--;
int t = -1;
for (int i = 0; i < 2; i++) {
if (pin[i] == take) {
t = i;
break;
}
}
Vector ori = p[pin[t]];
pin[t] = to;
if (1 - t == grav) continue;
if (grav == -1) {
Vector l1 = p[pin[1 - t]];
ref = ref + l1 + Vector(0, -length(l1));
} else {
Vector l1 = ori;
Vector l2 = p[pin[1 - t]];
long double ang = angle(l1, Vector(0, 1));
if (l1.x < 0) ang = -ang;
l2 = rotate(l2, ang);
ref = ref + l2 + Vector(0, -length(l2));
}
grav = 1 - t;
} else {
int t;
scanf("%d", &t);
t--;
Vector ans = p[t];
cout << setprecision(6) << fixed;
if (grav == -1)
cout << (ans + ref).x << " " << (ans + ref).y << endl;
else {
Vector l1 = p[pin[grav]];
long double ang = angle(l1, Vector(0, 1));
if (l1.x < 0) ang = -ang;
Vector ans = ref + rotate(p[t], ang);
cout << ans.x << " " << ans.y << endl;
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAX = 2e5 + 10;
int main()
{
int n; cin>>n;
int a[MAX]; int ans=0;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++){
if(a[i] == i+1){
swap(a[i],a[i+1]); ans++;
}
}
cout<<ans;
}
// abcab | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long LINF = (long long)1e18 + 47;
const int INF = 1e9 + 47;
const int MOD = 998244353;
const int modulo = 1e8;
const int nax = 2 * (int)1e5 + 10;
const double EPS = 1e-9;
const double PI = acos(-1.0);
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
int tt;
cin >> tt;
for (int test = (1); test < (tt + 1); test++) {
long long n, k;
cin >> n >> k;
long long l1, r1;
cin >> l1 >> r1;
long long l2, r2;
cin >> l2 >> r2;
if (l1 > l2) swap(l2, l1);
if (r1 > r2) swap(r1, r2);
long long ans = LINF;
if (l2 <= r1) {
long long cur = (r1 - l2) * n;
if (cur >= k) {
cout << 0 << '\n';
continue;
}
long long have = ((l2 - l1) + (r2 - r1)) * n;
if (have + cur >= k) {
cout << k - cur << '\n';
} else {
cur += have;
ans = ((l2 - l1) + (r2 - r1)) * n;
k -= cur;
cout << ans + k * 2 << '\n';
}
continue;
}
long long invest = l2 - r1;
for (int i = (0); i < (n); i++) {
long long price = invest * (i + 1);
long long have = (r2 - l1) * (i + 1);
if (have >= k) {
ans = min(ans, price + k);
} else {
ans = min(ans, have + (k - have) * 2 + price);
}
}
cout << ans << '\n';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n, j, k;
cin >> n;
string s, ans = "", ans2 = "";
cin >> s;
for (j = 0; j < n; ++j)
if (s[j] == '1')
break;
else
ans.push_back('0');
for (k = n - 1; k >= 0; --k)
if (s[k] == '0')
break;
else
ans2.push_back('1');
if (j <= k) ans.push_back('0');
for (auto it : ans) cout << it;
for (auto it2 : ans2) cout << it2;
cout << "\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
const long long inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
vector<int> a;
int vis[maxn];
int fin[maxn];
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int k;
scanf("%d", &k);
for (int j = 1; j <= k; j++) {
int x;
scanf("%d", &x);
if (!fin[i] && !vis[x]) {
vis[x] = 1;
fin[i] = 1;
}
}
}
int ans1 = 0, ans2 = 0;
int flg = 0;
for (int i = 1; i <= n && !flg; i++) {
if (!fin[i]) {
for (int j = 1; j <= n && !flg; j++) {
if (!vis[j]) {
ans1 = i;
ans2 = j;
flg = 1;
}
}
}
}
if (ans1) {
printf("IMPROVE\n");
printf("%d %d\n", ans1, ans2);
} else
printf("OPTIMAL\n");
for (int i = 1; i <= n; i++) {
vis[i] = 0;
fin[i] = 0;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 205;
const int INF = 0x3f3f3f3f;
int sg[N][3][3];
int vis[N << 4];
pair<int, int> p[N];
int main() {
ios ::sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = (int)(1); i <= (int)(m); ++i) {
cin >> p[i].first >> p[i].second;
p[i].first--, p[i].second--;
}
sort(p + 1, p + m + 1);
int ca = 0;
for (int len = (int)(1); len <= (int)(n); ++len) {
for (int left = 0; left < (int)(3); ++left) {
for (int right = 0; right < (int)(3); ++right) {
ca++;
for (int move = 0; move < (int)(len); ++move) {
for (int at = (int)(1); at <= (int)(2); ++at) {
if (move == 0 && at + left == 3) continue;
if (move == len - 1 && at + right == 3) continue;
int tmp = sg[move][left][at] ^ sg[len - 1 - move][at][right];
vis[tmp] = ca;
}
}
for (int i = 0; i < (int)(INF); ++i)
if (vis[i] < ca) {
sg[len][left][right] = i;
break;
}
}
}
}
int pre = 0, left = 0, ans = 0;
for (int i = (int)(1); i <= (int)(m); ++i) {
int at = 0;
if (p[i].second == 0) at = 1;
if (p[i].second == 1) at = 2;
ans ^= sg[p[i].first - pre][left][at];
pre = p[i].first + 1;
left = at;
}
ans ^= sg[n - pre][left][0];
cout << (ans ? "WIN" : "LOSE") << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct data {
long long l, r, a, s;
} t[4 * 150100];
long long x[150100], p[150100], n, a[150100], m, c;
data ini(long long val) {
data a;
a.l = a.r = a.a = (val > 0 ? val : 0);
a.s = val;
return a;
}
data comb(data l, data r) {
data z;
z.l = max(l.l, (l.s + r.l));
z.r = max(r.r, (r.s + l.r));
z.s = l.s + r.s;
z.a = max(max(l.a, r.a), (l.r + r.l));
return z;
}
void make(int v, int l, int r) {
if (l == r) {
t[v] = ini(a[l]);
return;
}
int m = (l + r) / 2;
make(2 * v, l, m);
make(2 * v + 1, m + 1, r);
t[v] = comb(t[2 * v], t[2 * v + 1]);
}
data que(int v, int l, int r, int le, int re) {
if (le == l && re == r) return t[v];
int m = (l + r) / 2;
if (re <= m) return que(2 * v, l, m, le, re);
if (m + 1 <= le) return que(2 * v + 1, m + 1, r, le, re);
return comb(que(2 * v, l, m, le, m), que(2 * v + 1, m + 1, r, m + 1, re));
}
void read(void) {
cin >> n >> m >> c;
for (int i = 0; i < n; ++i) cin >> x[i];
for (int i = 1; i < n; ++i) cin >> p[i];
for (int i = 1; i < n; ++i) a[i] = 100 * (x[i] - x[i - 1]) - 2 * c * p[i];
}
void kill(void) {
make(1, 1, n - 1);
long long ans = 0;
for (int i = 0, l, r; i < m; ++i) {
cin >> l >> r;
r--;
ans = ans + que(1, 1, n - 1, l, r).a;
}
cout << (double)ans / 200 << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cout.precision(9);
cout << fixed;
read();
kill();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, c;
long long ucln(long long a, long long b) {
long long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
long long tam = 1;
while (n--) {
cin >> c;
tam = (tam * c) / ucln(tam, c);
tam = ucln(tam, k);
if (tam == k) break;
}
if (tam == k)
cout << "Yes";
else
cout << "No";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, par;
int x[2001], y[2001];
map<pair<int, int>, int> a;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x[i];
cin >> y[i];
for (int j = 1; j < i; j++) par += a[make_pair(x[i] + x[j], y[i] + y[j])]++;
}
cout << par;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MXN = 1e5 + 20, SQ = 350;
int ext[SQ][SQ], jumps[SQ][SQ];
int p[MXN];
int n, q;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> q;
for (int i = 0; i < n; i++) {
cin >> p[i];
}
for (int i = n - 1; i >= 0; i--) {
if (i + p[i] >= min(SQ * (i / SQ + 1), n)) {
ext[i / SQ][i % SQ] = i;
jumps[i / SQ][i % SQ] = 1;
} else {
ext[i / SQ][i % SQ] = ext[(i + p[i]) / SQ][(i + p[i]) % SQ];
jumps[i / SQ][i % SQ] = 1 + jumps[(i + p[i]) / SQ][(i + p[i]) % SQ];
}
}
while (q--) {
int type, a, b;
cin >> type;
if (type == 0) {
cin >> a >> b;
a--;
p[a] = b;
for (int i = a; i >= a / SQ * SQ; i--) {
if (i + p[i] >= min(SQ * (i / SQ + 1), n)) {
ext[i / SQ][i % SQ] = i;
jumps[i / SQ][i % SQ] = 1;
} else {
ext[i / SQ][i % SQ] = ext[(i + p[i]) / SQ][(i + p[i]) % SQ];
jumps[i / SQ][i % SQ] = 1 + jumps[(i + p[i]) / SQ][(i + p[i]) % SQ];
}
}
} else {
cin >> a;
a--;
int lst = 0, x = 0;
while (a < n) {
x += jumps[a / SQ][a % SQ];
a = ext[a / SQ][a % SQ];
lst = a;
a += p[a];
}
cout << lst + 1 << " " << x << "\n";
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 55, mod = 1000000007;
int dp[N][N][N][N], n, c[N], pw[N], jo;
void upd(int& x) {
if (x >= mod) x -= mod;
}
int main() {
scanf("%d%d", &n, &jo);
pw[0] = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
pw[i] = pw[i - 1] * 2 % mod;
}
dp[0][0][0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int b0 = 0; b0 < i; b0++) {
for (int b1 = 0; b0 + b1 < i; b1++) {
for (int c0 = 0; b0 + b1 + c0 < i; c0++) {
int c1 = i - b0 - b1 - c0 - 1;
if (c[i] == -1 || c[i] == 0) {
if (c1) {
dp[i][b0 + 1][b1][c0] +=
(long long)dp[i - 1][b0][b1][c0] * pw[i - 2] % mod;
dp[i][b0][b1 + 1][c0] +=
(long long)dp[i - 1][b0][b1][c0] * pw[i - 2] % mod;
} else {
dp[i][b0][b1 + 1][c0] +=
(long long)dp[i - 1][b0][b1][c0] * pw[i - 1] % mod;
}
upd(dp[i][b0 + 1][b1][c0]), upd(dp[i][b0][b1 + 1][c0]);
}
if (c[i] == -1 || c[i] == 1) {
if (b1) {
dp[i][b0][b1][c0 + 1] +=
(long long)dp[i - 1][b0][b1][c0] * pw[i - 2] % mod;
dp[i][b0][b1][c0] +=
(long long)dp[i - 1][b0][b1][c0] * pw[i - 2] % mod;
} else {
dp[i][b0][b1][c0] +=
(long long)dp[i - 1][b0][b1][c0] * pw[i - 1] % mod;
}
upd(dp[i][b0][b1][c0 + 1]), upd(dp[i][b0][b1][c0]);
}
}
}
}
}
int ans = 0;
for (int b0 = 0; b0 <= n; b0++) {
for (int b1 = 0; b0 + b1 <= n; b1++) {
for (int c0 = 0; b0 + b1 + c0 <= n; c0++) {
int c1 = n - b0 - b1 - c0;
if ((b1 + c1 & 1) == jo) ans += dp[n][b0][b1][c0], upd(ans);
}
}
}
printf("%d\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const long long P = 1e17 + 3;
const int N = 18 + 2, K = 128 + 2, H = 23;
double w[4] = {1. / 6, 1. / 6, 1. / 3, 1. / 3};
unordered_map<long long, double> mp;
long long h, pwr[K] = {1};
int n, a[N][3], ted[N];
void _sum(long long &a, long long b) {
if ((a += b) < 0) a += P;
if (a >= P) a -= P;
}
long long get_hash(int p) {
int h = min(a[p][0], a[p][2]) + 4 * a[p][1] + 16 * max(a[p][0], a[p][2]);
return p < n - 1 ? !a[p][1] || ted[p] < 2 ? 0 : pwr[h] : pwr[64 + h];
}
double bt() {
if (mp.count(h)) return mp[h];
bool t = ted[n - 1] > 2;
double p[4] = {K, K, K, K};
double cur = 1, expect = 1;
_sum(h, -get_hash(n - 1)), n += t, ted[n - 1]++;
if (t) _sum(h, get_hash(n - 2));
for (int i = 0; i < n - t - 1; i++)
for (int j = 0; j < 3; j++)
if (a[i][j] && (ted[i] > 2 || (a[i][1] && j ^ 1))) {
int x = a[i][j];
_sum(h, -get_hash(i)), ted[i]--, a[i][j] = 0, _sum(h, get_hash(i));
for (int k = 0; k < 3; k++)
if (!a[n - 1][k]) {
a[n - 1][k] = x, _sum(h, get_hash(n - 1)), p[x] = min(p[x], bt());
_sum(h, -get_hash(n - 1)), a[n - 1][k] = 0;
}
_sum(h, -get_hash(i)), ted[i]++, a[i][j] = x, _sum(h, get_hash(i));
}
if (t) _sum(h, -get_hash(n - 2));
ted[n - 1]--, n -= t, _sum(h, get_hash(n - 1));
for (int i = 0; i < 4; i++)
p[i] + eps < K ? expect += w[i] * p[i] : cur -= w[i];
return mp[h] = cur > eps ? expect / cur : 0;
}
int main() {
cin >> n;
for (int i = 1; i < K; i++) pwr[i] = 1LL * pwr[i - 1] * H % P;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
char c;
cin >> c;
if (c == 'R') a[i][j] = 1;
if (c == 'G') a[i][j] = 2;
if (c == 'B') a[i][j] = 3;
}
ted[i] = 3, _sum(h, get_hash(i));
}
cout << fixed << bt();
}
| 5 |
#include <iostream>
using namespace std;
int mtoi(const string& str)
{
int res = 0,temp = 1;
for (int j = 0; j < str.length(); j++) {
if(str[j] >= '0' && str[j] <= '9')
temp = (int)(str[j] - '0');
else if (str[j] == 'i') {
res += temp;
temp = 1;
}
else if (str[j] == 'x') {
res += temp * 10;
temp = 1;
}
else if (str[j] == 'c') {
res += temp * 100;
temp = 1;
}
else if (str[j] == 'm') {
res += temp * 1000;
temp = 1;
}
}
return res;
}
int main()
{
int n,res;
string a,b;
char ch;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b;
res = mtoi(a) + mtoi(b);
if (res / 1000) {
if (res / 1000 != 1) {
cout << res / 1000;
}
cout << 'm';
res -= res / 1000 * 1000;
}
if (res / 100) {
if (res / 100 != 1) {
cout << res / 100;
}
cout << 'c';
res -= res / 100 * 100;
}
if (res / 10) {
if (res / 10 != 1) {
cout << res / 10;
}
cout << 'x';
res -= res / 10 * 10;
}
if (res) {
if (res != 1) {
cout << res;
}
cout << 'i';
}
cout << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9 + 7;
vector<pair<long long, long long>> adj[101];
map<pair<long long, long long>, long long> mp;
map<long long, long long> ans;
long long start, dest;
void dfs(long long node, long long col) {
mp[{node, col}] = 1;
if (node == dest) ans[col]++;
for (auto child : adj[node]) {
if (mp[{child.first, child.second}] == 0 &&
(child.second == col || col == -1)) {
dfs(child.first, child.second);
}
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= m; i++) {
long long u, v, c;
cin >> u >> v >> c;
adj[u].push_back({v, c});
adj[v].push_back({u, c});
}
long long q;
cin >> q;
while (q--) {
ans.clear();
mp.clear();
cin >> start >> dest;
dfs(start, -1);
long long temp = 0;
for (auto x : ans) {
if (x.second != 0) temp++;
}
cout << temp << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
if(s[0]==s[s.size()-1]){
if(s.size()%2==1) printf("Second");
else printf("First");
}
else {
if(s.size()%2==1) printf("First");
else printf("Second");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long cy[] = {1,
60,
300,
1500,
15000,
150000,
1500000,
15000000,
150000000,
1500000000,
15000000000ll,
150000000000ll,
1500000000000ll,
15000000000000ll};
const long long ex10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000ll,
100000000000ll,
1000000000000ll,
10000000000000ll};
long long r;
long long mul(long long a, long long b, long long mod) {
long long res = 0;
for (long long x = 1ll << 60; x; x >>= 1) {
res <<= 1;
if (x & b) res += a;
res %= mod;
}
return res;
}
long long fib(long long k, long long mod) {
long long mar[63][3][3];
memset(mar, 0, sizeof(mar));
mar[0][1][1] = 0;
mar[0][1][2] = 1;
mar[0][2][1] = 1;
mar[0][2][2] = 1;
for (int t = 1; t <= 60; t++) {
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 2; j++)
for (int k = 1; k <= 2; k++) {
mar[t][i][k] += mul(mar[t - 1][i][j], mar[t - 1][j][k], mod);
mar[t][i][k] %= mod;
}
}
long long res[2][3];
int now = 0;
res[now][1] = 0;
res[now][2] = 1;
for (int t = 0; t <= 60; t++)
if ((1ll << t) & k) {
now ^= 1;
for (int i = 1; i <= 2; i++) res[now][i] = 0;
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 2; j++) {
res[now][j] += mul(res[now ^ 1][i], mar[t][i][j], mod);
res[now][j] %= mod;
}
}
return res[now][1];
}
vector<long long> ans[15];
int D = 13;
int main() {
cin >> r;
ans[0].push_back(0);
for (int i = 1; i <= D; i++) {
for (int k = 0; k < cy[i] / cy[i - 1]; k++)
for (int j = 0; j < ans[i - 1].size(); j++)
if (fib(k * cy[i - 1] + ans[i - 1][j], ex10[i]) == r % ex10[i])
ans[i].push_back(k * cy[i - 1] + ans[i - 1][j]);
}
if (ans[D].size() == 0)
cout << -1 << endl;
else
cout << ans[D][0] << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char now[1 << 16], *S, *T;
if (T == S) {
T = (S = now) + fread(now, 1, 1 << 16, stdin);
if (T == S) return EOF;
}
return *S++;
}
inline int read() {
int x = 0, f = 1;
char ch = gc();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = gc();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
return x * f;
}
const int N = 2e5 + 10;
int a[N], n, L, b[N], gr[N << 1];
inline bool check(int len) {
static int br[N << 1];
int top = 0;
for (int i = 1; i <= n; ++i)
if (b[i] >= len) br[++top] = b[i];
for (int i = 1; i <= n; ++i)
if (b[i] + len < L) br[++top] = b[i] + L;
int i = 1, j = 1;
while (i <= top) {
while (j <= 2 * n && abs(br[i] - gr[j]) > len) ++j;
if (j == 2 * n + 1) break;
++i;
++j;
}
return i == top + 1;
}
int main() {
n = read();
L = read();
for (int i = 1; i <= n; ++i) a[i] = read();
for (int i = 1; i <= n; ++i) b[i] = read();
sort(a + 1, a + n + 1);
sort(b + 1, b + n + 1);
for (int i = 1; i <= n; ++i) gr[i] = a[i], gr[n + i] = a[i] + L;
int l = 0, r = L >> 1;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid))
r = mid - 1;
else
l = mid + 1;
}
printf("%d\n", l);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const double PI = 3.14159265358979323846;
typedef vector<int> vint;
typedef pair<int, int> pint;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int N;
int x[220], y[220];
int tw[220];
int mod = 998244353;
signed main() {
tw[0] = 1;
for (int i = 1; i < 220; i++) {
tw[i] = tw[i - 1] * 2 % mod;
}
cin >> N;
for (int i = 0; i < N; i++)cin >> x[i] >> y[i];
int ans = (tw[N] - N - 1 + mod) % mod;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
int cur = 0;
for (int k = j + 1; k < N; k++) {
if ((x[i] - x[j]) * (y[i] - y[k]) == (x[i] - x[k]) * (y[i] - y[j])) cur++;
}
(ans += mod - tw[cur]) %= mod;
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
long long r, g;
cin >> r >> g;
long long sum = r + g;
long long h = 0;
for (long long i = 1; i <= r + g + 1; i++) {
if ((i * (i + 1)) / 2 > sum) {
h = i - 1;
break;
}
}
long long arr[r + 2][2];
for (long long i = 0; i < r + 2; i++) {
arr[i][0] = 0;
arr[i][1] = 0;
}
arr[0][0] = 1;
for (long long i = 1; i <= h; i++) {
long long lst = i;
for (long long j = 0; j <= r; j++) {
if (j > ((lst * (lst + 1))) / 2) {
for (long long l = 0; l < j; l++) {
arr[l][0] = arr[l][1];
}
break;
}
if (j >= lst) {
arr[j][1] = (arr[j][0] + arr[j - lst][0]) % mod;
} else {
arr[j][1] = arr[j][0];
}
if (j == r) {
for (long long l = 0; l <= j; l++) {
arr[l][0] = arr[l][1];
}
}
}
}
long long bound = ((h * (h + 1)) / 2) - g;
long long res = 0;
for (long long i = max(bound, (long long)0); i <= r; i++) {
res = (res + arr[i][1]) % mod;
}
cout << res;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<string> a;
vector<pair<string, pair<vector<long long>, vector<long long>>>> widgets;
vector<pair<string, string>> edges;
long long used[100500];
void createWidget(string s, long long x, long long y) {
vector<long long> xx;
vector<long long> yy;
xx.push_back(0);
xx.push_back(x);
xx.push_back(y);
xx.push_back(0);
xx.push_back(0);
xx.push_back(0);
widgets.push_back(make_pair(s, make_pair(xx, yy)));
}
void createVbox(string s) {
vector<long long> xx;
vector<long long> yy;
xx.push_back(1);
xx.push_back(0);
xx.push_back(0);
xx.push_back(0);
xx.push_back(0);
xx.push_back(0);
widgets.push_back(make_pair(s, make_pair(xx, yy)));
}
void createHbox(string s) {
vector<long long> xx;
vector<long long> yy;
xx.push_back(2);
xx.push_back(0);
xx.push_back(0);
xx.push_back(0);
xx.push_back(0);
xx.push_back(0);
widgets.push_back(make_pair(s, make_pair(xx, yy)));
}
void addWidget(string s1, string s2) { edges.push_back(make_pair(s1, s2)); }
void setBorder(string s1, long long x) {
long long l = 0;
while (widgets[l].first != s1) ++l;
widgets[l].second.first[3] = x;
}
void setSpace(string s1, long long x) {
long long l = 0;
while (widgets[l].first != s1) ++l;
widgets[l].second.first[4] = x;
}
void getEdges() {
if (edges.size() != 0) {
for (long long i = 0; i < edges.size(); ++i) {
string s1 = edges[i].first;
string s2 = edges[i].second;
long long l1 = 0;
long long l2 = 0;
for (long long j = 0; j < widgets.size(); ++j) {
if (widgets[j].first == s1) l1 = j;
if (widgets[j].first == s2) l2 = j;
}
widgets[l1].second.first[5]++;
widgets[l1].second.second.push_back(l2);
}
}
}
pair<long long, long long> getSizes(long long u) {
pair<long long, long long> res;
used[u] = true;
if (widgets[u].second.second.size() != 0) {
for (long long i = 0; i < widgets[u].second.second.size(); ++i) {
long long v = widgets[u].second.second[i];
pair<long long, long long> pp;
if (!used[v])
pp = getSizes(v);
else {
pp.first = widgets[v].second.first[1];
pp.second = widgets[v].second.first[2];
}
if (i == 0) {
widgets[u].second.first[1] = pp.first + 2 * widgets[u].second.first[3];
widgets[u].second.first[2] = pp.second + 2 * widgets[u].second.first[3];
} else {
if (widgets[u].second.first[0] == 1) {
widgets[u].second.first[2] += widgets[u].second.first[4] + pp.second;
widgets[u].second.first[1] =
max(widgets[u].second.first[1],
pp.first + 2 * widgets[u].second.first[3]);
} else {
widgets[u].second.first[1] += widgets[u].second.first[4] + pp.first;
widgets[u].second.first[2] =
max(widgets[u].second.first[2],
pp.second + 2 * widgets[u].second.first[3]);
}
}
}
}
res.first = widgets[u].second.first[1];
res.second = widgets[u].second.first[2];
return res;
}
void dfs(long long u) {
if (widgets[u].second.second.size() != 0) {
for (long long i = 0; i < widgets[u].second.second.size(); ++i) {
long long v = widgets[u].second.second[i];
pair<long long, long long> pp;
if (!used[v])
pp = getSizes(v);
else {
pp.first = widgets[v].second.first[1];
pp.second = widgets[v].second.first[2];
}
if (i == 0) {
widgets[u].second.first[1] = pp.first + 2 * widgets[u].second.first[3];
widgets[u].second.first[2] = pp.second + 2 * widgets[u].second.first[3];
} else {
if (widgets[u].second.first[0] == 1) {
widgets[u].second.first[2] += widgets[u].second.first[4] + pp.second;
widgets[u].second.first[1] =
max(widgets[u].second.first[1],
pp.first + 2 * widgets[u].second.first[3]);
} else {
widgets[u].second.first[1] += widgets[u].second.first[4] + pp.first;
widgets[u].second.first[2] =
max(widgets[u].second.first[2],
pp.second + 2 * widgets[u].second.first[3]);
}
}
}
}
}
void solve() {
getEdges();
for (long long i = 0; i < widgets.size(); ++i) {
if (widgets[i].second.first[5] != 0) {
dfs(i);
}
}
}
int main() {
long long n;
cin >> n;
string s;
getline(cin, s);
for (long long i = 0; i < n; ++i) {
getline(cin, s);
a.push_back(s);
}
for (long long i = 0; i < a.size(); ++i) {
long long l = 0;
if (a[i][0] == 'W') {
s = a[i];
while (a[i][l] != ' ') {
++l;
}
++l;
string s1 = "";
while (s[l] != '(') {
s1 += s[l];
++l;
}
long long x = 0;
++l;
while (s[l] != ',') {
x = x * 10 + s[l] - '0';
++l;
}
long long y = 0;
++l;
while (s[l] != ')') {
y = y * 10 + s[l] - '0';
++l;
}
createWidget(s1, x, y);
}
if (a[i][0] == 'V') {
s = a[i];
while (s[l] != ' ') {
++l;
}
++l;
string s1 = "";
while (l < s.length()) {
s1 += s[l];
++l;
}
createVbox(s1);
}
if (a[i][0] == 'H') {
s = a[i];
while (s[l] != ' ') {
++l;
}
++l;
string s1 = "";
while (l < s.length()) {
s1 += s[l];
++l;
}
createHbox(s1);
}
if (a[i][0] != 'W' && a[i][0] != 'V' && a[i][0] != 'H') {
string s1 = "";
s = a[i];
while (s[l] != '.') {
s1 += s[l];
++l;
}
++l;
if (s[l] == 'p') {
while (s[l] != '(') ++l;
string s2 = "";
++l;
while (s[l] != ')') {
s2 += s[l];
++l;
}
addWidget(s1, s2);
} else {
while (s[l] != '_') {
++l;
}
++l;
if (s[l] == 'b') {
while (s[l] != '(') {
++l;
}
++l;
long long x = 0;
while (s[l] != ')') {
x = x * 10 + s[l] - '0';
++l;
}
setBorder(s1, x);
} else {
while (s[l] != '(') {
++l;
}
++l;
long long x = 0;
while (s[l] != ')') {
x = x * 10 + s[l] - '0';
++l;
}
setSpace(s1, x);
}
}
}
}
sort(widgets.begin(), widgets.end());
solve();
for (long long i = 0; i < widgets.size(); ++i) {
cout << widgets[i].first << ' ' << widgets[i].second.first[1] << ' '
<< widgets[i].second.first[2] << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int p, q, n, m;
int c[110];
int d[110];
char a[110], b[110];
int main() {
scanf("%d%d", &p, &q);
scanf("%s", a + 1);
scanf("%s", b + 1);
n = strlen(a + 1);
m = strlen(b + 1);
for (int i = 1; i <= m; i++) {
int k = i;
for (int j = 1; j <= n; j++) {
if (a[j] == b[k]) {
k++;
if (k == m + 1) {
k = 1;
d[i]++;
}
}
}
c[i] = k;
}
int j = 1;
long long ans = 0;
for (int i = 1; i <= p; i++) {
ans += d[j];
j = c[j];
}
cout << ans / q << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int N;
double pEatenBy[18][1 << 18];
double a[18][18];
double dp[1 << 18];
int pows[1 << 18];
double getVals(int i, int m) {
if (m == 0) return 0;
int mone = m & (~(m - 1));
int ind = pows[mone];
return pEatenBy[i][m] < -0.5
? pEatenBy[i][m] = a[ind][i] + getVals(i, m & (~(mone)))
: pEatenBy[i][m];
}
double f(int mask) {
int ones = __builtin_popcount(mask);
if (ones == N) return 1;
double& r = dp[mask];
if (r > -0.5) return r;
r = 0;
for (int i = 0; i < N; i++)
if (!(mask & (1 << i))) r += f(mask | (1 << i)) * pEatenBy[i][mask];
r *= (2.0 / ((double)ones * (ones + 1)));
return r;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++) scanf("%lf", &a[i][j]);
for (int i = 0; i < 1 << N; i++) dp[i] = -1;
for (int i = 0; i < N; i++)
for (int j = 0; j < 1 << N; j++) pEatenBy[i][j] = -1;
for (int i = 0; i < N; i++) pows[1 << i] = i;
for (int victim = 0; victim < N; victim++)
for (int mask = 0; mask < 1 << N; mask++) getVals(victim, mask);
for (int i = 0; i < N; i++) {
double x = f(1 << i);
if (i == 0)
printf("%.6lf", x);
else
printf(" %.6lf", x);
}
printf("\n");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
string s;
cin >> s;
long long n = s.length();
vector<long long> fibo(n + 1, 0);
fibo[1] = 1;
fibo[2] = 2;
for (long long i = 3; i <= n; i++) {
fibo[i] = (fibo[i - 1] + fibo[i - 2]) % 1000000007;
}
bool chk = false;
for (long long i = 0; i < n; i++) {
if (s[i] == 'm' || s[i] == 'w') {
chk = true;
break;
}
}
if (chk) {
cout << 0 << endl;
return 0;
}
vector<long long> grup;
long long counter_u = 0;
long long counter_n = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == 'n') {
counter_n++;
} else {
if (counter_n != 0) {
grup.push_back(counter_n);
counter_n = 0;
}
}
}
if (counter_n != 0) {
grup.push_back(counter_n);
counter_n = 0;
}
for (long long i = 0; i < n; i++) {
if (s[i] == 'u') {
counter_u++;
} else {
if (counter_u != 0) {
grup.push_back(counter_u);
counter_u = 0;
}
}
}
if (counter_u != 0) {
grup.push_back(counter_u);
counter_u = 0;
}
long long ans = 1;
for (long long i = 0; i < grup.size(); i++) {
ans = ans * fibo[grup[i]];
ans %= 1000000007;
}
cout << ans << endl;
return 0;
}
| 3 |
#include <iostream>
#include <vector>
#define int long long
using namespace std;
struct node {
int i, j;
node *left, *right;
int val, lazy;
};
vector<int> a;
node* build(int i, int j) {
if (i == j) {
return new node{i, j, nullptr, nullptr, a[i], 0};
}
node *left = build(i, (i + j) / 2);
node *right = build((i + j) / 2 + 1, j);
return new node{i, j, left, right, left->val + right->val, 0};
}
void propagate(node* cur) {
if (cur->lazy != 0) {
cur->val += (cur->j - cur->i + 1) * cur->lazy;
if (cur->left != nullptr) {
cur->left->lazy += cur->lazy;
cur->right->lazy += cur->lazy;
}
cur->lazy = 0;
}
}
int query(node *cur, int i, int j) {
propagate(cur);
if (cur->i > j || cur->j < i) {
return 0;
}
if (cur->i >= i && cur->j <= j) {
return cur->val;
}
return query(cur->left, i, j) + query(cur->right, i, j);
}
void update(node* cur, int i, int j, int newVal) {
//cout << i << ' ' << j << endl;
//cout << cur->i << ' ' << cur->j << endl;
propagate(cur);
if (cur->i > j || cur->j < i) {
return;
}
if (cur->i >= i && cur->j <= j) {
cur->lazy += newVal;
propagate(cur);
return;
}
update(cur->left, i, j, newVal);
update(cur->right, i, j, newVal);
cur->val = cur->left->val + cur->right->val;
return;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
//cout << n << q << endl;
a = vector<int>(n, 0);
node *root = build(0, n - 1);
//cout << root->val << endl;
while (q--) {
int c;
int s, t, x;
cin >> c >> s >> t;
//cout << s << t << endl;
if (c == 0) {
cin >> x;
update(root, s - 1, t - 1, x);
} else {
cout << query(root, s - 1, t - 1) << '\n';
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, k, v, cnt, mx;
unordered_map<int, int> frq, play, cost;
int mem[600][6000];
int dp(int i, int sum) {
if (i == cnt) {
return 0;
}
int& ret = mem[i][sum];
if (~ret) return ret;
ret = 0;
for (int j = 1; j <= k; j++) {
if (sum - j >= 0) {
ret = max(ret, cost[j] + dp(i + 1, sum - j));
}
}
return ret;
}
int main() {
cin >> n >> k;
for (int i = 0; i < n * k; i++) {
cin >> v;
frq[v]++;
}
for (int i = 0; i < n; i++) {
cin >> v;
play[v]++;
}
for (int i = 1; i <= k; i++) {
cin >> cost[i];
}
int sum = 0;
for (auto i : play) {
memset(mem, -1, sizeof mem);
cnt = i.second, mx = frq[i.first];
sum += dp(0, mx);
}
cout << sum;
}
| 6 |
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int main(){
int a;
cin >> a;
cout << (a-30)/2 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int x, co = 0;
cin >> x;
for (int n = 1; n <= x; n++) {
for (int m = n; m <= x; m++) {
double a = sqrt(n * n + m * m);
if (floor(a) == ceil(a) && (a <= x)) {
co++;
}
}
}
cout << " " << co << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 35005, K = 55;
int dp[N][K];
int seg[N << 2], push[N << 2], a[N], prv[N], tmp[N];
void pusher(int id) {
seg[id << 1] += push[id];
seg[id << 1 | 1] += push[id];
push[id << 1] += push[id];
push[id << 1 | 1] += push[id];
push[id] = 0;
}
void up(int val, int l, int r, int L, int R, int id) {
if (push[id] && L ^ R) pusher(id);
if (R < l || r < L) return;
if (l <= L && R <= r) {
push[id] += val;
seg[id] += val;
return;
}
int m = (L + R) >> 1;
up(val, l, r, L, m, id << 1);
up(val, l, r, m + 1, R, id << 1 | 1);
seg[id] = max(seg[id << 1], seg[id << 1 | 1]);
}
int qu(int l, int r, int L, int R, int id) {
if (push[id] && L ^ R) pusher(id);
if (R < l || r < L) return 0;
if (l <= L && R <= r) return seg[id];
int m = (L + R) >> 1;
return max(qu(l, r, L, m, id << 1), qu(l, r, m + 1, R, id << 1 | 1));
}
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) {
prv[i] = tmp[a[i]];
tmp[a[i]] = i;
}
memset(tmp, 0, sizeof(tmp));
int ct = 0;
for (int i = 1; i <= n; i++) {
dp[i][1] = dp[i - 1][1] + (tmp[a[i]] == 0);
tmp[a[i]] = 1;
}
for (int i = 2; i <= k; i++) {
for (int j = 0; j < 4 * N; j++) seg[j] = push[j] = 0;
for (int j = 1; j <= n; j++) up(dp[j][i - 1], j, j, 1, n, 1);
for (int j = i; j <= n; j++) {
up(1, max(1, prv[j]), j - 1, 1, n, 1);
dp[j][i] = qu(1, j - 1, 1, n, 1);
}
}
cout << dp[n][k] << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 rng(1477);
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
ll res = 0;
int neg = 0, pos = 0, zer = 0;
for (int i = 0; i < n; ++i) {
int t;
cin >> t;
if (t < 0) {
res += -t - 1;
++neg;
} else if (t == 0) {
++zer;
} else {
res += t - 1;
++pos;
}
}
if (neg % 2 && zer) {
++res;
--neg;
--zer;
} else if (neg % 2) {
res += 2;
}
cout << res + zer;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int c=0;
int main()
{
int n;
string s;
cin>>n>>s;
for (int i=0;i<n;i++)
if (s[i]=='R') c++;
if (c*2>n) puts("Yes");
else puts("No");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e18;
const long double EPS = 1e-9;
long long n, m;
vector<bool> used_l, used_s;
bool dfs(long long i, long long j, vector<vector<char>> &r,
vector<vector<char>> &v, bool line) {
if ((line && used_l[i]) || (!line && used_s[j])) {
return true;
}
bool res = true;
if (line) {
used_l[i] = true;
for (int j1 = 0; j1 < m; ++j1) {
if (used_s[j1] && v[i][j1] != '#') {
return false;
}
if (v[i][j1] == '#') {
res &= dfs(i, j1, r, v, !line);
}
}
} else {
used_s[j] = true;
for (int i1 = 0; i1 < n; ++i1) {
if (used_l[i1] && v[i1][j] != '#') {
return false;
}
if (v[i1][j] == '#') {
res &= dfs(i1, j, r, v, !line);
}
}
}
r[i][j] = '.';
return res;
}
int main() {
srand(time(0));
ios::sync_with_stdio(false);
cin >> n >> m;
used_l.resize(n, false), used_s.resize(m, false);
vector<vector<char>> v(n, vector<char>(m)), r;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> v[i][j];
}
}
r = v;
bool ans = true;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (r[i][j] == '#') {
ans &= dfs(i, j, r, v, true);
fill(used_l.begin(), used_l.end(), false),
fill(used_s.begin(), used_s.end(), false);
}
}
}
if (ans) {
cout << "Yes";
} else {
cout << "No";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int n, i, j;
cin >> n;
string s;
cin >> s;
cout << n + 1 << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<pair<long long, long long>, int> > P;
bool notsameline(int p) {
long long x1 = P[0].first.first, y1 = P[0].first.second;
long long x2 = P[1].first.first, y2 = P[1].first.second;
long long x3 = P[p].first.first, y3 = P[p].first.second;
if (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2) == 0) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
long long x, y;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> x >> y;
P.push_back(make_pair(make_pair(x, y), i + 1));
}
sort((P).begin(), (P).end());
for (int i = 2; i < n; ++i) {
if (notsameline(i)) {
cout << P[0].second << " " << P[1].second << " " << P[i].second << "\n";
break;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
template <typename T>
class Fenwick {
private:
vector<T> array;
int n;
public:
Fenwick(int a) {
n = a;
array.resize(n + 10, 0);
}
void put(int idx, T val) {
if (idx >= n || idx < 0) {
cout << "Out of range";
exit(1);
}
idx++;
while (idx <= n) {
array[idx] += val;
idx += (idx & -idx);
}
}
T get(int idx) {
if (idx >= n || idx < -1) {
cout << "Out of range";
exit(1);
}
idx++;
T ret = 0;
while (idx) {
ret += array[idx];
idx -= (idx & -idx);
}
return ret;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k, q;
long long int a, b;
cin >> n >> k >> a >> b >> q;
Fenwick<long long int> pre(n), post(n);
while (q--) {
int type;
cin >> type;
if (type == 1) {
int d;
long long int add;
cin >> d >> add;
d--;
long long int value = post.get(d) - post.get(d - 1);
long long int a1 = max(min(a - value, add), 0ll);
post.put(d, a1);
value = pre.get(d) - pre.get(d - 1);
a1 = max(min(b - value, add), 0LL);
pre.put(d, a1);
} else {
int d;
cin >> d;
d--;
int ret = 0;
ret += pre.get(d - 1);
ret += post.get(n - 1) - post.get(d - 1 + k);
cout << ret << '\n';
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
using namespace std;
typedef int ll;
typedef pair<ll, ll> pll;
class PMA {
PMA* next[256];
vector<ll> matched;
PMA() { REP(i, 0, 256) next[i] = NULL; }
void build(vector<string> &p) {
REP(i, 0, 256) next[i] = NULL;
this->next[0] = this;
REP(i, 0, p.size()) {
PMA *now = this;
REP(j, 0, p[i].length()) {
if(now->next[p[i][j]] == 0) now->next[p[i][j]] = new PMA;
now = now->next[p[i][j]];
}
now->matched.push_back(i);
}
queue<PMA*> q;
REP(i, 1, 256) {
if(!this->next[i]) this->next[i] = this;
else {
this->next[i]->next[0] = this;
q.push(this->next[i]);
}
}
while(!q.empty()) {
PMA *now = q.front();
q.pop();
REP(i, 1, 256) if(now->next[i]){
PMA *nxt = now->next[0];
while(!nxt->next[i]) nxt = nxt->next[0];
now->next[i]->next[0] = nxt->next[i];
vector<ll> &m1 = now->next[i]->matched, &m2 = nxt->next[i]->matched;
REP(j, 0, m2.size()) if(!binary_search(m1.begin(), m1.end(), m2[j])) m1.push_back(m2[j]);
sort(m1.begin(), m1.end());
q.push(now->next[i]);
}
}
}
public:
PMA(vector<string> &p) {
build(p);
}
vector<pll> match(string s) {
vector<pll> ret;
PMA* pma = this;
REP(i, 0, s.length()) {
while(!pma->next[s[i]]) pma = pma->next[0];
pma = pma->next[s[i]];
for(ll j : pma->matched) ret.push_back(pll(i, j));
}
return ret;
}
};
int main(void) {
string S;
cin >> S;
ll N;
cin >> N;
vector<string> P(N);
REP(i, 0, N) cin >> P[i];
sort(P.begin(), P.end());
vector<string> Q;
Q.push_back(P[0]);
REP(i, 1, P.size()) {
ll len = min(Q[(ll) Q.size() - 1].size(), P[i].size());
bool ok = true;
REP(j, 0, len) ok = ok && (Q[(ll) Q.size() - 1][j] == P[i][j]);
if(!ok) Q.push_back(P[i]);
}
PMA pma(Q);
vector<pll> p = pma.match(S);
REP(i, 0, p.size()) p[i].second = p[i].first + 1 - Q[p[i].second].length();
sort(p.begin(), p.end());
ll ans = 0, t = -1;
REP(i, 0, p.size()) {
if(t < p[i].second) {
ans++;
t = p[i].first;
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a, b, sum, uc, x, y, c[100005];
vector<long long> veca, vecb;
set<long long> myset;
int main() {
cin >> a >> b;
while (sum < a + b) {
uc++;
sum += uc;
if (sum <= a + b) c[uc] = uc;
}
x = uc;
while ((a > 0) && (uc > 0)) {
if (c[uc] != 0) {
if (a - c[uc] >= 0) {
veca.push_back(c[uc]);
a -= c[uc];
c[uc] = 0;
} else
uc--;
} else
uc--;
}
for (int i = 1; i <= x; i++)
if (c[i] != 0) {
if (b - c[i] >= 0) {
vecb.push_back(c[i]);
b -= c[i];
c[i] = 0;
} else
break;
}
cout << veca.size() << '\n';
for (int i = 0; i < veca.size(); i++) cout << veca[i] << " ";
cout << '\n';
cout << vecb.size() << '\n';
for (int i = 0; i < vecb.size(); i++) cout << vecb[i] << " ";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int ans = 0;
int pre = 0;
int uku = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == 'R')
uku++;
else
uku--;
if (uku % 4 == 0) {
if (uku > pre) ans++;
pre = uku;
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int best[2][100010];
char s[2][100010];
queue<int> qu;
int main() {
int i, j, n, k, res;
scanf("%d %d ", &n, &k);
scanf("%s %s ", s[0], s[1]);
memset(best, 9, sizeof(best));
best[0][0] = 0;
qu.push(0);
qu.push(0);
res = 0;
while (!res && !qu.empty()) {
i = qu.front();
qu.pop();
j = qu.front();
qu.pop();
if (j + k >= n) res = 1;
if (j + 1 < n) {
if (s[i][j + 1] == '-' && best[i][j + 1] > best[i][j] + 1) {
best[i][j + 1] = best[i][j] + 1;
if (best[i][j + 1] - 1 < j + 1) {
qu.push(i);
qu.push(j + 1);
}
}
}
if (j) {
if (s[i][j - 1] == '-' && best[i][j - 1] > best[i][j] + 1) {
best[i][j - 1] = best[i][j] + 1;
if (best[i][j - 1] - 1 < j - 1) {
qu.push(i);
qu.push(j - 1);
}
}
}
if (j + k < n) {
if (s[1 - i][j + k] == '-' && best[1 - i][j + k] > best[i][j] + 1) {
best[1 - i][j + k] = best[i][j] + 1;
if (best[1 - i][j + k] - 1 < j + k) {
qu.push(1 - i);
qu.push(j + k);
}
}
}
}
if (res)
puts("YES");
else
puts("NO");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a;
cin >> a;
for(int i = 1;i <= 9; ++i){
if(a/i <= 9 && (a/i) * i == a){
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
return 0;
} | 0 |
#include <iostream>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <complex>
#include <cmath>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define EPS 1e-12
using namespace std;
typedef complex<double> C;
const double PI = 4 * atan(1.0);
struct L : public vector<C>
{
L(){}
L(const C a,const C b){
push_back(a); push_back(b);
}
};
double cross(const C a,const C b)
{
return imag(conj(a)*b);
}
double dot (const C a,const C b)
{
return real(conj(a)*b);
}
C projection(const L& l,const C p)
{
double t = dot(p-l[0],l[0]-l[1]) / norm(l[0]-l[1]);
return l[0] + t*(l[0]-l[1]);
}
int ccw(C a,C b,C c)
{
b -=a,c -= a;
if(cross(b,c) > 0) return +1;
if(cross(b,c) < 0) return -1;
if(dot(b,c) < 0) return +2;
if(norm(b) < norm(c)) return -2;
return 0;
}
double intersectSP(const L& s,const C p)
{
return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < EPS;
}
double intersectSS(const L& s,const L& t)
{
return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0;
}
double distanceSP(const L& s,const C p)
{
const C r = projection(s,p);
if(intersectSP(s,r)) return abs(r-p);
return min(abs(s[0]-p),abs(s[1]-p));
}
bool dp[(1 << 16)];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> r(n);
vector<C> s(n),t(n);
rep(i,n){
int a,b,c,d,e;
cin >> a >> b >> c >> d >> e;
r[i] = a,s[i] = C(b,c),t[i] = C(d,e);
}
int mx = 0;
rep(i,(1<<n)){
dp[i] = false;
}
dp[0] = true;
for(int i=1;i<(1<<n);i++){
bool flag = false;
rep(j,n){
if(i & (1 << j)){
if(dp[i^(1<<j)]){
bool dame = false;
int cri = (i^(1<<j));
vector<int> vec(n,0);
rep(k,n){
if(cri & (1 << k)){
vec[k] = 1;
}
}
rep(k,n){
if(k == j){
continue;
}
if(vec[k] == 1){
if(distanceSP(L(s[j],t[j]),t[k]) <= r[j]+r[k]){
dame = true;
break;
}
}else{
if(distanceSP(L(s[j],t[j]),s[k]) <= r[j]+r[k]){
dame = true;
break;
}
}
}
if(!dame){
flag = true;
break;
}
if(flag){
break;
}
}
}
}
dp[i] = flag;
}
rep(i,(1<<n)){
if(dp[i]){
mx = max(mx, __builtin_popcount(i));
}
}
cout << mx << "\n";
}
| 0 |
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <numeric>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<vector<lli> > mat;
const lli MOD = 1000000007;
lli n,k;
vll d;
vll dp;
lli ans = 0;
lli pow(lli a,lli n,lli mod = MOD){
lli ret = 1;
for(;n!=0;n=n>>1){
if(n&1) ret = (ret * a) % mod;
a = (a * a) % mod;
}
return ret;
}
int main(){
cin >> n >> k;
lli i;
for(i = 1;i*i < n;i++) if(n%i == 0) d.push_back(i),d.push_back(n/i);
if(i*i == n) d.push_back(i);
sort(d.begin(),d.end());
dp = vector<lli> (d.size());
for(lli i = 0;i < d.size();i++){
dp[i] += pow(k,(d[i]+1)/2);//k*(d[i]+1)/2;
dp[i] %= MOD;
for(lli j = i+1;j < d.size();j++){
if(d[j]%d[i] == 0) dp[j] -= dp[i],dp[j] = (dp[j] + MOD) % MOD;
}
}
for(lli i = 0;i < d.size();i++){
ans += (dp[i]*(d[i]%2 ? d[i] : d[i]/2)) % MOD;
ans %= MOD;
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int a[n + 1];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long dp[n];
dp[0] = 1;
long long ans = 1;
for (int i = 1; i < n; i++) {
if (a[i] > a[i - 1]) {
dp[i] = dp[i - 1] + 1;
} else {
dp[i] = 1;
}
ans = max(ans, dp[i]);
}
cout << ans;
}
| 1 |
#include <iostream>
#include <fstream>
#include <algorithm>
//#define LOCAL
using namespace std;
typedef long long ll;
const int mx=1e3+10;
ll num[mx];
int main() {
#ifdef LOCAL
ifstream fin("1.txt");
streambuf*p;
p=cin.rdbuf(fin.rdbuf());
#endif // LOCAL
ll n;
cin>>n;
for(ll i=0;i<n;i++) cin>>num[i];
ll qn;
cin>>qn;
for(ll i=1;i<=qn;i++){
ll b, e, t;
cin>>b>>e>>t;
ll k=e-b;
for(ll d=0;d<k;d++){
swap(num[b+d], num[t+d]);
}
}
for(ll i=0;i<n;i++){
if(0==i) printf("%lld", num[i]);
else printf(" %lld", num[i]);
}
puts("");
#ifdef LOCAL
fin.close();
#endif // LOCAL
}
| 0 |
#include <bits/stdc++.h>
template <typename T>
inline void read(T &x) {
x = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
}
using namespace std;
inline void MIN(int &a, int b) {
if (b < a) a = b;
}
inline void MAX(int &a, int b) {
if (b > a) a = b;
}
int n, m;
struct Edge {
int u, v, d;
} E[157];
bool cmp(const Edge &a, const Edge &b) { return a.d < b.d; }
struct matrix {
int n;
bitset<157> b[157];
matrix(int x) {
n = x;
for (int i = 1; i <= n; ++i) b[i].reset();
}
matrix operator*(const matrix a) const {
matrix mt(n);
for (int i = 1; i <= n; ++i)
for (int k = 1; k <= a.n; ++k)
if (b[i][k]) mt.b[i] = mt.b[i] | a.b[k];
return mt;
}
};
struct edge {
int nxt, to;
} e[157];
int head[157], ecnt;
inline void addedge(int from, int to) {
e[++ecnt] = (edge){head[from], to};
head[from] = ecnt;
}
int dep[157], que[157], front, rear;
inline int bfs(matrix mt) {
memset(dep, 0x3f, sizeof(dep));
dep[n] = 0;
front = rear = 0;
que[++rear] = n;
while (front < rear) {
int cur = que[++front];
for (int i = head[cur]; i; i = e[i].nxt) {
int to = e[i].to;
if (dep[to] <= 1e9) continue;
dep[to] = dep[cur] + 1;
que[++rear] = to;
}
}
int res = 1e9 + 10000;
for (int i = 1; i <= n; ++i)
if (mt.b[1][i]) MIN(res, dep[i]);
return res;
}
inline void Quickpow(matrix &a, matrix b, int k) {
while (k) {
if (k & 1) a = a * b;
b = b * b;
k >>= 1;
}
}
inline void work() {
sort(E + 1, E + 1 + m, cmp);
matrix A(1), B(n);
A.b[1][1] = 1;
int nw = 0, ans = 2e9;
for (int i = 1; i <= m; ++i) {
int nwd = E[i].d;
Quickpow(A, B, nwd - nw);
B.b[E[i].u][E[i].v] = 1;
addedge(E[i].v, E[i].u);
nw = nwd;
MIN(ans, nw + bfs(A));
}
if (ans > 1e9 + 1000)
puts("Impossible");
else
printf("%d\n", ans);
}
int main() {
read(n), read(m);
for (int i = 1; i <= m; ++i) {
read(E[i].u), read(E[i].v), read(E[i].d);
}
work();
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main() {
int n, k, i, j;
int hols, arr[1000];
int temp, loop, flag = 0;
scanf("%d%d", &n, &k);
scanf("%d", &hols);
for (i = 0; i <= n; i++) {
arr[i] = 0;
}
for (i = 0; i < hols; i++) {
scanf("%d", &temp);
arr[temp] = 2;
}
for (i = 0; i <= n;) {
for (j = i; j < i + k; j++) {
if (arr[j] == 2) {
flag = 1;
loop = j;
}
}
if (flag == 1) {
flag = 0;
i = loop;
} else {
i = i + k;
}
arr[i] = 1;
}
int min = 0;
for (i = 1; i <= n; i++) {
if (arr[i] == 1 || arr[i] == 2) min++;
}
printf("%d", min);
return 0;
}
| 1 |
#include <bits/stdc++.h>
const long long int mod = 1000 * 1000 * 1000 + 7;
long long int clean(long long int x) {
x %= mod;
if (x < 0) x += mod;
return x;
}
long long int power(long long int x, long long int n) {
if (n == 0) return 1;
x = clean(x);
long long int half = power(x, n / 2);
if (n % 2 == 0)
return half * half % mod;
else
return half * half % mod * x % mod;
}
int main(int argc, char **argv) {
long long int n, k;
scanf("%lld %lld", &n, &k);
std::vector<std::vector<long long int>> f(
n + 1, std::vector<long long int>(n + 1, -1));
if (k == 1) {
printf("1\n");
return 0;
}
std::vector<std::vector<long long int>> nCr(
n + 1, std::vector<long long int>(n + 1, -1));
for (int front = 0; front <= n; front++) {
nCr[front][0] = 1, nCr[front][front] = 1;
for (int back = 1; back < front; back++)
nCr[front][back] =
(nCr[front - 1][back - 1] + nCr[front - 1][back]) % mod;
}
std::vector<long long int> kpower = {1}, k1power = {1};
for (int i = 1; i <= n; i++) {
kpower.push_back(kpower.back() * k % mod);
k1power.push_back(k1power.back() * (k - 1) % mod);
}
for (int r = 1; r <= n; r++)
f[r][0] = clean(power(power(k, n) - power(k - 1, n), r));
for (int c = 1; c <= n; c++) f[1][c] = power(k, n - c);
for (int r = 2; r <= n; r++)
for (int c = 1; c <= n; c++) {
f[r][c] = clean(kpower[n - c] - k1power[n - c]) * k1power[c] % mod *
f[r - 1][c] % mod;
for (int c0 = 1; c0 <= c; c0++) {
f[r][c] += k1power[c - c0] * kpower[n - c] % mod * nCr[c][c0] % mod *
f[r - 1][c - c0] % mod;
f[r][c] %= mod;
}
}
printf("%lld\n", f[n][n]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
const int mod = (1e9) + 7;
const double eps = 1e-10;
const int siz = 3e5 + 5;
int n, m, k, cur, ans[siz];
struct edge {
int v, w, i;
};
vector<edge> adj[siz];
bool intree[siz];
long long dis[siz];
struct msel {
long long d;
int e, u;
};
struct cmp {
bool operator()(const msel &a, const msel &b) { return a.d < b.d; }
};
multiset<msel, cmp> ms;
void dfs(int node, int p) {
for (edge &ed : adj[node]) {
if (intree[ed.i] && ed.v != p && cur < k) {
ans[cur++] = ed.i;
dfs(ed.v, node);
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
adj[u].push_back({v, w, i});
adj[v].push_back({u, w, i});
}
memset(dis, -1, sizeof dis);
ms.insert({0, 0, 1});
while (!ms.empty()) {
msel top = *ms.begin();
ms.erase(ms.begin());
if (dis[top.u] == -1) {
dis[top.u] = top.d;
intree[top.e] = true;
for (edge &ed : adj[top.u]) {
ms.insert({top.d + ed.w, ed.i, ed.v});
}
}
}
dfs(1, 0);
printf("%d\n", cur);
for (int i = 0; i < cur; i++) {
printf("%d%s", ans[i], (i < cur - 1 ? " " : "\n"));
}
return 0;
}
| 4 |
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int q,i,j,x,y;
cin>>q;
int a[100005],b[100005],c[100005];
for(i=2;i<=100000;i++)
a[i]=1;
for(i=2;i<=int(sqrt(100000));i++)
{
if(a[i]==1)
for(j=i+i;j<=100000;j+=i)
a[j]=0;
}
b[0]=0;
b[1]=0;
for(i=3;i<=100000;i++)
{
b[i]=0;
if(a[i]==1 && a[(i+1)/2]==1)
b[i]=1;
}
c[1]=0;
c[2]=0;
for(i=3;i<=100000;i++)
c[i]=c[i-1]+b[i];
for(i=1;i<=q;i++)
{
cin>>x>>y;
cout<<c[y]-c[x-1]<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename __T1, typename __T2>
ostream &operator<<(ostream &out, pair<__T1, __T2> &__VAR) {
cout << "[" << __VAR.first << ", " << __VAR.second << "]";
return out;
}
template <typename __T>
ostream &operator<<(ostream &out, vector<__T> &__VAR) {
cout << "[";
for (int i = 0; i <= (int)__VAR.size() - 2; i++) cout << __VAR[i] << ", ";
if (__VAR.size() > 0) cout << __VAR[__VAR.size() - 1];
cout << "]" << endl;
return out;
}
const int INF = 1e9;
int r, n, R;
vector<int> t;
vector<pair<int, int> > point;
vector<int> result;
void getInput() {
cin >> r >> n;
t.resize(n + 1, 0);
point.resize(n + 1);
result.resize(n + 1, 0);
R = 4 * r;
t[0] = 0;
point[0] = {1, 1};
for (int i = 1; i <= n; i++) cin >> t[i] >> point[i].first >> point[i].second;
}
int dist(int a, int b) {
return abs(point[a].first - point[b].first) +
abs(point[a].second - point[b].second);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
getInput();
vector<int> suff(n + 1, 0);
vector<int> ind(n + 1, 0);
for (int i = n; i >= 1; i--) {
int it = i + 1;
int res = 0;
while (it <= n && it < (i + R)) {
if (dist(i, it) <= (t[it] - t[i])) res = max(res, result[it]);
it++;
}
ind[i] = it;
result[i] = res + 1;
}
for (int i = n; i >= 1; i--) {
if (ind[i] <= n) result[i] = max(result[i], suff[ind[i]] + 1);
suff[i] = result[i];
if (i < n) suff[i] = max(result[i], suff[i + 1]);
}
int val = 0;
for (int i = 1; i <= n; i++)
if (dist(0, i) <= t[i]) val = max(val, result[i]);
cout << val << '\n';
return 0;
}
| 3 |
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
vector<int> point[100001];
int n,x,y,siz[100001],son[100001];
bool check[100001];
void dfs1(int u,int f){siz[u]=1;for(int v:point[u])if(v!=f)dfs1(v,u),siz[u]+=siz[v];}
void dfs2(int u,int f){
for(int v:point[u])if(v!=f){dfs2(v,u);if(siz[v]>0)++son[u];}
if(son[u]==1)siz[u]=0;if(son[u]>1)puts("First"),exit(0);
}
int main(){
scanf("%d",&n);for(int i=1;i<n;++i)scanf("%d%d",&x,&y),point[x].push_back(y),point[y].push_back(x);
dfs1(1,0);dfs2(1,0);
puts(siz[1]?"First":"Second");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long expo(long long base, long long exponent, long long mod) {
long long ans = 1;
while (exponent != 0) {
if ((exponent & 1) == 1) {
ans = ans * base;
ans = ans % mod;
}
base = base * base;
base %= mod;
exponent >>= 1;
}
return ans % mod;
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long modInv(long long a, long long m) {
long long gc = gcd(a, m);
a /= gc;
m /= gc;
long long m0 = m, y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long q = a / m, t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) {
unsigned long long int n, m, z = 0, num = 0, den, temp = 1;
cin >> n >> m;
vector<long long> p(n), q(n);
for (long long int i = 0; i < n; i++) {
cin >> p[i];
if (p[i] == 0) z++;
}
for (long long int i = 0; i < n; i++) {
cin >> q[i];
if (q[i] == 0) z++;
}
den = expo(m, z, 1000000007);
for (long long int i = 0; i < n; i++) {
if (!p[i] && q[i]) {
z--;
num += ((((m - q[i]) * expo(m, z, 1000000007)) % 1000000007) * temp) %
1000000007;
num = num % 1000000007;
} else if (p[i] && !q[i]) {
z--;
num += ((((p[i] - 1) * expo(m, z, 1000000007)) % 1000000007) * temp) %
1000000007;
num = num % 1000000007;
} else if (!p[i] && !q[i]) {
z -= 2;
unsigned long long int s = m * (m - 1);
s /= 2;
num +=
(((s * expo(m, z, 1000000007)) % 1000000007) * temp) % 1000000007;
num = num % 1000000007;
temp *= m;
temp = temp % 1000000007;
} else {
if (p[i] < q[i])
break;
else if (p[i] > q[i]) {
num += expo(m, z, 1000000007) * temp;
num = num % 1000000007;
break;
}
}
}
unsigned long long int ans = (num * modInv(den, 1000000007)) % 1000000007;
cout << ans;
}
return 0;
}
| 4 |
#include<cstdio>
int main()
{
int N,x;
scanf("%d",&N);
x = N * (N + 1) * 0.5;
printf("%d",x);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int N, A, B;
int P[100000];
set<int> all;
map<int, int> rev;
map<int, vector<int>> adj;
vector<int> one;
char ans[100000];
int main() {
scanf("%d%d%d", &N, &A, &B);
for (int i = 0; i < N; i++)
scanf("%d", P + i), all.insert(P[i]), rev[P[i]] = i;
if (A == B) {
for (int i = 0; i < N; i++)
if (!all.count(A - P[i])) {
printf("NO\n");
return 0;
}
printf("YES\n");
for (int i = 0; i < N; i++) printf("0%c", " \n"[i == N - 1]);
return 0;
}
for (int i = 0; i < N; i++) {
if (all.count(A - P[i])) adj[P[i]].push_back(A - P[i]);
if (all.count(B - P[i])) adj[P[i]].push_back(B - P[i]);
if (adj[P[i]].empty()) {
printf("NO\n");
return 0;
}
if (adj[P[i]].size() == 1) one.push_back(P[i]);
}
for (size_t i = 0; i < one.size(); i++) {
if (!all.count(one[i])) continue;
if (adj[one[i]].empty()) {
printf("NO\n");
return 0;
}
if (one[i] == adj[one[i]][0]) {
all.erase(one[i]);
ans[rev[one[i]]] = one[i] * 2 == A ? '0' : '1';
continue;
}
all.erase(one[i]);
all.erase(adj[one[i]][0]);
ans[rev[one[i]]] = (A - one[i] == adj[one[i]][0]) ? '0' : '1';
ans[rev[adj[one[i]][0]]] = ans[rev[one[i]]];
for (auto it : adj[adj[one[i]][0]])
if (it != one[i]) {
adj[it].erase(find(adj[it].begin(), adj[it].end(), adj[one[i]][0]));
if (adj[it].size() <= 1) one.push_back(it);
}
}
printf("YES\n");
for (int i = 0; i < N; i++) printf("%c%c", ans[i], " \n"[i == N - 1]);
return 0;
}
| 2 |
#include<iostream>
#include<cstdio>
#include<string>
#include<ctype.h>
using namespace std;
int main(){
string a;
int b=0,c=0,d=0;
cin>>a;
for(int i=0;i<a.size();i++){
if(a[i]-'0'>=0&&a[i]-'0'<=9)b++;
if(isupper(a[i]))c++;
if(islower(a[i]))d++;
}
if(b==0||c==0||d==0||a.size()<6)printf("IN");
printf("VALID\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5 * 1e5 + 10, MaxH = 60;
inline int read() {
char c = getchar();
int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int T, Q, fa[MAXN], N;
vector<int> v[MAXN];
double f[MAXN][61];
void mem(double *f) {
for (int i = 0; i <= MaxH; i++) f[i] = 1;
}
int main() {
Q = read();
N = 1;
mem(f[1]);
while (Q--) {
int opt = read(), x = read();
if (opt == 1) {
fa[++N] = x;
mem(f[N]);
double pre = f[x][0], now;
f[x][0] *= 0.5;
for (int h = 1; h <= MaxH; h++, x = fa[x]) {
now = f[fa[x]][h];
f[fa[x]][h] /= 0.5 + 0.5 * pre;
f[fa[x]][h] *= 0.5 + 0.5 * f[x][h - 1];
pre = now;
}
} else if (opt == 2) {
double ans = 0;
for (int i = 0; i <= MaxH; i++) ans += i * (f[x][i] - f[x][i - 1]);
printf("%.10lf\n", ans);
}
}
return 0;
}
| 5 |
Subsets and Splits