solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 998244353;
struct H {
int p, id;
} he[N];
long long qpow(long long base, long long n) {
long long ans = 1;
while (n) {
if (n & 1) ans = (ans * base) % mod;
n >>= 1;
base = base * base % mod;
}
return ans;
}
long long n, m;
int ar[N];
int dp[N];
long long C(long long n, long long m) {
long long resdw1 = 1;
for (int i = 1; i <= n; i++) resdw1 = resdw1 * i % mod;
long long resdw2 = 1;
for (int i = 1; i <= m - n; i++) resdw2 = resdw2 * i % mod;
long long resup = 1;
for (int i = 1; i <= m; i++) resup = resup * i % mod;
return resup * qpow(resdw1, mod - 2) % mod * qpow(resdw2, mod - 2) % mod;
}
long long ans;
int main() {
{
scanf("%lld %lld", &n, &m);
if (n == 2) {
printf("0\n");
return 0;
}
ans = (n - 2) * qpow(2, n - 3);
ans %= mod;
ans *= C(n - 1, m);
ans %= mod;
printf("%lld\n", ans);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int A[4];
int ff[(1001000)];
int ge0, ge1, ge01, ge10;
inline void solve() {
ge0 = ge1 = -1;
for (int i = 1; i <= n; ++i)
if (ff[i] == A[0]) {
ge0 = i;
break;
}
for (int i = 1; i <= n; ++i)
if (ff[i] == A[3]) {
ge1 = i;
break;
}
if (A[0] == 0 && A[1] == 0 && A[2] == 0) ge0 = 0;
if (A[3] == 0 && A[2] == 0 && A[1] == 0) ge1 = 0;
if (A[0] + A[1] + A[2] + A[3] == 0) {
printf("0\n");
return;
}
if (ge0 == -1 || ge1 == -1) {
printf("Impossible\n");
return;
}
if (A[1] + A[2] != ge0 * ge1) {
printf("Impossible\n");
return;
}
ge01 = A[1];
for (; ge0; --ge0) {
if (ge01 - ge1 < 0) break;
printf("0");
ge01 -= ge1;
}
for (int i = 1; i <= ge1 - ge01; ++i) printf("1");
ge1 = ge01;
if (ge0) {
printf("0");
ge0--;
}
for (int i = 1; i <= ge1; ++i) printf("1");
for (int i = 1; i <= ge0; ++i) printf("0");
}
inline void beforedo() {
cin >> A[0] >> A[1] >> A[2] >> A[3];
for (int i = 1; i; ++i) {
ff[i] = i * (i - 1) / 2;
if ((long long)i * (long long)(i - 1) > 2000000000) {
n = i - 1;
break;
}
}
}
int main() {
beforedo();
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long double PI = acos(-1);
const int N = 1e5 + 10;
const int MOD = 1e9 + 7;
const int mod1 = 998244353;
const long long INF = 2e18;
template <class T>
inline void amin(T &x, const T &y) {
if (y < x) x = y;
}
template <class T>
inline void amax(T &x, const T &y) {
if (x < y) x = y;
}
int read() {
long long s = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
s = s * 10 + c - '0';
c = getchar();
}
return s * f;
}
long long Pow(long long a, long long b) {
if (a == 1 || b == 1) return a;
if (b % 2 == 1) return (a * Pow(a, b - 1)) % MOD;
return (Pow(a, b / 2) * Pow(a, b / 2)) % MOD;
}
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++)
if (n % i == 0) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
{
int n, m;
cin >> n >> m;
int x, y;
set<int> st;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
for (int i = 0; i < s.length(); i++) {
if (s[i] == 'G') {
x = i;
}
if (s[i] == 'S') {
y = i;
}
}
if (y < x) {
cout << -1;
return 0;
} else {
st.insert(x - y);
}
}
cout << st.size();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, b[200010], c[200010];
long long sum;
int a[200010];
int bucket[30];
bool check() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < 30; j++) {
if ((a[i] >> j) & 1) {
bucket[j]++;
}
}
}
for (int i = 0; i < n; i++) {
long long s = 0;
for (int j = 0; j < 30; j++) {
if ((a[i] >> j) & 1) s += bucket[j] * (1LL << j);
}
if (s != b[i]) return 1;
s = 0;
for (int j = 0; j < 30; j++) {
if ((a[i] >> j) & 1) {
s += n * (1LL << j);
} else {
s += bucket[j] * (1LL << j);
}
}
if (s != c[i]) return 1;
}
return 0;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", b + i);
for (int i = 0; i < n; i++) scanf("%d", c + i);
sum = accumulate(b, b + n, accumulate(c, c + n, 0LL));
if (sum % (2 * n) != 0) {
puts("-1");
return 0;
}
sum /= 2 * n;
for (int i = 0; i < n; i++) {
long long v = b[i] + c[i] - sum;
if (v < 0 || v % n != 0) {
puts("-1");
return 0;
}
a[i] = v / n;
}
if (check()) {
puts("-1");
return 0;
}
for (int i = 0; i < n; i++) printf("%d ", a[i]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int nm = 2e5, pm = 1e4;
int n, p, c[pm], ten[pm];
long long rv = 0;
string s;
int main() {
cin >> n >> p >> s;
if (10 % p) {
ten[0] = 1;
for (int i = 1; i < p - 1; i++) {
ten[i] = ten[i - 1] * 10 % p;
}
c[0]++;
for (int i = 0, v = 0; i < n; i++) {
v = (v + (s[n - 1 - i] - '0') * ten[i % (p - 1)]) % p;
c[v]++;
}
for (int i = 0; i < p; i++) {
rv += (long long) c[i] * (c[i] - 1) / 2;
}
} else {
for (int i = 0; i < n; i++) {
if (!((s[i] - '0') % p)) {
rv += i + 1;
}
}
}
cout << rv << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
constexpr long long int MOD = 1000000007;
constexpr double EPS = 1e-9;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
vector<int> a({0, 1, 2, 3});
vector<int> b({0, 2, 3, 1});
vector<int> c({0, 3, 1, 2});
cin >> K;
while (K--) {
cin >> N;
if (N <= 3) {
cout << N << endl;
continue;
}
N--;
long long int add = 1;
long long int sum = 0;
while (N / 3 >= add + sum) {
sum += add;
add <<= 2;
}
long long int ans = add * (N % 3 + 1);
long long int amari = N / 3 - sum;
add >>= 2;
if (N % 3 == 0) {
while (add) {
ans += add * a[amari / add];
amari %= add;
add >>= 2;
}
} else if (N % 3 == 1) {
while (add) {
ans += add * b[amari / add];
amari %= add;
add >>= 2;
}
} else {
while (add) {
ans += add * c[amari / add];
amari %= add;
add >>= 2;
}
}
cout << ans << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool smin(T &a, const T &b) {
return a > b ? a = b : a;
}
template <typename T>
inline bool smax(T &a, const T &b) {
return a < b ? a = b : a;
}
const int N = (int)72, mod = (int)0;
int ma[N][N], px[N], mb[N][N], par[N], sz[N], rev[N];
vector<pair<int, int>> adj[N];
int find(int first) {
return first == par[first] ? first : par[first] = find(par[first]);
}
void unite(int u, int v) { par[find(u)] = find(v); }
int res[N], mark[N], q[N * N], dp[1 << 18][N], add_mask[N];
int main() {
int n, m, a, b;
cin >> n >> m >> a >> b;
memset(ma, 63, sizeof ma);
memset(mb, 63, sizeof mb);
for (int j = 0; j < n; ++j) par[j] = j, ma[j][j] = mb[j][j] = 0;
for (int j = 0; j < m; ++j) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
if (w == a) {
unite(u, v);
ma[u][v] = ma[v][u] = w;
} else {
mb[u][v] = mb[v][u] = w;
}
}
for (int j = 0; j < n; ++j) {
par[j] = find(j);
sz[par[j]]++;
}
int tot = 0;
for (int j = 0; j < n; ++j)
if (par[j] == j && sz[j] >= 4) {
px[tot] = j;
rev[j] = tot++;
}
for (int j = 0; j < n; ++j) {
if (sz[par[j]] >= 4) {
add_mask[j] = 1 << rev[par[j]];
}
}
memset(dp, 63, sizeof dp);
dp[add_mask[0]][0] = 0;
for (int mask = 0; mask < 1 << tot; ++mask) {
int h = 0, t = 0;
for (int v = 0; v < n; ++v) q[t++] = v, mark[v] = 1;
while (h != t) {
int v = q[h++];
mark[v] = 0;
if (dp[mask][v] < 1e9) {
for (auto e : adj[v]) {
int u = e.first, w = e.second;
if (par[u] == par[v] && w == b) continue;
if (par[u] != par[v]) {
if (!(mask & add_mask[u])) {
if (dp[mask | add_mask[u]][u] > dp[mask][v] + w) {
dp[mask | add_mask[u]][u] = dp[mask][v] + w;
if (!add_mask[u] && !mark[u]) {
mark[u] = 1;
q[t++] = u;
}
}
}
} else {
if (dp[mask][u] > dp[mask][v] + w) {
dp[mask][u] = dp[mask][v] + w;
if (!mark[u]) {
mark[u] = 1;
q[t++] = u;
}
}
}
}
}
}
}
memset(res, 63, sizeof res);
for (int mask = 0; mask < 1 << tot; ++mask)
for (int v = 0; v < n; ++v) res[v] = min(res[v], dp[mask][v]);
for (int j = 0; j < n; ++j) cout << res[j] << ' ';
cout << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
string s, t;
cin >> s >> t;
int ans = 0;
vector<int> v;
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
char cur = t[i];
int ind = -1;
for (int j = i + 1; j < n; j++)
if (s[j] == cur) ind = j;
if (ind == -1) {
cout << -1;
return 0;
}
for (int j = ind - 1; j >= i; j--) {
swap(s[ind], s[j]);
ind = j;
v.push_back(j + 1);
ans++;
}
}
}
cout << ans << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const double EPS = 1e-9;
long long int N;
vector<long long int> X;
int main(void) {
cin >> N;
X.push_back(1);
for (long long int A = 1; A * A <= N; A++) {
if (N % A) continue;
X.push_back(A);
if (A * A != N) X.push_back(N / A);
}
long long int Min = 1000000000000LL, Max = 0LL;
int CntDivisors = X.size();
for (int i = 0; i < CntDivisors; i++)
for (int j = 0; j < CntDivisors; j++) {
if (N % (X[i] * X[j])) continue;
long long int A = X[i] + 1LL;
long long int B = X[j] + 2LL;
long long int C = N / (X[i] * X[j]) + 2LL;
long long int Cnt = A * B * C - N;
if (Cnt < Min) Min = Cnt;
if (Cnt > Max) Max = Cnt;
}
cout << Min << " " << Max << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll b[2010][2010], ans = 1e18;
int main(void) {
ll n, x;
cin >> n >> x;
vector<ll> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
ll sum = i * x;
for (int j = 0; j < n; j++) {
if (i) {
b[i][j] = min(b[i - 1][j], a[(j - i + n) % n]);
} else {
b[i][j] = a[j];
}
sum += b[i][j];
}
ans = min(ans, sum);
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
int n;
long long int ra, rb, rc;
long long int d;
long long int a[1001000];
long long int cost[1001000][2];
long long int dp[1001000][2][2];
void setmin(long long int &lval, long long int rval) {
if (lval > rval) lval = rval;
}
int main() {
scanf("%d%lld%lld%lld%lld", &n, &ra, &rb, &rc, &d);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 0; i < n; i++) {
cost[i][0] = ra * a[i] + rc;
cost[i][1] = ra * (a[i] + 2);
if (cost[i][1] > rb + ra) cost[i][1] = rb + ra;
cost[i][0] += d;
cost[i][1] += 2 * d;
if (cost[i][1] > cost[i][0] + d) cost[i][1] = cost[i][0] + d;
if (cost[i][0] > cost[i][1] + d) cost[i][0] = cost[i][1] + d;
}
for (int i = 0; i <= n; i++) {
dp[i][0][0] = 1e18;
dp[i][0][1] = 1e18;
dp[i][1][0] = 1e18;
dp[i][1][1] = 1e18;
}
dp[0][0][0] = 0;
dp[0][0][1] = d;
for (int i = 0; i < n; i++) {
setmin(dp[i + 1][0][0], dp[i][0][0] + cost[i][0]);
setmin(dp[i + 1][0][0], dp[i][0][1] + cost[i][1]);
setmin(dp[i + 1][0][1], dp[i][0][0] + cost[i][1]);
setmin(dp[i + 1][0][1], dp[i + 1][0][0] + d);
setmin(dp[i + 1][0][0], dp[i + 1][0][1] + d);
setmin(dp[i + 1][1][1], dp[i + 1][0][0]);
setmin(dp[i + 1][1][1], dp[i + 1][0][1]);
setmin(dp[i + 1][1][0], dp[i + 1][0][0]);
setmin(dp[i + 1][1][0], dp[i][1][1] + cost[i][0]);
setmin(dp[i + 1][1][1], dp[i][1][1] + cost[i][1]);
setmin(dp[i + 1][1][1], dp[i + 1][1][0] + d);
setmin(dp[i + 1][1][0], dp[i + 1][1][1] + d);
}
printf("%lld", dp[n][1][0] - d);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, x, ind, h[2005], arr[2005];
vector<int> v[2005], g[2005];
void dfs(int nod) {
if (h[nod]) return;
h[nod] = 1;
for (typeof(v[nod].begin()) it = v[nod].begin(); it != v[nod].end(); it++)
dfs(*it);
arr[++ind] = nod;
}
void rec(int nod) {
if (h[nod]) return;
h[nod] = 1;
for (typeof(g[nod].begin()) it = g[nod].begin(); it != g[nod].end(); it++)
rec(*it);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
scanf("%d", &x);
if (x) v[i].push_back(j), g[j].push_back(i);
}
for (int i = 1; i <= n; i++) {
if (h[i]) continue;
dfs(i);
}
reverse(arr + 1, arr + ind + 1);
ind = 0;
memset(h, 0, sizeof h);
for (int i = 1; i <= n; i++) {
if (h[arr[i]]) continue;
ind++;
if (ind == 2) return puts("NO");
rec(arr[i]);
}
puts("YES");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main(){
string a,b;
cin>>a>>b;
if(a==b)
cout<<"H";
else
cout<<"D";
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int N = 2000005;
const long long int mod = 1e9 + 7;
long long int n, i, j;
long long int a[N];
long long int sz[N];
long long int in[N];
vector<long long int> g[N];
long long int par[N];
double dp[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (i = 2; i <= n; i++) {
cin >> par[i];
}
for (i = 1; i <= n; i++) sz[i] = 1;
for (i = n; i > 0; i--) sz[par[i]] += sz[i];
dp[1] = 1;
for (i = 2; i <= n; i++) {
dp[i] = dp[par[i]] + 0.5 * (1 + sz[par[i]] - sz[i]);
}
for (i = 1; i <= n; i++) {
cout << fixed << setprecision(10) << dp[i] << " ";
}
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a[55];
int p=0;
cin >> n;
for(int i=1;i<=n;i++){
cin >> a[i];
if(a[i] != i) p++;
}
if(p == 1 || p>2) printf("NO\n");
else printf("YES\n");
return 0;
}
| 0 |
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
pair<string, vector<string> > parse(string s)
{
for(int i=0; i<s.size(); i++)
if(!isalpha(s[i])) s[i] = ' ';
s.erase(s.end() - 1);
stringstream ss(s);
string n;
ss >> n;
string a;
vector<string> res;
while(ss >> a) { res.push_back(a); }
return make_pair(n, res);
}
bool vis[100];
set<string> ans;
void dfs(int u, map<string, int>& dic, vector<vector<string> >& graph)
{
vis[u] = true;
for(int i=0; i<graph[u].size(); i++) {
string n = graph[u][i];
if(dic.count(n)) {
int v = dic[n];
if(vis[v]) continue;
dfs(v, dic, graph);
}
else ans.insert(n);
}
}
int main()
{
int N;
while(cin >> N, N) {
map<string, int> dic;
vector<vector<string> > graph(N);
for(int i=0; i<N; i++) {
string s;
cin >> s;
pair<string, vector<string> > p = parse(s);
string n = p.first;
dic[n] = i;
for(int j=0; j<p.second.size(); j++)
graph[i].push_back(p.second[j]);
}
memset(vis, 0, sizeof(vis));
ans.clear();
dfs(0, dic, graph);
cout << ans.size() << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int Set(int mask, int pos) { return mask = mask | (1 << pos); }
bool check(int mask, int pos) { return (bool)(mask & (1 << pos)); }
int dp[2005][2005], l, r, n, h;
int arr[2005];
int solve(int pos, int hour) {
if (pos == n) {
if (hour >= l && hour <= r) return 1;
return 0;
}
if (dp[pos][hour] != -1) return dp[pos][hour];
int ret =
(hour >= l && hour <= r) + solve(pos + 1, (hour + arr[pos] - 1) % h);
ret = max(ret,
(hour >= l && hour <= r) + solve(pos + 1, (hour + arr[pos]) % h));
return dp[pos][hour] = ret;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> h >> l >> r;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
memset(dp, -1, sizeof(dp));
int res = solve(0, 0);
if (l == 0) res--;
cout << res << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
int main() {
int h, w;
cin >> h >> w;
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
char arr[h + 1][w + 1];
int level[h + 1][w + 1];
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j)
cin >> arr[i][j], level[i][j] = 1e9;
level[r1][c1] = 0;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
deque <int> dq;
dq.push_back(r1);
dq.push_back(c1);
while (!dq.empty()) {
int x = dq.front();
dq.pop_front();
int y = dq.front();
dq.pop_front();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx > 0 and nx <= h and ny > 0 and ny <= w and arr[nx][ny] == '.' and level[nx][ny] > level[x][y]) {
level[nx][ny] = level[x][y];
dq.push_front(ny);
dq.push_front(nx);
}
}
for (int nx = x + 2; nx >= x - 2; --nx) {
for (int ny = y + 2; ny >= y - 2; --ny) {
if (nx > 0 and nx <= h and ny > 0 and ny <= w and arr[nx][ny] == '.' and level[nx][ny] > level[x][y] + 1) {
level[nx][ny] = level[x][y] + 1;
dq.push_back(nx);
dq.push_back(ny);
}
}
}
}
cout << (level[r2][c2] == 1e9 ? -1 : level[r2][c2]) << endl;
}
| 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:167772160000")
using namespace std;
vector<int> LIS;
int addToLIS(int a) {
int low = 0;
int high = LIS.size();
while (low < high) {
int mid = (low + high) / 2;
if (LIS[mid] <= a)
low = mid + 1;
else
high = mid;
}
if (low == LIS.size()) LIS.push_back(0);
LIS[low] = a;
return low + 1;
}
int answers[110000];
int a[110000];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < (n); i++) {
int l, r;
cin >> l >> r;
a[l]++;
a[r + 1]--;
}
for (int i = 1; i <= (m); i++) a[i] += a[i - 1];
for (int i = 1; i <= (m); i++) answers[i] += addToLIS(a[i]);
LIS.clear();
for (int i = m; i >= 1; i--) answers[i] += addToLIS(a[i]);
int answer = 0;
for (int i = 1; i <= (m); i++) answer = max(answer, answers[i] - 1);
cout << answer << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
const int M = 2e9;
int n, i, a = M, k, max1 = -M, max2 = -M, min1 = M, min2 = M;
int main() {
scanf("%d%d%d", &n, &n, &n);
for (i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
if (x + y > max1) max1 = x + y;
if (x + y < min1) min1 = x + y;
if (x - y > max2) max2 = x - y;
if (x - y < min2) min2 = x - y;
}
scanf("%d", &n);
for (i = 0; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
int p1 = x + y, p2 = x - y;
int c = 0;
if (max1 - p1 > c) c = max1 - p1;
if (p1 - min1 > c) c = p1 - min1;
if (max2 - p2 > c) c = max2 - p2;
if (p2 - min2 > c) c = p2 - min2;
if (c < a) a = c, k = i;
}
printf("%d\n%d\n", a, k + 1);
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int N = 300005;
using namespace std;
int n, m, q, qn, st[N], f[N], lst, sz[N], top[N], son[N], dep[N], t, a[N],
b[N * 3], tot;
struct G {
int to[N * 4], hd[N * 4], lk[N], cnt = 1, dfn[N], low[N], scn, scc[N];
inline void add(int u, int v) { to[++cnt] = v, hd[cnt] = lk[u], lk[u] = cnt; }
inline void Add(int u, int v) {
if (u && v) add(u, v), add(v, u);
}
void dfs(int x, int y) {
if (dfn[x]) return;
dfn[x] = low[x] = ++cnt, st[++t] = x;
for (int s, i = lk[x]; i; i = hd[i])
if (i ^ y) dfs(s = to[i], i ^ 1), low[x] = min(low[x], low[s]);
if (dfn[x] == low[x])
for (scn++; scc[st[t]] = scn, st[t--] ^ x;)
;
}
void dfs(int x) {
sz[x] = 1, dep[x] = dep[f[x]] + 1, dfn[x] = ++cnt;
for (int s, i = lk[x]; i; i = hd[i])
if (f[x] ^ (s = to[i]))
f[s] = x, dfs(s), sz[x] += sz[s], sz[son[x]] < sz[s] ? son[x] = s : 0;
}
void clr(int x) {
if (!dfn[x]) return;
dfn[x] = 0;
for (int s, i = lk[x]; i; i = hd[i]) clr(to[i]);
lk[x] = 0;
}
void dfss(int x) {
!top[x] ? top[x] = x : 0;
top[son[x]] = top[x];
for (int s, i = lk[x]; i; i = hd[i])
if (f[s = to[i]] == x) dfss(s);
}
void trans();
} I, T, Q;
void G::trans() {
for (int x = 1; x <= n; x++)
for (int i = lk[x]; i; i = hd[i])
if (scc[to[i]] ^ scc[x]) T.add(scc[to[i]], scc[x]);
}
bool cmp(int x, int y) { return T.dfn[x] < T.dfn[y]; }
inline int lca(int u, int v) {
for (; top[u] ^ top[v]; u = f[top[u]])
if (dep[top[u]] < dep[top[v]]) swap(u, v);
return dep[u] < dep[v] ? u : v;
}
inline void rd(int& x) {
scanf("%d", &x), x = (x + n + lst - 1) % n + 1;
b[tot++] = x = I.scc[x];
}
int u, v, w;
int main() {
scanf("%d%d%d", &n, &m, &q);
for (; m--;) scanf("%d%d", &u, &v), I.Add(u, v);
for (int i = 1; i <= n; i++) I.dfs(i, 0);
I.trans();
for (int i = 1; i <= I.scn; i++)
if (!T.dfn[i]) T.dfs(i), T.dfss(i);
top[0] = 0;
for (int al = 1; al <= q; al++) {
scanf("%d%d", &qn, &m);
tot = 0, Q.cnt = 1;
for (int i = 0; i < qn; i++) rd(a[i]);
for (; m--;) rd(u), rd(v), Q.Add(u, v);
sort(b, b + tot, cmp);
for (int i = 0; i < tot; i++) {
w = lca(u = b[i], v = st[t]);
for (v = 0; dep[st[t]] > dep[w]; v = st[t--])
if (v) Q.Add(v, st[t]);
if (v) Q.Add(v, w);
if (st[t] ^ w) st[++t] = w;
if (u ^ w) st[++t] = u;
}
for (; t; t--) Q.Add(st[t], st[t - 1]);
for (int i = 0; i < tot; i++) Q.dfs(b[i], 0);
for (int i = 0; i < tot; i++) Q.clr(b[i]);
v = Q.scc[a[0]], u = 1;
for (int i = 1; i < qn; i++)
if (Q.scc[a[i]] ^ v) u = 0;
if (u)
puts("YES"), lst = (lst + al) % n;
else
puts("NO");
}
}
| 6 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N, K[100], L[100], dp[1 << 16];
while(cin >> N, N) {
for(int i = 0; i < N; i++) {
int M;
cin >> M >> L[i];
K[i] = 0;
while(M--) {
int S, E;
cin >> S >> E;
S -= 6, E -= 6;
K[i] |= (1 << E) - (1 << S);
}
}
memset(dp, -1, sizeof(dp));
dp[0] = 0;
int ret = 0;
for(int j = 0; j < 1 << 16; j++) {
if(~dp[j]) {
ret = max(ret, dp[j]);
for(int i = 0; i < N; i++) {
if(!(K[i] & j)) {
dp[K[i] | j] = max(dp[K[i] | j], dp[j] + L[i]);
}
}
}
}
cout << ret << endl;
}
}
| 0 |
#include<cstdio>
#define maxn 100050
#define HR 1000000007
using namespace std;
int s[maxn],dp[maxn];
int main(){
int n,k;
scanf("%d%d",&n,&k);
s[0]=dp[0]=1;
for (int i=1;i<=n;i++){
int x;
scanf("%d",&x);
for (int j=1;j<=k;j++)
s[j]=(s[j-1]+dp[j])%HR;
for (int j=1;j<=k;j++)
if (j-x>0) dp[j]=(s[j]-s[j-x-1]+HR)%HR;else
dp[j]=s[j];
}
printf("%d\n",dp[k]);
return 0;
}
/*
dp[i][j]表示前i个人 一共分了j个的方案数
dp[i][j]=dp[i-1][j-k] (0<=k<=a[i])
前缀和优化
*/
| 0 |
#include <bits/stdc++.h>
using namespace std;
char ans[105][105];
int n, m, i, j;
int main() {
scanf("%d%d", &n, &m);
if (n == 5) {
printf(">...v\nv.<..\n..^..\n>....\n..^.<\n1 1");
return 0;
}
if (n == 3) {
printf(">vv\n^<.\n^.<\n1 3");
return 0;
}
for (i = 1; i <= 100; i++)
for (j = 1; j <= 100; j++) ans[i][j] = '.';
for (i = 100; i >= 52; i--) ans[i][1] = '^';
for (i = 2; i <= 51; i += 2) ans[i][1] = '^';
ans[1][1] = '>';
for (i = 1; i <= 50; i++) ans[i][2] = 'v';
for (i = 51; i <= 98; i += 2) ans[i][2] = 'v';
ans[99][2] = '>';
for (i = 99; i >= 50; i--) ans[i][3] = '^';
for (i = 49; i >= 2; i -= 2) ans[i][3] = '^';
ans[1][3] = '>';
for (i = 4; i <= 100; i++)
for (j = 1; j <= 99; j++) ans[j][i] = ans[j][i - 2];
ans[99][100] = 'v';
ans[100][100] = '<';
for (i = 50; i <= 99; i++) ans[100][i] = '<';
for (i = 2; i <= 49; i += 2) ans[100][i] = '<';
for (i = 1; i <= 100; i++) {
for (j = 1; j <= 100; j++) cout << ans[i][j];
cout << endl;
}
cout << "100 1";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << n << endl;
while (n != 0) {
cout << 1 << " ";
n--;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> sou[112345];
int ans[112345];
long long sum;
vector<int> l[112345];
bool dfs(int pred, int akt) {
bool p = 1;
for (int i = 0; i < sou[akt].size(); i++) {
if (sou[akt][i] != pred) {
if (dfs(akt, sou[akt][i])) {
l[akt].push_back(sou[akt][i]);
sum += 2;
p = 0;
}
}
}
if (l[akt].size() > 0) {
ans[l[akt][0]] = akt;
ans[akt] = l[akt][l[akt].size() - 1];
for (int i = 1; i < l[akt].size(); i++) {
ans[l[akt][i]] = l[akt][i - 1];
}
}
l[akt].clear();
return p;
}
int main() {
int n;
cin >> n;
int a, b;
for (int i = 1; i < n; i++) {
cin >> a >> b;
sou[a].push_back(b);
sou[b].push_back(a);
}
sum = 0;
if (dfs(1, 1)) {
sum += 2;
for (int i = 1; i <= n; i++)
if (ans[i] == sou[1][0]) ans[i] = 1;
ans[1] = sou[1][0];
}
cout << sum << endl;
for (int i = 1; i <= n; i++) cout << ans[i] << " ";
cout << endl;
return 0;
}
| 2 |
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long ll;
int days[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int main()
{
int y1, m1, d1, y2, m2, d2;
while(cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2 && y1 != -1) {
int ret = 0, m = m1, y = y1;
for(;;) {
int f;
if(y % 4 == 0) {
if(y % 100 == 0) {
if(y % 400 == 0) {
f = 1;
} else {
f = 0;
}
} else {
f = 1;
}
} else {
f = 0;
}
if(y == y2 && m == m2) {
ret += d2 - d1;
break;
} else {
ret += days[f][m - 1];
}
if(m == 12) {
y++; m = 1;
continue;
} else {
m++;
}
}
cout << ret << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y){
if (x<y) return gcd(y,x);
else if(x%y == 0) return y;
else return gcd(y,x%y);
}
int main(){
int T;
cin >> T;
for (int i=0; i<T; i++){
long long A, B, C, D;
cin >> A >> B >> C >>D;
if ((D < B) || (A < B)){
cout <<"No" << endl;
} else if (C >= B){
cout << "Yes" << endl;
} else if ((C >= B-gcd(B,D)) && (A%B <= C)){
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
| 0 |
#include <iostream>
using namespace std;
int main()
{
int p[200][3];
int a[101];
int n; cin >> n;
for (int i = 0; i < n; ++i) {
cin >> p[i][0] >> p[i][1] >> p[i][2];
}
for (int i = 0; i < 3; ++i) {
for (int j = 1; j <= 100; ++j) a[j] = 0;
for (int j = 0; j < n; ++j) {
a[ p[j][i] ] += 1;
}
for (int j = 0; j < n; ++j) {
if (a[ p[j][i] ] != 1) p[j][i] = 0;
}
}
for (int i = 0; i < n; ++i) {
cout << p[i][0] + p[i][1] + p[i][2] << endl;
}
}
| 0 |
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
int H, W;
while(cin >> H >> W){
vector<string> vs(H);
for(auto& s : vs) cin >> s;
vector<vector<string>> res(2, vector<string>(H, string(W, '.')));
for(int i=0;i<2;i++){
for(int j=0;j<H;j++){
res[i][j][i==0?0:W-1] = '#';
for(int k=1;k<W-1;k++){
if(j%2 == i || vs[j][k]=='#') res[i][j][k] = '#';
}
}
}
for(int i=0;i<2;i++){
for(auto& s : res[i]) cout << s << endl;
if(i==0) cout << endl;
}
}
}
| 0 |
#include <bits/stdc++.h>
const int N=2005;
using namespace std;
int n,c,d[N],cnt,res;
struct edge{
int u,v,nxt;
}e[N<<1]; int tail,adj[N];
void addedge(int u,int v){ e[++tail]=(edge){u,v,adj[u]}; adj[u]=tail; }
void dfs(int u,int fa){
for(int k=adj[u]; k!=-1; k=e[k].nxt){
int v=e[k].v;
if(v==fa) continue;
d[v]=d[u]+1;
if(d[v]>c) ++cnt;
dfs(v,u);
}
}
int main(){
int u,v;
cin>>n>>c;
tail=1; fill(adj+1,adj+n+1,-1);
for(int i=1; i<n; ++i){
cin>>u>>v;
addedge(u,v);
addedge(v,u);
}
res=n+1;
if(!(c&1)){
c>>=1;
for(int i=1; i<=n; ++i){
cnt=0;
d[i]=0;
dfs(i,0);
res=min(res,cnt);
}
} else{
c--;
c>>=1;
for(int i=2; i<=tail; i+=2){
cnt=0;
d[e[i].u]=0;
dfs(e[i].u,e[i].v);
d[e[i].v]=0;
dfs(e[i].v,e[i].u);
res=min(res,cnt);
}
}
cout<<res<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, t, it, a[1005000], fa[10005000];
bool op[10050000];
struct SB {
int id, x, y;
bool operator<(const SB a) const { return (x == a.x) ? y < a.y : x < a.x; }
};
vector<SB> v1, v2;
void add(int x, int y, int z, int b) {
fa[++it] = x;
op[it] = b;
v2.push_back({it, y, z});
}
int main() {
ios::sync_with_stdio(0);
cin >> t;
while (t--) {
cin >> n;
it = 0;
for (i = 1; i <= n; i++) cin >> a[i];
v1.push_back({0, -11451419, -11451419});
for (i = 1; i <= n; i++) {
v2.clear();
for (auto &[id, x, y] : v1) {
if (x < -a[i])
add(id, -a[i], y, 1);
else if (y < -a[i])
add(id, x, -a[i], 1);
if (x < a[i])
add(id, a[i], y, 0);
else if (y < a[i])
add(id, x, a[i], 0);
}
if (v2.empty()) {
cout << "NO\n";
goto ccc;
}
v1.clear();
k = 1e9;
sort(v2.begin(), v2.end());
for (auto &i : v2) {
if (i.y < k) {
k = i.y;
v1.push_back(i);
}
}
}
i = (*v1.begin()).id;
j = n;
while (i) {
if (op[i]) a[j] *= -1;
j--;
i = fa[i];
}
cout << "YES\n";
for (i = 1; i <= n; i++) cout << a[i] << ' ';
cout << '\n';
ccc:;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int N, K;
char dp[105][105];
int tmp[105];
int tmp2[105];
int data[105];
int trgt[105];
bool goTmp(int tails, int hds) {
for (int i = 0; i < (N); i++) tmp[i] = i + 1;
for (int i = 0; i < (hds); i++) {
for (int i = 0; i < (N); i++) tmp2[data[i] - 1] = tmp[i];
copy(tmp2, tmp2 + N, tmp);
}
for (int i = 0; i < (tails); i++) {
for (int i = 0; i < (N); i++) tmp2[i] = tmp[data[i] - 1];
copy(tmp2, tmp2 + N, tmp);
}
return equal(tmp, tmp + N, trgt);
}
bool slv(int idx, int heads) {
char &nw = dp[idx][heads];
if (nw != '?') return nw;
bool tst = goTmp(idx - heads, heads);
if (idx == K) {
return nw = tst;
}
if (tst) return nw = false;
return nw = slv(idx + 1, heads) || slv(idx + 1, heads + 1);
}
int main() {
cin >> N >> K;
for (int i = 0; i < (N); i++) cin >> data[i];
for (int i = 0; i < (N); i++) cin >> trgt[i];
memset(dp, '?', sizeof dp);
if (slv(0, 0))
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
const long long mx_error = 15 * 1e5;
long long px = 0, py = 0;
bool try_it(long long dx, long long dy) {
long long n_x = px + dx;
long long n_y = py + dy;
long long n_x_neg = px - dx;
long long n_y_neg = py - dy;
long long er = n_x * n_x + n_y * n_y;
long long er_neg = n_x_neg * n_x_neg + n_y_neg * n_y_neg;
if (er > er_neg) return false;
return true;
}
struct delta {
int idx;
long long dx, dy;
};
bool cmp(delta a, delta b) { return a.dx > b.dx; }
int ans[100009];
int main() {
int n;
scanf("%d", &n);
vector<delta> a(n);
for (int i = 0; i < n; i++) {
int dx, dy;
scanf("%d %d", &dx, &dy);
a[i] = {i, dx, dy};
}
bool done = false;
while (!done) {
px = 0, py = 0;
random_shuffle(a.begin(), a.end());
done = true;
for (int i = 0; i < n; i++) {
long long dx, dy;
dx = a[i].dx;
dy = a[i].dy;
int res = 1;
if (!try_it(dx, dy)) res = -1;
px += 1LL * res * dx;
py += 1LL * res * dy;
ans[a[i].idx] = res;
}
if (px > mx_error || py > mx_error ||
px * px + py * py > mx_error * mx_error)
done = false;
}
for (int i = 0; i < n; i++) printf("%d%c", ans[i], " \n"[i == n - 1]);
cerr << "final:" << px << ' ' << py << '\n';
cerr << "final error = " << px * px + py * py << '\n';
}
| 5 |
#include<cstdio>
int main(){
char s[101];
long long k;
scanf("%s%lld",s,&k);
int num1=0;
while(s[num1]=='1')num1++;
if(k<=(long long)num1)printf("1");
else printf("%c",s[num1]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline int nxt() {
int first;
scanf("%d", &first);
return first;
}
const int mod = 1000000007;
pair<int, int> solve_path(const vector<pair<char, char>>& a) {
pair<long long, long long> cur[2] = {{1, 0}, {1, 0}};
pair<long long, long long> tmp[2];
for (const auto& p : a) {
if (p.first == 0 && p.second == 0) {
tmp[0] = {cur[0].first + cur[1].second, cur[0].second + cur[1].first};
tmp[1] = {cur[0].second + cur[1].second, cur[0].first + cur[1].first};
} else if (p.first == 0 && p.second == 1) {
tmp[0] = {cur[0].second + cur[1].second, cur[0].first + cur[1].first};
tmp[1] = {cur[0].first + cur[1].second, cur[0].second + cur[1].first};
} else if (p.first == 1 && p.second == 0) {
tmp[0] = {cur[1].first + cur[0].second, cur[1].second + cur[0].first};
tmp[1] = {cur[1].second + cur[0].second, cur[1].first + cur[0].first};
} else if (p.first == 1 && p.second == 1) {
tmp[0] = {cur[1].second + cur[0].second, cur[1].first + cur[0].first};
tmp[1] = {cur[1].first + cur[0].second, cur[1].second + cur[0].first};
}
for (int i = 0; i < 2; i++) {
tmp[i].first %= mod;
tmp[i].second %= mod;
cur[i] = tmp[i];
}
}
return {(cur[0].first + cur[1].first) % mod,
(cur[0].second + cur[1].second) % mod};
}
pair<int, int> solve_pathl(const vector<pair<char, char>>& a, bool rev) {
pair<long long, long long> cur[2] = {{1, 0}, {0, 1}};
if (rev) swap(cur[0], cur[1]);
pair<long long, long long> tmp[2];
for (const auto& p : a) {
if (p.first == 0 && p.second == 0) {
tmp[0] = {cur[0].first + cur[1].second, cur[0].second + cur[1].first};
tmp[1] = {cur[0].second + cur[1].second, cur[0].first + cur[1].first};
} else if (p.first == 0 && p.second == 1) {
tmp[0] = {cur[0].second + cur[1].second, cur[0].first + cur[1].first};
tmp[1] = {cur[0].first + cur[1].second, cur[0].second + cur[1].first};
} else if (p.first == 1 && p.second == 0) {
tmp[0] = {cur[1].first + cur[0].second, cur[1].second + cur[0].first};
tmp[1] = {cur[1].second + cur[0].second, cur[1].first + cur[0].first};
} else if (p.first == 1 && p.second == 1) {
tmp[0] = {cur[1].second + cur[0].second, cur[1].first + cur[0].first};
tmp[1] = {cur[1].first + cur[0].second, cur[1].second + cur[0].first};
}
for (int i = 0; i < 2; i++) {
tmp[i].first %= mod;
tmp[i].second %= mod;
cur[i] = tmp[i];
}
}
return {(cur[0].first + cur[1].first) % mod,
(cur[0].second + cur[1].second) % mod};
}
pair<int, int> solve_pathr(const vector<pair<char, char>>& a, bool rev) {
pair<long long, long long> cur[2] = {{1, 0}, {1, 0}};
pair<long long, long long> tmp[2];
for (const auto& p : a) {
if (p.first == 0 && p.second == 0) {
tmp[0] = {cur[0].first + cur[1].second, cur[0].second + cur[1].first};
tmp[1] = {cur[0].second + cur[1].second, cur[0].first + cur[1].first};
} else if (p.first == 0 && p.second == 1) {
tmp[0] = {cur[0].second + cur[1].second, cur[0].first + cur[1].first};
tmp[1] = {cur[0].first + cur[1].second, cur[0].second + cur[1].first};
} else if (p.first == 1 && p.second == 0) {
tmp[0] = {cur[1].first + cur[0].second, cur[1].second + cur[0].first};
tmp[1] = {cur[1].second + cur[0].second, cur[1].first + cur[0].first};
} else if (p.first == 1 && p.second == 1) {
tmp[0] = {cur[1].second + cur[0].second, cur[1].first + cur[0].first};
tmp[1] = {cur[1].first + cur[0].second, cur[1].second + cur[0].first};
}
for (int i = 0; i < 2; i++) {
tmp[i].first %= mod;
tmp[i].second %= mod;
cur[i] = tmp[i];
}
}
if (rev)
return {(cur[0].second + cur[1].first) % mod,
(cur[0].first + cur[1].second) % mod};
else
return {(cur[0].first + cur[1].second) % mod,
(cur[0].second + cur[1].first) % mod};
}
pair<int, int> solve_pathlr(const vector<pair<char, char>>& a, bool rev1,
bool rev2) {
pair<long long, long long> cur[2] = {{1, 0}, {0, 1}};
if (rev1) swap(cur[0], cur[1]);
pair<long long, long long> tmp[2];
for (const auto& p : a) {
if (p.first == 0 && p.second == 0) {
tmp[0] = {cur[0].first + cur[1].second, cur[0].second + cur[1].first};
tmp[1] = {cur[0].second + cur[1].second, cur[0].first + cur[1].first};
} else if (p.first == 0 && p.second == 1) {
tmp[0] = {cur[0].second + cur[1].second, cur[0].first + cur[1].first};
tmp[1] = {cur[0].first + cur[1].second, cur[0].second + cur[1].first};
} else if (p.first == 1 && p.second == 0) {
tmp[0] = {cur[1].first + cur[0].second, cur[1].second + cur[0].first};
tmp[1] = {cur[1].second + cur[0].second, cur[1].first + cur[0].first};
} else if (p.first == 1 && p.second == 1) {
tmp[0] = {cur[1].second + cur[0].second, cur[1].first + cur[0].first};
tmp[1] = {cur[1].first + cur[0].second, cur[1].second + cur[0].first};
}
for (int i = 0; i < 2; i++) {
tmp[i].first %= mod;
tmp[i].second %= mod;
cur[i] = tmp[i];
}
}
if (rev2)
return {(cur[0].second + cur[1].first) % mod,
(cur[0].first + cur[1].second) % mod};
else
return {(cur[0].first + cur[1].second) % mod,
(cur[0].second + cur[1].first) % mod};
}
pair<int, int> solve_cycle(const vector<pair<char, char>>& a) {
pair<long long, long long> cur[2][2] = {{{1, 0}, {0, 0}}, {{0, 0}, {1, 0}}};
pair<long long, long long> tmp[2];
for (int ij = 0; ij < (int)a.size() - 1; ij++) {
auto p = a[ij];
for (int j = 0; j < 2; j++) {
if (p.first == 0 && p.second == 0) {
tmp[0] = {cur[j][0].first + cur[j][1].second,
cur[j][0].second + cur[j][1].first};
tmp[1] = {cur[j][0].second + cur[j][1].second,
cur[j][0].first + cur[j][1].first};
} else if (p.first == 0 && p.second == 1) {
tmp[0] = {cur[j][0].second + cur[j][1].second,
cur[j][0].first + cur[j][1].first};
tmp[1] = {cur[j][0].first + cur[j][1].second,
cur[j][0].second + cur[j][1].first};
} else if (p.first == 1 && p.second == 0) {
tmp[0] = {cur[j][1].first + cur[j][0].second,
cur[j][1].second + cur[j][0].first};
tmp[1] = {cur[j][1].second + cur[j][0].second,
cur[j][1].first + cur[j][0].first};
} else if (p.first == 1 && p.second == 1) {
tmp[0] = {cur[j][1].second + cur[j][0].second,
cur[j][1].first + cur[j][0].first};
tmp[1] = {cur[j][1].first + cur[j][0].second,
cur[j][1].second + cur[j][0].first};
}
for (int i = 0; i < 2; i++) {
tmp[i].first %= mod;
tmp[i].second %= mod;
cur[j][i] = tmp[i];
}
}
}
return {(cur[a.back().second][a.back().first].first +
cur[a.back().second ^ 1][a.back().first].second +
cur[a.back().second][a.back().first ^ 1].second +
cur[a.back().second ^ 1][a.back().first ^ 1].second) %
mod,
(cur[a.back().second][a.back().first].second +
cur[a.back().second ^ 1][a.back().first].first +
cur[a.back().second][a.back().first ^ 1].first +
cur[a.back().second ^ 1][a.back().first ^ 1].first) %
mod};
}
int main() {
int n = nxt(), m = nxt();
int intended = 1;
vector<char> zalupa(m, 2);
vector<vector<int>> eds(m);
vector<pair<int, int>> ed;
for (int i = 0; i < n; i++) {
int k = nxt();
int first, second;
if (k == 1)
first = second = nxt();
else
first = nxt(), second = nxt();
if (first == -second) {
intended ^= 1;
} else if (first == second) {
zalupa[abs(first) - 1] = (first < 0);
} else {
int i1 = abs(first) - 1, i2 = abs(second) - 1;
eds[i1].push_back(ed.size());
eds[i2].push_back(ed.size());
ed.push_back({first, second});
}
}
pair<long long, long long> ans = {1, 0};
vector<char> used(m);
for (int i = 0; i < m; i++) {
if (!used[i] && eds[i].size() == 1) {
int pr = -1;
int v = i;
used[v] = 1;
vector<pair<char, char>> edges;
do {
int ind = eds[v][0];
if (abs(ed[ind].first) - 1 != v) swap(ed[ind].first, ed[ind].second);
eds[v].clear();
edges.push_back({ed[ind].first < 0, ed[ind].second < 0});
v = abs(ed[ind].second) - 1;
if (eds[v][0] == ind)
eds[v].erase(eds[v].begin());
else
eds[v].pop_back();
used[v] = 1;
} while (!eds[v].empty());
pair<int, int> p;
if (zalupa[i] != 2 && zalupa[v] != 2)
p = solve_pathlr(edges, zalupa[i], zalupa[v]);
else if (zalupa[i] != 2)
p = solve_pathl(edges, zalupa[i]);
else if (zalupa[v] != 2)
p = solve_pathr(edges, zalupa[v]);
else
p = solve_path(edges);
ans = {(ans.first * p.first + ans.second * p.second) % mod,
(ans.first * p.second + ans.second * p.first) % mod};
}
}
for (int i = 0; i < m; i++) {
if (!used[i] && eds[i].size() == 2) {
int v = i;
vector<pair<char, char>> edges;
do {
used[v] = 1;
int ind = eds[v][0];
if (abs(ed[ind].first) - 1 != v) swap(ed[ind].first, ed[ind].second);
eds[v].clear();
edges.push_back({ed[ind].first < 0, ed[ind].second < 0});
v = abs(ed[ind].second) - 1;
if (v != i) {
if (eds[v][0] == ind)
eds[v].erase(eds[v].begin());
else
eds[v].erase(eds[v].begin() + 1);
}
} while (v != i);
pair<int, int> p = solve_cycle(edges);
ans = {(ans.first * p.first + ans.second * p.second) % mod,
(ans.first * p.second + ans.second * p.first) % mod};
}
}
for (int i = 0; i < m; i++) {
if (!used[i] && zalupa[i] == 2) {
ans = {ans.first * 2 % mod, ans.second * 2 % mod};
}
}
for (int i = 0; i < m; i++) {
if (!used[i] && zalupa[i] < 2) {
ans = {(ans.first + ans.second) % mod, (ans.first + ans.second) % mod};
}
}
cout << (intended ? ans.second : ans.first) << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool a;
int main() {
int n, m;
cin >> n >> m;
for (int i = n + 1; i <= m; ++i) {
for (int j = 2; j < i; ++j) {
if (i % j == 0) a = 1;
}
if (i == m && a == 0) {
cout << "YES";
return 0;
}
if (a == 0) {
break;
}
a = 0;
}
cout << "NO";
}
| 1 |
#include <bits/stdc++.h>
const double pi = 3.1415926535897932;
static bool circle_center(std::complex<double> *z,
const std::complex<double> z0,
const std::complex<double> z1,
const std::complex<double> z2) {
const double a = 2 * (z0 - z1).real(), b = 2 * (z0 - z1).imag();
const double c = 2 * (z0 - z2).real(), d = 2 * (z0 - z2).imag();
const double p = std::norm(z0) - std::norm(z1);
const double q = std::norm(z0) - std::norm(z2);
const double det = a * d - b * c;
const double s = d * p + -b * q;
const double t = -c * p + a * q;
if (det != 0.0) {
if (z != nullptr) *z = {s / det, t / det};
return true;
}
return false;
}
int main(int argc, const char *const argv[]) {
double ax, ay, bx, by, cx, cy;
const int m = 100;
if (scanf("%lf %lf %lf %lf %lf %lf", &ax, &ay, &bx, &by, &cx, &cy) != 6) {
fprintf(stderr, "Header IO error.\n");
return EXIT_FAILURE;
}
const std::complex<double> z0(ax, ay), z1(bx, by), z2(cx, cy);
std::complex<double> z;
if (circle_center(&z, z0, z1, z2)) {
const std::complex<double> u = (z1 - z) / (z0 - z), v = (z2 - z) / (z0 - z);
for (int k = 3; k <= m; k++)
if (std::abs(std::arg(std::pow(u, k))) < 0.5 * pi / m &&
std::abs(std::arg(std::pow(v, k))) < 0.5 * pi / m) {
const double a = k * std::sin(2 * pi / k) / 2;
printf("%.6f\n", a * std::norm(z0 - z));
break;
}
}
return EXIT_SUCCESS;
}
| 3 |
#include <bits/stdc++.h>
char ss[510000 * 2], s[510000];
int b[510000], a[510000];
int main() {
int n, i, cnt, len, k, t, j;
int slen, p;
while (~scanf("%d", &n)) {
cnt = 0, k = 0, t = 0;
slen = 0, p = 0;
for (i = 0; i < n; i++) {
scanf("%s", s);
len = strlen(s);
slen += len;
a[cnt++] = len;
for (j = 0; j < len; j++) ss[k++] = s[j];
}
ss[k] = '\0';
for (i = 0; i < slen - 1; i++) {
if (ss[i] == '#') b[p++] = i;
}
char *rear, *q;
int g = 2;
while (p - g >= 0) {
rear = ss + b[p - g + 1] + 1, q = ss + b[p - g] + 1;
while (*q != '#') {
if (*q < *rear)
break;
else if (*q == *rear) {
q++;
rear++;
} else {
while (*q != '#') {
*q = '0';
q++;
}
}
}
g++;
}
for (i = 0; i < slen; i++) {
if (ss[i] != '0') printf("%c", ss[i]);
if (ss[i + 1] == '#') printf("\n");
}
printf("\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100010];
map<int, int> cnt;
int main() {
cin >> n;
for (int i = 0; i < n; i++) scanf("%d", a + i);
if (n == 1) {
cout << -1;
} else {
sort(a, a + n);
for (int i = 0; i < n - 1; i++) cnt[a[i + 1] - a[i]]++;
int t = 0;
for (__typeof((cnt).begin()) it = (cnt).begin(); it != (cnt).end(); it++)
t++;
if (t == 1) {
int k = a[1] - a[0];
if (k != 0) {
if (n == 2 && k % 2 == 0) {
cout << 3 << endl;
cout << a[0] - k << " " << a[0] + k / 2 << " " << a[n - 1] + k
<< endl;
} else {
cout << 2 << endl;
cout << a[0] - k << " " << a[n - 1] + k << endl;
}
} else {
cout << 1 << endl;
cout << a[0] << endl;
}
} else if (t > 2)
cout << 0 << endl;
else {
int p = -1;
int u = -1;
int k = -1;
for (__typeof((cnt).begin()) it = (cnt).begin(); it != (cnt).end();
it++) {
if ((*it).second == 1) {
if (p == -1)
p = (*it).first;
else
u = (*it).first;
} else
k = (*it).first;
}
vector<int> v;
for (int i = 0; i < n - 1; i++) {
if (a[i + 1] - a[i] == p && p != -1) {
if (u != -1 && a[i] + 2 * u == a[i + 1])
v.push_back(a[i] + u);
else if (k != -1 && a[i] + 2 * k == a[i + 1])
v.push_back(a[i] + k);
}
if (a[i + 1] - a[i] == u && u != -1) {
if (p != -1 && a[i] + 2 * p == a[i + 1])
v.push_back(a[i] + p);
else if (k != -1 && a[i] + 2 * k == a[i + 1])
v.push_back(a[i] + k);
}
}
cout << v.size() << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define vi vector<int>
#define MAX_N 200005
#define i197 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> Pi;
typedef pair<ll,ll> P1;
typedef pair<int,P1> P2;
const int inf=1000000000;
P1 xy[MAX_N]={};
int main() {
Pi hw[6];
int h1,w1;
rep(i,6){
cin>>h1>>w1;
if(h1>w1)swap(h1,w1);
hw[i].first=h1;
hw[i].second=w1;
}
sort(hw,hw+6);
bool flg=true;
if(hw[0]!=hw[1]||hw[2]!=hw[3])flg=false;
if(hw[4]!=hw[5]||hw[0].first!=hw[2].first)flg=false;
if(hw[0].second!=hw[4].first||hw[2].second!=hw[4].second)flg=false;
if(flg){
cout<<"yes"<<endl;
}else{
cout<<"no"<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double w,h,x,y,k=0,area;
cin>>w>>h>>x>>y;
area=w/2*h;
if(x==w/2 && y==h/2) k=1;
cout<<area<<" "<<k<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000000;
bool vis[N] = {};
int prime[N], num = 0;
const int S = 10;
inline long long mult_mod(long long a, long long b, long long c) {
a %= c;
b %= c;
long long ret = 0;
while (b) {
if (b & 1) {
ret += a;
if (ret >= c) ret %= c;
}
a <<= 1;
if (a >= c) a %= c;
b >>= 1;
}
return ret;
}
inline long long pow_mod(long long x, long long n, long long m) {
if (n == 1) return x % m;
x %= m;
long long tmp = x;
long long ret = 1;
while (n) {
if (n & 1) ret = mult_mod(ret, tmp, m);
tmp = mult_mod(tmp, tmp, m);
n >>= 1;
}
return ret;
}
bool check(long long a, long long n, long long x, long long t) {
long long ret = pow_mod(a, x, n);
long long last = ret;
for (int i = 1; i <= t; i++) {
ret = mult_mod(ret, ret, n);
if (ret == 1 && last != 1 && last != n - 1) return true;
last = ret;
}
if (ret != 1) return true;
return false;
}
bool Miller_Rabin(long long n) {
if (n < 2) return false;
if (n == 2) return true;
if ((n & 1) == 0) return false;
long long x = n - 1;
long long t = 0;
while ((x & 1) == 0) {
x >>= 1;
t++;
}
for (int i = 0; i < 3; i++) {
long long a = rand() % (n - 1) + 1;
if (check(a, n, x, t)) return false;
}
return true;
}
inline long long gcd(long long a, long long b) {
while (b ^= a ^= b ^= a %= b)
;
return a;
}
int n, cnt[N];
long long a[N];
int main() {
for (int i = 2; i < N; i++) {
if (!vis[i]) prime[++num] = i;
for (int j = 1; j <= num && prime[j] * i < N; j++) {
vis[prime[j] * i] = true;
if (i % prime[j] == 0) break;
}
}
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
for (int j = 1; j <= num && prime[j] <= a[i]; j++) {
while (a[i] % prime[j] == 0) a[i] /= prime[j], cnt[prime[j]]++;
}
}
long long ans = 1;
for (int i = 2; i < N; i++)
if (cnt[i] > 0) (ans *= cnt[i] + 1) %= 998244353LL;
for (int i = 1; i <= n; i++) {
if (a[i] == 1) continue;
if (Miller_Rabin(a[i])) {
long long cnt = 0, r = a[i];
for (int j = i; j <= n; j++)
while (a[j] % r == 0) {
a[j] /= r;
cnt++;
}
(ans *= cnt + 1) %= 998244353LL;
continue;
}
long long r = 1;
for (int j = i + 1; j <= n; j++) {
if (r == 1)
r = gcd(a[i], a[j]);
else
r = gcd(a[i], a[j]) == 1 ? r : min(r, gcd(a[i], a[j]));
}
if (r == 1) {
long long s = sqrt(a[i]) + 0.01;
if (s * s == a[i])
(ans *= 3) %= 998244353LL;
else
(ans *= 4) %= 998244353LL;
} else {
long long cnt = 0, cnt1 = 0, rr = a[i] / r, s = sqrt(a[i]) + 0.01;
if (s * s == a[i]) {
r = s;
for (int j = i; j <= n; j++) {
while (a[j] % r == 0) {
a[j] /= r;
cnt++;
}
}
} else {
if (rr == 1) {
for (int j = i; j <= n; j++) {
while (a[j] % r == 0) {
a[j] /= r;
cnt++, cnt1++;
}
}
} else {
for (int j = i; j <= n; j++) {
while (a[j] % r == 0) {
a[j] /= r;
cnt++;
}
while (a[j] % rr == 0) {
a[j] /= rr;
cnt1++;
}
}
}
}
(ans *= cnt + 1) %= 998244353LL;
(ans *= cnt1 + 1) %= 998244353LL;
}
}
cout << ans << endl;
cout.flush();
return 0;
}
| 4 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
int main(){
string s; cin>>s;
string res;
for(char c:s){
if(c!='B') res+=c;
else if(!res.empty()) res.pop_back();
}
cout<<res<<'\n';
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int ve_m(int x, int y, int xx, int yy) {
return (x * 1LL * yy) - (xx * 1LL * y);
}
int n, m;
vector<pair<int, int> > p;
void Init() {
scanf("%d%d", &n, &m);
if (m == 3) {
if (n > 4) {
printf("-1\n");
exit(0);
}
printf("0 0\n");
printf("0 3\n");
printf("3 0\n");
if (n == 4) {
printf("1 1\n");
}
exit(0);
}
}
void solve() {
int x, y, xx, yy, kx, ky;
printf("0 0\n");
printf("100000 0\n");
printf("-1 -2\n");
printf("100001 -2\n");
p.push_back(make_pair(0, 0));
p.push_back(make_pair(100000, 0));
p.push_back(make_pair(-1, -2));
p.push_back(make_pair(100001, -2));
x = -1;
y = -2;
xx = 100001;
yy = -2;
n -= 4;
m -= 4;
kx = 2;
ky = 3;
while (m != n) {
x -= kx;
y -= ky;
printf("%d %d\n", x, y);
p.push_back(make_pair(x, y));
n--;
if (n == m) break;
xx += kx;
yy -= ky;
printf("%d %d\n", xx, yy);
p.push_back(make_pair(xx, yy));
kx++;
ky++;
n--;
}
kx = 2;
for (int i = 0; i < m; i++) {
x += kx;
y--;
bool flag = false;
for (int k = 0; k < p.size(); k++) {
for (int j = 0; j < p.size(); j++) {
if (k != j) {
if (ve_m(p[k].first - x, p[k].second - y, p[j].first - x,
p[j].second - y) == 0) {
flag = true;
}
}
}
}
if (!flag)
printf("%d %d\n", x, y);
else {
i--;
}
kx++;
}
}
int main() {
Init();
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
int A[1005], B[1005];
int book[1005];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> A[i];
for (int i = 1; i <= n; i++) cin >> B[i];
int x, y, z;
for (int i = 1; i <= n; i++) {
if (!book[A[i]])
book[A[i]] = i;
else
x = i, y = book[A[i]];
}
for (int i = 1; i <= n; i++)
if (!book[i]) z = i;
int t = A[x];
A[x] = z;
int p = 0;
for (int i = 1; i <= n; i++) {
if (A[i] != B[i]) p++;
}
if (p != 1) {
A[x] = t;
A[y] = z;
for (int i = 1; i <= n; i++) cout << A[i] << " ";
} else {
for (int i = 1; i <= n; i++) cout << A[i] << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool test(long long k, long long x, long long n, long long m, long long p1,
long long l1, long long r1, long long p2, long long l2,
long long r2) {
if (p1 * 2 + l1 + r1 > n) return false;
if (p2 * 2 + l2 + r2 > m) return false;
for (long long i = 0; i < k - 2; i++) {
long long p3 = p1 + p2 + (r1 * l2);
long long l3 = l1;
long long r3 = r2;
l1 = l2;
r1 = r2;
p1 = p2;
l2 = l3;
r2 = r3;
p2 = p3;
if (p2 > x) return false;
}
return p2 == x;
}
string build(long long n, long long p, long long l, long long r) {
string s(n, 'X');
if (l) s[0] = 'C';
if (r) s[n - 1] = 'A';
for (long long i = 0; i < p; i++) {
s[l + i * 2] = 'A';
s[l + i * 2 + 1] = 'C';
}
return s;
}
signed main() {
ios::sync_with_stdio(false);
long long k, x, n, m;
cin >> k >> x >> n >> m;
for (long long p1 = 0; p1 <= n / 2; p1++) {
for (long long l1 = 0; l1 <= 1; l1++) {
for (long long r1 = 0; r1 <= 1; r1++) {
for (long long p2 = 0; p2 <= m / 2; p2++) {
for (long long l2 = 0; l2 <= 1; l2++) {
for (long long r2 = 0; r2 <= 1; r2++) {
if (test(k, x, n, m, p1, l1, r1, p2, l2, r2)) {
string s1 = build(n, p1, l1, r1);
string s2 = build(m, p2, l2, r2);
cout << s1 << "\n" << s2 << "\n";
return 0;
}
}
}
}
}
}
}
cout << "Happy new year!\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
long long int arr[n];
for (long long int i = 0; i <= n - 1; i++) {
cin >> arr[i];
}
long long int g = 0;
for (long long int i = 0; i <= n - 2; i++) {
if (arr[i] < arr[i + 1]) {
g = g + ((arr[i + 1] - arr[i]) * (n - arr[i + 1] + 1));
} else {
g = g + (arr[i + 1] * (arr[i] - arr[i + 1]));
}
}
g = g + (arr[0] * (n - arr[0] + 1));
cout << g << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;
int main(void) {
ll n;
cin >> n;
vector<ll> a(n);
REP(i, n) cin >> a[i], a[i]--;
if(n == 2) {
cout << -1 << endl;
return 0;
}
map<ll, ll> mp;
REP(i, n) mp[a[i]]++;
set<ll> st;
REP(i, n) st.insert(i);
vector<ll> ans;
auto add = [&](ll x) {
ans.push_back(x);
st.erase(x);
mp[a[x]]--;
if(mp[a[x]] == 0) mp.erase(a[x]);
};
REP(i, n) {
ll mi = st.size() ? *st.begin() : INF;
ll mi2 = st.size()>=2 ? *(next(st.begin())) : INF;
// dump(ans, st, mp);
if(mp.size() == 2) {
auto p1 = *mp.begin();
auto p2 = *(next(mp.begin()));
if(st.find(p1.first) != st.end() && p2.second == 1) {
add(p1.first);
continue;
}
if(st.find(p2.first) != st.end() && p1.second == 1) {
add(p2.first);
continue;
}
}
if(st.size() == 3) {
vector<ll> v;
for(auto j: st) v.push_back(j);
bool f0 = true, f1 = true, f2 = true;
if(a[v[1]] == v[2] && a[v[2]] == v[1]) f0 = false;
if(i && a[ans.back()] == v[0]) f0 = false;
if(a[v[0]] == v[2] && a[v[2]] == v[0]) f1 = false;
if(i && a[ans.back()] == v[1]) f1 = false;
if(a[v[0]] == v[1] && a[v[1]] == v[0]) f2 = false;
if(i && a[ans.back()] == v[2]) f2 = false;
if(f0) add(v[0]);
else if(f1) add(v[1]);
else if(f2) add(v[2]);
else {
cout << -1 << endl;
return 0;
}
}
else if(i == 0) add(0);
else if(a[ans.back()] == mi) add(mi2);
else add(mi);
}
REP(i, n) cout << ans[i]+1 << (i==n-1 ? '\n': ' ');
cout << flush;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main(){
int n;
scanf("%d", &n);
LL ans = 0;
int rem = 0;
for(int i=0;i<n;i++){
int a;
scanf("%d", &a);
if(rem == 1 && a != 0) a--, ans++;
ans += a/2;
rem = a%2;
}
printf("%lld\n", ans);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void IO() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long binary_exponention(long long a, long long b) {
if (b == 0) return 1;
long long res = binary_exponention(a, b / 2);
if (b % 2) return (res * res % 1000000007) * a % 1000000007;
return res * res % 1000000007;
}
inline void read(int a[], int n, int which = 0) {
if (which)
for (int i = 1; i <= n; i++) cin >> a[i];
else
for (int i = 0; i < n; i++) cin >> a[i];
}
inline void write(int a[], int n, int which = 0) {
if (which == 0)
for (int i = 0; i < n; i++) cout << a[i] << " ";
else
for (int i = 1; i <= n; i++) cout << a[i] << " ";
}
int dir8i[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dir4i[4] = {-1, 0, 1, 0};
int dir4j[4] = {0, -1, 0, 1};
void solve() {
int n, m;
cin >> n >> m;
int dp[n + 1][m + 1];
string a, b;
cin >> a >> b;
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (a[i - 1] == b[j - 1])
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 2);
else
dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]) - 1);
}
int mx = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++) mx = max(mx, dp[i][j]);
cout << mx << "\n";
}
int main() {
IO();
solve();
}
| 2 |
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
int main()
{
constexpr int n = 1001;
int v, d;
while (std::cin >> v >> d) {
std::vector<bool> exist(n, false);
int prev = 1, current = 1;
for (auto i = 0; i < v; ++i) {
current = (current + prev) % n;
prev = (current - prev + n) % n;
exist[current] = true;
}
std::queue<int> queue;
int count = 0;
for (auto i = 0; i < n; ++i) if (exist[i]) {
while (!queue.empty() && i - queue.front() >= d) queue.pop();
if (queue.empty()) ++count;
queue.push(i);
}
std::cout << count << '\n';
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, c = 0, ans = 0;
string s;
cin >> s;
n = s.size();
vector<int> v;
for (i = 0; i <= (n - 4); i++) {
if (s[i] == 'b' && s[i + 1] == 'e' && s[i + 2] == 'a' && s[i + 3] == 'r') {
v.push_back(i);
c++;
}
}
for (i = 0; i <= (n - 4); i++) {
if (lower_bound(v.begin(), v.end(), i) != v.end()) {
ans = ans + n - v[(lower_bound(v.begin(), v.end(), i) - v.begin())] - 3;
}
}
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int kol, f[100010], l[100010];
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= k; i++) {
int x;
cin >> x;
l[x] = i;
if (f[x] == 0) f[x] = i;
}
for (int i = 1; i <= n; i++) {
if (!f[i]) kol++;
if (i > 1) {
if (!f[i])
kol++;
else if (!f[i - 1] || f[i] > l[i - 1])
kol++;
}
if (i < n) {
if (!f[i])
kol++;
else if (!f[i + 1] || f[i] > l[i + 1])
kol++;
}
}
cout << kol;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
const long long mod = 1e9 + 7;
int dp[301][N + 1];
struct data {
int one, two, thr;
};
vector<data> v;
bool cmp(data A, data B) { return A.one < B.one; }
int Bit[N + 2];
void Upd(int x, int n, int vl) {
for (; x <= N; x += (x & -x)) Bit[x] += vl;
}
int Sum(int x) {
int sum = 0;
for (; x > 0; x -= (x & -x)) sum += Bit[x];
return sum;
}
int Par[N + 2], Lev[N + 2], Sp[N + 2][22];
vector<int> Adj[N + 2];
int Sub[N + 2];
int Ent[N + 2], Ext[N + 2], Tim, Anc[N + 2];
void Reckon(int n, int p, int h) {
Par[n] = p;
Lev[n] = h;
Sub[n] = 1;
for (auto &x : Adj[n]) {
if (x == p) continue;
Reckon(x, n, h + 1);
Sub[n] += Sub[x];
if (Sub[x] >= Sub[Adj[n][0]]) swap(x, Adj[n][0]);
}
}
void Build_Sparse(int n) {
memset(Sp, -1, sizeof(Sp));
for (int i = 1; i <= n; i++) Sp[i][0] = Par[i];
for (int j = 1; j <= 20; j++) {
for (int i = 1; i <= n; i++) {
if (Sp[i][j - 1] == -1) continue;
Sp[i][j] = Sp[Sp[i][j - 1]][j - 1];
}
}
}
int Lca(int u, int v, int &d) {
if (Lev[u] < Lev[v]) swap(u, v);
d = 0;
for (int i = 20; i >= 0; i--) {
if (Lev[u] - (1 << i) >= Lev[v]) u = Sp[u][i], d += (1 << i);
}
if (u == v) return u;
for (int i = 20; i >= 0; i--) {
if (Sp[u][i] == -1 || Sp[v][i] == -1) continue;
if (Sp[u][i] == Sp[v][i]) continue;
u = Sp[u][i];
v = Sp[v][i];
d += 2 * (1 << i);
}
d += 2;
return Par[u];
}
void hld(int n, int p) {
Ent[n] = ++Tim;
for (auto x : Adj[n]) {
if (x == p) continue;
if (x == Adj[n][0])
Anc[x] = Anc[n];
else
Anc[x] = x;
hld(x, n);
}
Ext[n] = Tim;
}
int hld_query(int u, int lc) {
int ret = 0;
while (Anc[u] != Anc[lc]) {
int v = Anc[u];
ret += Sum(Ent[u]) - Sum(Ent[v] - 1);
u = Par[v];
}
return ret + Sum(Ent[u]) - Sum(Ent[lc] - 1);
}
int m;
long long rec(int i, int j) {
if (i > m) return 0;
if (j >= v.size()) return 1;
if (dp[i][j] != -1) return dp[i][j];
long long now = 0;
now = (now + rec(i + 1, j + 1)) % mod;
now = (now + (i - v[j].thr) * rec(i, j + 1)) % mod;
return dp[i][j] = now;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
Adj[u].push_back(v);
Adj[v].push_back(u);
}
Reckon(1, -1, 0);
Build_Sparse(n);
Anc[1] = 1;
hld(1, -1);
while (q--) {
int k, r;
cin >> k >> m >> r;
int d;
v.clear();
for (int i = 1; i <= k; i++) {
int x;
cin >> x;
Lca(x, r, d);
Upd(Ent[x], n, 1);
v.push_back({d, x, 0});
}
sort(v.begin(), v.end(), cmp);
int id = 0;
for (auto x : v) {
int lc = Lca(x.two, r, d);
int ans = hld_query(x.two, lc);
ans += hld_query(r, lc);
ans -= (Sum(Ent[lc]) - Sum(Ent[lc] - 1));
v[id++].thr = ans - 1;
}
for (int i = 1; i <= m; i++)
for (int j = 0; j < v.size(); j++) dp[i][j] = -1;
int pr = rec(1, 1);
cout << pr << "\n";
for (auto x : v) Upd(Ent[x.two], n, -1);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
bool pt(int n) {
return (n&(-n)) == n;
}
void eg(int a, int b) {
cout << a << " " << b << '\n';
}
int lso(int n) {
return n&(-n);
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
int n;
cin >> n;
if(pt(n)) {
cout << "No\n";return 0;
}
if(n%4 != 3 && n <= 4) {
cout << "No\n";return 0;
}
cout << "Yes\n";
for(int i=0;i<2;i++) {
eg(i+1,i+2);
}
eg(3,n+1);
for(int i=0;i<2;i++) {
eg(n+i+1,n+i+2);
}
for(int i=4;i<n;i+=2) {
eg(i,i+1);
eg(i+1,1);
eg(1,i+n);
eg(i+n,i+n+1);
}
if(n%2 == 0) {
int a = lso(n),b = (n-lso(n))+1;
eg(n,a+n);
eg(b,n+n);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const long long LLINF = 9223372036854775807LL;
template <int MOD>
struct Num {
int x;
Num() : x(0) {}
explicit Num(int x, bool skip_check = false) {
if (skip_check)
this->x = x;
else {
if (x >= 0 && x < MOD)
this->x = x;
else {
this->x = x % MOD;
if (this->x < 0) this->x += MOD;
};
}
}
Num<MOD> operator+(const Num<MOD>& oth) const {
int rem = this->x + oth.x;
if (rem >= MOD) rem -= MOD;
return Num(rem, true);
}
Num<MOD> operator-(const Num<MOD>& oth) const {
int rem = this->x - oth.x;
if (rem < 0) rem += MOD;
return Num(rem, true);
}
Num<MOD> operator*(const Num<MOD>& oth) const {
int rem = (long long)this->x * oth.x % MOD;
return Num(rem, true);
}
Num<MOD> to_pow(int n) const {
Num<MOD> res(1);
Num<MOD> cur = *this;
while (n) {
if (n & 1) res = res * cur;
cur = cur * cur;
n >>= 1;
}
return res;
}
Num<MOD> inv_prime() const { return this->to_pow(MOD - 2); }
Num<MOD> operator/(const Num<MOD>& oth) const {
return *this * oth.inv_prime();
}
bool operator==(const Num<MOD>& oth) const { return x == oth.x; }
bool operator!=(const Num<MOD>& oth) const { return !(x == oth); }
};
void solve() {
int x;
scanf("%d", &x);
string s;
cin >> s;
Num<1000000007> total_len(int((s).size()));
int l = 0;
while (true) {
if (l == x) break;
l += 1;
int cur_cnt = int(s[l - 1] - '0') - 1;
Num<1000000007> cur_l = total_len - Num<1000000007>(l);
total_len = total_len + cur_l * Num<1000000007>(cur_cnt);
int slen = int((s).size());
for (int it = 0; it < cur_cnt; ++it)
for (int pos = l; pos < slen; ++pos) {
if (int((s).size()) > x) break;
s += s[pos];
}
}
printf("%d\n", total_len.x);
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000010;
const double INF = 1e10;
int n;
int cmp(double x) {
if (abs(x) < 1e-10) return 0;
if (x > 0) return 1;
return -1;
}
double sqr(double a) { return a * a; }
struct Point {
double x, y;
inline Point operator-(const Point &t) {
Point ret;
ret.x = x - t.x;
ret.y = y - t.y;
return ret;
}
inline Point operator+(const Point &t) {
Point ret;
ret.x = x + t.x;
ret.y = y + t.y;
return ret;
}
inline double det(const Point &t) { return x * t.y - t.x * y; }
inline double dist(Point &t) { return sqrt(sqr(x - t.x) + sqr(y - t.y)); }
} d[MAXN], D;
bool cm(const Point &a, const Point &b) {
double ka = (a.x == 0 ? INF : (double)a.y / a.x);
double kb = (b.x == 0 ? INF : (double)b.y / b.x);
return ka < kb;
}
int convex[MAXN], cTotal;
void get_convex_hull() {
sort(d, d + n, cm);
int Total = 0, tmp;
for (int i = 0; i < n; ++i) {
while ((Total > 1) && ((d[convex[Total - 1]] - d[convex[Total - 2]])
.det(d[i] - d[convex[Total - 1]]) <= 0))
Total--;
convex[Total++] = i;
}
cTotal = Total;
}
int main() {
scanf("%d%lf%lf", &n, &D.x, &D.y);
double xmax = 0, ymax = 0;
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &d[i].x, &d[i].y);
xmax = max(d[i].x, xmax);
ymax = max(d[i].y, ymax);
}
d[n].x = xmax;
d[n].y = 0;
d[n + 1].x = 0;
d[n + 1].y = ymax;
n += 2;
get_convex_hull();
double k = D.y / D.x;
int pos;
double mk;
for (pos = 1; pos < cTotal; pos++) {
if (d[convex[pos]].y / d[convex[pos]].x > k) break;
}
if (pos == cTotal) pos--;
double l = 0, r = 1000010;
Point K;
while (l <= r) {
double mid = (l + r) / 2;
K.x = D.x / mid;
K.y = D.y / mid;
double dot = (K - d[convex[pos - 1]]).det(d[convex[pos]] - K);
if (cmp(dot) < 0)
r = mid;
else if (cmp(mid - l) == 0 && cmp(mid - r) == 0) {
printf("%.10lf", mid);
break;
} else
l = mid;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using u32 = unsigned int;
using u64 = unsigned long long;
char buf[10];
void NO() {
puts("Impossible");
exit(0);
}
ll update(ll x, ll c) {
if (x == LLONG_MAX || x == LLONG_MIN) return x;
return x + c;
}
int main() {
int n;
scanf("%d", &n);
ll max_r = LLONG_MAX;
ll min_r = LLONG_MIN;
for (int i = (0); i < (n); ++i) {
ll c_i, d_i;
scanf("%lld%lld", &c_i, &d_i);
;
if (d_i == 2) {
if (min_r >= 1900) NO();
if (max_r >= 1900) max_r = 1899;
} else if (d_i == 1) {
if (max_r < 1900) NO();
if (min_r < 1900) min_r = 1900;
}
max_r = update(max_r, c_i);
min_r = update(min_r, c_i);
}
if (max_r == LLONG_MAX)
puts("Infinity");
else
printf("%lld", max_r);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char a[500001 + 10], b[500001 + 10], s[(500001 << 1) + 10];
int n, m;
struct BIT {
long long c[500001 << 1];
long long query(int x) {
if (x <= 0) return 0;
long long ans = 0;
for (int i = x; i; i -= (i & -i)) ans += c[i];
return ans;
}
void add(int x, long long p) {
if (x <= 0) x = 1;
for (int i = x; i <= 1000000; i += (i & -i)) c[i] += p;
}
} b1, b2;
int za[500001 * 3], zb[500001 * 3];
char mod[500001 * 3];
void zfunc(char *s, int n, int *z) {
int l = 0, r = 0;
z[1] = n;
int pt;
for (int i = 2; i <= n; ++i) {
if (r < i) {
for (pt = i; pt <= n && s[pt] == s[pt - i + 1]; ++pt)
;
--pt;
z[i] = pt - i + 1;
if (z[i]) l = i, r = pt;
} else {
int b = r - i + 1, i_ = i - l + 1;
if (z[i_] < b)
z[i] = z[i_];
else {
for (pt = r + 1; pt <= n && s[pt] == s[pt - i + 1]; ++pt)
;
--pt;
z[i] = pt - i + 1;
l = i, r = pt;
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s%s%s", a + 1, b + 1, s + 1);
memcpy(mod + 1, s + 1, m - 1);
mod[m] = '$';
memcpy(mod + m + 1, a + 1, n);
zfunc(mod, n + m, za);
for (int i = 1; i <= n; ++i) za[i] = za[i + m];
reverse(s + 1, s + 1 + m);
reverse(b + 1, b + 1 + n);
memcpy(mod + 1, s + 1, m - 1);
mod[m] = '$';
memcpy(mod + m + 1, b + 1, n);
zfunc(mod, n + m, zb);
for (int i = 1; i <= n; ++i) zb[i] = zb[i + m];
reverse(zb + 1, zb + 1 + n);
int cur = 1;
long long ans = 0;
for (int i = 1; i <= n; ++i) {
while (cur <= min(i + m - 2, n))
b1.add(m - 1 - zb[cur], 1), b2.add(m - 1 - zb[cur], zb[cur]), ++cur;
ans += 1ll * b1.query(za[i]) * (za[i] - m + 1) + b2.query(za[i]);
b1.add(m - 1 - zb[i], -1), b2.add(m - 1 - zb[i], -zb[i]);
}
printf("%lld\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct node {
char x;
vector<node*> ch;
int ans[103];
int q;
};
char c[2005][505];
int pref[25][25];
node root;
int k;
void add_string(const char* c) {
int k = strlen(c);
node* cur = &root;
int i;
for (i = 0; i < k; i++) {
cur->q++;
if (i < k - 1) {
int sz = cur->ch.size();
int j;
for (j = 0; j < sz; j++) {
if (cur->ch[j]->x == c[i + 1]) {
break;
}
}
if (j == sz) {
node* tmp = new node;
tmp->x = c[i + 1];
tmp->q = 0;
cur->ch.push_back(tmp);
}
cur = cur->ch[j];
}
}
}
void solve(node* cur, node* prev_child) {
int sz = cur->ch.size();
int i;
node* lc = NULL;
for (i = 0; i < sz; i++) {
solve(cur->ch[i], (i > 0) ? cur->ch[i - 1] : NULL);
lc = cur->ch[i];
}
for (i = 0; i <= k; i++) {
cur->ans[i] = 0;
int j;
for (j = 0; j <= i; j++) {
int last_ans = 0;
if (prev_child) last_ans = prev_child->ans[i - j];
int qq = min(cur->q, j);
int ex = 0;
int ss = 0;
if (cur->x != '$' && cur->x != '#') {
ss = qq * (qq - 1) / 2;
}
if (lc) {
ex += lc->ans[j];
}
cur->ans[i] = max(cur->ans[i], last_ans + ss + ex);
}
}
}
int main() {
int n;
scanf("%d%d", &n, &k);
int i;
root.x = '$';
root.q = 0;
for (i = 0; i < n; i++) {
scanf("%s", c[i] + 1);
c[i][0] = '$';
int k = strlen(c[i]);
c[i][k] = '#';
c[i][k + 1] = '\0';
add_string(c[i]);
}
int j;
solve(&root, NULL);
int ans = root.ans[k];
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char c[5005];
int n, i, j, k, l[26], o, m, a[26][5005], ans, b[26];
int main() {
scanf("%s", c);
n = strlen(c);
for (i = 0; i < n; i++) a[c[i] - 'a'][l[c[i] - 'a']++] = i;
for (i = 0; i < 26; i++) {
for (j = m = 0; j < n; j++) {
memset(b, 0, sizeof(b));
for (k = 0; k < l[i]; k++) {
o = a[i][k] + j;
if (o >= n) o -= n;
b[c[o] - 'a']++;
}
for (k = o = 0; k < 26; k++) o += b[k] == 1;
m = max(m, o);
}
ans += m;
}
printf("%.10lf\n", (double)ans / n);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
static int u[100005];
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int r, c;
scanf("%d %d", &r, &c);
u[r] = 1;
u[c + 2 * n] = 1;
u[r + n] = 1;
u[c + 3 * n] = 1;
}
int sum = 0;
for (int i = 1; i <= 4 * n; i++) {
if (i == 1 || i == n || i == n + 1 || i == 2 * n || i == 2 * n + 1 ||
i == 3 * n || i == 3 * n + 1 || i == 4 * n)
continue;
if (!u[i]) {
sum++;
if (i < n) {
if (n & 1 && i == n / 2 + 1) u[i + 3 * n] = 1;
u[i + n] = 1;
}
if (i < 2 * n) {
u[i + 2 * n] = 1;
}
if (i > 2 * n && i < 3 * n) u[i + n] = 1;
}
}
printf("%d\n", sum);
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
const int mxv = 7009;
bitset<mxv> mu, a[100009], mask[71];
int p[7009];
vector<int> g[7009];
void pre() {
mu.reset();
mu[1] = 1;
int cnt = 0;
for (int i = 2; i <= (7000); ++i) {
if (!p[i]) {
for (int j = 2 * i; j <= 7000; j += i) p[j] = i;
mu[i] = 1;
cnt++;
} else {
if (mu[i / p[i]] == 0 || (i / p[i]) % p[i] == 0)
mu[i] = 0;
else
mu[i] = 1, cnt++;
}
}
for (int i = 1; i <= (7000); ++i) {
for (int j = i; j <= 7000; j += i) g[j].push_back(i);
}
for (int i = 1; i <= (70); ++i) {
for (int j = i; j <= 7000; j += i) mask[i][j] = mu[j / i];
}
}
void solve() {}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
pre();
int n;
cin >> n;
int q;
cin >> q;
for (int i = 0; i < (q); ++i) {
int t, x, v;
cin >> t >> x >> v;
if (t == 1) {
a[x].reset();
for (auto& j : g[v]) a[x][j] = 1;
} else if (t == 2) {
int z;
cin >> z;
a[x] = a[v] ^ a[z];
} else if (t == 3) {
int z;
cin >> z;
a[x] = a[v] & a[z];
} else {
if (v > 70) {
bool ans = 0;
for (int j = 1; j <= (7000); ++j) {
if (j * v > 7000) break;
ans ^= mu[j] & a[x][j * v];
}
cout << ans;
} else {
cout << (mask[v] & a[x]).count() % 2;
}
}
}
return 0;
}
| 6 |
#include "bits/stdc++.h"
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<iomanip>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<complex>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;
#define WHATS(var)//cout<<__LINE__<<' '<<#var<<"="<<var<<endl;
template<class S, class T> ostream& operator <<(ostream &os, const pair<S, T> v){
os << "( " << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<T> &v){
for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<vector<T>> &v){
for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<set<T>> &v){
for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const set<T> &v){
int i=0;
for(auto it:v){
if(i > 0){os << ' ';}
os << it;
i++;
}
return os;
}
using ll =long long ;
ll mod;
void solve(vector<vector<int>>&cango){
int tt=cango.size();
for(int i=0;i<tt;++i){
for(int j=0;j<tt;++j){
for(int k=0;k<tt;++k){
cango[j][k]|=cango[j][i]&cango[i][k];
}
}
}
}
int main() {
ios::sync_with_stdio(false);
int N;cin>>N;
vector<int>v(N);
for(int i=0;i<N;++i){
char ch;cin>>ch;
v[i]=ch=='T';
}
int now=((!v[0])||v[1]);
for(int i=2;i<N;++i){
now=((!now)||v[i]);
}
if(now)cout<<'T'<<endl;
else cout<<'F'<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
typedef vector<vector<int> > VVI;
inline int mult(int x, int y) { return ((long long)x * y) % 1000000007; }
int power(int x, int y) {
int ret = 1;
while (y) {
if (y & 1) ret = mult(ret, x);
x = mult(x, x);
y = y / 2;
}
return ret;
}
int gcd(int a, int b) {
if (b) return gcd(b, a % b);
return a;
}
vector<int> prime;
void generateprime(int n) {
int i, j;
vector<int> num(n + 5);
num[1] = 1;
for (i = 4; i < n; i = i + 2) num[i] = 1;
for (i = 3; i < n; i++) {
if (num[i] == 0) {
for (j = 3 * i; j < n; j = j + 2 * i) num[j] = 1;
}
}
num[0] = 0;
for (i = 2; i < n; i++) {
if (num[i] == 0) prime.push_back(i);
num.clear();
}
}
int main() {
long long mx, flag, flag1, n, m, w[120][120], mat[120][120], a[120], b[120],
k;
int i, j;
mx = INT_MIN;
flag = 0;
flag1 = 0;
k = 0;
scanf("%lld", &n);
scanf("%lld", &m);
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
scanf("%lld", &w[i][j]);
mx = max(w[i][j], mx);
}
}
a[0] = 0;
for (i = 0; i < m; i++) {
b[i] = w[0][i];
}
for (i = 0; i < n; i++) {
a[i] = w[i][0] - b[0];
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
mat[i][j] = abs(a[i] + b[j] - w[i][j]);
if (mat[i][j] != 0 && flag == 0) flag = 1;
}
}
if (flag == 0)
k = mx + 1;
else {
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
k = gcd(mat[i][j], k);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (k <= w[i][j]) {
flag1 = 1;
break;
}
}
if (flag1 == 1) break;
}
if (flag1 == 1) {
printf("NO\n");
return 0;
}
}
for (i = 0; i < n; i++) {
if (a[i] < 0) a[i] = a[i] + k;
}
printf("YES\n");
printf("%lld\n", k);
for (i = 0; i < n; i++) {
printf("%lld ", a[i]);
}
printf("\n");
for (i = 0; i < m; i++) {
printf("%lld ", b[i]);
}
printf("\n");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, c, a[200005], b[200005];
long long dp[200005][3];
int main() {
cin >> n >> c;
for (int i = 1; i <= n - 1; i++) cin >> a[i];
for (int i = 1; i <= n - 1; i++) cin >> b[i];
dp[1][1] = c;
for (int i = 2; i <= n; i++) {
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + a[i - 1];
dp[i][1] = min(dp[i - 1][0] + c, dp[i - 1][1]) + b[i - 1];
}
for (int i = 1; i <= n; i++) {
cout << min(dp[i][0], dp[i][1]) << " ";
}
cout << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
using u64 = uint64_t;
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
int bin(long int a[], long int low, long int high, long d, long day[]) {
if (low > high) return -1;
long mid = (low + high) / 2;
if (a[mid] > (a[low] + d) && (a[mid - 1] <= (a[low] + d) || day[mid - 1]) &&
day[mid] == 0)
return mid;
else if (a[mid] > (a[low] + d) && day[mid] == 0)
return bin(a, low, mid - 1, d, day);
else
return bin(a, mid + 1, high, d - a[mid + 1] + a[low], day);
}
long long power(long long a, long long b) {
if (b == 0) return 1;
if (b % 2 == 0) return (power(((a * a)), b / 2));
return (((a) * (power(((a * a)), b / 2))));
}
bool cmp(int a, int b) { return a > b; }
bool sortbysecdesc(const pair<int, int> &a, const pair<int, int> &b) {
return a.first > b.first;
}
u64 binpower(u64 base, u64 e, u64 m) {
u64 ans = 1;
base %= m;
while (e) {
if (e & 1) {
ans = ans * base % m;
}
base = base * base % m;
e >>= 1;
}
return ans;
}
int table[100000][5][20], a[100000][5], n, lg[100001], val[100000], m, k;
void build() {
int i, j, k, k1;
lg[1] = 0;
for (i = 2; i <= 100000; i++) lg[i] = lg[i / 2] + 1;
for (long long int i = 0; i < (n); i++)
for (long long int j = 0; j < (m); j++) table[i][j][0] = a[i][j];
for (j = 1; j < 20; j++) {
k1 = 1 << j;
for (i = 0; i + k1 <= n; i++) {
for (long long int k = 0; k < (m); k++) {
table[i][k][j] = max(table[i][k][j - 1], table[i + k1 / 2][k][j - 1]);
}
}
}
}
bool query(int l, int r) {
int a = lg[r - l + 1], sum = 0, i, b;
for (long long int i = 0; i < (m); i++) {
b = max(table[l][i][a], table[r - (1 << a) + 1][i][a]);
sum += b;
}
if (sum <= k)
return true;
else
return false;
}
void query(int l, int r, int b[]) {
int a = lg[r - l + 1], i;
for (long long int i = 0; i < (m); i++)
b[i] = max(table[l][i][a], table[r - (1 << a) + 1][i][a]);
}
void fun() {
int i, lo, hi, mid;
bool a;
for (long long int i = 0; i < (n); i++) {
lo = i, hi = n - 1;
bool ch = false;
while (lo <= hi) {
mid = (lo + hi) >> 1;
if (query(i, mid)) {
if (mid == n - 1) {
ch = true;
break;
}
a = query(i, mid + 1);
if (!a) {
ch = true;
break;
}
lo = mid + 1;
} else
hi = mid - 1;
}
if (ch)
val[i] = mid - i + 1;
else
val[i] = 0;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
for (int X = 1; X <= t; X++) {
cin >> n >> m >> k;
int i, j, ans[m], l, r, pos, ma;
for (long long int i = 0; i < (n); i++)
for (long long int j = 0; j < (m); j++) cin >> a[i][j];
build();
fun();
ma = -1, pos = -1;
for (long long int i = 0; i < (n); i++) {
if (ma < val[i]) {
ma = val[i];
pos = i;
}
}
if (ma == 0) {
for (long long int i = 0; i < (m); i++) cout << "0 ";
cout << "\n";
continue;
}
l = pos, r = val[pos] + pos - 1;
query(l, r, ans);
for (long long int i = 0; i < (m); i++) cout << ans[i] << " ";
cout << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
cin >> n >> m >> k;
int a[200][200] = {};
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) cin >> a[i][j];
int ya[200] = {};
int pr[200] = {};
int ot[200] = {};
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++) {
if (pr[i] || a[i][j] == 0) continue;
if (ya[a[i][j]]) {
pr[i] = 1;
ot[i] = j;
}
}
for (int i = 1; i <= n; i++) {
if (pr[i] || a[i][j] == 0) continue;
for (int t = i + 1; t <= n; t++) {
if (pr[t] || a[i][j] == 0) continue;
if (a[i][j] == a[t][j]) {
pr[i] = 1;
pr[t] = 1;
ya[a[i][j]] = 1;
ot[i] = j;
ot[t] = j;
}
}
}
}
for (int i = 1; i <= n; i++) cout << ot[i] << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> ev, od;
int eve = 0, ode = 0;
for (int i = 0; i < 2 * n; i++) {
int k;
cin >> k;
if (k % 2 == 0) {
eve++;
ev.push_back(i);
} else {
ode++;
od.push_back(i);
}
}
if (eve % 2 == 0) {
if (ode == 0) {
for (int i = 2; i < eve - 1; i = i + 2) {
cout << ev[i] + 1 << " " << ev[i + 1] + 1 << endl;
}
} else if (eve == 0) {
for (int i = 2; i < ode - 1; i = i + 2) {
cout << od[i] + 1 << " " << od[i + 1] + 1 << endl;
}
} else {
for (int i = 0; i < eve - 1; i = i + 2) {
cout << ev[i] + 1 << " " << ev[i + 1] + 1 << endl;
}
for (int i = 2; i < ode - 1; i = i + 2) {
cout << od[i] + 1 << " " << od[i + 1] + 1 << endl;
}
}
} else {
for (int i = 1; i < eve - 1; i = i + 2) {
cout << ev[i] + 1 << " " << ev[i + 1] + 1 << endl;
}
for (int i = 1; i < ode - 1; i = i + 2) {
cout << od[i] + 1 << " " << od[i + 1] + 1 << endl;
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const int MAXN = 0;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string str;
cin >> str;
cout << str;
reverse(str.begin(), str.end());
cout << str << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int check(vector<long int> a, int index) {
long int r = a[index];
a[index] = 0;
long int g = r / 14;
for (int i = 0; i < 14; i++) {
a[i] += g;
}
long int h = r % 14;
for (int i = 0; i < h; i++) {
a[(i + index + 1) % 14]++;
}
long long int ans = 0;
for (int i = 0; i < 14; i++) {
if (a[i] % 2 == 0) {
ans += a[i];
}
}
return ans;
}
int main() {
vector<long int> a(14);
for (int i = 0; i < 14; i++) cin >> a[i];
long long int ans = 0;
for (int i = 0; i < 14; i++) {
if (a[i] > 0) {
ans = max(ans, check(a, i));
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long double a;
cin >> a;
if (a >= -128 && a <= 127)
cout << "byte" << endl;
else if (a >= -32768 && a <= 32767)
cout << "short" << endl;
else if (a >= -2147483648 && a <= 2147483647)
cout << "int" << endl;
else if (a >= -9223372036854775808 && a <= 9223372036854775807)
cout << "long" << endl;
else
cout << "BigInteger" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, sum = 0, rest;
cin >> n >> d;
int *a = new int[n];
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) sum += a[i];
rest = (d - sum) / 5;
if (sum + (n - 1) * 10 > d)
cout << -1;
else
cout << rest;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxh = 201;
double p[2][maxh], ans[2];
double l[2], r[2], d[2], lp[2];
double t[2], cp[2];
int h[2];
void done(int o) {
t[o] += d[o];
for (int i = 1; i <= h[o ^ 1]; ++i) {
if (p[o ^ 1][i] < 1e-10) continue;
for (int j = l[o]; j <= r[o]; ++j) {
p[o ^ 1][max(0, i - j)] += p[o ^ 1][i] * lp[o];
}
p[o ^ 1][i] *= cp[o] * 0.01;
}
ans[o] += (1 - p[o][0]) * p[o ^ 1][0];
if (o == 0) p[1][0] = 0.0;
}
int main() {
for (int i = 0; i < 2; ++i) {
cin >> h[i] >> d[i] >> l[i] >> r[i] >> cp[i];
if (cp[i] == 100) {
printf("%.12lf\n", i * 1.0);
return 0;
}
p[i][h[i]] = 1.0;
lp[i] = 1.0 / (r[i] - l[i] + 1) * (1.0 - cp[i] * 0.01);
}
for (int cc = 0; cc < 1e4; ++cc) {
done(t[0] > t[1]);
}
printf("%.12lf\n", ans[0]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long n, x, m1;
string s;
long long m;
cin >> n;
long long a[n];
for (long long i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
long long j = 0;
for (long long i = 1; i < n; i++) {
if (a[i] != a[0]) {
j = i;
break;
}
}
long long p = 0;
if (j == 0) {
cout << 0;
return;
}
long long r = j;
for (long long i = 0; i < n; i++) {
for (long long j = r; j < n; j++) {
if (a[i] < a[j]) {
r = j + 1;
p++;
break;
}
}
}
cout << p;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int TESTS = 1;
while (TESTS--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void result() {
long long n, d;
cin >> n >> d;
char c = 'a';
char c1 = 'a';
long long a = 0;
for (int i = 0; i < (int)n; i++) {
if (i <= d - 1) {
cout << c;
c = c + 1;
} else {
a++;
if (a > d) {
c1 = 'a';
a = 1;
}
cout << c1;
c1 = c1 + 1;
}
}
cout << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
result();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> ara;
int main() {
long long n, a, b, c, t;
cin >> n >> a >> b >> c >> t;
for (long long i = 0; i < n; i++) {
long long temp;
cin >> temp;
ara.push_back(temp);
}
sort(ara.begin(), ara.end());
long long ans = n * a;
if (b < c) {
for (long long i = 0; i < ara.size(); i++) {
long long diff = t - ara[i];
ans += (diff * (c - b));
}
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000,100000000")
using namespace std;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = 2 * acos(0.0);
const double E = 2.71828;
struct SegTree {
long long* t;
void build(long long pos, long long L, long long R, long long a[]) {
if (L > R) return;
if (L == R)
t[pos] = a[L];
else {
long long Mid = (L + R) / 2;
build(pos * 2, L, Mid, a);
build(pos * 2 + 1, Mid + 1, R, a);
t[pos] = t[pos * 2] + t[pos * 2 + 1];
}
}
long long get(long long pos, long long tl, long long tr, long long L,
long long R) {
if (L > R) return 0;
if (L == tl && R == tr) return t[pos];
long long Mid = (tl + tr) / 2;
long long i = get(pos * 2, tl, Mid, L, min(Mid, R));
long long j = get(pos * 2 + 1, Mid + 1, tr, max(L, Mid + 1), R);
return i + j;
}
void update(long long pos, long long tl, long long tr, long long P,
long long val) {
if (tl == tr)
t[pos] = val;
else {
long long Mid = (tl + tr) / 2;
if (P <= Mid)
update(pos * 2, tl, Mid, P, val);
else
update(pos * 2 + 1, Mid + 1, tr, P, val);
t[pos] = t[pos * 2] + t[pos * 2 + 1];
}
}
} t[7][14];
long long aa[100001], bb[100001], A[7][14][100001], C[7][100001];
int main(void) {
long long n;
cin >> n;
for (int i = 0; i < n; i++) {
scanf("%I64d", &aa[i]);
}
for (int i = 2; i <= 6; i++) {
for (int y = 1; y <= i; y++) {
long long u = 1, h = y - 1;
for (int j = 0; j <= n; j++) {
h += u;
if (h == i) u = -1;
if (h == 1) u = 1;
bb[j] = h * aa[j];
A[i][y][j] = h;
if (h == 1) C[i][j] = y;
}
t[i][y].t = new long long[4 * n + 1];
t[i][y].build(1, 0, n - 1, bb);
}
for (int y = 1; y < i; y++) {
long long u = -1, h = i - y + 1;
for (int j = 0; j <= n; j++) {
h += u;
if (h == i) u = -1;
if (h == 1) u = 1;
bb[j] = h * aa[j];
A[i][y + i][j] = h;
if (h == 1) C[i][j] = y + i;
}
t[i][i + y].t = new long long[4 * n + 1];
t[i][i + y].build(1, 0, n - 1, bb);
}
}
long long m;
cin >> m;
for (int i = 0; i < m; i++) {
long long T;
scanf("%I64d", &T);
if (T == 1) {
long long a, b;
scanf("%I64d%I64d", &a, &b);
a--;
for (int j = 2; j <= 6; j++) {
for (int y = 1; y <= j; y++) {
t[j][y].update(1, 0, n - 1, a, b * A[j][y][a]);
}
for (int y = 1; y < j; y++) {
t[j][j + y].update(1, 0, n - 1, a, b * A[j][j + y][a]);
}
}
} else {
long long a, b, c;
scanf("%I64d%I64d%I64d", &a, &b, &c);
a--, b--;
long long w1 = C[c][a];
printf("%I64d\n", t[c][w1].get(1, 0, n - 1, a, b));
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
if (k > 0) {
if ((n == 1 && k == 1))
cout << "0\n";
else {
if (s[0] == '1') {
for (int i = 1; i <= n; i++) {
if (s[i] != '0' && k > 0) {
k--;
s[i] = '0';
}
}
} else {
s[0] = '1';
k = k - 1;
for (int i = 1; i < n; i++) {
if (s[i] != '0' && k > 0) {
k--;
s[i] = '0';
}
}
}
cout << s << "\n";
}
} else
cout << s << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void fast() {
ios_base::sync_with_stdio(NULL);
cin.tie(0);
cout.tie(0);
}
void online_judge() { freopen("output.txt", "w", stdout); }
const int flag_max = 0x3f3f3f3f;
const long long OO = 2e9 + 10;
const double EPS = (1e-8);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; }
const long long MOD = 1e9 + 7;
long long Power(long long n, long long deg) {
if (!deg)
return 1;
else if (deg & 1)
return (Power(n, deg - 1) * n) % MOD;
else {
long long half = Power(n, deg / 2) % MOD;
return ((half * half) % MOD);
}
}
vector<pair<string, int> > ans;
int main() {
fast();
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int> > Q;
while (n--) {
int x;
string s;
cin >> s;
if (s[0] == 'i') {
cin >> x;
Q.push(x);
ans.push_back({s, x});
} else if (s[0] == 'g') {
cin >> x;
if (Q.size() >= 1)
if ((Q.top()) == x) {
ans.push_back({"getMin", x});
continue;
}
while (!Q.empty()) {
if (Q.top() < x)
ans.push_back({"removeMin", INT_MAX}), Q.pop();
else
break;
}
if (Q.size() >= 1)
if (Q.top() == x) {
ans.push_back({"getMin", x});
continue;
}
Q.push(x);
ans.push_back({"insert", x});
ans.push_back({"getMin", x});
} else {
if (Q.size() >= 1) {
ans.push_back({"removeMin", INT_MAX});
Q.pop();
} else {
ans.push_back({"insert", 0});
ans.push_back({"removeMin", INT_MAX});
}
}
}
cout << ans.size() << '\n';
for (int i = 0; i < ans.size(); i++) {
if (ans[i].second != INT_MAX)
cout << ans[i].first << ' ' << ans[i].second << '\n';
else
cout << ans[i].first << '\n';
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int const MOD = 998244353;
int const INF = 1e5;
void solve() {
long long n, k;
cin >> n >> k;
long long a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long ct = 0;
long long sum = 0;
long long expiry = 0;
for (int i = 0; i < n; i++) {
if (sum == 0) {
if (a[i] == k) {
ct++;
expiry = 0;
sum = 0;
} else if (a[i] < k) {
sum += a[i];
expiry = i + 1;
} else {
int q = a[i] / k;
int rem = a[i] % k;
ct += q;
a[i] = rem;
if (a[i] == k) {
ct++;
expiry = 0;
sum = 0;
} else {
sum += a[i];
expiry = i + 1;
}
}
} else {
if (a[i] + sum <= k) {
ct++;
expiry = 0;
sum = 0;
} else {
ct++;
a[i] -= (k - sum);
sum = 0;
expiry = 0;
int q = a[i] / k;
int rem = a[i] % k;
ct += q;
a[i] = rem;
if (a[i] == k) {
ct++;
expiry = 0;
sum = 0;
} else {
sum += a[i];
expiry = i + 1;
}
}
}
}
if (sum > 0 && expiry == n) ct++;
cout << ct << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 1 << 17;
bool checker[10001];
struct Segment_Tree {
vector<int> t;
Segment_Tree(vector<bool> b) {
t.resize(max_n << 1, 0);
for (int i = 0; i < int(b.size()); i++) t[i + max_n] = b[i];
for (int i = max_n - 1; i; i--) t[i] = t[i << 1] + t[(i << 1) + 1];
}
public:
int get(int l, int r) { return gette(0, max_n - 1, l, r, 1); }
void add(int pos, int res) {
if (res != 0) add_el(0, max_n - 1, pos, res, 1);
}
private:
int gette(int tl, int tr, int l, int r, int v) {
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return t[v];
return gette(tl, ((tl + tr) >> 1), l, r, (v << 1)) +
gette(((tl + tr) >> 1) + 1, tr, l, r, (v << 1) + 1);
}
void add_el(int tl, int tr, int pos, int res, int v) {
if (tl > pos || tr < pos) return;
if (tl == pos && tr == pos) {
t[v] += res;
return;
}
add_el(tl, ((tl + tr) >> 1), pos, res, v << 1);
add_el(((tl + tr) >> 1) + 1, tr, pos, res, (v << 1) + 1);
t[v] = t[v << 1] + t[(v << 1) + 1];
}
};
bool check(int r) {
while (r != 0) {
int t = r % 10;
if (!(t == 4 || t == 7)) return false;
r /= 10;
}
return true;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n);
vector<bool> b(n, false);
for (int i = 0; i <= 10000; i++) checker[i] = check(i);
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = checker[a[i]];
}
Segment_Tree t(b);
while (m--) {
string s;
cin >> s;
if (int(s.size()) == 3) {
int l, r, c;
cin >> l >> r >> c;
for (int i = l - 1; i < r; i++) {
t.add(i, int(checker[a[i] + c]) - int(checker[a[i]]));
a[i] += c;
}
continue;
}
int l, r;
cin >> l >> r;
cout << t.get(--l, --r) << '\n';
}
return 0;
}
| 5 |
#include <algorithm>
#include <numeric>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
const ll MD = 1e9+7;
#define REP(i,n) for(int i=0;i<n;i++)
#define ALL(a) a.begin(),a.end()
#define EACH(it,a) for(auto it=a.begin();it!=a.end();it++)
int main() {
while(true){
int n;cin >> n;if(n==0)break;
vector<pair<int,int>> segs;//right,c
segs.push_back(make_pair(0,-1));segs.push_back(make_pair(1e9+50,-1));
vector<int> ls;
ls.push_back(0);ls.push_back(1e9+50);
REP(i,n){
string s;cin >> s;
if(s=="W"){
int l,s;cin >> l >> s;
for(int j=1;j<segs.size();j++)if(segs[j].second==-1){
if(s<segs[j].first-segs[j-1].first){//set
pair<int,int> p=make_pair(segs[j-1].first+s,l);
segs.push_back(p);
ls.push_back(p.first);
sort(ALL(segs));sort(ALL(ls));break;
}else{
s-=segs[j].first-segs[j-1].first;
segs[j].second=l;
}
}
}else if(s=="D"){
int l;cin >> l;
REP(j,segs.size())if(segs[j].second==l){
segs[j].second=-1;
}
}else{
int p;cin >> p;
int vi=upper_bound(ALL(ls),p)-ls.begin();
cout << segs[vi].second <<endl;
}
}
cout <<endl;
}
}
| 0 |
#include <bits/stdc++.h>
int main() {
int n, i, a[100000], b[100000], d = 0, c[100000];
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d%d", &a[i], &b[i]);
}
for (i = 0; i < n; i++) {
c[i] = a[i] + b[i];
if (c[i] > d) d = c[i];
}
printf("%d", d);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int c;
void gcd(int a, int b) {
if (b == 0) {
if (a != 1) c = INF;
return;
}
c += a / b;
gcd(b, a % b);
}
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
int mn = INF;
for (int i = 1; i <= n; i++) {
c = 0;
gcd(n, i);
mn = min(mn, c - 1);
}
cout << mn << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 3e5 + 5;
long long droga[max_n], hejo[max_n];
int dojscie[max_n];
bool krotki[max_n];
vector<pair<int, pair<int, int> > > graf[max_n];
void djikstra(int x) {
for (int i = 1; i < max_n; i++) droga[i] = 1e16;
priority_queue<pair<long long, pair<int, int> > > kolejka;
droga[x] = 0;
kolejka.push(make_pair(0, make_pair(x, 0)));
while (kolejka.size()) {
pair<long long, pair<int, int> > u = kolejka.top();
kolejka.pop();
if (droga[u.second.first] < -u.first) continue;
for (auto i : graf[u.second.first]) {
if (droga[i.second.first] > i.first + droga[u.second.first]) {
droga[i.second.first] = i.first + droga[u.second.first];
kolejka.push(make_pair(-droga[i.second.first],
make_pair(i.second.first, i.second.second)));
dojscie[i.second.first] = i.second.second;
}
if (droga[i.second.first] == i.first + droga[u.second.first] &&
i.first < hejo[dojscie[i.second.first]])
dojscie[i.second.first] = i.second.second;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, a, b, c, poczatek;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> a >> b >> c;
hejo[i] = c;
graf[a].push_back(make_pair(c, make_pair(b, i)));
graf[b].push_back(make_pair(c, make_pair(a, i)));
}
cin >> poczatek;
djikstra(poczatek);
long long wynik = 0;
for (int i = 1; i <= n; i++) wynik += hejo[dojscie[i]];
cout << wynik << "\n";
for (int i = 1; i <= n; i++)
if (i != poczatek) cout << dojscie[i] << " ";
cout << "\n";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
template <class T>
typename T::value_type arr_sum(const T& v, int n) {
typename T::value_type sum = 0;
for (int i = (0); i < (n); ++i) sum += v[i];
return sum;
}
struct Sync_stdio {
Sync_stdio() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
}
} _sync_stdio;
int C = 5500000;
vector<int> get_primes(int n) {
vector<int> prime(n + 1, 1);
prime[0] = prime[1] = 0;
int s = sqrt(n);
for (int i = 2; i <= s; ++i) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i) {
prime[j] = 0;
}
}
}
vector<int> primes;
for (int i = (0); i < (n + 1); ++i) {
if (prime[i]) {
long long t = i;
while (t < C) {
primes.push_back(t);
t *= i;
}
}
}
sort(primes.begin(), primes.end());
return primes;
}
int main() {
vector<long long> v(C);
vector<int> primes = get_primes(C);
for (auto i : primes) {
int t = i;
while (t < C) {
++v[t];
t += i;
}
}
vector<long long> ps(C);
for (int i = (1); i < (C); ++i) {
ps[i] = ps[i - 1] + v[i];
}
int n;
cin >> n;
for (int t = (0); t < (n); ++t) {
int a, b;
cin >> a >> b;
cout << ps[a] - ps[b] << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int e = 1e6 + 10;
int n, m;
int fa[e], ind[e], pl[e];
long long sum[e], a[e], ans[e];
void init() {
for (int i = 1; i <= n; i++) {
sum[i] = 0, fa[i] = i;
}
}
int father(int x) {
if (x == fa[x]) return x;
fa[x] = father(fa[x]);
return fa[x];
}
long long maxn = 0;
inline long long da(long long t1, long long t2) { return t1 < t2 ? t2 : t1; }
void hb(int t1, int t2) {
int u = father(t1), v = father(t2);
fa[u] = v;
sum[v] += sum[u];
maxn = da(sum[v], maxn);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
init();
for (int i = 1; i <= n; i++) {
scanf("%d", &ind[i]);
}
ans[n] = 0;
for (int i = n; i > 0; i--) {
int d = ind[i];
pl[d] = 1;
sum[d] = a[d];
maxn = da(maxn, a[d]);
if (pl[d - 1] == 1) hb(d - 1, d);
if (pl[d + 1] == 1) hb(d + 1, d);
ans[i - 1] = maxn;
}
for (int i = 1; i <= n; i++) {
printf("%lld\n", ans[i]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
int main() {
std::string input;
getline(std::cin, input);
int k;
scanf("%d\n", &k);
long total(0);
for (int p = 0; p < k; p++) {
std::string forbidden;
getline(std::cin, forbidden);
bool inside = 0;
long first(0), second(0);
for (long s = 0; s < input.size(); s++) {
if (!inside && input[s] != forbidden[0] && input[s] != forbidden[1]) {
continue;
}
if (inside && input[s] != forbidden[0] && input[s] != forbidden[1]) {
inside = 0;
long minimum = first;
if (first > second) {
minimum = second;
}
total += minimum;
first = 0;
second = 0;
} else if (input[s] == forbidden[0] || input[s] == forbidden[1]) {
inside = 1;
if (input[s] == forbidden[0]) {
++first;
} else if (input[s] == forbidden[1]) {
++second;
}
}
}
if (inside) {
long minimum = first;
if (first > second) {
minimum = second;
};
total += minimum;
}
}
printf("%ld\n", total);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void solve(int arr[], long long n) {
unordered_map<long long, int> M;
vector<pair<int, int> > out;
long long sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (sum == 0) out.push_back(make_pair(0, i));
if (M.find(sum) != M.end()) {
out.push_back(make_pair(M[sum] + 1, i));
}
M[sum] = i;
}
int last = 0, res = 0;
for (int i = 0; i < int(out.size()); i++) {
swap(out[i].first, out[i].second);
}
sort(out.begin(), out.end());
for (int i = 0; i < int(out.size()); i++) {
if (out[i].second >= last) {
res++;
last = out[i].first;
}
}
cout << res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
solve(arr, n);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 111111;
vector<int> g[MAXN];
int a[MAXN];
int len, cnt, n, m, x, y, i, j;
long long MODUL;
bool used[MAXN];
void dfs(int v) {
cnt++;
used[v] = true;
for (size_t i = 0; i < g[v].size(); i++) {
if (!used[g[v][i]]) dfs(g[v][i]);
}
}
template <class T>
T sqr(T a) {
return a * a;
}
long long binpow(long long a, long long n) {
if (n == 0) return 1;
if (n % 2 == 1) return (binpow(a, n - 1) * a) % MODUL;
return sqr(binpow(a, n / 2)) % MODUL;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> m >> MODUL;
while (m--) {
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for (i = 1; i <= n; i++) {
if (!used[i]) {
cnt = 0;
dfs(i);
a[len++] = cnt;
}
}
long long ans;
if (len == 1) {
ans = 1 % MODUL;
} else {
ans = 1;
ans = binpow(n, len - 2);
for (i = 0; i < len; i++) ans = (ans * (long long)a[i]) % MODUL;
}
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int dp[105][105][105][2], p[105], pre[105];
int odd, even;
int main() {
std::ios::sync_with_stdio(false);
memset(dp, 0x3f, sizeof(dp));
int n;
cin >> n;
dp[0][0][0][0] = dp[0][0][0][1] = 0;
for (int i = 1; i <= n; i++) {
cin >> p[i];
if (p[i]) {
if (p[i] & 1)
++odd;
else
++even;
pre[i] = pre[i - 1];
} else
pre[i] = pre[i - 1] + 1;
}
even = n / 2 - even;
odd = (n + 1) / 2 - odd;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= even; j++)
for (int k = 0; k <= odd; k++) {
if (p[i]) {
if (j + k <= pre[i])
if (p[i] % 2 == 0)
dp[i][j][k][0] = min(dp[i - 1][j][k][0], dp[i - 1][j][k][1] + 1);
else
dp[i][j][k][1] = min(dp[i - 1][j][k][0] + 1, dp[i - 1][j][k][1]);
} else if (j + k <= pre[i])
dp[i][j][k][0] =
min(dp[i - 1][j - 1][k][0], dp[i - 1][j - 1][k][1] + 1),
dp[i][j][k][1] =
min(dp[i - 1][j][k - 1][0] + 1, dp[i - 1][j][k - 1][1]);
}
cout << min(dp[n][even][odd][0], dp[n][even][odd][1]) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif
typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;
const int inf=1<<30;
const long long int infll=1LL<<58;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
os << "[";
for (const auto &v : vec) {
os << v << ",";
}
os << "]";
return os;
}
void solve(){
while(true){
string s;
cin >> s;
if(s=="0") break;
const int size=s.size();
vector<int> nums(size);
int ans=0,cnt[11][11]={{1}},prev[2]={};
rep(i,0,size) nums[i]=s[size-i-1]-'0';
rep(i,0,size){
const int x=(prev[0]+(i%2?0:nums[i]))%11,y=(prev[1]+(i%2?nums[i]:0))%11;
if(nums[i]!=0 and i>0) rep(j,0,11) rep(k,0,11) if(((x-j)-(y-k)+22)%11==0) ans+=cnt[j][k];
++cnt[x][y];
prev[i%2]=(prev[i%2]+nums[i])%11;
}
cout << ans << endl;
}
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
solve();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
struct rec {
unsigned long long n, c;
unsigned long long a, b;
};
bool operator<(const rec &a, const rec &b) { return a.c < b.c; }
rec X[50];
unsigned long long T[50][50][101];
char W[50][50][101];
char V[50][50][101];
void print(int n, int i, int j) {
if (n) {
--n;
print(n, W[n][i][j], V[n][i][j]);
cout << X[i].n << " " << X[i].a + j << endl;
}
}
int main(void) {
unsigned long long i, j, r, n, m, k, t, y;
cin >> n >> m >> k;
for (i = 0; i < m; i++) {
cin >> X[i].a >> X[i].b >> X[i].c;
X[i].n = i + 1;
}
sort(X, X + m);
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
for (r = 0; r <= 100; r++) T[i][j][r] = 0;
for (i = 0; i < m; i++) {
for (j = X[i].a; j <= X[i].b; j++) T[0][i][j - X[i].a] = j;
}
for (r = 1; r < n; r++) {
for (i = r; i < m; i++) {
for (t = r - 1; t < i; t++) {
if (X[t].c < X[i].c) {
for (j = X[i].a; j <= X[i].b; j++) {
if (j > k) {
y = j - k;
if (y <= X[t].b && y >= X[t].a && T[r - 1][t][y - X[t].a] > 0) {
if (T[r][i][j - X[i].a] < T[r - 1][t][y - X[t].a] + j) {
T[r][i][j - X[i].a] = T[r - 1][t][y - X[t].a] + j;
W[r][i][j - X[i].a] = t;
V[r][i][j - X[i].a] = y - X[t].a;
}
}
}
if (j % k == 0) {
y = j / k;
if (y <= X[t].b && y >= X[t].a && T[r - 1][t][y - X[t].a] > 0) {
if (T[r][i][j - X[i].a] < T[r - 1][t][y - X[t].a] + j) {
T[r][i][j - X[i].a] = T[r - 1][t][y - X[t].a] + j;
W[r][i][j - X[i].a] = t;
V[r][i][j - X[i].a] = y - X[t].a;
}
}
}
}
}
}
}
}
unsigned long long imax, jmax;
y = 0;
for (i = 0; i < m; i++)
for (j = 0; j <= 100; j++)
if (T[n - 1][i][j] > y) y = T[n - 1][imax = i][jmax = j];
if (y > 0) {
cout << "YES" << endl;
print(n, imax, jmax);
} else
cout << "NO";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long UNDEF = -1;
const long long INF = 1e18;
template <typename T>
inline bool chkmax(T& aa, T bb) {
return aa < bb ? aa = bb, true : false;
}
template <typename T>
inline bool chkmin(T& aa, T bb) {
return aa > bb ? aa = bb, true : false;
}
static char stdinBuffer[1024];
static char* stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);
static const char* stdinPos = stdinDataEnd;
void readAhead(size_t amount) {
size_t remaining = stdinDataEnd - stdinPos;
if (remaining < amount) {
memmove(stdinBuffer, stdinPos, remaining);
size_t sz = fread(stdinBuffer + remaining, 1,
sizeof(stdinBuffer) - remaining, stdin);
stdinPos = stdinBuffer;
stdinDataEnd = stdinBuffer + remaining + sz;
if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;
}
}
int readInt() {
readAhead(16);
int x = 0;
bool neg = false;
while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
if (*stdinPos == '-') {
++stdinPos;
neg = true;
}
while (*stdinPos >= '0' && *stdinPos <= '9') {
x *= 10;
x += *stdinPos - '0';
++stdinPos;
}
return neg ? -x : x;
}
char readCh() {
readAhead(16);
while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
char ans = *stdinPos;
++stdinPos;
return ans;
}
const int mp = 60;
typedef struct S {
int a[60];
int len;
} S;
S identity;
S combine(S& dest, S x, S y) {
if (x.a[0] == 0) dest = y;
if (y.a[0] == 0) dest = x;
for (int i = 0; i < mp; i++) {
int t = x.a[i];
dest.a[i] = t + y.a[(i + t) % mp];
}
dest.len = x.len + y.len;
return dest;
}
S combine(S x, S y) {
S dest;
combine(dest, x, y);
return dest;
}
const long long MAXN = 100002;
S t[2 * MAXN];
int segn;
void build(int n) {
segn = n;
for (int i = n - 1; i > 0; --i) {
combine(t[i], t[i << 1], t[i << 1 | 1]);
}
}
void modify(int p, const S& value) {
int n = segn;
for (t[p += n] = value; p >>= 1;) combine(t[p], t[p << 1], t[p << 1 | 1]);
}
S query(int l, int r) {
int n = segn;
r++;
S resl = identity, resr = identity;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
resl = combine(resl, t[l++]);
}
if (r & 1) {
resr = combine(t[--r], resr);
}
}
return combine(resl, resr);
}
S one[7];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n = readInt();
segn = n + 1;
for (int x = 2; x <= 6; x++) {
one[x].len = 1;
for (int i = 0; i < mp; i++) {
one[x].a[i] = (i % x == 0) ? 2 : 1;
}
}
for (int i = 0; i < n; i++) {
int per = readInt();
t[segn + i] = one[per];
}
build(segn);
S tmp = combine(one[4], one[2]);
int q = readInt();
for (int i = 0; i < q; i++) {
int c = readCh(), x = readInt(), y = readInt();
if (c == 'A') {
--x;
--y;
S q = query(x, y - 1);
int ans = q.a[0];
printf("%d\n", ans);
} else {
--x;
modify(x, one[y]);
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct T {
int a;
};
long long int bigmod(long long int a, long long int b, long long int mod) {
if (b == 0) return 1;
if (b % 2 == 0) {
long long int hh = bigmod(a, b / 2, mod);
return (hh * hh) % mod;
} else {
return (a * bigmod(a, b - 1, mod)) % mod;
}
}
char st[1003], ar[100005][10];
int main() {
int i, j, k, l, t, cs = 1, r = 1, s, m, n, a, b, c, d, e, f, g, h, u, v;
scanf("%d", &n);
t = 0;
for (i = 0; i < n; i++) {
scanf("%s", &st);
if (t == -1) continue;
u = 0;
l = strlen(st);
if (i == 0)
for (j = 0; j < l; j++) {
if (st[j] == '?') {
if (u != 0)
ar[i][j] = '0';
else
ar[i][j] = '1', u = 1;
} else
ar[i][j] = st[j], u = 1;
}
else {
a = strlen(ar[i - 1]);
g = 0;
for (j = 0; j < l; j++) {
if (l < a) {
t = -1;
} else if (l > a) {
if (st[j] == '?') {
if (u == 0)
ar[i][j] = '1', u = 1;
else
ar[i][j] = '0';
g = 1;
} else
ar[i][j] = st[j], u = 1;
} else if (l == a) {
if (st[j] == '?') {
ar[i][j] = ar[i - 1][j];
} else {
ar[i][j] = st[j];
}
}
}
}
ar[i][j] = '\0';
if (u == 0) {
v = -1;
g = 0;
for (j = 0; j < l; j++) {
if (st[j] != '?' && ar[i][j] < ar[i - 1][j]) break;
if (st[j] != '?' && ar[i][j] > ar[i - 1][j]) {
v = j;
break;
}
if (st[j] == '?' && ar[i - 1][j] < '9') v = j;
}
if (v >= 0) {
if (st[v] == '?') ar[i][v] = ar[i - 1][v] + 1;
for (j = v + 1; j < l; j++) {
if (st[j] == '?') ar[i][j] = '0';
}
} else
t = -1;
}
}
if (t == -1) {
printf("NO\n");
} else {
printf("YES\n");
for (i = 0; i < n; i++) {
printf("%s\n", ar[i]);
}
}
return 0;
}
| 5 |
#include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <stack>
#include <climits>
#include <deque>
#include <bitset>
#include <cassert>
#include <ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int dy[]={-1,0,1,0},dx[]={0,1,0,-1};
// adjust problem by problem
const double EPS=1e-8;
const double PI=acos(-1.0);
#ifdef __GNUC__
int popcount(int n){return __builtin_popcount(n);}
int popcount(ll n){return __builtin_popcountll(n);}
#endif
#ifndef __GNUC__
template<class T> int popcount(T n){int cnt=0;while(n){if(n%2)cnt++;n/=2;}return cnt;}
#endif
template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> void PrintSeq(T &a,int sz){for(int i=0;i<sz;i++){cout<<a[i];if(sz==i+1)cout<<endl;else cout<<' ';}}
bool EQ(double a,double b){return abs(a-b)<EPS;}
void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}
vector<string> split(string str,char del){
vector<string> res;
for(int i=0,s=0;i<SIZE(str);i++){
if(str[i]==del){if(i-s!=0)res.push_back(str.substr(s,i-s));s=i+1;}
else if(i==SIZE(str)-1){res.push_back(str.substr(s));}
}
return res;
}
typedef tuple<int,int,int> tpl;
int field[10][10];
int N;
tpl ans[20];
const int sx[]={0,-1,0,1,0};
const int sy[]={0,1,1,1,2};
const int mx[]={0,1,2,0,1,2,0,1,2};
const int my[]={0,0,0,1,1,1,2,2,2};
const int lx[]={0,-1,0,1,-2,-1,0,1,2,-1,0,1,0};
const int ly[]={0,1,1,1,2,2,2,2,2,3,3,3,4};
bool dp[13][201];
bool dfs(int pos,int cnt,int sum){
int cx=pos%10;
int cy=pos/10;
if(pos==100){
return (sum==0&&cnt==N);
}
else{
if(field[cy][cx]==0)return dfs(pos+1,cnt,sum);
else{
if(cnt==N)return false;
if((N-cnt)*5>sum)return false;
if(!dp[cnt][sum])return false;
int bk[10][10];
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
bk[i][j]=field[i][j];
// small
{
bool isOK=true;
for(int i=0;i<5;i++){
int ny=cy+sy[i];
int nx=cx+sx[i];
if(!(ny>=0&&nx>=0&&ny<10&&nx<10&&field[ny][nx]))
isOK=false;
}
if(isOK){
for(int i=0;i<5;i++){
int ny=cy+sy[i];
int nx=cx+sx[i];
field[ny][nx]--;
}
ans[cnt]=tpl(cx,cy+1,1);
if(sum-5>=0&&dfs(pos,cnt+1,sum-5))
return true;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
field[i][j]=bk[i][j];
}
}
// med
{
bool isOK=true;
for(int i=0;i<9;i++){
int ny=cy+my[i];
int nx=cx+mx[i];
if(!(ny>=0&&nx>=0&&ny<10&&nx<10&&field[ny][nx]))
isOK=false;
}
if(isOK){
for(int i=0;i<9;i++){
int ny=cy+my[i];
int nx=cx+mx[i];
field[ny][nx]--;
}
ans[cnt]=tpl(cx+1,cy+1,2);
if(sum-9>=0&&dfs(pos,cnt+1,sum-9))
return true;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
field[i][j]=bk[i][j];
}
}
// lrg
{
bool isOK=true;
for(int i=0;i<13;i++){
int ny=cy+ly[i];
int nx=cx+lx[i];
if(!(ny>=0&&nx>=0&&ny<10&&nx<10&&field[ny][nx]))
isOK=false;
}
if(isOK){
for(int i=0;i<13;i++){
int ny=cy+ly[i];
int nx=cx+lx[i];
field[ny][nx]--;
}
ans[cnt]=tpl(cx,cy+2,3);
if(sum-13>=0&&dfs(pos,cnt+1,sum-13))
return true;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
field[i][j]=bk[i][j];
}
}
//if(dfs(pos+1,cnt,sum))return true;
}
}
return false;
}
int main(){
int sum=0;
cin>>N;
for(int i=0;i<10;i++)
for(int j=0;j<10;j++)
cin>>field[i][j],sum+=field[i][j];
for(int i=N;i>=0;i--){
for(int j=0;j<=200;j++){
if(i==N)dp[i][j]=(j==0);
else{
dp[i][j]=false;
if(j-5>=0)dp[i][j]|=dp[i+1][j-5];
if(j-9>=0)dp[i][j]|=dp[i+1][j-9];
if(j-13>=0)dp[i][j]|=dp[i+1][j-13];
}
}
}
if(!dfs(0,0,sum))cout<<"no"<<endl;
for(int i=0;i<N;i++)
cout<<get<0>(ans[i])<<" "<<get<1>(ans[i])<<" "<<get<2>(ans[i])<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int hotel[100001];
int track[100001];
int num[100001];
int sol[100001];
int main() {
int i, j, k, n, m, res = 0, cnt = 0, mx = 0, mn = (1 << 30), x;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &hotel[i]);
num[i] = 0;
}
for (i = 0; i < n; i++) {
scanf("%d", &track[i]);
num[track[i] - 1]++;
if (track[i] == i + 1) track[i] = 0;
}
for (i = 0; i < n; i++) {
if (hotel[i]) {
j = i;
k = 0;
while (track[j] && (hotel[j] == 0 || k == 0) && num[j] <= 1) {
j = track[j] - 1;
k++;
}
if (track[j] == 0 && (hotel[j] == 0 || k == 0) && num[j] <= 1) k++;
if (k > mx) {
mx = k;
x = i;
}
}
}
printf("%d\n", mx);
j = x;
k = 0;
while (track[j] && (hotel[j] == 0 || k == 0) && num[j] <= 1) {
sol[k++] = j + 1;
j = track[j] - 1;
}
if (track[j] == 0 && (hotel[j] == 0 || k == 0) && num[j] <= 1)
sol[k++] = j + 1;
for (i = k - 1; i >= 0; i--) {
if (i == 0)
printf("%d\n", sol[i]);
else
printf("%d ", sol[i]);
}
return 0;
}
| 2 |
#include<stdio.h>
int main(){
long long int x,y,n=0;
scanf("%lld%lld",&x,&y);
do{
n++;
x*=2;
}while(x<=y);
printf("%lld",n);
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.