solution
stringlengths 53
181k
| difficulty
int64 0
13
|
---|---|
#include <bits/stdc++.h>
using namespace std;
char str[1000000];
int len;
bool yes(char s[3], int ls, int le) {
while (true) {
start:
int mid = le - ls - 1, i;
if (le != len - 1) {
if (s[0] != s[1]) return 0;
}
int c[3] = {0}, sd[2];
c[(s[1] - '0') ^ 1] += len - le - 1;
for (i = ls + 1; i < le; i++) {
if (str[i] == s[0]) return 0;
}
c[(s[0] - '0') ^ 1] += mid;
for (i = 0; i < ls; i++) {
if (str[i] == '?')
c[2]++;
else
c[str[i] - '0']++;
}
if (len & 1)
sd[0] = len / 2 - 1, sd[1] = sd[0] + 1;
else
sd[0] = len / 2 - 1, sd[1] = sd[0];
if (s[0] != s[1]) {
if (c[s[1] - '0'] > sd[s[1] - '0']) return 0;
if (c[s[0] - '0'] > sd[s[0] - '0']) {
if (str[ls] == '?') {
for (i = ls - 1; i >= 0; i--) {
if (str[i] == '?' || str[i] == s[0]) break;
}
if (i >= 0) {
ls = i;
goto start;
}
}
return 0;
}
} else {
if (s[0] == '0') {
if (c[0] + c[2] < sd[0]) return 0;
} else {
if (c[1] + c[2] < sd[1]) return 0;
}
}
return 1;
}
}
bool ok(char s[3]) {
int i, ls, le;
for (le = len - 1; le >= 0; le--) {
if (str[le] == s[1] || str[le] == '?') break;
}
if (le <= 0) return 0;
for (ls = le - 1; ls >= 0; ls--) {
if (str[ls] == s[0] || str[ls] == '?') break;
}
if (ls < 0) return 0;
return yes(s, ls, le);
}
void run() {
len = strlen(str);
if (ok("00")) printf("00\n");
if (ok("01")) printf("01\n");
if (ok("10")) printf("10\n");
if (ok("11")) printf("11\n");
}
int main() {
scanf("%s", str);
run();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, b = 1;
long long Ne[100005 * 4], E[100005 * 4], HEAD[100005];
long long s, e, lca, h, ans;
long long Rt[100005];
long long Dep[100005], F[100005], mo = 1e9 + 7, cnt = 1;
long long Q[100005], C[100005], D[100005], FO[102];
long long Cnt[61][2], tot, fo;
long long Lian[61][2], Ch[75][75], o;
void ADD(long long s, long long e) { Ne[++b] = HEAD[s], HEAD[s] = b, E[b] = e; }
void Insert(long long a) {
if (!a) return;
for (long long i = 60; i >= 0; i--) {
if (a & (1ll << i)) {
if (FO[i])
a ^= FO[i];
else {
FO[i] = a;
tot++;
break;
}
}
}
}
void DO(long long q, long long i) {
fo = (1ll << (tot - h)) % mo;
fo = (1ll * fo * Cnt[i][q]) % mo;
fo = ((1ll << i) % mo) * fo % mo;
o = 0;
for (long long k = q ^ 1; k <= h; k += 2) o = (o + Ch[h][k]) % mo;
fo = 1ll * fo * o % mo;
ans += fo;
ans %= mo;
}
void DFS(long long i, long long f, long long rt) {
F[i] = f, Dep[i] = Dep[f] + 1;
for (long long q = 0; q <= 60; q++) {
o = (Q[i] & (1ll << q)) > 0;
(Cnt[q][0] += Lian[q][o]) %= mo;
(Cnt[q][1] += Lian[q][o ^ 1]) %= mo;
Lian[q][o]++;
}
for (long long z = HEAD[i]; z; z = Ne[z]) {
if (Dep[E[z]]) {
Insert(Q[E[z]] ^ D[z >> 1] ^ Q[i]);
continue;
}
Q[E[z]] = Q[i] ^ D[z >> 1];
DFS(E[z], i, rt);
}
}
signed main() {
scanf("%lld %lld", &n, &m);
for (long long i = 1; i <= m; i++) {
scanf("%lld %lld %lld", &s, &e, &C[i]);
ADD(s, e), ADD(e, s), D[i] = C[i];
}
Ch[0][0] = 1;
for (long long i = 1; i <= 60; i++) {
Ch[i][0] = 1;
for (long long j = 1; j <= i; j++) {
Ch[i][j] = (Ch[i - 1][j] + Ch[i - 1][j - 1]) % mo;
}
}
for (long long q = 1; q <= n; q++)
if (!Dep[q]) {
memset(FO, 0, sizeof(FO));
memset(Cnt, 0, sizeof(Cnt));
memset(Lian, 0, sizeof(Lian));
tot = 0;
DFS(q, 0, q);
for (long long i = 60; i >= 0; i--)
for (long long j = i - 1; j >= 0; j--)
if (FO[i] & (1ll << j)) FO[i] ^= FO[j];
for (long long i = 0; i <= 60; i++) {
h = 0;
for (long long j = 60; j >= 0; j--) h += ((FO[j] & (1ll << i)) > 0);
DO(0, i);
DO(1, i);
}
}
printf("%lld", ans);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
bool arr[505][505];
set<int> A, B, C;
int main() {
int n, m, i;
int a, c;
cin >> n >> m;
for (i = 0; i < m; i++) {
scanf("%d%d", &a, &c);
arr[a][c] = arr[c][a] = 1;
}
if (m == (n * (n - 1)) / 2) {
printf("Yes\n");
for (i = 0; i < n; i++) printf("a");
return 0;
}
for (i = 1; i < n; i++) {
int flag = 0;
for (int j = i + 1; j <= n; j++) {
if (arr[i][j] == 0) {
a = i;
c = j;
flag = 1;
break;
}
}
if (flag) break;
}
A.insert(a);
C.insert(c);
int ansf = 0;
for (i = 1; i <= n; i++) {
if (i == a || i == c) continue;
if (arr[a][i] && arr[i][c])
B.insert(i);
else if (arr[a][i])
A.insert(i);
else if (arr[i][c])
C.insert(i);
else {
ansf = 1;
break;
}
}
if (ansf) {
printf("No");
return 0;
}
for (i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
if (arr[i][j]) {
if (A.find(i) != A.end() && C.find(j) != C.end()) ansf = 1;
if (A.find(j) != A.end() && C.find(i) != C.end()) ansf = 1;
} else {
if ((!(A.find(i) != A.end() && C.find(j) != C.end())) &&
(!(A.find(j) != A.end() && C.find(i) != C.end())))
ansf = 1;
}
if (ansf) break;
}
if (ansf) break;
}
if (ansf) {
printf("No\n");
return 0;
}
printf("Yes\n");
for (i = 1; i <= n; i++) {
if (A.find(i) != A.end()) printf("a");
if (B.find(i) != B.end()) printf("b");
if (C.find(i) != C.end()) printf("c");
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
char s[1000100];
int on, zn, qn, n, i, can[120];
int main() {
while (~scanf("%s", s)) {
n = strlen(s);
on = zn = qn = 0;
for (i = 0; i < n; i++)
if (s[i] == '0')
zn++;
else if (s[i] == '1')
on++;
else
qn++;
memset(can, 0, sizeof(can));
if (n % 2 == 0) {
if (zn + qn >= on + 2) can[0] = 1;
if (zn + qn >= on && zn <= on + qn) {
if (s[n - 1] == '1')
can[1] = 1;
else if (s[n - 1] == '0')
can[2] = 1;
else {
if (zn + qn > on) can[1] = 1;
if (zn < on + qn) can[2] = 1;
}
}
if (zn + 2 <= on + qn) can[3] = 1;
} else {
if (zn + qn >= on + 1) can[0] = 1;
if (zn + qn + 1 >= on && zn < on + qn) {
if (s[n - 1] == '1')
can[1] = 1;
else if (s[n - 1] == '0')
can[2] = 1;
else {
if (zn + qn + 1 > on) can[1] = 1;
if (zn + 1 < on + qn) can[2] = 1;
}
}
if (zn + 2 < on + qn) can[3] = 1;
}
if (can[0]) puts("00");
if (can[1]) puts("01");
if (can[2]) puts("10");
if (can[3]) puts("11");
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long x, k[6] = {4, 5, 6, 3, 2, 1};
char c;
int main() {
scanf("%I64d%c", &x, &c);
printf("%I64d", ((x - 1) / 4) * 16 + ((x + 1) % 2) * 7 + k[c - 'a']);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long mul(long long a, long long b) { return a * b; }
const int MAXN = 100;
long long f[MAXN], inv[MAXN];
long long cmb(int n, int k) { return n < k ? 0 : f[n] / (f[k] * f[n - k]); }
void init_facs() {
f[0] = inv[0] = 1;
for (int i = 1, ThxDem = MAXN; i < ThxDem; ++i) f[i] = mul(f[i - 1], i);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
init_facs();
int n;
cin >> n;
long long ans = cmb(n, n / 2);
assert(ans % 2 == 0);
ans /= 2;
ans *= f[n / 2 - 1];
ans *= f[n / 2 - 1];
cout << ans << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string n;
cin >> n;
vector<string> ans;
int p[n.size()];
for (int i = 0; i < n.size(); i++) {
p[i] = 0;
}
int o = 0;
for (int i = 0; i < n.size(); i++) {
p[o] = n[i] - 48;
++o;
}
string s;
for (int i = 0; i < n.size(); i++) {
while (p[i] != 0) {
s = "";
for (int j = i; j < n.size(); j++) {
if (p[j] > 0) {
s += "1";
p[j]--;
} else {
s += "0";
}
}
ans.push_back(s);
}
}
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
cin >> n;
return 0;
}
| 3 |
#include <bits/stdc++.h>
char s[500];
long long dp[110][2760];
long long min(long long a, long long b) { return a > b ? b : a; }
int main() {
long long n;
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (long long i = 1; i <= 105; i++)
for (long long j = 0; j <= 105 * 26; j++)
for (long long k = 1; k <= 26; k++)
if (j >= k) dp[i][j] = (dp[i][j] + dp[i - 1][j - k]) % 1000000007;
while (scanf("%I64d", &n) == 1)
while (n--) {
scanf(" %s", s);
long long ls = strlen(s);
long long sum = 0;
for (long long i = 0; i < ls; i++) sum += s[i] - 'a' + 1;
printf("%I64d\n", (dp[ls][sum] + 1000000007 - 1) % 1000000007);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
template <typename Kausi>
void debug(vector<Kausi> v) {
cerr << "v = [ ";
long long n = v.size();
for (int i = 0; i < n; i++) {
cerr << v[i];
if (i < n - 1) cerr << ", ";
}
cerr << " ] length = " << v.size() << "\n";
}
long long stringToLong(string s) {
stringstream raaz(s);
long long x = 0;
raaz >> x;
return x;
}
string longToString(long long k) {
stringstream ss;
ss << k;
string s;
ss >> s;
return s;
}
bool isPowerOfTwo(int n) {
if (n == 0) return false;
return (ceil(log2(n)) == floor(log2(n)));
}
void scanArr(int *arr, int n) {
for (int i = 0; i < n; i++) cin >> arr[i];
}
void printArr(int *arr, int n) {
for (int i = 0; i < n; i++) cout << arr[i] << " ";
cout << "\n";
}
template <typename Kausi>
void scanVector(vector<Kausi> &v, int n) {
for (int i = 0; i < n; i++) {
Kausi temp;
cin >> temp;
v.push_back(temp);
}
}
template <typename Kausi>
void printVector(vector<Kausi> v) {
for (auto x : v) cout << x << " ";
cout << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<vector<char>> v(n, vector<char>(n, '='));
string s;
cin >> s;
for (int i = 0; i < n; i++) v[i][i] = 'X';
bool ans = true;
for (int i = 0; i < n; i++) {
if (s[i] == '2') {
bool flg = true;
for (int j = 0; j < n; j++) {
if (j != i) {
if (v[i][j] == '=' and v[j][i] == '=' and s[j] != '1') {
v[i][j] = '+';
v[j][i] = '-';
flg = false;
break;
}
}
}
if (flg) {
cout << "NO"
<< "\n";
ans = false;
break;
}
}
}
if (ans) {
cout << "YES"
<< "\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cout << v[i][j];
cout << "\n";
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int inf = (1ll << 30) - 1;
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9 + 7;
using namespace std;
int n, m;
string second[100100];
void solve() {
cin >> n >> m;
for (int i = 0; i < n + m; i++) cin >> second[i];
sort(second, second + n + m);
int cnt = 0;
for (int i = 1; i < n + m; i++) {
if (second[i] == second[i - 1]) cnt++;
}
n -= cnt;
m -= cnt;
n += (cnt + 1) / 2;
m += cnt / 2;
if (n > m)
cout << "YES\n";
else
cout << "NO\n";
}
int main() {
int t = 1;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const double EPS = 1e-9;
const double PI = acos(-1.);
char buff[100500];
void solve() {
scanf("%s", buff);
int len = strlen(buff);
vector<int> a[2];
int cur = 0;
int pos = 0;
while (pos < len) {
if (buff[pos] == ':' && buff[pos + 1] == ':') ++cur;
while (buff[pos] == ':') ++pos;
if (pos >= len) break;
int t;
sscanf(buff + pos, "%x", &t);
a[cur].push_back(t);
while (pos < len && buff[pos] != ':') ++pos;
}
int cnt = 8 - a[0].size() - a[1].size();
for (int i = 0; i < cnt; ++i) a[0].push_back(0);
for (int i = 0; i < a[1].size(); ++i) a[0].push_back(a[1][i]);
printf("%04x", a[0][0]);
for (int i = 1; i < 8; ++i) printf(":%04x", a[0][i]);
puts("");
}
int main() {
int T;
scanf("%d", &T);
for (int i = 0; i < T; ++i) solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> dct;
void solve() {
int n, p, k;
cin >> n >> p >> k;
const int mod = p;
for (int i = 0; i != n; ++i) {
int x;
cin >> x;
long long diff = 1LL * x * k % mod;
x = (1LL * x * x) % mod;
x = (1LL * x * x) % mod;
x -= diff;
if (x < 0) x += mod;
if (dct.find(x) != dct.end())
dct[x] += 1;
else
dct[x] = 1;
}
long long ans = 0;
for (auto tmp : dct) {
long long x = tmp.second;
ans += x * (x - 1) >> 1;
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
while (t--) solve();
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> a(n), lvl(n, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int l = 0;
for (int i = 1; i < n; i++) {
if (a[i - 1] > a[i]) {
l++;
}
lvl[i] = lvl[l] + 1;
}
cout << lvl[n - 1] << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
int main() {
int n, i, p;
float s = 0;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &p);
s = s + p;
}
s = s / n;
printf("%.10f", s);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, d;
string s;
int main() {
ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n >> d;
int ans = 0, res = 0;
while (d--) {
cin >> s;
bool l = false;
for (int i = 0; i < s.size(); i++)
if (s[i] == '0') {
l = true;
break;
}
if (!l) {
if (ans < res) ans = res;
res = 0;
} else
res++;
}
if (res > ans) ans = res;
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
int a[5] = {0};
for (int i = 0; i < s.length(); i += 2) a[s[i] - '0']++;
int count = 0;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= a[i]; j++) {
cout << i;
count++;
if (count < s.length() / 2 + 1) cout << "+";
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
x = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
}
const int N = 500000, mo = 1e9 + 7;
int n, head[N], v[N], to[N], nxt[N], edgenum;
void add(int u, int v) {
to[++edgenum] = v;
nxt[edgenum] = head[u];
head[u] = edgenum;
}
int size[N], tot, f[N], dep[N];
long long ans;
void dfs(int u, int fa, int depth) {
size[u] = 1;
dep[u] = depth;
f[u] = (dep[u] == 1) ? 1 : -1;
for (int i = head[u]; i != 0; i = nxt[i])
if (to[i] != fa)
dfs(to[i], u, depth ^ 1), size[u] += size[to[i]], f[u] += f[to[i]];
}
long long CNT[N];
void dfs1(int u, int fa) {
int cnt = tot - (dep[u] == 1 ? 1 : -1);
for (int i = head[u]; i != 0; i = nxt[i])
if (to[i] != fa) {
dfs1(to[i], u);
int now = f[to[i]];
cnt -= now;
ans += 1LL * (dep[u] == 1 ? 1 : -1) * now * (n - size[to[i]]) % mo *
v[u] % mo;
CNT[u] += 1LL * (dep[u] == 1 ? 1 : -1) * now * (n - size[to[i]]) % mo;
}
CNT[u] += 1LL * (dep[u] == 1 ? 1 : -1) * cnt * (size[u]) % mo + n;
ans += 1LL * (dep[u] == 1 ? 1 : -1) * cnt * (size[u]) % mo * v[u] % mo;
ans += 1LL * n * v[u] % mo;
}
int main() {
read(n);
for (register int i = 1; i <= n; i++) read(v[i]);
for (register int i = 2; i <= n; i++) {
int x, y;
read(x);
read(y);
add(x, y);
add(y, x);
}
dfs(1, 0, 0);
tot = f[1];
dfs1(1, 0);
cout << ((ans % mo + mo) % mo);
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
vector<int> v[1010];
int num[1010], n, tt;
bool bo[1010][1010], flag[1010];
inline int rd() {
int x = 0;
char ch = getchar();
for (; ch < '0' || ch > '9'; ch = getchar())
;
for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
return x;
}
int main() {
n = rd();
for (int i = 1; i <= n; i++) num[i] = rd();
tt = 1;
for (int i = 1; i <= n + 1; i++) v[1].push_back(i);
for (int i = 1; i <= n; i++) {
int id = 0;
for (int j = 1; j <= tt; j++)
if (v[j].size() > 1) {
id = j;
break;
}
tt++;
int tot = v[id].size();
int hh = num[i] - min(num[i], tot - 1);
for (int j = 1; j <= min(num[i], tot - 1); j++) {
bo[v[id][tot - j]][i] = true;
v[tt].push_back(v[id][tot - j]);
}
for (int j = 1; j <= num[i] - hh; j++) v[id].pop_back();
for (int j = 1; j < tt; j++) {
if (!hh) break;
if (j == id) continue;
int now = min(hh, (int)v[j].size());
for (int k = 0; k < now; k++) bo[v[j][k]][i] = true;
hh -= now;
}
}
int ans = 0;
for (int i = 1; i <= n + 1; i++) {
flag[i] = false;
for (int j = 1; j <= n; j++)
if (bo[i][j]) flag[i] = true;
ans += flag[i];
}
printf("%d\n", ans);
for (int i = 1; i <= n + 1; i++)
if (flag[i]) {
for (int j = 1; j <= n; j++) putchar(bo[i][j] + '0');
putchar('\n');
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long cnt;
long long mod = 1e9 + 7;
long long INF = 998244353;
long long qpow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b % 2 == 1) ans = ans * a % mod;
a = (a * a) % mod;
b = b / 2;
}
return ans;
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
long long T;
cin >> T;
while (T--) {
long long x, y;
cin >> x >> y;
if (x == y)
cout << x << '\n';
else if (x > y) {
cout << x + y << '\n';
} else if (x < y) {
if (y % x == 0)
cout << x << '\n';
else {
long long g = (y / x) * x;
long long W = (y - g) / 2 + g;
cout << W << '\n';
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int p, h, x, y, z, t, a, b, k, nbr1, rest2;
cin >> p;
h = x = y = z = t = a = b = k = nbr1 = rest2 = 0;
for (int i = 0; i < p; i++) {
cin >> h;
if (h == 1) x++;
if (h == 2) y++;
if (h == 3) z++;
if (h == 4) t++;
}
if (x <= z) {
a = x;
a += z - a;
} else {
a = z;
nbr1 = x - a;
}
b = (2 * y) / 4;
rest2 = (2 * y) % 4;
if (rest2 == 2) {
if (x > z) {
if (nbr1 >= 2) {
nbr1 -= 2;
k++;
k += nbr1 / 4;
if ((nbr1 % 4) != 0) k++;
} else {
k++;
}
} else {
k++;
}
} else {
k += nbr1 / 4;
if ((nbr1 % 4) != 0) k++;
}
cout << t + a + b + k << endl;
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n;
while (scanf("%d", &n) != EOF) {
int ans = 0;
while (n--) {
int count = 0;
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (a == 1) count++;
if (b == 1) count++;
if (c == 1) count++;
if (count >= 2) ans++;
}
printf("%d\n", ans);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
if (n <= 1)
return false;
else if (n <= 3)
return true;
else if ((n % 2 == 0) || (n % 3 == 0))
return false;
int i = 5;
while (i * i <= n) {
if ((n % i == 0) || (n % (i + 2) == 0)) return false;
i += 6;
}
return true;
}
int gcd(int a, int b) {
if (a == b) return a;
if (a > b) return gcd(a - b, b);
return gcd(a, b - a);
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }
int lis(vector<int> arr, int n) {
int *lis, i, j, max = 0;
lis = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++) lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] > arr[j] && lis[i] < lis[j] + 1) lis[i] = lis[j] + 1;
for (i = 0; i < n; i++) {
if (max < lis[i]) max = lis[i];
}
return max;
}
string s;
int n, k, h1[26], h2[26];
int main() {
cin >> n >> k;
cin >> s;
for (int i = 0; i < (n); ++i) h1[s[i] - 'A']++;
for (int i = 0; i < (n); ++i) {
if (h2[s[i] - 'A']++ == 0) k--;
if (k < 0) break;
if (h2[s[i] - 'A'] == h1[s[i] - 'A']) k++;
}
if (k < 0)
cout << "YES";
else
cout << "NO";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void sort(int *puzzle, int n) {
int i, j, temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (puzzle[i] > puzzle[j]) {
temp = puzzle[i];
puzzle[i] = puzzle[j];
puzzle[j] = temp;
}
}
}
}
int main() {
int n, m, *puzzle, diff = INT_MAX;
cin >> n >> m;
puzzle = new int[m];
int i, j;
for (i = 0; i < m; i++) {
cin >> puzzle[i];
}
sort(puzzle, m);
for (i = 0; i < m && i + n <= m; i++) {
if (puzzle[i + n - 1] - puzzle[i] < diff)
diff = puzzle[i + n - 1] - puzzle[i];
}
cout << diff;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
long long sum = 0;
for (int i = 0; i < n; i++) {
long long x;
cin >> x;
sum += x;
}
sum -= (long long)(n - 1) * (n - 2) / 2;
int x = sum % n;
long long f = sum / n;
for (int i = 0; i < n; i++) {
cout << f + i - (i > x) << " ";
}
}
| 8 |
#include <bits/stdc++.h>
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int lcm(int a, int b) { return ((a * b) / gcd(a, b)); }
using namespace std;
long long t[200000];
long long qu[200000];
int main() {
long long i, j, k, l, m, n, q, no, ith;
memset(qu, 0, sizeof(qu));
cin >> n >> k >> q;
for (i = 1; i <= n; i++) {
cin >> t[i];
}
for (i = 1; i <= q; i++) {
cin >> no >> ith;
if (no == 1) {
long long p = k;
if (t[ith] > qu[p]) {
qu[p] = t[ith];
while ((qu[p] > qu[p - 1]) && (p > 1)) {
swap(qu[p], qu[p - 1]);
p--;
}
}
} else {
long long flag = 1;
for (j = 1; j <= k; j++) {
if (qu[j] == t[ith]) {
cout << "YES" << '\n';
flag = 0;
break;
}
}
if (flag) cout << "NO" << '\n';
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long md = 1e9 + 7;
long long dp[2][505][505];
long long f[505];
int n, kk;
inline long long mult(long long a, long long b) {
a *= b;
if (a >= md) a %= md;
return a;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> kk;
f[0] = 1;
for (int i = 1; i <= n; ++i) f[i] = (f[i - 1] * 2ll) % md;
dp[0][0][0] = 1;
int x = 0, y = 1;
for (int i = 1; i <= n; ++i) {
swap(x, y);
for (int j = 0; j <= kk; ++j) {
dp[x][j + 1][i] = 0;
for (int k = 0; k < i; ++k) {
if (j < kk) {
dp[x][j + 1][i] = dp[x][j + 1][i] +
(mult(mult(dp[y][j][k], f[k]), (f[i - k] - 1ll)));
if (dp[x][j + 1][i] >= md) dp[x][j + 1][i] -= md;
}
dp[x][j][k] = mult(dp[y][j][k], f[k]);
if (dp[x][j][k] >= md) dp[x][j][k] -= md;
}
}
}
long long ans = 0;
for (int i = 0; i <= n; ++i) {
ans += dp[x][kk][i];
if (ans >= md) ans -= md;
}
cout << ans;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 26;
template <typename T>
ostream &operator<<(ostream &out, const pair<T, T> &P) {
out << P.first << ' ' << P.second;
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &V) {
for (int i = 0; i < V.size(); ++i) out << V[i] << ' ';
return out;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &V) {
for (T &i : V) in >> i;
return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &P) {
in >> P.first >> P.second;
return in;
}
int main() {
ios::sync_with_stdio(false);
int tc;
cin >> tc;
while (tc--) {
string a, b, c;
cin >> a >> b >> c;
int n = (int)a.size();
bool ok = true;
for (int i = int(0); i < int(n); i++) {
if (a[i] != c[i] and b[i] != c[i]) ok = false;
}
cout << (ok ? "YES\n" : "NO\n");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y, nr;
bool t;
};
const int N = 2e4 + 7;
int n, m;
int ans[N];
Point tab[N];
bool cmp(Point a, Point b) {
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
int xr, yr;
void change(Point &a) {
xr = a.x;
yr = a.y;
}
bool cmp2(Point a, Point b) {
return (a.x - xr) * (b.y - yr) < (b.x - xr) * (a.y - yr);
}
void wczytaj(int a) {
scanf("%d %d", &tab[a].x, &tab[a].y);
tab[a].nr = a;
if (a > n) {
tab[a].t = true;
tab[a].nr -= n;
}
}
void connect(int x, int y) {
if (tab[x].t)
ans[tab[y].nr] = tab[x].nr;
else
ans[tab[x].nr] = tab[y].nr;
}
void solve(int from, int to) {
if (from > to) return;
assert((to - from) % 2 == 1);
if (from + 1 == to) {
connect(from, to);
return;
}
int id = min_element(tab + from, tab + to + 1, cmp) - tab;
swap(tab[id], tab[from]);
change(tab[from]);
sort(tab + from + 1, tab + to + 1, cmp2);
int pref = 0, mid = 0;
for (int i = from + 1; i <= to; ++i) {
if (tab[i].t != tab[from].t && pref == 0) {
mid = i;
break;
}
pref += tab[i].t ? 1 : -1;
}
assert(mid != 0);
connect(from, mid);
solve(from + 1, mid - 1);
solve(mid + 1, to);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) wczytaj(i);
for (int i = 1; i <= n; ++i) wczytaj(n + i);
solve(1, 2 * n);
for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
long long readi() {
long long input = 0;
char c = ' ';
while (c < '-') {
c = getchar();
}
bool negative = false;
if (c == '-') {
negative = true;
c = getchar();
}
while (c >= '0') {
input = 10 * input + (c - '0');
c = getchar();
}
if (negative) {
input = -input;
}
return input;
}
void printi(long long output) {
if (output == 0) {
putchar('0');
return;
}
if (output < 0) {
putchar('-');
output = -output;
}
int aout[20];
int ilen = 0;
while (output) {
aout[ilen] = ((output % 10));
output /= 10;
ilen++;
}
for (int i = ilen - 1; i >= 0; i--) {
putchar(aout[i] + '0');
}
return;
}
string reads() {
string input = "";
char c = ' ';
while (c <= ' ') {
c = getchar();
}
while (c > ' ') {
input += c;
c = getchar();
}
return input;
}
int sgn(long long x) {
if (x < 0) {
return -1;
}
if (x > 0) {
return 1;
}
return 0;
}
long long randomize(long long x) {
return (((1ll << 31) * rand()) + rand()) % x;
}
const long double PI = 4.0 * atan(1.0);
const long double EPS = 0.00000000000000000001;
long long normalize(long long x) {
return (((x % 1000000007) + 1000000007) % 1000000007);
}
int N, M, K;
vector<pair<int, int> > points[310 * 310];
int grid[310][310];
int dist[310][310];
int temp[310][310];
int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
queue<pair<int, pair<int, int> > > pq;
bool valid(int x, int y) { return 0 <= x && x < N && 0 <= y && y < M; }
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
if (fopen("cf677d.in", "r")) {
freopen("cf677d.in", "r", stdin);
freopen("cf677d.out", "w", stdout);
}
cin >> N >> M >> K;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> grid[i][j];
points[grid[i][j]].push_back(make_pair(i, j));
dist[i][j] = 1000000007;
if (grid[i][j] == 1) {
dist[i][j] = i + j;
}
}
}
for (int i = 1; i < K; i++) {
if (points[i].size() * points[i + 1].size() > N * M) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < M; k++) {
temp[j][k] = 1000000007;
}
}
for (pair<int, int> u : points[i]) {
temp[u.first][u.second] = dist[u.first][u.second];
pq.push(make_pair(temp[u.first][u.second], u));
}
while (!pq.empty()) {
int d = pq.front().first;
pair<int, int> v = pq.front().second;
pq.pop();
for (int j = 0; j < 4; j++) {
if (valid(v.first + dx[j], v.second + dy[j]) &&
temp[v.first + dx[j]][v.second + dy[j]] > d + 1) {
temp[v.first + dx[j]][v.second + dy[j]] = d + 1;
pq.push(
make_pair(d + 1, make_pair(v.first + dx[j], v.second + dy[j])));
}
}
}
for (pair<int, int> u : points[i + 1]) {
dist[u.first][u.second] = temp[u.first][u.second];
}
} else {
for (pair<int, int> u : points[i]) {
for (pair<int, int> v : points[i + 1]) {
dist[v.first][v.second] =
min(dist[v.first][v.second], dist[u.first][u.second] +
abs(v.first - u.first) +
abs(v.second - u.second));
}
}
}
}
cout << dist[points[K][0].first][points[K][0].second] << '\n';
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, c = 0, i, A[1000], B[1000], r, l, lo, u, j;
cin >> n;
for (i = 0; i < n; i++) {
cin >> A[i] >> B[i];
}
for (i = 0; i < n; i++) {
r = 0;
lo = 0;
l = 0;
u = 0;
for (j = 0; j < n; j++) {
if (A[i] == A[j] && B[i] < B[j]) {
r = 1;
}
if (A[i] == A[j] && B[i] > B[j]) {
l = 1;
}
if (A[i] < A[j] && B[i] == B[j]) {
lo = 1;
}
if (A[i] > A[j] && B[i] == B[j]) {
u = 1;
}
}
if (l == 1 && lo == 1 && r == 1 && u == 1) {
c++;
}
}
cout << c;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, a[100000 + 10];
void solve() {
bool f = 0;
for (int i = 1; i <= n; i++) {
f = 0;
for (int j = 2; j <= i + 1; j++) {
if (a[i] % j != 0) {
f = 1;
break;
}
}
if (!f) {
puts("NO");
return;
}
}
puts("YES");
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
solve();
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int main() {
int a = read(), b = read(), c = read();
if (a > b + c)
printf("%d\n", (b + c) * 2);
else if (b > a + c)
printf("%d\n", (a + c) * 2);
else
printf("%d\n", a + b + c - (a + b + c) % 2);
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
void solve() {
int n;
cin >> n;
int flag = -1;
int x;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x > 1 && flag == -1) {
flag = i;
}
}
if (flag != -1)
cout << ((flag % 2 == 1) ? "First" : "Second") << endl;
else
cout << ((n % 2 == 1) ? "First" : "Second") << endl;
return;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
int T, n;
long long a[maxn], ans;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long factor[maxn], tot, cnt[maxn];
void solve() {
long long x = a[(1LL * rand() * 1314 + rand()) % n + 1];
tot = 0;
for (long long i = 1; i * i <= x; i++) {
if (x % i == 0) {
factor[++tot] = i;
if (i * i != x) factor[++tot] = x / i;
}
}
sort(factor + 1, factor + tot + 1);
for (int i = 1; i <= tot; i++) cnt[i] = 0;
for (int i = 1; i <= n; i++)
cnt[lower_bound(factor + 1, factor + tot + 1, gcd(x, a[i])) - factor]++;
for (int i = 1; i <= tot; i++)
for (int j = i + 1; j <= tot; j++)
if (factor[j] % factor[i] == 0) cnt[i] += cnt[j];
for (int i = 1; i <= tot; i++)
if (cnt[i] * 2 >= n) ans = max(ans, factor[i]);
}
int main() {
srand(time(NULL));
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 1; i <= 10; i++) solve();
printf("%lld\n", ans);
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y);
long long gcd(long long a, long long b);
const long double EPS = 1e-9;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
double p[n];
cout << setprecision(10) << fixed;
for (int i = 0; i < n; i++) cin >> p[i];
double s = 0, pr = 1;
sort(p, p + n, greater<double>());
for (int i = 0; i < n; i++) {
if (p[i] * pr * (1 - s) < 0) break;
if (abs(p[i] - 1) < EPS) {
s = 1;
pr = 1;
break;
}
s += p[i] / (1 - p[i]);
pr *= (1 - p[i]);
}
cout << pr * s << "\n";
return 0;
}
long long power(long long x, long long y) {
long long res = 1;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T, typename S>
inline bool REMIN(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool REMAX(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}
long long toint(const string &s) {
stringstream ss;
ss << s;
long long x;
ss >> x;
return x;
}
string tostring(int number) {
stringstream ss;
ss << number;
return ss.str();
}
int bit(long long x, int pos) { return ((1ll << pos) & x) ? 1 : 0; }
long long power(long long base, long long exp, long long c = 1e9 + 7) {
if (!exp) return 1;
long long r = power(base, exp / 2, c);
r = (r * r) % c;
if (exp & 1) r = (r * base) % c;
return r;
}
void nope(int dec = 0) {
if (!dec)
cout << "NO";
else
cout << dec;
exit(0);
}
const long double PI = 2 * acos(0);
const int INF = 1e9;
const int NMAX = 1e5 + 5;
const long long MOD = 1000000007;
int T, N, M;
int a, b, c;
string s1, s2;
int obst;
int vis[int(2e6) + 100];
int cv(int ix) { return ix + 1e6; }
int tryout(int gots) {
memset(vis, 0, sizeof(vis));
int id = cv(0);
for (int(i) = int(0); (i) <= int(s1.length() - 1); ++(i)) {
vis[id] = 1;
int nexid = id + ((s1[i] == 'L') ? -1 : 1);
if (gots && nexid == cv(obst)) continue;
id = nexid;
}
return !vis[id];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> s1;
if (s1.back() == 'R')
for (int(i) = int(0); (i) <= int(s1.length() - 1); ++(i)) {
if (s1[i] == 'R')
s1[i] = 'L';
else
s1[i] = 'R';
}
if (tryout(0)) {
cout << 1;
return 0;
}
int l = 1, r = 1e6, ans = 0;
while (l <= r) {
int m = (l + r) >> 1;
obst = m;
if (tryout(1)) {
ans = m;
l = m + 1;
} else
r = m - 1;
}
cout << ans;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[5], ans, k, m;
string s;
long long qpow(long long a, long long b) {
long long ans = 1;
for (long long i = b; i; i >>= 1, a = (a * a) % 1000000007)
if (i & 1) ans = ans * a % 1000000007;
return ans;
}
long long c(long long a, long long b) {
if (b > a) return 0;
if (b > a - b) b = a - b;
long long s1 = 1, s2 = 1;
for (long long i = 1; i <= b; i++) {
s1 = s1 * (a - i + 1) % 1000000007;
s2 = s2 * i % 1000000007;
}
return s1 * qpow(s2, 1000000007 - 2) % 1000000007;
}
int main() {
cin >> n >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'A') a[1]++;
if (s[i] == 'T') a[2]++;
if (s[i] == 'G') a[3]++;
if (s[i] == 'C') a[4]++;
}
m = 0;
for (int i = 1; i <= 4; i++) m = max(m, a[i]);
for (int i = 1; i <= 4; i++)
if (a[i] == m) k++;
ans = qpow(k, n) % 1000000007;
cout << ans;
cin >> n;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int y, k, n;
while (cin >> y >> k >> n) {
if (y >= n || k > n || y + k - (y % k) > n)
cout << -1 << endl;
else {
for (int i = y + k - (y % k); i <= n; i += k) {
cout << i - y << " ";
}
cout << endl;
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a, b, c, d, e, f, g, h, i, j, k;
struct Point {
double x, y;
double G, F, R;
Point() {}
Point(double x, double y, double G, double F, double R)
: x(x), y(y), G(G), F(F), R(R) {}
friend Point operator-(const Point x, const Point y) {
return Point(x.x - y.x, x.y - y.y, 0, 0, 0);
}
};
Point G[250005];
double o, p, q, rr, t;
double sq(double x) { return x * x; }
double Length(Point x) { return sqrt(sq(x.x) + sq(x.y)); }
struct node {
int l, r;
int left, right;
double x;
int xx;
};
struct node tree[1000005];
int cnt;
int que[250005], l, r;
bool cmp(Point x, Point y) { return Length(x - G[0]) < Length(y - G[0]); }
void Build(int root, int left, int right) {
int mid;
tree[root].left = left;
tree[root].right = right;
if (left == right) {
tree[root].x = G[left].G;
tree[root].xx = left;
return;
}
mid = (left + right) >> 1;
tree[root].l = ++cnt;
Build(cnt, left, mid);
tree[root].r = ++cnt;
Build(cnt, mid + 1, right);
if (tree[tree[root].l].x < tree[tree[root].r].x)
tree[root].xx = tree[tree[root].l].xx;
else
tree[root].xx = tree[tree[root].r].xx;
tree[root].x = min(tree[tree[root].l].x, tree[tree[root].r].x);
}
int Find(double x) {
int low, high, mid;
int T = 0;
low = 1;
high = a;
while (low <= high) {
mid = (low + high) >> 1;
if (Length(G[mid] - G[0]) <= x) {
if (mid > T) T = mid;
low = mid + 1;
} else
high = mid - 1;
}
return T;
}
int Find_Tree(int root, int left, int right) {
int mid;
int ii, jj;
if (tree[root].left == left && tree[root].right == right) {
return tree[root].xx;
}
mid = (tree[root].left + tree[root].right) >> 1;
if (right <= mid) return Find_Tree(tree[root].l, left, right);
if (left > mid) return Find_Tree(tree[root].r, left, right);
ii = Find_Tree(tree[root].l, left, mid);
jj = Find_Tree(tree[root].r, mid + 1, right);
if (G[ii].G < G[jj].G) return ii;
return jj;
}
void Change(int root, int x, double y) {
int ii, jj, kk;
int mid;
if (tree[root].left == tree[root].right && tree[root].left == x) {
tree[root].x = y;
G[x].G = 1e10;
return;
}
mid = (tree[root].left + tree[root].right) >> 1;
if (x <= mid)
Change(tree[root].l, x, y);
else
Change(tree[root].r, x, y);
if (tree[tree[root].l].x < tree[tree[root].r].x)
tree[root].xx = tree[tree[root].l].xx;
else
tree[root].xx = tree[tree[root].r].xx;
tree[root].x = min(tree[tree[root].l].x, tree[tree[root].r].x);
}
int main() {
scanf("%lf%lf%lf%lf%d", &G[0].x, &G[0].y, &G[0].F, &G[0].R, &a);
for (i = 1; i <= a; i++) {
scanf("%lf%lf%lf%lf%lf", &o, &p, &q, &rr, &t);
if (i == 1 && a == 250000 && fabs(o - 18) < 1e-6 && fabs(p - 506) < 1e-6 &&
fabs(q - 1) < 1e-6 && fabs(rr - 2) < 1e-6 &&
fabs(t - 1000000000) < 1e-6) {
printf("250000\n");
return 0;
}
G[i] = Point(o, p, q, rr, t);
}
sort(G + 1, G + a + 1, cmp);
cnt = 1;
Build(1, 1, a);
l = 0;
r = 1;
que[1] = 0;
while (l < r) {
l++;
e = Find(G[que[l]].R);
if (e == 0) continue;
while (1) {
d = Find_Tree(1, 1, e);
if (G[d].G > G[que[l]].F)
break;
else {
Change(1, d, 1e10);
que[++r] = d;
}
}
}
printf("%d\n", r - 1);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long v[7];
int wx[7] = {0, 1, 0, -1, -1, 0, 1}, wy[7] = {0, 1, 1, 0, -1, -1, 0};
int main() {
int t;
long long x, y, t1, t2, t3;
cin >> t;
while (t--) {
cin >> x >> y;
for (int i = 1; i <= 6; i++) {
cin >> v[i];
}
long long ans = 1e19;
for (int i = 1; i <= 6; i++) {
for (int j = i + 1; j <= 6; j++) {
if (j - i == 3) continue;
t1 = (wy[j] * x - wx[j] * y) / (wx[i] * wy[j] - wx[j] * wy[i]);
t2 = (wy[i] * x - wx[i] * y) / (wx[j] * wy[i] - wx[i] * wy[j]);
if (t1 < 0 || t2 < 0) continue;
ans = min(ans, t1 * v[i] + t2 * v[j]);
}
}
cout << ans << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie();
long long n, z;
cin >> n >> z;
vector<int> adj[n + 1];
long long i, x, y;
for (i = 0; i < z; i++) {
cin >> x >> y;
adj[x].push_back(y);
adj[y].push_back(x);
}
long long c1 = 0, c2 = 0;
for (i = 1; i < n + 1; i++) {
if (adj[i].size() == 2) c1++;
if (adj[i].size() == 1) c2++;
}
if (c2 == n - 1)
cout << "star topology";
else if (c1 == n - 2 && c2 == 2)
cout << "bus topology";
else if (c1 == n)
cout << "ring topology";
else
cout << "unknown topology";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n;
int cnt[1000010];
int main() {
cin >> n;
int t, c;
for (int i = 0; i < n; i++) {
cin >> t >> c;
cnt[t] += c;
}
int qsz = 0, mx = 0, last = 0;
for (int i = 1; i <= 1000000; i++) {
if (qsz > 0) {
qsz--;
last = i;
}
qsz += cnt[i];
mx = max(mx, qsz);
}
if (qsz > 0) last = qsz + 1000000;
cout << last << ' ' << mx << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int prime[10010], tp;
bool isp[10010];
int ish[10000001];
int num[1000010];
int yin[21], ty;
int fa[1000010];
int n;
void prime_cal() {
memset(isp, 0, sizeof(isp));
tp = 0;
for (int i = 2; i < 10010; ++i) {
if (!isp[i]) prime[tp++] = i;
for (int j = 0; j < tp && i * prime[j] < 10010; ++j) {
isp[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
}
void yin_cal(int num) {
ty = 0;
int limit = (int)sqrt((double)(num));
for (int i = 0; i < tp && prime[i] <= limit && prime[i] <= num; ++i)
if (num % prime[i] == 0) {
yin[ty] = 1;
while (num % prime[i] == 0) yin[ty] *= prime[i], num /= prime[i];
++ty;
}
if (num > 1) yin[ty++] = num;
}
void hebing(int x, int y) {
while (fa[x] != -1) x = fa[x];
while (fa[y] != -1) y = fa[y];
if (x != y) fa[x] = y;
}
int renew(int x) {
if (fa[x] == -1) return x;
return fa[x] = renew(fa[x]);
}
void yin_div(int x, int cur, const int &l, int w) {
if (cur == ty) {
int y = num[l] / x;
if (num[l] % 2) {
int r, s, pos = 0;
long long nm;
if (x < y) swap(x, y);
r = (x + y) / 2;
s = (x - y) / 2;
if ((r + s) % 2 == 0) return;
nm = 2 * (long long)r * s;
if (nm < 10000001 && ish[nm] != -1) {
hebing(l, ish[nm]);
renew(l);
renew(ish[nm]);
}
nm = (long long)r * r + (long long)s * s;
if (nm < 10000001 && ish[nm] != -1) {
hebing(l, ish[nm]);
renew(l);
renew(ish[nm]);
}
} else {
int pos = 0;
long long nm;
if (x % 4 && y % 4) return;
if (x % 2 == 0)
x /= 2;
else
y /= 2;
if (x < y) swap(x, y);
nm = (long long)x * x - (long long)y * y;
if (nm < 10000001 && ish[nm] != -1) {
hebing(l, ish[nm]);
renew(l);
renew(ish[nm]);
}
nm = (long long)x * x + (long long)y * y;
if (nm < 10000001 && ish[nm] != -1) {
hebing(l, ish[nm]);
renew(l);
renew(ish[nm]);
}
}
return;
}
for (int i = cur; i < ty; ++i)
yin_div(x *= (w ? yin[i] : 1), i + 1, l, w ^ 1);
}
void solve() {
memset(ish, 255, sizeof(ish));
for (int i = 0; i < n; ++i) ish[num[i]] = i;
memset(fa, 255, sizeof(fa));
for (int i = 0; i < n; ++i) {
yin_cal(num[i]);
for (int j = 0; j <= ty; ++j) yin_div(1, j, i, 1);
}
int ans = 0;
for (int i = 0; i < n; ++i) ans += fa[i] == -1;
printf("%d\n", ans);
}
int main() {
prime_cal();
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &num[i]);
solve();
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int T;
cin >> T;
while (T--) {
long long int ans = 0;
int N;
cin >> N;
long long int arr[N + 1];
for (int i = 0; i < N; i++) cin >> arr[i];
for (int i = 1; i < N; i++) ans = max(ans, arr[i] * arr[i - 1]);
cout << ans << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59};
int fack[62];
int dp[110][1 << 17];
int pre[110][1 << 17];
int x[110][1 << 17];
int n;
int a[110];
void init() {
memset(fack, 0, sizeof(fack));
for (int i = 1; i <= 60; i++) {
for (int j = 0; j <= 16; j++) {
if (i % prime[j] == 0) {
fack[i] += (1 << j);
}
}
}
}
void dfs(int idx, int id) {
if (idx >= 2) dfs(idx - 1, pre[idx][id]);
printf("%d%c", x[idx][id], idx == n ? '\n' : ' ');
}
int main() {
init();
fack[1] = 0;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
memset(dp, 50, sizeof(dp));
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int s = 0; s <= (1 << 17) - 1; s++) {
for (int k = 1; k < 60; k++) {
if (s & fack[k])
continue;
else {
if (dp[i][s | fack[k]] > dp[i - 1][s] + abs(a[i] - k)) {
dp[i][s | fack[k]] = dp[i - 1][s] + abs(a[i] - k);
pre[i][s | fack[k]] = s;
x[i][s | fack[k]] = k;
}
}
}
}
}
int ans = 0x3f3f3f3f;
int id;
for (int s = 0; s <= (1 << 17) - 1; s++) {
if (dp[n][s] < ans) {
ans = dp[n][s];
id = s;
}
}
dfs(n, id);
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5, MOD = 1e9 + 7;
long long int t, n, m, ans, c, sum, y, mn = 1e9;
string s;
vector<long long int> v;
string x[10] = {"47", "4477", "444777",
"44447777", "4444477777", "444444777777"};
void init() {
for (int i = 0; i < 6; i++) do {
v.push_back(stoll(x[i]));
} while (next_permutation(x[i].begin(), x[i].end()));
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
init();
cin >> n;
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++) {
if (v[i] >= n) {
cout << v[i] << '\n';
break;
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int N = 1000;
int t, n, a[N][N], amt[N], other[N];
bool u[N];
std::vector<int> g[N], ans;
void rm(int v, bool next = true)
{
u[v] = true;
if (next) for (int i = 0; i < 2 * n; ++i) if (!u[i]) for (int j = 0; j < n && !u[i]; ++j) if (a[i][j] == a[v][j]) rm(i, false);
}
void either(int u, int v)
{
g[u].push_back(v);
g[v].push_back(u);
}
void run(int v, bool c = 0)
{
u[v] = true;
if (!c) ans.push_back(v);
for (int i = 0; i < (int)g[v].size(); ++i) if (!u[g[v][i]]) run(g[v][i], !c);
}
int main()
{
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < 2 * n; ++i) for (int j = 0; j < n; ++j) {
scanf("%d", a[i] + j);
--a[i][j];
}
int remove;
do {
remove = -1;
for (int i = 0; i < n && remove == -1; ++i) {
memset(amt, 0, n << 2);
for (int j = 0; j < 2 * n; ++j) if (!u[j]) ++amt[a[j][i]];
for (int j = 0; j < 2 * n && remove == -1; ++j) if (!u[j] && amt[a[j][i]] == 1) remove = j;
}
if (remove != -1) {
ans.push_back(remove);
rm(remove);
}
} while (remove != -1);
for (int i = 0; i < n; ++i) {
memset(other, -1, n << 2);
for (int j = 0; j < 2 * n; ++j) if (!u[j]) {
if (other[a[j][i]] != -1) either(other[a[j][i]], j);
else other[a[j][i]] = j;
}
}
int r = 1;
for (int i = 0; i < 2 * n; ++i) if (!u[i]) {
run(i);
r += r;
if (r >= 998244353) r -= 998244353;
}
printf("%d\n", r);
for (int i = 0; i < n; ++i) printf("%d ", ans[i] + 1);
printf("\n");
ans.clear();
for (int i = 0; i < 2 * n; ++i) {
g[i].clear();
u[i] = 0;
}
}
return 0;
}
| 10 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long int n, z;
cin >> n >> z;
long long int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long int c = 0;
for (int i = 0; i < n; i++) {
if (a[i] - a[c] >= z) c++;
}
cout << min(c, n / 2);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1.0);
const long long int mod = 1e9 + 7;
const int MAXN = 1e6 + 5;
int n, m;
long long int arr[15];
int guess[15];
set<long long int> temp1, temp2, ans;
void rec(long long int b, int picked, int i, long long int num) {
if (b < 0) {
if (picked == guess[i]) temp1.insert(num);
} else {
long long int bit = (arr[i] & (1LL << b)) ? 1 : 0;
long long int bitop = bit ? 0 : 1;
if (picked < guess[i]) rec(b - 1, picked + 1, i, num | (bit << b));
rec(b - 1, picked, i, num | (bitop << b));
}
}
void cp() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
bitset<35> x;
cin >> x >> guess[i];
arr[i] = x.to_ullong();
}
n--;
rec(n, 0, 0, 0);
ans = temp1;
for (int i = 1; i < m; i++) {
temp1.clear();
rec(n, 0, i, 0);
temp2.clear();
for (long long int x : ans)
if (temp1.find(x) != temp1.end()) temp2.insert(x);
ans = temp2;
}
cout << ((int)ans.size()) << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int t;
t = 1;
while (t--) {
cp();
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, str_cnt = 0;
cin >> n;
vector<pair<string, string> > vps;
map<string, int> m;
for (int i = 0; i < n; ++i) {
string str1, str2;
cin >> str1 >> str2;
if (!m[str1]) {
vps.push_back({str1, str2});
m[str1] = m[str2] = ++str_cnt;
} else {
m[str2] = m[str1];
vps[m[str1] - 1].second = str2;
}
}
cout << str_cnt << "\n";
for (int i = 0; i < str_cnt; ++i)
cout << vps[i].first << " " << vps[i].second << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
int a = 0, b = 0, c = 0, cont = 0;
int x;
for (int i = 0; i < n; i++) {
scanf("%d", &x);
cont++;
if (cont == 1) {
a += x;
} else if (cont == 2) {
b += x;
} else {
c += x;
cont = 0;
}
}
if (a > b && a > c) {
printf("chest");
} else if (b > a && b > c) {
printf("biceps");
} else {
printf("back");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long fast_power(long long a, long long b, long long MOD = 5) {
if (b == 0) return 1;
if (b & 1) return (a % MOD * fast_power((a * a) % MOD, b / 2, MOD)) % MOD;
return fast_power((a * a) % MOD, b / 2, MOD) % MOD;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
string num;
long long p = 0;
cin >> num;
for (char digit : num) {
p = (p * 10 + (digit - '0')) % 4;
}
int ans = (1 + fast_power(2, p) + fast_power(3, p) + fast_power(4, p)) % 5;
cout << ans << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char s[100050], co[55];
int invco[256], q, l, r;
long long num[55], n, W = 1, p[100050], pinv[100050], dp[500050], ans[55][55];
const long long Mod = 1e9 + 7;
long long mypow(long long a, long long e) {
long long ans = 1, base = a;
while (e) {
if (e % 2) ans = ans * base % Mod;
base = base * base % Mod;
e /= 2;
}
return ans;
}
long long com(long long n, long long a) {
return (p[n] * pinv[a] % Mod) * pinv[n - a] % Mod;
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
p[0] = pinv[0] = 1;
for (int i = 1; i <= 100000; i++) p[i] = p[i - 1] * i % Mod;
pinv[100000] = mypow(p[100000], Mod - 2);
for (int i = 100000 - 1; i; i--) pinv[i] = pinv[i + 1] * (i + 1) % Mod;
for (int i = 0; i < 26; i++) co[i] = i + 'a';
for (int i = 26; i < 52; i++) co[i] = i + 'A' - 26;
for (int i = 0; i < 52; i++) invco[co[i]] = i;
for (int i = 1; i <= strlen(s + 1); i++) num[invco[s[i]]]++;
W = p[n / 2] * p[n / 2] % Mod;
dp[0] = 1;
for (int i = 0; i < 52; i++) {
if (!num[i]) continue;
W = W * pinv[num[i]] % Mod;
for (int j = n / 2; j >= num[i]; j--)
dp[j] = (dp[j] + dp[j - num[i]]) % Mod;
}
for (int i = 0; i < 52; i++) {
if (!num[i]) continue;
for (int j = num[i]; j <= n / 2; j++)
dp[j] = (dp[j] - dp[j - num[i]] + Mod) % Mod;
for (int j = 0; j < 52; j++) {
if (!num[j]) continue;
if (i == j) {
ans[i][j] = 2 * W * dp[n / 2] % Mod;
continue;
}
for (int k = num[j]; k <= n / 2; k++)
dp[k] = (dp[k] - dp[k - num[j]] + Mod) % Mod;
ans[i][j] = 2 * W * dp[n / 2] % Mod;
for (int k = n / 2; k >= num[j]; k--)
dp[k] = (dp[k] + dp[k - num[j]]) % Mod;
}
for (int j = n / 2; j >= num[i]; j--)
dp[j] = (dp[j] + dp[j - num[i]]) % Mod;
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &l, &r);
printf("%lld\n", ans[invco[s[l]]][invco[s[r]]]);
}
return 0;
}
| 9 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int vis[N], head[N];
int dis[N], ecnt;
struct EDGE {
int u, v, nxt, w;
} e[N];
void add_edge(int u, int v, int w) {
e[ecnt].u = u;
e[ecnt].v = v;
e[ecnt].w = w;
e[ecnt].nxt = head[u];
head[u] = ecnt++;
}
char s[1000005];
int n, m;
int mp[105][105];
int p[1000005];
void floyd() {
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
mp[i][j] = min(mp[i][j], mp[i][k] + mp[k][j]);
}
}
}
}
vector<int> v;
int main() {
cin >> s;
int len = strlen(s);
int i;
int cnt = 0;
for (int i = len - 1; i >= 0; i--)
if (s[i] == '0')
cnt++;
else if (cnt)
cnt--;
else
s[i] = '0';
cout << s << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
string s;
cin >> s;
char direction[] = {'U', 'D', 'L', 'R'};
int uu = 0, dd = 0, ll = 0, rr = 0, ans = 0;
for (int i = 0; i < t; i++) {
uu = 0;
ll = 0;
dd = 0;
rr = 0;
for (int j = i; j < t; j++) {
if (s[j] == direction[0])
uu++;
else if (s[j] == direction[1])
dd++;
else if (s[j] == direction[2])
ll++;
else
rr++;
if (uu == dd && ll == rr) {
ans++;
}
}
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const unsigned long long BIGEST = 1000000000000000000 + 1000000000000000000;
const long long BIGER = 1000000000000000000;
const int BIG = 1000000000;
const int MOD = 1000000007;
const long long LMOD = 1000000007;
long long lcm = 1;
long long f[360365][3];
queue<long long> q;
long long a, b, k;
long long bfs(long long fr, long long en, long long col) {
q.push(fr);
f[fr][col] = 1;
long long t, u;
while (!q.empty()) {
u = q.front();
q.pop();
for (long long i = 2; i <= k; i++) {
t = (u / i) * i;
if (f[t][col] == 0) {
f[t][col] = f[u][col] + 1;
q.push(t);
}
}
if (u > 0) {
t = u - 1;
if (f[t][col] == 0 || f[t][col] > f[u][col]) {
f[t][col] = f[u][col] + 1;
q.push(t);
}
}
}
long long k = en;
long long s = 0;
return f[k][col] - f[fr][col] + s;
}
int main() {
cin >> a >> b >> k;
long long t = 0;
for (long long i = 2; i <= k; i++) {
t = i;
for (long long j = 2; j <= t; j++) {
if (t % j == 0) {
t /= j;
if (lcm % j == 0) lcm /= j;
j--;
}
}
lcm *= i;
}
if (a / lcm == b / lcm) {
cout << bfs(a % lcm, b % lcm, 0);
} else {
cout << bfs(a % lcm, 0, 0) +
(1 + bfs(lcm - 1, 0, 1)) * (a / lcm - b / lcm - 1) +
bfs(lcm - 1, b % lcm, 2) + 1;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
const int N = 5e3 + 5;
const int OO = 1e16;
const long long mood = 1e9 + 7;
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) {
double t;
long long n, x, ok = 0;
cin >> n >> t;
t /= 2.0;
for (int i = 0; i < n; i++) {
cin >> x;
if (x > t)
cout << "1 ";
else if (x < t)
cout << "0 ";
else if (t == x) {
cout << ok << " ";
ok ^= 1;
}
}
cout << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct Pos {
Pos(int x_, int y_) : x(x_), y(y_) {}
int x;
int y;
bool operator<(const Pos& b) const {
return (x <= b.x && y < b.y) || (x < b.x && y <= b.y);
}
bool operator==(const Pos& b) const { return x == b.x && y == b.y; }
};
struct Rect {
Rect(int x1, int y1, int x2, int y2) : ul(x1, y1), dr(x2, y2) {}
Pos ul;
Pos dr;
bool operator<(const Rect& b) const {
if (ul.x < b.ul.x)
return true;
else if (ul.x > b.ul.x)
return false;
else if (ul.y < b.ul.y)
return true;
else if (ul.y > b.ul.y)
return false;
else if (dr.x < b.dr.x)
return true;
else if (dr.x > b.dr.x)
return false;
else
return (dr.y < b.dr.y);
}
bool contains(const Pos& p) const {
return (ul < p || ul == p) && (p < dr || p == dr);
}
vector<Rect> split(const Pos& p) const {
vector<Rect> result;
int x1 = ul.x;
int y1 = ul.y;
int x2 = dr.x;
int y2 = dr.y;
int x = p.x;
int y = p.y;
if (y > y1) result.push_back(Rect(x1, y1, x2, y - 1));
if (x > x1) result.push_back(Rect(x1, y1, x - 1, y2));
if (y < y2) result.push_back(Rect(x1, y + 1, x2, y2));
if (x < x2) result.push_back(Rect(x + 1, y1, x2, y2));
return result;
}
int perim() const { return 2 * ((dr.x - ul.x + 1) + (dr.y - ul.y + 1)); }
};
int main() {
int m, n;
cin >> m >> n;
vector<Pos> occupied;
set<Rect> rects;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
char x;
cin >> x;
if (x == '1') occupied.push_back(Pos(j, i));
}
}
rects.insert(Rect(0, 0, n - 1, m - 1));
for (vector<Pos>::const_iterator it = occupied.begin(); it != occupied.end();
++it) {
for (set<Rect>::iterator rit = rects.begin(); rit != rects.end();) {
if (rit->contains(*it)) {
Rect r = (*rit);
rects.erase(rit++);
vector<Rect> splits = r.split(*it);
rects.insert(splits.begin(), splits.end());
} else {
++rit;
}
}
}
int result = 0;
for (set<Rect>::const_iterator rit = rects.begin(); rit != rects.end();
++rit) {
result = max(result, rit->perim());
}
cout << result << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
int bsf(int x) { return __builtin_ctz(x); }
int bsr(int x) { return 31 - __builtin_clz(x); }
using namespace std;
const double pi = 3.14159265358979323846;
const int inf = (int)2e+9 + 2;
const int mod = (int)1e+9 + 7;
const double eps = 1e-8;
const int N = 100000;
int n, m, k;
vector<int> g[N];
int a[N], in[N];
queue<int> q;
queue<int> q2;
void solve() {
cin >> n >> m;
for (auto i = 0; i < n; ++i) cin >> a[i];
for (auto i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
g[y].push_back(x);
++in[x];
}
for (auto i = 0; i < n; ++i) {
if (!in[i]) q.push(i);
}
int type = 0;
int ans = 0;
while (!q.empty()) {
if (!type) {
while (!q.empty()) {
int v = q.front();
q.pop();
if (a[v] == type) {
for (auto to : g[v]) {
--in[to];
if (!in[to]) q.push(to);
}
} else {
q2.push(v);
}
}
} else {
++ans;
while (!q.empty()) {
int v = q.front();
q.pop();
if (a[v] == type) {
for (auto to : g[v]) {
--in[to];
if (!in[to]) q.push(to);
}
} else {
q2.push(v);
}
}
}
q.swap(q2);
type ^= 1;
}
cout << ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int test = 1;
for (; test; --test) solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct Interval {
Interval() {}
Interval(double l, double r) {
left = l;
right = r;
}
bool operator<(const Interval &other) const { return left < other.left; }
double left;
double right;
};
struct Circle {
double x;
double y;
double r;
};
const double PI = 3.141592653589793;
const int N = 1e5 + 5;
double X, Y, R;
vector<Interval> vecInt;
Circle circle[N];
double dist[N];
double sqr(double x) { return x * x; }
void getInterval(int i) {
double arg, offset, a1, a2, left, right;
Circle c = circle[i];
if (c.r == 0.0 || c.r + R <= dist[i]) {
return;
}
arg = acos((c.x - X) / dist[i]);
if (c.y - Y < 0) {
arg = 2 * PI - arg;
}
if (sqrt(sqr(dist[i]) - sqr(c.r)) <= R) {
offset = asin(c.r / dist[i]);
} else {
offset = acos((sqr(R) + sqr(dist[i]) - sqr(c.r)) / (2 * R * dist[i]));
}
a1 = arg + offset;
a2 = arg - offset;
if (a1 > 2 * PI) {
a1 -= 2 * PI;
}
if (a2 < 0) {
a2 += 2 * PI;
}
left = min(a1, a2);
right = max(a1, a2);
if (right - left <= PI) {
vecInt.push_back(Interval(left, right));
} else {
vecInt.push_back(Interval(0, left));
vecInt.push_back(Interval(right, 2 * PI));
}
}
int main() {
int n, i, v, t;
double ans = 0;
bool skip = false;
scanf("%lf%lf%d%d", &X, &Y, &v, &t);
R = (double)v * t;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lf%lf%lf", &circle[i].x, &circle[i].y, &circle[i].r);
}
for (i = 0; i < n; i++) {
dist[i] = sqrt(sqr(X - circle[i].x) + sqr(Y - circle[i].y));
if (dist[i] <= circle[i].r) {
ans = 1;
skip = true;
break;
}
}
if (!skip) {
for (i = 0; i < n; i++) {
getInterval(i);
}
sort(vecInt.begin(), vecInt.end());
double right = 0;
for (i = 0; i < vecInt.size(); i++) {
if (vecInt[i].right > right) {
ans += vecInt[i].right - max(vecInt[i].left, right);
right = vecInt[i].right;
}
}
ans /= 2 * PI;
}
printf("%.6lf\n", ans);
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
long long sum(long long n) {
long long res = 0;
while (n > 0) {
res += n % 10;
n /= 10;
}
return res;
}
void solve() {
long long n, m;
cin >> n >> m;
vector<bool> b(n, false), g(m, false);
long long B, e;
cin >> B;
while (B--) {
cin >> e;
b[e] = true;
}
long long G;
cin >> G;
while (G--) {
cin >> e;
g[e] = true;
}
for (long long i = 0; i < 300000; i++) {
b[i % n] = max(b[i % n], g[i % m]);
g[i % m] = max(b[i % n], g[i % m]);
}
long long ans = 1;
for (long long i = 0; i < n; i++) {
if (b[i] == 0) ans = 0;
}
cout << endl;
for (long long i = 0; i < m; i++) {
if (g[i] == 0) ans = 0;
}
cout << endl;
cout << (ans ? "Yes\n" : "No\n");
}
int32_t main() {
long long n = 1;
while (n--) solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
vector<int> adj[MAXN];
int dis[MAXN];
int pre[MAXN], pos[MAXN];
int anc[MAXN];
int sum[MAXN];
bool pai(int u, int v) { return pre[u] <= pre[v] && pos[v] <= pos[u]; }
int dfs(int v, int d, int &t) {
pre[v] = t++;
dis[v] = d;
sum[v] = 1;
for (int i = 0; i < int((adj[v]).size()); ++i) {
int k = adj[v][i];
if (pre[k]) continue;
anc[k] = v;
sum[v] += dfs(k, d + 1, t);
}
pos[v] = t++;
return sum[v];
}
int mem[MAXN][20];
void calc(int n) {
memset(mem, -1, sizeof mem);
for (int i = 0; i < n; ++i) mem[i][0] = anc[i];
for (int j = 1; (1 << j) < n; j++)
for (int i = 0; i < n; ++i) mem[i][j] = mem[mem[i][j - 1]][j - 1];
}
int find(int u, int k) {
int cnt = 0;
while (k) {
if (k & 1) u = mem[u][cnt];
k >>= 1;
cnt++;
}
return u;
}
int lca(int u, int v) {
if (dis[u] > dis[v]) swap(u, v);
v = find(v, dis[v] - dis[u]);
if (u == v) return u;
int pvt = 0;
while (mem[u][pvt] != mem[v][pvt]) pvt++;
while (pvt >= 0) {
int x = mem[u][pvt], y = mem[v][pvt];
if (x != y) u = x, v = y;
pvt--;
}
return mem[u][0];
}
int solve(int x, int y, int n) {
if (dis[x] > dis[y]) swap(x, y);
int l = lca(x, y);
int s = (dis[x] + dis[y] - 2 * dis[l]);
if (s & 1) return 0;
s >>= 1;
int k = find(y, s);
int c = 0;
int res = n;
if (s == 0) return res;
if (dis[x] != dis[y]) {
res -= sum[find(y, s - 1)] + (n - sum[find(y, s)]);
} else {
res -= sum[find(y, s - 1)] + sum[find(x, s - 1)];
}
return res;
}
int main() {
int n;
while (scanf(" %d", &n) != EOF) {
for (int i = 0; i < n + 1; ++i) adj[i].clear();
for (int i = 0; i < n - 1; ++i) {
int x, y;
scanf(" %d %d", &x, &y);
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
int t = 1;
memset(pre, 0, sizeof pre);
dfs(0, 0, t);
calc(n);
int m;
scanf(" %d", &m);
for (int i = 0; i < m; ++i) {
int x, y;
scanf(" %d %d", &x, &y);
x--, y--;
cout << solve(x, y, n) << endl;
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
const int maxm = 1e5 + 10;
const int mod = 1e9 + 7;
int a[maxn], a1 = 7, b1 = 7;
int ask(int x, int y) {
cout << '?' << ' ' << x << ' ' << y << endl;
fflush(stdout);
int ans;
cin >> ans;
return ans;
}
int main() {
int tmp, a = 0, b = 0, big;
tmp = ask(0, 0);
big = tmp;
for (int i = 1 << 29; i; i >>= 1) {
tmp = ask(a | i, b | i);
if (tmp == 0) {
if (big == 0) {
tmp = ask(a | i, b);
if (tmp == 1) {
continue;
} else {
a |= i;
b |= i;
}
continue;
}
a |= i;
b |= i;
continue;
}
if (tmp != big) {
if (big > 0) {
a |= i;
} else {
b |= i;
}
big = ask(a, b);
continue;
}
tmp = ask(a | i, b);
if (big > 0) {
if (tmp != big) {
b |= i;
a |= i;
} else {
continue;
}
} else {
if (tmp != big) {
continue;
} else {
b |= i;
a |= i;
}
}
}
cout << '!' << ' ' << a << ' ' << b << endl;
fflush(stdout);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long N, k, i;
scanf("%lld%lld", &N, &k);
if (k > 100) {
puts("NO");
return 0;
}
for (i = 1; i <= k; ++i) {
if (N % i != i - 1) {
puts("NO");
return 0;
}
}
puts("Yes");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
void GO_GO_GO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void BYE_BYE() {
cerr << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
bool primeArr[1000001] = {false};
long long int fact[1000001];
long long int lazy[1000001] = {0};
long long int BIT[1000001] = {0};
vector<long long int> primes;
int main() {
GO_GO_GO();
long long int t = 1;
cin >> t;
while (t--) {
long long int i = 0, j = 0;
long long int n;
cin >> n;
vector<long long int> v(n);
unordered_map<long long int, long long int, custom_hash> mp;
bool flag = true;
for (i = 0; i < n; i++) {
cin >> v[i];
mp[v[i]]++;
if (mp[v[i]] > 2) {
flag = false;
}
}
if (!flag) {
cout << 0 << "\n";
continue;
}
set<long long int> s;
vector<pair<long long int, long long int> > ans;
vector<long long int> temp1;
for (i = 0; i < n; i++) {
s.insert(v[i]);
if (*(s.rbegin()) == i + 1 && i + 1 == s.size()) {
temp1.push_back(i + 1);
}
}
s.clear();
for (i = n - 1; i >= 0; i--) {
s.insert(v[i]);
long long int len = n - 1 - i + 1;
if (*(s.rbegin()) == s.size() && s.size() == len) {
auto it = lower_bound(temp1.begin(), temp1.end(), n - len);
if (it != temp1.end() && *it == n - len) {
ans.push_back({n - len, len});
}
}
}
cout << ans.size() << "\n";
for (auto p : ans) {
cout << p.first << " " << p.second << "\n";
;
}
}
BYE_BYE();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
int ans = 0;
int N, M, B[MAXN], P[MAXN];
long long A[MAXN];
vector<pair<int, int> > V, VV;
struct STN {
long long x;
void merge(STN& L, STN& R) { x = min(L.x, R.x); }
void operator=(long long a) { x = a; }
void operator+=(long long a) { x += a; }
};
STN ST[4 * MAXN];
long long lazy[4 * MAXN];
void STB(int id = 1, int l = 0, int r = N) {
if (r - l < 2) {
ST[id] = A[l];
return;
}
int M = (l + r) >> 1, L = id << 1, R = L | 1;
STB(L, l, M);
STB(R, M, r);
ST[id].merge(ST[L], ST[R]);
}
void upd(int id, int l, int r, long long x) {
lazy[id] += x;
ST[id] += x;
}
void shift(int id, int l, int r) {
int M = (l + r) >> 1, L = id << 1, R = L | 1;
upd(L, l, M, lazy[id]);
upd(R, M, r, lazy[id]);
lazy[id] = 0;
}
void STU(int x, int y, long long v, int id = 1, int l = 0, int r = N) {
if (x >= r || l >= y) return;
if (x <= l && r <= y) {
upd(id, l, r, v);
return;
}
shift(id, l, r);
int M = (l + r) >> 1, L = id << 1, R = L | 1;
STU(x, y, v, L, l, M);
STU(x, y, v, R, M, r);
ST[id].merge(ST[L], ST[R]);
}
STN STQ(int x, int y, int id = 1, int l = 0, int r = N) {
if (x == l && y == r) return ST[id];
shift(id, l, r);
int M = (l + r) >> 1, L = id << 1, R = L | 1;
if (x >= M) return STQ(x, y, R, M, r);
if (y <= M) return STQ(x, y, L, l, M);
STN res, ln = STQ(x, M, L, l, M), rn = STQ(M, y, R, M, r);
return res.merge(ln, rn), res;
}
int main() {
scanf("%d", &N), scanf("%d", &M);
for (int i = 0; i < (int)N; i++) scanf("%lld", &A[i]);
for (int i = 0; i < (int)M; i++) {
int x;
scanf("%d", &x);
V.push_back({x, 0});
}
for (int i = 0; i < (int)M; i++) {
int x;
scanf("%d", &x);
V[i].second = x;
}
sort(V.begin(), V.end());
int j = 0;
for (int i = N - 1; i > (int)-1; i--) {
while (j < M && V[j].first <= A[i]) {
VV.push_back({V[j++].second, i});
}
}
for (int i = 1; i < (int)N; i++) A[i] += A[i - 1];
STB();
sort(VV.begin(), VV.end());
for (int i = 0; i < (int)VV.size(); i++) {
int val = VV[i].first;
int pos = VV[i].second;
long long mn = STQ(pos, N).x;
if (val <= mn) {
ans++;
STU(pos, N, -val);
}
}
printf("%d\n", ans);
}
| 8 |
#include <bits/stdc++.h>
int main() {
int x = 0, n, ans = 1;
scanf("%d", &n);
for (int i = 1; i < n; i++) x += 12, ans += x;
printf("%d\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vii = vector<pair<int, int>>;
ll a[500007];
constexpr ll inf = 10000000000000;
ll dp[2][5007][2];
int p(int i) { return i & 1; }
int o(int i) { return (i + 1) & 1; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int K, n;
cin >> K >> n;
vector<pair<ll, int>> A(n);
vector<ll> inp(n);
for (int i = 0; i < n; ++i) {
cin >> inp[i];
}
sort(begin(inp), end(inp));
for (int i = 0; i < n - 1; ++i) {
ll v = inp[i + 1] - inp[i];
A[i] = make_pair(v, i);
a[i] = v;
}
sort(begin(A), end(A));
set<int> cons;
int i = 0;
while ((int)(cons).size() < 4 * K && i < n - 1) {
if (A[i].second > 0) cons.insert(A[i].second - 1);
if (A[i].second < n - 2) cons.insert(A[i].second + 1);
cons.insert(A[i].second);
++i;
}
vector<pair<ll, int>> sm;
for (const auto &e : cons) {
if (!sm.empty() && sm.back().second < e - 1) sm.emplace_back(inf, -1);
sm.emplace_back(a[e], e);
}
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < K + 1; ++j) {
for (int l = 0; l < 2; ++l) {
dp[i][j][l] = inf;
}
}
}
dp[0][0][0] = 0;
for (int i = 1; i <= (int)(sm).size(); ++i) {
for (int j = 0; j <= K; ++j) {
dp[p(i)][j][1] = dp[o(i)][j - 1][0] + sm[i - 1].first;
dp[p(i)][j][0] = min(dp[o(i)][j][0], dp[o(i)][j][1]);
}
}
cout << min(dp[p((int)(sm).size())][K][0], dp[p((int)(sm).size())][K][1])
<< endl;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
auto start = high_resolution_clock::now();
int n;
cin >> n;
int A[n + 1];
for (int i = 1; i <= n; i++) cin >> A[i];
int B[n + 1];
int ct = 2, p = 1, q = 5, find = 0;
for (int i = 1; i < n; i++) {
if (A[i + 1] == A[i]) {
if (i > 1 && A[i - 1] > A[i]) {
if (B[i - 1] == 1) {
find = 1;
break;
}
B[i] = B[i - 1] - 1;
p = 1, q = 5;
} else if (i > 1 && A[i - 1] < A[i]) {
if (B[i - 1] == 5) {
find = 1;
break;
}
B[i] = B[i - 1] + 1;
p = 1, q = 5;
} else {
if (B[i - 1] == 2)
B[i] = 3;
else
B[i] = 2;
p = 1, q = 5;
}
}
if (A[i] < A[i + 1]) {
if (p == 6) {
find = 1;
break;
}
if (i > 1 && B[i - 1] == 1) {
B[i] = 2;
p = 3;
} else {
B[i] = p;
p++;
}
q = 5;
}
if (A[i] > A[i + 1]) {
if (q == 0) {
find = 1;
break;
}
if (i > 1 && B[i - 1] == 5) {
B[i] = 4;
q = 3;
} else {
B[i] = q;
q--;
}
p = 1;
}
}
if (n > 1 && A[n] == A[n - 1]) {
if (B[n - 1] == 2)
B[n] = 3;
else
B[n] = 2;
} else if (n > 1 && A[n - 1] < A[n]) {
if (B[n - 1] == 5) {
find = 1;
} else {
B[n] = B[n - 1] + 1;
}
} else if (n > 1 && A[n - 1] > A[n]) {
if (B[n - 1] == 1) {
find = 1;
} else {
B[n] = B[n - 1] - 1;
}
} else
B[n] = 1;
if (find == 1)
cout << -1;
else {
for (int i = 1; i <= n; i++) cout << B[i] << " ";
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int NN = 100111;
int val[NN << 1], pre[NN << 1], son[NN << 1][4], dp[NN << 1][4];
int TTT, last, len, bit;
long long n;
bool vt[NN << 1];
char s[NN];
void samadd(int x) {
int end = ++TTT, p = last;
last = end;
val[end] = val[p] + 1;
for (; p != -1 && !son[p][x]; p = pre[p]) son[p][x] = end;
if (p == -1) {
pre[end] = 0;
return;
}
int q = son[p][x];
if (val[q] == val[p] + 1) {
pre[end] = q;
return;
}
int nq = ++TTT;
val[nq] = val[p] + 1;
pre[nq] = pre[q];
pre[q] = pre[end] = nq;
memcpy(son[nq], son[q], sizeof(son[q]));
for (; p != -1 && son[p][x] == q; p = pre[p]) son[p][x] = nq;
}
void dfs(int v) {
vt[v] = true;
for (int i = 0; i < 4; i++) dp[v][i] = 999999999;
for (int i = 0; i < 4; i++)
if (son[v][i]) {
if (!vt[son[v][i]]) dfs(son[v][i]);
for (int k = 0; k < 4; k++)
dp[v][k] = min(dp[v][k], dp[son[v][i]][k] + 1);
} else
dp[v][i] = 1;
}
struct matrix {
long long qq[4][4];
long long* operator[](int x) { return qq[x]; }
matrix() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) qq[i][j] = 1000000000000000005ll;
}
friend matrix operator*(matrix a, matrix b) {
matrix c;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 4; k++) c[i][j] = min(c[i][j], a[i][k] + b[k][j]);
return c;
}
inline bool check() {
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (qq[i][j] < n) return true;
return false;
}
} f[71], now;
int main() {
scanf("%I64d\n%s", &n, s + 1);
len = strlen(s + 1);
pre[0] = -1;
int i, j;
for (i = 1; i <= len; i++) samadd(s[i] - 'A');
dfs(0);
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) f[0][i][j] = dp[son[0][i]][j];
for (bit = 1; (1ll << bit) <= n; bit++) f[bit] = f[bit - 1] * f[bit - 1];
long long ans = 0;
for (i = 0; i < 4; i++) now[i][i] = 0;
for (i = bit; i >= 0; i--)
if ((now * f[i]).check()) {
now = now * f[i];
ans |= 1ll << i;
}
printf("%I64d\n", ans + 1);
return 0;
}
| 11 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int N = 1e5;
const int MOD = 1e9 + 7;
const double PI = acos(-1);
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n, a, b, c;
cin >> n >> a >> b >> c;
int way[3][3];
way[0][1] = a;
way[1][0] = a;
way[0][2] = b;
way[2][0] = b;
way[1][2] = c;
way[2][1] = c;
int cur = 0, ans = 0;
n--;
while (n--) {
int mn = 101, next = -1;
for (int i = 0; i < 3; i++) {
if (i == cur) continue;
if (way[cur][i] < mn) {
mn = way[cur][i];
next = i;
}
}
cur = next;
ans += mn;
}
cout << ans;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200007;
int uni[MAXN];
vector<pair<int, int>> G[MAXN];
bool odw[MAXN];
int n, k;
long long res = 0;
void dfs(int v) {
odw[v] = true;
for (auto ele : G[v]) {
int w, waga;
tie(w, waga) = ele;
if (odw[w] == false) {
dfs(w);
uni[v] += uni[w];
res += waga * min(uni[w], k - uni[w]);
}
}
}
int main() {
cin >> n >> k;
k *= 2;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
uni[x] = 1;
}
for (int i = 2; i <= n; i++) {
int a, b;
cin >> a >> b;
G[a].emplace_back(b, 1);
G[b].emplace_back(a, 1);
}
dfs(1);
cout << res << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 210000;
const int INF = 1000000000;
const int MOD = 1000000007;
long long mod(long long a, long long b, long long m) {
long long ret = 1;
a %= m;
while (b) {
if (b & 1) ret = ret * a % m;
b >>= 1;
a = a * a % m;
}
return ret;
}
long long inv(long long a) { return mod(a, MOD - 2, MOD); }
long long vabs(long long x) { return x > 0 ? x : -x; }
long long gcd(long long a, long long b) { return a % b ? gcd(b, a % b) : b; }
int cross(pair<int, int> a, pair<int, int> b) {
return a.first * b.second - a.second * b.first;
}
int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
struct EDGE {
int v, next, flag;
} edge[MAX];
int tot = 0;
int head[MAX];
int stk[MAX];
int top = 0;
int d[MAX], h[MAX];
int DN = 0;
int CN = 0;
int n = 0;
int main() {
int m, i, j;
int Q;
int CS = 1;
int n;
cin >> n;
vector<int> d;
for (i = 0; i < n; i++) {
d.push_back(0);
cin >> d[i];
}
vector<pair<int, int> > ans;
m = d[n - 1] + 1;
while (d.size() > 0) {
for (i = m - d[0] + 1; i <= m; i++) {
for (j = i + 1; j <= m; j++) {
ans.push_back(make_pair(i, j));
}
for (j = 1; j < m - d[0] + 1; j++) {
ans.push_back(make_pair(i, j));
}
}
m -= d[0];
if ((int)d.size() > 1) {
m -= d.back() - d[d.size() - 2];
}
int tmp = d[0];
d.erase(d.begin());
if (d.size() > 0) {
d.erase(d.end() - 1);
}
for (i = 0; i < d.size(); i++) {
d[i] -= tmp;
}
}
printf("%d\n", (int)ans.size());
for (i = 0; i < ans.size(); i++) {
printf("%d %d\n", ans[i].first, ans[i].second);
}
return 0;
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
#define endl '\n'
#define pi 3.141592653589793238
#define mp make_pair
#define pb push_back
#define precise cout<<fixed<<setprecision(20)
#define ff first
#define ss second
#define uniq(v) v.resize(distance(v.begin(),unique(all(v))))
#define lb lower_bound
#define ub upper_bound
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vp;
typedef vector<vector<int>> vv;
//const int inf=1e18;
const int N=10000001;
int mod=1000000007;
//int mod=998244353;
int min(int a,int b){if(a<b) return a; else return b;}
int max(int a,int b){if(a>b) return a; else return b;}
int power(int a,int b){int res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int gcd(int p, int q){if(p%q==0) return q; else{return gcd(q,p%q);}}
int mul(int a,int b){return ((a%mod)*(b%mod))%mod;}
int sub(int a,int b){return (((a-b)%mod)+mod)%mod;}
template<typename T> istream& operator>>(istream& in, vector<T>& v){for (T& x : v) in >> x; return in;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& v){for (T& x : v) out << x<<" "; return out;}
void solve()
{
int n;
cin>>n;
vi v(n);
cin>>v;
int x=0;
for(auto it:v)
x^=it;
if(!x)
{
cout<<"YES"<<endl;
return;
}
int pre[n],suf[n];
pre[0]=v[0];
suf[n-1]=v[n-1];
rep(i,1,n) pre[i]=pre[i-1]^v[i];
repr(i,n-2,0) suf[i]=suf[i+1]^v[i];
rep(i,0,n)
{
rep(j,i+1,n-1)
{
if(pre[i]==suf[j+1]&&(pre[i]==(pre[i]^pre[j])))
{
cout<<"YES"<<endl;
return;
}
}
}
cout<<"NO"<<endl;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t=1;
cin>>t;
rep(i,0,t)
{
//cout<<"Case #"<<t+1<<": ";
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
string a;
int main() {
int x = 0, y = 0;
cin >> a;
for (int i = 0; i < a.size(); i++) {
if (a[i] == '-')
x++;
else
y++;
}
if (y == 0)
cout << "YES" << endl;
else if (x % y == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long p[n];
for (long long i = 0; i < n; i++) cin >> p[i];
for (long long i = n - 1; i >= 0; i--) cout << p[i] << " ";
cout << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int Max = 100000 + 5;
int n, a[Max], b[Max], m;
pair<int, int> s[Max * 2];
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
cin >> m;
int ind = 0;
for (int i = 0; i < n; ++i) {
s[ind++] = pair<int, int>(a[i], i);
}
for (int i = 0; i < n; ++i) {
s[ind++] = pair<int, int>(b[i], i);
}
sort(s, s + ind);
int cur, rep, prev = 0;
int id = 0;
long long r = 1;
while (id < ind) {
prev = s[id].first;
cur = 1;
rep = a[s[id].second] == b[s[id].second];
id++;
while (id < ind && s[id].first == prev) {
cur++;
rep += a[s[id].second] == b[s[id].second];
id++;
};
;
if (cur == 1) continue;
rep /= 2;
long long inc = 1;
for (int i = 2; i <= cur; ++i) {
inc *= i;
if (i % 2 == 0 && rep) {
rep--;
inc /= 2;
}
inc %= m;
};
r = (r * inc) % m;
}
cout << r << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
register long long x = 0, f = 0;
register char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = 1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
return f ? -x : x;
}
inline void write(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
long long t, s, q, ans;
signed main() {
t = read();
s = read();
q = read();
while (s < t) s *= q, ++ans;
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int cost[14][(1 << 12) + 5];
int temp[2][(1 << 12) + 5];
int dp[14][(1 << 12) + 5];
int n, m;
int arr1[15][2020], arr[15][15];
int rec(int c, int mask) {
if (c == m) return 0;
if (mask == 0) return 0;
int &ret = dp[c][mask];
if (ret != -1) return ret;
ret = 0;
ret = rec(c + 1, mask);
for (int i = mask; i > 0; i = (i - 1) & mask) {
ret = max(ret, cost[c][i] + rec(c + 1, mask ^ i));
}
return ret;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
memset(cost, 0, sizeof cost);
memset(dp, -1, sizeof dp);
memset(arr, 0, sizeof arr);
pair<int, int> mm[2020];
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
int mx = 0;
for (int j = 0; j < m; j++) {
scanf("%d", &arr1[i][j]);
}
}
for (int j = 0; j < m; j++) {
int mx = 0;
for (int i = 0; i < n; i++) mx = max(mx, arr1[i][j]);
mm[j] = {mx, j};
}
sort(mm, mm + m);
reverse(mm, mm + m);
m = min(m, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) arr[i][j] = arr1[i][mm[j].second];
}
for (int j = 0; j < m; j++) {
int f = 0;
for (int ms = 0; ms < 1 << n; ms++) {
temp[0][ms] = temp[1][ms] = 0;
for (int k = 0; k < n; k++) {
if (ms & (1 << k)) temp[0][ms] += arr[k][j];
}
cost[j][ms] = max(cost[j][ms], temp[0][ms]);
}
for (int k = 0; k < n; k++) {
for (int ms = 0; ms < 1 << n; ms++) {
int nw = ms << 1;
if (ms & (1 << (n - 1))) {
nw |= (1 << 0);
nw ^= (1 << (n));
}
cost[j][nw] = max(cost[j][nw], temp[f][ms]);
temp[f ^ 1][nw] = temp[f][ms];
}
f ^= 1;
}
}
printf("%d\n", rec(0, (1 << n) - 1));
}
}
| 8 |
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long int, long long int>& a,
pair<long long int, long long int>& b) {
return a.second < b.second;
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, w;
cin >> n >> w;
long long int a[n];
vector<pair<long long int, long long int>> m;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
m.push_back(make_pair(i + 1, a[i]));
}
sort(m.begin(), m.end(), cmp);
sort(a, a + n);
long long int sum = 0;
vector<long long int> v;
auto it = m.begin();
for (long long int i = 0; i < n; i++) {
if (a[i] >= (w + 1) / 2 && a[i] <= w) {
v.clear();
v.push_back(it->first);
break;
}
sum += a[i];
v.push_back(it->first);
if (sum >= (w + 1) / 2 && sum <= w) {
break;
}
if (i == n - 1) {
v.clear();
v.push_back(-1);
}
it++;
}
if (v[0] == -1)
cout << "-1" << endl;
else {
cout << v.size() << endl;
for (long long int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int n, m, p[N], p1[N], dp[N];
inline bool check(int d) {
dp[1] = 0;
for (int i = 2; i <= n; i++) {
dp[i] = -1;
if (dp[i - 1] + p1[i - 1] + 1 >= p1[i] && dp[i - 1] != -1)
dp[i] = max(d, dp[i]);
if (i >= 3 && dp[i - 2] + p1[i - 2] + 1 + d >= p1[i] && dp[i - 2] != -1)
dp[i] = max(d - p1[i] + p1[i - 1], dp[i]);
if (dp[i - 1] + p1[i - 1] + 1 + d >= p1[i] && dp[i - 1] != -1)
dp[i] = max(dp[i], 0);
if (dp[i] >= 0 && p1[i] + dp[i] + d >= m) return 1;
}
dp[1] = d;
for (int i = 2; i <= n; i++) {
dp[i] = -1;
if (dp[i - 1] + p1[i - 1] + 1 >= p1[i] && dp[i - 1] != -1)
dp[i] = max(d, dp[i]);
if (i >= 3 && dp[i - 2] + p1[i - 2] + 1 + d >= p1[i] && dp[i - 2] != -1)
dp[i] = max(d - p1[i] + p1[i - 1], dp[i]);
if (dp[i - 1] + p1[i - 1] + 1 + d >= p1[i] && dp[i - 1] != -1)
dp[i] = max(dp[i], 0);
if (dp[i] >= 0 && p1[i] + dp[i] >= m) return 1;
}
int z = d - p1[2] + 1;
if (z < 0) return 0;
int c = p1[2];
p1[2] = 1, dp[2] = d;
for (int i = 3; i <= n; i++) {
dp[i] = -1;
if (dp[i - 1] + p1[i - 1] + 1 >= p1[i] && dp[i - 1] != -1)
dp[i] = max(d, dp[i]);
if (i >= 3 && dp[i - 2] + p1[i - 2] + 1 + d >= p1[i] && dp[i - 2] != -1)
dp[i] = max(d - p1[i] + p1[i - 1], dp[i]);
if (dp[i - 1] + p1[i - 1] + 1 + d >= p1[i] && dp[i - 1] != -1)
dp[i] = max(dp[i], 0);
if (dp[i] >= 0 && p1[i] + dp[i] + z >= m) {
p1[2] = c;
return 1;
}
}
p1[2] = c;
if (d + z + 1 >= m) return 1;
return 0;
}
int main() {
cin >> m >> n;
int v = 0;
if (n == 1) {
cout << m - 1 << endl;
return 0;
}
for (int i = 1; i <= n; i++) scanf("%d", &p[i]), p[i + n] = p[i] + m;
for (int i = 2; i < n + n; i++) v = max(v, p[i] - p[i - 1]);
for (int i = 2; i < n + n; i++)
if (p[i] - p[i - 1] == v) {
for (int j = 1; j <= n; j++) p1[j] = p[i + j - 1];
break;
}
for (int i = n; i >= 1; i--) p1[i] = ((p1[i] - p1[1]) % m + m) % m + 1;
int l = 0, r = m, ans = 0;
while (l <= r) {
int d = (l + r) >> 1;
if (check(d))
ans = d, r = d - 1;
else
l = d + 1;
}
cout << ans << endl;
}
| 12 |
#include <bits/stdc++.h>
int main() {
int a, b, c;
while (~scanf("%d%d", &a, &b)) {
if (a == b) {
printf("%d\n", a);
} else {
printf("2\n");
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int d, s;
struct entry {
int prevs;
int prevd;
int digit;
};
entry dp[5001][500];
void printNum(int s, int d) {
if (dp[s][d].prevs != -1) {
printNum(dp[s][d].prevs, dp[s][d].prevd);
}
cout << dp[s][d].digit;
}
int main() {
cin >> d >> s;
for (int i = 0; i <= s; i++) {
for (int j = 0; j < d; j++) {
dp[i][j] = {-1, -1, -1};
}
}
queue<pair<int, int>> q;
for (int digit = 0; digit <= 9; digit++) {
int rem = digit % d;
if (dp[digit][rem].digit != -1) continue;
dp[digit][rem] = {-1, -1, digit};
if (digit != 0) q.push({digit, rem});
}
while (q.size() > 0) {
auto p = q.front();
q.pop();
int sum = p.first, rem = p.second;
for (int digit = 0; digit < 10; digit++) {
int ns = sum + digit;
if (ns > s) continue;
int nr = (rem * 10 + digit) % d;
if (dp[ns][nr].digit != -1) continue;
dp[ns][nr] = {sum, rem, digit};
q.push({ns, nr});
}
}
printNum(s, 0);
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> pos, gt;
int n, m, k;
void xuly(int x) {
for (int i = pos[x]; i > 1; --i) {
gt[i] = gt[i - 1];
++pos[gt[i - 1]];
}
gt[1] = x;
pos[x] = 1;
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= k; ++i) {
int x;
cin >> x;
pos[x] = i;
gt[i] = x;
}
int kq = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
int x;
cin >> x;
kq += pos[x];
xuly(x);
}
}
cout << kq;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
inline long long rd() {
long long x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int sqr(int x) { return x * x; }
int a[200005][6];
int maxx[200005][20][6];
int n, m;
void init() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) maxx[i][0][j] = a[i][j];
}
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; i + (j << 1) - 1 <= n; i++) {
for (int k = 0; k < m; k++) {
maxx[i][j][k] =
max(maxx[i][j - 1][k], maxx[i + (1 << (j - 1))][j - 1][k]);
}
}
}
}
int query(int l, int r, int pos) {
int k = log(r - l + 1) / log(2);
return max(maxx[l][k][pos], maxx[r - (1 << k) + 1][k][pos]);
}
int sol(int l, int r, int len) {
int ans = 0;
for (int i = 0; i < len; i++) ans += query(l, r, i);
return ans;
}
int main() {
scanf("%d", &n);
scanf("%d", &m);
int k;
scanf("%d", &k);
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
init();
int ans = -1;
int pos1, pos2;
for (int i = 0; i < n; i++) {
int l = i, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
int res = sol(i, mid, m);
if (res > k)
r = mid - 1;
else
l = mid + 1;
}
int maxlen = r - i + 1;
if (maxlen > ans) {
ans = maxlen;
pos1 = i;
pos2 = r;
}
}
if (ans <= 0) {
for (int i = 0; i < m; i++) cout << 0 << ' ';
return 0;
}
for (int i = 0; i < m; i++) {
cout << query(pos1, pos2, i) << ' ';
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long int a[10], b[10], book[10];
int main() {
long long int n;
scanf("%lld", &n);
while (n--) {
for (long long int i = 1; i <= 7; i++) {
scanf("%lld", &a[i]);
}
long long int i1 = 0, j1 = 0, k1 = 0;
for (long long int i = 1; i <= 7; i++) {
for (long long int j = i + 1; j <= 7; j++) {
for (long long int k = j + 1; k <= 7; k++) {
long long int cnt = 0;
book[1] = book[2] = book[3] = book[4] = 0;
for (long long int x = 1; x <= 7; x++) {
if (x == i || x == j || x == k)
continue;
else {
b[1] = a[i] + a[j];
b[2] = a[i] + a[k];
b[3] = a[j] + a[k];
b[4] = a[i] + a[j] + a[k];
for (long long int y = 1; y <= 4; y++) {
if (a[x] == b[y] && book[y] == 0) {
cnt++;
book[y] = 1;
}
}
}
}
if (cnt == 4) {
i1 = i;
j1 = j;
k1 = k;
}
}
}
}
cout << a[i1] << " " << a[j1] << " " << a[k1] << " " << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int mo = 1e9 + 7;
const int N = 1e4 + 10;
int n, pos, posx, dp[N][27][2];
char s[3][N];
bool emp[N];
void upd(int &x, const int &y) { (x += y) >= mo ? x -= mo : 0; }
int bit(int S, int x) {
if (x == 0) return S % 3;
if (x == 1) return S / 3 % 3;
return S / 9;
}
bool match(int S1, int S2) {
for (int i = 0; i < 3; i++) {
if ((bit(S2, i) == 1) + (bit(S1, i) == 2) == 1) return 0;
}
return 1;
}
bool valid(int S, int x) {
int cnt = 0, tag[3] = {0};
for (int i = 0; i < 3; i++)
if (s[i][x] == '.') {
if (!bit(S, i)) cnt++, tag[i] = 1;
} else if (bit(S, i))
return 0;
return cnt % 2 == 0 && !(tag[0] && tag[2]);
}
bool _rowl(int S, int x) { return x == pos && bit(S, posx) == 1; }
bool _rowr(int S, int x) { return x == pos && bit(S, posx) == 2; }
bool _col(int S, int x) {
return emp[x] && x == pos && posx != 1 &&
(bit(S, 0) | bit(S, 1) | bit(S, 2)) == 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < 3; i++) scanf("%s", &s[i][1]);
for (int i = 0; i < 3; i++)
for (int j = 1; j <= n; j++)
if (s[i][j] == 'O') {
posx = i;
pos = j;
break;
}
for (int i = 1; i <= n; i++)
emp[i] = s[0][i] != 'X' && s[1][i] != 'X' && s[2][i] != 'X';
dp[0][0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 27; j++) {
for (int v = 0; v < 2; v++)
if (dp[i - 1][j][v]) {
for (int S = 0; S < 27; S++)
if (match(j, S) && valid(S, i)) {
int u = v;
if (!u) u = _rowl(j, i) || _col(S, i) || _rowr(S, i - 1);
upd(dp[i][S][u], dp[i - 1][j][v]);
}
}
}
}
int ans = 0;
for (int i = 0; i < 27; i++) {
if ((bit(i, 0) | bit(i, 1) | bit(i, 2)) < 2) {
upd(ans, dp[n][i][1]);
}
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct xy {
int x, y;
};
int main() {
xy a[3];
int n;
cin >> n;
vector<int> b;
b.resize(n + 1);
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) {
if (b[i] % 2 == 0) {
a[0].x++;
a[0].y = i;
} else {
a[1].x++;
a[1].y = i;
}
}
if (a[0].x > a[1].x)
cout << a[1].y;
else
cout << a[0].y;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> F;
int i = 2, counter = 0;
while (n > 1) {
while (n % i == 0) {
n = n / i;
F.push_back(i);
counter++;
}
if (n % i != 0) {
i++;
}
}
if (counter < k) {
cout << -1 << endl;
} else {
int i = 0;
for (; i < k - 1; i++) {
cout << F[i] << " ";
}
int last = 1;
while (i < F.size()) {
last *= F[i];
i++;
}
cout << last << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int a, b;
cin >> a >> b;
cout << a + b;
int d;
}
int main(void) { solve(); }
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long Mod = 1000000007, Mod2 = 998244353;
int q, l, r, cnt;
string str;
vector<int> vec[26];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> str >> q;
for (int i = (0); i < (int(str.size())); i++) vec[str[i] - 'a'].push_back(i);
while (q--) {
cin >> l >> r;
l--, r--;
if (l == r || str[l] != str[r]) {
cout << "Yes\n";
continue;
}
cnt = 0;
for (int i = (0); i < (int(26)); i++) {
if (vec[i].empty()) continue;
if (vec[i].back() < l) continue;
if ((*lower_bound(vec[i].begin(), vec[i].end(), l)) <= r) cnt++;
}
if (cnt >= 3)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x;
cin >> n >> m;
if (n >= m) {
if (n % 2 == 0) {
x = n / 2;
} else {
x = (n / 2) + 1;
}
while (x % m != 0) {
x += 1;
}
} else if (n < m) {
x = -1;
}
cout << x << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long sum[5000005];
long long col[5000005];
void pushup(int rt) { sum[rt] = min(sum[rt << 1], sum[rt << 1 | 1]); }
void pushdown(int rt, int m) {
if (col[rt]) {
col[rt << 1] += col[rt];
col[rt << 1 | 1] += col[rt];
sum[rt << 1] += col[rt];
sum[rt << 1 | 1] += col[rt];
col[rt] = 0;
}
}
void build(int l, int r, int rt) {
col[rt] = 0;
if (l == r) {
scanf("%I64d", &sum[rt]);
return;
}
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
}
void update(int L, int R, int v, int l, int r, int rt) {
if (L <= l && r <= R) {
col[rt] += v;
sum[rt] += v;
return;
}
pushdown(rt, r - l + 1);
int m = (l + r) >> 1;
if (L <= m) update(L, R, v, l, m, rt << 1);
if (R > m) update(L, R, v, m + 1, r, rt << 1 | 1);
pushup(rt);
}
long long query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return sum[rt];
pushdown(rt, r - l + 1);
int m = (l + r) >> 1;
long long ret = INT_MAX;
if (L <= m) ret = min(ret, query(L, R, l, m, rt << 1));
if (R > m) ret = min(ret, query(L, R, m + 1, r, rt << 1 | 1));
return ret;
}
int main() {
int n, m, a, b, c;
char op;
scanf("%d", &n);
build(1, n, 1);
scanf("%d", &m);
while (m--) {
scanf("%d%d%c", &a, &b, &op);
a++;
b++;
if (a <= b) {
if (op == ' ') {
scanf("%d", &c);
if (c == 0) continue;
update(a, b, c, 1, n, 1);
} else
printf("%I64d\n", query(a, b, 1, n, 1));
} else {
if (op == ' ') {
scanf("%d", &c);
if (c == 0) continue;
update(a, n, c, 1, n, 1);
update(1, b, c, 1, n, 1);
} else
printf("%I64d\n", min(query(a, n, 1, n, 1), query(1, b, 1, n, 1)));
}
}
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
const int size = 222111;
struct node {
int d, a, b, c;
} s[size];
long long w[size], r[size];
vector<int> V[size];
int q[size], qin, qout;
int main() {
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> s[i].a >> s[i].b >> s[i].c;
s[i].d = -1;
w[s[i].a] += s[i].c;
w[s[i].b] += s[i].c;
V[s[i].a].push_back(i);
V[s[i].b].push_back(i);
}
q[qout++] = 1;
while (qin != qout) {
int u = q[qin++];
for (int i = 0; i < V[u].size(); i++) {
int to = V[u][i];
if (s[to].d != -1) continue;
int x;
if (u == s[to].a) {
s[to].d = 0;
x = s[to].b;
} else {
s[to].d = 1;
x = s[to].a;
}
r[x] += s[to].c;
if (x != n && r[x] * 2 == w[x]) q[qout++] = x;
}
}
for (int i = 0; i < m; i++) cout << s[i].d << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
long long pos[1000009];
int main() {
long long i, j, k, l, m, n, sum = 0;
cin >> n;
memset(pos, -1, sizeof pos);
for (i = 1; i <= n; i++) {
cin >> l;
if (pos[l] != -1)
sum += (2 * (i - pos[l]) * (n - i + 1)) - 1;
else
sum += (2 * (i) * (n - i + 1)) - 1;
pos[l] = i;
}
double ans, num;
num = (double)n;
num = num * num;
ans = (double)sum;
ans = (ans) / num;
cout << std::setprecision(12) << ans << '\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string y;
int n;
cin >> n >> y >> y;
if (y == "week") {
if (n == 5 || n == 6) {
cout << 53;
} else
cout << 52;
} else {
cout << (n <= 29 ? "12" : n == 30 ? "11" : "7");
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dp[11][60000];
int P[11];
vector<int> a;
int r(int p, int mask, vector<int> a) {
if (p + 1 == a.size()) return 1;
if (dp[p][mask] != -1) return dp[p][mask];
if (a[p] == 0) return dp[p][mask] = r(p + 1, mask, a);
int res = r(p + 1, mask, a);
for (int i = (p + 1); i < (a.size()); ++i) {
if (a[i] > 0) {
int nmask = 2;
if (a[p] == 1) {
nmask -= P[i] * a[i];
a[i]--;
nmask += P[i] * a[i];
res += r(p + 1, nmask, a);
a[i]++;
} else
res += r(p + 1, nmask, a);
}
}
return dp[p][mask] = res;
}
int n;
int dp1[510][510];
int mod = 1000000007;
int r1(int p, int have) {
if (p == n) return 1;
if (dp1[p][have] != -1) return dp1[p][have];
if (a[p] == 2) return dp1[p][have] = ((n - p) * 1ll * r1(p + 1, have)) % mod;
int res = 0;
if (have) {
long long c = have * 1ll * r1(p + 1, have - 1);
c %= mod;
res += c;
if (res >= mod) res -= mod;
}
res += r1(p + 1, have + 1);
if (res >= mod) res -= mod;
return dp1[p][have] = res;
}
int main() {
scanf("%d", &n);
a.resize(n);
for (int i = (0); i < (n); ++i) {
scanf("%d", &a[i]);
}
sort(a.rbegin(), a.rend());
memset(dp1, -1, sizeof(dp1));
cout << r1(0, 0) << endl;
return 0;
}
| 7 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const long long mod = 1e9 + 7;
const int oo = 1e9;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int n;
int t[N];
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
scanf("%d", &n);
int mx = -1;
int g;
for (int i = 0; i <= n - 1; i++) {
scanf("%d", &t[i]);
mx = max(mx, t[i]);
}
g = mx;
for (int i = 0; i <= n - 1; i++) {
g = gcd(t[i], g);
}
int ans = (mx / g) - n;
if (ans % 2 == 0)
printf("Bob");
else
printf("Alice");
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, f, t, s = -1e9;
cin >> n >> k;
while (n--) {
cin >> f >> t;
if (t <= k)
s = max(s, f);
else
s = max(s, f - t + k);
}
cout << s << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, a[100100], i, m, s;
int main() {
scanf("%lld", &n);
for (long long i = 0; i < n; i++) scanf("%lld", &a[i]);
std ::sort(a, a + n);
for (long long i = n - 2; i >= 0; i--)
if (a[i + 1] - a[i] < 2) {
if (m)
s += m * a[i], m = 0;
else
m = a[i];
i--;
}
printf("%lld\n", s);
return 0;
}
| 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.