solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
long long int power(long long int x, long long int b, long long int mod1) {
long long int p = 1;
while (b > 0) {
if (b & 1) {
p = p * x;
p %= mod1;
}
b >>= 1;
x *= x;
x %= mod1;
}
return p % mod1;
}
using namespace std;
struct lex_compare {
bool operator()(const pair<long long int, long long int> p1,
const pair<long long int, long long int> p2) {
return p1.first > p2.first;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long int n, x, y;
cin >> n >> x >> y;
long long int arr[n];
double left = n;
long long int sum = 0;
if (n > y) {
cout << -1 << "\n";
return 0;
}
arr[0] = ceil(sqrt(max(1.00, double(x - n + 1))));
for (long long int i = 1; i < n; i++) arr[i] = 1;
if (n - 1 + arr[0] > y || (arr[0] * arr[0] + n - 1) < x) {
cout << -1 << "\n";
return 0;
}
for (long long int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int N = 41;
typedef long long ll;
ll dp[N][N][N][N], dp1[N][N][N][N];
bool vis[N][N][N][N];
char s[N][N];
void dfs(int i, int j, int u, int v){
if (i > j || u > v || vis[i][j][u][v]){
return;
}
vis[i][j][u][v] = true;
if (i == j && u == v){
if (s[i][u] == '1'){
dp[i][j][u][v] = dp1[i][j][u][v] = 1;
}
return;
}
for (int p = i; p <= j; ++ p){
for (int q = u; q <= v; ++ q){
dfs(i, p - 1, p + 1, j);
dfs(u, q - 1, q + 1, v);
if (s[p][q] == '1'){
dp[i][j][u][v] += (i == j ? 1 : dp1[i][p - 1][p + 1][j]) * (u == v ? 1 : dp1[u][q - 1][q + 1][v]);
}
}
}
dp1[i][j][u][v] = dp[i][j][u][v];
for (int p = i; p <= j; ++ p){
for (int q = u; q <= v; ++ q){
dfs(i, p, q, v);
dfs(p + 1, j, u, q - 1);
dp1[i][j][u][v] += dp[i][p][q][v] * dp1[p + 1][j][u][q - 1];
}
}
}
int main(){
int n;
scanf("%d", &n);
for (int i = 0; i < 2 * n; ++ i){
scanf("%s", s[i]);
}
if (n == 1){
putchar(s[0][1]);
puts("");
return 0;
}
dfs(1, 2, 4, 5);
for (int i = 0; i < 2 * n; ++ i){
for (int j = i; j < 2 * n; ++ j){
for (int u = j + 1; u < 2 * n; ++ u){
for (int v = u; v < 2 * n; ++ v){
dfs(i, j, u, v);
}
}
}
}
ll ans = 0;
for (int i = 1; i < 2 * n; ++ i){
if (s[0][i] == '1'){
ans += dp1[1][i - 1][i + 1][2 * n - 1];
}
}
printf("%lld\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5, LOG = 22;
vector<long long> g[N];
long long child[N], p[N][LOG], d[N];
void dfs(long long node, long long par) {
child[node] = 1;
for (long long v : g[node])
if (v != par) {
d[v] = d[node] + 1;
p[v][0] = node;
dfs(v, node);
child[node] += child[v];
}
}
void pre() {
for (long long k = 1; k < LOG; k++) {
for (long long i = 1; i < N; i++) p[i][k] = p[p[i][k - 1]][k - 1];
}
}
long long walk(long long node, long long k) {
for (long long i = 0; i < LOG; i++)
if (k & (1LL << i)) {
node = p[node][i];
}
return node;
}
long long lca(long long u, long long v) {
if (d[u] > d[v]) {
u = walk(u, d[u] - d[v]);
}
if (d[u] < d[v]) {
v = walk(v, d[v] - d[u]);
}
if (u == v) return u;
for (long long i = LOG - 1; i >= 0; i--)
if (p[u][i] != p[v][i]) {
u = p[u][i];
v = p[v][i];
}
return p[u][0];
}
long long distance(long long u, long long v) {
return d[u] + d[v] - 2 * d[lca(u, v)];
}
int32_t main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
;
long long n;
cin >> n;
for (long long i = 1; i <= n - 1; i++) {
long long u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
pre();
long long q, u, v;
cin >> q;
while (q--) {
cin >> u >> v;
if (d[u] < d[v]) swap(u, v);
long long dis = distance(u, v);
if (dis % 2 == 1) {
cout << 0 << "\n";
continue;
}
long long mid_node = walk(u, dis / 2);
long long lc = lca(u, v);
long long dis1 = d[u] - d[mid_node];
long long dis2 = d[v] - d[mid_node];
long long ans = 0;
if (dis1 == dis2) {
ans = n - child[walk(u, dis / 2 - 1)] - child[walk(v, dis / 2 - 1)];
} else {
ans = child[mid_node] - child[walk(u, dis / 2 - 1)];
}
cout << ans << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e2 + 5;
const int mod = 1e9 + 7;
const int INF = 1e8 + 5;
const long long inf = 1e15 + 5;
const double eps = 1e-8;
const long long hp = 2333;
const long long base = 1e9 + 13;
long long n, l, r;
long long cal(long long x) {
if (x <= 1) return 1;
return cal(x >> 1) * 2ll + 1;
}
long long dfs(long long x, long long cl, long long cr) {
if (x == 0 || cr < l || cl > r) return 0;
if (x == 1) return 1;
long long mid = cl + (cr - cl) / 2 - 1;
return dfs(x >> 1, cl, mid) + dfs(x & 1, mid + 1, mid + 1) +
dfs(x >> 1, mid + 2, cr);
}
void solve() {
while (cin >> n >> l >> r) {
long long tot = cal(n);
long long ans = dfs(n, 1, tot);
cout << ans << endl;
}
}
int main() {
int t = 1, cas = 1;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
const long long inf = 0x3f3f3f3f3f3f3f3f;
using namespace std;
const long long maxn = 5009;
long long read() {
long long qqq;
scanf("%lld", &qqq);
return qqq;
}
long long n, m;
long long pos[maxn];
long long dp[maxn][maxn], sum[maxn], S;
long long q[maxn];
struct node {
long long pos, num;
bool operator<(const node& b) const { return pos < b.pos; }
} a[maxn];
signed main() {
cin >> n >> m;
for (long long i = 1; i <= n; i++) pos[i] = read();
sort(pos + 1, pos + 1 + n);
for (long long i = 1; i <= m; i++) {
a[i].pos = read(), a[i].num = read(), S += a[i].num;
}
sort(a + 1, a + 1 + m);
if (S < n) {
puts("-1");
return 0;
}
for (long long i = 1; i <= n; i++) dp[0][i] = inf;
for (long long i = 1; i <= m; i++) {
for (long long j = 1; j <= n; j++) {
sum[j] = sum[j - 1] + abs(pos[j] - a[i].pos);
}
long long head = 1, tail = 0;
for (long long j = 0; j <= n; j++) {
while (head <= tail and q[head] < j - a[i].num) head += 1;
while (head <= tail and
dp[i - 1][j] - sum[j] <= dp[i - 1][q[tail]] - sum[q[tail]]) {
tail -= 1;
}
q[++tail] = j;
dp[i][j] = dp[i - 1][q[head]] - sum[q[head]] + sum[j];
}
}
printf("%lld\n", dp[m][n]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int M = 100000;
const int S = 17;
int n, K, len;
char buf[M + 5];
int str[M * 2 + 5], bel[M * 2 + 5], rt[M * 2 + 5], st[M + 5], ed[M + 5];
struct suffix_array {
int sa[M * 2 + 5], h[M * 2 + 5], v[M * 2 + 5], rk[M * 2 + 5],
ST[S][M * 2 + 5], Log2[M * 2 + 5];
void build(int *s, int n, int m) {
int *x = rk, *y = h;
for (int i = 0, i_END_ = m + 1; i < i_END_; ++i) v[i] = 0;
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i) v[x[i] = s[i]]++;
for (int i = 1, i_END_ = m + 1; i < i_END_; ++i) v[i] += v[i - 1];
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i) sa[v[x[i]]--] = i;
for (int k = 1; k <= n; k <<= 1) {
int p = 0;
for (int i = n - k + 1, i_END_ = n + 1; i < i_END_; ++i) y[++p] = i;
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i)
if (sa[i] > k) y[++p] = sa[i] - k;
assert(p == n);
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i) v[x[sa[i]]] = i;
for (int i = (n + 1) - 1, i_BEGIN_ = 1; i >= i_BEGIN_; --i)
sa[v[x[y[i]]]--] = y[i];
p = 0;
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i) {
if (x[sa[i]] != x[sa[i - 1]] || x[sa[i] + k] != x[sa[i - 1] + k]) ++p;
y[sa[i]] = p;
}
swap(x, y);
if (p == n) break;
}
if (x != rk) memcpy(rk, x, n + 1 << 2);
int k = 0;
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i) {
int j = sa[rk[i] - 1];
for (; s[i + k] == s[j + k]; ++k)
;
h[rk[i]] = k;
if (k) --k;
}
pret(n);
}
void pret(int n) {
Log2[1] = 0;
for (int i = 2, i_END_ = n + 1; i < i_END_; ++i) Log2[i] = Log2[i >> 1] + 1;
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i) ST[0][i] = h[i];
for (int i = 1, i_END_ = S; i < i_END_; ++i)
for (int j = 1, j_END_ = n + 1; j < j_END_; ++j) {
if (j + (1 << i - 1) > n) break;
ST[i][j] = min(ST[i - 1][j], ST[i - 1][j + (1 << i - 1)]);
}
}
int query_mi(int a, int b) {
int k = Log2[b - a + 1];
return min(ST[k][a], ST[k][b - (1 << k) + 1]);
}
int query(int a, int b) {
if (a > b) swap(a, b);
a++;
return query_mi(a, b);
}
} sa;
bool check(int x, int tl) {
int L = 1, R = sa.rk[x] - 1, l = sa.rk[x], r = sa.rk[x];
while (L <= R) {
int mid = (L + R) >> 1;
if (sa.query(mid, sa.rk[x]) >= tl)
l = mid, R = mid - 1;
else
L = mid + 1;
}
L = sa.rk[x] + 1, R = len;
while (L <= R) {
int mid = (L + R) >> 1;
if (sa.query(sa.rk[x], mid) >= tl)
r = mid, L = mid + 1;
else
R = mid - 1;
}
return rt[l] <= r;
}
int cnt[M + 5], num = 0;
void add(int x) {
int b = bel[sa.sa[x]];
if (!b) return;
if (cnt[b] == 0) num++;
cnt[b]++;
}
void del(int x) {
int b = bel[sa.sa[x]];
if (!b) return;
cnt[b]--;
if (cnt[b] == 0) num--;
}
void pret() {
int R = 0;
memset(cnt, 0, sizeof cnt);
for (int i = 1, i_END_ = len + 1; i < i_END_; ++i) {
while (num < K && R < len) {
++R;
add(R);
}
assert(i <= R);
assert(num <= K);
if (num == K)
rt[i] = R;
else
rt[i] = len + 1;
del(i);
}
}
long long ans[M + 5];
int main() {
scanf("%d%d", &n, &K);
int m = 26;
len = 1;
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i) {
scanf("%s", buf);
st[i] = len;
for (int j = 0, j_END_ = strlen(buf); j < j_END_; ++j) {
bel[len] = i;
str[len++] = buf[j] - 'a' + 1;
}
ed[i] = len;
str[len++] = ++m;
}
len -= 2;
sa.build(str, len, m);
pret();
for (int i = 1, i_END_ = len + 1; i < i_END_; ++i) {
if (str[i] > 26) continue;
int L = 0, R = ed[bel[i]] - i, res = 0;
while (L <= R) {
int mid = (L + R) >> 1;
if (check(i, mid))
res = mid, L = mid + 1;
else
R = mid - 1;
}
ans[bel[i]] += res;
}
for (int i = 1, i_END_ = n + 1; i < i_END_; ++i) cout << ans[i] << ' ';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int n, m, k;
int a[maxn], S[maxn], cnt[maxn], top;
int main() {
bool single_color = true;
scanf("%d%d%d", &n, &k, &m);
for (int i = 0; i < n; i++) scanf("%d", a + i);
for (int i = 1; i < n; i++)
if (a[i] != a[i - 1]) {
single_color = false;
break;
}
if (single_color) {
printf("%I64d\n", (long long)n * m % k);
return 0;
}
for (int i = 0; i < n; i++) {
S[++top] = a[i];
if (top > 1 && S[top] == S[top - 1])
cnt[top] = cnt[top - 1] + 1;
else
cnt[top] = 1;
if (cnt[top] >= k) top -= k;
}
int L = 1, R = top;
long long t = 0;
while (S[L] == S[R] && L < R) {
int l = L, r = R, cnt = 0;
while (S[l] == S[L] && l < r && cnt < k) {
cnt++;
l++;
}
while (S[r] == S[L] && l < r && cnt < k) {
cnt++;
r--;
}
if (cnt == k) {
L = l;
R = r;
t += k;
} else
break;
}
single_color = true;
for (int i = L; i < R; i++)
if (S[i] != S[i + 1]) {
single_color = false;
break;
}
if (single_color) {
long long mid = (long long)(R - L + 1) * m % k;
if (mid)
printf("%lld\n", mid + t);
else
printf("0\n");
} else {
printf("%lld\n", (long long)(R - L + 1) * m + t);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
int k, n = 0, m = 0;
char a[N][N];
int main() {
cin >> k;
for (int i = 5; i < k; i++) {
if ((k % i == 0 && i > 4) && (k % (k / i) == 0 && k / i > 4)) {
n = i;
m = k / i;
}
}
if (n == 0) {
cout << "-1" << endl;
return 0;
}
a[0][0] = 'a';
a[0][1] = 'e';
a[0][2] = 'i';
a[0][3] = 'o';
a[0][4] = 'u';
a[1][0] = 'e';
a[1][1] = 'i';
a[1][2] = 'o';
a[1][3] = 'u';
a[1][4] = 'a';
a[2][0] = 'i';
a[2][1] = 'o';
a[2][2] = 'u';
a[2][3] = 'a';
a[2][4] = 'e';
a[3][0] = 'o';
a[3][1] = 'u';
a[3][2] = 'a';
a[3][3] = 'e';
a[3][4] = 'i';
a[4][0] = 'u';
a[4][1] = 'a';
a[4][2] = 'e';
a[4][3] = 'i';
a[4][4] = 'o';
for (int i = 5; i < n; i++) {
a[i][0] = 'a';
a[i][1] = 'e';
a[i][2] = 'i';
a[i][3] = 'o';
a[i][4] = 'u';
}
for (int i = 5; i < m; i++) {
a[0][i] = 'a';
a[1][i] = 'e';
a[2][i] = 'i';
a[3][i] = 'o';
a[4][i] = 'u';
}
for (int i = 5; i < n; i++)
for (int j = 5; j < m; j++) a[i][j] = 'g';
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) cout << a[i][j];
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN = 5040, MOD = 1e9 + 7;
int dp[MAXN][MAXN], DP[MAXN], a[MAXN], b[MAXN];
int mul(int a, int b) {return (a * b) % MOD;}
int sum(int a, int b) {return (a + b) % MOD;}
int32_t main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int N, S; string s; cin >> N >> s;
S = s.size();
dp[0][0] = 1;
for (int i = 1; i <= N; i ++) {
dp[i][0] = dp[i - 1][1] * 2 + dp[i - 1][0];
for (int j = 1; j <= N; j++) dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j + 1] * 2) % MOD;
}
cout << dp[N][S] << "\n";
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
if (n == 1) {
cout<<1<<endl;
return 0;
}
if (n == 0) {
cout<<2<<endl;
return 0;
}
cout<<2-n*n<<endl;
return 0;
int SZ = (n+100)*1024*1000/4;
vector<int> v(SZ);
for (int i=0; i<SZ; i++) v[i]= rand();
if (v[rand()%SZ] == 0) cout<<2<<endl;
else cout<<1<<endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 51;
int p[N], s[N];
void init() {
for (int i = 0; i < N; i++) p[i] = i, s[i] = 1;
}
int find(int x) { return p[x] == x ? x : p[x] = find(p[x]); }
void dsu(int x, int y) {
x = find(x), y = find(y);
if (s[x] < s[y])
p[x] = y, s[y] += s[x];
else
p[y] = x, s[x] += s[y];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, x, y;
set<int> ch;
init();
cin >> n >> m;
while (m--) cin >> x >> y, dsu(x, y);
for (int i = 0; i < n; i++) ch.insert(find(i + 1));
n -= ch.size();
cout << (1LL << n);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long l, r;
long long func(long long r) {
long long sol = 2;
while (sol <= r) sol *= 2;
if (sol - 1 == r) return sol;
return sol / 2;
}
int main() {
scanf("%d", &n);
while (n--) {
scanf("%lld%lld", &l, &r);
if (l == r) {
printf("%lld\n", r);
continue;
}
long long so = 0;
while (1) {
long long temp = func(r);
so += temp;
if (l <= so - 1) {
printf("%lld\n", so - 1);
break;
}
r -= temp;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void out(T x) {
cout << x << endl;
exit(0);
}
using ll = long long;
const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;
const int inf = 1e9 + 10;
struct dsu0 {
vector<int> par, siz;
int n;
int cc;
int largest;
void init(int n) {
assert(n > 0);
this->n = n;
cc = n;
par.resize(n + 10);
siz.resize(n + 10);
for (int i = 0; i < n; i++) par[i] = i, siz[i] = 1;
largest = 1;
}
int parent(int x) {
assert(x >= 0 && x < n);
return par[x] == x ? x : par[x] = parent(par[x]);
}
void join(int x, int y) {
x = parent(x);
y = parent(y);
if (x == y) return;
cc--;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
largest = max(largest, siz[x]);
}
};
int n, m;
bool mst[maxn];
int ans[maxn];
int w[maxn], u[maxn], v[maxn];
int p[maxn];
const int LOG = 17;
vector<pair<int, int>> g[maxn];
int par[LOG + 1][maxn];
int dep[maxn];
int upEd[LOG + 1][maxn];
void dfs(int at, int p = -1) {
if (p != -1) {
par[0][at] = p;
for (int j = 1; j < LOG; j++) {
par[j][at] = par[j - 1][par[j - 1][at]];
}
for (int j = 1; j < LOG; j++) {
upEd[j][at] = max(upEd[j - 1][at], upEd[j - 1][par[j - 1][at]]);
}
}
for (auto ed : g[at]) {
int to = ed.second;
if (to != p) {
int wei = ed.first;
upEd[0][to] = wei;
dep[to] = 1 + dep[at];
dfs(to, at);
}
}
}
int lca(int u, int v) {
if (u == v) return v;
if (dep[u] < dep[v]) swap(u, v);
int dx = dep[u] - dep[v];
for (int j = LOG - 1; j >= 0; j--) {
if (dx >> j & 1) {
u = par[j][u];
}
}
assert(dep[u] == dep[v]);
if (u == v) return v;
for (int j = LOG - 1; j >= 0; j--) {
if (par[j][u] != par[j][v]) {
u = par[j][u];
v = par[j][v];
}
}
return par[0][u];
}
int walk(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int res = 0;
for (int j = LOG - 1; j >= 0; j--) {
int to = par[j][u];
if (dep[to] > dep[v]) {
res = max(res, upEd[j][u]);
u = to;
}
}
res = max(res, upEd[0][u]);
return res;
}
int get(int u, int v) {
assert(u != v);
int mid = lca(u, v);
int res = 0;
if (mid != u) res = max(res, walk(u, mid));
if (mid != v) res = max(res, walk(v, mid));
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
memset(ans, -1, sizeof(ans));
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> w[i];
--u[i];
--v[i];
}
iota(p, p + m, 0);
sort(p, p + m, [&](int i, int j) { return w[i] < w[j]; });
dsu0 dsu;
dsu.init(n);
for (int i = 0; i < m; i++) {
int a = u[p[i]];
int b = v[p[i]];
int wei = w[p[i]];
if (dsu.parent(a) != dsu.parent(b)) {
mst[p[i]] = true;
dsu.join(a, b);
g[a].push_back({wei, b});
g[b].push_back({wei, a});
}
}
dfs(0);
for (int i = 0; i < m; i++) {
if (mst[p[i]]) continue;
ans[p[i]] = get(u[p[i]], v[p[i]]);
}
for (int i = 0; i < m; i++) {
if (~ans[i]) {
cout << ans[i] << "\n";
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 4e18;
const long long mod = 1e9 + 7;
const long long maxn = 1e5 + 10;
int mark[maxn];
long long lo[maxn], wi[maxn];
vector<long long> rev[maxn], v[maxn];
stack<long long> st;
void dfs(long long u, bool state) {
for (long long i = 0; i < (rev[u]).size(); i++) {
long long y = rev[u][i];
if (state) {
if (lo[y] == 0) {
lo[y] = u;
dfs(y, 0);
}
} else {
if (wi[y] == 0) {
wi[y] = u;
dfs(y, 1);
}
}
}
}
bool cycle(long long u) {
if (mark[u] == 1) return 1;
if (mark[u] == 2) return 0;
mark[u] = 1;
for (long long i = 0; i < (v[u]).size(); i++) {
long long y = v[u][i];
if (cycle(y)) return 1;
}
mark[u] = 2;
return 0;
}
int main() {
long long n, m;
cin >> n >> m;
for (long long i = 1; i <= n; i++) {
long long c;
cin >> c;
if (c == 0) {
st.push(i);
lo[i] = -1;
}
for (long long j = 0; j < c; j++) {
long long x;
cin >> x;
rev[x].push_back(i);
v[i].push_back(x);
}
}
while ((st).size()) {
dfs(st.top(), 0);
st.pop();
}
long long s;
cin >> s;
if (wi[s] != 0) {
cout << "Win\n";
long long tmp = s, state = 1;
while (tmp != -1) {
cout << tmp << " ";
tmp = (state ? wi[tmp] : lo[tmp]);
state = 1 - state;
}
} else if (cycle(s)) {
cout << "Draw";
} else {
cout << "Lose";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, s, e;
long long dp[5005][5005];
vector<int> x, a, b, c, d;
int main() {
cin >> n >> s >> e;
x.resize(n);
a.resize(n);
b.resize(n);
c.resize(n);
d.resize(n);
for (int i = 0; i < n; i++) {
cin >> x[i];
}
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
for (int i = 0; i < n; i++) {
cin >> c[i];
}
for (int i = 0; i < n; i++) {
cin >> d[i];
}
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
bool iss = 0, ise = 0;
for (int i = 0; i < n; i++) {
if (i == s - 1) {
iss = 1;
}
if (i == e - 1) {
ise = 1;
}
if (i == n - 1) {
long long p = dp[i][0];
if (i == s - 1) {
dp[i + 1][0] = min(dp[i + 1][0], p + x[i] + c[i]);
} else if (i == e - 1) {
dp[i + 1][0] = min(dp[i + 1][0], p + x[i] + a[i]);
} else {
dp[i + 1][0] = min(dp[i + 1][0], p + 2 * x[i] + a[i] + c[i]);
}
continue;
}
for (int j = 0; j <= n - 2; j++) {
long long p = dp[i][j];
if (i == s - 1) {
dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + d[i]);
if (j) dp[i + 1][j - 1] = min(dp[i + 1][j - 1], p + x[i] + c[i]);
} else if (i == e - 1) {
dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i]);
if (j) dp[i + 1][j - 1] = min(dp[i + 1][j - 1], p + x[i] + a[i]);
} else {
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], p - 2 * x[i] + b[i] + d[i]);
if (j) {
dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i] + x[i] + c[i]);
dp[i + 1][j] = min(dp[i + 1][j], p + x[i] + a[i] - x[i] + d[i]);
}
if (iss) {
dp[i + 1][j] = min(dp[i + 1][j], p + x[i] + a[i] - x[i] + d[i]);
}
if (ise) {
dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i] + x[i] + c[i]);
}
if (j + iss + ise > 1) {
dp[i + 1][j - 1] =
min(dp[i + 1][j - 1], p + x[i] + a[i] + x[i] + c[i]);
}
}
}
}
cout << dp[n][0] << endl;
return 0;
}
| 2 |
#include<iostream>
#include<string>
using namespace std;
#define MAX 50
int main(){
int n;
int H,W;
string pict[MAX];
int zai['Z'-'A'+1][MAX][MAX];
/*
0..??????
1..?????????
2..??°
3..??????
*/
bool zok['Z'-'A'+1];
cin>>n;
while(n--){
cin>>H>>W;
for(int i=0;i<H;i++){
cin>>pict[i];
}
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
for(int k=0;k<='Z'-'A';k++){
zai[k][i][j]=0;
}
}
}
for(int i=0;i<='Z'-'A';i++){
zok[i]=false;
}
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
if(pict[i][j]!='.'){
zai[pict[i][j]-'A'][i][j]=1;
}
}
}
while(1){
bool ex=true;
for(int i=0;i<='Z'-'A';i++){
if(zok[i]) continue;
bool fok=true;
for(int j=0;j<H;j++){
for(int k=0;k<W;k++){
if(zai[i][j][k]==1) fok=false;
}
}
if(fok) zok[i]=true;
}
for(int i=0;i<='Z'-'A';i++){
if(zok[i]) continue;
int mu=-1,mb=-1,mr=-1,ml=-1;
for(int j=0;j<H&&mu==-1;j++){
for(int k=0;k<W&&mu==-1;k++){
if(zai[i][j][k]==1) mu=j;
}
}
for(int j=H-1;j>=0&&mb==-1;j--){
for(int k=0;k<W&&mb==-1;k++){
if(zai[i][j][k]==1) mb=j;
}
}
for(int j=0;j<W&&ml==-1;j++){
for(int k=0;k<H&&ml==-1;k++){
if(zai[i][k][j]==1) ml=j;
}
}
for(int j=W-1;j>=0&&mr==-1;j--){
for(int k=0;k<H&&mr==-1;k++){
if(zai[i][k][j]==1) mr=j;
}
}
bool sqf=true;
for(int j=mu;j<=mb;j++){
for(int k=ml;k<=mr;k++){
if(zai[i][j][k]==0) sqf=false;
}
}
if(sqf){
ex=false;
for(int j=mu;j<=mb;j++){
for(int k=ml;k<=mr;k++){
for(int l=0;l<='Z'-'A';l++){
if(l!=i) zai[l][j][k]=2;
}
zai[i][j][k]=0;
}
}
}
}
if(ex) break;
}
bool ansf=true;
for(int i=0;i<='Z'-'A';i++){
for(int j=0;j<H;j++){
for(int k=0;k<W;k++){
}
}
if(!zok[i]) ansf=false;
}
if(ansf) cout<<"SAFE"<<endl;
else cout<<"SUSPICIOUS"<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
constexpr int MAX_N = 100100;
constexpr int MAX_V = 7005;
int n, q;
bitset<MAX_V> base[MAX_V];
bitset<MAX_V> mask[MAX_V];
bitset<MAX_V> val[MAX_N];
void prt(int x) {}
int main() {
scanf(" %d %d", &n, &q);
for (int i = 1; i < MAX_V; ++i) {
for (int j = i; j < MAX_V; j += i) {
base[j][i] = 1;
}
}
for (int i = MAX_V - 1; i >= 1; --i) {
mask[i][i] = 1;
for (int j = 2 * i; j < MAX_V; j += i) {
mask[i] = mask[i] ^ mask[j];
}
}
for (int it = 0; it < q; ++it) {
int typ;
scanf(" %d", &typ);
if (typ == 1) {
int x, v;
scanf(" %d %d", &x, &v);
val[x] = base[v];
prt(x);
} else if (typ == 2) {
int x, y, z;
scanf(" %d %d %d", &x, &y, &z);
val[x] = val[y] ^ val[z];
prt(x);
} else if (typ == 3) {
int x, y, z;
scanf(" %d %d %d", &x, &y, &z);
val[x] = val[y] & val[z];
prt(x);
} else if (typ == 4) {
int x, v;
scanf(" %d %d", &x, &v);
bitset<MAX_V> ans = val[x] & mask[v];
printf("%d", (int)(ans.count() & 1));
prt(x);
}
}
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long ans=0,pw[20],d,cur;
long long bt(long long now,int rem,int dg){
if(rem==0) return (now==0);
if(rem==1) return (now==0)*10LL;
long long prd=pw[dg-1]-pw[cur-dg];
long long mul=now/prd,ret=0;
if(rem==dg){
ret+=1LL*max(0LL,9-abs(mul))*bt(now-prd*mul,rem-2,dg-1);
if(mul<0) mul--;
else if(mul==0) mul++, ret+=1LL*max(0LL,9-abs(mul))*bt(now-prd*mul,rem-2,dg-1), mul-=2;
else mul++;
ret+=1LL*max(0LL,9-abs(mul))*bt(now-prd*mul,rem-2,dg-1);
}
else{
ret+=1LL*max(0LL,10-abs(mul))*bt(now-prd*mul,rem-2,dg-1);
if(mul<0) mul--;
else if(mul==0) mul++, ret+=1LL*max(0LL,10-abs(mul))*bt(now-prd*mul,rem-2,dg-1), mul-=2;
else mul++;
ret+=1LL*max(0LL,10-abs(mul))*bt(now-prd*mul,rem-2,dg-1);
}
return ret;
}
int main(){
ios_base::sync_with_stdio(0);
cin>>d;
pw[0]=1;
for(int i=1;i<=18;i++) pw[i]=pw[i-1]*10LL;
for(int i=1;i<=19;i++) cur=i, ans+=bt(d,i,i);
cout<<ans;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
long n,dp[100001],a[100001];
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
dp[2]=abs(a[2]-a[1]);
for(int i=3;i<=n;i++)
{
dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
}
cout<<dp[n];
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int h1, m1, h2, m2;
char ch;
cin >> h1 >> ch >> m1 >> h2 >> ch >> m2;
if (m2 > m1) {
h1--;
m1 = 60 - (m2 - m1);
} else
m1 -= m2;
h1 -= h2;
while (h1 < 0) {
h1 = 24 + h1;
}
string h = (h1 < 10) ? "0" : "", m = (m1 < 10) ? "0" : "";
cout << h << h1 << ":" << m << m1;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1, ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(int x) {
write(x);
puts("");
}
const int N = 1e5 + 5;
struct edge {
int u, v, w;
} e[N << 1];
int n, m, c[N], Sum;
inline void init() {
n = read();
for (int i = 1; i < n; i++) {
e[i] = (edge){read(), read(), read()};
}
for (int i = 1; i <= n; i++) {
c[i] = read();
Sum += c[i];
}
}
inline bool cmp(edge A, edge B) { return A.w < B.w; }
int tmp, fa[N], sz[N], sum[N];
int getfa(int x) { return (fa[x] == x) ? x : fa[x] = getfa(fa[x]); }
inline void Union(int u, int v) {
int p = getfa(u), q = getfa(v);
if (p != q) {
fa[p] = q;
sz[q] += sz[p];
}
}
inline bool judge(int mid) {
for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = c[i];
memset(sum, 0, sizeof sum);
tmp = 0;
for (int i = 1; i <= m; i++) {
if (e[i].w < mid) {
tmp = i;
Union(e[i].u, e[i].v);
} else {
break;
}
}
for (int i = 1; i <= n; i++) {
fa[i] = getfa(i);
sum[fa[i]]++;
}
for (int i = 1; i <= n; i++) {
if (sum[i]) {
int other = Sum - sz[i];
if (other < sum[i]) {
return 0;
}
}
}
return 1;
}
inline void solve() {
if (n == 1) {
puts("0");
return;
}
m = n - 1;
sort(e + 1, e + m + 1, cmp);
int l = 1, r = 1e9;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (judge(mid))
l = mid;
else
r = mid - 1;
}
writeln(l);
}
int main() {
init();
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N,A,B;
ll v[55];
long double S,D;
ll comb(int n,int r){
if(r==0) return 1;
return (n-r+1)*comb(n,r-1)/r;
}
int main(){
cin>>N>>A>>B;
for(int i=0; i<N; i++) scanf("%lld",&v[i]);
sort(v,v+N);
int i=N; int C=B-A; int E=A;
while(E){
i--;
S+=v[i];
E--; D++;
}
if(v[i]==v[N-1]){
double ans=v[i]; ll cnt=0;
int k=i; int Z=A;
while(1){
k--;
if(v[k]!=v[i]) break;
Z++;
}
if(Z>=B) for(int j=A;j<=B;j++) cnt+=comb(Z,j);
else for(int j=A;j<=Z;j++) cnt+=comb(Z,j);
printf("%.16f\n",ans);
cout<<cnt<<endl;
return 0;
}
else{
ll p=v[i];
int k=i; int X=1;
while(1){
k++;
if(v[k]!=p) break;
X++;
}
int Z=X; //cout<<p<<endl;
k=i;
while(1){
k--;
if(v[k]!=p) break;
X++;
}
ll cnt=0;
double ans=S/D;
printf("%.16f\n",ans); //cout<<Z<<" "<<X<<endl;
cout<<comb(X,Z)<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
constexpr int MAXN = 1003;
constexpr int MAXC = 10004;
int n;
ll w, b, x;
int c[MAXN];
ll cost[MAXN];
ll dp[MAXN][MAXC];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> w >> b >> x;
for (int i = 0; i < n; ++i) cin >> c[i];
for (int i = 0; i < n; ++i) cin >> cost[i];
memset(dp, -1, sizeof(dp));
dp[0][0] = w;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < MAXC and dp[i][j] > -1; ++j) {
for (ll k = 0; k <= c[i] and dp[i][j] - cost[i] * k >= 0; ++k) {
dp[i + 1][j + k] = max(
dp[i + 1][j + k], min(w + (j + k) * b, dp[i][j] + x - cost[i] * k));
}
}
}
int ans = 0;
for (int j = 0; j < MAXC; ++j) {
if (dp[n][j] != -1) ans = max(ans, j);
}
cout << ans << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = (long long)(1e9 + 7);
const long long inv = (long long)(5e8 + 4);
int main() {
long long n, m;
cin >> n >> m;
long long ans = (n % mod) * (m % mod) % mod;
long long l = 1, r = 1;
while (l <= min(n, m)) {
long long a = n / l;
r = min(m, n / a);
long long x1, x2, x3, x4;
x1 = (n / r) % mod;
x2 = (l + r) % mod;
x3 = (x1 * x2) % mod;
x4 = (r - l + 1) % mod;
long long temp = ((x3 * x4) % mod) * inv % mod;
ans = ((ans + mod) - temp) % mod;
l = r + 1;
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
int tcase, n, m, K;
int main() {
scanf("%d", &tcase);
while (tcase--) {
scanf("%d%d%d", &n, &m, &K);
int x = 1;
for (int k = 1; k <= K; k++) {
int y = x;
for (int i = 1; i <= n % m; i++) {
printf("%d", n / m + 1);
for (int j = 1; j <= n / m + 1; j++) printf(" %d", y), y = y % n + 1;
printf("\n");
}
x = y;
for (int i = 1; i <= m - n % m; i++) {
printf("%d", n / m);
for (int j = 1; j <= n / m; j++) printf(" %d", y), y = y % n + 1;
printf("\n");
}
}
printf("\n");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1);
const int maxn = 100000;
const long long inf = (~0ull) >> 2;
long long p[maxn];
long long yue[2][maxn];
long long all[maxn];
int ynum[2], an, a[maxn];
long long base, ans[3], tmp[3], res, suf, v;
int main() {
int T, i, j, k, ii, l, r;
scanf("%d", &T);
long long trv;
for (int cas = 1; cas <= T; ++cas) {
scanf("%d", &k);
ynum[0] = ynum[1] = 0;
v = 1;
yue[0][ynum[0]++] = 1;
yue[1][ynum[1]++] = 1;
for (ii = 0; ii < k; ++ii) {
scanf("%I64d%d", &p[ii], &a[ii]);
for (i = 1; i <= a[ii]; ++i) v *= p[ii];
}
trv = min(ceil(pow(v, 1.0 / 3)) * 1000, ceil(pow(v, 1.0 / 2)));
for (ii = 0; ii < k; ++ii) {
base = p[ii];
for (i = 1; i <= a[ii]; ++i, base *= p[ii])
for (j = 0; j < ynum[0]; ++j) {
if (yue[0][j] * base > trv) continue;
yue[0][ynum[1]] = yue[1][ynum[1]] = yue[0][j] * base;
++ynum[1];
}
ynum[0] = ynum[1];
}
for (i = an = 0; i < ynum[0]; ++i) all[an++] = yue[0][i];
sort(all, all + an);
an = unique(all, all + an) - all;
suf = inf;
trv = floor(pow(v, 1.0 / 3));
int beg = lower_bound(all, all + an, trv) - all;
for (i = max(beg - 1000, 0); i < beg + 1000 && i < an; ++i) {
for (j = i; j < i + 11 && j < an; ++j) {
tmp[0] = all[i];
tmp[1] = all[j];
if (v % (tmp[0] * tmp[1])) continue;
tmp[2] = v / (tmp[0] * tmp[1]);
res = 2 * (tmp[0] * tmp[1] + tmp[1] * tmp[2] + tmp[0] * tmp[2]);
if (res < suf) {
suf = res;
for (k = 0; k < 3; ++k) ans[k] = tmp[k];
}
}
}
printf("%I64d %I64d %I64d %I64d\n", suf, ans[0], ans[1], ans[2]);
}
return 0;
}
| 4 |
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<iomanip>
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
typedef long long ll;
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<string> vs;
Def inf = sizeof(Def) == sizeof(ll) ? 2e18:1e9+10;
#include<complex>
typedef complex<double> P;
typedef vector<P> G;
const double PI=acos(-1);
const double EPS=1e-7;
struct L: public vector<P> {
L(const P &a,const P &b){
push_back(a);push_back(b);
}
};
struct C{
P c;double r;
C(const P &c,double r):c(c),r(r){}
};
namespace std{
bool operator < (const P &a, const P &b){
return real(a) != real(b) ? real(a)<real(b) :imag(a)<imag(b);
}
bool operator == (const P &a, const P &b){
return a.real() == b.real() && a.imag() == b.imag();
}
}
double dot(P a,P b){
return real(conj(a)*b);
}
P projection(L a, P p){
double t=dot(p-a[0],a[0]-a[1])/norm(a[0]-a[1]);
return a[0]+t*(a[0]-a[1]);
}
double distanceLP(const L &l, const P &p){
return abs(p-projection(l,p));
}
P turn(P p,double t){
return p*exp(P(.0,t*PI/180.0));
}
vector<L> tangentCC(C a, C b){
if(a.r<b.r)swap(a,b);
double d=abs(a.c-b.c);
vector<L> l;
if(d<EPS)return l;
if(a.r+b.r<d-EPS){
double t=acos((a.r+b.r)/d);
t=t*180/PI;
l.pb(L(a.c+turn(a.r/d*(b.c-a.c),t),b.c+turn(b.r/d*(a.c-b.c),t)));
l.pb(L(a.c+turn(a.r/d*(b.c-a.c),-t),b.c+turn(b.r/d*(a.c-b.c),-t)));
}else if(a.r+b.r<d+EPS){
P p=a.c+a.r/d*(b.c-a.c);
l.pb(L(p,p+turn(b.c-a.c,90)));
}
if(abs(a.r-b.r)<d-EPS){
double t1=acos((a.r-b.r)/d);
t1=t1*180/PI;
double t2=180-t1;
l.pb(L(a.c+turn(a.r/d*(b.c-a.c),t1),b.c+turn(b.r/d*(a.c-b.c),-t2)));
l.pb(L(a.c+turn(a.r/d*(b.c-a.c),-t1),b.c+turn(b.r/d*(a.c-b.c),t2)));
}else if(abs(a.r-b.r)<d+EPS){
P p=a.c+a.r/d*(b.c-a.c);
l.pb(L(p,p+turn(b.c-a.c,90)));
}
return l;
}
int main(){
int n;
while(cin>>n,n){
vector<C>in;
vector<C>je;
vi r(n),m(n);
rep(i,n){
int x,y;
cin>>x>>y>>r[i]>>m[i];
je.pb(C(P(x,y),r[i]));
in.pb(C(P(x,y),r[i]));
in.pb(C(P(x,y),m[i]+r[i]));
}
if(n==1){
cout<<1<<endl;
continue;
}
int out=0;
rep(i,in.size())loop(j,i+1,in.size()){
vector<L>l=tangentCC(in[i],in[j]);
rep(k,l.size()){
int co=0;
rep(q,n){
double dis=distanceLP(l[k],je[q].c);
if(dis<EPS+m[q]+r[q]&&r[q]<EPS+dis)co++;
}
out=max(out,co);
}
}
cout<<out<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, id_start, id_end, i, j;
string s;
cin >> n >> s;
for (i = 0; i < s.size(); i++) {
if (n - i > 2 && s[i] == 'o' && s[i + 1] == 'g' && s[i + 2] == 'o') {
id_start = i;
id_end = i + 3;
j = i + 3;
while (j + 1 < s.size() && s[j] == 'g' && s[j + 1] == 'o') {
id_end = j + 2;
j += 2;
}
s.erase(id_start, id_end - id_start);
s.insert(id_start, "***");
}
}
cout << s;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int num = 0, f = 1;
for (; !isdigit(c); c = getchar()) f = c == '-' ? -1 : f;
for (; isdigit(c); c = getchar()) num = num * 10 + c - '0';
return num * f;
}
const int N = 25005;
int n, m, l;
bitset<N> a, b, c, d;
char s[100005];
int main() {
n = read(), m = read(), l = read();
for (int i = 0; i < n; ++i) {
scanf("%s", s);
for (int j = 0; j < m; ++j) {
if (s[j] == '#')
b.set(i * m + j);
else
a.set(i * m + j);
if (s[j] == 'E') d.set(i * m + j);
}
}
scanf("%s", s + 1);
c = a;
if (c == d) {
puts("0");
return 0;
}
for (int i = 1; i <= l; ++i) {
if (s[i] == 'U')
c = (c >> m & a) | (c & (b << m));
else if (s[i] == 'D')
c = (c << m & a) | (c & (b >> m));
else if (s[i] == 'L')
c = (c >> 1 & a) | (c & (b << 1));
else
c = (c << 1 & a) | (c & (b >> 1));
if (c == d) {
printf("%d", i);
return 0;
}
}
puts("-1");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string p, s, ep[2001], es[2001], ans = "233";
int slen, plen, k;
int main() {
getline(cin, p);
getline(cin, s);
cin >> k;
slen = s.size();
plen = p.size();
for (int i = 0; i < plen; i++) ep[i % k] += p[i];
for (int l = 1; l <= k; l++) {
for (int i = 0; i < l; i++) es[i].clear();
for (int i = 0; i < slen; i++) es[i % l] += s[i];
int now = l - 1;
string tmp;
for (int i = k - 1; i >= 0; --i)
if (now >= 0 && ep[i] == es[now])
tmp += "1", --now;
else
tmp += "0";
reverse(tmp.begin(), tmp.end());
if (now < 0) ans = min(ans, tmp);
}
if (ans == "233") ans = "0";
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int h,w,x,y;
cin >> h >> w >> x >> y;
cout << (h-x)*(w-y) << endl;
} | 0 |
#include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
int a;
scanf("%d",&a);
printf("%d\n",a*32);
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n, m, k, z[1000002], v[2][500002];
string second, t;
int main() {
cin >> n >> m >> k;
cin >> second >> t;
memset(v, -1, sizeof(v));
for (int h = 0; h < 2; h++) {
string a = t + '$' + second;
memset(z, 0, sizeof(z));
vector<vector<int> > mx;
mx.resize(m + 1);
set<int> S;
int st = 0, dr = 0;
for (int i = 1; i < a.size(); i++) {
if (i > dr) {
st = dr = i;
while (dr < a.size() && a[dr - st] == a[dr]) dr++;
z[i] = dr - st;
dr--;
} else {
int k = i - st;
if (z[k] < dr - i + 1)
z[i] = z[k];
else {
st = i;
while (dr < a.size() && a[dr - st] == a[dr]) dr++;
z[i] = dr - st;
dr--;
}
}
}
for (int i = 0; i < n; i++) {
mx[z[i + (m + 1)]].push_back(i);
if (z[i + (m + 1)] >= m) {
int x = min(i, n - 2 * k);
int y = x + k;
if (y >= n) continue;
cout << "Yes\n";
cout << x + 1 << " " << y + 1;
return 0;
}
}
for (int i = m; i > 0; i--) {
for (auto it : mx[i]) S.insert(it);
auto it = S.lower_bound(k - i);
if (it != S.end()) v[h][i] = *it;
}
reverse(second.begin(), second.end());
reverse(t.begin(), t.end());
}
for (int i = 1; i < m; i++) {
if (i <= k && m - i <= k && v[0][i] != -1 && v[1][m - i] != -1) {
int x = v[0][i] + i - 1, y = v[1][m - i] + (m - i) - 1;
if (x + y >= n - 1) continue;
cout << "Yes\n";
cout << x - (k - 1) + 1 << " " << n - y;
return 0;
}
}
cout << "No";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int getnum() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int N = 4010;
int n, k;
int s[N][N];
int F[N];
int G[N];
struct tnode {
int l, r, x;
} Q[N];
void get_read() {
n = getnum();
k = getnum();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + getnum();
}
}
int trans(int i, int j, int C) {
return F[j] + (s[i][i] + s[j][j] - s[i][j] * 2) / 2 + C;
}
void calc(int C) {
int h = 1, t = 1;
Q[1] = (tnode){1, n, 0};
for (int i = 1; i <= n; ++i) {
while (h < t && Q[h].r < i) ++h;
F[i] = trans(i, Q[h].x, C);
G[i] = G[Q[h].x] + 1;
while (h < t && i >= Q[h].r) ++h;
if (trans(n, Q[t].x, C) <= trans(n, i, C)) continue;
while (h < t && trans(Q[t].l, Q[t].x, C) > trans(Q[t].l, i, C)) --t;
int l = Q[t].l, r = Q[t].r, ret = Q[t].r + 1;
while (l <= r) {
int mid = (l + r) >> 1;
if (trans(mid, Q[t].x, C) > trans(mid, i, C))
ret = mid, r = mid - 1;
else
l = mid + 1;
}
if (ret > n) continue;
Q[t].r = ret - 1;
Q[++t] = (tnode){ret, n, i};
}
}
void get_work() {
int l = 0, r = s[n][n], ans;
while (l <= r) {
int mid = (l + r) >> 1;
calc(mid);
if (G[n] <= k) {
r = mid - 1;
ans = F[n] - mid * k;
} else
l = mid + 1;
}
cout << ans;
}
signed main() {
get_read();
get_work();
return 0;
}
| 5 |
#include<stdio.h>
#pragma GCC optimize ("O2")
int main(void)
{
int K,div,N;long long a=0;K=scanf("%d",&N);
while(K<=N){div=N/K;a+=1ll*K++*div*(div+1)/2;}
return printf("%lld\n",a),0;
} | 0 |
#include <bits/stdc++.h>
template <typename Arg1>
void ZZ(const char* name, Arg1&& arg1) {
std::cout << name << " = " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
std::cout.write(names, comma - names) << " = " << arg1;
ZZ(comma, args...);
}
using namespace std;
long long int to_ll(string s) {
long long int i, ret = 0, p = 1;
for (i = (long long int)s.length() - 1; i >= 0; i--)
ret += (s[i] - '0') * p, p *= 10LL;
return ret;
}
long long int gcd(long long int x, long long int y) {
if (y == 0) return x;
return gcd(y, x % y);
}
long long int pwr(long long int base, long long int expo, long long int m) {
if (base == 0) return 0LL;
if (expo == 0) return (1LL % m);
if ((expo & 1) == 0) {
long long int temp = pwr(base, expo >> 1, m);
return (temp * temp) % m;
}
return ((base % m) * pwr(base, expo - 1, m)) % m;
}
long long int n, pos, l, r;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n >> pos >> l >> r;
long long int ans;
if (l == 1 && r == n)
ans = 0;
else if (l == 1) {
if (pos <= l)
ans = (l - pos) + (r - l + 1);
else if (pos >= r)
ans = (pos - r + 1);
else
ans = r - pos + 1;
} else if (r == n) {
if (pos <= l)
ans = (l - pos + 1);
else if (pos >= r)
ans = (pos - l + 1);
else
ans = pos - l + 1;
} else if (pos <= l)
ans = (l - pos + 1) + (r - l + 1);
else if (pos >= r)
ans = (pos - r + 1) + (r - l + 1);
else
ans = min(pos - l + 1 + r - l + 1, r - pos + 1 + r - l + 1);
cout << ans << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
// 蟻本そのまま
int extgcd(int a, int b, int& x, int& y) {
int d = a;
if(b != 0) {
d = extgcd(b, a%b, y, x);
y -= (a/b) * x;
}
else {
x = 1; y = 0;
}
return d;
}
int main() {
int a, b;
cin >> a >> b;
int x, y;
extgcd(a, b, x, y);
cout << x << " " << y << "\n";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1};
const int dy[] = {3, 3, 3};
int n, k;
bool inRange(int x, int y) {
if (x < 0 || x > 2) return false;
if (y < 0 || y >= n) return false;
return true;
}
int main() {
int T;
cin >> T;
while (T--) {
int s;
string M[3];
vector<bool> vst[3];
cin >> n >> k;
for (int i = 0; i < 3; i++) vst[i].resize(n);
for (int i = 0; i < 3; i++) {
cin >> M[i];
if (M[i][0] == 's') s = i;
}
queue<int> que;
que.push(s);
que.push(0);
bool YES = 0;
while (!que.empty() && !YES) {
int x = que.front();
que.pop();
int y = que.front();
que.pop();
if (!inRange(x, y)) continue;
if (vst[x][y]) continue;
vst[x][y] = 1;
if ('A' <= M[x][y] && M[x][y] <= 'Z') continue;
if (y == n - 1) {
YES = 1;
break;
}
for (int i = 0; i < 3; i++) {
int xx = x;
int yy = y + 1;
if (!inRange(xx, yy)) continue;
if ('A' <= M[xx][yy] && M[xx][yy] <= 'Z') continue;
if (yy == n - 1) {
YES = 1;
break;
}
xx += dx[i];
if (!inRange(xx, yy)) continue;
if ('A' <= M[xx][yy] && M[xx][yy] <= 'Z') continue;
yy++;
if (!inRange(xx, yy)) continue;
if ('A' <= M[xx][yy] && M[xx][yy] <= 'Z') continue;
if (yy == n - 1) {
YES = 1;
break;
}
yy++;
que.push(xx);
que.push(yy);
}
}
if (YES)
puts("YES");
else
puts("NO");
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string x;
getline(cin, x);
int prev = 0;
for (int i = 0; i < x.length(); i++) {
string curStr, prevStr;
curStr = bitset<8>(x.c_str()[i]).to_string();
reverse(curStr.begin(), curStr.end());
unsigned int cur = std::strtol(curStr.c_str(), NULL, 2);
unsigned int res = (prev - cur) % 256;
string ress = bitset<8>(res).to_string();
cout << res << endl;
prev = cur;
}
return 0;
}
| 1 |
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<bitset>
#include<map>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long LL;
typedef double db;
int get(){
char ch;
while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-');
if (ch=='-'){
int s=0;
while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
return -s;
}
int s=ch-'0';
while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0';
return s;
}
const int N = 100010;
int s[N],mx[N];
struct edge{
int x,nxt,c;
}e[N*2];
int h[N],tot;
int n;
LL ans,co[N];
void inse(int x,int y,int c){
e[++tot].x=y;
e[tot].c=c;
e[tot].nxt=h[x];
h[x]=tot;
}
void dfs(int x){
s[x]=1;
mx[x]=0;
for(int p=h[x];p;p=e[p].nxt)
if (!s[e[p].x]){
co[e[p].x]=e[p].c;
dfs(e[p].x);
s[x]+=s[e[p].x];
mx[x]=max(mx[x],s[e[p].x]);
}
mx[x]=max(mx[x],n-s[x]);
ans=ans+2ll*co[x]*min(n-s[x],s[x]);
}
int main(){
n=get();
fo(i,2,n){
int x=get(),y=get(),v=get();
inse(x,y,v);
inse(y,x,v);
}
dfs(1);
bool bz=0;
fo(x,2,n)
if (s[x]==n-s[x]){
ans=ans-co[x];
bz=1;
break;
}
if (!bz)
fo(i,1,n)
if (mx[i]<=n/2){
int tmp =1e+9;
for(int p=h[i];p;p=e[p].nxt)tmp=min(tmp,e[p].c);
ans=ans-tmp;
break;
}
printf("%lld\n",ans);
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
string a;
int main()
{
cin>>a;
if(a[0]=='7' || a[1]=='7' || a[2]=='7')
cout<<"Yes";
else
cout<<"No";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); }
struct index {
int value;
vector<int> pos;
};
struct node {
int start, end;
long long int max_sum;
};
struct answer {
bool is_valid;
long long int max_sum;
};
struct node segment_tree[400002];
struct index ind[300002];
int arr[300002];
bool is_cut[300002];
int binary_search_in_struct(struct index a[], int key, int low, int high) {
if (low > high) {
return -1;
} else {
int mid = (low + high) / 2;
if (a[mid].value == key) {
return mid;
} else if (a[mid].value < key) {
low = mid + 1;
return binary_search_in_struct(a, key, low, high);
} else {
high = mid - 1;
return binary_search_in_struct(a, key, low, high);
}
}
}
long long int build_segment_tree(int in, int a[], int low, int high) {
if (low == high) {
segment_tree[in].start = low;
segment_tree[in].end = high;
if (a[low] >= 0) {
segment_tree[in].max_sum = a[low];
} else {
segment_tree[in].max_sum = 0;
}
return segment_tree[in].max_sum;
} else {
segment_tree[in].start = low;
segment_tree[in].end = high;
segment_tree[in].max_sum =
build_segment_tree(2 * in + 1, a, low, (low + high) / 2) +
build_segment_tree(2 * in + 2, a, (low + high) / 2 + 1, high);
return segment_tree[in].max_sum;
}
}
struct answer query_segment_tree(int in, int pos_start, int pos_end) {
struct answer ans;
if (segment_tree[in].end < pos_start || segment_tree[in].start > pos_end) {
ans.is_valid = false;
return ans;
} else if (segment_tree[in].start >= pos_start &&
segment_tree[in].end <= pos_end) {
ans.max_sum = segment_tree[in].max_sum;
ans.is_valid = true;
return ans;
}
struct answer left_ans = query_segment_tree(2 * in + 1, pos_start, pos_end);
struct answer right_ans = query_segment_tree(2 * in + 2, pos_start, pos_end);
ans.max_sum = 0;
if (left_ans.is_valid) {
ans.max_sum += left_ans.max_sum;
}
if (right_ans.is_valid) {
ans.max_sum += right_ans.max_sum;
}
ans.is_valid = true;
return ans;
}
int main() {
int n;
vector<int> b;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
b.push_back(arr[i]);
}
sort(b.begin(), b.end());
int k = 0;
for (int i = 0; i < n; i++) {
if (binary_search_in_struct(ind, b[i], 0, k - 1) == -1) {
ind[k].value = b[i];
ind[k].pos.clear();
k++;
}
}
for (int i = 0; i < n; i++) {
ind[binary_search_in_struct(ind, arr[i], 0, k - 1)].pos.push_back(i);
}
build_segment_tree(0, arr, 0, n - 1);
long long int max;
struct answer max_ans;
int max_left, max_right;
for (int i = 0; i < k; i++) {
if (ind[i].pos.size() > 1) {
if (ind[i].pos[0] + 1 <= ind[i].pos[ind[i].pos.size() - 1] - 1) {
max_ans = query_segment_tree(0, (ind[i].pos[0]) + 1,
(ind[i].pos[ind[i].pos.size() - 1]) - 1);
max = 2 * ind[i].value + max_ans.max_sum;
max_left = ind[i].pos[0];
max_right = ind[i].pos[ind[i].pos.size() - 1];
} else {
max = 2 * ind[i].value;
max_left = ind[i].pos[0];
max_right = ind[i].pos[ind[i].pos.size() - 1];
}
break;
}
}
for (int i = 0; i < k; i++) {
if (ind[i].pos.size() > 1) {
if (ind[i].pos[0] + 1 <= ind[i].pos[ind[i].pos.size() - 1] - 1) {
max_ans = query_segment_tree(0, ind[i].pos[0] + 1,
ind[i].pos[ind[i].pos.size() - 1] - 1);
if (max < 2 * ind[i].value + max_ans.max_sum) {
max = 2 * ind[i].value + max_ans.max_sum;
max_left = ind[i].pos[0];
max_right = ind[i].pos[ind[i].pos.size() - 1];
}
} else {
if (max < 2 * ind[i].value) {
max = 2 * ind[i].value;
max_left = ind[i].pos[0];
max_right = ind[i].pos[ind[i].pos.size() - 1];
}
}
}
}
int count = 0;
memset(is_cut, 1, sizeof(is_cut));
for (int i = max_left; i <= max_right; i++) {
if (arr[i] >= 0) {
is_cut[i] = false;
}
}
is_cut[max_left] = false;
is_cut[max_right] = false;
for (int i = 0; i < n; i++) {
if (is_cut[i]) {
count++;
}
}
printf("%lld %d\n", max, count);
for (int i = 0; i < n; i++) {
if (is_cut[i]) {
printf("%d ", i + 1);
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename _T>
inline void read(_T &f) {
f = 0;
_T fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + (c & 15);
c = getchar();
}
f *= fu;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x < 10)
putchar(x + 48);
else
print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x);
putchar(t);
}
template <typename T>
struct hash_map_t {
vector<T> v, val, nxt;
vector<int> head;
int mod, tot, lastv;
T lastans;
bool have_ans;
hash_map_t(int md = 0) {
head.clear();
v.clear();
val.clear();
nxt.clear();
tot = 0;
mod = md;
nxt.resize(1);
v.resize(1);
val.resize(1);
head.resize(mod);
have_ans = 0;
}
void clear() { *this = hash_map_t(mod); }
bool count(int x) {
int u = x % mod;
for (register int i = head[u]; i; i = nxt[i]) {
if (v[i] == x) {
have_ans = 1;
lastv = x;
lastans = val[i];
return 1;
}
}
return 0;
}
void ins(int x, int y) {
int u = x % mod;
nxt.push_back(head[u]);
head[u] = ++tot;
v.push_back(x);
val.push_back(y);
}
int qry(int x) {
if (have_ans && lastv == x) return lastans;
count(x);
return lastans;
}
};
const int N = 205, M = 6e5 + 5, INF = 0x7fffffff;
struct edge_t {
int u, v, next, cap, flow;
} G[M << 1];
int head[N], nowhead[N], d[N], x[N], y[N], _x[N], _y[N], a[N], b[N];
int n, m, s, t, tot = 1, al, bl;
inline void addedge(int u, int v, int cap) {
G[++tot] = (edge_t){u, v, head[u], cap, 0}, head[u] = tot;
G[++tot] = (edge_t){v, u, head[v], 0, 0}, head[v] = tot;
}
queue<int> q;
int bfs() {
memset(d, 0, sizeof(d));
d[s] = 1;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (register int i = head[u]; i; i = G[i].next) {
int v = G[i].v;
if (!d[v] && G[i].cap > G[i].flow) {
d[v] = d[u] + 1;
q.push(v);
}
}
}
return d[t];
}
int dfs(int u, int Flow) {
if (u == t || !Flow) return Flow;
int flow = 0, f;
for (register int &i = nowhead[u]; i; i = G[i].next) {
int v = G[i].v;
if (d[v] == d[u] + 1 && (f = dfs(v, min(Flow, G[i].cap - G[i].flow))) > 0) {
G[i].flow += f;
G[i ^ 1].flow -= f;
Flow -= f;
flow += f;
if (!Flow) break;
}
}
return flow;
}
int dinic() {
int ans = 0;
while (bfs()) {
for (register int i = s; i <= t; i++) nowhead[i] = head[i];
ans += dfs(s, INF);
}
return ans;
}
int main() {
read(n);
read(m);
for (register int i = 1; i <= m; i++) {
read(x[i]);
read(y[i]);
read(_x[i]);
read(_y[i]);
a[++al] = x[i];
a[++al] = _x[i] + 1;
b[++bl] = y[i];
b[++bl] = _y[i] + 1;
}
sort(a + 1, a + al + 1);
al = unique(a + 1, a + al + 1) - a - 1;
sort(b + 1, b + bl + 1);
bl = unique(b + 1, b + bl + 1) - b - 1;
s = 0;
t = al + bl + 1;
for (register int i = 1; i < al; i++) addedge(s, i, a[i + 1] - a[i]);
for (register int i = 1; i < bl; i++) addedge(i + al, t, b[i + 1] - b[i]);
for (register int i = 1; i <= m; i++) {
int xl = lower_bound(a + 1, a + al + 1, x[i]) - a;
int xr = lower_bound(a + 1, a + al + 1, _x[i] + 1) - a;
int yl = lower_bound(b + 1, b + bl + 1, y[i]) - b;
int yr = lower_bound(b + 1, b + bl + 1, _y[i] + 1) - b;
for (register int j = xl; j < xr; j++) {
for (register int k = yl; k < yr; k++) {
addedge(j, k + al, INF);
}
}
}
print(dinic(), '\n');
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
for (; t > 0; --t) {
int n, r;
cin >> n >> r;
map<long long int, long long int> aa;
map<long long int, long long int> dp;
int po[r + 1];
for (long long int i = 0; i < r; ++i) {
long long int j;
cin >> j;
aa[j] = 1;
po[i] = j;
}
if (n == 1)
cout << 0 << endl;
else {
dp[n] = 0;
po[r] = 0;
for (long long int i = 1; i < r + 1; ++i) {
if (po[i] == po[i - 1] - 1) {
int z = 1;
z = z;
} else if (po[i] == po[i - 1] - 2)
dp[po[i] + 1] = dp[po[i - 1]];
else {
dp[po[i] + 1] = dp[po[i - 1]];
dp[po[i] + 2] = dp[po[i - 1]];
}
if (aa[po[i]] == 0)
dp[po[i]] = dp[po[i] + 1];
else
dp[po[i]] = 1 + dp[po[i] + 1];
if (po[i] + 2 <= n && aa[po[i] + 1] == 1 && aa[po[i]] == 1) {
dp[po[i]] = min(dp[po[i]], dp[po[i] + 2]);
} else if (po[i] + 2 <= n && aa[po[i] + 1] == 0 && aa[po[i]] == 0)
dp[po[i]] = min(dp[po[i]], dp[po[i] + 2] + 2);
else if (po[i] + 2 <= n)
dp[po[i]] = min(dp[po[i]], dp[po[i] + 2] + 1);
if (i == r) {
dp[0] = dp[po[i] + 1];
if (po[i] + 2 <= n && aa[po[i] + 1] == 1)
dp[0] = min(dp[po[i]], dp[po[i] + 2]);
else if (po[i] + 2 <= n)
dp[0] = min(dp[po[i]], dp[po[i] + 2] + 1);
}
}
cout << dp[0] << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<int(n);++i)
using ll = long long;
string s[410];
int Y, X;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
void calc(int y, int x, ll &black, ll &white, char pre) {
if (y < 0 || x < 0 || x>=X || y>=Y) return;
if (s[y][x] == '!' || s[y][x] == pre) return;
if (s[y][x] == '#') black++;
if (s[y][x] == '.') white++;
char cur = s[y][x];
s[y][x] = '!';
rep(i,4){
calc(y+dy[i], x+dx[i], black, white, cur);
}
}
int main(void){
cin >> Y >> X;
rep(y,Y) cin >> s[y];
ll res = 0;
rep(y,Y) rep(x,X){
ll black = 0;
ll white = 0;
calc(y, x, black, white, '!');
res += black * white;
}
cout << res << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dp[3939][50];
int n, m, k;
int a[3939];
int getMin(int x, int y) { return x < y ? x : y; }
int getMax(int x, int y) { return x > y ? x : y; }
void solve() {
int i = 0, j, d;
vector<int> vec;
scanf("%d %d %d", &n, &m, &k);
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
d = n - m;
for (i = 0; i + d < n; i++) {
vec.push_back(getMax(a[i], a[i + d]));
}
for (i = 0; i < vec.size(); i++) {
dp[i][0] = vec[i];
}
int ans = 0;
d = vec.size() - k;
if (d < 1) d = 1;
for (i = 0; i + d <= vec.size(); i++) {
int x = vec[i];
for (j = 0; j < d; j++) {
if (vec[i + j] < x) {
x = vec[i + j];
}
}
if (x > ans) {
ans = x;
}
}
printf("%d\n", ans);
}
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[100500];
vector<int> gr[100500];
char res[100500];
int p[100500];
char x[100500];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
p[a[i]] = i;
}
for (int i = 1; i <= n; i++) {
int d = a[i];
for (int j = i; j >= 1; j -= d) {
if (a[j] > a[i]) {
gr[a[i]].push_back(a[j]);
}
}
for (int j = i; j <= n; j += d) {
if (a[j] > a[i]) {
gr[a[i]].push_back(a[j]);
}
}
}
for (int i = n; i >= 1; i--) {
res[i] = 'B';
for (auto v : gr[i]) {
if (res[v] == 'B') {
res[i] = 'A';
}
}
x[p[i]] = res[i];
}
for (int i = 1; i <= n; i++) {
cout << x[i];
}
return 0;
}
| 3 |
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cstdio>
using namespace std;
int main()
{
double a,b;
cin>>a>>b;
printf("%.5f\n",a*b/3.305785);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct amulet {};
int main() {
int n, k, c;
scanf("%d%d%d", &n, &k, &c);
vector<bool> arr(n + 1, false);
for (int i = 0; i < c; i++) {
int t;
scanf("%d", &t);
arr[t] = true;
}
int pre = 1, now = 1;
while (now <= n) {
if (arr[now])
pre = 1;
else {
if (pre == k) {
arr[now] = true;
pre = 1;
} else
pre++;
}
now++;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
ans += arr[i] ? 1 : 0;
}
printf("%d\n", ans);
return 0;
}
| 1 |
#include <cstdio>
#include <iostream>
#include <cstring>
#define maxn 36
using namespace std;
char map[6][3][3], tmp[maxn];
int hole[6][4], hpair[6][4];
void clean()
{
memset(map, 0, sizeof(map));
memset(hole, 0, sizeof(hole));
memset(hpair, 0, sizeof(hpair));
}
void read()
{
char tmp2[maxn];
for(int i = 0; i < 6; i++){
for(int j = 0; j < 3; j++){
if(i == 0 && j == 0)
strcpy(tmp2, tmp);
else scanf("%s", tmp2);
for(int k = 0; k < 3; k++)
map[i][j][k] = tmp2[k];
}
}
}
void signp()
{
hpair[0][0] = hole[0][0] & hole[2][0];//1-3-0
hpair[0][1] = hole[0][1] & hole[2][3];//1-3-1
hpair[0][2] = hole[0][2] & hole[2][2];//1-3-2
hpair[0][3] = hole[0][3] & hole[2][1];//1-3-3
hpair[1][0] = hole[1][0] & hole[3][0];
hpair[1][1] = hole[1][1] & hole[3][3];
hpair[1][2] = hole[1][2] & hole[3][2];
hpair[1][3] = hole[1][3] & hole[3][1];
hpair[4][0] = hole[4][0] & hole[5][2];
hpair[4][1] = hole[4][1] & hole[5][1];
hpair[4][2] = hole[4][2] & hole[5][0];
hpair[4][3] = hole[4][3] & hole[5][3];
}
void sign()
{
for(int i = 0; i < 6; i++)
for(int j = 0; j < 3; j++)
for(int k = 0; k < 3; k++)
if(map[i][j][k] == '*'){
if(j == 0) hole[i][0] = 1;//up
if(k == 2) hole[i][1] = 1;//right
if(j == 2) hole[i][2] = 1;//down
if(k == 0) hole[i][3] = 1;
}
signp();
}
int chk()
{
if(hpair[0][2]) return 0;
if(hpair[0][1]) return 1;
if(hpair[0][3]) return 1;
if(hpair[4][0]) return 1;
if(hpair[4][2]) return 1;
if(hpair[4][1]) return 2;
if(hpair[4][3]) return 2;
if(hpair[0][0]) return 2;
if(hpair[1][1]) return 2;
if(hpair[1][3]) return 2;
if(hpair[1][0]) return 3;
if(hpair[1][2]) return 3;
return -1;
}
void solve()
{
clean();
read();
sign();
cout << chk() << endl;
}
int main(void)
{
while(scanf("%s", tmp), tmp[0] !='#')
solve();
return 0;
} | 0 |
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef pair<int,int> P;
int D(P p){
int x = p.first;
int y = p.second;
return (x * x + y * y);
}
void solve(){
int x=0, y=0, dx, dy, ans_d=0;
vector<P> vp;
P ans_p;
bool flag = false;
while( cin >> dx >> dy , dx || dy ){
x += dx;
y += dy;
P p(x,y);
vp.push_back(p);
}
for(int i=0 ; i < vp.size() ; i++ ){
ans_d = max( ans_d , D(vp[i]) );
}
for(int i=0 ; i < vp.size() ; i++ ){
if( ans_d == D(vp[i]) ){
if( flag == false ){
flag = true;
ans_p = vp[i];
}else{
ans_p = max( ans_p , vp[i] );
}
}
}
cout << ans_p.first << " " << ans_p.second << endl;
}
int main(){
int N;
cin >> N;
for(int i=0 ; i < N ; i++ )
solve();
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
unsigned long long int arr[n], sumarr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
unsigned long long int e, t = 0, m = *min_element(arr, arr + n);
bool pos = true;
for (int i = 0; i < n && pos == true; i++) {
e = arr[i];
sumarr[i] = (e - m) / k;
if ((e - m) % k != 0) pos = false;
}
if (pos == true) {
for (int i = 0; i < n; i++) {
t += sumarr[i];
}
cout << t << endl;
} else
cout << -1 << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline void splay(int &v) {
v = 0;
char c = 0;
int p = 1;
while (c < '0' || c > '9') {
if (c == '-') p = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = (v << 3) + (v << 1) + c - '0';
c = getchar();
}
v *= p;
}
int a[200010], t[200010];
int main() {
int n, m;
splay(n);
splay(m);
for (int i = 1; i <= n; i++) splay(a[i]);
t[n] = n + 1;
for (int i = n - 1; i >= 1; i--) {
if (a[i] == a[i + 1])
t[i] = t[i + 1];
else
t[i] = i + 1;
}
for (int i = 1; i <= m; i++) {
int l, r, x;
splay(l), splay(r), splay(x);
if (a[l] != x)
printf("%d\n", l);
else {
if (t[l] <= r)
printf("%d\n", t[l]);
else
printf("-1\n");
}
}
return (0);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int dp[5001][5001];
int quickpow(int x, int y) {
long long res = 1, base = x;
while (y) {
if (y & 1) res *= base, res %= 1000000007;
y >>= 1;
base *= base, base %= 1000000007;
}
return res;
}
int get_ans(int a, int b, int tot) {
if (dp[a][b]) return dp[a][b];
int c = tot - b;
if (a == 0) {
return dp[a][b] = quickpow(2, c);
}
return dp[a][b] = (((b ? (1ll * b * get_ans(a - 1, b, tot)) : 0ll)) +
((c ? (1ll * c * get_ans(a - 1, b + 1, tot)) : 0ll))) %
1000000007;
}
signed main() {
int n, k;
scanf("%d%d", &n, &k);
printf("%d\n", get_ans(k, 0, n));
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 250005;
struct gripper {
int x, y, m, p;
long long dist, r;
gripper(){};
gripper(long long a) { dist = a; }
} grips[MAXN];
void calc_dist(int i) {
grips[i].dist =
(long long)(grips[i].x - grips[0].x) * (grips[i].x - grips[0].x) +
(long long)(grips[i].y - grips[0].y) * (grips[i].y - grips[0].y);
}
bool operator<(gripper a, gripper b) { return a.dist < b.dist; }
int n, ri;
int tree[4 * MAXN];
void change(int k, int v, int i = 1, int l = 0, int r = n - 1) {
if (k < l || r < k) return;
if (l == r) {
tree[i] = v;
return;
}
int mid = (l + r) >> 1;
change(k, v, 2 * i, l, mid);
change(k, v, 2 * i + 1, mid + 1, r);
tree[i] = min(tree[2 * i], tree[2 * i + 1]);
}
int find_first(int v, int i = 1, int l = 0, int r = n - 1) {
if (l == r) {
if (tree[i] > v) return -1;
return l;
}
int mid = (l + r) >> 1;
if (tree[2 * i] <= v)
return find_first(v, 2 * i, l, mid);
else
return find_first(v, 2 * i + 1, mid + 1, r);
}
int ngrips;
void dfs(int i) {
change(i, INT_MAX);
int en = upper_bound(grips, grips + n,
gripper((long long)grips[i].r * grips[i].r)) -
grips;
int j = find_first(grips[i].p);
while (j != -1 && j < en) {
ngrips++;
dfs(j);
j = find_first(grips[i].p);
}
}
int main() {
scanf("%d %d %d %d %d", &grips[0].x, &grips[0].y, &grips[0].p, &grips[0].r,
&n);
grips[0].m = INT_MAX;
grips[0].dist = 0;
n++;
for (int i = 1; i < n; i++) {
scanf("%d %d %d %d %d", &grips[i].x, &grips[i].y, &grips[i].m, &grips[i].p,
&ri);
grips[i].r = ri;
calc_dist(i);
}
sort(grips, grips + n);
for (int i = 0; i < n; i++) change(i, grips[i].m);
ngrips = 0;
dfs(0);
printf("%d\n", ngrips);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, m;
cin >> N >> m;
vector<int> c(m), d(m);
for (int i = 0; i < m; i++){
cin >> c[i] >> d[i];
}
vector<int> cnt(N + 1, 0);
for (int i = 0; i < m; i++){
for (int j = c[i]; j < d[i]; j++){
cnt[j]++;
}
}
int ans = N + 1;
for (int i = 0; i <= N; i++){
if (cnt[i] > 0){
ans += 2;
}
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, op;
int flag[35];
int main() {
scanf("%d %d", &m, &n);
for (int i = 0; i < n; i++) {
puts("1");
fflush(stdout);
scanf("%d", &op);
if (op == 1)
flag[i] = 1;
else if (op == -1)
flag[i] = -1;
else
exit(0);
}
int l = 1, r = m, cnt = 0;
while (l <= r) {
int mid = (l + r) >> 1;
printf("%d\n", mid);
fflush(stdout);
scanf("%d", &op);
op *= flag[cnt % n];
cnt++;
if (op == 1)
l = mid + 1;
else if (op == -1)
r = mid - 1;
else
exit(0);
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
typedef long long i64;
const int P=998244353,M=100007;
char s1[M],s2[M];
int A=0,B=0,N,K,rev[M];
i64 fac[M],fiv[M],_A[M],_B[M],E[M];
i64 pw(i64 a,i64 n){
if(n<0)n+=P-1;
i64 v=1;
for(;n;n>>=1,a=a*a%P)if(n&1)v=v*a%P;
return v;
}
void init(int mx){
for(N=2,K=0;N<mx;N<<=1,++K);
for(int i=1;i<N;++i)rev[i]=rev[i>>1]>>1|(i&1)<<K;
}
inline i64 fix(i64 x){return x+(x>>63&P);}
void ntt(i64*a,int t){
for(int i=0;i<N;++i)if(i<rev[i])std::swap(a[i],a[rev[i]]);
for(int i=1;i<N;i<<=1){
E[0]=1;
E[1]=pw(3,(P-1)/(i<<1)*t);
for(int j=2;j<i;++j)E[j]=E[j-1]*E[1]%P;
for(int j=0;j<N;j+=i<<1){
i64*b=a+j,*c=b+i;
for(int k=0;k<i;++k){
i64 x=b[k],y=c[k]*E[k]%P;
b[k]=fix(x+y-P);
c[k]=fix(x-y);
}
}
}
if(t==-1){
i64 I=pw(N,-1);
for(int i=0;i<=A;++i)a[i]=a[i]*I%P;
for(int i=A+1;i<N;++i)a[i]=0;
}
}
void pre(){
const int mx=1e5;
for(int i=fac[0]=1;i<=mx;++i)fac[i]=i*fac[i-1]%P;
fiv[mx]=pw(fac[mx],-1);
for(int i=mx;i;--i)fiv[i-1]=i*fiv[i]%P;
}
void mul(i64*a,i64*b){
for(int i=0;i<N;++i)a[i]=a[i]*b[i]%P;
}
int main(){
scanf("%s%s",s1,s2);
pre();
for(int i=0;s1[i];++i)if(s1[i]=='1'){
if(s2[i]=='1')++A;
else ++B;
}
for(int i=0;i<=A;++i)_A[i]=fiv[i+1];
_B[0]=1;
init(A*2+5);
for(int n=B;n;n>>=1){
if(n&1)ntt(_B, 1);ntt(_A, 1);
if(n&1)mul(_B,_A);mul(_A,_A);
if(n&1)ntt(_B,-1);ntt(_A,-1);
}
i64 ans=0;
for(int i=0;i<=A;++i)ans=fix(ans+_B[i]-P);
ans=ans*fac[A]%P*fac[B]%P*fac[A+B]%P;
printf("%lld\n",ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
for (long long i = 0; i < n - 1; i++)
if ((arr[i] == 2 && arr[i + 1] == 3) || (arr[i] == 3 && arr[i + 1] == 2))
return cout << "Infinite", 0;
long long sum = 0;
for (long long i = 0; i < n - 1; i++) {
if ((arr[i] == 2 && arr[i + 1] == 1) || (arr[i + 1] == 2 && arr[i] == 1))
sum += 3;
else if ((arr[i] == 3 && arr[i + 1] == 1) ||
(arr[i] == 1 && arr[i + 1] == 3))
sum += 4;
}
for (long long i = 2; i < n; i++) {
if (arr[i] == 2 && arr[i - 1] == 1 && arr[i - 2] == 3) sum--;
}
cout << "Finite" << endl;
cout << sum;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int Mod = 1e9 + 7;
int n, m, k, sum[100], a[3][1000005];
struct Matrix {
int a[100][100];
int* operator[](int idx) { return a[idx]; }
} s, s1, s2, s3;
Matrix get(int a[]) {
Matrix s;
memset(s.a, 0, sizeof(s.a));
memset(sum, 0, sizeof(sum));
for (int i = 0; i < n; i++) {
sum[a[i] % k]++;
}
for (int i = 0; i < k; i++)
for (int j = 0; j < k; j++) s[i][(i + j) % k] += sum[j];
return s;
}
Matrix operator*(Matrix a, Matrix b) {
Matrix c;
memset(c.a, 0, sizeof(c.a));
for (int i = 0; i < k; i++)
for (int j = 0; j < k; j++)
for (int l = 0; l < k; l++)
c[i][j] = (c[i][j] + 1LL * a[i][l] * b[l][j]) % Mod;
return c;
}
void Pow(Matrix& s, Matrix a, int x) {
for (; x; x >>= 1, a = a * a)
if (x & 1) s = s * a;
}
int main() {
scanf("%d %d %d", &n, &m, &k);
for (int i = 0; i < 3; i++)
for (int j = 0; j < n; j++) scanf("%d", &a[i][j]);
s1 = get(a[0]);
s2 = get(a[1]);
memset(s.a, 0, sizeof(s.a));
s[0][0] = 1;
s = s * s1;
Pow(s, s2, m - 2);
int ans = 0;
for (int i = 0; i < n; i++) {
int tmp = a[1][i] + a[2][i];
tmp = k - tmp % k;
if (tmp == k) tmp = 0;
ans = (ans + s[0][tmp]) % Mod;
}
printf("%d\n", ans);
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 long long ll;
const ll MOD = 1000000007;
ll N, M, A[1000], B[1000];
bool row[1000001], col[1000001];
ll solve() {
REP(i, 0, N) {
if (row[A[i]]) return 0;
row[A[i]] = true;
}
REP(i, 0, M) {
if (col[B[i]]) return 0;
col[B[i]] = true;
}
ll ans = 1, cr = 0, cc = 0;
for (ll k = N * M; k >= 1; k--) {
ll r = row[k], c = col[k];
if (row[k] && col[k]) {
cr++;
cc++;
} else if (row[k] && !col[k]) {
(ans *= cc) %= MOD;
cr++;
} else if (!row[k] && col[k]) {
(ans *= cr) %= MOD;
cc++;
} else {
ll t = N * M - k, x = (cr * cc) - t;
(ans *= x) %= MOD;
}
}
return ans;
}
int main(void) {
cin >> N >> M;
REP(i, 0, N) cin >> A[i];
REP(i, 0, M) cin >> B[i];
cout << solve() << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, T;
long long a[25][25];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cout << ((i + 1ll) % 2 << i + j) << ' ';
cout << endl;
}
fflush(stdout);
int T;
cin >> T;
while (T--) {
long long k;
cin >> k;
int x = 0, y = 0;
cout << 1 << ' ' << 1 << endl;
for (long long i = 1; i <= n * 2 - 2; i++) {
if ((k >> i) % 2 == x % 2)
x++;
else
y++;
cout << x + 1 << ' ' << y + 1 << endl;
}
fflush(stdout);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
long long l;
cin >> l;
vector<long long> arr(n + 5);
for (__typeof(n + 1) i = (1) - (1 > n + 1); i != (n + 1) - (1 > n + 1);
i += 1 - 2 * (1 > n + 1))
cin >> arr[i];
vector<int> used(n + 2, 0);
int grps = 0;
for (__typeof(n + 1) i = (1) - (1 > n + 1); i != (n + 1) - (1 > n + 1);
i += 1 - 2 * (1 > n + 1)) {
if (arr[i] > l) {
if (used[i - 1] != 1) grps++;
used[i] = 1;
}
}
while (m--) {
int t;
cin >> t;
if (t == 0)
cout << grps << '\n';
else {
int a, p;
cin >> a >> p;
arr[a] += p;
if (arr[a] > l and used[a] != 1) {
used[a] = 1;
if (used[a - 1] != 1 and used[a + 1] != 1) grps++;
if (used[a - 1] == 1 and used[a + 1] == 1) grps--;
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline string get() {
char ch[1000010];
scanf("%s", ch);
return ch;
}
const int MAX_LG = 21;
const int maxn = 5e5 + 10;
const int base = 29;
const int mod = 1e9 + 7;
const int INF = 1e9;
inline int getlen(int x) {
int ret = 0;
while (x) ret++, x /= 10;
return ret;
}
int has[20], cnt2[20], cnt[20], theln;
string res, ans, check, mini;
int32_t main() {
string s = get(), t = get();
if (s == "0" || s == "00") {
cout << 0 << "\n";
return 0;
}
for (int i = 1; i <= 1000000; i++) {
int ln = getlen(i);
if (i + ln == s.size()) {
theln = i;
break;
}
}
for (int i = 0; i < s.size(); i++) cnt[s[i] - '0']++;
while (theln) {
cnt[theln % 10]--;
theln /= 10;
}
for (int i = 0; i < t.size(); i++) cnt[t[i] - '0']--;
for (int i = 0; i <= 10; i++) cnt2[i] = cnt[i];
for (int i = 0; i <= 9; i++)
while (cnt2[i]) cnt2[i]--, mini += char(i + '0');
int lim = -1;
if (!cnt[0]) {
for (int i = 1; i <= 9; i++) {
while (cnt[i]) {
res += char('1' + i - 1);
cnt[i]--;
}
}
} else {
int mrk = -1;
for (int i = 1; i <= 9; i++) {
if (cnt[i]) {
mrk = i;
cnt[i]--;
break;
}
}
if (mrk != -1) res += char('1' + mrk - 1);
for (int i = 0; i <= 9; i++) {
while (cnt[i]) {
res += char('1' + i - 1);
cnt[i]--;
}
}
}
if (res.size() >= 2 && res[1] == '0')
for (int j = 1; j < res.size() && res[j] == '0'; j++) lim = j;
if (!res.size()) {
cout << t << "\n";
return 0;
}
for (int i = 0; i < res.size(); i++) has[res[i] - '0']++;
if (res[0] == '0') {
cout << t << res << "\n";
return 0;
}
if (t[0] != '0') {
ans = t + mini;
} else {
for (int i = 0; i <= 1e6; i++) ans += '9';
}
int yk = -1;
if (t.size() > 1)
for (int i = 1; i < t.size(); i++) {
if (yk == -1 && t[i] != t[0]) {
yk = t[i] - '0';
}
}
if (!has[t[0] - '0']) {
bool fl = false;
for (int i = lim + 1; i < s.size(); i++) {
if (s[i] > t[0]) {
lim = max(lim, i - 1);
break;
}
}
for (int i = 0; i < res.size(); i++) {
if (i > lim && (t[i] != '0' || i) && !fl) {
check += t;
fl = true;
}
check += res[i];
}
if (!fl) check += t;
} else if (yk == -1 || yk > (t[0] - '0')) {
bool fl = false;
for (int i = 0; i < res.size(); i++) {
if (res[i] > t[0]) break;
if (res[i] == t[0]) lim = i;
}
for (int i = 0; i < res.size(); i++) {
if (i > lim && !fl) {
check += t;
fl = true;
}
check += res[i];
}
if (!fl) check += t;
} else {
bool fl = false;
for (int i = lim + 1; i < res.size(); i++) {
if (res[i] == t[0]) {
lim = max(lim, i - 1);
break;
}
}
for (int i = 0; i < res.size(); i++) {
if (i > lim && !fl) {
check += t;
fl = true;
}
check += res[i];
}
if (!fl) check += t;
}
ans = min(ans, check);
cout << ans << "\n";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const long long MAX = 1e9 + 7;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int a[n];
set<int> s;
for (int i = 0; i < n; i++) s.insert(i + 1);
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i]) s.erase(a[i]);
}
int prev = -1;
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
auto it = s.begin();
while (it != s.end() && *it == (i + 1)) it++;
if (it == s.end()) {
it = s.begin();
a[i] = a[prev];
a[prev] = *it;
} else {
a[i] = *it;
s.erase(it);
prev = i;
}
}
}
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> g[100010];
int visit[100010] = {0};
double ans = 0;
void dfs(long long int n, double prob, long long int d) {
visit[n] = 1;
int f = 0;
for (long long int i = 0; i < g[n].size(); i++) {
if (visit[g[n][i]] == 0) {
f = 1;
if (n == 1)
dfs(g[n][i], prob / g[n].size(), d + 1);
else
dfs(g[n][i], prob / (g[n].size() - 1), d + 1);
}
}
if (f == 0) ans += d * prob;
return;
}
int main() {
ios::sync_with_stdio(false);
long long int n;
cin >> n;
for (long long int i = 1; i < n; i++) {
long long int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1, 0);
std::cout << std::fixed;
std::cout << std::setprecision(7);
cout << ans << endl;
return (0);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3010;
char s[N], t[N], st[N];
int f[N][N];
int main() {
scanf("%s%s", s + 1, t + 1);
int n = strlen(s + 1), m = strlen(t + 1);
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= m; ++j) {
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if(s[i] == t[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
}
}
int now = f[n][m] + 1;
int top = 0;
for(int i = n; i; --i) {
for(int j = m; j; --j) {
if(s[i] == t[j] && f[i][j] == now - 1) {
--now;
st[++top] = s[i];
break;
}
}
}
while(top) putchar(st[top--]);
} | 0 |
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, k, a[N], s[N];
bool check(int x) {
for(int i = 1; i <= n; i++) s[i] = s[i - 1] + (a[i] >= x ? 1 : -1);
int mi[2] = {1000000, 1000000};
for(int i = k; i <= n; i++) {
int j = i - k;
mi[j & 1] = min(mi[j & 1], s[j]);
if(mi[0] <= s[i] - !(i&1)) return true;
if(mi[1] <= s[i] - (i&1)) return true;
}
return false;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
int l = 1, r = n;
while(l < r) {
int mid = (l + r + 1) >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
cout << l << endl;
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int z = 0; z < n; z++) {
string s;
cin >> s;
queue<char> odds;
queue<char> evens;
for (uint i = 0; i < s.size(); i++) {
int parity = (s[i] - '0') % 2;
if (parity == 0) {
evens.push(s[i]);
} else {
odds.push(s[i]);
}
}
vector<char> output;
while (evens.size() > 0 && odds.size() > 0) {
if (evens.front() < odds.front()) {
output.push_back(evens.front());
evens.pop();
} else {
output.push_back(odds.front());
odds.pop();
}
}
while (evens.size() > 0) {
output.push_back(evens.front());
evens.pop();
}
while (odds.size() > 0) {
output.push_back(odds.front());
odds.pop();
}
for (char c : output) {
cout << c;
}
cout << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
template <typename T, size_t N>
struct MakeVector {
template <typename... Args,
typename R = std::vector<decltype(MakeVector<T, N - 1>::make_vector(
std::declval<Args>()...))>>
static R make_vector(std::size_t first, Args... sizes) {
auto inner = MakeVector<T, N - 1>::make_vector(sizes...);
return R(first, inner);
}
};
template <typename T>
struct MakeVector<T, 1> {
template <typename R = std::vector<T>>
static R make_vector(std::size_t size, const T& value) {
return R(size, value);
}
};
template <typename T, typename... Args>
auto make_vector(Args... args)
-> decltype(MakeVector<T, sizeof...(Args) - 1>::make_vector(args...)) {
return MakeVector<T, sizeof...(Args) - 1>::make_vector(args...);
}
template <typename T>
class IntegerIterator
: public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> {
public:
explicit IntegerIterator(int value) : value(value) {}
IntegerIterator& operator++() {
++value;
return *this;
}
IntegerIterator operator++(int) {
IntegerIterator copy = *this;
++value;
return copy;
}
IntegerIterator& operator--() {
--value;
return *this;
}
IntegerIterator operator--(int) {
IntegerIterator copy = *this;
--value;
return copy;
}
T operator*() const { return value; }
bool operator==(IntegerIterator rhs) { return value == rhs.value; }
bool operator!=(IntegerIterator rhs) { return !(*this == rhs); }
private:
T value;
};
template <typename T>
class IntegerRange {
public:
IntegerRange(T begin, T end) : begin_(begin), end_(end) {}
IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); }
IntegerIterator<T> end() const { return IntegerIterator<T>(end_); }
private:
T begin_;
T end_;
};
template <typename T>
class ReversedIntegerRange {
public:
ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) {}
std::reverse_iterator<IntegerIterator<T>> begin() const {
return std::reverse_iterator<IntegerIterator<T>>(
IntegerIterator<T>(begin_));
}
std::reverse_iterator<IntegerIterator<T>> end() const {
return std::reverse_iterator<IntegerIterator<T>>(IntegerIterator<T>(end_));
}
private:
T begin_;
T end_;
};
template <typename T>
IntegerRange<T> range(T to) {
;
return IntegerRange<T>(0, to);
}
template <typename T>
IntegerRange<T> range(T from, T to) {
;
return IntegerRange<T>(from, to);
}
template <typename T>
IntegerRange<T> inclusiveRange(T to) {
;
return IntegerRange<T>(0, to + 1);
}
template <typename T>
IntegerRange<T> inclusiveRange(T from, T to) {
;
return IntegerRange<T>(from, to + 1);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from) {
;
return ReversedIntegerRange<T>(from, 0);
}
template <typename T>
ReversedIntegerRange<T> downrange(T from, T to) {
;
return ReversedIntegerRange<T>(from, to);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from) {
;
return ReversedIntegerRange<T>(from + 1, 0);
}
template <typename T>
ReversedIntegerRange<T> inclusiveDownrange(T from, T to) {
;
return ReversedIntegerRange<T>(from + 1, to);
}
template <typename T>
T identity();
template <typename T, typename Enable = std::true_type>
struct impl__IdentityHelper {};
template <typename T>
struct impl__SampleIdentityHelper {
static T identity(const T&) { return ::identity<T>(); }
};
template <typename T>
struct impl__IdentityHelper<T, typename std::is_arithmetic<T>::type> {
static T identity() { return 1; }
};
template <typename T>
T identity(const T& sample) {
return impl__SampleIdentityHelper<T>::identity(sample);
}
template <typename T>
T identity() {
return impl__IdentityHelper<T>::identity();
}
struct impl__Normalizator {
static void softUp(int& value, int mod) {
if (value < 0) {
value += mod;
}
}
static void softDown(int& value, int mod) {
if (value >= mod) {
value -= mod;
}
}
template <typename T>
static void hardDown(T& value, int mod) {
value %= mod;
}
template <typename T>
static void hard(T& value, int mod) {
value %= mod;
softUp(value, mod);
}
};
template <typename T>
T extendedGcd(T a, T b, T& x, T& y) {
if (a == 0) {
x = 0;
y = 1;
return b;
}
T d = extendedGcd(b % a, a, y, x);
x -= (b / a) * y;
return d;
}
template <int mod>
class Zn {
static_assert(mod > 0, "Mod has to be positive integer");
public:
Zn() : value(0) {}
static Zn valueOf(int value) {
impl__Normalizator::hard(value, mod);
return Zn(value);
}
static Zn valueOf(long long value) {
impl__Normalizator::hard(value, mod);
return Zn(static_cast<int>(value));
}
Zn& operator+=(const Zn& rhs) {
value += rhs.value;
impl__Normalizator::softDown(value, mod);
return *this;
}
Zn& operator+=(int rhs) { return *this += Zn::valueOf(rhs); }
Zn& operator-=(const Zn& rhs) {
value -= rhs.value;
impl__Normalizator::softUp(value, mod);
return *this;
}
Zn& operator-=(int rhs) { return *this -= Zn::valueOf(rhs); }
Zn& operator*=(const Zn& rhs) {
long long result = value;
result *= rhs.value;
impl__Normalizator::hardDown(result, mod);
value = static_cast<int>(result);
return *this;
}
Zn& operator*=(int rhs) { return *this *= Zn::valueOf(rhs); }
Zn operator-() const {
Zn result(mod - value);
impl__Normalizator::softDown(result.value, mod);
return result;
}
Zn& operator/=(const Zn& rhs) { return *this *= rhs.inversed(); }
Zn& operator/=(int rhs) { return *this /= Zn::valueOf(rhs); }
bool operator==(const Zn& rhs) const { return value == rhs.value; }
Zn inversed() const {
;
int x, y;
int gcd = extendedGcd(value, mod, x, y);
(void)gcd;
;
impl__Normalizator::softUp(x, mod);
return Zn(x);
}
template <int m>
friend std::ostream& operator<<(std::ostream&, const Zn<m>& zn);
template <int m>
friend std::istream& operator>>(std::istream&, Zn<m>& zn);
int intValue() const { return value; }
private:
explicit Zn(int value) : value(value) {}
int value;
};
template <int m>
bool operator==(const Zn<m>& lhs, int rhs) {
return lhs == Zn<m>::valueOf(rhs);
}
template <int m>
bool operator==(int lhs, const Zn<m>& rhs) {
return rhs == lhs;
}
template <int m>
bool operator!=(const Zn<m>& lhs, const Zn<m>& rhs) {
return !(lhs == rhs);
}
template <int m>
bool operator!=(const Zn<m>& lhs, int rhs) {
return !(lhs == rhs);
}
template <int m>
bool operator!=(int lhs, const Zn<m>& rhs) {
return !(lhs == rhs);
}
template <int m>
Zn<m> operator+(const Zn<m>& lhs, const Zn<m>& rhs) {
Zn<m> copy = lhs;
return copy += rhs;
}
template <int m>
Zn<m> operator+(const Zn<m>& lhs, int rhs) {
Zn<m> copy = lhs;
return copy += rhs;
}
template <int m>
Zn<m> operator+(int lhs, const Zn<m>& rhs) {
return rhs + lhs;
}
template <int m>
Zn<m> operator-(const Zn<m>& lhs, const Zn<m>& rhs) {
Zn<m> copy = lhs;
return copy -= rhs;
}
template <int m>
Zn<m> operator-(const Zn<m>& lhs, int rhs) {
Zn<m> copy = lhs;
return copy -= rhs;
}
template <int m>
Zn<m> operator-(int lhs, const Zn<m>& rhs) {
return Zn<m>::valueOf(lhs) - rhs;
}
template <int m>
Zn<m> operator*(const Zn<m>& lhs, const Zn<m>& rhs) {
Zn<m> copy = lhs;
return copy *= rhs;
}
template <int m>
Zn<m> operator*(const Zn<m>& lhs, int rhs) {
Zn<m> copy = lhs;
return copy *= rhs;
}
template <int m>
Zn<m> operator*(int lhs, const Zn<m>& rhs) {
return rhs * lhs;
}
template <int m>
Zn<m> operator/(const Zn<m>& lhs, const Zn<m>& rhs) {
Zn<m> copy = lhs;
return copy /= rhs;
}
template <int m>
Zn<m> operator/(const Zn<m>& lhs, int rhs) {
Zn<m> copy = lhs;
return copy /= rhs;
}
template <int m>
Zn<m> operator/(int lhs, const Zn<m>& rhs) {
return Zn<m>::valueOf(lhs) / rhs;
}
template <int m>
std::ostream& operator<<(std::ostream& stream, const Zn<m>& zn) {
return stream << zn.value;
}
template <int m>
std::istream& operator>>(std::istream& stream, Zn<m>& zn) {
long long value;
stream >> value;
impl__Normalizator::hard(zn.value, m);
zn.value = static_cast<int>(value);
return stream;
}
template <int m>
struct impl__IdentityHelper<Zn<m>> {
static Zn<m> identity() { return Zn<m>::valueOf(1); }
};
using namespace std;
class TaskE {
public:
using Z = Zn<1000000007>;
int values = 16;
int functions = 1 << values;
vector<Z> to_submask(vector<Z> input) {
auto dp = make_vector<Z>(values + 1, functions, Z());
dp[0] = input;
for (int i : range(values)) {
for (int j : range(functions)) {
if (j & (1 << i))
dp[i + 1][j] += dp[i][j];
else {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j ^ (1 << i)] += dp[i][j];
}
}
}
return dp.back();
}
vector<Z> to_supermask(vector<Z> input) {
auto dp = make_vector<Z>(values + 1, functions, Z());
dp[0] = input;
for (int i : range(values)) {
for (int j : range(functions)) {
if (!(j & (1 << i)))
dp[i + 1][j] += dp[i][j];
else {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j ^ (1 << i)] += dp[i][j];
}
}
}
return dp.back();
}
Z one = Z::valueOf(1);
vector<Z> from_submask(vector<Z> input) {
auto dp = make_vector<Z>(values + 1, functions, Z());
dp[0] = input;
for (int i : range(values)) {
for (int j : range(functions)) {
if (j & (1 << i))
dp[i + 1][j] += dp[i][j];
else {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j ^ (1 << i)] -= dp[i][j];
}
}
}
return dp.back();
}
vector<Z> from_supermask(vector<Z> input) {
auto dp = make_vector<Z>(values + 1, functions, Z());
dp[0] = input;
for (int i : range(values)) {
for (int j : range(functions)) {
if (!(j & (1 << i)))
dp[i + 1][j] += dp[i][j];
else {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j ^ (1 << i)] -= dp[i][j];
}
}
}
return dp.back();
}
string s;
int pos;
vector<int> lis = {
43690,
52428,
61680,
65280,
};
vector<Z> solve() {
if (s[pos] == '(') {
++pos;
vector<Z> l = solve();
++pos;
char op = s[pos];
++pos;
++pos;
vector<Z> r = solve();
++pos;
vector<Z> res(functions);
if (op == '?' || op == '|') {
auto L = to_submask(l);
auto R = to_submask(r);
for (int i : range(functions)) {
L[i] *= R[i];
}
L = from_submask(L);
for (int i : range(functions)) {
res[i] += L[i];
}
}
if (op == '?' || op == '&') {
auto L = to_supermask(l);
auto R = to_supermask(r);
for (int i : range(functions)) {
L[i] *= R[i];
}
L = from_supermask(L);
for (int i : range(functions)) {
res[i] += L[i];
}
}
return res;
} else {
vector<Z> res(functions);
for (int i : range(4)) {
int cur = lis[i];
if (s[pos] == '?' || s[pos] == 'A' + i) {
res[cur] += one;
}
if (s[pos] == '?' || s[pos] == 'a' + i) {
res[functions - 1 - cur] += one;
}
}
++pos;
return res;
}
}
void solve(std::istream& in, std::ostream& out) {
in >> s;
pos = 0;
int n;
in >> n;
auto res = solve();
vector<pair<int, int>> queries(n);
for (int i = 0; i < n; ++i) {
int a, b, c, d, r;
in >> a >> b >> c >> d >> r;
queries[i] = make_pair(a + 2 * b + 4 * c + 8 * d, r);
}
Z ans;
for (int i : range(functions)) {
bool ok = true;
for (auto q : queries) {
int val = (i >> q.first) & 1;
if (val != q.second) {
ok = false;
break;
}
}
if (ok) ans += res[i];
}
out << ans;
}
};
int main() {
std::ios_base::sync_with_stdio(false);
TaskE solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
in.tie(0);
out << std::fixed;
out.precision(20);
solver.solve(in, out);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5001;
int n, m, mod = 1e9 + 7;
vector<int> s, v[N];
int calc_inc(int g, int h, int j) {
if (g > h) swap(g, h);
int b = upper_bound(v[j].begin(), v[j].end(), h) - v[j].begin() - 1;
if (b == -1) return 0;
if (b == 0) return 1;
if (v[j][0] <= g) return 2;
return 1;
}
int calc_ways(int g, int h, int j) {
int inc = calc_inc(g, h, j);
if (inc == 0) return 1;
int l = upper_bound(v[j].begin(), v[j].end(), g) - v[j].begin();
int r = upper_bound(v[j].begin(), v[j].end(), h) - v[j].begin();
if (inc == 1) return (l + r) % mod;
return (1LL * l * r - min(l, r)) % mod;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
s.resize(n);
for (auto &i : s) cin >> i;
for (int i = 0; i < m; i++) {
int f, h;
cin >> f >> h;
v[f].push_back(h);
}
for (int i = 1; i <= n; i++) {
sort(v[i].begin(), v[i].end());
}
vector<int> pref(n + 1), suff(n + 1);
for (auto &i : s) suff[i]++;
vector<int> all;
int mx = 0;
for (int i = 0; i < n; i++) {
suff[s[i]]--;
pref[s[i]]++;
int b = lower_bound(v[s[i]].begin(), v[s[i]].end(), pref[s[i]]) -
v[s[i]].begin();
if (b == (int)v[s[i]].size() || pref[s[i]] != v[s[i]][b]) continue;
if (b > 0)
b = 0;
else
b++;
int ans = 1;
if (b < (int)v[s[i]].size() && v[s[i]][b] <= suff[s[i]]) ans++;
for (int j = 1; j <= n; j++) {
if (j == s[i]) continue;
ans += calc_inc(pref[j], suff[j], j);
}
if (ans == mx)
all.push_back(i);
else if (ans > mx) {
all.clear();
all.push_back(i);
mx = ans;
}
}
vector<int> h(n);
for (auto &i : all) h[i] = 1;
pref.assign(n + 1, 0);
suff.assign(n + 1, 0);
for (auto &i : s) suff[i]++;
int cnt = 0;
for (int i = 0; i < n; i++) {
suff[s[i]]--;
pref[s[i]]++;
if (h[i] == 0) continue;
int ans = 1;
int b = upper_bound(v[s[i]].begin(), v[s[i]].end(), suff[s[i]]) -
v[s[i]].begin() - 1;
if (b != -1) {
if (pref[s[i]] <= suff[s[i]])
ans = b;
else
ans = b + 1;
}
ans = max(ans, 1);
for (int j = 1; j <= n; j++) {
if (j == s[i]) continue;
ans = 1LL * ans * calc_ways(pref[j], suff[j], j) % mod;
}
cnt = (cnt + ans) % mod;
}
int g = 0;
for (int i = 1; i <= n; i++) {
g += calc_inc(0, pref[i], i);
}
if (g == mx) {
int cur = 1;
for (int i = 1; i <= n; i++) {
cur = 1LL * cur * calc_ways(0, pref[i], i) % mod;
}
cnt = (cnt + cur) % mod;
}
cout << mx << " " << cnt << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
const int n = 8;
string chess[n];
char chs[n][n];
int bi = 0, wi = 0, b = 8, w = 8;
for (int i = 0; i < n; i++) {
cin >> chess[i];
}
for (int i = 0; i < n; i++) {
wi = 0;
for (int j = 0; j < n; j++) {
if (chess[j][i] == 'B') break;
if (chess[j][i] == 'W') {
wi = j;
break;
}
}
if (w > wi && wi != 0) w = wi;
}
for (int i = 0; i < n; i++) {
for (int j = n - 1; j >= 0; j--) {
if (chess[j][i] == 'W') break;
if (chess[j][i] == 'B') {
bi = n - 1 - j;
break;
}
}
if (b > bi && bi != 0) b = bi;
}
if (w <= b)
cout << 'A';
else
cout << 'B';
return 0;
}
| 1 |
#include <iostream>
using namespace std;
int P[10010];
void init(int N);
int root(int a);
bool is_same_set(int a,int b);
void unite(int a,int b);
int main() {
int N,Q;
cin >> N >> Q;
init(N);
for (int i=0;i<Q;++i) {
int com,x,y;
cin >> com >> x >> y;
if (com==0)
unite(x,y);
else {
if (is_same_set(x,y)==true)
cout << 1 << endl;
else
cout << 0 << endl;
}
}
}
void init(int N) {
for (int i=0;i<=N;++i) P[i] = i;
}
int root(int a) {
if (P[a] == a) return a;
return (P[a] = root(P[a]));
}
bool is_same_set(int a,int b) {
return root(a)==root(b);
}
void unite(int a,int b) {
P[root(a)] = root(b);
} | 0 |
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string s;
while(getline(cin, s)) {
for (int i=0; i<26; i++) {
string s2(s);
for (int j=0; j<s.length(); j++) {
if (isalpha(s[j]))
s2[j] = (s[j]-'a'+i) % 26 + 'a';
}
if (s2.find("the")!=-1||s2.find("this")!=-1||s2.find("that")!=-1) {
cout << s2 << endl;
break;
}
}
}
} | 0 |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define N 5050
const int mod=1e9+7;
inline int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return x*f;
}
int p[N],n,s[N],vis[N],tot,nd,dp[N],ans,fac[N],pw[N];
vector<int> G[N];
#define ck(x) (x>=mod?x-mod:x)
void dfs(int u){
vis[u]=1;
++s[tot];
for(auto v:G[u]){
++nd;
if(!vis[v])dfs(v);
}
}
int main(){
n=read();
for(int i=1;i<=n;++i){
p[i]=read();
if(~p[i]){
G[p[i]].push_back(i);
G[i].push_back(p[i]);
}
}
pw[0]=fac[0]=1;
for(int i=1;i<=n;++i){
pw[i]=1LL*pw[i-1]*(n-1)%mod;
fac[i]=1LL*fac[i-1]*i%mod;
}
int tmp=n;
for(int i=1;i<=n;++i){
if(!vis[i]){
++tot;nd=0;
dfs(i);
if(nd==(s[tot]<<1)){
s[tot]=0;
--tmp,--tot;
}
}
}
ans=1LL*tmp*pw[tot]%mod;
dp[0]=1;
for(int i=1;i<=tot;++i){
for(int j=i;j>=1;--j){
dp[j]=(dp[j]+1LL*dp[j-1]*s[i]%mod)%mod;
}
}
for(int i=2;i<=tot;++i){
ans=(ans-1LL*dp[i]*pw[tot-i]%mod*fac[i-1]%mod+mod)%mod;
}
for(int i=1;i<=tot;++i){
ans=(ans-1LL*(s[i]-1)*pw[tot-1]%mod+mod)%mod;
}
printf("%d\n",ans);
return 0;
}
| 0 |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <utility>
#include <functional>
using namespace std;
typedef pair<int, int> P;
int main() {
int n;
while (cin >> n && n != 0) {
P s[100010];
int go, k = 1, ans = 0;
for (int i = 1; i <= n; ++i) {
cin >> go;
if (i == 1) {
s[k++] = P(i, go);
continue;
}
if (s[k - 1].second != go) {
if (i % 2) {//?\???°
s[k++] = P(i, go);
}
else {
if (k == 2)
s[k - 1].second = go;
else
k--;
}
}
}
s[k] = P(n + 1, 0);
for (int i = 1; i < k; ++i) {
if (s[i].second == 0)
ans += s[i + 1].first - s[i].first;
}
cout << ans << endl;
}
return 0;
}
/*
20
0
1
1
0
1
0
0
0
0
0
1
1
1
1
1
0
1
0
0
1
*/ | 0 |
#include <bits/stdc++.h>
#define de(x) cout<<#x<<"="<<x<<endl
#define dd(x) cout<<#x<<"="<<x<<" "
using namespace std;
#define int long long
const int MOD = 1000000007;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, X, D;
cin >> N >> X >> D;
if (D == 0) {
if (X == 0) {
cout << 1 << endl;
} else {
cout << 1 + N << endl;
}
return 0;
}
if (D < 0) {
X = -X;
D = -D;
}
map<int, int> R;
int mn = 0;
int mx = 0;
int res = 0;
vector<pair<int, pair<int, int> > > vp;
vp.emplace_back(0, make_pair(0, 0));
for (int i = 1; i <= N; i++) {
int sum = X * i;
mn += i - 1;
mx += N - i;
int ll = sum / D;
// if (D * ll > sum) {
// ll--;
// }
int md = sum - D * ll;
int l = ll + mn;
int r = ll + mx;
// dd(sum); dd(ll); dd(md); dd(l); de(r);
vp.emplace_back(md, make_pair(l, r));
}
sort(vp.begin(), vp.end());
for (int i = 0; i < vp.size(); i++) {
//cerr << i << " " << l << " " << r << endl;
int md = vp[i].first;
int l = vp[i].second.first;
int r = vp[i].second.second;
int t;
// dd(md);dd(l);de(r);
if (R.count(md) == 0) {
t = -((int)1 << 60);
} else {
t = R[md];
}
if (t >= l) {
l = t + 1;
}
if (t < r) {
res += r - l + 1;
R[md] = r;
}
}
cout << res << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int y, w;
cin >> y >> w;
int ma = max(y, w);
double re = (6 - ma) + 1;
if (re == 6)
cout << "1/1";
else if (re == 5)
cout << "5/6";
else if (re == 4)
cout << "2/3";
else if (re == 3)
cout << "1/2";
else if (re == 2)
cout << "1/3";
else if (re == 1)
cout << "1/6";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main () {
int H,W;
cin >> H >> W;
for (int i=0; i<H; ++i) {
for (int j=0; j<W; ++j) {
string s;
cin >> s;
if( s == "snuke")
printf("%c%d\n",'A'+j,i+1);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::make_pair;
using std::max;
using std::min;
using std::pair;
using std::string;
using std::vector;
template <typename T>
inline T input() {
T ans;
cin >> ans;
return ans;
}
int main() {
uint32_t n = input<uint32_t>();
uint32_t k = input<uint32_t>() - 1;
vector<uint32_t> data(n);
uint32_t l = 0;
uint32_t r = n - 1;
uint32_t cur_pow = 1 << (n - 1);
for (uint32_t i = 1; i <= n; i++) {
if (k < cur_pow / 2)
data[l++] = i;
else {
data[r--] = i;
k -= cur_pow / 2;
}
cur_pow /= 2;
}
for (uint32_t i = 0; i < n; ++i) cout << data[i] << " ";
endl(cout);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, i, sum = 0, c = 0;
cin >> n;
for (i = 0; i < n; i++) {
cin >> x;
sum += x;
}
n++;
for (i = 1; i <= 5; i++) {
if ((sum + i) % n != 1) c++;
}
cout << c << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
int m, n, res, a, b, mas[50][50], i, j, k, l, min = 100000, s = 0;
cin >> n >> m;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
cin >> mas[i][j];
}
}
cin >> a >> b;
for (i = 0; i <= n - a; i++) {
for (j = 0; j <= m - b; j++) {
s = 0;
for (k = i; k < i + a; k++) {
for (l = j; l < j + b; l++) {
if (mas[k][l] == 1) {
s++;
}
}
}
if (s < min) min = s;
}
}
k = a;
a = b;
b = k;
for (i = 0; i <= n - a; i++) {
for (j = 0; j <= m - b; j++) {
s = 0;
for (k = i; k < i + a; k++) {
for (l = j; l < j + b; l++) {
if (mas[k][l] == 1) {
s++;
}
}
}
if (s < min) min = s;
}
}
cout << min;
return EXIT_SUCCESS;
}
| 2 |
#include<iostream>
using namespace std;
int dp[100001];
int main() {
int n, q;
string s;
cin >> n >> q >> s;
for (int i = 1; i <= n; i++)
dp[i] = dp[i - 1] + s[i - 1] - 'a' + 1;
while (q--) {
int l, r;
cin >> l >> r;
cout << dp[r] - dp[l - 1] << '\n';
}
} | 2 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 5000009;
char s[MaxN], t[MaxN];
int p[MaxN], q[MaxN], r[MaxN], z[MaxN];
int n;
int main(void) {
int i, j, k, res;
while (scanf(" %s", s + 1) != EOF) {
s[0] = t[0] = '\0';
n = strlen(s + 1);
for (i = 1; i <= n; i++) t[n - i + 1] = s[i];
t[n + 1] = '\xFF';
k = -1;
p[0] = k;
for (i = 1; i <= n; i++) {
while (k >= 0 && s[k + 1] != s[i]) k = p[k];
k++;
p[i] = k;
}
k = 0;
q[0] = k;
for (i = 1; i <= n; i++) {
while (k >= 0 && s[k + 1] != t[i]) k = p[k];
k++;
q[i] = k;
}
memset(z, 0, sizeof(z));
k = 0;
i = 1;
while (i <= n) {
if (k == -1) k++;
while (t[i + k] == s[k + 1]) k++;
z[i] = k;
i += k - p[k];
k = p[k];
}
memset(r, 0, sizeof(r));
res = 0;
k = 0;
for (i = 1; i <= n; i++) {
j = i >> 1;
if (z[n - i + 1] >= j) r[i] = 1 + r[j];
}
for (i = 1; i <= n; i++) res += r[i];
printf("%d\n", res);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
void gSr(int n) {
int magicSquare[n][n];
memset(magicSquare, 0, sizeof(magicSquare));
int i = n / 2;
int j = n - 1;
int num;
for (num = 1; num <= n * n;) {
if (i == -1 && j == n) {
j = n - 2;
i = 0;
} else {
if (j == n) j = 0;
if (i < 0) i = n - 1;
}
if (magicSquare[i][j]) {
j -= 2;
i++;
continue;
} else
magicSquare[i][j] = num++;
j++;
i--;
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (j) printf(" ");
printf("%d", magicSquare[i][j]);
}
printf("\n");
}
}
int main() {
int n;
scanf("%d", &n);
gSr(n);
return 0;
}
| 3 |
#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
//const int INF = 1e8;
using namespace std;
class RollingHash {
public:
typedef pair<long long,long long> hash_type;
long long base1, base2;
long long mod1, mod2;
vector<long long> hash1, hash2;
vector<long long> pow1, pow2;
RollingHash(const string &s) : base1(1009), base2(1007), mod1(1000000007), mod2(1000000009) {
int n = s.size();
hash1.assign(n + 1,0);
hash2.assign(n + 1,0);
pow1.assign(n + 1,1);
pow2.assign(n + 1,1);
rep(i,n){
hash1[i + 1] = (hash1[i] + s[i]) * base1 % mod1;
hash2[i + 1] = (hash2[i] + s[i]) * base2 % mod2;
pow1[i + 1] = pow1[i] * base1 % mod1;
pow2[i + 1] = pow2[i] * base2 % mod2;
}
}
hash_type get(int l, int r) { // 区間[l,r)のハッシュ値を計算する
long long t1 = ((hash1[r] - hash1[l] * pow1[r - l]) % mod1 + mod1) % mod1;
long long t2 = ((hash2[r] - hash2[l] * pow2[r - l]) % mod2 + mod2) % mod2;
return make_pair(t1, t2);
}
hash_type concat(hash_type h1, hash_type h2, int h2_len) {
return make_pair((h1.first * pow1[h2_len] + h2.first) % mod1, (h1.second * pow2[h2_len] + h2.second) % mod2);
}
};
int main(){
int n, m;
string s;
cin >> n >> m >> s;
int l = 0, r = 1;
vector<pair<int, int>> p(m);
rep(i,m){
string t;
cin >> t;
if(t[0] == 'R'){
if(t[1] == '+'){
r++;
}else{
r--;
}
}else{
if(t[1] == '+'){
l++;
}else{
l--;
}
}
p[i] = make_pair(l,r);
}
set<RollingHash::hash_type> st;
RollingHash rh(s);
for(auto i : p){
tie(l,r) = i;
st.emplace(rh.get(l, r));
}
cout << st.size() << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long fac[1000006];
inline long long exp(long long x, long long n) {
long long r = 1;
x = x % 1000000007;
while (n) {
if (n % 2) r = (r * x) % 1000000007;
x = (x * x) % 1000000007;
n = n / 2;
}
return r;
}
inline long long mmi(long long a) {
return exp(a, 1000000007 - 2) % 1000000007;
}
inline long long fact(long long n) {
long long res = 1;
for (long long i = 1; i < (n + 1); ++i) {
res = (res * i) % 1000000007;
}
return res;
}
inline void fact_a() {
fac[0] = 1;
fac[1] = 1;
for (long long i = 1; i < (1000006); ++i) {
fac[i] = (fac[i - 1] * i) % 1000000007;
}
}
inline long long inv_fact(long long n) {
fact_a();
long long par = fac[n];
long long res = mmi(par);
return res;
}
inline long long comb(long long n, long long r) {
fact_a();
if (n < r) return 0;
return ((fac[n] * inv_fact(r)) % 1000000007 * inv_fact(n - r)) % 1000000007;
}
struct triplet {
long long a, b, c;
};
bool operator<(const triplet &t1, const triplet &t2) {
if (t1.a < t2.a) return true;
if (t1.a == t2.a && t1.b < t2.b) return true;
if (t1.a == t2.a && t1.b == t2.b && t1.c < t2.c) return true;
return false;
}
pair<long long, pair<long long, long long> > ex_gcd(long long a, long long b) {
if (b == 0) {
return make_pair(a, make_pair(1, 0));
}
pair<long long, pair<long long, long long> > p = ex_gcd(b, a % b);
long long gcd = p.first;
long long x1 = p.second.first;
long long y1 = p.second.second;
long long x = y1;
long long y = x1 - (a / b) * y1;
return make_pair(gcd, make_pair(x, y));
}
long long prime[1000006];
long long spf_prime[1000006];
void sieve() {
for (long long i = 2; i * i <= 1000000; i++)
if (prime[i] == 0)
for (long long j = i * i; j <= 1000000; j += i) prime[j] = 1;
}
void spf() {
for (long long i = 2; i * i <= 1000000; i++)
if (!spf_prime[i])
for (long long j = i * i; j <= 1000000; j += i)
if (!spf_prime[j]) spf_prime[j] = i;
for (long long i = 2; i <= 1000000; i++)
if (!spf_prime[i]) spf_prime[i] = i;
}
long long getparent_BIT(long long idx) { return idx - (idx & -idx); }
long long getnext_BIT(long long idx) { return idx + (idx & -idx); }
long long getsum_BIT(long long idx, long long BIT[], long long n) {
long long sum = 0;
while (idx > 0) {
sum += BIT[idx];
idx = getparent_BIT(idx);
}
return sum;
}
void update_BIT(long long idx, long long BIT[], long long val, long long n) {
while (idx <= n) {
BIT[idx] += val;
idx = getnext_BIT(idx);
}
}
void build_BIT(long long BIT[], long long a[], long long n) {
for (long long i = 0; i < (n); ++i) {
update_BIT(i, BIT, a[i], n);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long q;
cin >> q;
while (q--) {
long long l, r;
cin >> l >> r;
long long cnt1 = 0, cnt2 = 0;
long long f = l;
long long s = r;
long long k = 0;
while (f > 0) {
f = f >> 1;
cnt1++;
}
while (s > 0) {
if (s & 1) k++;
s = s >> 1;
cnt2++;
}
vector<long long> v;
if (k == cnt2) {
cout << r << "\n";
continue;
}
if (cnt1 < cnt2) {
long long ans = cnt2 - 1;
v.resize(ans);
for (long long i = 0; i < (v.size()); ++i) v[i] = 1;
} else if (cnt1 == cnt2) {
vector<long long> p;
vector<long long> q;
f = l;
s = r;
while (f > 0) {
p.push_back(f % 2);
f = f >> 1;
}
while (s > 0) {
q.push_back(s % 2);
s = s >> 1;
}
reverse(p.begin(), p.end());
reverse(q.begin(), q.end());
v.resize(p.size());
for (long long i = 0; i < (p.size()); ++i) {
if (p[i] == q[i])
v[i] = p[i];
else {
long long cc = 0;
for (long long j = i + 1; j < (q.size()); ++j) {
if (q[j] == 0) {
cc = 1;
break;
}
}
if (cc == 0) {
for (long long j = i; j < (q.size()); ++j) v[j] = q[j];
} else {
v[i] = 0;
for (long long j = i + 1; j < (q.size()); ++j) v[j] = 1;
}
break;
}
}
}
long long ans = 0;
for (long long i = 0; i < (v.size()); ++i) {
ans = ans * 2 + v[i];
}
cout << ans << "\n";
}
}
| 3 |
#include <iostream>
#include <string>
#include <deque>
#include <algorithm>
#include <cctype>
using namespace std;
int main(){
int n, m;
for(cin >> n; n--; ){
string cmd, msgstr;
cin >> cmd >> msgstr;
deque<char> msg(msgstr.begin(), msgstr.end());
deque<char>::iterator it1, it2;
for(int i = cmd.size() - 1; i >= 0; --i){
switch(cmd[i]){
case 'J':
msg.push_front( msg.back() );
msg.pop_back();
break;
case 'C':
msg.push_back( msg.front() );
msg.pop_front();
break;
case 'E':
m = msg.size() / 2;
it1 = msg.begin();
it2 = msg.end() - m;
while( m-- ){
swap( *it1++, *it2++ );
}
break;
case 'A':
reverse(msg.begin(), msg.end());
break;
case 'P':
for(it1 = msg.begin(); it1 != msg.end(); ++it1){
if( isdigit(*it1) ){
*it1 = (*it1 == '0' ? '9' : *it1 - 1);
}
}
break;
case 'M':
for(it1 = msg.begin(); it1 != msg.end(); ++it1){
if( isdigit(*it1) ){
*it1 = (*it1 == '9' ? '0' : *it1 + 1);
}
}
break;
}
}
cout << string(msg.begin(), msg.end()) << '\n';
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << n % 2;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, d;
int a[N], v[N * 12], cnt[N];
int main() {
scanf("%d%d%d", &n, &m, &d);
for (int i = 1; i <= m; i++) {
int x;
scanf("%d", &x);
while (x--) {
int y;
scanf("%d", &y);
a[y] = i;
}
}
int p = 0, k = 1 << m, ans = m + 1;
for (int i = 0; i < k; i++) v[i] = 1;
for (int i = 1; i <= n; i++) {
if (i >= d) {
cnt[a[i - d]]--;
if (cnt[a[i - d]] == 0) p ^= 1 << (a[i - d] - 1);
}
cnt[a[i]]++;
if (cnt[a[i]] == 1) p ^= 1 << (a[i] - 1);
if (i >= d) v[k - 1 - p] = 0;
}
for (int i = k - 1; i >= 0; i--)
if (!v[i]) {
for (int j = 0; j < m; j++)
if ((i | (1 << j)) == i) v[i - (1 << j)] = 0;
} else {
int tmp = 0;
for (int j = 0; j < m; j++)
if ((i | (1 << j)) == i) tmp++;
ans = min(ans, tmp);
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, k, m, u[2000], v[2000], c[2000];
vector<pair<int, int> > G[82];
int INF = (1 << 28);
int cache[82][82][82][82];
int rec(int now, int left, int right, int reftk) {
int ans;
if (cache[now + 1][left + 1][right + 1][reftk + 1] != -1) {
return cache[now + 1][left + 1][right + 1][reftk + 1];
} else if (reftk == 0) {
ans = 0;
} else {
ans = INF;
for (int i = (0); i < (int)(((int)G[now].size())); ++i)
if (left < G[now][i].first && G[now][i].first < right) {
if (G[now][i].first < now) {
ans =
min(ans, G[now][i].second + rec(G[now][i].first, G[now][i].first,
now, reftk - 1));
ans = min(ans, G[now][i].second + rec(G[now][i].first, left,
G[now][i].first, reftk - 1));
} else {
ans = min(ans, G[now][i].second + rec(G[now][i].first, now,
G[now][i].first, reftk - 1));
ans =
min(ans, G[now][i].second + rec(G[now][i].first, G[now][i].first,
right, reftk - 1));
}
}
}
return cache[now + 1][left + 1][right + 1][reftk + 1] = ans;
}
int main(void) {
memset(cache, -1, sizeof(cache));
cin >> n >> k >> m;
for (int i = (0); i < (int)(m); ++i) cin >> u[i] >> v[i] >> c[i];
for (int i = (0); i < (int)(m); ++i) {
G[u[i] - 1].push_back(make_pair(v[i] - 1, c[i]));
}
int ans = INF;
for (int i = (0); i < (int)(n); ++i) ans = min(ans, rec(i, -1, n, k - 1));
if (ans == INF)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long a, b;
scanf("%lld%lld", &a, &b);
if (a == b) return 0 * puts("1");
long long ans = 1;
if (b - a < 20) {
for (long long i = a + 1; i <= b; i++) {
long long t = i % 10;
ans = 1ll * ans * t % 10;
}
} else
ans = 0;
printf("%lld\n", ans);
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int H,W;
char F[555][555];
char R[555][555];
char B[555][555];
int main(){
cin >> H >> W;
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
cin >> F[j][i];
R[j][i] = F[j][i];
B[j][i] = F[j][i];
}
}
for(int i=0;i<H;i++){
if( i & 1 )
for(int j=0;j<W-1;j++)
R[j][i] = '#';
else
for(int j=1;j<W;j++)
B[j][i] = '#';
R[0][i] = '#';
B[W-1][i] = '#';
}
for(int i=0;i<H;i++){
for(int j=0;j<W;j++) cout << R[j][i];
cout << endl;
}
cout << endl;
for(int i=0;i<H;i++){
for(int j=0;j<W;j++) cout << B[j][i];
cout << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void func(void) {
freopen("input.c", "r", stdin);
freopen("output.c", "w", stdout);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, q, i, j = 0, temp, t, k, ans = 0, sum = 0, cnt = 0, m, fg = 0, x,
y, mx = 0, mx1 = 0, mn = 10000000000000000,
mn1 = 10000000000000000;
map<long long, long long> a;
cin >> n;
while (n--) {
cin >> x;
a[x]++;
}
auto it = a.begin();
for (; it != a.end(); it++) {
mx = max(mx, it->first);
cnt += it->second % 2;
k = it->second / 2;
if (k) {
a[it->first + 1] += k;
}
}
cout << mx + 1 - cnt << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, q;
vector<int> a, k;
int last(int x) {
int cnt = 0;
for (auto y : a)
if (y <= x) cnt++;
for (auto y : k) {
if (y > 0 and y <= x) cnt++;
if (y < 0 and abs(y) <= cnt) cnt--;
}
return cnt;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> q;
a.resize(n);
k.resize(q);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < q; i++) cin >> k[i];
if (last(1e9) == 0) {
cout << 0;
return 0;
}
int lo = 0;
int hi = int(1e6) + 1;
while (hi - lo > 1) {
int mid = (lo + hi) / 2;
if (last(mid) > 0) {
hi = mid;
} else {
lo = mid;
}
}
cout << hi;
return 0;
}
| 4 |
#include<stdio.h>
int main(void)
{
int i,h,k,a[3],b[3],c[5];
for(i=0;i<2;i++) scanf("%d %d",&a[i],&b[i]);
for(i=0;i<4;i++) scanf("%d",&c[i]);
h=a[0]/10*c[2]+a[0]*c[0];
k=a[1]/10*c[2]+a[1]*c[0];
h=h+b[0]/20*c[3]+b[0]*c[1];
k=k+b[1]/20*c[3]+b[1]*c[1];
if(h>k) printf("hiroshi\n");
else if(h==k) printf("even\n");
else printf("kenjiro\n");
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
int i, j, k, n, m, T;
double ax, ay, va, xu, yu, vu, r, R, adis, bdis, ao, bo, v, l, rr, mid, b, d,
bx, by;
double check(double ti) {
b = atan2(xu, yu) - ti * v;
bx = sin(b) * R;
by = cos(b) * R;
d = atan2(ax, ay) - atan2(bx, by);
if (d < 0) d = -d;
if (d > pi) d = 2 * pi - d;
if (ao + bo >= d)
return (sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by)));
return (adis + bdis + r * (d - ao - bo));
}
int main() {
scanf("%lf%lf%lf%lf%lf%lf%lf", &xu, &yu, &vu, &ax, &ay, &va, &r);
R = sqrt(xu * xu + yu * yu);
adis = sqrt(ax * ax + ay * ay - r * r);
bdis = sqrt(R * R - r * r);
ao = atan(adis / r);
bo = pi / 2 - asin(r / R);
v = vu / R;
l = 0;
rr = 1e10;
while (l + 1e-8 <= rr) {
mid = (l + rr) / 2;
if (check(mid) / va <= mid)
rr = mid;
else
l = mid;
}
printf("%.6lf\n", mid);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e5 + 9;
const int INF = 1e9 + 7;
long long power(long long x, long long y) {
if (y == 0) return 1;
long long temp = power(x, y / 2);
if (y % 2 == 1)
return temp * temp * x;
else
return temp * temp;
}
int n, m, x, y, ans = 0;
int a[N];
vector<int> adj[N];
bool vis[N];
void dfs(int u, int parent, int cnt) {
if (a[u] == 1)
cnt++;
else
cnt = 0;
if (cnt == m) {
return;
}
if (adj[u].size() == 1 && u != 1) {
ans++;
}
for (auto v : adj[u]) {
if (v == parent) continue;
dfs(v, u, cnt);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for (int i = (1); i <= (n); ++i) {
vis[i] = false;
cin >> a[i];
}
m++;
for (int i = (1); i <= (n - 1); ++i) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1, 0, 0);
cout << ans << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long int arr[100050], dp[100050];
int main() {
double n, k;
cin >> n >> k;
double sub = 0;
for (int i = 1; i < n; i++) {
sub += pow((i * 1.0) / n, k);
}
cout << setprecision(7) << n - sub << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
bool possible[100101], occor[100001];
char cmd[100001];
int his[100001];
bool in[100001];
int main() {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> cmd[i] >> his[i];
}
int cnt = 0;
for (int i = 0; i < m; i++) {
if (cmd[i] == '-') {
if (!in[his[i]]) {
cnt++;
possible[his[i]] = true;
}
} else {
in[his[i]] = true;
}
occor[his[i]] = true;
}
if (cmd[0] == '+') {
if (cnt == 0) possible[his[0]] = true;
cnt++;
} else {
cnt--;
int i = 0;
if (cnt != 0) {
possible[his[i]] = false;
} else if (i + 1 < m && (cmd[i + 1] != '+' || his[i + 1] != his[i])) {
possible[his[i]] = false;
}
}
for (int i = 1; i < m; i++) {
if (cmd[i] == '+') {
cnt++;
} else {
cnt--;
if (cnt != 0) {
possible[his[i]] = false;
} else if (i + 1 < m && (cmd[i + 1] != '+' || his[i + 1] != his[i])) {
possible[his[i]] = false;
}
}
}
vector<int> ans;
for (int i = 1; i <= n; i++) {
if (possible[i] || !occor[i]) ans.push_back(i);
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
}
| 3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.