solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int MOD;
struct mint {
int n;
mint(int n_ = 0) : n(n_) {}
};
mint operator-(mint a) { return -a.n + MOD * (a.n != 0); }
mint operator+(mint a, mint b) {
int x = a.n + b.n;
return x - (x >= MOD) * MOD;
}
mint operator-(mint a, mint b) {
int x = a.n - b.n;
return x + (x < 0) * MOD;
}
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
istream &operator>>(istream &i, mint &a) { return i >> a.n; }
ostream &operator<<(ostream &o, mint a) { return o << a.n; }
bool operator<(mint a, mint b) { return a.n < b.n; }
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
mint k;
cin >> n >> MOD >> k;
map<mint, ll> mp;
ll ans = 0;
for (int i = 0; i < (n); i++) {
mint x;
cin >> x;
ans += mp[x * x * x * x - k * x]++;
}
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1001;
string s;
char c;
int n, mx = -1, k = -1;
vector<string> a;
int main() {
while (getline(cin, s)) {
int n = s.size();
mx = max(mx, n);
a.push_back(s);
}
for (int i = 0; i < mx + 2; i++) cout << '*';
cout << "\n";
for (int i = 0; i < a.size(); i++) {
cout << '*';
int len = mx - a[i].size();
if (len % 2 == 0) {
for (int j = 0; j < len / 2; j++) cout << " ";
cout << a[i];
for (int j = 0; j < len / 2; j++) cout << " ";
} else {
k++;
for (int j = 0; j < len / 2 + k % 2; j++) cout << " ";
cout << a[i];
for (int j = 0; j < len / 2 + 1 - k % 2; j++) cout << " ";
}
cout << "*\n";
}
for (int i = 0; i < mx + 2; i++) cout << '*';
cout << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
return x;
}
const int maxn = 2e5 + 10;
const int mod = 998244353;
const double eps = 1e-9;
long long f[51][51], g[51][51];
long long inv[3000];
long long qpow(int a, int b) {
if (b == 0) return 1;
long long now = qpow(a, b / 2);
now = now * now % mod;
if (b & 1) now = now * a % mod;
return now;
}
int like[51], cnt[2], w[51];
int main() {
inv[0] = inv[1] = 1;
for (int i = 2; i < 3000; ++i) inv[i] = qpow(i, mod - 2);
int n = read(), m = read();
for (int i = 1; i <= n; ++i) like[i] = read();
for (int i = 1; i <= n; ++i) {
w[i] = read();
cnt[like[i]] += w[i];
}
int sum = cnt[0] + cnt[1];
for (int j = 0; j <= m; ++j) {
f[j][m - j] = 1;
g[j][m - j] = 1;
}
for (int j = m - 1; j >= 0; --j) {
for (int k = m - j - 1; k >= 0; --k) {
f[j][k] = ((cnt[1] + j + 1) * inv[sum + j - k] % mod * f[j + 1][k] % mod +
(cnt[0] - k) * inv[sum + j - k] % mod * f[j][k + 1]) %
mod;
g[j][k] = ((cnt[1] + j) * inv[sum + j - k] % mod * g[j + 1][k] % mod +
(cnt[0] - k - 1) * inv[sum + j - k] % mod * g[j][k + 1]) %
mod;
}
}
for (int i = 1; i <= n; ++i) {
if (like[i])
printf("%lld\n", w[i] * f[0][0] % mod);
else
printf("%lld\n", w[i] * g[0][0] % mod);
}
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N,K,sum=0;
cin>>N>>K;
for(int i=0;i<N;i++){
int xi;
cin>>xi;
sum+=(xi<abs(K-xi)? xi*2 : abs(K-xi)*2);
}
cout<<sum<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long read() {
long long a = 0;
char c = getchar();
while (c > '9' || c < '0') c = getchar();
while ('0' <= c && c <= '9') {
a = a * 10 + c - 48;
c = getchar();
}
return a;
}
long long N;
long long mu[200005], phi[200005], p[200005 / 10], cnt;
bool vis[200005];
void init(long long n) {
mu[1] = phi[1] = 1;
for (long long i = 2; i <= n; ++i) {
if (!vis[i]) {
p[++cnt] = i;
mu[i] = 1000000007 - 1;
phi[i] = i - 1;
}
for (long long j = 1; p[j] * i <= n; ++j) {
vis[p[j] * i] = 1;
if (i % p[j] == 0) {
mu[i * p[j]] = 0;
phi[i * p[j]] = phi[i] * p[j];
break;
}
phi[i * p[j]] = phi[i] * (p[j] - 1);
mu[i * p[j]] = 1000000007 - mu[i];
}
}
}
long long n, siz[200005], fa[200005], dep[200005], w[200005], tot, rt;
long long a[200005], inv[200005], val[200005], app[200005];
vector<long long> d[200005];
vector<long long> edge[200005];
void dfs(long long x) {
siz[x] = 1;
w[x] = 0;
for (long long i = 0; i < edge[x].size(); ++i) {
long long v = edge[x][i];
if (fa[x] == v || app[v]) continue;
fa[v] = x;
dep[v] = dep[x] + 1;
dfs(v);
siz[x] += siz[v];
if (siz[w[x]] < siz[v]) w[x] = v;
}
if (max(siz[w[x]], N - siz[x]) <= N / 2) rt = x;
}
long long sum[200005], sumv[200005], ans;
vector<long long> rub;
void DFS1(long long x) {
long long V = a[x];
for (long long i = 0; i < d[V].size(); ++i) {
long long t = d[V][i];
ans = (ans + val[t] * sum[t] % 1000000007 * phi[V] % 1000000007 * dep[x]) %
1000000007;
ans = (ans + val[t] * sumv[t] % 1000000007 * phi[V]) % 1000000007;
rub.push_back(t);
}
for (long long i = 0; i < edge[x].size(); ++i) {
long long v = edge[x][i];
if (fa[x] == v || app[v]) continue;
DFS1(v);
}
}
void DFS2(long long x) {
long long V = a[x];
for (long long i = 0; i < d[V].size(); ++i) {
long long t = d[V][i];
sum[t] = (sum[t] + phi[V]) % 1000000007;
sumv[t] = (sumv[t] + phi[V] * dep[x]) % 1000000007;
}
for (long long i = 0; i < edge[x].size(); ++i) {
long long v = edge[x][i];
if (fa[x] == v || app[v]) continue;
DFS2(v);
}
}
void work(long long x, long long tot) {
N = tot;
fa[x] = 0;
dep[x] = 0;
rt = 0;
dfs(x);
long long tmp = rt;
fa[rt] = 0;
dep[rt] = 0;
dfs(rt);
rt = tmp;
long long V = a[rt];
for (long long i = 0; i < d[V].size(); ++i) {
long long t = d[V][i];
rub.push_back(t);
sum[t] = (sum[t] + phi[V]) % 1000000007;
}
for (long long i = 0; i < edge[rt].size(); ++i) {
long long v = edge[rt][i];
if (app[v]) continue;
DFS1(v);
DFS2(v);
}
for (long long i = 0; i < rub.size(); ++i) sum[rub[i]] = sumv[rub[i]] = 0;
rub.clear();
app[rt] = 1;
for (long long i = 0; i < edge[tmp].size(); ++i) {
long long v = edge[tmp][i];
if (app[v]) continue;
work(v, siz[v]);
}
}
signed main() {
n = read();
init(n);
inv[1] = 1;
for (long long i = 2; i <= n; ++i)
inv[i] = (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007;
for (long long i = 1; i <= n; ++i)
for (long long j = i; j <= n; j += i)
val[j] = (val[j] + i * inv[phi[i]] % 1000000007 * mu[j / i]) % 1000000007;
for (long long i = 1; i <= n; ++i) {
a[i] = read();
}
for (long long i = 1; i <= n; ++i)
for (long long j = i; j <= n; j += i) d[j].push_back(i);
for (long long i = 1; i < n; ++i) {
long long u = read(), v = read();
edge[u].push_back(v);
edge[v].push_back(u);
}
work(1, n);
printf("%lld\n", ans * 2 * inv[n] % 1000000007 * inv[n - 1] % 1000000007);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL<<60;
int N;
vector<int> A;
vector<vector<int>> G;
vector<ll> dp;
vector<int> res;
int ma;
void f(int p, int q) {
ll x = A[p];
auto y = (int)(lower_bound(dp.begin(), dp.end(), x) - dp.begin());
auto tmp_ma = ma;
ma = max(ma, y);
res[p] = ma;
auto tmp = dp[y];
dp[y] = x;
for (int v : G[p]) if (v != q) {
f(v, p);
}
dp[y] = tmp;
ma = tmp_ma;
}
int main() {
cin >> N;
A.resize(N);
for (auto& x : A) cin >> x;
G.resize(N);
for (int i = 0; i < N-1; ++ i) {
int u, v;
cin >> u >> v;
-- u; -- v;
G[u].push_back(v);
G[v].push_back(u);
}
res.resize(N);
dp.resize(N+10, INF);
dp[0] = -INF;
f(0, -1);
for (auto x : res) cout << x << endl;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
long long int po(long long int a, long long int b, long long int mod) {
if (b == 0) {
return 1;
}
long long int x = po(a, b / 2, mod);
x = (x * x) % mod;
if (b % 2) {
x = (x * a) % mod;
}
return x;
}
long long int modin(long long int a, long long int mod) {
return po(a, mod - 2, mod);
}
double n;
double p[20][20];
double dp[2000000];
void pri(int x) {
vector<int> v;
for (int i = 0; i < 6; i++) {
if (x & (1 << i)) {
v.push_back(1);
} else
v.push_back(0);
}
reverse(v.begin(), v.end());
for (auto x : v) cout << x << " ";
cout << '\n';
}
double rec(int i, int mask, int rem) {
int t = (mask & (1 << i));
if (t != 0) return 0.00;
if (rem == 1) return 1.000;
rem = double(rem * (1.0));
if (dp[mask] != -1.0) return dp[mask];
double &res = dp[mask];
res = 0.00;
double x = 2.0 / (rem * (rem - 1));
for (int j = 0; j < n; j++) {
int l = mask & 1 << j;
if (l == 0) {
for (int k = 0; k < n; k++) {
int pq = mask & 1 << k;
if (k != j && pq == 0) {
int temp = mask | 1 << k;
res += (p[j][k] * rec(i, temp, rem - 1) * x);
}
}
}
}
return res;
}
void in() {
for (int j = 0; j < 2000000; j++) dp[j] = -1.0;
}
void solve() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> p[i][j];
}
}
for (int i = 0; i < n; i++) {
in();
cout << fixed;
cout << setprecision(9);
cout << double(rec(i, 0, n * 1.0)) << " ";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
int p = 1;
while (TESTS--) {
solve();
p++;
}
return 0;
}
| 5 |
#include<bits/stdc++.h>
int n,r,i,j,a[100];
int main(){
scanf("%d",&n);
for(;i<n;i++)scanf("%d",&a[i]);
for(i=0;i<n;i++){
for(j=0;j<i;j++){
if(a[i]<a[j])r++;
}
}
std::sort(a,a+n);
for(int i=0;i<n;i++){if(i)printf(" ");printf("%d",a[i]);}
printf("\n%d\n",r);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
long long n;
cin >> n;
long long a[n];
long long count0 = 0, count5 = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0)
count0++;
else
count5++;
}
while (count5) {
long long check = (5 * count5);
if (check % 9 == 0)
break;
else {
count5--;
}
}
if (count0 == 0)
cout << "-1" << endl;
else if ((count5 > 0) && (count0 > 0)) {
for (long long i = 0; i < count5; i++) cout << "5";
for (long long i = 0; i < count0; i++) cout << "0";
} else
cout << "0" << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
struct node {
int v, x;
};
vector<node> g[100010];
int visit[100010], col[100010];
bool bfs(int i) {
queue<int> qu;
qu.push(i);
visit[i] = 1;
col[i] = 1;
while (!qu.empty()) {
int top = qu.front();
qu.pop();
for (int j = 0; j < g[top].size(); j++) {
int v = g[top][j].v;
int x = g[top][j].x;
if (col[v] == -1) {
if (x == 0)
col[v] = !col[top];
else
col[v] = col[top];
} else {
if (x == 0 && col[v] == col[top]) return false;
if (x == 1 && col[v] == !col[top]) return false;
}
if (!visit[v]) {
qu.push(v);
}
visit[v] = 1;
}
}
return true;
}
int main() {
int n, m, a, b, k;
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) g[i].clear();
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &k);
node tem;
tem.x = k, tem.v = b;
g[a].push_back(tem);
tem.x = k, tem.v = a;
g[b].push_back(tem);
}
memset(visit, 0, sizeof(visit));
memset(col, -1, sizeof(col));
long long ans = (mod + 1) / 2;
for (int i = 1; i <= n; i++) {
if (!visit[i]) {
if (bfs(i))
ans = (ans * 2) % mod;
else {
ans = 0;
break;
}
}
}
printf("%lld\n", ans);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie();
int n;
cin >> n;
char c[n];
int c1 = 0, c2 = 0;
for (int i = 0; i < n; i++) {
cin >> c[i];
}
for (int i = 0; i < n; i++) {
if ((c[i] - '0') != i % 2) c1++;
if ((c[i] - '0') != (i + 1) % 2) c2++;
}
cout << min(c1, c2) << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
long long l, r;
cin >> l >> r;
long long ans = (1LL << 60) - 1;
for (int i = 59; i >= 0; i--) {
if (ans >= l && ans <= r) break;
if ((ans ^ (1LL << i)) < l) continue;
ans ^= (1LL << i);
}
cout << ans << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int col[1001];
vector<int> vs[1001], edge[1001];
struct UnionFind {
int par[1001], rank[1001];
void init() {
for (int i = 0; i < 1001; i++) {
par[i] = i;
rank[i] = 0;
}
}
int find(int n) {
if (n == par[n])
return n;
else
return par[n] = find(par[n]);
}
void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (rank[a] < rank[b]) {
par[a] = b;
} else {
par[b] = a;
if (rank[a] == rank[b]) rank[a]++;
}
}
} t;
int calc(int v) {
int ret = 0;
for (int i = 0; i < vs[v].size(); i++) {
int dist[1001];
bool u[1001] = {};
fill(dist, dist + 1001, 1000000000);
queue<int> que;
que.push(vs[v][i]);
dist[vs[v][i]] = 0;
while (!que.empty()) {
int c = que.front();
que.pop();
if (u[c]) continue;
u[c] = 1;
for (int j = 0; j < edge[c].size(); j++) {
if (dist[edge[c][j]] > dist[c] + 1) {
dist[edge[c][j]] = dist[c] + 1;
que.push(edge[c][j]);
}
}
}
for (int i = 0; i < 1001; i++)
if (dist[i] < 500000) ret = max(ret, dist[i]);
}
return ret;
}
void coloring(int v, int c) {
col[v] = c;
for (int i = 0; i < edge[v].size(); i++) {
if (col[edge[v][i]] == -1) {
coloring(edge[v][i], 1 - c);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> m;
memset(col, -1, sizeof(col));
t.init();
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
edge[u].push_back(v);
edge[v].push_back(u);
t.unite(u, v);
}
for (int i = 1; i <= n; i++) {
if (col[i] == -1) coloring(i, 0);
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < edge[i].size(); j++) {
if (col[i] == col[edge[i][j]]) {
cout << -1 << endl;
return 0;
}
}
}
int ans = 0;
for (int i = 1; i <= n; i++) vs[t.find(i)].push_back(i);
for (int i = 1; i <= n; i++) {
if (t.find(i) == i) {
ans += calc(i);
}
}
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000 * 1000 * 1000;
vector<string> pref;
vector<string> st;
int n, m;
int dp[1 << 10][11][1 << 7];
int fp[1 << 7][4];
const int MOD = inf + 9;
const string s = "ACGT";
int findp(int p, int c) {
if (fp[p][c] == inf) {
fp[p][c] = -1;
int res = -1;
string ss = pref[p] + s[c];
for (int i = 0; i < pref.size(); ++i)
if (pref[i].size() <= ss.size())
if (ss.substr(ss.size() - pref[i].size(), pref[i].size()) == pref[i])
if (res < (int)pref[i].size()) res = pref[i].size(), fp[p][c] = i;
if (res == 0) fp[p][c] = -1;
}
return fp[p][c];
}
int ff(int k, int p) {
int res = k;
for (int i = 0; i < (int)pref[p].size() - k + 1; ++i)
for (int j = 0; j < st.size(); ++j)
if (st[j].size() + i <= pref[p].size())
if (pref[p].substr(i, st[j].size()) == st[j])
res = min(res, (int)pref[p].size() - i - (int)st[j].size());
return res;
}
int f(int pos, int k, int p) {
if (pos > n) return 0;
if (pos == n) return k == 0;
if (k > pref[p].size()) return 0;
if (dp[pos][k][p] == -1) {
int& res = dp[pos][k][p];
res = 0;
for (int i = 0; i < 4; ++i) {
int newp = findp(p, i);
if (newp == -1) continue;
int rem = ff(k + 1, newp);
res = (res + f(pos + 1, rem, newp)) % MOD;
}
}
return dp[pos][k][p];
}
int main() {
for (int i = 0; i < (1 << 7); ++i)
for (int j = 0; j < 4; ++j) fp[i][j] = inf;
memset(dp, -1, sizeof(dp));
cin >> n >> m;
pref.push_back("");
for (int i = 0; i < m; ++i) {
string s;
cin >> s;
st.push_back(s);
string temp = "";
for (int j = 0; j < s.size(); ++j) temp += s[j], pref.push_back(temp);
}
cout << f(0, 0, 0);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
pair<long long, long long> tbl[200001];
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; ++i) {
long long x, w;
cin >> x >> w;
tbl[i] = pair<long long, long long>(x + w, x - w);
}
sort(tbl, tbl + n);
int acc = 1;
int last = 0;
for (int i = 1; i < n; ++i) {
if (tbl[last].first <= tbl[i].second) {
last = i;
++acc;
}
}
cout << acc << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MOD = 1e9 + 7;
set<int> pos[26];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
for (int i = 0; i < s.size(); ++i) {
pos[s[i] - 'a'].insert(i);
}
int q;
cin >> q;
while (q--) {
int type;
cin >> type;
if (type == 1) {
int p;
char c;
cin >> p >> c;
--p;
pos[s[p] - 'a'].erase(p);
s[p] = c;
pos[s[p] - 'a'].insert(p);
} else {
int l, r;
cin >> l >> r;
--l;
--r;
int ans = 0;
for (int i = 0; i < 26; ++i) {
auto it = pos[i].lower_bound(l);
if (it != pos[i].end() and *it <= r) ++ans;
}
cout << ans << '\n';
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, r;
int a[1500];
while (scanf("%d%d", &n, &r) != EOF) {
int i, j;
double b[1500];
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
double ans = r;
for (j = 0; j < i; j++) {
int dx = abs(a[j] - a[i]);
if (dx <= 2 * r) {
ans = max(ans, b[j] + sqrt(4 * r * r - dx * dx));
}
}
b[i] = ans;
}
for (i = 0; i < n; i++) printf("%.10lf ", b[i]);
printf("\n");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
template <class T>
inline T my_min(T a, T b) {
return a < b ? a : b;
}
template <class T>
inline T my_max(T a, T b) {
return a > b ? a : b;
}
template <class T>
inline void my_swap(T& a, T& b) {
T tmp = a;
a = b;
b = tmp;
}
template <class T>
inline T my_abs(T a) {
return (a > 0 ? a : -a);
}
const int max_size = 1e5 + 5;
int n, s, maxv;
int main() {
scanf("%d", &n);
s = sqrt(n * 1.0);
maxv = n;
for (int i = 0; i < n; i += s) {
for (int j = n - i - s + 1; j <= maxv; j++) {
if (j > 0) printf("%d ", j);
}
maxv -= s;
}
printf("\n");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long bip(long long x, long long n, long long mod) {
long long res = 1;
while (n) {
if (n & 1) {
res = (res * x) % mod;
}
x = (x * x) % mod;
n >>= 1;
}
return res;
}
const int maxn = 1e9;
const long long inf = 1e18;
const int mod = 998244353;
const int N = 1e6 + 10;
vector<long long> a[N];
void solve() {
long long n;
cin >> n;
vector<long long> cnt(N, 0);
vector<long long> k(n + 10);
for (int i = 1; i <= n; i++) {
cin >> k[i];
for (int j = 1; j <= k[i]; j++) {
long long x;
cin >> x;
cnt[x]++;
a[i].push_back(x);
}
}
long long ans = 0;
for (int i = 1; i <= n; i++)
for (auto v : a[i])
ans = (ans + (bip((n * n * k[i]) % mod, mod - 2, mod) * cnt[v]) % mod) %
mod;
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tt = 1;
while (tt--) {
solve();
cout << '\n';
}
}
| 4 |
#include <bits/stdc++.h>
int main() {
unsigned int n;
unsigned short pmn;
unsigned int pc;
unsigned int i;
unsigned short a;
unsigned short p;
if (scanf("%u", &n) != 1) return -1;
if (n < 1 || n > 100000) return -1;
pmn = 101;
pc = 0;
for (i = 0; i < n; i++) {
if (scanf("%hu %hu", &a, &p) != 2) return -1;
if (a < 1 || a > 100 || p < 1 || p > 100) return -1;
if (p < pmn) pmn = p;
pc += a * pmn;
}
printf("%u\n", pc);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
double x[200], y[200];
vector<pair<double, int> > e;
double pi = acos(-1.0);
void add(double x) {
if (x < -pi / 2) {
e.push_back(make_pair(-pi, 1));
e.push_back(make_pair(x + pi / 2, -1));
e.push_back(make_pair(x + 3 * pi / 2, 1));
e.push_back(make_pair(pi, -1));
} else if (x > pi / 2) {
e.push_back(make_pair(-pi, 1));
e.push_back(make_pair(x - 3 * pi / 2, -1));
e.push_back(make_pair(x - pi / 2, 1));
e.push_back(make_pair(pi, -1));
} else {
e.push_back(make_pair(x - pi / 2, 1));
e.push_back(make_pair(x + pi / 2, -1));
}
}
int n;
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &x[i], &y[i]);
}
for (int i = 0; i < n; i++) {
e.clear();
for (int j = 0; j < n; j++) {
if (i == j) {
continue;
}
double ag = atan2(y[j] - y[i], x[j] - x[i]);
add(ag);
}
sort(e.begin(), e.end());
double b = -pi;
double s = 0;
int cnt = 0;
for (int i = 0; i < e.size(); i++) {
if (cnt == 0) {
s += e[i].first - b;
}
b = e[i].first;
cnt += e[i].second;
}
if (cnt == 0) {
s += pi - b;
}
printf("%.15f\n", s / (2 * pi));
}
return 0;
}
| 0 |
#include<iostream>
using namespace std;
int main(){
int t,y,n;
char c;
int ty=0,h=0;
while(cin>>t>>c>>y>>c>>n){
if(t*t+y*y==n*n)ty++;
if(t==y)h++;
}
cout<<ty<<"\n"<<h<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 1;
int arr[N];
int main() {
int n, k;
scanf("%d%d", &n, &k);
while (n--) {
int x;
scanf("%d", &x);
++arr[x];
}
long long ans = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
int b = __builtin_popcount(i ^ j);
if (b == k) {
if (k == 0)
ans += (1LL * arr[i] * (arr[j] - 1));
else
ans += (1LL * arr[i] * arr[j]);
}
}
}
ans >>= 1;
printf("%lld\n", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double s, a, b, c;
double x, y, z;
scanf("%lf%lf%lf%lf", &s, &a, &b, &c);
if (a + b + c == 0) {
printf("0.0 0.0 0.0\n");
} else {
x = s / (a + b + c) * a;
y = s / (a + b + c) * b;
z = s / (a + b + c) * c;
printf("%.16lf %.16lf %.16lf\n", x, y, z);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = -1;
int n;
map<long long, int> M;
int main() {
while (cin >> n) {
M.clear();
for (int i = (0); i < (int)(n); i++) {
long long x;
scanf("%lld", &x);
if (M.count(x))
M[x]++;
else
M[x] = 1;
}
bool win = false;
for (auto it = M.begin(); it != M.end(); ++it) {
if ((it->second) % 2) win = true;
}
cout << (win ? "Conan" : "Agasa") << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int s[26][2];
int t[26][2];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string ss, st;
cin >> ss >> st;
int n = ss.size();
int m = st.size();
for (int i = 0; i < (n); i++) {
if (ss[i] < 'a')
s[ss[i] - 'A'][1]++;
else
s[ss[i] - 'a'][0]++;
}
for (int i = 0; i < (m); i++) {
if (st[i] < 'a')
t[st[i] - 'A'][1]++;
else
t[st[i] - 'a'][0]++;
}
int g = 0, b = 0;
for (int i = 0; i < (26); i++) {
if (s[i][0] <= t[i][0] && s[i][1] <= t[i][1]) {
g += s[i][0] + s[i][1];
} else if (s[i][0] <= t[i][0] && s[i][1] > t[i][1]) {
g += s[i][0] + t[i][1];
b += min(s[i][1] - t[i][1], t[i][0] - s[i][0]);
} else if (s[i][0] > t[i][0] && s[i][1] <= t[i][1]) {
g += s[i][1] + t[i][0];
b += min(t[i][1] - s[i][1], s[i][0] - t[i][0]);
} else if (s[i][0] > t[i][0] && s[i][1] > t[i][1]) {
g += t[i][0] + t[i][1];
}
}
cout << g << " " << b << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e6 + 5;
int a[N], m[N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, b;
cin >> n >> b;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = n; i >= 0; --i) m[i] = max(m[i + 1], a[i]);
int ans = b;
for (int i = 1; i <= n; ++i) {
ans = max(ans, b % a[i] + b / a[i] * m[i + 1]);
}
cout << (n == 1 ? b : ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
vector<pair<pair<int, int>, string> > crew;
cin >> n;
for (int i = 0; i < n; ++i) {
int priority;
string name, kind;
cin >> name >> kind;
if (kind == "captain")
priority = 5;
else if (kind == "man")
priority = 4;
else if (kind == "woman" || kind == "child")
priority = 3;
else
priority = 2;
crew.push_back(make_pair(make_pair(priority, i), name));
}
sort(crew.begin(), crew.end());
for (int i = 0; i < n; ++i) cout << crew[i].second << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("-O3")
using namespace std;
void _main();
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
template <int NV, class V>
struct MinCostFlow {
struct edge {
int to, cap, rev;
V cost;
edge(int a, int b, V c, int d) {
to = a;
cap = b;
cost = c;
rev = d;
}
};
vector<edge> g[NV];
V h[NV], dist[NV];
int prevv[NV], preve[NV];
void add_edge(int from, int to, int cap, V cost) {
g[from].push_back(edge(to, cap, cost, g[to].size()));
g[to].push_back(edge(from, 0, -cost, g[from].size() - 1));
}
V mincost(int s, int t, int f) {
V res = 0;
int i;
for (i = 0; i < NV; i++) h[i] = 0;
while (f > 0) {
int d = f;
deque<pair<V, int> > q;
for (i = 0; i < NV; i++) dist[i] = 1LL << 60;
dist[s] = 0;
q.push_back(make_pair(0, s));
while (!q.empty()) {
V c = q.front().first;
int v = q.front().second;
q.pop_front();
if (dist[v] < c) continue;
for (i = 0; i < g[v].size(); i++) {
edge &e = g[v][i];
if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
if (dist[e.to] == dist[v] + 1) {
q.push_back(make_pair(dist[e.to], e.to));
} else {
q.push_front(make_pair(dist[e.to], e.to));
}
}
}
}
if (dist[t] == 1LL << 60) return -1;
for (i = 0; i < NV; i++) h[i] += dist[i];
for (i = t; i != s; i = prevv[i]) d = min(d, g[prevv[i]][preve[i]].cap);
f -= d;
res += d * h[t];
for (i = t; i != s; i = prevv[i]) {
edge &e = g[prevv[i]][preve[i]];
e.cap -= d;
g[i][e.rev].cap += d;
}
}
return res;
}
};
int N, A[101];
string S;
void _main() {
cin >> N >> S;
for (int i = 0; i < N; i++) cin >> A[i];
MinCostFlow<2010, long long> mcf;
int s = 2000, t = 2001, c = 1900, ma = 1010;
for (int i = 0; i < N; i++) mcf.add_edge(s, i, 1, 0);
for (int i = 0; i < N / 2; i++) {
int j = N - 1 - i;
for (int k = 0; k < 26; k++) {
if (S[i] - 'a' == k)
mcf.add_edge(i, N + i * 26 + k, 1, ma - A[i]);
else
mcf.add_edge(i, N + i * 26 + k, 1, ma);
if (S[j] - 'a' == k)
mcf.add_edge(j, N + i * 26 + k, 1, ma - A[j]);
else
mcf.add_edge(j, N + i * 26 + k, 1, ma);
mcf.add_edge(N + i * 26 + k, c + k, 1, 0);
}
}
vector<int> cnt(26);
for (auto &c : S) cnt[c - 'a']++;
for (int i = 0; i < 26; i++) {
mcf.add_edge(c + i, t, cnt[i], 0);
}
long long ans = ma * N - mcf.mincost(s, t, N);
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5;
const long long INF = 1e9 + 5;
const long long MOD = 1000000007;
int main() {
ios::sync_with_stdio(false);
ifstream inf("input.txt");
long long n;
cin >> n;
long long t = 1, ans = 0;
for (int i = 1; t <= n; i++) {
ans += (min(t * 10, n + 1) - t) * i;
t = t * 10;
}
cout << ans;
}
| 2 |
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const double eps = 1e-8;
class P {
public:
double x, y, z;
P(){};
P(double x, double y, double z) : x(x), y(y), z(z) {};
P operator+(const P &p) {
return P(x + p.x, y + p.y, z + p.z);
}
P operator-(const P &p) {
return P(x - p.x, y - p.y, z - p.z);
}
P operator*(const double &n) {
return P(x * n, y * n, z * n);
}
P operator/(const double &n) {
return P(x / n, y / n, z / n);
}
};
double dot(const P &a, const P &b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
P cross(const P &a, const P &b) {
return P(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
int main() {
P p[5];
for (int i=0; i<5; ++i) cin >> p[i].x >> p[i].y >> p[i].z;
P z = cross(p[3] - p[2], p[4] - p[2]);
double a = dot(p[2] - p[0], z);
double b = dot(p[1] - p[0], z);
double c = abs(a);
double d = abs(b) - c;
if (abs(a) < eps || (b < -eps && eps < a) || (a < -eps && eps < b) || d < -eps) {
cout << "HIT" << endl;
return 0;
}
P i = (p[0] * d + p[1] * c) / (c + d);
int plus = 0;
if (-eps < dot(z, cross(p[3] - p[2], i - p[2]))) ++plus;
if (-eps < dot(z, cross(p[4] - p[3], i - p[3]))) ++plus;
if (-eps < dot(z, cross(p[2] - p[4], i - p[4]))) ++plus;
int minus = 0;
if (eps > dot(z, cross(p[3] - p[2], i - p[2]))) ++minus;
if (eps > dot(z, cross(p[4] - p[3], i - p[3]))) ++minus;
if (eps > dot(z, cross(p[2] - p[4], i - p[4]))) ++minus;
cout << (plus == 3 || minus == 3 ? "MISS" : "HIT") << endl;
return 0;
}
| 0 |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a; i ++)
int main(void){
int N;
while(cin>>N,N){
vector<pair<int,int> > v(N);
rep(i,N)cin>>v[i].second>>v[i].first;
sort(v.begin(),v.end());
int M = 0;
bool is = true;
rep(i,N){
M += v[i].second;
if(v[i].first < M){
is = false;
break;
}
}
cout<<(is?"Yes":"No")<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct pt {
int64_t x, y;
pt() {}
pt(int64_t _x, int64_t _y) : x(_x), y(_y) {}
int64_t cross(const pt& p) const { return x * p.y - y * p.x; }
int64_t cross(const pt& a, const pt& b) const {
return (a - *this).cross(b - *this);
}
int64_t norm() const { return x * x + y * y; }
pt operator-(const pt& p) const { return pt(x - p.x, y - p.y); }
};
int N;
vector<pt> seq;
bool lexComp(const pt& l, const pt& r) {
return l.x < r.x || (l.x == r.x && l.y < r.y);
}
void prepare(vector<pt>& points) {
N = points.size();
int pos = 0;
for (int i = 0; i < N; ++i)
if (lexComp(points[i], points[pos])) pos = i;
rotate(points.begin(), points.begin() + pos, points.end());
N--;
seq.resize(N);
for (int i = 0; i < N; ++i) seq[i] = points[i + 1] - points[0];
}
bool inTriangle(pt a, pt b, pt c, pt point) {
return abs(a.cross(b, c)) == abs(point.cross(a, b)) + abs(point.cross(b, c)) +
abs(point.cross(c, a));
}
int sgn(int64_t val) { return val >= 0 ? val ? 1 : 0 : -1; }
bool inPolygon(pt point) {
if (seq[0].cross(point) != 0 &&
sgn(seq[0].cross(point)) != sgn(seq[0].cross(seq[N - 1])))
return false;
if (seq[N - 1].cross(point) != 0 &&
sgn(seq[N - 1].cross(point)) != sgn(seq[N - 1].cross(seq[0])))
return false;
if (seq[0].cross(point) == 0) return seq[0].norm() >= point.norm();
if (seq[N - 1].cross(point) == 0) return seq[N - 1].norm() >= point.norm();
int l = 0, r = N - 1, m, pos;
while (l <= r) {
m = (l + r) / 2;
if (seq[m].cross(point) <= 0)
pos = m, l = m + 1;
else
r = m - 1;
}
return inTriangle(seq[pos], seq[pos + 1], pt(0, 0), point);
}
bool clockwise(pt p1, pt p2, pt p3) { return p1.cross(p2, p3) < 0; }
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
vector<pt> points(n);
for (pt& p : points) cin >> p.x >> p.y;
prepare(points);
int t;
cin >> t;
auto solve = [&](pt point) -> int64_t {
int cur = 0;
int64_t ans = 0;
for (int i = 0; cur < n; (++i) %= n) {
if (i == cur) continue;
if (!clockwise(points[cur], points[i], point)) {
int upb = i - 1;
int dist = upb - cur;
if (dist < 0) dist += n;
int64_t add = dist * 1ll * (dist - 1) / 2;
if (add > 0) ans += add;
++cur, --i;
}
}
return ans;
};
int64_t c3n = n * 1ll * (n - 1) / 2 * 1ll * (n - 2) / 3;
while (t-- > 0) {
pt point;
cin >> point.x >> point.y;
if (!inPolygon(point - points[0]))
cout << 0;
else
cout << c3n - solve(point);
cout << '\n';
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000001;
template <class T, class U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
return out << p.first << ' ' << p.second;
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &v) {
for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {
out << *i << ' ';
}
return out;
}
template <class T, class U>
istream &operator>>(istream &in, pair<T, U> &p) {
return in >> p.first >> p.second;
}
template <class T>
istream &operator>>(istream &in, vector<T> &v) {
for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {
in >> (*i);
}
return in;
}
template <class T>
ostream &operator,(ostream &out, const T &t) {
return out << t;
}
template <class T>
istream &operator,(istream &in, T &t) {
return in >> t;
}
int io_init() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout, fixed, setprecision(9);
return 1;
}
int _io_dummy = io_init();
template <class T, class U>
bool remin(T &a, const U &b) {
return b < a ? a = b, true : false;
}
template <class T, class U>
bool remax(T &a, const U &b) {
return b > a ? a = b, true : false;
}
int main() {
int a;
string s;
cin, a, s;
reverse((s).begin(), (s).end());
istringstream iss(s);
int b;
iss, b;
cout, a + b, '\n';
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
bool flag = 0;
cin >> s;
for (int i = 0; i < s.size(); i++)
if (s[i] != 'a')
while (i < s.size()) {
flag = 1;
if (s[i] != 'a')
s[i++]--;
else {
cout << s;
return 0;
}
}
if (!flag) s.back() = 'z';
cout << s;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 3.14159265
#define mod 1000000007
#define pb push_back
const int N = 5e3+2;
const ll mxn =1e18;
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FF fflush(stdout);
#define lcm(a,b) (a*b)/__gcd(a,b)
int main()
{
FAST
ll t;
cin >> t;
while(t--)
{
ll n,i,j,p=0;
cin >> n ;
vector<ll>u(n),s(n);
for(i=0;i<n;i++)cin >> u[ i ];
for(i=0;i<n;i++)cin >> s[ i ];
vector<pair<ll,ll> >a(n);
map<ll,ll>m,z;
for(i=0;i<n;i++)
{
a[ i ].first=u[ i ];
a[ i ].second=s[ i ];
m[u[ i ] ]++;
z[u[ i ] ]+=s[ i ];
}
sort(a.begin(),a.end());
map<ll,ll>ans,temp;
ll c=0;
for(i=0;i<n;i++)
{
c++;
temp[c]+=(a[ i ].second+temp[c-1]);
ans[c]+=(z[a[ i ].first ]-temp[m[a[ i ].first ]%c ]);
if(i<n-1 && a[ i ].first!=a[i+1].first)
{
c=0;
temp.clear();
}
}
for(i=1;i<=n;i++)cout << ans[ i ] << ' ';
cout << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> s;
char cur = CHAR_MAX;
for (auto& c : s) {
cout << (c <= cur ? "Mike\n" : "Ann\n");
cur = min(c, cur);
}
}
| 3 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N, M; cin >> N >> M;
vector<int> P(M), Y(M), ord[100010];
for (int i = 0; i < M; i++) {
cin >> P[i] >> Y[i];
ord[P[i]].push_back(Y[i]);
}
for (int i = 1; i <= N; i++) sort(ord[i].begin(), ord[i].end());
for (int i = 0; i < M; i++) {
printf("%06d", P[i]);
int d = lower_bound(ord[P[i]].begin(), ord[P[i]].end(), Y[i]) - ord[P[i]].begin() + 1;
printf("%06d\n", d);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct SegmentTree {
int n;
vector<int> st;
SegmentTree() {}
void build(int _) {
st.resize(3 * _, 0);
n = _;
}
int op(int a, int b) { return a + b; }
void update(int pos, int value) {
for (st[pos += n] += value; pos > 1; pos >>= 1)
st[pos >> 1] = op(st[pos], st[pos ^ 1]);
}
int query(int l, int r) {
int ans = 0;
r++;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = op(ans, st[l++]);
if (r & 1) ans = op(ans, st[--r]);
}
return ans;
}
};
int last[300010];
bool vis[300010];
int mx[300010];
int v[300010];
int main() {
memset(last, -1, sizeof last);
int n, m;
cin >> n >> m;
SegmentTree st;
st.build(m + 10);
last[1] = 0;
for (int i = 1; i <= m; ++i) {
int x;
cin >> x;
v[i] = x;
mx[i] = i;
if (last[x] == -1) {
st.update(i, 1);
} else {
mx[x] = max(mx[x], 1 + st.query(last[x] + 1, i - 1));
st.update(last[x], -1);
st.update(i, 1);
}
last[x] = i;
}
SegmentTree st2;
st2.build(n + 10);
for (int i = 1; i <= m; ++i) {
if (vis[v[i]]) continue;
vis[v[i]] = 1;
mx[v[i]] = max(mx[v[i]], v[i] + st2.query(v[i] + 1, n));
st2.update(v[i], 1);
}
for (int i = 1; i <= n; ++i) {
if (last[i] == -1) {
mx[i] = max(mx[i], i + st2.query(i + 1, n));
}
}
for (int i = 1; i <= n; ++i) {
if (last[i] != -1) mx[i] = max(mx[i], 1 + st.query(last[i] + 1, m));
if (last[i] == -1) {
cout << i << " " << mx[i] << "\n";
} else {
cout << 1 << " " << mx[i] << "\n";
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
long long x;
cin >> x;
cout << x / 2 + 1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
const int Mod = 1e9 + 7;
int n;
int a[maxn], cnt[maxn];
int ok = 0;
void add(int i) {
if (cnt[a[i]] == 1) ++ok;
++cnt[a[i]];
}
void sub(int i) {
--cnt[a[i]];
if (cnt[a[i]] == 1) --ok;
}
long long Divide(int l, int r) {
if (l == r) {
if (a[l] == 1) return 1;
return 0;
}
long long ret = 0;
int M = (l + r) >> 1;
ret = ret + Divide(l, M);
ret = ret + Divide(M + 1, r);
int u, v, it, maxu = 0, maxv = 0;
u = M + 1;
it = M + 1;
for (v = M + 1; v <= r; ++v) {
add(v);
maxv = max(maxv, a[v]);
while (u - 1 >= l && a[u - 1] < maxv) {
--u;
}
int pos = v - maxv + 1;
if (u <= pos && pos <= M) {
while (it < pos) sub(it++);
while (it > pos) add(--it);
if (!ok) ++ret;
}
}
for (int i = M + 1; i <= r; ++i) sub(i);
for (int i = M; i >= it; --i) sub(i);
maxu = 0;
maxv = 0;
v = M;
it = M;
for (u = M; u >= l; --u) {
add(u);
maxu = max(maxu, a[u]);
while (v + 1 <= r && a[v + 1] <= maxu) {
++v;
}
int pos = u + maxu - 1;
if (M + 1 <= pos && pos <= v) {
while (it < pos) add(++it);
while (it > pos) sub(it--);
if (!ok) ++ret;
}
}
for (int i = M; i >= l; --i) sub(i);
for (int i = M + 1; i <= it; ++i) sub(i);
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
srand(time(NULL));
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
cout << Divide(1, n);
return 0;
}
| 6 |
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
#define INF 999999999
#define REP(i,n) for(int i=0; i<(int)(n); i++)
int bi(int v){
int r = 0;
REP(i,20){
if((1 << i) & v) r++;
}
//cout << r << endl;
return r;
}
int main(){
int n, m;
double lab[20][3];
double ans=0, now;
cin >> n >> m;
REP(i,n){
cin >> lab[i][0] >> lab[i][1] >> lab[i][2];
}
if(m==1){
cout << "0" <<endl;
return 0;
}
REP(i,1<<n){
if(bi(i) != m) continue;
now = 0;
REP(j,n){
for(int k=j+1;k<n;k++){
if((i&1<<j)&&(i&1<<k)){
now += pow((lab[j][0] - lab[k][0]),2);
now += pow((lab[j][1] - lab[k][1]),2);
now += pow((lab[j][2] - lab[k][2]),2);
}
}
}
if(now > ans) ans = now;
}
printf("%.13f\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
char s[7];
int main() {
scanf("%s", s);
int mx = 0;
for (int i = 0; s[i]; i++) mx = max(mx, s[i] - '0');
printf("%d\n", mx);
int len = strlen(s);
for (int i = 1; i <= mx; i++) {
bool has = 0;
for (int j = 0; s[j]; j++) {
if (s[j] != '0')
printf("1"), has = 1, s[j]--;
else if (has)
printf("0");
}
printf(" ");
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[1024][1024];
int ans[1024][1024];
int c[1024][1024];
struct node {
int x, y, length;
};
std::vector<node> v;
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char ch;
cin >> ch;
if (ch == '.')
a[i][j] = 0;
else
a[i][j] = 1;
}
}
int t = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j]) {
int cnt = 1;
while (a[i - cnt][j] && a[i + cnt][j] && a[i][j - cnt] && a[i][j + cnt])
cnt++;
cnt--;
if (!cnt) continue;
for (int q = 0; q <= cnt; q++)
c[i - q][j] = c[i + q][j] = c[i][j - q] = c[i][j + q] = cnt;
ans[i][j] = cnt;
t++;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] && c[i][j] == 0) {
printf("-1\n");
return 0;
}
}
}
printf("%d\n", t);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (ans[i][j]) printf("%d %d %d\n", i, j, ans[i][j]);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int s[105], e[105];
int main() {
int n, ans = 0, res = 0;
cin >> n;
while (n--) {
int a, b, c;
cin >> a >> b >> c;
if (s[a] || e[b])
res += c, s[b] = e[a] = 1;
else
s[a] = e[b] = 1;
ans += c;
}
cout << min(res, ans - res);
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int main(){
cin>>a>>b;
vector<int>d(a,0);
for(int i=0;i<b*2;i++){
cin>>c;
d[c-1]++;
}
for(int i=0;i<a;i++)cout<<d[i]<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int grid[45][45];
int numrec[45][45][45][45];
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
for (int j = 1; j <= m; j++) grid[i][j] = s[j - 1] - '0';
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
grid[i][j] += grid[i - 1][j] + grid[i][j - 1] - grid[i - 1][j - 1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int x = i; x > 0; x--)
for (int y = j; y > 0; y--) {
if (grid[i][j] + grid[x - 1][y - 1] ==
grid[x - 1][j] + grid[i][y - 1])
numrec[x][y][i][j]++;
numrec[x][y][i][j] += numrec[x + 1][y][i][j] +
numrec[x][y + 1][i][j] -
numrec[x + 1][y + 1][i][j];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int x = i; x <= n; x++)
for (int y = j; y <= m; y++)
numrec[i][j][x][y] += numrec[i][j][x - 1][y] +
numrec[i][j][x][y - 1] -
numrec[i][j][x - 1][y - 1];
for (int i = 0; i < q; i++) {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << numrec[a][b][c][d] << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000000000000000;
map<pair<long long, int>, long long> memo;
long long solve(long long n, int x) {
pair<long long, int> p = make_pair(n, x);
if (memo.find(p) != memo.end()) return memo[p];
if (n == 0) return memo[p] = 0;
if (n == 1) return memo[p] = (x == 0) ? 1 : 0;
return memo[p] = solve((n + 1) / 2, x) + (x ? solve(n / 2, x - 1) : 0);
}
int main() {
ios_base::sync_with_stdio(false);
long long n, t;
cin >> n >> t;
for (int i = 0; (1ll << i) <= t; ++i)
if (t == (1ll << i)) {
cout << solve(n + 2, i + 1) - solve(2, i + 1);
return 0;
}
cout << 0;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
vector<vector<int>> dp(N, vector<int>(N + 1, -1));
for (int i = 0; i < N; i++) dp[i][i + 1] = A[i];
for (int l = N - 1; l >= 0; l--) {
for (int r = l + 2; r <= N; r++) {
for (int m = l + 1; m < r; m++) {
if (dp[l][m] != -1 && dp[l][m] == dp[m][r]) {
dp[l][r] = dp[l][m] + 1;
}
}
}
}
vector<int> mdp(N + 1, N);
mdp[0] = 0;
for (int i = 0; i < N; i++) {
for (int j = i + 1; j <= N; j++) {
if (dp[i][j] != -1) {
mdp[j] = min(mdp[j], mdp[i] + 1);
}
}
}
cout << mdp[N] << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
std::vector<long long int> v;
v.clear();
for (long long int i = 0; i < n; i++) {
long long int x;
cin >> x;
v.push_back(x);
}
sort(v.begin(), v.end());
long long int x = 0, y = 0;
for (long long int i = 0; i < n / 2; i++) x += v[i];
for (long long int i = n / 2; i < n; i++) y += v[i];
cout << (x * x + y * y) << endl;
return 0;
}
| 2 |
#include <iostream>
#include <algorithm>
using namespace std;
struct Card{
char a;
int b;
};
bool compare_as_ints(Card i, Card j)
{
return (i.b<j.b);
}
int partition(Card A[], int p, int r)
{
Card x = A[r];
int i = p-1;
for(int j=p; j<=r-1; j++){
if(A[j].b<=x.b){
i++;
swap(A[i], A[j]);
}
}
swap(A[i+1], A[r]);
return i+1;
}
void quickSort(Card A[], int p, int r)
{
if(p<r){
int q = partition(A, p, r);
quickSort(A, p, q-1);
quickSort(A, q+1, r);
}
}
int main()
{
int n;
cin >> n;
Card A[100000], B[100000];
for(int i=0; i<n; i++){
cin >> A[i].a >> A[i].b;
B[i] = A[i];
}
quickSort(A, 0, n-1);
stable_sort(B, B+n, compare_as_ints);
bool flg = true;
for(int i=0; i<n; i++){
if(!(A[i].a==B[i].a && A[i].b==B[i].b)){
flg = false;
break;
}
}
if(flg){
cout << "Stable" << endl;
}
else{
cout << "Not stable" << endl;
}
for(int i=0; i<n; i++){
cout << A[i].a << ' ' << A[i].b << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4,avx,avx2,fma")
using namespace std;
const char nl = '\n';
using ll = long long;
using vi = vector<long long>;
using vl = vector<ll>;
using pii = pair<long long, long long>;
using pll = pair<ll, ll>;
using str = string;
using vpii = vector<pii>;
using vpll = vector<pll>;
template <class T>
inline bool ckmin(T& a, T b) {
return b < a ? a = b, 1 : 0;
}
template <class T>
inline bool ckmax(T& a, T b) {
return b > a ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
str to_string(char c) { return str(1, c); }
str to_string(bool b) { return b ? "true" : "false"; }
str to_string(const char* second) { return (str)second; }
str to_string(str second) { return second; }
template <class A>
str to_string(complex<A> c) {
stringstream ss;
ss << c;
return ss.str();
}
str to_string(vector<bool> v) {
str res = "{";
for (long long i = 0; i < (long long)v.size(); i++) res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ>
str to_string(bitset<SZ> b) {
str res = "";
for (long long i = 0; i < b.size(); i++) res += char('0' + b[i]);
return res;
}
template <class A, class B>
str to_string(pair<A, B> p);
template <class T>
str to_string(T v) {
bool fst = 1;
str res = "{";
for (const auto& x : v) {
if (!fst) res += ", ";
fst = 0;
res += to_string(x);
}
res += "}";
return res;
}
template <class A, class B>
str to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
cerr << to_string(h);
if (sizeof...(t)) cerr << ", ";
DBG(t...);
}
const long long mxN = 101;
long long n, m, d[mxN], ways[mxN], vis[mxN], out[mxN], in[mxN], ways2[mxN],
d2[mxN];
vi adj[mxN];
queue<long long> q;
queue<pii> q2;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (long long i = 0; i < m; i++) {
long long a, b;
cin >> a >> b;
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
memset(d, -1, sizeof(d));
d[0] = 0;
ways[0] = 1;
q.push(0);
while (!q.empty()) {
long long u = q.front();
q.pop();
for (auto& v : adj[u])
if (d[v] == -1) {
q.push(v);
d[v] = d[u] + 1;
ways[v] += ways[u];
} else if (d[v] == d[u] + 1) {
ways[v] += ways[u];
}
}
memset(d2, -1, sizeof(d2));
d2[n - 1] = 0;
ways2[n - 1] = 1;
q.push(n - 1);
while (!q.empty()) {
long long u = q.front();
q.pop();
for (auto& v : adj[u])
if (d2[v] == -1) {
q.push(v);
ways2[v] += ways2[u];
d2[v] = d2[u] + 1;
} else if (d2[u] + 1 == d2[v]) {
ways2[v] += ways2[u];
}
}
ll ans = 0;
for (long long i = 0; i < n; i++) {
cerr << d[i] << " " << d2[i] << nl;
if (d[i] + d2[i] == d[n - 1]) {
if (i != 0 && i != n - 1)
ckmax(ans, (ll)ways[i] * ways2[i] * 2);
else
ckmax(ans, (ll)ways[i] * ways2[i]);
}
}
cout << fixed << setprecision(12) << (double)ans / ways[n - 1] << nl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long f = 1, x = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
const int MAXN = 1005;
const int INF = 1e9;
bitset<MAXN> G[MAXN], S[MAXN], nl;
int n, fa[MAXN];
int get_fa(int x) {
if (fa[x] == x) return x;
return fa[x] = get_fa(fa[x]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
n = read();
for (int i = 1; i <= n; ++i) {
int k = read();
while (k--) {
int x = read();
G[i][x] = 1;
}
}
for (int i = 1; i <= n; ++i) fa[i] = i;
bitset<MAXN> t;
int u, v, cnt = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
t = (G[i] & G[j]);
if (t.count() == 2) {
u = 0;
v = 0;
for (int k = 1; k <= n; ++k)
if (t[k]) (!u) ? (u = k) : (v = k);
int fu = get_fa(u), fv = get_fa(v);
if (fu != fv) {
fa[fu] = fv;
cout << u << " " << v << endl;
cnt++;
S[u][u] = S[v][v] = S[u][v] = S[v][u] = 1;
nl[u] = nl[v] = 1;
}
}
}
}
if (cnt == 0) {
for (int i = 2; i <= n; ++i) cout << 1 << " " << i << endl;
return 0;
} else if (cnt == 1) {
for (int i = 1; i <= n; ++i) {
if (G[i].count() < n) {
for (int j = 1; j <= n; ++j) {
if (!nl[j]) cout << (G[i][j] ? u : v) << " " << j << endl;
}
break;
}
}
return 0;
}
for (int i = 1; i <= n; ++i) {
if (!nl[i]) {
int mn = INF;
for (int j = 1; j <= n; ++j) {
if (G[j][i] && (G[j] & nl).count() < mn) {
t = (G[j] & nl);
mn = t.count();
}
}
for (int j = 1; j <= n; ++j) {
if (t[j] && S[j] == t) {
cout << i << " " << j << endl;
break;
}
}
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
int n;
char s[200][200];
int row[200];
int col[200];
bool vis[200][200];
while (~scanf("%d", &n)) {
memset(row, 0, sizeof(row));
memset(col, 0, sizeof(col));
memset(vis, true, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%s", s[i]);
for (int j = 0; j < n; j++) {
if (s[i][j] == 'E') {
row[i]++;
col[j]++;
vis[i][j] = false;
}
}
}
bool rflag = false, cflag = false;
for (int i = 0; i < n; i++) {
if (row[i] == n) {
rflag = true;
}
if (col[i] == n) {
cflag = true;
}
}
if (rflag && cflag) {
printf("-1\n");
continue;
}
if (!rflag) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (vis[i][j]) {
printf("%d %d\n", i + 1, j + 1);
break;
}
}
}
} else {
for (int j = 0; j < n; j++) {
for (int i = 0; i < n; i++) {
if (vis[i][j]) {
printf("%d %d\n", i + 1, j + 1);
break;
}
}
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
template <typename T>
inline void SWAP(T &a, T &b) {
T tmp = a;
a = b;
b = tmp;
}
template <typename T>
inline T ABS(const T &VAL) {
return VAL < 0 ? -VAL : VAL;
}
template <typename T>
inline T MAX(const T &a, const T &b) {
return a < b ? b : a;
}
template <typename T>
inline T MIN(const T &a, const T &b) {
return a < b ? a : b;
}
template <typename T>
inline T SQR(const T &a) {
return a * a;
}
const int INTinf = 1000000005;
const int nINTinf = 0 - 2147483648;
const long long LLinf = 9223372036854775807LL;
using namespace std;
int n;
bool a[2005][2005];
int ch[2005][2005];
const int l = -3;
const int r = 4;
const int p = 22;
bool check2(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= n) return false;
return ch[x][y] == 2;
}
bool check(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= n) return false;
if (ch[x][y]) return false;
int sum = 0;
for (int i = MAX(x + l, 0); i < MIN(x + r, n); i++) {
for (int j = MAX(y + l, 0); j <= MIN(y + r, n); j++) {
sum += a[i][j];
}
}
if (sum >= p) {
ch[x][y] = 2;
return true;
}
ch[x][y] = 1;
return false;
}
vector<pair<int, int> > order;
long long sX;
long long sY;
int cnt;
int squares = 0;
int circles = 0;
inline void put(int x, int y) {
if (check(x, y)) {
order.push_back(pair<int, int>(x, y));
cnt++;
sX += x;
sY += y;
}
}
void add(int x, int y) {
sX = x;
sY = y;
cnt = 1;
order.push_back(pair<int, int>(x, y));
while (!order.empty()) {
x = order.back().first;
y = order.back().second;
order.pop_back();
if (x > 0) {
put(x - 1, y);
}
if (x < n - 1) {
put(x + 1, y);
}
if (y > 0) {
put(x, y - 1);
}
if (y < n - 1) {
put(x, y + 1);
}
}
int aX = sX / cnt;
int aY = sY / cnt;
if (cnt < 50) {
return;
}
int diu = 0;
while (check2(aX - diu, aY)) {
diu++;
}
int diu2 = 0;
while (check2(aX - diu2, aY - diu2)) {
diu2++;
}
int diu3 = 0;
while (check2(aX - diu3, aY - diu3 * 2)) {
diu3++;
}
int diu4 = 0;
while (check2(aX + diu4, aY - diu4 * 2)) {
diu4++;
}
diu--;
diu2--;
diu3--;
diu4--;
double val[3];
val[0] = diu;
val[1] = sqrt((long double)(diu2 * diu2 * 2));
val[2] = sqrt((long double)(diu3 * diu3 * 5));
val[3] = sqrt((long double)(diu4 * diu4 * 5));
sort(val, val + 4);
if (val[0] / val[3] < 0.86) {
squares++;
} else {
circles++;
}
}
int main() {
scanf("%d", &n);
for (int i = 0; i != n; i++) {
for (int j = 0; j != n; j++) {
int q;
scanf("%d", &q);
a[i][j] = q;
}
}
for (int i = 0; i != n; i++) {
for (int j = 0; j != n; j++) {
if (!ch[i][j]) {
if (check(i, j)) {
add(i, j);
}
}
}
}
cout << circles << ' ' << squares << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
ll n, k;
cin >> n >> k;
ll minsplits = 0;
ll maxsplits = 0;
bool done = false;
for (ll a = n; a >= 0; --a) {
if (minsplits > k) {
cout << "NO\n";
done = true;
break;
}
ll maxbefnsplits = -1;
if (a > 0) {
ll smin1 = (1 << (n - a)) - 1;
ll subs = smin1 * smin1;
if (subs > 0) {
maxbefnsplits = maxsplits;
for (ll j = a; j > 0; --j) {
maxbefnsplits += subs;
subs *= 4;
if (maxbefnsplits >= k) break;
}
}
}
if (maxsplits >= k || maxbefnsplits >= k) {
cout << "YES " << a << "\n";
done = true;
break;
}
maxsplits *= 4;
maxsplits += 1;
minsplits += (1 << (n - a + 1)) - 1;
}
if (!done) cout << "NO\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long maxN = 1e5 + 7, MOD = 1e9 + 7, INF = 1e18 + 7, N = 5000 + 7;
pair<pair<long long, long long>, long long> edge[maxN];
long long h[maxN], par[maxN], dis[maxN], n, m, k, x, y, ans, col[maxN], w, now,
cur;
vector<pair<long long, long long> > a[maxN];
bool spec[maxN], v[maxN];
vector<pair<pair<long long, long long>, long long> > r;
set<pair<long long, long long> > q;
bool edge_sort(pair<pair<long long, long long>, long long> a,
pair<pair<long long, long long>, long long> b) {
return a.second < b.second;
}
void dijkstra() {
for (long long i = 1; i <= n; i++) {
if (!spec[i]) dis[i] = INF;
col[i] = i;
q.insert(pair<long long, long long>(dis[i], i));
}
for (long long i = 1; i <= n; i++) {
now = q.begin()->second;
v[now] = true;
q.erase(q.begin());
for (long long j = 0; j < a[now].size(); j++) {
cur = a[now][j].first;
w = a[now][j].second;
if (!v[cur] && dis[cur] > dis[now] + w) {
q.erase(pair<long long, long long>(dis[cur], cur));
dis[cur] = dis[now] + w;
q.insert(pair<long long, long long>(dis[cur], cur));
col[cur] = col[now];
}
}
}
}
long long find_par(long long xx) {
if (par[xx] != xx) par[xx] = find_par(par[xx]);
return par[xx];
}
void merge(long long hx, long long hy) {
if (h[hx] > h[hy]) swap(hx, hy);
par[hx] = hy;
h[hy] += (h[hx] == h[hy]);
}
int main() {
cin.tie(0), cout.tie(0), ios::sync_with_stdio(false);
cin >> n >> m;
for (long long i = 1; i <= m; i++)
cin >> edge[i].first.first >> edge[i].first.second >> edge[i].second,
a[edge[i].first.first].push_back(
pair<long long, long long>(edge[i].first.second, edge[i].second)),
a[edge[i].first.second].push_back(
pair<long long, long long>(edge[i].first.first, edge[i].second));
cin >> k;
for (long long i = 1; i <= k; i++) cin >> x, spec[x] = true;
dijkstra();
for (long long i = 1; i <= n; i++) par[i] = col[i];
for (long long i = 1; i <= m; i++) {
x = edge[i].first.first;
y = edge[i].first.second;
if (col[x] != col[y]) {
edge[i].second += dis[x] + dis[y];
r.push_back(edge[i]);
}
}
sort(r.begin(), r.end(), edge_sort);
for (long long i = 0; i < r.size(); i++) {
x = r[i].first.first;
y = r[i].first.second;
if (find_par(x) != find_par(y)) {
merge(find_par(x), find_par(y));
ans += r[i].second;
}
}
if (!spec[1]) ans += dis[1];
return cout << ans << "\n", 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
inline int read() {
int w = 1, s = 0;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch)) {
s = s * 10 + ch - '0';
ch = getchar();
}
return w * s;
}
int n, p[1000010], fac[1000010], Sum[1000010];
inline long long ksm(long long x, long long y) {
long long res = 1;
while (y) {
if (y & 1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
inline int lowbit(int x) { return x & (-x); }
inline void add(int pos, int d) {
while (pos <= n) Sum[pos] += d, pos += lowbit(pos);
}
inline int Qry(int pos) {
int res = 0;
while (pos) res += Sum[pos], pos -= lowbit(pos);
return res;
}
set<int> st;
inline void Init() {
fac[0] = 1;
for (register int i = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % mod;
}
int main() {
n = read();
int tot = 0;
for (register int i = 1; i <= n; ++i) p[i] = read(), tot += (p[i] == (-1));
Init();
long long ans = 0;
for (register int i = n; i; --i) {
if (p[i] != -1) {
add(p[i], 1);
ans += 1ll * fac[tot] * Qry(p[i] - 1) % mod;
ans %= mod;
}
}
ans += 1ll * tot * (tot - 1) % mod * ksm(2, mod - 2) % mod * ksm(2, mod - 2) %
mod * fac[tot] % mod;
ans %= mod;
memset(Sum, 0, sizeof(Sum));
for (register int i = 1; i <= n; ++i) add(i, 1);
for (register int i = 1; i <= n; ++i)
if (p[i] != -1) add(p[i], -1);
if (tot) {
int sum = 0;
for (register int i = 1; i <= n; ++i) {
if (p[i] == -1) {
ans += 1ll * sum * fac[tot - 1] % mod;
ans %= mod;
} else {
int tmp = Qry(p[i]);
sum += tmp;
sum %= mod;
}
}
sum = 0;
for (register int i = n; i; --i) {
if (p[i] == -1) {
ans += 1ll * sum * fac[tot - 1] % mod;
ans %= mod;
} else {
int tmp = tot - Qry(p[i]);
sum += tmp;
sum %= mod;
}
}
}
cout << 1ll * ans * ksm(fac[tot], mod - 2) % mod << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using llong = long long;
using ldbl = long double;
using P = pair<llong, llong>;
#define BE(x) x.begin(), x.end()
const llong inf = llong(1e18)+7;
const llong mod = 1e9+7;
int main(){
llong N;
vector<llong> d;
cin >> N;
d.resize(N);
for(int i = 0; i < N; i++)
cin >> d[i];
llong maxi = 0;
for(int i = 0; i < N; i++){
if(maxi < i*10)
break;
maxi = max(maxi, i*10+d[i]);
}
if(maxi < 10*(N-1)){
cout << "no" << endl;
return 0;
}
reverse(BE(d));
maxi = 0;
for(int i = 0; i < N; i++){
if(maxi < i*10)
break;
maxi = max(maxi, i*10+d[i]);
}
if(maxi < 10*(N-1))
cout << "no" << endl;
else
cout << "yes" << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
using namespace std;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const long long NINF = -INF;
const long long MAXN = 1e+6 + 8;
int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};
long long t = 1;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
;
string S, C;
long long suff[5005];
void MAIN() {
cin >> S >> C;
long long szs = S.size(), szc = C.size();
char res = 'z';
long long ind = -1;
long long fg = 0;
suff[szs - 1] = szs - 1;
for (long long i = (szs - 2); i >= (0); i--) {
suff[i] = (S[i] < S[suff[i + 1]]) ? i : suff[i + 1];
}
long long ok = 0;
for (long long i = (0); i <= (szs - 1); i++) {
swap(S[i], S[suff[i]]);
if (S < C) {
ok = 1;
break;
}
swap(S[i], S[suff[i]]);
}
if (ok or S < C)
cout << S << "\n";
else
cout << "---\n";
tim:;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
cin >> t;
while (t--) {
MAIN();
}
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int H, W; cin >> H >> W;
int N; cin >> N;
vector<int> res;
for (int i = 0; i < N; i++) {
int a; cin >> a;
while (a--) {
res.push_back(i+1);
}
}
for (int i = 0; i < H; i++) {
vector<int> v(res.begin() + i * W, res.begin() + (i+1) * W);
if (i & 1) reverse(v.begin(), v.end());
for (int j = 0; j < W; j++) {
cout << v[j] << " \n"[j+1==W];
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using s64 = long long;
const int MaxN = 50;
const int L = 50;
int n;
int a[MaxN + 1], b[MaxN + 1];
bool chosen[L + 1];
inline bool validate(int k) {
chosen[k] = false;
s64 reach[L + 1];
for (int i = 0; i <= L; ++i) {
reach[i] = 1ll << i;
for (int j = 1; j <= i; ++j) {
if (chosen[j]) {
reach[i] |= reach[i % j];
}
}
}
for (int i = 0; i < n; ++i) {
if (~reach[a[i]] >> b[i] & 1) {
return false;
}
}
return true;
}
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
fill(chosen, chosen + L + 1, true);
s64 answer = 0;
for (int i = L; i >= 0; --i) {
if (!validate(i)) {
chosen[i] = true;
answer |= 1ll << i;
}
}
if (answer == (1ll << L + 1) - 1) {
answer = -1;
}
cout << answer << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int UNDEF = -1;
const long long INF = 1e18;
template <typename T>
inline bool chkmax(T &aa, T bb) {
return aa < bb ? aa = bb, true : false;
}
template <typename T>
inline bool chkmin(T &aa, T bb) {
return aa > bb ? aa = bb, true : false;
}
int rint();
char rch();
long long rlong();
const int mn = 1e5 + 4;
vector<pair<long long, long long> > v[2];
long long vs[mn], va[mn], vb[mn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n = rint(), sperp = rint();
long long alls = 0;
long long base = 0;
for (int i = 0; i < n; i++) {
vs[i] = rint(), va[i] = rint(), vb[i] = rint();
if (va[i] > vb[i]) {
v[0].push_back(make_pair(va[i] - vb[i], vs[i]));
} else if (vb[i] > va[i]) {
v[1].push_back(make_pair(vb[i] - va[i], vs[i]));
}
base += min(va[i], vb[i]) * (long long)vs[i];
alls += vs[i];
}
for (int t = 0; t < 2; t++) {
sort(v[t].begin(), v[t].end());
reverse(v[t].begin(), v[t].end());
}
long long wanta = 0;
for (auto &w : v[0]) {
wanta += w.second;
}
long long totp = (alls + sperp - 1) / sperp;
long long wantap = wanta / sperp;
long long final = 0;
for (long long canda = max(0ll, wantap - 2); canda <= min(totp, wantap + 2);
canda++) {
long long candb = totp - canda;
if (0 <= candb && candb <= totp) {
long long pa = canda * sperp, pb = candb * sperp;
long long gain = 0;
for (auto &w : v[0]) {
long long take = min(pa, w.second);
gain += take * (long long)w.first;
pa -= take;
}
for (auto &w : v[1]) {
long long take = min(pb, w.second);
gain += take * (long long)w.first;
pb -= take;
}
chkmax(final, gain);
}
}
final += base;
printf("%lld\n", final);
}
static char stdinBuffer[1024];
static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);
static const char *stdinPos = stdinDataEnd;
void readAhead(size_t amount) {
size_t remaining = stdinDataEnd - stdinPos;
if (remaining < amount) {
memmove(stdinBuffer, stdinPos, remaining);
size_t sz = fread(stdinBuffer + remaining, 1,
sizeof(stdinBuffer) - remaining, stdin);
stdinPos = stdinBuffer;
stdinDataEnd = stdinBuffer + remaining + sz;
if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;
}
}
int rint() {
readAhead(16);
int x = 0;
bool neg = false;
while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
if (*stdinPos == '-') {
++stdinPos;
neg = true;
}
while (*stdinPos >= '0' && *stdinPos <= '9') {
x *= 10;
x += *stdinPos - '0';
++stdinPos;
}
return neg ? -x : x;
}
char rch() {
readAhead(16);
while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
char ans = *stdinPos;
++stdinPos;
return ans;
}
long long rlong() {
readAhead(32);
long long x = 0;
bool neg = false;
while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
if (*stdinPos == '-') {
++stdinPos;
neg = true;
}
while (*stdinPos >= '0' && *stdinPos <= '9') {
x *= 10;
x += *stdinPos - '0';
++stdinPos;
}
return neg ? -x : x;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int Limit = 410000;
long long N;
int dp[111][Limit + 11];
int K, s[110];
long long go(int k, long long n) {
if (k == 0) return n;
if (n < Limit && dp[k][n] != -1) return dp[k][n];
long long res = go(k - 1, n) - go(k - 1, n / s[k - 1]);
if (n < Limit) dp[k][n] = res;
return res;
}
int main() {
cin >> N >> K;
for (int i = 0; i < K; ++i) cin >> s[i];
sort(s, s + K);
memset(dp, -1, sizeof(dp));
cout << go(K, N) << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long t;
cin >> t;
while (t--) {
unsigned long long n, k, a, b, i, x = 0;
cin >> n >> k;
if (n <= k)
cout << 1 << endl;
else {
for (i = 1; i * i <= n; i++)
if (n % i == 0) {
if (i <= k) x = max(x, i);
if ((n / i) <= k) x = max(x, n / i);
}
}
if (x == 1)
cout << n << endl;
else if (x > 1)
cout << n / x << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename t>
t in(t q) {
cin >> q;
return q;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "[";
for (int i = 0; i < ((int)size(v)); ++i) {
os << v[i];
if (i != ((int)size(v)) - 1) os << ",";
}
os << "]";
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v) {
for (auto it : v) os << "(" << it.first << ":" << it.second << ")";
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v) {
os << "(" << v.first << "," << v.second << ")";
return os;
}
const long double PI = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
inline long long rand(long long l, long long r) {
return uniform_int_distribution<long long>(l, r)(rng);
}
int p;
int i;
int xw;
long long ans = 0;
int j;
bool s1, s2;
int n, k;
struct node;
node* last;
struct node {
int sum;
node *lft, *rht;
node() {
sum = 0;
lft = NULL;
rht = NULL;
}
int query(int l) {
if (l == 2) return sum;
if (l) {
if (!rht) rht = new node();
last = rht;
if (!lft) return 0;
return lft->sum;
}
if (!lft) lft = new node();
last = lft;
if (!rht) return 0;
return rht->sum;
}
void update(int l, int r) {
if (l == r) {
sum++;
return;
}
int mid = (l + r) / 2;
if (xw <= mid) {
if (!lft) lft = new node();
lft->update(l, mid);
} else {
if (!rht) rht = new node();
rht->update(mid + 1, r);
}
sum++;
}
};
node* r = new node();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << setprecision(10);
cin >> n >> k;
xw = 0;
r->update(0, 1073741823);
for (i = 1; i < n + 1; ++i) {
cin >> p;
last = r;
p ^= xw;
for (j = 29; j >= 0; --j) {
s1 = ((1 << j) & k);
s2 = (1 << j) & p;
if (s1 && s2) {
if (!last->lft) break;
last = last->lft;
} else if (s1 && !s2) {
if (!last->rht) break;
last = last->rht;
} else if (s2) {
ans += last->query(1);
} else {
ans += last->query(0);
}
}
if (j == -1) ans += last->query(2);
xw = p;
r->update(0, 1073741823);
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
int a[101], b[101];
cin >> n >> m;
if (n == 1) a[0] = 1;
if (n == 2) a[0] = 3, a[1] = 4;
if (n > 2 && n % 2 == 0) {
for (int i = 0; i < n - 1; ++i) a[i] = 1;
a[n - 1] = (n - 2) / 2;
}
if (n > 2 && n % 2 != 0) {
a[0] = 2;
for (int i = 1; i < n - 1; ++i) a[i] = 1;
a[n - 1] = (n + 1) / 2;
}
if (m == 1) b[0] = 1;
if (m == 2) b[0] = 3, b[1] = 4;
if (m > 2 && m % 2 == 0) {
for (int i = 0; i < m - 1; ++i) b[i] = 1;
b[m - 1] = (m - 2) / 2;
}
if (m > 2 && m % 2 != 0) {
b[0] = 2;
for (int i = 1; i < m - 1; ++i) b[i] = 1;
b[m - 1] = (m + 1) / 2;
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) cout << a[i] * b[j] << " ";
cout << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n;
int h;
long long make_step(long long x, char d) {
int level = 0;
long long cur = x;
while (cur % 2 == 0) {
cur /= 2;
level++;
}
long long del_up = 1ll << level;
long long del_low = del_up / 2;
if (d == 'U') {
if (level == h) return x;
if ((cur - 1) % 4 == 0)
x += del_up;
else
x -= del_up;
return x;
}
if (level == 0) return x;
if (d == 'L')
x -= del_low;
else
x += del_low;
return x;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
while ((1ll << h) <= n) h++;
h--;
int q;
cin >> q;
while (q--) {
long long v;
cin >> v;
string s;
cin >> s;
for (char c : s) v = make_step(v, c);
cout << v << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long ans = 1;
void mul(int x) {
ans *= x;
ans %= mod;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < m; i++) {
cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for(int i = 1; i < n + 1; i++) {
if(a[i] == a[i - 1]) {
cout << 0 << endl;
return 0;
}
}
for(int i = 1; i < m + 1; i++) {
if(b[i] == b[i - 1]) {
cout << 0 << endl;
return 0;
}
}
int cnt = 0;
for(int i = m * n; i >= 1; i--) {
auto x = lower_bound(a.begin(), a.end(), i);
auto y = lower_bound(b.begin(), b.end(), i);
int xx = a.end() - x;
int yy = b.end() - y;
if(*x == i && *y == i) {
cnt++;
continue;
}
if(*x == i) {
mul(yy);
cnt++;
continue;
}
if(*y == i) {
mul(xx);
cnt++;
continue;
}
mul(xx * yy - cnt);
cnt++;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
array<int, 4> ct = {};
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
ct[(x / 2) % 2 * 2 + (y / 2) % 2]++;
}
long long ans = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
int even = (i == j) + (j == k) + (i == k);
if (even % 2 == 0) {
continue;
}
array<int, 3> c = {ct[i], ct[j] - (i == j),
ct[k] - (i == k) - (j == k)};
ans += 1LL * c[0] * c[1] * c[2];
}
}
}
assert(ans % 6 == 0);
ans /= 6;
cout << ans << '\n';
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int swap(int array[], int i, int j) {
int x = array[i];
array[i] = array[j];
array[j] = x;
}
int main() {
int n, arr[3005];
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
vector<int> v;
for (int i = 0; i < n; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) min = j;
}
if (min != i) {
swap(arr, i, min);
v.push_back(i);
v.push_back(min);
}
}
cout << v.size() / 2 << endl;
for (auto i = v.begin(); i != v.end(); i++) {
cout << *i << " ";
i++;
cout << *i << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mx = 1000005;
int p[mx], r[mx], c[mx], ans[mx], n, m;
vector<int> arr[mx];
vector<pair<int, int> > row[mx], col[mx], comp[mx];
vector<pair<int, pair<int, int> > > elements;
int get(int x, int y) { return x * m + y + 1; }
int find(int x) {
if (p[x] == x) return x;
return p[x] = find(p[x]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
p[v] = u;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
arr[i].resize(m);
row[i].resize(m);
}
for (int i = 0; i < m; i++) {
col[i].resize(n);
}
for (int i = 1; i <= n * m; i++) {
p[i] = i;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d ", &arr[i][j]);
elements.push_back(make_pair(arr[i][j], make_pair(i, j)));
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
row[i][j] = (make_pair(arr[i][j], j));
}
sort(row[i].begin(), row[i].end());
for (int j = 1; j < m; j++) {
if (row[i][j].first == row[i][j - 1].first)
merge(get(i, row[i][j].second), get(i, row[i][j - 1].second));
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
col[i][j] = (make_pair(arr[j][i], j));
}
sort(col[i].begin(), col[i].end());
for (int j = 1; j < n; j++) {
if (col[i][j].first == col[i][j - 1].first) {
merge(get(col[i][j].second, i), get(col[i][j - 1].second, i));
}
}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
comp[find(get(i, j))].push_back(make_pair(i, j));
sort(elements.begin(), elements.end());
for (int i = 0; i < elements.size(); i++) {
int ri = elements[i].second.first, ci = elements[i].second.second;
int idx = get(ri, ci);
int cc = find(idx);
if (cc == idx) {
int cur = 0;
for (int j = 0; j < comp[cc].size(); j++) {
cur = max(cur, max(r[comp[cc][j].first], c[comp[cc][j].second]));
}
for (int j = 0; j < comp[cc].size(); j++) {
r[comp[cc][j].first] = cur + 1;
c[comp[cc][j].second] = cur + 1;
}
ans[idx] = cur + 1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", ans[find(get(i, j))]);
}
printf("\n");
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
const long long mod = 1e9 + 7;
long long a[100005];
long long pown(long long x, long long y) {
long long res = 1, ans = x;
while (y) {
if (y & 1) res = res * ans % mod;
ans = ans * ans % mod;
y >>= 1;
}
return res;
}
int main(void) {
while (scanf("%d", &n) != EOF) {
long long cnt = 1;
long long x, ans;
ans = 2;
for (int i = 1; i <= n; i++) {
scanf("%lld", &x);
cnt = cnt * x % 10;
ans = pown(ans, x) % mod;
}
ans = ans * pown(2, mod - 2) % mod;
if (cnt % 2 == 0) {
printf("%lld/%lld\n", (ans + 1) * pown(3, mod - 2) % mod, ans);
} else {
printf("%lld/%lld\n", (ans - 1 + mod) * pown(3, mod - 2) % mod, ans);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int l;
cin >> l;
while (l--) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << n << " ";
}
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int A,B,C;
cin >> A >> B >> C;
if((A<C && C<B) || (A>C && C>B))
cout << "Yes";
else
cout << "No";
cout << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> tab[101];
vector<int> v[101];
int g[101] = {0};
void dfs(int n) {
g[n] = 1;
for (int i = 0; i < v[n].size(); i++)
if (!g[v[n][i]]) dfs(v[n][i]);
}
int main() {
int n, a, b, s, size = 1;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &s, &a, &b);
if (s == 1) {
tab[size] = make_pair(a, b);
size++;
for (int ta = 1; ta < size - 1; ta++) {
if ((tab[ta].first < tab[size - 1].first &&
tab[ta].second > tab[size - 1].first) ||
(tab[ta].first < tab[size - 1].second &&
tab[ta].second > tab[size - 1].second))
v[size - 1].push_back(ta);
if ((tab[size - 1].first < tab[ta].first &&
tab[size - 1].second > tab[ta].first) ||
(tab[size - 1].first < tab[ta].second &&
tab[size - 1].second > tab[ta].second))
v[ta].push_back(size - 1);
}
} else {
for (int ha = 0; ha < n; ha++) g[ha] = 0;
dfs(a);
if (g[b])
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}
| 5 |
#include <iostream>
#include <vector>
#include <algorithm>
using ll = long long;
int const nmax = (1 << 18);
int const threshold = (1 << 9);
int const lim = 100000;
int dp[1 + threshold][1 + lim];
int v[1 + nmax], weight[1 + nmax];
int query(int node, int val) {
if(node <= threshold)
return dp[node][val];
else {
if(weight[node] <= val)
return std::max(query(node / 2, val), query(node / 2, val - weight[node]) + v[node]);
else
return query(node / 2, val);
}
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int n;
std::cin >> n;
for(int i = 1;i <= n; i++)
std::cin >> v[i] >> weight[i];
for(int i = 1;i <= std::min(n, threshold); i++) {
for(int j = 0; j <= lim; j++)
dp[i][j] = dp[i / 2][j];
for(int j = weight[i]; j <= lim; j++)
dp[i][j] = std::max(dp[i][j], dp[i / 2][j - weight[i]] + v[i]);
}
int q;
std::cin >> q;
for(int i = 1;i <= q; i++) {
int node, val;
std::cin >> node >> val;
std::cout << query(node, val) << '\n';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void Solve() {
int i, j, k, m;
long long n;
scanf("%I64d", &n);
long long f1 = 1;
long long f2 = 2;
int games = 0;
while (f2 <= n) {
games++;
long long t = f2;
f2 += f1;
f1 = t;
}
printf("%d", games);
}
int main() {
Solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
if (n == 1)
cout << a[0] << "\n";
else {
long long int sum = 0;
int p = n;
while (p) {
for (int i = n - 1; i >= n - p; i--) sum = sum + a[i];
p = p / 4;
}
cout << sum << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
int n, k;
cin >> n >> k;
cout << (k != 0 && k != n ? 1 : 0) << " "
<< (k >= (n + 2) / 3 ? n - k : 2 * k) << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct Elem {
int a, b, c;
};
struct Benef {
int money, amount;
Benef(int m, int a) : money(m), amount(a) {}
bool operator<(const Benef &a) const { return money > a.money; }
};
int main(int narg, char **arg) {
int n, m, amount_total;
cin >> n >> m >> amount_total;
Elem elem[n][m];
for (int i = 0; i < n; i++) {
string str;
cin >> str;
for (int j = 0; j < m; j++) {
cin >> elem[i][j].a >> elem[i][j].b >> elem[i][j].c;
}
}
int maxim = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
vector<Benef> benef;
for (int k = 0; k < m; k++) {
if (elem[j][k].b - elem[i][k].a > 0 && elem[i][k].c > 0)
benef.push_back(Benef(elem[j][k].b - elem[i][k].a, elem[i][k].c));
}
sort(benef.begin(), benef.end());
int amount = 0, k = 0, earning = 0;
while (k < int(benef.size()) && amount < amount_total) {
if (benef[k].amount >= amount_total - amount) {
int rest = amount_total - amount;
amount += rest;
earning += benef[k].money * rest;
break;
} else {
amount += benef[k].amount;
earning += benef[k].amount * benef[k].money;
k++;
}
}
maxim = max(maxim, earning);
}
}
cout << maxim << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long n, m, l1[300000], l2[300000], x1[300000], aasdfasdfasdf[300000],
x2[300000], y2[300000];
long l, r, mid;
map<long, long> mapp;
vector<long> ys, uy;
vector<pair<pair<long, long>, pair<long, long> > > evs;
class Fenwick {
long long *m, *mt, N;
public:
Fenwick(long n);
Fenwick(long long a[], long n);
void add_range(long l, long long d);
void add_range(long l, long r, long long d);
long long sum(long r);
long long sum(long l, long r);
};
Fenwick::Fenwick(long n) {
N = n;
m = new long long[N];
mt = new long long[N];
for (int i = 0; i < n; i++) m[i] = mt[i] = 0;
}
void Fenwick::add_range(long r, long long d) {
if (r < 0) return;
for (int i = r; i >= 0; i = (i & (i + 1)) - 1) mt[i] += d;
for (int i = r | (r + 1); i < N; i |= i + 1)
m[i] += d * (r - (i & (i + 1)) + 1);
}
void Fenwick::add_range(long l, long r, long long d) {
add_range(r, d);
add_range(l - 1, -d);
}
long long Fenwick::sum(long r) {
if (r < 0) return 0;
long long res = 0;
for (int i = r; i >= 0; i = (i & (i + 1)) - 1)
res += m[i] + mt[i] * (i - (i & (i + 1)) + 1);
for (int i = r | (r + 1); i < N; i |= i + 1)
res += mt[i] * (r - (i & (i + 1)) + 1);
return res;
}
long long Fenwick::sum(long l, long r) { return sum(r) - sum(l - 1); }
bool solve(long val) {
Fenwick f(300000);
vector<long> ys;
for (int i = 1; i <= n; i++) {
if (l1[i] >= val * 2) {
ys.push_back(aasdfasdfasdf[i] + val);
ys.push_back(aasdfasdfasdf[i] + l1[i] - val);
}
}
for (int i = 1; i <= m; i++) {
if (l2[i] >= val * 2) ys.push_back(y2[i]);
}
mapp.clear();
sort(ys.begin(), ys.end());
uy.clear();
for (int i = 0; i < ys.size(); i++) {
if (i == 0 || ys[i] != ys[i - 1]) {
uy.push_back(ys[i]);
mapp[ys[i]] = uy.size();
}
}
evs.clear();
for (int i = 1; i <= n; i++) {
if (l1[i] >= val * 2) {
evs.push_back(make_pair(make_pair(x1[i], 1),
make_pair(mapp[aasdfasdfasdf[i] + val],
mapp[aasdfasdfasdf[i] + l1[i] - val])));
}
}
for (int i = 1; i <= m; i++) {
if (l2[i] >= val * 2) {
evs.push_back(make_pair(make_pair(x2[i] + val, 0),
make_pair(mapp[y2[i]], mapp[y2[i]])));
evs.push_back(make_pair(make_pair(x2[i] + l2[i] - val, 2),
make_pair(mapp[y2[i]], mapp[y2[i]])));
}
}
sort(evs.begin(), evs.end());
for (int i = 0; i < evs.size(); i++) {
if (evs[i].first.second != 1) {
f.add_range(evs[i].second.first, evs[i].second.second,
1 - evs[i].first.second);
} else {
long t = f.sum(evs[i].second.first, evs[i].second.second);
if (t > 0) return true;
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> x1[i] >> aasdfasdfasdf[i] >> l1[i];
}
for (int i = 1; i <= m; i++) {
cin >> x2[i] >> y2[i] >> l2[i];
}
l = 0;
r = 2e9;
while (l < r) {
long mid = l + r + 1;
mid /= 2;
if (solve(mid))
l = mid;
else
r = mid - 1;
}
cout << l << endl;
cin.get();
cin.get();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 5007;
const double eps = 0.00000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
return a > 0 ? a : -a;
}
template <typename T>
inline T powMM(T a, T b) {
T ret = 1;
for (; b; b >>= 1ll, a = a * a % M) ret = 1ll * ret * a % M;
return ret;
}
vector<int> edge[maxn];
vector<long long> dp1[maxn], dp2[maxn];
long long now1[maxn], now2[maxn];
long long a[maxn], b[maxn], m;
int num[maxn], ans;
void dfs(int x) {
num[x] = 1;
dp1[x].resize(maxn, INFF);
dp2[x].resize(maxn, INFF);
dp1[x][1] = a[x];
dp1[x][0] = 0;
dp2[x][1] = a[x] - b[x];
dp2[x][0] = INFF;
int i, j;
for (int v : edge[x]) {
dfs(v);
for (i = 0; i <= num[x]; i++) now1[i] = dp1[x][i], now2[i] = dp2[x][i];
for (i = 0; i <= num[x]; i++)
for (j = 1; j <= num[v]; j++) {
dp1[x][i + j] = min(dp1[x][i + j], now1[i] + dp1[v][j]);
dp2[x][i + j] = min(dp2[x][i + j], now2[i] + min(dp1[v][j], dp2[v][j]));
}
vector<long long>().swap(dp1[v]);
vector<long long>().swap(dp2[v]);
num[x] += num[v];
}
}
int n;
int i, j, k;
int main() {
scanf("%d%I64d", &n, &m);
for (i = 1; i <= n; i++) {
scanf("%I64d%I64d", &a[i], &b[i]);
if (i != 1) {
int v;
scanf("%d", &v);
edge[v].push_back(i);
};
}
dfs(1);
ans = 0;
for (i = 1; i <= n; i++)
if (dp1[1][i] <= m || dp2[1][i] <= m) ans = max(ans, i);
printf("%d", ans);
}
| 3 |
#include <iostream>
using namespace std;
int N;
long long int a[55];
int main(void) {
cin >> N;
long long int tmp;
for (int i = 1; i <= N; i++) {
cin >> tmp; a[i] = tmp;
}
int sig = 0;
int max_ab = 0;
int I;
for (int i = 1; i <= N; i++) {
if (abs(a[i]) >= max_ab) { max_ab = abs(a[i]); I = i; if (a[i] >= 0) { sig = 1; }
else { sig = -1; }
}
}
cout << 2 * N-1 << endl;
if (sig == 1) {
for (int i = 1; i <= N; i++) {
cout << I << " " << i << endl;
}
for (int i = 1; i <= N-1; i++) {
cout << i << " " << i + 1 << endl;
}
}
else {
for (int i = 1; i <= N; i++) {
cout << I << " " << i << endl;
}
for (int i = N; i >= 2; i--) {
cout << i << " " << i - 1 << endl;
}
}
//system("pause");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long solve(long long a, long long b) {
long long x = a + b;
vector<long long> divA;
for (long long i = 1; i * i <= a; i++) {
if (a % i == 0) divA.push_back(i);
}
long long res = 2 * x + 2;
long long j = -1;
int n = divA.size();
for (long long i = 1; i * i <= x; i++) {
if (x % i == 0) {
while (j + 1 < n && divA[j + 1] <= i) j++;
if (x / i >= a / divA[j]) {
res = min(res, 2 * ((x / i) + i));
}
}
}
return res;
}
int main() {
long long a, b;
cin >> a >> b;
cout << min(solve(a, b), solve(b, a));
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long a, b;
cin >> a >> b;
long long x = 1;
while (true) {
if ((x * (x + 1) / 2) > (a + b)) {
break;
}
x++;
}
x--;
vector<long long> A;
vector<long long> B;
long long k_a = 0;
long long k_b = 0;
while (x) {
if (a >= x) {
a -= x;
A.push_back(x);
x--;
k_a++;
} else if (b >= x) {
b -= x;
B.push_back(x);
x--;
k_b++;
} else {
break;
}
}
cout << k_a << endl;
for (long long i = 0; i < k_a; i++) {
cout << A[i] << ' ';
}
cout << endl;
cout << k_b << endl;
for (long long i = 0; i < k_b; i++) {
cout << B[i] << ' ';
}
cout << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long N = 505;
const long long mod = 1e9 + 7;
long long a[N][N], dp[N][N][2], n;
signed main() {
scanf("%lld", &n);
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= n; j++) {
scanf("%lld", &a[i][j]);
}
}
memset(dp, 0, sizeof(dp));
for (long long i = 1; i <= n; i++) dp[i][i][0] = dp[i][i][1] = 1;
for (long long i = n - 1; i >= 1; i--) {
for (long long j = i + 1; j <= n; j++) {
for (long long k = 1; k >= 0; k--) {
if (k) dp[i][j][k] = (dp[i][j][k] + dp[i + 1][j][0]) % mod;
for (long long l = i + 1; l <= j; l++) {
if (a[i][l] == 0) continue;
if (k && l == j) continue;
dp[i][j][k] = (dp[i][j][k] + dp[i][l][1] * dp[l][j][k]) % mod;
}
}
}
}
printf("%lld", dp[1][n][0]);
return 0;
}
| 6 |
#include<bits/stdc++.h>
#define ll int64_t
#define pp pair<int,int>
#define ppn pair<pp,int>
#define F first
#define S second
#define N 1000005
#define mod 1000000007
#define pi 3.14159265358979323846
using namespace std;
vector<int>v(5000001),l(5000001);
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// map<ll,ll>v,m;
int t;
int n;
string a, b, k;
cin>>t;
while(t--)
{
cin>>a>>b;
int ans = 0;
map<string,int>m;
for(int i=0; i<a.length(); i++)
{
k = a[i];
m[k]=1;
for(int j=i+1; j<a.length(); j++)
{
k+=a[j];
m[k]=j-i+1;
//cout<<k<<endl;
}
}
for(int i=0; i<b.length(); i++)
{
k = b[i];
if(m[k])ans=max(ans,m[k]);
for(int j=i+1; j<b.length(); j++)
{
k+=b[j];
if(m[k])ans=max(ans,m[k]);
}
}
// cout<<ans<<endl;
cout<<a.length()+b.length()-(2*ans)<<endl;
}
}
| 3 |
#include<iostream>
#include<cstdio>
using namespace std;
#define rep(i, n)for((i) = 0;(i) < (n);++(i))
#define ma 1001001
int res[ma];
bool isp[ma];
int main(void){
int i, j, n, m;
rep(i, ma){
res[i] = 1;
isp[i] = true;
}
isp[0] = false; isp[1] = false; res[0] = 0;
for(int i = 2;i < ma;++i){
if(!isp[i])continue;
for(j = i + i;j < ma;j+=i)isp[j] = false;
if(i > 5){
for(j = i;j < ma;j+=i)res[j] = 0;
}
}
rep(i, ma - 1)res[i + 1] += res[i];
for(;;){
scanf("%d%d", &m, &n);
if(m == 0)break;
printf("%d\n", res[n] - res[m - 1]);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-11;
const long double PI = 3.1415926535897932384626433832795;
int main() {
cout << setiosflags(ios::fixed) << setprecision(8);
long long n, x, y;
cin >> n >> x >> y;
if (n > y || (y - (n - 1)) * (y - (n - 1)) + n - 1 < x)
cout << -1 << endl;
else {
cout << y - (n - 1) << endl;
for (int i = 1; i < n; i++) {
puts("1");
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, h, l, r;
int a[2000 + 5], dp[2000 + 5][2000 + 5];
int solve(int curr, int idx) {
if (idx == n) {
if (l <= curr && curr <= r)
return 1;
else
return 0;
}
if (dp[curr][idx] != -1) return dp[curr][idx];
int p1 = 0, p2 = 0;
if (l <= curr && curr <= r) {
p1 = 1;
p2 = 1;
}
p1 += solve((curr + a[idx]) % h, idx + 1);
p2 += solve((curr + a[idx] - 1) % h, idx + 1);
return dp[curr][idx] = max(p1, p2);
}
int main(int argc, const char* argv[]) {
scanf("%d %d %d %d", &n, &h, &l, &r);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i <= h; i++) {
for (int j = 0; j <= n; j++) {
dp[i][j] = -1;
}
}
int k = solve(a[0], 1);
for (int i = 0; i <= h; i++) {
for (int j = 0; j <= n; j++) {
dp[i][j] = -1;
}
}
k = max(k, solve(a[0] - 1, 1));
printf("%d\n", k);
return 0;
}
| 5 |
#include <bits/stdc++.h>
int diru[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dirv[] = {-1, 0, 1, -1, 1, -1, 0, 1};
using namespace std;
class Debugger {
public:
Debugger(const string& _separator = " ")
: first(true), separator(_separator) {}
template <typename ObjectType>
Debugger& operator,(const ObjectType& v) {
if (!first) cerr << separator;
cerr << v;
first = false;
return *this;
}
~Debugger() { cerr << endl; }
private:
bool first;
string separator;
};
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
return os << "(" << p.first << "," << p.second << ")";
}
template <typename T>
inline ostream& operator<<(ostream& os, const vector<T>& v) {
bool first = true;
os << "{";
for (unsigned int i = 0; i < v.size(); i++) {
if (!first) os << ",";
os << v[i];
first = false;
}
return os << "}";
}
template <typename T>
inline ostream& operator<<(ostream& os, const set<T>& v) {
bool first = true;
os << "{";
for (typename set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
if (!first) os << ",";
os << *ii;
first = false;
}
return os << "}";
}
template <typename T1, typename T2>
inline ostream& operator<<(ostream& os, const map<T1, T2>& v) {
bool first = true;
os << "{";
for (typename map<T1, T2>::const_iterator ii = v.begin(); ii != v.end();
++ii) {
if (!first) os << ",";
os << *ii;
first = false;
}
return os << "}";
}
template <class T>
T sq(T n) {
return n * n;
}
template <class T>
T gcd(T a, T b) {
return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
return (a / gcd<T>(a, b) * b);
}
template <class T>
bool inside(T a, T b, T c) {
return a <= b && b <= c;
}
template <class T>
void setmax(T& a, T b) {
if (a < b) a = b;
}
template <class T>
void setmin(T& a, T b) {
if (b < a) a = b;
}
template <class T>
T power(T N, T P) {
return (P == 0) ? 1 : N * power(N, P - 1);
}
int in[100010];
int M[100010];
pair<int, int> val[100010];
int n;
bool isluc(int v) {
while (v) {
if (v % 10 != 4 && v % 10 != 7) return 0;
v /= 10;
}
return 1;
}
bool issorted() {
int i;
for (i = 1; i < n; i++)
if (in[i - 1] > in[i]) return 0;
return 1;
}
vector<pair<int, int> > ans;
int main() {
int T, t = 1, m, i, j, k, v;
scanf("%d", &n);
v = -1;
for (i = 0; i < n; i++) {
scanf("%d", &in[i]);
if (isluc(in[i])) {
v = i;
}
val[i] = pair<int, int>(in[i], i);
}
if (issorted()) {
puts("0");
return 0;
}
if (v == -1) {
puts("-1");
return 0;
}
sort(val, val + n);
for (i = 0; i < n; i++) M[i] = in[i] = i;
for (i = 0; i < n; i++) {
if (in[i] == val[i].second || val[i].second == v) {
continue;
}
k = val[i].second;
m = in[i];
swap(in[M[v]], in[M[m]]);
swap(M[v], M[m]);
if (M[v] != M[m]) ans.push_back(pair<int, int>(M[v], M[m]));
swap(in[M[v]], in[M[k]]);
swap(M[v], M[k]);
if (M[v] != M[k]) ans.push_back(pair<int, int>(M[v], M[k]));
}
printf("%d\n", ((int)ans.size()));
for (i = 0; i < ((int)ans.size()); i++)
printf("%d %d\n", ans[i].first + 1, ans[i].second + 1);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch - '0' < 0 || ch - '0' > 9) {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch - '0' >= 0 && ch - '0' <= 9) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m, cnt, N;
char s[205];
int id[205][205];
int head[205 * 205], nxt[205 * 205 * 2], to[205 * 205 * 2], tot;
void add(int u, int v) {
tot++;
nxt[tot] = head[u];
head[u] = tot;
to[tot] = v;
}
int book[205 * 205];
int vis[205 * 205][205], f[205 * 205][205];
void addx(int &x, int y) { x = (x + y) % 10007; }
int ans;
int dfs(int x, int c) {
if (c < 0) return 0;
if (vis[x][c]) return f[x][c];
vis[x][c] = 1;
for (int i = head[x]; i; i = nxt[i]) addx(f[x][c], dfs(to[i], c - book[x]));
return f[x][c];
}
int b[205 * 2][205 * 2], a[205 * 2][205 * 2];
void mul(int A[205 * 2][205 * 2], int B[205 * 2][205 * 2],
int C[205 * 2][205 * 2]) {
int tmp[205 * 2][205 * 2];
memset(tmp, 0, sizeof(tmp));
for (int i = 1; i <= N; i++) {
for (int k = i; k <= N; k++) {
for (int j = k; j <= N; j++) addx(tmp[i][j], A[i][k] * B[k][j]);
}
}
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++) C[i][j] = tmp[i][j];
}
void quick_pow(int p) {
for (int i = 1; i <= N; i++) a[i][i] = 1;
while (p) {
if (p & 1) mul(a, b, a);
mul(b, b, b);
p >>= 1;
}
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
m = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) id[i][j] = ++cnt;
cnt++;
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
if (s[i] == s[j] && j - i <= 1)
add(cnt, id[i][j]);
else if (s[i] == s[j])
add(id[i + 1][j - 1], id[i][j]);
else {
add(id[i][j - 1], id[i][j]);
add(id[i + 1][j], id[i][j]);
book[id[i][j]] = 1;
}
}
}
for (int i = 0; i < n; i++) vis[id[1][n]][i] = 1;
f[id[1][n]][book[id[1][n]]] = 1;
for (int i = 0; i < n; i++) dfs(cnt, i);
int nx = (n + 1) / 2;
for (int i = 1; i <= n - 1; i++) b[i][i] = 24;
for (int i = 1; i < n + nx - 1; i++) b[i][i + 1] = 1;
for (int i = n; i < n + nx; i++) b[i][i + nx] = 1, b[i][i] = 25;
for (int i = n + nx; i < n + 2 * nx; i++) b[i][i] = 26;
N = n - 1 + 2 * nx;
quick_pow((n + m) / 2);
for (int i = 0; i < n; i++) {
int x = (n - i + 1) / 2;
addx(ans, f[cnt][i] * a[n - i][n + nx + x - 1]);
}
if ((n + m) & 1) {
ans = ans * 26 % 10007;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n; j++) {
int x = (n - j + 1) / 2;
addx(ans, f[id[i][i]][j] * a[n - j][n + x - 1]);
}
}
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long dp[2][2][31][31][31], n, h, mod = 1000000009;
int main() {
memset(dp, 0, sizeof dp);
cin >> n >> h;
for (int i = 0; i < 2; i++) {
for (int d1 = 0; d1 <= h; d1++) {
for (int d2 = 0; d2 <= h; d2++) {
for (int d3 = 0; d3 <= h; d3++) {
dp[(n + 1) % 2][i][d1][d2][d3] = (i || d1 || d2 || d3);
}
}
}
}
for (int i = n; i >= 1; i--) {
for (int alive = 0; alive < 2; alive++) {
for (int d1 = 0; d1 <= h; d1++) {
for (int d2 = 0; d2 <= h; d2++) {
for (int d3 = 0; d3 <= h; d3++) {
dp[i % 2][alive][d1][d2][d3] = 0;
dp[i % 2][alive][d1][d2][d3] +=
dp[(i + 1) % 2][alive][(!d1 || d1 == h) ? 0 : (d1 + 1)]
[(!d2 || d2 == h) ? 0 : (d2 + 1)]
[(!d3 || d3 == h) ? 0 : (d3 + 1)];
dp[i % 2][alive][d1][d2][d3] +=
dp[(i + 1) % 2][d1 || i < h][!alive || alive == h ? 0 : 2]
[(!d2 || d2 == h) ? 0 : (d2 + 1)]
[(!d3 || d3 == h) ? 0 : (d3 + 1)];
dp[i % 2][alive][d1][d2][d3] +=
dp[(i + 1) % 2][d2 || i < h][!alive || alive == h ? 0 : 2]
[(!d1 || d1 == h) ? 0 : (d1 + 1)]
[(!d3 || d3 == h) ? 0 : (d3 + 1)];
dp[i % 2][alive][d1][d2][d3] +=
dp[(i + 1) % 2][d3 || i < h][!alive || alive == h ? 0 : 2]
[(!d1 || d1 == h) ? 0 : (d1 + 1)]
[(!d2 || d2 == h) ? 0 : (d2 + 1)];
dp[i % 2][alive][d1][d2][d3] %= mod;
}
}
}
}
}
cout << dp[1][1][1][1][1] << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
const unsigned int Max_K = 5005;
const unsigned int Mod = 1000000007;
unsigned long long int Power(unsigned long long int Base, unsigned int Exp) {
unsigned long long int res = 1;
for (; Exp; Base = Base * Base % Mod, Exp >>= 1)
if (Exp & 1) res = res * Base % Mod;
return res;
}
unsigned int N, K;
unsigned int Pcnt, Prime[Max_K];
bool is_comp[Max_K];
unsigned int Kth_Power[Max_K], Inv[Max_K], BinomialF[Max_K], BinomialR[Max_K];
int main(int argc, char **argv) {
scanf("%u %u", &N, &K);
Kth_Power[0U] = K == 0, BinomialF[0U] = 1U, BinomialR[0U] = 1U;
Kth_Power[1U] = 1U, Inv[1U] = 1U, BinomialF[1U] = N, BinomialR[1U] = N - K;
for (unsigned int i = 2U; i <= K; ++i) {
if (!is_comp[i]) {
Prime[Pcnt++] = i;
Kth_Power[i] = Power(i, K);
Inv[i] = static_cast<unsigned long long int>(Mod - Mod / i) *
Inv[Mod % i] % Mod;
}
for (unsigned int j = 0U; j != Pcnt && i * Prime[j] <= K; ++j) {
is_comp[i * Prime[j]] = true;
Kth_Power[i * Prime[j]] =
static_cast<unsigned long long int>(Kth_Power[i]) *
Kth_Power[Prime[j]] % Mod;
Inv[i * Prime[j]] =
static_cast<unsigned long long int>(Inv[i]) * Inv[Prime[j]] % Mod;
if (i % Prime[j] == 0U) break;
}
BinomialF[i] = BinomialF[i - 1] * (N - i + 1ULL) % Mod * Inv[i] % Mod;
BinomialR[i] = BinomialR[i - 1] * (N - K - 1ULL + i) % Mod * Inv[i] % Mod;
}
unsigned int Ans = 0U;
if (N <= K) {
for (unsigned int i = 0U; i <= N; ++i)
Ans = (Ans +
static_cast<unsigned long long int>(BinomialF[i]) * Kth_Power[i]) %
Mod;
} else {
unsigned int coef = 1U, sbinom = 0U;
for (unsigned int i = 0U; i <= K; ++i)
sbinom =
(sbinom + BinomialF[i] * static_cast<unsigned long long int>(coef)) %
Mod,
coef = coef * ((Mod - 1ULL) / 2U) % Mod;
unsigned int Power2 = Power(2U, N);
for (unsigned int i = 0U; i <= K; ++i) {
Ans = (Ans + static_cast<unsigned long long int>(BinomialF[i]) *
Kth_Power[i] % Mod * Power2 % Mod * sbinom) %
Mod;
coef = coef * (Mod - 2ULL) % Mod;
sbinom =
(sbinom + Mod -
static_cast<unsigned long long int>(coef) * BinomialR[K - i] % Mod) *
2U % Mod;
Power2 = Power2 * ((Mod + 1ULL) / 2U) % Mod;
}
}
printf("%u", Ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin >> s;
long long n = s.size();
map<char, long long> mp, next;
map<string, long long> ans;
for (long long i = 0; i < n; i++) next[s[i]]++;
for (long long i = 0; i < n; i++) {
mp[s[i]]++;
next[s[i]]--;
if (next[s[i]] == 0) next.erase(s[i]);
for (char j = 'a'; j <= 'z'; j++) {
string x = "";
x += s[i];
x += j;
ans[x] += next[j];
}
}
long long mx = 0;
for (auto x : mp) mx = max(mx, x.second);
for (auto x : ans) mx = max(mx, x.second);
cout << mx << "\n";
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double SUCK = 999999999999999999.99;
const int MX = 51 * 101;
int n;
int r;
int f[55];
int s[55];
int p[55];
double cp[55];
double dp[55][MX];
bool isOk(double det) {
for (int i = 0; i <= r; i++) {
dp[n][i] = 0;
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= r; j++) {
dp[i][j] = det;
double togo = 0;
if (j + f[i] > r) {
togo = det;
} else {
togo += cp[i] * (f[i] + dp[i + 1][j + f[i]]);
if (j + s[i] > r)
togo += (1 - cp[i]) * (s[i] + det);
else
togo += (1 - cp[i]) * (s[i] + dp[i + 1][j + s[i]]);
}
dp[i][j] = min(det, togo);
}
}
if (dp[0][0] >= det) return false;
return true;
}
const double eps = 0.000000001;
time_t bg;
double getTime() {
time_t en = clock();
double ret = (double)(en - bg) / CLOCKS_PER_SEC;
return ret;
}
double bsearch(double l, double r) {
double m = (l + r) / 2;
if (r - l < eps) return m;
if (getTime() >= 1.999) return m;
if (isOk(m))
return bsearch(l, m);
else
return bsearch(m, r);
}
int main() {
bg = clock();
scanf("%d%d", &n, &r);
for (int i = 0; i < n; i++) scanf("%d%d%d", &f[i], &s[i], &p[i]);
for (int i = 0; i < n; i++) cp[i] = 0.010000 * p[i];
double ans = bsearch(n, 50000000);
printf("%.12f", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
/* -------------------------------- Template -------------------------------- */
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> T inf() { assert(false); }
template<> constexpr int inf<int>() { return 1e9; }
template<> constexpr ll inf<ll>() { return 1e18; }
template<> constexpr ld inf<ld>() { return 1e30; }
/* -------------------------------- Library -------------------------------- */
struct Trie {
static const int SIZE = 32;
struct State {
int index, next[SIZE];
State(int index) : index(index) { memset(next, -1, sizeof(next)); }
};
vector<State> pma;
vector<int> cnt;
vector<int> acc;
void dfs(int v) {
int res = 0;
REP(i,SIZE) {
int nv = pma[v].next[i];
if (nv != -1) {
dfs(nv);
res += cnt[nv];
}
}
cnt[v] += res;
}
Trie(const vector<string> &str) {
pma.clear();
pma.push_back(State(0));
cnt.clear();
cnt.push_back(0);
acc.clear();
acc.push_back(0);
REP(i,str.size()) {
int t = 0;
for (char cc : str[i]) {
int c = cc - 'a';
if (pma[t].next[c] == -1) {
int m = pma.size();
pma[t].next[c] = m;
pma.push_back(State(m));
cnt.push_back(0);
acc.push_back(0);
}
t = pma[t].next[c];
}
++cnt[t];
++acc[t];
}
dfs(0);
}
pair<vector<vector<int>>,int> query(const string &str) {
vector<vector<int>> table(26, vector<int>(26, 0));
int res = 1;
int st = 0;
REP(i,str.size()) {
res += acc[st];
REP(j,SIZE) {
int nst = pma[st].next[j];
if (nst != -1)
table[str[i] - 'a'][j] += cnt[nst];
}
st = pma[st].next[str[i] - 'a'];
}
return make_pair(table, res);
}
};
/* ---------------------------------- Main ---------------------------------- */
int main() {
int N, Q;
cin >> N;
vector<string> strs(N);
REP(i,N) cin >> strs[i];
Trie trie(strs);
vector<pair<vector<vector<int>>,int>> pre(N);
REP(i,N) pre[i] = trie.query(strs[i]);
cin >> Q;
REP(i,Q) {
int k; string ord;
cin >> k >> ord; --k;
int res = pre[k].second;
REP(i,26) REP(j,i) {
int from = ord[i] - 'a';
int to = ord[j] - 'a';
res += pre[k].first[from][to];
}
cout << res << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
string name, ip;
map<string, string> s1;
while (cin >> n >> m) {
for (int i = 0; i < n; i++) {
cin >> name >> ip;
ip = ip + ";";
s1[ip] = name;
}
for (int i = 0; i < m; i++) {
cin >> name >> ip;
cout << name << " " << ip << " ";
cout << "#" << s1[ip] << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
using namespace std;
inline long long read() {
long long s = 0, f = 1;
char c = getchar();
;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
;
}
while (c >= '0' && c <= '9') {
s = s * 10 + c - '0';
c = getchar();
;
}
return s * f;
}
const int N = 1e6 + 5;
const int M = 2048;
const long long Inf = 1ll << 60;
int n, m, U, w[N];
long long K, D, Ans, a[N], p[12], dp[12][M];
unordered_map<long long, vector<int>> mp;
unordered_map<long long, vector<long long>> H;
vector<int> G[N];
struct cmp {
bool operator()(int x, int y) { return w[x] < w[y]; }
};
priority_queue<int, vector<int>, cmp> q;
long long Gcd(long long a, long long b) { return !b ? a : Gcd(b, a % b); }
int Ins(int x) {
if (q.size() < m)
return q.push(x), 1;
else if (w[q.top()] > w[x])
return q.pop(), q.push(x), 1;
return 0;
}
int main() {
n = read(), K = read();
for (int i = 1; i <= n; i++) a[i] = read(), D = !D ? a[i] : Gcd(D, a[i]);
for (int i = 1; i <= n; i++) w[i] = read();
for (int i = 2; 1ll * i * i <= D; i++)
if (D % i == 0) {
p[m++] = i;
while (D % i == 0) D /= i;
}
if (D > 1) p[m++] = D;
U = (1 << m) - 1;
if (m == 0) return printf("0"), 0;
for (int i = 1; i <= n; i++) {
long long res = 1, tp = 1;
for (int j = 0; j < m; j++)
while (a[i] % p[j] == 0) a[i] /= p[j], res *= p[j];
mp[a[i] = res].push_back(i);
if (!H[a[i]].size()) {
vector<long long> F;
F.resize(U + 1);
for (int j = 0; j < m; j++) {
F[1 << j] = 1;
while (res % p[j] == 0) res /= p[j], F[1 << j] *= p[j];
}
F[0] = 1;
for (int S = 1; S <= U; S++) F[S] = F[S - (S & -S)] * F[S & -S];
H[a[i]] = F;
}
}
for (auto T : mp) {
vector<int> F;
for (auto t : T.second) Ins(t);
while (q.size()) F.push_back(q.top()), q.pop();
reverse(F.begin(), F.end()), mp[T.first] = F;
}
for (int S = 1; S <= U; S++) {
for (auto T : mp) {
if (H[T.first][S] > K) continue;
for (auto t : T.second)
if (!Ins(t)) break;
}
while (q.size()) G[q.top()].push_back(S), q.pop();
}
for (int i = 0; i <= m; i++)
for (int S = 0; S <= U; S++) dp[i][S] = Inf;
Ans = Inf, dp[0][0] = 0;
for (int i = 1; i <= n; i++)
if (G[i].size())
for (int j = m; j >= 1; j--)
for (auto S : G[i]) {
int C = U ^ S;
for (int T = C;; (--T) &= C) {
dp[j][T | S] = min(dp[j][T | S], dp[j - 1][T] + w[i]);
if (!T) break;
}
}
for (int i = 0; i <= m; i++)
if (dp[i][U] < Inf) Ans = min(Ans, dp[i][U] * i);
printf("%lld", Ans < Inf ? Ans : -1);
return 0;
}
| 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.