code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include <bits/stdc++.h> #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=a;i<=(ll)(b);i++) #define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--) #define ALL(x) (x).begin(),(x).end() #define RALL(x) (x).rbegin(),(x).rend() #define SIZE(x) ((ll)(x).size()) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) using namespace std; 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; } typedef long long ll; const ll LINF = INT64_MAX; const int IINF = INT32_MAX; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> l(n, IINF), r(n, -IINF); REP(i, n){ int x, c; cin >> x >> c; --c; chmin(l[c], x); chmax(r[c], x); } vector<P> d; d.emplace_back(0, 0); REP(i, n) if(l[i] != IINF){ d.emplace_back(l[i], r[i]); } d.emplace_back(0, 0); vector<ll> dp(2); FOR(i, 1, SIZE(d)-1){ vector<ll> p(2, LINF); swap(p, dp); int L = d[i].first, R = d[i].second; REP(j, 2){ int x = j ? d[i-1].second : d[i-1].first; chmin(dp[0], p[j] + abs(x-R) + (R-L)); chmin(dp[1], p[j] + abs(x-L) + (R-L)); } } cout << dp[0] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxl=3e5+10; const ll inf=1ll<<60; int n,m,k,cnt,tot,cas,ans; struct node { int x,c; }a[maxl]; int l[maxl],r[maxl]; ll dp[maxl][2]; bool vis[maxl]; char s[maxl]; inline bool cmp(const node &a,const node &b) { return a.x<b.x; } inline void prework() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].x,&a[i].c); if(l[a[i].c]==0) l[a[i].c]=r[a[i].c]=a[i].x; else l[a[i].c]=min(l[a[i].c],a[i].x), r[a[i].c]=max(r[a[i].c],a[i].x); } } inline void chmi(ll &x,ll y) { x=min(x,y); } inline void mainwork() { for(int i=1;i<=n;i++) if(l[i]==0) { l[i]=l[i-1];dp[i][0]=dp[i-1][0]; r[i]=r[i-1];dp[i][1]=dp[i-1][1]; } else { dp[i][0]=dp[i][1]=inf; chmi(dp[i][0],dp[i-1][0]+abs(r[i]-l[i-1])+r[i]-l[i]); chmi(dp[i][0],dp[i-1][1]+abs(r[i]-r[i-1])+r[i]-l[i]); chmi(dp[i][1],dp[i-1][0]+abs(l[i]-l[i-1])+r[i]-l[i]); chmi(dp[i][1],dp[i-1][1]+abs(l[i]-r[i-1])+r[i]-l[i]); } } inline void print() { ll ans=min(dp[n][0]+abs(l[n]),dp[n][1]+abs(r[n])); printf("%lld",ans); } int main() { int t=1; //scanf("%d",&t); for(cas=1;cas<=t;cas++) { prework(); mainwork(); print(); } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ long n, a[100000], i; cin >> n; long double dn = n, x2 = 2; for(i = 0; i < n; i++) cin >> a[i]; if(n == 1) cout << fixed << setprecision(15) << a[0] / x2 << endl; else if(n == 2) cout << fixed << setprecision(15) << max(a[0], a[1]) / x2 << endl; else{ sort(a, a + n); long hsum = 0; x2 = a[int(n / 2)] / x2; for(i = n / 2; i < n; i++) hsum += a[i]; if(n % 2 == 0){ cout << fixed << setprecision(15) << hsum / dn << endl; } else{ cout << fixed << setprecision(15) << x2 + (hsum - int((n + 1)/ 2) * a[int(n / 2)]) / dn << endl; } } return 0; }
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <bits/stdc++.h> using namespace __gnu_pbds; using namespace std; using ll = long long; using ld = long double; typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define mp make_pair int MOD = 1e9 + 7; int mul(int a, int b) { return (1LL * a * b) % MOD; } int add(int a, int b) { int s = (a+b); if (s>=MOD) s-=MOD; return s; } int sub(int a, int b) { int s = (a+MOD-b); if (s>=MOD) s-=MOD; return s; } int po(int a, ll deg) { if (deg==0) return 1; if (deg%2==1) return mul(a, po(a, deg-1)); int t = po(a, deg/2); return mul(t, t); } int inv(int n) { return po(n, MOD-2); } mt19937 rnd(time(0)); /* const int LIM = 1000; vector<int> facs(LIM), invfacs(LIM); void init() { facs[0] = 1; for (int i = 1; i<LIM; i++) facs[i] = mul(facs[i-1], i); invfacs[LIM-1] = inv(facs[LIM-1]); for (int i = LIM-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1); } int C(int n, int k) { if (n<k) return 0; if (n<0 || k<0) return 0; return mul(facs[n], mul(invfacs[k], invfacs[n-k])); }*/ /*struct DSU { vector<int> sz; vector<int> parent; void make_set(int v) { parent[v] = v; sz[v] = 1; } int find_set(int v) { if (v == parent[v]) return v; return find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; } } DSU (int n) { parent.resize(n); sz.resize(n); for (int i = 0; i<n; i++) make_set(i); } };*/ int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int n; cin>>n; vector<int> a(n); for (int i = 0; i<n; i++) cin>>a[i]; sort(a.begin(), a.end()); ld ans = 1e18; ld cur = 0; ld tmp = 0; for (auto it: a) tmp+=it; ans = min(ans, tmp); for (int i = 0; i<n; i++) { ld cur1 = (ld)a[i]/(ld)(2); tmp+=(cur1-cur)*n; tmp-=2*(cur1-cur)*(n-i); cur = cur1; ans = min(ans, tmp); } cout<<setprecision(10)<<fixed<<ans/(ld)(n); }
#include<bits/stdc++.h> #define lint long long #define st first #define nd second #define INF 0x3f3f3f3f using namespace std; struct eq{ long double a, b; // a*f0 + b eq(): a(0), b(0){} eq(long double a, long double b): a(a), b(b){} eq operator+(const eq &oth){ return eq(a + oth.a, b + oth.b); } eq operator-(const eq &oth){ return eq(a - oth.a, b - oth.b); } eq operator*(long double f){ return eq(a*f, b*f); } }; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, k; cin>>n>>m>>k; vector<int> a(k); for(int &x:a) cin>>x; vector<eq> dp(n+m+1); vector<eq> suf(n+m+1); for(int i = n-1; i>=0; i--){ suf[i] = suf[i+1]; if(binary_search(a.begin(), a.end(), i)){ dp[i] = {1, 0}; } else { dp[i] = (suf[i+1] - suf[i+m+1]) * (1.0/m); dp[i].b += 1.0; } suf[i] = suf[i] + dp[i]; } if(abs(1.0 - dp[0].a) < 1e-6) cout<<"-1\n"; else cout<<fixed<<setprecision(4)<<dp[0].b/(1.0 - dp[0].a)<<"\n"; return 0; }
#include <bits/stdc++.h> #if MYDEBUG #include "lib/cp_debug.hpp" #else #define DBG(...) ; #endif #if __cplusplus <= 201402L template <typename T> T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); } template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } #endif using LL = long long; constexpr LL LINF = 334ll << 53; constexpr int INF = 15 << 26; constexpr LL MOD = 1E9 + 7; namespace Problem { using namespace std; struct RangeSum { using T = double; T operator()(const T &a, const T &b) { return a + b; } static constexpr T id() { return T(0); }; }; template <typename M> struct SegmentTree { using Val = typename M::T; M calc; vector<Val> data; int n; explicit SegmentTree(int size) { n = 1; while (n < size) n <<= 1; data = vector<Val>(n << 1, calc.id()); } explicit SegmentTree(vector<Val> &v) { n = 1; while (n < v.size()) n <<= 1; data = vector<Val>(n << 1); copy(v.begin(), v.end(), data.begin() + n); for (int i = n + (int)v.size(); i < 2 * n; i++) data[i] = calc.id(); for (int i = n - 1; i > 0; --i) { data[i] = calc(data[i * 2], data[i * 2 + 1]); } } void update(int i, Val v) { int k = i + n; data[k] = v; for (k >>= 1; k >= 1; k >>= 1) data[k] = calc(data[k * 2], data[k * 2 + 1]); } Val query(int l, int r) { //get[a,b) Val resl = calc.id(), resr = calc.id(); for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) resl = calc(resl, data[l++]); if (r & 1) resr = calc(data[r - 1], resr); } return calc(resl, resr); } }; class Solver2 { public: int n, m; vector<int> is_return; SegmentTree<RangeSum> a_0, a_x; Solver2(LL n, LL m) : n(n), m(m), is_return(n + 1), a_0(n + 1), a_x(n + 1){}; void solve() { int k; cin >> k; for (int i = 0; i < k; ++i) { int tmp; cin >> tmp; is_return[tmp] = 1; } for (int i = 0, j = 0; i <= n; ++i) { if (is_return[i]) { j++; if (j == m) { cout << -1 << endl; return; } } else { j = 0; } } a_0.update(n, 0); a_x.update(n, 0); for (int i = n - 1; i > 0; --i) { if (is_return[i]) { a_0.update(i, 0); a_x.update(i, 1); } else { double sum_0 = a_0.query(i + 1, min(i + m, n) + 1); double sum_x = a_x.query(i + 1, min(i + m, n) + 1); a_0.update(i, sum_0 / m + 1.0); a_x.update(i, sum_x / m); if (i > 99900) DBG(i, m - (n - i), sum_0 / m + 1.0, sum_x / m) } } double sum_0 = a_0.query(1, min(m, n) + 1); double sum_x = a_x.query(1, min(m, n) + 1); DBG(sum_0 / m + 1.0, sum_x / m) double ans = (sum_0 / m + 1.0) / (1.0 - sum_x / m); cout << ans << endl; } }; } // namespace Problem int main() { std::cin.tie(0); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(12); long long n = 0, m; std::cin >> n >> m; Problem::Solver2 sol(n, m); sol.solve(); return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vll = vector<long long>; using vb = vector<bool>; using vd = vector<double>; using vc = vector<char>; using vvi = vector<vector<int>>; using vvll = vector<vector<long long>>; using vvc = vector<vector<char>>; using pii = pair<int,int>; using pll = pair<ll,ll>; template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; template<typename T> using graph = vector<vector<pair<T, int>>>;// pair of weight and vertex void my_answer(){ string s; cin >> s; deque<char> t; int turn = 1; for(const auto c: s){ if(c == 'R'){ turn *= -1; continue; } if(turn == 1){ if(!t.size()) t.emplace_back(c); else if(t.back() != c) t.emplace_back(c); else t.pop_back(); }else{ if(!t.size()) t.emplace_front(c); else if(t.front() != c) t.emplace_front(c); else t.pop_front(); } } if(turn == 1){ for(auto itr = t.begin(); itr != t.end(); ++itr){ cout << *itr; } }else{ for(auto itr = t.rbegin(); itr != t.rend(); ++itr){ cout << *itr; } } cout << endl; } void best_answer(){ } int main(){ my_answer(); // best_answer(); }
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <stack> #include <cstring> #include <set> #include <utility> #include <iostream> #include <iomanip> #include <list> #include <queue> #include <algorithm> #include <cmath> //#include <boost/multiprecision/cpp_int.hpp> #include <unordered_set> #include <bitset> #include <cassert> #include <cstdio> #include <numeric> #include <complex> #define FOR(i,a,b) for(ll i=(a);i<(ll)(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define SUM(v) accumulate(ALL(v),0ll) using ll = long long; const ll INF=100; const ll eps=1; const double pi=3.14159265359; const ll mod=1000000+7; using namespace std; int main() { string s; cin>>s; int len=s.size(); string t; bool ok=true; for(int i=0;i<len;i++) { if(s[i]=='R') { // reverse(t.begin(),t.end()); if(ok==true)ok=false; else ok=true; } else { if(ok==true) { t+=s[i]; if(t.size()>=2) { if(t[t.size()-2]==t[t.size()-1])t.erase(t.size()-2,2); } } else { t=s[i]+t; if(t.size()>=1) { if(t[1]==t[0])t.erase(0,2); } } } } if(ok==false)reverse(t.begin(),t.end()); cout<<t<<endl; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; char getch() { static char buf[1 << 14], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 14, stdin), p1 == p2) ? EOF : *p1++; } void read(int &x) { x = 0; char c = getch(); while(c < '0' || c > '9') c = getch(); while(c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getch(); } } int n, m, cnt; int head[100001], vis[100001]; struct node { int v, w, next; }e[500003]; void add(int u, int v, int w) { e[++cnt].v = v; e[cnt].w = w; e[cnt].next = head[u]; head[u] = cnt; } void dfs(int u) { for(int i = head[u]; i; i = e[i].next) { int v = e[i].v, w = e[i].w; if(vis[v]) continue; if(vis[u] == w) vis[v] = w % n + 1; else vis[v] = w; dfs(v); } } int main() { read(n); read(m); int x, y, z; for(int i = 1; i <= m; ++i) { read(x); read(y); read(z); add(x, y, z); add(y, x, z); } vis[1] = 1; dfs(1); int flag = 0; for(int i = 1; i <= n; ++i) if(!vis[i]) { flag = 1; break; } if(flag) cout << "No" << endl; else { for(int i = 1; i <= n; ++i) printf("%d\n", vis[i]); } return 0; }
#include <bits/stdc++.h> #define rep(i, s, n) for (int i = (s); i < (int)(n); i++) #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<x<<endl using namespace std; using ll = long long; const ll MOD = 1e9+7; const ll LINF = LLONG_MAX; const int INF = INT_MAX; const double PI=acos(-1); template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int main(){ char s,t; cin >> s >> t; if(s == 'Y'){ if(t == 'a'){ cout << 'A' << endl; }else if(t == 'b'){ cout << 'B' << endl; }else{ cout << 'C' << endl; } }else{ cout << t << endl; } }
/* author: blue_matrix */ #include <bits/stdc++.h> using namespace std; #define n_l '\n' #define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)), __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); } #define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long a,b,c; cin>>a>>b>>c; if(pow(a,2) + pow(b,2)<pow(c,2)) cout<< "Yes"; else cout<< "No"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int64_t a, b, c; cin >> a >> b >> c; cout << ((a * a + b * b < c * c) ? "Yes" : "No") << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string(string(s)); } string to_string(bool b) { return to_string(int(b)); } string to_string(vector<bool>::reference b) { return to_string(int(b)); } string to_string(char b) { return "'" + string(1, b) + "'"; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { string res = "{"; for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x); return res + "}"; } void debug() { cerr << endl; } template <typename Head, typename... Tail> void debug(Head H, Tail... T) { cerr << " " << to_string(H); debug(T...); } #define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__) #else #define db(...) 42 #endif typedef long long ll; typedef long double ld; const int MOD = 998244353; struct Mint { int val; Mint() { val = 0; } Mint(ll x) { val = (-MOD <= x && x < MOD) ? x : x % MOD; if (val < 0) val += MOD; } template <typename U> explicit operator U() const { return (U)val; } friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; } friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); } friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; } Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; } Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; } Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; } friend Mint modex(Mint a, ll p) { assert(p >= 0); Mint ans = 1; for (; p; p >>= 1, a *= a) if (p & 1) ans *= a; return ans; } Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); } Mint& operator++() { return *this += 1; } Mint& operator--() { return *this -= 1; } Mint operator++(int) { Mint result(*this); *this += 1; return result; } Mint operator--(int) { Mint result(*this); *this -= 1; return result; } Mint operator-() const { return Mint(-val); } friend Mint operator+(Mint a, const Mint& b) { return a += b; } friend Mint operator-(Mint a, const Mint& b) { return a -= b; } friend Mint operator*(Mint a, const Mint& b) { return a *= b; } friend Mint operator/(Mint a, const Mint& b) { return a /= b; } friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; } friend string to_string(const Mint& b) { return to_string(b.val); } }; vector<Mint> fac(1, 1), invfac(1, 1); Mint binom(int n, int k) { if (k < 0 || k > n) return 0; while (fac.size() <= n) { fac.push_back(fac.back() * fac.size()); invfac.push_back(1 / fac.back()); } return fac[n] * invfac[k] * invfac[n - k]; } vector<int> pfac; // pfac[i] = largest prime factor of i (pfac[i <= 1] = 0) void genPrimes(int n) { pfac = vector<int>(n + 1); for (int i = 2; i <= n; ++i) pfac[i] = i; for (int i = 2; i <= n; ++i) if (pfac[i] == i) for (int j = i + i; j <= n; j += i) pfac[j] = i; } /* Prime factors (with precomputing). */ vector<pair<int, int>> factorize(int x) { vector<pair<int, int>> factors; while (x > 1) { int p = pfac[x]; int cnt = 0; while (pfac[x] == p) { ++cnt; x /= p; } factors.emplace_back(p, cnt); } return factors; } int main() { int n, m; scanf("%d%d", &n, &m); binom(n + 20, 0); genPrimes(m); Mint ans = 0; for (int i = 1; i <= m; ++i) { auto ps = factorize(i); Mint prod = 1; for (auto& p : ps) { prod *= binom(n - 1 + p.second, p.second); } ans += prod; } printf("%d\n", ans.val); }
#include <algorithm> #include <iostream> #include <iomanip> #include <numeric> #include <cassert> #include <vector> #include <random> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> //#include <atcoder/all> #define syosu(x) fixed<<setprecision(x) using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef pair<double,double> pdd; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<double> vd; typedef vector<vd> vvd; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<P> vp; typedef vector<vp> vvp; typedef vector<pll> vpll; typedef pair<int,P> pip; typedef vector<pip> vip; const int inf=1<<30; const ll INF=1ll<<60; const double pi=acos(-1); const double eps=1e-8; const ll mod=998244353; const int dx[8]={-1,0,1,0,1,1,-1,-1},dy[8]={0,1,0,-1,1,-1,1,-1}; //using namespace atcoder; ll Pow(ll n,ll p){ ll r=1; for(;p>0;p>>=1){ if(p&1) r=(r*n)%mod; n=(n*n)%mod; } return r; } const int M=1000005; ll F[M]; void Init(){ F[0]=1; for(int i=1;i<M;i++) F[i]=F[i-1]*i%mod; } ll Div(ll n,ll m){ return n*Pow(m,mod-2)%mod; } ll nCk(ll n,ll k){ return Div(F[n],F[n-k]*F[k]%mod); } int n,m; int main(){ Init(); cin>>n>>m; ll res=1; for(int i=2;i<=m;i++){ vi a; int x=i; for(int j=2;j*j<=x;j++) if(x%j==0){ a.push_back(j); while(x%j==0) x/=j; } if(x>1) a.push_back(x); ll tmp=1; for(auto j:a){ x=i; int t=0; while(x%j==0) x/=j,t++; (tmp*=nCk(n+t-1,n-1))%=mod; } (res+=tmp)%=mod; } cout<<res<<endl; }
/** * author: tomo0608 * created: 18.02.2021 21:47:19 **/ #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; typedef pair<int,int> pii; typedef pair<long long, long long> pll; #define all(x) x.begin(),x.end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) #define unique(a) a.erase(unique(a.begin(),a.end()),a.end()) template<class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T> >; template<class T, class U> inline bool chmax(T &a, const U &b) { if (a<b) { a=b; return 1; } return 0; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a>b) { a=b; return 1; } return 0; } template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; } template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template<typename T> ostream& operator << (ostream& os, set<T>& set_var) {os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {os << *itr;++itr;if(itr != set_var.end()) os << ", ";itr--;}os << "}";return os;} template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) {os << "{";for(auto itr = map_var.begin(); itr != map_var.end(); itr++) {os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--;}os << "}";return os;} template<class T> inline int count_between(vector<T> &a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r) #define pb push_back #define eb emplace_back #define elif else if #define mp make_pair #define bit(n, k) ((n >> k) & 1) /*nのk bit目*/ template<typename T> T gcd(T x, T y){if(x%y == 0)return y;return gcd(y, x%y);} template<typename T> T gcd(vector<T> a){T res = a[0];for(auto &x: a)res = gcd(res, x);return res;} template <typename T>T mypow(T x, ll n){T ret = 1;while(n > 0){if(n & 1)(ret *= x);(x *= x);n >>= 1;}return ret;} #define endl '\n' int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1}; // Primality test class Prime { public: const int N; vector<int> min_prime; vector<int> primes; Prime(int size): N(size), min_prime(N+1){ rep(i,N+1)min_prime[i] = i; min_prime[0] = -1; min_prime[1] = -1; for(int i = 2;i<=N;i++){ if(min_prime[i]!=i)continue; primes.push_back(i); int tmp = 2*i; while(tmp <= N){ if(min_prime[tmp]==tmp)min_prime[tmp] = i; tmp += i; } } } bool check(int x){ return min_prime[x] == x;} }; void solve(){ ll n,x;cin >> n >> x; V<ll> a(n);cin >> a; map<pll, ll> memo; auto dp = [&](auto &&dp, ll now, ll X)->ll{ if(now == n-1)return 1; if(memo.count({now, X}))return memo[{now, X}]; ll nmax = a[now+1]/a[now]; memo[{now, X}] = dp(dp, now+1, X/nmax) + (X%nmax != 0)*dp(dp, now+1, X/nmax + 1); return memo[{now, X}]; }; cout << dp(dp, 0, x) << endl; //cout << memo << endl; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << setprecision(20); int codeforces = 1; //cin >> codeforces; while(codeforces--){ solve(); } return 0; }
#include <algorithm> #include <iostream> #include <limits> #include <unordered_map> #include <vector> using namespace std; int main() { int N; cin >> N; int numDog = N * 2; unordered_map<char, vector<long long>> a; a['R']; a['B']; a['G']; for(int i = 0; i < numDog; i++) { long long aa; char c; cin >> aa >> c; a[c].push_back(aa); } // もしどの色も偶数なら0が最小 bool allEven = true; char evenColor; for(auto &x : a) { if(x.second.size() % 2 == 1) { allEven = false; } else { evenColor = x.first; } } if(allEven) { cout << 0 << endl; return 0; } // そうでないなら、必ず偶数、奇数、奇数の組合せ // 奇数同士をペアにする場合と、偶数から2匹もらう場合がある for(auto &x : a) { sort(x.second.begin(), x.second.end()); } vector<vector<long long>*> odd; vector<long long> *even = nullptr; for(auto &x : a) { if(x.second.size() % 2 == 0) { even = &a[x.first]; } else { odd.push_back(&a[x.first]); } } // 奇数同士 int i = 0, j = 0; long long result = 1e17; while(true) { if(i >= odd[0]->size() || j >= odd[1]->size()) { break; } auto x = odd[0]->at(i); auto y = odd[1]->at(j); result = min(result, abs(x - y)); if(x < y) { i++; } else { j++; } } // 偶数から借りる i = j = 0; int used; long long t1 = 1e17; while(true) { if(i >= odd[0]->size() || j >= even->size()) { break; } auto x = odd[0]->at(i); auto y = even->at(j); if(abs(x - y) < t1) { used = j; } t1 = min(t1, abs(x - y)); if(x < y) { i++; } else { j++; } } i = j = 0; long long t2 = 1e17; while(true) { if(i >= odd[1]->size() || j >= even->size()) { break; } auto x = odd[1]->at(i); auto y = even->at(j); if(j != used) { t2 = min(t2, abs(x - y)); } if(x < y) { i++; } else { j++; } } result = min(result, t1 + t2); cout << result << endl; return 0; }
#include <bits/stdc++.h> #define maxn 100005 #define INF 9223372036854775807LL using namespace std; using Road = tuple<int, int, int>; using LL = long long; using pli = pair<LL, int>; vector<Road> G[maxn]; LL dist[maxn]; int main() { int n, m; scanf("%d%d", &n, &m); while(m--) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); if(--a == --b) continue; G[a].emplace_back(b, c, d); G[b].emplace_back(a, c, d); } dist[0] = 0LL; for(int i=1; i<n; i++) dist[i] = INF; priority_queue<pli, vector<pli>, greater<pli>> q; q.emplace(0LL, 0); while(!q.empty()) { auto [t, u] = q.top(); q.pop(); if(dist[u] != t) continue; for(auto [v, c, d]: G[u]) { LL t2 = sqrt((long double) d) - 0.5; if(t2 < t) t2 = t; t2 += LL(c) + LL(d) / (t2 + 1LL); if(t2 < dist[v]) q.emplace(dist[v] = t2, v); } } if(dist[n - 1] == INF) puts("-1"); else printf("%lld\n", dist[n - 1]); return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a,x) for (auto& a : x) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define ins insert 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; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1000000007; const char nl = '\n'; const int MX = 100001; //check the limits, dummy void solve() { int N, S, D; cin >> N >> S >> D; F0R(i, N) { int X, Y; cin >> X >> Y; if (X < S && Y > D) { cout << "Yes" << nl; return; } } cout << "No" << nl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while(T--) { solve(); } return 0; } // read the question correctly (ll vs int) // template by bqi343
//SKD // 16 32 108 #include<bits/stdc++.h> #define ll long long int #define pp push_back #define pi 2*acos(0.0) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout<<fixed; cout<<setprecision(9); double m=0,p=0,l,q=0,r=0,t; int n,k,i,j; cin>>n;ll a[n],b[n];k=0; for(i=0; i<n; i++)cin>>a[i]>>b[i]; for(i=0; i<n-1; i++) { for(j=i+1; j<n; j++) { l=b[j];q=b[i];t=a[i];r=a[j]; p=(l-q)/(t-r); if(p>=-1 && p<=1)k++; } } cout<<k; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;} template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);} template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;} const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1}; typedef pair<ll,ll> P; void Main() { ll n; R n; P a[n]; rep(i,n) cin >> a[i].F >> a[i].S; ll ans=0; rep(i,n) { REP(j,i+1,n) { ll x=abs(a[i].F-a[j].F),y=abs(a[i].S-a[j].S); if(x>=y) ans++; } } pr(ans); } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++) #define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--) #define all(v) v.begin(), v.end() template<class in_chmax> void chmax(in_chmax &x, in_chmax y) {x = max(x,y);} template<class in_chmin> void chmin(in_chmin &x, in_chmin y) {x = min(x,y);} void Yes() {cout << "Yes" << endl; exit(0);} void No() {cout << "No" << endl; exit(0);} template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);} template<class in_vec_cout> void vec_cout(vector<in_vec_cout> vec) { for (in_vec_cout res : vec) {cout << res << " ";} cout << endl; } const ll inf = 1e18; const ll mod = 1e9 + 7; ll H, W; vector<string> A; bool is_Takahashi(ll i, ll j) {return (i+j)%2==0;} void init() { cin >> H >> W; A.resize(H); rep(i,H) cin >> A[i]; } int main() { init(); vector<vector<ll>> dp(H,vector<ll>(W)); rep(i,H) rep(j,W) { if (is_Takahashi(i,j)) dp[i][j] = -inf; else dp[i][j] = inf; } dp[H-1][W-1] = 0; for (ll i=H-1; i>=0; i--) for (ll j=W-1; j>=0; j--) { ll add = 0; if (is_Takahashi(i,j)) { if (i+1<H) { if (A[i+1][j]=='+') add = 1; else add = -1; chmax(dp[i][j],dp[i+1][j]+add); } if (j+1<W) { if (A[i][j+1]=='+') add = 1; else add = -1; chmax(dp[i][j],dp[i][j+1]+add); } } else { if (i+1<H) { if (A[i+1][j]=='+') add = -1; else add = 1; chmin(dp[i][j],dp[i+1][j]+add); } if (j+1<W) { if (A[i][j+1]=='+') add = -1; else add = 1; chmin(dp[i][j],dp[i][j+1]+add); } } } ll x = dp[0][0]; if (x>0) Cout("Takahashi"); else if (x==0) Cout("Draw"); else Cout("Aoki"); }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define FOR(i,f,n) for(int i=f;i<n;i++) #define FORI(i,f,n) for(int i=f;i>=n;i--) #define sz(a) ((int)(a).size()) #define ff first #define ss second #define all(a) (a).begin(),(a).end() #define alli(a) (a).rbegin(),(a).rend() #define approx(a) fixed<<setprecision(a) #define trav(a,x) for(auto& a : x) typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<int,bool> pib; typedef pair<ll,bool> plb; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ld> vld; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<pii> vpii; typedef vector<pll> vpll; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; template <class T> void ckmin(T &a, const T &b) { a = min(a, b); } template <class T> void ckmax(T &a, const T &b) { a = max(a, b); } template <class T> void read(vector<T> &v); template <class F, class S> void read(pair<F, S> &p); template <class T> void read(T &x) {cin>>x;} template <class R, class... T> void read(R& r, T&... t){read(r); read(t...);}; template <class T> void read(vector<T> &v) {trav(x, v) read(x);} template <class F, class S> void read(pair<F, S> &p) {read(p.ff, p.ss);} template <class F, class S> void pr(const pair<F, S> &x); template <class T> void pr(const T &x) {cout<<x;} template <class R, class... T> void pr(const R& r, const T&... t) {pr(r); pr(t...);} template <class F, class S> void pr(const pair<F, S> &x) {pr("{", x.ff, ", ", x.ss, "}");} void ps() {pr("\n");} template <class T> void ps(const T &x) {pr(x); ps();} template <class T> void ps(vector<T> &v) {trav(x, v) pr(x, ' '); ps();} template <class F, class S> void ps(const pair<F, S> &x) {pr(x.ff, ' ', x.ss); ps();} template <class R, class... T> void ps(const R& r, const T &...t) {pr(r, ' '); ps(t...);} const int mod = 998244353; void solve(){ int n, k; read(n, k); vector<vl> dp(n+1, vl(2*n+1)); dp[1][1] = 1; FOR(i,2,n+1){ FORI(j,n,1){ dp[i][j] = (dp[i-1][j-1] + dp[i][2*j]) % mod; } } ps(dp[n][k]); } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t=1; //cin>>t; while(t--){ solve(); } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pll> vpll; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FOR0(n) for(int i=0;i<n;++i) #define FOR1(n) for(int i=1;i<=n;++i) ll gcd(ll a, ll b) { return a % b ? gcd(b, a % b) : b; } const ll mod = 1e9 + 7; const ll inf = 0x3f3f3f3f; ll qpow(ll a, ll b) { ll t = 1; while (b != 0) { if (b & 1)t = (t * a) % mod; a = (a * a) % mod; b >>= 1; }return t; } template <typename T> void re(T& a) { for (auto& t : a)cin >> t; } template <typename T> void pr(T& a) { for (auto& t : a)cout << t << " "; cout << endl; } void solve() { vi a(3); re(a); sort(a.begin(), a.end()); if (a[0] == a[1])cout << a[2]; else if (a[1] == a[2])cout << a[0]; else cout << 0; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; //cin >> T; while (T--) { solve(); } return 0; }
#include <iostream> #include <string> #include <vector> #include <cmath> #include <algorithm> #include <stack> #include <queue> #include <set> #include <map> using namespace std; typedef long long int ll; typedef pair<int, int> pii; #define rep(i, a, b) for (int i = a; i < (int)(b); 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; } int main() { int a, b, c; cin >> a >> b >> c; int ans; if(a==b) ans = c; else if(b==c) ans = a; else if(c==a) ans = b; else ans = 0; cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std ; #define vt vector #define all(c) (c).begin(), (c).end() #define sz(x) (int)(x).size() #define ll long long #define ld long double #define ar array #define inf() ifstream cin("d.txt") #define onf() ofstream cout("test.txt") #define mod 1000000007 #define pb push_back #define endl cout<<"\n"; #define prt1(a) cout<<a<<" " #define prt2(a,b) cout<<a<<" "<<b<<" " #define prt3(a,b,c) cout<<a<<" "<<b<<" "<<c<<" " #define in(a) cin>>a #define inn(a,b) cin>>a>>b #define innn(a,b,c) cin>>a>>b>>c #define rep(i,st,end) for(int i=st;i<end;i++) #define repv(i,st,end) for(int i=st;i>=end;i--) #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define TEST ll tc;cin>>tc;while(tc--) #define EACH(x, a) for (auto& x: a) template<class A> void read(vt<A>& v); template<class A, size_t S> void read(ar<A, S>& a); template<class T> void read(T& x) { cin >> x; } void read(double& d) { string t; read(t); d=stod(t); } void read(long double& d) { string t; read(t); d=stold(t); } template<class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } template<class A> void read(vt<A>& x) { EACH(a, x) read(a); } template<class A, size_t S> void read(array<A, S>& x) { EACH(a, x) read(a); } #define debug(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 <<"\n"; err(++it, args...); } void solve() { ll a,b,c,d; read(a,b,c,d); double x = ((a*d) + (b*c))/((b+d)*1.0); cout<<fixed <<setprecision(9)<<x; } int main() { clock_t start, end; start = clock(); fio; ll k = 1; ll tc = 1; //cin>>tc; while(tc--){ // cout<<"Case #"<<k<<": ";k++; solve(); endl; } double time_taken = double(end - start) / double(CLOCKS_PER_SEC); // debug(time_taken); }
#include <algorithm> #include <bitset> #include <cmath> #include <cctype> #include <fstream> #include <functional> #include <iostream> #include <iomanip> #include <limits> #include <map> #include <numeric> #include <queue> #include <stdio.h> #include <string> #include <sstream> #include <stack> #include <set> #include <vector> #define rep(i,a,b) for((i)=a;i<(int)(b);i++) const int AAA = 1000000007; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pairII; typedef vector<int> vecI; ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll powD(ll a, ll b) { ll ans = 1; a %= AAA; for (int i = 0; i < b; i++) { ans *= a; ans %= AAA; } return ans; } vecI input(int N) { int i; vecI A(N); rep(i, 0, N) { cin >> A[i]; } return A; } int main(int argc, char* argv[]) { int i, j; int N, M, K; double Sx, Sy, Gx, Gy; cin >> Sx >> Sy >> Gx >> Gy; double k = (-Gy - Sy) / (Gx - Sx); double ans = Sx - Sy / k; printf("%.12f", ans); return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <cassert> #include <functional> using ll = long long; using namespace std; template<typename A, typename B> bool chmin(A &a, const B b) { if (a <= b) return false; a = b; return true; } template<typename A, typename B> bool chmax(A &a, const B b) { if (a >= b) return false; a = b; return true; } #ifndef LOCAL #define debug(...) ; #else #define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl; template<typename T> ostream &operator<<(ostream &out, const vector<T> &v); template<typename T1, typename T2> ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template<typename T> ostream &operator<<(ostream &out, const vector<T> &v) { out << '{'; for (const T &item : v) out << item << ", "; out << "\b\b}"; return out; } void _tostr_rec(ostringstream &oss) { oss << "\b\b \b"; } template<typename Head, typename... Tail> void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) { oss << head << ", "; _tostr_rec(oss, forward<Tail>(tail)...); } template<typename... T> string _tostr(T &&...args) { ostringstream oss; int size = sizeof...(args); if (size > 1) oss << "{"; _tostr_rec(oss, forward<T>(args)...); if (size > 1) oss << "}"; return oss.str(); } #endif constexpr int mod = 1'000'000'007; //1e9+7(prime number) constexpr int INF = 1'000'000'000; //1e9 constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18 constexpr int SIZE = 200010; // {a, {b, c}} : a = gcd(p, q) = bp + cq pair<ll, pair<ll, ll>> ext_gcd(ll p, ll q) { if (q == 0) return {p, {1, 0}}; auto r = ext_gcd(q, p % q); return {r.first, {r.second.second, r.second.first - p / q * r.second.second}}; } /* 中国剰余定理 */ pair<ll, ll> CRT(ll a1, ll m1, ll a2, ll m2) { auto eg = ext_gcd(m1, m2); ll d = eg.first; if ((a2 - a1) % d) return {0, -1}; ll m = m1 / d * m2; ll r = ((a1 + m1 * ((__int128_t)(a2 - a1) / d * eg.second.first % (m2 / d))) % m + m) % m; return {r, m}; } void solve() { ll X, Y, P, Q; cin >> X >> Y >> P >> Q; ll ans = LLINF; for (int i = 0; i < Y; i++) { for (int j = 0; j < Q; j++) { auto r = CRT(X + i, (X + Y) * 2, P + j, P + Q); if (r.second != -1) { debug(X + i, r); chmin(ans, r.first); } } } if (ans == LLINF) puts("infinity"); else printf("%lld\n", ans); } int main() { int T; cin >> T; while (T--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; const long long mod=998244353,INF=1e18; #define ll long long #define line cout<<"\n"; #define flush cout<<endl; #define pll pair<ll,ll> #define x first #define y second #define p_all(arr) for(auto i:arr){cout<<i<<" ";}cout<<"\n"; ll power(ll u,ll v) { ll res=1; while(v) { if(v&1) { res=(res*u)%mod; } u=(u*u)%mod; v>>=1; } return res; } ll modInv(ll u) { return power(u,mod-2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); ll t=1; // cin>>t; for(ll test=1;test<=t;test++) { ll n,m; cin>>n>>m; if(m%2) { cout<<"0\n"; continue; } vector<ll> fac(5005),facInv(5005); fac[0]=facInv[0]=1; for(ll i=1;i<=5000;i++) { fac[i]=(i*fac[i-1])%mod; facInv[i]=modInv(fac[i]); } ll dp[2][5005]; memset(dp,0,sizeof(dp)); dp[1][0]=1; auto nCr=[&](ll u,ll v) { if(u<v) return 0LL; ll res=fac[u]; res=(res*facInv[v])%mod; res=(res*facInv[u-v])%mod; return res; }; ll set=0; for(int i=0;(1<<i)<=m;i++) { for(int j=0;j<=n;j+=2) { ll tot=(1<<i)*j; for(int k=0;(k+tot)<=m;k++) { dp[set][k+tot]=(dp[set][k+tot]+dp[1-set][k]*nCr(n,j))%mod; } } set^=1; for(int j=0;j<=n;j+=2) { for(int k=0;k<=m;k++) dp[set][k]=0; } } cout<<dp[1-set][m]<<"\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define sz(x) ((int)x.size()) #define all(x) x.begin(), x.end() typedef vector<int> vi; using ll = long long; typedef pair<int, int> pii; typedef unsigned long long ul; const int mn = 200005; const int mk = 302; const int mod = 998244353; int n, k; ll a[mn][mk]; ll s[mk]; ll powmod(ll a, ll b, ll P) { ll t = 1; for (; b; b >>= 1, a = a * a % P) if (b & 1) t = t * a % P; return t; } ll C[mk][mk]; inline void modp(ll &ans) { ans %= mod; if (ans < 0) ans += mod; } ll inv2 = 499122177; int main() { //cin.sync_with_stdio(0); #ifdef LOCAL freopen("../1.txt", "r", stdin); //freopen("../2.txt", "w", stdout); auto st = chrono::system_clock::now(); #endif C[0][0] = 1; for (int i = 1; i <= 300; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) { C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; } } scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { a[i][0] = 1; s[0] += 1; scanf("%lld", &a[i][1]); s[1] += a[i][1]; for (int j = 2; j <= k; j++) { a[i][j] = a[i][1] * a[i][j - 1] % mod; s[j] += a[i][j]; } } for (int i = 1; i <= k; i++) s[i] %= mod; for (int x = 1; x <= k; x++) { ll ans = 0; for (int i = 0; i <= x; i++) { ll sum = (s[i] * s[x - i] - s[x]) % mod * C[x][i] % mod; ans += sum; } ans %= mod; ans *= inv2; modp(ans); printf("%lld\n", ans); } #ifdef LOCAL auto ed = chrono::system_clock::now(); auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(ed - st); std::cerr << "cost time " << diff.count() << "ms." << std::endl; #endif return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep1(i, n) for (int i = 1; i <= (n); i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) #define rrep1(i, n) for (int i = n; i >= 1; i--) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define eb emplace_back #define fi first #define se second #define sz(x) (int)(x).size() template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; typedef long long int ll; typedef pair<int, int> P; // typedef modint1000000007 mint; void speedUpIO() { cin.tie(nullptr); ios::sync_with_stdio(false); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } /*--------------------------------------------------*/ const int INF = 1e9; const ll LINF = 1e18; const int MAX = 100010; void solve() { int h, w; cin >> h >> w; VV<int> G(h + 1, V<int>(w + 1)); rep(i, h) rep(j, w) { char c; cin >> c; G[i][j] = c == '+' ? 1 : -1; } VV<int> dp(h + 1, V<int>(w + 1, -INF)); rep(i, h) dp[i][w] = INF; rep(j, w) dp[h][j] = INF; dp[h - 1][w - 1] = 0; rrep(i, h) rrep(j, w) { if (i == h - 1 && j == w - 1) continue; chmax(dp[i][j], G[i + 1][j] - dp[i + 1][j]); chmax(dp[i][j], G[i][j + 1] - dp[i][j + 1]); } string ans = "Draw"; if (dp[0][0] > 0) ans = "Takahashi"; if (dp[0][0] < 0) ans = "Aoki"; cout << ans << "\n"; } int main() { speedUpIO(); int t = 1; // cin >> t; while (t--) { solve(); // cout << solve() << "\n"; // cout << (solve() ? "Yes" : "No") << "\n"; // cout << fixed << setprecision(15) << solve() << "\n"; } return 0; }
//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(),(x).end() const int N = 6666; const long long MOD = 998244353; int dp[N][N]; void solve() { int n, k; cin >> n >> k; dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = n + n; j >= 1; j--) { dp[i][j] = (dp[i - 1][j - 1] + (i >= j ? dp[i][j + j] : 0)) % MOD; } } cout << dp[n][k] << endl; } int main() { #ifdef QWERTY freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ios_base::sync_with_stdio(false); int TC = 1; // cin >> TC; while (TC--) solve(); return 0; }
#include <iostream> #include <stdio.h> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <iomanip> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <unordered_map> #include <bitset> #include <chrono> #include <random> #define rep(i,n) for(int i=0;i<n;i++) #define repn(i,n) for(int i=1;i<=n;i++) #define repr(e,x) for(auto& e:x) using namespace std; typedef long long ll; typedef long double ld; // typedef pair<int,int> P; // typedef pair<int,P> IP; // typedef pair<P,P> PP; double const PI=3.141592653589793; int const INF=1001001001; ll const LINF=1001001001001001001; ll const MOD=998244353; ll mpow(ll x,ll n){ if(n==0) return 1; else if(n%2) return x*mpow(x,n-1)%MOD; return mpow(x*x%MOD,n/2)%MOD; } ll N; ll A[200000]; int main(){ cin>>N; rep(i,N) cin>>A[i]; sort(A,A+N); vector<ll> sum(N+1,0); repn(i,N-1){ sum[i+1]=sum[i]+A[i]*mpow(2,i-1)%MOD; sum[i+1]%=MOD; } ll ans=0; repn(i,N){ ans+=A[i-1]*(sum[N]-sum[i])%MOD*mpow(mpow(2,i-1),MOD-2)%MOD; ans%=MOD; ans+=A[i-1]*A[i-1]; ans%=MOD; } if(ans<0) ans+=MOD; cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #define startt ios_base::sync_with_stdio(false);cin.tie(0); typedef unsigned long long ull; typedef long long ll; using namespace std; #define vint vector<int> #define all(v) v.begin(), v.end() #define sz(a) (int)a.size() #define MOD 1000000007 #define MOD2 998244353 #define MX 1000000000 #define nax 100005 #define MXL 1000000000000000000 #define PI 3.14159265 #define pb push_back #define pf push_front #define sc second #define fr first #define endl '\n' //delete if interactive #define ld long double #define mp make_pair void solve(){ int n;cin>>n; string a,b,c;cin>>a>>b>>c; int lim=2*n+1; int x = lim/3; for(int i=0;i<n;i++) cout<<"1"; for(int i=0;i<n;i++) cout<<"0"; cout<<"1\n"; } int main(){ int t=1;cin>>t; for(int tc=1;tc<=t;tc++){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int tc, n; string s[3]; int main(){ cin.tie(0)->sync_with_stdio(0); cin >> tc; while(tc--){ cin >> n; for(int i=0;i<3;i++) cin >> s[i]; for(int i=0;i<n;i++) cout << 1; for(int i=0;i<n;i++) cout << 0; cout << "1\n"; } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<ll(n);i++) #define REP(i,k,n) for(ll i=k;i<ll(n);i++) #define all(a) a.begin(),a.end() #define eb emplace_back #define lb(v,k) (lower_bound(all(v),k)-v.begin()) #define ub(v,k) (upper_bound(all(v),k)-v.begin()) #define fi first #define se second #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define UNIQUE(a) sort(all(a));a.erase(unique(all(a)),a.end()) #define decimal cout<<fixed<<setprecision(10) #define summon_tourist cin.tie(0);ios::sync_with_stdio(false) using ll=long long; using P=pair<ll,ll>; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; constexpr ll inf=1001001001001001; constexpr int INF=1001001001; constexpr int mod=1000000007; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} void YesNo(bool b){if(b)out("Yes");else out("No");} void yesno(bool b){if(b)out("yes");else out("no");} void YESNO(bool b){if(b)out("YES");else out("NO");} ll modpow(ll a,ll b){ll c=1;while(b>0){if(b&1){c=a*c%mod;}a=a*a%mod;b>>=1;}return c;} vi calc(ll x){vi v;while(x>0){v.eb(x%10);x/=10;}reverse(all(v));return v;} int main(){ ll n,x; cin>>n>>x; vi a(n); rep(i,n) cin>>a[i]; vi b; rep(i,n) if(a[i]!=x) b.eb(a[i]); outv(b); }
#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); #endif int n,x,i; cin>>n>>x; int ar[n]; vector<int>s; for(auto &x: ar)cin>>x; for(i=0;i<n;i++) { if(ar[i]!=x) s.push_back(ar[i]); } for(auto x:s)cout<<x<<" "; cout<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define dup(x,y) (((x)+(y)-1)/(y)) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef pair<int, int> pii; const double EPS = 1e-10; const int INF = 1e9; const ll LINF = 1e18; const int MOD = 1000000007; const double PI = acos(-1); int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; bool chmin(ll &a, ll b) {if(a>b){a=b; return 1; } return 0; } typedef pair<ll, int> P; struct edge {int to; ll cost; }; struct Graph { private: int n; vector<ll> d; vector<vector<edge>> es; public: Graph(int _n): n(_n), d(_n), es(_n) {} void add_edge(int from, int to, ll cost) { edge e; e.to = to, e.cost = cost; es[from].push_back(e); } void dijkstra(int s) { rep(i,n) d[i] = LINF; d[s] = 0; priority_queue<P, vector<P>, greater<P>> q; q.push(P(0, s)); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (d[v] < p.first) continue; for (edge e: es[v]) { if (chmin(d[e.to], p.first + e.cost)) { q.push(P(d[e.to], e.to)); } } } } ll get_dist(int u) { return d[u]; } }; int h, w; int f(int i, int j) { return i*w+j; } int main() { cin >> h >> w; vector<string> g(h); rep(i,h) cin >> g[i]; Graph dij(h*w+26); int ps = -1, pg = -1; rep(i,h) { rep(j,w) { if (g[i][j] == '#') continue; int cur = f(i,j); if (g[i][j] == 'S') ps = cur; if (g[i][j] == 'G') pg = cur; rep(d, 4) { int ni = i + dy[d]; int nj = j + dx[d]; if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue; if (g[ni][nj] == '#') continue; int nxt = f(ni,nj); dij.add_edge(cur, nxt, 1); } if ('a' <= g[i][j] && g[i][j] <= 'z') { int spr = h*w + (g[i][j] - 'a'); dij.add_edge(cur, spr, 1); dij.add_edge(spr, cur, 0); } } } dij.dijkstra(ps); if (dij.get_dist(pg) == LINF) cout << -1 << endl; else cout << dij.get_dist(pg) << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #ifdef ENABLE_DEBUG #define dump(a) cerr<<#a<<"="<<(a)<<endl #define dumparr(a,n) cerr<<#a<<"["<<(n)<<"]="<<(a[n])<<endl #else #define dump(a) #define dumparr(a,n) #endif #define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++) #define For(i, a) FOR(i, 0, a) #define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--) #define Rev(i, a) REV(i, 0, a) #define REP(a) For(i, a) #define SIGN(a) (a==0?0:(a>0?1:-1)) typedef long long int ll; typedef unsigned long long ull; typedef unsigned int uint; typedef pair<ll, ll> pll; typedef pair<ll,pll> ppll; typedef vector<ll> vll; typedef long double ld; typedef pair<ld,ld> pdd; pll operator+(pll a,pll b){ return pll(a.first+b.first,a.second+b.second); } pll operator-(pll a,pll b){ return pll(a.first-b.first,a.second-b.second); } pll operator*(ll a,pll b){ return pll(b.first*a,b.second*a); } const ll INF=(1LL<<60); #if __cplusplus<201700L ll gcd(ll a, ll b) { a=abs(a); b=abs(b); if(a==0)return b; if(b==0)return a; if(a < b) return gcd(b, a); ll r; while ((r=a%b)) { a = b; b = r; } return b; } #endif template<class T> bool chmax(T& a,const T& b){ if(a<b){ a=b; return true; } return false; } template<class T> bool chmin(T& a,const T& b){ if(a>b){ a=b; return true; } return false; } template<class S,class T> std::ostream& operator<<(std::ostream& os,pair<S,T> a){ os << "(" << a.first << "," << a.second << ")"; return os; } template<class T> std::ostream& operator<<(std::ostream& os,vector<T> a){ os << "[ "; REP(a.size()){ os<< a[i] << " "; } os<< " ]"; return os; } int main(){ cout<<setprecision(1000); ll N,M; cin>>N>>M; vector<ll> dp(1LL<<N); vector<vector<pll>> cond(N); REP(M){ ll x,y,z; cin>>x>>y>>z; --x;--y; cond[x].push_back(pll(y,z)); } auto f=[](ll num,ll y,ll z){ ll mask=(1LL<<(y+1))-1; num&=mask; return __builtin_popcountll(num)<=z; }; dp[0]=1; For(j,(1LL<<N)){ ll n=__builtin_popcountll(j); if(n==N)continue; ll cur=1; REP(N){ if((cur&j)==0){ ll jj=(j|cur); ([&]() { FOR(cidx, n, N) { for (auto &&[y, z] : cond[cidx]) { if (!f(jj, y, z)) { return; } } } dp[jj]+=dp[j]; })(); } cur<<=1; } } dump(dp); cout<<dp[(1LL<<N)-1]<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int calc_min_delta(const vector<int> &a){ int ret = 1e9; for(int i=1; i<int(a.size()); ++i){ ret = min(ret, abs(a[i] - a[i-1])); } return ret; } int main(){ int n; string s; cin >> n >> s; vector<int> a(n+1); for(int i=0; i<=n; ++i)cin >> a[i]; const auto k = calc_min_delta(a); cout << k << endl; for(int i=0; i<k; ++i){ cout << (a[0] + i) / k; for(int j=1; j<=n; ++j){ cout << " " << (a[j] + i) / k; } cout << endl; } return 0; }
#include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<cstdio> #include<string> #include<deque> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; int n; char s[110]; int a[110]; int b[110]; int maxB[110]; int szA; int step[110]; int r[110]; bool checkYes(int numA, int numB, char sign) { if (numA == numB) return false; if (sign == '>') { if (numA > numB) return true; else return false; } else { //< if (numA < numB) return true; else return false; } } void testB() { int curLevel = 0; b[0] = 0; int minD = b[0]; for (int i = 0 ; i < n ; i++) { if (s[i] == '<') { b[i + 1] = b[i] + 1; } else { b[i + 1] = b[i] - 1; } if (minD > b[i + 1]) minD = b[i + 1]; } int adds = -minD; for (int i = 0 ; i <= n ; i++) { b[i] = b[i] + adds; } for (int i = 0 ; i <= n ; i++) { maxB[i] = b[i]; } if (s[0] == '<') { b[0] = 0; } if (s[n - 1] == '>') { b[n] = 0; } for (int i = 1 ; i < n ; i++) { if ((s[i - 1] == '>') && (s[i] == '<')) { b[i] = 0; } } for (int ii = 0 ; ii <= 200 ; ii++) { for (int i = 0 ; i <= n ; i++) { int curNum = b[i] - 1; if (curNum < 0) continue; bool canDel = true; if ((i - 1) >= 0) { if (checkYes(b[i - 1], curNum, s[i - 1]) == false) { canDel = false; } } if (i + 1 <= n) { if (checkYes(curNum, b[i + 1], s[i]) == false) { canDel = false; } } if (canDel) { b[i] = curNum; } } } } bool checkLast(int b[]) { for (int i = 0 ; i < n ; i++) { int numA = a[i] - b[i]; int numB = a[i + 1] - b[i + 1]; if ((numA < 0) || (numB < 0)) return false; if ((a[i] - b[i]) == (a[i + 1] - b[i + 1])) return false; bool isBigger = true; if ((a[i] - b[i]) > (a[i + 1] - b[i + 1])) { isBigger = true; } else { isBigger = false; } if (s[i] == '>') { if (isBigger == false) { return false; } } else { //< if (isBigger == true) { return false; } } } return true; } int main(void) { scanf("%d\n", &n); scanf("%s\n", s); szA = n + 1; for (int i = 0 ; i <= n ; i++) { scanf("%d", &(a[i])); } testB(); int minDiff = abs(a[1] - a[0]); for (int i = 2 ; i <= n ; i++) { int curDiff = abs(a[i] - a[i - 1]); if (minDiff > curDiff) minDiff = curDiff; } int k = minDiff; printf("%d\n", k); for (int j = 0 ; j <= n ; j++) { a[j] = a[j] - b[j] * k; step[j] = a[j] / k; r[j] = a[j] % k; } for (int i = 0 ; i < k ; i++) { for (int j = 0 ; j <= n ; j++) { int curNum; if (r[j] > 0) { curNum = b[j] + step[j] + 1; r[j]--; } else { curNum = b[j] + step[j]; } printf("%d ", curNum); } printf("\n"); } return 0; }
#include<bits/stdc++.h> #define ll long long #define ld long double #define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) #define pb push_back #define pf push_front #define mp make_pair #define mod 1000000007 #define vll vector<ll> #define inf 3e15 +7 #define pll pair<ll,ll> #define ff first #define ss second #define PI acos(-1) #define prm 998244353 #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; /* ll dx[4]={1,-1,0,0}; ll dy[4]={0,0,-1,1}; cout<<fixed<<setprecision(12)<<; ll power(ll a, ll b){//a^b ll res=1; a=a%prm; while(b>0){ if(b&1){res=(res*a)%prm;b--;} a=(a*a)%prm; b>>=1; } return res; } ll inv(ll y){return power(y,prm-2);} ll fact[300060]; ll nCr(ll n, ll r) { if (r==0) return 1; return (fact[n]* inv(fact[r]) % prm * inv(fact[n-r]) % prm) % prm; } void init(ll n) { fact[0]=1; for(ll i=1;i<=n;i++)fact[i]=(fact[i-1]*i)%prm; } */ /* //cout<<"Case #"<<fff-T<<": "; /* ll spf[15000050]; void sieve() { for(ll i=2;i*i<=15000030;i++){ if(spf[i]==0){ for(ll j=i+i;j<=15000030;j+=i){ if(spf[j]==0){ spf[j]=i; } } } } }*/ int main() { fastio; ll T=1; //cin>>T; //ll fff=T; while(T--) { ll n; cin>>n; ll a[n]; for(ll i=0;i<n;i++)cin>>a[i]; sort(a,a+n); ld x1=(ld)a[n/2]/2; ld x2=(ld)a[n/2 -1]/2; ld x3=(ld)a[n/2 +1]/2; ld ans1=0,ans2=0,ans3=0; for(ll i=0;i<n;i++) { ans1=(ans1+x1+a[i]-min((ld)a[i],(ld)2*x1)); ans2=(ans2+x2+a[i]-min((ld)a[i],(ld)2*x2)); ans3=(ans3+x3+a[i]-min((ld)a[i],(ld)2*x3)); } cout<<fixed<<setprecision(12)<<(ld)min({ans1,ans2,ans3})/(ld)n; } } /* */
//Template Created By: OptimalKnight #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> #include <chrono> using namespace std; using namespace __gnu_pbds; using namespace chrono; #pragma GCC optimize("Ofast") #pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #pragma GCC optimize("unroll-loops") #define ios ios_base::sync_with_stdio(false);cin.tie(NULL); #define max3(a,b,c) max(max((a),(b)),(c)) #define max4(a,b,c,d) max(max((a),(b)),max((c),(d))) #define min3(a,b,c) min(min((a),(b)),(c)) #define min4(a,b,c,d) min(min((a),(b)),min((c),(d))) #define ll long long int #define fo(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define endl "\n" #define pb push_back #define mp make_pair #define lb lower_bound #define up upper_bound #define all(V) V.begin(),V.end() #define d0(x) cout<<(x)<<" " #define d1(x) cout<<(x)<<endl #define d2(x,y) cout<<(x)<<" "<<(y)<<endl #define ye(x) ((x)==0)?(cout<<"YES"<<endl):(cout<<"Yes"<<endl) #define no(x) ((x)==0)?(cout<<"NO"<<endl):(cout<<"No"<<endl) #define take(x) for(auto &y:x) cin>>y #define show(x) for(auto y:x) cout<<y<<" "; cout<<endl mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count()); //uniform_int_distribution<long long int> dis(L,R); //dis(gen): Produces uniformly distributed integer values on the closed interval [L,R] //dis.reset(): Resets the distribution template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //find_by_order(K): Returns an iterator to the Kth largest element(counting from zero) //order_of_key(K): Returns the number of items that are strictly smaller than K //const long double pi=3.14159265358979323846264338; //const long long INF=1000000000000000000; //const long long IINF=1000000000; //const long long int MOD=1000000007; //Template Ends int main(){ ios ll Tests=1; // cin>>Tests; while(Tests--){ float A,B; cin>>A>>B; float ans=A*B; ans/=100; cout<<ans<<endl; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll N,X,Y,a=-1e18,i=0;cin>>N>>X; ll A[N+1],T[N+1],B[N+1]; for(int i=1;i<=N;B[i]=A[i],T[i]=a,i++)cin>>A[i]; vector<vector<int>>G(N+1); for(;cin>>X>>Y;)G[X].push_back(Y); for(i=1;i<=N;i++)for(auto v:G[i])T[v]=max(T[v],A[v]-B[i]),B[v]=min(B[v],B[i]); cout<<*max_element(T+1,T+N+1); }
#include <bits/stdc++.h> using namespace std; #define int long long #define ld long double #define pb push_back #define mp make_pair #define f first #define s second #define all(x) x.begin(),x.end() #define LOL cout << '\n'; #define kill(x) {cout << (x) << '\n'; return;} #define sz(x) ((int)x.size()) #define INF 1e15 #define NINF LONG_LONG_MIN #define deb(x) cout << "[" << (#x) << "=" << x << "]" << '\n' #define deb2(x,y) cout << "[" << (#x) << "=" << x << "] [" << (#y) << "=" << y << "]" << '\n' #define deb3(x,y,z) cout << "[" << (#x) << "=" << x << "] [" << (#y) << "=" << y << "] [" << (#z) << "=" << z << "]" << '\n' int n,m; const int N=2e5+69; vector<int> adj[N]; int a[N]; vector<pair<int,int>> vis(N); int OP=NINF; pair<int,int> dfs(int s){ if(vis[s].f!=-1 && vis[s].s!=-1) return vis[s]; int ans=NINF,mx=0; for(auto &el:adj[s]){ pair<int,int> T=dfs(el); ans=min(ans,T.f); mx=max(mx,T.s); // deb3(s,ans,mx); } if(mx!=0) ans=max(ans,mx-a[s]); mx=max(mx,a[s]); // deb3(s,ans,mx); OP=max(OP,ans); return vis[s]={ans,mx}; } void solve(){ cin>>n>>m; for(int i=1 ; i<=n ; i++) cin>>a[i],vis[i]={-1,-1}; for(int i=0 ; i<m ; i++){ int u,v; cin>>u>>v; adj[u].pb(v); } for(int i=1 ; i<=n ; i++){ if(vis[i].f==-1 && vis[i].s==-1){ dfs(i); } } cout<<OP;LOL } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int _ = 1; // cin >> _; for(int i = 1 ; i <= _ ; i++){ // cout << "\nCase #" << i << ": \n"; solve(); } return 0; } // 1.77245
#include<bits/stdc++.h> using namespace std; typedef unsigned long long UINT64; int n; string s; int main() { scanf("%d", &n); s.resize(n); scanf("%s", &s[0]); UINT64 res = 0; if(n == 1) { if(s == "1") res = 20000000000; else res = 10000000000; } else if(n == 2) { if(s == "11" || s == "10") res = 10000000000; else if(s == "01") res = 9999999999; } else { if(s.substr(0, 3) == "110") { res = 10000000000; int sub = n/3; if(n%3 > 0) ++sub; res -= sub - 1; } else if(s.substr(0, 3) == "101") { res = 10000000000; int sub = (n+1)/3; if((n+1)%3 > 0) ++sub; res -= sub - 1; } else if(s.substr(0, 3) == "011") { res = 10000000000; int sub = (n+2)/3; if((n+2)%3 > 0) ++sub; res -= sub - 1; } for (int i = 3; i < s.size(); ++i) { if(s[i] != s[i%3]) { res = 0; break; } } } printf("%llu\n", res); return 0; }
#include <bits/stdc++.h> using namespace std; template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); } template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); } int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s; cin >> s; long long p = 1; for(int i = 0; i < 10; i++) p *= 10; int pieces = (n + 5) / 3; vector<int> vs; string to_match; for(int i = 0; i < pieces; i++){ vs.push_back(i); vs.push_back(i); vs.push_back(i); to_match.push_back('1'); to_match.push_back('1'); to_match.push_back('0'); } long long ans = 0; for(int i = 0; i < 3; i++){ bool ok = true; for(int j = 0; j < n; j++){ if(to_match[i + j] != s[j]) ok = false; } if(ok){ int len = vs[i + n - 1] - vs[i] + 1; ans += (p - len + 1); } } cout << ans << '\n'; return 0; }
//------HARSH------// #include<bits/stdc++.h> #include<vector> #include<string> #include<algorithm> using namespace std; #define int long long #define f(i,a,n) for(int i=a;i<n;i++) #define rf(i,a,n) for(int i=n-1;i>=a;i--) #define read(arr,n) for(int i=0;i<n;i++)cin>>arr[i]; #define in cin>> #define out cout<< #define fr first #define sc second #define VC vector<int> #define VP vector<pair<int,int>> #define pb push_back #define mp make_pair #define PI 3.1415926535897932384626433832 int gcd(int a, int b){ if (b == 0){return a;} else{return gcd(b,a%b);} } int lcm(int a, int b){ return (a*b)/(gcd(a,b)); } map<int,vector<int>> vec; void primeFactors(int n) { int TEMP=n; vector<int> temp; while (n % 2 == 0) { n = n/2; temp.pb(2); } for (int i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { n = n/i; temp.pb(i); } } if (n > 2){temp.pb(n);} vec[TEMP]=temp; } void solve(){ int n; cin>>n; int arr[n]; read(arr,n); f(i,0,n){ primeFactors(arr[i]); } int ans=1; set<int> s; // for(auto itr:vec){ // int flag=0; // for(auto itr2:itr.sc){ // if(s.find(itr2)!=s.end()){flag=1;break;} // } // if(flag==0){s.insert(*itr.sc.begin());ans*=*itr.sc.begin();} // } // cout<<ans<<"\n"; for(auto itr:vec){ for(auto itr2:itr.sc){ s.insert(itr2); } } for(auto itr:s){ans*=itr;} int ans2=ans; for(auto itr=s.rbegin();itr!=s.rend();itr++){ int temp=ans/(*itr); int flag=0; f(i,0,n){ if(gcd(temp,arr[i])==1){flag=1;break;} } if(flag==0){ans=temp;} } for(auto itr:s){ int temp=ans2/(itr); int flag=0; f(i,0,n){ if(gcd(temp,arr[i])==1){flag=1;break;} } if(flag==0){ans2=temp;} } cout<<min(ans,ans2)<<"\n"; } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(NULL); cin.tie(0); cout.tie(0); int t; t=1; //in t; while(t--){ solve(); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define int long long #define fs first #define sc second #define sz(x) (int)x.size() #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define MP make_pair mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #ifdef FlameDragon freopen("in.txt", "r", stdin); #endif int n; cin >> n; int k = 5; vector<vector<int>> a(n, vector<int>(k)); for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { cin >> a[i][j]; } } const int INF = 1e9 + 123; int l = 0, r = INF; auto check = [&](int x) -> bool { vector<int> cnt(1 << k); for (int i = 0; i < n; i++) { int mask = 0; for (int j = 0; j < k; j++) { if (a[i][j] >= x) { mask ^= 1 << j; } } cnt[mask] = 1; } for (int i = 0; i < (1 << k); i++) { for (int j = 0; j < (1 << k); j++) { for (int p = 0; p < (1 << k); p++) { if (cnt[i] && cnt[j] && cnt[p] && (i | j | p) == ((1 << k) - 1)) { return true; } } } } return false; }; while (r - l > 1) { int mid = (l + r) / 2; if (check(mid)) { l = mid; } else { r = mid; } } cout << l << '\n'; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; typedef pair<ll, ll> P; typedef vector<string> vs; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef vector<vp> vvp; // input int n, k; string s; void input() { cin >> n >> k; cin >> s; } int main() { input(); map<char, map<char, char>> m; m['R']['R'] = m['R']['S'] = m['S']['R'] = 'R'; m['P']['P'] = m['P']['R'] = m['R']['P'] = 'P'; m['S']['S'] = m['P']['S'] = m['S']['P'] = 'S'; rep(i, k) { s += s; int sn = s.size(); string t = ""; for (int j = 0; j < sn; j += 2) t += m[s[j]][s[j + 1]]; swap(s, t); } char ans = s[0]; cout << ans << endl; }
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 4000000000000000000 //オーバーフローに注意 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define SIZE 105 char buf[SIZE],work[2*SIZE]; char calc(char a,char b){ switch(a){ case 'R': switch(b){ case 'R':return 'R'; case 'P':return 'P'; case 'S':return 'R'; } break; case 'P': switch(b){ case 'R':return 'P'; case 'P':return 'P'; case 'S':return 'S'; } break; case 'S': switch(b){ case 'R':return 'R'; case 'P':return 'S'; case 'S':return 'S'; } break; } return '@'; } int main(){ int N,K; scanf("%d %d",&N,&K); scanf("%s",buf); for(int a = 0; a < K; a++){ int index = 0; for(int loop = 0; loop < 2; loop++){ for(int i = 0; i < N; i++){ work[index] = buf[i]; index++; } } index = 0; for(int i = 0; i < 2*N; i += 2){ buf[index++] = calc(work[i],work[i+1]); } } printf("%c\n",buf[0]); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long const int nax=2e5+100,mod=1e9+7; int dp[nax][19]; int32_t main() { int t=1; //cin>>t; while(t--) { string a; cin>>a; int k; cin>>k; set<int> s; s.clear(); for(int i=0;i<a.size();i++) { for(int j=0;j<17;j++) { dp[i+1][j]+=dp[i][j]*j; dp[i+1][j]%=mod; dp[i+1][j+1]+=dp[i][j]*(16-j); dp[i+1][j+1]%=mod; } int num=a[i]-'0'; if(a[i]>='A') num=10+a[i]-'A'; for(int j=0;j<num;j++) { if(i==0 and j==0) continue; if(s.count(j)) { dp[i+1][s.size()]++; dp[i+1][s.size()]%=mod; } else { dp[i+1][s.size()+1]++; dp[i+1][s.size()+1]%=mod; } } s.insert(num); if(i!=0) dp[i+1][1]+=15; } int res=dp[a.size()][k]; set<char> ss; for(int i=0;i<a.size();i++) ss.insert(a[i]); if(ss.size()==k) ++res; cout<<res; } return 0; }
#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<algorithm> #include<queue> #include<vector> #include<cstring> #include<map> #include<set> #include<cstdlib> #include<bitset> using namespace std; #define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) typedef long long ll; #define N 200005 ll p=1e9+7; ll n,m,f[N][17][17]={0},v[22]={0},sm; char a[N]; int main() { ll i,j,k,a1,a2,an=0; scanf("%s",a+1); n=strlen(a+1); for(ll i=1;i<=n;i++) if(a[i]>='0'&&a[i]<='9') a[i]-='0'; else a[i]-='A'-10; scanf("%lld",&m); for(ll i=0;i<=m;i++) f[0][i][i]=1; for(ll i=1;i<=n;i++) for(ll j=1;j<=m;j++) for(ll k=j;k<=m;k++) f[i][j][k]=((ll)f[i-1][j][k-1]*(17ll-k)+(ll)f[i-1][j][k]*k)%p; for(ll i=1;i<=n;i++) an=(an+15ll*f[i-1][1][m])%p; // for(ll i=1;i<=n;i++) // { // for(ll j=1;j<=m;j++) // for(ll k=j;k<=m;k++)printf("%lld\n",f[i][j][1]); // } for(ll i=1;i<=n;i++) { for(ll j=a[i]+1;j<=15;j++) an=(an-f[n-i][sm+(!v[j])][m]+p)%p; //printf("%lld\n",an); sm+=!v[a[i]]; v[a[i]]=1; } printf("%lld",an); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const double pi=3.1415926535897932384626; inline ll read(){ ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } int n; vector<int> ans; int main(){ n=read(); for(int i=10;n&&i<=10000;++i){ if(i%6==0||i%10==0||i%15==0){ ans.push_back(i); --n; } } for(int i=0;i<ans.size();++i) printf("%d%c",ans[i],i<ans.size()-1?' ':'\n'); return 0; }
#include <iostream> #include <cmath> #include <string> #include <vector> using namespace std; int main() { int N; cin >> N; int mx = 0; int mn = 1000; for (int i = 0; i < N;++i) { int a; cin >> a; mx = max(a, mx); } for (int i = 0;i < N;++i) { int b; cin >> b; mn = min(mn, b); } int ans = mn - mx + 1; if (ans > 0) { cout << ans; } else { cout << 0; } return 0; }
#include <iostream> #include <array> #include <vector> #include <cassert> #include <algorithm> using namespace std; using lint = long long; int main() { int n, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0; i < m; ++i) { int s, t; cin >> s >> t; s--; t--; g[s].push_back(t); g[t].push_back(s); } vector<int> idx(n, -1); //dfs順を記録。違う連結成分になら同じindexを持ちうる。 auto dfs = [&](auto dfs, int now, array<int, 20> &v) -> lint { for (int to : g[now]) { if (v[to] == v[now]) { return 0; } } lint res = 1; for (int to : g[now]) { if (idx[to] == -1) idx[to] = idx[now] + 1; if (idx[to] == idx[now] + 1) { lint tmp = 0; v[to] = (v[now] + 1) % 3; tmp += dfs(dfs, to, v); v[to] = (v[now] + 2) % 3; tmp += dfs(dfs, to, v); v[to] = -1; res *= tmp; if (tmp == 0) break; // tmp == 0 の時、早期に return してるのでidxが振り切れていない事に注意 } } return res; }; lint ans = 1; array<int, 20> v; v.fill(-1); for (int i = 0; i < n; ++i) { if (idx[i] != -1) continue; idx[i] = 0; v[i] = 0; ans *= dfs(dfs, i, v) * 3; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<pair<int,int>> vpi; #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) #define pb push_back #define rsz resize #define all(x) begin(x), end(x) #define pi pair<int, int> #define mp make_pair #define F first #define S second int n, m, a, b; vector<vi> adj; vi c, cgroup; vector<bool> vis; ll calc(int i) { if (i==cgroup.size()) return 1; int node = cgroup[i]; ll ans = 0; vector<bool> taken(4, false); for (int nb : adj[node]) taken[c[nb]]=true; F0R(j, 3) if (!taken[j]) { c[node]=j; ans += calc(i+1); } c[node]=3; return ans; } void group(int i) { if (vis[i]) return; vis[i]=true; cgroup.pb(i); for (int nb : adj[i]) if (! vis[nb]) group(nb); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; adj.rsz(n); F0R(i, m) { cin >> a >> b; adj[a-1].pb(b-1); adj[b-1].pb(a-1); } c.rsz(n, 3); ll ans = 1; vis.rsz(n, false); F0R(i, n) { if (!vis[i]) { cgroup.clear(); group(i); c[i]=0; ans *= 3*calc(1); } } cout << ans << '\n'; }
#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 sti stack<int64_t> #define qui 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 int main(){ ll li; string x; cin>>x; ll s=x.size(); vl vec(s); ll m,ma; ma=0; cin>>m; fo(i,s){ vec.at(i)=x.at(i)-'0'; } fo(i,s){ ma=max(ma,vec.at(i)); } if(s==1){if(vec.at(0)<=m){cout<<1<<endl;} else{cout<<0<<en;}} else{ll lb=-1; ll ub=2*pow(10,18); while(ub-lb>1){ ll mid=(lb+ub)/2; if(vec.at(0)*pow(mid,(s-1))>pow(10,18)){ ub=mid; } else{lb=mid;} } li=lb; ub=lb; lb=ma+1; if(lb>ub){cout<<0<<endl;} else{ lb=0; ub=2*pow(10,18); while(ub-lb>1){ ll mid=(lb+ub)/2; if(mid>li){ub=mid;} else{ll ans=0; fo(i,s){ ans=ans*mid; ans+=vec.at(i); } if(ans>m){ ub=mid; } else{lb=mid;} }} cout<<max(lb-ma,0L)<<en; } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back bool check(ll a, ll b, ll m) { if (a == 0 || b == 0) return false; if (a > m / b) return true; return false; } bool pos(vector<ll> &dig, ll x, ll m) { ll num = dig[0], cur = 1; for (int i = 1; i < dig.size(); i++) { if (check(cur, x, m)) return false; cur *= x; if (check(cur, dig[i], m)) return false; if (num > m - (cur * dig[i])) return false; num += cur * dig[i]; if (num > m) return false; } return num <= m; } ll solve() { string str; ll m, res = 0; cin >> str >> m; ll d = 0; vector<ll> dig; for (char s : str) { dig.pb((s - '0') * 1ll); d = max(d, dig.back()); } reverse(dig.begin(), dig.end()); if (dig.size() == 1) { if (dig[0] > m) return 0; return 1; } ll l = d + 1, r = 1e18; while (l < r) { ll mid = l + (r - l) / 2; if (pos(dig, mid, m)) { res = max(res, mid - d); l = mid + 1; } else r = mid - 1; } if (pos(dig, l, m)) res = max(res, l - d); return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << solve() << "\n"; return 0; }
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define IO freopen("in.txt","r",stdin),freopen("out.txt","w",stdout) #define P(x) cout<<x<<endl #define P2(x,y) cout<<x<<' '<<y<<endl #define P3(x,y,z) cout<<x<<' '<<y<<' '<<z<<endl #define P4(x,y,z,u) cout<<x<<' '<<y<<' '<<z<<' '<<u<<endl #define P5(x,y,z,u,v) cout<<x<<' '<<y<<' '<<z<<' '<<u<<' '<<v<<endl #define P6(x,y,z,u,v,w) cout<<x<<' '<<y<<' '<<z<<' '<<u<<' '<<v<<' '<<w<<endl #define PV(x) for(auto ii:(x)){cout<<ii<<' ';}cout<<endl #define PN(x,n) for(int ii=1;ii<=(n);ii++){cout<<x[ii]<<' ';}cout<<endl #define PNN(x,n,m) for(int ii=1;ii<=(n);ii++){for(int jj=1;jj<=(m);jj++){cout<<x[ii][jj]<<' ';}cout<<endl;} typedef long long ll; typedef unsigned long long ull; const ll D = 0; const ll N = 222; const ll mod = 1e9 + 7; const ll inf = 1e9 + 7; const double eps = 1e-9; ll n; bool visRow[10010]; vector<ll> adInRow[10010]; double score=0; struct AD { ll id,x,y,r,a,b,c,d; bool operator <(const AD &tmp)const { return y<tmp.y; } }ad[N]; bool cmp(ll id1,ll id2) { return ad[id1].y < ad[id2].y; } ll area(ll x,ll y,ll z,ll w) { return abs(x-y) * abs(z-w); } int solve() { cin>>n; ll maxX=0; for(ll i=1;i<=n;i++) { cin>>ad[i].x>>ad[i].y>>ad[i].r; ad[i].id = i; //visRow[ad[i].x]=true; adInRow[ad[i].x].push_back(i); maxX = max(maxX, ad[i].x); //P("ok"); } for(ll i=0;i<10000;i++) { sort(adInRow[i].begin(),adInRow[i].end(),cmp); } ll lstX=0,lstY=0,cnt,siz; for(ll i=0;i<10000;i++) { if(!adInRow[i].empty()) { cnt=0; siz = adInRow[i].size(); lstY=0; for(ll j=0;j<siz;j++) { AD *now = &ad[adInRow[i][j]]; now->a = lstY; now->b = now->y + 1; if(j == siz-1) now->b = 10000; lstY = now->b; if(i==maxX) now->d = 10000; else now->d = now->x + 1; ll tmpx = now->x; while(tmpx<now->x && area(now->b,now->a,now->d,now->c)<(now->r)) { tmpx--; } now->c = lstX;//[lstx,nowx] } lstX = i + 1; } } score=0; for(ll i=1;i<=n;i++) { P4(ad[i].a,ad[i].c,ad[i].b,ad[i].d); if(D) { double s = area(ad[i].b,ad[i].a,ad[i].d,ad[i].c); double r = ad[i].r; score += 1 - pow(1 - (min(r,s)/max(r,s)), 2.); //P((min(r,s)/max(r,s))); } } if(D) { score = 1e9 * (score / n); cout<<"Your score: "<<(ll)score; } return 0; } int main() { //BUFF; if(D)IO; solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define pb(x) push_back(x) #define mp(a, b) make_pair(a, b) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define rep(i, n) for (ll i = 0; i < (n); i++) #define rep2(i, n) for (ll i = n - 1; i >= 0; i--) struct Timer { private: chrono::high_resolution_clock::time_point start, end; public: Timer() { set(); } void set() { start = chrono::high_resolution_clock::now(); } int time() { end = chrono::high_resolution_clock::now(); return chrono::duration_cast<chrono::milliseconds>(end - start).count(); } void print() { cerr << "elapsed time: " << (double)time() / 1000 << " sec" << endl; } }; struct Random { private: uint32_t a, b, c, d; public: Random(uint32_t seed = 517) { set_seed(seed); } void set_seed(uint32_t seed) { a = seed = 1812433253u * (seed ^ (seed >> 30)); b = seed = 1812433253u * (seed ^ (seed >> 30)) + 1; c = seed = 1812433253u * (seed ^ (seed >> 30)) + 2; d = seed = 1812433253u * (seed ^ (seed >> 30)) + 3; } uint32_t operator()() { uint32_t t = (a ^ (a << 11)); a = b; b = c; c = d; return d = (d ^ (d >> 19)) ^ (t ^ (t >> 8)); } // [0,MAX) uint32_t operator()(int MAX) { return (uint64_t) operator()() * MAX >> 32; } // [L,R) uint32_t operator()(int L, int R) { return L + operator()(R - L); } // [0,1) の実数 double drnd() { return (double)operator()() / (1LL << 32); } // 確率 p で true を返す bool prob(const double p) { return (uint32_t)(p * (1LL << 32)) > operator()(); } }; double TL = 3500; double start_temp = 2.5, end_temp = 0; int main() { Timer T; Random rnd; int n; cin >> n; int x[n], y[n], r[n]; rep(i, n) cin >> x[i] >> y[i] >> r[i]; int a[n], b[n], c[n], d[n], cnt = 0; rep(i,n){ a[i] = -1; int ok = 0, ng = max(1.0, sqrt(r[i])) + 1e-5 + 1; while(ng - ok > 1){ int nr = (ok + ng) / 2; rep(k,4){ int xl = x[i], xr = x[i] + nr*(1+(-2)*(k/2)), yl = y[i], yr = y[i] + nr*(1+(-2)*(k%2)); if(xl>xr) swap(xl, xr); if(yl>yr) swap(yl, yr); int flag = 1; if(xl<0||yl<0||xr>10000||yr>10000) flag = 0; rep(j, i) if (a[j] < xr && c[j] > xl && b[j] < yr && d[j] > yl) flag = 0; if(flag){ a[i] = xl, b[i] = yl, c[i] = xr, d[i] = yr; ok = nr; break; } if(k == 3) ng = nr; } } if(a[i] == -1){ while (1){ int xl = cnt / 10000, yl = cnt % 10000; int xr = xl + 1, yr = yl + 1; cnt++; int flag = 1; rep(j, i) if (a[j] < xr && c[j] > xl && b[j] < yr && d[j] > yl) flag = 0; if(flag){ a[i] = xl, b[i] = yl, c[i] = xr, d[i] = yr; break; } } } } rep(i, n) cout << a[i] << " " << b[i] << " " << c[i] << " " << d[i] << endl; }
#include <bits/stdc++.h> int main() { using namespace std; ios_base::sync_with_stdio(false), cin.tie(nullptr); const int INF = 1e9; int N; cin >> N; string S; cin >> S; std::array<int, 26> dp; dp.fill(INF); dp[S[0] - 'a'] = 0; for (int i = 0; i < int(S.size()); i++) { int best = INF; for (int v = 0; v < 26; v++) { if (v == S[i] - 'a') continue; best = min(best, dp[v] + 1); } if (i+1 < int(S.size())) { dp[S[i+1] - 'a'] = min(dp[S[i+1] - 'a'], best); } else { if (best > N) best = -1; cout << best << '\n'; } } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; #define pow(n,m) powl(n,m) #define sqrt(n) sqrtl(n) const ll MAX = 5000000000000000000; const ll MOD = 0; random_device rd; mt19937 mt(rd()); int main(){ ll N; string S; cin >> N >> S; if(S[0] != S[N - 1]){ cout << 1 << endl; } else{ for(ll i = 0;i < N - 1;i++){ if(S[0] != S[i] && S[0] != S[i + 1]){ cout << 2 << endl; return 0; } } cout << -1 << endl; } }
#include<iostream> #include<string> using namespace std; string judge(long N) { if (N == 0) return "Yes"; while (N % 10 == 0) { N /= 10; } long reverse = 0; long buf = N; while (buf != 0) { reverse = reverse * 10 + buf % 10; buf /= 10; } if (N == reverse) return "Yes"; else return "No"; } int main() { long N = 0; cin >> N; cout << judge(N) << endl; }
#include <iostream> #include <map> #include <vector> #include <set> #include <stack> #include <bitset> #include <queue> #include <algorithm> #include <iomanip> #include <unordered_map> #include <unordered_set> #include <cmath> #include <cassert> #include <random> using namespace std; void solve(); int main() { srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif solve(); } #define int long long const int N = 2e5 + 100, mod = 1e9 + 7, INF = 2e18; long double EPS = 0.00000001; bool bit(int a, int n) { return (a >> n) & 1; } string s; int ans = 0; void solve() { cin >> s; for (int mask = 0; mask < (1 << s.size()); mask++) { int k = 0, cnt = 0; for (int i = 0; i < s.size(); i++) { if (bit(mask, i)) { k += s[i] - '0'; cnt++; } } if (k % 3 == 0) { ans = max(ans, cnt); } } if (ans == 0) cout << -1; else cout << s.size() - ans; }
#include <iostream> #include<vector> #include<bits/stdc++.h> using namespace std; int main(){ #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif double a,b; cin>>a; cin>>b; if(b<=a) { cout<<((a-b)/a)*100; ; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define rrep(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i) void solve(long long A, long long B){ cout << (double) (A - B) * 100 / A << endl; } int main(){ long long A; scanf("%lld",&A); long long B; scanf("%lld",&B); solve(A, B); return 0; }
#include <bits/stdc++.h> #define debug(var) do{std::cout << #var << " :";std::cout << std::endl;view(var);}while(0) template<typename T> void view(T e){ std::cout << e << std::endl;} template<typename T> void view(const std::vector<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; } template<typename T, typename U> void view(const std::pair<T, U>& v){ std::cout << v.first << ", " << v.second << std::endl; } template<typename T> void view(const std::set<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; } template<typename T, typename U> void view(const std::map<T, U>& v){ std::cout << "{" << std::endl; for(const std::pair<T, U>& e : v){ std::cout << e.first << ", " << e.second << std::endl; } std::cout << "}" << std::endl; } template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cout << "{" << std::endl; for(const auto& v : vv){ view(v); } std::cout << "}" << std::endl; } using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1LL << 60; const int MAX = 1000000010; const ll MOD = 1000000007; const double PI = acos(-1); int main() { int n; cin >> n; vector<int> A(n); rep(i,n) { cin >> A[i]; } int cnt = 0; int ans = -1; for (int i = 2; i <= 1000; i++) { int tmp = 0; for (auto x : A) { if (x % i == 0) tmp++; } if (chmax(cnt, tmp)) ans = i; } cout << ans << endl; }
#include <bits/stdc++.h> // clang-format off using namespace std; using ll = int64_t; using ull = uint64_t; const ll INF = 9e18; void print() { cout << endl; } template<typename Head,typename... Tail> void print(Head head,Tail... tail){cout<<head;if(sizeof...(Tail)>0)cout<<" ";print(tail...);} void print0() {} template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<head;print0(tail...);} // clang-format on struct person { ll id; ll a; ll b; }; bool recursive(ll N, ll start, ll donenum, ll anymovenum, vector<ll>& start_static, vector<ll>& end_static, ll skip_kai, ll rest_add, ll rest_end, vector<bool>& checked) { if (anymovenum < 0) { return false; } if (donenum == N) { return true; } if (start >= 2 * N) { return false; } if (start <= rest_end) { ll end = start + rest_add; if ((start_static[end] >= 0) || (end_static[start] >= 0) || (start_static[start] >= 0 && end_static[end] >= 0 && start_static[start] != end_static[end])) { return false; } ll anym = (start_static[start] >= 0 || end_static[end] >= 0) ? anymovenum : anymovenum - 1; return recursive(N, start + 1, donenum + 1, anym, start_static, end_static, skip_kai, rest_add, rest_end, checked); } else if (skip_kai) { return recursive(N, skip_kai, donenum, anymovenum, start_static, end_static, 0, -1, -1, checked); } else { if (checked[start]) { return false; } checked[start] = true; for (ll end = start + 1; true; end++) { if (end - 1 + end - start > 2 * N || end > start + N) { break; } if ((start_static[end] >= 0) || (end_static[start] >= 0) || (start_static[start] >= 0 && end_static[end] >= 0 && start_static[start] != end_static[end])) { continue; } ll anym = (start_static[start] >= 0 || end_static[end] >= 0) ? anymovenum : anymovenum - 1; bool ok = recursive(N, start + 1, donenum + 1, anym, start_static, end_static, end + end - start, (end - start), end - 1, checked); if (ok) { return true; } } return false; } } bool solve(ll N, vector<person>& persons) { set<ll> duplicate; for (auto p : persons) { if (p.a >= 0) { if (duplicate.count(p.a)) { return false; } duplicate.insert(p.a); } if (p.b >= 0) { if (duplicate.count(p.b)) { return false; } duplicate.insert(p.b); } } vector<ll> start_static(2 * N + 1, -1); vector<ll> end_static(2 * N + 1, -1); for (auto p : persons) { if (p.a >= 0) { start_static[p.a] = p.id; } if (p.b >= 0) { end_static[p.b] = p.id; } } ll anymovenum = 0; for (auto p : persons) { if (p.a == -1 && p.b == -1) { anymovenum++; } } vector<bool> checked(2 * N + 1, false); return recursive(N, 1, 0, anymovenum, start_static, end_static, 0, -1, -1, checked); } int main() { ll N; cin >> N; vector<person> persons(N); for (ll i = 0; i < N; i++) { person p; cin >> p.a >> p.b; p.id = i; persons[i] = p; } if (solve(N, persons)) { print("Yes"); } else { print("No"); } }
// Problem: C - Unexpressed // Contest: AtCoder - Caddi Programming Contest 2021(AtCoder Beginner Contest // 193) URL: https://atcoder.jp/contests/abc193/tasks/abc193_c Memory Limit: // 1024 MB Time Limit: 2000 ms Powered by CP Editor // (https://github.com/cpeditor/cpeditor) /*-----------------------------------------------------------*/ #include <bits/stdc++.h> #include <stdio.h> using namespace std; #define l long long #define rep(i, x, n) for (i = x; i < n; i++) #define all(a) a.begin(), a.end() #define fill0(a) memset(a, 0, sizeof(a)) #define fill_1(a) memset(a, -1, sizeof(a)) #define pb push_back #define v(n) vector<l> v(n) #define mp(l) map<l, l> mp #define mp1(s, l) map<string, l> mp1 #define s(x) x.size() #define space cout << endl #define YES cout << "YES\n" #define Yes cout << "Yes\n" #define NO cout << "NO\n" #define No cout << "No\n" #define mx 1e17 #define mn LONG_MIN #define mod 1000000007 #define mode 998244351 #define in(a, x) binary_search(a.begin(), a.end(), x) #define ina(a, n, x) binary_search(a, a + n, x) #define see(args...) \ { \ cerr << "LINE " << __LINE__; \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ cerr << endl; \ } void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << ' ' << *it << " = " << a; err(++it, args...); } inline void op() { cout << '\n'; } template <typename T, typename... Types> inline void op(T var1, Types... var2) { cout << var1 << ' '; op(var2...); } inline void ip() {} template <typename T, typename... Types> inline void ip(T& var1, Types&... var2) { cin >> var1; ip(var2...); } template <typename T, typename U> inline void min_self(T& x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void max_self(T& x, U y) { if (x < y) x = y; } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } template <class T> ostream& operator<<(ostream& out, vector<T> vec) { out << "("; for (auto& v : vec) out << v << ' '; return out << ")"; } template <class T> ostream& operator<<(ostream& out, set<T> vec) { out << "("; for (auto& v : vec) out << v << ", "; return out << ")"; } template <class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out << "("; for (auto& v : vec) out << "[" << v.first << ", " << v.second << "]"; return out << ")"; } template <class A, class B> istream& operator>>(istream& in, pair<A, B>& a) { return in >> a.first >> a.second; } template <class A> istream& operator>>(istream& in, vector<A>& a) { for (A& i : a) in >> i; return in; } template <class Container> void split(const std::string& str, Container& cont, char delim = ',') { stringstream ss(str); string token; while (std::getline(ss, token, delim)) { cont.push_back(token); } } void solve() { l n, i, j, k; ip(n); l ans = n; map<l, l> mp; for (i = 2; i <= 100005; i++) { l x = i * i; while (x < n) { if (mp[x] == 0) { ans--; mp[x]++; } x *= i; } if (x == n) { if (mp[x] == 0) { ans--; mp[x]++; } } } op(ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; // cin >> t; while (t--) { solve(); } }
//IQ134高知能系Vtuberの高井茅乃です。 //Twitter: https://twitter.com/takaichino //YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF INT_MAX #define LLINF LLONG_MAX #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define MODA 1000000007 #define MODB 998244353 template <typename T> std::istream& operator>>(std::istream& is, std::vector<T>& vec) { for (T& x: vec) { is >> x; } return is; } int main() { ll ans = 0; ll tmp = 0; int n; cin >> n; string s; cin >> s; vector<int> a(n+1); cin >> a; int mi[n+1] = {}; REP(i, n) if(s[i] == '<') mi[i+1] = max(mi[i+1], mi[i]+1); for(int i = n; i >= 0; i--) if(s[i] == '>') mi[i] = max(mi[i], mi[i+1]+1); ans = INF; //REP(i, n+1) if(mi[i] > 0) ans = min(ans, (ll)(a[i]/mi[i])); REP(i, n) ans = min(ans, (ll)abs(a[i] - a[i+1])); cout << ans << endl; REP(i, ans){ REP(j, n+1){cout << (a[j] + i)/ ans << " ";} cout << endl;} //REP(i, ans - 1){ REP(j, n+1){cout << mi[j] << " ";} cout << endl;} //REP(i, n+1){cout << a[i] - mi[i] *(ans - 1) << " ";} cout << endl; }
#include <iostream> #include <algorithm> #include <cstring> #define NMAX 101 using namespace std; int frecv[NMAX], n, m, k, ans; pair<int, int> ab[NMAX], cd[NMAX]; int res(int start) { memset(frecv, 0, sizeof(frecv)); for (int i = 0; i < k; ++i) if (start & (1 << i)) ++frecv[cd[i].first]; else ++frecv[cd[i].second]; int rez = 0; for (int i = 1; i <= m; ++i) if (frecv[ab[i].first] && frecv[ab[i].second]) ++rez; return rez; } int main() { cin >> n >> m; for (int i = 1; i <= m; ++i) cin >> ab[i].first >> ab[i].second; cin >> k; for (int i = 0; i < k; ++i) cin >> cd[i].first >> cd[i].second; for (int i = 0; i < 1<<k; ++i) ans = max(ans, res(i)); cout << ans; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<ll, ll>; // Sieve of Eratosthenes // https://youtu.be/UTVg7wzMWQc?t=2774 struct Sieve { int n; vector<int> f, primes; Sieve(int n = 1) : n(n), f(n + 1) { f[0] = f[1] = -1; for (ll i = 2; i <= n; ++i) { if (f[i]) continue; primes.push_back(i); f[i] = i; for (ll j = i * i; j <= n; j += i) { if (!f[j]) f[j] = i; } } } bool isPrime(int x) { return f[x] == x; } vector<int> factorList(int x) { vector<int> res; while (x != 1) { res.push_back(f[x]); x /= f[x]; } return res; } vector<P> factor(int x) { vector<int> fl = factorList(x); if (fl.size() == 0) return {}; vector<P> res(1, P(fl[0], 0)); for (int p : fl) { if (res.back().first == p) { res.back().second++; } else { res.emplace_back(p, 1); } } return res; } vector<pair<ll, int>> factor(ll x) { vector<pair<ll, int>> res; for (int p : primes) { int y = 0; while (x % p == 0) x /= p, ++y; if (y != 0) res.emplace_back(p, y); } if (x != 1) res.emplace_back(x, 1); return res; } }; ll A, B; unordered_map<ll, int> mp; ll dp[1 << 20][74]; ll dfs(ll t, int mask) { if (dp[mask][t - A] != 0) { return dp[mask][t - A]; } if (t > B) { return dp[mask][t - A] = 1; } int tmask = mp[t]; ll retVal = 0; // cerr << "tmask =" << tmask << "bit wise=" << bitset<20>(mask & tmask) << endl; if ((mask & tmask) == 0) { retVal += dfs(t + 1, mask | tmask); } retVal += dfs(t + 1, mask); return dp[mask][t - A] = retVal; } void solve() { Sieve sieve(72); for (ll i = A; i <= B; i++) { int mask = 0; rep(j, sieve.primes.size()) { if (i % sieve.primes[j] == 0) { mask |= (1 << j); } } // cerr << "i=" << i << " mask=" << mask << endl; mp[i] = mask; } dfs(A, 0); cout << dp[0][0] << endl; } // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template) int main() { scanf("%lld", &A); scanf("%lld", &B); solve(); return 0; }
//#include "bits/stdc++.h" #define _USE_MATH_DEFINES #include <iostream> #include <sstream> #include <iomanip> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <numeric> #include <functional> #include <utility> #include <tuple> #include <vector> #include <string> #include <list> #include <set> #include <unordered_set> #include <map> #include <unordered_map> #include <queue> #include <deque> #include <stack> #include <iterator> #include <bitset> #include <complex> #include <limits> #include <random> #include<fstream> #include<array> #include<assert.h> using namespace std; #define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define all(c) begin(c),end(c) #define int ll #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<double, double> pdd; typedef vector< vector<int> > mat; template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)1e9 + 7; const double EPS = 1e-9; signed main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int X; cin >> X; vector<int> A(N); rep(i, 0, N) { cin >> A[i]; } if (A[0] > X || X % A[0] != 0) { cout << 0 << endl; return 0; } X /= A[0]; rrep(i, 0, N) { A[i] /= A[0]; } A.push_back(INF); map<int, int> DP; DP[X] = 1; rrep(i, 0, N) { int t = A[i + 1] / A[i] - 1; map<int, int> NDP; for (const auto& p : DP) { if (abs(p.first) < A[i]) { NDP[p.first] += p.second; } int d = p.first - A[i]; int cnt = 0; if (d > 0) { cnt += d / A[i]; d = d - d / A[i] * A[i]; } while (cnt < t&&abs(d) < A[i]) { NDP[d] += p.second; d -= A[i]; cnt++; } d = p.first + A[i]; cnt = 0; if (d < 0) { cnt += (-d) / A[i]; d = d + (-d) / A[i] * A[i]; } while (cnt<t&&abs(d) < A[i]) { NDP[d] += p.second; d += A[i]; cnt++; } } DP = NDP; } cout << DP[0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<long>p(60);map<pair<long,int>,long> m;long n,x; long z(long a,int i) { if (i == n)return 1; if (m.count({a, i}))return m[{a, i}]; long r, y = p[i], b = a-a%y; if (a % y == 0)r = z(a, i + 1); else if (a > 0)r = z(b, i + 1) + z(b + y, i + 1); else r = z(b, i + 1) + z(b - y, i + 1); return m[{a, i}] = r; } int main() { cin >> n >> x; for (int i = 0; i < n; i++)cin >> p[i]; cout << z(x, 1); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--) #define rrep(i,n) RREP(i,(n)-1,0) #define all(v) v.begin(), v.end() #define endk '\n' const int inf = 1e9+7; const ll longinf = 1LL<<60; const ll mod = 1e9+7; const ll mod2 = 998244353; const ld eps = 1e-10; template<typename T1, typename T2> inline void chmin(T1 &a, T2 b){if(a>b) a=b;} template<typename T1, typename T2> inline void chmax(T1 &a, T2 b){if(a<b) a=b;} int main() { cin.tie(0); ios::sync_with_stdio(false); ll x, y, a, b; cin >> x >> y >> a >> b; ll tmp = 0; ll ans = 0; while(x < y) { chmax(ans, (y-x-1)/b+tmp); if(y/x < a) break; x *= a; tmp++; } cout << ans << endk; return 0; }
#include <bits/stdc++.h> typedef long int l; typedef unsigned long int ul; typedef long long int ll; typedef unsigned long long int ull; #define endl '\n' #define PB push_back #define MP make_pair #define fs first #define sd second #define ALL(V) (V).begin(),(V).end() #define SORT(V) sort((V).begin(),(V).end()) #define REVERSE(V) reverse((V).begin(),(V).end()) #define EACH(V,i) for(typeof((V).begin()) i=(V).begin();i!=(V).end();++i) #define equals(a,b) (fabs((a)-(b))<EPS) #define INF ((1LL<<62)-(1LL<<31)) #define EPS 1 e-10 #define PI 3.141592653589793238 #define MOD 1000000007 #define MAX 5100000 using namespace std; inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;} template<class T>inline string toString(T x){ostringstream sout;sout<<x;return sout.str();} 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;} typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef pair<int, int> PII; typedef long long LL; const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0}; l gcd(l a, l b){return b?gcd(b,a%b):a;} l lcm(l a, l b){return a/gcd(a,b)*b;} void solve_test(){ int a , b , x , y ; l answer ; cin >> a >> b >> x >> y ; if(a == b){ cout << x <<endl ; return ; } if( a < b){ answer = x + (b - a ) * y ; answer = min(answer , (l)(2 *(b-a) + 1) * x ) ; cout << answer << endl; return ; } if(a > b ){ answer = x + ( a - b -1) * y ; answer = min(answer , (l)(2 *(a-b) -1) * x ) ; cout << answer << endl ; return ; } } int main(){ cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); l test = 1 ; //cin>>test; while(test--){ solve_test(); } return 0 ; }
#include <bits/stdc++.h> using namespace std; #define INIT std::ios::sync_with_stdio(false);std::cin.tie(0); #define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__); // __VA_ARGS__可変引数マクロ template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest& ...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } #define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i; #define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& R:c)for(auto& w:R)std::cin>>w; template<typename T>void MACRO_OUT(const T t) { std::cout << t; } template<typename First, typename...Rest>void MACRO_OUT(const First first, const Rest...rest) { std::cout << first << " "; MACRO_OUT(rest...); } #define OUT(...) MACRO_OUT(__VA_ARGS__); #define FOUT(n, dist) std::cout<<std::fixed<<std::setprecision(n)<<(dist); // std::fixed 浮動小数点の書式 / setprecision 浮動小数点数を出力する精度を設定する。 #define SP std::cout<<" "; #define BR std::cout<<"\n"; #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define ENDL std::cout<<std::endl; #define FLUSH std::cout<<std::flush; #define ALL(a) (a).begin(),(a).end() #define FOR(w, a, n) for(int w=(a);w<(n);++w) #define REP(w, n) for(int w=0;w<int(n);++w) #define IN(a, x, b) (a<=x && x<b) template<class T> inline T CHMAX(T & a, const T b) { return a = (a < b) ? b : a; } template<class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } template<class T> using V = std::vector<T>; template<class T> using VV = V<V<T>>; using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<int, int>; using PAIRLL = std::pair<ll, ll>; constexpr int INFINT = (1 << 30) - 1; // 1.07x10^ 9 constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9 constexpr ll INFLL = 1LL << 60; // 1.15x10^18 constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); // 9.22x10^18 constexpr double eps = 1e-7; constexpr int MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template<class T, size_t N> void FILL(T(&a)[N], const T & val) { for (auto& x : a) x = val; } // int a[5] = {1,2,3,4,5}; FILL(a,10); template<class ARY, size_t N, size_t M, class T> void FILL(ARY(&a)[N][M], const T & val) { for (auto& b : a) FILL(b, val); } // 2次元配列でもそのまま使える template<class T> void FILL(std::vector<T> & a, const T & val) { for (auto& x : a) x = val; } // 使い方 vector<int> a(3); FILL(a,10); template<class ARY, class T> void FILL(std::vector<std::vector<ARY>> & a, const T & val) { for (auto& b : a) FILL(b, val); } // 2次元配列でもそのまま使える int main() { INIT; VAR(ll,n) ll i =2; // V<bool> done(n +1); map<int,bool> done; ll ans =0; while(1){ if(i *i > n) break; if(done[i] !=1 ){ __int128_t out =0; ll j =2; while(1){ out = pow(i , j); if(out > n) break; done[out] = true; ans++; j++; } } i++; } OUT( n -ans) return 0; }
#include<bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define ll long long #define pb push_back #define mod 1000000007 #define ff first #define ss second #define pi 3.1415926535 #define endl '\n' using namespace std; struct custom { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; //using namespace __gnu_pbds; //typedef tree<ll,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set; const ll inf=4e10; /*ll power(ll x,ll y) { ll res=1; while(y>0) { if(y%2==1) res=(res*x)%mod; x=(x*x)%mod; y=(y>>1); } return res; }*/ int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); unordered_map<ll,ll,custom>mp; ll i,a,b,c,d,n,x,f=0; cin>>n; c=0; for(i=2;i<=1e5;i++) { a=i*i; while(a<=n) { if(mp[a]==0) { c++; mp[a]=1; } a=a*i; } } cout<<n-c<<endl; }
/*@author Vipen Loka*/ #include <bits/stdc++.h> #define endl '\n' #define ff first #define ss second #define ll long long #define vi vector<int> #define vll vector<ll> #define vvi vector < vi > #define pii pair<int,int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 1000000000000000001; #define deb(x) cout << #x << ':' << x << '\n'; using namespace std; template<class T>void show(vector<T> &a) {cerr << "[ "; for (int ij = 0; ij < (int)a.size(); ij++) {cerr << a[ij] << " ";} cerr << "]\n";} template<class T>void show(T a) {cerr << a << endl;} template<typename... T>void show(T... args) {((cerr << args << ' '), ...); cerr << endl;} void solve() { int i, j; map<int, int> mp; mp[0] = 0; mp[1] = 1; mp[6] = 9; mp[8] = 8; mp[9] = 6; string s; cin >> s; for (int i = s.size() - 1; i >= 0; i--) { int n = s[i] - '0'; cout << mp[n]; } cout << endl; } int32_t main(int32_t argc, char *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); int T = 1; // cin >> T; while (T--) { solve(); } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n ; cout << n - 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define SZ(x) ((int)(x).size()) #define FOR(var, begin, end) for (int var = (begin); var <= (end); var++) #define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--) #define REP(var, length) FOR(var, 0, length - 1) #define RREP(var, length) RFOR(var, length - 1, 0) #define EACH(value, var) for (auto value : var) #define SORT(var) sort(var.begin(), var.end()) #define REVERSE(var) reverse(var.begin(), var.end()) #define RSORT(var) SORT(var); REVERSE(var) #define OPTIMIZE_STDIO ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed #define endl '\n' const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; void solve(istream& cin, ostream& cout) { string s; cin >> s; int n = s.size(); REVERSE(s); map<char, char> mp; mp['0'] = '0'; mp['1'] = '1'; mp['6'] = '9'; mp['8'] = '8'; mp['9'] = '6'; string t = ""; REP(i, n) t += mp[s[i]]; cout << t << endl; } #ifndef TEST int main() { OPTIMIZE_STDIO; solve(cin, cout); return 0; } #endif
#include <cstdio> int n, a[300006]; long long res, sum; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i); for (int i = 0; i < n; i++) res += (n - 1LL) * a[i] * a[i]; for (int i = 0; i < n; i++) res -= 2 * a[i] * sum, sum += a[i]; printf("%lld", res); }
#include<bits/stdc++.h> #define rep(i,m,n) for (int i=m;i<n;i++) using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); int N,K,M; cin>>N>>K>>M; int sum=0; rep(i,0,N-1){ int data; cin>>data; sum +=data; } if(sum/N>=M){ cout<<0<<endl; return 0; } rep(i,0,K+1){ int d=(sum+i)/N==M; if(d) { cout<<i<<endl; return 0; } } cout<<-1<<endl; }
#include<bits/stdc++.h> using namespace std; #define foreach(i,n) for(auto&i:(n)) #define all(x) (x).begin(),(x).end() using ll = long long; template<class t> auto collect(int n,t f){ vector<decltype(f())> res(n); for(auto &i:res){ i = f(); } return res; } template<class t,class f> auto mapcar(t x,f func){ int n = x.size(); vector<typename t::value_type> res(n); auto itr = res.begin(); for(auto &i:x){ *itr = func(i); ++itr; } return res; } template<class t,class f> void mapc(t x,f func){ for(auto &i:x){ func(i); } } template<class t,class u> auto sellect(t x,u f){ vector<typename t::value_type> res; for(auto &i:x){ if(f(i)){ res.push_back(i); } } return res; } ll in(){ ll res; scanf("%lld",&res); return res; } int main(){ int n = in(); int k = in(); int m = in(); vector<ll> line = collect(n-1,in); ll ans = n * m - accumulate(all(line),0ll); if(ans<=k){ cout << max(0ll,ans) << endl; }else{ cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long const ll mod=1e9+7; const ll INF=4e18; const int inf=1e9; const double pi=acos(-1); ll n, ans; ll cek(ll pos, ll pjg){ return pjg*(2*pos+(pjg-1))/2; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for(int len=1;len<=2000000;++len){ ll l=1, r=n; while(l<=r){ ll mid=(l+r)/2; ll cur=cek(mid, len); if(cur==n){ ans+=2; break; } else if(cur>n){ r=mid-1; } else l=mid+1; } } cout << ans << '\n'; }
// #pragma GCC optimize("Ofast,unroll-loops") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2") #include "bits/stdc++.h" #define ll long long #define ul unsigned long long #define ld long double #define rep(i, a, b) for(int i=(a); i<(b); i++) #define repi(i, a, b) for(int i=(a); i>(b); i--) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define db(x) (cout<<#x<<":"<<(x)<<'\n') #define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr) #define gcd _gcd #define lcm(a,b) __detail::__lcm(a,b) #define goog(tno) cout << "Case #" << tno <<": " #define pb push_back #define endl "\n" #define iceil(n, x) (((n) + (x) - 1) / (x)) const ll mod = 1000000007; const ld pi = 3.1415926535897932384626433832795; const ll INF = 1e18; using namespace std; void solve() { ll n; cin >> n; ll cnt = 0; for (ll i = 1; (i * (i - 1)) / 2 < n; i++) { ll start = (n - ((i * (i - 1)) / 2)) / i; if ((start * i + (i * (i - 1)) / 2) == n) cnt++; } cout << cnt * 2 << endl; } int main() { fastio; int tz = 1; //cin >> tz; while (tz--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pi; #define PB push_back #define MP make_pair #define REP(i,a,b) for(int i = a; i<=b; i++) void print(stack<char> st){ while(!st.empty()){ cout << st.top() << " "; st.pop(); } } int main (){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n = 0; cin >> n; int a[n], b[n]; int maxa = INT_MIN , minb = INT_MAX; for(int i = 0; i < n; i++){ cin >> a[i]; maxa = max(maxa, a[i]); } for(int i = 0; i < n; i++){ cin >> b[i]; minb = min(minb, b[i]); } int ans = minb - maxa + 1; if(ans <= 0){ cout << 0 << "\n"; }else{ cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define ull unsigned long long #define loops(i, s, n) for (ll i = s; i < (ll)n; i++) #define loop(i, n) loops(i, 0, n) #define ALL(a) a.begin(), a.end() #define pub push_back #define pob pop_back #define mp make_pair #define dump(x) cerr << #x << " = " << (x) << endl; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef vector<int> v; typedef vector<double> vd; typedef vector<string> vs; typedef vector<ll> vl; inline int in_int() { int x; cin >> x; return x; } inline ll in_ll() { ll x; cin >> x; return x; } inline string in_str() { string x; cin >> x; return x; } // for dp // 第一引数と第二引数を比較し、第一引数(a)をより大きい/小さい値に上書き template <typename T> inline bool chmin(T &a, const T &b) { bool compare = a > b; if (a > b) a = b; return compare; } template <typename T> inline bool chmax(T &a, const T &b) { bool compare = a < b; if (a < b) a = b; return compare; } void _INPUT_IMPL_(){}; template <class T, class... Args> void _INPUT_IMPL_(T &dest, Args &...args) { cin >> dest; _INPUT_IMPL_(args...); } void _OUTPUT_IMPL_(){}; template <class T, class... Args> void _OUTPUT_IMPL_(T dest, Args... args) { cout << dest << (sizeof...(Args) ? " " : ""); _OUTPUT_IMPL_(args...); } #define in(type, ...) \ type __VA_ARGS__; \ _INPUT_IMPL_(__VA_ARGS__) #define in_v(type, name, cnt) \ vector<type> name(cnt); \ loop(i, cnt) cin >> name[i]; #define sort_v(v) std::sort(v.begin(), v.end()) #define unique_v(v) v.erase(std::unique(v.begin(), v.end()), v.end()) //必ずソート後に実行 #define out(...) (_OUTPUT_IMPL_(__VA_ARGS__), std::cout << std::endl) #define set_fix(x) ((std::cerr << std::fixed << std::setprecision(x)), (std::cout << std::fixed << std::setprecision(x))) //cout for vector template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "{"; for (int i = 0; i < (int)v.size(); i++) o << (i > 0 ? ", " : "") << v[i]; o << "}"; return o; } // gcd 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); } //prime numbers bool IsPrime(ll num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = sqrt(num); for (ll i = 3; i <= sqrtNum; i += (ll)2) { if (num % i == 0) { return false; } } return true; } vector<ll> Eratosthenes(ll N) { int arr[N]; vector<ll> res; for (int i = 0; i < N; i++) { arr[i] = 1; } for (int i = 2; i < sqrt(N); i++) { if (arr[i]) { for (int j = 0; i * (j + 2) < N; j++) { arr[i * (j + 2)] = 0; } } } for (int i = 2; i < N; i++) { if (arr[i]) { res.push_back(i); } } return res; } //digit number int GetDigit(ll num, ll radix) { unsigned digit = 0; while (num != 0) { num /= radix; digit++; } return digit; } int digsum(ll n) { ll res = 0; while (n > 0) { res += n % (ll)10; n /= (ll)10; } return res; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int n; cin >> n; set<int> a; for (int i = 0; i < n; i++) { int ai; cin >> ai; a.insert(ai); } int mn = *a.begin(); int mx = *a.rbegin(); dump(mx); dump(mn); int cnt = 0; while (mx != mn) { a.erase(mx); a.insert(mx - mn); mn = *a.begin(); mx = *a.rbegin(); cnt++; // cout << "cnt: " << cnt << " mx: " << mx << " mn: " << mn << endl; } cout << mx << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define _1a1ee int t;cin>>t;for(int tt=0; tt<t; ++tt) #define MOD 1000000007 #define pb push_back #define ppb pop_back #define mp make_pair #define ff first #define ss second #define br cout <<'\n'; #define sz(x) ((int)(x).size()) #define lcm(x,y) (x*y/__gcd(x,y)) #define PI 3.141592653589793238462 #define all(x) (x).begin(), (x).end() #define f(i,k,n) for(long long int i=k; i<n; ++i) #define fr(i,k,n) for(long long int i=k; i>=n; --i) #define ni1(a) ll a;cin>>a; #define ni2(a,b) ll a,b;cin>>a>>b #define ni3(a,b,c) ll a,b,c;cin>>a>>b>>c #define ni4(a,b,c,d) ll a,b,c,d;cin>>a>>b>>c>>d #define ni5(a,b,c,d,e) ll a,b,c,d,e;cin>>a>>b>>c>>d>>e #define ni6(a,b,c,d,e,f) ll a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f typedef long long int ll; typedef unsigned long long ull; typedef long double lld; typedef vector<int> vi; typedef vector<long long int> vll; ///////////////////////////////////////////////---#--DEBUG--#---////////////////////////////////////////////////////////////// #ifndef ONLINE_JUDGE #define debug(x) cerr << #x <<" "; _print(x); cerr << endl; #else #define debug(x) #endif void _print(ll t) {cerr << t;} void _print(int t) {cerr << t;} void _print(string t) {cerr << t;} void _print(char t) {cerr << t;} void _print(lld t) {cerr << t;} void _print(double t) {cerr << t;} void _print(ull t) {cerr << t;} template <class T, class V> void _print(pair <T, V> p); template <class T> void _print(vector <T> v); template <class T> void _print(set <T> v); template <class T, class V> void _print(map <T, V> v); template <class T> void _print(multiset <T> v); template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";} template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";} template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";} //////////////////////////////////////////////---#--MAIN-CODE--#---/////////////////////////////////////////////////////////// long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } void solve(){ ni1(n); ll ans = 1.08*n; //cout<<ans; if(ans < 206)cout<<"Yay!"; else if(ans == 206)cout<<"so-so"; else if(ans > 206)cout<<":("; } int main() { #ifndef ONLINE_JUDGE freopen("Error.txt", "w", stderr); #endif fast clock_t start,end; start = clock(); {solve();} end = clock(); double time_taken = double(end-start) / double(CLOCKS_PER_SEC); #ifndef ONLINE_JUDGE cerr << "Time: " << (time_taken*1000) << "\n"; #endif return 0; }
#include <iostream> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int main(){ int N; cin >> N; N = N * 108 / 100; if(N < 206) cout << "Yay!"; else if(N == 206) cout << "so-so"; else cout << ":("; cout << endl; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_inst;
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; void chmin(int& a, int b){ if(a > b){ a = b; } } int main(){ int N, M; cin >> N >> M; vector<int> H(N); for(auto& h : H) cin >> h; vector<int> W(M); for(auto& w : W) cin >> w; sort(H.begin(), H.end()); vector<int> sum1((N + 1) / 2), sum2((N + 1) / 2); for(int i = 0; i + 1 < N; i += 2) sum1[i / 2 + 1] = sum1[i / 2] + H[i + 1] - H[i]; for(int i = N - 2; i > 0; i -= 2) sum2[i / 2] = sum2[i / 2 + 1] + H[i + 1] - H[i]; int ans = INF; for(int w : W){ int x = lower_bound(H.begin(), H.end(), w) - H.begin(); if(x & 1) x ^= 1; chmin(ans, sum1[x / 2] + sum2[x / 2] + abs(H[x] - w)); } cout << ans << 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 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 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;} int main(){ INT(n); set<ll> Set; ll x,maxv,minv; rep(i,n){ in(x); Set.insert(x); } while(true){ if(Set.size()==1)break; maxv=*Set.rbegin(); minv=*Set.begin(); Set.erase(maxv); Set.insert(maxv-minv); } out((ll)*Set.begin()); }
// author : lynmisakura // TIMESTAMP: 2020-10-17, 20:07:37 #include<bits/stdc++.h> using namespace std; #define REP(i,n) for(int i = 0;i < n;i++) #define RNG(i,s,n) for(int i = s;i < n;i++) #define _RNG(i,s,e) for(int i=s;i>=e;i--) #define mp make_pair #define pb push_back #define eb emplace_back #define dup(x,y) (x + y - 1) / (y) #define all(x) (x).begin(),(x).end() #define lb(x,key) lower_bound(all(x) , (key)) #define ub(x,key) upper_bound(all(x) , (key)) template<class T> bool chmax(T& a,T b){ if(a < b){ a = b; return true; }else return false; } template<class T> bool chmin(T& a,T b){ if(a > b){ a = b; return true; }else return false; } using ll = long long; using ld = long double; using vi = vector<int>; using vl = vector<ll>; using pi = pair<int,int>; using pl = pair<ll,ll>; using vpi = vector<pi>; using vpl = vector<pl>; #define debug(arr) cout << #arr << " = " << arr << '\n' #define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n" #define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n" template<class T> ostream &operator << (ostream& out, const vector<T>& arr) { cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}"; return out; } template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) { cout << "{\n"; for (auto& vec : arr)cout << " " << vec << ",\n"; cout << "}"; return out; } template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){ cout << "{" << p.first << "," << p.second << "}" << '\n'; return out; } template<class T> istream &operator >> (istream& in, vector<T>& arr) { for (auto& i : arr)cin >> i; return in; } template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){ cin >> p.first >> p.second; return in; } #define F first #define S second ///////////////////////////////////////////////////////////////////////// ll x,y,a,b; ll ans; map<ll,ll> M; int cnt = 0; void dfs(ll tmp,ll cur = 0){ //if(cnt++ <= 20) cout << tmp << ' ' << cur << endl; if(tmp >= y){ return; } chmax(ans , cur + (y - 1 - tmp) / b); if((y / tmp) <= x) return; if(!M.count(a * tmp) || (M[a * tmp] < cur + 1)){ M[a * tmp] = cur + 1; dfs(a * tmp , cur + 1); } //if(!M.count(b + tmp) || (M[b + tmp] < cur + 1)){ // M[b + tmp] = cur + 1; // dfs(b + tmp , cur + 1); //} } int main(void){ cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(20); cin >> x >> y >> a >> b; dfs(x); cout << ans << '\n'; return 0; }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; #define fo(i,a,b) for(int i=a;i<=b;++i) #define fod(i,a,b) for(int i=a;i>=b;--i) #define jx 1000000 char s[100001]; int len,f[26]; int main(){ cin>>len; scanf("%s",s); fo(i,0,25)f[i]=jx; f[s[0]-'a']=0; if(len==1){ printf("-1"); return 0; } fo(i,1,len-2){ int u=jx; fo(j,0,25){ if(j!=(s[i]-'a'))u=min(u,f[j]+1); } f[s[i+1]-'a']=min(u,f[s[i+1]-'a']); } int u=jx; fo(j,0,25){ if(j!=(s[len-1]-'a'))u=min(u,f[j]+1); } if(u>=jx)printf("-1"); else printf("%d",u); return 0; }
#include <iostream> #include <string> #include <vector> #include <map> #include <set> #include <utility> #include <algorithm> #include <cmath> #include <climits> #include <iomanip> #include <queue> #include <stack> #include <ctype.h> using namespace std; typedef long long ll; const int INF = (1<<30)-1; const ll LINF = 1e18; #define rep(i, n) for (int i = 0; i < n; i++) template<class T> bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;} template<class T> bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;} int main() { int n, m, k; cin >> n >> m >> k; vector<int> a(n+1, 0); rep(i, k) { int x; cin >> x; a[x] = 1; } for (int i = 0; i < n+1; i++) { int j = i; while (j < n+1 && a[j] == 1) j++; if (j - i >= m) { cout << -1 << endl; return 0; } i = j; } vector<double> b(n+m+10), c(n+m+10), sb(n+m+10, 0), sc(n+m+10, 0); for (int i = n-1; i >= 0; i--) { if (a[i]) { b[i] = 1; c[i] = 0; } else { b[i] = (sb[i+1] - sb[i+m+1]) / m; c[i] = (sc[i+1] - sc[i+m+1]) / m + 1; } sb[i] = b[i] + sb[i+1]; sc[i] = c[i] + sc[i+1]; } cout << fixed << std::setprecision(15) << c[0] / (1 - b[0]) << endl; return 0; } //小数点精度 //cout << fixed << std::setprecision(15) << y << endl;
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) using ll = long long; const double eps = 1e-10; const double INF = 1e9; class Union_Find{ vector<int> par; vector<int> rank; vector<int> num; public: Union_Find(int n) { par = vector<int>(n); rank = vector<int>(n,0); num = vector<int>(n,1); rep(i,n) par[i] = i; } int find(int x) { if(par[x] == x) return x; else return par[x] = find(par[x]); } int number(int x) { return num[find(x)]; } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) rank[x]++; par[y] = x; num[x] += num[y]; } bool same(int x, int y) { return find(x) == find(y); } }; int main(){ int n; cin >> n; double x[n], y[n]; rep(i,n) cin >> x[i] >> y[i]; double l = 0, r = 100.0; while(r - l > eps) { double m = (l + r) / 2; Union_Find uf(n+2); rep(i,n) { if(y[i]+m > -m+100.0) uf.unite(n,i); if(y[i]-m < m-100.0) uf.unite(n+1,i); } rep(i,n) rep(j,n) if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]) < 4.0*m*m) uf.unite(i,j); if(uf.find(n) == uf.find(n+1)) r = m; else l = m; } printf("%.12lf\n",l); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define REP(NAME, NUM) for (int NAME = 0; NAME < int(NUM); ++NAME) #define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME) #define ALL(NAME) (NAME).begin(), (NAME).end() #define cMOD 1000000007ULL #define cINF ((1ll<<62)-1) #define cINFINT ((1<<30)-1) template <typename T> void chmin(T& a, T b){ if(a > b) a = b; } template <typename T> void chmax(T& a, T b){ if(a < b) a = b; } int main() { cin.tie( 0 ); ios::sync_with_stdio( false ); // ---------------------------------------------------------------- ull n = 0; string s; cin >> n >> s; ++n; vector<ll> vec( n, 0 ); REP(i, n) cin >> vec[i]; ll k = 100000; REP(i, n-1) chmin(k, abs(vec[i+1] - vec[i])); cout << k << endl; REP(kk, k) REP(i, n) { cout << (vec[i]+kk)/k << ((i==n-1)?"\n":" "); } // ---------------------------------------------------------------- return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 105; int n, A[N], k = INT_MAX; int main() { scanf("%d%*s", &n); for(int i = 0; i <= n; ++i) scanf("%d", &A[i]), k = min(k, i ? abs(A[i] - A[i - 1]) : k); printf("%d\n", k); for(int i = 1; i <= k; ++i) for(int j = 0; j <= n; ++j) printf("%d%c", A[j] / k + (A[j] % k >= i), " \n"[j == n]); return 0; }
#include <bits/stdc++.h> #include <iostream> #include <queue> #include <stack> #include <vector> #include <string> #include <set> #include <map> #include <random> #define rep(i,n) for (int i = 0; i < (n); ++i) #define repp(i,n,m) for (int i = m; i < (n); ++i) using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; using PI = pair<pair<int,int>,int>; using PL = pair<long long, long long>; using PLL = pair<pair<long long, long long>, long long>; using Pxy = pair<long double, long double>; const int INF = 2001001007; const int modd = 1000000007; const long long modl = 1000000007LL; const long long mod = 998244353LL; const ll inf = 1e18; template <typename SA> void priv(vector<SA> &ar){ rep(i,ar.size()-1) cout << ar[i] << " "; cout << ar[ar.size()-1] << endl; } template <typename SB> void privv(vector<vector<SB>> &ar){ rep(i,ar.size()){ rep(j,ar[i].size()-1) cout << ar[i][j] << " "; cout << ar[i][ar[i].size()-1] << endl; } } template <typename SC> bool range(SC a, SC b, SC x){return (a <= x && x < b);} bool rrange(P a, P b, P xy){ bool s = range(a.first,b.first,xy.first); bool t = range(a.second,b.second,xy.second); return (s && t); } template <typename SD> void sor(vector<SD> &ar){sort(ar.begin(),ar.end());} template <typename SE> void rev(vector<SE> &ar){reverse(ar.begin(),ar.end());} template <typename SF> bool chmin(SF &a, const SF &b){if(a>b){a = b; return true;} return false;} template <typename SG> bool chmax(SG &a, const SG &b){if(a<b){a = b; return true;} return false;} template <typename SH> void eru(vector<SH> &ar){sor(ar);ar.erase(unique(ar.begin(),ar.end()),ar.end());} void yes(){cout << "Yes" << endl;} void no (){cout << "No" << endl;} void yn (bool t){if(t)yes();else no();} ll cel (ll a, ll b){ if (a % b == 0) return a / b; else return a / b + 1; } ll gcds(ll a, ll b){ ll c = a % b; while (c != 0){ a = b; b = c; c = a % b; } return b; } vector<string> saiki(int n){ if (n == 1){ vector<string> ans = {"AB"}; return ans; } vector<string> a = saiki(n/2); vector<string> ans(2*n-1); string c = ""; rep(i,n) c += 'A'; rep(i,n) c += 'B'; ans[0] = c; rep(i,n-1){ string d = a[i]; d += a[i]; ans[i+1] = d; } rep(i,n-1){ string d = a[i]; rep(j,n){ if (d[j] == 'A') d += 'B'; else d += 'A'; } ans[n+i] = d; } return ans; } int main(){ int n; cin >> n; int m = 1; rep(i,n-1) m *= 2; auto ans = saiki(m); cout << ans.size() << endl; rep(i,ans.size()) cout << ans[i] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define fi first #define se second #define mp make_pair #define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int main(){ fastIO; int n; cin >> n; vector<vector<int>> sol; sol.push_back({0,1}); for(int i = 2; i <= n; i ++ ){ vector<vector<int>> nw; for(auto x : sol){ nw.push_back({}); for(auto r : x){ nw.back().push_back(r); } for(auto r : x){ nw.back().push_back(r); } nw.push_back({}); for(auto r : x){ nw.back().push_back(r); } for(auto r : x){ nw.back().push_back((r^1)); } } nw.push_back({}); for(int j = 0 ; j < (1 << i); j ++ ){ if(j < (1 << (i - 1))){ nw.back().push_back(0); } else{ nw.back().push_back(1); } } sol = nw; } cout << sol.size() << "\n"; for(auto p : sol){ for(auto q : p){ cout << char(q+'A'); } cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long using vec_int = vector<int>; using P = pair<int,int>; using T = tuple<int,int,int>; using ll = long long; #define rep(i, n) for(int i = 0; i < (int)(n); i++) int charToInt(char c){ char zero_num = '0'; return (int)c - (int)zero_num; } signed main(){ int H; cin>>H; int W; cin>>W; vector<string> S(H); rep(i,H)cin>>S.at(i); int ans = 0; for(int i=1;i<H;i++){ for(int j=1; j<W;j++ ){ if((S.at(i).at(j)=='#'&&S.at(i).at(j-1)=='.')){ if(!(S.at(i-1).at(j)=='#'&&S.at(i-1).at(j-1)=='.')){ ans++; } } if((S.at(i).at(j)=='.'&&S.at(i).at(j-1)=='#')){ if(!(S.at(i-1).at(j)=='.'&&S.at(i-1).at(j-1)=='#')){ // if(S.at(i-1).at(j-1)=='#'){ ans++; } } } } for(int j=1; j<W;j++ ){ for(int i=1;i<H;i++){ if((S.at(i).at(j)=='#'&&S.at(i-1).at(j)=='.')){ if(!(S.at(i).at(j-1)=='#'&&S.at(i-1).at(j-1)=='.')){ ans++; } } if((S.at(i).at(j)=='.'&&S.at(i-1).at(j)=='#')){ if(!(S.at(i).at(j-1)=='.'&&S.at(i-1).at(j-1)=='#')){ ans++; } } } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i) #define rep(i, n) drep(i, 0, n - 1) #define P pair<ll, ll> int main(){ ll K; cin >> K; string S, T; cin >> S >> T; vector<ll> card(9); vector<ll> a_hand(9), t_hand(9); rep(i, 9) card.at(i) = K; rep(i, 4){ card.at(S[i]-'0'-1)--; t_hand.at(S[i]-'0'-1)++; card.at(T[i]-'0'-1)--; a_hand.at(T[i]-'0'-1)++; } vector<P> conbi; for(ll a=1; a<=9; a++){ if(card.at(a-1) == 0) continue; ll a_score = 0; card.at(a-1)--; a_hand.at(a-1)++; rep(i, 9) a_score += (i+1)*pow(10, a_hand.at(i)); for(ll t=1; t<=9; t++){ if(card.at(t-1) == 0) continue; ll t_score = 0; t_hand.at(t-1)++; rep(i, 9) t_score += (i+1)*pow(10, t_hand.at(i)); //cout << "a_score:" << a_score << ", t_score:" << t_score << endl; if(a_score < t_score) conbi.push_back(make_pair(a, t)); t_hand.at(t-1)--; } a_hand.at(a-1)--; card.at(a-1)++; } double ans = 0; for(ll i=0; i<conbi.size(); i++){ double first_p = (double)card.at(conbi.at(i).first-1)/(9*(double)K-8); card.at(conbi.at(i).first-1)--; double second_p = (double)card.at(conbi.at(i).second-1)/(9*(double)K-9); ans += first_p*second_p; card.at(conbi.at(i).first-1)++; } cout << fixed << setprecision(10) << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<endl #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;} template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);} template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;} const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1}; typedef pair<ll,ll> P; vector<string> C(vector<string> s) { ll n=s.size(); rep(i,n)REP(j,i+1,n) swap(s[i][j],s[j][i]); return s; } ll n,m; string s[1000]; ll calc(vector<string> t) { ll cnt=0; rep(k,m) { ll f=0; rep(l,2) { rep(i,n) { rep(j,n) { if(t[i].substr(0,s[k].size())==s[k]) f=1; rotate(t[i].begin(),t[i].begin()+1,t[i].end()); } } t=C(t); } cnt+=f; } return cnt; } vector<string> ans; ll MM; void solve(vector<string> t) { ll d=calc(t); if(MM<d) { MM=d; ans=t; } rep(k,5) { random_shuffle(all(t)); rep(i,n) rotate(t[i].begin(),t[i].begin()+rand()%n,t[i].end()); ll d=calc(t); if(MM<d) { MM=d; ans=t; } } } void Main() { srand((unsigned)time(NULL)); cin >> n >> m; rep(i,m) R s[i]; vector<string> v[15]; rep(i,m) v[s[i].size()].pb(s[i]); rep(e,30) { vector<string> t(n); rrep(i,15) { random_shuffle(all(v[i])); rep(j,v[i].size()) { ll M=MAX,x=0; string rr; rep(k,n) { rep(l,t[k].size()+1) { string r1=t[k].substr(l); if(r1.size()<=v[i][j].size()) { string r2=v[i][j].substr(0,r1.size()); if(r1==r2) { ll d=t[k].size()+v[i][j].size()-r1.size(); if(d<=n&&M>d) { M=d; x=k; rr=t[k]+v[i][j].substr(r1.size()); } } } } } if(rand()%5==0) M=MAX; if(M!=MAX) t[x]=rr; } } rep(i,n) { while(t[i].size()<n) { //t[i]+='.'; t[i]+=(char)(rand()%8+'A'); } } solve(t); } rep(i,n) pr(ans[i]); } int main(){Main();return 0;}
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<vector> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<deque> #include<iomanip> #include<tuple> #include<cassert> #include<set> #include<complex> using namespace std; typedef long long int LL; typedef pair<int,int> P; typedef pair<LL,int> LP; const int INF=1<<30; const LL MAX=1e9+7; void array_show(int *array,int array_n,char middle=' '){ for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n')); } void array_show(LL *array,int array_n,char middle=' '){ for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n')); } void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){ if(vec_n==-1)vec_n=vec_s.size(); for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n')); } void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){ if(vec_n==-1)vec_n=vec_s.size(); for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n')); } vector<int> t; LL su[220000][2]; int main(){ int n,m; int i,j,k; LL a,b,c; LL s=1LL<<60; cin>>n>>m; for(i=0;i<n;i++){ cin>>a; t.push_back(a); } sort(t.begin(),t.end()); for(i=0;i<n;i++){ su[i+1][0]=su[i][0]; if(i%2)su[i+1][0]+=t[i]-t[i-1]; } for(i=n-1;i>=0;i--){ su[i][1]=su[i+1][1]; if(i%2)su[i][1]+=t[i+1]-t[i]; } vector<int> v1; for(i=0;i<m;i++){ cin>>a; v1.push_back(a); } sort(v1.begin(),v1.end()); for(i=0,j=0;i<m;i++){ for(;j<n && t[j]<v1[i];j++); a=su[j][0]+su[j][1]; if(j%2)a+=v1[i]-t[j-1]; else a+=t[j]-v1[i]; s=min(s,a); } cout<<s<<endl; }
#include<bits/stdc++.h> using namespace std; typedef long long LL; const int maxn=1e5+7; const LL MOD=1e9+7; LL rec[maxn]; int main() { int len;cin >> len; string arr; LL ans=1; rec[3]=1;rec[4]=2;rec[2]==1; for(int i=0;i<4;++i) { char tem; cin >> tem; arr+=tem; } if(len<4||arr[0]=='A'&&arr[1]=='A'||arr[1]=='B'&&arr[3]=='B'){cout << 1;return 0;} for(int i=5;i<=len;i++) rec[i]=(rec[i-1]+rec[i-2])%MOD; for(int i=4;i<=len;++i) ans=(ans<<1)%MOD; if(arr=="BAAB"||arr=="BBBA"||arr=="ABBA"||arr=="BAAA") { cout <<rec[len]; return 0; } else cout << ans; return 0; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll t,i,endi=0,starti=0; string s; cin>>s; string ss; for(i=s.size()-1;i>=0;i--) { ss.push_back(s[i]); } if(ss==s)cout<<"Yes"<<endl; else{ ss={}; //if() for(i=s.size()-1;i>=0;i--) { if(s[i]!='0')break; else endi++; } for(i=0;i<s.size();i++) { if(s[i]!='0')break; else starti++; } string xs; if(endi==0)cout<<"No"<<endl; else { if(endi>starti) { for(i=starti;i!=endi;i++) { xs.push_back('0'); } xs+=s; for(i=xs.size()-1;i>=0;i--) { ss.push_back(xs[i]); } if(ss==xs)cout<<"Yes"<<endl; else cout<<"No"<<endl; } else cout<<"No"<<endl; } } }
#include<bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/detail/standard_policies.hpp> // using namespace __gnu_pbds; #pragma GCC optimize("O3") #ifdef LOCAL #include "/Users/lbjlc/Desktop/coding/debug_utils.h" #else #define print(...) ; #define printn(...) ; #define printg(...) ; #define fprint(...) ; #define fprintn(...) ; #endif #define rep(i, a, b) for(auto i = (a); i < (b); i++) #define rrep(i, a, b) for(auto i = (a); i > (b); i--) #define all(v) (v).begin(), (v).end() #define pb push_back // #define mp make_pair #define fi first #define se second #define maxi(x, y) x = max(x, y) #define mini(x, y) x = min(x, y) // long long fact(long long n) { if(!n) return 1; return n*fact(n-1); } // #define endl '\n' mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int get_random() { static uniform_int_distribution<int> dist(0, 1e9 + 6); return dist(rng); } #define solve_testcase int T;cin>>T;for(int t=1;t<=T;t++){solve(t);} typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<pdd> vpdd; typedef vector<long long> vll; #define bd(type,op,val) bind(op<type>(), std::placeholders::_1, val) template<class T> void make_unique(T & v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } int geti() { int x; cin >> x; return x; } long long getll() { long long x; cin >> x; return x; } double getd() { double x; cin >> x; return x; } // pair<int, int> a(geti(), geti()) does not work // pair<int, int> a({geti(), geti()}) works, since it uses initializer. const int MAXN = 3e5 + 100; void solve(int tt) { // cout<<"Case #"<<tt<<": "; } int main(int argc, char * argv[]) { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); // solve_testcase; int n; cin>>n; string s,t; cin>>s; cin>>t; int cs=0,ct=0; for(auto c:s) cs+=(c=='1'); for(auto c:t) ct+=(c=='1'); print(cs,ct); if(cs<ct||(cs-ct)%2) { cout<<-1<<endl; return 0; } ll res=0; int i=0,j=0; while(1) { for(;i<n&&s[i]=='0';i++); for(;j<n&&t[j]=='0';j++); print(i,j); if(i==n&&j==n) break; if(i==n) { cout<<-1<<endl; return 0; } if(i>=j) { res+=i-j; i++;j++; } else { int i1=i+1; for(;i1<n&&s[i1]=='0';i1++); if(i1==n) { cout<<-1<<endl; return 0; } res+=i1-i; i=i1+1; } } cout<<res<<endl; return 0; }
#include <stdio.h> #include <string.h> #include <vector> #include <set> #include <queue> #include <string> #include <map> #include <stdlib.h> #include <time.h> #include <algorithm> #include <iostream> #include <cmath> #include <functional> #include <numeric> const int MAXN = 500010; char str_s[MAXN]; char str_t[MAXN]; std::vector<int> vec_index_s; std::vector<int> vec_index_t; int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int t; int n, m, k, x; //scanf(" %d", &t); //while (t--) { scanf("%d", &n); scanf(" %s", str_s); scanf(" %s", str_t); int cnt1 = 0; int cnt2 = 0; for (int i = 0; i < n; i++) { if (str_s[i] == '1') { cnt1++; vec_index_s.push_back(i); } } for (int i = 0; i < n; i++) { if (str_t[i] == '1') { cnt2++; vec_index_t.push_back(i); } } if (cnt2 > cnt1 || (cnt1 - cnt2) % 2) { printf("-1\n"); return 0; } int index_now_s = 0; int index_now_t = 0; bool tag = true; long long res = 0; while (index_now_s < vec_index_s.size()) { if (index_now_t < vec_index_t.size() && vec_index_s[index_now_s] < vec_index_t[index_now_t]) { index_now_s++; if (index_now_s >= vec_index_s.size()) { break; } res += (vec_index_s[index_now_s] - vec_index_s[index_now_s - 1]); index_now_s++; if (index_now_s >= vec_index_s.size()) { break; } } else { if (index_now_t < vec_index_t.size()) { res += (vec_index_s[index_now_s] - vec_index_t[index_now_t]); index_now_s++; index_now_t++; if (index_now_s >= vec_index_s.size()) { break; } } else { index_now_s++; if (index_now_s >= vec_index_s.size()) { tag = false; break; } res += (vec_index_s[index_now_s] - vec_index_s[index_now_s - 1]); index_now_s++; if (index_now_s >= vec_index_s.size()) { break; } } } } if ((index_now_s >= vec_index_s.size() && index_now_t < vec_index_t.size()) || !tag) { printf("-1\n"); return 0; } printf("%lld\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; using i64 = long long; using P = pair<i64, i64>; using vec = vector<int>; using mat = vector<vector<int>>; #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define endl "\n" constexpr int MOD = 1000000007; constexpr int INF = 1001001001; constexpr bool is_multiple = false; using State = tuple<P, P, P>; void flip_x(P &pos, i64 p) { i64 dx = abs(pos.first - p); if (pos.first > p) { pos.first -= 2 * dx; } else { pos.first += 2 * dx; } } void flip_y(P &pos, i64 p) { i64 dy = abs(pos.second - p); if (pos.second > p) { pos.second -= 2 * dy; } else { pos.second += 2 * dy; } } void print_state(State s) { P e0 = get<0>(s); P ex = get<1>(s); P ey = get<2>(s); printf("Origin: %lld %lld\n", e0.first, e0.second); printf("Ex: %lld %lld\n", ex.first, ex.second); printf("Ey: %lld %lld\n", ey.first, ey.second); } void solve() { int n; cin >> n; vector<i64> x(n), y(n); rep(i, n) cin >> x[i] >> y[i]; int m; cin >> m; // (0, 0), (1, 0), (0, 1) vector<State> states(m+1); states[0] = {{0, 0}, {1, 0}, {0, 1}}; rep(i, m) { int t, p = -1; cin >> t; State old = states[i]; P e0 = get<0>(old); P ex = get<1>(old); P ey = get<2>(old); if (t == 1) { e0 = {e0.second, -e0.first}; ex = {ex.second, -ex.first}; ey = {ey.second, -ey.first}; } else if (t == 2) { e0 = {-e0.second, e0.first}; ex = {-ex.second, ex.first}; ey = {-ey.second, ey.first}; } else { cin >> p; if (t == 3) { flip_x(e0, p); flip_x(ex, p); flip_x(ey, p); } else { flip_y(e0, p); flip_y(ex, p); flip_y(ey, p); } } states[i+1] = {e0, ex, ey}; // print_state(states[i+1]); } int q; cin >> q; vec a(q), b(q); rep(i, q) { cin >> a[i] >> b[i]; b[i]--; i64 sx = x[b[i]]; i64 sy = y[b[i]]; State state = states[a[i]]; P e0 = get<0>(state); P ex = get<1>(state); P ey = get<2>(state); i64 gx, gy; gx = e0.first + (ex.first - e0.first) * sx + (ey.first - e0.first) * sy; gy = e0.second + (ex.second - e0.second) * sx + (ey.second - e0.second) * sy; printf("%lld %lld\n", gx, gy); } } int main() { int t = 1; if (is_multiple) cin >> t; while (t--) solve(); return 0; }
#include<cstdio> #include<algorithm> #define int long long using namespace std; const int N = 2e5 + 50; struct Point{ int x, y; }p[N]; int n, m, Q; int r[N], x[N], y[N], nx[N], ny[N]; signed main(){ scanf("%lld", &n); for(int i = 1; i <= n; ++i) scanf("%lld%lld", &p[i].x, &p[i].y); scanf("%lld", &m); for(int i = 1, opt; i <= m; ++i){ scanf("%lld", &opt); r[i] = r[i - 1], x[i] = x[i - 1], y[i] = y[i - 1]; nx[i] = nx[i - 1], ny[i] = ny[i - 1]; if(opt == 1){ x[i] *= -1; swap(x[i], y[i]); if(nx[i] + ny[i] == 1) ++r[i]; else --r[i]; } else if(opt == 2){ y[i] *= -1; swap(x[i], y[i]); if(nx[i] + ny[i] == 1) --r[i]; else ++r[i]; } else{ int p; scanf("%lld", &p); if(opt == 3) x[i] = 2 * p - x[i], nx[i] ^= 1; else y[i] = 2 * p - y[i], ny[i] ^= 1; } } scanf("%lld", &Q); while(Q--){ int A, B; scanf("%lld%lld", &A, &B); int dx = p[B].x; int dy = p[B].y; r[A] = (r[A] % 4 + 4) % 4; if(r[A] == 1) dy *= -1, swap(dx, dy); else if(r[A] == 2) dx *= -1, dy *= -1; else if(r[A] == 3) dx *= -1, swap(dx, dy); if(nx[A]) dx *= -1; if(ny[A]) dy *= -1; printf("%lld %lld\n", dx + x[A], dy + y[A]); } return 0; }
#include <stack> #include <queue> #include <set> #include <vector> #include <map> #include <unordered_map> #include <unordered_set> #include <string> #include <utility> #include <climits> #include <algorithm> #include <numeric> #include <cmath> #include <cstring> #include <iostream> #include <fstream> #include <iomanip> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef pair<ll,int> pli; typedef pair<int,ll> pil; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vl; #define mp make_pair #define fr first #define sc second #define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl int main(int argc, char const *argv[]) { int x,y,a,b,n,m,k,j; cin>>n>>m; vi arr(n); vi buf(n,0); set<int> s; for (int i = 0; i <= n; i++) { s.insert(i); } int res=n; int i=0; for (; i < m; i++) { cin>>a; arr[i]=a; buf[a]++; s.erase(a); } res=min(res,*s.begin()); for ( ; i < n; i++) { cin>>a; arr[i]=a; s.erase(a); buf[a]++; buf[arr[i-m]]--; if(buf[arr[i-m]]==0){ s.insert(arr[i-m]); } res=min(res,*s.begin()); } cout<<res<<endl; return 0; }
#include <bits/stdc++.h> #define range(i, a, b) for(int i = (a); i < (b); i++) #define rep(i, a) range(i, 0, a) using namespace std; int main() { int n, m; cin >> n >> m; vector <int> a(n); vector <vector <int>> pos(n); rep (i, n) { cin >> a[i]; pos[a[i]].push_back(i); } int ans = 0; rep (i, n) { if (pos[i].size() == 0) break; rep (j, pos[i].size()) { int tmp = j == 0 ? pos[i][j] - (-1) : pos[i][j] - pos[i][j - 1]; if (tmp > m) { cout << i << endl; return 0; } } if (n - pos[i].back() > m) break; ans += 1; } cout << ans << endl; return 0; }
#include<iostream> using namespace std; int main() { int x1,x2,x3; cin>>x1>>x2>>x3; if(x1== x2) { cout<<x3; } else if(x1==x3) { cout<<x2; } else if(x2==x3) { cout<<x1; } else { cout<< 0; } }
#include<bits/stdc++.h> #define ll long long #define re register #define INF 2147483647 using namespace std; inline int read() { int f=1,x=0;char s=getchar(); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&s<='9') { x=x*10+s-48; s=getchar(); } return f*x; } int main() { int a=read(),b=read(),c=read(); int sum=a+b+c; if(a==b||a==c) { printf("%d\n",sum-2*a); return 0; } if(b==c) { printf("%d\n",sum-2*b); return 0; } cout<<0<<endl; return 0; }
#include<bits/stdc++.h> //#include<atcoder/all> //#include <boost/multiprecision/cpp_int.hpp> using namespace std; //using namespace atcoder; //using namespace boost::multiprecision; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(),A.end() #define RALL(A) A.rbegin(),A.rend() typedef unsigned long long ll; typedef pair<ll,ll> P; 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> T gcd(T a,T b){return b?gcd(b,a%b):a;} const ll mod=998244353; const ll LINF=1ll<<60; const int INF=1<<30; int dx[]={0,1,0,-1,0,1,-1,1,-1}; int dy[]={0,0,1,0,-1,1,-1,-1,1}; using E = tuple<int, int, int>; int main(){ int h,w;cin >> h >> w; vector<vector<int>> a(h, vector<int> (w - 1)); for (int i = 0; i < h; i++) { for (int j = 0; j < w - 1; j++) { cin >> a[i][j]; } } vector<vector<int>> b(h - 1, vector<int> (w)); for (int i = 0; i < h-1; i++) { for (int j = 0; j < w; j++) { cin >> b[i][j]; } } priority_queue<E, vector<E>, greater<E>> pq; pq.push({0, 0, 0}); vector<vector<int>> dp(h + 1, vector<int> (w + 1, INF)); dp[0][0] = 0; while(not pq.empty()){ auto [c, y, x] = pq.top();pq.pop(); if(dp[y][x] < c) continue; if(x + 1 < w and chmin(dp[y][x + 1], c + a[y][x])){ pq.push({dp[y][x + 1], y, x + 1}); } if(x - 1 >= 0 and chmin(dp[y][x - 1], c + a[y][x - 1])){ pq.push({dp[y][x - 1], y, x - 1}); } if(y + 1 < h and chmin(dp[y + 1][x], c + b[y][x])){ pq.push({dp[y + 1][x], y + 1, x}); } for (int i = 1; i <= y; i++) { if(chmin(dp[y - i][x], c + i + 1)){ pq.push({dp[y - i][x], y - i, x}); } } } cout << dp[h - 1][w - 1] << endl; return 0; }
// E - 潜入 #include <bits/stdc++.h> using namespace std; #define vec vector using vi = vec<int>; using PR = pair<int,int>; #define rep(i,n) for(int i=0;i<(int)(n);++i) int V; // 頂点数 vec<vec<PR>> G; // [from]<to, cost> 隣接リスト int INF = 1e9; vi dijkstra(int s){ vi dist(V, INF); dist[s] = 0; priority_queue<PR, vec<PR>, greater<PR>> pq; pq.push({0, s}); while(pq.size()){ auto[from_d, from_v] = pq.top(); pq.pop(); if(dist[from_v] < from_d) continue; for(auto[to_u, to_d]:G[from_v]){ to_d += dist[from_v]; if(dist[to_u] <= to_d) continue; dist[to_u] = to_d; pq.push({to_d, to_u}); } } return dist; } int main(){ int R, C; cin>>R>>C; auto rc2id = [&](int r, int c){ return r*C + c; }; V = R*C*2; G.resize(V); int x, a, b; rep(r, R) rep(c, C-1){ cin>>x; a = rc2id(r, c), b = rc2id(r, c+1); G[a].emplace_back(b, x), G[b].emplace_back(a, x); } rep(r, R-1) rep(c, C){ cin>>x; G[rc2id(r, c)].emplace_back(rc2id(r+1, c), x); } rep(r, R) rep(c, C){ a = rc2id(r, c), b = rc2id(r, c+R*C); G[a].emplace_back(b, 1), G[b].emplace_back(a, 0); if(r != R-1) G[rc2id(r+1, c+R*C)].emplace_back(rc2id(r, c+R*C), 1); } cout<< dijkstra(0)[rc2id(R-1,C-1)] <<endl; }
#include <bits/stdc++.h> #define up(i,a,b) for(int (i) = (a);(i)<=(b);++(i)) #define down(i,b,a) for(int (i) = (b);i>=(a);--i) #define bits(x,i) (((x) >> (i)) & 1) #define mid ((l+r)/2) #define pr pair<int,int> using namespace std; int a[3]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> a[0] >> a[1] >> a[2]; sort(a,a+3); cout << a[1] + a[2]; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B, C; cin >> A >> B >> C; cout << A+B+C - min(A,min(B,C)) << endl; }
#include<bits/stdc++.h> using namespace std; int main() { int h,w; cin>>h>>w; char c; int in[h][w]; for(int i=0;i<h;i++) { for(int j=0;j<w;j++) { cin>>c; if(c=='.')in[i][j]=0; else in[i][j]=1; } } int ans=0; for(int i=1;i<h;i++) { for(int j=0;j<w-1;j++) { int sum=in[i-1][j]+in[i-1][j+1]+in[i][j]+in[i][j+1]; if(sum==1 || sum==3)ans+=1; } } cout<<ans; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> #define rep(i,n) for(int i = 0; i < (n); ++i) #define rep1(i,n) for(int i = 1; i <= (n); ++i) // #define MOD 998'244'353 // #define MOD 1'000'000'007 // #define INF 1'000'000'000'000'000'000 using namespace std; // using namespace atcoder; typedef long long ll; int main(){ int N; cin >> N; int a[N+1]; int b[N+1]; int c[N+1]; rep1(i, N) cin >> a[i]; rep1(i, N) cin >> b[i]; rep1(i, N) cin >> c[i]; a[0] = 0; b[0] = 0; c[0] = 0; ll ans = 0; map<int, ll> mpa; map<int, ll> mpbc; rep1(i, N){ mpa[a[i]]++; } rep1(j, N){ mpbc[b[c[j]]]++; } rep1(i, N){ ans += mpa[i] * mpbc[i]; } cout << ans << endl; return 0; }
#include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <iomanip> #include <functional> #include <bitset> #include <limits> #include <cstdio> #include <cmath> #include <cassert> #include <random> #ifdef DEBUG #include "library/Utility/debug.cpp" #else #define debug(...) #endif #define rep(i,n) for(int i=0;i<(n);++i) #define EL '\n' #define print(i) std::cout << (i) << '\n' #define all(v) (v).begin(), (v).end() using lnt = long long; struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO; template<typename T> using V = std::vector<T>; template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; } /*-*/ int main() { int n,m; std::cin >> n >> m; V<lnt> w(n); fill(w); std::map<lnt,lnt> map; lnt minload=1e18; rep(i,m) { lnt a,b; std::cin >> a >> b; map[b]=std::max(map[b],a); minload=std::min(minload,b); } rep(i,n) { if(w[i]>minload) { print(-1); return 0; } } V<std::pair<lnt,lnt> > v; v.emplace_back(0,0); for(auto e:map) v.emplace_back(e); { lnt max=0; int k=v.size(); rep(i,k) { max=std::max(max,v[i].second); v[i].second=max; } } V<int> p(n); std::iota(all(p),0); lnt min=1e18; std::sort(all(w)); do { V<V<std::pair<lnt,lnt> > > g(n); V<lnt> a(n); rep(i,n) a[i]=w[p[i]]; V<lnt> ac(n+1,0); rep(i,n) ac[i+1]=ac[i]+a[i]; rep(i,n) { for(int j=i+1;j<n;j++) { lnt x=ac[j+1]-ac[i]; int l=-1,r=v.size(); while(r-l>1) { int o=(l+r)>>1; if(v[o].first<x) l=o; else r=o; } g[i].emplace_back(j,v[l].second); } } auto dfs=[&](auto f, int i) -> lnt { lnt max=0; for(auto to:g[i]) { max=std::max(max,f(f,to.first)+to.second); } return max; }; lnt x=dfs(dfs,0); min=std::min(min,x); } while(std::next_permutation(all(p))); print(min); }
#include <bits/stdc++.h> using namespace std; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl 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) { for(auto it : P) { s << "<" << it << "> "; } return s << endl; } template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) { for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; } long long solve() { int N, M; cin >> N >> M; vector<long long> w(N); long long maxW = 0; for (int i = 0; i < N; ++i) { cin >> w[i]; chmax(maxW, w[i]); } vector<long long> l(M), v(M); for (int j = 0; j < M; ++j) { cin >> l[j] >> v[j]; if (v[j] < maxW) return -1; } // 前処理 vector<long long> dist(1<<N, 0); for (int bit = 0; bit < (1<<N); ++bit) { long long W = 0; for (int i = 0; i < N; ++i) { if (bit & (1<<i)) W += w[i]; } for (int j = 0; j < M; ++j) { if (v[j] < W) { chmax(dist[bit], l[j]); } } } // DP long long res = 1LL<<60; vector<int> ids(N); iota(ids.begin(), ids.end(), 0); do { //COUT(ids); vector<long long> dp(N, 0); for (int i = 1; i < N; ++i) { int bit = (1<<ids[i]); for (int j = i-1; j >= 0; --j) { bit |= (1<<ids[j]); chmax(dp[i], dp[j] + dist[bit]); } //cout << i << ": " << ids[i] << ", " << dp[i] << endl; } chmin(res, dp[N-1]); } while (next_permutation(ids.begin(), ids.end())); return res; } int main() { cout << solve() << endl; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define rep(i, start, end) for (long long i = start; i < end; ++i) #define repreverse(i, start, end) for (long long i = start; i >= end; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define inrange(a, x, b) (a <= x && x <= b) #define len(x) ((long long)(x).size()) #define lcm(a, b) ((a) / __gcd((a), (b)) * (b)) using namespace std; using ll = long long; using ld = long double; using vll = vector<ll>; using vllvll = vector<vll>; using vc = vector<char>; using vcvc = vector<vc>; using pll = pair<ll, ll>; // using mint = atcoder::static_modint<1'000'000'007>; // atcoder::static_modint<998'244'353>; template<class T> void print(T x,bool cl=1){cout<<(cl?"\x1b[36m":"")<<x<<(cl?"\x1b[39m":"")<<'\n';} template<class T> void print1d(T x,ll n=-1,bool cl=true){if(n==-1)n=x.size();rep(i,0,n){cout<<(cl?"\x1b[36m":"")<<x[i]<<(i==n-1?'\n':' ');}cout<<(cl?"\x1b[39m":"");} template<class T> void print2d(T x,ll r=-1,ll c=-1,bool cl=1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c,cl);} template<class T, class U> bool isin(T el, U container) { return find(all(container), el) != container.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; } template<class T> bool even(T n) { return ! (n & 1); } template<class T> bool odd(T n) { return n & 1; } template<class T> T rup(T a, T b) { return a / b + !!(a % b); } template<class Arr, class T, size_t Sz> void myfill(Arr (&a)[Sz], const T &x) { fill((T*)a, (T*)(a + Sz), x); } template<class T> ld deg2rad(T deg) { return M_PI * deg / 180.0; } template<class T> ld rad2deg(T rad) { return 180.0 * rad / M_PI; } const long double pi = M_PI; const long long inf = 1LL << 60; const long long mod = 1e9 + 7; ll intpow(ll a, ll n, ll _mod=numeric_limits<ll>::max()) { ll p=1; while (n) { if (n&1) p=p*a%_mod; a=a*a%_mod; n>>=1; } return p; } ll modc(ll a, char op, ll b, ll _mod=mod) { a %= _mod; b %= _mod; ll res = 1; switch (op) { case '+': res = (a + b) % _mod; break; case '-': res = (a - b) % _mod; break; case '*': res = a * b % _mod; break; case '/': res = modc(a, '*', modc(b, '^', _mod-2, _mod), _mod); break; case '^': res = intpow(a, b, _mod); break; case 'P': rep(i, a-b+1, a+1) res = modc(res, '*', i, _mod); break; case 'C': res = modc(modc(a, 'P', b, _mod), '/', modc(b, 'P', b, _mod)); break; } if (res < 0) { res += _mod; } return res; } int main() { ll N; cin >> N; vll A(N); rep(i, 0, N) cin >> A[i]; sort(rall(A)); vll acc(N + 1); rep(i, 0, N) acc[i + 1] = acc[i] + A[i]; ll ans = 0; rep(i, 0, N) { ll tmp = acc[N] - acc[i + 1]; ans += A[i] * (N - 1 - i) - tmp; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll N; int main() { // (4 3 2 1) // (4-3) (4-2) (4-1) (3-2) (3-1) (2-1) // 4 * 3 + 3 * 1 + 2 * (-1) + 1 * (-3) cin >> N; vector<ll> v; ll m; for(int i=0; i<N; ++i) { cin >> m; v.push_back(m); } sort(v.rbegin(), v.rend()); ll ret = 0; for(int i=0; i<N; ++i){ ret += v[i] * (N-1 - 2*i); } cout << ret << '\n'; }
#include<bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2") #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define req(i,a,b) for(int i=(a);i>=(b);--i) #define rep_(i,a,b) for(int i=(a);i<(b).size();++i) #define F(a) rep(a,1,n) #define M(a,b) memset(a,b,sizeof a) #define DC int T;cin>>T;while(T--) #define ll long long #define Z(a,b) sort(a+1,a+b+1) using namespace std; const unsigned _mod=998244353; const unsigned mod=1e9+7; const ll infi=0x3f3f3f3f3f3f3fll; const int inf=0x3f3f3f3f; void rd(auto &x){x=0;int f=1;char ch=getchar();while(ch<48||ch>57){if(ch==45)f=-1;ch=getchar();}while(ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();x*=f;} ll ksm(ll x,ll y=mod-2,ll m=mod){ll ret=1;while(y){if(y&1)ret=ret*x%m;y>>=1ll;x=x*x%m;}return ret;} ll qpow(ll x,ll y){ll ret=1;while(y){if(y&1ll)ret=ret*x;y>>=1ll;x=x*x;}return ret;} /* [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] */ int h,w,fa[2010],s,t; char c; unordered_map<int,bool> mp; inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} inline void merge(int x,int y){fa[find(x)]=find(y);} int main() { cin>>h>>w; rep(i,1,h+w) fa[i]=i; merge(1,h+1),merge(1,h+w),merge(h,h+1),merge(h,h+w); rep(i,1,h) rep(j,1,w) cin>>c,c=='#'&&(merge(i,h+j),1); rep(i,1,h) s+=!mp[find(i)],mp[find(i)]=1; mp.clear(); rep(i,h+1,h+w) t+=!mp[find(i)],mp[find(i)]=1; cout<<min(s,t)-1<<'\n'; return 0; }
#pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define N 1010 #define MOD 1000000007 //998244353 #define ll long long #define rep(i, n) for(int i = 0; i < n; ++i) #define rep2(i, a, b) for(int i = a; i <= b; ++i) #define rep3(i, a, b) for(int i = a; i >= b; --i) #define eb emplace_back #define pb push_back #define all(c) (c).begin(), (c).end() #define vi vector<int> #define pii pair<int,int> #define pll pair<ll,ll> struct Setup_io { Setup_io() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); } } setup_io; char a[N][N]; vector<int>e[2*N]; bool v[2 * N]; bool used[2 * N]; int ansa, ansb; void dfs(int k) { if (used[k])return; used[k] = true; int sz = e[k].size(); rep(i, sz) { if (!used[e[k][i]])dfs(e[k][i]); } return; } int main() { rep(i, N)rep(j, N)a[i][j] = 0; rep(i, 2 * N) { v[i] = false; } int n, m; cin >> n >> m; rep(i, n)cin >> a[i]; rep2(i, 1, m - 2) { if (a[0][i] == '#')v[N + i - 1] = true; if (a[n - 1][i] == '#')v[N + i - 1] = true; } rep2(i, 1, n - 2) { if (a[i][0] == '#')v[i - 1] = true; if (a[i][m - 1] == '#')v[i - 1] = true; rep2(j, 1, m - 2) { if (a[i][j] == '#') { e[i - 1].pb(N + j - 1); e[N + j - 1].pb(i - 1); } } } rep(i, n - 2) { used[i] = false; } rep(i, m - 2) { used[N + i] = false; } rep(i, n - 2) { if (v[i]) { if (!used[i])dfs(i); } } rep(i, m - 2) { if (v[N + i]) { if (!used[N + i])dfs(N + i); } } ansa = 0; rep(i, n - 2) { if (!used[i]) { dfs(i); ansa++; } } rep(i, n - 2) { used[i] = false; } rep(i, m - 2) { used[N + i] = false; } rep(i, n - 2) { if (v[i]) { if (!used[i])dfs(i); } } rep(i, m - 2) { if (v[N + i]) { if (!used[N + i])dfs(N + i); } } ansb = 0; rep(i, m - 2) { if (!used[N + i]) { dfs(N + i); ansb++; } } n = min(ansa, ansb); cout << n << endl; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i,n) for (int i=0;i<n;i++) #define INF 1000000001 #define lINF 1000000000000001 using ll = long long; ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;} ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} //1e9<int型の最大値<1e10 //1e18<long型の最大値<1e19 //配列直入れ↓ //g++ -std=c++17 -Wshadow -Wall -D_GLIBCXX_DEBUG -o a b.cpp && ./a int main(){ int h,w,x,y; cin>>h>>w>>x>>y; x--; y--; vector<string> s(h); rep(i,h) cin>>s[i]; int ans=0; for(int i=x;i<h;i++){ if(s[i][y]=='.') ans++; else break; } for(int i=x;i>=0;i--){ if(s[i][y]=='.') ans++; else break; } for(int j=y;j<w;j++){ if(s[x][j]=='.') ans++; else break; } for(int j=y;j>=0;j--){ if(s[x][j]=='.') ans++; else break; } cout<<ans-3<<endl; }
#include <bits/stdc++.h> #define fi first #define se second #define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++) #define mk make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define IT iterator #define V vector #define TP template <class o> #define TPP template <typename t1, typename t2> #define SZ(a) ((int)a.size()) #define all(a) a.begin(), a.end() #define rep(i, a, b) for (int i = a; i <= b; i++) #define REP(i, a, b) for (int i = b; i >= a; i--) #define FOR(i,n) rep(i,1,n) #define debug(x) cerr << #x << " = " << x << endl using namespace std; typedef unsigned ui; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef long double ld; const int N = 110, size = 1 << 20, mod = 998244353, inf = 2e9; const ll INF = 1e15; // char buf[size],*p1=buf,*p2=buf; TP void qr(o& x) { char c = gc; x = 0; int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = gc; } while (isdigit(c)) x = x * 10 + c - '0', c = gc; x *= f; } TP void qw(o x) { if (x / 10) qw(x / 10); putchar(x % 10 + '0'); } TP void pr1(o x) { if (x < 0) x = -x, putchar('-'); qw(x); putchar(' '); } TP void pr2(o x) { if (x < 0) x = -x, putchar('-'); qw(x); putchar('\n'); } // math ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll power(ll a, ll b = mod - 2, ll p = mod) { ll c = 1; while (b) { if (b & 1) c = c * a % p; b /= 2; a = a * a % p; } return c; } TP void cmax(o& x, o y) { if (x < y) x = y; } TP void cmin(o& x, o y) { if (x > y) x = y; } TPP void ad(t1& x, t2 y) { x += y; if (x >= mod) x -= mod; } TPP void dl(t1& x, t2 y) { x -= y; if (x < 0) x += mod; } template<typename T> struct BIT { T* c; int n; // require you to define 0 as the initial value !! BIT(int _n):n(_n){c=new T[n];c--; FOR(i,n) c[i]=T(0); } void add(int x,T y) { for( ;x<=n;x += x&-x) c[x]=c[x]+y; } T sum(int x) {T y=T(0); for( ;x;x &= x-1) y=y+c[x]; return y;} }; int n, a[N], vis[N], lst[N], now; char s[N][N]; void dfs(int x) { if(lst[x] == now) return; lst[x]=now; vis[x]++; FOR(y,n) if(s[x][y] == '1') dfs(y); } void solve() { qr(n); FOR(i,n) scanf("%s",s[i]+1); for(now=1;now<=n;now++) dfs(now); db ans=0; FOR(i,n) ans += 1.0/vis[i]; printf("%.10lf\n",ans); } int main() { int T = 1; // qr(T); while (T--) solve(); return 0; }
#include <bits/stdc++.h> #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define size_t unsigned long long #define ll long long #define rep(i, a) for (int i = 0; i < (a); i++) #define repr(i, a) for (int i = (int)(a)-1; i >= 0; i--) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define FORR(i, a, b) for (int i = (int)(b)-1; i >= a; i--) #define ALL(a) a.begin(), a.end() using namespace std; int si() { int x; scanf("%d", &x); return x; } long long sl() { long long x; scanf("%lld", &x); return x; } string ss() { string x; cin >> x; return x; } void pi(int x) { printf("%d ", x); } void pl(long long x) { printf("%lld ", x); } void pd(double x) { printf("%.15f ", x); } void ps(const string &s) { printf("%s ", s.c_str()); } void br() { putchar('\n'); } const ll MOD = 1e9 + 7; const ll INF = 1e9 + 5; const double PI = 3.14159265358979323846; struct Node { ll val; Node *left; Node *right; }; typedef pair<int, int> P; const ll N = 20; const ll M = 400; int n; unordered_map<int, bitset<10>> m; void printResult(const bitset<10> &x, const bitset<10> &y) { cout << "Yes\n" << x.count(); rep(i, x.size()) { if (x.test(i)) cout << " " << i + 1; } cout << "\n" << y.count(); rep(i, y.size()) { if (y.test(i)) cout << " " << i + 1; } cout << endl; return; } int main() { cin >> n; vector<ll> a(n); int count = min(n, 9); rep(i, count) { cin >> a[i]; a[i] %= 200; bitset<10> bs(0); bs.set(i); if (!m.emplace(a[i], bs).second) { printResult(m[a[i]], bs); return 0; } } for (int i = 0; i < (1 << count); i++) { bitset<10> bs(i); if (bs.count() < 2) continue; int sum = 0; rep(j, count) { if (bs.test(j)) { sum += a[j]; } } sum %= 200; if (!m.emplace(sum, bs).second) { printResult(m[sum], bs); return 0; } } cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int H, W; cin >> H >> W; vector<vector<char>>S(H, vector<char>(W)); for(int i=0; i<H; i++){ for(int j=0; j<W; j++)cin >> S.at(i).at(j); } vector<vector<int>>A(H, vector<int>(W)); A.at(0).at(0)=1; vector<vector<int>>tate(H, vector<int>(W)); vector<vector<int>>yoko(H, vector<int>(W)); vector<vector<int>>naname(H, vector<int>(W)); for(int i=0; i<H; i++){ for(int j=0; j<W; j++){ if(i>0){ A.at(i).at(j)+=tate.at(i-1).at(j); tate.at(i).at(j)+=tate.at(i-1).at(j); A.at(i).at(j)%=1000000007; } if(j>0){ A.at(i).at(j)+=yoko.at(i).at(j-1); yoko.at(i).at(j)+=yoko.at(i).at(j-1); A.at(i).at(j)%=1000000007; } if(i>0&&j>0){ A.at(i).at(j)+=naname.at(i-1).at(j-1); naname.at(i).at(j)+=naname.at(i-1).at(j-1); A.at(i).at(j)%=1000000007; } tate.at(i).at(j)+=A.at(i).at(j); yoko.at(i).at(j)+=A.at(i).at(j); naname.at(i).at(j)+=A.at(i).at(j); tate.at(i).at(j)%=1000000007; yoko.at(i).at(j)%=1000000007; naname.at(i).at(j)%=1000000007; if(S.at(i).at(j)=='#'){ A.at(i).at(j)=0; tate.at(i).at(j)=0; yoko.at(i).at(j)=0; naname.at(i).at(j)=0; } } } cout << A.at(H-1).at(W-1) << endl; }
#include <bits/stdc++.h> using namespace std; long long MOD=998244353; long long MODpow(long long a,int b){ if(b==0)return 1; if(b%2==0){ long long t=MODpow(a,b/2); return (t*t)%MOD; } return (a*MODpow(a,b-1))%MOD; } int main(){ int N,M,K; cin >> N >> M >> K; vector<long long> A(K+1,0); vector<long long> B(K+1,0); for(int i=1;i<=K;i++)A[i]=MODpow(i,N); for(int i=1;i<=K;i++)B[i]=MODpow(i,M); long long ans=0; for(int i=1;i<=K;i++){ int j=K-i+1; if(M==1)j=1; long long add=(A[i]-A[i-1]+MOD)*B[j]; if(N==1)add=(A[i]-A[i-1])*(B[j]-B[j-1]); add%=MOD; ans+=add; ans%=MOD; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define TRACE(x) cerr << #x << " = " << x << endl #define pb push_back #define mp make_pair #define fi first #define se second #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define REP(i, n) FOR(i, 0, n) #define SZ(x) (int)(x).size() using namespace std; typedef long long ll; typedef double lf; typedef pair<int, int> pii; typedef vector<int> VI; template<class Num> Num mabs(Num A){ if(A < 0) return -A; return A; } const int mod = 1e9 + 7; inline int add(int a, int b){ int ret = a + b; if(ret >= mod) ret -= mod; return ret; } inline int mult(int a, int b){ return (int)((ll)a * b % mod); } int digni(int x, int y){ int ret = 1; while(y){ if(y % 2 == 1) ret = mult(ret, x); x = mult(x, x); y /= 2; } return ret; } int inverse(int x){ return digni(x, mod - 2); } inline int divi(int a, int b){ return mult(a, inverse(b)); } inline int sub(int a, int b){ int ret = a - b; if(ret < 0) ret += mod; return ret; } struct Matrix{ int n, m; vector<VI> mat; Matrix(){} Matrix(int _n, int _m){ n = _n; m = _m; mat.resize(n + 1, VI(m + 1, 0)); } int& operator()(int x, int y){ return mat[x][y]; } int operator()(int x, int y) const{ return mat[x][y]; } Matrix operator*(const Matrix &A){ Matrix ret(n, A.m); FOR(i, 1, n + 1){ FOR(j, 1, A.m + 1){ FOR(k, 1, m + 1){ ret(i, j) = add(ret(i, j), mult(mat[i][k], A(k, j))); } } } return ret; } }; Matrix matrix_exp(Matrix &A, int y){ //mora biti kvadratna int n = A.n; assert(A.n == A.m); Matrix ret(n, n); FOR(i, 1, n + 1) ret(i, i) = 1; while(y){ if(y % 2 == 1) ret = ret * A; A = A * A; y /= 2; } return ret; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<int> a(n + 1); FOR(i, 1, n + 1){ cin >> a[i]; } vector<int> E[n + 1]; REP(i, m){ int u, v; cin >> u >> v; E[u].pb(v); E[v].pb(u); } Matrix poc(1, n); Matrix prijelaz(n, n); const int pola = divi(1, 2); FOR(i, 1, n + 1){ poc(1, i) = a[i]; int deg = SZ(E[i]); int prob = divi(deg, m); prijelaz(i, i) = sub(1, mult(pola, prob)); for(int nei : E[i]){ prijelaz(i, nei) = divi(1, mult(2, m)); } } prijelaz = matrix_exp(prijelaz, k); Matrix sol = poc * prijelaz; FOR(i, 1, n + 1){ cout << sol(1, i) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) #define ll long long #define vi std::vector<ll> #define si set<ll> #define pb push_back vi v[2020]; bool visited[2020]; ll ans; void dfs(ll s){ if(visited[s])return; visited[s] = true; ans++; for (auto u: v[s]){ dfs(u); } return; } ll solve(){ for (int i = 0; i < 2020; ++i){ visited[i] = false; v[i].clear(); } ll n, m; cin >> n >> m; for (int i = 0; i < m; ++i){ ll a, b; cin >> a >> b; v[a].pb(b); } ll totans = 0; for (ll i = 1; i <= n; ++i){ memset(visited, false, 2020); ans = 0; dfs(i); totans += ans; } cout << totans; return 0; } int main(){ fastio; ll t= 1; // cin >> t; for (int i = 0; i < t; ++i){ // cout << "Case #" << i+1 << ": "; solve(); cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, x, y) for (int i = x; i < y; i++) vector<vector<bool>> ok; void dfs(map<int, set<int>> &path, int i, int p) { if (ok[p][i]) return; ok[p][i] = true; for (auto v : path[i]) dfs(path, v, p); } int main() { int N, M; cin >> N >> M; map<int, set<int>> path; ok.resize(N); rep(i, 0, N) ok[i].resize(N); rep(i, 0, M) { int A, B; cin >> A >> B; path[A - 1].insert(B - 1); } ll ans = 0; rep(i, 0, N) { dfs(path, i, i); ans += count_if(ok[i].begin(), ok[i].end(), [](bool f) { return f; }); } cout << ans << endl; }
/* Hemant Gupta */ #include <bits/stdc++.h> using namespace std; #define int long long #define show(v) \ for (int i = 0; i < (int)v.size(); i++) \ cout << v[i] << (i == (int)v.size() - 1 ? "\n" : " "); #define pb push_back #define INF 1e18 + 5 const int MOD = 1e9 + 7; const int N = 3e5 + 5; vector<int> adj[N]; vector<bool> vis(N), take(N); void test_case() { char a[3]; cin >> a[0] >> a[1] >> a[2]; if (a[0] == a[1] && a[1] == a[2]) cout << "Won"; else cout << "Lost"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); test_case(); return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <map> #include <sstream> #include <iomanip> #include <set> #include <cmath> #include <bitset> #include <queue> #include <climits> using namespace std; int main(){ int a, b, c, d; cin >> a >> b >> c >> d; int right = c * d - b; if (right <=0){ cout << -1 << endl; } else{ float thresh = double(a) / right; int ans = ceil(thresh); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, s, e, p) for (ll i = (ll)s; i < (ll)e; i += p) #define ALL(vec) vec.begin(), vec.end() int main(){ int n; cin >> n; ll l = pow(10, 9), s = -pow(10, 9), a, t, m = 0; rep(i, 0, n, 1){ cin >> a >> t; if (t == 1){ m += a; s += a; l += a; }else if (t == 2){ if (l < a) l = a; s = max(a, s); }else{ if (a < s) s = a; l = min(l, a); } } ll q, x; cin >> q; rep (i, 0, q, 1){ cin >> x; if (x + m <= s) cout << s << endl; else if (l <= x + m) cout << l << endl; else cout << x + m << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) (x).begin(),(x).end() template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;} template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;} int dx[4]={0,1,0,-1}, dy[4]={1,0,-1,0}; long double eps = 1e-9; long double pi = acos(-1); signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); int n,q; cin>>n; ll l=-1e18,r=1e18,p=0; for(int i=0;i<n;i++){ ll a,t; cin>>a>>t; if(t==1)l+=a,r+=a,p+=a; if(t==2)chmax(r,a),chmax(l,a); if(t==3)chmin(l,a),chmin(r,a); } cin>>q; for(int i=0;i<q;i++){ ll x; cin>>x; x += p; chmax(x,l); chmin(x,r); cout << x << "\n"; } }
#include <bits/stdc++.h> using namespace std; typedef long long i64; typedef unsigned long long ui64; typedef vector<i64> vi; typedef vector<vi> vvi; typedef pair<i64, i64> pi; #define pb push_back #define sz(a) i64((a).size()) #define all(c) (c).begin(), (c).end() #define REP(s, e, i) for(i=(s); i < (e); ++i) inline void RI(i64 &i) {scanf("%lld", &(i));} inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } } inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } } inline void WI(const i64 &i) {printf("%lld\n", i);} inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); } inline void WS(const string &s) { printf("%s\n", s.c_str()); } inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} } inline void YESNO(bool b) { WB(b, "YES", "NO"); } inline void YesNo(bool b) { WB(b, "Yes", "No"); } #define BUF_LENGTH 1000000 inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;} template<typename T> inline bool IN(T &S, const typename T::key_type &key) { return S.find(key) != S.end(); } template<typename T> inline bool ON(const T &b, i64 idx) { return ((T(1) << idx) & b) != 0; } int main(int argc, char *argv[]) { i64 i, j, k; i64 N; cin >> N; vector<array<i64, 5>> A(N); REP(0, N, i) { REP(0, 5, j) { RI(A[i][j]); } } auto check = [&](i64 val) { using ull = unsigned long long; vector<ull> B(N, 0); //cerr << val << endl; REP(0, N, i) { REP(0, 5, j) { if(A[i][j] >= val) { B[i] |= 1LL << j; } } //cerr << B[i] << " "; } //cerr << endl; vi FS(32, 0); REP(0, N, i) { REP(i+1, N, j) { //cerr << i << " " << j << endl; ull res = (~(B[i] | B[j])) & 31; if(FS[res] == 1) { //cerr << "found\n"; return true; } //cerr << "not found\n"; } REP(0, 32, k) { if((ull(k) & B[i]) == ull(k)) { //cerr << "add FS " << k << endl; FS[k] = 1; } } } return false; }; i64 L = 0, H = 1000000000LL+1; assert(check(L)); assert(!check(H)); while(H - L > 1) { i64 M = (H + L) / 2; if(check(M)) { L = M; } else { H = M; } } WI(L); return 0; }
/* ** author : Dhiraj Govindvira ** date : 02 May 2021 ~ Sunday ** time : 09:20:20 PM */ #include <bits/stdc++.h> using namespace std; #ifdef DHIRAJ #include "D:/dhiraj/Programming/debug.h" #else #define dbg(...) 1 #define cerr if(0) cerr #endif using ll = long long int; #define endl '\n' template <typename T, typename U> inline istream & operator >> (istream &in, pair<T, U> &p) { in >> p.first >> p.second; return in; } template <typename T> inline istream & operator >> (istream &in, vector<T> &v) { for(T &x : v) in >> x; return in; } void solve(ll &tc) { ll n; cin >> n; vector<vector<ll>> a(n, vector<ll>(5)); for(ll i = 0; i < n; i++) { for(ll j = 0; j < 5; j++) { cin >> a[i][j]; } } auto f = [&](ll m) { vector<ll> b(32, 0); for(ll i = 0; i < n; i++) { ll x = 0; for(ll j = 0; j < 5; j++) { if(a[i][j] >= m) { x |= (1ll << j); } } b[x] = 1; } for(ll i = 0; i < 32; i++) { for(ll j = 0; j < 32; j++) { for(ll k = 0; k < 32; k++) { if(b[i] and b[j] and b[k]) { if((i | j | k) == 31) { return 1; } } } } } return 0; }; ll l = 1, r = 1e9 + 5; while(l <= r) { ll m = (l + r) / 2; if(f(m)) { l = m + 1; } else { r = m - 1; } } cout << l - 1 << endl; } int main() { #ifdef DHIRAJ freopen("D:/dhiraj/Programming/i1.txt", "r", stdin); freopen("D:/dhiraj/Programming/o1.txt", "w", stdout); freopen("D:/dhiraj/Programming/e1.txt", "w", stderr); #endif ios::sync_with_stdio(0); cin.tie(0); ll rep = 3; while(rep--) { ll tc = 1; for(ll i = 1; i <= tc; i++) { cerr << "Case #" << i << "\n"; solve(i); } if(dbg()) break; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x, y; cin >> a >> b >> x >> y; int ans = 0; if (a == b) { ans = x; } else if (a < b) { if (y < 2 * x) { ans = (b - a) * y + x; } else { ans = (b - a) * 2 * x + x; } } else { if (y < 2 * x) { ans = (a - b - 1) * y + x; } else { ans = (a - b - 1) * 2 * x + x; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i,j,n) for (int i=j; i < (int)(n); i++) int h[200010]; int w[200010]; int main() { int n,a,b,m,jj,jj2; long long p,q,ans; p=1e15; ans=1e15; cin >> n >> m ; rep(i, n) cin >> h[i]; rep(i, m) cin >> w[i]; sort(h, h+n); //h[0:n] を値が小さい順にソート sort(w, w+m); //w[0:m] を値が小さい順にソート p=0; rep(i,n){ if (i%2>0) p-=h[i];//奇数はマイナス else p+=h[i];//偶数はプラス } q=p; jj=0; rep(j,m){ if (w[j]<h[0]) ans=min(ans,p-w[j]); else if (w[j]>h[n-1]) ans=min(ans,w[j]-p); else { rep2(i,jj,n){ if (w[j]<h[i]) break; if (i%2>0) q=q+h[i]+h[i]; else q=q-h[i]-h[i]; jj++; } if (jj%2>0) ans=min(ans,q+w[j]); else ans=min(ans,q-w[j]); } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> //#define maxn using namespace std; int read() { int s=0,w=1; 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; } void rein(char*str) { int n=0;char opt; while((opt=getchar())<'a'||opt>'z'); while(opt>='a'&&opt<='z')str[n++]=opt,opt=getchar(); str[n]='\0'; } //int ksm(int x,int y) //{ // int r=1; // while(y) // { // if(y&1) r=1ll*r*x%mod; // x=1ll*x*x%mod,y>>=1; // } // return r; //} //int a[maxn]; int n; long long ans;char S[10]; signed main() { n=read(); for(int i=1;i<=n;i++) { scanf("%s",S); if(S[0]=='O') ans+=(1ll<<i); } cout<<ans+1; return 0; }
#include<bits/stdc++.h> using namespace std; long long n,dp[70][2]={0}; int main() { cin>>n; string s[n]; for(int i=0;i<n;i++) cin>>s[i]; dp[0][0]=1; dp[0][1]=1; for(int i=0;i<n;i++) { for(int j=0;j<2;j++) { for(int k=0;k<2;k++) { if(s[i]=="AND") dp[i+1][j&k]+=dp[i][j]; else dp[i+1][j|k]+=dp[i][j]; } } } cout<<dp[n][1]; }
#include <bits/stdc++.h> #define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++) #define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--) #define pb push_back #define mp make_pair #define pll pair<ll,ll> #define endl "\n" using ll=long long; using ld=long double; using namespace std; ll mod= 1e9+7; ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; } ll a[330000]; map<ll,ll>x; int main(){ ll n,ans=0; cin>>n; cout<<ceil(n/100.0); }
#include<bits/stdc++.h> using namespace std; int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif string s; cin>>s; int count = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] == 'Z') { if (s[i+1] == 'O') { if (s[i+2] == 'N') { if (s[i+3] == 'e') { count++; } } } } } cout<<count<<'\n'; return 0; }
//雪花飄飄北風嘯嘯 //天地一片蒼茫 #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; #define ll long long #define ii pair<ll,ll> #define iii pair<ii,ll> #define fi first #define se second #define endl '\n' #define debug(x) cout << #x << " is " << x << endl #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define lb lower_bound #define ub upper_bound #define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> //change less to less_equal for non distinct pbds, but erase will bug mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); int n; int arr[505]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); int TC; cin>>TC; while (TC--){ cin>>n; rep(x,1,n+1) cin>>arr[x]; if (n==2){ if (arr[1]==2) cout<<1<<endl<<1<<endl; else cout<<0<<endl; continue; } vector<int> ans; int par=1; rep(x,n+1,4){ int pos; rep(y,1,n+1) if (arr[y]==x) pos=y; if (pos==x) continue; if (pos%2!=par){ if (par==1){ if (arr[2]==x) ans.pub(3),swap(arr[3],arr[4]); else ans.pub(1),swap(arr[1],arr[2]); } else{ if (arr[3]==x){ ans.pub(2),swap(arr[2],arr[3]); ans.pub(3),swap(arr[3],arr[4]); ans.pub(2),swap(arr[2],arr[3]); } else ans.pub(2),swap(arr[2],arr[3]); } par^=1; } rep(z,pos,x){ ans.pub(z),swap(arr[z],arr[z+1]); par^=1; } } while (arr[1]!=1 || arr[2]!=2 || arr[3]!=3){ if (par==1) ans.pub(1),swap(arr[1],arr[2]); else ans.pub(2),swap(arr[2],arr[3]); par^=1; } cout<<sz(ans)<<endl; for (auto &it:ans) cout<<it<<" "; cout<<endl; } }
#include <bits/stdc++.h> using namespace std; #define mxx 1e18 #define mnn -1e18 //#define int long long #define Y() cout<< "YES" <<endl #define N() cout << "NO"<<endl #define endl "\n" #define Ceil(x,y) ((x+y-1)/y) #define sz(s) (int)s.size() #define angle(x) double(x * acos(-1) / 180.0) #define max_3(a,b,c) max(a, max(b,c)) #define min_3(a,b,c) min(a, min(b,c)) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*b)/gcd(a,b) #define ll long long #define PI acos(-1) #define mem(a,v) memset(a,v,sizeof(a)) #define SORT(v) sort(v.begin(),v.end()) #define REV(v) reverse(v.begin(),v.end()) #define B begin() #define E end() #define V vector #define F first #define S second #define PSB push_back #define MP make_pair #define flash cout.flush() #define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0); constexpr ll MOD = 998244353; constexpr ll mod = 1e9 + 7; int dx[] = {0,0,1,-1}; int dy[] = {1,-1,0,0}; /*-----*/ #define bug1(a) cerr<<a<<endl; #define bug2(a,b) cerr<<a<<" "<<b<<endl; #define bug3(a,b,c) cerr<<a<<" "<<b<<" "<<c<<endl; /*----*/ const ll N=3e6+5; vector<ll> adj[N]; ll power(ll n,ll p){if(p==0) return 1;if(p==1)return n;if(p%2)return power(n,p-1)*n;else{ll x=power(n,p/2);return x*x;}} ll modpow(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;} ll nsum(ll num){return (num*(num+1))/2;} void edge (ll u,ll v) {adj[u].PSB(v) ;adj[v].PSB(u);} /*------------------START---------------------*/ /*-----*/ void solve(){ ll n; string s; cin>>n; V<ll> a(n+1),ok(n+1); a[0]=ok[0]; for(ll i=1;i<=n;i++){ cin>>a[i]; ok[i]=i; } ll ct=1; V<ll> ans; for(ll i=1;i<=n*n;i++){ if(a==ok) break; bool ck=false; for(ll j=1;j<n;j++){ if(j%2 == ct%2 && a[j]>a[j+1]){ ct++; ans.PSB(j); swap(a[j],a[j+1]); ck=true; break; } } if(!ck){ for(ll j=n-1;j>=0;j--){ if(j%2 == ct%2){ ct++;ans.PSB(j); swap(a[j],a[j+1]); break; } } } } cout<<ans.size()<<endl; for(auto it: ans) cout<<it<<' '; cout<<endl; //cout << fixed << setprecision(10); } /*-----*/ //signed main(){ int main(){ InTheNameOfGod ll Test=1; cin>>Test; while(Test--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define fr(i,j,k) for(int i=j;i<k;i++) #define f(n) fr(i,0,n) #define f1(n) fr(i,1,n+1) #define pb push_back #define F first #define S second #define all(x) x.begin(), x.end() using pii = pair<int,int>; const ll mod = 1e9 + 7; const int maxn = 1e5+5; void go() { ll n; cin >> n; ll p3 = 1; f1(37) { p3 *= 3; ll p5 = 1; fr(j,1,26) { //cout << (ll)pow(3,i) + (ll)pow(5,j) << endl; p5 *= 5; if (p3 + p5 == n) { cout << i <<' '<<j<<endl; exit(0); } } } cout << -1 << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int c = 0; int t; if (!c) { t = 1; } else { cin >> t; } while (t--) { go(); } }
#include <bits/stdc++.h> using namespace std; #define int long long typedef vector<int> vi; const int limit = (int)1e18; void build(vi &v, int f) { int b = f; while (b <= limit) v.push_back(b), b *= f; } int32_t main() { vi a, b; build(a, 3); build(b, 5); int n; cin >> n; bool flag = false; pair<int, int> ans; for (int i = 0; i < (int)a.size(); i++) for (int j = 0; j < (int)b.size(); j++) if (a[i] + b[j] == n) { flag = true, ans.first = i + 1, ans.second = j + 1; break; } if (flag) cout << ans.first << ' ' << ans.second; else cout << -1; return 0; }
#include<bits/stdc++.h> #define MAX 10000 typedef long long ll; using namespace std; void manak() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif } int main() { manak(); int a[2][2]; for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { cin>>a[i][j]; } } cout<<((a[0][0]*a[1][1])-(a[0][1]*a[1][0])); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define _GLIBCXX_DEBUG typedef long long ll; int main() { int a,b,c,d; cin >> a >> b >> c >> d; cout << a*d - b*c << endl; }
#include <iostream> #define pb push_back using namespace std; long long n,nr,i,a,b,y,xp,c; int main() { cin>>n>>y>>a>>b; xp=0; while(a <= (n + b) / n&&n*a<y) { xp++; n*=a; } /*else { c=(y-n)/b; xp+=c+1; if((y-n)%b==0) xp--; break; }*/ xp+=(y-n-1)/b; cout<<xp; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, a, b) for(ll i = a; i < b; i++) #define Rep(i, a, b) for(ll i = a; i <= b; i++) #define repr(i, a, b) for(ll i = b-1; i >= a; i--) // #define _GLIBCXX_DEBUG template <class T> using V = vector<T>; #define ALL(v) (v).begin(),(v).end() #define endl '\n' #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define sz(v) ((ll)(v).size()) const double pi = acos(-1.0); const ll MOD = 1000000007LL; const ll INF = 1LL << 60; const int dy[] = {1, 0, -1, 0}; const int dx[] = {0, 1, 0, -1}; const int dy2[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dx2[] = {1, 1, 0, -1, -1, -1, 0, 1}; // ios::sync_with_stdio(false); // cin.tie(nullptr); /*-------------------------------------------------------------------------------- --------------------------------------------------------------------------------*/ #include <bits/stdc++.h> using namespace std; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } int len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } __int128 parse(string &s) { __int128 ret = 0; for (int i = 0; i < s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } int main() { __int128 x, y, a, b; string o, p, q, r; cin >> o >> p >> q >> r; x = parse(o), y = parse(p), a = parse(q), b = parse(r); ll cnt = 0; while(1){ if(x*a < x+b){ __int128 bfx = x; x *= a; if(bfx != x/a || x >= y){ cout << cnt << endl; return 0; } cnt++; } if(x*a >= x+b){ ll dif = y - x; ll add = dif/b; if(dif%b == 0) add--; cout << cnt+add << endl; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define REP(NAME, NUM) for (int NAME = 0; NAME < int(NUM); ++NAME) #define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME) #define ALL(NAME) (NAME).begin(), (NAME).end() #define cMOD 1000000007ULL #define cINF ((1ll<<62)-1) #define cINFINT ((1<<30)-1) template <typename T> void chmin(T& a, T b){ if(a > b) a = b; } template <typename T> void chmax(T& a, T b){ if(a < b) a = b; } int main() { cin.tie( 0 ); ios::sync_with_stdio( false ); // ---------------------------------------------------------------- ull n = 0; cin >> n; vector<ll> vec( 2*n, 0 ); ll sum = 0; REP(i, 2*n) {cin >> vec[i]; sum+=vec[i];} ll aosum = 0; priority_queue<ll, vector<ll>, greater<ll>> q; REP(i, n) { ll a = n - 1 - i; ll b = n + 0 + i; q.push(vec[a]); q.push(vec[b]); aosum += q.top(); q.pop(); } cout << sum - aosum << endl; // ---------------------------------------------------------------- return 0; }
#include<iostream> #include<vector> #include<queue> using namespace std; int main() { int N; cin >> N; vector<int> V(2*N); int i; for(i=0;i<2*N;i++)cin >> V[i]; priority_queue<int,vector<int>,greater<int>> q; for(i=0;i<N;i++) { q.push(V[N-i-1]); q.push(V[N+i]); q.pop(); } long long ans = 0; while(!q.empty()) { ans += q.top(); q.pop(); } cout << ans << endl; }
#include <bits/stdc++.h> #define For(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rFor(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, lint> pll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } template <typename T> struct coord_comp { vector<T> v; bool sorted = false; coord_comp() {} int size() { return v.size(); } void add(T x) { v.push_back(x); } void build() { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); sorted = true; } int get_idx(T x) { assert(sorted); return lower_bound(v.begin(), v.end(), x) - v.begin(); } T &operator[](int i) { return v[i]; } }; constexpr lint mod = 1000000007; constexpr lint INF = mod * mod; constexpr int MAX = 200010; int main() { int n; scanf("%d", &n); int a[n]; rep(i, n) scanf("%d", &a[i]), a[i] %= 200; bool dp[n + 1][200][2][2]; rep(i, n + 1) rep(j, 200) rep(k, 2) rep(l, 2) dp[i][j][k][l] = false; dp[0][0][0][0] = true; tuple<int, int, int> prv[n + 1][200][2][2]; rep(i, n) rep(j, 200) rep(k, 2) rep(l, 2) if (dp[i][j][k][l]) { dp[i + 1][(j + a[i]) % 200][1][l] = true; prv[i + 1][(j + a[i]) % 200][1][l] = {0, k, l}; dp[i + 1][(j - a[i] + 200) % 200][k][1] = true; prv[i + 1][(j - a[i] + 200) % 200][k][1] = {1, k, l}; dp[i + 1][j][k][l] = true; prv[i + 1][j][k][l] = {2, k, l}; } if (dp[n][0][1][1]) { puts("Yes"); int j = 0, k = 1, l = 1; vector<int> b, c; rFor(i, n + 1, 1) { auto [t, nk, nl] = prv[i][j][k][l]; if (t == 0) { b.push_back(i - 1); j = (j + 200 - a[i - 1]) % 200; } else if (t == 1) { c.push_back(i - 1); j = (j + a[i - 1]) % 200; } k = nk; l = nl; } reverse(b.begin(), b.end()); reverse(c.begin(), c.end()); printf("%d ", b.size()); for (auto x : b) printf("%d ", x + 1); printf("\n"); printf("%d ", c.size()); for (auto x : c) printf("%d ", x + 1); printf("\n"); return 0; } puts("No"); }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; N = min(8, N); vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; A[i] %= 200; } vector<vector<int>> rs(200); for (int i = 1; i < (1 << N); ++i) { int acc = 0; for (int j = 0; j < N; ++j) { if ((i >> j) & 1) { acc += A[j]; acc %= 200; } } rs[acc].push_back(i); } int m = -1; for (int i = 0; i < 200; ++i) { if (rs[i].size() > 1) { m = i; break; } } if (m < 0) { cout << "No" << endl; return 0; } int b = rs[m][0]; vector<int> B; for (int i = 0; i < N; ++i) { if ((b >> i) & 1) { B.push_back(i); } } int c = rs[m][1]; vector<int> C; for (int i = 0; i < N; ++i) { if ((c >> i) & 1) { C.push_back(i); } } cout << "Yes" << endl; cout << B.size() << " "; for (int e : B) { cout << e + 1<< " "; } cout << endl; cout << C.size() << " "; for (int e : C) { cout << e + 1 << " "; } cout << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define MOD 1000000007 #define pii pair <ll, ll> long long GCD(long long a, long long b) { if (!b) return a; return GCD(b, a % b); } vector <int> graph[2001]; queue <int> q; int n; pii arr[200001]; int main() { ll n, k; scanf("%lld %lld", &n, &k); for (int i = 0; i < n; i++) { scanf("%lld %lld", &arr[i].first, &arr[i].second); } sort(arr, arr + n); for (int i = 0; i < n; i++) if (k >= arr[i].first) k += arr[i].second; printf("%lld", k); }
#include <bits/stdc++.h> 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(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} // You should only debug a pair of simple data types. For example, // the debug won't work if one of pair's elements is collection type // (std::vector, std::map, std::set...). template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif // Shortcuts for "common" data types in contests typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<char,char> pcc; typedef pair<char,int> pci; typedef pair<int,char> pic; typedef vector<pii> vpii; typedef set<int> si; typedef set<ll> sll; typedef map<int,int> mii; typedef map<ll,ll> mll; #define ff first #define ss second #define endl '\n' #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(),v.rend() #define rep(i, a, b) for(int i = a; i < b; i++) #define repv(c, it) \ for(auto it = (c).begin(); it != (c).end(); it++) #define repm(c, it) \ for(mll::iterator it = (c).begin(); it != (c).end(); it++) #define inf 1000000007 #define pb push_back const long long mod = 1000000007; const double pi = acos(-1); int main() { //decreases the time taken by cin,cout. ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); //freopen("error.txt", "w", stderr); freopen("output.txt", "w", stdout); #endif ll n,k;cin>>n>>k; //ll c[n+1];c[0]=k; ll x,y; vector<vll> tt(n); rep(i,0,n) { cin>>x>>y; tt[i].push_back(x); tt[i].push_back(y); } sort(all(tt)); //rep(i,0,n){c[i+1]=c[i]+a[i][1];} ll ans=k,sum=k; rep(i,0,n) { if(sum<tt[i][0]){ans=sum;break;} else { sum+=tt[i][1]; ans=sum; } } cout<<ans; return 0; }
//#include <atcoder/maxflow.hpp> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #include <iostream> #include <map> #include <list> #include <set> #include <algorithm> #include <vector> #include <string> #include <functional> #include <queue> #include <deque> #include <stack> #include <unordered_map> #include <unordered_set> #include <cmath> #include <iterator> #include <random> #include <chrono> #include <complex> #include <bitset> #include <fstream> #define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i) #define set_map_includes(set, elt) (set.find((elt)) != set.end()) #define readint(i) int i; cin >> i #define readll(i) ll i; cin >> i #define readdouble(i) double i; cin >> i #define readstring(s) string s; cin >> s typedef long long ll; //using namespace __gnu_pbds; //using namespace atcoder; using namespace std; //const ll modd = (1000LL * 1000LL * 1000LL + 7LL); const ll modd = 998244353; int main(int argc, char *argv[]) { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen(0, modd); // rand_gen(rng) gets the rand no ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(12); // readint(test_cases); int test_cases = 1; forr(t, 1, test_cases) { readint(n); readint(m); struct info { int i; ll c,d; }; vector<vector<info>> adj(n); forr(i,0,m) { readint(aa); --aa; readint(bb); --bb; readll(c); readll(d); if (aa!=bb) { adj[aa].push_back({bb,c,d}); adj[bb].push_back({aa,c,d}); } } vector<bool> visited(n, false); stack<int> todo; todo.push(0); while (!todo.empty()) { auto curr = todo.top(); todo.pop(); if (visited[curr]) { continue; } visited[curr] = true; for(auto x : adj[curr]) { todo.push(x.i); } } if (!visited.back()) { cout << -1 << endl; continue; } priority_queue<pair<ll,int>> todoo; todoo.push(make_pair(0,0)); vector<bool> visit(n, false); while (!todoo.empty()) { auto curr = todoo.top(); todoo.pop(); if (visit[curr.second]) { continue; } visit[curr.second] = true; ll tim = -curr.first; if (curr.second==n-1) { cout << tim << endl; return 0; } for(auto x : adj[curr.second]) { vector<ll> candidates; candidates.push_back(0); ll p = (ll)sqrt(x.d) - 1-tim; candidates.push_back(p-2); candidates.push_back(p-1); candidates.push_back(p); candidates.push_back(p+1); candidates.push_back(p+2); for(ll w : candidates) { if (w>=0) todoo.push(make_pair(-(tim + x.c + (x.d/(tim+1+w)) + w ), x.i)); } } } } return 0; }
#include <iostream> #include <vector> #include <numeric> #include<cstdlib> #include <map> #include <set> #include <math.h> #define ll long long #define MAXN 100000 using namespace std; std::vector<std::tuple<int,ll,ll>> edges[MAXN+2]; ll GetOptTime(ll d) { // t + d/(t+1) is minimized ll topt = (ll)sqrt(d); ll opt_time = -1; for(ll t=max(0ll,topt-20);t<=topt+20;t++) { ll curr_time = t+d/(t+1); if (opt_time == -1 || curr_time < opt_time) { topt = t; opt_time = curr_time; } } return topt; } bool done[MAXN+2]; int main() { int N,M; cin>>N>>M; for(int i=1;i<=M;i++) { int a,b; ll c,d; cin>>a>>b>>c>>d; edges[a].push_back(std::make_tuple(b,c,d)); edges[b].push_back(std::make_tuple(a,c,d)); } for(int u=1;u<=N;u++) { done[u]=false; } set<pair<ll,int>> Q; Q.insert({0,1}); while(!Q.empty()) { auto it = Q.begin(); int u = it->second; ll curr_time = it->first; Q.erase(it); if (done[u]) continue; if (u == N) { cout << curr_time << endl; return 0; } done[u]=true; for(auto vcd : edges[u]) { int v = std::get<0>(vcd); ll c = std::get<1>(vcd); ll d = std::get<2>(vcd); // t + c + d/(t+1) for t >= curr_time ll opt_time = max(curr_time, GetOptTime(d)); Q.insert({opt_time+c+d/(opt_time+1),v}); } } cout << -1 << endl; }
#include <iostream> #include <algorithm> #include <vector> typedef long long ll; using namespace std; template <class T> void input(T& a){ cin>>a; } int main(){ int A,B,C; input(A); input(B); input(C); if((A*A+B*B)<C*C){ cout <<"Yes"<<endl; } else{ cout <<"No"<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define fs(n) fixed << setprecision(n) using ll = long long; using ld = long double; ll fact(ll x) { return x <= 1 ? 1 : x * fact(x - 1); } //再帰処理で時間を食い過ぎるので階乗をメモ化する map<ll, ll> factMemo; ll memo_fact(ll x){ if(factMemo[x] != 0) return factMemo[x]; else return x <= 1 ? 1 : x * fact(x - 1); } int sign(int x){ return (x > 0) - (x < 0); } bool isLeapYear(int y, int m){ return (y % 400 == 0 || (y % 4 == 0 && y % 100)) && m == 2; } vector<int> AllDivs(int d){ vector<int> resDivs(0); for(int i = 1; i < (int)sqrt(d); i++){ if(d % i == 0){ resDivs.push_back(i); resDivs.push_back(d / i); } } sort(resDivs.begin(), resDivs.end()); return resDivs; } bool IsPrime(int num){ bool ans = true; if(num < 2) return false; else if (num == 2) return true; else{ for(int i = 2; i <= (int)sqrt(num); i++){ if(num % i == 0){ ans = false; } } } return ans; } int NextPrime(int d){ int ans = d; while(true){ ans++; if(IsPrime(ans)) break; } return ans; } vector<int> Dec2Bin(int x, int len){ vector<int> res(len, 0); int d = 1, pt = len - 1; while(d <= x){ res[pt] = (x & d ? 1 : 0); d *= 2; pt--; } return res; } int avg(int a, int b){ return (a + b) / 2; } ll permu(ll m, ll c){ ll ans = 1; for(ll i = c; i <= m; i++){ ans *= i; } return ans; } ll combi(ll m, ll c){ ll mf = memo_fact(c); factMemo[c] = mf; return (permu(m, c) / mf); } ll choose_two(ll m){ return m * (m + 1) / 2; } 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); } int main(){ // 反転はフラグで先頭と末尾を切り替え string s; cin >> s; bool flg = true; string t = ""; for(int i = 0; i < s.length(); i++){ string u = s.substr(i, 1); if(u != "R"){ if(t.length() == 0){ t += u; continue; } if(flg){ // 削除する if(t.substr(t.length() - 1, 1) == u){ t.erase(t.length() - 1); }else t += u; }else{ if(t.substr(0, 1) == u){ t.erase(0, 1); }else t = u + t; } }else flg = !flg; // cout << t << endl; } if(!flg) reverse(t.begin(), t.end()); cout << t << endl; }
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); } 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...); } #ifdef LOCAL #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__) #else #define debug(...) 42 #endif double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // returns unsigned ints! int main() { int K, N, M; cin >> K >> N >> M; vector<int> A(K); for(int i = 0; i < K; i++) cin >> A[i]; vector<long long> B(K); auto can = [&] (long long tar) { // |N * B_i - M * A_i| <= tar vector<long long> low(K), high(K); for(int i = 0; i < K; i++) { low[i] = ((long long) M * A[i] - tar); if(low[i] < 0) { low[i] = 0; } else low[i] = (low[i] + N - 1) / N; high[i] = ((long long) M * A[i] + tar) / N; while(low[i] > 0 && abs((long long) N * (low[i] - 1) - (long long) M * A[i]) <= tar) low[i]--; while(abs((long long) N * (high[i] + 1) - (long long) M * A[i]) <= tar) high[i]++; if(low[i] <= high[i]) { while(low[i] > 0 && abs((long long) N * (low[i] - 1) - (long long) M * A[i]) <= tar) low[i]--; while(abs((long long) N * (high[i] + 1) - (long long) M * A[i]) <= tar) high[i]++; if(low[i] > high[i]) return false; assert(abs((long long) N * low[i] - (long long) M * A[i]) <= tar); assert(abs((long long) N * high[i] - (long long) M * A[i]) <= tar); } } long long have = M; for(int i = 0; i < K; i++) { B[i] = low[i]; have -= low[i]; } if(have < 0) return false; for(int i = 0; i < K; i++) { long long can = high[i] - low[i]; B[i] += min(can, have); have -= min(can, have); } if(have > 0) return false; return true; }; long long lo = 0, hi = 1E18 + 5; while(lo < hi) { long long mid = (lo + hi) / 2; if(can(mid)) hi = mid; else lo = mid + 1; } assert(can(lo)); for(int i = 0; i < K; i++) cout << B[i] << ' '; cout << '\n'; }
#include<bits/stdc++.h> template <typename _Tp>void read(_Tp &x){ char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar(); x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar(); if(f)x=-x; } template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);} const int N=105,mod=1000000007,inv2=(mod+1)/2; typedef long long ll; template<typename _Tp1,typename _Tp2>inline void add(_Tp1 &a,_Tp2 b){(a+=b)>=mod&&(a-=mod);} template<typename _Tp1,typename _Tp2>inline void sub(_Tp1 &a,_Tp2 b){(a-=b)<0&&(a+=mod);} ll ksm(ll a,ll b=mod-2){ll res=1;while(b){if(b&1)res=res*a%mod;a=a*a%mod,b>>=1;}return res;} int a[N],b[N]; int X[N*N],Y[N*N],n; struct mat{ int a[N][N]; mat operator * (const mat &o)const{ mat c;memset(c.a,0,sizeof(c.a)); for(int i=0;i<n;++i)for(int k=0;k<n;++k)for(int j=0;j<n;++j)add(c.a[i][j],1ULL*a[i][k]*o.a[k][j]%mod); return c; } }I,M,orig; mat ksm(mat a,int b){ mat res=I; while(b){ if(b&1)res=res*a; a=a*a,b>>=1; } return res; } int main(){ int m,k;read(n,m,k); for(int i=0;i<n;++i)I.a[i][i]=1; for(int i=0;i<n;++i)read(orig.a[i][0]); for(int i=1;i<=m;++i)read(X[i],Y[i]); ll inv=ksm(m); for(int i=0;i<n;++i)M.a[i][i]=1; for(int i=1;i<=m;++i){ int x=X[i]-1,y=Y[i]-1; add(M.a[x][x],1LL*inv*(mod-inv2)%mod),add(M.a[x][y],1LL*inv*inv2%mod); add(M.a[y][y],1LL*inv*(mod-inv2)%mod),add(M.a[y][x],1LL*inv*inv2%mod); } mat o=ksm(M,k)*orig; for(int i=0;i<n;++i)printf("%d\n",o.a[i][0]); return 0; }
#include <bits/stdc++.h> using namespace std; long long INF = 1LL^60; using ll = long long; using vll = vector<ll>; using mll = map<ll, ll>; void solve(long long N, long long A, long long B){ cout << N - A + B<< endl; } int main(){ long long N; scanf("%lld",&N); long long A; scanf("%lld",&A); long long B; scanf("%lld",&B); solve(N, A, B); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int A,B,C; cin >> A >> B >> C; if(A > B ){ cout << "Takahashi" << endl; } else if(A == B && C == 1){ cout << "Takahashi" << endl; } else{ cout << "Aoki" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> z; pair<int, int> c; for (int i=0; i<n; i++) { cin >> c.first >> c.second; z.push_back(c); } bool flag = false; for (int i1=0; i1<n; i1++) { for (int i2=i1+1; i2<n; i2++) { for (int i3=i2+1; i3<n; i3++) { int p = (z[i1].first - z[i2].first) * (z[i1].second - z[i3].second); int q = (z[i1].first - z[i3].first) * (z[i1].second - z[i2].second); if (p == q) { flag = true; break; } } if (flag) break; } if (flag) break; } if (flag) cout << "Yes" << endl; else cout << "No" << endl; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++) #define repr(i, n) for (ll i = n; i >= 0; i--) #define pb push_back #define COUT(x) cout << (x) << "\n" #define COUTF(x) cout << setprecision(15) << (x) << "\n" #define ENDL cout << "\n" #define DF(x) x.erase(x.begin()) #define ALL(x) x.begin(), x.end() #define SORT(x) sort(ALL(x)) #define RSORT(x) sort(x.rbegin(), x.rend()) #define REVERSE(x) reverse(ALL(x)) #define MAX(x) *max_element(ALL(x)) #define MAXI(x) max_element(ALL(x)) - x.begin() #define SUM(x) accumulate(ALL(x), 0ll) #define COUNT(x, y) count(ALL(x), y); #define ANS cout << ans << "\n" #define YES cout << "YES\n"; #define NO cout << "NO\n"; #define Yes cout << "Yes\n"; #define No cout << "No\n"; #define init() \ cin.tie(0); \ ios::sync_with_stdio(false) #define LINE cerr << "[debug] line: " << __LINE__ << endl; #define debug(x) cerr << "[debug] " << #x << ": " << x << endl; #define debugV(v) \ cerr << "[debugV] " << #v << ":"; \ rep(z, v.size()) cerr << " " << v[z]; \ cerr << endl; using namespace std; using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vector<ll>>; using mll = map<ll, ll>; using qll = queue<ll>; using P = pair<ll, ll>; using vp = vector<P>; using vs = vector<string>; template <typename T> inline istream& operator>>(istream& i, vector<T>& v) { rep(j, v.size()) i >> v[j]; return i; } template <typename T1, typename T2> inline istream& operator>>(istream& i, pair<T1, T2>& v) { return i >> v.first >> v.second; } constexpr ll INF = 0x3f3f3f3f3f3f3f3f; constexpr ld PI = 3.141592653589793238462643383279; ll get_digit(ll x) { return to_string(x).size(); } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> 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; } signed main() { init(); ll N; cin >> N; vp A(N); cin >> A; rep(i, N) { rep(j, N) { rep(k, N) { if (i == j) continue; if (j == k) continue; if (i == k) continue; ll a = A[j].first - A[i].first; ll b = A[j].second - A[i].second; ll c = A[k].first - A[i].first; ll d = A[k].second - A[i].second; if (b * c == a * d) { Yes; return 0; } } } } No; return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <cmath> #include <string> #include <functional> using namespace std; struct mountain { string name; int height; }; int main() { int n; string input, most, second; long input_height, most_height, second_height; most_height = 0; second_height = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> input >> input_height; if (i == 0) { most = input; most_height = input_height; } else if (most_height < input_height) { second = most; second_height = most_height; most = input; most_height = input_height; } else { if (second_height < input_height) { second = input; second_height = input_height; } } } cout << second << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define mp make_pair #define pb push_back #define inF freopen("input.txt", "r", stdin); #define outF freopen("output.txt", "w", stdout); #define endl '\n' #define MOD 1000000007 #define mm(arr) memset(arr, 0, sizeof(arr)) #define F first #define S second #define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i];} #define coutArray(a,n) for(int i = 0; i < n; i++){cout << a[i] << " ";}cout << endl; #define ld long double #define PI 3.141592653589793238 #define all(v) v.begin(), v.end() #define sz(x) (int)(x.size()) #define int ll void solve(){ int n; cin >> n; vector<pair<int, string>> v(n); for(int i = 0; i < n; i++){ cin >> v[i].second >> v[i].first; } sort(all(v)); cout << v[n - 2].second << endl; } int32_t main(){ FAST int t = 1; //cin >> t; while(t--){ solve(); } return 0; }
#include<bits/stdc++.h> #define pi 3.141592653589793238 #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #define MOD 1000000007 #define INF 999999999999999999 #define pb push_back #define ff first #define ss second #define mt make_tuple #define ll long long #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); using namespace std; #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; ll gcd(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1; y = 0; return a; } ll x1, y1; ll d = gcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0, ll &g) { g = gcd(abs(a), abs(b), x0, y0); if (c % g) { return false; } x0 *= c / g; y0 *= c / g; if (a < 0) x0 = -x0; if (b < 0) y0 = -y0; return true; } typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); fast; ll T; cin>>T; while(T--) { ll n,s,k; cin>>n>>s>>k; ll val=n-s; ll g=__gcd(n,k); if(val%g!=0) { cout<<-1<<endl; continue; } ll pass=0; ll x0,y0; int vothing=find_any_solution(k,-n,val,x0,y0,pass); ll front=n/pass; if(x0<0) { ll c=-x0; ll to=c/front; to++; x0+=front*to; } if(x0>0) { ll to=x0/front; x0-=front*to; } cout<<x0<<endl; } return 0; }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; template<class S, class T> inline S min_L(S a,T b){ return a<=b?a:b; } inline int my_getchar_unlocked(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread_unlocked(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = my_getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite_unlocked(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar_unlocked(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar_unlocked('-'); } while(s--){ my_putchar_unlocked(f[s]+'0'); } } int X; int Y; int A[10000]; int main(){ rd(X); rd(Y); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(X*Y);Lj4PdHRW++){ rd(A[Lj4PdHRW]); } } X *= Y; { int e98WHCEY; int cTE1_r3A; if(X==0){ cTE1_r3A = 0; } else{ cTE1_r3A = A[0]; for(e98WHCEY=(1);e98WHCEY<(X);e98WHCEY++){ cTE1_r3A = min_L(cTE1_r3A, A[e98WHCEY]); } } { int xr20shxY; int WYIGIcGE; if(X==0){ WYIGIcGE = 0; } else{ WYIGIcGE = A[0]; for(xr20shxY=(1);xr20shxY<(X);xr20shxY++){ WYIGIcGE += A[xr20shxY]; } } wt_L(WYIGIcGE- X *cTE1_r3A); wt_L('\n'); } } return 0; } // cLay version 20201206-1 // --- original code --- // int X, Y, A[1d4]; // { // rd(X,Y,A(X*Y)); // X *= Y; // wt(sum(A(X)) - X * min(A(X))); // }
/* author : aryan57 created : 06-June-2021 18:04:35 IST */ #include <bits/stdc++.h> using namespace std; template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream &operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifndef ONLINE_JUDGE #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif #define int long long #define X first #define Y second #define pb push_back #define sz(a) ((int)(a).size()) #define all(a) (a).begin(), (a).end() #define F(i, a, b) for (int i = a; i <= b; i++) #define RF(i, a, b) for (int i = a; i >= b; i--) const int mxn = 1e5; const long long INF = 1e16; const int32_t M = 1000000007; // const int32_t M = 998244353; const long double pie = acos(-1); struct Edge { int u, v, d, c; }; void solve_LOG() { int n, m; cin >> n >> m; vector<Edge> adj[n + 1]; F(i, 1, m) { Edge e; cin >> e.u >> e.v >> e.c >> e.d; // if(e.u==e.v)continue; adj[e.u].push_back(e); adj[e.v].push_back(e); } vector<int> mn(n + 1, INF); mn[1] = 0; using pii = pair<int, int>; priority_queue<pii, vector<pii>, greater<pii>> q; q.push({0, 1}); while (!q.empty()) { int v = q.top().second; int mn_v = q.top().first; q.pop(); if (mn_v != mn[v]) continue; for (auto edge : adj[v]) { int c=edge.c; int d=edge.d; int to=-1; if(edge.u==v)to=edge.v; else to=edge.u; int temp=INF; int dd=(int)sqrt(d); F(t,max(0ll,dd-(mn[v]+1)-5),max(0ll,dd-(mn[v]+1)+5)) { temp=min(temp,mn[v]+t+c+d/(mn[v]+t+1)); } if (temp < mn[to]) { mn[to] = temp; q.push({mn[to], to}); } } } if (mn[n] == INF) { cout << "-1\n"; return; } cout << mn[n]; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifndef ONLINE_JUDGE // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); #endif #ifdef ARYAN_SIEVE sieve(); #endif #ifdef ARYAN_SEG_SIEVE segmented_sieve(); #endif #ifdef ARYAN_FACT fact_init(); #endif // cout<<fixed<<setprecision(10); int _t = 1; // cin>>_t; for (int i = 1; i <= _t; i++) { // cout<<"Case #"<<i<<": "; solve_LOG(); } return 0; } // parsed : 06-June-2021 18:04:32 IST
//This Code was made by Chinese_zjc_. #include <iostream> #include <iomanip> #include <algorithm> #include <vector> #include <bitset> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstring> #include <cstdio> #include <cstdlib> #include <map> #include <set> #include <time.h> // #include<windows.h> #define int long long #define double long double const double PI = acos(-1); const double eps = 0.0000000001; const int INF = 0x3fffffffffffffff; int n, m, dis[100005], a[100005], b[100005], c[100005], d[100005], e[100005]; std::vector<int> to[100005]; std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> que; signed main() { std::ios::sync_with_stdio(false); std::cin >> n >> m; for (int i = 1; i <= m; ++i) { std::cin >> a[i] >> b[i] >> c[i] >> d[i]; to[a[i]].push_back(i); to[b[i]].push_back(i); e[i] = sqrt(d[i]); } std::fill(dis + 1, dis + 1 + n, INF); que.push({dis[1] = 0, 1}); while (!que.empty()) { int now = que.top().second; if (dis[now] != que.top().first) { que.pop(); continue; } que.pop(); for (auto i : to[now]) { int nxt = a[i] ^ b[i] ^ now; for (int j = std::max(e[i] - 2, dis[now]), lim = j + 5; j <= lim; ++j) { if (dis[nxt] > c[i] + d[i] / (j + 1) + j) { dis[nxt] = c[i] + d[i] / (j + 1) + j; que.push({dis[nxt], nxt}); } } } } std::cout << (dis[n] >= INF ? -1 : dis[n]) << std::endl; return 0; }
#include<bits/stdc++.h> using namespace std; double k; double a,b,c,d,s,p; double f(double x) { return -b/k+a; } int main() { cin>>a>>b>>c>>d; b=-b; k=(b-d)/(a-c); s=f(0); printf("%.10lf",s); return 0; }
#include<bits/stdc++.h> #define loop(i, n) for(int i = 0; i < n; i++) #define pb push_back #define ll long long int #define vi vector<int> #define pi pair<int, int> #define W(t) int t; cin >> t; while(t--) #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ff first #define ss second #define rep(i, a, b)for(ll i = a; i <= b; i++) #define dep(i, a, b)for(ll i = a; i >= b; i--) #define all(x) x.begin(), x.end() #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++) #define PI 3.14159265 using namespace std; const ll mod = (ll)(998244353); vector<ll> read(int n){ vector<ll> v(n); loop(i, n) cin >> v[i]; return v; } vector<ll> fact, invf; ll powmod(ll a, ll b) { ll res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } void build(int n) { fact = vector<ll>(n+1, 1); invf = vector<ll>(n+1, 1); for (int i = 2; i <= n; i++) fact[i] = fact[i-1] * i % mod; invf[n] = powmod(fact[n], mod-2); for (int i = n-1; i >= 2; i--) invf[i] = invf[i+1] * (i+1) % mod; } ll nChoosek(int n, int k) { if (n < k) return 0; return fact[n] * invf[k] % mod * invf[n-k] % mod; } ll test = 0; void solve(){ ll i, j, k, m, n, cnt = 0, ans = 0, sum = 0; double sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; double res = (gy*sx + gx*sy)/(sy+gy); cout <<fixed << setprecision(10) << res << "\n"; test++; } int main(){ FIO #ifndef ONLINE_JUDGE freopen("C:/Codeforces/input.txt", "r", stdin); freopen("C:/Codeforces/output.txt", "w", stdout); #endif ll i = 1; //W(t) solve(); return 0; }
#include "bits/stdc++.h" //#include "atcoder/all" using namespace std; //using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a%b); } long long lcm(long long x, long long y) { return x * y / gcd(x, y); } unordered_map<int, int>mp; //約数列挙 void calc(int N) { for (int i = 1; i <= sqrt(N); i++) { if (0 == N % i) { mp[i] = gcd(mp[i], N); if (N != i * i) { mp[N / i] = gcd(mp[N / i], N); } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<int>A(N); rep(i, N) { cin >> A[i]; } int minA = 1e9; rep(i, N) { minA = min(minA, A[i]); } rep(i, N) { calc(A[i]); } int ans = 0; for (auto p : mp) { if ((p.first <= minA) && (p.first == p.second)) { ans++; } } cout << ans << endl; return 0; }
#include "bits/stdc++.h" #define rep(i,b) for(ll i=0;i<b;i++) #define ll long long using namespace std; /*--Input//////////////////////////////////////////////////*/ inline void IN(void){return;} template <typename First, typename... Rest> void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return; } #define SS(T, ...) T __VA_ARGS__; IN(__VA_ARGS__); #define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i; /*--Output/////////////////////////////////////////////////*/ #define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl #define First(n) cout << ((n) ? "First" : "Second" ) << endl inline int p(void){cout << endl; return 0;} template<class Head> int p(Head&& head){cout << head;p();return 0;} template<class Head,class... Tail> int p(Head&& head,Tail&&... tail){cout<<head<<" ";p(forward<Tail>(tail)...);return 0;} struct ProconInit { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; ProconInit() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); if(AUTOFLUSH) cout << unitbuf; } } PROCON_INIT; #define vl vector<ll> #define debug(x) cerr<<#x<<" "<<x<<'\n'; #define all(x) (x).begin(),(x).end() ll GCD(ll a, ll b) { if(a < b) return GCD(b, a); if(b==0){ return a; }else{ return GCD(b,a%b); } } 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; } bool cmp(pair<ll,ll> a, pair<ll,ll> b) { return a.second < b.second; } ll MOD=1e9+7; struct UnionFind { vector<int> par; UnionFind(int n) : par(n,-1) { } void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; int main(){ bool ok=true; SS(int,N,M); vector<vl> path(N); UnionFind uf(N); SV(ll,a,N); SV(ll,b,N); rep(i,M){ SS(int,c,d); c--;d--; uf.unite(c,d); } rep(i,N){ path.at(uf.root(i)).push_back(i); } rep(i,N){ ll a_sum=0; ll b_sum=0; //debug(path.at(i).size()); int t=path.at(i).size(); rep(j,t){ int po=path.at(i).back(); path.at(i).pop_back(); a_sum+=a[po]; b_sum+=b[po]; } if(a_sum!=b_sum)ok=false; } Yes(ok); }
#include <bits/stdc++.h> #define sz(v) ((int)(v).size()) #define all(v) ((v).begin()),((v).end()) #define allr(v) ((v).rbegin()),((v).rend()) #define pb push_back #define mp make_pair #define Y imag() #define X real() #define clr(v,d) memset( v, d ,sizeof(v)) #define angle(n) atan2((n.imag()),(n.real())) #define vec(a,b) ((b)-(a)) #define length(a) hypot( (a.imag()),(a.real()) ) #define normalize(a) (a)/(length(a)) #define dp(a,b) (((conj(a))*(b)).real()) #define cp(a,b) (((conj(a))*(b)).imag()) #define lengthsqrt(a) dp(a,a) #define rotate0( a,ang) ((a)*exp( point(0,ang) )) #define rotateA(about,p,ang) (rotate0(vec(about,p),ang)+about) #define lcm(a,b) ((a*b)/(__gcd(a,b))) #define reflection0(m,v) (conj((v)/(m))*(m)) #define reflectionA(m,v,p0) (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0 #define same(p1,p2) ( dp( vec(p1,p2),vec(p1,p2)) < eps ) #define point complex<long double> #define PI acos(-1) typedef long long ll ; typedef unsigned long long ull; const double eps= (1e-15); using namespace std; int dcmp(double a,double b){ return fabs(a-b)<=eps ? 0: (a>b)? 1:-1 ;} int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;} int setBit1(int num, int idx) {return num | (1<<idx);} ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);} ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);} void GO(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);} int countNumBit1(int mask) {int ret=0; while (mask) {mask &= (mask-1);++ret; }return ret;} const ll N=1000+9,mod=998244353,sh=1000000,inf=1000000; int n,m,a[N],b[N],mem[N][N][6]; int solve(int id1,int id2,int fl){ if(id1>=n&&id2>=m){ if(fl==1){ return 0; } return inf; } if(id1>=n){ if(fl==1) return (m-id2); return inf; } if(id2>=m){ if(fl==1) return (n-id1); return inf; } int &ret=mem[id1][id2][fl]; if(ret!=-1) return ret; int ans=inf; if(fl==2){ if(id1<n&&id2<m) ans=min(ans,(a[id1]!=b[id2])+solve(id1+1,id2+1,1)); if(id1<n&&id2+1<m) ans=min(ans,1+solve(id1,id2+1,3)); if(id2<m&&id1+1<n) ans=min(ans,1+solve(id1+1,id2,4)); } else if(fl==1){ if(id1<n&&id2<m) ans=min(ans,(a[id1]!=b[id2])+solve(id1+1,id2+1,1)); if(id1<n&&id2+1<m) ans=min(ans,1+solve(id1,id2+1,3)); if(id2<m&&id1+1<n) ans=min(ans,1+solve(id1+1,id2,4)); } else if(fl==3){ if(id1<n&&id2<m) ans=min(ans,(a[id1]!=b[id2])+solve(id1+1,id2+1,1)); if(id1<n&&id2+1<m) ans=min(ans,1+solve(id1,id2+1,3)); } else if(fl==4){ if(id1<n&&id2<m) ans=min(ans,(a[id1]!=b[id2])+solve(id1+1,id2+1,1)); if(id2<m&&id1+1<n) ans=min(ans,1+solve(id1+1,id2,4)); } return ret=ans; } int main(){ GO(); int T=1; //cin>>T; while(T--){ cin>>n>>m; for(int i=0;i<n;i++) cin>>a[i]; for(int i=0;i<m;i++) cin>>b[i]; clr(mem,-1); cout<<solve(0,0,2)<<"\n"; } }
#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> #include<bits/stdc++.h> using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; #define pf printf #define sf(a) scanf("%d",&a) #define sfl(a) scanf("%lld",&a) #define sff(a,b) scanf("%d %d",&a,&b) #define sffl(a,b) scanf("%lld %lld",&a,&b) #define sfff(a,b,c) scanf("%d %d %d",&a,&b,&c) #define sfffl(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define sffff(a,b,c,d) scanf("%d %d %d %d",&a,&b,&c,&d) #define sffffl(a,b,c,d) scanf("%lld %lld %lld %lld",&a,&b,&c,&d) #define sfffff(a,b,c,d,e) scanf("%d %d %d %d %d",&a,&b,&c,&d,&e) #define sfffffl(a,b,c,d,e) scanf("%lld %lld %lld %lld %lld",&a,&b,&c,&d,&e) #define sfc(a) scanf("%c",&a) #define pii pair<int,int> #define ms(a,b) memset(a,b,sizeof(a)) #define pb(a) push_back(a) #define pbp(a,b) push_back({a,b}) #define db double #define ft float #define ll long long #define ull unsigned long long #define pii pair<int,int> #define ff first #define ss second #define sz(x) x.size() #define all(x) x.begin(),x.end() #define CIN ios_base::sync_with_stdio(0); cin.tie(0) #define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c) #define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c) #define for0(i,n) for(int i=0;i<n;i++) #define for1(i,n) for(int i=1;i<=n;i++) #define forrev(i,n) for(int i=n-1; i>=0; i--) #define forab(i,a,b) for(int i=a;i<=b;i++) #define forba(i,b,a) for(int i=b;i>=a;i--) #define stlloop(x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++) #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a)*((b)/gcd(a,b))) #define case1(z) cout<<"Case "<<z<<": " #define case2(z) printf("Case %d: ",z) #define PI acos(-1) //3.14159265358979323846264338328 #define valid(tx,ty) tx>=0 && tx<row && ty>=0 && ty<col #define intlim 2147483648 #define N 300005 #define inf 100000008 #define mod 1000000007 /*------------------------------Graph Moves----------------------------*/ //const int fx[]={+1,-1,+0,+0}; //const int fy[]={+0,+0,+1,-1}; //const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move //const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move //const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move //const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move /*---------------------------------------------------------------------*/ ll a[1003],b[1003]; ll dp[1003][1003]; int main() { ll n,m,i,j; cin>>n>>m; for1(i,n)cin>>a[i]; for1(i,m)cin>>b[i]; for0(i,n+1)for0(j,m+1)dp[i][j] = max(i,j); for1(i,n) { for1(j,m) { if(a[i]==b[j]) { dp[i][j] = min3(dp[i][j],dp[i-1][j-1],min(dp[i-1][j]+1,dp[i][j-1]+1)); } else { dp[i][j] = min3(dp[i][j],dp[i-1][j-1]+1,min(dp[i-1][j]+1,dp[i][j-1]+1)); } } } cout<<dp[n][m]<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define input(v) for (auto &i : v) cin >> i; #define print(v) for (auto &j : v) cout << j << " "; cout << "\n"; void solve() { int n; cin>>n; vector<int>a(n),b(n);input(a);input(b); int mx = max(a[0],b[0]); int mn = min(a[0],b[0]); for(int i=0;i<n;i++) { mx=min(mx,max(a[i],b[i])); mn=max(mn,min(a[i],b[i])); } if(mx-mn+1<0) { cout<<"0\n"; return; } cout<<mx-mn+1<<"\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr);cout.tie(nullptr); int testcases=1; //cin>>testcases; while(testcases--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define quickread \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define pb push_back #define all(x) x.begin(), x.end() #define hii cout << "Hii there Sparky! \n" #define endl '\n' #define mod 1000000007 //#define mod 998244353 #define maxn 100010 //check the limits, dummy void solve() { int n; cin >> n; int arr[n]; int mx = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; mx = max(arr[i], mx); } int arr2[n]; int mn = INT_MAX; for (int i = 0; i < n; i++) { cin >> arr2[i]; mn = min(arr2[i], mn); } cout << max(0LL, mn - mx + 1); } int32_t main() { quickread; int test = 1; //cin >> test; for (int i = 0; i < test; i++) { solve(); } return 0; }
/* * Written by: Richw818 * Lang: C++17 * main.cpp */ #include<bits/stdc++.h> using namespace std; //Macros #define ll long long #define ld long double #define ull unsigned long long #define uint unsigned int #define pb push_back #define pf push_front #define eb emplace_back #define ef emplace_front #define str string #define fi first #define se second #define REP(i, n) for(int i = 0; i < (int)n; ++i) #define sz(a) (int)(a.size()) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() //IO template<class H, class T> void read(pair<H, T> &p); template<class T> void read(vector<T>& v); template<class T> void read(T& t){ cin >> t; } template<class H, class... T> void read(H& h, T&... t){ read(h); read(t...); } template<class T> void read(vector<T>& v){ for(auto &t : v) read(t); } template<class H, class T> void read(pair<H, T> &p){ read(p.fi, p.se); } void setIn(str s){ freopen(s.c_str(), "r", stdin); } void setOut(str s){ freopen(s.c_str(), "w", stdout); } void setIO(str s = ""){ cin.tie(0)->sync_with_stdio(0); if(sz(s)) setIn(s+".in"), setOut(s+".out"); } //Debug void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} template<class H, class... T> void dbg(H& h, T&... t){ dbg(h); dbg(t...); } #ifdef LOCAL #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x); #else #define dbg(x...) #endif //Constants ll MODS[2] = {1000000007, 998244353}; const ll MOD = MODS[0]; const int INF = (int)1e9; const ll LINF = (ll)1e18; const ld PI = acos((ld)-1.0); const int MAXN = (int)3e5+7; //Code void solve(){ int n, m; read(n, m); vector<ll> h(n), w(m); read(h, w); sort(all(h)); vector<ll> pref(n+1); for(int i = 1; i <= n; ++i) pref[i] = pref[i-1] + (i % 2 ? -h[i-1] : h[i-1]); ll ans = LINF; for(ll val : w){ int ind = lower_bound(all(h), val) - h.begin(); ll curr = 2 * pref[ind] - pref[n]; curr += (ind % 2 ? val : -val); ans = min(ans, curr); } cout << ans; } int main(){ setIO(); int t = 1; //read(t); while(t--) solve(); return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> #include <cstdint> 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 rep(i,n) for (int i = 0; i < (n); ++i) typedef long long ll; using P = pair<int,int>; const int INF=1001001001; const int mod =998244353; int main() { int N,M; cin>>N>>M; vector<ll>H(N),W(M); for(int i=0;i<N;i++){cin>>H[i];} for(int i=0;i<M;i++){cin>>W[i];} sort(H.begin(),H.end()); vector<ll>l(N/2+1),r(N/2+1); for(int i=0;i<N/2;i++){ l[i+1]=l[i]+H[2*i+1]-H[2*i]; r[i+1]=r[i]+H[2*i+2]-H[2*i+1];//1インデックス } ll ans=INF; for(int i=0;i<M;i++){ int x=lower_bound(H.begin(),H.end(),W[i])-H.begin(); if((x&1)==1){x^=1;}//0インデックスならWは偶数番目としか組めない。 chmin(ans,abs(H[x]-W[i])+l[x/2]+r[N/2]-r[x/2]); } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; 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; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan0     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, M, X, Y; int A[101010], B[101010], T[101010], K[101010]; vector<pair<int, pair<int,int>>> E[101010]; //--------------------------------------------------------------------------------------------------- template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; int vis[101010]; ll D[101010]; ll dijk() { rep(i, 0, N) D[i] = infl; rep(i, 0, N) vis[i] = 0; min_priority_queue<pair<ll, int>> que; D[X] = 0; que.push({ 0, X }); while (!que.empty()) { auto q = que.top(); que.pop(); ll cst = q.first; int cu = q.second; if (cu == Y) return D[Y]; if (vis[cu]) continue; vis[cu] = 1; fore(p, E[cu]) { int to = p.first; int T = p.second.first; int K = p.second.second; ll cst2 = (cst + K - 1) / K * K + T; if (chmin(D[to], cst2)) que.push({ D[to], to }); } } return -1; } //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> M >> X >> Y; X--; Y--; rep(i, 0, M) { cin >> A[i] >> B[i] >> T[i] >> K[i]; A[i]--; B[i]--; E[A[i]].push_back({ B[i], {T[i], K[i]} }); E[B[i]].push_back({ A[i], {T[i], K[i]} }); } ll ans = dijk(); cout << ans << endl; }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<cstdio> #include<cstring> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; if ((tmp & ma0) ^ ma0) { int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if ((ct = *ci++) >= '0') { tmp = tmp * 10 + ct - '0'; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } } return tmp; } int tmp[100000], P[50000], Q[50000]; void pakuri_sort(int N, int A[]) { const int b = 8; rep(k, 4) { int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i]; for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i]; k++; rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i]; for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i]; } } int main() { //cin.tie(0); //ios::sync_with_stdio(false); int N = getint(); ll kotae = 0; rep(i, N) { tmp[i] = getint(); kotae += tmp[i]; } rep(i, N) { int b = getint(); if (i & 1) P[i >> 1] = (b - tmp[i] + 1000000000); else Q[i >> 1] = (b - tmp[i] + 1000000000); } pakuri_sort(N / 2, P); pakuri_sort(N / 2, Q); int k = N / 2 - 1; while (k >= 0) { int a = P[k] - 1000000000; int b = Q[k] - 1000000000; if (a + b <= 0) break; kotae += a + b; k--; } printf("%lld\n", kotae); Would you please return 0; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define setbits(x) __builtin_popcountll(x) #define max(a,b) (a>b?a:b) #define min(a,b) (a<b?a:b) #define int long long int #define ff first #define ss second #define float long double void solve() { int n=0,x=0,y=0; cin>>n>>x>>y; float dist=sqrt((float)x*(float)x+(float)y*(float)y); float temp=dist; int temp2=(int)temp; if((float)temp2 == dist) { if((int)dist%n == 0) { cout<<dist/n<<"\n"; return; } } for(int i=2;i<1e6;i++) { if(dist < (float)(i*n)) { cout<<i<<"\n"; return; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(); int t; //cin>>t; t=1; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ long long int a,b,c; cin >> a >> b >> c; long long int x=(a*(a+1))/2,y=(b*(b+1))/2,z=(c*(c+1))/2; x%=998244353; y%=998244353; z%=998244353; long long int d=x*y%998244353; cout << d*z%998244353 << endl; }
#include<bits/stdc++.h> #define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i) #define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i) #define foreach(i, n) for(auto &i:(n)) #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) #define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE #define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; //const ll MOD = (ll)1e9+7; const ll MOD = 998244353; const int INF = (ll)1e9+7; const ll INFLL = (ll)1e18; template<class t> using vvector = vector<vector<t>>; template<class t> using vvvector = vector<vector<vector<t>>>; template<class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;} template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;} #ifdef DEBUG #define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl; #else #define debug(x) (void)0 #endif namespace templates{ ll modpow(ll x, ll b,ll mod=MOD){ ll res = 1; while(b){ if(b&1)res = res * x % mod; x = x * x % mod; b>>=1; } return res; } ll modinv(ll x){ return modpow(x, MOD-2); } bool was_output = false; template<class t> void output(t a){ if(was_output)cout << " "; cout << a; was_output = true; } void outendl(){ was_output = false; cout << endl; } ll in(){ ll res; cin >> res; return res; } template<class t> istream& operator>>(istream&is, vector<t>&x){ for(auto &i:x)is >> i; return is; } template<class t, class u> istream& operator>>(istream&is, pair<t, u>&x){ is >> x.first >> x.second; return is; } template<class t> t in(){ t res; cin >> res; return res; } template<class t> void out(t x){ cout << x; } template<class t> vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){ sort(line.begin(),line.end(),comp); return line; } template<class t> vector<t> reversed(vector<t> line){ reverse(line.begin(),line.end()); return line; } } using namespace templates; vector<ll> func(){ int n = in(); vector<ll> A(n); vector<ll> B(n); foreach(i,A)i=in(); foreach(i,B)i=in(); vector<ll> AM(n,0); rep(i,n){ chmax(AM[i],A[i]); if(i+1<n)chmax(AM[i+1],AM[i]); } ll CM = 0; vector<ll> res(n); rep(i,n){ chmax(CM,AM[i] * B[i]); res[i] = CM; } return res; } int main(){ vector<ll> ans = func(); foreach(i,ans){ cout << i << endl; } return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; #define rep(i, n) for (ll i = 0; i < (ll)(n); ++i) #define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++) #define repr(i, n) for (ll i = n; i >= 0; i--) #define pb push_back #define COUT(x) cout << (x) << "\n" #define COUTF(x) cout << setprecision(21) << (x) << "\n" #define ENDL cout << "\n" #define DF(x) x.erase(x.begin()) #define ALL(x) x.begin(), x.end() #define SORT(x) sort(ALL(x)) #define RSORT(x) sort(x.rbegin(), x.rend()) #define REVERSE(x) reverse(ALL(x)) #define MAX(x) *max_element(ALL(x)) #define MAXI(x) max_element(ALL(x)) - x.begin() #define SUM(x) accumulate(ALL(x), 0ll) #define COUNT(x, y) count(ALL(x), y); #define ANS cout << ans << "\n" #define YES cout << "YES\n"; #define NO cout << "NO\n"; #define Yes cout << "Yes\n"; #define No cout << "No\n"; #define init() \ cin.tie(0); \ ios::sync_with_stdio(false) #define LINE cerr << "[debug] line: " << __LINE__ << endl; #define debug(x) cerr << "[debug] " << #x << ": " << x << endl; #define debugV(v) \ cerr << "[debugV] " << #v << ":"; \ rep(z, v.size()) cerr << " " << v[z]; \ cerr << endl; using namespace std; using ll = long long; using ld = long double; using vll = vector<ll>; using vvll = vector<vector<ll>>; using mll = map<ll, ll>; using qll = queue<ll>; using P = pair<ll, ll>; using vp = vector<P>; using vs = vector<string>; template <typename T> inline istream& operator>>(istream& i, vector<T>& v) { rep(j, v.size()) i >> v[j]; return i; } template <typename T1, typename T2> inline istream& operator>>(istream& i, pair<T1, T2>& v) { return i >> v.first >> v.second; } constexpr ll INF = 0x3f3f3f3f3f3f3f3f; constexpr ld PI = 3.141592653589793238462643383279; ll get_digit(ll x) { return to_string(x).size(); } ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } void warshall_floyd(ll N, vvll& d) { rep(k, N) rep(i, N) rep(j, N) chmin(d[i][j], d[i][k] + d[k][j]); } signed main() { init(); ll N; cin >> N; vs S(N); cin >> S; vvll D(100, vll(100, INF)); rep(i, N) rep(j, N) { if (S[i][j] == '1') D[i][j] = 0; if (i == j) D[i][j] = 0; } warshall_floyd(N, D); ld ans = 0; rep(i, N) { ll c = 0; rep(j, N) if (D[j][i] == 0) c++; ans += 1.0 / c; } COUTF(ans); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int #define all(xs) xs.begin(), xs.end() #define rall(xs) xs.rbegin(), xs.rend() #define sz(x) (int)((x).size()) #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define mp make_pair #define ff first // #define ss second #define prec(n) fixed<<setprecision(n) #define debug(x) cout << (#x) << " is " << (x) << "\n" #define debug2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << "\n" #define debug3(x, y,z) cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "\n" #define yes cout<<"YES"; #define no cout<<"NO"; #define ll cout<<"\n"; #define nil 0 #define inf 1e15 #define inf18 1e18 #define SZ 200005 int gcd(int A,int B){ return (B == 0) ? A : gcd(B, A % B);} int lcm(int A,int B){ return A * B / gcd(A, B);} priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq; const int nn=1e5+10; bool permutation(vector<int>arr, int n) { // Set to check the count // of non-repeating elements set<int> hash; int maxEle = 0; for (int i = 0; i < n; i++) { // Insert all elements in the set hash.insert(arr[i]); // Calculating the max element maxEle = max(maxEle, arr[i]); } if (maxEle != n) return false; // Check if set size is equal to n if (hash.size() == n) return true; return false; } void solve(){ int n;cin>>n; map<int,int>m; set<int> ss; vector<int> a(n); for(auto &i:a){ cin>>i; } if (permutation(a, n)) cout << "Yes" << endl; else cout << "No" << endl; } signed main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int t=1; // cin>>t; for(int i=0;i<t;i++){ // cout<<"Case #"<<i+1<<": "; solve(); } }
#define _GLIBCXX_DEBUG #include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> A(N), check(N); for (int &x : A) cin >> x; for (int i = 0; i < N; i++) { if (check.at(A.at(i) - 1) == 1) { cout << "No" << endl; return 0; } check.at(A.at(i) - 1) = 1; } cout << "Yes" << endl; return 0; }
#include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <deque> #include <complex> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <cstring> #include <ctime> #include <iterator> #include <bitset> #include <numeric> #include <list> #include <iomanip> #include <cassert> #if __cplusplus >= 201103L #include <array> #include <tuple> #include <initializer_list> #include <unordered_set> #include <unordered_map> #include <forward_list> #include <random> using namespace std; #define cauto const auto& #define ALL(v) begin(v),end(v) #else #define ALL(v) (v).begin(),(v).end() #endif namespace{ typedef long long LL; typedef pair<int,int> pii; typedef pair<LL,LL> pll; typedef vector<int> vint; typedef vector<vector<int> > vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long> > vvll, vvLL; #define VV(T) vector<vector< T > > template <class T> void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){ v.assign(a, vector<T>(b, t)); } template <class T> inline bool chmin(T &x, const T &y){ if(y < x){ x = y; return true; } return false; } template <class T> inline bool chmax(T &x, const T &y){ if(x < y){ x = y; return true; } return false; } template <class F, class T> void convert(const F &f, T &t){ stringstream ss; ss << f; ss >> t; } template <class Con> string concat(const Con &c, const string &spr){ stringstream ss; typename Con::const_iterator it = c.begin(), en = c.end(); bool fst = true; for(; it != en; ++it){ if(!fst){ ss << spr; } fst = false; ss << *it; } return ss.str(); } template <class Con, class Fun> vector<typename Con::value_type> cfilter(const Con &c, Fun f) { vector<typename Con::value_type> ret; typename Con::const_iterator it = c.begin(), en = c.end(); for(; it != en; ++it){ if(f(*it)){ ret.emplace_back(*it); } } return ret; } #if __cplusplus >= 201103L template <class Con, class Fun> auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> { vector<decltype(f(*c.begin()))> ret; ret.reserve(c.size()); for(const auto &x: c){ ret.emplace_back(f(x)); } return ret; } #endif #if __cplusplus >= 201402L #define lambda(e) ([&](const auto &_){ return (e); }) #define lambda2(e) ([&](const auto &_a, const auto &_b){ return (e); }) #endif #define REP(i,n) for(int i=0;i<int(n);++i) #define RALL(v) (v).rbegin(),(v).rend() #define tget(t,i) get<i>(t) #define MOD 1000000007LL #define EPS 1e-8 void mainmain(){ int n; cin >> n; const int m = 100010; vector<char> dp(m); dp[0] = 1; int s = 0; REP(i, n){ int t; cin >> t; s += t; for(int j = m - 1; j >= t; --j){ dp[j] |= dp[j - t]; } } int ans = s; REP(i, m){ if(dp[i]){ chmin(ans, max(i, s - i)); } } cout << ans << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(4); mainmain(); }
#include<iostream> #include<vector> #include<string> #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i) #define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;} template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;} int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> T(N); for (auto& t : T) { cin >> t; } vector<int> cumsum(N + 1, 0); rep(i, 0, N) { cumsum[i + 1] = cumsum[i] + T[i]; } vector<vector<bool>> dp(N + 1, vector<bool>(100001, false)); dp[0][0] = true; rep(i, 0, N) rep(j, 0, 100001) { if (!dp[i][j]) { continue; } int another = cumsum[i] - j; dp[i + 1][max(j + T[i], another)] = true; dp[i + 1][max(j, another + T[i])] = true; } rep(i, 0, 100001) { if (dp[N][i]) { cout << i << endl; return 0; } } return 0; }
#include <iostream> #include <cmath> #include <string> #include <vector> #include <algorithm> #include <utility> #include <tuple> #include <cstdint> #include <cstdio> #include <map> #include <queue> #include <set> #include <stack> #include <deque> #include <unordered_map> #include <unordered_set> #include <bitset> #include <cctype> #include <climits> #include <cassert> #include <fstream> #define rep(i, n) for(int i = 0; i < n; i++) #define per(i, n) for(int i = n - 1; i >= 0; i--) using ll = long long; #define vi vector<int> #define vvi vector<vi> #define vl vector<ll> #define pii pair<int, int> #define pll pair<ll, ll> #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mod 1000000007 using namespace std; template<class T, class U> T &chmax(T &a, const U &b){ if(a < b) a = b; return a; } template<class T, class U> T &chmin(T &a, const U &b){ if(a > b) a = b; return a; } #define y first #define x second constexpr bool istest = false; constexpr int n = 50; ifstream file; ofstream out; void input_setup(){ if(istest){ cout << "write the in file number\n"; string s; cin >> s; file.open("C:/Users/souta/downloads/ahc/tools/in/" + s + ".txt"); if(file.fail()){ cout << "the file was not found\n"; return; } cin.rdbuf(file.rdbuf()); } } //答えるときのやつ string ans = ""; void output(){ if(istest){ out.open("C:/users/souta/downloads/ahc/tools/out.txt"); if(out.fail()){ cout << "the file was not found\n"; return; } cout.rdbuf(out.rdbuf()); } cout << ans << "\n"; } pii st_pos; vvi tiles(n, vi(n)), point(n, vi(n)); void inputs(){ //start position cin >> st_pos.y >> st_pos.x; //tiles rep(i, n) rep(j, n) cin >> tiles[i][j]; //points rep(i, n) rep(j, n) cin >> point[i][j]; } //過去に同じ番号のタイルを通ったことがあるのかを確認する bool checkVisited(const int &Y, const int &X, const vector<pii> &logs){ int col = tiles[Y][X]; for(auto a : logs){ if(col == tiles[a.y][a.x]) return true; } return false; } //とりあえずBFSで探索してみる int dy[] = {-1,1,0,0}; int dx[] = {0,0,-1,1}; char dir[] = {'U', 'D', 'L', 'R'}; void bfs(){ priority_queue<pair<pii, int>> que; vvi checked(n, vi(n, -1)); checked[st_pos.y][st_pos.x] = point[st_pos.y][st_pos.x]; vector<vector<vector<pii>>> logs(n, vector<vector<pii>>(n)); logs[st_pos.y][st_pos.x].push_back(make_pair(st_pos.y, st_pos.x)); pii bestpos = st_pos; int mx = point[st_pos.y][st_pos.x]; rep(loop, 10){ vector<pii> temp = logs[bestpos.y][bestpos.x]; logs = vector<vector<vector<pii>>>(n, vector<vector<pii>>(n)); checked = vvi(n, vi(n, -1)); logs[bestpos.y][bestpos.x] = temp; checked[bestpos.y][bestpos.x] = mx; que.push(make_pair(bestpos, mx)); while(!que.empty()){ pii pos; int score; tie(pos, score) = que.top(); que.pop(); if(checked[pos.y][pos.x] > score) continue; rep(i, 4){ int Y = pos.y + dy[i]; int X = pos.x + dx[i]; if(Y >= n || X >= n || Y < 0 || X < 0) continue; if(checkVisited(Y, X, logs[pos.y][pos.x])) continue; int v = score + point[Y][X]; if(checked[Y][X] == -1 || checked[Y][X] < v){ que.push(make_pair(make_pair(Y, X), v)); checked[Y][X] = v; logs[Y][X] = logs[pos.y][pos.x]; logs[Y][X].push_back(make_pair(Y, X)); } } } mx = 0; bestpos = st_pos; rep(i, n) rep(j, n){ if(mx < checked[i][j]){ bestpos = make_pair(i, j); mx = checked[i][j]; } } } vector<pii> track = logs[bestpos.y][bestpos.x]; pii last = st_pos; for(auto a : track){ rep(i, 4){ if(last.y + dy[i] == a.y && last.x + dx[i] == a.x){ ans += dir[i]; break; } } last = a; } } int main(){ input_setup(); inputs(); bfs(); output(); }
#include <bits/stdc++.h> using namespace std; void used_tile(vector<vector<int> > &t,int x,int y) { if(t[x][y]==t[x-1][y]) t[x-1][y]=-1; else if(t[x][y]==t[x+1][y]) t[x+1][y]=-1; else if(t[x][y]==t[x][y-1]) t[x][y-1]=-1; else if(t[x][y]==t[x][y+1]) t[x][y+1]=-1; t[x][y]=-1; } int cant_move(vector<vector<int> > t,int x,int y) { if(t[x-1][y]==-1&&t[x+1][y]==-1&&t[x][y-1]==-1&&t[x][y+1]==-1) return 1; else return 0; } void can_move(vector<vector<int> > t,int x,int y,vector<int> &can) { if(t[x-1][y]!=-1) can[2]=1; if(t[x+1][y]!=-1) can[3]=1; if(t[x][y-1]!=-1) can[0]=1; if(t[x][y+1]!=-1) can[1]=1; } void random(int &x,int &y,vector<int> can,vector<string> &answer){ int select=rand()%can.size(); if(select==0&&can[select]==1) {y=y-1; answer.push_back("L");} else if(select==1&&can[select]==1) {y=y+1;answer.push_back("R");} else if(select==2&&can[select]==1) {x=x-1;answer.push_back("U");} else if(select==3&&can[select]==1) {x=x+1;answer.push_back("D");} } void to_right(int &x,int &y,vector<int> can,vector<string> &answer) { if(can[1]==1) {y=y+1;answer.push_back("R");} else if(can[3]==1) {x=x+1;answer.push_back("D");} else if(can[2]==1) {x=x-1;answer.push_back("U");} else if(can[0]==1) {y=y-1;answer.push_back("L");} } void circle(int &x,int &y,vector<int> can,vector<string> &answer,int &type) { if(type==1){ if(x>35) type=2; if(can[3]==1){x=x+1;answer.push_back("D");} else if(can[1]==1) {y=y+1;answer.push_back("R");} else if(can[0]==1) {y=y-1;answer.push_back("L");} else if(can[2]==1) {x=x-1;answer.push_back("U");} } else if(type==2){ if(y>35) type=3; if(can[1]==1) {y=y+1;answer.push_back("R");} else if(can[3]==1) {x=x+1;answer.push_back("D");} else if(can[2]==1) {x=x-1;answer.push_back("U");} else if(can[0]==1) {y=y-1;answer.push_back("L");} } else if(type==3){ if(x<15) type=4; if(can[2]==1){x=x-1;answer.push_back("U");} else if(can[1]==1) {y=y+1;answer.push_back("R");} else if(can[0]==1) {y=y-1;answer.push_back("L");} else if(can[3]==1) {x=x+1;answer.push_back("D");} } else if(type==4){ if(y<15) type=1; if(can[0]==1) {y=y-1;answer.push_back("L");} else if(can[3]==1) {x=x+1;answer.push_back("D");} else if(can[2]==1) {x=x-1;answer.push_back("U");} else if(can[1]==1) {y=y+1;answer.push_back("R");} } } int main() { vector<string> answer; vector<vector<int> > t(52,vector<int>(52)); vector<vector<int> > p(52,vector<int>(52)); int si,sj; cin>>si>>sj; si=si+1; sj=sj+1; for(int i=1;i<51;i++){ for(int j=1;j<51;j++) cin>>t[i][j]; } for(int i=1;i<51;i++){ for(int j=1;j<51;j++) cin>>p[i][j]; } for(int i=0;i<52;i++){ t[0][i]=-1; t[51][i]=-1; t[i][0]=-1; t[i][51]=-1; } used_tile(t,si,sj); int flag=0; int type=1; while(flag==0){ vector<int> can(4,0); can_move(t,si,sj,can); circle(si,sj,can,answer,type); used_tile(t,si,sj); flag=cant_move(t,si,sj); } for(int i=0;i<answer.size();i++) cout<<answer[i]; cout<<endl; }
/*ver 7*/ #include <bits/stdc++.h> using namespace std; void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);} using ll = long long; using ld = long double; using vl = vector<ll>; using vd = vector<ld>; using vs = vector<string>; using vb = vector<bool>; using vvl = vector<vector<ll>>; using vvd = vector<vector<ld>>; using vvs = vector<vector<string>>; using vvb = vector<vector<bool>>; using pll = pair<ll,ll>; using mll = map<ll,ll>; template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; #define each(x,v) for(auto& x : v) #define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++) #define rep(i,n) for(ll i=0;i<(ll)n;i++) #define pb push_back #define eb emplace_back #define fi first #define se second #define mp make_pair const ll INF = 1LL << 60; #define CLR(mat,f) memset(mat, f, sizeof(mat)) #define IN(a, b, x) (a<=x&&x<=b) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除 #define debug cout << "line : " << __LINE__ << " debug" << endl; #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ind(...) long double __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}} template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}} template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}} template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}} template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}} void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);} void die(){cout << endl;exit(0);} template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);} template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;} template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}} template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}} template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}} template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}} template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}} #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;} ll ceilDiv(ll a,ll b) {return (a+b-1)/b;} ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離 ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a,ll b){ return a / gcd(a,b) * b;} template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } #define YES(n) ((n) ? "YES" : "NO" ) #define Yes(n) ((n) ? "Yes" : "No" ) #define yes(n) ((n) ? "yes" : "no" ) int main(){ init(); inl(a,b,w);w*=1000; ll mx=w/a; ll mn=ceil((ld)w/b); if(mx<mn)die("UNSATISFIABLE"); out(mn,mx); return 0; }
#include <iostream> #include <string> #include <vector> #include <math.h> #include <algorithm> #include <iomanip> using namespace std; int a,b,w; #define PI 3.14159265359 ; int main() { cin >> a >> b >> w; w*=1000; int min=0; int max=0; min = ceil((double)w/b); max = w/a; if(max < min){ cout << "UNSATISFIABLE"; } else{ cout << min << " " << max; } return 0; }