solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k, i, j, ans;
cin >> n >> k;
vector<int> a(n + 1);
int count = 0;
for (i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] < 0) count++;
}
k = k - count;
if (k < 0)
cout << -1;
else {
ans = 0;
vector<int> arr1;
vector<int> arr2;
i = n;
count = 0;
while (i >= 1 && a[i] >= 0) {
i--;
count++;
}
j = 1;
while (j <= n && a[j] >= 0) j++;
if (count != n) {
ans++;
if (count) {
ans++;
arr1.push_back(count);
}
}
for (; j <= i;) {
if (j <= n && a[j] < 0)
j++;
else {
int w = j;
count = 0;
while (w <= n && a[w] >= 0) {
count++;
w++;
}
arr2.push_back(count);
j = w;
}
}
ans += 2 * arr2.size();
sort(arr2.begin(), arr2.end());
int diff1 = 0, diff2 = 0;
int copy1 = k, copy2 = k;
for (i = 0; i < arr1.size(); i++) {
if (copy1 - arr1[i] >= 0) {
copy1 = copy1 - arr1[i];
diff1 += 1;
}
}
for (i = 0; i < arr2.size(); i++) {
if (copy1 - arr2[i] >= 0) {
copy1 = copy1 - arr2[i];
diff1 += 2;
}
}
for (i = 0; i < arr2.size(); i++) {
if (copy2 - arr2[i] >= 0) {
copy2 = copy2 - arr2[i];
diff2 += 2;
}
}
for (i = 0; i < arr1.size(); i++) {
if (copy2 - arr1[i] >= 0) {
copy2 = copy2 - arr1[i];
diff2 += 1;
}
}
cout << min(ans - diff1, ans - diff2);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
#define fastcin() cin.tie(0);ios::sync_with_stdio(false)
typedef long long ll;
typedef pair<ll,ll> P;
#define MAX_V 10000
struct edge{int to;long long cap,rev;};
vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];
void add_edge(int from,int to,long long cap){
G[from].push_back((edge){to,cap,(ll)G[to].size()});
G[to].push_back((edge){from,0,(ll)G[from].size()-1});
}
void bfs(int s){
memset(level,-1,sizeof(level));
queue<int> que;
level[s] = 0;
que.push(s);
while(!que.empty()) {
int v = que.front();que.pop();
for(int i = 0;i < G[v].size();++i){
edge &e = G[v][i];
if(e.cap > 0 && level[e.to] < 0){
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
int dfs(int v ,int t,long long f){
if(v == t)return f;
for(int &i = iter[v];i < G[v].size() ;++i){
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to]){
long long d = dfs(e.to,t,min(f,e.cap));
if(d > 0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
long long Dinic(int s,int t){
long long flow = 0;
while(1){
bfs(s);
if(level[t] < 0)return flow;
memset(iter,0,sizeof(iter));
long long f;while((f = dfs(s,t,INF)) > 0)flow += f;
}
}
int main(){
ll st = 1000, en = 1001;
ll n, m;cin >> n >> m;
string str;cin >> str;
REP(i, n){
ll a;cin >> a;
if(str[i] == 'R'){
add_edge(st, i, 0);
add_edge(i, en, a);
}
else {
add_edge(st, i, a);
add_edge(i, en, 0);
}
}
REP(i, m){
ll a, b ,c;cin >> a >> b >> c;
a--;b--;
if(a > b)swap(a, b);
add_edge(b, a, c);
}
cout << Dinic(st, en) << endl;
return 0;
}
| 0 |
#include <iostream>
#include <string>
int main()
{
int n, a, b;
std::string s;
std::cin >> n >> a >> b >> s;
int cnt = 0;
int bcnt = 0;
const int max = a + b;
for (char c : s) {
if (c == 'b') bcnt++;
if (cnt >= max || c == 'c' || (c == 'b' && bcnt > b)) {
std::cout << "No\n";
}
else {
std::cout << "Yes\n";
cnt++;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
string str1, str2;
cin >> str1 >> str2;
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
int first = str2.find("1");
int second = str1.find("1", first);
cout << second - first << endl;
}
}
| 1 |
#include <bits/stdc++.h>
constexpr int a[] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 0};
int f(int n) { return a[n % 16] + (n >= 16 ? f(n >> 4) : 0); }
int main(void) {
int n;
std::cin >> n;
std::cout << f(n);
return 0;
}
| 2 |
#include<iostream>
#include<vector>
#include<algorithm>
typedef long long ll;
using namespace std;
ll gcd(ll a,ll b){
if(b==0)return a;
return gcd(b, a%b);
}
ll lcm(ll a,ll b){
return a*b/gcd(a,b);
}
ll lcm_n(vector<ll> num){
ll l=num[0];
for(int i=0;i<num.size();i++)l=lcm(l,num[i]);
return l;
}
ll func(ll a, ll m){
ll cnt=1,x=a%m;
while(x!=1)x=(a*x)%m,cnt++;
return cnt;
}
int main(void){
ll m[4],a[4];
while(true){
int b=0;
for(int i=1;i<4;i++){
cin >> a[i] >> m[i];
b=a[i]+m[i];
}
if(b==0)break;
ll cnt=0,x=1,y=1,z=1;
vector<ll>num;
for(int i=1;i<4;i++)
num.push_back(func(a[i],m[i]));
cout << lcm_n(num) << endl;
//cout << lcm(lcm(num[0],num[1]),num[2])<< endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, a, b;
cin >> n >> a >> b;
if (a * b >= 6 * n) {
cout << a * b << endl;
cout << a << " " << b << endl;
} else {
long long ret = (1LL << 60);
long long x, y;
for (long long i = 1; i <= 100000; i++) {
long long j = 6 * n / i;
if (6 * n % i) {
j++;
}
if (i >= a && j >= b && i * j >= 6 * n && i * j < ret) {
ret = i * j;
x = i;
y = j;
}
}
for (long long j = 1; j <= 100000; j++) {
long long i = 6 * n / j;
if (6 * n % j) {
i++;
}
if (i >= a && j >= b && i * j >= 6 * n && i * j < ret) {
ret = i * j;
x = i;
y = j;
}
}
cout << ret << endl;
cout << x << " " << y << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct LCA {
int n;
std::vector<std::vector<int>> parent;
std::vector<int> depth;
LCA(std::vector<std::vector<int>> &graph, int _n, int root)
: n(_n), depth(n), parent(n, std::vector<int>(log2(n) + 1, -1)) {
dfs(graph, root, -1, 0);
for (int k = 0; k < int(log2(n)); ++k) {
for (int i = 0; i < n; ++i) {
if (parent[i][k] != -1) {
parent[i][k + 1] = parent[parent[i][k]][k];
}
}
}
}
void dfs(const std::vector<std::vector<int>> &graph, int v, int p, int d) {
depth[v] = d;
parent[v][0] = p;
for (int i = 0; i < graph[v].size(); ++i) {
if (graph[v][i] != p) dfs(graph, graph[v][i], v, d + 1);
}
}
int get(int v, int w) {
if (depth[v] < depth[w]) std::swap(v, w);
int dif = depth[v] - depth[w];
for (int i = 0; i < log2(n) + 1; ++i) {
if (1 << i & dif) v = parent[v][i];
}
if (v == w) return v;
for (int i = log2(n) - 1; i >= 0; --i) {
if (parent[v][i] != parent[w][i] && parent[v][i] != -1) {
v = parent[v][i];
w = parent[w][i];
}
}
return parent[v][0];
}
};
constexpr ll mod = 998244353;
ll po(ll a, ll n) {
if (n == 0) return 1;
ll d = po(a, n / 2);
if (n & 1) return a * d % mod * d % mod;
return d * d % mod;
}
ll inv(ll a) { return po(a, mod - 2); }
pll dfs(vvi &g, vi &color, int v, int prev) {
pll ret(0, 0);
if (color[v] == -1) {
vector<pll> p;
for (ll i = 0; i < ll(g[v].size()); ++i)
if (g[v][i] != prev) p.push_back(dfs(g, color, g[v][i], v));
ret.first = 1;
for (ll i = 0; i < ll(p.size()); ++i) {
ret.first *= p[i].first + p[i].second;
ret.first %= mod;
}
for (ll i = 0; i < ll(p.size()); ++i) {
ret.second +=
p[i].second * ret.first % mod * inv(p[i].first + p[i].second) % mod;
ret.second %= mod;
}
} else {
vector<pll> p;
for (ll i = 0; i < ll(g[v].size()); ++i)
if (g[v][i] != prev) p.push_back(dfs(g, color, g[v][i], v));
ret.second = 1;
for (ll i = 0; i < ll(p.size()); ++i) {
ret.second *= p[i].first + p[i].second;
ret.second %= mod;
}
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vi c(n);
for (ll i = 0; i < ll(n); ++i) cin >> c[i];
for (ll i = 0; i < ll(n); ++i) c[i]--;
vvi g(n);
for (ll i = 0; i < ll(n - 1); ++i) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
vvi v(k);
for (ll i = 0; i < ll(n); ++i) {
if (c[i] != -1) v[c[i]].push_back(i);
}
LCA lca(g, n, 0);
vi par(n);
vi color = c;
bool ok = true;
for (ll i = 0; i < ll(n); ++i) par[i] = lca.parent[i][0];
for (ll i = 0; i < ll(k); ++i) {
int k = v[i][0];
for (ll j = 0; j < ll(v[i].size() - 1); ++j) {
int l = lca.get(k, v[i][j + 1]);
while (l != k) {
int t = par[k];
if (color[t] == -1) {
color[t] = i;
} else if (color[t] != i) {
ok = false;
break;
} else
break;
k = t;
}
int tmp = v[i][j + 1];
while (l != tmp) {
int t = par[tmp];
if (color[t] == -1) {
color[t] = i;
} else if (color[t] != i) {
ok = false;
break;
} else
break;
tmp = t;
}
if (color[l] != -1 && color[l] != i) ok = false;
color[l] = i;
k = l;
}
}
if (!ok) {
cout << 0 << endl;
} else {
auto ans = dfs(g, color, 0, -1);
cout << ans.second << endl;
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[1005];
vector<pair<int, int> > vec[1005];
int f[3][3];
long long go[125][125][32];
long long one[125][125][3];
long long dp[125][2];
long long way[1005][4];
long long DP[1005][4];
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
cin >> m;
for (int i = 0; i < m; i++) {
int x, y, c;
cin >> x >> y >> c;
x--;
c--;
vec[x].push_back(make_pair(y, c));
}
for (int i = 0; i < n; i++) vec[i].push_back(make_pair(a[i] + 1, -1));
for (int i = 0; i < n; i++) sort(vec[i].begin(), vec[i].end());
for (int c = 0; c < 3; c++)
for (int j = 0; j < 3; j++) cin >> f[c][j];
for (int c = 0; c < 3; c++) {
for (int i = 0; i < 64; i++) {
int v[4];
int x = i;
for (int j = 0; j < 3; j++) {
v[j] = x % 4;
x /= 4;
}
bool used[5] = {};
for (int j = 0; j < 3; j++) {
if (f[c][j] == 0) continue;
used[v[2 - j]] = 1;
}
for (int i = 0; i < 5; i++)
if (!used[i]) {
v[3] = i;
break;
}
int vv = v[1] + 4 * v[2] + 16 * v[3];
go[i][vv][0]++;
one[i][vv][c]++;
}
}
for (int k = 0; k < 31; k++)
for (int ab = 0; ab < 64; ab++)
for (int a = 0; a < 64; a++)
for (int b = 0; b < 64; b++) {
go[a][b][k + 1] += go[a][ab][k] * go[ab][b][k] % 998244353;
if (go[a][b][k + 1] >= 998244353) go[a][b][k + 1] -= 998244353;
}
DP[0][0] = 1;
for (int i = 0; i < n; i++) {
int C = 0;
for (int a = 0; a < 64; a++)
for (int b = 0; b < 2; b++) dp[a][b] = 0;
dp[63][0] = 1;
int cur = 0, nxt = 1;
for (int j = 0; j < vec[i].size(); j++) {
int pos = vec[i][j].first;
int num = pos - C - 1;
for (int q = 0; q < 31; q++) {
if (!((num >> q) & 1)) continue;
for (int a = 0; a < 64; a++) dp[a][nxt] = 0;
for (int a = 0; a < 64; a++)
for (int b = 0; b < 64; b++) {
dp[b][nxt] += dp[a][cur] * (go[a][b][q]) % 998244353;
}
for (int a = 0; a < 64; a++) dp[a][nxt] %= 998244353;
swap(cur, nxt);
}
if (j == vec[i].size() - 1) break;
{
for (int a = 0; a < 64; a++) dp[a][nxt] = 0;
for (int a = 0; a < 64; a++)
for (int b = 0; b < 64; b++) {
dp[b][nxt] +=
dp[a][cur] * (one[a][b][vec[i][j].second]) % 998244353;
}
for (int a = 0; a < 64; a++) dp[a][nxt] %= 998244353;
swap(cur, nxt);
}
C = pos;
}
for (int a = 0; a < 64; a++) {
if (a / 16 >= 4) {
assert(dp[a][cur] == 0);
continue;
}
way[i][a / 16] += dp[a][cur];
way[i][a / 16] %= 998244353;
}
for (int a = 0; a < 4; a++)
for (int b = 0; b < 4; b++) {
DP[i + 1][b] += DP[i][a] * way[i][a ^ b] % 998244353;
DP[i + 1][b] %= 998244353;
}
}
cout << (DP[n][0] + 998244353) % 998244353 << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct Point {
long long x;
long long y;
};
int n;
Point p[1000];
vector<int> adj[1000];
map<long long, vector<pair<long long, int>>> samex, samey;
int vis[1000];
int comp[1000];
vector<array<long long, 5>> hor, vert;
void dfs(int u, int c) {
vis[u] = 1;
comp[u] = c;
for (auto v : adj[u]) {
if (!vis[v]) dfs(v, c);
}
}
bool get(long long t) {
for (int i = 0; i < n; ++i) adj[i].clear();
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (p[i].x == p[j].x && abs(p[i].y - p[j].y) <= t) {
adj[i].push_back(j);
adj[j].push_back(i);
}
if (p[i].y == p[j].y && abs(p[i].x - p[j].x) <= t) {
adj[i].push_back(j);
adj[j].push_back(i);
}
}
}
int c = 0;
for (int i = 0; i < n; ++i) {
if (!vis[i]) dfs(i, c++);
}
if (c == 1) return true;
if (c == 2) {
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (comp[i] != comp[j])
if (abs(p[i].x - p[j].x) + abs(p[i].y - p[j].y) <= 2 * t) {
return true;
}
}
}
}
if (c == 3) {
for (auto &h : hor) {
for (int i = 0; i < n; ++i) {
int pres[] = {0, 0, 0};
pres[comp[i]] = 1;
pres[comp[h[3]]] = 1;
pres[comp[h[4]]] = 1;
if (pres[0] == 0 || pres[1] == 0 || pres[2] == 0) continue;
int x = p[i].x;
int y = h[2];
if (abs(x - h[0]) > t || abs(x - h[1]) > t || abs(y - p[i].y) > t)
continue;
return true;
}
}
for (auto &v : vert) {
for (int i = 0; i < n; ++i) {
int pres[] = {0, 0, 0};
pres[comp[i]] = 1;
pres[comp[v[3]]] = 1;
pres[comp[v[4]]] = 1;
if (pres[0] == 0 || pres[1] == 0 || pres[2] == 0) continue;
int y = p[i].y;
int x = v[2];
if (abs(y - v[0]) > t || abs(y - v[1]) > t || abs(x - p[i].x) > t)
continue;
return true;
}
}
}
if (c == 4) {
for (auto &h : hor) {
for (auto &v : vert) {
int pres[] = {0, 0, 0, 0};
pres[comp[h[3]]] = 1;
pres[comp[h[4]]] = 1;
pres[comp[v[3]]] = 1;
pres[comp[v[4]]] = 1;
if (pres[0] == 0 || pres[1] == 0 || pres[2] == 0 || pres[3] == 0)
continue;
int x = v[2];
int y = h[2];
if (abs(y - v[0]) > t || abs(y - v[1]) > t || abs(x - h[0]) > t ||
abs(x - h[1]) > t)
continue;
return true;
}
}
}
return false;
}
void solve(int tc) {
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> p[i].x >> p[i].y;
samex[p[i].x].push_back({p[i].y, i});
samey[p[i].y].push_back({p[i].x, i});
}
for (auto &x : samex) {
sort((x.second).begin(), (x.second).end());
}
for (auto &y : samey) {
sort((y.second).begin(), (y.second).end());
}
for (auto &sx : samex) {
for (int j = 0; j < sx.second.size() - 1; ++j) {
vert.push_back({sx.second[j].first, sx.second[j + 1].first, sx.first,
sx.second[j].second, sx.second[j + 1].second});
}
}
for (auto &sy : samey) {
for (int j = 0; j < sy.second.size() - 1; ++j) {
hor.push_back({sy.second[j].first, sy.second[j + 1].first, sy.first,
sy.second[j].second, sy.second[j + 1].second});
}
}
long long l = 0, r = 1e10;
while (l < r) {
long long m = (l + r) / 2;
if (get(m)) {
r = m;
} else
l = m + 1;
}
if (get(l))
cout << l << endl;
else
cout << -1 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
for (int tc = 1; tc <= t; ++tc) {
solve(tc);
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,N) for(i=0;i<N;i++)
typedef long long ll;
typedef struct{
int from,to,cost;
}edge;
int main(void){
int i,n_node=1,n_2=1,L;
cin >> L;
vector<edge> v;
while(2*n_2<=L&&n_node<20){
v.push_back({n_node,n_node+1,n_2});
n_2*=2;
n_node++;
}
int N_2=n_2;
for(i=n_node-1;i>0;i--){
n_2/=2;
if(L-n_2>=N_2){
v.push_back({i,n_node,L-n_2});
L-=n_2;
}
}
REP(i,n_node-1){
v.push_back({i+1,i+2,0});
}
cout << n_node << " " << v.size() << endl;
REP(i,v.size()){
cout << v[i].from << " " << v[i].to << " " << v[i].cost << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int k;
vector<pair<int, int>> es[200006];
vector<int> ns(200006, 0);
pair<long long, bool> solveG(int idx, int fa) {
long long res = 0;
bool odd = true;
ns[idx] = 1;
for (int i = 0; i < es[idx].size(); ++i) {
if (es[idx][i].first == fa) continue;
auto tRes = solveG(es[idx][i].first, idx);
ns[idx] += ns[es[idx][i].first];
res += tRes.first;
if (tRes.second) {
res += es[idx][i].second;
odd = !odd;
}
}
return make_pair(res, odd);
}
struct BResult {
long long cost;
long long nPaths;
};
BResult solveB(int idx, int fa) {
BResult res;
res.cost = 0;
for (int i = 0; i < es[idx].size(); ++i) {
if (es[idx][i].first == fa) continue;
auto tRes = solveB(es[idx][i].first, idx);
res.cost += tRes.cost + tRes.nPaths * es[idx][i].second;
}
res.nPaths = min(ns[idx], k + k - ns[idx]);
return res;
}
int main() {
int tt, a, b, t;
scanf("%d", &tt);
while (--tt >= 0) {
scanf("%d", &k);
for (int i = 1; i <= k + k; ++i) es[i].clear();
for (int i = 1; i < k + k; ++i) {
scanf("%d %d %d", &a, &b, &t);
es[a].emplace_back(b, t);
es[b].emplace_back(a, t);
}
cout << solveG(1, 0).first;
cout << " " << solveB(1, 0).cost << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
double dp[201][256][300][2];
int main() {
int x, k, p, i, j, l, r;
double ans = 0;
scanf("%d %d %d", &x, &k, &p);
if (x & (1 << 8)) {
int y = x >> 8, c = 0;
while (y) {
if (y % 2 == 0) break;
y >>= 1;
c++;
}
dp[0][x % 256][c][1] = 1;
} else {
int y = x >> 8, c = 0;
while (y) {
if (y % 2 == 1) break;
y >>= 1;
c++;
}
dp[0][x % 256][c][0] = 1;
}
for (i = 0; i < k; i++) {
for (j = 0; j < 256; j++) {
for (l = 0; l < 300; l++) {
for (r = 0; r < 2; r++) {
double p1, p2;
if (dp[i][j][l][r] == 0) continue;
p1 = dp[i][j][l][r] * p / 100;
p2 = dp[i][j][l][r] * (100 - p) / 100;
if (j * 2 >= 256) {
if (r == 0) {
dp[i + 1][j * 2 % 256][1][1] += p1;
} else {
dp[i + 1][j * 2 % 256][l + 1][1] += p1;
}
} else {
if (r == 0) {
dp[i + 1][j * 2 % 256][l + 1][0] += p1;
} else {
dp[i + 1][j * 2 % 256][1][0] += p1;
}
}
if (j + 1 == 256) {
if (r == 0) {
dp[i + 1][0][1][1] += p2;
} else {
dp[i + 1][0][l][0] += p2;
}
} else {
dp[i + 1][j + 1][l][r] += p2;
}
}
}
}
}
for (i = 0; i < 256; i++) {
for (j = 0; j < 300; j++) {
for (l = 0; l < 2; l++) {
if (i == 0) {
ans += dp[k][i][j][l] * (j * (1 - l) + 8);
} else {
ans += dp[k][i][j][l] * __builtin_ctz(i);
}
}
}
}
printf("%.12lf\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long in;
cin >> in;
long long c = 0;
bool flag = true;
long long out = 1;
while (in > 1 && flag) {
flag = false;
for (long long i = 2; i <= in / i; i++)
if (in % i == 0) {
flag = true;
if (c < 2) out *= i;
c++;
in /= i;
break;
}
}
if (c == 0)
cout << 1 << endl << 0 << endl;
else if (c == 1)
cout << 2 << endl;
else
cout << 1 << endl << out << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void no() {
cout << -1 << "\n";
exit(0);
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
map<long long, long long> col;
cin >> col[4];
cin >> col[7];
cin >> col[47];
cin >> col[74];
if (abs(col[47] - col[74]) > 1) {
no();
}
string an = "";
if (col[47] >= col[74]) {
for (long long i = 0; i < col[47]; ++i) {
col[4]--;
col[7]--;
an += "47";
}
if (col[47] == col[74]) {
if (col[4] > 0) {
an += "4";
col[4]--;
} else if (col[7] > 0) {
an = "7" + an;
col[7]--;
} else {
no();
}
}
col[47] = 0;
col[74] = 0;
} else {
for (long long i = 0; i < col[74]; ++i) {
an += "74";
col[7]--;
col[4]--;
}
col[74] = 0;
col[47] = 0;
}
if (col[47] == col[74] && col[47] > 0) {
if (an[((long long)an.size()) - 1] == '4') {
an += '7';
col[7]--;
} else {
an += '4';
col[4]--;
}
}
if (col[4] < 0 || col[7] < 0) {
no();
}
long long sev = (long long)count((an).begin(), (an).end(), '7');
for (char c : an) {
if (c == '4') {
if (col[4] > 0) {
for (long long i = 0; i < col[4]; ++i) {
cout << '4';
}
col[4] = 0;
}
} else {
if (col[7] > 0 && sev == 1) {
for (long long i = 0; i < col[7]; ++i) {
cout << '7';
}
col[7] = 0;
}
}
sev -= c == '7';
cout << c;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void print(vector<int> &v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
void div(int a, vector<int> &v) {
int ans = 0;
for (int i = 1; i <= sqrt(a); i++) {
if (a % i == 0) {
if (a / i == i) {
ans++;
} else {
ans += 2;
}
}
}
v[a] = ans;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
vector<int> v(1000005, -1);
long long int ans = 0;
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
for (int k = 1; k <= c; k++) {
if (v[i * j * k] == -1) {
div(i * j * k, v);
ans += v[i * j * k];
} else {
ans += v[i * j * k];
}
}
}
}
cout << ans % 1073741824 << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline long long gi() {
long long _x = 0, _tmp = 1;
char _tc = getchar();
while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
if (_tc == '-') _tc = getchar(), _tmp = -1;
while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
return _x * _tmp;
}
void bye() {
puts("-1");
exit(0);
}
long long mod7 = 1e9 + 7;
long long inf = INT_MAX - 10;
long long ncase = 1;
long long N, M;
string str;
const int mxn = 3e5 + 5;
void pre() { return; }
void init() { N = gi(); }
void sol() {
long long res = 0;
for (int i = 2; i <= N; ++i) {
for (int j = 2; 1ll * j * i <= N; ++j) {
res += j * 4;
}
}
cout << res << endl;
return;
}
int main() {
pre();
while (ncase--) {
init();
sol();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int m, n, q[int(1e6)], a[int(1e6)];
int main() {
scanf("%i", &m);
int mn = 1e9;
for (int i = 0; i < m; i++) {
scanf("%i", &q[i]);
if (q[i] < mn) mn = q[i];
}
scanf("%i", &n);
for (int i = 0; i < n; i++) scanf("%i", &a[i]);
sort(a, a + n);
long long int sum = 0;
for (int i = n - 1; i >= 0; i--) {
if (n - (n - i) >= mn) {
int now = i;
for (i; i > now - mn; i--) sum += a[i];
i--;
} else {
for (i; i >= 0; i--) sum += a[i];
}
}
printf("%I64d\n", sum);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const long long int MAX = 2e18;
int main() {
int t;
cin >> t;
while (t--) {
int ar[10], cnt = 0;
cin >> ar[0] >> ar[1] >> ar[2];
if (ar[0]) {
ar[0]--;
cnt++;
}
if (ar[1]) {
ar[1]--;
cnt++;
}
if (ar[2]) {
ar[2]--;
cnt++;
}
sort(ar, ar + 3);
if (ar[2] && ar[1]) {
ar[1]--;
ar[2]--;
cnt++;
}
if (ar[2] && ar[0]) {
ar[0]--;
ar[2]--;
cnt++;
}
if (ar[0] && ar[1]) {
ar[1]--;
ar[0]--;
cnt++;
}
if (ar[0] && ar[1] && ar[2]) {
cnt++;
}
cout << cnt << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAXN = 35, MAXK = 50 + 10;
int t, dp[MAXN][MAXN][MAXK];
vector<int> res;
void make_dp() {
for (int i = 0; i < MAXN; i++)
for (int j = 0; j < MAXN; j++)
for (int k = 0; k < MAXK; k++) dp[i][j][k] = INF;
dp[0][0][0] = 0;
dp[1][1][1] = 0;
dp[1][1][0] = 0;
for (int n = 0; n < MAXN; n++) {
for (int m = 0; m < MAXN; m++) {
for (int k = 0; k < MAXK; k++) {
if (n == 0 || m == 0) dp[n][m][k] = 0;
if (k == 0) dp[n][m][k] = 0;
if (m * n == k) dp[n][m][k] = 0;
if (k > m * n) continue;
for (int i = 1; i < n; i++)
for (int j = 0; j <= k; j++)
dp[n][m][k] =
min(dp[n][m][k], dp[i][m][j] + m * m + dp[n - i][m][k - j]);
for (int i = 1; i < m; i++)
for (int j = 0; j <= k; j++)
dp[n][m][k] =
min(dp[n][m][k], dp[n][i][j] + n * n + dp[n][m - i][k - j]);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
make_dp();
for (int SinaKhare_Dadasham = 0; SinaKhare_Dadasham < t;
SinaKhare_Dadasham++) {
int n, m, k;
cin >> n >> m >> k;
cout << dp[n][m][k] << endl;
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000;
int a[MAXN];
int d[MAXN];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) cin >> a[i];
d[0] = a[0];
d[1] = a[1];
for (int i = 2; i < n; ++i) d[i] = min(a[i], max(d[i - 1], d[i - 2]));
cout << min(d[n - 1], d[0]) << endl;
}
int main() {
solve();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const long long OO = 1LL << 61;
const long long MOD = 2147483647;
const int maxn = 5005;
long long dp[maxn][3];
struct EDGE {
int v, next;
long long w;
} E[maxn << 1];
int head[maxn], tol;
int U[maxn], V[maxn];
long long C[maxn];
void add_edge(int u, int v, long long w) {
E[tol].v = v;
E[tol].w = w;
E[tol].next = head[u];
head[u] = tol++;
}
void Init() {
memset(head, -1, sizeof head);
tol = 0;
}
void dfs(int u, int pre) {
dp[u][0] = dp[u][1] = dp[u][2] = 0;
for (int i = head[u]; i != -1; i = E[i].next) {
int v = E[i].v;
if (v == pre) continue;
dfs(v, u);
dp[u][2] += dp[v][2] + dp[v][1] * dp[u][0] + dp[v][0] * dp[u][1] +
dp[u][0] * dp[v][0] * E[i].w;
dp[u][1] += dp[v][1] + E[i].w * dp[v][0];
dp[u][0] += dp[v][0];
}
dp[u][0]++;
dp[u][2] += dp[u][1];
}
void dfsm(int u, int pre, long long& mi) {
mi = min(mi, dp[u][1]);
for (int i = head[u]; i != -1; i = E[i].next) {
int v = E[i].v;
if (v == pre) continue;
dp[v][1] = dp[v][1] + (dp[u][1] - dp[v][1] - E[i].w * dp[v][0]) +
(dp[u][0] - dp[v][0]) * E[i].w;
dp[v][0] = dp[u][0];
dfsm(v, u, mi);
}
}
int main() {
int n;
long long c;
while (scanf("%d", &n) != EOF) {
Init();
for (int i = 1; i <= n - 1; i++) {
scanf("%d %d %lld", &U[i], &V[i], &C[i]);
add_edge(U[i], V[i], C[i]);
add_edge(V[i], U[i], C[i]);
}
long long ans = OO;
for (int i = 1; i <= n - 1; i++) {
long long mi = OO;
dfs(U[i], V[i]);
dfsm(U[i], V[i], mi);
long long s1 = (n - dp[U[i]][0]) * mi + dp[U[i]][2];
mi = OO;
dfs(V[i], U[i]);
dfsm(V[i], U[i], mi);
long long s2 = (n - dp[V[i]][0]) * mi + dp[V[i]][2];
long long sum = s1 + s2 + dp[U[i]][0] * dp[V[i]][0] * C[i];
ans = min(ans, sum);
}
cout << ans << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 505;
long long dp[2][maxn][maxn];
char str[maxn][maxn];
const int mod = 1000000007;
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", str[i]);
if (n + m <= 3) {
printf("%d\n", str[0][0] == str[n - 1][m - 1]);
return 0;
}
if (str[0][0] != str[n - 1][m - 1]) {
printf("%d\n", 0);
return 0;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) dp[0][i][j] = 0;
dp[0][0][n - 1] = 1;
int cur = 0;
for (int step = 1; step < (n + m) / 2; step++) {
cur ^= 1;
memset(dp[cur], 0, sizeof(dp[cur]));
for (int i = 0; i < n && i <= step; i++)
for (int j = n - 1; j >= 0 && j >= i && n - 1 - j <= step; j--) {
if ((step - i) > (m - 1 - (step - (n - 1 - j)))) continue;
int x1 = i, x2 = j, y1 = step - i, y2 = m - 1 - (step - (n - 1 - j));
if (str[x1][y1] != str[x2][y2]) continue;
dp[cur][x1][x2] = dp[cur ^ 1][x1][x2];
dp[cur][x1][x2] = (dp[cur][x1][x2] + dp[cur ^ 1][x1][x2 + 1]) % mod;
if (x1 > 0) {
dp[cur][x1][x2] = (dp[cur][x1][x2] + dp[cur ^ 1][x1 - 1][x2]) % mod;
dp[cur][x1][x2] =
(dp[cur][x1][x2] + dp[cur ^ 1][x1 - 1][x2 + 1]) % mod;
}
}
}
long long ans = 0;
for (int i = 0; i < n; i++) ans = (ans + dp[cur][i][i]) % mod;
if ((n + m) % 2) {
for (int i = 0; i < n - 1; i++) ans = (ans + dp[cur][i][i + 1]) % mod;
}
printf("%I64d\n", ans);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long ii, r, need, pl, ans, d[200000], x, y, i, j, n;
int ch(int x, int y) {
if (x > 0 && x <= n && y > 0 && y <= n)
return 1;
else
return 0;
}
int main() {
cin >> n;
for ((i) = (0); i < (100001); ++i) d[i] = -1;
for ((j) = (0); j < (n); ++j) {
cin >> x >> y;
if (y == 0) {
r = 2;
d[1] = j;
d[x] = j;
for ((i) = (2); i < ((int)(sqrt(x)) + 1); ++i)
if (x % i == 0) {
r = r + 1;
d[i] = j;
if (x / i != i) {
++r;
d[x / i] = j;
}
}
cout << r << endl;
} else {
ans = 0;
for ((i) = (1); i < ((int)sqrt(x) + 1); ++i) {
if (x % i == 0) {
if (d[i] < j - y || d[i] > j)
++ans;
else if (d[i] == -1)
++ans;
d[i] = j;
ii = x / i;
if (ii != i) {
if (d[ii] < j - y || d[ii] > j)
++ans;
else if (d[ii] == -1)
++ans;
d[ii] = j;
}
}
}
cout << ans << endl;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, r;
cin >> m >> r;
double d0, d1, ans = 0;
d0 = 2 * r;
d1 = r * (2 + sqrt(2));
ans += m * d0 + (2 * m - 2) * d1;
for (int i = 1; i <= m; i++) {
double j = i - 2;
if (j > 0) ans += j * (j + 1) * r + j * 2 * sqrt(2) * r;
}
for (int i = 0; i < m; i++) {
double j = m - i - 2;
if (j > 0) ans += j * (j + 1) * r + j * 2 * sqrt(2) * r;
}
cout << fixed << setprecision(10) << ans / m / m << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N,M;
cin >> N >> M;
int a,b,c,d;
a=1; b=N;
for (int i=0;i<M;i++){
cin >> c >> d;
if (a<c) a=c;
if (d<b) b=d;
}
cout << max(0,b-a+1) << endl;
} | 0 |
#include <bits/stdc++.h>
int main() {
char a[102][102];
int n, m, i, j, d = 0;
scanf("%d %d", &n, &m);
for (i = 0; i < n; i++) {
getchar();
scanf("%s", a[i]);
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (j > 0 && a[i][j] != a[i][j - 1]) {
d++;
break;
}
if (i > 0 && a[i][j] == a[i - 1][j]) {
d++;
break;
}
}
if (d == 1) break;
}
if (d == 1)
printf("NO");
else
printf("YES");
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
void fn0(int n, string a, string b)
{
string s;
int j = 0, k = 0;
while (true) {
if (k >= 2 * n && j >= 2 * n) break;
if (k >= 2 * n) s += a[j++];
else if (j >= 2 * n) s += b[k++];
else if (a[j] == b[k]) s += a[j++], k++;
else if (a[j] == '1') s += a[j++];
else if (b[k] == '1') s += b[k++];
}
while (s.size() < 3 * n) s += '0';
cout<<s<<endl;
}
void fn1(int n, string a, string b)
{
string s;
int j = 0, k = 0;
while (true) {
if (k >= 2 * n && j >= 2 * n) break;
if (k >= 2 * n) s += a[j++];
else if (j >= 2 * n) s += b[k++];
else if (a[j] == b[k]) s += a[j++], k++;
else if (a[j] == '0') s += a[j++];
else if (b[k] == '0') s += b[k++];
}
while (s.size() < 3 * n) s += '0';
cout<<s<<endl;
}
int main() {
int tc;
cin>>tc;
while (tc--) {
string a, b, c;
int n;
cin>>n>>a>>b>>c;
if (count(a.begin(), a.end(), '0') >= n && count(b.begin(), b.end(), '0') >= n ) fn0(n, a, b);
else if (count(a.begin(), a.end(), '1') >= n && count(b.begin(), b.end(), '1') >= n ) fn1(n, a, b);
else if (count(a.begin(), a.end(), '0') >= n && count(c.begin(), c.end(), '0') >= n ) fn0(n, a, c);
else if (count(a.begin(), a.end(), '1') >= n && count(c.begin(), c.end(), '1') >= n ) fn1(n, a, c);
else if (count(c.begin(), c.end(), '0') >= n && count(b.begin(), b.end(), '0') >= n ) fn0(n, c, b);
else if (count(c.begin(), c.end(), '1') >= n && count(b.begin(), b.end(), '1') >= n ) fn1(n, c, b);
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
long long x, y;
long long gcd(long long a, long long b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
int main() {
cin >> x >> y;
if (gcd(x, y) != 1) {
puts("Impossible");
return 0;
}
vector<pair<long long, char> > ans;
while (x > 1 || y > 1) {
if (x > y) {
ans.push_back(make_pair(x / y, 'A'));
x %= y;
} else {
ans.push_back(make_pair(y / x, 'B'));
y %= x;
}
}
ans.back().first--;
for (auto it : ans) cout << it.first << it.second;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using l2 = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL);
int n, m, k;
cin >> n >> m >> k;
char s[n][m];
vector<int> ans(m, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> s[i][j];
if (s[i][j] == 'L') {
if (j >= i) {
ans[j - i]++;
}
} else if (s[i][j] == 'R') {
if (i + j < m) {
ans[i + j]++;
}
} else if (s[i][j] == 'U') {
if (((i) & (1)) == 0) {
ans[j]++;
}
}
}
}
for (auto i : ans) cout << i << ' ';
}
| 2 |
#include <iostream>
using namespace std;
int main(){
string x,y;
cin>>x>>y;
cout<<y<<x;
return 0;}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6;
const long long MOD = 1610612741;
const long long mod = 1e9 + 7;
const long long INF = 1e15 + 5040;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
vector<pair<int, string>> reg[MAXN];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s;
int x, sc;
cin >> s >> x >> sc;
x--;
reg[x].push_back(make_pair(sc, s));
}
for (int i = 0; i < m; i++)
sort(reg[i].begin(), reg[i].end()), reverse(reg[i].begin(), reg[i].end());
for (int i = 0; i < m; i++) {
if (reg[i].size() == 2) {
cout << reg[i][0].second << ' ' << reg[i][1].second << '\n';
continue;
}
if (reg[i][2].first != reg[i][1].first) {
cout << reg[i][0].second << ' ' << reg[i][1].second << '\n';
continue;
}
cout << "?\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, x, y;
cin >> n >> x >> y;
string s;
cin >> s;
long long zero = 0, one = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == '1')
one++;
else
zero++;
}
string ans = "";
for (long long i = 0; i < y; i++) ans += '0';
ans = '1' + ans;
for (long long i = 1; i <= (x - y - 1); i++) ans = '0' + ans;
long long cnt = 0;
long long j = 0;
for (long long i = n - x; i < n; i++) {
if (s[i] != ans[j]) cnt++;
j++;
}
cout << cnt << '\n';
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline bool check(int x) {
for (; x; x /= 10)
if (x % 10 != 4 && x % 10 != 7) return 0;
return 1;
}
const int maxn = 100100;
int head[maxn], nxt[maxn << 1], ver[maxn << 1], val[maxn << 1], tot;
inline void addedge(int a, int b, int c) {
nxt[++tot] = head[a];
ver[tot] = b;
val[tot] = c;
head[a] = tot;
nxt[++tot] = head[b];
ver[tot] = a;
val[tot] = c;
head[b] = tot;
}
int size[maxn], sum[maxn];
int n, m;
inline void getsize(int x, int fat) {
size[x] = 1;
sum[x] = 0;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (y == fat) continue;
getsize(y, x);
size[x] += size[y];
sum[x] += val[i] ? size[y] : sum[y];
}
}
long long ans = 0;
inline void getans(int x, int fat, int upv) {
ans += 1ll * (upv + sum[x]) * (upv + sum[x] - 1);
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (y == fat) continue;
getans(y, x, val[i] ? (size[1] - size[y]) : (upv + sum[x] - sum[y]));
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
addedge(a, b, check(c));
}
getsize(1, 0);
getans(1, 0, 0);
printf("%lld\n", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double n, m, a, b, s, t = 500.0;
int num;
cin >> n >> m >> num;
for (int i = 0; i < num; ++i) {
cin >> a >> b >> s;
if (sqrt((a - n) * (a - n) + (b - m) * (b - m)) / s < t)
t = sqrt((a - n) * (a - n) + (b - m) * (b - m)) / s;
}
printf("%.20f\n", t);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
double x[2005], y[2005];
double d[2005];
double eps = 1e-11;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
x[i] = 1.0 * a * c / (a * a + b * b);
y[i] = 1.0 * b * c / (a * a + b * b);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (abs(x[i] - x[j]) <= eps) {
if (abs(y[i] - y[j]) > eps)
d[j] = 214748364.0;
else
d[j] = -214748364.0;
} else
d[j] = 1.0 * (y[i] - y[j]) / (x[i] - x[j]);
}
sort(d + 1, d + i);
d[0] = -214748364.0;
int cnt = 1;
for (int j = 1; j < i; j++) {
if (d[j] == -214748364.0)
ans += (i - j - 1);
else if (abs(d[j] - d[j - 1]) > eps) {
ans += cnt * (cnt - 1) / 2;
cnt = 1;
} else
cnt++;
}
ans += cnt * (cnt - 1) / 2;
}
printf("%d\n", ans);
return 0;
}
| 4 |
#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;
map<vector<int>, int>perm_mp;
int main() {
{
vector<int>perms(4);
std::iota(perms.begin(), perms.end(), 0);
int k = 0;
do {
perm_mp[perms] = k++;
} while (next_permutation(perms.begin(), perms.end()));
}
int N, K; cin >> N >> K;
vector<vector<int>>fs = vector<vector<int>>(N, vector<int>(4));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < K; ++j) {
cin >> fs[i][j];
}
}
vector<vector<int>>priority_dates(24, vector<int>(N));
{
vector<int>perms(4);
std::iota(perms.begin(), perms.end(), 0);
int k = 0;
do {
vector<int>v(N);
std::iota(v.begin(), v.end(), 0);
for (int j = 3; j >= 0; --j) {
std::stable_sort(v.begin(), v.end(), [&](const int a, const int b) {
return fs[a][perms[j]] > fs[b][perms[j]];
});
}
for (int i = 0; i <v.size() ;++i){
priority_dates[k][v[i]] = i;
}
k++;
} while (next_permutation(perms.begin(), perms.end()));
}
int D; cin >> D;
vector<vector<int>>edges(N);
vector<int>come_nums(N);
for (int i = 0; i < D; ++i) {
int a, b; cin >> a >> b; a--; b--;
edges[a].push_back(b);
come_nums[b]++;
}
map<int, int>change_prioritys;
{
vector<int>es(4);
for (int j = 0; j < K; ++j) {
cin >> es[j];
es[j]--;
}
for (int j = K; j < 4; ++j)es[j] = j;
change_prioritys[0] = perm_mp[es];
}
{
int R; cin >> R;
for (int i = 0; i < R; ++i) {
int m; cin >> m;
vector<int>es(4);
for (int j = 0; j < K; ++j) {
cin >> es[j];
es[j]--;
}
for (int j = K; j < 4; ++j)es[j] = j;
change_prioritys[m] = perm_mp[es];
}
}
vector<priority_queue<int>>que;
vector<set<pair<int, int>>>sets(24);
for (int i = 0; i < N; ++i) {
if (!come_nums[i]) {
for (int j = 0; j < 24; ++j) {
sets[j].emplace(priority_dates[j][i], i);
}
}
}
int prio_id = -1;
for (int i = 0; i < N; ++i) {
if (change_prioritys.find(i) != change_prioritys.end())
prio_id = change_prioritys[i];
int task = sets[prio_id].begin()->second;
for(int j = 0; j < 24; ++j) {
sets[j].erase(make_pair(priority_dates[j][task], task));
}
for (auto e : edges[task]) {
come_nums[e]--;
if (come_nums[e] == 0) {
for (int j = 0; j < 24; ++j) {
sets[j].emplace(priority_dates[j][e], e);
}
}
}
cout << task+1 << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long double pie = 3.141592653589793238462643383279;
long long mod = 1e9 + 7;
long long dx[] = {-1, 1, 0, 0};
long long dy[] = {0, 0, 1, -1};
long long fact[2000005];
long long infact[2000005];
long long inv(long long a) {
long long y = mod - 2;
long long r = 1;
while (y != 0) {
if (y & 1) {
r *= a;
r %= mod;
}
y /= 2;
a *= a;
a %= mod;
}
return r;
}
long long po(long long a, long long y) {
long long r = 1;
while (y != 0) {
if (y & 1) {
r *= a;
r %= mod;
}
y /= 2;
a *= a;
a %= mod;
}
return r;
}
void facto() {
fact[0] = 1;
for (long long i = 1; i < (long long)2000005; ++i) {
fact[i] = i * fact[i - 1];
fact[i] %= mod;
}
for (long long i = 0; i < (long long)2000005; ++i) infact[i] = inv(fact[i]);
}
long long nck(long long n, long long k) {
if (k < 0 || k > n) return 0;
long long ret = fact[n];
ret *= infact[k];
ret %= mod;
ret *= infact[n - k];
ret %= mod;
return ret;
}
long long n, m;
vector<long long> adj[1000005];
bool vis[1000005];
bool vis2[1000005];
long long ans[1000005];
bool poss = 1;
long long dfs(long long c, long long p) {
bool lp = 1;
long long leaf = 0;
for (auto it : adj[c]) {
if (it != p) {
lp = 0;
leaf += dfs(it, c);
}
}
if (lp)
return 1;
else {
if (leaf < 3) poss = 0;
return 0;
}
}
void dfs2(long long s, long long val) {
if (vis2[s]) return;
vis2[s] = 1;
ans[s] = val;
for (auto it : adj[s]) dfs2(it, val);
}
long long rev(long long a) {
long long r = 0;
while (a != 0) {
r += a % 10;
r *= 10;
a /= 10;
}
return r / 10;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long tt = 1;
cin >> tt;
while (tt--) {
long long n;
cin >> n;
long long a[n];
for (long long i = 0; i < (long long)n; ++i) cin >> a[i];
vector<long long> ret;
vector<long long> un;
for (long long i = 0; i < (long long)n; ++i)
if (a[i] != i) {
un.push_back(i);
}
long long xp = un.size();
while (xp > 0) {
long long cp[n];
for (long long i = 0; i < (long long)n; ++i) cp[i] = a[i];
sort(cp, cp + n);
long long expec = 0;
for (long long i = 0; i < (long long)n; ++i) {
if (i != 0 && cp[i] == cp[i - 1]) continue;
if (cp[i] != expec) break;
++expec;
}
if (expec == n) {
ret.push_back(un[xp - 1]);
a[un[xp - 1]] = n;
} else {
ret.push_back(expec);
a[expec] = expec;
vector<long long> tmp;
bool fg = 0;
for (auto it : un)
if (it == expec) {
fg = 1;
continue;
} else
tmp.push_back(it);
assert(fg == 1);
un = tmp;
--xp;
}
}
cout << ret.size() << "\n";
for (auto it : ret) cout << it + 1 << " ";
cout << "\n";
}
return 0;
}
| 4 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct edge { int to, cap, rev; };
vector<edge>X[100009]; bool used[100009]; string S[200]; int H, W;
class Max_Flow {
public:
void add_edge(int u, int v, int w) {
X[u].push_back(edge{ v, w, (int)X[v].size() });
X[v].push_back(edge{ u, 0, (int)X[u].size() - 1 });
}
int dfs(int s, int t, int f) {
if (s == t) return f;
used[s] = true;
for (int i = 0; i < X[s].size(); i++) {
if (used[X[s][i].to] == true || X[s][i].cap == 0) continue;
int d = min(X[s][i].cap, dfs(X[s][i].to, t, min(f, X[s][i].cap)));
if (d <= 0) continue;
X[s][i].cap -= d;
X[X[s][i].to][X[s][i].rev].cap += d;
return d;
}
return 0;
}
int max_flow(int p, int q) {
int sum = 0;
while (true) {
for (int i = 0; i <= 220; i++) used[i] = false;
int D = dfs(p, q, (1 << 30));
if (D == 0) break;
sum += D;
if (D >= 205) return 1000000007;
}
return sum;
}
};
int main() {
cin >> H >> W;
for (int i = 0; i < H; i++) cin >> S[i];
int sx = 0, gx = 0; Max_Flow X;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (S[i][j] == 'S') { X.add_edge(i, j + H, 1000000007); X.add_edge(j + H, i, 1000000007); sx = i; }
if (S[i][j] == 'T') { X.add_edge(i, j + H, 1000000007); X.add_edge(j + H, i, 1000000007); gx = i; }
if (S[i][j] == 'o') { X.add_edge(i, j + H, 1); X.add_edge(j + H, i, 1); }
}
}
int ret = X.max_flow(sx, gx);
if (ret >= 100000) ret = -1;
cout << ret << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void amn(long long &x, long long y) { x = min(x, y); }
void amx(long long &x, long long y) { x = max(x, y); }
const long long INF = 1e18;
const long long N = 3e5 + 5;
long long n, q;
string s;
long long pref[N][2][2];
void solve() {
cin >> n >> q;
cin >> s;
s = "#" + s;
for (long long i = 1; i <= n; i++) {
for (long long j = 0; j < 2; j++) {
for (long long k = 0; k < 2; k++) {
pref[i][j][k] = 0;
}
}
}
for (long long i = 1; i <= n; i++) {
if (s[i] == '+') {
pref[i][i % 2][0]++;
} else {
pref[i][i % 2][1]++;
}
for (long long j = 0; j < 2; j++) {
for (long long k = 0; k < 2; k++) {
pref[i][j][k] += pref[i - 1][j][k];
}
}
}
for (long long i = 0; i < q; i++) {
long long l, r;
cin >> l >> r;
long long pos[2], neg[2];
for (long long j = 0; j < 2; j++) {
pos[j] = pref[r][j][0] - pref[l - 1][j][0];
neg[j] = pref[r][j][1] - pref[l - 1][j][1];
}
long long k = min(pos[0], pos[1]);
pos[0] -= k, pos[1] -= k;
k = min(neg[0], neg[1]);
neg[0] -= k, neg[1] -= k;
long long ps = pos[0] + pos[1], ne = neg[0] + neg[1];
if (ps == ne) {
if (ps + ne <= 2) {
cout << (ps + ne) << endl;
} else {
cout << 2 << endl;
}
} else {
cout << 1 << endl;
}
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long tt;
cin >> tt;
while (tt--) {
solve();
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> vet(n);
for (int &x : vet) cin >> x;
int l = 0, r = n - 1;
while (l < n - 1 && vet[l] < vet[l + 1]) l++;
while (r >= 1 && vet[r] < vet[r - 1]) r--;
l++;
r = n - r;
if (l % 2 || r % 2)
cout << "Alice";
else
cout << "Bob";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int n;
cin >> n;
vector<double> v;
while (n--) {
int x, y, vv;
cin >> x >> y >> vv;
long long d = (x - a) * (x - a) + (y - b) * (y - b);
double dd = sqrt(d * 1.000000);
dd /= (vv * 1.00);
v.push_back(dd);
}
sort(v.begin(), v.end());
printf("%.20lf\n", v[0]);
return 0;
}
| 1 |
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
int N;
double X[10009], Y[10009];
double getscore(double px, double py) {
double maxn = 0;
for (int i = 1; i <= N; i++) {
double dst = sqrt((px - X[i]) * (px - X[i]) + (py - Y[i]) * (py - Y[i]));
maxn = max(maxn, dst);
}
return maxn;
}
double getscore_Y(double px) {
double L = -1e9, R = 1e9, c1, c2, ans = 1e9;
for (int i = 0; i < 100; i++) {
c1 = (L + L + R) / 3.0;
c2 = (L + R + R) / 3.0;
double a1 = getscore(px, c1);
double a2 = getscore(px, c2);
ans = min(ans, min(a1, a2));
if (a1 > a2) L = c1;
else R = c2;
}
return ans;
}
int main() {
cin >> N;
for (int i = 1; i <= N; i++) cin >> X[i] >> Y[i];
double L = -1e9, R = 1e9, c1, c2, ans = 1e9;
for (int i = 0; i < 100; i++) {
c1 = (L + L + R) / 3.0;
c2 = (L + R + R) / 3.0;
double a1 = getscore_Y(c1);
double a2 = getscore_Y(c2);
ans = min(ans, min(a1, a2));
if (a1 > a2) L = c1;
else R = c2;
}
printf("%.12lf\n", ans);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int r = 0, c = 0;
string str;
cin >> str;
if (str.length() > 2) {
for (int i = 0; i < str.length() - 2; i++) {
if (str[i] == 'Q') {
for (int j = i + 1; j < str.length() - 1; j++) {
if (str[j] == 'A') r++;
if (r > 0) {
for (int k = j + 1; k < str.length(); k++) {
if (str[k] == 'Q') {
c++;
}
r = 0;
}
}
}
}
}
}
printf("%d", c);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
map<long long int, long long int> m;
vector<long long int> v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, k;
cin >> n >> k;
string a;
cin >> a;
long long int max = 0;
string ss = "abcdefghijklmnopqrstuvwxyz";
for (long long int i = 0; i < 26; i++) {
long long int ans = 0;
long long int count = 0;
for (long long int j = 0; j < n; j++) {
if (k == 1) {
if (a[j] == ss[i]) ans++;
} else {
if (a[j] == a[j + 1] && a[j] == ss[i])
count++;
else
count = 0;
if (count == k - 1) {
ans++;
count = 0;
j++;
}
}
}
if (ans > max) max = ans;
}
cout << max;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, flag = 0;
cin >> n;
map<string, int> z;
stack<string> e;
stack<string> c;
string s;
for (int i = n - 1; i >= 0; i--) {
cin >> s;
c.push(s);
z[c.top()] = 1;
}
while (!c.empty()) {
if (z[c.top()] == 1) {
cout << c.top() << endl;
z.erase(c.top());
c.pop();
} else {
c.pop();
}
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
cin>>s;
int n=s.size();
for(int i=n-2;i>=0;i--)
{
if((i+1)&1)continue;
int x=(i+1)/2;
if(s.substr(0,x)==s.substr(x,x))
{
printf("%d\n",x*2);
return 0;
}
}
printf("0\n");
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void init_ios() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
const int N = 5 * (int)1e5 + 10;
int n, res[N], ktory;
string s[N];
set<int> se, pom;
int main() {
init_ios();
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> s[i];
res[i] = s[i].size();
if (i > 1 && s[i - 1] > s[i]) se.insert(i - 1);
}
while (!se.empty()) {
int nr = *se.begin();
se.erase(se.begin());
int x2 = res[nr], x1 = 1, x3;
while (x2 - x1 > 1) {
x3 = (x1 + x2) / 2;
if (s[nr].substr(0, x3) > s[nr + 1].substr(0, res[nr + 1]))
x2 = x3;
else
x1 = x3;
}
if (s[nr].substr(0, x2) <= s[nr + 1].substr(0, res[nr + 1]))
res[nr] = x2;
else
res[nr] = x1;
if (nr > 1 && s[nr].substr(0, res[nr]) < s[nr - 1].substr(0, res[nr - 1]))
se.insert(nr - 1);
}
for (int i = 1; i <= n; ++i) cout << s[i].substr(0, res[i]) << "\n";
}
| 4 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <array>
#include <cassert>
// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef unsigned long long ull;
typedef tuple<int, int, int, int> T;
int main(){
int n;
while (cin >> n, n){
vector<int> t(n), c(n);
rep(i, n) cin >> t[i] >> c[i];
// time, t, rem, pos
priority_queue<T, vector<T>, greater<T>> q;
rep(i, n) q.push(MT(0, t[i], c[i], 0));
int lim[2] = {};
while (!q.empty()){
auto t = q.top();
q.pop();
int curtime, time, rem, pos;
tie(curtime, time, rem, pos) = t;
if (!rem) continue;
int npos = pos ^ 1;
int nxttime = max(curtime+time, lim[npos]);
if (!npos) --rem;
lim[npos] = max(lim[npos], nxttime);
q.push(MT(nxttime, time, rem, npos));
}
cout << *max_element(lim, lim + 2) << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, e, nn;
deque<int> d;
cin >> n;
e = n;
for (int i = 0; i < n; i++) cin >> x, d.push_back(x);
for (int i = 0; i < n - 1; i++)
if (d[i] > d[i + 1]) e = i + 1;
nn = n - e;
while (nn) {
d.push_front(d.back());
d.pop_back();
nn--;
}
if (is_sorted(d.begin(), d.end()))
cout << n - e;
else
cout << -1;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long b, d, s, mx, mi = (1LL << 60), r;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> b >> d >> s;
mx = max(b, mx);
mx = max(d, mx);
mx = max(s, mx);
mi = min(b, mi);
mi = min(d, mi);
mi = min(s, mi);
r += max((mx - 1) - b, 0LL);
r += max((mx - 1) - d, 0LL);
r += max((mx - 1) - s, 0LL);
cout << r;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d;
cin >> n >> d;
long long x[n + 1];
for (int i = 0; i < n; i++) cin >> x[i];
x[n] = 2e18;
long long i = 0, j = 0;
long long ans = 0;
while (i < n - 1) {
long long p = abs(x[j] - x[i]);
if (p <= d) {
j++;
} else {
ans += (j - i - 1) * (j - i - 2) / 2;
i++;
}
}
cout << ans << endl;
}
| 1 |
#include <stdio.h>
int root[1000][1000];
bool right[1000][1000];
int main()
{
int h, w, n, tmp, ax, ay;
while(true) {
scanf("%d%d%d", &h, &w, &n);
if(h == 0 && w == 0 && n == 0) break;
for(int y = 0; y < h; ++y) {
for(int x = 0; x < w; ++x) {
scanf("%d", &tmp);
right[x][y] = tmp;
root[x][y] = 0;
if(x == 0 && y == 0)
root[x][y] = n - 1;
if(x != 0) {
if(right[x - 1][y])
root[x][y] += (root[x - 1][y] + 1) / 2;
else
root[x][y] += (root[x - 1][y]) / 2;
}
if(y != 0) {
if(right[x][y - 1])
root[x][y] += (root[x][y - 1]) / 2;
else
root[x][y] += (root[x][y - 1] + 1) / 2;
}
}
}
ax = 0;
ay = 0;
while(ax < w && ay < h) {
if((right[ax][ay] + root[ax][ay]) % 2 == 1)
++ax;
else
++ay;
}
printf("%d %d\n", ay + 1, ax + 1);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
float H,W,N;
cin>>H>>W>>N;
cout<<min(ceil(N/H),ceil(N/W));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n = 0, mov[500500], sum[500500], dp[500500];
bool able[500500];
int main()
{
#define pud(x, y) min((x), abs((x) - (y)))
scanf("%d%d", &n, sum);
for(int i = 1; i <= n; i++)
{
scanf("%d", mov + i);
sum[i] = pud(sum[i - 1], mov[i]);
}
dp[n + 1] = 1;
for(int i = n; i >= 1; i--)
{
able[i] = (sum[i - 1] >= dp[i + 1]);
dp[i] = dp[i + 1] + mov[i];
if(pud(dp[i + 1], mov[i]) == dp[i + 1]) dp[i] = min(dp[i], dp[i + 1]);
if(mov[i] - dp[i + 1] >= 0 and pud(mov[i] - dp[i + 1], mov[i]) == dp[i + 1]) dp[i] = min(dp[i], mov[i] - dp[i + 1]);
}
int q = 0; scanf("%d", &q);
for(int i = 1; i <= q; i++)
{
int s = 0; scanf("%d", &s);
puts(able[s]? "YES": "NO");
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, i, k, a;
int main() {
cin >> n;
k = sqrt(2 * n);
if (k * (k + 1) / 2 == n) a = k;
if (k * (k + 1) / 2 < n) {
a = n - k * (k + 1) / 2;
} else if (k * (k + 1) / 2 > n) {
a = n - k * (k - 1) / 2;
}
cout << a;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long n;
cin >> n;
long long arr[n];
for (long long i = 0; i < n; i++) cin >> arr[i];
if (n == 1) {
cout << -1;
return 0;
}
sort(arr, arr + n);
long long d = arr[1] - arr[0];
if (n == 2 && (d % 2) == 0 && d != 0) {
cout << 3 << "\n";
cout << arr[0] - d << " " << arr[0] + d / 2 << " " << arr[n - 1] + d;
return 0;
}
long long ans;
long long re = 0;
if (n == 3 && (arr[2] - arr[1]) != d) {
if ((arr[1] - (arr[0] + arr[2] - arr[1])) == (arr[2] - arr[1])) {
cout << 1 << "\n";
cout << arr[0] + arr[2] - arr[1];
return 0;
}
}
if (n > 3 && ((arr[n - 1] - arr[n - 2]) != d &&
(arr[n - 2] - arr[n - 3]) == (arr[n - 1] - arr[n - 2])))
d = (arr[n - 1] - arr[n - 2]);
for (long long i = 0; i < n - 1; i++) {
if ((arr[i + 1] - arr[i]) != d &&
(((arr[i + 1] - arr[i]) != (2 * d)) ||
(arr[i + 1] - arr[i]) == (2 * d) && re == 1)) {
cout << 0;
return 0;
} else if ((arr[i + 1] - arr[i]) == (2 * d)) {
re = 1;
ans = arr[i] + d;
}
}
if (re == 1) {
cout << 1 << "\n";
cout << ans;
return 0;
}
cout << 2 << "\n";
cout << arr[0] - d << " " << arr[n - 1] + d;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
int ans = 0;
for (int i = 1; i <= t; i++) {
for (int j = i + 1; j <= t; j++) {
int k = j + 1;
if (i + j == k && k <= t) {
ans += i * j * k;
}
}
}
cout << ans << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
int m[5];
int w[5];
int x[5] = {50, 100, 150, 200, 250};
int h1, h2;
for (int32_t i = 0; i < 5; ++i) {
cin >> m[i];
}
for (int32_t i = 0; i < 5; ++i) {
cin >> w[i];
}
cin >> h1 >> h2;
int res = 0;
for (int32_t i = 0; i < 5; ++i) {
res += std::max(3 * x[i], x[i] * 10 - 2 * (i + 1) * m[i] - 50 * w[i]);
}
res += h1 * 100 - h2 * 50;
cout << res;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 105, M = 1e5 + 7, INF = 0x3f3f3f3f;
int ans = 0, head[N], to[M], nxt[M], val[M], fw[M], cnt = 1, S, T, d[N],
q[N * N * N], pre[N], du[N];
bool v[N];
inline void link(int a, int b, int l, int r, int c) {
to[++cnt] = b, nxt[cnt] = head[a], head[a] = cnt, fw[cnt] = r - l,
val[cnt] = c;
du[a] -= l;
to[++cnt] = a, nxt[cnt] = head[b], head[b] = cnt, fw[cnt] = 0, val[cnt] = -c;
du[b] += l;
}
bool SPFA() {
for (int i = 1; i <= T; ++i) d[i] = 0x3f3f3f3f;
d[S] = 0;
int h, t;
q[h = t = 0] = S;
while (h <= t) {
int x = q[h++];
v[x] = 0;
for (int i = head[x]; i; i = nxt[i])
if (fw[i]) {
int y = to[i];
if (d[y] > d[x] + val[i]) {
d[y] = d[x] + val[i], pre[y] = i;
if (!v[y]) q[++t] = y, v[y] = 1;
}
}
}
return d[T] != 0x3f3f3f3f;
}
int main() {
int n, m;
cin >> n >> m;
S = n + 1, T = n + 2;
while (m--) {
int a, b, c, f;
cin >> a >> b >> c >> f;
if (f <= c) {
link(b, a, 0, f, 1);
link(a, b, 0, c - f, 1);
link(a, b, 0, INF, 2);
} else {
ans += f - c;
link(b, a, 0, f - c, 0);
link(b, a, 0, c, 1);
link(a, b, 0, INF, 2);
}
link(a, b, f, f, 0);
}
link(n, 1, 0, INF, 0);
for (int i = 1; i <= n; ++i) {
if (du[i] < 0)
link(i, T, 0, -du[i], 0);
else
link(S, i, 0, du[i], 0);
}
while (SPFA()) {
int res = INF;
for (int i = T; i != S; i = to[pre[i] ^ 1]) res = min(res, fw[pre[i]]);
for (int i = T; i != S; i = to[pre[i] ^ 1])
ans += res * val[pre[i]], fw[pre[i]] -= res, fw[pre[i] ^ 1] += res;
}
cout << ans << endl;
return 0;
}
| 4 |
#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 "functional"
#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-8;
//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> K;
vector<vector<long long int>>dp(N + 1, vector<long long int>(N*N + 1));
dp[N][0] = 1;
vector<vector<long long int>>ans(N + 1, vector<long long int>(N*N + 1));
for (int i = N; i > 0; i--) {
for (int j = 0; j <= N * N; j++) {
for (int k = 0; k <= K; k++) {
int nxj = j;
long long int amari = 0;
if (k > i) {
amari = k+j;
}
else {
amari = (k + j) % i;
nxj += (k + j) / i;
}
if (nxj > N*N)continue;
dp[i - 1][nxj] += dp[i][j];
dp[i - 1][nxj] %= MOD;
ans[i - 1][nxj] += ans[i][j] + amari * dp[i][j];
ans[i - 1][nxj] %= MOD;
}
}
}
M = accumulate(ans.front().begin(), ans.front().end(), 0LL);
cout << M % MOD << endl;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s,t;
cin>>s>>t;
int ans=0;
for(int i=0;i<s.length();i++)
ans+=(s[i]!=t[i]);
cout<<ans;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long k, d, t, cycle, subTimes;
unsigned long long ans = 0, velocity, t2;
scanf("%lld%lld%lld", &k, &d, &t);
k *= 2;
d *= 2;
t *= 2;
subTimes = (k + d - 1) / d;
cycle = subTimes * d;
velocity = k + (cycle - k) / 2;
ans = t / velocity * cycle;
t2 = t - t / velocity * velocity;
if (t2 <= k) {
ans += t2;
} else {
ans += k + 2 * (t2 - k);
}
printf("%.1lf\n", ans / 2.);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a, b, p1, p2, g1, g2, now1, now2, k;
string s;
int main() {
cin >> a >> b;
p1 = 0;
p2 = 0;
cin >> s;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == 'U') ++p2;
if (s[i] == 'D') --p2;
if (s[i] == 'L') --p1;
if (s[i] == 'R') ++p1;
}
bool bb = false;
g1 = 0;
g2 = 0;
for (int i = 0; i < s.size(); ++i) {
now1 = a - g1;
now2 = b - g2;
if (p1 != 0) {
if (now1 % p1 == 0)
k = now1 / p1;
else
k = 0;
} else {
if (p2 != 0) {
if (now2 % p2 == 0)
k = now2 / p2;
else
k = 0;
} else
k = 1;
}
if (k >= 0 && k * p1 == now1 && k * p2 == now2) {
bb = true;
break;
}
if (s[i] == 'U') ++g2;
if (s[i] == 'D') --g2;
if (s[i] == 'L') --g1;
if (s[i] == 'R') ++g1;
}
if (bb)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
| 1 |
// 2019/11/20 Tazoe
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1000000001; // ありえない大きい値
int main()
{
int N, M;
cin >> N >> M;
int D[1001];
for(int i=1; i<=N; i++){
cin >> D[i];
}
int C[1001];
for(int j=1; j<=M; j++){
cin >> C[j];
}
int DP[1001][1001];
for(int i=1; i<=N; i++){
DP[0][i] = INF;
}
for(int j=0; j<=M; j++){
DP[j][0] = 0;
}
for(int j=1; j<=M; j++){
for(int i=1; i<=N; i++){
DP[j][i] = min(DP[j-1][i], DP[j-1][i-1]+D[i]*C[j]);
}
}
cout << DP[M][N] << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int M = 998244353;
const int N = 2e5 + 1;
int f[N];
int power(int a, int b)
{
int c = 1;
while (b)
{
if (b & 1)
c = (long long)c * a % M;
b >>= 1;
a = (long long)a * a % M;
}
return c;
}
int choose(int a, int b)
{
return (long long)f[a] * power((long long)f[b] * f[a - b] % M, M - 2) % M;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false), cin.tie(NULL);
f[0] = 1;
for (int i = 1; i < N; i++)
f[i] = (long long)f[i - 1] * i % M;
int t;
cin >> t;
while (t--)
{
int n;
string s;
cin >> n >> s;
int k = 0, z = 0;
for (int i = 0; i < s.length() - 1; i++)
if (s[i] == '1' && s[i + 1] == '1')
k++, i++;
for (char c : s)
if (c == '0')
z++;
cout << choose(k + z, z) << '\n';
}
} | 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, ans, p_cnt[300005], m_cnt[300005], sp = 0;
char in_s[300005];
int main() {
long long i, j, x, y, len;
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%s", in_s);
len = strlen(in_s);
x = 0;
for (j = 0; j < len; j++) x += ((in_s[j] - '(') ? -1 : 1);
if (x > 0) {
x = 0;
for (j = 0; j < len; j++) {
x += ((in_s[j] - '(') ? -1 : 1);
if (x < 0) break;
}
if (j == len) p_cnt[x]++;
} else if (x < 0) {
y = x;
x = 0;
for (j = 0; j < len; j++) {
x += ((in_s[j] - '(') ? -1 : 1);
if (y > x) break;
}
if (j == len) m_cnt[-x]++;
} else {
x = 0;
for (j = 0; j < len; j++) {
x += ((in_s[j] - '(') ? -1 : 1);
if (x < 0) break;
}
if (j == len) sp++;
}
}
ans = ((sp) * (sp));
for (i = 1; i <= 300005 - 3; i++) ans += p_cnt[i] * m_cnt[i];
printf("%lld\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
void pre() {}
void solve() {}
vector<int> a, v;
vector<long long> dp;
vector<pair<int, pair<int, int> > > p;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
pre();
dp.resize(300, 0);
for (int i = 0; i < (8); ++i) a.push_back(7 - i);
int n, m, k;
cin >> n >> m >> k;
v.resize(n, -1);
p.resize(2 * n, {0, {0, 0}});
for (int i = 0; i < (n); ++i)
cin >> p[2 * i].first >> p[2 * i + 1].first, p[2 * i + 1].first++;
for (int i = 0; i < (n); ++i)
p[2 * i].second = {0, i}, p[2 * i + 1].second = {-1, i};
sort(p.begin(), p.end());
long long l = 1;
set<pair<int, int> > s;
for (int i = 0; i < (2 * n); ++i) {
pair<int, pair<int, int> > c = p[i];
if (c.second.first == 0) {
assert((int)(a).size() != 0);
v[c.second.second] = a.back();
a.pop_back();
for (int m = 0; m < (256); ++m)
if (__builtin_popcount(m) % 2) {
dp[m] += c.first - l;
}
for (int m = 0; m < (256); ++m)
if (m & (1 << v[c.second.second])) {
dp[m] = dp[m ^ (1 << v[c.second.second])];
}
} else {
assert(v[c.second.second] != -1);
a.push_back(v[c.second.second]);
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
for (int m = 0; m < (256); ++m)
if (__builtin_popcount(m) % 2) {
dp[m] += c.first - l;
}
for (int m = 0; m < (256); ++m)
if (!(m & (1 << v[c.second.second]))) {
dp[m] = max(dp[m], dp[m ^ (1 << v[c.second.second])]);
}
for (int m = 0; m < (256); ++m)
if (m & (1 << v[c.second.second])) dp[m] = 0;
}
l = c.first;
}
cout << dp[0];
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
set<int> s;
int n, x, y;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> x;
int ans = 0;
while (n--) {
cin >> y;
s.insert(y);
}
for (int i = 0; i < x; ++i)
if (s.count(i) == 0) ans++;
if (s.count(x) == 1) ans++;
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
int main() {
int n;
scanf("%d", &n);
if (n % 2 == 0) {
puts("-1");
return 0;
}
for (int i = 0; i < n; i++) printf("%d ", i);
puts("");
for (int i = 0; i < n; i++) printf("%d ", i);
puts("");
for (int i = 0; i < n; i++) printf("%d ", (i + i) % n);
puts("");
return 0;
}
| 3 |
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
/*-------------------------------------------*/
int A, B;
int d[20][20];
int N, M;
int cost[101][101];
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> A >> B;
FOR(x, 1, A + 1) FOR(y, 1, B + 1) cin >> d[x][y];
N = 202;
M = 10401;
rep(i, 101) rep(j, 101) FOR(x, 1, A + 1) FOR(y, 1, B + 1)
chmax(cost[i][j], d[x][y] - i * x - j * y);
FOR(x, 1, A + 1) FOR(y, 1, B + 1) {
int mn = 1 << 30;
rep(i, 101) rep(j, 101) chmin(mn, x * i + y * j + cost[i][j]);
if(mn != d[x][y]) {
cout << "Impossible\n";
return 0;
}
}
cout << "Possible\n";
cout << N << " " << M << "\n";
rep(i, 100) cout << i + 1 << " " << i + 2 << " X\n";
rep(j, 100) cout << j + 102 << " " << j + 103 << " Y\n";
rep(i, 101) rep(j, 101) cout << i + 1 << " " << N - j << " " << cost[i][j]
<< "\n";
cout << "1 " << N << "\n";
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long n;
cin >> n;
string s;
cin >> s;
long long x = 0, y = 0;
for (long long i = 0; i < n; i++) {
if (s[i] == '0')
x++;
else
y++;
}
cout << n - min(x, y) * 2;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int mod = 1073741824;
char isprime[1000001];
memset(isprime, 'y', sizeof(isprime));
vector<int> primes;
for (int i = 2; i < 1000001; i++) {
if (isprime[i] == 'n') continue;
int j = 2 * i;
primes.push_back(i);
while (j < 1000001) {
isprime[j] = 'n';
j += i;
}
}
int ans = 0;
int a, b, c;
cin >> a >> b >> c;
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
for (int k = 1; k <= c; k++) {
int p = i * j * k;
if (p == 1) {
ans += 1;
if (ans >= mod) ans -= mod;
continue;
}
if (isprime[p] == 'y') {
ans += 2;
if (ans >= mod) ans -= mod;
continue;
}
int lmt = sqrt(p);
int tmp = 1;
for (int x = 0; primes[x] <= p && primes[x] <= lmt; x++) {
int cnt = 0;
while (p % primes[x] == 0) {
p /= primes[x];
cnt++;
}
tmp *= (cnt + 1);
}
if (p > 1) tmp *= 2;
ans += tmp;
if (ans > mod) ans -= mod;
}
}
}
cout << ans << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int a[11][111], b[11][111], c[11][111];
char str[111];
struct node {
int n, v;
node(int _n, int _v) { n = _n, v = _v; }
node() {}
friend bool operator<(node a, node b) { return a.v > b.v; }
} p[111];
int main() {
int n, m, cc;
while (~scanf("%d%d%d", &n, &m, &cc)) {
for (int i = 0; i < n; i++) {
scanf(" %*s", str);
for (int j = 0; j < m; j++) {
scanf("%d %d %d", &a[i][j], &b[i][j], &c[i][j]);
}
}
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
int t = 0, cnt = 0, q = cc;
for (int k = 0; k < m; k++) {
if (b[j][k] > a[i][k]) p[cnt++] = node(c[i][k], (b[j][k] - a[i][k]));
}
sort(p, p + cnt);
for (int k = 0; k < cnt; k++) {
if (q <= p[k].n) {
t += q * p[k].v;
break;
} else {
t += p[k].n * p[k].v;
q -= p[k].n;
}
}
res = max(res, t);
}
}
printf("%d\n", res);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, q, a[N];
namespace Seg {
int si[N << 2];
long double sm[N << 2], mt[N << 2], st[N << 2];
inline void pushup(int v) { sm[v] = sm[v << 1] + sm[v << 1 | 1]; }
inline void pushdown(int v) {
if (mt[v] != 1) {
sm[v] *= mt[v];
if (si[v] > 1)
mt[v << 1] *= mt[v], st[v << 1] *= mt[v], mt[v << 1 | 1] *= mt[v],
st[v << 1 | 1] *= mt[v];
mt[v] = 1;
}
if (st[v]) {
sm[v] += si[v] * st[v];
if (si[v] > 1) st[v << 1] += st[v], st[v << 1 | 1] += st[v];
st[v] = 0;
}
}
inline void init(int *a, int v = 1, int l = 1, int r = n) {
si[v] = r - l + 1;
mt[v] = 1;
st[v] = 0;
if (l == r) {
sm[v] = a[l];
return;
}
int mid = (l + r) >> 1;
init(a, v << 1, l, mid);
init(a, v << 1 | 1, mid + 1, r);
pushup(v);
}
inline void modi(int x, int y, long double z1, long double z2, int v = 1,
int l = 1, int r = n) {
pushdown(v);
if (l >= x && r <= y) {
mt[v] *= z1;
st[v] += z2;
pushdown(v);
return;
}
if (l > y || r < x) return;
int mid = (l + r) >> 1;
modi(x, y, z1, z2, v << 1, l, mid);
modi(x, y, z1, z2, v << 1 | 1, mid + 1, r);
pushup(v);
}
inline long double ask(int x, int y, int v = 1, int l = 1, int r = n) {
if (l > y || r < x) return 0;
pushdown(v);
if (l >= x && r <= y) return sm[v];
int mid = (l + r) >> 1;
return ask(x, y, v << 1, l, mid) + ask(x, y, v << 1 | 1, mid + 1, r);
}
} // namespace Seg
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
Seg::init(a);
for (int i = 1, l, r, x, y, op; i <= q; ++i) {
scanf("%d%d%d", &op, &x, &y);
if (op == 1) {
scanf("%d%d", &l, &r);
long double s1 = Seg::ask(x, y), s2 = Seg::ask(l, r);
Seg::modi(x, y, 1 - double(1) / (y - x + 1),
s2 / (r - l + 1) / (y - x + 1));
Seg::modi(l, r, 1 - double(1) / (r - l + 1),
s1 / (r - l + 1) / (y - x + 1));
} else
printf("%lf\n", (double)Seg::ask(x, y));
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5050;
int a[maxn];
struct node {
int l, r;
};
node fuck[maxn];
int have[maxn], tmp[maxn], prefix[maxn];
int main() {
int i, j, k, n, m;
while (scanf("%d%d", &n, &m) != EOF) {
memset(have, 0, sizeof(have));
for (i = 1; i <= m; i++) {
scanf("%d%d", &fuck[i].l, &fuck[i].r);
for (j = fuck[i].l; j <= fuck[i].r; j++) have[j]++;
}
prefix[0] = 0;
int ans = 0;
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++) tmp[j] = have[j];
for (j = fuck[i].l; j <= fuck[i].r; j++) tmp[j]--;
int res = 0;
for (j = 1; j <= n; j++)
if (tmp[j] == 0) res++;
for (j = 1; j <= n; j++)
if (tmp[j] == 1)
prefix[j] = 1;
else
prefix[j] = 0;
for (j = 1; j <= n; j++) prefix[j] += prefix[j - 1];
int minmove = n + 1;
for (j = 1; j <= m; j++)
if (i != j) {
minmove = min(minmove, prefix[fuck[j].r] - prefix[fuck[j].l - 1]);
}
res += minmove;
ans = max(ans, n - res);
}
printf("%d\n", ans);
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct Data {
int value;
bool nonzero;
bool use;
Data() {
value = 0;
nonzero = false;
use = false;
}
friend bool operator<(Data a, Data b) { return (a.value > b.value); }
};
vector<Data> Latin_set(10);
void process(string& str) {
int size = str.size();
int base = 1;
for (int i = size - 1; i >= 0; i--) {
Latin_set[str[i] - 'a'].value += base;
Latin_set[str[i] - 'a'].use = true;
base *= 10;
}
Latin_set[str[0] - 'a'].nonzero = true;
}
int main() {
int n;
bool used;
int sum, current;
sum = 0;
used = false;
current = 1;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
string str;
cin >> str;
process(str);
}
sort(Latin_set.begin(), Latin_set.end());
for (int i = 0; i < 10; i++) {
if (!Latin_set[i].use) continue;
if (!used && !Latin_set[i].nonzero)
used = true;
else {
sum += Latin_set[i].value * current;
current++;
}
}
printf("%d\n", sum);
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
bool check(vector<long long> a) {
for (long long i = 0; i < a.size(); i++) {
if (a[i] == i + 1) {
return false;
}
}
return true;
}
long long coun(long long n) {
if (n == 0) {
return 1;
}
vector<long long> a;
for (long long i = 1; i <= n; i++) {
a.push_back(i);
}
long long ans = 0;
while (next_permutation(a.begin(), a.end())) {
if (check(a)) {
ans++;
}
}
return ans;
}
signed main() {
long long i, j, n, k;
cin >> n >> k;
long long answer = 0;
for (i = n - k; i <= n; i++) {
long long ans = 1;
long long cur_k = n - i;
for (j = i + 1; j <= n; j++) {
ans *= j;
}
for (j = 1; j <= (n - i); j++) {
ans /= j;
}
ans *= coun(cur_k);
answer += ans;
}
cout << answer << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, a[55], sum = 0, p;
long long f[55][55], c[55][55];
double ans = 0.0;
void Build() {
for (int i = 0; i <= n; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
}
}
void Solve(int pos) {
for (int i = 0; i < 55; ++i)
for (int j = 0; j < 55; ++j) f[i][j] = 0;
f[0][0] = 1;
for (int i = 1; i <= n; ++i)
if (i != pos)
for (int j = p; j >= a[i]; --j)
for (int k = 1; k <= n - 1; ++k) f[j][k] += f[j - a[i]][k - 1];
for (int i = p; i >= max(0, p - a[pos] + 1); --i)
for (int j = 0; j <= n - 1; ++j)
ans += (double)(f[i][j] * j) / (c[n][j] * (n - j));
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
sum += a[i];
}
scanf("%d", &p);
if (sum <= p) {
printf("%d", n);
return 0;
}
Build();
for (int i = 1; i <= n; ++i) Solve(i);
printf("%f", ans);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1100;
const long long inf = (int)1e18;
const int infi = (int)1e9 + 1000;
int n, m, L, s, t;
vector<pair<int, int> > graf[MAXN];
vector<pair<pair<int, int>, int> > edgesN, edgesV;
void load() {
scanf("%d%d%d%d%d", &n, &m, &L, &s, &t);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (!c) c = -edgesV.size() - 1;
graf[a].push_back(pair<int, int>(b, c));
graf[b].push_back(pair<int, int>(a, c));
if (c > 0)
edgesN.push_back(make_pair(pair<int, int>(a, b), c));
else
edgesV.push_back(make_pair(pair<int, int>(a, b), infi));
}
}
long long len;
long long dist[MAXN];
long long dijkstra() {
set<pair<long long, int> > S;
for (int i = 0; i < n; i++) {
dist[i] = inf;
if (i == s) dist[i] = 0;
S.insert(pair<long long, int>(dist[i], i));
}
pair<long long, int> a;
while (!S.empty()) {
a = *S.begin();
S.erase(S.begin());
int b = a.second;
for (int i = 0; i < graf[b].size(); i++) {
pair<int, int> c = graf[b][i];
int sus = c.first;
int distsus = c.second;
if (distsus < 0) distsus = edgesV[-distsus - 1].second;
long long alt = dist[b] + distsus;
if (alt < dist[sus]) {
S.erase(pair<long long, int>(dist[sus], sus));
dist[sus] = alt;
S.insert(pair<long long, int>(dist[sus], sus));
}
}
}
return dist[t];
}
int v;
bool solve() {
v = edgesV.size();
if (dijkstra() < L) {
return false;
}
for (int i = 0; i < v; i++) edgesV[i].second = 1;
if (dijkstra() > L) {
return false;
}
int lo = 0, hi = v;
while (lo < hi) {
int mid = (lo + hi) / 2;
for (int i = 0; i < mid; i++) edgesV[i].second = 1;
for (int i = mid; i < v; i++) edgesV[i].second = infi;
if (dijkstra() <= L)
hi = mid;
else
lo = mid + 1;
}
for (int i = 0; i < lo; i++) edgesV[i].second = 1;
for (int i = lo; i < v; i++) edgesV[i].second = infi;
int lo_ = 1, hi_ = L;
while (lo_ < hi_) {
int mid = (lo_ + hi_) / 2;
if (lo - 1 < v && lo - 1 >= 0) edgesV[lo - 1].second = mid;
if (dijkstra() < L)
lo_ = mid + 1;
else
hi_ = mid;
}
if (lo - 1 < v && lo - 1 >= 0) edgesV[lo - 1].second = lo_;
return true;
}
void ispis() {
printf("YES\n");
for (int i = 0; i < m; i++) {
int a, b, c;
if (i < v) {
a = edgesV[i].first.first;
b = edgesV[i].first.second;
c = edgesV[i].second;
} else {
a = edgesN[i - v].first.first;
b = edgesN[i - v].first.second;
c = edgesN[i - v].second;
}
printf("%d %d %d\n", a, b, c);
}
}
int main() {
load();
if (solve())
ispis();
else
printf("NO\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
bool m[maxn];
int a[maxn];
int main() {
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
if (n == sum) {
cout << "YES" << endl;
return 0;
}
bool flag = false;
for (int t = 3; t <= n; t++)
if (n % t == 0) {
memset(m, 0, sizeof(m));
for (int i = 1; i <= n / t; i++) {
if (a[i] == 1) {
for (int j = i; j <= n; j += n / t)
if (a[i] + a[j] != 2) m[i] = 1;
} else
m[i] = 1;
}
int cnt = 0;
for (int i = 1; i <= n / t; i++)
if (!m[i]) cnt++;
if (cnt) flag = true;
}
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void write(int x) {
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
inline void writeln(int x) {
write(x);
putchar('\n');
}
const int N = 1000005;
struct node {
int l, r;
} b[N];
struct que {
int l, r, id;
} c[N];
int now;
int Ans[N];
int n, k, q;
int Mx[N * 4], pos[N * 4];
int Mxdis[N];
int stkc[N], topc;
int Pos, ans;
inline bool cmp2(node a, node b) {
return a.r < b.r || (a.r == b.r && a.l > b.l);
}
inline bool cmp3(que a, que b) {
return a.r < b.r || (a.r == b.r && a.l > b.l);
}
inline void change(int x, int y) {
int l = 1, r = n, num = 1;
while (1) {
int mid = (l + r) >> 1;
if (Mx[num] < y) pos[num] = x, Mx[num] = y;
if (l == r) break;
if (x <= mid)
r = mid, num = (num << 1);
else
l = mid + 1, num = (num << 1 | 1);
}
}
inline void query(int num, int l, int r, int x, int y) {
if (x <= l && r <= y) {
if (Mx[num] > ans) {
Pos = pos[num];
ans = Mx[num];
}
return;
}
int mid = (l + r) >> 1;
if (x <= mid) query(num << 1, l, mid, x, y);
if (y > mid) query(num << 1 | 1, mid + 1, r, x, y);
}
int main() {
n = read();
k = read();
for (int i = 1; i <= k; ++i) b[i].l = read(), b[i].r = read();
q = read();
for (int i = 1; i <= q; ++i) c[i].l = read(), c[i].r = read(), c[i].id = i;
sort(b + 1, b + k + 1, cmp2);
sort(c + 1, c + q + 1, cmp3);
now = 1;
for (int i = 1; i <= n; ++i) Mxdis[i] = i;
for (int i = 1; i <= q; ++i) {
while (now <= k && b[now].r <= c[i].r) {
if (Mxdis[b[now].l] < b[now].r) {
change(b[now].l, b[now].r);
Mxdis[b[now].l] = b[now].r;
}
++now;
}
topc = 0;
stkc[++topc] = c[i].l;
int last = c[i].l;
while (1) {
int x = stkc[topc];
Pos = 0, ans = 0;
query(1, 1, n, last, Mxdis[x]);
if (ans > Mxdis[x])
stkc[++topc] = Pos, last = Mxdis[x];
else
break;
}
int mm = Mxdis[stkc[topc--]];
while (topc > 0) {
change(stkc[topc], mm);
Mxdis[stkc[topc--]] = mm;
}
Ans[c[i].id] = mm;
}
for (int i = 1; i <= q; ++i) writeln(Ans[i]);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using ul = std::uint32_t;
using ull = std::uint64_t;
using li = std::int32_t;
using ll = std::int64_t;
ul n, m;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cin >> n >> m;
if (n > m) {
std::swap(n, m);
}
if (n == 1) {
std::cout << m / 6 * 6 + (m % 6 > 3 ? ul(2 * (m % 6 - 3)) : ul(0))
<< std::endl;
return 0;
}
if (n == 2) {
if (m == 2) {
std::cout << 0 << std::endl;
return 0;
}
if (m == 3) {
std::cout << 4 << std::endl;
return 0;
}
if (m == 7) {
std::cout << 12 << std::endl;
return 0;
}
std::cout << ull(m) * n << std::endl;
return 0;
}
if (n & 1 & m) {
std::cout << ull(m) * n - 1 << std::endl;
return 0;
}
std::cout << ull(m) * n << std::endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, x;
cin >> n >> m;
map<int, int> mp;
for (int i = 0; i < n; i++) {
cin >> x;
x %= m;
mp[x]++;
}
int res = mp[0];
if (res % 2) res--;
for (int i = 1; i < m; i++) {
if (i != m - i) {
res += min(mp[i], mp[m - i]) * 2;
mp[i] = 0;
;
mp[m - i] = 0;
}
if (i == m - i) {
x = mp[i];
if (x % 2) x--;
res += x;
}
}
cout << res << endl;
}
| 2 |
#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const long long INF = 0x7fffffffffffffff;
const double eps = 1e-10;
unsigned int seed = 19260817;
const unsigned int _RAND_MAX_ = 4294967295u;
__inline__ __attribute__((always_inline)) unsigned int Rand() {
return seed = seed * 998244353u + 1000000007u;
}
template <typename _Tp>
_Tp gcd(const _Tp &a, const _Tp &b) {
return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) {
return a > 0 ? a : -a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) {
return a < b ? b : a;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) {
return a < b ? a : b;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) {
(a < b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) {
(a > b) && (a = b);
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) bool _cmp(const _Tp &a,
const _Tp &b) {
return abs(a - b) <= eps;
}
template <typename _Tp>
__inline__ __attribute__((always_inline)) void read(_Tp &x) {
register char ch(getchar());
bool f(false);
while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar();
x = ch & 15, ch = getchar();
while (ch >= 48 && ch <= 57)
x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
if (f) x = -x;
}
template <typename _Tp, typename... Args>
__inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) {
read(t);
read(args...);
}
__inline__ __attribute__((always_inline)) int read_str(char *s) {
register char ch(getchar());
while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar();
register char *tar = s;
*tar = ch, ch = getchar();
while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
*(++tar) = ch, ch = getchar();
return tar - s + 1;
}
const int N = 500005;
struct edge {
int v, nxt;
} c[N << 1];
int front[N], cnt;
__inline__ __attribute__((always_inline)) void add(int u, int v) {
c[++cnt] = (edge){v, front[u]}, front[u] = cnt;
}
std::vector<int> v[N];
int dfn[N], low[N], id;
int st[N], top, node_cnt;
void tarjan(int x, int fa) {
dfn[x] = low[x] = ++id;
st[++top] = x;
for (int i = front[x]; i; i = c[i].nxt) {
if (c[i].v != fa) {
if (!dfn[c[i].v]) {
tarjan(c[i].v, x);
chmin(low[x], low[c[i].v]);
if (low[c[i].v] >= dfn[x]) {
++node_cnt;
int tmp = 0;
while (tmp != c[i].v) {
tmp = st[top];
--top;
v[node_cnt].push_back(tmp);
v[tmp].push_back(node_cnt);
}
v[node_cnt].push_back(x);
v[x].push_back(node_cnt);
}
} else {
chmin(low[x], dfn[c[i].v]);
}
}
}
}
int siz[N];
int _n;
int Fa[N];
void dfs(int x, int fa) {
siz[x] = (x <= _n);
Fa[x] = fa;
for (auto it : v[x]) {
if (it != fa) {
dfs(it, x);
siz[x] += siz[it];
}
}
}
void MAIN() {
int n, m, a, b;
read(n, m, a, b);
_n = n;
memset(dfn, 0, 8 * (n + 3));
memset(low, 0, 8 * (n + 3));
memset(siz, 0, 8 * (n + 3));
id = 0;
top = 0;
node_cnt = n;
cnt = 0;
memset(front, 0, 8 * (n + 3));
for (int i = 1; i <= n << 1; ++i) v[i].clear();
int x, y;
for (int i = 1; i <= m; ++i) {
read(x, y);
add(x, y), add(y, x);
}
for (int i = 1; i <= n; ++i) {
if (!dfn[i]) {
top = 0;
tarjan(i, 0);
}
}
dfs(a, 0);
int cur = b;
while (Fa[cur] != a) cur = Fa[cur];
printf("%lld\n", 1ll * (siz[b] - 1) * (n - siz[cur] - 1));
}
int main() {
int _;
read(_);
while (_--) MAIN();
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
double f[1001][1001];
int a[1001];
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) f[i][j] = (a[i] > a[j]);
while (m--) {
int u, v;
cin >> u >> v;
u--;
v--;
f[u][v] = f[v][u] = 0.5;
for (int i = 0; i < n; i++) {
if (i != u && i != v) {
f[u][i] = f[v][i] = (f[u][i] + f[v][i]) * 0.5;
f[i][u] = f[i][v] = (f[i][u] + f[i][v]) * 0.5;
}
}
}
double ans = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) ans += f[i][j];
printf("%.8lf\n", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long double ep = 0.000001;
const long double PI = 3.14159265359;
const int mod = 998244353;
const int MAX = 300009;
long long n, m, l[4], x, y, t, vis[MAX], po[2 * MAX], color[MAX], visited[MAX],
po3[2 * MAX];
vector<long long> d[MAX];
bool check(long long v) {
if (!visited[v]) {
visited[v] = 1;
for (auto u : d[v]) {
if (visited[u] == false) {
color[u] = 1 - color[v];
if (!check(u)) return false;
} else if (color[u] == color[v])
return false;
}
}
return true;
}
long long bfs(long long s) {
if (vis[s]) return 0;
queue<pair<long long, bool>> q;
q.push({s, 0});
vis[s] = 1;
while (!q.empty()) {
pair<long long, bool> vll = q.front();
long long v = vll.first;
bool le = vll.second;
q.pop();
l[le]++;
for (auto u : d[v]) {
if (!vis[u]) {
vis[u] = 1;
q.push({u, 1 - le});
}
}
}
if (d[s].size() == 0)
return 3;
else
return 0;
}
int main() {
po[0] = 1;
po3[0] = 1;
for (int i = 1; i <= 2 * MAX - 5; i++)
po[i] = ((po[i - 1] * 2) % mod + mod) % mod;
for (int i = 1; i <= 2 * MAX - 5; i++)
po3[i] = ((po3[i - 1] * 3) % mod + mod) % mod;
cin >> t;
while (t--) {
long long ans = 1;
bool c = 1;
cin >> n >> m;
if (m == 0) {
cout << po3[n];
cout << endl;
continue;
}
for (int i = 0; i < n + 5; i++) d[i].clear();
for (int i = 0; i < n + 5; i++) vis[i] = 0;
for (int i = 0; i < n + 5; i++) visited[i] = 0;
for (int i = 0; i < n + 5; i++) color[i] = 0;
for (int i = 0; i < m; i++) {
cin >> x >> y;
x--;
y--;
d[x].push_back(y);
d[y].push_back(x);
}
for (int i = 0; i < n; i++) {
c = check(i) && c;
l[0] = l[1] = 0;
long long t = bfs(i);
if (l[0] && l[1])
ans *= (po[l[0]] + po[l[1]]), ans = (ans % mod + mod) % mod;
if ((l[1] == 1 && l[0] == 0) || l[0] == 1 && l[1] == 0)
ans *= 3, ans = (ans % mod + mod) % mod;
}
if (c)
cout << ans;
else
cout << 0;
cout << endl;
}
}
| 4 |
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
using namespace std;
#define all(c) c.begin(),c.end()
#define rall(c) c.rbegin(),c.rend()
#define mp(a,b) make_pair((a),(b))
#define eq ==
typedef long long ll;
typedef complex<double> point;
typedef pair<int,int> pii;
const double EPS = 1e-9;
const int INF = 100000000;
double kyo(pair<int,int> a,pair<int,int> b){
return sqrt((a.first-b.first)*(a.first-b.first) + (a.second-b.second)*(a.second-b.second));
}
bool solve(int ind,double zikan,set<int> &rest,const vector<vector<double> > &kyori,const vector<double> &mao){
if(rest.size() == 0) return true;
bool allok = true;
for(set<int>::iterator it = rest.begin();it!=rest.end();++it){
allok = allok and (zikan + kyori[ind][*it] < mao[*it]-EPS);
}
if(allok){
bool any = false;
for(set<int>::iterator it = rest.begin();it!=rest.end();++it){
rest.erase(*it);
any = any or solve(*it,zikan+kyori[ind][*it],rest,kyori,mao);
rest.insert(*it);
}
return any;
}else{
return false;
}
}
template<class T>
void cerr_vec(vector<T> vec){
for(int i=0;i<vec.size();i++){
cerr << vec[i] << " ";
}
cerr << endl;
}
int main(){
while(true){
int n,hx,hy,dx,dy;
cin >> n >> hx >> hy >> dx >> dy;
if(n == 0) break;
vector<pair<int,int> > chi(n);
pair<int,int> m = make_pair(dx,dy);
pair<int,int> y = make_pair(hx,hy);
vector<double> yusya(n);
vector<double> mao(n);
vector<vector<double> > kyori(n,vector<double>(n));
set<int> rest;
for(int i=0;i<n;i++){
cin >> chi[i].first >> chi[i].second;
mao[i] = kyo(chi[i],m);
yusya[i] = kyo(chi[i],y);
rest.insert(i);
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
kyori[i][j] = kyo(chi[i],chi[j]);
}
}
bool any = false;
for(int i=0;i<n;i++){
rest.erase(i);
if(yusya[i] < mao[i]-EPS){
any = any or solve(i,yusya[i],rest,kyori,mao);
}
rest.insert(i);
}
if(any){
cout << "YES" << endl;
}else{
cout << "NO" << endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MOD = 1000000007;
int n;
ll ar[200005];
ll ans = 1e18;
set<ll> mem;
void tes(ll x) {
if (x == 1) return;
if (mem.count(x)) return;
mem.insert(x);
ll tp = 0;
for (int i = (0); i < (n); i++) {
if (ar[i] <= x)
tp += x - ar[i];
else
tp += min(ar[i] % x, x - (ar[i] % x));
}
ans = min(ans, tp);
}
void rn(ll x) {
if (!x) return;
for (ll i = 2; i * i <= x; i++) {
if (x % i) continue;
tes(i);
while (x % i == 0) x /= i;
}
tes(x);
}
mt19937 rng(108616);
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = (0); i < (n); i++) cin >> ar[i];
tes(2);
vector<int> order(n);
for (int i = (0); i < (n); i++) order[i] = i;
shuffle(order.begin(), order.end(), rng);
for (int lp = (0); lp < (min(30, n)); lp++) {
rn(ar[order[lp]] - 1);
rn(ar[order[lp]]);
rn(ar[order[lp]] + 1);
}
cout << ans << endl;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mo = 998244853;
const long long INF = 0x3f3f3f3f;
template <typename _T>
void read(_T &x) {
_T f = 1;
x = 0;
char s = getchar();
while (s > '9' || s < '0') {
if (s == '-') f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + (s ^ 48);
s = getchar();
}
x *= f;
}
long long n, m, fac[4005], inv[4005], f[4005], ans, g[4005];
void init() {
fac[0] = inv[0] = fac[1] = inv[1] = f[1] = 1;
for (long long i = 2; i <= 4e3; i++) {
fac[i] = 1ll * fac[i - 1] * i % mo;
f[i] = 1ll * (mo - mo / i) * f[mo % i] % mo;
inv[i] = 1ll * inv[i - 1] * f[i] % mo;
}
}
long long C(long long x, long long y) {
if (x < 0 || y < 0 || x < y) return 0;
return 1ll * fac[x] * inv[y] % mo * inv[x - y] % mo;
}
signed main() {
init();
read(n);
read(m);
long long d = n - m < 1 ? 1 : n - m, sum;
for (long long i = d; i <= n; i++) g[i] = C(n + m, n - i);
for (long long i = d; i <= n; i++) (ans += g[i]) %= mo;
printf("%lld\n", (ans + (d - 1LL + mo) * g[d] % mo) % mo);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using std::max;
using std::min;
using std::vector;
const int MAXN = 1e6 + 20;
namespace MyIO {
const int MAXB = 1 << 10;
char gbuf[MAXB], *ps = gbuf, *pt = gbuf;
char pbuf[MAXB + 1], *pp = pbuf;
struct Flusher {
~Flusher() { fwrite(pbuf, 1, pp - pbuf, stdout); }
} flusher;
inline char Getchar() {
if (ps == pt) {
ps = gbuf;
pt = gbuf + fread(gbuf, 1, MAXB, stdin);
}
return (ps == pt) ? EOF : *ps++;
}
inline void Putchar(const char &ch) {
if (pp == pbuf + MAXB) {
pp = pbuf;
fwrite(pbuf, 1, MAXB, stdout);
}
*pp++ = ch;
}
} // namespace MyIO
void write(const int &x) {
if (x / 10) write(x / 10);
MyIO::Putchar('0' + x % 10);
}
struct string {
char data[MAXN];
int len;
int size() const { return len; }
void reverse() { std::reverse(data, data + len); }
char &operator[](const int &index) { return data[index]; }
char operator[](const int &index) const { return data[index]; }
};
void read_string(string &s) {
char ch = MyIO::Getchar();
while (ch < 32 || ch > 126) ch = MyIO::Getchar();
s.len = 0;
while (ch >= 32 && ch <= 126) s[s.len++] = ch, ch = MyIO::Getchar();
}
void Puts(const char *s) {
for (const char *p = s;
((*p >= '0' && *p <= '9') || (*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') || (*p == ' ') || (*p == '\n') || (*p == '-'));
++p)
MyIO::Putchar(*p);
MyIO::Putchar('\n');
}
vector<int> kmp(const string &s1, const string &s2,
const vector<int> &pi_s2_init = vector<int>(1, -1)) {
const int n1 = s1.size(), n2 = s2.size();
vector<int> pi(n1, 0);
const vector<int> &pi_s2 = (pi_s2_init[0] != -1) ? pi_s2_init : pi;
pi[0] = (&s1 == &s2) ? 0 : (s1[0] == s2[0]);
for (int i = 1; i < n1; ++i) {
int j = pi[i - 1];
if (j == n2) j = pi_s2[0 + j - 1];
while (j && s2[0 + j - 1 + 1] != s1[i]) j = pi_s2[0 + j - 1];
pi[i] = j + (s2[0 + j - 1 + 1] == s1[i]);
}
return pi;
}
vector<int> z_algorithm(const string &s1, const string &s2,
const vector<int> &z_s2_init = vector<int>(1, -1)) {
const int n1 = s1.size(), n2 = s2.size();
vector<int> z(n1, 0);
const vector<int> &z_s2 = (z_s2_init[0] != -1) ? z_s2_init : z;
while (z[0] < n2 && s1[z[0]] == s2[z[0]]) ++z[0];
for (int i = 1, l = 0, r = 0; i < n1; ++i) {
if (i < r) z[i] = min(r - i, z_s2[i - l]);
while (z[i] < n2 && i + z[i] < n1 && s1[i + z[i]] == s2[z[i]]) ++z[i];
if (r < i + z[i]) l = i, r = i + z[i];
}
return z;
}
int main() {
string A, B;
read_string(A), read_string(B);
auto Ans = [&](const int &i, const int &j) {
if (i == -1)
Puts("-1 -1");
else
write(i), MyIO::Putchar(' '), write(j);
return 0;
};
if (A.size() != B.size()) return Ans(-1, -1);
const int n = A.size();
string RA = A;
RA.reverse();
const vector<int> z_A_B = z_algorithm(A, B, z_algorithm(B, B)),
pi_B_RA = kmp(B, RA, kmp(RA, RA));
int init_i = 0;
while (init_i < n && A[init_i] == B[n - 1 - init_i]) ++init_i;
for (int i = min(n - 2, init_i - 1); i >= 0; --i) {
const int len_pre = i + 1;
const int j_min = (n - 1 - len_pre) - pi_B_RA[n - 1 - len_pre] + 1;
const int j_max = 0 + z_A_B[i + 1];
if (j_max >= j_min) return Ans(i, i + ((j_min - 1) - 0 + 1) + 1);
}
return Ans(-1, -1);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 10010, oo = 100000000, T = 1000;
int i, j, k, n, m, x, now, last, fg, Time;
int a[N], b[N];
int main() {
now = -oo;
last = 12211221;
while (now <= oo) {
printf("0 %d %d\n", now, now);
fflush(stdout);
scanf("%d", &x);
if (!x) {
if (now > -oo) {
printf("0 %d %d\n", last, now);
fflush(stdout);
scanf("%d", &x);
if (x == 0) b[++m] = now;
printf("0 %d %d\n", now, last);
fflush(stdout);
scanf("%d", &x);
if (x == 0) a[++n] = now;
} else {
fg = 1;
}
if (now + T <= oo) {
printf("0 %d %d\n", now + T, now + T);
fflush(stdout);
scanf("%d", &x);
if (x != T)
now++;
else
now += T;
} else
now++;
} else
now += x;
}
if (fg) {
now = -oo;
printf("0 %d %d\n", last, now);
fflush(stdout);
scanf("%d", &x);
if (x == 0) b[++m] = now;
printf("0 %d %d\n", now, last);
fflush(stdout);
scanf("%d", &x);
if (x == 0) a[++n] = now;
}
printf("1 %d %d\n", n, m);
for (i = 1; i <= n; i++) printf("%d ", a[i]);
for (i = 1; i <= m; i++) printf("%d ", b[i]);
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC target(sse, sse2, sse3, ssse3, sse4, popcnt, tune = native)
#pragma GCC optimize( \
"-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2", \
3)
using namespace std;
const long long MAXN = 3e5 + 47;
vector<long long> graf[MAXN];
vector<pair<long long, long long> > event[MAXN];
long long n, m;
void read() {
cin >> n;
for (long long i = 0; i < n - 1; i++) {
long long p1, p2;
cin >> p1 >> p2;
--p1, --p2;
graf[p1].push_back(p2);
graf[p2].push_back(p1);
}
cin >> m;
for (long long i = 0; i < m; i++) {
long long p1, d, add;
cin >> p1 >> d >> add;
--p1;
event[p1].push_back(make_pair(d, add));
}
}
long long ans[MAXN];
long long update[MAXN];
void dfs(long long p1, long long pr, long long fat, long long h) {
for (long long i = 0; i < (long long)(event[p1].size()); i++) {
fat += event[p1][i].second;
long long kek = min(MAXN - 1, h + 1 + event[p1][i].first);
update[kek] -= event[p1][i].second;
}
fat += update[h];
ans[p1] = fat;
for (long long i = 0; i < (long long)(graf[p1].size()); i++) {
long long p2 = graf[p1][i];
if (p2 == pr) continue;
dfs(p2, p1, fat, h + 1);
}
for (long long i = 0; i < (long long)(event[p1].size()); i++) {
long long kek = min(MAXN - 1, h + 1 + event[p1][i].first);
update[kek] += event[p1][i].second;
}
}
void solve() {
memset(ans, 0, sizeof(ans));
dfs(0, -1, 0, 0);
for (long long i = 0; i < n; i++) cout << ans[i] << " ";
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
read();
solve();
}
| 5 |
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = 1e9;
int solve(vector< vector< pair<int, int> > > &G, int C){
int n = G.size();
vector< vector<int> > D(n, vector<int>(n, INF));
priority_queue< pair<int, int> > wait;
wait.emplace(0, 0);
D[0][0] = 0;
while(!wait.empty()){
int v = wait.top().second/n, c = wait.top().second%n, d = -wait.top().first;
wait.pop();
if(D[v][c] < d || v == n-1) continue;
for(int i = 0; i < G[v].size(); ++i){
int d_ = d + G[v][i].first, v_ = G[v][i].second;
if(d_ <= C && d_ < D[v_][c]){
D[v_][c] = d_;
wait.emplace(-d_, v_*n + c);
}
if(c < n - 1 && d < D[v_][c+1]){
D[v_][c+1] = d;
wait.emplace(-d, v_*n + c+1);
}
}
}
int ans = n;
for(int i = 0; i < n; ++i){
if(D[n-1][i] <= C){
ans = i;
break;
}
}
return ans;
}
int main(){
int n, m, c;
while(cin >> n >> m >> c, n){
int f, t, d;
vector< vector< pair<int,int> > > G(n);
for(int i = 0; i < m; ++i){
cin >> f >> t >> d;
--f;--t;
G[f].emplace_back(d, t);
}
cout << solve(G, c) << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
bool f(long long a, long long b) {
if (!a) return 0;
return !(f(b % a, a) && (b / a % (a + 1) % 2));
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long a, b;
scanf("%I64d%I64d", &a, &b);
if (a > b) a ^= b, b ^= a, a ^= b;
puts(f(a, b) ? "First" : "Second");
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> mapa;
vector<int> ls;
int tot, ne;
int a[300100], b[300100], c[300100];
int qtd[2 * 300100];
void func(int p, int v) {
qtd[p] += v;
if (v > 0 && qtd[p] == 0) ne++;
if (v < 0 && qtd[p] == -1) ne--;
}
int main() {
int n, m, p;
scanf("%d %d %d", &n, &m, &p);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
int val = mapa.size();
if (mapa.find(a[i]) != mapa.end())
a[i] = mapa[a[i]];
else
a[i] = mapa[a[i]] = val;
}
tot = 0;
for (int i = 0; i < m; i++) {
scanf("%d", &b[i]);
int val = mapa.size();
if (mapa.find(b[i]) != mapa.end())
b[i] = mapa[b[i]];
else
b[i] = mapa[b[i]] = val;
if (qtd[b[i]] == 0) tot++;
qtd[b[i]]--;
}
for (int i = 0; i < p; i++) {
int cnt = 0;
for (int j = i; j < n; j += p) {
c[cnt++] = a[j];
}
ne = 0;
for (int j = 0; j < cnt; j++) {
if (j >= m - 1) {
func(c[j], +1);
if (ne == tot) ls.push_back(i + (j - m + 1) * p);
func(c[j - m + 1], -1);
} else
func(c[j], +1);
}
for (int j = max(0, cnt - m + 1); j < cnt; j++) func(c[j], -1);
}
sort(ls.begin(), ls.end());
printf("%d\n", ls.size());
for (int i = 0; i < ls.size(); i++) {
if (i) printf(" ");
printf("%d", ls[i] + 1);
}
printf("\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int totalCups = 0;
int totalMedals = 0;
for (int i = 0; i < 3; i++) {
int x;
cin >> x;
totalCups += x;
}
for (int i = 0; i < 3; i++) {
int x;
cin >> x;
totalMedals += x;
}
int n;
cin >> n;
int a = (totalCups + 4) / 5;
int b = (totalMedals + 9) / 10;
if (a + b > n) {
cout << "NO" << endl;
} else {
cout << "YES" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k;
string s;
cin >> s >> k;
vector<string> c(k * s.length());
for(int i = 0; i < s.length(); i++)
for(int j = 0; j < k; j++)
c.at(k * i + j) = s.substr(i, j + 1);
sort(c.begin(), c.end());
unique(c.begin(), c.end());
cout << c.at(k - 1) << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using db = double;
using ll = long long;
using ull = unsigned long long;
using pII = pair<int, int>;
using pLL = pair<ll, ll>;
constexpr int mod = 1e9 + 7;
template <class T1, class T2>
inline void chadd(T1 &x, T2 y, int Mod = mod) {
x += y;
while (x >= Mod) x -= Mod;
while (x < 0) x += Mod;
}
template <class T1, class T2>
inline void chmax(T1 &x, T2 y) {
if (x < y) x = y;
}
template <class T1, class T2>
inline void chmin(T1 &x, T2 y) {
if (x > y) x = y;
}
inline int nextInt() {
int x;
cin >> x;
return x;
}
void rd() {}
template <class T, class... Ts>
void rd(T &arg, Ts &...args) {
cin >> arg;
rd(args...);
}
void err() {
cout << "\033[39;0m"
<< "\n";
}
template <class T, class... Ts>
void err(const T &arg, const Ts &...args) {
cout << arg << ' ';
err(args...);
}
template <template <typename...> class T, typename t, typename... A>
void err(const T<t> &arg, const A &...args) {
for (auto &v : arg) cout << v << ' ';
err(args...);
}
void ptt() { cout << "\n"; }
template <class T, class... Ts>
void ptt(const T &arg, const Ts &...args) {
cout << ' ' << arg;
ptt(args...);
}
template <class T, class... Ts>
void pt(const T &arg, const Ts &...args) {
cout << arg;
ptt(args...);
}
void pt() {}
template <template <typename...> class T, typename t, typename... A>
void pt(const T<t> &arg, const A &...args) {
for (int i = 0, sze = arg.size(); i < sze; ++i)
cout << arg[i] << " \n"[i == sze - 1];
pt(args...);
}
inline ll qpow(ll base, ll n) {
assert(n >= 0);
ll res = 1;
while (n) {
if (n & 1) res = res * base % mod;
base = base * base % mod;
n >>= 1;
}
return res;
}
constexpr int N = 5e5 + 10;
int n, ans[N], dead[N];
vector<vector<int>> fac;
void run() {
fac.resize(n + 1);
for (int i = 1; i <= n; ++i) {
dead[i] = 0;
for (int j = i; j <= n; j += i) {
fac[i].push_back(j);
}
}
int pos = n;
while (int((fac[pos]).size()) == 1) --pos;
for (int i = n; i >= 2; --i) {
ans[i] = pos;
int cnt = 1;
while (pos > 1) {
while (int((fac[pos]).size()) > 1 && dead[fac[pos].back()] == 1)
fac[pos].pop_back();
if (int((fac[pos]).size()) > 1) {
if (!cnt) break;
--cnt;
dead[fac[pos].back()] = 1;
} else
--pos;
}
}
for (int i = 2; i <= n; ++i) cout << ans[i] << " \n"[i == n];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout << fixed << setprecision(20);
while (cin >> n) run();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int cost;
double amount;
cin >> cost;
string s[n];
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int counter = 1;
double sum = 1.0;
for (int i = n - 2; i >= 0; i--) {
if (s[i] == "halfplus") {
counter++;
sum = (sum + 0.5) * 2;
} else if (s[i] == "half") {
sum = sum * 2;
}
}
double num;
num = sum - (0.5 * counter);
amount = cost * num;
cout << (long long int)amount << endl;
}
| 1 |
Subsets and Splits