code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define READ freopen("in.txt", "r", stdin);
#define WRITE freopen("out.txt", "w", stdout);
#define optimize ios::sync_with_stdio(0);cin.tie(0);
#define RESET(a, b) memset(a, b, sizeof(a))
#define gcd(a, b) __gcd(a, b)
#define MX 300005
#define MOD 1000000007
#define inf ll(1e18)
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p & 1LL) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
inline ll mod(ll a, ll m){ if(a<0) return ((a%m)+m)%m; else if(a<m) return a; else return (a%m); }
/*bool cmp(ll x,ll y)
{
return x>y;
}*/
bool cmp(pair<int,int>a, pair<int,int>b)
{
if(a.first==b.first)
return a.second<b.second;
else
return a.first<b.first;
}
int main(void)
{
optimize
ll t,n,m,k,x,y,a[200001];
vector<ll> v,v1,v2;
vector< pair<ll,ll> > p,p1,p2;
set<ll> st;
map<ll,ll> mp;
priority_queue<ll> pq;
priority_queue< ll,vector<ll>,greater<ll> > lpq;
priority_queue< pair<ll,ll> > phpq;
priority_queue< pair<ll,ll>,vector< pair<ll,ll> >,greater< pair<ll,ll> > > plpq;
string s;
//cin>>t;
//while(t--)
{
cin>>n>>x>>y;
cout<<n-x+y<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int n, x;
string str;
cin >> n >> x >> str;
for (char& ch : str) if (ch == 'o')x++;else x=max(0,x-1);
cout << x << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 10e16;
int main() {
int n, m;
cin >> n >> m;
vector<ll> a(m), b(m);
vector<char> c(m);
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--, b[i]--;
}
vector<vector<vector<P>>> G(n, vector<vector<P>>(n));
rep(i, m) rep(j, m) {
if (i == j) continue;
if (c[i] != c[j]) continue;
G[a[i]][a[j]].push_back({b[i], b[j]});
G[b[i]][b[j]].push_back({a[i], a[j]});
G[a[i]][b[j]].push_back({b[i], a[j]});
G[b[i]][a[j]].push_back({a[i], b[j]});
}
vector<vector<ll>> dist(n, vector<ll>(n, INF));
queue<P> que;
dist[0][n - 1] = 0;
que.push({0, n - 1});
while (!que.empty()) {
P p = que.front();
que.pop();
for (P np : G[p.first][p.second]) {
if (dist[np.first][np.second] != INF) continue;
dist[np.first][np.second] = dist[p.first][p.second] + 1;
que.push(np);
}
}
ll ans = INF;
rep(i, n) ans = min(ans, dist[i][i] * 2);
rep(i, m) {
ans = min(ans, dist[a[i]][b[i]] * 2 + 1);
ans = min(ans, dist[b[i]][a[i]] * 2 + 1);
}
if (ans == INF) ans = -1;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
vector<pair<int,char>> adj[1000];
bool vis[1000][1000];
int best[1000][1000];
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
best[i][j] = 1e9;
}
}
while (m--) {
int u, v;
char c;
cin >> u >> v >> c, --u, --v;
adj[u].emplace_back(v, c);
adj[v].emplace_back(u, c);
}
best[0][n-1] = 0;
queue<pair<int,int>> q;
q.push({0, n-1});
int ans = INT_MAX;
while (!q.empty()) {
int u, v;
tie(u, v) = q.front();
q.pop();
if (vis[u][v]) continue;
if (u == v) ans = min(ans, best[u][v]);
vis[u][v] = true;
for (auto [x1, c1] : adj[u]) {
if (x1 == v) {
ans = min(ans, best[u][v] + 1);
continue;
}
for (auto [x2, c2] : adj[v]) {
if (c1 == c2) {
if (best[x1][x2] > best[u][v] + 2) {
best[x1][x2] = best[u][v] + 2;
q.push({x1, x2});
}
}
}
}
}
if (ans == INT_MAX) cout << "-1";
else cout << ans;
return 0;
}
|
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
// avoid unordered_map and unordered_set!!!
#include <vector>
#include <utility>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
using i32 = int32_t;
using pi64 = pair<i64, i64>;
#define vec vector
#define let const
#define DRi64(x) i64 x; cin >> x;
#define DRS(x) string x; cin >> x;
#define DRVi64(v, n) vec<i64> v(n); { for (i64 i = 0; i < n; ++i) { cin >> v[i]; }}
#define DRpi64(x) pi64 x; cin >> x.first >> x.second;
#ifdef DEBUG
#define P(x) cerr << x << "\n"
#else
#define P(x)
#endif
constexpr i64 MAXN = 3*100*1000LL+5LL;
constexpr i64 MOD = 1000000007LL;
constexpr i64 INF64 = MOD * MOD;
template<i64 mod>
class modint_t
{
public:
modint_t() : val_(0) {}
// not explicit
modint_t(const i64 x) : val_(((x % mod) + mod) % mod) {}
modint_t& operator+=(const modint_t& o) { val_ += o.val_; val_ %= mod; return *this; }
modint_t& operator*=(const modint_t& o) { val_ *= o.val_; val_ %= mod; return *this; }
// only use if mod is prime!!!
static modint_t inv(const modint_t& x)
{
modint_t<mod> acc(1);
modint_t<mod> curpow(*x);
for (i64 e = mod - 2; e; e >>= 1, curpow *= curpow)
{
if (e & 1)
{
acc *= curpow;
}
}
return acc;
}
i64 operator*() const { return val_; }
private:
i64 val_;
};
void
dfs(const i64 u, const i64 p, const vec<vec<pi64>>& adj, vec<i64>& dists, const i64 ancestor_edge_wt_xor = 0)
{
dists[u] = ancestor_edge_wt_xor;
for (let auto [child, w] : adj[u])
{
if (child == p)
{
continue;
}
dfs(child, u, adj, dists, ancestor_edge_wt_xor ^ w);
}
}
int
main()
{
ios_base::sync_with_stdio(false);
// fast io: see 1423K
cin.tie(nullptr);
cout.tie(nullptr);
DRi64(N);
vec<vec<pi64>> adj(N + 1);
for (i64 i = 0; i + 1 < N; ++i)
{
DRi64(U); DRi64(V); DRi64(W);
adj[U].emplace_back(V, W);
adj[V].emplace_back(U, W);
}
vec<i64> dists(N + 1);
dfs(1, -1, adj, dists);
modint_t<MOD> acc;
for (i64 bitpos = 0; bitpos < 60; ++bitpos)
{
let i64 mask = 1LL << bitpos;
i64 num_on = 0;
for (i64 i = 1; i <= N; ++i)
{
num_on += !!(dists[i] & mask);
}
modint_t<MOD> tmp(mask);
tmp *= num_on * (N - num_on);
acc += tmp;
}
cout << *acc << "\n";
return 0;
}
| #include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
class ModInt{
public:
ll v;
ModInt(ll _v = 0){
if(_v >= MOD) _v %= MOD;
v = _v;
}
ModInt operator+(ll n){
return ModInt((v+n)%MOD);
}
ModInt operator-(ll n){
return ModInt((v-n+MOD)%MOD);
}
ModInt operator*(ll n){
if(n >= MOD) n %= MOD;
return ModInt((v*n)%MOD);
}
ModInt operator/(ll n){
return ModInt((ModInt(n).inv()*v).v%MOD);
}
void operator+=(ll n){
v = (v+n)%MOD;
}
void operator-=(ll n){
v = (v-n+MOD)%MOD;
}
void operator*=(ll n){
v = (v*n+MOD)%MOD;
}
ModInt operator+(ModInt n){
return ModInt((v+n.v)%MOD);
}
ModInt operator-(ModInt n){
return ModInt((v-n.v+MOD)%MOD);
}
ModInt operator*(ModInt n){
return ModInt((v*n.v)%MOD);
}
ModInt operator/(ModInt n){
return ModInt((n.inv()*v).v%MOD);
}
void operator+=(ModInt n){
v = (v+n.v)%MOD;
}
void operator-=(ModInt n){
v = (v-n.v+MOD)%MOD;
}
void operator*=(ModInt n){
v = (v*n.v)%MOD;
}
void operator=(ModInt n){
v = n.v;
}
bool operator==(ModInt n){
return v == n.v;
}
bool operator!=(ModInt n){
return v != n.v;
}
void operator=(ll n){
v = n%MOD;
}
ModInt inv(){
if(v == 1) return ModInt(1);
else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD);
}
};
ostream& operator<<(ostream& os, const ModInt& m){
os << m.v;
return os;
}
istream & operator >> (istream &in, ModInt &m){
in >> m.v;
return in;
}
int ch[200000];
bool used[200000];
ll xo[200000];
struct edge{
int to;
ll cost;
};
vector<edge> g[200000];
using mint = ModInt;
void dfs(int v, ll x){
used[v] = true;
ch[v]++;
xo[v] = x;
for(edge e : g[v]){
if(!used[e.to]){
dfs(e.to, x^e.cost);
ch[v] += ch[e.to];
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n; cin >> n;
for(int i = 0; i < n-1; i++){
int u, v; ll w; cin >> u >> v >> w; u--; v--;
g[v].push_back(edge{u, w});
g[u].push_back(edge{v, w});
}
dfs(0, 0);
mint ans = mint(0);
for(int j = 0; j < 60; j++){
ll cnt = 0;
for(int i = 0; i < n; i++){
if(xo[i]&(1ll<<j)) cnt++;
}
ans += mint(1ll<<j)*cnt*(n-cnt);
}
cout << ans << endl;
} |
/* -*- coding: utf-8 -*-
*
* f.cc: F - Max Matrix
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 200000;
const int MAX_E2 = 1 << 19; // = 524288
const int MAX_QN = 200000;
/* typedef */
typedef long long ll;
struct Qry {
int t, x, y;
Qry() {}
Qry(int _t, int _x, int _y): t(_t), x(_x), y(_y) {}
void read() { scanf("%d%d%d", &t, &x, &y); x--; }
void print() { printf("Qry(%d,%d,%d)\n", t, x, y); }
};
template <typename T, const int MAX_E2>
struct SegTreeSum {
int e2;
T nodes[MAX_E2];
SegTreeSum() {}
void init(int n) {
for (e2 = 1; e2 < n; e2 <<= 1);
//fill(nodes, nodes + MAX_E2, 0);
}
T &geti(int i) { return nodes[e2 - 1 + i]; }
void set(int i, T v) {
int j = e2 - 1 + i;
nodes[j] = v;
while (j > 0) {
j = (j - 1) / 2;
nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
}
}
void add(int i, T v) { set(i, geti(i) + v); }
T sum_range(int r0, int r1, int k, int i0, int i1) {
if (r1 <= i0 || i1 <= r0) return 0;
if (r0 <= i0 && i1 <= r1) return nodes[k];
int im = (i0 + i1) / 2;
T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);
return v0 + v1;
}
T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};
/* global variables */
Qry qs[MAX_QN];
int as[MAX_N], bs[MAX_N], uas[MAX_N + 1], ubs[MAX_N + 1];
SegTreeSum<int,MAX_E2> asti, bsti;
SegTreeSum<ll,MAX_E2> astl, bstl;
/* subroutines */
inline int get_index(int un, int us[], int v) {
return lower_bound(us, us + un, v) - us;
}
/* main */
int main() {
int n, m, qn;
scanf("%d%d%d", &n, &m, &qn);
int uan = 0, ubn = 0;
uas[uan++] = ubs[ubn++] = 0;
for (int i = 0; i < qn; i++) {
qs[i].read();
if (qs[i].t == 1) uas[uan++] = qs[i].y;
else ubs[ubn++] = qs[i].y;
}
sort(uas, uas + uan);
sort(ubs, ubs + ubn);
uan = unique(uas, uas + uan) - uas;
ubn = unique(ubs, ubs + ubn) - ubs;
asti.init(uan), astl.init(uan);
asti.set(0, n);
bsti.init(ubn), bstl.init(ubn);
bsti.set(0, m);
ll sum = 0;
for (int i = 0; i < qn; i++) {
Qry &qi = qs[i];
//qi.print();
if (qi.t == 1) {
int pa = as[qi.x];
if (pa != qi.y) {
int i0 = get_index(ubn, ubs, pa);
sum -= (ll)pa * bsti.sum_range(0, i0) + bstl.sum_range(i0, ubn);
int j0 = get_index(uan, uas, pa);
asti.add(j0, -1), astl.add(j0, -pa);
int i1 = get_index(ubn, ubs, qi.y);
sum += (ll)qi.y * bsti.sum_range(0, i1) + bstl.sum_range(i1, ubn);
int j1 = get_index(uan, uas, qi.y);
asti.add(j1, 1), astl.add(j1, qi.y);
as[qi.x] = qi.y;
}
}
else {
int pb = bs[qi.x];
if (pb != qi.y) {
int i0 = get_index(uan, uas, pb);
sum -= (ll)pb * asti.sum_range(0, i0) + astl.sum_range(i0, uan);
int j0 = get_index(ubn, ubs, pb);
bsti.add(j0, -1), bstl.add(j0, -pb);
int i1 = get_index(uan, uas, qi.y);
sum += (ll)qi.y * asti.sum_range(0, i1) + astl.sum_range(i1, uan);
int j1 = get_index(ubn, ubs, qi.y);
bsti.add(j1, 1), bstl.add(j1, qi.y);
bs[qi.x] = qi.y;
}
}
printf("%lld\n", sum);
}
return 0;
}
| #include "bits/stdc++.h"
#include <chrono>
#include <random>
#define lli long long int
using namespace std;
#define mod 1000000007
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define INF 2000000000
#define common cout << "Case #" << w+1 << ": "
#define maxn 1000010
void setIO(string name) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
template< typename T>
void PVecPrint(vector<T>&v)
{
for(int i=0;i<(int)v.size();i++)
cout << v[i].first << "," << v[i].second << ' ';
cout << '\n';
}
template<class T>
void VecPrint(vector<T>&v)
{
for(int i=0;i<v.size();i++)
cout << v[i] << ' ';
cout << '\n';
}
/*---------------------------------------Code-----------------------------------------------------*/
struct FenwickTree {
vector<lli> bit;
int n;
void build(vector<lli> &a)
{
n=a.size();
bit.resize(n);
for (int i = 0; i < a.size(); i++)
add(i, a[i]);
}
lli sum(int r) {
lli ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
lli sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, lli delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
int n,m,q;
vector<pair<int,pair<int,int> > >v;
set<int>s;
map<int,int>getInd;
lli ans=0;
FenwickTree F[4];
int tot;
lli getLessSum(int id,lli num)
{
int numId=getInd[num];
lli sum=F[id].sum(0,numId);
return sum*1LL*num;
}
lli getMoreSum(int id,lli num)
{
int numId=getInd[num];
lli sum=F[id].sum(numId+1,tot+1);
return sum;
}
int main()
{
cin >> n >> m >> q;
vector<int>v1(n+10,0),v2(m+10,0);
v.resize(q);
s.insert(0);
for(int i=0;i<q;i++)
{
cin >> v[i].first >> v[i].second.first >> v[i].second.second;
s.insert(v[i].second.second);
}
int ind=0;
for(auto &num:s)
{
getInd[num]=ind;
++ind;
}
vector<lli>t(ind+10,0);
for(int i=0;i<4;i++)
F[i].build(t);
tot=ind;
F[0].add(0,n);
F[1].add(0,m);
for(int i=0;i<q;i++)
{
int t=v[i].first;
if(t==1)
{
int pid=v[i].second.first,id=getInd[v1[pid]],nid=getInd[v[i].second.second];
lli sum=getLessSum(1,v1[pid])+getMoreSum(3,v1[pid]);
ans-=sum;
F[0].add(id,-1);
F[2].add(id,-v1[pid]);
v1[pid]=v[i].second.second;
F[0].add(nid,1);
F[2].add(nid,v1[pid]);
sum=getLessSum(1,v1[pid])+getMoreSum(3,v1[pid]);
ans+=sum;
}
else
{
int pid=v[i].second.first,id=getInd[v2[pid]],nid=getInd[v[i].second.second];
lli sum=getLessSum(0,v2[pid])+getMoreSum(2,v2[pid]);
ans-=sum;
F[1].add(id,-1);
F[3].add(id,-v2[pid]);
v2[pid]=v[i].second.second;
F[1].add(nid,1);
F[3].add(nid,v2[pid]);
sum=getLessSum(0,v2[pid])+getMoreSum(2,v2[pid]);
ans+=sum;
}
cout << ans << '\n';
}
} |
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
using namespace std;
random_device rnd;
mt19937 mt(rnd());
int main(){
int n,m;
cin >> n >> m;
vector<string> ss(m);
for(int i = 0;i < m;i++){
cin >> ss[i];
}
sort(ss.begin(),ss.end(),[](const string a, const string b){
return a.size() > b.size();
});
//for (auto e:ss){
// cerr << e << endl;
//}
vector<string> newss;
vector<int> val;
for (int i = 0;i < m;i++){
if (i == 0) {
newss.push_back(ss[i]);
val.push_back(1);
}
else{
int len = newss.back().size();
if (len + ss[i].size() <= 20){
string adds = newss.back()+ss[i];
newss.pop_back();
newss.push_back(adds);
val.back()++;
}
else{
newss.push_back(ss[i]);
val.push_back(1);
}
}
}
//max val
int maxval = -1;
int maxidx = 0;
for (int i = 0; i < val.size();i++){
if (val[i] > maxval){
maxidx = i;
maxval = val[i];
}
}
int len = newss.size();
vector<string> ans;
for (int i = 0; i < 20;i++){
if (len-1 < i){
string st = newss[maxidx];
while(st.size() < 20){
char c = 'A'+(mt())%8;
st += c;
}
ans.push_back(st);
}
else{
if (newss[i].size() < 20){
int clen = newss[i].size();
for (int t = 0; t < 20-clen;t++){
newss[i].push_back(char('A'+(mt()%8)));
}
}
ans.push_back(newss[i]);
}
}
for (auto x:ans){
for (auto c:x){
cout << c;
}
cout << "\n";
}
} | // 2021-06-26 16:47:13
// clang-format off
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
using namespace std;
// clang-format on
random_device seed_gen;
mt19937 engine(seed_gen());
const int n = 20;
int m;
vector<string> ss;
map<int, vector<int> > mp;
struct Out {
int a[n][n];
};
void output(Out& out) {
stringstream buffer;
rep(i, n) {
rep(j, n) {
if (out.a[i][j] == 0) {
buffer << '.';
} else {
buffer << char('A' + out.a[i][j] - 1);
}
}
buffer << '\n';
}
cout << buffer.str();
}
bool matched(Out& state, int mi, int si, int sj, int d) {
bool res = true;
int di = (d == 1);
int dj = (d == 0);
string& s = ss[mi];
int sz = s.size();
rep(idx, sz) {
int ti = (si + di * idx) % n;
int tj = (sj + dj * idx) % n;
res &= (s[idx] - 'A' + 1 == state.a[ti][tj]);
}
return res;
}
int calc(Out& state) {
int c = 0;
rep(mi, m) {
bool ok = false;
rep(si, n) {
rep(sj, n) {
rep(d, 2) {
ok |= matched(state, mi, si, sj, d);
if (ok) break;
}
if (ok) break;
}
if (ok) break;
}
c += ok;
}
double score = double(1e8) * c / m;
if (c == m) {
int d = 0;
rep(i, n) rep(j, n) d += state.a[i][j] == 0;
score *= 2 * n * n;
score /= 2 * n * n - d;
}
return int(round(score));
}
void init() {
int n_;
cin >> n_ >> m;
ss.resize(m);
rep(i, m) {
cin >> ss[i];
mp[-ss[i].size()].push_back(i);
}
}
const double TL = 2.955;
double normalized_time() { return double(clock()) / CLOCKS_PER_SEC / TL; }
void answer() {
init();
Out ans, state;
rep(i, n) rep(j, n) ans.a[i][j] = 0;
int score = 0;
while (1) {
double t = normalized_time();
if (t >= 1.0) break;
rep(i, n) rep(j, n) state.a[i][j] = 0;
for (auto& [k, v] : mp) {
int sz = -k;
shuffle(ALL(v), engine);
for (auto mi : v) {
int mxsi = -1, mxsj = -1, mxd = -1, mx = -1;
rep(si, n) {
rep(sj, n) {
rep(d, 2) {
int cur = 0;
bool ok = true;
rep(idx, sz) {
int ti = (si + idx * (d == 1)) % n;
int tj = (sj + idx * (d == 0)) % n;
if (state.a[ti][tj] == ss[mi][idx] - 'A' + 1) {
cur++;
} else if (state.a[ti][tj] > 0) {
ok = false;
break;
}
}
if (ok && mx < cur) {
mx = cur;
mxsi = si;
mxsj = sj;
mxd = d;
}
}
}
}
if (mx >= 0) {
rep(idx, sz) {
int ti = (mxsi + idx * (mxd == 1)) % n;
int tj = (mxsj + idx * (mxd == 0)) % n;
state.a[ti][tj] = ss[mi][idx] - 'A' + 1;
}
}
}
}
int prev = score;
score = calc(state);
if (prev < score) {
ans = state;
} else {
score = prev;
}
}
output(ans);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
answer();
return 0;
} |
#include<bits/stdc++.h>
#define eps 1e-9
#define pii pair<double,double>
#define mp make_pair
using namespace std;
const int N=200005;
pii dp[N],sum;
int n,m,k;
bool in[N];
pii operator -(const pii &a,const pii &b)
{
return mp(a.first-b.first,a.second-b.second);
}
pii operator +(const pii &a,const pii &b)
{
return mp(a.first+b.first,a.second+b.second);
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1,x;i<=k;++i)
{
scanf("%d",&x);
in[x]=1;
}
sum=mp(0,0);
for(int i=n-1;i>=0;--i)
{
if(in[i])dp[i]=mp(1,0);
else dp[i]=mp(sum.first/m,sum.second/m+1);
sum=sum+dp[i]-dp[i+m];
}
if(abs(dp[0].first-1)<=eps)printf("-1");
else printf("%.4lf",dp[0].second/(1-dp[0].first));
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
ll N, M, K, A;
cin >> N >> M >> K;
vector<bool> start(N + 1, false);
rep(i, K) {
cin >> A;
start[A] = true;
}
double L = 0, R = 1e20;
vector<double> dp(N + 1 + M, 0);
rep(i, 100) {
double m = (L + R) / 2, s = 0;
rep(j, N) {
if (start[N - 1 - j]) {
dp[N - 1 - j] = m;
} else {
dp[N - 1 - j] = 1 + s / M;
}
s += dp[N - 1 - j];
if (j >= M - 1) s -= dp[N - 1 - j + M];
//cout << "s=" << s << " ";
}
if (dp[0] > m) L = m;
else R = m;
//rep(i, N + 1) cout << dp[i] << " "; cout << "\n";
//cout << L << " " << R << "\n";
}
if (dp[0] > 1e15) cout << "-1\n";
else cout << fixed << setprecision(20) << dp[0] << "\n";
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int N;cin>>N;
vector<pair<int,int>>points(N);
// taking points
for(int i=0,x,y;i<N;i++){
cin>>x>>y;
points[i].first = x;
points[i].second = y;
}
int m;cin>>m;
vector<pair<int,int>>op(m);
// taking operations
for(int i=0;i<m;i++){
cin>>op[i].first;
if(op[i].first > 2) cin>>op[i].second;
}
int q;cin>>q;
vector<pair<pair<int,int>,int>>queries(q);
for(int i=0;i<q;i++){
cin>>queries[i].first.first>>queries[i].first.second;
queries[i].second = i;
}
sort(queries.begin(),queries.end());
vector<pair<int,int>>ans(q);
int idx = 0 , sx = 1 , sy = 1 , a = 0 , b = 0 ;
bool swapped = 0;
for(int i=0;i<q;i++){
while(idx < queries[i].first.first) {
if(op[idx].first == 1){
swap(sx, sy);
swap(a,b);
swapped ^= 1;
sy = -sy;
b = -b;
}
else if(op[idx].first == 2){
swap(sx, sy);
swap(a, b);
swapped ^= 1;
sx = -sx;
a = -a;
}
else if(op[idx].first == 3){
a = 2*op[idx].second - a;
sx = -sx;
}
else{
b = 2 * op[idx].second - b;
sy = -sy;
}
idx++;
}
int id = queries[i].second;
int p_id = queries[i].first.second - 1;
if(swapped){
ans[id] = {points[p_id].second*sx + a , points[p_id].first*sy + b};
}
else{
ans[id] = {points[p_id].first*sx + a , points[p_id].second * sy + b};
}
}
for(auto p : ans) {
cout<<p.first<<" "<<p.second<<endl;
}
} | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define N 200005
struct Mat { ll m[3][3]; };
Mat op[N], nd[N], p[5], cur;
Mat mul(Mat x, Mat y) {
Mat tmp;
rep(i,3)rep(j,3) {
tmp.m[i][j] = 0;
rep(k,3) {
tmp.m[i][j] += x.m[i][k] * y.m[k][j];
}
}
return tmp;
}
int main() {
int n;
cin >> n;
p[1].m[0][1] = -1; p[1].m[1][0] = 1;
p[2].m[0][1] = 1; p[2].m[1][0] = -1;
p[3].m[0][0] = -1; p[3].m[1][1] = 1;
p[4].m[0][0] = 1; p[4].m[1][1] = -1;
p[1].m[2][2] = p[2].m[2][2] = p[3].m[2][2] = p[4].m[2][2] = 1;
for(int i = 1; i <= n; i++) {
cin >> nd[i].m[0][0] >> nd[i].m[0][1];
nd[i].m[0][2] = 1;
}
int m;
cin >> m;
rep(i,3) op[0].m[i][i] = 1;
for(int i = 1; i <= m; i++) {
int x, y;
cin >> x;
if(x == 3) {
cin >> y;
p[3].m[2][0] = 2*y;
} else if(x == 4) {
cin >> y;
p[4].m[2][1] = 2*y;
}
op[i] = mul(op[i-1], p[x]);
}
int q;
cin >> q;
while(q--) {
int a, b;
cin >> a >> b;
cur = mul(nd[b], op[a]);
cout << cur.m[0][0] << " " << cur.m[0][1] << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define maxn 300001
using namespace std;
typedef long long LL;
int n;
LL ans;
int ar[maxn];
int tree[maxn];
void update(int x) {
while(x <= n) {
tree[x]++;
x += (x&(-x));
}
}
int query(int x) {
int sum = 0;
while(x) {
sum += tree[x];
x -= (x&(-x));
}
return sum;
}
int main() {
scanf("%d",&n);
for( int i = 1 ; i <= n ; i++ ) {
scanf("%d",&ar[i]);
update(++ar[i]);
ans += i - query(ar[i]);
}
printf("%lld ",ans);
for( int i = 1 ; i < n ; i++ ) {
ans += n+1 - 2*ar[i];
printf("%lld ",ans);
}
return 0;
} | // Author: old_school
// Created: 30.01.2021 17:30:27
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
using namespace std;
#define lld long long int
#define ld long double
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define F first
#define S second
#define nl '\n'
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define sz(c) (c).size()
#define tr(x,a) for(auto &a : x)
#define psnt(x,a) (x).find(a)!=(x).end()
#define vpsnt(x,a) find(all(x),a)!=(x).end()
#define MOD 1000000007
#define tod 1
#define pi 3.1415926536
#define itr(i,a,b) for(lld i=a;i<=b;i++)
#define itrn(i,a,b) for(lld i=a;i>=b;i--)
#define iot(n) for(lld i=0;i<n;i++)
#define pls(n,arr) lld arr[n]; iot(n) cin>>arr[i];
#define bye fflush(stdout)
typedef pair<lld,lld> pii;
typedef pair<string,lld> psi;
template <typename T>
bool mycomp(T x,T y){
return (x==y); //give your condition here
}
bool paircomp(const pair<lld,lld> &x,const pair<lld,lld> &y){
return x.second<y.second;
}
lld _mergeSort(lld arr[], lld temp[],
lld left, lld right);
lld merge(lld arr[], lld temp[], lld left,
lld mid, lld right);
lld mergeSort(lld arr[], lld array_size)
{
lld temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
lld _mergeSort(lld arr[], lld temp[],
lld left, lld right)
{
lld mid, inv_count = 0;
if (right > left) {
mid = (right + left) / 2;
inv_count += _mergeSort(arr, temp,
left, mid);
inv_count += _mergeSort(arr, temp,
mid + 1, right);
inv_count += merge(arr, temp, left,
mid + 1, right);
}
return inv_count;
}
lld merge(lld arr[], lld temp[], lld left,
lld mid, lld right)
{
lld i, j, k;
lld inv_count = 0;
i = left;
j = mid;
k = left;
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
inv_count = inv_count + (mid - i);
}
}
while (i <= mid - 1)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
void solve(){
lld n; cin>>n;
pls(n,a);
lld b[n];
iot(n) b[i]=a[i];
lld ans = mergeSort(a, n);
cout<<ans<<nl;
iot(n-1){
ans+=(n-2*b[i]-1);
cout<<ans<<nl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
lld t=1;// cin>>t;
while(t--){
solve();
cout<<endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
//begin of def
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define endl '\n'
using lli = long long int;
using ulli = unsigned long long int;
using Ld = long double;
using pii = pair<int, int>;
using pll = pair<lli, lli>;
using pld = pair<Ld, Ld>;
#define X first
#define Y second
#define rep(I, S, E) for(int I = (S); I < (E); I++)
#define repq(I, S, E) for(int I = (S); I <= (E); I++)
#define pb push_back
#define epb emplace_back
#define ALL(X) X.begin(), X.end()
//end of def
map<lli, int> st[2];
int main(){
fastio;
int n;
cin >> n;
vector<lli> a(n), b(n);
rep(i, 0, n)
cin >> a[i];
st[0][a[0]]++;
b[0] = a[0];
rep(i, 1, n){
b[i] = a[i] - b[i - 1];
st[i & 1][b[i]]++;
}
lli m = 0, res = 0;
rep(i, 0, n){
int ct = 0;
if(i & 1)
ct = st[0][m] + st[1][-m];
else
ct = st[0][-m] + st[1][m];
st[i & 1][b[i]]--;
res += ct;
m = b[i];
}
cout << res;
return 0;
}
| #include<bits/stdc++.h>
#include<vector>
#include<string>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
bool flag[2510];
int dx[4];
int dy[4];
int t[51][51];
int p[51][51];
string direction = "UDLR";
int nx,ny;
int mx;
int sm,maxsum;
bool is_legal_action(int nx,int ny,bool *flag){
if(0<=nx&&nx<50&&0<=ny&&ny<50&&flag[t[nx][ny]]==0){
return true;
}else{
return false;
}
}
int legallistlength(int x,int y,bool *flag){
int k=0;
rep(i,4){
nx = x + dx[i];
ny = y + dy[i];
if(is_legal_action(nx,ny,flag)){
k++;
}
}
return k;
}
void dfs(int x,int y,bool *flag,int depth){
flag[t[x][y]]=1;
sm+=p[x][y];
//sm++;
rep(i,4){
nx = x + dx[i];
ny = y + dy[i];
if(depth>=1&&is_legal_action(nx,ny,flag)){
//cout << x <<" " << y <<" " << i << endl;
dfs(nx,ny,flag,depth-1);
}
}
if(depth==0||legallistlength(x,y,flag)==0){
if(maxsum<sm){
maxsum=sm;
//cout << maxsum << endl;
}
}
sm-=p[x][y];
//sm--;
flag[t[x][y]]=0;
return;
}
void func(int x,int y,bool *flag,vector<char>& route){
flag[t[x][y]]=1;
int s=-1;
int nxt=-1;
rep(i,4){
nx = x + dx[i];
ny = y + dy[i];
if(is_legal_action(nx,ny,flag)){
/*if(s<=p[nx][ny]){
s=p[nx][ny];
nxt=i;
}*/
sm=0;
maxsum=-1;
dfs(nx,ny,flag,22);
if(maxsum>=s){
s=maxsum;
nxt=i;
}
//cout << i << " " << maxsum <<endl;
}
//cout <<x<<" " << y <<direction[i]<< nxt << endl;
}
flag[t[x][y]]=1;
if(nxt==-1){
for (auto i: route){
cout << i;
}
cout << "\n";
}else{
route.push_back(direction[nxt]);
func(x + dx[nxt],y + dy[nxt],flag,route);
route.pop_back();
}
flag[t[x][y]]=0;
return;
}
int main(){
int si,sj;
scanf("%d %d",&si,&sj);
int M=0;
rep(i,50)rep(j,50){
cin >> t[i][j];
if(M<t[i][j])M=t[i][j];
}
M++;
rep(i,50)rep(j,50) scanf("%d", &p[i][j]);
rep(i,2500)flag[i]=0;
vector<char> route;
dx[0] = -1;
dx[1] = 1;
dx[2]=0;
dx[3]=0;
dy[0]=0;
dy[1]=0;
dy[2]=-1;
dy[3]=1;
func(si,sj,flag,route);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int T;
cin>>T;
while(T--){
map<int,bool>cnt;
int n;cin>>n;
for(int i=0;i<n;i++){
int x;cin>>x;
cnt[x]^=1;
}
if(n&1)puts("Second");
else {
bool f=false;
for(auto& i:cnt)f|=i.second;
puts(f?"First":"Second");
}
}
} | //Code by Ritik Agarwal
#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define int long long int
#define loop(i,a,b) for(int i=a;i<b;i++)
#define scan(arr,n) for (int i = 0; i < n; ++i) cin >> arr[i]
#define vi vector<int>
#define si set<int>
#define pii pair <int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map <int, int>
#define pb push_back
#define ff first
#define ss second
#define all(aa) aa.begin(), aa.end()
#define rall(a) a.rbegin() , a.rend()
#define read(a,b) int a,b; cin>>a>>b
#define readt(a,b,c) int a,b,c; cin>>a>>b>>c
#define readf(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d;
#define print(v) for(auto x:v) cout<<x<<" ";cout<<endl
#define printPair(res) for(pair<int,int>& p:res) cout<<p.first<<" "<<p.second<<endl;
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
//***** Constants *****
const int mod = 1000000007; /* 1e9 + 7*/
const int MAXN = 1000005; /*1e6 +5 */
const int mod1= 998244353;
int vis[MAXN]={0};
int fac[300001];
void fact(int m){fac[0]=1;for(int i=1;i<=300000;i++) fac[i]=(fac[i-1]%m * i%m)%m;}
/* Iterative Function to calculate (x^y)%p in O(log y)*/
long long power( long long x, int y, int p){ long long res = 1;
x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p;} return res;}
// Returns n^(-1) mod p
long long modInverse( long long n, int p) { return power(n, p - 2, p); }
long long nCr( long long n,int r, int p) { if (r == 0) return 1; if(n<r) return 0;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }
int binarySearch(vi arr, int l, int r, int x) {
if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x); } return -1; }
const int NAX=3E5+5;
int BIT[NAX];
int getSum( int index)
{
int sum=0;
while(index>0)
{
sum+=BIT[index];
index -=index & (-index);
}
return sum;
}
void update(int n , int index , int val)
{
while(index<=n)
{
BIT[index]+=val;
index += index & (-index);
}
}
void convert(int arr[], int n)
{
// Create a copy of arrp[] in temp and sort the temp array
// in increasing order
int temp[n];
for (int i=0; i<n; i++)
temp[i] = arr[i];
sort(temp, temp+n);
// Traverse all array elements
for (int i=0; i<n; i++)
{
// lower_bound() Returns pointer to the first element
// greater than or equal to arr[i]
arr[i] = lower_bound(temp, temp+n, arr[i]) - temp + 1;
}
}
void solve(int test)
{
int n;cin>>n;
vi arr(n);
scan(arr,n);
sort(all(arr));
if(n&1) cout<<"Second\n";
else
{
int f=0;
for(int i=-0;i<n;i+=2)
{
if(arr[i]!=arr[i+1])
f=1;
}
if(f==1)
cout<<"First\n";
else
cout<<"Second\n";
}
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
faster;//fact(mod1);
int t=1;
cin>>t;
for(int test=1;test<=t;test++)
{
solve(test);
}
} |
#include <bits/stdc++.h>
using namespace std;
struct Node{
vector<Node*> nodes;
long long val;
int depth;
};
vector<Node> node;
void depth_dfs(Node& a, int d){
a.depth = d;
for (auto& next:a.nodes)
if(next->depth==-1)
depth_dfs(*next, d+1);
}
void imos_dfs(Node& a, long now){
now += a.val;
a.val = now;
for (auto& next:a.nodes)
if(next->depth > a.depth)
imos_dfs(*next, now);
}
int main(){
int N;
cin >> N;
int a[N], b[N];
node.resize(N);
for (int i = 0; i < N; i++)node[i].val = 0;
for (int i = 0; i < N; i++)node[i].depth = -1;
for (int i = 0; i < N-1; i++){
cin >> a[i] >> b[i];
a[i]--;b[i]--;
node[a[i]].nodes.push_back(&node[b[i]]);
node[b[i]].nodes.push_back(&node[a[i]]);
}
depth_dfs(node[0],0);
int Q;
cin >> Q;
for (int i = 0; i < Q; i++){
int t, e, x, u, v;
cin >> t >> e >> x;
e--;
u = a[e]; v = b[e];
if(t==2)swap(u,v);
if(node[u].depth > node[v].depth)
node[u].val += x;
else{
node[0].val += x;
node[v].val -= x;
}
}
imos_dfs(node[0],0);
for (int i = 0; i < N; i++)
cout << node[i].val << endl;
}
| /* In the name of Allah, The Most Gracious, The Most Merciful!
Iffat Sanzida
JU CSE48
❀(◕‿◕)❀
*/
#include<bits/stdc++.h>
#define ll long long int
#define pi acos(-1)
#define FAST() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
//ll mod=1000000007;
//bool sortBysec(pair<ll,ll>a,pair<ll,ll>b)
//{
// return a.second>b.second;
//}
//
//bool prime[2000000];
//vector<ll>vv;
//void SieveOfEratosthenes(ll n)
//{
//
// prime[2]=true;
// memset(prime, true, sizeof(prime));
// for(ll i=4; i<=n; i+=2)
// {
// prime[i]=false;
// }
//
// for (ll p=3; p*p<=n; p+=2)
// {
//
// if (prime[p] == true)
// {
//
// for (ll i=p*p; i<=n; i +=(2*p))
// prime[i] = false;
// }
// }
// vv.push_back(2);
// for(ll i=3; i<=n; i+=2)
// {
// if(prime[i])
// {
// vv.push_back(i);
// }
// }
//
//}
////
////ll phi[10000000];
////void EulerTotientFunction(ll n)
////{
//// for(ll i=1; i<=n; i++)
//// {
//// phi[i]=i;
//// }
//// for(ll i=2; i*i<=n; i++)
//// {
//// if(phi[i]==i)
//// {
//// for(ll j=i; j<=n; j+=i)
//// {
//// phi[j]/=i;
//// phi[j]*=(i-1);
//// }
//// }
//// }
////}
//
//
//ll binExp(ll a,ll n)
//{
// ll res=1;
// while(n)
// {
// if(n%2==1)
// {
// res=(res*a)%mod;
// n--;
// }
// else
// {
// a=(a*a)%mod;
// n=n/2;
// }
// }
// return res;
//}
//
//ll ncr(ll n,ll r)
//{
// if(r>n-r)
// {
// r=n-r;
// }
// ll i,j,ans=1;
// for(i=n,j=1; j<=r; j++,i--)
// {
// ans=((ans*i)%mod);
// ans=ans/j;
// //ans=(ans*binExp(j,mod-2))%mod;
// }
// return ans;
//}
//
//ll power(ll n,ll m)
//{
// ll ans=1;
// while(m)
// {
// ans*=n;
// m--;
// }
// return ans;
//}
//
//ll digsum(ll n)
//{
// ll sum=0;
// while(n)
// {
// sum+=n%10;
// n=n/10;
// }
// return sum;
//}
//ll fac[2000006];
//void fact(ll n)
//{
//
// fac[1]=1;
// fac[0]=1;
// for (ll i=1; i<=n; i++)
// {
// fac[i]=((fac[i-1]%mod)*(i%mod))%mod;
// }
//
//}
//
//ll npr(ll n,ll r)
//{
// ll ans=1;
// for(ll i=n,j=1; j<=r; j++,i--)
// {
// ans=((ans%mod)*(i%mod))%mod;
// }
// return ans;
//}
//
//
//ll bigmod(ll a,ll b)
//{
// ll ans=1;
// for(ll i=1; i<=b; i++)
// {
// ans*=a;
//
// }
// return ans;
//}
//bool sortbysecTuple(const tuple<int, int, int>& a, const tuple<int, int, int>& b)
//{
// return (get<1>(a) < get<1>(b));
//}
//
//ll ex_gcd(ll a,ll b,ll &x, ll &y)
//{
// if(b==0)
// {
// x=1;
// y=0;
// return a;
// }
// ll x1,y1;
// ll d=ex_gcd(b,a%b,x1,y1);
// x=y1;
// y=x1-y1*(a/b);
// return d;
//}
//ll cntdig(ll n)
//{
// ll cnt=0;
// while(n)
// {
// cnt++;
// n=n/10;
// }
// return cnt;
//}
int main()
{
FAST();
ll t=1,i=0,m,n,j,l,k,x,y,z,a,b,p,c,d,q,tt=0,r,s;
//cin>>t;
while(t--)
{
tt++;
double D,H,di,hi,ans=0.0;
// vector<double>d,h;
cin>>n>>D>>H;
for(i=0;i<n;i++)
{
cin>>di>>hi;
ans=max(ans,H-((D*(H-hi))/(D-di)));
}
cout<<fixed<<setprecision(15)<<ans<<endl;
}
return 0;
}
|
/*pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
//constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-7;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T> &vector, unsigned int index)
{
vector.erase(vector.begin() + index);
}
using Graph = vector<vector<ll>>;
// MOD確認
ll N,M,K;
ll A[110],B[110];
ll C[110],D[110];
ll E[110];
int main(){
ll ans = 0;
cin >> N >> M;
rep(i,M) cin >> A[i] >> B[i];
cin >> K;
rep(i,K) cin >> C[i] >> D[i];
rep(i,(1LL << K)){
rep(j,105) E[j] = 0;
rep(j,K){
if(i & (1LL<<j)) E[C[j]]++;
else E[D[j]]++;
}
ll cnt = 0;
rep(j,M) if(E[A[j]] >= 1 && E[B[j]] >= 1) cnt++;
ans = max(ans,cnt);
}
cout << ans << endl;
}
| /*Name:- Prince Soni
College:- Jalpaiguri Government Engineering College*/
// this is how we declare a constant value
// const int xyz=2500000;
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define fr(i,n) for(ll i=0;i<n;i++)
#define fr1(i,n) for(ll i=1;i<=n;i++)
#define endl "\n"
#define mp make_pair
#define inf 1000000009
int main()
{
fio;
ll n;
cin>>n;
ll a[n];
fr(i,n)
cin>>a[i];
ll ans=0,res;
for(int i=2;i<=1000;i++)
{
ll c=0;
for(int j=0;j<n;j++)
{
if(a[j]%i==0)
c++;
}
if(max(ans,c)==c)
{
ans=max(ans,c);
res=i;
}
}
cout<<res<<endl;
} |
#include<bits/stdc++.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#define ll long long
#define skip cin>>ws;
#define vll vector<ll>
#define vb vector<bool>
#define vpll vector<pair<ll,ll>>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vvll vector<vector<ll>>
#define vvi vector<vector<int>>
#define vvvll vector<vector<vector<ll>>>
#define pll pair<ll,ll>
#define vs vector<string>
#define vvpll vector<vector<pair<ll, ll>>>
#define vvpi vector<vector<pair<int, int>>>
#define pb push_back
#define pob pop_back()
#define MOD (ll)(1e9 + 7)
#define MOD2 (ll)(998244353)
#define INF (ll)(1e18 + 5)
#define count1(n) __builtin_popcountll(n)
#define test ll t; cin>>t; while(t--)
#define all(x) begin(x), end(x)
using namespace std;
void inline show(vll &a){for(ll e: a){if(e == INF) cout<<"INF ";else cout<<e<<" ";}cout<<"\n";}
template<typename T> void enter(vector<T> &a){ for(T &e: a) cin>>e; }
ll inline mo(ll a){ return a%MOD;}
ll inline po(ll x, ll y, ll p)
{
ll res = 1; x = x % p;
while (y > 0) { if (y & 1) res = (res * x) % p; y >>= 1; x = (x * x) % p; }
return res%p;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin>>n;
vector<pair<pll, ll>> a(n), ma;
for(ll i=0;i<n;i++)
{
ll x ,y;
cin>>x>>y;
a[i] = {{x, y}, i};
}
sort(all(a));
ma.pb({
{min(a[0].second, a[n-1].second), max(a[0].second, a[n-1].second)}
, a[n - 1].first.first - a[0].first.first});
ma.pb({
{min(a[0].second, a[n-2].second), max(a[0].second, a[n-2].second)}
, a[n - 2].first.first - a[0].first.first});
ma.pb({
{min(a[1].second, a[n-1].second), max(a[1].second, a[n-1].second)}
, a[n - 1].first.first - a[1].first.first});
sort(all(a), [&](pair<pll, ll> a, pair<pll, ll> b){
return a.first.second < b.first.second;
});
ma.pb({
{min(a[0].second, a[n-1].second), max(a[0].second, a[n-1].second)}
, a[n - 1].first.second - a[0].first.second});
ma.pb({
{min(a[0].second, a[n-2].second), max(a[0].second, a[n-2].second)}
, a[n - 2].first.second - a[0].first.second});
ma.pb({
{min(a[1].second, a[n-1].second), max(a[1].second, a[n-1].second)}
, a[n - 1].first.second - a[1].first.second});
sort(all(ma), [&](pair<pll, ll> a, pair<pll, ll> b){
return a.second > b.second;
});
for(ll i=1;i<ma.size();i++)
{
if(ma[i].first != ma[i - 1].first)
{
cout<<ma[i].second<<"\n";
return 0;
}
}
return 0;
}
| #pragma GCC optimize(3)
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <algorithm>
#define maxn 200010
using namespace std;
int read()
{
int x=0,w=1;
char ch=getchar();
while((ch>'9'||ch<'0')&&(ch!='-')) ch=getchar();
if(ch=='-')
{
w=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=(x<<1)+(x<<3)+ch-'0';
ch=getchar();
}
return x*w;
}
int n;
struct Code
{
int l,r,num;
}code[maxn];
struct Heap
{
int l,r,num;
}heap[maxn];
priority_queue <int> q;
int ans,res;
bool cmpl(Code x,Code y)
{
return x.l<y.l;
}
bool cmpr(Heap x,Heap y)
{
return x.r<y.r;
}
int main()
{
n=read();
for(register int i=1;i<=n;i++)
{
code[i].l=read();
code[i].r=read();
code[i].num=i;
heap[i].l=code[i].l;
heap[i].r=code[i].r;
heap[i].num=i;
}
sort(code+1,code+n+1,cmpl);
sort(heap+1,heap+n+1,cmpr);
if(code[n].l-code[1].l>code[n].r-code[1].r) q.push(code[n].l-code[1].l);
if(code[n-1].l-code[1].l>code[n-1].r-code[1].r) q.push(code[n-1].l-code[1].l);
if(code[n].l-code[2].l>code[n].r-code[2].r) q.push(code[n].l-code[2].l);
if(heap[n].r-heap[1].r>=heap[n].l-heap[1].l) q.push(heap[n].r-heap[1].r);
if(heap[n-1].r-heap[1].r>=heap[n-1].l-heap[1].l) q.push(heap[n-1].r-heap[1].r);
if(heap[n].r-heap[2].r>=heap[n].l-heap[2].l) q.push(heap[n].r-heap[2].r);
q.pop();
cout<<q.top();
return 0;
} |
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#define l(x) (x<<1)
#define r(x) ((x<<1)|1)
#define IL inline
#define reg register
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
LL b,c,x,y,ans;
IL LL Min(LL a,LL b){return (a<b)?a:b;}
IL LL Max(LL a,LL b){return (a>b)?a:b;}
IL LL read(){
LL p=0,f=1; char c=getchar();
while (c<48||c>57) {if (c=='-') f=-1; c=getchar();}
while (c>=48&&c<=57) p=(p<<1)+(p<<3)+c-48,c=getchar();
return p*f;
}
int main(){
#ifdef __Marvolo
freopen("zht.in","r",stdin);
freopen("zht.out","w",stdout);
#endif
b=read(); c=read();
if (!c) {
puts("1");
return 0;
}
if (!b){
ans=c/2+(c-1)/2+1;
cout<<ans<<endl;
return 0;
}
if (b>0){
if (2*b<=c) ans=1; // 0
ans+=Min(c/2+1,b);
ans+=Min((c-1)/2+1,b);
c--;
if (!c) {
cout<<ans<<endl;
return 0;
}
ans+=c/2;
ans+=(c-1)/2;
cout<<ans<<endl;
} else {
b=-b; c--;
if (2*b<=c) ans=1; // 0
ans++;
ans+=Min(c/2+1,b);
ans+=Min((c-1)/2+1,b);
if (!c) {
cout<<ans<<endl;
return 0;
}
ans+=c/2;
ans+=(c-1)/2;
cout<<ans<<endl;
}
return 0;
} | /* CREATED BY
STREAM_CIPHER
june-2021
*/
#include<bits/stdc++.h>
using namespace std;
void __print(long long x) {cerr << x;}void __print(unsigned long long 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...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#define int long long int
#define double long double
#define fix_precision(n) cout<<fixed<<setprecision(n)
#define all(a) a.begin(),a.end()
const double pi=acos(-1.0);
int inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
// const int mod=998244353;
const int mx=5*1000000;//5*64M bit ->5*8M byte ->40MB size for long long int (64 bit)
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
double a,b;
cin>>a>>b;
cout<<(b/100.00)*a<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<n-1<<endl;
return 0;
}
| //yukicoder@cpp14
//coder:luckYrat(twitter:@luckYrat_)
//競技プログラミングから逃げるな
//https://www.youtube.com/watch?v=z_B9iJ8r4ic
//せんげん!
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <cctype>
#include <utility>
#include <climits>
//なまえくーかん!
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
//てーすう!
const int mod = 1000000007;
const int inf = (1<<30)-1;
const ll linf = (1LL<<62LL)-1;
const double EPS = (1e-10);
//でふぁいん!
#define anyfill(n,s) setw(n) << setfill(s)
#define loop(s) for(int i = 0; s > i; i++)
#define rep(i,q) for(int i = 0; (q) > i; i++)
#define repp(i,n,q) for(int i = n; (q) > i; i++)
#define dep(i,q) for(int i = (q); 0 < i; i--)
//みじかく!
#define pb push_back
#define mkp make_pair
#define fir first
#define scn second
#define ednl endl
//いぇすのー!
#define YesNo(a) (a?"Yes":"No")
#define YESNO(a) (a?"YES":"NO")
#define yesno(a) (a?"yes":"no")
//きんぼーnほーこー!!
P ar4[4] = {mkp(0,1),mkp(0,-1),mkp(1,0),mkp(-1,0)};
P ar8[8] = {mkp(-1,-1),mkp(-1,0),mkp(-1,1),mkp(0,-1),mkp(0,1),mkp(1,-1),mkp(1,0),mkp(1,1)};
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる
計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/
__attribute__((constructor))
void initial() {
cin.tie(0);
ios::sync_with_stdio(false);
}
int main(){
int n;cin>>n;
cout << n-1 << endl;
return 0;
} |
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }
#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DominatorTree.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "Factorization.h"
#include "FFT.h"
#include "Field.h"
#include "FlatTree.h"
#include "FlowSolver.h"
#include "Geometric2D.h"
#include "Geometric2DFloat.h"
#include "Geometric3D.h"
#include "Geometric3DFloat.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "HLD.h"
#include "Interpolation.h"
#include "IntMod.h"
#include "KDTree.h"
#include "LazySegmentTree.h"
#include "LCA.h"
#include "LIS.h"
#include "List.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Quadratic.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RollingHash.h"
#include "RuntimeMod.h"
#include "SafeInt.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif
#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int mx = -INF;
for (int x : {a, b}) {
for (int y : {c, d}) {
mx = max(mx, x - y);
}
}
cout << mx << endl;
return 0;
}
| #include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<b-c<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
double N, D, H, maxi;
cin >> N >> D >> H;
// cout << " N " << N << " D " << D << " H " << H;
vector<double> d(N), h(N);
double a = H/D, ans;
// cout << " a " << a << " ans " << ans <<endl;
for(int i = 0; i < N; i++){
cin >> d.at(i) >> h.at(i);
if (a > (H-h.at(i))/(D-d.at(i))){
maxi = i;
a = (H-h.at(i))/(D-d.at(i));
// cout << " a " << a << " i " << i << " maxi " << maxi << endl;
}
}
ans = H - D * a;
cout << ans << endl;
}
| /* author: Kite_kuma
created: 2021.01.23 20:57:55 */
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int n, m, k;
cin >> n >> m >> k;
vector<int> furidashi_ni_modoru(n + m + 1);
{
vector<int> a;
rep(_, k) {
int b;
cin >> b;
furidashi_ni_modoru[b] = 1;
a.push_back(b);
}
rep(i, k - m + 1) {
if(a[i + m - 1] - a[i] == m - 1) {
cout << -1 << endl;
return 0;
}
}
}
// x := dp[i] ( = answer)
// dp[i] = a[i] * x + b[i]
vector<double> a(n + m + 1), b(n + m + 1);
double a_sum = 0, b_sum = 0;
for(int i = n - 1; i >= 0; i--) {
if(furidashi_ni_modoru[i]) {
a[i] = 1;
} else {
a[i] = a_sum / m;
b[i] = 1.0 + b_sum / m;
}
a_sum += a[i] - a[i + m];
b_sum += b[i] - b[i + m];
}
// x == a[0] * x + b[0]
cout << b[0] / (1.0 - a[0]) << '\n';
return 0;
}
|
#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <numeric>
#include <unordered_set>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
int main(){
int n,k,m; cin >> n >> k >> m;
vector<int> a(n-1);
for(int i=0;i<n-1;i++) cin >> a[i];
int sum=0;
for(int i=0;i<n-1;i++) sum += a[i];
int goal = m * n;
if( goal > sum + k ) cout << -1 << endl;
else cout << max(0,goal - sum) << endl;
} | #include<bits/stdc++.h>
using namespace std;
using ll = unsigned long long;
int main(){
int n,k,m;
while( cin >> n >> k >> m ){
int arr[n];
int sum = 0;
for( int i = 0; i < n-1; i++ ){
int a;
cin >> a;
sum += a;
}
bool flag = false;
for( int i = 0; i <= k; i++ ){
int temp = sum+i;
if( temp/n >= m ){
cout << i << endl;
flag = true;
break;
}
}
if( flag == false ){
cout << "-1\n";
}
}
}
|
//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include<algorithm>
#include<string>
#include <map>
#include <queue>
#include <stack>
#include<set>
#include<math.h>
#define DIV 998244353
#define INF 1e15
using namespace std;
using ll = long long;
using ldb = long double;
vector<string> s(3);
map<char, char> mp;
bool chk() {
vector<ll> N(3);
for (int i = 0; i < s[0].size(); i++) {
if (i == 0 && mp[s[0][i]] == '0')return false;
N[0] *= 10;
N[0] += mp[s[0][i]] - '0';
}
for (int i = 0; i < s[1].size(); i++) {
if (i == 0 && mp[s[1][i]] == '0')return false;
N[1] *= 10;
N[1] += mp[s[1][i]] - '0';
}
for (int i = 0; i < s[2].size(); i++) {
if (i == 0 && mp[s[2][i]] == '0')return false;
N[2] *= 10;
N[2] += mp[s[2][i]] - '0';
}
//cout << N[0] << endl;
//cout << N[1] << endl;
//cout << N[2] << endl;
if (N[0] + N[1] == N[2]) {
cout << N[0] << endl;
cout << N[1] << endl;
cout << N[2] << endl;
return true;
}
else return false;
}
int main() {
cin >> s[0] >> s[1] >> s[2];
vector<char> c;
map<char, bool> b;
for (int i = 0; i < s[0].size(); i++) {
if (!b[s[0][i]]) {
c.push_back(s[0][i]);
b[s[0][i]] = true;
}
}
for (int i = 0; i < s[1].size(); i++) {
if (!b[s[1][i]]) {
c.push_back(s[1][i]);
b[s[1][i]] = true;
}
}
for (int i = 0; i < s[2].size(); i++) {
if (!b[s[2][i]]) {
c.push_back(s[2][i]);
b[s[2][i]] = true;
}
}
int kind = b.size();
if (kind > 10) {
cout << "UNSOLVABLE" << endl;
return 0;
}
vector<int> P(10);
for (int i = 0; i < 10; i++) { P[i] = i; }
do {
for (int i = 0; i < kind; i++) {
mp[c[i]] = P[i] + '0';
}
if (chk()) {
return 0;
}
} while (next_permutation(P.begin(), P.end()));
cout << "UNSOLVABLE" << endl;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL<<60;
const double PI = acos(-1.0);
/*const double PI = atan2(0.0,-1.0)*/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep1(i,aa,n) for(int i = aa; i <= (int)(n); i++)
#define ALL(a) (a).begin(),(a).end()
#define ar_init(aa,val) rep(i,aa.size()) aa[i]=val;
using pii = pair<ll,ll>;
using vec2d = vector<vector<ll>>;
ll mo=998244353;
ll gg(ll N,ll n){
if(n==0) return 1;
ll pp,hh;
pp=n/2;
hh=n%2;
int tt=gg(N,n/2);
if(hh==0)return tt%mo*tt%mo;
else {return tt%mo*tt%mo*N%mo;}
}
int main(){
//cout<<fixed<<setprecision(15);
ll nn,mm,kk;
cin>>nn>>mm>>kk;
if(nn==1&&mm==1){
cout<<kk<<endl;
return 0;
}
if(nn==1){
cout<<gg(kk,mm)<<endl;
return 0;
}
if(mm==1){
cout<<gg(kk,nn)<<endl;
return 0;
}
ll sd=0;
vector<ll> df1(kk+1);
df1[0]=0;
rep1(i,1,kk){
sd+=df1[i-1];
df1[i]=gg(i,nn)-gg(i-1,nn);
}
ll ans=0;
rep1(i,1,kk){
ans+=df1[i]*gg(kk-i+1,mm)%mo;
ans%=mo;
}
cout<<(ans+mo)%mo<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define arep(i,x,n) for(int i=int(x);i<(int)(n);i++)
#define rep(i,n) for(long long i = 0;i < n;++i)
#define rrep(i,n) for(int i=int(n-1);i>=0;i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define coy cout<<"Yes"<<endl
#define con cout<<"No"<<endl
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9+7
#define LINF 1e18+10
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
using PIP = pair<int, P>;
using Ps = pair<int, string>;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T>
bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; }
template <typename T>
bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; }
//const ll mod=998244353;
const ll mod = 1e9 + 7;
const ll MAX = 300000;
template <typename T>
T abs(T a) { if (a < 0)return -a; else return a; }//2020/09/30 stdlib has abs(long) abs(long long) error
//////////////////////////////////////
int main(){
int n;
cin>>n;
vl x(n);
rep(i,n)cin>>x[i];
ll a,b,c;
a=0;
rep(i,n)a+=abs(x[i]);
b=0;
rep(i,n)b+=x[i]*x[i];
c=-1;
rep(i,n)chmax(c,abs(x[i]));
cout<<a<<endl;
printf("%.10lf\n",sqrt(b));
cout<<c<<endl;
return 0;
} | ///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
#define in(x) scanf("%lld",&x)
#define in2(x,y) scanf("%lld %lld",&x,&y)
#define in3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define out(x) printf("%lld\n",x)
#define cy printf("YES\n")
#define cn printf("NO\n")
#define inf 1e18
#define neg -1e18
#define mxx 2111111
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define si second
#define ce cout<<endl
#define pb push_back
#define vc(x) x.begin(),x.end()
#define pb push_back
#define pi pair<ll,ll>
#define debug printf("ok\n");
#define pg pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> >
#define mod 1000000007
int main(){
ll i,j,s=0,n,mx=-999999999;
double sum=0.0,ans=0.0;
in(n);
ll a[n+9];
for(i=0;i<n;i++){
cin>>j;
s+=abs(j);
sum+=abs(j*j) ;
mx=max(mx,abs(j));
}
ans=sqrt(sum);
cout<<s<<endl;
cout<<setprecision(15)<<fixed <<ans<<endl;
cout<<mx<<endl;
}
|
#include<bits/stdc++.h>
#define LL long long
#define rint register int
#define LB lower_bound
#define UB upper_bound
#define MS(x,y) memset(x,y,sizeof(x))
#define rep(i,a,b) for(rint i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(rint i=a,i##end=b;i>=i##end;--i)
#define cms printf("%.2lf\n",(&o2-&o1)/1024.0/1024);
using namespace std;bool o1;
char IO;
inline int Rd(){
int _s_=0;bool _f_=0;
while(!isdigit(IO=getchar()))
(IO=='-')&&(_f_=1);
do _s_=(_s_<<1)+(_s_<<3)+(IO^48);
while(isdigit(IO=getchar()));
return _f_?-_s_:_s_;
}
const int P=1e9+7,n2=5e8+4;
int n,m,k,n2m,a1,b1,in[104];
void cmod(int &x){
x>=P&&(x-=P);
}
int ksm(int x){
int rst=1;
for(rint y=P-2;y;y>>=1,x=1ll*x*x%P)
(y&1)&&(rst=1ll*rst*x%P);
return rst;
}
struct node{
int a[105][105];
node(){MS(a,0);}
node operator * (const node &_) const{
node rst;
rep(i,1,n)
rep(j,1,n)
rep(l,1,n)
cmod(rst.a[i][j]+=1ll*a[i][l]*_.a[l][j]%P);
return rst;
}
}st,nw;
bool o2;int main(){
n=Rd(),m=Rd(),k=Rd();
n2m=1ll*ksm(m)*n2%P;
rep(i,1,n)
st.a[1][i]=Rd();
rep(i,1,m){
in[a1=Rd()]++,in[b1=Rd()]++;
nw.a[a1][b1]=nw.a[b1][a1]=n2m;
}
rep(i,1,n)
cmod(nw.a[i][i]=P+1-1ll*in[i]*n2m%P);
rep(i,0,29){
if(k&(1<<i))
st=st*nw;
nw=nw*nw;
}
rep(i,1,n)
printf("%d\n",st.a[1][i]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const ll INF = (ll)(1e10 + 5);
const ll MAX = (ll)(2e5 + 5);
const ll mult = (ll)(1e4);
double xd, yd, rd;
ll x, y, r;
bool check(ll next, ll width_2_pow) {
return next * next <= width_2_pow;
}
ll mod_floor(ll right, ll mod) {
if (right >= 0) {
return right - (right % mod);
} else {
return right - (right % mod) - ((right % mod) == 0LL ? 0LL : mod);
}
}
ll mod_ceil(ll left, ll mod) {
if (left >= 0) {
return left - (left % mod) + ((left % mod) == 0LL ? 0LL : mod);
} else {
return left - (left % mod);
}
}
ll count_for(ll length) {
ll width_2_pow = r * r - length * length;
ll ok = 0LL;
ll ng = MAX * mult;
while (abs(ok - ng) > 1) {
ll next = (ok + ng) / 2LL;
if (check(next, width_2_pow))
ok = next;
else
ng = next;
}
ll left = mod_ceil(x - ok, mult);
ll right = mod_floor(x + ok, mult);
return (right - left) / mult + 1LL;
}
int main(void) {
// Here your code !
scanf("%lf %lf %lf", &xd, &yd, &rd);
x = (ll)(round(xd * (double)(mult)));
y = (ll)(round(yd * (double)(mult)));
r = (ll)(round(rd * (double)(mult)));
ll bottom = (ll)(ceil(yd - rd)) * mult;
ll top = (ll)(floor(yd + rd)) * mult;
// printf("%lld, %lld\n", bottom, top);
ll ans = 0LL;
for (ll i = bottom; i <= top; i += mult) {
// printf("i: %lld - ", i);
ans += count_for(abs(y - i));
}
printf("%lld\n", ans);
return 0;
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}
const ll INF=1e18;
const int mx=200005;
//abc197
struct edge{
int to;
char C;
};
int main(){
//オーバーフローは大丈夫ですか??
cin.tie(0);ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
V<V<edge>> G(n);
rep(i,m){
int a,b;
char c;
cin>>a>>b>>c;
a--;b--;
G[a].pb({b,c});
G[b].pb({a,c});
}
V<V<ll>> dp(n,V<ll>(n,INF));
/*function<ll(int,int,int,int)> rec=[&](int u,int v,int p1,int p2){
//cout<<u<<' '<<v<<endl;
if(u==v){
//cout<<"="<<endl;
dp[u][v]=dp[v][u]=0;
return 0LL;
}
int f=0;
for(edge e:G[v]){
if(e.to==u) f=1;
}
if(f){
dp[u][v]=dp[v][u]=1;
return 1LL;
}
ll &res=dp[u][v];
if(res<INF) return res;
for(edge e1:G[u]){
for(edge e2:G[v]){
if(e1.C==e2.C){
if(!(e1.to==p1&&e2.to==p2))res=min(res,2LL+rec(e1.to,e2.to,u,v));
//res=min(res,2LL+dp[e2.to][e1.to]);
}
}
}
//dp[v][u]=res;
return res;
};
ll ans=rec(0,n-1,-1,-1);
if(ans==INF) ans=-1;
out(ans);*/
rep(i,n){
for(edge e1:G[i]){
for(edge e2:G[i]){
if(e1.C==e2.C){
dp[e1.to][e2.to]=2;
dp[e2.to][e1.to]=2;
}
}
}
}
rep(i,n) dp[i][i]=0;
rep(i,n){
for(edge e:G[i]) dp[i][e.to]=dp[e.to][i]=1;
}
priority_queue<pair<ll,P>,V<pair<ll,P>>,greater<pair<ll,P>>> que;
rep(i,n){
rep(j,n){
if(dp[i][j]<INF) que.push({dp[i][j],{i,j}});
}
}
while(que.size()){
pair<ll,P> p=que.top();
que.pop();
int u=p.sc.ft;
int v=p.sc.sc;
for(edge e1:G[u]){
for(edge e2:G[v]){
if(e1.C==e2.C){
if(dp[e1.to][e2.to]>dp[u][v]+2){
dp[e1.to][e2.to]=dp[u][v]+2;
que.push({dp[e1.to][e2.to],{e1.to,e2.to}});
}
}
}
}
}
ll ans=dp[0][n-1];
if(ans==INF) ans=-1;
out(ans);
}
//ペナルティ出しても焦らない ACできると信じろ!!!
//どうしてもわからないときはサンプルで実験 何か見えてくるかも
//頭で考えてダメなら紙におこせ!! | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<vector<int>>> adj_list(n, vector<vector<int>>(26));
vector<pair<int, int>> edges(m);
for (int i = 0; i < m; ++i) {
int a, b;
char cc;
cin >> a >> b >> cc;
int c = (int) (cc - 'a');
--a;
--b;
adj_list[a][c].push_back(b);
adj_list[b][c].push_back(a);
edges[i] = {a, b};
}
vector<int> dist(n*n, -1);
queue<pair<int, int>> q;
q.push({0, n - 1});
dist[0 * n + (n - 1)] = 0;
while (!q.empty()) {
auto [u, v] = q.front();
q.pop();
int d = dist[u * n + v];
for (int c = 0; c < 26; ++c) {
for (int u1 : adj_list[u][c]) {
for (int v1 : adj_list[v][c]) {
int w = u1 * n + v1;
if (dist[w] == -1) {
dist[w] = d + 1;
q.push({u1, v1});
}
}
}
}
}
int best = numeric_limits<int>::max();
for (int i = 0; i < n; ++i) {
if (dist[i * n + i] != -1) {
best = min(best, dist[i * n + i] * 2);
}
}
for (auto [u, v] : edges) {
if (dist[u * n + v] != -1) {
best = min(best, dist[u * n + v] * 2 + 1);
}
if (dist[v * n + u] != - 1) {
best = min(best, dist[v * n + u] * 2 + 1);
}
}
if (best != numeric_limits<int>::max()) {
cout << best << "\n";
} else {
cout << "-1\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
} |
// Powered by CP Editor (https://cpeditor.org)
//a+b = (a^b) + 2*(a&b)
//b^c = (a^b)^(a^c)
//gcd(x,y) = gcd(x-y,y)
//if n = 5000 then complexity cannot be (n^2*log(n)) means no map ,no sets
//check for long long overflow if input is large
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define mod 1000000007
// #define mod 998244353
#define pb push_back
#define ll long long
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define mii map<int,int>
#define loop(i,n) for(int i=0;i<n;i++)
#define pp ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define fill(a,b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define en cout<<"\n"
#define trace(x) cout<<#x<<": "<<x<<" "<<endl
#define trace2(x,y) cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x,y,z) cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a,b,c,d) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
const long long INF = 2000000000000000000;
long long power(long long a,long long b){
long long ans=1;
while(b>0){
if(b&1){ans=(ans*a)%mod;}
a=(a*a)%mod;
b>>=1;
}
return ans;
}
#define M 2000005
int fact[M],fact_inv[M];
void pre(){
fact[0]=1;
fact_inv[0]=1;
for(int i=1;i<M;i++){
fact[i]=fact[i-1]*i;
fact[i]%=mod;
fact_inv[i]=power(fact[i],mod-2);
}
}
long long ncr(long long n,long long r){
if(n<r){return 0;}
if(n==r||r==0){return 1;}
return (((fact[n]*fact_inv[n-r])%mod)*fact_inv[r])%mod;
//return x;
}
void solve()
{
int n,m,k;
cin>>n>>m>>k;
int ans = ncr(n+m,n);
if(n>m+k)
{
cout<<0;
return;
}
int x = ncr(n+m,m+k+1);
cout<<(ans - x + mod)%mod;
}
int32_t main()
{
pp;
pre();
int test=1;
// cin>>test;
while(test--)
{
solve();
en;
}
return 0 ;
} | #include <algorithm>
#include <iostream>
using namespace std;
const int MD = 1000000007;
long long inv(int n) {
return n == 1 ? 1 : inv(n - MD % n) * (MD / n + 1) % MD;
}
long long choose(int n, int k) {
return n < k ? 0 : k == 0 ? 1 : choose(n - 1, k - 1) * n % MD * inv(k) % MD;
}
int main() {
int n, m, k; cin >> n >> m >> k;
if (n - m >= k + 1) {
cout << "0\n";
return 0;
}
int ans = choose(n + m, n) - choose(n + m, n + (k + 1 - (n - m)));
if (ans < 0)
ans += MD;
cout << ans << '\n';
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <complex>
#include <vector>
#include <limits>
#include <iomanip>
#include <cassert>
#include <numeric>
#include <chrono>
#include <random>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << " = " << (x) << endl;
#define debug_pii(x) cerr << "(" << x.first << "," << x.second << ")";
#define rep(i, n) for(int i = 0;i < n;i++)
#define repr(i, n) for(int i = n-1;i >= 0;i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;
const int nmax = 18;
const int inf = 30;
int dp[1 << nmax];
int g[nmax][nmax];
int n, e, a, b;
bool fullc(int mask) {
rep(i, n) {
rep(j, n) {
if(i == j) continue;
int ii = mask & (1 << i);
int jj = mask & (1 << j);
if(ii > 0 && jj > 0) {
if(g[i][j] == 0) {
return false;
}
}
}
}
return true;
}
void solve() {
cin >> n >> e;
rep(i, e) {
cin >> a >> b;
a--;
b--;
g[a][b] = g[b][a] = 1;
}
for(int mask = 1;mask < (1 << n);mask++) {
if(fullc(mask)) {
dp[mask] = 1;
continue;
}
dp[mask] = inf;
for(int j = mask;j > 0;j = (j-1) & mask) {
dp[mask] = min(dp[j] + dp[mask ^ j], dp[mask]);
}
}
cout << dp[(1 << n)-1] << endl;
}
int main() {
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
// cout << fixed << setprecision(15);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
| #include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = long long; using ull = unsigned long long; using pll = pair<ll,ll>; const ll INF = 4e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
// clang-format on
vector<ll> nodes(19);
vector<vector<ll>> nodesset((1ULL << 19) + 5);
ll n, m;
vector<ll> memo2(500000, -1);
// 確定済みを s とするbitDP
ll dfs2(ll s) {
if (s == (1ULL << n) - 1) {
return 0;
}
if (memo2[s] >= 0) {
return memo2[s];
}
ll ans = INF;
auto task = stack<ll>();
for (ll u = 0; u < n; u++) {
ll ub = (1ULL << u);
if (ub & s) continue;
task.push(ub);
}
set<ll> done;
while (!task.empty()) {
ll t = task.top();
task.pop();
if (done.count(t)) continue;
done.insert(t);
bool added = false;
for (auto v : nodesset[t]) {
ll vb = (1ULL << v);
if (vb & s) continue;
if (vb & t) continue;
task.push(t | vb);
added = true;
}
// 次の状態の探索は、拾える連結成分を全部拾ったときだけ
if (!added) ans = min(ans, 1 + dfs2(t | s));
}
return memo2[s] = ans;
}
int main() {
cin >> n >> m;
for (ll i = 0; i < m; i++) {
ll a, b;
cin >> a >> b;
a--;
b--;
nodes[a] |= (1ULL << b);
nodes[b] |= (1ULL << a);
}
for (ll i = 0; i < (1ULL << n); i++) {
for (ll j = 0; j < n; j++) {
ll nd = nodes[j];
if ((nd & i) == i) {
nodesset[i].push_back(j);
}
}
}
print(dfs2(0));
}
|
#include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define debug(x) cout << #x << ": " << (x) << endl
#else
#define debug(x)
#endif
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int maxn=1<<20,inf=0x3f3f3f3f,mod=1e9+7;
int f[maxn],cnt[maxn],pop[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
for(int i=0,u,v;i<m;++i)
{
cin>>u>>v;
u--;
v--;
for(int j=0;j<(1<<n);++j) if(j&(1<<u) && (j&(1<<v))) cnt[j]++;
}
for(int i=1;i<(1<<n);++i) pop[i]=pop[i>>1]+(i&1);
memset(f,0x3f,sizeof(f));
f[0]=0;
for(int i=0;i<(1<<n);++i)
{
int c=pop[i];
for(int j=i;j;j=(j-1)&i)
{
if(pop[j]*(pop[j]-1)/2==cnt[j]) f[i]=min(f[i],f[i^j]+1);
}
}
cout<<f[(1<<n)-1]<<'\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }
class ChromaticrNumber {
public:
int V;
vector<int> adj;
const static int MOD = 1000000007;
ChromaticrNumber(int node_size) : V(node_size), adj(V, 0) {
for (int i = 0; i < V; i++) {
adj.at(i) = (1 << i);
}
}
void add(int u, int v) {
adj.at(u) |= (1 << v), adj.at(v) |= (1 << u);
}
int solve() {
vector<int> t(1 << V, 0), I(1 << V, 0);
t.at((1 << V) - 1) = I.at(0) = 1;
for (int i = 1; i < (1 << V); i++) {
int v = __builtin_ctz(i);
I.at(i) = I.at(i ^ (1 << v)) + I.at(i & (~adj.at(v)));
I.at(i) = (I.at(i) >= MOD) ? (I.at(i) - MOD) : I.at(i);
t.at(i - 1) = (((V - __builtin_popcount(i - 1)) % 2) ? (MOD - 1) : 1);
}
for (int k = 1; k < V; k++) {
long res = 0;
for (int i = 0; i < (1 << V); i++) {
res += (t.at(i) = (long) t.at(i) * I.at(i) % MOD);
}
if (res % MOD) return k;
}
return V;
}
};
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<pair<int, int>> V(M);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b, a--, b--;
if (a > b) swap(a, b);
V.at(i) = {a, b};
}
set<pair<int, int>> SE;
for (const auto& [a, b] : V) SE.insert({a, b});
ChromaticrNumber CN(N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
if (SE.count({j, i})) continue;
CN.add(j, i);
}
}
cout << CN.solve() << '\n';
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<double> a(n),b(n),c(n),d(n);
for(int _=0; _<2; _++){
for(int i=0;i<n;i++) cin>>a[i]>>b[i];
int x=0,y=0;
for(int i=0;i<n;i++){
x+=a[i];
y+=b[i];
a[i]*=n;
b[i]*=n;
}
for(int i=0;i<n;i++){
a[i]-=x;
b[i]-=y;
}
swap(a,c);
swap(b,d);
}
for(int i=0;i<n;i++){
if(a[i]!=0||b[i]!=0){
swap(a[i],a[0]);
swap(b[i],b[0]);
}
}
string ans="No";
const double eps=1e-6;
for(int i=0;i<n;i++){
double angle=atan2(d[i],c[i])-atan2(b[0],a[0]);
bool flag=true;
for(int j=0;j<n;j++){
double A=a[j]*cos(angle)-b[j]*sin(angle);
double B=a[j]*sin(angle)+b[j]*cos(angle);
bool flag2=false;
for(int k=0;k<n;k++){
if(abs(A-c[k])<=eps&&abs(B-d[k])<=eps) flag2=true;
}
flag&=flag2;
}
if(flag) ans="Yes";
}
cout<<ans;
} | #include<bits/stdc++.h>
#define Woody
#define int long long
#define rep(n) for(int i=0;i<n;i++)
#define mp make_pair
#define eb emplace_back
#define F first
#define S second
#ifdef Woody
#define quick ios::sync_with_stdio(0);cin.tie(0);
#else
#define quick
#endif
#define INF INT64_MAX
#define PI 3.14159265
using namespace std;
const double eps=1e-3;
typedef pair<double,double> p;
bool ok(int n,vector<p> v1,vector<p> v2){
// cout<<"OK\n";
sort(v2.begin(),v2.end());
/* rep(n){
printf("%.5lf %.5lf\n",v1[i].F,v1[i].S);
}//cout<<"\n";
rep(n){
printf("%.5lf %.5lf\n",v2[i].F,v2[i].S);
}cout<<"\n";*/
double disx=v1[0].F-v2[0].F;
double disy=v1[0].S-v2[0].S;
for(int i=1;i<n;i++){
double dx=v1[i].F-v2[i].F;
double dy=v1[i].S-v2[i].S;
if(abs(disx-dx)>eps||abs(disy-dy)>eps) return false;
}
return true;
}
bool solve(){
int n;
cin>>n;
vector<p> v1(n);
vector<p> v2(n);
rep(n){
cin>>v2[i].F>>v2[i].S;
}
rep(n){
cin>>v1[i].F>>v1[i].S;
}
sort(v1.begin(),v1.end());
vector<p> v3(n);
for(double d=0;d<=360;d+=0.0005){
double s=sin(d*PI/180.0);
double c=cos(d*PI/180.0);
rep(n){
double x=v2[i].F;
double y=v2[i].S;
v3[i].F=x*c+y*(-1)*s;
v3[i].S=x*s+y*c;
}
if(ok(n,v1,v3)) return true;
}
return false;
}
signed main(){
quick
bool f=solve();
if(f) cout<<"Yes\n";
else cout<<"No\n";
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vbl;
typedef vector<vector<bool>> vvbl;
void INX(){}
template<typename Head, typename... Tail>
void INX(Head&& head, Tail&&... tail)
{
cin >> head;
INX(forward<Tail>(tail)...);
}
void OUTX(){}
template<typename Head, typename... Tail>
void OUTX(Head&& head, Tail&&... tail)
{
cout << head << endl;
OUTX(forward<Tail>(tail)...);
}
void OUTX2(){cout << endl;}
template<typename Head, typename... Tail>
void OUTX2(Head&& head, Tail&&... tail)
{
cout << head << ' ';
OUTX2(forward<Tail>(tail)...);
}
#define ADD emplace_back
#define MP make_pair
#define __LONG_LONG_MIN__ (-__LONG_LONG_MAX__ - 1)
//#define MOD 1000000007 // 10^9 + 7
//setprecision(10)
ll gcd(ll a, ll b)
{
if(a < b) swap(a, b);
while(b > 0)
{
a %= b;
swap(a, b);
}
return a;
}
int main()
{
ll N;
INX(N);
ll N2 = (1 << N) - 1;
vll chk(51, 51);
for (ll i = 0; i <= 50; i++)
{
chk[i] = i;
}
for (ll i = 2; i <= 50; i++)
{
ll i2 = i;
while(i2 <= 50)
{
chk[i2] = min(chk[i2], i);
i2 += i;
}
}
vll fact;
for (ll i = 2; i <= 50; i++)
{
if(chk[i] == i) fact.ADD(i);
}
vll X(N);
for (ll i = 0, ibit = 1; i < N; i++, ibit <<= 1)
{
INX(X[i]);
}
ll result = __LONG_LONG_MAX__;
for (ll i = 1, endi = 1 << fact.size(); i < endi; i++)
{
ll candi = 1;
for(ll j = 0, j2 = i; j2 > 0; j++, j2 >>= 1)
{
if(j2 & 1) candi *= fact[j];
}
bool isok = true;
for (ll j = 0; j < N; j++)
{
if(gcd(X[j], candi) == 1)
{
isok = false;
break;
}
}
if(isok)
{
result = min(result, candi);
}
}
OUTX(result);
return 0;
}
| #include<bits/stdc++.h>
#define fi first
#define se second
#define pii pair<double,double>
#define eps 1e-8
#define equals(a,b) fabs(a - b) < eps
using namespace std;
typedef long long ll;
ll rd(){
ll x;
scanf("%lld",&x);
return x;
}
double dis(pii a,pii b){
return sqrt((a.fi - b.fi) * (a.fi - b.fi) + (a.se - b.se) * (a.se - b.se));
}
pii c1,c2;
bool cmp1(pii a,pii b){
return atan2(a.se - c1.se,a.fi - c1.fi) < atan2(b.se - c1.se,b.fi - c1.fi);
}
bool cmp2(pii a,pii b){
return atan2(a.se - c2.se,a.fi - c2.fi) < atan2(b.se - c2.se,b.fi - c2.fi);
}
double cross(pii a,pii b){
return a.fi * b.se - a.se * b.fi;
}
double dot(pii a,pii b){
return a.fi * b.fi + a.se * b.se;
}
pii operator - (const pii &a,const pii &b){
return make_pair(a.fi - b.fi,a.se - b.se);
}
int main(){
int n = rd();
double mi1 = 1e9;
double mx1 = 0;
double mi2 = 1e9;
double mx2 = 0;
vector<pii> p1,p2;
vector<double> dis1,dis2;
for(int i = 1;i <= n;i++){
int a = rd();
int b = rd();
c1.fi += a;
c1.se += b;
p1.push_back(make_pair(a,b));
}
c1.fi /= n;
c1.se /= n;
if(n == 1) {
puts("Yes");
return 0;
}
for(int i = 1;i <= n;i++){
int a = rd();
int b = rd();
c2.fi += a;
c2.se += b;
p2.push_back(make_pair(a,b));
}
c2.fi /= n;
c2.se /= n;
for(int i = 0;i < n;i++){
p1[i].fi -= c1.fi;
p1[i].se -= c1.se;
p2[i].fi -= c2.fi;
p2[i].se -= c2.se;
}
for(int i = 0;i < n;i++){
if(p1[i].fi || p1[i].se) {
swap(p1[0].fi,p1[i].fi);
swap(p1[0].se,p1[i].se);
}
}
bool flag = false;
for(int i = 0;i < 1;i++){
for(int j = 0;j < n;j++){
double ang = atan2(p1[i].se,p1[i].fi) - atan2(p2[j].se,p2[j].fi);
int cnt = 0;
for(int k = 0;k < n;k++){
bool f = 0;
for(int kk = 0;kk < n;kk++){
double x = cos(ang) * p2[kk].fi - sin(ang) * p2[kk].se;
double y = sin(ang) * p2[kk].fi + cos(ang) * p2[kk].se;
f |= (equals(x,p1[k].fi) && equals(y,p1[k].se));
}
if(!f) break;
else cnt++;
}
flag |= (cnt == n);
}
}
if(flag) puts("Yes");
else puts("No");
}
|
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
#define sz 209
#define mx 10000000000008
#define mod 998244353
ll n,t,arr[sz][sz],brr[sz],crr[sz],a,k;
vector<ll>v[sz];
void bfs(ll node)
{
a++;
crr[node]=1;
for(int i=0;i<v[node].size();i++)
{
ll x=v[node][i];
if(!crr[x])
bfs(x);
}
return;
}
int main()
{
scanf("%lld %lld",&n,&k);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
scanf("%lld",&arr[i][j]);
}
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
ll cnt=0;
for(int x=1;x<=n;x++)
{
if(arr[i][x]+arr[j][x]>k)
cnt++;
}
if(!cnt){
v[i].push_back(j);
v[j].push_back(i);
//printf("%d %d\n",i,j);
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
ll cnt=0;
for(int x=1;x<=n;x++)
{
if(arr[x][i]+arr[x][j]>k)
cnt++;
}
if(!cnt){
v[i+n].push_back(j+n);
v[j+n].push_back(i+n);
// printf("%d %d\n",i,j);
}
}
}
brr[0]=1;
for(int i=1;i<=2*n;i++)
brr[i]=(brr[i-1]*i)%mod;
ll ans=1;
for(int i=1;i<=2*n;i++)
{
if(!crr[i])
{
a=0;
bfs(i);
ans=((ans*(brr[a]))%mod);
}
}
printf("%lld\n",ans);
return 0;
}
| //LYC_music yyds!
#include<bits/stdc++.h>
#define int long long
using namespace std;
int read()
{
int pos=1,num=0;
char ch=getchar();
while (!isdigit(ch))
{
if (ch=='-') pos=-1;
ch=getchar();
}
while (isdigit(ch))
{
num=num*10+(int)(ch-'0');
ch=getchar();
}
return pos*num;
}
void write(int x)
{
if (x<0)
{
putchar('-');
write(-x);
return;
}
if (x>=10) write(x/10);
putchar(x%10+'0');
}
void writesp(int x)
{
write(x);
putchar(' ');
}
void writeln(int x)
{
write(x);
putchar('\n');
}
const int N=3e5+1;
int n,fa[N],a[N],ans;
int find(int x)
{
if (fa[x]==x) return x;
return fa[x]=find(fa[x]);
}
signed main()
{
n=read();
for (int i=1;i<N;i++)
fa[i]=i;
for (int i=1;i<=n;i++)
a[i]=read();
for (int i=1;i<n-i+1;i++)
{
int x=a[i],y=a[n-i+1];
if (find(x)!=find(y))
{
ans++;
fa[fa[x]]=fa[y];
}
}
writeln(ans);
}
|
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include <bitset>
#include<cstring>
#define forn(i,a,n) for(int i=(a);i<(n);i++)
#define ford(i,a,d) for(int i=(a);i>(d);i--)
#define pb push_back
using namespace std;
const long long N=2e5+6;
const long long inf=0x3f3f3f3f;
typedef long long ll;
int a[N];
vector<int> adj[N];
int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,m;
cin>>n>>m;
forn(i,1,n+1) cin>>a[i];
forn(i,0,m)
{
int x,y;
cin>>x>>y;
adj[x].pb(y);
}
vector<int> dp(n+1,2e9);
int ans=-2e9;
forn(i,1,n+1)
{
ans=max(ans,a[i]-dp[i]);
for(auto j:adj[i])
{
dp[j]=min(dp[j],dp[i]);
dp[j]=min(dp[j],a[i]);
}
}
cout<<ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;
vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
vector<vector<ll>> matsum(vector<vector<ll>> A, vector<vector<ll>> B) {
if (A.size()!=B.size()||A[0].size()!=B[0].size()) return {{}};
ll h = A.size(), w = A[0].size();
vector<vector<ll>> result(h,vector<ll>(w,0));
rep(i,h) rep(j,w) result[i][j] = (A[i][j]+B[i][j])%MOD;
return result;
}
vector<vector<ll>> matdiff(vector<vector<ll>> A, vector<vector<ll>> B) {
if (A.size()!=B.size()||A[0].size()!=B[0].size()) return {{}};
ll h = A.size(), w = A[0].size();
vector<vector<ll>> result(h,vector<ll>(w,0));
rep(i,h) rep(j,w) result[i][j] = (MOD+(A[i][j]-B[i][j])%MOD)%MOD;
return result;
}
vector<vector<ll>> matdot(vector<vector<ll>> A, vector<vector<ll>> B) {
if (A[0].size()!=B.size()) return {{}};
ll h = A.size(), w = B[0].size(), x = A[0].size();
vector<vector<ll>> result(h,vector<ll>(w,0));
rep(i,h) rep(j,w) rep(k,x) result[i][j] = (result[i][j] + A[i][k]*B[k][j]);
return result;
}
vector<vector<ll>> matpow(vector<vector<ll>> A, ll n) {
if (A.size()!=A[0].size()) return {{}};
int N = A.size();
vector<vector<ll>> result(N,vector<ll>(N,0)), now;
copy(all(A),back_inserter(now));
rep(i,N) result[i][i] = 1;
ll p2 = 1;
while (n>=p2) {
if (n&p2) result = matdot(result,now);
now = matdot(now,now); p2 *= 2;
}
return result;
}
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
int main() {
ll N; cin >> N;
ll X[N], Y[N]; rep(i,N) cin >> X[i] >> Y[i];
ll M; cin >> M;
ll op[M][2] = {}; rep(i,M) { cin >> op[i][0]; if (op[i][0]>=3) cin >> op[i][1]; }
ll Q; cin >> Q;
ll A[Q], B[Q]; rep(i,Q) { cin >> A[i] >> B[i]; B[i]--; }
vector<vector<ll>> ope = { {1,0,0}, {0,1,0}, {0,0,1} };
vector<vector<vector<ll>>> opmat(M+1); opmat[0] = ope;
rep(i,M) {
vector<vector<ll>> mulmat;
if (op[i][0]==1) {
mulmat = {
{0,1,0},
{-1,0,0},
{0,0,1}
};
}
else if (op[i][0]==2) {
mulmat = {
{0,-1,0},
{1,0,0},
{0,0,1}
};
}
else if (op[i][0]==3) {
mulmat = {
{-1,0,2*op[i][1]},
{0,1,0},
{0,0,1}
};
}
else if (op[i][0]==4) {
mulmat = {
{1,0,0},
{0,-1,2*op[i][1]},
{0,0,1}
};
}
opmat[i+1] = matdot(mulmat,opmat[i]);
}
// rep(i,M+1) {
// for (auto x: opmat[i]) debug(all(x));
// cout << endl;
// }
rep(i,Q) {
vector<vector<ll>> resmat = matdot(opmat[A[i]],{{X[B[i]]},{Y[B[i]]},{1}});
cout << resmat[0][0] << " " << resmat[1][0] << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define MAX( a , b ) ( a > b ) ? a : b
#define MIN( a , b ) ( a < b ) ? a : b
using ull = unsigned long long;
using ll = long long;
using ul = unsigned long;
ull Euclidean(ull a, ull b){
if((a % b) == 0)
return b;
else
return Euclidean(b , (a % b));
}
int main(){
ull N;
ull result = 1;
ull sub[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
bool count[15] = {false};
ull c;
ull z;
bool r = false;
cin >> N;
vector<ull> x(N,0);
for (ull i = 0; i < N; i++)
cin >> x[i];
for (ull j = 0; j < 15; j++){
result *= sub[j];
}
for (ull i = 0; i < 32768; i++){
z = i;
c = 1;
r = true;
for (ull j = 0; j < 15; j++){
if((z % 2) == 1)
c *= sub[j];
z /= 2;
}
for(ull k = 0; k < N; k++){
if((Euclidean(MAX(c , x[k]) , MIN(c , x[k]))) == 1){
r = false;
break;
}
}
if(r)
result = MIN(result , c);
}
cout << result << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &x){
x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
x *= f;
}
inline void write(int x){if (x > 9) write(x/10); putchar(x%10+'0'); }
const int P = 998244353;
int n,a[105];
inline void upd(int &x,int v){ x = (x+v>=P)?(x+v-P):(x+v); }
int f[105][10005];
int s,fac[105],l;
inline void add(int n,int x){
register int i,j,v;
for (i = n; i ; --i) for (j = x; j <= s; ++j) upd(f[i][j],f[i-1][j-x]);
}
int main(){
int i;
cin >> n;
for (fac[0] = i = 1; i <= 100; ++i) fac[i] = fac[i-1] * 1ll * i % P;
for (i = 1; i <= n; ++i) cin >> a[i],s += a[i]; l = s/2;
if (s % 2 == 1){ cout << 0 << '\n'; return 0; }
f[0][0] = 1;
for (i = 1; i <= n; ++i) add(i,a[i]);
long long ans = 0;
for (i = 0; i <= n; ++i){
if (f[i][s / 2])
ans = (ans + 1ll * f[i][s/2] * fac[i] % P * fac[n-i] % P) % P;
}
cout << ans << '\n';
return 0;
} |
#include<cstdio>
#include<vector>
#include<algorithm>
typedef long long ll;
ll m,a[45];
std::vector<ll> f,g;
inline ll read() {
ll x=0,f=1;register char s=getchar();
while(s>'9'||s<'0') {if(s=='-') f=-1;s=getchar();}
while(s>='0'&&s<='9') {x=x*10+s-'0';s=getchar();}
return x*f;
}
inline ll max(const ll &x,const ll &y) {return x>y? x:y;}
void dfs(int L,int R,ll sum,std::vector<ll> &x) {
if(R==L) {x.push_back(sum);return;}
dfs(L+1,R,sum,x);
if(sum+a[L+1]<=m) dfs(L+1,R,sum+a[L+1],x);
}
int main() {
int n=read(),mid=n>>1;
ll res=0;m=read();
for(register int i=1;i<=n;++i) a[i]=read();
dfs(0,mid,0,f);dfs(mid,n,0,g);
std::sort(f.begin(),f.end());
std::sort(g.begin(),g.end());
for(register int i=0;i<f.size();++i) {
int pos=std::upper_bound(g.begin(),g.end(),m-f[i])-g.begin()-1;
if(pos<0||g[pos]+f[i]>m) continue;
res=max(res,f[i]+g[pos]);
}//res+=std::upper_bound(g.begin(),g.end(),m-f[i])-g.begin();
printf("%lld\n",res);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
string X;
ll M;
cin >> X >> M;
int n = X.size();
vector<ll> b(n);
ll m = 0;
for (int i = 0; i < n; i++) {
b[i] = X[i] - '0';
m = max(m, b[i]);
}
auto test = [&](ll N, ll k) { //转 k 进制
vector<ll> a(0);
while (N > 0) {
a.push_back((N % k));
N /= k;
}
reverse(a.begin(), a.end()); //正过来
if (a.size() < X.size())
return false;
else if (a.size() > X.size())
return true;
else {
int i = 0;
while (i<n&& a[i] == b[i]) {
++i;
}
if (i == n)
return true;
else if (a[i] < b[i])
return false;
else
return true;
}
};
if (n == 1)
cout << (b[0] <= M) ? '1': '0' ;
else if (!(test(M, m + 1)))
cout << 0 << endl;
else {
ll ok = m + 1;
ll ng = M + 1;
ll mid;
while (ng - ok > 1) {
mid = (ok + ng) / 2;
if (test(M, mid))
ok = mid;
else
ng = mid;
}
cout << ok - m << endl;
return 0;
}
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define ff first
#define ar array
#define ss second
#define ll long long int
#define mem(x) memset(x,0,sizeof(x))
#define setbits(x) __builtin_popcount(x)
#define ull unsigned long long int
#define all(c) (c).begin(),(c).end()
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define db double
#define endl "\n"
#define fbo(x) find_by_order(x)
#define ook(x) order_of_key(x)
using namespace __gnu_pbds;
using namespace std;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair<ll,ll > iil;
typedef pair<ll,int> li;
typedef pair<int,int> ii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
os1;
typedef tree<int, null_type, greater<int>, rb_tree_tag,
tree_order_statistics_node_update>
os2;
inline void fast()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
const ll pi=31;
const ll MOD[2] = {1187278649, 1149736019};//for double hashing
const ll mod=(ll)(1e9)+7;
ll pmod(ll x,ll n){
ll r=1;
while(n>0)
{
if(n&1LL)
r=(r*x)%mod;
x=(x*x)%mod;
n=n/2;
n%=mod;
}
return r;
}
ll pMod(ll x,ll n,ll mod)
{ ll r=1;
while(n>0)
{
if(n&1LL)
r=(r*x)%mod;
x=(x*x)%mod;
n=n/2;
n%=mod;
}
return r;
}
//think recursively also;
ll dp[18][(1<<18)];
void solve()
{
ll n;
cin>>n;
ar<ll,3>node[n+1];
for(int i=0;i<n;i++)
{
cin>>node[i][0]>>node[i][1]>>node[i][2];
}
ll dis[n+1][n+1];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
dis[i][j]=abs(node[i][0]-node[j][0])+abs(node[i][1]-node[j][1])+max(0LL,(node[j][2]-node[i][2]));
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<(1<<n);j++)
{
dp[i][j]=(ll)(1e18);
}
}
dp[0][1]=0;
for(int j=0;j<(1<<n);j++)
{
for(int i=0;i<(n);i++)
{
if(dp[i][j]==(ll)(1e18))
continue;
for(int k=0;k<n;k++)
{
if((j>>k)&1)
continue;
int x=(j|(1<<k));
dp[k][x]=min(dp[k][x],dp[i][j]+dis[i][k]);
}
}
}
ll cost=LLONG_MAX;
for(int i=1;i<n;i++)
{
cost=min(cost,dp[i][(1<<n)-1]+dis[i][0]);
}
cout<<cost<<endl;
}
int main()
{
fast();
// int t;
// cin>>t;
// for(int casee=1;casee<=t;casee++)
{//cout<<"Case #"<<casee<<": ";
solve();
}
} | #include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>
using namespace std;
typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;
const double PI=3.14159265358979323846;
#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))
#define rep(i, N) for(int i = 0; i < N; ++i)
#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL
#define IN(l, r, x) ((l) <= (x) && (x) < (r))
inline s64 dis(s64 x, s64 y, s64 z, s64 p, s64 q, s64 r)
{
return abs(x - p) + abs(y - q) + max(s64(0), r - z);
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vs64> g(n, vs64(n));
vs32 x(n), y(n), z(n);
rep (i, n)
{
cin >> x[i] >> y[i] >> z[i];
}
rep (i, n) for (int j = i + 1; j < n; ++j)
{
s64 d1 = dis(x[i], y[i], z[i], x[j], y[j], z[j]);
s64 d2 = dis(x[j], y[j], z[j], x[i], y[i], z[i]);
g[i][j] = d1;
g[j][i] = d2;
}
const s64 inf = 1e18;
vector< vs64 > dp(1 << n, vs64(n, inf));
dp[(1 << n) - 1][0] = 0;
for (int s = (1 << n) - 2; s >= 0; --s)
{
rep (v, n)
{
rep (u, n)
{
if (!(s >> u & 1))
{
dp[s][v] = min(dp[s][v], dp[s | (1 << u)][u] + g[u][v]);
}
}
}
}
cout << dp[0][0] << "\n";
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int pr[100009], sz[100009], d[100009];
vector<int> ch[100009];
void dfs(int n)
{
vector<int> v;
int s = 0;
sz[n] = 1; d[n] = 1;
for (int i = 0; i < ch[n].size(); i++) {
int tn = ch[n][i];
dfs(tn);
sz[n] += sz[tn];
d[n] += sz[tn] - d[tn];
int t = 2 * d[tn] - sz[tn];
if (sz[tn] & 1) v.push_back(t);
else if (t < 0) d[n] += t;
else s += t;
}
if (v.size() % 2 == 0) d[n] += s;
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i += 2)
d[n] += v[i];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
for (int i = 2; i <= n; i++) {
cin >> pr[i];
ch[pr[i]].push_back(i);
}
dfs(1);
cout << d[1] << '\n';
return 0;
} | #include <bits/stdc++.h>
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define lowbit(x) (x & -x)
#define FOR(i,a,b) for(int i=(a); i< (b); ++i)
#define RFOR(i,b,a) for(int i=(b);i>=(a);i--)
#define REP(i,a,b) for(int i=(a); i<=(b); ++i)
#define PI 3.14159265358979323846264338327950L
using namespace std;
typedef long long ll;
template<typename T>
void PrArr(const T a[] , int len){
for(int i = 0;i <= len; i++)cout << a[i] << " ";
cout << endl;
}
template<typename T>
void PrVec(const vector<T> a){
for(auto it : a)cout << it << " ";
cout << endl;
}
const int MAX = 0x7ffffff;
const int MIN = 0xcf;
int test;
void slove()
{
ll n, x = 1, t, len = 0;
cin >> n;
t = n;
while(t >= 10){
t /= 10;x *= 10;len ++;
} len ++;
ll ans = (n - x + 1) * ((len / 3) - (len % 3 == 0));
t = 0, n = 1, len = 0;
while(t <= x)
{
t = t * 10 + 9;
len ++;
if(t > x)break;
ans += (t - n + 1)* ((len / 3) - (len % 3 == 0));
//cout << t << " " << n << " " << len << endl;
n = n * 10;
}
cout << ans << endl;
}
int main()
{
#ifdef LOCAL
auto start_time = clock();
cerr << setprecision(3) << fixed; // 在iomanip中
#endif
SIS;slove();
#ifdef LOCAL
auto end_time = clock();
cerr << "Execution time: " << (end_time - start_time) * (int)1e3 / CLOCKS_PER_SEC << " ms\n";
#endif
} |
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*
*/
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
ll N, K;
cin >> N >> K;
ll ans = 0;
for(ll i=1; i<=N; i++){
for(ll k=1; k<=K; k++){
ll tmp = 100*i + k;
ans += tmp;
}
}
cout << ans << endl;
}
| #include<iostream>
#include<cstdio>
#include<cstring>
#define re
#define X first
#define Y second
#define mp std::make_pair
#define pb push_back
#define ohh(hhh...) fprintf(stderr,hhh)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
template<class T1> inline bool cmax(T1 &x,T1 y) {return x<y?(x=y,1):0;}
template<class T1> inline bool cmin(T1 &x,T1 y) {return x>y?(x=y,1):0;}
int read()
{
int x=0,w=0;
char ch=0;
while(ch<'0'||ch>'9') w|=ch=='-',ch=getchar();
while('0'<=ch&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return w?-x:x;
}
int main()
{
re int n=read(),res=0;
re int i=1;
for(;res<n;i++) res+=i;
printf("%d\n",i-1);
return 0;
} |
#include<bits/stdc++.h>
#define N 100005
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cerr<<#x<<"="<<x<<endl;
#define db2(x,y) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int rd(){
int res=0,c,f=0;
while(!isdigit(c=getchar()))f=c=='-';
do res=(res<<1)+(res<<3)+(c^48);
while(isdigit(c=getchar()));
return f?-res:res;
}
int n;
int main(){
n=rd();
for(int i=1;i<=n;i++){
int t=0,x=i;
for(int j=2;1ll*j*j<=x;j++)while(x%j==0)x/=j,++t;
if(x!=1)++t;
printf("%d%c",t+1," \n"[i==n]);
}
return 0;
}
| #include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#include "queue"
using namespace std;
#define MAXN 600006
//#define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin() , (x).end()
#define mem( a ) memset( a , 0 , sizeof a )
typedef long long ll;
int n , m;
char aa , ab , ba , bb;
const int P = 1e9 + 7;
void solve() {
cin >> n;
if( n <= 2 ) { puts("1"); return; }
char ch[4];
scanf("%s",ch); aa = ch[0];
scanf("%s",ch); ab = ch[0];
scanf("%s",ch); ba = ch[0];
scanf("%s",ch); bb = ch[0];
static int p2[MAXN] , F[MAXN];
p2[0] = 1;
rep( i , 1 , n ) p2[i] = p2[i - 1] * 2 % P;
F[0] = F[1] = 1;
rep( i , 2 , n ) F[i] = ( F[i - 1] + F[i - 2] ) % P;
if( ab == 'B' ) {
if( bb == 'B' ) return void( puts("1") );
if( ba == 'A' ) return void( cout << p2[n - 3] << endl );
cout << F[n - 2] << endl;
} else {
if( aa == 'A' ) return void( puts("1") );
if( ba == 'B' ) return void( cout << p2[n - 3] << endl );
cout << F[n - 2] << endl;
}
}
signed main() {
// freopen("5.in","r",stdin);
// int T;cin >> T;while( T-- ) solve();
solve();
}
|
//include <atcoder>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)
template <typename T>
T my_abs(T x){
return (x >= 0)? x : -x;
}
template <typename T>
void chmax(T &a, T b){
a = max(a, b);
}
template <typename T>
void chmin(T &a, T b){
a = min(a, b);
}
ll gcd(ll a, ll b){
if (a > b) return gcd(b, a);
if (a == 0) return b;
return gcd(b % a, a);
}
// --------------------------------------------------------------------------------
int main(void){
int i, j;
ll n;
cin >> n;
ll ans;
ll a, b, c;
ans = LINF;
rep0(b, 64){
a = (n>>b);
c = n - (a<<b);
chmin(ans, a + b + c);
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
const ll mod = 1e9+7;
const int N = 1e5+5;
#pragma region Debugger
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...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion Debugger
//My tools
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define rep(i,s,e) for(int i=s;i<e;i++)
#define all(v) (v).begin(),(v).end()
void solve(){
int n;cin>>n;
int a[n];
rep(i,0,n){
cin>>a[i];
}
sort(a,a+n);
rep(i,1,n+1){
if(a[i-1]!=i) {
cout<<"No\n";
return;
}
}
cout<<"Yes\n";
}
signed main(){
fastio;
int t=1;
//cin>>t;
rep(i,1,t+1){
solve();
}
return 0;
}
/*
1
0
*/ |
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#include<algorithm>//next_permutation
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
ll gcd(ll a,ll b){
if(b == 0) return a;
return gcd(b , a % b);
}
const ll MOD = 1000000007;
const ll MAX = 2000001;
ll fac[MAX], finv[MAX], inv[MAX];
void nCrprep() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll nCr(ll n, ll r){
if (n < r) return 0;
if (n < 0 || r < 0) return 0;
return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD;
}
ll mod(ll a){
return a % MOD;
}
ll lcm(ll a,ll b){
return a/gcd(a,b) * b;
}
vector<pair<ll,ll>> prime_factorize(ll n){
vector<pair<ll,ll>> res;
for(ll i=2; i*i <= n; i++){
if(n % i != 0) continue;
ll ex = 0;
while(n % i == 0){
ex++;
n /= i;
}
res.push_back({i,ex});
}
if(n != 1) res.push_back({n,1});
return res;
}
int main(){
ll h,w; cin >> h >> w;
vector<vector<char>> table(h,vector<char>(w));
rep(i,h){
rep(j,w) cin >> table[i][j];
}
vvl cnttable(h,vl(w));
vvl hsum(h,vl(w));
vvl wsum(h,vl(w));
vvl dsum(h,vl(w));
ll now = 2;
rep(i,w){
if(table[0][i] == '.'){
if(i == 0 or i == 1){
cnttable[0][i] = 1;
hsum[0][i] = 1;
dsum[0][i] = 1;
}
else{
cnttable[0][i] = now;
hsum[0][i] = now;
dsum[0][i] = now;
now *= 2;
now = mod(now);
}
}
else break;
}
now = 2;
rep(j,h){
if(table[j][0] == '.'){
if(j == 0 or j == 1){
cnttable[j][0] = 1;
wsum[j][0] = 1;
dsum[j][0] = 1;
}
else{
cnttable[j][0] = now;
wsum[j][0] = now;
dsum[j][0] = now;
now *= 2;
now = mod(now);
}
}
else break;
}
for(ll i=1; i < h; i++){
for(ll j=1; j < w; j++){
if(table[i][j] == '#') continue;
cnttable[i][j] = hsum[i-1][j] + dsum[i-1][j-1] + wsum[i][j-1];
cnttable[i][j] = mod(cnttable[i][j]);
hsum[i][j] = hsum[i-1][j] + cnttable[i][j];
wsum[i][j] = wsum[i][j-1] + cnttable[i][j];
dsum[i][j] = dsum[i-1][j-1] + cnttable[i][j];
hsum[i][j] = mod(hsum[i][j]); wsum[i][j] = mod(wsum[i][j]);
dsum[i][j] = mod(dsum[i][j]);
/*if(i == 2 and j == 1){
cout << hsum[i-1][j] << dsum[i-1][j-1] << wsum[i][j-1] << hsum[i-1][j] + dsum[i-1][j-1] + wsum[i][w-1] << endl;
}*/
}
}
/*for(auto array : cnttable){
for(ll x : array) cout << x;
cout << endl;
}
cout << endl;
for(auto array : hsum){
for(ll x : array) cout << x;
cout << endl;
}
cout << endl;
for(auto array : wsum){
for(ll x : array) cout << x;
cout << endl;
}
cout << endl;
for(auto array : dsum){
for(ll x : array) cout << x;
cout << endl;
}*/
cout << cnttable[h-1][w-1] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll int64_t
void solve()
{
int n, m;
cin >> n >> m;
vector<int> f(n);
for (int i = 0; i < m; i++)
{
int u, v;
cin >> u >> v;
v--, u--;
f[v] |= (1 << u);
f[u] |= (1 << v);
}
for (int i = 0; i < n; i++)
f[i] |= (1 << i);
int st = 1 << n;
vector<int> flag(1 << n);
for (int i = 0; i < st; i++)
{
int o = 0;
vector<int> tmp;
for (int j = 0; j < n; j++)
{
if ((i >> j) & 1)
o |= (1 << j), tmp.push_back(j);
}
int ok = 1;
for (int j : tmp)
{
if ((f[j] & o) != o)
ok = 0;
}
if (ok)
{
flag[i] = 1;
}
}
vector<int> dp((1<<n), n);
dp[0] = 0;
for (int i = 1; i < st; i++)
{
for (int j = i; j; j = (j - 1) & i)
{
if (flag[j])
{
dp[i] = min(dp[j^i] + 1, dp[i]);
}
}
}
cout << dp.back() << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
while (T--)
{
solve();
}
} |
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define eb emplace_back
#define mk make_pair
using namespace std;
void vec(vector<int> & v){
for(int i = 0;i<v.size() ; i++){
cout<<v[i]<< " ";
}
cout<<endl;
}
void vec_ll(vector<long long int> & v){
for(int i = 0;i<v.size() ; i++){
cout<<v[i]<< " ";
}
cout<<endl;
}
void vec_ull(vector<ull> & v){
for(int i = 0;i<v.size() ; i++){
cout<<v[i]<< " ";
}
cout<<endl;
}
int parsestring(string temp){
int num= 0;
int digs = temp.size() - 1;
for(int i = 0 ;i<=digs;i++){
num += pow(10 , digs -i)*(temp[i] - '0');
}
return num;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,k;
cin>>n>>k;
bool poss =true;
ll cur_pos = 0;
vector<pair<ll ,ll >>v;
for(ll i = 0;i<n;i++){
ll a ,b;
cin>>a>>b;
v.eb(mk(a,b));
}
sort(v.begin() , v.end());
for(int i = 0;i<n;i++){
if(!poss)break;
if(k >= v[i].first - cur_pos){
k -= (v[i].first-cur_pos);
cur_pos = v[i].first;
k += v[i].second;
}
else{
cur_pos += k;
k = 0;
poss = false;
break;}
}
if(k)cur_pos += k;
cout<<cur_pos<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define debug(x) cerr << #x << ": " << x << '\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
using ll = long long;
using ld = long double;
using intp = pair<int, int>;
using intmap = map<int, int>;
using intset = set<int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
constexpr int MOD = 998244353;
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 62;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }
inline bool bit(ll b, ll i) { return b & (1 << i); }
inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
template<class T> bool chmin(T &a, T b) {if(a > b){a = b; return 1;} return 0;}
template<class T> bool chmax(T &a, T b) {if(a < b){a = b; return 1;} return 0;}
signed main() {
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
ld ans;
ans = -sy * (sx-gx);
ans /= (sy + gy);
ans += sx;
printf("%.15Lf\n", ans);
} |
#include <iomanip>
#include <iostream>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
const int64_t mod = 1e9 + 7;
unordered_map<int64_t, int64_t> memo;
int64_t mod_pow(int64_t x, int64_t n) {
int64_t key = x * 1e6 + n;
if (memo.count(key)) return memo[key];
int64_t ret = 1;
while (n) {
if (n % 2) {
ret = (ret * x) % mod;
}
x = x * x % mod;
n /= 2;
}
return memo[key] = ret;
}
int main(void) {
ios::sync_with_stdio(false);
int H, W;
cin >> H >> W;
vector<string> S(H);
int K = 0;
rep(i, H) {
cin >> S[i];
rep(j, W) {
if (S[i][j] == '.') K++;
}
}
vector<vector<int>> left(H, vector<int>(W));
rep(i, H) {
left[i][0] = (S[i][0] == '.') ? 1 : 0;
for (int j = 1; j < W; ++j) {
if (S[i][j] == '#') {
left[i][j] = 0;
} else {
left[i][j] = left[i][j - 1] + 1;
}
}
}
vector<vector<int>> right(H, vector<int>(W));
rep(i, H) {
right[i][W - 1] = (S[i][W - 1] == '.') ? 1 : 0;
for (int j = W - 2; j >= 0; --j) {
if (S[i][j] == '#') {
right[i][j] = 0;
} else {
right[i][j] = right[i][j + 1] + 1;
}
}
}
vector<vector<int>> up(H, vector<int>(W));
rep(j, W) {
up[0][j] = (S[0][j] == '.') ? 1 : 0;
for (int i = 1; i < H; ++i) {
if (S[i][j] == '#') {
up[i][j] = 0;
} else {
up[i][j] = up[i - 1][j] + 1;
}
}
}
vector<vector<int>> down(H, vector<int>(W));
rep(j, W) {
down[H - 1][j] = (S[H - 1][j] == '.') ? 1 : 0;
for (int i = H - 2; i >= 0; --i) {
if (S[i][j] == '#') {
down[i][j] = 0;
} else {
down[i][j] = down[i + 1][j] + 1;
}
}
}
int64_t answer = 0;
rep(i, H) {
rep(j, W) {
if (S[i][j] == '#') continue;
int lamps = up[i][j] + down[i][j] + left[i][j] + right[i][j] + 1 - 4;
// cout << "i=" << i << " j=" << j << " lamps=" << lamps << endl;
// cout << "up=" << up[i][j] << " down=" << down[i][j]
// << " left=" << left[i][j] << " right= " << right[i][j] << endl;
int64_t add = mod_pow(2, lamps);
add = (add - 1 + mod) % mod;
int64_t mult = mod_pow(2, K - lamps);
add = (add * mult) % mod;
// cout << add << endl;
answer = (answer + add) % mod;
}
}
cout << answer << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;}
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k; cin >> n >> k;
vector<vector<int>> G(n);
rep(i, n-1) {
int u, v; cin >> u >> v;
u--; v--;
G[u].push_back(v);
G[v].push_back(u);
}
auto test = [&](int x) {
int need = 0;
vector<int> dist(n);
auto dfs = [&](auto dfs, int cur, int par) -> void {
vector<int> tmp;
for(int ne: G[cur]) {
if(ne != par) {
dfs(dfs, ne, cur);
chmax(dist[cur], dist[ne]+1);
tmp.push_back(dist[ne]+1);
}
}
if(tmp.empty()) return;
sort(all(tmp));
int mn = *tmp.begin();
int mx = *tmp.rbegin();
if(mn + mx < 0) {
chmin(dist[cur], mn);
return;
}
if(cur == 0 || dist[cur] == x) {
need++;
dist[cur] = -x-1;
}
};
dfs(dfs, 0, -1);
return need <= k;
};
auto bsearch = [&]() {
int ok = n-1, ng = 0;
while(ok-ng>1) {
int mid = (ok+ng)/2;
(test(mid) ? ok : ng) = mid;
}
return ok;
};
cout << bsearch() << endk;
return 0;
}
|
#include <bits/stdc++.h>
#define MOD 100000000
#define MAX 2005
using namespace std;
void test_case(){
long long n, m;
cin>>n>>m;
vector<long long> arr(n);
for(int i=0; i<n; i++)cin>>arr[i];
sort(arr.begin(), arr.end());
// for(auto i:arr)cout<<i<<' ';
// cout<<endl;
int q;
cin>>q;
vector<long long> acc(n);
acc[0] = arr[0];
for(int i=1; i<n; i++)acc[i] = acc[i-1] ^ arr[i];
for(int i=0; i<q; i++){
long long l, r;
cin>>l>>r;
long long ret = 0;
auto be = lower_bound(arr.begin(), arr.end(), l) - arr.begin();
auto en = upper_bound(arr.begin(), arr.end(), r) - arr.begin();
en--;
// cout<<be<<' '<<en<<endl;
if(be == 0)ret = acc[en];
else ret = acc[en] ^ acc[be-1];
if(ret <=l)cout<<'B';
else cout<<'A';
// cout<<reta<<' '<<l<<' '<<retb<<endl;
// if(ret == 0)cout<<'B';
// else cout<<'A';
}
}
vector<int> color, ans;
vector<vector<int>> adj;
map<int, int> taken;
void solve(int x, int par = -1){
// cout<<x<<' ';
if(!taken[color[x]]){
ans.push_back(x+1);
taken[color[x]] ++;
for(int i=0; i<adj[x].size(); i++){
if(adj[x][i] == par)continue;
solve(adj[x][i], x);
}
taken[color[x]] --;
}
else {
for(int i=0; i<adj[x].size(); i++){
if(adj[x][i] == par)continue;
solve(adj[x][i], x);
}
}
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
color.resize(n);
adj.resize(n);
for(int i=0; i<n; i++)cin>>color[i];
for(int i=0; i<n-1;i++){
int x, y;
cin>>x>>y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
solve(0);
sort(ans.begin(), ans.end());
for(auto x:ans)cout<<x<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //これつけるとA[N]でもいいらしい
//for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp(a,b) make_pair(a,b)
#define big 1000000007
#define all(a) sort((a).begin(),(a).end()) //ソートのマクロ
#define Re(a) reverse((a).begin(),(a).end())
#define YN(a) if(a){cout<<"Yes"<<endl;}else cout<<"No"<<endl;//条件によってYes、Noを出力する
vector<int> G[100002];
int visited[100002]={};
int c[100002]={};
vector<int> cm(100002,0);
vector<int> ans={};
//今まで見ている点でどの色が何回出たか
void dfs(int now){
//cout<<"今から"<<now+1<<"を探索"<<endl;
// cout<<"もうある色"<<endl;
// for(auto color:cm){
// cout<<color.first<<" ";
// }
//cout<<endl;
visited[now]=1;
if(cm[c[now]]==0){
ans.push_back(now+1);
}
cm[c[now]]+=1;
for(int j=0;j<G[now].size();j++){
int child = G[now][j];
if(!visited[child]){
dfs(child);
}
}
cm[c[now]]--;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin>>n;
//vector<int> c(n);
vector<int> a(n),b(n);
int maxc=0;
rep(i,n){
cin>>c[i];
c[i]--;
maxc = max(c[i],maxc);
}
rep(i,n-1){
cin>>a[i]>>b[i];
a[i]--,b[i]--;
G[a[i]].push_back(b[i]);
G[b[i]].push_back(a[i]);
}
// for(int i=0;i<n;i++){
// cout << "G[" <<i <<"]={";
// for(int j=0;j<G[i].size();j++){
// cout<<G[i][j]<<" ";
// }
// cout<<"}"<<endl;
// }
// rep(i,maxc+1){
// cout<<cm[i]<<endl;
// }
dfs(0);
all(ans);
for(auto node: ans){
cout<<node<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, x, y) for (int i = x; i < y; i++)
int main()
{
ll N;
cin >> N;
ll ans = 0;
map<int, int> numbers;
rep(i, 0, N)
{
int input;
cin >> input;
if (numbers.find(input) == numbers.end())
{
numbers[input] = 1;
ans += i;
}
else
{
numbers[input]++;
ans += i + 1 - numbers[input];
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define rep(i, a , b) for (ll i =a ; i < b ; i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define pb push_back
#define F first
#define S second
#define print(a) for(auto x : a) cout << x << " "; cout << endl
#define print1(a) for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y) for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl
typedef long long int ll;
const ll N = 1e6+5;
const ll mod = 998244353;
const ll inf = 1e18;
ll n , k , m , q , w , u , x , v , z , a , b, c;
ll pow_mod_m(ll a , ll n)
{
if(!n)
return 1;
ll pt = pow_mod_m(a,n/2);
pt *= pt , pt %= mod;
if(n&1)
pt *= a ,pt %= mod;
return pt;
}
ll ways(ll x)
{
return min( max(x-1,0ll) , max(2*n+1 - x,0ll) );
}
int main()
{
IOS;
int ts =1;
///cin >> ts ;
while(ts--)
{
cin >> n >> k;
ll ans = 0;
rep(i,2,2*n+1)
{
ans += ways(i-k)*ways(i);
}
cout << ans << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
long long gcd_vec(vector<long long> const &A) { // N個の要素に対する最大公約数
int size = (int)A.size();
long long ret = A[0];
for (int i = 1; i < size; i++) {
ret = gcd(ret, A[i]);
}
return ret;
}
long long lcm2(long long a, long long b) {
long long d = gcd(a, b);
return a / d * b;
}
long long lcm(const vector<long long> &vec) {
long long l = vec[0];
for (int i = 0; i < vec.size() - 1; i++) {
l = lcm2(l, vec[i + 1]);
}
return l;
}
long long int mypow(long long int a,long long int b){
long long int i=0,ans;
for(i=0;i<b;i++)
{
if(i==0)
{
ans=a;
}
else
{
ans*=a;
}
}
return ans;
}
struct UnionFind{
int n;
vector<int> par; // 親の番号
vector<int> siz; // サイズを表している。
// 初期化する。parは自身を指す(iotaは 0 ~ nまでの数字を順に入れていく)
// size(siz) は最初はすべて1
UnionFind(int n) : n(n){
par.resize(n);
iota(par.begin(),par.end(),0);
siz.assign(n,1);
}
// 頂点の親を求める。
// 再帰関数を使っており、経路圧縮もしている。
int root(int x){
// ここは親の処理。
if (par[x] == x){
return x;
}
//経路圧縮をしながら値を返す。
// par[x] = root(par[x]);
// return par[x];
// と同じ意味。
return par[x] = root(par[x]);
}
// unite :: a と bを同じグループに入れる。
// もし初めから同じなら何もしない。
void unite(int a,int b){
int ra = root(a);
int rb = root(b);
if(ra == rb){
return;
}
// サイズの大きい方にサイズの小さいほうを合成するので、swapしている。
if (siz[ra] < siz[rb]){
swap(ra,rb);
}
// サイズが大きい方に小さいのを合体
// 小さいほうの親は大きい方の親になる。
siz[ra] += siz[rb];
par[rb] = ra;
}
// 同じグループかどうか判定するには、親が一緒かどうかをみたらよい
bool same(int x,int y){
return root(x) == root(y);
}
// 頂点が含まれるグループの頂点を求める。
int get_size(int x){
return siz[root(x)];
}
};
long long int facctorialMethod(int k){
long long int sum = 1;
for (int i = 1; i <= k; ++i)
{
sum *= i;
}
return sum;
}
#define PI 3.14159265359
int main()
{
[[maybe_unused]]long long int i=0,j=0,k,n,m=0;
set<long long int>st;
[[maybe_unused]]double qw;
string s,s1;
cin >> s;
for(i=0;i<9;i++)
{
if(s[i]=='Z' && s[i+1]=='O' && s[i+2]=='N' && s[i+3]=='e')
{
m++;
}
}
cout << m;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#define pb push_back
#include <iomanip>
#define mp make_pair
#define ll long long
#define vll vector <ll>
#define pll pair <ll,ll>
#define sll set <ll>
#define msll multiset <ll>
#define mll map <ll,ll>
#define mod 1000000007
ll prime[1000001];
using namespace std;
//// FOR SINGLE TO KNOW FOR PRIME
int Prime(int a)
{
for(int i=2;i<=a/2;++i)
{
if(a%i==0)
{
return 0;
}
}
return 1;
}
//// FOR PRIMES TILL A VALUE
void sieve()
{ for(int i=2;i<=1000000;i++)
prime[i]=1;
int maxn=1000000;
for(int i=2;i*i<=maxn;i++)
{
if(prime[i]==1)
{
for(int j=i;j*j<=maxn;j+=i)
{
prime[j]=0;
}
}
}
}
void fibo(ll a[],ll n)
{
ll x=0;
ll y=1;
a[0]=0;
a[1]=1;
for(ll i=2;i<n;++i)
{
a[i]=(x+y)%10;
x=y;
y=a[i];
}
}
/*ll f(ll w[],ll a[][2],ll i,ll y)
{
for(ll j=0;j<y;++j)
{
if(a[i][2]<=w[j])
{
cout<<a[i][2];
return w[j];
}
}
return w[y-1];
}*/
/*ll s(ll v[],ll a[][2],ll i,ll x)
{
for(ll j=0;j<x;++j)
{
if(a[i][1]>=v[j])
{
cout<<" "<<a[i][1]<<" ";
return v[j];
}
}
return v[0];
}*/
bool binarySearch(vector <ll> arr, int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return 1;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return 0;
}
//////// FOR FAST POWER
/*ll poop[5000];
memset(poop,-1,sizeof(poop));
ll po(ll n,ll x)
{
if(poop[x]!=0)
{
return poop[x];
}
if(x==1)
{
return poop[x]=n%mod;
}
if(x%2)
{
return poop[x]=(n*((po(n,x/2))%mod)*((po(n,x/2))%mod))%mod;
}
else
{
return poop[x]=(((po(n,x/2))%mod)*((po(n,x/2))%mod))%mod;
}
}*/
/*ll v[3001],cont[3001],f[3001],n,c,k,x,y,z,t,l,v;
//memset(f,0,sizeof(f));
ll fact(ll a)
{
if(a==0||a==1)
{
return 1;
}
if(f[a]!=0)
{
return f[a];
}
return f[a]=a*fact(a-1);
}*/
/*ll solve(ll k,ll t,ll i)
{
if(i<0||k==0)
{
return t;
}
if((v[i]<=k)&&(cont[i]>2))
{
return min(solve(k,t,i),solve(k-val[i],t-fact(count[i])/(6*fact(count[i-3]))))
}
else
{
return solve()
}
}*/
/*ll s[200001],x,a[200001];
void bs(ll low,ll high)
{
ll mid=(low+high)/2;
if(s[mid]==x)
{
cout<<mid+1<<" "<<a[mid];
}
else if((x<s[mid])&&(x>=s[mid-1]))
{
cout<<mid<<" "<<x-s[mid-1];
}
else if(x>s[mid])
{
return bs(mid+1,high);
}
else
{
return bs(low,mid-1);
}
}*/
/*vector<ll>v[100001],vis(100001,0),d(100001,0);
void dfs(ll n)
{
vis[n]=1;
for(auto x:v[n])
{
if(!vis[x])
{
d[x]=d[n]+1;
dfs(x);
}
}
}*/
ll vis[1000][1000],dis[1000][1000],di[1000][1000],dx[]={0,1,1,1,0,-1,-1,-1},dy[]={-1,-1,0,1,1,1,0,-1},n,m;
char s[1000][1000];
bool check(ll a,ll b)
{
if(a>=0&&b>=0&&a<n&&b<m)
{
return true;
}
return false;
}
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout)
ll t,k,b,c,ans,r,e,a,f,g,u,d;
//cin>>t;
//while(t--)
{
string x;
cin>>x;
n=x.size();
k=0;
for(ll i=0;i<n-3;++i)
{
if(x[i]=='Z'&&x[i+1]=='O'&&x[i+2]=='N'&&x[i+3]=='e')
{
i+=3;
k++;
}
}
cout<<k;
}
return 0;
}
|
/* -*- coding: utf-8 -*-
*
* e.cc: E - White and Black Balls
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 1000000;
const int MAX_M = 1000000;
const int MOD = 1000000007;
/* typedef */
typedef long long ll;
/* global variables */
int fs[MAX_N + MAX_M + 1], invfs[MAX_N + MAX_M + 1];
/* subroutines */
int powmod(int a, int n) { // a^n % MOD
int pm = 1;
while (n > 0) {
if (n & 1) pm = (ll)pm * a % MOD;
a = (ll)a * a % MOD;
n >>= 1;
}
return pm;
}
inline int nck(int n, int k) { // nCk % MOD
if (n < k || k < 0) return 0;
return (ll)fs[n] * invfs[n - k] % MOD * invfs[k] % MOD;
}
void prepare_fracs(int n) {
fs[0] = invfs[0] = 1;
for (int i = 1; i <= n; i++) {
fs[i] = (ll)fs[i - 1] * i % MOD;
invfs[i] = powmod(fs[i], MOD - 2);
}
}
/* main */
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
if (n > m + k) { puts("0"); return 0; }
prepare_fracs(n + m);
int a = (nck(n + m, n) + MOD - nck(n + m, m + k + 1)) % MOD;
printf("%d\n", a);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define per(i,a,b) for(int i = (a); i > (b); i--)
#define repl(i,a,b) for(ll i = (a); i < (b); i++)
#define perl(i,a,b) for(ll i = (a); i > (b); i--)
const ll MOD = 1000000007;
int _max = 2000000;
vector<ll> fact(_max+1), factinv(_max+1);
//Fast modular exponentiation
long long fast_exp(long long base, long long exp, long long mod) {
long long res=1;
while(exp>0) {
if(exp%2==1) res=(res*base)%mod;
base=(base*base)%mod;
exp/=2;
}
return res%mod;
}
int n, m;
void precompute(){
_max = n+m;
fact[0] = 1;
for(int i = 1; i <= _max; i++){
fact[i] = (fact[i-1]*i)%MOD;
}
for(int i = 0; i <= _max; i++){
factinv[i] = fast_exp(fact[i], MOD-2, MOD);
}
}
void solve(){
int k;
cin >> n >> m >> k;
precompute();
if(n-k-1 >= m){
cout << 0 << "\n";
}
else{
ll total = (((fact[n+m]*factinv[n])%MOD)*factinv[m])%MOD;
//cout << total << "\n";
if(n == k){
cout << total << "\n";
}
else{
ll bad = (((fact[n+m]*factinv[n-k-1])%MOD)*factinv[m+k+1])%MOD;
ll ans = total - bad;
if(ans < 0) ans += MOD;
cout << ans << "\n";
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
precompute();
//ll t;
//cin >> t;
t = 1;
while(t--)solve();
return 0;
} |
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
template<class T> using indexed_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N=2e5+7;
const int mod=1e9+7;
const int INF=1e18+7;
const double EPS=1e-12;
const int dx[]={-1,1,0,0}; // UDLR
const int dy[]={0,0,-1,1}; // UDLR
inline int maximum(int a,int b) {int c=a>b?a:b; return c;}
inline int minimum(int a,int b) {int c=a>b?b:a; return c;}
int r,c; bool visited [550][550]; vector<vector<int>> d(550,vector<int>(550,INF));
vector<vector<int>> A(550,vector<int>(550)),B(550,vector<int>(550));
int32_t main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin>>r>>c; priority_queue<tuple<int,int,int>> q;
for(int i=1;i<=r;i++){
for(int j=1;j<=c-1;j++) cin>>A[i][j];
}
for(int i=1;i<=r-1;i++){
for(int j=1;j<=c;j++) cin>>B[i][j];
}
q.push({0,1,1}); d[1][1]=0;
while(!q.empty()){
auto [cost,row,col]=q.top(); q.pop();
if(row==r&&col==c){
cout<<d[row][col]<<'\n'; break;
}
if(visited[row][col]) continue;
visited[row][col]=true;
for(int i=1;i<row;i++){
if(d[row][col]+1+row-i<d[i][col]){
d[i][col]=d[row][col]+1+row-i; q.push({-d[i][col],i,col});
}
}
if(row+1<=r&&d[row][col]+B[row][col]<d[row+1][col]){
d[row+1][col]=d[row][col]+B[row][col]; q.push({-d[row+1][col],row+1,col});
}
if(col-1>=1&&d[row][col]+A[row][col-1]<d[row][col-1]){
d[row][col-1]=d[row][col]+A[row][col-1]; q.push({-d[row][col-1],row,col-1});
}
if(col+1<=c&&d[row][col]+A[row][col]<d[row][col+1]){
d[row][col+1]=d[row][col]+A[row][col]; q.push({-d[row][col+1],row,col+1});
}
}
return 0;
}
| #pragma GCC optimize("O3")
#pragma GCC target("avx")
#include <cstdio>
#include <functional>
#include <queue>
#include <tuple>
using namespace std;
int main() {
int R, C;
scanf("%d%d", &R, &C);
int A[250000], B[250000];
int dist[500000], seen[500000] = {0}, cost, pt, to_hide = R * C;
int top, bottom, right, left, hide, i;
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
queue;
for(i = 0; i < R * C; ++i)
if(i % C != C - 1) scanf("%d", &A[i]);
for(i = 0; i < (R - 1) * C; ++i) scanf("%d", &B[i]);
for(i = 1; i < to_hide + to_hide; ++i) dist[i] = 10000000;
queue.emplace(pair<int, int>(0, 0));
while(!queue.empty()) {
tie(cost, pt) = queue.top();
queue.pop();
seen[pt] = true;
if(pt < to_hide) {
left = pt - 1, right = pt + 1, top = pt + C, hide = pt + to_hide;
if(!seen[right] && pt % C != C - 1) {
if(cost + A[pt] < dist[pt + 1]) {
dist[right] = cost + A[pt];
queue.emplace(pair<int, int>(dist[right], right));
}
}
if(!seen[left] && pt % C != 0) {
if(cost + A[left] < dist[left]) {
dist[left] = cost + A[left];
queue.emplace(pair<int, int>(dist[left], left));
}
}
if(!seen[top] && top < to_hide) {
if(cost + B[pt] < dist[top]) {
dist[top] = cost + B[pt];
queue.emplace(pair<int, int>(dist[top], top));
}
}
if(!seen[hide] && cost + 1 < dist[hide]) {
dist[hide] = cost + 1;
queue.emplace(pair<int, int>(dist[hide], hide));
}
} else {
bottom = pt - C, hide = pt - to_hide;
if(to_hide < bottom && !seen[bottom] && cost + 1 < dist[bottom]) {
dist[bottom] = cost + 1;
queue.emplace(pair<int, int>(cost + 1, bottom));
}
if(!seen[hide] && cost < dist[hide]) {
dist[hide] = cost;
queue.emplace(pair<int, int>(cost, hide));
}
}
}
printf("%d\n", dist[to_hide - 1]);
} |
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define pb push_bac(X/j)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define ll long long
using namespace std;
void file(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
}
int tc;
const int N=2e5+5,M=2e6+5,MOD=998244353,OO=1e9;
ll sum(int x){
return (ll)x*(x+1)/2 %MOD;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//file();
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
ll ans = (((sum(a) * sum(b))%MOD) * sum(c))%MOD;
printf("%lld\n",ans );
} | #line 1 "main.cpp"
#include <bits/stdc++.h>
using namespace std;
// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;
constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};
struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;
template <typename T = i64> T input() { T x; cin >> x; return x; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
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); }
// }}}
void solve() {
int n = input(), k = input();
i64 ans = 0;
range(i, 1, n + 1) range(j, 1, k + 1) {
ans += 100 * i + j;
}
cout << ans << endl;
}
signed main() {
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
typedef vector<bool> vbl;
typedef vector<vector<bool>> vvbl;
void INX(){}
template<typename Head, typename... Tail>
void INX(Head&& head, Tail&&... tail)
{
cin >> head;
INX(forward<Tail>(tail)...);
}
void OUTX(){}
template<typename Head, typename... Tail>
void OUTX(Head&& head, Tail&&... tail)
{
cout << head << endl;
OUTX(forward<Tail>(tail)...);
}
void OUTX2(){cout << endl;}
template<typename Head, typename... Tail>
void OUTX2(Head&& head, Tail&&... tail)
{
cout << head << ' ';
OUTX2(forward<Tail>(tail)...);
}
#define ADD emplace_back
#define MP make_pair
#define VVEC(type) vector<vector<type>>
#define __LONG_LONG_MIN__ (-__LONG_LONG_MAX__ - 1)
//#define MOD 1000000007 // 10^9 + 7
//setprecision(10)
// Least Significant Bit (LSB)
ll LSB(ll x)
{
return x & (-x);
}
// Most Significant Bit (MSB)
ll MSB(ll x)
{
if(x == 0) return x;
ll cnt = 0;
while(x)
{
cnt++;
x >>= 1;
}
return 1 << (cnt - 1);
}
// 参考:https://www.slideshare.net/hcpc_hokudai/binary-indexed-tree
// Binary Indexed Tree (BIT)
// フェニック木
class BinaryIndexedTree
{
private:
ll N; // 要素数
vll data; // 実体のデータ
// コンストラクタ共通の初期化処理
void initial(ll n, ll initialvalue = 0)
{
N = n;
ll msb = MSB(n);
data.resize((msb == n ? n : MSB(n) << 1) + 1, initialvalue);
}
public:
// コンストラクタ
BinaryIndexedTree(ll n, ll initialvalue = 0)
{
initial(n, initialvalue);
}
// a[i]にxを足す(i=1,2,...,N)
void AddValue(ll i, ll x)
{
while(i < (ll)data.size())
{
data[i] += x;
i += LSB(i);
}
}
// a[1]からa[i]までの累積和
ll GetCumulativeSum(ll i)
{
ll result = 0;
while(i)
{
result += data[i];
i ^= LSB(i);
}
return result;
}
// a[i]からa[j]までの部分和
ll GetPartialSum(ll i, ll j)
{
return GetCumulativeSum(j) - GetCumulativeSum(i - 1);
}
};
// 公式解説を参考に実装
int main()
{
ll N;
INX(N);
vll a(N);
for (ll i = 0; i < N; i++)
{
INX(a[i]);
}
ll result = 0;
BinaryIndexedTree bitt(N);
for(ll x : a)
{
result += bitt.GetPartialSum(x + 1, N);
bitt.AddValue(x + 1, 1);
}
for(ll x : a)
{
OUTX(result);
result += N - (1 + 2 * x);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(ll i=((ll)n)-1;i>=0;i--)
#define range(i,a,n) for(ll i=((ll)a);i<((ll)n);i++)
#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI (3.1415926535897932384)
int dx[]={1,0,-1,0,1,-1,-1,1},dy[]={0,1,0,-1,1,1,-1,-1};
template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S sum(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
int sgn(const double&r){return (r>EPS)-(r<-EPS);} // a>0 : sgn(a)>0
int sgn(const double&a,const double&b){return sgn(a-b);} // b<=c : sgn(b,c)<=0
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>void tf(bool b,S t,T f){if(b)puta(t);else puta(f);}
void YN(bool b){tf(b,"YES","NO");}
void Yn(bool b){tf(b,"Yes","No");}
void yn(bool b){tf(b,"yes","no");}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1;for(auto s:t){os<<(a?"":" ")<<s;a=0;}return os;}
template<class S>auto&operator>>(istream&is,vector<S>&t){for(S&a:t)cin>>a;return is;}
/*他のライブラリを入れる場所*/
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll mv,v,n,l,t=0;
cin>>v>>n>>l;
mv=v;
rep(i,n){
ll a,b; cin>>a>>b;
v-=(a-t);
if(v<=0){
Yn(0);
return 0;
}
v+=b-a;
v=min(v,mv);
t=b;
}
Yn(v>(l-t));
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
#include "string"
#define int long long
#define pi pair <int, int>
#define ff first
#define ss second
#define boost ios::sync_with_stdio(false);cin.tie(nullptr)
#define endl '\n'
#define vi vector<int>
int32_t main() {
boost;
int n, m;
cin >> n >> m;
if(n == 1 && m == 0) {
cout << 1 << ' ' << 2 << endl;
return 0;
}
if(m < 0 or m >= n - 1) {
cout << -1 << endl;
return 0;
}
cout << 1 << ' ' << (int)1e7 << endl;
int l = 2;
for(int i = 0; i <= m; i++, l += 2)
cout << l << ' ' << l + 1 << endl;
l = 1e7 + 2;
for(int i = 0; i < n - m - 2; i++, l += 2)
cout << l <<' ' << l + 1<< endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define si(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sl(a) scanf("%lld",&a)
#define sl2(a,b) scanf("%lld%lld",&a,&b)
#define pb push_back
#define mk make_pair
#define loop(n) for(int i=0; i<n; i++)
#define FOR(a,b) for(int i=a; i<=b; i++)
#define sz size()
#define ff first
#define ss second
#define mem(a,val) memset(a, val, sizeof(a))
#define md 1000000007
#define pi acos(-1.0)
int main()
{
ll n,m, l,r;
cin>>n>>m;
if(m==0)
{
l=2, r=3;
for(int i=1; i<=n; i++)
{
printf("%lld %lld\n", l,r);
//cout<<l<<" "<<r<<endl;
l+=2, r+=2;
}
return 0;
}
if(m<0 || m>=n-1)
{
cout<<"-1"; return 0;
}
cout<<1<<" "<<(ll)1e7<<endl;
l=2, r=3;
for(int i=1; i<=m+1; i++)
{
printf("%lld %lld\n", l,r);
l+=2, r+=2;
}
l=2e7, r=l+1;
for(int i=m+3; i<=n; i++)
{
printf("%lld %lld\n", l,r);
l+=2, r+=2;
}
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
//#pragma GCC target("fpmath=387")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef string str;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define watch(x) cout << (#x) << " is : " << (x) << newl
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vi dirx = {0,0,1,-1};
vi diry = {1,-1,0,0};
char to_upper (char x){
if( 97 <= int(x) && int(x) <= 122) return char(x-32);
if( 65 <= int(x) && int(x) <= 90) return x;
return -1;
}
char to_lower (char x){
if( 97 <= int(x) && int(x) <= 122) return x;
if( 65 <= int(x) && int(x) <= 90) return char(x+32);
return -1;
}
int numerize (char x){
if(48 <= int(x) && int(x) <= 57) return int(x-'0');
if(97 <= int(x) && int(x) <= 122) return int(x-96);
if(65 <= int(x) && int(x) <= 90) return int(x-64);
return -1;
}
bool isect (int l1, int r1, int l2, int r2){ return max(l1,l2) <= min(r1,r2); }
ll quickpow (ll num1, ll num2, ll MOD){
if(num2==0)return 1%MOD;
else if(num2==1)return num1%MOD;
else{
ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
}
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template
bool valid(int x, vi &v){
int g = 0;
for(auto i : v){
if(i % x == 0) g = (g ? __gcd(g,i) : i);
}
return g == x;
}
int main(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
vi v(n); for(int i = 0; i < n; ++i) cin >> v[i];
int mn = INT_MAX; for(auto i : v) mn = min(mn,i);
set <int> s;
for(auto x : v){
for(int i = 1; i * i <= x; ++i){
if(x % i == 0){
if(i <= mn) s.insert(i);
if(i != x / i && x / i <= mn) s.insert(x / i);
}
}
}
int res = 0;
for(auto i : s) res += valid(i,v);
cout << res << newl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void exgcd(ll a, ll b, ll& x, ll& y) {
if (a == 0) {
x = 0;
y = 1;
return;
}
exgcd(b % a, a, y, x);
x -= (b / a) * y;
}
ll m = 1e9 + 7;
ll flip(ll a) {
ll x, y;
exgcd(a, m, x, y);
if (x < 0) {
ll kj = -x;
kj /= m;
x += (kj + 1) * m;
}
x %= m;
return x;
}
int main () {
int N;
ll M;
cin >> N >> M;
M += (ll)N;
ll sum = (ll)N;
for (int i = 0; i < N; i ++) {
ll a;
cin >> a;
sum += a;
}
ll ans = 1;
for (ll i = 1; i <= sum; i ++) {
ans *= (M - i + 1);
ans %= m;
}
ll fli = 1;
for (ll i = 1; i <= sum; i ++) {
fli *= i;
fli %= m;
}
ans *= flip(fli);
ans %= m;
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define Mashu cout << "UUZ ate it." << endl
#define RE register int
#define ll long long
using namespace std;
inline int read(){
char ch=getchar();
int x=0,cf=1;
while(ch<'0'||ch>'9') {
if(ch=='-') cf=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
if (cf < 0) return -x;
else return x;
}
int G[55];
template<class T> inline void write(T x){
if (x < 0){
putchar('-');
x = -x;
}
int g=0;
do{G[++g]=x%10;x/=10;}while(x);
for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
}
struct edge{
int to;
int nxt;
int w;
}e[400010];
int f[200010], cnt = 0;
void add(int u, int v, int w){
e[++cnt].to = v;
e[cnt].w = w;
e[cnt].nxt = f[u];
f[u] = cnt;
}
int main(){
long long n;
cin >> n;
long long ans = 1;
for (long long i = 2; i <= n; i++){
ans = i * ans / __gcd(ans, i);
}
cout << ans + 1 << endl;
return 0;
Mashu;
} | /*ॐ नमो भगवते वासुदेवाय नमः*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9 + 7;
const double pi = 3.14159265359;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int binpow(int a, int b, int m) {
a %= m;
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
int inverse(int x){
return binpow(x, MOD - 2, MOD);
}
void solve()
{
int n, ans = 0;
cin >> n;
n *= 2;
for(int i = 1; i * i <= n; i++){
if(n % i == 0){
if(i % 2 != (n / i) % 2) ans++;
}
}
cout << 2 * ans << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
solve();
cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n ";
} |
#include <bits/stdc++.h>
#define endl '\n'
#define all(a) (a).begin(), (a).end()
#define len(a) (int) (a).size()
#define forn(i, n) for (int (i) = 0; (i) < (n); ++(i))
using namespace std;
void solve();
mt19937 rnd(2007);
signed main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
swap(rng, rnd);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
void solve() {
int n; cin >> n;
vector<int> a(n); forn (i, n) cin >> a[i];
int mn = *min_element(all(a));
unordered_map<int, int> vals;
for (auto v : a){
for (int i = 1; i * i <= v; ++i){
if (v % i == 0){
vals[i] = __gcd(vals[i], v / i);
vals[v / i] = __gcd(vals[v / i], i);
}
}
}
int ans = 0;
for (auto i : vals){
if (i.first <= mn && i.second == 1) {
++ans;
}
}
cout << ans << endl;
} | #include<iostream>
#include<vector>
#include<string>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}
using P = pair<int, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> A(N), B(N);
rep(i, 0, N) {
cin >> A[i] >> B[i];
}
P undefined_val = P(-1, -1);
int left_dir = 0;
int right_dir = 1;
vector<P> dir_and_idx(2 * N + 1, undefined_val);
rep(i, 0, N) {
if (A[i] > 0 && B[i] > 0 && A[i] >= B[i]) {
cout << "No" << endl;
return 0;
}
if (A[i] > 0 && dir_and_idx[A[i]] != undefined_val) {
cout << "No" << endl;
return 0;
}
if (A[i] > 0) {
dir_and_idx[A[i]] = P(i, left_dir);
}
if (B[i] > 0 && dir_and_idx[B[i]] != undefined_val) {
cout << "No" << endl;
return 0;
}
if (B[i] > 0) {
dir_and_idx[B[i]] = P(i, right_dir);
}
}
vector<bool> dp(2 * N + 1, false);
dp[0] = true;
rep(i, 1, 2 * N + 1) {
if (dir_and_idx[i].second == left_dir) {
continue;
}
rep(j, 0, i - 1) {
if ((i - j) % 2) {
continue;
}
bool is_ok = true;
rep(k, j + 1, j + (i - j) / 2 + 1) {
if (dir_and_idx[k].second == right_dir || dir_and_idx[k + (i - j) / 2].second == left_dir) {
is_ok = false;
break;
}
int left_idx = dir_and_idx[k].first;
int right_idx = dir_and_idx[k + (i - j) / 2].first;
if (left_idx >= 0 && right_idx >= 0 && left_idx != right_idx) {
is_ok = false;
break;
}
if (left_idx >= 0 && A[left_idx] > 0 && B[left_idx] > 0 && left_idx != right_idx) {
is_ok = false;
break;
}
if (right_idx >= 0 && A[right_idx] > 0 && B[right_idx] > 0 && left_idx != right_idx) {
is_ok = false;
break;
}
}
dp[i] = (dp[i] | (dp[j] & is_ok));
}
}
if (dp[2 * N]) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
} |
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
int a, b, x, y;
cin >> a >> b >> x >> y;
int diff = a > b ? a - b - 1 : b - a;
cout << x + min(2 * x, y) * diff << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)((n)-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}
template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
int l = vv.size();
if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n " ) << vv[i] << (i==l-1 ? " ]" : ""); }
return o;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& p){
return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}
template <typename ... Args>
string format(const std::string& fmt, Args ... args ){
size_t len = std::snprintf(nullptr, 0, fmt.c_str(), args ... );
vector<char> buf(len + 1);
snprintf(&buf[0], len + 1, fmt.c_str(), args ... );
return string(&buf[0], &buf[0] + len);
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) {
freopen(filename.c_str(), "r", stdin);
}
vector<vector<pii>> g(310);
vi dijk(int s){
vi d(310, INF);
d[s] = 0;
priority_queue<pii, vector<pii>, greater<pii>> Q;
Q.emplace(0,s);
while(!Q.empty()){
int c = Q.top().fi;
int v = Q.top().se;
Q.pop();
if(c > d[v]) continue;
for(auto p : g[v]){
int u = p.fi;
int uc = p.se;
if(d[u] > d[v] + uc){
d[u] = d[v] + uc;
Q.emplace(d[u], u);
}
}
}
return d;
}
int main(){
ios_base::sync_with_stdio(0);
// finput("./input");
int M = 200;
int a,b,x,y;
cin >> a >> b >> x >> y;
a -= 1; b += M-1;
rep(i,100){
g[i].emplace_back(i+M, x);
g[i+M].emplace_back(i, x);
g[i+1].emplace_back(i+M, x);
g[i+M].emplace_back(i+1, x);
g[i].emplace_back(i+1,y);
g[i+1].emplace_back(i,y);
g[i+M].emplace_back(i+M+1,y);
g[i+M+1].emplace_back(i+M,y);
}
vi d = dijk(a);
cout << d[b] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)
ll INF = 1ll << 60;
ll cl[200010], cr[200010];
ll dp[200010][2];
int main(){
int n;
cin >> n;
set<ll> col;
rep(i, 0, 200010){
cl[i] = INF;
cr[i] = -1*INF;
}
rep(i, 0, n){
ll x, c;
cin >> x >> c;
col.insert(c);
if(cl[c] > x){
cl[c] = x;
}
if(cr[c] < x){
cr[c] = x;
}
}
auto itr = col.begin();
ll st = *itr;
dp[st][0] = abs(cr[st])+abs(cr[st]-cl[st]);
dp[st][1] = abs(cl[st])+abs(cr[st]-cl[st]);
rep(i, 1, col.size()){
ll cb = *itr;
itr++;
ll cn = *itr;
dp[cn][0] = min(dp[cb][0] + abs(cr[cn]-cl[cn]) + abs(cr[cn]-cl[cb]), dp[cb][1] + abs(cr[cn]-cl[cn]) + abs(cr[cn]-cr[cb]));
dp[cn][1] = min(dp[cb][0] + abs(cr[cn]-cl[cn]) + abs(cl[cn]-cl[cb]), dp[cb][1] + abs(cr[cn]-cl[cn]) + abs(cl[cn]-cr[cb]));
}
ll ls = *itr;
ll ans = min(dp[ls][0] + abs(cl[ls]), dp[ls][1] + abs(cr[ls]));
itr = col.begin();
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }
int main() {
init();
int n; cin >> n;
int ans = 0;
eFOR(i, 1, n) {
int tmp = 1;
string s = to_string(i);
for (char c : s)if (c == '7')tmp = 0;
int j = i;
for (; j; j /= 8)if (j % 8 == 7)tmp = 0;
ans += tmp;
}
print(ans);
return 0;
} |
#include <iostream>
#include <vector>
#include <utility>
#include<algorithm>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <random>
#include <tuple>
#include <set>
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main(){
ll n;
cin >> n;
vector<ll> a= {6,10,15};
rep(k,3){
for(int i = 2;i <= 10000/a[k];i ++){
if(a.size()==n){
rep(j,n){
cout << a[j] << ' ';
}
cout << endl;
return 0;
}
if(k >= 1 and (i*a[k]%6==0)){
continue;
}
a.push_back(i*a[k]);
}
}
return 0;
} | //QwQcOrZ yyds!!!
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define R(i,a,b) for (int i=(a);i<(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define go(i,x) for (int i=head[x];i;i=e[i].nx)
#define mp make_pair
#define pb push_back
#define pa pair < int,int >
#define fi first
#define se second
#define re register
#define be begin()
#define en end()
#define sqr(x) ((x)*(x))
#define ret return puts("-1"),0;
#define put putchar('\n')
#define inf 1000000005
#define mod 998244353
#define int ll
//#define N
using namespace std;
inline char gc(){static char buf[100000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}
#define gc getchar
inline ll read(){char c=gc();ll su=0,f=1;for (;c<'0'||c>'9';c=gc()) if (c=='-') f=-1;for (;c>='0'&&c<='9';c=gc()) su=su*10+c-'0';return su*f;}
inline void write(ll x){if (x<0){putchar('-');write(-x);return;}if (x>=10) write(x/10);putchar(x%10+'0');}
inline void writesp(ll x){write(x),putchar(' ');}
inline void writeln(ll x){write(x);putchar('\n');}
ll n,now;
signed main()
{
n=read();
for (int i=1;i<=n-1;i++)
{
now++;
while (now%3&&now%5&&now%7) now++;
writesp(now*2);
}
writeln(105);
}
/*
*/
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n;
cin >> n;
ll ans = n;
ll t = n + 1;
for (int i = 1; i <= n; i++) {
if (t >= i) t -= i, ans--;
else break;
}
cout << ans + 1 << endl;
} | /**
* Author : RDP
* There are no two words in the English language more harmful than "good job".
* 1729 ;)
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T)
{
cerr << " " << to_string(H);
debug_out(T...);
}
#define endl '\n'
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#define FAST_IO \
ios::sync_with_stdio(0); \
std::cin.tie(0); \
std::cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define PI 3.1415926535897932384626433832795
const ll MOD = 1000000007;
void test_case()
{
ll n, ans = 0;
cin >> n;
set<ll> s;
for (ll cur = 2; cur * cur <= n; cur++)
{
ll x = cur * cur;
while (x <= n)
{
ans++;
s.insert(x);
x *= cur;
}
}
cout << n - s.size();
return;
}
int main()
{
FAST_IO
int t = 1;
//cin >> t;
while (t--)
test_case();
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i,n) cin >> a[i];
sort(a.begin(), a.end());
map<int,int> m;
auto f = [&](int i,int v){
if(i>a[0]) return;
if(m[i]==0) m[i] = v;
m[i] = gcd(v,m[i]);
};
rep(i,n){
for(int x=1;x*x<=a[i];++x){
if(a[i]%x!=0) continue;
f(x,a[i]);
f(a[i]/x,a[i]);
}
}
int ans = 0;
for(auto&i:m) ans += i.first==i.second;
cout << ans << endl;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define rep(i, n) for(int i = 0; i < n; i++)
int main()
{
int n;
cin >> n;
cout << (n+100-1)/100 << endl;
return 0;
} |
/*ver 7*/
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : v)
#define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO" )
#define Yes(n) ((n) ? "Yes" : "No" )
#define yes(n) ((n) ? "yes" : "no" )
int main(){
init();
inl(n);
V<ll> a(n);
in1(a);
V<ll> goal(n);
V<ll> l(n),r(n);
ll now=0;
rep(i,n){
now+=a[i];
goal[i]=now;
if(i==0){
l[i]=min(0LL,now);
r[i]=max(0LL,now);
}else{
l[i]=min(l[i-1],now);
r[i]=max(r[i-1],now);
}
}
ll ansMax=0,ansMin=0;
now=0;
rep(i,n){
chmin(ansMin,now+l[i]);
chmax(ansMax,now+r[i]);
now+=goal[i];
}
out(ansMax);
return 0;
} | #include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stdio.h>
using namespace std;
int main(){
long long N;
cin >> N;
vector<long long> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
vector<long long> p(N); // 動作iでの合計の座標
vector<long long> q(N); // 動作iを座標0出始めた場合の最大値
long long now = 0; // 動作i-1での座標
// p[i]を求める
for (int i = 0; i < N; i++) {
p[i] = now + A[i];
now = p[i]; // 更新
}
// q[i]を求める
long long Max_i = 0;
for (int i = 0; i < N; i++) {
Max_i = max(Max_i, p[i]);
q[i] = Max_i;
}
long long r = 0; // 答え
long long x = 0; // 現在の座標
for (int i = 0; i < N; i++) {
r = max(r, x + q[i]);
x += p[i]; // 現在の座標が増える
}
cout << r << endl;
return 0;
} |
#include <algorithm>
#include <cmath>
#include <vector>
#include <functional>
#include <cstdlib>
#include <map>
#include <set>
#include <ctype.h>
#include <climits>
#include <queue>
#include <iostream>
#include <string>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
// #define rep(i, a, b) for(ll i = a; i < b; i++)
using namespace std;
typedef long long ll;
const long long INF = 1001001001;
const ll mod = 1e9 + 7; //10^9+7
// template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
// template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//using ll = long long;
//using P = pair<int,int>;
//const int INF = 1001001001;
//小数 printf("%.10f\n", ans);
//string s; int n = stoi(s);
//int number; string s = to_string(number);
//string s; printf("%s\n", s.c_str);
//シフト演算子(190) 2^n : 1<<n
//sのi桁目を取る(190) : (s>>i)&1
//if文にint型を入れたとき,0のときfalse,それ以外はtrue
//set<ll> s; 重複しない、順番を昇順にしてくれる
// UnionFind
//子: 親の番号(非負), 親: -(サイズ)(負)
struct UnionFind {
vector<int> d; //配列
UnionFind(int n=0): d(n,-1) {} //コンストラクタ,頂点数,最初は全て-1(根)であり、かつサイズは-1
int find(int x) {
if (d[x] < 0) return x; //頂点番号がマイナス、つまり根ならその頂点番号を返す
return d[x] = find(d[x]); //マイナスでないなら、その親についてfindを投げる. メモ化して経路縮約
}
bool unite(int x, int y) { //2つ頂点を持ってくる
x = find(x); y = find(y); //両方根に変換する. 根同士をくっつける.
if (x == y) return false; //連結成分が一致しているとき,何もすることはない. 連結失敗のfalse
if (d[x] > d[y]) swap(x,y); //d[]そのものの値はマイナスなので不等号の向きに注意. 本来はif(-d[x] < -d[y]). これにより,大きい方xに小さい方yをくっつけることができる. swapは入れ替え
d[x] += d[y]; //連結成分のサイズも管理しているのでサイズを合算する
d[y] = x; //両方根だったがyの親をxにはりかえる.
return true;
}
bool same(int x, int y) { return find(x) == find(y);} //同じ集合に蔵しているかどうか。
int size(int x) { return -d[find(x)];} //サイズ. 根のマイナスをとったものとなる.
};
int main () {
ll n;
ll x;
cin >> n >> x;
vector<ll> a(n);
rep(i,n) cin >> a[i];
int count = 0;
vector<ll> b(n);
rep(i,n) {
if(a[i] == x) continue;
b[count] = a[i];
count++;
}
rep(i,n) {
if(b[i] == 0) break;
cout << b.at(i);
cout << " ";
}
cout << "" << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)
{
int tt;
cin>>tt;
if(tt!=k)
{
cout<<tt<<" ";
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = 3.14159265358979;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
#define pii pair<int,int>
#define endl "\n"
#define dtor(deg) (((deg)/360)*2*pi)
#define all(a) a.begin(),a.end()
#define overload(_1,_2,_3,_4,name,...) name
#define _rep1(n) for(int i = 0; i < (n); i++)
#define _rep2(i,n) for(int i = 0; i < (n); i++)
#define _rep3(i,a,b) for(int i = (a); i < (b); i++)
#define _rep4(i,a,b,c) for(int i = (a); i < (b); i += (c))
#define rep(...) overload(__VA_ARGS__,_rep4,_rep3,_rep2,_rep1)(__VA_ARGS__)
#define _rrep1(n) for(int i = (n) - 1; i >= 0; i--)
#define _rrep2(i,n) for(int i = (n) - 1; i >= 0; i--)
#define _rrep3(i,a,b) for(int i = (b) - 1; i >= (a); i--)
#define _rrep4(i,a,b,c) for(int i = (b) - 1; i >= (a); i -= (c))
#define rrep(...) overload(__VA_ARGS__,_rrep4,_rrep3,_rrep2,_rrep1)(__VA_ARGS__)
#define vec(type,name,...) vector<type> name(__VA_ARGS__)
#define vv(type,name,size,...) vector<vector<type>> name(size,vector<type>(__VA_ARGS__))
#define ForEach(a,b) for_each(a.begin(),a.end(),b)
struct Edge { int to, cost; Edge(int to, int cost) : to(to), cost(cost) {} };
using Graph = vector<vector<Edge>>;
template <class T> bool chmin(T& a, T b){ if(a > b){ a = b; return 1; } return 0; }
template <class T> bool chmax(T& a, T b){ if(a < b){ a = b; return 1; } return 0; }
void Main(){
string n;
int k;
cin >> n >> k;
rep(k){
sort(all(n));
int a = stoi(n);
reverse(all(n));
int b = stoi(n);
n = to_string(b - a);
}
cout << n << endl;
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(10) << fixed;
Main();
} | // Author: Vinay Khilwani
// Language: C++
// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB, CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank
// Never Stop Trying.
// Trying to be Better than Myself.
// while(true)
// {
// if(AC)
// {
// break;
// }
// else if(Contest Over)
// {
// Try.
// Check out Editorial.
// Understand.
// Find out your Mistake.
// Learn the topic (if new).
// Solve Problems on that topic (if new).
// Upsolve that problem.
// break;
// }
// else
// {
// Try.
// Use Pen-Paper.
// Find errors, edge cases, etc.
// continue;
// }
// }
// Optimizations
// #pragma GCC optimize("O2")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2")
// #pragma GCC optimize("Os")
// Libraries
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// Debugging
#define dbg(a) cerr<<a<<"\n";
#define debug_a(a) for(auto x:a) {cerr<<x<<" ";} cerr<<"\n";
#define debug_b(a) for(auto x:a) {cerr<<"["<<x.first<<", "<<x.second<<"]"<<"\n";} cerr<<"\n";
#define debug_c(a) for(auto x:a) {debug_a(x)} cerr<<"\n";
#define debug_d(a) for(auto x:a) {debug_b(x)} cerr<<"\n";
#define debug_e(a) cerr<<"["<<a.first<<", "<<a.second<<"]"<<"\n";
// Defines
#define fast ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define loop(i,a,n) for(int i=a; i<n; i++)
#define rloop(i,a,n) for(int i=a; i>=n; i--)
#define fr(i,a,n,b) for(int i=a; i<n; i+=b)
#define rfr(i,a,n,b) for(int i=a; i>=n; i-=b)
#define IN cin>>
#define OUT cout<<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(),(a).end()
#define each(a,b) for(auto &a:b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a) for(auto x:a) {OUT x<<" ";} OUT endl;
#define read(a) for(auto &x:a) {IN x;}
// #define oset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vi=vector<int>;
const ll mod=(ll)(1e9)+7LL;
const ll M=998244353LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const ld pi=acos(-1);
// General Functions
ll gcd(ll a, ll b)
{
return (b?gcd(b,a%b):a);
}
ll P(ll B, ll power, ll modulo) //Fast Power
{
ll ans=1LL;
while(power>0LL)
{
if(power%2LL==1LL)
{
ans=(ans*B)%modulo;
}
B=(B*B)%modulo;
power/=2LL;
}
return ans;
}
bool isPrime(ll n)
{
if(n<=1LL)
{
return false;
}
if(n<=3LL)
{
return true;
}
if(n%2==0LL || n%3==0LL)
{
return false;
}
for(ll i=5LL; (i*i)<=n; i+=6LL)
{
if(n%i==0LL || n%(i+2LL)==0LL)
{
return false;
}
}
return true;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r)
{
uniform_int_distribution<ll> uid(l,r);
return uid(rng);
}
void vok()
{
fast
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
}
// Global Variables
const int mxN=int(1e5)+100;
// Solver Function(s)
void solve()
{
ll n,k;
IN n>>k;
vll v(k+1);
v[0]=n;
loop(i,1,k+1)
{
string s=to_string(v[i-1]);
string S=s;
sort(all(s),greater<>());
sort(all(S));
v[i]=stoi(s)-stoi(S);
}
OUT v.back()<<nl;
}
// Main Function
int main()
{
vok();
int t=1;
// IN t;
while(t--)
{
solve();
}
return 0;
} |
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
#define G getchar()
int read()
{
int x=0; bool flg=false; char ch=G;
for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
return flg?-x:x;
}
#undef G
#define fi first
#define se second
typedef long long ll;
/*const int mod=;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,const int &y){x=upd(x+y-mod);}
inline void iadd(int &x,const int &y){x=upd(x-y);}
int qpow(int x,int y){
int res=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1) res=1LL*res*x%mod;
return res;
}*/
#define rep(i,l,r) for (int i(l);i<=(r);i++)
#define per(i,l,r) for (int i(r);i>=(l);i--)
int n;
void solve(){
n=read();
rep(i,0,n-1){
int x=i*2%n,y=(x+1)%n;
printf("%d %d\n",x+1,y+1);
}
}
int main()
{
for (int T=1;T--;) solve();
}
| #include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
const int p = 31;
const int m = 1e9 + 7;
typedef long long ll;
typedef int in;
#define ff first
#define ss second
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...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int n;
in main () {
cin>>n;
if(n<=11){
int i=1;
// cout<<1<<" "<<(n+1)/2<<'\n';
while(i<=n){
cout<<max(i-1,1)<<" "<<min(i+1,n)<<'\n';
++i;
}
}
else{
cout<<(n+1)/2<<" "<<n<<'\n';
for(int i=2;i<n;++i){
cout<<(i+1)/2<<" "<<(n+i+1)/2<<'\n';
}
cout<<(n+1)/2<<" "<<1;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int A,B;
cin >> A >> B;
cout << 100 * (1 - double(B)/A) << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
/*-----<Defines>-----*/
#define int long long int
#define fast() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(x) begin(x),end(x)
#define rz(x) resize(x)
#define asn(x,y) assign(x,y)
#define mem(a,b) memset(a,b,sizeof(a))
#define sz(x) ((int)size(x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ins insert
#define vi vector<int>
#define pii pair<int,int>
#define mii map<int,int>
#define F first
#define S second
#define remax(a,b) a=max(a,b)
#define remin(a,b) a=min(a,b)
#define bitcount(x) __builtin_popcountll(x)
#define iceil(n,x) (((n)-1)/(x)+1)
#define flush fflush(stdout)
using ull=unsigned long long;
using ll=long long;
using ld=long double;
/*-----</Defines>-----*/
/*-----<Mod Operations>-----*/
const int M=1e9+7;
inline int add(int a,int b,int p=M){ a=a+b; if(a>=p) a-=p; return a; }
inline int sub(int a,int b,int p=M){ a=a-b; if(a<0) a+=p; return a; }
inline int mul(int a,int b,int p=M){ a=(a*1ll*b)%p; return a; }
/*-----</Mod Operations>-----*/
/*-----<Constants>-----*/
const ld pi=acos(-1),eps=1e-9;
const ll inf=1e18;
const int N=1e6+5;
/*-----</Constants>-----*/
void solve()
{
int n,k;
cin>>n>>k;
if(min(n,k)+3>max(n,k))
cout<<"Yes\n";
else
cout<<"No\n";
}
int32_t main()
{
fast();
int t;
t=1;
for(int z=1;z<=t;++z)
{
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
template <class T1, class T2>
bool chmax(T1 &a, const T2 b) { if (a < b) { a = b; return true; } return false; }
template <class T1, class T2>
bool chmin(T1 &a, const T2 b) { if (a > b) { a = b; return true; } return false; }
template <class T>
istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; }
void co() { cout << '\n'; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) { cout << head << ' '; co(forward<Tail>(tail)...); }
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); }
void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); }
void setp(const int n) { cout << fixed << setprecision(n); }
constexpr int64_t LINF = 1000000000000000001;
constexpr int64_t MOD = 1000000007;
constexpr int64_t MOD_N = 998244353;
constexpr long double EPS = 1e-11;
const double PI = acos(-1);
int64_t extGCD(int64_t a, int64_t b, int64_t &x, int64_t &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
int64_t d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
pair<int64_t, int64_t> chinese_rem(const vector<int64_t> &b, const vector<int64_t> &m){
int64_t r = 0, M = 1;
for (int64_t i = 0; i < b.size(); ++i) {
int64_t p, q;
int64_t d = extGCD(M, m[i], p, q);
if ((b[i] - r) % d != 0) return {0, -1};
int64_t tmp = (b[i] - r) / d * p % (m[i] / d);
r += M * tmp;
M *= m[i] / d;
}
return {(r % M + M) % M, M};
}
int64_t gcd(int64_t a, int64_t b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
void solve() {
ll n, s, k;
cin >> n >> s >> k;
Vec m = {n, k}, b = {n - s, 0};
auto p = chinese_rem(b, m);
// ce(p.first, p.second);
if (p.second == -1) co(-1);
else
co(p.first / k);
}
int main(void) {
ll t;
cin >> t;
while (t--) solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return u;
}
int32_t main(){
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
while(T--){
int n,s,k;
cin >> n >> s >> k;
int g=__gcd(n,k);
if(s%g){
cout << "-1\n";
continue;
}
n/=g,s/=g,k/=g;
int i=(inverse(k,n)%n+n)%n;
cout << (n-s)%n*i%n << '\n';
}
} |
/**
* author: tomo0608
* created: 22.05.2021 20:58:10
**/
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include<atcoder/all>
using namespace atcoder;
#endif
typedef long long ll;
typedef long double ld;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
#define all(x) x.begin(),x.end()
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define unique(a) a.erase(unique(a.begin(),a.end()),a.end())
template<class... T>void input(T&... a){(cin >> ... >> a);};
#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define STRING(...) string __VA_ARGS__; input(__VA_ARGS__)
void print(){cout << '\n';}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a; (cout << ... << (cout << ' ', b)); cout << '\n';}
template<class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T> >;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<typename T> ostream& operator << (ostream& os, set<T>& set_var) {os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {os << *itr;++itr;if(itr != set_var.end()) os << ", ";itr--;}os << "}";return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) {os << "{";for(auto itr = map_var.begin(); itr != map_var.end(); itr++) {os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--;}os << "}";return os;}
#ifdef __LOCAL
void debug_out(){ cerr << endl;}
template < class Head, class... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...);}
#define debug(...) cerr << 'L' << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << 'L' << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
template<class T> inline int count_between(vector<T> &a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r)
#define pb push_back
#define eb emplace_back
#define elif else if
#define mp make_pair
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
template<typename T> T gcd(T x, T y){if(x%y == 0)return y;return gcd(y, x%y);}
template<typename T> T gcd(vector<T> a){T res = a[0];for(auto &x: a)res = gcd(res, x);return res;}
template <typename T>T mypow(T x, ll n){T ret = 1;while(n > 0){if(n & 1)(ret *= x);(x *= x);n >>= 1;}return ret;}
#define endl '\n'
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
void solve(){
INT(n);
V<int> a(n),b(n),c(n);cin >> a >> b >> c;
V<ll> A(n+1,0),B(n+1,0);
rep(i,n){
A[a[i]]++;
B[b[c[i]-1]]++;
}
ll ans = 0;
rep(i,n+1)ans += A[i]*B[i];
print(ans);
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << setprecision(20);
int codeforces = 1;
//cin >> codeforces;
while(codeforces--){
solve();
}
return 0;
} | /****Jai Ganesha Deva****/
#include<bits/stdc++.h>
using namespace std ;
#define int long long
#define sz(a) (int)a.size()
#define INF 2000000000000000000
#define double long double
#define bug(x) cerr<<#x<<" is "<<x<<endl
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
const int M = 1000000007;
const int MM = 998244353;
const double PI = acos(-1);
int tError()
{
int n;cin>>n;n*=2;
string s;cin>>s;
int q;cin>>q;
int state =0;
while(q--)
{
int t,a,b;cin>>t>>a>>b;a--;b--;
if(t==2)state = !state;
else
{
if(state==0)
swap(s[a],s[b]);
else
{
if(a>=n/2)
a=a-n/2;
else
a=a+n/2;
if(b>=n/2)
b=b-n/2;
else
b=b+n/2;
swap(s[a],s[b]);
}
}
}
if(state ==0)cout<<s;
else
{
for (int i = 0; i < n/2; ++i)
{
/* code */
swap(s[i],s[n/2+i]);
}
cout<<s;
}
return 0;
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
/*#ifndef ONLINE_JUDGE
freopen("input.txt" ,"r",stdin) ;
freopen("output.txt","w",stdout);
#endif
*/
int TESTS=1 ;
// cin>>TESTS;
while(TESTS--){
tError();
}
//clock_t z=clock();
//debug("Total Time:%.4Lf\n",(double)(clock()-z)/CLOCKS_PER_SEC);
return 0 ;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=200010;
const double PI=acos(-1.0);
signed main(){
ll n;cin>>n;
vector<ll>a(0),b(0);
vector<ll>c(2*n);
rep(i,2*n){
cin>>c[i];
if(i%2==0){
a.pb(c[i]);
}else{
b.pb(c[i]);
}
}
sort(all(a)),sort(rall(b));
ll idx=-1;
rep(i,n){
if(b[i]<=a[i]){
idx=i;
break;
}
}
string ans="";
if(idx==-1){
rep(i,n){
ans+="()";
}
}else{
map<ll,ll>aa;
map<ll,ll>bb;
rep(i,idx){
aa[a[i]]++;
bb[b[i]]++;
}
ll x=0,y=0,z=0,w=0;
rep(i,n*2){
if(i%2==0){
if(y>0){
if(aa[c[i]]>0){
ans+=')';
aa[c[i]]--;
y--;
}else if(w>0){
w--;
ans+=')';
}else{
z++;
ans+='(';
}
}else{
if(aa[c[i]]>0){
aa[c[i]]--;
ans+='(';
x++;
}else if(w>0){
w--;
ans+=')';
}else{
z++;
ans+='(';
}
}
}else{
if(x>0){
if(bb[c[i]]>0){
ans+=')';
bb[c[i]]--;
x--;
}else if(z>0){
z--;
ans+=')';
}else{
w++;
ans+='(';
}
}else{
if(bb[c[i]]>0){
bb[c[i]]--;
ans+='(';
y++;
}else if(z>0){
z--;
ans+=')';
}else{
w++;
ans+='(';
}
}
}
}
}
cout<<ans<<endl;
} | #include<bits/stdc++.h>
using namespace std;
int get() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)) { if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
const int N = 4e5 + 5;
int n, a[N], len, st[N], top, col[N];
pair<int, int> b[N];
int main() {
n = get();
for(int i = 1; i <= 2 * n; i++) a[i] = get(), b[i] = make_pair(a[i], i);
sort(b + 1, b + 1 + 2 * n);
for(int i = 1; i <= n; i++) col[b[i].second] = 0;
for(int i = n + 1; i <= n + n; i++) col[b[i].second] = 1;
for(int i = 1; i <= 2 * n; i++) {
if(!top) st[++top] = i, printf("(");
else if(col[st[top]] != col[i]) printf(")"), top--;
else printf("("), st[++top] = i;
}
return 0;
}
|
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string>
using namespace std;
const int N = 1e8 + 10;
typedef long long ll;
int main(){
int n,m,t,k,ans;
string s;
// cin>>t;
// while(t--){
cin>>s;
int ok=1;
for(int i=0;i<s.size()&&ok;i++){
if(i%2==0&&(s[i]<'a'||s[i]>'z'))ok=0;
if(i%2==1&&(s[i]<'A'||s[i]>'Z'))ok=0;
}
if(ok)cout<<"Yes\n";
else cout<<"No\n";
// }
return 0;
}
| #include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define fast_az_fuk ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define ull unsigned ll
#define ld long double
#define pb push_back
#define pf push_front
#define dll deque<ll>
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define dpll deque<pll>
#define mapll map<ll,ll>
#define umapll umap<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define ms(a,x) memset(a,x,sizeof(a))
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
vector<string> split(const string& s, char c) {
vector<string> v; stringstream ss(s); string x;
while (getline(ss, x, c)) v.push_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
stringstream s; s << "[";
for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
s << arr[n - 1] << "]";
return s.str();
}
#define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __evars_out_var(T val) { cerr << val; }
inline void __evars(vector<string>::iterator it) { cerr << endl; }
template<typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
__evars_out_var(a);
cerr << "; ";
__evars(++it, args...);
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
template<class T>using uset=unordered_set<T,custom_hash>;
bool isLoweCase(char c){
return (c-'a'>=0);
}
int32_t main()
{
clock_t clk = clock();
fast_az_fuk
string s; cin>>s; ll n = s.length();
bool isUnreadable=1;
for(ll i=0;i<n;i+=2){
if(!isLoweCase(s[i])) {isUnreadable=0; break;}
}
if(!isUnreadable) {cout<<"No\n"; return 0;}
for(ll i=1;i<n;i+=2){
if(isLoweCase(s[i])) { isUnreadable=0; break;}
}
if(!isUnreadable) cout<<"No\n"; else cout<<"Yes\n";
// cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
return 0;
} |
#include <bits/stdc++.h>
#define ll long long int
#define uu first
#define vv second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define tp tuple<int,int,int>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const int N = 1e6 + 6;
int main() {
fastio;
int t;
cin>>t;
string f = "First";
string s = "Second";
while(t--) {
int n;
cin>>n;
map<int,int>mp;
for(int i =0 ; i < n ;i++ ) {
int tmp;
cin>>tmp;
mp[tmp]++;
}
if(n%2) {
cout<<s<<'\n';
continue;
}
int ok = 0;
for(auto x : mp) {
if(x.vv%2) ok = 1;
}
if(ok) cout<<f<<'\n';
else cout<<s<<'\n';
}
return 0;
} | /*
このコード、と~おれ!
Be accepted!
∧_∧
(。・ω・。)つ━☆・*。
⊂ ノ ・゜+.
しーJ °。+ *´¨)
.· ´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·'* ☆
*/
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
////多倍長整数, cpp_intで宣言
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
//
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a) //少数出力
#define printLdb(a) printf("%.50Lf\n", a) //少数出力
#define printdbd(a) printf("%.16lf\n", a) //少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) //string出力
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()
using ll = long long;
using ull = unsigned long long;
constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);
using namespace std;
void scans(string& str) {
char c;
str = "";
scanf("%c", &c);
if (c == '\n')scanf("%c", &c);
while (c != '\n' && c != -1 && c != ' ') {
str += c;
scanf("%c", &c);
}
}
void scanc(char& str) {
char c;
scanf("%c", &c);
if (c == -1)return;
while (c == '\n') {
scanf("%c", &c);
}
str = c;
}
double acot(double x) {
return PI / 2 - atan(x);
}
ll LSB(ll n) { return (n & (-n)); }
template<typename T>
inline T chmin(T& a, const T& b) {
if (a > b)a = b;
return a;
}
template<typename T>
inline T chmax(T& a, const T& b) {
if (a < b)a = b;
return a;
}
////atcoder library
//#include <atcoder/all>
//using namespace atcoder;
/*-----------------------------------------ここからコード-----------------------------------------*/
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
vector<int> a(n);
rep(i, n)scanf("%d", &a[i]);
if (n & 1)puts("Second");
else {
ll fst = 0, scd = 0;
sort(all(a), greater<int>());
rep(i, n / 2)fst += a[i * 2];
rep(i, n / 2)scd += a[i * 2 + 1];
puts(fst > scd ? "First" : "Second");
}
}
Please AC;
}
|
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
typedef long long ll;
typedef long double lld;
#define fo(i,n) for(int i=0;i<(int)n;i++)
#define rep(i,x,n) for(auto i=x;i<n;i++)
#define ch " "
#define nline "\n"
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
const int MOD=1e9+7;
const int INF=1e9;
bool pow2(int x){
return x && (!(x&(x-1)));
}
void solve(){
int n;
cin>>n;
ll a[n];
for(auto &x:a) cin>>x;
sort(a,a+n);
ll ans=0;
ll sum=0;
for(int i=0;i<n;i++){
ans+=(i*a[i])-sum;
sum+=a[i];
}
// for(int i=0;i<n;i++){
// ans+=((i*a[i])-((n-i-1)*a[i]));
// }
// ll total=accumulate(a,a+n,0);
// ll sum=0;
// for(int i=0;i<n;i++){
// sum+=a[i];
// ll sumleft=total-sum;
// ans+=((i*a[i])-((n-i-1)*a[i]) + sumleft-sum);
// }
// ans/=2;
cout<<ans<<nline;
}
int main(){
fastio;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
auto start=chrono::high_resolution_clock::now();
int t=1;
// cin>>t;
for(int i=1;i<=t;i++){
// cout<<"Case #"<<i<<": ";
solve();
}
auto end=chrono::high_resolution_clock::now();
double time_taken=chrono::duration_cast<chrono::nanoseconds>(end-start).count();
time_taken*=1e-9;
// cout<<"time taken: "<<fixed<<time_taken<<setprecision(9);
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define mat vector<vector<int>>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define pi pair<int, int>
#define inf 1000000000
#define mod 998244353
#define endl "\n"
const ll INF=1e18+5;
const int naxN=30;
int fact[naxN], inv_fact[naxN];
int power(int a, int n){
int res=1;
while(n){
if(n%2) res=(res*a)%mod, n--;
else a=(a*a)%mod, n/=2;
}
return res;
}
void init(){
fact[0]=inv_fact[0]=1;
for(int i=1; i<naxN; i++){
fact[i]=(i*fact[i-1])%mod;
inv_fact[i]=power(fact[i], mod-2)%mod;
}
}
int ncr(int a, int b){
if(a<0 || b<0 || a<b) return 0;
return (fact[a]%mod*inv_fact[b]%mod*inv_fact[a-b]%mod)%mod;
}
//int dx[8]={-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8]={-1, 0, 1, 1, 1, 0, -1, -1};
int par[30];
int R[30];
int find(int a){
if(par[a]<0) return a;
return par[a]=find(par[a]);
}
void merge(int a, int b){
a=find(a);
b=find(b);
if(a==b) return;
if(R[a]>=R[b]){
par[b]=a;
R[a]+=R[b];
}
else{
par[a]=b;
R[b]+=R[a];
}
}
const int naxP=30;
int prime[naxP];
void sieve(){
prime[0]=1;
prime[1]=1;
for(int i=2; i*i<=naxP; i++){
if(prime[i]==0){
for(int j=i*i; j<=naxP; j+=i){
prime[j]=1;
}
}
}
}
//****DO NOT TOUCH ABOVE THIS LINE****//
//#define kk
const int maxN=2e5+5;
int arr[maxN];
void solve(){
int n;
cin>>n;
for(int i=1; i<=n; i++) cin>>arr[i];
sort(arr+1, arr+n+1);
for(int i=1; i<=n; i++) arr[i]+=arr[i-1];
int sum1=0, sum2=0;
for(int i=1; i<n; i++){
sum1+=(arr[n]-arr[i]);
}
for(int i=1; i<n; i++){
sum2+=(arr[i]-arr[i-1])*(n-i);
}
cout<<sum1-sum2<<endl;
}
//****DO NOT TOUCH BELOW THIS LINE****//
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout<<fixed<<setprecision(6);
#ifdef kk
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
int T=1;
//cin>>T;
while(T--){
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;
const int n = 20;
const int n2 = 1<<n;
const vector<int> p = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
int main() {
ll a, b;
cin >> a >> b;
vector<ll> dp(n2);
dp[0] = 1;
for (ll x = a; x <= b; x++) {
int s = 0;
rep(i, n) if(x % p[i] == 0) s |= 1 << i;
rep(i, n2) {
if (i & s) continue;
dp[i|s] += dp[i];
}
}
ll ans = 0;
rep(i, n2) ans += dp[i];
cout << ans << endl;
return 0;
} | //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <cassert>
#include <memory>
#include <random>
#include <functional>
#include <complex>
#include <immintrin.h>
#ifdef DEBUG
#include "./CompetitiveProgrammingCpp/debug_VC.hpp"
#include "./CompetitiveProgrammingCpp/Timer.hpp"
#include "./CompetitiveProgrammingCpp/sample.hpp"
#else
#define dump(...)
#endif
/* macro */
#define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x,c) for(const auto& x:(c))
#define REPI2(it,b,e) for(auto it = (b); it != (e); ++it)
#define REPI(it,c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(),(x).end()
#define cauto const auto&
/* macro func */
template<class T>
inline auto sort(T& t) { std::sort(ALL(t)); }
template<class T>
inline auto rsort(T& t) { std::sort((t).rbegin(), (t).rend()); }
template<class T>
inline auto unique(T& t) { (t).erase(unique((t).begin(), (t).end()), (t).end()); }
template<class T, class S>
inline auto chmax(T& t, const S& s) { if (s > t) { t = s; return true; } return false; }
template<class T, class S>
inline auto chmin(T& t, const S& s) { if (s < t) { t = s; return true; } return false; }
inline auto BR() { std::cout << "\n"; }
/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template<class T>
using V = std::vector<T>;
/* using std */
using std::cout;
constexpr char endl = '\n';
using std::cin;
using std::pair;
using std::string;
using std::stack;
using std::queue;
using std::vector;
using std::list;
using std::map;
using std::unordered_map;
using std::multimap;
using std::unordered_multimap;
using std::set;
using std::unordered_set;
using std::unordered_multiset;
using std::multiset;
using std::bitset;
using std::priority_queue;
/* Initial processing */
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;
/* Remove the source of the bug */
signed pow(signed, signed) { assert(false); return -1; }
/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> { public: size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } };
}
/* input */
template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for (T& x : vec) is >> x; return is; }
/* constant value */
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
//=============================================================================================
signed main() {
ll n;
cin >> n;
VD v(n);
cin >> v;
auto val = [&](double d) {
double ans = 0.0;
REPC(x, v) {
ans += (d + x) / n;
ans -= std::min(x, 2.0 * d) / n;
}
return ans;
};
double low = 0;
double high = 1e15;
int cnt = 500;
while (cnt--) {
double c1 = (low * 2 + high) / 3;
double c2 = (low + high * 2) / 3;
if (val(c1) > val(c2)) low = c1;
else high = c2;
}
cout << std::fixed << std::setprecision(15)
<< val(low) << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
int n,m;
int edge[1001][1001];
bool visited[1001][1001];
vector<int> vc[1001][26];
struct PT{
int st,ed;
int size;
};
int main(){
int x,y,sz;
char c;
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>x>>y>>c;
edge[x][y]=edge[y][x]=1;
vc[x][c-'a'].push_back(y);
vc[y][c-'a'].push_back(x);
}
bool flag=false;
queue<PT> q;
PT temp,in;
temp.st=1; temp.ed=n; temp.size=0;
visited[temp.st][temp.ed]=true;
q.push(temp);
int ans=1e9;
while(!q.empty()){
temp=q.front();
q.pop();
x=temp.st; y=temp.ed; sz=temp.size;
if(edge[x][y]>0||x==y){
if(x==y) ans=min(ans,sz);
else ans=min(ans,sz+1);
}
for(int t=0;t<26;t++){
for(auto i:vc[x][t]){
for(auto j:vc[y][t]){
if(visited[i][j]) continue;
visited[i][j]=true;
temp.st=i; temp.ed=j; temp.size=sz+2;
q.push(temp);
}
}
}
}
if(ans==1e9) cout<<"-1\n";
else cout<<ans<<'\n';
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(40)
const ll MOD = 1000000007;
const ll INF = (ll) 1e18;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, M;
cin >> N >> M;
ll n = N * N;
vector<ll> dist(n, INF);
vector<vector<ll>> V(n);
vector<vector<pll>> E(26);
REP(i, M) {
char c;
ll a, b;
cin >> a >> b >> c;
a--;
b--;
E[c - 'a'].push_back(pll(a, b));
}
REP(c, 26) {
REP(i, E[c].size()) {
FOR(j, E[c].size(), i + 1) {
vector<ll> v1 = {E[c][i].first, E[c][i].second};
vector<ll> v0 = {E[c][j].first, E[c][j].second};
REP(x, 2) {
REP(y, 2) {
{
ll idx0 = v0[x] * N + v1[y];
ll idx1 = v0[(x + 1) % 2] * N + v1[(y + 1) % 2];
V[idx0].push_back(idx1);
V[idx1].push_back(idx0);
}
{
ll idx0 = v1[x] * N + v0[y];
ll idx1 = v1[(x + 1) % 2] * N + v0[(y + 1) % 2];
V[idx0].push_back(idx1);
V[idx1].push_back(idx0);
}
}
}
}
}
}
ll s = N - 1;
dist[s] = 0;
queue<ll> q;
q.push(s);
while (!q.empty()) {
ll t = q.front();
q.pop();
ll cost = dist[t] + 1;
REP(i, V[t].size()) {
ll nxt = V[t][i];
if (dist[nxt] <= cost)
continue;
dist[nxt] = cost;
q.push(nxt);
}
}
ll ans = INF;
REP(i, N) {
ll idx = i * N + i;
ans = std::min(dist[idx] * 2, ans);
}
REP(c, 26) {
REP(i, E[c].size()) {
ll a = E[c][i].first, b = E[c][i].second;
ll idx0 = a * N + b;
ll idx1 = b * N + a;
ans = std::min({ans, dist[idx0] * 2 + 1, dist[idx1] * 2 + 1});
}
}
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int INF = 1e9;
int dp[1 << 18][18];
int shortestTSPdistance(vector<vector<int> > dist) {
int n = dist.size();
int lim = 1 << n;
for (int i = 0; i < n; ++i)
for (int j = 0; j < lim; ++j)
dp[j][i] = INF;
for (int i = 0; i < 1; i++) {
dp[1 << i][i] = 0;
}
for (int mask = 0; mask < lim; mask++) {
for (int last = 0; last < n; last++) {
if (mask && (1 << last) == 0) {
continue;
}
for (int curr = 0; curr < n; curr++) {
if (mask && (1 << curr) == 0) {
continue;
}
int otherMask = mask ^ (1 << curr);
dp[mask][curr] = min(dp[mask][curr], dp[otherMask][last] + dist[last][curr]);
}
}
}
int ans = INF;
for (int i = 0; i < 1; i++) {
ans = min(ans, dp[lim - 1][i]);
}
return ans;
}
int main() {
int n;
cin >> n;
vector <vector<int>> vec;
for (int i = 0; i < n; ++i) {
vector <int> temp(3);
for (int& a : temp)
cin >> a;
vec.pb(temp);
}
vector <vector<int>> dist;
for (int i = 0; i < n; ++i) {
vector <int> temp;
for (int j = 0; j < n; ++j) {
temp.pb(abs(vec[i][0] - vec[j][0]) + abs(vec[i][1] - vec[j][1]) + max(0, vec[j][2] - vec[i][2]));
}
dist.pb(temp);
}
cout << shortestTSPdistance(dist) << endl;
} | #include <bits/stdc++.h>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define reset(a, v) memset((a), v, sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
int n;
pair<ii, int> dat[20];
int dp[20][(1<<17)+5];
int calcDist(pair<ii, int> A, pair<ii, int> B) {
return abs(A.fi.fi - B.fi.fi) + abs(A.fi.se - B.fi.se) + max(0, B.se - A.se);
}
int DP(int cur, int mask) {
int& ret = dp[cur][mask];
if (ret != -1) return ret;
if (n == __builtin_popcount(mask)) return ret = calcDist(dat[cur], dat[0]);
ret = INT_MAX;
for (int nxt = 1; nxt < n; nxt++) {
if (mask & (1<<nxt)) continue;
int nxtDist = calcDist(dat[cur], dat[nxt]);
ret = min(ret, nxtDist + DP(nxt, mask | (1<<nxt)));
}
return ret;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x, y, z; scanf("%d %d %d", &x, &y, &z);
dat[i] = {{x,y},z};
}
reset(dp,-1);
printf("%d\n", DP(0, 1));
return 0;
} |
#include<cstdio>
#include<cmath>
#define db double
#define ri register int
#define sqr(x) ((x)*(x))
#define eps 1e-6
using namespace std;
const int MAXN=100+10;
int n;
int x[MAXN],y[MAXN];
db sdis(int i,int j){return sqr(x[i]-x[j])+sqr(y[i]-y[j]);}
int fa[MAXN];
int get_fa(int u){return u==fa[u]?u:fa[u]=get_fa(fa[u]);}
void merge(int u,int v){fa[get_fa(u)]=get_fa(v);}
bool check(db r)
{
for(ri i=0;i<=n+1;++i)fa[i]=i;
for(ri i=1;i<=n;++i)
for(ri j=i+1;j<=n;++j)if(sdis(i,j)<sqr(r*2))
merge(i,j);
for(ri i=1;i<=n;++i)if(sqr(100-y[i])<sqr(r*2))merge(i,0);
for(ri i=1;i<=n;++i)if(sqr(y[i]+100)<sqr(r*2))merge(i,n+1);
return get_fa(0)!=get_fa(n+1);
}
int main()
{
// freopen("1.in","r",stdin);
scanf("%d",&n);
for(ri i=1;i<=n;++i)scanf("%d %d",&x[i],&y[i]);
db l=0,r=100,m;
while(r-l>eps)
{
m=(l+r)/2;
if(check(m))l=m;
else r=m;
}
printf("%lf\n",l);
return 0;
} | //
// main.cpp
//
#include <algorithm>
#include <array>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <limits>
#include <inttypes.h>
#include <map>
#include <math.h>
#include <memory>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <atcoder/dsu>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
// using mint = modint;
using namespace std;
using ll = int64_t;
using ull = uint64_t;
[[maybe_unused]] constexpr ll LL_MAX = numeric_limits<ll>::max();
[[maybe_unused]] constexpr ll LL_MIN = numeric_limits<ll>::min();
[[maybe_unused]] constexpr ull ULL_MAX = numeric_limits<ull>::max();
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (a)-1; i >= (b); i--)
template<typename T>
void chmin(T& x, T y) {
x = min(x, y);
}
template<typename T>
void chmax(T& x, T y) {
x = max(x, y);
}
ll csum(ll x, ll y) {
if (x > 0 && y > 0 && x > LL_MAX - y) {
return LL_MAX;
}
if (x < 0 && y < 0 && x < LL_MIN - y) {
return LL_MIN;
}
return x + y;
}
ll readi() {
ll ret;
scanf("%" PRId64 "", &ret);
return ret;
}
vector<ll> readvi(ll n) {
vector<ll> ret(n);
rep(i, 0, n) { ret[i] = readi(); }
return ret;
}
double readf() {
double ret;
scanf("%lf", &ret);
return ret;
}
string reads() {
string s;
cin >> s;
return s;
}
void writei(ll x) {
printf("%" PRId64 "\n", x);
}
void writevi(const vector<ll>& xs) {
rep(i,0,xs.size()) {
if (i < xs.size() - 1) {
printf("%" PRId64 " ", xs[i]);
} else {
printf("%" PRId64 "\n", xs[i]);
}
}
}
void writes(const string& s) {
cout << s.c_str() << endl;
}
void writef(double x) {
printf("%.10f\n", x);
}
template<typename T>
vector<T> make_vec_nd(T init, ll size) {
return vector<T>(size, init);
}
template<typename T, typename... Args>
auto make_vec_nd(T init, ll size, Args... rest) {
auto inner = make_vec_nd(init, rest...);
return vector<decltype(inner)>(size, inner);
}
struct UnionFind {
vector<size_t> parent;
vector<size_t> rank;
size_t find_parent(size_t x) {
if (parent[x] == x) {
return x;
} else {
parent[x] = find_parent(parent[x]);
return parent[x];
}
}
explicit UnionFind(size_t size) {
parent.resize(size);
rank.resize(size);
for (size_t i = 0; i < size; i++) {
parent[i] = i;
rank[i] = 0;
}
}
virtual ~UnionFind() {}
void unite(size_t x, size_t y) {
size_t px = find_parent(x);
size_t py = find_parent(y);
if (px == py) {
return;
}
if (rank[px] < rank[py]) {
parent[px] = py;
} else if (rank[px] > rank[py]) {
parent[py] = px;
} else { // rank[px] == rank[py]
parent[py] = px;
rank[px]++;
}
}
bool is_same(size_t x, size_t y) {
return find_parent(x) == find_parent(y);
}
unordered_map<size_t, set<size_t>> create_map_parent_to_elements() {
unordered_map<size_t, set<size_t>> ans;
for (size_t i = 0; i < parent.size(); i++) {
ans[find_parent(i)].insert(i);
}
return ans;
}
};
struct P {
double x;
double y;
double dist(const P& rhs) const {
double sq = (x - rhs.x) * (x - rhs.x) + (y - rhs.y) * (y - rhs.y);
return sqrt(sq);
}
};
int main() {
ll N = readi();
vector<P> pts(N);
rep(i,0,N) {
pts[i].x = readi();
pts[i].y = readi();
}
double good = 0.0;
double bad = 100.0;
while (bad - good > 1.0e-5) {
double mid = (good + bad) / 2;
UnionFind tree(N + 2);
rep(i,0,N) {
P a = pts[i];
rep(j,i+1,N) {
P b = pts[j];
if (a.dist(b) < 2*mid) {
tree.unite(i, j);
}
}
if (a.y + 100 < 2*mid) {
tree.unite(i, N);
}
if (100 - a.y < 2*mid) {
tree.unite(i, N+1);
}
}
if (tree.is_same(N, N+1)) {
bad = mid;
} else {
good = mid;
}
}
writef(good);
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}
int main() {
ll n,input;
string input2;
cin>>n;
vector<ll>hight(n);
map<ll,string>m;
rep(i,n){
cin>>input2;
cin>>input;
hight[i]=input;
m[input]=input2;
}
sort(hight.begin(),hight.end());
cout<<m[hight[n-2]]<<endl;
} | #include <bits/stdc++.h>
using namespace std;
vector <string> solve(int x) {
if (x == 1) {
vector <string> tmp{"AB"};
return tmp;
}
vector <string> a, b;
a = solve(x - 1);
for (auto s : a) {
string r, t;
for (auto c : s)
if (c == 'A') r += "AB", t += "AA";
else r += "BA", t += "BB";
b.push_back(r);
b.push_back(t);
}
string s;
for (int i = 1; i <= (1 << (x - 1)); i ++) s += "AB";
b.push_back(s);
return b;
}
int main() {
int x; cin >> x;
vector <string> tmp = solve(x);
cout << tmp.size() << endl;
for (auto s : tmp) cout << s << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL N, fib[100];
vector<int> ans;
int vis[100];
void print() {
LL x = 0, y = 0;
for (int i : ans) {
if (i == 1) x++;
if (i == 2) y++;
if (i == 3) x = x + y;
if (i == 4) y = x + y;
}
cerr << x << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
fib[0] = fib[1] = 1;
for (int i = 2; i < 90; i++) fib[i] = fib[i - 1] + fib[i - 2];
cin >> N;
for (int i = 89; ~i; i--)
if (N >= fib[i]) N -= fib[i], vis[i] = 1;
int lim = 0;
for (int i = 1; i < 90; i++) if (vis[i]) lim = i;
for (int i = lim, j = 1; ~i; i--, j++) {
if (vis[i]) {
if (i & 1) ans.push_back(2);
else ans.push_back(1);
}
if (j & 1) ans.push_back(4);
else ans.push_back(3);
}
//print();
printf("%lu\n", ans.size());
for (int i : ans) printf("%d\n", i);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop(i, a) for (int i = 0; i < (a); ++i)
#define cont(i, a) for (int i = 1; i <= (a); ++i)
#define circ(i, a, b) for (int i = (a); i <= (b); ++i)
#define range(i, a, b, c) for (int i = (a); (c) > 0 ? i <= (b) : i >= (b); i += (c))
#define parse(it, x) for (auto &it : (x))
#define pub push_back
#define pob pop_back
#define emb emplace_back
#define mak make_pair
#define mkt make_tuple
typedef long long ll;
typedef long double lf;
const int Inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll n;
ll calc(ll x) {
ll y = n;
ll cs = 0;
while (y) {
cs += x / y;
x %= y;
swap(x, y);
}
return cs + x;
}
int main() {
cin >> n;
while (1) {
ll m = uniform_int_distribution<ll>(0, n)(Rand);
if (calc(m) <= 130) {
ll x = n, y = m;
vector<int> ans;
while (x && y) {
if (x < y) {
y -= x;
ans.pub(4);
}
else {
x -= y;
ans.pub(3);
}
}
while (x) --x, ans.pub(1);
while (y) --y, ans.pub(2);
reverse(all(ans));
printf("%d\n", SZ(ans));
parse(i, ans) printf("%d\n", i);
return 0;
}
}
} |
#include <bits/extc++.h>
using namespace std;
using ll = long long;
#define REP(i,n) for(int i=0;i<int(n);i++)
#define FOR(i,a,b) for(int i=a;i<=int(b);i++)
#define ALL(x) x.begin(),x.end()
#define MOD (ll)1000000007
set<ll> ans;
vector<pair<ll, ll>> xy;
signed main() {
ll n;
int m;
cin >> n >> m;
ans.insert(n);
xy.resize(m);
REP(i, m){
ll x, y;
cin >> x >> y;
xy[i] = make_pair(x, y);
}
ll xb = 0;
if(m > 0) {
sort(xy.begin(), xy.end());
xb = xy[0].first;
}
vector<ll> dellist(0);
vector<ll> addlist(0);
REP(i, m){
ll x, y;
tie(x, y) = xy[i];
if(xb != x){
for(ll del: dellist){
if(ans.find(del) != ans.end()) ans.erase(del);
}
for(ll add: addlist){
if(ans.find(add) == ans.end()) ans.insert(add);
}
dellist.clear();
addlist.clear();
}
if(ans.find(y) != ans.end()){
dellist.push_back(y);
}
if(y - 1 >= 0){
if(ans.find(y - 1) != ans.end()){
addlist.push_back(y);
}
}
if(y + 1 <= 2 * n){
if(ans.find(y + 1) != ans.end()){
addlist.push_back(y);
}
}
xb = x;
}
for(ll del: dellist){
if(ans.find(del) != ans.end()) ans.erase(del);
}
for(ll add: addlist){
if(ans.find(add) == ans.end()) ans.insert(add);
}
cout << ans.size();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int n,m,sum=0;
char str[15][15];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>str[i]+1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(str[i][j]!='#')continue;
int u=0;
if(str[i][j-1]=='#'||(str[i][j-1]!='#'&&str[i-1][j-1]!='#'&&str[i-1][j]=='#'))u+=1;
if(str[i][j+1]=='#'||(str[i][j+1]!='#'&&str[i+1][j+1]!='#'&&str[i+1][j]=='#'))u+=2;
if(str[i-1][j]=='#'||(str[i-1][j]!='#'&&str[i-1][j-1]!='#'&&str[i][j-1]=='#'))u+=4;
if(str[i+1][j]=='#'||(str[i+1][j]!='#'&&str[i+1][j+1]!='#'&&str[i][j+1]=='#'))u+=8;
sum+=4-__builtin_popcount(u);
//cout<<i<<' '<<j<<' '<<u<<' '<<sum<<endl;
}
}
cout<<sum;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin>>N;
int A[N];
for(int i = 0; i < N; i++){
cin>>A[i];
}
int count = 0;
for(int i = 0; i < N; i++){
if(A[i] <= 10){
}else if(A[i] > 10){
count += A[i] - 10;
}else{
}
}
cout<<count<<endl;
return 0;
} | #include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
struct UF{
vector<int> p;
UF(int n):p(n, -1){}
int leader(int x){
if(p[x] < 0)return x;
return p[x] = leader(p[x]);
}
void merge(int a, int b){
a = leader(a);
b = leader(b);
if(a == b)return;
p[a] += p[b];
p[b] = a;
}
};
set<int> leaders;
int main(){
int n, f;
cin >> n;
UF uf(n+1);
for(int i = 1;i <= n;i++){
cin >> f;
uf.merge(i, f);
}
for(int i = 1;i <= n;i++){
leaders.insert(uf.leader(i));
}
long long ans = 1;
for(int i = 0;i < leaders.size();i++){
ans = (ans + ans) % 998244353;
}
ans--;
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define ld long double
#define mod 1000000007
#define mod2 998244353
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define vi vector<int>
#define si set<int>
#define pii pair<int,int>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define vii vector<pii>
#define sii set<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vsi vector<si>
#define in(arr,n) for(int i=0;i<n;i++)cin>>arr[i];
#define out(arr,n) for(int i=0;i<n;i++)cout<<arr[i]<<" ";cout<<endl;
#define inn(arr,m,n) for(int i=m;i<n;i++)cin>>arr[i];
#define outt(arr,m,n) for(int i=m;i<n;i++)cout<<arr[i]<<" ";cout<<endl;
#define narr int arr[n];in(arr,n)
#define nbrr int brr[n];in(brr,n)
#define intt(n,m) int n,m;cin>>n>>m;
using namespace std;
void solve1();
int power(int x, int y, int p){
//if(x==0&&y==0)return 1;
int res = 1;x = x % p;if (x == 0) return 0;
while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}
return res;
}
int power(int x, int y){
//if(x==0&&y==0)return 1;
int res = 1;if (x == 0) return 0;
while (y > 0){if (y & 1)res = (res*x);y = y>>1;x = (x*x);}
return res;
}
int fib(int s1) {
int s2, s4, s5, s3;
s2 = s3 = 1;
s4 = s5 = 0;
while (s1 > 0) {
if (s1 % 2 == 0) {
int s6 = s3*s3;
s3 = 2*s5*s3 + s6;
s5 = s5*s5 + s6;
s1 /= 2;
} else {
int s6 = s2*s3;
s2 = s4*s3 + s6 + s2*s5;
s4 = s4*s5 + s6;
s1 -= 1;
}
}
return s4;
}
int sum(int arr[],int a,int n)
{
int zz=0;
for(int i=a;i<n;i++)
{
zz+=arr[i];
}
return zz;
}
void solve(){
int t;cin>>t;
while(t--){solve1();}
}
signed main()
{
//std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
solve1();
return 0;
}
void solve1()
{
int n,m,a,b;cin>>n>>m;
if(n==1&&m==0)
{
cout<<1<< " "<<3<<endl;return;
}
if(((m<0)||(m>=n-1)))
{
cout<<-1<<endl;return;
}
int arr[n][2];
arr[0][0]=2;arr[0][1]=4;
for(int i=1;i<n;i++)
{
arr[i][0]=arr[i-1][0]+4;
arr[i][1]=arr[i-1][1]+4;
}
if(m>0)
{
arr[0][1]+=(m)*4+3;
}
else if(m<0)
{
arr[n-1][0]-=3+(abs(m))*4;
}
for(int i=0;i<n;i++)
{
cout<<arr[i][0]<<" "<<arr[i][1]<<endl;
}
}
| #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define pqueue priority_queue
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);
int main() {
ll N, M;
cin>>N>>M;
vector<pair<ll, ll> > ans;
if(M>=0) {
if(N==1 && M==0) {
cout<<1<<" "<<2<<endl;
return 0;
}
if(M>N-2) {
cout<<-1<<endl;
return 0;
}
ll now1 = 1, now2 = (ll)1e9;
for(ll i=0;i<=N-(M+2);++i) {
ans.push_back(mp(now1, now2));
now1++;
now2--;
}
for(ll i=0;i<M+1;++i) {
ans.push_back(mp(now1, now1+1));
now1 += 2;
}
}
else {
cout<<-1<<endl;
return 0;
}
for(ll i=0;i<N;++i) cout<<ans[i].fi<<" "<<ans[i].se<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
#define all(v) begin(v), end(v)
#define sz(v) (int)(v).size()
#define fi first
#define se second
const int N = 1e5 + 5;
int n, dp[N];
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
cin >> n;
for(int i = n; i >= 1; --i) {
dp[i] = 1;
for(int j = i+i; j <= n; j += i) {
dp[i] = max(dp[i], dp[j]+1);
}
}
for(int i = 1; i <= n; ++i)
cout << dp[i] << ' ';
cout << endl;
return 0;
}
/*
* use std::array instead of std::vector, if u can
* overflow?
* array bounds
*/ | #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=1e9+7 ;
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin>>n;
vector<vector<ll>> c(n,vector<ll>(n));
rep(i,n){
rep(j,n)cin>>c[i][j];
}
vector<ll> a(n), b(n);
rep(i,n)a[i]=c[i][0];
rep(i,n)b[i]=c[0][i];
ll ami = inf,bmi=inf;
rep(i,n)ami=min(a[i],ami);
rep(i,n)bmi=min(b[i],bmi);
if(ami+bmi<c[0][0]){
cout<<"No"<<endl;
return 0;
}
rep(i,n)a[i]-=ami;
rep(i,n)b[i]-=(c[0][0]-ami);
rep(i,n)rep(j,n){
if(a[i]+b[j]!=c[i][j]){
cout<<"No"<<endl;
return 0;
}
}
cout<<"Yes"<<endl;
rep(i,n)cout<<a[i]<<" \n"[i+1==n];
rep(i,n)cout<<b[i]<<" \n"[i+1==n];
return 0;
} |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char s[30];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int cnt0=0,cnt1=0;
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
int cnt=0;
for(int j=1;j<=m;j++)cnt+=(s[j]==49);
if(cnt&1) cnt1++;
else cnt0++;
}
printf("%lld",(long long)cnt0*cnt1);
return 0;
} | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 1e9;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep2(i, x, n) for(int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
void Main() {
int n, m;
cin >> n >> m;
vector<string> s(n);
rep(i, n) cin >> s[i];
vector<int> cnt(n);
rep(i, n) rep(j, m) if(s[i][j] == '1')++ cnt[i];
ll sum = 0;
vector<int> r(n);
ll sum2 = 0;
vector<int> r2(n);
rep(i, n) {
if(cnt[i] % 2 == 1) {
++sum;
} else {
++sum2;
}
r[i] = sum;
r2[i] = sum2;
// cout << sum << endl;
// cout << sum2 << endl;
}
ll ans = 0;
rep(i, n) {
if(cnt[i] % 2 == 1) {
// cout << sum2 << " " << r2[i] << endl;
ans += sum2 - r2[i];
} else {
// cout << sum << " " << r[i] << endl;
ans += sum - r[i];
}
}
cout << ans << endl;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int, int> P;
#define yn \
{ puts("Yes"); } \
else { \
puts("No"); \
}
#define MAX_N 200005
int main() {
int n;
cin >> n;
ll ans = 0;
ll sum = 0;
while (sum < n) {
ans++;
sum += ans;
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
#define int long long
#define fer( a , b , c ) for( register int a = b ; a <= c ; a ++ )
#define der( a , b , c ) for( register int a = b ; a >= c ; a -- )
using namespace std ;
inline int read(){
int sum = 0 , f = 1 ;
char ch = getchar() ;
while( ch < '0' || ch > '9' ){
if( ch == '-' ){
f = -1 ;
}
ch = getchar() ;
}
while( ch >= '0' && ch <= '9' ){
sum = ( sum << 1 ) + ( sum << 3 ) + ( ch ^ 48 ) ;
ch = getchar() ;
}
return sum * f ;
}
int s , p ;
signed main(){
s = read() , p = read() ;
fer( i , 1 , 10000005 ) if( p % i == 0 ) if( i + ( p / i ) == s ){ puts( "Yes" ) ; return 0 ; }
puts( "No" ) ;
return 0 ;
} |
#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926536
#define ll long long int
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main(){
fastio
ll n;
cin>>n;
ll res=(1LL<<60);
ll a,b,c;
for(int i=0;i<60;i++){
//cout<<a<<b<<c<<endl;
b=i;
a=n/(1LL<<i);
c=n-(a*(1LL<<i));
res=min(res,a+b+c);
}
cout<<res<<endl;
return 0;
} | #pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include <functional>
template <typename T>
inline void hash_combine(std::size_t &seed, const T &val) {
seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template <typename T> inline void hash_val(std::size_t &seed, const T &val) {
hash_combine(seed, val);
}
template <typename T, typename... Types>
inline void hash_val(std::size_t &seed, const T &val, const Types &... args) {
hash_combine(seed, val);
hash_val(seed, args...);
}
template <typename... Types>
inline std::size_t hash_val(const Types &... args) {
std::size_t seed = 0;
hash_val(seed, args...);
return seed;
}
struct pair_hash {
template <class T1, class T2>
std::size_t operator()(const std::pair<T1, T2> &p) const {
return hash_val(p.first, p.second);
}
};
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define G 3
#define MAX_N 300005
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
typedef complex<double> E;
string a,b="ZONe";
int main(){
cin>>a;
int ans=0;
for(int i=0;i<9;i++){
bool f=1;
for(int j=i;j<i+4;j++)if(a[j]!=b[j-i])f=0;
if(f)ans++;
}
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// template {{{
#define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
constexpr i32 mod = 1e9 + 7;
// constexpr i32 mod = 998244353;
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;
constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};
struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;
template <typename T = i64> T input() { T x; cin >> x; return x; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
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); }
// }}}
void solve() {
int n = input();
vector< int > as(n), bs(n);
cin >> as >> bs;
whole(reverse, as);
range(i, 0, n) if (as[i] > bs[i]) swap(as[i], bs[i]);
i64 ans = 0;
using P = pair<i64, i64>;
priority_queue<P, vector< P >, greater< P >> pq;
range(i, 0, n) {
if (not pq.empty() and pq.top().first < as[i]) {
ans += bs[i] + as[i] - pq.top().first;
pq.emplace(pq.top().second, infll);
pq.emplace(as[i], bs[i]);
pq.pop();
} else {
ans += bs[i];
pq.emplace(bs[i], infll);
}
}
cout << ans << endl;
}
signed main() {
solve();
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,string> pi;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
vector<pi>arr;
for(int i=0; i<n; i++){
string s;
int h;
cin>>s>>h;
arr.push_back(pi(h,s));
}
sort(arr.begin(),arr.end());
arr.pop_back();
cout<<arr.back().second;
} |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
#define lin(x) ll x; cin>>x;
#define stin(x) string x; cin>>x;
#define yn(x) if(x) { cout<<"Yes"<<endl; } else { cout<<"No"<<endl; }
#define YN(x) if(x) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; }
#define co(x) cout<<x<<endl;
using ll=long long;
using ld=long double;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll mod=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
return ((a > b) ? (a = b, true) : (false));
}
ll powmod(ll n, ll k, ll m) {
ll r=1;
for(; k>0; k >>=1) {
if(k&1) r=(r*n)%m;
n=(n*n)%m;
}
return r;
}
ll fact(ll n) {
ll a=1;
rep(i,n) {
a=a*(n-i);
}
return a;
}
ll pow(ll a, ll b) {
ll x=1;
rep(i,b) {
x=x*a;
}
return x;
}
int main() {
lin(N); lin(M); lin(X); lin(Y);
vl A(M);
vl B(M);
vl T(M);
vl K(M);
rep(i,M) {
ll a,b,t,k;
cin>>a>>b>>t>>k;
A.at(i)=a-1;
B.at(i)=b-1;
T.at(i)=t;
K.at(i)=k;
}
X=X-1;
Y=Y-1;
vvpl G(N);
rep(i,M) {
G.at(A.at(i)).pb({B.at(i),i});
G.at(B.at(i)).pb({A.at(i),i});
}
vl dist(N,INF);
dist.at(X)=0;
priority_queue<pl, vpl, greater<pl>>que;
que.push({0,X});
while(!que.empty()) {//que
auto[d,v]=que.top();
que.pop();
if(dist[v]<d) continue;
for(auto e:G[v]) {
ld aa=(ld)d/(ld)K[e.se];
ll aaa=ceil(aa);
if( chmin(dist[e.fi], ((d + K[e.second] - 1) / K[e.second]) * K[e.second] + T[e.second]) ){
que.push({dist[e.fi],e.fi});
}
}
}//endque
if(dist.at(Y)==INF) {
co(-1);
}
else {
co(dist.at(Y));
}
}//end
| #include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<set>
#include<queue>
#include<unordered_map>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll inf=1e18,N=1e5+500;
ll gti(void)
{
char c=getchar();
ll ret=0,st=1;
for (;c!='-'&&!isdigit(c);c=getchar());
if (c=='-') st=-1,c=getchar();
for (;isdigit(c);c=getchar()) ret=ret*10+c-'0';
return ret*st;
}
struct edge{edge *nxt;int to,t,k;}pool[N<<1],*pt[N],*p=pool;
void addedge(int a,int b,int t,int k)
{
*(++p)=(edge){pt[a],b,t,k},pt[a]=p;
*(++p)=(edge){pt[b],a,t,k},pt[b]=p;
}
ll dis[N];
struct P
{
ll dis;
int id;
bool operator<(const P &b)const
{
return dis>b.dis;
}
};
ll dijkstra(int n,int s,int t)
{
priority_queue<P> hp;
fill(dis,dis+n+1,inf);
dis[s]=0,hp.push((P){dis[s],s});
while (hp.size())
{
P now=hp.top();
hp.pop();
if (dis[now.id]!=now.dis)
continue;
for (edge *i=pt[now.id];i;i=i->nxt)
{
ll tm=now.dis+(i->k-now.dis%i->k)%i->k+i->t;
if (dis[i->to]>tm)
dis[i->to]=tm,hp.push((P){dis[i->to],i->to});
}
}
if (dis[t]==inf) return -1;
return dis[t];
}
int main(void)
{
int n=gti(),m=gti(),x=gti(),y=gti();
for (int i=1;i<=m;i++)
{
int u=gti(),v=gti(),t=gti(),k=gti();
addedge(u,v,t,k);
}
printf("%lld\n",dijkstra(n,x,y));
return 0;
}
|
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> //Policy Based Data Structure
// using namespace __gnu_pbds; //Policy Based Data Structure
using namespace std;
// typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; //Policy Based Data Structure
// #define gc getchar_unlocked
// #define pqb priority_queue<int>
// #define pqs priority_queue<int, vi, greater<int> >
// #define mk(arr,n,type) type *arr = new type[n]
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long
#define endl '\n'
#define w(t) int t; cin>>t; while(t--)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x,y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it,a) for(auto it = a.begin(); it != a.end(); it++)
#define ps(x,y) fixed<<setprecision(y)<<x
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define PI 3.1415926535897932384626
#define inf 1e18
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //Random Shuffler
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef map<int, int> mii;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
// const int N = 3e5, M = N;
// vi g[N];
//=======================
void sol()
{
int a, b; cin >> a >> b;
cout << (a + b) / 2 << " " << (a - b) / 2;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
sol();
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % mod;
base = (base * base) % mod;
exp >>= 1;
}
return result;
}
// void ipgraph(int n, int m){
// int i, u, v;
// while(m--){
// cin>>u>>v;
// g[u-1].pb(v-1);
// g[v-1].pb(u-1);
// }
// }
//
// void dfs(int u, int par){
// for(int v:g[u]){
// if (v == par) continue;
// dfs(v, u);
// }
// } | #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
const ll mod = 1000000007;
const int INF = 1001001001;
char winner(char a, char b){
if(a == b) return a;
if (a > b) swap(a, b);
if(a == 'P' && b == 'R'){
return a;
}else if(a == 'P' && b == 'S'){
return b;
}else{
return a;
}
}
int main(){
int n, k;
cin >> n >> k;
string s;
cin >> s;
rep(i, k){
string s_ = s + s;
string ns;
for (int i = 0; i < 2*n; i+=2){
ns += winner(s_[i], s_[i+1]);
}
s = ns;
}
cout << s[0] << endl;
} |
#include "bits/stdc++.h"
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define endl '\n'
#define all(V) (V).begin(), (V).end()
using namespace std;
typedef long long ll;
int main() {fio;
int n;
cin >> n;
vector <pair <int, int>> a(2 * n);
vector <pair <int, int>> b(2 * n);
string s;
for(int i = 0; i < 2 * n; i++) {
cin >> a[i].first;
a[i].second = i;
b[i] = a[i];
}
sort(all(b));
vector <bool> oh(2 * n);
for(int i = 0; i < 2 * n; i++) {
if(a[i] < b[n]) {
oh[i] = 0;
} else {
oh[i] = 1;
}
}
int ls = 0;
for(int i = 0; i < 2 * n; i++) {
if(oh[i]) {
if(ls >= 0) {
s += '(';
} else {
s += ')';
}
ls++;
} else {
if(ls <= 0) {
s += '(';
} else {
s += ')';
}
ls--;
}
}
assert(ls == 0);
cout << s << endl;
return 0;
} | #include<bits/stdc++.h>
#include<bits/extc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t){
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr<<#x<<": "; for(auto i:x) cerr<<i<<" "; cerr<<endl;}}
#define debugr(l,r,x) {{cerr<<#x<<": "; for(int i=l;i<=r;i++) cerr<<x<<" "; cerr<<endl;}}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debugr(l,r,x)
#define cerr while(0) cerr
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define int long long
typedef __int128 INT;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int a[500005];
bool good[500005];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string ans="";
int n;
cin>>n;
n*=2;
for(int i=0; i<n; i++) ans.push_back('#');
for(int i=0; i<n; i++){
cin>>a[i];
}
vi idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&](int x, int y){
return a[x] < a[y];
});
for(int i=0; i<n/2; i++){
good[idx[i]]=1;
}
stack<int> st;
for(int i=0; i<n; i++){
if(st.empty()){
ans[i]='(';
st.push(i);
}
else{
int t=st.top();
if(good[t]==good[i]){
st.push(i);
}
else{
st.pop();
ans[t]='(';
ans[i]=')';
}
}
}
cout<<ans<<endl;
} |
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
template<typename T>
void __p(T a) {
cout << a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout << "{";
__p(a.first);
cout << ",";
__p(a.second);
cout << "}";
}
template<typename T>
void __p(std::vector<T> a) {
cout << "{";
for (auto it = a.begin(); it < a.end(); it++)
__p(*it), cout << ",}"[it + 1 == a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout << name << " : ";
__p(arg1);
cout << endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket = 0, i = 0;
for (;; i++)
if (names[i] == ',' && bracket == 0)
break;
else if (names[i] == '(')
bracket++;
else if (names[i] == ')')
bracket--;
const char *comma = names + i;
cout.write(names, comma - names) << " : ";
__p(arg1);
cout << " | ";
__f(comma + 1, args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__);
#else
#define trace(...)
#endif
typedef long long int ll;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
typedef pair<ll , ll> pp;
typedef pair<pair<ll, ll>, ll> ppp;
#define X first
#define Y second
#define getones(n) __builtin_popcountll(n)
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define frv(i,a,b) for (ll i = (a), _b = (b); i < _b; i++)
#define frrv(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--)
#define fr(i,n) for (ll i = 0, _n = (n); i < _n; i++)
#define frr(i,n) for (ll i = n - 1; i >= 0; i--)
#define fordata(it,ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fill(ar,val) fr(i,sizeof(ar)/sizeof(ll)) ar[i]=val
#define fill2(ar,val) fr(i,sizeof(ar)/sizeof(ar[0])) fr(j,sizeof(ar[0])/sizeof(ll)) ar[i][j]=val
#define fill0(ar) memset(ar,0,sizeof(ar))
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define all(ar) ar.begin(), ar.end()
#define INF 1000000000000000000
#define PI 3.14159265358979323846L
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
const ll N = 1e6 + 1;
const ll K = 998244353;
#define vv vector<ll>
const ll globalmod = 1e9 + 9;
vv spf(N);
void sieve() {
frv(i, 2, N) {
spf[i] = i;
}
for (int i = 2; i * i <= N; i++) {
for (int j = i; j <= N; j += i) {
if (spf[j] == j) {
spf[j] = i;
}
}
}
}
ll binaryexpo(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1) {
res = (res * a) % m;
}
a = (a * a) % m;
b >>= 1;
}
return res;
}
vv fact(N);
ll ncr(ll a, ll b) {
ll temp = fact[a];
ll init = ((fact[b] * fact[a - b]) % K);
temp *= binaryexpo(init, K - 2, K);
temp %= K;
return temp;
}
int main() {
fact[0] = 1;
frv(i, 1, N) {
fact[i] = fact[i - 1] * i;
fact[i] %= K;
}
sieve();
ll t = 1;
// cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
ll sum = 0;
frv(i, 1, m + 1) {
ll temp = i;
ll pr = 1;
while (spf[temp] != 0) {
ll ind = spf[temp];
ll cnt = 0;
while (spf[temp] == ind) {
temp /= ind;
cnt++;
}
pr *= (ncr(cnt + n - 1, n - 1));
pr %= K;
}
// trace(pr)
sum += pr;
sum %= K;
}
cout << sum;
}
} | #include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i=0; i<(n); i++)
#define readVec(v) forn(i, v.size()){cin >> v[i];}
#define printArr(arr, n) forn(i, n){if (i) cout << " "; cout << arr[i];} cout << endl;
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define f first
#define s second
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vii;
//Printing pairs and vectors
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
//2 147 483 647 int max
//9 223 372 036 854 775 807 ll max
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void file_io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();
}
int main(){
fast_io();
ll b, c;
cin >> b >> c;
// ll left = b, right = b;
vpl v;
v.pb(mp(-b-(c-1)/2, -b));
v.pb(mp(-b, -b+(c-1)/2));
v.pb(mp(b-c/2, b));
if(c > 1) {v.pb(mp(b, b+(c-2)/2));}
else {v.pb(mp(b, b));}
sort(v.begin(), v.end());
ll tot = 0;
forn(i, 4){
tot += v[i].s-v[i].f + 1;
}
forn(i, 4){
for(int j=i+1; j<4; j++){
tot -= max(0LL, min(v[i].s, v[j].s)-max(v[i].f, v[j].f)+1);
}
}
forn(i, 4){
tot += max(0LL, min(v[i].s, min(v[(i+1)%4].s, v[(i+2)%4].s))-max(v[i].f, max(v[(i+1)%4].f, v[(i+2)%4].f))+1);
}
tot -= max(0LL, min(v[0].s, min(v[1].s, min(v[3].s, v[2].s)))-max(v[0].f, max(v[1].f, max(v[3].f, v[2].f)))+1);
cout << tot << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
if((a+b==2*c)||(a+c==2*b)||(b+c==2*a))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for(ll i = 0; i < n; ++i)
#define rep2(i, a, b) for(ll i = a; i <= b; ++i)
#define rep3(i, a, b) for(ll i = a; i >= b; --i)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define drop(s) cout << #s << endl, exit(0)
#define endl '\n'
using Graph = vector<vector<int>>;
const int inf = 100100101;
const ll ll_inf = 1e18;
const int mod = 1000000007;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vc<T>>;
template <class T> using vvvc = vector<vvc<T>>;
template <class T> using vvvvc = vector<vvvc<T>>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
int main() {
vector<int> a(3);
rep(i,3) cin >> a[i];
sort(all(a));
if(a[2]-a[1] == a[1]-a[0]) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) v.begin(), v.end()
template<class T>bool chmax(T& a, const T& b) { if (a<b) { a=b; return 1;} return 0;}
template<class T>bool chmin(T& a, const T& b) { if (b<a) { a=b; return 1;} return 0;}
ll mul(ll x, ll a) {
ll ret = 1;
rep(i, a) ret *= x;
return ret;
}
int main() {
string X;
ll M;
cin >> X >> M;
if (X.size() == 1) {
ll x = stoll(X);
if (x <= M) cout << 1 << endl;
else cout << 0 << endl;
return 0;
}
ll len = X.size();
ll ma = 0;
for (int i = 0; i < X.size(); ++i) {
chmax(ma, (ll)(X[i] - '0'));
}
for (ll i = ma+1; i < 2e18; ++i) {
if (i > 100) i += 1e12;
ll tmp = 0;
double judge = 0;
for (int j = 0; j < len; ++j) {
tmp += mul(i, (len - j - 1)) * (X[j] - '0');
judge += pow(i, (len - j - 1)) * (X[j] - '0');
}
if (judge > 2e18 || tmp > M) {
for (ll k = max(ma+1, i - (ll)1e12-1); k < 2e18; k++) {
if (k > 100) k += 1e6;
ll tm = 0;
double ju = 0;
for (int l = 0; l < len; ++l) {
tm += mul(k, (len - l - 1)) * (X[l] - '0');
ju += pow(k, (len - l - 1)) * (X[l] - '0');
}
if (ju > 2e18 || tm > M) {
for (ll m = max(ma+1, k - (ll)1e6-1); m < 2e18; ++m) {
ll t = 0;
double j = 0;
for (int n = 0; n < len; ++n) {
t += mul(m, (len - n - 1)) * (X[n] - '0');
j += pow(m, (len - n - 1)) * (X[n] - '0');
}
if (j > 2e18 || t > M) {
cout << m - (ma+1) << endl;
return 0;
}
}
}
}
}
}
} | #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
int main() {
long k;
string s, t;
long double co[10] = {}, co1[10] = {}, co2[10] = {};
long double ans = 0, ans1 = 0;
cin >> k >> s >> t;
long double kd = k;
for (int i = 0; i < 4; i++) {
co[int(s[i] - '0')]++;
co1[int(s[i] - '0')]++;
co[int(t[i] - '0')]++;
co2[int(t[i] - '0')]++;
}
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
if (i == j && co[i] < k - 1) {
co1[i]++, co2[j]++;
long s1 = 0, t1 = 0;
for (int k1 = 1; k1 < 10; k1++) {
long s2 = 1, t2 = 1;
for (int l = 0; l < co1[k1]; l++) {
s2 *= 10;
}
for (int l = 0; l < co2[k1]; l++) {
t2 *= 10;
}
s1 += k1 * s2, t1 += k1 * t2;
}
if (s1 > t1) {
ans += (kd - co[i]) * (kd - co[i] - 1);
}
co1[i]--, co2[j]--;
}
else if (i != j && co[i] < k && co[j] < k) {
co1[i]++, co2[j]++;
long s1 = 0, t1 = 0;
for (int k1 = 1; k1 < 10; k1++) {
long s2 = 1, t2 = 1;
for (int l = 0; l < co1[k1]; l++) {
s2 *= 10;
}
for (int l = 0; l < co2[k1]; l++) {
t2 *= 10;
}
s1 += k1 * s2, t1 += k1 * t2;
}
if (s1 > t1) {
ans += (kd - co[i]) * (kd - co[j]);
}
co1[i]--, co2[j]--;
}
}
}
ans1 = (9 * kd - 8) * (9 * kd - 9);
cout << fixed << setprecision(16) << ans / ans1 << endl;
} |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
int main() {
ll N;
cin>>N;
vector<pair<int,string>> A(N);
rep(i,N){
cin>>A[i].second>>A[i].first;
}
sort(all(A));
cout<<A[N-2].second<<endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<long long> Vll;
ll mm=1000000000;ll MM=mm+7;
#define rep(i, n) for(int i=0;i<n;i++)
#define PI 3.141592653589793
int main(){
ll n;
cin >> n;
vector<ll> a(n),b(n);
ll suma=0;;
rep(i,n){
cin >> a[i] >> b[i];
suma+=a[i];
}
priority_queue<ll> dif;
rep(i,n){
dif.push(2*a[i]+b[i]);
}
ll ans=0;
rep(i,n){
suma-=dif.top();
dif.pop();
ans++;
if(suma<0)break;
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
#define ll long long int
#define mod 1000000007
using namespace std;
void solve() {
ll arr[4];
ll sum = 0;
// ll one;
for(int i=0; i<4; i++){
cin>>arr[i];
sum+=arr[i];
}
//Two
sort(arr, arr+4);
ll one = arr[3];
sum-=arr[3];
if(one < sum){
if(one + arr[0] == sum-arr[0]){
cout<<"Yes"<<endl;
return;
}
else if(one + arr[1] == sum - arr[1]){
cout<<"Yes"<<endl;
}
else if(one + arr[2] == sum-arr[2]){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
else if(one == sum){
cout<<"Yes"<<endl;
return;
}
else{
cout<<"No"<<endl;
return;
}
return;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
// cin>>t;
t = 1;
while(t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
#define ALL(obj) (obj).begin(), (obj).end()
#define debug(x) cout << #x << ": " << x << '\n';
#define degreeToRadian(deg) (((deg)/360)*2*M_PI)
#define radianTodegree(rad) (((rad)/2/M_PI)*360)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
using P = pair<int,int>;
using ll = long long;
const ll INF = 1LL<<60;
const int MOD = 1e9 + 7;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//cin.tie(0);ios::sync_with_stdio(false);
//cout << fixed << setprecision(15) << y << endl;
int main() {
int A, B, C, D;
cin >> A >> B >> C >> D;
vector<int> T;
T.push_back(A);
T.push_back(B);
T.push_back(C);
T.push_back(D);
bool ok = false;
for (int bit = 1; bit < (1 << 4); bit++) {
ll sum = A + B + C + D;
ll x = 0;
for (int i = 0; i < 4; i++) {
if (bit & 1 << i) {
sum -= T[i];
x += T[i];
}
}
if (sum == x) ok = true;
}
if (ok) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
using ll = long long;
using namespace std;
int main() {
string s,t;
cin >> s >> t;
if (s[0]=='Y') {
t[0]^=32;
cout << t << endl;
}
else {
cout << t << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
char s, t;
cin >> s >> t;
if (s == 'Y'){
if (t == 'a'){
cout << 'A' << endl;
}else if (t == 'b'){
cout << 'B' << endl;
}else if (t == 'c'){
cout << 'C' << endl;
}
} else if (s == 'N'){
cout << t << endl;
}
}
|
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
using ll = long long;
#define SWAP(a,b) ((a != b)? (a += b,b = a - b,a -= b) : 0 ) //数値のみ
#define SWAP(type,a,b) { type temp = a; a = b; b = temp; } //ポインタ, 構造体, 文字列
#define rng(i, a, b) for(int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define pb push_back
#define eb emplace_back
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
ll jo(ll a, ll n){
ll tmp = 1;
rep(i, n) {
tmp *= a;
if(tmp <= 0) return -1;
}
return tmp;
}
void yn(bool Yes){
cout << ((Yes)? "Yes" : "No") << endl;
}
void ny(bool No){
cout << ((No)? "No" : "Yes") << endl;
}
void YN(bool YES){
cout << ((YES)? "YES" : "NO") << endl;
}
void NY(bool NO){
cout << ((NO)? "NO" : "YES") << endl;
}
void Yay(bool y){
cout << ((y)? "Yay!" : ":(") << endl;
}
int main() {
int n, a, b, ans = 0;
char c;
string s;
cin >> s;
rep(i, s.size()){
if(s[i] == '.') break;
cout << s[i];
}
cout << endl;
//printf("%.10lf\n", ans);
} | #include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n;
cin >> n;
long long res=0;
long long st,fi;
long long c=0;
for(st=1;;st*=10){
fi=10*st-1;
fi=min(n,fi);
res+=(c/3)*(fi-st+1);
c++;
if(fi==n){break;}
}
cout << res << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#include <map>
typedef long long ll;
#define REP(i, n) for(ll i = 0; i < n; i++)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOR(i, m, n) for(ll i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using Graph = vector<vector<int>>;
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll modinv(ll a, ll m){
ll b = m, u = 1, v = 0;
while(b) {
ll t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if(u < 0) u += m;
//cout << u << endl;
return u;
}
ll paty_counter(ll level, ll x){
ll size = pow(2, level+2) - 3;
ll pmi = pow(2, level) - 1;
if(x == 0){
return 0;
}else if(level == 0){
return 1;
}else{
if(x <= (size - 1)/2){
return paty_counter(level-1, x-1);
}else{
return pmi + 1 + paty_counter(level-1, x - pow(2, level+1) + 3 - 2);
}
}
}
using namespace std;
using Graph = vector<vector<int>>;
// 深さ優先探索
vector<bool> seen;
static ll x;
void dfs(const Graph &G, int v) {
seen[v] = true;
for (auto next_v : G[v]) {
if (seen[next_v]) continue;
dfs(G, next_v); // 再帰的に探索
x ++;
}
}
ll fr(ll n){
ll ans = 1;
REP(i, n){
ans *= i+1;
ans %= MOD;
}
return ans;
}
ll conb(ll n, ll k){
return fr(n)% MOD * modinv(fr(k) * fr(n-k) % MOD, MOD) % MOD;
}
//bfs
int main() {
ll h, w;
cin >> h >> w;
vector<vector<char>> g(h, vector<char>(w));
REP(i, h){
REP(j, w){
char tmp;
cin >> tmp;
//cout << tmp << endl;
g[i][j] = tmp;
//cout << g[i][j] << endl;
}
}
ll ans = 0;
REP(i, h){
REP(j, w){
if(i+1 < h){
if(g[i][j] == '.' && g[i+1][j] == '.') ans += 1;
}
if(j + 1 < w){
if(g[i][j] == '.' && g[i][j+1] == '.') ans += 1;
}
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define F first
#define S second
#define pb push_back
#define sz(x) (int)x.size()
#define len(x) (int)x.length()
#define pii pair<int,int>
#define ppi pair<pii,int>
#define vi vector<int>
#define mp make_pair
#define minheap priority_queue<int,vector<int>,greater<int>>
int32_t main(){
FAST
int h,w;
cin>>h>>w;
char a[h][w];
int ans=0;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
cin>>a[i][j];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(a[i][j]=='.' && (i<h-1 && a[i+1][j]=='.'))
ans++;
if(a[i][j]=='.' && (j<w-1 && a[i][j+1]=='.'))
ans++;
}
}
cout << ans;
} |
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
constexpr int DEBUG = 0;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, x;
cin >> n >> x;
x *= 100;
int c = 0;
for (int i = 0; i < n; i++) {
int v, p;
cin >> v >> p;
c += v * p;
if (c > x) {
cout << i + 1 << endl;
return 0;
}
}
cout << -1 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define endl ("\n")
#define pi (3.141592653589)
#define mod 1e9+7
#define int long long
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int32_t main(){
fast
int t=1;
// cin>>t;
while(t--){
int n;cin>>n;
float X;cin>>X;
int a[n], b[n];
rep(i, n)cin>>a[i]>>b[i];
int f=0;
float x=0;
rep(i, n){
x+=(b[i]*a[i]);
if(x>X*100){
cout<<i+1<<endl;
f=1;
break;
}
}
if(f==0)cout<<-1<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define IOS ios_base::sync_with_stdio(0);cin.tie(0)
/* start */
const int N = 52;
const int mod = 998244353;
int g[N][N], tot;
int was[N];
int fat[N];
vector<int> vx[N], vy[N];
void dfs1(int v) {
tot++;
was[v]=1;
for(int u : vx[v]) {
if(!was[u]) {
dfs1(u);
}
}
}
void dfs2(int v) {
tot++;
was[v]=1;
for(int u : vy[v]) {
if(!was[u]) {
dfs2(u);
}
}
}
signed main(){
IOS;
fat[0] = fat[1] = 1;
for(int i = 2; i < N; i++) {
fat[i] = (fat[i-1] * i) % mod;
}
int n, K;
cin >> n >> K;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> g[i][j];
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
bool ok = true;
for(int k = 0; k < n; k++) {
if(g[k][i] + g[k][j] > K) {
ok = false;
break;
}
}
if(ok) {
vx[i].push_back(j);
vx[j].push_back(i);
}
}
}
int ans = 1;
for(int i = 0; i < n; i++) {
if(!was[i]) {
tot = 0;
dfs1(i);
ans = (ans * fat[tot])%mod;
}
}
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
bool ok = true;
for(int k = 0; k < n; k++) {
if(g[i][k] + g[j][k] > K) {
ok = false;
break;
}
}
if(ok) {
vy[i].push_back(j);
vy[j].push_back(i);
}
}
}
memset(was, 0, sizeof(was));
for(int i = 0; i < n; i++) {
if(!was[i]) {
tot = 0;
dfs2(i);
ans = (ans * fat[tot])%mod;
}
}
cout << ans << "\n";
}
/*
Coisas pra se lembrar:
* Overflow, tamanho do N
* Casos especiais (e.g. n = 1)
* Nao ficar parado e fazer alguma coisa
* Escrever coisas no papel
* NAO FICAR TRAVADO EM UMA IDEIA
*/ | #include <bits/stdc++.h>
#ifndef ONLINE_JUDGE
#define debug(x) cout << #x << ": " << (x) << endl
#else
#define debug(x)
#endif
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int maxn=1e6+7,inf=0x3f3f3f3f,mod=1e9+7;
int fa[maxn];
int find(int x)
{
return fa[x]==x?fa[x]:fa[x]=find(fa[x]);
}
string mp[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int h,w;
cin>>h>>w;
for(int i=0;i<h;++i) cin>>mp[i];
mp[0][0]='#',mp[0][w-1]='#',mp[h-1][0]='#',mp[h-1][w-1]='#';
for(int i=0;i<=h+w;++i) fa[i]=i;
for(int i=0;i<h;++i)
{
for(int j=0;j<w;++j)
{
if(mp[i][j]=='#')
{
int fx=find(i),fy=find(j+h);
if(fx!=fy) fa[fy]=fx;
}
}
}
set<int>row,col;
for(int i=0;i<h;++i) row.insert(find(i));
for(int i=0;i<w;++i) col.insert(find(h+i));
cout<<min(row.size(),col.size())-1<<'\n';
return 0;
}
|
/**
Lost Arrow (Aryan V S)
Saturday 2020-12-19
**/
#ifdef LOST_IN_SPACE
# if __cplusplus > 201703LL
# include "lost_pch1.h" // C++20
# elif __cplusplus > 201402LL
# include "lost_pch2.h" // C++17
# else
# include "lost_pch3.h" // C++14
# endif
#else
# include <bits/stdc++.h>
#endif
constexpr bool test_cases = false;
void solve () {
int n;
std::cin >> n;
int cnt = 0;
for (int i = 1; i <= n; ++i) {
std::string base10 = std::to_string(i);
std::string base8;
int j = i;
while (j) {
base8 += '0' + (j % 8);
j /= 8;
}
bool bad = false;
for (char c : base10)
if (c == '7')
bad = true;
for (char c : base8)
if (c == '7')
bad = true;
if (!bad)
++cnt;
}
std::cout << cnt << '\n';
}
int main () {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.precision(10);
std::cerr.precision(10);
std::cout << std::fixed << std::boolalpha;
std::cerr << std::fixed << std::boolalpha;
int32_t cases = 1;
if (test_cases)
std::cin >> cases;
while (cases--)
solve();
return 0;
}
// g++.exe -Wall -Weffc++ -Wextra -pedantic -std=c++20 -g -D_GLIBCXX_DEBUG -DLOST_IN_SPACE -H
// Replace failing with learning
// Replace overthinking with action
// Replace blame with responsibility
// Replace toxic friends with mentors
// Replace complaining with gratitude
// Replace netflix marathons with sleep
// Replace fake influencers with inspiring creators
| #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define f0(i,n) for(i=0;i<n;i++)
#define rf0(i,n) for(i=n-1;i>=0;i--)
#define f2(i,n) for(i=1;i<n;i++)
#define f1(i,n) for(i=1;i<=n;i++)
#define fab(i,a,b) for(i=a;i<=b;i++)
#define shr ll t;cin>>t; while(t--)
#define fi first
#define sc second
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpll vector<pll>
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define dev(a) for(auto it:a)cout<<it<<" "
#define prec(nm,prc) cout<<fixed<<setprecision(nm)<<prc<<" "
#define print(a) cout<<a<<"\n";
#define print2(a,b) cout<<a<<" "<<b<<"\n";
#pragma GCC optimization ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define nl cout<<"\n"
#define ye cout<<"Yes\n"
#define yee cout<<"YES\n"
#define no cout<<"No\n"
#define noo cout<<"NO\n"
#define ln length()
#define ms(cnt,r) memset(cnt, r, sizeof(cnt))
using namespace std;
bool comp(pll &a,pll &b)
{
return (a.sc<b.sc);
}
ll power(ll n,ll p)
{
if(p==0)
return 1;
else
{
ll ans=power(n,p/2)%mod;
ans=(ans*ans)%mod;
if(p%2==0)
return ans;
else
return ans=(ans*n)%mod;
}
}
bool isPrime(ll n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
/********************HAR HAR MAHADEV***********JAI BAJRANG BALI************************/
int main()
{
FAST;
/* ifstream cin;
cin.open("input.txt");
ofstream cout;
cout.open("output.txt");
*/
//shr
{
ll i,j,n,a,b,c,d,m,sum=0,ans=0,cnt=0,r=0,e=0;
string str,s1,om,s;
unordered_map<ll,ll>mp,mp1;
set<ll>st; vll v;
cin>>a;
n=sqrt(a);
fab(i,1,n)
{
if(a%i==0)
{
e=a/i;
if(i%2==1)r++;
if(e%2==1)r++;
if(i==e&&i%2==1)r--;
}
}
cout<<(r*2);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define all(x) x.begin(),x.end()
#define MOD 1000000007
#define PI acos(-1)
#define debug cerr <<
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define close << "\n";
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("../input.txt","r",stdin);
ll N,W;
cin >> N >> W;
vector<ll> arr((int)2e5+60,0);
ll max_T = 0;
for (ll i = 0; i < N; ++i) {
ll S,T,P;
cin >> S >> T >> P;
T--;
arr[S]+=P;
arr[T+1]-=P;
max_T = max(max_T,T);
}
for (ll i = 1; i < arr.size(); ++i) {
arr[i] += arr[i-1];
}
bool possible = true;
for (ll i = 0; i < arr.size(); ++i) {
//cout << arr[i] << " ";
if(arr[i] > W){
possible = false;
break;
}
}
cout << ((possible)?"Yes":"No") close
}
| #include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<limits>
#include<set>
#include<deque>
#include<queue>
#include<stack>
using namespace std;
#define int long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dup(x,y) (((x)+(y)-1)/(y))
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef pair<int, int>P;
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const int INF = 1e18;
const long double PI = (acos(-1));
signed main() {
int N, W;
cin >> N >> W;
vector<int>S(N), T(N), P(N);
rep(i, N)cin >> S[i] >> T[i] >> P[i];
int tmx = -INF;
rep(i, N)chmax(tmx, T[i]);
vector<int>v(tmx + 1, 0);
rep(i, N) {
v[S[i]] += P[i];
v[T[i]] -= P[i];
}
rep(i, tmx) {
v[i + 1] += v[i];
}
rep(i, tmx + 1) {
if (v[i] > W) {
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
} |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <unordered_map>
#include <map>
#include <algorithm>
#define int long long
inline int read()
{
int num=0,f=1;char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>47&&c<58)num=(num<<3)+(num<<1)+(c^48),c=getchar();
return num*f;
}
const int mod=998244353;
int dp[200005][25];
inline int min(int a,int b){return a<b?a:b;}
inline int max(int a,int b){return a>b?a:b;}
int C[200005];
int dfs(int v,int qwq,int n,int m)
{
if(qwq>n)return 0;
if(dp[v][qwq])return dp[v][qwq];
int ans=C[qwq-1];
for(int i=qwq?2:1;i<=m;i++)
{
if(v*i>m)break;
ans=(ans+dfs(v*i,qwq+1,n,m))%mod;
}
return dp[v][qwq]=ans;
/*if(mp[x].count(y))return mp[x][y];
//if(x==0)return mp[x][y]=1;
if(x==1)return m/y-1+f;
int ans=0;
for(int i=2-f;i*y<=m;i++)ans+=dfs(x-1,y*i,m),ans%=mod;
return mp[x][y]=ans%mod;
return ans%mod;*/
}
inline int qp(int x,int p)
{
int res=1;
while(p)
{
if(p&1)res=res*x%mod;
x=x*x%mod;
p>>=1;
}
return res;
}
signed main()
{
int n=read(),m=read(),ans=0;
C[0]=1;
for(int i=1;i<=n-1;i++)C[i]=C[i-1]*(n-i)%mod*qp(i,mod-2)%mod;
//for(int i=1;i<=18;i++)
//{ans+=dfs(i,1,m,1)*C[i-1]%mod;for(int i=1;i<=n;i++)mp[i].clear();}
/*for(int i=1;i<=n;i++)
{
ans=(ans+mp[i][m]*qp(i,n-i))%mod;
}*/
printf("%lld\n",dfs(1,0,n,m));
//printf("%lld\n",dfs(n,1,m));
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
const int N = 2e5 + 200;
int n, m;
int factorial[N], invfac[N];
int power(int base, int expo) {
int res = 1;
while(expo) {
if(expo & 1) {
res = (res * base) % mod;
}
base = (base * base) % mod;
expo /= 2;
}
return res;
}
int modInverse(int num) {
return power(num, mod - 2);
}
int nCr(int n, int r) {
int num = factorial[n];
int den = invfac[n - r] * invfac[r];
den %= mod;
int ans = (num * den) % mod;
return ans;
}
int solve() {
cin >> n >> m;
int ans = 0;
for(int i = 1; i <= m; i++) {
int curr = 1;
int num = i;
for(int j = 2; j * j <= num; ++ j) {
int count = 0;
while(num % j == 0) {
num /= j;
++ count;
}
curr *= nCr(n + count - 1, count);
curr %= mod;
}
if(num > 1) {
curr *= n;
curr %= mod;
}
ans += curr;
ans %= mod;
}
return ans;
}
signed main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
//this can be opted out if you want to print the output to the sublime console
freopen("output.txt", "w", stdout);
#endif
factorial[0] = 1;
for(int i = 1; i < N; i++) {
factorial[i] = factorial[i - 1] * i;
factorial[i] %= mod;
}
invfac[N - 1] = modInverse(factorial[N - 1]) % mod;
for(int i = N - 2; i >= 0; -- i) {
invfac[i] = invfac[i + 1] * (i + 1);
invfac[i] %= mod;
}
cout << solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std ;
#define Next( i, x ) for( register int i = head[x]; i; i = e[i].next )
#define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i )
#define drep( i, s, t ) for( register int i = (t); i >= (s); -- i )
#define re register
#define int long long
int gi() {
char cc = getchar() ; int cn = 0, flus = 1 ;
while( cc < '0' || cc > '9' ) { if( cc == '-' ) flus = - flus ; cc = getchar() ; }
while( cc >= '0' && cc <= '9' ) cn = cn * 10 + cc - '0', cc = getchar() ;
return cn * flus ;
}
const int N = 100 + 5 ;
const int M = 6e5 + 5 ;
int n, m, P, lim, Ans[N], dp[N][M], sum[N][M] ;
signed main()
{
n = gi(), m = gi(), P = gi(), lim = n * (n + 2) / 2 * m, dp[0][0] = 1 ;
rep( i, 1, n ) {
rep( j, 0, i - 1 ) sum[i][j] = dp[i - 1][j] ;
rep( j, i, lim ) sum[i][j] = (sum[i][j - i] + dp[i - 1][j]) % P ;
rep( j, 0, (m + 1) * i - 1 ) dp[i][j] = sum[i][j] ;
rep( j, i * (m + 1), lim ) dp[i][j] = (sum[i][j] - sum[i][j - i * (m + 1)] + P) % P ;
}
rep( i, 1, n ) rep( j, 0, lim )
Ans[i] = (Ans[i] + dp[i - 1][j] * dp[n - i][j]) % P ;
rep( i, 1, n ) printf("%lld\n", (Ans[i] * (m + 1) % P + P - 1) % P ) ;
return 0 ;
} | #include <bits/stdc++.h>
using namespace std;
/*
* @title ModInt
* @docs md/util/ModInt.md
*/
template<long long& mod> class ModInt {
public:
long long x;
constexpr ModInt():x(0) {}
constexpr ModInt(long long y) : x(y>=0?(y%mod): (mod - (-y)%mod)%mod) {}
ModInt &operator+=(const ModInt &p) {if((x += p.x) >= mod) x -= mod;return *this;}
ModInt &operator+=(const long long y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;}
ModInt &operator+=(const int y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;}
ModInt &operator-=(const ModInt &p) {if((x += mod - p.x) >= mod) x -= mod;return *this;}
ModInt &operator-=(const long long y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;}
ModInt &operator-=(const int y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;}
ModInt &operator*=(const ModInt &p) {x = (x * p.x % mod);return *this;}
ModInt &operator*=(const long long y) {ModInt p(y);x = (x * p.x % mod);return *this;}
ModInt &operator*=(const int y) {ModInt p(y);x = (x * p.x % mod);return *this;}
ModInt &operator^=(const ModInt &p) {x = (x ^ p.x) % mod;return *this;}
ModInt &operator^=(const long long y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;}
ModInt &operator^=(const int y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;}
ModInt &operator/=(const ModInt &p) {*this *= p.inv();return *this;}
ModInt &operator/=(const long long y) {ModInt p(y);*this *= p.inv();return *this;}
ModInt &operator/=(const int y) {ModInt p(y);*this *= p.inv();return *this;}
ModInt operator=(const int y) {ModInt p(y);*this = p;return *this;}
ModInt operator=(const long long y) {ModInt p(y);*this = p;return *this;}
ModInt operator-() const {return ModInt(-x); }
ModInt operator++() {x++;if(x>=mod) x-=mod;return *this;}
ModInt operator--() {x--;if(x<0) x+=mod;return *this;}
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
ModInt operator^(const ModInt &p) const { return ModInt(*this) ^= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inv() const {int a=x,b=mod,u=1,v=0,t;while(b > 0) {t = a / b;swap(a -= t * b, b);swap(u -= t * v, v);} return ModInt(u);}
ModInt pow(long long n) const {ModInt ret(1), mul(x);for(;n > 0;mul *= mul,n >>= 1) if(n & 1) ret *= mul;return ret;}
friend ostream &operator<<(ostream &os, const ModInt &p) {return os << p.x;}
friend istream &operator>>(istream &is, ModInt &a) {long long t;is >> t;a = ModInt<mod>(t);return (is);}
};
long long mod;
using mint = ModInt<mod>;
int main() {
cin.tie(0);ios::sync_with_stdio(false);
int N,K;
cin >> N >> K >> mod;
int L = N*(N-1)/2*K;
vector<vector<mint>> dp(N,vector<mint>(L+1,0));
dp[0][0]=1;
for(int i=1;i<N;++i) {
dp[i]=dp[i-1];
//jの値をj,j+1*i,j+2*i,...,j+K*iに加算したい
//累積和で考えると、j+(K+1)*i,j+(K+2)*i,...に足しすぎる。
//imosを考えると先に j+(K+1)*iからjの分を引けば良い。
for(int j=L; 0 <= j; --j) if(j + (K+1)*i <= L) dp[i][j + (K+1)*i] -= dp[i][j];
for(int j=0;j<=L; ++j) if(j + i <= L) dp[i][j+i] += dp[i][j];
}
vector<mint> ans(N+1);
for(int i=1;i<=N;++i) {
int l = i-1;
int r = N-i;
mint sum = 0;
for(int j=0;j<=L;++j) sum += dp[l][j]*dp[r][j];
ans[i]=sum*(K+1) - 1;
}
for(int i=1;i<=N;++i) {
cout << ans[i] << "\n";
}
return 0;
}
|
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("trapv")
#pragma GCC target("sse4")
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long long int ll;
typedef unsigned long long int ull;
typedef priority_queue<ll> mxhpl;
typedef priority_queue<int> mxhpi;
typedef priority_queue<ll, vector<ll>, greater<ll>> mnhpl;
typedef priority_queue<int, vector<int>, greater<int>> mnhpi;
#define rep(i,start,end) for(ll i = start; i <= end; ++i)
#define rrep(i,end,start) for(ll i = end; i >= start; --i)
#define parr(a,n) rep(i,0,n-1){cout << a[i] << " ";}cout<<"\n"
#define sarr(a,n) rep(i,0,n-1)cin >> a[i]
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define mset(a,x) memset(a, x, sizeof(a))
#define ps(x,y) fixed<<setprecision(y)<<x //cout << ps(ans, decimal places);
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
// DEBUGGING
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...); }
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// Fast IO
void IO() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
}
#define MOD 1000000007
//CODE
/*******************************************************/
#define MX 200001
ll n;
void solve() {
cin >> n;
vl a(n), b(n), c(n), whoHasMyBag(n);
sarr(a, n);
sarr(b, n);
sarr(c, n);
vector<pll> wp;
rep(i, 0, n - 1) {
if (c[i] - 1 != i && a[i] <= b[c[i] - 1]) {
cout << "-1\n";
return;
}
c[i]--;
wp.pb({ a[i],i });
whoHasMyBag[c[i]] = i;
}
sort(all(wp));
vector<pll> ans;
for (auto p : wp) {
if (c[p.S] == p.S) continue;
ans.pb({ p.S + 1,whoHasMyBag[p.S] + 1 });
c[whoHasMyBag[p.S]] = c[p.S];
whoHasMyBag[c[whoHasMyBag[p.S]]] = whoHasMyBag[p.S];
}
// debug("TEST");
cout << ans.size() << "\n";
for (auto p : ans) {
cout << p.F << " " << p.S << "\n";
}
// debug(ans, c);
}
int main() {
IO();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
/*******************************************************/
| #include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N; cin >> N;
int A[200010], B[200010], P[200010];
int pos[200010];
pair<int, int> W[200010];
for(int i = 1; i <= N; i++) cin >> A[i];
for(int i = 1; i <= N; i++) cin >> B[i];
for(int i = 1; i <= N; i++) cin >> P[i];
for(int i = 1; i <= N; i++) pos[P[i]] = i;
for(int i = 1; i <= N; i++){
W[i].first = A[i]; W[i].second = i;
}
sort(W+1, W+N+1);
vector<pair<int, int>> ans;
for(int i = 1; i <= N; i++){
int a = W[i].second;
if(P[a] == a) continue;
if(A[a] <= B[P[a]]){
cout << -1 << "\n";
return 0;
}
ans.emplace_back(a, pos[a]);
int x = P[a]; int y = pos[a];
P[a] = a; P[y] = x;
pos[a] = a; pos[x] = y;
}
cout << ans.size() << "\n";
for(auto p : ans){
cout << p.first << " " << p.second << "\n";
}
} |
#include <iostream>
using namespace std;
int main()
{
int A, B;
cin >> A >> B;
for(int r = B; r >= 1; r--)
{
if((B/r * r) - r >= A)
{
cout << r << '\n';
return 0;
}
}
}
| #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <functional>
using ll = long long;
using namespace std;
template<typename A, typename B>
bool chmin(A &a, const B b) {
if (a <= b) return false;
a = b;
return true;
}
template<typename A, typename B>
bool chmax(A &a, const B b) {
if (a >= b) return false;
a = b;
return true;
}
#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);
template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
out << '{';
for (const T &item : v) out << item << ", ";
out << "\b\b}";
return out;
}
void _tostr_rec(ostringstream &oss) {
oss << "\b\b \b";
}
template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
oss << head << ", ";
_tostr_rec(oss, forward<Tail>(tail)...);
}
template<typename... T>
string _tostr(T &&...args) {
ostringstream oss;
int size = sizeof...(args);
if (size > 1) oss << "{";
_tostr_rec(oss, forward<T>(args)...);
if (size > 1) oss << "}";
return oss.str();
}
#endif
constexpr int mod = 1'000'000'007; //1e9+7(prime number)
constexpr int INF = 1'000'000'000; //1e9
constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18
constexpr int SIZE = 200010;
template<typename T>
vector<T> divisor(T n) {
vector<T> res, res2;
for (T i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i * i < n) res2.push_back(n / i);
}
}
reverse(res2.begin(), res2.end());
res.insert(res.end(), res2.begin(), res2.end());
return res;
}
// GCC __gcd(long long A, long long B)
ll gcd(ll a, ll b) {
if (a == 0) return b;
return gcd(b % a, a);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
int main() {
int A, B;
cin >> A >> B;
int ans = 1;
for (int i = A; i <= B; i++) {
int X = i;
auto ds = divisor(X);
for (int j : ds) {
int Y = X - j;
if (A <= Y && Y < X)
chmax(ans, gcd(X, Y));
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1010000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,M;
cin >> N >> M;
vvl dp(N+1,vl(int(1<<N),0));
dp[0][0] = 1;
ve<ve<pii>> A(N+1);
rep(i,M) {
int x;
cin >> x;
int y,z;
cin >> y >> z;
A[x].pb(make_pair(y,z));
}
rep(i,N) rep(j,int(1<<N)) if(dp[i][j] > 0){
rep(k,N) if(!(j & 1 << k)) {
int b = int(j | 1 << k);
int f = 1;
for (pii a : A[i+1]) {
int n = 0;
rep(ii, a.fi) if(b & 1 << ii) n++;
if(n > a.se){
f = 0;
break;
}
}
if(f){
dp[i+1][b] += dp[i][j];
}
}
}
//outmat(dp);
cout << dp[N][int(1<<N)-1] << endl;
return 0;
}
| #include <iostream>
#include <vector>
#include <unordered_set>
#include <random>
#include <algorithm>
#define INF (1ll<<60)
#define ll long long
using namespace std;
vector<pair<int,int>> counts[22];
bool HasBit(int x, int b) {
return ((x>>b)&1);
}
ll dp[(1<<18)+1];
ll Solve(int N, int mask, int x) {
if (x == N) {
return 1;
}
if (dp[mask]!=-1) return dp[mask];
ll ans=0;
for(int val=0;val<N;val++) {
if (HasBit(mask,val)) continue;
int nmask = (mask|(1<<val));
bool ok=true;
for(pair<int,int> yz : counts[x]) {
int y = yz.first;
int z = yz.second;
if (__builtin_popcount(nmask&((1<<(y+1))-1)) > z) ok=false;
}
if (ok) {
ans += Solve(N, nmask, x+1);
}
}
dp[mask]=ans;
return ans;
}
int main() {
int N,M;
cin>>N>>M;
for(int i=1;i<=M;i++) {
int x,y,z;
cin>>x>>y>>z;
x--;
y--;
counts[x].push_back({y,z});
}
for(int mask=0;mask<(1<<N);mask++)
dp[mask]=-1;
cout << Solve(N, 0, 0) << endl;
} |
Subsets and Splits