solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
int dayNum[13]={
0,31,29,31,30,31,30,31,31,30,31,30,31
};
string youbi[7]={
"Wednesday","Thursday","Friday","Saturday","Sunday","Monday","Tuesday"
};
int manth,day;
while(cin>>manth>>day)
{
if(manth==0)
{
break;
}
int sum=0;
for(int i=0;i<manth;i++)
{
sum+=dayNum[i];
}
sum+=day;
cout<<youbi[sum%7]<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int inf = 1e9 + 7;
int n, m, k;
pair<int, int> p[N];
map<int, vector<int> > r, c;
map<int, bool> mp[N];
set<int> R, C;
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= k; i++) {
scanf("%d%d", &p[i].first, &p[i].second);
mp[p[i].first][p[i].second] = 1;
r[p[i].first].push_back(p[i].second);
c[p[i].second].push_back(p[i].first);
R.insert(p[i].first);
C.insert(p[i].second);
}
for (auto i : R) {
r[i].push_back(0);
sort(r[i].begin(), r[i].end());
r[i].push_back(m + 1);
}
for (auto i : C) {
c[i].push_back(0);
sort(c[i].begin(), c[i].end());
c[i].push_back(n + 1);
}
int x = 1, y = 1, X = n, Y = m, cc = 1, rr = 1, d = 1;
vector<int> v;
while (1) {
if (x >= X && y >= Y) break;
if (d == 1) {
v.swap(r[rr]);
if ((int)v.size() == 0) {
v.push_back(0);
v.push_back(m + 1);
}
int j = upper_bound(v.begin(), v.end(), cc) - v.begin();
int vv = min(v[j], Y + 1);
for (int i = vv; i <= Y; i++) {
for (int l = rr; l <= X; l++) {
if (mp[l][i] == 0) {
puts("No");
return 0;
}
}
}
x++;
rr++;
Y = vv - 1;
cc = vv - 1;
d = 2;
v.swap(r[rr - 1]);
} else if (d == 2) {
v.swap(c[cc]);
if ((int)v.size() == 0) {
v.push_back(0);
v.push_back(n + 1);
}
int j = upper_bound(v.begin(), v.end(), rr) - v.begin();
int vv = min(v[j], X + 1);
for (int i = vv; i <= X; i++) {
for (int l = y; l <= cc; l++) {
if (mp[i][l] == 0) {
puts("No");
return 0;
}
}
}
Y--;
cc--;
X = vv - 1;
rr = vv - 1;
d = 3;
v.swap(c[cc + 1]);
} else if (d == 3) {
v.swap(r[rr]);
if ((int)v.size() == 0) {
v.push_back(0);
v.push_back(m + 1);
}
int j = upper_bound(v.begin(), v.end(), cc) - v.begin() - 1;
int vv = max(v[j], y - 1);
for (int i = y; i <= vv; i++) {
for (int l = x; l <= rr; l++) {
if (mp[l][i] == 0) {
puts("No");
return 0;
}
}
}
X--;
rr--;
y = vv + 1;
cc = vv + 1;
d = 4;
v.swap(r[rr + 1]);
} else if (d == 4) {
v.swap(c[cc]);
if ((int)v.size() == 0) {
v.push_back(0);
v.push_back(n + 1);
}
int j = upper_bound(v.begin(), v.end(), rr) - v.begin() - 1;
int vv = max(v[j], x - 1);
for (int i = x; i <= vv; i++) {
for (int l = cc; l <= Y; l++) {
if (mp[i][l] == 0) {
puts("No");
return 0;
}
}
}
y++;
cc++;
x = vv + 1;
rr = vv + 1;
d = 1;
v.swap(c[cc - 1]);
}
}
puts("Yes");
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, r, vt, a[1005];
int main() {
cin >> n >> r;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int d = 0;
int cuoi = 0;
while (cuoi < n) {
vt = 0;
for (int j = min(n, cuoi + r); j > max(0, cuoi - r + 1); j--)
if (a[j] == 1 && cuoi >= j - r) {
vt = j;
break;
}
if (vt == 0) {
cout << -1;
return 0;
}
d++;
cuoi = vt + r - 1;
}
cout << d;
return 0;
}
| 2 |
#include <bits/stdc++.h>
template <class T>
inline void read(T &res) {
res = 0;
bool bo = 0;
char c;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
if (c == '-')
bo = 1;
else
res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
if (bo) res = ~res + 1;
}
const int N = 5005, M = 680, L = N << 1;
int tm, n, cnt[L], tot, pri[N], id[L], top, m, ecnt, nxt[L], adj[L], go[L],
fa[L], sze[L], maxs[L], G;
bool mark[N];
long long ans;
void add_edge(int u, int v) {
nxt[++ecnt] = adj[u];
adj[u] = ecnt;
go[ecnt] = v;
}
void sieve() {
mark[0] = mark[1] = 1;
for (int i = 2; i <= n; i++) {
if (!mark[i]) pri[++tot] = i;
for (int j = 1; j <= tot; j++) {
if (1ll * i * pri[j] > n) break;
mark[i * pri[j]] = 1;
if (i % pri[j] == 0) break;
}
}
}
struct node {
int cnt[M];
void get(int k) {
for (int i = 1; i <= tot; i++) cnt[i] = 0;
for (int i = 1; i <= tot; i++)
for (int j = k / pri[i]; j; j /= pri[i]) cnt[i] += j;
}
friend inline bool operator==(node a, node b) {
for (int i = 1; i <= tot; i++)
if (a.cnt[i] != b.cnt[i]) return 0;
return 1;
}
} stk[L], pt[L];
node lca(node a, node b) {
node res;
for (int i = 1; i <= tot; i++) res.cnt[i] = 0;
for (int i = tot; i >= 1; i--) {
res.cnt[i] = std::min(a.cnt[i], b.cnt[i]);
if (a.cnt[i] != b.cnt[i]) break;
}
return res;
}
int dep(node a) {
int res = 0;
for (int i = 1; i <= tot; i++) res += a.cnt[i];
return res;
}
void dfs(int u) {
sze[u] = cnt[u];
maxs[u] = 0;
for (int e = adj[u], v = go[e]; e; e = nxt[e], v = go[e])
dfs(v), sze[u] += sze[v], maxs[u] = std::max(maxs[u], sze[v]);
}
void dfs2(int u) {
if (std::max(maxs[u], tm - sze[u]) < std::max(maxs[G], tm - sze[G])) G = u;
for (int e = adj[u], v = go[e]; e; e = nxt[e], v = go[e]) dfs2(v);
}
int main() {
int x;
read(tm);
for (int i = 1; i <= tm; i++) {
read(x);
if (!x) x = 1;
cnt[x]++;
n = std::max(n, x);
}
sieve();
m = n;
for (int i = 1; i <= n; i++) {
node tmp;
tmp.get(i);
pt[i] = tmp;
if (!top) {
stk[++top] = tmp;
id[top] = i;
continue;
}
node u = lca(stk[top], tmp);
while (top && dep(stk[top]) > dep(u)) {
if (top == 1 || dep(stk[top - 1]) < dep(u)) pt[fa[id[top]] = ++m] = u;
top--;
}
if (!(u == stk[top])) fa[m] = id[top], stk[++top] = u, id[top] = m;
stk[++top] = tmp;
id[top] = i;
fa[i] = id[top - 1];
}
int rt;
for (int i = 1; i <= m; i++)
if (fa[i])
add_edge(fa[i], i);
else
rt = i;
dfs(rt);
G = rt;
dfs2(rt);
for (int i = 1; i <= n; i++)
ans +=
1ll * (dep(pt[G]) + dep(pt[i]) - dep(lca(pt[G], pt[i])) * 2) * cnt[i];
return std::cout << ans << std::endl, 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
string s;
int k;
void changeNumbers(int i) {
s[i] = '9';
if (i > 1 && s[i - 1] != '0') {
s[i - 1] -= 1;
} else {
if (i > 1 && s[i - 1] == '0') {
s[i - 1] = '9';
changeNumbers(i - 1);
}
}
if (i == 1 && s[i - 1] != '0') {
s[i - 1] -= 1;
}
}
int main() {
long long x;
cin >> x;
s = to_string(x);
k = s.length();
long long sum1 = 0;
for (int i = 0; i < k; i++) {
sum1 += s[i];
}
sum1 -= (k * '0');
long long max_sum = sum1;
string that_number = s;
long long extra_sum = 0;
for (int i = k - 1; i > 0; i--) {
extra_sum = 0;
changeNumbers(i);
for (int j = 0; j < k; j++) {
extra_sum += s[j];
}
extra_sum -= (k * '0');
if (extra_sum > max_sum) {
max_sum = extra_sum;
that_number = s;
}
}
bool first = true;
for (int i = 0; i < that_number.size(); i++) {
if (that_number[i] != '0') first = false;
if (!first) cout << that_number[i];
}
cout << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline long long getnum() {
char c = getchar();
long long num, sign = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') sign = -1;
for (num = 0; c >= '0' && c <= '9';) {
c -= '0';
num = num * 10 + c;
c = getchar();
}
return num * sign;
}
vector<long long> V;
int main() {
long long n = getnum(), N = getnum();
for (; N > 0; N = N / 10) {
long long x = N % 10;
if (x == 4) {
V.push_back(3);
V.push_back(2);
V.push_back(2);
} else if (x == 6) {
V.push_back(5);
V.push_back(3);
} else if (x == 8) {
V.push_back(7);
V.push_back(2);
V.push_back(2);
V.push_back(2);
} else if (x == 9) {
V.push_back(7);
V.push_back(3);
V.push_back(3);
V.push_back(2);
} else if (x > 1) {
V.push_back(x);
}
}
sort(V.begin(), V.end());
reverse(V.begin(), V.end());
for (int i = 0; i < V.size(); i++) printf("%d", V[i]);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long dp[2][2000], a[2000][2000], o[2000][2000];
int main() {
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < m; j++) cin >> a[i][j];
for (int j = 1; j <= m; j++) o[i][j] = o[i][j - 1] + a[i][j - 1];
}
dp[1][0] = dp[1][1] = dp[0][m] = -1000000000;
for (int i = 1; i <= n; i++) {
if (i % 2) {
long long x = 1;
for (int j = (i % 2) + 1; j <= m; j++) {
dp[i % 2][j] = dp[1 - i % 2][x] + o[i][j];
if (dp[1 - i % 2][j] > dp[1 - i % 2][x]) x = j;
}
} else {
long long x = m;
for (int j = m - 1; j; j--) {
dp[i % 2][j] = dp[1 - i % 2][x] + o[i][j];
if (dp[1 - i % 2][j] > dp[1 - i % 2][x]) x = j;
}
}
}
long long ans = -1LL * 2000 * 2000 * 2000 * 2000;
for (int i = 1; i <= m; i++) ans = (ans < dp[n % 2][i] ? dp[n % 2][i] : ans);
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<int> s, t;
vector<int> G[100000];
int used[100000];
void dfs1(int v) {
s.push_back(v);
used[v] = 1;
for (auto to : G[v]) {
if (!used[to]) {
dfs1(to);
break;
}
}
}
void dfs2(int v) {
t.push_back(v);
used[v] = 1;
for (auto to : G[v]) {
if (!used[to]) {
dfs2(to);
break;
}
}
}
int main()
{
cin >> N >> M;
for (int i = 0, a, b; i < M; i++) {
cin >> a >> b;
G[a - 1].push_back(b - 1);
G[b - 1].push_back(a - 1);
}
dfs1(0);
dfs2(0);
reverse(t.begin(), t.end());
t.pop_back();
cout << s.size() + t.size() << endl;
for (int v : t) {
cout << v + 1 << ' ';
}
for (int i = 0; i < s.size() - 1; i++) {
cout << s[i] + 1 << ' ';
}
cout << s.back() + 1 << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int sum, t, p, pos, a[100005], b[100005], n, f, c, f1;
cin >> t;
while (t--) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
cin >> n;
f = 1;
sum = 0;
p = 0;
f1 = 1, c = 0;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < n; i++) {
if (f == 1 && a[i] != b[i]) {
if (p == 0) pos = i, p = 1, c = b[i] - a[i];
sum++;
f = 0;
} else if (a[i] > b[i])
f1 = 0;
else if (a[i] == b[i])
f = 1;
}
if (sum >= 2) {
cout << "NO" << endl;
} else if (f1 == 0 || c < 0)
cout << "NO" << endl;
else {
if (sum == 0)
cout << "YES" << endl;
else {
for (int i = pos; a[i] != b[i]; i++) {
if (b[i] - a[i] != c) f1 = 0;
}
if (f1 == 0)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
char s[N];
int main() {
int aOdd = 0, aEven = 0, bOdd = 0, bEven = 0;
scanf("%s", s);
int n = strlen(s);
for (int i = 0; i < (n); ++i) {
if (s[i] == 'a') {
if (i & 1)
++aOdd;
else
++aEven;
} else {
if (i & 1)
++bOdd;
else
++bEven;
}
}
long long odd = 0, even = 0;
for (int i = 0; i < (n); ++i) {
if (s[i] == 'a') {
if (i & 1) {
odd += aOdd;
even += aEven;
--aOdd;
} else {
odd += aEven;
even += aOdd;
--aEven;
}
} else {
if (i & 1) {
odd += bOdd;
even += bEven;
--bOdd;
} else {
odd += bEven;
even += bOdd;
--bEven;
}
}
}
cout << even << " ", cout << odd << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t = 1;
cin >> t;
while (t--) {
long long n, a, b;
cin >> n >> a >> b;
long long a1 = a * n;
long long a2 = (n / 2) * b;
if (n % 2 != 0) a2 += a;
if (a1 < a2) {
cout << a1 << endl;
} else
cout << a2 << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int h, n, m, cnt, out[MAXN], ans, a[MAXN];
bool vis[MAXN];
struct Edge {
int nxt, from, ver;
} e[MAXN * 2];
int head[MAXN], tot;
void add(int u, int v) {
e[++tot].from = u;
e[tot].ver = v;
e[tot].nxt = head[u];
head[u] = tot;
}
stack<int> s;
int dfn[MAXN], low[MAXN], sc[MAXN], sn, sz[MAXN];
void tarjan(int u) {
low[u] = dfn[u] = ++cnt;
vis[u] = 1;
s.push(u);
int i;
for (i = head[u]; i; i = e[i].nxt) {
int v = e[i].ver;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (vis[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
int tp;
sn++;
while (1) {
tp = s.top();
s.pop();
sc[tp] = sn;
vis[tp] = 0;
sz[sn]++;
if (tp == u) return;
}
}
}
int main() {
int i;
scanf("%d %d %d", &n, &m, &h);
for (i = 1; i <= n; i++) scanf("%d", &a[i]);
for (i = 1; i <= m; i++) {
int u, v;
scanf("%d %d", &u, &v);
if ((a[u] + 1) % h == a[v]) add(u, v);
if ((a[v] + 1) % h == a[u]) add(v, u);
}
for (i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
for (i = 1; i <= tot; i++) {
int u = sc[e[i].from], v = sc[e[i].ver];
if (u != v) out[u]++;
}
sz[0] = 0x3f3f3f3f;
for (i = 1; i <= sn; i++)
if (!out[i]) {
if (sz[i] < sz[ans]) ans = i;
}
printf("%d\n", sz[ans]);
for (i = 1; i <= n; i++)
if (sc[i] == ans) printf("%d ", i);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int A[5000 + 2], n;
vector<pair<int, int> > v;
int binary(int num, int x, int y) {
int lo = x, hi = y, mi;
if (num <= v[lo].first) return lo;
if (!(num <= v[hi].first)) return n;
while (hi - lo > 1) {
mi = (lo + hi) / 2;
if (num <= v[mi].first)
hi = mi;
else
lo = mi;
}
return hi;
}
int main() {
int K;
cin >> n >> K;
for (int i = 0; i < n; i++) {
cin >> A[i];
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 21; j++) {
if (j != 0) v.push_back(make_pair(A[i] * j, j));
}
sort(v.begin(), v.end());
int q;
cin >> q;
for (int k = 0; k < q; k++) {
int num;
cin >> num;
int paso = 1000, paso2 = 1000;
int pos2 = binary(num, 0, ((int)v.size()) - 1);
if (v[pos2].first == num && pos2 != ((int)v.size()) - 1 &&
v[pos2].second <= K) {
paso2 = v[pos2].second;
}
for (int i = 0; i < ((int)v.size()); i++) {
int val = v[i].first;
int pos = binary(num - val, 0, ((int)v.size()) - 1);
if (pos == n) continue;
if (((v[i].first + v[pos].first) == num) &&
((v[pos].second + v[i].second <= K)) && pos != i) {
int u;
u = v[pos].second + v[i].second;
paso = min(paso, u);
}
}
if (paso == 1000 && paso2 == 1000)
cout << -1 << endl;
else
cout << min(paso, paso2) << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
;
long long int t, i, n = 7;
cin >> t;
long long int arr[7];
while (t--) {
for (i = 0; i < n; i++) {
cin >> arr[i];
}
cout << arr[0] << " " << arr[1] << " " << arr[6] - (arr[0] + arr[1])
<< endl;
}
return 0;
}
| 1 |
/*Bismillahir Rahmanir Rahim*/
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define ff first
#define ss second
#define ll long long int
#define dd double
#define pb push_back
#define pr pair<ll,ll>
#define pqmx priority_queue<ll>
#define pqmn priority_queue<ll,vector<ll>,greater<ll>>
// #define MOD 1000000007
#define pd(x,y) fixed<<setprecision(y)<<x
#define w(x) ll x; cin>>x; while(x--)
#define loop(i,x,n) for(ll i=x;i<n;i++)
#define rloop(x,n) for(ll i=n-1;i>=x;i--)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define dbg1(x) cout<<"["<<#x<<": "<<x<<"]"<<endl;
#define dbg2(x, y) cout<<"["<<#x<<": "<<x<<"]"<<" ["<<#y<<": "<<y<<"]"<<endl;
#define dbg3(x, y, z) cout<<"["<<#x<<": "<<x<<"]"<<" ["<<#y<<": "<<y<<"]"<<" ["<<#z<<": "<<z<<"]"<<endl;
#define dbg4(x, y, z, k) cout<<"["<<#x<<": "<<x<<"]"<<" ["<<#y<<": "<<y<<"]"<<" ["<<#z<<": "<<z<<"]"<<" ["<<#k<<": "<<k<<"]"<<endl;
#define no cout << "NO" << endl;
#define yes cout << "YES" << endl;
// #define endl "\n"
#define FAST ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
#define pi 3.141592653
ll const N=100006;
//Samee Sevas
using namespace std;
vector<ll> prime;
ll f[N]={0};
void inti(){
vector<bool> is_prime(N+1, true);
for (int i = 2; i <= N; i++) {
if (is_prime[i] && (long long)i * i <= N) {
for (int j = i * i; j <= N; j += i)
is_prime[j] = false;
}
}
loop(i,2,N)
{
if(is_prime[i]==true)
prime.push_back(i);
}
}
void solve()
{
ll n,m,r,k;
cin>>n>>m>>k;
ll x1=0,x2=0;
ll x=n,y=m;
for(ll i=0;i<prime.size();i++)
{
if(prime[i]*prime[i]>x) break;
if(x%prime[i]==0)
{
while(x%prime[i]==0)
{
x1++;
x/=prime[i];
}
}
}
if(x>1)
x1++;
x=m;
for(ll i=0;i<prime.size();i++)
{
if(prime[i]*prime[i]>x) break;
if(x%prime[i]==0)
{
while(x%prime[i]==0)
{
x2++;
x/=prime[i];
}
}
}
if(x>1)
x2++;
y=min(x1,x2);
//dbg1(y);
if(k>=2){
y=x1+x2;
if(y<k)
no
else
yes
}
else
{
ll mn=min(n,m);
ll mx=max(n,m);
if(mx%mn==0)
{
ll z=mx/mn;
if(z>1)
{
//dbg1(1)
yes
}
else
no
}
else
no
}
}
signed main()
{
FAST;
ll t=1,u=0;
cin>>t;
inti();
while(t--)
{
// cout<<"Case "<<++u<<": ";
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char cad[100];
int tam, signo = 1;
long long res = 0, acarreo = 0;
scanf("%s", cad);
tam = strlen(cad);
for (int i = 0; i < tam; i++) {
if (cad[i] == '-' || cad[i] == '+') {
res += signo * acarreo;
acarreo = 0;
}
if (cad[i] == '-') signo = -1;
if (cad[i] == '+') signo = 1;
acarreo = acarreo * 10 + cad[i] - '0';
}
res += signo * acarreo;
printf("%lld", res);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> it[1 << 21];
void it_add(pair<int, int> val, int pos) {
int p = pos + (1 << 20) - 1;
while (p) {
it[p] = max(it[p], val);
p /= 2;
}
}
pair<int, int> it_get(int nd, int nl, int nr, int l, int r) {
if (l <= nl && nr <= r) return it[nd];
if (l > nr || nl > r) return make_pair(-1, -1);
return max(it_get(2 * nd, nl, (nl + nr) / 2, l, r),
it_get(2 * nd + 1, (nl + nr) / 2 + 1, nr, l, r));
}
pair<int, int> it_get(int l, int r) {
if (l > r) return make_pair(-1, -1);
return it_get(1, 1, (1 << 20), l, r);
}
int n, c;
int a[1 << 20], b[1 << 20];
char tp[1 << 20];
int hs[1 << 20];
int ne[1 << 20];
int fin[1 << 20];
int li[1 << 20];
int mik[1 << 20], mxk[1 << 20];
bool getDP(int l, int r) {
if (hs[l] != -1) return hs[l];
if (l >= r) {
return 1;
}
hs[l] = 0;
int k = mik[l];
if (k < l) k = l;
vector<int> vs;
while (k <= r && k <= mxk[l]) {
pair<int, int> si = it_get(l + 1, k);
int s = si.first;
if (s <= k) {
for (int i = 0; i < (int)vs.size(); ++i) {
fin[vs[i]] = k;
}
if (getDP(l + 1, k) && getDP(k + 1, r)) {
ne[l] = k;
hs[l] = 1;
break;
}
break;
}
int id = si.second;
if (id != -1) {
vs.push_back(id);
if (fin[id] != -1 && a[id] > l && a[id] <= k && fin[id] != k) {
k = fin[id];
continue;
}
}
if (s > k) {
k = s;
}
}
return hs[l];
}
int le[1 << 20], ri[1 << 20];
void cli(int l, int r) {
if (l >= r) return;
li[l] = ne[l];
if (li[l] > l) {
le[l] = l + 1;
cli(l + 1, li[l]);
}
if (li[l] < r) {
ri[l] = li[l] + 1;
cli(li[l] + 1, r);
}
}
vector<int> sl;
void sol(int v) {
if (le[v] != -1) sol(le[v]);
sl.push_back(v);
if (ri[v] != -1) sol(ri[v]);
}
int main() {
memset(it, -1, sizeof(it));
memset(mik, 0, sizeof(mik));
memset(mxk, 63, sizeof(mxk));
scanf("%d %d", &n, &c);
char buf[32];
for (int i = 0; i < c; i++) {
scanf("%d %d %s", &a[i], &b[i], buf);
it_add(make_pair(b[i], i), a[i]);
tp[i] = buf[0];
if (tp[i] == 'L') {
mik[a[i]] = max(mik[a[i]], b[i]);
}
if (tp[i] == 'R') {
mxk[a[i]] = min(mxk[a[i]], b[i] - 1);
}
if (a[i] >= b[i]) {
printf("IMPOSSIBLE\n");
return 0;
}
}
memset(hs, -1, sizeof(hs));
memset(fin, -1, sizeof(fin));
bool ok = getDP(1, n);
if (!ok) {
printf("IMPOSSIBLE\n");
return 0;
}
memset(li, -1, sizeof(li));
memset(le, -1, sizeof(le));
memset(ri, -1, sizeof(ri));
cli(1, n);
sol(1);
for (int i = 0; i + 1 < (int)sl.size(); i++) {
printf("%d ", sl[i]);
}
printf("%d\n", sl.back());
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int M = 1000000007;
int n, m, t;
long long dp[61][31][31];
int arr[61];
long long solve(int ind, int x, int y) {
if (x > 3 && y > 0 && x + y == t) return 1;
if (ind == n + m + 1) return 0;
long long &ret = dp[ind][x][y];
if (ret != -1) return ret;
ret = solve(ind + 1, x, y);
if (x < n && arr[ind] == 1) ret += solve(ind + 1, x + 1, y);
if (y < m && arr[ind] == 2) ret += solve(ind + 1, x, y + 1);
return ret;
}
int main() {
scanf("%d%d%d", &n, &m, &t);
int i = 0;
for (; i < n; i++) arr[i] = 1;
for (; i < n + m; i++) arr[i] = 2;
memset(dp, -1, sizeof dp);
printf("%lld\n", solve(0, 0, 0));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const double error = 1e-15;
const double pi = 2 * acos(0);
const int maxn = (int)1e5 + 10;
const int mod = (int)1e9;
int dx[] = {0, -1, 0, +1, -1, -1, +1, +1};
int dy[] = {-1, 0, +1, 0, -1, +1, +1, -1};
long int A[900], B[900], C[900];
vector<pair<long, pair<int, int> > > v;
int main() {
ios_base::sync_with_stdio(0);
long n, i, j, x;
cin >> n;
for (i = 2; i <= 2 * n; i++) {
for (j = 1; j < i; j++) {
cin >> x;
v.push_back(make_pair(x, make_pair(i, j)));
}
}
sort(v.begin(), v.end());
x = v.size();
for (i = x - 1; i >= 0; i--)
if (A[v[i].second.first] == 0 && A[v[i].second.second] == 0) {
C[v[i].second.first] = v[i].second.second;
C[v[i].second.second] = v[i].second.first;
A[v[i].second.first] = 1;
A[v[i].second.second] = 1;
}
for (i = 1; i <= 2 * n; i++) cout << C[i] << " ";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int fin() {
long long int x;
scanf("%lld", &x);
return x;
}
void func() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
pair<long long int, long long int> p;
map<pair<long long int, long long int>, long long int> mp;
int main() {
long long int i, j, k, u, v, w, l, n, t;
n = fin();
while (n--) {
k = fin();
if (k == 1) {
u = fin();
v = fin();
w = fin();
while (v != u) {
if (v < u) {
p.first = u;
p.second = u / 2;
mp[p] += w;
u = u / 2;
} else {
p.first = v;
p.second = v / 2;
mp[p] += w;
v = v / 2;
}
}
} else if (k == 2) {
long long int ans = 0;
u = fin();
v = fin();
while (v != u) {
if (v < u) {
p.first = u;
p.second = u / 2;
ans += mp[p];
u = u / 2;
} else {
p.first = v;
p.second = v / 2;
ans += mp[p];
v = v / 2;
}
}
cout << ans << endl;
}
}
return 0;
}
| 3 |
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main(){
int n,m,s;
while(cin>>n>>m>>s,n){
int N=n*n;
vvi dp(N+1,vi(s+1));
dp[0][0]=1;
loop(k,1,m+1)for(int i=N-1;i>=0;i--)rep(j,s){
if(j+k>s)continue;
dp[i+1][j+k]+=dp[i][j];
dp[i+1][j+k]%=100000;
}
cout<<dp[N][s]<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 4e18;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long a, b;
cin >> a >> b;
long long d = abs(a - b);
if (b == a)
cout << 0 << '\n';
else if (b > a) {
if (d & 1)
cout << 1 << '\n';
else
cout << 2 << '\n';
} else {
if (d & 1)
cout << 2 << '\n';
else
cout << 1 << '\n';
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool isnum(string s) {
if (s[0] == '0') return false;
for (char c : s)
if (c > '9' || c < '0') return false;
return true;
}
int n;
vector<pair<string, string> > V;
void dod(string a, string b) { V.push_back(pair<string, string>(a, b)); }
char A[10];
string tost(int x) {
for (int i = 0; i < 10; ++i) A[i] = 0;
sprintf(A, "%d", x);
return string(A);
}
int T[120000];
int main() {
scanf("%d", &n);
vector<string> ex, re;
for (int i = 0; i < n; ++i) {
int t;
scanf("%s%d", A, &t);
string s(A);
if (t == 0)
re.push_back(s);
else
ex.push_back(s);
}
vector<string> res, exs;
for (string s : ex) {
if (isnum(s) && atoi(s.c_str()) <= n) {
int k = atoi(s.c_str());
if (k <= ex.size())
T[k] = 1;
else
T[k] = 3;
} else
exs.push_back(s);
}
for (string s : re) {
if (isnum(s) && atoi(s.c_str()) <= n) {
int k = atoi(s.c_str());
if (k > ex.size())
T[k] = 1;
else
T[k] = 2;
} else
res.push_back(s);
}
stack<int> Fe;
stack<int> Fr;
stack<int> Ce;
stack<int> Cr;
for (int i = 1; i <= ex.size(); ++i) {
if (T[i] == 0)
Fe.push(i);
else {
if (T[i] == 2) Cr.push(i);
}
}
for (int i = ex.size() + 1; i <= n; ++i) {
if (T[i] == 0)
Fr.push(i);
else {
if (T[i] == 3) Ce.push(i);
}
}
while (Ce.size() > 0 || Cr.size() > 0) {
if (Fe.size() == 0 && Fr.size() == 0) {
string s = "rgassg";
dod(tost(Ce.top()), s);
Fr.push(Ce.top());
Ce.pop();
exs.push_back(s);
}
while (Ce.size() > 0 && Fe.size() > 0) {
dod(tost(Ce.top()), tost(Fe.top()));
Fr.push(Ce.top());
Ce.pop();
Fe.pop();
}
while (Cr.size() > 0 && Fr.size() > 0) {
dod(tost(Cr.top()), tost(Fr.top()));
Fe.push(Cr.top());
Cr.pop();
Fr.pop();
}
}
for (string s : exs) {
dod(s, tost(Fe.top()));
Fe.pop();
}
for (string s : res) {
dod(s, tost(Fr.top()));
Fr.pop();
}
printf("%d\n", V.size());
for (auto v : V) printf("move %s %s\n", v.first.c_str(), v.second.c_str());
}
| 3 |
#include <bits/stdc++.h>
int main() {
int a, b, m, s = 0;
scanf("%d %d", &a, &b);
s = a;
while (1) {
m = a / b;
a = m + (a % b);
s += m;
if (a < b) break;
}
printf("%d", s);
return 0;
}
| 1 |
#include <iostream>
#include <vector>
#include <climits>
#include <functional>
template<typename T>
struct segment_tree {
long long N; // 木の頂点数
long long M; // 葉の数
std::vector<T> V; // セグメント木 1-indexed
std::vector<T> W; // 遅延配列 1-indexed
std::vector<bool> flag; // true : W[i] の子孫に更新していない遅延配列が存在
std::function<T(T, T)> op1; // 二項演算
std::function<T(T, T, T)> op2; // 遅延伝播用の二項演算
std::function<T(T, T)> op3; // 遅延配列更新用の二項演算
T id1; // 単位元
T id2; // 遅延伝播用の単位元
// ===============================================================
segment_tree(
std::vector<T>& a,
std::function<T(T, T)> f,
std::function<T(T, T, T)> g,
std::function<T(T, T)> h,
T t1, T t2
) : op1(f), op2(g), op3(h), id1(t1), id2(t2) {
M = 1;
while (M < a.size()) M *= 2;
N = 2 * M;
V.resize(N, id1); W.resize(N, id2); flag.resize(N, false);
for (long long i = 0; i < a.size(); i++) V[M + i] = a[i];
for (long long i = M - 1; i > 0; i--) {
V[i] = op1(V[2 * i], V[2 * i + 1]);
}
}
segment_tree() {}
// ===============================================================
void init(
std::vector<T>& a,
std::function<T(T, T)> f,
std::function<T(T, T, T)> g,
std::function<T(T, T)> h,
T t1, T t2
) {
op1 = f; op2 = g; op3 = h; id1 = t1; id2 = t2;
M = 1;
while (M < a.size()) M *= 2;
N = 2 * M;
V.resize(N, id1); W.resize(N, id2); flag.resize(N, false);
for (long long i = 0; i < a.size(); i++) V[M + i] = a[i];
for (long long i = M - 1; i > 0; i--) {
V[i] = op1(V[2 * i], V[2 * i + 1]);
}
}
void laze_eval(long long cur_node, long long width) {
if (W[cur_node] != id2) {
V[cur_node] = op2(V[cur_node], W[cur_node], width);
if (width > 1) {
W[cur_node * 2] = op3(W[cur_node * 2], W[cur_node]);
W[cur_node * 2 + 1] = op3(W[cur_node * 2 + 1], W[cur_node]);
}
W[cur_node] = id2;
}
}
void range_update(
long long l, long long r, T x,
long long cur_node = 1,
long long cur_left = 0, long long cur_right = -1
) {
if (cur_right == -1) cur_right = M;
long long width = cur_right - cur_left;
if (cur_right <= l || r <= cur_left) return;
if (l <= cur_left && cur_right <= r) {
W[cur_node] = op3(W[cur_node], x);
}
else {
laze_eval(cur_node, width);
flag[cur_node] = true;
long long mid = (cur_left + cur_right) / 2;
range_update(l, r, x, cur_node * 2, cur_left, mid);
range_update(l, r, x, cur_node * 2 + 1, mid, cur_right);
V[cur_node] = op1(V[cur_node * 2], V[cur_node * 2 + 1]);
}
}
T range_fold(
long long l, long long r,
long long cur_node = 1,
long long cur_left = 0, long long cur_right = -1
) {
if (cur_right == -1) cur_right = M;
long long width = cur_right - cur_left;
if (cur_right <= l || r <= cur_left) return id1;
laze_eval(cur_node, width);
if (width == 1) {
flag[cur_node] = false;
return V[cur_node];
}
long long mid = (cur_left + cur_right) / 2;
if (l <= cur_left && cur_right <= r && !flag[cur_node]) {
return V[cur_node];
}
else if (l <= cur_left && cur_right <= r) {
flag[cur_node] = false;
range_fold(cur_left, mid);
range_fold(mid, cur_right);
return V[cur_node] = op1(V[cur_node * 2], V[cur_node * 2 + 1]);
}
return op1(
range_fold(l, r, cur_node * 2, cur_left, mid),
range_fold(l, r, cur_node * 2 + 1, mid, cur_right)
);
}
};
int main() {
long long n, q;
std::cin >> n >> q;
std::vector<long long> a(n, 0);
auto f = [] (long long x, long long y) {return x + y;};
auto g = [] (long long x, long long y, long long z) {return y * z;};
auto h = [] (long long x, long long y) {return y;};
segment_tree<long long> sg(a, f, g, h, 0, LLONG_MAX);
while (q--) {
long long z, s, t;
std::cin >> z >> s >> t;
if (z == 0) {
long long x;
std::cin >> x;
sg.range_update(s, t + 1, x);
}
else std::cout << sg.range_fold(s, t + 1) << '\n';
}
}
| 0 |
#include<bits/stdc++.h>
#define mxn 1000010
#define LL long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define pii pair<int,int>
#define mp make_pair
#define fr first
#define sc second
#define pb push_back
#define node(a,b,c) (node){a,b,c}
using namespace std;
const int inf=1e9,mod=1e9+7;
int n,m,sl,fh,top,ans,f[mxn],dp[110][mxn];
int rd()
{
sl=0;fh=1;
char ch=getchar();
while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
return sl*fh;
}
void upd(int &x,int y) {x+=y; if(x>=mod) x-=mod;}
int main()
{
n=rd();m=rd();
for(int r,l=1;l<=n;l=r+1)
{
r=min(n,n/(n/l));
f[++top]=r-l+1;
}
for(int i=1;i<=top;++i) dp[1][i]=f[i];
for(int i=2;i<=m;++i)
for(int j=1;j<=top;++j)
upd(dp[i-1][j],dp[i-1][j-1]),dp[i][top-j+1]=1ll*dp[i-1][j]*f[top-j+1]%mod;
for(int i=1;i<=top;++i) upd(ans,dp[m][i]);
printf("%d\n",ans);
return 0;
} | 0 |
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
string str;
int last[256];
int cost[216000];
int nxt[216000];
char chr[216000];
int main(){
cin >> str;
fill(last, last + 256, str.size() + 1);
cost[str.size() + 1] = 0;
for(int i = str.size();i >= 0;i--){
cost[i] = 216000;
for(int j = 'a';j <= 'z';j++){
int to = last[j];
if(cost[to] + 1 < cost[i]){
cost[i] = cost[to] + 1;
nxt[i] = to;
chr[i] = j;
}
}
if(i > 0)last[str[i-1]] = i;
}
string ans = "";
int now = 0;
while(now <= str.size()){
ans += chr[now];
now = nxt[now];
}
cout << ans << endl;
return 0;
} | 0 |
#include<cstdio>
#include<cstring>
const int N=2005;
int n,m,he[N],to[N*2],nx[N*2],f[N][N],g[N],h[N],fa[N],sz[N],a=1e9;
inline void wk(int w,int x,int y){to[w]=y,nx[w]=he[x],he[x]=w;}
inline int mn(int x,int y){return x<y?x:y;}
void dfs(int x)
{
memset(f[x],0x3f,sizeof(f[x])),f[x][0]=0,sz[x]=1;
for(int i=he[x],y;i;i=nx[i])if((y=to[i])!=fa[x])
{
fa[y]=x,dfs(y),sz[x]+=sz[y],g[0]=f[x][0],h[0]=f[y][0];
for(int j=1;j<=m;++j)g[j]=mn(g[j-1],f[x][j]),h[j]=mn(h[j-1],f[y][j]);
f[x][m]=mn(f[x][m]+f[y][0]+1,f[y][m-1]+f[x][0]);
for(int j=m-1;j;--j)f[x][j]=mn(f[x][j]+h[mn(j-1,m-j-1)],f[y][j-1]+g[mn(j,m-j)]);//
f[x][0]+=f[y][0]+1;
}
for(int i=0;i<=m;++i)a=mn(a,n-sz[x]+f[x][i]);
}
int main()
{
// freopen("t.in","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1,x,y;i<n;++i)
scanf("%d%d",&x,&y),wk(i*2,x,y),wk(i*2+1,y,x);
return dfs(1),printf("%d",a),0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
int n;
bool del[MAXN];
int a[MAXN], to[MAXN];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 0; i < n; ++i) to[i] = i + 1;
vector<int> v;
for (int i = n - 1; i >= 0; --i) {
if (to[i] < n && a[to[i]] < a[i]) v.push_back(i);
}
int ans = 0;
while (v.size()) {
++ans;
vector<int> nv;
for (int i : v) {
if (del[i]) continue;
del[to[i]] = 1;
to[i] = to[to[i]];
if (to[i] < n && a[to[i]] < a[i]) nv.push_back(i);
}
v = nv;
}
cout << ans << '\n';
return 0;
}
| 2 |
//g++ -Wall -Werror -O2 -std=c++11 main.cpp -o main && main.exe < in.txt
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
int main() {
ios::sync_with_stdio(false);
int N, Q;
string S;
cin >> N >> S >> Q;
for (int i = 0; i < Q; i++) {
int k;
cin >> k;
ll a = 0;
ll b = 0;
ll m = 0;
ll ans = 0;
for (int j = 0; j < N; j++) {
if (S[j] == 'D') {
a++;
} else if (S[j] == 'M') {
b += a;
m++;
} else if (S[j] == 'C') {
ans += b;
}
if (j >= k - 1) {
if (S[j-k+1] == 'D') {
a--;
b -= m;
} else if (S[j-k+1] == 'M') {
m--;
}
}
}
cout << ans << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
while (q--) {
int b, w;
cin >> b >> w;
if (b > 3 * w + 1 || w > 3 * b + 1) {
cout << "NO\n";
continue;
}
cout << "YES\n";
int sx = 2, sy = 3;
if (b > w) {
++sx;
swap(b, w);
}
for (int i = 0; i < 2 * b; i++) cout << sx << " " << sy + i << endl;
w -= b;
if (w > 0) {
cout << sx << " " << sy - 1 << endl;
--w;
}
for (int i = 0; i < b && w > 0; i++) {
cout << sx - 1 << " " << sy + i * 2 << endl;
--w;
}
for (int i = 0; i < b && w > 0; i++) {
cout << sx + 1 << " " << sy + i * 2 << endl;
--w;
}
}
}
| 5 |
#include<bits/stdc++.h>
const int N = 200010;
int a[N], left[N], right[N];
int main(){
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++ i){
scanf("%d", &a[i]);
}
std::stack <int> s;
for (int i = 0; i <= n + 1; ++ i){
while (!s.empty() && a[s.top()] > a[i]){
right[s.top()] = i;
s.pop();
}
if (!s.empty()) left[i] = s.top();
s.push(i);
}
long long ans = 0;
for (int i = 1; i <= n; ++ i){
ans += 1ll * a[i] * (right[i] - i) * (i - left[i]);
}
printf("%lld\n", ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long a, b;
string s, t;
bool chk(string s) {
int ss = 0;
for (auto c : s) ss += c == 0;
for (auto c : t) ss -= c == 0;
if (ss) return 0;
if (t.find(s) != t.npos) return 1;
return 0;
}
int main() {
cin >> a >> b;
if (a == b) {
cout << "YES\n";
return 0;
}
while (a) s.push_back(a & 1), a >>= 1;
while (b) t.push_back(b & 1), b >>= 1;
reverse(s.begin(), s.end());
reverse(t.begin(), t.end());
if (!t.back()) {
cout << "NO\n";
return 0;
}
string ss = s;
ss.push_back(1);
if (chk(ss)) {
cout << "YES\n";
return 0;
}
reverse(ss.begin(), ss.end());
if (chk(ss)) {
cout << "YES\n";
return 0;
}
while (!s.back()) s.pop_back();
if (chk(s)) {
cout << "YES\n";
return 0;
}
reverse(s.begin(), s.end());
if (chk(s)) {
cout << "YES\n";
return 0;
}
cout << "NO\n";
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
int const N = 5e5 + 20, mod = 998244353;
int n, dp[N][2], ans, sz[N], pw[N];
vector<int> g[N];
inline void add(int &a, int b) {
a += b;
if (a >= mod) a -= mod;
}
void dfs(int v, int par = -1) {
sz[v] = 1;
int d0 = 1, d1 = 0;
for (int u : g[v]) {
if (u == par) continue;
dfs(u, v);
sz[v] += sz[u];
add(dp[u][1], dp[u][1]);
add(dp[u][1], dp[u][0]);
d1 = (1ll * d0 * dp[u][0] + 1ll * d1 * (dp[u][0] + dp[u][1])) % mod;
d0 = 1ll * d0 * dp[u][1] % mod;
}
dp[v][0] = d0, dp[v][1] = d1;
add(ans, 1ll * pw[n - sz[v] + 1] * d1 % mod);
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
pw[0] = 1;
for (int i = 1; i < N; i++) pw[i] = 2 * pw[i - 1] % mod;
cin >> n;
for (int i = 0, u, v; i < n - 1; i++) {
cin >> u >> v;
u--, v--;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(0);
cout << ans << '\n';
}
| 5 |
#include <bits/stdc++.h>
int a[1001];
int main() {
int n, m = -1, M = 1e7, c = 0, x;
scanf("%d", &n);
while (n--) scanf("%d", &x), x > m ? m = x, c++ : 0, x < M ? M = x, c++ : 0;
printf("%d", c - 2);
}
| 1 |
#include<iostream>
using namespace std;
int main()
{
string s,t;
cin>>s>>t;
int s1=s.size(),t1=t.size();
string dp[2][t1+1];
for(int i=0;i<=t1;++i)
dp[0][i]="";
for(int i=1;i<=s1;++i)
{
dp[1][0]="";
dp[0][0]="";
for(int j=1;j<=t1;++j)
{
if(s[i-1]==t[j-1])
{
dp[1][j]=dp[0][j-1];
dp[1][j]+=s[i-1];
}
else
{
if(dp[0][j].size()>dp[1][j-1].size())
dp[1][j]=dp[0][j];
else
dp[1][j]=dp[1][j-1];
}
}
for(int k=0;k<=t1;++k)
dp[0][k]=dp[1][k];
}
cout<<dp[1][t1];
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,mi=100001,m,sum=0,x;
cin>>n>>x;
for(int i=0;i<n;i++){
cin>>m;
mi=min(mi,m);
sum+=m;
}
cout<<n+(x-sum)/mi<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool prime[1000000 + 1];
void SieveOfEratosthenes() {
memset(prime, true, sizeof(prime));
prime[1] = false;
for (int p = 2; p * p <= 1000000; p++) {
if (prime[p] == true) {
for (int i = p * 2; i <= 1000000; i += p) prime[i] = false;
}
}
}
long long int gcd(long long int a, long long int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int modexp(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int lcm(long long int a, long long int b) {
return (a * b) / gcd(a, b);
}
long long int mypow(long long int b, long long int e) {
long long int ans = 1;
for (; e; b = b * b % 1000000007, e /= 2)
if (e & 1) ans = ans * b % 1000000007;
return ans;
}
long long int power2(long long int x, long long int y) {
if (y == 0)
return 1;
else if (y % 2 == 0)
return power2(x, y / 2) * power2(x, y / 2);
else
return x * power2(x, y / 2) * power2(x, y / 2);
}
long long int conv(char c) { return (long long int)(c - 'a'); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n, k;
cin >> n >> k;
long long int a[n * k];
for (long long int i = 0, _n = (n * k); i < _n; i++) cin >> a[i];
long long int ans = 0;
long long int med = (n % 2 == 0) ? (n / 2) : (n / 2 + 1);
long long int rem = n - med + 1;
med--;
long long int count = 1;
long long int i = 0;
long long int j = (n * k - 1);
if (j == 0) {
cout << a[0] << endl;
continue;
}
while (i < j) {
while (count < rem) {
j--;
count++;
}
ans += a[j];
count = 1;
while (count <= med) {
i++;
count++;
}
count = 0;
}
cout << ans << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
char s[101];
char vowels[] = {'A', 'E', 'I', 'O', 'U', 'Y', '\0'};
int main() {
scanf("%s", s);
int ans = 0;
int cur = -1;
for (int i = 0; i < strlen(s); ++i) {
for (int j = 0; j < strlen(vowels); ++j) {
if (s[i] == vowels[j]) {
if (ans < i - cur) {
ans = i - cur;
}
cur = i;
break;
}
}
}
if (ans < strlen(s) - cur) {
ans = strlen(s) - cur;
}
printf("%d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
#ifdef __WIN32
#define LLFORMAT "I64"
#define Rand() ((rand() << 15) + rand())
#else
#define LLFORMAT "ll"
#define Rand() (rand())
#endif
using namespace std;
const int maxn = 1e5 + 10;
int n, rt, dp[maxn];
vector<int> g[maxn];
int dfs(int u, int p = 0) {
int f = 0;
for (int &v: g[u]) {
if(v != p) {
int t = dfs(v, u);
if(t) {
dp[u] += t;
}
else {
dp[u] += f;
f = 1;
}
}
}
return dp[u];
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; ++i) {
int u, v;
scanf("%d%d", &u, &v);
++u;
++v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
if(g[i].size() > 2) {
rt = i;
break;
}
}
if(!rt) {
puts("1");
return 0;
}
printf("%d\n", dfs(rt));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int h[1010];
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) scanf("%d", &h[i]);
int ans = 1, de = 1, num = 1, su = 5000;
for (int i = 1; i <= 1000; i++) {
int w = i - k;
de = num = 0;
int j;
for (j = 1; j <= n; j++) {
w += k;
if (w != h[j]) ++num;
if (w > h[j]) ++de;
}
if (su > num) {
ans = i;
su = num;
}
if (de >= n) break;
}
int w = ans - k;
int c;
cout << su << endl;
for (int i = 1; i <= n; i++) {
w += k;
c = w - h[i];
if (c < 0) {
c = 0 - c;
cout << "- " << i << " " << c << endl;
} else if (c > 0) {
cout << "+ " << i << " " << c << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){long long x,y;cin>>x>>y;cout<<floor(log2((long double)(y/x)))+1<<endl;} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, s;
cin >> a >> b >> s;
int sum = abs(a) + abs(b);
int temp = (sum - s) % 2;
if (!temp && s >= sum)
cout << "Yes\n";
else
cout << "No\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long f(long long x, long long y, long long n, long long c) {
if (c == -1) return 0LL;
long long ret = (c + 1) * (c + 1) + c * c;
if (c > x - 1) ret -= (c - x + 1) * (c - x + 1);
if (c > n - x) ret -= (c + x - n) * (c + x - n);
if (c > y - 1) ret -= (c - y + 1) * (c - y + 1);
if (c > n - y + 1) ret -= (c + y - n) * (c + y - n);
if (c > x + y - 1) ret += (c - x - y + 1) * (c - x - y + 2) / 2;
if (c > x + n - y) ret += (c + y - x - n) * (c + y - x - n + 1) / 2;
if (c > n - x + y) ret += (c + x - y - n) * (c + x - y - n + 1) / 2;
if (c > 2 * n - x - y + 1)
ret += (c + x + y - 2 * n) * (c + x + y - 2 * n - 1) / 2;
return ret;
}
long long pesquisa(long long x, long long y, long long n, long long c) {
long long ini = 0LL, fim = (long long)1000000000, meio = (ini + fim) >> 1;
for (; ini <= fim; meio = (ini + fim) >> 1) {
if (f(x, y, n, meio) >= c && f(x, y, n, meio - 1) < c)
return meio;
else if (f(x, y, n, meio) < c)
ini = meio + 1;
else
fim = meio - 1;
}
}
int main() {
long long n, x, y, c;
cin >> n >> x >> y >> c;
cout << pesquisa(x, y, n, c) << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
struct node {
int l, r;
};
node a[N];
int vis[N];
int v1[N], v2[N];
int solve(int x, int y) {
if (a[x].l > a[y].l) {
swap(x, y);
}
if (a[x].r < a[y].l) return 0;
if (a[x].r > a[y].r) {
return v2[a[y].r] - v2[a[y].l - 1];
}
return v2[a[x].r] - v2[a[y].l - 1];
}
int main() {
int n, q;
cin >> n >> q;
for (int i = 1; i <= q; i++) {
cin >> a[i].l >> a[i].r;
for (int j = a[i].l; j <= a[i].r; j++) {
vis[j]++;
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (vis[i] > 0) ans++;
if (vis[i] == 1) v1[i] = 1;
if (vis[i] == 2) v2[i] = 1;
v1[i] += v1[i - 1];
v2[i] += v2[i - 1];
}
int res = 10000;
for (int i = 1; i <= q; i++) {
for (int j = 1; j <= q; j++) {
if (i == j) continue;
int tt = 0;
tt += v1[a[i].r] - v1[a[i].l - 1];
tt += v1[a[j].r] - v1[a[j].l - 1];
tt += solve(i, j);
res = min(res, tt);
}
}
cout << ans - res << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int size = 110000, mod = 1000000009;
long long cal(long long a, int n, int m) {
long long s = 1;
while (n) {
if (n & 1) s = (s * a) % m;
a = (a * a) % m;
n /= 2;
}
return s;
}
int main() {
int n, m;
cin >> n >> m;
long long ans = 1, tmp = cal(2, m, mod);
for (int i = 1; i < n + 1; i++) ans = (ans * (tmp - i)) % mod;
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[101];
int edge[101][2];
int len_graph[101];
int graph[103][100];
bool prime[32001];
int cnt[101];
int cap[103][103];
int rsd[103][103];
int path[103];
bool flag[103];
bool has_aug_path(int from, int to, int path[], int &len_path) {
path[len_path++] = from;
if (from == to) return true;
if (flag[from]) {
len_path--;
return false;
}
flag[from] = true;
for (int i = 0; i < len_graph[from]; i++) {
int target = graph[from][i];
if (rsd[from][target] > 0 && has_aug_path(target, to, path, len_path))
return true;
}
len_path--;
return false;
}
int max_flow(int from, int to) {
int len_path = 0;
int res = 0;
for (int i = 1; i <= n + 2; i++) flag[i] = false;
while (has_aug_path(from, to, path, len_path)) {
int min = rsd[path[0]][path[1]];
for (int i = 2; i < len_path; i++)
if (rsd[path[i - 1]][path[i]] < min) min = rsd[path[i - 1]][path[i]];
res += min;
for (int i = 1; i < len_path; i++) {
rsd[path[i - 1]][path[i]] -= min;
rsd[path[i]][path[i - 1]] += min;
}
for (int i = 1; i <= n + 2; i++) flag[i] = false;
len_path = 0;
}
return res;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) len_graph[i] = 0;
for (int i = 1; i <= m; i++) {
int from, to;
cin >> from >> to;
if (to % 2) swap(from, to);
edge[i][0] = from;
edge[i][1] = to;
graph[from][len_graph[from]++] = to;
graph[to][len_graph[to]++] = from;
}
for (int i = 1; i <= n; i++)
if (i % 2) {
graph[n + 1][len_graph[n + 1]++] = i;
graph[i][len_graph[i]++] = n + 1;
} else {
graph[i][len_graph[i]++] = n + 2;
graph[n + 2][len_graph[n + 2]++] = i;
}
for (int i = 2; i <= 32000; i++) prime[i] = true;
for (int i = 2; i <= 200; i++)
if (prime[i])
for (int j = i * i; j <= 32000; j += i) prime[j] = false;
for (int i = 1; i <= n + 2; i++)
for (int j = 1; j <= n + 2; j++) cap[i][j] = rsd[i][j] = 0;
int res = 0;
for (int p = 2; p <= 32000; p++)
if (prime[p]) {
for (int i = 1; i <= n; i++) cnt[i] = 0;
for (int i = 1; i <= n; i++)
while (a[i] % p == 0) {
cnt[i]++;
a[i] /= p;
}
for (int i = 1; i <= m; i++) {
int from = edge[i][0];
int to = edge[i][1];
cap[from][to] = min(cnt[from], cnt[to]);
rsd[from][to] = cap[from][to];
rsd[to][from] = 0;
}
for (int i = 1; i <= n; i += 2) {
cap[n + 1][i] = cnt[i];
rsd[n + 1][i] = cap[n + 1][i];
rsd[i][n + 1] = 0;
}
for (int i = 2; i <= n; i += 2) {
cap[i][n + 2] = cnt[i];
rsd[i][n + 2] = cap[i][n + 2];
rsd[n + 2][i] = 0;
}
res += max_flow(n + 1, n + 2);
}
set<int> work_list;
for (int i = 1; i <= n; i++)
if (a[i] != 1) work_list.insert(a[i]);
for (set<int>::iterator it = work_list.begin(); it != work_list.end(); it++) {
int p = *it;
for (int i = 1; i <= n; i++) cnt[i] = 0;
for (int i = 1; i <= n; i++)
while (a[i] % p == 0) {
cnt[i]++;
a[i] /= p;
}
for (int i = 1; i <= m; i++) {
int from = edge[i][0];
int to = edge[i][1];
cap[from][to] = min(cnt[from], cnt[to]);
rsd[from][to] = cap[from][to];
rsd[to][from] = 0;
}
for (int i = 1; i <= n; i += 2) {
cap[n + 1][i] = cnt[i];
rsd[n + 1][i] = cap[n + 1][i];
rsd[i][n + 1] = 0;
}
for (int i = 2; i <= n; i += 2) {
cap[i][n + 2] = cnt[i];
rsd[i][n + 2] = cap[i][n + 2];
rsd[n + 2][i] = 0;
}
res += max_flow(n + 1, n + 2);
}
cout << res << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool check(long long int mid, vector<pair<long long int, long long int>> v,
long long int k) {
long long int c = 0;
for (long long int i = v.size() - 1; i >= 0; i--) {
c += (v[i].first) / mid;
}
if (c >= k) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int t = 1;
while (t--) {
long long int n, k;
cin >> n >> k;
map<long long int, long long int> m;
map<long long int, long long int>::iterator it;
for (long long int a, i = 0; i < n; i++) {
cin >> a;
m[a]++;
}
vector<pair<long long int, long long int>> v;
for (it = m.begin(); it != m.end(); it++) {
v.push_back(make_pair(it->second, it->first));
}
long long int beg = 1, end = n / k;
long long int ans = 1;
while (beg <= end) {
long long int mid = (beg + end) / 2;
if (check(mid, v, k)) {
ans = mid;
beg = mid + 1;
} else
end = mid - 1;
}
long long int c = k;
for (long long int i = v.size() - 1; i >= 0; i--) {
long long int d = v[i].first / ans;
while (c > 0 && d > 0) {
cout << v[i].second << " ";
c--;
d--;
}
}
cout << endl;
}
}
| 4 |
#include<iostream>
int a[99]={11,31,41,61,71,101,131,151,181,191,211,241,251,271,281,311,331,401,421,431,461,491,521,541,571,601,631,641,661,691,701,751,761,811,821,881,911,941,971,991,1021,1031,1051,1061,1091,1151,1171,1181,1201,1231,1291,1301,1321,1361,1381};
int main(){int n;std::cin>>n;for(int i=0;i<n;i++){std::cout<<a[i]<<" "; }} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1LL << 60;
int value[100100], n, k, taken[100100];
long long dist[10010], H;
vector<pair<long long, int> > treasure[10010];
vector<long long> method;
priority_queue<pair<int, int> > reach;
void dijkstra() {
priority_queue<pair<long long, int> > q;
for (int i = 1; i < k; i++) dist[i] = H;
q.push(make_pair(0, 0));
while (!q.empty()) {
int x = q.top().second;
q.pop();
for (int i = 0; i < int(method.size()); i++) {
long long D = dist[x] + method[i];
int y = D % k;
if (dist[y] <= D) continue;
dist[y] = D;
q.push(make_pair(-D, y));
}
}
for (int i = 1; i < k; i++)
while (!treasure[i].empty()) {
pair<long long, int> u = treasure[i].back();
if (u.first < dist[i]) break;
reach.push(make_pair(value[u.second], -u.second));
treasure[i].pop_back();
}
}
int query() {
while (!reach.empty()) {
int i = -reach.top().second, V = reach.top().first;
reach.pop();
if (taken[i]) continue;
if (value[i] != V)
reach.push(make_pair(value[i], -i));
else {
taken[i] = 1;
return V;
}
}
return 0;
}
int main() {
int type, m, x, y;
long long pos, step;
cin >> H >> n >> m >> k;
for (int i = 0; i < n; i++) {
scanf("%I64d%d", &pos, value + i);
pos--;
if (pos % k == 0)
reach.push(make_pair(value[i], -i));
else
treasure[pos % k].push_back(make_pair(pos, i));
}
for (int i = 1; i < k; i++) sort(treasure[i].begin(), treasure[i].end());
while (m--) {
scanf("%d", &type);
if (type == 1) {
scanf("%I64d", &step);
method.push_back(step);
dijkstra();
}
if (type == 2) {
scanf("%d%d", &x, &y);
value[--x] -= y;
}
if (type == 3) printf("%d\n", query());
}
}
| 3 |
#include <iostream>
#include <vector>
using namespace std;
vector<int> g[100005];
int N, u, v;
vector<int> ret(100005);
void dfs(int now, int d, int par){
ret[now] = d;
for(auto next : g[now]){
if(next == par) continue;
dfs(next, d+1, now);
}
}
vector<int> dist(int s){
dfs(s, 0, -1);
return ret;
}
int main(){
cin >> N >> u >> v;
for(int i=0; i<N-1; i++){
int a, b;
cin >> a >> b;
a--; b--;
g[a].push_back(b);
g[b].push_back(a);
}
u--; v--;
vector<int> td = dist(u);
vector<int> ad = dist(v);
int ans = 0;
for(int i=0; i<N; i++){
if(td[i] >= ad[i]) continue;
ans = max(ans, ad[i]);
}
cout << ans-1 << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1E9;
int D, V, N;
pair<int, int> gas[200005];
int d[200005], a[200005], dp[19][200005], lenj[200005];
void rmq() {
for (int i = (0); i < (19); i++) {
for (int len = (1 << i); len < (1 << (i + 1)); len++) {
if (len >= N) break;
lenj[len] = i;
}
}
for (int i = (0); i < (N); i++) dp[0][i] = i;
for (int j = (1); j < (19); j++) {
for (int i = (0); i < (N); i++) {
dp[j][i] = dp[j - 1][i];
int i2 = i + (1 << (j - 1));
if (i2 < N && a[dp[j - 1][i2]] < a[dp[j][i]]) {
dp[j][i] = dp[j - 1][i2];
}
}
}
}
int get(int l, int r) {
if (l > r) swap(l, r);
int j = lenj[r - l + 1];
int p = dp[j][l], q = dp[j][r - (1 << j) + 1];
if (a[p] < a[q])
return p;
else if (a[q] < a[p])
return q;
else
return p < q ? p : q;
}
int main() {
cin >> D >> V >> N;
for (int i = (0); i < (N); i++) {
int x, p;
scanf("%d%d", &x, &p);
gas[i] = pair<int, int>(x, p);
}
gas[N++] = pair<int, int>(D, 0);
sort(gas, gas + N);
for (int i = (0); i < (N); i++) {
d[i] = gas[i].first;
a[i] = gas[i].second;
if (d[i] - (i ? d[i - 1] : 0) > V) {
return puts("-1"), 0;
}
}
rmq();
int nowi = 0, v = V - d[0];
long long cost = 0;
while (nowi < N - 1) {
int maxi = upper_bound(d + nowi, d + N, d[nowi] + V) - d - 1;
int mini = get(nowi + 1, maxi);
if (a[mini] >= a[nowi]) {
cost += (long long)(V - v) * a[nowi];
v = V;
v -= d[mini] - d[nowi];
nowi = mini;
} else {
for (int j = (nowi + 1); j < (mini + 1); j++) {
if (a[j] >= a[nowi]) continue;
int req = d[j] - d[nowi];
if (req > v) {
cost += (long long)(req - v) * a[nowi];
v = req;
}
v -= req;
nowi = j;
}
}
}
cout << cost << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char s[55][55], ss[55][55];
vector<pair<int, int> > path1, path2;
int r, c;
bool check(char s[][55]) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j += 2) {
if (s[i][j] != 'L') return false;
}
}
return true;
}
bool check2(char s[][55]) {
for (int i = 0; i < r; i += 2) {
for (int j = 0; j < c; j++) {
if (s[i][j] != 'U') return false;
}
}
return true;
}
bool isUU(char s[][55], int y, int x) {
if (x + 1 < c && s[y][x] == 'U' && s[y][x + 1] == 'U') return true;
return false;
}
bool isLL(char s[][55], int y, int x) {
if (y + 1 < r && s[y][x] == 'L' && s[y + 1][x] == 'L') return true;
return false;
}
void toHor(vector<pair<int, int> > &path, char s[][55], int y, int x) {
path.push_back(make_pair(y, x));
s[y][x] = s[y + 1][x] = 'L';
s[y][x + 1] = s[y + 1][x + 1] = 'R';
}
void toVer(vector<pair<int, int> > &path, char s[][55], int y, int x) {
path.push_back(make_pair(y, x));
s[y][x] = s[y][x + 1] = 'U';
s[y + 1][x] = s[y + 1][x + 1] = 'D';
}
void print(char s[][55]) {
puts("\nDEBUG");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) printf("%c", s[i][j]);
puts("");
}
}
void change(vector<pair<int, int> > &path, char s[][55], int y, int x,
bool type) {
if (x >= c) {
change(path, s, y + 1, 0, false);
return;
}
if (y >= r) return;
if (!type && x + 1 < c && s[y][x] == 'U' && s[y][x + 1] == 'L') {
change(path, s, y, x + 1, true);
if (s[y][x + 1] == 'L') toVer(path, s, y, x + 1);
toHor(path, s, y, x);
change(path, s, y, x + 2, false);
return;
}
if (!type && isUU(s, y, x)) {
toHor(path, s, y, x);
change(path, s, y, x + 2, false);
return;
}
if (!type && s[y][x] == 'L') {
change(path, s, y, x + 2, false);
return;
}
if (isUU(s, y, x)) {
toHor(path, s, y, x);
return;
}
if (isLL(s, y, x)) {
toVer(path, s, y, x);
return;
}
if (s[y][x] == 'L') {
change(path, s, y + 1, x, true);
if (s[y + 1][x] == 'U') toHor(path, s, y + 1, x);
toVer(path, s, y, x);
} else {
change(path, s, y, x + 1, true);
if (s[y][x + 1] == 'L') toVer(path, s, y, x + 1);
toHor(path, s, y, x);
}
}
void change2(vector<pair<int, int> > &path, char s[][55], int y, int x,
bool type) {
if (y >= r) {
change2(path, s, 0, x + 1, false);
return;
}
if (x >= c) return;
if (!type && y + 1 < r && s[y][x] == 'L' && s[y + 1][x] == 'U') {
change2(path, s, y + 1, x, true);
if (s[y + 1][x] == 'U') toHor(path, s, y + 1, x);
toVer(path, s, y, x);
change2(path, s, y + 2, x, false);
return;
}
if (!type && isLL(s, y, x)) {
toVer(path, s, y, x);
change2(path, s, y + 2, x, false);
return;
}
if (!type && s[y][x] == 'U') {
change2(path, s, y + 2, x, false);
return;
}
if (isUU(s, y, x)) {
toHor(path, s, y, x);
return;
}
if (isLL(s, y, x)) {
toVer(path, s, y, x);
return;
}
if (s[y][x] == 'L') {
change2(path, s, y + 1, x, true);
if (s[y + 1][x] == 'U') toHor(path, s, y + 1, x);
toVer(path, s, y, x);
} else {
change2(path, s, y, x + 1, true);
if (s[y][x + 1] == 'L') toVer(path, s, y, x + 1);
toHor(path, s, y, x);
}
}
int main() {
cin >> r >> c;
for (int i = 0; i < r; i++) cin >> s[i];
for (int i = 0; i < r; i++) cin >> ss[i];
bool flag = true;
if (c % 2 == 0) {
change(path1, s, 0, 0, false);
if (!check(s))
flag = false;
else {
change(path2, ss, 0, 0, false);
if (!check(ss)) flag = false;
}
} else {
change2(path1, s, 0, 0, false);
if (!check2(s))
flag = false;
else {
change2(path2, ss, 0, 0, false);
if (!check2(ss)) flag = false;
}
}
if (!flag)
puts("-1");
else {
printf("%d\n", path1.size() + path2.size());
for (int i = 0; i < path1.size(); i++)
printf("%d %d\n", path1[i].first + 1, path1[i].second + 1);
for (int i = path2.size() - 1; i >= 0; i--)
printf("%d %d\n", path2[i].first + 1, path2[i].second + 1);
}
return 0;
}
| 4 |
#include<bits/stdc++.h>
#define inf 1000000000
#define N 2009
using namespace std;
int n,cnt,f[N],g[N]; char a[N],b[N];
struct node{ int x,y; }c[N];
bool cmp(node u,node v){ return u.x<v.x; }
int calc(int x,int y,int u,int v){
return x+y+min(y+abs(x-u),x+abs(y-v));
}
int main(){
scanf("%s%s",a,b); n=strlen(a);
int i,j,x;
for (i=0; i<n; i++) if (b[i]=='1') break;
if (i>=n){
for (i=0; i<n; i++) if (a[i]=='1') break;
puts(i<n?"-1":"0"); return 0;
}
int ans=inf;
for (i=n-1; i>=0; i--) if (b[i]=='1'){ f[n-1]=n-1-i; break; }
for (i=0; i+1<n; i++) f[i]=(b[i]=='1'?0:f[(i-1+n)%n]+1);
for (i=0; i<n; i++) if (b[i]=='1'){ g[0]=i; break; }
for (i=n-1; i; i--){
g[i]=(b[i]=='1'?0:g[(i+1)%n]+1);
}
for (i=0; i<n; i++){
for (j=cnt=0; j<n; j++) if (a[j]!=b[(i+j)%n])
c[++cnt]=(node){f[j],g[j]};
sort(c+1,c+cnt+1,cmp);
for (j=cnt,x=0; j>=0; j--){
ans=min(ans,cnt+calc(x,c[j].x,i,(n-i)%n));
x=max(x,c[j].y);
}
}
printf("%d\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
template <class T>
inline void read(T &res)
{
char ch; bool flag = false; res = 0;
while (ch = getchar(), !isdigit(ch) && ch != '-');
ch == '-' ? flag = true : res = ch ^ 48;
while (ch = getchar(), isdigit(ch))
res = res * 10 + ch - 48;
flag ? res = -res : 0;
}
const int N = 1e7 + 5;
const int mod = 998244353;
int fra[N], inv[N], ex[N], n;
inline int quick_pow(int x, int k)
{
int res = 1;
while (k)
{
if (k & 1)
res = 1ll * res * x % mod;
x = 1ll * x * x % mod;
k >>= 1;
}
return res;
}
inline void add(int &x, int y)
{
x += y;
x >= mod ? x -= mod : 0;
}
inline void dec(int &x, int y)
{
x -= y;
x < 0 ? x += mod : 0;
}
inline int C(int n, int m)
{
return 1ll * fra[n] * inv[m] % mod * inv[n - m] % mod;
}
int main()
{
read(n);
fra[0] = 1;
for (int i = 1; i <= n; ++i)
fra[i] = 1ll * fra[i - 1] * i % mod;
inv[n] = quick_pow(fra[n], mod - 2);
for (int i = n; i >= 1; --i)
inv[i - 1] = 1ll * inv[i] * i % mod;
ex[0] = 1;
for (int i = 1; i <= n; ++i)
add(ex[i] = ex[i - 1], ex[i - 1]);
int half = n >> 1, ans = quick_pow(3, n);
for (int i = half + 1; i <= n; ++i)
dec(ans, 2ll * C(n, i) * ex[n - i] % mod);
printf("%d\n", ans);
} | 0 |
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <sstream>
#include <cmath>
#include <climits>
#include <set>
#include <iostream>
#include <map>
#include <functional>
#include <cstdlib>
#include <numeric>
#include <queue>
#include <complex>
#include <sstream>
#include <stack>
using namespace std;
#define reep(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reep(i,0,n)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef complex<double> Point;
const char* strs = "abcdefghijklmnopqrstuvwxyz.?! ";
int main()
{
char str[256];
while(scanf("%s", str) != EOF){
int n = strlen(str);
char output[256] = {0};
bool ok = true;
for(int i=0; i<n; i+=2){
if(i+1 == n){
ok = false;
break;
}
int a = str[i] - '1';
int b = str[i+1] - '1';
if(a<0 || 6<=a || b<0 || 5<=b){
ok = false;
break;
}
output[i/2] = strs[a*5+b];
}
puts(ok ? output : "NA");
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct pq {
int st, ed;
long long tot_ac;
int f;
pq() {}
pq(int i, int j, long long k) { st = i, ed = j, tot_ac = k, f = 0; }
};
pq fib[100];
pq find_sol(pq a, pq b) {
if (a.ed != -1 && b.ed != -1) {
if (a.ed == 1 && b.st == 2)
return pq(a.st, b.ed, a.tot_ac + b.tot_ac + 1);
else
return pq(a.st, b.ed, a.tot_ac + b.tot_ac);
}
if (a.ed == -1 && b.ed != -1) {
if (a.st == 1 && b.st == 2)
return pq(a.st, b.ed, a.tot_ac + b.tot_ac + 1);
else
return pq(a.st, b.ed, a.tot_ac + b.tot_ac);
}
if (a.ed != -1 && b.ed == -1) {
if (a.ed == 1 && b.st == 2)
return pq(a.st, b.st, a.tot_ac + b.tot_ac + 1);
else
return pq(a.st, b.st, a.tot_ac + b.tot_ac);
}
if (a.ed == -1 && b.ed == -1) {
if (a.st == 1 && b.st == 2)
return pq(a.st, b.st, a.tot_ac + b.tot_ac + 1);
else
return pq(a.st, b.st, a.tot_ac + b.tot_ac);
}
return pq(0, 0, 0);
}
bool calc(int k, long long x) {
for (int i = 3; i <= k; i++) {
fib[i] = find_sol(fib[i - 2], fib[i - 1]);
}
if (fib[k].tot_ac == x) return true;
return false;
}
string make_string(pq a, int len) {
string ret = "";
if (a.tot_ac * 2 == len && len > 2) {
for (int i = 0; i < a.tot_ac; i++) ret += "AC";
return ret;
} else if (len - a.tot_ac * 2 == 1 && len > 2) {
for (int i = 0; i < a.tot_ac; i++) ret += "AC";
if (a.f == 1) {
if (a.st == 0)
ret = "Z" + ret;
else if (a.st == 1)
ret = "A" + ret;
else
ret = "C" + ret;
} else {
if (a.st == 0)
ret = ret + "Z";
else if (a.ed == 1)
ret = ret + "A";
else
ret = ret + "C";
}
return ret;
}
if (a.ed == -1) {
if (a.st == 0)
ret += "Z";
else if (a.st == 1)
ret += "A";
else
ret += "C";
} else {
if (a.st == 0)
ret += "Z";
else if (a.st == 1)
ret += "A";
else
ret += "C";
for (int i = 0; i < a.tot_ac && len > 2; i++) ret += "AC";
if (((int)ret.size()) < len - 1) {
ret += string(len - ((int)ret.size()) - 1, 'B');
}
if (a.ed == 0)
ret += "Z";
else if (a.ed == 1)
ret += "A";
else
ret += "C";
}
return ret;
}
int main() {
int kk, x, n, m;
while (cin >> kk >> x >> n >> m) {
int rem1 = n / 2;
int rem2 = m / 2;
string s1, s2;
bool good = 1;
for (int i = 0; i <= rem1 && good; i++)
for (int j = 0; j <= rem2 && good; j++) {
for (int k = 0; k <= 2 && good; k++) {
for (int l = 0; l <= 2 && good; l++) {
fib[1] = pq(k, l, i);
if (n == 1)
fib[1].ed = -1;
else if (n - (i * 2) == 1) {
if (k == 0)
continue;
else if (k == 1) {
if (l == 0)
fib[1].st = 0, fib[1].ed = 2;
else if (l == 1)
fib[1].st = 1, fib[1].ed = 2;
else
fib[1].st = 2, fib[1].ed = 2;
fib[1].f = 1;
} else {
if (l == 0)
fib[1].st = 1, fib[1].ed = 0;
else if (l == 1)
fib[1].st = 1, fib[1].ed = 1;
else
fib[1].st = 1, fib[1].ed = 2;
fib[1].f = 2;
}
} else if (n == (i * 2))
fib[1] = pq(1, 2, i);
for (int ii = 0; ii <= 2 && good; ii++)
for (int jj = 0; jj <= 2 && good; jj++) {
fib[2] = pq(ii, jj, j);
if (m == 1)
fib[2].ed = -1;
else if (m - (j * 2) == 1) {
if (ii == 0)
continue;
else if (ii == 1) {
if (jj == 0)
fib[2].st = 0, fib[2].ed = 2;
else if (jj == 1)
fib[2].st = 1, fib[2].ed = 2;
else
fib[2].st = 2, fib[2].ed = 2;
fib[2].f = 1;
} else {
if (jj == 0)
fib[2].st = 1, fib[2].ed = 0;
else if (jj == 1)
fib[2].st = 1, fib[2].ed = 1;
else
fib[2].st = 1, fib[2].ed = 2;
fib[2].f = 2;
}
} else if (m == (j * 2)) {
fib[2] = pq(1, 2, j);
}
if (calc(kk, x)) {
good = 0;
s1 = make_string(fib[1], n);
s2 = make_string(fib[2], m);
}
}
}
}
}
if (!good) {
cout << s1 << endl;
cout << s2 << endl;
} else
cout << "Happy new year!" << endl;
}
}
| 4 |
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,b,a) for(int i=b;i>=a;i--)
#define efo(i,u,v) for(int i=BB[u],v=B[BB[u]][1];i;v=B[i=B[i][0]][1])
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
char ch;
int read(){int n=0,p=1;for(ch=getchar();ch<'0' || '9'<ch;ch=getchar())if(ch=='-') p=-1;for(;'0'<=ch && ch<='9';ch=getchar()) n=n*10+ch-'0';return n*p;}
const int N=5e5+5;
int n,L;
db q[N][2];
int main()
{
//freopen("arc072f.in","r",stdin);
//freopen("arc072f.out","w",stdout);
n=read(),L=read();
db S=0,V=0;int l=1,r=0;
fo(i,1,n)
{
int t=read(),v=read();
for(S+=(db)t*v,V+=v;V>L;)
if(V-L>=q[l][0]) S-=q[l][0]*q[l][1],V-=q[l][0],++l;
else S-=(V-L)*q[l][1],q[l][0]-=V-L,V=L;
printf("%.7lf\n",S/L);
for(q[++r][0]=v,q[r][1]=t;l<r && q[r-1][1]>q[r][1] && q[r][0]<L;r--)
{
int dt=min(q[r-1][0],L-q[r][0]);
q[r-1][1]=(q[r-1][1]*dt+q[r][1]*q[r][0])/(dt+q[r][0]);
q[r-1][0]=dt+q[r][0];
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, m, i, j, c1 = 0, c2, c = 0, s;
cin >> n >> m;
if (n == 1)
cout << "YES" << endl;
else if (m == 1)
cout << "YES" << endl;
else if (n == 2 && m == 2)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const pair<int, int> bound(0, 0);
int main() {
int k, n, maxb, t;
scanf("%d%d%d%d", &k, &n, &maxb, &t);
while (k--) {
vector<int> seq(n);
vector<vector<pair<int, int>>> dp(
n + 1, vector<pair<int, int>>(maxb + 1, make_pair(0, 0)));
vector<pair<int, int>> early(maxb + 1, make_pair(987654321, 987654321));
vector<vector<int>> follower(n, vector<int>(maxb + 1, 987654321));
vector<int> first(maxb + 1, 987654321);
for (int i = 0; i < n; i++) scanf("%d", &seq[i]);
for (int i = 0; i < n; i++) first[seq[i]] = min(first[seq[i]], i);
follower[0] = first;
int f = seq[0];
follower[0][f] = 987654321;
for (int i = 1; i < n; i++) {
if (seq[i] == f) {
follower[0][f] = i;
break;
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= maxb; j++) {
if (seq[i] != j) {
follower[i][j] = follower[i - 1][j];
} else {
for (int k = i + 1; k < n; k++)
if (seq[k] == j) {
follower[i][j] = k;
break;
}
}
}
}
for (int i = 0; i < n; i++)
early[seq[i]] = make_pair(1, min(early[seq[i]].second, i));
for (int j = 2; j <= maxb; j++) early[j] = min(early[j - 1], early[j]);
for (int i = 1; i <= maxb; i++)
if (first[i] < n) dp[1][i] = make_pair(1, first[i]);
for (int i = 2; i <= n; i++) {
for (int j = maxb; j > 0; j--) {
early[j] = make_pair(987654321, 987654321);
if (first[j] == 987654321) continue;
pair<int, int> beforePos = early[j - 1];
if (beforePos.first == 987654321) continue;
int nextPos = follower[beforePos.second][j];
if (nextPos == 987654321) {
if (beforePos.first < t) {
dp[i][j] = make_pair(beforePos.first + 1, first[j]);
}
} else {
dp[i][j] = make_pair(beforePos.first, nextPos);
}
if (dp[i][j] != bound) early[j] = dp[i][j];
}
for (int j = 2; j <= maxb; j++) early[j] = min(early[j - 1], early[j]);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= maxb; j++) {
if (dp[i][j] != bound) ans = i;
}
}
printf("%d\n", ans);
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long getgcd(long long a, long long b) {
long long t, sum;
sum = a * b;
while (b > 0) {
t = b;
b = a % b;
a = t;
}
return sum / a;
}
int main() {
long long n, a, b, p, q, sum1, sum2, sum3, x, y, ans;
while (scanf("%lld%lld%lld%lld%lld", &n, &a, &b, &p, &q) != EOF) {
sum1 = n / a;
sum2 = n / b;
sum3 = n / getgcd(a, b);
ans = (sum1 - sum3) * p + (sum2 - sum3) * q + sum3 * max(p, q);
printf("%lld\n", ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int count = 0, n, max, a, min;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
if (i == 0) {
max = a;
min = a;
} else if (a > max) {
max = a;
count++;
} else if (a < min) {
min = a;
count++;
}
}
cout << count << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-12;
const int inf = 2000000000;
const long long int infLL = (long long int)2e18;
long long int MOD = 1000000007;
int MOD1 = 1000000007;
int MOD2 = 1000000009;
inline bool checkBit(long long int n, long long int i) {
return n & (1LL << i);
}
inline long long int setBit(long long int n, long long int i) {
return n | (1LL << i);
;
}
inline long long int resetBit(long long int n, long long int i) {
return n & (~(1LL << i));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long int year) {
return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long int &a) {
a %= MOD;
(a < 0) && (a += MOD);
}
inline long long int modMul(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a * b) % MOD;
}
inline long long int modAdd(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
return (a + b) % MOD;
}
inline long long int modSub(long long int a, long long int b) {
a %= MOD, b %= MOD;
normal(a), normal(b);
a -= b;
normal(a);
return a;
}
inline long long int modPow(long long int b, long long int p) {
long long int r = 1LL;
while (p) {
if (p & 1) r = modMul(r, b);
b = modMul(b, b);
p >>= 1LL;
}
return r;
}
inline long long int modDiv(long long int a, long long int b) {
return modMul(a, modPow(b, MOD - 2));
}
bool comp(const pair<long long int, pair<long long int, long long int> > &p1,
const pair<long long int, pair<long long int, long long int> > &p2) {
return p1.first > p2.first;
}
bool comp1(const pair<long long int, long long int> &p1,
const pair<long long int, long long int> &p2) {
if (p1.first == p2.first) {
return p1.second > p2.second;
}
return p1.first < p2.first;
}
long long int converter(string a) {
long long int i, mul = 1, r, t, ans = 0LL;
if (a.length() == 0) return 0;
for (i = a.length() - 1; i >= 0; i--) {
t = a[i] - '0';
r = t % 10;
ans += (mul * r);
mul = mul * 10;
}
return ans;
}
int msb(unsigned x) {
union {
double a;
int b[2];
};
a = x;
return (b[1] >> 20) - 1023;
}
int sum[10][1003];
int go[10][1003][130], n, a[1003];
int dp[260][1003];
int need;
void pre() {
int i, j, k;
for (i = 1; i <= n; ++i) {
sum[a[i]][i] = 1;
for (j = 1; j <= 8; ++j) sum[j][i] += sum[j][i - 1];
}
for (i = 1; i <= 8; ++i) {
for (j = 1; j <= 128; ++j) {
for (k = 1; k <= n; ++k) {
int ans = inf, low = k, high = n, mid;
while (low <= high) {
mid = (low + high) >> 1;
if (sum[i][mid] - sum[i][k - 1] >= j) {
ans = min(ans, mid);
high = mid - 1;
} else
low = mid + 1;
}
go[i][k][j] = ans;
}
}
}
}
int solve(int mask, int idx) {
if (idx > n) {
if (__builtin_popcount(mask) == 8)
return 0;
else
return -inf;
}
if (dp[mask][idx] != -1) return dp[mask][idx];
int ret = -inf;
for (int i = 0; i < 8; ++i) {
if (checkBit(mask, i) == 0) {
if (go[i + 1][idx][need] != inf) {
ret = max(ret, need + solve(setBit(mask, i), go[i + 1][idx][need] + 1));
}
if (go[i + 1][idx][need + 1] != inf) {
ret =
max(ret, need + 1 +
solve(setBit(mask, i), go[i + 1][idx][need + 1] + 1));
}
}
}
ret = max(ret, solve(mask, idx + 1));
return dp[mask][idx] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int i;
cin >> n;
set<int> s;
for (i = 1; i <= n; ++i) {
cin >> a[i];
s.insert(a[i]);
}
pre();
int ans = 0;
for (i = 1; i <= 126; ++i) {
memset(dp, -1, sizeof(dp));
need = i;
ans = max(ans, solve(0, 1));
}
cout << max((int)s.size(), ans) << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 1e6;
vector<int> e[SIZE];
vector<int> gg[SIZE];
int jump[20][SIZE], lv[SIZE];
void __init(int x, int lt) {
gg[lv[x]].push_back(x);
jump[0][x] = lt;
for (int i = 0; i < (((int)(e[x]).size())); ++i) {
int y = e[x][i];
if (y != lt) {
lv[y] = lv[x] + 1;
__init(y, x);
}
}
}
int adv(int x, int v) {
for (int i = 0; (1 << i) <= v; i++) {
if ((v >> i) & 1) x = jump[i][x];
}
return x;
}
int lca(int x, int y) {
if (lv[x] > lv[y])
x = adv(x, lv[x] - lv[y]);
else
y = adv(y, lv[y] - lv[x]);
if (x == y) return x;
for (int i = 19; i >= 0; i--)
if (jump[i][x] != jump[i][y]) {
x = jump[i][x];
y = jump[i][y];
}
return jump[0][x];
}
void build(int root, int N) {
__init(root, root);
for (int i = (1); i < (20); ++i) {
for (int x = (1); x < (N + 1); ++x)
jump[i][x] = jump[i - 1][jump[i - 1][x]];
}
}
long long an[SIZE];
int h[SIZE];
pair<int, int> stk[SIZE];
void handle(int now_lv) {
int m = ((int)(gg[now_lv]).size());
for (int i = 0; i < (m); ++i) {
int x = gg[now_lv][i];
an[x] = an[jump[0][x]] + lv[x];
}
for (int i = (1); i < (m); ++i) {
h[i] = lv[lca(gg[now_lv][i - 1], gg[now_lv][i])] + 1;
}
long long now = 0;
int sn = 0;
for (int i = (1); i < (m); ++i) {
int num = 1;
while (sn && h[i] <= stk[sn - 1].first) {
now -= (long long)stk[sn - 1].first * stk[sn - 1].second;
num += stk[--sn].second;
}
now += (long long)h[i] * num;
stk[sn++] = make_pair(h[i], num);
an[gg[now_lv][i]] += now;
}
sn = 0;
now = 0;
for (int i = m - 1; i > 0; i--) {
int num = 1;
while (sn && h[i] <= stk[sn - 1].first) {
now -= (long long)stk[sn - 1].first * stk[sn - 1].second;
num += stk[--sn].second;
}
now += (long long)h[i] * num;
stk[sn++] = make_pair(h[i], num);
an[gg[now_lv][i - 1]] += now;
}
}
int main() {
int root = 0;
int(N);
scanf("%d", &N);
for (int i = (1); i < (N + 1); ++i) {
int(x);
scanf("%d", &x);
if (!x)
root = i;
else
e[x].push_back(i);
}
build(root, N);
for (int i = (1); i < (SIZE); ++i) {
if (!((int)(gg[i]).size())) break;
handle(i);
}
for (int i = (1); i < (N + 1); ++i) printf("%I64d ", an[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int Mod = 1000000007;
const int Maxn = 1010101;
long long p, k, Ans;
bool visited[Maxn];
void dfs(long long v) {
visited[v] = true;
if (!visited[v * k % p]) dfs(v * k % p);
}
int main() {
cin >> p >> k;
Ans = 1;
if (k == 1) Ans = p;
for (int i = 1; i < p; i++) {
if (!visited[i]) {
dfs(i);
Ans = Ans * p % Mod;
}
}
cout << Ans << endl;
return 0;
}
| 4 |
#include<iostream>
using namespace std;
int main()
{
int A,B;
cin>>A>>B;
if(A<=9 && B<=9)
cout<<A*B;
else
cout<<-1;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << '\n';
}
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 long long maxn = (long long)1e6 + 6;
const long double EPS = 1e-9;
const long long INF = (long long)1e18 + 18;
const long long mod = (long long)1e9 + 9;
long long n, m, k;
inline long long add(long long x, long long y) {
x += y;
if (x >= mod) x -= mod;
return x;
}
inline long long sub(long long x, long long y) {
x -= y;
if (x < 0) x += mod;
return x;
}
inline long long mul(long long x, long long y) { return (x * 1LL * y) % mod; }
inline long long binpow(long long a, long long b) {
long long x = 1 % mod;
while (b) {
if (b & 1) x = mul(x, a);
a = mul(a, a);
b >>= 1;
}
return x;
}
inline long long inv(long long a) { return binpow(a, mod - 2); }
bool works(long long x) {
long long t = x * k + ((n - x * k) / k) * (k - 1) + (n - x * k) % k;
return t >= m;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
long long left = 0, right = (n + k - 1) / k;
while (left < right) {
long long mid = (left + right) >> 1;
if (works(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
long long ans = binpow(2, left + 1);
ans = sub(ans, 2);
ans = mul(ans, k);
ans = add(ans, sub(m, left * k));
cout << ans << '\n';
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, m, q;
struct query {
int type, idx, i, j;
query() {}
query(int type, int idx, int i, int j) {
this->type = type;
this->idx = idx;
this->i = i;
this->j = j;
}
};
bool shelf[2000][2000];
bool invert[2000];
int row[200000];
vector<vector<query> > G;
int ans[200000], all;
void dfs(int i) {
for (int j = 0; j < G[i].size(); ++j) {
query now = G[i][j];
if (now.type == 1) {
int I = now.i, J = now.j;
if (!shelf[I][J] && !invert[I]) {
ans[now.idx] = ++all;
shelf[I][J] = 1;
row[I]++;
dfs(now.idx);
row[I]--;
shelf[I][J] = 0;
--all;
} else if ((!shelf[I][J] && invert[I]) || (shelf[I][J] && !invert[I])) {
ans[now.idx] = all;
dfs(now.idx);
} else if (shelf[I][J] && invert[I]) {
shelf[I][J] = 0;
ans[now.idx] = ++all;
row[I]++;
dfs(now.idx);
row[I]--;
shelf[I][J] = 1;
--all;
}
} else if (now.type == 2) {
int I = now.i, J = now.j;
if ((!shelf[I][J] && !invert[I]) || (shelf[I][J] && invert[I])) {
ans[now.idx] = all;
dfs(now.idx);
} else if (!shelf[I][J] && invert[I]) {
shelf[I][J] = 1;
ans[now.idx] = --all;
row[I]--;
dfs(now.idx);
row[I]++;
shelf[I][J] = 0;
++all;
} else if (shelf[I][J] && !invert[I]) {
shelf[I][J] = 0;
ans[now.idx] = --all;
row[I]--;
dfs(now.idx);
row[I]++;
shelf[I][J] = 1;
++all;
}
} else if (now.type == 3) {
int I = now.i;
invert[I] = !invert[I];
all = all + m - 2 * row[I];
row[I] = m - row[I];
ans[now.idx] = all;
dfs(now.idx);
invert[I] = !invert[I];
all = all + m - 2 * row[I];
row[I] = m - row[I];
} else {
ans[now.idx] = all;
dfs(now.idx);
}
}
}
int main() {
scanf("%d%d%d", &n, &m, &q);
G = vector<vector<query> >(q + 3);
int t, x, y;
for (int i = 1; i <= q; ++i) {
scanf("%d", &t);
if (t == 1) {
scanf("%d%d", &x, &y);
G[i - 1].push_back(query(t, i, x, y));
} else if (t == 2) {
scanf("%d%d", &x, &y);
G[i - 1].push_back(query(t, i, x, y));
} else if (t == 3) {
scanf("%d", &x);
G[i - 1].push_back(query(t, i, x, -1));
} else {
scanf("%d", &x);
G[x].push_back(query(t, i, -1, -1));
}
}
dfs(0);
for (int i = 0; i < q; ++i) {
printf("%d\n", ans[i + 1]);
}
return 0;
}
| 4 |
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
const int INF=1001001001;
int map[10][10000];//0????????????
int main(){
while(1){
int R,C;
scanf("%d %d",&R,&C);
if(R==0&&C==0)break;
rep(i,R)rep(j,C){scanf("%d",&map[i][j]);}
int ans=0;
for(int i=0;i < 1<<R ;i++){
int sans=0;
rep(k,C){
int rans=0;
rep(j,R){if( map[j][k]==0 && (i>>j)%2==0 || map[j][k]==1&& (i>>j)%2==1)rans++;}
sans+=max(rans,R-rans);
}//????????¨????????°?????°????????????????????????
ans=max(ans,sans);
}
printf("%d\n",ans);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int cnst = -(1e9 + 7);
bool IsQuery = true, found;
long long int q = 1, cnt;
void solve() {
long long int b, c, r, g, o, e, p, d, x, y, z, n,
minm = cnst, maxm = cnst, k = 200, sum = 0, ans = 1, l = 0, flag = 0,
sum1;
string s, t, s1;
set<string> set1;
set<string>::iterator it;
map<string, long long int> m, f;
vector<long long int> a, v;
cin >> n;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] < k) {
k = s[i];
v.clear();
}
if (k == s[i]) v.push_back(i + 1);
}
for (auto i : v) {
string s2;
s1 = s.substr(0, i - 1);
t = s.substr(i - 1, n);
if ((n - i + 1) % 2 == 1) reverse(s1.begin(), s1.end());
t.append(s1);
if (m[t] == 0) m[t] = i;
set1.insert(t);
}
s1 = *set1.begin();
for (it = set1.begin(); it != set1.end(); it++) {
t = *it;
for (int j = 0; j < n; j++) {
if (t[j] < s1[j]) {
s1 = t;
break;
} else if (t[j] > s1[j])
break;
}
}
cout << s1 << endl;
cout << m[s1] << endl;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (IsQuery) cin >> q;
for (int i = 0; i < q; i++) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a[32000];
for (int i = 0; i < 32000; i++) a[i] = i * i;
if (n == 1) {
cout << 2;
return 0;
}
for (int i = 1; i < 32000; i++) {
if (n <= a[i + 1] && n >= a[i]) {
if (n <= a[i] + i) {
cout << 2 * i + 1;
return 0;
} else {
cout << 2 * i + 2;
return 0;
}
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, t;
int main() {
scanf("%d", &t);
for (k = 0; k < t; k++) {
scanf("%d %d", &n, &m);
int res = 0;
char a[252][252];
bool b[252][252];
short sum_row[252][252];
short sum_col[252][252];
short sum_d1[252][252];
short sum_d2[252][252];
char c = getc(stdin);
for (i = 0; i <= n + 1; i++)
for (j = 0; j <= m + 1; j++) {
sum_row[i][j] = 0;
sum_col[i][j] = 0;
sum_d1[i][j] = 0;
sum_d2[i][j] = 0;
a[i][j] = '0';
b[i][j] = false;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
c = getc(stdin);
a[i][j] = c;
}
c = getc(stdin);
}
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++) {
if (a[i][j] == '1') {
sum_row[i][j]++;
sum_col[i][j]++;
sum_d1[i][j]++;
sum_d2[i][j]++;
}
if (a[i][j - 1] == '1') sum_row[i][j] += sum_row[i][j - 1];
if (a[i - 1][j] == '1') sum_col[i][j] += sum_col[i - 1][j];
if (a[i - 1][j - 1] == '1') sum_d1[i][j] += sum_d1[i - 1][j - 1];
if (a[i - 1][j + 1] == '1') sum_d2[i][j] += sum_d2[i - 1][j + 1];
}
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (a[i][j] == '1' && !b[i][j]) {
queue<pair<int, int> > q;
int col = 0;
q.push(make_pair(i, j));
b[i][j] = true;
pair<int, int> L1, L2, L3, L4, D1, D2, D3, D4;
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
int cy = cur.first, cx = cur.second;
col++;
if (L1.first == 0 || (cy <= L1.first && cx <= L1.second))
L1 = make_pair(cy, cx);
if (L2.first == 0 || (cy <= L2.first && cx >= L2.second))
L2 = make_pair(cy, cx);
if (L3.first == 0 || (cy >= L3.first && cx >= L3.second))
L3 = make_pair(cy, cx);
if (L4.first == 0 || (cy >= L4.first && cx <= L4.second))
L4 = make_pair(cy, cx);
if (D1.first == 0 || (cy < D1.first)) D1 = make_pair(cy, cx);
if (D2.first == 0 || (cx > D2.second)) D2 = make_pair(cy, cx);
if (D3.first == 0 || (cy > D3.first)) D3 = make_pair(cy, cx);
if (D4.first == 0 || (cx < D4.second)) D4 = make_pair(cy, cx);
for (int y = -1; y <= 1; y++)
for (int x = -1; x <= 1; x++)
if (!b[cy + y][cx + x] && a[cy + y][cx + x] == '1') {
b[cy + y][cx + x] = true;
q.push(make_pair(cy + y, cx + x));
}
}
if (col < 4 || ((col + 4) % 4) != 0) continue;
int z = (col + 4) / 4;
if ((sum_col[L4.first][L4.second] - sum_col[L1.first][L1.second] +
1) == z &&
(sum_row[L2.first][L2.second] - sum_row[L1.first][L1.second] +
1) == z &&
(sum_col[L3.first][L3.second] - sum_col[L2.first][L2.second] +
1) == z &&
(sum_row[L3.first][L3.second] - sum_row[L4.first][L4.second] +
1) == z) {
res++;
continue;
}
if ((sum_d1[D2.first][D2.second] - sum_d1[D1.first][D1.second] + 1) ==
z &&
(sum_d1[D3.first][D3.second] - sum_d1[D4.first][D4.second] + 1) ==
z &&
(sum_d2[D3.first][D3.second] - sum_d2[D2.first][D2.second] + 1) ==
z &&
(sum_d2[D4.first][D4.second] - sum_d2[D1.first][D1.second] + 1) ==
z)
res++;
}
cout << res << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
int x = 0, y = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
char c;
cin >> c;
if (c == '*') {
x = x ^ i;
y = y ^ j;
}
}
}
cout << x << " " << y;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct message {
string from;
string to;
int time;
};
bool mark[int(1e4)];
message msg[int(1e4)];
int n, d, result;
vector<pair<string, string> > friends;
int main() {
cin >> n >> d;
for (int i = 0; i < n; i++) {
string from, to;
int time;
cin >> from >> to >> time;
msg[i].from = from;
msg[i].to = to;
msg[i].time = time;
}
for (int i = 0; i < n; i++) {
if (mark[i]) continue;
for (int j = i; j >= 0; j--) {
if (mark[j]) continue;
if (msg[i].from == msg[j].to && msg[i].to == msg[j].from) {
if ((0 < abs(msg[i].time - msg[j].time)) &&
(abs(msg[i].time - msg[j].time) <= d)) {
result++;
friends.push_back(make_pair(msg[i].from, msg[i].to));
for (int k = 0; k < n; k++)
if ((msg[i].from == msg[k].from && msg[i].to == msg[k].to) ||
(msg[j].from == msg[k].from && msg[j].to == msg[k].to))
mark[k] = true;
break;
}
}
}
}
cout << result << endl;
for (int i = 0; i < friends.size(); i++)
cout << friends[i].first << " " << friends[i].second << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > V;
int main() {
int n, k, i, j;
scanf("%d %d", &n, &k);
int arr[n];
long long int suffix_total = 0;
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
suffix_total = suffix_total + arr[i];
}
sort(arr, arr + n);
pair<int, int> p;
p.first = arr[0];
p.second = 1;
V.push_back(p);
for (i = 1; i < n; i++) {
if (arr[i] == V[V.size() - 1].first) {
V[V.size() - 1].second++;
} else {
p.first = arr[i];
V.push_back(p);
}
}
if (V[0].second >= k || V[V.size() - 1].second >= k) {
printf("0");
return 0;
}
long long int ans = 1000000000000000000, prefix_total = 0;
int prefix_num = 0, suffix_num = n;
for (i = 0; i < V.size(); i++) {
if (V[i].second >= k) {
printf("0");
return 0;
}
int current_num = V[i].second;
long long int current_sum = V[i].first;
current_sum = current_sum * current_num;
suffix_total = suffix_total - current_sum;
suffix_num = suffix_num - current_num;
long long int cc = (k - V[i].second);
long long int first = V[i].first - 1, last = V[i].first + 1;
first = first * prefix_num - prefix_total;
last = suffix_total - last * suffix_num;
if (V[i].second + suffix_num >= k) {
ans = min(ans, last + cc);
}
if (V[i].second + prefix_num >= k) {
ans = min(ans, first + cc);
}
ans = min(ans, first + last + cc);
prefix_num = prefix_num + current_num;
prefix_total = prefix_total + current_sum;
}
printf("%I64d", ans);
return 0;
}
| 6 |
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
int main()
{
int n,q,c,t,x;
vector<priority_queue<int, vector<int>>> a;
scanf("%d", &n);
scanf("%d", &q);
for (int i = 0; i < n; i++) {
a.emplace_back();
}
for (int i = 0; i < q; i++) {
scanf("%d", &c);
scanf("%d", &t);
if (c == 0){
scanf("%d", &x);
a[t].push(x);
}else if (c == 1){
if (a[t].empty()==0){ //空:1
printf("%d\n",a[t].top());
}
}else if (c == 2){
if (a[t].empty()==0){ //空:1
a[t].pop();
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 1; i <= t; i++) {
int n, k;
cin >> n >> k;
if (k % 3 != 0) {
if (n % 3 == 0) {
cout << "Bob" << endl;
} else {
cout << "Alice" << endl;
}
} else {
n %= (k + 1);
if (n % 3 == 0 && n != k) {
cout << "Bob" << endl;
} else {
cout << "Alice" << endl;
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool isrange(int second, int first, int n, int m) {
if (0 <= second && second < n && 0 <= first && first < m) return true;
return false;
}
int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},
ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const long long int mod = 1e9 + 7;
const int MAX = 111;
string a;
int n, first;
long long int dp[MAX][MAX][MAX][4], two[MAX];
long long int dfs(int l, int r, int k, int t) {
if (dp[l][r][k][t] != -1) return dp[l][r][k][t];
if (k == 0) {
if (l != r)
return 0;
else {
if (a[l] == '0') return 1;
return 0;
}
} else if (k == 1) {
if (l != r)
return 0;
else {
if (a[l] == '1') return 1;
return 0;
}
}
if (t == 0) {
long long int f1 = (dfs(l, r, k - 1, 0) * two[k - 2]) % mod;
f1 = (f1 + dfs(l, r, k - 2, 0) * two[k - 1]) % mod;
for (int e = l; e < r; e++) {
long long int v1 = (dfs(l, e, k - 1, 1) * dfs(e + 1, r, k - 2, 2)) % mod;
f1 = (f1 + v1) % mod;
}
return dp[l][r][k][t] = f1;
}
if (t == 1) {
long long int f1 = dfs(l, r, k - 1, 1);
f1 = (f1 + dfs(l, r, k - 2, 1) * two[k - 1]) % mod;
for (int e = l; e < r; e++) {
long long int v1 = (dfs(l, e, k - 1, 1) * dfs(e + 1, r, k - 2, 3)) % mod;
f1 = (f1 + v1) % mod;
}
return dp[l][r][k][t] = f1;
}
if (t == 2) {
long long int f1 = (dfs(l, r, k - 1, 2) * two[k - 2]) % mod;
f1 = (f1 + dfs(l, r, k - 2, 2)) % mod;
for (int e = l; e < r; e++) {
long long int v1 = (dfs(l, e, k - 1, 3) * dfs(e + 1, r, k - 2, 2)) % mod;
f1 = (f1 + v1) % mod;
}
return dp[l][r][k][t] = f1;
}
if (t == 3) {
long long int f1 = dfs(l, r, k - 1, 3);
f1 = (f1 + dfs(l, r, k - 2, 3)) % mod;
for (int e = l; e < r; e++) {
long long int v1 = (dfs(l, e, k - 1, 3) * dfs(e + 1, r, k - 2, 3)) % mod;
f1 = (f1 + v1) % mod;
}
return dp[l][r][k][t] = f1;
}
}
int main(void) {
memset(dp, -1, sizeof(dp));
scanf("%d%d", &n, &first);
cin >> a;
two[0] = 2;
two[1] = 2;
for (int e = 2; e < MAX; e++) two[e] = (two[e - 1] * two[e - 2]) % mod;
printf("%lld", dfs(0, n - 1, first, 0));
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int flag = 0, n;
cin >> n;
for (int i = 0; i < 150; i++) {
int x = 7 * i;
for (int j = 0; j <= 200; j++) {
int y = 5 * j;
int z = n - x - y;
if (z < 0) continue;
if (!z) {
if (!(j + i)) continue;
cout << 0 << ' ' << j << ' ' << i << endl;
flag = 1;
break;
}
if (!(z % 3)) {
cout << z / 3 << ' ' << j << ' ' << i << endl;
flag = 1;
break;
}
}
if (flag) break;
}
if (!flag) cout << -1 << endl;
;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m[5];
int w[5];
int s, u;
for (int i = 0; i < 5; i++) {
cin >> m[i];
}
for (int i = 0; i < 5; i++) {
cin >> w[i];
}
cin >> s >> u;
double out[5];
int x[5];
x[0] = 500;
x[1] = 1000;
x[2] = 1500;
x[3] = 2000;
x[4] = 2500;
out[0] = 0.3 * 500;
out[1] = out[0] * 2;
out[2] = out[0] * 3;
out[3] = out[0] * 4;
out[4] = out[0] * 5;
for (int i = 0; i < 5; i++) {
out[i] =
max(out[i], (((double)1 - (double)m[i] / (double)250) * (double)x[i] -
50 * (double)w[i]));
}
double ans = 0.0;
for (int i = 0; i < 5; i++) {
ans += out[i];
}
long long add = 100 * s - 50 * u;
ans += (double)add;
cout << (long long)ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6;
const int INF = 1e9 + 9;
struct FenTree {
int n;
unordered_map<int, int> t;
void init(int _n) { n = _n; }
void upd(int x, int val) {
for (; x <= n; x = (x | (x + 1))) t[x] = max(t[x], val);
}
int get(int x) {
int ans = 0;
for (; x >= 0; x = (x & (x + 1)) - 1) ans = max(ans, t[x]);
return ans;
}
} tree;
int n;
int d[N];
pair<long long, int> a[N];
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = int(1); i <= int(n); ++i) cin >> a[i].first >> a[i].second;
sort(a + 1, a + 1 + n);
tree.init(2e9);
int ans = 1;
for (int i = int(1); i <= int(n); ++i) {
d[i] = tree.get(a[i].first - a[i].second) + 1;
ans = max(ans, d[i]);
tree.upd(a[i].first + a[i].second, d[i]);
}
cout << ans << "\n";
return 0;
}
| 2 |
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define LL long long
#define Maxn 800005
using namespace std;
const LL P = 200003; const double PI = acos(-1.0);
inline LL read() {
LL x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while('0' <= c && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
LL n, m = 1, val[Maxn], mapp[Maxn], trans[Maxn], g[Maxn];
struct Point {
double x, y;
}
f[Maxn];
Point Plus(Point a, Point b) {
Point c;
c.x = a.x + b.x; c.y = a.y + b.y;
return c;
}
Point Minus(Point a, Point b) {
Point c;
c.x = a.x - b.x; c.y = a.y - b.y;
return c;
}
Point Mul(Point a, Point b) {
Point c;
c.x = a.x * b.x - a.y * b.y;
c.y = a.x * b.y + b.x * a.y;
return c;
}
void DFT(Point *F, bool op) {
for(int i = 0; i < m; ++i) trans[i] = (trans[i >> 1] >> 1) | ((i & 1) ? m >> 1 : 0);
for(int i = 0; i < m; ++i) if(i < trans[i]) swap(F[i], F[trans[i]]);
for(int len = 2; len <= m; len <<= 1) {
Point base = {cos(2 * PI / len), sin(2 * PI / len)};
if(op) base.y *= -1.0;
for(int i = 0; i < m; i += len) {
Point now = {1.0, 0.0};
for(int k = i; k < i + (len >> 1); ++k) {
Point temp = Mul(now, F[k + (len >> 1)]);
F[k + (len >> 1)] = Minus(F[k], temp);
F[k] = Plus(F[k], temp);
now = Mul(now, base);
}
}
}
}
int main() {
n = read();
for(int i = 1; i <= n; ++i) val[i] = read();
LL mul = 1;
for(int i = 0; i <= P - 2; ++i) {
mapp[mul] = i;
mul = mul * 2 % P;
}
for(int i = 1; i <= n; ++i)
if(val[i]) ++f[mapp[val[i]]].x;
while(m < 2 * P - 3) m <<= 1;
// cout << m << endl;
DFT(f, 0);
for(int i = 0; i < m; ++i) f[i] = Mul(f[i], f[i]);
DFT(f, 1);
for(int i = 0; i < m; ++i) g[i] = f[i].x * 1.0 / m + 0.5;
LL ans = 0; mul = 1;
for(int i = 1; i <= n; ++i) {
if(val[i]) --g[mapp[val[i]] * 2];
}
for(int i = 0; i <= P - 2; ++i) {
ans += mul * (g[i] + g[i + P - 1]) / 2;
mul = mul * 2 % P;
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long int tree[5 * 300006];
long long int lazy[5 * 300006];
long long int n;
long long int arr[300006];
bool vis[300006];
map<long long int, long long int> m;
vector<long long int> v, vv;
long long int p = 1000000007;
long long int power(long long int x, long long int y) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int a[1000][1000];
int main() {
long long int n, m;
cin >> n;
for (int i = 1; i <= n; i++) {
a[i][1] = i;
}
long long int cnt = n + 1;
for (int j = 2; j <= n; j++) {
if (j & 1) {
for (int i = 1; i <= n; i++) {
a[i][j] = cnt++;
}
} else {
for (int i = n; i >= 1; i--) a[i][j] = cnt++;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main () {
long long n, t, i, x, m, r, ans, y, p ;
string s;
cin >> n >> m >> s;
map < long long, long long > a;
a[0] = 0;
for ( i = 0; i < n; i ++) {
a[i +1] = a[i] + s[i] - 'a';
}
for ( i =1 ; i <= m; i ++) {
scanf("%lld",& x );
scanf("%lld",& y );
p = a[y] - a[x-1];
cout << p + y- x + 1 << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int mod = (int)1e+9 + 7;
const double pi = acos(-1.);
const int maxn = 5 * 100100;
priority_queue<long long> q;
long long t = 0;
int main() {
srand(time(0));
const string file = "";
if (!file.empty()) {
freopen((file + ".in").c_str(), "r", stdin);
freopen((file + ".out").c_str(), "w", stdout);
}
long long n, k;
scanf("%I64d%I64d", &n, &k);
for (int i = 0; i < n; i++) {
long long s, m;
scanf("%I64d%I64d", &s, &m);
while (q.size() == k) {
t = -q.top();
q.pop();
}
t = max(t, s);
q.push(-t - m);
printf("%I64d\n", t + m);
}
return (0);
}
| 4 |
#include <bits/stdc++.h>
char a[128], c[128];
int b, d, t[128];
int main() {
scanf("%d%d", &b, &d);
scanf("%s%s", a, c);
int s = 0, n = strlen(c);
for (int i = 0; c[i]; ++i) {
int& k = t[i];
for (int j = 0; a[j]; ++j)
if (a[j] == c[(i + k) % n]) ++k;
}
for (int i = 0; i < b; ++i) s += t[s % n];
printf("%d\n", s / n / d);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 1000111, max_lev = 20, mod = 1000000007;
int n, k, a[max_n], mx[max_lev][max_n], dp[max_n];
multiset<int> q;
void add(int &x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
}
int main() {
scanf("%d%d", &n, &k);
--k;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for (int i = n - k + 1; i < n; ++i) {
q.insert(a[i]);
}
for (int i = n - k; i >= 0; --i) {
q.insert(a[i]);
mx[0][i] = *q.rbegin();
for (int lev = 1; lev < max_lev; ++lev) {
long long nxt = i + (1LL << (lev - 1)) * k;
int val = mx[lev - 1][i];
if (nxt < n) {
val = max(val, mx[lev - 1][nxt]);
}
mx[lev][i] = val;
}
int cur = i;
for (int lev = max_lev - 1; lev >= 0; --lev) {
long long nxt = cur + (1LL << lev) * k;
if (nxt <= n && mx[lev][cur] <= mx[0][i]) {
cur = nxt;
}
}
dp[i] = (1LL * mx[0][i] * ((cur - i) / k)) % mod;
if (cur < n) {
add(dp[i], dp[cur]);
}
q.erase(q.find(a[i + k - 1]));
}
int ans = 0;
for (int i = 0; i + k < n; ++i) {
int first = max(a[i], mx[0][i + 1]);
int cur = i + k + 1;
for (int lev = max_lev - 1; lev >= 0; --lev) {
long long nxt = cur + (1LL << lev) * k;
if (nxt <= n && mx[lev][cur] <= first) {
cur = nxt;
}
}
int pl = (1LL * first * (1 + (cur - i - k - 1) / k)) % mod;
if (cur < n) {
add(pl, dp[cur]);
}
add(ans, pl);
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
string a[123];
bool b[1234];
int pos[1234];
int main() {
int m, n;
cin >> m >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
for (int j = 0; j < m; j++) {
if (a[i][j] == '0') {
b[i] = 1;
break;
}
}
}
for (int i = 0; i < n; i++) {
if (i == 0) {
if (b[i])
pos[i] = 1;
else
pos[i] = 0;
continue;
}
if (b[i]) {
pos[i] = pos[i - 1] + 1;
} else
pos[i] = 0;
}
int res = 0;
for (int i = 0; i < n; i++) {
if (pos[res] < pos[i]) res = i;
}
cout << pos[res];
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, n;
set<int> s;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a);
s.insert(a);
}
if (s.size() <= 2) {
puts("YES");
} else if (s.size() == 3) {
vector<int> v(s.begin(), s.end());
if (v[2] - v[1] == v[1] - v[0])
puts("YES");
else
puts("NO");
} else
puts("NO");
return 0;
}
| 2 |
#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;
struct min_cost_max_flow {
struct edge {
int to;
ll cap, cost;
int rev;
bool isrev;
};
int n, s, t;
ll neg;
vector<vector<edge>> g;
vector<ll> d, h, dist, prevv, preve;
ll flow(vector<ll> d0) {
ll res = 0;
priority_queue<PII, vector<PII>, greater<PII>> que;
h.assign(n, 0);
preve.assign(n, -1);
prevv.assign(n, -1);
ll f = 0;
REP(i, d.size()) {
if(i < (ll)d0.size()) d[i] += d0[i];
if(d[i] > 0) add_edge(s, i, d[i], 0), f += d[i];
else if(d[i] < 0) add_edge(i, t, -d[i], 0);
}
while(f > 0) {
dist.assign(n, INF);
dist[s] = 0;
que.push({0, s});
while(que.size()) {
PII p = que.top(); que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
REP(i, g[v].size()) {
edge &e = g[v][i];
if(e.cap>0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]) {
dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
prevv[e.to] = v;
preve[e.to] = i;
que.push({dist[e.to], e.to});
}
}
}
if(dist[t] == INF) return -1;
REP(v, n) h[v] += dist[v];
ll d = f;
for(int v = t; v != s; v = prevv[v]) {
chmin(d, g[prevv[v]][preve[v]].cap);
}
f -= d; res += d * h[t];
for(int v = t; v != s; v = prevv[v]) {
edge &e = g[prevv[v]][preve[v]];
e.cap -= d;
g[v][e.rev].cap += d;
}
}
return neg + res;
}
min_cost_max_flow(int n0) : n(n0+2), s(n0), t(n0+1), neg(0), g(n0+2), d(n0+2) {}
void add_edge(int from, int to, ll cap, ll cost) {
if(cost >= 0) {
g[from].push_back({to, cap, cost, (int)g[to].size(), false});
g[to].push_back({from, 0, -cost, (int)g[from].size()-1, true});
} else {
d[from] -= cap;
d[to] += cap;
neg += cap * cost;
add_edge(to, from, cap, -cost);
}
}
// SからTに流量fを流す 流せないなら-1
// F'を負辺の容量の和とすると O((f+F')ElogV)
ll flow(int S, int T, ll f) {
vector<ll> d0(n);
d0[S] = f, d0[T] = -f;
return flow(d0);
}
friend ostream &operator <<(ostream& out, const min_cost_max_flow& a){
out << endl;
for(int i = 0; i < (int)a.g.size(); i++) {
for(auto &e : a.g[i]) {
if(e.isrev) continue;
auto &rev_e = a.g[e.to][e.rev];
out << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ") cost:" << e.cost << endl;
}
}
return out;
}
};
int main(void) {
ll n;
cin >> n;
vector<vector<ll>> w(n, vector<ll>(n)), e(n, vector<ll>(n));
vector<string> f(n);
REP(i, n) REP(j, n) cin >> w[i][j];
REP(i, n) REP(j, n) cin >> e[i][j];
REP(i, n) cin >> f[i];
ll ret = 0;
min_cost_max_flow graph(2*n+2);
ll s = 2*n, t = s+1;
REP(i, n) {
graph.add_edge(s, i, 1, 0);
graph.add_edge(n+i, t, 1, 0);
}
REP(i, n) REP(j, n) {
if(f[i][j] == 'o') {
ret += e[i][j];
graph.add_edge(i, n+j, 1, -e[i][j]);
} else {
graph.add_edge(i, n+j, 1, w[i][j]);
}
}
cout << ret + graph.flow(s, t, n) << endl;
// 復元
vector<string> g(n, string(n, '.'));
REP(i, n) for(auto e: graph.g[i]) {
if(e.cap == 0 && n <= e.to && e.to < 2*n) {
g[i][e.to-n] = 'o';
}
}
vector<PII> ans;
REP(i, n) REP(j, n) if(f[i][j]!=g[i][j]) ans.push_back({i, j});
cout << ans.size() << endl;
for(auto i: ans) {
cout << i.first+1 << " " << i.second+1;
if(f[i.first][i.second] == '.') cout << " write" << endl;
else cout << " erase" << endl;
}
return 0;
}
| 0 |
#include <iostream>
using namespace std;
int main() {
int a,b,c,d;
cin >> a >> b >> c >> d;
cout << max(0, min(b,d) - max(a,c)) << endl;
} | 0 |
#include<stdio.h>
#include<algorithm>
using namespace std;
char str[60][60];
int dp[52][52][52][52];
int v[52][52][52][52];
int r[600];
int w[600];
int sz;
int H,W;
int solve(int a,int b,int c,int d){
if(a>c||b>d)return 0;
if(a<0||b<0||c>=H||d>=W)return 0;
if(str[a][b]=='#'||str[c][d]=='#')return 0;
if(~dp[a][b][c][d])return dp[a][b][c][d];
if(!v[a][b][c][d])return 0;
if(a==c&&b==d)return dp[a][b][c][d]=0;
int ret=0;
if('a'<=str[a][b]&&str[a][b]<='z'&&str[a][b]-32==str[c][d]){
// printf("%c %c\n",str[a][b],str[c][d]);
if(v[a][b][c][d])ret=1;
for(int i=0;i<sz;i++){
for(int j=0;j<sz;j++){
if(i==j)continue;
if(v[a][b][r[i]][w[i]]&&(a!=r[i]||b!=w[i])&&
v[r[j]][w[j]][c][d]&&(c!=r[j]||d!=w[j])&&v[r[i]][w[i]][r[j]][w[j]]){
// if(solve(r[i],w[i],r[j],w[j])+1==3)printf("%d %d %d %d: %d %d %d %d\n",a,b,c,d,r[i],w[i],r[j],w[j]);
ret=max(ret,solve(r[i],w[i],r[j],w[j])+1);
}
}
}
}
for(int i=0;i<sz;i++){
if(v[a][b][r[i]][w[i]]&&(a!=r[i]||b!=w[i])){
ret=max(ret,solve(r[i],w[i],c,d));
}
if(v[r[i]][w[i]][c][d]&&(c!=r[i]||d!=w[i])){
ret=max(ret,solve(a,b,r[i],w[i]));
}
}
for(int i=0;i<sz;i++){
if(v[a][b][r[i]][w[i]]&&v[r[i]][w[i]][c][d]&&(a!=r[i]||b!=w[i])&&(c!=r[i]||d!=w[i])){
ret=max(ret,solve(a,b,r[i],w[i])+solve(r[i],w[i]+1,c,d));
ret=max(ret,solve(a,b,r[i],w[i])+solve(r[i]+1,w[i],c,d));
}
}
// if(ret>1)printf("%d %d %d %d: %d\n",a,b,c,d,ret);
return dp[a][b][c][d]=ret;
}
int main(){
int a,b;
while(scanf("%d%d",&a,&b),a){
H=a;W=b;
for(int i=0;i<a;i++)scanf("%s",str[i]);
for(int i=0;i<a;i++)for(int j=0;j<b;j++)
for(int k=0;k<a;k++)for(int l=0;l<b;l++){
dp[i][j][k][l]=-1;
v[i][j][k][l]=0;
}
sz=0;
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
if(str[i][j]!='.'&&str[i][j]!='#'){
r[sz]=i;w[sz++]=j;
}
}
}
for(int i=0;i<a;i++)for(int j=0;j<b;j++){
if(str[i][j]=='#')continue;
v[i][j][i][j]=1;
for(int k=0;k<a;k++)for(int l=0;l<b;l++){
if(str[k][l]=='#')continue;
if(k&&v[i][j][k-1][l])v[i][j][k][l]=1;
if(l&&v[i][j][k][l-1])v[i][j][k][l]=1;
}
}
int ret=solve(0,0,a-1,b-1);
if(!v[0][0][a-1][b-1])printf("-1\n");
else printf("%d\n",ret);
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, i, p = 0;
cin >> n;
int ara[n];
for (i = 0; i < n; i++) {
cin >> ara[i];
}
sort(ara + 0, ara + n);
for (i = 0; i < n - 1; i++) {
if (ara[i] + 1 != ara[i + 1]) p = p - 1 + ara[i + 1] - ara[i];
}
cout << p;
}
| 1 |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
while(1){
int N;
cin>> N;
if(!N) break;
vector<pair<double, string>> V;
for(int i=0; i<N; i++){
string L;
int P, A, B, C, D, E, F, S, M;
cin>> L>> P>> A>> B>> C>> D>> E>> F>> S>> M;
int T=A+B+C+D+E;
int t=D+E;
int R=F*S;
for(int i=0; i<M-1; i++){
T+=t;
R+=F*S;
}
V.push_back(make_pair(-(double)(R-P)/T, L));
}
sort(V.begin(), V.end());
for(int i=0; i<V.size(); i++){
cout<< V[i].second<< endl;
}cout<< "#"<< endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long int f(long long int x, long long int b, long long int n) {
return x * x + b * x - n;
}
long long int find(int lo, int hi, long long int b, long long int n) {
if (lo > hi) return -1;
long long int m = (lo + hi) / 2;
if (f(m, b, n) == 0)
return m;
else {
if (f(m, b, n) < 0 || m < 0)
return find(m + 1, hi, b, n);
else
return find(lo, m - 1, b, n);
}
}
int s(long long int x) {
int a = 0;
while (x) {
a += x % 10;
x /= 10;
}
return a;
}
bool is_int(double x) { return x - floor(x) == 0; }
int main() {
long long int n, ans = -1, tmp;
scanf("%lld", &n);
long long int m = sqrt(n) + 1;
for (long long int i = 0; i < 91; i++) {
tmp = find(0, m, i, n);
if (tmp != -1 && s(tmp) == i) {
ans = tmp;
break;
}
}
printf("%lld\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, n;
cin >> a >> b >> c >> n;
int d = n - (a + b - c);
if ((a - c) >= 0 && (b - c) >= 0 && c >= 0 && d > 0) {
cout << d << endl;
} else
cout << -1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 5;
int aib_repaired[MAX], aib_broken[MAX], n, k, updated, current, q;
int lsb(int x) { return x & (-x); }
int sum_query(int aib[], int pos) {
if (!pos || pos > n) return 0;
int sum = 0;
for (int i = pos; i > 0; i -= lsb(i)) sum += aib[i];
return sum;
}
void update(int aib[], int pos, int val) {
for (int i = pos; i <= n; i += lsb(i)) aib[i] += val;
}
int main() {
cin.sync_with_stdio(false);
cin >> n >> k >> updated >> current >> q;
for (int i = 1; i <= q; i++) {
int type, day, cnt;
cin >> type;
if (type == 1) {
cin >> day >> cnt;
int temp1 = sum_query(aib_broken, day) - sum_query(aib_broken, day - 1);
int temp2 =
sum_query(aib_repaired, day) - sum_query(aib_repaired, day - 1);
if (temp1 + cnt <= current) {
update(aib_broken, day, cnt);
} else
update(aib_broken, day, current - temp1);
if (temp2 + cnt <= updated) {
update(aib_repaired, day, cnt);
} else
update(aib_repaired, day, updated - temp2);
} else {
cin >> day;
long long sum = sum_query(aib_broken, day - 1);
if (day + k - 1 <= n)
sum +=
sum_query(aib_repaired, n) - sum_query(aib_repaired, day + k - 1);
cout << sum << '\n';
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1e9 + 7, MOD1 = 998244353;
void _print(long long int t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(long double t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(unsigned long long int t) { cerr << t; }
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
cerr << "{";
_print(p.first);
cerr << ",";
_print(p.second);
cerr << "}";
}
template <class T>
void _print(vector<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(set<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
cerr << "[ ";
for (T i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
cerr << "[ ";
for (auto i : v) {
_print(i);
cerr << " ";
}
cerr << "]";
}
long long int gcd(long long int a, long long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long int sum = 0;
void check(const vector<long long int> &v) {
long long int s = sum, op = sum;
long long int d, a;
cin >> d >> a;
long long int idx = lower_bound(v.begin(), v.end(), d) - v.begin();
if (idx == 0) {
long long int take = v[idx];
s = s - take;
long long int ans = 0;
if (take < d) {
ans += d - take;
}
if (s < a) {
ans += a - s;
}
cout << ans << "\n";
return;
} else if (idx == v.size()) {
long long int take = v[idx - 1];
s = s - take;
long long int ans = 0;
if (take < d) {
ans += d - take;
}
if (s < a) {
ans += a - s;
}
cout << ans << "\n";
return;
} else {
long long int ans1 = 0, ans2 = 0;
long long int first = idx - 1;
long long int second = idx;
long long int take = v[first];
s = s - take;
if (take < d) {
ans1 += d - take;
}
if (s < a) {
ans1 += a - s;
}
take = v[second];
op = op - take;
if (take < d) {
ans2 += d - take;
}
if (op < a) {
ans2 += a - op;
}
cout << min(ans1, ans2) << "\n";
return;
}
}
void solve() {
long long int n;
cin >> n;
vector<long long int> v(n);
for (long long int i = 0; i < n; i++) {
cin >> v[i];
sum += v[i];
}
sort((v).begin(), (v).end());
long long int m;
cin >> m;
while (m--) {
check(v);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1, i = 1;
while (t--) {
solve();
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[110000];
int b[100021];
int c[111002];
int main() {
int n;
while (scanf("%d", &n) == 1) {
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int i = 0; i < n; i++) scanf("%d", &b[i]);
for (int i = 0; i < n; i++) c[a[i] - 1] = b[i];
for (int i = 0; i < n; i++) printf("%d%c", c[i], i == n - 1 ? '\n' : ' ');
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> g[2501];
long long visited[2501];
long long low[2501];
long long disc[2501];
long long parent[2501];
long long apa[2501] = {0};
void ap(long long u) {
static long long time = 0;
long long child = 0;
visited[u] = 1;
disc[u] = low[u] = ++time;
for (auto v : g[u]) {
if (!visited[v]) {
child++;
parent[v] = u;
ap(v);
low[u] = min(low[u], low[v]);
if (parent[u] == -1 && child > 1) {
apa[u] = 1;
}
if (parent[u] != -1 && low[v] >= disc[u]) {
apa[u] = 1;
}
} else if (v != parent[u]) {
low[u] = min(low[u], disc[v]);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
memset(visited, 0, sizeof(visited));
memset(parent, -1, sizeof(parent));
long long n, m;
cin >> n >> m;
vector<string> v;
string s;
for (long long i = 0; i < n; i++) {
cin >> s;
v.push_back(s);
}
long long c = 1;
long long root;
long long count = 0;
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
if (v[i][j] == '#') {
root = c;
count++;
if (i - 1 >= 0 && v[i - 1][j] == '#') {
g[c].push_back(c - m);
}
if (i + 1 < n && v[i + 1][j] == '#') {
g[c].push_back(c + m);
}
if (j - 1 >= 0 && v[i][j - 1] == '#') {
g[c].push_back(c - 1);
}
if (j + 1 < m && v[i][j + 1] == '#') {
g[c].push_back(c + 1);
}
}
c++;
}
}
if (count <= 2) {
cout << -1 << endl;
return 0;
}
ap(root);
for (long long i = 1; i < n * m + 1; i++) {
if (apa[i] == 1) {
cout << 1 << endl;
return 0;
}
}
cout << 2 << endl;
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.