code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include <bits/stdc++.h>
typedef long long i64;
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
using namespace std;
int main() {
// freopen("#.in", "r", stdin); // @
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
i64 sum = 0;
vector<i64> a;
rep(r, 0, n + 1) {
a.push_back(sum);
if (r == n) {break;}
int x; cin >> x; r & 1 ? sum += x : sum -= x;
}
sort(all(a));
i64 res = 0;
for (int l = 0, r = 0; l < sz(a); l = r) {
for (; r < sz(a) and a[r] == a[l]; ++r);
res += 1ll * (r - l) * (r - l - 1) / 2;
}
cout << res << '\n';
return 0;
}
/* 📌https://www.cnblogs.com/George1123/p/tips.html */ | #include <bits/stdc++.h>
using namespace std;
unordered_map <long long, int> mp;
int main() {
int n; scanf("%d", &n);
long long sum = 0, ans = 0;
mp[0] = 1;
for (int i = 0; i < n; ++i) {
int x; scanf("%d", &x);
if (i & 1)
sum -= x;
else
sum += x;
ans += mp[sum]++;
}
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for(int i = 3, c1 = 1 ; i <= 1e18 ; i*=3, c1++)
{
for(int j = 5, c2 = 1 ; j <= 1e18 ; j*=5, c2++)
{
if(i+j == N)
{
cout << c1 << " " << c2 << '\n';
return 0;
}
}
}
cout << -1 << '\n';
return 0;
} | #include"bits/stdc++.h"
using namespace std;
using ll=long long;
template<class T=long long>inline T in(istream&is=cin){T ret;is>>ret;return ret;}
template<class T,class Size=typename vector<T>::size_type>inline auto vector_2d(Size h,Size w,T v){return vector<vector<T>>(h,vector<T>(w,v));}
template<class T>auto comp_pairs_by_second(){return[](const T&p1,const T&p2){return p1.second<p2.second||(p1.second==p2.second&&p1.first<p2.first);};}
int main()
{
vector<ll>p3{3},p5{5};
while(p3.back()<static_cast<ll>(1e+18))p3.push_back(p3.back()*3);
while(p5.back()<static_cast<ll>(1e+18))p5.push_back(p5.back()*5);
ll n=in();
for(int i=0;i<p3.size();++i){
for(int j=0;j<p5.size();++j){
if(p3[i]+p5[j]==n){
cout<<i+1<<' '<<j+1<<endl;
return 0;
}
}
}
cout<<"-1"<<endl;
}
|
/*
* Author: JophieQu
* Date: 06/06/21 19:53
* Welcome to follow my sister's CODEFORCES account:@Cccc23333
*/
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
const int N=2e3+7;
const int mod=1e9+7;
using namespace std;
ll ans=0;
vector<ll>e[N+1];
int vis[N];
void dfs(int ii,int fa,int f){
if(ii==fa&&f) return ;
for(int i=0;i<e[ii].size();++i){
if(!vis[e[ii][i]]){
vis[e[ii][i]]=1;
++ans;
// cout<<fa<<" "<<ii<<" "<<e[ii][i]<<" "<<ans<<endl;
dfs(e[ii][i],fa,1);
}
}
}
void solves(){
int n,m;cin>>n>>m;
while(m--){
int a,b;cin>>a>>b;
e[a].push_back(b);
}
for(int i=1;i<=n;++i){
memset(vis,0,sizeof(vis));
vis[i]=1;
dfs(i,i,0);
}
cout<<ans+n<<endl;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int OwwwO=1;
// cin>>OwwwO;
while(OwwwO--){
solves();
}
}
| #include <bits/stdc++.h>
#include <cassert>
#define rep(i, N) for (int 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 pb push_back
#define eb emplace_back
#define fi first
#define se second
#define nl '\n'
#define endl '\n'
#define all(c) begin(c), end(c)
#define ok() puts(ok ? "Yes" : "No");
#define pcnt(x) __builtin_popcountll(x)
#define df(x) ll x = in();
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using vs = vector<string>;
using P = pair<ll, ll>;
using gt = greater<P>;
template <class T> using minq = priority_queue<T, vector<T>, greater<T>>;
using vP = vector<P>;
inline ll in() {
ll x;
scanf("%lld", &x);
return x;
}
template <class T> void takeUnique(vector<T> &v) {
auto last = std::unique(v.begin(), v.end());
v.erase(last, v.end());
}
template <class T> void print(const initializer_list<T> &il) {
for (auto x : il) {
cout << x << " ";
}
cout << "\n";
}
inline void priv(vll a) {
rep(i, (int)a.size() - 1) cout << a[i] << " ";
cout << a.back() << nl;
}
inline void priv(vi a) {
rep(i, (int)a.size() - 1) cout << a[i] << " ";
cout << a.back() << nl;
}
const ll LINF = 8e18L + 1;
const int INF = 8e9 + 1;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int dxx[] = {0, 1, 1, 1, 0, -1, -1, -1};
int dyy[] = {1, 1, 0, -1, -1, -1, 0, 1};
// g++ -std=c++17 -stdlib=libc++
#define _GLIBCXX_DEBUG
// This slows down the execution; even the time complexity, since it checks if
// std funcs such as lower_bound meets prereqs
vi to[2005];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(16);
int n, m;
cin >> n >> m;
rep(i, m) {
int a, b;
cin >> a >> b;
a--;
b--;
to[a].push_back(b);
}
int ans = 0;
auto f = [&](int v) {
vi vis(n);
vis[v] = 1;
queue<int> q;
q.push(v);
while (q.size()) {
int u = q.front();
q.pop();
for (int nx : to[u]) {
if (vis[nx])
continue;
q.push(nx);
}
vis[u] = true;
}
rep(i, n) {
if (vis[i])
ans++;
}
};
rep(i, n) { f(i); }
cout << ans << nl;
return 0;
}
|
#include <bits/stdc++.h>
int n;
long long x;
long long a[100];
long long b[100];
long long dp[100][2];
// dp[i]最低i位的方案数字
// dp[i][0]: 不借位的方案数
// dp[i][1]: 借位的方案数
int main() {
scanf("%d%lld", &n, &x);
for (int i = 1; i <= n; ++i) {
scanf("%lld", &a[i]);
}
long long tmp = x;
for (int i = n; i >= 1; --i) {
if (i == n) {
b[i] = tmp / a[i];
} else {
b[i] = std::min(tmp / a[i], a[i + 1] / a[i] - 1);
}
tmp = tmp - b[i] * a[i];
// printf("%lld,", b[i]);
}
assert(tmp == 0);
// printf("\n");
// 不被借位
if (b[1] == 0) {
dp[1][0] = 1;
} else {
dp[1][0] = 1;
dp[1][1] = 1;
}
// printf("%d: %lld %lld\n", 1, dp[1][0], dp[1][1]);
for (int i = 2; i < n; ++i) {
if (!b[i]) {
dp[i][0] += dp[i - 1][0];
dp[i][1] += 0ll;
dp[i][0] += dp[i - 1][1];
dp[i][1] += dp[i - 1][1];
} else {
dp[i][0] += dp[i - 1][0];
dp[i][1] += dp[i - 1][0];
if (b[i] + 1 < a[i + 1] / a[i]) {
dp[i][0] += dp[i - 1][1];
}
dp[i][1] += dp[i - 1][1];
}
// printf("%d: %lld %lld\n", i, dp[i][0], dp[i][1]);
}
if (n == 1) {
printf("1\n");
} else {
printf("%lld\n", dp[n - 1][0] + dp[n - 1][1]);
}
return 0;
} | #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
#define int long long
#define endl "\n"
constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;
struct fast_io {
fast_io(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
};
} fio;
signed main(){
cout<<fixed<<setprecision(10);
int N;
int x = 0;
vector<int> A;
vector<int> X;
vector<vector<int>> dp;
cin>>N>>x;
A.resize(N);
X.resize(N);
dp.resize(N+1, vector<int>(2));
for(int i = 0; i < N; i++){
cin>>A[i];
}
for(int i = N-1; i >= 0; i--){
X[i] = x/A[i];
x %= A[i];
// cout<<"X = "<<X[i]<<endl;
}
dp[0][0] = 1;
for(int i = 0; i < N; i++){
if(X[i] == 0) {
dp[i+1][0] += dp[i][0];
dp[i+1][0] += dp[i][1];
dp[i+1][1] += dp[i][1];
} else if(i + 1 != N && X[i] == A[i+1]/A[i] - 1) {
dp[i+1][0] += dp[i][0];
dp[i+1][1] += dp[i][0];
dp[i+1][1] += dp[i][1];
} else {
dp[i+1][0] += dp[i][0];
dp[i+1][1] += dp[i][0];
dp[i+1][0] += dp[i][1];
dp[i+1][1] += dp[i][1];
}
// cout<<"i = "<<i<<endl;
// cout<<dp[i][0]<<endl;
// cout<<dp[i][1]<<endl;
// cout<<endl;
}
// cout<<dp[N][0]<<endl;
// cout<<dp[N][1]<<endl;
cout<<dp[N-1][0] + dp[N-1][1]<<endl;
return 0;
} |
#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) 1e15;
ll A[300005];
template<typename T>
struct SegmentTree {
using F = function<T(T, T)>;
SegmentTree(F func, T id)
: merge(func), identity(id) {}
void init(ll n) {
offset = 1;
while (offset < n)
offset <<= 1;
node.assign(offset << 1, identity);
}
void build(const vector<T> &v) {
int n = static_cast<int>(v.size());
init(n);
for (int i = 0; i < n; ++i) {
node[offset + i] = v[i];
}
for (int i = offset - 1; i >= 0; --i) {
node[i] = merge(node[(i << 1) | 0], node[(i << 1) | 1]);
}
}
void set(int index, T value) {
index += offset;
node[index] = value;
while (index >>= 1) {
node[index] = merge(node[(index << 1) | 0], node[(index << 1) | 1]);
}
}
//! [left, right]
T get(int left, int right) {
left += offset;
right += offset + 1;
T val = identity;
while (left < right) {
if (left & 1)
val = merge(val, node[left++]);
if (right & 1)
val = merge(val, node[--right]);
left >>= 1;
right >>= 1;
}
return val;
}
T identity;
F merge;
vector<T> node;
int offset;
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, Q;
cin >> N >> Q;
SegmentTree<ll> seg([](ll a, ll b) { return a ^ b; }, 0);
seg.init(N);
REP(i, N) {
ll a;
cin >> a;
seg.set(i, a);
}
REP(_, Q) {
ll t, a, b;
cin >> t >> a >> b;
a--;
if (t == 1) {
ll p = seg.get(a, a);
seg.set(a, (p ^ b));
}
else {
b--;
cout << seg.get(a, b) << endl;
}
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FASTINOUT ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int N=3e5+9;
ll a[N],tree[4*N];
void build(int node, int start, int end)
{
if (start == end)
{
tree[node] = a[start];
}
else
{
int mid = (start + end) / 2;
build(2 * node, start, mid);
build(2 * node + 1, mid + 1, end);
tree[node] = (tree[2 * node]^tree[2 * node + 1]);
}
}
long long query_range(int node, int start, int end, int l, int r)
{
if (start > r or end < l)
return 0;
if (start >= l and end <=r)
return tree[node];
int mid = (start + end) / 2;
long long p1 = query_range(node * 2, start, mid, l, r);
long long p2 = query_range(node * 2 + 1, mid + 1, end, l, r);
return (p1^p2);
}
void update(int node, int start, int end, int position, long long val)
{
if (start > position or end < position)
return;
if (start==end)
{
a[start]=val^a[start];
tree[node]=a[start];
return;
}
int mid = (start + end) / 2;
update(node * 2, start, mid, position, val);
update(node * 2 + 1, mid + 1, end, position, val);
tree[node] = (tree[node * 2] ^ tree[node * 2 + 1]);
}
int main()
{
FASTINOUT;
int x,q;
cin>>x>>q;
for (int i=1; i<=x; i++)
cin>>a[i];
build(1,1,x);
while (q--)
{
ll type,l,r;
cin>>type>>l>>r;
if (type==1)
{
update(1,1,x,l,r);
}
else
{
cout<<query_range(1,1,x,l,r)<<endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<long long> ans;
int a, b;
cin >> a >> b;
if (a == b){
for (int i = 1; i <= a; i++){
ans.push_back(i);
ans.push_back(-i);
}
}else if (a > b){
for (int i = 1; i <= a; i++){
ans.push_back(i);
}
for (int i = 1; i < b; i++){
ans.push_back(-i);
}
long long sum = accumulate(ans.begin(), ans.end(), 0);
ans.push_back(-sum);
}else{
for (int i = 1; i < a; i++){
ans.push_back(i);
}
for (int i = 1; i <= b; i++){
ans.push_back(-i);
}
long long sum = accumulate(ans.begin(), ans.end(), 0);
ans.push_back(-sum);
}
for (int i = 0; i < ans.size(); i++) cout << ans.at(i) << " ";
cout << endl;
return 0;
}
| #pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP2(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = (l), i##_len = (int)(r); i < i##_len; ++i)
#define GET_MACRO_REP(_1, _2, _3, NAME, ...) NAME
#define REP(...) GET_MACRO_REP(__VA_ARGS__, REP3, REP2) (__VA_ARGS__)
#define RREP2(i, n) for (int i = (n - 1); i >= 0; --i)
#define RREP3(i, l, r) for (int i = (r - 1), i##_len = (l); i >= i##_len; --i)
#define GET_MACRO_RREP(_1, _2, _3, NAME, ...) NAME
#define RREP(...) GET_MACRO_REP(__VA_ARGS__, RREP3, RREP2) (__VA_ARGS__)
#define IN(type, n) type n; cin >> n
#define INALL(type, v, s) vector<type> v(s); for (auto &e : v) { cin >> e; }
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#ifdef _DEBUG
#define DEBUG(x) cout << #x << ": " << x << endl
#else
#define DEBUG(x)
#endif
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; }
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
#pragma endregion
int main() {
int query = 1000;
REP(q, query) {
IN(int, si);
IN(int, sj);
IN(int, ti);
IN(int, tj);
int now_i = si;
int now_j = sj;
string order = "";
while (true) {
if (ti == now_i && tj == now_j) break;
if (rand() % 2) {
if (ti > now_i) {
order += "D";
now_i++;
} else if (ti < now_i) {
order += "U";
now_i--;
}
} else {
if (tj > now_j) {
order += "R";
now_j++;
} else if (tj < now_j) {
order += "L";
now_j--;
}
}
}
cout << order << endl;
IN(ll, score);
}
return 0;
}
|
// #include <atcoder/all>
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
/*-------------------------------------------*/
int n;
int a[200], b[200];
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> n;
int c[200]{};
rep(i, n) {
cin >> a[i] >> b[i];
if(a[i] != -1) c[--a[i]]++;
if(b[i] != -1) c[--b[i]]++;
}
rep(i, 2 * n) if(c[i] > 1) {
cout << "No\n";
return 0;
}
int type[200]{};
int dp[200];
memset(dp, -1, sizeof(dp));
dp[0] = 0;
rep(i, n) {
if(a[i] != -1) type[a[i]] = -(i + 1);
if(b[i] != -1) type[b[i]] = (i + 1);
if(a[i] == -1 && b[i] == -1) dp[0]++;
}
rep(i, n + 1) {
rep(j, i) {
int cnt = i - j;
bool ok = true;
FOR(k, j * 2, j + i) if(type[k] != 0) {
if(type[k] > 0)
ok = false;
else if(type[k + (i - j)] != 0 && type[k] != -type[k + (i - j)])
ok = false;
cnt--;
}
FOR(k, j + i, i * 2) if(type[k] != 0) {
if(type[k] < 0) ok = false;
if(type[k - (i - j)] == 0) cnt--;
}
if(!ok) continue;
chmax(dp[i], dp[j] - cnt);
}
}
cout << (dp[n] >= 0 ? "Yes" : "No") << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
void fail(){
cout << "No" << endl;
exit(0);
}
int main(){
int N;
cin >> N;
vector<int> A(N), B(N);
vector<pair<int, int>> p2n(2*N+1, {-1, -1});
for(int i=0; i<N; i++){
cin >> A[i] >> B[i];
if(A[i] != -1 && B[i] != -1 && A[i] >= B[i]) fail();
if(A[i] != -1){
if(p2n[A[i]].first != -1) fail();
p2n[A[i]] = {i, 0};
}
if(B[i] != -1){
if(p2n[B[i]].first != -1) fail();
p2n[B[i]] = {i, 1};
}
}
auto check = [&](int L, int R)->bool{
int d = (R-L)/2;
for(int l=L; l<L+d; l++){
int r = l+d;
auto [li, lc] = p2n[l];
auto [ri, rc] = p2n[r];
if(li != -1){
if(lc != 0) return false;
if(B[li] != -1 && B[li] != r) return false;
}
if(ri != -1){
if(rc != 1) return false;
if(A[ri] != -1 && A[ri] != l) return false;
}
if(li != -1 && ri != -1 && li != ri) return false;
}
return true;
};
vector<bool> ok(2*N+2);
ok[1] = true;
for(int i=1; i<=2*N; i+=2) if(ok[i]) for(int r=i+2; r<=2*N+1; r+=2){
if(check(i, r)) ok[r] = true;
}
cout << (ok[2*N+1] ? "Yes" : "No") << 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 int ll;
typedef long double ld;
#define S 600020
struct dinic {
struct edge {int b, cap, flow, flip;};
vector<edge> g[S+2];
int ans=0, d[S+2], ptr[S+2];
void add_edge (int a, int b, int cap) {
g[a].push_back({b, cap, 0, g[b].size()});
g[b].push_back({a, 0, 0, g[a].size()-1});
}
int dfs (int u, int flow=INT_MAX) {
if (u==S+1 || !flow) return flow;
while (++ptr[u] < g[u].size()) {
edge &e = g[u][ptr[u]];
if (d[e.b] != d[u]+1) continue;
if (int pushed = dfs(e.b, min(flow, e.cap-e.flow))) {
e.flow += pushed;
g[e.b][e.flip].flow -= pushed;
return pushed;
}
}
return 0;
}
void calc() {
do {
vector<int> q {S};
memset(d, 0, sizeof d);
int i = -(d[S] = 1);
while (++i<q.size() && !d[S+1])
for (auto e: g[q[i]])
if (!d[e.b] && e.flow<e.cap) {
q.push_back(e.b);
d[e.b] = d[q[i]]+1;
}
memset(ptr, -1, sizeof ptr);
while(int pushed=dfs(S)) ans+=pushed;
} while (d[S+1]);
}
};
dinic flow;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
set<int> colors;
for (int i=0; i<n; ++i) {
int a, b;
cin>>a>>b;
flow.add_edge(S, i, 1);
flow.add_edge(i, a+n, 1);
flow.add_edge(i, b+n, 1);
colors.insert(a);
colors.insert(b);
}
for (auto c:colors) {
flow.add_edge(c+n, S+1, 1);
}
flow.calc();
cout<<flow.ans<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl '\n'
#define pb push_back
#define pob pop_back
#define pii pair<int,int>
// int mod = 3;
#define mod 1000000007
// #define mod 1000000009
// #define mod 163577857
// #define mod 998244353
#define rep(i,n) for (int i = 0; i < n; i++)
#define repp(i,a,b) for(int i = a ; i<b ; i++)
#define reppr(i,a,b) for(int i = a-1 ; i>=b ; i--)
#define repr(i,n) for (int i = n - 1; i >= 0; i--)
#define ff first
#define ss second
#define inf 9223372036854775807
#define infn -9223372036854775807
#define pi 3.14159265358979323846
#define eps 0.0000000001
#define setprec(x) cout << fixed << setprecision(x);
#define REVERSE(a) reverse(all(a));
#define SORT(a) sort(all(a));
#define all(n) n.begin(),n.end()
#define setbits(x) __builtin_popcountll(x)
//GCD and LCM
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; }
//Modular Exponentiation
int powmod(int x,int y)
{
if (y == 0) return 1;
int p = powmod(x, y/2) % mod;
p = (p * p) % mod;
return (y%2 == 0)? p : (x * p) % mod;
}
//Modular Inverse
int inverse(int a)
{
return powmod(a,mod-2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////NO TEXT ABOVE IT//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool codejam = 0, testcases = 0;
int parent[400001];
int rank[400001];
int find_set(int v) {
if (v == parent[v])
return v;
return find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b)
parent[b] = a;
}
void solve()
{
int n , a , b;
cin >> n;
map<int,vector<int>> m;
repp(i,1,400001)
parent[i] = i;
rep(i,n)
{
cin >> a >> b;
if(a>b) swap(a,b);
m[a].pb(b);
}
set<int> ans;
for(auto i : m)
{
for(auto j : i.ss)
{
if(ans.find(find_set(j))==ans.end())
ans.insert(find_set(j)) , union_sets(i.ff,j);
else
ans.insert(find_set(i.ff));
}
}
cout << ans.size() << endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////NO TEXT BELOW IT//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t = 1;
if(testcases)
cin >> t;
rep(i,t)
{
if(codejam)
cout << "Case #" << i+ 1<< ": ";
solve();
}
} |
#include<bits/stdc++.h>
#include<cmath>
#define SZ(x) ((int)x.size())
#include<vector>
#include<set>
#include<map>
#define ll long long
#define lli long long int
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#include<string>
#include<cctype>
#define F first
#define rep(i,a,b) for(int i=a;i<b;i++)
#define S second
#include<queue>
#include <sstream>
#define lli long long int
#define pairs pair<int,int>
#define ld long double
#define mod 1000000007;
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
//first try to write all condition on copy then go for implement
ll pows(ll a,ll n,ll m)
{
a=a%mod;
ll res=1;
while(n)
{
if(n%2!=0)
{
res=(res*a)%m;
n--;
}
else
{
a=(a*a)%m;
n=n/2;
}
}
return res%m;
}
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
bool isprime(ll n)
{
if(n==1)
{
return false;
}
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
bool istrue(string s)
{
int i=0;
int j=s.size()-1;
while(i<j)
{
if(s[i]==s[j])
{
i++;
j--;
}
else
{
return false;
}
}
return true;
}
const int N=2005;
string s[N];
ll dp[N][N];
ll phor[N][N];
ll pver[N][N];
ll pdia[N][N];
bool wall(int x,int y){
if(x < 1 || y < 1 || s[x-1][y-1] == '#') return true;
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int h,w;
cin >> h >> w;
rep(i,0,h){
cin >> s[i];
}
dp[1][1] = 1;
phor[1][1] = 1;
pver[1][1] = 1;
pdia[1][1] = 1;
rep(i,1,h+1){
rep(j,1,w+1){
if(i == 1 && j == 1) continue;
if(wall(i,j)) continue;
dp[i][j] = (phor[i-1][j] + pver[i][j-1] + pdia[i-1][j-1]) % mod;
phor[i][j] = dp[i][j];
pver[i][j] = dp[i][j];
pdia[i][j] = dp[i][j];
phor[i][j] += phor[i-1][j];
pver[i][j] += pver[i][j-1];
pdia[i][j] += pdia[i-1][j-1];
phor[i][j] %= mod;
pver[i][j] %= mod;
pdia[i][j] %= mod;
}
}
cout << dp[h][w]<<endl;
} | #include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
int h,w;
long long int dp[2001][2001];
char board[2001][2001];
long long int up[2001];
long long int diag[2001];
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> h >> w;
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
cin >> board[i][j];
}
}
memset(diag,0,sizeof(diag));
memset(up,0,sizeof(up));
dp[1][1] = 1;
for(int i=1;i<=h;i++)
{
long long int left = 0;
for(int j=1;j<=w;j++)
{
if(board[i][j]=='#')
{
left = 0;
}
else
{
if(i==1 && j==1)
{
left += 1;
}
else
{
dp[i][j] = ((dp[i][j]%MOD) + (left%MOD)%MOD);
dp[i][j]%=MOD;
dp[i][j] = ((dp[i][j]%MOD) + (up[j]%MOD)%MOD);
dp[i][j]%=MOD;
if(j>1)
{
dp[i][j] = ((dp[i][j]%MOD) + (diag[j-1]%MOD)%MOD);
dp[i][j]%=MOD;
}
left = ((left%MOD) + (dp[i][j]%MOD)%MOD);
left%=MOD;
}
}
}
long long int temp[2001];
//memcpy(temp,diag,sizeof(diag));
for(int j=1;j<=w;j++)
{
if(board[i][j]=='#')
{
up[j] = 0;
}
else
{
up[j] = ((up[j]%MOD) + (dp[i][j]%MOD)%MOD);
dp[i][j]%=MOD;
}
}
for(int j=w;j>=1;j--)
{
if(j==1)
{
diag[j] = dp[i][j];
}
else
{
if(board[i][j]=='#')
{
diag[j] = 0;
}
else
{
diag[j] = ((diag[j-1]%MOD) + (dp[i][j]%MOD)%MOD);
diag[j]%=MOD;
}
}
}
}
cout << dp[h][w] << '\n';
return 0;
} |
#include<bits/stdc++.h>
typedef double D;
typedef long long int LL;
typedef long double LD;
#define OR ||
#define AND &&
#define nl '\n'
#define S string
#define inf INT_MAX
#define SQR(a) (a) * (a)
#define pb push_back
#define GCD(a, b) __gcd(a, b)
#define PI 2.0*acos(0.0)
#define pii pair<int,int>
#define pll pair<long long,long long>
#define LCM(a, b) (a * b) / GCD(a, b)
#define mem(a,b) memset(a,b,sizeof(a))
#define srtv(v) sort(v.begin(),v.end())
//#define T int t; cin>>t; while(t--)
#define Rep(i,a,b) for(int i = a; i <= b; i++)
#define rep(i,a,b) for(int i = a; i >= b; i--)
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define inout freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define si(x) scanf("%d",&x)
#define pi(x) printf("%d",x)
#define ss(str) scanf("%s",str)
#define pl(x) printf("%lld",x)
#define sl(x) scanf("%lld",&x)
#define sii(x,y) scanf("%d %d",&x,&y)
#define sll(x,y) scanf("%lld %lld",&x,&y)
#define siii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
using namespace std;
/*
int bs(int l,int r,int n)
{
int mid,res=inf;
while(l<=r)
{
mid=l+(r-l)/2;
if(check(mid,n))
{
r=mid-1;
res=min(res,mid);
}
else
l=mid+1;
}
return res;
}
*/
//--------------------------code starts here---------------------------------------
const int mx=100005;
vector<int> adj[mx];
bool vis[mx];
int clr[mx];
int now[mx];
bool isGood[mx];
void dfs(int u)
{
vis[u]=true;
now[clr[u]]++;
if(now[clr[u]]==1)
isGood[u]=true;
for(int i=0;i<adj[u].size();i++)
{
if(!vis[adj[u][i]])
{
//now[clr[u]]++;
dfs(adj[u][i]);
//now[clr[u]]--;
}
}
now[clr[u]]--;
}
int main()
{
int n;
si(n);
Rep(i,1,n)
{
si(clr[i]);
}
int u,v;
Rep(i,1,n-1)
{
sii(u,v);
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
Rep(i,1,n)
{
if(isGood[i])
{
printf("%d\n",i);
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
//*******************************************Defines***************************************************************************
#define all(x) (x).begin(),(x).end()
#define sort(x) sort(x.begin(),x.end())
#define maxin(x) *max_element(x.begin(),x.end())
#define minin(x) *min_element(x.begin(),x.end())
#define lint long long int
#define nl '\n'
#define INF (lint)1e18
#define EPS (double)1/(INF)
//#define MOD (lint)998244353
#define MOD (lint)(1e9+7)
#define debug(x) cout<<#x<<"\t=\t"<<(x)<<"\n"
// ********************************************** Templates********************************************************************
template <typename T> inline T powmod(T a, T b){ if(b==0) return 1; T half = powmod(a,b/2); return (((half*half)%MOD)*(b%2?a:1))%MOD;}
template <typename T> inline T gcd(T a, T b) { while (b != 0) swap(b, a %= b); return a;}
template <typename T> inline T lcm(T a, T b) { return ((a/gcd(a,b))*b);}
template <typename T> inline T raise_pow(T base, T exp){
if(exp==0) return (T)1;
T half = raise_pow(base,exp/2);
if(exp%2) return (half*half*base);
return half*half;
}
template <typename T> inline T max_pow(T base, T exp){
if(base<exp) return 0;
return 1+max_pow(base/exp,exp);
}
void solve(){
int n;
cin>>n;
vector<lint> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
sort(arr);
int r = (n/2);
long double ans = 0;
for(int i=0;i<n;i++){
if(i+1>=r+1)
ans+=arr[i];
if(i+1==r+1 && n%2==1)
ans=ans-((long double)arr[i])/2;
}
cout<<setprecision(25)<<ans/n;
}
int main()
{ ios_base::sync_with_stdio(0);cin.tie(0);lint testnum=1,totalnum=1;
//cin>>totalnum;
for(testnum=1;testnum<=totalnum;++testnum){
//cout<<"Case #"<<testnum<<": ";
solve();
cout<<nl;
}
bool time=false;
//time=true;
if(time){
std::chrono::high_resolution_clock::time_point end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time_taken = std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time);
cout << "\nTime Taken: " <<setprecision(10)<<time_taken.count() << " s";
}
return 0;}
/*
created on: 12.06.2021
*/
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long int
#define INF 1000000007
#define llINF 1000000000000000007
#define v(i) vector<i>
#define vv(i) vector<vector<i>>
// int num = atoi(string.c_str()); 文字列の整数変換
int keta(ll n){int ans=0;while(n>0){n /= 10; ans++;}return ans;}
ll _pow(ll a,ll b){ll ans = 1;rep(i,b){ ans *= a;}return ans;}
int main() {
string a;cin>>a;
map<int,int> m;
vector<int> v;
rep(i,a.size()){
int cnt = a[i] - '0';
m[cnt]++;
v.push_back(cnt);
}
bool flag = false;
if(v.size() == 1){
if(v[0] == 8)flag = true;
}
else if(v.size() == 2){
if((v[0]*10+v[1])%8==0 || (v[1]*10+v[0])%8==0)flag = true;
}
else{
rep(i,10){
rep(j,10){
rep(k,10){
map<int,int>M;
M[i]++;M[j]++;M[k]++;
int cnt = i*100 +j*10 +k;
if(cnt % 8 !=0 || cnt == 0) continue;
if(M[i]<=m[i]&&M[j]<=m[j]&&M[k]<=m[k]) flag = true;
}
}
}
}
if(flag){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
return 0;
}
| #include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i=0;i<n;++i)
#define ren(i,n) for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define gc getchar
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array
using namespace std;
int main() {
string s;
cin>>s;
ll n=s.size();
if(n==1){
ll num=s[0]-'0';
if(num%8)cout<<"No";
else cout<<"Yes";
return 0;
}
if(n==2){
ll num=(s[0]-'0')*10+(s[1]-'0');
if(num%8==0){
cout<<"Yes";
return 0;
}
num=(s[1]-'0')*10+(s[0]-'0');
if(num%8==0){
cout<<"Yes";
return 0;
}
cout<<"No";
return 0;
}
map<char,ll> help;
for(ll i=0;i<n;i++) help[s[i]]++;
vector<string> a;
for(ll i=0;i<=999;i+=8){
if(i%8==0){
string h=to_string(i);
ll to_add=max(0ll,3-(ll)h.size());
while(to_add--) h='0'+h;
map<char,ll> cn;
cn[h[0]]++;
cn[h[1]]++;
cn[h[2]]++;
if(cn[h[0]]<=help[h[0]] && cn[h[1]]<=help[h[1]] && cn[h[2]]<=help[h[2]]){
cout<<"Yes";
return 0;
}
}
}
cout<<"No";
return 0;
}
|
#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>
#include <climits>
#include <cstring>
#include <cassert>
using namespace std;
//using namespace atcoder;
#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
REP(i, SZ(v)) {
if (i) os << ", ";
os << v[i];
}
return os << "]";
}
template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "(" << p.first << " " << p.second << ")";
}
template<class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;
// edit
void solve() {
ll R, X, Y;
cin >> R >> X >> Y;
ll norm = X * X + Y * Y;
if (norm == R * R) {
cout << 1 << endl;
} else if (norm < R * R) {
cout << 2 << endl;
} else {
ll k = 1;
while (norm > k * k * R * R) {
k++;
}
cout << k << endl;
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
solve();
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define Ma7moud_7amdy \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define Open_Sesame Open()
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define watch(x) cout << #x << " = " << x << endl;
#define endl "\n"
#define double long double
#define INF 0x3f3f3f3f3f3f3f3fLL
const int dx[] = { 1, 0, -1, 0, 1, 1, -1, -1 };
const int dy[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
typedef long long ll;
typedef unsigned long long ull;
void Open()
{
#ifndef ONLINE_JUDGE
freopen("standard.in", "r", stdin);
freopen("standard.out", "w", stdout);
#else
//freopen("eliminating.in", "r", stdin);
//freopen("standard.out", "w", stdout);
#endif
}
const ll mod = int(1e9 + 7), N = 500 + 5, oo = int(1e9), M = N * 104 * 2;
//"The secret of getting ahead is getting started". Mark Twain
//if you get WR, don't try to debug the code too much: think about another idea;
//don't be afraid of letters,letter G doesn't mean the problem is hard;
//don't stuck in one problem after you gets alot WR ,just jump to the next one
int main()
{
Ma7moud_7amdy;
//Open_Sesame;
ll r, x, y;
cin >> r >> x >> y;
double dis = sqrt(x * x + y * y);
if (dis == r) {
cout << 1 << endl;
}
else if (dis < r)cout << 2 << endl;
else {
ll cnt = ceil(dis / r);
int add = 0;// (dis / r) != (ceil(dis / r) * r);
cout << cnt + add << endl;
}
}
//"There's a way to do it better - find it". Thomas A. Edison |
#line 1 "main.cpp"
/**
* @title Template
*/
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>
#include <stack>
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/chmin_chmax.cpp"
template <class T, class U>
constexpr bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) { lhs = rhs; return true; }
return false;
}
template <class T, class U>
constexpr bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) { lhs = rhs; return true; }
return false;
}
/**
* @title Chmin/Chmax
*/
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"
#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"
class range {
struct iter {
std::size_t itr;
constexpr iter(std::size_t pos) noexcept: itr(pos) { }
constexpr void operator ++ () noexcept { ++itr; }
constexpr bool operator != (iter other) const noexcept { return itr != other.itr; }
constexpr std::size_t operator * () const noexcept { return itr; }
};
struct reviter {
std::size_t itr;
constexpr reviter(std::size_t pos) noexcept: itr(pos) { }
constexpr void operator ++ () noexcept { --itr; }
constexpr bool operator != (reviter other) const noexcept { return itr != other.itr; }
constexpr std::size_t operator * () const noexcept { return itr; }
};
const iter first, last;
public:
constexpr range(std::size_t first, std::size_t last) noexcept: first(first), last(std::max(first, last)) { }
constexpr iter begin() const noexcept { return first; }
constexpr iter end() const noexcept { return last; }
constexpr reviter rbegin() const noexcept { return reviter(*last - 1); }
constexpr reviter rend() const noexcept { return reviter(*first - 1); }
};
/**
* @title Range
*/
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/rev.cpp"
#include <type_traits>
#include <iterator>
#line 6 "/Users/kodamankod/Desktop/cpp_programming/Library/other/rev.cpp"
template <class T>
class rev_impl {
public:
using iterator = decltype(std::rbegin(std::declval<T>()));
private:
const iterator M_begin;
const iterator M_end;
public:
constexpr rev_impl(T &&cont) noexcept: M_begin(std::rbegin(cont)), M_end(std::rend(cont)) { }
constexpr iterator begin() const noexcept { return M_begin; }
constexpr iterator end() const noexcept { return M_end; }
};
template <class T>
constexpr decltype(auto) rev(T &&cont) {
return rev_impl<T>(std::forward<T>(cont));
}
/**
* @title Reverser
*/
#line 18 "main.cpp"
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;
int main() {
usize N;
std::cin >> N;
std::vector<u32> S(N), T(N);
for (auto &x: S) {
char c;
std::cin >> c;
x = c - '0';
}
for (auto &x: T) {
char c;
std::cin >> c;
x = c - '0';
}
std::stack<usize> one;
{
u32 count = 0;
for (auto i: rev(range(0, N))) {
if (S[i] == 1) {
count += 1;
one.push(i);
}
if (T[i] == 1) {
if (count == 0) {
std::cout << "-1\n";
return 0;
}
count -= 1;
}
}
if (count % 2 == 1) {
std::cout << "-1\n";
return 0;
}
}
u64 ans = 0;
for (auto i: range(0, N)) {
if (T[i] == 1) {
while (one.top() < i) {
usize x = one.top();
one.pop();
usize y = one.top();
one.pop();
ans += y - x;
}
ans += one.top() - i;
one.pop();
}
}
while (!one.empty()) {
usize x = one.top();
one.pop();
usize y = one.top();
one.pop();
ans += y - x;
}
std::cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
string s, t;
set<int> have;
int nxt(int idx)
{
auto it = have.upper_bound(idx);
if(it == have.end()){
cout<<-1<<'\n';
exit(0);
}
int res = *it;
have.erase(it);
s[res] = '0';
return res;
}
void solve()
{
int a = 0, b = 0;
for(int i=0;i<n;++i){
a+=(s[i]=='1');
b+=(t[i]=='1');
}
if(a%2!=b%2 || a<b){
cout<<-1<<'\n';
return;
}
for(int i=0;i<n;++i)if(s[i]=='1')have.insert(i);
ll ans = 0;
for(int i=0;i<n;++i){
if(s[i]!=t[i]){
if(s[i]=='1'){
ans+=1+(nxt(i)-(i+1));
}else{
ans += (nxt(i)-i);
}
}
}
cout<<ans<<'\n';
}
int main()
{
cin >> n >> s >> t;
solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
template<typename T>inline T read(){
T f=0,x=0;char c=getchar();
while(!isdigit(c)) f=c=='-',c=getchar();
while(isdigit(c)) x=x*10+c-48,c=getchar();
return f?-x:x;
}
#define int long long
namespace run{
const int N=4e5+9;
int head[N],nex[N],to[N],cnt;
inline void add(int u,int v){
nex[++cnt]=head[u];
head[u]=cnt,to[cnt]=v;
}
int n,m,a[N],b[N],vis[N],tota,totb;
inline void dfs(int u){
vis[u]=1,tota+=a[u],totb+=b[u];
for(int i=head[u];i;i=nex[i])
if(!vis[to[i]]) dfs(to[i]);
}
int main(){
n=read<int>(),m=read<int>();
for(int i=1;i<=n;i++) a[i]=read<int>();
for(int i=1;i<=n;i++) b[i]=read<int>();
for(int i=1;i<=m;i++){
int u=read<int>(),v=read<int>();
add(u,v),add(v,u);
}
for(int i=1;i<=n;i++) if(!vis[i]){
tota=totb=0,dfs(i);
if(tota!=totb) puts("No"),exit(0);
}
puts("Yes");
return 0;
}
}
#undef int
int main(){
#ifdef my
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
return run::main();
} | #include<bits/stdc++.h>
#define ll int64_t
#define pp pair<int,int>
#define ppn pair<pp,int>
#define F first
#define S second
#define N 1000005
#define mod 1000000007
#define pi 3.14159265358979323846
using namespace std;
vector<int>graph[N];
int vis[N], vis1[N];
ll a[N],b[N], n, m;
void dfs(int s, int r)
{
vis[s]=1;
for(int i=0; i<graph[s].size(); i++)
{
int d =graph[s][i];
if(!vis[d])
{
dfs(d,s);
}
}
if(a[s]!=b[s] && r!=-1){
if(b[s]>a[s]){
ll k = b[s]-a[s];
a[s]+=k;
a[r]-=k;
}
else{
ll k = a[s]-b[s];
a[s]-=k;
a[r]+=k;
//cout<<k<<endl;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++)cin>>a[i];
for(int i=1; i<=n; i++)cin>>b[i];
int u, v;
for(int i=0; i<m; i++)
{
cin>>u>>v;
graph[u].push_back(v);
graph[v].push_back(u);
}
for(int i=1; i<=n; i++)
{
if(!vis[i])
{
dfs(i,-1);
}
}
int f = true;
for(int i=1; i<=n; i++)
{
//cout<<a[i]<<" "<<b[i]<<endl;
if(a[i]!=b[i])
{
f = false;
break;
}
}
cout<<(f?"Yes":"No")<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n,l,r,mid;
scanf("%lld",&n),l=1,r=n;
while(l<r){
mid=(l+r+1)>>1;
if(mid+1<=((n+1)<<1)/mid)l=mid;
else r=mid-1;
}printf("%lld\n",n+1-r);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define For(i,a,b) for(int (i) = (a);(i) < (b);i++)
#define Rep(i,n) For((i),0,(n))
#define All(a) (a).begin(),(a).end()
#define sp " "
#define INF 1e18
#define INT_INF 1e9
typedef long long ll;
const ll MOD = 1000000007;
const ll MOD_9 = 998244353;
vector<char> boin = {'a','i','u','e','o'};
vector<char> alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char> Capital = {'Q','W','E','R','T','Y','U','I','O','P','A','S','D','F','G','H','J','K','L','Z','X','C','V','B','N','M'};
int main(){
int N;
cin >> N;
double ans = 0;
for(int i = 1;i < N;i++){
ans += (double)1/i;
//cout << i << sp << E[i] << endl;
}
cout << fixed << setprecision(8) << N*ans << endl;
cin >> N;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int N; cin >> N;
V<bool> is(200010);
int j=0;
rep(i,N){
int x; cin >> x; is[x]=true;
while(is[j])j++;
cout<<j<<endl;
}
}
| #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using ld = long double;
const int INF = (INT_MAX >> 1);
const ll LLINF = (LLONG_MAX >> 1);
#define all(x) x.begin(), x.end()
#define rep(i, s, e) for(ll i = s; i < e; ++i)
#define repr(i, s, e) for(ll i = s; i > e; --i)
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; }
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll a, b, w; cin >> a >> b >> w;
w *= 1000;
ll cnt_min = LLINF, cnt_max = -LLINF;
rep(cnt, 1, w + 1){
if (a * cnt <= w && w <= b * cnt){
cnt_max = max(cnt, cnt_max);
cnt_min = min(cnt, cnt_min);
}
}
if (cnt_max == -LLINF && cnt_min == LLINF) cout << "UNSATISFIABLE" << endl;
else cout << cnt_min << " " << cnt_max << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
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;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;
vvint g;
P dfs(int v, int p = -1) {
vint tmp;
int score = 1, cnt = 1, penalty = 0;
for (int n_v : g[v]) {
if (n_v == p) continue;
auto p = dfs(n_v, v);
cnt += p.second;
if (p.first < 0 && p.second % 2 == 0) score += p.first;
else if (p.first >= 0 && p.second % 2 == 0) penalty += p.first;
else tmp.push_back(p.first);
}
sort(all(tmp));
int turn = 1;
rep(i,tmp.size()) {
if (turn == 1) score += tmp[i];
else score -= tmp[i];
turn *= -1;
}
if (turn == 1) score += penalty;
else score -= penalty;
return P(score, cnt);
}
int main() {
int n; cin >> n;
g.resize(n);
REP(i,n) {
int p; cin >> p;
p--;
g[i].push_back(p);
g[p].push_back(i);
}
auto p = dfs(0);
cout << (p.second + p.first) / 2 << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
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;}
//-------------------------------------------------------
void solve() {
int i,j,k,l,r,x,y; string s;
FOR(y,20) {
FOR(x,20) cout<<(char)('A'+rand()%8);
cout<<endl;
}
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
|
// clang-format off
#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;
#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> oset;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vii;
#define INF 1000000000000000000
#define inf 1000000000
#define MOD 1000000007
// #define MOD 998244353
#define PI 3.1415926535897932385
#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()
#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar)) // 0x3f for inf, 0x80 for -INF can also use with pairs
#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
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__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng32)
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }
ld modp(ld b, ll e) {
ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }
void solve()
{
// START
ll n,m;
cin>>n>>m;
vi arr(m);
rep(i,m)cin>>arr[i];
sort(all(arr));
vi vec;
arr.pb(n+1);
if(arr[0]>1){
vec.pb(arr[0]-1);
}
fr(i,1,m){
if(arr[i]-arr[i-1]>1)
vec.pb(arr[i]-arr[i-1]-1);
}
ll mn=INF;
for(auto k:vec)mn=min(mn, k);
ll ans=0;
for(auto k:vec){
ans+=ceil((k*1.0)/mn);
}
cout<<ans<<"\n";
// END
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0), cout.precision(15); cout<<fixed;
#ifdef godfather
// cin.exceptions(cin.failbit);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
// cin>>t;
fr(i,1,t)
{
solve();
}
end_routine();
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
if(m==0){
cout<<1;
return 0;
}
if(n==m){
cout<<0;
return 0;
}
vector<int> v;
for(int i=0;i<m;i++){
int a;
cin>>a;
v.push_back(a);
}
int gap=1e9;
v.push_back(0);
v.push_back(n+1);
int l=v.size();
sort(v.begin(),v.end());
for(int i=0;i<l-1;i++){
if(v[i+1]-v[i]-1==0)
continue;
gap=min(gap,v[i+1]-v[i]-1);
}
int count=0;
for(int i=0;i<l-1;i++){
count+=(v[i+1]-v[i]-1+gap-1)/gap;
}
cout<<count;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
const int inf = 0x3f3f3f3f;
int mod = 1000000007;
bool remender(ll a , ll b){return a%b;}
int p[200005],siz[200005];
int get(int x){
return p[x]=(x==p[x] ? x : get(p[x]));
}
void unio(int u , int v){
int a = get(u);
int b=get(v);
if(a==b)return;
if(siz[a]>siz[b])swap(a,b);
p[b]=a;
siz[a]+=siz[b];
}
void solve(){
ll n;ll k;
ll d=0;
cin>>n>>k;
bool bb = 0;
for(int i = 0; i < n; i++){
ll a,b;
cin>>a>>b;
if((a*b)%100LL){
d+=(a*b)%100LL;
}
k-=(a*b)/100LL;
if(d >= 100LL){
d%=100LL;
k--;
}
if(k < 0 || (k==0 && d)){
if(bb==0){
cout<<i+1<<'\n';
bb=1;
}
}
}
if(bb){
return;
}
cout<<-1<<'\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin >> t;while(t--)
solve();
return 0;
}
| //#pragma GCC optimize ("Ofast,unroll-loops")
//#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include <unistd.h>
#define FAST_IO ios::sync_with_stdio(0);cin.tie(NULL)
#define all(v) (v).begin(),(v).end()
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
typedef long long Long;
typedef long double Double;
typedef unsigned long long ULong;
typedef pair<Long, Long> Pair;
const int N = 1e6;
const Long INF = 1e18;
const Double EPS = 1e-9;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
Long random(Long a, Long b) { return uniform_int_distribution<Long> (a, b) (rng); }
void Solve(void) {
Long n, x;
cin >> n >> x;
vector<Pair> a(n);
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
Long cur = 0;
for (int i = 0; i < n; i++) {
cur += a[i].first * a[i].second;
if (cur > 100LL * x) {
cout << i + 1 << '\n';
return;
}
}
cout << -1 << '\n';
}
int main(void) {
FAST_IO;
int t = 1;
//cin >> t;
while (t--) Solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << fixed << setprecision(15);
int n;
cin >> n;
vector<int>x(n);
for(int i=0;i<n;i++)cin >> x[i];
long ans1=0;
for(int i=0;i<n;i++)ans1+=abs(x[i]);
cout << ans1 << endl;
long ans2=0;
for(int i=0;i<n;i++)ans2+=abs(x[i])*(long)abs(x[i]);
cout << sqrt(ans2) << endl;
int ans3=0;
for(int i=0;i<n;i++)ans3=max(ans3,abs(x[i]));
cout << ans3 << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
long long int n,sum=0;
long double sum1=0;
cin>>n;
long long int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]<0)
a[i]=-a[i];
sum=sum+a[i];
sum1=sum1+a[i]*a[i];
}
sort(a,a+n);
cout<<sum<<endl<<setprecision(16)<<sqrt(sum1)<<endl<<a[n-1];
} |
#include <bits/stdc++.h>
#define f first
#define s second
#define MOD 1000000007
#define PMOD 998244353
#define pb(x) push_back(x)
using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> plii;
typedef pair<int, pii> piii;
const int INF = 1e9+10;
const ll LINF = 1LL*INF*INF;
const int MAXN = 1e4+10;
const int MAXM = 5e3+10;
priority_queue<int> pq;
vector<vector<int> > graph;
queue<int> que;
int A[MAXN];
int dp[MAXN][110];
int fac[110];
int main()
{
int n,m,k,a,b,x,y,q;
int sum = 0;
int cnt = 0;
int mx = 0;
int mn = INF;
int cur = 0, idx = -1;
int tc;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>A[i];
sum += A[i];
}
if(sum&1){
cout<<0<<"\n";
return 0;
}
dp[0][0] = 1;
for(int t=1;t<=n;t++){
cur = A[t];
for(int i=sum;i>=cur;i--){
for(int j=t;j>=1;j--){
dp[i][j] += dp[i-cur][j-1];
if(dp[i][j]>=PMOD)dp[i][j]-=PMOD;
}
}
}
fac[0] = 1;
for(int i=1;i<=n;i++){
fac[i] = (1LL*i*fac[i-1])%PMOD;
}
ll res = 0;
ll curv;
for(int i=1;i<n;i++){
curv = dp[(sum>>1)][i];
curv = (curv*(ll)fac[i])%PMOD;
curv = (curv*(ll)fac[n-i])%PMOD;
res += curv;
if(res>=PMOD)res-=PMOD;
}
cout<<res<<"\n";
return 0;
}
| #include <bits/stdc++.h>
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define size_t unsigned long long
#define ll long long
#define rep(i, a) for (int i = 0; i < (a); i++)
#define repr(i, a) for (int i = (int)(a)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= a; i--)
#define ALL(a) a.begin(), a.end()
using namespace std;
int si()
{
int x;
scanf("%d", &x);
return x;
}
long long sl()
{
long long x;
scanf("%lld", &x);
return x;
}
string ss()
{
string x;
cin >> x;
return x;
}
void pi(int x) { printf("%d ", x); }
void pl(long long x) { printf("%lld ", x); }
void pd(double x) { printf("%.15f ", x); }
void ps(const string &s) { printf("%s ", s.c_str()); }
void br() { putchar('\n'); }
const ll MOD = 1e9 + 7;
const ll INF = 1e9 + 5;
struct mint
{
int n;
mint(int n_ = 0) : n(n_) {}
};
mint operator+(mint a, mint b)
{
a.n += b.n;
if (a.n >= MOD)
a.n -= MOD;
return a;
}
mint operator-(mint a, mint b)
{
a.n -= b.n;
if (a.n < 0)
a.n += MOD;
return a;
}
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
typedef pair<int, int> P;
const ll N = 1e5 + 5;
string s;
ll n, result, q;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s >> q;
vector<int> t(q), a(q), b(q);
rep(i, q)
{
cin >> t[i] >> a[i] >> b[i];
a[i]--;
b[i]--;
}
vector<string> str(2);
int ini = 0;
str[0] = s.substr(0, n);
str[1] = s.substr(n, n);
rep(i, q)
{
if (t[i] == 2)
ini = (ini + 1) % 2;
else
{
if (a[i] < n && b[i] < n)
swap(str[ini][a[i]], str[ini][b[i]]);
else if (a[i] < n)
swap(str[ini][a[i]], str[(ini + 1) % 2][b[i] - n]);
else if (b[i] < n)
swap(str[ini][b[i]], str[(ini + 1) % 2][a[i] - n]);
else
swap(str[(ini + 1) % 2][a[i] - n], str[(ini + 1) % 2][b[i] - n]);
}
}
cout << str[ini] + str[(ini + 1) % 2] << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ln "\n"
void solve(int input)
{
int a, b;
cin >> a >> b;
int r = abs(a - b);
if (r <= 2)
{
cout << "Yes";
}
else
cout << "No";
return;
}
signed main()
{
int t = 1;
while (t--)
{
solve(t);
}
return 0;
} | #include<bits/stdc++.h>
#define ll long long int
#define pragi(a,b) for(ll i = a;i<b;i++)
#define pragj(a,b) for(ll j = a;j<b;j++)
#define pragk(a,b) for(ll k = a;k>=b;k--)
#define all(v) (v.begin(),v.end())
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define MP make_pair
#define MT make_tuple
#define F first
#define S second
#define KAKA ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int mod = 1e9 + 9;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef long double ld;
typedef pair<int, int> pii;
//typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef pair<ll, ll> pll;
int main()
{
KAKA
ll x,y;
cin>>x>>y;
if((llabs)(x - y) < 3) cout<<"Yes"<<"\n";
else cout<<"No"<<"\n";
} |
#include <bits/stdc++.h>
using namespace std;
const int c=200002;
long long kom[c], a[c], sok=1e16, nagymax=-sok, kismin=sok;
int n, q, t[c];
int main()
{
ios_base::sync_with_stdio(false);
cin >> n;
for (int i=1; i<=n; i++) {
cin >> a[i] >> t[i];
kom[i]=kom[i-1];
if (t[i]==1) {
kom[i]+=a[i];
} else {
a[i]-=kom[i];
if (t[i]==2) {
nagymax=max(nagymax, a[i]);
kismin=max(kismin, nagymax);
}
if (t[i]==3) {
kismin=min(kismin, a[i]);
nagymax=min(nagymax, kismin);
}
}
}
cin >> q;
for (int i=1; i<=q; i++) {
long long x; cin >> x;
cout << max(min(x, kismin), nagymax)+kom[n] << "\n";
}
return 0;
}
| //#define MULTIPLE_TESTS 0
#include "bits/stdc++.h"
#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
template<typename T> using policy_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
struct mul {
int m;
mul(int m) : m(m) {}
int operator()(int a, int b) { return 1ll * a * b % m; }
};
int identity_element(mul) { return 1; }
#define fastpower(a, b, m) power(a,b,mul(m))
#define endl '\n'
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define SZ(v) int((v).size())
const int N = 2e5 + 7, M = 2 * N, MOD = 1e9 + 7, OO = 0x3f3f3f3f;
int n, m, a[N], t[N], x[N], sorted[N];
ll ans[N];
void MAIN(int tc = 0) {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", a + i);
scanf("%d", t + i);
}
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
scanf("%d", x + i);
}
iota(sorted, sorted + m, 0);
sort(sorted, sorted + m, [](int i, int j) {
return x[i] < x[j];
});
ll plus = 0;
vector <pair<ll, int>> y;
for (int i = 0; i < m; ++i) {
y.emplace_back(x[sorted[i]], 1);
}
int st = 0, en = m - 1;
for (int i = 0; i < n; ++i) {
if (t[i] == 1) {
plus += a[i];
}
else if (t[i] == 2) {//max
int j;
int cnt = 0;
for (j = st; j <= en && y[j].F+plus < a[i]; ++j) cnt += y[j].S;
if (cnt)
y[j - 1] = {a[i]-plus, cnt}, st = j - 1;
}
else if (t[i] == 3) {//min
int j;
int cnt = 0;
for (j = en; j >= st && y[j].F+plus > a[i]; --j) cnt += y[j].S;
if (cnt)
y[j + 1] = {a[i]-plus, cnt}, en = j + 1;
}
}
int i = 0;
for(int j = st; j <= en; ++j){
ll cur = y[j].F;
int cnt = y[j].S;
while(cnt--) {
ans[sorted[i++]] = cur + plus;
}
}
for (int i = 0; i < m; ++i) {
printf("%lld\n", ans[i]);
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
#ifdef CLION
freopen("in", "rt", stdin);
#endif
#ifdef MULTIPLE_TESTS
int nTests;
#if MULTIPLE_TESTS == 0
scanf("%d", &nTests);
#else
cin >> nTests;
#endif
for (int tc = 1; tc <= nTests; ++tc) {
MAIN(tc);
}
#else
MAIN();
#endif
} |
#include<bits/stdc++.h>
using namespace std;
#define dd double
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ll long long
#define ull unsigned long long
#define ld long double
#define mx 300005
#define mod 1000000007
#define fr first
#define cti(a) (a-48)
#define itc(a) char(a+48)
#define se second
#define End cout<<"\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define memo(Array,val) memset(Array, val, sizeof Array)
#define PI acos(-1)
bool check(ll n, ll pos){ return (n&(1<<pos)); }
bool Set(ll n, ll pos) { return (n | (1<<pos)); }
ld LOG(ld b, ld e){ return log(b)/log(e); }
ll a[mx],tree[4*mx],lazy[4*mx];
ll Merge(ll lft, ll rgt)
{
return lft^rgt;
}
void update(ll node, ll b, ll e, ll i, ll val)
{
if(i>e || i<b)return;
if(b>=i && e<=i){
tree[node]^=val;
return;
}
ll mid=(b+e)/2;
ll left=node*2;
ll right=left+1;
update(left,b,mid,i,val);
update(right,mid+1,e,i,val);
tree[node]=Merge(tree[left],tree[right]);
}
ll Query(ll node, ll b, ll e, ll i, ll j)
{
//relax(node,b,e);
if(i>e || j<b)return 0;
if(b>=i && e<=j)return tree[node];
ll mid=(b+e)/2;
ll left=node*2;
ll right=left+1;
ll p=Query(left,b,mid,i,j);
ll q=Query(right,mid+1,e,i,j);
return Merge(p,q);
}
void clr() { memo(tree, 0), memo(lazy, 0), memo(a, 0); }
void solve(int kk)
{
clr();
int n,q;
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++){
scanf("%lld", &a[i]);
update(1, 1, n, i, a[i]);
}
//printf("Case %d:\n", kk);
while(q--){
int aa,bb,cc;
ll val;
scanf("%d %d %d", &aa, &bb, &cc);
if(aa == 1){
update(1, 1, n, bb, cc);
}
else printf("%lld\n", Query(1,1,n,bb,cc));
}
}
int main()
{
//fast;
int t=1,kk=0;
//scanf("%d", &t);
while(++kk<=t) solve(kk);
return 0;
}
| /*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
#define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
#define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1<<20;
const int BIT_NUM = 20;
struct BIT{
int _n;
long long _d[1 << BIT_NUM];
void init(int __n) {
_n=__n;
memset(_d, 0, sizeof(long long) * (_n+1));
}
void ins(int x, long long v) {
for(; x <= _n; x += x & -x) { _d[x] ^= v; }
}
long long qq(int x) {
if(x <= 0) {
return 0;
}
long long res = 0;
for(; x; x -= x & -x) { res ^= _d[x];}
return res;
}
long long qq_range(int x, int y) {
if(x > y) {
return 0;
}
long long ret = qq(y);
if(x)ret ^= qq(x-1);
return ret;
}
long long k_th(int k){
int now = 0;
for(int i = BIT_NUM - 1; i >= 0; i--) {
if(_d[now + (1 << i)] < k) {
k -= _d[now + (1 << i)];
now += 1 << i;
}
}
return now + 1;
}
}bit;
int N, Q;
void solve() {
FOR(i, 1, Q) {
int ty, x, y;
R(ty, x, y);
if(ty == 1) {
bit.ins(x, y);
} else {
W(bit.qq_range(x, y));
}
}
}
void input() {
R(N, Q);
bit.init(N);
FOR(i, 1, N) {
int x;
R(x);
bit.ins(i, x);
}
}
int main(){
input();
solve();
return 0;
}
|
#include <bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/rope>
using namespace std;
//usin g namespace __gnu_pbds;
//using namespace __gnu_cxx;
//template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
using namespace std;
#define go ios::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
#define ll long long
const int mxn = 2e5 + 5 , mod = 1e9 + 7 , MOD = 1e9 + 7 , mod2 = 998244353;
int n ;
vector<int>vi[mxn * 2];
int siz ;
bool ok ;
bool exist[mxn * 2] , vis[mxn * 2];
void dfs(int node, int par = -1){
vis[node] = true;
siz++;
for(auto it : vi[node]){
if(it == par)
continue;
if(vis[it])ok = true;
if(!vis[it])
dfs(it , node);
}
}
void bingo(){
cin>>n;
map<int,int>mp;
int mx = 0;
for(int i =0 ;i < n ; i++){
int u , v;
cin>>u>>v;
vi[u].push_back(v);
vi[v].push_back(u);
mx = max({mx , u , v});
exist[u] = exist[v] = true;
}
int out = 0;
for(int i = 1 ; i <= mx ; i++){
if(exist[i] && !vis[i] ){
ok = false;
siz = false ;
dfs(i);
if(ok)out += siz;
else out += siz - 1;
}
}
cout<<out;
}
int main() {
go
#ifdef LOCAL
freopen("input.in", "rt", stdin);
// freopen("output.in", "wt", stdout);
#endif
int t = 1;
//cin>>t;
while(t--){
bingo();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using edge = pair<int, int>;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
const int MX = 400000;
int n;
cin >> n;
vector<vector<edge>> g(MX, vector<edge>());
unordered_set<int> color;
rep(i, n) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].emplace_back(b, i);
g[b].emplace_back(a, i);
color.insert(a);
color.insert(b);
}
vector<bool> vis(MX, false);
auto is_tree = [&](auto self, int v, int i = -1) -> bool {
// @return
// true=木
// false=サイクル
bool ret = true;
vis[v] = true;
for (auto [u, j] : g[v]) {
if (j == i) continue; // 訪問に用いた辺を逆にたどっている
if (vis[u]) {
ret = false;
continue;
}
if (!self(self, u, j)) ret = false;
}
return ret;
};
int ans = (int)color.size();
for (auto v : color) {
if (vis[v]) continue;
if (is_tree(is_tree, v)) ans--;
}
cout << ans << "\n"s;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
///////////////////////////
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<
long long,
null_type,
less<long long>,// _equal for multiset
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
///////////////////////////
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define out(x) cout<<x<<endl,exit(0)
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second
const int N=2e5+8;
int h,w,m,cnt[N];
vector <int> v[N];
ordered_set con;
int32_t main(){
ios;
cin>>h>>w>>m;
int mn=1e9;
F(i,1,m){
int x,y;
cin>>x>>y;
if(x==1) mn=min(mn,y);
v[y].pb(x);
con.insert(x);
cnt[x]++;
}
//dummy obstacles
if(mn<1e9){
F(j,mn+1,w){
con.insert(1);
v[j].pb(1);
cnt[1]++;
}
}
if(v[1].size()){
int id=*min_element(all(v[1]));
F(i,id+1,h){
con.insert(i);
cnt[i]++;
}
}
//subtracting unreachable points
int ans=h*w;
R(j,w,1){
if(v[j].size()==0) continue;
int id=*min_element(all(v[j]));
ans-=con.size()-con.order_of_key(id);
for(auto it:v[j]){
cnt[it]--;
if(cnt[it]==0) con.erase(it);
}
}
cout<<ans<<endl;
}
| #pragma GCC optimize("O3")
//#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
#define re return
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define make_unique(x) sort(all(x)),x.resize(unique(all(x))-x.begin())
#define fi first
#define se second
#define ss second.second
#define sf second.first
#define ff first.first
#define fs first.second
#define sqrt(x) sqrt(abs(x))
#define mp make_pair
#define PI 3.14159265358979323846
#define E 2.71828182845904523536
#define er erase
#define in insert
#define fo(i,n) for((i)=0;(i)<(n);(i)++)
#define ro(i,n) for((i)=n-1;(i)>=0;(i)--)
#define fr(i,j,n) for((i)=(j);(i)<(n);(i)++)
#define rf(i,j,n) for((i)=((n)-1);(i)>=(j);(i)--)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
void eras(map<int,int> &m,int x)
{
m[x]--;
if (!m[x])
m.erase(x);
}
const int N=(int)3e5+100;
const int M=(int)2e6+100;
const int inf=(int)1e9+100;
#define filename ""
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//freopen(filename".in","r",stdin);
//freopen(filename".out","w",stdout);
//freopen("ans.txt","w",stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
srand(time(0));
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int a,b,w,i;
cin>>a>>b>>w;
w*=1000;
fr(i,1,1000001)
{
if (a*i<=w&&w<=b*i) break;
}
if (i==1000001)
{
cout<<"UNSATISFIABLE";
}
else
{
cout<<i<<' ';
while(a*(i+1)<=w&&w<=b*(i+1))
{
i++;
}
cout<<i;
}
}
|
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll N;
cin>>N;
vector<ll>arr(N,0);
ll maxi=0;
for(ll i=0;i<N;i++)
{
cin>>arr[i];
maxi=max(maxi,arr[i]);
}
ll i=0;
ll j=N-1;
ll ans=0;
vector<ll>mapper(maxi+100,0);
for(ll i=0;i<=maxi+1;i++)
{
mapper[i]=i;
}
vector<ll>sizes(maxi+100,1);
while(i<j)
{
if(arr[i]!=arr[j])
{
ll i1=arr[i];
ll i2=arr[j];
while(i1!=mapper[i1])
{
i1=mapper[i1];
}
while(i2!=mapper[i2])
{
i2=mapper[i2];
}
if(i1!=i2)
{
ans++;
if(sizes[i1]>=sizes[i2])
{
mapper[i2]=mapper[i1];
sizes[i1]+=sizes[i2];
}
else
{
mapper[i1]=mapper[i2];
sizes[i2]+=sizes[i1];
}
}
}
i++;
j--;
}
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define pb push_back
int main()
{
ll n,i,j,k;
cin>>n;
ll arr[n];
ll mx=0;
ll temp1=0,temp=0;
for(i=0;i<n;i++)
{
cin>>arr[i];
mx=max(mx,arr[i]);
temp1=temp1+arr[i];
temp=temp1+temp;
cout<<(i+1)*mx+temp<<"\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<ll> a(n), b(n);
ll first = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
first += a[i];
}
vector<int> p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](int i, int j){
return (-a[i]-a[i]-b[i]) < (-a[j]-a[j]-b[j]);
});
for (int i = 0; i < n; ++i) {
int x = p[i];
first += -a[x]-a[x]-b[x];
if (first < 0) {
cout << i+1 << '\n';
return 0;
}
}
return 0;
}
|
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL) ;cout.tie(NULL)
using namespace std;
#define deb(x) cout << #x << " " << x << endl;
#define ll long long int
#define pb push_back
#define endl "\n"
#define mp make_pair
#define ss second
#define ff first
void online_judge() {
#ifndef ONLINE_JUDGE
freopen("D:/C++ projects sublime/input/input.txt", "r", stdin);
freopen("D:/C++ projects sublime/input/output.txt", "w", stdout);
#endif
}
const int N = 2e5 + 5;
int small = INT_MIN;
int large = INT_MAX;
void solve() {
int n;
cin >> n;
vector<pair<double, double>> v;
for (int i = 0; i < n; i++) {
double t1, t2;
cin >> t1 >> t2;
v.push_back(make_pair(t1, t2));
}
int ans = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (v[j].first != v[i].first) {
double slope = (v[j].second - v[i].second) / (v[j].first - v[i].first);
if (slope >= -1 && slope <= 1) {
ans++;
}
}
}
}
cout << ans << endl;
}
int main() {
fast_cin();
online_judge();
ll t;
t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}
|
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#ifdef _LOCAL_
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define name(x) #x
#define dump(x) cout << name(x) << " = " << x << endl;
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define rrep(i,n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep2(i,s,n) for (int i = (s); i < (int)(n); i++)
#define For(i,x) for (auto &i : x)
#define len(x) ll(x.size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pcnt(bit) ll(__builtin_popcountll(bit))
#define gcd(a,b) __gcd(a, b)
#define lcm(a,b) a * b / __gcd(a, b)
#define roundup(x,y) (x + y - 1) / y
using ll = long long;
using pii = pair<int,int>;
const long double pi = acos(-1.0);
const ll INF = 4500000000000000000ll;
const int MAX = 1000010;
const int MOD1 = 1000000007;
const int MOD2 = 998244353;
const int di[4] = {1, 0, -1, 0};
const int dj[4] = {0, 1, 0, -1};
const int di2[8] = {0, 1, 0, -1, 1, 1,-1, -1};
const int dj2[8] = {1, 0,-1, 0, 1, -1, 1, -1};
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 (b < a) {a = b; return 1;} return 0;}
template <typename T> T bpow(T a, int n) {T r(1); while(n) {if (n & 1) r *= a; a *= a; n >>= 1;} return r;}
template <typename T> auto comp(vector<T> a) {sort(all(a)); a.erase(unique(all(a)),a.end()); return a;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T,U> &p) {os << p.first << " " << p.second; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T,U> &mp) {For(p,mp) {os << "[" << p << "]\n";} return os;}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {rep(i,len(v)) {if(i) os << " "; os << v[i];} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T> &st) {int c(0); For(x,st) {if (c) os << " "; os << x; c++;} return os;}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {rep(j,len(v)) i >> v[j]; return i;}
template <typename T, typename U> istream &operator>>(istream &i, pair<T,U> &p) {i >> p.first >> p.second; return i;}
void solve() {
int n; cin >> n;
string s; cin >> s;
string t = "";
int num = 10000000000;
rep(i,n/3+2) t += "110";
bool ok = false;
rep(i,3) if (t.substr(i,n) == s) ok = true;
if (!ok) cout << 0 << endl;
else {
int c = 0;
rep(i,n) if (s[i] == '0') c++;
if (s == "1") cout << 2 * num << endl;
else if (s[n-1] == '0') cout << num - c + 1 << endl;
else cout << num - c << endl;
}
return;
}
signed main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#ifdef _DEBUG
#define show(x) \
cerr << #x << " : "; \
showVal(x)
template <typename T>
void showVal(const T &a) { cerr << a << endl; }
template <typename T>
void showVal(const vector<T> &a) {
for (const T &v : a) cerr << v << " ";
cerr << endl;
}
template <typename T>
void showVal(const vector<vector<T>> &a) {
cerr << endl;
for (const vector<T> &v : a) showVal(v);
}
#else
#define show(x)
#endif
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<ll> s1(n + 1), s2(n + 1);
for (int i = 0; i < n; i++) {
s1[i + 1] = s1[i];
if (i % 2 == 0) s1[i + 1] += a[i];
s2[i + 1] = s2[i];
if (i % 2 == 1) s2[i + 1] += a[i];
}
show(s1);
show(s2);
map<ll, int> diff;
for (int i = 0; i < n + 1; i++) {
diff[s1[i] - s2[i]]++;
}
ll ans = 0;
for (int i = 0; i < n; i++) {
diff[s1[i] - s2[i]]--;
ans += diff[s1[i] - s2[i]];
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
struct BIT{
vector<LL> node;
int size;
BIT(int n){
size=n;
node.resize(size,0);
}
void add(int a,int x){
for(int i=a;i<size;i|=i+1) node[i]+=x;
}
LL sum(int a){
LL res=0;
for(int i=a-1;i>=0;i=(i&(i+1))-1){
res+=node[i];
}
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int h,w,m;
cin>>h>>w>>m;
vector<int> x(m),y(m);
for(int i=0;i<m;i++){
cin>>x[i]>>y[i];
x[i]--,y[i]--;
}
int clsh=h,clsw=w;
for(int i=0;i<m;i++){
if(x[i]==0) clsw=min(clsw,y[i]);
if(y[i]==0) clsh=min(clsh,x[i]);
}
vector<int> rih(clsh,w);
for(int i=0;i<m;i++){
if(x[i]<clsh) rih[x[i]]=min(rih[x[i]],y[i]);
}
vector<int> riw(clsw,h);
for(int i=0;i<m;i++){
if(y[i]<clsw) riw[y[i]]=min(riw[y[i]],x[i]);
}
LL res=0;
for(int i=0;i<clsh;i++) res+=rih[i];
for(int i=0;i<clsw;i++) res+=riw[i];
vector<vector<int>> dels(w+1);
for(int i=0;i<clsh;i++) dels[rih[i]].push_back(i);
BIT bt(h);
for(int i=0;i<clsh;i++) bt.add(i,1);
for(int i=0;i<clsw;i++){
for(int d:dels[i]) bt.add(d,-1);
res-=bt.sum(riw[i]);
}
cout<<res;
return 0;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#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 ll long long
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPI(i, a, b) for (ll i = b - 1; i >= a; i--)
#define i_os ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define INF (ll)1e18 + 100
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
// SOME BITMASK KNOWLEDGE
// 1)x & (x - 1):sets the last one bit of x to zero
// power of two exactly when x & (x − 1) = 0.
// 2)x & -x:sets all the one bits to zero, except last one bit
// 3)x | (x - 1):inverts all the bits after the last one bit
#define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define o_setll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
typedef tree<pair<ll, ll>,null_type,less<pair<ll, ll>>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_set1;
typedef tree<string,null_type,less<string>,rb_tree_tag,tree_order_statistics_node_update> indexed_set2;
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll power(ll a,ll n,ll c){
if(n == 0) return 1 % c;
ll s = power(a,n/2,c);
s = (s * s) % c;
if(n % 2) s = (s * a) % c;
return s;
}
int main()
{
i_os;
// freopen("output.txt","w",stdout);
ll h,w,m;
cin>>h>>w>>m;
vector<set<ll>> edges1,edges2;
edges1.resize(h,set<ll>());
edges2.resize(w,set<ll>());
// set<ll> edges1[h],edges2[w];
REP(i,0,m){
ll x,y;
cin>>x>>y;
edges1[x-1].insert(y-1);
edges2[y-1].insert(x-1);
}
vector<ll> col(w),row(h);
bool f = 0;
REP(i,0,w){
if(edges2[i].size()){
if(f){
col[i] = -1;
}
else {
col[i] = *edges2[i].begin() - 1;
if(col[i] == -1) f = 1;
}
}
else {
if(f) col[i] = -1;
else col[i] = h-1;
}
}
f = 0;
REP(i,0,h){
if(edges1[i].size()){
if(f){
row[i] = -1;
}
else {
row[i] = *edges1[i].begin() - 1;
if(row[i] == -1) f = 1;
}
}
else {
if(f) row[i] = -1;
else row[i] = w-1;
}
}
ll ans = 0;
REP(i,0,w){
ans += col[i] + 1;
}
vector<pair<ll,ll>> v;
REP(i,0,h){
if(row[i] >= 0){
v.push_back({row[i],i});
}
}
sort(v.begin(),v.end());
indexed_set s;
ll curr = -1,v1 = 1;
for(auto it:v){
while(curr < it.first){
curr++;
s.insert({col[curr],v1});
v1++;
}
ans += s.order_of_key({it.second,0});
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define P pair<ll,ll>
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
#define per(i,n) for(ll (i)=(n)-1;(i)>=0;--(i))
#define inf 9223372036854775807
#ifdef LOCAL
#define debug(x) cerr<<__LINE__<<": "<<#x<<"="<<x<<endl;
#define debugALL(x) cerr<<__LINE__<<": "<<#x<<"=";for(auto i=(x).begin();i!=--(x).end();i++)cerr<<*i<<" ";cerr<<*--(x).end()<<endl;
#else
#define debug(x) true
#define debugALL(x) true
#endif
using namespace std;
int main(){
double n;
cin>>n;
double x0,y0;
cin>>x0>>y0;
double xn,yn;
cin>>xn>>yn;
double X=(x0+xn)/2;
double Y=(y0+yn)/2;
double rad=2*3.14159265358979/n;
double c=cos(rad);
double s=sin(rad);
cout<<fixed<<setprecision(10);
cout<<X+(x0-X)*c-(y0-Y)*s<<" "<<Y+(y0-Y)*c+(x0-X)*s<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repb(i,a,b) for(int i=a;i>b;i--)
#define pb push_back
#define mp make_pair
#define all(A) A.begin(),A.end()
#define allr(A) A.rbegin(),A.rend()
#define precise(i) fixed << setprecision(i)
#define fi first
#define se second
#define err() cout<<"\n==================================\n";
#define errA(A) for(auto i:A) cout<<i<<" "; cout<<"\n";
#define err1(a) cout<<#a<<" "<<a<<"\n";
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<"\n";
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<"\n";
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<"\n";
const int logN = 20;
const int M = 1000000007;
const int INF = 1e12;
#define PI 3.14159265;
const int N = 200005;
#define Pii pair<int,int>
#define Vi vector<int>
#define Vpii vector<Pii>
void solve()
{
int n,s,d;
cin>>n>>s>>d;
Vpii a;
rep(c,0,n)
{
int x,y;
cin>>x>>y;
a.pb(mp(x,y));
}
rep(c,0,n)
{
if((a[c].fi >= s) || (a[c].se <= d))
{
continue;
}
cout<<"Yes";
return;
}
cout<<"No";
}
int32_t main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t=1;
// cin>>t;
while(t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T,class U> using P = pair<T,U>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// https://ei1333.github.io/luzhiled/
// combination with fermat theorem
// https://qiita.com/ofutonfuton/items/92b1a6f4a7775f00b6ae
// binary search
// https://qiita.com/drken/items/97e37dd6143e33a64c8c
// modint
// https://qiita.com/DaikiSuyama/items/2dcc49932f2489292e44
int main(){
ll n; cin >> n;
ll M = 998244353;
ll a, b, c;
a = 0;
b = 0;
c = 0;
ll ans = std::numeric_limits<ll>::max();
ll tmp = 1;
while(true) {
if (b == 0) {
a = n;
c = 0;
} else {
tmp *= 2;
a = n / tmp;
c = n - a * tmp;
if (c < 0) break;
}
ans = min(ans, a + b + c);
b++;
if (b > 63) break;
// cout << b << ": " << a + b + c << " = " << a * tmp + c << endl;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}
int main(){
ll N;
cin >> N;
ll b = 0;
ll B = 1;
ll ans = INF;
while(B <= N){
ll a = N / B;
ll c = N - a * B;
ans = min(ans, a + b + c);
b++;
B *= 2;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
// p^q
long long sisu(long long p, long long q){
long long ans = 1;
for (int i=0; i<q; i++){
ans *= p;
}
return ans;
}
//log(p^q)
int hantei(long long p, long long q){
return q*log(p);
}
int main(){
string X_;
long long M;
cin >> X_ >> M;
int S = X_.size();
vector<int> X(S);
for (int i = 0; i<S; i++){
X.at(i) = (int)X_.at(i) - (int)'0' ; //0番目に最上位桁が収納
}
int d = 1;
for (int i=0; i< S; i++){
if (d < X.at(i)){
d = X.at(i);
}
}
long long m;
long long D;
if (S != 1){
D = 10000 + exp(log((double)M / (double)X.at(0)) / (double)(S-1));
}else{
D = X.at(0);
}
long long count = D - d;
if (count > 0){
while (true){
m=0;
if ( log((double)X.at(0)) + (double)(S-1)*log((double)D) > log((double)M) ){
count--;
D--;
continue;
}
for (int i =0; i<S; i++){
m += X.at(i)*sisu(D,S-i-1);
}
if (M >= m)break;
count--;
D--;
}
}
if (count < 0){
count = 0;
}
if (S == 1){
if (X.at(0) <= M){
count = 1;
}else{
count = 0;
}
}
cout << count << endl;
} | /*
{
######################
# Author #
# Gary #
# 2020 #
######################
*/
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int n;
vector<int> v[2];
LL rest=0;
int a[100000+20],b[100000+20];
int main(){
cin>>n;
rb(i,1,n)
{
cin>>a[i];
rest+=a[i];
}
rb(i,1,n)
{
cin>>b[i];
v[i&1].PB(b[i]-a[i]);
}
sort(ALL(v[0]));
sort(ALL(v[1]));
LL MAX=0;
LL sum=0;
reverse(ALL(v[0]));
reverse(ALL(v[1]));
rep(i,n/2){
sum+=v[0][i]+v[1][i];
check_max(MAX,sum);
}
rest+=MAX;
cout<<rest<<endl;
return 0;
}
/** 程序框架:
*
*
*
*
**/
|
#include<bits/stdc++.h>
#define pairs pair<int,int>
#define loop(i,a,n) for(int i=a;i<n;i++)
#define m_p make_pair
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(v) v.begin(),v.end()
using namespace std;
void solve(){
ll a,b,c;
cin>>a>>b>>c;
string ans;
if(c==1){
ans="Aoki";
if(b>a)
{
cout<<ans;
}
else{
cout<<"Takahashi";
}
}
else
{
if(a>b){
cout<<"Takahashi";
}
else
cout<<"Aoki";
}
}
int main(){
int test=1;
//cin>>test;
while(test--){
solve();
cout<<endl;
}
}
| #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;
// #pragma GCC optimize("O2")
typedef long long int ll;
typedef long double ld;
typedef map <ll,ll> mm;
typedef vector <ll> mv;
typedef pair <ll,ll> mp;
typedef set <ll> ms;
typedef multiset <ll> mms;
typedef queue <ll> mq;
typedef deque <ll> mdq;
typedef stack <ll> mst;
typedef priority_queue <ll> mpq;
typedef priority_queue <ll, vector<ll>, greater<ll> > mmh;
typedef complex<double> cd;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define flush cout.flush();
#define all(v) v.begin(),v.end()
#define deb(x) cout << #x << " " << x << endl;
#define ff first
#define ss second
#define pb(v) push_back(v)
#define lb(v) lower_bound(v)
#define ub(v) upper_bound(v)
#define cn continue
#define forl(i,n) for(ll i=0;i<n;i++)
#define forlr(i,n) for(ll i=n-1;i>=0;i--)
#define rev(s) reverse(s.begin(),s.end())
void swap(ll &a,ll &b) {
ll tmp=a;
a=b;
b=tmp;
}
ll atoistring(string s) {
stringstream lol(s);
ll x;
lol>>x;
return x;
}
void pv(mv v) {
forl(i,v.size()) cout<<v[i]<<" ";
cout<<"\n";
}
void pa(ll *v, ll size) {
forl(i,size) cout<<v[i]<<" ";
cout<<"\n";
}
void removeDups(mv &v) {
sort(all(v));
mv::iterator it = unique(v.begin(), v.end());
v.resize(distance(v.begin(), it));
}
inline ld kthRoot(ld n, ll k) {
return pow(k, (1.0 / k) * (log(n) / log(k)));
}
ll power(ll x, ll y, ll p) {
ll res=1;
x=x%p;
while(y>0) {
if(y&1) res = (res*x)%p;
y=y>>1;
x=(x*x)%p;
}
return res;
}
void checkmod(ll &x, ll mod) {
if(x>=mod) x%=mod;
}
ll mod = 1e9 + 7;
set < string > sett1, sett2;
void thisIsMain() {
ll n;
cin>>n;
forl(i,n) {
string s;
cin>>s;
if(s[0] == '!') {
s.replace(0,1,"");
sett2.insert(s);
} else sett1.insert(s);
}
for(auto v : sett1) {
if(sett2.find(v) != sett2.end()) {
cout<<v<<"\n";
return ;
}
}
cout<<"satisfiable\n";
}
int main(void)
{
fastio
ll t = 1;
// cin>>t;
while(t--) thisIsMain();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
char a , b , c;
cin>> a >> b >> c;
if( a==b and b==c ){
cout<<"Won";
}
else{
cout<<"Lost";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define V vector
#define vcin(v) rep(i, v.size()) cin >> v[i]
int main(){
string s;
cin >>s;
puts(s[0]==s[1]&&s[0]==s[2]?"Won":"Lost");
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> Pair;
typedef tuple<int,int,int> tpl;
#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
int MSB(int n) {
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
return n - (n >> 1);
}
void solve(int N){
int msb = MSB(N), msb2 = msb<<1;
REP(i,N){
int a = 2*i % msb2, b = (2*i+1) % msb2;
if(a>=N) a %= msb;
if(b>=N) b %= msb;
cout << a+1 << " " << b+1 << en;
}
}
void Main(){
int N; cin >> N;
solve(N);
return;
}
int main(void){
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
int t=1; //cin>>t;
REP(_,t) Main();
return 0;
} | #include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int n;
int main()
{
cin>>n;
for(int i=0;i<n;i++)
cout<<(2*i)%n+1<<' '<<(2*i+1)%n+1<<endl;
return 0;
} |
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n, m;
cin >> k >> n >> m;
int a[k], b[k];
vector<pair<int,int>> v;
int rem = m;
for (int i = 0; i < k; i++) {
cin >> a[i];
b[i] = (m*a[i])/n;
rem -= b[i];
v.push_back({m*a[i]-b[i]*n,i});
}
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
for (int i = 0; i < rem; i++) b[v[i].second]++;
for (int i = 0; i < k; i++) cout << b[i] << ' ';
cout << '\n';
} | /**
template for testing/using the algorithms
import(copy-paste the methods ;v) algorithm/s and use here
**/
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
///------------------- algorithm function/s to be placed here start -------------------///
string s[3];
unsigned long long N[3][10+5], n[3];
vector<char> chars;
vector<bool> isFirst;
void insertNumWhereChar(long long num, char c){
for(int i=0;i<3;i++){
for(int j=0;j<s[i].size();j++){
if(s[i][j] == c) N[i][j] = num;
}
}
}
bool isN1PlusN2EqualsN3(){
for(int i=0;i<3;i++){
n[i] = 0;
for(int j=0;j<s[i].size();j++){
n[i] = n[i]*10 + N[i][j];
}
}
int len[3]; bool lenSame = true;
for(int i=0;i<3;i++){
long long x = n[i]; len[i] = 0;
while(x){
x/=10;
len[i]++;
}
lenSame = lenSame && (len[i]==s[i].size());
}
return (n[0]+n[1] == n[2]) && lenSame;
}
bool findd( int i, bool taken[10]){
if(i==chars.size()) {
return isN1PlusN2EqualsN3();
}
int start = 0;
if(isFirst[i]) start++;
bool found = false;
for(int ii=start;ii<10;ii++){
if(taken[ii]) continue;
taken[ii] = true;
insertNumWhereChar(ii, chars[i]);
found = found || findd(i+1, taken);
if(found) return true;
//removeNumWhereChar(chars[i]);
taken[ii] = false;
}
return found;
}
///-------------------- algorithm function/s to be placed here end --------------------///
void exec(int T){
// execute the algorithm
string s1[3]; cin>>s[0]>>s[1]>>s[2];
map<char, bool> uniq_chars;
for(int i=0;i<3;i++){
for(int j=0;j<s[i].size();j++) uniq_chars[s[i][j]] = (j==0);
}
if(uniq_chars.size()>10){
cout<<"UNSOLVABLE\n";
return;
}
chars.clear();
isFirst.clear();
for(auto i = uniq_chars.begin();i!=uniq_chars.end();i++){
chars.push_back(i->first);
isFirst.push_back(i->second);
}
bool taken[10];
for(int i=0;i<10;i++) taken[i] = false;
if(findd(0, taken)) cout<<n[0]<<"\n"<<n[1]<<"\n"<<n[2]<<"\n";
else cout<<"UNSOLVABLE\n";
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int t=1;
//cin>>t;
for(int T=1;T<=t;T++){
exec(T);
}
return 0;
}
|
//Littleboy123 Template 1.8
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
//pragmas
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef unsigned long long int ull;
typedef long double ld;
typedef unsigned long long ull;
const long double pi = acos(-1.0);
#define FOR(a,b,c) for(int (a)=(b);(a)<(c);++(a))
#define FORE(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define FORN(a,b,c) for(int (a)=(b);(a)>(c);--(a))
#define FORNE(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define TC(n) cout << "Case #" << (n) << ": "
#define DEBUG(x) cout << "DEBUG " << #x << ' ' << x << '\n';
#define MEM(a,b) memset((a), (b), sizeof((a)))
#define MAX(a,b) (a) = max((a), (b))
#define MIN(a,b) (a) = min((a), (b))
#define nl '\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void init(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void infile(){
freopen("dull.in", "r", stdin);
}
void outfile(){
freopen("input.txt", "w", stdout);
}
ll rp(ll a, ll b, ll mod){
ll res = 0;
while(b > 0){
if(b & 1)
res = (res + a) % mod;
res %= mod;
a = (a << 1)%mod;
b = b >> 1;
}
return res;
}
ll modex(ll a, ll b, ll mod){
if(b == 0)
return 1;
if(b == 1)
return a;
ll tmp = modex(a, b/2LL, mod);
tmp = rp(tmp, tmp, mod);
if(b & 1LL)
tmp = rp(tmp, a, mod);
return tmp;
}
ll ex(ll a, ll b){
if(b == 0)
return 1;
if(b == 1)
return a;
ll tmp = ex(a, b/2LL);
tmp = (tmp * tmp);
if(b & 1LL)
tmp = (tmp * a);
return tmp;
}
//End of template
int main(){
init();
ll n, x;
string s;
cin >> n >> x >> s;
FOR(i, 0, n){
if(s[i] == 'x'){
x = max(x-1, 0LL);
}
else{
x++;
}
}
cout << x << endl;
return 0;
}
| #include <bits/stdc++.h>
#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))
#ifdef LOCAL
#define log(val) std::cerr << std::setw(3) << __LINE__ << "| " << #val << " = " << (val) << std::endl
#define dbg(proc) proc
#else
#define log(val) 0
#define dbg(proc) 0
#endif
template <typename T>
std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){os << "{";if(!v.empty()){os << v[0];for(std::size_t i=1;i<v.size();++i){os << ", " << v[i];}}return os << "}";}
using ll = std::int64_t;
using P = std::tuple<int,int>;
template <typename T>
std::tuple<T, T, T> extgcd(T a, T b){
T s1 = 1, t1 = 0, s2 = 0, t2 = 1;
while(b != 0){
std::tie(s1, t1, s2, t2) = std::make_tuple(s2, t2, s1 - (a / b) * s2, t1 - (a / b) * t2);
std::tie(a, b) = std::make_tuple(b, a % b);
}
return std::make_tuple(s1, t1, a);
}
// 注意: a と mod が互いに素である必要がある
template <typename T>
T mod_inverse(T a, T mod){
T b;
std::tie(b, std::ignore, std::ignore) = extgcd(a, mod);
if(b < 0){b += mod;}
return b;
}
template <typename T>
struct Combinatorics{
std::vector<T> fact, inv_fact, pow;
Combinatorics(T max) : fact(max + 1), inv_fact(max + 1), pow(max + 1) {
fact[0] = 1;
pow[0] = 0;
for(T i=1;i<=max;++i){
int j = i, p = 0;
while(j % 3 == 0){
j /= 3;
p += 1;
}
fact[i] = j * fact[i - 1] % 3;
pow[i] = p + pow[i-1];
}
for(T i=0;i<=max;++i){
inv_fact[i] = mod_inverse(fact[i], 3);
}
}
T nCk(T n, T k){
if(n < k){return 0;}
if(pow[n] > pow[n-k] + pow[k]){
return 0;
}
return fact[n] * inv_fact[k] % 3 * inv_fact[n - k] % 3;
}
};
Combinatorics<int> comb(400100);
int N;
std::string S;
int A[400100];
std::string T = "BWR";
int main(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> N >> S;
for(int i=0;i<N;++i){
if(S[i] == 'B'){
A[i] = 0;
}else if(S[i] == 'W'){
A[i] = 1;
}else{
A[i] = 2;
}
}
int res = 0;
for(int i=0;i<N;++i){
// std::cout << A[i] << ", " << comb.nCk(N - 1, i) << std::endl;
res += A[i] * comb.nCk(N - 1, i);
}
res %= 3;
if(N % 2 == 0){
res = (3 - res) % 3;
}
std::cout << T[res] << std::endl;
}
|
#include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
const int N=1000010;
int pr[N];
void sev(){
pr[1]=1;
for(int i=2; i<N; ++i){
if(pr[i])
continue;
for(int j=i; j<N; j+=i){
pr[j]=i;
}
}
}
int main(){
ll l, r;
scanf("%lld%lld",&l,&r);
ll tmp, ans = 0;
ll tmp1, cnt, tmp2, cnt1;
sev();
for(ll i=2; i<=r; ++i){
tmp = (r/i*i - (l-1)/i*i)/i;
cnt = 0;
tmp1 = i;
while(tmp1!=1){
++cnt;
tmp2 = pr[tmp1];
cnt1 = 0;
while(tmp1%tmp2==0){
tmp1/=tmp2;
++cnt1;
}
if(cnt1>1)break;
}
if(cnt1>1)continue;
// cout<<i<<" "<<tmp<<" "<<cnt<<endl;
if(cnt%2){
ans += tmp*(tmp-1);
}
else{
ans -= tmp*(tmp-1);
}
}
// cout<<ans<<endl;
for(int i=max(l,2ll); i<=r; ++i){
tmp1 = (r)/i*i+i;
// cout<<i<<" "<<tmp1<<endl;
ans -= ((tmp1-i)/i-1)*2;
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
signed main(){
int A, B, i, ans = 0;
scanf("%d%d", &A, &B);
for(i = 1; i <= B; i++){
if(i * ((A + i - 1) / i) < i * (B / i)){
ans = i;
}
}
printf("%d\n", ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector< ll > vi;
typedef pair< ll, ll > pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for(ll i = a; i <= b; i++)
#define nl "\n"
const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e11;
const ll NINF = -1e11;
const ll epsilon = 1e-9;
ll fast_pow(ll base, ll exp, ll mod){
if(exp == 0)
return 1;
if(exp == 1)
return base%mod;
ll half = fast_pow(base, exp/2, mod);
if(exp % 2 == 0)
return (half%mod * half%mod)%mod;
else
return ((half%mod * half%mod)%mod * base%mod)%mod;
}
ll fast_pow_iter(ll base, ll exp, ll mod){
ll ans = 1;
while(exp > 0){
if(exp & 1)
ans = (ans * base)%mod;
base = (base * base)%mod;
exp >>= 1;
}
return ans;
}
ll gcd(ll a, ll b){
if(b == 0)
return a;
return gcd(b, a%b);
}
ll gcd_extended(ll a, ll b, ll& s, ll& t){
if(b == 0){
s = 1;
t = 0;
return a;
}
ll s1,t1;
ll d = gcd_extended(b, a%b, s1, t1);
s = t1;
t = s1 - t1 * (a/b);
return d;
}
ll sum = 0;
ll fact[10];
set< string > st;
void find_all(vector< ll > must, vector< ll > v){
if(must.size() == 4){
ll hash[10] = {0};
string s;
for(ll i=0; i<4; i++){
s.PB('0' + must[i]);
hash[must[i]]++;
}
sort(s.begin(), s.end());
if(st.find(s) != st.end()){
return;
}
st.insert(s);
ll value = 24;
for(ll i=0; i<10; i++){
value /= fact[hash[i]];
}
sum += value;
return;
}
for(ll i=0; i<v.size(); i++){
must.PB(v[i]);
find_all(must, v);
must.pop_back();
}
return;
}
void solve() {
// Code for problem here
string s;
cin >> s;
ll o=0,unsure=0;
vector< ll > v;
vector< ll > must;
for(ll i=0; i<s.length(); i++){
if(s[i] == 'o'){
o++;
v.PB(i);
must.PB(i);
}
else if(s[i] == '?'){
unsure++;
v.PB(i);
}
}
if(o > 4){
cout << 0 << endl;
return;
}
fact[0] = 1;
for(ll i=1; i<10; i++){
fact[i] = i * fact[i-1];
}
sum = 0;
find_all(must, v);
cout << sum << endl;
return;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int total_cases = 1;
// cin >> total_cases;
for (int t = 1; t <= total_cases; t++) {
// cout << "Case #" << t << ": ";
solve();
}
return 0;
} | #pragma region Template // clang-format off
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, m, n) for (int i = m; i < (int)(n); i++)
#define repe(i, m, n) for (int i = m; i <= (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define yesno {cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
using ll = long long;
using vvi = vector<vector<int>>;
using P = pair<int,int>;
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; }
#pragma endregion Template // clang-format on
int main() {
int n;
cin >> n;
map<int, ll> m;
rep(i, 0, n) {
int a;
cin >> a;
m[a]++;
}
ll ans = 0;
repe(i, -200, 200) {
repe(j, i, 200) { ans += m[i] * m[j] * (i - j) * (i - j); }
}
cout << ans << endl;
}
|
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
int main(){
int N;
cin >> N;
vector<pair<int, int>> S(0), T(0);
int a, b, c, d;
for(int i = 0; i < N; i++){
cin >> a >> b;
a *= N;
b *= N;
S.push_back(make_pair(a, b));
}
for(int i = 0; i < N; i++){
cin >> c >> d;
c *= N;
d *= N;
T.push_back(make_pair(c, d));
}
pair<int, int> gs{0, 0}, gt{0, 0};
for(int i = 0; i < N; i++){
gs.first += S.at(i).first;
gs.second += S.at(i).second;
gt.first += T.at(i).first;
gt.second += T.at(i).second;
}
gs.first /= N;
gs.second /= N;
gt.first /= N;
gt.second /= N;
for(int i = 0; i < N; i++){
S.at(i).first -= gs.first;
S.at(i).second -= gs.second;
T.at(i).first -= gt.first;
T.at(i).second -= gt.second;
}
for(int i=0; i<N; i++){
if(S.at(i).first != 0 || S.at(i).second != 0){
swap(S.at(i).first, S.at(0).first);
swap(S.at(i).second, S.at(0).second);
}
}
if(N == 1){
cout << "Yes" << endl;
return 0;
}
else if(N == 2){
int sdis = pow(S.at(1).first - S.at(0).first, 2) + pow(S.at(1).second - S.at(0).second, 2);
int tdis = pow(T.at(1).first - T.at(0).first, 2) + pow(T.at(1).second - T.at(0).second, 2);
if(sdis == tdis) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
const double eps = 1e-6;
string ans = "No";
for(int i = 0; i < N; i++){
double angle = atan2(T.at(i).second, T.at(i).first) - atan2(S.at(0).second, S.at(0).first);
int flag = 1;
for(int j = 0; j < N; j++){
double X = S.at(j).first * cos(angle) - S.at(j).second * sin(angle);
double Y = S.at(j).first * sin(angle) + S.at(j).second * cos(angle);
int tmpflag = 0;
for(int k = 0; k < N; k++){
if(abs(X - T.at(k).first) <= eps && abs(Y - T.at(k).second) <= eps) tmpflag = 1;
}
flag &= tmpflag;
}
if(flag) ans = "Yes";
}
cout << ans << endl;
} | #include <iostream>
#include <array>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define prl(a) cout << (a) << endl
#define allrange(a) a.begin(),a.end()
bool solve_translated(vector<pair<int,int>> &S,vector<pair<int,int>> &T){
int N = S.size();
int dx = S[0].first-T[0].first;
int dy = S[0].second-T[0].second;
bool flg = true;
reps(i,1,min(N,20)){
if(!((S[i].first==T[i].first+dx) && (S[i].second==T[i].second+dy))){flg = false; break;}
}
return flg;
}
vector<pair<int,int>> Pitagora_rot(vector<pair<int,int>> &S,int a, int b , int c){
int N = S.size();
vector<pair<int,int>> PS(N);
auto pt0 = S[0];
PS[0] = pt0;
bool flg = true;
int dx,dy,x,y;
reps(i,1,N){
dx = S[i].first - pt0.first;
dy = S[i].second - pt0.second;
x=dx*a-dy*b;
y=dx*b+dy*a;
if(((x%c)!=0) || ((y%c)!=0)) {flg = false; break;}
PS[i] = make_pair(x/c+pt0.first, y/c+pt0.second);
}
if(flg) return PS;
else return vector<pair<int,int>>();
}
int main(){
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int N;cin >> N;
vector<pair<int,int>> S(N),T(N);
rep(i,N)
{
int x,y;
cin >> x >> y;
S[i].first = x;S[i].second = y;
}
rep(i,N)
{
int x,y;
cin >> x >> y;
T[i].first = x;
T[i].second = y;
}
sort(allrange(T));
/*ピタゴラ三角形
5 12 13
8 15 17
3 4 5
*/
constexpr int tri[12][3] ={
{3,4,5},
{4,3,5},
{-3,4,5},
{-4,3,5},
{0,1,1},
{1,0,1},
{-1,0,1},
{0,-1,1},
{-3,-4,5},
{-4,-3,5},
{3,-4,5},
{4,-3,5}
};
/*
vector<vector<int>> tri(0);
tri.push_back(vector<int>({3,4,5}));
tri.push_back(vector<int>({4,3,5}));
tri.push_back(vector<int>({-3,4,5}));
tri.push_back(vector<int>({-4,3,5}));
tri.push_back(vector<int>({0,1,1}));
tri.push_back(vector<int>({1,0,1}));
tri.push_back(vector<int>({-1,0,1}));
tri.push_back(vector<int>({0,-1,1}));
tri.push_back(vector<int>({-3,-4,5}));
tri.push_back(vector<int>({-4,-3,5}));
tri.push_back(vector<int>({3,-4,5}));
tri.push_back(vector<int>({4,-3,5}));
// tri.push_back(vector<int>({-5, -12, 13}));
// tri.push_back(vector<int>({-12, -5, 13}));
// tri.push_back(vector<int>({-8,-15,17}));
// tri.push_back(vector<int>({-15,-8,17}));
// tri.push_back(vector<int>({5, 12, 13}));
// tri.push_back(vector<int>({12, 5, 13}));
// tri.push_back(vector<int>({8,15,17}));
// tri.push_back(vector<int>({15,8,17}));
// tri.push_back(vector<int>({-5, 12, 13}));
// tri.push_back(vector<int>({-12, 5, 13}));
// tri.push_back(vector<int>({-8,15,17}));
// tri.push_back(vector<int>({-15,8,17}));
// tri.push_back(vector<int>({5, -12, 13}));
// tri.push_back(vector<int>({12, -5, 13}));
// tri.push_back(vector<int>({8,-15,17}));
// tri.push_back(vector<int>({15,-8,17}));
*/
bool flg;
if(N==2){
auto sx = S[0].first-S[1].first;
auto sy = S[0].second-S[1].second;
auto tx = T[0].first-T[1].first;
auto ty = T[0].second-T[1].second;
flg= (sx*sx+sy*sy==tx*tx+ty*ty);
} else {
rep(j,12){
auto S2 = Pitagora_rot(S,tri[j][0],tri[j][1],tri[j][2]);
if(S2.empty()) continue;
sort(allrange(S2));
flg = solve_translated(S2,T);
if(flg) break;
}
}
if(flg) prl("Yes"); else prl("No");
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,a,b) for(int i=a;i<=b;i++)
inline ll rd() {
ll 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;
}
#define d rd()
ll n;
struct node{
ll x,y,sum;
}a[200010];
bool cmp(node a,node b){
return a.sum+a.x>b.sum+b.x;
}
ll del,cnt;
int main(){
n=d;
f(i,1,n)a[i].x=d,a[i].y=d,a[i].sum=a[i].x+a[i].y;
sort(a+1,a+n+1,cmp);
f(i,1,n)del+=a[i].x;
f(i,1,n){
del-=a[i].x,cnt+=a[i].sum;
if(del<cnt){
cout<<i;
return 0;
}
}
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define ll long long
#define F(type, i, a, b, incr) for (type i = a; i <= (type)(b); i += (type)(incr))
#define RF(type, i, a, b, decr) for (type i = a; i >= (type)(b); i -= (type)(decr))
#define sz(a) sizeof(a)
#define deb(a) cerr << " [" << #a << "->" << a << "] "
#define next_line cerr << '\n'
#define all(a) a.begin(), a.end()
#define iter(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define setbits(x) __builtin_popcountll(x)
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
/*
1. If you cannot approach a problem
directly in the way it is proposed
then try to think from backwards
2. Have you checked the implementation
before starting to write the code?
3. Have you read the question carefully ?
4. Keep the names of variable descriptive.
5. Check the types and range of the values properly
6. Erase a iterator in the set or map only after using its value.
7. 1 bitset of 1e9 size only takes 256 mb.
8. Try to use global variables in case of recursion
9. Short and precise exectutes faster
*/
void solve()
{
int n, su = 0;
cin >> n;
vector<pii> vo(n);
F(int, i, 0, n - 1, 1){
cin >> vo[i].first >> vo[i].second;
su += vo[i].first;
}
sort(all(vo), [](pii a, pii b){
return make_pair(2LL*a.first + a.second, a.first) < make_pair(2LL*b.first + b.second, b.first);
});
int ans = 0, sc = 0;
RF(int, i, n - 1, 0, 1){
if(sc > su){
break;
}else{
ans ++;
sc += (vo[i].first + vo[i].second);
su -= (vo[i].first);
}
}
cout << ans << '\n';
}
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);
// freopen("Debug.txt", "w", stderr);
#else
#endif
// cout << fixed << setprecision(10);
// int _t;
// cin>>_t;
// while(_t --)
solve();
}
|
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
putchar('\n');
}
bool yes;
LL X[100011], Y[100011];
LL solve(int len) {
if (len <= 1) return 0;
// rprintf("%lld", X, X+len);
// rprintf("%lld", Y, Y+len);
LL ret = len - 1;
int left = 0;
for (int i=1; i<len; ) {
int j = i;
while (j < len && Y[i] == Y[j]) j++;
while (X[left] <= Y[i]) left++;
if (left && X[left-1] == Y[i]) {
ret += i - left;
} else {
yes = false;
return 0;
}
i = j;
}
return ret;
}
int N;
LL L;
LL A[100011];
LL B[100011];
void MAIN() {
scanf("%d%lld", &N, &L);
A[0] = B[0] = 0;
REP (i, N) scanf("%lld", A+i+1);
REP (i, N) scanf("%lld", B+i+1);
N += 2;
A[N-1] = B[N-1] = L+1;
REP (i, N) {
A[i] -= i;
B[i] -= i;
}
yes = true;
LL ans = 0;
for (int i=0; i<N && yes; ) {
int j = i + 1;
while (B[j] < A[j]) j++;
int len = j - i;
REP (a, len) {
X[a] = A[i+a] - A[i];
Y[a] = B[i+a] - B[i];
}
ans += solve(len);
int k = j;
while (A[k] < B[k]) k++;
if (B[k] < A[k]) {
yes = false;
break;
}
len = k - j + 1;
REP (a, len) {
X[a] = A[k] - A[k-a];
Y[a] = B[k] - B[k-a];
}
ans += solve(len);
i = k;
}
if (yes) {
printf("%lld\n", ans);
} else {
puts("-1");
}
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| #define _GIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
// 解説を見て
int main(void) {
string x;
cin >> x;
ll m;
cin >> m;
if (x.size() == 1) {
if (stoi(x) <= m) cout << 1 << endl;
else cout << 0 << endl;
return 0;
}
int d = 0;
for (char c : x) d = max(d, int(c - '0'));
ll ac = d, wa = m + 1;
while (wa - ac > 1) {
ll wj = (ac + wa) / 2;
ll v = 0;
for (char c : x) {
if (v > m / wj) v = m + 1;
else v = v * wj + (c - '0');
}
if (v <= m) ac = wj;
else wa = wj;
}
cout << ac - d << endl;
return 0;
} |
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007; // 998244353;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const ld PI = acos((ld)-1);
const vector<ll> dx={-1,0,1,0},dy={0,1,0,-1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}
inline void yes() { cout << "Yes" << ENDL; }
inline void no() { cout << "No" << ENDL; }
template<typename T,typename Y> inline T mpow(T a, Y n) {
T res = 1;
for(;n;n>>=1) {
if (n & 1) res = res * a;
a = a * a;
}
return res;
}
template <typename T> V<T> prefix_sum(const V<T>& v) {
int n = v.size();
V<T> ret(n + 1);
rep(i, 0, n) ret[i + 1] = ret[i] + v[i];
return ret;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
for(auto&& x: vec) is >> x;
return is;
}
template<typename T,typename Y>
ostream& operator<<(ostream& os,const pair<T,Y>& p){
return os<<"{"<<p.fs<<","<<p.sc<<"}";
}
template<typename T> ostream& operator<<(ostream& os,const V<T>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
template<typename ...Args>
void debug(Args&... args){
for(auto const& x:{args...}){
cerr<<x<<' ';
}
cerr<<ENDL;
}
template <long long MOD = mod> // MODを適宜設定すること
struct Mint {
using M = Mint;
ll a;
Mint(ll x = 0) {
a = x % MOD;
if(a < 0) a += MOD;
} // Mintで
M pow(ll n) {
M b = *this, r = 1 % MOD;
while(n) {
if(n & 1) r *= b;
b *= b;
n >>= 1;
}
return r;
}
M inv() { return pow(MOD - 2); }
M& operator+=(M r) {
a += r.a;
if(a >= MOD) a -= MOD;
return *this;
}
M& operator-=(M r) {
a += MOD - r.a;
if(a >= MOD) a -= MOD;
return *this;
}
M& operator*=(M r) {
a = 1LL * a * r.a % MOD;
return *this;
}
M& operator/=(M r) { return (*this) *= r.inv(); }
M operator+(M r) const { return M(a) += r; };
M operator-(M r) const { return M(a) -= r; };
M operator*(M r) const { return M(a) *= r; };
M operator/(M r) const { return M(a) /= r; };
M operator-() const { return M() - *this; }
friend ostream& operator<<(ostream& os, const M& r) { return os << r.a; }
};
using M = Mint<mod>;
/*
using M = Mint<mod>;
M xとint iの演算はx.a+=i またはx+=M{i}
M(x).pow(exp)
*/
const int MAX=100010;
M fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i;
inv[i] = M{mod}-inv[mod%i] * (mod / i);
finv[i] = finv[i - 1] * inv[i];
}
}
signed main(){
cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false);
cout<<fixed<<setprecision(20);
COMinit();
ll n,m;cin>>n>>m;
V<ll> a(n);cin>>a;
ll asum=0;
M ans=1;
rep(i,0,n){
asum+=a[i];
}
rep(i,1,n+asum+1){
ans/=i;
ans*=(m+n+1-i);
}
cout<<ans<<ENDL;
}
//! ( . _ . ) !
//CHECK overflow,vector_size,what to output?
//any other simpler approach?
//list all conditions, try mathematical and graphic observation | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define pll pair<long long,long long>
#define pdd pair<long double,long double>
#define vll vector<ll>
#define rep(i,j,n) for(int i=j;i<n;i++)
#define mp make_pair
#define pb push_back
#define pf push_front
#define inf 1e17
#define N 200010
priority_queue<pll,vector<pll> >pq;
priority_queue<ll,vector<ll>,greater<ll> >pq1;
ll result(ll a, ll b, ll p){
ll ans=1;
if(b==(-1))
b=p-2;
while(b){
if(b&1){
ans=(ans*a)%p;
}
a=(a*a)%p;
b>>=1;
}
return ans;
}
ll T = 1,n,m,k;
int main(){
if(fopen("input.txt", "r"))
freopen("input.txt", "r", stdin),
freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
ll d = 2*n+100;
cout << max(0ll, d-m) << "";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;
cin >> x;
if(x >= 0) cout << x << endl;
else cout << "0" << endl;
return 0;
} | #include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using namespace __gnu_pbds;
//using namespace boost::multiprecision;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef long long ll;
typedef vector<ll> VI;
typedef vector<vector<ll>> VVI;
typedef pair<ll, ll> PII;
typedef vector<PII> VPII;
typedef set<ll> SI;
typedef map<string, ll> MSI;
typedef map<ll, ll> MII;
typedef pair<ll, PII> PIII;
#define flash ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define MP make_pair
#define PB push_back
#define FF first
#define SS second
#define MOD 1000000007
#define test ll t;cin>>t;while(t--)
#define REP(i,a,b) for (int i = a; i <= b; i++)
void solve()
{
ll x;
cin >> x;
if(x<0)
cout << 0 << "\n";
else cout << x << "\n";
return;
}
int main()
{
flash;
// test
// {
// solve();
// }
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < (int)(b); i++)
#define all(v) v.begin(), v.end()
const int MOD = 1e9 + 7;
const int INF = 1e9;
//'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122
class UnionFind {
public:
vector<int> par;
int num;
UnionFind(int n) : par(n, -1), num(n) {};
int root(int x) {
if (par[x] < 0) return x;
return par[x] = root(par[x]);
}
int size(int x) {
return -par[root(x)];
}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (size(x) < size(y)) swap(x, y);
par[x] += par[y];
par[y] = x;
num--;
return true;
}
bool same(int x, int y) {
return root(x) == root(y);
}
};
int main() {
int n, m;
cin >> n >> m;
int a[n], b[n];
rep(i, 0, n) cin >> a[i];
rep(i, 0, n) cin >> b[i];
UnionFind uf(n);
rep(i, 0, m) {
int c, d;
cin >> c >> d;
c--;
d--;
if (!uf.same(c, d)) uf.unite(c, d);
}
vector<ll> x(n, 0), y(n, 0);
rep(i, 0, n) {
x[uf.root(i)] += a[i];
y[uf.root(i)] += b[i];
}
if (x == y) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
| #include <bits/stdc++.h>
#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 rep0(i, a) for (int i = 0, i##end = a; i < i##end; ++i)
#define per0(i, a) for (int i = (int)a-1; ~i; --i)
#define chkmax(a, b) a = std::max(a, b)
#define chkmin(a, b) a = std::min(a, b)
#define x first
#define y second
#define pb push_back
#define mp std::make_pair
#define enter putchar('\n')
typedef long long ll;
int read() {
int w = 0, f = 1; char c;
while (!isdigit(c = getchar())) c == '-' && (f = -1);
while (isdigit(c)) w = w*10+(c^48), c = getchar();
return w * f;
}
int a[4];
int main() {
rep0(i, 4) a[i] = read();
std::sort(a, a+4);
if (a[0] + a[1] + a[2] == a[3] || a[0] + a[2] == a[1] + a[3] || a[0] + a[3] == a[1] + a[2]) printf("Yes");
else printf("No");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int,int> pp;
bool com(pp x,pp y){
if(x.ff==y.ff) return x.ss<y.ss;
return x.ff<y.ff;
}
ll power(ll x,ll y){
ll prod=1;
while(y){
if(y&1) prod=(prod*x)%mod;
x=(x*x)%mod;
y/=2;
}
return prod;
}
const int N=1e5+9;
void solve(){
int n,t,s,k;
cin>>n>>t>>s>>k;
if(k<n*t||k>s*n) puts("Yes");
else puts("No");
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while(t--)
solve();
return 0;
}
/*
*/ | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define ub upper_bound
#define lb lower_bound
#define all(v) ((v).begin()),((v).end())
#define allr(v) ((v).rbegin()),((v).rend())
#define ff first
#define ss second
#define mp make_pair
#define pll pair<ll,ll>
#define fo(i,n) for(ll i=0;i<n;i++)
#define foa(i,s,e) for(ll i=(s);i<=e;i++)
#define fod(i,s,e) for(ll i= (s);i>=(e);i--)
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
#define sz(x) (ll)(x.size())
#define ANS cout<<ans<<'\n'
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
const ld pi = 3.14159265358979323846;
ll MOD = 1e9 + 7;
//ll MOD = 998244353;
const char nl = '\n';
const ll inf = 1e15;
#define fill(a,b) memset(a, b, sizeof(a))
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) fo(i, n){fo(j, m)cout<<dp[i][j]<<" ";cout<<"\n";}
ll nxt(){ll x; cin >> x; return x;}
ll mul(ll x,ll y){
return (1ll* (x%MOD)*(y%MOD));
}
ll modpow(ll x,ll y){ll z=1;while(y>0){if(y%2)z=mul(z,x);x =mul(x,x) ;y/=2;}return z;}
ll power(ll x,ll y){ll z=1;while(y>0){if(y%2)z=z*x;x =x*x ;y/=2;}return z;}
ll gcd(ll a,ll b){if(a<b) return gcd(b,a);if(b==0) return a;return gcd(b,a%b);}
ll min(ll a,ll b){if(a<b)return a;return b;}
ll max(ll a,ll b){if(a>b)return a;return b;}
ll sq(ll a){
ll ans = (1ll*a*a);
return ans;
}
void solve(){
ll v, t, s, d;
cin >> v >> t >> s >> d;
ll x = v*t;
ll y = v*s;
if(d < x || d > y){
Yes;
}
else No;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("inputf.txt" , "r" , stdin) ;
freopen("outputf.txt" , "w" , stdout) ;
freopen("error.txt" , "w" , stderr) ;
#endif
ll TC = 1;
//cin>>TC;
fo(TT, TC){
// Google,FB cout << "Case #"<< TT + 1 << ": ";
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}
|
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#define IN
#define LL long long
#define P(t1) ((t1 - 1) / blk + 1)
using namespace std;
const int MAXN = 2e3 + 5;
int T;
LL A, B, C;
int main() {
int i,j,k;
scanf ("%d", &T);
while (T--) {
scanf ("%lld%lld", &A, &B), C = max (B - 2ll * A + 1, 0ll);
printf ("%lld\n", (C + 1) * C / 2);
}
return 0;
} | #include <bits/stdc++.h>
#pragma region my_template
struct Rep {
struct I {
int i;
void operator++() { ++i; }
int operator*() const { return i; }
bool operator!=(I o) const { return i < *o; }
};
const int l_, r_;
Rep(int l, int r) : l_(l), r_(r) {}
Rep(int n) : Rep(0, n) {}
I begin() const { return {l_}; }
I end() const { return {r_}; }
};
struct Per {
struct I {
int i;
void operator++() { --i; }
int operator*() const { return i; }
bool operator!=(I o) const { return i > *o; }
};
const int l_, r_;
Per(int l, int r) : l_(l), r_(r) {}
Per(int n) : Per(0, n) {}
I begin() const { return {r_ - 1}; }
I end() const { return {l_ - 1}; }
};
template <class F>
struct Fix : private F {
Fix(F f) : F(f) {}
template <class... Args>
decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
template <class T = int>
T scan() {
T res;
std::cin >> res;
return res;
}
template <class T, class U = T>
bool chmin(T& a, U&& b) {
return b < a ? a = std::forward<U>(b), true : false;
}
template <class T, class U = T>
bool chmax(T& a, U&& b) {
return a < b ? a = std::forward<U>(b), true : false;
}
#ifndef LOCAL
#define DUMP(...) void(0)
template <int OnlineJudge, int Local>
constexpr int OjLocal = OnlineJudge;
#endif
using namespace std;
#define ALL(c) begin(c), end(c)
#pragma endregion
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cout << fixed << setprecision(20);
int n = scan();
vector<int64_t> a(n), b(n);
generate(ALL(a), scan<>);
generate(ALL(b), scan<>);
int64_t ans = 0, mx = 0;
for (int i : Rep(n)) {
chmax(mx, a[i]);
chmax(ans, mx * b[i]);
cout << ans << '\n';
}
}
|
#include <bits/stdc++.h>
#include <math.h>
#define ALL(a) a.begin(), a.end()
#define rep(i, K, N) for(long long int i = K; i < N; i++)
#define ll long long int
#define PP 1000000007
using namespace std;
int main(){
string S;cin >> S;
ll N = S.size();
bool C;
map<char, ll> M;
vector<ll> V(124, -1);
if(1 == N)V[0] = 8;
if(2 == N)V[0] = 16;
if(3 <= N)V[0] = 104;
rep(i, 0, 123){
if(8 + V[i] > 1000)break;
V[i + 1] = 8 + V[i];
}
for(auto x : S )M[x]++;
for(auto x : V ){
string T = to_string(x);
map<char, ll> m;
C = 1;//cout << T << endl;
for(auto y : T )m[y]++;
for(auto z : m ){
if(z.second > M[z.first]){C = 0;break;}
}
if(C == 1)break;
m.clear();
}
if(C == 1)cout << "Yes";
else cout << "No";
} | #include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
string S;
cin >> S;
map<char, int> digits;
for (int i = 0; i < S.length(); i++) {
digits[S[i]]++;
}
// for (auto d : digits) {
// cout << d.first << " " << d.second << endl;
// }
if (S.length() >= 3) {
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
for (int k = 1; k < 10; k++) {
map<char, int> tmp = digits;
if ((i * 100 + j * 10 + k) % 8 == 0) {
if (tmp['0' + i] != 0)
tmp['0' + i]--;
else
continue;
if (tmp['0' + j] != 0)
tmp['0' + j]--;
else
continue;
if (tmp['0' + k] != 0)
tmp['0' + k]--;
else
continue;
cout << "Yes" << endl;
return 0;
}
}
}
}
} else if (S.length() == 1) {
if (S[0] == '8') {
cout << "Yes" << endl;
return 0;
} else {
cout << "No" << endl;
return 0;
}
} else {
if (((S[0] - '0') * 10 + (S[1] - '0')) % 8 == 0) {
cout << "Yes" << endl;
return 0;
}
if (((S[1] - '0') * 10 + (S[0] - '0')) % 8 == 0) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
struct fastio{fastio(){cin.tie(nullptr);ios_base::sync_with_stdio(false);std::cout<<std::fixed<<setprecision(10);}}oitsaf;
#define rep(i,n) for(int i=0;i<int(n);++i)
template<class T>std::vector<T>vec(size_t a){return std::vector<T>(a);}
template<class T, class... Ts>auto vec(size_t a,Ts... ts){return std::vector<decltype(vec<T>(ts...))>(a,vec<T>(ts...));}
template<class T>std::istream&operator>>(std::istream&is,std::vector<T>&v){for(auto &elemnt:v)is>>elemnt;return is;}
template<class T>std::ostream&operator<<(std::ostream&os,std::vector<T>const&v){for(auto const& vi:v)os<<vi<<" ";return os;}
int main() {
int n, k;
cin >> n >> k;
auto dist = vec<int>(n, n);
cin >> dist;
vector<int> idx(n - 1);
iota(begin(idx), end(idx), 1);
int ans = 0;
do {
int tmp = dist[0][idx[0]];
rep(i, n - 2) tmp += dist[idx[i]][idx[i + 1]];
tmp += dist[idx[n - 2]][0];
ans += tmp==k;
} while (next_permutation(begin(idx), end(idx)));
cout << (ans) << endl;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll g[20][20];
ll a[20],n,k,res;
bool vis[20];
void dfs(int t)
{
if(t > n)
{
int ans = 0;
for(int i = 2;i <= n;i++)
{
ans += g[a[i-1]][a[i]];
}
ans += g[a[n]][1];
if(ans == k) res++;
}
else{
for(ll i = 2;i <= n;i++)
{
if(!vis[i])
{
vis[i]=true;
a[t]=i;
dfs(t+1);
vis[i]=false;
a[t]=0;
}
}
}
}
int main()
{
scanf("%lld%lld",&n,&k);
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
scanf("%lld",&g[i][j]);
}
}
a[1]=1;
vis[1] = true;
dfs(2);
printf("%lld",res);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using s64 = signed long long;
using u64 = unsigned long long;
using f128 = long double;
#define LOGVAR(var) cout << #var << ": " << var << endl
int main()
{
vector<s64> a(3);
cin >> a[0] >> a[1] >> a[2];
sort(a.begin(), a.end());
if(a[2] - a[1] == a[1] - a[0])
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return 0;
}
| #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define sz(v) (int)v.size()
#define ps(y) cout << fixed << setprecision(y)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define trav(x, v) for(auto &x: v)
#define w(t) int t; cin >> t; while(t--)
#define rep0(i, n) for(int i = 0; i < n; i++)
#define rrep0(i, n) for(int i = n - 1; i >= 0; i--)
#define rep1(i, n) for(int i = 1; i <= n; i++)
#define rrep1(i, n) for(int i = n; i > 0; i--)
#define inp(arr, n) rep0(i, n) cin >> arr[i];
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = a; i >= b; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<ll, ll> mii;
typedef map<char, ll> mci;
typedef priority_queue<ll> pq_mx;
typedef priority_queue<ll, vi, greater<>> pq_mn;
typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
* find_by_order(i) -> returns an iterator to the element at ith position (0 based)
* order_of_key(i) -> returns the position of element i (0 based)
*/
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const ll inf = 1e18;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void fio() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
}
int main() {
fio();
vi a(3);
inp(a, 3)
sort(all(a));
if(a[1] - a[0] == a[2] - a[1]) cout<<"Yes";
else cout<<"No";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
stack<pair<int,int>> st;
st.push({v[0],0});
vector<int> right(n,-1);
vector<int> left(n,-1);
for(int i=1;i<n;i++){
while(!st.empty() && st.top().first>v[i]){
right[st.top().second]=i;
st.pop();
}
st.push({v[i],i});
}
while(!st.empty()){
st.pop();
}
st.push({v[n-1],n-1});
for(int i=n-2;i>=0;i--){
while(!st.empty() && st.top().first>v[i]){
left[st.top().second]=i;
st.pop();
}
st.push({v[i],i});
}
int ans=0;
for(int i=0;i<n;i++){
if(left[i]==-1 && right[i]==-1){
ans=max(ans,v[i]*n);
}
else if(left[i]==-1 && right[i]!=-1){
ans=max(ans,v[i]*right[i]);
}
else if(right[i]==-1 && left[i]!=-1){
ans=max(ans,v[i]*(n-left[i]-1));
}
else{
ans=max(ans,v[i]*(right[i]-left[i]-1));
}
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int cnt[10];
int calc_points(string s)
{
//cout << "b";
int res = 0;
vector<int> c(10,0);
rep(i,5)
{
c[s[i]-'0']++;
}
for (int i = 1; i <= 9; ++i)
{
int t = i;
rep(j,c[i])
{
t *= 10;
}
res += t;
}
//cout << "c";
return res;
}
int main()
{
int K;
cin >> K;
rep(i,9)
{
cnt[i+1] = K;
}
string S,T;
cin >> S >> T;
rep(i,4)
{
cnt[S[i]-'0']--;
cnt[T[i]-'0']--;
}
double ans = 0.0;
for (int i = 1; i <= 9; ++i)
{
if (cnt[i] < 1) continue;
char new_c = '0'+i;
S[4] = new_c;
int S_point = calc_points(S);
for (int j = 1; j <= 9; ++j)
{
if ((i == j && cnt[j] < 2) || (i != j && cnt[j] < 1)) continue;
char new_c2 = '0'+j;
T[4] = new_c2;
int T_point = calc_points(T);
if (S_point > T_point)
{
if (i != j) ans += double(cnt[i])/double(9*K-8) * double(cnt[j])/double(9*K-9);
else ans += double(cnt[i])/double(9*K-8) * double(cnt[j]-1)/double(9*K-9);
}
}
//cout << "a";
}
cout << setprecision(10) << ans << endl;
return 0;
} |
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
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; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
//#include <unistd.h>
void solve(){
int n;
cin>>n;
V<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
a[i]--;
}
int t=0;
V<int> ans;
V<bool> ok(n,false);
while(1){
bool ok=true;
for(int j=0;j<n;j++){
if(a[j]<j){
ok=false;
if(j%2==t){
if(t==0){
swap(a[0],a[1]);
ans.emplace_back(1);
}else{
swap(a[1],a[2]);
ans.emplace_back(2);
}
t^=1;
}
while(a[j]<j){
swap(a[j-1],a[j]);
ans.emplace_back(j);
j--;
t^=1;
}
break;
}
}
if(ok)break;
}
// print(a);
cout<<ans.size()<<"\n";
print(ans);
}
int main(){
int t;
cin>>t;
while(t--)solve();
} | #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, a, N) for (int i = (a); i< N; i++)
typedef pair<int, int> pii;
int N;
int main() {
cin >> N;
vector<int> a(N);
vector<int> b(N);
vector<int> c(N);
int cnt[100001];
memset(cnt, 0, sizeof(cnt));
rep (i, 1, N+1) cin >> a[i];
rep (i, 1, N+1) cin >> b[i];
rep (i, 1, N+1) cin >> c[i], cnt[b[c[i]]]++;
long long ans = 0;
rep (i, 1, N+1) {
ans += cnt[a[i]];
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int MAXN=505;
int n,p[MAXN];
int opt[MAXN*MAXN],m;
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch))(ch=='-')&&(f=-f),ch=getchar();
while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline int find(int x){
for(int i=1;;++i) if(p[i]==x) return i;
}
inline bool do_til(int now,int tar,int &oe){
int i;bool flag=0;
if(now==tar) return 1;
if(((now-1)&1)==oe&&now>tar) for(i=now,flag=1;i>tar;--i) swap(p[i-1],p[i]),opt[++m]=i-1,oe^=1;
if(((now)&1)==oe&&now<tar)for(i=now,flag=1;i<tar;++i) swap(p[i],p[i+1]),opt[++m]=i,oe^=1;
return flag;
}
inline bool randswap(int now,int &oe){
if(oe){
if(now&1) swap(p[now+2],p[now+3]),opt[++m]=now+2;
else swap(p[now+1],p[now+2]),opt[++m]=now+1;
}else{
if(now&1) swap(p[now+1],p[now+2]),opt[++m]=now+1;
else swap(p[now+2],p[now+3]),opt[++m]=now+2;
}
oe^=1;
return 1;
}
inline void solve(){
int i,oe=1;
n=read();m=0;
for(i=1;i<=n;++i) p[i]=read();
// printf("%d\n",n);
// for(i=1;i<=n;++i) printf("%d ",p[i]);
for(i=1;i<=n-2;++i){
while(!do_til(find(i),i,oe))
randswap(i-1,oe);
}
if(p[n-1]==n-1);
else if(((n-2)&1)==oe){
opt[++m]=n-2;
opt[++m]=n-1;
opt[++m]=n-2;
opt[++m]=n-1;
opt[++m]=n-2;
}else opt[++m]=n-1;
// puts("");
printf("%d\n",m);
for(i=1;i<=m;++i) printf("%d ",opt[i]);puts("");
}
int main(){
// freopen("data.in","r",stdin);
// freopen("1.out","w",stdout);
for(int T=read();T;--T)
solve();
return 0;
}
/*
2
5
4 1 2 3 5
10
1 4 3 2 5 6 9 7 8 10
*/ | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0};
long double eps = 1e-9;
long double pi = acos(-1);
template< int mod = 1000000007 >
struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t 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 ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int) (1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
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 inverse() 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(int64_t n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt< mod >(t);
return (is);
}
static int get_mod() { return mod; }
};
// const int mod = 1000000007;
const int mod = 998244353;
using mint = ModInt< mod >;
const int MAX = 300005;
mint fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i;
}
finv[MAX-1] = fac[MAX-1].inverse();
for(int i=MAX-2;i>=1;i--){
finv[i] = finv[i+1]*(i+1);
inv[i+1] = fac[i]*finv[i+1];
}
}
// 二項係数計算
mint COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k]);
}
mint ans = 0;
ll n,m;
unordered_map<int,int> mp[200020];
vector<int> prime;
bool not_prime[MAX];
void era(){
not_prime[0]=not_prime[1]=1;
for(int i=2;i*i<MAX;i++){
if(not_prime[i])continue;
for(int j=i+i;j<MAX;j+=i){
not_prime[j]=1;
}
}
for(int i=2;i<MAX;i++)if(not_prime[i] == false)prime.push_back(i);
}
mint dfs(ll now,int cnt,ll ma,mint p){
// if(mp[now][cnt]){
// return mp[now][cnt];
// }
mint ret = p;
for(ll i=ma+1;i<(int)prime.size();i++){
ll x = now*prime[i];
if(x > m)break;
int c = cnt+1;
int cc = 1;
while(x<=m){
cc++;
ret += dfs(x,c,i,p*COM(n+cc-2,cc-1));
x *= prime[i];
c++;
}
}
// mp[now][cnt]=ret.x;
// cerr << now << " " << ret << endl;
return ret;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
int n,m;
cin>>n>>m;
COMinit();
mint dp[20][5005];
dp[0][0]=1;
for(int i=0;i<19;i++){
for(int j=0;j<=n;j+=2){
for(int k=0;k<=m;k++){
int sum = k + (1<<i)*j;
if(sum <= m){
dp[i+1][sum] += dp[i][k] * COM(n,j);
}
}
}
}
cout << dp[19][m] << endl;
} |
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <math.h>
#include <string.h>
#include <cstdlib>
#include <sstream>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <fstream>
typedef long long ll;
#define INF 10000000;
using namespace std;
vector<int> insertVector(int n)
{
vector<int> x;
for(ll i = 0; i < n; i++)
{
int y;
cin >> y;
x.push_back(y);
}
return x;
}
void displayVector(vector<int> x)
{
for(auto i : x)
cout << i << " ";
cout << "\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, k;
double ans = 0;
cin >> n >> k;
vector<pair<ll,ll>> x;
for(ll i = 0; i < n; i++)
{
ll y, z;
cin >> y >> z;
x.push_back(make_pair(y, z));
}
sort(x.begin(), x.end());
/*for(auto i : x)
{
cout << i.first << " " << i.second << endl;
}*/
for(ll i = 0; i < n; i++)
{
if(x[i].first == x[i - 1].first)
{
k = k + x[i].second;
}
else if((ans + k) - x[i].first < 0)
{
ans = ans + k;
k = 0;
break;
}
else
{
k = (ans + k) - x[i].first + x[i].second;
ans = x[i].first;
}
}
if(k > 0)
ans = ans + k;
cout << setprecision(100) << ans;
}
| #include<bits/stdc++.h>
#define rep(i, m, n) for(int i = m; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
int main() {
ll n, k;
cin >> n >> k;
vector<pair<ll, ll>> ab(n);
rep(i, 0, n) cin >> ab[i].first >> ab[i].second;
sort(ab.begin(), ab.end());
rep(i, 1, n) {
if(ab[i].first == ab[i - 1].first) {
ab[i].second += ab[i - 1].second;
ab[i - 1].first = 0;
ab[i - 1].second = 0;
}
}
ll ans = 0;
rep(i, 0, n) {
if((ans + k) - ab[i].first < 0) {
ans += k;
break;
}
k -= (ab[i].first - ans);
k += ab[i].second;
ans += (ab[i].first - ans);
if(i == n - 1) ans += k;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int _ = (cout << fixed << setprecision(9), cin.tie(0), ios::sync_with_stdio(0));
using Int = long long;
void solve(int N, string S, vector<int> A) {
int k = 1e9;
for (int i = 0; i < N; i++) {
k = min(k, abs(A[i + 1] - A[i]));
}
vector<vector<int>> B(k, vector<int>(N + 1));
for (int i = 0; i < N + 1; i++) {
for (int j = 0; j < k; j++) {
B[j][i] += A[i] / k;
}
for (int j = 0; j < A[i] % k; j++) {
B[j][i] += 1;
}
}
cout << k << '\n';
for (int j = 0; j < k; j++) {
for (int i = 0; i < N + 1; i++) {
cout << B[j][i] << " \n"[i + 1 == N + 1];
}
}
}
int main() {
int N; cin >> N;
string S; cin >> S;
vector<int> A(N + 1); for (auto &a : A) cin >> a;
solve(N, S, A);
return 0;
}
| #include<iostream>
#include<cstring>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N=2e5+10;
int a[N];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d",&a[i]);
}
a[++m]=0;
a[++m]=n+1;
sort(a+1,a+1+m);
int minv=n;
for(int i=1;i<=m-1;i++){
if(a[i+1]>a[i]+1)
minv=min(a[i+1]-a[i]-1,minv);
}
int cnt=0;
for(int i=1;i<=m-1;i++){
if(a[i+1]>a[i]+1)
cnt+=(a[i+1]-a[i]-1+minv-1)/minv;
}
cout<<cnt;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define repd(i, a, b) for(int i=a; i>=b; i--)
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define fr first
#define sc second
#define vb vector<bool>
typedef long long ll;
typedef unsigned long long ull;
/*struct cmp {
bool operator()(const pair<int, pii> &p1, const pair<int, pii> &p2) {
return p1.fr > p2.fr;
}
};*/
void google(){
static int tc=1;
cout<<"Case #"<<tc<<": ";
tc++;
}
ll n,q;
void solve(int tc = 1) {
vll v(n);
for(int i=0;i<n;i++) cin>>v[i];
sort(v.begin(),v.end());
for(int i=0;i<q;i++){
ll k;
cin>>k;
ll l=1,r=2e18;
ll ans=2e18;
while(l<=r){
ll mid=(l+r)/2;
ll id=lower_bound(v.begin(),v.end(),mid+1)-v.begin();
ll val=mid-id;
// cout<<mid<<" "<<val<<endl;
if(val==k){
ans=min(ans,mid);
r=mid-1;
}
else if(val>k){
r=mid-1;
}
else{
l=mid+1;
}
}
cout<<ans<<endl;
}
}
int main() {
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
ios::sync_with_stdio(false);
cin.tie(0);
// cout << setprecision(16) << fixed;
int t = 1;
// cin >> t;
int tc=0;
while (t--) {
cin>>n>>q;
tc++;
solve(tc);
}
}
| #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
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;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const ll MAX = 1e18 + 1;
int main() {
int n, q; cin >> n >> q;
vll a(n + 2);
for (int i = 1; i <= n; i++) cin >> a[i];
a[0] = 0;
a[n + 1] = inf_l;
vll cnt(n + 1, 0);
rep(i,n+1) {
cnt[i] = a[i + 1] - a[i] - 1;
}
rep(i,n) cnt[i + 1] += cnt[i];
// debug(cnt);
rep(i,q) {
ll k; cin >> k;
int id = lower_bound(all(cnt), k) - cnt.begin();
cout << a[id + 1] + (k - cnt[id]) - 1 << endl;
// debug(id);
}
} |
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define INF 2e9
using namespace std;
int main(){
int a,b;
cin >> a;
if(a%100 == 0) b = a/100;
else b = (a+100)/100;
cout << b << endl;
} | #include<cstdio>
#include<iostream>
using namespace std;
int main(){
int x;
scanf("%d", &x);
printf("%d", (x+99)/100);
return 0;
} |
/*Look at Abhinav man, so inspirational*/
#pragma GCC optimize ("-O3")
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define forn(i,x,n) for(ll i=x;i<n;i++)
#define pb push_back
#define int long long
#define mp make_pair
#define debug(x) cout<<x<<"\n";
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mod 1000000007
#define endl '\n'
int n,m,k,a,b,c,d,x,y,z,q,e,v;
string s;
int ans;
int arr[1000000];
int brr[1000000];
int crr[1000000];
char vec[100000];
int nCr(int n, int r)
{
int p = 1, k = 1;
// C(n, r) == C(n, n-r),
// choosing the smaller value
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
// gcd of p, k
int m = __gcd(p, k);
// dividing by gcd, to simplify
// product division by their gcd
// saves from the overflow
p /= m;
k /= m;
n--;
r--;
}
// k should be simplified to 1
// as C(n, r) is a natural number
// (denominator should be 1 ) .
}
else
p = 1;
// if our approach is correct p = ans and k =1
return p;
}
int32_t main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//std::cout << fixed<< std::setprecision(9) << '\n';
int t;
t=1;
//cin>>t;
while(t--)
{
cin >> a >> b >> k;
int n=a+b;
for(int i=0;i<n;i++)
{
if(a==0)
{
cout<<'b';
b--;
continue;
}
else if(b==0)
{
cout<<'a';
a--;
continue;
}
d=nCr(a+b-1,a-1);
if(d<k)
{
k-=d;
cout<<'b';
b--;
}
else
{
cout<<'a';
a--;
}
}
}
}
| #include<bits/stdc++.h>
using namespace std;
const int toSreachDirX[4]={-1,0,1,0};
const int toSreachDirY[4]={0,1,0,-1};
const string toSreachDir="URDL";
int Term=0;
const int Height=30;
const int Width=30;
unordered_map<char,int>Direction2Index={{'U',0},{'R',1},{'D',2},{'L',3}};
struct road{
array<pair<int,int>,4>dist;
road(){
dist[0]={5000,0};
dist[1]={5000,0};
dist[2]={5000,0};
dist[3]={5000,0};
}
pair<int,int>get(char s){
switch (s){
case 'U':
return dist[0];
case 'R':
return dist[1];
case 'D':
return dist[2];
case 'L':
return dist[3];
default:
return pair<int,int>({1e8,0});
}
}
};
string solve(vector<vector<road>> &mati,int sx,int sy,int gx,int gy){
string operate="";
vector<vector<int>>dist(30,vector<int>(30,1e9));
vector<vector<char>>op(30,vector<char>(30,'/'));
dist[sx][sy]=0;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>dijk;
dijk.push({0,sx*30+sy});
while(dijk.size()){
auto [d,vn]=dijk.top();
dijk.pop();
int vx=vn/30,vy=vn%30;
if(d>dist[vx][vy])continue;
for(int i=0;i<4;i++){
if(vx+toSreachDirX[i]>=0 && vx+toSreachDirX[i]<30 && vy+toSreachDirY[i]>=0 && vy+toSreachDirY[i]<30){
if(dist[vx+toSreachDirX[i]][vy+toSreachDirY[i]]>d+mati[vx][vy].get(toSreachDir[i]).first){
dist[vx+toSreachDirX[i]][vy+toSreachDirY[i]]=d+mati[vx][vy].get(toSreachDir[i]).first;
dijk.push({dist[vx+toSreachDirX[i]][vy+toSreachDirY[i]],(vx+toSreachDirX[i])*30+vy+toSreachDirY[i]});
op[vx+toSreachDirX[i]][vy+toSreachDirY[i]]=toSreachDir[i];
}
}
}
}
while(gx!=sx||gy!=sy){
operate+=op[gx][gy];
switch(op[gx][gy]){
case 'U':
gx++;break;
case 'D':
gx--;break;
case 'R':
gy--;break;
case 'L':
gy++;break;
}
}
reverse(operate.begin(),operate.end());
return operate;
}
string solve2(vector<vector<road>> &mati,int sx,int sy,int gx,int gy){
string ret="";
for(int i=0;i<sx-gx;i++){
ret+='U';
}
for(int i=0;i<gx-sx;i++){
ret+='D';
}
for(int i=0;i<sy-gy;i++){
ret+='L';
}
for(int i=0;i<gy-sy;i++){
ret+='R';
}
return ret;
}
void evoluate(vector<vector<road>> &mati,double dist,string &op,int sx,int sy){
int len=op.size();
for(int i=0;i<len;i++){
auto t=mati[sx][sy].dist[Direction2Index[op[i]]];
mati[sx][sy].dist[Direction2Index[op[i]]].first=(t.first*t.second+dist/len*2)/(t.second+2);
mati[sx][sy].dist[Direction2Index[op[i]]].second++;
sx+=toSreachDirX[Direction2Index[op[i]]];
sy+=toSreachDirY[Direction2Index[op[i]]];
}
}
int main(){
vector<vector<road>> mati(30,vector<road>(30));
double dist=0;
int sx,sy,gx,gy;
for(int i=0;i<1000;i++){
Term++;
cin>>sx>>sy>>gx>>gy;
string t="";
if(Term<250)t=solve2(mati,sx,sy,gx,gy);
else t=solve(mati,sx,sy,gx,gy);
cout<<t<<endl;
cin>>dist;
evoluate(mati,dist,t,sx,sy);
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template<class T> using VE = vector<T>;
template<class T> using P = pair<T, T>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,k,n) for(int i=(k);i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define output(x,y) cout << fixed << setprecision(y) << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
ll Gcd(ll a, ll b) { return b ? Gcd(b, a % b) : a; }
ll Lcm(ll a, ll b) { return a / Gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
int main(){
int n;
cin >> n;
double x0, y0, xh, yh;
cin >> x0 >> y0 >> xh >> yh;
double xmid = (x0 + xh) / 2;
double ymid = (y0 + yh) / 2;
double degtmp = 2 * pi / (double)n;
double r = sqrt(pow(xh - x0, 2) + pow(yh - y0, 2)) / 2;
double deg0 = atan2(y0 - ymid, x0 - xmid);
double deg = deg0 + degtmp;
cout << fixed << setprecision(8) << xmid + r * cos(deg) << " " << ymid + r * sin(deg) << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define N 100005
#define Db double
#define For(i,x,y)for(i=x;i<=(y);i++)
struct dls
{
int val,id;
}b[N];
int a[N],h[N],n,m;
bool cmp(dls _,dls __)
{
return 1LL*_.val*n-1LL*a[_.id]*m>1LL*__.val*n-1LL*a[__.id]*m;
}
int main()
{
int k,i,tot=0;
scanf("%d%d%d",&k,&n,&m);
For(i,1,k)
{
scanf("%d",&a[i]);
b[i].val=ceil(Db(a[i])/Db(n)*Db(m));
tot+=b[i].val;
b[i].id=i;
}
sort(b+1,b+k+1,cmp);
For(i,1,tot-m)b[i].val--;
For(i,1,k)h[b[i].id]=b[i].val;
For(i,1,k)printf("%d ",h[i]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
#define int long long
const int MOD=998244353;
int dp[15][1000001];
int pw(int n,int k){
assert(k>=0);
int res=1;
while(k){
if(k&1)(res*=n)%=MOD;
(n*=n)%=MOD;
k>>=1;
}
return res;
}
int Factorial[5000000],Finverse[5000000];
inline void Cinit(){
Factorial[0]=1;
for(int i=1;i<5e6;i++)Factorial[i]=(Factorial[i-1]*i)%MOD;
Finverse[4999999]=pw(Factorial[4999999],MOD-2);
for(int i=4999998;i>=0;i--)Finverse[i]=(i+1)*Finverse[i+1]%MOD;
}
int memo[5001][5001];
int nCk(int n,int k){
if(n<k)return 0;if(k<0)return 0;
if(!Factorial[0])Cinit();
if(~memo[n][k])return memo[n][k];
int res=Factorial[n];
(res*=Finverse[k])%=MOD;
(res*=Finverse[n-k])%=MOD;
return memo[n][k]=res;
}
signed main(){
memset(memo,-1,sizeof(memo));
int n,m;cin>>n>>m;
dp[0][0]=1;
for(int i=0;i<14;i++){
for(int j=0;j<1000000;j++){
if(((m>>i)&1)^(j&1))continue;
if(dp[i][j]==0)continue;
int s=j/2;//上に繰り上がる予約
for(int k=0;2*k<=n;k++){//k新たに繰り上げる
//n個のうち2k個が1
(dp[i+1][s+k]+=dp[i][j]*nCk(n,2*k)%MOD)%=MOD;
}
}
}
cout<<dp[14][0]<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define M 5005
#define K 13
#define MOD 998244353
int comb[M][M];
int dp[K][M];
int main()
{
for(int i = 0; i < M; i++) comb[i][0] = 1;
for(int i = 1; i < M; i++)
{
for(int j = 1; j <= i; j++) comb[i][j] = (comb[i-1][j]+comb[i-1][j-1])%MOD;
}
int n, m;
scanf("%d %d", &n, &m);
for(int j = 0; j <= min(n, m); j += 2) dp[0][j] = comb[n][j];
for(int i = 1; i < K; i++)
{
for(int j = 0; j <= m; j++)
{
for(int k = 0; k <= n && (1<<i)*k <= j; k += 2)
{
dp[i][j] += 1LL*comb[n][k]*dp[i-1][j-(1<<i)*k]%MOD;
if(dp[i][j] >= MOD) dp[i][j] -= MOD;
}
}
}
int ans = dp[K-1][m];
printf("%d\n", ans);
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<sstream>
#include<iomanip>
#define forn(i,n) for(int (i)=0;i<(n);i++)
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int,int>pii;
typedef long long ll;
typedef pair<ll,ll> pll;
const int MAXN=100005;
const int INF=2147483647;
const ll LINF=9223372036854775807;
const int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
int a,b;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>a>>b;
if(a==b)cout<<a;
else cout<<3-a-b;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long; //个人习惯
int main()
{
stack <char> fox; //定义
int n; //定义
string s; //定义
cin >> n; //输入
cin >> s; //输入
for(int i = 0; i < s.length(); i++)
{
if(fox.size() < 2)
{
fox.push(s[i]);
continue;
}
char foox = fox.top();
fox.pop();
if(fox.top() == 'f' && foox == 'o' && s[i] == 'x')
{
fox.pop(); //弹出
continue;
}
fox.push(foox);
fox.push(s[i]);
}
cout << fox.size(); //输出
return 0;
} |
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
std::vector<std::vector<int>> nx_init(const std::string &s, const char basis = 'a', const int sigma = 26) {
int n = s.size();
std::vector<std::vector<int>> nx(n, std::vector<int>(sigma, n));
nx[n - 1][s[n - 1] - basis] = n - 1;
for (int i = n - 2; i >= 0; --i) {
for (int j = 0; j < sigma; ++j) nx[i][j] = nx[i + 1][j];
nx[i][s[i] - basis] = i;
}
return nx;
}
void solve() {
int n; cin >> n;
string s[3]; REP(i, 3) cin >> s[i];
vector<vector<int>> nx[3];
REP(i, 3) nx[i] = nx_init(s[i] + s[i], '0', 2);
REP(i, 2) REP(j, 2) REP(l, 2) {
string ans = string(n, '0' + i) + to_string(j) + string(n, '0' + l);
bool valid = true;
REP(x, 3) {
bool ok = true;
int pos = 0;
for (char c : ans) {
if (pos >= n * 4) {
ok = false;
break;
}
pos = nx[x][pos][c - '0'];
if (pos >= n * 4) {
ok = false;
break;
}
++pos;
}
valid &= ok;
}
if (valid) {
cout << ans << '\n';
return;
}
}
REP(i, 2) REP(j, 2) REP(l, 2) {
string ans = to_string(j) + string(n, '0' + i) + string(n, '0' + l);
swap(ans[n - 1], ans[n]);
bool valid = true;
REP(x, 3) {
bool ok = true;
int pos = 0;
for (char c : ans) {
if (pos >= n * 4) {
ok = false;
break;
}
pos = nx[x][pos][c - '0'];
if (pos >= n * 4) {
ok = false;
break;
}
++pos;
}
valid &= ok;
}
if (valid) {
cout << ans << '\n';
return;
}
}
REP(i, 2) REP(j, 2) REP(l, 2) {
string ans = string(n, '0' + i) + string(n, '0' + l) + to_string(j);
swap(ans[n - 1], ans[n]);
bool valid = true;
REP(x, 3) {
bool ok = true;
int pos = 0;
for (char c : ans) {
if (pos >= n * 4) {
ok = false;
break;
}
pos = nx[x][pos][c - '0'];
if (pos >= n * 4) {
ok = false;
break;
}
++pos;
}
valid &= ok;
}
if (valid) {
cout << ans << '\n';
return;
}
}
// cout << n << endl;
// REP(i, 3) cout << s[i] << endl;
assert(false);
}
int main() {
int t; cin >> t;
while (t--) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int,int> > x,y;
vector<int> ans;
map<pair<int,int>,int > ma;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
int u,v;
cin>>u>>v;
x.push_back({u,i});
y.push_back({v,i});
}
sort(x.begin(),x.end());
sort(y.begin(),y.end());
int a=x.size();
int b=y.size();
for(int i=a-1;i>=a-3;i--)
{
ans.push_back(abs(x[0].first-x[i].first));
ma[{x[0].second,x[i].second}]=1;
}
for(int i=0;i<3;i++)
{
if(ma[{x[i].second,x[x.size()-1].second}]==0)
{
ans.push_back(abs(x[i].first-x[x.size()-1].first));
ma[{x[i].second,x[x.size()-1].second}]=1;
}
}
for(int i=b-1;i>=b-3;i--)
{
if(ma[{y[0].second,y[i].second}]==0)
{
ans.push_back(abs(y[0].first-y[i].first));
ma[{y[0].second,y[i].second}]=1;
}
}
for(int i=0;i<3;i++)
{
if(ma[{y[i].second,y[x.size()-1].second}]==0)
{
ans.push_back(abs(y[i].first-y[x.size()-1].first));
ma[{x[i].second,x[x.size()-1].second}]=1;
}
}
sort(ans.begin(),ans.end());
cout<<ans[ans.size()-2];
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
string S;
cin >> S;
stack<char> s;
int cnt = 0;
for(int i = 0; i < N; i++){
if(S[i] == 'f') s.push('f');
else if(S[i] == 'o'){
if(s.empty() || s.top() != 'f'){
while(!s.empty()) s.pop();
}
else if(s.top() == 'f') s.push('o');
}
else if(S[i] == 'x'){
if(s.empty() || s.top() != 'o'){
while(!s.empty()) s.pop();
}
else if(s.top() == 'o'){
s.pop();
s.pop();
cnt += 3;
}
}
else{
while(!s.empty()) s.pop();
}
}
cout << N - cnt << endl;
} | #include <bits/stdc++.h>
#include <functional>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef pair<int,int> pii;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
#define ld long double
#define rep(i,a) for(ll i = 0 ; i < a ; i++)
#define repe(i,a,b) for(ll i = a ; i < b ; i++)
#define bac(i,a) for(ll i = a ; i >= 0 ; i--)
#define bace(i,a,b) for(ll i = a ; i >= b ; i--)
#define pb push_back
#define in insert
#define ff first
#define ss second
#define setbit(x) __builtin_popcountll(x)
#define init(c,a) memset(c,a,sizeof(c))
#define all(c) c.begin(),c.end()
#define sz(c) (ll)c.size()
#define lb lower_bound
#define ub upper_bound
#define maxe *max_element
#define mine *min_element
#define rev reverse
#define Y "YES"
#define N "NO"
#define endl "\n"
#define debug(x) cout << #x << " : " << x << endl;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll MAX = 2e5 + 1;
//priority_queue<pii, vector<pii>, greater<pii> > pq;
inline ll add(ll a,ll b) {return ((a % mod) + (b % mod)) % mod;}
inline ll sub(ll a,ll b) {return ((a % mod) - (b % mod) + mod) % mod;}
inline ll mul(ll a,ll b) {return ((a % mod) * (b % mod)) % mod;}
ll pwr(ll x,ll n){
if(n == 0){
return 1;
}
if(n&1){
return mul(x, pwr(mul(x, x), (n - 1) / 2));
} else {
return pwr(mul(x, x), n / 2);
}
}
ll modInv(ll n){
return pwr(n, mod - 2);
}
ll gcd(ll a,ll b) {
if(b == 0) {
return a;
}
return gcd(b, a % b);
}
int toDigit(char ch)
{
return (ch - '0');
}
long modPow(long a, long x, long p) {
//calculates a^x mod p in logarithmic time.
long res = 1;
while(x > 0) {
if( x % 2 != 0) {
res = (res * a) % p;
}
a = (a * a) % p;
x /= 2;
}
return res;
}
long modInverse(long a, long p) {
return modPow(a, p-2, p);
}
int nCrModpDP(int n, int r, int p)
{
int C[r+1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j-1])%p;
}
return C[r];
}
int nCrModpLucas(int n, int r, int p)
{
if (r==0)
return 1;
int ni = n%p, ri = r%p;
return (nCrModpLucas(n/p, r/p, p) *
nCrModpDP(ni, ri, p)) % p;
}
ll fact(int f){
ll ans=1;
for(int i=1;i<=f;++i){
ans=mul(i,ans);
}
return ans;
}
void solve(){
int n;
cin>>n;
string s;
cin>>s;
stack<int> f;
stack<int> o;
stack<int> x;
int a=0;
int fi,oi,xi;
fi=oi=xi=0;
rep(i,n){
if(s[i]=='f') f.push(i-oi);
else if(s[i]=='o') o.push(i-oi);
else if(s[i]=='x'){
x.push(i-oi);
if(!f.empty()&&!o.empty()){
if(f.top()+1==o.top()&&o.top()+1==x.top()){
a++;
f.pop();o.pop();x.pop();
oi+=3;
}
}
}
}
cout<<n-a*3<<'\n';
}
int main(){
int t=1;
while(t--) solve();
return 0;
} |
#include <iostream>
using namespace std;
char c[4];
typedef long long ll;
ll mod = 1000000007;
ll pw(ll a, ll x){
if(x<0) return 1;
ll ret = 1;
while(x){
if(x&1) (ret *= a) %= mod;
(a *= a) %= mod; x /= 2;
}
return ret;
}
ll dp1[2010][2],dp2[2010][2];
void solve(){
dp1[0][0] = 1,dp2[0][0] = 1;
int i,n = 1010;
for(i=1;i<=n;i++){
(dp1[i][0] += dp1[i - 1][1]) %= mod;
(dp1[i][1] += dp1[i - 1][0] + dp1[i - 1][1]) %= mod;
(dp2[i][0] += dp2[i - 1][0] + dp2[i - 1][1]) %= mod;
(dp2[i][1] += dp2[i - 1][0]) %= mod;
}
}
int main(){
int i,n; cin >> n;
for(i=0;i<4;i++) cin >> c[i];
solve();
if(c[1]=='A'){
if(c[0]=='A'){
cout << 1 << endl;
}else{
if(c[0]==c[1]){
if(n>=3) cout << pw(2,n - 3) << endl;
else cout << 1 << endl;
}else{
if(c[0]=='B' && c[2]=='B') cout << pw(2,n - 3) << endl;
else cout << dp2[n - 1][1] << endl;
}
}
}else{
if(c[3]=='B'){
cout << 1 << endl;
}else{
if(c[2]==c[3]){
if(n>=3) cout << pw(2,n - 3) << endl;
else cout << 1 << endl;
}else{
if(c[2]=='A' && c[3]=='A') cout << pw(2,n - 3) << endl;
else cout << dp1[n - 1][1] << endl;
}
}
}
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
long long N;
cin >> N;
vector<long long> A(N);
vector<long long> sumVec(N);
for (long long i = 0; i < N; i++)
{
cin >> A.at(i);
if (i == 0)
{
sumVec.at(i) = A.at(i);
}
else
{
sumVec.at(i) = sumVec.at(i - 1) + A.at(i);
}
}
// 1+2 + 2+1+2
// 1+max, 2+1+max,3+2+1+max
// 2 1 1 3
// 1 + 4 + 4
long long maxValue = 0;
long long addedMaxValue = 0;
long long sum = 0;
long long sumsums = 0;
for (long long i = 0; i < A.size(); i++)
{
sumsums += sumVec.at(i);
if (maxValue < A.at(i))
{
maxValue = A.at(i);
}
auto sum = sumsums + maxValue * (i + 1);
cout << sum << endl;
}
} |
//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
rep(i,a.size()) cout << a[i] << " ";
cout << endl;
}
template<class T>
bool chmax(T&a, T b){
if(a < b){
a = b;
return 1;
}
return 0;
}
template<class T>
bool chmin(T&a, T b){
if(a > b){
a = b;
return 1;
}
return 0;
}
template<class T>
void g(T &a){
cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
template<class T>
void add(T&a,T b){
a+=b;
if(a >= mod) a-=mod;
}
int n, a[105], b[105], cnt[205];
bool dp[105];
int main(){
cin >> n;
repn(i, n){
cin >> a[i] >> b[i];
if(a[i] >= 1) cnt[a[i]]++;
if(b[i] >= 1) cnt[b[i]]++;
}
rep(i, 205){
if(cnt[i] > 1){
cout << "No" << '\n';
return 0;
}
}
dp[0] = 1;
for(int i=0;i<n;i++){
if(dp[i] == 0) continue;
for(int j=i+1;j<=n;j++){
//[i+1, j]
int use = 0, blank = 0;
bool fail = 0;
repn(k, n){
if(a[k] == -1 && b[k] == -1){
blank ++;
}
else if(a[k] != -1 && b[k] != -1){
if(i*2+1 <= a[k] && a[k] <= j*2){
use++;
if(a[k]+j-i != b[k]) fail = 1;
}
else if(b[k] <= i*2) use++;
}
else if(a[k] == -1){
if(i*2+1 <= b[k] && b[k] <= j*2){
use++;
if(i+j+1 <= b[k] && b[k] <= j*2 && cnt[b[k]-(j-i)] == 0);
else fail = 1;
}
else if(b[k] <= i*2) use++;
}
else{
if(i*2+1 <= a[k] && a[k] <= j*2){
use++;
if(i*2+1 <= a[k] && a[k] <= i+j && cnt[a[k]+(j-i)] == 0);
else fail = 1;
}
else if(a[k] <= i*2) use++;
}
}
//cout << i << " " << j << " " << use << " " << blank << endl;
if(use > j) fail = 1;
if(j-use > blank) fail = 1;
if(!fail){
dp[j] = 1;
}
}
}
cout << (dp[n]?"Yes":"No") << '\n';
} | #include <iostream>
#include <cstdio>
#define N 202
#define int long long
using namespace std;
const int mod=998244353;
const int G=3;
int read()
{
char c=getchar();
int w=0,f=1;
while(c<'0'||c>'9'){
if(c=='-') f=-1;
c=getchar();
}
while(c<='9'&&c>='0'){
w=w*10+c-'0';
c=getchar();
}
return w*f;
}
int n,i,j,a[N],b[N],cnta[N],cntb[N],f[N][N],g[N],r[N],f1[N],g1[N];
bool judge(int l,int r)
{
int len=(r-l+1)/2+1;
for(int i=1;i<=n;i++){
if(a[i]!=-1&&b[i]!=-1){
if(l<=a[i]&&b[i]<=r&&b[i]-a[i]+1!=len) return 0;
if(a[i]<l&&b[i]>=l) return 0;
if(a[i]<=r&&b[i]>r) return 0;
}
else{
if(a[i]!=-1&&l<=a[i]&&a[i]<=r&&cntb[a[i]+len-1]) return 0;
if(b[i]!=-1&&l<=b[i]&&b[i]<=r&&cnta[b[i]-len+1]) return 0;
if(a[i]!=-1&&b[i]!=-1) continue;
}
}
for(int i=l;i+len-1<=r;++i){
if(cntb[i]||cnta[i+len-1]) return 0;
}
return 1;
}
int poww(int a,int b)
{
int ans=1,base=a;
while(b){
if(b&1) ans=ans*base%mod;
base=base*base%mod;
b>>=1;
}
return ans;
}
void NTT(int *a,int n,int inv)
{
for(int i=0;i<n;i++){
if(i<r[i]) swap(a[i],a[r[i]]);
}
for(int l=2;l<=n;l<<=1){
int mid=l/2;
int cur=poww(G,(mod-1)/l);
if(inv==-1) cur=poww(cur,mod-2);
for(int i=0;i<n;i+=l){
int omg=1;
for(int j=0;j<mid;j++,omg=omg*cur%mod){
int tmp=omg*a[i+j+mid]%mod;
a[i+j+mid]=(a[i+j]-tmp+mod)%mod;
a[i+j]=(a[i+j]+tmp)%mod;
}
}
}
if(inv==-1){
for(int i=0;i<n;i++) a[i]=a[i]*poww(n,mod-2)%mod;
}
}
void getinv(int *f,int *g,int n)
{
if(n==1){
g[0]=poww(f[0],mod-2);
return;
}
getinv(f,g,n/2);
for(int i=0;i<n;i++) f1[i]=f[i];
for(int i=0;i<n;i++) g1[i]=g[i];
int m=2*n,lim=0;
while((1<<lim)<m) lim++;
for(int i=0;i<m;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(lim-1));
NTT(f1,m,1);NTT(g1,m,1);
for(int i=0;i<m;i++) g1[i]=f1[i]*g1[i]%mod*g1[i]%mod;
NTT(g1,m,-1);
for(int i=0;i<n;i++) g[i]=(2*g[i]%mod-g1[i]+mod)%mod;
}
signed main()
{
n=read();
for(i=1;i<=n;++i){
a[i]=read(),b[i]=read();
if(a[i]!=-1) cnta[a[i]]++;
if(b[i]!=-1) cntb[b[i]]++;
}
for(i=1;i<=2*n;++i){
if(cnta[i]+cntb[i]>1){
puts("No");
return 0;
}
}
for(i=1;i<=n;++i){
if(a[i]!=-1&&b[i]!=-1&&a[i]>b[i]){
puts("No");
return 0;
}
}
for(i=1;i<=n*2;i+=2){
for(j=i+1;j<=n<<1;j+=2) f[i][j]=judge(i,j);
}
for(i=1;i<=100;i++)
g[0]=1;
for(i=2;i<=2*n;i+=2){
for(j=0;j<i;j+=2){
if(f[j+1][i]) g[i]|=g[j];
}
}
if(g[n*2]!=0) puts("Yes");
else puts("No");
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> V;
#define F(N,S,E) for(int N=(int)(S);N<=(int)(E);N++)
#define R(N,S,E) for(int N=(int)(S);N>=(int)(E);N--)
#define D(N,E) for(int N=0;N<(int)(E);N++)
LL read(){LL x;scanf("%lld",&x);return x;}
int main(){
int n=read();
int mx=-1;
LL sum=0;
LL sum2=0;
F(i,1,n){
int a=read();
mx=max(mx,a);
sum+=a;
sum2+=sum;
printf("%lld\n",(LL)mx*i+sum2);
}
return 0;
} | #include <iostream>
using namespace std;
typedef long long ll;
int main() {
int N, A[110];
cin >> N;
for (int i = 0; i < N; ++i){
cin >> A[i];
}
int res = 0;
int res_index = 2;
for(int i = 2; i <= 1000; ++i){
int score = 0;
for (int j = 0; j < N; ++j){
if (A[j] % i == 0) score++;
}
if (score >= res){
res = score;
res_index = i;
}
}
cout << res_index << 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, q;
cin >> n >> q;
VL a(n);
cin >> a;
VL v(n);
RREP(i, n) {
v[i] = a[i] - i;
}
dump(v);
REP(_, q) {
ll k;
cin >> k;
auto it = std::upper_bound(ALL(v), k) - v.begin();
--it;
if (it == -1) {
cout << k << endl;
} else {
cout << a[it] + k - v[it] + 1 << endl;
}
}
}
| /**
* File : B.cpp
* Author : JCHRYS <[email protected]>
* Date : 12.01.2020
* Last Modified Date: 12.01.2020
* Last Modified By : JCHRYS <[email protected]>
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, m;
cin >> n >> k >> m;
int total = 0;
for (int i = 0; i < n - 1; i++) {
int tmp;
cin >> tmp;
total += tmp;
}
int need = m*(n) - total;
if (need > k) {
cout << -1;
} else {
cout << max(0, need);
}
return 0;
}
|
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
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);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}
const unsigned int mod = 1000000007;
//const unsigned int mod = 998244353;
struct mint {
unsigned int x;
mint():x(0) {}
mint(int64_t x_) {
int64_t v = int64_t(x_ % mod);
if(v < 0) v += mod;
x = (unsigned int)v;
}
static mint row(int v) {
mint v_;
v_.x = v;
return v_;
}
mint operator-() const { return mint(-int64_t(x));}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
uint64_t z = x;
z *= a.x;
x = (unsigned int)(z % mod);
return *this;
}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
friend bool operator==(const mint &a, const mint &b) {return a.x == b.x;}
friend bool operator!=(const mint &a, const mint &b) {return a.x != b.x;}
mint &operator++() {
x++;
if(x == mod) x = 0;
return *this;
}
mint &operator--() {
if(x == 0) x = mod;
x--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint pow(int64_t t) const {
mint x_ = *this, r = 1;
while(t) {
if(t&1) r *= x_;
x_ *= x_;
t >>= 1;
}
return r;
}
//for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) {return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
string to_string(mint a) {
return to_string(a.x);
}
//head
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
char aa, ab, ba, bb;
cin >> aa >> ab >> ba >> bb;
mint ans;
if(ab == 'A') {
if(aa == 'A') ans = 1;
else {
if(ba == 'A') {
mint a, b;
a = 1;
rep(i, n-1) {
swap(a, b);
a += b;
}
ans = b;
}
else {
ans = mint(2).pow(max(n-3, 0));
}
}
}
else {
if(bb == 'B') ans = 1;
else {
if(ba == 'B') {
mint a, b;
a = 1;
rep(i, n-1) {
swap(a, b);
a += b;
}
ans = b;
}
else {
ans = mint(2).pow(max(n-3, 0));
}
}
}
cout << ans << endl;
} | #define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector <int> vi;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
constexpr ll nax = 2e5+6969, INF = 2e9+6969, mod = 1e9+7;
ll n;
char c[4];
ll Pow(ll a, ll b)
{
ll w = 1;
while(b)
{
if(b&1) w = (w*a) % mod;
a = (a*a) % mod;
b >>= 1;
}
return w;
}
ll F(ll a)
{
ll f[nax];
f[0] = f[1] = 1;
for(int i=2;i<=a;++i) f[i] = (f[i-1] + f[i-2]) % mod;
return f[a];
}
int main()
{
// cout << F(577) << "\n";
cin >> n;
for(int i=0;i<4;++i) cin >> c[i];
if(n <= 3) puts("1");
else
{
if(c[1] == 'B')
{
if(c[3] == 'B') puts("1");
else
{
if(c[2] == 'A') cout << Pow(2LL, n-3) << "\n";
else cout << F(n-2) << "\n";
}
}
else
{
if(c[0] == 'A') puts("1");
else
{
if(c[2] == 'B') cout << Pow(2LL, n-3) << "\n";
else cout << F(n-2) << "\n";
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long N;
cin >> N;
vector<pair<long long, long long>> A;
long long ans = 0;
for (long long i=0; i<(pow(2,N)); i++) {
long long x;
cin >> x;
A.push_back( make_pair(i+1, x) );
}
vector<pair<long long, long long>> B = A;
while (B.size() > 2) {
vector<pair<long long, long long>> C;
for (long long i=0; i<B.size(); i+=2) {
if (B.at(i).second > B.at(i+1).second)
C.push_back(B.at(i));
else
C.push_back(B.at(i+1));
}
B = C;
C.clear();
}
if (B.at(0).second > B.at(1).second)
cout << B.at(1).first << endl;
else
cout << B.at(0).first << endl;
return 0;
} | #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
int N;
char buf[200011];
void MAIN() {
scanf("%d", &N);
scanf("%s", buf);
scanf("%s", buf);
scanf("%s", buf);
putchar('0');
REP (i, N) putchar('1');
REP (i, N) putchar('0');
puts("");
}
int main() {
int TC = 1;
scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
ll c=a%i_7;
if(c>=0)return c;
return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll f(char c){
ll k=c-'0';
if(0<=k && k<=9){
return k;
}
ll l=c-'A';
return 10+l;
}
int main(){fastio
string N;cin>>N;
ll K;cin>>K;
ll n;n=(ll)N.size();
ll dp[2][n][17];
ll bol[n][16];rep(i,0,n-1)rep(j,0,15)bol[i][j]=0;
rep(i,0,n-1){
if(i>0){
rep(j,0,15){
bol[i][j]=bol[i-1][j];
}
}
bol[i][f(N[i])]=1;
}
/*
rep(i,0,15){
rep(j,0,n-1){
cout<<bol[j][i]<<' ';
}
cout<<endl;
}
cout<<endl;
*/
rep(i,0,n-1){
rep(j,1,15){
bol[i][j] = bol[i][j-1]+bol[i][j];
}
}
/*
rep(j,0,15){
rep(i,0,n-1){
cout<<bol[i][j]<<' ';
}
cout<<endl;
}
cout<<endl;
*/
//bol[i][j]=0~jのうちN[i]までで現れたものの個数
rep(i,0,n-1){
rep(j,0,16){
if(j==bol[i][15]){
dp[0][i][j]=1;
}else{
dp[0][i][j]=0;
}
if(i==0){
if(j==0){
dp[1][i][j]=1;
}else if(j==1){
dp[1][i][j]=f(N[i])-1;
}else{
dp[1][i][j]=0;
}
}else{
if(j==0){
dp[1][i][j]=1;
}else{
if(f(N[i]) == 0){
if(j==1){
dp[1][i][j] = mod(dp[1][i-1][j-1]*(16-j)+dp[1][i-1][j]*j);
}else{
dp[1][i][j] = mod(dp[1][i-1][j-1]*(17-j)+dp[1][i-1][j]*j);
}
}else{
if(j==1){
dp[1][i][j] = mod(dp[1][i-1][j-1]*(16-j)+dp[1][i-1][j]*j);
}else{
dp[1][i][j] = mod(dp[1][i-1][j-1]*(17-j)+dp[1][i-1][j]*j);
}
dp[1][i][j] += dp[0][i-1][j]*(bol[i-1][f(N[i])-1]);
dp[1][i][j] = mod(dp[1][i][j]);
dp[1][i][j] += dp[0][i-1][j-1]*(f(N[i])-bol[i-1][f(N[i])-1]);
dp[1][i][j] = mod(dp[1][i][j]);
}
}
}
}
}
ll ans=0;
ans += dp[0][n-1][K];
ans = mod(ans);
ans += dp[1][n-1][K];
ans = mod(ans);
cout<<ans<<endl;
/*
rep(i,0,15){
rep(j,0,n-1){
cout<<bol[j][i]<<' ';
}
cout<<endl;
}
cout<<endl;
rep(k,0,1){
cout<<"k: "<<k<<endl;
rep(j,0,15){
rep(i,0,n-1){
cout<<dp[k][i][j]<<' ';
}
cout<<endl;
}
cout<<endl;
}
*/
return 0;
}
| #include<bits/stdc++.h>
using namespace std ;
#define all(x) (x).begin(), (x).end()
#define ll long long
const ll mod = 1e9 + 7;
int ri(){
int n;
scanf("%d", &n);
return n;
}
void solve(){
int n = ri();
int a[n];
for(auto &i : a) i = ri();
int res = INT_MAX;
for(int i=0; i < (1 << (n-1)); i++){
int ored = 0, xored = 0;
for(int j=0; j<=n; j++){
if(j < n) ored |= a[j];
if(j == n || (i >> j & 1)) xored ^= ored, ored = 0;
}
res = min(res, xored);
}
cout << res << "\n" ;
}
int main(){
int t = 1;
// cin >> t;
while(t--){
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
long long get(long long x) {
string s = bitset<100>(x).to_string();
int ans = 0;
int cur = 0;
for (int i = 0; i < 100; i++)
if (s[i] == '1') cur++;
else {
ans += min(cur, 2);
cur = 0;
}
ans += min(cur, 2);
return ans;
}
long long get(long long x, long long y, int jumps) {
x <<= jumps;
long long diff = x - y;
if (diff < 0) diff = -diff;
long long rem = diff >> jumps;
return min(rem + get(diff - (rem << jumps)),
rem + 1 + get(((rem + 1) << jumps) - diff));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long x, y;
cin >> x >> y;
long long ans = max(x, y) - min(x, y);
for (int i = 0; (x << i) <= (y << 2); i++)
ans = min(ans, i + get(x, y, i));
cout << ans << '\n';
return 0;
}
| #pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <algorithm>
#include <iostream>
using namespace std;
#define f(x,y,z) for(int x=y;x<=z;++x)
#define d(x,y,z) for(int x=y;x>=z;--x)
typedef long long ll;
ll n, ans = 1, a[100001];
const int mod = 1e9+7;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n;
f(i,1,n) cin >> a[i];
sort(a+1, a+n+1);
d(i,n,2) a[i] -= a[i-1];
f(i,1,n) ans = ans*(a[i]+1)%mod;
cout << ans << '\n';
} |
#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
preprocess() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} ____;
#pragma endregion head
#pragma region library
const int MOD = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime MOD
mint inv() const {
return pow(MOD - 2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
bool operator==(const mint a) const {
return x == a.x;
}
bool operator!=(const mint a) const {
return x != a.x;
}
friend ostream& operator<<(ostream& os, const mint& value) {
os << value.x;
return os;
}
friend istream& operator>>(istream& is, mint& value) {
ll t;
is >> t;
value = mint(t);
return is;
}
};
#pragma endregion library
int main() {
int h, w, k;
cin >> h >> w >> k;
vv<char> cs(h, vector<char>(w, '#'));
rep(i, k) {
int h, w;
char c;
cin >> h >> w >> c;
h--;
w--;
cs[h][w] = c;
}
mint three = 3, p = three.pow(h * w - k);
mint div3 = (mint)1 / 3;
vv<mint> dp(h, vector<mint>(w));
dp[0][0] = p;
rep(i, h) rep(j, w) {
if (cs[i][j] == '#') {
if (i < h - 1) {
dp[i + 1][j] += dp[i][j] * 2 * div3;
}
if (j < w - 1) {
dp[i][j + 1] += dp[i][j] * 2 * div3;
}
}
if (cs[i][j] == 'X') {
if (i < h - 1) {
dp[i + 1][j] += dp[i][j];
}
if (j < w - 1) {
dp[i][j + 1] += dp[i][j];
}
}
if (cs[i][j] == 'R') {
if (j < w - 1) {
dp[i][j + 1] += dp[i][j];
}
}
if (cs[i][j] == 'D') {
if (i < h - 1) {
dp[i + 1][j] += dp[i][j];
}
}
}
cout << dp[h - 1][w - 1] << '\n';
}
| #pragma GCC optimize ("O2")
#pragma GCC target ("avx")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int dp[2001];
char S[4002000], c;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H = 0, W = 0;
while ((c = getchar()) >= '0') H = H * 10 + c - '0';
while ((c = getchar()) >= '0') W = W * 10 + c - '0';
fread(S, 1, H * (W + 1), stdin);
auto s = S + (H - 1) * (W + 1);
dp[W - 1] = 44 - s[W - 1];
for (int j = W - 2; j >= 0; j--) {
dp[j] = 44 - s[j] - dp[j + 1];
}
for (int i = H - 2; i >= 0; i--) {
s = S + i * (W + 1);
dp[W - 1] = 44 - s[W - 1] - dp[W - 1];
for (int j = W - 2; j >= 0; j--) {
dp[j] = 44 - s[j] - max(dp[j], dp[j + 1]);
}
}
if (S[0] == '+') dp[0]--;
else dp[0]++;
if (dp[0] < 0) co("Takahashi");
else if (dp[0] > 0) co("Aoki");
else co("Draw");
Would you please return 0;
} |
#ifdef _DEBUG
#define dout(x) clog << "Line " << __LINE__ << ": " << #x << "=" << (x) << el
#else
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#define dout(x)
#endif
#include <bits/stdc++.h>
using namespace std;
#define MP(a, b) make_pair(a, b)
#define PB(x) push_back(x)
#define EB(args...) emplace_back(args)
#define F first
#define S second
#define die exit(0)
template<typename T>
using vc = vector<T>;
template<typename T>
using uset = unordered_set<T>;
template<typename A, typename B>
using umap = unordered_map<A, B>;
template<typename T, typename Comp = less<T>>
using pq = priority_queue<T, vc<T>, Comp>;
template<typename T>
using minpq = pq<T, greater<T>>;
using db = double;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using tri = tuple<int, int, int>;
using vi = vc<int>;
using vii = vc<pii>;
using str = string;
constexpr char el = '\n';
constexpr char sp = ' ';
constexpr int INF = 0x3f3f3f3f;
constexpr ll LLINF = 0x3f3f3f3f3f3f3f3fLL;
// ---------------------------------------------------------------------
int a[10];
int n, k;
int g1(int x){
for(int& x: a)
x = 0;
while(x>0){
++a[x%10];
x/=10;
}
int ans = 0;
for(int i = 9; i >= 0; --i)
while(a[i]--){
ans *= 10;
ans += i;
}
return ans;
}
int g2(int x){
for(int& x: a)
x = 0;
while(x>0){
++a[x%10];
x/=10;
}
int ans = 0;
for(int i = 1; i < 10; ++i)
while(a[i]--){
ans *= 10;
ans += i;
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cout << fixed; clog << fixed;
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
#endif
cin >> n >> k;
while(k--)
n = g1(n) - g2(n);
cout << n;
}
| #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
std::pair<std::vector<int>,bool> fct(std::vector<int>z) {
std::vector<int> a(z.begin(), z.end());
std::sort(a.rbegin(), a.rend());
std::vector<int> b(a.rbegin(), a.rend());
int taille = a.size();
std::vector<int>ans(taille);
bool ok = true;
for (int i = (taille - 1); i >= 0; i--) {
if (a[i] < b[i]) {
ans[i] = 10 + a[i] - b[i];
if (ans[i] != z[i])ok = false;
if (i >= 1)b[i - 1]++;
}
else {
ans[i] = a[i] - b[i]; if (z[i] != ans[i])ok = false;
}
}
int index = ans.size()-1;
for (int i = 0; i < ans.size(); i++) {
if (ans[i] != 0) {
index = i;
break;
}
}
std::vector<int> aa;
for (int i = index; i < ans.size(); i++)aa.emplace_back(ans[i]);
return make_pair(aa,ok);
}
int main() {
//int t;
//std::cin >> t;
//while (t--) {
int n, k;
std::cin >> n >> k;
if (k == 0) {
std::cout << n;
return 0;
}
std::vector<int> a;
int tmp = n;
while (tmp) {
a.emplace_back(tmp % 10);
tmp /= 10;
}
//aa.resize(a.size() / 2);
while (k--) {
std::pair<std::vector<int>, bool> response = fct(a);
a.resize(response.first.size());
a = response.first;
if (response.second == true) {
break;
}
//for (auto x : a)std::cout << x;
//std::cout << "\n";
}
for (auto x : a)std::cout << x;
//std::cout << "\n";
//}
} |
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(a) a.size()
#define md(a, b) ((a) % b + b) % b
#define mod(a) md(a, MOD)
#define srt(a) sort(all(a))
#define mem(a, h) memset(a, (h), sizeof(a))
#define f first
#define s second
#define forn(i, n) for(int i = 0; i < n; i++)
#define fore(i, b, e) for(int i = b; i < e; i++)
#define forg(i, b, e, m) for(int i = b; i < e; i+=m)
//int in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//find_by_order kth largest order_of_key <
const int tam = 100010;
const int MOD = 1000000007;
const int MOD1 = 998244353;
const double EPS = 1e-9;
const double PI = acos(-1);
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
//freopen("asd.txt", "r", stdin);
//freopen("qwe.txt", "w", stdout);
int n, m;
cin>>n>>m;
string s[n];
int con = 0;
fore(i, 0, n)
{
cin>>s[i];
fore(j, 0, m - 1)
{
con += s[i][j] == '.' && s[i][j + 1] == '.';
if(i > 0)
con += s[i][j] =='.' && s[i - 1][j] == '.';
}
if(i > 0) con += s[i][m - 1] == '.' && s[i - 1][m - 1] == '.';
}
cout<<con<<'\n';
return 0;
}
// read the question correctly (is y a vowel? what are the exact constraints?)
// look out for SPECIAL CASES (n=1?) and overflow (ll vs int?) ARRAY OUT OF BOUNDSS2 | #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
vector<int> dx = {0, 0, -1, 1}, dy = {1, -1, 0, 0};
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
for (int i = 0; i < h; i++) {
cin >> s[i];
}
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (s[i][j] == '#') continue;
for (int k = 0; k < 4; k++) {
int x = j + dx[k];
int y = i + dy[k];
if (x < 0 || w <= x || y < 0 || h <= y) continue;
if (s[y][x] == '.') ans++;
}
}
}
cout << ans / 2 << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
//const ll INF = numeric_limits<ll>::max() / 4;
//const int INF = numeric_limits<int>::max() / 4;
map<int,set<pair<int,int> > > M; // (moji, (k,d)) dは、その文字の位
vector<bool> F(10,true);
ll X[3];
ll Z[3]; // 最大digit
bool loop(map<int,set<pair<int,int> > >::iterator it, ll x0, ll x1, ll x2){
if(it == M.end()){
if(x0 + x1 == x2){
X[0] = x0;
X[1] = x1;
X[2] = x2;
return true;
}
return false;
}
auto nt = it;
nt++;
for(ll i = 0; i < 10; i++){
if(!F[i]) continue;
F[i] = false;
ll y[3];
y[0] = x0, y[1] = x1, y[2] = x2;
bool f = true;
for(auto jt = it->second.begin(); jt != it->second.end(); jt++){
if(i == 0 && jt->second == Z[jt->first]) f = false;
y[jt->first] += i * jt->second;
}
if(f){
if(loop(nt, y[0], y[1], y[2])) return true;
}
F[i] = true;
}
return false;
}
int main() {
// ll N;
string S0, S1, S2;
cin >> S0 >> S1 >> S2;
ll d = 1;
for(int i = S0.length() - 1; 0 <= i; i--){
int s = S0[i];
M[s].insert(make_pair(0,d));
Z[0] = d;
d *= 10;
}
d = 1;
for(int i = S1.length() - 1; 0 <= i; i--){
int s = S1[i];
M[s].insert(make_pair(1,d));
Z[1] = d;
d *= 10;
}
d = 1;
for(int i = S2.length() - 1; 0 <= i; i--){
int s = S2[i];
M[s].insert(make_pair(2,d));
Z[2] = d;
d *= 10;
}
int m = M.size();
if(m > 10){
cout << "UNSOLVABLE" << endl;
return 0;
}
if(loop(M.begin(),0,0,0)){
cout << X[0] << endl;
cout << X[1] << endl;
cout << X[2] << endl;
return 0;
}
cout << "UNSOLVABLE" << endl;
return(0);
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "/debug.h"
#else
#define db(...)
#endif
#define all(v) v.begin(), v.end()
#define pb push_back
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;
string solveCase(int n, const vector<string> &a)
{
// TODO: edit here
vector<int> exist(26);
vector<char> ss;
for (auto &x : a)
for (auto &y : x)
{
exist[y - 'a'] = 1;
ss.pb(y);
}
int cnt = count(all(exist), 1);
sort(all(ss));
ss.erase(unique(all(ss)), ss.end());
vector<char> perms;
for (size_t i = 0; i < 10; i++)
perms.pb('0' + i);
if (cnt > 10 || ss.size() > 10)
return "UNSOLVABLE";
cnt = 0;
do
{
map<char, char> mp;
for (size_t i = 0; i < ss.size(); i++)
mp[ss[i]] = perms[i];
auto b = a;
db(mp);
auto check = [&]() -> bool {
for (auto &x : b)
for (auto &y : x)
y = mp[y];
vector<long long> nums(3);
for (size_t i = 0; i < b.size(); i++)
{
for (auto &x : b[i])
{
if ('0' <= x && x <= '9')
nums[i] = nums[i] * 10 + x - '0';
else
return false;
}
if (b[i][0] == '0')
return false;
}
return nums[0] && nums[1] && nums[2] && nums[0] + nums[1] == nums[2];
};
if (check())
{
string ret;
for (auto &x : b)
{
for (auto &y : x)
{
ret += y;
}
ret += "\n";
}
ret.pop_back();
return ret;
}
} while (next_permutation(all(perms)));
return "UNSOLVABLE";
}
// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main()
{
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
constexpr char endl = '\n';
// failed to analyze input format
// TODO: edit here
int n = 3;
// cin >> n;
vector<string> a(n);
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
auto ans = solveCase(n, a);
// failed to analyze output format
// TODO: edit here
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define N 20
using namespace std;
char c[N];int k;
int u[N],v[N],s[N],t[N],a[N],b[N];
int Pow(int x,int y){
int now=1;
for(;y;y>>=1,x=x*x)if(y&1)now=now*x;
return now;
}
bool check(){
long long ans=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
rep(i,1,5)a[s[i]]++,b[t[i]]++;
rep(i,1,9)ans+=i*Pow(10,a[i])-i*Pow(10,b[i]);
return ans>0;
}
int main(){
scanf("%d",&k);
scanf("%s",c+1);
rep(i,1,4)s[i]=c[i]-'0';
scanf("%s",c+1);
rep(i,1,4)t[i]=c[i]-'0';
rep(i,1,4)u[s[i]]++,u[t[i]]++;
double ans=0;
for(s[5]=1;s[5]<=9;s[5]++)for(t[5]=1;t[5]<=9;t[5]++)if(check()){
ans+=1.00*(k-u[s[5]])/(9*k-8)*(k-u[t[5]]-(t[5]==s[5]))/(9*k-9);
}
printf("%lf\n",ans);
return 0;
} | //#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}
template<typename T>void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}};
void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++){for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}};
template<typename T>void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;};
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
template<typename T>void rearrange(vector<ll>&ord, vector<T>&v){
auto tmp = v;
for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];
}
template<typename Head, typename... Tail>void rearrange(vector<ll>&ord,Head&& head, Tail&&... tail){
rearrange(ord, head);
rearrange(ord, tail...);
}
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int popcount(ll x){return __builtin_popcountll(x);};
int poplow(ll x){return __builtin_ctzll(x);};
int pophigh(ll x){return 63 - __builtin_clzll(x);};
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll res=0,buf=0;
bool judge = true;
ll n;cin>>n;
ll k=1<<n;
ll m=k-1;
//vector<string>ret(m,string(k,'B'));
vector<string>ret(1,"AB");
rep(i,1,n){
vector<string>nret((1<<i+1)-1,string(1<<i+1,'B'));
rep(j,0,ret.size()){
rep(o,0,ret[j].size()){
if(ret[j][o]=='A'){
nret[j][o]='A';
nret[j][o|1<<i]='A';
nret[j+ret.size()][o]='A';
}
if(ret[j][o]=='B'){
nret[j+ret.size()][o|1<<i]='A';
}
}
}
/*rep(j,ret.size(),nret.size()){
ll dif=j-(ll)ret.size();
//rep(o,0,1<<i)nret[j][(dif+o)%(1<<i+1)]='A';
rep(o,0,1<<i)nret[j][o]='A';
swap(nret[j][dif],nret[j][dif+(1<<i)]);
}*/
rep(o,0,1<<i)nret.back()[o]='A';
ret=nret;
}
cout<<m<<endl;
debug(ret,m,k);
/*ll pos=3;
vector<ll>cnt(k);
rep(i,0,m)rep(j,0,k){
if(ret[i][j]==ret[i][pos])cnt[j]++;
}
debug(cnt,k);*/
return 0;
} |
//templete
#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>;
int main(){
int N;
ll A,B;
ll ans=0;
cin>>N;
rep(i,N){
cin>>A>>B;
ans+=B*(B+1)/2-A*(A-1)/2;
}
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++)
struct ad{
int left, up, right, down;
};
int time_start;
int Num;
std::vector<ad> ads;
std::vector<std::pair<ad,int>> ads_start;
void in(){
std::cin >> Num;
ads.resize(Num);
ads_start.resize(Num);
rep(i,0,Num-1){
std:: cin >> ads[i].left >> ads[i].up;
ads[i].right = ads[i].left + 1;
ads[i].down = ads[i].up + 1;
ads_start[i].first = ads[i];
std::cin >> ads_start[i].second;
}
}
void make_randomcase(){
}
bool is_collision(int N){
if(ads[N].left<0||ads[N].up<0||ads[N].right>10000||ads[N].down>10000) return true;
rep(i,0,Num-1){
if(i==N) continue;
if( std::max(ads[i].left,ads[N].left) < std::min(ads[i].right,ads[N].right) && std::max(ads[i].up,ads[N].up) < std::min(ads[i].down,ads[N].down) ){
return true;
}
}
return false;
}
int score(){
long double point=0;
rep(i,0,Num-1){
point+=1.0-pow(1-1.0*std::min(ads_start[i].second,(ads[i].right-ads[i].left)*(ads[i].down-ads[i].up))/std::max(ads_start[i].second,(ads[i].right-ads[i].left)*(ads[i].down-ads[i].up)),2);
}
point *= 1e9;
point /= Num;
return round(point);
}
//ランダムに広げる(広げて得をして、なおかつできるものだけ広げる)
void solve(){
std::vector<std::vector<int>> delta(4,std::vector<int> (4,0));
delta[0][0] = delta[1][1] = -1;
delta[2][2] = delta[3][3] = 1;
std::vector<ad> best_ads(Num);
int bestscore = score();
rep(_,0,44){
rep(i,0,Num-1){
ads[i] = ads_start[i].first;
}
time_start = clock();
while(clock() - time_start < 100000){
int select = random()%Num;
int direction = random()%4;
int stretch = ( 10 - (clock() - time_start) / 10000 ) * ( random()%10 + 1 );
ads[select].left += stretch * delta[direction][0];
ads[select].up += stretch * delta[direction][1];
ads[select].right += stretch * delta[direction][2];
ads[select].down += stretch * delta[direction][3];
if(is_collision(select)||(ads[select].right-ads[select].left)*(ads[select].down-ads[select].up)>ads_start[select].second*std::min((long)10, 7 + ( clock() - time_start ) / 20000) / 10){
ads[select].left -= stretch * delta[direction][0];
ads[select].up -= stretch * delta[direction][1];
ads[select].right -= stretch * delta[direction][2];
ads[select].down -= stretch * delta[direction][3];
}
}
if(bestscore < score()){
bestscore = score();
best_ads = ads;
}
}
ads = best_ads;
}
void out(){
rep(i,0,Num-1){
std::cout << ads[i].left << ' ' << ads[i].up << ' ' << ads[i].right << ' ' << ads[i].down << '\n';
}
}
int main() {
srand((unsigned int)time(NULL));
time_start = std::clock();
in();
solve();
out();
std::cerr << score() << std::endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i,b) for(int i=0;i<b;i++)
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
main()
{
fast
int n,k;
cin>>n>>k;
int o=0,e=0;
loop(i,n){
string str;
cin>>str;
int cnt=0;
loop(i,k){
if(str[i]=='1') cnt++;
}
if(cnt%2==1) o++;
else e++;
}
cout<<(ll)o*(ll)e;
cerr<<"time taken: "<<(float)clock()/CLOCKS_PER_SEC<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ui unsigned int
#define us unsigned short
#define inf_int 1e9
#define inf_ll 1e18
#define mod 1000000007
#define smod 998244353
int n, m;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin >> n >> m;
string data[n];
for(int x=0;x<n;x++){
cin >> data[x];
}
ll a = 0, b = 0;
for(int x=0;x<n;x++){
int cnt = 0;
for(int y=0;y<m;y++){
if(data[x][y] == '1') cnt++;
}
if(cnt&1) a++;
else b++;
}
cout << a*b << "\n";
return 0;
}
// Santai
// Pikirin dengan benar-benar |
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
int g=max(a,b),l=min(a,b),n=l;
while(g%l!=0){
n=g%l;
g=l;
l=n;
}
return l;
}
int main(){
int n;
cin>>n;
vector<int64_t>a(n);
for(int i=0;i<n;i++)cin>>a[i];
int ans=a[0];
for(int i=0;i<n;i++){
ans=gcd(ans,a[i]);
}
cout<<ans<<endl;
} | #include <iostream>
#include <cstdio>
using namespace std;
int n,a,now;
int gcd(int x,int y)
{
if(x<y) swap(x,y);
if(y==0) return x;
return gcd(y,x%y);
}
int main()
{
scanf("%d%d",&n,&now);
for(int i=2;i<=n;i++)
{
scanf("%d",&a);
now=gcd(now,a);
}
printf("%d",now);
return 0;
} |
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int n,d;
inline ll calc(int x)
{
if(x<2 || x>2*n) return 0;
int l = max(x-n, 1), r = min(x-1, n);
return r<l? 0: r-l+1;
}
int main(void)
{
scanf("%d%d",&n,&d);
ll ans = 0;
for(int i=2; i<=2*n; ++i)
ans += calc(i) * calc(i-d);
printf("%lld",ans);
return 0;
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[300005], b[300005];
int main(){
int n;
cin >> n;
ll res = 0;
for(int i = 0; i < n; i ++ ) cin >> a[i], res += a[i] * a[i] * (n - 1);
for(int i = n - 1; i >= 0; i -- ){
b[i] = b[i + 1] + a[i];
}
ll ans = 0;
for(int i = 0; i < n; i ++ ){
ans += b[i + 1] * a[i];
}
ans *= 2;
cout << res - ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#define For(a,b,c) for(int a=b;a<=c;++a)
#define Dor(a,b,c) for(int a=b;a>=c;--a)
using namespace std;
typedef long long LL;
const int N=1007;
int n,m,c,a,vH[N],vL[N];
char G[N][N];
void flyL(int);
void flyH(int u) {
if(vH[u]) return;
vH[u]=c;
For(v,1,m) if(G[u][v]=='#') flyL(v);
}
void flyL(int u) {
if(vL[u]) return;
vL[u]=c;
For(v,1,n) if(G[v][u]=='#') flyH(v);
}
int main() {
scanf("%d%d",&n,&m);
For(i,1,n) scanf("%s",G[i]+1);
G[1][1]=G[n][1]=G[1][m]=G[n][m]='#';
For(i,1,n) if(!vH[i]) ++c,flyH(i);
For(i,1,n) vH[i]=0;
For(i,1,m) vL[i]=0;
a=c,c=0;
For(i,1,m) if(!vL[i]) ++c,flyL(i);
printf("%d",min(c-1,a-1));
return 0;
} | #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>
#include <climits>
#include <cstring>
#include <cassert>
using namespace std;
//using namespace atcoder;
#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
REP(i, SZ(v)) {
if (i) os << ", ";
os << v[i];
}
return os << "]";
}
template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "(" << p.first << " " << p.second << ")";
}
template<class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;
// edit
struct UnionFind {
vector<int> par, sz;
vector<map<int, int>> cnum;
UnionFind(int n) : par(n), sz(n, 1), cnum(n) {
for (int i = 0; i < n; ++i) par[i] = i;
}
void cls(int x, int s) {
cnum[x][s] = 1;
}
int root(int x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return;
if (sz[x] < sz[y]) swap(x, y);
par[y] = x;
sz[x] += sz[y];
sz[y] = 0;
for (auto kv : move(cnum[y])) {
cnum[x][kv.first] += kv.second;
}
}
bool issame(int x, int y) {
return root(x) == root(y);
}
int size(int x) {
return sz[root(x)];
}
};
void solve() {
int N, Q;
cin >> N >> Q;
UnionFind uf(N);
for (int i = 0; i < N; ++i) {
int c;
cin >> c;
c--;
uf.cls(i, c);
}
while (Q--) {
int t;
cin >> t;
if (t == 1) {
int a, b;
cin >> a >> b;
a--, b--;
uf.merge(a, b);
} else {
int x, y;
cin >> x >> y;
x--;
y--;
ll ans = uf.cnum[uf.root(x)][y];
cout << ans << endl;
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, n) for (int i = (int)(l); i < (int)(n); i++)
#define drep(i, n, l) for (int i = (int)(n); i > (int)(l); i--)
#define INF INT_MAX
#define INFF (pow(2,64))
#define def (200007)
#define MOD (1000000007)
typedef int64_t intt;
typedef vector<int> veci;
typedef vector<vector<int>> Veci;
typedef vector<int64_t> vecit;
typedef vector<vector<int64_t>> Vecit;
typedef vector<vector<double>> Vecd;
typedef vector<double> vecd;
typedef pair<intt, intt> P;
typedef unordered_set<int> uset;
typedef unordered_set<intt> usett;
int main(){
intt N,M,T;cin>>N>>M>>T;
intt rest=N;
intt pre_date=0;
int judge=1;
rep(i,0,M){
int a,b;cin>>a>>b;
if(i==0){
rest-=a;
}else rest-=a-pre_date;
if(rest<=0)judge=0;
rest+=b-a;
if(rest>N)rest=N;
pre_date=b;
}
rest-=T-pre_date;
if(rest<=0)judge=0;
if(judge)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
} | #include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
int a[maxn],b[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int sum,num,en;
cin>>sum>>num>>en;
int ls=0,x=sum;
for(int i=1;i<=num;i++)
{
cin>>a[i]>>b[i];
sum-=(a[i]-ls);
if(sum<=0)
{
cout<<"No"<<endl;
return 0;
}
ls=b[i];
sum+=(b[i]-a[i]);
sum=min(x,sum);
}
sum-=en-ls;
if(sum<=0)
{
cout<<"No"<<endl;
return 0;
}
cout<<"Yes"<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long int lli;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<pll> vop;
typedef vector<ll> vl;
typedef vector<vl> vvl;
#define REP(i,a,b) for(long long int i=a;i<b;i++)
#define REPR(i,a,b) for(long long int i=a;i>=b;i--)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define all(x) (x).begin(),(x).end()
#define endl "\n"
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
#define MAX LLONG_MAX
const int mxn=2e5+5;
#define INF 1e18
//----------------------------------------------------------------------------------------
//lower_bound returns an iterator pointing to the first ele. which has a value not less than ‘val
#define LB(arr,n) ll c=0;it=lower_bound(arr.begin(),arr.end(),n);if(it==arr.end()){c=1;}
//define it and if c==1 means not found
#define LB1(arr,n) ll idx=it-arr.begin();if(arr[idx]==n){ idx=idx;}else{idx=idx-1;}
// here idx holds the index( 0 based) of found element
//-----------------------------------------------------------------------------------------
//upper_bound returns an iterator pointing to the first ele. which has a value greater than ‘val’.
#define UB(arr,n) ll c=0;it=upper_bound(arr.begin(),arr.end(),n);if(it==arr.end()){c=1;}
#define UB1(arr,n) ll idx=it-arr.begin();
//-----------------------------------------------------------------------------------------
ll power(ll a,ll b,ll m) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return (ans+m)%m; }
ll lcm(ll a, ll b) { return (a*b)/__gcd(a, b); }
ll modInverse(ll a, ll m) { ll g = __gcd(a, m); return power(a, m-2, m); }
bool cmp(pair<int ,int > &a,pair<int ,int > & b){
if(a.second!=b.second){
return a.first<b.first;
}
else{
return a.second<b.second;
}
}
//prime factors of 10^18
vector<long long int> genfactors(long long int n){
vector<long long int> res;
long long int val = 2;
for(long long int i = 2; i*i <= n ; ++i){
while(n%i == 0){
res.push_back(i);
n/=i;
}
}
if(n > 1) res.push_back(n);
return res;
}
//--------------------------------------------------------------------------------------------
void solve(){
ll n,k,m;
ll a,b,c;
cin>>a>>b>>c;
if(c==0){
if(a<=b)cout<<"Aoki"<<endl;
else cout<<"Takahashi"<<endl;
}
else {
if(a>=b)cout<<"Takahashi"<<endl;
else cout<<"Aoki"<<endl;
}
}
int main()
{
fastIO;
ll t=1;
//cin >> t;
while(t--)
{
solve();
}
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(firstsecond) '(' << first< ',' << second< ')'
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-8;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T> &vector, unsigned int index)
{
vector.erase(vector.begin() + index);
}
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; }
using Graph = vector<vector<pair<ll,ll>>>;
const unsigned long long int cycle_per_sec = 2800000000;
unsigned long long int getCycle(){
unsigned int low, high;
__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
return ((unsigned long long int)low) | ((unsigned long long int)high << 32);
}
double getTime (unsigned long long int begin_cycle) {
return (double)(getCycle() - begin_cycle) / cycle_per_sec;
}
auto start_cycle = getCycle();
random_device seed_gen;
mt19937_64 rnd(seed_gen());
ll range_rand(ll a,ll b){
uniform_int_distribution<ll> dist_res(a,b);
return dist_res(rnd);
}
// MOD確認
constexpr long double start_temp = 100,end_temp = 30,TIME_LIMIT = 2.0;
ll dx[4]{-1,0,1,0};
ll dy[4]{0,-1,0,1};
char D[4]{'L','U','R','D'};
ll N = 50,SX,SY;
ll feild[60][60];
ll point[2510];
ll done[2510];
string ans_command = "";
ll ans_score = 0;
bool in_range(ll x,ll y){
return (0 <= y && y < N && 0 <= x && x < N);
}
void dfs(ll now_y,ll now_x,ll now_score,string now_command){
ll now_tile = feild[now_y][now_x];
ll p = point[now_tile];
done[now_tile] = 1;
now_score += p;
if(!in_range(now_y,now_x)){
cout << "BAD" << endl;
}
if(now_score > ans_score){
ans_score = now_score;
ans_command = now_command;
}
if(getTime(start_cycle) > TIME_LIMIT) return;
rep(i,4){
ll next_y = now_y + dy[i],next_x = now_x + dx[i];
ll next_tile = feild[next_y][next_x];
if(!in_range(next_y,next_x) || done[next_tile]) continue;
string next_command = now_command + D[i];
dfs(next_y,next_x,now_score,next_command);
}
done[now_tile] = 0;
now_score -= p;
return ;
}
int main(){
cin >> SY >> SX;
rep(i,N) rep(j,N) cin >> feild[i][j];
rep(i,N) rep(j,N){
ll p;
cin >> p;
point[feild[i][j]] += p;
}
start_cycle = getCycle();
dfs(SY,SX,0,"");
cout << ans_command << endl;
} |
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
using namespace std;
constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;
//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<int>v(N * 2);
for (auto& i : v)cin >> i;
priority_queue<int, vector<int>, greater<int>>PQ;
for (int i = N, j = N - 1; i < 2 * N; i++, j--) {
PQ.push(v[i]);
PQ.push(v[j]);
PQ.pop();
}
long long int ans = 0;
while (!PQ.empty()) {
ans += PQ.top();
PQ.pop();
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
void read (int &x) {
char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
} const int N = 2e5 + 5;
int n; pair<int, int> a[N * 2];
struct tree {
int c[N << 2], tag[N << 2];
#define ls (p << 1)
#define rs (p << 1 | 1)
void push_down (int p) {
c[ls] += tag[p], c[rs] += tag[p];
tag[ls] += tag[p], tag[rs] += tag[p]; tag[p] = 0;
}
void modify (int p, int l, int r, int ql, int qr, int v) {
if (ql <= l && r <= qr) {
c[p] += v, tag[p] += v; return;
}
int mid (l + r >> 1); push_down (p);
if (ql <= mid) modify (ls, l, mid, ql, qr, v);
if (qr > mid) modify (rs, mid + 1, r, ql, qr, v);
c[p] = min (c[ls], c[rs]);
}
int query (int p, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return c[p];
int mid (l + r >> 1), s = 1e9; push_down (p);
if (ql <= mid) s = query (ls, l, mid, ql, qr);
if (qr > mid) s = min (s, query (rs, mid + 1, r, ql, qr));
return s;
}
} qwq;
signed main() {
read (n); int c = 0;
for (int i = n, x; i >= 1; --i)
read (x), a[++c] = {x, i};
for (int i = 1, x; i <= n; ++i)
read (x), a[++c] = {x, i};
sort (a + 1, a + c + 1);
for (int i = 1; i <= n; ++i) qwq.modify (1, 1, n, i, i, i);
long long res = 0;
for (int i = c, s = n; i >= 1 && s; --i) {
int x = a[i].first, y = a[i].second;
if (qwq.query (1, 1, n, y, n) >= 1) {
--s, res += x, qwq.modify (1, 1, n, y, n, -1);
}
}
return printf ("%lld\n", res), 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<vector<ll>>adj(200010);
ll vis[200010];
vector<ll>ans;
map<ll,ll>mp;
ll c[200010];
void dfs(ll u)
{
vis[u]=1;
mp[c[u]]++;
for(auto it=adj[u].begin();it!=adj[u].end();it++)
{
if(!vis[*it])
{
if(mp[c[*it]]==0)
ans.push_back(*it);
dfs(*it);
}
}
mp[c[u]]--;
}
int main()
{
ll n;
cin>>n;
ll i;
for(i=1;i<=n;i++)
{
cin>>c[i];
}
for(ll i=0;i<n-1;i++)
{
ll x,y;
cin>>x>>y;
adj[x].push_back(y);
adj[y].push_back(x);
}
dfs(1);
ans.push_back(1);
sort(ans.begin(),ans.end());
for(i=0;i<ans.size();i++)
cout<<ans[i]<<endl;
}
| #include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define ice(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
vector<vector<int>> adj(100001);
vector<int> color(100001);
vector<bool> good(100001);
void dfs(int n, multiset<int>& s, int m = -1) {
if (!s.count(color[n])) {
good[n] = true;
}
s.insert(color[n]);
for (int p : adj[n]) {
if (p != m) {
dfs(p, s, n);
}
}
s.erase(s.find(color[n]));
}
void solve() {
int n; cin >> n;
ice (i, 0, n) {
cin >> color[i];
}
ice (i, 0, n - 1) {
int a, b; cin >> a >> b; a--; b--;
adj[a].pb(b); adj[b].pb(a);
}
multiset<int> s;
dfs(0, s);
ice (i, 0, n) {
if (good[i]) {
cout << i + 1 << '\n';
}
}
}
int main() {
solve();
} |
#include<bits/stdc++.h>
using namespace std;
// modpow
template<class T> T modpow(T b,T p,T m){
T rt = 1; b %= m;
while(p>0){
if(p&1) rt = (rt*b)%m;
b = (b*b)%m; p>>=1;
}
return rt;
}
// small mod combination
// # f,g を次のようにする
// - f[i] : i が何回 MOD で割れるか. これを f_pre とする
// - g[i] : i の MOD で割れない部分の MOD での余り. これを g_pre とする
// # その後, f,g を次のようにする
// - f[i] : i! が何回 MOD で割れるか
// * f[i] = f_pre[1]+...+f_pre[i] 累積和
// - g[i] : i! が MOD で割れない部分の MOD での余り
// * g[i] = g_pre[1]*...*g_pre[i] 累積積(mod)
const int64_t MOD = 3;
const int64_t fMAX = 400007;
int64_t f[fMAX+1],g[fMAX+1],g_inv[fMAX+1];
void init(){
for(int64_t i=1,tmp;i<=fMAX;i++){
tmp = i;
while(tmp%3==0){
tmp /= 3; f[i]++;
}
g[i] = tmp%3;
}
g[0]=1;
for(int64_t i=1;i<=fMAX;i++)
f[i] += f[i-1];
for(int64_t i=1;i<=fMAX;i++)
g[i] = g[i]*g[i-1]%MOD;
for(int64_t i=0;i<=fMAX;i++)
g_inv[i] = modpow(g[i],MOD-2,MOD);
return;
}
int64_t modcomb_s(int64_t n,int64_t r,int64_t m){
if(f[n]-f[r]-f[n-r]>0) return 0;
return g[n]*g_inv[r]*g_inv[n-r]%m;
}
int main(){
std::cin.tie(nullptr);
std::cout.tie(nullptr);
ios::sync_with_stdio(false);
int64_t n; cin >> n;
string s; cin >> s;
init();
vector<int64_t> a(n);
for(int64_t i=0;i<n;i++){
if(s[i]=='B') a[i] = 0;
else if(s[i]=='R') a[i] = 1;
else if(s[i]=='W') a[i] = 2;
}
int64_t ans=0;
for(int64_t i=0;i<n;i++){
ans += a[i] * modcomb_s(n-1,i,MOD);
ans %= MOD;
}
ans = ans * modpow((int64_t)2,n-1,MOD) % MOD;
string ans_s;
if(ans==0) ans_s='B';
else if(ans==1) ans_s='R';
else if(ans==2) ans_s='W';
cout << ans_s << "\n";
return 0;
}
| // KNOW NOTHING!
#include <bits/stdc++.h>
#define ll long long int
#define F(i,j,k,in) for(int i=j;i<k;i+=in)
#define DF(i,j,k,in) for(int i=j;i>=k;i-=in)
#define feach(it,l) for (auto it = l.begin(); it != l.end(); ++it)
#define fall(a) a.begin(),a.end()
#define sz(x) (int)x.size()
#define szs(x) (int)x.length()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define eq equal_range
#define fs first
#define ss second
#define ins insert
#define mkp make_pair
#define endl "\n"
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef queue<int> qin;
typedef deque<ll> dqll;
typedef deque<int> dqint;
typedef priority_queue<ll> pqll;
typedef priority_queue<int> pqin;
typedef map<ll,ll> mpll;
typedef map<int,int> mpin;
typedef pair<ll,ll> pll;
typedef pair<int,int> pin;
const ll MOD=1000000007;
const long double PI=3.1415926535897932384626433832795;
ll pwr(ll b,ll p){ll res=1;while(p){if(p&1) {res*=b; p--;}else{b*=b; p>>=1;}}return res;}
ll mpwr(ll b,ll p){ll res=1;while(p){if(p&1) {res=(res*b)%MOD; p--;}else{b=(b*b)%MOD; p>>=1;}}return res;}
int msb (int n) {return 31-__builtin_clz(n);}
ll msbl (ll n) {return 63-__builtin_clzll(n);}
int lsb (int n) {return __builtin_ctz(n);}
ll lsbl (ll n) {return __builtin_ctzll(n);}
int setbit (int n) {return __builtin_popcount(n);}
ll setbitl (ll n) {return __builtin_popcountll(n);}
int main()
{
/*
freopen ("input.txt","r","stdin");
freopen ("output.txt","w","stdout");
*/
ios_base::sync_with_stdio(false) , cin.tie(NULL) , cout.tie(NULL);
int h,w; cin>>h>>w;
int x, y; cin>>x>>y;
vector<string> v (h);
F (i,0,h,1) cin>>v[i];
--x; --y;
int cnt=0;
DF (i,y,0,1) {
if (v[x][i]=='#') break;
else ++cnt;
}
F (i,y+1,w,1) {
if (v[x][i]=='#') break;
else ++cnt;
}
DF (i,x-1,0,1) {
if (v[i][y]=='#') break;
else ++cnt;
}
F (i,x+1,h,1) {
if (v[i][y]=='#') break;
else ++cnt;
}
cout<<cnt;
return 0;
}
/* What we know is a drop , but what we don't know is an ocean !*/ |
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
#define rep(i,n) for (ll i = 0; i < (n) ; i++)
#define rep2(i,n,i2,n2) for (ll i = 0; i < (n) ; i++) for (ll i2 = 0; i2 < (n2) ; i2++)
#define incRepFT(i,s,n) for (ll i = (s); i <= (n) ; i++)
#define decRepFT(i,s,n) for (ll i = (s); i >= (n) ; i--)
#define INF 1e9
#define llINF 1e18
#define base10_4 10000 //1e4
#define base10_5 100000 //1e5
#define base10_6 1000000 //1e6
#define base10_7 10000000 //1e7
#define base10_8 100000000 //1e8
#define base10_9 1000000000 //1e9
#define MOD 1000000007
#define pb push_back
#define ll long long
#define ld long double
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vstr vector<string>
#define vvstr vector<vector<string>>
typedef pair<ll,ll> P;
// #include <iomanip>
// cout << fixed << setprecision(15) << y << endl;
// for(char c : S)
//min({a1, a2, ..., an})
//max({a1, a2, ..., an})
//swap(a, b)
//S.substr(si)
// sort <--> reverse
//count(v.begin(), v.end(), x)
// char t - 'a'
// char t - '0'
//xを2進数にした時の1の数
//__builtin_popcount(x)
//__builtin_popcountll(x)
ll A;
ll B;
ll C;
ll D;
ll N;
ll M;
ll K;
ll T;
ll H;
ll W;
ll X;
ll Y;
ll Z;
string S;
ll ltmp;
string stmp;
double dtmp;
ll llmin(ll a,ll b){
if(a>=b) return b;
return a;
}
ll llmax(ll a,ll b){
if(a<=b) return b;
return a;
}
P d_move[4] = {
P(0 , 1),P(0 , -1),P(1 , 0),P(-1 , 0)//,P(1 , 1),P(1 , -1),P(-1 , 1),P(-1 , -1)
};
//for(P drc : d_move)
double double_hosei = 1000000; //求められる精度分補正をかけておく
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N;
ll ans = 0;
for( ll v1 = 1 ; v1 <=N ; v1++ ){
S = to_string(v1);
ll keta = pow(10,S.size());
ltmp = v1 * keta + v1;
if(ltmp>N) break;
ans++;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
ll f(ll x){
string s = to_string(x);
return stoll(s+s);
}
int main(){
ll n;
cin >> n;
ll x = 1;
// n以下までループする
while(f(x) <= n) ++x;
ll ans = x-1;
cout << ans <<endl;
return 0;
} |
#include <iostream>
#include <tuple>
#include <vector>
#include <algorithm>
#include <unordered_map>
// using declaration, alias
using llong = long long;
using ldouble = long double;
// constant
constexpr auto INF_LL = 0x7FFFFFFFFFFFFFFFLL;
// constexpr auto MOD = 0LL;
namespace io {
template <class T = llong> auto in() {T a;std::cin >> a;return a;}
template <class T = llong> auto in(int n) {std::vector<T> v(n); for (auto& vi : v) vi = in<T>(); return v;}
template <class T = llong> auto in(int n, int m) {std::vector<std::vector<T>> v(n, std::vector<T>(m)); for (auto& vi : v) for (auto& vij : vi) vij = in<T>(); return v;}
template <size_t... S, class... T> void in_var(std::index_sequence<S...>, std::tuple<T...>& t) {std::initializer_list<int>{(std::get<S>(t) = in<T>(), 0)...};}
template <size_t... S, class... T> void in_vec(std::index_sequence<S...>, std::tuple<std::vector<T>...>& t) {std::initializer_list<int>{(std::get<S>(t).emplace_back(in<T>()), 0)...};}
template <class T, class U, class... V> auto in() {std::tuple<T, U, V...> t; in_var(std::index_sequence_for<T, U, V...>(), t); return t;}
template <class T, class U, class... V> auto in(int n) {std::tuple<std::vector<T>, std::vector<U>, std::vector<V>...> t; for (auto i = 0; i < n; i++) in_vec(std::index_sequence_for<T, U, V...>(), t); return t;}
template <int N, class T = llong, class... U> auto in() {if constexpr (N > 1) return in<N - 1, T, T, U...>(); else return in<T, U...>();}
template <int N, class T = llong, class... U> auto in(int n) {if constexpr (N > 1) return in<N - 1, T, T, U...>(n); else return in<T, U...>(n);}
template <class T> void out(T a) {std::cout << a << "\n";}
template <class T, class... U> void out(T a, U... b) {std::cout << a << " "; out(b...);}
}
void dfs(llong node, llong node_b, std::vector<llong>& color, std::unordered_map<llong, std::vector<llong>>& graph, std::vector<llong>& count, std::vector<llong>& result) {
if (count[color[node - 1] - 1] == 0) result.emplace_back(node);
count[color[node - 1] - 1]++;
for (auto to : graph[node]) {
if (to != node_b) dfs(to, node, color, graph, count, result);
}
count[color[node - 1] - 1]--;
}
int main() {
// input
auto N = io::in();
auto C = io::in(N);
auto [A, B] = io::in<2>(N - 1);
// solve
std::unordered_map<llong, std::vector<llong>> graph;
for (auto i = 0LL; i < N - 1; i++) {
graph[A[i]].emplace_back(B[i]);
graph[B[i]].emplace_back(A[i]);
}
std::vector<llong> count(100000, 0LL);
std::vector<llong> result;
dfs(1, 0, C, graph, count, result);
std::sort(result.begin(), result.end());
// output
for (auto r : result) io::out(r);
} | #include <bits/stdc++.h>
using namespace std;
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); }
template <class A, class B> inline void chmod(A &a, const B b) { a = a%b; return; }
void dfs(int node);
int n, c[100000], a, b, pal[100000], pass[100000];
vector<int> vec[100000];
int main() {
cin >> n;
for (int i=0; i<n; i++) {
cin >> c[i];
c[i]--;
}
for (int i=0; i<n-1; i++) {
cin >> a >> b;
a--, b--;
vec[a].push_back(b);
vec[b].push_back(a);
}
dfs(0);
for (int i=0; i<n; i++) {
if (c[i] > -1) {
cout << i+1 << endl;
}
}
return (0);
}
void dfs(int node) {
if ( pass[node] == 0 ) {
pass[node] = 1;
pal[c[node]]++;
for (int i=0; i<vec[node].size(); i++) {
dfs(vec[node][i]);
}
pal[c[node]]--;
if ( pal[c[node]] > 0 ) {
c[node] = -1;
}
}
return;
} |
#include <bits/stdc++.h>
#define ALL(s) (s).begin(),(s).end()
using namespace std;
using ll=long long;
ll M=1000000007LL;
int main(void)
{
ll h,w,ans=0; cin>>h>>w; vector<string> s(h);
for(ll i=0;i<h;i++) cin>>s[i];
for(ll i=0;i<h;i++)
{
for(ll j=0;j<w-1;j++)
{
if(s[i][j]==s[i][j+1]&&s[i][j]=='.') ans++;
}
}
for(ll i=0;i<w;i++)
{
for(ll j=0;j<h-1;j++)
{
if(s[j][i]==s[j+1][i]&&s[j][i]=='.') ans++;
}
}
cout<<ans<<endl;
}
| #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <tuple>
#include <random>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
const ll SIZE = 1e6 * 2 + 10, INF = 1e9 * 1e9 + 10;
ll symb(char a) {
if (a == 'A') return 0;
if (a == 'T') return 1;
if (a == 'C') return 2;
if (a == 'G') return 3;
}
int main()
{
fastInp;
string s;
ll n;
cin >> n >> s;
ll ans = 0;
for (int i = 0; i < s.size(); i++) {
vector<ll> k(4);
for (int j = i; j < s.size(); j++) {
k[symb(s[j])]++;
if (k[0] == k[1] && k[2] == k[3]) ans++;
}
}
cout << ans;
return 0;
} |
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <math.h>
#include <random>
#include <chrono>
#include <assert.h>
#include <ctime>
using namespace std;
#define x first
#define y second
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define all(x) x.begin(),x.end()
#define SZ(x) int(x.size())
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = b-1; i >= a; i--)
#define DBG(x) cerr << (#x) << "=" << x << "\n";
#define ll long long
#define inf 1000000007
#define mod 1000000007
#define N 200005
template<typename U, typename V> void Min(U &a, const V & b){if(a > b)a = b;}
template<typename U, typename V> void Max(U &a, const V & b){if(a < b)a = b;}
template<typename U, typename V> void add(U &a, const V & b){a = (a + b) % mod;}
template<typename U> U gcd(U a, U b){
if(a == 0)return b;
if(b == 0)return a;
if(a >= b)return gcd(a % b, b);
else return gcd(a, b % a);
}
int pow(int a, int b){
int ans = 1;
while(b){
if(b & 1)ans = 1LL * ans * a % mod;
a = 1LL * a * a % mod;
b >>= 1;
}
return ans;
}
int pow(int a, ll b, int c){
int ans = 1;
while(b){
if(b & 1)ans = 1LL * ans * a % c;
a = 1LL * a * a % c;
b >>= 1;
}
return ans;
}
int a[N], b[N];
int main(){
int ca = 0, T, i, j, k, K, n, m;
scanf("%d", &n);
rep(i, 0, n) scanf("%d", &a[i]);
reverse(a, a+n);
rep(i, 0, n) scanf("%d", &b[i]);
ll ans = 0, tot = 0;;
set<pii> s;
rep(i, 0, n){
tot += a[i] + b[i];
s.insert({a[i], i});
s.insert({b[i], i+n});
ans += s.begin()->x;
s.erase(s.begin());
}
printf("%lld\n", tot - ans);
}
| #include <bits/stdc++.h>
// #define ONLINE_JUDGE 1
using namespace std;
#define ll long long int
#define endl '\n'
#define vi vector<int>
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define f(var,start,end) for(int var = start; var<=end; var++)
#define all(x) x.begin(),x.end()
#define v(x) vector<x>
#define rall(x) x.rbegin(),x.rend()
#define mod 1000000007LL
const ll INF = (ll)(1e9);
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...);}
#ifdef _Deep_M
#define dbg(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
// fflush(stdout) or cout.flush() in C ++;
void solver();
int main()
{
#ifdef _Deep_M
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#else
ios::sync_with_stdio(0);cin.tie(0);
#endif
ll t;
t = 1;
// cin >> t;
for(int i = 1; i<=t; i++)
{
// cout << "Case #"<<i<<": ";//google kickstart;
solver();
}
return 0;
}
void solver()
{
ll deep;
ll n;
ll k;
cin >> n>> k;
map<ll,ll> arr;
for(int i= 0; i<n; i++)
{
ll x,y;
cin >> x>> y;
arr[x]+=y;
}
ll ans = 0;
for(auto i: arr)
{
dbg(i);
if(i.first-ans<=k)
{
k-=i.first;
k+=i.second;
ans+=i.first;
}
else
{
break;
}
}
ans+=k;
cout << ans<< endl;
}
|
#include <bits/stdc++.h>
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
//const long long MOD = 998244353;
const long long LINF = 1e18;
using namespace std;
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE" ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible" ) << endl
#define dump(x) cout << #x << " = " << (x) << endl
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define COUT(x) cout<<(x)<<endl
#define SCOUT(x) cout<<(x)<<" "
#define VECCOUT(x) for(auto&youso_: (x) )cout<<right<<setw(10)<<youso_<<" ";cout<<endl
#define ENDL cout<<endl
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) long long __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define mp make_pair
#define PQ priority_queue<long long>
#define PQG priority_queue<long long,vector<long long>,greater<long long> >
typedef long long ll;
typedef vector<long long> vl;
typedef vector<long long> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef pair<long long, long long> pll;
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
cin>>head;
CINT(move(tail)...);
}
template<class T>
void mod(T &x) {
x %= MOD;
x += MOD;
x %= MOD;
}
ll GCD(ll a, ll b) {
if(b == 0) return a;
else return GCD(b, a%b);
}
struct COMB{
vl fact, fact_inv, inv;
void init_nCk(long long SIZE) {
fact.resize(SIZE + 5);
fact_inv.resize(SIZE + 5);
inv.resize(SIZE + 5);
fact.at(0) = fact.at(1) = fact_inv.at(0) = fact_inv.at(1) = inv.at(1) = 1;
for(long long i = 2; i < SIZE + 5; i++) {
fact.at(i) = fact.at(i - 1)*i%MOD;
inv.at(i) = MOD - inv.at(MOD%i)*(MOD/i)%MOD;
fact_inv.at(i) = fact_inv.at(i - 1)*inv.at(i)%MOD;
}
}
long long nCk (long long n, long long k) {
assert(!(n < k));
assert(!(n < 0 || k < 0));
return fact.at(n)*(fact_inv.at(k)*fact_inv.at(n - k)%MOD)%MOD;
}
};
ll extGCD(ll a, ll b, ll &x, ll &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a%b, y, x);
y -= a/b*x;
return d;
}
void Main() {
ll ans = 0;
LCIN(N);
vl V(2*N);
VECCIN(V);
PQG p;
ans = accumulate(V.begin(),V.end(), 0LL);
for(int i = 0; i < N; i++) {
p.push(V.at(N - 1 - i));
p.push(V.at(N + i));
ans -= p.top();
p.pop();
}
cout << ans << endl;
}
int main() {
cout << fixed << setprecision(15);
Main();
return 0;
} | #include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define x first
#define y second
using namespace std;
typedef pair<int, int> pii;
const int md = 998244353;
const int N = 2e5 + 7;
int n, m, f[2][18];
int prvdiv[N];
void addmod(int &a, int b) {
a=(a+b)%md;
}
void mulmod(int &a, int b) {
a=(1ll*a*b)%md;
}
int main(){
// freopen("input.inp", "r", stdin);
// freopen("output.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i <= 17; ++i) {
f[0][i] = 1;
}
for(int i = 2; i <= n; ++i) {
int prv = i%2;
int cur = (i+1)%2;
f[cur][0] = f[prv][0];
for(int j = 1; j <= 17; ++j) {
f[cur][j] = f[cur][j-1];
addmod(f[cur][j], f[prv][j]);
}
}
int cur = (n+1)%2;
for(int i = 2; i <= m; ++i) {
if (prvdiv[i] == 0) {
prvdiv[i] = i;
for(long long j = 1ll*i*i; j <= m; j += i) {
prvdiv[j] = i;
}
}
}
int ans = 1;
for(int i = 2; i <= m; ++i) {
int s = 1;
int tmp = i;
while (tmp > 1) {
int cnt = 0;
int c = prvdiv[tmp];
while (tmp % c == 0) {
tmp /= c;
++cnt;
}
mulmod(s, f[cur][cnt]);
}
addmod(ans, s);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int(i)=0;i<(n);i++)
#define SIZE 1001
typedef long long ll;
int h,w,ans=0;
typedef pair<int,int> P;
vector<string> s(SIZE);
int main(){
cin>>h>>w;
REP(i,h) cin>>s.at(i);
REP(i,h) REP(j,w){
if(j+1<w && s.at(i).at(j)=='.'&&s.at(i).at(j+1)=='.') ans++;
if(i+1<h && s.at(i).at(j)=='.'&&s.at(i+1).at(j)=='.') ans++;
}
cout<<ans;
}
| #include <cstdio>
#include <iostream>
using namespace std;
char ar[101][101];
int main()
{
int h, w;
scanf("%d%d", &h, &w);
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> ar[i][j];
}
}
int xdir[4] = { 0, 1, 0, -1 };
int ydir[4] = { 1, 0, -1, 0 };
int cnt = 0;
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
if (ar[i][j] == '.')
{
for (int aa = 0; aa < 4; aa++)
{
int nx = i + xdir[aa];
int ny = j + ydir[aa];
if (0 <= nx && nx < h && 0 <= ny && ny < w)
{
if (ar[nx][ny] == '.') cnt++;
}
}
}
}
}
printf("%d", cnt / 2);
} |
#include<bits/stdc++.h>
using namespace std;
#define fi(a,b) for(int i=a;i<b;i++)
#define fj(a,b) for(int j=a;j<b;j++)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define ull unsigned long long
#define bp(x) __builtin_popcount(x)
#define pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define getMax(x) max_element(x.begin(),x.end())
#define getMin(x) min_element(x.begin(),x.end())
#define endl "\n"
typedef vector<int> vi;
typedef vector< pair<int,int> > vii;
typedef vector<long long> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector< pair<ll,ll> > vll;
//int dx[]={1,0,-1,0};
//int dy[]={0,1,0,-1};
//int dx[]={-1,0,1,1,1,0,-1,-1};
//int dy[]={-1,-1,-1,0,1,1,1,0};
void nikal_pehli_fursat_mai(){
ll n;
cin>>n;
vl v(n);
vl h(401, 0);
fi(0, n) {
cin>>v[i];
}
ll ans = 0;
h[v[0]+200]++;
fi(1, n){
fj(0, 401){
ans += abs(v[i]-(j-200))*abs(v[i]-(j-200))*h[j];
}
h[v[i]+200]++;
}
cout<<ans<<endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
int tc=1;
// cin>>tc;
while(tc--){
nikal_pehli_fursat_mai();
}
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n; ll k; cin >> n >> k;
ll dp[4][3000005] = {0};
dp[0][0] = 1;
for(int i = 0; i < 3;i++){
for(int j = 0; j <= i * n;j++){
dp[i + 1][j + 1] += dp[i][j];
dp[i + 1][j + n + 1] -= dp[i][j];
}
for(int j = 1; j <= (i + 1) * n; j++){
dp[i + 1][j] += dp[i + 1][j - 1];
}
}
int x;
for(int i = 3; i <= 3 * n; i++){
if(k <= dp[3][i]){
x = i; break;
}else{
k -= dp[3][i];
}
}
for(int i = 1; i <= n; i++){
int j_mi = max(1, x - i - n);
int j_ma = min(n, x - i - 1);
if(j_mi > j_ma) continue;
if(k > (j_ma - j_mi + 1)){
k -= (j_ma - j_mi + 1);
continue;
}
int y = j_mi + k - 1;
int z = x - i - y;
cout << i << " " << y << " " << z << endl;
return 0;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n);i++)
using ll = long long;
const ll INF = 1e9+7;
const ll mod=1000000007;
using P = pair<ll, ll>;
int main(){
int n;
string s;
cin >> n >> s;
string t ="";
int ans =0;
rep(i,n){
t += s[i];
while(t.size()>=3){
if(t.substr(t.size()-3,3)=="fox"){
t = t.substr(0,t.size()-3);
ans++;
}
else{
break;
}
}
}
cout << n - ans*3 << endl;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FIO ios::sync_with_stdio(false); cin.tie(nullptr)
#define TC(t) int t; cin >> t; for(int i = 1; i <= t; i++)
#define deb(x) cerr << #x << " = " << x << endl
#define deb2(x, y) cerr << #x << " = " << x << ", " << #y << " = " << y << endl
#define f0(i, a, n) for(i = a; i < n; i++)
#define f1(i, a, n) for(i = a; i <= n; i++)
#define ini(x, y) memset(x, y, sizeof(x))
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define ll long long int
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define M 1000000007
#define endl '\n'
#define bits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define multi_ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
const int N = 1e5 + 1;
const int MX = 1e9;
const ll INF = 1e18;
using namespace std;
using namespace __gnu_pbds;
inline ll uceil(ll a,ll b) {return (a % b ? a / b + 1 : a / b);}
inline ll mod(ll x) {return ( (x % M + M) % M );}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {for(auto& x : v) os << x << " "; return os << '\n';}
template<typename... T> void in(T&... args) {((cin >> args), ...);}
template<typename... T> void out(T&&... args) {((cout << args << endl), ...);}
template<typename... T> void out2(T&&... args) {((cout << args << " "), ...);}
void solve() {
ll n, i;
string s;
cin >> n >> s;
i = 0;
while(i < n-2) {
if(s[i] == 'f' && s[i+1] == 'o' && s[i+2] == 'x') {
// deb(i);
s.erase(s.begin()+i);
s.erase(s.begin()+i);
s.erase(s.begin()+i);
if(i >= 2) i-=2;
else if(i >= 1) i-=1;
n -= 3;
// deb(i);
// deb(s);
} else i++;
}
out(sz(s));
}
int main () {
FIO;
// TC(t)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
int main(){
int N;
string S;
cin >> N >> S;
map<char,int> ma;
map<int,char> am;
ma['R'] = 0; am[0] = 'R';
ma['B'] = 1; am[1] = 'B';
ma['W'] = 2; am[2] = 'W';
vector<int> f(N,1),c(N,0);
FOR(i,1,N){
int k = i;
while(k%3==0){
k /= 3;
c[i]++;
}
f[i] = f[i-1] * k % 3;
c[i] += c[i-1];
}
int ans = 0;
FOR(i,0,N){
ans += f[N-1] * f[i] * f[N-1-i] * ma[S[i]] * ( (c[N-1]-c[i]-c[N-1-i]) == 0 );
ans %= 3;
}
if(N%2 == 0) ans = -ans;
ans = ( (ans % 3) + 3 ) % 3;
cout << am[ans] << endl;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.first << "," << P.second << ")";}
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector< pii >
const int mod=3;
inline int mul(int a,int b){return (a*1ll*b)%mod;}
inline int add(int a,int b){a+=b;if(a>=mod)a-=mod;return a;}
inline int sub(int a,int b){a-=b;if(a<0)a+=mod;return a;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,mod-2);}
inline void modadd(int &a,int &b){a+=b;if(a>=mod)a-=mod;}
const int M = 4e5 + 12;
int cal(int x){
int ret = 0;
while(x > 0) {
ret += x/3;
x = x/3;
}
return ret;
}
int P[M];
int fac[M];
int ifac[M];
int red[M];
void pre() {
fac[0] = 1;
P[0] = 1;
ifac[0] = 1;
for(int i=1; i<M; i++) {
int x = i;
P[i] = cal(i);
while((x%3) == 0) x/=3;
red[i] = x;
fac[i] = mul(x, fac[i-1]);
}
ifac[M-1] = inv(fac[M-1]);
for(int i=M-2;i>0;i--){
ifac[i] = mul(ifac[i+1], red[i+1]);
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<setprecision(25);
int n; cin>>n;
// vi c(n);
pre();
int ans = 0;
for(int i=1;i<=n;i++){
char x; cin>>x;
int y;
if(x=='B') y = 0;
else if(x=='W') y = 1;
else if(x=='R') y = 2;
int r = n - i;
int dif = P[n-1] - P[r] - P[n-r-1];
if(dif > 0) continue;
int con = mul(fac[n-1], mul(ifac[r], ifac[n-r-1]));
// trace(n-1, r, fac[n-1], ifac[r], ifac[n-r-1]);
// trace(y, con, ans);
con = mul(con, y);
ans = add(ans, con);
}
if(((n-1) % 2)==1 ) ans = sub(0 , ans);
// trace(ans);
if( ans == 0) cout<<'B';
else if (ans == 1) cout<<'W';
else cout<<'R';
} |
#include<bits/stdc++.h>
#define LL long long
#define SZ(x) (int)x.size()-1
#define F(i,a,b) for (int i=a;i<=b;++i)
#define DF(i,a,b) for (int i=a;i>=b;--i)
#define pb push_back
#define ms(a,b) memset(a,b,sizeof a)
using namespace std;
LL read(){
char ch=getchar(); LL w=1,c=0;
for (;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
for (;isdigit(ch);ch=getchar()) c=(c<<3)+(c<<1)+(ch^48);
return w*c;
}
struct Li{
LL l,r;
};
bool cmp(Li A,Li B){
return A.l<B.l;
}
LL B,C;
Li a[10];
int main(){
cin>>B>>C;
if (C==1){
cout<<(B ? 2 : 1)<<"\n";
return 0;
}
a[0]=(Li){B-C/2,B},a[2]=(Li){-B-(C-1)/2,-B};
a[1]=(Li){-B,-(B-(C-1)/2)},a[3]=(Li){B,B+(C-2)/2};
sort(a,a+4,cmp);
LL ans=0,pos=a[0].r,lp=a[0].l;
F(i,1,3){
if (a[i].l>pos){
ans=pos-lp+1;
pos=a[i].r;
lp=a[i].l;
}
else pos=max(pos,a[i].r);
}
ans+=pos-lp+1;
cout<<ans<<'\n';
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
long long int b, c; cin >> b >> c;
long long int ansin = 0, ansout = 0;
long long int C = c, B = b;
long long int p1 = 0, p2 = 1;
p1 = 2*abs(b)+1;
if(b<0){
if(c>0){
C--;
p2++;
}
p2 += C-1;
}
else{
if(c>0) p2++;
p2 += C-1;
}
ansin = min(p1, p2);
if(b>0){
b *= -1;
c--;
}
ansout = c-1;
if(c<=0) ansout = 0;
cout << ansin + ansout << endl;
} |
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int h,w,a,ans=0;
bool f[16][16];
inline void dfs(int x,int y,int n) {
if(y>w) {
y=1,
++x;
}
if(x==h&&y==w) {
if(n==0) {
++ans;
}
return;
}
dfs(x,y+1,n);
if(n>0&&f[x][y]) {
if(f[x+1][y]) {
f[x+1][y]=f[x][y]=false;
dfs(x,y+1,n-1);
f[x+1][y]=f[x][y]=true;
}
if(f[x][y+1]) {
f[x][y+1]=f[x][y]=false;
dfs(x,y+1,n-1);
f[x][y+1]=f[x][y]=true;
}
}
return;
}
signed main() {
memset(f,false,sizeof f);
cin>>h>>w>>a;
for(int i=1; i<=h; ++i) {
for(int j=1; j<=w; ++j) {
f[i][j]=true;
}
}
dfs(1,1,a);
cout<<ans;
return 0;
} | #include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
int H, W, A, B, ans = 0;
void dfs(int i, long long bit, int a, int b) {
if (i == H*W) {
ans++;
return;
}
if (bit & (1 << i)) {
dfs(i + 1, bit, a, b);
return;
}
bit |= (1 << i);
if (a) {
if ((i + 1) <= H*W && ((i + 1) % W) != 0 && (~bit & 1 << (i + 1))) {
dfs(i + 1, bit | (1 << (i + 1)), a-1, b);
}
if ((i + W) <= (W*H)) {
dfs(i + 1, bit | (1 << (i + W)), a-1, b);
}
}
if (b) {
dfs(i + 1, bit, a, b-1);
}
}
int main()
{
cin >> H >> W >> A >> B;
dfs(0, 0, A, B);
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
int mod=1e9+7;
int h,w,a,b;
int ans=0;
int mat[20][20];
void calc(int mark){
if(mark==a){
ans++;
}
else{
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(i!=h && mat[i][j]==0 && mat[i+1][j]==0){
mat[i][j]=1;
mat[i+1][j]=1;
calc(mark+1);
mat[i][j]=0;
mat[i+1][j]=0;
}
if(j!=w && mat[i][j]==0 && mat[i][j+1]==0){
mat[i][j]=1;
mat[i][j+1]=1;
calc(mark+1);
mat[i][j]=0;
mat[i][j+1]=0;
}
}
}
}
}
signed main(){
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
cin>>h>>w>>a>>b;
calc(0);
for(int i=1;i<=a;i++){
ans=ans/i;
}
cout<<ans<<endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int h,w;
bool used[16][16];
int Dfs(int i, int j, int a, int b){
if(a < 0 || b < 0) return 0;
if(j == w) j = 0, i++;
if(i == h) return 1;
if(used[i][j]) return Dfs(i, j + 1, a, b);
int res = 0;
used[i][j] = 1;
res += Dfs(i, j + 1, a, b - 1);
if(j + 1 < w && !used[i][j + 1]){
used[i][j + 1] = 1;
res += Dfs(i, j + 1, a - 1, b);
used[i][j + 1] = false;
}
if(i + 1 < h && !used[i + 1][j]){
used[i + 1][j] = 1;
res += Dfs(i, j + 1, a - 1, b);
used[i + 1][j] = false;
}
used[i][j] = false;
return res;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int a,b; cin >> h >> w >> a >> b;
cout << Dfs(0,0,a,b) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define P pair<ll, ll>
#define all(a) (a).begin(), (a).end()
int main(){
ll n;
cin >> n;
vector<ll> a(n), b(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
vector<bool> x(1001, true);
rep(i, n){
drep(j, 0, a[i]-1) x[j] = false;
drep(j, b[i]+1, 1000) x[j] = false;
}
ll ans = 0;
rep(i, x.size()) if(x[i]) ans++;
cout << ans << endl;
} | #include<string>
#include<iostream>
int main(){
int N,A[100],B[100],z=1000,w=0;
std::cin>>N;
for(int f=0;f<N;f++){
std::cin>>A[f];
}
for(int i=0;i<N;i++){
std::cin>>B[i];
}
for(int t=0;t<N;t++){
if(z>=B[t])z=B[t];
if(w<=A[t])w=A[t];
}
if(z>=w)std::cout<<z-w+1;
else std::cout<<0;
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.