code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include<iostream> #include<stdio.h> #include<iomanip> using namespace std; #include<vector> #include<set> #include<map> #include<algorithm> #include<cmath> long long int pw(int x,int n) { long long int res=1; while(n>0) { if(n%2==0) { n=n/2; x=x*x; } else { n=n-1; res=res*x; } } return res; } int main() { long long int k,i,j; cin>>k; long long int cnt[10]={0},cntt[10]={0},cnta[10]={0}; for(i=1;i<=9;i=i+1) { cnt[i]=k; } string s,t; cin>>s>>t; for(i=0;i<s.length()-1;i=i+1) { cnt[s[i]-'0']--; cntt[s[i]-'0']++; } for(i=0;i<t.length()-1;i=i+1) { cnt[t[i]-'0']--; cnta[t[i]-'0']++; } long long int sumt=0,suma=0; for(i=1;i<=9;i=i+1) { sumt=sumt+(i*pw(10,cntt[i])); suma=suma+(i*pw(10,cnta[i])); } long long int prob=0; long long int total=(9*k-8); for(i=1; i<=9; i=i+1) { for(j=1; j<=9; j=j+1) { long long int csumt=sumt-(i*pw(10,cntt[i]))+(i*pw(10,cntt[i]+1)); long long int csuma=suma-(j*pw(10,cnta[j]))+(j*pw(10,cnta[j]+1)); if(csumt>csuma && i!=j) { prob=prob+(cnt[j])*(cnt[i]); } else if(csumt>csuma && i==j) { prob=prob+(cnt[i])*(cnt[i]-1); } } } cout<<(double)prob/total/(total-1) <<endl; }
#define _USE_MATH_DEFINES #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <iomanip> #include <algorithm> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #include <array> //#include <atcoder/all> using namespace std; //using namespace atcoder; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, int> pdi; typedef tuple<ll, ll, ll> tl3; typedef tuple<double, ll, ll> tdl2; //typedef modint998244353 mint; const int BIG_NUM = 1e9 + 5; const ll INF = 1000000000000000000 + 5; //const ll MOD = 1e9 + 7; const ll MOD = 998244353; //const int MOD = 10007; const double EPS = 1e-7; const int MAX = 24 * 60 * 60; ll pow10(ll p) { ll a = 1; for (int i = 0; i < p; i++) { a *= 10; } return a; } int main() { ll k; cin >> k; vector<string> s(2); for (int i = 0; i < 2; i++) { cin >> s[i]; } vector<int> t(10); vector<int> a(10); for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { int num = s[j][i] - '0'; if (j == 0) { t[num]++; } else { a[num]++; } } } ll w = 0; ll sum = 0; for (int i = 1; i < 10; i++) { vector<int> ta(t); ta[i]++; for (int j = 1; j < 10; j++) { vector<int> ao(a); ao[j]++; bool canConsist = true; for (int l = 0; l < 10; l++) { if (ta[l] + ao[l] > k) { canConsist = false; } } if (!canConsist) { continue; } ll sc = 0; for (int l = 0; l < 10; l++) { ll st = l * pow10(ta[l]); sc += st; ll sa = l * pow10(ao[l]); sc -= sa; } ll p = 1; if (i != j) { p *= (k - t[i] - a[i]); p *= (k - t[j] - a[j]); } else { ll r = k - t[i] - a[i]; p *= r * (r - 1); } sum += p; if (sc > 0) { w += p; } } } cout << setprecision(20) << 1.0 * w / sum << endl; }
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208; template <u32 M> struct modint { u32 val; public: static modint raw(int v) { modint x; x.val = v; return x; } modint() : val(0) {} template <class T> modint(T v) { ll x = (ll)(v%(ll)(M)); if (x < 0) x += M; val = u32(x); } modint(bool v) { val = ((unsigned int)(v) % M); } modint& operator++() { val++; if (val == M) val = 0; return *this; } modint& operator--() { if (val == 0) val = M; val--; return *this; } modint operator++(int) { modint result = *this; ++*this; return result; } modint operator--(int) { modint result = *this; --*this; return result; } modint& operator+=(const modint& b) { val += b.val; if (val >= M) val -= M; return *this; } modint& operator-=(const modint& b) { val -= b.val; if (val >= M) val += M; return *this; } modint& operator*=(const modint& b) { u64 z = val; z *= b.val; val = (u32)(z % M); return *this; } modint& operator/=(const modint& b) { return *this = *this * b.inv(); } modint operator+() const { return *this; } modint operator-() const { return modint() - *this; } modint pow(long long n) const { modint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } modint inv() const { return pow(M-2); } friend modint operator+(const modint& a, const modint& b) { return modint(a) += b; } friend modint operator-(const modint& a, const modint& b) { return modint(a) -= b; } friend modint operator*(const modint& a, const modint& b) { return modint(a) *= b; } friend modint operator/(const modint& a, const modint& b) { return modint(a) /= b; } friend bool operator==(const modint& a, const modint& b) { return a.val == b.val; } friend bool operator!=(const modint& a, const modint& b) { return a.val != b.val; } }; using mint = modint<MOD>; template <class T, class U> vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); } template<class... Ts, class U> auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); } template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); } template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); } int main() { int h, w; cin >> h >> w; vector<string> v(h); for (auto &&i : v) { cin >> i; } auto dp1 = make_v(h+1, w+1, mint(0)); auto dp2 = dp1, dp3 = dp1; dp1[1][1] = 1; dp2[1][1] = 1; dp3[1][1] = 1; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if(i == 0 && j == 0) continue; if(v[i][j] == '#') continue; mint val = dp1[i][j+1] + dp2[i+1][j] + dp3[i][j]; dp1[i+1][j+1] = dp1[i][j+1] + val; dp2[i+1][j+1] = dp2[i+1][j] + val; dp3[i+1][j+1] = dp3[i][j] + val; } } cout << (dp1[h][w] - dp1[h-1][w]).val << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define S second #define F first #define f(i,n) for(int i=0;i<n;i++) #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define vi vector<int> #define pii pair<int,int> const int MOD = 1e9+7; const int N = 2005; int dp[N][N][4]; string s[N]; int n,m; void solve() { cin >> n >> m; f(i,n) cin >> s[i]; f(i,n) f(j,m) { if(s[i][j] == '#') continue; if(i == 0 && j == 0) dp[i][j][0] = 1; if(i > 0) dp[i][j][0] += dp[i-1][j][1]; if(j > 0) dp[i][j][0] += dp[i][j-1][2]; if(i > 0 && j > 0) dp[i][j][0] += dp[i-1][j-1][3]; dp[i][j][0]%=MOD; f(k,3) dp[i][j][k+1] = dp[i][j][0]; if(i > 0 && j > 0) dp[i][j][3] = (dp[i][j][3] + dp[i-1][j-1][3])%MOD; if(i > 0) dp[i][j][1] = (dp[i][j][1] + dp[i-1][j][1])%MOD; if(j > 0) dp[i][j][2] = (dp[i][j][2] + dp[i][j-1][2])%MOD; } cout << dp[n-1][m-1][0]; } signed main() { fast; int t = 1; // cin >> t; while(t--) solve(); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define Pr pair<ll,ll> #define Tp tuple<ll,ll,ll> using Graph = vector<vector<int>>; ll mod = 1000000007; int main() { ll N; cin >> N; ll dp[N+1][N+1]; ll sum[N+1]; sum[0] = 0; rep(i,N){ ll a; cin >> a; sum[i+1] = sum[i] + a; } rep(i,N+1){ rep(j,N+1){ dp[i][j] = 0; } } rep(i,N) dp[1][i+1] = 1; vector<int> prev; for(ll i=2;i<=N;i++){ prev.assign(N+1,-1); prev[0] = 0; rep(j,N){ ll k = sum[j+1]%i; if(prev[k]!=-1){ dp[i][j+1] = dp[i][prev[k]] + dp[i-1][prev[k]]; dp[i][j+1] %= mod; } prev[k] = j+1; } } ll ans = 0; rep(i,N+1){ ans += dp[i][N]; ans %= mod; } cout << ans << endl; }
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <chrono> #include <random> #include <bitset> #include <iomanip> #include <climits> #define ll long long #define mod 1000000007 // templates #define all(v) v.begin(),v.end() #define F first #define S second #define sz(x) (int)x.size() #define po(x,y) fixed<<setprecision(y)<<x #define ss(s) scanf(" %[^\n]%*c", s) #define sc(n) scanf("%d",&n) #define sl(n) scanf("%lld",&n) #define ps(s) printf("%s\n",s) #define pr(n) printf("%d\n",n) #define pl(n) printf("%lld\n",n) #define prs(n) printf("%d ",n) #define pls(n) printf("%lld ",n) using namespace std; const ll inf = (ll)1e15 + 10; const int N = 19; ll dp[N][1 << N]; vector<pair<int, int>>v[N]; int n, m; ll calc(int p, int mask) { if (dp[p][mask] != -1)return dp[p][mask]; ll ans = 0; for (auto x : v[p]) { int y = x.F, z = x.S; int rem = mask % (1 << y); int d = __builtin_popcount(rem); if (d > z) { return dp[p][mask] = 0; } } if (p == n) { return dp[p][mask] = 1; } for (int i = 0; i < n; i++) { if ((mask >> i) & 1)continue; ans += calc(p + 1, mask | (1 << i)); } return dp[p][mask] = ans; } void solve() { memset(dp, -1LL, sizeof(dp)); sc(n), sc(m); for (int i = 0; i < m; ++i) { int x, y, z; sc(x), sc(y), sc(z); v[x].push_back({y, z}); } pl(calc(0, 0)); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t = 1; // sc(t); while (t--) { solve(); } // cerr<< (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define endl '\n' string s[100005]; int dist(string& a, string& b) { int ret = 0; for (size_t i = 0; i < a.length(); i++) { if (a[i] != b[i]) ret++; } return ret; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; } long long first = 0; long long ans = 0; for (int i = 1; i < n; i++) { if (dist(s[0], s[i]) & 1) first++; } ans = first; for (int i = 1; i < n; i++) { if (dist(s[0], s[i]) & 1) { ans += n - first; } else { ans += first; } } cout << ans / 2 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n, m; cin >> n >> m; long long odd = 0, even = 0; for(int i = 0; i < n; ++i){ int count = 0; string s; cin >> s; for(int j = 0; j < m; ++j){ if(s[j] == '1'){count += 1;} } if(count%2 == 1){odd += 1;} else{even += 1;} } cout << odd*even << endl; }
#include <math.h> #include <assert.h> #include <algorithm> #include <set> #include <iostream> #include <vector> #include <iomanip> #include <queue> #include <map> #include <string> #include <cstring> #include <functional> #include <stack> #include <array> #include <random> #include <chrono> #include <climits> #include <bitset> using namespace std ; #define rep(i, a, b) for (int i=a; i<(b); i++) #define forn(i, a) for (int i=0; i<(a); i++) #define repd(i,a,b) for (int i = (b)-1; i >= a; i--) #define ford(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a,x) for (auto& a : x) #define int long long #define ii pair<int,int> #define ar array #define FAST_IO ios_base::sync_with_stdio(false) ;cin.tie(NULL);cout.tie(NULL) #define ms(x,a) memset(x,(int)(a),sizeof(x)) #define sz(x) ((int)(x).size()) #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define pb push_back #define endl '\n' #define ff first #define ss second const int INF = 1e18 ; //const int MOD = 1e9 + 7 ; const int N = 2e5+7 ; int a[N], dp[N] ; vector<int> g[N] ; int n , m ; void solve(){ cin >> n >> m ; forn(i,n){ cin >> a[i] ; dp[i] = INF ; } forn(i,m){ int x , y ; cin >> x >> y ; --x ; --y ; g[x].pb(y) ; } int ans = -INF ; for(int i = 0 ; i < n ; i++){ for(int& x : g[i]){ dp[x] = min(dp[x], min(dp[i], a[i])) ; } if(dp[i] < INF) ans = max(ans, a[i]-dp[i]) ; } cout << ans << endl ; } int32_t main(){ FAST_IO ; int t = 1 ; // cin >> t ; while(t--){ solve() ; } }
#define _USE_MATH_DEFINES #include <iostream> #include <cstring> #include <regex> #include <cmath> #include <algorithm> #include <vector> #include <list> #include <map> #include <unordered_set> #include <queue> #include <unordered_map> #include <set> #include <cassert> #include <climits> #pragma GCC optimize("O3") using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i) #define repd(i, n) for(ll i = (n) - 1ll; i >= 0; --i) #define FOR(i, a, b) for(ll i = a; i < ll(b); ++i) #define FORD(i, a, b) for(ll i = a; i > ll(b); --i) #define foreach(i, itr) for(auto& i : (itr)) #define asort(arr, size) std::sort(arr, arr + size) #define dsort(arr, size) std::sort(arr, arr + size, [](auto a, auto b) { return a > b; }) #define cauto const auto& #define bit(n) (1LL << (n)) //////////////////////////////////// // ここから下に任意のコードを書く // //////////////////////////////////// int main() { ull N, K; cin >> N >> K; rep(i, K) { if (N % 200) { N = N * 1000 + 200; } else { N /= 200; } } cout << N << endl; return 0; }
/** * created: 09/01/2021, 14:06:07 **/ #include <bits/stdc++.h> using namespace std; const int max_n = 600666, inf = 1000111222; namespace max_flow { const int max_v = max_n, inf = 1000111222; struct edge { int to, residual_capacity; edge(int to, int residual_capacity): to(to), residual_capacity(residual_capacity) { } }; vector<edge> edges; vector<int> g[max_v]; void add_edge(int u, int v, int capacity) { g[u].push_back(edges.size()); edges.push_back({v, capacity}); g[v].push_back(edges.size()); edges.push_back({u, 0}); } int h[max_v], num[max_v]; bool bfs(int s, int t) { memset(h, -1, sizeof(h[0]) * (t + 1)); h[s] = 0; queue<int> q; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); if (v == t) { return true; } for (int id : g[v]) { if (edges[id].residual_capacity && h[edges[id].to] == -1) { h[edges[id].to] = h[v] + 1; q.push(edges[id].to); } } } return h[t] != -1; } int dfs(int v, int t, int f) { if (v == t) { return f; } for (int &i = num[v]; i < g[v].size(); ++i) { const int id = g[v][i]; if (edges[id].residual_capacity && h[v] + 1 == h[edges[id].to]) { int res = dfs(edges[id].to, t, min(f, edges[id].residual_capacity)); if (res) { edges[id].residual_capacity -= res; edges[id ^ 1].residual_capacity += res; return res; } } } return 0; } int dinic(int s, int t) { int res = 0; while (bfs(s, t)) { memset(num, 0, sizeof(num[0]) * (t + 1)); while (int f = dfs(s, t, inf)) { res += f; } } return res; } void clr(int t) { edges.clear(); for (int i = 0; i <= t; ++i) { g[i].clear(); } } } int n, a[max_n], b[max_n]; vector<int> v; int compress(int x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } int main() { //freopen("in/1_025.txt", "r", stdin); //freopen("output.txt", "w", stdout); scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d%d", &a[i], &b[i]); v.push_back(a[i]); v.push_back(b[i]); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); int s = n + v.size(), t = n + v.size() + 1; for (int i = 0; i < n; ++i) { a[i] = compress(a[i]); b[i] = compress(b[i]); max_flow::add_edge(s, i, 1); max_flow::add_edge(i, n + a[i], 1); if (i != a[i]) { max_flow::add_edge(i, n + b[i], 1); } } for (int i = 0; i < v.size(); ++i) { max_flow::add_edge(n + i, t, 1); } int ans = max_flow::dinic(s, t); printf("%d\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> pii; typedef pair<string,int> psi; typedef pair<int,string> pis; typedef array<int,2> aii; typedef array<int,3> aiii; typedef array<int,4> aiiii; typedef unsigned long long ull; typedef long long int ll; typedef array<ll,2> all; typedef array<ll,3> alll; #define pb push_back #define ff first #define ss second #define MAX 200005 #define MOD 1000000007 #define INF 1e9+100 vector<int>adj[2*MAX]; int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 }; int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 }; int p[2*MAX],sz[2*MAX][2]; void init() { for(int i=1;i<=400000;i++) p[i]=i,sz[i][0]=1; } int find_parent(int u) { return p[u]=p[u]==u?u:find_parent(p[u]); } void union_sets(int u, int v) { int pu=find_parent(u); int pv=find_parent(v); if(pu!=pv) { if(sz[pu][0]<sz[pv][0]) swap(pu,pv); p[pv]=pu; sz[pu][0]+=sz[pv][0]; sz[pu][1]+=1+sz[pv][1]; } else sz[pu][1]++; } int main() { // freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); cin.tie(0),cout.tie(0); ios_base::sync_with_stdio(0); cout.setf(ios::fixed); cout.precision(10); int TC=1; int n,m,q; //cin>>TC; for(int t1=1; t1<=TC; t1++) { cin>>n; init(); for(int i=0; i<n; i++) { int a,b; cin>>a>>b; union_sets(a,b); } bool processed[2*MAX]={}; int ans=0; for(int i=1;i<=400000;i++) { int p=find_parent(i); if(!processed[p]) { processed[p]=1; ans+=min(sz[p][0],sz[p][1]); } } cout<<ans<<endl; } return 0; }
#include <bits/stdc++.h> #define int long long #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); #define pb push_back #define mod 1000000007 //#define lld long double #define mii map<int, int> #define mci map<char, int> #define msi map<string, int> #define pii pair<int, int> #define ff first #define ss second #define bs(yup,x) binary_search(yup.begin(),yup.end(),x) //it will return bollean value #define lb(yup,x) lower_bound(yup.begin(),yup.end(),x) //it will return the adress of the number which is equal to or just greater than x ,and if it is equal to yup.end() than their in no such number exist #define ub(yup,x) upper_bound(yup.begin(),yup.end(),x) #define all(x) (x).begin(), (x).end() #define rep(i,x,y) for(int i=x; i<y; i++) #define fill(a,b) memset(a, b, sizeof(a)) #define vi vector<int> #define setbits(x) __builtin_popcountll(x) using namespace std; const long long N=100005, INF=2000000000000000000; int power(int a, int b, int p) { if(a==0) return 0; int res=1; a%=p; while(b>0) { if(b&1) res=(res*a)%p; b>>=1; a=(a*a)%p; } return res; } vi prime; bool isprime[N]; void pre() { for(int i=2;i<N;i++) { if(isprime[i]) { for(int j=i*i;j<N;j+=i) isprime[j]=false; prime.pb(i); } } return; } int32_t main() { int a,b,c; cin>>a>>b>>c; cout<<21-(a+b+c)<<"\n"; }
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c; cin >> a >> b >> c; int v[] = {0,6,5,4,3,2,1}; cout << v[a] + v[b] + v[c] << "\n"; return 0; }
#include "bits/stdc++.h" using namespace std; int N; vector<int>to[200005]; vector<set<int>>st(100); vector<vector<bool>> pass(100, vector<bool>(100)); void dfs(int v, int i, int p = -1) { for (int e : to[v]) { if (p == e) { continue; } if (pass[i][e]) { continue; } pass[i][e] = true; st[i].insert(e); dfs(e, i, v); } } int main() { int N; cin >> N; vector<string>S(N); for (int i = 0; i < N; ++i) { cin >> S[i]; } for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { if ('1' == S[i][j]) { to[i].push_back(j); } } } for (int i = 0; i < N; ++i) { pass[i][i] = true; dfs(i, i); } /* for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cout << pass[i][j]; } cout << endl; }*/ double ans = 0; for (int i = 0; i < N; ++i) { int count = 0; for (int j = 0; j < N; ++j) { if (pass[j][i]) { count++; } } ans += (double)1 / count; } printf("%.16f\n", ans); return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++) #define mk make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define IT iterator #define V vector #define TP template <class o> #define TPP template <typename t1, typename t2> #define SZ(a) ((int)a.size()) #define all(a) a.begin(), a.end() #define rep(i, a, b) for (int i = a; i <= b; i++) #define REP(i, a, b) for (int i = b; i >= a; i--) #define FOR(i,n) rep(i,1,n) #define debug(x) cerr << #x << " = " << x << endl using namespace std; typedef unsigned ui; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; const int N = 110, size = 1 << 20, mod = 998244353, inf = 2e9; const ll INF = 1e15; // char buf[size],*p1=buf,*p2=buf; TP void qr(o& x) { char c = gc; x = 0; int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = gc; } while (isdigit(c)) x = x * 10 + c - '0', c = gc; x *= f; } TP void qw(o x) { if (x / 10) qw(x / 10); putchar(x % 10 + '0'); } TP void pr1(o x) { if (x < 0) x = -x, putchar('-'); qw(x); putchar(' '); } TP void pr2(o x) { if (x < 0) x = -x, putchar('-'); qw(x); putchar('\n'); } // math ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll power(ll a, ll b = mod - 2, ll p = mod) { ll c = 1; while (b) { if (b & 1) c = c * a % p; b /= 2; a = a * a % p; } return c; } TP void cmax(o& x, o y) { if (x < y) x = y; } TP void cmin(o& x, o y) { if (x > y) x = y; } TPP void ad(t1& x, t2 y) { x += y; if (x >= mod) x -= mod; } TPP void dl(t1& x, t2 y) { x -= y; if (x < 0) x += mod; } template<typename T> struct BIT { T* c; int n; // require you to define 0 as the initial value !! BIT(int _n):n(_n){c=new T[n];c--; FOR(i,n) c[i]=T(0); } void add(int x,T y) { for( ;x<=n;x += x&-x) c[x]=c[x]+y; } T sum(int x) {T y=T(0); for( ;x;x &= x-1) y=y+c[x]; return y;} }; int n, a[N], vis[N], lst[N], now; char s[N][N]; void dfs(int x) { if(lst[x] == now) return; lst[x]=now; vis[x]++; FOR(y,n) if(s[x][y] == '1') dfs(y); } void solve() { qr(n); FOR(i,n) scanf("%s",s[i]+1); for(now=1;now<=n;now++) dfs(now); db ans=0; FOR(i,n) ans += 1.0/vis[i]; printf("%.10lf\n",ans); } int main() { int T = 1; // qr(T); while (T--) solve(); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using ll = long long; using P = pair<int, int>; const int MOD = 998244353; int solve(vector<ll> abc) { ll ans = 1; rep(i, 3) { ll tmp = abc[i] * (abc[i] + 1) / 2 % MOD; ans = ans * tmp % MOD; } return ans; } int main() { vector<ll> abc(3); rep(i, 3) cin >> abc[i]; int ans = solve(abc); cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a[4]; cin>>a[0]>>a[1]>>a[2]>>a[3]; sort(a, a+4); cout<<a[0]; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define sz(a) (int)(a.size()) #define all(a) a.begin(),a.end() #define lb lower_bound #define ub upper_bound #define owo ios_base::sync_with_stdio(0);cin.tie(0); #define MOD (ll)(1e8) #define INF (ll)(1e18) #define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr) #define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\ debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC)) typedef long long int ll; typedef long double ld; typedef pair<ll,ll> PII; typedef pair<int,int> pii; typedef vector<vector<int>> vii; typedef vector<vector<ll>> VII; ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);} int main() { owo int n; cin>>n; vector<ll>a(2*n); for(int i=0;i<2*n;i++)cin>>a[i]; vector<int>id(2*n); string ans(2*n,'?'); for(int i=0;i<2*n;i++)id[i] = i; sort(all(id),[&](int x,int y){return a[x] > a[y];}); vector<bool>beeg(2*n); for(int i=0;i<n;i++)beeg[id[i]] = 1; stack<int>stk; for(int i=0;i<2*n;i++){ if(stk.empty() || beeg[stk.top()] == beeg[i])stk.push(i); else{ ans[stk.top()] = '('; ans[i] = ')'; stk.pop(); } } cout<<ans<<'\n'; }
#include <bits/stdc++.h> #define ll long long using namespace std; int _ = 1; ll dp[200001][2]; map<ll, ll>mn; map<ll, ll>mx; vector<int>colors; ll solve(int i , int left){ ll &ret = dp[i][left]; if(~ret)return ret; ret = 1e16; ll inter = abs(mx[colors[i]] - mn[colors[i]]); if(i == colors.size()-1){ if(left)ret = inter + abs(mx[colors[i]]); else ret = inter + abs(mn[colors[i]]); }else{ if(left){ ret = min(ret, inter + abs(mx[colors[i]] - mx[colors[i+1]]) + solve(i+1, 0)); ret = min(ret, inter + abs(mx[colors[i]] - mn[colors[i+1]]) + solve(i+1, 1)); }else{ ret = min(ret, inter + abs(mn[colors[i]] - mx[colors[i+1]]) + solve(i+1, 0)); ret = min(ret, inter + abs(mn[colors[i]] - mn[colors[i+1]]) + solve(i+1, 1)); } } return ret; } void _solve() { memset(dp,-1,sizeof(dp)); int n; cin>>n; for(int i = 0 ; i < n ; i++) { ll x,c; cin>>x>>c; if(mn.find(c) != mn.end()){ mn[c] = min(mn[c] , x); mx[c] = max(mx[c] , x); }else{ mn[c] = x; mx[c] = x; } } colors.clear(); for(auto x: mx){ colors.push_back(x.first); } cout<< min(solve(0,0) + abs(mx[colors[0]]) , solve(0,1) + abs(mn[colors[0]]))<<'\n'; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); while (_--)_solve(); return 0; }
#include<iostream> #include<vector> #include<string> #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i) #define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;} template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;} int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W, N, M; cin >> H >> W >> N >> M; vector<vector<char>> grid1(H, vector<char>(W, '.')); vector<vector<char>> grid2(H, vector<char>(W, '.')); int a, b, c, d; rep(i, 0, N) { cin >> a >> b; --a, --b; grid1[a][b] = grid2[a][b] = '?'; } rep(i, 0, M) { cin >> c >> d; --c, --d; grid1[c][d] = grid2[c][d] = '#'; } rep(i, 0, H) { rep(j, 1, W) { if (grid1[i][j] != '.') { continue; } if (grid1[i][j - 1] == '?' || grid1[i][j - 1] == '+') { grid1[i][j] = '+'; } } rrep(j, W - 1, 0) { if (grid1[i][j] != '.') { continue; } if (grid1[i][j + 1] == '?' || grid1[i][j + 1] == '+') { grid1[i][j] = '+'; } } } rep(i, 0, W) { rep(j, 1, H) { if (grid2[j][i] != '.') { continue; } if (grid2[j - 1][i] == '?' || grid2[j - 1][i] == '-') { grid2[j][i] = '-'; } } rrep(j, H - 1, 0) { if (grid2[j][i] != '.') { continue; } if (grid2[j + 1][i] == '?' || grid2[j + 1][i] == '-') { grid2[j][i] = '-'; } } } int ans = 0; rep(i, 0, H) rep(j, 0, W) { if (grid1[i][j] == '?' || grid1[i][j] == '+' || grid2[i][j] == '-') { ++ans; } } cout << ans << endl; return 0; }
#include <iostream> #include <vector> using namespace std; int mapn[1500][1500]; int a, b, c, d; int h, w, n, m; int ans; int main() { cin >> h >> w >> n >> m; for (int i = 0; i < n; ++i) { cin >> a >> b; mapn[a - 1][b - 1] = 1; } for (int i = 0; i < m; ++i) { cin >> c >> d; mapn[c - 1][d - 1] = -1; } for (int i = 0; i < h; ++i) { bool l = 0; for (int j = 0; j < w; ++j) { if (mapn[i][j] == 1) { l = true; } else if (mapn[i][j] == -1) { l = false; } else if (l) { mapn[i][j] = 2; } } l = 0; for (int j = w - 1; j >= 0; --j) { if (mapn[i][j] == 1) { l = true; } else if (mapn[i][j] == -1) { l = false; } else if (l) { mapn[i][j] = 2; } } } for (int j = 0; j < w; ++j) { bool l = false; for (int i = 0; i < h; ++i) { if (mapn[i][j] == 1) { l = true; } else if (mapn[i][j] == -1) { l = false; } else if (l) { mapn[i][j] = 2; } } l = false; for (int i = h - 1; i >= 0; --i) { if (mapn[i][j] == 1) { l = true; } else if (mapn[i][j] == -1) { l = false; } else if (l) { mapn[i][j] = 2; } } } for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { ans += mapn[i][j] > 0; } } cout << ans; }
#include<bits/stdc++.h> #include<cmath> #define SZ(x) ((int)x.size()) #include<vector> #include<set> #include<map> #define ll long long #define lli long long int #define REP(i,a,b) for(ll i=a;i<b;i++) #define pb push_back #include<string> #include<cctype> #define F first #define S second #include<queue> #include <sstream> #define endl '\n' #define lli long long int #define pairs pair<int,int> #define ld long double #define mod 1000000007 const double PI = 3.141592653589793238460; typedef std::complex<double> Complex; typedef std::valarray<Complex> CArray; using namespace std; //first try to write all condition on copy then go for implement ll pows(ll a,ll n,ll m) { a=a%m; ll res=1; while(n) { if(n%2!=0) { res=(res*a)%m; n--; } else { a=(a*a)%m; n=n/2; } } return res%m; } ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b); } bool isprime(ll n) { if(n==1||n==0) { return false; } for(ll i=2;i*i<=n;i++) { if(n%i==0) { return false; } } return true; } bool istrue(string s) { int i=0; int j=s.size()-1; while(i<j) { if(s[i]==s[j]) { i++; j--; } else { return false; } } return true; } const int N=100005; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin>>n; string s; cin>>s; int flip=0; string s1; string s2; for(int i=0;i<n;i++) { s1.pb(s[i]); } for(int i=n;i<2*n;i++) { s2.pb(s[i]); } int q; cin>>q; while(q--) { ll t,a,b; cin>>t>>a>>b; if(t==1) { if(flip==0) { if(a>n) { a=a-n; b=b-n; a--; b--; swap(s2[a],s2[b]); } else if(a<=n&&b>n) { a--; b=b-n; b--; swap(s1[a],s2[b]); } else{ a--; b--; swap(s1[a],s1[b]); } } else { if(a>n) { a=a-n; b=b-n; a--; b--; swap(s1[a],s1[b]); } else if(a<=n&&b>n) { a--; b=b-n; b--; swap(s2[a],s1[b]); } else{ a--; b--; swap(s2[a],s2[b]); } } } else { flip=flip^1; } } if(flip==0) { cout<<s1<<s2<<endl; } else { cout<<s2<<s1<<endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef pair<ll,ll> pll; #define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define loop(i,a,b) for(ll i=a;i<b;i++) #define test() ll t;cin>>t;for(ll test = 1;test<=t;test++) #define f first #define s second #define pb push_back #define nl cout<<"\n" #define MOD(x) ((x%P)+P)%P #define vec(v) loop(i,0,v.size()) cout<<v[i]<<" "; nl const int N = 1000005; const ll P = 1000000007; const ll INF = 1e17; const double eps = 1e-6; int main() { fastio(); int n; cin>>n; vector<ll> a(n), t(n); for(int i=0; i<n; i++) cin>>a[i]>>t[i]; stack<pair<ll,ll>> st; ll sum = 0; for(int i=n-1; i>=0; i--) { if(t[i]==1) sum += a[i]; else if(t[i]==2) st.push({2, a[i]+sum}); else st.push({3, a[i]+sum}); } ll mn = -INF, mx = INF; while(st.size()) { pair<ll,ll> now = st.top(); st.pop(); if(now.f == 2) { mn = max(mn, now.s); if(mn > mx) mx = mn; } else { mx = min(mx, now.s); if(mx < mn) mn = mx; } } int q; cin>>q; for(int i=0; i<q; i++) { ll temp; cin>>temp; temp += sum; temp = max(temp, mn); temp = min(temp, mx); cout<<temp,nl; } cerr << "\nTime elapsed : " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n"; return 0; }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for(int i = 0; i < (int)(n); ++i) typedef long long ll; int n; int g[21]; bool isOK(int pat) { REP(i, n) { if(pat & (1 << i)) { if((g[i] & pat) != pat) { return false; } } } return true; } int memo[1 << 18]; int solve(int rest) { int &res = memo[rest]; if(res < 0) { if(rest == 0) { res = 0; } else if(((rest-1) & rest) == 0) { res = 1; } else { int first = rest & -rest; int rest2 = rest ^ first; res = 1 + solve(rest2); for(int s = rest2; s; s = (s-1) & rest2) { if(isOK(s | first)) { res = min(res, 1 + solve(rest ^ first ^ s)); } } } } return res; } int main(void) { int nEdge; scanf("%d%d", &n, &nEdge); REP(i, n) { g[i] |= 1 << i; } REP(i, nEdge) { int a, b; scanf("%d%d", &a, &b); --a; --b; g[a] |= 1 << b; g[b] |= 1 << a; } if(nEdge == n*(n-1) / 2) { cout << 1 << endl; return 0; } memset(memo, -1, sizeof memo); int res = solve((1 << n)-1); cout << res << endl; return 0; }
#include <bits/stdc++.h> #include <vector> #include<math.h> #include<string.h> using namespace std; #define MAX 6000005 #define MOD 1000000007 #define SMOD 998244353 #define GMAX 19 #define INF 2000000000000000000 #define EPS 0.0000000001 #define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> int n; int Graph[GMAX]; bool possibility[MAX]; int components[MAX]; int mcom(int mask) { if(components[mask]!=-1) { return components[mask]; } components[mask]=n; int i; for(i=mask;i>0;i=mask&(i-1)) { if(possibility[i]) components[mask]=min(components[mask],1+mcom(mask^i)); } return components[mask]; } int main() { int i,m,j; scanf("%d %d",&n,&m); for(i=0;i<n;i++) { int mask=1<<i; Graph[i]=Graph[i]|mask; } for(i=0;i<m;i++) { int u,v; scanf("%d %d",&u,&v); u--; v--; int U=1<<u; int V=1<<v; Graph[u]=Graph[u]|V; Graph[v]=Graph[v]|U; } m=1<<n; for(i=0;i<m;i++) { int mask=i; for(j=0;j<n;j++) { int tmask=1<<j; tmask=tmask&mask; if(tmask==0) continue; tmask=mask; tmask=tmask&Graph[j]; if(tmask!=mask) break; } possibility[i]=(j>=n); components[i]=-1; } components[0]=0; j=mcom(m-1); printf("%d",j); return 0; }
#include <bits/stdc++.h> using ll = long long; using namespace std; #define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++) #define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define rreps(i, n) for (int i = ((int)(n)); i > 0; i--) #define repi(i, x) \ for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++) #define all(x) (x).begin(), (x).end() #define F first #define S second #define mp make_pair #define mt make_tuple #define pb push_back #define eb emplace_back typedef vector<int> Vi; typedef vector<Vi> VVi; typedef pair<int, int> Pi; typedef vector<Pi> VPi; typedef vector<long long> V; typedef vector<V> VV; typedef pair<long long, long long> P; typedef vector<P> VP; 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; } 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) { rep(i, v.size()) { if (i) os << " "; os << v[i]; } return os; } template <class T, class U> istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <class T> istream& operator>>(istream& is, vector<T>& v) { rep(i, v.size()) { is >> v[i]; } return is; } const long long INFLL = 1LL << 60; const int INF = 1 << 30; const double PI = acos(-1); #ifdef LOCAL #define dbg(x) cerr << #x << ": " << (x) << '\n' #define say(x) cerr << (x) << '\n' #else #define dbg(x) #define say(x) #endif //ダイクストラ法:スタートから任意の地点での最短距離、最短パスを求める //計算量:(頂点+辺)log(頂点) //始点,終点,隣接リスト(pathlist[edge.A]=(edgeB,legth))-->距離(notfound:INFLL),previous(notfound:INF); vector<pair<long long, int>> Dikstra( int start, vector<vector<pair<int, long long>>>& pathlist) { int edge = pathlist.size(); pair<long long, int> cerching; priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> q; // start to distance,[ditance ,islandNo.] vector<pair<long long, int>> D( edge, mp(INFLL, INF)); // distance (ans), preious path vector<bool> had(edge, false); D[start] = mp(0, -1); // distance ,Previous egdg q.emplace(0LL, start); while (!q.empty()) { cerching = q.top(); q.pop(); if (had[cerching.second] == false) { had[cerching.second] = true; for (auto i = pathlist[cerching.second].begin(); i != pathlist[cerching.second].end(); i++) { if (had[i->first] == false) { if (chmin(D[i->first].F, (cerching.first) + (i->second))) { D[i->first].S = cerching.second; } q.emplace(D[i->first].F, i->first); } } } } return D; } string solve(bool a) { return ((a) ? "Yes" : "No"); } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<vector<pair<int, long long>>> path(n); VV dis(n, V(n, INFLL)); int a, b, c; rep(i, m) { cin >> a >> b >> c; path[a - 1].emplace_back(b - 1, c); chmin(dis[a - 1][b - 1], (ll)c); } vector<pair<long long, int>> res; rep(i, n) { res = Dikstra(i, path); rep(j, n) { if (res[j].F == 0) continue; chmin(dis[i][j], res[j].F); } } rep(i, n) { ll ans = INFLL; chmin(ans, dis[i][i]); rep(j, n) { chmin(ans, dis[i][j] + dis[j][i]); } if (ans == INFLL) { cout << -1 << endl; } else { cout << ans << endl; } } }
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; typedef unsigned long long int ull; typedef long int li; #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define test ll t; cin >> t; while(t--) const long long int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0}; const long long int cons = 100005, ncr_cons = 100005; const long long int MOD = 1000000007; // const long long int MOD = 998244353; const long long int const_INT_MAX = 1000000000000000000, const_INT_MIN = -1 * 1000000000000000000; using namespace std; bool sortinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.first > b.first);} bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second < b.second);} bool sortbysecinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second > b.second);} ll gcd(ll x, ll y){return (ll)(__gcd(x, y));} ll lcm(ll x, ll y){return (ll)((x * y) / gcd(x, y));} ll mod_expo(ll x, ll y, ll p){ if((x + p) % p == 0) return 0; ll res = 1; x = (x + p) % p; while (y > 0){ if(y & 1) res = (res*x + p) % p; y = y>>1; x = (x*x + p) % p; } return res; } void usaco(string str = ""){ fastio; if(str.size()){ freopen((str + ".in").c_str(), "r", stdin); freopen((str + ".out").c_str(), "w", stdout); } } vector<pair<ll, ll>> adj[cons]; vector<ll> par, dist; void dijkstra(ll n, ll source){ par.assign(n + 1, -1); dist.assign(n + 1, const_INT_MAX); priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> p; p.push(make_pair(0, source)); dist[source] = 0; while(p.size() > 0){ pair<ll, ll> v = p.top(); p.pop(); if(dist[v.second] < v.first) continue; // already initialised else { for(auto u : adj[v.second]){ if(dist[v.second] + u.second < dist[u.first]){ dist[u.first] = dist[v.second] + u.second; par[u.first] = v.second; p.push(make_pair(dist[u.first], u.first)); } } } } } int main(){ usaco(); ll n, m; cin >> n >> m; vector<ll> fin_ans(n, const_INT_MAX); for(ll i=0;i<m;i++){ ll a, b, c; cin >> a >> b >> c; a--; b--; if(!(a - b)) fin_ans[a] = min(fin_ans[a], c); else { adj[a].push_back(make_pair(b, c)); } } for(ll i=0;i<n;i++){ for(auto u : adj[i]){ dijkstra(n, (ll)(u.first)); fin_ans[i] = min(fin_ans[i], u.second + dist[i]); } } for(ll i=0;i<n;i++){ if(!(fin_ans[i] - const_INT_MAX)) cout << -1 << "\n"; else cout << fin_ans[i] << "\n"; } cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i, n) for (int i = 0; i < (n); ++i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) using namespace std; // using namespace atcoder; typedef long long int ll; typedef pair<int, int> P; #define MAX_N 200005 const ll PERFECT = 100000000; static uint32_t randxor() { static uint32_t x = 123456789; static uint32_t y = 362436069; static uint32_t z = 521288629; static uint32_t w = 88675123; uint32_t t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } // 0以上1未満の小数をとる乱数 static double rand01() { return (randxor() + 0.5) * (1.0 / UINT_MAX); } int n, m; string s[1000]; vector<int> a[1000]; int b[1000] = {}; int ans[20][20]; ll ma; ll calc() { int cnt = 0; rep(k, m) { int ok = 0; rep(i, n) { rep(j, n) { ok = 1; rep(l, b[k]) { if (ans[i][(j + l) % n] != a[k][l]) { ok = 0; break; } } if (ok) break; ok = 1; rep(l, b[k]) { if (ans[(i + l) % n][j] != a[k][l]) { ok = 0; break; } } if (ok) break; } if (ok) break; } cnt += ok; } ll res = 0; if (cnt < m) { res = PERFECT * cnt / m; } else { int cnt2 = 0; rep(i, n) { rep(j, n) { if (ans[i][j] == 0) cnt2++; } } res = PERFECT * 2 * n * n / (2 * n * n - cnt2); } return res; } int main() { srand((unsigned)time(NULL)); while (rand() % 100) randxor(); string fileNameIfs = "in\\0000.txt"; ifstream ifs(fileNameIfs.c_str()); if (!ifs.is_open()) { // 標準入力する cin >> n >> m; rep(i, m) cin >> s[i]; rep(i, m) { b[i] = s[i].size(); rep(j, b[i]) { a[i].push_back(s[i][j] - 'A' + 1); } } } else { // ファイル入力する ifs >> n >> m; rep(i, m) ifs >> s[i]; rep(i, m) { b[i] = s[i].size(); rep(j, b[i]) { a[i].push_back(s[i][j] - 'A' + 1); } } } rep(i, 20) { rep(j, 20) { ans[i][j] = randxor() % 8 + 1; } } clock_t start, end; start = clock(); ma = calc(); int loop = 0; while (true) { loop++; int x = randxor() % n; int y = randxor() % n; int val = randxor() % 9; int keep = ans[x][y]; ans[x][y] = val; ll tmp = calc(); if (tmp >= ma) { ma = tmp; } else { ans[x][y] = keep; } end = clock(); if ((double)(end - start) / CLOCKS_PER_SEC > 2.9) break; } // cout << loop << endl; // cout << calc() << endl; rep(i, n) { rep(j, n) { if (ans[i][j] == 0) { cout << '.'; } else { cout << (char)(ans[i][j] + 'A' - 1); } } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n, m, ops[2000][2000][4]; string s[2000]; int modpow(int a, int b){ if(!b) return 1; long long res = modpow(a, b / 2); res = (res * res) % mod; if(b % 2) res = (res * a) % mod; return res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; long long open = 0; for(int i = 0; i < n; i++){ cin >> s[i]; for(char j : s[i]) if(j == '.') open++; } //left for(int i = 0; i < n; i++){ int cur = 0; for(int j = 0; j < m; j++) if(s[i][j] == '#') cur = 0; else ops[i][j][0] = cur++; } //right for(int i = 0; i < n; i++){ int cur = 0; for(int j = m - 1; j >= 0; j--) if(s[i][j] == '#') cur = 0; else ops[i][j][2] = cur++; } //up for(int i = 0; i < m; i++){ int cur = 0; for(int j = 0; j < n; j++) if(s[j][i] == '#') cur = 0; else ops[j][i][1] = cur++; } //down for(int i = 0; i < m; i++){ int cur = 0; for(int j = n - 1; j >= 0; j--) if(s[j][i] == '#') cur = 0; else ops[j][i][3] = cur++; } long long ans = (open * modpow(2, open)) % mod; for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){ if(s[i][j] == '#') continue; int rem = modpow(2, open - ops[i][j][0] - ops[i][j][1] - ops[i][j][2] - ops[i][j][3] - 1); ans = ((ans - rem) % mod + mod) % mod; } cout << ans << '\n'; return 0; } /* find what scenarios where none activated */
// give up #include <bitset> #include <cassert> #include <iostream> #include <iomanip> #include <list> #include <numeric> #include <queue> #include <unordered_map> #include <unordered_set> #include <valarray> // alias, using declaration using llint = long long; using ldouble = long double; template <class T = llint> using vector = std::vector<T>; template <class T = llint> using deque = std::deque<T>; template <class T = llint> using list = std::list<T>; template <class T = llint, class U = llint> using umap = std::unordered_map<T, U>; template <class T = llint, class U = llint> using ummap = std::unordered_multimap<T, U>; template <class T = llint> using uset = std::unordered_set<T>; template <class T = llint> using umset = std::unordered_multiset<T>; template <class T = llint, class U = T> using pair = std::pair<T, U>; template <class T = llint> using varray = std::valarray<T>; template <class T = llint, class U = vector<T>, class V = std::less<class U::value_type>> using pqueue = std::priority_queue<T, U, V>; using std::array; using std::bitset; using std::string; using std::tuple; // constant constexpr llint INF = 0x7FFFFFFFFFFFFFFF; constexpr llint INVALID = 0x8000000000000000; namespace io { // in template <class... T, size_t... S> auto in(std::index_sequence<S...>) {tuple<T...> t; (..., (std::cin >> std::get<S>(t))); return t;} template <class... T, size_t... S> auto in(std::index_sequence<S...>, size_t n) {tuple<vector<T>...> t{vector<T>(n)...}; for (size_t i = 0; i < n; i++) (..., (std::cin >> std::get<S>(t)[i])); return t;} template <size_t N, class T = llint, class... U> auto in() {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(); else return in<T, U...>(std::index_sequence_for<T, U...>());} template <size_t N, class T = llint, class... U> auto in(size_t n) {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(n); else return in<T, U...>(std::index_sequence_for<T, U...>(), n);} template <size_t N, class T = llint> auto in(size_t n, size_t m) {static_assert(N == 1); vector<vector<T>> v(n, vector<T>(m)); for (auto&& vi : v) for (auto&& vij : vi) std::cin >> vij; return std::make_tuple(v);} // out template <class T, class... U> void out(const T& a, const U&... b) {std::cout << a << (sizeof...(b) ? " " : "\n"); if constexpr (sizeof...(b)) out(b...);} template <class T, class U = const char*> void out(const vector<T>& a, U d = " ") {for (auto&& b : a) std::cout << b << (&b != &a.back() ? d : "\n");} } int main() { // input auto [N, M, Q] = io::in<3>(); auto [T, X, Y] = io::in<3>(Q); // solve vector<> a(N + 1), b(M + 1); umap<> Z; { Z.emplace(0, 0); vector<> Y_ = Y; std::sort(Y_.begin(), Y_.end()); for (llint i = 0; i < Q; i++) Z.emplace(Y_.at(i), i + 1); } constexpr llint ST_SIZE = 1 << 19; vector<pair<>> st_a(ST_SIZE), st_b(ST_SIZE); auto update = [&Z](vector<pair<>>& st, llint v, llint n) { for (llint i = (ST_SIZE >> 1) + Z.at(v); i > 0; i >>= 1) { st.at(i).first += n; st.at(i).second += v * n; } }; auto culc_line = [&Z](const vector<pair<>>& st, llint v) { llint r = st.at((ST_SIZE >> 1) + Z.at(v)).second; for (llint i = (ST_SIZE >> 1) + Z.at(v); i > 1; i >>= 1) { r += i & 1 ? st.at(i ^ 1).first * v : st.at(i ^ 1).second; } return r; }; update(st_a, 0, N); update(st_b, 0, M); vector<> result(Q); for (llint i = 0; i < Q; i++) { auto& t = (T.at(i) == 1 ? a : b).at(X.at(i)); auto& st_x = T.at(i) == 1 ? st_a : st_b; auto& st_y = T.at(i) == 1 ? st_b : st_a; update(st_x, t, -1); result.at(i) = -culc_line(st_y, t); t = Y.at(i); update(st_x, t, 1); result.at(i) += culc_line(st_y, t); } for (llint i = 1; i < Q; i++) result.at(i) += result.at(i - 1); // output io::out(result, "\n"); }
#include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <array> #include <algorithm> #include <utility> #include <map> #include <queue> #include <set> #include <cmath> #include <cstdio> #include <cstring> #define ll long long #define ld long double #define eps 1e-8 #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f3f // change if necessary #define MAXN 200010 using namespace std; struct segtree { struct Node { Node *l, *r; int cnt; ll sum; Node() { l = r = nullptr; cnt = sum = 0; } Node operator+(const Node &b) const { Node res; res.cnt = this->cnt + b.cnt; res.sum = this->sum + b.sum; return res; } }; deque<Node> dq; segtree() { dq.push_back(Node()); } Node *new_node() { dq.push_back(Node()); return &dq.back(); } Node get(Node *p) { return p ? *p : Node(); } void upd(int x, int y) { upd(&dq[0], 0, 1e8, x, y); } void upd(Node *p, int l, int r, int x, int y) { if (l == r) { p->cnt += y; p->sum += x * y; } else { int m = (l + r) / 2; if (x <= m) { if (!p->l) p->l = new_node(); upd(p->l, l, m, x, y); } else { if (!p->r) p->r = new_node(); upd(p->r, m + 1, r, x, y); } Node res = get(p->l) + get(p->r); p->cnt = res.cnt; p->sum = res.sum; } } Node qry(int x, int y) { return qry(&dq[0], 0, 1e8, x, y); } Node qry(Node *p, int l, int r, int x, int y) { if (p == nullptr || r < x || l > y) return Node(); if (x <= l && r <= y) { return *p; } int m = (l + r) / 2; return qry(p->l, l, m, x, y) + qry(p->r, m + 1, r, x, y); } }; segtree a, b; ll cur_a[MAXN]; ll cur_b[MAXN]; int main() { cin.tie(0)->sync_with_stdio(0); int n, m, q; cin >> n >> m >> q; a.upd(0, n); b.upd(0, m); ll ans = 0; while (q--) { int t, x, y; cin >> t >> x >> y; x--; if (t == 1) { auto res = b.qry(cur_a[x], 1e8); ans -= res.sum; ans -= (m - res.cnt) * cur_a[x]; a.upd(cur_a[x], -1); cur_a[x] = y; a.upd(cur_a[x], 1); res = b.qry(cur_a[x], 1e8); ans += res.sum; ans += (m - res.cnt) * cur_a[x]; } else { auto res = a.qry(cur_b[x], 1e8); ans -= res.sum; ans -= (n - res.cnt) * cur_b[x]; b.upd(cur_b[x], -1); cur_b[x] = y; b.upd(cur_b[x], 1); res = a.qry(cur_b[x], 1e8); ans += res.sum; ans += (n - res.cnt) * cur_b[x]; } cout << ans << '\n'; } return 0; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define s(x) x.size() #define endl '\n' #define r(n) ll n; cin>>n; #define vl vector<ll> #define all(x) (x).begin(),(x).end() #define fk(i,start,end) for(ll i=start;i<ll(end);i++) typedef long long int ll; const ll MOD = 1e9 + 7; string A="YES"; string B="NO"; void solution() { std::vector<ll> v(3); fk(i,0,3) { cin>>v[i]; } sort(all(v)); cout<<v[1]+v[2]; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input1.txt", "r", stdin); freopen("output1.txt", "w", stdout); #endif solution(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin>>N; int sam =0; int i=1; while(sam < N){ sam += i; i++; } cout<< i-1 <<endl; }
#pragma region header #include <bits/stdc++.h> using namespace std; #define rep(i,n)for(int i=0;i<(n);i++) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define pb push_back #define int ll #define each(i, a) for (auto &&i : (a)) using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; using vvs = vector<vs>; using vd = vector<ld>; using vvd = vector<vd>; using vb = vector<bool>; using vvb = vector<vb>; using P = pair<int, int>; using vp = vector<P>; using int128 = __int128_t;//cin coutはできない template <class T> using greater_queue = priority_queue<T, vector<T>, greater<T>>; int gcd(int a,int b){return b?gcd(b,a%b):a;} int lcm(int a,int b){return a / gcd(a, b) * b;}; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; template <class T> void CVEC(const T &v) { int c = v.size() - 1; for (int i = 0; i < c; i++) cout << v[i] << ' '; if (c > -1) cout << v[c]; cout << '\n'; } #pragma endregion header const int INF = 110110110; signed main(){ int n; cin >> n; vector<int> t(n); rep(i,n) cin >> t[i]; int sum = accumulate(ALL(t), 0); vector<vector<int>> dp(110, vector<int>(100010, INF)); dp[0][0] = 0; for(int i = 0; i < n; i++){ for(int j = 0; j < 100010; j++){ if(dp[i][j] != INF){ dp[i + 1][j + t[i]] = min(dp[i + 1][j + t[i]], dp[i][j]); dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + t[i]); } } } int ans = INF; for(int j = 0; j < 100010; j++) ans = min(ans, max(j, dp[n][j])); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 105,M = 100005; int f[N][M];//f[i][j] = 从1~i种中选,且总和正好为j的方案是否存在 int n; int times[N];//烹饪时间 int main() { ios::sync_with_stdio(0); cin>>n; int tot_time = 0;//总时间 for(int i=1;i<=n;i++) cin>>times[i],tot_time+=times[i]; for(int i=1;i<=n;i++) { f[i][times[i]] = 1; for(int j=times[i]+1;j<=tot_time;j++) { f[i][j] = max(f[i-1][j],f[i-1][j-times[i]]); } } int ans = 0x3f3f3f3f; for(int i=1;i<=n;i++) { for(int j=times[i];j<=tot_time;j++) { if(f[i][j]) ans = min(ans,max(j,tot_time-j)); } } cout<<ans; return 0; }
#include<stdio.h> int main() { int min,i,a[4]; for(i=0;i<4;i++) { scanf("%d",&a[i]); } min=a[0]; for(i=1;i<4;i++) { if(a[i]<min) min=a[i]; } printf("%d",min); }
#include<bits/stdc++.h> using namespace std; int main() { char a,b,c; scanf("%c %c %c",&a,&b,&c); if((a==b)&&(b==c)&&(a==c)) { if((a>='A'&&a<='Z')&&(b>='A'&&b<='Z')&&(c>='A'&&c<='Z')) { printf("Won\n"); } } else { printf("Lost\n"); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double struct unionfind{ vector<int> parent; vector<int> rank; vector<int> size; unionfind (int N) { parent = vector<int>(N); iota(parent.begin(), parent.end(), 0); rank = vector<int>(N, 0); size = vector<int>(N, 1); } int root(int x) { if (parent[x] == x) return x; parent[x] = root(parent[x]); return parent[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]) { parent[y] = x; size[x] += size[y]; } else { parent[x] = y; size[y] += size[x]; if (rank[x] == rank[y]) rank[y] += 1; } } int getsize(int x) { return size[root(x)]; } }; int main() { int H, W; cin >> H >> W; vector<string> grid(H); for (int i = 0; i < H; i++) cin >> grid[i]; unionfind uf(H+W); uf.unite(0, H); uf.unite(H-1, H); uf.unite(0, H+W-1); uf.unite(H-1, H+W-1); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (grid[i][j] == '#') { uf.unite(i, H+j); } } } int ans = 0; int x = 0, y = 0; for (int i = 0; i < H+W; i++) { if (uf.root(i) == i) ans++; if (uf.getsize(i) == 1) { if (i < H) x++; else y++; } } cout << ans-max(x, y)-1 << endl; return 0; }
#include <bits/stdc++.h> #include <cmath> #include <algorithm> #include <vector> #include <string> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define MOD 1000000007 using P = pair<int, int>; int main() { int n, m; cin >> n >> m; vector<int> a(m); rep(i, m)cin >>a[i]; sort(a.begin(), a.end()); a.push_back(n + 1); int car =1; vector<int>s; rep(i, m + 1) { int w = a[i] - car; if (w != 0) s.push_back(w); car = a[i] + 1; } int k = n; for (int w : s) k = min(k, w); int ans=0; for (int w : s) ans += (w + k - 1) / k; cout << ans << endl; return 0; }
#include <bits/extc++.h> using namespace std; using ll = long long; #define REP(i,n) for(ll i=0;i<ll(n);i++) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define ALL(x) x.begin(),x.end() #define MOD (ll)1000000007 signed main() { ll n; cin >> n; cout << n - 1; return 0; }
#include<iostream> using namespace std; int main() { int N; cin>>N; int w; w=N-1; cout<<w<<endl; return 0; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using ll = int64_t; using ull = uint64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} //head int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> cnt(n+1); rep(i, n) { if(!i) cnt[1] = a[0]; else cnt[i+1] = cnt[i]+a[i]-a[i-1]-1; } rep(i, q) { ll k; cin >> k; auto itr = upper_bound(all(cnt), k); cout << k+(itr-cnt.begin())-1 << '\n'; } }
#include<bits/stdc++.h> #define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i) #define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i) #define foreach(i, n) for(auto &i:(n)) #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) #define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE #define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; //const ll MOD = (ll)1e9+7; const ll MOD = 998244353; const int INF = (ll)1e9+7; const ll INFLL = (ll)1e18; template<class t> using vvector = vector<vector<t>>; template<class t> using vvvector = vector<vector<vector<t>>>; template<class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;} template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;} #ifdef DEBUG #define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl; #else #define debug(x) (void)0 #endif namespace templates{ ll modpow(ll x, ll b,ll mod=MOD){ ll res = 1; while(b){ if(b&1)res = res * x % mod; x = x * x % mod; b>>=1; } return res; } ll modinv(ll x){ return modpow(x, MOD-2); } bool was_output = false; template<class t> void output(t a){ if(was_output)cout << " "; cout << a; was_output = true; } void outendl(){ was_output = false; cout << endl; } template<class t> istream& operator>>(istream&is, vector<t>&x){ for(auto &i:x)is >> i; return is; } template<class t, class u> istream& operator>>(istream&is, pair<t, u>&x){ is >> x.first >> x.second; return is; } template<class t> ostream& operator<<(ostream&os, vector<t> &v){ os << "{"; for(t &i:v){ os << i << ", "; } os << "}"; return os; } template<class t = long long> t in(){ t res; cin >> res; return res; } template<class t> void out(t x){ cout << x; } template<class t> vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){ sort(line.begin(),line.end(),comp); return line; } template<class t> vector<t> reversed(vector<t> line){ reverse(line.begin(),line.end()); return line; } string reversed(string str){ reverse(str.begin(),str.end()); return str; } long long gcd(long long a,long long b){ while(b){ a %= b; swap(a,b); } return a; } long long lcm(long long a,long long b){ return a / gcd(a,b) * b; } } using namespace templates; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); string str = in<string>(); foreach(i,str){ if(i=='.')break; cout <<i; } cout << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int a,b; cin>>a>>b; if(a+b>=15&&b>=8)cout<<1; else if(a+b>=10&&b>=3)cout<<2; else if(a+b>=3)cout<<3; else cout<<4; return 0; }
//by azidoazideazide(C2N14) #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll, ll> pll; #define pb push_back #define fi first #define se second #define FOR(i, a, b) for (ll i = a; i < (b); i++) #define FORE(i, a, b) for (ll i = a; i <= (b); i++) void listv(int st, int ed, vector<int> v); ull llpow(ull x, ull y){ if(y==0) return 1; ull tt=llpow(x,y>>1); return (y&1) ? x*tt*tt : tt*tt; } //ifstream fin("test1.txt"); //ofstream fout("test1.txt"); int main(){ // ios::sync_with_stdio(0), cin.tie(0); int fat, solid, b; cin>>b>>fat; solid=b+fat; if(solid>=15 && fat>=8) puts("1"); else if(solid>=10 && fat >=3) puts("2"); else if(solid>=3) puts("3"); else puts("4"); } void listv(int st, int ed, vector<int> v){ FORE(i, st, ed) cout<<v[i]<<' '; cout<<endl; }
#include <bits/stdc++.h> #define rep(i, l, r) for (int i = l; i < (int)(r); i++) #define all(vec) vec.begin(), vec.end() #define allr(vec) vec.rbegin(), vec.rend() 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; } using ll = long long; using vb = vector<bool>; using vc = vector<char>; using vs = vector<string>; using vi = vector<int>; using vll = vector<ll>; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; int ans = 3; int a = abs(r2 - r1), b = abs(c2 - c1); if (a == 0 && b == 0) { ans = 0; } else if (a == b || a + b <= 3) { ans = 1; } else if ((a + b) % 2 == 0 || a + b <= 6 || abs(a - b) <= 3) { ans = 2; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int a, b, c, d; cin >> a >> b >> c >> d; cout << b - c << endl;; }
#include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; #define int long long int #define vi vector<int> #define ff first #define ss second #define pb push_back #define MOD 1000000007 //const int nax = 1e6+5; #define inf 4e18 #define Graph vector<vi> #define Graphw vector<vector<pair<int,int>>> #define sz(x) (int)x.size() #define pr pair<int,int> #define display(v) for(int i =0;i<(int)v.size();++i)cout << v[i] << " " #define all(v) v.begin(),v.end() #define read(a) for(int i = 0;i<(int)a.size();++i)cin>>a[i] #define show(x) cout << #x << " is " << x << "\n"; // typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_si; // typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ind_msi; #define show_arr(a) cout << #a << "\n"; display(a) ; cout << "\n"; inline int add(int a, int b){ int c = (a%MOD) + (b%MOD); if(c >= MOD) c -= MOD; return c; } inline int mul(int a, int b){ return ((a%MOD) * 1ll * (b%MOD)) % MOD; } inline int sub(int a, int b){ int c = (a%MOD) - (b%MOD); if(c < 0) c += MOD; return c; } const int nax = 300*1000 + 10; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //#define ook order_of_key //#define fbo find_by_order // order_of_key(x) -> number of elements less than x. // * find_by_order(x) -> gives the element at index x (0 - indexing) void test_case(); int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; //cin>>t; while(t--){ test_case(); } return 0; } void test_case(){ int m , h ; cin>>m>>h; if(h%m == 0){ cout << "Yes"; } else{ cout << "No"; } } // Химаншу // check overflow // special case n == 1 // 🍪🍪🍪
#include<bits/stdc++.h> using namespace std; #define ll long long int #define pii pair<int,int> #define pll pair<ll,ll> #define pb push_back #define f first #define s second #define mk make_pair int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t=1; //cin>>t; while(t--) { int m,h; cin>>m>>h; if(h%m==0) cout<<"Yes\n"; else cout<<"No\n"; }//while }//main
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using ld = long double; using P = pair<ll, ll>; using Vec = vector<ll>; using VecP = vector<P>; template <class T> using Vec2 = vector<vector<T>>; #define REP(i, m, n) for(ll i = (m); i < (n); ++i) #define REPN(i, m, n) for(ll i = (m); i <= (n); ++i) #define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i) #define REPNR(i, m, n) for(ll i = (m); i >= (n); --i) #define rep(i, n) REP(i, 0, n) #define repn(i, n) REPN(i, 1, n) #define repr(i, n) REPR(i, n, 0) #define repnr(i, n) REPNR(i, n, 1) #define all(s) (s).begin(), (s).end() template <class T1, class T2> bool chmax(T1 &a, const T2 b) { return a < b ? a = b, true : false; } template <class T1, class T2> bool chmin(T1 &a, const T2 b) { return a > b ? a = b, true : false; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; } void _co() { cout << '\n'; } template <class Head, class... Tail> void _co(Head&& head, Tail&&... tail) { cout << ' ' << head; _co(forward<Tail>(tail)...); } template <class Head, class... Tail> void co(Head&& head, Tail&&... tail) { cout << head; _co(forward<Tail>(tail)...); } void ce() { cerr << '\n'; } template <class Head, class... Tail> void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); } void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); } void setp(const int n) { cout << fixed << setprecision(n); } constexpr int64_t INF = 1000000000000000001; constexpr int64_t MOD = 1000000007; constexpr int64_t MOD_N = 998244353; constexpr long double EPS = 1e-11; const double PI = acos(-1); struct S { bool f; ll a; bool operator<(const S &rhs) { if (a < 0 && rhs.a < 0) { if (f != rhs.f) return !f; else return a < rhs.a; } else if (a >= 0 && rhs.a >= 0) { if (f != rhs.f) return f; else return a < rhs.a; } else { return a < 0; } } S &operator+=(const S &rhs) { if (f) a -= rhs.a; else a += rhs.a; f ^= rhs.f; return *this; } }; int main(void) { ll n; cin >> n; Vec p(n - 1); cin >> p; rep(i, n - 1) p[i]--; Vec2<ll> c(n); rep(i, n - 1) c[p[i]].push_back(i + 1); auto dfs = [&](auto self, ll r) -> S { S res = S{false, 0}; res.a++; vector<S> s; for (auto i : c[r]) { s.push_back(self(self, i)); } sort(all(s)); for (auto i : s) res += i; res.f ^= true; return res; }; auto ans = dfs(dfs, 0); co((n + ans.a) / 2); return 0; }
#include<bits/stdc++.h> using namespace std; #define fl(n) for(long long int i=0;i<n;i++) #define sz(a) int((a).size()) #define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++) #define trr(c,i) for(auto i = (c).rbegin(); i != (c).rend(); i++) #define present(c,x) ((c).find(x) != (c).end()) #define cpresent(c,x) (find(all(c),x) != (c).end()) #define vi vector <int> #define vvi vector < vi > #define pb push_back #define all(x) (x).begin(),(x).end() #define ll long long #define fr first #define sc second #define vc vector #define pii pair<ll,ll> #define msi map<string,int> #define mii map<int,int> #define psi pair<string,int> #define pis pair<int,string> #define M ((long long)(1000000007)) bool inrange(pii p1, pii p2) { if(p1.fr+p1.sc==p2.fr+p2.sc) return true; if(p1.fr-p1.sc==p2.fr-p2.sc) return true; if(abs(p1.fr-p2.fr)+abs(p1.sc-p2.sc)<=3) return true; return false; } int solve() { pii st,en; cin>>st.fr>>st.sc; cin>>en.fr>>en.sc; if(st==en) return cout<<"0\n",0; if(inrange(st,en)) return cout<<"1\n",0; if(((st.fr+st.sc)&1)==((en.fr+en.sc)&1)) return cout<<"2\n",0; for(ll i=st.fr-3;i<=st.fr+3;i++) { for(ll j=st.sc-3;j<=st.sc+3;j++) { if(abs(i-st.fr)+abs(j-st.sc)>3) continue; if(inrange(en,{i,j})) { return cout<<"2\n",0; } } } cout<<"3\n",0; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int t=1; //cin>>t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int tt(int n){ if(n<1) return 0; return n+tt(n-1); } int main() { int n,k; cin>>n>>k; int s=0; int a=100; int r=tt(k); while(n--){ s=s+( a*k) + r; a=a+100; } cout<<s; return 0; }
//int a = s - '0'; 文字から数字 //小文字から大文字 //transform(a.begin(), a.end(), a.begin(), ::toupper); //map 全探索 //auto begin = p.begin(), end = p.end(); //for (auto it = begin; it != end; it++) {} //mapのキー:it->first mapのバリュー:it->second //大文字判定 isupper(文字) 小文字判定 islower(文字) //do{}while(next_permutation(ALL(配列))) //小文字に対応する文字コード:S[i] - 'a' //文字コード→小文字:(char)(数字+'a') //グラフの距離:隣接行列で扱う //絶対値はソートで取れる //mapで出現チェックするときは++じゃなくて1にする //島渡りの問題:中間ノードに着目 //数が大きい時の比較はstring型で行う //全て0になったか調べたい->0になるたびにcntする //例外処理は最初にする //x = p^m × q^n ×...の約数の個数:(n+1)*(m+1).... //N!のどの素因数で何回割れるか //⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和 //パズルの問題->一般化して全探索 //stack<ll> s; //s.push(要素);s.top();s.pop(); //queue<ll> q; //q.push(要素);q.front();q.pop(); //同じ作業繰り返す系の問題:収束先を見つける //過半数:N/2.0で判定 //優先度付きキュー //priority_queue< //ll, //vector<ll> //> q; #include <bits/stdc++.h> #define rep(i,N) for(int i = 0; i < N; i++) #define ALL(a) (a).begin(),(a).end() #define ll long long int #define PI 3.14159265358979323846264338327950L using namespace std; const ll MOD (pow(10, 9) + 7); // K進数でのNの桁数 ll dig(ll N, ll K) { ll dig = 0; while (N) { dig++; N /= K; } return dig; } // a,bの最大公約数 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); } //a,bの最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } //階乗計算 ll f(ll n) { if (n == 0 || n == 1) return 1; else return (n * f(n - 1)); } //Nのd桁目の数 ll dignum(ll N, ll d) { ll x = pow(10, d); N %= x; ll y = pow(10, d - 1); N /= y; return N; } //Nをdで何回割れるか ll divcnt(ll N, ll d) { ll ans = 0; while (1) { if (N%d == 0) { ans++; N /= d; } else break; } return ans; } //素数判定 bool prime(ll 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> memo(pow(10, 6) + 1); ll fibo(ll n) { if (n == 1) return 1; else if (n == 2) return 1; else if (memo[n] != 0) return memo[n]; else return memo[n] = fibo(n - 1) + f(n - 2); } ll RS(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RS(N, P / 2, M); return t * t % M; } return N * RS(N, P - 1, M); } vector<int> IntegerToVector(int bit, int N) { vector<int> S; for (int i = 0; i < N; ++i) { if (bit & (1 << i)) { S.push_back(i); } } return S; } int main() { int N; cin >> N; vector<ll> A(N); rep(i,N) cin >> A[i]; map<ll,ll> mod_cnt; for(int i = 0; i < N; i++) mod_cnt[A[i]%200]++; ll ans = 0; for(int n = 0; n <= 199; n++){ ans += mod_cnt[n] * (mod_cnt[n]-1) / 2; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int inf32 = (1 << 30) - 1; constexpr ll inf64 = (1LL << 62) - 1LL; constexpr int mod = 1e9 + 7; #define rep(i,n) for(int i=0; i<(n); ++i) #define all(x) x.begin(),x.end() int main(){ //cout << fixed << setprecision(10); int n, m; cin >> n >> m; vector<string> vecs(m); rep(i, m) cin >> vecs[i]; rep(i, n) { string s; while (true) { int r = rand() % m; s += vecs[r]; if (s.length() > n) break; } s = s.substr(0, n); cout << s << endl; } return 0; }
#include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <iomanip> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_map> #include <bitset> #include <chrono> #include <random> #define rep(i,n) for(int i=0;i<n;i++) #define repn(i,n) for(int i=1;i<=n;i++) #define repr(e,x) for(auto& e:x) using namespace std; typedef long long ll; typedef long double ld; // typedef pair<int,int> P; // typedef pair<int,P> IP; // typedef pair<P,P> PP; double const PI=3.141592653589793; int const INF=1001001001; ll const LINF=1001001001001001001; ll const MOD=1000000007; int N,M; string S[800]; void solve(vector<vector<char>> &ans){ rep(i,N){ rep(j,S[i].size()){ ans[i][j]=S[i][j]; } } } int main(){ cin>>N>>M; rep(i,M) cin>>S[i]; vector<vector<char>> ans(N,vector<char>(N,'.')); solve(ans); rep(i,N){ rep(j,N) cout<<ans[i][j]; cout<<endl; } return 0; }
#include<bits/stdc++.h> #include<cmath> #define pb push_back #define ld long double #define mp make_pair #define vl vector<ll> #define vd vector<double> #define vld vector<long double> #define ll long long int #define pl pair<ll, ll> #define all(a) a.begin(), a.end() #define forr(i, n) for(ll i=0; i<n; i++) #define forr1(i, n) for(ll i=1; i<=n; i++) using namespace std; const ld PI =3.1415926535897923846; const ll MOD = 1000000007; const ll N=998244353; ll power(ll x,ll n){ll res=1;while(n>0){if(n&1) res=res*x%MOD;x=x*x%MOD;n>>=1;}return res;} ll modinverse(ll a){return power(a, MOD-2);} void solve() { ll n, k; cin>>n>>k; vector<vl> pos(n, vl(2, 0)); forr(i, n) { cin>>pos[i][0]>>pos[i][1]; } sort(all(pos)); ll po=0; ll money=k; forr(i, n) { if(pos[i][0]-po<=money) { money-=(pos[i][0]-po); po=pos[i][0]; money+=pos[i][1]; } else { po+=money; cout<<po<<endl; return; } } cout<<po+money<<endl; } int main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll test=1; //cin>>test; while(test--) { solve(); } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include <bits/stdc++.h> using namespace std; #define LL long long #define pii pair<LL, LL> int cnt; LL B, C; pii a[5]; int main() { scanf("%lld %lld", &B, &C); a[++ cnt] = pii(B, B + (C - 2) / 2); a[++ cnt] = pii(-B, -B + (C - 1) / 2); a[++ cnt] = pii(B - C / 2, B); a[++ cnt] = pii(-B - (C - 1) / 2, -B); sort(a + 1, a + cnt + 1); int i = 1; LL ans = 0, r; while(i <= cnt) { int j = i + 1; r = a[i].second; while(j <= cnt && a[j].first <= r) r = max(r, a[j].second), j ++; ans += r - a[i].first + 1; i = j; } printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1e9 + 7; constexpr ll INF = 1e18; int main() { ll n, m, i, j, k; ll H, W; cin >> H >> W; ll N, M; cin >> N >> M; vector<ll> A(N), B(N), C(M), D(M); for(i = 0; i < N; i++) { cin >> A[i] >> B[i]; } vector<vector<ll>> blockRow(H + 1), blockColumn(W + 1); for(i = 0; i < M; i++) { cin >> C[i] >> D[i]; blockRow[C[i]].push_back(D[i]); blockColumn[D[i]].push_back(C[i]); } for(i = 0; i < H + 1; i++) { blockRow[i].push_back(0); blockRow[i].push_back(W + 1); sort(blockRow[i].begin(), blockRow[i].end()); } for(i = 0; i < W + 1; i++) { blockColumn[i].push_back(0); blockColumn[i].push_back(H + 1); sort(blockColumn[i].begin(), blockColumn[i].end()); } ll grid1[H + 2][W + 2], grid2[H + 2][W + 2]; for(i = 0; i < H + 2; i++) { for(j = 0; j < W + 2; j++) { grid1[i][j] = 0; grid2[i][j] = 0; } } for(i = 0; i < N; i++) { if(blockRow[A[i]].size() == 2) { grid1[A[i]][0]++; grid1[A[i]][W + 1]--; } else { auto p = lower_bound(blockRow[A[i]].begin(), blockRow[A[i]].end(), B[i]); if(p == blockRow[A[i]].begin()) { grid1[A[i]][(*p) + 1]++; grid1[A[i]][(*(p + 1))]--; } else if(p == blockRow[A[i]].end()) { grid1[A[i]][(*(p - 1)) + 1]++; grid1[A[i]][(*(p))]--; } else { grid1[A[i]][(*(p-1)) + 1]++; grid1[A[i]][(*(p))]--; } } if(blockColumn[B[i]].size() == 2) { grid2[0][B[i]]++; grid2[H + 1][B[i]]--; } else { auto p = lower_bound(blockColumn[B[i]].begin(), blockColumn[B[i]].end(), A[i]); grid2[(*(p-1)) + 1][B[i]]++; grid2[(*(p))][B[i]]--; } } //cout << grid1[3][0] << endl; unordered_set<ll> st; for(i = 1; i < H + 1; i++) { for(j = 1; j < W + 1; j++) { grid1[i][j] += grid1[i][j - 1]; if(grid1[i][j] > 0) { st.insert(i*5000+ j); } } } for(i = 1; i < W + 1; i++) { for(j = 1; j < H + 1; j++) { grid2[j][i] += grid2[j - 1][i]; if(grid2[j][i] > 0) { st.insert(j * 5000 + i); } } } cout << st.size() << endl; return 0; }
#include<bits/stdc++.h> #define ll long long int #define ld long double #define inf LLONG_MAX>>2 #define MAX 1000000 #define mod 1000000007 #define pb push_back #define f(i,a,n,x) for ((i)=(a);(i)<(n);(i)+=(x)) #define fd(i,a,n,x) for ((i)=(a);(i)>=(n);(i)-=(x)) #define fi first #define se second #define mk make_pair #define pi pair #define vt vector #define ms multiset #define is insert #define um unordered_map using namespace std; void read(vt<ll> &a,ll n){ll i,temp;f(i,0,n,1){cin>>temp;a.pb(temp);}} ll fa[MAX]; ll power(ll p,ll q){ ll res=1; p%=mod; while(q){ if (q&1) res=(res*p)%mod; q>>=1; p=(p*p)%mod; } return res; } ll fact(ll n){ if (n<=1) return fa[n]=1; if (fa[n]) return fa[n]; return fa[n]=(n*fact(n-1))%mod; } ll ncr(ll n,ll r){ if (!r) return 1; return (((fact(n)*power(fact(n-r),mod-2))%mod)*power(fact(r),mod-2))%mod; } char a[1500][1500]; void solve(){ ll h,w,n,m,i,j,x,y,count=0; f(i,0,1500,1) f(j,0,1500,1) a[i][j]='_'; cin>>h>>w>>n>>m; f(i,0,n,1){ cin>>x>>y; a[x-1][y-1]='X'; } f(i,0,m,1){ cin>>x>>y; a[x-1][y-1]='Y'; } f(i,0,h,1){ bool flag=false; f(j,0,w,1){ if (a[i][j]=='X') flag=true; if (a[i][j]=='Y' && flag) flag=false; if (flag && a[i][j]=='_') a[i][j]='L'; } } f(i,0,h,1){ bool flag=false; fd(j,w-1,0,1){ if (a[i][j]=='X') flag=true; if (a[i][j]=='Y' && flag) flag=false; if (flag && a[i][j]=='_') a[i][j]='L'; } } f(j,0,w,1){ bool flag=false; f(i,0,h,1){ if (a[i][j]=='X') flag=true; if (a[i][j]=='Y' && flag) flag=false; if (flag && a[i][j]=='_') a[i][j]='L'; } } f(j,0,w,1){ bool flag=false; fd(i,h-1,0,1){ if (a[i][j]=='X') flag=true; if (a[i][j]=='Y' && flag) flag=false; if (flag && a[i][j]=='_') a[i][j]='L'; } } f(i,0,h,1) f(j,0,w,1) if (a[i][j]=='X' || a[i][j]=='L') count++; cout<<count<<endl; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t=1; //cin>>t; while(t--) solve(); return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define ud unsigned int #define ll long long #define ull unsigned long long #define MAX_INF 0x3f #define MAX_INF_VAL 0x3f3f3f3f #define MAX_INF_VAL_LL 0x3f3f3f3f3f3f3f3f //#define pi 3.141592653589 #define eps 1e-9 #define F(x) ((x)/3+((x)%3==1?0:tb)) #define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2) //#define p 2173412051LL //#define sz 2 using namespace std; template< typename T > void read( T &x ) { x = 0; char ch = getchar(); ll f = 1; while( !isdigit( ch ) ) { if( ch == '-' ) f *= -1; ch = getchar(); } while( isdigit( ch ) ) { x = x * 10 + ch - 48; ch = getchar(); } x *= f; } struct custom_hash { static uint64_t splitmix64( uint64_t x ) { x += 0x9e3779b97f4a7c15; x = ( x ^ ( x >> 30 ) ) * 0xbf58476d1ce4e5b9; x = ( x ^ ( x >> 27 ) ) * 0x94d049bb133111eb; return x ^ ( x >> 31 ); } size_t operator() ( uint64_t x ) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64( x + FIXED_RANDOM ); } }; bool vis[ 210 ]; int main() { ios::sync_with_stdio( false ); cin.tie( 0 ), cout.tie( 0 ); int t; ll n; cin >> t >> n; for( int i = 1; i <= 100; ++i ) { int tmp = 0; while( ( tmp + 1 ) * 100 <= ( 100 + t ) * i ) ++tmp; vis[ tmp ] = true; } vector< ll > fk; for( int i = 1; i <= 100 + t; ++i ) if( !vis[ i ] ) fk.push_back( i ); ll m = fk.size(); ll p = n / m, q = n % m; if( q == 0 ) { --p; q = fk.size(); } cout << p * ( 100 + t ) + fk[ q - 1 ]; return 0; }
#include <bits/stdc++.h> using namespace std; int _ = (cout << fixed << setprecision(9), cin.tie(0), ios::sync_with_stdio(0)); using Int = long long; void solve(int N, string S, vector<int> A) { int k = 1e9; for (int i = 0; i < N; i++) { k = min(k, abs(A[i + 1] - A[i])); } vector<vector<int>> B(k, vector<int>(N + 1)); for (int i = 0; i < N + 1; i++) { for (int j = 0; j < k; j++) { B[j][i] += A[i] / k; } for (int j = 0; j < A[i] % k; j++) { B[j][i] += 1; } } cout << k << '\n'; for (int j = 0; j < k; j++) { for (int i = 0; i < N + 1; i++) { cout << B[j][i] << " \n"[i + 1 == N + 1]; } } } int main() { int N; cin >> N; string S; cin >> S; vector<int> A(N + 1); for (auto &a : A) cin >> a; solve(N, S, A); return 0; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, from, to) for (ll i = from; i < (to); ++i) #define mp(x,y) make_pair(x,y) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define pb push_back using ll = long long; using ld=long double; using vin=vector<int>; using vvin=vector<vin>; using vll=vector<ll>; using vvll=vector<vll>; using vst=vector<string>; using P = pair<ll,ll>; const int inf=1e9+7; const ll INF=9e18; const long double PI = acos(-1.0); template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;} template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;} template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } const int dx[4] = { 1, 0, -1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; int main(){//cout<<fixed<<setprecision(20); int h,w; cin>>h>>w; vst s(h); rep(i,0,h)cin>>s[i]; int ans=0; rep(i,0,h){ rep(j,0,w){ if(s[i][j]!='.')continue; if(j+1<w&&s[i][j+1]=='.')ans++; if(i+1<h&&s[i+1][j]=='.')ans++; } } cout<<ans<<endl; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++) #define repr(i, n) for (ll i = n; i >= 0; i--) #define pb push_back #define COUT(x) cout << (x) << "\n" #define COUTF(x) cout << setprecision(15) << (x) << "\n" #define ENDL cout << "\n" #define DF(x) x.erase(x.begin()) #define ALL(x) x.begin(), x.end() #define SORT(x) sort(ALL(x)) #define RSORT(x) sort(x.rbegin(), x.rend()) #define REVERSE(x) reverse(ALL(x)) #define MAX(x) *max_element(ALL(x)) #define MAXI(x) max_element(ALL(x)) - x.begin() #define SUM(x) accumulate(ALL(x), 0ll) #define COUNT(x, y) count(ALL(x), y); #define ANS cout << ans << "\n" #define YES cout << "YES\n"; #define NO cout << "NO\n"; #define Yes cout << "Yes\n"; #define No cout << "No\n"; #define init() \ cin.tie(0); \ ios::sync_with_stdio(false) #define debug(x) cerr << "[debug] " << #x << ": " << x << endl; #define debugV(v) \ cerr << "[debugV] " << #v << ":"; \ rep(z, v.size()) cerr << " " << v[z]; \ cerr << endl; using namespace std; using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vector<ll>>; using mll = map<ll, ll>; using qll = queue<ll>; using P = pair<ll, ll>; using vp = vector<P>; using vs = vector<string>; template <typename T> inline istream& operator>>(istream& i, vector<T>& v) { rep(j, v.size()) i >> v[j]; return i; } template <typename T1, typename T2> inline istream& operator>>(istream& i, pair<T1, T2>& v) { return i >> v.first >> v.second; } constexpr ll INF = 0x3f3f3f3f3f3f3f3f; constexpr ld PI = 3.141592653589793238462643383279; ll get_digit(ll x) { return to_string(x).size(); } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } template <class T> void chmin(T& a, const T& b) { if (b < a) a = b; } signed main() { init(); ll N, M; cin >> N >> M; vvll to(N); rep(i, M) { ll a, b; cin >> a >> b; a--; b--; to[a].pb(b); } ll ans = 0; rep(i, N) { vll dist(N, -1); queue<P> q; q.push({i, 0}); while (!q.empty()) { ll index = q.front().first; ll d = q.front().second; q.pop(); if (dist[index] != -1) continue; dist[index] = d; for (auto t : to[index]) q.push({t, d + 1}); } rep(i, N) if (dist[i] != -1) ans++; } ANS; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define chmin(a, b) a = min(a, b) #define chmax(a, b) a = max(a, b) #define ALL(v) v.begin(),v.end() #define sz(v) (ll)v.size() const ll INF = 1e18; // const ll MOD = 998244353; // const ll MOD = 1000000007; ll dx[] = {1, 0, -1, 0}; ll dy[] = {0, 1, 0, -1}; const double PI = acos(-1.0); //-------------------------------------------------- //-------------------------------------------------- int main(){ // ios::sync_with_stdio(false); // cin.tie(nullptr); ll n; cin >> n; string s; cin >> s; vector<ll> f(400100), g(400100); ll san = 1; g[0] = 1; for(int i = 1; i <= 400000; i++){ ll pos = i; while(pos % 3 == 0) {pos /= 3; f[i]++;} g[i] = pos%3; } for(ll i = 1; i <= 400000; i++) { f[i] += f[i-1]; g[i] = (g[i]*g[i-1])%3; } // cout << "f : "; // for(ll i = 0; i < 10; i++) { // cout << f[i] << " \n"[i==9]; // } // cout << "g : "; // for(ll i = 0; i < 10; i++) { // cout << g[i] << " \n"[i==9]; // } // BWR // 012 for(ll i = 0; i < n; i++) { if(s[i]=='B') s[i] = '0'; else if(s[i]=='W') s[i] = '1'; else if(s[i]=='R') s[i] = '2'; } ll ans = 0; ll N = n-1; ll r; for(ll i = 0; i < n; i++) { r = i; ll p; if(f[N] > f[r] + f[N-r]) { p = 0; }else{ if(g[N]==1&&g[r]*g[N-r]==1) p = 1; else if(g[N]==1&&g[r]*g[N-r]==2) p = 2; else if(g[N]==1&&g[r]*g[N-r]==4) p = 1; else if(g[N]==2&&g[r]*g[N-r]==1) p = 2; else if(g[N]==2&&g[r]*g[N-r]==2) p = 1; else if(g[N]==2&&g[r]*g[N-r]==4) p = 2; } ans = (ans + (s[i]-'0') * p) % 3; } if(n%2==0) ans = (3-ans)%3; char ret; if(ans == 0) ret = 'B'; else if(ans == 1) ret = 'W'; else ret = 'R'; cout << ret << endl; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> using namespace std; // size(x), rbegin(x), rend(x) need C++17 #define sz(x) int((x).size()) #define pb push_back #define mp make_pair #define fi first #define se second #define dbg(x) cout << "[" << #x << "]: " << x << endl; #define forn(i,n) for(int i=0; i < n;i++) using ll = long long; using db = long double; //pode ser double using pi = pair<int,int>; using pl = pair<ll,ll>; using pd = pair<db,db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<string>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count()); const int MAXN = 2e5 + 5; const int MOD = 1e9+7; //998244353; const int INF = 0x3f3f3f3f; const ll INF64 = ll(4e18) + 5; const db EPS = 1e-9; const db PI = acos((db)-1); bool perm(vi &p, int n){ bool certo = true; forn(i,n){ if(p[i] != i) certo = false; } return certo; } void solve(){ int n; cin >> n; vi p(n); forn(i,n){ cin >> p[i]; p[i] -= 1; } vi ans; int at = 0; for(int i = n-1; i >= 3; i--){ //colocando i em seu devido lugar //achando onde esta i int pos_i = -1; for(int j = 0; j <= i; j++){ if(p[j] == i) pos_i = j; } if(pos_i == i) continue; if(pos_i%2 != at%2){ if(at%2 == 0){ swap(p[0], p[1]); at++; ans.pb(0); if(p[0] == i){ swap(p[1], p[2]); at++; ans.pb(1); pos_i = 0; } }else{ swap(p[1], p[2]); at++; ans.pb(1); if(p[1] == i){ swap(p[2], p[3]); at++; ans.pb(2); pos_i = 1; } } } //agora posso comecar a mover o i para direita for(int j = pos_i; j < i; j++){ ans.pb(j); at++; swap(p[j], p[j+1]); } } //neste momento tenho um de tamanho 3 if(n >= 3){ if(p[2] != 2){ //se 2 nao esta certo if(p[1] == 2 && at%2 == 1){ swap(p[1], p[2]); at++; ans.pb(1); }else if(p[1] == 2 && at%2 == 0){ swap(p[0], p[1]); at++; ans.pb(0); swap(p[1],p[2]); at++; ans.pb(1); swap(p[0],p[1]); at++; ans.pb(0); swap(p[1], p[2]); at++; ans.pb(1); }else{ if(at%2 == 1){ swap(p[1], p[2]); at++; ans.pb(1); } swap(p[0],p[1]); at++; ans.pb(0); swap(p[1], p[2]); at++; ans.pb(1); } }else{ if(at%2 == 1 && p[0] == 1){ swap(p[1], p[2]); at++; ans.pb(1); swap(p[0],p[1]); at++; ans.pb(0); swap(p[1], p[2]); at++; ans.pb(1); swap(p[0],p[1]); at++; ans.pb(0); swap(p[1], p[2]); at++; ans.pb(1); } } } if(p[0] == 1){ swap(p[0],p[1]); at++; ans.pb(0); } cout << ans.size() << '\n'; if(ans.size() > 0){ for(int i = 0; i < ans.size(); i++){ cout << ans[i] +1 << " "; }cout << '\n'; } } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while(t--){ solve(); } return 0; } /* PRESTA ATENCAO!!!! NAO FIQUE PRESO EM UMA ABORDAGEM A SUA INTUICAO PODE ESTAR ERRADA - TENTE OUTRAS COISAS LIMPOU TUDO PARA O PROXIMO CASO? caso especial? n == 1 */
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl") #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <string.h> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define LL long long #define VE vector #define VI vector<int> #define FOR(i,a,n) for(i=a;i<n;++i) #define rep(i,n) FOR(i,0,n) #define MOD 998244353 #define coutl cout<<fixed<<setprecision(15) #define FAST cin.tie(0);ios::sync_with_stdio(false) #define MI modint<MOD> template<int mod>struct modint{ int x; modint():x(0){} modint(LL n):x(n>=0?n%mod:(mod-(-n)%mod)%mod){} modint &operator+=(const modint &n){if((x+=n.x)>=mod)x-=mod;return *this;} modint &operator-=(const modint &n){if((x+=mod-n.x)>=mod)x-=mod;return *this;} modint &operator++(){*this+=1;return *this;} modint &operator--(){*this-=1;return *this;} modint &operator*=(const modint &n){x=(int)((LL)x*n.x%mod);return *this;} modint &operator/=(const modint &n){*this*=n.inv();return *this;} modint operator-()const{return modint(-x);} modint operator+(const modint &n)const{return modint(*this)+=n;} modint operator-(const modint &n)const{return modint(*this)-=n;} modint operator++(int){modint ret(*this);*this+=1;return ret;} modint operator--(int){modint ret(*this);*this-=1;return ret;} modint operator*(const modint &n)const{return modint(*this)*=n;} modint operator/(const modint &n)const{return modint(*this)/=n;} bool operator<(const modint &n)const{return x<n.x;} bool operator>(const modint &n)const{return x>n.x;} bool operator<=(const modint &n)const{return x<=n.x;} bool operator>=(const modint &n)const{return x>=n.x;} bool operator!=(const modint &n)const{return x!=n.x;} bool operator==(const modint &n)const{return x==n.x;} friend istream &operator>>(istream &is,modint &n){LL l;is>>l;n=modint<mod>(l);return is;} friend ostream &operator<<(ostream &os,const modint &n){return os<<n.x;} int getmod(){return mod;} modint inv()const{int a=x,b=mod,c=1,d=0,n;while(b){n=a/b;swap(a-=n*b,b);swap(c-=n*d,d);}return modint(c);} modint pow(LL n)const{modint ret(1),m(x);while(n){if(n&1)ret*=m;m*=m;n>>=1;}return ret;} }; int main(){ FAST; int i,j,N,K; cin>>N>>K; VI A(N); rep(i,N)cin>>A[i]; MI k,ans; VE<MI> v(K+1),p(K+1),q(K+1),a(K+1); p[0]=q[0]=1; FOR(i,1,K+1)p[i]=p[i-1]*i,q[i]=q[i-1]/i; v[0]=N; rep(j,N){ k=A[j]; FOR(i,1,K+1){ v[i]+=k*q[i]; a[i]+=k; k*=A[j]; } } FOR(i,1,K+1){ ans=0; rep(j,i+1)ans+=v[j]*v[i-j]; ans*=p[i]; k=a[i]; rep(j,i)k*=2; ans=(ans-k)/2; cout<<ans<<endl; } }
/* #pragma GCC optimize("O2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx,avx2,sse,sse2,fma") //*/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int ,int > pii; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll maxn = 3e6; const ll mod =998244353; const ld PI = acos((ld)-1); #define pb push_back #define endl '\n' #define dokme(x) cout << x , exit(0) #define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define ms(x , y) memset(x , y , sizeof x) ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);} ll fact[maxn]; ll inv[maxn]; ll n , k; ll a[maxn]; ll sum[maxn]; int32_t main(){ migmig; cin >> n >> ::k; fact[0] = inv[0] = 1; for(int i = 1 ; i <= ::k ; i ++) fact[i] = (fact[i - 1] * i)%mod , inv[i] = pw(fact[i] , mod - 2); for(int i = 1 ; i <= n ; i ++) cin >> a[i]; for(int i = 0 ; i <= ::k ; i ++) for(int j = 1 ; j <= n ; j ++) sum[i] = (sum[i] + pw(a[j] , i))%mod; for(int x = 1 ; x <= ::k ; x ++){ ll ans = 0; for(int k = 0 ; k <= x ; k ++){ ll res = (sum[k] * sum[x - k])%mod; res = (res * inv[k])%mod; res = (res * inv[x - k])%mod; ans = (ans + res)%mod; } ans = (ans * fact[x])%mod; for(int i = 1 ; i <= n ; i ++) ans = (ans - pw(a[i]*2 , x) + mod)%mod; cout << (ans*pw(2 , mod - 2))%mod << endl; } return(0); }
#include<bits/stdc++.h> #define int long long #define pb push_back #define mp make_pair #define WT int T=read();while(T--) #define NO puts("NO"); #define YES puts("YES"); using namespace std; inline int read() { char c=getchar();int x=0;bool f=0; for(;!isdigit(c);c=getchar())f^=!(c^45); for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48); if(f)x=-x;return x; } const int Mod=998244353; const int M=510; int n,m,a[M][M],ans=1; vector<int>v[M<<1]; signed main() { cin>>n>>m; for (int i=1;i<=n;i++) { string s;cin>>s; for (int j=0;j<m;j++) if (s[j]=='B')a[i][j+1]=1; else if (s[j]=='R')a[i][j+1]=2; else a[i][j+1]=3; } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) v[i+j].pb(a[i][j]); for (int i=2;i<=n+m;i++) { int X=0,cnt=0,f=1; for (int j=0;j<v[i].size();j++) { if (v[i][j]<=2) { if (!X||X==v[i][j])X=v[i][j]; else f=0; } else cnt++; }ans=ans*f*(1+(X==0))%Mod; }cout<<ans<<endl; return 0; }
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") #pragma GCC optimize("O3") #include <bits/stdc++.h> #include <sys/time.h> using namespace std; #define NUM_CAN 400 struct state { int x; int y; int score; string path; set<int> used; }; bool operator< (const state &state1, const state &state2){ return state1.score < state2.score; }; int main() { struct timeval tmp_time; gettimeofday(&tmp_time, NULL); long long program_init_time = tmp_time.tv_sec*1000000+tmp_time.tv_usec; long long fin_time, current_time; fin_time = program_init_time + 1500000; int si, sj, count; cin >> si >> sj; vector<vector<int>> tiling(50,vector<int>(50)), grids(50,vector<int>(50)); for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) cin >> tiling.at(i).at(j); } for (int i = 0; i < 50; i++) { for (int j = 0; j < 50; j++) cin >> grids.at(i).at(j); } vector<int> dx{1,-1,0,0}, dy{0,0,1,-1}; string dir = "DURL"; queue<state> que; priority_queue<state> next_que, blank; state now_state; int best_score; string best_path; now_state.x = si, now_state.y = sj; now_state.score = grids.at(si).at(sj); now_state.path = ""; now_state.used.insert(tiling.at(si).at(sj)); best_score = now_state.score; best_path = now_state.path; que.push(now_state); count = 0; while (!que.empty()) { while (!que.empty()) { count++; now_state = que.front(); que.pop(); for (int i = 0; i < 4; i++) { if (now_state.x+dx.at(i) < 0 || now_state.x+dx.at(i) >= 50 || now_state.y+dy.at(i) < 0 || now_state.y+dy.at(i) >= 50) continue; if (now_state.used.count(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i)))) continue; now_state.score += grids.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i)); now_state.path += dir.at(i); now_state.used.insert(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i))); now_state.x += dx.at(i), now_state.y += dy.at(i); if (best_score < now_state.score) { best_score = now_state.score; best_path = now_state.path; } next_que.push(now_state); now_state.x -= dx.at(i), now_state.y -= dy.at(i); now_state.score -= grids.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i)); now_state.path.pop_back(); now_state.used.erase(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i))); } if (count % 100 == 0) { gettimeofday(&tmp_time, NULL); current_time = tmp_time.tv_sec*1000000 + tmp_time.tv_usec; if (current_time > fin_time) break; } } if (count % 100 == 0) { if (current_time > fin_time) break; } for (int i = 0; i < NUM_CAN; i++) { if (next_que.empty()) break; que.push(next_que.top()); next_que.pop(); } next_que = blank; } cout << best_path << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define rep2(i,x,n) for(int i=x; i<(n); i++) #define pb push_back #define eb emplace_back #define sz size #define ALL(v) v.begin(),v.end() const long long INF = 1LL << 60; using namespace std; using ll = long long; using P = pair<int, int>; using LP = pair<ll, ll>; using P3 = tuple<int, int, int>; #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vs vector<string> 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 h, w, n, m; cin >> h >> w >> n >> m; vvi g(h, vi(w, 0)); vector<P> p; rep(i, n){ int a, b; cin >> a >> b; --a; --b; // g[a][b] = 1; p.eb(a, b); } rep(i, m){ int a, b; cin >> a >> b; --a; --b; g[a][b] = -1; } //tate rep(i, n){ int x = p[i].first; int y = p[i].second; if(!(g[x][y] &1)){ rep2(xi, x+1, h){ if(g[xi][y] == -1) break; if((g[xi][y] &1)) break; g[xi][y] = 1; } for(int xi = x-1; xi>=0 ; xi--){ if(g[xi][y] == -1) break; if((g[xi][y] &1)) break; g[xi][y] = 1; } } if(!(g[x][y] &2)){ rep2(yi, y+1, w){ if(g[x][yi] == -1) break; if((g[x][yi] &2)) break; g[x][yi] = 2; } for(int yi = y-1; yi>=0 ; yi--){ if(g[x][yi] == -1) break; if((g[x][yi] &2)) break; g[x][yi] = 2; } } g[x][y] = 3; } int ans = 0; rep(i, h)rep(j, w){ if(g[i][j] > 0) ++ans; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // #include "icld.cpp" using namespace std; using ll = long long int; using vi = vector<int>; using si = set<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using ss = string; using db = double; template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; #define V vector #define P pair<int,int> #define PLL pair<ll,ll> #define rep(i,s,n) for(int i=(s);i<(int)(n);i++) #define rev(i,s,n) for(int i=(s);i>=(int)(n);i--) #define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i] #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define ci(x) cin >> x #define cii(x) ll x;cin >> x #define cci(x,y) ll x,y;cin >> x >> y #define co(x) cout << x << endl #define pb push_back #define eb emplace_back #define rz resize #define pu push #define sz(x) int(x.size()) #define vij v[i][j] // ll p = 1e9+7; // ll p = 998244353; // n do -> n*pi/180 #define yn cout<<"Yes"<<endl;else cout<<"No"<<endl #define YN cout<<"YES"<<endl;else cout<<"NO"<<endl template<class T>void chmax(T &x,T y){x=max(x,y);} template<class T>void chmin(T &x,T y){x=min(x,y);} int main(){ cci(h,w); vvi v(h+2,vi(w+2,-1)); rep(i,1,h+1){ rep(j,1,w+1)vij=0; } cci(n,m); vvi lt(n,vi(2)),bl(m,vi(2)); rep(i,0,n){ rep(j,0,2)ci(lt[i][j]); } rep(i,0,m){ rep(j,0,2)ci(bl[i][j]); v[bl[i][0]][bl[i][1]]=-1; } vvi tt=v,yk=v; rep(i,0,n){ int x=lt[i][0]; int y=lt[i][1]; int nx=x,ny=y; tt[x][y]=yk[x][y]=1; while(tt[nx-1][y]==0)tt[--nx][y]=1; while(yk[x][ny-1]==0)yk[x][--ny]=1; nx=x,ny=y; while(tt[nx+1][y]==0)tt[++nx][y]=1; while(yk[x][ny+1]==0)yk[x][++ny]=1; } /* rep(i,2,h+1){ rep(j,1,w+1){ if(yk[i][j]==-1)continue; chmax(yk[i][j],yk[i-1][j]); } } rep(i,2,w+1){ rep(j,1,h+1){ if(tt[j][i]==-1)continue; chmax(tt[j][i],tt[j][i-1]); } } */ int ans=0; rep(i,1,h+1){ rep(j,1,w+1){ if(yk[i][j]==1 or tt[i][j]==1){ // cout<<1<<" "; ans++; } // else if(vij==-1)cout<<'x'<<" "; // else cout<<0<<" "; } // co(""); } co(ans); }
// Jai Shree Ram #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; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define ook order_of_key #define fbo find_by_order #define rep(i,a,n) for(int i=a;i<n;i++) #define ll long long #define int long long #define pb push_back #define all(v) v.begin(),v.end() #define endl "\n" #define x first #define y second #define gcd(a,b) __gcd(a,b) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define sz(a) (int)a.size() #define pii pair<int,int> #define hell 1000000007 #define elasped_time 1.0 * clock() / CLOCKS_PER_SEC template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;} template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;} template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;} const int N=1e9+5; const int M=1e7; struct node{ int l;int r; int val; int cnt; }t[M]; int cnt=0; int n; void extend(int v){ if(t[v].l==0){ t[v].l=++cnt; t[v].r=++cnt; } } void pushup(int v){ t[v].val = t[t[v].l].val + t[t[v].r].val; t[v].cnt = t[t[v].l].cnt + t[t[v].r].cnt; } void update(int v,int l,int r,pii val,int tl=0,int tr=1e9){ if(tl>r || l>tr)return; if(l<=tl && tr<=r){ assert(tl == tr); t[v].val += val.x; t[v].cnt += val.y; return; } extend(v); int mid=(tl+tr)/2; update(t[v].l,l,r,val,tl,mid); update(t[v].r,l,r,val,mid+1,tr); pushup(v); } pii query(int v,int l,int r,int tl=0,int tr=1e9){ if(tl>r || l>tr)return {0,0}; if(l<=tl && tr<=r){ return {t[v].val,t[v].cnt}; } int mid=(tl+tr)/2; extend(v); pii a =query(t[v].l,l,r,tl,mid); pii b =query(t[v].r,l,r,mid+1,tr); return {a.x+b.x,a.y+b.y}; } int solve(){ int n,m,q; cin >> n >> m >> q; int ans = 0; int roota = ++cnt; int rootb = ++cnt; vector<int>v1(n+1),v2(m+1); update(roota,0,0,{0,n}); update(rootb,0,0,{0,m}); rep(i,0,q){ int t; cin >> t; int x,y; cin >> x >> y; if(t == 1){ if(y > v1[x]){ auto s = query(rootb,0,y); ans += s.y*y; s = query(rootb,v1[x]+1,y); ans -= s.x; s = query(rootb,0,v1[x]); ans -= v1[x]*s.y; } else if(y < v1[x]){ auto s = query(rootb,0,v1[x]); ans -= s.y*v1[x]; s = query(rootb,0,y); ans += s.y*y; s = query(rootb,y+1,v1[x]); ans += s.x; } update(roota,v1[x],v1[x],{-v1[x],-1}); update(roota,y,y,{y,1}); v1[x] = y; } else{ if(y > v2[x]){ auto s = query(roota,0,y-1); ans += s.y*y; s = query(roota,0,v2[x]-1); ans -= s.y*v2[x]; s = query(roota,v2[x],y-1); ans -= s.x; } else if(y < v2[x]){ auto s = query(roota,0,v2[x]-1); ans -= s.y*v2[x]; s = query(roota,0,y-1); ans += s.y*y; s = query(roota,y,v2[x]-1); ans += s.x; } update(rootb,v2[x],v2[x],{-v2[x],-1}); update(rootb,y,y,{y,1}); v2[x] = y; } cout << ans << endl; } return 0; } signed main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif int t=1;//cin>>t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s[3]; cin >>s[0]; cin>>s[1]; cin>>s[2]; int n[3]; n[0]=s[0].size();n[1]=s[1].size();n[2]=s[2].size(); set<char> st; for( int k = 0; k < 3; k++ ) { for( int i = 0; i < n[k]; i++ ) { st.insert( s[k][i] ); } } int st_s = st.size(); if( st_s > 10 ) { cout << "UNSOLVABLE" << endl; return 0; } set<char> st1; st1.insert( s[0][0] ); st1.insert( s[1][0] ); st1.insert( s[2][0] ); vector<int> num( 10 ); for( int i = 0; i < 10; i++ ) num[i] = i; long long ans[3]; ans[0] = -1; do { map<char, int> mp; int flag = 0; for( int i = 0; i < st_s; i++ ) { char c = *next( st.begin(), i ); mp[c] = num[i]; if( num[i] == 0 && st1.count( c ) ) { flag = 1; break; } } if( flag == 0 ) { long long m[3]; for( int k = 0; k < 3; k++ ) { m[k] = 0; for( int i = 0; i < n[k]; i++ ) { m[k] = m[k] * 10 + mp[ s[k][i] ]; } } if( m[0] + m[1] == m[2] ) { ans[0] = m[0]; ans[1] = m[1]; ans[2] = m[2]; break; } } } while( next_permutation( num.begin(), num.end() ) ); if( ans[0] < 0 ) cout << "UNSOLVABLE" << endl; else { cout << ans[0] << endl; cout << ans[1] << endl; cout << ans[2] << endl; } }
//BISMILLAHIR RAHMANIR RAHIM #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mem(a, b) (memset(a, b, sizeof(a))) #define pb push_back #define mk make_pair #define ff first #define ss second #define PI acos(-1) #define min3(a,b,c) min(a,min(b,c)) #define max3(a,b,c) max(a,max(b,c)) #define min4(a,b,c,d) min(a,min(b,min(c,d))) #define max4(a,b,c,d) max(a,max(b,max(c,d))) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define ROF(i,a,b) for(int i=a;i>=b;i--) #define REP(i,b) for(int i=0;i<b;i++) #define all(v) v.begin(),v.end() #define SORT(v) sort(v.begin(),v.end()) #define REV(v) reverse(v.begin(),v.end()) #define INF 2147483647 #define MOD 1000000007 #define MAX 200005 using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; typedef pair<ii, int> pii; typedef pair<ll, ll> LL; typedef vector<ii> vii; typedef priority_queue<int,vector<int>,greater<int> > PQ; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int setBit(int mask, int pos){return mask = mask | (1<<pos);} bool checkBit(int mask, int pos){return (bool)(mask & (1<<pos));} ll power(ll a, ll b) { ll ans = 1; FOR(i, 1, b) ans *= a; return ans; } ll getScore(vector<int> cnt) { ll score = 0; FOR(i, 1, 9) { score += i * 1LL * power(10LL, (ll) cnt[i]); } return score; } void solve() { ll k; cin>>k; string s, t; cin>>s>>t; vector<int> cnt1(10, 0), cnt2(10, 0); REP(i, 4) { cnt1[s[i]-'0']++; cnt2[t[i]-'0']++; } ll cnt = 0; FOR(i, 1, 9) { FOR(j, 1, 9) { if((k - (cnt1[i] + cnt2[i])) > 0 && (k - (cnt1[j] + cnt2[j])) > 0) { //cout<<i<<" "<<j<<endl; cnt1[i]++; cnt2[j]++; ll sa = getScore(cnt1); ll st = getScore(cnt2); cnt1[i]--; cnt2[j]--; if(sa > st) { //cout<<i<<" "<<j<<endl; ll x; if(i != j) x = 1LL * (k - (cnt1[i] + cnt2[i])) * (k - (cnt1[j] + cnt2[j])); else { ll ttx = (k - (cnt1[i] + cnt2[i])); x = (ttx * (ttx - 1)); } cnt += x; } } } } ll tx = 9 * k - 8; tx = (tx * (tx - 1)); //cout<<"TEST: "<<cnt<<" "<<tx<<endl; double ans = (double) cnt / (double) tx; cout<<setiosflags(ios::fixed)<<setprecision(12); cout<<ans<<endl; return; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int test = 1; //cin>>test; while(test--) { solve(); } return 0; }
#include <bits/stdc++.h> #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #endif using namespace std; using ll = long long; struct Edge{ ll to; ll cost; }; using Graph = vector<vector<Edge>>; using P =pair<ll,ll>; #define mp make_pair #define REP(i, n) for (int i = 0; i < (n); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) #define ALL(v) (v).begin(),v.end() const ll MOD = 1000000007; const ll nmax = 8; const ll INF = LLONG_MAX; const int MAX = 510000; bool graph[nmax][nmax]; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF)); void warshall_floyd(ll n) { for (size_t i = 0; i < n; i++) { for (size_t j = 0; j < n; j++) { for (size_t k = 0; k < n; k++) { dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]); } } } } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); } ll powMod(ll a, ll p) { if (p == 0) { return 1; } else if (p % 2 == 0) { ll half = powMod(a, p / 2); return mulMod(half, half); } else { return mulMod(powMod(a, p - 1), a); } } ll ceil(ll a, ll b) { return (a + b - 1) / b; } void solve(long long N, long long A, long long B){ cout << N - A + B << endl; } int main(){ long long N; scanf("%lld",&N); long long A; scanf("%lld",&A); long long B; scanf("%lld",&B); solve(N, A, B); return 0; }
#include <bits/stdc++.h> #include <algorithm> #include <string.h> using namespace std; #define MOD (1000000000+7) #define alp "abcdefghijklmnopqrstuvwxyz" #define for0(i,n) for(long long i = 0;i < n;i++) #define for01(i,n) for(long long i = 0;i <= n;i++) #define for1(i,n) for(long long i = 1;i < n;i++) #define for11(i,n) for(long long i = 1;i <= n;i++) #define pii pair<int, int> #define vi vector<int> #define vii vector<vector<int> > #define vb vector<bool> #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define mp make_pair #define test int T; cin>>T; #define uset unordered_set #define umultiset unordered_multiset typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; void no(){cout << "NO\n";} void yes(){cout << "YES\n";} bool sortbysec(const pair<int,int> &a, const pair<int,int> &b) {return (a.second < b.second);} bool sortpairdif(const pair<int,int> &a, const pair<int,int> &b) {if(a.first == b.first) return a.second > b.second; return a.first < b.first;} ll llgcd(ll a, ll b){if (a == 0) return b; return llgcd(b % a, a);} int igcd(int a, int b){if (a == 0) return b; return igcd(b % a, a);} int main(){ cin.tie(0); ios::sync_with_stdio(0); test; ull n; while(T--){ cin >> n; if(n % 4 == 0){ cout << "Even\n"; }else if(n % 2 == 0){ cout << "Same\n"; }else{ cout << "Odd\n"; } } return 0; }
//#include <tourist> #include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<ll, ll> p; const int INF = 1e9; const double eps = 1e-7; const ll LINF = ll(1e18); const int MOD = 1000000007; const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const long double pi = 4 * atan(1); #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl #define rep(i, n) for (int i = 0; i < n; i++) #define ALL(v) v.begin(), v.end() #define debug(v) \ cout << #v << ":"; \ for (auto x : v) \ { \ cout << x << ' '; \ } \ cout << 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 (b < a) { a = b; return 1; } return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; } //cout<<fixed<<setprecision(15);有効数字15桁 //-std=c++14 //g++ yarudake.cpp -std=c++17 -I . 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 calc(string a, string b) { ll a_int = stoi(a); ll b_int = stoi(b); return b_int - a_int; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, k; string s; cin >> s >> k; rep(i, k) { sort(ALL(s)); string s_min = s; reverse(ALL(s)); string s_max = s; ll r = calc(s_min, s_max); s = to_string(r); } cout << s << "\n"; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20); vector<string> s(3); set<char> S; for (int i = 0; i < 3; i++) { cin >> s[i]; for (auto c : s[i]) { S.insert(c); } } if (S.size() > 10) { cout << "UNSOLVABLE\n"; return 0; } vector<int> p(10); iota(p.begin(), p.end(), 0); vector<char> v; for (auto c : S) v.push_back(c); map<char, int> M; for (int i = 0; i < int(v.size()); i++) M[v[i]] = i; do { vector<ll> a(3); bool ok = false; for (int i = 0; i < 3; i++) { if (p[M[s[i][0]]] == 0) { ok = true; break; } for (auto c : s[i]) { a[i] = a[i] * 10 + p[M[c]]; } if (a[i] <= 0) { ok = true; break; } } if (ok) continue; if (a[0] + a[1] == a[2]) { for (int i = 0; i < 3; i++) { cout << a[i] << '\n'; } return 0; } } while (next_permutation(p.begin(), p.end())); cout << "UNSOLVABLE\n"; return 0; }
/* "An anomaly, I'm Muhammad Ali Cause I know one day I'm gonna be the" - Greatest, Eminem */ #pragma GCC optimize ("O3") #pragma GCC target ("sse4") #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; typedef long long int ll; #define ff first #define Shazam ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define ss second #define all(c) c.begin(),c.end() #define endl "\n" #define test() int t; cin>>t; while(t--) #define fl(i,a,b) for(int i = a ; i <b ;i++) #define get(a) fl(i,0,a.size()) cin>>a[i]; #define pra(a) fl(i,0,a.size()) cout<<a[i]<<" "; cout<<endl; #define pr(a,n) fl(i,0,n) cout<<a[i]<<" "; cout<<endl; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const ll INF = 2e18; const int inf = 2e9; const int mod1 = 1e9 + 7; int main(){ Shazam; vector<int> val(10); iota(all(val), 0); string a, b, c; cin >> a >> b >> c; set<char> bag; for(char c : a){ bag.insert(c); } for(char c : b){ bag.insert(c); } for(char c : c){ bag.insert(c); } if(bag.size() > 10){ cout << "UNSOLVABLE" << endl; return 0; } vector<char> letters; vector<int> whereat(26); for(char c : bag){ letters.push_back(c); } for(int i = 0; i < letters.size(); i++){ whereat[letters[i] - 'a'] = i; } auto getnum = [&](string s){ ll res = 0; for(char c : s){ int loc = whereat[c - 'a']; ll currval = val[loc]; res *= 10LL; res += currval; if(!res) return 0LL; } return res; }; do{ ll num1 = getnum(a); ll num2 = getnum(b); ll num3 = getnum(c); if(num1 && num2 && num3 && num1 + num2 == num3){ cout << num1 << endl; cout << num2 << endl; cout << num3 << endl; return 0; } }while(next_permutation(all(val))); cout << "UNSOLVABLE" << endl; return 0; }
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> const int N = 2e5; const int mod = 998244353; using namespace std; int n,a[N + 5],fa[N + 5]; int mypow(int a,int x){int s = 1;for (;x;x & 1 ? s = 1ll * s * a % mod : 0,a = 1ll * a * a % mod,x >>= 1);return s;} int find(int x) { if (x == fa[x]) return x; return fa[x] = find(fa[x]); } int main() { scanf("%d",&n); for (int i = 1;i <= n;i++) scanf("%d",&a[i]); for (int i = 1;i <= n;i++) fa[i] = i; for (int i = 1;i <= n;i++) { int X = find(i),Y = find(a[i]); if (X != Y) fa[X] = Y; } int sm = 0; for (int i = 1;i <= n;i++) if (fa[i] == i) sm++; cout<<((mypow(2,sm) - 1) % mod + mod) % mod<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define all(v) v.begin(), v.end() using in = int64_t; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define Yes cout << "Yes" << endl #define No cout << "No" << endl #define yes cout << "yes" << endl #define no cout << "no" << endl #define vec(a, y, x) vector<vector<char>> a(y, vector<char>(x)) using P = pair<int, int>; using T = tuple<int, int, int>; const double PI = 3.14159265358979323846; const in MOD = 998244353; const in INF = 1e18 + 7; const int inf = 1e9 + 7; const vector<int> dx = {1, 0, -1, 0}; const vector<int> dy = {0, 1, 0, -1}; //cout << fixed << setprecision(10); int main(void) { int n; cin>>n; vector<int>graph(n); set<int> seen;//観たら消去 int kazu=0;//サイクルの個数 rep(i,n){ int f; cin>>f; f--; graph.at(i)=f; } rep(i,n){ seen.insert(i); } while(!seen.empty()){ int start = *begin(seen); set<int> mita;//見たやつを入れていく mita.insert(start); int now=start; seen.erase(start); while(1){ now=graph.at(now); if(mita.count(now)){ kazu++; break; } if(!seen.count(now)) break; seen.erase(now); mita.insert(now); } } in ans = 1; rep(i,kazu){ ans*=2; ans=ans%MOD; } ans--; cout<<ans<<endl; }
#include<bits/stdc++.h> #define FULL(x,y) memset(x,y,sizeof(x)) #define ll long long #define SZ(x) (int)x.size() #define pb push_back using namespace std; const int N=5005; const ll mod=998244353; ll n,m; ll f[N][N],ff[N]; int main() { cin>>n>>m; for(ll i=0;i<=m;i++) f[i][0]=1; for(ll i=1;i<=m;i++) { for(ll j=1;j<=n;j++) { f[i][j]=(f[i][j-1]*i)%mod; } } ll ans=(n*f[m][n])%mod; for(ll i=0;i<=n;i++) { ll tmp=0; for(ll j=1;j<=m;j++) { tmp=(tmp+f[m-j][i])%mod; } ff[i]=tmp; } for(ll i=1;i<=n;i++) { for(ll j=1;j<i;j++) { ll tmp=ff[i-j-1]; tmp=(tmp*f[m][n-i+j-1])%mod; ans=(ans-tmp+mod)%mod; } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; using ull = unsigned long long; #define rep(i,n) for(int i=0;i<int(n);i++) #define rep1(i,n) for(int i=1;i<=int(n);i++) #define per(i,n) for(int i=int(n)-1;i>=0;i--) #define per1(i,n) for(int i=int(n);i>0;i--) #define all(c) c.begin(),c.end() #define si(x) int(x.size()) #define pb emplace_back #define fs first #define sc second template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;} template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;} template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());} template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();} template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){ return o<<"("<<p.fs<<","<<p.sc<<")"; } template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){ o<<"{"; for(const T& v:vc) o<<v<<","; o<<"}"; return o; } constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } #ifdef LOCAL #define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl void dmpr(ostream& os){os<<endl;} template<class T,class... Args> void dmpr(ostream&os,const T&t,const Args&... args){ os<<t<<" ~ "; dmpr(os,args...); } #define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__) #define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \ for(auto v: x) cerr << v << ","; cerr << "}" << endl; #else #define show(x) void(0) #define dump(x) void(0) #define shows(...) void(0) #endif template<class D> vector<D> dijkstra(const vector<vector<pair<int,D>>>& G, int s = 0){ //G:(v,cost) using P = pair<D,int>; priority_queue<P,vector<P>,greater<P> > que; int N = G.size(); D inf = 1e9; vector<D> d(N,inf); d[s] = 0; que.push(P(0,s)); while(!que.empty()){ P p=que.top(); que.pop(); int v = p.sc; D c = p.fs; if(d[v]!=c) continue; for(auto p:G[v]){ int to=p.fs; if(d[to]>d[v]+p.sc){ d[to]=d[v]+p.sc; que.push(P(d[to],to)); } } } return d; } int main(){ cin.tie(0); ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int H,W; cin >> H >> W; VV<int> A(H,V<int>(W-1)); rep(i,H) rep(j,W-1) cin >> A[i][j]; VV<int> B(H-1,V<int>(W)); rep(i,H-1) rep(j,W) cin >> B[i][j]; auto id = [&](int x,int y){return x*W+y;}; auto is = [&](int x,int y){return 0<=x&&x<H&&0<=y&&y<W;}; using P = pair<int,int>; int X = H*W * 2; VV<P> G(X); auto add_edge = [&](int x,int y,int c){ G[x].pb(y,c); }; rep(i,H) rep(j,W){ shows(i,j); int v = id(i,j); { int ni = i, nj = j-1; if(is(ni,nj)){ int nv = id(ni,nj); int c = A[i][j-1]; add_edge(v,nv,c); } } { int ni = i, nj = j+1; if(is(ni,nj)){ int nv = id(ni,nj); int c = A[i][j]; add_edge(v,nv,c); } } { int ni = i+1, nj = j; if(is(ni,nj)){ int nv = id(ni,nj); int c = B[i][j]; add_edge(v,nv,c); } } add_edge(v,v+H*W,1); add_edge(v+H*W,v,0); if(i){ add_edge(v+H*W,id(i-1,j)+H*W,1); } } cout << dijkstra(G,0)[H*W-1] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, Q; string S; cin >> N >> S >> Q; bool flag = true; for (int i = 0; i < Q; i++) { int T, A, B; cin >> T >> A >> B; if (T == 1) { if (!flag) { if (A > N) { A -= N; } else { A += N; } if (B > N) { B -= N; } else { B += N; } } swap(S.at(A - 1), S.at(B - 1)); } else { if (flag) { flag = false; } else { flag = true; } } } if (!flag) { for (int i = 0; i < N; i++) { cout << S.at(i + N); } for (int i = 0; i < N; i++) { cout << S.at(i); } cout << endl; } else { cout << S << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define input_output freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #define int long long #define all(ds) ds.begin(), ds.end() #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define gcd(x,y) __gcd(x,y) #define precise(x) cout<<fixed<<setprecision(x) #define endl "\n" #define pii pair<int,int> #define sz(x) ((int)(x).size()) #define lb lower_bound #define ub upper_bound #define pb push_back #define ff first #define ss second #define ms(x,a) memset(x,(int)(a),sizeof(x)) #define rep(i,a,b) for (int i=a; i<(b); i++) #define forn(i,a) for (int i=0; i<(a); i++) #define repd(i,a,b) for (int i = (b)-1; i >= a; i--) #define ford(i,a) for (int i = (a)-1; i >= 0; i--) template<class T> bool gmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool gmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template <typename Arg1> void prn(Arg1&& arg1) { cout<<arg1<<endl;} template <typename Arg1, typename... Args> void prn(Arg1&& arg1, Args&&... args) { cout<<arg1<<endl; prn(args...); } template <typename Arg1> void prsn(Arg1&& arg1) { cout<<arg1<<endl;} template <typename Arg1, typename... Args> void prsn(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prsn(args...); } template <typename Arg1> void prs(Arg1&& arg1) { cout<<arg1<<" ";} template <typename Arg1, typename... Args> void prs(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prs(args...); } template <typename Arg1> void read(Arg1&& arg1) { cin>>arg1; } template <typename Arg1, typename... Args> void read(Arg1&& arg1, Args&&... args) { cin>>arg1; read(args...); } const long long INF = 1e18; const int32_t MOD = 1e9+7; const int N = 2e5+5; void solve(){ int n, q; string s; read(n,s,q); bool parity = false; forn(i,q){ int type, x, y; read(type,x, y); if(type == 2){ parity = !parity; } else{ if(parity){ if(x <= n) x += n; else x -= n; if(y <= n) y += n; else y -= n; } swap(s[x-1], s[y-1]); } } if(parity) { s = s.substr(n, 2*n) + s.substr(0, n); } prn(s); } signed main() { #ifndef ONLINE_JUDGE input_output #else fastio #endif #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif int t = 1; // cin>>t; for(int tt = 1; tt <= t; ++tt){ // cout << "Case #" << tt << ": "; solve(); } return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; int main() { int n; cin >> n; vector<string> ans{"AB"}; FOR(i, 2, n + 1) { int per = 1 << i, m = ans.size(); vector<string> nx{""}; REP(_, per / 2) nx.back() += 'A'; REP(_, per / 2) nx.back() += 'B'; for (auto &s : ans) { nx.emplace_back(s + s); string t = s; for (char c : s) t += c == 'A' ? 'B' : 'A'; nx.emplace_back(t); } ans.swap(nx); } int k = ans.size(); assert(k == (1 << n) - 1); // vector a(1 << n, vector(1 << n, 0)), b(1 << n, vector(1 << n, 0)); // for (auto &s : ans) { // REP(i, 1 << n) REP(j, 1 << n) { // if (j != i) (s[i] == s[j] ? a[i][j] : b[i][j]) += 1; // } // } // int N = a[0][1], M = b[0][1]; // REP(i, n) FOR(j, i + 1, n) assert(a[i][j] == N && b[i][j] == M); cout << k << '\n'; for (auto &s : ans) cout << s << '\n'; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize ("Ofast") #pragma GCC optimization ("unroll-loops, no-stack-protector") #pragma GCC target ("avx") #pragma GCC target ("avx2") #pragma GCC target ("fma") #define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define ll long long #define ull unsigned long long #define ld long double #define pii pair <int, int> #define pll pair <ll, ll> #define pci pair <char, int> #define pld pair <ld, ld> #define ppld pair <pld, pld> #define ppll pair <pll, pll> #define pldl pair <ld, ll> #define vll vector <ll> #define vvll vector <vll> #define vpll vector <pll> #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define mll map <ll, ll> #define fastmap gp_hash_table #define cd complex <double> #define vcd vector <cd> #define PI 3.14159265358979 #define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update> #pragma 03 using namespace std; using namespace __gnu_pbds; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); void solve(){ string s; cin >> s; if (s > "atcoder"){ cout << 0 << "\n"; return; } ll n = s.length(); for (ll i = 0; i < n; i++){ if (s[i] == 'a') continue; else{ if (i == 1){ cout << 1 << "\n"; return; } else{ if (s[i] > 't'){ cout << i - 1 << "\n"; } else cout << i << "\n"; return; } } } cout << -1 << "\n"; return; } int main(){ fastio; ll t; cin >> t; while (t--) solve(); }
#include<bits/stdc++.h> using namespace std::chrono; using namespace std; #define int long long #define loop(i, start, end) for (int i = start; i <= end; i++) #define rloop(i, start, end) for (int i = start; i >= end; i--) #define read(a,n) loop(i,0,n-1) cin>>a[i]; #define vi vector<int> #define vec(x) vector<x> #define sz(v) (int)v.size() #define UB upper_bound #define LB lower_bound #define all(v) (v).begin(),(v).end() #define arl(v) (v).rbegin(),(v).rend() #define fsp(a) fixed<<setprecision(a) #define mem(a,with) memset(a, with, sizeof (a)) #define vmn(a) (*min_element(a.begin(),a.end())) #define vmx(a) (*max_element(a.begin(),a.end())) #define bs(a,key) binary_search(all(a),key) /// return bool. #define rotl(a,x) rotate(a.begin(),a.begin()+x,a.end()); #define rotr(a,x) rotl(a,a.size()-x); #define nl cout<<endl #define dbg(x) cerr << #x << " :: " << x << endl; #define dbg2(x, y) cerr << #x << " :: " << x << "\t" << #y << " :: " << y << endl; #define MOD 1000000007 int add(int x, int y) {int res = x + y; return (res >= MOD ? res - MOD : res);} int mul(int x, int y) {int res = x * y; return (res >= MOD ? res % MOD : res);} int power(int x, int y) {int res = 1; x %= MOD; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} void solve(){ //code goes here int N; cin >> N; string S; cin >> S; stack<char> st; int res = N; for (const auto &s : S) { if (s == 'f') { st.push(s); } else if (s == 'o') { st.push(s); } else if (s == 'x') { if (!st.empty() && st.top() == 'o') { st.pop(); if (!st.empty() && st.top() == 'f') { st.pop(); res -= 3; } else { st.push('a'); } } else { st.push('a'); } } else { st.push('a'); } } cout<<res; } int32_t main() { auto start = chrono::high_resolution_clock::now(); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL); int t=1; // cin>>t; while (t--) { solve(); } #ifndef ONLINE_JUDGE auto stop = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<microseconds>(stop - start); cout<<"\n\nExecuted In: "<<duration.count()<<" ms"; #endif return 0; }
#pragma GCC optimize("Ofast", "unroll-loops") #pragma GCC target("avx2") #include <bits/stdc++.h> using namespace std; #define ll long long int N; string s; void input(void){ cin >> N >> s; } int solve(void){ static char buf[200005]; int ptr = 0; for (int i = 0; i < N; ++i){ if ( ptr >= 2 && buf[ptr - 2] == 'f' && buf[ptr - 1] == 'o' && s[i] == 'x' ){ ptr -= 2; } else buf[ptr++] = s[i]; } return ptr; } int main(void){ input(); cout << solve() << endl; return 0; }
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <tuple> #include <numeric> #include <functional> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for(ll i = 0; i < n; i++) #define exrep(i, a, b) for(ll i = a; i <= b; i++) #define out(x) cout << x << endl #define exout(x) printf("%.10f\n", x) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define re0 return 0 const ll mod = 1000000007; const ll INF = 1e10; int main() { ll n; cin >> n; vvl v(n, vl(5)); rep(i, n) { rep(j, 5) { cin >> v[i][j]; } } // 判定問題「チームの総合力をx以上にできるか?」を二分探索で解く ll left = 0; ll right = INF; /*・INFの値が大きすぎてオーバーフローしていないか? ・INFの値が制約に対して小さすぎないか? */ while(right - left > 1) { ll x = (left + right) / 2; set<ll> st; rep(i, n) { ll y = 0; rep(j, 5) { if(v[i][j] >= x) { y |= 1<<j; } } st.insert(y); } vl w; for(ll y : st) { w.pb(y); } bool ok = false; rep(i, (ll)w.size()) { rep(j, i+1) { rep(k, j+1) { if((w[i] | w[j] | w[k]) == (1<<5) - 1) { ok = true; } } } } if(ok) { left = x; } else { right = x; } } out(left); re0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main(){ int N; cin >> N; vector<vector<int>> A(5, vector<int>(N)); for(int i = 0 ; i < N; i++) { for(int j = 0; j < 5; j++){ int a; cin >> a; A[j][i] = a; } } int ub = 1000000001, lb = 1; while(ub - lb > 1){ int mid = (lb + ub) / 2; vector<int> temp(N); for(int i = 0; i < 5; i++){ for(int j = 0; j < N; j++){ if(A[i][j] >= mid) temp[j] |= (1 << i); } } vector<int> ten(1 << 5, 10000000); ten[0] = 0; for(int j = 0; j < (1 << 5); j++){ for(int k = 0; k < N; k++){ ten[j | temp[k]] = min(ten[j | temp[k]], ten[j] + 1); } } if(ten[(1 << 5) - 1] <= 3) lb = mid; else ub = mid; } cout << lb << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; string s,d; cin>>n>>s; for (char c : s) { d += c; if (d.size() >= 3 && d.substr(d.size()-3) == "fox") { d.erase(d.begin()+d.size()-3, d.end()); } } cout<<d.size()<<endl; return 0; }
#include<bits/stdc++.h> #define pb push_back #define f first #define s second #define pi acos(-1.0) #define mod 1000000007 #define test int t; cin>>t ;while(t--) typedef long long ll; using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); stack<char>st; int n; string s; cin>>n; char a; st.push('*'); st.push('*'); st.push('*'); for(int i=1;i<=n;i++) { cin>>a; if(a=='x') { char p,q; p=st.top(); st.pop(); q=st.top(); st.pop(); if(p=='o'&&q=='f') { continue; } else { st.push(q); st.push(p); st.push(a); } } else st.push(a); } cout<<st.size()-3<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct compare { inline bool operator()(const std::string& first, const std::string& second) const { return first.size() < second.size(); } }; int main() { int n, m; cin>>n>>m; string ins[m]; for (int i=0; i<m; i++) cin>>ins[i]; vector<string> s; for (int i=0; i<m; i++) { bool contains = false; for (int j=0; j<m; j++) { if (i == j) { continue; } if (ins[j].find(ins[i]) != -1) { contains = true; break; } } if (!contains) { s.push_back(ins[i]); } } compare c; sort(s.begin(), s.end(), c); reverse(s.begin(), s.end()); vector<string> result[20]; vector<string> suffix[20]; int len[20]; for (int i=0; i<20; i++) len[i] = 0; int i = 0; for (string si: s) { for (int i=0; i<20; i++) { bool match_suffix = false; for (string i_suffix : suffix[i]) { if (len[i] + si.length() - i_suffix.length() > 20) { continue; } if (i_suffix == si.substr(0, i_suffix.length())) { match_suffix = true; result[i].push_back(si.substr(i_suffix.length())); len[i] += si.length() - i_suffix.length(); break; } } if (!match_suffix) { if (len[i] + si.length() > 20) { continue; } result[i].push_back(si); len[i] += si.length(); } suffix[i].clear(); for (int j=0; j<si.length()-1; j++) { suffix[i].push_back(si.substr(j+1)); } break; } } for (int i=0; i<20; i++) { for (string r : result[i]) { cout<<r; } for (int j=len[i]; j<20; j++) { cout<<"A"; } cout<<endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int h,w,count=0; cin>>h>>w; vector<string>s(h); for(int i=0;i<h;i++)cin>>s.at(i); for(int i=0;i<h;i++){ for(int j=0;j<w-1;j++){ if(s.at(i).at(j)=='.'&&s.at(i).at(j+1)=='.'){ count++; } } } for(int i=0;i<h-1;i++){ for(int j=0;j<w;j++){ if(s.at(i).at(j)=='.'&&s.at(i+1).at(j)=='.'){ count++; } } } cout<<count; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define MOD 1'000'000'007 #define ll long long int #define ull unsigned ll #define vt vector #define uset unordered_set #define umap unordered_map #define mset multiset #define umset unordered_multiset #define in insert #define pb push_back #define ff first #define ss second #define F(i, end) LF(i, 0, end) #define LF(i, beg, end) for (ll i = beg; i < end; ++i) #define LR(i, end, beg) for (ll i = end; i >= beg; --i) #define HAS(col, e) (col.find(e) != col.end()) #define outcol(col, sep) for (auto e: col) cout << e << sep void solve() { ull n; cin >> n; ll dig[3] = {0}; ll digCount = 0; ll temp = n; while (temp) { ++digCount; ++dig[(temp % 10) % 3]; temp /= 10; } // cout << dig[0] << ' ' << dig[1] << ' ' << dig[2] << endl; if (abs(dig[1] - dig[2]) == digCount) cout << -1 << endl; else { if (dig[1] % 3 == 2 && dig[2] % 3 == 0) { if (dig[2] > 0) cout << 1 << endl; else cout << 2 << endl; } else if (dig[1] % 3 == 0 && dig[2] % 3 == 2) { if (dig[1] > 0) cout << 1 << endl; else cout << 2 << endl; } else cout << abs((dig[1] % 3) - (dig[2] % 3)) << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll c = 1; // cin >> c; F(i, c) { // cout << "Case #" << i + 1 << ": "; solve(); } return 0; }
#ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wunused-result" #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif #include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <sstream> #include <numeric> #include <map> #include <set> #include <queue> #include <vector> using namespace std; typedef long long LL; typedef long double LD; typedef pair<LL, LL> II; typedef pair<LL, II> III; typedef priority_queue<III, vector<III>, greater<>> Queue; static const LL INF = 1LL << 60; LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; } #ifdef __GNUC__ #define popcount __builtin_popcount #elif _MSC_VER >= 1400 #include <intrin.h> #define popcount __popcnt #else static inline int popcount(unsigned int b) { b = (b & 0x55555555) + (b >> 1 & 0x55555555); b = (b & 0x33333333) + (b >> 2 & 0x33333333); b = (b & 0x0F0F0F0F) + (b >> 4 & 0x0F0F0F0F); b += b >> 8; return (b + (b >> 16)) & 0x3F; } #endif void solve(string N) { const int INF = 1 << 30; int ans = INF; int len = N.length(); int bm = 1 << len; for (int b = 1; b < bm; ++b) { int sum = 0; for (int i = 0; i < len; ++i) { if (b & (1 << i)) { sum += N[i] - '0'; } } if ((sum % 3) == 0) { ans = min(ans, (int)(len - popcount(b))); } } if (ans == INF) ans = -1; cout << ans << endl; } int main() { string N; std::cin >> N; solve(N); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; #define mp make_pair #define fr first #define sc second int lg(ll n) { assert(n > 0); int ans = -1; while (n) { ans++; n >>= 1; } return ans; } template <int mod_> struct modint { static const int mod = mod_; int val; modint() { val = 0; } modint(const ll& v) { val = (-mod <= v && v < mod) ? v : v % mod; if (val < 0) val += mod; } friend ostream& operator<<(ostream& os, const modint& a) { return os << a.val; } friend bool operator==(const modint& a, const modint& b) { return a.val == b.val; } friend bool operator!=(const modint& a, const modint& b) { return !(a == b); } friend bool operator<(const modint& a, const modint& b) { return a.val < b.val; } modint operator-() const { return modint(-val); } modint& operator+=(const modint& m) { if ((val += m.val) >= mod) val -= mod; return *this; } modint& operator-=(const modint& m) { if ((val -= m.val) < 0) val += mod; return *this; } modint& operator*=(const modint& m) { val = (ll)val * m.val % mod; return *this; } modint qpow(ll p) { modint ans = 1, base = val; for (; p; p /= 2, base *= base) if (p & 1) ans *= base; return ans; } modint inv() { assert(val); return qpow((ll)mod - 2); } modint& operator/=(modint m) { return (*this) *= m.inv(); } friend modint operator+(modint a, const modint& b) { return a += b; } friend modint operator-(modint a, const modint& b) { return a -= b; } friend modint operator*(modint a, const modint& b) { return a *= b; } friend modint operator/(modint a, const modint& b) { return a /= b; } operator ll() const { return val; } }; using mint = modint<998244353>; const int MX_sieve = 2e5; int par[MX_sieve + 5]; vector<int> pri; // par is the lowest prime factor int pricnt = (int)pri.size(); map<int, int> primeidx; void sieve() { for (int i = 1; i <= MX_sieve; i++) { if (i == 1) continue; else { if (!par[i]) { pri.push_back(i); par[i] = i; } for (int x : pri) { if (x > MX_sieve / i) break; par[x * i] = x; if (x == par[i]) { break; } } } } int pricnt = (int)pri.size(); /* for (int i = 0; i < pricnt; i++) { primeidx[pri[i]] = i; } */ } // need mint const int MX_fact = 2e5 + 500; mint fact[MX_fact + 5], invfact[MX_fact + 5]; void prepare_fact() { fact[0] = 1; invfact[0] = 1; for (int i = 0; i < MX_fact; i++) { fact[i + 1] = fact[i] * mint(i + 1); invfact[i + 1] = fact[i + 1].inv(); } } mint C(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * invfact[k] * invfact[n - k]; } mint H(int n, int k) { return C(n + k - 1, n - 1); } void solve() { int N, M; cin >> N >> M; mint ans = 0; for (int i = 1; i <= M; i++) { mint comb = 1; int reg = i; while (reg > 1) { int p = par[reg], pw = 0; while (par[reg] == p) { pw++; reg /= p; } comb *= C(N - 1 + pw, pw); } ans += comb; } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif sieve(); prepare_fact(); int t = 1; // cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main(){ // 初期値 // nが階数、kが1階ごとの部屋数 int n, k; int count=0; // 入力 cin >> n >> k; // 処理 for(int i=1; i<=n; i++){ for(int j=1; j<=k; j++){ count += (100*i) + (1*j); } } // 出力 cout << count << endl; }
#include <bits/stdc++.h> using namespace std; #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define ll long long #define ull unsigned long long #define pi 2*acos(0.0) #define readl(v,n) for(ll i=0;i<n;i++) {ll val; cin>>val; v.pb(val);} #define readi(v,n) for(int i=0;i<n;i++) {int val; cin>>val; v.pb(val);} #define srt(v) sort(v.begin(), v.end()); #define rsrt(v) sort(v.rbegin(), v.rend()); #define MIN(v) *min_element(v.begin(), v.end()) #define MAX(v) *max_element(v.begin(), v.end()) #define sz(x) ((ll) (x).size()) #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for(ll i = (a); i < (b); i++) #define rep2(i, a, b) for(ll i = (a); i <= (b); i++) #define vll vector <ll> #define vii vector <int> #define pii pair <int, int> #define pll pair <ll, ll> #define M 1000007 #define MOD 1000000007 #define pb push_back #define mp make_pair #define ff first #define ss second #define endl "\n" #define inf 1000000000000000000 #define ninf -1000000000000000000 #define deb(n) cout<<"--"<<(n)<<"--"<<endl #define debx(x) cout << #x << " = " << (x) << endl #define deb2(a,b) cout<<(a)<<"----"<<(b)<<endl #define debv(v) for(auto x: v) {cout<<x<<", ";} cout<<endl; #define put(n) cout<<(n)<<endl #define toDecimal(s) bitset<64>(s).to_ullong() #define toBinaryStr(n) bitset<64>(n).to_string() #define toBinary(n) stoll(bitset<64>(n).to_string()) /* ---- take notes here ---- */ void solve() { ll a,b,c; cin>>a>>b>>c; if(a*a+b*b<c*c) { cout<<"Yes"<<endl; } else { cout<<"No"<<endl; } } int main() { int t = 1; //cin >> t; while(t--) { solve(); } return 0; }
#include <iostream> #include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; // #define ONLINE_JUDGE #ifndef ONLINE_JUDGE template<typename T> void __p(T a) { cout << a; } template<typename T, typename F> void __p(pair<T, F> a) { cout << "{"; __p(a.first); cout << ","; __p(a.second); cout << "}"; } template<typename T> void __p(std::vector<T> a) { cout << "{"; for (auto it = a.begin(); it < a.end(); it++) __p(*it), cout << ",}"[it + 1 == a.end()]; } template<typename T, typename ...Arg> void __p(T a1, Arg ...a) { __p(a1); __p(a...); } template<typename Arg1> void __f(const char *name, Arg1 &&arg1) { cout << name << " : "; __p(arg1); cout << endl; } template<typename Arg1, typename ... Args> void __f(const char *names, Arg1 &&arg1, Args &&... args) { int bracket = 0, i = 0; for (;; i++) if (names[i] == ',' && bracket == 0) break; else if (names[i] == '(') bracket++; else if (names[i] == ')') bracket--; const char *comma = names + i; cout.write(names, comma - names) << " : "; __p(arg1); cout << " | "; __f(comma + 1, args...); } #define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__); #else #define trace(...) #endif typedef long long int ll; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename T> using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; #define pb(x) push_back(x) #define mp(x,y) make_pair(x,y) typedef pair<ll , ll> pp; typedef pair<pair<ll, ll>, ll> ppp; #define X first #define Y second #define getones(n) __builtin_popcountll(n) #define debug(x) cout<<#x<<" :: "<<x<<"\n"; #define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"; #define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"; #define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define frv(i,a,b) for (ll i = (a), _b = (b); i < _b; i++) #define frrv(i,a,b) for (ll i = (a), _b = (b); i >= _b; i--) #define fr(i,n) for (ll i = 0, _n = (n); i < _n; i++) #define frr(i,n) for (ll i = n - 1; i >= 0; i--) #define fordata(it,ar) for (auto it = ar.begin(); it != ar.end(); it++) #define fill(ar,val) fr(i,sizeof(ar)/sizeof(ll)) ar[i]=val #define fill2(ar,val) fr(i,sizeof(ar)/sizeof(ar[0])) fr(j,sizeof(ar[0])/sizeof(ll)) ar[i][j]=val #define fill0(ar) memset(ar,0,sizeof(ar)) #define sz(x) (int)x.size() #define len(x) (int)x.length() #define all(ar) ar.begin(), ar.end() #define INF 1000000000000000000 #define PI 3.14159265358979323846L #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) const ll N = 1e6 + 1; const ll K = 1e9 + 7; #define vv vector<ll> const ll globalmod = 1e9 + 9; int main() { ll a, b, c; cin >> a >> b >> c; cout << (a * a + b * b < c * c ? "Yes" : "No"); }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; int k = n/100; if(n%100 != 0) k++; cout << k; return 0; }
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; n %= 100; cout << 100 - n; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define rep(i,j,n) for(ll i=j;i<n;i++) #define scn(a) scanf("%lld",&a) #define nl printf("\n") #define scns(a,b) scanf("%lld %lld",&a,&b) #define print(a) printf("%lld",a) #define tc ll t; scn(t); while(t--) #define vec vector<ll> #define pb push_back #define pairs pair<ll,ll> #define fi first #define se second #define all(v) v.begin(),v.end() #define srt(v) sort(v.rbegin(),v.rend()) #define mem(a,b) memset(a,b,sizeof(a)) #define maxx 5000005 #define inf LLONG_MAX #define zer LLONG_MIN #define dbug(a) cout<<"dbug : "<<a<<endl #define debug(a,b) cout<<a<<" "<<b<<endl #define read(v) vec v(n,0); rep(i,0,n) {cin>>v[i];} #define write(a,n) rep(i,0,n) {cout<<a[i]<<" ";} #define All rep(i,0,n) const ll N = 4000; const ll mod = 1000000000+7; ll zero = 0; //1 - uska vote //2 - mera vote // bool comp(pair<ll,ll> p1, pair<ll,ll> p2) { // if(p1.fi > p2.fi) return true; // if(p1.fi == p2.fi) { // if(v[p1.se].fi >= v[p2.se].fi) return true; // else return false; // } // return false; // } void solve() { ll n; cin >> n; vector<pair<ll,ll>> v(n); vector<pair<ll,ll>> total(n); ll totaluska = 0; rep(i,0,n) { cin >> v[i].fi; cin >> v[i].se; totaluska += v[i].fi; } rep(i,0,n) { ll tot = v[i].fi + v[i].se + v[i].fi; total[i] = {tot,i}; } //sort(total.begin(), total.end() srt(total); ll mera = 0; ll uska = totaluska; ll i = 0; //cout << totaluska << endl; while(mera <= uska) { mera += total[i].fi - v[total[i].se].fi; uska -= v[total[i].se].fi; i++; } //cout << "mera " << mera << " uska " << uska << endl; cout << i; return; } //Check for test case int main(){ int test=1; //cin>>test; while(test--) { solve(); } return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; int32_t main() { int n; cin>>n; pair<int,int> a[n]; for(int i=0;i<n;i++){ cin>>a[i].first>>a[i].second; } int x=0,y=0; // x-> aoki's votes and y-> takahashi's votes for(int i=0;i<n;i++){ x+=a[i].first; } for(int i=0;i<n;i++){ int temp = a[i].first; a[i].first = 2*a[i].first+a[i].second; a[i].second = temp;//{2a+b,a} } sort(a,a+n); int ans=0; int i=n-1; while(x>=y){ ans++; y+=a[i].first-a[i].second; x-=a[i].second; i--; } cout<<ans; /* a[i].first=a,a[i].second=b; 2*a+b */ }
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> using namespace std; int n; int s[200005]; vector<int>v[200005]; int ans=0; int main() { ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n; for(int i=1;i<=n;++i) { cin>>s[i]; v[s[i]].push_back(i); } for(int i=1;i<=n/2;++i) { if(s[i]!=s[n+1-i]) { int x=s[i],y=s[n+1-i]; ++ans; if(v[x].size()<=v[y].size()) { for(int j=0;j<v[x].size();++j) { int z=v[x][j]; s[z]=s[n+1-i]; v[y].push_back(z); } } else { for(int j=0;j<v[y].size();++j) { int z=v[y][j]; s[z]=s[i]; v[x].push_back(z); } } } } cout<<ans; return 0; }
#include <bits/stdc++.h> #define ll long long #define sz(x) ((int) (x).size()) #define all(x) (x).begin(), (x).end() #define vi vector<int> #define vl vector<long long> #define pii pair<int, int> #define REP(i,a) for (int i = 0; i < (a); i++) #define add push_back using namespace std; template<typename T> using minpq = priority_queue<T, vector<T>, greater<T>>; const ll MOD = 1000000007LL; //const ll MOD = 998244353LL; int ni() { int x; cin >> x; return x; } ll nl() { ll x; cin >> x; return x; } double nd() { double x; cin >> x; return x; } string next() { string x; cin >> x; return x; } struct DisjointSetUnion { vi parent; vi weight; int count; //number of components DisjointSetUnion(int N) { count = N; parent.assign(N,0); REP(i,N) parent[i] = i; weight.assign(N,1); } //"find" int root(int p) { if (p==parent[p]) return p; int r = root(parent[p]); parent[p] = r; return r; } //"union" void connect(int p, int q) { p = root(p); q = root(q); if (p==q) return; if (weight[p]<weight[q]) { parent[p] = q; weight[q] += weight[p]; } else { parent[q] = p; weight[p] += weight[q]; } count--; } bool connected(int p, int q) { return root(p)==root(q); } }; int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif ios::sync_with_stdio(false); cin.tie(0); int N = ni(); vi nums(N); REP(i,N) { nums[i] = ni()-1; } DisjointSetUnion dsu(200000); for (int i = 0; i < N; i++) dsu.connect(nums[i],nums[N-1-i]); cout << 200000-dsu.count; }
#include <algorithm> #include <array> #include <cassert> #include <chrono> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <vector> #include <stack> using namespace std; #define enviar {ios_base::sync_with_stdio(false);} #define aiuda {cin.tie(NULL); cout.tie(NULL);} #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 EACH(x, a) for (auto& x: a) #define exacto(v) cout << setprecision(v) << fixed; #define ll long long #define lld long double #define eb emplace_back #define em emplace #define ef emplace_front #define f first #define s second #define big greater<ll>() #define bpc(x) __builtin_popcount(x) #define bpc2(x) __builtin_clz(x) #define bpc3(x) __builtin_ctz(x) #define DIE(args...) {print(args); return;} #define lb lower_bound #define ub upper_bound #define ar(v) array<int,v> #define all(v) v.begin(), v.end() #define sz(x) (int)(x).size() #define meminf(v) memset(v, 0x3f , sizeof(v)) #define mem(v,x) memset(v , x , sizeof v); #define ai(a, n) for (int ele = 0; ele < n; ele++) cin >> a[ele]; #define ao(a, n) {for (int ele = 0; ele < (n); ele++) { if (ele) cout << " "; cout << a[ele]; } cout << '\n';} #define readgraph(list, edges) for (int i = 0; i < edges; i++) {int n1, n2; cin >> n1 >> n2; n1--; n2--; list[n1].eb(n2); list[n2].eb(n1);} template <typename T> struct combinator { T t_; combinator(T&& t) : t_(std::forward<T>(t)) {} template <typename... Args> auto operator () (Args&&... args) const { return t_(*this, std::forward<Args>(args)...); } }; template<class T> bool umin(T& a, const T& b) { return b<a?a=b, 1:0; } template<class T> bool umax(T& a, const T& b) { return a<b?a=b, 1:0; } string to_string(bool b) {return b?"true":"false"; } string to_string(char c) { return string(1, c);} string to_string(const char* s) { return string(s); } string to_string(string s) { return s; } template<class T> string to_string(T v) { bool f=1; string res; EACH(x, v) { if(!f) res+=' '; f=0; res+=to_string(x); } return res; } template<class A> void write(A x) { cout << to_string(x); } void print() { write("\n"); } template<class H, class... T> void print(const H& h, const T&... t) { write(h); if(sizeof...(t)) write(' '); print(t...); } typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pair<ll, ll>> vpll; typedef map<ll, ll> mll; typedef priority_queue<int, std::vector<int>, std::greater<int>> priomin; const ll mod = 998244353; //const ll mod = 1e9 + 7; const ll maxsize =3e5 + 5; const ll inf = 0x3f3f3f3f3f3f3f3f ; // size, caracter ll a[maxsize]; //vll adj[maxsize]; //ll l[maxsize]; ll l[maxsize][2]; ll dp[maxsize][2]; void solve() { ll n; cin >> n; ai(a, n); FOR(i , n) { if (i%2) a[i]*=-1; } partial_sum(a , a+ n , a); mll cnt; ll ans = 0; cnt[0]++; FOR(i , n) { ans += cnt[a[i]]++; } print(ans); } int main(){ enviar aiuda int tc = 1; //cin >> tc; while(tc--) solve(); return 0; }
// Problem: C - ARC Wrecker 2 // Contest: AtCoder - AtCoder Regular Contest 119 // URL: https://atcoder.jp/contests/arc119/tasks/arc119_c // Memory Limit: 1024 MB // Time Limit: 2000 ms #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 3e5 + 10; int p[N]; ll sum[N]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> p[i]; sum[i] = sum[i - 1] + p[i] * (i % 2 == 0 ? 1 : -1); } map<ll, int> cnt; cnt[0] = 1; ll ans = 0; for (int i = 1; i <= n; ++i) { ans += cnt[sum[i]]; ++cnt[sum[i]]; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define ld long double #define ll long long #define FF first.first #define FS first.second #define SF second.first #define SS second.second #define PB push_back #define MP make_pair #define all(cont) cont.begin(),cont.end() #define rall(cont) cont.rbegin(), cont.rend() #define FOR(i, j) for(int i=0;i<j;i++) #define RFOR(i, j) for (int i=j;i>=0;i--) #define GO cin.tie(NULL); #define FAST ios_base::sync_with_stdio(false); typedef pair<int,int> pii; // Your function //DEBBUGGING STUFF, JUST USE deb(a,b,c) and it will print the variables; #define deb(...) logger(#__VA_ARGS__, __VA_ARGS__) template<typename ...Args> void logger(string vars, Args&&... values) { cout << vars << " = "; string delim = ""; (..., (cout << delim << values, delim = ", ")); cout<<endl; } const int maxn=1e5+10; int main(){ GO FAST; int n;cin>>n; bitset<maxn> possiveis(0); possiveis.set(0); int soma=0; FOR(i,n){ int a; cin>>a; soma+=a; auto cop=possiveis; cop<<=a; possiveis|=cop; } int ans=soma; FOR(i,maxn){ if (possiveis[i]){ ans=min(max(i,soma-i),ans); } } cout<<ans<<'\n'; }
#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include<stdio.h> #include<algorithm> constexpr long long MOD = 998244353; constexpr long long MOD32 = 665496236; char f[5000][5000]; long long dp[5000]; int main(){ long H=0, W=0, K=0; char c=getchar_unlocked(); while(c<='9'&&c>='0') {H=(H<<1)+(H<<3)+c-'0'; c=getchar_unlocked();} c=getchar_unlocked(); while(c<='9'&&c>='0') {W=(W<<1)+(W<<3)+c-'0'; c=getchar_unlocked();} c=getchar_unlocked(); while(c<='9'&&c>='0') {K=(K<<1)+(K<<3)+c-'0'; c=getchar_unlocked();} long long y = H * W - K; while(--K>=0){ long h=0, w=0; c=getchar_unlocked(); while(c<='9'&&c>='0') {h=(h<<1)+(h<<3)+c-'0'; c=getchar_unlocked();} c=getchar_unlocked(); while(c<='9'&&c>='0') {w=(w<<1)+(w<<3)+c-'0'; c=getchar_unlocked();} f[h-1][w-1] = getchar_unlocked(); getchar_unlocked(); } --H; --W; dp[0] = 1; for(int i = 0; i < H; ++i){ for (int j = 0; j < W; ++j){ switch(f[i][j]) { case 'R': dp[j + 1] += dp[j]; dp[j] = 0; break; case '\0': dp[j] = dp[j] * MOD32 % MOD; case 'X': dp[j + 1] += dp[j]; } } switch(f[i][W]) { case 'R': dp[W] = 0; break; case '\0': dp[W] = dp[W] * MOD32 % MOD; } } for (int j = 0; j < W; ++j){ switch(f[H][j]) { case '\0': dp[j] = dp[j] * MOD32 % MOD; case 'R': case 'X': dp[j + 1] += dp[j]; } } long long ans = dp[W] % MOD; long long x = 3; while(y) { if(y & 1) ans = ans * x % MOD; x = x * x % MOD; y >>= 1; } printf("%d", ans); return 0; }
#include <iostream> // cout, endl, cin #include <cmath> //sqrt pow #include <string> // string, to_string, stoi #include <vector> // vector #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <utility> // pair, make_pair #include <tuple> // tuple, make_tuple #include <cstdint> // int64_t, int*_t #include <cstdio> // printf #include <map> // map #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <deque> // dequef #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset #include <cctype> // isupper, islower, isdigit, toupper, tolower #include <climits> #define rep(i, n) for (int i = 0; i < n; i++) using ll = long long; using ld = long double; #define vi vector<int> #define vvi vector<vi> #define vl vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define all(a) (a).begin(), (a).end() #define mod 1000000007 using namespace std; ll gcd(ll a, ll b){ if(a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b){ return a / gcd(a, b) * b; } int main(){ int n; cin >> n; ll ans = 1; for(int i = 2; i <= n; i++) ans = lcm(ans, i); cout << ans + 1 << endl; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); //a long long lcm = 1; int n; cin >> n; for (long long i = 2; i <= n; i++) { long long g = __gcd(lcm, i); lcm = lcm * i / g; } lcm++; assert(lcm <= 1e13); for (int i = 2; i <= n; i++) { cerr << "i = " << i << " rem = " << lcm % i << endl; assert((lcm % i) == 1); } cout << lcm << endl; return 0; }
#include <iostream> #include <vector> #include<iomanip> #include<functional> #include<algorithm> #include<deque> #include<set> #include<queue> #include<map> using namespace std; #define rep(i,n) for(int i = 0;i<n;i++) #define req(i,n) for(int i = 1;i<=n;i++) #define rrep(i,n) for(int i = n-1;i>=0;i--) #define ALL(a) a.begin(),a.end() typedef long long ll; typedef double d; typedef pair<int, int> P; const int inf = 1 << 30; struct liner { d a, b; liner(d a = 0, d b = 0): a(a), b(b) {} liner operator+(const liner& x)const { return liner(a + x.a, b + x.b); }liner operator-(const liner& x)const { return liner(a - x.a, b - x.b); }liner operator/(int x)const { return liner(a / x, b / x); } }; int main() { int n, m, k,p,sum=0; cin >> n >> m >> k; vector<bool> f(n + 1, 0); rep(i, k) { cin >> p; f[p] = 1; }rep(i, n + 1) { if (f[i])sum++; else sum = 0; if (sum >= m) { cout << -1 << endl; return 0; } }vector<liner> dp(n + 1); liner S; rrep(i, n) { if (f[i]) dp[i] = liner(1, 0); else dp[i] = S / m + liner(0, 1); S = S+dp[i]; if (i + m <= n) S = S - dp[i + m]; }cout <<fixed<<setprecision(10) << dp[0].b / (1 - dp[0].a) << endl; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define u64 long long #define rint register int #define co(x) cout << (x) << endl #define cosp(x) cout << (x) << ' ' #define ce(x) cerr << (x) << endl #define cesp(x) cerr << (x) << ' ' using namespace std; //#define getchar() (p_1==p_2&&(p_2=(p_1=buf)+fread(buf,1,1<<21,stdin),p_1==p_2)?EOF:*p_1++) namespace Fast_Sec { namespace Calculation { inline u64 max_(u64 a, u64 b) { return a > b ? a : b; } inline u64 min_(u64 a, u64 b) { return a < b ? a : b; } inline void swap_(int& a, int& b) { a ^= b; b ^= a; a ^= b; } inline void swap_(u64& a, u64& b) { a ^= b; b ^= a; a ^= b; } inline u64 abs_(u64 a) { return a < 0 ? -a : a; } }; namespace IO { char buf[1<<23],*p_1=buf,*p_2=buf,obuf[1<<23],*O_=obuf; struct Fread{ } fin, fout; int f_; char ch_, Endl = '\n'; inline Fread& operator >>(Fread& F, int &a) { f_ = 1; a = 0; ch_ = getchar(); while (ch_ < '0' || ch_ > '9') { if (ch_ == '-') f_ = -1; ch_ = getchar(); } while (ch_ >= '0' && ch_ <= '9') { a = (a << 3) + (a << 1) + (ch_ ^ 48); ch_ = getchar(); } a *= f_; return F; } inline Fread& operator >>(Fread& F, u64 &a) { f_ = 1; a = 0; ch_ = getchar(); while (ch_ < '0' || ch_ > '9') { if (ch_ == '-') f_ = -1; ch_ = getchar(); } while (ch_ >= '0' && ch_ <= '9') { a = (a << 3) + (a << 1) + (ch_ ^ 48); ch_ = getchar(); } a *= f_; return F; } Fread& operator <<(Fread& F, const int& a) { if (a < 0) { *O_++ = '-'; return F << -a; } if (a > 9) F << (a / 10); *O_++ = a % 10 + '0'; return F; } Fread& operator <<(Fread& F, const u64& a) { if (a < 0) { *O_++ = '-'; return F << -a; } if (a > 9) F << (a / 10); *O_++ = a % 10 + '0'; return F; } Fread& operator <<(Fread& F, const char& c) { *O_++ = c; return F; } }; namespace Random { unsigned int _sd_ = 233; inline unsigned int rd() { return _sd_ ^= _sd_ >> 13, _sd_ ^= _sd_ << 17, _sd_ ^= _sd_ >> 5; } }; }; using namespace Fast_Sec::IO; using namespace Fast_Sec::Random; using namespace Fast_Sec::Calculation; int n, a, x; string s; int main() { //freopen(".in", "r", stdin); //freopen(".out", "w", stdout); fin >> n >> x; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'o') x++; else if (x > 0) x--; } fout << x; fwrite(obuf, O_ - obuf, 1, stdout); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; typedef pair<double, double> pdd; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<int, int> pii; typedef vector<pii> vii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; //const int mod = ; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; cin >> n; vl a(n), b(n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; vi ts(n); ll sum = 0; for (int i = 0; i < n; ++i) { ll add = i % 2 ? a[i] : b[i]; sum += add; ts[i] = a[i] + b[i] - 2 * add; } sort(ts.rbegin(), ts.rend()); for (int i = 0; i < n / 2; ++i) sum += ts[i]; cout << sum << '\n'; return 0; }
#include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef pair<int,int>P; const int MOD=1000000007; const int INF=0x3f3f3f3f; const ll INFL=0x3f3f3f3f3f3f3f3f; ll a[200000],b[200000]; int main(){ int n;cin>>n; rep(i,n)scanf("%lld",&a[i]); rep(i,n)scanf("%lld",&b[i]); vector<ll>u,v; rep(i,n){ if(i%2==0)v.push_back(b[i]-a[i]); else u.push_back(b[i]-a[i]); } ll sum=0; rep(i,n)sum+=a[i]; sort(v.begin(),v.end(),greater<>()); sort(u.begin(),u.end(),greater<>()); ll Max=sum; for(int i=0;i<v.size();i++){ sum+=v[i]+u[i]; Max=max(Max,sum); } cout<<Max<<endl; } /* [([(())])(([]))] */
#include<bits/stdc++.h> using namespace std; using ll = long long; using pint = pair<int,int>; // #include"debug.hpp" const int INF = 1e9; vector<vector<int>> lcs(vector<int> s, vector<int> t) { int n = s.size(), m = t.size(); vector<vector<int>> dp(n + 1, vector<int>(m + 1, INF)); //dp[i][j] := s[i - 1], t[j - 1]まで処理し終えたときの最小コスト for(int i = 0; i <= n; i++) { for(int j = 0; j < m; j++) { if(i == 0) { dp[i][j] = j; } if(j == 0) { dp[i][j] = i; } } } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { dp[i + 1][j + 1] = min({dp[i][j] + 1,dp[i + 1][j] + 1, dp[i][j + 1] + 1, dp[i + 1][j + 1]}); if(s[i] == t[j]) { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]); } } } return dp; } int main() { int N, M; cin >> N >> M; vector<int> A(N), B(M); for(int i = 0; i < N; i++) cin >> A[i]; for(int i = 0; i < M; i++) cin >> B[i]; vector<vector<int>> dp = lcs(A, B); cout << dp[N][M] << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define rep2(i,x,n) for(int i=x; i<(n); i++) #define all(x) x.begin(),x.end() typedef long long ll; ll mod = 1000000007; ll inf = 1e18; int main(){ int n,m; cin >> n >> m; vector<int> h(n),w(m); vector<int> dist(n-1); vector<int> teacher(n); ll sum=0,ans=mod; rep(i,n) cin >> h[i]; rep(i,m) cin >> w[i]; sort(all(h)); sort(all(w)); if(n==1){ int a=mod; rep(i,m){ a=min(a,abs(w[i]-h[0])); } cout << a << endl; return 0; } for(int i=0; i<n-1; i+=2){ if(i==0) dist[i]=h[i+1]-h[i]; else dist[i]=h[i+1]-h[i]+dist[i-2]; } for(int i=n-2; i>0; i-=2){ if(i==n-2) dist[i]=h[i+1]-h[i]; else dist[i]=h[i+1]-h[i]+dist[i+2]; } for(int i=0; i<n; i+=2){ if(i==0) teacher[i]=dist[i+1]; else if(i==n-1) teacher[i]=dist[i-2]; else teacher[i]=dist[i-2]+dist[i+1]; } rep(i,m){ int key=w[i]; auto iter=lower_bound(all(h),key); int x=iter-h.begin(); if(x%2) x--; sum=abs(key-h[x])+teacher[x]; // cout << sum << endl; ans=min(ans,sum); sum=0; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int template<class T> auto vec(int r, int c, T v){ return vector<vector<T>>(r, vector<T>(c, v)); } template<class T> auto vec(int o1, int o2, int o3, T v){ return vector<vector<vector<T>>>(o1, vector<vector<T>>(o2, vector<T>(o3, v))); } int power(int x, int n, int m = -1){ if(m != -1) x %= m; int ans = 1; while(n > 0){ if( n & 1 ) { ans *= x; if(m != -1) ans = ans % m; } x = x * x; if(m != -1) x = x % m; n >>= 1; } return ans; } #define dbg(x) cerr << "[" << #x << " = " << x << "] "; #define dbga(x, l, r) { cerr << "[" << #x << " = "; for(int i = l; i <= r; i++) { cerr << x[i]; if(i != r) cerr << ", ";} cerr << "]"; } #define ub(x, y) (upper_bound(x.begin(), x.end(), y) - x.begin()) #define lb(x, y) (lower_bound(x.begin(), x.end(), y) - x.begin()) int TIME = 0; void DFS(auto &adj, auto &dp, auto &d, auto &in, auto &out, int u = 0, int p = -1, int h = 0) { d[u] = h; in[u] = TIME++; dp[h].push_back(in[u]); for(int v : adj[u]) { if(v == p) continue; DFS(adj, dp, d, in, out, v, u, h + 1); } out[u] = TIME++; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> p(n, -1); vector<vector<int>> adj(n); vector<vector<int>> dp(n); vector<int> d(n, 0), in(n), out(n); for(int i = 1; i < n; i++) { cin >> p[i]; p[i]--; adj[p[i]].push_back(i); adj[i].push_back(p[i]); } DFS(adj, dp, d, in, out); int q; cin >> q; for(int qq = 0, u, d; qq < q; qq++) { cin >> u >> d; u--; cout << lb(dp[d], out[u]) - lb(dp[d], in[u]) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define REPP(i,a,b,d) for(int i=(a);i<=(int)(b);i+=(d)) #define REP(i,a,b) REPP(i,a,b,1) #define REVV(i,a,b,d) for(int i=(a);i>=(int)(b);i-=(d)) #define REV(i,a,b) REVV(i,a,b,1) #define FOR(i,a) for(int i=0; i<(int)(a); i++) #define FORD(i,a) for(int i=(int)(a)-1; i>=0; i--) #define pb push_back typedef long long ll; struct Matrix { ll a, b, c, d; }; struct Point { ll x, y; }; const int N = 2e5+5; int n, m, q; Point a[N]; pair<int, int> op[N]; vector<pair<int, int>> queries[N]; Point ans[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; REP(i, 1, n) cin >> a[i].x >> a[i].y; cin >> m; REP(i, 1, m) { cin >> op[i].first; if(op[i].first > 2) cin >> op[i].second; } cin >> q; FOR(i, q) { int x, y; cin >> x >> y; queries[x].pb({y, i}); } Matrix mul = {1, 0, 0, 1}; Point add = {0, 0}; REP(i, 0, m) { if(i > 0) { if(op[i].first == 1) { mul = {mul.c, mul.d, -mul.a, -mul.b}; add = {add.y, -add.x}; } else if(op[i].first == 2) { mul = {-mul.c, -mul.d, mul.a, mul.b}; add = {-add.y, add.x}; } else if(op[i].first == 3) { mul = {-mul.a, -mul.b, mul.c, mul.d}; add = {-add.x, add.y}; add.x += 2 * op[i].second; } else { mul = {mul.a, mul.b, -mul.c, -mul.d}; add = {add.x, -add.y}; add.y += 2 * op[i].second; } } for(auto &q : queries[i]) { int &idx = q.first, &id = q.second; Point nw; nw.x = mul.a * a[idx].x + mul.b * a[idx].y; nw.y = mul.c * a[idx].x + mul.d * a[idx].y; nw.x += add.x, nw.y += add.y; ans[id] = nw; } } FOR(i, q) cout << ans[i].x << ' ' << ans[i].y << '\n'; return 0; }
#pragma region head #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <class T> using vv = vector<vector<T>>; #define _roverload(_a1, _a2, _a3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(...) _roverload(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define _rrep(i, n) rrepi(i, 0, n) #define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); --i) #define rrep(...) _roverload(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define bit(n) (1LL << (n)) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } void print() { cout << '\n'; } template <class T> void print(const vector<T> &v) { bool f = false; for (const auto &e : v) { if (f) cout << ' '; cout << e; f = true; } cout << '\n'; } template <class... T> void print(const T &...args) { bool f = false; for (const auto &e : {args...}) { if (f) cout << ' '; cout << e; f = true; } cout << '\n'; } const int INF = 1002003004; const ll LINF = 1002003004005006007ll; struct preprocess { preprocess() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); } } ____; #pragma endregion head #pragma region library #pragma endregion library int main() { vi a(3); rep(i, 3) cin >> a[i]; sort(all(a)); if (a[0] == a[1]) print(a[2]); else if (a[1] == a[2]) print(a[0]); else print(0); }
#include <cstdio> using namespace std; int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); if(a == b) printf("%d\n", c); else if(b == c) printf("%d\n", a); else if(a == c) printf("%d\n", b); else puts("0"); return 0; }
#include <iostream> using namespace std; using LL = long long; const int kMaxN = 1e6 + 1; LL f[4][kMaxN * 3]; LL n, k, s; int main() { cin >> n >> k; f[0][0] = 1; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3 * n; j++) { f[i][j] = f[i][j - 1] + f[i - 1][j - 1] - (j > n ? f[i - 1][j - 1 - n] : 0); } } for (s = 3; k > f[3][s]; s++) { k -= f[3][s]; } for (int i = 2, v; i >= 1; i--) { for (v = 1; k > f[i][s - v]; v++) { k -= f[i][s - v]; } cout << v << " "; s -= v; } cout << s; return 0; }
#include<bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define lli long long int #define pb push_back #define mp make_pair #define pii pair<int,int> #define plli pair<lli,lli> #define vi vector<int> #define vlli vector<lli> #define vpi vector<pii> #define vplli vector<plli> #define mi map<int,int> #define mlli map<lli,lli> #define forl(i,a,b) for(lli i=a;i<b;i++) #define ford(i,a,b) for(lli i=a;i>=b;i--) #define cout0(a) cout << a << endl #define cout1(a) cout << a << " " #define cout2(a,b) cout << a << " " << b << endl #define cout3(a,b,c) cout << a << " " << b << " " << c << endl #define cout4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << endl lli gcd(lli a,lli b) { if(b>a) { lli t=a; a=b; b=t; } while(b!=0) { lli t = a; a = b; b = t%b; } return a; } bool sortbysec(const pair<lli,lli> &a,const pair<lli,lli> &b) { return (a.first < b.first); } void dfs(vlli v[],lli s,lli depth[],lli isvisited[]) { forl(i,0,v[s].size()) { lli t=v[s][i]; if(!isvisited[t]) { depth[t]=depth[s]+1; isvisited[t]++; dfs(v,t,depth,isvisited); } } } lli nCr(lli n,lli r) { lli p = 1,k = 1; if (n-r < r) r = n - r; if (r != 0) { while (r) { p*=n; k*=r; lli m = __gcd(p, k); p/=m; k/=m; n--; r--; } } else p = 1; return p; } lli SieveOfEratosthenes(lli n) { bool prime[n+1]; memset(prime,true,n+1); forl(i,2,n+1) if(prime[i]) for(lli j=2*i;j<n+1;j+=i) prime[i]=false; lli count=0; forl(i,2,n+1) if(prime[i]) count++; return count; } int main() { fastio; // time_t start, end; // time(&start); lli q; // cin >> q; q = 1; while(q--) { lli n,k; cin >> n >> k; lli kthcake = 0; lli r = 0; lli sum = 0; for(r=3; r<=3*n;r++) { sum = (r-1)*(r-2)/2; if(r-n-1 >= 2) sum -= 3*(((r-n-1)*(r-n-2))/2); if(r-2*n-1 >= 2) sum += 3*(((r-2*n-1)*(r-2*n-2))/2); kthcake += sum; if(kthcake >= k) break; } kthcake -= sum; lli diff = k - kthcake; int r1 = 0; lli requiredcake = 0; for(r1=1; r1<=n; r1++) { sum = r-r1-1; if(r-r1-n-1 >= 1) sum -= 2*(r-r1-n-1); if(r-r1-2*n-1 >= 1) sum += (r-r1-2*n-1); requiredcake += sum; if(requiredcake >= diff) break; } requiredcake -= sum; lli diff2 = diff - requiredcake; int r2 = 0; lli requiredcake2 = 0; for(r2=1; r2<=n; r2++) { sum = 1; if(r-r1-r2-n-1 >= 0) sum -= 1; requiredcake2 += sum; if(requiredcake2 == diff2) break; } lli beauty_ans = r1; lli taste_ans = r2; lli popularity_ans = r - (beauty_ans + taste_ans); cout3(beauty_ans,taste_ans,popularity_ans); } // time(&end); // cout<<fixed<<double(end - start)<<setprecision(10)<<endl; }
#include <bits/stdc++.h> #define int long long // #define endl "\n" #define MOD 1000000007 using namespace std; template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, array<T, 2> A) { os << "[ "; for (auto a : A) { os << a << " "; } os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, set<T> S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class T> ostream &operator<<(ostream &os, multiset<T> S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) { os << "{ "; for (auto m : M) os << "(" << m.first << ":" << m.second << ") "; return os << "}"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } void solve() { int n; cin >> n; int a[n]; pair<int, int> dp[n][2]; for(int i = 0; i < n; i++) { cin >> a[i]; } auto f = [&] (long double x) { long double res = 0; for(int i = 0; i < n; i++) { res += x + a[i] - min((long double)a[i], 2 * x); } res /= n; return res; }; long double l = 0, r = 1e9, mid1 = 0, mid2 = 0; do { mid1 = (2 * l + r) / 3; mid2 = (l + 2 * r) / 3; if(f(mid1) > f(mid2)) { l = mid1; } else r = mid2; } while(abs(r - l) > 0.0000000001); cout << fixed; cout << setprecision(8) << f((l + r) / 2) << endl; } int32_t main() { int t = 1; #ifndef ONLINE_JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); // cin >> t; while(t--) { solve(); } #ifndef ONLINE_JUDGE cout << "\nTime Elapsed : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << " ms\n"; #endif return 0; }
#include <bits/stdc++.h> const int N = 105, P = 998244353; int n, s, w[N], dp[N][N * N], fac[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &w[i]), s += w[i]; for (int i = fac[0] = 1; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % P; if (s & 1) return puts("0"), 0; dp[0][0] = 1; for (int i = 1; i <= n; ++i) for (int j = i; j; --j) for (int v = s / 2; v; --v) { if (v >= w[i]) dp[j][v] = (dp[j][v] + dp[j - 1][v - w[i]]) % P; } int ans = 0; for (int i = 1; i < n; ++i) ans = (ans + 1ll * dp[i][s / 2] * fac[i] % P * fac[n - i]) % P; return printf("%d\n", ans), 0; }
#include<bits/stdc++.h> using namespace std; #define lln long long int #define MOD 1000000007 vector<vector<int> > adj(20001); int freq; void dfs(int i, vector<int> & vis) { freq=freq+1; vis[i]=1; for(auto child: adj[i]) { if(vis[child]==false) { dfs(child,vis); } } } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int n; cin>>n; int i, t[n],sum=0,j; for(i=0;i<n;i++) { cin>>t[i]; sum+=t[i]; } // the question is reduced to fact that we have to divide the array in 2 parts such // that difference of their sum is minimum // each element can either go to first part or second part. int dp[sum+1]; for(i=0;i<=sum;i++) dp[i]=0; dp[0]=1; for(i=0;i<n;i++) { for(j=sum;j>=t[i];j--) dp[j] = dp[j] | dp[j-t[i]] ; } int min=INT_MAX, ans=-1; for(i=0;i<=sum;i++) { if(dp[i]>0) { if(abs(2*i-sum)< min) { min=abs(2*i-sum); ans = max(i, sum-i); } } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++) // Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac #define dump(var) do{std::cerr << #var << " : ";view(var);}while(0) template<typename T> void view(T e){std::cerr << e << "\n";} template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";} template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } } template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); } ll dp[110][100010]; void chmin(ll &a, ll b) { a = min(a, b); } void solve() { rep(i, 0, 110) rep(j, 0, 100010) dp[i][j] = 1e9; ll n; cin >> n; vector<ll> t(n); rep(i, 0, n) cin >> t[i]; dp[0][0] = 0; rep(i, 0, n) { rep(j, 0, 100001) { chmin(dp[i+1][j], dp[i][j]+t[i]); if(j + t[i] <= 100001) chmin(dp[i+1][j+t[i]], dp[i][j]); } } ll ans = 1e9; rep(i, 0, 100001) chmin(ans, max(dp[n][i], i)); cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
#include<bits/stdc++.h> using namespace std; int const p=1e9+7; int f[1005][2][2],g[1005][2][2],h[1005][2][2]; int main() { int n; char aa,ab,ba,bb; cin>>n>>aa>>ab>>ba>>bb; f[2][0][0]=f[2][0][1]=f[2][1][0]=f[2][1][1]=1; g[2][0][0]=g[2][0][1]=g[2][1][0]=g[2][1][1]=1; h[2][0][0]=h[2][0][1]=h[2][1][0]=h[2][1][1]=1; int t0=aa-'A',t1=ab-'A',t2=ba-'A',t3=bb-'A'; for(int i=3;i<=n;i++) { for(int j=2;j<i;j++) { if(t0)f[i][0][0]=(f[i][0][0]+1ll*g[j][0][t0]*f[i-j+1][t0][0])%p; else f[i][0][0]=(f[i][0][0]+1ll*h[j][0][t0]*f[i-j+1][t0][0])%p; if(t1)f[i][0][1]=(f[i][0][1]+1ll*f[j][0][t1]*g[i-j+1][t1][1])%p; else f[i][0][1]=(f[i][0][1]+1ll*h[j][0][t1]*f[i-j+1][t1][1])%p; if(t2)f[i][1][0]=(f[i][1][0]+1ll*g[j][1][t2]*f[i-j+1][t2][0])%p; else f[i][1][0]=(f[i][1][0]+1ll*h[j][1][t2]*f[i-j+1][t2][0])%p; if(t3)f[i][1][1]=(f[i][1][1]+1ll*g[j][1][t3]*f[i-j+1][t3][1])%p; else f[i][1][1]=(f[i][1][1]+1ll*h[j][1][t3]*f[i-j+1][t3][1])%p; if(!t0)g[i][0][0]|=(g[j][0][0]&g[i-j+1][0][0]); if(!t1)g[i][0][1]|=(g[j][0][0]&g[i-j+1][0][1]); if(!t2)g[i][1][0]|=(g[j][1][0]&g[i-j+1][0][0]); if(!t3)g[i][1][1]|=(g[j][1][0]&g[i-j+1][0][1]); if(t0)h[i][0][0]|=(h[j][0][1]&h[i-j+1][1][0]); if(t1)h[i][0][1]|=(h[j][0][1]&h[i-j+1][1][1]); if(t2)h[i][1][0]|=(h[j][1][1]&h[i-j+1][1][0]); if(t3)h[i][1][1]|=(h[j][1][1]&h[i-j+1][1][1]); } } cout<<f[n][0][1]; return 0; }
#include <iostream> using namespace std; typedef long long ll; const int N=1007; const ll mod=1e9+7; int n; ll fib[N]; char c1,c2,c3,c4; ll ksm(ll x,ll p){ ll res=1; while(p){ if(p&1) res=res*x%mod; p/=2; x=x*x%mod; } return res; } void init(){ fib[1]=1,fib[2]=1; for(int i=3;i<=n;i++){ fib[i]=(fib[i-1]+fib[i-2])%mod; } } int main(){ cin>>n; cin>>c1>>c2>>c3>>c4; if(n==2){ cout<<1<<"\n"; return 0; } init(); if((c2=='A'&&c1=='A')||(c2=='B'&&c4=='B')){ cout<<"1\n"; }else if(c2=='A'&&c1=='B'&&c3=='B'){ cout<<ksm(2,n-3)<<"\n"; }else if(c2=='B'&&c4=='A'&&c3=='A'){ cout<<ksm(2,n-3)<<"\n"; }else{ cout<<fib[n-1]<<"\n"; } }
#include<bits/stdc++.h> using ll = int_fast64_t; using P = std::pair<ll,ll>; using PP = std::pair<ll,P>; #define REP(i,b,e) for(int i=b; i<e; i++) #define PRINT(vec) {printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");} #define fi first #define se second const int MOD = 1e9+7; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1}; int main(){ int n, w; scanf("%d %d", &n, &w); printf("%d\n", n/w); return 0; }
#include <iostream> using namespace std; int main() { int N, W; cin >> N >> W; cout << N / W; return 0; }
#include <cstdio> #include <algorithm> const int N = 1000 + 10; const int MOD = 1e9 + 7; inline int qmod(int x) { return (x>=MOD)?(x-MOD):x; } int main() { int i, p2, f1, f2, tmp, n; char aa, ab, ba, bb; scanf("%d", &n); if(n == 2 or n == 3) { printf("1\n"); return 0; } f1 = f2 = 1; for(i=2; i<=n-2; i++) { tmp = qmod(f1 + f2); f2 = f1; f1 = tmp; } for(p2=1, i=1; i<=n-3; i++) p2 = qmod(p2 * 2); scanf(" %c %c %c %c", &aa, &ab, &ba, &bb); if(ab == 'B') { if(bb == 'B') printf("1\n"); else { if(ba == 'A') printf("%d\n", p2); else printf("%d\n", f1); } } else { if(aa == 'A') printf("1\n"); else { if(ba == 'B') printf("%d\n", p2); else printf("%d\n", f1); } } return 0; }
#include<bits/stdc++.h> #define con(typ) const typ typedef long long ll; template<typename T>void sf(T &x){x=0;T f=0;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')f=1;for(;c>='0'&&c<='9';c=getchar())x=(x<<3)+(x<<1)+(c^'0');if(f)x=-x;} template<typename T>void pf(T x,char l='\n'){static int s[100],t;if(x<0)putchar('-'),x=-x;do s[++t]=x%10,x/=10;while(x);while(t)putchar(s[t--]^'0');putchar(l);} int main(){ std::vector<std::pair<ll,ll>> a; int n; ll k; sf(n),sf(k); for(int i=1;i<=n;++i){ ll x,y; sf(x),sf(y); a.emplace_back(std::make_pair(x,y)); } #define all(x) (x).begin(),(x).end() std::sort(all(a)); for(std::pair<ll,ll> now:a){ if(now.first>k)break; k+=now.second; } pf(k); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; constexpr ll mod = 1e9 + 7; ll modpow(ll a, ll b) { ll r = 1; while (b) { if (b & 1) r = r * a % mod; a = a * a % mod; b >>= 1; } return r; } int main() { // ios::sync_with_stdio(false); // cin.tie(nullptr); ll n, w, h, ans = 0, cnt = 0; cin >> h >> w; string s[h]; ll tate[h][w] = {}, yoko[h][w] = {}; rep(i, h) cin >> s[i]; rep(i, h) rep(j, w) if (s[i][j] == '.') cnt++; if (cnt == h * w) { ll t = h + w - 1; ans = (modpow(2, t) - 1) * modpow(2, h * w - t); ans %= mod; ans *= h * w; ans %= mod; cout << ans << endl; return 0; } ans = 0; rep(i, h) rep(j, w) { if (s[i][j] == '#') continue; ll now = 1; for (int k = i - 1; k >= 0; --k) if (s[k][j] == '.') now++; else break; for (int k = i + 1; k < h; ++k) if (s[k][j] == '.') now++; else break; for (int k = j - 1; k >= 0; --k) if (s[i][k] == '.') now++; else break; for (int k = j + 1; k < w; ++k) if (s[i][k] == '.') now++; else break; ll add = (modpow(2, now) - 1) * modpow(2, cnt - now) % mod; (ans += add) %= mod; } cout << ans << endl; return 0; }
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0) #pragma GCC optimize(2) #include<set> #include<map> #include<cmath> #include<queue> #include<string> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<unordered_map> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int N=2010; const ll mod=1e9+7; char g[N][N]; int n,m; ll base[N*N]; int w[N][N]; int main() { IO; int T=1; //cin>>T; while(T--) { cin>>n>>m; for(int i=1;i<=n;i++) cin>>g[i]+1; int cnt=0; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cnt+=(int)g[i][j]=='.'; base[0]=1; for(int i=1;i<=cnt;i++) base[i]=base[i-1]*2%mod; ll res=0; for(int i=1;i<=n;i++) { int j=1; while(j<=m) { while(g[i][j]=='#') j++; int now=1,k=1; while(j+k<=m&&g[i][j+k]=='.') now++,k++; for(int p=j;p<j+k;p++) w[i][p]+=now; j=j+k; } } for(int i=1;i<=m;i++) { int j=1; while(j<=n) { while(g[j][i]=='#') j++; int now=1,k=1; while(j+k<=n&&g[j+k][i]=='.') now++,k++; for(int p=j;p<j+k;p++) w[p][i]+=now; j=j+k; } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(g[i][j]=='#') continue; int now=w[i][j]-1; res=(res+(base[now]-1)*base[cnt-now]%mod)%mod; } } cout<<(ll)(res+mod)%mod<<'\n'; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const double pi=3.1415926535897932384626; inline ll read(){ ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } const int maxn=4e5+5; int n,q,t,a,b,cnt; char s[maxn]; int main(){ n=read(); scanf("%s",s+1); q=read(); while(q--){ t=read(),a=read(),b=read(); if(t&1){ if(cnt&1){ if(a<=n) a+=n; else a-=n; if(b<=n) b+=n; else b-=n; } swap(s[a],s[b]); } else ++cnt; } if(cnt&1){ for(int i=1;i<=n;++i) swap(s[i],s[i+n]); } printf("%s\n",s+1); return 0; }
#include<iostream> #include <algorithm> #include <vector> #include <string> #include <cmath> #include <iomanip> using namespace std; typedef long long ll; typedef pair<int,int> P; int main(){ int N, Q; cin >> N; char S1[N], S2[N]; for(int i=0;i<N;i++){ cin >> S1[i]; } for(int i=0;i<N;i++){ cin >> S2[i]; } cin >> Q; int flip = 0; for(int i=0;i<Q;i++){ int T, A, B; char tmp; cin >> T >> A >> B; if(T==2){ flip = 1 - flip; }else{ if(flip==0){ if(B<=N){ tmp = S1[A-1]; S1[A-1] = S1[B-1]; S1[B-1] = tmp; }else if(N+1<=A){ tmp = S2[A-N-1]; S2[A-N-1] = S2[B-N-1]; S2[B-N-1] = tmp; }else{ tmp = S1[A-1]; S1[A-1] = S2[B-N-1]; S2[B-N-1] = tmp; } }else{ if(B<=N){ tmp = S2[A-1]; S2[A-1] = S2[B-1]; S2[B-1] = tmp; }else if(N+1<=A){ tmp = S1[A-N-1]; S1[A-N-1] = S1[B-N-1]; S1[B-N-1] = tmp; }else{ char tmp = S2[A-1]; S2[A-1] = S1[B-N-1]; S1[B-N-1] = tmp; } } } } if(flip==0){ for(int i=0;i<N;i++){ cout << S1[i]; } for(int i=0;i<N;i++){ cout << S2[i]; } cout <<endl; }else{ for(int i=0;i<N;i++){ cout << S2[i]; } for(int i=0;i<N;i++){ cout << S1[i]; } cout <<endl; } return 0; }
#include<iostream> #include<bits/stdc++.h> #include<climits> #define PI 3.14159265358979323846264338327950L #define ll long long #define db double using namespace std; const int MOD = 1e9+7; void go(){ ios_base::sync_with_stdio(0); cin.tie(0); cout<<fixed; cout<<setprecision(20); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif } void solve(){ int a,b; cin>>a>>b; for(int i = -200; i <= 200; i++){ if((2*i)==(a+b)){ cout<<i<<" "<<a-i<<endl; return; } } } int main(){ go(); // code here int t = 1; while(t--) solve(); }
#include <iostream> #include <map> #include<vector> #include<set> #include<math.h> #include <bits/stdc++.h> // #define testPrint using namespace std; bool solve() { int m,h; cin >> m>>h; return ((h%m) == 0); } signed main(int arg, char **args) { cout<<((solve())?"Yes":"No"); return 0; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define ll long long #define rep(i,n) for (ll i = 0;i<(n);++i) #define all(v) v.begin(),v.end() 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;} vector<long long> divisor(long long n){vector<long long> res;long long i = 1;while (i*i<=n){if(n%i==0){res.push_back(i);}i++;}if(res.size()==0)return res;for(long long i = res.size()-1-(res.back()*res.back()==n);i>=0;--i){res.push_back(n/res[i]);}return res;} long long safe_mod(long long x,long long m){x%=m;if(x<0){x+=m;}return x;} long long modpow(long long x,long long n,long long mod){long long ans=1;while(n>0){if(n&1){ans*=x;ans%=mod;}n>>=1;x*=x;x%=mod;}return ans;} long long intpow(long long x,long long n){long long ans=1;while(n>0){if(n&1){ans*=x;}n>>=1;x*=x;}return ans;} vector<pair<long long,long long>> factor_lst(long long n){vector<pair<long long,long long>> factor_lst;long long d=2;while(d*d<=n){if(n%d==0){long long num=0;while(n%d==0){num+=1;n/=d;}factor_lst.push_back({d,num});}d+=1;}if(n!=1){factor_lst.push_back({n,1});}return factor_lst;} int msb(int x){return x==0 ? -1:32-__builtin_clz(x);}//1-indexed int lsb(int x){return x==0 ? 32:__builtin_ctz(x)+1;}//1-indexed int popcnt(int x){return __builtin_popcount(x);} const ll LINF=4*1e18; const ll MINF=5*1e15; const int INF=2*1e9; template<class T> class Queue{ struct node{ T val; node *next,*prev; }; node *head=NULL,*tail=NULL; public: bool empty(){return head==NULL;} void push(T v){ node *q=new node; q->val=v; q->prev=tail; q->next=NULL; if(tail!=NULL)tail->next=q; tail=q; if(head==NULL)head=q; } T pop(){ node *q=head; head=q->next; if(head!=NULL)head->prev=NULL; T v=q->val; delete q; if(head==NULL)tail=NULL; return v; } T front(){ return head->val; } }; void Main(); int main(){cout<<fixed<<setprecision(15);Main();} void Main(){ int N,M; cin>>N>>M; vector<vector<int>> G(N); rep(i,M){ int A,B; cin>>A>>B; A--;B--; G[A].push_back(B); G[B].push_back(A); } int K; cin>>K; int C[K]; rep(i,K){ cin>>C[i]; C[i]--; } int table[K][K]; rep(s,K){ int dist[N]; rep(i,N)dist[i]=INF; dist[C[s]]=0; Queue<int> q; q.push(C[s]); while(!q.empty()){ int v=q.pop(); for(int nv:G[v]){ if(dist[v]+1<dist[nv]){ dist[nv]=dist[v]+1; q.push(nv); } } } rep(t,K)table[s][t]=dist[C[t]]; } int dp[1<<K][K]; rep(i,1<<K)rep(j,K)dp[i][j]=INF; rep(bit,1<<K){ if(popcnt(bit)==0){ rep(j,K)dp[0][j]=0; } else if(popcnt(bit)==1){ rep(j,K)dp[bit][j]=1; } else{ rep(k,K)if((bit>>k)&1){ int prebit=bit^(1<<k); rep(j,K)if((prebit>>j)&1){ if(dp[prebit][j]!=INF&&table[j][k]!=INF)chmin(dp[bit][k],dp[prebit][j]+table[j][k]); } } } } int ans=INF; rep(i,K)chmin(ans,dp[(1<<K)-1][i]); if(ans==INF)cout<<-1<<endl; else cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;} template <class T> inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;} #define REP(i, s, e) for (int i = s; i < e; i++) #define RREP(i, s, e) for (int i = s; i >= e; i--) typedef long long ll; typedef long double ld; #define MP make_pair #define FI first #define SE second typedef pair<int, int> ii; typedef pair<ll, ll> pll; #define MT make_tuple typedef tuple<int, int, int> iii; #define ALL(_a) _a.begin(), _a.end() #define pb push_back typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ii> vii; #ifdef DEBUG #define debug(args...) _debug(args) void _debug(const char* format, ...) { va_list args; va_start(args, format); vprintf(format, args); va_end(args); } #else #define debug(args...) #endif #define INF 1000000005 #define LINF 1000000000000000005 #define MOD 1000000007 #define MAXN 105 #define EPS 1e-7 int n; int a[MAXN], b[MAXN], c[MAXN], d[MAXN]; int main() { scanf("%d", &n); REP (_, 0, 2) { int x = 0, y = 0; REP (i, 0, n) { scanf("%d%d", &a[i], &b[i]); x += a[i]; y += b[i]; a[i] *= n; b[i] *= n; } REP (i, 0, n) { a[i] -= x; b[i] -= y; } swap(c, a); swap(d, b); } if (n == 1) { printf("Yes\n"); return 0; } if (a[0] == 0 && b[0] == 0) { swap(a[0], a[1]); swap(b[0], b[1]); } REP (i, 0, n) { if (d[i] == c[i]) continue; ld angle = atan2(d[i], c[i]) - atan2(b[0], a[0]); bool pos = 1; REP (j, 0, n) { ld x = a[j] * cos(angle) - b[j] * sin(angle); ld y = a[j] * sin(angle) + b[j] * cos(angle); bool found = 0; REP (k, 0, n) { if (abs(x - c[k]) <= EPS && abs(y - d[k]) <= EPS) { found = 1; break; } } if (!found) { pos = 0; break; } } if (pos) { printf("Yes\n"); return 0; } } printf("No\n"); return 0; } /* 3 1 0 1 1 2 0 -1 0 -1 1 -3 0 */
#include <bits/stdc++.h> using namespace std; int main() { int num, nuts, x = 0, y = 0; cin >> num; for (int i = 1; i <= num; i++){ cin >> nuts; if (nuts > 10){ x = nuts - 10; //y += x; } else{ continue; } y += x; } cout << y << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; int main() { string s; cin>>s; ll n=s.size(); map<char,ll>p; ll r=0; for(ll i=n-1;i>0;i--) { if(s[i-1]==s[i]){ r+=(n-i-1-p[s[i]]); for (ll j=0;j<26;j++) p[('a'+j)] = 0; p[s[i]]=n-i; }else p[s[i]]++; } cout<<r<<'\n'; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> using namespace std; #define int long long #define double long double #define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define mod 1000000007 #define M2 1000000009 #define M1 998244353 #define inf 1e18 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); using namespace __gnu_pbds; typedef tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> new_data_set; bool isprime(int n){ if(n==1) return false; for(int i = 2;i*i<=n;i++){ if(n%i==0) return false; } return true; } int lcm(int a,int b){ return (a*b)/__gcd(a,b); } int gcd(int a, int b){ if (b == 0){ return a; } return gcd(b, a % b); } int power(int x,int y,int p){ int res=1; if(x==0)return 0; while(y>0){ if(y&1){ res*=x; res%=p; } y=y>>1; x=(x*x); x%=p; res%=p; } return res; } int modInverse(int n, int p){ return power(n, p-2,p); } struct node{ int a,b; }; /* BAAP BAAP HOTA HAI Author : Sagar */ signed main(){ FAST int n,m; cin>>n>>m; int arr[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ char x; cin>>x; if(x=='-')arr[i][j]=-1; else arr[i][j]=1; } } int dp[n][m]; dp[n-1][m-1]=0; for(int i=n-1;i>=0;i--){ for(int j=m-1;j>=0;j--){ if(i==n-1 && j==m-1)continue; if((i+j)%2==0){ dp[i][j]=INT_MIN; if(i+1<n)dp[i][j]=max(dp[i][j],dp[i+1][j]+arr[i+1][j]); if(j+1<m)dp[i][j]=max(dp[i][j],dp[i][j+1]+arr[i][j+1]); } else{ dp[i][j]=INT_MAX; if(i+1<n)dp[i][j]=min(dp[i][j],dp[i+1][j]-arr[i+1][j]); if(j+1<m)dp[i][j]=min(dp[i][j],dp[i][j+1]-arr[i][j+1]); } } } if(dp[0][0]>0)cout<<"Takahashi"; else if(dp[0][0]<0)cout<<"Aoki"; else cout<<"Draw"; }
/* Created by: Amit Kumar at 17:50 on 15 May 21 */ #include <bits/stdc++.h> #ifdef LOCAL #include "debug.h" #else #define db(...) #endif using namespace std; #define int long long #define S(x, n) memset(x, n, sizeof(x)); #define pb(x) emplace_back(x) #define all(x) x.begin(), x.end() #define NIL -1 const long long MAXN = 1e5 + 1; const long long mod = 1e18; template <typename T> inline istream &operator>>(istream &is, vector<T> &A); int dp[2010][2010][2]; vector<string> arr; int n, m; int calc(int i, int j, int turn) { int &ret = dp[i][j][turn]; if (ret != -mod) return ret; if (turn == 0) { ret = -mod; if (i + 1 < n) ret = max(ret, (arr[i + 1][j] == '+' ? 1 : -1) + calc(i + 1, j, turn ^ 1)); if (j + 1 < m) ret = max(ret, (arr[i][j + 1] == '+' ? 1 : -1) + calc(i, j + 1, turn ^ 1)); if (ret == -mod) ret = 0; } else { ret = mod; if (i + 1 < n) ret = min(ret, (arr[i + 1][j] == '+' ? -1 : 1) + calc(i + 1, j, turn ^ 1)); if (j + 1 < m) ret = min(ret, (arr[i][j + 1] == '+' ? -1 : 1) + calc(i, j + 1, turn ^ 1)); if (ret == mod) ret = 0; } db(ret, i, j, turn); return ret; } void solve() { cin >> n >> m; arr = vector<string>(n); cin >> arr; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) dp[i][j][0] = dp[i][j][1] = -mod; int ans = calc(0, 0, 0); db(ans); if (ans == 0) cout << "Draw\n"; else if (ans > 0) cout << "Takahashi\n "; else cout << "Aoki\n"; } int32_t main() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); auto started = std::chrono::high_resolution_clock::now(); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; //cin >> t; for (int I = 1; I <= t; I++) { #ifdef LOCAL cerr << "TestCase " << I << "\n"; #endif solve(); } #ifdef LOCAL auto done = std::chrono::high_resolution_clock::now(); cerr << "Time = " << std::chrono::duration_cast<std::chrono::milliseconds>(done - started).count() << " ms\n"; #endif return 0; } template <typename T> inline istream &operator>>(istream &is, vector<T> &A) { for (int i = 0; i < A.size(); i++) is >> A[i]; return is; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<long long> Vll; ll mm=1000000000;ll MM=mm+7; #define rep(i, n) for(int i=0;i<n;i++) #define PI 3.141592653589793 int main(){ ll n,k; cin >> n >> k; vector<Vll> t(n,Vll(n)); rep(i,n){ rep(j,n){ cin >> t[i][j]; } } ll ans=0; vector<int> v(n-1); for(int i=0;i<n-1;i++)v[i]=i+1; do{ int cnt=0; rep(i,n-1){ if(i==n-2){ cnt+=t[v[i-1]][v[i]]; cnt+=t[v[i]][0]; } else if(i==0){ cnt+=t[0][v[i]]; } else{ cnt+=t[v[i-1]][v[i]]; } if(cnt==k)ans++; } }while(next_permutation(v.begin(),v.end())); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int N, ft[200005]; set< pair<int,int> > s; void update (int x, int v) { x++; while (x <= N) ft[x] += v, x += x & -x; } int query (int x) { x++; int ans = 0; while (x) ans += ft[x], x -= x & -x; return ans; } int main() { scanf("%d", &N); memset(ft,0,sizeof(ft)); for (int i = 1; i < N; ++i) update(i,1); for (int i = 0; i < N; ++i) { int x; scanf("%d", &x); s.emplace(x+i,i); } long long ans = 0; for (int i = 0; i < N; ++i) { int x; scanf("%d", &x); x += i; auto it = s.lower_bound(make_pair(x,-1)); if (it == s.end()) { printf("-1"); return 0; } int n = (*it).second; ans += query(n); update(n,-1); s.erase(it); } printf("%lld", ans); return 0; }
/*One cannot escape the feeling ... that these mathematical formulae have an independent existence and an intelligence of their own ... that they are wiser than we are, wiser even than their discoverers ... that we get more out of them that was originally put into them. */ #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; id good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side. >= 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[52],dp[52],span[52],g[52],possible[52],fact[52]; int main(){ ll n,x; cin>>n>>x; forn(i,n) cin>>a[i]; forn(i,n-1){ fact[i]=(a[i+1]/a[i])-1; } fact[n-1]=1e18; for(ll i=n-1;i>=0;i--){ g[i]=x/a[i]; x%=a[i]; } for(ll i=0;i<n;i++){ ll c=0; for(ll j=i+1;j<n;j++){ if(g[j]==fact[j]) c++; else break; } span[i]=c; } possible[n-1]=1; dp[n-1]=1; for(ll i=n-2;i>=0;i--){ dp[i]=0; ll c=0; if(g[i]) c+=possible[i+span[i]+1]; dp[i]+=dp[i+1]; dp[i]+=c; if(g[i]) possible[i]=dp[i]; else possible[i]=dp[i+1]+possible[i+1+span[i]]; } cout<<dp[0]; }
#include<bits/stdc++.h> long long n,x; long long ans[2],k[2],a,b,c,now; int main() { scanf("%lld%lld",&n,&x); scanf("%lld",&b); ans[0]=1;ans[1]=0; for(int i=1;i<n;++i) { scanf("%lld",&a); c=a/b;b=a; now=x%c; x/=c; k[0]=ans[0],k[1]=ans[1]; if(now)k[1]+=ans[0]; if(++now!=c)k[0]+=ans[1]; ans[0]=k[0];ans[1]=k[1]; } printf("%lld",ans[0]+ans[1]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; int a[10]={0}; int zh(string s); cin >> str; for(int i=0;i<str.size();i++) { a[str[i]-'0']++; } if(str.size()==1) { if(zh(str) == 8) { cout << "Yes"; } else { cout << "No"; } return 0; } if(str.size()==2) { if(zh(str)%8==0) { cout << "Yes"; } else { swap(str[0],str[1]); if(zh(str)%8==0) cout << "Yes"; else cout << "No"; } return 0; } for(int i=112;i<999;i+=8) { int b,sh,g; b=i/100; sh=i/10%10; g=i%10; a[b]--; a[sh]--; a[g]--; if(a[b]>=0 && a[sh]>=0 && a[g]>=0) { cout << "Yes"; return 0; } else { a[b]++; a[sh]++; a[g]++; } } cout << "No"; return 0; } int zh(string a) { int ans=0; for(int i=0;i<a.size();i++) { ans = ans*10+(a[i]-'0'); } return ans; }
/* BISMILLAHIR-RAHMANIR-RAHIM ____________________________________ | | | SHANTO_SUST_SWE-19__029 | | shanto-swe029.github.io | |____________________________________| */ #include <bits/stdc++.h> using namespace std; #define ll long long #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define pb push_back #define Pi acos(-1.0) #define r0 return 0 #define endl "\n" #define show(x) cout << x << endl #define take(x) cin >> x #define debug 1 int main() { string s; std::cin >> s; int sz = s.size(); if( sz == 1 ) { if( s == "8" ) std::cout << "Yes"; else std::cout << "No"; return 0; } if( sz == 2 ) { int d1 = s[0] - '0'; int d2 = s[1] - '0'; int n1 = d1 * 10 + d2; int n2 = d2 * 10 + d1; if( ( n1 % 8 == 0 ) || ( n2 % 8 == 0 ) ) { std::cout << "Yes"; return 0; } else { std::cout << "No"; return 0; } } int counter[10] = {0}; for( int i = 0; i < sz; i++ ) { int x = ( int ) ( s[i] - '0' ); counter[x]++; } std::vector < int > v; for( int i = 0; i < 10; i++ ) { if( counter[i] > 0 ) v.push_back(i); if( counter[i] > 1 ) v.push_back(i); if( counter[i] > 2 ) v.push_back(i); } bool gotcha = false; for( int i = 0; i < v.size(); i++ ) { for( int j = i + 1; j < v.size(); j++ ) { for( int k = j + 1; k < v.size(); k++ ) { int d1 = v[i]; int d2 = v[j]; int d3 = v[k]; int n1 = d1 * 100 + d2 * 10 + d3; int n2 = d1 * 100 + d3 * 10 + d2; int n3 = d2 * 100 + d1 * 10 + d3; int n4 = d2 * 100 + d3 * 10 + d1; int n5 = d3 * 100 + d1 * 10 + d2; int n6 = d3 * 100 + d2 * 10 + d1; bool pass = ( n1 % 8 == 0 ) || ( n2 % 8 == 0 ) || ( n3 % 8 == 0 ) || ( n4 % 8 == 0 ) || ( n5 % 8 == 0 ) || ( n6 % 8 == 0 ); if( pass ) { gotcha = true; break; } } if( gotcha ) break; } if( gotcha ) break; } if( gotcha ) cout << "Yes"; else cout << "No"; return 0; } //ALHAMDULILLAH
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef double dd; #define tst int tttt;cin>>tttt;for(int TTTT=1;TTTT<=tttt;TTTT++) #define nl cout<<"\n"; #define forn(a,b) for(int ii=a;ii<b;ii++) const ll MOD1=1e9+7; const ll MOD2=998244353; #define mp(a,b) make_pair(a,b) #define pb(a) push_back(a) #define all(a) a.begin(),a.end() #define pii pair<int,int> #define vi vector<int> #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define hhh cout<<"here"<<endl; ll mod=MOD1; ll po(ll x,ll y) {y%=mod;ll res=1;while(y>0){if(y&1)res=(res*x);x=(x*x);y>>=1;}return res;} ll gcd(int a, int b){if(a<b) swap(a,b);if(b==0) return a;return gcd(a%b,b);} void solve(){ int n,m;cin>>n>>m; if(n==1 && m==0){ cout<<"1 2\n";return; } if(!(m>=0 && m<=n-2)){cout<<"-1\n";return;} int inf=1e8; forn(1,n-m){ cout<<ii<<" "<<ii+inf;nl; }int i=0; forn(n-m,n+1){ cout<<ii+i<<" "<<ii+1+i;nl; i++; } } int main(){ // fastio // ++*(int*)0;// crash // freopen("out.txt","r",stdin);freopen("outt.txt","w",stdout); // tst { solve(); } } /* */
#include<bits/stdc++.h> using namespace std; int main() { // freopen("1.in","r",stdin); int n,m; scanf("%d %d",&n,&m); if(n==1) { if(m==0)printf("1 2\n"); else puts("-1"); } else { if(m<0 || m>n-2)puts("-1"); else { int nn=n,r=0; if(m>=1) { printf("1 %d\n",n*2); --nn;r=1; for(int i=1;i<=m+1;++i) { printf("%d %d\n",r+1,r+2); --nn;r+=2; } r=n*2; } while(nn) { printf("%d %d\n",r+1,r+2); --nn;r+=2; } puts(""); } } return 0; }
#include <bits/stdc++.h> #include <iostream> #include <map> #include <math.h> #include <string> #include <string.h> #include <vector> #include <queue> #include <algorithm> #include <set> #define _GLIBCXX_DEBUG #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() using namespace std; using ll = long long; const ll INF = 50000000000000000; const ll dx[8] = {1,0,-1,0,1,1,-1,-1}; const ll dy[8] = {0,1,0,-1,1,-1,1,-1}; ll mod(ll x,ll m){return x & m;} ll modinv(ll a,ll m){ ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } ll modpow(ll a, ll n, ll m) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % m; a = a * a % m; n >>= 1; } return res; } ll npow(ll a, ll n){ ll res = 1; while (n > 0) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } ll yaku(ll x){ ll cnt = 0; for(int i = 1; i * i <= x; i++){ if(x % i == 0){ if(i == x / i) cnt++; else cnt += 2; } } return cnt; } ll modwaru(ll a, ll b, ll m){ a %= m; return a * modinv(b, m) % m; } ll gcd(ll x, ll y){ if(x % y == 0)return y; else return gcd(y, x % y); } struct UnionFind { vector<int> par; UnionFind(int N) : par(N) { for(int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main(){ ll K, N, M, wa = 0; cin >> K >> N >> M; vector<ll> A(K), ans(K); vector<pair<ll,ll>> C(K); for(int i = 0; i < K; i++) cin >> A[i]; for(int i = 0; i < K; i++){ ll s = M * A[i] / N; ans[i] = s; C[i].first = s * N - M * A[i]; C[i].second = i; } for(int i = 0; i < K; i++) wa += ans[i]; sort(ALL(C)); for(int i = 0; i < M - wa; i++){ ans[C[i].second]++; } for(int i = 0; i < K; i++){ cout << ans[i]; if(i != K - 1) cout << ' '; } cout << endl; }
//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" #define dbug(x) cout<<#x<<"="<<x<<endl; 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 fr[3*100002]; ll mex[3*100002]; int main(){ ll n,k,ans=0; cin>>n>>k; forn(i,n){ ll te; cin>>te; fr[te]++; } forn(i,n){ for(ll j=0;j<(min(fr[i],k));j++){ if(mex[j]==i) mex[j]++; } } forn(i,k) ans+=mex[i]; cout<<ans; }
#include <bits/stdc++.h> using namespace std; int main(){ string s, s1; cin >> s; s1 = s; for (int i = 0; i < s.size(); i++) { if (s.at(i) == '0'){ s1.at(s.size() - 1 - i) = '0'; } else if (s.at(i) == '1'){ s1.at(s.size() - 1 - i) = '1'; } else if (s.at(i) == '6'){ s1.at(s.size() - 1 - i) = '9'; } else if (s.at(i) == '8'){ s1.at(s.size() - 1 - i) = '8'; } else if (s.at(i) == '9'){ s1.at(s.size() - 1 - i) = '6'; } } cout << s1 << endl; }
#include<bits/stdc++.h> using namespace std; #define int long long #define pii pair<int,int> const int M=1e9+7; const int N=200005; signed main(){ int t1=1; //cin>>t1; while(t1--){ string s; cin>>s; reverse(s.begin(),s.end()); for(int i=0;i<s.length();i++){ if(s[i]=='6') s[i]='9'; else if(s[i]=='9') s[i]='6'; } cout<<s<<endl; } }
#include "bits/stdc++.h" using namespace std; #define mod 1000000007 #define pie 3.14159265 #define ll long long int #define fr(i,a,n) for(ll i=a;i<n;i++) #define vi vector<int> #define vlli vector<ll> #define pb push_back #define ppb pop_back #define mp make_pair #define fv(i,a) for(auto i:a) #define sz(a) int((a).size()) #define all(c) (c).begin(),(c).end() #define frv(i,c) for(auto i = c.begin(); i != c.end(); i++) void solve() { ll a, b; cin >> a >> b; ll m = -1; fr(i, 1, b) { ll x = (a - 1) / i; ll y = b / i; if (y - x >= 2) m = max(m, i); } cout << m << endl; } int main() { //#ifndef ONLINE_JUDGE #ifdef yogi23 freopen("input.txt", "r", stdin); freopen ("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int t; t = 1; //cin >> t; while (t--) { solve(); } return 0; }
#include <iostream> #include <cmath> using namespace std; int getMaxGCD(int arr[], int n) { int high = 0; for (int i = 0; i < n; i++) high = max(high, arr[i]); int divisors[high + 1] = { 0 }; //array to store all gcd values for (int i = 0; i < n; i++) { for (int j = 1; j <= sqrt(arr[i]); j++) { if (arr[i] % j == 0) { divisors[j]++; if (j != arr[i] / j) divisors[arr[i] / j]++; } } } for (int i = high; i >= 1; i--) if (divisors[i] > 1) return i; } int main() { int a,b; cin >> a >> b; int arr[b-a+1]; int t=0; for(int i=a;i<=b;i++){ arr[t] = i; t++; } int n = sizeof(arr) / sizeof(arr[0]); cout << getMaxGCD(arr,n); }
#include <bits/stdc++.h> #include <math.h> #include <cmath> using namespace std; using ll =long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vs = vector<string>; using vvs = vector<vs>; using vc = vector<char>; using vvc = vector<vc>; using vb = vector<bool>; using vvb = vector<vb>; 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; } const long long INF = 1LL << 60; typedef pair<int, int> PR; #define rep(i,n) ;for( int i =0; i < n ; i ++) #define all(a) a.begin(),a.end() #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define fr(i,a,b) for(int i=a;i<b;i++) #define pri(a) printf("%.14lf\n",a); #define MOD 1000000007 bool is_int_lround(double x){ return std::lround(x)==x; } ll keta(ll x){ ll n=0; while(x>0){ x /=10 ; n ++; } return n; } ll conbi(int n,int m){ cin>>n>>m; vector<ll> a(100); a[0] =1; for(int i=0;i<14;i++){ a[i+1]=a[i]*(i+1); } return a[n] /(a[m] *a[n-m]); } long long modpow(long long a, long long n, long long mod) { long long res = 1;//繰り返し二乗法 while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll kaijo(ll x){ ll z=1; if(x==0){ return 1; } while(x>0){ z *=x; z %=MOD; x--; } return z; } ll yakusu_num(ll n){ vl yakusu(n+1,1); for(ll i=2;i*i<=n;i++){ while(n%i==0){ n /=i; yakusu[i]++; } } if(n!=1)yakusu[n]++; ll num=1; for(ll i=0;i <=n;i++){ num*=yakusu[i]; } return num; } //cout<<""<<endl; int main(){ ll N;cin>>N; set<ll> a; a.insert(1); for( ll i=1;i*i<=N;i++){ if(N%i==0){ a.insert(i); a.insert(N/i); } } for(auto x: a)cout<<x<<endl; }
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <iomanip> #include <cmath> #include <queue> #define int long long #define rep(i, a, b) for(int i = (a); i < (b); i++) #define fcout cout << fixed << setprecision(20) using namespace std; vector<long long> divisor(long long n) { vector<long long> ret; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); // 昇順に並べる return ret; } signed main() { int n; cin >> n; vector<int> v = divisor(n); rep(i, 0, (int)v.size()) cout << v[i] << "\n"; }
#include<bits/stdc++.h> #include<string> #define FIO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define ll long long #define mod (ll)(1e9+7) #define nax (ll)(1e5 + 10) #define INFL LLONG_MAX #define NINFL LLONG_MIN #define INF INT_MAX #define disp(a,n,arr) for(int i=a;i<(ll)n;i++) cout << arr[i] << " "; cout << "\n"; #define check cout << "Working\n"; #define pi pair<int,int> #define pl pair<ll,ll> #define vi vector<int> #define vl vector<ll> #define vpl vector<pl> #define vpi vector<pi> #define vb vector<bool> #define pb push_back #define mp make_pair #define fi first #define se second #define all(a) a.begin(),a.end() using namespace std; void code(){ ll a,b,c; cin >> a >> b >> c; if(a < 0 && c%2 == 0) a = -a; if(b < 0 && c%2 == 0) b = -b; if(a < 0 && b >= 0) cout << "<\n"; else if(a >= 0 && b < 0) cout << ">\n"; else{ if(a == b) cout << "=\n"; else if(a > b) cout << ">\n"; else cout << "<\n"; } } int main() { FIO; #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif int t = 1; //cin >> t; while(t--){ code(); } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int64_t a, b, c; cin >> a >> b >> c; if (c & 1 == 1) c = 1; else c = 2; if (pow(a, c) > pow(b, c)) cout << ">\n"; else if (pow(a, c) < pow(b, c)) cout << "<\n"; else cout << "=\n"; }
#include <stdio.h> #include <string.h> int ans[110][10010]; int a; char x[110]; int y[110]; void func(int k) { printf("%d\n",k); for(int i=0;i<=a;i++) { int s = y[i]/k; int t = y[i]%k; for(int j=1;j<=t;j++) ans[i][j] = s+1; for(int j=t+1;j<=k;j++) ans[i][j] = s; } for(int j=1;j<=k;j++) { for(int i=0;i<=a;i++) printf("%d ",ans[i][j]); printf("\n"); } } int main() { scanf("%d",&a); scanf("%s",x+1); for(int i=0;i<=a;i++) scanf("%d",&y[i]); int limit = 10000; for(int i=limit;i>=1;i--) { int s = y[0]/i; int t = y[0]%i; for(int j=1;j<=a;j++) { int s2 = y[j]/i; int t2 = y[j]%i; if(x[j]=='<') { if(t<t2) { if(s+1 < s2+1 && s < s2+1 && s<s2); else goto u; } else if(t>t2) { if(s+1 < s2+1 && s+1 < s2 && s<s2); else goto u; } else { if(s+1 < s2+1 && s<s2); else goto u; } } else { if(t<t2) { if(s+1 > s2+1 && s > s2+1 && s>s2); else goto u; } else if(t>t2) { if(s+1 > s2+1 && s+1 > s2 && s>s2); else goto u; } else { if(s+1 > s2+1 && s>s2); else goto u; } } s = s2; t = t2; } func(i); return 0; u:; } }
#include<bits/stdc++.h> using namespace std; #define endl "\n" #define lln long long int #define ld long double #define all(x) (x).begin(),(x).end() #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); const int MOD = 1e9 + 7; const int N = (int)2e5 + 5; int main(){ IOS; int TT = 1; //cin >> TT; for(int tt = 1; tt <= TT; tt++){ string s; cin >> s; int n = s.length(); reverse(all(s)); for(int i = 0; i < n; i++){ if(s[i] == '9') s[i] = '6'; else if(s[i] == '6') s[i] = '9'; } cout << s << endl; } return 0; }
// /Applications/Geany.app/Contents/MacOS/geany #include<bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define ppb pop_back #define mp make_pair #define pairii pair<int,int> #define ff first #define ss second #define all(x) x.begin(),x.end() const int N = 10000000; vector<int> lp(N+1,0); vector<int> pr; void primefactor(); int binpow(int a, int b); int binpow(int a, int b, int mod); int gcd(int a, int b); int lcm (int a, int b); bool comp(int a, int b); int inversemod(int a, int mod); void test() { int n,x,i,j; cin>>n>>x; x*=100; vector<pairii> pre(n); for(i=0;i<n;i++) cin>>pre[i].ff>>pre[i].ss; double sum=0; for(i=0;i<n;i++) { sum+=pre[i].ss*pre[i].ff; if(sum>x) { cout<<i+1<<endl; return; } } cout<<-1<<endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int t=1; //cin>>t; while(t--) test(); return 0; } void primefactor() { for (int i=2; i<=N; ++i) { if (lp[i] == 0) { lp[i] = i; pr.push_back (i); } for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) lp[i * pr[j]] = pr[j]; } } int binpow(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } int binpow(int a, int b, int mod) { int res = 1; while (b > 0) { if (b & 1) res = (res * a)%mod; a = (a * a)%mod; b >>= 1; } return res%mod; } int gcd(int a, int b) { if(b==0) return a; else return gcd(b,a%b); } int lcm (int a, int b) { return a / gcd(a, b) * b; } bool comp(int a, int b) { return a>b; } int inversemod(int a, int mod) { return binpow(a,mod-2, mod); }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, from, to) for (ll i = from; i < (to); ++i) #define mp(x,y) make_pair(x,y) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define pb push_back using ll = long long; using ld=long double; using vin=vector<int>; using vvin=vector<vin>; using vll=vector<ll>; using vvll=vector<vll>; using vst=vector<string>; using P = pair<ll,ll>; using vp=vector<P>; using vvp=vector<vp>; const int inf=1e9+7; const ll INF=9e18/2; const long double PI = acos(-1.0); template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;} template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;} template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } const int dx[4] = { 1, 0, -1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; int main(){//cout<<fixed<<setprecision(20); ll n,x; cin>>n>>x; x*=100; rep(i,0,n){ ll v,p; cin>>v>>p; x-=v*p; if(x<0){ cout<<i+1<<endl; return 0; } } cout<<-1<<endl; }
#include <bits/stdc++.h> using namespace std; #define SZ(a) ((int)(a).size()) typedef long long int64; int main() { #ifdef LOCAL freopen(".a.in", "r", stdin); #endif ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); int n, m; cin >> n >> m; vector<string> S(n); vector<vector<int>> A(n, vector<int>(m)), B(n, vector<int>(m)), C(n, vector<int>(m)), D(n, vector<int>(m)); int K = 0; for (int i = 0; i < n; ++i) cin >> S[i], K += count(S[i].begin(), S[i].end(), '.'); int64 md = 1e9 + 7; vector<int64> two(max(100, K + 2)); two[0] = 1; for (int i = 1; i < SZ(two); ++i) two[i] = two[i - 1] * 2 % md; int64 res = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (S[i][j] == '.') { A[i][j] = 1; if (i - 1 >= 0) A[i][j] += A[i - 1][j]; B[i][j] = 1; if (j - 1 >= 0) B[i][j] += B[i][j - 1]; } } } for (int i = n - 1; i >= 0; --i) { for (int j = m - 1; j >= 0; --j) { if (S[i][j] == '.') { C[i][j] = D[i][j] = 1; if (i + 1 <= n - 1) C[i][j] += C[i + 1][j]; if (j + 1 <= m - 1) D[i][j] += D[i][j + 1]; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) if (S[i][j] == '.') { int tot = A[i][j] + B[i][j] + C[i][j] + D[i][j] - 3; (res += (two[tot] + md - 1) * two[K - tot] % md) %= md; } } cout << res; return 0; } //14.10.2020 04:31:12 CST
#include <bits/stdc++.h> // #include <atcoder/all> // #include "icld.cpp" using namespace std; using ll = long long int; using vi = vector<int>; using si = set<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using ss = string; using db = double; template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; #define V vector #define P pair<int,int> #define rep(i,s,n) for(int i=(s);i<(int)(n);i++) #define rev(i,s,n) for(int i=(s);i>=(int)(n);i--) #define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i] #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define ci(x) cin >> x #define cii(x) ll x;cin >> x #define cci(x,y) ll x,y;cin >> x >> y #define co(x) cout << x << endl #define pb push_back #define eb emplace_back #define rz resize #define sz(x) int(x.size()) #define yn cout<<"Yes"<<endl;else cout<<"No"<<endl #define YN cout<<"YES"<<endl;else cout<<"NO"<<endl template<class T>void chmax(T &x,T y){x=max(x,y);} template<class T>void chmin(T &x,T y){x=min(x,y);} ll p=1e9+7; ll f(ll n){ if(n==0)return 1; if(n==1)return 2; if(n%2)return 2*f(n-1)%p; ll res=f(n/2); return res*res%p; } int main(){ cci(h,w); int k=0; vvi v(h+2,vi(w+2,-1)); rep(i,0,h){ rep(j,0,w){ char c;ci(c); if(c=='.')v[i+1][j+1]=0,k++; } } vvi tt=v,yk=v; rep(i,1,h+1){ int now=0; rep(j,1,w+1){ if(v[i][j]==0)yk[i][j]=++now; else now=0; } rev(j,w,1){ if(yk[i][j]==-1)continue; chmax(yk[i][j],yk[i][j+1]); } } rep(i,1,w+1){ int now=0; rep(j,1,h+1){ if(v[j][i]==0)tt[j][i]=++now; else now=0; } rev(j,h,1){ if(tt[j][i]==-1)continue; chmax(tt[j][i],tt[j+1][i]); } } ll ans=0; ll k2=f(k); map<int,int>mp; rep(i,1,h+1){ rep(j,1,w+1){ ll now=max(-1,yk[i][j]+tt[i][j]-1); // cout<<now<<" "; mp[now]++; } // co(""); } for(auto x:mp){ if(x.first==-1)continue; ll now=k2-f(k-x.first); ans+=now*x.second%p; ans%=p; } ans=(ans+p)%p; co(ans); }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <deque> #include <list> #include <set> #include <map> #include <stack> #include <queue> #include <numeric> #include <iomanip> #include <bitset> #include <sstream> #include <fstream> #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define rep(i,a,b) for(int i=a;i<=b;i++) #define req(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define fi first #define se second using namespace std; const int inf=0x3f3f3f3f; typedef long long ll; const int N = 1e5+7; const ll mod = 1e9+7; int a[N]; int main() { IO; string s; cin>>s; if(s.size()==1){ cout<<((s[0]=='8')?"Yes":"No")<<endl;return 0; } if(s.size()==2){ if(((s[0]-'0')*10+(s[1]-'0'))%8==0||((s[0]-'0')+(s[1]-'0')*10)%8==0)cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; } for(char i:s)a[i-'0']++; rep(i,1,9){ if(!a[i])continue; int k=(i*100+7)/8*8; while(k<(i+1)*100){ int p=k; if(k%10>0&&k/10%10>0){ if(a[p%10]>0){ int x=p%10; p/=10; a[x]--; if(a[p%10]>0){ int y=p%10; p/=10; a[y]--; if(a[p%10]>0){ cout<<"Yes"<<endl; return 0; }a[y]++; }a[x]++; } } k+=8; } } cout<<"No"<<endl; return 0; }
#include <iostream> #include <iomanip> #include <string> #include <stack> #include <vector> #include <complex> #include <math.h> #include <stdio.h> #include <algorithm> #include <utility> #include <functional> #include <iterator> #include <map> #include <set> #include <queue> #include <list> #include <regex> #include <limits> #include <time.h> #include <cstdint> #include <unordered_map> #include <unordered_set> #include <bitset> #include <limits.h> using namespace std; using pii = pair<int,int>; using ll=long long; using ld=long double; #define pb push_back #define mp make_pair #define sc second #define fr first #define stpr setprecision #define cYES cout<<"YES"<<endl #define cNO cout<<"NO"<<endl #define cYes cout<<"Yes"<<endl #define cNo cout<<"No"<<endl #define rep(i,n) for(ll i=0;i<(n);++i) #define Rep(i,a,b) for(ll i=(a);i<(b);++i) #define rrep(i,n) for(ll i=n-1;i>=0;i--) #define rRep(i,a,b) for(ll i=a;i>=b;i--) #define crep(i) for(char i='a';i<='z';++i) #define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;}); #define ALL(x) (x).begin(),(x).end() #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define endl '\n' int ctoi(const char c){ if('0' <= c && c <= '9') return (c-'0'); return -1; } ll gcd(ll a,ll b){return (b == 0 ? a : gcd(b, a%b));} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll MOD=1000000007; ll INF=1000000011; ll MOD2=998244353; ll LINF = 1001002003004005006ll; ld EPS=10e-8; template <class T, class U> inline bool chmax(T& lhs, const U& rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T& lhs, const U& rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } template<typename T> istream& operator>>(istream& is,vector<T>& v){for(auto&& x:v)is >> x;return is;} template<typename T,typename U> istream& operator>>(istream& is, pair<T,U>& p){ is >> p.first; is >> p.second; return is;} template<typename T,typename U> ostream& operator>>(ostream& os, const pair<T,U>& p){ os << p.first << ' ' << p.second; return os;} template<class T> ostream& operator<<(ostream& os, vector<T>& v){ for(auto i=begin(v); i != end(v); ++i){ if(i !=begin(v)) os << ' '; os << *i; } return os; } string S; ll A[10]; signed main(){ cin >> S; rep(i,S.size()){ A[ctoi(S[i])]++; } if(S.size()==2){ string V=S; reverse(ALL(V)); if(stoi(S)%8==0 || stoi(V)%8==0){ cYes;return 0; } } else if(S.size()==1){ if(stoi(S)%8==0){ cYes;return 0; } } else{ for(int i=0;i<1000;i+=8){ vector<ll> B(10,0); ll K=i; rep(j,3){ B[K%10]++; K/=10; } ll Z=1; rep(j,10){ if(B[j]>A[j]){ Z=0; } } if(Z){ //cout << i << endl; cYes;return 0; } } } cNo; }
#include <bits/stdc++.h> #include <iostream> using namespace std; #define ll long long #define ld long double #define mp make_pair #define pb push_back #define fo(i,n) for(ll i=0;i<n;i++) #define fo1(i,n) for(ll i=1;i<=n;i++) #define loop(i,a,b)for(ll i=a;i<=b;i++) #define loopr(i,a,b)for(ll i=b;i>=a;i--) #define vll vector<ll> #define vvl vector<vll> #define vvvl vector<vvl> #define pii pair<ll,ll> #define piii pair<ll,pair<ll,ll>> #define F first #define S second #define M 1000000007 ll max(ll a,ll b){if (a>b) return a; else return b;} ll min(ll a,ll b){if(a<b) return a; return b;} ll mod=998244353; vll pr; void seive(ll n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int p = 2; p <= n; p++) if (prime[p]) pr.pb(p); } ll powermod(ll x,ll y,ll p) { ll res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; // y = y/2 x = (x*x) % p; } return res; } ll gcd(ll x, ll y){ if (y == 0) return x; else return gcd(y, x % y); } ll nck(ll n, ll k){ ll C[k + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (ll i = 1; i <= n; i++) { for (ll j = min(i, k); j > 0; j--) C[j] = (C[j]%M + C[j - 1]%M)%M; } return C[k]%M; } void call(vll &primefactor,int N){ for(int i=1;i<N;i++) { primefactor[i]=i; } for(int i=2;i<N;i++) { if(primefactor[i]!=i) continue; for(int j=i;j<N;j+=i) primefactor[j]=i; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t=1; //cin>>t; while(t--){ ll n; cin>>n; vll v(n); ll s=0; fo(i,n){ cin>>v[i]; s+=v[i]; } sort(v.begin(),v.end()); vll run(n); ll ss=0; fo(i,n){ ss+=v[i]; run[i]=ss; } ld ans=1e18; fo(i,n){ ld x=(ld)v[i]/2.0; while(i<n && v[i]==x*2) i++; i--; // cout<<i<<" "<<v[i]<<endl; ld num=n*x+s-(n-i-1)*v[i]-run[i]; ld an=num/n; if(an<ans) ans=an; } cout<<setprecision(8)<<ans<<endl; } return 0; }
#include<bits/stdc++.h> #define reg register typedef long long ll; using namespace std; inline int qr(){ int x=0,f=0;char ch=0; while(!isdigit(ch)){f|=ch=='-';ch=getchar();} while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return f?-x:x; } const int N=1e5+100; int n; double a[N]; int main(){ n=qr(); for(reg int i=1;i<=n;i++){ scanf("%lf",&a[i]); } sort(a+1,a+n+1); int mid=(1+n)/2; double x=a[mid]/2; double ans=0; for(reg int i=1;i<=n;i++){ ans+=x+a[i]-min(x*2,a[i]); } printf("%.7f",ans/n); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define F first #define S second #define ALL(a) a.begin(),a.end() #define mp make_pair #define pb push_back #define FOR(i,a,b) for(int i=a;i<b;i++) #define FORN(i,a,b) for(int i=a;i<=b;i++) #define FORB(i,a,b) for(int i=a;i>=b;i--) #define INT_MAXI 2147483647 #define INT_MINI -2147483648 #define REV(a) reverse(a.begin(),a.end()) #define NP(a) next_permutation(a.begin(),a.end()) typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef vector<int> vi; typedef vector<ll> vl; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int x, y; cin >> x >> y; if(x > y) swap(x, y); if(y - x < 3) cout << "Yes\n"; else cout << "No\n"; return 0; }
/* @memendra_singh */ #include<bits/stdc++.h> using namespace std; #define m7 1000000007 #define m9 1000000009 #define ll long long #define boost ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) #define F first #define S second #define pb push_back #define f1(i,n) for(int i=0;i<n;i++) #define all(c) c.begin(),c.end() #define ldb long double #define pi acos(-1); bool check(int x,vector<vector<int>> &vec,int n) { set<int> st; for(int i=0;i<n;i++) { int bit = 0; for(int j=0;j<5;j++) { if(vec[i][j] >= x) { bit = bit|(1<<j); } } st.insert(bit); } for(auto a : st) for(auto b : st) for(auto c : st) if((a|b|c) == 31) return 1; return 0; } int main() { boost; int t = 1; //cin>>t; while(t--) { int n; cin>>n; vector<vector<int>> vec(n,vector<int>(5)); f1(i,n) { f1(j,5) { cin>>vec[i][j]; } } int low = 0,high = 1000000000; int mid; while(low < high) { mid = (low+high+1)/2; if(check(mid,vec,n)) { low = mid; } else { high = mid-1; } } cout<<low<<'\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll i,j,n,m; scanf("%lld%lld",&n,&m); pair<ll,ll>p[m+1]; for(i=0;i<m;++i) scanf("%lld%lld",&p[i].first,&p[i].second); sort(p,p+m); set<ll>s; s.insert(n); p[m].first=1e10; queue<ll>q1,q2; for(i=0;i<m;++i){ if(s.find(p[i].second)!=s.end()) q1.push(p[i].second); if(s.find(p[i].second+1)!=s.end()||s.find(p[i].second-1)!= s.end()) q2.push(p[i].second); if(p[i].first!=p[i+1].first){ while(q1.size()) s.erase(q1.front()),q1.pop(); while(q2.size()) s.insert(q2.front()),q2.pop(); } } printf("%lld",s.size()); return 0; }
#include<bits/stdc++.h> #define For(a,b,c) for(int a=b;a<=c;++a) #define Dor(a,b,c) for(int a=b;a>=c;--a) using namespace std; typedef long long LL; const int N=1007; int n,m,c,a,vH[N],vL[N]; char G[N][N]; void flyL(int); void flyH(int u) { if(vH[u]) return; vH[u]=c; For(v,1,m) if(G[u][v]=='#') flyL(v); } void flyL(int u) { if(vL[u]) return; vL[u]=c; For(v,1,n) if(G[v][u]=='#') flyH(v); } int main() { scanf("%d%d",&n,&m); For(i,1,n) scanf("%s",G[i]+1); G[1][1]=G[n][1]=G[1][m]=G[n][m]='#'; For(i,1,n) if(!vH[i]) ++c,flyH(i); For(i,1,n) vH[i]=0; For(i,1,m) vL[i]=0; a=c,c=0; For(i,1,m) if(!vL[i]) ++c,flyL(i); printf("%d",min(c-1,a-1)); return 0; }
#include <bits/stdc++.h> #define rep(i,l,r) for(int i=(l); i<(r); ++i) using ll = long long int; using namespace std; const int INF = 1234567890; int main(){ //入力 int x, y; cin >> x >> y; //処理 if(x>y) cout << (x<y+3?"Yes":"No") << endl; else cout << (x+3>y?"Yes":"No") << endl; //出力 return 0; }
#include <iostream> #include<bits/stdc++.h> using namespace std; #define ll long long int void solve(){ int a,b; cin>>a>>b; if(a==b){ cout<<a<<endl; }else{ cout<<3-a-b<<endl; } return; } int main() { int test =1; // cin>>test; while(test--){ solve(); } return 0; }
#include<iostream> using namespace std; int p[][4]={{0,0,0,0},{1,1,1,1},{6,2,4,8},{1,3,9,7},{6,4,6,4},{5,5,5,5},{6,6,6,6},{1,7,9,3},{6,8,4,2},{1,9,1,9}}; int f(int b,int c)//快速幂 { int ans=1; while(c) { if(c%2==1) { ans*=b%4; c--; } b=b*b%4; c/=2; } return ans; } int main() { int a,b,c; int temp; cin>>a>>b>>c; a%=10; temp=f(b,c); cout<<p[a][temp]<<endl; return 0; }
/** * * @Author : Sindys * Language : C++ */ #include <iostream> #include <vector> #include <algorithm> #include <string> #include <queue> #include <iomanip> #include <map> #include <set> #include <limits>//numeric_limits<int>::max() ... numeric_limits<int>::min() #include <cmath> #include <bitset> #include <tuple> #include <deque> #include <stack> using namespace std; #define ll long long #define pii pair<int, int> #define vpii vector<pii> #define vi vector<int> #define vd vector<double> #define vll vector<ll> #define INF 1000000000 #define pb push_back #define FOR(i,a,b,p) for (int i = a; i < b; i += p) #define ROF(i,b,a,p) for (int i = b-1; i >= a; i -= p) #define mp make_pair #define f first #define s second #define FORX(a,b,n) for (; a < b; a += n)//for every +n operation #define ROFX(a,b,n) for (; a > b; a -= n)//for every -n operation const int M = 1000000007; //memset(memo, -1, sizeof memo); // initialize DP memoization table with -1 //memset(arr, 0, sizeof arr); // to clear array of integers // index = (index + 1) % n; // index++; if (index >= n) index = 0; // index = (index + n - 1) % n; // index--; if (index < 0) index = n - 1; // int ans = (int)((double)d + 0.5); // for rounding to nearest integer void solve() { long double x, y, r; cin>>x>>y>>r; ll res = 0; ll highestPointY = floor( y + r ); ll lowestPointY = ceil( y - r); r = nextafter(r, INFINITY); for(ll i = highestPointY; i >= lowestPointY; i--) { long double width = sqrt( r * r - abs(i - y) * abs(i - y)); res += floor(x + width) - ceil(x - width) + 1; } cout<<res<<'\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); } /*stuff you should look for: -->overflow,array bounds -->special cases n=1.. -->use printf for precision or setprecision on cout*/
#include<bits/stdc++.h> #define lint long long int #define rep(i,n) for(int i=0;i<int(n);i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define arep(i,a,n) for(int i=a;i<n;i++) #define sort(a) sort(a.begin(),a.end()) #define reverse(a) reverse(a.begin(),a.end()) #define fill(a,x) fill(a.begin(),a.end(),x) #define eb(data) emplace_back(data) #define pb(data) emplace_back(data) #define mp make_pair #define ALNUM 26 #define vint vector<int> #define vlint vector<lint> #define F first #define S second #define ALL(data) data.begin(),data.end() #define GEts(s) getline(cin,s); #define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end()) using namespace std; template<typename Rast>inline void out(Rast rast){cout<<rast<<"\n";return;} template<typename Rast>inline void in(Rast& rast){cin>>rast;return;} template<typename T>istream& operator >> (istream& is, vector<T>& vec){for(T& x: vec) is >> x;return is;} template<typename First, typename... Rest>void in(First& first, Rest&... rest){cin >> first;in(rest...);return;} template<typename First, typename... Rest>void out(First first, Rest... rest){cout << first<<" ";out(rest...);return;} template<typename T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);} template<typename T>T lcm(T a,T b){return a * b / gcd(a, b);} template<typename T1,typename T2>bool chmax(T1& a,T2 b){if(a<b){a=b;return true;}else{return false;}} template<typename T1,typename T2>bool chmin(T1& a,T2 b){if(a>b){a=b;return true;}else{return false;}} static const double pi = 3.141592653589793; int modpow(int a,int n,int p){if (n==0)return 1%p; if(n==1)return a%p;if(n%2==1)return (a*modpow(a,n-1,p))%p;lint t=modpow(a,n/2,p);return (t*t)%p;}//a^n%p lint MOD=1e9+7; //lint MOD=998244353; lint inf=pow(2,50); int intinf=pow(2,30); /**int dirx[]={1,0};int diry[]={0,1};//*///右、下 /**/int dirx[]={0,1,0,-1};int diry[]={-1,0,1,0};//*///四方位 /**int dirx[]={-1,0,1,1,1,0,-1,-1};int diry[]={-1,-1,-1,0,1,1,1,0};//*///八方位 class unionfind{ public: vector<int> table; void init(int size){ table.resize(size); rep(i,size)table[i]=i; }; int root(int index){ if(table[index]==index)return index; else{ int hoge=root(table[index]); table[index]=hoge; return hoge; } }; bool same(int x,int y){ return(root(x)==root(y)); }; int marge(int x,int y){ int yroot=root(y); int xroot=root(x); if(xroot==yroot)return 0; table[yroot]=xroot; return 0; } int getsize(){ set<int> ma; rep(i,table.size())ma.insert(root(i)); int re=ma.size(); return re; } }; int main(){ cin.tie(0);ios::sync_with_stdio(false);cout<<std::fixed<<std::setprecision(16); int a,b; in(a,b); arep(x,-100,101)arep(y,-100,101){ if(x+y==a&&x-y==b){ out(x,y); break; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long vector<ll> prime_numbers = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}; int n = prime_numbers.size(); int main() { ios::sync_with_stdio(0); cin.tie(0); ll A, B; cin >> A >> B; ll N = B-A+1; vector<vector<ll>> dp(N+1, vector<ll>(1<<n, 0LL)); dp[0][0] = 1LL; vector<int> pre(N); for (ll i=A;i<=B;i++) { int ret = 0; for (int j=0;j<n;j++) { if (i%prime_numbers[j] == 0) { ret += (1<<j); } } pre[i-A] = ret; } for (ll i=A;i<=B;i++) { for (int j=0;j<(1<<n);j++) { if (dp[i-A][j] == 0) continue; dp[i-A+1][j] += dp[i-A][j]; if ((j&pre[i-A]) != 0) continue; dp[i-A+1][j|pre[i-A]] += dp[i-A][j]; } } ll ans = 0LL; for (int j=0;j<(1<<n);j++) ans += dp[N][j]; cout << ans << "\n"; return 0; }
// ###### ### ####### ####### ## # ##### ### ##### // // # # # # # # # # # # # # # ### // // # # # # # # # # # # # # # ### // // ###### ######### # # # # # # ######### # // // # # # # # # # # # # #### # # # // // # # # # # # # ## # # # # # // // ###### # # ####### ####### # # ##### # # # # // // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") // #pragma GCC target("avx,avx2,fma") #include "bits/stdc++.h" using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double lld; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> ppl; #define ain(a,n) for(ll i=0;i<(n);++i) cin>>(a)[i]; #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define loop(i,n) for(ll i=0;i<(n);++i) #define FOR(i,a,b) for(ll i=(a);i<=(b);++i) #define FORD(i,a,b) for(ll i=(a);i>=(b);--i) #define cases ll T=0;cin>>T;while(T--) #define ff first #define ss second #define all(v) v.begin(),v.end() #define END "\n" #define pb push_back #define mp make_pair #define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr) // #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr) #define PI 3.14159265359 #define inf 9e18 #define MOD 1000000007 #define MODU 998244353 #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define MAXN 1000005 #define BLOCK 555 const string alpha = "abcdefghijklmnopqrstuvwxyz"; const ll N = 1000005; const long double epsilon = 1e-9; inline ll myceil(ll a, ll b) { return (ceil(1.0 * a / b * 1.0)); } ll binexp(ll a, ll b, ll m) { a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } ll modinvfermat(ll a, ll m) { return binexp(a, m - 2, m); } void task(bool flag) { if (flag) cout << "YES\n"; else cout << "NO\n"; } ll lcm(ll a, ll b) { return ((1LL * a * b) / (__gcd(a, b))); } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast ll n, m; cin >> n >> m; vector<vector<char>> grid(n, vector<char>(m)); loop(i, n) loop(j, m) cin >> grid[i][j]; ll ans = 0; loop(i, n) { ll len = 0, j = 0; while (j < m) { if (grid[i][j] == '.') { len = 0; while (j < m && grid[i][j] == '.') ++j , ++len; ans += len - 1; } else ++j; } } loop(j, m) { ll len = 0, i = 0; while (i < n) { if (grid[i][j] == '.') { len = 0; while (i < n && grid[i][j] == '.') ++i , ++len; ans += len - 1; } else ++i; } } cout << ans << END; #ifndef ONLINE_JUDGE cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n"; #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define int long long int #define mod 1000000007 #define vi vector<int> #define pint pair<int,int> #define vint vector<vector<int>> #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); const int N = 1e6+5; int A[N], B[N], P[N], Q[N][3], D[N][3]; string S[105]; int mat[5005][5005]; pint edges[N]; unordered_map<int, set<int>> m; bool compare(vi a, vi b) { return a[0] < b[0]; } int lcm(int a, int b) { return (a*b)/(__gcd(a,b)); } int mypow(int a, int x) { if(a<=1 || x==1) return a; if(x==0) return 1; int temp = mypow(a,x/2); if(x%2==0) return ((temp%mod) * (temp%mod))%mod; else return (a%mod * ((temp%mod) * (temp%mod))%mod)%mod; } int count_bits(int n) { int cnt = 0; while(n) { n = n & (n-1); cnt++; }return cnt; } void bfs(int src, int n) { unordered_map<int, bool> visited; for(int i=0;i<n;i++) visited[i] = false; queue<int> q; q.push(src); visited[src] = true; while(!q.empty()) { int curr = q.front(); q.pop(); B[curr] -= min(B[curr], A[src]); //cout<<curr<<" "<<endl; for(auto neigh: m[curr]) { if(visited[neigh] == false) { //cout<<neigh<<endl; q.push(neigh); visited[neigh] = true; m[src].erase(neigh); m[neigh].erase(src); } } } } int length_of_lis(int n) { if(n<=1) return n; vector<int> res; res.push_back(A[0]); int i; for(i=1;i<n;i++) { if(A[i] > res.back()) res.push_back(A[i]); else { int index = lower_bound(res.begin(), res.end(), A[i]) - res.begin(); res[index] = A[i]; } }return res.size(); } int solve(int h, int w) { int i,j,k; int ans = 0; for(i=0;i<h;i++) { for(j=0;j<w-1;j++) { if(S[i][j] == '.' && S[i][j+1] == '.') ans++; } } for(i=0;i<w;i++) { for(j=0;j<h-1;j++) { if(S[j][i] == '.' && S[j+1][i] == '.') ans++; } } return ans; } int32_t main() { //code IOS; int t,n,m,a,b,x,y,i,j,z; //cin>>t; t = 1; while(t--) { int h,w; cin>>h>>w; for(i=0;i<h;i++) cin>>S[i]; cout<<solve(h,w)<<endl; } return 0; }
#include<bits/stdc++.h> using namespace std; const int N=100005,M=1000000007; int n,i,a,b,sa[N],st[N],sc[N],sg[N],ans,j; char c[N]; int main() { scanf("%d %s",&n,c+1); for(i=1;i<=n;++i) { sa[i]=sa[i-1]; sc[i]=sc[i-1]; st[i]=st[i-1]; sg[i]=sg[i-1]; if(c[i]=='A') ++sa[i]; if(c[i]=='T') ++st[i]; if(c[i]=='G') ++sg[i]; if(c[i]=='C') ++sc[i]; } for(i=1;i<=n;++i) for(j=i-1;j>=0;--j) if(sa[i]-sa[j]==st[i]-st[j]&&sg[i]-sg[j]==sc[i]-sc[j]) ++ans; cout<<ans; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; using pll = pair<ll, ll>; using vll = vector<ll>; #define endl "\n" #define space " " #define TLE_na_ho ios_base::sync_with_stdio(false);cin.tie(NULL) #define MAX_SIZE 1024 #define MOD 1000000007 #define pb push_back #define fi first #define se second #define all(v) v.begin(),v.end() #define f(n) for(ll i=0;i<n;i++) #define rep(i,a,n) for(ll i=a;i<n;i++) #define repe(i,a,n) for(ll i=a;i<=n;i++) #define repr(i,a,n) for(ll i=a;i>=n;i--) #define ain(arr,n) for(ll i1=0;i1<n;i1++ ) cin>>arr[i1] #define aout(arr,n) for(ll i1=0;i1<n;i1++ ) cout<<arr[i1]<<space ll testcases; int fact(int n); int nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } // Returns factorial of n int fact(int n) { int res = 1; for (int i = 2; i <= n; i++) res = res * i; return res; } int main() { TLE_na_ho; //READ THE QUESTION PROPERLY!! testcases = 1; //cin>>testcases; ll n; cin >> n; char s[n]; ain(s, n); ll ans = 0, cont1 = 0, cont2 = 0; rep(i, 0, n - 1) { if(s[i] == 'A') { if(s[i + 1] == 'T') { ans++; } } else if(s[i] == 'T') { if(s[i + 1] == 'A') { ans++; } } else if(s[i] == 'C') { if(s[i + 1] == 'G') { ans++; } } else if(s[i] == 'G') { if(s[i + 1] == 'C') { ans++; } } } for(ll i = 1; i < n; i++) { if(s[i] == 'A') { if(s[i - 1] == 'T') { cont1++; i++; } } if(s[i] == 'T') { if(s[i - 1] == 'A') { cont1++; i++; } } if(s[i] == 'C') { if(s[i - 1] == 'G') { cont2++; i++; } } if(s[i] == 'G') { if(s[i - 1] == 'C') { cont2++; i++; } } } ll ans2 = 0; map<ll, ll>cnt; rep(i, 0, n) { //cout << "##" << i << space << cnt[0] << space << cnt[19] << space << cnt[2] << space << cnt[6] << endl; if(cnt[0] == cnt[19] && cnt[2] == cnt[6]) { if(cnt[0] + cnt[2] >= 2) ans2++; } cnt.clear(); rep(j, i, n) { if(cnt[0] == cnt[19] && cnt[2] == cnt[6]) { if(cnt[0] + cnt[2] >= 2) ans2++; } cnt[s[j] - 'A']++; } } cout << ans + ans2; }
#include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<algorithm> using namespace std; typedef int int_; #define int long long #define maxn 400010 const int inf=1e9+7; queue<int> q; inline int read(){ int ret=0,ff=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') ff=-ff; ch=getchar(); } while(ch>='0'&&ch<='9'){ ret=ret*10+ch-'0'; ch=getchar(); } return ret*ff; } struct Edge{ int u,v,w,next; }E[maxn<<1]; int ecnt=0,head[maxn]; int in[maxn]; void addedge(int u,int v){ ++in[v]; E[++ecnt].u=u; E[ecnt].v=v; E[ecnt].next=head[u]; head[u]=ecnt; } void Addedge(int u,int v){ addedge(u,v); addedge(v,u); } int gcd(int a,int b){ return b?gcd(b,a%b):a; } int ksm(int a,int b,int p){ int ret=1; while(b){ if(b&1) ret=ret*a%p; a=a*a%p; b>>=1; } return ret%p; } bool cmp(int x,int y){ return x>y; } int a[10010]; int G2(int x){ int cnt=0; while(x){ a[++cnt]=x%10; x/=10; } sort(a+1,a+1+cnt); int ret=0; for(int i=1;i<=cnt;++i) ret=ret*10+a[i]; return ret; } int G1(int x){ int cnt=0; while(x){ a[++cnt]=x%10; x/=10; } sort(a+1,a+1+cnt,cmp); int ret=0; for(int i=1;i<=cnt;++i) ret=ret*10+a[i]; return ret; } int F(int x){ return G1(x)-G2(x); } char X[10010]; bool ck(int Mid,int len,int m){ int sum=0,tmp=1; for(int j = len; j;--j){ sum+=tmp*(X[j]-'0'); tmp*=Mid; } return sum>m; } int dp[2][110][110][110]; int_ main(){ int n=read(),x=read(); for(int i=1;i<=n;++i) a[i]=read(); memset(dp, -1, sizeof dp); for(int i=1;i<=n;i++) dp[0][i][0][0] = 0; for(int i=0;i<n;i++){ int qwq=i&1; for(int j =1;j<=n;j++){ for(int qaq =0;qaq<=i;qaq++){ for(int res=0;res<j;res++){ if(dp[qwq][j][qaq][res]==-1) continue; dp[qwq^1][j][qaq+1][(res+a[i+1])%j]=max(dp[qwq^1][j][qaq+1][(res+a[i+1])%j],dp[qwq][j][qaq][res]+a[i+1]); dp[qwq^1][j][qaq][res]=max(dp[qwq^1][j][qaq][res], dp[qwq][j][qaq][res]); } } } } int qwq=n&1,ans=x; for(int qaq=1;qaq<=n;qaq++){ if(dp[qwq][qaq][qaq][x%qaq]>0&&x>=dp[qwq][qaq][qaq][x%qaq])ans=min(ans,(x-dp[qwq][qaq][qaq][x%qaq])/qaq); } printf("%lld",ans); return 0; } /* 1 6 3 */ /* 111111111111111111111111111111111111111111111111111111111111 1000000000000000000 100000000000000000000000000000000000000000000000000000000000 1000000000000000000 2 1000000000000000000 */
#include<bits/stdc++.h> typedef long long int ll; typedef long double ld; typedef std::vector<ll> vi; typedef std::vector<std::vector<ll> > vv; typedef std::vector<std::pair<ll,ll> > pii; typedef std::pair<ll,ll> pi; #define mod 1000000007 #define IO ios_base::sync_with_stdio(false);cin.tie(NULL); #define fo(i,a,b) for(i=a;i<b;i++) #define forr(i,a,b) for(i=a;i>=b;i--) #define mp make_pair #define pb(x) push_back(x) #define fi first #define se second #define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl; #define all(a) a.begin(),a.end() #define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i]; #define ms(a,val) memset(a,val,sizeof(a)) using namespace std; const int N = 2e5+ 5; ll expo_pow(ll x, ll y) { if (y == 0) return 1; y = y % (mod - 1); x %= mod; if (y == 0) y = mod - 1; ll res = 1; while (y) { if (y & 1) res = (res * x) % mod; x = (x * x) % mod; y >>= 1; } return res; } ll modInverse(ll a, ll m = mod) { return expo_pow(a, m - 2); } ll dp[2001][2001]; bool done[2001][2001]; string s[2001]; ll h,w; ll cal(ll i, ll j) { if(done[i][j]) return dp[i][j]; if(i >= h || j >= w) return INT_MAX; if(i == h-1 && j == w-1) { dp[i][j] = 0; done[i][j] = 1; return dp[i][j]; } ll down = INT_MAX, right = INT_MAX; if(i+1 < h) down = cal(i+1,j) + (s[i+1][j] == '+' ? -1:1); if(j+1 < w) right = cal(i,j+1) + (s[i][j+1] == '+' ? -1:1); done[i][j] = 1; dp[i][j] = -min(down, right); return dp[i][j]; } void solve(){ ll i,j,n,m,k; cin>>h>>w; fo(i,0,h) cin>>s[i]; if(cal(0,0) > 0) { cout<<"Takahashi"<<endl; } else if(cal(0,0) < 0) { cout<<"Aoki"<<endl; } else cout<<"Draw"<<endl; } int main() { IO; ll t=1,i; // cin>>t; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long const int N=1e5+3; template <typename T> inline void read(T &x){ x=0;char ch=getchar();bool f=false; while(!isdigit(ch)) f|=ch=='-',ch=getchar(); while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); x=f?-x:x; } template <typename T> inline void print(T x){ if(x<0) putchar('-'),x=-x; if(x>9) print(x/10); putchar(x%10^48); } int a,b; int main(){ read(a),read(b); a=a+b; if(a>=15&&b>=8){ putchar('1');return 0; } if(a>=10&&b>=3){ putchar('2');return 0; } if(a>=3){ putchar('3');return 0; } putchar('4'); return 0; }
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<class T> ostream &operator<<(ostream &os, const vector<T> &x){ os << "{"; for(size_t i = 0; i < x.size(); i++){ if(i < x.size()-1) os << x[i] << ", "; else os << x[i]; } os << "}"; return os; } int main(void){ int a, b, c; cin >> a >> b >> c; cout << 21-(a+b+c) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define each(e, v) for(auto &e: v) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; //const int MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; struct io_setup{ io_setup(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; int main(){ int N; cin >> N; vector<ll> A(2*N); rep(i, 2*N) cin >> A[i]; vector<int> p(2*N); vector<int> v(2*N); iota(all(v), 0); sort(all(v), [&](int i, int j){ return A[i] > A[j]; }); set<int> s1[2], s2[2]; vector<int> u(2*N, 0); rep(i, N){ s1[v[i]&1].emplace(v[i]); u[v[i]] = 1; } rep(i, N){ s2[v[N+i]&1].emplace(v[N+i]); } vector<bool> used(2*N, false); rep(i, 2*N){ if(used[i]) continue; if(u[i] == 0){ int j = *s1[(i&1)^1].begin(); s2[(i&1)].erase(i); s1[(i&1)^1].erase(j); p[i] = j, p[j] = i; used[i] = true, used[j] = true; } else{ int j = *s2[(i&1)^1].begin(); s1[(i&1)].erase(i); s2[(i&1)^1].erase(j); p[i] = j, p[j] = i; used[i] = true, used[j] = true; } } //rep(i, 2*N) cout << u[i] << ' '; cout << '\n'; //rep(i, 2*N) cout << p[i] << ' '; cout << '\n'; string ans(2*N, ')'); rep(i, 2*N){ if(i < p[i]) ans[i] = '('; } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; const int N=4e5+2; int stk[N],top; int n,m,p,e,a[N],b[N]; int main() { scanf("%d",&n); m=n<<1; for(int i=1;i<=m;++i) { scanf("%d",&a[i]); b[i]=a[i]; } sort(b+1,b+m+1); for(int i=1;i<=m;++i) if(a[i]<b[n])++p; else if(a[i]>b[n])++e; for(int i=1;i<=m;++i) { int fl=0; if(a[i]>b[n])fl=1; else if(a[i]==b[n]&&p<n)++p; else if(a[i]==b[n]&&p==n)fl=1,++e; //else if(a[i]==b[n]&&!top)fl=1,++e; if(top&&stk[top]!=fl)putchar(')'),--top; else stk[++top]=fl,putchar('('); } }
#include <bits/stdc++.h> using namespace std; ////////////////////////////////////////////////////////////////////////////<editor-fold desc="macros"> #define mod 1000000007 #define endl "\n" #define long long long #define all(v) (v).begin(),(v).end() #define makeset(v) (v).resize(unique((v).begin(),(v).end())-(v).begin()) #define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define filein freopen("input.txt", "r", stdin); #define fileout freopen("output.txt", "w", stdout); #define getLine(x) getline(cin,x) #define makelower(s) transform(s.begin(),s.end(),s.begin(),::tolower) bool sortby(pair<int, int> a, pair<int, int> b) { double x = (double) a.first / a.second; double y = (double) b.first / b.second; return x < y; } long powUnderMod(long x, long y) { if(y == 0)return 1; long res = powUnderMod(x, y / 2) % mod; res = (res * res) % mod; if(y & 1)res = (res * x) % mod; return res; } #define printcase(x) cout<<"Case "<<in++<<": "<<x<<endl #define memset(a, b) memset(a,b,sizeof(a)) #define print1d(ary) cout<<"[";for(auto x:(ary)){cout<<x<<",";}cout<<"]"<<endl; #define print2d(x) for(int m = 0; m <sizeof(x)/sizeof(x[0]) ; ++m){for(int l = 0; l <sizeof(x[0])/sizeof(x[0][0]) ; ++l){cout<<x[m][l]<<" ";}cout<<endl;} #define pi acos(-1) #define esp 1e-9 ////////////////////////////////////////////////////////////////////////////</editor-fold> long n, m, f = 0, q, ans; long tmp, flag = 0; int main() { ///<editor-fold desc="ios"> //IOS #ifdef _AD_ filein //fileout #endif ///</editor-fold> int in = 1, o; cin>>n; vector<long> v; double sum=0; for(int i = 0; i < n; ++i){ cin>>tmp; v.push_back(tmp); sum+=tmp; } double avg=sum/2; sort(all(v)); long mx=0; for(int i = v.size()-1; i >=0 ; --i){ long count=0; for(int j = i; j >=0 ; --j){ if(count+v[j]<=avg)count+=v[j]; } mx=max(count,mx); } cout<<(long)sum-mx<<endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i,n) for (int i = 0; i < (n); ++i) void solve(long long N, std::vector<long long> A){ ll ans = 1E18; rep(i,1<<N){ if(i == 0) continue; ll r = 0; vector<ll> d; rep(j,N){ ll js = i >> j & 1; r |= A[j]; if(js) { d.push_back(r); r = 0; } } if(~ i >> (N-1) & 1) d.push_back(r); ll t = d[0]; for(ll j = 1; j<d.size(); j++){ t ^= d[j]; } ans = min(ans,t); } cout << ans << endl; } int main(){ long long N; scanf("%lld",&N); std::vector<long long> A(N); for(int i = 0 ; i < N ; i++){ scanf("%lld",&A[i]); } solve(N, std::move(A)); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ long n, a[100000], i; cin >> n; double dn = n, x2 = 2; for(i = 0; i < n; i++) cin >> a[i]; if(n == 1) cout << fixed << setprecision(15) << a[0] / x2 << endl; else if(n == 2) cout << fixed << setprecision(15) << max(a[0], a[1]) / x2 << endl; else{ sort(a, a + n); long hsum = 0; x2 = a[int(n / 2)] / x2; for(i = n / 2; i < n; i++) hsum += a[i]; if(n % 2 == 0){ cout << fixed << setprecision(15) << hsum / dn << endl; } else{ cout << fixed << setprecision(15) << x2 + (hsum - int((n + 1)/ 2) * a[int(n / 2)]) / dn << endl; } } return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i, n) for (int i = 0; i < (n); ++i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) using namespace std; // using namespace atcoder; typedef long long int ll; typedef pair<int, int> P; #define MAX_N 200005 int main() { int n; cin >> n; double a[n]; double sum = 0; rep(i, n) { cin >> a[i]; sum += a[i]; } sort(a, a + n); double ans = sum / n; rep(i, n) { sum -= a[i]; double tmp = sum - a[i] * (n - 1 - i) + a[i] * n / 2; tmp /= n; ans = min(ans, tmp); } cout << fixed << setprecision(10) << ans << endl; return 0; }