solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m, a[N], b[N];
struct edge {
int from, to, v;
} e[N * 2];
bool cmp(edge a, edge b) { return a.v > b.v; }
int cnt;
long long ans;
int f[N];
int find(int k) {
if (f[k] == k) return k;
return f[k] = find(f[k]);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) cin >> b[i];
for (int i = 1; i <= m + n; i++) f[i] = i;
for (int i = 1; i <= n; i++) {
int tot;
cin >> tot;
for (int j = 1; j <= tot; j++) {
int x;
cin >> x;
e[++cnt] = (edge){i, x + n, a[i] + b[x]};
ans += a[i] + b[x];
}
}
sort(e + 1, e + cnt + 1, cmp);
for (int i = 1; i <= cnt; i++) {
int u = e[i].from, v = e[i].to, w = e[i].v;
u = find(u);
v = find(v);
if (f[u] != f[v]) {
f[u] = f[v];
ans -= w;
}
}
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
inline long long refine(long long x) {
while (x < 0) x += MOD;
return x % MOD;
}
inline long long add(long long x, long long y) {
return (refine(x) + refine(y)) % MOD;
}
inline long long add(long long x, long long y, long long z) {
return add(add(x, y), z);
}
inline long long add(long long x, long long y, long long z, long long w) {
return add(add(x, y), add(z, w));
}
inline long long sub(long long x, long long y) {
return (refine(x) - refine(y) + MOD) % MOD;
}
inline long long mult(long long x, long long y) {
return (refine(x) * refine(y)) % MOD;
}
inline long long mult(long long x, long long y, long long z) {
return mult(mult(x, y), z);
}
inline long long mult(long long x, long long y, long long z, long long w) {
return mult(mult(x, y), mult(z, w));
}
long long modMultInv(long long denom, long long exp) {
if (exp == 0) return 1;
if (exp == 1) return denom % MOD;
if (exp % 2 == 0) {
long long d = modMultInv(denom, exp / 2);
return mult(d, d);
}
long long d = modMultInv(denom, (exp - 1) / 2);
return mult(d, d, denom);
}
long long get(long long n, long long k) {
k--;
n--;
long long x = 1, y = 1;
for (long long i = 0; i < k; i++) {
x = mult(x, n - i);
y = mult(y, i + 1);
}
long long res = mult(x, modMultInv(y, MOD - 2));
return res;
}
void solve() {
long long n, k;
cin >> n >> k;
if (k == 1) {
cout << n;
return;
}
if (k > n) {
cout << 0;
return;
}
long long res = 0;
long long div = 1;
while (n / div >= k) {
res = add(res, get(n / div, k));
div++;
}
cout << res;
}
int main() {
solve();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int *a = new int[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
cout << a[n - 1];
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n, d;
scanf("%d%d", &n, &d);
for (int i = 0; i < n; i++) scanf("%d", a + i);
long long ans = 0;
for (int i = 0; i < n; i++) {
int x = lower_bound(a, a + n, a[i] + d) - a;
long long t;
if (x != n && a[x] == a[i] + d) {
t = x - i;
} else {
t = x - i - 1;
}
if (t >= 2) {
ans += (t - 1) * t / 2;
}
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
vector<long long> v(10, 0);
string s;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'L') {
for (int j = 0; j < 10; j++)
if (v[j] == 0) {
v[j] = 1;
break;
}
} else if (s[i] == 'R') {
for (int j = 9; j >= 0; j--)
if (v[j] == 0) {
v[j] = 1;
break;
}
} else
v[s[i] - '0'] = 0;
}
for (int i = 0; i < v.size(); i++) cout << v[i];
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = 1 << 29;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n >> b) & 1; }
inline void set_bit(int& n, int b) { n |= two(b); }
inline void unset_bit(int& n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) {
int res = 0;
while (n && ++res) n -= n & (-n);
return res;
}
template <class T>
void chmax(T& a, const T& b) {
a = max(a, b);
}
template <class T>
void chmin(T& a, const T& b) {
a = min(a, b);
}
int main() {
int n;
scanf("%d", &n);
string a, b;
cin >> a >> b;
int z = 0, o = 0, zero = 0, one = 0;
for (int i = 0; i < (n); i++) {
if (a[i] == '0')
zero++;
else
one++;
}
for (int i = 0; i < (n); i++) {
if (b[i] == '0') {
if (a[i] == '0')
z++;
else if (a[i] == '1')
o++;
}
}
long long total = (long long)z * one + (long long)o * zero - (long long)z * o;
printf("%I64d\n", total);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 112345;
int q, x, tot1, tot2, trie1[maxn * 15][26], trie2[maxn * 62][2],
End1[maxn * 15], End2[maxn * 62], root1[maxn], root2[maxn];
char op[20], s[20];
inline int newnode1(int now) {
End1[++tot1] = End1[now];
for (int i = 0; i < 26; ++i) trie1[tot1][i] = trie1[now][i];
return tot1;
}
inline void insert1(int &root, char s[], int v) {
int n = strlen(s);
root = newnode1(root);
int now = root;
for (int i = 0; i < n; ++i) {
int x = s[i] - 'a';
trie1[now][x] = newnode1(trie1[now][x]);
now = trie1[now][x];
}
End1[now] = v;
}
inline int query1(int now, char s[]) {
int n = strlen(s);
for (int i = 0; i < n; ++i) {
int x = s[i] - 'a';
if (!trie1[now][x]) return 0;
now = trie1[now][x];
}
return End1[now];
}
inline int newnode2(int now) {
End2[++tot2] = End2[now];
for (int i = 0; i < 2; ++i) trie2[tot2][i] = trie2[now][i];
return tot2;
}
inline void insert2(int &root, int t, int v) {
root = newnode2(root);
int now = root;
for (int i = 30; i >= 0; --i) {
int x = (t >> i) & 1;
trie2[now][x] = newnode2(trie2[now][x]);
now = trie2[now][x];
End2[now] += v;
}
}
inline int query2(int now, int t) {
int ret = 0;
for (int i = 30; i >= 0; --i) {
int x = (t >> i) & 1;
if (x) ret += End2[trie2[now][0]];
if (!trie2[now][x]) return ret;
now = trie2[now][x];
}
return ret;
}
int main() {
scanf("%d", &q);
for (int i = 1; i <= q; ++i) {
scanf("%s", op);
root1[i] = root1[i - 1];
root2[i] = root2[i - 1];
if (op[0] == 's') {
scanf("%s%d", s, &x);
int v = query1(root1[i], s);
if (v) insert2(root2[i], v, -1);
insert2(root2[i], x, 1);
insert1(root1[i], s, x);
} else if (op[0] == 'r') {
scanf("%s", s);
int v = query1(root1[i], s);
if (v) {
insert2(root2[i], v, -1);
insert1(root1[i], s, 0);
}
} else if (op[0] == 'q') {
scanf("%s", s);
int v = query1(root1[i], s);
if (!v)
puts("-1");
else
printf("%d\n", query2(root2[i], v));
fflush(stdout);
} else {
scanf("%d", &x);
root1[i] = root1[i - x - 1];
root2[i] = root2[i - x - 1];
}
}
return 0;
}
| 4 |
#include <iostream>
using namespace std;
struct node{
int key;
node *left = NULL;
node *right = NULL;
node *parent = NULL;
node(int k, node*l = NULL, node*r = NULL, node*p = NULL) : key(k), left(l), right(r), parent(p) {}
};
node *Tree;
void insert(int z)
{
node *y = NULL;
node *x = Tree;
while(x != NULL){
y = x;
if(z < x->key)
x = x->left;
else
x = x->right;
}
if(y == NULL)
Tree = new node(z);
else if(z < y->key)
y->left = new node(z, NULL, NULL, y);
else
y->right = new node(z, NULL, NULL, y);
}
void midprint(node *root)
{
if(root == NULL) return;
midprint(root->left);
cout << " " << root->key;
midprint(root->right);
}
void preprint(node *root)
{
if(root == NULL) return;
cout << " " << root->key ;
preprint(root->left);
preprint(root->right);
}
node* find(int k)
{
node *x = Tree;
while(x != NULL && x->key != k){
if(k < x->key)
x = x->left;
else
x = x->right;
}
return x;
}
node* TreeSuccessor(node* x)
{
x = x->right;
while(x != NULL && x->left != NULL)
x = x->left;
return x;
}
void del(int k)
{
node *z = find(k);
if(z == NULL) return;
node *y = NULL;
if(z->left == NULL || z->right == NULL) y = z;
else y = TreeSuccessor(z);
node* x;
if(y->left != NULL)
x = y->left;
else
x = y->right;
if(x != NULL)
x->parent = y->parent;
if(y->parent == NULL)
Tree = x;
else{
if(y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
}
if( y != z)
z->key = y->key;
delete y;
}
void print(node *root)
{
midprint(root);
cout << endl;
preprint(root);
cout << endl;
}
int main()
{
int n;
cin >> n;
while(n--){
string s;
cin >> s;
if(s[0] == 'i'){
int k;
cin >> k;
insert(k);
}
else if(s[0] == 'p')
print(Tree);
else if(s[0] == 'f'){
int k;
cin >> k;
node *ans = find(k);
if(ans == NULL ) cout << "no" << endl;
else cout << "yes" <<endl;
}
else if(s[0] == 'd'){
int k;
cin >> k;
del(k);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
if(s=="a"||s=="i"||s=="u"||s=="o"||s=="e")cout<<"vowel"<<endl;
else cout<<"consonant"<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
double xo;
cin >> a >> b;
if (a < b)
cout << -1;
else {
if (a == b)
xo = a;
else {
xo = min((a - b) / floor(((a - b) / 2.0) / b),
(a + b) / floor(((a + b) / 2.0) / b));
xo /= 2.0;
}
cout << fixed << setprecision(12) << xo;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long a, b, c, i, j, k, l, m, t, x, y, n, z;
string s;
map<long long, long long> mp;
map<long long, long long>::iterator it;
cin >> a >> s;
n = s.size();
long long a1[n + 1];
a1[0] = 0;
for (i = 1; i < n + 1; i++) a1[i] = s[i - 1] - '0' + a1[i - 1];
for (i = 0; i < n + 1; i++) {
for (j = i + 1; j < n + 1; j++) {
mp[a1[j] - a1[i]]++;
}
}
c = 0;
if (!a) c = (mp[0] * (n * (n + 1) - mp[0]));
if (!a) {
cout << c << endl;
return 0;
}
for (i = 0; i < n + 1; i++) {
for (j = i + 1; j < n + 1; j++) {
if (a1[j] - a1[i] != 0)
if (a % (a1[j] - a1[i]) == 0) {
it = mp.find(a / (a1[j] - a1[i]));
if (it != mp.end()) c = c + mp[a / (a1[j] - a1[i])];
}
}
}
cout << c << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int n, m, d, l;
long long L, R;
int main() {
scanf("%d%d%d%d", &n, &d, &m, &l);
for (int i = 1; i <= n; i++) {
L = l + 1ll * (i - 1) * m, R = 1ll * i * m - 1;
if (L / d != R / d) break;
}
printf("%I64d\n", L / d * d + d);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char s[5005];
int main() {
int i, j, k;
scanf("%s", s);
int len, n, m;
len = strlen(s);
int Q, D, ans;
ans = 0;
for (i = 0; i < len; i++) {
Q = 0;
D = 0;
for (j = i; j < len; j++) {
Q += s[j] == '(';
Q -= s[j] == ')';
D += s[j] == '?';
if (Q == D) ans++;
if (D > Q) swap(Q, D);
if (Q + D < 0) break;
}
}
printf("%d", ans);
return 0;
}
| 1 |
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> //Policy Based Data Structure
// using namespace __gnu_pbds; //Policy Based Data Structure
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; //Policy Based Data Structure
// #define gc getchar_unlocked
// #define pqb priority_queue<int>
// #define pqs priority_queue<int, vi, greater<int> >
// #define mk(arr,n,type) type *arr = new type[n]
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long
#define endl '\n'
#define w(t) int t; cin>>t; while(t--)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x,y) cout << #x << "=" << x << "," << #y << "=" << y << endl
void print(bool n) {
if (n) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x,val) memset(x, val, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it,a) for(auto it = a.begin(); it != a.end(); it++)
#define ps(x,y) fixed<<setprecision(y)<<x
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define PI 3.1415926535897932384626
#define inf 1e18
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //Random Shuffler
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef map<int, int> mii;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
// const int N = 3e5, M = N;
// vi g[N];
// no of prime numbers in range : (70,19) , (1000,168), (100000,1229) , (sqrt(10^9),3409)
//=======================
void sol()
{
int a, b;
cin >> a >> b;
if (a - b == 0)
{
cout << 0 << " " << 0 << endl;
return;
}
if (a < b)
{
swap(a, b);
}
int ans = a - b;
int ans2 = inf;
if (((a + ans - 1) / ans) * ans - a >= 0)
{
ans2 = min(ans2, ((a + ans - 1) / ans) * ans - a);
}
if ( a - (a / ans)*ans >= 0)
{
ans2 = min(ans2, a - (a / ans) * ans);
}
if (((b + ans - 1) / ans)*ans - b >= 0)
{
ans2 = min(ans2, ((b + ans - 1) / ans) * ans - b);
}
if (b - (b / ans)*ans >= 0)
{
ans2 = min(ans2, b - (b / ans) * ans);
}
cout << ans << " " << ans2 << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
w(t)
sol();
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % mod;
base = (base * base) % mod;
exp >>= 1;
}
return result;
}
// void ipgraph(int n, int m){
// int i, u, v;
// while(m--){
// cin>>u>>v;
// g[u-1].pb(v-1);
// g[v-1].pb(u-1);
// }
// }
//
// void dfs(int u, int par){
// for(int v:g[u]){
// if (v == par) continue;
// dfs(v, u);
// }
// }
| 1 |
#include <bits/stdc++.h>
using namespace std;
int maximum(int a, int b) {
if (a > b)
return a;
else
return b;
}
bool cmp(const pair<int, int>& firs, const pair<int, int>& sec) {
return firs.first < sec.first;
}
string in[100005];
char mark[100005];
int lag[100005];
int main() {
int n;
int l, i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
cin >> in[i];
}
l = in[0].size();
for (i = 0; i < l; i++) {
mark[i] = in[0][i];
}
int j;
for (i = 1; i < n; i++) {
for (j = 0; j < l; j++) {
if (mark[j] == in[i][j]) {
continue;
} else {
if (mark[j] == '?' && lag[j] == 0) {
mark[j] = in[i][j];
} else if (in[i][j] == '?')
continue;
else if (mark[j] != '?' && in[i][j] != '?') {
mark[j] = '?';
lag[j] = 1;
}
}
}
}
for (i = 0; i < l; i++) {
if (lag[i] == 1)
printf("%c", mark[i]);
else {
if (mark[i] == '?')
printf("a");
else
printf("%c", mark[i]);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[5][5] = {0};
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << (a[i][j] + a[i][j + 1] + a[i][j - 1] + a[i - 1][j] + a[i + 1][j] +
1) %
2;
}
cout << endl;
}
}
| 1 |
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
inline long long mod_fibonacci_numberDiv2(int n)
{
if (n < 3)
{
return n == 0 ? 0 : 1;
}
int dp[3] = { 1, 1, 2 };
for (int i = 3; i < n; i += 3)
{
dp[0] = (dp[1] + dp[2]) % 1000000007;
dp[1] = (dp[2] + dp[0]) % 1000000007;
dp[2] = (dp[0] + dp[1]) % 1000000007;
}
return dp[(n - 1) % 3];
}
long long K;
int main()
{
cin >> K;
long long x_yDiv2 = 2 * sqrt(K - sqrt(K / 100) - 0.01) - 1; // x + y
long long min_Div2 = ((x_yDiv2 + 1) * (x_yDiv2 + 1) / 4) + 1;
long long max_Div2 = ((x_yDiv2 + 2) * (x_yDiv2 + 2) / 4);
long long x, y;
if (2 * K <= min_Div2 + max_Div2)
{
y = (K - min_Div2) * 2;
}
else
{
y = (max_Div2 - K) * 2 + 1;
}
x = x_yDiv2 - y;
cout << (mod_fibonacci_numberDiv2(x + 2) * mod_fibonacci_numberDiv2(y + 2)) % 1000000007 << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct line
{
int m, c;
int eval(int x)
{
return (m*x+c);
}
double intersect(line &o)
{
return (double)(c-o.c)/(o.m-m);
}
};
int32_t main()
{
int n, c;
cin>>n>>c;
vector<int>h(n);
for(auto &x: h)
cin>>x;
deque<line>q;
int qs=1;
q.push_back({-2*h[n-1], h[n-1]*h[n-1]});
int dp=0;
for(int i=n-2; i>=0; --i)
{
while(qs>1 && q[0].eval(h[i])>=q[1].eval(h[i]))
{
q.pop_front();
qs--;
}
dp=q[0].eval(h[i])+h[i]*h[i]+c;
line nl={-2*h[i], dp+h[i]*h[i]};
while(qs>1 && q[qs-1].intersect(nl)>=q[qs-2].intersect(q[qs-1]))
{
q.pop_back();
qs--;
}
q.push_back(nl);
qs++;
}
cout<<dp;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x20202020;
const int mod = 1000000007;
template <class T>
inline void read(T& first) {
bool fu = 0;
char c;
for (c = getchar(); c <= 32; c = getchar())
;
if (c == '-') fu = 1, c = getchar();
for (first = 0; c > 32; c = getchar()) first = first * 10 + c - '0';
if (fu) first = -first;
};
template <class T>
inline void read(T& first, T& second) {
read(first);
read(second);
}
template <class T>
inline void read(T& first, T& second, T& z) {
read(first);
read(second);
read(z);
}
template <class T>
inline void read(T& first, T& second, T& z, T& q) {
read(first);
read(second);
read(z);
read(q);
}
const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1};
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long powmod(long long a, long long b, long long mod) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
const int N = 111111, M = 111111;
int l, m, n, t, C;
long long sum[N], num[N];
int main() {
scanf("%d%d", &n, &m);
for (int i = (1); i <= (n); ++i)
for (int j = (i + 1); j <= (n); ++j) {
scanf("%d", &t);
if (t != -1) sum[i] += t, sum[j] += t, num[i]++, num[j]++;
}
long long s = 0;
if (m == 2) {
for (int i = (1); i <= (n); ++i) s += 2 * sum[i] * (num[i] - 1);
printf("%I64d\n", s / n / (n - 1));
} else {
for (int i = (1); i <= (n); ++i) s += sum[i];
printf("%I64d\n", s / n);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long t, k, n, ans[21], cnt[21];
string s;
bool dfs(long long now, long long val, bool tag) {
if (val > k) return 0;
if (now == n && val <= k) {
for (register long long i = 0; i < n; ++i) cout << ans[i];
cout << endl;
return 1;
}
for (register long long i = tag ? s[now] - '0' : 0; i <= 9; ++i) {
++cnt[i];
ans[now] = i;
if (cnt[i] == 1) {
if (dfs(now + 1, val + 1, tag && i == s[now] - '0')) return 1;
} else if (dfs(now + 1, val, tag && i == s[now] - '0'))
return 1;
--cnt[i];
}
return 0;
}
signed main() {
cin >> t;
while (t--) {
for (register long long i = 0; i <= 10; ++i) cnt[i] = 0;
for (register long long i = 0; i <= 10; ++i) ans[i] = 0;
cin >> s >> k;
n = s.length();
if (!dfs(0, 0, 1)) {
cout << 10;
for (register long long i = 2; i < k; ++i) cout << i;
cout << endl;
}
}
return 0;
}
| 6 |
#include<cstdio>
#include<map>
using namespace std;int mp[144],m,n,hs,ch;int dir[]={-12,-1,1,12};map<int,int>memo;int func(int p,int d,int u){while(1){int h=mp[p];if(h>=0){int s=0;mp[p]=-1;hs^=1<<h;if(u>12){for(int i=0;i<4;i++)s+=func(p+dir[i],dir[i],u-1);}else{int mi=h<<23|hs;map<int,int>::iterator it=memo.find(mi);if(it==memo.end()){for(int i=0;i<4;i++)s+=func(p+dir[i],dir[i],u-1);memo[mi]=s;}else{s=it->second;}}hs|=1<<h;mp[p]=h;return s;}if(h==-9)break;p+=d;}return 0;}int main(){int i,j,p,t;for(;scanf("%d%d",&m,&n),m;memo.clear()){hs=0;for(i=0;i<144;i++)mp[i]=-9;for(i=1;i<=n;i++)for(j=1;j<=m;j++){p=i*12+j;scanf("%d",&t);switch(t){case 2:ch=p;case 0:mp[p]=-1;break;case 1:memo[hs<<23]=0;mp[p]=hs++;break;}}for(i=0;i<4;){t=dir[i++];for(j=ch;mp[j+=t]!=-9;mp[j]>=0?memo[mp[j]<<23]=1:0);}j=hs;hs=(1<<hs)-1;t=0;for(i=-1;++i<4;)t+=func(ch+dir[i],dir[i],j);printf("%d\n",t);}}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long a, b;
cin >> a >> b;
if (b < 2 * a)
cout << "-1 -1" << endl;
else
cout << a << ' ' << 2 * a << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 5;
struct Item {
int val, idx;
bool operator<(const Item &item) const { return val < item.val; }
};
Item item[MAXN];
int main() {
int q;
scanf("%d", &q);
while (q--) {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int val;
scanf("%d", &val);
item[i] = {val, i + 1};
}
sort(item, item + n);
int cnt_val = 0;
int ans = 0;
for (int pre_r = 0, pre_cnt = 0, i = 0; i < n;) {
cnt_val++;
int lidx = i;
int cur_l = n + 1, cur_r = 0;
while (i < n && item[i].val == item[lidx].val) {
cur_l = min(cur_l, item[i].idx);
cur_r = max(cur_r, item[i].idx);
i++;
}
if (pre_r < cur_l) {
pre_cnt++;
} else {
pre_cnt = 1;
}
pre_r = cur_r;
ans = max(ans, pre_cnt);
}
printf("%d\n", cnt_val - ans);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long OO = 10000000000000;
int x[502][502];
char c[502];
int n, m, t, f;
bool yes(int i) {
for (int j = 1; j <= n; j++)
if (i != j && x[i][j] == 0) return false;
return true;
}
void setx(int i, char cx) {
c[i] = cx;
for (int j = 1; j <= n; j++)
if (x[i][j] == 1 && c[j] != 'b') c[j] = cx;
}
bool check() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if ((x[i][j] == 1 && abs(c[i] - c[j]) > 1) ||
(x[i][j] == 0 && i != j && abs(c[i] - c[j]) < 2)) {
return false;
}
return true;
}
int main() {
cin >> n >> m;
memset(x, 0, sizeof x);
memset(c, 'd', sizeof c);
for (int i = 0; i < m; i++) {
cin >> t >> f;
x[t][f] = 1;
x[f][t] = 1;
}
for (int i = 1; i <= n; i++)
if (yes(i)) c[i] = 'b';
int d = 0;
for (int i = 1; i <= n; i++)
if (c[i] == 'd') {
if (d == 0)
setx(i, 'a');
else if (d == 1)
setx(i, 'c');
else {
cout << "No";
return 0;
}
d++;
}
if (check()) {
cout << "Yes" << endl;
for (int i = 1; i <= n; i++) cout << c[i];
} else
cout << "No";
return 0;
}
| 1 |
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<cstring>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)(n); i++)
double X[512];
double Y[512];
double Z[512];
bool see[512];
double len(double x, double y, double z) {
return sqrt(x * x + y * y + z * z);
}
int main() {
int N;
while( 1 ) {
memset(see, 0, sizeof(see));
scanf("%d", &N);
if( N == 0 ) break;
REP(i, N) {
double x, y, z;
scanf("%lf%lf%lf", &x, &y, &z);
X[i] = x;
Y[i] = y;
Z[i] = z;
}
int M;
scanf("%d", &M);
REP(i, M) {
double x, y, z, t;
scanf("%lf%lf%lf%lf", &x, &y, &z, &t);
REP(n, N) {
double s = ( x * X[n] + y * Y[n] + z * Z[n]) / ( len(x,y,z) * len(X[n], Y[n], Z[n]) );
if( acos(s) <= t )
see[n] = true;
}
}
int ans = 0;
REP(i, N) if( see[i] ) ans++;
printf("%d\n", ans);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct vect {
long double x[5];
void get() {
for (int i = 0; i < 5; i++) cin >> x[i];
}
};
vect operator-(vect a, vect b) {
vect c;
for (int i = 0; i < 5; i++) c.x[i] = a.x[i] - b.x[i];
return c;
}
vect operator^(vect a, vect b) { return b - a; }
long double operator*(vect a, vect b) {
long double ret = 0;
for (int i = 0; i < 5; i++) ret += a.x[i] * b.x[i];
return ret;
}
long double mod(vect a) { return sqrtl(a * a); }
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
int n;
cin >> n;
vect A[n];
for (int i = 0; i < n; i++) A[i].get();
vector<int> ats;
for (int i = 0; i < n; i++) {
bool good = true;
for (int a = 0; a < n && good; a++) {
if (a == i) continue;
for (int b = 0; b < n && good; b++) {
if (b == i || b == a) continue;
vect x = A[i] ^ A[b];
vect y = A[i] ^ A[a];
if (acosl((x * y) / (mod(x) * mod(y))) + (1e-14) <
(3.14159265358979323846264 / 2.0)) {
good = false;
}
}
}
if (good) ats.push_back(i);
}
cout << ats.size() << "\n";
for (int i : ats) cout << i + 1 << " ";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m; cin >> n >> m;
vector<int> h(n);
vector<bool> can(n, true);
for (int i=0; i<n; i++) {
cin >> h[i];
}
for (int i=0; i<m; i++) {
int u, v; cin >> u >> v;
u--, v--;
if (h[u] <= h[v]) can[u]=0;
if (h[v] <= h[u]) can[v]=0;
}
int ans=0;
for (bool b : can) ans+=b;
cout << ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct node {
int stat;
int doosra;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int stat[n + 1];
for (int i = 1; i <= n; i++) cin >> stat[i];
vector<int> temp[n + 1];
for (int i = 1; i <= m; i++) {
int var, k;
cin >> var;
for (int j = 1; j <= var; j++) {
cin >> k;
temp[k].push_back(i);
}
}
vector<node> edges[m + 1];
for (int i = 1; i <= n; i++) {
int first = temp[i].at(0);
int second = temp[i].at(1);
edges[first].push_back(node{stat[i], second});
edges[second].push_back(node{stat[i], first});
}
queue<int> q;
bool visited[m + 1];
int toggle[m + 1];
for (int i = 1; i <= m; i++) {
visited[i] = false;
}
for (int i = 1; i <= m; i++) {
if (visited[i]) continue;
toggle[i] = 0;
q.push(i);
while (!q.empty()) {
int var = q.front();
q.pop();
visited[var] = true;
for (auto idx : edges[var]) {
if (idx.stat == 0) {
if (!visited[idx.doosra]) {
toggle[idx.doosra] = 1 - toggle[var];
q.push(idx.doosra);
} else if (visited[idx.doosra]) {
if (toggle[idx.doosra] == toggle[var]) {
cout << "NO";
return 0;
}
}
}
if (idx.stat == 1) {
if (!visited[idx.doosra]) {
toggle[idx.doosra] = toggle[var];
q.push(idx.doosra);
} else if (visited[idx.doosra]) {
if (toggle[idx.doosra] != toggle[var]) {
cout << "NO";
return 0;
}
}
}
}
}
}
cout << "YES";
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int A, int B) {
if (!B) return A;
return gcd(B, A % B);
}
int n;
int a[200005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
long long int sum = a[n - 1] - a[0];
long long int ans = a[1] - a[0];
for (int i = 1; i < n; i++) {
ans = gcd(a[i] - a[i - 1], ans);
sum += a[n - 1] - a[i];
}
cout << sum / ans << " " << ans;
}
| 4 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:640000000")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)2e5 + 10;
const int maxv = (int)1e6;
const int mod = (int)1e9 + 7;
int C[maxn];
int f[maxn];
int a[maxv + 1];
int coef[maxv + 1];
int T = 0;
void add(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
void sub(int &x, int y) {
x -= y;
if (x < 0) x += mod;
}
int binpow(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1) res = res * 1LL * a % mod;
a = a * 1LL * a % mod;
b >>= 1;
}
return res;
}
int main() {
int n, k, q;
cin >> n >> k >> q;
f[0] = 1;
for (int i = 1; i < maxn; i++) {
f[i] = f[i - 1] * 1LL * i % mod;
}
for (int i = k; i < maxn; i++) {
C[i] = f[i] * 1LL * binpow(f[k] * 1LL * f[i - k] % mod, mod - 2) % mod;
}
for (int i = 1; i <= maxv; i++) {
coef[i] = i;
}
for (int i = 1; i <= maxv; i++) {
for (int j = 2; i * j <= maxv; j++) {
coef[i * j] -= coef[i];
}
}
for (int i = 0; i < n; i++) {
int x;
scanf("%d", &x);
for (int j = 1; j * j <= x; j++)
if (x % j == 0) {
a[j]++;
if (j * j != x) a[x / j]++;
}
}
int res = 0;
for (int i = 1; i <= maxv; i++) {
add(res, C[a[i]] * 1LL * coef[i] % mod);
}
while (q--) {
int x;
scanf("%d", &x);
for (int i = 1; i * i <= x; i++)
if (x % i == 0) {
sub(res, C[a[i]] * 1LL * coef[i] % mod);
a[i]++;
add(res, C[a[i]] * 1LL * coef[i] % mod);
if (i * i == x) continue;
int j = x / i;
sub(res, C[a[j]] * 1LL * coef[j] % mod);
a[j]++;
add(res, C[a[j]] * 1LL * coef[j] % mod);
}
printf("%d\n", res);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define br "\n"
#define sp " "
using namespace std;
int main() {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
ll t;
cin>>t;
while(t--){
string s;
cin>>s;
ll i,j,n=s.length(),ans=0;
if(n==1)
cout<<0<<br;
else if(n==2)
{
if(s[0]==s[1])
cout<<1<<br;
else
cout<<0<<br;
}
else{
for(i=0;i<n-1;i++)
{
if(s[i]==s[i+1])
s[i+1]='*';
if(s[i]==s[i+2]&&i!=n-2)
s[i+2]='*';
}
for(i=0;i<n;i++)
{
if(s[i]=='*')
ans++;
}
cout<<ans<<br;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }
const int INF = 1<<29;
int solve(int N) {
int offset = 0;
int a, b, end;
cin >> a >> b; --a, --b;
vector<vector<int>> A;
for (int i = 0; i < N-1; ++i) {
vector<int> v(3);
for (int j = 0; j < 3; ++j) cin >> v[j], --v[j];
sort(v.begin(), v.end());
if (v[0] == v.back()) ++offset;
else A.push_back(v);
}
cin >> end; --end;
vector<vector<int>> dp(N, vector<int>(N, -INF));
dp[a][b] = dp[b][a] = 0;
int ma = 0;
vector<int> vma(N, -INF);
vma[a] = vma[b] = 0;
for (auto v : A) {
vector<vector<int>> chs;
// 3 つのうち 2 つが一緒のとき
if (v[0] == v[1]) {
for (int p = 0; p < N; ++p) {
chs.push_back({p, v[2], dp[p][v[1]] + 1});
}
}
else if (v[1] == v[2]) {
for (int p = 0; p < N; ++p) {
chs.push_back({p, v[0], dp[p][v[1]] + 1});
}
}
// 前の 2 個が一緒
{
chs.push_back({v[1], v[2], dp[v[0]][v[0]] + 1});
chs.push_back({v[2], v[0], dp[v[1]][v[1]] + 1});
chs.push_back({v[0], v[1], dp[v[2]][v[2]] + 1});
}
// 前の 2 個を共に捨てる
for (int i = 0; i < 3; ++i) {
for (int j = i+1; j < 3; ++j) {
chs.push_back({v[i], v[j], ma});
}
}
// 前のうち 1 個を捨てる
for (int i = 0; i < 3; ++i) {
for (int p = 0; p < N; ++p) {
chs.push_back({p, v[i], vma[p]});
}
}
// change
for (auto ch : chs) {
int a = ch[0], b = ch[1], val = ch[2];
chmax(dp[a][b], val);
chmax(dp[b][a], val);
chmax(ma, val);
chmax(vma[a], val);
chmax(vma[b], val);
}
/*
cout << "-----------" << endl;
COUT(v);
for (int a = 0; a < N; ++a) {
for (int b = 0; b < N; ++b) {
if (dp[a][b] >= 0) {
cout << a << ", " << b << ": " << dp[a][b] << endl;
}
}
}
*/
}
int res = 0;
for (int a = 0; a < N; ++a) {
for (int b = 0; b < N; ++b) {
if (a == end && b == end) chmax(res, dp[a][b] + 1);
else chmax(res, dp[a][b]);
}
}
return res + offset;
}
int main() {
int N;
while (cin >> N) cout << solve(N) << endl;
}
| 0 |
#include<iostream>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
int ans=0;
while(n>0){
n=n/k;
ans++;
}
cout<<ans<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = int(1e9) + 7;
int debug = 1;
const int N = int(5e3) + 5;
using namespace std;
int q[N][N];
int fg[N][N], pref1[N], pref2[N];
int F[N], G[N], n;
vector<int> perm;
inline int get(int ind, int val) {
if (ind < 0) return 0;
if (val == 1) return pref1[ind];
return pref2[ind];
}
bool check(vector<int> perm) {
int n = perm.size();
int i;
for (i = 0; i < n; i++) F[i] = perm[i];
for (i = 0; i < n; i++) G[perm[i]] = i;
for (i = 0; i < n; i++)
if (fg[i][i] != (F[i] ^ G[i])) return false;
for (i = 0; i < n; i++)
if (fg[i][(i + 1) % n] != (F[i] ^ G[(i + 1) % n])) return false;
return true;
}
int cnt = 0;
int query(int x, int y) {
cnt++;
cout << "? " << x << " " << y << "\n";
fflush(stdout);
int z;
cin >> z;
return z;
}
int main() {
int i, j, t;
cin >> n;
for (i = 0; i < n; i++) {
q[i][i] = query(i, i);
fg[i][i] = q[i][i];
pref1[i] = (i == 0) ? fg[i][i] : fg[i][i] ^ pref1[i - 1];
}
for (i = 0; i < n; i++) {
q[i][(i + 1) % n] = query(i, (i + 1) % n);
fg[i][(i + 1) % n] = q[i][(i + 1) % n];
pref2[i] = (i == 0) ? q[i][(i + 1) % n] : q[i][(i + 1) % n] ^ pref2[i - 1];
}
int val = 0;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
int val1 = get(j - 1, 1) ^ get(i, 1);
int val2 = get(j - 1, 2) ^ get(i - 1, 2);
fg[i][j] = val1 ^ val2;
}
}
int ans = 0;
vector<int> answer;
perm.resize(n);
for (val = 0; val < n; val++) {
set<int> s;
int flag = 0;
for (i = 0; i < n; i++) {
int ind = fg[0][i] ^ val;
if (ind >= n) {
flag = 1;
break;
}
perm[ind] = i;
}
if (perm[0] != val || flag) continue;
if (!check(perm)) continue;
ans++;
answer = perm;
}
cout << "!\n";
cout << ans << "\n";
for (i = 0; i < answer.size(); i++) cout << answer[i] << " ";
fflush(stdout);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, k, s, t, c[200005], v[200005], g[200005], mg, ans = 1e9 + 1, mv;
bool ck(int v) {
if (v < mg) return 0;
int tol = 0;
for (int i = 1; i <= k + 1; i++) {
tol += max(g[i], 3 * g[i] - v);
if (tol > t) return 0;
}
return 1;
}
int main() {
scanf("%d%d%d%d", &n, &k, &s, &t);
for (int i = 1; i <= n; i++) scanf("%d%d", c + i, v + i);
for (int i = 1; i <= k; i++) scanf("%d", g + i);
sort(g + 1, g + k + 1);
g[k + 1] = s;
mg = 0;
for (int i = k + 1; i; i--) mg = max(mg, g[i] -= g[i - 1]);
int l = 0, r = 1e9, m;
while (l <= r) {
m = (l + r) / 2;
if (ck(m)) {
r = m - 1;
mv = m;
} else {
l = m + 1;
}
}
if (mv)
for (int i = 1; i <= n; i++)
if (v[i] >= mv) ans = min(ans, c[i]);
if (ans > 1e9) ans = -1;
printf("%d", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
int d1[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int d2[8][2] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0},
{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
double n, l, v1, v2, k;
cin >> n >> l >> v1 >> v2 >> k;
double L = 0, R = 10000000000;
int it = 200;
while (it--) {
double mid = (L + R) / 2;
double w = true;
int cnt = (n + k - 1) / k;
double x = l;
double T = mid;
for (int i = 0; i < cnt; ++i) {
double y = (T * v1 * v2 - v2 * x) / (v1 - v2);
if (y > x) w = false;
T -= y / v2;
if (i != (cnt - 1)) T -= (y - v1 * y / v2) / (v2 + v1);
if (T < 0) w = false;
x -= v1 * y / v2 + v1 * (y - v1 * y / v2) / (v2 + v1);
if (x < 0) w = false;
}
if (!w)
L = mid;
else
R = mid;
}
cout << fixed << setprecision(10) << L << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
string S[3];
cin >> S[0] >> S[1] >> S[2];
int i=0;
while(1){
if(S[i].size()==0){
cout << (char)(i+'A');
return 0;
}
char c=S[i][0];
S[i].erase(S[i].begin());
i=c-'a';
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int n;
int a[21];
int b[21];
int c[21];
int res=0;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
cin>>b[i];
for(int i=1;i<=n-1;i++)
cin>>c[i];
a[0]=-1;
for(int i=1;i<=n;i++)
{res+=b[a[i]];
if(a[i]==a[i-1]+1) res+=c[a[i-1]];}
cout<<res;
}
| 0 |
// / #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <bitset>
#include <time.h>
#include <stack>
#include <sstream>
#include <regex>
#define prev asasddlsa
#define rank aljds
using namespace std;
typedef long long ll;
typedef long double dbl;
// mt19937 mrand(random_device{} ());
// int rnd(int x = INT_MAX) {
// return mrand() % x;
// }
template<typename T>
void print(const vector<T>& s){
for (auto x : s)
cout << x + 1 << ' ';
// cout << endl;
}
template<class T>
void print(const T* s, int n){
for (int i = 0; i < n; i++){
cout << s[i] << ' ';
}
cout << endl;
}
template<class T>
void print(vector<vector<T>> s){
for (int i = 0; i < s.size(); i++)
print(s[i]);
}
int main(){
srand(time(0));
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> s(n);
set<int> unused;
vector<int> deg(n);
for (int i = 0; i < n; i++){
cin >> s[i];
s[i]--;
deg[s[i]]++;
unused.insert(i);
}
vector<int> order;
while (unused.size() > 8){
// bool ok = false;
for (int x : unused){
if (order.size() == 0 || s[order.back()] != x){
if (unused.count(s[x]) && deg[s[x]] + 1 == unused.size()){
order.push_back(s[x]);
unused.erase(s[x]);
deg[s[s[x]]]--;
} else {
order.push_back(x);
unused.erase(x);
deg[s[x]]--;
}
break;
}
}
}
vector<int> p;
for (int x : unused)
p.push_back(x);
int t = order.size() ? order.back() : -1;
do {
if (t != -1 && s[t] == p[0])
continue;
bool ok = true;
for (int i = 0; i + 1 < p.size(); i++){
if (s[p[i]] == p[i + 1]){
ok = false;
break;
}
}
if (ok){
print(order);
print(p);
cout << endl;
return 0;
}
} while (next_permutation(p.begin(), p.end()));
cout << -1 << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define M 1000000007
#define make_unique(vec) vec.erase(unique(all(vec)), vec.end())
#define itc int t;cin>>t;while(t--)
#define SET(a, b) memset(a, b, sizeof(a))
#define forn(i,a, n) for (int i = a; i < (int)(n); i++)
#define tcs(n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define ld long double
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef unsigned long long ubint;
void solve();
map<ll, bool> mp;
int main()
{
fio;
int tc=1;
for (ll i = 1; i < 10e4; i++)
{
mp[i * i * i] = true;
}
cin >> tc;
tcs(tc)
{
solve();
}
}
void solve()
{
ll t;
cin >> t;
for (ll i = 1; i <= t; i++)
{
ll dif=t-i*i*i;
if(dif<=0)
{
cout << "NO" << endl;
return;
}
if(mp[dif])
{
cout << "YES" << endl;
return;
}
}
cout << "eroor" << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
using vi=vc<int>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.a<<","<<p.b<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ";
dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,"Line:",__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif
using uint=unsigned;
using ull=unsigned long long;
template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
return os<<vc<t>(all(a));
}
template<int i,class T>
void print_tuple(ostream&,const T&){
}
template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
if(i)os<<",";
os<<get<i>(t);
print_tuple<i+1,T,Args...>(os,t);
}
template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
os<<"{";
print_tuple<0,tuple<Args...>,Args...>(os,t);
return os<<"}";
}
void print(ll x,int suc=1){
cout<<x;
if(suc==1)
cout<<"\n";
if(suc==2)
cout<<" ";
}
ll read(){
ll i;
cin>>i;
return i;
}
vi readvi(int n,int off=0){
vi v(n);
rep(i,n)v[i]=read()+off;
return v;
}
template<class T>
void print(const vector<T>&v,int suc=1){
rep(i,v.size())
print(v[i],i==int(v.size())-1?suc:2);
}
string readString(){
string s;
cin>>s;
return s;
}
template<class T>
T sq(const T& t){
return t*t;
}
//#define CAPITAL
void yes(bool ex=true){
#ifdef CAPITAL
cout<<"YES"<<endl;
#else
cout<<"Yes"<<endl;
#endif
if(ex)exit(0);
}
void no(bool ex=true){
#ifdef CAPITAL
cout<<"NO"<<endl;
#else
cout<<"No"<<endl;
#endif
if(ex)exit(0);
}
void possible(bool ex=true){
#ifdef CAPITAL
cout<<"POSSIBLE"<<endl;
#else
cout<<"Possible"<<endl;
#endif
if(ex)exit(0);
}
void impossible(bool ex=true){
#ifdef CAPITAL
cout<<"IMPOSSIBLE"<<endl;
#else
cout<<"Impossible"<<endl;
#endif
if(ex)exit(0);
}
constexpr ll ten(int n){
return n==0?1:ten(n-1)*10;
}
const ll infLL=LLONG_MAX/3;
#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif
int topbit(signed t){
return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
return __builtin_popcount(t);
}
int popcount(ll t){
return __builtin_popcountll(t);
}
bool ispow2(int i){
return i&&(i&-i)==i;
}
int mask(int i){
return (int(1)<<i)-1;
}
bool inc(int a,int b,int c){
return a<=b&&b<=c;
}
template<class t> void mkuni(vc<t>&v){
sort(all(v));
v.erase(unique(all(v)),v.ed);
}
ll rand_int(ll l, ll r) { //[l, r]
#ifdef LOCAL
static mt19937_64 gen;
#else
static random_device rd;
static mt19937_64 gen(rd());
#endif
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class t>
int lwb(const vc<t>&v,const t&a){
return lower_bound(all(v),a)-v.bg;
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
using ld=long double;
ld a,b,p,q,r;
cin>>a>>b>>p>>q>>r;
ld len=p*a+(p-q)*(b-a);
ld ans=len/(r+q);
cout<<ans+b<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int ss[100001];
int tt[100001];
int main(){
string s,t;
cin>>s>>t;
int q;
cin>>q;
for(int i=1;i<=s.length();i++){
if(s[i-1]=='A'){
ss[i]=ss[i-1]+1;
}else{
ss[i]=ss[i-1]+2;
}
}
for(int i=1;i<=t.length();i++){
if(t[i-1]=='A'){
tt[i]=tt[i-1]+1;
}else{
tt[i]=tt[i-1]+2;
}
}
int a,b,c,d;
while(q--){
cin>>a>>b>>c>>d;
if(abs(ss[b]-ss[a-1]-tt[d]+tt[c-1])%3==0){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long l, r;
long long get(long long x) {
long long l1 = 0, l2 = 0, op = 1, num = 1, a = 0, pl = 1;
for (pl = 1; pl <= x; pl += num) {
if (op & 1)
l1 = (l1 + num) % mod;
else
l2 = (l2 + num) % mod;
op++;
num <<= 1;
if (pl + num > x) a = pl;
}
if (op & 1)
l1 = (l1 + x - a) % mod;
else
l2 = (l2 + x - a) % mod;
return (l1 % mod * l1 % mod + l2 % mod * (l2 + 1) % mod) % mod;
}
signed main() {
cin >> l >> r;
cout << (get(r) - get(l - 1) + mod) % mod << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int def = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string s[500];
for (int i = 0; i < n; i++) cin >> s[i];
int three = 0, even = 0, zero = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < s[i].size(); j++) {
int num = s[i][j] - '0';
three += num;
if (num % 2 == 0) even += 1;
if (num == 0) zero += 1;
}
if ((three % 3 == 0) && (even > 1 && zero >= 1))
cout << "red" << endl;
else
cout << "cyan" << endl;
three = 0;
even = 0;
zero = 0;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, inf = 1000000000;
const int N = 100010;
inline int read() {
char ch;
while (!isdigit(ch = getchar()))
;
int x = ch - '0';
while (isdigit(ch = getchar())) x = (x << 1) + (x << 3) + ch - '0';
return x;
}
int n, cnt;
int a[N], last[N];
long long f[N][2];
struct edge {
int to, next;
} e[N << 1];
void insert(int u, int v) {
e[++cnt].to = v;
e[cnt].next = last[u];
last[u] = cnt;
e[++cnt].to = u;
e[cnt].next = last[v];
last[v] = cnt;
}
void dfs(int x, int fa) {
long long t1, t2;
if (a[x] == 1) f[x][1] = 1;
f[x][0] = f[x][1] ^ 1;
for (int i = last[x]; i; i = e[i].next)
if (e[i].to != fa) {
int y = e[i].to;
dfs(y, x);
t1 = f[x][0] * (f[y][0] + f[y][1]);
t2 = (f[x][1] * (f[y][0] + f[y][1]) + f[x][0] * f[y][1]);
f[x][0] = t1 % mod;
f[x][1] = t2 % mod;
}
}
int main() {
n = read();
for (int i = 1; i < n; i++) insert(read(), i);
for (int i = 0; i < n; i++) a[i] = read();
dfs(0, 0);
cout << f[0][1] << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long overlap(long long i1, long long i2, long long j1, long long j2) {
return max(min(j2, i2) - max(j1, i1) + 1, 0LL);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long pl, pr, vl, vr, k;
cin >> pl >> pr >> vl >> vr >> k;
vector<long long> lucky;
lucky.push_back(0);
for (int i = 1; i <= 9; i++) {
for (int j = 0; j < 1 << i; j++) {
long long cur = 0;
for (int k = i - 1; k >= 0; k--) {
if ((1 << k) & j) {
cur = 10 * cur + 7;
} else {
cur = 10 * cur + 4;
}
}
if (cur >= min(pl, vl) && cur <= max(pr, vr)) {
lucky.push_back(cur);
}
}
}
lucky.push_back(1e18);
long double num = 0, den = (pr - pl + 1) * (vr - vl + 1);
for (int i = 1; i < lucky.size(); i++) {
long long x = overlap(pl, pr, lucky[i - 1] + 1, lucky[i] - 1), y = 0;
if (i + k < lucky.size()) {
y += overlap(vl, vr, lucky[i + k - 1], lucky[i + k] - 1);
}
if (i - k > 0) {
y += overlap(vl, vr, lucky[i - k - 1] + 1, lucky[i - k]);
}
num += x * y;
}
for (int i = 0; i < lucky.size(); i++) {
if (pl <= lucky[i] && lucky[i] <= pr) {
if (i + k < lucky.size()) {
num += overlap(vl, vr, lucky[i + k - 1], lucky[i + k] - 1);
}
if (i - k >= 0) {
num += overlap(vl, vr, lucky[i - k] + 1, lucky[i - k + 1]);
}
if (k == 1) {
num--;
}
}
}
cout << fixed << setprecision(12) << num / den << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int a[26] = {}, c = 0;
for (auto i : s) {
if (a[i - 'a'] == 2) continue;
c++;
a[i - 'a']++;
}
cout << c / 2 << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
T &ctmax(T &x, const U &y) {
return x = max<T>(x, y);
}
template <class T, class U>
T &ctmin(T &x, const U &y) {
return x = min<T>(x, y);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
int n;
cin >> n;
vector<int> a(n << 1);
for (auto i = 0; i < n << 1; ++i) {
cin >> a[i];
}
sort(a.begin(), a.end());
long long res = 1LL * (a[n - 1] - a[0]) * (a[n * 2 - 1] - a[n]);
for (auto i = 1; i < n; ++i) {
ctmin(res, 1LL * (a.back() - a.front()) * (a[i + n - 1] - a[i]));
}
cout << res << "\n";
return 0;
}
| 1 |
#include "bits/stdc++.h"
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MAX_N = 200005;
const int INF = 1001001001;
vector<int> a, ans;
vector<int> to[MAX_N];
int dp[MAX_N];
void dfs(int v, int p = -1) {
int i = lower_bound(dp, dp + MAX_N, a[v]) - dp;
int old = dp[i];
dp[i] = a[v];
//ans[v] = i;
//if (p != -1) ans[v] = max(ans[v], ans[p]);
ans[v] = lower_bound(dp, dp + MAX_N, INF) - dp -1;
for (int u : to[v]) {
if (u == p) continue;
dfs(u, v);
}
dp[i] = old;
}
int main() {
int n;
cin >> n;
a = ans = vector<int>(n);
rep(i, n) cin >> a[i];
rep(i, n - 1) {
int u, v;
cin >> u >> v;
--u; --v;
to[v].push_back(u);
to[u].push_back(v);
}
rep(i, MAX_N) dp[i] = INF;
dp[0] = -INF;
dfs(0);
rep(i, n) cout << ans[i] << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long int n, k;
cin >> n >> k;
string s;
cin >> s;
bool pp = 0;
long int temp1 = 0, temp2 = 0;
long int kk = 0, second = 0;
for (long int i = 0; i < k; i++) {
temp1 = 0, temp2 = 0;
for (long int j = i; j < n; j += k) {
if (s[j] == '1')
temp1++;
else if (s[j] == '0')
temp2++;
}
if (temp1 > 0 && temp2 > 0) pp = 1;
if (temp1) kk++;
if (temp2) second++;
}
if (pp || kk > k / 2 || second > k / 2)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n;
std::cin >> n;
std::vector<int> a(n);
std::vector<int> mn(n);
for (int i = 0; i < n; ++i) {
std::cin >> a[i];
}
mn[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; --i) {
if (a[i] < mn[i + 1]) {
mn[i] = a[i];
} else {
mn[i] = mn[i + 1];
}
}
for (int i = 0; i < n; ++i) {
int left = i;
int right = n - 1;
int val = a[i];
while (left != right) {
int mid = (left + right + 1) / 2;
if (mn[mid] < val) {
left = mid;
} else {
right = mid - 1;
}
}
std::cout << right - i - 1 << ' ';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int EPS = 1e-9;
long long f[28];
int main() {
ios::sync_with_stdio(0);
long long n, k;
cin >> n >> k;
string s;
cin >> s;
long long ans = 0;
for (char c : s) {
f[c - 'A']++;
}
sort(f, f + 28, greater<long long>());
for (long long x : f) {
if (!x || !k) break;
long long d = min(k, x);
ans += d * d;
k -= d;
}
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, k, x, y;
long long mx, mn = 1e18, srj;
long long a[500][500];
int main() {
cin >> n >> m >> k >> x >> y;
long long ttl = n * m;
if (n > 2) {
ttl += m * (n - 2);
}
long long left = k % ttl;
long long amt = k / ttl;
int i = 1, j = 1, add = 1;
while (left) {
a[i][j]++;
if (j == m) {
if (i == n) {
add = -1;
}
if (n > 1) i += add;
j = 1;
} else {
j += 1;
}
left--;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i == 1 || i == n) {
mx = max(mx, a[i][j] + amt);
mn = min(mn, a[i][j] + amt);
if (i == x && j == y) {
srj = a[i][j] + amt;
}
} else {
mx = max(mx, a[i][j] + amt * 2);
mn = min(mn, a[i][j] + amt * 2);
if (i == x && j == y) {
srj = a[i][j] + amt * 2;
}
}
}
}
cout << mx << " " << mn << " " << srj;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, c1, r2, c2, rowdif, coldif;
cin >> r1 >> c1 >> r2 >> c2;
rowdif = abs(r1 - r2);
coldif = abs(c1 - c2);
if (r1 == r2 || c1 == c2) {
cout << "1 ";
} else {
cout << "2 ";
}
if ((rowdif + coldif) % 2 == 0) {
if (rowdif == coldif) {
cout << "1 ";
} else {
cout << "2 ";
}
} else {
cout << "0 ";
}
if (rowdif >= coldif) {
cout << rowdif << endl;
} else {
cout << coldif << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int dir[4] = {1, 55, -1, -55}, cnt;
int maap[55 * 55];
char f;
int go(int p, int last) {
int i;
for (i = 0; i < 4; i++) {
if (i != last) {
if (maap[dir[i] + p] == cnt)
return 1;
else if (maap[dir[i] + p] == f) {
maap[dir[i] + p] = cnt;
if (go(dir[i] + p, (i + 2) % 4)) return 1;
}
}
}
return 0;
}
int main() {
int n, m, i, j;
while (cin >> n >> m) {
cnt = -1;
memset(maap, 0, sizeof(maap));
for (i = 55; i <= n * 55; i += 55) {
for (j = 1; j <= m; j++) {
cin >> f;
maap[i + j] = (int)f;
}
}
for (i = 55; i <= n * 55; i += 55) {
for (j = 1; j <= m; j++) {
if (maap[i + j] > 0) {
f = maap[i + j];
maap[i + j] = cnt;
if (go(i + j, 100)) break;
cnt--;
}
}
if (j <= m) break;
}
if (i <= n * 55)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
map<pair<int, int>, int> mp;
int prefix_sum1 = 0, prefix_sum2 = 0;
int min_length = INT_MAX;
int st = -1;
for (int i = 0; i < n; i++) {
if (s[i] == 'L') {
prefix_sum1--;
} else if (s[i] == 'R') {
prefix_sum1++;
} else if (s[i] == 'U') {
prefix_sum2++;
} else {
prefix_sum2--;
}
if (prefix_sum2 == 0 && prefix_sum1 == 0) {
if (min_length > i + 1) {
min_length = i + 1;
st = 0;
}
}
pair<int, int> p = make_pair(prefix_sum1, prefix_sum2);
auto it = mp.find(p);
if (it != mp.end()) {
if (min_length > i - it->second) {
min_length = i - it->second;
st = it->second + 1;
}
it->second = i;
} else {
mp[p] = i;
}
}
if (st == -1) {
cout << -1 << endl;
} else {
cout << st + 1 << ' ' << min_length + st << endl;
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int caseno = 1;
typedef long long LL;
typedef unsigned long long ULL;
template <class T>
inline void SWAP(T &a, T &b) {
T t = a;
a = b;
b = t;
}
inline LL POW(LL base, LL power) {
LL res = base;
if (power == 0) return 1;
for (int I = 0; I < power - 1; I++) res *= base;
return res;
}
int ToInt(string s) {
int r = 0;
istringstream sin(s);
sin >> r;
return r;
}
double ToDouble(string s) {
double r = 0;
istringstream sin(s);
sin >> r;
return r;
}
string ToString(int n) {
string s;
stringstream convert;
convert << n;
s = convert.str();
return s;
}
const int SIZE = 20 + 10;
vector<string> G[SIZE], ans[SIZE];
string S, X, Y, name, phone;
map<string, int> MAP1;
map<int, string> MAP2;
int main() {
std::ios_base::sync_with_stdio(0);
int T, I, J, K, N, n, m, k, cnt = 0, len, id = 0, idx, SZ, lenX, lenY, flag;
cin >> n;
for (I = 1; I <= n; I++) {
cin >> name;
if (MAP1.find(name) == MAP1.end()) {
MAP1[name] = ++id;
MAP2[id] = name;
idx = id;
} else
idx = MAP1[name];
cin >> m;
for (J = 1; J <= m; J++) {
cin >> phone;
SZ = G[idx].size();
for (K = 0; K < SZ; K++) {
if (G[idx][K] == phone) break;
}
if (K == SZ) G[idx].push_back(phone);
}
}
for (I = 1; I <= MAP1.size(); I++) {
SZ = G[I].size();
for (J = 0; J < SZ; J++) {
X = G[I][J];
flag = 0;
for (K = 0; K < SZ; K++) {
if (J == K) continue;
Y = G[I][K];
lenX = X.length();
lenY = Y.length();
if (lenX > lenY) continue;
while (lenX >= 0 && lenY >= 0) {
if (X[lenX] != Y[lenY]) break;
lenX--;
lenY--;
}
if (lenX < 0) flag = 1;
}
if (!flag) ans[I].push_back(X);
}
}
cout << MAP1.size() << '\n';
for (I = 1; I <= MAP1.size(); I++) {
SZ = ans[I].size();
cout << MAP2[I] << ' ' << SZ;
for (J = 0; J < SZ; J++) cout << ' ' << ans[I][J];
cout << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int memo[5000000];
bool occ[5000000];
int N, M, c, a;
queue<int> Q;
vector<int> V;
void resolve(int x) {
if (memo[x] != -1) return;
memo[x] = c;
if (occ[x]) Q.push(x);
for (int i = 0; i < N; ++i)
if ((1 << i) & x) {
resolve(x ^ (1 << i));
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> N >> M;
for (int i = 0; i < M; ++i) {
cin >> a;
occ[a] = 1;
V.emplace_back(a);
}
memset(memo, -1, sizeof(memo));
for (auto i : V) {
if (memo[i] != -1) continue;
int opp = (1 << N) - 1 - i;
Q.push(opp);
while ((int)Q.size()) {
int x = Q.front();
Q.pop();
resolve((1 << N) - 1 - x);
}
++c;
}
cout << c;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int maind = -1, cur;
string problem, answer;
bool ok = true, notneed = false;
bool parse(string &t, bool on) {
int lol, i = 0;
for (; i < t.length(); i++) {
if (t[i] != ' ') {
if (t[i] == 't') {
if (t[i + 1] == 'r') {
lol = 1;
break;
} else {
lol = 2;
break;
}
} else {
lol = 3;
break;
}
}
}
cur = lol;
if (on) return false;
switch (lol) {
case 1:
if (maind != -1) {
notneed = true;
}
return false;
case 2:
while (t[i] != '(') i++;
for (; i < t.length(); i++) {
if (t[i] != '(' && t[i] != ' ') {
for (int j = 1; j + i < t.length(); j++) {
if (t[j + i] == ')' || t[j + i] == ' ') {
problem = t.substr(i, j);
maind = 1;
return false;
}
}
}
}
break;
case 3:
while (t[i] != '(') i++;
if (maind == -1) {
return false;
}
for (; i < t.length(); i++) {
if (t[i] != '(' && t[i] != ' ') {
for (int j = 1; j + i < t.length(); j++) {
if (t[j + i] == ')' || t[j + i] == ' ' || t[j + i] == ',') {
string w = t.substr(i, j);
if (w == problem) {
i = i + j + 1;
for (; i < t.length(); i++) {
if (t[i] == '"') {
i++;
for (int j = 1; j + i < t.length(); j++) {
if (t[j + i] == '"') {
answer = t.substr(i, j);
return true;
}
}
}
}
return true;
}
}
}
}
}
break;
}
return false;
}
int main() {
int a, b, n, m, i = 0;
string t;
ios::sync_with_stdio(0);
cin >> a;
getline(cin, t);
for (; i < a; i++) {
getline(cin, t);
if (t == "") continue;
if (parse(t, false)) {
cout << answer;
return 0;
} else if (notneed) {
int pp = 1;
for (; i < a && pp > 0; i++) {
getline(cin, t);
if (t == "") continue;
parse(t, true);
if (cur == 1)
pp++;
else if (cur == 3)
pp--;
}
notneed = false;
}
}
cout << "Unhandled Exception";
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
vector<int>G[100000];
queue<int>Q;
int col[100000],n,id=1;
void bfs(int u,int c){
Q.push(u);
col[u]=c;
while(!Q.empty()){
u=Q.front();Q.pop();
r(i,0,G[u].size()){
int w=G[u][i];
if(col[w]==-1){
Q.push(w);
col[w]=c;
}
}
}
}
void Color(){
r(i,0,100000)col[i]=-1;
r(i,0,n)if(col[i]==-1)bfs(i,id++);
}
int main(){
int s,t,m,q;
cin>>n>>m;
r(i,0,m){
cin>>s>>t;
G[s].push_back(t);
G[t].push_back(s);
}
Color();
cin>>q;
while(q--){
cin>>s>>t;
if(col[s]==col[t])cout<<"yes";
else cout<<"no";
cout<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[2000][2000];
vector<vector<int> > G;
vector<vector<pair<int, int> > > ans;
int in[2000], out[2000];
int num;
void dfs(int v, int s, int u) {
int i, j, k, l;
int n;
if (out[v] == 0) {
if (u == v) return;
num++;
ans[u].push_back({v, s});
return;
}
for (i = 0; i < G[v].size(); i++) {
k = G[v][i];
s = min(s, a[v][k]);
dfs(k, s, u);
}
return;
}
int main() {
int i, j, k, l;
int n, m, d;
cin >> n >> m;
G.resize(n * 2 + 10);
ans.resize(n * 2 + 10);
for (i = 0; i < m; i++) {
cin >> k >> l >> d;
G[k].push_back(l);
in[l]++;
out[k]++;
a[k][l] = d;
}
for (i = 1; i <= n; i++) {
if (in[i] == 0) dfs(i, 1e7, i);
}
cout << num;
cout << '\n';
for (i = 1; i <= n; i++) {
for (j = 0; j < ans[i].size(); j++) {
k = ans[i][j].first;
l = ans[i][j].second;
cout << i << ' ' << k << ' ' << l;
cout << '\n';
}
}
}
| 3 |
#include <bits/stdc++.h>
int main() {
char str[105];
int k, i, j, len, pos[30], loc[105], cnt;
while (scanf("%d", &k) != EOF) {
scanf("%s", str);
len = strlen(str);
memset(pos, 0, sizeof(pos));
for (i = 0, cnt = 0, j = len - 1; i <= j; i++, j--) {
if (str[i] != '?' && str[j] != '?') {
if (str[i] != str[j])
break;
else if (str[i] - 'a' + 1 > k)
break;
else {
pos[str[i] - 'a' + 1] = 1;
}
} else {
if (str[i] == '?' && str[j] != '?') {
str[i] = str[j];
pos[str[i] - 'a' + 1] = 1;
if (str[j] - 'a' + 1 > k) break;
} else if (str[j] == '?' && str[i] != '?') {
str[j] = str[i];
pos[str[i] - 'a' + 1] = 1;
if (str[i] - 'a' + 1 > k) break;
} else {
loc[cnt++] = i;
}
}
}
if (i < j)
printf("IMPOSSIBLE\n");
else {
for (i = cnt - 1; i >= 0; i--) {
for (j = k; j >= 1; j--) {
if (pos[j] == 0) {
str[loc[i]] = str[len - 1 - loc[i]] = 'a' + j - 1;
pos[j] = 1;
break;
}
}
if (j == 0) str[loc[i]] = str[len - 1 - loc[i]] = 'a';
}
for (j = 1; j <= k; j++) {
if (pos[j] == 0) break;
}
if (j <= k)
printf("IMPOSSIBLE\n");
else
printf("%s\n", str);
}
}
}
| 3 |
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll powmod(ll a,ll b) {ll res=1; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
int n,d;
void solve() {
int n; string a,b;
cin >> n >> a >> b;
if(a==b) { cout << b << '\n'; return ; }
if(a[0] == '0') { F0R(i,n) cout << "1"; cout<<'\n'; return; }
if(b.back() == '1' || a == b) { cout << b << '\n'; return ; }
{
int l = n-1;
while(a[l] == '1') --l;
a[l]='1'; FOR(i,l+1,n) a[i]='0';
}
if(a != b) b[n-1] = '1';
{ cout << b << '\n'; return ; }
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1,Tc=1;
while(t--){
//cout << "Case #"<<Tc<<": "; Tc++;
solve();
}
return 0;
}
| 5 |
#include <iostream>
using namespace std;
int main(){
int N, A, B, C, X;
int Y[100];
while(cin >> N >> A >> B >> C >> X, N){
for(int i=0;i<N;i++) cin >> Y[i];
int idx = 0;
int res = -1;
for(int turn=0;turn<=10000;turn++){
if(X == Y[idx]) ++idx;
if(idx == N){
res = turn;
break;
}
X = (A*X+B)%C;
}
cout << res << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int kMod = 1000000007;
map<string, int> mp;
string str[64];
const char CONV[4] = {'B', 'W', 'R', 'Y'};
bool valid(string s) {
return s[0] != s[1] && s[1] != s[2] && s.substr(0, 2) != "WY" &&
s.substr(0, 2) != "RB" && s.substr(1, 2) != "WY" &&
s.substr(1, 2) != "RB" && s.substr(0, 2) != "YW" &&
s.substr(0, 2) != "BR" && s.substr(1, 2) != "YW" &&
s.substr(1, 2) != "BR" && s != "BWR" && s != "RWB";
}
void add(int &a, int b) {
a += b;
if (a > kMod) a %= kMod;
}
void substract(int &a, int b) {
a -= b;
a %= kMod;
if (a < 0) a += kMod;
}
int mul(int a, int b) { return (long long)a * b % kMod; }
int powMod(int a, int n) {
int res = n & 1 ? a : 1;
for (n >>= 1; n; n >>= 1) {
a = mul(a, a);
if (n & 1) res = mul(res, a);
}
return res;
}
struct Matrix {
int ele[65][65];
int n, m;
} IdMat, F, T;
Matrix operator*(const Matrix &A, const Matrix &B) {
static Matrix C;
assert(A.m == B.n);
C.n = A.n, C.m = B.m;
for (int i = 0; i < C.n; i++)
for (int j = 0; j < C.m; j++) {
C.ele[i][j] = 0;
for (int k = 0; k < A.m; k++)
add(C.ele[i][j], mul(A.ele[i][k], B.ele[k][j]));
}
return C;
}
Matrix powMat(Matrix A, int n) {
Matrix res = n & 1 ? A : IdMat;
for (n >>= 1; n; n >>= 1) {
A = A * A;
if (n & 1) res = res * A;
}
return res;
}
int eval(int n) {
if (n == 1)
return 0;
else if (n == 2)
return 8;
Matrix R = powMat(T, n - 3);
R = R * F;
add(R.ele[64][0], 8);
return R.ele[64][0];
}
int tot = 0;
void test(string cur, int tar) {
if (tar == 0) {
tot++;
return;
}
for (int t = 0; t < 4; t++) {
string ns = cur.substr(1, 2);
ns += CONV[t];
if (valid(ns)) test(ns, tar - 1);
}
}
int solve(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 4;
else if (n == 2)
return 8;
int res = 0;
add(res, eval(n));
substract(res, 8);
add(res, eval((n + 1) / 2));
res = mul(res, powMod(2, kMod - 2));
add(res, 8);
return res;
}
void init() {
IdMat.n = IdMat.m = 65;
for (int i = 0; i < 65; i++) IdMat.ele[i][i] = 1;
for (int s = 0; s < 64; s++) {
string ts;
ts += CONV[(s >> 4) & 3];
ts += CONV[(s >> 2) & 3];
ts += CONV[(s >> 0) & 3];
mp[ts] = s;
str[s] = ts;
}
T.n = T.m = 65;
memset(T.ele, 0, sizeof(T.ele));
for (int s = 0; s < 64; s++) {
if (valid(str[s])) {
for (int t = 0; t < 4; t++) {
string ns = str[s].substr(1, 2) + CONV[t];
if (valid(ns)) T.ele[mp[ns]][s]++;
}
}
}
for (int i = 0; i < 64; i++)
for (int j = 0; j < 64; j++) T.ele[64][j] += T.ele[i][j];
T.ele[64][64] = 1;
F.n = 65, F.m = 1;
memset(F.ele, 0, sizeof(F.ele));
for (int s = 0; s < 64; s++)
if (valid(str[s])) {
F.ele[s][0] = 1;
F.ele[64][0]++;
}
}
int main() {
init();
int L, R;
scanf("%d%d", &L, &R);
int ans = solve(R) - solve(L - 1);
if (ans < 0) ans += kMod;
printf("%d\n", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long double pi = acos(-1);
vector<pair<long double, int> > angel;
int main() {
int n;
scanf("%d", &n);
long double ans = 3 * pi;
int li, ri;
angel.clear();
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
long double ang = atan2(y, x);
angel.push_back(make_pair(ang, i));
}
sort(angel.begin(), angel.end());
for (int i = 0; i < angel.size() - 1; i++) {
long double delta = fabs(angel[i].first - angel[i + 1].first);
if (delta > pi) delta = 2 * pi - delta;
if (ans > delta) {
ans = delta;
li = angel[i].second;
ri = angel[i + 1].second;
}
}
long double delta = fabs(angel[n - 1].first - angel[0].first);
if (delta > pi) delta = 2 * pi - delta;
if (ans > delta) {
ans = delta;
li = angel[n - 1].second;
ri = angel[0].second;
}
cout << li << ' ' << ri << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int res = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -f;
ch = getchar();
}
while (isdigit(ch)) {
res = res * 10 + ch - '0';
ch = getchar();
}
return res * f;
}
namespace qiqi {
const int N = 4005, P = 1e9 + 7;
int p, alpha, f[N][N][2][2], a[N], len;
char A[N];
void main() {
p = read();
alpha = read();
scanf("%s", A + 1);
int x = strlen(A + 1);
reverse(A + 1, A + x + 1);
for (int i = (1); i <= (x); ++i) A[i] -= '0';
while (x) {
long long k = 0;
for (int i = (x); i >= (1); --i) {
k = k * 10 + A[i];
A[i] = k / p;
k %= p;
if (!A[i] && i == x) --x;
}
a[++len] = k;
}
f[len + 1][0][0][1] = 1;
for (int i = (len); i >= (1); --i) {
int c0 = 1LL * p * (p + 1) / 2 % P, c1 = 1LL * p * (p - 1) / 2 % P;
for (int j = (0); j <= (len - i + 1); ++j) {
int f0 = f[i + 1][j][0][0], f1 = f[i + 1][j][0][1],
f2 = f[i + 1][j][1][0], f3 = f[i + 1][j][1][1];
f[i][j][0][0] =
(1LL * c0 * f0 % P + 1LL * a[i] * (a[i] + 1) / 2 % P * f1 % P +
1LL * c1 * f2 + 1LL * a[i] * ((p << 1) - a[i] - 1) / 2 % P * f3) %
P;
f[i][j + 1][1][0] =
(1LL * c1 * f0 % P + 1LL * a[i] * (a[i] - 1) / 2 % P * f1 % P +
1LL * c0 * f2 % P +
1LL * a[i] * ((p << 1) - a[i] + 1) / 2 % P * f3 % P) %
P;
f[i][j][0][1] =
(1LL * (a[i] + 1) * f1 % P + 1LL * (p - a[i] - 1) * f3 % P) % P;
f[i][j + 1][1][1] = (1LL * a[i] * f1 % P + 1LL * (p - a[i]) * f3 % P) % P;
}
}
x = 0;
for (int i = (alpha); i <= (len); ++i)
x = (x + (f[1][i][0][0] + f[1][i][0][1]) % P) % P;
printf("%d\n", (x + P) % P);
}
} // namespace qiqi
int main() {
qiqi::main();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int SMAX = 98, PMAX = 25, CMAX = 2944729, INF = 987654321;
int prime[PMAX], power[PMAX], cand[CMAX], d[CMAX];
bool sieve[SMAX], check[CMAX];
int main() {
int i, j, k, t, ccnt, l, r, p, pcnt, ans;
scanf("%d%d%d", &l, &r, &p);
for (i = 2; i <= p; i++) sieve[i] = 1;
pcnt = 0;
for (i = 2; i <= p; i++)
if (sieve[i]) {
prime[pcnt++] = i;
for (j = i + i; j <= p; j += i) sieve[j] = 0;
}
t = 1;
ccnt = 0;
while (1) {
for (i = 0; i < pcnt; i++) {
if (t <= r / prime[i]) {
power[i]++;
t *= prime[i];
break;
}
while (power[i]--) t /= prime[i];
power[i]++;
}
if (i == pcnt) break;
cand[ccnt++] = t;
}
sort(cand, cand + ccnt);
ans = 0;
fill(d, d + ccnt, INF);
for (i = 2; i <= p; i++) {
for (j = 0; cand[j] <= i; j++) d[j] = 1;
k = 0;
for (j = 0; j < ccnt; j++) {
for (; k < ccnt && cand[k] != cand[j] * i; k++)
;
if (k == ccnt) break;
if (d[k] > d[j] + 1) d[k] = d[j] + 1;
}
for (j = 0; j < ccnt && cand[j] < l; j++)
;
for (; j < ccnt; j++)
if (!check[j] && d[j] + i <= p) {
check[j] = 1;
ans++;
}
}
printf("%d", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
long long total = K * pow(K-1, N-1);
cout << total << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
int n, a, b;
cin >> n >> a >> b;
if (a * b < n) {
cout << -1;
return 0;
}
int s1 = 1, s2 = 2;
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
if ((i + j) % 2 == 0) {
if (s1 > n)
cout << 0 << " ";
else
cout << s1 << " ";
s1 += 2;
} else {
if (s2 > n)
cout << 0 << " ";
else
cout << s2 << " ";
s2 += 2;
}
}
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long ans;
int m, x[100010], y[100010], cannot[100010];
map<pair<int, int>, int> pos;
set<int> couldbe;
void update(int x, int y, int add) {
if (y == 0) return;
vector<int> below;
for (int dx = -1; dx <= 1; dx++)
if (pos.count({x + dx, y - 1})) below.push_back(pos[{x + dx, y - 1}]);
if (below.size() == 1) {
cannot[below[0]] += add;
if (add == -1 && cannot[below[0]] == 0) couldbe.insert(below[0]);
if (add == 1 && cannot[below[0]] == 1) couldbe.erase(below[0]);
}
}
int main() {
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d %d", &x[i], &y[i]);
pos[{x[i], y[i]}] = i;
}
for (int i = 0; i < m; i++) update(x[i], y[i], 1);
for (int i = 0; i < m; i++)
if (!cannot[i]) couldbe.insert(i);
int t = 0, val;
for (int z = 0; z < m; z++) {
if (t == 0)
val = *couldbe.rbegin();
else
val = *couldbe.begin();
couldbe.erase(val);
ans = (ans * m + val) % 1000000009;
pos.erase({x[val], y[val]});
update(x[val], y[val], -1);
for (int dx = -1; dx <= 1; dx++)
if (pos.count({x[val] + dx, y[val] + 1}))
update(x[val] + dx, y[val] + 1, 1);
t = 1 - t;
}
printf("%lld\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> operator+(pair<long long, long long> a,
pair<long long, long long> b) {
return make_pair(a.first + b.first, a.second + b.second);
}
long long m;
map<long long, pair<long long, long long> > mp;
pair<long long, long long> dfs(long long x) {
if (mp.find(x) != mp.end()) return mp[x];
if (x == 0) return make_pair(0, 0);
long long l = 1, r = 1e5;
while (l < r) {
long long mid = (l + r + 1) / 2;
if (mid * mid * mid <= x)
l = mid;
else
r = mid - 1;
}
long long k = l * l * l;
return mp[x] = max(dfs(x % k) + make_pair(x / k, x / k * k),
dfs(k - 1) + make_pair(x / k - 1, (x / k - 1) * k));
}
int main() {
scanf("%I64d", &m);
pair<long long, long long> ans = dfs(m);
printf("%I64d %I64d\n", ans.first, ans.second);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long f(long long x) {
vector<int> d;
for (; x > 0; x /= 10) d.push_back(x % 10);
long long res = 0;
reverse(d.begin(), d.end());
for (int i = 0; i < (int((d).size())); ++i) res = res * 10 + 9 - d[i];
return res;
}
long long five(long long x) {
long long res = 1;
for (; x >= 10; x /= 10) res *= 10;
return res * 5;
}
long long solve(long long l, long long r) {
long long x = five(r);
if (l <= x && x <= r) return x * f(x);
if (r < x) return r * f(r);
return f(l) * l;
}
int main() {
long long l, r;
cin >> l >> r;
cout << solve(l, r) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int facs[1000001];
long long int inv_modulo(long long int a, long long int b) {
long long int b0 = b, t, q;
long long int x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
void factorial() {
long long int n = 1000000;
facs[1] = 1;
facs[0] = 0;
long long int fac = 1;
for (long long int i = 2; i <= n; i++) {
fac = (fac * i) % 1000003;
facs[i] = fac;
}
}
string preProcess(string s) {
int n = s.length();
if (n == 0) return "^$";
string ret = "^";
for (int i = 0; i < n; i++) ret += "#" + s.substr(i, 1);
ret += "#$";
return ret;
}
string longestPalindrome(string s) {
string T = preProcess(s);
int n = T.length();
int* P = new int[n];
int C = 0, R = 0;
for (int i = 1; i < n - 1; i++) {
int i_mirror = 2 * C - i;
if (R > i) {
P[i] = min(R - i, P[i_mirror]);
} else {
P[i] = 0;
}
while (T[i + 1 + P[i]] == T[i - (1 + P[i])]) {
P[i]++;
}
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
int maxLen = 0;
int centerIndex = 0;
for (int i = 1; i < n - 1; i++) {
if (P[i] > maxLen) {
maxLen = P[i];
centerIndex = i;
}
}
int start = (int)(centerIndex - 1 - maxLen) / 2.00;
int fin = maxLen;
return s.substr(start, fin);
}
long long int bigmod(long long int a, long long int b, long long int M) {
if (b == 0) return 1;
long long int x = bigmod(a, b / 2, M);
x = (x * x) % M;
if ((b & 1) == 1) x = (x * a) % M;
return x;
}
template <typename t1>
t1 gcd(t1 a, t1 b) {
return b == 0 ? a : gcd(b, a % b);
}
template <typename t1>
t1 lcm(t1 a, t1 b) {
return a * (b / gcd(a, b));
}
bool check(int i, int k) { return i & (1 << k); }
int On(int i, int k) { return i | (1 << k); }
int Off(int i, int k) { return (i - ((check(i, k)) << k)); }
int gray_to_int(int n) {
int lm = -1;
int binary = 0;
for (int i = 31; i >= 0; i--) {
if (check(n, i) == 1) {
lm = i;
break;
}
}
if (lm == -1) return 0;
binary = On(binary, lm);
for (int i = lm - 1; i >= 0; i--) {
bool ck = check(n, i);
bool prev = check(binary, i + 1);
if (ck == 0 && prev == 0) {
binary = Off(binary, i);
} else if (ck == 1 && prev == 1) {
binary = Off(binary, i);
} else if (ck == 0 && prev == 1) {
binary = On(binary, i);
} else if (ck == 1 && prev == 0) {
binary = On(binary, i);
}
}
return binary;
}
struct skill {
long long int pt, need;
bool operator<(const skill& oth) const {
if (need < oth.need)
return true;
else if (need == oth.need) {
return pt < oth.pt;
} else {
return false;
}
}
};
vector<skill> ar;
int main() {
long long int n, k;
scanf("%I64d", &n);
scanf("%I64d", &k);
long long int temp;
skill g;
for (int i = 0; i < n; i++) {
scanf("%I64d", &temp);
if (temp >= 90 && temp <= 99) {
g.need = 100 - temp;
} else if (temp >= 100) {
g.need = 0;
} else {
g.need = 10 - (temp % 10);
}
g.pt = temp;
ar.push_back(g);
}
long long int stock = 0;
sort(ar.begin(), ar.end());
for (int i = 0; i < n; i++) {
if (k < ar[i].need) {
break;
} else if (ar[i].pt + ar[i].need < 101) {
k -= ar[i].need;
ar[i].pt += ar[i].need;
ar[i].need = 10;
stock += 100 - ar[i].pt;
}
}
long long int sum = 0;
for (int i = 0; i < n; i++) {
sum += (ar[i].pt / 10);
}
sum += (min(k, stock) / 10);
printf("%I64d\n", sum);
return 0;
}
| 3 |
#include<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
#include<cassert>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
//問題文および制約はちゃんと確認しよう!
//サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
//とりあえずサンプルを読んでから解法を考えよう?
#define N 15
lint dp[200100][N+5],d2[200100][N+5];
lint a[200100];
lint inf=12345678901234LL;
/*int cal(lint x,lint y){
int ret=0;
if(y<x){
while(y<x) y*=4,ret++;
}
else{
while(x*4<=y) x*=4,ret--;
}
return ret;
}*/
int main()
{
int n;cin>>n;rep(i,n) cin>>a[i];
//rep(i,n+5) rep(j,N+2) dp[i][j]=d2[i][j]=inf;
rep(i,N+1) dp[0][i]=i;
rep(i,n-1){
lint now=a[i+1],pre=a[i];int num=0;
rep(j,N+1){
while(pre<now) num++,pre*=4;
if(num>N) dp[i+1][j]=dp[i][N]+(i+1)*(num-N)+j;
else dp[i+1][j]=dp[i][num]+j;
now*=4;
}
}
rep(i,N+1) d2[n-1][i]=i;
for(int i=n-1;i>0;i--){
lint now=a[i-1],pre=a[i];int num=0;
rep(j,N+1){
while(pre<now) num++,pre*=4;
if(num>N) d2[i-1][j]=d2[i][N]+(n-i)*(num-N)+j;
else d2[i-1][j]=d2[i][num]+j;
now*=4;
}
}
//rep(i,n) rep(j,3) cout<<i<<' '<<j<<' '<<dp[i][j]<<' '<<d2[i][j]<<endl;
lint out=min(d2[0][0],dp[n-1][0]+n-1)*2;
rep(i,n-1){
out=min(out,dp[i][0]*2+i+1+d2[i+1][0]*2);
}
cout<<out<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
struct p {
int x, id;
bool operator<(const p &a) const { return x < a.x; }
} a[MAX];
int n, ans, K;
int fa[MAX], siz[MAX], tot[MAX], b[MAX];
set<int> S;
int find(int x) {
if (fa[x] != x) fa[x] = find(fa[x]);
return fa[x];
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i].x), b[i] = a[i].x, a[i].id = i, fa[i] = i, siz[i] = 1;
sort(a + 1, a + 1 + n);
b[0] = b[n + 1] = 2e9;
for (int i = 1; i <= n; ++i) {
int id = a[i].id, k = a[i].x + 1;
S.insert(1), ++tot[1];
if (b[id - 1] < k) {
int r1 = find(id - 1);
--tot[1], --tot[siz[r1]];
if (!tot[1]) S.erase(1);
if (!tot[siz[r1]]) S.erase(siz[r1]);
fa[id] = r1, ++tot[++siz[r1]];
if (tot[siz[r1]] == 1) S.insert(siz[r1]);
}
if (b[id + 1] < k) {
int r1 = find(id + 1), r2 = find(id);
--tot[siz[r1]], --tot[siz[r2]];
if (!tot[siz[r1]]) S.erase(siz[r1]);
if (!tot[siz[r2]]) S.erase(siz[r2]);
fa[r1] = r2, ++tot[siz[r2] += siz[r1]];
if (tot[siz[r2]] == 1) S.insert(siz[r2]);
}
if (S.size() == 1 && tot[*S.begin()] > ans) ans = tot[*S.begin()], K = k;
}
return printf("%d", K), 0;
}
| 4 |
#include <iostream>
#include <vector>
using namespace std;
const int INFTY=1<<29;
string substitute(string s,string a,string b)
{
for(int i=0;(i=s.find(a,i))!=-1;i+=b.size())
s.replace(i,a.size(),b);
return s;
}
int dfs(string from,string to,vector<string>& a,vector<string>& b)
{
if(from==to) return 0;
if(from.size()>=to.size()) return INFTY;
int res=INFTY;
for(int i=0;i<a.size();i++)
if(from.find(a[i])!=-1)
res=min(res,1+dfs(substitute(from,a[i],b[i]),to,a,b));
return res;
}
int main()
{
for(int n;cin>>n,n;){
vector<string> a(n),b(n);
for(int i=0;i<n;i++) cin>>a[i]>>b[i];
string from,to; cin>>from>>to;
int res=dfs(from,to,a,b);
cout<<(res==INFTY?-1:res)<<endl;
}
}
| 0 |
/*
* 0142.cpp
*
* Created on: 2011/07/27
* Author: isa
*/
#include <iostream>
#include <set>
#include <map>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
int n;
while(cin >> n && n){
vector<int> v;
int times[((n-1)/2)+1];
memset(times,0,sizeof(times));
for(int i=1;i<n;i++){
if(find(v.begin(),v.end(),i*i%n) == v.end()){
v.push_back(i*i%n);
}
}
for(int i=0;i<v.size();i++){
for(int j=0;j<v.size();j++){
int tmp;
if(i == j) continue;
tmp = v[i] - v[j];
if(tmp < 0) tmp+=n;
if(tmp > (n-1)/2) tmp = n - tmp;
times[tmp]++;
}
}
for(int i=1;i<=(n-1)/2;i++){
cout << times[i] << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long get(string a, string b) {
long long last = 0;
for (long long i = 0; i < b.size(); ++i) {
auto fd = find(a.begin() + last, a.end(), b[i]);
last = fd - a.begin() + 1;
if (fd == a.end()) {
return (long long)a.size() - (i) + (long long)b.size() - i;
}
}
return (long long)a.size() - b.size();
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
vector<long long> pw;
pw = {1};
while (pw.back() <= (1LL << 60LL)) {
pw.push_back(pw.back() * 2);
}
long long t;
cin >> t;
while (t--) {
string s;
cin >> s;
long long res = s.size() + 1;
for (auto i : pw) {
res = min(res, get(s, to_string(i)));
}
cout << res << '\n';
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void printArray(const T* a, int n, ostream& out = cerr);
template <class T>
void printArray(const vector<T>& arr, ostream& out = cerr);
const long long mod = 1e9 + 7;
const double PI = acos(-1);
int n, i, j, k, t;
int m;
int a[200002];
int b[200002];
int c[200002];
int pos[200002];
int ac[200002];
int bc[200002];
int cc[200002];
int mab[200002];
int hitung(int st, int pem, int ed) {
int total = ac[st];
total += (bc[pem] - bc[st]);
total += (cc[ed] - cc[pem]);
return total;
}
int hitung2(int st, int pem, int ed) {
int total = ac[st];
total += (bc[pem] - bc[st]);
total += (cc[ed] - cc[pem]);
return total;
}
int main() {
scanf("%d %d %d", &n, &m, &k);
int tot = n + m + k;
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
pos[a[i]] = 1;
}
for (int i = 0; i < m; ++i) {
scanf("%d", b + i);
pos[b[i]] = 2;
}
for (int i = 0; i < k; ++i) {
scanf("%d", c + i);
pos[c[i]] = 3;
}
for (int i = 1; i <= tot; ++i) {
ac[i] = ac[i - 1] + (pos[i] == 1);
bc[i] = bc[i - 1] + (pos[i] == 2);
cc[i] = cc[i - 1] + (pos[i] == 3);
}
mab[tot] = bc[tot];
for (int i = tot - 1; i >= 0; --i) {
mab[i] = max(mab[i + 1], bc[i] + cc[tot] - cc[i]);
}
int ans = 0;
for (int i = 0; i <= tot; ++i) {
ans = max(ans, ac[i] + mab[i] - bc[i]);
}
printf("%d\n", tot - ans);
return 0;
}
template <class T>
void printArray(const T* a, int n, ostream& out) {
for (int i = 0; i < n; ++i) {
out << a[i] << " ";
}
out << endl;
}
template <class T>
void printArray(const vector<T>& arr, ostream& out) {
for (const T& x : arr) {
out << x << " ";
}
out << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, g;
cin >> n >> g;
vector<long long> v, ans;
map<long long, long long> m;
map<long long, long long>::iterator it;
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
m[a]++;
}
for (it = m.begin(); it != m.end(); ++it) {
v.push_back(it->second);
}
long long s1 = 0;
for (int i = 0; i < v.size() - 1; i++) {
long long s = 0;
for (int j = i + 1; j < v.size(); j++) {
s += v[j];
}
s1 += s * v[i];
}
cout << s1 << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct Tree {
struct Node {
int mn, mx, add;
};
static const int INF = (int)1e9;
static Node *_t;
static int _last, _first, _x;
int n;
Node *t;
Tree(int power) : n(1 << power), t(new Node[1 << (power + 1)]) {}
~Tree() { delete[] t; }
static void _add(int i, int tl, int tr) {
if (tr <= tl || _last <= tl || tr <= _first) {
return;
}
if (_first <= tl && tr <= _last) {
_t[i].add += _x;
} else {
int m = (tl + tr) >> 1;
_add(2 * i + 0, tl, m);
_add(2 * i + 1, m, tr);
int a = _t[2 * i + 0].add, b = _t[2 * i + 1].add;
_t[i].mn = min(_t[2 * i + 0].mn + a, _t[2 * i + 1].mn + b);
_t[i].mx = max(_t[2 * i + 0].mx + a, _t[2 * i + 1].mx + b);
}
}
void add(int first, int last, int x) {
_t = t, _first = first, _last = last, _x = x;
_add(1, 0, n);
}
static int _min(int i, int tl, int tr) {
if (tr <= tl || _last <= tl || tr <= _first) {
return +INF;
}
if (_first <= tl && tr <= _last) {
return _t[i].mn + _t[i].add;
} else {
int m = (tl + tr) >> 1;
int v1 = _min(2 * i + 0, tl, m);
int v2 = _min(2 * i + 1, m, tr);
return min(v1, v2) + _t[i].add;
}
}
int get_min(int first, int last) {
_t = t, _first = first, _last = last;
return _min(1, 0, n);
}
static int _max(int i, int tl, int tr) {
if (tr <= tl || _last <= tl || tr <= _first) {
return -INF;
}
if (_first <= tl && tr <= _last) {
return _t[i].mx + _t[i].add;
} else {
int m = (tl + tr) >> 1;
int v1 = _max(2 * i + 0, tl, m);
int v2 = _max(2 * i + 1, m, tr);
return max(v1, v2) + _t[i].add;
}
}
int get_max(int first, int last) {
_t = t, _first = first, _last = last;
return _max(1, 0, n);
}
};
Tree::Node *Tree::_t;
int Tree::_last, Tree::_first, Tree::_x;
int main() {
Tree t(20);
int n;
static char s[1 << 20];
scanf("%d%s", &n, s);
long st = clock();
string typed(t.n, ' ');
int pos = 0, sum = 0;
for (char c : s) {
if (!c) {
break;
} else if (c == 'L') {
pos = max(0, pos - 1);
} else if (c == 'R') {
pos += 1;
} else {
int diff = 0;
if (typed[pos] == '(') {
diff += -1;
} else if (typed[pos] == ')') {
diff += +1;
}
if (c == '(') {
diff += +1;
} else if (c == ')') {
diff += -1;
}
typed[pos] = c;
t.add(pos, t.n, diff);
sum += diff;
}
if (t.get_min(0, t.n) < 0 || sum != 0) {
printf("-1 ");
} else {
printf("%d ", t.get_max(0, t.n));
}
}
printf("\n");
cerr << (clock() - st) / 1000.0 << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> tbl;
tbl.push_back("111111101010101111100101001111111");
tbl.push_back("100000100000000001010110001000001");
tbl.push_back("101110100110110000011010001011101");
tbl.push_back("101110101011001001111101001011101");
tbl.push_back("101110101100011000111100101011101");
tbl.push_back("100000101010101011010000101000001");
tbl.push_back("111111101010101010101010101111111");
tbl.push_back("000000001111101111100111100000000");
tbl.push_back("100010111100100001011110111111001");
tbl.push_back("110111001111111100100001000101100");
tbl.push_back("011100111010000101000111010001010");
tbl.push_back("011110000110001111110101100000011");
tbl.push_back("111111111111111000111001001011000");
tbl.push_back("111000010111010011010011010100100");
tbl.push_back("101010100010110010110101010000010");
tbl.push_back("101100000101010001111101000000000");
tbl.push_back("000010100011001101000111101011010");
tbl.push_back("101001001111101111000101010001110");
tbl.push_back("101101111111000100100001110001000");
tbl.push_back("000010011000100110000011010000010");
tbl.push_back("001101101001101110010010011011000");
tbl.push_back("011101011010001000111101010100110");
tbl.push_back("111010100110011101001101000001110");
tbl.push_back("110001010010101111000101111111000");
tbl.push_back("001000111011100001010110111110000");
tbl.push_back("000000001110010110100010100010110");
tbl.push_back("111111101000101111000110101011010");
tbl.push_back("100000100111010101111100100011011");
tbl.push_back("101110101001010000101000111111000");
tbl.push_back("101110100011010010010111111011010");
tbl.push_back("101110100100011011110110101110000");
tbl.push_back("100000100110011001111100111100000");
tbl.push_back("111111101101000101001101110010001");
int x, y;
cin >> x >> y;
cout << tbl[x][y] << endl;
}
| 2 |
#include <complex>
#include <cstdio>
using namespace std;
typedef complex<double> P;
double cross(const P& a, const P& b) {
return imag(conj(a)*b);
}
bool in_triangle(const P& p1,const P& p2,const P& p3,const P& p){
return cross(p2-p1,p-p1)>0 && cross(p3-p2,p-p2)>0 && cross(p1-p3,p-p3)>0;
}
int main(){
double x1,y1,x2,y2,x3,y3,x,y;
for(;~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x,&y);){
P p1(x1,y1),p2(x2,y2),p3(x3,y3),p(x,y);
bool in=in_triangle(p1,p2,p3,p)||in_triangle(p2,p1,p3,p);
puts(in?"YES":"NO");
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[4][4];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cin >> a[i][j];
}
}
for (int i = 0; i < 4; i++) {
if (a[i][3] == 1) {
if (a[i][0] == 1 || a[i][1] == 1 || a[i][2] == 1 ||
a[(i + 1) % 4][0] == 1 || a[(i + 2) % 4][1] == 1 ||
a[(i + 3) % 4][2] == 1) {
cout << "YES" << endl;
break;
} else {
if (i == 3) {
cout << "NO" << endl;
}
}
} else {
if (i == 3) {
cout << "NO" << endl;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const int N = 2e5 + 314;
void foo() {
int n;
cin >> n;
vector<int> v(n);
int mn = 0, mx = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
if (v[i] == 1) mn = i + 1;
if (v[i] == n) mx = i + 1;
}
cout << max(max(n - mx, n - mn), max(mx - 1, mn - 1));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
foo();
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<vector<int> > roads = vector<vector<int> >(n, vector<int>(n, 0));
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
roads[a - 1][b - 1] = 1;
roads[b - 1][a - 1] = 1;
}
vector<int> res = vector<int>(n, -1);
int city;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += roads[i][j];
if (sum == 0) {
city = i;
break;
}
}
res[city] = 1;
vector<pair<int, int> > resPairs;
int am = 0;
queue<int> qu;
qu.push(city);
while (!qu.empty()) {
int c = qu.front();
for (int i = 0; i < n; i++) {
if (roads[i][c] != 1 && res[i] == -1 && i != c) {
res[i] = c;
qu.push(i);
am++;
resPairs.push_back(make_pair(c, i));
}
}
qu.pop();
}
cout << am << endl;
for (int i = 0; i < resPairs.size(); i++) {
cout << resPairs[i].first + 1 << " " << resPairs[i].second + 1 << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1505;
char mmap[N][N];
bool vis[N][N];
pair<int, int> record[N][N];
int n, m;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {-1, 1, 0, 0};
struct Node {
int x, y;
Node(int x, int y) : x(x), y(y) {}
};
bool bfs(int x, int y) {
queue<Node> q;
q.push(Node(x, y));
while (!q.empty()) {
Node cur = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int __x = cur.x + dx[i];
int __y = cur.y + dy[i];
int _x = (__x % n + n) % n;
int _y = (__y % m + m) % m;
if (mmap[_x][_y] == '#') continue;
if (vis[_x][_y]) {
if (record[_x][_y].first != __x || record[_x][_y].second != __y)
return true;
} else {
vis[_x][_y] = true;
record[_x][_y] = make_pair(__x, __y);
q.push(Node(__x, __y));
}
}
}
return false;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", mmap[i]);
int x = -1, y = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (mmap[i][j] == 'S') {
x = i, y = j;
goto end;
}
end:
puts(bfs(x, y) ? "Yes" : "No");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
namespace io {
const int L = (1 << 20) + 1;
char buf[L], *S, *T, c;
char getchar() {
if (__builtin_expect(S == T, 0)) {
T = (S = buf) + fread(buf, 1, L, stdin);
return (S == T ? EOF : *S++);
}
return *S++;
}
int inp() {
int x = 0, f = 1;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return x * f;
}
unsigned inpu() {
unsigned x = 0;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return x;
}
long long inp_ll() {
long long x = 0;
int f = 1;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-') f = -1;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return x * f;
}
char B[25], *outs = B + 20, *outr = B + 20;
template <class T>
inline void print(register T a, register char x = 0) {
if (x) *--outs = x, x = 0;
if (!a)
*--outs = '0';
else
while (a) *--outs = (a % 10) + 48, a /= 10;
if (x) *--outs = x;
fwrite(outs, outr - outs, 1, stdout);
outs = outr;
}
}; // namespace io
using io ::inp;
using io ::inp_ll;
using io ::inpu;
using io ::print;
using i32 = int;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
long long power(long long a, long long b, long long p) {
if (!b) return 1;
long long t = power(a, b / 2, p);
t = t * t % p;
if (b & 1) t = t * a % p;
return t;
}
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <class T>
inline void freshmin(T &a, const T &b) {
if (a > b) a = b;
}
template <class T>
inline void freshmax(T &a, const T &b) {
if (a < b) a = b;
}
const int MAXN = 400010;
const int MAXM = 26;
const int MOD = 1000000007;
const f80 MI = f80(1) / MOD;
const long long INF = 10000000000000000LL;
const long long inf = 5000000000000LL;
int n;
char s[MAXN];
int tot;
struct node {
int dis;
node *pre, *next[MAXM];
} sa[MAXN], *root;
node *new_node() { return &sa[++tot]; }
int vertex[MAXN];
void build_sa() {
node *cur = root = new_node();
for (int i = n; i >= 1; --i) {
int x = s[i] - 'a';
node *p = cur;
cur = new_node();
cur->dis = p->dis + 1;
vertex[i] = cur - sa;
for (; p && !p->next[x]; p = p->pre) p->next[x] = cur;
if (!p)
cur->pre = root;
else {
node *q = p->next[x];
if (q->dis == p->dis + 1)
cur->pre = q;
else {
node *r = new_node();
*r = *q;
r->dis = p->dis + 1;
cur->pre = q->pre = r;
for (; p && p->next[x] == q; p = p->pre) p->next[x] = r;
}
}
}
}
vector<int> v[MAXN];
int size[MAXN], son[MAXN], father[MAXN], pre[MAXN], dis[MAXN];
void dfs1(int x) {
size[x] = 1;
if (v[x].empty()) return;
for (auto y : v[x]) {
dfs1(y);
size[x] += size[y];
}
son[x] = *max_element(v[x].begin(), v[x].end(),
[&](int a, int b) { return size[a] < size[b]; });
}
void dfs2(int x) {
if (!father[x]) father[x] = x;
if (v[x].empty()) return;
father[son[x]] = father[x];
dfs2(son[x]);
for (auto y : v[x])
if (y != son[x]) dfs2(y);
}
void build_tree() {
for (int i = 1; i <= tot; ++i) {
pre[i] = sa[i].pre ? sa[i].pre - sa : 0;
dis[i] = sa[i].dis;
if (pre[i]) v[pre[i]].push_back(i);
}
dfs1(1);
dfs2(1);
}
struct point {
int no, len, pos;
};
vector<point> p[MAXN];
void make_vertex(int v) {
int x = vertex[v];
while (x > 1) {
int y = father[x];
p[y].push_back({v, dis[x] - dis[pre[y]], v + dis[pre[y]] + 1});
x = pre[y];
}
}
int q;
struct query {
int id, len, bound;
bool operator<(const query &A) const { return bound < A.bound; }
};
vector<query> q1[MAXN], q2[MAXN];
void make_query(int l, int len, int id) {
int x = vertex[l];
while (x > 1) {
int y = father[x];
if (dis[pre[y]] < len) {
q1[y].push_back({id, min(dis[x], len) - dis[pre[y]], l - 1});
q2[y].push_back({id, min(dis[x], len) - dis[pre[y]], l + len});
}
x = pre[y];
}
}
int w[MAXN];
long long ww[MAXN];
void update(int p) {
for (int x = p; x < MAXN; x += x & -x) {
w[x] += 1;
ww[x] += p;
}
}
void resume(int x) {
for (; x < MAXN; x += x & -x) ww[x] = w[x] = 0;
}
int getnum(int x) {
int p = 0;
for (; x; x -= x & -x) p += w[x];
return p;
}
long long getsum(int x) {
long long p = 0;
for (; x; x -= x & -x) p += ww[x];
return p;
}
long long ans[MAXN];
void solve(vector<point> &p, vector<query> &q1, vector<query> &q2) {
if (p.empty()) return;
if (!q1.empty()) {
sort(p.begin(), p.end(),
[](const point &a, const point &b) { return a.no < b.no; });
sort(q1.begin(), q1.end());
auto j = p.begin();
int all = 0;
for (auto i = q1.begin(); i != q1.end(); ++i) {
for (; j != p.end() && j->no <= i->bound; ++j) {
++all;
update(j->len);
}
int s = getnum(i->len);
long long t = getsum(i->len);
ans[i->id] -= (long long)i->len * (all - s) + t;
}
for (auto j = p.begin(); j != p.end(); ++j) resume(j->len);
}
if (!q2.empty()) {
sort(p.begin(), p.end(), [](const point &a, const point &b) {
return a.pos + a.len < b.pos + b.len;
});
sort(q2.begin(), q2.end());
auto j = p.begin();
int all = 0;
for (auto i = q2.begin(); i != q2.end(); ++i) {
for (; j != p.end() && j->pos + j->len <= i->bound + 1; ++j) {
++all;
update(j->len);
}
int s = getnum(i->len);
long long t = getsum(i->len);
ans[i->id] += (long long)i->len * (all - s) + t;
}
for (auto j = p.begin(); j != p.end(); ++j) resume(j->len);
reverse(p.begin(), p.end());
reverse(q2.begin(), q2.end());
j = p.begin();
for (auto i = q2.begin(); i != q2.end(); ++i) {
for (; j != p.end() && j->pos + j->len > i->bound + 1; ++j)
update(j->pos);
int s = getnum(i->bound);
long long t = getsum(i->bound);
if (i->bound - i->len + 1 <= 0)
ans[i->id] += (long long)(i->bound + 1) * s - t;
else {
int s1 = getnum(i->bound - i->len + 1);
long long t1 = getsum(i->bound - i->len + 1);
ans[i->id] += (long long)i->len * s1 +
(long long)(i->bound + 1) * (s - s1) - (t - t1);
}
}
for (auto j = p.begin(); j != p.end(); ++j) resume(j->pos);
}
}
void solve() {
for (int c = 1; c <= tot; ++c) {
solve(p[c], q1[c], q2[c]);
}
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
build_sa();
build_tree();
for (int i = 1; i <= n; ++i) make_vertex(i);
scanf("%d", &q);
for (int i = 1; i <= q; ++i) {
int x, y;
scanf("%d%d", &x, &y);
int len = y - x + 1;
make_query(x, len, i);
}
solve();
for (int i = 1; i <= q; ++i) printf("%lld\n", ans[i]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, x, data[2][100005];
int main() {
scanf("%d%d", &n, &x);
for (int i = 0; i < n; i++) scanf("%d", &data[0][i]);
for (int i = 0; i < n; i++) scanf("%d", &data[1][i]);
sort(data[0], data[0] + n);
sort(data[1], data[1] + n);
int ans = 0;
for (int i = 0, j = n - 1; i < n; i++) {
if (data[0][i] + data[1][j] < x) continue;
j--;
ans++;
}
printf("%d %d\n", 1, ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long MD = 1000000007;
long long md = 998244353;
const long long INF = 1e18L + 5;
long long exp(long long a, long long b) {
long long r = 1ll;
while (b > 0) {
if (b & 1) {
r = r * (a % md);
r = (r + md) % md;
}
b /= 2;
a = (a % md) * (a % md);
a = (a + md) % md;
}
return (r + md) % md;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long pow_2(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a);
a = (a * a);
b >>= 1;
}
return res;
}
bool isPrime(long long a) {
for (long long i = 3; (i * i) <= a; i += 2) {
if ((a % i) == 0) return false;
}
if ((a != 2) && ((a % 2) == 0)) return false;
if (a == 1) return false;
return true;
}
string decToBinary(int n) {
string s = "";
for (int i = 31; i >= 0; i--) {
int k = n >> i;
if (k & 1)
s = s + "1";
else
s = s + "0";
}
return s;
}
int decimalToBinary(int N) {
unsigned long long int B_Number = 0;
int cnt = 0;
while (N != 0) {
int rem = N % 2;
unsigned long long int c = pow(10, cnt);
B_Number += rem * c;
N /= 2;
cnt++;
}
return B_Number;
}
string toString(unsigned long long int num) {
std::string number;
std::stringstream strstream;
strstream << num;
strstream >> number;
return number;
}
const double PI = acos(-1);
bool isPrime(int n) {
if (n == 1) return false;
if (n == 2 || n == 3) return true;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
int k;
cin >> k;
string s;
cin >> s;
int comp = 0;
int two = 0;
int three = 0;
int five = 0;
int seven = 0;
char num;
int pos = -1;
for (int i = 0; i < k; i++) {
int x = (int)s[i] - 48;
if (!isPrime(x) || s[i] == '1') {
comp++;
num = s[i];
} else if (x == 2) {
two++;
pos = i;
} else if (x == 3)
three++;
else if (x == 5)
five++;
else if (x == 7)
seven++;
}
if (comp > 0) {
cout << "1" << '\n';
cout << num << '\n';
} else {
if (s.size() == 1) {
cout << "1" << '\n';
cout << s << '\n';
} else {
if (two >= 2 || three >= 2 || five >= 2 || seven >= 2) {
cout << "2" << '\n';
if (two >= 2)
cout << "22" << '\n';
else if (three >= 2)
cout << "33" << '\n';
else if (five >= 2)
cout << "55" << '\n';
else if (seven >= 2)
cout << "77" << '\n';
} else if (two > 0 && pos > 0) {
cout << "2" << '\n';
cout << s[pos - 1] << s[pos] << '\n';
} else if (two > 0 && (five > 0 || seven > 0)) {
cout << "2" << '\n';
cout << "2";
if (five > 0)
cout << "5" << '\n';
else
cout << "7" << '\n';
} else {
if (five > 0) {
if (s[0] != '5') {
cout << "2" << '\n';
cout << s[0] << "5" << '\n';
} else if (s[0] == '5') {
if (seven > 0) {
cout << "2" << '\n';
cout << "57" << '\n';
}
}
} else {
cout << s.size() << '\n';
cout << s << '\n';
}
}
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef ostringstream OSS;
typedef istringstream ISS;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;
#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(),(x).end()
#define RANGE(x,y,maxX,maxY) (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
template < typename T > inline T fromString(const string &s) { T res; ISS iss(s); iss >> res; return res; };
template < typename T > inline string toString(const T &a) { OSS oss; oss << a; return oss.str(); };
const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[]={1,0,-1,0},DY[]={0,-1,0,1};
const double EPS = 1e-12;
double add(double a, double b) {
if (abs(a + b) < EPS * (abs(a) + abs(b)))
return 0;
return a + b;
}
bool eq(double a, double b) {
return abs(a - b) < EPS;
}
struct P {
double x, y;
P() {};
P(double x_, double y_) : x(x_), y(y_) {
}
P operator + (P p) {
return P(add(x, p.x), add(y, p.y));
}
P operator - (P p) {
return P(add(x, -p.x), add(y, -p.y));
}
P operator * (double d) {
return P(x * d, y * d);
}
P operator / (double d) {
return P(x / d, y / d);
}
bool operator < (const P &p) const {
return MP(x, y) < MP(p.x, p.y);
}
bool operator > (const P &p) const {
return MP(x, y) > MP(p.x, p.y);
}
bool eq(P p) {
return ::eq(x, p.x) && ::eq(y, p.y);
}
// ??????
double dot(P p) {
return add(x * p.x, y * p.y);
}
// ??????
double det(P p) {
return add(x * p.y, -y * p.x);
}
};
// ???????????????????????????
bool on_seg(P p1, P p2, P q) {
return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}
// ??´?????¨??´????????????
P intersection(P p1, P p2, P q1, P q2) {
return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
// ????????¨?????????????????£????????????
bool is_cross(P p1, P p2, P q1, P q2) {
P a = intersection(p1, p2, q1, q2);
return on_seg(p1, p2, a) && on_seg(q1, q2, a);
}
struct Line {
P p, q;
};
// ????????¨?????????????????\?????§????????£????????????(line)
bool is_cross(Line a, Line b) {
P p = intersection(a.p, a.q, b.p, b.q);
return on_seg(a.p, a.q, p) && on_seg(b.p, b.q, p) &&
!a.p.eq(p) && !a.q.eq(p) &&
!b.p.eq(p) && !b.q.eq(p);
}
vector<P> uniq(vector<P> &ps) {
sort(ALL(ps));
vector<P> ys;
if (!ps.size()) { return ys; }
P p = ps[0];
ys.PB(p);
for (int i = 1; i < (int)ps.size(); i++) {
if (!p.eq(ps[i])) {
p = ps[i];
ys.PB(p);
}
}
return ys;
}
void solve(int n) {
vector<Line> ls(n);
for (auto &l : ls) {
cin >> l.p.x >> l.p.y >> l.q.x >> l.q.y;
}
///
int cnt = 1;
for (int i = 0; i < n; i++) {
vector<P> cps;
for (int j = 0; j < i; j++) {
if (is_cross(ls[i], ls[j])) {
P cross_p = intersection(ls[i].p, ls[i].q, ls[j].p, ls[j].q);
cps.PB(cross_p);
}
}
cnt += uniq(cps).size() + 1;
}
cout << cnt << endl;
}
int main(void) {
int n;
while (cin >> n, n) {
solve(n);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 111111;
int n, k;
char s[maxn];
int chd[maxn][26];
int canwin[maxn], canlose[maxn];
int tn;
int ID[128];
void insert(char *s) {
int len = strlen(s), i;
int now = 0;
for (i = 0; i < len; i++) {
if (!chd[now][ID[s[i]]])
chd[now][ID[s[i]]] = tn, now = tn++;
else
now = chd[now][ID[s[i]]];
}
}
void dfs(int x) {
int i;
bool last = true;
bool cwin = 0, close = 0, canwinlose = 0, cant = 0;
for (i = 0; i < 26; i++) {
if (chd[x][i] != 0) {
dfs(chd[x][i]);
last = false;
if (canwin[chd[x][i]]) {
if (canlose[chd[x][i]])
canwinlose = 1;
else
cwin = 1;
} else {
if (canlose[chd[x][i]])
close = 1;
else
cant = 1;
}
}
}
canwin[x] = 0;
canlose[x] = 0;
if (last)
canwin[x] = 1;
else {
if (canwinlose == 0) {
if (!(cwin && close)) {
if (cant && !cwin && !close)
canwin[x] = 1, canlose[x] = 1;
else if (cwin)
canlose[x] = 1;
else if (close)
canwin[x] = 1;
}
}
}
}
int main() {
int i, j;
for (i = 0; i < (26); i++) {
ID[i + 'a'] = i;
}
while (scanf("%d%d", &n, &k) != EOF) {
memset(chd, 0, sizeof(chd));
tn = 1;
for (i = 0; i < n; i++) {
scanf("%s", s);
insert(s);
}
canwin[0] = 0;
canlose[0] = 0;
for (i = 0; i < 26; i++) {
if (chd[0][i] != 0) {
dfs(chd[0][i]);
if (canwin[chd[0][i]]) canwin[0] = 1;
if (canlose[chd[0][i]]) canlose[0] = 1;
}
}
bool first = false;
if (canwin[0])
if (canlose[0])
first = true;
else if ((k & 1) == 1)
first = true;
if (first)
puts("First");
else
puts("Second");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct edge {
int a, b, c, f, w;
};
int dd[200];
int en[200];
int go[200];
queue<int> qu;
vector<edge> ed;
vector<int> eds[200];
int n, q;
int l[200];
int r[200];
int s, t;
void add_edge(int a, int b, int c, int w) {
edge x;
x.a = a, x.b = b, x.c = c, x.w = w, x.f = 0;
eds[a].push_back(ed.size());
ed.push_back(x);
swap(x.a, x.b);
x.c = 0;
x.w = -x.w;
eds[b].push_back(ed.size());
ed.push_back(x);
}
long long flow() {
long long ans = 0;
for (int i = 0; i < n; ++i) {
fill(dd, dd + t + 1, (long long)1e9);
fill(en, en + t + 1, 0);
fill(go, go + t + 1, -1);
qu.push(s);
en[s] = 1;
dd[s] = 0;
while (!qu.empty()) {
int x = qu.front();
en[x] = 0;
qu.pop();
for (int e : eds[x]) {
if (ed[e].c == ed[e].f) continue;
int u = ed[e].b;
if (dd[u] > dd[x] + ed[e].w) {
dd[u] = dd[x] + ed[e].w;
go[u] = e;
if (!en[u]) qu.push(u), en[u] = 1;
}
}
}
int now = t;
ans += dd[t];
while (now != s) {
int e = go[now];
++ed[e].f;
--ed[e ^ 1].f;
now = ed[e].a;
}
}
return ans;
}
int main() {
cin >> n >> q;
for (int i = 0; i < n; ++i) {
l[i] = 0;
r[i] = n - 1;
}
for (int i = 0; i < q; ++i) {
int tl, tr, v, t;
scanf("%d%d%d%d", &t, &tl, &tr, &v);
--v, --tl;
for (int j = tl; j < tr; ++j) {
if (t == 1)
l[j] = max(l[j], v);
else
r[j] = min(r[j], v);
}
}
for (int i = 0; i < n; ++i)
if (l[i] > r[i]) {
cout << -1 << "\n";
return 0;
}
s = 2 * n;
t = s + 1;
for (int i = 0; i < n; ++i) {
add_edge(s, i, 1, 0);
for (int j = l[i]; j <= r[i]; ++j) add_edge(i, n + j, 1, 0);
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) add_edge(i + n, t, 1, j * 2 + 1);
}
cout << flow() << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int m[maxn];
int c[maxn];
int cnt[maxn];
vector<int> f[maxn];
int calc(int x, int y) { return x / y + (x % y != 0); }
int main(void) {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &m[i]);
cnt[m[i]]++;
}
for (int i = 1; i <= k; i++) scanf("%d", &c[i]);
int res = 0;
for (int i = k; i >= 1; i--) cnt[i] += cnt[i + 1];
for (int i = 1; i <= k; i++) {
res = max(res, calc(cnt[i], c[i]));
}
sort(m, m + n);
for (int i = 0; i < n; i++) {
f[i % res].push_back(m[i]);
}
printf("%d\n", res);
for (int i = 0; i < res; i++) {
int sz = f[i].size();
printf("%d ", sz);
for (int j = 0; j < sz; j++)
printf("%d%c", f[i][j], (j == sz - 1) ? '\n' : ' ');
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char s[105], t[105];
int nxt[105][26], cnts[26], cntt[26];
int T, N;
int Solve() {
memset(cnts, 0, sizeof(cnts));
memset(cntt, 0, sizeof(cntt));
for (int i = 1; i <= N; ++i) ++cnts[s[i] - 'a'];
for (int i = 1; i <= N; ++i) ++cntt[t[i] - 'a'];
for (int i = 0; i < 26; ++i)
if (cnts[i] != cntt[i]) return -1;
memset(nxt, 0, sizeof(nxt));
for (int i = N; i >= 1; --i) {
for (int j = 0; j < 26; ++j) nxt[i - 1][j] = nxt[i][j];
nxt[i - 1][s[i] - 'a'] = i;
}
int res = 0, temp, p;
for (int j = 1; j <= N; ++j) {
p = 0;
temp = 0;
for (int i = j; i <= N; ++i) {
if (nxt[p][t[i] - 'a']) {
++temp;
p = nxt[p][t[i] - 'a'];
} else
break;
}
res = max(res, temp);
}
if (!res) return -1;
return N - res;
}
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &N);
scanf("%s", s + 1);
scanf("%s", t + 1);
printf("%d\n", Solve());
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool umax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
long long POW(long long a, long long p, long long M) {
if (!p) return 1LL;
long long T = POW(a, p / 2, M);
T = T * T % M;
if (p & 1) T = T * (a % M) % M;
return T;
}
long long SQRT(long long a) {
long long b = (long long)sqrtl(((double)a) + 0.5);
while (b * b < a) ++b;
while (b * b > a) --b;
return (b * b == a) ? b : -1;
}
const long long MOD = 1e9 + 7;
int g, r;
const int N = 1e4 + 2;
const int M = 1002;
vector<int> d;
int n, m;
int vis[N][M];
void bfs() {
vis[0][0] = 1;
deque<pair<int, int>> dd;
dd.push_front({0, 0});
while (dd.size() > 0) {
auto it = dd.front();
dd.pop_front();
int nd = it.first;
int lft = it.second;
int tk = lft;
if (tk == 0) tk = g;
int dist = -1;
if (nd - 1 >= 0) dist = tk - (d[nd] - d[nd - 1]);
if (nd - 1 >= 0 && dist >= 0 && vis[nd - 1][dist] == -1) {
if (dist == 0) {
dd.push_back({nd - 1, 0});
vis[nd - 1][dist] = vis[nd][lft] + 1;
} else {
dd.push_front({nd - 1, dist});
vis[nd - 1][dist] = vis[nd][lft];
}
}
dist = -1;
if (nd + 1 < d.size()) dist = tk - (d[nd + 1] - d[nd]);
if (nd + 1 < d.size() && dist >= 0 && vis[nd + 1][dist] == -1) {
if (dist == 0) {
dd.push_back({nd + 1, 0});
vis[nd + 1][dist] = vis[nd][lft] + 1;
} else {
dd.push_front({nd + 1, dist});
vis[nd + 1][dist] = vis[nd][lft];
}
}
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
cin >> n >> m;
d.resize(m);
for (int i = 0; i < m; i++) cin >> d[i];
sort((d).begin(), (d).end());
cin >> g >> r;
memset(vis, -1, sizeof(vis));
bfs();
long long ans = -1;
for (int i = 0; i < g; i++) {
if (vis[d.size() - 1][i] != -1) {
int cnt = vis[d.size() - 1][i];
if (i == 0) cnt--;
long long here = g * (cnt - 1);
here += r * (cnt - 1);
here += g - i;
if (ans == -1)
ans = here;
else
umin(ans, here);
}
}
cout << ans << endl;
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 6e2;
vector<long long> g[MAXN];
int used[MAXN];
int main() {
ios_base::sync_with_stdio(0);
long long n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i) {
long long a, b;
cin >> a >> b;
--a;
--b;
g[a].push_back(b);
g[b].push_back(a);
}
int cur = -1;
bool fl = false;
long long b = 0;
for (int i = 0; i < n; ++i) {
if (int(g[i].size()) == n - 1) {
used[i] = 2;
++b;
} else if (!fl) {
fl = 1;
cur = i;
used[cur] = 1;
}
}
if (cur >= 0) {
long long a = 1;
for (int i = 0; i < int(g[cur].size()); ++i) {
if (used[g[cur][i]] == 0) {
used[g[cur][i]] = 1;
++a;
}
}
for (int i = 0; i < n; ++i) {
if (used[i] == 1) {
if (int(g[i].size()) != a - 1 + b) {
cout << "No";
return 0;
}
} else if (used[i] == 0) {
if (int(g[i].size()) != n - a - 1) {
cout << "No";
return 0;
}
}
}
cout << "Yes\n";
for (int i = 0; i < n; ++i) {
if (used[i] == 0) {
cout << 'c';
}
if (used[i] == 1) {
cout << 'a';
}
if (used[i] == 2) {
cout << 'b';
}
}
} else {
cout << "Yes\n";
for (int i = 0; i < n; ++i) {
cout << "a";
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int p[10100], root;
vector<vector<int> > ops;
vector<vector<pair<int, int> > > g;
vector<int> pr;
vector<vector<int> > leaves;
void dfs1(int x) {
p[x] = 1;
for (auto it : g[x])
if (!p[it.first]) {
pr[it.first] = x;
dfs1(it.first);
leaves[x].push_back(leaves[it.first][0]);
}
if (!leaves[x].size()) leaves[x].push_back(x);
}
void add_path(int x, int val) {
if (leaves[x].size() == 1) {
ops.push_back({x, root, val});
return;
}
ops.push_back({root, leaves[x][0], val / 2});
ops.push_back({root, leaves[x][1], val / 2});
ops.push_back({leaves[x][0], leaves[x][1], -val / 2});
}
void add_edge(int x, int val) {
if (pr[x] == root) {
add_path(x, val);
return;
}
add_path(x, val);
add_path(pr[x], -val);
}
void dfs2(int x) {
p[x] = 1;
for (auto it : g[x])
if (!p[it.first]) {
add_edge(it.first, it.second);
dfs2(it.first);
}
}
int main() {
int n, i, u, v, val;
ios::sync_with_stdio(false);
cin >> n;
g.resize(n + 1);
leaves.resize(n + 1);
pr.resize(n + 1);
for (i = 1; i < n; i++) {
cin >> u >> v >> val;
g[u].push_back(make_pair(v, val));
g[v].push_back(make_pair(u, val));
}
for (i = 1; i <= n; i++) {
if (g[i].size() == 2) {
cout << "NO" << endl;
return 0;
}
if (g[i].size() == 1) root = i;
}
dfs1(root);
memset(p, 0, sizeof(p));
dfs2(root);
cout << "YES" << endl;
cout << ops.size() << endl;
for (auto it : ops) cout << it[0] << " " << it[1] << " " << it[2] << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int const MAXN = 4000;
int adj[MAXN][MAXN]{};
int degree[MAXN]{};
int main() {
int n, m;
cin >> n >> m;
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
u--;
v--;
adj[u][v] = true;
adj[v][u] = true;
degree[u]++;
degree[v]++;
}
int res = INT_MAX;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (adj[i][j]) {
for (int k = j + 1; k < n; k++) {
if (adj[i][k] && adj[j][k]) {
res = min(res, degree[i] + degree[j] + degree[k] - 6);
}
}
}
}
}
if (res == INT_MAX) {
cout << -1;
} else {
cout << res;
}
return 0;
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.