solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <iostream>
#include <algorithm>
#include <cstring>
typedef long long ll;
const ll MOD = 1000000007ll;
int N, L;
int X[5000], A[5000];
ll BIT[2][5001];
inline void add(int k, ll v, ll (&bit)[5001]){
k += 1;
while(k <= L){
bit[k] = (bit[k] + v) % MOD;
k += k & -k;
}
}
inline ll sum(int k, ll (&bit)[5001]){
k += 1;
ll res = 0ll;
while(k > 0){
res = (res + bit[k]) % MOD;
k -= k & -k;
}
return res;
}
int main(){
scanf("%d %d", &N, &L);
for(int i=0;i<N;i++){
scanf("%d", X+i);
X[i] = L-1 - X[i];
}
std::reverse(X, X+N);
for(int i=0;i<N;i++){
scanf("%d", A+i);
}
std::reverse(A, A+N);
if(A[0] == 0){
add(X[0], 1, BIT[0]);
}else{
for(int i=X[0];i>=0;i-=A[0]){
add(i, 1, BIT[0]);
}
}
int prev = 0, next = 1;
for(int i=1;i<N;i++){
memset(BIT[next], 0, sizeof(BIT[next]));
if(A[i] == 0){
add(X[i], sum(X[i]-1, BIT[prev]), BIT[next]);
}else{
for(int j=X[i];j>=0;j-=A[i]){
add(j, sum(j-1, BIT[prev]), BIT[next]);
}
}
std::swap(prev, next);
}
printf("%lld\n", sum(L-1, BIT[prev]));
} | 0 |
#include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;
#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu();
do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;}
_in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) {pcu('0'+b);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_vo(string s){for(char c:s)pcu(c);}
#endif
_T _vo(T n){static char buf[20];char *p=buf;
if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
while (p!=buf)pcu(*--p);}
_vl(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}
using D = double;
using T = tuple<D, D, D>;
int main() {
vector<T> l;
int N = in();
D W = in(), V = 0;
for (; N; N--) {
const D v = in(), w = in();
l.push_back(make_tuple(v / w, v, w));
}
sort(l.rbegin(), l.rend(), [](const T &a, const T &b) {return get<0>(a) < get<0>(b);});
for (auto x: l) {
const D w = get<2>(x), m = min(W, w);
W -= m;
V += get<1>(x) * (m / w);
if (W <= 0)
break;
}
printf("%f\n", V);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int sum( int n, int s, int x ) {
if( n == 0 ) {
return s == 0;
}
int result = 0;
for( int i = x; i <= 9; ++i ) {
result += sum( n-1, s-i, i+1 );
}
return result;
}
int main() {
ios_base::sync_with_stdio( false );
int n, s;
while( cin >> n >> s ) {
if( n == 0 && s == 0 ) { break; }
cout << sum( n, s, 0 ) << '\n';
}
}
| 0 |
#include <stdio.h>
#include <queue>
using namespace std;
int main(){
int temp;
priority_queue<int> q;
char a[1000];
while(1){
scanf("%s",a);
if(a[2]=='d')return 0;
scanf("%d",&temp);
if(a[2]=='s'){
q.push(temp);
}
if(a[2]=='t'){
printf("%d\n",q.top());
q.pop();
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const long mod=1e9+7;
int main() {
long K,A,B;
cin >> K >> A >> B;
if(A>=B-2)cout << K+1;
else cout << (K-A+1)/2*(B-A)+(K-A+1)%2+A;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main () {
ll n;
cin >> n;
int sol = 0;
for (ll i = 2; i * i <= n; i++) {
if (n % i) continue;
int cur = 0;
while (n % i == 0) {
cur++;
n /= i;
}
for (int j = 1; j <= cur; j++) {
cur -= j;
sol++;
}
}
sol += (n > 1);
cout << sol << '\n';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<long> v(n + 1);
v[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
long x, y;
if (n < k) {
cout << -1;
return 0;
}
x = v[n - k];
y = x;
x++;
cout << x << " " << y;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long cost[100005], INVv[100005];
vector<long long> v[100005];
map<string, long long> mp;
string str[100005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, k, m, sum = 0;
cin >> n >> k >> m;
for (long long i = 1; i <= n; i++) cin >> str[i];
for (long long i = 1; i <= n; i++) cin >> cost[i];
for (long long i = 1; i <= k; i++) {
long long x;
cin >> x;
for (long long j = 1; j <= x; j++) {
long long y;
cin >> y;
v[i].push_back(cost[y]);
INVv[y] = i;
}
}
for (long long i = 1; i <= n; i++) mp[str[i]] = INVv[i];
for (long long i = 1; i <= k; i++) sort(v[i].begin(), v[i].end());
for (long long i = 1; i <= m; i++) {
string tmp;
cin >> tmp;
sum += v[mp[tmp]][0];
}
cout << sum;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-12;
const long long SZ = 55, SSZ = 21, APB = 52, one = 11;
const long long INF = 0x7f7f7f7f, mod = 1000000007;
long long n, m;
char ch[SZ][SSZ];
bool vst[1LL << 20], ok[1LL << 20];
vector<long long> vct;
long long core[1LL << 20], same[SZ][SSZ];
double dp[1LL << 20];
long long last[1LL << 20], num[1LL << 20], fix[1LL << 20];
void init() {
cin >> n;
if (n == 1) {
cout << 0 << endl;
return;
}
double res = 0;
for (long long i = 0; i < n; ++i) cin >> ch[i];
m = strlen(ch[0]);
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
for (long long k = 0; k < n; ++k) {
if (ch[i][j] == ch[k][j]) {
same[i][j] |= 1LL << k;
}
}
}
}
for (long long i = 1; i < (1LL << m); ++i) {
last[i] = __builtin_ctz(i);
num[i] = __builtin_popcount(i);
}
for (long long i = 0; i < n; ++i) {
core[0] = (1LL << n) - 1;
for (long long j = 1; j < (1LL << m); ++j) {
long long pos = last[j];
core[j] = core[j ^ (1LL << pos)] & same[i][pos];
if (__builtin_popcountll(core[j]) == 1) {
fix[j] |= 1LL << i;
}
}
}
dp[0] = 1;
for (long long i = 0; i < (1LL << m) - 1; ++i) {
double psb = dp[i] / num[((1LL << m) - 1) ^ i];
for (long long j = 0; j < m; ++j) {
if (!(i & (1LL << j))) {
dp[i ^ (1LL << j)] += psb;
res += psb * __builtin_popcountll(fix[i ^ (1LL << j)] ^ fix[i]) *
num[i ^ (1LL << j)];
}
}
}
res /= n;
cout << fixed << setprecision(10) << res << endl;
}
void work() {}
int main() {
std::ios::sync_with_stdio(0);
int casenum;
{
init();
work();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, h;
int a[200005], b[200005];
int ans, cur, cnt = 1;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
cin >> n >> h;
for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
ans = b[1] - a[1];
for (int i = 2; i <= n; ++i) {
cur += a[i] - b[i - 1];
while (cur >= h) {
cur -= (a[cnt + 1] - b[cnt]);
++cnt;
}
ans = max(ans, b[i] - a[cnt] - cur);
}
cout << ans + h;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
const long long mod = 1e9 + 7;
int r, g;
long long f[maxn + 10];
int main() {
scanf("%d%d", &r, &g);
int h = 0;
while ((h + 1) * (h + 2) <= 2 * (r + g)) ++h;
f[0] = 1;
for (int i = 1; i <= h; ++i) {
for (int j = r; j >= i; --j)
f[j] = (((f[j] + f[j - i]) >= mod) ? ((f[j] + f[j - i]) - mod)
: (f[j] + f[j - i]));
}
long long ans = 0;
int s = h * (h + 1) / 2;
for (int i = 0; i <= s && i <= r; ++i)
if (s - i <= g)
ans = (((ans + f[i]) >= mod) ? ((ans + f[i]) - mod) : (ans + f[i]));
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[100010], b[100010];
long long expmod(long long a, long long b, long long mod) {
long long rlt = 1;
for (a %= mod; b; a = a * a % mod, b >>= 1)
if (b & 1) rlt = rlt * a % mod;
return rlt;
}
int eval(int x, int P, vector<int>& w) {
int rlt = 0;
for (int i = w.size() - 1; i >= 0; i--) rlt = (1ll * rlt * x + w[i]) % P;
return rlt;
}
void eval(vector<int> w, vector<int> x, int P, vector<int>& rlt) {
rlt.resize(x.size());
vector<int> xx(x.size());
for (int i = 0; i < x.size(); i++) xx[i] = 1ll * x[i] * x[i] % P;
sort(xx.begin(), xx.end());
xx.erase(unique(xx.begin(), xx.end()), xx.end());
if (x.size() == xx.size()) {
for (int i = 0; i < x.size(); i++) rlt[i] = eval(x[i], P, w);
return;
}
vector<int> wo, we;
for (int i = 0; i < w.size(); i++) {
if (i & 1)
wo.push_back(w[i]);
else
we.push_back(w[i]);
}
vector<int> orlt, erlt;
eval(wo, xx, P, orlt);
eval(we, xx, P, erlt);
for (int i = 0; i < x.size(); i++) {
int j = 1ll * x[i] * x[i] % P;
int k = lower_bound(xx.begin(), xx.end(), j) - xx.begin();
assert(k < xx.size());
rlt[i] = (erlt[k] + 1ll * x[i] * orlt[k]) % P;
}
}
bool isPrime(int x) {
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) return 0;
}
return 1;
}
int main() {
scanf("%d%d", &n, &m);
scanf("%s%s", a, b);
for (int i = 0; i < n; i++) a[i] -= b[i];
int un = 1;
int U = min(n, m);
U = min(U, 10000);
while (un < U) un <<= 1;
int up = 1e9;
vector<int> w(n), rlt;
for (int i = 0; i < n; i++) w[i] = a[i];
for (int i = un + 1; i <= up; i += un) {
if (i < m) continue;
if (!isPrime(i)) continue;
vector<int> x;
for (int j = 2; j <= i - 2; j++) x.push_back(j);
eval(w, x, i, rlt);
for (int j = 0; j < x.size(); j++) {
if (rlt[j] == 0) {
cout << i << " " << x[j] << endl;
return 0;
}
}
}
assert(0);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long fac[500005];
map<long long, long long> prime;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = ((res % p) * (x % p)) % p;
y = y >> 1;
x = ((x % p) * (x % p)) % p;
}
return res;
}
long long modInverse(long long n, long long p) { return power(n, p - 2, p); }
long long nCrModPFermat(long long n, long long r, long long p) {
if (r == 0) return 1;
if (r == n) return 1;
return (((fac[n] % p) * (modInverse(fac[r], p) % p)) % p *
(modInverse(fac[n - r], p) % p)) %
p;
}
void sieve(long long n) {
for (long long p = 2; p * p <= n; p++) {
if (!prime[p]) {
for (long long i = p * p; i <= n; i += p) prime[i] = 1;
}
}
}
void fun() {
long long n, k;
cin >> n >> k;
map<long long, long long> mp1, mp2;
for (long long i = 0; i <= 64; i++) {
mp1[i] = 1;
}
long long arr[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
for (long long i = 0; i < n; i++) {
if (arr[i] == 0) continue;
long long n1 = arr[i];
long long ind = 0;
while (n1) {
if (n1 % k == 0) {
n1 = n1 / k;
ind++;
continue;
} else if (n1 % k == 1) {
if (mp1[ind] > 0) {
mp1[ind]--;
ind++;
n1 = n1 / k;
continue;
} else {
cout << "NO\n";
return;
}
} else {
cout << "NO\n";
return;
}
}
}
cout << "YES\n";
}
signed main() {
long long tc;
tc = 1;
cin >> tc;
while (tc--) {
fun();
}
}
| 3 |
#include<stdio.h>
int a[200000];
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int minl=a[1];
int maxl=-1000000000;
for(int i=2;i<=n;i++)
{
if((a[i]-minl)>maxl)
{
maxl=a[i]-minl;
}
if(a[i]<minl)
{
minl=a[i];
}
}
printf("%d\n",maxl);
return 0;
} | 0 |
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long int lli;
const lli maxn = lli(1e5)+5;
lli x[maxn], v[maxn], suff[maxn], pref[maxn];
int main(void)
{
lli n, c;
scanf("%lld%lld", &n, &c);
for(lli i = 1;i <= n;i++) scanf("%lld%lld", &x[i], &v[i]);
lli vsum = 0, res = 0;
for(lli i = 1;i <= n;i++)
{
vsum += v[i];
pref[i] = vsum-x[i];
pref[i] = max(pref[i], pref[i-1]);
res = max(res, pref[i]);
}
vsum = 0;
for(lli i = n;i > 0;i--)
{
vsum += v[i];
suff[i] = vsum-(c-x[i]);
suff[i] = max(suff[i], suff[i+1]);
res = max(res, suff[i]);
res = max(res, suff[i]-(c-x[i])+pref[i-1]);
}
for(lli i = 1;i <= n;i++) res = max(res, pref[i]-x[i]+suff[i+1]);
printf("%lld\n", res);
} | 0 |
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
using namespace std;
constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;
//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
int func(vector<set<pair<int, int>>>xs, vector<set<pair<int, int>>>ys, vector<int>&x, vector<int>&y, vector<char>&c, int st) {
int cx = x[st], cy = y[st];
int ret = 1;
xs[cx].erase(xs[cx].lower_bound({ cy,0 }));
ys[cy].erase(ys[cy].lower_bound({ cx,0 }));
char nx = c[st];
while (ret < N) {
if (nx == '^') {
auto it = xs[cx].lower_bound({ cy,0 });
if (xs[cx].begin() == it)break;
it = prev(it);
ret++;
cy = it->first;
nx = c[it->second];
}
else if (nx == '<') {
auto it = ys[cy].lower_bound({ cx,0 });
if (ys[cy].begin() == it)break;
it = prev(it);
ret++;
cx = it->first;
nx = c[it->second];
}
else if (nx == 'v') {
auto it = xs[cx].lower_bound({ cy,0 });
if (xs[cx].end() == it)break;
ret++;
cy = it->first;
nx = c[it->second];
}
else {
auto it = ys[cy].lower_bound({ cx,0 });
if (ys[cy].end() == it)break;
ret++;
cx = it->first;
nx = c[it->second];
}
xs[cx].erase(xs[cx].lower_bound({ cy,0 }));
ys[cy].erase(ys[cy].lower_bound({ cx,0 }));
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<int>x(N);
vector<int>y(N);
vector<char>c(N);
map<int, int>xp;
map<int, int>yp;
for (int i = 0; i < N; i++) {
cin >> x[i] >> y[i] >> c[i];
xp[x[i]] = 0;
yp[y[i]] = 0;
}
int cnt = 0;
for (auto &i : xp) {
i.second = cnt++;
}
cnt = 0;
for (auto &i : yp) {
i.second = cnt++;
}
vector<set<pair<int, int>>>xx(xp.size());
vector<set<pair<int, int>>>yy(yp.size());
for (int i = 0; i < N; i++) {
x[i] = xp[x[i]];
y[i] = yp[y[i]];
xx[x[i]].insert({ y[i],i });
yy[y[i]].insert({ x[i],i });
}
int ans = 0;
for (int i = 0; i < N; i++) {
ans = max(ans, func(xx, yy, x, y, c, i));
// cout << i << " " << ans << endl;
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<int> bt;
int par[305];
queue<int> pos;
stack<int> sta;
int cnt[305][305];
bool valid;
vector<int> hasil, adj[305];
void bfs(int S, int T) {
memset(par, -1, sizeof(par));
while (!pos.empty()) pos.pop();
pos.push(S);
par[S] = 0;
while (!pos.empty()) {
int u = pos.front();
pos.pop();
if (u == T) break;
for (int z = 0; z < adj[u].size(); z++) {
int v = adj[u][z];
if (par[v] == -1) {
par[v] = u;
pos.push(v);
}
}
}
while (T != 0) {
sta.push(T);
T = par[T];
}
int now = sta.top();
sta.pop();
while (!sta.empty()) {
hasil.push_back(now);
cnt[now][sta.top()]++;
cnt[sta.top()][now]++;
if (cnt[now][sta.top()] > 2) valid = 0;
now = sta.top();
sta.pop();
}
}
int main() {
valid = 1;
int n;
scanf("%d", &n);
for (int a = 1; a < n; a++) {
int l, r;
scanf("%d%d", &l, &r);
adj[l].push_back(r);
adj[r].push_back(l);
}
vector<int> lis;
lis.push_back(1);
string S;
getline(cin, S);
getline(cin, S);
S.push_back(' ');
int tmp = 0;
for (int a = 0; a < S.size(); a++) {
if (isdigit(S[a])) {
tmp *= 10;
tmp += (S[a] - '0');
} else {
lis.push_back(tmp);
tmp = 0;
}
}
lis.push_back(1);
for (int z = 1; z < lis.size(); z++) {
bfs(lis[z - 1], lis[z]);
}
hasil.push_back(1);
if (!valid) {
printf("-1\n");
return 0;
}
for (int z = 0; z < hasil.size(); z++) {
if (z > 0) printf(" ");
printf("%d", hasil[z]);
}
return 0;
}
| 4 |
#include <cstdio>
#include <algorithm>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf("%d\n", std::min({a+b, b+c, c+a}));
} | 0 |
#include <bits/stdc++.h>
long long a[200009], prefix[2 * 200009];
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int t;
cin >> t;
while (t--) {
long long n, k, i;
cin >> n >> k;
for (i = 1; i <= n; i++) cin >> a[i];
memset(prefix, 0, sizeof(prefix));
map<long long, long long> m;
for (i = 1; i <= n / 2; i++) {
long long l = a[i];
long long r = a[n - i + 1];
if (l > r) swap(l, r);
m[l + r]++;
prefix[l + 1]++;
prefix[r + k + 1]--;
}
for (i = 2; i <= 2 * k; i++) {
prefix[i] += prefix[i - 1];
}
long long ans = LONG_LONG_MAX;
for (i = 2; i <= 2 * k; i++) {
long long c0 = m[i];
long long c1 = prefix[i] - c0;
long long c2 = (n / 2) - c0 - c1;
ans = min(ans, c1 + (2 * c2));
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void preprocess(void) { return; }
map<int, int> mm;
void take() {
int k;
cin >> k;
mm.clear();
for (auto i = (0); i < k; i++) {
string st;
cin >> st;
for (auto j = (0); j < st.length(); j++) {
mm[(st[j] - '0')] = i;
}
}
}
bool check() {
if (mm[0] == mm[5]) return true;
return false;
}
bool check2() {
if (mm[6] == mm[0]) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(20);
preprocess();
string st1 = "0 1 2 3 4";
string st2 = "5 7 8 9";
cout << "next " << st2 << endl;
take();
while (!check()) {
cout << "next " << st2 << endl;
take();
cout << "next " << st1 << " " << st2 << endl;
take();
}
int cnt = 1;
cout << "next " << st2 << " " << st1 << endl;
take();
while (!check2()) {
cnt++;
cout << "next " << st2 << " " << st1 << " "
<< "6" << endl;
take();
}
cout << "done" << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long N = (long long)2e5 + 5;
const long long mod = (long long)1e9 + 7;
int main() {
long long n, k, cur;
cin >> n >> k >> cur;
long long res = 0;
for (long long i = 1, x; i < n; i++) {
cin >> x;
long long y = cur % k;
if (y > 0) y = k - y;
y = min(y, x);
cur += y;
res += (cur + k - 1) / k;
cur = x - y;
}
res += (cur + k - 1) / k;
cout << res << '\n';
return 0;
}
| 4 |
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;
string itos (int n )
{
stringstream ss;
ss << n;
return ss.str();
}
void x2n (string & s, int n )
{
string cn = itos (n );
while (s.find ('X' ) != string::npos )
s = s.replace(s.find('X'), 1, cn );
}
string longlongadd (string str1, string str2 )
{
string res = "";
reverse (str1.begin(), str1.end() );
reverse (str2.begin(), str2.end() );
int len1 = str1.length();
int len2 = str2.length();
while (len1 != len2 ){
if (len1 < len2 ){
str1 += '0';
len1 = str1.length();
}else if (len1 > len2 ){
str2 += '0';
len2 = str2.length();
} // end if
} // end while
bool carry = false;
for (int i = 0; i < len1; i++ ){
int num1 = str1[i] - '0';
int num2 = str2[i] - '0';
int sum = num1 + num2;
sum += (carry ? 1 : 0 );
if (sum < 10 ){
res += sum + '0';
if (carry){
carry = false;
} // end if
}else{
sum %= 10;
res += sum + '0';
carry = true;
} // end if
} // end for
if (carry)
res += '1';
reverse (res.begin(), res.end());
return res;
}
int main()
{
string str = "";
while (cin >> str ){
string a = str.substr (0, str.find('+') );
string b = str.substr (str.find ('+' )+1, str.find ('=' ) - str.find('+') - 1 );
string c = str.substr (str.find ('=')+1 );
bool found = false;
for (int i = 0; i < 10; i++ ){
string ca = a, cb = b, cc = c;
x2n (ca, i ), x2n(cb, i ), x2n (cc, i );
string add = longlongadd (ca, cb );
if (add == cc ){
cout << i << endl;
found = true;
} // end if
} // end for
if (!found )
cout << "NA" << endl;
} // end loop
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MAX_LG = 20;
namespace DSU {
int p[MAX_N], r[MAX_N];
void init(int n) {
iota(p, p + n + 1, 0);
fill_n(r, n + 1, 0);
}
int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); }
bool union_(int x, int y) {
x = find(x), y = find(y);
if (x == y) return 0;
if (r[x] < r[y]) swap(x, y);
p[y] = x;
r[x] += r[x] == r[y];
return 1;
}
} // namespace DSU
int n, m;
vector<array<int, 2>> g[MAX_N];
int anc[MAX_LG][MAX_N], dep[MAX_N];
int dist[MAX_LG][MAX_N];
void dfs(int u) {
for (int i = 1; i < MAX_LG; i++) {
anc[i][u] = anc[i - 1][anc[i - 1][u]];
dist[i][u] = max(dist[i - 1][u], dist[i - 1][anc[i - 1][u]]);
}
for (auto &e : g[u])
if (e[0] != anc[0][u]) {
anc[0][e[0]] = u;
dist[0][e[0]] = e[1];
dep[e[0]] = dep[u] + 1;
dfs(e[0]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<array<int, 3>> ed(m);
for (auto &e : ed) cin >> e[0] >> e[1] >> e[2];
vector<int> idx(m);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(),
[&](int i, int j) { return ed[i][2] < ed[j][2]; });
DSU::init(n);
vector<int> q;
for (int i : idx) {
auto &e = ed[i];
if (DSU::union_(e[0], e[1])) {
g[e[0]].push_back({e[1], e[2]});
g[e[1]].push_back({e[0], e[2]});
} else {
q.push_back(i);
}
}
anc[0][1] = 1;
dfs(1);
sort(q.begin(), q.end());
for (int i : q) {
int u = ed[i][0], v = ed[i][1];
if (dep[u] > dep[v]) swap(u, v);
int ans = 0;
for (int i = MAX_LG - 1; i >= 0; i--)
if (dep[v] - (1 << i) >= dep[u]) {
ans = max(ans, dist[i][v]);
v = anc[i][v];
}
if (u != v) {
for (int i = MAX_LG - 1; i >= 0; i--)
if (anc[i][u] != anc[i][v]) {
ans = max(ans, max(dist[i][u], dist[i][v]));
u = anc[i][u], v = anc[i][v];
}
ans = max(ans, max(dist[0][u], dist[0][v]));
}
cout << ans << "\n";
}
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
const int P=998244353;
const int N=2e4+5;
int n,m,k,len,r[N];
long long ans,Inv,f[N],g[N],h[N],fac[N],inv[N];
long long fsp(long long x,int y){
long long ans=1;
while(y){
if(y&1) ans=ans*x%P;
y>>=1,x=x*x%P;
}
return ans;
}
void NTT(long long *d,int f){
for(int i=1;i<n;i++) if(i<r[i]) swap(d[i],d[r[i]]);
for(int i=1;i<n;i<<=1){
int wn=fsp(3,(P-1)/(i<<1));
if(f) wn=fsp(wn,P-2);
for(int j=0;j<n;j+=i<<1){
long long w=1;
for(int k=0;k<i;k++,w=w*wn%P){
int x=d[j+k],y=w*d[i+j+k]%P;
d[j+k]=(x+y)%P,d[i+j+k]=(x-y+P)%P;
}
}
}
if(f) for(int i=0;i<n;i++) d[i]=d[i]*Inv%P;
}
long long C(int n,int m){
return fac[n]*inv[m]%P*inv[n-m]%P;
}
int main(){
scanf("%d%d",&k,&m),fac[0]=inv[0]=1;
for(int i=1;i<=k+2;i++){
fac[i]=fac[i-1]*i%P;
inv[i]=fsp(fac[i],P-2);
}
for(;(1<<len)<=k*2;++len);
n=1<<len,Inv=fsp(n,P-2),g[0]=1;
for(int i=1;i<=k;i++) h[i]=inv[i+2];
for(int i=1;i<n;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(len-1));
NTT(h,0);
while(m--){
for(int i=0;i<=k;i++) f[i]=(1LL*i*(i-1)/2+i+1)*g[i]%P;
for(int i=0;i<=k;i++) g[i]=g[i]*inv[i]%P;
NTT(g,0);
for(int i=0;i<n;i++) g[i]=g[i]*h[i]%P;
NTT(g,1);
for(int i=k+1;i<n;i++) f[i]=0;
for(int i=0;i<=k;i++) f[i]=(f[i]+g[i]*fac[i+2])%P;
swap(f,g);
}
for(int i=0;i<=k;i++) ans=(ans+g[i]*C(k,i))%P;
printf("%lld\n",ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
bool cmpx(const pll& a, const pll& b) { return a.first < b.first; }
bool cmpy(const pll& a, const pll& b) { return a.second < b.second; }
ll sq(const ll& first) { return first * first; }
ll dis(const pll& a, const pll& b) {
return sq(a.first - b.first) + sq(a.second - b.second);
}
vector<pll> px, py;
ll closest_pair(int l, int r) {
if (r - l <= 1) return 1e18;
int m = (l + r) / 2;
ll d = min(closest_pair(l, m), closest_pair(m, r));
inplace_merge(py.begin() + l, py.begin() + m, py.begin() + r, cmpy);
vector<pll> strip;
for (int i = l; i < r; i++)
if (sq(py[i].first - px[m].first) < d) strip.push_back(py[i]);
for (int i = 0; i < strip.size(); i++)
for (int j = i + 1;
j < strip.size() && sq(strip[j].second - strip[i].second) < d; j++)
d = min(d, dis(strip[i], strip[j]));
return d;
}
void solve() {
int n;
cin >> n;
ll cur = 0, first;
for (int i = 0; i < n; i++) {
cin >> first;
cur += first;
px.emplace_back(i, cur);
}
sort(px.begin(), px.end(), cmpx);
py = px;
cout << closest_pair(0, n) << '\n';
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int owo = 1;
while (owo--) solve();
}
| 4 |
#include <bits/stdc++.h>
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
using namespace std;
long long dp[5003][5003];
signed main() {
long long n = read();
dp[0][0] = 1;
for (long long i = 1; i <= n; i++) {
dp[i][0] = dp[i - 1][i - 1];
for (long long j = 1; j <= i; j++)
dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % 1000000007LL;
}
printf("%lld\n", dp[n][n - 1]);
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main()
{
std::string s1, s2;
std::cin >> s1 >> s2;
using vi = std::vector<int>;
std::vector<vi> dp(s1.size() + 1, (vi(s2.size() + 1)));
for (int i{}; i <= (int)s1.size(); i++)
dp[i][0] = i;
for (int i{}; i <= (int)s2.size(); i++)
dp[0][i] = i;
for (int i{1}; i <= (int)s1.size(); i++)
for (int j{1}; j <= (int)s2.size(); j++)
{
if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = dp[i - 1][j - 1] + 1;
dp[i][j] = std::min({dp[i][j], dp[i - 1][j] + 1, dp[i][j - 1] + 1});
}
printf("%d\n", dp.back().back());
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void update(long long &a, long long b) {
if (b > a) a = b;
}
long long qmin(long long a, long long b) { return (a < b) ? a : b; }
int T;
int n;
pair<int, long long> a[5050];
long long dp[2][5050];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%lld", &a[i].first, &a[i].second);
}
sort(a, a + n);
int now = 0;
long long sum_all = 0;
for (int i = 0; i < n; i++) sum_all += a[i].second;
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++) {
int now = i & 1, nxt = (i + 1) & 1;
memset(dp[nxt], 0, sizeof(dp[nxt]));
for (int j = 0; j < n + 1; j++) {
int val = qmin(0LL + j - 1, 0LL + n - a[i].first - 1);
if (val >= 0) update(dp[nxt][val], dp[now][j] + a[i].second);
update(dp[nxt][j], dp[now][j]);
}
}
int pos = (n & 1);
long long ans = sum_all;
for (int i = 0; i < n + 1; i++) {
ans = qmin(ans, sum_all - dp[pos][i]);
}
cout << ans << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, i, j, cnt = 0;
cin >> n;
set<char> s;
for (i = 0; i < 26; i++) s.insert(i + 'a');
for (i = 0; i < n - 1; i++) {
char c;
string str;
cin >> c >> str;
if (c == '!') {
set<char> temp;
for (j = 0; j < (int)str.size(); j++) {
if ((s.find(str[j]) != s.end())) temp.insert(str[j]);
}
temp.swap(s);
} else if (c == '.') {
for (j = 0; j < (int)str.size(); j++) {
if ((s.find(str[j]) != s.end())) s.erase(str[j]);
}
} else {
if ((s.find(str[0]) != s.end())) s.erase(str[0]);
}
if ((int)s.size() == 1) break;
}
for (j = i + 1; j <= n - 2; j++) {
char c;
string str;
cin >> c >> str;
if (c == '!' || c == '?') cnt++;
}
char c;
string str;
cin >> c >> str;
cout << cnt << "\n";
}
| 3 |
#include<iostream>
using namespace std ;
int main(){
long long r,d,x ;
cin >> r >> d >> x ;
for(int i=0;i<10;i++){
x = x*r-d ;
cout << x << endl ;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tmp;
cin >> tmp;
char c;
set<long long> s;
long long a;
string t;
map<long long, int> mp;
map<string, int> res;
while (tmp--) {
cin >> c;
if (c == '+') {
cin >> a;
if (mp[a] == 0) {
s.insert(a);
mp[a]++;
t.clear();
while (a) {
if (a % 2)
t = '1' + t;
else
t = '0' + t;
a /= 10;
}
while (t.length() != 18) {
t = '0' + t;
}
res[t]++;
} else {
mp[a]++;
t.clear();
while (a) {
if (a % 2)
t = '1' + t;
else
t = '0' + t;
a /= 10;
}
while (t.length() != 18) {
t = '0' + t;
}
res[t]++;
}
} else if (c == '-') {
cin >> a;
if (mp[a] == 1) {
s.erase(a);
}
t.clear();
mp[a]--;
while (a) {
if (a % 2)
t = '1' + t;
else
t = '0' + t;
a /= 10;
}
while (t.length() != 18) {
t = '0' + t;
}
res[t]--;
} else {
cin >> t;
while (t.length() != 18) {
t = '0' + t;
}
cout << res[t] << endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, i, j;
int q[100], s[100], r[100];
int p[201][100];
int up, down;
bool trouveup = false, trouvedown = false, trivial = true, ok;
scanf("%d %d\n", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d", &q[i]);
q[i]--;
r[q[i]] = i;
}
for (i = 0; i < n; i++) {
scanf("%d", &s[i]);
s[i]--;
if (i != s[i]) trivial = false;
}
if (trivial) {
printf("NO\n");
return 0;
}
for (i = 0; i < n; i++) p[100][i] = i;
for (i = 1; i <= k; i++) {
for (j = 0; j < n; j++) {
p[100 + i][j] = p[99 + i][q[j]];
p[100 - i][j] = p[101 - i][r[j]];
}
if (!trouveup) {
ok = true;
for (j = 0; j < n; j++) {
if (p[100 + i][j] != s[j]) ok = false;
}
if (ok) {
trouveup = true;
up = i;
}
}
if (!trouvedown) {
ok = true;
for (j = 0; j < n; j++) {
if (p[100 - i][j] != s[j]) ok = false;
}
if (ok) {
trouvedown = true;
down = i;
}
}
}
if (!trouveup && !trouvedown) {
printf("NO\n");
} else if (!trouveup && trouvedown) {
if ((down + k) % 2 == 0)
printf("YES\n");
else
printf("NO\n");
} else if (!trouvedown && trouveup) {
if ((up + k) % 2 == 0)
printf("YES\n");
else
printf("NO\n");
} else {
if (k == 1) {
if (up == 1 || down == 1)
printf("YES\n");
else
printf("NO\n");
} else {
if (up == 1 && down == 1)
printf("NO\n");
else {
if ((up + k) % 2 == 0 || (down + k) % 2 == 0)
printf("YES\n");
else
printf("NO\n");
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int X,Y,Z;
int main () {
cin >> X >> Y>> Z;
cout << (X-Z)/(Y+Z) << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, k;
cin >> n >> k;
priority_queue<pair<int, int> > p;
set<int> s;
for (int i = 1; i <= n; i++) {
int in;
cin >> in;
p.push({in, i});
s.insert(i + k);
}
long long ans = 0;
vector<int> v(n);
while (p.size()) {
int c = p.top().first, d = p.top().second;
p.pop();
auto it = s.lower_bound(d);
ans += 1LL * (*it - d) * c;
v[d - 1] = *it;
s.erase(it);
}
cout << ans << "\n";
for (auto i : v) cout << i << " ";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char symbol;
int a[50][50];
int n, m, answer;
bool Satisfy(int i1, int j1, int i2, int j2) {
int square;
square = a[i2][j2] - a[i1 - 1][j2] - a[i2][j1 - 1] + a[i1 - 1][j1 - 1];
return !square;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> symbol;
a[i][j] = (symbol == '1');
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int ii = i; ii <= n; ii++) {
for (int jj = j; jj <= m; jj++) {
if (Satisfy(i, j, ii, jj)) {
int p = 2 * (ii + jj - i - j + 2);
answer = max(p, answer);
}
}
}
}
}
printf("%d\n", answer);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
struct FBS {
int n, m;
vector<int> father;
void init(int n) {
this->n = n;
father.resize(n + 2);
for (int i = 1; i <= n; i++) father[i] = -1;
}
int find(int x) {
int p = x;
while (father[p] > 0) p = father[p];
while (x != p) {
int t = father[x];
father[x] = p;
x = t;
}
return x;
}
bool bind(int a, int b) {
int fa = find(a), fb = find(b);
if (fa == fb) return 0;
if (father[fa] < father[fb]) {
father[fa] += father[fb];
father[fb] = fa;
} else {
father[fb] += father[fa];
father[fa] = b;
}
return 1;
}
} fbs;
int A[1000 + 2], B[1000 + 2], cnt[1000 + 2];
int K[2][1000 + 2][1000 + 2], gd[1000 + 2];
long long DP[1000 + 2];
int main() {
int n, m, w;
scanf("%d%d%d", &n, &m, &w);
fbs.init(n);
for (int i = 1; i <= n; i++) scanf("%d", A + i);
for (int i = 1; i <= n; i++) scanf("%d", B + i);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
fbs.bind(a, b);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
int x = fbs.find(i);
if (!gd[x]) gd[x] = ++ans;
K[0][gd[x]][++cnt[gd[x]]] = A[i];
K[1][gd[x]][cnt[gd[x]]] = B[i];
K[0][gd[x]][0] += A[i];
K[1][gd[x]][0] += B[i];
}
for (int i = 1; i <= ans; i++)
for (int j = w; j >= 0; j--)
for (int k = 0; k <= (cnt[i] == 1 ? 0 : cnt[i]); k++)
if (K[0][i][k] <= j)
DP[j] = max(DP[j], DP[j - K[0][i][k]] + K[1][i][k]);
printf("%I64d\n", DP[w]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
long long n, w;
int ar[int(2e5) + 5];
long long calc() {
if (w % 2 == 1) {
return w / 2 + 1;
}
return w / 2;
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n >> w;
vec.clear();
for (int i = 0; i < n; i++) {
cin >> ar[i];
}
long long cnt = 0;
for (int i = 0; i < n; i++) {
if (cnt + 1ll * ar[i] > w) {
if (ar[i] > w) {
continue;
}
if (ar[i] >= calc()) {
vec.clear();
cnt = w / 2 + 1;
vec.push_back(i);
break;
}
} else {
cnt += ar[i];
vec.push_back(i);
if (cnt >= calc()) {
break;
}
}
}
if (cnt < calc()) {
cout << -1 << endl;
continue;
}
cout << vec.size() << endl;
for (auto a : vec) {
cout << a + 1 << " ";
}
cout << endl;
}
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n;
std::vector<int> A;
std::vector<int> B;
std::vector<int> actions;
std::cin >> n;
for (int i = 0; i < n; i++) {
int t;
std::cin >> t;
A.push_back(t);
}
for (int i = 0; i < n; i++) {
int t;
std::cin >> t;
B.push_back(t);
}
for (int i = 0; i < A.size(); i++) {
if (B[i] == A[i]) continue;
int j = (std::find(B.begin() + i, B.end(), A[i]) - B.begin());
while (j != i) {
int t = B[j];
B[j] = B[j - 1];
B[j - 1] = B[j];
j--;
actions.push_back(j);
}
}
std::cout << actions.size() << "\n";
for (int i = 0; i < actions.size(); i++) {
int j = actions[i] + 1;
std::cout << j << ' ' << j + 1 << '\n';
}
}
| 4 |
#include <iostream>
using namespace std;
int k,x;
int main()
{
cin>>k>>x;
for(int i=x-k+1;i<=x+k-1;i++)
cout<<i<<" ";
cout<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
int arr[100010];
int gd(int str) {
int cnt = 0;
if (arr[str] == 0) {
for (int i = str + 1; i < n; i++)
if (arr[i] != 0) cnt++;
return min(cnt, 2);
}
for (int i = str + 1; i + 1 < n && cnt < 2; i++) {
if (arr[str] * arr[i + 1] != arr[str + 1] * arr[i]) {
cnt++;
if (i + 2 < n && arr[str] * arr[i + 2] != arr[str + 1] * arr[i]) cnt++;
i++;
}
}
return min(cnt, 2);
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> arr[i];
if (n == 1) {
cout << 0 << endl;
return 0;
}
int t1 = gd(0);
int t2 = gd(1);
arr[1] = arr[0];
int t3 = gd(1);
if (t1 == 0)
cout << 0 << endl;
else if (t1 == 1 || t2 == 0 || t3 == 0)
cout << 1 << endl;
else
cout << 2 << endl;
return 0;
}
| 4 |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(){
ios_base::sync_with_stdio(false);
ll n, m, k;
cin >> n >> m >> k;
vector<ll> p;
const ll init = n % 10;
ll x = init;
do {
p.push_back(x);
x = (x * init) % 10;
} while(x != init);
ll len = 0, sum = 0, pos = 0;
do {
++len;
sum += p[pos];
pos = (pos + m) % p.size();
} while(pos > 0);
ll answer = sum * (k / len);
ll i = (k / len) * len;
while(i < k){
answer += p[pos];
pos = (pos + m) % p.size();
++i;
}
cout << answer << endl;
return 0;
}
| 0 |
#include<iostream>
#include<string>
using namespace std;
int main(){
int N;
string S;
cin >> N >> S;
int a=-1;
int b=-1;
int c=-1;
int count=0;
for(int i=1000;i<2000;i++){
int j=i%10;
int k=((i-j)/10)%10;
int l=(i-1000)/100;
int c[3];
c[0]=l;c[1]=k;c[2]=j;
int f=0;
for(int m=0;m<N;m++){
if(S[m]=='0'+c[f])f++;
if(f==3) break;
}
if(f==3)count++;
}
cout << count << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, k;
map<string, int> mp;
string Key[1005];
string GerStr() {
string res = "", s = "";
char c;
while ((c = getchar()) != '(')
if (c != ' ') res += c;
res += '|';
while ((c = getchar()) != '\n') {
if (isalnum(c))
s += c;
else if (c == ',' || c == ')') {
res += (mp[s] + 48);
s = "";
}
}
return res;
}
bool Check(string A, string B) {
bool f = 0;
if (A.size() != B.size()) return false;
for (int i = 0; i < A.size(); i++) {
if (A[i] == '|') f = 1;
if (A[i] == '0' && f)
continue;
else if (A[i] != B[i])
return false;
}
return true;
}
int main() {
mp["T"] = 0;
mp["int"] = 1;
mp["double"] = 2;
mp["string"] = 3;
cin >> n;
for (int i = 0; i < n; i++) {
string t;
cin >> t;
Key[i] = GerStr();
}
cin >> m;
for (int i = 0; i < m; i++) {
string A, B;
cin >> A >> B;
mp[B] = mp[A];
}
cin >> k;
getchar();
for (int i = 0; i < k; i++) {
int ans = 0;
string s = GerStr();
for (int j = 0; j < n; j++) {
if (Check(Key[j], s)) ans++;
}
cout << ans << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool check(long long x, long long pos) { return (x & (1LL << pos)); }
int main() {
long long s, xr, nd;
cin >> s >> xr;
if ((s - xr) % 2) {
cout << "0\n";
return 0;
}
nd = (s - xr) / 2;
long long ans = 1;
bool no = false;
for (long long i = 0; i < 63; i++) {
if (!check(nd, i) && check(xr, i)) {
ans *= 2;
}
if (check(nd, i) && check(xr, i)) no = true;
}
if (no) {
cout << "0\n";
return 0;
}
if (nd == 0) ans -= 2;
cout << ans << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
bool accept(long long u);
long long n, k, a[100010], b[100010], dau, cuoi, mid;
int main() {
ios::sync_with_stdio(0);
cin >> n >> k;
for (long long i = (1), _b = (n); i <= _b; i++) cin >> a[i];
for (long long i = (1), _b = (n); i <= _b; i++) cin >> b[i];
dau = 0;
cuoi = 2000000000;
while (dau <= cuoi) {
mid = (dau + cuoi) >> 1;
if (accept(mid))
dau = mid + 1;
else
cuoi = mid - 1;
}
cout << cuoi;
return 0;
}
bool accept(long long u) {
long long p = k;
for (long long i = (1), _b = (n); i <= _b; i++) {
long long o = u * a[i];
if (o <= b[i]) continue;
o -= b[i];
if (o > p) return false;
p -= o;
}
return true;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
const int mod = 1000000007;
int main() {
int x, y, z, k;
cin >> x >> y >> z >> k;
vector<ll> a(x), b(y), c(z);
rep(i, x) cin >> a[i];
rep(i, y) cin >> b[i];
rep(i, z) cin >> c[i];
vector<ll> ab;
rep(i, x)rep(j, y) ab.push_back(a[i] + b[j]);
sort(ab.rbegin(), ab.rend());
ab.resize(k);
vector<ll> abc;
rep(i, ab.size())rep(j, z) abc.push_back(ab[i] + c[j]);
sort(abc.rbegin(), abc.rend());
rep(i, k) cout << abc[i] << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int dp[100010][3];
vector<int> v;
int n;
int solve(int i, int op, int last) {
if (i == n) {
return 0;
}
if (dp[i][op] != -1) {
return dp[i][op];
}
int ans = 0x3f3f3f3f;
int aux = op;
if (aux == 2) {
aux = -1;
}
int aux2 = last;
if (aux2 == 2) {
aux2 = -1;
}
if ((v[i] - v[i - 1] + 1 - aux) == (v[i - 1] + aux - v[i - 2] - aux2)) {
ans = min(ans, 1 + solve(i + 1, 1, op));
}
if ((v[i] - v[i - 1] - aux) == (v[i - 1] + aux - v[i - 2] - aux2)) {
ans = min(ans, solve(i + 1, 0, op));
}
if ((v[i] - v[i - 1] - 1 - aux) == (v[i - 1] + aux - v[i - 2] - aux2)) {
ans = min(ans, 1 + solve(i + 1, 2, op));
}
return dp[i][op] = ans;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n;
v.resize(n, 0);
for (int i = 0; i < (n); i++) {
cin >> v[i];
}
memset(dp, -1, sizeof dp);
int ans = 0x3f3f3f3f;
if (n <= 2) {
cout << 0 << '\n';
return 0;
}
for (int i = 0; i < (3); i++) {
for (int j = 0; j < (3); j++) {
memset(dp, -1, sizeof dp);
int aux = (i > 0) + (j > 0);
ans = min(ans, aux + solve(2, i, j));
}
}
if (ans > n + 1) {
cout << -1 << '\n';
} else {
cout << ans << '\n';
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
int main() {
long long n, m, k;
cin >> n >> m >> k;
bool isEven = k % 2 == 0;
long long p = n * m;
if (isEven) k /= 2;
if (p % k != 0) {
cout << "NO" << endl;
return 0;
}
long long x = gcd(n, k);
k /= x;
long long a = n / x;
x = gcd(m, k);
k /= x;
assert(k == 1);
long long b = m / x;
if (!isEven) {
if (a < n)
a += a;
else {
assert(b < m);
b += b;
}
}
cout << "YES" << endl;
cout << "0 0\n";
cout << 0 << ' ' << b << endl;
cout << a << ' ' << 0 << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2002;
pair<int,int> arr[N];
int dp[N][N];
int solve(int i,int l,int r)
{
if(l > r) return 0;
int & ans = dp[l][r];
if(ans != -1) return ans;
ans = 0;
// place at left
ans = arr[i].first*abs(l - arr[i].second) + solve(i+1,l+1,r);
// place at right
ans = max(ans,arr[i].first*abs(r - arr[i].second) + solve(i+1,l,r-1));
return ans;
}
int32_t main() {
int n; cin>>n;
memset(dp,-1,sizeof dp);
for(int i = 0;i<n;i++)
{
cin>>arr[i].first;
arr[i].second = i;
}
sort(arr,arr + n);
reverse(arr,arr + n);
cout<<solve(0,0,n-1);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string s1, s2;
long long int x = 0, y = 0;
bool check() {
if (x > y) {
y = x - 1;
if (s2[y + 1] != '\0') {
if (s1[x] == '0' && s2[y] == '0' && s2[y + 1] == '0') {
s1[x] = '-';
s2[y] = '-';
s2[y + 1] = '-';
return true;
}
}
y = x;
if (s2[y + 1] != '\0') {
if (s1[x] == '0' && s2[y] == '0' && s2[y + 1] == '0') {
s1[x] = '-';
s2[y] = '-';
s2[y + 1] = '-';
return true;
}
}
if (x >= 0) {
if (s1[x] == '0' && s1[x - 1] == '0' && s2[y] == '0') {
s1[x] = '-';
s1[x - 1] = '-';
s2[y] = '-';
return true;
}
}
if (s1[x + 1] != '\0') {
if (s1[x] == '0' && s1[x + 1] == '0' && s2[y] == '0') {
s1[x] = '-';
s1[x + 1] = '-';
s2[y] = '-';
return true;
}
}
} else {
x = y - 1;
if (x >= 0) {
if (s2[y] == '0' && s1[x] == '0' && s1[x + 1] == '0') {
s2[y] = '-';
s1[x] = '-';
s1[x + 1] = '-';
return true;
}
}
x = y;
if (s1[x + 1] != '\0') {
if (s2[y] == '0' && s1[x] == '0' && s1[x + 1] == '0') {
s2[y] = '-';
s1[x] = '-';
s1[x + 1] = '-';
return true;
}
}
if (y >= 0) {
if (s2[y] == '0' && s2[y - 1] == '0' && s1[x] == '0') {
s2[y] = '-';
s2[y - 1] = '-';
s1[x] = '-';
return true;
}
}
if (s2[y + 1] != '\0') {
if (s2[y] == '0' && s2[y + 1] == '0' && s1[x] == '0') {
s2[y] = '-';
s2[y + 1] = '-';
s1[x] = '-';
return true;
}
}
}
if (s1[x] == '0' && s2[y] == '0') {
x++;
y++;
return false;
}
if (s1[x] == 'X') x++;
if (s2[y] == 'X') y++;
return false;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> s1 >> s2;
long long int c = 0;
while (1) {
while (s1[x] != '0' && s1[x] != '\0') x++;
while (s2[y] != '0' && s2[y] != '\0') y++;
if (check()) {
c++;
}
if (s1[x] == '\0' && s2[y] == '\0') break;
}
cout << c;
return 0;
}
| 4 |
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int> path[100005];
int t[100005];
int s[100005];
queue<int> q1;
int main(){
int n,m;
int i,j,k;
int a,b;
cin>>n>>m;
for(i=0;i<n-1+m;i++){
cin>>a>>b;
a--,b--;
t[b]++;
path[a].push_back(b);
}
for(i=0;i<n;i++){
if(t[i]==0){a=i;break;}
}
q1.push(a);
while(!q1.empty()){
a=q1.front(),q1.pop();
if(path[a].size()==0)continue;
for(i=0;i<path[a].size();i++){
b=path[a][i];
t[b]--;
if(t[b]==0){
s[b]=a+1;
q1.push(b);
}
}
}
for(i=0;i<n;i++){
cout<<s[i]<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
long long num_items(long long num_pairs) {
double delta_sqrt_float = sqrt(1 + 8 * num_pairs);
long long delta_sqrt = (long long)delta_sqrt_float;
if (delta_sqrt_float - delta_sqrt != 0) return -1;
long long result;
long long result_1 = (1 + delta_sqrt);
long long result_2 = (1 - delta_sqrt);
if (result_1 < 0 || result_1 % 2 != 0) {
if (result_2 < 0 || result_2 % 2 != 0)
return -1;
else
result = result_2 / 2;
} else
result = result_1 / 2;
return result;
}
int main() {
long long a00, a01, a10, a11;
scanf("%I64d %I64d %I64d %I64d", &a00, &a01, &a10, &a11);
long long n0 = num_items(a00);
long long n1 = num_items(a11);
if (n0 == -1 || n1 == -1) {
printf("Impossible\n");
return 0;
}
if (n1 == 1 && a01 + a10 == n0 * 0)
n1 = 0;
else if (n0 == 1 && a01 + a10 == 0 * n1) {
for (long long i = 0; i < n1; i++) printf("1");
printf("\n");
return 0;
} else if (a01 + a10 != n0 * n1) {
printf("Impossible\n");
return 0;
}
long long offset = a10 % n0;
for (long long i = 0; i < a10 / n0; i++) printf("1");
for (long long i = 0; i < n0 - a10 % n0; i++) printf("0");
if (offset != 0) printf("1");
for (long long i = 0; i < offset; i++) printf("0");
for (long long i = 0; i < n1 - a10 / n0 - (offset != 0 ? 1 : 0); i++)
printf("1");
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
char c=getchar();bool f=0;x=0;
while(!isdigit(c)) f|=c=='-',c=getchar();
while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
if(f) x=-x;return x;
}
template<class t> inline void write(t x){
if(x<0) putchar('-'),write(-x);
else{if(x>9) write(x/10);putchar('0'+x%10);}
}
const int N=2005;
int n,m,ans,f[N][N],top,s[N],l[N],r[N];
char c[N][N];
signed main(){
read(n);read(m);
ans=max(n,m);
n--;m--;
for(int i=0;i<=n;i++){
scanf("%s",c[i]);
for(int j=0;j<=m;j++) c[i][j]=c[i][j]=='.';
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) if((c[i][j]+c[i-1][j]+c[i-1][j-1]+c[i][j-1])%2==0)
f[i][j]=f[i-1][j]+1;
for(int i=1;i<=n;i++){
f[i][s[top=1]=0]=-1;
for(int j=1;j<=m;j++){
while(top&&f[i][s[top]]>=f[i][j]) top--;
l[j]=s[top]+1;
s[++top]=j;
}
f[i][s[top=1]=m+1]=-1;
for(int j=m;j;j--){
while(top&&f[i][s[top]]>=f[i][j]) top--;
r[j]=s[top]-1;
s[++top]=j;
}
for(int j=1;j<=m;j++) ans=max(ans,(f[i][j]+1)*(r[j]-l[j]+2));
}
write(ans);
} | 0 |
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
#define int long long
#define inf 1000000007
#define mp make_pair
#define pb push_back
#define ppa pair<int,pair<int,int>>
using namespace std;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int a,b,c,d;
int yaru(int sx,int sy,int gx,int gy){
int d1=abs(gx-sx)+abs(gy-sy);
int d2=0;
if(sx<a){
if(sy<b)d2+=a-sx+b-sy-1;
else if(sy<=d) d2+=a-sx-1;
else d2+=a-sx+sy-d-1;
}
else if(sx<=c){
if(sy<b)d2+=b-sy-1;
else if(sy<=d) d2+=0;
else d2+=sy-d-1;
}
else{
if(sy<b)d2+=sx-c+b-sy-1;
else if(sy<=d) d2+=sx-c-1;
else d2+=sx-c+sy-d-1;
}
if(gx<=a){
if(gy<=b)d2+=a-gx+b-gy;
else if(gy<=d) d2+=a-gx;
else d2+=a-gx+gy-d;
}
else if(gx<=c){
if(gy<=b)d2+=b-gy;
else if(gy<=d) d2+=0;
else d2+=gy-d;
}
else{
if(gy<=b)d2+=gx-c+b-gy;
else if(gy<=d) d2+=gx-c;
else d2+=gx-c+gy-d;
}
return min(d1,d2);
}
int solve(){
int n;
cin>>n;
if(n==0) exit(0);
cin>>a>>b>>c>>d;
int sx,sy;
cin>>sx>>sy;
int gx,gy;
int ans=0;
for(int i=0;i<n;i++){
cin>>gx>>gy;
ans+=yaru(sx,sy,gx,gy);
sx=gx;
sy=gy;
}
return ans;
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
while(1){
cout<<solve()<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool gg[2000];
int sz[2000], par[2000];
char s[100][2000];
int dp[2000][2000];
int C(int n, int m) {
if (n < m || m < 0) return 0;
if (n == 0 || n == m) return 1;
if (dp[n][m]) return dp[n][m];
if (n - m < m) return dp[n][m] = C(n, n - m);
return dp[n][m] = (C(n - 1, m - 1) + C(n - 1, m)) % 1000000007;
}
int main() {
int m, n, r = 1;
scanf("%d%d", &m, &n);
for (int i = 0; i < n; i++) scanf("%s", s[i]);
par[0] = 1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < i; j++) {
par[i] = (par[i] + 1LL * par[j] * C(i - 1, j) % 1000000007) % 1000000007;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < i; j++) {
if (gg[j]) continue;
gg[i] = true;
for (int k = 0; k < n; k++) {
if (s[k][j] != s[k][i]) {
gg[i] = false;
break;
}
}
if (gg[i]) {
sz[j]++;
break;
}
}
if (!gg[i]) sz[i] = 1;
}
for (int i = 0; i < m; i++) {
if (!gg[i]) r = 1LL * r * par[sz[i]] % 1000000007;
}
printf("%d\n", r);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n;
long double p, q;
long double a[N], b[N];
inline long double F(long double x) {
long double y = 1e130;
for (int i = 1; i <= n; i++) y = min(y, (1.0 - a[i] * x) / b[i]);
return p * x + q * y;
}
int main() {
double _p, _q, _a, _b;
scanf("%d%lf%lf", &n, &_p, &_q), p = _p, q = _q;
for (int i = 1; i <= n; i++) scanf("%lf%lf", &_a, &_b), a[i] = _a, b[i] = _b;
long double L = 0, R = 1e130, m1, m2;
for (int i = 1; i <= n; i++) R = min(R, 1.0 / a[i]);
while (R - L > 1e-15)
if (F(m1 = L + (R - L) / 3.0) < F(m2 = R - (R - L) / 3.0))
L = m1;
else
R = m2;
printf("%.15lf\n", (double)F((R + L) / 2));
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = numeric_limits<long long>::max();
const long long nax = (long long)(100001);
const long long mod = 1e9 + 7;
template <class X, class Y>
bool maximize(X& x, const Y y) {
if (y > x) {
x = y;
return true;
}
return false;
}
template <class X, class Y>
bool minimize(X& x, const Y y) {
if (y < x) {
x = y;
return true;
}
return false;
}
long long a[nax];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long tt;
cin >> tt;
while (tt--) {
long long n;
cin >> n;
long long sum = 0;
for (long long i = 0; i < n; ++i) {
cin >> a[i];
sum += a[i];
}
if (sum % (n * (n + 1) / 2)) {
cout << "NO" << '\n';
continue;
}
sum /= (n * (n + 1) / 2);
long long flag = 1;
for (long long i = 0; i < n; ++i) {
if ((sum + a[(i - 1 + n) % n] - a[i]) % n ||
(sum + a[(i - 1 + n) % n] - a[i]) < 1) {
flag = 0;
}
}
if (!flag) {
cout << "NO" << '\n';
continue;
}
cout << "YES" << '\n';
for (long long i = 0; i < n; ++i) {
cout << (sum + a[(i - 1 + n) % n] - a[i]) / n << " ";
}
cout << '\n';
for (long long i = 0; i < n; ++i) {
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string k;
cin >> k;
int st = 0;
while (st < (int)k.length() && k[st] == 'a' && 1) {
st++;
}
if (st == (int)k.length()) {
k[k.length() - 1] = 'z';
cout << k << endl;
return 0;
}
int se = st + 1;
while (se < (int)k.length() && k[se] != 'a' && 1) {
se++;
}
se--;
for (int i = st; i <= se; i++) {
k[i]--;
}
cout << k << endl;
}
| 3 |
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
if (a+b+c <=21)
cout << "win";
else
cout << "bust";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 * 1000 + 5;
int a[MAXN], b[MAXN];
int main() {
ios_base::sync_with_stdio(false);
string s;
cin >> s;
int st = 0, k = 0, prev = -1;
for (size_t i = 0; i < s.length(); i++) {
if (st == 0) {
if ('a' <= s[i] && s[i] <= 'z')
a[k]++;
else if (('0' <= s[i] && s[i] <= '9') || s[i] == '_')
continue;
else if (s[i] == '@')
st = 1;
else
a[k] = 0;
} else if (st == 1) {
if (('a' <= s[i] && s[i] <= 'z') || ('0' <= s[i] && s[i] <= '9')) {
st = 2;
prev = i - 1;
} else {
st = 0;
a[k] = 0;
}
} else if (st == 2) {
if (s[i] == '.')
st = 3;
else if (!('a' <= s[i] && s[i] <= 'z') && !('0' <= s[i] && s[i] <= '9')) {
st = 0;
a[k] = 0;
i = prev;
}
} else if (st == 3) {
b[k] = 0;
for (size_t j = i; j < s.length(); j++)
if (!('a' <= s[j] && s[j] <= 'z'))
break;
else
b[k]++;
if (!b[k])
a[k] = 0;
else
k++;
st = 0;
i--;
}
}
long long ans = 0;
for (int i = 0; i < k; i++) ans += (long long)a[i] * b[i];
cout << ans << '\n';
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[105];
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ans = 0;
for (int i = 0; i < k; i++) {
int cnt1 = 0, cnt2 = 0;
for (int j = 0; j < n / k; j++) {
if (a[i + j * k] == 1)
cnt1++;
else
cnt2++;
}
ans += min(cnt1, cnt2);
}
printf("%d\n", ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long double dis(double x, double y, double x1, double y1) {
return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
}
double PI = 3.14159265;
double e = 2.718281828;
void EnGz() {
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
}
long long GCD_(long long a, long long b) { return b ? GCD_(b, a % b) : a; }
long long LCM_(long long a, long long b) { return (a * b) / GCD_(a, b); }
bool prime(long long x) {
if (x == 2) return true;
if (x < 2 || x % 2 == 0) return false;
for (long long i = 3; i * i <= x; i += 2)
if (x % i == 0) return false;
return true;
}
void print(vector<int> v) {
for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}
long long int primeFactors(long long int n) {
if (n % 2 == 0) return 2;
for (long long int i = 3; i <= sqrt(n); i += 2)
if (n % i == 0) return i;
return n;
}
int n, m;
vector<vector<pair<int, long long>>> v;
vector<int> Parent;
vector<long long> dijkstra() {
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
greater<pair<long long, int>>>
q;
vector<long long> Cost(n + 1);
for (int i = 0; i <= n; i++) Cost[i] = 1e18;
vector<bool> visited(n + 1);
for (int i = 0; i <= n; i++) visited[i] = false;
Cost[1] = 0;
q.push(make_pair(0, 1));
Parent[1] = -1;
while (!q.empty()) {
int current = q.top().second;
q.pop();
if (visited[current]) continue;
visited[current] = true;
for (int i = 0; i < v[current].size(); i++) {
int child_node = v[current][i].first;
if (!visited[child_node] &&
Cost[child_node] > Cost[current] + v[current][i].second) {
Cost[child_node] = Cost[current] + v[current][i].second;
q.push(make_pair(Cost[child_node], child_node));
Parent[child_node] = current;
}
}
}
return Cost;
}
int main() {
EnGz();
cin >> n >> m;
v.resize(n + 1);
Parent.resize(n + 1);
for (int i = 0; i < m; i++) {
int to, from;
long long cost;
cin >> to >> from >> cost;
v[to].push_back(make_pair(from, cost));
v[from].push_back(make_pair(to, cost));
}
vector<long long> Cost = dijkstra();
if (Cost[n] == 1e18) return cout << -1, 0;
stack<int> Path;
Path.push(n);
int cur = n;
while (Parent[cur] != -1) {
Path.push(Parent[cur]);
cur = Path.top();
}
while (!Path.empty()) {
cout << Path.top() << " ";
Path.pop();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = long unsigned long;
using ld = double long;
template <class T = int, class F = plus<T>>
struct segtree {
int maxn;
vector<T> a;
T e;
F f;
segtree(int n, T e = T(), F f = F(), T v = T()) : e(e), f(f) {
maxn = 1;
while (maxn < n) maxn <<= 1;
a.assign(2 * maxn, v);
for (int i = maxn - 1; i; i--) a[i] = f(a[2 * i], a[2 * i + 1]);
}
void add(int p, const T& v) {
p += maxn;
a[p] = f(a[p], v);
for (p >>= 1; p; p >>= 1) a[p] = f(a[2 * p], a[2 * p + 1]);
}
void set(int p, const T& v) {
p += maxn;
a[p] = v;
for (p >>= 1; p; p >>= 1) a[p] = f(a[2 * p], a[2 * p + 1]);
}
T get(int l, int r, int x, int xl, int xr) const {
if (r <= xl || xr <= l) return e;
if (l <= xl && xr <= r) return a[x];
int xm = (xl + xr) >> 1;
return f(get(l, r, 2 * x, xl, xm), get(l, r, 2 * x + 1, xm, xr));
}
T operator()(int l, int r) const { return get(l, r, 1, 0, maxn); }
};
template <class T = int>
struct minval {
T x;
minval(T x = numeric_limits<T>::max()) : x(x) {}
T operator()() const { return x; }
minval operator+(const minval& b) const { return min(x, b.x); }
};
template <class T = int>
struct maxval {
T x;
maxval(T x = numeric_limits<T>::min()) : x(x) {}
T operator()() const { return x; }
maxval operator+(const maxval& b) const { return max(x, b.x); }
};
int main() {
ios::sync_with_stdio(!cin.tie(0));
int n, q;
cin >> n >> q;
segtree<maxval<ll>> hit(n + 1);
segtree<minval<ll>> lot(n + 1);
vector<int> a(n);
vector<ll> z(n + 1);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
hit.set(0, 0);
lot.set(0, 0);
for (int i = 0; i < n; i++) {
int b;
cin >> b;
z[i + 1] = z[i] + b - a[i];
hit.set(i + 1, z[i + 1]);
lot.set(i + 1, z[i + 1]);
}
while (q--) {
int l, r;
cin >> l >> r;
l--;
if (z[l] != z[r]) {
cout << "-1\n";
} else if (lot(l, r)() < z[l]) {
cout << "-1\n";
} else {
cout << hit(l, r)() - z[l] << '\n';
}
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
char a[100100], b[100100];
int cnt[15];
int main() {
scanf("%s%s", &a, &b);
int m = strlen(b);
int n = strlen(a);
for (int i = (0); i < (m); ++i) cnt[b[i] - '0']++;
int cur = 9;
for (int i = (0); i < (n); ++i) {
while ((cur >= 0) && (cnt[cur] == 0)) cur--;
if (cur < 0) break;
if (cur > a[i] - '0') {
cnt[cur]--;
a[i] = cur + '0';
}
}
printf("%s\n", a);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6, nBits = 2e8 + 5, MM = (1 << 16), MAX = 1111,
OO = 0x3f3f3f3f, MOD = 1e9 + 7, inf = 1 << 30;
const long long INF = (long long)1e18;
long long GCD(long long a, long long b) { return !b ? a : GCD(b, a % b); }
long long LCM(long long x, long long y) { return (x * y / GCD(x, y)); }
long long fact(long long z) { return (z <= 1) ? 1 : z * fact(z - 1); }
int w, h, minii;
long long ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> w >> h;
for (int i = 2; i <= h; i += 2)
for (int j = 2; j <= w; j += 2) {
ans += (h - i + 1) * (w - j + 1);
}
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, m, p, total, belong[222], sum[222], v[222], len[222], to[222][22],
f[2][222][222][555];
vector<int> record, l, r, st[222], data[1000050];
int Rand() { return (rand() << 15) | rand(); }
int Solve(vector<int> ask) {
int len = ask.size(), answer, i, j, k, digit, next, news;
memset(f, 0, sizeof(f));
for (i = 0; i < m; i++)
if (sum[belong[i]] <= p) {
f[0][len - 1][belong[i]][sum[belong[i]]]++;
if (i <= ask[len - 1]) {
f[1][len - 1][belong[i]][sum[belong[i]]]++;
}
}
for (i = len - 1; i > 0; i--) {
for (j = 1; j <= total; j++) {
for (k = 0; k <= p; k++) {
if (f[0][i][j][k] > 0) {
for (digit = 0; digit < m; digit++) {
next = to[j][digit];
news = k + sum[next];
if (news > p) {
continue;
}
f[0][i - 1][next][news] += f[0][i][j][k];
f[0][i - 1][next][news] %= 1000000007;
if (digit < ask[i - 1]) {
f[1][i - 1][next][news] += f[0][i][j][k];
f[1][i - 1][next][news] %= 1000000007;
}
}
}
if (f[1][i][j][k] > 0) {
next = to[j][ask[i - 1]];
news = k + sum[next];
if (news <= p) {
f[1][i - 1][next][news] += f[1][i][j][k];
f[1][i - 1][next][news] %= 1000000007;
}
}
}
}
}
answer = 0;
for (i = len - 1; i > 0; i--) {
for (j = 1; j <= total; j++) {
for (k = 0; k <= p; k++)
if (data[j][0] != 0) {
answer += f[0][i][j][k];
answer %= 1000000007;
}
}
}
for (i = 1; i <= total; i++) {
for (j = 0; j <= p; j++)
if (data[i][0] != 0) {
answer += f[1][0][i][j];
answer %= 1000000007;
}
}
return answer;
}
int main() {
int i, j, k, digit, temp, ls, rs, answer;
bool solved;
srand((unsigned)time(0));
scanf("%d%d%d", &n, &m, &p);
scanf("%d", &ls);
for (i = 1; i <= ls; i++) {
scanf("%d", &temp);
l.push_back(temp);
}
scanf("%d", &rs);
for (i = 1; i <= rs; i++) {
scanf("%d", &temp);
r.push_back(temp);
}
for (i = 1; i <= n; i++) {
scanf("%d", &len[i]);
for (j = 1; j <= len[i]; j++) {
scanf("%d", &temp);
st[i].push_back(temp);
}
scanf("%d", &v[i]);
}
total = 0;
for (i = 0; i < m; i++) {
total++;
data[total].push_back(i);
}
for (i = 1; i <= n; i++) {
for (j = 0; j < len[i]; j++) {
total++;
for (k = j; k < len[i]; k++) {
data[total].push_back(st[i][k]);
}
}
}
sort(data + 1, data + total + 1);
for (i = j = 1; i < total; i++)
if (data[i + 1] != data[j]) {
data[++j] = data[i + 1];
}
total = j;
for (i = 1; i <= total; i++) {
temp = data[i].size();
for (j = 1; j <= n; j++)
if (temp >= len[j]) {
record.clear();
for (k = 0; k < len[j]; k++) {
record.push_back(data[i][k]);
}
if (record == st[j]) {
sum[i] += v[j];
}
}
}
for (i = 1; i <= total; i++) {
temp = data[i].size();
for (digit = 0; digit < m; digit++) {
record.clear();
record.push_back(digit);
for (j = 0; j < temp; j++) {
record.push_back(data[i][j]);
}
while (true) {
solved = false;
for (j = 1; j <= total; j++)
if (record == data[j]) {
to[i][digit] = j;
solved = true;
break;
}
if (solved) {
break;
}
record.pop_back();
}
}
}
for (i = 0; i < m; i++) {
record.clear();
record.push_back(i);
for (j = 1; j <= total; j++)
if (data[j] == record) {
belong[i] = j;
break;
}
}
temp = l.size();
l[temp - 1]--;
for (i = temp - 1; i > 0; i--)
if (l[i] < 0) {
l[i] += m;
l[i - 1]--;
}
record.clear();
for (i = 0; i < temp; i++)
if (l[i] > 0) {
break;
}
for (; i < temp; i++) {
record.push_back(l[i]);
}
answer = 0;
if (record.size() > 0) {
answer -= Solve(record);
}
answer += Solve(r);
printf("%d\n", (answer % 1000000007 + 1000000007) % 1000000007);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
string s;
int i, dep = 0, h, np = 1, last, n = 0, af = 0;
cin >> s;
for (i = 0; i < s.length(); i++) {
switch (s[i]) {
case '(': {
dep++;
break;
}
case ')': {
dep--;
break;
}
case '#': {
n++;
dep--;
h = dep;
break;
}
}
if (dep < 0) {
np = 0;
break;
}
}
i = s.length() - 1;
while (s[i] != '#') {
switch (s[i]) {
case '(': {
af++;
break;
}
case ')': {
af--;
break;
}
}
if (af > 0) {
np = 0;
break;
}
i--;
}
if (np != 0) {
{ last = h + af; }
}
if (np == 0)
cout << -1;
else {
for (i = 1; i <= n - 1; i++) cout << 1 << endl;
cout << last + 1;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxi = 54321;
int papa[maxi];
int from[maxi], to[maxi];
int range[maxi];
bool visited[maxi];
void init(int n) {
for (int i = 0; i <= n; i++) {
papa[i] = i;
}
}
int get(int x) { return (x == papa[x] ? x : papa[x] = get(papa[x])); }
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> from[i] >> to[i];
}
init(n);
int k = 0;
for (int i = 1; i <= m; i++) {
int x = get(from[i]);
int y = get(to[i]);
if (x != y) {
range[k++] = i;
visited[i] = true;
papa[x] = y;
}
}
init(n);
for (int i = m; i >= 1; i--) {
int x = get(from[i]);
int y = get(to[i]);
if (x != y) {
if (!visited[i]) {
visited[i] = true;
range[k++] = i;
}
papa[x] = y;
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
init(n);
for (int i = 0; i < k; i++) {
if (range[i] < l || range[i] > r) {
int x = get(from[range[i]]);
int y = get(to[range[i]]);
if (x != y) {
papa[x] = y;
}
}
}
int ans(0);
for (int i = 1; i <= n; i++) {
if (papa[i] == i) ans++;
}
cout << ans << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void pairsort(long long int a[], long long int b[], long long int n) {
pair<long long int, long long int> pairt[n];
for (long long int i = 0; i < n; i++) {
pairt[i].first = a[i];
pairt[i].second = b[i];
}
sort(pairt, pairt + n);
for (long long int i = 0; i < n; i++) {
a[i] = pairt[i].first;
b[i] = pairt[i].second;
}
}
void display(const set<pair<long long int, long long int> >& s) {
for (auto const& x : s) {
cout << x.first << " " << x.second << endl;
}
}
int main() {
long long int a[3], b[3], i;
for (i = 0; i < 3; i++) cin >> a[i] >> b[i];
pairsort(a, b, 3);
set<pair<long long int, long long int> > s;
for (i = a[0]; i <= a[1]; i++) {
pair<long long int, long long int> x = make_pair(i, b[0]);
s.insert(x);
}
for (i = max(b[0], b[1]); i >= min(b[0], b[1]); i--) {
pair<long long int, long long int> x = make_pair(a[1], i);
s.insert(x);
}
if (b[2] < b[1]) {
for (i = b[1]; i >= b[2]; i--) {
pair<long long int, long long int> x = make_pair(a[1], i);
s.insert(x);
}
} else {
for (i = b[2]; i >= b[1]; i--) {
pair<long long int, long long int> x = make_pair(a[1], i);
s.insert(x);
}
}
for (i = a[1]; i <= a[2]; i++) {
pair<long long int, long long int> x = make_pair(i, b[2]);
s.insert(x);
}
cout << s.size() << endl;
display(s);
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<long long, long long> &a,
const pair<long long, long long> &b) {
return (a.second < b.second);
}
long long nod(long long n) {
string s = to_string(n);
return (long long)s.length();
}
long long modexp(long long x, long long y) {
x %= 1000000007;
long long res = 1;
while (y) {
if (y & 1) res *= x, res %= 1000000007;
x *= x;
y >>= 1;
x %= 1000000007;
}
return res;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
long long cnt = 0;
long long ans = 0;
for (long long i = 0; i < s.length(); i++) {
if (s[i] == 'a')
cnt++;
else {
ans += modexp(2, cnt);
ans = (ans - 1 + (long long)100 * 1000000007) % 1000000007;
ans %= 1000000007;
}
}
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
int first = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') first = -first;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= first;
}
int n;
const int MAXN = 888;
int ans[MAXN];
bool odd[MAXN], used[MAXN];
pair<int, int> pos[5];
void get_1_n() {
for (int i = 1; i <= n; i++) {
cout << "? " << n - 1;
for (int j = 1; j <= n; j++) {
if (i != j) cout << " " << j;
}
cout << endl;
int res;
cin >> res;
if (res) {
if (pos[1].first)
pos[1].second = i;
else
pos[1].first = i;
}
}
return;
}
void get_odd() {
for (int i = 1; i <= n; i++) {
if (i != pos[1].first) {
cout << "? " << 2 << " " << i << " " << pos[1].first << endl;
cin >> odd[i];
}
}
}
void get_234() {
for (int k = 1; k <= 3 && k <= n / 2; k++) {
for (int i = 1; i <= n; i++) {
if (used[i]) continue;
cout << "? " << n - k * 2 - 1;
for (int j = 1; j <= n; j++) {
if (i != j && used[j] == 0) cout << " " << j;
}
cout << endl;
int res;
cin >> res;
if (res) {
if (odd[i] ^ (k % 2 == 0))
pos[k + 1].second = i;
else
pos[k + 1].first = i;
}
}
used[pos[k + 1].first] = used[pos[k + 1].second] = true;
}
}
int get3(int x) {
int res;
cout << "? " << 3 << " " << x << " ";
cout << pos[1].first << " " << pos[2].first << endl;
cin >> res;
if (res) return 0;
cout << "? 3 " << x << " ";
cout << pos[2].first << " " << pos[3].first << endl;
cin >> res;
if (res)
return 1;
else
return 2;
}
int get5(int x) {
int res;
bool mark[5];
memset(mark, false, sizeof(mark));
for (int i = 1; i <= 4; i++) {
cout << "? 5 " << x << ' ' << pos[1].second << ' ' << pos[2].second << ' '
<< pos[3].second << ' ';
cout << pos[i].first << endl;
int value = (5 - (3 * n - 3 + i) % 5) % 5;
cin >> res;
if (res) return value;
mark[value] = true;
}
for (int i = 0; i <= 4; i++)
if (!mark[i]) return i;
}
int get7(int x) {
int res;
bool mark[7];
int cnt = 0;
memset(mark, false, sizeof(mark));
for (int i = 1; i <= 4 && cnt <= 5; i++)
for (int j = 1; j <= 4 && cnt <= 5; j++) {
int value = (7 - (n * 4 + 4 - i - (n - j + 1)) % 7) % 7;
if (mark[value]) continue;
cout << '?' << ' ' << 7 << ' ' << x << ' ';
for (int k = 1; k <= 4; k++)
if (k != i) cout << pos[k].first << ' ';
for (int k = 1; k <= 4; k++)
if (k != j) cout << pos[k].second << ' ';
cout << endl;
read(res);
if (res) return value;
mark[value] = true, cnt++;
}
for (int i = 0; i <= 6; i++)
if (!mark[i]) return i;
}
int get8(int x) {
int res;
int ans = odd[x];
for (int i = 1; i <= 4; i++) {
int value = (4 - (10 - i) % 4) % 4;
if (value == ans) {
cout << '?' << ' ' << 4 << ' ' << x << ' ';
for (int k = 1; k <= 4; k++)
if (k != i) cout << pos[k].first << ' ';
cout << endl;
read(res);
if (res)
ans = ans;
else
ans = ans + 2;
break;
}
}
for (int i = 1; i <= 4; i++) {
int value = (8 - (n * 4 + 4 - i) % 8) % 8;
if (value == ans || value == ans + 4) {
cout << '?' << ' ' << 8 << ' ' << x << ' ';
for (int k = 1; k <= 4; k++) {
if (k != i) cout << pos[k].first << ' ';
cout << pos[k].second << ' ';
}
cout << endl;
read(res);
if (res ^ (value == ans))
ans = ans + 4;
else
ans = ans;
break;
}
}
return ans;
}
void Output() {
if (ans[1] > n / 2) {
cout << "!";
for (int i = 1; i <= n; i++) cout << " " << n - ans[i] + 1;
cout << endl;
} else {
cout << "! ";
for (int i = 1; i <= n; i++) cout << " " << ans[i];
cout << endl;
}
return;
}
int main() {
cin >> n;
get_1_n();
used[pos[1].first] = used[pos[1].second] = true;
odd[pos[1].first] = true;
get_odd();
get_234();
for (int i = 1; i <= 4 && i <= n / 2; i++) {
ans[pos[i].first] = i;
ans[pos[i].second] = n + 1 - i;
}
if (n <= 8) {
Output();
return 0;
}
for (int i = 1; i <= n; i++) {
if (ans[i]) continue;
int t = get3(i);
int first = get5(i);
int second = get7(i);
int e = get8(i);
for (int j = 1; j <= n; j++)
if (j % 3 == t && j % 5 == first && j % 7 == second && j % 8 == e) {
ans[i] = j;
break;
}
}
Output();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int q, l, r, res;
cin >> q;
vector<int> ans;
for (int i = 0; i < q; i++) {
cin >> l >> r;
if (l % 2 == 1) {
res = (r - l + 1) / 2;
if (r % 2 == 1) {
res = res - r;
}
} else {
res = -(r - l + 1) / 2;
if (r % 2 == 0) {
res += r;
}
}
ans.push_back(res);
}
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, counta = 0, countd = 0;
cin >> t;
char s[1000000];
for (int i = 0; i < t; i++) {
cin >> s[i];
if (s[i] == 'A')
counta++;
else
countd++;
}
if (counta > countd)
cout << "Anton";
else if (counta < countd)
cout << "Danik";
else
cout << "Friendship";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> cnt(32, 0);
for (int i = 0; i < n; i++) {
int bit = __builtin_clz(a[i]);
cnt[bit] += 1;
}
long long ans = 0;
for (int bit = 0; bit < 32; bit++) {
ans += (long long)cnt[bit] * (cnt[bit] - 1) / 2;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
using INT = long long;
using pii = pair<int, int>;
vector<int> b[200100];
int a[200100];
void update(int u, int x) {
for (; u < 200100; u += u & -u) b[u].push_back(x);
}
int calc(int u, int x, int ans = 0) {
for (; u; u -= u & -u) {
int id = lower_bound(b[u].begin(), b[u].end(), x) - b[u].begin();
ans += (b[u].size() - id);
}
return ans;
}
int CALC(int l, int r, int x) { return calc(r, x) - calc(l - 1, x); }
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
update(i, a[i]);
}
for (int i = 1; i < 200100; i++) sort(b[i].begin(), b[i].end());
INT ans = 0;
for (int i = 1; i < n; i++) {
int l = i + 1, r = min(n, a[i]);
if (r < l) continue;
ans += CALC(l, r, i);
}
cout << ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& num) {
bool start = false, neg = false;
char c;
num = 0;
while ((c = getchar()) != EOF) {
if (c == '-')
start = neg = true;
else if (c >= '0' && c <= '9') {
start = true;
num = num * 10 + c - '0';
} else if (start)
break;
}
if (neg) num = -num;
}
char str[50];
int check(int s, int t) {
printf("? %d %d\n", s, t);
fflush(stdout);
scanf("%s", str);
if (str[0] == '<') return 0;
if (str[0] == '>') return 1;
return -1;
}
void solve() {
int n;
scanf("%d", &n);
int mi = 1, mx = 1;
if (n % 2 == 0) {
mx = n;
int c = check(mi, mx);
if (c == 1) swap(mi, mx);
}
for (int i = (2); i <= (n); i++)
if (i % 2 == 0 && i + 1 <= n) {
int s = i, t = i + 1;
int c = check(i, i + 1);
if (c == 1) swap(s, t);
c = check(s, mi);
if (c == 0) mi = s;
c = check(t, mx);
if (c == 1) mx = t;
}
printf("! %d %d\n", mi, mx);
fflush(stdout);
}
int main() {
int T;
scanf("%d", &T);
while (T--) solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int N;
const int SIZE = 100005;
int A[SIZE];
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int gd;
bool sat(int n) {
while (!(n % 2)) n /= 2;
while (!(n % 3)) n /= 3;
if (n == 1) return true;
return false;
}
bool solve() {
for (int i = 1; i <= N; i++) {
if (!sat(A[i] / gd)) return false;
}
return true;
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%d", &A[i]);
}
gd = A[1];
for (int i = 2; i <= N; i++) {
gd = gcd(gd, A[i]);
}
if (solve())
puts("Yes");
else
puts("No");
return 0;
}
| 3 |
//#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
using namespace std;
#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
//#define int long long
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
typedef long long ll;
//typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
#define VMAX 10010
struct edge
{
int to, cap, cost, rev;
edge(int to, int cap, int cost, int rev) :to(to), cap(cap), cost(cost), rev(rev) {}
edge() :to(-1), cap(-1), cost(-1), rev(-1) {}
};
vector<edge> G[VMAX];
int h[VMAX], dist[VMAX], prevv[VMAX], preve[VMAX];
int V, E, F;
void add_edge(int from, int to, int cap, int cost)
{
G[from].push_back(edge(to, cap, cost, SZ(G[to])));
G[to].push_back(edge(from, 0, -cost, SZ(G[from]) - 1));
}
int min_cost_flow(int s, int t, int f)
{
int res = 0;
fill(h, h + V, 0);
while (f > 0)
{
priority_queue<pii, vector<pii>, greater<pii>> q;
fill(dist, dist + V, INF);
dist[s] = 0;
q.push(pii(0, s));
while (!q.empty())
{
pii p = q.top(); q.pop();
int v = p.second;
if (dist[v] < p.first)continue;
rep(i, 0, SZ(G[v]))
{
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;
q.push(pii(dist[e.to], e.to));
}
}
}
if (dist[t] == INF)
{
return -1;
}
rep(v, 0, V)h[v] += dist[v];
int 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 res;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> V >> E >> F;
int s, t, c, d;
rep(i, 0, E)
{
cin >> s >> t >> c >> d;
add_edge(s, t, c, d);
}
cout << min_cost_flow(0, V - 1, F) << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
const long long int MOD = 1e9 + 7;
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};
using namespace std;
void vok() {
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
}
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 lcm(long long int a, long long int b) {
return ((a * b) / gcd(a, b));
}
vector<long long int> pp;
void gen() {
vector<bool> prime(33000, true);
prime[0] = prime[1] = false;
for (int p = 0; p * p <= 33000; p++) {
if (prime[p]) {
for (int i = p * p; i <= 33000; i += p) prime[i] = false;
}
}
for (int i = 0; i < 33000; i++)
if (prime[i]) pp.push_back(i);
}
long long int find(vector<int>& A, int n) {}
void solve() {
long long int n, m, x, y, a, b, k;
a = b = x = y = k = 0;
long long int ans = 0, res = 0;
cin >> n;
cin >> m >> k;
char A[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> A[i][j];
}
}
if (n == 1 and m == 1 and A[0][0] == '.' and k == 1) {
cout << 1 << endl;
return;
}
long long int mx = -1;
for (int i = 0; i < n; i++) {
mx = -1;
for (int j = 0; j < m; j++) {
if (A[i][j] == '.')
x++;
else {
if (x >= k) ans += x - k + 1;
x = 0;
}
}
if (x == 0) continue;
if (x >= k) ans += x - k + 1;
x = 0;
}
if (k == 1) {
cout << ans << endl;
return;
}
for (int i = 0; i < m; i++) {
mx = -1;
for (int j = 0; j < n; j++) {
if (A[j][i] == '.')
x++;
else {
if (x >= k) ans += x - k + 1;
x = 0;
}
}
if (x == 0) continue;
if (x >= k) ans += x - k + 1;
x = 0;
}
cout << ans << endl;
}
int main() {
vok();
int t = 1;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, r1, r2;
vector<int> rel[112345];
int path[112345];
bool vis[112345];
void dfs(int i) {
vis[i] = true;
for (int j = 0; j < rel[i].size(); j++) {
if (!vis[rel[i][j]]) {
path[rel[i][j]] = i;
dfs(rel[i][j]);
}
}
}
int main() {
cin >> n >> r1 >> r2;
for (int i = 1; i <= n; i++) {
if (i != r1) {
int j;
cin >> j;
rel[i].push_back(j);
rel[j].push_back(i);
}
}
dfs(r2);
for (int i = 1; i <= n; i++) {
if (i != r2) {
cout << path[i] << " ";
}
}
cout << endl;
return 0;
}
| 4 |
#pragma GCC optimize("O2")
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007LL
#define eps 1e-8
using namespace std;
ll a[100005];
int main()
{
cin.tie(0),ios::sync_with_stdio(0);
ll n,m,v,p; cin>>n>>m>>v>>p;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
int l=0,r=n-p,k;
while(l<r){
k=(l+r)>>1;
ll tot=(v-1)*m,cur=0;
priority_queue<int> pq;
for(int i=0;i<n;i++){
if(i==k) continue;
if(a[k]+m<a[i]) tot-=m,cur++;
else{
ll tmp=min(m,a[k]+m-a[i]);
tot-=tmp; pq.push(m-tmp);
}
}
while(tot>0){
cur++; tot-=pq.top(); pq.pop();
}
if(cur>=p) l=k+1;
else r=k;
}
cout<<n-l<<"\n";
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
string a, b, c, d, s;
int main() {
cin >> a >> b;
int m1 = 0, m2 = 0;
for (int i = 0; i < (int)a.size(); i++) m1 += (a[i] == '1');
for (int i = 0; i < (int)b.size(); i++) m2 += (b[i] == '1');
if (m1 & 1) m1++;
if (m1 >= m2)
puts("YES");
else
puts("NO");
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
const double eps = 1e-6;
long long n, l, r, c;
vector<pair<long long, long long> > V[maxn];
map<long long, int> H, H2;
int tot = 0, tot2;
long long gcd(long long x, long long y) {
return x % y == 0 ? y : gcd(y, x % y);
}
int main() {
scanf("%I64d", &n);
for (int i = 1; i <= n; i++) {
scanf("%I64d %I64d %I64d", &l, &r, &c);
H[l] = H[l] ? H[l] : ++tot;
V[H[l]].push_back(make_pair(r, c));
}
for (int i = 2; i <= tot; i++)
if (V[i].size() != V[i - 1].size()) return 0 * printf("0\n");
for (int i = 1; i <= tot; i++) sort(V[i].begin(), V[i].end());
for (int i = 2; i <= tot; i++) {
for (int j = 0; j < V[i].size(); j++)
if (V[i][j].first != V[1][j].first) return 0 * printf("0\n");
for (int j = 1; j < V[i].size(); j++) {
if ((double)V[i][j].second * V[1][j - 1].second -
(double)V[i][j - 1].second * V[1][j].second >
eps)
return 0 * printf("0\n");
}
}
long long ans = V[1][0].second;
for (int i = 1; i <= tot; i++)
for (int j = 0; j < V[i].size(); j++) ans = gcd(ans, V[i][j].second);
int cnt = 0;
for (long long i = 1; i <= ans / i; i++) {
if (ans % i == 0) {
cnt++;
if (ans / i != i) cnt++;
}
}
printf("%d\n", cnt);
}
| 3 |
#include <cstdio>
int main()
{
// freopen("AGC015-D.in", "r", stdin);
long long l, r;
scanf("%lld%lld", &l, &r);
if (l == r)
{
puts("1");
return 0;
}
auto k = l ^ r;
while (k != (k & -k))
k ^= k & -k;
l &= k * 2 - 1;
r &= k - 1;
while (r != (r & -r))
r ^= r & -r;
if (r)
r = r * 2 - 1;
printf("%lld\n", r < l ? 2 * (k - l) + r + 1 : 2 * k - l);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<string> ss;
string s;
cin >> s;
string t;
cin >> t;
int x1 = s[0] - 'a' + 1, y1 = s[1] - '0';
int x2 = t[0] - 'a' + 1, y2 = t[1] - '0';
if (x1 != x2 && y1 != y2) {
string a = "RU", b = "LU", c = "RD", d = "LD";
string ans;
if (x1 < x2 && y1 < y2)
ans = a;
else if (x1 < x2 && y1 > y2)
ans = c;
else if (x1 > x2 && y1 < y2)
ans = b;
else
ans = d;
while (1) {
if (x1 == x2 || y1 == y2) break;
if (x1 < x2)
x1++;
else
x1--;
if (y1 < y2)
y1++;
else
y1--;
ss.push_back(ans);
}
}
if (x1 != x2 || y1 != y2) {
string a = "R", b = "L", c = "D", d = "U";
string ans;
if (x1 < x2)
ans = a;
else if (x1 > x2)
ans = b;
else if (y1 < y2)
ans = d;
else
ans = c;
for (int i = 1; i <= abs(x1 - x2 + y1 - y2); i++) ss.push_back(ans);
}
cout << ss.size() << "\n";
for (int i = 0; i < ss.size(); i++) cout << ss[i] << "\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> adj[100005], d[100005];
vector<long long int> sum[100005];
long long int hasComp[100005], dist[100005], sz[100005];
long long int n, m, q, u, v;
pair<int, int> x;
long long int getfar(long long int i, long long int mark) {
queue<pair<int, int> > q;
q.push({i, 0});
while (!q.empty()) {
x = q.front();
q.pop();
if (mark == 0) hasComp[x.first] = i;
for (long long int j : adj[x.first])
if (j != x.second) q.push({j, x.first});
}
return x.first;
}
void set_distances(long long int u, long long int p, long long int d) {
dist[u] = max(dist[u], d);
for (long long int i : adj[u]) {
if (i != p) set_distances(i, u, d + 1);
}
}
void dfs_collect(long long int u, long long int p, long long int cmp) {
sz[cmp]++;
d[cmp].push_back(dist[u]);
for (long long int v : adj[u]) {
if (v != p) dfs_collect(v, u, cmp);
}
}
map<pair<int, int>, double> ans;
double query(long long int x, long long int y) {
if (ans.find({x, y}) != ans.end()) return ans[{x, y}];
double Ans = 0;
if (sz[x] > sz[y]) swap(x, y);
long long int diam = max(d[x].back(), d[y].back());
for (long long int i = 0; i < d[x].size(); i++) {
long long int left = d[x][i];
long long int j =
lower_bound(d[y].begin(), d[y].end(), diam - left) - d[y].begin();
Ans += j * diam + (d[y].size() * 1LL - j) * (left + 1) + sum[y][j];
}
Ans = Ans / (sz[x] * sz[y] * 1.0);
return ans[{x, y}] = ans[{y, x}] = Ans;
}
int main() {
scanf("%lld %lld %lld", &n, &m, &q);
for (long long int i = 0; i < m; i++) {
scanf("%lld %lld", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for (long long int i = 1; i <= n; i++) {
if (hasComp[i] == 0) {
long long int x = getfar(i, 0);
long long int y = getfar(x, 1);
set_distances(x, 0, 0);
set_distances(y, 0, 0);
dfs_collect(i, 0, i);
sort(d[i].begin(), d[i].end());
sum[i].resize(d[i].size() + 1);
sum[i][d[i].size()] = 0;
for (long long int j = d[i].size() - 1; j >= 0; j--) {
sum[i][j] = d[i][j] + sum[i][j + 1];
}
}
}
while (q--) {
cin >> u >> v;
u = hasComp[u];
v = hasComp[v];
if (u == v) {
printf("-1\n");
continue;
}
double anss = query(u, v);
printf("%.8f\n", anss);
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int ma = 0;
int ct = 0;
for (int i = 0; i < n; i++) {
ma = max(ma, a[i]);
if (ma == i + 1) {
ct++;
}
}
cout << ct << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 123;
const int mod = 1000000000 + 7;
using LL = long long;
char digits[maxn];
int n, k;
int fact[maxn];
int rfact[maxn];
int bigmod(int a, int b) {
if (b == 0) return 1;
int ta(bigmod(a, b >> 1));
ta = (LL)ta * ta % mod;
if (b & 1) return (LL)ta * a % mod;
return ta;
}
int calC(int n, int m) {
if (m > n || n < 0 || m < 0) return 0;
return (LL)fact[n] * rfact[m] % mod * rfact[n - m] % mod;
}
int main() {
scanf("%d%d%s", &n, &k, digits);
fact[0] = rfact[0] = 1;
for (int i = 1; i <= n - 1; ++i) {
fact[i] = (LL)fact[i - 1] * i % mod;
rfact[i] = bigmod(fact[i], mod - 2);
}
int res(0), nb(1), nv(0);
for (int i = n - 1; i >= 0; --i) {
int nd(digits[i] - '0');
res = (res + (LL)nv * nd % mod) % mod;
res = (res + (LL)nb * calC(i, k) % mod * nd % mod) % mod;
nv = (nv + (LL)nb * calC(i - 1, k - 1) % mod) % mod;
nb = (LL)nb * 10 % mod;
}
printf("%d\n", res % mod);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int ans(int a, int b) {
int answer;
int c = abs(a - b);
if (c % 10 == 0) {
answer = c / 10;
} else {
answer = c / 10 + 1;
}
return answer;
}
int main() {
int t;
cin >> t;
int a[t], b[t];
for (int i = 0; i < t; i++) {
cin >> a[i] >> b[i];
}
for (int i = 0; i < t; i++) {
cout << ans(a[i], b[i]) << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long double eps = 1e-7;
const int N = 1e5 + 10;
long long dp[N][36], mn[N][36], sum[N][36];
long long n, k;
pair<long long, long long> mnAndSum(int node) {
long long sum1 = 0, mn1 = 2e18, k1 = k;
for (long long i = 34; i >= 0; i--)
if ((1LL << i) <= k1)
k1 -= (1LL << i), mn1 = min(mn1, mn[node][i]), sum1 += sum[node][i],
node = dp[node][i];
return {sum1, mn1};
}
int main() {
ios_base::sync_with_stdio(false);
cin >> n >> k;
memset(dp, -1, sizeof dp);
for (long long i = 0; i < n; i++) {
long long next;
cin >> next;
dp[i][0] = next;
}
for (long long i = 0; i < n; i++) {
long long cost;
cin >> cost;
mn[i][0] = cost;
sum[i][0] = cost;
}
for (long long i = 1; i <= 34; i++) {
for (long long j = 0; j < n; j++) {
if (dp[j][i - 1] != -1) {
dp[j][i] = dp[dp[j][i - 1]][i - 1];
mn[j][i] = min(mn[j][i - 1], mn[dp[j][i - 1]][i - 1]);
sum[j][i] = sum[j][i - 1] + sum[dp[j][i - 1]][i - 1];
}
}
}
for (long long i = 0; i < n; i++) {
pair<long long, long long> ans = mnAndSum(i);
cout << ans.first << " " << ans.second << '\n';
}
}
| 5 |
#include <bits/stdc++.h>
int main() {
int n, i, d = 0, a = 0;
scanf("%d", &n);
char x;
for (i = 0; i <= n; i++) {
scanf("%c", &x);
if (x == 'D')
d++;
else if (x == 'A')
a++;
}
if (d > a)
printf("Danik");
else if (a > d)
printf("Anton");
else
printf("Friendship");
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct SAM_node {
int minlen, maxlen, slink;
int trans[4];
inline void reset(int _minlen, int _maxlen, int _slink, int *_trans) {
minlen = _minlen, maxlen = _maxlen, slink = _slink;
for (int i = 0; i < 4; i++) trans[i] = _trans[i];
return;
}
};
struct matrix {
long long g[4][4];
};
SAM_node SAM[300000];
matrix basic, result;
int g[300000], h[300000], p[300000];
char c[120000];
int i, j, k, n, head, tail, sum_node;
long long m, t, l, r, mid;
char cc, cd;
inline matrix operator*(matrix x, matrix y) {
matrix z;
memset(z.g, 63, sizeof(z.g));
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
for (int k = 0; k < 4; k++)
z.g[i][j] = min(z.g[i][j], x.g[i][k] + y.g[k][j]);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (z.g[i][j] > 1e18) z.g[i][j] = 1e18 + 1;
return z;
}
inline void buildSAM() {
scanf("%I64d", &m);
scanf("%s", c + 1);
n = strlen(c + 1);
SAM[0].slink = -1;
for (i = 1; i <= n; i++) {
sum_node++;
SAM[sum_node].maxlen = i;
p[i] = sum_node;
for (j = p[i - 1]; (j != -1) && (!SAM[j].trans[c[i] - 65]);
j = SAM[j].slink)
SAM[j].trans[c[i] - 65] = sum_node;
if (j == -1) {
SAM[sum_node].slink = 0;
SAM[sum_node].minlen = 1;
continue;
}
k = SAM[j].trans[c[i] - 65];
if (SAM[j].maxlen + 1 == SAM[k].maxlen) {
SAM[sum_node].slink = k;
SAM[sum_node].minlen = SAM[k].maxlen + 1;
continue;
}
sum_node++;
SAM[sum_node].reset(-1, SAM[j].maxlen + 1, SAM[k].slink, SAM[k].trans);
SAM[sum_node].minlen = SAM[SAM[sum_node].slink].maxlen + 1;
SAM[p[i]].slink = sum_node;
SAM[p[i]].minlen = SAM[sum_node].maxlen + 1;
SAM[k].slink = sum_node;
SAM[k].minlen = SAM[sum_node].maxlen + 1;
for (; (j != -1) && (SAM[j].trans[c[i] - 65] == k); j = SAM[j].slink)
SAM[j].trans[c[i] - 65] = sum_node;
}
return;
}
inline void buildfirstmatrix() {
for (cc = 'A'; cc <= 'D'; cc++) {
if (!SAM[0].trans[cc - 65]) {
for (cd = 'A'; cd <= 'D'; cd++) basic.g[cc - 65][cd - 65] = 1;
continue;
}
for (cd = 'A'; cd <= 'D'; cd++) basic.g[cc - 65][cd - 65] = n + 1;
memset(h, 0, sizeof(h));
tail = 1, g[tail] = SAM[0].trans[cc - 65], h[g[tail]] = 1;
for (head = 1; head <= tail; head++)
for (cd = 'A'; cd <= 'D'; cd++)
if (!SAM[g[head]].trans[cd - 65])
basic.g[cc - 65][cd - 65] =
min(basic.g[cc - 65][cd - 65], 1LL * h[g[head]]);
else if (!h[SAM[g[head]].trans[cd - 65]])
tail++, g[tail] = SAM[g[head]].trans[cd - 65],
h[g[tail]] = h[g[head]] + 1;
}
return;
}
inline matrix matrixpower(long long m) {
if (m == 1) return basic;
matrix z = matrixpower(m / 2);
z = z * z;
if (m & 1) z = z * basic;
return z;
}
inline void getans() {
l = 1, r = 1e18;
while (l < r) {
mid = (l + r) / 2;
result = matrixpower(mid);
t = 1e18 + 1;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) t = min(t, result.g[i][j]);
if (t >= m)
r = mid;
else
l = mid + 1;
}
printf("%I64d", l);
return;
}
int main() {
buildSAM();
buildfirstmatrix();
getans();
return 0;
}
| 5 |
#include <bits/stdc++.h>
int main() {
int n, a[400][400], h, i, j, cnt = 1;
scanf("%d", &n);
for (h = 0; 2 * n >= h * (h + 1); h++) {
}
h--;
printf("%d\n", h + 1);
for (i = 0; i < h; i++) {
for (j = i; j < h; j++) {
a[i][j] = cnt;
a[j + 1][i] = cnt;
cnt++;
}
}
for (i = 0; i < h + 1; i++) {
for (j = 0; j < h; j++) {
printf("%d ", a[i][j]);
}
printf("\n");
}
return 0;
}
| 3 |
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <tuple>
#include <functional>
using namespace std;
#define rep(i,n) for(int i = 0;i < n;i++)
#define int long long
const int INF = 1e9;
int dx[2][7] = {
{ +0,+1,+1,+0,-1,-1, 0 },
{ +0,+1,+1,+0,-1,-1, 0 },
};
int dy[2][7] = {
{ +1,+0,-1,-1,-1,+0, 0 },
{ +1,+1,+0,-1,+0,+1, 0 },
};
signed main() {
int w, h, sx, sy, gx, gy, n;
cin >> sx >> sy >> gx >> gy;
cin >> n;
vector<int> vx(n), vy(n);
rep(i, n) {
cin >> vx[i] >> vy[i];
}
cin >> w >> h;
map<int, map<int, int>> mp;
map<int, int> s;
for (int y = -h; y <= h; y++) {
s[y] = INF;
}
{
typedef pair<int, map<int, int>> pm;
queue<pm> q;
q.push(make_pair(0, s));
while (q.size()) {
auto x = q.front().first;
auto t = q.front().second;
q.pop();
if (mp.count(x))continue;
mp[x] = t;
if (abs(x)+1 > w)continue;
q.push(make_pair(x + 1, t));
q.push(make_pair(x - 1, t));
}
}
auto v = mp;
rep(i, n) {
v[vx[i]][vy[i]] = 1;
}
auto check = [&](int x, int y) {
if (abs(x) <= w && abs(y) <= h) {
if (v[x][y] == 1)return false;
return true;
}
return false;
};
vector<map<int, map<int, int>>> me(6, mp);
typedef tuple<int, int, int, int> T;
priority_queue<T, vector<T>,greater<T>> q;
q.push(T(0, sx, sy, 0));
me[0][sx][sy] = 0;
while (q.size()) {
int x = get<1>(q.top()),
y = get<2>(q.top()),
t = get<3>(q.top());
q.pop();
int d = abs(x % 2);
int nt = (t + 1) % 6;
{
int j = abs(x * y * t) % 6;
int tx = x + dx[d][j], ty = y + dy[d][j];
if (check(tx, ty) && me[nt][tx][ty] > me[t][x][y]) {
me[nt][tx][ty] = me[t][x][y];
q.push(T(me[nt][tx][ty], tx, ty, nt));
}
}
rep(j, 7) {
int tx = x + dx[d][j], ty = y + dy[d][j];
if (check(tx, ty) && me[nt][tx][ty] > me[t][x][y] + 1) {
me[nt][tx][ty] = me[t][x][y] + 1;
q.push(T(me[nt][tx][ty], tx, ty, nt));
}
}
}
int ans = INF;
rep(t, 6) {
ans = min(ans, me[t][gx][gy]);
}
if (ans == INF)ans = -1;
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxl = 2 * 1000 * 10 + 5;
const int maxm = 10 + 5;
bool adj[maxl][maxm];
int n, m, k;
int grp[maxm], node[maxl];
int main() {
cin >> n >> m >> k;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> adj[i][j];
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
grp[y]++;
node[x]--;
}
for (int i = 0; i < n; i++) {
int res = node[i];
for (int j = 0; j < m; j++) res += grp[j] * adj[i][j];
cout << res << " ";
}
cout << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if (c == 1) {
for (int i = 0; i <= 9; i++) {
if ((10 * a + i) % b == 0) {
cout << 10 * a + i << endl;
return 0;
}
}
cout << -1 << endl;
} else {
if (b == 10) {
cout << a;
for (int i = 0; i < c; i++) {
cout << 0;
}
return 0;
} else {
if (b >= 11) {
for (int i = 0; i <= 9; i++) {
if ((10 * a + i) % b == 0) {
cout << 10 * a + i;
for (int j = 0; j < c - 1; j++) {
cout << 0;
}
return 0;
}
}
cout << "-1" << endl;
} else {
for (int i = 0; i <= 9; i++) {
if ((10 * a + i) % b == 0) {
cout << 10 * a + i;
break;
}
}
for (int i = 0; i < c - 1; i++) {
cout << b;
}
return 0;
}
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[1000000 + 1];
string x[1000000 + 1];
int val[1000000 + 1];
int siaip[1000000 + 1];
void dfs(int i) {
for (int j : adj[i]) dfs(j);
if (x[i] == "IN") {
siaip[i] = val[i];
}
if (x[i] == "AND") {
siaip[i] = siaip[adj[i][0]] & siaip[adj[i][1]];
}
if (x[i] == "OR") {
siaip[i] = siaip[adj[i][0]] | siaip[adj[i][1]];
}
if (x[i] == "XOR") {
siaip[i] = siaip[adj[i][0]] ^ siaip[adj[i][1]];
}
if (x[i] == "NOT") {
siaip[i] = 1 - siaip[adj[i][0]];
}
}
int kita[1000000 + 1];
vector<int> J[2];
void dfs1(int i, int p) {
if (x[i] == "IN") {
int v = 0;
for (int j : adj[p])
if (j != i) v = siaip[j];
int vx = 1 - val[i];
if (x[p] == "NOT") {
kita[i] = J[1 - vx].back();
}
if (x[p] == "XOR") {
kita[i] = J[v ^ vx].back();
}
if (x[p] == "AND") {
kita[i] = J[v & vx].back();
}
if (x[p] == "OR") {
kita[i] = J[v | vx].back();
}
return;
}
if (i == 1) {
J[0].push_back(0);
J[1].push_back(1);
} else {
int v = 0;
for (int j : adj[p])
if (j != i) v = siaip[j];
int a[2] = {J[0].back(), J[1].back()};
if (x[p] == "NOT") {
J[0].push_back(a[1]);
J[1].push_back(a[0]);
}
if (x[p] == "XOR") {
J[0].push_back(a[0 ^ v]);
J[1].push_back(a[1 ^ v]);
}
if (x[p] == "AND") {
J[0].push_back(a[0 & v]);
J[1].push_back(a[1 & v]);
}
if (x[p] == "OR") {
J[0].push_back(a[0 | v]);
J[1].push_back(a[1 | v]);
}
}
for (int j : adj[i]) {
dfs1(j, i);
}
J[0].pop_back();
J[1].pop_back();
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0), cerr.tie(0);
int n;
cin >> n;
vector<int> ins;
for (int i = 1; i <= n; i++) {
cin >> x[i];
if (x[i] == "IN") {
int c;
cin >> c;
val[i] = c;
ins.push_back(i);
continue;
}
int k = 2;
if (x[i] == "NOT") {
k = 1;
}
while (k--) {
int j;
cin >> j;
adj[i].push_back(j);
}
}
dfs(1);
dfs1(1, 1);
for (int i : ins) cout << kita[i];
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long const M = 100 + 10, M2 = 1e7 + 10, mod = 1e9 + 7, inf = 1e18 + 10;
long long a[M];
bool mark[M][M];
long long lis[M], n, t;
struct mat {
long long dp[M][M];
mat(long long x) {
for (long long i = 1; i < M - 5; i++) {
for (long long j = 1; j < M - 5; j++) {
dp[i][j] = 0;
}
}
for (long long j = 1; j <= n; j++) dp[j][j] = x;
}
};
mat ans(0);
void fdp(long long i) {
for (long long j = 1; j <= n; j++) {
lis[j] = 0;
if (a[j] < a[i]) {
mark[i][j] = 1;
continue;
}
lis[j] = 1;
for (long long k = 1; k < j; k++) {
if (a[k] <= a[j]) lis[j] = max(lis[k] + 1, lis[j]);
}
ans.dp[i][j] = lis[j];
}
}
mat operator*(mat &a, mat &b) {
mat c(0);
for (long long i = 1; i <= n; i++) {
for (long long j = 1; j <= n; j++) {
if (mark[i][j]) continue;
for (long long k = 1; k <= n; k++) {
if (mark[i][k] || mark[k][j]) continue;
c.dp[i][j] = max(a.dp[i][k] + b.dp[k][j], c.dp[i][j]);
}
}
}
return c;
}
mat pw(mat &x, long long y) {
mat tmp = x;
mat c(0);
while (y) {
if (y % 2 == 1) c = c * tmp;
tmp = tmp * tmp;
y /= 2;
}
return c;
}
int32_t main() {
cin >> n >> t;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i <= n; i++) {
fdp(i);
}
ans = pw(ans, t);
long long mn = inf, ind;
long long all = 0;
for (long long i = 1; i <= n; i++) {
if (a[i] < mn) mn = a[i], ind = i;
}
for (long long i = 1; i <= n; i++) {
all = max(all, ans.dp[ind][i]);
}
cout << all;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline int add(int a, int b, int m = 1000000007) {
a += b;
if (a >= m) a -= m;
return a;
}
inline int mul(int a, int b, int m = 1000000007) {
return (int)(((long long)a * (long long)b) % m);
}
inline int norm(int x, int m = 1000000007) {
if (x >= m) x %= m;
if (x < 0) x += m;
return x;
}
inline int neg(int x, int m = 1000000007) {
x = -x;
return norm(x);
}
vector<int> z_function(string s) {
int n = s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r) z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
return z;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s, t;
cin >> s >> t;
int ns, nt;
ns = int((s).size()), nt = int((t).size());
string sr = s;
reverse((sr).begin(), (sr).end());
int i = 0;
vector<pair<int, int> > yay;
bool gg = 0;
while (i < nt) {
string a, b;
for (int j = i; j <= nt - 1; ++j) a += t[j];
a += "#";
b = a;
a += s;
b += sr;
vector<int> za = z_function(a);
vector<int> zb = z_function(b);
int val = 0;
pair<int, int> ans;
for (int j = nt + 1 - i; j <= int((za).size()) - 1; ++j) {
if (za[j] > val) {
val = za[j];
ans.first = j - (nt + 1 - i);
ans.second = j - (nt + 1 - i) + za[j] - 1;
}
}
for (int j = nt + 1 - i; j <= int((zb).size()) - 1; ++j) {
if (zb[j] > val) {
val = zb[j];
ans.first = j - (nt + 1 - i);
ans.second = j - (nt + 1 - i) + zb[j] - 1;
ans.first = ns - ans.first - 1;
ans.second = ns - ans.second - 1;
if (ans.first <= ans.second) swap(ans.first, ans.second);
}
}
i += val;
if (val == 0) {
cout << "-1\n";
return 0;
}
yay.push_back(ans);
}
cout << int((yay).size()) << "\n";
for (auto it = (yay).begin(); it != (yay).end(); ++it) {
cout << it->first + 1 << " " << it->second + 1 << "\n";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
vector<pair<int,int> > e[maxn];
int ans[maxn];
void dfs(int u, int p){
for(auto ee : e[u]){
int to = ee.first;
if(to == p) continue;
int k = ee.second % 2;
ans[to] = ans[u] ^ k;
dfs(to, u);
}
}
int main(){
int n; cin >> n;
for(int i = 0; i < n - 1; i++){
int u, v, w;
cin >> u >> v >> w;
w %= 2;
e[u].push_back({v, w});
e[v].push_back({u, w});
}
dfs(1, 0);
for(int i = 1; i <= n; i++) cout << ans[i] << endl;
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.