code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
//#define _GLIBCXX_DEBUG
#include <iostream>
#include <time.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
#include <functional>
#include <random>
#include <string>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <bits/stdc++.h>
#include <deque>
#include <queue>
#include <list>
/*
vector<int> a(N);
vector<vector<int>> a(N, vector<int>(N));
auto itr = a.begin();
for (auto itr = a.begin(); itr != a.end(); itr++)
{
cout << itr - a.begin() << endl; = 0,1,...,N-1
}
sort(a.begin(), a.end());
reverse(a.begin(), a.end());
*/
/*
list<int> lst;
lst.push_front(i);
lst.push_back(i);
lst.pop_front();
lst.pop_back();
for(auto itr = lst.begin(); itr != lst.end(); ++itr) {
std::cout << *itr << "\n";
}
*/
/*
bool cmp(const std::pair<int, int> &a, const std::pair<int, int> &b)
{
return a.first < b.first;
}
vector<pair<int, int>> p(N);
for (int i = 0; i < N; i++)
{
p[i].first = a[i];
p[i].second = b[i];
}
sort(p.begin(), p.end(), cmp);
for (int i = 0; i < N; i++)
{
a[i] = p[i].first;
b[i] = p[i].second;
}
struct Hara
{
int A, B;
};
bool cmp(const Hara &a, const Hara &b)
{
return a.A < b.A;
}
sort(h.begin(), h.end(), cmp);
*/
/*
std::unordered_set<int> hash;
*/
/*
struct Edge
{
int to;
long long w;
Edge(int to, long long w) : to(to), w(w) {}
};
using Graph = vector<vector<Edge>>;
Graph G(N);
for (int i = 0; i < M; ++i) {
int a, b;
long long w;
cin >> a >> b >> w;
G[a].push_back(Edge(b, w));
}
*/
/*
struct Hara
{
long long a, b;
};
bool operator<(const Hara &hara_1, const Hara &hara_2)
{
return hara_1.b < hara_2.b;
}
*/
using namespace std;
const int MAX = 510000;
const int MOD = 1000000007;
const int INF = 1000000000;
long long 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 % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long 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] % MOD) % MOD;
}
long long GCD(long long m, long long n)
{
if (m < n)
{
swap(m, n);
}
// ベースケース
if (n == 0)
{
return m;
}
// 再帰呼び出し
return GCD(n, m % n);
}
struct Hara
{
string str;
int len;
};
bool operator<(const Hara &hara_1, const Hara &hara_2)
{
return hara_1.len < hara_2.len;
}
int main()
{
int N, M;
cin >> N >> M;
vector<Hara> h(M);
for (int i = 0; i < M; i++)
{
cin >> h[i].str;
h[i].len = h[i].str.length();
}
sort(h.begin(), h.end());
/*
for (int i = 0; i < M; i++)
{
cout << h[i].str << endl;
}
*/
int p = 0;
for (int i = 0; i < N; i++)
{
int len = 0;
while (len < N)
{
if (len + h[p].len <= N)
{
cout << h[p].str;
len += h[p].len;
p++;
}
else
{
len++;
cout << '.';
}
}
cout << endl;
}
} | #include<bits/stdc++.h>
#define REP(i,b,e) for(int i=b;i<e;i++)
std::vector<std::vector<int>> scc_decompose(
std::vector<std::vector<int>> &links,
std::vector<std::vector<int>> &rlinks
){
int n = links.size();
int cnt = 0;
std::vector<int> order;
std::vector<bool> visited(n, false);
std::function<void(int)> dfs = [&](int x){
visited[x] = true;
for(auto to: links[x]) if(!visited[to]) dfs(to);
order.push_back(x);
};
for(int i=0; i<n; i++) if(!visited[i]) dfs(i);
std::reverse(order.begin(), order.end());
cnt = 0;
std::vector<std::vector<int>> scc;
std::fill(visited.begin(), visited.end(), false);
dfs = [&](int x){
visited[x] = true;
for(auto to: rlinks[x]) if(!visited[to]) dfs(to);
scc[cnt].push_back(x);
};
for(auto i: order) if(!visited[i]){
scc.push_back(std::vector<int>());
dfs(i);
cnt++;
}
return scc;
}
std::vector<int> topological_sort(
std::vector<std::vector<int>> &links,
std::vector<std::vector<int>> &scc
){
int n = links.size();
std::vector<int> scc_num(n);
for(int i=0; i<scc.size(); i++){
for(int &v: scc[i]) scc_num[v] = i;
}
std::vector<int> in_cnt(scc.size());
std::vector<std::vector<int>> scc_links(scc.size());
for(int i=0; i<links.size(); i++){
for(int j: links[i]){
if(scc_num[i]!=scc_num[j]){
scc_links[scc_num[i]].push_back(scc_num[j]);
in_cnt[scc_num[j]]++;
}
}
}
std::queue<int> que;
for(int i=0; i<scc.size(); i++) if(in_cnt[i]==0) que.push(i);
std::vector<int> ret;
while(!que.empty()){
int now = que.front();
que.pop();
ret.push_back(now);
for(auto to: scc_links[now]){
if(--in_cnt[to]==0) que.push(to);
}
}
return ret;
}
int main(){
int n, m;
std::cin >> n >> m;
std::string st[m];
REP(i, 0, m) std::cin >> st[i];
std::vector<std::vector<int>> links(m, std::vector<int>());
std::vector<std::vector<int>> rlinks(m, std::vector<int>());
REP(i, 0, m) REP(j, 0, m) {
bool ok = false;
REP(k, 0, st[i].size()){
std::string s1 = st[i].substr(k);
std::string s2 = st[j].substr(0, st[i].size()-k);
if(s1 == s2) ok = true;
}
if(ok){
links[i].push_back(j);
rlinks[j].push_back(i);
}
}
std::vector<std::vector<int>> scc = scc_decompose(links, rlinks);
std::vector<int> order = topological_sort(links, scc);
std::vector<std::string> ss;
for(auto x: order) for(auto v: scc[x]) {
ss.push_back(st[v]);
}
std::string ans[n];
bool used[m] = {};
REP(si, 0, m){
int minrow = 0, crost = 50, min_add_len = ss[si].size();
REP(i, 0, n){
REP(j, 0, ans[i].size()){
std::string s1 = ans[i].substr(j);
std::string s2 = ss[si].substr(0, ans[i].size()-j);
if(s1 != s2) continue;
int pushlen = ss[si].size() - ans[i].size() + j;
if(ans[i].size() + pushlen < n && min_add_len > pushlen){
min_add_len = pushlen;
minrow = i;
crost = j;
}
}
}
crost = std::min<int>(crost, ans[minrow].size());
std::string pushs = ss[si].substr(ans[minrow].size() - crost);
while(minrow < n && ans[minrow].size() + pushs.size() > n) minrow++;
if(minrow == n) continue;
ans[minrow] += pushs;
used[si] = true;
//for(auto &s: ans) std::cout << s << '\n'; puts("");
}
REP(si, 0, m) if(!used[si]) {
std::sort(ans, ans+n, [](std::string x, std::string y){
return x.size() > y.size();
});
REP(j, 0, n){
int ok_cnt = 0;
REP(i, 0, n){
if(ans[i].size() <= j) ok_cnt++;
}
if(ok_cnt < ss[si].size()) continue;
int idx = 0;
REP(i, 0, n){
if(idx >= ss[si].size()) break;
if(ans[i].size() <= j){
ans[i] += std::string(j - ans[i].size(), '.');
ans[i] += ss[si][idx];
idx++;
}
}
used[si] = true;
break;
}
}
for(auto &s: ans){
if(s.size() < n){
s += std::string(n-s.size(), '.');
}
}
for(auto &s: ans){
std::cout << s << '\n';
}
return 0;
}
|
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int cnt[100001];//色の有無を表す配列
int C[100001];
bool check[100001];
vector<vector<int> > E(100001);
void dfs(int n, int bef){
if(cnt[C[n]]==0){
check[n]=true;
}
cnt[C[n]]++;
for(int i=0; i<E[n].size(); i++){
if(E[n][i] != bef){
dfs(E[n][i], n);
}
}
cnt[C[n]]--;//ここ凄い大事、これがないと反対方向の色がカウントされっぱなしになる。
}
int main(){
int N;
cin>>N;
int a, b;
for(int i=0; i<N; i++){
cin>>C[i];
}
for(int i=0; i<N-1; i++){
cin>>a>>b;
E[a-1].push_back(b-1);
E[b-1].push_back(a-1);
}
dfs(0, -1);
for(int i=0; i<N; i++){
if(check[i]==true){
cout<<i+1<<endl;
}
}
return 0;
} | #pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll INF = (1LL << 60);
struct func{
ll base, m, M;
func() : base(0), m(-INF), M(INF) {}
ll value(ll x){
ll tmp = x + base;
tmp = max(m, tmp);
tmp = min(M, tmp);
return tmp;
}
};
void merge(func& f, int type, ll a){
if (type == 1){
f.base += a;
f.m += a;
f.M += a;
}
else if (type == 2){
if (f.M <= a){
f.base = 0;
f.m = a;
f.M = a;
return;
}
f.m = max(f.m, a);
}
else{
if (f.m >= a){
f.base = 0;
f.m = a;
f.M = a;
return;
}
f.M = min(f.M, a);
}
}
int main(void){
int N; cin >> N;
func f;
for (int i = 0; i < N; ++i){
int t; ll a;
cin >> a >> t;
merge(f, t, a);
}
int Q; cin >> Q;
while(Q--){
ll x; cin >> x;
cout << f.value(x) << endl;
}
return 0;
} |
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <array>
#include <cassert>
//#include <chrono>
#include <cmath>
//#include <cstring>
//#include <functional>
//#include <iomanip>
#include <iostream>
#include <map>
//#include <numeric>
//#include <queue>
//#include <random>
#include <set>
#include <vector>
using namespace std;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define int long long
#define ll int
#define all(a) a.begin(),a.end()
#define rev(a) a.rbegin(),a.rend()
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
const double pi=acosl(-1);
void solve()
{
int n;
cin>>n;
vector<vector<int>> same(n+2);
vector<int> x(n),c(n);
fi(0,n)
{
cin>>x[i]>>c[i];
same[c[i]].pb(x[i]);
}
same[n+1]={0,0};
fi(1,n+1)
{
if(same[i].size()==0) continue;
sort(all(same[i]));
}
ar<int,2> dp,prev;
prev[0]=prev[1]=dp[0]=dp[1]=0;
fk(1,n+2)
{
if(same[k].size()==0) continue;
ar<int,2> ndp;
ndp[0]=ndp[1]=1e15;
fi(0,2)
{
if(prev[i]<=same[k][0])
{
// ndp[0]=min(ndp[0],dp[i]+2*(same[k].back()-same[k][0])+same[k][0]-prev[i]);
}
else if(prev[i]>same[k][0]&&prev[i]<=same[k].back())
{
ndp[0]=min(ndp[0],dp[i]+(same[k].back()-same[k][0])+same[k].back()-prev[i]);
}
else
{
ndp[0]=min(ndp[0],dp[i]+(same[k].back()-same[k][0])+prev[i]-same[k].back());
}
}
fi(0,2)
{
if(prev[i]<=same[k][0])
{
ndp[1]=min(ndp[1],dp[i]+(same[k].back()-same[k][0])+same[k][0]-prev[i]);
}
else if(prev[i]>same[k][0]&&prev[i]<=same[k].back())
{
ndp[1]=min(ndp[1],dp[i]+(same[k].back()-same[k][0])+prev[i]-same[k][0]);
}
else
{
// ndp[1]=min(ndp[1],dp[i]+2*(same[k].back()-same[k][0])+prev[i]-same[k].back());
}
}
// if(same[k].size()==1)
// {
// assert(ndp[0]==ndp[1]);
// }
dp=ndp;
prev[0]=same[k][0];
prev[1]=same[k].back();
}
cout<<min(dp[0],dp[1])<<'\n';
}
signed main()
{
FAST;
int tt=1;
//cin>>tt;
while(tt--)
{
solve();
}
}
//int dx[] = {+1,-1,+0,+0,-1,-1,+1,+1}; // Eight Directions
//int dy[] = {+0,+0,+1,-1,+1,-1,-1,+1}; // Eight Directions
//int dx[]= {-2,-2,-1,1,-1,1,2,2}; // Knight moves
//int dy[]= {1,-1,-2,-2,2,2,-1,1}; // Knight moves
// For (a^b)%mod, where b is large, replace b by b%(mod-1).
// a+b = (a|b)+(a&b)
// a+b = 2*(a&b)+(a^b)
| //#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
//#include <x86intrin.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define PI 3.141592653589793L
#define FAST ios::sync_with_stdio(false); cin.tie(NULL);
// Use for file I/O;
#define FIN string _fname = "input"; \
string _is = _fname + ".in", _os = _fname + ".out"; \
freopen(_is.c_str(), "r", stdin); \
freopen(_os.c_str(), "w", stdout);
#define FIN2 freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define RINIT mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define gsize(x) (int)((x).size())
using namespace std;
//using namespace __gnu_pbds;
//typedef gp_hash_table<int, int, hash<int>> ht;
const char nl = '\n';
const char Nl = '\n';
typedef long long ll;
typedef long double ld;
// typedef unsigned int uint;
typedef unsigned long long ull;
const ll inf = 1e9 + 10;
const ll inf2 = 1e18 + 99LL;
const ld inf3 = 1e14;
const ll mod = 1e9+7, mod2 = 998244353;
const ld eps = 1e-2;
const bool local = false;
const int logn = 8, maxn = 400005, maxm = 10001, maxn2 = 3;
int main() {
FAST; //RINIT;
int n;
cin >> n;
vector<ll> p(n + 1);
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
p[i + 1] = p[i];
if (i&1) p[i + 1] += a;
else p[i + 1] -= a;
}
map<ll, int> ct;
ll ans = 0;
for (int i = 1; i <= n; i++) {
ct[p[i - 1]]++;
ans += ct[p[i]];
}
cout << ans << nl;
}
// © Benq
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
|
#include <bits/stdc++.h>
#define FOR(i, m, n) for (ll i = m; i < (n); i++)
#define RFOR(i, m, n) for (ll i = (m - 1); i >= 0; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
//#define print(ele) cout << (ele) << "\n"
#define print10(ele) cout << fixed << setprecision(10) << (ele) << "\n"
using namespace std;
typedef long long ll;
typedef vector<int> ivec;
typedef vector<string> svec;
typedef vector<ll> lvec;
const int mod = 1e9 + 7;
const ll INF = 1000000000000000000LL;
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
cout << head;
if (sizeof...(tail) != 0) cout << " ";
print(forward<Tail>(tail)...);
}
int main() {
int n;
cin >> n;
print(n%2==0 ? "White" : "Black");
return 0;
} | #include<iostream>
using namespace std;
int main()
{
int N;
while(cin>>N)
{
if(N%2==0)cout<<"White"<<endl;
else if(N%2==1) cout<<"Black"<<endl;
}
return 0;
} |
/*
*Team hhu_6814
*Author big_yellow_doge
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <string.h>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <list>
using namespace std;
//ios::sync_with_stdio(false);
const int MINF = 0x7fffffff;
const int INF = 0x3f3f3f3f;
const int Maxn = 1e5+5;
typedef long long ll;
int main()
{
int a,mi = 1e9;
for(int i = 0;i < 4;i++){
cin >> a;
mi = min(a,mi);
}
cout << mi << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
int a = floor(1.08 * n);
if (a < 206) cout << "Yay!" << endl;
if (a == 206) cout << "so-so" << endl;
if (a > 206) cout << ":(" << endl;
} |
#include<bits/stdc++.h>
using namespace std;
//typedef long long int lld;
using lld=long long int;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
lld n,ans=0;
cin>>n;
lld a[n];
map<lld,lld>m;
for(int i=0;i<n;i++){
cin>>a[i];
m[a[i]%200]++;
}
for(auto i:m)
ans+=((i.second)*(i.second-1))/2;
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
#define MOD 1000000007
int mpow(int a, int b) {
int res = 1;
for (; b; b >>= 1) {
if (b & 1) res = (int64_t) res * a % MOD;
a = (int64_t) a * a % MOD;
}
return res;
}
#ifdef WIN32
#define getchar_fast getchar
#else
#define getchar_fast getchar_unlocked
#endif
int main() {
int h = ri();
int w = ri();
std::vector<bool> a[h];
getchar_fast();
for (auto &i : a) {
i.resize(w);
for (int j = 0; j < w; j++) i[j] = getchar_fast() == '.';
getchar_fast();
}
int16_t free[h][w];
memset(free, 0, sizeof(free));
for (int i = 0; i < h; i++) {
int start = 0;
for (int j = 0; j <= w; j++) if (j == w || !a[i][j]) {
for (int k = start; k < j; k++) free[i][k] += j - start;
start = j + 1;
}
}
for (int i = 0; i < w; i++) {
int start = 0;
for (int j = 0; j <= h; j++) if (j == h || !a[j][i]) {
for (int k = start; k < j; k++) free[k][i] += j - start;
start = j + 1;
}
}
int k = 0;
for (auto &i : a) k += std::accumulate(i.begin(), i.end(), 0);
int b2[h + w];
b2[0] = 1;
int b2_rev[h + w];
b2_rev[0] = mpow(2, k);
for (int i = 1; i < h + w; i++) {
b2[i] = b2[i - 1] + b2[i - 1];
if (b2[i] >= MOD) b2[i] -= MOD;
b2_rev[i] = (int64_t) b2_rev[i - 1] * 500000004 % MOD;
}
int res = 0;
for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
if (!a[i][j]) continue;
// std::cerr << free[i][j] - 1 << std::endl;
res = (res + (int64_t) (b2[free[i][j] - 1] - 1 + MOD) * b2_rev[free[i][j] - 1]) % MOD;
}
std::cout << res << std::endl;
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ep emplace
#define epb emplace_back
#define scll static_cast<long long>
#define sz(x) static_cast<int>((x).size())
#define pfll(x) printf("%lld\n", x)
#define ci(x) cin >> x
#define ci2(x, y) cin >> x >> y
#define ci3(x, y, z) cin >> x >> y >> z
#define co(x) cout << x << endl
#define co2(x, y) cout << x << y << endl
#define co3(x, y, z) cout << x << y << z << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
typedef priority_queue<P> PQP;
typedef priority_queue<P, vector<P>, greater<P>> PQPG;
const int MAX_N = 1e2, MOD = 1e9 + 7, INF = 1e9;
int n, x[MAX_N], y[MAX_N];
int main() {
ci(n);
rep(i, n) ci2(x[i], y[i]);
bool ans = false;
for (int i = 2; i < n; i++) {
for (int j = 1; j < i; j++) {
for (int k = 0; k < j; k++) {
if ((x[i] - x[k]) * (y[j] - y[k]) ==
(y[i] - y[k]) * (x[j] - x[k])) ans = true;
}
}
}
if (ans) co("Yes");
else co("No");
return 0;
}
| #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vector<int> >
const ll mod = 1000000007;
const int INF = 1001001001;
int main(){
int a[4];
rep(i, 4) cin >> a[i];
int s = 0;
rep(i, 4) s += a[i];
rep(i, 1<<4){
int d = 0;
rep(j, 4){
if(((i>>j)&1)) d += a[j];
}
if(s - d == d){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
} |
#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() {
vector<int> v(4);
rep(i, 4) cin >> v[i];
ll total = 0;
rep(i, 4) total += v[i];
for (int bit = 0; bit < (1 << 4); ++bit) {
ll sum_bit = 0;
for (int i = 0; i < 4; ++i) {
if (bit & (1 << i)) {
sum_bit += v[i];
}
}
if (sum_bit * 2 == total) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
} | #include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false); cin.tie(NULL);
#define int long long
#define f first
#define s second
#define endl "\n"
//#define m 1000000007
#define out(v,n) for(int i=0;i<n;i++) cout<<v[i]<<" "; cout<<endl;
using namespace std;
signed main(){
//freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
fast
//int n,ans=LLONG_MAX; cin>>n;
vector<int> v(4);
for(int i=0;i<4;i++) cin>>v[i];
sort(v.begin(),v.end());
if(v[0]+v[1]+v[2]==v[3] || v[0]+v[2]==v[1]+v[3] || v[1]+v[2]==v[0]+v[3]) cout<<"Yes";
else cout<<"No";
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const ll INF = ll(1e18) + 5;
string atcoder = "atcoder";
void solve() {
string S;
cin >> S;
if (S > atcoder) {
cout << 0 << endl;
return;
}
string revS = S;
sort(revS.rbegin(), revS.rend());
if (atcoder >= revS) {
cout << -1 << endl;
return;
}
int n = S.size();
ll ans = n;
// Sの中でaより大きな最初のindexを求める.
rep(i, n) {
if (S[i] > 'a') {
ans = i;
break;
}
}
// Sの中でtより大きな最初のindexを求める.
for (int i = 2; i < n; i++) {
if (S[i] > 't') {
ans = min(ans, i - 1ll);
break;
}
if (S[i] == 't') {
ans = min(ans, (ll)i);
}
}
cout << ans << endl;
}
int main() {
int T;
cin >> T;
rep(i, T) {
solve();
}
return 0;
} | #include<bits/stdc++.h>
#define ll long long int
using namespace std;
ll n,low,high,mid,x,z,a;
void solve(ll the)
{
ll i,j,k,b,c,d,m,l,r,sum=0,ans=0,temp,t;
scanf("%lld",&n);a=n+1;
low=0;high=sqrt((8*a)+1)-1;high/=2;
while(low<=high){
mid=low+(high-low)/2;
x=1;
if(mid&1){
x=(mid+1)/2;x*=mid;
}
else{
x=mid/2;x*=(mid+1);
}
if(x<=a){
z=mid;
low=mid+1;
}
else{
high=mid-1;
}
// printf("%lld\n",x);
}
ans=1+(n-z);
//printf("%lld\n",z);
//cout << "Hello" << endl;
cout << ans;
}
int main()
{
long long int t=1,c=1;
// scanf("%lld",&t);
while(t--){
solve(c);
c++;
}
return 0;
} |
#include<iostream>
#include<vector>
#include<string>
#include<set>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}
class UnionFind {
private:
vector<int> parent_;
vector<int> node_rank_;
vector<int> sizes_;
public:
UnionFind(int node_num):
parent_(vector<int>(node_num)), node_rank_(vector<int>(node_num)), sizes_(vector<int>(node_num)) {
for (int i = 0; i < node_num; ++i) {
parent_[i] = i;
node_rank_[i] = 0;
sizes_[i] = 1;
}
}
int getRoot(int u) {
return parent_[u] == u ? u : parent_[u] = getRoot(parent_[u]);
}
bool isSame(int u, int v) {
return getRoot(u) == getRoot(v);
}
void unite(int u, int v) {
u = getRoot(u);
v = getRoot(v);
if (u == v) return;
if (node_rank_[u] < node_rank_[v]) {
parent_[u] = v;
sizes_[v] += sizes_[u];
}
else {
parent_[v] = u;
sizes_[u] += sizes_[v];
if (node_rank_[u] == node_rank_[v]) {
node_rank_[u]++;
}
}
}
int getSize(int u) {
return sizes_[getRoot(u)];
}
};
using P = pair<ll, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<ll> A(N), T(N);
rep(i, 0, N) {
cin >> A[i] >> T[i];
}
int Q;
cin >> Q;
vector<ll> X(Q);
rep(i, 0, Q) {
cin >> X[i];
}
set<P> S;
rep(i, 0, Q) {
S.emplace(X[i], i);
}
UnionFind uf(Q);
ll diff = 0;
rep(i, 0, N) {
if (T[i] == 1) {
diff -= A[i];
} else if (T[i] == 2) {
// A[i]+diff以下をマージ
int idx = -1;
while (!S.empty() && (*S.begin()).first <= A[i] + diff) {
P now = *S.begin();
if (idx < 0) {
idx = now.second;
}
uf.unite(idx, now.second);
S.erase(now);
}
if (idx >= 0) {
X[idx] = A[i] + diff;
S.emplace(X[idx], idx);
}
} else {
// A[i]+diff以上をマージ
int idx = -1;
while (!S.empty() && (*S.rbegin()).first >= A[i] + diff) {
P now = *S.rbegin();
if (idx < 0) {
idx = now.second;
}
uf.unite(idx, now.second);
S.erase(now);
}
if (idx >= 0) {
X[idx] = A[i] + diff;
S.emplace(X[idx], idx);
}
}
}
rep(i, 0, Q) {
cout << X[uf.getRoot(i)] - diff << endl;
}
return 0;
} | #include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <stdio.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
ll mod = 1000000007;
// 約数列挙
vector<long long> divisor(long long n) {
vector<long long> ret;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end()); // 昇順に並べる
return ret;
}
// 変数
int n;
int x[210], y[210];
ll r[210];
// スコア計算
ll f(vector<ll>a, vector <ll> b, vector<ll>c, vector<ll>d) {
long double sum = 0;
for (int i = 0; i < n; i++) {
if ((a[i] <= x[i] && x[i] < c[i]) && (b[i] <= y[i] && y[i] <= d[i])) {
ll now = (c[i] - a[i])*(d[i] - b[i]);
sum += 1.0 - (1.0 - ((long double)min(now, r[i]) / (long double)max(now, r[i])) * ((long double)min(now, r[i]) / (long double)max(now, r[i])));
}
}
sum /= n;
sum *= 1e9;
ll ans = (ll)sum;
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// 入力
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i] >> r[i];
}
vector<pair<pair<int, int>, pair<ll, int>>>p;
for (int i = 0; i < n; i++) {
p.push_back({ {x[i],y[i]}, {r[i], i} });
}
sort(p.begin(), p.end());
vector<int>ansa(n), ansb(n), ansc(n), ansd(n);
vector<vector<pair<int, pair<ll, int>>>>G(10010);
for (int i = 0; i < n; i++) {
G[p[i].first.first].push_back({ p[i].first.second, p[i].second });
}
int bx = 0;
for (int i = 0; i < 10010; i++) {
int s = G[i].size();
if (s == 0)continue;
if (s >= 2) {
int by = 0;
for (int j = 0; j < s; j++) {
int idx = G[i][j].second.second;
ansa[idx] = bx; ansb[idx] = by;
ansc[idx] = i + 1; ansd[idx] = G[i][j].first + 1;
by = G[i][j].first + 1;
}
}
else {
int idx = G[i][0].second.second;
int nowr = G[i][0].second.first;
int tmpy = min(10000, nowr / (i + 1 - bx));
if (tmpy < G[i][0].first + 1)tmpy = G[i][0].first + 1;
int now = (i + 1 - bx) * tmpy;
int K = 0;
if (now > nowr) {
int sy = nowr / (i + 1 - bx);
K = tmpy - sy;
}
ansa[idx] = bx; ansb[idx] = K;
ansc[idx] = i + 1; ansd[idx] = tmpy;
}
bx = i + 1;
}
for (int i = 0; i < n; i++) {
cout << ansa[i] << " " << ansb[i] << " " << ansc[i] << " " << ansd[i] << endl;
}
return 0;
} |
//GIVE ME AC!!!!!!!!!!!!!!!!!
//#pragma GCC target("avx")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define floatset() fixed<<setprecision(15)
#define all(n) n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define pb push_back
#define eb emplace_back
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using vl=vector<ll>;
using vi=vector<int>;
using vs=vector<string>;
using vc=vector<char>;
using vvl=vector<vl>;
using P=pair<ll,ll>;
using vvc=vector<vc>;
using vd=vector<double>;
using vp=vector<P>;
using vb=vector<bool>;
using P=pair<ll,ll>;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
const ll inf =1e18;
const ll MOD=1000000007;
const ll mod=998244353;
const double pi=acos(-1);
template<typename T1,typename T2 >
ostream &operator<<(ostream&os,const pair<T1,T2>&p) {
os<<p.first<<" "<<p.second;
return os;
}
template<typename T1,typename T2>
istream &operator>>(istream&is,pair<T1,T2>&p) {
is>>p.first>>p.second;
return is;
}
template<typename T>
ostream &operator<<(ostream&os,const vector<T>&v) {
for(int i=0;i<(int)v.size();i++) {
os<<v[i]<<(i+1!=v.size()?" ":"");
}
return os;
}
template<typename T>
istream &operator>>(istream&is,vector<T>&v) {
for(T &in:v)is>>in;
return is;
}
void scan(){}
template<class Head,class... Tail>
void scan(Head&head,Tail&... tail) {
cin>>head;
scan(tail...);
}
template<class T>
void print(const T &t) { cout << t << '\n'; }
template<class Head, class... Tail>
void print(const Head &head, const Tail &... tail) {
cout << head << ' ';
print(tail...);
}
template<class... T>
void fin(const T &... a) {
print(a...);
exit(0);
}
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);}
random_device seed_gen;
mt19937 engine(seed_gen());
int main(){
LL(n,m);
vs a(m);
cin>>a;
shuffle(all(a),engine);
ll now=0;
rep(i,0,n){
ll cnt=0;
string ans="";
while(cnt+(ll)(a[now].size())<=n){
cnt+=a[now].size();
ans+=a[now];
now++;
}
rep(i,0,n-cnt){
ll x=engine();
x%=8;
cout<<(char)('A'+x);
}
cout<<ans<<endl;
}
} | #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<tuple>
#include<array>
#include<map>
#include<memory>
int main()
{
int64_t N;
std::cin >> N;
std::vector<int64_t> A(N);
for (int64_t n = 0; n < N; ++n)
{
int64_t a;
std::cin >> a;
A[n] = a;
}
std::sort(A.begin(), A.end());
int64_t c = 0;
int64_t s = 1;
for (size_t i = 0; i < A.size(); ++i)
{
s *= ((A[i] - c) + 1);
s %= 1000000007;
c = A[i];
}
std::cout << s << std::endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pb push_back
#define int long long int
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define F first
#define S second
#define mod 1000000007
#define MOD 998244353
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define deb(x) cout<< ">" << #x << '=' << x << endl;
#define read(v) for(auto &it:v) cin>>it;
const int N = 2e5 + 5;
void solve() {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << min({a, b, c, d}) << endl;
}
signed 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;
int main(){
vector<int> A(4);
for(int i=0;i<4;i++) cin >> A[i];
int ans = 99999;
for(int i=0;i<4;i++){
ans = min(ans,A[i]);
}
cout << ans << endl;
return 0;
} |
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define f first
#define s second
#define sz(x) (int)x.size()
#define endl "\n"
#define forn(i,n) for(int i=0;i<n;++i)
#define fore(i,l,r) for(int i=int(l);i<=int(r);++i)
#define rep(i,begin,end) for(__typeof(end) i=(begin);i!=(end);i++)
#define fill(a,value) memset(a,value,sizeof(a));
#define gcd(a,b) __gcd((a),(b))
#define watch1(x) cout<<(x)<<endl
#define watch2(x,y) cout<<(x)<<" "<<(y)<<endl
#define watch3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
const int INF = 9e18;
const int mod = 998244353;
const int N = 2e5 + 5;
int i, n, a[N], b[N], p[N], vis[N];
void solve() {
cin >> n;
vpii w, ans;
for (i = 1; i <= n; i++) {
cin >> a[i];
w.pb({a[i], i});
}
for (i = 1; i <= n; i++) {
cin >> b[i];
}
map<int, int> have, mp;
for (i = 1; i <= n; i++) {
cin >> p[i];
have[p[i]] = i;
mp[i] = p[i];
}
for (i = 1; i <= n; i++) {
if (i != p[i] && a[i] <= b[p[i]]) {
cout << -1 << endl;
return;
}
}
int cnt = 0;
for (i = 1; i <= n; i++) {
if (!vis[i]) {
int v = i;
cnt++;
while (!vis[v]) {
vis[v] = 1;
v = p[v];
}
}
}
cout << n - cnt << endl;
sort(all(w));
for (i = 1; i <= n; i++) {
if (mp[w[i - 1].s] == w[i - 1].s) {
continue;
}
int give = mp[w[i - 1].s], get = have[w[i - 1].s];
ans.pb({w[i - 1].s, get});
have[give] = get;
mp[get] = give;
}
for (auto &i : ans) {
cout << i.f << " " << i.s << endl;
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fastio;
int t;
//cin >> t;
t = 1;
while (t--) {
solve();
}
return 0;
}
| #pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("trapv")
#pragma GCC target("sse4")
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long long int ll;
typedef unsigned long long int ull;
typedef priority_queue<ll> mxhpl;
typedef priority_queue<int> mxhpi;
typedef priority_queue<ll, vector<ll>, greater<ll>> mnhpl;
typedef priority_queue<int, vector<int>, greater<int>> mnhpi;
#define rep(i,start,end) for(ll i = start; i <= end; ++i)
#define rrep(i,end,start) for(ll i = end; i >= start; --i)
#define parr(a,n) rep(i,0,n-1){cout << a[i] << " ";}cout<<"\n"
#define sarr(a,n) rep(i,0,n-1)cin >> a[i]
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define mset(a,x) memset(a, x, sizeof(a))
#define ps(x,y) fixed<<setprecision(y)<<x //cout << ps(ans, decimal places);
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
// DEBUGGING
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template<typename T, typename V>
void __print(const pair<T, V>& x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; }
template<typename T>
void __print(const T& x) { int f = 0; cerr << '{'; for (auto& i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; }
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// Fast IO
void IO() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
}
#define MOD 1000000007
//CODE
/*******************************************************/
#define MX 200001
ll n;
void solve() {
cin >> n;
vl a(n), b(n), c(n), whoHasMyBag(n);
sarr(a, n);
sarr(b, n);
sarr(c, n);
vector<pll> wp;
rep(i, 0, n - 1) {
if (c[i] - 1 != i && a[i] <= b[c[i] - 1]) {
cout << "-1\n";
return;
}
c[i]--;
wp.pb({ a[i],i });
whoHasMyBag[c[i]] = i;
}
sort(all(wp));
vector<pll> ans;
for (auto p : wp) {
if (c[p.S] == p.S) continue;
ans.pb({ p.S + 1,whoHasMyBag[p.S] + 1 });
c[whoHasMyBag[p.S]] = c[p.S];
whoHasMyBag[c[whoHasMyBag[p.S]]] = whoHasMyBag[p.S];
}
// debug("TEST");
cout << ans.size() << "\n";
for (auto p : ans) {
cout << p.F << " " << p.S << "\n";
}
// debug(ans, c);
}
int main() {
IO();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
/*******************************************************/
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
//input
int N;
cin >> N;
string name[N];
int height[N];
vector<int> height_checker(N);
for(int i = 0; i < N; i++){
cin >> name[i] >> height[i];
height_checker.at(i) = height[i];
}
//高さをsort
sort(height_checker.begin(), height_checker.end());
//N-1番目の山の高さを聞き、それと合う山を探す。
int SecondMountain = height_checker.at(N-2);
for(int j = 0; j < N; j++){
if(height[j] == SecondMountain){cout << name[j] << endl; break;}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define input_output freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
template <typename Arg1>
void prn(Arg1&& arg1) { cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void prn(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prn(args...); }
template <typename Arg1>
void prs(Arg1&& arg1) { cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void prs(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prs(args...); }
template <typename Arg1>
void read(Arg1&& arg1) { cin>>arg1; }
template <typename Arg1, typename... Args>
void read(Arg1&& arg1, Args&&... args) { cin>>arg1; read(args...); }
#define int long long
#define all(ds) ds.begin(), ds.end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define gcd(x,y) __gcd(x,y)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define precise(x) cout<<fixed<<setprecision(x)
#define endl "\n"
const long long INF = 1e18;
const int32_t MOD = 1e9+7;
const int N = 2e5+5;
int n, m, k, q, r, l, x, y, z;
int a[N], b[N], c[N];
string s, t;
// int ans = 0;
vector<int>edges[N];
vector<int>mn(N, INF); // min cost of gold among all the cities which lead to the current node
void solve(){
read(n,m);
rep(i,1,n+1)read(a[i]);
rep(i,0,m){
read(x,y);
edges[x].push_back(y);
}
int ans = -INF;
for(int i = 1; i <= n; ++i){
for(int j: edges[i]){
mn[j] = min(mn[j], min(mn[i], a[i]));
}
if(mn[i] != INF){
ans = max(ans, a[i]-mn[i]);
}
}
prn(ans);
}
signed main()
{
#ifndef ONLINE_JUDGE
input_output
#else
fastio
#endif
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
int t = 1;
// cin>>t;
while(t--){
solve();
}
return 0;
} |
#include <cstdio>
typedef long long ll;
inline void in(ll &x) {
x=0;
ll f=1;
char ch=getchar();
while (ch<'0'||ch>'9') {
if (ch=='-') f=-1;
ch=getchar();
}
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
x*=f;
}
inline void out(ll x) {
if (x<0) putchar('-'),x=-x;
if (x>9) out(x/10);
putchar(x%10+'0');
}
ll n;
bool ok[10005];
int main() {
in(n);
for (ll i=1; i<=n; ++i) {
ll x;
in(x);
if (x<1||x>n||ok[x]) {
puts("No");
return 0;
}
ok[x]=1;
}
puts("Yes");
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
if(N>=0)cout<<N;
else cout<<0;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int C[3][3] = {{1,0,0}, {1,1,0}, {1,2,1}};
int Lucas(int X, int M){
int XX[20] = {};
int MM[20] = {};
int x = X; int m = M;
int ans = 1;
for(int i = 0; i < 15; i++){
XX[i] = x % 3; x /= 3;
MM[i] = m % 3; m /= 3;
ans *= C[XX[i]][MM[i]];
ans %= 3;
}
return ans;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N; cin >> N;
string S; cin >> S;
int A[400010];
int res = 0;
for(int i = 0; i < N; i++){
if(S[i] == 'B') A[i] = 0;
if(S[i] == 'W') A[i] = 1;
if(S[i] == 'R') A[i] = 2;
res += A[i] * Lucas(N-1, i) % 3;
res %= 3;
}
if(N % 2 == 0){
res = 3 - res; res %= 3;
}
if(res == 0) cout << "B\n";
if(res == 1) cout << "W\n";
if(res == 2) cout << "R\n";
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define w(x) int x; cin>>x; while(x--)
#define endl ("\n")
#define f(i, a, n) for(int i=a; i<n; i++)
#define cc(r) cout<<r<<" "
#define ce(r) cout<<r<<endl
void inout()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
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;
}
int32_t main()
{
inout();
int n;
cin >> n;
int r[n], a[n], ans = 0;
f(i, 0, n) {
r[i] = nCrModpLucas(n - 1, i, 3);
}
char k;
f(i, 0, n) {
cin >> k;
if (k == 'B')
a[i] = 0;
else if (k == 'W')
a[i] = 1;
else
a[i] = 2;
}
f(i, 0, n) {
ans = (ans + (a[i] * r[i]) % 3) % 3;
}
if ((n & 1) == 0) {
ans = (-ans + 3) % 3;
}
if (ans == 0)
ce('B');
else if (ans == 1)
ce('W');
else
ce('R');
}
|
#include<bits/stdc++.h>
using namespace std;
int n,m;
int main(){
cin>>n>>m;
if(n==1&&m==0)cout<<"1 2"<<endl,exit(0);
if(m<0||m>=n-1)cout<<-1,exit(0);
n--;
cout<<"1 1000001"<<endl;
for(int i=1;i<=m+1;i++)cout<<i*2<<" "<<i*2+1<<endl;
n-=m+1;
for(int i=1;i<=n;i++)cout<<1000000+i*2<<" "<<1000000+i*2+1<<endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <list>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<typename T = int> using V = vector<T>;
template<typename T = int> using VV = vector<vector<T>>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
os << '(' << a.first << ',' << a.second << ')';
return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
for (auto x : v) os << x << ' ';
return os;
}
void debug() {
cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
cerr << a;
if (sizeof...(b)) cerr << ", ";
debug(b...);
}
int solve(int n,vector<int>L, vector<int>R){
vector<pii>a(n);
rep(i,n){
a[i] = pii(L[i], R[i]);
}
sort(a.begin(), a.end(), [](pii x, pii y){
return x.second < y.second;
});
int pre = -1;
int ans = 0;
for(auto x: a){
if(pre < x.first){
ans++;
pre = x.second;
}
}
return ans;
}
int solve2(int n, vector<int>L, vector<int>R){
vector<pii>a(n);
rep(i,n){
a[i] = pii(L[i], R[i]);
}
sort(a.begin(), a.end(), [](pii x, pii y){
return x.first < y.first;
});
int pre = -1;
int ans = 0;
for(auto x: a){
if(pre < x.first){
ans++;
pre = x.second;
}
}
return ans;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
if(m < 0){
cout << -1 << endl;
return 0;
}
vector<int>a(n), b(n);
int t = 4;
//[0,1], 2[3,4], 5[
//[1,2] 3 [4,5]
rep(i,n-1){
a[i+1] = t++;
b[i+1] = t++;
t++;
}
a[0] = 1;
b[0] = 3;
rep(i,m+1)b[0] += 3;
if(solve(n,a,b)-solve2(n,a,b)!=m){
cout << -1 << endl;
return 0;
}
rep(i,n){
cout << a[i] << " " << b[i] << "\n";
}
}
|
#define _USE_MA_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
#include <random>
#include <cassert>
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define REPE(i, n) for (int (i)=0; (i) <= (n); (i)++)
#define CONTAINS_MAP(m, a) ((m).find((a)) != m.end())
#define CONTAINS_SET(m, a) ((m).find((a)) != m.end())
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; exit(0); }
void Yes() { cout << "Yes" << endl; exit(0); }
void No() { cout << "No" << endl; exit(0); }
int N;
int X[200001];
int Y[200001];
int M;
int Q;
const int PLUS_X = 1;
const int MINUS_X = -1;
const int PLUS_Y = 2;
const int MINUS_Y = -2;
struct Op
{
int p;
int f;
};
Op _op_x[200001];
Op _op_y[200001];
int func(Op op, int x, int y)
{
int ret = 0;
if (op.f == PLUS_X)
{
ret = x;
}
if (op.f == MINUS_X)
{
ret = -x;
}
if (op.f == PLUS_Y)
{
ret = y;
}
if (op.f == MINUS_Y)
{
ret = -y;
}
ret += op.p;
return ret;
}
signed main()
{
cin >> N;
REP(i, N) cin >> X[i] >> Y[i];
_op_x[0].p = 0;
_op_x[0].f = PLUS_X;
_op_y[0].p = 0;
_op_y[0].f = PLUS_Y;
cin >> M;
for (int i = 1; i <= M; i++)
{
int o;
cin >> o;
if (o == 1)
{
_op_x[i].p = _op_y[i - 1].p;
_op_x[i].f = _op_y[i - 1].f;
_op_y[i].p = -_op_x[i - 1].p;
_op_y[i].f = -_op_x[i - 1].f;
}
else if (o == 2)
{
_op_x[i].p = -_op_y[i - 1].p;
_op_x[i].f = -_op_y[i - 1].f;
_op_y[i].p = _op_x[i - 1].p;
_op_y[i].f = _op_x[i - 1].f;
}
else if (o == 3)
{
int p;
cin >> p;
_op_x[i].p = 2 * p - _op_x[i - 1].p;
_op_x[i].f = -_op_x[i - 1].f;
_op_y[i].p = _op_y[i - 1].p;
_op_y[i].f = _op_y[i - 1].f;
}
else if (o == 4)
{
int p;
cin >> p;
_op_x[i].p = _op_x[i - 1].p;
_op_x[i].f = _op_x[i - 1].f;
_op_y[i].p = 2 * p - _op_y[i - 1].p;
_op_y[i].f = -_op_y[i - 1].f;
}
}
cin >> Q;
REP(i, Q)
{
int a, b;
cin >> a >> b;
Op opx = _op_x[a];
Op opy = _op_y[a];
int x = X[b - 1];
int y = Y[b - 1];
int nx = func(opx, x, y);
int ny = func(opy, x, y);
cout << nx << " " << ny << endl;
}
}
| #include<bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
inline int read(){
int X=0;bool flag=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')flag=0;ch=getchar();}
while(ch>='0'&&ch<='9'){X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
if(flag)return X;
return ~(X-1);
}
int n,a[510][510],ansa[510],ansb[510],minn=1e9+7,minnn=1e9+7;
inline bool check(){
for(int i=2;i<=n;i++){
ansa[i]=a[i][1]-ansb[1],ansb[i]=a[1][i]-ansa[1];
if(ansa[i]<0||ansb[i]<0)return 0;
}
for(int i=2;i<=n;i++)
for(int j=2;j<=n;j++)
if(ansa[i]+ansb[j]!=a[i][j])return 0;
return 1;
}
int main(){
n=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
a[i][j]=read();
if(i>1&&j>1)
if((a[i][j]+a[i-1][j]+a[i][j-1]+a[i-1][j-1])&1){
puts("No");
return 0;
}
}
for(int i=1;i<=n;i++)minn=min(minn,a[1][i]),minnn=min(minnn,a[i][1]);
for(int k=minn;k>=0;k--){
if(a[1][1]-k<minnn)continue;
ansa[1]=k,ansb[1]=a[1][1]-k;
if(check()){
puts("Yes");
for(int i=1;i<=n;i++)printf("%d ",ansa[i]);
puts("");
for(int i=1;i<=n;i++)printf("%d ",ansb[i]);
return 0;
}
ansa[1]=a[1][1]-k,ansb[1]=k;
if(check()){
puts("Yes");
for(int i=1;i<=n;i++)printf("%d ",ansa[i]);
puts("");
for(int i=1;i<=n;i++)printf("%d ",ansb[i]);
return 0;
}
}
puts("No");
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
using lint=long long;
template<class T> struct edge{
int to;
T wt;
edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;
template<class T>
void add_directed_edge(weighted_graph<T>& G,int u,int v,const T& wt){
G[u].emplace_back(v,wt);
}
template<class T>
pair<vector<T>,bool> Bellman_Ford(const weighted_graph<T>& G,int s){
const T INF=numeric_limits<T>::max();
int n=G.size();
vector<T> d(n,INF);
d[s]=0;
// rep(_,n-1){
rep(_,n){
bool upd=false;
rep(u,n) if(d[u]<INF) for(const auto& e:G[u]) {
int v=e.to;
if(d[v]>d[u]+e.wt) d[v]=d[u]+e.wt, upd=true;
}
if(!upd) return {d,true};
if(_==n-1 && upd) return {d,false};
}
queue<int> Q;
rep(u,n) if(d[u]<INF) Q.emplace(u);
while(!Q.empty()){
int u=Q.front(); Q.pop();
for(const auto& e:G[u]){
int v=e.to;
if(d[v]>-INF && (d[u]==-INF || d[v]>d[u]+e.wt)) d[v]=-INF, Q.emplace(v);
}
}
return {d,true};
}
/*
template<class T>
bool find_negative_cycle(const weighted_graph<T>& G){
int n=G.size();
vector<T> d(n);
rep(t,n){
bool relaxed=false;
rep(u,n) for(const auto& e:G[u]) {
if(d[e.to]<d[u]+e.wt){
d[e.to]=d[u]+e.wt;
relaxed=true;
if(t==n-1) return true;
}
}
if(!relaxed) break;
}
return false;
}
*/
int main(){
int n,m; scanf("%d%d",&n,&m);
vector<lint> w(n),l(m),v(m);
rep(i,n) scanf("%lld",&w[i]);
rep(j,m) scanf("%lld%lld",&l[j],&v[j]);
if(*max_element(w.begin(),w.end())>*min_element(v.begin(),v.end())){
puts("-1");
return 0;
}
{
vector<int> p(m);
iota(p.begin(),p.end(),0);
sort(p.begin(),p.end(),[&](int i,int j){
return make_pair(v[i],-l[i])<make_pair(v[j],-l[j]);
});
vector<lint> v2,l2;
rep(j,m){
if(v2.empty() || l2.back()<l[p[j]]){
v2.emplace_back(v[p[j]]);
l2.emplace_back(l[p[j]]);
}
}
v=v2;
l=l2;
m=v.size();
}
lint lo=-1,hi=1e9;
while(hi-lo>1){
lint mi=(lo+hi)/2;
bool ok=false;
vector<int> p(n);
iota(p.begin(),p.end(),0);
do{
vector<lint> wsum(n+1);
rep(i,n) wsum[i+1]=wsum[i]+w[p[i]];
weighted_graph<lint> G(n);
add_directed_edge(G,0,n-1,mi);
rep(i,n-1){
add_directed_edge(G,i+1,i,0LL);
}
rep(i,n) for(int j=i+1;j<n;j++) {
int idx=lower_bound(v.begin(),v.end(),wsum[j+1]-wsum[i])-v.begin()-1;
if(idx>=0){
add_directed_edge(G,j,i,-l[idx]);
}
}
if(Bellman_Ford(G,0).second){
ok=true;
break;
}
}while(next_permutation(p.begin(),p.end()));
if(ok) hi=mi;
else lo=mi;
}
printf("%lld\n",hi);
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC optimize(2)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<cstring>
#include<bitset>
#include<stack>
#include<time.h>
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define ALL(x) x.begin(),x.end()
#define sz(a) ((int)a.size())
#define getmid ((l+r)>>1)
#define mst(var,val) memset(var,val,sizeof(var))
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define lowbit(x) x&(-x)
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ls rt<<1
#define rs rt<<1|1
using namespace std;
#ifdef local
#define dbg(args...) cout << #args << " -> ", err(args);
void err(){ cout << endl; }
template<typename T, typename... Args>
void err(T a, Args... args){ cout << a << ' '; err(args...); }
#else
#define dbg(args...)
#endif // local
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <int, ll> pil;
typedef pair <double, double> pdd;
const int inf=0x3f3f3f3f;
const long long INF=0x3f3f3f3f3f3f3f3fLL;
const double PI=acos(-1.0);
const long double eps=1e-8;
const int mod=1e9+7;
const int maxn=2e5+100;
const int N=2e5+100;
const int M=(1<<20)+10;
const ll mm=(1LL<<32);
template <class T>
inline void read(T &x)
{
x = 0;
char c = getchar();
bool f = 0;
for (; !isdigit(c); c = getchar())
f ^= c == '-';
for (; isdigit(c); c = getchar())
x = x * 10 + (c ^ 48);
x = f ? -x : x;
}
template <class T>
inline void write(T x)
{
if (x < 0)
{
putchar('-');
x = -x;
}
T y = 1;
int len = 1;
for (; y <= x / 10; y *= 10)
++len;
for (; len; --len, x %= y, y /= 10)
putchar(x / y + 48);
}
ll qpow(ll a,ll b,ll mod)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%mod;
b>>=1;
a=(a*a)%mod;
}
return ans;
}
int main()
{
#ifdef local
freopen("in.txt","r",stdin);
#endif // local
IOS;cout.tie(0);
int n,m;cin>>n>>m;
if(m==0)
{
int tag=1;
for(int i=1;i<=n;++i)
{
cout<<tag<<" "<<tag+1<<"\n";
tag+=2;
}
return 0;
}
if(m<0||(m>n-2)){
cout<<-1<<"\n";return 0;
}
int mx=2+2*m+2;
cout<<1<<" "<<mx<<"\n";
int tag=2;
for(int i=1;i<=m+1;++i)
{
cout<<tag<<" "<<tag+1<<"\n";tag+=2;
}
mx++;
for(int i=1;i<=n-m-2;++i){
cout<<mx<<" "<<mx+1<<"\n";mx+=2;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define u unsigned
#define endl '\n'
#define mod 1000000007
#define quick ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
void solve(){
ll n,k;
cin>>n>>k;
string str="";
while(k--){
if(n%200==0){
n/=200;
}
else{
str = to_string(n);
string s = to_string(200);
str+=s;
n = stod(str);
}
}
cout<<n<<endl;
}
int main() {
quick;
int t=1;
// cin>>t;
while(t--){
solve();
}
} | #include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif
namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class T> inline bool chmin(T &x, const T &y){ if(y < x){ x = y; return true; } return false; }
template <class T> inline bool chmax(T &x, const T &y){ if(x < y){ x = y; return true; } return false; }
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
template <class Con>
string concat(const Con &c, const string &spr){
stringstream ss;
typename Con::const_iterator it = c.begin(), en = c.end();
bool fst = true;
for(; it != en; ++it){
if(!fst){ ss << spr; }
fst = false;
ss << *it;
}
return ss.str();
}
template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
vector<typename Con::value_type> ret;
typename Con::const_iterator it = c.begin(), en = c.end();
for(; it != en; ++it){
if(f(*it)){
ret.emplace_back(*it);
}
}
return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
vector<decltype(f(*c.begin()))> ret;
ret.reserve(c.size());
for(const auto &x: c){
ret.emplace_back(f(x));
}
return ret;
}
#endif
#if __cplusplus >= 201402L
#define lambda(e) ([&](const auto &_){ return (e); })
#define lambda2(e) ([&](const auto &_a, const auto &_b){ return (e); })
#endif
#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()
#define tget(t,i) get<i>(t)
#define MOD 1000000007LL
#define EPS 1e-8
void mainmain(){
LL n, k;
cin >> n >> k;
REP(i, k){
if(n % 200){ n = n * 1000 + 200; }
else{ n /= 200; }
}
cout << n << endl;
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(4);
mainmain();
}
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <typeinfo>
#include <unordered_set>
using namespace std;
typedef long long ll;
int main() {
int a, b,c;
cin >> a>>b>>c;
for (int i = c;;i++) {
if (a == 0 && b == 0) {
if (c == 1) {
cout << "Takahashi";
break;
}
else if (c == 0) {
cout << "Aoki";
break;
}
}
if (a <= 0) {
cout << "Aoki";
break;
}
else if (b <= 0) {
cout << "Takahashi";
break;
}
if (i % 2 == 0)a--;
if (i % 2 == 1)b--;
}
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
if(a==b){
if(c==0){
cout<<"Aoki";
}
else{
cout<<"Takahashi";
}
}
else if(a>b){
cout<<"Takahashi";
}
else{
cout<<"Aoki";
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std ;
int main(){
int H , W ;
cin >> H >> W ;
vector<vector<int > > A(H , vector<int> (W)) ;
for(int i= 0 ; i < H ; i++){
for(int j = 0 ; j < W ; j++){
cin >> A.at(i).at(j) ;
}
}
int cnt = A.at(0).at(0) ;
for(int i= 0 ; i < H ; i++){
for(int j = 0 ; j < W ; j++){
cnt = min(cnt , A.at(i).at(j));
}
}
long long ans = 0 ;
for(int i= 0 ; i < H ; i++){
for(int j = 0 ; j < W ; j++){
ans += A.at(i).at(j)-cnt ;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#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)
typedef long long ll;
const ll INF = (1LL << 62) - (1LL << 31); /*オーバーフローしない程度に大きい数*/
const ll MOD = 1000000007;
using namespace std;
ll gcd(ll a, ll b) {
if (a % b == 0) return (b);
return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
template <class T>
void chmin(T& a, T b) { // choose minimum.
if (a > b) {
a = b;
}
}
template <class T>
void chmax(T& a, T b) {
if (a < b) {
a = b;
}
}
const int mod = 1e9 + 7;
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;
}
};
// MODのときの計算に使うのでmintになっている.
mint choose(int n, int a) {
mint x = 1, y = 1;
rep(i, a) {
x *= n - i;
y *= i + 1;
}
return x / y;
}
int main() {
#ifdef LOCAL
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
int h, w;
cin >> h >> w;
int a[101][101];
int m = 100000;
rep(i, h) rep(j, w) {
cin >> a[i][j];
chmin(m, a[i][j]);
}
int ans = 0;
rep(i, h) rep(j, w) { ans += max(0, a[i][j] - m); }
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
unordered_set<string> st;
bool ok = false;
string ans;
for (int i = 0; i < N; i++) {
string s;
cin >> s;
if (st.count(s)) {
ok = true;
if (ans == "") ans = s;
}
if (s[0] == '!') {
s.erase(0, 1);
} else {
s = "!" + s;
}
st.insert(s);
}
if (ans[0] == '!') ans.erase(0, 1);
if (ok) cout << ans << endl;
else cout << "satisfiable" << endl;
} | /* Author : Aaryan Srivastava ^__^ */
#include <bits/stdc++.h>
#include <random>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define rep(i,n) for(int i = 0 ; i < (n) ; i++)
#define repA(i,x,y) for(int i = (x) ; i <= (y) ; i++)
#define repD(i,x,y) for(int i = (x) ; i >= (y) ; i--)
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define setval(a,val) memset(a,val,sizeof(a))
#define Randomize mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define trav(x , a) for(auto &x : a)
#define sz(a) ((int)a.size())
typedef long long ll ;
#define int ll
using namespace std;
const int N = 3e5 +5 ;
const int mod = 1e9 + 7 ;
const ll inf = 1e18 ;
const int SZ = 101 ;
const double eps = 1e-9 ;
using namespace __gnu_pbds;
using ordered_set = tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ;
typedef pair<int, int> pii;
typedef pair<int , pii> ipii ;
typedef pair<pii , int> piii ;
typedef unsigned long long ull ;
typedef long double ld;
ll po(ll x,ll y,ll p = mod) {ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
void solve()
{
int n ;
cin >> n;
map<string , int> m, m2;
rep(i , n){
string s;
cin >> s;
if(s[0] == '!')
m2[string(s.begin() + 1 , s.end())]++;
else
m[s]++;
}
for(auto& it : m){
string tmp = it.ff;
if(m2.find(tmp) != m2.end()){
cout << tmp;
return;
}
}
cout << "satisfiable";
}
int32_t main(int32_t argc, char *argv[])
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1, t = 0;
//cin >> TC ;
while(t++ < TC)
{
//cout << "Case #" << t << ": " ;
solve();
cout << "\n" ;
}
return 0;
} |
#include <iomanip>
#include <iostream>
using namespace std;
double dp[100][100][100];
double rec(int i, int j, int k) {
if (i == 100 || j == 100 || k == 100) return 0.0;
if (dp[i][j][k] > 0.0) return dp[i][j][k];
double n = double(i + j + k);
return dp[i][j][k] = ((rec(i + 1, j, k)) * i + (rec(i, j + 1, k)) * j + (rec(i, j, k + 1)) * k) / n + 1;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
cout << fixed << setprecision(10) << rec(a, b, c);
return 0;
} | #include<bits/stdc++.h>
#include<iostream>
#include<set>
#include<map>
#include<iterator>
#define ll long long
#define lli long long int
#define pb push_back
#define RIP(i,n) for(int i=0; i<n; i++)
#define RIP1(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a;i<(b); i++)
#define FOR1(i,a,b) for(int i=a; i>=(b); i--)
#define sc(a) scanf("%lld",&a)
#define SC(a) scanf("%d",&a)
#define cin(a) cin >> a
#define cout(a) cout << a
#define pi acos(-1)
#define pr(a) printf("%lld\n",a)
#define PR(a) printf("%lld ",a)
#define s(a,b) sort(a,b)
#define sz(x) (int)(x).size()
#define nl '\n'
#define Max 110
using namespace std;
void c_p_c()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
/*#ifdef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif*/
}
double dp[Max][Max][Max];
double solve(int a,int b,int c)
{
double ret = dp[a][b][c];
if(ret>0)
{
return ret;
}
if(a>=100 || b>=100 || c>=100)
{
return 0;
}
ret = 0;
double sum = a+b+c;
ret = a*(solve(a+1,b,c)+1)/sum + b*(solve(a,b+1,c)+1)/sum + c*(solve(a,b,c+1)+1)/sum;
dp[a][b][c]=ret;
return ret;
}
int main()
{
//c_p_c();
int a,b,c;
SC(a);SC(b);SC(c);
/*double sum = a+b+c;
double p = (100-a)*(a/sum);
double q = (100-b)*(b/sum);
double r = (100-c)*(c/sum);
double ans = p+q+r;*/
//memset(dp,255,sizeof(dp));
/*RIP(i,Max)
{
RIP(j,Max)
{
RIP(k,Max)
{
dp[i][j][k]=255;
}
}
}*/
double ans = solve(a,b,c);
cout << fixed << setprecision(9) << ans << nl;
}
|
#include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
void solve() {
int n, m;
cin >> n >> m;
vector<vector<int>> G(n);
while (m--) {
int a, b;
cin >> a >> b;
--a, --b;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
uint32_t ans{};
for (int s = 0; s < 1 << n; s++) {
uint32_t tmp{1};
queue<int> q;
vector<int> checked(n);
vector<int> group(n);
for (int v = 0; v < n; v++) {
if (s >> v & 1) {
checked[v] = 1;
for (auto &&to : G[v]) {
if (checked[to]) {
tmp = 0;
}
}
}
}
for (int v = 0; v < n; v++)
if (!checked[v]) {
bool fail = 0;
q.push(v);
while (!q.empty()) {
auto now = q.front();
q.pop();
for (auto &&to : G[now]) {
if (checked[to]) {
if (s >> to & 1) continue;
if (group[to] == group[now]) {
fail = 1;
}
} else {
q.push(to);
checked[to] = 1;
group[to] = !group[now];
}
}
}
if (fail) {
tmp = 0;
} else {
tmp *= 2;
}
}
ans += tmp;
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 21;
vector<int> g[N];
bool vis[N];
vector<int> cc;
void dfs(int u) {
cc.push_back(u);
vis[u] = 1;
for (int v : g[u]) if (!vis[v]) dfs(v);
}
int nxt(int x, int b) {
x += b + 1;
if (x > 3) x -= 3;
return x;
}
short col[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
rep(e, 0, m) {
int u, v; cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
ll res = 1;
rep(u, 0, n) {
if (vis[u]) continue;
cc.clear(); dfs(u);
int mx = sz(cc) - 1;
mx = 1 << mx;
ll tmp = 0;
rep(S, 0, mx) {
for (auto u : cc) col[u] = 0;
col[u] = 1;
bool valid = 1;
int T = S;
int cur = 0;
for (auto u : cc)
for (auto v : g[u])
if (col[v]) valid &= col[u] != col[v];
else col[v] = nxt(col[u], T >> cur++ & 1);
tmp += valid;
}
res *= 3 * tmp;
}
cout << res << '\n';
} |
#include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
#pragma GCC optimize("O3")
#ifdef LOCAL
#include "/Users/lbjlc/Desktop/coding/debug_utils.h"
#else
#define print(...) ;
#define printn(...) ;
#define printg(...) ;
#define fprint(...) ;
#define fprintn(...) ;
#endif
#define rep(i, a, b) for(auto i = (a); i < (b); i++)
#define rrep(i, a, b) for(auto i = (a); i > (b); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
// #define mp make_pair
#define fi first
#define se second
#define maxi(x, y) x = max(x, y)
#define mini(x, y) x = min(x, y)
// long long fact(long long n) { if(!n) return 1; return n*fact(n-1); }
// #define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int get_random() {
static uniform_int_distribution<int> dist(0, 1e9 + 6);
return dist(rng);
}
#define solve_testcase int T;cin>>T;for(int t=1;t<=T;t++){solve(t);}
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pdd> vpdd;
typedef vector<long long> vll;
#define bd(type,op,val) bind(op<type>(), std::placeholders::_1, val)
template<class T>
void make_unique(T & v) {
sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());
}
int geti() { int x; cin >> x; return x; }
long long getll() { long long x; cin >> x; return x; }
double getd() { double x; cin >> x; return x; }
// pair<int, int> a(geti(), geti()) does not work
// pair<int, int> a({geti(), geti()}) works, since it uses initializer.
const int MAXN = 3e5 + 100;
void solve(int tt) {
// cout<<"Case #"<<tt<<": ";
}
int main(int argc, char * argv[]) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// solve_testcase;
int n;
cin>>n;
string s;
cin>>s;
const string s110="110";
ll tot=1e10;
if(n==1) {
if(s[0]=='0') cout<<tot<<endl;
else cout<<2*tot<<endl;
return 0;
}
if(s[0]=='0') s = "11"+s;
else if(s[1]=='0') s = "1"+s;
n=s.size();
if(s[n-1]=='1') {
if(s[n-2]=='1') s+='0';
else s += "10";
}
print(s);
if(s.size()%3) {
cout<<0<<endl;
return 0;
}
int ok=1;
rep(i,0,n) {
if(s[i]!=(i%3==2?'0':'1'))
ok=0;
}
if(!ok) {
cout<<0<<endl;
return 0;
}
cout<<tot-ll(s.size())/3+1<<endl;
return 0;
}
| #include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> II;
typedef pair<LL, II> III;
typedef priority_queue<III, vector<III>, greater<>> Queue;
static const LL INF = 1LL << 60;
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; }
void solve(long long N, long long M, long long K, std::vector<long long> &A) {
vector<LD> e(N + M + 2);
vector<LD> pz(N + M + 2);
vector<LL> z(N + 2);
LD esum = 0;
LD zsum = 0;
for (int i = 0; i < K; i++) {
z[A[i]] = 1;
}
for (LL i = N - 1; i >= 0; --i) {
e[i] = 1.0 + esum / M;
if (z[i]) {
e[i] = 0;
pz[i] = 1.0;
} else {
pz[i] = zsum / M;
}
esum -= e[i + M];
esum += e[i];
zsum -= pz[i + M];
zsum += pz[i];
}
LD ans = e[0];
if (pz[0] > 0) {
if (pz[0] >= (1.0 - 1e-10)) {
cout << -1 << endl;
return;
}
LD a = 1.0 - pz[0];
ans /= a;
}
cout << ans << endl;
}
int main() {
cout.precision(10);
long long N;
std::cin >> N;
long long M;
std::cin >> M;
long long K;
std::cin >> K;
std::vector<long long> A(K);
for (int i = 0; i < K; i++) {
std::cin >> A[i]; // A[i]--;
}
solve(N, M, K, A);
return 0;
}
|
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#define rep(i, n) for(int i = 0; i < n; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 998244353
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }
template<class T, class U>
T &chmin(T &a, const U &b){ if(a > b) a = b; return a; }
int main(){
int h,w;
cin >> h >> w;
vector<string> board(h);
rep(i, h) cin >> board[i];
ll ans = 1;
rep(i, w){
int y = 0, x = i;
int b = 0, r = 0, n = 0;
while(y < h && x >= 0){
if(board[y][x] == 'B') b++;
if(board[y][x] == 'R') r++;
if(board[y][x] == '.') n++;
y++; x--;
}
if(b && r){
cout << "0\n";
return 0;
}else if(!b && !r) (ans *= 2) %= mod;
}
for(int i = 1; i < h; i++){
int y = i, x = w - 1;
int b = 0, r = 0, n = 0;
while(y < h && x >= 0){
if(board[y][x] == 'B') b++;
if(board[y][x] == 'R') r++;
if(board[y][x] == '.') n++;
y++; x--;
}
if(b && r){
cout << "0\n";
return 0;
}else if(!b && !r) (ans *= 2) %= mod;
}
cout << ans << "\n";
} | #include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 998244353LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
ll n,k,x,y;
//moddivとnCkとgarner
//c剰余下でaをbで割った値
ll moddiv(ll a,ll b,ll c){
ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
if (t==0LL) return a/b;
for(int i=0;i<900;i++){
m=x0/x1;
x2=x0-x1*m;
n2=(n0-m*n1)%c;
if (x2==1LL){
ans=(n2+c)%c;
break;
}
x0=x1;x1=x2;
n0=n1;n1=n2;
}
return (a+((t*ans)%c)*b-t)/b;
}
//nCk
vi mulmod(301);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
mulmod[0]=1LL;
for(ll i=1;i<301;i++){
mulmod[i]=mulmod[i-1]*i%MOD;
}
}
ll nCk(ll nn,ll kk){
return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD);
}
ll modinv(ll a,ll b){
return moddiv(1,a,b);
}
int main(){
ll ans=0;
first_nCk();
cin >> n >> k;
k++;
vi a(n);
REP(i,n) cin>>a[i];
vvi data(n, vi(k));
vi tmsum(k);
for(ll i=0;i<n;i++){
ll gege=1;
ll ggg=1;
for(ll j=0;j<k;j++){
data[i][j]=gege;
gege=a[i]*gege%MOD;
tmsum[j]+=ggg;
ggg=ggg*a[i]*2%MOD;
}
}
for(ll j=0;j<k;j++)
tmsum[j]%=MOD;
vi datasum(k);
for(ll i=0;i<n;i++){
for(ll j=0;j<k;j++){
datasum[j]+=data[i][j];
datasum[j]%=MOD;
}
}
for(ll i=1;i<k;i++){
ans=0;
ll tmp=MOD;
for(ll j=0;j<i+1;j++){
tmp=tmp+datasum[i-j]*datasum[j]%MOD*nCk(i,j)%MOD;
}
tmp=(tmp-tmsum[i])%MOD;
cout<<(moddiv(tmp,2,MOD))<<endl;
}
return 0;
} |
/*
power code taken from geeks for geeks
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
// THINGS TO REMEMBER
// ENDL is slow, '\n' is fast
// Clear everything (including graphs) between test cases
// use anti-anti-hash: https://codeforces.com/blog/entry/62393
// pb-ds: https://codeforces.com/blog/entry/11080
// check when to use LLONG_MAX/LLONG_MIN vs INT_MAX or INT_MIN
// You frequently suffer from confirmation bias - you trust your initial solution and miss simple things.
// When you hit a roadblock, remember to rethink the solution ground up, not just try hacky fixes
#define flush fflush(stdout);
#define PLL pair<ll,ll>
const ll INF = 1e18 + 5;
const ll MAX_N = 2e5 + 5;
const ll MODD = 1e9 + 7;
const ll MAX_M = 5e3 + 5;
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
double max(double a,double b){return a>b?a:b;}
double min(double a,double b){return a<b?a:b;}
ld max(ld a,ld b){return a>b?a:b;}
ld min(ld a,ld b){return a<b?a:b;}
ll modd(ll a){if(a<0LL) return a*-1LL;return a;}
double modd(double a){if(a<0.0) return a*-1.0;return a;}
ll modulo(ll a){
ll ans = a;
while(ans < 0LL) {
ans += MODD;
}
return (a + MODD) % MODD;
}
ll power(ll k, ll p){
if (p == 0)
return 1LL;
ll P = power(k, p/2LL);
P = modulo((P * P));
if (p % 2 == 1)
P = modulo((P * k));
return P;
}
int iii = 0;void _debug() {cout << "here " << (iii++) << endl;}
void solve() {
ll n, w;
cin >> n >> w;
cout << n / w << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
} | #include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
const int mod=1e9+7;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int a,b;
cin>>a>>b;
cout<<2*a+100-b<<"\n";
return 0;
}
|
#include <bits/stdc++.h>
#define pb push_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define f(i,b) for(int i=0;i<b;i++)
#define int long long
using namespace std;
void solve()
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i],a[i]%=200;
int ans=0;
map<int,int> mp;
for(auto z:a)
{
ans+=mp[z];
mp[z]++;
}
cout<<ans<<"\n";
return;
}
signed main()
{
int t=1;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define deb(a,b,c) cout<<#a<<" = "<<(a)<<", "<<#b<<" = "<<(b)<<", "<<#c<<" = "<<(c)<<endl;
typedef long long int ll;
typedef string str;
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
ll nCrModP(ll n, ll r, ll p)
{
if (!r)
return 1;
ll fac[n+1];
fac[0] = 1;
for (ll i=1 ; i<=n; i++)
fac[i] = fac[i-1]*i%p;
return (fac[n]* power(fac[r], p-2,p) % p * power(fac[n-r], p-2,p) % p) % p;
}
ll gcd(ll a, ll b){
if (a == 0)
return b;
return gcd(b % a, a);
}
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main ()
{
ll t,n,m,k,i,j,a,b;
// cin >> t;
str st,s;
cin >> n;
ll h[201]={0};
for(i=0;i<n;i++)
{
cin >> a;
a%=200;
h[a]++;
}
b = 0;
for(i=0;i<200;i++)
{
a = h[i];
b += (a*(a-1))/2;
}
cout << b << "\n";
return 0;
} |
#include<iostream>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,b,a) for(int i=b;i>=a;i--)
#define N 2021
using namespace std;
string map[N];
int fa[N];
int tf1[N],tf2[N];
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
int main(){
ios::sync_with_stdio(false);
int h,w;
cin>>h>>w;
rep(i,0,h+w)fa[i]=i;
rep(i,0,h-1)cin>>map[i];
map[0][0]=map[h-1][0]=map[0][w-1]=map[h-1][w-1]='#';
rep(i,0,h-1)rep(j,0,w-1)
if(map[i][j]=='#'){
int x=find(i),y=find(j+h);
if(x!=y)fa[x]=y;
}
int cnt1=0,cnt2=0;
rep(i,0,h-1){
int x=find(i);
if(!tf1[x])tf1[x]=true,cnt1++;
}
rep(j,h,h+w-1){
int y=find(j);
if(!tf2[y])tf2[y]=true,cnt2++;
}
cout<<min(cnt1,cnt2)-1<<endl;
return 0;
} | #include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
long power_mod(long a, long b, long m)
{
int k = 0;
long c = b;
long n = 1L;
while (c >= 1)
{
c >>= 1;
++k;
}
for (int i = k; i >= 0; --i)
{
n = ((b >> i) & 1) ? (n * n * a) % m : (n * n) % m;
}
return n;
}
int main()
{
const long P = 1000000007;
int H, W;
cin >> H >> W;
//string *S = new string[H];
bool **S = new bool *[H];
string Str;
int K = 0;
int **illuminate = new int *[H];
for (int i = 0; i < H; ++i)
{
cin >> Str;
S[i] = new bool[W];
illuminate[i] = new int[W];
for (int j = 0; j < W; j++)
{
S[i][j] = (Str.at(j) == '.');
K += (S[i][j] ? 1 : 0);
illuminate[i][j] = 0;
}
}
//horizontal
for (int i = 0; i < H; i++)
{
int j = 0;
while (j < W)
{
int len = 0;
while (j < W && !S[i][j])
{
j++;
}
while (j + len < W && S[i][j + len])
{
len++;
}
for (int k = 0; k < len; ++k)
{
illuminate[i][j + k] += len;
}
j = j + len;
}
}
//vertical
for (int j = 0; j < W; j++)
{
int i = 0;
while (i < H)
{
int len = 0;
while (i < H && !S[i][j])
{
i++;
}
while (i + len < H && S[i + len][j])
{
len++;
}
for (int k = 0; k < len; ++k)
{
illuminate[i + k][j] += len;
}
i = i + len;
}
}
long sum = 0;
long C = power_mod(2, K, P);
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (illuminate[i][j] > 0)
{
sum += C - power_mod(2, K - illuminate[i][j] + 1, P);
}
}
}
cout << (sum % P + P) % P;
return 0;
} |
#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PIL = pair<int,ll>;
using PLI = pair<ll,int>;
using PLL = pair<ll,ll>;
using Graph = vector<vector<int>>;
using Cost_Graph = vector<vector<PIL>>;
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;}
template<class T> void show_vec(T v) {for (int i=0;i<v.size();i++) cout<<v[i]<<endl;}
template<class T> void show_pair(T p) {cout<<p.first<<" "<<p.second<<endl;}
template<class T> bool judge_digit(T bit,T i) {return (((bit&(1LL<<i))!=0)?1:0);}
#define REP(i,n) for(int i=0;i<int(n);i++)
#define ROUNDUP(a,b) ((a+b-1)/b)
#define YESNO(T) cout<<(T?"YES":"NO")<<endl
#define yesno(T) cout<<(T?"yes":"no")<<endl
#define YesNo(T) cout<<(T?"Yes":"No")<<endl
const int INFint = 1 << 29;
const ll INFLL = 1LL << 60;
const ll MOD = 1000000007LL;
const double pi = 3.14159265358979;
const vector<int> h_idx4 = {-1, 0,0,1};
const vector<int> w_idx4 = { 0,-1,1,0};
const vector<int> h_idx8 = {-1,-1,-1, 0,0, 1,1,1};
const vector<int> w_idx8 = {-1, 0, 1,-1,1,-1,0,1};
int n;
string s,t;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
cin >> n >> s >> t;
int s0 = 0,s1 = 0,t0 = 0,t1 = 0;
for (int i = 0; i < n; i++){
if (s[i] == '0') s0++;
else s1++;
if (t[i] == '0') t0++;
else t1++;
}
if (s0 != t0){
cout << -1 << endl;
return 0;
}
vector<ll> ss,tt;
for (int i = 0; i < n; i++){
if (s[i] == '0') ss.push_back(i);
if (t[i] == '0') tt.push_back(i);
}
int m = ss.size();
int ans = 0;
for (int i = 0; i < m; i++){
if (ss[i] != tt[i]) ans++;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define debug(output) if(debugFlag)cout<<#output<<"= "<<output<<endl
using lint = long long;
typedef pair<int,int> P;
const bool debugFlag=true;
const lint linf=1e18+7;
const lint inf=1e9+7;
const int MOD=1000000007;
signed main(){
int n;cin>>n;
string s;cin>>s;
string t;cin>>t;
lint res=0;
vector<int> a,b;
a.push_back(-1);
b.push_back(-1);
rep(i,n){
if(s[i]=='1')a.push_back(i);
if(t[i]=='1')b.push_back(i);
}
if(a.size()!=b.size()){
cout<<-1<<"\n";
return 0;
}
rep1(i,a.size()-1){
int move=b[i-1]-a[i-1];
int sub=b[i]-a[i];
if(move<=0){
if(b[i]<=a[i-1])res+=a[i]-a[i-1]-1;
else res+=abs(sub);
}
else if(move>0){
if(b[i-1]>=a[i])res+=b[i]-(b[i-1]+1);
else res+=abs(sub);
}
}
cout<<res<<"\n";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int N = 209;
int A, B, C;
double f[N][N][N];
signed main()
{
scanf("%d %d %d", &A, &B, &C);
if (A > B) swap(A, B);
if (A > C) swap(A, C);
if (B > C) swap(B, C);
if (A == 0 && B == 0) printf("%d\n", 100 - C);
else
{
f[A][B][C] = 1;
for (int i = A; i < 100; i++)
for (int j = B; j < 100; j++)
for (int k = C; k < 100; k++)
{
if ((A == 0 && i != 0) || (B == 0 && j != 0) || (C == 0 && k != 0))
continue;
int tmp = i + j + k;
if (i != 0) f[i + 1][j][k] = f[i + 1][j][k] + 1.0 * f[i][j][k] / tmp * i;
if (j != 0) f[i][j + 1][k] = f[i][j + 1][k] + 1.0 * f[i][j][k] / tmp * j;
if (k != 0) f[i][j][k + 1] = f[i][j][k + 1] + 1.0 * f[i][j][k] / tmp * k;
}
double ans = 0;
for (int i = A; i <= 100; i++)
for (int j = B; j <= 100; j++)
for (int k = C; k <= 100; k++)
{
int tmp = (i == 100) + (j == 100) + (k == 100);
if (tmp > 1 || tmp == 0) continue;
ans += f[i][j][k] * (i - A + j - B + k - C);
}
printf("%.10lf\n", ans);
}
return 0;
} | #include <iostream>
#include <map>
#include <tuple>
#include <algorithm>
#include <iomanip>
#include <array>
int main()
{
int A, B, C;
std::cin >> A >> B >> C;
std::array<int, 3> init_status = {A,B,C};
std::sort(init_status.begin(), init_status.end());
std::map<std::array<int,3>, double> status;
status.emplace(init_status, 1);
double coinsExpected = 0.0;
while( !status.empty() )
{
std::map<std::array<int, 3>, double> statusNext;
for (const auto& e : status)
{
const auto& state = e.first;
double coins = state[0] + state[1] + state[2];
double pA = e.second * state[0] / coins;
double pB = e.second * state[1] / coins;
double pC = e.second * state[2] / coins;
if (pA != 0)
{
if (state[0] == 99)
{
coinsExpected += pA * (coins + 1);
}
else
{
auto next = state;
next[0] += 1;
std::sort(next.begin(), next.end());
statusNext[next] = (statusNext.count(next) ? statusNext[next] : 0.0) + pA;
}
}
if (pB != 0)
{
if (state[1] == 99)
{
coinsExpected += pB * (coins + 1);
}
else
{
auto next = state;
next[1] += 1;
std::sort(next.begin(), next.end());
statusNext[next] = (statusNext.count(next) ? statusNext[next] : 0.0) + pB;
}
}
if (pC != 0)
{
if (state[2] == 99)
{
coinsExpected += pC * (coins + 1);
}
else
{
auto next = state;
next[2] += 1;
std::sort(next.begin(), next.end());
statusNext[next] = (statusNext.count(next) ? statusNext[next] : 0.0) + pC;
}
}
}
status = statusNext;
}
std::cout <<
std::setprecision(14) <<
coinsExpected-A-B-C << std::endl;
return 0;
}
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long tnum;
#define loop(i, num) for(int i = 0; i < num; i++)
tnum* tnumAlloc(int num);
tnum getNumStdin();
tnum* getNumsStdin(int);
char* getCharsStdin(int maxlength);
string getStringStdin();
void printlnNums(int count, tnum* nums);
void printlnNum(tnum num);
void printString(string str);
#define min(a,b) (a <= b ? a : b)
#define max(a,b) (a >= n ? a : b)
/*void sort(T* start, T* end) (in <algorithm>)
example:
int a[5] = {2, 1, 3, 5, 4};
sort(a, a + 5);
-> a: {1, 2, 3, 4, 5}
*/
int main() {
tnum m = getNumStdin();
tnum h = getNumStdin();
if (h % m == 0) {
printf("Yes");
}
else {
printf("No");
}
return 0;
}
//have to be free()
tnum* tnumAlloc(int num) {
return (tnum*)calloc(num,sizeof(tnum));
}
tnum getNumStdin() {
tnum ret;
cin >> ret;
return ret;
}
//have to be free()
tnum* getNumsStdin(int num) {
tnum* buf;
tnum* tmp;
buf = tnumAlloc(num);
for (int i = 0; i < num; i++) {
cin >> buf[i];
}
return buf;
}
//have to be free()
char* getCharsStdin(int maxlength) {
char* ret = (char*)malloc(maxlength+1);
scanf("%s", ret);
return ret;
}
//have to be free()
string getStringStdin() {
string ret;
cin >> ret;
return ret;
}
void printlnNum(tnum num) {
cout << num << endl;
}
// can be used for debugging
void printlnNums(int count, tnum *nums) {
loop(i, count) {
cout << nums[i];
if (i != count - 1) printf(" ");
}
printf("\n");
}
void printString(string str) {
cout << str;
} | #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(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//
int main() {
int N;
cin >> N;
vector<int> A(2 * N);
REP(i, 2 * N) scanf("%d", &A[i]);
ll ans = accumulate(ALL(A), 0ll);
priority_queue<int, vector<int>, greater<int> > pq;
REP(i, N) {
pq.emplace(A[N - i - 1]);
pq.emplace(A[N + i]);
ans -= pq.top();
pq.pop();
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
int mul (int a, int b) { return 1ll * a * b % MOD; }
int main() {
int n;
scanf("%d", &n);
vector<int> a(n);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
a.push_back(0);
sort(a.begin(), a.end());
int ans = 1;
for (int i = 1; i <= n; i++) {
ans = mul(ans, a[i] - a[i - 1] + 1);
}
printf("%d\n", ans);
return 0;
} | #include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
const ll MOD = 1000000007;
int main(){
ll n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
sort(a.begin(), a.end());
ll ans = 1;
rep(i, n){
if (i == 0)
ans *= (a[i] + 1);
else{
ll diff = a[i] - a[i-1];
if (diff == 0) continue;
ans *= (diff + 1);
}
ans %= MOD;
}
cout << ans << endl;
return 0;
} |
#include<iostream>
using namespace std;
int main(){
int N;
cin>>N;
if(N%2==0)
cout<<"White";
else
cout<<"Black";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int main()
{
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int N,i=1,save=0;
cin>>N;
while(save<N)
{
save=save+i;
i++;
}
cout<<i-1<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
static const ll mod=998244353;
ll N,K;
ll power[301];
ll modpow(ll a,ll n){
ll res=1;
while(0<n){
if(n%2==1)res=(res*a)%mod;
a=(a*a)%mod;
n>>=1;
}return res;
}
ll fac[301];
ll rev[301];
ll comb(ll n,ll k){
if(n<k)return 0;
ll x=(fac[n]*rev[k])%mod;
x=(x*rev[n-k])%mod;return x;
}
int main(){
cin>>N>>K;ll rev2=modpow(2,mod-2);
fac[0]=1;rev[0]=1;
for(ll i=1;i<=300;i++){
fac[i]=(i*fac[i-1])%mod;
rev[i]=modpow(fac[i],mod-2);
}
vector<ll>A(N);
for(ll i=0;i<N;i++)
cin>>A[i];power[0]=N;
for(ll i=0;i<N;i++){
ll a=A[i];
for(ll j=1;j<=300;j++){
power[j]=(power[j]+a)%mod;
a=(a*A[i])%mod;
}
}for(ll k=1;k<=K;k++){
ll ans=0;
for(ll i=1;i<=k/2;i++){
ll x=(comb(k,i)*(mod-power[k]+(power[i]*power[k-i])%mod))%mod;
ans=(ans+x)%mod;
}
ans=(ans+(N-1)*power[k])%mod;
if(k%2==0){ ll x=(comb(k,k/2)*(mod-power[k]+(power[k/2]*power[k/2])%mod))%mod;
x=(x*rev2)%mod;
ans=(mod+ans-x)%mod;}
cout<<ans<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1000000007;
int f[100000][2] = {0}, times[100000][2] = {0};
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n >> f[0][0];
times[0][0] = 1;
for (int i = 1; i < n; ++i) {
times[i][0] = (times[i - 1][0] + times[i - 1][1]) % mod;
times[i][1] = times[i - 1][0];
cin >> f[i][1];
f[i][0] = ((f[i - 1][0] + f[i - 1][1]) % mod + (ll)f[i][1] * times[i][0] % mod) % mod;
f[i][1] = (f[i - 1][0] - (ll)f[i][1] * times[i][1]) % mod;
if (f[i][1] < 0) f[i][1] += mod;
}
cout << (f[n - 1][0] + f[n - 1][1]) % mod;
} |
#include "bits/stdc++.h"
#include <chrono>
#include <random>
#define lli long long int
using namespace std;
#define mod 1000000007
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define INF 1000000000
#define common cout << "Case #" << w+1 << ": "
#define maxn 2000010
void setIO(string name) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
template< typename T>
void PVecPrint(vector<T>&v)
{
for(int i=0;i<(int)v.size();i++)
cout << v[i].first << "," << v[i].second << ' ';
cout << '\n';
}
template<class T>
void VecPrint(vector<T>&v)
{
for(int i=0;i<v.size();i++)
cout << v[i] << ' ';
cout << '\n';
}
/*-------------------------------------------------------------------Code-----------------------------------------------------------------*/
typedef complex<double> point;
#define x real()
#define y imag()
double piv= (double)3.141592653589793/(double)180;
point func(point X,int val)
{
double theta = piv*val;
point Xnew=(X * polar(1.0, theta));
return Xnew;
}
bool iseq(point p1,point p2)
{
if(abs(p1.x-p2.x)<=0.1 and abs(p1.y-p2.y)<=0.1)
return true;
return false;
}
bool found(point p,vector<point>&v)
{
for(int i=0;i<v.size();i++)
{
if(iseq(p,v[i]))
return true;
}
return false;
}
bool func(vector<point>&v1,vector<point>&v2)
{
for(int i=0;i<v1.size();i++)
{
if(!found(v1[i],v2))
return false;
}
return true;
}
int main()
{
fastio;
int n;
cin >> n;
vector<point>v1(n),v2(n);
for(int i=0;i<n;i++)
{
int x1,y1;
cin >> x1 >> y1;
point t(x1,y1);
v1[i]=t;
}
for(int i=0;i<n;i++)
{
int x1,y1;
cin >> x1 >> y1;
point t(x1,y1);
v2[i]=t;
}
int f=0;
for(int i=0;i<360 and f==0;i++)
{
vector<point>v3(n);
for(int j=0;j<n;j++)
v3[j]=func(v1[j],i);
for(int k=0;k<n;k++)
{
// cout << i << ' ' << k << '\n';
double dx = v2[k].x-v3[0].x,dy=v2[k].y-v3[0].y;
for(int j=0;j<n;j++)
v3[j]=point(v3[j].x+dx,v3[j].y+dy);
if(func(v3,v2))
f=1;
}
}
if(f==1)
cout << "Yes\n";
else
cout << "No\n";
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
ll pow(P p1, P p2) {
ll x0 = p1.first; ll y0 = p1.second;
ll x1 = p2.first; ll y1 = p2.second;
return (x0-x1)*(x0-x1)+(y0-y1)*(y0-y1);
}
int main(){
ll n; cin >> n;
vector<P> s(n);
vector<P> t(n);
REP(i,n) cin >> s[i].first >> s[i].second;
REP(i,n) cin >> t[i].first >> t[i].second;
if (n==1) {
cout << "Yes" << endl;
return 0;
}
if (n==2) {
if (pow(s[0],s[1])==pow(t[0],t[1])) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
ll xt = t[0].first; ll yt = t[0].second;
REP(i,n) t[i].first-=xt, t[i].second-=yt;
// REP(i,n) cout << s[i].first << " " << s[i].second << " " << t[i].first << " " << t[i].second << endl;
REP(i,n) REP(j,n){
if (i==j) continue;
ll d0 = pow(s[i],s[j]); ll d1 = pow(t[0],t[1]);
if (d0!=d1) continue;
vector<P> u(n);
ll x = s[i].first; ll y = s[i].second;
REP(k,n) u[k].first = s[k].first-x, u[k].second = s[k].second-y;
// cout << "test2 " << i << " " << j << endl; cout << x << " " << y << endl;
// cout << "u" << endl;
// REP(k,n) cout << u[k].first << " " << u[k].second << endl;
ll a, b, c;
x = u[j].first; y = u[j].second;
ll z = t[1].first; ll w = t[1].second;
b = y*z-x*w; a = z*x+w*y; c = x*x+y*y;
// cout << a << " " << b << " " << c << endl;
vector<P> uu(n);
REP(k,n) {
uu[k].first = a*u[k].first+b*u[k].second;
uu[k].second = a*u[k].second-b*u[k].first;
}
swap(u,uu);
vector<P> v(n);
REP(k,n) v[k].first = t[k].first*c, v[k].second = t[k].second*c;
sort(u.begin(),u.end());
sort(v.begin(),v.end());
// cout << "test1 " << i << " " << j << endl;
// cout << "u" << endl;
// REP(k,n) cout << u[k].first << " " << u[k].second << endl;
// cout << "v" << endl;
// REP(k,n) cout << v[k].first << " " << v[k].second << endl;
bool judtmp = true;
REP(k,n) if (v[k]!=u[k]) judtmp = false;
if (judtmp) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
} |
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
//#include <set>
using namespace std;
typedef long long LONG;
LONG gcd(LONG x, LONG y) {
if(x % y == 0) {
return y;
} else {
return gcd(y, x % y);
//x%y==0でないときはユーグリットの互除法を使って再帰的に関数を呼び出す。
}
}
void print(vector<int>A){
for(size_t i=0;i<A.size();i++){
if(A[i]==1){
cout<<'(';
}else{
cout<<')';
}
}
}
bool comp1(pair<int,int> lhs, pair<int,int>rhs) {
return lhs.first<rhs.first;
}
bool comp2(pair<string,int> lhs, pair<string,int>rhs) {
return lhs.second<rhs.second;
}
void solve(int min,int max,vector<int>&A,int B){
if(min+1==max){
cout<<std::min(A[max]-B,B-A[min])<<endl;
return;
}
int a=(min+max)/2;
if(A[a]<B){
solve(a,max,A,B);
}else{
solve(min,a,A,B);
}
}
LONG pow2(LONG a,LONG b){
LONG res=1;
for(int i=0;i<b;i++){
res=res*a;
}
return a;
}
void solve1(vector<bool>&A,vector<LONG>&C,int pos,vector<vector<pair<int,LONG>>>&path){
for(int i=0;i<path[pos].size();i++){
if(!A[path[pos][i].first]){
A[path[pos][i].first]=true;
C[path[pos][i].first]=C[pos]^path[pos][i].second;
solve1(A,C,path[pos][i].first,path);
}
}
}
const LONG mod=1000000007;
int main() {
int N;
cin>>N;
vector<LONG>A(N);
for(int i=0;i<N;i++){
cin>>A[i];
}
vector<LONG>B(N);
B[0]=A[0];
map<LONG,LONG>C;
map<LONG,LONG>D;
C[A[0]]=1;
for(int i=1;i<N;i++){
B[i]=A[i]-B[i-1];
if(i%2==0){
if(C.count(B[i])==0){
C[B[i]]=1;
}else{
C[B[i]]++;
}
}else{
if(D.count(B[i])==0){
D[B[i]]=1;
}else{
D[B[i]]++;
}
}
}
LONG result=0;
for(int i=1;i<N+1;i++){
if(i%2==0){
result+=C[-B[i-1]];
result+=D[B[i-1]]-1;
}else{
result+=C[B[i-1]]-1;
result+=D[-B[i-1]];
}
}
result=result/2;
result+=C[0]+D[0];
cout<<result<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 5;
int n;
int a[N];
map<int , int> mp;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1 ; i <= n ; ++i) cin >> a[i];
int cur = 0 , ans = 0;
mp[0] = 1;
for (int i = 1 ; i <= n ; ++i)
{
if (i & 1) cur += a[i];
else cur -= a[i];
ans += mp[cur];
mp[cur]++;
}
cout << ans;
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N,M;
string a;
int x,y;
char dna[20][20];
cin >> N >> M;
x=y=0;
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++){
dna[i][j]='.';
}
}
for(int i=0; i<M; i++) {
cin >> a;
if(a.size()>5) {
for(int i=0; i<a.size(); i++){
dna[y][x]=a[i];
x++;
if(x>=N){y++;x=0;}
if(y>=N){y=0;x=0;}
}
}
}
for(int y=0; y<N; y++) {
for(int x=0; x<N; x++){
cout << dna[y][x];
}
cout << endl;
}
};
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);
template <typename T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T &a, const T &b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
ll n, m;
void visualizer(vector<vector<char>> gene)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << gene[i][j];
}
cout << endl;
}
}
vector<vector<char>> make_initial_solution(priority_queue<pair<int, string>> q)
{
vector<vector<char>> gene(n, vector<char>(n, '.'));
while (q.size())
{
auto p = q.top();
q.pop();
bool tate, yoko, best_flag = false; // trueが横,falseが縦
int x, y;
ll best_score = INF;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
ll score = 0;
bool flag = true;
for (int k = 0; k < p.first; k++)
{
if (gene[i][(j + k) % n] != p.second[k] && gene[i][(j + k) % n] != '.')
{
flag = false;
break;
}
if (gene[i][(j + k) % n] != p.second[k])
{
score++;
}
}
if (flag)
{
if (chmin(best_score, score))
{
yoko = true;
tate = false;
best_flag = true;
x = i, y = j;
}
}
flag = true;
score = 0;
for (int k = 0; k < p.first; k++)
{
if (gene[(i + k) % n][j] != p.second[k] && gene[(i + k) % n][j] != '.')
{
flag = false;
break;
}
if (gene[(i + k) % n][j] != p.second[k])
{
score++;
}
}
if (flag)
{
if (chmin(best_score, score))
{
yoko = false;
tate = true;
best_flag = true;
x = i, y = j;
}
}
}
}
if (!best_flag)
{
continue;
}
for (int i = 0; i < p.first; i++)
{
gene[(x + (i * tate)) % n][(y + (i * yoko)) % n] = p.second[i];
}
}
return gene;
}
int main(void)
{
cin >> n >> m;
priority_queue<pair<int, string>> q;
for (int i = 0; i < m; i++)
{
string s;
cin >> s;
q.push({s.size(), s});
}
auto initial_gene = make_initial_solution(q);
visualizer(initial_gene);
} |
#include<bits/stdc++.h>
#define int long long
#define REP(i,a,n) for(int i=a;i<(n);i++)
#define REP_sz(i,s) for(int i=0;i<s.size();i++)
#define gcd(a,b) __gcd(a,b)
#define RE return
#define FILL(a,b) memset(a,b,sizeof(a))
#define SO(a) sort(all(a))
#define all(a) a.begin(),a.end()
#define pb push_back
#define sz(a) a.size()
#define V vector
#define ld long double
#define viit(a) vector<int>::iterator a
#define IT iterator
#define SET setprecision
#define FOR(i,a,n) for(int i=a;i<=(n);i++)
#define ER1(a) a.erase(a.begin())
#define ER0(a) a.erase(a.end())
#define pii pair<int,int>
#define pause system("PAUSE")
#define cls system("CLS")
#define mod1 1000000007
#define mod2 998244353
#define infmod 9223372036854775783
#define tc int t;cin>>t;while(t--)solve();
using namespace std;
int ans;
int n,k;
pii a[105],b[105];
map<int,bool>flag;
int z;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>z>>n;
REP(i,0,n)cin>>a[i].first>>a[i].second;
cin>>k;
REP(i,0,k)cin>>b[i].first>>b[i].second;
REP(i,0,1<<k){
int x=i;
REP(j,0,k){
if(x%2==0)flag[b[j].first]=1;
else flag[b[j].second]=1;
x/=2;
}
int cnt=0;
REP(j,0,n)
if(flag[a[j].first]&&flag[a[j].second])cnt++;
ans=max(ans,cnt);
map<int,bool>::IT it;
for(it=flag.begin();it!=flag.end();it++)it->second=0;
}
cout<<ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<long long>
#define f first
#define s second
#define pb push_back
#define printoneline(arr) for(long long i=0;i<arr.size();i++){cout<<arr[i]<<" ";} cout<<"\n";
#define sort(a) sort(a.begin(),a.end());
#define rsort(a) sort(a.rbegin(),a.rend());
#define reverse(a) reverse(a.begin(),a.end());
#define input(arr) for(long long i=0;i<arr.size();i++){cin>>arr[i];}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll N,W;
cin>>N>>W;
cout<<(N/W)<<"\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define repp(i,m,n) for(lint (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
#define all(x) (x).begin(),(x).end()
const lint MOD =1000000007;
//const lint MOD=998244353;
const lint MAX = 4000000;
using Graph =vector<vector<lint>>;
typedef pair<lint,lint> P;
typedef map<lint,lint> M;
#pragma GCC optimize("Ofast")
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
lint fac[MAX], finv[MAX], inv[MAX];
void COMinit()
{
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (lint i = 2; i < MAX; i++)
{
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
long long COM(lint n, lint k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
lint primary(lint num)
{
if (num < 2) return 0;
else if (num == 2) return 1;
else if (num % 2 == 0) return 0;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2)
{
if (num % i == 0)
{
return 0;
}
}
return 1;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
lint lcm(lint a,lint b){
return a/__gcd(a,b)*b;
}
lint gcd(lint a,lint b){
return __gcd(a,b);
}
int main(){
string s;
cin>>s;
bool isrev=false;
deque<char> que;
rep(i,s.size()){
if(s[i]=='R'){
if(isrev)isrev=false;
else isrev=true;
}else{
if(que.empty())que.push_front(s[i]);
else if(isrev){
char t=que.front();
if(t==s[i])que.pop_front();
else que.push_front(s[i]);
}else{
char t=que.back();
if(t==s[i])que.pop_back();
else que.push_back(s[i]);
}
}
}
lint g=que.size();
if(isrev){
rep(i,g){
cout<<que.back();
que.pop_back();
}
}else{
rep(i,g){
cout<<que.front();
que.pop_front();
}
}
cout<<endl;
}
| #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
vector<int> vDice(3);
for (int i = 0; i < 3; i++)
{
cin >> vDice.at(i);
}
vector<int> vFind(6);
for (int i = 0; i < 6; i++)
{
std::vector<int>::iterator itr;
size_t n_count = std::count(vDice.begin(), vDice.end(), i+1);
vFind.at(i) = n_count;
}
size_t count = std::count(vFind.begin(), vFind.end(), 1);
if (count == 3)
{
cout << 0 << endl;
}
else
{
for (int i = 0; i < 6; i++)
{
if (vFind.at(i) != 0 && vFind.at(i) != 2)
{
cout << i + 1 << endl;
break;
}
}
}
} |
/*
* @Author: tusikalanse
* @Date: 2021-06-12 20:33:13
* @LastEditTime: 2021-06-12 20:37:22
* @LastEditors: tusikalanse
* @Description:
* @FilePath: /undefined/mnt/e/algo/atcoder/arc/122/b.cpp
*/
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL gcd(LL a, LL b) {
return b == 0 ? a : gcd(b, a % b);
}
const int N = 1e5 + 10;
int n, a[N];
double check(double x) {
double ans = 0;
for (int i = 1; i <= n; ++i)
ans += x + a[i] - min(0.0 + a[i], 2 * x);
return ans / n;
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
double l = 0, r = 1e9;
while (r - l > 1e-6) {
double ll = l + (r - l) / 3;
double rr = r - (r - l) / 3;
if (check(ll) > check(rr))
l = ll;
else
r = rr;
}
printf("%.15f\n", check(l));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << ' '; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"
#define ll long long
#define ld long double
#define ull unsigned long long
#define pii pair<int,int>
#define MOD 1000000007
#define zeros(x) x&(x-1)^x
#define fi first
#define se second
const long double PI = acos(-1);
ll n, V[100005], SP[100005];
int main()
{
ios::sync_with_stdio(false);
cin >> n;
for (int i=1; i<=n; i++)
{
cin >> V[i];
}
sort(V+1, V+n+1);
for (int i=1; i<=n; i++)
{
SP[i] = SP[i-1] + V[i];
}
ld ans = 1e18;
for (int i=1; i<=n; i++)
{
ll loc = (2 * i - n);
ld x;
if (loc < 0)
x = 1.0 * V[i+1] / 2;
else
x = 1.0 * V[i] / 2;
ans = min(ans, x * loc - SP[i]);
}
cout << setprecision(8) << fixed << (ans + SP[n]) / n;
return 0;
} |
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//#include <atcoder/convolution>
//#include <atcoder/modint>
#define bug(x) cout << "zdongdebug1: " << x << endl;
#define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl;
#define bug3(x, y, z) \
cout << "zdongdebug3: " << x << " " << y << " " << z << endl;
#define bug4(x, y, z, w) \
cout << "zdongdebug4: " << x << " " << y << " " << z << " " << w << endl;
using namespace std;
// using Modint = atcoder::modint998244353;
typedef long long ll;
void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
ex_gcd(b, a % b, d, y, x);
y -= a / b * x;
}
}
ll inv(ll a, ll n) {
ll d, x, y;
ex_gcd(a, n, d, x, y);
return d == 1 ? (x % n + n) % (n / d) : -1;
}
ll gcd(ll x, ll y) {
if (y == 0) return x;
return gcd(y, x % y);
}
const int maxn = 1000005;
const int mod = 998244353;
int main() {
#ifdef suiyuan2009
freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
// freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
int x,y,z;
cin>>x>>y>>z;
cout<<21-x-y-z<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int a,b,c;
int main(void){
scanf("%d%d%d",&a,&b,&c);
printf("%d\n",7-a+7-b+7-c);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main(void){
int n,m;
cin >> n >> m;
vector<vector<int>> pos(n);
rep(i,n){
int a;
cin >> a;
pos[a].push_back(i);
}
rep(i,n){
int pre = -1;
pos[i].push_back(n);
for(int j:pos[i]){
if(j-pre > m){
cout << i << endl;
return 0;
}
pre = j;
}
}
cout << n << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned ll
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vb vector<bool>
#define vd vector<double>
#define vs vector<string>
#define vvi vector<vi>
#define ff first
#define ss second
#define pb push_back
#define vpi vector<pi>
#define vpll vector<pll>
#define umap unordered_map
#define uset unordered_set
#define all(x) x.begin(),x.end()
#define fr(i,j,n) for(int i=j;i<n;++i)
#define rfr(i,j,n) for(int i=j; i>=n;--i)
#define MOD 1000000007
#define INF INT_MAX
#define LINF LONG_LONG_MAX
#define mp make_pair
#define endl "\n"
#define sz size()
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
void solve(){
int n,m;
cin>>n>>m;
vi arr(n);
vvi pos(n+1);
fr(i,0,n+1){
// cin>>arr[i-1];
pos[i].pb(-1);
}
fr(i,0,n){
cin>>arr[i];
pos[arr[i]].pb(i);
}
fr(i,0,n+1){
// cin>>arr[i-1];
pos[i].pb(n);
}
fr(i,0,n+1){
fr(j,1,pos[i].sz){
if(pos[i][j]-pos[i][j-1]>m){
cout<<i<<endl;
return;
}
}
}
}
void pre() {
int t = 1;
// cin>>t;
while(t--){
solve();
// cout<<solve()<<endl;
}
}
int main() {
fastIO;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
pre();
return 0;
}
|
// ###### ### ####### ####### ## # ##### ### ##### //
// # # # # # # # # # # # # # ### //
// # # # # # # # # # # # # # ### //
// ###### ######### # # # # # # ######### # //
// # # # # # # # # # # #### # # # //
// # # # # # # # ## # # # # # //
// ###### # # ####### ####### # # ##### # # # # //
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a,n) for(ll i=0;i<(n);++i) cin>>(a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<=(b);++i)
#define FORD(i,a,b) for(ll i=(a);i>=(b);--i)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define mp make_pair
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 100005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 1000005;
const long double epsilon = 1e-9;
inline ll myceil(ll a, ll b) {
return (a + b - 1) / b;
}
ll binexp(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll modinvfermat(ll a, ll m)
{
return binexp(a, m - 2, m);
}
void task(bool flag)
{
if (flag)
cout << "YES\n";
else
cout << "NO\n";
}
ll lcm(ll a, ll b)
{
return ((1LL * a * b) / (__gcd(a, b)));
}
bool check(ll n)
{
ll m = n;
while (n > 0) {
if (n % 8 == 7) {
return false;
} else {
n /= 8;
}
}
while (m > 0) {
if (m % 10 == 7) {
return false;
} else {
m /= 10;
}
}
return true;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast
ll n;
cin >> n;
ll cnt = 0;
FOR(i, 1, n) {
if (check(i)) {
++cnt;
}
}
cout << cnt << END;
#ifndef ONLINE_JUDGE
cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
return 0;
}
| #include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
if(p) cout << fixed << setprecision(p) << a << "\n";
else cout << a << "\n";
}
template <typename T> inline void voutput(T &v){
rep(i, v.size()){
if (i) cout << " " << v[i];
else cout << v[i];
}
cout << endl;
}
// end of template
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
// source code
int N;
cin >> N;
vector<ll> A(2 * N);
ll sum = 0;
rep(i, 2 * N) {
cin >> A[i];
sum += A[i];
}
priority_queue<ll, vector<ll>, greater<ll>> pq;
ll r = 0;
rep(i, N) {
pq.push(A[N - 1 - i]);
pq.push(A[N + i]);
r += pq.top();
pq.pop();
}
output(sum - r);
return 0;
}
|
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define endl '\n'
#define mod 1000000007
#define mmod 998244353
#define inf 1000000000000000000
#define PI 3.141592653589793238
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define clr(x,n) x.clear();x.resize(n,0)
#define precise(x) fixed<<setprecision(x)
#define popcount(x) __builtin_popcountll(x)
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_multiset;
/**
* author: nayakashutosh9
* created: [2021-04-18 17:27]
* "Compete against yourself"
**/
const int N = 3e5 + 5;
void solve() {
int a, b; cin >> a >> b;
vi pos, neg;
int n = max(a, b);
for (int i = 1; i <= n; i++) pos.pb(i);
for (int i = 1; i <= n; i++) neg.pb(-i);
if (a >= b) {
int dif = a - b;
while (dif > 0) {
neg[sz(neg) - 2] += neg[sz(neg) - 1];
neg.pop_back();
dif--;
}
}
else {
int dif = b - a;
while (dif > 0) {
pos[sz(pos) - 2] += pos[sz(pos) - 1];
pos.pop_back();
dif--;
}
}
for (int i : pos) cout << i << " ";
for (int i : neg) cout << i << " ";
cout << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T; cin.ignore();
for (int i = 1; i <= T; i++) {
//cout << "Case #" << i << ": ";
// clock_t start = clock();
solve();
// clock_t end = clock();
// cout << (end-start) << endl;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int read()
{
int num=0;bool flag=1;
char c=getchar();
for(;c<'0'||c>'9';c=getchar())
if(c=='-')flag=0;
for(;c>='0'&&c<='9';c=getchar())
num=(num<<1)+(num<<3)+c-'0';
return flag?num:-num;
}
int n,m;
int jo[5];
char c[30];
long long ans;
int main(){
n=read();
m=read();
int cnt=0;
for(int i=1;i<=n;i++){
scanf("%s",c+1);
cnt=0;
for(int j=1;j<=m;j++) if(c[j]=='1') cnt++;
if(cnt%2==1){
ans+=jo[2];
jo[1]++;
}
else {
ans+=jo[1];
jo[2]++;
}
}
cout<<ans;
return 0;
} |
# include <stdio.h>
# define max(a, b) (a>b?a:b)
using namespace std;
int n, x;
char s[200200];
int main() {
scanf("%d%d%s", &n, &x, s);
for (int i = 0; i < n; i++) {
if (s[i] == 'x') {
x = max(0, x - 1);
} else {
x++;
}
}
printf("%d", x);
} | #include<bits/stdc++.h>
// #include<iomanip>
// #pragma GCC optimize("Ofast")
#define eb emplace_back
#define pb push_back
#define top 200007
#define min(a,b) (a<b) ? a:b
#define max(a,b) (a>b) ? a:b;
#define F first
#define S second
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(0);
int n, k;
cin>>n>>k;
int sum =0;
for(int i=1;i<=n; ++i)
for(int j=1; j<=k; ++j)
{
sum +=(i*100+j);
}
cout<<sum<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int BASE = 1e9 + 7;
int n;
char aa, ab, ba, bb;
int solve() {
if (n <= 3) return 1;
if (ab == 'A') {
if (aa == 'A') return 1;
if (ba == 'A') {
// number of strings of length n-3 so that no two bs can be adjacent
// A....AB (.... can have any chars)
int lastA = 1, lastB = 0;
for (int i = 1; i <= n-3; ++i) {
int newLastA = (lastA + lastB) % BASE;
int newLastB = lastA;
lastA = newLastA;
lastB = newLastB;
}
return (lastA + lastB) % BASE;
} else {
// number of strings of length n-3
// A....AB (.... can have any chars)
int x = 1;
for (int i = 1; i <= n-3; ++i) x = (x << 1) % BASE;
return x;
}
} else {
if (bb == 'B') return 1;
if (ba == 'B') {
// number of strings of length n-3 so that no two as can be adjacent
// AB....B (.... can have any chars)
int lastA = 0, lastB = 1;
for (int i = 1; i <= n-3; ++i) {
int newLastA = lastB;
int newLastB = (lastA + lastB) % BASE;
lastA = newLastA;
lastB = newLastB;
}
return (lastA + lastB) % BASE;
} else {
// number of strings of length n-3
// AB....B (.... can have any chars)
int x = 1;
for (int i = 1; i <= n-3; ++i) x = (x << 1) % BASE;
return x;
}
}
}
int main() {
cin >> n >> aa >> ab >> ba >> bb;
cout << solve() << endl;
return 0;
}
| #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int n,f[1005][2];
char s[2][2];
bool vis[2][2];
void dfs(int x,int y){
if(vis[x][y]){
return;
}
vis[x][y]=true;
dfs(x,s[x][y]);
dfs(s[x][y],y);
}
int main(){
cin>>n;
if(n==2){
puts("1");
return 0;
}
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
cin>>s[i][j];
s[i][j]-='A';
}
}
dfs(0,1);
if(s[0][1]==1){
f[1][1]=1;
n--;
}else f[1][0]=1;
for(int i=2;i<=n;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
if(vis[j][k]){
f[i][k]=(f[i][k]+f[i-1][j])%1000000007;
}
}
}
}
if(s[0][1]==0){
printf("%d\n",f[n-1][0]);
}else{
printf("%d\n",f[n][1]);
}
return 0;
} |
#include <bits/stdc++.h>
template <class T>
inline void read(T &x) {
static char ch;
static bool opt;
while (!isdigit(ch = getchar()) && ch != '-');
x = (opt = ch == '-') ? 0 : ch - '0';
while (isdigit(ch = getchar()))
x = x * 10 + ch - '0';
if (opt)
x = ~x + 1;
}
template <class T>
inline void putint(T x) {
static char buf[45], *tail = buf;
if (!x)
putchar('0');
else {
if (x < 0)
putchar('-'), x = ~x + 1;
for (; x; x /= 10) *++tail = x % 10 + '0';
for (; tail != buf; --tail) putchar(*tail);
}
}
template <class T>
inline bool tense(T &x, const T &y) {
return y < x ? x = y, true : false;
}
template <class T>
inline bool relax(T &x, const T &y) {
return x < y ? x = y, true : false;
}
template <class T>
inline T getAbs(const T &x) {
return x < 0 ? -x : x;
}
typedef long long s64;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::pair<int, int> pii;
#define mp(x, y) std::make_pair(x, y)
const int MaxN = 3e6 + 5;
int n, a[MaxN];
s64 s[MaxN];
s64 ans;
std::map<s64, int> cnt;
int main() {
#ifdef orzczk
freopen("a.in", "r", stdin);
#endif
read(n), cnt[s[0] = 0] = 1;
for (int i = 1; i <= n; ++i) {
read(a[i]);
s[i] = s[i - 1] + (i & 1 ? -1 : 1) * a[i];
ans += cnt[s[i]]++;
}
std::cout << ans << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define INF (ll)1e18
#define mod 998244353
#define maxn 100010
ll i, i1, j, k, k1, t, n, m, res, flag[10], a[maxn], b[maxn];
ll m1, m2, l[maxn], r[maxn], bsl, bsm, bsu;
bool solve(ll bsm, ll fll) {
ll i, sl, sr, k;
sl = 0; sr = 0;
for (i = 1; i <= n; i++) {
l[i] = max((ll)0, (a[i] * m2 - bsm + m1 - 1) / m1);
r[i] = min(m2, (a[i] * m2 + bsm) / m1);
sl += l[i]; sr += r[i];
}
if (fll == 1) {
k = m2;
for (i = 1; i <= n; i++) {
sl -= l[i]; sr -= r[i];
b[i] = max(l[i], k - sr);
b[i] = min(b[i], r[i]);
k -= b[i];
}
}
/* cout << "bsm = " << bsm << nl;
for (i = 1; i <= n; i++) {
cout << i _ l[i] _ r[i] << nl;
} */
for (i = 1; i <= n; i++) {
if (l[i] > r[i]) return false;
}
if (m2 >= sl && m2 <= sr) return true;
else return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif
cin >> n >> m1 >> m2;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
bsl = 0; bsu = 3 * INF;
while (bsl != bsu) {
bsm = (bsl + bsu) / 2;
if (solve(bsm, 0)) bsu = bsm;
else bsl = bsm + 1;
}
solve(bsl, 1);
for (i = 1; i <= n; i++) cout << b[i] << ' ';
cout << nl;
return 0;
}
|
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (n); i >= 0; i--)
#define FOR(i, m, n) for (ll i = (m); i < (ll)(n); i++)
#define BIT(bit, n) for (int bit = 0; bit < (1 << (n)); ++bit)
#define INF 1000000000
//配列 vector<型> 配列名(要素数, 初期値);
//二次元配列 vector<vector<型>> 配列名(要素数縦,vector<int>(要素数横, 初期値))
//配列のサイズ(二次元縦) 配列名.size(), 二次元横 配列名[0].size()
// 1秒間のforループは10^8くらい
//小数点桁数 cout << fixed << setprecision(20) <<
// bit全探索 条件判定 if(bit & (1 << n))
int main() {
int round;
REP(k, 1000) {
int Si, Sj, Ti, Tj;
cin >> Si >> Sj >> Ti >> Tj;
if (Si > Ti) {
REP(i, Si - Ti) cout << "U";
} else {
REP(i, Ti - Si) cout << "D";
}
if (Sj > Tj) {
REP(i, Sj - Tj) cout << "L";
} else {
REP(i, Tj - Sj) cout << "R";
}
cout << endl;
cin >> round;
}
} | //Soham
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define soham1192k ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#ifndef ONLINE_JUDGE
#define debug(x) cerr<<#x<<" ";_print(x);cerr<<'\n';
#else
#define debug(x)
#endif
const int mod=1000000007;
const double pi = 3.14159265358979323846;
const int mxN=2e5+2;
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
//*********************************************************************************************************************************************************************************************************
int p[200005];
int sz[200005];
int get(int x){
if(p[x]==x) return x;
return p[x]=get(p[x]);
}
void union_sets(int a, int b) {
a = get(a);
b = get(b);
if (a != b) {
if (sz[a]<sz[b])
swap(a, b);
p[b]=a;
sz[a]+=sz[b];
}
}
void solve(){
int n;cin>>n;
vector<int>a(n);
for(int&x:a) cin>>x;
for(int i=1;i<=n;i++){
p[a[i-1]]=a[i-1];
sz[a[i-1]]+=1;
}
int cnt=0;
int i=0,j=n-1;
for(i=0,j=n-1;i<j;i++,j--){
int x=get(a[i]);
int y=get(a[j]);
if(x==y) ;
else{
cnt++;
union_sets(a[i],a[j]);
}
}
cout<<cnt<<'\n';
}
int32_t main(){
soham1192k;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt","w",stderr);
#endif
int T=1;
// cin>>T;
while(T--){
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
using Graph = vector<vector<int> >;
#define INF 1e9
#define LLINF 1e18
#define PI 3.14159265358979323
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
typedef long double ld;
template<class T> void chmin(T& a, T b) {
if (a > b) {
a = b;
}
}
template<class T> void chmax(T& a, T b) {
if (a < b) {
a = b;
}
}
ll gcd(ll a, ll b) {
if(a % b == 0) {
return b;
}
else {
return(gcd(b, a % b));
}
}
ll lcm(ll x,ll y){
return ll(x / gcd(x, y)) * y;
}
int main() {
ll n, ans = 1;
cin >> n;
for (int i = 2; i <= n; i++) {
ans = lcm(ans, i);
}
cout << ans + 1 << endl;
} | #include <iostream>
using namespace std;
using ll = long long int;
int main(){
ll n;
cin >> n;
ll ac = 0;
ll wa = n+1;
while(wa-ac > 1){
ll mid = (ac+wa)/2;
if(mid <= (2LL*(n+1))/(mid+1)) ac = mid;
else wa = mid;
}
cout << n+1-ac << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
const ll Mod=1000000007;
const ll INF=999999999999999999;
const ll NN=(ll)(1e6+5);
ll min(ll x,ll y){if(x<y) return x;return y;}
ll max(ll x,ll y){if(x>y) return x;return y;}
ll power(ll x,unsigned ll y)
{
if (y==0)
return 1;
else if (y%2==0)
return power(x,y/2)*power(x,y/2);
else
return x*power(x,y/2)*power(x,y/2);
}
ll hcf(ll a,ll b)
{
if(b==0)
return a;
return hcf(b,a%b);
}
int main()
{
fio;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif*/
ll TT=1;
//cin>>TT;
while(TT--)
{
ll a,b;
cin>>a>>b;
cout<<(a+b)/2<<" "<<(a-b)/2<<"\n";
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int a,b;
cin>>a>>b;
int an = min(a,b);
int k = max(a,b);
if(an+3>k)
cout<<"Yes";
else
cout<<"No";
return 0;
} |
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define all(v) v.begin(), v.end()
using in = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
const int inf=max(int(1e18)+7,int(1e9)+7);
using P=pair<int,int>;
using T=tuple<int,int,int>;
vector<int> dx={0,1,-1,0};
vector<int> dy={1,0,0,-1};
template <typename Typ>
bool chmin(Typ &a, const Typ& b) {if (a > b) {a = b; return true;} return false;}
template <typename Typ>
bool chmax(Typ &a, const Typ& b) {if (a < b) {a = b; return true;} return false;}
template <typename Typ>
Typ ceil(Typ x, Typ y){return (x+y-1)/y;}
#ifdef _DEBUG
#define debug(x) cout<<"debug:"<<x<<endl
#define vdebug(x) cout<<"debug:";\
for(auto asdf:x) cout<<asdf<<" ";\
cout<<endl
#else
#define debug(x)
#define vdebug(x)
#endif
#define CASE(x) cout<<"Case #"<<x<<": "
//辺でカウント
vector<int> en(0),ex(0),vis(0);
vector<int> ran_f(0),ran_l(0);
vector<int> depth(0);
vector<int> par(0);
vector<vector<int>> tree(0);
int cnt=0;
void tour(int ed){
int n = par.size();
if(en.empty()){
en.resize(n,-1);
ex.resize(n,-1);
ran_f.resize(n,-1);
ran_l.resize(n,-1);
depth.resize(n,-1);
depth[ed] = 0;
}
en[ed] = cnt;
cnt ++;
vis.emplace_back(ed);
ran_f[ed] = vis.size()-1;
ran_l[ed] = vis.size()-1;
for(auto nxt:tree[ed]){
depth[nxt] = depth[ed]+1;
tour(nxt);
vis.emplace_back(ed);
ran_l[ed] = vis.size()-1;
}
ex[ed] = cnt;
cnt ++;
return;
}
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
//cout << fixed << setprecision(12);
int n; cin>>n;
vector<int> p(n,0);
REP(i,1,n){
cin>>p[i];
p[i]--;
}
int q; cin>>q;
vector<int> u(q),d(q);
rep(i,q) cin>>u[i]>>d[i];
rep(i,q) u[i]--;
par = p;
tree = vector<vector<int>>(n,vector<int>(0));
REP(i,1,n) tree[p[i]].emplace_back(i);
tour(0);
map<int,vector<int>> mp;
rep(i,n) mp[depth[i]].emplace_back(en[i]);
for(auto& [ignore,x]:mp) sort(all(x));
rep(i,q){
int x=u[i],y=d[i];
int s=en[x],t=ex[x];
int ans = upper_bound(all(mp[y]),t)-lower_bound(all(mp[y]),s);
cout<<ans<<"\n";
}
// vdebug(en);
// vdebug(ex);
// vdebug(ran_f);
// vdebug(ran_l);
// vdebug(vis);
}/*
./problem.exe
*/ | #include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<int> ps;
vector<vector<int>> children;
DSU(int n): ps(n), children(n) {
iota(ps.begin(), ps.end(), 0);
for (int i = 0; i < n; ++i) {
children[i].push_back(i);
}
}
int find(int i) {
if (ps[i] == i) {
return i;
}
return ps[i] = find(ps[i]);
}
bool merge(int i, int j) {
i = find(i);
j = find(j);
if (i == j) {
return false;
}
if (children[i].size() > children[j].size()) {
swap(i, j);
}
ps[i] = j;
children[j].insert(children[j].end(), children[i].begin(), children[i].end());
return true;
}
};
void solve() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> edges(m);
map<pair<int, int>, int> edges_dir; // -1 = not set, 0 = org, 1 = reverse
for (auto& edge : edges) {
int u, v;
cin >> u >> v;
--u;
--v;
edge = {u, v};
edges_dir[edge] = -1;
}
vector<int> cs(n);
for (int& c : cs) {
cin >> c;
}
vector<vector<int>> adj_list(n);
for (auto& edge : edges) {
int u, v;
tie(u, v) = edge;
if (cs[u] != cs[v]) {
edges_dir[edge] = cs[u] > cs[v] ? 0 : 1;
} else {
adj_list[u].push_back(v);
adj_list[v].push_back(u);
}
}
DSU dsu(n);
for (int u = 0; u < n; ++u) {
set<int> seen;
stack<int> seen_order;
stack<pair<int, int>> edge_order;
set<pair<int, int>> used_edges;
function<bool (int)> dfs = [&](int v) {
v = dsu.find(v);
seen_order.push(v);
if (seen.count(v)) {
return true;
}
seen.insert(v);
for (int w : dsu.children[v]) {
for (int z : adj_list[w]) {
if (dsu.find(z) != v && !used_edges.count({w, z})) {
used_edges.insert({w, z});
used_edges.insert({z, w});
edge_order.push({w, z});
if (dfs(z)) {
return true;
}
edge_order.pop();
}
}
}
seen_order.pop();
return false;
};
if (dfs(u)) {
assert (seen_order.size() == edge_order.size() + 1);
int v = seen_order.top();
seen_order.pop();
vector<int> cycle = {v};
while (true) {
int w = seen_order.top();
seen_order.pop();
if (w == v) {
break;
}
cycle.push_back(w);
}
int cycle_size = (int) cycle.size();
for (int i = 0; i < cycle_size; ++i) {
assert (!edge_order.empty());
int u0, v0;
tie(u0, v0) = edge_order.top();
edge_order.pop();
if (edges_dir.count({u0, v0})) {
assert ((edges_dir[{u0, v0}] == -1));
edges_dir[{u0, v0}] = 0;
} else {
assert (edges_dir.count({v0, u0}));
assert ((edges_dir[{v0, u0}] == -1));
edges_dir[{v0, u0}] = 1;
}
}
for (int w : cycle) {
dsu.merge(w, v);
}
--u;
}
}
for (auto& edge : edges) {
if (edges_dir[edge] == 1) {
cout << "<-\n";
} else {
cout << "->\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int N=200005;
typedef long long LL;
int a[N],b[N],f[N],st[N],n,m,top;
long long sum[N][2];
inline int Find(int x){
return x==f[x]?x:f[x]=Find(f[x]);
}
int main(void){
cin>>n>>m;
for(int i=1;i<=n;++i) f[i]=i;
for(int i=1;i<=n;++i) cin>>a[i];
for(int i=1;i<=n;++i) cin>>b[i];
for(int i=1;i<=m;++i){
int u,v;
cin>>u>>v;
int fu=Find(u);
int fv=Find(v);
if(fu==fv) continue;
else f[fv]=fu;
}
for(int i=1;i<=n;++i) st[++top]=Find(i);
sort(st+1,st+top+1);
top=unique(st+1,st+top+1)-st-1;
for(int i=1;i<=n;++i){
int fa=lower_bound(st+1,st+1+top,Find(i))-st;
sum[fa][0]+=a[i];
sum[fa][1]+=b[i];
}
int flag=1;
for(int i=1;i<=top;++i){
if(sum[i][0]!=sum[i][1]){
flag=0;
break;
}
}
if(flag==1) cout<<"Yes";
else cout<<"No";
return 0;
} | //Never stop trying
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef string str;
typedef long long ll;
#define int ll
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
const int MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const int MX = 2e5 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
#define dbg(x) cerr << " - " << #x << " : " << x << endl;
#define dbgs(x,y) cerr << " - " << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << " - " << #v << " : " << endl << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
void IO() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
vi adj[MX];
int N,M;
vi a(MX),b(MX);
bool vis[MX];
pi p;
void dfs(int u){
vis[u]=true;
p.fi+=a[u]; p.se+=b[u];
for(auto v: adj[u]) if(!vis[v]){
dfs(v);
}
}
int32_t main() {
boost; IO();
cin>>N>>M;
FOR(i,1,N+1) cin>>a[i];
FOR(i,1,N+1) cin>>b[i];
FOR(i,0,M){
int u,v; cin>>u>>v;
adj[u].pb(v); adj[v].pb(u);
}
memset(vis,0,MX);
FOR(i,1,N+1) if(!vis[i]){
p={0,0};
dfs(i);
if(p.fi!=p.se){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}
/* Careful!!!
.Array bounds
.Infinite loops
.Uninitialized variables / empty containers
.Order of input
Some insights:
.Binary search
.Graph representation
.Write brute force code
.Change your approach
*/
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
using namespace std;
inline int topbit(unsigned long long x){
return x?63-__builtin_clzll(x):-1;
}
inline int popcount(unsigned long long x){
return __builtin_popcountll(x);
}
inline int parity(unsigned long long x){//popcount%2
return __builtin_parity(x);
}
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;}
const ll INF=1e9;
const ll 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 { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= 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 { 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;}
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(ll n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(ll n, ll k) {
return fact[n]*ifact[n-k];
}
} c(1000005);
int main(){
ll a,b,c;
cin >> a >> b >> c;
mint A=a*(a+1)/2;
mint B=b*(b+1)/2;
mint C=c*(c+1)/2;
mint ans=A*B*C;
cout << ans << endl;
return 0;
} | // *********************************************************************************
// * MURTAZA MUSTAFA KHUMUSI *
// * NIT-DGP,CSE - 2019-2023 *
// *********************************************************************************
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define si set<int>
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io \
ios_base::sync_with_stdio(0); \
cin.tie(NULL);
const int maxm = 100001;
// ll m = 1e9 + 7;
ll m = 998244353;
vector<vi> adj;
vi h;
vi sz;
vi in;
ll add(ll a, ll b, ll mod = m)
{
return ((a % mod) + (b % mod)) % mod;
}
ll mul(ll a, ll b, ll mod = m)
{
return ((a % mod) * (b % mod)) % mod;
}
ll sub(ll a, ll b, ll mod = m)
{
return ((a % mod) - (b % mod) + mod) % mod;
}
ll modmul(ll a, ll b, ll mod = m)
{
ll ans = 0;
a = a % mod;
while (b)
{
if (b & 1)
ans = add(ans, a, mod);
a = mul(a, 2, mod);
b >>= 1;
}
return ans;
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll fe(ll base, ll exp, ll mod = m)
{
ll ans = 1;
while (exp)
{
if (exp & 1)
ans = mul(ans, base, mod);
base = mul(base, base, mod);
exp >>= 1;
}
return ans;
}
void dfs_h_sz(int cur, int par)
{
h[cur] = h[par] + 1;
sz[cur]++;
for (const auto &v : adj[cur])
{
if (v == par)
continue;
dfs_h_sz(v, cur);
sz[cur] += sz[v];
}
}
void buildGraph(int n, int m)
{
adj = vector<vi>(n + 1);
h = vector<int>(n + 1);
sz = vector<int>(n + 1);
in = vector<int>(n + 1);
loop(i, 0, m)
{
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs_h_sz(1, 0);
}
struct cmp
{
bool operator()(const pi &a, const pi &b)
{
return a.ff < b.ff;
}
};
ll modin(ll a){
return fe(a,m-2);
}
ll sum(ll a){
return mul(mul(a,a+1),modin(2));
}
void solve()
{
ll a,b,c;
cin>>a>>b>>c;
ll ans = sum(a);
ans = mul(ans,sum(b));
ans = mul(ans,sum(c));
cout<<ans<<"\n";
}
int main()
{
io;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int test;
// cin >> test;
test=1;
while (test--)
{
solve();
}
return 0;
}
|
///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll gcd(int a,int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
#define in(x) scanf("%lld",&x)
#define in2(x,y) scanf("%lld %lld",&x,&y)
#define in3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define out(x) printf("%lld\n",x)
#define cy printf("YES\n")
#define cn printf("NO\n")
#define inf 1e18
#define neg -1e18
#define mx 2111111
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define si second
#define ce cout<<endl
#define pb push_back
#define vc(x) x.begin(),x.end()
#define pb push_back
#define pi pair<ll,ll>
#define debug printf("ok\n");
#define pg pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> >
#define mod 1000000007
vector<ll>g[mx];
bool vis[mx];
ll a[mx],b[mx],m,n,s=0,sum=0;
void dfs(ll u){
queue<ll>q;
sum+=a[u];
s+=b[u];
vis[u]=1;
q.push(u);
while(!q.empty()){
ll v;
v=q.front();
q.pop();
for(auto x: g[v]){
if(!vis[x]){
vis[x]=1;
sum+=a[x];
s+=b[x];
q.push(x);
}
}
}
}
int main(){
//fast
ll t;
ll i,j,m,ans=0,n;
in2(n,m);
for(i=1;i<=n;i++)in(a[i]);
for(i=1;i<=n;i++)in(b[i]);
ll u,v;
for(i=0;i<m;i++){
cin>>u>>v;
g[v].pb(u);
g[u].pb(v);
}
for(i=1;i<=n;i++){
if(s!=sum){
cout<<"No"<<endl;return 0;
}
s=0;sum=0;
if(!vis[i]){
dfs(i);
}
}
cout<<"Yes"<<endl;
}
| /*
author: parv2809
*/
#include<bits/stdc++.h>
#define int long long int
#define mod 1000000007
#define inf 1e18
#define fo(i,y,n,inc) for(int i=y;i<n+y;i+=inc)
#define cin(t) int t;cin>>t
#define w(t) while(t--)
#define nl cout<<endl;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define ft(i) (i&(-1*i))
#define arrIn(arr,size) for(int i=0;i<size;i++){cin>>arr[i];}
#define arrOut(arr,size,seperater) for(int i=0;i<size;i++){cout<<arr[i]<<seperater;}
using namespace std;
int max(int a, int b) {
if (a > b)
return a;
return b;
}
int min(int a, int b) {
if (a < b)
return a;
return b;
}
int powmd(int a, int b) {
if (b == 1) return a;
if (b == 0) return 1;
int half = powmd(a, b / 2);
int ans = half * half;
ans %= mod;
if (b & 1) {
ans *= a;
}
ans %= mod;
return ans;
}
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
void dfs(vector<vector<int> >& gr, int src, vector<int>& visited, vector<int>& curr_tree) {
visited[src] = 1;
for (auto it : gr[src]) {
if (!visited[it])
dfs(gr, it, visited, curr_tree);
}
curr_tree.pb(src);
return;
}
int32_t main() {
fastIO();
int t = 1;
w(t) {
int n, m;
cin >> n >> m;
vector<int> a(n), b(n);
arrIn(a, n);
arrIn(b, n);
vector<vector<int> > graph(n);
int c, d;
for (int i = 0; i < m; i++) {
cin >> c >> d;
c--, d--;
graph[c].pb(d);
graph[d].pb(c);
}
vector<int> visited(n, 0);
bool ans = true;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
vector<int> curr_tree;
dfs(graph, i, visited, curr_tree);
int sum = 0;
for (auto it : curr_tree) {
sum += (a[it] - b[it]);
}
if (sum != 0) {
ans = false;
break;
}
}
}
if (ans)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
// cout << "Case #" << i << ": " << answer << endl; |
#include<bits/stdc++.h>
using namespace std;
#define ll int
vector<ll>seg[4*200009];
vector<ll>ad[200009];
ll s[200009];
ll f[200009];
ll T;
ll val[200009];
ll lev[200009];
void dfs(ll a,ll p)
{
lev[a]=lev[p]+1;
T++;
s[a]=T;
val[T]=a;
ll b,i,l=ad[a].size();
for(i=0;i<l;i++)
{
b=ad[a][i];
if(b!=p)
{
dfs(b,a);
}
}
f[a]=T;
}
void merge_(ll pos,ll l,ll r)
{
ll i,j,n,m;
n=seg[l].size();
m=seg[r].size();
i=j=0;
while(1)
{
if(i==n || j==m)
{
break;
}
if(seg[l][i]<=seg[r][j])
{
seg[pos].push_back(seg[l][i]);
i++;
}
else
{
seg[pos].push_back(seg[r][j]);
j++;
}
}
while(i<n)
{
seg[pos].push_back(seg[l][i]);
i++;
}
while(j<m)
{
seg[pos].push_back(seg[r][j]);
j++;
}
}
void create(ll lo,ll hi,ll pos)
{
if(lo==hi)
{
seg[pos].push_back(lev[val[lo]]);
return;
}
ll m=(lo+hi)/2;
create(lo,m,2*pos);
create(m+1,hi,2*pos+1);
merge_(pos,2*pos,2*pos+1);
return;
}
ll query(ll qlo,ll qhi,ll lo,ll hi,ll pos,ll val)
{
if(qlo>hi || qhi<lo)
{
return 0;
}
if(qlo<=lo && qhi>=hi)
{
ll a=lower_bound(seg[pos].begin(),seg[pos].end(),val)-seg[pos].begin();
ll b=upper_bound(seg[pos].begin(),seg[pos].end(),val)-seg[pos].begin();
return (b-a);
}
ll m=(lo+hi)/2;
return query(qlo,qhi,lo,m,2*pos,val)+query(qlo,qhi,m+1,hi,2*pos+1,val);
}
int main()
{
ll a,b,c,d,e,i,j,k,l,n,m,x,y,t,p;
scanf("%d",&n);
for(i=2;i<=n;i++)
{
scanf("%d",&a);
ad[a].push_back(i);
}
T=0;
lev[0]=-1;
dfs(1,0);
create(1,n,1);
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&c);
x=query(s[a],f[a],1,n,1,c);
printf("%d\n",x);
}
}
| #include "bits/stdc++.h"
using namespace std;
#define FastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define mod 1000000007
const int N = 2e5 + 5;
vector<int> tree[N], depth(N);
vector<int> time_in(N), time_out(N);
vector<int> orders[N];
int timer = 0, n;
void dfs(int s) {
time_in[s] = timer++;
orders[depth[s]].push_back(time_in[s]);
for (int v : tree[s]) {
depth[v] = 1 + depth[s];
dfs(v);
}
time_out[s] = timer++;
}
void test_case() {
cin >> n;
int j;
for (int i = 2; i <= n; i++) {
cin >> j;
tree[j].push_back(i);
}
dfs(1);
int q;
cin >> q;
while (q--) {
int u, d;
cin >> u >> d;
auto &arr = orders[d];
auto y = lower_bound(arr.begin(), arr.end(), time_out[u]);
auto x = lower_bound(arr.begin(), arr.end(), time_in[u]);
cout << y - x << "\n";
}
}
signed main() {
FastIO;
test_case();
return 0;
}
/*
▄ ▄
▌▒█ ▄▀▒▌
▌▒▒█ ▄▀▒▒▒▐
▐▄█▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
▄▄▀▒▒▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
▐▒▒▒▄▄▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀▄▒▌
▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌
▌░▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌
▌▒▒▒▄██▄▒▒▒▒▒▒▒▒░░░░░░░░▒▒▒▐
▐▒▒▐▄█▄█▌▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▐▀▐▀▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▐
▌▒▒▀▄▄▄▄▄▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▒▌
▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒▒▄▒▒▐
▀▄▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒▄▒▒▒▒▌
▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀
▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀
▀▀▀▀▀▀▀▀▀▀▀▀
*/ |
#include<bits/stdc++.h>
using namespace std;
#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 range(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define F first
#define S second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
using namespace std;
int main(){
string s;
cin >> s;
string ans = "";
for(long long i=0;i<s.length();i++) {
if(s[i] == '.') {
break;
} else {
ans += s[i];
}
}
cout << ans;
return 0;
} | #pragma region Macros
#include <bits/stdc++.h>
#if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG)
#include <atcoder/all>
#endif
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
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; }
template<class T>using vec = vector<T>;
template<class T, class U>using umap = unordered_map<T, U>;
using ll = long long;
using P = pair<ll, ll>;
using vl = vec<ll>;
#define fi first
#define se second
#define el endl
constexpr ll INF = numeric_limits<ll>::max()/2-1;
#pragma endregion
#pragma region IOMacros
template<class T>
istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T>
ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;}
#define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__);
void __i() {}
template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);}
void O() {cout << el;}
template<class T, class... Ts> void O(T&& o, Ts&&... args){cout << o << " ";O(forward<Ts>(args)...);}
#pragma endregion
void Main();
int main(){
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
}
void Main(){
I(ll, X);
ll t = (X+100)/100;
cout << 100*t-X << el;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define PI acos(-1)
#define pb push_back
#define int long long
#define ld long double
#define sp fixed<<setprecision
#define bp __builtin_popcountll
#define all(x) x.begin(),x.end()
#define pii pair<long long,long long>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int M = (1e9)+7;
const int N = 505;
const int INF = 1e12;
int n,m;
int x[105], y[105], z[105];
int memo[(1<<19)][20];
int solve(int mask, int pos)
{
// cout<<mask<<" "<<pos<<endl;
if(pos == n)return 1;
if(memo[mask][pos] != -1) return memo[mask][pos];
int ans = 0;
for(int i=0;i<n;i++)
{
if(!((mask>>i)&1LL))
{
int ok = 1;
int new_mask = mask|(1LL<<i);
for(int j=0;j<m;j++)
{
if(x[j]>=pos)
{
int cnt = 0;
for(int k=0;k<=y[j];k++) cnt += ((new_mask>>k)&1LL);
ok &= (cnt <= z[j]);
}
}
// cout<<new_mask<<endl;
if(ok) ans += solve(new_mask, pos+1);
}
}
// cout<<mask<<" "<<pos<<" "<<ans<<endl;
memo[mask][pos] = ans;
return ans;
}
signed main()
{
FAST
int tc=1;
// cin>>tc;
for(int ti=1;ti<=tc;ti++)
{
cin>>n>>m;
for(int i=0;i<m;i++) cin>>x[i]>>y[i]>>z[i], x[i]--,y[i]--;
for(int i=0;i<(1LL<<n);i++)for(int j=0;j<n;j++)memo[i][j] = -1;
int ans = solve(0, 0);
cout<<ans<<endl;
}
return 0;
} | #include<cstdio>
#define F(i,l,r) for(int i=l,i##_end=r;i<i##_end;++i)
using namespace std;
const int N=20,M=105;
template<typename T>void read(T &x)
{
bool neg=false;
unsigned char c=getchar();
for(;(c^48)>9;c=getchar())if(c=='-')neg=true;
for(x=0;(c^48)<10;c=getchar())x=(x<<3)+(x<<1)+(c^48);
if(neg)x=-x;
}
int n,m,x[M],y[M],z[M],pop[1<<N],bit[1<<N][N];
long long f[1<<N];
bool check(int i,int j)
{
F(k,0,m)if(i==x[k]&&bit[j][y[k]]>z[k])return false;
return true;
}
int main()
{
read(n);read(m);
F(i,0,m)read(x[i]),read(y[i]),read(z[i]);
F(i,1,1<<n)pop[i]=pop[i^(i&-i)]+1;
F(i,0,1<<n)F(j,0,n)bit[i][j+1]=bit[i][j]+(i>>j&1);
f[0]=1;
F(i,0,n)F(j,0,1<<n)if(pop[j]==i)
{
if(i==0||check(i,j))
F(k,0,n)if((j>>k&1)==0)
{
int v=j^1<<k;
f[v]+=f[j];
}
}
printf("%lld\n",f[(1<<n)-1]);
return 0;
}
|
#pragma GCC target ("sse4")
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define ld long double
#define bit(idx) idx&(-idx)
#define bin(x) bitset<32>(x).to_string()
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;
#define row vector<ll>
#define row_matrix vector<ll>
#define matrix vector<row_matrix>
#define ordered_set_T ll
using namespace std;
using namespace __gnu_pbds;
typedef tree<ordered_set_T, null_type, less<ordered_set_T>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
/// find_by_order(x) -> x-th element in the set
/// order_of_key(x) -> how many elements are smaller than x
/// insert(x) -> inserts x into the set
int main(){
/// ios_base::sync_with_stdio(false); cin.tie(NULL);
int n; cin >> n;
row A(n);
for(int i = 0; i < n; i ++) cin >> A[i];
for(int i = 1; i < n; i ++){
if(i & 1) A[i] = A[i - 1] - A[i];
else A[i] = A[i - 1] + A[i];
}
unordered_map<ll, ll> mp;
mp[0] = 1;
ll ans = 0;
for(auto x : A) ans += mp[x] ++;
cout << ans << endl;
}
/**
*/ | #pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
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
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
if ((tmp & ma0) ^ ma0) {
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += (72 - dig >> 3);
}
else {
tmp = tmp & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 8;
if ((ct = *ci++) >= '0') {
tmp = tmp * 10 + ct - '0';
if (*ci++ == '0') {
tmp = tmp * 10;
ci++;
}
}
}
return tmp;
}
ll num[524310];
const ll dekai = 1ll << 60;
int kazu[524310];
const int ma = (1 << 19) - 1;
int query(ll n) {
int n2 = (n & ma) | 1;
while (num[n2]) {
if (num[n2] == (n | dekai)) return kazu[n2];
n2 = n2 * 2 % 524309;
}
return 0;
}
void add(ll n) {
int n2 = (n & ma) | 1;
while (num[n2]) {
if (num[n2] == (n | dekai)) {
kazu[n2]++;
return;
}
n2 = n2 * 2 % 524309;
}
num[n2] = n | dekai;
kazu[n2] = 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N = getint();
add(0);
ll kotae = 0;
ll ima = 0;
int pn = 1;
rep(i, N) {
ima += pn * getint();
kotae += query(ima);
add(ima);
pn = -pn;
}
co(kotae);
Would you please return 0;
} |
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#define rep(i,n) for(ll i=0;i<(n);++i)
#define rep2(i,n) for(ll i=1;i<=(n);++i)
#define rep3(i,i0,n) for(ll i=i0;i<(n);++i)
#define rrep(i,n) for(ll i=((n)-1); i>=0; --i)
#define rrep2(i,n) for(ll i=(n); i>0; --i)
#define pb push_back
#define mod 1000000007
#define fi first
#define se second
#define len(x) ((ll)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
using Pi = pair< ll, ll >;
using vl = vector<ll>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vp = vector<Pi>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
const ll INF = 1LL << 60;
const ld PI = 3.1415926535897932385;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
void printb(ll N,ll d=16){
rep(i,d){
cout<<(N/(1<<(d-i-1)))%2;
}
cout<<endl;
}
void printv(vector<ll>a){
rep(i,a.size()){
if(i==a.size()-1){
cout<<a[i]<<endl;
}else{
cout<<a[i]<<" ";
}
}
}
bool In_map(ll y,ll x,ll h,ll w){
if(y<0 || x<0 || y>=h || x>=w){
return 0;
}else{
return 1;
}
}
bool compare(Pi a, Pi b) {
if(a.first != b.first){
return a.first < b.first;
}else{
return a.second < b.second;
}
}
//const vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1};
//const vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1};
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};
vvl A;
ll N;
// index が条件を満たすかどうか
bool isOK(ll idx, ll key) {
vvl B(N,vl(5));
rep(i,N){
rep(j,5){
if(A[i][j]>=idx){
B[i][j]=1;
}else{
B[i][j]=0;
}
}
}
vl C(N);
vector<ll>m(1000);
rep(i,N){
rep(j,5){
C[i]+=(B[i][j]<<(5-j-1));
}
m[C[i]]=1;
}
rep(i,32){
rep(j,32){
rep(k,32){
if(m[i]==1 && m[j]==1 &&m[k]==1){
if((i|j|k)==31){
return 1;
}
}
}
}
}
return 0;
}
// 汎用的な二分探索のテンプレ
ll binary_search(ll key) {
ll ng = INF; //「index = 0」が条件を満たすこともあるので、初期値は -1
ll ok = 0; // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()
/* ok と ng のどちらが大きいかわからないことを考慮 */
while (abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if (isOK(mid, key)) ok = mid;
else ng = mid;
}
return ok;
}
int main() {
cin>>N;
A=vvl(N,vl(5));
rep(i,N){
rep(j,5){
cin>>A[i][j];
}
}
ll ans=binary_search(0);
cout<<ans<<endl;
return 0;
} | #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std;
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
int main()
{
int N;
cin >> N;
int A[N], B[N];
rep(i,0,N){
cin >> A[i] >> B[i];
}
int min_time = inf;
rep(i,0,N){
rep(j,0,N){
if(i == j){
int time = A[i] + B[i];
min_time = min(time,min_time);
}else{
int time = max(A[i], B[j]);
min_time = min(time,min_time);
}
}
}
cout << min_time << endl;
} |
//Author- Vaibhav Singh
#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_multiset tree<long long , null_type,less_equal<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_set tree<long long , null_type,less<long long >, rb_tree_tag,tree_order_statistics_node_update>
*/
using namespace std;
#define ll long long int
#define YES cout<<"YES"<<"\n";
#define NO cout<<"NO"<<"\n";
#define ld long double
#define yes cout<<"yes"<<"\n";
#define no cout<<"no"<<"\n";
#define No cout<<"No"<<"\n";
#define Yes cout<<"Yes"<<"\n";
#define f(i,a) for(i=0;i<a;i++)
#define fo(i,a) for(i=1;i<=a;i++)
#define fa(i,a) for(auto i:a)
#define r(i,a) for(auto i=a.rbegin();i!=a.rend();i++)
#define en cout<<"\n";
#define ull unsigned long long int
#define o(x) cout<<x<<"\n";
#define o1(x) cout<<x<<" ";
#define pb push_back
#define F first
#define in insert
#define mp make_pair
#define S second
#define pre(n) cout<<fixed<<setprecision(n);
#define gcd(a,b) __gcd(a,b)
#define bs binary_search
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;cin>>t;while(t-->0)
const ll Mod = 998244353;
#define mod 1000000007
#define pi 3.14159265358979323846
#define all(x) x.begin(),x.end()
#define re return 0;
#define vl vector<ll>
#define vc vector<char>
#define ul unordered_map<ll,ll>
#define uc unordered_map<char,ll>
#define vp vector<pair<ll,ll>>
// *#######################################################################################*
/*
bool isp(ll n)
{
ll i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
{
return false;
}
}
}
*/
ll powerm(ll x, unsigned ll y, ll p)
{
ll res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
long long lcm(long long a, long long b){//?????????
return a * b / gcd(a, b);
}
/*
ll po(ll z,ll z1)
{
ll rer=0;
rer= ((z*(z-1))/2+(z1*(z1-1))/2);
return rer;
}
*/
/*
bool sortbysec(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.second < b.second);
}
*/
/*
bool is_prime(ll n){
for(ll i = 2;i*i<=n;i++){
if(n%i == 0) return false;
}
return true;
}
*/
/*
int inv(int a){
return powerm(a,mod-2,mod);
}
ll const N=1e5;
ll fact[N], invfact[N];
void precompute(){
fact[0] = 1;
for(int i=1;i<N;i++) fact[i] = (fact[i-1]*i)%mod;
invfact[N-1] = inv(fact[N-1])%mod;
for(int i=N-2;i>=0;i--) invfact[i] = (invfact[i+1]*(i+1))%mod;
}
ll ncr(ll n, ll r){
if(n<r) return 0;
return (((fact[n]*invfact[r])%mod)*invfact[n-r])%mod;
}
*/
//****************************************************************************
//****************************************************************************
int main()
{
fast
ll n,x;
cin>>n>>x;
string s;
cin>>s;
ll i;
f(i,n)
{
if(s[i]=='o')
{
x++;
}
else
{
if(x>0)
{
x--;
}
}
}
o(x)
//?????????
re} | #include <bits/stdc++.h>
using namespace std;
#define int long long int
mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count());
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N=200005;
#define M 1000000007
#define double long double
#define BINF 100000000000000
#define init(arr,val) memset(arr,val,sizeof(arr))
#define MAXN 17500001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;
#undef int
int main() {
#define int long long int
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, x;
cin >> n >> x;
string s;
cin >> s;
for(auto i : s){
if(i == 'o') x++;
else{
x--;
x = max(0LL, x);
}
}
cout << x << endl;
return 0;
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 998244353
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define vpii vector<pair<int,int> >
#define all(x) x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y) for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x) (int)x.size()
#define db(x) cout<<x<<"\n";
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//insert,find_by_order,order_of_key,lower_bound,upper_bound;
#define TRACE
#ifdef TRACE
#define deb(...) __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 deb(...)
#endif
//////////////////////////////code//////////////////////////////
const int N = 74;
int a,b;
int n;
vector<int> primes;
int dp[N][(1<<20)];
void solve()
{
cin >> a >> b;
for(int i = 2; i <= 72 ;i++)
{
int ok = 1;
for(int j = 2; j < i ;j++)
{
if(i % j == 0)
ok = 0;
}
if(ok)
primes.pb(i);
}
n = sz(primes);
memset(dp,0,sizeof(dp));
dp[0][0] = 1;
for(int i = 1;i <= (b - a + 1) ;i++)
{
int mask1 = 0,bit = 1;
for(int j = 0;j < n ; j++)
{
if((a + i - 1)%primes[j] == 0)
mask1 |= bit;
bit<<=1;
}
for(int mask = 0;mask < (1 << n); mask++)
{
if( ((mask)&(mask1)) == 0)
{
dp[i][(mask | mask1)] += dp[i-1][mask];
}
dp[i][mask] += dp[i - 1][mask];
}
}
int ans = 0;
for(int mask = 0; mask < (1 << n); mask++)
{
ans += dp[(b-a+1)][mask];
}
db(ans)
}
int32_t main()
{
LETS_GET_SCHWIFTY;
#ifndef ONLINE_JUDGE
freopen("INP.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--)
solve();
}
// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values
| #include <iostream>
#include <numeric>
#include <vector>
using namespace std;
using Mask = int;
bool IsPrime(int n) {
if (n < 2) {
return false;
}
for (int64_t i = 2; i * i <= n; i += 1) {
if (n % i == 0) {
return false;
}
}
return true;
}
vector<int> Primes(int limit) {
vector<int> primes;
for (int i = 2; i <= limit; i += 1) {
if (IsPrime(i)) {
primes.push_back(i);
}
}
return primes;
}
Mask GetMask(int64_t num) {
Mask mask = 0;
static const auto kPrimes = Primes(72);
for (size_t i = 0; i < kPrimes.size(); i += 1) {
if (num % kPrimes[i] == 0) {
mask |= (1 << i);
}
}
return mask;
}
int64_t Solve(int64_t l, int64_t r) {
vector<int64_t> sets(1 << 20, 0);
sets[0] = 1;
vector<Mask> masks;
for (auto i = l; i <= r; i += 1) {
masks.push_back(GetMask(i));
}
for (const auto& mask : masks) {
for (Mask primes = mask; primes < (1 << 20); primes += 1) {
if ((primes & mask) == mask) {
sets[primes] += sets[primes & (~mask)];
}
}
}
return accumulate(sets.begin(), sets.end(), 0);
}
int main() {
int64_t l, r;
cin >> l >> r;
auto res = Solve(l, r);
cout << res << "\n";
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(v) 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() {
IN(int, X);
IN(int, Y);
int mi = min(X, Y);
int ma = max(X, Y);
if (mi + 3 > ma) yes();
else no();
return 0;
}
| // Template
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <tuple>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <array>
#include <cassert>
#include <cmath>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll infll = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (T &element : vec) is >> element;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) {
os << vec[i] << (i + 1 == vec_len ? "" : " ");
}
return os;
}
struct IOSET {
IOSET() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} ioset;
// Main
int main() {
int t;
int n;
cin >> t >> n;
--n;
vector<int> vec(100 + t, 0);
rep(i, 100) vec[i * (100 + t) / 100] = 1;
int cnt = 0;
rep(i, 100 + t) if (vec[i] == 0) ++cnt;
ll ans = (100LL + t) * (n / cnt);
n %= cnt;
rep(i, 100 + t) {
if (vec[i] == 0) {
if (n == 0) {
cout << ans + i << '\n';
return 0;
}
--n;
}
}
}
|
//Bismillahir Rahmanir Rahim
//Allahumma Rabbi Jhidni Elma
/*--------Please carefully check--------
1.Overflow and underflow
2.Corner case
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ld PI = 2*acosl(0.0);
const int inf=1e5+7;
const int mxN=3000000;
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define pb push_back
#define reset(a) memset(a,0,sizeof a)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (a/gcd(a,b)*b)
#define abs(a) (a<0?-(a):a)
#define debug1(x) cout << #x << "=" << x << endl
#define debug2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define digit2(x) floor((log2(x)))
#define digit2(x) floor((log2(x)))
#define sc(a) scanf("%d",&a)
#define pf(a) printf("%d\n",a)
#define PII pair<int,int>
#define mk make_pair
int mark[inf];
struct node
{
int a,b;
};
vector<PII>tree[inf];
bool cmp(node x,node y)
{
return x.b<y.b;
}
int prims(int src)
{
priority_queue<PII ,vector<PII>,greater<PII>>pq;
pq.push(make_pair(0,src));
PII x;
while(!pq.empty())
{
x=pq.top();
int sc=x.second;
// cout<<x.first<<" "<<x.second<<endl;
pq.pop();
if(mark[sc])
{
continue;
}
mark[sc]=1;
// cout<<x.second<<" "<<x.first<<endl;
for(int i=0;i<tree[sc].size();i++)
{
int value=tree[sc][i].second;
//
if(mark[value]==0)
{
// mark[value]=1;
// cout<<sc<<" "<<value<<" "<<tree[sc][i].first<<endl;
pq.push(tree[sc][i]);
}
}
}
return 0;
}
int write[inf],par[inf],n;
void dfs(int src,int level)
{
// cout<<level<<" ";
// cout<<par[src]<<' '<<src<<" "<<write[par[src]]<<" "<<level<<endl;
if(write[par[src]]==level)
{
write[src]=(level%n)+1;
}
else
{
write[src]=level;
}
//ar[src]=level;
mark[src]=1;
for(auto x:tree[src])
{
//cout<<src<<"-->"<<x.second<<"---"<<x.first<<endl;
if(mark[x.second]==0)
{
par[x.second]=src;
dfs(x.second,x.first);
}
}
// cout<<src<<" ";
}
int main()
{
int m,u,v,c,i,temp,j;
sc(n),sc(m);
node nd;
for(i=0;i<m;i++)
{
sc(u),sc(v),sc(c);
//nd.a=v;
// nd.b=c;
tree[u].pb(mk(c,v));
//nd.a=u;
tree[v].pb(mk(c,u));
}/*
for(i=1;i<=n;i++)
{
for(j=0;j<tree[i].size();j++)
{
cout<<i<<"-->"<<tree[i][j].a<<"-->"<<tree[i][j].b<<endl;
}
cout<<endl;
}*/
dfs(1,1);
for(i=1;i<=n;i++)
{
cout<<write[i]<<endl;
}
cout<<endl;
//sort(tree.begin(),tree.end(),cmp());
//prims(1);
}
/*
6 7
1 2 6
2 5 3
5 6 4
5 3 5
3 4 1
1 3 2
1 5 2
*/
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define INF 0x3f3f3f3f
#define mem(a, b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)
// const ll mod = 998244353;
// const ll mod = 1e9 + 7;
// const double eps = 1e-6;
// const double PI = acos(-1);
// const double R = 0.57721566490153286060651209;
#define MAXN 205000//点数
#define MAXM 404000//道路数
#define search(i,y) for(int i=head[y];i;i=edge[i].next) //遍历一个点的出边
#define add(u,v,c) {edge[++cnt]={u,v,c,head[u]};head[u]=cnt;}//加入一条边
int head[MAXN],cnt=1;//0说明没有下一个值
struct EDGE{
int u,v,c;
int next;//下一个值的位置;
}edge[MAXM];
int inf = MAXN +1;
int color[MAXN];
bool used[MAXN];
void dfs(int x){
if(used[x])
return;
used[x]=true;
search(i,x){
int y=edge[i].v;
if(used[y])continue;
if(color[x]==edge[i].c){
color[y]=edge[i].c-1;
if(color[y]==0)
color[y]=edge[i].c+1;
dfs(y);
}else{
color[y]=edge[i].c;
dfs(y);
}
}
return ;
}
void solve() {
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
color[1]=1;
dfs(1);
for(int i=1;i<=n;i++){
printf("%d\n",color[i]);
}
}
signed main() {
ios_base::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}
|
#include<bits/stdc++.h>
#define int ll
#define sz(x) int((x).size())
#define all(x) (x).begin(),(x).end()
#define x first
#define y second
using namespace std;
using ll = long long;
using pi = pair<int,int>;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int minf = 0xc0c0c0c0c0c0c0c0;
int comb[65][65];
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
comb[0][0] = 1;
for (int i=1; i<=60; i++) {
comb[i][0] = 1;
for (int j=1; j<=i; j++) {
comb[i][j] = comb[i-1][j] + comb[i-1][j-1];
}
}
int a,b,k; cin>>a>>b>>k;
string res;
while (a || b) {
if (a == 0) {
res.push_back('b');
--b;
}
else if (b == 0) {
res.push_back('a');
--a;
}
else {
if (comb[a+b-1][b] < k) {
res.push_back('b');
k -= comb[a+b-1][b];
--b;
}
else {
res.push_back('a');
--a;
}
}
}
cout<<res<<'\n';
return 0;
} | #include <bits/stdc++.h>
#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc int t; cin >> t; for(int tt = 0; tt < t; ++tt)
#define sz(x) (int)(x).size()
#define sort_unique(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()));
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;
template<typename T> istream& operator>>(istream &is, vector<T> &v) { for(auto &it : v) is >> it; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep = ""; for(const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void dbg_out() { cerr << " end.\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
/**
Read problem statement carefully
**/
LL dp[35][35]; // numbers of ways.
string find_kth(int a, int b, LL k){
if(a == 0)
return string(b, 'b');
if(b == 0)
return string(a, 'a');
if(k <= dp[a-1][b]) // put a
return string("a") + find_kth(a-1, b, k);
else
return string("b") + find_kth(a, b-1, k - dp[a-1][b]);
}
void solve(int n){
int a, b;
LL k;
cin >> a >> b >> k;
dp[0][0] = 1;
for(int i = 0; i <= a; ++i){
for(int j = 0; j <= b; ++j){
if(i) dp[i][j] += dp[i-1][j];
if(j) dp[i][j] += dp[i][j-1];
}
}
cout << find_kth(a, b, k) << "\n";
}
int main()
{
Fast;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// louisfghbvc
solve(1);
return 0;
}
/**
Enjoy the problem.
**/
|
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rrep(i,n) for(int i=((n)-1);i>=0;--i)
#define all(x) (x).begin(), (x).end()
#define PI 3.14159265358979323846264338327950L
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
int n,m;
cin>>n>>m;
vector<bool> a(1010),b(1010);
int c;
rep(i,n){
cin>>c;
a[c]=true;
}
rep(i,m){
cin>>c;
b[c]=true;
}
rep(i,1010){
if((a[i]&&!b[i])||(!a[i]&&b[i])) cout<<i<<" ";
}
} | // Murabito-B 21/04/17
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
int n, m;
cin >> n >> m;
vector<int> a(n);
map<int, int> mp;
for (int i = 0; i < n; ++i) {
cin >> a[i];
mp[a[i]]++;
}
vector<int> ans;
for (int i = 0, x; i < m; ++i) {
cin >> x;
if (mp[x] == 0) ans.push_back(x);
mp[x]++;
}
for (int i = 0; i < n; ++i) {
if (mp[a[i]] == 1) ans.push_back(a[i]);
}
sort(ans.begin(), ans.end());
for (int x : ans) cout << x << " ";
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai{
#define ms(a) memset(a,0,sizeof(a))
#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define ce(i,r) i==r?'\n':' '
#define pb push_back
#define all(x) x.begin(),x.end()
#define gmn(a,b) a=min(a,b)
#define gmx(a,b) a=max(a,b)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int infi=1e9;//infi较大,注意涉及inf相加时爆int
const ll infl=4e18;
inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
inline int qi(){
int f=0,fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline ll ql(){
ll f=0;int fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline db qd(){
char c=getchar();int flag=1;double ans=0;
while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
if(c=='-') flag=-1,c=getchar();
while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
return ans*flag;
}
}
namespace Read{
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define rai(x,a,b) repi(i,a,b) x[i]=qi()
#define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
#define pi(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)
#define pc(x) printf("%c",x)
#define pe() puts("")
}
namespace DeBug{
#define MARK false
#define DB if(MARK)
#define pr(x) cout<<#x<<": "<<x<<endl
#define pra(x,a,b) cout<<#x<<": "<<endl; \
repi(i,a,b) cout<<x[i]<<" "; \
puts("");
#define FR(a) freopen(a,"r",stdin)
#define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
const int MAX_N=2e5+5;
int n;
char s[MAX_N],x[MAX_N];
bool dp[MAX_N][7];
int main()
{
n=qi(),ss(s+1),ss(x+1);
dp[n][0]=true;
repd(i,n,1){
if(x[i]=='T'){
repi(j,0,6)if(dp[i][(10*j)%7]||dp[i][(10*j+s[i]-'0')%7]) dp[i-1][j]=true;
}
else{
repi(j,0,6)if(dp[i][(10*j)%7]&&dp[i][(10*j+s[i]-'0')%7]) dp[i-1][j]=true;
}
}
puts(dp[0][0]?"Takahashi":"Aoki");
return 0;
} | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/rope>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
// typedef tree<long long, null_type, std::less<long long>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// typedef trie<std::string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>Trie;
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define rdv(x) long long x; cin>>x;
#define deb(x) cout<<#x<<"="<<x<<endl;
#define inf 1e18
#define endl "\n"
typedef vector<long long> vll;
typedef long long ll;
typedef pair<long long,long long> pii;
typedef long double ld;
mt19937 mrand(random_device{}());
const ll mod=100000000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
bool prime[200001];
void SieveOfEratosthenes()
{
memset(prime, true, sizeof(prime));
prime[1]=false;
for (int p=2; p*p<=200000; p++)
{
if (prime[p] == true)
{
for (int i=p*p; i<=200000; i += p)
prime[i] = false;
}
}
return;
}
ll ceil(ll x,ll y)
{
ll ans=x/y;
if(x%y)
ans++;
return ans;
}
void file_i_o(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
void YN(bool flag)
{
if(flag)
cout<<"YES";
else
cout<<"NO";
}
#define pi 3.1415926535
// head
void solve()
{
ll n;
cin>>n;
map<ll,vll>mp;
rep(i,0,n)
{
ll x,c;
cin>>x>>c;
mp[c].pb(x);
}
ll l=0,r=0,left=0,right=0;
for(auto it:mp)
{
ll nl=inf,nr=-inf;
for(auto i:it.se)
{
nl=min(nl,i);
nr=max(nr,i);
}
ll ldi=inf,rdi=inf;
//L->L2->R2
rdi=min(rdi,abs(l-nl)+abs(nr-nl)+left);
ldi=min(ldi,abs(l-nr)+abs(nr-nl)+left);
ldi=min(ldi,abs(r-nr)+abs(nr-nl)+right);
rdi=min(rdi,abs(r-nl)+abs(nr-nl)+right);
l=nl;
r=nr;
left=ldi;
right=rdi;
}
cout<<min(left+abs(l),right+abs(r));
cout<<"\n";
}
int main()
{
ios_base::sync_with_stdio(false); //Used to reduce the time
cin.tie(NULL);
cout.tie(NULL);
file_i_o();
ll T=1;
//cin>>T;
//ll temp=T;
while(T--)
{
//cout<<"Case #"<<temp-T<<": ";
solve();
}
cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<" ms\n";
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
int N, M;
cin >> N >> M;
vector<int> A(M);
vector<int> B(M);
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i];
}
int K;
cin >> K;
vector<int> C(K + 1);
vector<int> D(K + 1);
for (int i = 0; i < K; i++) {
cin >> C[i] >> D[i];
}
int ans = -1;
for (int i = 0; i < (1 << K); i++) {
vector<int> save(N + 1);
for (int j = 0; j < K; j++) {
if (i & (1 << j)) save.at(C.at(j))++;
else save.at(D.at(j))++;
}
int cnt = 0;
for (int j = 0; j < M; j++) {
if (save.at(A.at(j)) > 0 && save.at(B.at(j)) > 0) cnt++;
}
ans = max(ans, cnt);
}
cout << ans << endl;
} | #include<bits/stdc++.h>
#define ll long long
#define pi 3.1415926535897932384626
using namespace std;
struct P{
int x,y;
bool operator < (const P &a )const{
//if(x!=a.x) return x<a.x;
//if(y!=a.y)
return x>a.x;
};
};
int a,b,c,d,i,n,m,e,k,dx[10]={1,0,-1,0,1,1,-1,-1},dy[10]={0,1,0,-1,1,-1,1,-1};
int o[211111];
int l[201011],ck[201010];
ll dp[2];
int ss[201011];
int ans[1];
long long x,y,z;
const long long mod=1000000007,hf=(mod+1)/2;
//string r;
char r;
vector<int> v[201011];
P u[201011];
queue<P> q;
//multiset<int> s;
stack<int> s;
unordered_map<int,int> p[1];
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//rng()
bool as(P a,P b)
{
//if(a.x!=b.x)
//return a.x<b.x;
//if(a.y!=b.y) return a.y>b.y;
return min(a.x,a.y)<min(b.x,b.y);
}
int main()
{
scanf("%d %d",&a,&b);
for(int t=1;t<a;t++)
{
scanf("%d %d",&n,&m);
v[n].push_back(m);
v[m].push_back(n);
}
n=1,m=a;
for(;n<m;)
{
int k=(n+m)/2;
e=0;
queue<int> q;
memset(ss,0,sizeof(ss));
memset(l,-1,sizeof(l));
memset(o,0,sizeof(o));
memset(ck,0,sizeof(ck));
for(int t=1;t<=a;t++)
if(v[t].size()==1)
{
l[t]=-1;
//ck[t]=1;
q.push(t);
}
int r=0;
for(;q.size();q.pop())
{
int p=q.front();
//printf("#%d\n",p);
ck[p]=1;
r++;
if(-l[p]>o[p])
{
if(-l[p]>k)
{
//printf("**%d\n",p);
e++;
for(int h=0;h<v[p].size();h++)
if(ck[v[p][h]]==0)
{
ss[v[p][h]]++;
o[v[p][h]]=max(o[v[p][h]],k);
if(ss[v[p][h]]+1==v[v[p][h]].size())
{
//ck[v[p][h]]=1;
q.push(v[p][h]);
}
}
}
else if(r==a) e++;
else
{
for(int h=0;h<v[p].size();h++)
if(ck[v[p][h]]==0)
{
//printf("^^%d\n",v[p][h]);
ss[v[p][h]]++;
l[v[p][h]]=min(l[v[p][h]],l[p]-1);
if(ss[v[p][h]]+1==v[v[p][h]].size())
{
//ck[v[p][h]]=1;
q.push(v[p][h]);
}
}
}
}
else
{
for(int h=0;h<v[p].size();h++)
if(ck[v[p][h]]==0)
{
ss[v[p][h]]++;
if(o[p]>0) o[v[p][h]]=max(o[v[p][h]],o[p]-1);
//else l[v[p][h]]=min(l[v[p][h]],-1);
if(ss[v[p][h]]+1==v[v[p][h]].size())
{
//ck[v[p][h]]=1;
q.push(v[p][h]);
}
}
}
}
if(e<=b) m=k;
else n=k+1;
}
//printf("%d %d %d\n",n,m,e);
//for(int t=1;t<=a;t++)
//printf("#%d %d\n",l[t],o[t]);
printf("%d",n);
}
/*
7 2
1 2
2 3
3 4
4 5
5 6
6 7
*/
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mp make_pair
#define fr first
#define sc second
template<int MOD=1000000007>
struct modint{
/*static modint inv[2005];
static void init(){
for(int i=0;i<2005;i++)inv[i]=modpow(modint(i),MOD-2);
}*/
ull val;
modint(ull x){ val=x%MOD; }
modint(){}
friend modint modpow(modint x,ull k){
modint ret(1ULL);
while(k>0){
if(k&1ULL)ret*=x;
x*=x;
k>>=1;
}
return ret;
}
modint& operator +=(const modint& rhs){
this->val+=rhs.val;
if(this->val>=MOD)this->val-=MOD;
return *this;
}
friend modint operator+(modint lhs, const modint& rhs){
lhs+=rhs;
return lhs;
}
modint& operator -=(const modint& rhs){
this->val+=MOD-rhs.val;
if(this->val>=MOD)this->val-=MOD;
return *this;
}
friend modint operator-(modint lhs, const modint& rhs){
lhs-=rhs;
return lhs;
}
modint& operator *=(const modint& rhs){
this->val*=rhs.val;
this->val%=MOD;
return *this;
}
friend modint operator*(modint lhs, const modint& rhs){
lhs*=rhs;
return lhs;
}
modint& operator /=(const modint& rhs){
(*this)*=modpow(rhs,MOD-2);
//(*this)*=inv[rhs.val];
return *this;
}
friend modint operator/(modint lhs, const modint& rhs){
lhs/=rhs;
return lhs;
}
};
//template<int MOD>
//modint<MOD> modint<MOD>::inv[2005];
const int MOD=998244353;
typedef modint<MOD> mi;
int main(){
int n,m,k;
cin>>n>>m>>k;
if(n>m)swap(n,m);
if(n==1){
cout<<modpow(mi(k),m).val<<endl;
return 0;
}
mi ret=0;
for(int i=1;i<=k;i++){
mi l=modpow(mi(i),n)-modpow(mi(i-1),n);
mi r=modpow(mi(k-i+1),m);
ret+=l*r;
}
cout<<ret.val<<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;}
//-------------------------------------------------------
int N;
vector<pair<int,ll>> E[201010];
const ll mo=1000000007;
ll pat;
ll C[2];
void dfs(int cur,int pre,int id,int d) {
C[id]++;
FORR2(e,c,E[cur]) if(e!=pre) {
if(c&(1LL<<d)) {
dfs(e,cur,id^1,d);
}
else {
dfs(e,cur,id,d);
}
}
}
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N;
FOR(i,N-1) {
ll a;
cin>>x>>y>>a;
E[x-1].push_back({y-1,a});
E[y-1].push_back({x-1,a});
}
ll ret=0;
FOR(i,60) {
ZERO(C);
dfs(0,0,0,i);
(ret+=C[0]*C[1]%mo*((1LL<<i)%mo))%=mo;
}
cout<<ret<<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;
}
|
/* Lucky_Glass */
#include <cstdio>
#include <cstring>
#include <algorithm>
const int N = 1e5 + 10, INF = 0x3f3f3f3f;
int n;
char str[N];
int dp[N], mndp[30];
int main() {
scanf("%d%s", &n, str + 1);
memset(mndp, 0x3f, sizeof mndp);
mndp[str[1] - 'a'] = 0;
for (int i = 1; i <= n; ++i) {
int &now = dp[i] = INF;
for (int c = 0; c < 26; ++c)
if (c != str[i] - 'a')
now = std::min(now, mndp[c] + 1);
if (i < n)
mndp[str[i + 1] - 'a'] = std::min(mndp[str[i + 1] - 'a'], now);
}
printf("%d\n", dp[n] == INF ? -1 : dp[n]);
return 0;
} | // by pakhomovee
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <cmath>
#include <unordered_map>
#include <cassert>
#include <queue>
#include <unordered_set>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
#define dbg(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FEL(a, x) for (auto& a : x)
#define REV reverse
#define asrt assert
#define unique1(x) x.resize(unique(all(x)) - x.begin())
const int mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
if (s[0] != s.back()) {
cout << 1;
} else {
int df = 0;
for (int i = 0; i + 1 < s.size(); ++i) {
if (s[i] != s[0] && s[i + 1] != s[0]) {
cout << 2;
return;
}
}
cout << -1;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
solve();
}
}
|
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define int long long
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define max Max
#define min Min
#define abs Abs
using namespace std;
inline int read()
{
int res=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){res=res*10+c-'0';c=getchar();}
return res*f;
}
template<typename T,typename TT> inline T Max(T a,TT b){return a>b?a:b;}
template<typename T,typename TT> inline T Min(T a,TT b){return a>b?b:a;}
template<typename T> inline T Abs(T x){return x<0?-x:x;}
int a,b,x,y;
signed main()
{
a=read();b=read();x=read();y=read();
int ans=0;
if(a>b) ans=min((abs(a-b)-1)*y+x,(abs(a-b)*2-1)*x);
else if(a<b) ans=min(abs(a-b)*y+x,(abs(a-b)*2+1)*x);
else if(a==b) ans=x;
printf("%lld\n",ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define ll long long
int f(int n){
return n%10 + n%100/10 + n/100;
}
int main(){
int a, b;
cin >> a >> b;
cout << max(f(a), f(b)) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
using vll = vector<ll>;
constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=1e9+7 ;
int main(){
int n;
cin>>n;
vll v(n);
ll k=0;
rep(i,n){
ll a,b;cin>>a>>b;
v[i]=2*a+b;
k+=a;
}
sort(all(v),greater<ll>());
int ans=0;
rep(i,n){
if(k<0LL)break;
k-=v[i];
ans++;
}
cout<<ans;
} | #include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
using namespace std;
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using lli = long long;
using vi = vc<int>;
using ii = pair<int,int>;
lli gcd(lli a, lli b){return (b?gcd(b,a%b):a);}
lli lcm(lli a, lli b){ if(!a || !b) return 0; return a * b / gcd(a, b); }
int popcount(lli x) { return __builtin_popcountll(x); }
lli poww(lli a, lli b){
lli res = 1;
while(b){ if(b&1) res = res*a; a=a*a; b>>=1;}
return res;
}
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int rnd(int n){return uniform_int_distribution<int>(0, n-1)(rng);}
template<class t,class u>bool mmax(t&a,u b){if(a<b)a=b;return a<b;}
template<class t,class u>bool mmin(t&a,u b){if(b<a)a=b;return b<a;}
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
// ---- しゃけ ツナマヨ ('-')7
typedef struct node *pn;
struct node{
array<lli,2>a;
pn l,r;
node(){a[0]=a[1]=0; l = r = 0;}
};
lli g0(pn u){
if(u)return u->a[0];
return 0;
}
lli g1(pn u){
if(u)return u->a[1];
return 0;
}
array<lli,2> SUM(array<lli,2> a, array<lli,2> b){
return {a[0]+b[0],a[1]+b[1]};
}
void upd(pn &u, int l, int r, int val, int side){
if(l>r)return;
if(l==r){
u->a[0]+=side;
u->a[1]=(u->a[0])*val;
return;
}
int mid = (l+r)/2;
if(val<=mid){
if(!u->l)u->l = new node();
upd(u->l,l,mid,val,side);
}
else{
if(!u->r)u->r = new node();
upd(u->r,mid+1,r,val,side);
}
u->a[0]=g0(u->l)+g0(u->r);
u->a[1]=g1(u->l)+g1(u->r);
}
array<lli,2> find(pn &u, int l, int r, int ll, int rr){
if(!u or l>r or l>rr or r<ll)return {0,0};
if(ll<=l and r<=rr)return {g0(u),g1(u)};
int mid = (l+r)/2;
return SUM(find(u->l,l,mid,ll,rr), find(u->r,mid+1,r,ll,rr));
}
int main(){_
auto solve=[&](){
int L = 0, R = 1e8+5;
int n,m,q; cin>>n>>m>>q;
pn A = new node(), B = new node();
upd(A,L,R,0,n);
upd(B,L,R,0,m);
vi aa(n,0), bb(m,0);
lli sum = 0;
auto g=[&](pn &a, pn &b, int &x, int y){
auto tot = find(a,L,R,L,R);
auto aux = find(a,L,R,L,x);
sum-= (tot[1]-aux[1])+aux[0]*x;
upd(b,L,R,x,-1);
x = y;
upd(b,L,R,x,+1);
aux = find(a,L,R,L,x);
sum+=(tot[1]-aux[1])+aux[0]*x;
};
fore(i,0,q){
int t,x,y; cin>>t>>x>>y; x--;
if(t==2)g(A,B,bb[x],y);
else g(B,A,aa[x],y);
cout<<sum<<ENDL;
}
};
//int t; cin>>t; while(t--)
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// int/long: -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 (-2^63 <= long < 2^63)
//#define INF (1<<30)
#define INF (2147483647)
// 2^31 -1
//= 1,073,741,824 *2 -1
//= 536,870,912 *4 -1
#define MOD 1000000007
#define Rep0(i, n) for (auto i=0; i<n; i++)
#define Rep1(i, n) for (auto i=1; i<=n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll N;
cin >>N;
vector<ll> A(N, 0);
Rep0(i, N)
cin >>A.at(i);
ll ANS=0;
ll D=0;
ll DMax=0;
ll X=0;
Rep0(i, N) {
D +=A.at(i);
DMax=max(DMax, D);
ANS=max(ANS, X+DMax);
X +=D;
}
cout <<ANS <<endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
#define all(a) a.begin(), a.end()
using namespace std;
using P = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vv = vector<vi>;
template<typename T>
struct BIT{
int n;
vector<T> d;
BIT(int n=0): n(n), d(n+1) {}
void add(int i, T x=1){//場所i(0-indexed)にxを足す
for(i++; i<=n; i+= i&-i){
d[i] += x;
}
}
T sum(int i){//場所i以下(0-indexed)の和
T x = 0;
for(i++; i; i -= i&-i){
x += d[i];
}
return x;
}
};
int main(){
int h, w, m;
cin >> h >> w >> m;
vi left(h, w);
vi top(w, h);
vv wall(h);
//vector<set<int>> yoko(h);
//vector<set<int>> tate(w);
rep(i,m){
int x, y;
cin >> x >> y;
x--; y--;
left[x] = min(left[x], y);
top[y] = min( top[y], x);
wall[x].push_back(y);
//yoko[x].insert(y);
//tate[y].insert(x);
}
//rep(i,h)yoko[i].insert(w);
//rep(j,w)tate[j].insert(h);
//1手で動けるマス数
ll ans = left[0] + top[0] -1;
//横縦の順で動けるマス数
for(int j=1; j<left[0]; j++){
ans += top[j]-1;
}
//横縦の順で動けなかったが縦横でなら動ける(縦からは影になっている)マス数
BIT<int> bit(w);
for(int j0 = left[0]; j0<w; j0++)
bit.add(j0, 1);
for(int i=1; i<top[0]; i++){
ans += bit.sum(left[i]-1);
auto itr = wall[i].begin();
while(itr != wall[i].end()){
if(bit.sum(*itr) == bit.sum(*itr-1))
bit.add(*itr, 1);
itr++;
}
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=998244353;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int H,W;
cin>>H>>W;
vector<string> S(H);
rep(i,H) cin>>S[i];
vector<vector<pair<int,int>>> v(H+W);
rep(i,H) rep(j,W) v[i+j].push_back(mkp(i,j));
ll ans=1;
for(int k=0;k<H+W-1;k++){
ll r=0,b=0;
ll c=0;
for(auto [i,j]:v[k]){
if(S[i][j]=='R') r++;
else if(S[i][j]=='B') b++;
else c++;
}
if(r>0&&b>0) ans=0;
else if(r>0||b>0);
else{
ans=ans*2%MOD;
}
}
cout<<ans<<endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=500,mod=998244353;
int n,m;
char s[N+9][N+9];
void into(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
scanf("%s",s[i]+1);
}
int cnt[N*2+9][3],ans;
void Get_ans(){
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j) ++cnt[i+j][s[i][j]=='R'?2:s[i][j]=='B'];
ans=1;
for (int i=2;i<=n+m;++i){
if (cnt[i][1]&&cnt[i][2]) ans=0;
if (!cnt[i][1]&&!cnt[i][2]) ans=1LL*ans*2%mod;
}
printf("%d\n",ans);
}
void work(){
Get_ans();
}
void outo(){
}
int main(){
int T=1;
//scanf("%d",&T);
for (;T--;){
into();
work();
outo();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for (int i = (n)-1; i >= 0; --i)
#define dup(x,y) (((x)+(y)-1)/(y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
template<typename T> bool chmax(T &a, T b) {if(a<b){a=b; return 1; } return 0; }
template<typename T> bool chmin(T &a, T b) {if(a>b){a=b; return 1; } return 0; }
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;}
ll mpow(ll x, ll p) {
if (p==0) return 1;
ll res=mpow(x,p/2); res=res*res%MOD;
return p%2?res*x%MOD:res;
}
ll lpow(ll x, ll p) {
if (p==0) return 1;
ll res=lpow(x,p/2); res*=res;
return p%2?res*x:res;
}
int main() {
int x;
cin >> x;
x %= 100;
if (x == 0) cout << 100 << endl;
else cout << 100 - x << endl;
} | #include <iostream>
using namespace std;
int main() {
int n;
while(cin>>n){
int ans=100-n%100;
cout<<ans<<endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
#define GODSPEED ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define f f
#define s second
#define newl cout<<"\n";
#define pb push_back
#define mset(a,x) memset(a,x,sizeof(a))
#define debv(a) for(auto it: a)cout<<it<<" ";newl;
#define deb1(a) cout<<a<<"\n";
#define deb2(a,b) cout<<a<<" "<<b<<"\n";
#define deb3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n";
#define deb4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define uniq(a) a.resize(unique(a.begin(), a.end()) - a.begin());
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll N = 3005;
const ll mod = 998244353;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
ll n, k, dp[N][N];
ll mem(int i, int j){
if(i < j) return 0;
if(i == 0) return j == 0;
if(j == 0) return i == 0;
if(i == j) return 1;
if(dp[i][j] != -1) return dp[i][j];
dp[i][j] = (mem(i - 1, j - 1) + mem(i, 2 * j)) % mod;
return dp[i][j];
}
void solve(){
mset(dp, -1);
cin >> n >> k;
deb1(mem(n, k));
}
int main(){
GODSPEED;
int test = 1;
//cin >> test;
for(int i = 1; i <= test; i++){
solve();
}
#ifndef ONLINE_JUDGE
cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
#endif
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
template <const int32_t MOD> struct ModInt {
long long x;
ModInt() : x(0) {}
ModInt(long long u) : x(u) {
x %= MOD;
if (x < 0)
x += MOD;
}
friend bool operator==(const ModInt &a, const ModInt &b) {
return a.x == b.x;
}
friend bool operator!=(const ModInt &a, const ModInt &b) {
return a.x != b.x;
}
friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
friend bool operator<=(const ModInt &a, const ModInt &b) {
return a.x <= b.x;
}
friend bool operator>=(const ModInt &a, const ModInt &b) {
return a.x >= b.x;
}
static ModInt sign(long long k) {
return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
}
ModInt &operator+=(const ModInt &m) {
x += m.x;
if (x >= MOD)
x -= MOD;
return *this;
}
ModInt &operator-=(const ModInt &m) {
x -= m.x;
if (x < 0LL)
x += MOD;
return *this;
}
ModInt &operator*=(const ModInt &m) {
x = (1LL * x * m.x) % MOD;
return *this;
}
friend ModInt operator-(const ModInt &a) {
ModInt res(a);
if (res.x)
res.x = MOD - res.x;
return res;
}
friend ModInt operator+(const ModInt &a, const ModInt &b) {
return ModInt(a) += ModInt(b);
}
friend ModInt operator-(const ModInt &a, const ModInt &b) {
return ModInt(a) -= ModInt(b);
}
friend ModInt operator*(const ModInt &a, const ModInt &b) {
return ModInt(a) *= ModInt(b);
}
static long long fp(long long u, long long k) {
long long res = 1LL;
while (k > 0LL) {
if (k & 1LL)
res = (res * u) % MOD;
u = (u * u) % MOD;
k /= 2LL;
}
return res;
}
static constexpr int mod() { return MOD; }
ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
ModInt inv() {
assert(x);
return ModInt(fp(x, MOD - 2));
}
ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
friend ModInt operator/(const ModInt &a, const ModInt &b) {
return ModInt(a) *= ModInt(b).inv();
}
friend ostream &operator<<(ostream &out, const ModInt &a) {
return out << a.x;
}
friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
};
const int MOD = 998244353;
using Mint = ModInt<MOD>;
const int MAXN = 3001;
Mint dp[MAXN][MAXN];
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
dp[0][0] = 1;
for (int res(1); res < MAXN; ++res)
for (int nec(MAXN - 1); nec >= 1; --nec) {
dp[res][nec] =
dp[res - 1][nec - 1] + (2 * nec < MAXN ? dp[res][2 * nec] : 0);
}
cout << dp[N][K] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef vector <int> vi;
typedef pair <int, int> pii;
typedef vector <long long> vll;
typedef pair <long long, long long> pll;
#define pb push_back
#define all(c) c.begin(), c.end()
#define For(i, a, b) for (long long i = a; i < b; ++i)
#define Forr(i, a, b) for (long long i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second
#define ll long long
#define endl "\n"
#define min(a,b) (a < b ? a : b)
#define max(a,b) (a > b ? a : b)
#define fast_io() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) {cout << name << " : " << arg1 << 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...);}
/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
const int N = 2010;
vector <pii> graph[N];
bool visited[N];
int dist[N];
int dijkstra(int org) {
priority_queue <pii, vector <pii>, greater <pii>> pq;
For(i,0,N) dist[i] = INT_MAX;
memset(visited, 0, sizeof(visited));
pq.push({0, org});
bool f = 0;
while (!pq.empty()) {
int source = pq.top().S, d = pq.top().F;
pq.pop();
if (f and source == org) return d;
f = 1;
if (visited[source]) continue;
visited[source] = 1;
for (pii a : graph[source]) {
if (dist[a.F] < d + a.S) continue;
dist[a.F] = d + a.S;
pq.push({d + a.S, a.F});
}
}
return -1;
}
void solve() {
int n,m; cin >> n >> m;
For(i,0,m) {
int u,v,c; cin >> u >> v >> c;
graph[u].pb({v,c});
}
For(i,1,n+1) {
cout << dijkstra(i) << endl;
}
}
int main() {
fast_io();
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pb push_back
#define int long long int
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define F first
#define S second
#define mod 1000000007
#define MOD 998244353
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define deb(x) cout<< ">" << #x << '=' << x << endl;
#define read(v) for(auto &it:v) cin>>it;
const int N = 2e5 + 5;
void solve() {
int n, m, t;
cin >> n >> m >> t;
int prev = 0;
int charge = n;
int last;
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
charge -= (l - prev);
if (charge <= 0) {
cout << "No" << endl;
return;
}
charge += (r - l);
charge = min(charge, n);
prev = r;
}
charge -= (t - prev);
if (charge > 0) {
cout << "Yes" << endl;
return;
}
else {
cout << "No" << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
} |
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<set>
#include<map>
#include<iostream>
#define fi first
#define se second
#define mk make_pair
#define pb push_back
#define ls (t<<1)
#define rs ((t<<1)|1)
#define mid ((l+r)>>1)
#define N 500005
using namespace std;
int n, a[N], id[N], vis[N], Count[N];
int cmp(int x, int y)
{
return a[x] > a[y];
}
int main()
{
scanf("%d", &n); n <<= 1;
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), id[i] = i;
sort(id + 1, id + n + 1, cmp);
for (int i = 1; i <= n / 2; ++i) vis[id[i]] = 1;
for (int i = 1; i <= n; ++i)
{
if (!Count[vis[i] ^ 1]) printf("("), Count[vis[i]] ++;
else Count[vis[i] ^ 1] --, printf(")");
}
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<int,int,int>
using Graph = vector<vector<Tp>>;
ll mod = 998244353;
int main() {
ll N; cin >> N;
vector<Pr> seq;
rep(i,2*N){
ll a; cin >> a;
seq.push_back(make_pair(a,i));
}
sort(seq.begin(),seq.end());
int small[2*N];
rep(i,2*N) small[i] = 0;
rep(i,N){
int k = seq[i].second; small[k] = 1;
}
ll cnt[2];
rep(i,2) cnt[i] = 0;
rep(i,2*N){
if(cnt[small[i]]<cnt[(small[i]^1)]) cout << ")";
else cout << "(";
cnt[small[i]]++;
}
cout << endl;
//cout << ans << endl;
} |
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#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 main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
int A[1001], B[1001];
char C[1001];
rep(i, M) {
cin >> A[i] >> B[i] >> C[i];
A[i]--;
B[i]--;
}
vector<int> E[1000010];
rep(i, M) rep(j, M) {
if (C[i] != C[j]) continue;
if (i == j) continue;
E[A[i] * N + A[j]].pb(B[i] * N + B[j]);
E[A[i] * N + B[j]].pb(B[i] * N + A[j]);
E[B[i] * N + A[j]].pb(A[i] * N + B[j]);
E[B[i] * N + B[j]].pb(A[i] * N + A[j]);
}
int D[1000100] = {};
rep(i, N* N) D[i] = 1e9;
queue<int> que;
que.push(N - 1);
D[N - 1] = 0;
while (que.size()) {
int u = que.front();
que.pop();
for (auto to : E[u]) {
if (D[to] < 1e8) continue;
chmin(D[to], D[u] + 1);
que.push(to);
}
}
//if (D[(N - 1) * N] > 1e8) co(-1);
//else co(D[(N - 1) * N]);
int kotae = 1e9;
rep(i, N) chmin(kotae, D[i * N + i] * 2);
rep(i, M) chmin(kotae, D[A[i] * N + B[i]] * 2 + 1);
rep(i, M) chmin(kotae, D[B[i] * N + A[i]] * 2 + 1);
if (kotae > 1e8) co(-1);
else co(kotae);
Would you please return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
string t = "";
cin >> s;
int f = 1;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'R') f *= -1;
else if (f == 1) {
if (*t.end() == s[i]) t.pop_back();
else t.push_back(s[i]);
}
else if (f == -1) {
if (*t.begin() == s[i]) t.erase(0, 1);
else t = s[i] + t;
}
}
if (f == -1) reverse(t.begin(), t.end());
for (int i = 0; i < t.size() - 1; i++) {
if (t.size() == 0) break;
while (t[i] == t[i + 1] && t.size()) {
t.erase(i, 2);
if (i > 0) i--;
}
}
cout << t << endl;
} |
#include <bits/stdc++.h>
#define LL long long
#define st first
#define nd second
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
using namespace std;
template <class T> T read(T &a) {
a=0;char x=getchar();bool f=0;
for(;x<'0'||x>'9';x=getchar())f|=x=='-';
for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0';
if(f)a=-a;
return a;
}
template <class T> void write(T a) {
static char s[40];int len=-1;
if(a<0)putchar('-'),a=-a;
if(a==0)putchar('0');
for (int i=0;i<40;++i)s[i]=0;
for(;a!=0;s[++len]=a%10+'0',a/=10);
for(int i=len;i>=0;--i)putchar(s[i]);
}
template <class T> void writeln(T a) {
write(a);
puts("");
}
int n, k;
int main() {
read(n), read(k);
if (n == 1 && k == 0) {
puts("1 2");
return 0;
}
if (k < 0 || k > n - 2) {
puts("-1");
return 0;
}
printf("%d %d\n", 1, (k + 2) * 2);
for (int i = 1; i <= k + 1; ++i) {
printf("%d %d\n", i * 2, i * 2 + 1);
}
for (int i = k + 3; i <= n; ++i) {
printf("%d %d\n", i * 2, i * 2 + 1);
}
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
#define SZ(x) (int)x.size()
#define pb push_back
#define FULL(x, y) memset(x, y, sizeof(x))
using namespace std;
const int N = 1000005, M = 1005;
int n, m;
vector<int> c[26], g[N];
int f[M][M], vis[N], dis[N];
void add(int a, int b, int u, int v) {
int x = a*n + u, y = b*n + v;
g[x].pb(y), g[y].pb(x);
x = b*n + u, y = a*n + v;
g[x].pb(y), g[y].pb(x);
x = b*n + v, y = a*n + u;
g[x].pb(y), g[y].pb(x);
x = a*n + v, y = b*n + u;
g[x].pb(y), g[y].pb(x);
}
int main() {
cin>> n >> m;
int a, b, u, v;
char ch;
for(int i=1; i<=m; ++i) {
cin>> a >> b >> ch;
u = a - 1, v = b - 1;
f[u][v] = 1, f[v][u] =1;
c[ch-'a'].pb(u*n+v);
}
for(int i=0; i<26; ++i) {
int len = SZ(c[i]);
for(int j=0; j<len; ++j) {
for(int k=j+1; k<len; ++k) {
a = c[i][j]/n, b = c[i][j] - a*n;
u = c[i][k]/n, v = c[i][k] - u*n;
add(a, b, u, v), add(u, v, a, b);
}
}
}
queue<int> q;
q.push(n-1);
vis[n-1] = 1;
dis[n-1] = 0;
while(!q.empty()) {
int fr = q.front();
q.pop();
for(int i=0; i<SZ(g[fr]); ++i) {
v = g[fr][i];
if (!vis[v]) {
dis[v] = dis[fr] + 1;
vis[v] = 1;
q.push(v);
}
}
}
int ans = -1;
for(int i=0; i<n; ++i) {
v = i*n + i;
if (vis[v] && (ans == -1 || (dis[v]*2 < ans))) {
ans = dis[v] * 2;
}
}
for(int i=0; i<n ;++i) {
for(int j=0; j<n; ++j) {
v = i*n + j;
if (f[i][j] && vis[v] && (ans == -1 || (dis[v]*2 + 1 < ans))) {
ans = dis[v]*2 + 1;
}
}
}
cout<< ans << endl;
return 0;
} |
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,mmx,avx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define PB push_back
#define MP make_pair
#define endl "\n"
#define fst first
#define snd second
const int UNDEF = -1;
const int INF=1<<30;
template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; }
template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; }
typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;typedef vector<pii> vpii;typedef vector<pll> vpll;
template<typename T> void makeunique(vector<T> &vx) {sort(vx.begin(),vx.end());auto it=unique(vx.begin(),vx.end());vx.resize(std::distance(vx.begin(),it));}
//If w[L,R] >= v[i], then dist[L,R] >= len[i]
int f[1<<8];
#define FORSSI(sub, full) for(int sub = 0 ;sub!=(full) ; sub = (sub - (full)) & (full))
int hquery(map<int,int> &h, int key) {
//Queries the largest (k,v) pair where k<=key
auto it=h.upper_bound(key);
if (it==h.begin()) {
return 0;
}
--it;
return it->snd;
}
void hins(map<int,int> &h, int key, ll val) {
//Inserts (key,val) into h.
//Maintains the invariant that if key1<key2, then val1<val2.
auto it=h.upper_bound(key);
if (it==h.begin()) {
it=h.insert(MP(key,val)).fst;
}
else {
--it;
if (it->snd >= val) return;
if (it->fst==key) it->snd = val;
else it=h.insert(MP(key,val)).fst;
}
++it;
while(it!=h.end()) {
if (it->snd <= val) it=h.erase(it);
else return;
}
}
ll solve(long long N, long long M, std::vector<long long> w, std::vector<long long> vlen, std::vector<long long> v){
{
ll wmax=0; for (auto &x:w) chkmax(wmax,x);
ll vmin=LLONG_MAX; for (auto &x:v) chkmin(vmin,x);
if (wmax>vmin) return -1;
}
map<int,int> h;
for (int i=0;i<M;i++) hins(h,v[i],vlen[i]);
vi perm; for (int i=0;i<N;i++) perm.PB(i);
int final=INT_MAX;
do {
memset(f,0,sizeof f);
for (int l=0;l<N;l++) {
int sumw=w[perm[l]];
int sig=0;
for (int r=l+1;r<N;r++) {
sumw+=w[perm[r]];
int dist=hquery(h,sumw-1);
sig|=1<<(r-1);
//if(dist) printf("l:%d r:%d sig:%d dist:%d\n",l,r,sig,dist);
f[sig]=dist;
}
}
int lim=1<<N;
for (int x=3;x<lim;x++) {
int best=f[x];
FORSSI(y,x) {
chkmax(best,f[y]+f[x^y]);
}
//if(best) printf("x:%d best:%d\n",x,best);
f[x]=best;
}
chkmin(final,f[lim-1]);
} while(next_permutation(perm.begin(),perm.end()));
return final;
}
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
long long N;
scanf("%lld",&N);
long long M;
scanf("%lld",&M);
std::vector<long long> w(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld",&w[i]);
}
std::vector<long long> l(M);
std::vector<long long> v(M);
for(int i = 0 ; i < M ; i++){
scanf("%lld",&l[i]);
scanf("%lld",&v[i]);
}
ll ans=solve(N, M, std::move(w), std::move(l), std::move(v));
printf("%lld\n",ans);
return 0;
}
| // RioTian 21/03/24
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
vector<pair<ll, ll>> towns;
ll sa = 0, sb = 0; // T,A
for (int i = 0; i < n; ++i) {
int a, b;
cin >> a >> b;
towns.emplace_back(a, b);
sa += a;
}
sort(towns.begin(), towns.end(), [](pair<ll, ll>& p, pair<ll, ll>& q) {
return p.first * 2 + p.second > q.first * 2 + q.second;
});
for (int i = 0; i < n; ++i) {
if (sa < sb) {
cout << i << "\n";
return 0;
}
sa -= towns[i].first, sb += towns[i].first + towns[i].second;
}
cout << n << "\n";
return 0;
} |
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include <bits/stdc++.h>
using namespace std;
int main(){
long long N;
cin >> N;
long long ans = 0;
for(int i = 1; i <= 5; i++){
if(N >= pow(10, 3*i)){
ans += (N-pow(10, 3*i)+1);
}
}
cout << ans << endl;
}
| // 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());
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
string str;
cin>>str;
ll ans=0;
for(int i=0;i<=str.length()-4;i++){
string x = str.substr(i,4);
if(x=="ZONe")ans++;
}
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("little.in", "r", stdin);
freopen("little.out", "w", stdout);
#endif
int t=1;
// cin>>t;
fr(i,1,t)
{
// cout<<"Case #"<<i<<": ";
solve();
}
end_routine();
} |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5+10;
int to_int(char c) {
if(c == 'R') return 0;
if(c == 'W') return 1;
if(c == 'B') return 2;
assert(false);
}
char to_char(int x) {
if(x == 0) return 'R';
if(x == 1) return 'W';
if(x == 2) return 'B';
assert(false);
}
int C(int x, int y) {
int ans = 1;
while(x > 0) {
if(x%3 < y%3) return 0;
if(y%3 == 1 && x%3 == 2) ans = ans * 2 % 3;
x /= 3;
y /= 3;
}
return ans;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n;
string s;
cin >> n >> s;
ll ans = 0, pw = 1;
for(int i = 1; i < n; ++i) {
pw = pw * 2 % 3;
}
for(int i = 0; i < n; ++i) {
ans += C(n-1, i) * to_int(s[i]) * pw;
ans %= 3;
}
cout << to_char(ans) << endl;
} | #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define repr(i,a,b) for(int i = a ; i >= b; --i)
#define NAME "NAME"
#define pb push_back
#define mp make_pair
#define EL cout << '\n'
#define cqtshadow int main
#define fillchar(a,x) memset(a, x, sizeof (a))
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
typedef int64_t ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef pair<ll, ll> iil;
const ll oo = 1e18;
const int inf = 1e9;
const int mod = 3;
const int N = 4e5 + 7;
const int M = 1e6 + 7;
void file() {freopen ( NAME ".inp", "r", stdin ); freopen ( NAME ".out", "w", stdout ); freopen ( NAME ".err", "w", stderr);}
int n, c[N], C_[5][5];
string s;
void cal() {
rep(i, 0, 3)
rep(j, 0, i)
if(i == 0 || j == 0) C_[i][j] = 1;
else C_[i][j] = (C_[i - 1][j]%mod + C_[i - 1][j - 1]%mod)%mod;
}
int C(int k, int n) {
if(k < mod && n < mod) return C_[n][k];
return (C(k/mod, n/mod)%mod * C_[n%mod][k%mod])%mod;
}
cqtshadow () {
faster
cin >> n >> s;
rep(i, 1, n) {
if(s[i - 1] == 'W') c[i] = 0;
if(s[i - 1] == 'R') c[i] = 1;
if(s[i - 1] == 'B') c[i] = 2;
}
cal();
int res = 0;
rep(i, 1, n) {
res = (res + (c[i] * C(i - 1, n - 1)%mod)%mod)%mod;
}
if(n%2 == 0) res *= -1;
res = (res%mod + mod*1000000)%mod;
if(res == 0) cout << "W";
if(res == 1) cout << 'R';
if(res == 2) cout << "B";
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
#define mp make_pair
#define pb push_back
#define sz(x) ((int)((x).size()))
#define all(v) (v).begin(), (v).end()
#define endl "\n"
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#define files(name) ifstream fin(name".in"); ofstream fout(name".out");
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
const int mod = 1e9 + 7;
const int mxN = 101;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
int ans=n/100;
if(ans*100<n) ++ans;
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define w(x) int x; cin>>x; while(x--)
void basics()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int32_t main()
{
basics();
int year;
cin >> year;
float ans = (float)year / 100;
int yr = (int)ans;
// cout << ans;
if ((float)yr == ans)
cout << yr;
else
cout << yr + 1;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fast_io cin.tie(0);ios_base::sync_with_stdio(0);
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cout << endl; }
void show() { cout << endl; }
void pret() { cout << endl; exit(0);}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cout << " " << to_string(H); debug_out(T...);}
template <typename Head, typename... Tail> void show(Head H, Tail... T) {cout <<H<<" "; show(T...);}
template <typename Head, typename... Tail> void pret(Head H, Tail... T) {cout <<H<<" "; pret(T...);}
#define pr(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
typedef long long ll;
#define int ll
typedef long double ld;
typedef vector<int> vi;
#define disp(x) cout<<x<<" ";
#define rep(i,a,b) for(int i=a;i<(int)b;i++)
#define fo(i,a,b) for(int i=a;i<=(int)b;i++)
#define rf(i,a,b) for(int i=a;i>=(int)b;i--)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
#define endl '\n'
//cout.setf(ios::fixed);cout.precision(18)
const int MOD = 1e9+7;
const int maxn = 2e5+10;
int k, factorial[17], h[maxn][17], p[17];
int tot = 0, nCr[17][17];
vi dp(16, 0);
void add(int &ans, int add){
add %= MOD;
ans += add;
if(ans >= MOD) ans -= MOD;
if(ans < 0) ans += MOD;
}
void calc(){
p[0] = 0;
fo(i, 1, 16){
p[i] = i;
}
h[0][0] = 1;
fo(n, 1, maxn - 1){
fo(x, 0, k - 1){
if(n < x) continue;
int &ans = h[n][x];
for (int i = 0; i <= x; ++i)
{
int mul = 1;
if(i % 2) mul = -1;
add(ans, mul * nCr[x][i] * p[k - i]);
}
}
fo(i, 1, 16){
p[i] = (p[i] * i) % MOD;
}
}
}
int find(int n, int x){
if(x < 0) return 0;
if(n == 0){
if(x == 0) return 1;
return 0;
}
assert(x <= k);
int ans = nCr[16 + x - k][x];
// show(n, x, ans, h[n][x]);
ans =(ans * h[n][x]) % MOD;
return ans;
}
void increase(int id){
dp[id]++;
if(dp[id] == 1) tot++;
}
void decrease(int id){
dp[id]--;
if(dp[id] == 0) tot--;
}
int32_t main(){
fast_io;
string st;
cin >> st >> k;
factorial[0] = 1;
fo(i, 1, 16) factorial[i] = (factorial[i - 1] * i);
for (int i = 0; i < 17; ++i)
{
for (int j = 0; j < i + 1; ++j)
{
nCr[i][j] = (factorial[i]) / (factorial[j] * factorial[i - j]);
nCr[i][j] %= MOD;
}
}
calc();
vi v;
for(const auto &it: st){
if(it >= '0' and it <= '9') v.emplace_back(it - '0');
else v.emplace_back(10 + it - 'A');
}
int ans = 0, rem = v.size();
for(auto it: v){
if(tot > k) break;
rem--;
for (int i = 0; i < it; ++i)
{
increase(i);
if(tot == 1 and dp[0] > 0){
}
else add(ans, find(rem, k - tot));
decrease(i);
}
increase(it);
}
if(tot == k) add(ans, 1);
// show(ans);
rem = v.size() - 1;
fo(i, 1, v.size() - 1){
rem--;
add(ans, 15 * find(rem, k - 1));
}
show(ans);
return 0;
} | #include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#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";
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(z, v.size()) cerr << " " << v[z]; \
cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;
template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
rep(j, v.size()) i >> v[j];
return i;
}
template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
return i >> v.first >> v.second;
}
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
return to_string(x).size();
}
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
template <class T>
void chmax(T& a, const T& b) {
if (a < b) a = b;
}
template <class T>
void chmin(T& a, const T& b) {
if (b < a) a = b;
}
ll mod_pow(ll x, ll n, ll mod) {
if (n == 0) return 1;
ll res = mod_pow(x * x % mod, n / 2, mod);
if (n & 1) res *= x;
res %= mod;
return res;
}
inline ll mod(ll a, ll m) {
return (a % m + m) % m;
}
ll ext_gcd(ll a, ll b, ll& p, ll& q) {
if (b == 0) {
p = 1;
q = 0;
return a;
}
ll d = ext_gcd(b, a % b, q, p);
q -= a / b * p;
return d;
}
P crt(ll b1, ll m1, ll b2, ll m2) {
ll p, q;
ll d = ext_gcd(m1, m2, p, q);
if ((b2 - b1) % d != 0) return {0, -1};
ll m = lcm(m1, m2);
ll tmp = (b2 - b1) / d * p % (m2 / d);
ll r = mod(b1 + m1 * tmp, m);
return make_pair(r, m);
}
signed main() {
init();
ll T;
cin >> T;
while (T--) {
ll N;
cin >> N;
vs S(3);
cin >> S;
cout << 1;
rep(i, N) cout << 0;
rep(i, N) cout << 1;
ENDL;
}
return 0;
} |
// Problem: C - Swappable
// Contest: AtCoder - AtCoder Beginner Contest 206(Sponsored by Panasonic)
// URL: https://atcoder.jp/contests/abc206/tasks/abc206_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define fr(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define mp make_pair
#define vl vector<ll>
#define mll map<ll,ll>
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define vii std::vector<int,int>
#define tc int t;cin>>t;while(t--)
#define FILE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inp(a) long long a;cin>>a;
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
const ll mod=1e9+7;
const ll N=4e5+5;
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
void solve(){
ll n; cin>>n;
map<ll,ll> m;
fr(i,n){
ll x; cin>>x;
m[x]++;
}
ll ans=0, temp=n;
for(auto i:m){
ll j=i.second;
temp-=j;
ans+=j*temp;
}
cout<<ans;
}
int main(){
fio;
ll t=1; //cin>>t;
while(t--){
solve();
}
printclock;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
int N;
scanf("%d",&N);
map<int,int> M;
static int A[(int)3e5+16];
for(int i=0;i<N;i++){
scanf("%d",&A[i]);
M[A[i]]++;
}
ll result=0;
for(int i=0;i<N;i++){
result+=N-M[A[i]];
}
printf("%lld",result/2);
return 0;
}
|
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include "stdio.h"
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define mp(x,y) make_pair(x,y)
// input
void read(ll a[], int s, int n) {
for (int i = 0 + s; i < n + s; ++i) {
scanf("%ll", &a[i]);
}
}
void read(int a[], int s, int n) {
for (int i = 0 + s; i < n + s; ++i) {
scanf("%d", &a[i]);
}
}
// fenwick
ll getSum(ll bits[], int index)
{
ll sum = 0;
while (index > 0)
{
sum += bits[index];
index -= index & (-index);
}
return sum;
}
void updateAt(ll bits[], int n, int index, ll val)
{
while (index <= n)
{
bits[index] += val;
index += index & (-index);
}
}
static const int MX_VAL = 1e6 + 2;
ll bits[MX_VAL + 1] = {};
// inversions
ll getInvCnt(ll a[], int n)
{
ll cnt = 0;
memset(bits, 0, sizeof(bits));
for (int i = n - 1; i >= 0; i--)
{
cnt += getSum(bits, a[i] - 1);
updateAt(bits, MX_VAL, a[i], 1);
}
return cnt;
}
// MODFIY N!!!
const int N = 1 * 1e6 + 2;
// MODFIY MOD!!!
const int MOD = 1e9 + 7;
long long fac[N], rfac[N];
long long mul(long long a, long long b) { return 1LL * a * b % MOD; }
long long fpow(long long a, long long b) {
if (b == 0) return 1;
long long re = fpow(a, b / 2);
if (b % 2 == 1) return mul(a, mul(re, re));
return mul(re, re);
}
long long inv(long long x) {
return fpow(x, MOD - 2);
}
int nck(int x, int y) {
return mul(fac[x], mul(rfac[y], rfac[x - y]));
}
void prepareFac()
{
fac[0] = 1;
rfac[0] = inv(1);
for (int i = 1; i < N; ++i) {
fac[i] = i * fac[i - 1] % MOD, rfac[i] = inv(fac[i]);
}
}
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a%b);
}
ll nok(ll a, ll b) {
return a * b / gcd(a, b);
}
int a[N], b[N];
string s[2000];
vector<pii> v[256];
int vis[2000 * 2000];
int visa[256];
int ind(pii pii) {
return pii.first * 2000 + pii.second;
}
int n, m, cost; queue<pii> q;
void go(int x, int y) {
if (x < 0 || x == n)
return;
if (y < 0 || y == m)
return;
auto c = mp(x, y);
if (vis[ind(c)])
return;
if (s[x][y] == '#')
return;
q.push(c);
vis[ind(c)] = cost;
}
void solve_case()
{
cin >> n >> m;
pii start, finish;
for (int i = 0; i < n; ++i) {
cin >> s[i];
for (int j = 0; j < s[i].length(); ++j)
if (s[i][j] == 'S')
start = mp(i, j);
else if (s[i][j] == 'G')
finish = mp(i, j);
else if (s[i][j] >= 'a' && s[i][j] <= 'z')
v[s[i][j]].push_back(mp(i, j));
}
q.push(start);
vis[ind(start)] = 1;
while (!q.empty()) {
auto c = q.front(); q.pop();
if (c == finish) {
cout << vis[ind(c)] - 1;
return;
}
cost = vis[ind(c)] + 1;
go(c.first - 1, c.second);
go(c.first + 1, c.second);
go(c.first , c.second + 1);
go(c.first, c.second - 1);
int x = c.first;
int y = c.second;
if (s[x][y] >= 'a' && s[x][y] <= 'z' && !visa[s[x][y]]) {
visa[s[x][y]] = true;
for (auto p : v[s[x][y]]) {
if (!vis[ind(p)]) {
vis[ind(p)] = cost;
q.push(p);
}
}
}
}
cout << -1;
cout << endl;
}
void prepare()
{
}
int main()
{
#ifdef __OLIMP__
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T = 1;
// scanf("%d", &T);
//prepare();
for (int tc = 1; tc <= T; tc++)
solve_case();
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define db1(x) cout << #x << " = " << x << endl
#define db2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define paging cout << "-----------------------" << endl
using namespace std;
void localFileInput() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int dir[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1};
const ll mod = 998244353;
const int INF = 0x3f3f3f3f;
const int N = 2010;
int n, m;
char s[N][N];
bool vis[N][N];
bool use[30];
int f[N][N];
vector<pii> v[30];
pii st;
bool check(int x, int y) {
return (x>=0&&x<n) && (y>=0&&y<m) && !vis[x][y] && s[x][y] != '#';
}
int bfs() {
int x = st.first;
int y = st.second;
vis[x][y] = true;
f[x][y] = 0;
queue<pii> que; que.push(st);
while(!que.empty()) {
pii p = que.front(); que.pop();
x = p.first;
y = p.second;
if(s[x][y] == 'G') return f[x][y];
if(islower(s[x][y])) {
int t = s[x][y] - 'a';
if(!use[t]) {
use[t] = true;
for (pii val : v[t]) if(!vis[val.first][val.second]) {
vis[val.first][val.second] = true;
f[val.first][val.second] = f[x][y] + 1;
que.push(val);
}
}
}
for (int i = 0; i < 4; ++ i) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(check(nx, ny)) {
vis[nx][ny] = true;
f[nx][ny] = f[x][y] + 1;
que.push({nx, ny});
}
}
}
return -1;
}
void sol() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++ i) scanf("%s", s[i]);
for (int i = 0; i < n; ++ i) for (int j = 0; j < m; ++ j) {
if(s[i][j] == 'S') st = {i, j};
if(islower(s[i][j])) {
v[s[i][j]-'a'].pb({i, j});
}
}
printf("%d\n", bfs());
}
int main()
{
localFileInput();
// int _; scanf("%d", &_); while(_ --)
sol();
return 0;
} |
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <array>
#include <cassert>
//#include <chrono>
#include <cmath>
//#include <cstring>
//#include <functional>
//#include <iomanip>
#include <iostream>
#include <map>
//#include <numeric>
//#include <queue>
//#include <random>
#include <set>
#include <vector>
using namespace std;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define int long long
#define ll int
#define all(a) a.begin(),a.end()
#define rev(a) a.rbegin(),a.rend()
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
const double pi=acosl(-1);
int n,p;
int ans=0;
void rec(vector<int> a)
{
if(a.size()==n)
{
bool ok=true;
int sum=0;
fi(0,n)
{
sum+=a[i];
if(sum%p==0) ok=false;
}
if(ok)
{
ans++;
for(auto x:a)
{
cout<<x<<' ';
}
cout<<'\n';
}
return ;
}
fi(1,p)
{
a.pb(i);
rec(a);
a.pop_back();
}
}
const int mod=1e9+7;
const int mxn=1e6+3;
ll binpow(ll a,ll b)
{
ll res=1;
while(b)
{
if(b&1){
res=((res%mod)*(a%mod))%mod;
}
a=((a%mod)*(a%mod))%mod;
b=b>>1;
}
return res%mod;
}
void solve()
{
cin>>n>>p;
// rec({});
// cout<<ans<<'\n';
cout<<((p-1)*binpow((p-2),n-1))%mod<<'\n';
}
signed main()
{
FAST;
int tt=1;
//cin>>tt;
while(tt--)
{
solve();
}
}
//int dx[] = {+1,-1,+0,+0,-1,-1,+1,+1}; // Eight Directions
//int dy[] = {+0,+0,+1,-1,+1,-1,-1,+1}; // Eight Directions
//int dx[]= {-2,-2,-1,1,-1,1,2,2}; // Knight moves
//int dy[]= {1,-1,-2,-2,2,2,-1,1}; // Knight moves
// For (a^b)%mod, where b is large, replace b by b%(mod-1).
// a+b = (a|b)+(a&b)
// a+b = 2*(a&b)+(a^b)
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
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; }
long long pow(long long x, long long n, long long MM) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret = ret * x % MM; // n の最下位bitが 1 ならば x^(2^i) をかける
x = x * x % MM;
n >>= 1; // n を1bit 左にずらす
}
return ret;
}
int main(){
ll N,P;
cin>>N>>P;
cout << (P-1)*pow(P-2,N-1,MOD)%MOD << endl;
return 0;
} |
#include<bits/stdc++.h>
#define pb push_back
#define ll long long int
using namespace std;
int main()
{
ll i,j,k,T,c=0,n;
cin>>n>>T;
ll arr[n];
for(i=0;i<n;i++)
{
cin>>arr[i];
}
vector<ll>vec1,vec2,vec3;
for(i=0;i<n/2;i++)
{
vec1.pb(arr[i]);
}
for(i=n/2;i<n;i++)
{
vec2.pb(arr[i]);
}
n=vec1.size();
ll ans=0;
for(i=0;i<(1LL<<n);i++)
{
c=0;
for(j=0;j<n;j++)
{
if(i&(1LL<<j))
{
c+=vec1[j];
}
}
if(c<=T)
{
vec3.pb(c);
}
}
sort(vec3.begin(),vec3.end());
ans=vec3[vec3.size()-1];
c=0;
n=vec2.size();
for(i=0;i<(1LL<<n);i++)
{
c=0;
for(j=0;j<n;j++)
{
if(i&(1LL<<j))
{
c+=vec2[j];
}
}
ll id=lower_bound(vec3.begin(),vec3.end(),T-c)-vec3.begin();
if(c<=T)
ans=max(ans,c);
if(id==vec3.size())
ans=max(ans,c+vec3[id-1]);
else if(vec3[id]==T-c)
ans=max(ans,T);
else if(id>0)
ans=max(ans,c+vec3[id-1]);
}
cout<<ans;
return 0;
}
| #include<bits/stdc++.h>
#define re register
#define ll long long
#define dl double
#define LL inline ll
#define I inline int
#define V inline void
#define B inline bool
#define FOR(i,a,b) for(re int i=(a),i##i=(b) ; i<=i##i ; ++i)
#define ROF(i,a,b) for(re int i=(a),i##i=(b) ; i>=i##i ; --i)
#define gc getchar()
//#define gc (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<18,stdin),fs==ft))?0:*fs++
using namespace std;
const int N=4e5+10,inf=1e9+7;
char *fs,*ft,buf[1<<18];
LL read(){
ll p=0; bool w=0; char ch=gc;
while(!isdigit(ch)) w=ch=='-'?1:0,ch=gc;
while(isdigit(ch)) p=p*10+ch-'0',ch=gc;
return w?-p:p;
}
ll n,us1=3,us2=5,as1,as2;
map<ll,int>mp;
int main(){
n=read();
FOR(i,1,50){
mp[us1]=i,us1*=3;
if(us1>n) break;
}
FOR(i,1,50){
if(mp[n-us2]){
cout<<mp[n-us2]<<' '<<i;
return 0;
}
us2=us2*5;
if(us2>n) break;
}
cout<<-1;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define inf 1001000009
#define infmx 1e18ll
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define all(x) x.begin(),x.end()
///debug
#define wh(x) cerr<<#x<<" is "<<x<<endl;
#define error(args...){string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);err(_it,args);}
void err(istream_iterator<string>it){cout<<endl;}
template<class H,class... T>void err(istream_iterator<string>it,H a,T... t){cerr<<*it<<" = "<<a<<" | ";err(++it,t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class T>auto&operator<<(ostream& os,vector<T>_v){bool a=1;for(auto x:_v){os<<(a?"":" ")<<x;a=0;}return os;}
///
typedef pair<int,int>pi;
typedef pair<long long,long long>pll;
typedef pair<pi,int>ppi;
typedef pair<int,pi>pip;
typedef vector<int>vi;
typedef vector<pi>vpi;
const int mod=1e9+7;
ll power(ll a,ll p){ll r=1,y=a;while(p){if(p&1)r=(1LL*r*y)%mod;p>>=1;y=(1LL*y*y)%mod;}return r;}
const int N=2100000;
int ara[N];
int main()
{
IOS;
double tol=0.0000001;
double sx,sy,gx,gy;
cin>>sx>>sy>>gx>>gy;
if(sx>gx)
{
swap(sx,gx);
swap(sy,gy);
}
double l=sx,r=gx;
while(l<=r){
double mid=(l+r)/2;
if((sy/(mid-sx))>=(gy/(gx-mid)))l=mid+tol;
else r=mid-tol;
}
cout<<setprecision(8)<<r<<endl;
}
| #pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 998244353
using namespace std;
using ll = long long;
template<int M>
class ModInt {
public:
ll value;
constexpr ModInt(ll v = 0) { value = v % M; if (value < 0) value += M; }
constexpr ModInt pow(ll n) const { if (!n) return 1; ModInt a = pow(n >> 1); a *= a; if (n & 1) a *= *this; return a; }
constexpr ModInt inv() const { return this->pow(M - 2); }
constexpr ModInt operator + (const ModInt &rhs) const { return ModInt(*this) += rhs; };
constexpr ModInt operator - (const ModInt &rhs) const { return ModInt(*this) -= rhs; };
constexpr ModInt operator * (const ModInt &rhs) const { return ModInt(*this) *= rhs; };
constexpr ModInt operator / (const ModInt &rhs) const { return ModInt(*this) /= rhs; };
constexpr ModInt &operator += (const ModInt &rhs) { value += rhs.value; if (value >= M) value -= M; return *this; };
constexpr ModInt &operator -= (const ModInt &rhs) { value -= rhs.value; if (value < 0) value += M; return *this; };
constexpr ModInt &operator *= (const ModInt &rhs) { value = value * rhs.value % M; return *this; };
constexpr ModInt &operator /= (const ModInt &rhs) { return (*this) *= rhs.inv(); };
constexpr bool operator == (const ModInt &rhs) { return value == rhs.value; }
constexpr bool operator != (const ModInt &rhs) { return value != rhs.value; }
friend ostream &operator << (ostream &os, const ModInt &rhs) { os << rhs.value; return os; }
};
using mint = ModInt<MOD>;
int dx[] = {1, 0};
int dy[] = {0, 1};
vector<vector<char>> G(5000, vector<char>(5000, '*'));
vector<vector<mint>> dp(5001, vector<mint>(5001, 0));
int main() {
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
int H, W, K; cin >> H >> W >> K;
REP(i, 0, K) {
int h, w; char c; cin >> h >> w >> c;
G[--h][--w] = c;
}
dp[0][0] = mint(3).pow(H * W - K);
mint inv = mint(3).inv();
REP(i, 0, H) {
REP(j, 0, W) {
REP(d, 0, 2) {
if (d == 0 && G[i][j] == 'R') continue;
if (d == 1 && G[i][j] == 'D') continue;
int ni = i + dx[d];
int nj = j + dy[d];
if (G[i][j] == '*') {
dp[ni][nj] += (dp[i][j] * 2 * inv);
} else {
dp[ni][nj] += dp[i][j];
}
}
}
}
cout << dp[H - 1][W - 1] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr lint B1 = 1532834020;
constexpr lint M1 = 2147482409;
constexpr lint B2 = 1388622299;
constexpr lint M2 = 2147478017;
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }
template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N, M;
cin >> N >> M;
vector<P> B(M);
REP(i, M) cin >> B[i].first >> B[i].second;
sort(ALL(B));
set<int> st;
set<int> ins;
set<int> del;
st.insert(N);
int lastx = 0;
REP(i, M) {
int x = B[i].first;
int y = B[i].second;
if(x != lastx) {
for(int z : del) st.erase(z);
for(int z : ins) st.insert(z);
del.clear();
ins.clear();
lastx = x;
}
if(st.find(y) != st.end()) del.insert(y);
if(st.find(y+1) != st.end() || st.find(y-1) != st.end()) ins.insert(y);
}
for(int z : del) st.erase(z);
for(int z : ins) st.insert(z);
cout << st.size() << endl;
} | #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(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, t;
cin >> n >> t;
vector<int> a(n);
rep(i,n) cin >> a[i];
vector<int> b, c;
rep(i,n/2) b.push_back(a[i]);
for(int i = n/2; i < n; i++) c.push_back(a[i]);
vector<ll> sum1, sum2;
int n2 = n/2;
rep(i,1<<n2){
ll tmp = 0;
rep(j,n2) if(i>>j&1) tmp += b[j];
sum1.push_back(tmp);
}
rep(i,1<<(n-n2)){
ll tmp = 0;
rep(j,n-n2) if(i>>j&1) tmp += c[j];
sum2.push_back(tmp);
}
ll ans = 0;
sort(sum1.begin(), sum1.end());
sort(sum2.begin(), sum2.end());
rep(i,sum1.size()){
int id = upper_bound(sum2.begin(), sum2.end(), t-sum1[i]) - sum2.begin();
if(id == 0) continue;
ll tmp = sum1[i] + sum2[id-1];
ans = max(tmp, ans);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
string X; //原串
ll M; //最大
cin >> X >> M;
int n = X.size();
vector<ll> b(n); //n个向量(已经定型) 每个都是long long b数组
ll m = 0;
for (int i = 0; i < n; i++) {
b[i] = X[i] - '0'; //转数字
m = max(m, b[i]); //原串最大的数字
}
auto test = [&](ll N, ll k) { //把N转k进制
vector<ll> a(0); //向量口袋,虽是0个,但是向量可以扩大
while (N > 0) { //模拟短除(转进制)
a.push_back((N % k));
N /= k;
}
reverse(a.begin(), a.end()); //正过来
if (a.size() < X.size()) //比原串还短
return false; //滚
else if (a.size() > X.size()) //阔以
return true;
else {
int i = 0;
while (i<n&& a[i] == b[i]) { //有多少个相同的数字
++i;
}
if (i == n) //全相同
return true;
else if (a[i] < b[i]) //小于原串
return false;
else
return true;
}
};
if (n == 1) //串长度为1
cout << (b[0] <= M) ? '1': '0' ;
else if (!(test(M, m + 1)))
cout << 0 << endl;
else { //二分
ll ok = m + 1; //左
ll ng = M + 1; //右
ll mid;
while (ng - ok > 1) {
mid = (ok + ng) / 2;
if (test(M, mid))
ok = mid;
else
ng = mid;
}
cout << ok - m << endl;
return 0;
}
}
| #include <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
string X;
ull M;
bool check(ull d) {
ull val = 0;
for (auto c : X) {
if (1. * val * d > 1e18) {
return false; // too large
}
val *= d;
val += (c - '0');
}
return val <= M;
}
void solve() {
cin >> X >> M;
ull lb = 1; // ok
ull ub = M + 1; // ng
ull tapu;
if (X.size() == 1) {
ll val = stoll(X);
if (val <= M) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
return;
}
for (auto c : X) {
chmax(lb, (ull) c - '0' + 1);
}
tapu = lb;
if (!check(lb)) {
cout << 0 << endl;
return;
}
while (ub - lb > 1) {
ull md = (lb + ub) / 2;
if (check(md)) {
lb = md;
} else {
ub = md;
}
}
ull ans = ub - tapu;
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;
namespace Sakurajima_Mai{
#define ms(a) memset(a,0,sizeof(a))
#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define ce(i,r) i==r?'\n':' '
#define pb push_back
#define all(x) x.begin(),x.end()
#define gmn(a,b) a=min(a,b)
#define gmx(a,b) a=max(a,b)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int infi=1e9;//infi较大,注意涉及inf相加时爆int
const ll infl=4e18;
inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
inline int qi(){
int f=0,fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline ll ql(){
ll f=0;int fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline db qd(){
char c=getchar();int flag=1;double ans=0;
while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
if(c=='-') flag=-1,c=getchar();
while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
return ans*flag;
}
}
namespace Read{
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define rai(x,a,b) repi(i,a,b) x[i]=qi()
#define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
#define pi(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)
#define pc(x) printf("%c",x)
#define pe() puts("")
}
namespace DeBug{
#define MARK false
#define DB if(MARK)
#define pr(x) cout<<#x<<": "<<x<<endl
#define pra(x,a,b) cout<<#x<<": "<<endl; \
repi(i,a,b) cout<<x[i]<<" "; \
puts("");
#define FR(a) freopen(a,"r",stdin)
#define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
typedef long double ld;//%Lf
const db pi=acos(-1.0);
const db eps=1e-8;
inline int sgn(db x){ return fabs(x)<eps?0:(x<0?-1:1); }
struct Point{
#define Vec Point
db x,y;
Point(){}
Point(db _x,db _y){x=_x,y=_y;}
void input(){sd(x),sd(y);}
bool operator ==(Point b)const{return sgn(x-b.x)==0&&sgn(y-b.y)==0;}
Point operator +(const Point &b)const{return Point(x+b.x,y+b.y);}
Point operator -(const Point &b)const{return Point(x-b.x,y-b.y);}
Point operator /(const db b)const{return Point(x/b,y/b);}
Point operator *(const db b)const{return Point(x*b,y*b);}
db operator ^(const Point &b)const{return x*b.y-y*b.x;}//叉乘
db operator *(const Point &b)const{return x*b.x+y*b.y;}//点乘
Point transXY(Point b,db B){//绕点b 逆时针 旋转角度B(弧度制)后的坐标
db tx=x-b.x,ty=y-b.y;
return Point(tx*cos(B)-ty*sin(B)+b.x,tx*sin(B)+ty*cos(B)+b.y);
}
db dis(Point p){ return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); }//hypot(x-p.x,y-p.y)
};
int main()
{
int n=qi();
int x0=qi(),y0=qi(),xn2=qi(),yn2=qi();
Point center=Point(1.0*(x0+xn2)/2,1.0*(y0+yn2)/2);
db arc=2*pi/n;
Point res=Point(1.0*x0,1.0*y0);
res=res.transXY(center,arc);
printf("%.10lf %.10lf\n",res.x,res.y);
return 0;
} | #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cstdio>
#include <numeric>
#include <queue>
#include <list>
#include <random>
#include <iomanip>
using namespace std;
using ll=long long;
const double PI=3.14159265358979323846;
int main()
{
ll N;
double x0, xN2, y0, yN2;
cin >> N >> x0 >> y0 >> xN2 >> yN2;
double xc, yc;
xc = (x0+xN2)/2.0;
yc = (y0+yN2)/2.0;
double tx0 = x0 - xc;
double ty0 = y0 - yc;
double r = 360.0/N*(PI/180.0);
double tx1 = tx0*cos(r)-ty0*sin(r);
double ty1 = tx0*sin(r)+ty0*cos(r);
double x1 = tx1 + xc;
double y1 = ty1 + yc;
cout << std::fixed << std::setprecision(8) << x1 << " " << y1 << endl;
return 0;
} |
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
using namespace std;
// #define NDEBUG
#include<cassert>
namespace Elaina{
#define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i)
#define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i)
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define Endl putchar('\n')
#define mmset(a, b) memset(a, b, sizeof a)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T>inline T fab(T x){ return x<0? -x: x; }
template<class T>inline void getmin(T& x, const T rhs){ x=min(x, rhs); }
template<class T>inline void getmax(T& x, const T rhs){ x=max(x, rhs); }
template<class T>inline T readin(T x){
x=0; int f=0; char c;
while((c=getchar())<'0' || '9'<c) if(c=='-') f=1;
for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48));
return f? -x: x;
}
template<class T>inline void writc(T x, char s='\n'){
static int fwri_sta[1005], fwri_ed=0;
if(x<0) putchar('-'), x=-x;
do fwri_sta[++fwri_ed]=x%10, x/=10; while(x);
while(putchar(fwri_sta[fwri_ed--]^48), fwri_ed);
putchar(s);
}
}
using namespace Elaina;
const int maxn=3e5;
int a[maxn+5], n;
map<int, int>m;
signed main(){
n=readin(1);
rep(i, 1, n){
a[i]=readin(1);
++m[a[i]];
}
ll ans=0;
rep(i, 1, n){
ans+=(n-m[a[i]]);
}
writc(ans>>1);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
long n, g = 0;
cin >> n;
long a[n], b[n];
for(int i = 0; i < n; i++){
cin >> a[i];
g += a[i];
}
for(int i = 0; i < n; i++){
cin >> b[i];
g += b[i];
}
priority_queue<long, vector<long>, greater<long>> q;
for(int i = 0; i < n; i++){
q.push(a[n - 1 - i]);
q.push(b[i]);
g -= q.top();
q.pop();
}
cout << g << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
unordered_map<int,bool> x;
int count = 0;
for (long long i = 2; i <= 100000 && i <= n; i++) {
if (x.count(i) > 0) {
continue;
}
long long t = i * i;
while (t <= n) {
if (t <= 100000) x[t] = true;
count++;
t *= i;
}
}
cout << (n - count) << endl;
return 0;
} | #include<iostream>
using namespace std;
int main()
{
int N,p;
cin>>N;
if(1<=N && 300>=N)
{
p=N*1.08;
if(p<206)
{
cout<<"Yay!";
}
else if(p==206)
{
cout<<"so-so";
}
if(p>206)
{
cout<<":(" ;
}
}
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
string s;
cin >> s;
if(n<3) cout<<n;
else{
int temp=n;
stack<char> st;
for(int i=0; i<n; i++){
if(s[i]=='x'){
char c1='.',c2='.';
if(!st.empty()){
c1 = st.top();st.pop();
}
if(!st.empty()){
c2 = st.top();st.pop();
}
// debug(i,c1,c2);
if(c1=='o' && c2=='f'){
temp-=3;
}else{
st.push(c2);
st.push(c1);
st.push('x');
}
}else{
st.push(s[i]);
}
}
cout<<temp;
}
return 0;
} | #include<bits/stdc++.h>
#define int long long
using namespace std;
stack<char> st;
string s;
int n;
signed main(){
cin>>n>>s;
for(int i=0;i<n;i++){
st.push(s[i]);
if(st.size()<3) continue;
string cur="";
for(int j=0;j<3;j++) cur+=st.top(),st.pop();
//cout<<cur<<"\n";
if(cur!="xof") st.push(cur[2]),st.push(cur[1]),st.push(cur[0]);
}
cout<<st.size();
return 0;
} |
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<queue>
#define int long long
#define uc getchar()
#define ph putchar
#define R register
#define CN const
#define rint R int
#define cint CN int
#define ps ph(' ')
#define pe ph('\n')
using namespace std;
cint inf=998244353;
cint mix=1e6+5;
inline int in();
inline void out(rint x);
int n=in(),m=in();
int fa[mix],ans,wer;
bool vis[mix],cis[mix];
inline int gf(rint x){
return fa[x]!=x?fa[x]=gf(fa[x]):x;
}
inline void merge(rint x,rint y){
x=gf(x),y=gf(y);
if(x!=y)fa[x]=y;
}
#undef int
int main() {
#define int long long
R char c;
for(rint i=m+n;i;--i)fa[i]=i;
merge(1,n);
merge(1,n+1);
merge(1,n+m);
for(rint i=1;i<=n;++i)
for(rint j=1;j<=m;++j){
while((c=uc)<'#');
if(c<'.')merge(i,n+j);
} for(rint i=1;i<=n;++i)
if(!vis[gf(i)])vis[gf(i)]=1,++ans;
for(rint i=n+m;n<i;--i)
if(!cis[gf(i)])cis[gf(i)]=1,++wer;
out(min(ans,wer)-1),pe;
}
inline int in() {
rint x=0;
R bool f=0;
R char s=uc;
while((s<'0'||'9'<s)&&s!='-')s=uc;
if(s=='-')f=1,s=uc;
while('0'<=s&&s<='9')(x*=10)+=s^48,s=uc;
return f?-x:x;
}
inline void out(rint x) {
if(x<0)ph('-'),x=-x;
rint y=x/10;
if(y)out(y);
ph((x-y*10)^48);
} | #include <bits/stdc++.h>
typedef long long ll;
#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 sp(n) cout << fixed << setprecision(n)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
const int MAX_N = 2e4+10;
int par[MAX_N];//親
int pank[MAX_N];//木の深さ
//n要素で初期化
void init(int n){
for(int i=0;i<n;i++){
par[i]=i;
pank[i]=0;
}
}
//木の根を求める
int find(int x){
if(par[x]==x){
return x;
}else{
return par[x]=find(par[x]);
}
}
//xとyの属する集合を併合
void unite(int x,int y){
x = find(x);
y = find(y);
if(x==y)return;
if(pank[x]<pank[y]){
par[x]=y;
}else{
par[y]=x;
if(pank[x]==pank[y])pank[x]++;
}
}
//xとyが同じ集合に属するか否か
bool same(int x,int y){
return find(x)==find(y);
}
int main(void){
ll h,w;cin>>h>>w;
vector<vector<char>> s(h,vector<char>(w));
rep(i,h)rep(j,w)cin>>s[i][j];
s[0][0]=s[0][w-1]=s[h-1][0]=s[h-1][w-1]='#';
init(h+w);
rep(i,h)rep(j,w)if(s[i][j]=='#')unite(i,h+j);
map<ll,ll> mp1,mp2;
rep(i,h)mp1[find(i)]++;
rep(i,w)mp2[find(h+i)]++;
ll res=min((ll)mp1.size()-1,(ll)mp2.size()-1);
cout<<res<<endl;
}
|
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <bitset> //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip> //do setprecision
#include <ctime>
#include <complex>
using namespace std;
#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double
#define pii pair<int,int>
#define pll pair<LL,LL>
#define vi vector<int>
#define vl vector<LL>
#define vii vector<vi>
#define vll vector<vl>
const double pi = 3.14159265358979323846264;
const int mod=1000000007;
//mod演算ライブラリ
//mod=1000000007
LL add(LL a,LL b){
return (a+b)%mod;
}
LL sub(LL a,LL b){
return (a+mod-b)%mod;
}
LL mul(LL a,LL b){
return ((a % mod) * (b % mod)) % mod;
}
LL power(LL x,LL y){//繰り返し二乗法 単品利用可
if(y==0)return 1;
else if(y==1)return x%mod;
else if(y%2==0){
LL tmp=power(x, y/2) % mod;
return tmp*tmp%mod;
}else {
LL tmp=power(x, y/2) % mod;
return ((tmp*tmp)%mod)*x%mod;
}
}
LL dvs(LL a,LL b){
return mul(a,power(b,mod-2));
}
const int SIZE_mod=200005;
int invfact[SIZE_mod];
int fact[SIZE_mod];
void factset(int a){//aまでの階乗%mod表を作成
fact[0]=1;
FORQ(i,1,a){
fact[i]=mul(fact[i-1],i);
}
}
void cmbset(int a){//1~aまでの階乗と階乗数の逆元のmod(mod)を用意 O(a)
factset(a);
invfact[a]=power(fact[a],mod-2);
invfact[0]=1;
FORD(i,a,1){
invfact[i]=mul(invfact[i+1],i+1);
}
}
LL cmb(int n,int r){//nCr%mod (cmbset後に使用)
if(n<r||r<0||n<0)return 0;
return mul(fact[n],mul(invfact[r],invfact[n-r]));
}
LL prm(int n,int r){//nPr%mod
return mul(cmb(n,r),fact[n]);
}
LL homo(int n,int r){//nHr%mod
return cmb(n+r-1,r);
}
int main(){
int tt;
cin>>tt;
FOR(ti,0,tt){
LL n,a,b;
cin>>n>>a>>b;
if(b>a)swap(a,b);
LL all=mul(mul(n-a+1,n-a+1),mul(n-b+1,n-b+1));
LL ta=n-a+1,tb=n-b+1;
LL tt1=max(n-a-b+1,0LL);
LL tt2=sub(mul(ta,tb),mul((tt1+1),tt1));
LL rate=dvs(tt2,mul(ta,tb));
LL t=mul(all,mul(rate,rate));
cerr<<tt1<<" "<<tt2<<" "<<rate<<" "<<t<<endl;
cout<<sub(all,t)<<endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int64_t Q=1000000007;
int main(){
int T;
cin>>T;
for(int l=0;l<T;l++){
int64_t N,A,B;
cin>>N>>A>>B;
int64_t L=B-1;
if(A+B>N){
cout<<"0"<<endl;
continue;
}
int64_t X=(N-A+1)*(N-A+1),Y=(N-B+1)*(N-B+1),W=0,V=4,U=0,S=0;
X%=Q;
Y%=Q;
int64_t Z=X*Y;
Z%=Q;
Y=(A+B-1)*(A+B-1);
Y%=Q;
Y*=X;
Y%=Q;
Y-=((((N-A+1)*(((2*(A+B-1))%Q))%Q)*((B*(B-1))%Q))%Q)%Q;
Y%=Q;
Y+=Q;
Y%=Q;
W+=Y;
U=L*B;
U%=Q;
W+=((U*U)%Q);
Z+=(Q+Q+Q-W);
Z%=Q;
cout<<Z<<endl;
}
} |
#include <iostream>
#include <vector>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> xs, ys;
for (int i = 0; i < n; ++i) {
char c;
cin >> c;
if (c == '0') xs.push_back(i);
}
for (int i = 0; i < n; ++i) {
char c;
cin >> c;
if (c == '0') ys.push_back(i);
}
if (xs.size() != ys.size()) {
cout << "-1\n";
return;
}
int ans = 0;
for (int i = 0; i < (int)xs.size(); ++i) {
if (xs[i] != ys[i]) ++ans;
}
cout << ans << "\n";
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
solve();
return 0;
}
| #include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define all( v ) begin( v ), end( v )
#define rep( i, j ) for ( int i = 0; i < (int)j; ++i )
#define rep2( i, j, k ) for ( int i = j; i <= (int)k; ++i )
#define rep3( i, j, k ) for ( int i = j; i >= (int)k; --i )
template < class S, class T > inline bool chmax( S& x, T y ) {
if ( x < y ) {
x = y;
return true;
}
return false;
}
template < class S, class T > inline bool chmin( S& x, T y ) {
if ( x > y ) {
x = y;
return true;
}
return false;
}
template < class T > struct edge {
int from, to;
T cost;
edge( int to = 0 ) : from( -1 ), to( to ), cost( 0 ) { };
edge( int to, T cost ) : from( -1 ), to( to ), cost( cost ) { }
edge( int from, int to, T cost ) : from( from ), to( to ), cost( cost ) { }
edge& operator=( const int& x ) {
to = x;
return *this;
}
};
template < class T > using pq = priority_queue< T >;
template < class T > using pqg = priority_queue< T, vector< T >, greater< T > >;
template < class T > using Edges = vector< edge< T > >;
template < class T > using Graph = vector< Edges< T > >;
using ld = long double;
using ll = long long;
using pi = pair< int, int >;
using pl = pair< ll, ll >;
using vi = vector< int >;
using vl = vector< ll >;
using vpi = vector< pi >;
using vpl = vector< pl >;
using vvi = vector< vi >;
using vvl = vector< vl >;
const ll INFLL = 100000000000000000;
const ll INF = 1000000000;
const ll MAX = 500005;
const ll MOD = 1000000007;
// const ll MOD = 998244353;
int main( void ) {
ios::sync_with_stdio( 0 );
cin.tie( 0 );
cout << fixed << setprecision( 15 );
int n;
cin >> n;
string s, t;
cin >> s >> t;
ll x = count( all( s ), '0' ), y = count( all( t ), '0' );
if ( x != y ) {
cout << -1 << endl;
return ( 0 );
}
ll ans = x;
int a = 0, b = 0;
rep( i, n ) {
if ( s[i] == '0' ) ++a;
if ( t[i] == '0' ) ++b;
if ( s[i] == t[i] && s[i] == '0' && a == b ) --ans;
}
cout << ans << endl;
return ( 0 );
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.