code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#if _MSC_VER #define _CRT_SECURE_NO_WARNINGS #endif #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> using namespace std; #ifndef ONLINE_JUDGE const bool DEBUG = true; #else const bool DEBUG = false; #endif void read() {} template<class T> void read(vector<T>& var) { for (int i = 0; i < (int)var.size(); ++i) cin >> var[i]; } // Reads arbitrary values template <class T1, class... T2> void read(T1& var1, T2&... var2) { cin >> var1; read(var2...); } void pln() { cout << (DEBUG ? "]\n" : "\n"); } template <class T1, class... T2> void pln(const T1& var1, const T2& ... var2) { cout << var1 << " "; pln(var2...); } // Prints arbitrary values and goes to next line template <class T> void print(vector<T>& arg) { cout << (DEBUG ? " [ " : ""); for (auto& i : arg) cout << i << " "; cout << (DEBUG ? "]\n" : "\n"); } template <class T1, class... T2> constexpr void print(const T1& var1, const T2& ... var2) { cout << (DEBUG ? " [ " : ""); pln(var1, var2...); } // Prints arbitrary values and goes to next line ONLY IF IN DEBUG MODE template <class T1, class... T2> constexpr void debug(const T1& var1, const T2& ... var2) { if (!DEBUG) return; cout << " debug : [ "; pln(var1, var2...); } template <class T> void debug(vector<T>& arg) { cout << (DEBUG ? " debug : [ " : ""); for (auto& i : arg) cout << i << " "; cout << (DEBUG ? "]\n" : "\n"); } using ll = long long; using ull = unsigned long long; using ld = long double; #define all(x) begin(x), end(x) #define SINGLE_TEST_CASE 01 void exec() { int a, b; cin >> a >> b; ll sum = 0; for (int i = 1; i <= a - 1; ++i) { cout << i << " "; sum += i; } for (int j = 1; j <= b - 1; ++j) { cout << -j << " "; sum -= j; } cout << 500 * 1001 << " "; cout << -sum - (500 * 1001); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (SINGLE_TEST_CASE) exec(); else { int n; cin >> n; while (n--) exec(); } return 0; }
/** * Dont raise your voice, improve your argument. * --Desmond Tutu */ #include <bits/stdc++.h> using namespace std; const bool ready = [](){ ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); return true; }(); using ld=long double; const ld PI = acos((ld)-1); using ll= long long; #define int ll #define all(v) (v).begin(), (v).end() #define fori(n) for(int i=0; i<int(n); i++) #define cini(i) int i; cin>>i; #define cins(s) string s; cin>>s; #define cind(d) ld d; cin>>d; #define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; } #define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; } #define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; } using pii= pair<int, int>; using pdd= pair<ld, ld>; using vd= vector<ld>; using vb= vector<bool>; using vi= vector<int>; using vvi= vector<vi>; using vs= vector<string>; #define endl "\n" /* Use the bigger number with 1,2,3...m=max(a,b), * sum=m*(m+1)/2 * Construct a seq with min(a,b) elements and that sum. */ void solve() { cini(a); cini(b); vi ans; for(int i=1; i<=max(a,b); i++) ans.push_back(i); for(int i=1; i<min(a,b); i++) ans.push_back(-i); int ma=max(a,b)*(max(a,b)+1)/2; int mi=(min(a,b)-1)*min(a,b)/2; ans.push_back(-(ma-mi)); if(a<b) { for(int i=0; i<a+b; i++) ans[i]=-ans[i]; } for(int i : ans) cout<<i<<" "; cout<<endl; } signed main() { solve(); }
#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 n; cin >> n; vector<int> t(n); rep(i, n) cin >> t[i]; vector<unordered_map<int, int>> dp(n+1); dp[0][0] = 1; const int INF = 1e5; rep(i, n){ for(int j = -INF; j <= INF; j++){ if(dp[i][j] == 0) continue; if(j-t[i] >= -INF) chmax(dp[i+1][j-t[i]], dp[i][j]); if(j+t[i] <= INF) chmax(dp[i+1][j+t[i]], dp[i][j]); } } int ans = INF; for(int j = -INF; j <= INF; j++){ if(dp[n][j] == 1) chmin(ans, abs(j)); } int sum = 0; rep(i, n) sum+=t[i]; ans = (ans+sum)/2; cout << ans << endl; return 0; }
#include <bits/stdc++.h> 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; } std::vector<std::string> split(const std::string& source, const char delim); int main(){ std::string buffer; std::getline(std::cin, buffer); const int NUM_DISH = std::stoi(buffer); std::getline(std::cin, buffer); auto splitCostTime = split(buffer, ' '); std::vector<int> costTimeList; for(const auto& costTime : splitCostTime){ costTimeList.emplace_back(std::stoi(costTime)); } std::array<std::unique_ptr<int[]>, 101> dp; for(int i = 0; i < NUM_DISH + 1; ++i){ dp[i] = std::make_unique<int[]>(101010); for(int t = 0; t < 101010; ++t){ dp[i][t] = INT_MAX; } } dp[0][0] = 0; for(int i = 0; i < NUM_DISH; ++i){ for(int t = 0; t < 101010; ++t){ if(dp[i][t] != INT_MAX){ chmin(dp[i + 1][t + costTimeList[i]], dp[i][t]); chmin(dp[i + 1][t], dp[i][t] + costTimeList[i]); } } } int ans = INT_MAX; for(int t = 0; t < 101010; ++t){ chmin(ans, std::max(t, dp[NUM_DISH][t])); } std::cout << ans << std::endl; return 0; } std::vector<std::string> split(const std::string& source, const char delim){ std::vector<std::string> splitData; std::stringstream sstream(source); std::string itemBuffer; while(std::getline(sstream, itemBuffer, delim)){ splitData.emplace_back(itemBuffer); } return splitData; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef struct nodes{ ll x,y,z; }node; node m[3000]; ll d[300][300]; ll dp[1<<18][18]; int main() { ll n; cin>>n; for(ll i=1;i<=n;i++) { cin>>m[i].x>>m[i].y>>m[i].z; } for(ll i=1;i<=n;i++) {for(ll j=1;j<=n;j++) { d[i][j]=abs(m[i].x-m[j].x)+abs(m[i].y-m[j].y)+max((ll)0,m[j].z-m[i].z); } } for(ll k=1;k<=n;k++) { for(ll i=1;i<=n;i++) { for(ll j=1;j<=n;j++) { d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } } } ll x=1<<n; for(int i=0;i<1<<18;i++) for(int j=0;j<18;j++) dp[i][j]=(ll)99999999999999; dp[1][0]=(ll)0; for(ll i=1;i<1<<n;i++) { for(ll j=0;j<n;j++) { if((i>>j&1)==0) { for(ll k=0;k<n;k++) if(i>>k&1) dp[i|1<<j][j]=min(dp[i][k]+d[k+1][j+1],dp[i|1<<j][j]); } } } ll ans=999999999999; for(int i=0;i<n;i++){ ans=min(ans,dp[(1<<n)-1][i]+d[i+1][1]); } cout<<ans; return 0; }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i, n) for(int i = 0; i < (n); ++i) #define rep1(i, n) for(int i = 1; i < (n); ++i) using namespace std; typedef long long ll; ll n; vector<ll> x, y, z; ll dp[1ll<<18][20]; const ll INF = 1ll<<60; ll rec(ll s, ll v){ if(dp[s][v] != 0) return dp[s][v]; if(s == (1<<n) - 1 && v==0) return 0; ll ret = INF; for(ll nv = 0; nv < n; ++nv){ if(s & (1<<nv)) continue; ret = min(ret, rec((s|(1<<nv)), nv) + abs(x[v]-x[nv]) + abs(y[v]-y[nv]) + max(0ll, z[nv]-z[v])); } return dp[s][v] = ret; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cin >> n; x.resize(n); y.resize(n); z.resize(n); rep(i, n) cin >> x[i] >> y[i] >> z[i]; cout << rec(0, 0) << endl; return 0; }
#include<bits/stdc++.h> #define ll long long #define sameer ios_base::sync_with_stdio(false);cin.tie(NULL); using namespace std; int main() { sameer; ll n; cin>>n; ll a[n],ans=0; for(int i=0; i<n; i++) cin>>a[i]; map<int, int> m; for(int i=0; i<n; i++) { ans= ans + m[a[i]-(a[i]/200)*200]++; } cout<<ans; return 0; }
#include<iostream> using namespace std; int main() { long long n,k; cin>>n>>k; while(k--) { (n%200==0)?n/=200:n=n*1000+200; } cout<<n; return 0; }
#include <iostream> #include <cmath> #include <climits> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <stack> #include <bitset> #include <functional> #include <random> #include <cassert> typedef long long ll; using namespace std; constexpr ll mod = 1000000007; constexpr ll mod998 = 998244353; int N, F[200000], T[200000], cnt; int par(int x) { if (T[x] == x)return x; else return T[x] = par(T[x]); } void uni(int x, int y) { x = par(x); y = par(y); T[x] = y; } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> F[i]; F[i]--; T[i] = i; } for (int i = 0; i < N; i++) { uni(i, F[i]); } for (int i = 0; i < N; i++) { if (i == T[i])cnt++; } ll res = 1; for (int i = 0; i < cnt; i++) { res *= 2; res %= mod998; } cout << (res - 1 + mod998) % mod998 << endl; }
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> #include<assert.h> #include<numeric> using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; constexpr int inf=1e9+7; constexpr ll longinf=1LL<<60 ; constexpr ll mod=998244353 ; struct UnionFind{ vector<int> par; UnionFind(int n):par(n,-1){} int find(int x){ if(par[x]<0)return x; return par[x]=find(par[x]); } bool unite(int x,int y){ x=find(x); y=find(y); if(x==y)return false; if(par[x]>par[y]){ par[y]+=par[x]; par[x]=y; } else{ par[x]+=par[y]; par[y]=x; } return true; } bool same(int x,int y){ return find(x)==find(y); } int size(int x){ return -par[find(x)]; } }; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin>>n; UnionFind uf(n); rep(i,n){ int x; cin>>x; uf.unite(i,x-1); } ll ans = 1; rep(i,n){ if(uf.find(i)==i)ans=ans*2%mod; } cout<<ans-1<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { cin.tie(nullptr); // Do not flush "cout" when processing "cin". string x; ll m; cin >> x >> m; // コーナーケース if ((int)x.size() == 1) { if (stoi(x) <= m) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; } // x に含まれる最も大きい数字 d int d = 0; for (char c : x) { d = max(d, c - '0'); } // 二分探索 ll ac = d; // 下限 ll wa = m + 1; // 上限 while (wa - ac > 1) { // wj = waiting judge ll wj = (ac + wa) / 2; ll v = 0; for (char c : x) { // v * wj > m を超えてはいけない // 両辺に wj を掛ける // // v > (m / wj) if (v > (m / wj)) { // (v > m) を満たすように、v に適当な値を代入する。 v = m + 1; } else { // 文字列 x を reverse する必要がない。 // // 1桁目 --> 0 * wj + (c - '0') // 2桁目 --> (c - '0') * wj + (c - '0') // 3桁目 --> (c - '0') * wj^2 + (c - '0') * wj^1 + (c - '0') v = v * wj + (c - '0'); } } if (v <= m) { ac = wj; } else { wa = wj; } } cout << ac - d << endl; return 0; }
#include"bits/stdc++.h" using namespace std; using ll=long long; template<class T=ll>inline T in(istream&is=cin){T ret;is>>ret;return ret;} template<class T,class Size=typename vector<T>::size_type>inline auto vector_2d(Size h,Size w,T v){return vector<vector<T>>(h,vector<T>(w,v));} template<class P>struct comp_pair_by_second{bool operator()(const P&p1,const P&p2){return p1.second<p2.second||(p1.second==p2.second&&p1.first<p2.first);}}; int main() { vector<ll>x_v; for(auto x:in<string>())x_v.push_back(x-'0'); const ll m=in(); if(x_v.size()==1){ cout<<(x_v[0]<=m?1:0)<<endl; }else{ auto x_leq_m=[x_v,m](ll base){ vector<ll>m_v; for(ll m_c=m;m_c;m_c/=base)m_v.push_back(m_c%base); while(m_v.back()==0)m_v.pop_back(); reverse(m_v.begin(),m_v.end()); return x_v.size()<m_v.size()|| x_v.size()==m_v.size()&&( lexicographical_compare(x_v.begin(),x_v.end(),m_v.begin(),m_v.end())|| equal(x_v.begin(),x_v.end(),m_v.begin()) ); }; if(ll base_min=*max_element(x_v.begin(),x_v.end())+1;!x_leq_m(base_min)){ cout<<0<<endl; }else{ ll left=base_min,right=numeric_limits<ll>::max(); while(right-left>1){ ll base=left+(right-left)/2; (x_leq_m(base)?left:right)=base; } cout<<right-base_min<<endl; } } }
#include <bits/stdc++.h> using namespace std; #define int long long int #define mod 998244353 #define pb push_back #define mp make_pair #define bs binary_search #define lb lower_bound #define ub upper_bound #define gcd __gcd #define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define min3(a,b,c) min(a,min(b,c)) #define max3(a,b,c) max(a,max(b,c)) #define all(i) i.begin(),i.end() #define rep(i,a,b) for(int i=a;i<b;i++) #define set_bit __builtin_popcount //gives number of set_bits #define ff first #define ss second #define cases(t) int t;cin>>t; rep(casn,1,t+1) #define el cout<<"\n"; #define deci(n) int n; scanf("%lld",&n); #define decs(n) string n; cin>>n; #define sz(v) v.size() #define repit(m) for(auto it=m.begin();it!=m.end();it++) #define cYES cout<<"YES\n"; #define cNO cout<<"NO\n"; #define ins insert #define imax 10000 #define imin 1000 #define mid 50000 #define NP 50000 // cerr << "\nTime : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n"; // IIITA //SUNEET KUMAR --> ਸੁਨੀਤ // scanning numbers extremely fast///////// void fs(int &number) { bool negative = false; register int c; number = 0; c = getchar(); if (c=='-') { negative = true; c = getchar(); } for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48; if (negative) number *= -1; } // ======================================== int power(int a,int x) { a%=mod; int res=1; while(x>0) { if(x&1) res=res*a%mod; a=a*a%mod; x>>=1; } return res; } int max(int a,int b) { if(a>b) { return a; } else { return b; } } int madd(int a,int b) { int ans; ans=(a%mod+b%mod)%mod; return ans; } int mmult(int a,int b) { int ans=1; ans=((a%mod)*(b%mod))%mod; return ans%mod; } int modinverse(int a) { return power(a,mod-2); } void its_her_choice() { int n;cin>>n; string s;cin>>s; stack<char>st; char c1,c2,c3; for(int i=0;i<s.length();i++) { st.push(s[i]); if(st.top()=='x') { st.pop(); if(!st.empty()&&st.top()=='o') { st.pop(); if(!st.empty()&&st.top()=='f') { st.pop(); } else { st.push('o'); st.push('x'); } } else { st.push('x'); } } } int count=0; while(!st.empty()) { st.pop(); count++; } cout<<count;el } signed main() { // cases(t) { its_her_choice(); } }
#include <bits/stdc++.h> using namespace std; #define int long long #define debug(x) cerr << #x << ": " << x << '\n' #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define ALL(obj) (obj).begin(), (obj).end() #define rALL(obj) (obj).rbegin(), (obj).rend() using ll = long long; using ld = long double; using intp = pair<int, int>; using intmap = map<int, int>; using intset = set<int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; constexpr int MOD = 1e9 + 7; constexpr int INF = 1 << 30; constexpr ll LINF = 1LL << 62; inline int mod(ll a, int m = MOD) { return (a % m + m) % m; } inline bool bit(ll b, ll i) { return b & (1 << i); } inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; } template<class T> bool chmin(T &a, T b) {if(a > b){a = b; return 1;} return 0;} template<class T> bool chmax(T &a, T b) {if(a < b){a = b; return 1;} return 0;} template <class T, T (*operation)(T, T), T (*e)()> class SegmentTree { vector<T> v; int n = 1; public: SegmentTree(int size) { while(n < size) n <<= 1; v.resize(n << 1, e()); } SegmentTree(const vector<T> &init_v) { n = 1; while(n < init_v.size()) n <<= 1; v.resize(n << 1, e()); for(int i = 0; i < init_v.size(); i++) { v[n + i] = init_v[i]; } for(int i = 2*n-2; i > 0; i -= 2) { v[i >> 1] = operation(v[i], v[i|1]); } } void update(int idx, int val) { idx += n; v[idx] = val; while(idx > 0) { idx >>= 1; v[idx] = operation(v[(idx << 1)|0], v[(idx << 1)|1]); } } T get(int l, int r) { T valL = e(), valR = e(); for(l += n, r += n; l < r; l >>= 1, r >>= 1) { if(l & 1) valL = operation(valL, v[l++]); if(r & 1) valR = operation(v[--r], valR); } return operation(valL, valR); } T get_all() { return v[1]; } T operator[](const int &i) const { return v[n + i]; } }; int f(int a, int b) { return a xor b; } int e() { return 0; } signed main() { int n, Q; cin >> n >> Q; vector<int> a(n); rep(i, n) { cin >> a[i]; } SegmentTree<int, f, e> tree(a); while(Q--) { int t; cin >> t; int x, y; cin >> x >> y; if(t == 1) { x--; tree.update(x, tree.get(x, x+1) xor y); } else if(t == 2) { x--; cout << tree.get(x, y) << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { string ufo; cin >> ufo; int compCount = 0; for(int i = 0; i+3 < ufo.size(); i++) { if(ufo.at(i) == 'Z' && ufo.at(i+1) == 'O' && ufo.at(i+2) == 'N' && ufo.at(i+3) == 'e') { compCount++; } } cout << compCount << endl; }
#include <iostream> using namespace std; bool check(int x, string s){ int size; size = s.size(); if (x + 4 > size){ return false; } if (s.substr(x, 4) == "ZONe"){ return true; }else{ return false; } } int main(){ string a; int count; count = 0; cin >> a; if (a.size() < 3){ cout << 0 << endl; return 0; } for(int i=0; i < a.size()-3; i++){ if (check(i, a)){ count += 1; } } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++) #define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--) #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define eb emplace_back using ll = long long; using vll = vector<ll>; using vi = vector<int>; using vvi = vector<vector<int>>; using P = pair<int, int>; using LD = long double; template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> void coutall(T v) { if(v.empty()){cout << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; } void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; } void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; } void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; } ll myceil(ll a, ll b) { return a >= 0 ? (a+b-1)/b : -((-a)/b); } ll myfloor(ll a, ll b) { return a >= 0 ? a/b : -myceil(-a, b); } int keta(ll x){ int res = 0; while(x){ x /= 10; res++; } return res; } void Main(){ ll n; cin >> n; if(n < 1000){ cout << 0 << endl; return; } int k = keta(n); ll p = pow(10ll, k-1); ll t = n / p; ll ans = ((n % p) + 1) * ((k-1)/3); ans += (t-1) * p * ((k-1)/3); //cout << k << " " << ans << endl; for(ll i = k-1; i >= 4; i--){ ans += ll((i-1)/3) * 9 * pow(10ll, i-1); //cout << i << " " << ll((i-1)/3) * 9 * pow(10ll, i-1) << endl; } cout << ans << endl; return; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); return 0; }
// clang-format off #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mp make_pair #define fst first #define snd second #define forn(i,n) for (int i = 0; i < int(n); i++) #define forn1(i,n) for (int i = 1; i <= int(n); i++) #define popcnt __builtin_popcount #define ffs __builtin_ffs #define ctz __builtin_ctz #define clz __builtin_clz #define all(a) (a).begin(), (a).end() using namespace std; using namespace __gnu_pbds; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pli = pair<ll,int>; using pil = pair<int,ll>; using pll = pair<ll,ll>; template <typename T> using vec = vector<T>; using vi = vec<int>; using vl = vec<ll>; template <typename T> using que = queue<T>; template <typename T> using deq = deque<T>; template <typename T> T id(T b) {return b;}; template <typename T> void chmax(T &x, T y) {if (x < y) x = y;} template <typename T> void chmin(T &x, T y) {if (x > y) x = y;} template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); } void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); } template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>; // clang-format on int n; int x[17], y[17], z[17]; int dist(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0, z[j] - z[i]); } int main() { fastio(); cin >> n; forn(i, n) cin >> x[i] >> y[i] >> z[i]; vec<vi> dp(1 << n, vi(n, INT_MAX)); dp[1][0] = 0; forn1(sz, n - 1) { int s = (1 << sz) - 1; while (s < 1 << n) { forn(last, n) { int c = dp[s][last]; if (c == INT_MAX) continue; forn(next, n) { if (last == next) continue; if ((s >> next) & 1) continue; int x = c + dist(last, next); if (sz == n - 1) { x += dist(next, 0); } auto t = s | (1 << next); chmin(dp[t][next], x); } } int x = s & -s, y = s + x; s = ((s & ~y) / x >> 1) | y; } } int ans = INT_MAX; forn(i, n) { chmin(ans, dp[(1 << n) - 1][i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long INF = 1000000000000000000; int main(){ int N, Q; cin >> N; vector<long long> a(N), t(N); for(int i = 0; i < N; i++) cin >> a[i] >> t[i]; long long MIN = -INF;//上限 long long MAX = INF;//下限 long long temp = 0; bool flag = false; long long f = -INF; for(int i = 0; i < N; i++){ if(t[i] == 1) { temp += a[i]; MAX += a[i]; MIN += a[i]; if(flag) f += a[i]; } else if(t[i] == 2){ MIN = max(MIN, a[i]); if(MIN >= MAX){ flag = true; if(f == -INF) f = a[i]; } if(flag) f = max(f, a[i]); } else{ MAX = min(MAX, a[i]); if(MIN >= MAX) { flag = true; if(f == -INF) f = a[i]; } if(flag)f = min(f, a[i]); } } long long mi = MIN, ma = MAX; if(!flag){ for(int i = N - 1; i >= 0; i--){ if(t[i] == 1){ mi -= a[i]; ma -= a[i]; } } } cin >> Q; for(int i = 0; i < Q; i++){ long long x; cin >> x; if(flag) cout << f << endl; else{ if(x > ma) cout << MAX << endl; else if(x < mi) cout << MIN << endl; else cout << x + temp << endl; } } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define repk(i, k, n) for (int i = k; i < n; i++) using namespace std; using ll = long long; #define INF 1001001001001001 int main (){ ll n; cin >> n; ll max=-INF, min=INF, bias=0; rep(i, n){ ll a; int t; cin >> a >> t; if(t==1){ bias+=a; if(max>-INF) max += a; if(min<INF) min += a; } else if(t==2){ if(a>max) max=a; if(a>min) min=a; } else if(t==3){ if(a<min) min=a; if(a<max) max=a; } } ll q; cin >> q; rep(i, q){ ll x; cin >> x; ll y = x+bias; if(y>=min) y=min; else if(y<=max) y=max; cout << y << endl; } return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define mp make_pair #define ff first #define ar array #define ss second #define ll long long int #define mem(x) memset(x,0,sizeof(x)) #define setbits(x) __builtin_popcount(x) #define ull unsigned long long int #define all(c) (c).begin(),(c).end() #define debug(x) cout<<#x<<" :: "<<x<<endl; #define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl; #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n"; #define db double #define endl "\n" #define fbo(x) find_by_order(x) #define ook(x) order_of_key(x) using namespace __gnu_pbds; using namespace std; typedef vector< int > vi; typedef vector< vi > vvi; typedef pair<ll,ll > iil; typedef pair<ll,int> li; typedef pair<int,int> ii; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> os1; typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> os2; inline void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const ll pi=31; const ll MOD[2] = {1187278649, 1149736019};//for double hashing const ll mod=(ll)(1e9)+7; ll pmod(ll x,ll n){ ll r=1; while(n>0) { if(n&1LL) r=(r*x)%mod; x=(x*x)%mod; n=n/2; n%=mod; } return r; } ll pMod(ll x,ll n,ll mod) { ll r=1; while(n>0) { if(n&1LL) r=(r*x)%mod; x=(x*x)%mod; n=n/2; n%=mod; } return r; } //think recursively also; void solve() { int n; cin>>n; int N=2*n; bool vis[N+1]={0}; int a[n+1],b[n+1]; bool flag=0; for(int i=1;i<=n;i++) { cin>>a[i]>>b[i]; if(a[i]!=-1) { if(vis[a[i]]) { flag=1; } vis[a[i]]=1; } if(b[i]!=-1) { if(vis[b[i]]) { flag=1; } vis[b[i]]=1; } if(a[i]!=-1 && b[i]!=-1) { if(a[i]>=b[i]) { flag=1; } } } if(flag) { cout<<"No\n"; return; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) continue; if(a[i]==-1 || b[i]==-1) continue; if(b[j]!=-1 && a[j]!=-1 && a[i]<=a[j] && a[j]<=b[i] && a[i]<=b[j] && b[j]<=b[i]) { cout<<"No\n"; return; } if(a[j]!=-1) { if(b[j]!=-1) { if(a[i]<=a[j] && a[j]<=b[i]) {if(abs(a[i]-b[i])!=abs(a[j]-b[j])) { cout<<"No\n"; return; } } } else { if(a[i]<=a[j] && a[j]<=b[i]) {int g=b[i]-a[i]; int req=g+a[j]; if(req<=0 || req>N || vis[req]) { cout<<"No\n"; return; } //vis[req]=1; } } } if(b[j]!=-1) { if(a[j]!=-1) { if(a[i]<=b[j] && b[j]<=b[i]) {if(abs(a[i]-b[i])!=abs(a[j]-b[j])) { cout<<"No\n"; return; } } } else { if(a[i]<=b[j] && b[j]<=b[i]) {int g=b[i]-a[i]; int req=b[j]-g; if(req<=0 || req>N || vis[req]) { cout<<"No\n"; return; } // vis[req]=1; } } } } } cout<<"Yes\n"; return; } int main() { fast(); // int t; // cin>>t; // for(int casee=1;casee<=t;casee++) {//cout<<"Case #"<<casee<<": "; solve(); } }
#pragma region head #include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using pi = pair<int, int>; using pll = pair<ll, ll>; template <class T> using vv = vector<vector<T>>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define bit(n) (1LL << (n)) template <class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return 1; } return 0; } const int INF = 1002003004; const ll LINF = 1002003004005006007ll; struct preprocess { preprocess() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); } } ____; #pragma endregion head #pragma region library const int MOD = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } bool operator==(const mint a) const { return x == a.x; } bool operator!=(const mint a) const { return x != a.x; } friend ostream& operator<<(ostream& os, const mint& value) { os << value.x; return os; } friend istream& operator>>(istream& is, mint& value) { ll t; is >> t; value = mint(t); return is; } }; #pragma endregion library int main() { int _t; cin >> _t; while (_t--) { ll n, a, b; cin >> n >> a >> b; if(n<a+b){ cout << 0 << '\n'; continue; } mint ans = 0, sub = 0; ans = (mint)(n - a + 1) * (n - a + 1); ans *= (mint)(n - b + 1) * (n - b + 1); sub = (mint)(n - a + 1) * (n - b + 1) - (mint)(n - a - b + 2) * (n - a - b + 1); ans -= sub * sub; cout << ans << '\n'; } }
#include<bits/stdc++.h> using namespace std; int main() { string s; cin>>s; set<char> s1; int i; for(i=0;i<s.length();i++) { s1.insert(s.at(i)); } if(s1.size()==1) cout<<"Won"; else cout<<"Lost"; }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; if(n<191) cout << "Yay!" << endl; else if(n==191) cout << "so-so" << endl; else cout << ":(" << endl; }
#include <iostream> #include <cstdio> #include <cmath> using namespace std; using ll = long long; int main(){ ll n; cin >> n; ll a[n], mod[200] = {0}; for (ll i = 0; i < n; i++){ cin >> a[i]; } for (ll i = 0; i < n; i++) { mod[a[i]%200]++; } ll ans = 0; for (ll i = 0; i < 200; i++){ ans += (mod[i] * (mod[i]-1))/2; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> /*Fuck ratings Feel the magic in the code...*/ // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> using namespace std; // using namespace __gnu_pbds; // template<typename T> // using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; /* in log(n) time... find_by_order() - Returns an iterator to the k-th largest element (counting from zero) order_of_key() - The number of items in a set that are strictly smaller than our item // Rest same as set */ #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define mod 1000000007 #define inf 2e18 #define minf -2e18 #define PI 3.141592653589793238462 #define ll long long #define all(x) x.begin(), x.end() #define min3(a, b, c) min(min(a, b), c) #define max3(a, b, c) max(max(a, b), c) #define ff first #define ss second #define pb push_back #define ppb pop_back #define sz(v) (int)v.size() #define nline "\n" ll powr(ll a, ll b, ll p) { if(b == 0) return 1; else if(b == 1) return a; else if(!(b & 1)) return powr((a * a) % p, b / 2, p); else return (a * powr(a, b - 1, p)) % p; } ll gcd(ll a, ll b){ if(b == 0) return a; else return gcd(b, a % b); } ll lcm(ll a, ll b){ return a * b / gcd(a, b); } void extendgcd(ll a, ll b, ll *v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3 ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0]; } //for non prime b ll mod_inv(ll a, ll p){ return powr(a,p-2,p); } /*For prime p "Fermet lill theorem"*/ ll mod_add(ll a, ll b, ll m) { a = a % m; b = b % m; return ((a + b) % m + m) % m; } ll mod_mul(ll a, ll b, ll m) { a = a % m; b = b % m; return ((a * b) % m + m) % m; } ll mod_sub(ll a, ll b, ll m) { a = a % m; b = b % m; return ((a - b) % m + m) % m; } ll mod_div(ll a, ll b, ll m) { a = a % m; b = b % m; return (mod_mul(a, mod_inv(b, m), m) + m) % m; } /*For prime p*/ // void swap(int &a, int &b) { int temp = a; a = b; b = temp; } ll spf(ll n) { for(int i = 2; i * i <= n; i++) { if(n % i == 0) return i; } return n; }/* spf in sqrt(n) time... */ vector<ll> fact_modp(ll n, ll p) { vector<ll> fact(n + 1); fact[0] = 1; for(int i = 1; i <= n; i++){ fact[i] = (i * fact[i - 1]) % p; } return fact; } /*For prime p "Fermet lill theorem..."*/ ll nCr_modp(ll n, ll r, ll p, vector<ll> fact) { if(n < r) return 0; if(r == 0) return 1; else { return ((fact[n] * mod_inv(fact[r], p)) % p * mod_inv(fact[n - r], p)) % p; } } vector<ll> prime_sieve(ll n) { /*Returns vector of prime <= n in nloglogn time*/ vector<ll> t; vector<bool> a(n + 1, true); a[0] = a[1] = false; for(int i = 2; i * i <= n; i++) { if(a[i]) { for(int j = i * i; j <= n; j += i) a[j] = false; } } for(int i = 0; i <= n; i++) { if(a[i]) t.push_back(i); } return t; } vector<ll> spf_sieve(ll n) { /*Returns vector of spf[n] in nloglogn time*/ vector<ll> a(n + 1, -1); for(int i = 2; i * i <= n; i++) { if(a[i] == -1) { a[i] = i; for(int j =i * i; j <= n; j += i) { if(a[j] == -1) a[j] = i;} } } for(int i = 2; i <= n; i++) { if(a[i] == -1) a[i] = i;} return a; } /* --------------------------------------Template ends, Dee codes---------------------------------------*/ int main() { fastio(); int tests = 1; // cin >> tests; while(tests--) { int n; cin >> n; vector<int> a(n); ll res = 0; map<int, ll> mp; for(int i = 0; i < n; i++) { cin >> a[i]; int r1 = (200 - (a[i] % 200)); if(r1 != 200) { res += mp[200 - r1]; } mp[a[i] % 200]++; } res += ((mp[0] * (mp[0] - 1)) / 2); cout << res << nline; } return 0; }
#include<iostream> #include<vector> using namespace std; const int BUF = 105; const int EDGE = BUF * BUF; const int MOD = 1000000007; int add(int a, int b) { return (a + b) % MOD; } int mul(int a, int b) { return 1LL * a * b % MOD; } int modpow(int p, int n) { if (n == 0) return 1; int t = modpow(p, n / 2); return n % 2 == 0 ? mul(t, t) : mul(p, mul(t, t)); } vector<vector<int> > matmul(const vector<vector<int> > &a, const vector<vector<int> > &b) { int n = a.size(); vector<vector<int> > c = vector<vector<int> >(n, vector<int>(n, 0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { c[i][j] = add(c[i][j], mul(a[i][k], b[k][j])); } } } return c; } vector<vector<int> > modpow(vector<vector<int> > &p, int n) { if (n == 0) { vector<vector<int> > ret = vector< vector<int> >(p.size(), vector<int>(p.size(), 0)); for (int i = 0; i < p.size(); ++i) ret[i][i] = 1; return ret; } vector<vector<int> > t = modpow(p, n / 2); return n % 2 == 0 ? matmul(t, t) : matmul(p, matmul(t, t)); } int inv(int v) { return modpow(v, MOD - 2); } int divide(int a, int b) { return mul(a, inv(b)); } int nVal, nEdge, nPow; int val[BUF]; int edge[EDGE][2]; void read() { cin >> nVal >> nEdge >> nPow; for (int i = 0; i < nVal; ++i) cin >> val[i]; for (int i = 0; i < nEdge; ++i) { cin >> edge[i][0] >> edge[i][1]; --edge[i][0]; --edge[i][1]; } } void work() { vector< vector<int> > mat(nVal, vector<int>(nVal, 0)); for (int i = 0; i < nEdge; ++i) { mat[edge[i][0]][edge[i][0]] = add(mat[edge[i][0]][edge[i][0]], 1); mat[edge[i][0]][edge[i][1]] = add(mat[edge[i][0]][edge[i][1]], 1); mat[edge[i][1]][edge[i][0]] = add(mat[edge[i][1]][edge[i][0]], 1); mat[edge[i][1]][edge[i][1]] = add(mat[edge[i][1]][edge[i][1]], 1); for (int j = 0; j < nVal; ++j) { if (j == edge[i][0] || j == edge[i][1]) continue; mat[j][j] = add(mat[j][j], 2); } } for (int i = 0; i < nVal; ++i) { for (int j = 0; j < nVal; ++j) { mat[i][j] = divide(mat[i][j], 2 * nEdge); } } vector<vector<int> > ret = modpow(mat, nPow); vector<int> ans(nVal, 0); for (int i = 0; i < nVal; ++i) { for (int j = 0; j < nVal; ++j) { ans[i] = add(ans[i], mul(ret[i][j], val[j])); } } for (int i = 0; i < nVal; ++i) { cout << ans[i] << endl; } } int main() { read(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define forn(i,x,n) for(int i = x;i <= n;++i) typedef pair<int,int> pii; #define forr(i,x,n) for(int i = n;i >= x;--i) #define Angel_Dust ios::sync_with_stdio(0);cin.tie(0) #define x first #define y second const int N = 105,MOD = 1e9+7; pii edges[N * N]; int deg[N]; struct Mat { int c[N][N]; Mat() { memset(c,0,sizeof c); } Mat operator*(const Mat& r) const { Mat res; forn(i,1,N - 1) forn(j,1,N - 1) forn(k,1,N - 1) res.c[i][j] = (res.c[i][j] + 1ll*c[i][k] * r.c[k][j] % MOD) % MOD; return res; } void operator*(int r) { forn(i,1,N - 1) forn(j,1,N - 1) c[i][j] = 1ll*c[i][j] * r % MOD; } }; Mat qpow(Mat a,int b,int MOD) { Mat res;forn(i,1,N - 1) res.c[i][i] = 1; while(b) { if(b & 1) res = res * a; a = a * a; b >>= 1; } return res; } int qpow(int a,int b,int MOD) { int res = 1; while(b) { if(b & 1) res = 1ll * res * a % MOD; a = 1ll * a * a % MOD; b >>= 1; } return res; } int main() { // freopen("D://OJCodes//TTT//in.txt","r",stdin); // freopen("D://OJCodes//TTT//out.txt","w",stdout); int n,m,k;scanf("%d%d%d",&n,&m,&k); Mat f,B;forn(i,1,n) scanf("%d",&f.c[1][i]); forn(i,1,m) { int u,v;scanf("%d%d",&u,&v); ++deg[u];++deg[v]; edges[i] = {u,v}; } int m_2fact = qpow(m * 2,MOD - 2,MOD); forn(i,1,m) { int u = edges[i].x,v = edges[i].y; B.c[u][v] = m_2fact; B.c[v][u] = m_2fact; } forn(i,1,n) B.c[i][i] = (2 * m - deg[i]) * 1ll * m_2fact % MOD; B = qpow(B,k,MOD); f = f * B; forn(i,1,n) printf("%d\n",f.c[1][i]); // fclose(stdin); // fclose(stdout); return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "debug.h" #else #define dump(...) #endif #define endl '\n' //#define int long long #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define REP(i, x) for (int i = 0; i < (int)(x); i++) #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define INF 2147483647 #define LLINF 9223372036854775807LL #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define BIT(n) (1LL << (n)) #define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper) #define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower) #define FILLZERO(s, n) \ do { \ ostringstream os; \ os << setw(n) << setfill('0') << s; \ s = os.str(); \ } while (0); #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと using ll = long long; using vi = vector<int>; using vvi = vector<vector<int>>; using pi = pair<int, int>; template <typename T> using Pq = priority_queue<T>; template <typename T> using pQ = priority_queue<T, vector<T>, greater<T>>; template <typename T> vector<T> make_vec(size_t a, T n) { return vector<T>(a, n); } template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } 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 <typename T> bool isProductOverflow(T a, T b) { return (a > numeric_limits<T>::max() / b); } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } __attribute__((constructor)) void ioInit() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); cout << std::fixed << std::setprecision(10); } constexpr ll MOD = 1e9 + 7; //constexpr ll MOD = 998244353; int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z; string S, T; ll ans = 0; signed main() { cin >> N; S = to_string(N); K = S.size(); vi v(K); REP(i, K) { v[i] = S[i] - '0'; } ans = INF; REP(bit, 1 << K) { ll tmp = 0; REP(i, K) { if ((bit >> i) & 1) { tmp += v[i]; } } int cnt = __builtin_popcount(bit); if (cnt != 0 and tmp % 3 == 0) { chmin(ans, K - cnt); } } if (ans == INF) ans = -1; cout << ans << endl; }
#include "bits/stdc++.h" using namespace std; using LL = long long; constexpr int N = 1e5 + 5; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; cout << (1 << n) - 1 << endl; for (int i = 1; i < (1 << n); i++) { for (int j = 0; j < (1 << n); j++) { cout << char('A' + (__builtin_popcount(i & j) % 2)); } cout << endl; } }
#include <iostream> #include <cstring> using namespace std; string s[61]; long long n, memo[61][2]; long long f(int idx, int a) { if (idx == n) return a; if (memo[idx][a] != -1) return memo[idx][a]; return memo[idx][a] = (s[idx] == "OR" ? f(idx + 1, a|1) + f(idx + 1, a|0) : f(idx + 1, a&1) + f(idx + 1, a&0)); } int main() { cin >> n; memset(memo, -1, sizeof(memo)); for (int i = 0; i < n; i++) cin >> s[i]; cout << f(0, 1) + f(0, 0) << endl; }
#include <bits/stdc++.h> using namespace std; #define boostIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define rep(i, a, b) for (int i = a; i < b; i++) #define repn(i, n) for (int i = 1; i <= n; i++) #define rrep(i, b, a) for (int i = b; i >= a; i--) #define int long long #define fi first #define se second #define pb push_back #define pii pair<int, int> #define sz(v) (int)(v.size()) #define all(v) v.begin(), v.end() #define mem(a, val) memset(a, val, sizeof(a)) #define trav(a, x) for (auto &a : x) #define ppcll(x) __builtin_popcountll(x) void amn(int &x, int y) { x = min(x, y); } void amx(int &x, int y) { x = max(x, y); } #define mod 1000000007 const long long INF = 1e18; // const int N = 5e5 + 5; signed main() { boostIO; int n; cin >> n; vector<pair<int, string> > v; for (int i = 0; i < n; i++) { string s; int t; cin >> s >> t; v.push_back({t, s}); } sort(v.begin(), v.end()); cout << v[n - 2].se << endl; return 0; } // Play Hard Kiddo...
#include <bits/stdc++.h> #define ll long long #define ld long double #define endl "\n" using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); ll n; cin >> n; ll arr[n]; ll sum=0; for (ll i=0;i<=n-1;i++) { cin >> arr[i]; sum+=arr[i]; } sort(arr,arr+n); ll ans=0; for (ll i=0;i<=n-1;i++) { sum-=arr[i]; ans+=sum-((n-i-1)*arr[i]); } cout << ans << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <climits> using namespace std; using ll = long long; int main(){ int N; cin >> N; vector<ll> A(N); for(int i=0; i<N; ++i){ cin >> A[i]; } ll ans = -1; for(int i=0; i<N; ++i){ ll max_x = A[i]; for(int j=i; j<N; ++j){ max_x = min(max_x, A[j]); ans = max(ans, max_x*(j-i+1)); } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define MAXN 200005 #define lowbit(x) (x&-x) #define reg register #define mp make_pair #define fir first #define sec second typedef long long LL; typedef unsigned long long uLL; const int INF=0x3f3f3f3f; const int mo=1e9+7; const int iv2=5e8+4; const int lim=1000000; const int jzm=1e6+7; const int orG=3,invG=332748118; const double Pi=acos(-1.0); typedef pair<LL,LL> pii; const double PI=acos(-1.0); template<typename _T> _T Fabs(_T x){return x<0?-x:x;} template<typename _T> void read(_T &x){ _T f=1;x=0;char s=getchar(); while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();} while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();} x*=f; } template<typename _T> void print(_T x){if(x<0){x=(~x)+1;putchar('-');}if(x>9)print(x/10);putchar(x%10+'0');} int gcd(int x,int y){return !y?x:gcd(y,x%y);} int add(int x,int y){return x+y<mo?x+y:x+y-mo;} LL n,ans[2005],tot;pii f[2005]; pii solve1(pii tmp){return mp(tmp.fir+1LL,tmp.sec);} pii solve2(pii tmp){return mp(tmp.fir,tmp.sec+1LL);} pii solve3(pii tmp){return mp(tmp.fir+tmp.sec,tmp.sec);} pii solve4(pii tmp){return mp(tmp.fir,tmp.sec+tmp.fir);} void extend(LL id){ tot=id; while(f[tot].fir+f[tot].sec<=n){ if(f[tot].fir<=f[tot].sec)tot++,f[tot]=solve3(f[tot-1]),ans[tot]=3; else tot++,f[tot]=solve4(f[tot-1]),ans[tot]=4; } } LL check(LL id){return n-f[id].fir+id;} void print(){ while(f[tot].fir<n)++tot,f[tot]=solve1(f[tot-1]),ans[tot]=1; printf("%d\n",tot);for(int i=1;i<=tot;i++)printf("%d\n",ans[i]); } signed main(){ read(n);f[1]=solve2(f[0]);ans[1]=2;f[2]=solve1(f[1]);ans[2]=1;tot=2;extend(tot); if(check(tot)<=130){print();return 0;} for(int i=3;i<=tot;i++){ LL las=check(tot);pii ls=f[i];int lasway=ans[i];f[i]=solve1(f[i-1]);ans[i]=1;extend(i); if(check(tot)>=las)f[i]=ls,ans[i]=lasway,extend(i); //printf("%d:%lld %lld %lld %lld\n",i,f[i].fir,f[i].sec,check(tot),las); las=check(tot);ls=f[i];lasway=ans[i];f[i]=solve2(f[i-1]);ans[i]=2;extend(i); if(check(tot)>=las)f[i]=ls,ans[i]=lasway,extend(i); //printf("%d:%lld %lld %lld %lld\n",i,f[i].fir,f[i].sec,check(tot),las); las=check(tot);ls=f[i];lasway=ans[i];f[i]=solve3(f[i-1]);ans[i]=3;extend(i); if(check(tot)>=las)f[i]=ls,ans[i]=lasway,extend(i); //printf("%d:%lld %lld %lld %lld\n",i,f[i].fir,f[i].sec,check(tot),las); las=check(tot);ls=f[i];lasway=ans[i];f[i]=solve4(f[i-1]);ans[i]=4;extend(i); if(check(tot)>=las)f[i]=ls,ans[i]=lasway,extend(i); //printf("%d:%lld %lld %lld %lld\n",i,f[i].fir,f[i].sec,check(tot),las); if(check(tot)<=130){print();return 0;} //printf("nxt %lld %lld\n",f[i+1].fir,f[i+1].sec); } print(); return 0; } /* 0 1 1 1 1 2 */
#include <bits/stdc++.h> #include <bits/extc++.h> #define double long double #define rbtree __gnu_pbds::tree<int,__gnu_pbds::null_type,less<int>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update> #define int ll #define IOS ios_base::sync_with_stdio(false);cin.tie(0); #define pb push_back #define ALL(X) X.begin(),X.end() #define F(i,n) FF(i,0,n) #define F1(i,n) FF(i,1,n+1) #define FF(i,n,m) for(int i=(int)n;i<(int)m;++i) #ifndef LOCAL //#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector") #else #endif // LOCOL //#define mp make_pair using namespace std; //using namespace __gnu_pbds; template<typename T> bool remax(T& a, const T& b) {return b>a?a=b,1:0;} template<typename T> bool remin(T& a, const T& b) {return b<a?a=b,1:0;} inline ostream& operator << (ostream& os,__int128_t a){if(a==0) {return (os<<'0');}bool flag=0;if(a<0) {a=-a,flag=1;}string s;while(a){s+=a%10+'0';a/=10;}s+=flag?"-":"";reverse(ALL(s));return os<<s;} inline istream& operator >>(istream& is,__int128_t& a){string s;cin>>s;a=0;for(auto c:s) a=a*__int128_t(10)+__int128_t(c-'0');return is;} template<typename T,typename P> inline ostream& operator << (ostream& os,pair<T,P> a){os<<a.first<<" "<<a.second; return os;} template<typename T,typename P> inline istream& operator >> (istream& is,pair<T,P> &a){is>>a.first>>a.second; return is;} using ll=long long; using ull=unsigned long long; using int128= __int128_t; using uint128= __uint128_t; using pii =pair<int,int>; const double pi=acos(-1); const int N=1E5+5; const ll M=1000000000; const ll INF_64=0x3f3f3f3f3f3f3f3f; const int INF_32=0x3f3f3f3f; const int16_t INF_16=0x3f3f; const int klog=20; const int mod=1E9+7;//998244353 const double eps=1E-8; void gen(){ } vector<int> an; bool DD(int x,int y){ if(x==0||y==0){ if(an.size()+x+y>130) return false; while(x--) an.pb(1); while(y--) an.pb(2); return true; } if(x>=y) x-=y,an.pb(3); else y-=x,an.pb(4); return DD(x,y); } int re(){ int x=0,y=0; F(i,an.size()){ if(an[i]==1) x++; else if(an[i]==2) y++; else if(an[i]==3) x+=y; else if(an[i]==4) y+=x; } return x; } void sol(){ int n; cin>>n; std::random_device rd; std::default_random_engine gen = std::default_random_engine(rd()); std::uniform_int_distribution<int> dis(n/4,3*n/4); int tt; if(n<=130){ cout<<n<<"\n"; F(i,n) cout<<1<<"\n"; return; } while(tt=dis(gen)){ an.clear(); an.pb(3); if(!DD(tt,n-tt)) continue; //cerr<<tt<<"\n"; cout<<an.size()<<"\n"; reverse(ALL(an)); assert(re()==n); F(i,an.size()) cout<<an[i]<<"\n"; return; } } int32_t main(){ #ifdef LOCAL //freopen(".in","r",stdin); //freopen(".out","w",stdout); #endif // LOCAL IOS; int t=1; gen(); //cin>>t; FF(i,1,t+1){ //cout<<"Case #"<<i<<": "; sol(); } }
#include<iostream> #include<iomanip> #include<string.h> #include<string> #include<algorithm> #include<map> #include<iterator> #include<cmath> #include<vector> #include<utility> #include<stack> #include<queue> #define li long int #define ll long long int #define lli unsigned long long int #define mod 1e9+7 using namespace std; int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k; cin>>n>>k; for(int j=1; j<=k; j++){ vector<int>v,m; while(n!=0){ v.push_back(n%10); n/=10; } m=v; sort(v.begin(),v.end()); sort(m.rbegin(),m.rend()); int len=v.size(),len2; int num=0,num1=0; len--; len2=len; int p=pow(10,len),p1; int i=0; p1=p; while(len>=0){ num+=v[i++]*p; p/=10; len--; } i=0; while(len2>=0){ num1+=m[i++]*p1; p1/=10; len2--; } n=num1-num; } cout<<n<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 20; mt19937 mt; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); mt.seed(time(nullptr)); int n, m; cin >> n >> m; vector<string> v(m); for (auto& i : v) { cin >> i; } //sort(v.begin(), v.end(), [](string a, string b){return a.size() < b.size();}); shuffle(v.begin(), v.end(), mt); int ind[10] = {}; for (auto& i : v) { for (auto& j : i) { ++ind[j - 'A']; } } char a[N][N]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { a[i][j] = char(int(max_element(ind, ind + 10) - ind) + int('A')); } } int i = 0, j = 0; for (auto s : v) { if (j + s.size() >= n) { j = 0; ++i; } if (i >= n) { break; } for (int k = 0; k < s.size(); ++k, ++j) { a[i][j] = s[k]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cout << a[i][j]; } cout << '\n'; } return 0; }
#include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cassert> #include<cmath> #include<vector> #include<map> #include<set> #include<string> #include<queue> #include<stack> #include<list> #include <cstring> #include <functional> #include<unordered_map> #include<unordered_set> #include<bitset> using namespace std; #define MOD 1000000007 #define MOD2 998244353 #define INF ((1<<30)-1) #define LINF ((Int)1<<60) #define EPS (1e-10) #define REP(i,n) for(int i=0; i<(int)(n); ++i) #define RREP(i,n) for(int i=1; i<=(int)(n); ++i) typedef long long Int; typedef pair<Int, Int> PI; typedef pair<int, int> pi; typedef tuple<int, int, int> TP; typedef vector<double> vec; typedef vector<vec> mat; const int N = 201005; ////////////////////////////// int n, m; int x[100], y[100], z[100]; Int dp[1 << 18]; void solve() { cin >> n >> m; REP(i, m) cin >> x[i] >> y[i] >> z[i]; dp[0] = 1; REP(mask, (1 << n)) { REP(nxt, n) { if (((1 << nxt) & mask) != 0) continue; int comb = (1 << nxt) | mask; vector<int> nums; REP(i, n) { if ((comb & (1 << i))) { nums.push_back(i + 1); } } int flag = 1; REP(j, m) { if (x[j] != nums.size()) continue; int cnt = 0; for (int a : nums) { if (a <= y[j]) cnt++; } if (cnt > z[j]) { flag = 0; break; } } if (flag) { dp[comb] += dp[mask]; } } } cout << dp[(1 << n) - 1] << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(13); solve(); return 0; }
#include "bits/stdc++.h" #include <bits/stdc++.h> using namespace std; // #include <atcoder/all> // using namespace atcoder; using ll = long long; template <typename T> void print_vec2(const vector<vector<T>>& v){ cout << endl; cout << " "; for(ll i=0; i<v[0].size(); i++) cout << i << " "; cout << endl; for(ll i=0; i<v.size(); i++){ cout << "i=" << i << ": "; for(ll j=0; j<v[i].size(); j++){ if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << " "; else cout << "\x1B[31m" << v[i][j] << " ";//https://stackoverrun.com/ja/q/12618775 } cout << "\x1B[0m" << endl; } } // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; // const int mod = 998244353; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} int main(){ int H, W; cin >> H >> W; vector<string> S(H); for(int i=0; i<H; i++) cin >> S[i]; vector<vector<mint>> dp(H+1, vector<mint>(W+1, 0)); dp[0][0] = 1; vector<vector<mint>> tate=dp, yoko=dp, naname=dp;//累積和 for(int i=0; i<H; i++){ for(int j=0; j<W; j++){ if(S[i][j]=='#') continue; //もらうDP?? if(i==0 && j==0) continue;//初期化で計算済み if(i-1>=0) tate[i][j] += tate[i-1][j]; if(j-1>=0) yoko[i][j] += yoko[i][j-1]; if(i-1>=0 && j-1>=0) naname[i][j] += naname[i-1][j-1]; dp[i][j] += tate[i][j]; dp[i][j] += yoko[i][j]; dp[i][j] += naname[i][j]; tate[i][j] += dp[i][j]; yoko[i][j] += dp[i][j]; naname[i][j] += dp[i][j]; //配るDP?? できない 順番がおかしい? // tate[i+1][j] += tate[i][j]; // yoko[i][j+1] += yoko[i][j]; // naname[i+1][j+1] += naname[i][j]; // dp[i+1][j] += tate[i+1][j]; // dp[i][j+1] += yoko[i][j+1]; // dp[i+1][j+1] += naname[i+1][j+1]; // tate[i+1][j] += dp[i+1][j]; // yoko[i][j+1] += dp[i][j+1]; // naname[i+1][j+1] += dp[i+1][j+1]; // if(i-1>=0) dp[i][j] += tate[i-1][j]; // if(j-1>=0) dp[i][j] += yoko[i][j-1]; // if(i-1>=0 && j-1>=0) dp[i][j] += naname[i-1][j-1]; // // if(i-1>=0) tate[i][j] = tate[i-1][j] + dp[i][j]; // // if(j-1>=0) yoko[i][j] = yoko[i][j-1] + dp[i][j]; // // if(i-1>=0 && j-1>=0) naname[i][j] = naname[i-1][j-1] + dp[i][j]; // if(i-1>=0) tate[i][j] += tate[i-1][j] + dp[i][j]; // if(j-1>=0) yoko[i][j] += yoko[i][j-1] + dp[i][j]; // if(i-1>=0 && j-1>=0) naname[i][j] += naname[i-1][j-1] + dp[i][j]; } } // cout << " tate: " << endl; print_vec2(tate); // cout << " yoko: " << endl; print_vec2(yoko); // cout << " naname: " << endl; print_vec2(naname); // cout << " dp: " << endl; print_vec2(dp); cout << dp[H-1][W-1] << endl; return 0; // cout << fixed;//小数点を確実に10桁で表示するためのもの // cout << setprecision(10); }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for(int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; // using namespace atcoder; typedef long long int ll; typedef pair<int,int> P; #define yn {puts("Yes");}else{puts("No");} #define MAX_N 200005 int main() { int n; cin >> n; int a[n+1], b[n+1]; srep(i,1,n+1){ a[i] = i * 2 % n; b[i] = (i*2+1) % n; if(a[i] == 0) a[i] = n; if(b[i] == 0) b[i] = n; } srep(i,1,n+1) cout << a[i] << ' ' << b[i] << endl; return 0; }
#include <bits/stdc++.h> #define LL long long #define pb push_back #define st first #define nd second #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3f template <class T> T read(T &a) { a=0;char x=getchar();bool f=0; for(;x<'0'||x>'9';x=getchar())f|=x=='-'; for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0'; if(f)a=-a; return a; } using namespace std; int main() { int n; read(n); for (int i = 1; i <= n; ++i) { int x = i * 2, y = i * 2 + 1; while (x > n) x -= n; while (y > n) y -= n; printf("%d %d\n", x, y); } return 0; }
#include<iostream> using namespace std; int main() { int n,kk; int sum=0; cin >> n >> kk; for(int i=1; i<=n; i++) { for(int j=1; j<=kk; j++) { sum+=i*100+j; } } cout <<sum; }
#include <iostream> int main() { int N, K; std::cin >> N >> K; long long sum = 100 * K * N * (N + 1) / 2 + N * K * (K + 1) / 2; std::cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<long long, long long>; constexpr char ln = '\n'; //constexpr long long MOD = 1000000007; constexpr long long MOD = 998244353; constexpr long long INF = 1000000000 + 100; constexpr long long LINF = 1000000000000000000 + 100; #define all(x) (x).begin(),(x).end() #define rep(i,n) for(int i=0;i<(n);i++) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int main(){ ll n, k; cin >> n >> k; vector<ll> C(n); rep(i, n){ ll a; cin >> a; C[a]++; } vector<ll> box(k); ll last = k; rep(i, n){ last = min(last, C[i]); rep(j, last)box[j] = i+1; } ll res = accumulate(all(box), 0LL); cout << res << ln; }
#include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <stack> #include <queue> #include <array> #include <bitset> #include <cstdio> #include <string> #include <vector> #include <random> #include <chrono> #include <utility> #include <numeric> #include <cstdlib> #include <cstring> #include <climits> #include <sstream> #include <assert.h> #include <iostream> #include <iomanip> #include <algorithm> #include <functional> #include <unordered_map> using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(bool x) {cerr << (x ? "true" : "false");} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T>void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V>void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifdef HOME #warning CHECK int:ll::INT_MAX:LLONG_MAX #define maxn 20 #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define maxn 2000006 #define gcd __gcd #define debug(x...) #endif #define ff first #define endl '\n' #define ss second #define inf 0x3f3f3f3f #define MOD 1000000007 #define PI 3.14159265358979323846264338327950L #define f(i,x,n) for(int i=x;i<=n;i++) #define fr(i,x,n) for(int i=x;i>=n;i--) struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _; int dx[] = { -1, 0, 1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, 1, -1}; int main() { int n, k; cin >> n >> k; map<int, int> freq; vector<int> a(n, 0); for (int i = 0; i <= n - 1; i++) cin >> a[i], freq[a[i]]++; vector<pair<int, int>> v(freq.begin(), freq.end()); if (v[0].ff != 0) { cout << 0; return 0; } v[0].ss = min(k, v[0].ss); long long ans = v[0].ss; for (int i = 1; i < v.size(); i++) { if (v[i].ff != i) break; v[i].ss = min(v[i - 1].ss, v[i].ss); ans += v[i].ss; } cout << ans; return 0; }
#include<iostream> #include<algorithm> #include<string> #include<cstring> #include<string.h> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<cmath> typedef long long ll; using namespace std; int main() { int n; cin >> n; int ans=0x3f3f3f3f; for(int i=1;i<=n;i++){ int a,b,c; cin >> a >> b >> c; if(c-a>0){ ans=min(ans,b); } } if(ans==0x3f3f3f3f){ ans=-1; } cout << ans << endl; return 0; }
#pragma GCC optimize("Ofast") //#ifndef ONLINE_JUDGE //#define _GLIBCXX_DEBUG //#endif #ifdef ONLINE_JUDGE #include <atcoder/all> #endif #include <bits/stdc++.h> #include <chrono> #include <random> #include <math.h> #include <complex> using namespace std; #ifdef ONLINE_JUDGE using namespace atcoder; #endif #define rep(i,n) for (int i = 0;i < (int)(n);i++) using ll = long long; #ifdef ONLINE_JUDGE //using mint = modint998244353; //using mint = modint; using mint = modint1000000007; #endif const ll MOD=1000000007; //const ll MOD=998244353; const long long INF = 1LL << 60; const double pi=acos(-1.0); int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; int dy[9] = {0, 1, 0, -1, 1, -1, -1, 1, 0}; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); ll N; cin>>N; vector<tuple<ll,ll,ll>> v(N); rep(i,N) { ll a,p,x; cin>>a>>p>>x; v[i]={a,p,x}; } sort(v.begin(),v.end()); ll ans=INF; for(auto &[a,p,x]:v) { if(x-a>0) chmin(ans,p); } cout<<(ans<INF?ans:-1)<<'\n'; return 0; }
#include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define ll long long #define ull unsigned ll #define ld long double #define mod 1000000007 #define pb push_back #define pf push_front #define dll deque<ll> #define vll vector<ll> #define vvll vector<vll> #define pll pair<ll,ll> #define vpll vector<pll> #define dpll deque<pll> #define mapll map<ll,ll> #define umapll unordered_map<ll,ll,custom_hash> #define nl "\n" #define all(v) v.begin(),v.end() #define fr for(ll i=0;i<n;i++) #define ms(a,x) memset(a,x,sizeof(a)) //#define fr(aa,bb) for(ll i=aa;i<=bb;i++) #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") using namespace __gnu_pbds; vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.push_back(x); return move(v); } template<typename T, typename... Args> inline string arrStr(T arr, int n) { stringstream s; s << "["; for(int i = 0; i < n - 1; i++) s << arr[i] << ","; s << arr[n - 1] << "]"; return s.str(); } #define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);} inline void __evars_begin(int line) { cerr << "#" << line << ": "; } template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); } template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); } template<typename T> inline void __evars_out_var(T val) { cerr << val; } inline void __evars(vector<string>::iterator it) { cerr << endl; } template<typename T, typename... Args> inline void __evars(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == ' ', it->length()) << "="; __evars_out_var(a); cerr << "; "; __evars(++it, args...); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; //template<class T, class H>using umap=unordered_map<T,H,custom_hash>; //template<class T>using uset=unordered_set<T,custom_hash>; ll dmod(ll x){ return ((x+mod)%mod); } ll modular_power(ll x,ll y){ ll ans=1; while(y){ if(y&1)ans=dmod(ans*x); y/=2; x=dmod(x*x); } return ans; } ll inv(ll x){ return modular_power(dmod(x),mod-2); } int main() { //clock_t clk = clock(); fio //cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n'; ll t=1; //cin>>t; while(t--) { ll n,ans=0;cin>>n;ll a[n];ll b[n];ll c[n];ll d[n]; fr cin>>a[i]; fr cin>>b[i]; fr cin>>c[i]; fr { d[i]=b[c[i]-1]; } unordered_map<ll,ll,custom_hash>mp1; unordered_map<ll,ll,custom_hash>mp2; fr { mp1[a[i]]++; mp2[d[i]]++; } for(auto it:mp1) { ans+=(it.second*mp2[it.first]); } cout<<ans; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; using P = pair<int,int>; int main() { int n; cin >> n; vector<int> a(n),b(n),c(n); rep(i,n) cin >> a[i]; rep(i,n) cin >> b[i]; rep(i,n) cin >> c[i]; // 0<= i <=n and 0<=j <n // a[i] == b[c[j]] // a[i] == b[k] b[k]の値が a[i] に等しい i の数をam[k] に貯める map<int,int> am,bm; // a の値 の数 値 でindex b の値にとマッチさせる rep(i,n) am[a[i]]++; // c の値 の 数 値でindex でもb のindex にする rep(i,n) bm[c[i]-1]++; ll ans = 0; rep(i,n) ans += am[b[i]]*bm[i]; cout << ans << endl; return 0; }
#include<bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #define fp(x, a, b) for(int x = a; x < b; x++) #define fn(x, a, b) for(int x = a; x > b; x--) #define f(x, m) for(auto x : m) #define cpu() ios::sync_with_stdio(false); cin.tie(nullptr) #define pb push_back #define pii pair<int,int> #define pll pair<ll, ll> #define vi vector<int> #define vl vector<ll> #define vii vector<pair<int ,int>> #define vll vector<pair<ll ,ll>> #define all(v) v.begin(),v.end() #define sor(a) sort( a.begin(), a.end() ) #define ros(a) sort( a.rbegin(), a.rend()) #define prec(n) fixed << setprecision(n) #define ff first #define ss second // #define tt third #define print(x) for(auto it : x) cout << it << " "; #define debug(x) cerr << #x << " is " << x << endl; // #define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> typedef long long ll; using namespace std; // using namespace __gnu_pbds; #define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {cout << "NEXT\n"; } template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); } template<typename... T> void rd(T& ... args){ ((cin >> args), ...); } template<typename... T> void ps(T ... args){ ((cout << args << ' '), ...); cout<<'\n'; } const ll MOD = 1e9 + 7, MOD1 = 998244353LL, MAXI = 5e6 + 100, MAX = 1e5 + 5; const char nl = '\n'; const int INF = 1e9 + 5; int a, b, c; void solve(){ cin >> a >> b >> c; if((c == 0 && a >= b + 1) || (c == 1 && a >= b)){ cout << "Takahashi\n"; } else{ cout << "Aoki\n"; } } int main(){ cpu(); // int __; // cin >> __; // while(__--){ // solve(); // } solve(); // cout << nl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back using namespace std; template <class T = int> using V = vector<T>; template <class T = int> using VV = V<V<T>>; using ll = long long; using ld = long double; using pii = pair<int,int>; using pll = pair<ll,ll>; using pdd = pair<ld,ld>; const int mod = 1000000007; struct mint { ll x; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if(!t) return 1; mint a = pow(t>>1); a *= a; if(t&1) a *= *this; return a; } mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} struct combination { vector<mint> fact, ifact; combination(int n):fact(n+1),ifact(n+1) { assert(n < mod); fact[0] = 1; for(int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i; ifact[n] = fact[n].inv(); for(int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i; } mint operator()(int n, int k) { if(k < 0 || k > n) return 0; return fact[n]*ifact[k]*ifact[n-k]; } mint p(int n, int k) { return fact[n]*ifact[n-k]; } } c(2000005); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; if(n > m+k){ cout << 0 << endl; } else cout << c(n+m, n) - c(n+m, m+k+1) << endl; }
#include<stdio.h> int main(void){ int a,b,c; scanf("%d %d %d",&a,&b,&c); printf("%d",21-(a+b+c)); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; cout<<21-a-b-c<<endl; }
#include <bits/stdc++.h> using namespace std; #define int long long #define forn(i,a,n) for (int i=a; i<n; i++) int n; int a[1001],b[1001]; signed main(){ ios::sync_with_stdio(false); cin.tie(0); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); cin>>n; forn(i,0,n) cin>>a[i]>>b[i]; int cnt=0; forn(i,0,n){ forn(j,i+1,n){ long double x=a[i]-a[j]; long double y=b[i]-b[j]; if (y/x>=-1 && y/x<=1){ cnt++; } } } cout<<cnt<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; template<typename T, typename V> using umap = unordered_map<T, V>; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vs = vector<string>; using mapii = map<int, int>; using umapii = umap<int, int>; #define _rep(i, n) _repi(i, 0, n) #define _repi(i, a, b) for(ll i = (ll) (a); i < (ll) (b); i++) #define _get_rep(_1, _2, _3, NAME, ...) NAME #define rep(...) _get_rep(__VA_ARGS__, _repi, _rep) (__VA_ARGS__) #define all(x) x.begin(), x.end() #define chmax(x, y) (x) = max((x), (y)) #define chmin(x, y) (x) = min((x), (y)) #define mkpair make_pair #define print(...) osout(cout, __VA_ARGS__) #define dbg(...) osout(cerr, __VA_ARGS__) template<typename T> istream& operator >>(istream& is, vector<T>& v) { rep(i, v.size()) { cin >> v[i]; } return is; } template<typename T1, typename T2> ostream& operator <<(ostream& os, pair<T1, T2> p) { os << p.first << ", " << p.second; return os; } template<typename T> ostream& operator <<(ostream& os, vector<T> v) { if(v.size()==0){ return os; } rep(i, v.size()-1) { os << (v[i]) << ' '; } os << v[v.size()-1]; return os; } void osout(ostream& os) { os << endl; } template<class S, class... T> void osout(ostream& os, S s, T... t) { os << (s); if (sizeof...(t)) os << ' '; osout(os, t...); } void solve(); int main() { cin.tie(0); ios::sync_with_stdio(false); // cout << fixed << setprecision(15); solve(); return 0; } void solve() { ll a,b,c,d; cin>>a>>b>>c>>d; print(min({a,b,c,d})); }
#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 pb push_back #define ll long long #define ld long double #define sz(x) (ll)x.size() #define vll vector<long long> #define infi INT_MAX #define infl LONG_LONG_MAX #define infd LDBL_MAX #define f(i,a,b) for(ll i=a;i<b;i++) #define fi(i,a,b) for(ll i=(b-1);i>=a;i--) #define F first #define S second #define G(a,b) get<a>(b) #define MP make_pair #define MT make_tuple #define pll pair<ll,ll> #define endl "\n" #define ALL(v) v.begin(),v.end() #define nl cout<<"\n"; #define pr(x) cout<<x; #define pr1(x) cout<<x<<" "; #define pr2(x,y) cout<<x<<" "<<y; #define pr3(x,y,z) cout<<x<<" "<<y<<" "<<z; #define pr4(x,y,z,w) cout<<x<<" "<<y<<" "<<z<<" "<<w; #define deb cout<<"***************************************************************************************\n"; #define moshi cout<<"moshi moshi ! \n"; #define hi cout<<"hi ! \n"; #define bye cout<<"bye bye ! \n"; #define kawai cout<<"O kawai koto ! \n"; #define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define number_of_set_bits __builtin_popcountll #define eb emplace_back const ld PI=3.1415926535897932384626433; template<class T> using min_heap = priority_queue<T,vector<T>,greater<T> >; auto clk=clock(); mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cout << name << " : " << arg1 <<"\n"; //use cerr if u want to display at the bottom } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif //********************************************************** Code begins *********************************************** // trick for traversing all submasks // for (ll j=mask ;j; j=(j-1)&mask) // also include 0 manually // v.resize(unique(v.begin(), v.end()) - v.begin()); // remove duplicates char s[15]; void solve() { f(i,0,12) cin>>s[i]; ll ans=0; f(i,0,9) if(s[i]=='Z' && s[i+1]=='O' && s[i+2]=='N' && s[i+3]=='e') ans++; cout<<ans<<"\n"; } int main() { ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(8); ll test_cases=1; //cin>>test_cases; f(i,1,test_cases+1) { solve(); //clr(); } #ifdef rd cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl; #endif return 0; }
#include <bits/stdc++.h> #define DeltaCube23 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define pb push_back #define mp make_pair #define minimum(a) *min_element(a.begin(), a.end()) #define maximum(a) *max_element(a.begin(), a.end()) #define MOD 1000000007 #define forf(i,a,b) for(int i = (a); i < (b); i++) #define forr(i,a,b) for(int i = (a); i >= (b); i--) typedef long long int lli; lli binpow(lli a,lli b,lli m){lli res=1;for(b%=m-1,a%=m;b>0;b>>=1,a=a*a%m)if(b&1)res=res*a%m;return res;} lli gcd(lli a, lli b) {return b? gcd(b, a%b) : a;} #define int lli //CAREFUL using namespace std; const int mx=5e5+5; signed main() { DeltaCube23 int t=1; //cin>>t; while(t--) { int n; cin>>n; vector<int> a(n); int count = 1; a[0] = 105; forf(i,6,10000) { if(i % 2 == 0) { if(i % 3 == 0 || i % 5 == 0 || i % 7 == 0) { a[count] = i; count++; if(count == n) break; } } } forf(i,0,n) cout<<a[i]<<" "; } return 0; }
#include<iostream> using namespace std; int main() { int x,y; cin >> x >> y; if(x==y) { if(x==0 && y==0) { cout << "0"; } if(x==1 && y==1) { cout << "1"; } if(x==2 && y==2) { cout << "2"; } } else { if((x==1 && y==2) || (x==2 && y==1)) { cout << "0"; } if((x==0 && y==2) || (x==2 && y==0)) { cout << "1"; } if((x==0 && y==1) || (x==1 && y==0) ) { cout << "2"; } } return 0; }
#include<bits/stdc++.h> #define w(x) int x; cin>>x; for(int tc=1;tc<=x;tc++) #define trace(x) cerr<<#x<<": "<<x<<" "<<endl; #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long #define pb push_back #define pqg priority_queue<int> #define pqs priority_queue<int,vector<int>,greater<int>> #define mpii map<int,int> #define endl "\n" #define YES cout<<"YES\n"; #define Yes cout<<"Yes\n"; #define NO cout<<"NO\n"; #define No cout<<"No\n"; #define mod 1000000007 #define inp(x) cin>>x; using namespace std; void solve(int tc) { int a, b; cin >> a >> b; int c = b; a += b; b = c; if (a >= 15 and b >= 8) { cout << 1 << endl; } else if (a >= 10 and b >= 3) { cout << 2 << endl; } else if (a >= 3) { cout << 3 << endl; } else cout << 4 << endl; } int32_t main() { FIO #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif // w(x){ solve(1); // } return 0; }
#pragma GCC optimize(2) #include<iostream> #include<unordered_map> #include<algorithm> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<queue> #include<stack> #include<cmath> #include<map> #include<set> #define Buff ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define rush() int Case = 0; int T; cin >> T; while(T--) #define rep(i, a, b) for(int i = a; i <= b; i ++) #define per(i, a, b) for(int i = a; i >= b; i --) #define reps(i, a, b) for(int i = a; b; i ++) #define clc(a, b) memset(a, b, sizeof(a)) #define Buff ios::sync_with_stdio(false) #define readl(a) scanf("%lld", &a) #define readd(a) scanf("%lf", &a) #define readc(a) scanf("%c", &a) #define reads(a) scanf("%s", a) #define read(a) scanf("%d", &a) #define lowbit(n) (n&(-n)) #define pb push_back #define sqr(x) x*x #define lson rt<<1 #define rson rt<<1|1 #define ls lson, l, mid #define rs rson, mid+1, r #define y second #define x first using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int>PII; const int mod = 1e9+7; const double eps = 1e-6; const int N = 1e6+7; string s; string t = "atcoder"; bool judge() { reps(i, 0, s[i]) if(s[i] != 'a') return false; return true; } int main() { int T; cin >> T; while(T --) { cin >> s; if(judge()) { puts("-1"); continue; } int res = mod; for(int i = 0; s[i]; i ++) { for(int j = i; s[j]; j ++) { swap(s[i], s[j]); if(s > t) { res = min(res, j-i); swap(s[i], s[j]); break; } swap(s[i], s[j]); } } cout << res <<endl; } return 0; } /* aaaaaaatcoder */
#include <bits/stdc++.h> using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v).begin(), (v).end() #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) #define DEBUG #define int long long #define INF 1e18 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 T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template<class T> ostream& operator << (ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template<class T> ostream& operator << (ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";} typedef long long ll; string x="atcoder"; void solve(){ string s; cin>>s; //cerr<<string(s.size(),'a')<<endl;; if(s>x){ cout<<0<<endl; return ; } if(s==string(s.size(),'a')){ cout<<-1<<endl; } else{ for (int i = 0; i < s.size(); i++) { if(s[i]!='a'){ if(s[i]>'t'){ cout<<i-1<<endl; } else{ cout<<i<<endl; } return ; } } } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin>>t; while(t--){ solve(); } return 0; }
/* Author : Aditya Gupta Team : TechTurtle Date : 29-08-2020 Time : 20:05:54 */ #include<bits/stdc++.h> #define ll long long int #define ld long double #define vll vector<ll> #define vvll vector< vll > #define vld vector< ld > #define vvld vector< vld > #define pll pair<ll ,ll > #define vpll vector< pll > #define mp make_pair #define pb push_back #define MOD 1000000007 #define endl "\n" #define flash ios_base::sync_with_stdio(false);cin.tie(NULL); #define test ll t;cin>>t;while(t--) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define fst first #define scd second #define mk(arr,n,type) type *arr=new type[n]; #define forn(i,n) for(ll (i) = 0 ; (i) < (n) ; ++(i)) #define for1(i,n) for(ll (i) = 1 ; (i) <= (n) ; ++(i)) #define forr(i,n) for(ll (i) = (n)-1 ; (i)>=0 ; --(i)) #define forab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c)) using namespace std; ///////////////////////////////////////////////////////////////////////// Maths ////////////////////////////////////////////////////////////////////////////// //vll sieve; //void Sieve(int N){ //smallest prime factor // const ll maxn = N; // sieve.resize(maxn); // forn(i,maxn) sieve[i] = i; // sieve[1] = -1; // sieve[0] = -1; // forab(i,2,maxn,1) if(i == sieve[i]) for(ll j = i*i ; j < maxn ; j+=i) if(sieve[j] == j) sieve[j] = i; // //// for(auto i:sieve){ //// cout<<i<<" "; //// } // //} vll sieve; vll* Sieve(int N) { const ll maxn = N; sieve.resize(maxn); forn(i,maxn) sieve[i] = 1; sieve[1] = 0; sieve[0] = 0; forab(i,2,maxn,1) if(1 == sieve[i]) for(ll j = i*i ; j < maxn ; j+=i) sieve[j] = 0; vll* primes=new vll(); primes->pb(2); for(int i=3; i<maxn; i+=2) if(sieve[i]) { primes->pb(i); } return primes; } ll extended_GCD(ll a , ll b , ll &x , ll &y) { if(a == 0) { x = 0; y = 1; return b; } ll x1 , y1; ll gcd = extended_GCD(b%a , a , x1 , y1); x = y1 - (b/a)*x1; y = x1; return gcd; } ll ncr(ll n,ll k) { ll r=1; if(k>n-k) k=n-k; for(ll i=0; i<k; i++) { r*=(n-i); r/=(i+1); } return r; } ll power(ll a, ll b,ll m = MOD) { ll res = 1; while (b > 0) { if (b & 1) res = (res * a)%m; a = (a * a)%m; b >>= 1; } return res; } ll modinv(ll a , ll mod = MOD) { ll x , y; extended_GCD(a , mod , x , y); if(x < 0) x += mod; return x; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// bool prime(int n) { int c=0; for(int i=2; (i*i)<=n; i++) { if(n%i==0) { c++; return false; } } if(c==0) return true; } // bool compareInterval(char i1, char i2) //{ // return (i1 < i2); //} // int countoperation(int n,m){ // vector<int> d<m+1,-1>; // } vector<int> v[200006]; int vis[200006]; //int c; int in[200006]; vector<int> res; bool khan(int n){ priority_queue<int,vector<int>,greater<int>> q; for(int i=1;i<=n;i++){ if(in[i]==0) q.push(i); } while(!q.empty()){ int curr=q.top(); res.pb(curr); q.pop(); for(int node:v[curr]){ in[node]--; if(in[node]==0) q.push(node); } } return n==res.size(); } void dfs(int n){ vis[n]=1; cout<<n<<" "; for(int i:v[n]){ if(!vis[i]) dfs(i); } } void solve() { ll n,m,t; vector<pll> v; cin>>n>>m>>t; ll f=1; ll l=0; ll mx=n; forn(i,m) { ll a,b; cin>>a>>b; v.pb({a,b}); n-=(a-l); if(n<=0) f=0; if(n+(b-a)>mx) n=mx; else n+=(b-a); // cout<<n<<"*"; l=b; if(i==m-1){ n-=t-b; if(n<=0) f=0; } } if(f) cout<<"Yes\n"; else cout<<"No\n"; } int main() { flash // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); // test solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr ll MOD = 1000000007; #define REP(i, n) for (ll i = 0; i < (n); i++) #define REP2(i, x, n) for (ll i = x; i < (n); i++) #define PR(x) cout << (x) << "\n" #define PS(x) cout << (x) << " " #define PRYES(x) PR((x) ? "Yes" : "No") const long long INF = numeric_limits<long long>::max(); template <class T> using arr = vector<vector<T> >; using vint = vector<int>; using vll = vector<ll>; // SourceTest用出力マクロ #ifdef SOURCE_TEST #define SPR(x) PR(x) #define SPS(x) PS(x) #define STRACE(var) cout << #var << "=" << var << endl; #else #define SPR(x) \ {} #define SPS(x) \ {} #define STRACE(var) \ {} #endif bool isOK(ll N, ll M, ll T) { ll nMax = N; ll nowTime = 0; for (ll i = 0; i < M; ++i) { ll A, B; cin >> A >> B; N -= A - nowTime; SPR(N); if (N <= 0) return false; N += B - A; N = min(N, nMax); SPR(N); nowTime = B; } N -= T - nowTime; SPR(N); return N > 0; } int main() { ll N, M, T; cin >> N >> M >> T; PR(isOK(N, M, T) ? "Yes" : "No"); return 0; }
#include<bits/stdc++.h> #define ll long long #define mp make_pair #define f(i,n) for(int i=0;i<n;i++) #define F first #define S second #define pb push_back using namespace std; void test(){ ll n,m; cin>>n>>m; ll o = 0, e = 0; f(i,n){ string s; cin>>s; int cnt = 0; f(j,m){ if(s[j]=='1')cnt++; } if(cnt%2==0)e++; else o++; } ll res = o*e; cout<<res<<"\n"; } int main(){ std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tests=1; // cin>>tests; while(tests--){ test(); } }
// 2021-04-17 16:11:14 // clang-format off #include <bits/stdc++.h> #ifdef LOCAL #include "lib/debug.hpp" #else #define debug(...) 1 #endif #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define rep(i, n) REP(i, 0, (n)) #define repc(i, n) REPC(i, 0, (n)) #define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; } template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; // clang-format on void answer() { ll x, y, z; cin >> x >> y >> z; ll ans = -1; rep(a, 2000000) { if (a * x < y * z) chmax(ans, ll(a)); } cout << ans << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); answer(); return 0; }
using namespace std; #include<string> #include<cmath> #include<list> #include <map> #include <unordered_map> #include <set> #define ll long long #define ld long double #define ull unsigned long long #define ml map<ll,ll> #define pb push_back #define mp make_pair #define ppl pair<ll,ll> #define F first #define S second #define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL) #define fr(i,a,b) for(ll i=a;i<b;i++) #define ffr(i,a,b) for(ll i=a;i>=b;i--) #include<bits/stdc++.h> #include<vector> #define vl vector<ll> #define PI 3.141592654 const ll N = 1e9 + 7; #include<iterator> #define EPSILON numeric_limits<double>::epsilon() // vl b; // vector<vl> v; // ll n, m; // vl link, val; // vector<char>a; // vector<bool> visited; // void dfs(ll pnt, ll lin) { // link[pnt] = lin; // visited[pnt] = true; // fr(i, 0, v[pnt].size()) { // if (!visited[v[pnt][i]])dfs(v[pnt][i], lin); // } // } // vl pr(N, 0); // void precompute() { // pr[1] = 1; // fr(i, 2, N + 1) { // ll d = i, g = 0; // while (d > 0) { // if (d % 10 == 7) {g = 1; break;} // d /= 10; // } // if (g) {pr[i] = pr[i - 1]; continue;} // d = i; // while (d > 0) { // if (d % 8 == 7) {g = 1; break;} // d /= 8; // } // if (g) {pr[i] = pr[i - 1]; continue;} // else pr[i] = pr[i - 1] + 1; // } // } // void prime() { // pr[1] = 1; // fr(i, 2, N + 1)pr[i] = i; // fr(i, 4, N + 1) {pr[i] = 2; i++;} // fr(i, 3, N + 1) { // if (pr[i] == i) { // for (ll j = i * i; j <= N; j += i) {if (pr[j] == j)pr[j] = i;} // } // } // } ll gcd(ll a, ll b) { if (a == b)return a; if (a % b == 0)return b; if (b % a == 0)return a; if (b > a)return gcd(a, b % a); if (a > b)return gcd(a % b, b); return 0; } // ll fact(ll n) { // ll res = 1; // fr (i, 2, n + 1) res = (res * i) % N; // return res; // } // ll nCr(ll n, ll r) { // return fact(n) / (fact(r) * fact(n - r)); // } void solve() { ll n; cin >> n; ll a[n], b[n], ma = 0, maxi = 0, prev = 0; fr(i, 0, n)cin >> a[i]; fr(i, 0, n)cin >> b[i]; fr(i, 0, n) { if (a[i] > ma) { ma = a[i]; maxi = 0; } maxi = max(maxi, b[i]); cout << max(prev, maxi * ma) << '\n'; prev = max(maxi * ma, prev); } } int main() { fio; #ifndef ONLINE_JUDGE // for getting input freopen("input.txt", "r", stdin); //for writing output freopen("jout.txt", "w", stdout); #endif // prime(); // precompute(); ll t; if (1) t = 1; else cin >> t; while (t--) { //cout << "Case #" << h << ": "; solve(); //h++; cout << '\n'; } return 0; }
#include <iostream> #include <cstdio> using namespace std; typedef long long LL; const int N = 200010; LL a[N], b[N]; long long c[N]; int main() { int n; cin >> n; for (int i = 0; i < n; i ++) scanf("%d", &a[i]); for (int i = 0; i < n; i ++) scanf("%d", &b[i]); LL res = 0, maxd = 0; res = c[0] = a[0] * b[0]; maxd = a[0]; for (int i = 1; i < n; i ++) { maxd = max(a[i], maxd); if (maxd * b[i] > res) { c[i] = res = maxd * b[i]; } else c[i] = res; } for (int i = 0; i < n; i ++) printf("%lld\n", c[i]); return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; //reverse(s.begin(),s.end()) ll gcd(ll a,ll b){ ll s=1,c; while(a&&b){ if((~a&1)&&(~b&1)) a>>=1,b>>=1,s<<=1; else if(~a&1)a>>=1; else if(~b&1)b>>=1; else if(a>b)a=a-b; else c=b-a,b=a,a=c; } if(!a)return b*s; if(!b)return a*s; } int main(){ int n,m; cin>>n>>m; int x,y; cin>>x>>y; char mz[n+1][m+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>mz[i][j]; } } int ans=1; int tx=x; int ty=y; while(mz[tx+1][y]=='.'){ ans++; tx++; } tx=x; while(mz[tx-1][y]=='.'){ ans++; tx--; } ty=y; while(mz[x][ty+1]=='.'){ ans++; ty++; } ty=y; while(mz[x][ty-1]=='.'){ ans++; ty--; } cout<<ans; }
#include <algorithm> #include <array> #include <cassert> #include <chrono> #include <cmath> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <vector> using namespace std; using ll = long long; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define sz(x) (int)(x).size() #define ms(arr, v) memset(arr, v, sizeof(arr)) #define f first #define s second #define pb push_back // clang-format off template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template<typename T_vector>void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; } template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << "(" << v.first << ":" << v.second << ")" << ","; os << "}"; return os; } auto random_address = [] {char *p = new char; delete p;return uint64_t(p);}; const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1); mt19937_64 rng(SEED); // clang-format on void solve() { } int main() { ios::sync_with_stdio(0); cin.tie(0); int m, n, x, y; cin >> m >> n >> x >> y; string mat[m]; for (int i = 0; i < m; i++) { cin >> mat[i]; } int ans = 0; --x; --y; int t1 = x, t2 = y, t3 = x, t4 = y; while (t1 + 1 < m) { if (mat[t1 + 1][y] == '#') break; else if (mat[t1 + 1][y] == '.') { ans++; } t1++; } while (t2 + 1 < n) { if (mat[x][t2 + 1] == '#') break; else if (mat[x][t2 + 1] == '.') { ans++; } t2++; } while (t3 - 1 >= 0) { if (mat[t3 - 1][y] == '#') break; else if (mat[t3 - 1][y] == '.') { ans++; } t3--; } while (t4 - 1 >= 0) { if (mat[x][t4 - 1] == '#') break; else if (mat[x][t4 - 1] == '.') { ans++; } t4--; } cout << ans + 1; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; ll MOD = (ll)1000000007; vector<ll> nums; int main() { ios::sync_with_stdio(false); cin.tie(0); int t, a, p, x, ans=1999999999, n; ll k; string s1, s2; bool sad = false; cin >> n; while(n--) { cin >> a >> p >> x; if(a < x) ans = min(ans, p); } if(ans == 1999999999) cout << "-1\n"; else cout << ans << '\n'; }
#include <bits/stdc++.h> #define rep(i, n) for(int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++) #define ALL(v) (v).begin(), (v).end() #define LLA(v) (v).rbegin(), (v).rend() #define SZ(v) (v).size() #define INT(...) \ int __VA_ARGS__; \ read(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ read(__VA_ARGS__) #define DOUBLE(...) \ double __VA_ARGS__; \ read(__VA_ARGS__) #define CHAR(...) \ char __VA_ARGS__; \ read(__VA_ARGS__) #define STRING(...) \ string __VA_ARGS__; \ read(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ read(name) using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using Graph = vector<vector<int>>; template <typename T> struct edge { int to; T cost; edge(int t, T c) : to(t), cost(c) {} }; template <typename T> using WGraph = vector<vector<edge<T>>>; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; template <class T> inline vector<T> make_vec(size_t a, T val) { return vector<T>(a, val); } template <class... Ts> inline auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec(ts...))>(a, make_vec(ts...)); } void read() {} template <class T> inline void read(T &a) { cin >> a; } template <class T, class S> inline void read(pair<T, S> &p) { read(p.first), read(p.second); } template <class T> inline void read(vector<T> &v) { for(auto &a : v) read(a); } template <class Head, class... Tail> inline void read(Head &head, Tail &...tail) { read(head), read(tail...); } template <class T> inline void chmax(T &a, T b) { (a < b ? a = b : a); } template <class T> inline void chmin(T &a, T b) { (a > b ? a = b : a); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); INT(n); int res = INF; rep(i, n) { INT(a, x, p); if(p - a > 0) chmin(res, x); } cout << (res == INF ? -1 : res) << endl; }
#include<bits/stdc++.h> using namespace std; int main(){ int v,t,s,d; cin >> v >> t >> s >> d; if(v*t <= d && v*s >= d){ cout << "No" << endl; }else{ cout << "Yes" << endl; } }
/* Code by Brother_mohit */ #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include<bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0); cin.tie(0); #define tc int tt; cin>>tt; for(int t=1; t<=tt; t++) #define ll long long #define um unordered_map #define us unordered_set #define pq priority_queue #define mod 1000000007 #define endl "\n" using namespace std; // main code and functions *******************->\ int main() { fastio // tc { // int n ; // cin>>n; // vector<int> a(n); // for(int i=0; i<n; i++) // { // cin>>a[i]; // } int v,t,s,d; cin>>v>>t>>s>>d; int d1= v*t; int d2= v*s; if(d<d1||d>d2) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int INF = 2e9; const ld EPS = 1e-9; const int M = 31; const int N = 1e6 + 10; char c[5]; /* char get_ch(char a, char b) { if (a == 'A') { if (b == 'A') return c[0]; return c[1]; } if (b == 'A') return c[2]; return c[3]; } */ set<string> pow2 = {"ABAA", "BABA", "BABB", "BBAA"}; set<string> fib = {"ABBA", "BAAA", "BAAB", "BBBA"}; const ll MOD = 1e9 + 7; void solve() { int n; cin >> n; string s; for (int i = 0; i < 4; ++i) { cin >> c[i]; s += c[i]; } if (pow2.count(s)) { ll ans = 1; for (int i = 3; i < n; ++i) { ans = (ans * 2) % MOD; } cout << ans << endl; } else if (fib.count(s)) { ll f0 = 1; ll f1 = 1; for (int i = 3; i < n; ++i) { ll f_tmp = (f0 + f1) % MOD; f0 = f1; f1 = f_tmp; } cout << f1 << endl; } else { cout << 1 << endl; } /* string s = "AB"; set<string> ss = {s}; for (int len = 3; len < 10; ++len) { set<string> news; for (auto s: ss) { for (int pos = 0; pos + 1 < (int)s.size(); ++pos) { string t = s; t.insert(t.begin() + pos + 1, get_ch(t[pos], t[pos + 1])); news.insert(t); } } ss = news; for (auto s: ss) cout << s << "\t"; cout << endl << ss.size() << endl; cout << endl; } */ } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; while (t--) { solve(); } }
#include<bits/stdc++.h> #include<algorithm> #include<cmath> #include<climits> using namespace std; typedef long long int lli; typedef vector<int> vi; typedef vector<long long int> vlli; typedef pair<int,int> pii; typedef pair<long long int,long long int> plli; typedef vector< vi > vvi ; typedef vector< vlli > vvlli ; #define fi(i,a,b) for(int i=a;i<=b;i++) #define flli(i,a,b) for(long long int i=a;i<=b;i++) #define bi(i,a,b) for(int i=a;i>=b;i--) #define blli(i,a,b) for(long long int i=a;i>=b;i--) #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) #define all(x) x.begin(),x.end() #define sz(x) x.size() #define pi 2*acos(0.0) #define pb push_back #define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++) #define present(v,num) (v.find(num)!=v.end()) #define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end()) #define pq priority_queue #define mp make_pair const int inf=INT_MAX; const lli INF =LLONG_MAX; const lli mod = 1e9+7; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast; string s;cin>>s; deque<char> a; bool flag=true; lli n=sz(s); flli(i,0,n-1) { if(s[i]=='R') { flag=flag^true; } else { if(flag) { if(a.empty()) { a.push_back(s[i]); continue; } auto vari=a.rbegin(); if(*vari!=s[i]) a.push_back(s[i]); else a.pop_back(); } else { if(a.empty()) { a.push_front(s[i]); continue; } auto vari=a.begin(); if(*vari!=s[i]) a.push_front(s[i]); else a.pop_front(); } } } string t; while(!a.empty()) { auto vari=a.begin(); t.pb(*vari); a.pop_front(); } if(!flag)reverse(all(t)); tr(t,it)cout<<*it; cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' // #define int long long int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pair<int,int>> vec; for(int i = 0; i < (1 << n); i += 2) { int x, y; cin >> x >> y; if(x > y) vec.push_back({x, i}); else vec.push_back({y, i + 1}); if(n == 1) { if(x < y) cout << 1 << endl; else cout << 2 << endl; return 0; } } while(vec.size() != 2) { vector<pair<int,int>> temp; for(int i = 0; i < vec.size(); i += 2) { temp.push_back(max(vec[i], vec[i + 1])); } vec = temp; } if(vec[0].first > vec[1].first) cout << vec[1].second + 1 << endl; else cout << vec[0].second + 1 << endl; return 0; }
#include <iostream> #include <string> #include <vector> #include <set> #include <stack> #include <queue> #include <map> #include <algorithm> #include <iomanip> #include <math.h> #include <string.h> #include <cstdio> #include <tuple> #include <numeric> #include <functional> using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>; using vl = vector<ll>; using vll = vector<vl>; using vlll = vector<vll>; using vpll = vector<pll>; using vs = vector<string>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } const ld PI = 3.1415926535897932; #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define mp make_pair #define mt make_tuple #define pb push_back #define co(i) cout << i << endl; void in() {} void debug_out() { cerr << endl; } template<typename Head, typename... Tail> void debug_out(Head h, Tail... t) { cerr << " " << h; if (sizeof...(t) > 0) cerr << " :"; debug_out(t...); } template <typename T> ostream& operator<<(ostream& os, vector<T> vec) { for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } ll ctoi(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; default: return 0; } } bool pairCompare(const pll firstElof, pll secondElof) { return firstElof.second < secondElof.second; } //AtCoder以外ではコメントアウトを忘れずに! //#include <atcoder/all> //using namespace atcoder; //**Snippetリスト**// //rep, vin, all, iteLoop, bitSearch, bitList, nod, LIS, digitDP, treeDP, Rerooting// //gcdlcm, isPrime, eratos, primeFactorize, Npow, combination, divisor, modinv, doubling// //dfs, bfs, dijkstra, WarshallFloyd, BellmanFord, UnionFind, Kruskal, RMQ, LCA, FordFulkerson// //**AC Libraryリスト**// //pow_mod, inv_mod, floor_sum, convolution, convolution_ll, modint// //fenwicktree, segtree, lazysegtree, string// ll i, j, k, l; ll N, M, K, H, W, X, Y, Z, R, Q, T; ll MOD = 1000000007, INF = 1LL << 60, ans = 0, z = 0, o = 1; vl D; vector<vpll> path; //***********// int main() { cin >> N; vpll A(pow(2, N)); for (i = 0; i < pow(2,N); i++) { ll a; cin >> a; A[i] = mp(a, i+1); } for (i = 1; i <= N-1; i++) { vpll B; for (j = 1; j <= pow(2,N-i); j++) { if (A[2 * j - 2].first < A[2 * j - 1].first) { B.pb(A[2 * j - 1]); } else { B.pb(A[2 * j - 2]); } } A = B; //debug(A.size()); B.clear(); } if (A[0].first < A[1].first) { cout << A[0].second; } else { cout << A[1].second; } }
#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 a, b; scanf("%d %d", &a, &b); a += b; if(a>=15 && b>=8) puts("1"); else if(a>=10 && b>=3) puts("2"); else if(a>=3) puts("3"); else puts("4"); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int ms, nfms, mf; cin >> nfms >> mf; ms = nfms + mf; if(ms >= 15 && mf >= 8){ cout << 1 << endl; } else if(ms >= 10 && mf >= 3){ cout << 2 << endl; } else if(ms >= 3){ cout << 3 << endl; } else{ cout << 4 << endl; } }
#include <bits/stdc++.h> using namespace std; #define ld long double #define ll long long #define P pair<int,int> #define fi first #define se second #define rep(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(),v.end() #define pb push_back #define eb emplace_back template<class T>void chmax(T &a,T b){if(a<b)a=b;} template<class T>void chmin(T &a,T b){if(a>b)a=b;} constexpr int INF=1000000000; constexpr ll llINF=1000000000000000000; constexpr int mod=1000000007; constexpr double eps=1e-10; const double pi=acos(-1); int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; int Random(int mi,int ma){ random_device rnd; mt19937 mt(rnd());//32bit //[mi,ma] uniform_int_distribution<int>engine(mi,ma); return engine(mt); } int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b); } int lcm(int a,int b){ return a/gcd(a,b)*b; } bool prime(int a){ if(a==1)return false; for(int i=2;i*i<=a;i++){ if(a%i==0)return false; } return true; } int modpow(int a,int b){ if(b==0)return 1; if(b%2)return modpow(a,b-1)*a%mod; int memo=modpow(a,b/2); return memo*memo%mod; } vector<int>kaijo,invkaijo; void init_fact(int n){ kaijo.resize(n+1); invkaijo.resize(n+1); kaijo[0]=1; for(int i=1;i<=n;i++){ kaijo[i]=kaijo[i-1]*i; kaijo[i]%=mod; } rep(i,n+1)invkaijo[i]=modpow(kaijo[i],mod-2); } int comb(int a,int b){ if(a<b)return 0; if(a<0||b<0)return 0; return kaijo[a]*invkaijo[a-b]%mod*invkaijo[b]%mod; } int inv(int x){ x=modpow(x,mod-2); return x; } int main(){ cin.tie(0);ios::sync_with_stdio(false); int n; cin>>n; ll d[100010]; rep(i,n)cin>>d[i]; { ll sum=0; rep(i,n)sum+=abs(d[i]); printf("%lld\n",sum); } { ll sum=0; rep(i,n){ sum+=d[i]*d[i]; } if(sum==0)printf("%lld\n",(ll)0); else{ double h=1e9,l=0; rep(i,1000){ double mid=(l+h)/2; if(mid*mid>=(double)sum-eps)h=mid; else l=mid; } printf("%.12lf\n",h); } } { ll ans=0; rep(i,n)chmax(ans,abs(d[i])); printf("%lld\n",ans); } return 0; }
#include<iostream> #include<vector> #include<cmath> using namespace std; using ll = long long; int main() { int N,i; cin>>N; vector<ll> x(N); for (i=0;i<N;i++) { cin>>x[i]; x[i] = abs(x[i]); } ll l1=0, l3=0; long double l2=0; for (i=0;i<N;i++) { l1 += x[i]; l2 += x[i]*x[i]; l3 = max(l3, x[i]); } cout<<l1<<endl; printf("%.15Lf\n", sqrt(l2)); cout<<l3<<endl; return 0; }
#include<iostream> using namespace std; int main() { int A,B,C; cin>>A>>B>>C; if(C==0){ if(A>B){ cout<<"Takahashi"<<endl; }else{ cout<<"Aoki"<<endl; } } if(C==1){ if(A<B){ cout<<"Aoki"<<endl; }else{ cout<<"Takahashi"<<endl; } } return 0; }
#include <stdio.h> #define _USE_MATH_DEFINES #include <math.h> #include <vector> #include <algorithm> #include <iostream> #include <map> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; if(a+c > b){ cout << "Takahashi"; } else { cout << "Aoki"; } return 0; }
#include<bits/stdc++.h> #define INF 0x3f3f3f3f #define inf 0x3f3f3f3f3f3f3f3f using namespace std; typedef long long ll; typedef pair<int,int>P; int n,m; char tu[105][105]; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) cin>>tu[i]+1; int res=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(tu[i][j]=='.'&&tu[i][j-1]=='.')res++; if(tu[i][j]=='.'&&tu[i-1][j]=='.')res++; } } cout<<res; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < n; ++i) using ll = long long; using P = pair<int,int>; int main() { int h,w;cin>>h>>w; vector<string> v; string last(w+1,'#'); rep(i,h){ string s;cin>>s; v.push_back(s); v[i].push_back('#'); } int ans=0; v.push_back(last); rep(i,h){ rep(j,w){ if(v[i][j]=='.'){ ans+=(v[i][j+1]=='.'); ans+=(v[i+1][j]=='.'); } } } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; int main() { long long a, b, k; cin >> a >> b >> k; long long a1=a,b1=b; long long m = 1ll; for (long long i = 1ll; i <= b; i++) { m *= a + i; m /= i; } long long l = 1ll, u = m; for (long long i = 0; i < (a1 + b1); i++) { if(a+b==0) continue; if (u - m * b / (a + b) >= k) { cout << 'a'; u -= m*b/(a+b); m *= a; m /= (a + b); a--; } else { cout << 'b'; l += m*a/(a+b); m *= b; m /= (a + b); b--; } } cout << endl; }
#include <bits/stdc++.h> using namespace std; //#pragma GCC optimize "trapv" #define ll long long int const siz = 60; ll nCr[siz+1][siz+1]; void precomputation() { nCr[1][0] = nCr[1][1] = nCr[0][0] = 1; for (int i=2;i<=siz;i++) { nCr[i][0] = 1; for (int j=1;j<=i;j++) nCr[i][j] = nCr[i-1][j] + nCr[i-1][j-1]; } } void solve() { int a,b; ll k; cin>>a>>b>>k; int n = a+b; for (int i=n;i>=1;i--) { ll cnt = nCr[i-1][b]; //cout<<cnt<<" "<<k<<endl; if (cnt >= k) { cout<<"a"; } else { cout<<"b"; b--; k -= cnt; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); precomputation(); solve(); return 0; }
#include<bits/stdc++.h> #define reg register #define For(i,a,b) for(reg int i=(a),i##END=(b);i<=i##END;i++) #define Rof(i,b,a) for(reg int i=(b),i##END=(a);i>=i##END;i--) #define go(u) for(reg int i=head[u];i;i=nxt[i]) #define int long long using namespace std; inline int read(){ int 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 N=5e3+10,p=998244353; int a[N],b[N]; signed main(){ int n=read(),m=read(); a[0]=1;For(i,1,n)a[i]=a[i-1]*m%p; For(x,1,m-1){ int t=1;For(i,1,N-10)(t*=x)%=p,(b[i]+=t)%=p; } b[0]=m; int ans=a[n]*n%p; // cout<<ans<<endl; For(i,1,n)For(j,i+1,n){ (ans-=a[n-(j-i)-1]*b[(j-i)-1]%p)%=p; } cout<<(ans+p)%p<<endl; return 0; }
#define _CRT_SECURE_NO_DEPRECATE #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; #define int long long int #define SYNC std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); #define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); typedef long double ld; typedef pair<int,int> ii; typedef pair<int,ii> iii; typedef vector<int> vi; typedef vector<ii> vii; //typedef tree<int, null_type, less<int>, rb_tree_tag, // tree_order_statistics_node_update> // ost; #define rep(i,l,r) for (int i = (l); i < (r); i++) #define here cout << " Hey!!\n"; #define pb push_back #define F first #define S second #define all(v) (v).begin(),(v).end() #define sz(a) (int)((a).size()) #define sq(x) ((x)*(x)) template <typename T> void etch(T V) { for (auto x : V) cout << x << ' ' ; cout << '\n'; } vector<string> vec_splitter(string s) { s += ','; vector<string> res; while(!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out( vector<string> __attribute__ ((unused)) args, __attribute__ ((unused)) int idx, __attribute__ ((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } #ifndef ONLINE_JUDGE #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) #else #define debug(...) 42 #endif const int MOD = 1e9+7; const int MOD1 = 998244353; const int N = 2e5+5; const int INF = 1000111000111000111LL; const ld EPS = 1e-12; const ld PI = 3.141592653589793116; int dp[55][2], n, p; vi ans, v; int solve(int i, int j) { if (dp[i][j] != -1) { return dp[i][j]; } if (i == n-1) { return (dp[i][j] = 1); } int lim = v[i+1]/v[i]; int cur = ans[i] + j; if (cur == lim) { dp[i][j] = solve(i+1, 1); return dp[i][j]; } if (cur) { dp[i][j] = solve(i+1, 0) + solve(i+1, 1); } else { dp[i][j] = solve(i+1, 0); } return dp[i][j]; } int32_t main() { SYNC cin >> n >> p; v.resize(n); rep(i,0,n) { cin >> v[i]; } for (int i = n-1; i >=0 ;i--) { ans.pb(p/v[i]); p %= v[i]; } reverse(all(ans)); // etch(ans); memset(dp, -1, sizeof dp); cout << solve(0, 0); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; ll mm=1000000000;ll MM=mm+7; #define rep(i, n) for(int i=0;i<n;i++) #define PI 3.141592653589793 int main(){ vector<ll> c(4); rep(i,4)cin >> c.at(i); sort(c.begin(),c.end()); ll x=c.at(0)+c.at(1)+c.at(2); if(x==c.at(3))cout << "Yes" << endl; else if(c.at(0)+c.at(3)==c.at(1)+c.at(2))cout << "Yes" << endl; else cout << "No" << endl; }
#pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx,avx2,sse,sse2") #include <functional> template <typename T> inline void hash_combine(std::size_t &seed, const T &val) { seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } template <typename T> inline void hash_val(std::size_t &seed, const T &val) { hash_combine(seed, val); } template <typename T, typename... Types> inline void hash_val(std::size_t &seed, const T &val, const Types &... args) { hash_combine(seed, val); hash_val(seed, args...); } template <typename... Types> inline std::size_t hash_val(const Types &... args) { std::size_t seed = 0; hash_val(seed, args...); return seed; } struct pair_hash { template <class T1, class T2> std::size_t operator()(const std::pair<T1, T2> &p) const { return hash_val(p.first, p.second); } }; #include<bits/stdc++.h> #include<unordered_set> #include<unordered_map> #define INF 0x3f3f3f3f #define mod 1000000007 #define G 3 #define MAX_N 300005 using namespace std; typedef long long ll; typedef pair<int,int>P; typedef complex<double> E; int n,m,a[1005],b[1005],x; int main(){ cin>>n>>m; for(int i=0;i<n;i++){ scanf("%d",&x); a[x]++; } for(int i=0;i<m;i++){ scanf("%d",&x); b[x]++; } for(int i=1;i<=1000;i++){ if(a[i]>0&&b[i]==0)printf("%d ",i); else if(b[i]>0&&a[i]==0)printf("%d ",i); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i) #define ALL(v) (v).begin(),(v).end() #define CLR(t,v) memset(t,(v),sizeof(t)) template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";} template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;} template<class T>void chmin(T&a,const T&b){if(a>b)a=b;} template<class T>void chmax(T&a,const T&b){if(a<b)a=b;} ll nextLong() { ll x; scanf("%lld", &x); return x;} int main2() { int N = nextLong(); int M = 1 << N; cout << (M - 1) << endl; for (int i = 0; i < (M - 1); i++) { string s(M, '.'); for (int a = 0; a < M; a++) { if (__builtin_popcount((a & (1+i))&(M-1)) % 2 == 0) s[a] = 'A'; else s[a] = 'B'; } cout << s << endl; } return 0; } int main() { #ifdef LOCAL for (;!cin.eof();cin>>ws) #endif main2(); return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> #define endl "\n" using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template<class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const long long INF = 1e18; //const ll mod = 1000000007; vector<string> f(ll N) { if(N == 1) { return {"AB"}; } auto v = f(N - 1); vector<string> ret; for(int i = 0; i < v.size(); i++) { auto tmp = v[i] + v[i]; ret.push_back(tmp); } for(int i = 0; i < v.size(); i++) { auto tmp = v[i]; for(auto &c: tmp) { if(c == 'A') c = 'B'; else c = 'A'; } tmp += v[i]; ret.push_back(tmp); } string S; for(int i = 0; i < (1 << N) / 2; i++) S.push_back('A'); for(int i = 0; i < (1 << N) / 2; i++) S.push_back('B'); ret.push_back(S); return ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; auto ans = f(N); cout << ans.size() << endl; for(auto s : ans) cout << s << endl; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //required //#include <ext/pb_ds/tree_policy.hpp> //required //using namespace __gnu_pbds; //required using namespace std; //template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // ordered_set <int> s; // s.find_by_order(k); returns the (k+1)th smallest element // s.order_of_key(k); returns the number of elements in s strictly less than k #define MOD (998244353) // change as required #define pb(x) push_back(x) #define mp(x,y) make_pair(x,y) #define all(x) x.begin(), x.end() #define print(vec,l,r) for(int i = l; i <= r; i++) cout << vec[i] <<" "; cout << endl; #define input(vec,N) for(int i = 0; i < (N); i++) cin >> vec[i]; #define debug(x) cerr << #x << " = " << (x) << endl; #define leftmost_bit(x) (63-__builtin_clzll(x)) #define rightmost_bit(x) __builtin_ctzll(x) #define set_bits(x) __builtin_popcountll(x) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long int ll; // highly risky #defines #define int ll // disable when you want to make code a bit faster #define endl '\n' // disable when dealing with interactive problems const int MAXN = 3002; int dp[MAXN][2*MAXN]; void solve(){ // code starts from here int N, K; cin >> N >> K; for(int i = 0; i < MAXN; i++){ dp[i][i] = 1; dp[i][0] = 0; } for(int i = 0; i < MAXN; i++){ for(int j = i+1; j < 2*MAXN; j++) dp[i][j] = 0; } for(int i = 2; i <= N; i++){ for(int j = N; j >= 1; j--){ dp[i][j] = dp[i-1][j-1] + dp[i][2*j]; dp[i][j] %= MOD; } } cout << dp[N][K] << endl; } clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); //startTime = clock(); int T = 1; // cin >> T; while(T--){ solve(); } //cerr << getCurrentTime() << endl; return 0; }
#include <bits/stdc++.h> #define ll long long int #define pb push_back #define st first #define nd second #define pii pair<int,int> #define mp make_pair #define pll pair<long long,long long> using namespace std; const int nax = 3005; const int mod = 998244353; ll dp[nax][nax]; int n,k; void solve(){ cin >> n >> k; dp[0][0] = 1; for(int i=1;i<=n;i++){ for(int j=0;j<nax;j++){ for(int c=0;c<12;c++){ if(j * (1 << c) - 1 < nax) dp[i][j] += dp[i - 1][j * (1 << c) - 1]; dp[i][j] %= mod; } } } cout << dp[n][k]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int tt = 1; // cin >> tt; while(tt--) solve(); return 0; }
#pragma GCC optimize("O3") // #include <atcoder/all> // using namespace atcoder; #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; // gp_hash_table<int, int> mapka; using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define LD long double #define FI first #define SE second #define st FI #define nd SE #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<'='<<h<<','; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif // using mint = modint998244353; // ostream& operator<<(ostream& out, mint m){ // out << m.val(); // return out; // } const int P = 998244353; void add(int &a,int b){ a += b; if(a >= P) a -= P; } void sub(int &a,int b){ a -= b; if(a < 0) a += P; } int mul(int a,int b){ return a * b % P; } int n; int res = 0; vector<vector<int>> dp; vector<int> t; void add(int x){ int pom = t[x]; int a = SZ(dp); int b = SZ(dp[0]); vector<vector<int>> dp2(a + 1,vector<int>(b + pom)); R(i,a)R(j,b){ add(dp2[i + 1][j + pom], mul(dp[i][j], i + 1)); if(j <= pom){ add(dp2[a - i][pom - j], mul(dp[i][j], a - i)); }else{ add(dp2[i][j - pom], mul(dp[i][j], a - i)); } } // debug(dp,pom,dp2); swap(dp,dp2); } void licz(int po,int ko){ if(po + 1 == ko){ debug(po,dp); R(i,SZ(dp)){ R(j,min(SZ(dp[0]), t[po])){ sub(res, dp[i][j]); } } debug(po,t[po],res); return; } int m = (po + ko) / 2; auto pam = dp; for(int i = po; i < m; i++)add(i); licz(m,ko); swap(dp,pam); for(int i = m; i < ko; i++)add(i); licz(po,m); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n; t.resize(n); res = 1; R(i,n){ cin >> t[i]; res = mul(res, i + 1); } debug(res); dp.resize(1,vector<int>(1,1)); licz(0,n); cout << res << "\n"; }
#include<bits/stdc++.h> using namespace std; const int mod = 998244353, MAXW = 100; int sum(int a, int b) { return a+b<mod?a+b:a+b-mod; } int cnt[101], f[10001][101], g[10001][101]; int inv[101], fact[101], invfact[101]; int main() { int n, tot = 0; cin >> n; for (int i = 1; i <= n; i++) { int w; cin >> w; ++cnt[w], tot += w; } if (tot & 1) return cout << 0 << endl, 0; tot >>= 1; inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = 1ll*(mod-mod/i)*inv[mod%i]%mod; fact[0] = invfact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = 1ll*fact[i-1]*i%mod; for (int i = 1; i <= n; i++) invfact[i] = 1ll*invfact[i-1]*inv[i]%mod; f[0][0] = 1; for (int i = 1; i <= MAXW; i++) { memset(g, 0, sizeof(g)); for (int j = 0; j <= n*MAXW; j++) for (int k = 0; k <= n; k++) { if (!f[j][k]) continue; for (int t = 0; t <= cnt[i]; t++) g[j+t*i][t+k] = (g[j+t*i][t+k]+1ll*f[j][k]*invfact[t]%mod*invfact[cnt[i]-t])%mod; } memcpy(f, g, sizeof(f)); } int ans = 0; for (int i = 0; i <= n; i++) ans = (ans + 1ll*fact[i]*fact[n-i]%mod*f[tot][i]) % mod; for (int i = 1; i <= MAXW; i++) ans = 1ll*ans*fact[cnt[i]]%mod; cout << ans << endl; }
//To debug : g++ -g file.cpp -o code //to flush output : fflush(stdout) or cout.flush() //cout<<setprecision(p)<<fixed<<var //use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll //take care of precedence rule of operators //do not forget to change the sizes of arrays and value of contants and other things after debugging #include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i,a,n) for(i=a;i<n;++i) #define irep(i,n,a) for(i=n;i>a;--i) #define mod 1000000007 #define pb push_back #define big 9223372036854775807 #define big1 LONG_MAX #define big2 ll_MAX #define big3 1000000000 #define sma1 LONG_MIN #define sma2 ll_MIN #define sma3 -1000000000 #define mp make_pair #define dub double #define ivec vector<ll> #define lvec vector<long long> #define cvec vector<char> #define svec vector<string> #define mt make_tuple #define MOD 998244353 #define ld long double #define pi acos(-1.0) #define SZ(x) (ll)(x.size()) //comment the below if not required /* #define ss second.second #define ff first.first #define f first #define s second #define sf second.first #define fs first.second */ #define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) //cout<<"Case #"<<c<<": "<<ans<<"\n" ; const ll M = 5e3+1 , B=14; ll dp[B][M],n,m,i,j,sum,k,temp,tot,f[M],num,den,mi[M]; ll fac(ll n,ll k) { num=f[n]; den=(mi[n-k]*mi[k])%MOD; num=(num*den)%MOD; return num; } ll modinv(ll num,ll md) { ll p = md-2; ll ans = 1; for(;p;num=(num*num)%md,p/=2) { if(p&1) ans = (ans*num)%md; } return ans; } int main() { IOS; cin>>n>>m; dp[0][0]=1; f[0]=1; for(i=1;i<M;++i) f[i]=(f[i-1]*i)%MOD; for(i=0;i<M;++i) mi[i]=modinv(f[i],MOD); for(i=0;i<=m;++i) { for(j=0;j<=12;++j) { sum=dp[j][i]; for(k=0;k<=n;k+=2) { if((i+k*(1<<j))<=m) temp=(sum*fac(n,k))%MOD; else break; dp[j+1][i+k*(1<<j)]=(dp[j+1][i+k*(1<<j)]+temp)%MOD; } } } cout<<dp[13][m]<<"\n"; return 0; }
// DeNsE - EcLiPsE // // WHAT is DEAD may NEVER die // #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <map> #include <unordered_map> #include <cmath> #include <iomanip> #include <set> #include <cstring> #include <stack> #include <sstream> #include <queue> #include <unordered_set> #include <cstdlib> using namespace std; #define nitro ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define int long long #define double long double //#define endl " \n" const int inf = (1ll << 62) - 1; const int mod = 1e9 + 7; const int N = 21; const double pi = 3.14159265358979323846; int bPow(int a, int b){ int res = 1; while(b) { if (b & 1) { res = (res * a) % mod; } b >>= 1; a = (a * a) % mod; } return res % mod; } int gcd(int a, int b) { if(a < b) swap(a, b); if (b == 0) return a; return gcd(b, a % b); } vector<int> fact(N, 0ll); void factorial(){ fact[0] = 1, fact[1] = 1; for(int i = 2; i < N; i++){ fact[i] = (fact[i - 1] * i) % mod; } } int ncr(int n, int r){ if(r > n) return 0; int ans = fact[n] % mod; ans *= bPow(fact[r], mod - 2) % mod; ans %= mod; ans *= bPow(fact[n - r], mod - 2) % mod; ans %= mod; return ans; } vector<int> primes(N, -1); void sieve(){ iota(primes.begin(), primes.end(), 0); for (int i = 2; i * i <= N; ++i) { if(primes[i] == i){ for (int j = 2 * i; j < N; j += i) { primes[j] = i; } } } } bool isEven(char c){ int num = c - '0'; if(num & 1) return false; return true; } string divide(string s){ string ans; int cur = 0; for (char i : s) { cur = 10 * cur; cur += (i - '0'); if(cur < 2) { if(!ans.empty()) ans += '0'; continue; } if(cur & 1){ cur /= 2; string ss = to_string(cur); ans += ss; cur = 1; }else{ cur /= 2; string ss = to_string(cur); ans += ss; cur = 0; } } return ans; } void solve() { string s; int k; cin >> s >> k; while (k--){ int n = s.size(); if(n < 3){ s += "200"; continue; } n--; if(s.back() != '0'){ s += "200"; continue; } n--; if(s[n] != '0'){ s += "200"; continue; } n--; if(!isEven(s[n])){ s += "200"; continue; } s.pop_back(); s.pop_back(); s = divide(s); } cout << s << endl; } signed main(){ nitro int tc = 1, test = 1; //cin >> tc; while(tc--){ //cout << "Case #" << test++ << ": "; solve(); } }
#include<bits/stdc++.h> using namespace std; int n,a[102],maxn,ans,w; int main(){ scanf("%d",&n); for(int i=1;i<=n;++i) scanf("%d",&a[i]),maxn=max(maxn,a[i]); for(int i=2;i<=maxn;++i){ int cnt=0; for(int j=1;j<=n;++j) if(__gcd(a[j],i)==i) ++cnt; if(cnt>ans) ans=cnt,w=i; } printf("%d\n",w); }
#include <cstdio> #include <cstring> #include <map> #include <iostream> #include <queue> #include <string> #include <memory.h> #include <set> #include <ctime> #include <stack> #include <cmath> #include <vector> #include <algorithm> #include <limits> #include <climits> #include <list> #include <ctime> #define null NULL using namespace std; clock_t BEGIN, END; typedef pair<int, int> pai; int main(){ int a[1000]; int n; cin >> n; for(int i = 0; i < n; i ++) cin >> a[i]; int m = 0, ans = -1; for(int t = 2; t < 1009; t ++){ int coun = 0; for(int i = 0; i < n; i ++){ if(a[i] % t == 0) coun ++; } if(coun >= m) ans = t, m = coun; } if(m == 0) cout << -1; else cout << ans; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define FORR(i, m, n) for (int i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).end() template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const ll INF=1LL<<60; const int inf=(1<<30)-1; const int mod=1e9+7; int dx[8]={1,0,-1,0,-1,-1,1,1}; int dy[8]={0,1,0,-1,-1,1,-1,1}; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n;cin >> n; string s,t;cin >> s >> t; int cnt=0; REP(i,n) cnt+=s[i]-'0'; REP(i,n) cnt-=t[i]-'0'; if(cnt!=0){ cout << -1 << endl; return 0; } vector<int> a,b; REP(i,n){ if(s[i]=='0') a.push_back(i); if(t[i]=='0') b.push_back(i); } int ans=0; REP(i,a.size()) if(a[i]!=b[i]) ans++; cout << ans << endl; }
#include<iostream> #include<vector> using namespace std; const int N=1e6+7; int n,a[N],b[N],sa,sb; vector<int>ho,aq; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%1d",&a[i]); sa+=a[i]; if(!a[i])ho.push_back(i); } for(int i=1;i<=n;i++){ scanf("%1d",&b[i]); sb+=b[i]; if(!b[i])aq.push_back(i); } if(sa!=sb){ puts("-1"); return 0; } int ans=0; for(int i=0;i<ho.size();i++){ if(ho[i]!=aq[i])ans++; } printf("%d\n",ans); }
#include<bits/stdc++.h> #define ll long long using namespace std; const int N = 55; int n,m,q, box[N], tmp[N]; struct node{ int w, v; }a[N]; int cmp(node n1, node n2){ return n1.v > n2.v;} int main(){ cin >> n >> m >> q; for(int i = 1; i <= n; ++ i) cin >> a[i].w >> a[i].v; sort(a + 1, a + 1 + n, cmp); for(int i = 1; i <= m; ++ i) cin >> box[i]; while(q --){ int l, r, idx = 0; ll ans = 0; cin >> l >> r; //printf("l = %d r = %d\n", l, r); for(int i = 1; i <= m; ++ i){ if(i >= l && i <= r) continue; tmp[++ idx] = box[i]; } //printf("idx = %d\n", idx); sort(tmp + 1, tmp + 1 + idx); for(int i = 1; i <= n; ++ i){ for(int j = 1; j <= idx; ++ j){ if(tmp[j] >= a[i].w){ ans += a[i].v; tmp[j] = 0; break; } } } cout << ans << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; #define pb push_back typedef long double ld; #define fi first #define se second #define PI 3.14159265358979323846264338327950288419716939937510 #define mp make_pair #define vvl vector<vector<ll>> #define vll vector<ll> #define vpll vector<pll> /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> */ ll inf = 1e18; ll p = 998244353 ; ll power(ll x, ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } ll pwr(ll x, ll y) { ll res = 1; x = x ; while (y > 0) { if (y & 1) res = (res * x) ; y = y >> 1; x = (x * x) ; } return res; } ll modInverse(ll n, ll p) { return power(n, p - 2, p); } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); ll i, y, x, z, g, key, h, n, m, t, a, b, c, j, k, d; ll t2, t3, t4, t1, t5, t6; string s, s2; ll l, r; //cin >> t; t = 1; ll x1, y1, x2, y2; while (t--) { cin >> n >> m; char arr[n][m]; ll ti, tj; vector<set<pll>>adj(26); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> arr[i][j]; if (arr[i][j] == 'S') { ti = i; tj = j; } if (arr[i][j] >= 'a' && arr[i][j] <= 'z') { t1 = arr[i][j] - 'a'; adj[t1].insert(mp(i, j)); } } } ll d[n][m], used[n][m]; ll ans = -1; memset(d, 0, sizeof(d)); memset(used, 0, sizeof(used)); queue<pll>q; q.push(mp(ti, tj)); used[ti][tj] = 1; d[ti][tj] = 0; while (!q.empty()) { auto u = q.front(); q.pop(); if (arr[u.fi][u.se] == 'G') { ans = d[u.fi][u.se]; break; } ll dx[4] = {1, -1, 0, 0}; ll dy[4] = {0, 0, 1, -1}; for (i = 0; i < 4; i++) { ll ti = u.fi + dx[i]; ll tj = u.se + dy[i]; if (ti >= 0 && ti < n && tj >= 0 && tj < m) { if (arr[ti][tj] != '#' && used[ti][tj] == 0) { if (used[ti][tj] == 0) { used[ti][tj] = 1; d[ti][tj] = d[u.fi][u.se] + 1; q.push(mp(ti, tj)); } } } } if (arr[u.fi][u.se] >= 'a' && arr[u.fi][u.se] <= 'z') { t1 = arr[u.fi][u.se] - 'a'; for (auto v : adj[t1]) { if (used[v.fi][v.se] == 0) { ti = v.fi; tj = v.se; used[ti][tj] = 1; d[ti][tj] = d[u.fi][u.se] + 1; q.push(mp(ti, tj)); } } adj[t1].clear(); } } cout << ans; } return 0; }
#include <iostream> using namespace std; int main() { long long n,count=0,ans=0; cin>>n; for(long long i=1000;i<=n;i = i*1000) { ans = ans + (n-i); count++; } cout<<ans+count; return 0; }
#pragma GCC optimize("Ofast") #include <iostream> #include <stdio.h> #include <string> #include <vector> #include <algorithm> #include <cstdlib> #include <cmath> #include <iomanip> #include <cctype> #include <sstream> #include <stack> #include <deque> #include <queue> #include <list> #include <set> #include <map> #include <unordered_map> using namespace std; using ll = long long; using P = pair<ll, ll>; using Po = pair<double, double>; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #define REP(i,n) for(ll i=0;i<ll(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=ll(a);i<=ll(b);i++) #define FORD(i,a,b) for(ll i=ll(a);i>=ll(b);i--) #define PB push_back #define MP make_pair #define SZ(x) ll(x.size()) #define ALL(x) x.begin(),x.end() #define NIL -1 #define INF 1ll<<63-1 //#define MOD 1000000007 #define PI 3.14159265358979323846 #define endl "\n" #define EPS 1e-9 #define YES cout << "YES" << endl #define NO cout << "NO" << endl #define Yes cout << "Yes" << endl #define No cout << "No" << endl template <class T, class U> bool chmin(T& t, const U& u) { if (t > u) { t = u; return true; } else return false; } template <class T, class U> bool chmax(T& t, const U& u) { if (t < u) { t = u; return true; } else return false; } bool YN(bool b) { if (b)Yes; else No; return true; } ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } const ll N = 200009; signed main() { ll n; cin >> n; ll ans = 0; if (n >= 1e3) { ans += n - 999; } if (n >= 1e6) { ans += n - 999999; } if (n >= 1e9) { ans += n - 999999999; } if (n >= 1e12) { ans += n - 999999999999; } if (n >= 1e15) { ans += n - 999999999999999; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; int ans = 1001001001; rep(i,n) { int a, b, x; cin >> a >> b >> x; int cnt = x - a; if (cnt > 0) ans = min(ans, b); } if (ans == 1001001001) ans = -1; cout << ans << endl; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <stack> #include <queue> #include <string> #include <map> using namespace std; using ll = long long; int main() { int N; cin >> N; vector<pair<ll, ll>> vec(N); for (int i = 0; i < N; ++i) { ll a, p, x; cin >> a >> p >> x; vec[i] = pair<ll, ll>(p, x - a); } sort(vec.begin(), vec.end()); ll res = -1; for (int i = 0; i < N; ++i) { auto p = vec[i]; // cout << p.first << endl; if (p.second > 0) { res = p.first; break; } } cout << res << endl; }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG using namespace std; typedef long long ll; #define INF 1e9 #define rep(i, n) for(int i = 0; i < n; i++) #define repr(i, n) for(int i = n; i >= 0; i--) #define FOR(i, m, n) for(int i = m; i < n; i++) #define all(x) (x).begin(),(x).end() int main() { int A,B,C; cin >> A >> B >> C; bool ans = false; if (C == 1) { if (A >= B) ans = true; } else { if (A > B) ans = true; } if(ans) cout << "Takahashi" << endl; else cout << "Aoki" << endl; }
#include <bits/stdc++.h> using namespace std; int main(){ int a, b, c; cin >> a >> b >> c; if(a > b) cout <<"Takahashi"; else if(a < b) cout << "Aoki"; else if (a == b && c == 0) cout << "Aoki"; else cout <<"Takahashi"; return 0; }
#include "bits/stdc++.h" using namespace std; double dp[101][101][101]; int main(){ int A,B,C; cin>>A>>B>>C; dp[A][B][C]=1; double ans=0; for(int a=A;a<100;++a){ for(int b=B;b<100;++b){ for(int c=C;c<100;++c){ int s=a+b+c; int d=s+1-(A+B+C); dp[a+1][b][c]+=dp[a][b][c]*(1.0*a/s); if(a+1==100) ans+=d*dp[a+1][b][c]; dp[a][b+1][c]+=dp[a][b][c]*(1.0*b/s); if(b+1==100) ans+=d*dp[a][b+1][c]; dp[a][b][c+1]+=dp[a][b][c]*(1.0*c/s); if(c+1==100) ans+=d*dp[a][b][c+1]; } } } cout<<setprecision(30)<<ans<<endl; }
// Username: WillTheBill // Time of creation: 2020-11-22 13:51 // Original filename: m.cp.cpp #include<bits/stdc++.h> using namespace std; // shortcuts #define pb push_back #define fi first #define se second #define all(_v) _v.begin(),_v.end() #define ll long long // IO #define multitest signed __T; cin >> __T; while(__T--) template<typename T> void _read(T& t); template<typename T> void _read(vector<T>&v); template<typename T1, typename T2> void _read(pair<T1,T2>&p); template<typename T> void _read(T& t){cin >> t;} template<typename T> void _read(vector<T>&v){for(unsigned _i=0;_i<v.size();_i++)_read(v[_i]);} template<typename T1, typename T2> void _read(pair<T1,T2>&p){_read(p.first);_read(p.second);} void _masterread(){} template<typename T,typename... V> void _masterread(T& t, V&... v){_read(t);_masterread(v...);} #define re(...)_masterread(__VA_ARGS__) template<typename T> void _print(T t); template<typename T> void _print(vector<T>&v); template<typename T1, typename T2> void _print(pair<T1,T2>&p); template<typename T> void _print(T t){cout<<t;} template<typename T> void _print(vector<T>&v){for(unsigned _i=0;_i<v.size();_i++)_print(v[_i]),cout<<(_i==v.size()-1?"":" ");} template<typename T1, typename T2> void _print(pair<T1,T2>&p){_print(p.first);cout<<" ";_print(p.second);} void _masterprint(){cout<<endl;} template<typename T,typename... V> void _masterprint(T t, V... v){_print(t);if(sizeof...(v))cout<<" ";_masterprint(v...);} #define pr(...)_masterprint(__VA_ARGS__) // DEBUG // colored output??? template <typename T> // start forward declaration void _debug(T t); template<typename T1,typename T2> void _debug(pair<T1,T2> p); template<typename T> void _debug(vector<T>v); template <typename T> // end forward declaration void _debug(T t){cerr<<t;} template<typename T1,typename T2> void _debug(pair<T1,T2> p){cerr<<"{";_debug(p.first);cerr<<", ";_debug(p.second);cerr<<"}";} template<typename T> void _debug(vector<T>v){cerr<<"(";for(unsigned _i=0;_i<v.size();_i++)_debug(v[_i]),cerr<<(_i==v.size()-1?"":", ");cerr << ")";} void _masterdebug(){cerr<<"]\n";} template<typename T,typename... V> void _masterdebug(T t,V... v){_debug(t);if(sizeof...(v))cerr<<", ";_masterdebug(v...);} #ifdef __local__ #define debug(...) cerr<<"["<<#__VA_ARGS__<<"] = [";_masterdebug(__VA_ARGS__) #else #define debug(...) #endif // TYPES // #define int long long signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); vector<vector<vector<double>>> dp (101, vector<vector<double>> (101, vector<double> (101, 0.0))); int A,B,C; re(A,B,C); dp[A][B][C] = 1.0; double sum = 0; double propsum = 0; for(int i = 0; i <= 100; i++){ for(int j = 0; j <= 100; j++){ for(int k = 0; k <= 100; k++){ if(i == A && j == B && k == C) continue; int s = i + j + k; if(s <= 1) continue; if((i == 100 && j == 100) || (i == 100 && k == 100) || (j == 100 && k == 100)) continue; if(i > 0 && j != 100 && k != 100) dp[i][j][k] += dp[i-1][j][k] * (i-1) / ((double)(s-1)); if(j > 0 && i != 100 && k != 100) dp[i][j][k] += dp[i][j-1][k] * (j-1) / ((double)(s-1)); if(k > 0 && i != 100 && j != 100) dp[i][j][k] += dp[i][j][k-1] * (k-1) / ((double)(s-1)); if(dp[i][j][k] < 0){ debug(i,j,k); } if(i == 100 || j == 100 || k == 100){ int amount = i - A + j - B + k - C; if(amount < 0) continue; debug(dp[i][j][k], i, j, k, amount); sum += amount * dp[i][j][k]; propsum += dp[i][j][k]; } } } } debug(dp[99][99][99]); debug(sum, propsum); cout << setprecision(10) << fixed; pr(sum / propsum); }
#include <iostream> #include <bits/stdc++.h> #define ll long long int using namespace std; int main() { ll n; cin>>n; for(ll i =3,a=1; i<n; i*=3,a++){ for(ll j=5,b=1; j<n; j*=5,b++){ if(i+j==n){ cout<<a<<" "<<b; return 0; } } } cout<<"-1"<<endl; return 0; }
/* winners never quit and quitters never win. #swap */ #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; #pragma GCC optimize ("-O3") using ll = long long; using ld = long double; const ll mod = 1000000007; const ll inf = 1000000000000000000; const ll rk = 256; const ld PI = 3.141592653589793; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; template <typename T> void make_unique(vector<T>& vec) { sort(all(vec)); vec.erase(unique(all(vec)), vec.end()); } #define endl '\n' #define pb push_back #define mp make_pair #define vc vector #define fs first #define sec second #define pq priority_queue #define lb lower_bound #define ub upper_bound #define pll pair<ll,ll> #define pls pair<ll,string> #define psl pair<string,ll> #define plc pair<ll,char> #define pcl pair<char,ll> #define pss pair<string,string> #define all(x) (x).begin(), (x).end() #define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower); #define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper); #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n"; #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define T ll t=0;cin>>t;for(ll test=0;test<t;test++) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll nxt() { ll TemporaryVariable; cin >> TemporaryVariable; return TemporaryVariable; } struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); //isprime(); //coeff(); /*freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);*/ ll n=nxt(); ll cnt=1; for(ll a=3;a<n;a*=3) { ll b=n-a; ll count=0; while(b%5==0) { b/=5; count++; } if(b==1 and cnt>0 and count>0) { cout<<cnt<<" "<<count<<endl; exit(0); } cnt++; } cout<<-1<<endl; printclock; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ int N = 0; cin >> N; string s[60] = {}; long long ans = 1; for(int i = 0 ; i < N ; i ++) cin >> s[i]; for(int i = 0 ; i < N ; i ++) if(s[i] == "OR") ans += 1ll << (i + 1); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int32_t main(){ ios::sync_with_stdio(0),cin.tie(0); int n; cin >> n; int x=1,y=1; for(int i=0;i<n;i++){ string s; cin >> s; int nx,ny; if(s=="AND"){ nx=x,ny=x+2*y; } else { nx=2*x+y,ny=y; } x=nx,y=ny; } cout << x; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define ff first #define ss second #define all(x) (x).begin(), (x).end() #define len(x) int((x).size()) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define randint(n) uniform_int_distribution<int>(1, (n))(rng) const ll p = 1000000007LL; // const ll p = 998244353LL; // that other one ll mod_exp(ll a, ll b) { if(b==0LL) return 1LL; ll ans = mod_exp(a,ll(b/2LL)); ans*=ans; ans%=p; if(b%2LL==1LL) ans*=a; ans%=p; return ans; } int dp[2][1001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef ARTHUR_LOCAL ifstream cin("input.txt"); #endif int n; cin>>n; string caa,cab,cba,cbb; cin>>caa>>cab>>cba>>cbb; char aa=caa[0]; char ab=cab[0]; char ba=cba[0]; char bb=cbb[0]; if((ab=='A'&&aa=='A')||n==2){ cout << 1 << endl; return 0; } if(ab=='B'&&bb=='B'){ cout << 1 << endl; return 0; } if((ab=='A'&&aa=='B'&&ba=='B')||(ab=='B'&&bb=='A'&&ba=='A')){ cout << mod_exp(2,n-3) << endl; return 0; } dp[0][0]=1; for(int i=1; i<=n-3; i++){ dp[0][i] = dp[0][i-1] + dp[1][i-1]; dp[0][i] %= p; dp[1][i] = dp[0][i-1]; } cout << (dp[0][n-3]+dp[1][n-3])%p << endl; }
#include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_poizcy.hpp> typedef long long ll; typedef long llu; #define pb push_back #define mp make_pair #define all(a) (a).begin(), (a).end() #define mem(a,h) memset(a, (h), sizeof(a)) #define por(a,b) (((a%MOD) * (b%MOD))%MOD) #define forg(i, b, e, c) for (ll i = (ll)b; i < (ll)e; i+=c) #define forr(i, b, e) for (ll i = b; i < e; i++) using namespace std; //using namespace __gnu_pbds; typedef double lldb; typedef pair<ll, ll> ii; typedef pair<ii, ll> iii; typedef vector<ll> vi; typedef pair<ii, ll> iii; typedef pair<double, double> iidb; //typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long INF = 1e18; const double PI = acos(-1); #define initseg ll new_nodo=(pnodo*2),mid=(iz+der)/2; #define fi first #define se second const ll tam =5010000; const ll MOD=1e9+7; const ll cmplog=29; int n; char arr[tam]; int dp2[tam],dp3[tam]; int f2(int u) { if (u==0)return 1; if (u<=1)return 0; if (dp2[u]!=-1) return dp2[u]; int tot=0; tot+=f2(u-1); tot+=f2(u-2); tot%=MOD; return dp2[u]=tot; } int f3(int u) { if (u==2)return 1; if (u<=2)return 0; if (dp3[u]!=-1) return dp3[u]; int tot=0; forr(i,0,u) { tot+=f3(u-1-i); tot%=MOD; } return dp3[u]=tot; } int main() { memset(dp2,-1,sizeof dp2); memset(dp3,-1,sizeof dp3); ios::sync_with_stdio(false);cin.tie(0); cin>>n; forr(i,0,4) cin>>arr[i]; if (arr[1]=='A') { if (arr[0]=='A') cout<<1<<endl; else { if (arr[2]=='A') cout<<f2(n)<<endl; else cout<<f3(n)<<endl; } } else { if (arr[3]=='B') cout<<1<<endl; else { if (arr[2]=='B') cout<<f2(n); else cout<<f3(n)<<endl; } } }
#include<bits/stdc++.h> #include<iostream> #define mi map<int,int> #define umi unordered_map<int,int> #define pii pair<int,int> #define si set<int> #define st stack<int> #define di deque<int> #define qi queue<int> #define pq priority_queue<int> #define dl deque<long long int> #define usi unordered_set<int> #define vi vector<int> #define vl vector<long long int> #define ll long long int #define pb push_back #define mk make_pair #define all(v) v.begin(),v.end() #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define M1 1000000007 #define M2 998244353 #define I1 1000000001 #define I2 1000000000000000001 #define asort(v) sort(v.begin(),v.end()) #define dsort(v) sort(v.begin(),v.end(),greater<int>()) using namespace std; ll gcd(ll a,ll b){if(b==0) return a;else return gcd(b,a%b);} bool isprime(ll a){for(int i=2;i<=sqrt(a);i++){if(a%i==0) return false;} return true;} ll lcm(ll a,ll b){return (a*b)/gcd(a,b);} // the number of zeros at the beginning of the bit representation // int prefixzero(int x) {return __builtin_clz(x);} // the number of zeros at the end of the bit representation // int suffixzero(int x) {return __builtin_ctz(x);} // the number of ones in the bit representation // int numberone(int x) {return __builtin_popcount(x);} // push the value of array element // void read(vector<int> &v,int n) {for(int i=0;i<n;i++) cin>>v[i];} void read_1(vector<int> &v,int n) {for(int i=1;i<=n;i++) cin>>v[i];} void read_l(vector<ll> &v,ll n) {for(ll i=0;i<n;i++) cin>>v[i];} // to check the given number prime or not // vector<bool>prime(1e6+1,true); void sprime() { prime[0]=false; prime[1]=false; for(ll i=2;i<=sqrt(1e6);i++) { if(prime[i]) { for(ll j=i*i;j<=1e6;j+=i) prime[j]=false; } } } // return the value of x and y in given equation of ax+by=gcd(a,b) // tuple<int,int,int> GCD(int a,int b) { if(b==0) return {1,0,a}; else { int x,y,g; tie(x,y,g)=GCD(b,a%b); return {y,x-(a/b)*y,g}; } } // to find the power of given number in logn time // ll pw(ll base,ll power) { ll res=1; while(power!=0) { if(power&1) {power-=1;res=(res*base);} else {power/=2;base=(base*base);} } return res; } // dfs traversal for graph and find the minimum distance from root node // /*void dfs(int node,int d) { visit[node]=1; dist[node]=d; for(int child:ar[node]) { if(visit[child]==0) dfs(child,dist[node]+1); } }*/ // bfs traversal for graph and find the minimum distance from root node // /*void bfs(int node) { queue<int>q; q.push(node); visit[node]=1; dist[node]=0; while(!q.empty()) { int curr=q.front(); q.pop(); for(int child:ar[curr]) { if(visit[child]==0) { q.push(child); dist[child]=dist[curr]+1; visit[child]=1; } } } }*/ //---------------------------------START THE CODE------------------------------// void solve() { int n;cin>>n; vi v(n); read(v,n); vl prime={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; ll ans=1e18; for(ll i=0;i<(1<<prime.size());i++) { ll num=1; for(int j=0;j<prime.size();j++) { if(i>>j & 1) num*=prime[j]; } bool p=true; for(int i=0;i<n;i++) { if(gcd(v[i],num)==1) { p=false; break; } } if(p) { ans=min(ans,num); } } cout<<ans<<"\n"; return; } int main() { IOS; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ll t=1;//cin>>t; while(t--) {solve();} return 0; } /*-----------------END OF CODE------------------*/
#include <bits/stdc++.h> using namespace std ; #define int int64_t #define endl "\n" #define f(i,a,b) for(int i=int(a);i<int(b);++i) #define pr pair #define ar array #define fr first #define sc second #define vt vector #define pb push_back #define LB lower_bound #define UB upper_bound #define PQ priority_queue #define sz(x) ((int)(x).size()) #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define mem0(a) memset(a, 0, sizeof(a)) #define mem1(a) memset(a, -1, sizeof(a)) template<class A> void rd(vt<A>& v); template<class T> void rd(T& x){ cin >> x; } template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;} template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;} template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template<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<<"}\n"; } template<typename T> void __p(std::vector<T> a) { cout<<"{"; for(auto it=a.begin(); it<a.end(); it++) __p(*it),cout<<",}\n"[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...); } void setIO(string s = "") { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(cin.failbit); cout.precision(15); cout << fixed; if(sz(s)){ freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } } const vt<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; const int N = sz(primes); signed main(){ setIO() ; int n; rd(n); vt<int> a(n); rd(a); int ans = 2e18; for(int mask = 1; mask < (1<<N); mask++){ int cur = 1 ; f(i,0,N) if(mask & (int(1) << i)) cur *= primes[i] ; bool ok = true ; f(i,0,n) ok &= __gcd(cur,a[i]) > 1 ; if(ok){ ckmin(ans,cur); } } cout << ans; }
#include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); typedef long long ll; typedef long double ld; #define pb push_back #define mp make_pair #define ff first #define ss second #define mod 1000000007 #define pii pair<ll,ll> #define inf 1000000000000000000 #define bpc(x) __builtin_popcountll(x) #define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++) #define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++) #define rep(n) for(ll i = 0; i < n; i++) #define repi(i,n) for(ll i = 0; i < n; i++) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); int main() { FAST/**/ ll n,m; cin>>n>>m; ll lim = (1ll<<m); string arr[n]; rep(n) cin>>arr[i]; ll num[n]; rep(n) { num[i] = 0; for(auto x : arr[i]) num[i] = 2*num[i] + (x == '1'); } map<ll,ll> ma; rep(n) ma[bpc(num[i])%2]++; ll ans = ma[0]*ma[1]; cout<<ans<<'\n'; return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<n;i++) #define repl(i,l,r) for(ll i=(l);i<(r);i++) #define per(i,n) for(ll i=(n)-1;i>=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x,vector<x>,greater<x>> #define all(x) (x).begin(),(x).end() #define CST(x) cout<<fixed<<setprecision(x) #define rev(x) reverse(x); using ll=long long; using vl=vector<ll>; using vvl=vector<vector<ll>>; using pl=pair<ll,ll>; using vpl=vector<pl>; using vvpl=vector<vpl>; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={-1,0,1,0,1,1,-1,-1}; const ll dx[8]={0,-1,0,1,1,-1,1,-1}; template <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } int main(){ ll n,k;cin >> n >>k; vvl dp(4,vl(3*n));dp[0][0]=1; rep(i,3){ rep(j,3*n){ if(dp[i][j]){ dp[i+1][j]+=dp[i][j];dp[i+1][j+n]-=dp[i][j]; } } rep(j,3*n-1)dp[i+1][j+1]+=dp[i+1][j]; } // rep(j,3*n)cout << dp[3][j] <<" ";cout << endl; ll cost=0,sum=-1; rep(i,3*n){ cost+=dp[3][i]; if(cost>=k){ k-=cost-dp[3][i]; sum=i;break; } } cost=0; rep(i,n){ ll rem=sum-i; ll left=rem-(n-1),right=rem; left=max(left,0LL),right=min(right,n-1); ll p=max(right-left+1,0LL); //cout << (right-left+1) <<endl; if(k<=cost+p){ cout << i+1 <<" "; ll cnt=0; rep(j,n){ if(rem-j<=n-1){ cnt++; if(k==cost+cnt){ cout << j+1 <<" " << rem-j+1 <<endl; return 0; } } } } cost+=p; } //cout << sum <<" " << k <<endl; }
typedef long long ll; typedef long double ld; #include <bits/stdc++.h> using namespace std; int main() { ll n,k; std::cin >> n>>k; vector<vector<ll>> t(n,vector<ll>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::cin >> t[i][j]; } } ll ans = 0; vector<bool> used(n,false); function<void(ll,ll,ll,ll)> rec = [&] (ll fir, ll sum, ll now, ll cnt) { if(cnt==n){ if(sum+t[now][fir]==k) ans++; // std::cout << sum+t[now][fir] << std::endl; return; } for (int i = 0; i < n; i++) { if(used[i])continue; used[i] = true; rec(fir, sum + t[now][i], i, cnt+1); used[i] = false; } }; for (int i = 0; i < n; i++) { used[i] = true; rec(i, 0,i,1); used[i] = false; } std::cout << ans/n << std::endl; }
#include<bits/stdc++.h> #define R register #define ll long long using namespace std; const int MAXN =25; vector <int> edge[MAXN]; int tot,dfn[MAXN],col[MAXN],pos[MAXN]; int n,m; ll ans; bool vis[MAXN]; void make_list(int k) { if(vis[k]) return ; vis[k]=1; dfn[++tot]=k; pos[k]=tot; for(R int i=0;i<edge[k].size();++i) make_list(edge[k][i]); } void dfs(int now,ll sum) { if(now>tot) { ans+=sum; return ; } int f[4]; memset(f,0,sizeof f); bool flag=0; for(R int i=0;i<edge[dfn[now]].size();++i) { int to=edge[dfn[now]][i]; if(pos[to]<now) f[col[to]]=1; else flag=1; } int k=0; for(R int i=1;i<=3;++i) k+=(f[i]==0); if(!flag) dfs(now+1,sum*k); else { for(R int i=1;i<=3;++i) { if(f[i]) continue; col[dfn[now]]=i; dfs(now+1,sum); // col[now]=0; } } } int main() { scanf("%d %d",&n,&m); for(R int i=1;i<=m;++i) { int u,v; scanf("%d %d",&u,&v); edge[u].push_back(v); edge[v].push_back(u); } ll res=1; for(R int i=1;i<=n;++i) { if(vis[i]) continue; tot=0; ans=0; make_list(i); dfs(1,1); res*=ans; } printf("%lld",res); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; #define rep(i,a,b) for(ll i=a;i<b;i++) #define nl cout<<endl #define pii pair<ll,ll> #define vi vector<ll> #define vii vector<pii> #define mi map<ll,ll> #define all(a) (a).begin(),(a).end() #define pb push_back #define ff first #define ss second #define hell 1000000007 #define test4(x,y,z,a) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<" a is "<<a<<endl; #define test3(x,y,z) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<endl; #define test2(x,y) cout<<"x is "<<x<<" y is "<<y<<endl; #define test1(x) cout<<"x is "<<x<<endl; #define N 200009 ll power(ll a,ll b,ll m) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans; } string s,x; ll n; ll dp[N][7]; ll go(ll ind,ll mod) { //test2(ind,mod); if(ind == n) { if(mod % 7 == 0) return 1; return 0; } if(dp[ind][mod] != -1) return dp[ind][mod]; bool ans; if(x[ind] == 'T') { ll dig = s[ind] - '0'; bool p1 = go(ind+1, (mod*10)%7); bool p2 = go(ind+1, (mod*10 + dig)%7); if(p1 or p2) ans = 1; else ans = 0; } else { ll dig = s[ind] - '0'; bool p1 = go(ind+1, (mod*10)%7); bool p2 = go(ind+1, (mod*10 + dig)%7); if(!p1 or !p2) ans = 0; else ans = 1; } //test3(ind,mod,ans); return dp[ind][mod] = ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n; cin>>s>>x; memset(dp,-1,sizeof(dp)); ll ans = go(0,0); if(ans) cout << "Takahashi" << endl; else cout << "Aoki " << endl; }
#include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define rep(i,n) for(int i=0;i<(int)(n);i++) #define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++) #define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++) #define codefor int test;scanf("%d",&test);while(test--) #define INT(...) int __VA_ARGS__;in(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;in(__VA_ARGS__) #define yes(ans) if(ans)printf("yes\n");else printf("no\n") #define Yes(ans) if(ans)printf("Yes\n");else printf("No\n") #define YES(ans) if(ans)printf("YES\n");else printf("NO\n") #define vector1d(type,name,...) vector<type>name(__VA_ARGS__) #define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__)) #define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__))) #define umap unordered_map #define uset unordered_set using namespace std; using ll = long long; const int MOD=1000000007; const int MOD2=998244353; const int INF=1<<30; const ll INF2=(ll)1<<60; //入力系 void scan(int& a){scanf("%d",&a);} void scan(long long& a){scanf("%lld",&a);} template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);} template<class T> void scan(T& a){cin>>a;} template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);} void in(){} template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);} //出力系 void print(const int& a){printf("%d",a);} void print(const long long& a){printf("%lld",a);} void print(const double& a){printf("%.15lf",a);} template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);} template<class T> void print(const T& a){cout<<a;} template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}} void out(){putchar('\n');} template<class T> void out(const T& t){print(t);putchar('\n');} template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);} //デバッグ系 template<class T> void dprint(const T& a){cerr<<a;} template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}} void debug(){cerr<<endl;} template<class T> void debug(const T& t){dprint(t);cerr<<endl;} template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);} ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; } ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; } ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;} template<class T> void chmax(T &a,const T b){if(b>a)a=b;} template<class T> void chmin(T &a,const T b){if(b<a)a=b;} vector<int> digit; int n; vector2d(int,dp,100001,7,-1); string s,t; int rec(int i,int sum){ if(i==n)return (sum%7==0); if(dp[i][sum]!=-1)return dp[i][sum]; int ret=0; if(t[i]=='A'){ //0のほうを選ぶ if(!rec(i+1,(sum+digit[i]*(s[i]-'0'))%7))ret=0; else ret=rec(i+1,sum); }else{ //1のほうを選ぶ if(rec(i+1,(sum+digit[i]*(s[i]-'0'))%7))ret=1; else ret=rec(i+1,sum); } return dp[i][sum]=ret; } int main(){ in(n); cin>>s>>t; digit.resize(n); ll d=1; rep(i,n){ digit[i]=d; (d*=10)%=7; } reverse(all(digit)); out(rec(0,0)==1?"Takahashi":"Aoki"); }
// // a.cpp // // // Created by Sagar Singh on 13/12/20. // #include <iostream> #include <fstream> #include <vector> #include <set> #include <map> #include <cstring> #include <string> #include <cmath> #include <cassert> #include <ctime> #include <algorithm> #include <numeric> #include <sstream> #include <list> #include <queue> #include <deque> #include <stack> #include <cstdlib> #include <cstdio> #include <iterator> #include <functional> #include <bitset> #include <unordered_map> #include <unordered_set> typedef unsigned long long ul; typedef long double ld; typedef long long ll; using namespace std; const int nax = 1.5e3+10; int a[nax][nax],l[nax][nax],r[nax][nax],u[nax][nax],d[nax][nax]; int n,m,p,q,ans; int main() { cin >> n >> m >> p >> q; for(int i=0,x,y;i<p;++i){ cin >> x >> y; a[x][y] = +1; } for(int i=0,x,y;i<q;++i){ cin >> x >> y; a[x][y] = -1; } for(int i=1;i<=n;++i){ int c = 0; for(int j=1;j<=m;++j){ if( a[i][j] == -1 ) c = 0; if( a[i][j] == +1 ) c = 1; l[i][j] = c; } } for(int i=1;i<=n;++i){ int c = 0; for(int j=m;j>=1;--j){ if( a[i][j] == -1 ) c = 0; if( a[i][j] == +1 ) c = 1; r[i][j] = c; } } for(int j=1;j<=m;++j){ int c = 0; for(int i=1;i<=n;++i){ if( a[i][j] == -1 ) c = 0; if( a[i][j] == +1 ) c = 1; u[i][j] = c; } } for(int j=1;j<=m;++j){ int c = 0; for(int i=n;i>=1;--i){ if( a[i][j] == -1 ) c = 0; if( a[i][j] == +1 ) c = 1; d[i][j] = c; } } for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ ans += (l[i][j]|r[i][j]|u[i][j]|d[i][j]); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, Q; string S; cin >> N >> S >> Q; int t[Q], a[Q], b[Q]; for (int i = 0; i < Q; i++) cin >> t[i] >> a[i] >> b[i]; char temp; bool f = false; for (int i = 0; i < Q; i++) { if (t[i] == 1) { if (f) { if (a[i] <= N) a[i] += N; else a[i] -= N; if (b[i] <= N) b[i] += N; else b[i] -= N; } temp = S[a[i] - 1]; S[a[i] - 1] = S[b[i] - 1]; S[b[i] - 1] = temp; } else { if (f) f = false; else f = true; } } if (f) { for (int i = 0; i < N; i++) { temp = S[i]; S[i] = S[i + N]; S[i + N] = temp; } } cout << S << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template<class T> using vc = vector<T>; template<class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i,n) for (auto& i: n) template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;} template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;} struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_; #ifdef DEBUG template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; } template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; } template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; } template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; } template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); } #endif const ll INF = 4e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// int main() { ll N; cin >> N; vvc<ll> G(N); vc<pair<ll, ll>> E(N - 1); rep(i, N - 1){ ll a, b; cin >> a >> b, a--, b--; G[a].eb(b), G[b].eb(a); E[i] = {a, b}; } vl A(2 * N); vl used(N); ll r = 0; vl in(N), out(N); auto dfs = [&](auto&& dfs, ll now) ->void{ A[r] = now; in[now] = out[now] = r++; used[now] = true; repa(to, G[now]){ if (used[to]) continue; used[to] = true; dfs(dfs, to); A[r] = now; out[now] = r++; } }; dfs(dfs, 0); // verr(A, A.size()); vl S(2 * N); ll Q; cin >> Q; rep(_, Q){ ll t, e, x; cin >> t >> e >> x; e--; auto [a, b] = E[e]; if(t == 1){ if(in[a] < in[b]){ // aが根側 S[0] += x, S[in[b]] -= x; S[out[b] + 1] += x, S[2 * N - 1] -= x; }else{ S[in[a]] += x, S[out[a] + 1] -= x; } }else{ if(in[b] < in[a]){ // bが根側 S[0] += x, S[in[a]] -= x; S[out[a] + 1] += x, S[2 * N - 1] -= x; }else{ S[in[b]] += x, S[out[b] + 1] -= x; } } } // rep(i, N) err(i, in[i], out[i]); rep(i, 2 * N - 1) S[i + 1] += S[i]; // verr(S, 2 * N); vl ans(N); rep(i, 2 * N - 1) ans[A[i]] = S[i]; rep(i, N) cout << ans[i] << '\n'; }
//YashS #include <bits/stdc++.h> using namespace std; #define test int T;cin>>T;while(T--) #define int long long #define pb push_back #define all(x) (x).begin(),(x).end() #define sz(x) (int)((x).size()) #define fr first #define sc second #define minf LONG_LONG_MIN #define inf LONG_LONG_MAX #define pii pair<int,int> #define rep(i,a,b) for(int i=a;i<b;i++) #define mem1(x, arr) memset((arr),x,sizeof((arr)) #define pVec(v) for(auto e:v)cout<<e<<" ";cout<<"\n" const int MOD = 1e9 + 7; const int N = 1e5 + 7; void init_code() { #ifndef ONLINE_JUDGE freopen("inputf.txt", "r", stdin); freopen("outputf.txt", "w", stdout); #endif // ONLINE_JUDGE } void yash() { int n; cin >> n; vector<int> a(n, 0); int sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } vector<bool> f(sum + 1, 0); f[0] = 1; for (int i = 0; i < n; i++) { for (int j = sum; j > 0; j--) { if (j - a[i] >= 0 && f[j - a[i]]) f[j] = 1; } } int ans = sum; for (int i = 1; i < sum; i++) { if (f[i]) { int ma = (sum - i) > i ? (sum - i) : i; if (ma < ans) ans = ma; } } cout << ans << endl; } signed main() { init_code(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //test yash(); return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define int long long const int inf = 1e12; #define D long double #define NeedForSpeed ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int MOD = 1000000007; #define fi first #define se second #define endl '\n' // random_device rd; // mt19937 rng(rd()); // int getRand(int l,int r){ // get random number in the range [l,r] // uniform_int_distribution<int> uid(l,r); // return uid(rng); // } const int N = 2e5 + 10; int n , a[N] ; vector<pair<int,int>> graph[N]; int subtree[N]={0}; int bits[100][2] = {0}; int power[100]; void dfs(int u , int p = 0){ for(auto v : graph[u]){ if(v.fi==p)continue; subtree[v.fi] = subtree[u]^v.se; dfs(v.fi,u); } } void go(){ cin >> n ; for(int i = 0 ; i < n-1 ; i++){ int u , v , w ; cin >> u >> v >> w ; graph[u].push_back({v,w}); graph[v].push_back({u,w}); } subtree[1] = 0; subtree[0] = 0; dfs(1); for(int i = 1 ; i <= n ; i++){ for(int j = 0 ; j < 60 ; j++){ bits[j][(subtree[i]>>j)&1]++; } } int ans = 0; for(int i = 0 ; i < 60 ; i++){ int l = bits[i][0] , r = bits[i][1]; int cur = l*r; cur %= MOD ; cur += MOD ; cur %= MOD; cur *= power[i]; cur %= MOD ; cur += MOD ; cur %= MOD; ans += cur; ans %= MOD; ans += MOD; ans %= MOD; } cout << ans << endl; } signed main() { // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); NeedForSpeed int t = 1; cout << fixed << setprecision(12); power[0] = 1; for(int i = 1 ; i < 62 ; i++)power[i] = power[i-1]*2; for(int i = 0 ; i < 62 ; i++){ power[i] %= MOD ; power[i] += MOD ; power[i] %= MOD ; } // cin >> t; while(t--){ // cout << "Case #" << cases << ": "; go(); } }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x){ x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();} while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();} x *= f; } inline void write(int x){if (x > 9) write(x/10); putchar(x%10+'0'); } int n,ans[11]; const int P = 1e9+ 7; char caa,cab,cba,cbb; unordered_map<string,int>mp; inline void dfs(string s){ if (mp.count(s)) return; ++ans[s.size()]; mp[s] = 1; if (s.size() ==10){ return; } for (int i = 0; i < s.size()-1; ++i){ bool s1 = s[i] == 'a' ? 1 : 0,s2 = s[i+1] == 'a' ? 1 : 0; if (s1&&s2) dfs(s.substr(0,i+1) + caa + s.substr(i+1,s.size()-i-1)); else if (s1&&!s2) dfs(s.substr(0,i+1) + cab + s.substr(i+1,s.size()-i-1)); else if (!s1&&!s2) dfs(s.substr(0,i+1) + cbb + s.substr(i+1,s.size()-i-1)); else if (!s1&&s2) dfs(s.substr(0,i+1) + cba + s.substr(i+1,s.size()-i-1)); } } inline int all1(){ for (int i = 2; i <= 10; ++i) if (ans[i] ^ 1) return 0; return 1; } inline int pw2(){ for (int i = 2; i <= 3; ++i) if (ans[i] ^ 1) return 0; for (int i = 4,now = 2; i <= 10; ++i,now *= 2) if (ans[i] ^ now) return 0; return 1; } int fib[1005]; int main(){ // caa = 'b'; // cab = 'b'; // cba = 'a'; // cbb = 'b'; fib[2] = 1,fib[3] = 1; for (int i = 4; i <= 1000; ++i) fib[i] = (fib[i-1] + fib[i-2]) %P; cin>>n>>caa>>cab>>cba>>cbb; if (n<=3)cout<<1<<'\n',exit(0); caa -= 'A'-'a'; cab -= 'A'-'a'; cba -= 'A'-'a'; cbb -= 'A'-'a'; dfs("ab"); // for (int i = 2; i <= 10; ++i) cout<<i<<' '<<ans[i]<<'\n'; if (all1()) cout<<1<<'\n',exit(0); if (pw2()){ int ans= 1; for (int i = 1; i <= n-3; ++i) ans=ans*2%P; cout<<ans<<'\n';exit(0); } cout<<fib[n]<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; template<class t> inline t read(t &x){ char c=getchar();bool f=0;x=0; while(!isdigit(c)) f|=c=='-',c=getchar(); while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar(); if(f) x=-x;return x; } template<class t,class ...A> inline void read(t &x,A &...a){ read(x);read(a...); } template<class t> inline void write(t x){ if(x<0) putchar('-'),write(-x); else{if(x>9) write(x/10);putchar('0'+x%10);} } const int N=5e5+5; int n,a[N],b[N]; long long ans; signed main(){ read(n); for(int i=1,x;i<=n;i++){ scanf("%1d",&x); if(x) a[++*a]=i; } for(int i=1,x;i<=n;i++){ scanf("%1d",&x); if(x) b[++*b]=i; } if(*a<*b||*a-*b&1) return write(-1),0; int i=1; for(int j=1;j<=*b;i++,j++){ while(i<=*a&&a[i]<b[j]) ans+=a[i+1]-a[i],i+=2; if(i>*a) return write(-1),0; ans+=a[i]-b[j]; } while(i<=*a) ans+=a[i+1]-a[i],i+=2; write(ans); }
#include <bits/stdc++.h> using namespace std; const int maxn=500005; char s[maxn]; int pos1[maxn],n1,pos2[maxn],n2; int main() { int i,j; long long ans=0; scanf("%*d%s",s); for(i=0,n1=0;s[i];i++) if(s[i]=='1') pos1[n1++]=i; scanf("%s",s); for(i=0,n2=0;s[i];i++) if(s[i]=='1') pos2[n2++]=i; if(((n1^n2)&1) || n1<n2) { printf("-1\n"); return 0; } for(i=0,j=0;i<n1;) if(j>=n2 || pos1[i]<pos2[j]) ans+=pos1[i+1]-pos1[i],i+=2; else ans+=pos1[i]-pos2[j],i++,j++; printf("%lld\n",(j>=n2)?ans:-1ll); return 0; }
#include <bits/stdc++.h> using namespace std; #define clr(a) memset(a, 0, sizeof(a)) #define full(a) memset(a, 0x3f, sizeof(a)) #define mset(a, b) memset(a, b, sizeof(a)) #define cpy(a,b) memcpy(a, b, sizeof(a)) #define fornext(x, i) for(signed i = hd[x], y = ver[i]; i; i = nxt[i], y = ver[i]) #define Rep(i, a, b) for(signed i = (signed)(a); i <= (signed)(b); ++i) #define dRep(i, a, b) for(signed i = (signed)(a); i >= (signed)(b); --i) #define IOset(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout); #define pb push_back #define It iterator #define endl '\n' #define un unsigned #define int ll #define ll long long #define db double #define rept cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl; #define dbg cout<<"c " #define dbug(x) cerr << #x " = " << (x) << endl template<typename _T> inline void _prt(_T _d) { cerr << _d << ' '; } template <typename _T, typename ...Args> inline void _prt(_T _d, Args ..._a) { _prt(_d), _prt(_a...); } template <typename _T, typename ...Args> inline void prt(_T _d, Args ..._a) { _prt(_d), _prt(_a...), cerr<<endl; } template <typename _T, typename ...Args> inline void prt(_T _d) { cerr << _d <<endl; } template<typename _T> inline void rd(_T &_d) { signed _f; char _c; _f = 1,_d = 0; while (_c = getchar(), !isdigit(_c)) if(_c=='-') _f=-1; while (isdigit(_c)) _d = _d * 10 + _c - '0', _c = getchar(); _d=_f*_d; } template <typename _T, typename ...Args> inline void rd(_T &_d, Args &..._a) { rd(_d); rd(_a...); } const int N=2005; int fa[N],ex[N][2],n,m,vis[N]; char s[N]; int find(int x) { if (fa[x] == x) return x; return fa[x] = find(fa[x]); } void uni(int x,int y) { int p=find(x),q=find(y); if(p==q) return; fa[p]=q,ex[q][0]|=ex[p][0],ex[q][1]|=ex[p][1]; } signed main() { rd(n,m); Rep(i,1,n+m) fa[i]=i; Rep(i,1,n) ex[i][0]=1; Rep(i,n+1,n+m) ex[i][1]=1; Rep(i,1,n) { scanf("%s",s+1); Rep(j,1,m) if(s[j]=='#') uni(i,j+n); } uni(1,n+1),uni(n,n+1),uni(1,n+m),uni(n,n+m); int rec=0,ans=0; Rep(i,1,n+m) find(i); Rep(i,1,n+m) if(!vis[fa[i]]&&fa[i]!=fa[1]&&ex[fa[i]][0]) ++ans,vis[fa[i]]=1; clr(vis); Rep(i,1,n+m) if(!vis[fa[i]]&&fa[i]!=fa[1]&&ex[fa[i]][1]) ++rec,vis[fa[i]]=1; cout<<min(ans,rec)<<endl; return 0; }
#pragma GCC optimize(2) #include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define MOD 1000000007 #define mod 998244353 #define INF63 1061109567 #define INF127 9187201950435737471 #define UINF 18446744073709551615 #define F first #define S second #define ll long long #define N 1010 using namespace std; char buf[N]; string s[N]; ll n,m,ans=0; bool vis[N*N]; bool mark_r[N],mark_c[N]; vector<ll> vt[N*N]; void dfs(ll x) { vis[x]=true; ll i; for(i=0;i<vt[x].size();i++) { if(!vis[vt[x][i]]) { dfs(vt[x][i]); } } return; } int main(){ ll i,j,k,m1,m2; scanf("%lld%lld",&n,&m); for(i=0;i<n;i++) { scanf("%s",&buf); s[i]=buf; } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(s[i][j]=='#') { mark_r[i]=true; mark_c[j]=true; for(k=j+1;k<m;k++) { if(s[i][k]=='#') { vt[i*m+j].push_back(i*m+k); vt[i*m+k].push_back(i*m+j); break; } } for(k=i+1;k<n;k++) { if(s[k][j]=='#') { vt[i*m+j].push_back(k*m+j); vt[k*m+j].push_back(i*m+j); break; } } } } } for(i=0;i<n;i++) { if(s[i][0]=='#'&&(!vis[i*m])) { dfs(i*m); } if(s[i][m-1]=='#'&&(!vis[i*m+m-1])) { dfs(i*m+m-1); } } for(i=0;i<m;i++) { if(s[0][i]=='#'&&(!vis[i])) { dfs(i); } if(s[n-1][i]=='#'&&(!vis[(n-1)*m+i])) { dfs((n-1)*m+i); } } for(i=1;i+1<n;i++) { for(j=1;j+1<m;j++) { if(s[i][j]=='#'&&(!vis[i*m+j])) { ans++; dfs(i*m+j); } } } m1=m2=0; for(i=1;i+1<n;i++) { if(!mark_r[i]) { m1++; } } for(i=1;i+1<m;i++) { if(!mark_c[i]) { m2++; } } ans+=min(m1,m2); printf("%lld\n",ans); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(3) #define int long long #define ri register #define mk make_pair #define fi first #define se second #define pb push_back #define eb emplace_back #define is insert #define es erase using namespace std; const int N=210; inline int read() { int s=0, w=1; ri char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); } while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar(); return s*w; } int n,m,c[N],to[N][N],v1[N*N],v2[N*N]; struct Edge { int u,v; }e[N*N]; int Ans[N*N]; struct Node { int u,ty,id; }; vector<Node> G[N]; void DFS(int x) { for(auto i:G[x]) { if(~Ans[i.id]) continue; if(c[x]^c[i.u]) continue; Ans[i.id]=i.ty; DFS(i.u); } } signed main() { n=read(), m=read(); memset(Ans,-1,sizeof(Ans)); for(ri int i=1;i<=m;i++) { e[i].u=read(), e[i].v=read(); G[e[i].u].eb((Node){e[i].v,0,i}); G[e[i].v].eb((Node){e[i].u,1,i}); } for(ri int i=1;i<=n;i++) c[i]=read(); for(ri int i=1;i<=m;i++) { if(c[e[i].u]<c[e[i].v]) Ans[i]=1; else if(c[e[i].u]>c[e[i].v]) Ans[i]=0; } for(ri int i=1;i<=m;i++) { if(~Ans[i]) continue; DFS(e[i].u); } for(ri int i=1;i<=m;i++) Ans[i]?puts("<-"):puts("->"); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, S, D; cin >> N >> S >> D; vector<int> X(N), Y(N); for (int i = 0; i < N; i++) { cin >> X.at(i) >> Y.at(i); } int count = 0; for (int i = 0; i < N; i++) { if (X.at(i) < S && Y.at(i) > D) { count++; } } if (count == 0) { cout << "No" << endl; } else { cout << "Yes" << endl; } }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i,s,n) for (int i = (s); i < (n); ++i) #define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define len(x) (int)(x).size() #define dup(x,y) (((x)+(y)-1)/(y)) #define pb push_back #define eb emplace_back #define Field(T) vector<vector<T>> #define pq(T) priority_queue<T,vector<T>,greater<T>> using namespace std; using ll = long long; using P = pair<int,int>; int main() { int n; cin >> n; int x = 0; vector<int> a(n); rep(i,0,n) cin >> a[i]; rep(i,0,n) { int b; cin >> b; x += a[i]*b; } if (x == 0) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include<iostream> #include<bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<int> A(N); vector<int> B(N); for(int i=0; i<N; i++) cin >> A[i]; for(int i=0; i<N; i++) cin >> B[i]; int sum=0; for(int i=0; i<N; i++){ sum += A[i]*B[i]; } if(sum==0) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long r, x, y; cin >> r >> x >> y; double d = sqrt(x*x + y*y) / r; if (d < 1) { cout << 2 << endl; } else { cout << ceil(d) << endl; } }
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <string.h> #include <vector> #include <queue> #include <cmath> #include <complex> #include <functional> #include <numeric> #include <iomanip> #include <cassert> #include <random> #include <chrono> /* #include <atcoder/all> */ /* using namespace atcoder; */ using namespace std; void debug_out(){ cout << "\n"; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << H << " "; debug_out(T...); } #ifdef _DEBUG #define debug(...) debug_out(__VA_ARGS__) #else #define debug(...) #endif #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for(int i=(a);i<(n);++i) #define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=int(n)-1;i>=0;--i) #define IN(a, x, b) (a<=x && x<b) #define OUT(a, x, b) (x<a || b<=x) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const ll MOD = 1000000007; /* const ll MOD = 998244353; */ const ll INF = 1ll<<60; const double PI = acos(-1); struct INIT { INIT(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }}INIT; signed main() { int R, X, Y; cin >> R >> X >> Y; int d = (X*X+Y*Y); R *= R; int i = 0; while(true){ if(i*i*R == d){ cout << i << "\n"; return 0; } if(i*i*R > d){ if(i == 1) cout << 2 << "\n"; else cout << i << "\n"; return 0; } i++; } /* cout << R << " " << d << " " << d/R << "\n"; */ return 0; }
#include<iostream> #include<algorithm> #include<string> #include<cmath> #include<vector> #include<map> #include<bitset> #include<set> #include<stack> #include<queue> #include<list> typedef long long ll; using namespace std; /* time: wa: 反省: 考察・解説: */ int main(){ int n; cin >> n; if(n % 2 == 0) cout << "White" << endl; else cout << "Black" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int number=0; if ( N==3 ) cout << 6 << " " << 10 << " " << 15; else { for ( int i=1; i<=10000; i++ ) { if ( i%6==0 || i%10==0 || i%15==0 ) { cout << i << " "; number++; } if ( number == N ) break; } } }
#include<bits/stdc++.h> using namespace std; int main(){ int N,K; cin >> N >> K; vector<vector<int>> T(N, vector<int>(N)); for(int i=0; i<N; i++){ for(int j=0; j<N; j++){ cin >> T[i][j]; } } int array[N-1]; for(int i=2; i<=N; i++){ array[i-2] = i; } int temp; int time; int count = 0; do { temp = 1; time = 0; for (int i = 0; i < N-1; i++) { time += T.at(temp-1).at(array[i]-1); temp = array[i]; } time += T.at(temp-1).at(0); if(time == K) count++; }while (next_permutation(array, array + N-1)); cout << count << endl; }
#include <iostream> #include <bits/stdc++.h> #include <cmath> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <iterator> #include <list> #include <stack> #include <map> #include <set> #include <functional> #include <numeric> #include <utility> #include <limits> #include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <map> #include <tuple> #define for1(x,y) for(long long x = 1; x<ll(y); x++) #define forn(x,y,z) for(long long x = y; x< ll(z); x++) #define for0(x,y,z) for(long long x = y; x< ll(z); x++) #define PII pair<int,int> #define FF first #define SS second #define PB push_back #define all(x) x.begin(), x.end() #define forn(x,y,z) for(long long x = y; x< ll(z); x++) #define for0(x,y,z) for(long long x = y; x< ll(z); x++) using namespace std; //Errichto Orz debug template #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " // ends debug // aliases using ll = long long; using ull = unsigned long long; using ui = unsigned int; using ld = long double; #define vi vector<int> #define vl vector<ll> #define pb(x) push_back(x) const double EPS = (1e-7), EPSm= (1e7); const ll linf = (ll)1e17 + 5; long double PI = 3.14159265359; int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{1,1},{-1,1},{1,-1}}; const int MAXN = 1e5+10; const int NINF = -1e5; const int INF = 20000000; const ll MOD = 1e9+7; // Mathematical functions. int GCD(int a, int b) { if (!b) return a; return GCD(b, a % b); } int LCM(int a, int b) { return (a * b) / GCD(a, b); } int modpow(int x, int n, int m = MOD) { if (n == 0) return 1 % m; long long u = modpow(x, n / 2, m); u = (u * u) % m; if (n % 2 == 1) u = (u * x) % m; return u; } /** * m should be prime */ int modinv(int x, int m = MOD) { return modpow(x, m - 2, m); } //////////////////// START CODE HERE /////////////////////////// int n,k; int dis[9][9]; map<ll,int> ans; void f(int mask,int d, int pos){ if(mask==(1<<n)-1){ ans[d+dis[pos][0]]++; return; } else { forn(i,0,n){ if((mask&(1<<i))==0){ f(mask|(1<<i),d+dis[pos][i],i); } } } } void solve() { cin>>n>>k; forn(i,0,n){ forn(j,0,n) cin>>dis[i][j]; } f(1,0,0); cout<<ans[k]; return; } int main() { ios::sync_with_stdio(false); cin.tie(0); int test; //cin >> test; //while(test--) solve(); return 0; }
#include <iostream> int main() { int N, K; std::cin >> N >> K; long long sum = 100 * K * N * (N + 1) / 2 + N * K * (K + 1) / 2; std::cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; #define all(a) (a).begin(), (a).end() #define int long long typedef long long ll; int32_t main() { cin.tie(0); ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; int s = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= k; ++j) { string str = to_string(i) + "0" + to_string(j); s += stoll(str); } } cout << s; }
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } int main() { int n = ri(); int m = ri(); if (n == 1) { if (m == 0) puts("1 2"); else puts("0"); return 0; } if (m < 0 || m > n - 2) { puts("-1"); return 0; } int first = n - 2 - m; for (int i = 0; i < first; i++) printf("%d %d\n", i * 2 + 1, i * 2 + 2); printf("%d %d\n", 2 * first + 1, 2 * n); for (int i = first + 1; i < n; i++) printf("%d %d\n", i * 2, i * 2 + 1); return 0; }
// atcoder/arc106/C/main.cpp // author: @___Johniel // github: https://github.com/johniel/ #include <bits/stdc++.h> #define each(i, c) for (auto& i : c) #define unless(cond) if (!(cond)) using namespace std; template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; } template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; } template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; for (auto& i: v) os << i << ","; os << ")"; return os; } template<typename T> istream& operator >> (istream& is, vector<T>& v) { for (auto& i: v) is >> i; return is; } template<typename T> ostream& operator << (ostream& os, set<T> s) { os << "#{"; for (auto& i: s) os << i << ","; os << "}"; return os; } template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; for (auto& i: m) os << i << ","; os << "}"; return os; } template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); } template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); } using lli = long long int; using ull = unsigned long long; using point = complex<double>; using str = string; template<typename T> using vec = vector<T>; constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1}); constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1}); constexpr lli mod = 1e9 + 7; int main(int argc, char *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); cout.setf(ios_base::fixed); cout.precision(15); lli n, m; while (cin >> n >> m) { if (m == 0) { int a = 1; int b = a + 1; for (int i = 0; i < n; ++i) { cout << a << ' ' << b << endl; a = b + 1; b = a + 1; } continue; } const int M = 2 * 1e6; if (m < 0) { cout << -1 << endl; continue; } unless (m + 2 <= n) { cout << -1 << endl; continue; } if (m) { cout << 1 << ' ' << M << endl; int a = 2; int b = a + 1; for (int i = 0; i < m + 1; ++i) { cout << a << ' ' << b << endl; a = b + 1; b = a + 1; } } int a = M + 1; int b = a + 1; for (int i = m + 2; i < n; ++i) { cout << a << ' ' << b << endl; a = b + 1; b = a + 1; } } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); const int n=3; vector<int> a(n); for(auto& ai : a) { cin >> ai; } sort(a.begin(), a.end()); cout << accumulate(next(a.begin()), a.end(), 0) << "\n"; return 0; }
#include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " void solve() { vector<int> v(3); for (int &i : v) cin >> i; sort(v.begin(), v.end()); cout << v[1] + v[2] << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int _ = 1; //cin >> _; while (_--) { solve(); } }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(void){ ll n; cin >> n; unordered_set<ll> num; for (int i=1; i<=n; i++){ string s_i = to_string(i); for (int j=0; j<s_i.size(); j++){ if (s_i[j] == '7'){ num.insert(i); } } stringstream ss; ss << oct << i; for (int k=0; k<ss.str().size(); k++){ if (ss.str()[k] == '7'){ num.insert(i); } } } cout << n - num.size() << endl; }
#include<bits/stdc++.h> #define fast_az_fuk ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define ll long long #define ull unsigned ll #define ld long double #define pb push_back #define pf push_front #define dll deque<ll> #define vll vector<ll> #define pll pair<ll,ll> #define vpll vector<pll> #define dpll deque<pll> #define mapll map<ll,ll> #define endl "\n" #define all(v) v.begin(),v.end() #define ms(a,x) memset(a,x,sizeof(a)) using namespace std; bool octal(ll a){ while(a){ ll r=a%8; if(r==7) return 0; a/=8; } return 1; } bool decimal(ll a){ while(a){ ll r=a%10; if(r==7) return 0; a/=10; } return 1; } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fast_az_fuk ll n; cin>>n; ll c=0; for(ll i=1;i<=n;i++){ if(octal(i)&&decimal(i)) c++; } cout<<c; return 0; }
//#undef DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; const ll llinf = (1ll<<61)-1; #define sz(a) int(a.size()) #define all(x) begin(x), end(x) #ifdef DEBUG const int DEBUG_END = 26; #define DOS cout #include <debug.h> #else #define bug(args...) void() #define cbug(a, args...) #endif #define ASSERT(a, o, b, args...) if (!((a)o(b))) bug(a, b, ##args), assert((a)o(b)); #define fi first #define se second int TC = 1, CN; const int inf = 998244353; template<class C, class D> bool operator ==(const C &ls, const D &rs) { return !(ls<rs or rs<ls); }//{ template<class C, class D> bool operator !=(const C &ls, const D &rs) { return !(rs==ls); } template<class C, class D> bool operator >(const C &ls, const D &rs) { return rs<ls; } template<class C, class D> bool operator <=(const C &ls, const D &rs) { return !(rs<ls); } template<class C, class D> bool operator >=(const C &ls, const D &rs) { return !(ls<rs); } // += can't be made generic as x+=y is not the same as x=x+y when x is string template<class C, class D> C& operator -=(C &x, const D &y) { return x = x-y; } template<class C, class D> C& operator *=(C &x, const D &y) { return x = x*y; } #include <ext/numeric> using __gnu_cxx::power; template<int mod> struct mint// Make sure to not do any operation with int outside [-mod, 2*mod), *1 to mod forcefully), use << instead of 2* { int x; mint() = default; mint(const int &rs) : x(rs + (rs>=0 ? 0 : mod) - (rs<mod ? 0 : mod)) {}; mint operator -() { return -x; } // mint operator ~() = delete; // when mod is not surely prime mint operator ~() { ASSERT(x, !=, 0, "modinverse 0 is undefined"); return power(*this, mod-2); } mint operator +(const mint &rs) const { return x+rs.x; } mint& operator +=(const mint &rs) { return *this = *this + rs; } mint operator -(const mint &rs) const { return x-rs.x; } mint operator *(const mint &rs) const { return int(x*ll(rs.x)%mod); } mint operator <<(const int &rs) { return rs==1 ? *this+*this : *this*mint{1<<rs}; } mint& operator <<=(const int &rs) { return *this = *this<<rs; } mint& operator >>=(const int &rs) { return x>>=rs, *this; } // only for power() mint& operator ++() { return *this += 1; } mint& operator --() { return *this -= 1; } mint operator ++(int) { mint ret = x; ++*this; return ret; } mint operator --(int) { mint ret = x; --*this; return ret; } friend istream& operator >>(istream &is, mint &rs) { return is>>rs.x; } friend ostream& operator <<(ostream &os, const mint &rs) { return os<<rs.x; } friend mint operator +(const mint &ls, const int &rs) { return ls+mint(rs); } friend mint operator +(const int &ls, const mint &rs) { return mint(ls)+rs; } friend mint operator -(const mint &ls, const int &rs) { return ls-mint(rs); } friend mint operator -(const int &ls, const mint &rs) { return mint(ls)-rs; } friend mint operator *(const mint &ls, const int &rs) { return ls*mint(rs); } friend mint operator *(const int &ls, const mint &rs) { return mint(ls)*rs; } bool operator <(const mint &rs) const { return x<rs.x; } friend bool operator <(const int &ls, const mint &rs) { return ls<rs.x; } friend bool operator <(const mint &ls, const int &rs) { return ls.x<rs; } }; using Int=mint<inf>; Int operator""_i(unsigned long long x) { return {int(x)}; }//} const int N = 200005; int n, m, k; signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); cout.precision(11), cout.setf(ios::fixed); //cin >> TC;// inputAll + resetAll -> solution auto kase = [&]()->void { cin >> n >> m >> k; Int ans = 0; if (n == 1) ans = power(Int(k), m); else if (m == 1) ans = power(Int(k), n); else { for (int i = 1; i <= k; i++) { ans += (power(Int(i), n)-(i == 1 ? 0 : power(Int(i-1), n)))*power(Int(k-i+1), m); } } cout << ans << '\n'; }; while (CN++!=TC) kase(); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll mod = 998244353; ll modpow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main(){ ll h,w,k; cin >> h >> w >> k; if(h==1){ cout << modpow(k,w,mod) << endl; return 0; } if(w==1){ cout << modpow(k,h,mod) << endl; return 0; } ll ans = 0; vector<ll> Ah(k,0); vector<ll> Aw(k,0); Ah.at(0) = 1; Aw.at(0) = 1; for(int i=2;i<=k;i++){ Ah.at(i-1) = (modpow(i,h,mod)-modpow(i-1,h,mod))%mod; Aw.at(i-1) = (modpow(i,w,mod)-modpow(i-1,w,mod))%mod; } ll sum = 0; for(int i=1;i<=k;i++){ //cout << Ah.at(i-1) << endl; sum += Ah.at(i-1); sum%=mod; ans += (sum*Aw.at(k-i))%mod; ans%=mod; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() string F(string a){//aのAとBを反転させたやつを返す for(auto &p:a) if(p=='A')p='B'; else p='A'; return a; } int G(int a){ return (1<<a)-1; } int main(){ int n;cin>>n; vector<vector<string>> s(n+1);//s[i]:n=iの時の答え for(int i=1;i<=n;i++)s[i].resize(G(i)); s[1][0]="AB"; for(int i=2;i<=n;i++){ REP(j,G(i-1)){ s[i][j] =s[i-1][j]+F(s[i-1][j]); s[i][G(i-1)+j]=s[i-1][j]+ s[i-1][j]; } s[i].back()=string(1<<(i-1),'A')+string(1<<(i-1),'B'); } cout<<s[n].size()<<endl; for(string p:s[n])cout<<p<<endl; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; ll I=167167167167167167; ll Q=1e9+7; string han(string S){ string T; for(int i=0;i<S.size();i++){ if(S[i]=='A') T+="B"; else T+="A"; } return T; } int main() { ll N,A=1; cin>>N; vector<string> p; for(int i=0;i<N;i++){ A*=2; vector<string> q; string T; for(int j=0;j<A;j++){ if(j*2<A) T+="A"; else T+="B"; } q.push_back(T); for(auto x:p){ q.push_back(x+x); q.push_back(x+han(x)); } p=q; } cout<<A-1<<endl; for(auto x:p) cout<<x<<endl; }
#ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include<bits/stdc++.h> using namespace std; #define rep(i,s,t) for(ll i = (ll)(s); i < (ll)(t); i++) #define rrep(i,s,t) for(ll i = (ll)(s-1);(ll)(t) <= i; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<ll,ll> Pll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; constexpr ll INF = numeric_limits<ll>::max()/4; constexpr ll n_max = 210; #define int ll const long double pi = 3.14159265358979323846; template <typename A, typename B> string to_string(pair<A, B> p); string to_string(const string &s) {return '"' + s + '"';} string to_string(const char *c) {return to_string((string) c);} string to_string(bool b) {return (b ? "true" : "false");} template <size_t N> string to_string(bitset<N> v){ string res = ""; for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]); return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for(const auto &x : v) { if(!first) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";} void debug_out() {cerr << endl;} template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif template<class T> bool chmax(T &a, T b){if(a < b){a = b; return true;} return false;} template<class T> bool chmin(T &a, T b){if(a > b){a = b; return true;} return false;} void YES(bool ok){ cout << (ok ? "Yes" : "No") << endl; exit(0); } signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll n; cin >> n; vector<Pll> v(2 * n, {-1, -1}); rep(i, 0, n) { ll a,b; cin >> a >> b; if(a != -1){ a--; if (v[a] != Pll{-1, -1}) YES(false); v[a] = {0, i}; } if(b != -1){ b--; if (v[b] != Pll{-1, -1}) YES(false); v[b] = {1, i}; } if(a != -1 && b != -1){ if (a > b) YES(false); } } // [l, r]の区間を正しく補完できるかチェック auto check = [&](ll l, ll r) { if (l & 1) return false; if (~r & 1) return false; ll len = (r - l + 1) / 2; bool res = true; rep(i, l, r + 1){ ll j; if (i <= (l + r) / 2) { j = i + len; res &= v[i].first != 1; if(v[i] != Pll{-1, -1} && v[j] != Pll{-1, -1}){ res &= v[j].first == 1 && v[i].second == v[j].second; } } else { j = i - len; res &= v[i].first != 0; if (v[i] != Pll{-1, -1} && v[j] != Pll{-1, -1}) { res &= v[j].first == 0 && v[i].second == v[j].second; } } } if (res) debug(l, r); return res; }; bitset<n_max> dp(0); rep(i,0,2 * n){ if (~i & 1) continue; rep(j, 0, i) { if (!dp[j]) continue; if (check(j + 1, i)){ dp[i] = 1; break; } } if (check(0, i)) dp[i] = 1; } debug(dp); YES(dp[2 * n - 1]); }
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(false); cin.tie(0); #define FOR(i,s,n) for(int i = (s); i < (n); i++) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for(int i = (n); i >= 0; i--) #define ALL(n) (n).begin(), (n).end() #define RALL(n) (n).rbegin(), (n).rend() #define ATYN(n) cout << ( (n) ? "Yes":"No") << '\n'; #define CFYN(n) cout << ( (n) ? "YES":"NO") << '\n'; #define OUT(n) cout << (n) << '\n'; using ll = long long; using ull = unsigned long long; using pii = pair<int,int>; using pll = pair<ll,ll>; int main(void) { IOS int n; cin >> n; ll mh = 0, che = 0; double eu = 0; REP(_,n) { ll x; cin >> x; mh += abs(x); eu += x*x; che = max(che,abs(x)); } eu = sqrt(eu); OUT(mh) cout << fixed << setprecision(10) << eu << '\n'; OUT(che) return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> int main(void) { long long a, b, c; std::cin >> a >> b >> c; if (a * a < c * c - b * b) std::cout << "Yes" << std::endl; else std::cout << "No" << std::endl; return (0); }
#include <bits/stdc++.h> #define rep(a,n) for (ll a = 0; a < (n); ++a) using namespace std; //using namespace atcoder; using ll = long long; typedef pair<ll,ll> P; typedef pair<ll,P> PP; //typedef vector<vector<int> > Graph; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a >= b) { a = b; return 1; } return 0; } const ll INF = 1e18; #define debug(v) cout<<#v<<": ",prt(v); template <typename A,typename B> inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";} template <typename A,typename B,typename C> inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";} inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';} template <typename T> inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template<typename T> inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } } template <typename T> inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} /* */ int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); ll a,b,w; cin >> a >> b >> w; w *= 1000; ll MIN = (w+b-1)/b; ll MAX = w/a; if(MIN>MAX){ cout << "UNSATISFIABLE" << endl; return 0; } cout << MIN << ' '; cout << MAX << endl; return 0; }
//Made by imalyd //Problem Name: ... //Problem Link: ... //Category: ... #include<bits/stdc++.h> using namespace std; typedef unsigned int ui; typedef long long ll; typedef unsigned long long ull; #define rep(i,a,b) for(i=(a);i<=(b);++i) #define per(i,a,b) for(i=(a);i>=(b);--i) #define REP(i,a,b) for(i=(a);i< (b);++i) #define PER(i,a,b) for(i=(a);i> (b);--i) #define ERR(...) fprintf(stderr,__VA_ARGS__) template<class T>inline void cmn(T &x,const T &a){if(a<x)x=a;} template<class T>inline void cmx(T &x,const T &a){if(x<a)x=a;} template<class T>inline bool bmn(T &x,const T &a){if(a<x){x=a;return true;}else return false;} template<class T>inline bool bmx(T &x,const T &a){if(x<a){x=a;return true;}else return false;} const int p=998244353,ps2=p-2; inline void add(int &x){if(x>=p)x-=p;} inline void sub(int &x){if(x< 0)x+=p;} #define MUL(a,b) ((0ll|(a))*(b)%p) template<class T>int expo(int a,T b){ int s(1); while(b){if(b&1)s=MUL(s,a);b>>=1;a=MUL(a,a);} return s; } int expo(int a){ int b(ps2),s(1); while(b){if(b&1)s=MUL(s,a);b>>=1;a=MUL(a,a);} return s; } inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; } int S[150]; inline void sol(ll n,ll m){ int t=0; for(;;){ if(!n){while(m--)S[++t]=2;break;} if(!m){while(n--)S[++t]=1;break;} if(n>m){S[++t]=3;n-=m;}else{S[++t]=4;m-=n;} if(t>130)return; } if(t>130)return; printf("%d\n",t); while(t)printf("%d\n",S[t--]); exit(0); } int main(){ #ifdef ONLINE_JUDGE //freopen("problemname.in","r",stdin); //freopen("problemname.out","w",stdout); #endif ll n,m;int t=0; scanf("%lld",&n); m=(sqrt(1.25)-0.5)*n; cmx(m,1ll); cmn(m,n-1ll); ll i; per(i,m,1ll)if(gcd(n,i)==1ll)sol(n,i); rep(i,m,n )if(gcd(n,i)==1ll)sol(n,i); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int exgcd(int a,int b,int &x,int &y){ if(!b){ x=1; y=0; return a; } else { int d=exgcd(b,a%b,x,y); int temp=x; x=y; y=temp-a/b*y; return d; } } //求ax+by=c,的x的最小整数解,无解为-1 int findK(int a,int b,int c){ int x,y; int gcd=exgcd(a,b,x,y); if(c%gcd)return -1; int k=c/gcd; int p=x*k; int q=p*k; p%=b/gcd; p+=b/gcd; p%=b/gcd; return p; } int T; int x,y,p,q; void solve(){ cin>>T; while(T--){ cin>>x>>y>>p>>q; int a,b,c,d,d1,d2; a=x,b=x+y-1,c=p,d=p+q-1,d1=2*(x+y),d2=p+q; int ans=2e18; if((a>=c&&a<=d)||(c>=a&&c<=b)){ if(a>=c&&a<=d) ans=min(ans,a); if(c>=a&&c<=b) ans=min(ans,c); cout<<ans<<endl; } else{ if(d1==d2) cout<<"infinity"<<endl; else{ int num=x%d2; for(int i=c;i<=d;++i){ int a1=a,b1=d1,c1=d2,d11=i; int num1=findK(b1,c1,d11-a1); if(findK(b1,c1,d11-a1)!=-1){ ans=min(ans,num1*b1+a1); } } num=p%d1; for(int i=a;i<=b;++i){ int a1=c,b1=d2,c1=d1,d11=i; int num1=findK(b1,c1,d11-a1); if(findK(b1,c1,d11-a1)!=-1){ ans=min(ans,num1*b1+a1); } } if(ans==2e18) cout<<"infinity"<<endl; else cout<<ans<<endl; } } } } signed main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i < (int)(n); i++) #define all(x) (x).begin(),(x).end() typedef long long ll; #define F first #define S second #define YES(n)cout << ((n) ? "YES" : "NO" ) << endl #define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl #define chmin(a,b) a=min(a,b) #define chmax(a,b) a=max(a,b) #define _GLIBCXX_DEBUG const ll INF = 1e12; set<pair<int,int>> st; vector<vector<int>> graph(3000,vector<int>(0)); void dfs(int a,int b){ st.insert(make_pair(a,b)); for(int i=0;i<graph[b].size();i++){ if(st.count(make_pair(a,graph[b][i]))==0){ dfs(a,graph[b][i]); } } } int main(){ int n,m; cin>>n>>m; rep(i,m){ int a,b; cin>>a>>b; a--;b--; graph[a].push_back(b); } rep(i,n){ dfs(i,i); } cout<<st.size()<<endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; ++i) #define PI 3.14159265359 #define INF 100100100 #define mod 1000000007 using namespace std; using ll = long long; using P = pair<int,int>; using Graph = vector<vector<int>>; const int mx = 200005; int main(){ int n, w; cin >> n >> w; vector<ll> d(mx); rep(i,n){ int s, t, p; cin >> s >> t >> p; d[s] += p; d[t] -= p; } rep(i,mx) d[i+1] += d[i]; rep(i,mx){ if(d[i] > w){ cout << "No" << endl; return 0; } } cout << "Yes" << endl; return 0; }
#include<bits/stdc++.h> #define int long long #define pii pair<int,int> #define S second #define F first using namespace std; int n,q,t,a,b,k,p[400005]; string s; signed main(){ ios::sync_with_stdio(0),cin.tie(0); cin>>n>>s>>q; for(int i=0;i<2*n;i++)p[i]=i; while(q--){ cin>>t>>a>>b; a--; b--; if(t==1){ swap(p[(a+k)%(2*n)],p[(b+k)%(2*n)]); } else{ k=(k+n)%(2*n); } } for(int i=0;i<2*n;i++)cout<<s[p[(i+k)%(2*n)]]; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(obj) (obj).begin(), (obj).end() typedef long long ll; template <class T = ll> T in() { T x; cin >> x; return (x); } int main() { int Ans = 0; ll N = in<ll>(); string S = in<string>(); ll Q = in<ll>(); vector<int> T(Q); vector<ll> A(Q); vector<ll> B(Q); REP(i, Q) { cin >> T[i]; cin >> A[i]; cin >> B[i]; if (i > 0) { if ((T[i - 1] == 2) && (T[i] == 2)) { T[i] = 0; T[i - 1] = 0; } } } REP(i, Q) { if (T[i] == 1) { swap(S[A[i]-1], S[B[i]-1]); } else if (T[i] == 2) { S = S.substr(N) + S.substr(0, N); } } cout << S << endl; return 0; }
#include <bits/stdc++.h> #include <unordered_set> #include <algorithm> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using vs = vector<string>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repll(i,n) for (ll i = 0; i < (ll)(n); i++) #define fore(x,a) for(auto&(x) : (a)) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } #define ALL(a) (a).begin(), (a).end() const ll INFL = 1e18; const int INFI = 1e9; const int MOD = 1e9 + 7; int main(){ ll N,P; cin >> N >> P; ll res = 1; res *= P - 1; ll i = P - 2; ll t = 0; ll s = 1; while(true) { if (t + s >= N) { break; } res *= i; res %= MOD; t += s; i = i*i%MOD; s *= 2; } for (int i = t + 1; i < N; i++) { res *= P - 2; res %= MOD; } cout << res << endl; }
#include <bits/stdc++.h> #include <variant> #define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++) #define rep(i,n) rep2(i,0,n) #define all(x) begin(x),end(x) using namespace std; using namespace std::string_literals; using i32 = int32_t; using i64 = int64_t; using f64 = double; using f80 = long double; using vi32 = vector<i32>; using vi64 = vector<i64>; char rps(char l, char r) { if (l == r) return l; switch (l) { case 'R': return r == 'P' ? 'P' : 'R'; case 'P': return r == 'S' ? 'S' : 'P'; case 'S': return r == 'R' ? 'R' : 'S'; } throw runtime_error("BAD"); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); i32 n,k; cin>>n>>k; string s; cin>>s; rep(i,k) { string t = s; rep(j,n) { i32 l = (2*j)%n; i32 r = (2*j+1)%n; t[j] = rps(s[l], s[r]); } swap(s, t); } cout<<s[0]<<endl; return 0; }
#include <bits/stdc++.h> #define ll long long int using namespace std; ll arr[10000]; // Function to find power ll power(ll x, ll y, ll p) { ll res = 1; // Initialize result // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with the result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); ll a,b,c,i,j,k,x,y,z; cin>>a>>b>>c; a=a%10; if (a==0) { cout<<0<<endl; return 0; } if (a==1) { cout<<1<<endl; return 0; } if (a==2) { arr[1]=2; arr[2]=4; arr[3]=8; arr[0]=6; cout<<arr[power(b,c,4)]<<endl; return 0; } if (a==3) { arr[1]=3; arr[2]=9; arr[3]=7; arr[0]=1; cout<<arr[power(b,c,4)]<<endl; return 0; } if (a==4) { arr[1]=4; arr[0]=6; cout<<arr[power(b,c,2)]<<endl; return 0; } if (a==5) { cout<<5<<endl; return 0; } if (a==6) { cout<<6<<endl; return 0; } if (a==7) { arr[1]=7; arr[2]=9; arr[3]=3; arr[0]=1; cout<<arr[power(b,c,4)]<<endl; return 0; } if (a==8) { arr[1]=8; arr[2]=4; arr[3]=2; arr[0]=6; cout<<arr[power(b,c,4)]<<endl; return 0; } if (a==9) { arr[1]=9; arr[0]=1; cout<<arr[power(b,c,2)]<<endl; return 0; } }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define rng(i, a, b) for (int i = int(a); i < int(b); ++i) #define rep(i, b) rng(i, 0, b) #define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define per(i, b) gnr(i, 0, b) #define all(obj) begin(obj), end(obj) #define allr(obj) rbegin(obj), rend(obj) #define sz(x) (int)(x).size() #define cinv(a) rep(i, sz(a)) cin >> a[i] #define debug(a) rep(i, sz(a)) cout << a[i] << " \n"[i == sz(a)-1] #define show(x) cerr<<#x<<" = "<<x<<endl; #define pb push_back #define eb emplace_back typedef long long ll; typedef pair<int, int> Pi; typedef vector<Pi> vp; typedef vector<vp> vvp; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<vb> vvb; template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, 1, 0, -1}; const int MOD = 1e9 + 7; const int INF = 1e9 + 9; const ll LINF = 1e18; const long double eps = 1e-10; const char nl = '\n'; ll power(ll a, ll b, int mod) { return b ? power(a * a % mod, b / 2, mod) * (b % 2 ? a : 1) % mod : 1; } ll nCk(int n, int k, int mod) { ll x = 1, y = 1; for (int i = 1; i <= k; ++i) { x = x * (n - i + 1) % mod; y = y * i % mod; } return x * power(y, mod - 2, mod) % mod; } struct UF { vi d; UF(int n) : d(n, -1) {} int root(int x) { if (d[x] < 0) return x; return d[x] = root(d[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -d[root(x)]; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout<<fixed<<setprecision(20); int a, b, c; cin >> a >> b >> c; cout << power(a % 10, power(b, c, 4) + 4, 10) << nl; }
#include <iostream> #include <bits/stdc++.h> using namespace std; #define with_test(t, isT) int t=1;if(isT)cin>>t;while(t--) void init(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; } #define for0(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i < (int)(n); ++i) #define YES cout<<"YES\n"; #define YESC cout<<"YES\n";continue; #define NO cout<<"NO\n"; #define NOC cout<<"NO\n";continue; #define pb push_back #define mp make_pair #define fe first #define se second #define lld long long int #define ll long long void print(vector<int> arr){ for0(i,arr.size()){ cout<<arr[i]<<" "; } cout<<"\n"; } void print(int arr[], int n){ for0(i, n){ cout<<arr[i]<<" "; }cout<<"\n"; } void test_case(){ string s; cin>>s; reverse(s.begin(), s.end()); for(int i=0;i<s.length();i++){ if(s[i]=='6'){ cout<<"9"; } else if(s[i]=='9'){ cout<<"6"; }else{ cout<<s[i]; } } } int main() { init(); int isTest = 0; with_test(t,isTest){ test_case(); } return 0; } /* ====== WORK ===== */
#include<bits/stdc++.h> /*#include<boost/multiprecision/cpp_int.hpp> using boost :: multiprecision::cpp_int;*/ #define ll long long int #define mod 1e9+7 using namespace std; /*int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); }*/ void solve() { string n; cin >> n; for (int i = n.length() - 1; i >= 0; i--) { if (n[i] == '0') { n.erase(i); } else { break; } } ll l = n.length(); bool check = true; for (int i = 0; i < l / 2; i++) { if (n[i] != n[l - i - 1]) { check = false; } } if (check) { cout << "Yes"; } else { cout << "No"; } } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
#include <algorithm> #include <bitset> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <unordered_map> #include <unordered_set> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <typeinfo> #include <utility> #include <valarray> #include <vector> #include <cassert> #include <numeric> #define _GLIBCXX_DEBUG #define INF 100000010 #define PI 3.14159265359 using namespace std; const int64_t mod = 1000000007; const int64_t tmpMod = 998244353; int64_t getNum(int64_t N, int64_t n) { return N-abs(n-N) > 0 ? N-abs(n-N): 0; } int main() { int64_t N,K,ans=0; cin>>N>>K; // unordered_map<int, int> map; //vector<int64_t> A(N); for(int64_t i=1; i<=2*N-K-1; i++){ // cout<<i << " " <<getNum(N,i)<<" "<<getNum(N,i+K)<<endl; ans+=(getNum(N,i) * getNum(N,i+K)); } //vector<vector<int> > A(N,vector<int>(M)); // for(int i=1; i<=N; i++){ // for(int j=1; j<=N; j++){ // map[i+j]++; // } // } //sort(A.begin(),A.end()); //for (int i = 0; i < N; i++) { maximum = max(maximum , A[i]);} //for (int i = 0; i < N; i++) { minimum = min(minimum , A[i]);} cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <tuple> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef vector<pair<ll, ll>> vpll; #define rep(i, n) for (ll i = 0; i < (ll)n; ++i) #define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; ++i) const ll INF = 1LL << 60; const double EPS = 1e-20; const ll MOD = 1000000007; const double pi = 3.141592653589793; /* //1 int main() { ll n; cin >> n; cout << (n+1)*5000 << endl; } //*/ //* //2 ll _4Hr(ll r){ return double(3+r)/6*(2+r)*(1+r); } int main() { ll n, k; cin >> n >> k; ll J = 2*n - 2 - abs(k); ll ans = _4Hr(J); if (J > n - 1) ans -= 4 * _4Hr(J - n); cout << ans << endl; // k = abs(k); // FOR(i, 2, 2*n-k+1) { // ll x1=min((k+i)-1, 2*n+1-(k+i)); // ll x2=min(i-1, 2*n+1-i); // ans += x1 * x2; // } } //*/ /* //3 //https://atcoder.jp/contests/code-festival-2015-morning-middle/tasks/cf_2015_morning_easy_c int main() { ll n, k, m, r; cin >> n >> k >> m >> r; vll s(n-1); rep(i, n-1) cin >> s[i]; sort(s.begin(), s.end(), greater<ll>()); long double sum = 0; rep(i, k) sum += (long double)(s[i]) / k; // if (sum >= r * k) { // cout << 0 << endl; // } else { // ll score = r * k - (sum - s[k - 1]); // cout << ((score > m) ? -1 : score) << endl; // } if (sum >= r) { cout << 0 << endl; } else { ll score = ((long double)r - (sum - (long double)(s[k - 1]) / k)) * k; cout << ((score > m) ? -1 : score) << endl; } } //*/ /* //4 // https://atcoder.jp/contests/arc118/tasks/arc118_c int main() { ll n; cin >> n; vll v; if (n == 3) { v.push_back(6); v.push_back(10); v.push_back(15); } else if (n == 4) { v.push_back(210 / 2); v.push_back(210 / 3); v.push_back(210 / 5); v.push_back(210 / 7); } else { ll q[] = {2, 3, 5, 7, 11}; rep(z, 4) { rep(i, 30) { rep(J, 20) { rep(k, 20) { rep(L, 10) { ll p = pow(q[(z > 0)], i); if (p > 10000) break; ll tmp = pow(q[1 + (z > 1)], J); if (tmp > 10000) break; p *= tmp; tmp = pow(q[2 + (z > 2)], k); if (tmp > 10000) break; p *= tmp; tmp = pow(q[3 + (z > 3)], L); if (tmp > 10000) break; p *= tmp; v.push_back(p); } } } } } } rep(i, n) cout << v[i] << " "; cout << endl; cout << v.size() << endl; } //*/
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG //これつけるとA[N]でもいいらしい //for文のマクロ #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define mp(a,b) make_pair(a,b) #define big 1000000007 #define all(a) sort((a).begin(),(a).end()) //ソートのマクロ #define Re(a) reverse((a).begin(),(a).end()) #define YN(a) if(a){cout<<"Yes"<<endl;}else cout<<"No"<<endl;//条件によってYes、Noを出力する int main(){ #define int int64_t int a,b,c; map<int,int> p; cin>>a>>b>>c; p[a]+=1,p[b]+=1,p[c]+=1; if(p.size()==3){ cout<<0<<endl; } else{ for(auto c:p){ int cnt = c.second; if(cnt==1 || cnt==3){ cout<<c.first<<endl; return 0; } } } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { long long int a, b, c; cin >> a >> b >> c; if(a==b) cout << c << endl; else if(a==c) cout << b << endl; else if(b==c) cout << a << endl; else cout << '0' << endl; }
#include<bits/stdc++.h> using namespace std; using namespace std::chrono; #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define int long long #define rep(i,a,b) for(int i=a;i<b;i++) #define repn(i,a,b) for(int i=a;i>=b;i--) #define F first #define S second #define pii pair<int,int> #define vi vector<int> #define vii vector<pii> #define pb push_back #define all(v) (v).begin(), (v).end() #define mod 1000000007 int fexp(int a, int b,int m){int ans = 1;while(b){if(b&1)ans=(ans*a)%m; b/=2;a=(a*a)%m;}return ans;} int inverse(int a, int p){return fexp(a, p-2,p);} // int fac[2000001]; // int nCrModPFermat(int n, int r,int p) { // // cout<<1<<endl; // if (r==0) return 1; // return (((fac[n]*((((inverse(fac[r],p)%p)*inverse(fac[n-r],p))%p))))%p); // } // vi spf(10000001,1); vector<vector<int>> v(1000,vector<int>(1000,0)); int k,n; bool check(int num){ vector<vector<int>> dp(n+1,vector<int>(n+1,0)); int val=(k*k+1);val=val/2; rep(i,0,n){ rep(j,0,n){ if(v[i][j]>num)dp[i][j]=0; else dp[i][j]=1; if(j-1>=0)dp[i][j]+=dp[i][j-1]; if(i-1>=0)dp[i][j]+=dp[i-1][j]; if(i&&j)dp[i][j]-=dp[i-1][j-1]; } } int mx=0; rep(i,0,n-k+1){ rep(j,0,n-k+1){ int ans=dp[i+k-1][j+k-1]; if(i){ ans-=dp[i-1][j+k-1]; if(j)ans+=dp[i-1][j-1]; } if(j)ans-=dp[i+k-1][j-1]; // cout<<ans<<endl; mx=max(mx,ans); } } // cout<<num<<"__"<<mx<<endl; return (mx>=val); // return true; } void solve(){ cin>>n>>k; rep(i,0,n){ rep(j,0,n){ cin>>v[i][j]; } } int l=0,r=mod,ans=mod; while(l<r){ int mid=(l+r)/2; if(check(mid))r=mid; else l=mid+1; } cout<<r; } signed main(){ IOS; auto start = high_resolution_clock::now(); int t=1; // cin>>t; // spf[1]=1; // for(int i=2; i<=10000001; i++){ // if(spf[i]==1){ // // allprime.pb(i); // for(int j=i; j<=10000001; j+=i) // if(spf[j]==1)spf[j]=i; // } // } // fac[0] = 1; // for (int i=1 ; i<=200001; i++) fac[i] = fac[i-1]*i%mod; rep(i,0,t){ // cout<<"Case #"<<i+1<<": "; solve(); cout<<"\n"; } auto stop = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop - start); cerr<<"Time taken:"<<duration.count()/1000000.0<<"seconds"<<"\n"; // return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n, k, i, j, cent; cin >> n >> k; cent = (k * k) - int(k * k / 2); int a[800][800], s[801][801]; for(i = 0; i < n; i++){ for(j = 0; j < n; j++) cin >> a[i][j]; } for(i = 0; i <= n; i++) s[i][0] = 0, s[0][i] = 0; int L = 0, R = 1000000000, mid; while(L != R){ mid = (L + R) / 2; for(i = 1; i <= n; i++){ for(j = 1; j <= n; j++){ s[i][j] = s[i][j-1] + s[i-1][j] - s[i-1][j-1]; if(a[i-1][j-1] <= mid){ s[i][j]++; } } } bool exist = false; for(i = 1; i <= n - (k - 1) && !exist; i++){ for(j = 1; j <= n - (k - 1) && !exist; j++){ if(s[i+(k-1)][j+(k-1)] - s[i-1][j+(k-1)] - s[i+(k-1)][j-1] + s[i-1][j-1] >= cent){ exist = true; } } } exist ? R = mid : L = mid + 1; } cout << L << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1 << 19; int n, m; long long dp[maxn]; bool ok[maxn]; int x[110], y[110], z[110]; int main() { scanf ("%d %d", &n, &m); for (int i = 1; i <= m; i++) scanf ("%d %d %d", x + i, y + i, z + i); int upp = 1 << n; dp[0] = 1; for (int sta = 0; sta < upp; sta++) { bool f = 1; int cnt = __builtin_popcount(sta); for (int i = 1; i <= m; i++) { if (cnt > x[i]) continue; int c = 0; for (int o = 0; o < y[i]; o++) if ((1 << o) & sta) c++; if (c > z[i]) f = 0; } if (!f) dp[sta] = 0; for (int o = 0; o < n; o++) if (!((1 << o) & sta)) dp[sta | (1 << o)] += dp[sta]; } printf ("%lld\n", dp[upp - 1]); 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 MAX_N = 19; const int MAX_M = 105; ll dp[(1 << MAX_N)]; int X[MAX_M],Y[MAX_M],Z[MAX_M]; int main() { int N,M; cin >> N >> M; rep(i,M) { cin >> X[i] >> Y[i] >> Z[i]; //X[i] = i%(N-1)+1; Y[i] = i%(N-1)+1; Z[i] = i%N; } dp[0] = 1; for (int bits = 0; bits < (1 << N); ++bits) { vector<int> ppcnt(N+1,0); rep(i,N) {ppcnt[i+1] = ppcnt[i] + ((bits >> i) & 1);} /* rep(i,N+1) cout << ppcnt[i] << " "; cout << endl; */ rep(i,N) { if ((bits >> i) & 1 != 0) continue; bool ok = true; int c; rep(j,M) { if (i+1 <= Y[j]) c = 1; else c = 0; if (X[j] == ppcnt[N]+1 && ppcnt[Y[j]]+c > Z[j]) ok = false; } if (ok) dp[bits | (1 << i)] += dp[bits]; } //cout << bits << " " << dp[bits] << endl; } cout << dp[(1 << N) - 1] << endl; return 0; }
#include<iostream> //#include<bits/stdc++.h> #define r read() #define bb printf("!!\n"); #define cc printf("\n"); using namespace std; inline int read() { bool ok=0 ;int res=0;char c; while(((c=getchar())<'0'||c>'9')&&c!='-'); if(c=='-') ok=1;else res=c-48; while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+(c-48); return ok ? ~res + 1:res; } int n,m; int main() { #ifndef ONLINE_JUDGE // freopen("cin.txt","r",stdin); // freopen("cout.txt","w",stdout); #endif int a,b; cin>>a>>b; a+=b; int ans=4; if(a>=15&&b>=8)ans=1; else if(a>=10&&b>=3)ans=2; else if(a>=3)ans=3; printf("%d\n",ans); 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 ALL(n) begin(n),end(n) struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star; const long long INF = numeric_limits<long long>::max(); typedef long long ll; int main(){ int h,w,ans=0; cin >> h >> w; vector<string>s(h); rep(i,h)cin >> s[i]; rep(i,h){ rep(j,w){ if(j!=w-1 and s[i][j]=='.' and s[i][j+1]=='.')ans++; if(i!=h-1 and s[i][j]=='.' and s[i+1][j]=='.')ans++; } } cout << ans; return 0; }
#include<bits/stdc++.h> using namespace std; #define fRead(x) freopen(x,"r",stdin) #define fWrite(x) freopen (x,"w",stdout) #define LL long long #define ULL unsigned long long #define ff first #define ss second #define pb push_back #define PI acos(-1.0) #define mk make_pair #define pii pair<int,int> #define pll pair<LL,LL> #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 SQR(a) ((a)*(a)) #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 MEM(a,x) memset(a,x,sizeof(a)) #define ABS(x) ((x)<0?-(x):(x)) #define all(v) v.begin(),v.end() #define SORT(v) sort(v.begin(),v.end()) #define REV(v) reverse(v.begin(),v.end()) //#pragma GCC target ("avx2") //#pragma GCC optimization ("O3") //#pragma GCC optimization ("unroll-loops") //#pragma comment(linker, "/stack:200000000") //#pragma GCC optimize("Ofast") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define FastRead ios_base::sync_with_stdio(0);cin.tie(nullptr); #ifdef VAMP #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__) template < typename Arg1 > void __f(const char* name, Arg1&& arg1){ cout << name << " = " << arg1 << std::endl; } template < typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names+1, ','); cout.write(names, comma - names) << " = " << arg1 <<" | "; __f(comma+1, args...); } #else #define debug(...) #endif int main() { #ifdef VAMP clock_t tStart = clock(); freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif string str; cin >> str; bool hard = true; for(int i = 0;i < str.size();i += 2){ if(!islower(str[i]))hard = false; } for(int i = 1;i < str.size();i += 2){ if(!isupper(str[i]))hard = false; } if(hard)cout << "Yes\n"; else cout << "No\n"; #ifdef VAMP fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC); #endif }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(ll i = 0; i < (n); ++i) #define repA(i, a, n) for(ll i = a; i < (n); ++i) #define repD(i, a, n) for(ll i = a; i > (n); --i) #define trav(a, x) for(auto& a : x) #define all(x) x.begin(), x.end() #define sz(x) (long long) (x).size() #define fill(a) memset(a, 0, sizeof(a)) #define fst first #define snd second // #define mp make_pair #define pb push_back void __print(int x) {cout << x;} void __print(long x) {cout << x;} void __print(long long x) {cout << x;} void __print(unsigned x) {cout << x;} void __print(unsigned long x) {cout << x;} void __print(unsigned long long x) {cout << x;} void __print(float x) {cout << x;} void __print(double x) {cout << x;} void __print(long double x) {cout << x;} void __print(char x) {cout << '\'' << x << '\'';} void __print(const char *x) {cout << '\"' << x << '\"';} void __print(const string &x) {cout << '\"' << x << '\"';} void __print(bool x) {cout << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';} template<typename T> void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";} void _print() {cout << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define dbg(x...) cout << "[" << #x << "] = ["; _print(x) #else #define dbg(x...) #endif const long double PI = 3.141592653589793238462643383; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<long long int> vll; typedef vector<double> vd; typedef vector<pii> vii; long long Ceil(long long a, long long b){ if(a%b==0) return a/b; else return (a/b)+1; } // ll p=1000000007; ll p = 1000000000000000000+5; float E = 0.00000000000001; ll power(ll x, ll y){ if(y==0){ return 1; } if(y%2==0){ ll q=power(x,y/2); q=q%p; return (q*q)%p; } ll q=power(x,y/2); q=q%p; return ((x%p)*((q*q)%p))%p; } bool isPowerOfTwo (long long x) { /* First x in the below expression is for the case when x is 0 */ return x && (!(x&(x-1))); } long long int gcd(long long int a, long long int b){ if (a == 0) return b; return gcd(b % a, a); } long long int mod(long long int a, long long int b) { long long int ret = a%b; return ret>=0? ret: ret+b; } bool compare(pair<int, int> p1, pair <int, int> p2){ if(p1.first < p2.first) return true; else if(p1.first > p2.first) return false; else return p1.second > p2.second; } int main(){ std::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 n;cin>>n; vector <long long> vec(n); for(auto &x:vec) cin>>x; long long ans = 0; vector <long long> pref(n); vector <long long> max_pref(n); max_pref[0] = max(0ll, vec[0]); pref[0] = vec[0]; for(int i = 1 ; i < n ; i++){ pref[i] = pref[i-1]+vec[i]; max_pref[i] = max(pref[i], max_pref[i-1]); } long long curr = 0; for(int i = 0 ; i < n ; i++){ ans = max(curr+max_pref[i], ans); curr += pref[i]; } cout<<ans<<endl; } return 0; }
#include<bits/stdc++.h> #define ll long long int #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define nl "\n" #define fi first #define se second #define pb push_back #define mp make_pair #define fl(i,a,b) for(ll i=a;i<b;i+=1) #define rl(i,a,b) for(ll i=a;i>b;i-=1) #define input(a,n) fl(i,0,n)cin>>a[i]; #define printt(a,n) fl(i,0,n) cout<<a[i]<<' '; #define all(a) a.begin(),a.end() #define test() ll t; cin>>t; while(t--) #define maxelement(arr,n) *max_element(arr,arr+n) //typedef long long ll; const int INF = 1e9 + 1; const int MOD = 1e9 + 7; const int N = 1e6; using namespace std; int main(){ iamspeed; long n,x,h; cin>>n>>x; fl(i,0,n){ cin>>h; if(h!=x){ cout<<h<<" "; } } cout<<endl; } //APPROACHING A QUESTION //+ Think of binary search (max of min etc also if n<=2*10^5) //+ Think of common dp states (Even if it appears as maths //but constraints are small) //+ Check constraints //+ Keep calm and enjoy the question //+ Be sure to remove MOD from binpow (if needed) //+ Try bidirectional analysis for constructive questions //+ If given some sequence try thinking of prefix sums //+ If constraints are too large maybe its simple maths //+ In questions with binary operations think of bits independently //and also the change pattern //+ If two or more binary operations are given mostly there is //a relation between them and an arithmatic operator //+ multi sorce bfs // credits: fischerman
#include <bits/stdc++.h> using namespace std; mt19937_64 llrand((int) chrono::steady_clock::now().time_since_epoch().count()); #define st first #define nd second #define pb push_back #ifndef ONLINE_JUDGE #define db(x) cerr << #x << " == " << x << endl #define dbs(x) cerr << x << endl #define _ << ", " << #else #define db(x) ((void)0) #define dbs(x) ((void)0) #endif typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; const int INF = 0x3f3f3f3f, MOD = 1e9+7; const int N = 1e5+5; const ll MAX = 1e13; ll n; int p[35]; ll exp(ll b, ll e) { ll ans = 1; for(int i = 1; i <= e; i++) ans *= b; return ans; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n; ll ans = 1ll; for(ll i = 2; i <= n; i++) { ll aux = i; for(ll k = 2; k <= aux; k++) { int cnt = 0; while(aux % k == 0) { aux /= k; cnt++; } p[k] = max(p[k], cnt); } } for(int i = 2; i <= n; i++) ans = ans * exp(i, p[i]); ans++; cout << ans << "\n"; assert(ans >= n and ans <= MAX); for(ll i = 2; i <= n; i++) assert(ans % i == 1); }
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; int ans = 4; if(A+B>=15 && B>=8) ans = 1; else if(A+B>=10 && B>=3) ans = 2; else if(A+B>=3) ans = 3; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define fo(a,b) for(int64_t a=0;a<b;a++) #define sor(a) sort(a.begin(),a.end()) #define rev(a) reverse(a.begin(),a.end()) #define ll int64_t #define mod 1000000007 #define vl vector<int64_t> #define vc vector<char> #define vs vector<string> #define np(vec) next_permutation(vec.begin(),vec.end())==true #define en endl #define vvl vector<vector<int64_t>> #define stl stack<int64_t> #define qul que<int64_t> #define ms(a,b) memset(a,b,sizeof(a)) #define mp make_pair #define mt make_tuple #define fi first #define se second #define pq priority_queue #define pql priority_queue<int64_t> #define pu push #define po pop() #define to top() #define pqr priority_queue<ll,vector<ll>,greater<ll>> #define co cout #define ci cin #define wh while #define size size() int main() { ll a,b; cin>>a>>b; if(a+b>=15&&b>=8){cout<<1<<en;} else if(a+b>=10&&b>=3){co<<2<<en;} else if(a+b>=3){cout<<3<<en;} else{cout<<4<<en;} }
#include <bits/stdc++.h> using namespace std; #define int 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 repa(i,a,n) for(int i=(a); i<(int)(n); i++) #define all(vec) vec.begin(),vec.end() #define COUT(x) cout<<(x)<<endl #define YES(x) cout<<(x?"YES":"NO")<<endl #define Yes(x) cout<<(x?"Yes":"No")<<endl using vi = vector<int>; using vs = vector<string>; using vvi = vector<vi>; using pii = pair<int, int>; using psi = pair<string, int>; const int dx[4] = {1, 0,-1, 0}; const int dy[4] = {0, 1, 0,-1}; const int mod = 1e9+7; int gcd(int a, int b){if(a%b == 0){return(b);}else{return(gcd(b, a%b));}} int lcm(int a, int b){return a/gcd(a, b)*b;} //cout << fixed << setprecision(15); int N, M, K, H, W, L, R, X, Y; string S, T; int INF = 1e15; signed main(){ cin >> N; vi p(N); rep(i,N) cin >> p[i]; int cur = 0; map<int, int> mp; rep(i,N){ mp[p[i]]++; while(mp[cur]!=0) cur++; COUT(cur); } return 0; }
#include <iostream> using namespace std; int main() { int N, W; cin >> N >> W; cout << N / W << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<(int)(n); i++) #define rep1(i, n) for(int i=1; i<=(int)(n); i++) #define rep2(i, n, m) for(int i=(int)n; i<=(int)m; i++) typedef long long ll; typedef vector<int> vi; typedef vector<vi> wi; typedef vector<ll> vl; const ll inf=1LL<<60; const ll mod=1000000007; inline void InitRnd(){ srand((unsigned int)time(NULL)); } int n; vector<tuple<int, int, int, int>> s(210); bool checker(int a, int b, int c, int d, int j){ rep(i, n){ if(i==j)continue; int e, f, g, h; tie(e, f, g, h)=s[i]; int sw=c-a, sh=d-b, tw=g-e, th=h-f, dx=e-a, dy=f-b; if(-tw<dx&&dx<sw&&-th<dy&&dy<sh)return false; } return true; } int main(){ cin.tie(0); ios::sync_with_stdio(false); InitRnd(); cin >> n; vi x(n), y(n), r(n); rep(i, n){ cin >> x[i] >> y[i] >> r[i]; s[i]={x[i], y[i], x[i]+1, y[i]+1}; } vector<bool> ok(n, false), ok0(n, false), ok1(n, false), ok2(n, false), ok3(n, false); int cnt=0; while(true){ rep(i, n){ if(ok[i])continue; int a, b, c, d; tie(a, b, c, d)=s[i]; int t=(c-a)*(d-b); if(checker(a-1, b, c, d, i)&&a-1>=0&&!ok0[i]){ if(t-r[i]>t-r[i]+d-b){ cnt++; ok0[i]=true; } else s[i]=make_tuple(a-1, b, c, d); } else if(checker(a, b-1, c, d, i)&&b-1>=0&&!ok1[i]){ if(t-r[i]>t-r[i]+c-a){ cnt++; ok1[i]=true; } else s[i]=make_tuple(a, b-1, c, d); } else if(checker(a, b, c+1, d, i)&&c+1<=10000&&!ok2[i]){ if(t-r[i]>t-r[i]+d-b){ cnt++; ok2[i]=true; } else s[i]=make_tuple(a, b, c+1, d); } else if(checker(a, b, c, d+1, i)&&d+1<=10000&&!ok3[i]){ if(t-r[i]>t-r[i]+c-a){ cnt++; ok3[i]=true; } else s[i]=make_tuple(a, b, c, d+1); } else{ cnt++; ok[i]=ok0[i]=ok1[i]=ok2[i]=ok3[i]=true; } if(ok0[i]&&ok1[i]&&ok2[i]&&ok3[i])ok[i]=true; } if(cnt>=n)break; } rep(i, n){ int a, b, c, d; tie(a, b, c, d)=s[i]; cout << a << " " << b << " " << c << " " << d << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <iomanip> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <sstream> #include <tuple> #include <vector> using namespace std; using namespace chrono; #ifdef DEBUG #define LOCAL_INPUT_FILE #else //~ #define USE_FILE_IO #endif #ifdef USE_FILE_IO #define INPUT_FILE "input.txt" #define OUTPUT_FILE "output.txt" #define cin ____cin #define cout ____cout ifstream cin(INPUT_FILE); ofstream cout(OUTPUT_FILE); #else #ifdef LOCAL_INPUT_FILE #define cin ____cin ifstream cin("input.txt"); #endif #endif const int infinity = (int)1e9 + 42; const int64_t llInfinity = (int64_t)1e18 + 256; const int module = (int)1e9 + 7; const long double eps = 1e-8; mt19937_64 randGen(system_clock().now().time_since_epoch().count()); inline void raiseError(string errorCode) { cerr << "Error : " << errorCode << endl; exit(42); } inline bool check(vector< pair<int, int> > trans) { int n = trans.size(); for (int beg = 0; beg < n; ++beg) { vector<int> d(n, -1); d[beg] = 0; queue<int> q; q.push(beg); while (!q.empty()) { int v = q.front(); q.pop(); for (int to : {trans[v].first, trans[v].second}) { if (d[to] < 0) { q.push(to); d[to] = d[v] + 1; } } } for (int i : d) { if (i > 10 || i < 0) { return false; } } } return true; } inline vector< pair<int, int> > make(int n) { vector< pair<int, int> > trans(n); for (int i = 0; i < n; ++i) { trans[i].first = (i*2) % n; trans[i].second = (i*2+1) % n; } return trans; } signed main() { #ifndef USE_FILE_IO ios_base::sync_with_stdio(false); #endif int n; cin >> n; for (auto [a1, a2] : make(n)) { cout << a1+1 << " " << a2+1 << endl; } //~ for (int i = 1; i <= 1000; ++i) { //~ if (i % 50 == 0) { //~ cout << "Check " << i << endl; //~ } //~ auto trans = make(i); //~ if (!check(trans)) { //~ cout << "bad at " << i << endl; //~ } //~ } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ long long X,Y,A,B,T,K=0; cin >> X >> Y >> A >> B; T=X; while(true){ if(T>=Y/A){ break; } if(T*A>=Y){ break; } if(T*A<T+B){ K++; T*=A; } else{ break; } } long long add=(Y-1-T)/B; cout << K+add << endl; }
#include<bits/stdc++.h> using namespace std; /***typededfs***/ typedef long long ll; typedef long double ldb; /***defines***/ #define M1 1000000007 #define M2 998244353 #define F first #define S second #define pb push_back #define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define forz(i,n) for(int i=0;i<(n);i++) #define fore(i,a,b) for(int i=(a);i<=(b);i++) #define rforz(i,n) for(int i=(n-1);i>=0;i--) #define rfore(i,a,b) for(int i=(a);i>=(b);i--) #define test int T; cin>>T; while(T--) #define all(v) v.begin(),v.end() #define mm(a,b) ((a%M1)*(b%M1))%M1 #define ma(a,b) ((a%M1)+(b%M1))%M1 void io(){ FASTIO; #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif } int main(){ io(); ll x,y,a,b; cin>>x>>y>>a>>b; ll cnt{}; while(x<=(b-1)/a&&x*a<y){ x*=a; cnt++; } if(y-x-1>=0) cnt+=(y-x-1)/b; cout<<cnt<<'\n'; }
#include<bits/stdc++.h> using namespace std; const int N=1e6+10,M=1e3+5; int n,m,par[N],sum[M][M],add[M][M],res; char s[M][M]; bool vis[N]; set<int>x,y; int find(int x){ return par[x]==x?x:par[x]=find(par[x]); } int f(int x,int y){ return x*1000+y; } void un(int x,int y){ x=find(x),y=find(y); if(x==y)return; if(x>y)swap(x,y); par[y]=x; } int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;++i){ scanf("%s",s[i]); for(int j=0;j<m;++j){ int v=f(i,j); par[v]=v; } } s[0][0]=s[n-1][0]=s[0][m-1]=s[n-1][m-1]='#'; for(int i=0;i<n;++i){ for(int j=0;j<m;++j){ if(s[i][j]=='#'){ add[i][0]++;add[i][m]--; add[i+1][0]--;add[i+1][m]++; add[0][j]++;add[n][j]--; add[0][j+1]--;add[n][j+1]++; for(int k=i+1;k<n;++k)if(s[k][j]=='#'){un(f(i,j),f(k,j));break;} for(int k=j+1;k<m;++k)if(s[i][k]=='#'){un(f(i,j),f(i,k));break;} } } } for(int i=0;i<n;++i){ for(int j=0;j<m;++j){ if(i-1>=0)sum[i][j]+=sum[i-1][j]; if(j-1>=0)sum[i][j]+=sum[i][j-1]; if(i-1>=0 && j-1>=0)sum[i][j]-=sum[i-1][j-1]; sum[i][j]+=add[i][j]; //printf("i:%d j:%d v:%d\n",i,j,sum[i][j]); if(!sum[i][j]){ x.insert(i); y.insert(j); } int v=f(i,j); if(s[i][j]=='#' && par[v]==v)res++; } } printf("%d\n",res-1+min(x.size(),y.size())); return 0; }
// This code wrote by chtholly_micromaker(MicroMaker) #include <bits/stdc++.h> #define reg register #define int long long #define ALL(x) (x).begin(),(x).end() #define mem(x,y) memset(x,y,sizeof x) #define sz(x) (int)(x).size() #define ln std::puts("") #define lsp std::putchar(32) #define pb push_back #define MP std::make_pair #define dbg(x) std::cerr<<__func__<<"\tLine:"<<__LINE__<<' '<<#x<<": "<<x<<"\n" #define dprintf(x...) std::fprintf(stderr,x) #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define per(i,b,a) for(int i=(b);i>=(a);--i) const int MaxN=1050; template <class t> inline void read(t &s){s=0; reg int f=1;reg char c=getchar();while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c))s=(s<<3)+(s<<1)+(c^48),c=getchar();s*=f;return;} template<class t,class ...A> inline void read(t &x,A &...a){read(x);read(a...);} template <class t> inline void write(t x){if(x<0)putchar('-'),x=-x; int buf[21],top=0;while(x)buf[++top]=x%10,x/=10;if(!top)buf[++top]=0; while(top)putchar(buf[top--]^'0');return;} inline void setIn(std::string s){freopen(s.c_str(),"r",stdin);return;} inline void setOut(std::string s){freopen(s.c_str(),"w",stdout);return;} inline void setIO(std::string s=""){setIn(s+".in");setOut(s+".out");return;} template <class t>inline bool checkmin(t&x,t y){if(x>y){x=y;return 1;}return 0;} template <class t>inline bool checkmax(t&x,t y){if(x<y){x=y;return 1;}return 0;} inline int lowbit(int x){return x&(-x);} char s[MaxN][MaxN]; int f[MaxN<<1]; inline int getf(int x) { return f[x]==x?x:f[x]=getf(f[x]); } inline void merge(int x,int y) { reg int a=getf(x),b=getf(y); if(a!=b) f[b]=a; } signed main(void) { int n,m;read(n,m); for(int i=1;i<=n;++i) std::scanf("%s",s[i]+1); s[1][1]=s[n][1]=s[1][m]=s[n][m]='#'; for(int i=1;i<=n+m;++i) f[i]=i; for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(s[i][j]=='#') merge(i,j+n); std::unordered_map<int,int> r,c; for(int i=1;i<=n;++i) r[getf(i)]; for(int i=n+1;i<=n+m;++i) c[getf(i)]; write(std::min((int)r.size(),(int)c.size())-1),ln; return 0; } /* * Check List: * 1. Input / Output File (OI) * 2. long long * 3. Special Test such as n=1 * 4. Array Size * 5. Memory Limit (OI) int is 4 and longlong is 8 * 6. Mod (a*b%p*c%p not a*b*c%p , (a-b+p)%p not a-b ) * 7. Name ( int k; for(int k...)) * 8. more tests , (T=2 .. more) * 9. blank \n after a case */