code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
template<typename T>void ckmn(T&a,T b){a=min(a,b);}
template<typename T>void ckmx(T&a,T b){a=max(a,b);}
void rd(int&x){scanf("%i",&x);}
void rd(ll&x){scanf("%lld",&x);}
void rd(char*x){scanf("%s",x);}
void rd(ldb&x){scanf("%lf",&x);}
void rd(string&x){scanf("%s",&x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);}
template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);}
template<typename T>void rd(){T x;rd(x);return x;}
int ri(){int x;rd(x);return x;}
template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;}
template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);}
template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);}
template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);}
void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}}
template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}}
const int mod=998244353;
int add(int a,int b){a+=b;return a>=mod?a-mod:a;}
void ckadd(int&a,int b){a=add(a,b);}
int sub(int a,int b){a-=b;return a<0?a+mod:a;}
void cksub(int&a,int b){a=sub(a,b);}
int mul(int a,int b){return (ll)a*b%mod;}
void ckmul(int&a,int b){a=mul(a,b);}
int powmod(int x,int k){int ans=1;for(;k;k>>=1,ckmul(x,x))if(k&1)ckmul(ans,x);return ans;}
int inv(int x){return powmod(x,mod-2);}
const int N=1000050;
int F[N],I[N];
int binom(int n, int k){ return mul(F[n],mul(I[k],I[n-k]));}
void calc()
{
F[0]=1;
for(int i=1;i<N;i++) F[i]=mul(F[i-1],i);
I[N-1]=inv(F[N-1]);
for(int i=N-2;~i;i--) I[i]=mul(I[i+1],i+1);
}
int go[N];
int main(){
calc();
int n,m;
rd(n,m);
for(int i=2;i<=m;i++){
if(!go[i]){
for(int j=i;j<=m;j+=i)go[j]=i;
}
}
int ans=0;
for(int last=1;last<=m;last++){
vector<pii> fact;
for(int i=last;i>1;i/=go[i]){
if(fact.empty()||fact.back().first!=go[i]){
fact.pb({go[i],1});
}else fact.back().second++;
}
int sol=1;
for(auto it:fact){
ckmul(sol,binom(it.second+n-1,it.second));
}
ckadd(ans,sol);
}
printf("%i\n",ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
int main() {
const int mod = 998244353, i2 = 499122177;
int n, k;
scanf("%d %d", &n, &k);
vector <int> a(n);
rep(i, n) scanf("%d", &a[i]);
vector <int> sum(k + 1), sum2(k + 1);
rep(i, n) {
int e = 1, e2 = 1;
rep(j, k + 1) {
sum[j] += e;
if (sum[j] >= mod) sum[j] -= mod;
sum2[j] += e2;
if (sum2[j] >= mod) sum2[j] -= mod;
e = 1LL * e * a[i] % mod;
e2 = 1LL * e2 * (a[i] * 2) % mod;
}
}
vector <vector <int> > C(k + 1, vector <int> (k + 1));
rep(i, k + 1) rep(j, i + 1) C[i][j] = j ? (C[i - 1][j - 1] + C[i - 1][j]) % mod : 1;
vector <int> ans(k + 1);
for (int x = 1; x <= k; ++ x) {
for (int y = 0; y <= x; ++ y)
(ans[x] += 1LL * C[x][y] * sum[y] % mod * sum[x - y] % mod) %= mod;
(ans[x] += mod - sum2[x]) %= mod;
ans[x] = 1LL * ans[x] * i2 % mod;
printf("%d\n", ans[x]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repx(i, x, n) for (ll i = (ll)(x); i < (ll)(n); i++)
#define repr(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define reprx(i, x, n) for (ll i = ((ll)(n)-1); i >= (ll)(x); i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((ll)(x).size())
#define submit(x) {cout<<x<<endl; return 0;}
#define X first
#define Y second
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T>bool chmax(T &a,T b) {if (a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a,T b) {if (b<a) {a=b; return 1;} return 0;}
template<class T>long long ip(T a, T b) {bitset<60> s(b); long long val=1; for(int i=0;i<60;i++){if(s.test(i))val*=a; a*=a;} return val;}
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 60;
const double PI = 3.1415926535897932;
int main() {
double x,y,z;
cin>>x>>y>>z;
double price = y / x;
double ans = z * price;
ll ans2 = ans;
if (ans2 - ans == 0) ans2--;
submit(ans2)
}
| #include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <limits.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <string>
#include <string.h>
#include <sstream>
#include <ctime>
using namespace std;
#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll,ll>
int x, y, z;
void solve()
{
cin >> x >> y >> z;
for(int i = 0;; i++)
{
if(x*i >= z*y)
{
cout << i - 1 << "\n";
return;
}
}
}
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
#ifdef localProject
freopen("in.txt", "r", stdin);
#endif
solve();
return 0;
} |
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (long long i = m; i < (n); ++i)
//#define int long long
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int,int>,int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using Tiib = tuple<int, int, bool>;
const int INF = 1001001007;
const long long mod = 1000000007LL;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT>
void printvec(vector<AT> &ar){
rep(i,ar.size()-1) cout << ar[i] << " ";
cout << ar[ar.size()-1] << endl;
}
template <typename Q>
void printvvec(vector<vector<Q>> &ar){
rep(i,ar.size()){
rep(j,ar[0].size()-1) cout << ar[i][j] << " ";
cout << ar[i][ar[0].size()-1] << endl;
}
}
template <typename S>
bool range(S a, S b, S x){
return (a <= x && x < b);
}
void yes(){
cout << "Yes" << endl;
}
void no (){
cout << "No" << endl;
}
ll cel (ll a, ll b){
if (a % b == 0) return a / b;
else return a / b + 1;
}
ll gcds(ll a, ll b){
ll c = a % b;
while (c != 0){
a = b;
b = c;
c = a % b;
}
return b;
}
int main(){
int n; cin >> n;
ld a = 0.0;
ld b = 0.0;
ld c = 0.0;
rep(i,n){
ld k; cin >> k;
a += abs(k);
b += k * k;
c = max(c,abs(k));
}
cout << setprecision(20);
cout << a << endl;
cout << sqrt(b) << endl;
cout << c << endl;
} | #include <string>
#include <math.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> n;
long long a = 0;
long long b = 0;
long long c = 0;
long long x;
for (long i = 0; i < n; i++) {
cin >> x;
if (x < 0)
x = x * (-1);
a = a + x;
b = b + x * x;
if (c < x)
c = x;
}
long double bb =sqrt(b);
cout << a << '\n' << fixed << setprecision(15) << bb << '\n';
cout << c << '\n';
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int &it : a) {
cin >> it;
it %= 200;
}
vector<int> p(200, -1);
if (n > 8) n = 8;
for (int m = 1; m < (1 << n); m++) {
int sum = 0;
for (int i = 0; i < n; i++) {
if ((m >> i) % 2 == 1) {
sum += a[i];
}
}
sum %= 200;
if (p[sum] != -1) {
cout << "Yes\n";
cout << __builtin_popcount(p[sum]);
for (int i = 0; i < n; i++) {
if ((p[sum] >> i) % 2 == 1) {
cout << ' ' << i + 1;
}
}
cout << '\n';
cout << __builtin_popcount(m);
for (int i = 0; i < n; i++) {
if ((m >> i) % 2 == 1) {
cout << ' ' << i + 1;
}
}
cout << '\n';
return 0;
} else {
p[sum] = m;
}
}
cout << "No\n";
}
| #include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 200010, mod = 1e9 + 7;
ll dp[N][20], k, num[N];
char s[N];
ll dfs(int pos, bool limit, bool lead, int cnt, ll state, int now){
if(!pos){
//printf("now = %d cnt = %d state = %lld\n", now, cnt, state);
return cnt == k;
}
if(cnt > k) return 0;
if(!limit && !lead && ~dp[pos][cnt]) return dp[pos][cnt];
ll ans = 0;
int up = limit ? num[pos] : 15;
//printf("up = %d\n", up);
for(int i = 0; i <= up; ++ i){
if(lead && !i) ans = (ans + dfs(pos - 1, limit && i == up, true, 0, 0, 0)) % mod;
else{
//printf("pos = %d i = %d a = %d st = %d st | a = %d\n", pos, i, 1 << i, state, state | (1 << i));
//printf("State = %lld x = %d\n", state, state || (1 << i));
if((state >> i) & 1) ans = (ans + dfs(pos - 1, limit && i == up, false, cnt, state, now * 16 + i)) % mod;
else ans = (ans + dfs(pos - 1, limit && i == up, false, cnt + 1, state | (1 << i), now * 16 + i)) % mod;
}
}
if(!limit && !lead) dp[pos][cnt] = ans;
return ans;
}
ll solve(){
int len = strlen(s + 1);
for(int i = 1, j = len; i < j; ++ i, -- j) swap(s[i], s[j]);
for(int i = 1; i <= len; ++ i){
if(s[i] >= '0' && s[i] <= '9') num[i] = s[i] - '0';
else num[i] = s[i] - 'A' + 10;
}
// for(int i = 1; i <= len; ++ i) printf("%d ", num[i]);
// exit(0);
return dfs(len, true, true, 0, 0, 0);
}
int main(){
scanf("%s", s + 1);
cin >> k;
memset(dp, -1, sizeof dp);
cout << solve();
return 0;
} |
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define endl "\n"
#define mp make_pair
#define HCF __gcd
#define LCM(m,n) (m*n)/__gcd(m,n)
#define maxa(v) *(max_element(all(v))
#define mina(v) *(min_element(all(v))
#define vi vector<ll>
#define pll pair<ll,ll>
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define M 998244353ll
#define S second
#define F first
#define inf (int)1e9
#define fon(i,j,n) for(ll i=j;i<(n);++i)
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define asort(v) sort(v.begin(),v.end())
#define dsort(v) sort(v.begin(),v.end(),greater<int>())
#define BS(v,k) binary_search(v.begin(),v.end(),k)
#define UB(v,k) (upper_bound(v.begin(),v.end(),k)-v.begin())
#define LB(v,k) (lower_bound(v.begin(),v.end(),k)-v.begin())
#define mod 1000000007ll
using namespace std;
bool prime(ll n){bool b=true;
for(ll i=2;i<=sqrt(n);i++){if(n%i==0){b=false;break;
}
}
if(b)return true;
else return false;
}
void solve(){
ll n;cin>>n;
map<ll,ll>m;set<ll>s;
fon(i,0,n){ll x;cin>>x;s.insert(x);m[x]++;}
vi sum;
sum.pb(0);
ll k=1;
for(auto i:s) { sum.pb(sum[k - 1] + m[i]);k++; }
k=sum.size();
ll ans=0;
fon(i,1,k){ans+=(sum[i]-sum[i-1])*(sum[k-1]-sum[i]);}
cout<<ans;
return;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int tsc=1;
while(tsc--){
solve();
cout<<endl; }
return 0;
} | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using LL=long long;
using vL=vector<LL>;
using vvL=vector<vL>;
int main() {
long long n;
cin>>n;
long long a;
map<long long ,long long > Map;
for (int i=0 ; i<n ; i++) {
cin>>a;
Map[a]++;
}
long long ans=n*(n-1)/2;
for (auto k : Map) {
long long m=k.second;
ans-=m*(m-1)/2;
}
cout<<ans<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1000000007;
template <class T>
struct fenwick_tree {
vector<T> x;
fenwick_tree(int n) : x(n, 0) {}
T sum(int i, int j) {
if (i == 0) {
T S = 0;
for (j; j >= 0; j = (j & (j + 1)) - 1) S += x[j];
return S;
} else
return sum(0, j) - sum(0, i - 1);
}
void add(int k, T a) {
for (; k < x.size(); k |= k + 1) x[k] += a;
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> A(2 * N);
int res = 0;
for (int i = 0; i < 2 * N; i++) {
cin >> A[i];
}
vector<int> B;
for (int i = 0; i < N; i++) {
B.push_back(min(A[N - 1 - i], A[i + N]));
B.push_back(max(A[N - 1 - i], A[i + N]));
}
int sum = 0;
priority_queue<int> pq;
for (int i = 1; i < 2 * N; i++) {
if (i % 2 == 0) {
int a = -pq.top();
if (a < B[i]) {
res += B[i] - a;
pq.pop();
pq.push(-B[i]);
}
} else {
res += B[i];
pq.push(-B[i]);
}
}
// fenwick_tree<int> ft(2 * N);
// for (int i = 0; i < 3 * N; i++) {
// ft.add(i, 1);
// }
// vector<pair<int, int> > vp;
// for (int i = 0; i < 2 * N; i++) {
// vp.emplace_back(B[i], i);
// }
// sort(B.rbegin(), B.rend());
// for (int i = 0; i < 2 * N; i++) {
// //cerr << vp[i].first << " " << vp[i].second << endl;
// int p = vp[i].second;
// if (ft.sum(0, p) - 2 >= 0) {
// ft.add(p, -2);
// res += vp[i].first;
// }
// }
cout << res << endl;
} | #include<iostream>
#include<algorithm>
#include<numeric>
#include<queue>
#include<vector>
using namespace std;
const int BUF = 400005;
int nVal;
int val[BUF];
void read() {
cin >> nVal;
nVal *= 2;
for (int i = 0; i < nVal; ++i) {
cin >> val[i];
}
}
void work() {
long long sum = accumulate(val, val + nVal, 0LL);
priority_queue<int, vector<int>, greater<int> > Q;
for (int i = 0, L = nVal / 2 - 1, R = nVal / 2; i < nVal; i += 2, --L, ++R) {
Q.push(val[L]);
Q.push(val[R]);
sum -= Q.top();
Q.pop();
}
cout << sum << endl;
}
int main() {
read();
work();
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0, length = n; i < length; 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 ci4(w, x, y, z) cin >> w >> 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, ll> 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 = 2e5, MOD = 1e9 + 7, INF = 1e9;
int n, m, q, t[MAX_N], x[MAX_N], y[MAX_N], memo[MAX_N][2];
map<int, int> ma;
vector<ll> ans;
P bit[MAX_N + 2][2];
P sum(int i, int j) {
P s = P(0, 0);
while (i > 0) {
s.fi += bit[i][j].fi;
s.se += bit[i][j].se;
i -= i & -i;
}
return s;
}
void add(int i, int j, int x, int y) {
while (i <= q + 1) {
bit[i][j].fi += x;
bit[i][j].se += y;
i += i & -i;
}
}
int main() {
ci3(n, m, q);
rep(i, q) {
ci3(t[i], x[i], y[i]);
ma.ep(y[i], 0);
}
ma.ep(0, 0);
int cnt = 0;
for (auto &i: ma) i.se = ++cnt;
ans.epb(0);
rep(i, n) add(1, 0, 1, 0);
rep(i, m) add(1, 1, 1, 0);
rep(i, q) {
int old_y = memo[x[i] - 1][t[i] - 1];
add(ma[y[i]], t[i] - 1, 1, y[i]);
add(ma[old_y], t[i] - 1, -1, -old_y);
P p1 = sum(ma[y[i]] - t[i] % 2, t[i] % 2);
P p2 = sum(ma[old_y] - t[i] % 2, t[i] % 2);
ll tmp = ans.back();
tmp += scll(p1.fi) * y[i] - scll(p2.fi) * old_y;
tmp += p2.se - p1.se;
ans.epb(tmp);
memo[x[i] - 1][t[i] - 1] = y[i];
}
rep(i, q) co(ans[i + 1]);
return 0;
}
| #include<bits/stdc++.h>
#include<bits/extc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t){
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr<<#x<<": "; for(auto i:x) cerr<<i<<" "; cerr<<endl;}}
#define debugr(l,r,x) {{cerr<<#x<<": "; for(int i=l;i<=r;i++) cerr<<x<<" "; cerr<<endl;}}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define debugr(l,r,x)
#define cerr while(0) cerr
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef __int128 INT;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
struct Node{
typedef ll T;
static constexpr T unset = INT_MIN, unit=0;
T f(T a, T b) { return max(a, b); }
T mset=unset, madd=0, val=unit; int lo, hi;
Node *l=0, *r=0;
Node(int lo, int hi): lo(lo), hi(hi){
if(lo<hi){
int mid=(lo+hi)/2;
l=new Node(lo, mid); r=new Node(mid+1, hi);
}
}
T query(int L, int R){
push();
if(R<lo || hi<L) return unit; //sentinel
if(L<=lo && hi<=R) return val;
return f(l->query(L, R), r->query(L, R));
}
void set(int L, int R, T x, int t=1){
push();
if(R<lo || hi<L) return;
if(L<=lo && hi<=R){
(t==1 ? mset : madd)=x;
push(); return;
}
l->set(L, R, x, t), r->set(L, R, x, t);
val=f(l->val, r->val);
}
void add(int L, int R, T x) { return set(L, R, x, 0); }
void push(){
mset!=unset ? val=mset : val+=madd; //multiply (hi-lo+1) for sum queries
if(l){ //if parent, push down
if(mset!=unset){
l->mset=r->mset=mset;
l->madd=r->madd=0;
}
else if(madd){
l->mset!=unset ? l->mset+=madd : l->madd+=madd;
r->mset!=unset ? r->mset+=madd : r->madd+=madd;
}
}
mset=unset, madd=0;
}
};
map<int, int> mp;
ordered_set os[3];
ll a[3][200006];
ll cnt[3];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, q;
cin>>n>>m>>q;
vector<pair<int, pii>> queries;
set<int> s;
s.insert(0);
for(int i=1; i<=q; i++){
int t, x, y;
cin>>t>>x>>y;
queries.push_back({t, {x, y}});
s.insert(y);
}
int cur=1;
for(int i: s) mp[i]=cur++;
ll ans = 0;
Node **tree = new Node*[3];
for(int i=1;i<3;i++) tree[i] = new Node(0, 200005);
auto sum = [&](int t, ll v){
ll ans = cnt[t];
ans -= tree[t]->query(mp[v], mp[v]);
ans += os[t].order_of_key(v+1) * v;
return ans;
};
for(int i=0; i<n; i++) os[1].insert(0);
for(int j=0; j<m; j++) os[2].insert(0);
//debugv(os[1]);
//debugv(os[2]);
for(auto [t, p]: queries){
auto [x, y] = p;
ans += sum(3-t, y) - sum(3-t, a[t][x]);
cout<<ans<<'\n';
cnt[t]+=y-a[t][x];
tree[t]->add(mp[y], 200004, y);
tree[t]->add(mp[a[t][x]], 200004, -a[t][x]);
os[t].erase(os[t].lower_bound(a[t][x]-1));
os[t].insert(y);
a[t][x] = y;
debugv(os[1]);
debugv(os[2]);
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, b) for (ll i = a; i < b; ++i)
#define rep_down(i, a, b) for (ll i = a; i > b; --i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
constexpr ll INF = (1ll << 60);
constexpr ll mod = 1000000007;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; }
int main(){
ll n,a[100001],b[100001],sum=0;
cin>>n;
rep(i,n){
cin>>a[i];
}
rep(i,n){
cin>>b[i];
}
rep(i,n){
sum+=a[i]*b[i];
}
if(sum==0){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define test int t;cin>>t;while(t--)
#define REP(i,n) for (int i = 1; i <= n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define ll long long
#define ld long double
#define int ll
#define pb push_back
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ff first
#define ss second
#define INF 1000000000
#define HINF 1000000000000000
#define mem(a,b) memset(a,b,sizeof(a))
#define arrin(x,n) int x[n]; for(int o=0;o<n;o++) cin>>x[o]
#define arrout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define vecin(x,n); vi x;int hool; for(int o=0;o<n;o++) {cin>>hool; x.pb(hool);}
#define vecout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define all(x) x.begin(),x.end()
#define deb(x) cout<<#x<<'='<<x<<endl
#define deb2(x,y) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<endl
#define deb3(x,y,z) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<" "<<#z<<'='<<z<<endl
#define debarr(arr,n) for(int o=0;o<n;o++) deb2(o,arr[o])
#define debarrall(arr) debarr(arr,arr.size())
#define IO freopen("input1.txt", "r", stdin); freopen("output1.txt", "w+", stdout)
const double pi = 3.14159265358979323846;
int powersimple(int a, int b){//a^b
int res=1;
while(b>0){
if(b&1)
{res=(res*a);
b--;}
a=(a*a);
b>>=1;
}
return res;
}
int ncr(int n,int k)
{
int ans=1;
if(k>n-k)
k=n-k;
for(int i=1;i<=k;i++)
ans*=(n-i+1),ans/=i;
return ans;
}
int power(int x,int y,int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
int ncrModPFermat(int n,int r, int p)
{
if (r == 0)
return 1;
int fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
///////////////END OF TEMPLATE//////////////////
void solve()
{
int i,j,a,b,ca=0,cb=0;
cin>>a>>b;
while(a>0)
{
ca+=a%10;
a/=10;
}
while(b>0)
{
cb+=b%10;
b/=10;
}
cout<<max(ca,cb);
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w+", stdout);
#else
// online submission
#endif
fast
//IO;
//cout<<fixed<<setprecision(15);
//test
{
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define FOR(i,a,b) for(int i=a;i<b;++i)
#define RFOR(i,a,b) for(int i=a;i>=b;--i)
#define init(a, x) memset(a,x,sizeof(a))
#define ld long double
#define dd double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(),a.end()
#define sz(a) (ll)(a.size())
typedef long long ll;
typedef unsigned long long ull;
typedef pair <ll, ll> pll;
typedef pair <int, int> pii;
typedef vector<pll> vplll;
ll mod = 1e9+7;
const ld PI = 2 * acos(0.0);
const vector<ll> dx = {1, -1, 0, 0};
const vector<ll> dy = {0, 0, 1, -1};
#define round(x, y) ((x + y - 1) / y)
#define ce(x, y) ((x + y - 1) / y)
#define amax(x, y) \
if (y > x) \
x = y;
#define amin(x, y) \
if (y < x) \
x = y;
#define lcm(x, y) ((x) * (y) / __gcd(x, y))
#define len(x) (ll) x.length()
#define sq(x) ((x) * (x))
#define cb(x) ((x) * (x) * (x))
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll a,b,c,t,n,m,k,q;
t = 1;
// cin>>t;
while(t--)
{
cin>>a>>b>>n>>m;;
string s[a];
for(int i=0;i<a;i++)
cin>>s[i];
ll ans =1;
int cnt = 0;
for(int i=m;i<b;i++)
{
if(s[n-1][i]=='.')
cnt++;
else
break;
}
// ans += (cnt*(cnt-1))/2;
ans += cnt;
cnt =0;
for(int i=m-2;i>=0;i--)
{
if(s[n-1][i]=='.')
cnt++;
else
break;
}
ans += cnt;
cnt = 0;
for(int i=n;i<a;i++)
{
if(s[i][m-1]=='.')
cnt++;
else
break;
}
ans += cnt;
cnt =0;
for(int i=n-2;i>=0;i--)
{
if(s[i][m-1]=='.')
cnt++;
else
break;
}
ans += cnt;
// ans += (cnt*(cnt-1))/2;
cout<<ans<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
using pint = pair<ll, ll>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int main() {
int h, w, x, y;
cin >> h >> w >> x >> y;
vector<string> s(h);
rep(i, 0, h) cin >> s[i];
int cnt = 0;
rep(i, x - 1, h) {
if (s[i][y - 1] == '#') break;
cnt++;
}
per(i, 0, x - 1) {
if (s[i][y - 1] == '#') break;
cnt++;
}
rep(i, y, w) {
if (s[x - 1][i] == '#') break;
cnt++;
}
per(i, 0, y - 1) {
if (s[x - 1][i] == '#') break;
cnt++;
}
cout << cnt << "\n";
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for (int i = (m); i < (int)(n); i++)
using ll = long long;
using vl = vector<ll>;
using P = pair<ll, int>;
ll solve(vl& A, map<P, ll>& m, ll x, int i) {
if (i == int(A.size()) - 1) return 1;
if (m.count(P(x, i))) return m[P(x, i)];
ll d = x % A[i + 1];
ll ret = solve(A, m, x - d, i + 1);
if (d > 0) ret += solve(A, m, x + (A[i + 1] - d), i + 1);
return m[P(x, i)] = ret;
}
int main() {
// input
ll N, X;
cin >> N >> X;
vl A(N);
rep(i, N) cin >> A[i];
// solve
map<P, ll> m;
ll ans = solve(A, m, X, 0);
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<(n); ++i)
#define rep1(i,n) for(ll i=1; i<=(n); ++i)
#define revrep(i,n) for(ll i=(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 62) -1;
#define fastio cin.tie(nullptr); ios_base::sync_with_stdio(false);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
template <class T> bool updmax(T& a, T b) { if (b > a) { a = b; return true;} return false;}
template <class T> bool updmin(T& a, T b) { if (b < a) { a = b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
ll needs[200005];
int main() {
fastio;
ll ans=1;
ll N,W;
cin >> N >> W;
rep(i,N) {
ll s,t,p;
cin >> s >> t >> p;
needs[s] += p;
needs[t] -= p;
}
rep(i,200005-1) {
needs[i+1] += needs[i];
if (needs[i] > W)
ans = 0;
}
cout << YN(ans) << endl;
}
|
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
FAST/**/
ll n;
cin>>n;
vector<ll> divi[n+1];
for(ll i=1;i<n;i++)
for(ll j=2*i;j<=n;j+=i)
divi[j].pb(i);
ll val[n+1];
memset(val,0,sizeof(val));
set<ll, greater<ll>> st;
for(ll i=1;i<=n;i++)
st.insert(i);
while(st.size()>0)
{
auto curr = *st.begin();
st.erase(st.begin());
autoit(divi[curr], it)
{
if(val[*it] > val[curr]-1)
val[*it] = val[curr]-1;
}
}
ll maxi = 0;
for(ll i=1;i<=n;i++)
maxi = min(maxi, val[i]);
maxi--;
for(ll i=1;i<=n;i++)
val[i]-=maxi;
for(ll i=1;i<=n;i++)
cout<<val[i]<<" ";
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define f(i,a,b) for(ll i=a; i<b; i++)
#define fr(i,a,b) for(ll i=a; i>=b; i--)
typedef vector<int> vi;
typedef long long int ll;
typedef vector<ll> vll;
#define fastio std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PI 3.1415926535
ll Mod = 1e9+7;
// ll Mod = 998244353;
// ll mult(ll a, ll b) {
// ll ret = 1;
// ret = ((a%Mod) * (b%Mod))%Mod;
// return ret;
// }
// ll power(ll a, ll b = Mod-2) {
// if(b == 0) return 1;
// ll ret = 1;
// while(b) {
// if(b&1) {
// ret = mult(ret,a);
// }
// a = mult(a,a);
// b>>=1;
// }
// return ret;
// }
// ll sub(ll a, ll b) {
// ll ret = (a%Mod - b%Mod + Mod)%Mod;
// return ret;
// }
// void seive(vector<bool> &status) {
// for(ll i = 2; i<=1e6; i++) {
// if(status[i]) {
// for(ll j =i*i; j<=1e6; j+=i) status[j] = 0;
// }
// }
// }
// int getmex(vi& occr, int n, int mex) {
// for(int i = mex; i<n; i++) {
// if(occr[i] == 0) return i;
// }
// return n;
// }
// void swap(int &a, int &b) {
// a = a + b;
// b = a - b;
// a = a - b;
// }
void solve_case() {
int n;
cin>>n;
f(i,1,n+1) {
fr(j,31,0) {
if(i&(1<<j)) {
cout<<j+1<<' ';
break;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
fastio;
// vector<bool> status(1e6+1,1);
int t = 1;
// vll primes;
// seive(status);
// f(i,2, 1e6+1) {
// if(status[i]) primes.push_back(i);
// }
// cin>>t;
while(t--) {
solve_case();
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using iPair = pair<int,int>;
using lPair = pair<ll, ll>;
using ivector = vector<int>;
using lvector = vector<ll>;
using istack = stack<int>;
using iqueue = queue<int>;
using ivv = vector<vector<int>>;
using lvv = vector<vector<ll>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};
#define dump(x) cout << #x << " = " << (x) << endl
#define ALL(x) begin(x),end(x)
#define rep(i,s,e) for(ll i=(s), i_stop=(e); i<i_stop; ++i)
#define repRev(i,s,e) for(ll i=(e)-1, i_stop=(s); i>=i_stop; --i)
#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)
#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)
#define foreach(x,container) for(auto &&x:container)
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;}
template<typename T> void printArr(vector<T> &arr){
for(auto &x:arr) {cout << x << " ";} cout << endl;
}
// =====================================================>
/*
*/
void solve() {
ll n,q; cin>>n>>q;
lvector a(n+1);
range(i, 1, n) cin>>a[i];
sort(ALL(a));
// printArr(a);
lvector s(n); s[0] = a[1] - 1;
for(ll i = 1; i < n; ++i) {
// [ a[i]+1, a[i+1] )
s[i] = s[i-1] + a[i+1] - a[i] - 1;
}
// printArr(s);
while(q--) {
ll k; cin>>k;
if(k > s[n-1]) {
cout << a[n] + (k-s[n-1]) << endl;
} else {
int idx = lower_bound(ALL(s), k) - s.begin();
if(idx == 0) {
cout << k << endl;
} else {
// 在 [a[i]+1, a[i+1]) 之間
cout << a[idx] + (k - s[idx-1]) << endl;
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
// modpow
template<class T> T modpow(T b,T p,T m){
T rt = 1; b %= m;
while(p>0){
if(p&1) rt = (rt*b)%m;
b = (b*b)%m; p>>=1;
}
return rt;
}
// small mod combination
// # f,g を次のようにする
// - f[i] : i が何回 MOD で割れるか. これを f_pre とする
// - g[i] : i の MOD で割れない部分の MOD での余り. これを g_pre とする
// # その後, f,g を次のようにする
// - f[i] : i! が何回 MOD で割れるか
// * f[i] = f_pre[1]+...+f_pre[i] 累積和
// - g[i] : i! が MOD で割れない部分の MOD での余り
// * g[i] = g_pre[1]*...*g_pre[i] 累積積(mod)
const int64_t MOD = 3;
const int64_t fMAX = 400007;
int64_t f[fMAX+1],g[fMAX+1],g_inv[fMAX+1];
void init(){
for(int64_t i=1,tmp;i<=fMAX;i++){
tmp = i;
while(tmp%3==0){
tmp /= 3; f[i]++;
}
g[i] = tmp%3;
}
g[0]=1;
for(int64_t i=1;i<=fMAX;i++)
f[i] += f[i-1];
for(int64_t i=1;i<=fMAX;i++)
g[i] = g[i]*g[i-1]%MOD;
for(int64_t i=0;i<=fMAX;i++)
g_inv[i] = modpow(g[i],MOD-2,MOD);
return;
}
int64_t modcomb_s(int64_t n,int64_t r,int64_t m){
if(f[n]-f[r]-f[n-r]>0) return 0;
return g[n]*g_inv[r]*g_inv[n-r]%m;
}
int main(){
std::cin.tie(nullptr);
std::cout.tie(nullptr);
ios::sync_with_stdio(false);
int64_t n; cin >> n;
string s; cin >> s;
init();
vector<int64_t> a(n);
for(int64_t i=0;i<n;i++){
if(s[i]=='B') a[i] = 0;
else if(s[i]=='R') a[i] = 1;
else if(s[i]=='W') a[i] = 2;
}
int64_t ans=0;
for(int64_t i=0;i<n;i++){
ans += a[i] * modcomb_s(n-1,i,MOD);
ans %= MOD;
}
ans = ans * modpow((int64_t)2,n-1,MOD) % MOD;
string ans_s;
if(ans==0) ans_s='B';
else if(ans==1) ans_s='R';
else if(ans==2) ans_s='W';
cout << ans_s << "\n";
return 0;
}
|
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#define ll long long
#define nl '\n'
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define pii pair<int, int>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
void solve() {
int n,k;
cin>>n>>k;
int sumn = 0;
for(int i =1;i<=n;i++){
for(int j = 1;j<=k;j++){
string room_no = to_string(i)+"0"+to_string(j);
sumn+=stoi(room_no);
}
}
cout<<sumn<<nl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int te = 1;
//cin >> te;
while(te--) solve();
} | #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(){
ll ii, jj, count(0),flor(0);
cin >> ii>>jj;
rep(i,jj){
count+=i+1;
}
rep(i,ii){
flor+=i+1;
}
count =100 * flor * jj + count*ii;
cout << count;
return 0;
} |
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<cstdint>
#include<cstddef>
#include<vector>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = uint_fast64_t;
template<typename T>
using vec = vector<T>;
#define rep(i, n) for (i64 i = 0; i < (i64)(n); ++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using P = pair<i64,i64>;
void solve(){
}
using i64 = int_fast64_t;
constexpr i64 MAX = 10000000;
constexpr i64 MOD = 1000000007;
i64 fac[MAX], finv[MAX], inv[MAX];
template <i64 modulus>
class modcal
{
public:
i64 a;
constexpr modcal(const i64 x = 0) noexcept : a(x % modulus) {}
constexpr i64 &value() noexcept { return a; }
constexpr const i64 &value() const noexcept { return a; }
constexpr modcal operator+(const modcal rhs) const noexcept
{
return modcal(*this) += rhs;
}
constexpr modcal operator-(const modcal rhs) const noexcept
{
return modcal(*this) -= rhs;
}
constexpr modcal operator*(const modcal rhs) const noexcept
{
return modcal(*this) *= rhs;
}
constexpr modcal operator/(const modcal rhs) noexcept
{
return modcal(*this) /= rhs;
}
constexpr modcal &operator+=(const modcal rhs) noexcept
{
a += rhs.a;
if (a >= modulus)
{
a -= modulus;
}
return *this;
}
constexpr modcal &operator-=(const modcal rhs) noexcept
{
if (a < rhs.a)
{
a += modulus;
}
a -= rhs.a;
return *this;
}
constexpr modcal &operator*=(const modcal rhs) noexcept
{
a = a * rhs.a % modulus;
return *this;
}
constexpr modcal &operator/=(modcal rhs) noexcept
{
i64 exp = modulus - 2;
while (exp)
{
if (exp % 2)
{
*this *= rhs;
}
rhs *= rhs;
exp /= 2;
}
return *this;
}
void COMninit()
{
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 % modulus;
inv[i] = MOD - inv[modulus % i] * (modulus / i) % modulus;
finv[i] = finv[i - 1] * inv[i] % modulus;
}
}
i64 COMn(i64 n, i64 k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % modulus) % modulus;
}
constexpr modcal<1000000007> modpow(const modcal<1000000007> &a, i64 n)
{
if (n == 0)
return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1)
t = t * a;
return t;
}
};
using modc = modcal<1000000007>;
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
i64 h,w;
cin >> h >> w;
vec<string> s(h);
rep(i,h)cin >> s[i];
i64 dx[3] = {1,1,0};
i64 dy[3] = {0,1,1};
vec<vec<vec<modc>>> dp(h,vec<vec<modc>>(w,vec<modc>(4,0)));
dp[0][0][3] = 1;
rep(i,h){
rep(j,w){
rep(k,4){
if(dp[i][j][k].a == 0)continue;
rep(v,3){
i64 ny = dy[v] + i;
i64 nx = dx[v] + j;
if(nx < 0 || nx >= w || ny < 0 || ny >= h)continue;
if(s[ny][nx] == '#')continue;
dp[ny][nx][v] += dp[i][j][k];
if(k == v)dp[ny][nx][v] += dp[i][j][k];
}
}
}
}
modc ans = 0;
rep(i,3){
ans += dp[h - 1][w - 1][i];
}
cout << ans.a << endl;
} | // Author : ps41
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll M = 1e9 + 7, N = 2e3 + 5;
string a[N];
ll row[N][N], col[N][N], dp[N][N], diag[N][N];
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr);
ll n, m;
cin >> n >> m;
for (ll i = 1; i <= n; i++) cin >> a[i], a[i] = "#" + a[i];
dp[1][1] = 1;
row[1][1] = 1, col[1][1] = 1, diag[1][1] = 1;
for (ll i = 1; i <= n; i++)
{
for (ll j = 1; j <= m; j++)
{
if (i == 1 and j == 1) continue;
if (a[i][j] == '#') dp[i][j] = 0, row[i][j] = 0, col[i][j] = 0, diag[i][j] = 0;
else
{
dp[i][j] = (row[i][j - 1] + col[i - 1][j] + diag[i - 1][j - 1]) % M;
row[i][j] = (dp[i][j] + row[i][j - 1]) % M;
col[i][j] = (dp[i][j] + col[i - 1][j]) % M;
diag[i][j] = (dp[i][j] + diag[i - 1][j - 1]) % M;
}
}
}
cout << dp[n][m];
} |
/*
Sat-Chitta-Ananda
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("in.txt", "r", stdin);
int x, y; cin >> x >> y;
int a; cin >> a;
int b; cin >> b;
int e = 0;
while(x < y)
{
if(LLONG_MAX/x >= a && x*a <= x+b) x *= a, ++e;
else {
e += (y-x)/b;
if((y-x)%b != 0) ++e;
break;
}
}
cout << e-1 << '\n';
return 0;
} | // Created by ...
#include <bits/stdc++.h>
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define vll vector< ll >
#define M 100000
#define MD 1000000007
#define pb push_back
#define rep(i,a,b) for(ll i = a; i <= b; ++i)
#define fo(i,a,b) for(int i = a; i<= b; ++i)
#define pii pair<ll,ll>
#define vec(a) vector<a >
#define all(a) a.begin(),a.end()
#define se second
#define fi first
#define inf 0xffffffff
#define inchar getchar_unlocked
#define outchar(x) putchar_unlocked(x)
//#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
using ll = long long;
ll md=MD;
ll exp(ll a,ll b){ll r=1ll;while(b>0){if(b&1){r=r*(a%md);r=(r+md)%md;}b>>=1;a=(a%md)*(a%md);a=(a+md)%md;}return (r+md)%md;}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll Min(ll a,ll b){if(a<b)return a; return b;}
ll Max(ll a,ll b){if(a>b)return a; return b;}
int32_t main() {
IOS;
unsigned long long int x,y,a,b,exp=0;
cin>>x>>y>>a>>b;
while(x<y && x*a<x+b){
if(x*a>=y || x*a<x)break;
x*=a;
if(x<0)break;
++exp;
}
exp+=Max(0,(y-x-1)/b);
cout<<exp;
cout<<'\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define M 1000000007
typedef pair<int, int> ii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ii> vii;
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1, i;
//cin >> t;
while (t--)
{
ll n, k;
cin >> n >> k;
while (k--)
{
if (n % 200 == 0)
{
n /= 200;
}
else
{
n *= 1000;
n += 200;
}
}
cout << n;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll N, K;
cin >> N >> K;
for (; K; K--) {
if (N % 200 == 0) {
N /= 200;
}
else {
N = N * 1000 + 200;
}
}
cout << N << endl;
} |
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <numeric>
#include <functional>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <memory>
#include <thread>
#include <tuple>
#include <limits>
#include <iostream>
using namespace std;
int n;
vector<vector<pair<int, int>>> graph;
int best_path[2001][2001];
long long calc(int start) {
priority_queue<pair<long long, int>> pq;
vector<long long> dist(n, LLONG_MAX);
vector<char> visit(n, 0);
pq.emplace(0, start);
dist[start] = 0;
while (!pq.empty()) {
long long curdist;
int cur;
tie(curdist, cur) = pq.top();
pq.pop();
curdist = -curdist;
if (visit[cur]) continue;
for (auto edge : graph[cur]) {
int next = edge.first;
long long nd = edge.second + curdist;
if (dist[next] > nd) {
dist[next] = nd;
pq.emplace(-nd, next);
}
}
}
long long ans = LLONG_MAX;
for (int i = 0; i < n; i++) {
if (dist[i] == LLONG_MAX)continue;
if (best_path[i][start] == -1) continue;
ans = min(ans, best_path[i][start] + dist[i]);
}
if (ans == LLONG_MAX) ans = -1;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int m;
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) best_path[i][j] = -1;
graph.resize(n);
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
graph[a].emplace_back(b, c);
if (best_path[a][b] == -1 || best_path[a][b] > c) {
best_path[a][b] = c;
}
}
for (int i = 0; i < n; i++) {
cout << calc(i) << "\n";
}
return 0;
}
| #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep_inv(i, n, m) for(int i = (int)(n); i > (int)(m); i--)
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
ll infty = (ll)pow(10, 15);
int main(){
ll N, M;
cin >> N >> M;
map<pll, ll> loads;
map<ll, vl> from;
map<ll, vl> to;
ll A, B, C;
rep(i, M){
cin >> A >> B >> C;
if(loads.count({A, B}) == 1){ // 既に登録されていた場合、注意が必要
loads[{A, B}] = min(loads[{A, B}], C);
} else{
loads[{A, B}] = C;
from[A].push_back(B);
to[B].push_back(A);
}
}
rep2(i, 1, N + 1){
if((from.count(i) == 0) || (to.count(i) == 0)){
cout << -1 << "\n";
continue;
}
vl dist(N + 1, infty);
vl done(N + 1, 0);
priority_queue<pll, vpll, greater<pll> > Q;
dist[0] = 0;
Q.push({0, 0});
while(!Q.empty()){
ll cost = Q.top().first;
ll cnode = Q.top().second;
Q.pop();
if(cost > dist[cnode])
continue;
if(cnode == 0){
for(auto itr = from[i].begin(); itr != from[i].end(); itr++){
if(dist[*itr] > loads[{i, *itr}] + dist[cnode]){
dist[*itr] = loads[{i, *itr}] + dist[cnode];
Q.push({dist[*itr], *itr});
}
}
} else{
for(auto itr = from[cnode].begin(); itr != from[cnode].end(); itr++){
if(dist[*itr] > loads[{cnode, *itr}] + dist[cnode]){
dist[*itr] = loads[{cnode, *itr}] + dist[cnode];
Q.push({dist[*itr], *itr});
}
}
}
}
if(dist[i] < infty)
cout << dist[i] << "\n";
else
cout << -1 << "\n";
}
return 0;
}
|
#include <iostream>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define mes(a) cout << (a) << endl
#define dmes(a, b) cout << (a) << " " << (b) << endl
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << endl
#define debug_4(x1, x2, x3, x4) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define debug(...) \
CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~)) \
(__VA_ARGS__)
#define ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
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;
}
struct io
{
io()
{
ios::sync_with_stdio(false);
cin.tie(0);
}
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
signed main(void)
{
ll h, w, c = 0;
cin >> h >> w;
char s[100][100];
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> s[i][j];
}
}
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w - 1; j++)
{
if (s[i][j] == s[i][j + 1] && s[i][j] == '.')
{
c++;
}
}
}
for (int j = 0; j < w; j++)
{
for (int i = 0; i < h - 1; i++)
{
if (s[i][j] == s[i + 1][j] && s[i][j] == '.')
{
c++;
}
}
}
mes(c);
} | #include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, 1, 0, 1}, dy[4] = {0, 0, 1, 1};
int main(){
int H, W;
cin >> H >> W;
vector<string> S(H);
for(int i = 0; i < H; i++) cin >> S[i];
int ans = 0;
for(int i = 0; i < H - 1; i++){
for(int j = 0; j < W - 1; j++){
int cnt = 0;
for(int k = 0; k < 4; k++){
int nx = i + dx[k], ny = j + dy[k];
if(S[nx][ny] == '#') cnt++;
}
if(cnt % 2 == 1) ans++;
}
}
cout << ans << endl;
} |
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <cmath>
#include <cstring>
#define For(i,x,y) for(int i = (x); i <= (y); i ++ )
#define fori(i,x,y) for(int i = (x); i < (y); i ++ )
#define sz(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define db double
//#define endl '\n'
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long LL;
typedef long long ll;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
typedef pair<db,db> pdd;
const db eps = 1e-6;
const db pi = acos(-1.0);
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
print(x), putchar(let);
}
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int maxn = 2e5 + 10;
vector<int>e[maxn];
int dpt[maxn<<1];
int dfs_clock, seq[maxn], dfn[maxn], fa[maxn];
int in[maxn<<1], out[maxn<<1];
vector<vector<int>>dir(maxn);
void dfs(int x, int dis){
in[x] = ++dfs_clock;
//seq[dfn[x] = ++dfs_clock] = x;
dir[dis].pb(in[x]);
// dir.pb({dpt[x],dfn[x]});
for(auto v:e[x]){
dfs(v,dis+1);
}
out[x] = ++dfs_clock;
}
int pre[maxn];
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin>>n;
For(v,2,n){
cin>>fa[v];
e[fa[v]].pb(v);
}
dfs(1,0);
// debug("ok");
// sort(ALL(dir));
int q;
cin>>q;
while(q--){
int u, d;
cin>>u>>d;
const auto di= dir[d];
cout<<(lower_bound(di.cbegin(),di.cend(),out[u]) - lower_bound(di.cbegin(),di.cend(),in[u]) )<<endl;
}
// }
return 0;
} | #include <iostream>
using namespace std;
const int N_MX = 1e3;
int n;
int x[N_MX], y[N_MX];
int ans;
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
double grad = double(y[j] - y[i]) / (x[j] - x[i]);
if (grad >= -1.0 && grad <= 1.0) ans++;
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define ALL(v) v.begin(), v.end()
using namespace std;
using P = pair<int, int>;
typedef long long ll;
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 int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main() {
int n;
cin >> n;
string s;
cin >> s;
stack<char> st;
rep(i, n){
int ok = 1;
if(st.size() < 3){
st.push(s[i]);
continue;
}
while(ok == 1){
if(st.size() > 2){
char three = st.top();
st.pop();
char two = st.top();
st.pop();
char one = st.top();
st.pop();
if(one == 'f' && two == 'o' && three == 'x'){
ok = 1;
}else{
st.push(one);
st.push(two);
st.push(three);
ok = 0;
}
}else{
break;
}
}
st.push(s[i]);
}
if(st.size() > 2){
char three = st.top();
st.pop();
char two = st.top();
st.pop();
char one = st.top();
st.pop();
if (one != 'f' || two != 'o' || three != 'x'){
st.push(one);
st.push(two);
st.push(three);
}
}
cout << st.size() << 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 67280421310721
#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
ll n;
cin >> n;
vi arr(n+1);
rep(i,n)cin>>arr[i+1];
sort(all(arr));
vi pre(n+1);
fr(i,1,n)pre[i]=pre[i-1]+arr[i];
ld ans=INF;
ll tot = pre[n];
fr(i,0,n){
ld tmp = tot;
ld x = arr[i];
x/=2;
tmp += n*x;
tmp -= (n-i)*arr[i] + pre[i];
// cout<<tmp<<"\n";
ans=min(ans, tmp);
}
ans/=n;
cout<<ans<<"\n";
// END
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0), cout.precision(15); cout<<fixed;
#ifdef godfather
cin.exceptions(cin.failbit);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
// cin>>t;
fr(i,1,t)
{
// cout<<"Case #"<<i<<": ";
solve();
}
end_routine();
} |
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define mem memset
#define ff first
#define ss second
#define loop0(i,n) for(i=0;i<n;i++)
#define loop1(i,n) for(i=1;i<=n;i++)
ll dist(pair<ll,ll> p1,pair<ll,ll> p2)
{
return max(abs(p1.ff-p2.ff),abs(p1.ss-p2.ss));
}
int main()
{
ll n,i,j,x,y,k,l,a,b;
cin>>n;
pair<pair<ll,ll>,ll> p[n],q[n];
loop0(i,n)
{
cin>>x>>y;
p[i]={{x,y},i};
q[i]={{y,x},i};
}
sort(p,p+n);
sort(q,q+n);
priority_queue<ll> pq;
map<pair<ll,ll>,ll> mp;
ll ans;
a=p[0].ss;b=p[n-1].ss;
x=min(a,b);y=max(a,b);
if(!mp[{x,y}])
{
pq.push(dist(p[0].ff,p[n-1].ff));
mp[{x,y}]=1;
}
a=p[0].ss;b=p[n-2].ss;
x=min(a,b);y=max(a,b);
if(!mp[{x,y}])
{
pq.push(dist(p[0].ff,p[n-2].ff));
mp[{x,y}]=1;
}
a=p[1].ss;b=p[n-1].ss;
x=min(a,b);y=max(a,b);
if(!mp[{x,y}])
{
pq.push(dist(p[1].ff,p[n-1].ff));
mp[{x,y}]=1;
}
a=q[0].ss;b=q[n-1].ss;
x=min(a,b);y=max(a,b);
if(!mp[{x,y}])
{
pq.push(dist(q[0].ff,q[n-1].ff));
mp[{x,y}]=1;
}
a=q[0].ss;b=q[n-2].ss;
x=min(a,b);y=max(a,b);
if(!mp[{x,y}])
{
pq.push(dist(q[0].ff,q[n-2].ff));
mp[{x,y}]=1;
}
a=q[1].ss;b=q[n-1].ss;
x=min(a,b);y=max(a,b);
if(!mp[{x,y}])
{
pq.push(dist(q[1].ff,q[n-1].ff));
mp[{x,y}]=1;
}
pq.pop();
ans=pq.top();
cout<<ans<<'\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long int ull;
#include <fstream>
#define int long long int
#define mod 1000000007
#define watch(x) cout<<(#x)<<" is "<<(x)<<"\n"
#define watch2(x,y) cout<<(#x)<<" is "<<(x)<<" and "<<(#y)<< " is " <<(y)<<"\n"
int powerr(int m,int n){if(m==0 || n==1)return m;if(n==0)return 1;int ans = 1;if(n&1)ans = m*powerr(m,n-1);else {ans = powerr(m,n/2);ans = ans * ans;}return ans%mod;}
int gcdd(int a,int b){if(b==0)return a; return gcdd(b,a%b);}
int lcmm(int a,int b){return (a*b)/gcdd(a,b);}
/*
signed main(){
set<pair<int,int>> s;
s.insert({1,2});
s.insert({2,1});
s.insert({1,2});
cout<<s.size();
}
*/
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
vector<pair<int,int>> x,y;
vector<pair<int,int>> V;
for(int i=0;i<n;++i){
int x1,y1;cin>>x1>>y1;
x.push_back({x1,i});
y.push_back({y1,i});
V.push_back({x1,y1});
}
sort(x.begin(),x.end());
sort(y.begin(),y.end());
int diff1_x = abs(x[0].first - x[n-1].first);
int diff2_x = abs(x[1].first - x[n-1].first);
int diff3_x = abs(x[0].first - x[n-2].first);
int ind1_x = x[0].second , ind2_x = x[1].second, ind3_x = x[n-2].second, ind4_x = x[n-1].second;
int diff1_y = abs(y[0].first - y[n-1].first);
int diff2_y = abs(y[1].first - y[n-1].first);
int diff3_y = abs(y[0].first - y[n-2].first);
int ind1_y = y[0].second , ind2_y = y[1].second, ind3_y = y[n-2].second, ind4_y = y[n-1].second;
set<pair<int,int>> soul;
if(ind1_x < ind4_x){
soul.insert({ind1_x,ind4_x});
}
else {
soul.insert({ind4_x,ind1_x});
}
if(ind1_x < ind3_x){
soul.insert({ind1_x,ind3_x});
}
else {
soul.insert({ind3_x,ind1_x});
}
if(ind2_x < ind4_x){
soul.insert({ind2_x,ind4_x});
}
else {
soul.insert({ind4_x,ind2_x});
}
//reyy
if(ind1_y < ind4_y){
soul.insert({ind1_y,ind4_y});
}
else {
soul.insert({ind4_y,ind1_y});
}
if(ind1_y < ind3_y){
soul.insert({ind1_y,ind3_y});
}
else {
soul.insert({ind3_y,ind1_y});
}
if(ind2_y < ind4_y){
soul.insert({ind2_y,ind4_y});
}
else {
soul.insert({ind4_y,ind2_y});
}
vector<int> vec;
for(auto x:soul){
int a = x.first , b = x.second;
vec.emplace_back( max( abs(V[a].first-V[b].first) , abs(V[a].second - V[b].second) ) );
}
sort(vec.begin(),vec.end());
if(vec.size()==1 || vec.size()==2){
cout<<vec[0];return 0;
}
cout<<vec[vec.size()-2];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
ll n, m;
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
if(m < 0) {
cout << -1 << endl;
return 0;
}
if(m == 0) {
ll rr = 0;
for(int i = 1; i <= n; i++) {
cout << rr + (2*i - 1) << " " << rr + (2*i) << endl;
}
return 0;
}
if((m > 0) and (n < (m+2))) {
cout << -1 << endl;
return 0;
}
ll r = n - (m+2);
ll rr = (m+1) * 2 + 2;
cout << 1 << " " << rr << endl;
for(int i = 1; i <= m+1; i++) {
cout << 2*i << " " << 2*i+1 << endl;
}
for(int i = 1; i <= r; i++) {
cout << rr + (2*i - 1) << " " << rr + (2*i) << endl;
}
return 0;
}
| #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
inline int read_int(){
int t=0;bool sign=false;char c=getchar();
while(!isdigit(c)){sign|=c=='-';c=getchar();}
while(isdigit(c)){t=(t<<1)+(t<<3)+(c&15);c=getchar();}
return sign?-t:t;
}
inline LL read_LL(){
LL t=0;bool sign=false;char c=getchar();
while(!isdigit(c)){sign|=c=='-';c=getchar();}
while(isdigit(c)){t=(t<<1)+(t<<3)+(c&15);c=getchar();}
return sign?-t:t;
}
inline char get_char(){
char c=getchar();
while(c==' '||c=='\n'||c=='\r')c=getchar();
return c;
}
inline void write(LL x){
char c[21],len=0;
if(!x)return putchar('0'),void();
if(x<0)x=-x,putchar('-');
while(x)c[++len]=x%10,x/=10;
while(len)putchar(c[len--]+48);
}
inline void space(LL x){write(x),putchar(' ');}
inline void enter(LL x){write(x),putchar('\n');}
const int MAXN=2e5+5;
int p[MAXN],a[MAXN],b[MAXN];
LL s[MAXN];
int Find(int x){
return p[x]==x?x:p[x]=Find(p[x]);
}
int main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
int n=read_int(),m=read_int();
if(n==1&&m==0)
printf("%d %d\n",1,2);
else if(m<0||m>=n-1)puts("-1");
else if((n-m)%2==1){
m++;
int pos=1,d=(n-m)/2;
_for(i,1,d){
printf("%d %d\n%d %d\n",pos,pos+3,pos+1,pos+2);
pos+=4;
}
d=n-2*(d-1)-1;
printf("%d %d\n",pos,pos+1+2*d);
pos++;
_rep(i,1,d-2)
printf("%d %d\n",pos,pos+1),pos+=2;
printf("%d %d\n%d %d\n",pos,pos+3,pos+1,pos+2);
}
else{
int pos=1,d=(n-m)/2;
_for(i,1,d){
printf("%d %d\n%d %d\n",pos,pos+3,pos+1,pos+2);
pos+=4;
}
d=n-2*(d-1)-1;
printf("%d %d\n",pos,pos+1+2*d);
pos++;
_rep(i,1,d)
printf("%d %d\n",pos,pos+1),pos+=2;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int>A(2*N);
vector<pair<int,int>>B(2*N);
vector<int>C(2*N);
for(int i = 0; i < 2*N; i++) {
cin >> A[i];
B[i] = {A[i],i};
}
sort(B.rbegin(),B.rend());
for(int i = 0; i < N; i++) {
C[B[i].second]++;
}
string ans = "";
int sum1 = 0,sum2 = 0;
for(int i = 0; i < 2*N; i++) {
if(C[i] == 0) {
if(sum2) {
sum2--;
ans += ')';
}
else {
sum1++;
ans += '(';
}
}
else {
if(sum1) {
sum1--;
ans += ')';
}
else {
sum2++;
ans += '(';
}
}
}
cout << ans << endl;
}
| #pragma region RegionDefs
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define reps(i,l,r) for(int i=(l),i##_len=(r);i<i##_len;++i)
#define repr(i,l,r) for(int i=(r)-1,i##_left=(l);i>=i##_left;--i)
#define all(x) begin(x),end(x)
using namespace std;
typedef long long ll;
const ll INF = 1e9;
template<class T=ll> using V = vector<T>;
template<class T=ll> using PQ = priority_queue<T>;
template<class T=ll> using PQG = priority_queue<T, V<T>, greater<T>>;
const ll MOD = 1000000007LL;
void in() {}
template<class Head, class... Tail>
void in(Head&& head, Tail&&... tail) { cin >> head; in(move(tail)...); }
#define IN(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define TIN(T, ...) T __VA_ARGS__; in(__VA_ARGS__)
#define VIN(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem
#define VIND(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem,--_elem
#define OUT(x) cout << (x);
#define OUTL(x) cout << (x) << endl;
template<class T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) {if (a>b) {a=b; return 1;} return 0;}
template<class T>
string join(vector<T>& v, string delim="") { ostringstream os; rep(i,v.size())i?os<<delim<<v[i]:os<<v[0]; return os.str(); }
#pragma endregion RegionDefs
ll acc[214514];
void solve()
{
IN(n, w);
rep(i, n) {
IN(s, t, p);
acc[s] += p;
acc[t] -= p;
}
ll tmp = 0;
rep(i, 214514) {
tmp += acc[i];
if (tmp > w) {
OUTL("No");
return;
}
}
OUTL("Yes");
}
int main()
{
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
} |
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("l.txt", "r", stdin);
freopen("m.txt", "w", stdout);
#else
#endif
ll n,s,d;
cin>>n>>s>>d;
bool f=false;
for(ll i=0;i<n;i++)
{
ll a,b;
cin>>a>>b;
if(a<s && b>d)
{
f=true;
}
}
if(f)
{
cout<<"Yes"<<endl;
}
else
{cout<<"No"<<endl;}
}
| #include <iostream>
int main() {
int a, b, c;
std::cin >> a >> b >> c;
if ( a > b )
std::cout << "Takahashi" << std::endl;
else if ( a < b )
std::cout << "Aoki" << std::endl;
else {
if ( c == 0 )
std::cout << "Aoki" << std::endl;
else
std::cout << "Takahashi" << std::endl;
}
return 0;
} |
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << "[" << H << "]";
debug_out(T...);
}
#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SZ(s) ((int)s.size())
clock_t startTime;
double getCurrentTime() {
return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
typedef long long ll;
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 4e4;
const int M = 1234;
const int LOG = 18;
int sum(int x){
return x / 100 + x / 10 % 10 + x % 10;
}
void solve(int TC) {
int a, b;
cin >> a >> b;
cout << max(sum(a), sum(b));
}
int main() {
startTime = clock();
ios_base::sync_with_stdio(false);
bool llololcal = false;
#ifdef dddxxz
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
llololcal = true;
#endif
int TC = 1;
//cin >> TC;
for (int test = 1; test <= TC; test++) {
debug(test);
solve(test);
}
if (llololcal) cerr << endl << "Time: " << getCurrentTime() * 1000 << " ms" << endl;
return 0;
} |
//#include <atcoder/all>
//using namespace atcoder;
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <array>
#include <algorithm>
#include <utility>
#include <numeric>
#include <functional>
#include <limits>
#include <iomanip>
#include <cmath>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <typename T = ll>
using Coord = pair<T, T>;
template <typename T = ll>
using Mat = vector<vector<T>>;
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p)
{
os << '(' << p.first << ", " << p.second << ')';
return os;
}
template <class T>
Mat<T> MakeMat(size_t w, size_t h)
{
return Mat<T>(h, vector<T>(w));
}
template <class ...Args>
void Input(Args &...args)
{
(cin >> ... >> args);
}
template <class T>
void InputArray(size_t size, T& t)
{
t.resize(size);
for (auto& e : t) {
cin >> e;
}
}
template <class ...Args>
void InputArray(size_t size, Args &...args)
{
(InputArray(size, args), ...);
}
template <class ...Args>
void InputVerticalArray(size_t size, Args &...args)
{
(args.resize(size), ...);
for (int i = 0; i < size; i++) {
((cin >> args[i]), ...);
}
}
template <class T>
void InputMatrix(size_t width, size_t height, T& t)
{
t.assign(height, typename T::value_type(width));
for (auto& a : t) {
for (auto& e : a) {
cin >> e;
}
}
}
template <class ...Args>
void InputMatrix(size_t width, size_t height, Args &...args)
{
(InputMatrix(width, height, args), ...);
}
template <class ...Args>
void Output(const Args &...args)
{
((cout << args << ' '), ...) << endl;
}
template <class ...Args>
void OutputVertical(const Args &...args)
{
((cout << args << endl), ...);
}
template <class T>
void OutputArray(const T& t)
{
for (const auto& e : t) {
cout << e << ' ';
}
cout << endl;
}
template <class ...Args>
void OutputArray(const Args &...args)
{
(OutputArray(args), ...);
}
template <class ...Args>
void OutputVerticalArray(const Args &...args)
{
for (int i = 0; i < min({ args.size()... }); i++) {
((cout << args[i] << ' '), ...) << endl;
}
}
template <class T>
void OutputMatrix(const T& t)
{
for (const auto& a : t) {
for (const auto& e : a) {
cout << e << ' ';
}
cout << '\n';
}
cout << flush;
}
template <class ...Args>
void OutputMatrix(const Args &...args)
{
(OutputMatrix(args), ...);
}
#define X first
#define Y second
#define inp(type, ...) type __VA_ARGS__; Input(__VA_ARGS__)
#define inpa(type, size, ...) type __VA_ARGS__; InputArray((size), __VA_ARGS__)
#define inpva(type, size, ...) type __VA_ARGS__; InputVerticalArray((size), __VA_ARGS__)
#define inpm(type, width, height, ...) type __VA_ARGS__; InputMatrix((width), (height), __VA_ARGS__)
#define otp(...) Output(__VA_ARGS__)
#define otpv(...) OutputVertical(__VA_ARGS__)
#define otpa(...) OutputArray(__VA_ARGS__)
#define otpva(...) OutputVerticalArray(__VA_ARGS__)
#define otpm(...) OutputMatrix(__VA_ARGS__)
#define rep(counter, range) for (ll (counter) = 0; (counter) < (range); (counter)++)
#define rep2(counter, start, end) for (ll (counter) = (start); (counter) < (end); (counter)++)
#define rep3(counter, start, end, step) for (ll (counter) = (start); (counter) < (end); (counter) += (step))
#define repi(range) rep(i, range)
#define repi2(start, end) rep2(i, start, end)
#define repi3(start, end, step) rep3(i, start, end, step)
#define rrep(counter, range) for (ll (counter) = (range) - 1; (counter) >= 0; (counter)--)
#define rrepi(range) rrep(i, range)
#define repc(element, container) for (auto &(element) : (container))
#define repe(container) repc(e, container)
#define all(container) (begin(container)), (end(container))
#ifdef DBG
# define dbg(x) x
#else
# define dbg(x)
#endif
int main()
{
inp(string, a, b);
int sa = a[0] + a[1] + a[2] - '0' * 3;
int sb = b[0] + b[1] + b[2] - '0' * 3;
otp(sa >= sb ? sa : sb);
return 0;
}
|
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rFor(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i)
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
T div_floor(T a, T b) {
if (b < 0) a *= -1, b *= -1;
return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
if (b < 0) a *= -1, b *= -1;
return a > 0 ? (a - 1) / b + 1 : a / b;
}
template <typename T>
struct coord_comp {
vector<T> v;
bool sorted = false;
coord_comp() {}
int size() { return v.size(); }
void add(T x) { v.push_back(x); }
void build() {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
sorted = true;
}
int get_idx(T x) {
assert(sorted);
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
T &operator[](int i) { return v[i]; }
};
constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;
int main() {
int a, b;
scanf("%d%d", &a, &b);
rrep(x, b + 1) if (div_floor(b, x) - div_floor(a - 1, x) >= 2) {
printf("%d\n", x);
return 0;
}
} | #include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#include "queue"
using namespace std;
#define MAXN 222
//#define int long long
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all(x) (x).begin() , (x).end()
#define mem( a ) memset( a , 0 , sizeof a )
typedef long long ll;
int n , m , k;
const int P = 1e9 + 7;
int A[MAXN];
int Pow( int x , int a ) {
int ret = 1;
while( a ) {
if( a & 1 ) ret = ret * 1ll * x % P;
x = x * 1ll * x % P , a >>= 1;
}
return ret;
}
struct mtrx {
#define N 100
int A[103][103] , flg;
mtrx( ) {
mem( A );
}
int* operator [] ( int x ) { return A[x]; }
mtrx operator * ( mtrx B ) {
mtrx as;
rep( i , 0 , N ) rep( k , 0 , N ) if( A[i][k] ) rep( j , 0 , N )
as[i][j] = ( as[i][j] + A[i][k] * 1ll * B[k][j] ) % P;
return as;
}
} tr , cur ;
void Pow( int a ) {
while( a ) {
if( a & 1 ) cur = tr * cur;
a >>= 1 , tr = tr * tr;
}
}
int deg[MAXN];
void solve() {
cin >> n >> m >> k;
int im = Pow( 2 * m , P - 2 );
rep( i , 1 , n ) scanf("%d",A + i) , cur[i][1] = A[i];
rep( i , 1 , m ) {
int u , v;
scanf("%d%d",&u,&v);
tr[u][v] = tr[v][u] = im;
tr[u][u] = ( tr[u][u] + im ) % P;
tr[v][v] = ( tr[v][v] + im ) % P;
++ deg[u] , ++ deg[v];
}
rep( u , 1 , n ) tr[u][u] += ( m - deg[u] ) * 1ll * Pow( m , P - 2 ) % P;
Pow( k );
rep( i , 1 , n ) printf("%d\n",cur[i][1]);
}
signed main() {
// int T;cin >> T;while( T-- ) solve();
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a;
cin>>a;
int b;
b=100-a%100;
cout<<b<<endl;
}
| #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
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; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
//priority_queue<int,vector<int>, greater<int> > q2;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int X, Y, N, M, A, B, C;
cin >> A;
A %= 100;
cout << 100 -A << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <math.h>
#define ALL(a) a.begin(), a.end()
#define rep(i, K, N) for(long long int i = K; i < N; i++)
#define ll long long int
using namespace std;
int main(){
ll N, Q;
string S, x, y;cin >> N >> S >> Q;
bool C = 0;
x = S.substr(0, N);
y = S.substr(N, N);
rep(i, 0, Q){
ll T, A, B;cin >> T >> A >> B;A--;B--;
if(T == 1 && !C){
if(A < N){
if(B < N)swap(x[A], x[B]);
if(N <= B)swap(x[A], y[B - N]);
}
if(N <= A)swap(y[A - N], y[B - N]);
}
if(T == 1 && C){
if(A < N){
if(B < N)swap(y[A], y[B]);
if(N <= B)swap(y[A], x[B - N]);
}
if(N <= A)swap(x[A - N], x[B - N]);
}
if(T == 2){
if(C)C = 0;
else C = 1;
}
}
if(C)cout << y << x;
else cout << x << y;
}
| #include <bits/stdc++.h>
using namespace std;
//LPFI
int main()
{
int n, q;
cin >> n;
string s;
cin >> s;
cin >> q;
/*string s1 = s.substr(0, n);
string s2 = s.substr(n, 2 * n);*/
string s1, s2;
for (int i = 0; i < n; i++)
s1 += s[i];
for (int i = n; i < 2 * n; i++)
s2 += s[i];
int t, a, b;
while(q--)
{
cin >> t >> a >> b;
if (t == 1)
{
if (a <= n && b <= n)
{
swap(s1[a - 1], s1[b - 1]);
}
else if (a > n && b <= n)
{
if (a == 2 * n)
swap(s2[n - 1], s1[b - 1]);
else
{
a -= n;
swap(s2[a - 1], s1[b - 1]);
}
}
else if (a <= n && b > n)
{
if (b == 2 * n)
swap(s1[a - 1], s2[n - 1]);
else
{
b -= n;
swap(s1[a - 1], s2[b - 1]);
}
}
else if (a > n && b > n)
{
if (a == 2 * n)
a = n;
else
a -= n;
if (b == 2 * n)
b = n;
else
b -= n;
swap(s2[a - 1], s2[b - 1]);
}
}
else if (t == 2)
swap(s1, s2);
}
s = s1 + s2;
cout << s << endl;
return 0;
} |
//#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);
ll N;
cin >> N;
int kotae = 0;
rep1(i, 3000000) {
if (N % i == 0) {
ll a = i;
ll b = N / i;
if (a > b) break;
if (a % 2 == 1) kotae++;
if (a != b) {
if (b % 2 == 1) kotae++;
}
}
}
rep1(i, 3000000) {
if (2 * N % i == 0) {
ll a = i;
ll b = 2 * N / i;
if (a > b) break;
if (a % 2) {
kotae++;
}
if (a != b) {
if (b % 2) kotae++;
}
}
}
co(kotae);
Would you please return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
}
ll a[100005],b[100005],c[1005],d[1005],v[1005],vv[1005];
int main()
{
ll n,m,k,i,j,flag=0,ansx=0,ansy=0,t,sum1,sum,s,ma,ans,q=0;
a[1]=a[2]=2;
a[3]=4;
ans=0;
scanf("%lld",&n);
n*=2;
for(i=1;i*i<=n;i++)
{
if((i%2==1&&n%i==0)||(n%i==0&&(n/i)%2==1))
{
ans++;
}
}
printf("%lld\n\n",ans*2);
return 0;
}
|
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define int long long int
#define ll long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define rep(i, init, n) for (int i = init; i < (int)n; i++)
#define rev(i, n, init) for (int i = (int)n; i >= init; i--)
#define MOD1 1e9 + 7
#define MOD2 998244353
#define f first
#define s second
#define endl '\n'
#define pii pair<int, int>
#define tii tuple<int, int>
#define all(v) v.begin(), v.end()
#define mt make_tuple
#define precise(i) cout << fixed << setprecision(i)
#define codejam cout << "Case #" << ii + 1 << ": ";
#define impossible cout << "IMPOSSIBLE" << endl;
#define error(s) throw runtime_error(s)
#define hash hash1
#define prev prev1
std::mt19937_64 gen(std::chrono::steady_clock::now().time_since_epoch().count());
std::mt19937 gen1(std::chrono::steady_clock::now().time_since_epoch().count());
//change according to int or long long int
int rng(int l, int r)
{
return std::uniform_int_distribution<int>(l, r)(gen);
}
const long double PI = atan(1.0) * 4;
const int32_t INF32 = 2e9 + 7;
const int64_t INF64 = 3e18;
const int32_t LOG = 21;
int32_t MOD = MOD1;
using namespace std;
//-------------------DEBUGGING-------------------------
void my_debugger(string s, int LINE_NUM) { cerr << endl; }
template <typename start, typename... end>
void my_debugger(string s, int LINE_NUM, start x, end... y)
{
if (s.back() != ',')
{
s += ',';
cerr << "LINE(" << LINE_NUM << "): ";
}
int i = s.find(',');
cerr << s.substr(0, i) << " = " << x;
s = s.substr(i + 1);
if (!s.empty())
cerr << ", ";
my_debugger(s, LINE_NUM, y...);
}
#ifdef AJIT
#define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__);
#else
#define debug(...) ;
#endif
void setMod(int mod_val)
{
MOD = mod_val;
}
void files_init()
{
freopen("file.in", "r", stdin);
freopen("file.out", "w", stdout);
}
const int N = 1e6 + 5;
const int LOGN = 20;
//-----------------------------------------------------
int dp[3005][3005];
int pref[3005][3005];
int32_t main()
{
getFaster;
//files_init();
int tests = 1;
//cin >> tests;
rep(ii, 0, tests)
{
int n;
cin>>n;
vector<int> a(n+1);
rep(i,1,n+1)
cin>>a[i];
dp[0][0]=1;
pref[0][1]=1;
int sum=0;
rep(i,1,n+1)
{
sum+=a[i];
rep(j,1,n+1)
{
int rem=sum%j;
dp[i][j]=pref[rem][j];
}
rep(j,1,n+1)
{
int rem=sum%j;
pref[rem][j]+=dp[i][j-1];
while(pref[rem][j]>=MOD)
pref[rem][j]-=MOD;
}
}
int ans=0;
rep(j,1,n+1)
{
ans+=dp[n][j];
while(ans>=MOD)
ans-=MOD;
}
cout<<ans<<endl;
}
return 0;
} | #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<numeric>
//#include<atcoder/scc>
using namespace std;
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
//#define MOD 1000000007
#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
const double EPS = 1e-10;
using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;
class DisjointSet{
public:
vector<ll> rank,p,siz;
DisjointSet(){}
DisjointSet(ll size){
rank.resize(size,0);
p.resize(size,0);
siz.resize(size,1);
rep(i,size) makeSet(i);
}
void makeSet(ll x){
p[x] = x;
rank[x] = 0;
}
bool same(ll x, ll y){
return root(x) == root(y);
}
void unite(ll x, ll y){
x = root(x);
y = root(y);
if(rank[x] > rank[y]){
siz[x] += siz[y];
p[y] = x;
}
else{
siz[y] += siz[x];
p[x] = y;
if(rank[x] == rank[y]) rank[y]++;
}
}
ll root(ll x){
if(x != p[x]){
p[x] = root(p[x]);
}
return p[x];
}
ll size(ll x){
return siz[root(x)];
}
};
ll modpow(ll a, ll n, ll m){
if(n == 0) return 1;
ll half = modpow(a,n/2,m);
ll res = half * half % m;
if(n & 1) res = res * (a%m) % m;
return res;
}
int main(){
int n; cin >> n;
DisjointSet uf(n);
rep(i,n){
int f; cin >> f;
f--;
if(!uf.same(i,f)) uf.unite(i,f);
}
vector<bool> used(n,false);
int ans0 = 0;
rep(i,n){
if(!used[uf.root(i)]){
used[uf.root(i)] = true;
ans0++;
}
}
cout << modpow(2,ans0,MOD)-1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long int
#define f(i,j,n) for(int i=j;i<=n;i++)
#define mod 1000000007
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
string s; cin>>s;
int ans=0;
f(i,0,9)
f(j,0,9)
f(k,0,9)
f(l,0,9){
if(s[i]!='x'&& s[j]!='x' && s[k]!='x' && s[l]!='x')
{
int x=1;
f(a,0,9)
if(a!=i && a!= j && a!= k && a!= l && s[a]=='o')
x=0;
ans+=x;
}
}
cout<<ans;
} | #include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
#define rep(i, n) for(int i=0;i<n;i++)
#define all(i, v) for(auto& i : v)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
const ll N = 200007;
ll fac[N],finv[N],inv[N];
void cominit(){
fac[0]=fac[1]=1;
finv[0]=finv[1]=1;
inv[1]=1;
for(int i=2;i<N;i++){
fac[i]=fac[i-1]*i%mod;
inv[i]=mod-inv[mod%i]*(mod/i)%mod;
finv[i]=finv[i-1]*inv[i]%mod;
}
}
ll com(ll n,ll k){
if(n<k)return 0;
if(n<0 || k<0)return 0;
return fac[n]*(finv[k]*finv[n-k]%mod)%mod;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> s;
cominit();
ll cnt = 0, cnt1 = 0;
all(e, s) {
if(e == 'o') cnt++;
else if(e == '?') cnt1++;
}
ll ans = 0;
rep(bit, 1 << cnt1) {
int num = __builtin_popcount(bit);
if(num + cnt >= 5) continue;
if(num + cnt == 0) continue;
num += cnt;
if(num == 1) ans += 1;
if(num == 2) ans += 14;
if(num == 3) ans += 36;
if(num == 4) ans += 24;
}
cout << ans << endl;
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define out(x) cerr<<#x<<" = "<<x<<" "
#define outln(x) cerr<<#x<<" = "<<x<<endl
#define outarr(x,l,r) cerr<<#x"["<<l<<"-"<<r<<"] = "; for (int _i=l;_i<=r;++_i) cerr<<x[_i]<<" ";cerr<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define gc() getchar()
//char buf[1<<23],*p1=buf,*p2=buf;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x)
{
x=0; char c=gc(); int flag=0;
while (c<'0'||c>'9') flag|=(c=='-'),c=gc();
while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
if (flag) x=-x;
}
template <class T> inline T _max(T a,T b){return a>b ? a : b;}
template <class T> inline T _min(T a,T b){return a<b ? a : b;}
template <class T> inline bool checkmax(T &a,T b){return b>a ? a=b,1 : 0;}
template <class T> inline bool checkmin(T &a,T b){return b<a ? a=b,1 : 0;}
const int N=105,inf=0x3f3f3f3f;
int n;
int g[N][N];
int d[N];
char mp[N];
void init()
{
read(n);
for (int i=1;i<=n;++i)
{
scanf("%s",mp+1);
for (int j=1;j<=n;++j)
{
g[i][j]=(i==j ? 0 : inf);
if (mp[j]=='1') g[i][j]=1;
}
}
for (int k=1;k<=n;++k)
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
if (g[i][k]<inf&&g[k][j]<inf)
checkmin(g[i][j],g[i][k]+g[k][j]);
for (int i=1;i<=n;++i)
for (int j=1;j<=n;++j)
if (g[j][i]<inf) ++d[i];
}
void solve()
{
double ans=0;
for (int i=1;i<=n;++i) ans+=1.0/d[i];
printf("%.12lf\n",ans);
}
int main()
{
init();
solve();
return 0;
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
const long long INF = 1e18;
//const ll mod = 1000000007;
ll dist[105][105];
ll N;
string S[105];
long double ans;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
for(int i = 0; i < N; i++) {
cin >> S[i];
for(int j = 0; j < N; j++) {
if(S[i][j] == '1') dist[i][j] = 1;
else dist[i][j] = INF;
}
dist[i][i] = 0;
}
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
for(int k = 0; k < N; k++) {
chmin(dist[j][k], dist[j][i] + dist[i][k]);
}
}
}
for(int i = 0; i < N; i++) {
//cerr << "---" << i << "---" << endl;
ll num = 0;
for(int j = 0; j < N; j++) {
if(dist[j][i] < INF) {
//cerr << j << endl;
num++;
}
}
ans += (long double)1 / num;
}
cout << fixed << setprecision(30) << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_NUM = 3e5 + 10;
int bit[MAX_NUM];
inline int low(int i) { return i & (-i); }
void update(int i, int k) {
while (i < MAX_NUM) {
bit[i] ^= k;
i += low(i);
}
}
int query(int i) {
int res = 0;
while (i > 0) {
res ^= bit[i];
i -= low(i);
}
return res;
}
int main() {
int N, Q, num;
cin >> N >> Q;
for (int i = 0; i < N; i++) {
cin >> num;
update(i + 1, num);
}
int t, x, y;
for (int i = 0; i < Q; i++) {
cin >> t >> x >> y;
if (t == 1) {
update(x, y);
}
else {
cout << (query(y) ^ query(x - 1)) << "\n";
}
}
return 0;
}
| //ye moh moh ke dhaage,tere ungliyon se ja uljhe
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i,n) for(i=0;i<n;i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it,x) for(auto it:x)
#define PI 3.14159265
#define sync ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl "\n"
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
ll a[3*100002];
ll tree1[12*100002];//0 based indexing (max)
void recalculate1(ll node){
tree1[node]=(tree1[2*node+1]^tree1[2*node+2]);
}
void build1(ll node,ll l,ll r){
if(l==r){
tree1[node]=a[l];
return;
}
ll m=(l+r)/2;
build1(2*node+1,l,m);
build1(2*node+2,m+1,r);
recalculate1(node);
}
ll query1(ll node,ll l,ll r,ll ql,ll qr){
if((ql>r)||(qr<l)) return(-1);
if((ql<=l)&&(qr>=r)) return(tree1[node]);
ll m=(l+r)/2;
ll q1=query1(2*node+1,l,m,ql,qr);
ll q2=query1(2*node+2,m+1,r,ql,qr);
if(q1==-1) return(q2);
if(q2==-1) return(q1);
return(q1^q2);
}
void update1(ll node,ll l,ll r,ll pos,ll val){
if(l==r){
a[l]=(a[l]^val);
tree1[node]=a[l];
return;
}
ll m=(l+r)/2;
if(pos>m) (update1(2*node+2,m+1,r,pos,val));
else (update1(2*node+1,l,m,pos,val));
recalculate1(node);
return;
}
int main(){
ll n,q;
cin>>n>>q;
forn(i,n) cin>>a[i];
build1(0,0,n-1);
forn(i,q){
ll t,x,y;
cin>>t>>x>>y;
if(t==1){
update1(0,0,n-1,x-1,y);
}
else{
//cout<<"---";
cout<<query1(0,0,n-1,x-1,y-1)<<endl;
}
}
//cout<<a[9]<<" ";
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define ll long long
#define F(type, i, a, b, incr) for (type i = a; i <= (type)(b); i += (type)(incr))
#define RF(type, i, a, b, decr) for (type i = a; i >= (type)(b); i -= (type)(decr))
#define sz(a) sizeof(a)
#define deb(a) cerr << " [" << #a << "->" << a << "] "
#define next_line cerr << '\n'
#define all(a) a.begin(), a.end()
#define iter(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define setbits(x) __builtin_popcountll(x)
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
void solve()
{
int n;
cin>> n;
string s, fir, sec;
vector<string> parts(2);
cin>>s;
F(int,i, 0, n - 1, 1)
fir += s[i];
F(int,i, n, 2*n - 1, 1)
sec += s[i];
parts[0] = fir;
parts[1] = sec;
int q, t = 0;
cin >> q;
while(q --){
int ty,a,b;
cin >> ty >> a >> b;
if(ty == 1){
a --;
b --;
int a_id = (a < n)?0:1;
if(a >= n)
a-=n;
int b_id = (b < n)?0:1;
if(b >= n)
b -= n;
char c = parts[a_id ^ t][a];
parts[a_id ^ t][a] = parts[b_id ^ t][b];
parts[b_id ^ t][b] = c;
}else{
t ^= 1;
}
// deb(parts[0]);deb(parts[1]);next_line;
}
cout << parts[t] + parts[t ^ 1];
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// freopen("Debug.txt", "w", stderr);
#else
#endif
// cout << fixed << setprecision(10);
// int _t;
// cin>>_t;
// F(int, i, 1, _t, 1){
// cout << "Case #" << i << ": ";
solve();
// }
}
| #include <stdio.h>
#include <bits/stdc++.h>
int main()
{
int i, N, A[2][131072];
scanf("%d", &N);
for (i = 1; i <= N; i++) scanf("%d", &(A[0][i]));
for (i = 1; i <= N; i++) scanf("%d", &(A[1][i]));
int j, k, l, r;
const long long inf = -(1LL << 60);
long long dp[2][131072];
for (i = 0; i <= N; i++) {
dp[0][i] = inf;
dp[1][i] = inf;
}
dp[0][1] = A[0][1];
dp[1][1] = A[1][1];
for (i = 2; i <= N; i++) {
if (i % 2 == 0) {
dp[0][0] = dp[0][1] + A[0][i];
if (dp[0][0] < dp[1][1] + A[1][i]) dp[0][0] = dp[1][1] + A[1][i];
} else dp[1][0] = dp[0][0];
k = (i < N - i)? i: N - i;
l = i % 2;
r = 1 - i % 2;
for (j = 2 - l; j <= k; j += 2) {
dp[0][j] = std::max(dp[0][j-1] + A[r][i], dp[0][j+1] + A[l][i]);
dp[1][j] = std::max(dp[1][j-1] + A[l][i], dp[1][j+1] + A[r][i]);
}
}
printf("%lld\n", dp[0][0]);
fflush(stdout);
return 0;
} |
#include<bits/stdc++.h>
// no of bits to store no 2e5 is 19
// 2e5 ka 5 times square root lene se 2 or 3 ke pass pahunch jate hai
using namespace std;
#define int long long
#define mod 1000000007
#define MOD 1000000007
#define MAX 1000000000
#define inf 1e18
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define mii map<int, int>
#define all(x) (x).begin(), (x).end()
#define fill(a,b) memset(a, b, sizeof(a))
#define sp(x,y) fixed<<setprecision(y)<<x
#define setbits(x) __builtin_popcount(x)
#define fast_in_out ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define rep(i,a,b) for(int i=a;i<b;i++)
#define ll long long
// powermod(a,b,m) == powermod(a,b%(m-1),m) when m is prime fermat's theorem a^(p-1) %p=1 p is prime
int powermod(int a,int b,int c) // gives (a^b)%c
{
int ans = 1,tempans=a%c;
while(b>0)
{
if(b&1==1)
{
ans=(ans*tempans)%c;
}
tempans=(tempans*tempans)%c;
b=b>>1;
}
return ans;
}
int modinverse(int a,int m) // gives a inverse wrt m and m is prime
{
return powermod(a,m-2,m);
}
ll add(ll x, ll y) {
x += y;
if (x >= mod) return x - mod;
return x;
}
ll sub(ll x, ll y) {
x -= y;
if (x < 0) return x + mod;
return x;
}
ll mult(ll x, ll y) {
return (x * y) % mod;
}
// my template m
// prime factorization pf
// ncr and factorials ncr
// matrix exponentiation of fibonacci series in log(n) matexpofib
// cycles in a graph cycle
// prime factorization in log(n) spf
void solve()
{
string s;
cin>>s;
int ans =0;
int n=s.length();
int a[26]={0};
for(int i=n-1;i>=1;i--)
{
if(s[i]==s[i-1])
{
int re= n-i-1;
int fr= a[s[i]-'a'];
ans+=(re-fr);
for(int j=0;j<26;j++)
a[j]=0;
a[s[i]-'a']=n-i;
}
else
{
a[s[i]-'a']++;
}
}
cout<<ans<<endl;
}
int32_t main()
{
fast_in_out;
int t=1;//cin>>t;
int count=0;
while(t--)
{
count++;
solve();
}
}
| #include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <tuple>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include<iomanip>
using namespace std;
using ll = long long;
using ULL = unsigned long long;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vb = std::vector<bool>;
using db = double;
using vdb = std::vector<db>;
using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending
using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending
using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending
using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending
template<class T>
using vv = std::vector<std::vector<T> >;
#define REPL(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FORL(i, 0, n)
#define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- )
#define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
#define INF pow(10, 10)
void change(ll id, vl &counts, ll &Times)
{
ll num = 0;
REP(i, 26)
{
if(i != id)
{
num += counts[i];
counts[i] = 0;
}
}
counts[id] += num;
Times += num;
}
int main(void)
{
ll N, K;
string Str;
cin >> Str;
ll Len = Str.size();
vl counts(26, 0);
ll Time_Max = 0;
MFOR(i, 1, Len)
{
ll id = Str[i]-'a';
if(Str[i] == Str[i-1])
{
change(id, counts, Time_Max);
// cout << i << " " << Str[i] << ", "<< Time_Max << endl;
counts[id] ++;
}
else
{
counts[id] ++;
}
}
cout << Time_Max << endl;
return 0;
}
|
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
char In[1 << 20], *ss = In, *tt = In;
#define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
ll read() {
ll x = 0, f = 1; char ch = getchar();
for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
return x * f;
}
enum col{
cB = 0,
cW = 1,
cR = 2
};
int readc() {
char ch = getchar();
for(; ch < 'A' || ch > 'Z'; ch = getchar());
return ch == 'B' ? cB : (ch == 'W' ? cW : cR);
}
const int MAXN = 400005;
int n, c[MAXN], ans;
int C(int n, int m) {
if(n < 0 || m < 0 || n < m) return 0;
if(m == 0) return 1;
else if(m == 1) return n;
else return n * (n-1) / 2 % 3;
}
int Lucas(int n, int m) {
if(m == 0) return 1;
return Lucas(n / 3, m / 3) * C(n % 3, m % 3) % 3;
}
int main() {
n = read();
for(int i = 1; i <= n; i++) c[i] = readc();
for(int i = 1; i <= n; i++)
(ans += c[i] * Lucas(n-1, i-1)) %= 3;
if(~n & 1) ans = (3 - ans) % 3;
printf(ans == cB ? "B\n" : (ans == cW ? "W\n" : "R\n"));
return 0;
}
| #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
using ld = long double;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define mp make_pair
int MOD = 1e9 + 7;
int mul(int a, int b) {
return (1LL * a * b) % MOD;
}
int add(int a, int b) {
int s = (a+b);
if (s>=MOD) s-=MOD;
return s;
}
int sub(int a, int b) {
int s = (a+MOD-b);
if (s>=MOD) s-=MOD;
return s;
}
int po(int a, ll deg)
{
if (deg==0) return 1;
if (deg%2==1) return mul(a, po(a, deg-1));
int t = po(a, deg/2);
return mul(t, t);
}
int inv(int n)
{
return po(n, MOD-2);
}
mt19937 rnd(time(0));
/*const int LIM = 1e6;
vector<int> facs(LIM), invfacs(LIM);
void init()
{
facs[0] = 1;
for (int i = 1; i<LIM; i++) facs[i] = mul(facs[i-1], i);
invfacs[LIM-1] = inv(facs[LIM-1]);
for (int i = LIM-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}
int C(int n, int k)
{
if (n<k) return 0;
if (n<0 || k<0) return 0;
return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}*/
/*
struct DSU
{
vector<int> sz;
vector<int> parent;
void make_set(int v) {
parent[v] = v;
sz[v] = 1;
}
int find_set(int v) {
if (v == parent[v])
return v;
return find_set(parent[v]);
}
void union_sets(int a, int b) {
find_set(a);
find_set(b);
a = find_set(a);
b = find_set(b);
if (a != b) {
if (sz[a] < sz[b])
swap(a, b);
parent[b] = a;
sz[a] += sz[b];
};
}
DSU (int n)
{
parent.resize(n);
sz.resize(n);
for (int i = 0; i<n; i++) make_set(i);
}
};*/
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n; cin>>n;
string s; cin>>s;
map<char, int> m;
m['B']=0;
m['R']=1;
m['W']=2;
map<int, char> m1;
for (auto it: m) m1[it.second]=it.first;
vector<int> facs(n), degs(n);
facs[0]=1;
degs[0]=0;
for (int i = 1; i<n; i++)
{
facs[i] = facs[i-1];
degs[i] = degs[i-1];
int i1 = i;
while (i1%3==0)
{
i1/=3; degs[i]++;
}
facs[i] = (facs[i]*i1)%3;
}
int tot = 0;
for (int i = 0; i<n; i++)
{
int C;
int deg = degs[n-1] - degs[i] - degs[n-i-1];
if (deg>0) C=0;
else C = (facs[n-1]*facs[i]*facs[n-i-1])%3;
tot+=m[s[i]]*C; tot%=3;
//cout<<degs[i]<<' '<<C<<' '<<tot<<endl;
}
if (n%2==0) tot = (3-tot)%3;
//cout<<tot<<endl;
cout<<m1[tot];
}
/*
W
BR
RWB
RRBB
-a1-3a2-3a3-a4
-4 = 2
*/ |
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define f(i,a,n) for(int i=a ; i<n ; i++)
#define rf(i,n,a) for(int i=n ; i>=a ; i--)
#define F first
#define S second
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;
void solve() {
int n;
cin >> n;
int ans = 0;
f(i,1,1e6+1){
int len = log10(i) + 1;
int cur = 1;
f(j,0,len) cur*=10;
if(cur*i + i <= n){
ans++;
}
}
cout << ans << "\n";
}
int32_t main() {
fast;
int t = 1;
// cin >> t;
while (t--) solve();
} | #include<bits/stdc++.h>
using namespace std;
int main() {
long long int n,count=0;
cin>>n;
for(int i=1;i<=1000000;i++){
string s=to_string(i);
string r=s+s;
long long int res=stoll(r);
count++;
if(res>n){
break;
}
}
cout<<count-1<<endl;
return 0;
} |
/*Jai Shree Ram*/
// Never Give Up
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define F(i,s,e,j) for(int i=s;i<=e;i+=j)
#define mt19937 rng(chrono::steady_clock::now().tjhe_since_epoch().count());
//shuffle(arr,arr+n,rng)
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
vector<vi> graph[100001];
int32_t main()
{ c_p_c();
int n,m,x,y;
cin>>n>>m>>x>>y;
vi vt(3);
for(int i=0;i<m;i++)
{
int a,b,t,k;
cin>>a>>b>>vt[1]>>vt[2];
vt[0] = b;
graph[a].pb(vt);
vt[0] = a;
graph[b].pb(vt);
}
priority_queue<pii,vector<pii>,greater<pii> > pq;
pq.push(mp(0,x));
vector<bool> vis(n+1,false);
int ans = -1;
while(!pq.empty())
{
pii node = pq.top();
pq.pop();
if(vis[node.ss])
continue;
vis[node.ss] = true;
if(node.ss==y)
{
ans = node.ff;
break;
}
for(vi v:graph[node.ss])
{
if(!vis[v[0]])
{
int t = node.ff;
t += node.ff%v[2]==0?0:v[2]-node.ff%v[2];
t += v[1];
pq.push(mp(t,v[0]));
}
}
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(a) int((a).size())
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1e9 + 7;
template<class T, class U> inline void add_self(T &a, U b){a += b;if (a >= mod) a -= mod;if (a < 0) a += mod;}
template<class T, class U> inline void min_self(T &x, U y) { if (y < x) x = y; }
template<class T, class U> inline void max_self(T &x, U y) { if (y > x) x = y; }
#define _deb(x) cout<<x;
void _print() {cerr << "]\n";} template <typename T, typename... V>void _print(T t, V... v) {_deb(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
template <class T, class U> void print_m(const map<T,U> &m, int w=3){if(m.empty()){cout<<"Empty"<<endl; return;}for(auto x: m)cout<<"("<<x.first<<": "<<x.second<<"),"<<endl;cout<<endl;}
template<class T, class U>void debp(const pair<T, U> &pr, bool end_line=1){cout<<"{"<<pr.first<<" "<<pr.second<<"}"; cout<<(end_line ? "\n" : ", ");}
template <class T> void print_vp(const T &vp, int sep_line=0){if(vp.empty()){cout<<"Empty"<<endl; return;}if(!sep_line) cout<<"{ ";for(auto x: vp) debp(x,sep_line);if(!sep_line) cout<<"}\n";cout<<endl;}
template <typename T>void print(const T &v, bool show_index = false){int w = 2;if(show_index){for(int i=0; i<sz(v); i++)cout<<setw(w)<<i<<" ";cout<<endl;}for(auto &el: v) cout<<setw(w)<<el<<" ";cout<<endl;}
template <typename T>void print_vv(const T &vv){if(sz(vv)==0) {cout<<"Empty"<<endl; return;} int w = 3;cout<<setw(w)<<" ";for(int j=0; j<sz(*vv.begin()); j++)cout<<setw(w)<<j<<" ";cout<<endl;int i = 0;for(auto &v: vv){cout<<i++<<" {";for(auto &el: v) cout<<setw(w)<<el<<" ";cout<<"},\n";}cout<<endl;}
template <typename T> ostream& operator<<(ostream &os, const vector<T> &v){print(v); return os;};
template <typename T> ostream& operator<<(ostream &os, const vector<vector<T>> &vv){print_vv(vv); return os;};
template <class T, class U> ostream& operator<<(ostream &os, const map<T,U> &m){print_m(m); return os;};
template <class T, class U> ostream& operator<<(ostream &os, const pair<T, U> &pr){debp(pr); return os;};
template <class T, class U> ostream& operator<<(ostream &os, const vector<pair<T, U>> &vp){ print_vp(vp); return os;};
typedef tuple<ll,ll,ll> tii; // ad, ti, ki
const ll inf = 1e18L;
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n, m;
while(cin>>n>>m){
ll st, ed; cin>>st>>ed;
vector<vector<tii>> adj(n+1);
forn(i,m){
ll x,y,t,k; cin>>x>>y>>t>>k;
adj[x].pb({y,t,k});
adj[y].pb({x,t,k});
}
vector<bool> vis(n+1);
vl dist(n+1,inf);
priority_queue<pll,vector<pll>, greater<pll>> pq; // {time, node}
dist[st] = 0;
pq.push({0,st});
while(!pq.empty()){
auto [d, node] = pq.top(); pq.pop();
if(d>dist[node]) continue;
vis[node] = 1;
for(auto [ad, t, k]: adj[node]){
if(vis[ad]) continue;
ll depart = ((d+k-1)/k)*k;
ll reach = depart + t;
// deb(node,d,depart,reach,k);
if(reach<dist[ad]){
dist[ad] = reach;
pq.push({reach,ad});
}
}
// if(node==ed) break;
}
ll ans = dist[ed];
cout<<(ans>=inf ? -1 : ans)<<"\n";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
ll x;
cin>>n>>x;
vector<ll> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
vector<ll> b(n),c(n);
for(int i=n-1;i>=0;i--){
b[i]=x/a[i];
if(i<n-1)c[i]=a[i+1]/a[i];
x-=a[i]*b[i];
}
vector<vector<ll>> dp(n,vector<ll>(2));
dp[0][0]=1;
dp[0][1]=0;
for(int i=1;i<n;i++){
if(b[i-1]==0){
dp[i][0]=dp[i-1][0]+dp[i-1][1];
dp[i][1]=dp[i-1][1];
}else if(b[i-1]==c[i-1]-1){
dp[i][0]=dp[i-1][0];
dp[i][1]=dp[i-1][0]+dp[i-1][1];
}else{
dp[i][0]=dp[i-1][0]+dp[i-1][1];
dp[i][1]=dp[i-1][0]+dp[i-1][1];
}
}
cout<<dp[n-1][0]+dp[n-1][1]<<endl;
} | // abc182_f
#include <bits/stdc++.h>
// #include "../cxx-prettyprint/prettyprint.hpp"
// #include <iostream>
#ifdef LOCAL
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()
#define INF (ll)(1e9)
#define MOD (1000000007)
#define print2D(h, w, arr) \
REP(i, h) \
{ \
REP(j, w) \
cout << arr[i][j] << " "; \
cout << endl; \
}
#define print_line(vec, n) \
{ \
for (int idx = 0; idx < (n - 1); idx++) \
cout << (vec)[idx] << " "; \
cout << (vec)[(n)-1] << endl; \
}
template <class T>
void print(const T &x)
{
cout << x << "\n";
}
template <class T, class... A>
void print(const T &first, const A &... rest)
{
cout << first << " ";
print(rest...);
}
struct PreMain
{
PreMain()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} premain;
int main()
{
#ifdef LOCAL
ifstream in("../arg.txt");
cin.rdbuf(in.rdbuf());
#endif
ll N, X;
cin >> N >> X;
vector<ll> A(N);
REP(i, N)
cin >> A[i];
vector<ll> B(N - 1);
REP(i, N - 1)
{
B[i] = A[i + 1] / A[i];
}
vector<map<ll, ll>> dp(N, map<ll, ll>());
REP_REV(i, N)
{
ll res = X % A[i];
if (i == N - 1)
{
dp[i][res] += 1;
dp[i][res - A[i]] += 1;
}
else
{
for (auto d : dp[i + 1])
{
if ((abs(d.first - res) % A[i] == 0) && ((abs(d.first - res) / A[i]) < B[i]))
{
dp[i][res] += d.second;
}
if (res == 0)
continue;
if ((abs(d.first - res + A[i]) % A[i] == 0) && ((abs(d.first - res + A[i]) / A[i]) < B[i]))
{
dp[i][res - A[i]] += d.second;
}
}
}
// print(dp[i]);
}
ll ans = 0;
for (auto d : dp[0])
{
if (d.first == 0)
{
ans += d.second;
}
}
print(ans);
// vector<int> ans2;
// REPN(y, X, X+A.back()+1){
//
// bool valid = true;
// ll yy = y;
// vector<int> C(N);
// REP_REV(i, N){
// C[i] = yy / A[i];
// yy %= A[i];
// }
//
// yy = y - X;
// REP_REV(i, N){
// if (C[i] > 0 && yy / A[i]){
// valid = false;
// break;
// }
// yy %= A[i];
// }
//
// if (valid){
// ans2.emplace_back(y);
// }
//
// }
//
// print(ans2.size());
// print(ans2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long C(int n, int r){
__int128 ans = 1;
for(int i=n;i>n-r;i--){
ans=ans*i;
}
for(int i=1;i<r+1;i++){
ans = ans/i;
}
return ans;
}
int main(){
int L;
cin >> L;
cout << C((L-1),11) << endl;
} | #include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a) memset(a, 0, sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
typedef __int128_t int128;
const int maxn = 1e3 + 10;
char a[maxn];
char b[] = "atcoder";
int c[30];
int main()
{
//cout << ("atcodeer" < "atcoder") << endl;
IOS;
int t;
cin >> t;
while (t--)
{
int ans = -1;
cin >> a;
int len = strlen(a);
if (strcmp(a,b)>0)
ans = 0;
else
for (int i = 0; i < len; i++)
{
if(a[i]=='a')
continue;
if(a[i]<='t')
{
ans = i;
break;
}
else
{
ans = i - 1;
break;
}
}
cout << ans << endl;
}
return 0;
} |
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>
#include <locale>
#include <omp.h>
using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "YES" << endl; \
} \
else \
{ \
cout << "NO" << endl; \
}
#define IFYes(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "Yes" << endl; \
} \
else \
{ \
cout << "No" << endl; \
}
#define IFyes(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "yes" << endl; \
} \
else \
{ \
cout << "no" << endl; \
}
#define DEBUG_OUTPUT_ARRAY(XXX, ONE) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
}
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << "<<< i = " << i << " >>>" << endl; \
for (int j = 0; j < (TWO); j++) \
{ \
cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
} \
}
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << i << " "; \
for (int j = 0; j < (TWO); j++) \
{ \
cout << XXX[i][j] << " "; \
} \
cout << endl; \
}
typedef pair<long long int, long long int> pll;
typedef pair<long long int, pll> lpll;
const long long int mod = 998244353;
const long long int INF = 1e16;
const long double PI=3.14159265358979323;
const long long int pl=1100;
const long double eps =0.0000001;
long long int N,M,K,A[200200],check[200200]={},res=0,sum=0;
long double dp[200200]={},a[200200]={},b[200200]={};
string S;
bool wana[200200]={};
pll op[200200];
pair<pll,pll>mode[200200];
int main()
{
cout << fixed << setprecision(18);
cin>>N>>M>>K;
for (long long int i = 1; i <= K; i++)
{
cin>>A[i];
wana[A[i]]=true;
check[A[i]]++;
b[A[i]]=1.0;
}
for (long long int i = 1; i <= N; i++)
{
check[i]+=check[i-1];
}
for (long long int i = 1; i <= N; i++)
{
if(check[M+i]-check[i-1]==M){
cout<<-1<<endl;return 0;
}
}
long double tmp=0.0,tmpb=0.0;
for (long long int i = N-1; i >=0; i--)
{
tmp+=(a[i+1]-a[i+M+1]);
tmpb+=(b[i+1]-b[i+M+1]);
if(b[i]<=0.99999999)a[i]=tmp/M+1.0;
if(b[i]<=0.99999999)b[i]=tmpb/M;
}
cout<<a[0]/(1.0-b[0])<<endl;
}
| // #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#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 <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
void dump_func()
{
cerr << endl;
}
template <class Head, class... Tail>
void dump_func(Head &&h, Tail &&... t)
{
cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...);
}
#define dump(...) \
cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), \
cerr << "*/\n\n";
typedef long long ll;
template <class T>
using V = vector<T>;
template <class T>
using P = pair<T, T>;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(20);
int a, b, c;
cin >> a >> b >> c;
cout << (a * a + b * b < c * c ? "Yes" : "No") << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
long long N, L = 0;
long long ans = 0;
cin >> N;
vector<long long> vec(N);
for (int i = 0; i < N; i++){
int x;
cin >> x;
vec.at(i) = x;
}
sort(vec.begin(), vec.end());
for (int i = 0; i < N; i++){
if( vec.at(L) != vec.at(i) ){
ans += (i - L) * ( N - i );
L = i;
}
}
cout << ans << endl;
} | using namespace std;
#include "bits/stdc++.h"
#define len(X) (int)(X).size()
#define all(X) (X).begin(), (X).end()
#define elif else if
#define ceil(x,y) (x + y-1)/y
#define int long long
int32_t main() { cin.tie(nullptr)->sync_with_stdio(false);
int n;cin>>n;
map<int,int> m;
for(int a,i {}; i < n; ++i) cin>>a,m[a]++;
// for(auto i : m) cout << i.second << ' ';
// cout << '\n';
vector<int> v;
for(auto &i : m) v.push_back(i.second);
vector<int> suff=v;
for(int i = len(v)-2; i >= 0; --i) suff[i] += suff[i+1];
// for(auto i : suff) cout << i << ' ';
// cout << '\n';
int ans{};
for(int i {}; i < len(v)-1; ++i) {
int res = v[i]; //res*suffix
res *= suff[i+1];
ans += res;
}
cout << ans << '\n';
return 0;
}
// taskkill /IM "3.exe" /F
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
int main(){
int n, q, t, e, x, l, r;
cin >> n;
vector<vector<int>> g(n);
int a[n-1], b[n-1];
rep(i,n-1) {
cin >> a[i] >> b[i];
a[i]--; b[i]--;
g[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]);
}
vector<ll> par(n), val(n);
par[0] = -1;
auto dfs1 = [&](auto self, int v, int p) -> void {
for(auto x : g[v]) {
if(x == p) continue;
par[x] = v;
self(self,x,v);
}
};
dfs1(dfs1,0,-1);
cin >> q;
rep(i,q) {
cin >> t >> e >> x;
e--;
l = a[e], r = b[e];
if(t == 2) swap(l,r);
if(par[l] == r) val[l] += x;
else val[0] += x, val[r] -= x;
}
auto dfs2 = [&](auto self, int v, int p) -> void {
for(auto x : g[v]) {
if(x == p) continue;
val[x] += val[v];
self(self,x,v);
}
};
dfs2(dfs2,0,-1);
rep(i,n) cout << val[i] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
priority_queue < pair < ll, int > > q;
int n, m, tot, hd[100005], vis[100005], to[200005], nxt[200005], c[200005], d[200005];
ll dis[100005];
int read()
{
int x = 0, fl = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') fl = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0'; ch = getchar();}
return x * fl;
}
void add(int x, int y, int c0, int d0)
{
tot ++ ;
to[tot] = y;
nxt[tot] = hd[x];
c[tot] = c0;
d[tot] = d0;
hd[x] = tot;
return;
}
ll s(ll x, ll y)
{
if (x <= 0) return 1e18;
return x + y / x;
}
void Dij()
{
for (int i = 2; i <= n; i ++ )
dis[i] = 1e18;
q.push(make_pair(0, 1));
while (!q.empty())
{
pair < int, int > h = q.top(); q.pop();
int x = h.second;
if (vis[x]) continue; vis[x] = 1;
for (int i = hd[x]; i; i = nxt[i])
{
int y = to[i], c0 = c[i], d0 = d[i], p0, p = (int)(sqrt(d0));
ll r1 = s(p - 1, d0), r2 = s(p, d0), r3 = s(p + 1, d0), z, tmp;
if (r1 <= r2 && r1 <= r3) p0 = p - 1; else if (r2 <= r1 && r2 <= r3) p0 = p; else p0 = p + 1;
// printf("%lld %lld %lld %d %d\n", r1, r2, r3, p, p0);
if (dis[x] < p0 - 1) tmp = (ll)(s(p0, d0) + c0 - 1); else tmp = (ll)(s(dis[x] + 1, d0) + c0 - 1);
// printf("%d %d %lld\n", x, y, tmp);
if (dis[y] > tmp)
{
dis[y] = tmp;
q.push(make_pair(-dis[y], y));
}
}
}
return;
}
int main()
{
n = read(); m = read();
for (int i = 1; i <= m; i ++ )
{
int x = read(), y = read(), c0 = read(), d0 = read();
add(x, y, c0, d0), add(y, x, c0, d0);
}
Dij();
printf("%lld\n", dis[n] == 1e18 ? -1 : dis[n]);
return 0;
} |
// In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef string str;
const ll maxn = 105, inf = 2e18;
ll n, s[maxn];
ld ans, x = 1, y;
bool mark[maxn];
vector <ll> adj[maxn];
void dfs(ll u){
mark[u] = 1;
s[u] ++;
for(ll j : adj[u]){
if(!mark[j])dfs(j);
}
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(ll i = 1; i <= n; i ++){
for(ll j = 1; j <= n; j ++){
char c;
cin >> c;
if(c == '1')adj[i].pb(j);
}
}
for(ll i = 1; i <= n; i ++){
dfs(i);
memset(mark, 0, sizeof mark);
}
for(ll i = 1; i <= n; i ++){
y = s[i];
ans += x / y;
}
cout << fixed << setprecision(10) << ans << endl;
}
| //#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
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; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int main(){
int N;
string S[102];
int A[102][102];
scan(N);
rep(i,N) scan(S[i]);
fill(A[0],A[N],INF);
rep(i,N) rep(j,N) if(i==j||S[i][j]=='1') A[i][j]=0;
rep(k,N){
rep(i,N){
rep(j,N){
if(k==i||k==j) continue;
if(A[i][j]>A[i][k]+A[k][j]) A[i][j]=A[i][k]+A[k][j];
}
}
}
int ikeru[101]={};
rep(i,N){
rep(j,N){
if(A[i][j]==0) ikeru[j]++;
}
}
double ans=0;
rep(i,N) ans+=1.0/ikeru[i];
prin(setprecision(11)<<ans);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int tree[4 * N], n, a[N];
void built(int l = 1, int r = n, int pos = 1) {
if (l == r) {
tree[pos] = a[l];
return;
}
int m = (l + r) / 2;
built(l, m, 2 * pos);
built(m + 1, r, 2 * pos + 1);
tree[pos] = tree[2 * pos] ^ tree[2 * pos + 1];
}
void udt(int id, int val, int l = 1, int r = n, int pos = 1) {
if (l == r) {
tree[pos] ^= val;
return;
}
int m = (l + r) / 2;
if (id <= m) udt(id, val, l, m, 2 * pos);
else udt(id, val, m + 1, r, 2 * pos + 1);
tree[pos] = tree[2 * pos] ^ tree[2 * pos + 1];
}
int get(int tl, int tr, int l = 1, int r = n, int pos = 1) {
if (tl > r || l > tr) return 0;
if (tl <= l && r <= tr) return tree[pos];
int m = l + r >> 1;
int k1 = get(tl, tr, l, m, 2 * pos);
int k2 = get(tl, tr, m + 1, r, 2 * pos + 1);
return k1 ^ k2;
}
void solve(){
int q;
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> a[i];
built();
while (q--) {
int type, x, y;
cin >> type >> x >> y;
if (type & 1) udt(x, y);
else cout << get(x, y) << '\n';
}
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
solve();
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define ii pair<int,int>
#define vii vector<ii>
#define vi vector<int>
#define pi 2*acos(0.0)
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define FORE(x, a) for (auto& x: a)
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
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...);}
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template <class T> struct Tree {
static const T LOW = INT_MIN;
T f(T a, T b) { return a^b; } // any associative func
vector<T> s; int n; // main segtree vector, size of init vector
Tree(int n = 0) : s(2*n, 0), n(n) {}
void update(int pos, T val) { // s[pos] = val
for (s[pos += n] = val; pos > 1; pos /= 2)
s[pos / 2] = f(s[pos & ~1], s[pos | 1]);
}
T query(int b, int e) { // query [b, e)
T ra = LOW, rb = LOW;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b&1) ra = f(ra, s[b++]);
if (e&1) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
// kactl
void solve(){
int n,q; cin >> n >> q;
vector<int> a(n);
FOR(n) cin >> a[i];
Tree<int> st(n);
FOR(n) st.update(i,a[i]);
FOR(q){
int t,x,y; cin >> t >> x >> y;
--x; --y;
if(t == 1){
a[x] ^= (y+1);
st.update(x,a[x]);
} else{
cout << st.query(x,y+1) << '\n';
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
//typedef
typedef unsigned int UINT;
typedef unsigned long long ULL;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> PLL;
typedef tuple<LL, LL, LL> TLL3;
typedef tuple<LL, LL, LL, LL> TLL4;
typedef set<LL, greater<LL> > setdownLL;
#define PQ priority_queue
typedef PQ<LL, vector<LL>, greater<LL> > pqupLL;
//container utill
#define ALL(v) (v).begin(),(v).end()
#define CR [](auto element1, auto element2){return element1>element2;}
#define LB lower_bound
#define UP upper_bound
#define PB push_back
#define MP make_pair
#define MT make_tuple
//constant
#define PI 3.141592653589793
const LL MOD=1000000007;
void solve(LL N, LL A, LL B){
if(A+B>N){
cout << 0 << endl;
return;
}
if(A<B) swap(A,B);
LL ans=0;
LL val,val2;
//0
ans=N-A+1LL;
ans%=MOD;
ans*=ans;
ans%=MOD;
val2=N-B+1LL;
val2%=MOD;
ans*=val2;
ans%=MOD;
val2=N-B+1LL;
val2%=MOD;
ans*=val2;
ans%=MOD;
//1st
val=N-A+1LL;
val%=MOD;
val*=val;
val%=MOD;
val2=A-B+1LL;
val2%=MOD;
val*=val2;
val%=MOD;
val*=val2;
val%=MOD;
ans+=(MOD-val);
ans%=MOD;
//2nd
val=((B-1LL)*B)/2LL;
val%=MOD;
val2=(B-1LL)*(N-A-B+1LL);
val2%=MOD;
val+=val2;
val%=MOD;
val2=A-B+1;
val*=val2;
val%=MOD;
val2=N-A+1;
val2%=MOD;
val*=val2;
val%=MOD;
val*=4LL;
val%=MOD;
ans+=(MOD-val);
ans%=MOD;
//3rd 1
val=((B-1LL)*B)/2LL;
val%=MOD;
val*=val;
val%=MOD;
val*=4LL;
val%=MOD;
ans+=(MOD-val);
ans%=MOD;
//3rd 2
val=B-1;
val%=MOD;
val2=N-A-B+1;
val2%=MOD;
val*=val2;
val%=MOD;
val2=B-1;
val2%=MOD;
val*=val2;
val%=MOD;
val*=B;
val%=MOD;// 2/2=1
val*=4LL;
val%=MOD;
ans+=(MOD-val);
ans%=MOD;
//3rd 3
val=N-A-B+1;
val%=MOD;
val*=val;
val%=MOD;
val2=B-1;
val2%=MOD;
val*=val2;
val%=MOD;
val*=val2;
val%=MOD;
val*=4LL;
val%=MOD;
ans+=(MOD-val);
ans%=MOD;
//output
cout << ans << endl;
}
int main(){
LL T;
cin >> T;
LL N,A,B;
LL t;
for(t=0; t<T; t++){
cin >> N >> A >> B;
solve(N,A,B);
}
system("pause");
return 0;
} | // 問題の URL を書いておく
//
#include <bits/stdc++.h>
using namespace std;
//#define ENABLE_PRINT
#if defined(ENABLE_PRINT)
#define Print(v) \
do {\
cout << #v << ": " << v << endl; \
}while(0)
#define PrintVec(v) \
do {\
for(int __i = 0; __i < v.size(); ++__i) \
{ \
cout << #v << "[" << __i << "]: " << v[__i] << endl; \
}\
}while(0)
#else
#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)
#endif
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
using ll = int64_t;
template<int Mod>
struct MintBase {
int64_t x; // typedef long long int64_t;
MintBase(int64_t x=0):x((x%Mod+Mod)%Mod){}
MintBase operator-() const { return MintBase(-x);}
MintBase& operator+=(const MintBase a) {
if ((x += a.x) >= Mod) x -= Mod;
return *this;
}
MintBase& operator-=(const MintBase a) {
if ((x += Mod-a.x) >= Mod) x -= Mod;
return *this;
}
MintBase& operator*=(const MintBase a) { (x *= a.x) %= Mod; return *this;}
MintBase operator+(const MintBase a) const { return MintBase(*this) += a;}
MintBase operator-(const MintBase a) const { return MintBase(*this) -= a;}
MintBase operator*(const MintBase a) const { return MintBase(*this) *= a;}
MintBase pow(int64_t t) const {
if (!t) return 1;
MintBase a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime Mod
MintBase inv() const { return pow(Mod-2);}
MintBase& operator/=(const MintBase a) { return *this *= a.inv();}
MintBase operator/(const MintBase a) const { return MintBase(*this) /= a;}
};
//const int Mod = 1000000007;
//const int Mod = 998244353;
using mint = MintBase<1000000007>;
void solve()
{
ll n, a, b;
cin >> n >> a >> b;
mint amax = mint((n - a + 1) * (n - a + 1));
mint bmax = mint((n - b + 1) * (n - b + 1));
auto total = amax * bmax;
Print(total.x);
auto x = (n - a - b + 1);
Print(x.x);
auto nonOverlapX = (n - a - b >= 0) ? ((1 + x) * x) : 0;
Print(nonOverlapX.x);
mint ab = mint(n - a + 1) * mint(n - b + 1);
auto overlapX = ab - nonOverlapX;
auto overlap = overlapX * overlapX;
Print(overlap.x);
auto ans = total - overlap;
cout << ans.x << endl;
}
int main(int, const char**)
{
ll t;
cin >> t;
rep(i, t)
{
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");}
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define debugbit(x, n) cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
//#define int long long
//using mint = atcoder::modint;
typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;
void solve(long long x){
cout << ((x >= 0) ? x : 0) << endl;
}
signed main(){
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
long long x;
scanf("%lld",&x);
solve(x);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<pair<ll,ll> > vpl;//追加はmake_pairを使う
typedef long double ld;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqg; //昇順のPriority queue
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define all(v) v.begin(), v.end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0;}
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0;}
#define dbl(i) fixed << setprecision(15) << i << endl;
#define GET_VARIABLE_NAME(Variable) (#Variable)
#define dbc(a) cout << GET_VARIABLE_NAME(a) << "=" << a << endl;
#define vsum(v) accumulate(all(v), 0);
#define rsort(v) sort(v.begin(),v.end(),greater<ll>());
bool IsPrime(int num)
{
if (num < 2) return false;
else if (num == 2) return true;
else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2)
{
if (num % i == 0)
{
// 素数ではない
return false;
}
}
// 素数である
return true;
}
vector<ll> enum_divisors(ll N) {//約数列挙
vector<ll> res;
for (ll i = 1; i * i <= N; ++i) {
if (N % i == 0) {
res.push_back(i);
// 重複しないならば i の相方である N/i も push
if (N/i != i) res.push_back(N/i);
}
}
// 小さい順に並び替える
sort(res.begin(), res.end());
return res;
}
vector<pair<long long, long long> > prime_factorize(long long N) {//因数分解
vector<pair<long long, long long> > res;
for (long long a = 2; a * a <= N; ++a) {
if (N % a != 0) continue;
long long ex = 0; // 指数
// 割れる限り割り続ける
while (N % a == 0) {
++ex;
N /= a;
}
// その結果を push
pair <ll,ll> push;
push.first=a;
push.second=ex;
res.push_back(push);
}
// 最後に残った数について
if (N != 1){
pair <ll,ll> push;
push.first=N;
push.second=1;
res.push_back(push);
}
return res;
}
ll gcd(ll a, ll b){//最大公約数
if (a%b == 0){
return(b);
}else{
return(gcd(b, a%b));
}
}
ll lcm(ll a, ll b){//最小公倍数
return a / gcd(a, b) * b;
}
ll comb(ll n, ll r) {
if(n<r) return 0;
if ( r * 2 > n ) r = n - r;
ll dividend = 1;
ll divisor = 1;
for ( ll i = 1; i <= r; ++i ) {
dividend *= (n-i+1);
divisor *= i;
}
return dividend / divisor;
}
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==0){ x=1;y=0; }
else{ exgcd(b,a%b,y,x);y-=a/b*x; }
}
ll inv(ll a,ll Mod){//modにおけるaの逆元
ll x,y;
exgcd(a,Mod,x,y);
return (x%Mod+Mod)%Mod;
}
ll RepeatSquaring(ll N, ll P, ll M){
if(P==0) return 1;
if(P%2==0){
ll t = RepeatSquaring(N, P/2, M);
return t*t % M;
}
return N * RepeatSquaring(N, P-1, M);
}
ll rui(ll a,ll n){
ll x=1;
while(n>0){
if(n&1){
x=x*a;
}
a=a*a;
n>>=1;
}
return x;
}
ll f(ll n, vector <string>s){
if(s[n]=="OR"){
if(n==0)return 3;
return rui(2,n+1)+f(n-1,s);
}else{
if(n==0)return 1;
return f(n-1,s);
}
}
int main(){
ll n;
cin>>n;
vector <string>s(n);
rep(i,n) cin>>s[i];
ll ans=f(n-1,s);
cout<<ans<<endl;
} |
#include <iostream>
using namespace std;
int main() {
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
if (sy < 0) {
if (gy < 0) {
gy = -gy;
}
} else {
if (gy > 0) {
gy = -gy;
}
}
double a = (double)(gy - sy) / (gx - sx);
double b = sy - a * sx;
double x = (0.0 - b) / a;
printf("%.7f\n", x);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,-1,0,1,1,-1,-1,1};
ll dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
if(a>b){
a = b; return true;
}
else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
if(a<b){
a = b; return true;
}
else return false;
}
template<typename T> inline void print(T &a){
int sz = a.size();
for(auto itr = a.begin(); itr != a.end(); itr++){
cout << *itr;
sz--;
if(sz) cout << " ";
}
cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;
int main(){
int sx,sy,gx,gy; cin >> sx >> sy >> gx >> gy;
double t = abs(sy) / (double)abs(sy+gy);
double ans = t * (gx-sx) + sx;
printf("%.12f\n",ans);
} |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
template<class T> struct Fenwick {
int n;
T* d;
Fenwick() : n(0), d(NULL) {}
Fenwick(int n_) : n(n_) {
d = new T[n_]();
}
Fenwick(const Fenwick &y) : n(y.n) {
d = new T[n];
memcpy(d, y.d, sizeof (T) * n);
}
~Fenwick() {
delete[] d; d = NULL;
n = 0;
}
friend void swap(Fenwick &x, Fenwick &y) {
swap(x.n, y.n); swap(x.d, y.d);
}
Fenwick& operator=(Fenwick y) {
swap(*this, y);
return *this;
}
inline void add(int i, const T &x) {
for (; i<n; i|=i+1) d[i] += x;
}
inline T sum(int r) const {
T s = T();
for (; r; r&=r-1) s += d[r-1];
return s;
}
T sum(int l, int r) const {
return sum(r) - sum(l);
}
int lower_bound(const T &x) const { // sum(ret) < x <= sum(ret+1);
if (n == 0) return 0;
int i = 0; T s = T();
for (int k=1<<__lg(n); k; k>>=1) {
if (i+k <= n && s + d[i+k-1] < x) {
i += k; s += d[i-1];
}
}
return i;
}
int upper_bound(const T &x) const { // sum(ret) <= x < sum(ret+1);
if (n == 0) return 0;
int i = 0; T s = T();
for (int k=1<<__lg(n); k; k>>=1) {
if (i+k <= n && !(x < s + d[i+k-1])) {
i += k; s += d[i-1];
}
}
return i;
}
};
int H, W, M;
int R[200011], C[200011];
int idx[200011];
bool use[200011];
bool act[200011];
void MAIN() {
scanf("%d%d%d", &H, &W, &M);
REP (i, M) scanf("%d%d", R+i, C+i), R[i]--, C[i]--;
LL ans = 0;
if (M == 0) {
ans = (LL) H * W;
} else {
int minR = H;
int minC = W;
REP (i, M) {
if (R[i] == 0) amin(minC, C[i]);
if (C[i] == 0) amin(minR, R[i]);
}
ans += (LL)H * W;
ans -= W - minC;
ans -= H - minR;
REP (i, M) idx[i] = i;
sort(idx, idx + M, [&](int i, int j) {
return C[i] < C[j];
});
for (int i=minR; i<H; i++) use[i] = true;
REP (i, M) {
if (!use[R[idx[i]]]) {
use[R[idx[i]]] = true;
act[idx[i]] = true;
}
}
Fenwick<LL> F(H+11);
LL bad = 0;
int p = M-1;
for (int i=W-1; i>0; i--) {
int nxt = H;
int q = p;
while (q >= 0 && C[idx[q]] == i) {
q--;
}
for (int k=p; k>q; k--) {
int r = R[idx[k]];
int c = C[idx[k]];
amin(nxt, r);
}
if (minC <= i) nxt= 0;
F.add(nxt, 1);
for (int k=p; k>q; k--) {
int r = R[idx[k]];
int c = C[idx[k]];
// [c, Right[r]);
if (r != 0 && act[idx[k]]) {
bad += F.sum(0, r+1);
}
}
p = q;
}
for (int i=minR; i<H; i++) {
bad += F.sum(0, i+1);
}
ans -= bad;
}
printf("%lld\n", ans);
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| #include <vector>
template <typename T, typename F>
class segment_tree {
public:
segment_tree(int n, const F& f, const T& e)
: f(f), e(e)
{
sz = 1;
while (sz < n) {
sz *= 2;
}
dat.assign(2 * sz, e);
}
void update(int i, const T& x) {
i += sz;
dat[i] = x;
while (i > 1) {
i /= 2;
dat[i] = f(dat[2 * i + 0], dat[2 * i + 1]);
}
}
T query(int i) const {
return dat[i + sz - 1];
}
T query(int a, int b) const {
T l = e;
T r = e;
a += sz;
b += sz;
for (; a < b; a /= 2, b /= 2) {
if (a & 1) {
l = f(l, dat[a]);
++a;
}
if (b & 1) {
--b;
r = f(dat[b], r);
}
}
return f(l, r);
}
private:
F f;
T e;
int sz;
std::vector<T> dat;
};
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int H, W, M;
cin >> H >> W >> M;
vector<int> X(M), Y(M);
for (int k = 0; k < M; ++k) {
cin >> X[k] >> Y[k];
--X[k];
--Y[k];
}
vector<vector<int>> o_cols(H);
vector<vector<int>> o_rows(W);
for (int k = 0; k < M; ++k) {
o_cols[X[k]].push_back(Y[k]);
o_rows[Y[k]].push_back(X[k]);
}
for (int i = 0; i < H; ++i) {
sort(o_cols[i].begin(), o_cols[i].end());
}
for (int j = 0; j < W; ++j) {
sort(o_rows[j].begin(), o_rows[j].end());
}
if (!o_cols[0].empty()) {
int leftmost = o_cols[0][0];
for (int col = leftmost + 1; col < W; ++col) {
o_cols[0].push_back(col);
}
sort(o_cols[0].begin(), o_cols[0].end());
o_cols[0].erase(unique(o_cols[0].begin(), o_cols[0].end()), o_cols[0].end());
}
long long ans = 0;
int j_max = o_cols[0].empty() ? W : o_cols[0][0];
for (int j = 0; j < j_max; ++j) {
int upmost = o_rows[j].empty() ? H : o_rows[j][0];
ans += upmost;
}
// range sum query
auto f = [](int a, int b) { return a + b; };
auto e = 0;
segment_tree<int, decltype(f)> seg(W, f, e);
int i_max = o_rows[0].empty() ? H : o_rows[0][0];
for (int i = 1; i < i_max; ++i) {
for (auto& col : o_cols[i - 1]) {
seg.update(col, 1);
}
int leftmost = o_cols[i].empty() ? W : o_cols[i][0];
ans += seg.query(0, leftmost);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout)
#define pii pair<int,int>
#define Please return
#define AC 0
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T>
void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }
const int mod=998244353;
const int maxn = 1e6+10;
int n,m,a[maxn],q,t;
char s[maxn];
int flag;
void dfs(int step,LL nw){
if(step>4){
if(nw==0) flag = 1;
return;
}
if(a[step]<=nw) dfs(step+1,nw-a[step]);
dfs(step+1,nw);
}
int main(){
LL sum = 0;
fo(i,1,4) read(a[i]),sum+=a[i];
if(sum&1){
puts("No");return 0;
}
dfs(1,sum/2);
if(flag) puts("Yes");
else puts("No");
Please AC;
} | #include<bits/stdc++.h>
#define ll long long
ll a,b,c,d,s;
int main(){
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);s=a+b+c+d;
if(a==s-a||b==s-b||c==s-c||d==s-d)puts("Yes");
else if(a+b==s-a-b||a+c==s-a-c||a+d==s-a-d||b+c==s-b-c||b+d==s-b-d||c+d==s-c-d)puts("Yes");
else puts("No");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; ++i)
cin >> a[i];
sort(a, a + n);
if(n & 1){
cout << "Second\n";
}else{
// 1 2 4 5
// 100
// 101
// 010
// 001
for(int i = 0; i < n; i += 2){
if(a[i] != a[i + 1]){
cout << "First\n";
return;
}
}
cout << "Second\n";
}
}
int main()
{
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
long long INF = 1LL<<60;
using ll = long long;
using vll = vector<ll>;
using mll = map<ll, ll>;
#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()
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; }
const string YES = "Yes";
const string NO = "No";
void solve(long long S, long long P){
for(ll x = 1; x * x <= P; x++){
if(P % x != 0) continue;
if(P/x + x == S){
cout << YES << endl; return;
}
}
cout << NO << endl;
}
int main(){
long long S;
scanf("%lld",&S);
long long P;
scanf("%lld",&P);
solve(S, P);
return 0;
}
|
#include <iostream>
using namespace std;
int main()
{
int res, matrix[4];
for (auto i = 0; i < 4; i++)
{
cin >> matrix[i];
}
res = matrix[0] * matrix[3] - matrix[1] * matrix[2];
cout << res;
return 0;
} | #include<bits/stdc++.h>
#define fo(i,a,b) for((i)=(a);i<=(b);i++)
#define rfo(i,a,b) for((i)=(a);i>=(b);i--)
#define inrange(x,y,z) (((x)>=(y))&&((x)<=(z)))
#define ALL(vec) (vec).begin(),(vec).end()
#define SOR(vec) sort(ALL(vec))
#define UNI(vec) (vec).erase(unique(ALL(vec)),(vec).end())
using namespace std;
int a,b,c,d;
int main(){
#ifdef FILIN
#ifndef DavidDesktop
freopen(FILIN,"r",stdin);
freopen(FILOUT,"w",stdout);
#endif
#endif
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>a>>b>>c>>d;
cout<<a*d-b*c<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define boost ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//#define mp make_pair
#define pb push_back
#define fp first
#define sp second
typedef long long int ll;
typedef pair<ll,ll> pp;
#define check(x, i) ((x>>i)&1LL)
#define set(x,i) (x|(1LL<<i))
#define unset(x,i) (x&~(1LL<<i))
#define INF LONG_MAX
#define MAX_N 20
#define MAX 200005
#define mod 1000000007
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define RFOR(i,a,b) for(i=a;i>=b;i--)
#define len(i,s) for(i=0;s[i];i++)
#define T() ll t;cin>>t;while(t--)
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
ll gcd (ll a, ll b){return (a?gcd(b%a,a):b);}
ll pow1(ll a,ll b,ll MOD){ll ans=1;while(b!=0){if(b&1)ans=(ans*a)%MOD;a=(a*a)%MOD;b=b/2;}return ans;}
ll modInverse(ll a,ll p){return pow1(a,p-2,p);}
void ash(){
boost
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
ash();
ll n;
cin>>n;
cout<<n-1<<"\n";
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
struct Person {
int val, id, ok, nxt;
}a[maxn], q[maxn];
struct D {
int a, b;
};
int n, fflag, ansflag = 1;
int b[maxn], p[maxn];
vector<D> ans;
int cmp(const Person &a, const Person &b) {
return a.val > b.val;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].val);
a[i].id = i;
}
for (int i = 1; i <= n; i++) scanf("%d", b + i);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i].nxt);
if (a[i].val <= b[a[i].nxt] && a[i].nxt != i)
fflag = 1;
if (a[i].nxt != i)
ansflag = 0;
else
a[i].ok = 1;
}
if (ansflag) {
printf("0\n");
return 0;
}
if (fflag) {
printf("-1\n");
return 0;
}
for (int i = 1; i <= n; i++) q[i] = a[i];
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++) {
if (!q[a[i].id].ok) {
while (a[i].nxt != a[i].id) {
ans.push_back((D){a[i].id, a[i].nxt});
q[a[i].nxt].ok = 1;
a[i].nxt = q[a[i].nxt].nxt;
}
a[i].ok = 1;
}
}
cout << ans.size() << endl;
for (const auto &i : ans) {
printf("%d %d\n", i.a, i.b);
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define int long long
#define pb push_back
#define pii pair<int,int>
#define endl "\n"
#define gok ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9 + 7,mxn=2e5 + 5;
/*struct cmp
{
bool operator() (pair<int,pii> a, pair<int,pii> b)
{
return a.first > b.first;
}
};*/
signed main(){
gok
int k;
string n;
cin>>n>>k;
for(int i=0;i<k;i++)
{
string n1 = n,n2 = n;
sort(n1.begin(),n1.end());
sort(n2.rbegin(),n2.rend());
int v1 = stoi(n1);
int v2 = stoi(n2);
v2 -= v1;
n = to_string(v2);
}
cout<<n;
return 0;
}
|
// RAKSHIT KADAM
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> 3
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree< int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update>
#warning check the size of arrays!!!
// NUMERIC DEFINITIONS :
#define INF 1000000000000000005
#define inf 1000000004
#define MOD 1000000007
#define mod 998244353
#define MAX 300006
#define pi 3.1415926535897932384626433832795
#define ld long double
#define ll long long
#define int long long
#define P(gg) for(auto rax:gg){cout<<rax<<" ";}cout<<endl;
#define Need_for_speed(activated) ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define satisfy ll ter; cin>> ter; while(ter--)
#define inp(n) int n;cin >> n
#define mp make_pair
#define pb push_back
#define endl "\n"
#define x first
#define y second
#define fore(i,a,b) for(ll i=a;i<b;i++)
#define forci(i,n) for(ll i=0;i<n;i++)
#define vi vector<ll>
#define Endl endl
#define lb lower_bound
#define ub upper_bound
#define pii pair<ll,ll>
#define input freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout)
#define time cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
using namespace std;
//ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
//
//int power(int x, unsigned int y, unsigned int m){ if (y == 0) return 1;int p = power(x, y/2, m) % m; p = (p * p) % m;return (y%2 == 0)? p : (x * p) % m;}
//ll modInverse(int a, int m){{ return power(a, m-2, m);}}
//
//const int maxn = 500003;
//
//ll fact[200009];
//ll modIN[200009];
//void facto()
//{
// ll lim =200005;fact[0]=1;
// for(ll i=1;i<lim;i++)
// {
// fact[i]=(fact[i-1]*i)%MOD;
// }
//
//}
//ll ncr(ll a,ll b)
//{
// ll ret = 0;
// if(b==0)return 1;
// if(b>a)return 0;
// ret = fact[a]*(modInverse(fact[b],MOD));
//
// ret%=MOD;
// ret*=modInverse(fact[a-b],MOD);
// ret%=MOD;
//
// return ret;
//}
// """"" 1LL << x """""
void solve()
{
ll n,k;cin>>n>>k;
while(k--)
{
string s = to_string(n);
sort(s.begin(),s.end());
string a = s;
string b = s;
reverse(b.begin(),b.end());
ll x= 0;
for(auto it:a)
{
x*=10;
x+=it-'0';
}
ll y = 0;
for(auto it:b)
{
y*=10;
y+=it-'0';
}
n=abs(y-x);
if(n==0)
{
cout<<0<<endl;return;
}
}
cout<<n<<endl;
}
signed main()
{
Need_for_speed(activated);
// satisfy
// {
// solve();
// }
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
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 T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) 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 << '}'; }
template<typename T> ostream& operator<<(ostream &os, const set<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &mp) { os << '['; string sep; for (const auto &x : mp) os << sep << x, sep = ", "; return os << ']'; }
template<typename T> ostream& operator<<(ostream &os, const multiset<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] =", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define int long long
#define mod 1000000007
#define inf 1e18
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)(x).size()
#define sb(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()
#define rall(x) (x).rbegin(), (x).rend()
int decimaltoOctal(int deciNum)
{
int octalNum = 0, countval = 1;
int dNo = deciNum;
while (deciNum != 0) {
int remainder = deciNum % 8;
octalNum += remainder * countval;
countval = countval * 10;
deciNum /= 8;
}
return octalNum;
}
bool check(int n) {
while (n > 0) {
int digit = n % 10;
if (digit == 7) return false;
n /= 10;
}
return true;
}
void solve()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//cout << fixed << setprecision(10);
int n; cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
if (check(i) && check(decimaltoOctal(i))) {
ans++;
}
}
dbg(ans);
// for (int i = 1; i <= n; i++) {
// if (check(decimaltoOctal(i))) {
// dbg(decimaltoOctal(i));
// ans++;
// }
// }
cout << ans << endl;
}
int32_t main()
{
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll N;
cin >> N;
unordered_set<ll> s;
for(ll a = 2; a * a <= N; a++){
ll x = a * a;
while(x <= N){
s.insert(x);
x *= a;
}
}
cout << N - s.size() << endl;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef long long ll;
typedef long double ld;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i,n) for(int i=n; i>-1; --i)
#define ALL(a) (a).begin(),(a).end()
#define FILL(a,n,x) REP(i,(n)){ (a)[i]=(x); }
#define CINA(a,n) REP(i,(n)){ cin >> (a)[i]; }
#define FILL2(a,n,m,x) REP(i,(n)){ REP(j,(m)){(a)[i][j]=(x);} }
#define CINA2(a,n,m) REP(i,(n)){ REP(j,(m)){cin >> (a)[i][j];} }
#define Liny "Yes\n"
#define Linn "No\n"
#define LINY "YES\n"
#define LINN "NO\n"
//cout << setfill('0') << right << setw`(4) << 12; // "0012"
int keta(ll x){ if(x<10){return 1;} else{return keta(x/10) + 1;}}
int keta_wa(ll x){ if(x<10){return x;} else{return keta_wa(x/10) + x%10;} }
int ctoi(char c){ return ( (c>='0' && c<='9')? c - '0': 0 );}
int __stoi(string s){ return atoi(s.c_str()); }
ll sum(ll a[],ll N){ return accumulate(a,a+N,0LL);}
ll gcd(ll a,ll b){if(a<b)swap(a,b); return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){if(a<b){swap(a,b);} return a/gcd(a,b)*b;}
template<class T> void chmax(T& a, T b){ if(a<b){a=b;} }
template<class T> void chmin(T& a, T b){ if(a>b){a=b;} }
template<class T> bool isIn(T a,vector<T> v){ for(T x:v){ if(a==x){return true;}} return false;}
string strReplace(string s,string target, string replacement){
if (!target.empty()) {
std::string::size_type pos = 0;
while ((pos = s.find(target, pos)) != std::string::npos) {
s.replace(pos, target.length(), replacement);
pos += replacement.length();
}
}
return s;
}
const ll MOD = 998244353;
// using mint = static_modint<MOD>;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
bool DEBUG = 0;
template<class T> void dprint(T s){
if(DEBUG){ cout << s << "\n"; }
}
void solve(){
int N; cin>>N;
vector<int> a(N),b(N);
int M = N*2;
vector<int> tp(M,0),com(M,-1);
REP(i,N){
cin >> a[i] >> b[i];
if(a[i]!=-1){
--a[i];
if(tp[ a[i] ]!=0){ cout << Linn; return; }
tp[ a[i] ] = i+1;
}
if(b[i]!=-1){
--b[i];
if(tp[ b[i] ]!=0 ){cout << Linn; return;}
tp[ b[i] ] = -(i+1);
}
if(a[i]!=-1 && b[i]!=-1){
com[ a[i] ] = b[i];
com[ b[i] ] = a[i];
}
}
dprint("---78----");
vector<bool> dp(M+1,false);
dp[0] = true;
REP(i,M){
if(!dp[i]){continue;}
dprint("--");
for(int j=i+1;j<M;++j){
int w = j - i + 1;
if(w%2){continue;}
w /= 2;
bool ok = true;
vector<bool> exist(N,false);
REP(k,w){
int start = i+k;
int stop = i+k+w;
if(com[start] != -1 && !(i<=com[start] && com[start]<=j) ){
ok = false;
break;
}
dprint("r1");
if(com[stop] != -1 && !(i<=com[stop] && com[stop]<=j)){
ok = false;
break;
}
dprint("r2");
if(tp[start] != 0 && tp[stop] != 0){
if( tp[start]<0 || tp[start]+tp[stop] != 0){
ok = false;
break;
}
}
dprint("r3");
if(tp[start]<0 || tp[stop]>0){
ok = false;
break;
}
dprint("r4");
if(tp[start] != 0){
int temp = tp[start]-1;
if(exist[temp]){
ok = false;
break;
}
exist[temp] = 1;
}
dprint("r5");
}
if(ok){
dp[j+1] = 1;
}
}
}
dprint("---130---");
REP(i,M+1){
dprint(dp[i]);
}
if(dp[M]){ cout << Liny; }
else{ cout << Linn; }
return;
}
int main(int argc, char *argv[]){
if(argc>1 && argv[1][0]=='D'){ DEBUG = 1;}
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>
ostream& operator<<(ostream &os, vector<T> &v){
string sep = " ";
if(v.size()) os << v[0];
for(int i=1; i<v.size(); i++) os << sep << v[i];
return os;
}
template<typename T>
istream& operator>>(istream &is, vector<T> &v){
for(int i=0; i<v.size(); i++) is >> v[i];
return is;
}
#ifdef DBG
void debug_(){ cout << endl; }
template<typename T, typename... Args>
void debug_(T&& x, Args&&... xs){
cout << x << " "; debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif
int main() {
ios_base::sync_with_stdio(false);
cout << setprecision(20) << fixed;
int n; cin >> n;
vector<int> s(2*n), d(2*n);
for(int i=0; i<n; i++){
int a, b; cin >> a >> b;
a--; b--;
if(a>=0&&b>=0){
if(b<=a||s[a]!=0||s[b]!=0) {
cout << "No" << endl;
return 0;
}
s[a] = (i+1);
s[b] = -(i+1);
d[a] = b-a;
d[b] = a-b;
} else if(a>=0){
if(s[a]!=0){
cout << "No" << endl;
return 0;
}
s[a] = i+1;
} else if(b>=0){
if(s[b]!=0){
cout << "No" << endl;
return 0;
}
s[b] = -(i+1);
}
}
vector<bool> dp(2*n+1);
dp[0] = true;
for(int i=0; i<2*n; i++){
if(!dp[i]) continue;
for(int j=i+1; j<2*n; j+=2){
int w = (j-i+1)/2;
bool ok = true;
for(int k=0; k<w; k++){
int p = i+k, q = i+w+k;
if(d[p]!=0&&p+d[p]!=q) ok = false;
if(d[q]!=0&&q+d[q]!=p) ok = false;
if(s[p]!=0&&s[q]!=0&&s[p]+s[q]!=0) ok = false;
if(s[p]<0 || 0<s[q]) ok = false;
// if(s[p]<0) ok = false;
// if(d[p]!=0&&d[p]!=w) ok = false;
// if(s[q]>0) ok = false;
// if(d[p]!=0&&d[q]!=-w) ok = false;
}
if(ok) dp[j+1] = true;
}
}
cout << (dp[2*n]?"Yes":"No") << endl;
return 0;
} |
/*
Saturday 08 May 2021 05:51:43 PM IST
@uthor::astrainL3gi0N
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef std::vector<int> vi;
typedef std::pair<int,int> ii;
typedef std::vector<ii> vii;
typedef std::vector<ll> vl;
#define pb push_back
#define mp make_pair
#define debg(x) std::cerr<<(#x)<<" => "<<x<<'\n';
#define debgg(x,y) std::cerr<<(#x)<<" => "<<x<<'\t'<<(#y)<<' '<<y<<'\n';
#define len(a) (int)(a).size()
#define all(x) x.begin(),x.end()
const int mod = 200;
const int modd = 998244353;
const int esc = -1;
const string ys = "Yes\n";
const string no = "No\n";
bool comp (int x, int y) {
return x > y;
}
void printarr (int arr[], int n) {
for(int i = 0; i < n; ++i)
cout<<arr[i]<<(i<n-1?' ':'\n');
}
template < typename T> void printv (T &a) {
for (auto it = a.begin(); it != a.end(); ++it)
cout<<*it<<' ';
cout<<'\n';
}
int gint() {
int n; cin>>n;
return n;
}
//int t[510][510];
ll ans;
void solve () {
map <int,int> freq;
set <int> hsh;
ans = 0;
bool ok = false;
int n = gint();
vi arr(100,0);
for (int i = 0; i < n;++i) {
arr[i] = gint()%200;
}
int res = 0;
for (int b = 1; b < (1<<(min(10,n))); ++b) {
ans = 0;
ok = 1;
for (int i = 0;i < min(n,30); ++i) {
if (b & (1<<i)) {
ans += arr[i];
ans %= mod;
}
}
if (ok)
if (freq[ans]) {
res = b;
break;
}
freq[ans] = b;
}
vi rs1,rs2;
cout<<(res?"YES":"NO");
cout<<'\n';
if (res) {
int temp = freq[ans];
for (int i = 0; i < 10; ++i) {
if (res & (1<<i)) {
rs1.pb(i+1);
}
if (temp & (1<<i)) {
rs2.pb(i+1);
}
}
cout<<len(rs1)<<' ';
printv(rs1);
cout<<len(rs2)<<' ';
printv(rs2);
}
}
void sol () {
//test
}
int main () {
std::ios_base::sync_with_stdio(false);std::cin.tie(nullptr);
//cout << setprecision(15) << fixed;
int testcases = 1;
for (int t = 0; t < testcases; ++t) {
//cout<<"Case #"<<t+1<<": ";
solve();
}
return 0;
}
| /*#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")*/
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(firstsecond) '(' << first< ',' << second< ')'
//constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
constexpr ll MOD = 3;
ll INF = 1001001001001001001;
long double EPS = 1e-8;
long double PI = 3.141592653589793238;
template <typename T>
void remove(std::vector<T> &vector, unsigned int index)
{
vector.erase(vector.begin() + index);
}
random_device seed_gen;
mt19937_64 rnd(seed_gen());
ll range_rand(ll a,ll b){
uniform_int_distribution<ll> dist_res(a,b);
return dist_res(rnd);
}
using Graph = vector<vector<ll>>;
// MOD確認
ll N;
ll A[210];
set<set<ll>> dp[210][210];
int main(){
cin >> N;
rep(i,N) cin >> A[i];
set<ll> sstt;
dp[0][0].insert(sstt);
rep(i,N){
rep(j,200){
for(auto itr = dp[i][j].begin();itr != dp[i][j].end();itr++){
if(dp[i+1][(A[i]+j) % 200].size() >= 3 && dp[i+1][j].size() >= 2) break;
set<ll> st = *itr;
st.insert(i);
dp[i+1][(A[i]+j) % 200].insert(st);
dp[i+1][j].insert(*itr);
}
}
}
rep(i,200){
if(dp[N][i].size() >= 2){
set<ll> st1,st2,st;
ll cnt = 0;
ll n = dp[N][i].size();
for(auto itr = dp[N][i].begin();itr != dp[N][i].end();itr++){
st = *itr;
cnt++;
if(st.size() != 0 && cnt != n){
st1 = *itr;
itr++;
st2 = *itr;
break;
}
}
if(st1.size() != 0){
cout << "Yes" << endl;
}else{
continue;
}
if(st1 == st2) continue;
cout << st1.size() << ' ';
for(auto itr = st1.begin();itr != st1.end();itr++){
cout << *itr + 1 << ' ';
}
cout << endl;
cout << st2.size() << ' ';
for(auto itr = st2.begin();itr != st2.end();itr++){
cout << *itr + 1 << ' ';
}
cout << endl;
return 0;
}
}
cout << "No" << endl;
} |
#include <stack>
#include <queue>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <set>
#include <tuple>
#include <cmath>
#include <random>
#include <algorithm>
#include <vector>
#include <iostream>
#include <fstream>
#include <chrono>
#include <numeric>
#define fio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
#define rng mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count())
#define ri(...) i64 __VA_ARGS__; rx(__VA_ARGS__)
#define rc(...) char __VA_ARGS__; rx(__VA_ARGS__)
#define rs(...) string __VA_ARGS__; rx(__VA_ARGS__)
#define rvi(x, n) vi x(n); cin >> x
#define rvvi(x, n, m) vvi x(n, vi(m)); cin >> x
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << '[' << #__VA_ARGS__ << "]: "; dbg_log(__VA_ARGS__)
#else
#define dbg(...) ;;
#endif
#define fa(i, v) for (auto &i : v)
#define fi(i, n) for (i32 i = 0; i < n; i++)
#define fp(i, n) for (i32 i = 1; i < n; i++)
#define fr(i, n) for (i32 i = n - 1; i >= 0; i--)
#define fu(i) ri(__i); fi(i, __i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint64_t u64;
typedef int64_t i64;
#ifdef __SIZEOF_INT128__
typedef __uint128_t u128;
typedef __int128_t i128;
#endif
typedef vector<i64> vi;
typedef vector<vi> vvi;
template<typename T>
inline istream &operator>>(istream &in, vector<T> &v) {
fa(x, v) in >> x;
return in;
}
template<typename T>
inline ostream &operator<<(ostream &out, const vector<T> &v) {
fa(x, v) out << x << ' ';
return out;
}
template<typename T>
inline void rx(T &x) {
cin >> x;
}
template<typename T, typename... R>
inline void rx(T &x, R &... l) {
cin >> x;
rx(l...);
}
template<typename T>
inline void dbg_log(const T &x) {
cerr << x << endl;
}
template<typename T, typename... R>
inline void dbg_log(const T &x, const R &... l) {
cerr << x << ", ";
dbg_log(l...);
}
template<typename T>
inline void wl(const T &x) {
cout << x << '\n';
}
template<typename T, typename... R>
inline void wl(const T &x, const R &... l) {
cout << x << ' ';
wl<R...>(l...);
}
int main() {
fio;
ri(n, m);
rvi(a, n);
vi len(m, 0);
vi prv(m, -1);
fi(i, n) {
i64 k = a[i];
if (k >= m) continue;
len[k] = max(len[k], i - prv[k]);
prv[k] = i;
}
dbg(prv);
fi(i,m) {
len[i] = max(len[i], n - prv[i]);
}
dbg(len);
i64 res = m;
fr(i, m) {
if (len[i] > m) res = i;
}
wl(res);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l) ; i < (r); ++i)
#define incII(i, l, r) for(int i = (l) ; i <= (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define decII(i, l, r) for(int i = (r) ; i >= (l); --i)
#define inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) < (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin = [](auto & a, auto b) { return (b < a ? a = b, true : false); };
auto setmax = [](auto & a, auto b) { return (b > a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
auto fl = [](auto a, auto b) { assert(b != 0); return a / b - (a % b != 0 && ((a >= 0) != (b >= 0)) ? 1 : 0); };
auto ce = [](auto a, auto b) { assert(b != 0); return a / b + (a % b != 0 && ((a >= 0) == (b >= 0)) ? 1 : 0); };
auto mo = [](auto a, auto b) { assert(b != 0); a %= b; if(a < 0) { a += abs(b); } return a; };
LL gcd(LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); }
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
// ---- ----
int main() {
int n, m, k;
cin >> n >> k >> m;
vector<int> a(n - 1);
inc(i, n - 1) { cin >> a[i]; }
int s = 0;
inc(i, n - 1) { s += a[i]; }
int ans = max(n * m - s, 0);
cout << (ans <= k ? ans : -1) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i))
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int INF = 1e9;
constexpr ll INFL = 1LL << 61;
constexpr ll mod = 1e9+7;
double dp[105][105][105];
bool flag[105][105][105];
double rec(int A, int B, int C){
if(flag[A][B][C]) return dp[A][B][C];
if(A==100 || B == 100 || C == 100){
return 0;
}
flag[A][B][C] = true;
double sum = A + B + C;
double res = A / sum * (rec(A+1, B, C)+1) + B / sum * (rec(A, B+1, C)+1) + C / sum * (rec(A, B, C+1)+1);
return dp[A][B][C] = res;
}
int main(){
int A, B, C;
cin >> A >> B >> C;
printf("%.9f\n", rec(A, B, C));
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL<<60;
const double PI = acos(-1.0);
/*const double PI = atan2(0.0,-1.0)*/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep1(i,aa,n) for(ll i = aa; i <= (ll)(n); i++)
#define ALL(a) (a).begin(),(a).end()
#define ar_init(aa,val) rep(i,aa.size()) aa[i]=val;
#define v2d(aa,val1,val2,ini) vector<vector<ll>> aa(val1,vector<ll>(val2,ini));
#define pqe priority_queue<ll>
#define pqeg priority_queue<ll,vector<ll>,greater<ll>>
#define lcin(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define eout(...) COU(__VA_ARGS__);
#define sout(...) SCOU(__VA_ARGS__);
#define scin(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define lb(aa,val) lower_bound(ALL(aa),val)
#define pb push_back
#define mkp make_pair
#define si size()
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
cin>>head;
CINT(move(tail)...);
}
void COU(){}
template <class Head,class... Tail>
void COU(Head&& head,Tail&&... tail){
cout<<head<<endl;
COU(move(tail)...);
}
void SCOU(){}
template <class Head,class... Tail>
void SCOU(Head&& head,Tail&&... tail){
cout<<head<<" ";
SCOU(move(tail)...);
}
template <class T = ll>
T IN(){T x;cin>>x;return (x);}
using pii = pair<ll,ll>;
using v2 = vector<vector<ll>>;
using v1 = vector<ll>;
using v2p = vector<vector<pii>>;
using v1p = vector<pii>;
//ll mo=1000000007;
ll mo=998244353;
ll mod_pow(ll N,ll n){
if(n==0) return 1;
ll pp,hh;
pp=n/2;
hh=n%2;
int tt=mod_pow(N,n/2);
if(hh==0)return tt%mo*tt%mo;
else {return tt%mo*tt%mo*N%mo;}
}
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a%b, y, x); // 再帰的に解く
y-=a/b*x;
return d;
}
ll modpow(ll a, ll n = mo-2) {
ll r=1;a%=mo;
while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
return r;
}
v2d (aa,5003,5003,0);
v2d (dp,5003,5003,0);
int main(){
lcin(h,w);
lcin(k);
dp[0][0]=1;
rep(i,k){
ll a,b;
char f;
cin>>a>>b>>f;
if(f=='R'){
aa[a-1][b-1]=1;
}else if(f=='D'){
aa[a-1][b-1]=2;
}else{
aa[a-1][b-1]=3;
}
}
ll g=2*modpow(3);
rep(i,h){
rep(j,w){
if(aa[i][j]){
if(aa[i][j]!=1)dp[i+1][j]+=dp[i][j];
if(aa[i][j]!=2)dp[i][j+1]+=dp[i][j];
}else{
dp[i+1][j]+=g*dp[i][j]%mo;
dp[i][j+1]+=g*dp[i][j]%mo;
dp[i+1][j]%=mo;
dp[i][j+1]%=mo;
}
}
}
eout(mod_pow(3,h*w-k)*dp[h-1][w-1]%mo);
} |
#include <bits/stdc++.h>
using namespace std;
int n;
int dp[200001][8] ;
string s, x ;
bool fun(int idx, int num) {
if (idx > n) return (num == 0) ;
int &ret = dp[idx][num] ;
if (ret != -1) return ret ;
int cur = 0 ;
if (x[idx] == 'A') {
cur = fun(idx+1, (num * 10)%7) ;
if (cur == 1) cur = fun(idx+1, (num * 10 + (s[idx] - '0'))%7) ;
}
else {
cur = fun(idx+1, (num * 10)%7) ;
if (cur == 0) cur = fun(idx+1, (num * 10 + (s[idx] - '0'))%7) ;
}
return ret = cur ;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s >> x;
s = "#" + s ;
x = "#" + x ;
memset(dp, -1, sizeof(dp)) ;
bool ok = fun(1, 0) ;
string ans = "Aoki" ;
if (ok) ans = "Takahashi" ;
cout << ans << endl ;
return 0;
}
| #ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
/*---------macro---------*/
#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 ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)
/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const ll big=1000000007;
//const ll big=998244353;
const double EPS=1e-8; //適宜変える
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
constexpr operator int(){return -int(1e9)-10;}
constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
constexpr operator int(){return int(1e9)+10;}
constexpr operator ll(){return ll(1e18)+10;}
constexpr auto operator -(){return neginf;}
}inf;
/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif
/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.precision(10);
/*--------------------------------*/
int n;cin >> n;
string s;cin >> s;
string x;cin >> x;
//「高橋の勝ち」.
vector<vector<bool>> dp(n+1,vector<bool>(7,false));
dp[n][0]=true;
for(int i=n-1;i>=0;i--){
int now=expm(10,n-i-1,7);
if(x[i] == 'T'){
rep(j,7){
if(dp[i+1][j] || dp[i+1][(j+now*(s[i]-'0')) %7]){
dp[i][j]=true;
}
else{
dp[i][j]=false;
}
}
}
else{
rep(j,7){
if((!dp[i+1][j])|| !dp[i+1][(j+now*(s[i]-'0')) %7]){
dp[i][j]=false;
}
else{
dp[i][j]=true;;
}
}
}
}
cout << (dp[0][0] ? "Takahashi" : "Aoki") << newl;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define fi first
#define se second
#define endl '\n'
#define MAX INT_MAX
#define MIN INT_MIN
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mod 1000000007
#define pb push_back
#define sz(a) (int)((a).size())
#define all(a) a.begin(),a.end()
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define forn(i, n) for (int i = 0; i < n; i++)
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef pair< int,int > pii;
int32_t main()
{
fast
int test=1;
//cin>>test;
while(test--)
{
int n;
cin>>n;
set<int> s;
int x;
forn(i,n)
{
cin>>x;
s.insert(x);
}
while(s.size()>1)
{
int mn=*s.begin();
int mx=*s.rbegin();
s.erase(mx);
s.insert(mx-mn);
}
cout<<*s.begin();
//while loop ending
}
return 0;
} | #include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <numeric>
#include <string>
#include <cstring>
#include <list>
#include <unordered_set>
#include <tuple>
#include <cmath>
#include <limits>
#include <type_traits>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <regex>
#include <random>
#include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exout(x) printf("%.12f\n", x)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX_N = 1010101;
//組み合わせの余りを求める
ll fac[MAX_N], finv[MAX_N], inv[MAX_N];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX_N; 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(ll n, ll k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// mod.m での a の逆元 a^ { -1 } を計算する
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
//最大公約数
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
if (x == 0 || y == 0)return 0;
return (x /gcd(x, y) * y );
}
//union-find木について
ll par[101010];
ll rank2[101010];
void init(ll n) {
rep(i, n) {
par[i] = i;
rank2[i] = 0;
}
}
ll find(ll x) {
if (par[x] == x) {
return x;
}
else {
return par[x] = find(par[x]);
}
}
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)return;
if (rank2[x] < rank2[y]) {
par[x] = y;
}
else {
par[y] = x;
if (rank2[x] == rank2[y]) {
rank2[x]++;
}
}
}
ll dx[8] = { 1,-1,0,0,1,1,-1,-1 };
ll dy[8] = { 0,0,1,-1,1,-1,1,-1 };
//long longしか使わない
//素数は1より大きい
//lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
//upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
ll n;
cin >> n;
vector<ll>a(n);
priority_queue<ll>q1;
rep(i, n) {
cin >> a[i];
}
ll ans = a[0];
rep(i, n) {
ans = gcd(ans, a[i]);
}
cout << ans << endl;
return 0;
}
|
#pragma GCC optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <utility>
#include <map>
#define rush() int T; while(cin>>T) while(T--)
#define ms(a,b) memset(a,b,sizeof a)
#define lowbit(x) ((x)&(-x))
#define inf 0x7f7f7f7fll
#define eps 1e-11
#define sd(a) scanf("%d",&a)
#define sll(a) scanf("%lld",&a)
#define sll2(a,b) scanf("%lld%lld",&a,&b)
#define sll3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a) scanf("%lf",&a)
//#define sc(a) scanf("%c\n",&a)
#define ss(a) scanf("%s",a)
#define pd(a) printf("%d\n",a)
#define pdd(a,b) printf("%d %d\n",a,b)
#define pddd(a,b,c) printf("%d %d %d\n",a,b,c)
#define pll(a) printf("%lld\n",a)
#define pf(a) printf("%.1lf\n",a)
#define pc(a) printf("%c",a)
#define ps(a) printf("%s\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define forn(i,a,b,x) for(int i=(a);i<=(b);i+=(x))
#define per(i,a,b) for(int i=(b);i>=(a);i--)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define debug(a) cout<<#a<<" : "<<a<<" "<<" ; "
#define dbg(a) cout<<#a<<" : "<<a<<endl
#define show(a,b) cout<<a<<" :\t"<<b<<"\t*"<<endl
#define IOS ios::sync_with_stdio(0); cin.tie(0)
#define PAUSE system("pause")
#define all(x) x.begin(),x.end()
//#define print(a) cout<<a<<endl;continue;
#define fr first
#define sc second
#define ceils(a,b) ((a-1ll+b)/(b))
#define floors(a,b) floor(double(a)/double(b))
using namespace std;
//inline void swap(int &x,int &y){x^=y^=x^=y;}
typedef long long ll;
//using ui=unsigned int;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const double pi=acos(-1.0);
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
const int limit=5e5;
int read()
{
int s=0;
char c=getchar(),lc='+';
while (c<'0'||'9'<c) lc=c,c=getchar();
while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar();
return lc=='-'?-s:s;
}
void write(int x)
{
if (x<0) putchar('-'),x=-x;
if (x<10) putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
void print(int x,char c='\n')
{
write(x);
putchar(c);
}
ll mod=2147483648;
const int N=3e5+5;
int n, m, _;
int i, j, k;
int a[N];
int pos[N];
vector<int> ans;
signed main()
{
//IOS;
rush(){
sd(n);
rep(i, 1, n) sd(a[i]), pos[a[i]] = i;
int opt = 1;
rep(i, 1, n){
if(i == pos[i]) continue;
if((pos[i] - 1) % 2 != opt){
if(pos[i] - 2 >= i){
ans.pb(pos[i] - 2);
int x = pos[i] - 2, y = pos[i] - 1;
swap(pos[a[x]], pos[a[y]]);
swap(a[x], a[y]);
opt ^= 1;
}
else if(pos[i] == i + 1){
ans.pb(i - 1);
ans.pb(i);
ans.pb(i - 1);
ans.pb(i);
ans.pb(i - 1);
//ans.pb(i);
opt ^= 1;
int x = pos[i], y = pos[i] - 1;
swap(pos[a[x]], pos[a[y]]);
swap(a[x], a[y]);
continue;
}
}
for(int j = pos[i] - 1; j >= i; j--){
ans.pb(j);
int x = j, y = j + 1;
swap(pos[a[x]], pos[a[y]]);
swap(a[x], a[y]);
opt ^= 1;
}
}
pd(ans.size());
for(auto it : ans) printf("%d ", it);
puts("");
ans.clear();
}
//PAUSE;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define trav(a, x) for(auto &a:x)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define pb push_back
#define f first
#define s second
#define nl '\n'
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
int n, turn, t;
int a[501];
int pos[501];
vi ans;
void swap(int i){
swap(a[i], a[i+1]);
swap(pos[a[i]], pos[a[i+1]]);
ans.pb(i);
turn++;
}
void solve3(){
while(!(a[1]==1 && a[2]==2 && a[3]==3)){
if(turn&1) swap(1);
else swap(2);
}
}
void solve4(){
while(a[4]!=4){
if(turn&1){
if(a[1]==4) swap(1);
else swap(3);
}
else swap(2);
}
solve3();
}
int main(){
cin.tie(0)->sync_with_stdio(0);
// freopen("input.txt", "r", stdin);
cin >> t;
while(t--){
turn=1;
ans.clear();
cin >> n;
rep(i, 1, n){
cin >> a[i];
pos[a[i]]=i;
}
if(n==2){
if(a[1]==1) cout << "0";
else cout << "1\n1";
cout << nl;
continue;
}
if(n==3) solve3();
else if(n==4) solve4();
else{
turn=1;
for(int i=n; i>=5; i--) {
if(pos[i]==i) continue;
if(turn%2!=pos[i]%2){
if(pos[i]-3>=1) swap(pos[i]-3);
else swap(pos[i]+1);
}
while(pos[i]!=i){
swap(pos[i]);
}
}
solve4();
}
cout << sz(ans) << nl;
trav(k, ans) cout << k << " ";
cout << nl;
}
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
typedef int ll;
typedef long double ld;
const ll N = 505;
char en = '\n';
ll inf = 1e16;
ll mod = 998244353;
ll power(ll x, ll n, ll mod) {
ll res = 1;
x %= mod;
while (n) {
if (n & 1)
res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
using namespace __gnu_pbds; //(or pb_ds)
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
char arr[N][N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll a[n + 5];
ll b[n + 5];
map<ll, vector<ll>> indices;
for (ll i = 1; i <= n; i++) {
cin >> a[i];
indices[i + a[i]].push_back(i);
}
for (ll i = 1; i <= n; i++) {
cin >> b[i];
}
for (auto &it : indices) {
reverse(it.second.begin(), it.second.end());
}
pbds X;
ll res = 0;
for (ll i = 1; i <= n; i++) {
ll req = b[i] + i;
if (indices.find(req) == indices.end()) {
cout << -1 << en;
return 0;
}
vector<ll> &vect = indices[req];
ll indx = vect.back();
vect.pop_back();
if (vect.empty()) {
indices.erase(req);
}
// adjust for changes
// find indices > indx
ll change = (i - 1) - X.order_of_key(indx);
X.insert(indx);
res += indx + change - i;
}
cout << res << en;
return 0;
}
| #include <bits/stdc++.h>
#define mod 998244353
using namespace std;
int n,i,x;
int a[200005];
long long ans;
vector<int> seg;
map<int, vector<int>> m;
void upd(int stt, int drt, int st, int dr, int p, int val)
{
if(stt==st && drt==dr)
{
seg[p]+=val;
return;
}
int mij=(st+dr)/2;
if(drt<=mij)
upd(stt, drt, st, mij, 2*p, val);
else if(stt>mij)
upd(stt, drt, mij+1, dr, 2*p+1, val);
else
{
upd(stt, mij, st, mij, 2*p, val);
upd(mij+1, drt, mij+1, dr, 2*p+1, val);
}
}
int query(int i, int st, int dr, int p)
{
if(st==dr)
return seg[p];
int mij=(st+dr)/2;
if(i<=mij)
return seg[p]+query(i, st, mij, 2*p);
else
return seg[p]+query(i, mij+1, dr, 2*p+1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
seg.resize(4*n+4);
for(i=1;i<=n;i++)
{
cin>>a[i];
a[i]+=i;
}
for(i=n;i>=1;i--)
{
upd(i, i, 1, n, 1, i);
m[a[i]].push_back(i);
}
for(i=1;i<=n;i++)
{
cin>>x;
x+=i;
if(m[x].empty())
{
cout<<-1;
return 0;
}
int p=m[x].back();
m[x].pop_back();
ans+=query(p, 1, n, 1)-i;
if(p>1)
upd(1, p-1, 1, n, 1, 1);
}
cout<<ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef int_fast32_t int32;
typedef int_fast64_t int64;
const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;
#define YES(n) cout << ((n) ? "YES\n" : "NO\n" )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n" )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n" )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;
template<class T> inline bool chmax(T& a, T b) {
if (a < b) { a = b; return true; } return false;
}
template<class T> inline bool chmin(T& a, T b) {
if (a > b) { a = b; return true; } return false;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int64 n,k;
cin >> n >> k;
while(k > 0){
if(n % 200 == 0){
n = n / 200;
--k;
}else{
if(k >= 2){
n = 5 * n + 1;
k -= 2;
}else{
n = 1000 * n + 200;
--k;
}
}
}
ANS(n);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
long long N, K;
cin >> N >> K;
rep(i, K){
if(N % 200 == 0){
N /= 200;
continue;
}
else{
N *= 1000;
N += 200;
}
}
cout << N << endl;
} |
//ver 8.1
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : (v))
#define reps(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) ((a)<=(x)&&(x)<=(b))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
template<typename T> bool inside(T a,T b){auto it=a.begin()-1;each(x,b){it=find(it+1,a.end(),x);if(it==a.end())return false;}return true;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO" )
#define Yes(n) ((n) ? "Yes" : "No" )
#define yes(n) ((n) ? "yes" : "no" )
int main(){
init();
inl(a,b,c);
out(21-a-b-c);
return 0;
} | #pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
int main(){
int a,b,c;
cin>>a>>b>>c;
cout<<21-(a+b+c)<<"\n";
} |
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i,n) for(long i=0; i<(long)(n);i++)
#define REP(i,s,t) for(long i=s; i<t; i++)
#define RNG(i,s,t,u) for(long i=s; i<t; i+=u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
int main(){
string s;
cin >> s;
int sum = 0;
vi cnt(3,0);
rep(i,s.size()){
int num = s.at(i) - '0';
sum += num;
cnt.at(num % 3)++;
}
if(sum % 3 == 0){
cout << 0 << endl;
return 0;
}
if(s.size() == 1){
cout << -1 << endl;
return 0;
}else if(s.size() == 2){
if(cnt.at(0) == 0){
cout << -1 << endl;
}else{
cout << 1 << endl;
}
return 0;
}else{
int a = cnt.at(1);
int b = cnt.at(2);
int mini = 10000;
for(int i=a;i>=0;i-=3){
for(int j=b;j>=0;j-=3){
//cout << i << j << endl;
chmin(mini,abs(i-j));
}
}
cout << mini << endl;
}
} |
// '-.-'
// () __.'.__
// .-:--:-. |_______|
// () \____/ \=====/
// /\ {====} )___(
// (\=, //\\ )__( /_____\
// __ |'-'-'| // .\ ( ) /____\ | |
// / \ |_____| (( \_ \ )__( | | | |
// \__/ |===| )) `\_) /____\ | | | |
// /____\ | | (/ \ | | | | | |
// | | | | | _.-'| | | | | | |
// |__| )___( )___( /____\ /____\ /_____\
// (====) (=====) (=====) (======) (======) (=======)
// }===={ }====={ }====={ }======{ }======{ }======={
// (______)(_______)(_______)(________)(________)(_________)
//
#include<bits/stdc++.h>
using namespace std;
typedef long long li;
typedef long double ld;
typedef pair<int,int> pt;
typedef pair<ld, ld> ptd;
typedef unsigned long long uli;
#define SORT(vec) sort(vec.begin(), vec.end())
#define loop(i,a,n) for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define mset(a, val) memset(a, val, sizeof (a))
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define ft first
#define sc second
#define sz(a) int((a).size())
#define x first
#define y second
const int INF = int(1e9);
const li INF64 = li(INF) * li(INF);
const ld EPS = 1e-9;
const ld PI = ld(3.1415926535897932384626433832795);
int main(){
li n;
cin>>n;
for(li i=1;i<=100000;i++){
li a=i*(i+1)/2;
if(a>=n){
cout<<i<<"\n";break;
}
}
return 0;
}
|
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
void three(int x, int y, int z,int* hi){
if(x < y){
if(x < z){
return;
}
hi[0] = z;
hi[2] = x;
return;
}
else if(y < z){
hi[0] = y;
hi[1] = x;
return;
}
hi[0] = z;
hi[2] = x;
return;
}
int equal(const vector<int>a,const vector<int>b,int n){
for(int i = 0; i < n; i++){
if(a[i] != b[i]){
return false;
}
}
return true;
}
int main(){
vector<vector<vector<int>>> eight(9);
int a,b,c;
int e;
for(int i = 13; i < 125; i++){
e = 8*i;
a = e/100;
e -= a*100;
b = e/10;
c = e - b*10;
if(b > 0 && c > 0){
int hi[] = {a,b,c};
eight[hi[0]-1].push_back({hi[1],hi[2]});
eight[hi[1]-1].push_back({hi[0],hi[2]});
eight[hi[2]-1].push_back({hi[1],hi[0]});
}
}
string s;
cin >> s;
int n = s.length();
int ans = 0;
char a_c;
vector<int> judge(9,1);
vector<int> owari(9);
if(n == 1){
if((int)s[0] % 8 == 0){
ans = 1;
}
}
else if(n == 2){
if((10*(int)(s[0]- '0') + (int)(s[1]-'0')) % 8 == 0 || (10*(int)(s[1]-'0') + (int)(s[0]-'0')) % 8 == 0 ){
ans = 1;
}
}
else{
int count = 0;
for(int x = 0; x < n-2; x++){
a_c = s[x];
a = (int)(a_c - '0')-1;
if(judge[a] != 0){
for(int i = 0; i < eight[a].size(); i++){
count = 0;
b = eight[a][i][0];
c = eight[a][i][1];
for(int y = x+1; y < n; y++){
int s_y = (int)(s[y] - '0');
if(s_y == b){
b = 0;
count++;
}
else if(s_y == c){
c = 0;
count++;
}
if(count == 2){
cout << "Yes" << endl;
return 0;
}
}
}
}
judge[a] = 0;
if(equal(judge, owari, 9)){
break;
}
}
}
if(ans == 0){
cout << "No" << endl;
}
else{
cout << "Yes" << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;
bool f(int i, map<char, int> a){
string s = to_string(i);
for(auto c : s){
if(a[c] == 0) return 0;
else a[c]--;
}
return 1;
}
int main() {
string s;
cin >> s;
int n = s.size();
map<char, int> a;
for(auto c : s) a[c]++;
string ans = "No";
if(n == 1){
if(s == "8") ans = "Yes";
}
else if(n == 2){
for(int i = 16; i < 100; i += 8){
if(f(i,a)) ans = "Yes";
}
}
else {
for(int i = 104; i < 1000; i += 8){
if(f(i,a)) ans = "Yes";
}
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <bits/stdc++.h>
#include <queue>
#include <math.h>
#include <bitset>
#include <map>
#include <vector>
#include <cstdio>
#include <climits>
#define white 0
#define gray 1
#define black 2
#define LIMIT (1<<30)
#define MOD 1000000007
#define ll long long
using namespace std;
class DisjointSet{
public:
vector<ll> rank, p;
DisjointSet(){}
DisjointSet(ll size){
rank.resize(size, 0);
p.resize(size, 0);
for(ll i=0; i<size; i++) makeSet(i);
}
void makeSet(ll x){
p[x]=x;
rank[x]=0;
}
bool same(ll x, ll y){
return findSet(x) == findSet(y);
}
void unite(ll x, ll y){
link(findSet(x), findSet(y));
}
void link(ll x, ll y){
if(rank[x]>rank[y]){
p[y]=x;
}else{
p[x]=y;
if(rank[x]==rank[y]){
rank[y]++;
}
}
}
ll findSet(ll x){
if(x!=p[x]){
p[x]=findSet(p[x]);
}
return p[x];
}
};
int main(){
ll n, m;
cin >> n >> m;
ll a[n], b[n];
for(ll i=0; i<n; i++) cin >> a[i];
for(ll i=0; i<n; i++) cin >> b[i];
DisjointSet ds = DisjointSet(n + 1);
for(ll i=0; i<m; i++){
ll c, d;
cin >> c >> d;
ds.unite(c, d);
}
map<ll, ll> mpA, mpB;
for(ll i=1; i<=n; i++){
mpA[ds.findSet(i)] += a[i - 1];
mpB[ds.findSet(i)] += b[i - 1];
//cout << ds.findSet(i) << endl;
}
bool ans = true;
for(auto it = mpA.begin(); it != mpA.end(); it++){
ll num = it -> first;
//cout << num << " " << mpA[num] << " " << mpB[num] << endl;
if(mpA[num] != mpB[num]){
ans = false;
break;
}
}
if(ans) cout << "Yes" << endl;
else cout << "No" << endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define endl '\n'
vector<int> vis;
vector<vector<int>> g;
int dfs(int u){
int ans=1;
vis[u]=1;
for(auto v:g[u]) if(!vis[v]) ans+=dfs(v);
return ans;
}
void tests(){
int n, m;
cin>>n>>m;
g.resize(n+1);
int res=0;
for(int i=0;i<m;i++){
int u, v;
cin>>u>>v;
g[u].push_back(v);
}
for(int i=1;i<=n;i++){
vis.assign(n+1,0);
res+=dfs(i);
}
cout<<res<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL) ;
int t = 1;
// cin>>t;
while(t--){
tests();
}
return 0 ;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
map<int, string> mountain;
for (int i = 0; i < N; i++) {
string S;
int T;
cin >> S >> T;
mountain[T] = S;
}
auto no2 = mountain.end();
advance(no2, -2);
cout << no2->second << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
using ll = long long;
const int INF = 1001001001;
const ll LINF = 1002003004005006007ll;
void solve(){
int n;
cin >> n;
string s;
int t;
priority_queue<pair<int,string> > memo;
rep(i,n){
cin >> s >> t;
memo.push(make_pair(t,s));
}
memo.pop();
cout << memo.top().second << endl;
return;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(16);
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
template<typename T> inline bool chmin(T &a, const T &b) {if(a>b) {a=b; return true;} return false;}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M, X, Y;
cin >> N >> M >> X >> Y;
X--;
Y--;
map<int, vector<tuple<int, int, int>>> mp;
vector<int> A(M), B(M), T(M), K(M);
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i] >> T[i] >> K[i];
A[i]--;
B[i]--;
mp[A[i]].emplace_back(B[i], T[i], K[i]);
mp[B[i]].emplace_back(A[i], T[i], K[i]);
}
// time, node
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> que;
que.emplace(0, X);
const ll INF = 1LL << 60;
vector<ll> used(N, INF);
used[X] = 0;
ll ans = -1;
while (que.size()) {
ll time;
int node;
tie(time, node) = que.top();
que.pop();
if (time > used[node]) continue;
used[node] = true;
if (node == Y) {
ans = time;
break;
}
for (auto tp : mp[node]) {
int nx, cost, mag;
tie(nx, cost, mag) = tp;
int dif = (time % mag == 0 ? 0 : mag - time % mag);
ll reach_time = time + dif + cost;
if (chmin(used[nx], reach_time)) {
que.emplace(reach_time, nx);
}
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#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 ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
int N,Q;
int C[202020];
map<int,int> M[202020];
int P[202020];
template<int um> class UF {
public:
vector<int> par,rank,cnt;
UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
void reinit() {int i; FOR(i,um) rank[i]=0,cnt[i]=1,par[i]=i;}
int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
int count(int x) { return cnt[operator[](x)];}
int operator()(int x,int y) {
if((x=operator[](x))==(y=operator[](y))) return x;
if(cnt[x]<cnt[y]) cnt[y]=cnt[x]+cnt[y]; return par[x]=y;
cnt[x]=cnt[x]+cnt[y]; return par[y]=x;
}
};
UF<202020> uf;
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N>>Q;
FOR(i,N) {
cin>>C[i+1];
M[i+1][C[i+1]]=1;
P[i+1]=i+1;
}
while(Q--) {
cin>>i>>x>>y;
if(i==1) {
x=uf[x];
y=uf[y];
if(x!=y) {
uf(x,y);
i=uf[x];
j=x+y-i;
if(M[i].size()<M[j].size()) swap(M[i],M[j]);
FORR(m,M[j]) M[i][m.first]+=m.second;
}
}
else {
x=uf[x];
cout<<M[x][y]<<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;
} |
#include <cstdio>
#include <algorithm>
using namespace std;
int a[300000];
int main() {
int n, k, c, i, j;
long long ans = 0;
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
c = k;
for (i = 0, j = 0; ; i++) {
int x = 0;
while (j < n && a[j] == i) {
x++;
j++;
}
if (x < c) {
ans += (long long)i * (c - x);
c = x;
}
if (c == 0) break;
}
printf("%lld\n", ans);
return 0;
}
| #include <iostream>
#include <vector>
using namespace std;
class UnionFind {
public:
UnionFind(int n) : par(n), rank(n, 0) {
for (int i = 0; i < n; ++i) {
par[i] = i;
}
}
int root(int x) {
return par[x] == x ? x : par[x] = root(par[x]);
}
bool same(int x, int y) {
return root(x) == root(y);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return;
if (rank[x] < rank[y]) {
par[x] = y;
}
else {
par[y] = x;
if (rank[x] == rank[y]) {
++rank[x];
}
}
}
private:
vector<int> par, rank;
};
const int MAX = 200001;
int main() {
int n;
cin >> n;
vector<int> v(n);
for (auto& nx : v) {
cin >> nx;
}
UnionFind uf(MAX);
for (int i = 0; i < n / 2; ++i) {
uf.unite(v[i], v[n - 1 - i]);
}
int ans = 0;
for (int j = 0; j < MAX; ++j) {
if (uf.root(j) != j) {
++ans;
}
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#include <set>
using ll = long long;
const double PI = 3.1415926535897932;
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main()
{
int n, m, t;
int a[1010];
int b[1010];
cin >> n >> m >> t;
for(int i = 1; i <= m; i++)
{
cin >> a[i] >> b[i];
}
b[0] = 0;
bool bat = true;
int battery = n;
for(int j = 1; j <= m; j++)
{
battery -= a[j] - b[j - 1]; if(battery <= 0) bat = false;
battery += b[j] - a[j]; if(battery > n) battery = n;
}
battery -= t - b[m]; if(battery <= 0) bat = false;
if(bat) cout << "Yes" << endl;
else cout << "No" << endl;
} | //debug icin gdb kullanmayi dene
#include<bits/stdc++.h>
using namespace std;
#define mods 1000000007
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define rs resize
#define pii pair<lint,lint>
#define piii pair<lint,pair<lint,lint> >
#define inf LLONG_MAX-7
#define vvL vector<vector<lint> >
#define vvB vector<vector<bool> >
#define vvvL vector<vector<vector<lint> > >
#define vL vector<lint>
#define vB vector<bool>
typedef long long int lint;
typedef unsigned long long int ulint;
lint fastpow(lint tab,lint us){
if(tab==0) return 0;
if(tab==1) return 1;
if(us==0) return 1;
if(us==1) return tab%mods;
tab%=mods;
if(us%2==1) return tab*fastpow(tab,us-1)%mods;
lint a=fastpow(tab,us/2)%mods;
return a*a%mods;
}
lint t=1;
void solve(){
lint l=0,a,b,i,j,k,n,m,T,sum;
cin>>n>>m>>T;
sum=n;
for(i=0;i<m;i++){
cin>>a;
n-=a-l;
if(n<=0){cout<<"No"<<endl; return;}
cin>>b;
n+=b-a;
n=fmin(n,sum);
l=b;
}
n-=T-l;
if(n<=0){cout<<"No"<<endl; return;}
cout<<"Yes"<<endl;
return;
}
int main(){
// freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// cin>>t;
while(t--) solve();
return 0;
}
|
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef __int128 it;
typedef long double ldb;
int main(){
ll s,p;
scanf("%lld%lld",&s,&p);
it sqt=it(s)*s-it(p<<2);
// if(p*4<(s*2-1)){
// puts("No");
// return 0;
// }
ldb tmp=sqrt(ldb(s)*s-(p<<2));
for(it i=max(tmp-1000000,ldb(0)),ed=tmp+1000000; i<=ed; ++i)
if(i*i==sqt){
if((i+s)&1){
puts("No");
return 0;
}
puts("Yes");
return 0;
}
puts("No");
return 0;
} | /*
Author: QAQAutoMaton
Lang: C++
Code: A.cpp
Mail: [email protected]
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define int long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<double> cp;
typedef pair<int,int> pii;
int inf;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;}
template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;}
template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
template<class T>bool sort2(T &a,T &b){return a>b?swap(a,b),1:0;}
#define min mmin
#define max mmax
#define abs aabs
struct __INIT__{
__INIT__(){
fill((unsigned char*)&inf,(unsigned char*)&inf+sizeof(inf),0x3f);
}
}__INIT___;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
template<typename A>
inline bool read (A &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}
inline bool read (char &x) {
while((x=gc())==' '||x=='\n' || x=='\r');
return x!=EOF;
}
inline bool read(char *x){
while((*x=gc())=='\n' || *x==' '||*x=='\r');
if(*x==EOF)return 0;
while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
*x=0;
return 1;
}
template<typename A,typename ...B>
inline bool read(A &x,B &...y){
return read(x)&&read(y...);
}
template<typename A>
inline bool write (A x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}
inline bool write (char x) {
putc(x);
return 0;
}
inline bool write(const char *x){
while(*x){putc(*x);++x;}
return 0;
}
inline bool write(char *x){
while(*x){putc(*x);++x;}
return 0;
}
template<typename A,typename ...B>
inline bool write(A x,B ...y){
return write(x)||write(y...);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
signed main(){
#ifdef QAQAutoMaton
freopen("A.in","r",stdin);
freopen("A.out","w",stdout);
#endif
int s,p;
read(s,p);
for(int i=1;i*i<=p;++i)if(!(p%i)){
if(i+p/i==s)return write("Yes\n");
}
return write("No\n");
}
|
#line 1 "A.cpp"
#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
#if defined(DONLINE_JUDGE)
#define NDEBUG
#elif defined(ONLINE_JUDGE)
#define NDEBUG
#endif
template<typename T> std::vector<T> make_v(size_t a){return std::vector<T>(a);}
template<typename T, typename... Ts> auto make_v(size_t a, Ts... ts){
return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
int main() {
int x, y, z; scanf("%d%d%d", &x, &y, &z);
// cout << "test" << endl;
for(int i = 1e6; i >= 0; i--) {
if(y * z <= x * i) continue;
printf("%d\n", i);
return 0;
}
return 0;
}
| //#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
//typedef vector<long long> vll;
#define rep(i,n) for(long long(i)=0;(i)<(n);(i)++)
#define kiriage(a,b) ((a)+(b)-1)/(b)
int main(){
double x,y,z;
cin >> x >> y >> z;
cout << (long)((y * z - 1) / x) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define rep(i,n) for(int i=0;i<(n);i++)
#define drep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << " = " << (x) << endl;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int H,W;
cin >> H >> W;
vvec<char> S(H,vec<char>(W));
rep(i,H) rep(j,W) cin >> S[i][j];
int ans = 0;
rep(i,H-1) rep(j,W-1){
int c = 0;
rep(a,2) rep(b,2) c += S[i+a][j+b];
if(c%2) ans++;
}
cout << ans << "\n";
} | #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define u unsigned
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 1000000007
#define f(i,n) for(int i=0;i<n;i++)
#define f1(i,n) for(int i=1;i<n;i++)
#define in(i,arr) for(auto &i:arr)cin>>i;
#define out(i,arr) for(auto &i:arr)cout<<i<<" ";
#define all(x) (x).begin(), (x).end()
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb empalce_back
#define fe first
#define se second
#define uset unordered_set
#define umap unordered_map
template <typename T>
ll powe(T base, T to){ll ans=1;while(to>0){if (to & 1)ans =(ans*base)%mod, to--;else base = (base*base)%mod, to /= 2;}return ans;}
ll gcd(ll a,ll b){return (b==0)?a:gcd(b,a%b);}
bool prime(ll n){if(n==1)return 0;for(ll i=2;i*i<=n;i++)if(n%i==0)return 0;return 1;}
int main() {
fast;
int tl = 1;
//cin>>tl;
while (tl--) {
int h, w;
cin >> h >> w;
char mat[h][w];
f(i, h)f(j, w)cin >> mat[i][j];
int ans = 0;
f(i, h)
f(j, w) {
if (mat[i][j] == '#' && mat[i][j - 1] == '.') {
if ((mat[i - 1][j] != mat[i][j]) || (mat[i - 1][j - 1] == '#'))
ans++;
}
}
f(j, w)
f(i, h) {
if (mat[i][j] == '#' && mat[i][j - 1] == '.') {
if ((mat[i - 1][j] != mat[i][j]) || (mat[i - 1][j - 1] == '#'))
ans++;
}
}
// cout<<ans<<" ";
f(i, h / 2)f(j, w) {
swap(mat[i][j], mat[h - i - 1][j]);
}
f(j, w / 2)f(i, h) {
swap(mat[i][j], mat[i][w - j - 1]);
}
// f(i,h)
// {
// f(j,w)cout<<mat[i][j];
// cout<<"\n";
// }
f(i, h)
f(j, w) {
if (mat[i][j] == '#' && mat[i][j - 1] == '.') {
if ((mat[i - 1][j] != mat[i][j]) || (mat[i - 1][j - 1] == '#'))
ans++;
}
}
f(j, w)
f(i, h) {
if (mat[i][j] == '#' && mat[i][j - 1] == '.') {
if ((mat[i - 1][j] != mat[i][j]) || (mat[i - 1][j - 1] == '#'))
ans++;
}
}
cout << ans;
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll,ll>;
#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846
#define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i))
#define FORR(i,v) for(auto (i):v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x) ASC((x)); REV((x))
#define pb push_back
#define eb emplace_back
int main()
{
ll N;
cin >> N;
map<ll,ll> A;
REP(i,0,N) {
ll a;
cin >> a;
++A[a%200];
}
ll cnt = 0;
FORR(a,A) cnt += a.second * (a.second - 1) / 2;
PR(cnt);
return 0;
}
/*
*/ | #include <bits/stdc++.h>
#define int long long
using namespace std;
#define se second
#define fi first
#define M 1e9+7
#define N 1e9
double dp[102][102][102];
void F(){
int a, b, c;
cin >> a >> b >> c;
for(int i = 100; i >= a; i--){
for(int j = 100; j >= b; j--){
for(int k = 100; k >= c; k--){
if(i == 100 || j == 100 || k == 100){
dp[i][j][k] = 0;
continue;
}
double w = i + j + k, x = i, y = j, z = k;
dp[i][j][k] += (dp[i+1][j][k] + 1) * x / w;
dp[i][j][k] += (dp[i][j+1][k] + 1) * y / w;
dp[i][j][k] += (dp[i][j][k+1] + 1) * z / w;
}
}
}
cout << fixed << setprecision(12) << dp[a][b][c];
}
signed main() {
int T = 1;
//cin >> T;
for(int X = 1; X <= T; X++){
F();
}
} |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define F first
#define S second
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ll INF = 2e18;
constexpr ld EPS = 1e-9;
constexpr int MOD = 1e9 + 7;
#define int long long
// Custom hash map
struct custom_hash
{
static uint64_t splitmix64(uint64_t x)
{
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const
{
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
// Use int type for keys
template <typename T1, typename T2>
using safe_map = unordered_map<T1, T2, custom_hash>;
// Operator overloads
// cin >> pair<T1, T2>
template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2> &p)
{
istream >> p.first >> p.second;
return istream;
}
// cin >> vector<T>
template<typename T>
istream& operator>>(istream &istream, vector<T> &v)
{
for (auto &it : v) cin >> it;
return istream;
}
// cout << pair<T1, T2>
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p)
{
ostream << p.first << " " << p.second;
return ostream;
}
// cout << vector<T>
template<typename T>
ostream& operator<<(ostream &ostream, const vector<T> &c)
{
for (auto &it : c) cout << it << " ";
return ostream;
}
// Utility functions
template <typename T>
void print(T &&t)
{
cout << t << "\n";
}
template <typename T, typename... Args>
void print(T &&t, Args &&... args)
{
cout << t << " ";
print(forward<Args>(args)...);
}
template <typename T>
int32_t size_i(T &container) { return static_cast<int32_t>(container.size()); }
// Mathematical functions
int GCD(int a, int b)
{
if (!b) return a;
return GCD(b, a % b);
}
int LCM(int a, int b)
{
return (a * b) / GCD(a, b);
}
int modpow(ll x, int n, int m = MOD) {
ll res = 1;
while (n > 0)
{
if (n & 1)
res = (res * x) % m;
x = (x * x) % m;
n >>= 1;
}
return res;
}
int binpow(int x, int n) {
int res = 1;
while (n > 0)
{
if (n & 1)
res = res * x;
x = (x * x);
n >>= 1;
}
return res;
}
// @param m should be prime
int modinv(int x, int m = MOD)
{
return modpow(x, m - 2, m);
}
// Flags to use: -std=c++17 -O2 -Wshadow -DLOCAL_PROJECT -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -fsanitize=address -fsanitize=undefined
////////////////////////////////// START CODE HERE //////////////////////////////////
void preSolve()
{
}
void solve(int tc)
{
int a, b;
cin >> a >> b;
int ans = 1;
for (int i = 2; i <= 1e5; i++)
{
int y = (b / i) * i;
int x = (a / i) * i;
if (x < a) x += i;
if (x < y) ans = i;
}
print(ans);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(12) << fixed;
preSolve();
int tests = 1;
// cin >> tests;
for (int t = 1; t <= tests; t++)
solve(t);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
int l = b - a;
for (int i = l; i >= 1; i--)
{
int x = (a+i-1)/i;
if (b-x*i>=i)
{
cout<<i<<endl;
return 0;
}
}
cout<<a<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lf;
// #define int ll
#define FOR(i, f, t) for (int i = (f), ed##i = (t); i <= ed##i; ++i)
#define ROF(i, t, f) for (int i = (t), ed##i = (f); i >= ed##i; --i)
#define DBG(x) (cerr << "(dbg) " << #x " = " << (x) << '\n')
#define FIO(x) (freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout))
#define y1 y1__
// clang-format off
struct Reader{template<class T>Reader&operator()(T&x){x=0;T f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();x*=f;return*this;}ll operator()(){ll x;operator()(x);return x;}Reader&operator()(char*x){scanf("%s",x);return*this;}template<class T>Reader&operator()(vector<T>&v,int n){T x;FOR(i,1,n){operator()(x),v.push_back(x);}return*this;}template<class T>Reader&operator()(T*x,int n,int st=1){FOR(i,st,n){operator()(x[i]);}return*this;}}in;template<class T>T Abs(const T&a){return a>0?a:-a;}template<class T>void Up(T&a,const T&b){b>a&&(a=b);}template<class T>void Down(T&a,const T&b){b<a&&(a=b);}template<class T,size_t E>void Fill(T(&a)[E],const int&x){memset(a,x,E*sizeof(*a));}ll Pow(ll a,ll b,const ll&m){ll res=1;a%=m;while(b>0){if(b&1)res=res*a%m;a=a*a%m,b>>=1;}return res;}template<class T>T Gcd(T a,T b){T t;while(b!=0)t=a%b,a=b,b=t;return a;}
// clang-format on
const int N = 2e6 + 5;
typedef pair<int, int> pii;
int n, m, q, x[N], rn[N];
pii a[N];
bitset<N> ban;
void Solve(int l, int r) {
ban.reset();
int ans = 0;
FOR (i, l, r) { ban[i] = 1; }
FOR (i, 1, n) {
FOR (j, 1, m) {
if (!ban[rn[j]] && x[rn[j]] >= a[i].first) {
// cerr << "put " << i << " to " << j << '\n';
ban[rn[j]] = 1;
ans += a[i].second;
break;
}
}
}
// cerr << "end\n";
cout << ans << '\n';
}
signed main() {
in(n)(m)(q);
FOR (i, 1, n) { in(a[i].first)(a[i].second); }
sort(a + 1, a + 1 + n, [](pii a, pii b) { return a.second > b.second; });
in(x, m);
FOR (i, 1, m) { rn[i] = i; }
sort(rn + 1, rn + 1 + m, [](int a, int b) { return x[a] < x[b]; });
FOR (_, 1, q) {
int l = in(), r = in();
Solve(l, r);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = a; i < (b); i++)
int main() {
int n, m, q;
cin >> n >> m >> q;
int x[m];
vector<pair<int, int>> vw(n);
rep(i, 0, n) {
int w, v;
cin >> w >> v;
vw[i].first = v, vw[i].second = w;
}
rep(i, 0, m) cin >> x[i];
sort(vw.rbegin(), vw.rend());
auto query = [&](int l, int r) {
vector<int> tmpm;
rep(i, 0, m) {
if (l <= i && i < r) continue;
tmpm.push_back(x[i]);
}
sort(tmpm.begin(), tmpm.end());
bool conf[n] = { 0 };
int res = 0;
for (auto t : tmpm) {
int maxv = 0;
rep(i, 0, n) {
if (vw[i].second > t || conf[i]) continue;
maxv = max(maxv, vw[i].first);
conf[i] = true;
break;
}
res += maxv;
}
return res;
};
rep(i, 0, q) {
int l, r;
cin >> l >> r;
cout << query(l - 1, r) << endl;
}
} |
#include <bits/stdc++.h>
#define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++)
#define REPS(i, ss, nn ) for(int i = ss ; i < (int) nn; i++)
#define REV(i, ss, nn ) for(int i = ss ; i >= nn; i--)
#define deb(x) std::cout << #x << " " << x << endl;
#define debl(x) std::cout << #x << " " << x << " ";
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
namespace std{
template<class Fun>
class y_combinator_result{
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)){}
template<class ...Args>
decltype(auto) operator()(Args&&...args){
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun && fun){
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
};
template<typename T>
bool u_max(T& a, T b){
if( a < b){
a = b;
return true;
}
return false;
}
template<typename T>
bool u_min(T& a, T b){
if( a > b){
a = b;
return true;
}
return false;
}
struct edge{
int to, from;
// ll cost;
edge(){ edge(0,0);}
// edge(int to_, ll cost_){ edge(to_, -1, cost_);}
// edge(int to_, int from_, ll cost_) : to(to_), from(from_), cost(cost_){}
edge(int to_, int from_) : to(to_), from(from_){};
};
template<typename... T>
void read(T& ... a){
((cin >> a),...);
}
void dfs(int node, int par, vector<vector<int>>& adj, vector<int>& parent){
parent[node] = par;
for(const auto & vert : adj[node]){
if(vert == par) continue;
dfs(vert, node, adj, parent);
}
}
void summary(int node, int par, ll acc, vector<vector<int>>& adj, vector<ll>& add, vector<ll>& results){
results[node] += acc;
for(const auto & vert : adj[node]){
if(vert == par) continue;
summary(vert, node, acc + add[vert], adj, add, results);
}
}
void solve(){
int n;
read(n);
vector<edge> edges(n);
vector<vector<int>> adj(n);
REP(i, n - 1){
int a, b;
read(a, b);
a--; b--;
edges[i].from = a;
edges[i].to = b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vector<int> parent(n, -1);
int root = 0;
dfs(root, -1, adj, parent);
// cout << "dfs done" << endl;
vector<ll> add(n);
int q;
read(q);
REP(i, q){
int t, e, x;
read(t,e, x);
e--;
int a = edges[e].from;
int b = edges[e].to;
if(t == 2){
swap(a, b);
}
//apply left a but not right b
if( a == parent[b]){
add[root] += x;
add[b] -= x;
}else{
add[a] += x;
}
}
vector<ll> results(n);
summary(root, -1, add[0], adj, add, results);
REP(i, n){
cout << results[i] << "\n";
}
}
int main()
{
//making data IO Fast
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
/****************************/
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
int n,query,i,a[200010],b[200010],dis[200010],c[200010],e,x,s,t;
bool vis[200010];
vector<int> adj[200010];
queue<int> q;
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
for (i=1;i<n;i++) cin>>a[i]>>b[i];
for (i=1;i<n;i++) {adj[a[i]].pb(b[i]);adj[b[i]].pb(a[i]);}
for (i=1;i<n+1;i++) vis[i]=false;
dis[1]=0;vis[1]=true;q.push(1);
while (!q.empty())
{
s=q.front();q.pop();
for (auto u:adj[s])
{
if (vis[u]) continue;
vis[u]=true;
dis[u]=dis[s]+1;
q.push(u);
}
}
for (i=1;i<n+1;i++) c[i]=0;s=0;
cin>>query;
for (i=1;i<query+1;i++)
{
cin>>t>>e>>x;
if (t==2) swap(a[e],b[e]);
if (dis[a[e]]>dis[b[e]]) c[a[e]]=c[a[e]]+x;
else
{
s=s+x;c[b[e]]=c[b[e]]-x;
}
if (t==2) swap(a[e],b[e]);
}
t=s;
for (i=1;i<n+1;i++) vis[i]=false;
dis[1]=c[1];q.push(1);vis[1]=true;
while (!q.empty())
{
s=q.front();q.pop();
for (auto u:adj[s])
{
if (vis[u]) continue;
vis[u]=true;
dis[u]=dis[s]+c[u];
q.push(u);
}
}
for (i=1;i<n+1;i++) dis[i]=dis[i]+t;
for (i=1;i<n+1;i++) cout<<dis[i]<<"\n";
} |
#include<bits/stdc++.h>
#define inf 2000000000
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define fastio ios::sync_with_stdio(false);cin.tie(NULL)
#define rng_23 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef set<int> si;
typedef multiset<int> msi;
typedef map<int,int> mii;
int grid[1001][1001],reps[1001][1001];
pii parent[1001][1001];
pii rep(pii a)
{
if(parent[a.fi][a.se]==a)
return a;
return parent[a.fi][a.se]=rep(parent[a.fi][a.se]);
}
int main()
{
int i,j,k;
int n,m;
string str;
cin>>n>>m;
for(i=1;i<=n;i++){
cin>>str;
for(j=1;j<=m;j++){
grid[i][j]=str[j-1];
parent[i][j]=make_pair(i,j);
}
}
grid[1][1]='#';
grid[1][m]='#';
grid[n][1]='#';
grid[n][m]='#';
int r=0,c=0;
for(i=1;i<=n;i++){
int sig=1;
for(j=1;j<=m;j++)
if(grid[i][j]=='#')
sig=0;
r+=sig;
if(sig)
grid[i][1]='*';
}
for(j=1;j<=m;j++){
int sig=1;
for(i=1;i<=n;i++)
if(grid[i][j]=='#')
sig=0;
c+=sig;
if(sig)
grid[1][j]='*';
}
//cout<<r<<" "<<c<<endl;
if(r<c){
for(j=1;j<=m;j++)
if(grid[1][j]=='*')
grid[1][j]='.';
}
else {
for(i=1;i<=n;i++)
if(grid[i][1]=='*')
grid[i][1]='.';
}
for(i=1;i<=n;i++){
vii vec;
for(j=1;j<=m;j++)
if(grid[i][j]=='#' || grid[i][j]=='*')
vec.pb(make_pair(i,j));
pii lrp;
if(!vec.empty())
lrp=rep(vec.front());
for(j=1;j<vec.size();j++){
pii rp=rep(vec[j]);
parent[rp.fi][rp.se]=lrp;
}
}
for(j=1;j<=m;j++){
vii vec;
for(i=1;i<=n;i++)
if(grid[i][j]=='#' || grid[i][j]=='*')
vec.pb(make_pair(i,j));
pii lrp;
if(!vec.empty())
lrp=rep(vec.front());
for(i=1;i<vec.size();i++){
pii rp=rep(vec[i]);
parent[rp.fi][rp.se]=lrp;
}
}
//cout<<"hola"<<endl;
set<pii> cnt;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(grid[i][j]=='#' || grid[i][j]=='*')
cnt.insert(rep(make_pair(i,j)));
cout<<cnt.size()-1+min(r,c)<<endl;
//cout<<rep(make_pair(7,2)).fi<<" "<<rep(make_pair(7,2)).se<<endl;
}
| #include <iostream>
#include <vector>
#include <algorithm>
#include<numeric>
#include<unordered_map>
using namespace std;
#define MP make_pair
#define PB push_back
#define REP(i, L, R) for (int i = L; i < (R); ++i)
#define SZ(x) (int)x.size()
using ll = long long;
using ii = pair<int,int>;
struct DSU {
int n;
// p[i] := rep of the component of i.
vector<int> p;
// rank[i] := size of the component of i.
vector<int> rank;
void init(int sz) {
n = sz;
p.resize(n);
iota(p.begin(), p.end(), 0);
rank.assign(n,1);
}
int getRep(int i) {
if(p[i] == i) {
return i;
}
return p[i] = getRep(p[i]);
}
bool same(int x, int y) {
return getRep(x) == getRep(y);
}
bool merge(int x, int y) {
x = getRep(x);
y = getRep(y);
if(x == y) {
return false;
}
if(rank[x] < rank[y]) {
swap(x, y);
}
rank[x] += rank[y];
p[y] = x;
return true;
}
};
int main()
{
int h, w;
cin >> h >> w;
// Consider bipartite graph: vertices represent rows 0,...h-1
// and cols h,...h+w-1; and edges between rows and columns.
vector<ii> edges;
vector<string> s(h);
REP(i, 0, h) {
cin >> s[i];
REP(j, 0, w) {
if(s[i][j] == '#') {
edges.emplace_back(i, j+h);
}
}
}
// row 0 <-> col 0 (indexed as vertex h)
edges.emplace_back(0, h);
// row 0 <-> col w-1 (last col)
edges.emplace_back(0, h+w-1);
// row h-1 (last row) <-> col 0
edges.emplace_back(h-1, h);
// row h-1 <-> col w-1
edges.emplace_back(h-1, h+w-1);
DSU dsu;
dsu.init(h+w);
for(auto e: edges) {
dsu.merge(e.first, e.second);
}
unordered_map<int, bool> comp;
int cc = 0;
REP(i, 0, h) {
int c = dsu.getRep(i);
if(!comp[c]) {
++cc;
comp[c] = true;
}
}
int ans = cc-1;
comp.clear();
cc = 0;
REP(i, h, h+w) {
int c = dsu.getRep(i);
if(!comp[c]) {
++cc;
comp[c] = true;
}
}
ans = min(ans, cc-1);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int N, M;
cin >> N >> M;
vector<long long> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
vector<vector<int>> graph(N);
for (int i = 0; i < M; i++) {
int X, Y;
cin >> X >> Y;
X--;
Y--;
graph[X].push_back(Y);
}
vector<long long> minimum(N, 1e10);
minimum[0] = A[0];
for (int i = 0; i < N; i++) {
for (auto g : graph[i]) {
minimum[g] = min({minimum[g], minimum[i], A[i]});
}
}
long long ans = -1e10;
for (int i = 1; i < N; i++) {
ans = max(A[i] - minimum[i], ans);
}
cout << ans << endl;
return 0;
} | #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
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);
vector<vector<int>> adj;
vector<int> a;
int n,m;
vector<ar<int,3>> dp;
vector<bool> vis;
// 0-->dp value (profit)
// 1-->max number till here
// 2-->min number till here
vector<int> maxi,mini;
void dfs(int node)
{
maxi[node]=max(maxi[node],a[node]);
mini[node]=min(mini[node],a[node]);
vis[node]=true;
for(auto i:adj[node])
{
mini[i]=min(mini[i],mini[node]);
if(!vis[i])
{
dfs(i);
}
maxi[node]=max(maxi[i],maxi[node]);
}
}
void solve()
{
cin>>n>>m;
a=vector<int> (n);
fi(0,n) cin>>a[i];
adj=vector<vector<int>> (n);
// dp=vector<ar<int,3>> (n);
const int INF=1e13;
maxi=vector<int> (n);
mini=vector<int> (n,INF);
vis=vector<bool> (n);
fi(0,m)
{
int x,y;
cin>>x>>y;
--x,--y;
adj[x].pb(y);
}
fi(0,n)
{
if(!vis[i])
{
dfs(i);
}
}
int ans=-INF;
fi(0,n)
{
for(auto x:adj[i])
{
ans=max(ans,maxi[x]-mini[i]);
}
}
cout<<ans<<'\n';
}
signed main()
{
FAST;
int tt=1;
// cin>>tt;
while(tt--)
{
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
namespace DEBUG {
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
#define rand __rand
mt19937 generator(chrono::system_clock::now().time_since_epoch().count());
template <class T = int>
T rand(T range = numeric_limits <T> :: max()){
return (T) (generator() % range);
}
}
using namespace DEBUG;
template <class T,class U>
void maximize(T &lhs,U rhs){
if(lhs < rhs)lhs = rhs;
}
template <class T,class U>
void minimize(T &lhs,U rhs){
if(lhs > rhs)lhs = rhs;
}
template <class T>
T tabs(T x){
return (x < (T)0 ? -x : x);
}
#define len(c) (int)((c).size())
#define llong int64_t
#define MASK(i) ((1LL) << (i))
#define BIT(x,i) (((x) >> (i)) & 1)
#define file_name "test" /// FILE NAME HERE!!!
/* ================================= END OF TEMPLATE ================================= */
namespace Task{
const llong INF = 1e18;
class Solver{
public:
int n;
llong X;
vector < int > a;
void solve(){
cin>>n>>X;
a.resize(n + 2,0);
for(int i = 1;i <= n;++i){
cin>>a[i];
}
auto compute = [&](const int k) -> llong{
vector < vector < vector < llong > > > dp(n + 2,vector < vector < llong > > (k + 2,vector < llong > (k + 2,-INF)));
dp[0][0][0] = 0;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= k;++j){
for(int m = 0;m < k;++m){
maximize(dp[i][j][m],dp[i - 1][j][m]);
int rm = ((m % k) - (a[i] % k) + k) % k;
maximize(dp[i][j][m],dp[i - 1][j - 1][rm] + (llong)a[i]);
}
}
}
return dp[n][k][X % k];
};
llong ans = INF;
for(int i = 1;i <= n;++i){
llong S = compute(i);
if(S >= 0){
ans = min(ans,(X - S) / i);
}
}
cout<<ans<<'\n';
}
~Solver(){}
};
void solve(){
int ntest = 1;
#ifndef ONLINE_JUDGE
ntest = 2; // single test to multitest.
#endif
//cin>>ntest; // multitest
while(ntest--){
Solver().solve();
}
}
}
int main(int argc, char** argv){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen(file_name".inp","r",stdin);
//freopen(file_name".out","w",stdout);
#endif // ONLINE_JUDGE
Task::solve();
}
| #include<bits/stdc++.h>
#define forn(i,s,t) for(register int i=(s);i<=(t);++i)
#define form(i,s,t) for(register int i=(s);i>=(t);--i)
using namespace std;
typedef long long LL;
const int N = 1e2+3;
const LL INF = 2e18;
template<typename T>inline T Max(T A, T B) {return A>B?A:B;}
template<typename T>inline T Min(T A, T B) {return A<B?A:B;}
LL a[N], f[N][N], X, Ans; int n;
int main() {
scanf("%d%lld", &n, &X);
forn(i,1,n) scanf("%lld", a+i);
Ans = INF;
forn(i,1,n) {
forn(j,1,n) form(k,i-1,0) f[j][k] = -1;
f[0][0] = 0;
forn(j,1,n) form(k,i,0) forn(l,0,i-1) if(f[k][l] != -1) {
LL tmp = f[k][l] + a[j];
f[k+1][tmp % i] = Max(f[k+1][tmp % i], tmp);
}
if(f[i][X % i] != -1) Ans = Min(Ans, (X - f[i][X % i]) / i);
}
printf("%lld\n", Ans);
return 0;
} |
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=200010,mod=1e9+7;
double f[N][2];
bool vis[N];
int main()
{
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= k;i++)
{
int x;
cin >> x;
vis[x] = true;
}
double p = 0, q = 0;
for (int i = n - 1; i >= 0;i--)
{
if(i + m <= n - 1)
{
p -= f[i + m + 1][0];
q -= f[i + m + 1][1];
}
if(!vis[i])
{
f[i][0] = p / m;
f[i][1] = q / m + 1;
}
else
{
f[i][0] = 1;
f[i][1] = 0;
}
p += f[i][0];
q += f[i][1];
}
if(fabs(1.00000-f[0][0])<=1e-12)
puts("-1");
else
printf("%.6lf\n", f[0][1] / (1 - f[0][0]));
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;
}
#ifndef fenwick_tree_hpp
#define fenwick_tree_hpp
#include<vector>
template<typename T> struct Fenwick_Tree {
private:
int n;
std::vector<T> bit;
public:
Fenwick_Tree(int _n): n(_n), bit(_n, 0) {}
// [0, r)の要素の総和
T sum(int i) {
T s = 0;
while (i > 0) {
s += bit[i-1];
i -= i & -i;
}
return s;
}
// [l, r)の要素の総和
T sum(int l, int r) {
return sum(r) - sum(l);
}
void add(int i, T x) {
i += 1;
while (i <= n) {
bit[i-1] += x;
i += i & -i;
}
}
};
#endif // fenwick_tree_hpp
const int M = 100000;
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<int> batsu(n+1, 0);
Fenwick_Tree<double> fw_a(M+M+5), fw_b(M+M+5);
rep(i,k) {
int x; cin >> x;
batsu[x]++;
fw_a.add(x, 1);
}
for (int i = n-1; i >= 0; --i) {
if (batsu[i]) continue;
fw_a.add(i, fw_a.sum(i+1, i+m+1)/m);
fw_b.add(i, fw_b.sum(i+1, i+m+1)/m + 1);
}
double a = fw_a.sum(0, 1);
double b = fw_b.sum(0, 1);
if (1 - a < EPS) cout << -1 << endl;
else {
double ans = b / (1 - a);
cout << fixed << setprecision(10) << ans << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <class T1, class T2>
bool chmax(T1 &a, const T2 b){if(a < b){a = b; return true;} return false;}
template <class T1, class T2>
bool chmin(T1 &a, const T2 b){if(a > b){a = b; return true;} return false;}
ll pow2(const int n){return (1LL << n);}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &i : v) os << i << ' ';
return os;
}
void co() { cout << '\n'; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
cout << head << ' ';
co(forward<Tail>(tail)...);
}
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
cerr << head << ' ';
ce(forward<Tail>(tail)...);
}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const int n){cout << fixed << setprecision(n);}
constexpr int INF = 1000000001;
constexpr ll LINF = 1000000000000000001;
constexpr ll MOD = 1000000007;
constexpr ll MOD_N = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);
struct S {
ll l, v;
bool operator>(const S &rhs) const {
return v > rhs.v;
}
bool operator<(const S &rhs) const {
return v < rhs.v;
}
bool operator==(const S &rhs) const {
return v == rhs.v;
}
void input() {
cin >> l >> v;
}
};
int main(void) {
ll n, m;
cin >> n >> m;
Vec w(n);
rep(i, n) cin >> w[i];
ll max_w = *max_element(all(w));
vector<S> ss(m);
rep(i, m) ss[i].input();
rep(i, m) {
if(max_w > ss[i].v) {
co(-1);
return 0;
}
}
sort(all(ss));
Vec max_l(m);
rep(i, m) {
max_l[i] = ss[i].l;
if (i) chmax(max_l[i], max_l[i - 1]);
}
Vec p(n);
rep(i, n) p[i] = i;
ll ans = LINF;
do {
Vec cs(n);
rep(i, n) {
cs[i] = w[p[i]];
if (i) cs[i] += cs[i - 1];
}
// ce(cs);
Vec q(n, 0);
rep(i, n) {
REP(j, i + 1, n) {
ll pos = lower_bound(all(ss), S{0, cs[j]}) - ss.begin();
if (pos == 0) continue;
chmax(q[j], q[i] + max_l[pos - 1]);
}
REP(j, i + 1, n) cs[j] -= w[p[i]];
}
chmin(ans, q.back());
// ce(ans);
} while (next_permutation(all(p)));
co(ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#ifdef LOCAL
#define Gene template< class
#define Rics printer& operator,
Gene c> struct rge{c b, e;};
Gene c> rge<c> range(c i, c j){ return {i, j};}
struct printer{
~printer(){cerr<<endl;}
Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
Rics(string x){cerr<<x;return *this;}
Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
Gene c >Rics(rge<c> x){
*this,"["; for(auto it = x.b; it != x.e; ++it)
*this,(it==x.b?"":", "),*it; return *this,"]";}
};
#define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
#define dbg(x) "[",#x,": ",(x),"] "
#define tham getchar()
#endif
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define eq(x, y) (fabs((x)-(y))<error)
#define bt(i) (1LL<<(i))
mt19937_64 rng((unsigned)chrono::system_clock::now().time_since_epoch().count());
//const ll mod = ;
//const ld error = ;
const ld PI = acosl(-1);
//inline ll MOD(ll x, ll m = mod){
// ll y = x % m;
// return (y >= 0) ? y: y+m;
//}
//const int/ll inf = ;
//const int nmax = ;
///=========================================== template =======================================================
ll req[8][8];
ll memo[8];
int main(){
FASTIO;
int n, m;
cin>>n>>m;
vector<ll> input(n);
vector<pair<ll, ll>> v(m);
ll tot = 0;
ll mx = 0;
for(int i = 0; i<n; i++){
cin>>input[i];
mx = max(input[i], mx);
}
bool good = true;
for(int i = 0; i<m; i++){
cin>>v[i].second>>v[i].first;
tot += v[i].second;
if(v[i].first < mx)
good = false;
}
if(!good){
cout<<-1;
return 0;
}
v.emplace_back(0ll, 0ll);
sort(input.begin(), input.end());
sort(v.begin(), v.end());
for(int i = 1; i<=m; i++){
v[i].second = max(v[i].second, v[i-1].second);
}
ll ans = 1e18;
do{
for(int i = 0; i<n; i++){
ll w = 0;
for(int j = i; j<n; j++){
w += input[j];
req[i][j] = (lower_bound(v.begin(), v.end(), make_pair(w, -1ll))-1)->second;
}
}
bool ok = true;
for(int i = 0; i<n; i++){
if(req[i][i] > 0)
ok = false;
memo[i] = 0;
}
if(!ok) continue;
for(int i = 0; i<n; i++){
for(int j = i+1; j<n; j++){
memo[j] = max(memo[j], memo[i] + req[i][j]);
}
}
ans = min(ans, memo[n-1]);
}while(next_permutation(input.begin(), input.end()));
if(ans == ll(1e18))
ans = -1;
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
int main() {
ll N;
cin >> N;
vl A(2 * N), F(2 * N);
rep(i, 2 * N) cin >> A[i];
vc<pair<ll, ll>> P(2 * N);
rep(i, 2 * N) P[i] = {A[i], i};
sort(all(P));
rep(i, N) F[P[i].second] = 1;
ll cnt0 = 0, cnt1 = 0;
string ans;
rep(i, 2 * N){
if(F[i] == 0){
if(cnt1 > 0){
cnt1--;
ans += ')';
}else{
cnt0++;
ans += '(';
}
}else{
if(cnt0 > 0){
cnt0--;
ans += ')';
}else{
cnt1++;
ans += '(';
}
}
}
cout << ans << endl;
} | #include <iostream>
#include <cmath>
#include <utility>
#include <algorithm>
#include <vector>
using namespace std;
#define mp make_pair
#define x first
#define y second
typedef pair<int,int> pii;
typedef long long ll;
typedef pair<ll,int> pl;
int arr[400005];
pii a[400004];
vector<int> st;
int col[400005];
char s[400004];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=2*n;i++){
cin>>arr[i];
a[i]=mp(arr[i],i);
}
sort(a+1,a+2*n+1);
for(int i=1;i<=n;i++){
col[a[i].y]=1;
}
for(int i=1;i<=2*n;i++){
if(st.empty() or col[st.back()]==col[i]) st.push_back(i);
else{
s[st.back()]='(';
s[i]=')';
st.pop_back();
}
}
for(int i=1;i<=2*n;i++) cout<<s[i];
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> pll;
const ll inf = 1LL<<60;
int main(){
int n; cin >> n;
vector<pll> vec(n+1, make_pair(inf, -1*inf));
rep(i, n)
{
ll x, c; cin >> x >> c;
vec[c].first = min(vec[c].first, x);
vec[c].second = max(vec[c].second, x);
}
vector<pll> dp(n+1, make_pair(0, 0));
ll l_l = 0, l_r = 0;
rep(i, n)
{
ll l = vec[i+1].first, r = vec[i+1].second;
if(l == inf){
dp[i+1].first = dp[i].first;
dp[i+1].second = dp[i].second;
}else{
ll ff,fs,sf,ss;
if(l_l < r) ff = (r - l_l) + (r - l);
else ff = l_l - l;
if(l_r < r) sf = (r - l_r) + (r - l);
else sf = l_r - l;
dp[i+1].first = min(dp[i].first + ff, dp[i].second + sf);
if(l < l_l) fs = (l_l - l) + (r - l);
else fs = r - l_l;
if(l < l_r) ss = (l_r-l) + (r-l);
else ss = r - l_r;
dp[i+1].second = min(dp[i].first + fs, dp[i].second + ss);
l_l = l;
l_r = r;
//cout << ff << ':' << sf << ':' << fs << ':' << ss << endl;
//cout << dp[i+1].first << ':' << dp[i+1].second << endl;
}
}
ll ans = min(dp[n].first + abs(l_l), dp[n].second + abs(l_r));
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
const int INF = 2e9;
vector<int> divisor(int x) {
vector<int> ret;
for (int i = 1; i * i <= x; i++) {
if (x % i == 0) {
ret.emplace_back(i);
if (i * i != x) ret.emplace_back(x / i);
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int mini = INF;
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
for (int d : divisor(x)) {
mp[d] = __gcd(mp[d], x);
}
mini = min(mini, x);
}
int ans = 0;
for (auto& e : mp) {
if (e.first > mini) continue;
ans += (e.first == e.second);
}
cout << ans << '\n';
return 0;
} |
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author NikuKyabe
*/
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define REV(i, a, b) for (int i = (b) - 1; i >= (a); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001l
#define MOD 1000000007
#define fcout cout << fixed << setprecision(12)
using namespace std;
typedef pair<int, int> P;
class AHealthMDeath {
public:
void solve(std::istream& cin, std::ostream& cout) {
int m,h;
cin >> m >> h;
cout << (h % m == 0 ? "Yes" : "No") << endl;
}
};
signed main() {
AHealthMDeath solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int m,h;
cin>>m>>h;
cout<<(h%m==0?"Yes" : "No")<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define memf(a) memset(a,false,sizeof(a))
#define all(v) (v).begin(),(v).end()
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define DEC(x) fixed<<setprecision(x)
#define FAST ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
const int M=1000000007;
const int MM=998244353;
const long double PI = acos(-1);
const long long INF=2e18;
template<typename T,typename T1>void amax(T &a,T1 b){if(b>a)a=b;}
template<typename T,typename T1>void amin(T &a,T1 b){if(b<a)a=b;}
ll power( ll b, ll e)
{
if(e==0) return 1;
if(e&1) return b*power(b*b,e/2);
return power(b*b,e/2);
}
ll power(ll b,ll e,ll m)
{
if(e==0) return 1;
if(e&1) return b*power(b*b%m,e/2,m)%m;
return power(b*b%m,e/2,m);
}
ll modinv(ll a,ll m)
{
return power(a,m-2,m);
}
// #define _NCR_
int TLE_TERROR()
{
ll n,c,l,r,k,j,z,zz,ans=0;
cin>>n>>c;
map<ll,ll>m;
for(ll i=0;i<n;i++)
{
cin>>l>>r>>k;
m[l]+=k;
m[r+1]-=k;
}
for(auto it=m.begin();it!=m.end();it++)
{
if(it==m.begin())
{
z=it->S;
continue;
}
it->S+=z;
z=it->S;
}
// for(auto it=m.begin();it!=m.end();it++)
// {
// cout<<it->F<<" "<<it->S<<endl;
// }
for(auto it=m.begin();it!=m.end();it++)
{
if(it==m.begin())
{
z=it->F;
zz=it->S;
if(zz<c)
ans+=zz;
else
ans+=c;
continue;
}
// cout<<ans<<endl;
ll y=it->F-z;
y--;
if(zz<c)
ans+=y*(zz);
else
ans+=y*c;
if(it->S<c)
ans+=it->S;
else
ans+=c;
z=it->F;
zz=it->S;
}
cout<<ans<<endl;
return 0;
}
int main()
{
FAST
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll int TESTS=1;
// cin>>TESTS;
#ifdef _NCR_
initialvalues();
#endif
while(TESTS--)
TLE_TERROR();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)
void solve() {
int N, C;
cin >> N >> C;
vector<pair<int, int>> event;
REP(i, N) {
int a, b, c;
cin >> a >> b >> c;
event.emplace_back(a - 1, c);
event.emplace_back(b, -c);
}
sort(ALL(event));
int ans = 0, fee = 0, t = 0;
for (auto [x, y] : event) {
if (x != t) {
ans += min(C, fee) * (x - t);
t = x;
}
fee += y;
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long s = 0, a, n;
map<long long, long long> m;
cin >> n;
m[0] += 1;
for (int i = 0; i < n; i++) {
cin >> a;
if (i % 2 == 0) s += a;
else s -= a;
m[s] += 1;
}
long long count = 0;
for (auto i : m) {
count += i.second * (i.second - 1) / 2;
}
cout << count << endl;
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 (in i = 0; i < (in)(n); i++)
#define REP(i,a,b) for(in i=(in)(a);i<(in)(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 in INF=(in)1e18+7;
const in inf=max(in(1e18)+7,in(1e9)+7);
using P=pair<in,in>;
using T=tuple<in,in,in>;
vector<in> dx={0,1,-1,0};
vector<in> 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<<": "
int main(){
ios::sync_with_stdio(false);cin.tie(nullptr);
//cout << fixed << setprecision(12);
in n; cin>>n;
vector<in> a(n);
rep(i,n) cin>>a[i];
vector<in> b(n);
map<in,in> mp0,mp1;
set<in> st0,st1;
rep(i,n){
if(i==0) b[i]=a[i];
else{
b[i] = a[i]-b[i-1];
if(i%2==0) mp0[b[i]] ++;
else mp1[b[i]] ++;
}
}
in cnt=0,sum=0;
for(in i=0;i<n-1;i++){
cnt += mp0[sum]+mp1[-sum];
if((i+1)%2==0) mp0[b[i+1]] --;
else mp1[b[i+1]] --;
if(i%2==0) sum += a[i];
else sum -= a[i];
}
cout<<cnt<<"\n";
}/*
./problem.exe
*/ |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int>people_weight(n);
vector<int>bag_weight(n);
vector<int>has_bag(n);
set<pair<int,int>>people_priority;
vector<int>has_my_bag(n);
for(int i = 0; i < n; ++i){
cin >> people_weight[i];
people_priority.insert({people_weight[i],i});
}
for(int j = 0; j < n; ++j){
cin >> bag_weight[j];
}
for(int i = 0; i < n; ++i){
cin >> has_bag[i];
has_bag[i]--;
has_my_bag[has_bag[i]] = i;
}
bool works = true;
vector<pair<int,int>>ans;
while(people_priority.size()){
auto nxt = *people_priority.begin();
int me = nxt.second;
people_priority.erase(people_priority.begin());
if(has_bag[me] == me){
continue;
}
else if(bag_weight[has_bag[me]] >= people_weight[me]){
//cerr << me << " has a weight of "<< people_weight[me]
//<< " he is carrying a bag of weight of "<<
// bag_weight[has_bag[me]] << "\n";
works = false;
break;
}
else{
int who = has_my_bag[me];
if(bag_weight[has_bag[who]] >= people_weight[who]){
works = false;
break;
}
has_my_bag[has_bag[me]] = who;
has_bag[who] = has_bag[me];
has_bag[me] = me;
ans.emplace_back(me,who);
//cerr << me << " and "<< who << " are exchanging bags\n";
}
}
if(works){
cout << ans.size() << "\n";
for(auto a : ans){
cout << a.first+1 << " " << a.second+1 << "\n";
}
}
else {
cout << "-1\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> A(n), B(n), P(n);
copy_n(istream_iterator<int>(cin), n, A.begin());
copy_n(istream_iterator<int>(cin), n, B.begin());
copy_n(istream_iterator<int>(cin), n, P.begin());
for (auto &p: P)
p--;
for (int i = 0; i < n; i++){
if (i != P[i] and A[i] <= B[P[i]]){
puts("-1");
return 0;
}
}
vector<array<int, 2>> AI(n);
for (int i = 0; i < n; i++){
AI[i][0] = A[i];
AI[i][1] = i;
}
sort(AI.begin(), AI.end());
vector<int> Q(n);
for (int i = 0; i < n; i++)
Q[P[i]] = i;
vector<array<int, 2>> ans;
for (const auto &[a, i]: AI){
if (i == P[i])
continue;
const int j = Q[i];
ans.push_back({i, j});
Q[P[i]] = j;
Q[i] = i;
swap(P[i], P[j]);
}
cout << ans.size() << endl;
for (const auto &[i, j]: ans)
cout << i + 1 << " " << j + 1 << "\n";
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
char str[N][N];
int n, m, ans;
struct Res {
int x, y;
};
bool cmp1(Res a, Res b) {
return a.x != b.x ? a.x < b.x : a.y < b.y;
}
bool cmp2(Res a, Res b) {
return a.y != b.y ? a.y < b.y : a.x < b.x;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> str[i] + 1;
}
vector<Res> vt;
vt.clear();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (str[i][j] == '#' && str[i - 1][j] == '.') {
vt.push_back({i, j});
}
}
}
sort(vt.begin(), vt.end(), cmp1);
for (int i = 1; i < vt.size(); i++) {
if (vt[i].x == vt[i - 1].x && vt[i].y == vt[i - 1].y + 1) {
continue;
}
ans++;
}
ans++;
vt.clear();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (str[i][j] == '#' && str[i + 1][j] == '.') {
vt.push_back({i, j});
}
}
}
sort(vt.begin(), vt.end(), cmp1);
for (int i = 1; i < vt.size(); i++) {
if (vt[i].x == vt[i - 1].x && vt[i].y == vt[i - 1].y + 1) {
continue;
}
ans++;
}
ans++;
vt.clear();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (str[i][j] == '#' && str[i][j + 1] == '.') {
vt.push_back({i, j});
}
}
}
sort(vt.begin(), vt.end(), cmp2);
for (int i = 1; i < vt.size(); i++) {
if (vt[i].y == vt[i - 1].y && vt[i].x == vt[i - 1].x + 1) {
continue;
}
ans++;
}
ans++;
vt.clear();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (str[i][j] == '#' && str[i][j - 1] == '.') {
vt.push_back({i, j});
}
}
}
sort(vt.begin(), vt.end(), cmp2);
for (int i = 1; i < vt.size(); i++) {
if (vt[i].y == vt[i - 1].y && vt[i].x == vt[i - 1].x + 1) {
continue;
}
ans++;
}
ans++;
cout << ans << "\n";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int n, a;
cin >> n;
string s, t;
vector<int> p, q;
cin >> s >> t;
for(int i = 0; i < n; i++){
if(s[i] == '0')
p.push_back(i);
if(t[i] == '0')
q.push_back(i);
}
a = 0;
if(p.size() != q.size()){
a = -1;
}
else{
for(int i = 0; i < p.size(); i++){
if(p[i] != q[i])
a++;
}
}
cout << a << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e+7;
const ld PI = acos(-1);
const ld EPS = 1e-8;
const ll LINF = 1LL<<60;
const int INF = 1LL<<17;
#define rep(i, a, b) for(ll i=a; i<(ll)(b); i++)
#define rrep(i, a, b) for(ll i=b-1; a<=i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
ll ceil(ll a, ll b) {if(a==0) return 0; return (a-1)/b+1;}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
string YES[2] = {"NO", "YES"};
string yes[2] = {"No", "Yes"};
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<1000000007>;
int main(){
ll n; cin >> n;
if(n%2) cout << "Black" << endl;
else cout << "White" << endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define N 107
#define ldb long double
char s[N][N];
int g[N][N],cnt[N],n;
bool vis[N];
ldb f[N],cof[N];
void dfs(int v){
cnt[v]++; vis[v]=1;
for(int i=1;i<=n;i++)
if(g[v][i]&&!vis[i])
dfs(i);
}
int main(){
//freopen("cpp.in","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
for(int j=1;j<=n;j++)
g[i][j]=s[i][j]-'0';
}
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
dfs(i);
}
for(int i=1;i<=n;i++)cof[i]=(1.0-1.0*cnt[i]/n),f[i]=1;
ldb ans=0;
for(int o=1;o<=100000;o++){
for(int i=1;i<=n;i++)ans+=f[i];
for(int i=1;i<=n;i++)f[i]=f[i]*cof[i];
}
ans/=n;
printf("%.10Lf\n",ans);
return 0;
}
|
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;
//struct edge{lint to,num;};
//vector<int> bucket[MAX_N/1000];
int main(void){
int N;
cin >> N;
string S,T;
cin >> S >> T;
int scnt=0,tcnt=0;
rep(i,N){
if(S[i]=='1') scnt++;
if(T[i]=='1') tcnt++;
}
if(scnt<tcnt){
cout << -1 << endl;
return 0;
}
if((scnt-tcnt)%2){
cout << -1 << endl;
return 0;
}
lint ans=0;
lint rem=scnt-tcnt;
set<int> ones;
rep(i,N) if(S[i]=='1') ones.insert(i);
rep(i,N){
if(S[i]==T[i]){
if(S[i]=='1') ones.erase(ones.begin());
continue;
}
if(S[i]=='1'){
if(rem==0){
cout << -1 << endl;
return 0;
}
auto itr=ones.begin();
itr++;
int plc=*itr;
ans+=plc-i;
S[plc]='0';
ones.erase(itr);
ones.erase(i);
rem-=2;
if(rem<0){
cout << -1 << endl;
return 0;
}
}
else if(T[i]=='1'){
auto itr=ones.begin();
int plc=*itr;
ans+=plc-i;
S[plc]='0';
ones.erase(itr);
}
}
cout << ans << endl;
} | #include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) -> iterator of kth element (in sorted fashion : low - high)
// order_of_key(x) -> #numbers strictly less than x
#pragma GCC optimize("Ofast","unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")
#define sync ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
#define unq(a) sort(all(a));a.resize(unique(all(a)) - a.begin())
#define fi first
#define se second
#define endl '\n'
using pii = pair<int , int>;
using ll = long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937 rng(0);
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
sync
int t = 1;
//cin >> t;
auto get = [&](vector<ll>& x, vector<ll>& y){
set<ll> hi(all(x));
set<ll, greater<ll>> lo(all(x));
ll mx = 1e18;
for (const ll& v : y){
auto nx = hi.lower_bound(v);
if (nx != hi.end()) mx = min(mx , *nx - v);
auto fx = lo.lower_bound(v);
if (fx != lo.end()) mx = min(mx , v - *fx);
}
return mx;
};
while(t--){
int n;
cin >> n;
n = 2 * n;
vector<ll> r, g, b;
for (ll bu, i = 0; i < n; i++){
char c;
cin >> bu >> c;
if (c == 'R') r.push_back(bu);
else if (c == 'G') g.push_back(bu);
else b.push_back(bu);
}
if ((r.size() & 1) or (b.size() & 1)){
vector<ll> x, y, z;
if (r.size() & 1){
x = r;
if (g.size() & 1){
y = g; z = b;
}
else{
y = b; z = g;
}
}
else{
x = g; y = b; z = r;
}
assert(x.size() & 1);
assert(y.size() & 1);
ll mx = get(x , y);
if (z.size()){
vector<array<ll , 2>> up, dwn;
set<ll> hi(all(x));
set<ll, greater<ll>> lo(all(x));
for (int i = 0; i < z.size(); i++){
ll nw = 1e18;
auto nx = hi.lower_bound(z[i]);
if (nx != hi.end()) nw = min(nw , *nx - z[i]);
auto fx = lo.lower_bound(z[i]);
if (fx != lo.end()) nw = min(nw , z[i] - *fx);
up.push_back({nw , i});
}
hi = set<ll>(all(y));
lo = set<ll, greater<ll>>(all(y));
for (int i = 0; i < z.size(); i++){
ll nw = 1e18;
auto nx = hi.lower_bound(z[i]);
if (nx != hi.end()) nw = min(nw , *nx - z[i]);
auto fx = lo.lower_bound(z[i]);
if (fx != lo.end()) nw = min(nw , z[i] - *fx);
dwn.push_back({nw , i});
}
sort(all(up)); sort(all(dwn));
for (int i = 0; i < up.size(); i++){
int ix = -1;
for (int j = 0; j < dwn.size(); j++){
if (up[i][1] == dwn[j][1]) continue;
ix = j;
break;
}
assert(ix != -1);
mx = min(mx , up[i][0] + dwn[ix][0]);
}
}
cout << mx << endl;
}
else{
cout << 0 << endl;
}
}
cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s ";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
using ld = long double;
const ld et=1e-9;
int32_t main(){
ios::sync_with_stdio(0),cin.tie(0);
int n,m,k;
cin >> n >> m >> k;
vector <ld> p(n+1),e(n+1);
int cnt[n+1]={};
p[n]=e[n]=cnt[n]=0;
for(int i=0;i<k;i++){
int x;
cin >> x;
cnt[x]=1;
}
for(int i=n-1;i>=0;i--){
if(!cnt[i]){
p[i]=(p[i+1]-p[min(i+m+1,n)])/m;
e[i]=(e[i+1]-e[min(i+m+1,n)])/m+1;
}
else e[i]=0,p[i]=1;
e[i]+=e[i+1],p[i]+=p[i+1];
cerr << i << '\n';
}
p[0]-=p[1],e[0]-=e[1];
if(p[0]>=1-et)cout << "-1";
else{
cout << setprecision(10) << e[0]/(1-p[0]);
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (int)(a); (i) < (int)(b); (i)++)
#define rrep(i, a, b) for (int i = (int)(b) - 1; (i) >= (int)(a); (i)--)
#define all(v) v.begin(), v.end()
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = vector<V<T>>;
/* 提出時これをコメントアウトする */
#define LOCAL true
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif
typedef vector<ll> vec;
typedef vector<vec> mat;
ll M = 1e9+7;
mat mul(mat &A, mat &B) {
mat C(A.size(), vec(B[0].size()));
for (int i = 0; i < A.size(); i++) {
for (int k = 0; k < B.size(); k++) {
for (int j = 0; j < B[0].size(); j++) {
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M;
}
}
}
return C;
}
mat pow(mat A, ll n) {
mat B(A.size(), vec(A.size()));
for (int i = 0; i < A.size(); i++) {
B[i][i] = 1;
}
while (n > 0) {
if (n & 1) B = mul(B, A);
A = mul(A, A);
n >>= 1;
}
return B;
}
ll mod = 1e9+7;
long long mp(long long a, long long b) {
long long res = 1;
for (; b; b >>= 1) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
}
return res;
}
ll rev2, revm;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
constexpr char endl = '\n';
rev2 = mp(2,mod-2);
ll n,m,k;
cin >> n >> m >> k;
revm = mp(m, mod-2);
V<ll> a(n);
rep(i,0,n) cin >> a[i];
V<ll> x(m), y(m);
mat maze(n, vec(n));
rep(i,0,m) {
cin >> x[i] >> y[i];
x[i]--;
y[i]--;
maze[x[i]][y[i]] = 1;
maze[y[i]][x[i]] = 1;
}
V<ll> cnt(n);
rep(i,0,m) {
cnt[x[i]]++;
cnt[y[i]]++;
}
mat A(n, vec(n,0));
rep(i,0,n) {
rep(j,0,n) {
if (maze[i][j] == 1) {
A[i][j] = rev2 * revm % mod;
}
}
}
rep(i,0,n) {
// cnt/2m
A[i][i] = ((cnt[i] * revm) % mod * rev2) % mod;
A[i][i] %= mod;
// (m-cnt)/m
A[i][i] += ((m-cnt[i]) * revm) % mod;
A[i][i] %= mod;
}
// rep(i,0,n) {
// rep(j,0,n) {
// cout << setw(10) << A[i][j];
// }
// cout << endl;
// }
// cout << endl;
mat B = pow(A, k);
vec ret(n);
rep(i,0,n) {
rep(j,0,n) {
ret[i] += B[i][j] * a[j] % mod;
ret[i] %= mod;
}
}
rep(i,0,n) {
cout << ret[i] << endl;
}
return 0;
} |
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int>
#define vivi vector <vi>
#define v(s) vector<s>
#define rsz(a,n) a.resize(n)
#define rszv(a,n,v) a.resize(n,v)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine
const int max_n = 1005;
using namespace std;
int h,w;
bool usd[max_n][max_n],hv[4][max_n],a[max_n][max_n];
int next[4][max_n][max_n];
void read() {
cin >> h >> w;
rep(i,h){
string s;
cin >> s;
rep(j,sz(s)) {
if(s[j]=='#') {
a[i][j]=1;
}
}
}
}
void dfs(int x,int y,int type) {
if(usd[x][y]) return;
usd[x][y]=1;
rep(i,h) {
if(!a[i][y] or usd[i][y])continue;
dfs(i,y,type);
break;
}
rep(i,w) {
if(!a[x][i] or usd[x][i])continue;
dfs(x,i,type);
break;
}
hv[0][y]=1;
hv[1][x]=1;
}
int slv(int type = 0) {
rep(i,h){
rep(j,w)usd[i][j]=0;
}
rep(i,max(h,w)){rep(t,2)hv[t][i]=0;}
rep(i,w) {
if(a[0][i])dfs(0,i,type);
if(a[h-1][i])dfs(h-1,i,type);
}
rep(i,h) {
if(a[i][0])dfs(i,0,type);
if(a[i][w-1])dfs(i,w-1,type);
}
int c=0;
rep(i,h) {
rep(j,w) {
if(a[i][j] == 1 and usd[i][j]==0) {
dfs(i,j,type);
c++;
}
}
}
int e=0,r=0;
crep(i,1,h-1) {
if(hv[1][i]==0) e++;
}
crep(i,1,w-1) {
if(hv[0][i]==0) r++;
}
return c+min(e,r);
}
int main(){
fcin;
read();
int ans = slv();
cout << ans << "\n";
/*
*/
return 0;
} | //#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define Dump(x) \
if (dbg) { \
cerr << #x << " = " << (x) << endl; \
}
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return 1;
} else
return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
} else
return 0;
}
void Yes(bool flag = true) {
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void No(bool flag = true) {
Yes(!flag);
}
void YES(bool flag = true) {
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void NO(bool flag = true) {
YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
} // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans
//UnionFind
//How to use
//unite(x,y) : 集合 x と y を併合する. 併合済のとき false, 未併合のとき true が返される
//find(k) : 要素 k が属する集合を求める
//size(k) : 要素 k が属する集合の要素の数を求める
struct UnionFind {
vector<int> data;
UnionFind(int sz) {
data.assign(sz, -1);
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y)
return (false);
if (data[x] > data[y])
swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if (data[k] < 0)
return (k);
return (data[k] = find(data[k]));
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int k) {
return (-data[find(k)]);
}
};
int H, W;
string S[1010];
//vvi V[1010];
void solve() {
UnionFind uf(H + W);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (S[i][j] == 1) {
uf.unite(i, j + H);
}
}
}
uf.unite(0, H - 1);
uf.unite(0, H);
uf.unite(0, H + W - 1);
set<int> set_r, set_c;
for (int i = 0; i < H; i++) {
set_r.insert(uf.find(i));
}
for (int j = 0; j < W; j++) {
set_c.insert(uf.find(j + H));
}
cout << min(set_r.size() - 1, set_c.size() - 1) << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> H >> W;
for (int i = 0; i < H; i++) {
cin >> S[i];
for (auto& c : S[i]) {
if (c == '.')
c = 0;
else
c = 1;
}
}
solve();
return 0;
}
|
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
using ll=long long;
using namespace std;
int main(){
ll n;
cin>>n;
ll x=1000;
ll ans=0;
while(n>=x){
ans+=n-x+1;
x*=1000;
}
cout<<ans<<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() {
string x;
cin >> x;
int n = x.find('.');
cout << x.substr(0,n) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vpii=vector<pair<int,int>>;
using vpll=vector<pair<ll,ll>>;
using Graph_1=vector<vector<int>>;
const ll INF=1e18;
const int inf=1e9;
const double eps=1e-10;
const double pi=acos(-1);
const ll mod=1e9+7;
const ll MOD=998244353;
//数学関連
int num_order(ll N){
int res=0;
while(N>0){
N/=10;
res++;
}
return res;
}
template<typename T>
T GCD(T A,T B){
if(B==0) return A;
else return GCD(B,A%B);
}
template<typename T>
ll LCM(T A,T B){
return A/GCD(A,B)*B;
}
template<typename T>
vpll prime_factorize(T N){
vpll res;
for(ll i=2;i*i<=N;i++){
if(N%i!=0) continue;
ll ex=0;
while(N%i==0){
ex++;
N/=i;
}
res.push_back({i,ex});
}
if(N!=1) res.push_back({N,1});
return res;
}
template<typename S,typename T,typename U>
ll modpow(S x,T p,U y){
ll res=1;
while(p>0){
if(p&1) res=res*x%y;
x=x*x%y;
p>>=1;
}
return res;
}
template<typename S,typename T>
ll small_nCk(S n,T k){
ll res=1,fact=1;
for(int i=0;i<k;i++){
res=res*(n-i)/fact;
fact++;
}
return res;
}
//データ構造
struct UnionFind {
//自身が親であれば、その集合に属する頂点数に-1を掛けたもの
//そうでなければ親のid
vector<int> r;
UnionFind(int N){
r=vector<int>(N,-1);
}
int root(int x){
if (r[x]<0) return x;
return r[x]=root(r[x]);
}
bool unite(int x,int y){
x=root(x);
y=root(y);
if(x==y) return false;
if(r[x]>r[y]) swap(x,y);
r[x]+=r[y];
r[y]=x;
return true;
}
int size(int x){
return -r[root(x)];
}
};
struct Egde{
int to;
ll W;
Egde(int to,ll W) : to(to),W(W) {}
};
using Graph_2=vector<vector<Egde>>;
int main(){
string S;
cin>>S;
int ans=0;
for(int i=0;i<S.size()-3;i++){
if(S[i]=='Z'&&S[i+1]=='O'&&S[i+2]=='N'&&S[i+3]=='e') ans++;
}
cout<<ans<<endl;
} | #include<iostream>
#include<string>
using namespace std;
int main() {
string s;
cin >> s;
int t = s.size();
for (int i = 0; i < t; i++) {
if (i%2 == 0 && isupper(s[i])) {cout << "No" << endl; return 0;}
if (i%2 == 1 && islower(s[i])) {cout << "No" << endl; return 0;}
}
cout << "Yes" << endl;
return 0;
} |
#include "bits/stdc++.h"
#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};
ll mod(ll a, ll mod){
ll res = a%mod;
if(res<0)res=res + mod;
return res;
}
ll modpow(ll a, ll n, ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
ll modinv(ll a, ll mod){
ll b=mod, u=1, v=0;
while(b){
ll t=a/b;
a-=t*b; swap(a, b);
u-=t*v; swap(u, v);
}
u%=mod;
if(u<0)u+=mod;
return u;
}
ll gcd(ll a, ll b){
ll r = a%b;
if(r==0) return b;
else return gcd(b, a%b);
}
bool is_prime(ll n){
ll i = 2;
if(n==1)return false;
if(n==2)return true;
bool res = true;
while(i*i <n){
if(n%i==0){
res = false;
}
i = i+1;
}
//if(i==1)res = false;
if(n%i==0)res=false;
return res;
}
/**************************************
** A main function starts from here **
***************************************/
int main(){
ll N;
cin >> N;
llvec A(N);
rep(i, N)cin >> A[i];
vector<P> v(N+1, {0, 0});
rep(i, N){
v[i+1] = {v[i].fi+A[i], max(v[i].se, v[i].fi+A[i])};
//cerr << v[i+1].fi << " " << v[i+1].se << endl;
}
ll cur=0;
ll ans = 0;
rep(i, N){
ans = max(cur + v[i+1].se, ans);
cur += v[i+1].fi;
}
cout << ans << endl;
return 0;
}
| #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <chrono>
#include <random>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <iomanip>
#define int long long
#define pii pair<int,int>
#define F first
#define S second
#define mp make_pair
#define endl '\n'
#define all(a) a.begin(),a.end()
#define pb push_back
#define ti(a) ((int)(a-'0'))
#define tc(a) ((char)(a+'0'))
#define mod ((int)1e9+7)
using namespace std;
const int mxn = 3e5+10;
void solve(){
int n ;
cin>>n; vector<int> a(n) ;
for(int& x:a)cin>>x ;
vector<int> b=a;
for(int i=1;i<n;i++)b[i]+=b[i-1];
int coor= 0 , mx = 0 ,ans = 0 ;
for(int i=0;i<n;i++){
mx =max(mx,b[i]) ;
ans = max(mx+coor,ans) ;
coor+=b[i];
}
cout << ans ;
return ;
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin) ;
freopen("out.txt","w",stdout) ;
#endif
int t=1 ;
// cin>>t ;
//cout << t << endl ;
while(t--){
solve() ;
//cout << endl ;
}
return 0 ;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1005;
const int M=1000000007;
int n,jc[N],inv[N];
char c[2][2],s[5];
int C(int n,int m){return (ll)jc[n]*inv[m]%M*inv[n-m]%M;}
int qpow(int x,int y){int ans=1;for(;y;y>>=1,x=(ll)x*x%M)if(y&1)ans=(ll)ans*x%M;return ans;}
int main()
{
scanf("%d",&n);
scanf("%s",s);c[0][0]=s[0];
scanf("%s",s);c[0][1]=s[0];
scanf("%s",s);c[1][0]=s[0];
scanf("%s",s);c[1][1]=s[0];
jc[0]=inv[0]=1;for(int i=1;i<=n;i++)jc[i]=(ll)jc[i-1]*i%M,inv[i]=qpow(jc[i],M-2);
if(c[0][1]=='B'){
if(c[1][1]=='B'||n==2){puts("1");return 0;}
if(c[1][0]=='A')printf("%d",qpow(2,n-3));
else{
int ans=0;
for(int i=0;i<=n-2;i++){
if(i<(n-2-i))continue;
ans=((ll)ans+C(i,n-2-i))%M;
}cout<<ans;
}
}
if(c[0][1]=='A'){
if(c[0][0]=='A'||n==2){puts("1");return 0;}
if(c[1][0]=='B')printf("%d",qpow(2,n-3));
else{
int ans=0;
for(int i=0;i<=n-2;i++){
if(i<(n-2-i))continue;
ans=((ll)ans+C(i,n-2-i))%M;
}cout<<ans;
}
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
const int MN = 1e3 + 5, MOD = 1e9 + 7;
int dp[MN][2];
char o(char c) {return c == 'A' ? 'B' : 'A';}
int fpow (int base, int exp) {
int res = 1; exp = max(exp,0);
while (exp) {
if (exp & 1) res = (long long)res * base % MOD;
base = (long long)base * base % MOD;
exp >>= 1;
}
return res;
}
int main () {
int n; char caa, cab, cba, cbb;
scanf ("%d %c %c %c %c",&n,&caa,&cab,&cba,&cbb);
if (cab == 'B') {
char ccc = caa;
caa = o(cbb);
cbb = o(ccc);
cab = o(cab);
cba = o(cba);
}
if (caa == 'A') printf ("1\n");
else if (cba == 'A') {
dp[1][0] = 1;
for (int i = 2; i <= n; i++) {
dp[i][0] = dp[i-1][0] + dp[i-1][1];
if (dp[i][0] >= MOD) dp[i][0] -= MOD;
dp[i][1] = dp[i-1][0];
}
printf ("%d\n",dp[n][1]);
} else printf ("%d\n",fpow(2,n-3));
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll=int64_t;
#define rep1(i,n) for(ll i=0;i<n;i++)
#define rep2(i,x,n) for(ll i=x;i<n;i++)
int main(){
ll N;cin>>N;
ll counter=0;
vector<ll>V;
for(ll i=2;i<=1e5;i++){
for(ll j=2;j<=34;j++){
ll number=pow(i,j);
if(number<=N){
ll flag=1;
for(auto x:V){
if(number==x){
flag=0;
break;
}
}
if(flag==1){
V.push_back(number);
}
}else{
break;
}
}
}
cout<<N-V.size()<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
//const int INF = 2e9;
//const ll INF = 9e18;
void solve(){
ll N;
cin >> N;
set<ll> st;
ll ans = 0;
for (ll i=2;i*i<=N;i++){
if (st.find(i)!=st.end()) continue;
for (ll j=i*i;j<=N;j*=i){
if (st.find(j)==st.end()) {
ans++;
st.insert(j);
}
}
}
cout << N - ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(9);
solve();
return 0;
} |
Subsets and Splits