code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define fastio ios_base::sync_with_stdio(0);cin.tie(0) #define fp(i,a,b) for(ll i=a ; i<b ; i++) #define fn(i,a,b) for(int i=a ; i>=b ; i--) #define ones(x) __builtin_popcount(x) #define pb push_back #define mk make_pair #define ff first #define ss second #define all(x) x.begin(),x.end() #define dbg(x) cout << (#x) << " = " << x << " " #define fini cout << "\n"; #define line cout << "-----------------------------------\n"; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef tree< ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int read (){ int x = 0, f = 1; char s = getchar();while (s < '0' || s > '9') {if (s == '-') f = -1; s = getchar();} while (s >= '0' && s <= '9') x = x * 10 + s - '0', s = getchar(); return x * f; } const int M=1e2+7; const int N=1e6+7; const int inf=1e9; const ll mod=1e9+7; const ll mod2=2e9+11; int a,b,w; ll mn=inf,mx; void go(int ide){ cin >> a >> b >> w; w *= 1000; fp(i,1,1000001){ if (a*i <= w && w <= b*i){ mn = min(mn, i); mx = max(mx, i); } } if (mn == inf || !mx){ cout << "UNSATISFIABLE\n"; return; } cout << mn << " " << mx << "\n"; } int main(){ fastio; int tst=1; // cin >> tst; // cout << fixed << setprecision(12); fp(i,0,tst) go(i+1); return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ordered_set tree<int, null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> #define int long long int #define ull unsigned long long #define pb push_back #define inf 1e18 #define mk make_pair #define ld long double #define mod 998244353 #define fi first #define se second #define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define test int t; cin>>t; while(t--) #define setbits __builtin_popcount #define endl '\n' #define LOCAL #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i));} sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " int32_t main() { fastIO; ld a,b; cin>>a>>b; ld val=a/(ld)100; ld ans=val*b; cout<<fixed<<setprecision(10)<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; #define ll unsigned long long int #define mod 1000000007 bool isPrime[1000001]; ll power(ll a,ll b) { ll result = 1; while(b>0) { if(b%2) { result = (a*result)%mod; } a = (a*a)%mod; b = b/2; } return result; } int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } void sieve() { memset(isPrime,1,sizeof(isPrime)); isPrime[0] = isPrime[1] = 0; for(int i=2;i*i<=1000000;i++) { if(isPrime[i]) { for(int j=i*i;j<=1000000;j+=i) { isPrime[j] = 0; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL),cout.tie(NULL); int t,n; t = 1; for(int i=0;i<t;i++) { int n,k,m; cin>>n>>k>>m; int req = n*m; int sum =0,x; for(int i=0;i<n-1;i++) { cin>>x; sum+=x; } int ans = max(req-sum,0); if(ans>k) { cout<<"-1"; } else { cout<<ans; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n,k,m,a,x; cin >> n >> k >> m; int sum = 0; for(int i = 0; i < (n -1); i++){ cin >> a; sum += a; } x = m * n - sum; x = 0 > x ? 0 : x; x = k >= x ? x : -1 ; cout << x << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template<class T> using vc = vector<T>; template<class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i,n) for (auto& i: n) template<class T> inline bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1;} return 0;} template<class T> inline bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1;} return 0;} struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_; #ifdef DEBUG template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); } #endif const ll INF = 4e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// int main() { ll N; cin >> N; vl E(2 * N, -1), id(2 * N, -1), used(2 * N), se(N), A(N), B(N); rep(i, N) { cin >> A[i] >> B[i]; if (A[i] > 0) A[i]--, id[A[i]] = i, used[A[i]]++; if (B[i] > 0) B[i]--, id[B[i]] = i, used[B[i]]++; if (A[i] >= 0 and B[i] >= 0) se[i] = 1; // 始点終点が決まっている if((A[i] >= 0 and B[i] >= 0) and A[i] > B[i]){ cout << "No" << endl; return 0; } } rep(i, 2 * N){ if(used[i] > 1){ cout << "No" << endl; return 0; } } rep(i, N) { if (A[i] == -1 or B[i] == -1) continue; ll d = B[i] - A[i]; repe(j, A[i], B[i] + 1) { if(!(E[j] == -1 or E[j] == d)){ cout << "No" << endl; return 0; } E[j] = d; } } vl pass(2 * N + 1); pass[0] = 1; rep(i, 2 * N - 1){ if (!pass[i]) continue; // 選ぶ人数 repe(n, 1, N + 1){ if (i + 2 * n > 2 * N) break; bool isok = true; rep(j, n){ if(id[i + j] == -1 and id[i + j + n] == -1){ continue; }else if(id[i + j] == -1){ if (se[id[i + j + n]]) isok = false; }else if(id[i + j + n] == -1){ if (se[id[i + j]]) isok = false; }else{ if (id[i + j] != id[i + j + n]) isok = false; } if (!isok) break; } if (isok) pass[i + 2 * n] = 1; } } cout << (pass[2 * N] ? "Yes" : "No") << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define debug(x) cout << #x << ":\t" << x << endl; const int N = 2e6 + 10; const int INF = 0x3f3f3f3f; const ll inf = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7; int n; int a[N], b[N]; int dp[1010][1010], vis[1010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int l, r; scanf("%d%d", &l, &r); if (l != -1 && r != -1 && r <= l) { puts("No"); return 0; } if (l != -1) { if (vis[l]) { puts("No"); return 0; } a[l] = r; vis[l] = 1; } if (r != -1) { if (vis[r]) { puts("No"); return 0; } b[r] = l; vis[r] = 1; } } n <<= 1; for (int len = 2; len <= n; len += 2) { for (int i = 1; i <= n - len + 1; i++) { int j = i + len - 1; for (int k = i; k < j; k++) { if (dp[i][k] && dp[k + 1][j]) { dp[i][j] = 1; break; } } if (dp[i][j])continue; dp[i][j] = 1; for (int k = i; k <= i + len / 2 - 1; k++) { if (b[k]) { dp[i][j] = 0; break; } if (a[k]) { if (a[k] != -1 && a[k] - k != len / 2)dp[i][j] = 0; else { if (b[k + len / 2] && b[k + len / 2] != k)dp[i][j] = 0; } } } for (int k = i + len / 2; k <= j; k++) { if (a[k]) { dp[i][j] = 0; break; } if (b[k]) { if (b[k] != -1 && k - b[k] != len / 2)dp[i][j] = 0; else { if (a[k - len / 2] && a[k - len / 2] != k)dp[i][j] = 0; } } } } } puts(dp[1][n] ? "Yes" : "No"); return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<cmath> using namespace std; int main(){ int n; cin >> n; vector<int> x(n); vector<int> y(n); for(int i=0;i<n;i++){ cin>>x.at(i)>>y.at(i); } bool ans=false; int re_up,re_down,im_up,im_down; for(int i=0;i<n-2;i++){ for(int j=i+1;j<n-1;j++){ for(int k=j+1;k<n;k++){ re_down=x.at(j)-x.at(i); im_down=y.at(j)-y.at(i); re_up=x.at(k)-x.at(i); im_up=y.at(k)-y.at(i); if((re_down*im_up-re_up*im_down) == 0){ ans = true; break; } } if(ans){ break; } } if(ans){ break; } } if(ans){ cout << "Yes" <<endl; }else{ cout << "No" <<endl; } return 0; }
#include "bits/stdc++.h" #define ll long long #define F0R(i, a) for (int i=0; i<(a); i++) #define FOR(i, a, b) for (int i=a; i<(b); i++) using namespace std; bool collinear(int x1, int y1, int x2, int y2, int x3, int y3) { return (y1 - y2) * (x1 - x3) == (y1 - y3) * (x1 - x2); } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n; cin >> n; vector<pair<int, int>> v(n); F0R(i,n) { cin >> v[i].first >> v[i].second; } F0R(i,n) { FOR(j,i+1,n) { FOR(k,j+1,n) { if(collinear(v[i].first, v[i].second, v[j].first, v[j].second, v[k].first, v[k].second) == true) { cout << "Yes\n"; return 0; } } } } cout << "No\n"; return 0; }
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i) #define irreps(i, m, n) for (long long i = ((long long)(n)-1); i > (long long)(m); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v+n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cinline(n) getline(cin,n); #define replace_all(s, b, a) replace(s.begin(),s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) (long long)(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll,ll,ll>>; using vb = vector<bool>; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const ll INF = 1e9+10; const ll MOD = 1e9+7; const ll LINF = 1e18; signed main() { cin.tie( 0 ); ios::sync_with_stdio( false ); ll n; cin>>n; string t; cin>>t; if(n==1){ ll ans=10000000000; if(t=="0") cout<<ans<<endl; else cout<<ans*2<<endl; }else if(n==2){ ll ans=10000000000; if(t=="11") cout<<ans<<endl; else if(t=="00") cout<<0<<endl; else if(t=="10") cout<<ans<<endl; else if(t=="01") cout<<ans-1<<endl; }else{ bool flg=1; char cycle[]={'1','1','0'}; ll base=10000000000, rem=0; base*=3; if(t.substr(0,3)=="110"){ irep(i,3,n) if(cycle[i%3]!=t[i]) flg=0; }else if(t.substr(0,2)=="10"){ rem=1; irep(i,2,n) if(cycle[(i-2)%3]!=t[i]) flg=0; }else if(t.substr(0,1)=="0"){ rem=2; irep(i,1,n) if(cycle[(i-1)%3]!=t[i]) flg=0; }else{ cout<<0<<endl; return 0; } if(!flg) cout<<0<<endl; else{ ll x=(base-n-rem)/3; cout<<x+1<<endl; // rem=rem; // cout<<rem<<endl; } } }
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <map> #include <iterator> #include <cmath> #include <set> #include <deque> #include <string> using namespace std; int n; char s[200010]; long long totalCount; long long getStartPos() { if ((s[0] == '1') && (s[1] == '1') && (s[2] == '0')) { return 0; } else if ((s[0] == '1') && (s[1] == '0') && (s[2] == '1')) { return 1; } else if ((s[0] == '0') && (s[1] == '1') && (s[2] == '1')) { return 2; } return -1; } int main() { scanf("%d\n", &n); scanf("%s\n", s); totalCount = 10000000000L; if (n == 1) { long long zeroCount = 10000000000L; long long oneCount = 20000000000L; if (s[0] == '0') { printf("%lld\n", zeroCount); } else { printf("%lld\n", oneCount); } return 0; } if (n == 2) { if ((s[0] == '0') && (s[1] == '0')) { printf("0\n"); } else if ((s[0] == '0') && (s[1] == '1')) { printf("%lld\n", totalCount - 1); } else { printf("%lld\n", totalCount); } return 0; } for (int i = 3 ; i < n ; i++) { if (s[i] != s[i - 3]) { printf("0\n"); return 0; } } long long startPos = getStartPos(); long long endPos = startPos + n - 1; if (startPos == -1) { printf("0\n"); return 0; } long long startSect = startPos / 3; long long endSect = endPos / 3; long long ans = totalCount - endSect; printf("%lld\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int c=5002; long long n, m, sum, mod=998244353; long long po[c][c]; int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i=0; i<=m; i++) { po[i][0]=1; } for (int i=0; i<=m; i++) { for (int j=1; j<=n; j++) { po[i][j]=po[i][j-1]*i%mod; } } sum=po[m][n]*n%mod; for (int i=1; i<n; i++) { for (int j=1; j<=m; j++) { sum-=(n-i)*po[m][n-i-1]%mod*po[m-j][i-1]%mod; } } sum%=mod; if (sum<0) { sum+=mod; } cout << sum << "\n"; return 0; }
#include <iostream> #include <algorithm> #include <cmath> #include <limits> #include <iomanip> #include <vector> #include <cstring> #include <queue> #include <map> #include <set> #include <numeric> #include <unordered_map> #include <unordered_set> #define rep(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> P; typedef pair<ll,ll> Pll; typedef vector<vector<int>> Graph; typedef vector<string> Grid; const int dx[4] = {0,1,0,-1}; const int dy[4] = {1,0,-1,0}; template<class T> inline bool chMAX(T &a,T& b){if(a < b){a = b; return true;} else return false;} template<class T> inline bool chmin(T &a,T& b){if(a > b){a = b; return true;} else return false;} //#define DEBUG //declaration area const ll int_inf = 1LL << 30; const ll INF = 1LL << 60; const ll MOD = 998244353; //struct area //function area ll pow(ll x, ll n, ll MOD){ ll ret = 1; while(n > 0){ if(n & 1) ret = ret * x % MOD; x = x * x % MOD; n >>= 1; } return ret; } //main area int main(){ ll n, m; cin >> n >> m; cout << ((pow(10,n,m*m))-(pow(10,n,m)))/m << endl; } /* */
#include <cstdio> #include <cstring> #include <cmath> #include <cassert> #include <vector> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <iostream> #include <numeric> /* debug macros */ #ifdef WAFDAYO #define DBG_LINE() {std::cerr<<"\e[2m[L"<<__LINE__<<"]\e[m ";} #define DBG_PRINT(s,t,u) {std::cerr<<(s)<<" \e[2m=\e[m \e[1m"<<(t)<<"\e[m"<<(u);} #define SELECT_7TH(x1,x2,x3,x4,x5,x6,x7,...) x7 #define dbg1(x1) DBG_PRINT(#x1,x1,std::endl) #define dbg2(x1,x2) DBG_PRINT(#x1,x1,", ")dbg1(x2) #define dbg3(x1,x2,x3) DBG_PRINT(#x1,x1,", ")dbg2(x2,x3) #define dbg4(x1,x2,x3,x4) DBG_PRINT(#x1,x1,", ")dbg3(x2,x3,x4) #define dbg5(x1,x2,x3,x4,x5) DBG_PRINT(#x1,x1,", ")dbg4(x2,x3,x4,x5) #define dbg6(x1,x2,x3,x4,x5,x6) DBG_PRINT(#x1,x1,", ")dbg5(x2,x3,x4,x5,x6) #define dbg(...) DBG_LINE()\ SELECT_7TH(__VA_ARGS__,dbg6,dbg5,dbg4,dbg3,dbg2,dbg1)(__VA_ARGS__) #else #define dbg(...) {} #endif /* utility functions */ struct read_item{read_item(){}; template<class T>operator T(){T t;std::cin>>t;return t;}}; char splf(int i,int n){return(i+1<n)?' ':'\n';}; template<bool up> struct _RI{int i;_RI(int a):i(a){} int operator*(){return i;}void operator++(){i+=(up?1:-1);} bool operator!=(const _RI& r){return up?i<r.i:i>r.i;}}; template<bool up> struct _RX{_RI<up> x,y;_RI<up> begin(){return x;}_RI<up> end(){return y;} _RX(int a,int b):x(up?a:(b-1)),y(up?b:(a-1)){}_RX(int a):_RX(0,a){}}; typedef _RX<true> range; typedef _RX<false> revrange; /* types and constants */ typedef int64_t i64; typedef uint64_t u64; // const i64 inf = (i64)1.05e18; // const int inf = (int)1.05e9; using namespace std; bool iseven(int x) { return (x & 1) == 0; } void oesort3(vector<int>& xs, int pos, bool even, vector<int>& ops) { int retry = 0; auto check = [&]() { for(auto i : range(3)) { if(xs[pos + i] != pos + i) { return false; } } return true; }; while(!check()) { int p = (iseven(pos) == even) ? pos : pos + 1; ops.push_back(p); swap(xs[p], xs[p + 1]); even = !even; retry += 1; assert(retry < 100); } } void oesort(vector<int>& xs, int pos, bool even, vector<int>& ops) { const int N = xs.size(); if(N - pos == 3) { return oesort3(xs, pos, even, ops); } assert(N - pos >= 4); int target = pos; while(xs[target] != pos) { target += 1; } if(target == pos) { oesort(xs, pos + 1, even, ops); return; } int retry = 0; while(iseven(target - 1) != even) { if(target - 2 >= pos) { ops.push_back(target - 2); swap(xs[target - 2], xs[target - 1]); even = !even; } else { ops.push_back(target); swap(xs[target], xs[target + 1]); target += 1; even = !even; } retry += 1; assert(retry < 100); } for(auto i : revrange(pos, target)) { assert(iseven(i) == even); ops.push_back(i); swap(xs[i], xs[i + 1]); even = !even; } oesort(xs, pos + 1, even, ops); } void solve() { const int N = read_item(); vector<int> As(N); for(auto i : range(N)) { As[i] = (int)read_item() - 1; } if(N == 2) { if(As[0] == 0) { printf("0\n"); return; } else { printf("1\n1\n"); return; } } static vector<int> ops; ops.resize(0); oesort(As, 0, true, ops); int c = ops.size(); assert(c <= N * N); printf("%d\n", c); for(auto i : range(c)) { printf("%d%c", ops[i] + 1, splf(i, c)); } } int main() { const int T = read_item(); for(auto _ : range(T)) { solve(); } return 0; } /* wafdayo~~~ */
/* * author: cyclexit * start from: 2021-01-25 16:23 */ #include <bits/stdc++.h> using namespace std; typedef long long ll; template<typename T> struct Point { T x, y; }; template<typename T> struct Plane { array<T, 3> unitx{1, 0, 0}; array<T, 3> unity{0, 1, 0}; }; template<typename T> inline Plane<T> rotate90(Plane<T> p) { swap(p.unitx, p.unity); for (int i = 0; i < 3; ++i) { p.unity[i] *= -1; } return p; } template<typename T> inline Plane<T> rotate270(Plane<T> p) { swap(p.unitx, p.unity); for (int i = 0; i < 3; ++i) { p.unitx[i] *= -1; } return p; } template<typename T> inline Plane<T> symmetric_x(Plane<T> p, T x) { for (int i = 0; i < 3; ++i) { p.unitx[i] *= -1; } p.unitx[2] += 2 * x; return p; } template<typename T> inline Plane<T> symmetric_y(Plane<T> p, T y) { for (int i = 0; i < 3; ++i) { p.unity[i] *= -1; } p.unity[2] += 2 * y; return p; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<Point<ll>> points(n); for (int i = 0; i < n; ++i) { cin >> points[i].x >> points[i].y; } int m; cin >> m; vector<Plane<ll>> planes(m + 1); ll op, v; for (int i = 1; i <= m; ++i) { cin >> op; switch (op) { case 1: planes[i] = rotate90(planes[i - 1]); break; case 2: planes[i] = rotate270(planes[i - 1]); break; case 3: cin >> v; planes[i] = symmetric_x(planes[i - 1], v); break; case 4: cin >> v; planes[i] = symmetric_y(planes[i - 1], v); break; } } int q; cin >> q; while (q--) { int a, b; cin >> a >> b; --b; auto unitx = planes[a].unitx; auto unity = planes[a].unity; ll x = unitx[0] * points[b].x + unitx[1] * points[b].y + unitx[2]; ll y = unity[0] * points[b].x + unity[1] * points[b].y + unity[2]; cout << x << " " << y << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; using VP = vector<pair<long long, long long>>; ll A,B; int main(){ cin >> A >> B; ll E[A+B]; ll sum1 = 0,sum2 = 0; if(A >= B) { for(int i = 1; i <= A; ++i) { E[i] = i; sum1 += i; } for(int i = 1; i < B; ++i) { E[A + i] = -i; sum2 += i; } E[A+B] = -(sum1-sum2); for(int i = 1; i <= A+B; ++i){ cout << E[i] << " "; } cout << endl; }else { for(int i = 1; i <= B; ++i) { E[i] = -i; sum1+=E[i]; } for(int i = 1; i < A; ++i) { E[B + i] = i; sum2 += E[i]; } E[A+B] = -(sum1 - sum2); for(int i = 1; i <= A+B; ++i){ cout << E[i] << " "; } cout << endl; } return 0; }
//xhxh //LYC_music yyds! #include<bits/stdc++.h> #define int long long using namespace std; int read() { int pos=1,num=0; char ch=getchar(); while (!isdigit(ch)) { if (ch=='-') pos=-1; ch=getchar(); } while (isdigit(ch)) { num=num*10+(int)(ch-'0'); ch=getchar(); } return pos*num; } void write(int x) { if (x<0) { putchar('-'); write(-x); return; } if (x>=10) write(x/10); putchar(x%10+'0'); } void writesp(int x) { write(x); putchar(' '); } void writeln(int x) { write(x); putchar('\n'); } int A,B,sum; signed main() { A=read(); B=read(); sum=A-B; if (sum==0) { for (int i=1;i<=A;i++) { writesp(i); writesp(-i); } return 0; } if (sum>0) { for (int i=1;i<=A;i++) { writesp(i); } for (int i=1;i<B;i++) { writesp(-i); } write(-(A*(A+1)/2-(B-1)*B/2)); return 0; } for (int i=1;i<=B;i++) { writesp(-i); } for (int i=1;i<A;i++) { writesp(i); } write((B+1)*B/2-(A-1)*A/2); }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef set<int> si; typedef map<int,int> mii; typedef map<ll,ll> mll; typedef vector<pii> vii; typedef vector<pll> vll; #define fi first #define se second #define pi 3.141592653589793 #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() #define pqmax priority_queue<int> #define pqmin priority_queue<int,vi,greater<int>> #define fio ios_base::sync_with_stdio(0), cin.tie(NULL) #define tc int tt;cin>>tt;for(int ti=1;ti<=tt;ti++) #define case_g "Case #"<<ti<<": " #define RED "\033[31m" #define GREEN "\033[32m" #define RESET "\033[0m" #define sleep for (int i = 1, a;i < 100000000;i++, a = a * a) typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set; typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map; int main() { fio; ll n, w; cin >> n >> w; cout << n / w << '\n'; }
#include<bits/stdc++.h> #define ll long long int #define nl '\n' #define pi acos(-1) #define GCD(a, b) __gcd(a, b) #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define inout freopen("input.txt","r",stdin);freopen("output.txt","w",stdout) #define mod 11092019 using namespace std; int main(void){ //boost; ll t,a,n,x,b,c,d,mx=1e9+5,ans=0,cnt; // cin>>t; read: cin>>a>>b; cout<<a/b; }
#include <stdio.h> #include <iostream> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <set> #include <bitset> using namespace std; #define rep(i,s,N) for(int i=s;i<N;i++) typedef long long int lli; //#define MOD 100000000 //#define MOD 998244353 int main(void) { int N; cin >> N; set<int> A; rep(i, 0, N) { int a; cin >> a; A.insert(a); } while (A.size() > 1) { int X = *(--A.end()); A.erase(--A.end()); int x = *A.begin(); A.insert(X - x); } cout << *A.begin() << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define repp(i,k, n) for(int i = (int)(k); i < (int)(n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int main(){ int n; cin >> n; if(n%2 == 0) cout << "White" << endl; else cout << "Black" << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long int euclidExt(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int x1, y1; int d = euclidExt(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } void shiftAnswer(int &x, int &y, int k, int a, int b, int g) { x += k * (b / g); y -= k * (a / g); } bool solve(int a, int b, int c, int &n, int &m, int minize) { int g = euclidExt(abs(a), abs(b), n, m); if (c % g) return false; n *= (c / g); m *= (c / g); if (a < 0) n *= -1; if (b < 0) m *= -1; if (minize == 0) { int k; int num = -n * g; if (num >= 0) { k = (num + abs(b) - 1) / abs(b); } else { k = num / abs(b); } shiftAnswer(n, m, k, abs(a), abs(b), g); } else { int k; int num = -m * g; if (num >= 0) { k = (num + abs(a) - 1) / abs(a); } else { k = num / abs(a); } shiftAnswer(n, m, -k, a, b, g); } return n >= 0 && m >= 0; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { int x, y, p, q; cin >> x >> y >> p >> q; int ans = LLONG_MAX; for (int k = 0; k < q; k++) { int n1 = 0, m1 = 0; if (solve(2 * x + 2 * y, -(p + q), p + k - x, n1, m1, 1)) ans = min(ans, (p + q) * m1 + p + k); } for (int k = 0; k < y; k++) { int n2 = 0, m2 = 0; if (solve(2 * x + 2 * y, -(p + q), p - k - x, n2, m2, 1)) ans = min(ans, (p + q) * m2 + p); } cout << (ans == LLONG_MAX ? "infinity" : to_string(ans)) << '\n'; } }
#include <bits/stdc++.h> using namespace std; template<typename T> void read(T&x){ x=0; int f=1; char ch=getchar(); while(!isdigit(ch)){ if(ch=='-')f*=-1; ch=getchar(); } while(isdigit(ch)){ x=x*10+(ch-'0'); ch=getchar(); }x*=f; } template<typename T> void write(T x){ if(x<0)putchar('-'),x=-x; if(x>9)write(x/10); putchar(x%10+'0'); } //============================================= typedef long long ll; #define int ll int t,x,y,p,q; vector<int>T1,T2; int mod[3]; int ans[3]; void prelude(){ for(int i=x;i<x+y;++i)T1.push_back(i); for(int i=p;i<p+q;++i)T2.push_back(i); } int mul(int a,int b,int mod){ int res=0; while(b){ if(b&1)res=(res+a)%mod; a=(a+a)%mod; b>>=1; } return res; } int exgcd(int a,int b,int&x,int&y){ if(b==0){ x=1,y=0; return a; } int r=exgcd(b,a%b,x,y); int t=x; x=y; y=t-a/b*y; return r; } int excrt(int ans[],int mod[],int n){ int M=mod[0]; int res=ans[0]; for(int i=1;i<n;++i){ int a=M,b=mod[i],c=((ans[i]-res)%b+b)%b; int x,y; int r=exgcd(a,b,x,y); if(c%r)return -1; int bb=b/r; x=mul(x,c/r,bb); res+=x*M; M*=bb; res=(res%M+M)%M; } return (res%M+M)%M; } int solve(){ ll res=INT64_MAX; mod[0]=2*x+2*y; mod[1]=p+q; T1.clear(); T2.clear(); prelude(); for(auto t1:T1){ for(auto t2:T2){ ans[0]=t1;ans[1]=t2; int tmp=excrt(ans,mod,2); if(tmp==-1)continue; res=min(res,tmp); } } return res==INT64_MAX?-1:res; } signed main(){ //freopen("in.txt","r",stdin); read(t); while(t--){ read(x),read(y),read(p),read(q); int res=solve(); if(res==-1)puts("infinity"); else write(res),putchar('\n'); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9+7; const ll MAXN = 5e6; ll n, m, k; ll fac[MAXN]; void init_fac() { fac[0] = 1; for (ll i = 1; i < MAXN; i++) fac[i] = fac[i-1] * i % MOD; } ll modpow(ll n, ll x) { ll res = 1; while (x) { if (x & 1) res = (res * n) % MOD; x >>= 1, n = (n * n) % MOD; } return res; } ll modinv(ll n) { return modpow(n, MOD-2); } ll comb(ll n, ll r) { return fac[n] * modinv(fac[r] * fac[n - r] % MOD) % MOD; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); init_fac(); cin >> n >> m >> k; if (n > m+k) cout << 0 << '\n'; else cout << (MOD + comb(n+m, n) - comb(n+m, m+k+1)) % MOD << '\n'; return 0; }
// #pragma GCC optimize ("O3") // #pragma GCC target ("sse4") #include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; #define int long long int #define vi vector<int> #define ff first #define ss second #define pb push_back #define MOD 1000000007 //const int nax = 1e6+5; #define inf 4e18 #define Graph vector<vi> #define Graphw vector<vector<pair<int,int>>> #define sz(x) (int)x.size() #define pr pair<int,int> #define display(v) for(int i =0;i<(int)v.size();++i)cout << v[i] << " " #define all(v) v.begin(),v.end() #define read(a) for(int i = 0;i<(int)a.size();++i)cin>>a[i] #define show(x) cout << #x << " is " << x << "\n"; // typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_si; // typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ind_msi; #define show_arr(a) cout << #a << "\n"; display(a) ; cout << "\n"; inline int add(int a, int b){ int c = (a%MOD) + (b%MOD); if(c >= MOD) c -= MOD; return c; } inline int mul(int a, int b){ return ((a%MOD) * 1ll * (b%MOD)) % MOD; } inline int sub(int a, int b){ int c = (a%MOD) - (b%MOD); if(c < 0) c += MOD; return c; } inline int sum_till(int n) { return mul(mul(n, n + 1), (MOD + 1) / 2); } const int nax = 2000*1000 + 10; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //#define ook order_of_key //#define fbo find_by_order // order_of_key(x) -> number of elements less than x. // * find_by_order(x) -> gives the element at index x (0 - indexing) int fac[nax] , faci[nax]; int mod_inv(int a, int n){ int res = 1LL; while(n > 0){ if(n&1){ res = mul(res , a); --n; } a = mul(a , a); n/=2; } return res; } void pre(){ fac[0] = fac[1] = 1LL; faci[0] = faci[1] = 1LL; for(int i = 2;i<nax;++i){ fac[i] = mul(i , fac[i - 1]); faci[i] = mod_inv(fac[i] , MOD - 2); } } int ncrm(int n, int r){ if(n==r || r==0){ return 1LL; } if(n<r){ return 0LL; } return mul(fac[n],mul(faci[r],faci[n-r])); } void test_case(); int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); pre(); int t = 1; //cin>>t; while(t--){ test_case(); } return 0; } void test_case(){ int n, m, k; cin>>n>>m>>k; if(n > m + k){ cout << 0 << '\n'; } else{ int ans = sub(ncrm(n + m, n), ncrm(n + m, m + k + 1)); cout << ans%MOD; } } // Химаншу // check overflow // special case n == 1 // 🍪🍪🍪
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; const ll md = 998244353; const ll OO = 1e16; #define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL) int ad(ll a, ll b) { return (a+b+md) % md; } int ml(ll a, ll b) { return ((a%md) * (b%md)) % md; } ll n, m; const int N = 2e5 + 4; pair<ll,ll> a[N+1]; ll dp[N+1]; void set_dp() { for(int i=0; i<N; ++i) dp[i] = -1; } int upperbound(int lo, int hi, pair<ll,ll> K) { while (lo < hi) { int mid = (lo + hi + 1) >> 1; if (a[mid].first < K.first) { lo = mid + 1; } else if (a[mid].first > K.first) { hi = mid - 1; } else { if (a[mid].second <= K.second) { lo = mid; } else { hi = mid-1; } } } if (lo > hi) return -1; if (a[lo].first != K.first || a[lo].second > K.second) return -1; return lo; } ll reach(int idx) { if (a[idx].first == n && a[idx].second == 0) return 1; ll &ans = dp[idx]; if (ans != -1) return ans; ans = 0; int i = upperbound(0, idx-1, {a[idx].first-1, a[idx].second-1}); if (i >= 0 && i < m) ans += reach(i); i = upperbound(idx+1, m-1, {a[idx].first+1, a[idx].second-1}); if (i >= 0 && i < m) ans += reach(i); return ans = ans > 0; } int main() { FAST_IO; set_dp(); cin >> n >> m; for (int i=0; i<m; ++i) { cin >> a[i].second >> a[i].first; } a[m] = {n,0}; ++m; sort(a, a + m); ll ans = 0; for(int i=0; i<m-1; ++i) { if (a[i].first != a[i+1].first && reach(i) > 0) ans++; } if (reach(m-1) > 0) ans++; cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define pob pop_back #define S second #define F first #define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define vll vector<long long int> #define pll pair<long long int,long long int> #define mod 1000000007 #define mod2 998244353 #define ll long long int #define ld long double #define pi 3.141592653589793238 #define Endl endl #define endl "\n" const int N = 2e5 + 15; const ll inf = 1e18; void solve() { ll n , m; cin >> n >> m; map<ll,set<ll>> mp; for(ll i=1;i<=m;i++) { ll r , c; cin >> r >> c; mp[r].insert(c); } set<ll> ans = {n}; for(auto x:mp) { set<ll> good; set<ll> bad; for(auto y:x.S) { if(ans.count(y-1) or ans.count(y+1)) good.insert(y); else bad.insert(y); } for(auto y:good) ans.insert(y); for(auto y:bad) ans.erase(y); } cout << ans.size(); } void debug(ll tt) {} signed main() { FAST; int t = 1; // cin >> t; while(t--) { solve(); } }
#include <algorithm> #include <cstdio> #include <functional> #include <iostream> #include <cfloat> #include <climits> #include <cstdlib> #include <cstring> #include <cmath> #include <map> #include <unordered_map> #include <unordered_set> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <complex> #include <vector> #include <limits> #include <iomanip> #include <cassert> #include <numeric> #include <chrono> #include <random> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define debug(x) cerr << #x << " = " << (x) << endl; #define debug_pii(x) cerr << "(" << x.first << "," << x.second << ")"; #define rep(i, n) for(int i = 0;i < n;i++) #define pb push_back #define F first #define S second // template<typename _Ty1, typename _Ty2> // ostream& operator<<(ostream& _os, const pair<_Ty1, _Ty2>& _p) { // _os << '(' <<_p.first << ',' << _p.second << ')'; // return _os; // } // // template<typename _Ty1, typename _Ty2> // istream& operator>>(istream& _is, pair<_Ty1, _Ty2>& _p) { // _is >> _p.first >> _p.second; // return _is; // } void solve() { int n;cin>>n; vector<ll> a(2*n);ll sum=0, temp = 0; rep(i,2*n){ cin>>a[i];sum+=a[i]; } int i = n-1, j = n; multiset<ll> ms;ms.insert(a[i]);ms.insert(a[j]); while(i >= 0) { temp += *ms.begin(); ms.erase(ms.begin()); i--;j++; if(i >= 0) { ms.insert(a[i]);ms.insert(a[j]); } } cout<<sum-temp<<endl; } // 6 -> 1 2 3 6 // < // greedy will fail // 8 4 7 4 -> 8, 4 -> 12 // 8 4 7 4 -> 7, 8 -> 15 // // 1 2 3 4 5 6 7 8 // + * * + * * + + // // 1 2 3 4 5 6 7 8 // * * * * + + + + // // 1 2 3 4 5 6 7 8 // + + * * * * + + // // 1 4 5 8 7 6 3 2 // // 7 5 3 1=16 (36) int main() { // freopen("input.in","r",stdin); // freopen("output.out","w",stdout); // cout << fixed << setprecision(15); ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG #endif using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = s; i < (int)(n); i++) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define vec vector #define rip return 0 typedef long long ll; typedef pair<ll,ll> P; ll max(ll x,ll y){ if(x>y)return x; else return y; } ll min(ll x,ll y){ if(x<y)return x; else return y; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout<<fixed<<setprecision(12); /*-------------------------------------------*/ int n; cin>>n; vec<vec<ll>> c(n,vec<ll>(n)); rep(i,n){ rep(j,n){ cin>>c[i][j]; } } vec<ll> down(n-1),across(n-1); rep(i,n-1){ down[i]=c[i+1][0]-c[0][0]; across[i]=c[0][i+1]-c[0][0]; rep(j,n-1){ if(c[i+1][j]-c[0][j]!=down[i] || c[j][i+1]-c[j][0]!=across[i]){ cout<<"No"<<endl;rip; } } } vec<ll> a(n),b(n); ll ma=1e10,md=1e10; rep(i,n-1){ md=min(md,down[i]); ma=min(ma,across[i]); } if(md<0)a[0]=md*-1; if(ma<0&&(c[0][0]-a[0])<(ma*-1)){ cout<<"No"<<endl;rip; } b[0]=c[0][0]-a[0]; rep(i,n-1)a[i+1]=a[0]+down[i]; rep(i,n-1)b[i+1]=b[0]+across[i]; if(a[n-1]+b[n-1]!=c[n-1][n-1]){ cout<<"No"<<endl;rip; } cout<<"Yes"<<endl; rep(i,n)cout<<a[i]<<" "; cout<<endl; rep(i,n)cout<<b[i]<<" "; cout<<endl; }
#include<cstdio> long long n,a,b; int main(){ scanf("%lld%lld%lld",&n,&a,&b); printf("%lld\n",n-a+b); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ll a,b,c,d; cin>>a>>b; cin>>c>>d; cout<<a*d-c*b<<endl; }
#pragma GCC target ("avx2") // #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <iostream> #include <array> #include <algorithm> #include <vector> #include <bitset> #include <set> #include <unordered_set> #include <cmath> #include <complex> #include <deque> #include <iterator> #include <numeric> #include <map> #include <unordered_map> #include <queue> #include <stack> #include <string> #include <tuple> #include <utility> #include <limits> #include <iomanip> #include <functional> #include <cassert> // #include <atcoder/all> using namespace std; using ll=long long; template<class T> using V = vector<T>; template<class T, class U> using P = pair<T, U>; using vll = V<ll>; using vii = V<int>; using vvll = V<vll>; using vvii = V< V<int> >; using PII = P<int, int>; using PLL = P<ll, ll>; #define RevREP(i,n,a) for(ll i=n;i>a;i--) // (a,n] #define REP(i,a,n) for(ll i=a;i<n;i++) // [a,n) #define rep(i, n) REP(i,0,n) #define ALL(v) v.begin(),v.end() #define eb emplace_back #define pb push_back #define sz(v) int(v.size()) 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; } template< class A, class B > ostream& operator <<(ostream& out, const P<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template< class A > ostream& operator <<(ostream& out, const V<A> &v) { out << '['; for (int i=0;i<int(v.size());i++) { if (i) out << ", "; out << v[i]; } return out << ']'; } const long long MOD = 1000000007; const long long HIGHINF = (long long)1e18; const int INF = (int)1e9; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vii c(n); rep(i, n) cin >> c[i]; vvii edges(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--, b--; edges[a].eb(b), edges[b].eb(a); } vii exist(100001, 0), fc(100001, -1); vii ans; auto rec = [&](auto && me, int i, int p) -> void { if (!exist[c[i]]) { ans.eb(i + 1); exist[c[i]] = 1; fc[c[i]] = i; } for (auto e: edges[i]) { if (e == p) continue; me(me, e, i); } if (fc[c[i]] == i) { fc[c[i]] = -1; exist[c[i]] = 0; } }; rec(rec, 0, -1); sort(ALL(ans)); for (auto a: ans) cout << a << '\n'; return 0; }
//#pragma GCC optimize ("O2") //#pragma GCC target ("avx2") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int N; vector<pair<int, int>> E[200001]; int A[200001], B[200001]; ll zentai = 0; ll kari = 0; ll q[200001]; ll kotae[200001]; void dfs(int a, int mae) { kotae[a] = kari; for (auto p : E[a]) { int to = p.first; int e = p.second; if (mae == to) continue; if (B[e] == a) { kari += q[e]; dfs(to, a); kari -= q[e]; } else { zentai += q[e]; kari -= q[e]; dfs(to, a); kari += q[e]; } } } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; rep1(i, N - 1) { int a, b; cin >> a >> b; A[i] = a; B[i] = b; E[a].pb(mp(b, i)); E[b].pb(mp(a, i)); } int Q; cin >> Q; rep(i, Q) { int t, e, x; cin >> t >> e >> x; if (t == 2) { zentai += x; q[e] += -x; } else q[e] += x; } dfs(1, 0); rep1(i, N) { co(zentai + kotae[i]); } Would you please return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <utility> #include <set> #include <map> #include <cmath> #include <queue> #include <cstdio> #include <limits> #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) using namespace std; template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; } template<class T> inline int sz(T &a) { return a.size(); } using ll = long long; using ld = long double; using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; const int inf = numeric_limits<int>::max(); const ll infll = numeric_limits<ll>::max(); // Modint // modint<MOD> で宣言 template<long long MOD> struct modint{ long long x; long long mod = MOD; modint(long long x=0):x(x%MOD){} modint& operator+=(const modint a){ if((x+=a.x)>=MOD) x-=MOD; return *this; } modint& operator-=(const modint a){ if((x += MOD-a.x)>=MOD) x-=MOD; return *this; } modint& operator*=(const modint a){ (x*=a.x)%=MOD; return *this; } modint operator+(const modint a) const{ modint res(*this); return res+=a; } modint operator-(const modint a) const{ modint res(*this); return res-=a; } modint operator*(const modint a) const{ modint res(*this); return res*=a; } modint pow(long long t) const{ if(!t) return 1; modint a = pow(t>>1); a*=a; if(t&1) a*=*this; return a; } //for prime mod modint inv() const{ return pow(MOD-2); } modint& operator/=(const modint a){ return (*this) *= a.inv(); } modint operator/(const modint a) const{ modint res(*this); return res/=a; } }; using mint = modint<1000000007>; const int NMAX=1000010; // we can calculate nCk until n is equal to NMAX mint fact[NMAX],infac[NMAX]; void Make_Fact(){ fact[0]=fact[1]=1; for(int i=2;i<=NMAX-1;++i){ fact[i]=fact[i-1]*(mint)i; } } void Make_InvFact(){ infac[0]=infac[1]=1; for(int i=2;i<=NMAX-1;++i){ infac[i]=infac[i-1]/(mint)i; } } mint Comb(int n,int k){ if(n<0||k<0||n-k<0) return 0; return fact[n]*infac[k]*infac[n-k]; } int main() { int n; cin >> n; vi a(n); rep(i,n) { cin >> a[i]; } vector<mint> dpp(n+1),cntp(n+1); vector<mint> dpm(n+1),cntm(n+1); dpp[0].x = a[0]; cntp[0].x = 1; rep(i,n-1) { cntp[i+1] = cntp[i] + cntm[i]; cntm[i+1] = cntp[i]; dpp[i+1] = dpp[i] + dpm[i] + cntp[i+1] * (mint)a[i+1]; dpm[i+1] = dpp[i] - cntm[i+1] * mint(a[i+1]); //cout << dpp[i].x << ":" << dpm[i].x << "\n"; } cout << (dpp[n-1] + dpm[n-1]).x << "\n"; return 0; }
#include <iostream> #include <cmath> #include <string> #include <algorithm> #include <vector> using namespace std; int main() { int prime=1000000007; int N; cin>>N; vector <int> arr(N,0); for(int i=0;i<N;i++){ int k; cin>>k; arr[i]=k; } sort(arr.begin(),arr.end()); int dif[N]; dif[0]=arr[0]; for(int i=0;i<N-1;i++){ dif[i+1]=arr[i+1]-arr[i]; } long long int out=1; for(int i=0;i<N;i++){ out*=(dif[i]+1); out%=prime; } cout<<out<<endl; }
/*** author: yuji9511 ***/ #include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; using namespace std; using ll = long long; using lpair = pair<ll, ll>; using vll = vector<ll>; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i=(m);i<(n);i++) #define rrep(i,m,n) for(ll i=(m);i>=(n);i--) #define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];}; void print() {} template <class H,class... T> void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);} void solve(){ ll H,W; cin >> H >> W; ll A[110][110]; rep(i,0,H) rep(j,0,W) cin >> A[i][j]; ll min_val = INF; rep(i,0,H){ rep(j,0,W){ min_val = min(min_val, A[i][j]); } } ll ans = 0; rep(i,0,H)rep(j,0,W) ans += A[i][j] - min_val; print(ans); } int main(){ cin.tie(0); ios::sync_with_stdio(false); solve(); }
#include <bits/stdc++.h> #include <cstring> using namespace std; typedef long long ll; #define F0R(i, x, y) for (int i = x; i < y; i++) #define FOR(i, n) F0R(i, 0, n) #define F0RD(i, x, y) for (int i = y - 1; i >= x; i--) #define FORD(i, n) F0RD(i, 0, n) #define F first #define S second int mp(int x, int c) { return 1000 * c + x; } vector<int> adj[2005]; bool vis[2005]; void dfs(int v) { vis[v] = true; for (int to : adj[v]) { if (!vis[to]) dfs(to); } } int main() { #ifdef mikey freopen("a.in", "r", stdin); #endif ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; auto ad = [&] (int a, int b, int c, int d) { adj[mp(a, b)].push_back(mp(c, d)); adj[mp(c, d)].push_back(mp(a, b)); }; auto add = [&] (int a, int b, int c, int d) { adj[mp(a, b)].push_back(mp(c, d)); }; FOR(i, m) add(i, 1, 0, 0); FOR(i, m) add(i, 1, n - 1, 0); FOR(i, n) add(i, 0, 0, 1); FOR(i, n) add(i, 0, m - 1, 1); FOR(i, n) { string s; cin >> s; FOR(j, m) { if (s[j] == '#') { ad(i, 0, j , 1); } } } int rcc = 0; FOR(i, n) { if (!vis[mp(i, 0)]) { dfs(mp(i, 0)); ++rcc; } } memset(vis, false, sizeof(vis)); int ccc = 0; FOR(i, m) { if (!vis[mp(i, 1)]) { dfs(mp(i, 1)); ++ccc; } } cout << min(rcc - 1, ccc - 1) << '\n'; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<n;i++) #define repl(i,l,r) for(ll i=(l);i<(r);i++) #define per(i,n) for(ll i=(n)-1;i>=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x,vector<x>,greater<x>> #define all(x) (x).begin(),(x).end() #define CST(x) cout<<fixed<<setprecision(x) #define rev(x) reverse(x); using ll=long long; using vl=vector<ll>; using vvl=vector<vector<ll>>; using pl=pair<ll,ll>; using vpl=vector<pl>; using vvpl=vector<vpl>; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={-1,0,1,0,1,1,-1,-1}; const ll dx[8]={0,-1,0,1,1,-1,1,-1}; template <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } const int mod = MOD9; const int max_n = 200005; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } bool operator==(const mint &p) const { return x == p.x; } bool operator!=(const mint &p) const { return x != p.x; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} struct combination { vector<mint> fact, ifact; combination(int n):fact(n+1),ifact(n+1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n]*ifact[k]*ifact[n-k]; } }comb(max_n); int main(){ ll n,m;cin >> n >>m; mint dp[64][5010];//iけた目のbitまでをたてて、合計がjになる組み方 memset(dp,0,sizeof(dp));dp[0][0]=1; mint two=2; for(int i=0;i<32;i++){ ll base=1LL<<i; rep(j,5010){ ll cnt=0; for(ll k=0;k<5010;k+=base*2){ if(j+k>=5010)break; dp[i+1][j+k]+=dp[i][j]*comb(n,cnt); cnt+=2; } } } cout << dp[32][m] <<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; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define gc getchar_unlocked #define fo(i,n) for(i=0;i<n;i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define ll long long #define si(x) scanf("%d",&x) #define sl(x) scanf("%lld",&x) #define ss(s) scanf("%s",s) #define pi(x) printf("%d\n",x) #define pl(x) printf("%lld\n",x) #define ps(s) printf("%s\n",s) #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define pb push_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++) #define set(x, i) (x | (1 << i)) #define check(x, i) (x & (1 << i)) #define printcase printf("Case %d: ",test++) #define PI 3.1415926535897932384626 typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int lim) { uniform_int_distribution<int> uid(0,lim-1); return uid(rang); } int mpow(int base, int exp); void ipgraph(int n, int m); void dfs(int u, int par); const int mod = 1'000'000'007; const int N = 3e5, M = N; //======================= int test = 1; vi g[N]; int a[N]; void solve() { int i, j, n, m; //printcase; cin >> n; vi nums(n); fo(i,n) cin >> nums[i]; sortall(nums); fo(i,n) { if(nums[i] != i+1) { cout << "No" << endl; return; } } cout << "Yes" << endl; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); int t = 1; // cin >> t; while(t--) { solve(); } return 0; } int mpow(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((ll)result * base) % mod; base = ((ll)base * base) % mod; exp >>= 1; } return result; } void ipgraph(int n, int m){ int i, u, v; while(m--){ cin>>u>>v; u--, v--; g[u].pb(v); g[v].pb(u); } } void dfs(int u, int par){ for(int v:g[u]){ if (v == par) continue; dfs(v, u); } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0;i < n;i++) using ll = long long; int main(){ char s,t; cin >>s >>t; if(s=='Y') t-=32; cout << t << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define PI 3.141592653589793238462 /*########################################### DEBUG ###############################################################################################*/ #ifndef ONLINE_JUDGE #define debug(x) \ cerr << #x << " "; \ _print(x); \ cerr << endl; #else #define debug(x) #endif void _print(long long t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } /*###############################################################################################################################################*/ int main() { #ifndef ONLINE_JUDGE freopen("error.txt", "w", stderr); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); char S, T; cin >> S >> T; string up = "ABC"; string down = "abc"; if (S == 'Y') { T ^= 32; } cout << T << "\n"; }
#include <bits/stdc++.h> using namespace std; using Int = long long; const char newl = '\n'; 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;} template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);} template<typename T=Int> vector<T> read(size_t n){ vector<T> ts(n); for(size_t i=0;i<n;i++) cin>>ts[i]; return ts; } template<typename T,T MOD = 1000000007> struct Mint{ static constexpr T mod = MOD; T v; Mint():v(0){} Mint(signed v):v(v){} Mint(long long t){v=t%MOD;if(v<0) v+=MOD;} Mint pow(long long k){ Mint res(1),tmp(v); while(k){ if(k&1) res*=tmp; tmp*=tmp; k>>=1; } return res; } static Mint add_identity(){return Mint(0);} static Mint mul_identity(){return Mint(1);} Mint inv(){return pow(MOD-2);} Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;} Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;} Mint& operator/=(Mint a){return (*this)*=a.inv();} Mint operator+(Mint a) const{return Mint(v)+=a;} Mint operator-(Mint a) const{return Mint(v)-=a;} Mint operator*(Mint a) const{return Mint(v)*=a;} Mint operator/(Mint a) const{return Mint(v)/=a;} Mint operator-() const{return v?Mint(MOD-v):Mint(v);} bool operator==(const Mint a)const{return v==a.v;} bool operator!=(const Mint a)const{return v!=a.v;} bool operator <(const Mint a)const{return v <a.v;} static Mint comb(long long n,Int k){ Mint num(1),dom(1); for(Int i=0;i<k;i++){ num*=Mint(n-i); dom*=Mint(i+1); } return num/dom; } }; template<typename T,T MOD> constexpr T Mint<T, MOD>::mod; template<typename T,T MOD> ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;} //INSERT ABOVE HERE using M = Mint<Int>; M inv2=M(2).inv(); M calc(Int x,Int y,Int n){ // cout<<x<<' '<<y<<' '<<n<<':'<<M(x+y)*inv2*M(n)<<endl; return M(x+y)*inv2*M(n); } signed solve(){ Int n,a,b; cin>>n>>a>>b; if(a+b>n){ cout<<0<<newl; return 0; } vector<M> vs; if(b <= n - a - b){ // i in [0, B) // i in [B, N - A - B] // i in (N - A - B, N - A] vs.emplace_back(calc(0+a,b-1+a,b-0)); vs.emplace_back(calc(a+b-1,a+b-1,(n-a-b)-b+1)); vs.emplace_back(calc(a+b-1,a,(n-a)-(n-a-b))); }else{ // i in [0, N - A - B] // i in (N - A - B, B) // i in [B, N - A] vs.emplace_back(calc(0+a,n-b,(n-a-b)-0+1)); vs.emplace_back(calc(n-b+1,n-b+1,b-(n-a-b)-1)); vs.emplace_back(calc(n-b,a,(n-a)-b+1)); } M ans{1}; ans*=M(n-a+1)*M(n-a+1); ans*=M(n-b+1)*M(n-b+1); for(Int i=0;i<3;i++) for(Int j=0;j<3;j++) ans-=vs[i]*vs[j]; cout<<ans<<newl; return 0; } signed main(){ cin.tie(0); ios::sync_with_stdio(0); Int T; cin>>T; while(T--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; typedef unsigned long long ll; const ll mod = 1e9 + 7; int main() { ll t, n, a, b; scanf("%lld", &t); while(t--) { scanf("%lld%lld%lld", &n, &a, &b); if(a + b > n) { printf("0\n"); continue; } ll tmp = (n - a - b) % mod; ll ans = (tmp + 1) % mod * (tmp + 2) % mod * (2 * (n - a + 1) % mod * (n - b + 1) % mod - (tmp + 1) % mod * (tmp + 2) % mod + mod) % mod; printf("%lld\n", ans); } return 0; }
#include"iostream" #include"cstdio" #include"cmath" #include"cstring" #include"algorithm" #include"stack" #include"queue" using namespace std; #define read(x) scanf("%d",&x) #define readl(x) scanf("%lld",&x) #define ll long long #define ull unsigned long long #define MOD 998244353 #define MAXN 200005 int n,k; ll sum[305],Cs[305]; ll fac[305]; ll a[MAXN]; ll quickpow(ll a,ll b) { ll ans=1,base=a; while(b) { if(b&1) ans=ans*base%MOD; b>>=1; base=base*base%MOD; } return ans%MOD; } ll inv(ll a){return quickpow(a,MOD-2)%MOD;} int main() { read(n),read(k); fac[0]=1ll; for(int i=1;i<=n;i++) readl(a[i]); for(int i=1;i<=k;i++) fac[i]=fac[i-1]*i%MOD; for(int i=1;i<=n;i++) { ll op=1ll; for(int j=0;j<=k;j++) { sum[j]=(sum[j]+op)%MOD; op=op*a[i]%MOD; } } for(int x=1;x<=k;x++) { ll ans=0; if(x&1) { for(int i=0;i<=x/2;i++) { ll c=fac[x]*inv(fac[x-i]*fac[i]%MOD)%MOD; ll now=(sum[i]*sum[x-i]-sum[x]+MOD)%MOD; ans=(ans+now*c%MOD)%MOD; } } else { for(int i=0;i<x/2;i++) { ll c=fac[x]*inv(fac[x-i]*fac[i]%MOD)%MOD; ll now=(sum[i]*sum[x-i]%MOD-sum[x]+MOD)%MOD; ans=(ans+now*c%MOD)%MOD; } //499122177 ll c=fac[x]*inv(fac[x/2]*fac[x/2]%MOD)%MOD; ll now=(sum[x/2]*sum[x/2]%MOD-sum[x]+MOD)%MOD; ans=(ans+now*c%MOD*499122177%MOD)%MOD; } printf("%d\n",ans%MOD); } return 0; }
#include<bits/stdc++.h> using namespace std; const int MN = 2e5 + 5, MK = 305, MOD = 998244353; int a[MN], sum[MK], c[MK][MK]; int main () { int n,k; scanf ("%d %d",&n,&k); for (int i = 1; i <= n; i++) { int a,pw=1; scanf ("%d",&a); for (int i = 0; i <= k; i++) { sum[i] += pw; if (sum[i] >= MOD) sum[i] -= MOD; pw = (long long)pw * a % MOD; } } c[0][0] = 1; for (int x = 1; x <= k; x++) { int ans = 0; for (int i = 0; i <= x; i++) { c[x][i] = c[x-1][i] + (i?c[x-1][i-1]:0); if (c[x][i] >= MOD) c[x][i] -= MOD; ans = (ans + (long long)c[x][i] * ((long long)sum[i] * sum[x-i]%MOD + MOD - sum[x]) % MOD * 499122177LL) % MOD; } printf ("%d\n",ans); } return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <math.h> #include <queue> #include <numeric> using namespace std; using ll = long long; #define For(i,a,b) for(ll i=(a);i<(ll)(b);i++) #define rFor(i,a,b) for(ll i=(a);i>=(ll)(b);i--) #define co(a) cout<<a<<endl #define Sort(a) sort((a).begin(),(a).end()) #define Rev(a) reverse((a).begin(),(a).end()) #define Unique(a) (a).erase(unique((a).begin(), (a).end()),(a).end()) #define cfs(a) cout<<fixed<<setprecision(a) int main() { ll N; cin >> N; vector<ll> A(N); ll temp; ll sum = 0; ll ans = 0; For(i, 0, N) { cin >> temp; A[i] = temp; sum += temp; ans += (N - 1) * (temp * temp); } For(i, 0, N) { sum -= A[i]; ans -= 2 * (A[i] * sum); } co(ans); }
#include<bits/stdc++.h> #define FULL(x,y) memset(x,y,sizeof(x)) #define ll long long #define SZ(x) (int)x.size() #define pb push_back using namespace std; const int N=300005; int n; ll a[N],pre[N],pre2[N]; int main() { cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) pre[i]=pre[i-1]+a[i]; for(int i=1;i<=n;i++) pre2[i]=pre2[i-1]+a[i]*a[i]; ll ans=0; for(int i=2;i<=n;i++) { ans+=(i-1)*a[i]*a[i]; } for(int i=2;i<=n;i++) { ans+=pre2[i-1]; } for(int i=2;i<=n;i++) { ans-=(2*a[i]*pre[i-1]); } cout<<ans; return 0; }
/* problem : algorithm : created : Programmer_juruo */ #include <bits/stdc++.h> using namespace std; #define int long long #define N 100005 #define lowbit(x) x & (-x) #define F1(i, n) for(int i = 1; i <= n; i++) #define F(i, a, n) for(int i = a; i <= n; i++) #define F0(i, n) for(int i = 0; i < n; i++) #define dbg(x) cout << #x << ":" << x << endl; #define Se second #define Fi first #define y1 juruo #define Mp make_pair #define Pb push_back typedef pair<int, int> pii; typedef priority_queue<int> pq; typedef long long ll; int n, m; inline ll Read() { ll y = 0, f = 1;char c = getchar(); while(!isdigit(c)){if(c == '-') f = -1;c = getchar();} while(isdigit(c)) {y = y*10 + (c - '0'), c = getchar();} return y*f; } inline void read(int &x){x = Read();} void work() { cin >> n; if(n % 4 == 0) cout << "Even" << endl; else if(n % 2 == 0) cout << "Same" << endl; else cout << "Odd" << endl; } signed main() { //freopen("1.in", "r", stdin); //freopen("1.out", "w", stdout); int T = 1; cin >> T; while(T--) { work(); } return 0; }
#include <bits/stdc++.h> #include <cmath> #include <regex> #define fi first #define se second #define pb push_back #define mp make_pair #define ll long long #define all(x) (x).begin(),(x).end() #define vll vector<long long int> #define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL) using namespace std; #define endl "\n" ll C(ll n, ll k) { double res = 1; for (int i = 1; i <= k; ++i) res = res * (n - k + i) / i; return (ll)(res + 0.01); } vector<vll> readGraph(ll n,ll m){ vector<vll> g(n); for(ll i=0;i<m;i++){ ll a,b; cin>>a>>b; //sc.read(a,b); //a--;b--; g[a].pb(b); g[b].pb(a); } return g; } vector<vll> readTree(int n){ return readGraph(n,n-1); } //---------------------------------------------------------------------------- void solve() { ll n; cin>>n; if(n%4==0){ cout<<"Even"<<endl; return; } if(n%2==0){ cout<<"Same"<<endl; return; } else{ cout<<"Odd"<<endl; return; } } int main() { FAST; ll t; cin>>t; while(t--){ solve(); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; #define MOD 1000000007 // x^n ll mod_pow(ll x, ll n) { if (n == 0) return 1; ll res = mod_pow(x * x % MOD, n / 2); if (n & 1) res = res * x % MOD; return res; } // x^{-1} ll mod_inv(ll x) { return mod_pow(x, MOD - 2); } int main() { int n, p; cin >> n >> p; ll ans = p - 1; ans = (ans * mod_pow(p - 2, n - 1)) % MOD; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define debug(x) cerr << #x << " = " << x << endl; main(){ ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; while(k > 0){ if(n % 200 == 0) n /= 200; else n = n * 1000 + 200; k--; } cout << n; }
#include<bits/stdc++.h> using namespace std; using ll = long long; #define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf #define MOD 1000000007 ll myceil(ll a,ll b){return (a + (b - 1)) / b;} ll myfloor(ll a,ll b){return a / b;} int ReLU(int x){return x >= 0 ? x : 0;} int gcd(int a, int b){ return (a % b) ? gcd(b, a % b) : b;} int lcm(int a, int b){ return (a / gcd(a, b)) * b;} template<class t,class u> void chmax(t& a,u b){if(a < b) a = b;} template<class t,class u> void chmin(t& a,u b){if(b < a) a = b;} template<class T> void inner_product(const vector<pair<T, T>>& vec, T& product){for(const auto& e:vec) product += e.first * e.second;} int factorial(int n){ if(n == 0) return 1; else return factorial(n - 1) * n; } int is_prime(int x){ if(x <= 1) return 0; for(int i = 2; i * i <= x; i++){ if(x % i == 0) return 0; } return 1; } int ngcd(int n, int a[]) { int i, d; d = a[0]; for (i = 1; i < n && d != 1; i++) d = gcd(a[i], d); return d; } vector<ll> enum_divisors(ll n){ vector<ll> res; for(ll i = 1; i * i <= n; ++i){ if(n % i == 0){ res.push_back(i); if(n / i != i) res.push_back(n / i); } } sort(res.begin(), res.end()); return res; } int my_calc(const int a, const int b, const char& ope){ if(ope == '+') return a + b; else if(ope == '-') return a - b; else return 0; } struct Edge { int to; // 辺の行き先 int weight; // 辺の重み Edge(int t, int w) : to(t), weight(w) { } }; void dfs_grid(int h, int w, int H, int W, const vector<string>& field, vector<vector<bool>>& seen){ const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; seen[h][w] = true; for(int dir = 0; dir < 4; ++dir){ int nh = h + dx[dir]; int nw = w + dy[dir]; if(nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if(field[nh][nw] == 'x') continue; if(seen[nh][nw]) continue; dfs_grid(nh, nw, H, W, field, seen); } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); string x; char ans[203]; cin >> x; int cnt = 0; for(int i = 0; i < x.size(); ++i){ //cnt += i; if(x[i] == '.'){ ans[i] = '\0'; break; } //ans.push_back(x[i]); ans[i] = x[i]; cnt++; } if(cnt == x.size()){ ans[cnt] = '\0'; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1e9+7; int main() { ll n; cin >> n; ll ans=0; vector<ll>data; for(ll i=1;i*i<=n;i++){ if(n%i==0){ ll x=n/i; data.push_back(i); if(x!=i) data.push_back(x); } } sort(data.begin(),data.end()); for(ll i=0;i<data.size();i++){ cout << data[i] << endl; } }
#include<bits/stdc++.h> using namespace std; using ll = long long; using llu = unsigned long long; using ld = long double; #define CEIL(x, y) (ll)ceil((ld)(x) / (ld)(y)) #define dbgPR(ds) cout << ds.first << ' ' << ds.second; #define dbgLST(ds) for(auto it : ds) cout << it << ' '; #define dbgMP(ds) for(auto it : ds) cout << it.first << ' ' << it.second << '\n'; const double PI = acos(-1.0), g = 9.80665; const bool unseen = 0; const ll pinf = 1e18 + 505, ninf = -1e18 - 505; const int MOD = 1000000007; int ans = 0; void rcr(vector<vector<bool>> v, int h, int w, int a, int b) { if(a == 0 && b == 0) { bool is = 1; for(int i = 0; is && i < h; i++) { for(int j = 0; j < w; j++) { if(!v[i][j]) { is = 0; break; } } } if(is) ans++; return; } for(int i = 0; i < h; i++) { for(int j = 0; j < w; j++) { if(!v[i][j]) { if(b != 0) { v[i][j] = 1; rcr(v, h, w, a, b - 1); v[i][j] = 0; } if(a != 0) { if(i < (h - 1)) { v[i][j] = 1; v[i + 1][j] = 1; rcr(v, h, w, a - 1, b); v[i][j] = 0; v[i + 1][j] = 0; } if(j < (w - 1)) { v[i][j] = 1; v[i][j + 1] = 1; rcr(v, h, w, a - 1, b); v[i][j] = 0; v[i][j + 1] = 0; } } return; } } } } void solve(void) { int h, w, a, b; cin >> h >> w >> a >> b; vector<vector<bool>> v(h, vector<bool> (w, 0)); rcr(v, h, w, a, b); cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int testcase = 1; // cin >> testcase; while(testcase--) { solve(); } return 0; }
// I've had my share of sand kicked in my face but I've come through #pragma GCC optimize("Ofast") #define _CRT_SECURE_NO_WARNINGS #include "bits/stdc++.h" using namespace std; #define int long long #define pb push_back #define all(v) v.begin(),v.end() #define allr(v) v.rbegin(),v.rend() #define mod (int)(1e9+7) void solve() { int n,m,x,y; cin>>n>>m>>x>>y; x--;y--; vector<vector<pair<int,pair<int,int>>>> v(n+1); for(int i=0;i<m;i++){ int a,b,t,k; cin>>a>>b>>t>>k; a--;b--; v[a].pb({b,{t,k}}); v[b].pb({a,{t,k}}); } set<pair<int,int>> s; vector<int> d(n+1,1e18); s.insert({0,x}); d[x]=0; while(!s.empty()){ auto z=*s.begin(); s.erase(s.begin()); for(auto i:v[z.second]){ int time=(((z.first+i.second.second-1)/i.second.second)*i.second.second)+i.second.first; if(time<d[i.first]){ s.erase({d[i.first],i.first}); d[i.first]=time; s.insert({d[i.first],i.first}); } } } if(d[y]==(1e18))cout<<"-1\n"; else cout<<d[y]<<'\n'; } int32_t main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int t=1; // cin>>t; while(t--){ solve(); } return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; #define rep(i,n) for(int i=0; i<n; i++) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define fi first #define se second long long mo = 1000000007; typedef long long ll; typedef long double ld; typedef pair<int,int> Pii; typedef pair<ll,ll> Pll; typedef pair<ll,Pll> PlP; template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; } template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; } template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";} string zero_padding(int val, int nf){ ostringstream sout;sout << setfill('0') << setw(nf) << val; return sout.str();}; ld PI=3.14159265358979323846; //using namespace atcoder; int main(){ ll N; string S; cin >> S; N = S.size(); if(N==1){ if(S == "8"){ cout << "Yes" << endl; }else{ cout << "No" << endl; } return 0; }else if(N==2){ if(S == "16" || S == "24" || S == "32" || S == "48" || S == "56" || S == "64" || S == "72" || S == "88" || S == "96"){ cout << "Yes" << endl; return 0; } swap(S[0], S[1]); if(S == "16" || S == "24" || S == "32" || S == "48" || S == "56" || S == "64" || S == "72" || S == "88" || S == "96"){ cout << "Yes" << endl; return 0; } cout << "No" << endl; return 0; }else{ vector<ll> v(10); rep(i,N){ ll c = S[i] - '0'; v[c]++; } for(ll i = 112; i<1000; i += 8){ bool flag = 1; string s = to_string(i); vector<ll> u(10); rep(j,3){ u[s[j] - '0']++; } rep(j,10){ if(v[j] < u[j] || u[0] > 0){ flag = 0; break; } } if(flag){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=3e5+10; struct BIT{ int n,tr[N]; void init(int _n){ n=_n; memset(tr,0,sizeof tr); } void add(int x,int v){ for(int i=x;i<=n;i+=i&-i) tr[i]+=v; } int sum(int x){ int ans=0; for(int i=x;i;i-=i&-i) ans+=tr[i]; return ans; } }tr; int n,a[N]; ll ans; int main(){ scanf("%d",&n); for(int i=1;i<=n;++i){ scanf("%d",&a[i]); a[i]++; } tr.init(n+2); for(int i=1;i<=n;++i){ int v=i-1-tr.sum(a[i]); ans+=v; tr.add(a[i],1); } for(int i=1;i<=n;++i){ printf("%lld\n",ans); tr.add(a[i],-1); int v=tr.sum(a[i]); ans-=v; v=tr.sum(n+2)-tr.sum(a[i]); ans+=v; tr.add(a[i],1); } return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <numeric> #include <random> #include <chrono> #include <queue> #include <utility> #include <string> #include <set> #include <map> #include <cmath> #include <limits> #include <stack> #define faster ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define showv(v) for(int i : v) cout << i + 1 << ' '; cout << '\n'; #define all(v) v.begin(), v.end() #define MAXN 120000 using namespace std; using namespace std::chrono; typedef long long ll; typedef long double ld; int inf = 1e9 + 9; int mod = 1e9 + 7; int dp[2005][2005]; int df[2005][2005]; template <typename T> void show(T * begin, T * end) { for(T * it = begin;it != end;it++) cout << *it << ' '; cout << '\n'; } void solve() { string s; cin >> s; if(s[0] == s[1] && s[1] == s[2]) cout << "Won\n"; else cout << "Lost\n"; } int main() { faster int test; test = 1; while(test--) { solve(); } // auto start = high_resolution_clock::now(); // auto stop = high_resolution_clock::now(); // double time = duration_cast<milliseconds>( stop - start ).count(); // cout << time << " ms" << endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < (n); ++i) #define srep(i,s,t) for(int i = s; i < t; ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) using namespace std; typedef long long int ll; typedef pair<int,int> P; #define yn {puts("First");}else{puts("Second");} #define MAX_N 200005 int main() { int T; cin >> T; rep(_,T){ int n; cin >> n; ll a[n] = {}; rep(i,n) cin >> a[i]; if(n % 2 == 1){ cout << "Second" << endl; continue; } sort(a,a+n); int ok = 0; rep(i,n/2){ if(a[i*2]!=a[i*2+1])ok = 1; } if(ok)yn; } return 0; }
/* __ /\ \ _ __ ___ ___\ \ \/'\ ___ __ ___ ___ __ /\`'__\/ __`\ /'___\ \ , < / __`\ /'__`\ /' _ `\ /' _ `\ /'__`\ \ \ \//\ \L\ \/\ \__/\ \ \\`\ /\ \L\ \/\ \L\.\_/\ \/\ \/\ \/\ \/\ \L\.\_ \ \_\\ \____/\ \____\\ \_\ \_\ \____/\ \__/.\_\ \_\ \_\ \_\ \_\ \__/.\_\ \/_/ \/___/ \/____/ \/_/\/_/\/___/ \/__/\/_/\/_/\/_/\/_/\/_/\/__/\/_/ */ #ifndef __AHA__HEADER #define __AHA__HEADER #include <bits/stdc++.h> using namespace std; #define g0 get<0> #define g1 get<1> #define g2 get<2> #define ft first #define sd second #define sz(x) (i6) x.size() #define psb(x) push_back(x) #define ppb() pop_back() #define bg(x) x.begin() #define ed(x) x.end() #define col(x) x.begin(), x.end() #define srt(x) sort(x.begin(), x.end()) #define vec vector #define deq deque #define hmap unordered_map #define pq priority_queue #define fn function #ifdef LOCAL #define git stauDBG_MACRO_NO_WARNING #include <dbg.h> #define dbgt dbg(dbg::time()) #else #define dbg(...) #define dbgt #endif typedef string str; typedef int32_t i3; typedef int64_t i6; typedef int64_t i64; typedef uint32_t u3; typedef uint64_t u6; typedef long double d6; typedef pair<i6, i6> p6; typedef vec<i6> vi6; typedef vec<p6> vp6; typedef vec<bool> vb; typedef vec<vi6> vv; typedef deq<p6> dp6; typedef deq<i6> di6; typedef map<i6, i6> mi6; typedef map<p6, i6> mp6; typedef set<i6> si6; typedef hmap<i6, i6> hi6; typedef vv graph; typedef vv matrix; const d6 EPS = 1 / 1000000.0; const i6 INF = INT64_MAX / 4; const i6 NINF = -INF; const i6 ZERO = 0; const i6 ONE = 1; namespace std { template <typename T1, typename T2> struct hash<pair<T1, T2>> { std::size_t operator()(const pair<T1, T2>& pair) const noexcept { return hash<T1>()(pair.first) ^ hash<T2>()(pair.second); } }; } // namespace std template <typename T1, typename T2> istream& operator>>(istream& stream, pair<T1, T2>& p) { stream >> p.ft; stream >> p.sd; return stream; } template <typename T1, typename T2> ostream& operator<<(ostream& stream, const pair<T1, T2>& p) { return stream << p.ft << " " << p.sd; } template <typename T> istream& operator>>(istream& stream, vec<T>& v) { if (v.empty()) { u6 len; stream >> len; v.assign(len, T()); } for (auto i = 0; i < sz(v); i++) { stream >> v[i]; } return stream; } template <typename T> ostream& operator<<(ostream& stream, const vec<T>& v) { if (!v.empty()) { stream << v[0]; } for (auto i = 1; i < sz(v); i++) { stream << " " << v[i]; } return stream; } #endif vi6 res; hmap<i6, vi6> g; vb colors(200002); vi6 c; void dfs(i64 idx, i64 p) { bool added = false; if (!colors[c[idx - 1]]) { added = true; colors[c[idx - 1]] = true; res.push_back(idx); } for (auto& e : g[idx]) { if (e != p) { dfs(e, idx); } } if (added) { colors[c[idx - 1]] = false; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL ifstream cin{"input.txt"}; ofstream cout{"output.txt"}; #endif cin >> c; i64 n = sz(c); for (i6 i = 0; i < n - 1; i++) { p6 tmp; cin >> tmp; g[tmp.ft].push_back(tmp.sd); g[tmp.sd].push_back(tmp.ft); } dfs(1, -1); srt(res); for (auto& e : res) { cout << e << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define endl "\n" typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;} template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;} vector<int> ans; void dfs(vector<vector<int>> &G, int v, int p, map<int, int> &S, vector<int> &C) { if (S[C[v]] == 0) { ans.push_back(v + 1); } S[C[v]]++; for(int i = 0; i < G[v].size(); i++) { int u = G[v][i]; if (u == p) continue; dfs(G, u, v, S, C); } S[C[v]]--; } void solve() { int N; cin >> N; vector<int> C(N); for (int i = 0; i < N; i++) { cin >> C[i]; } vector<vector<int>> G(N); for (int i = 0; i < N - 1; i++) { int a, b; cin >> a >> b; a--, b--; G[a].push_back(b); G[b].push_back(a); } map<int, int> S; dfs(G, 0, -1, S, C); sort(all(ans)); for (int i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } } int main() { #ifdef LOCAL_ENV cin.exceptions(ios::failbit); #endif cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); }
// #include <bits/stdc++.h> #include <iostream> #include <cmath> #include <string> #include <algorithm> #include <functional> #include <vector> #include <stack> #include <queue> #include <map> // #include <unordered_map> #include <set> // #include <unordered_set> #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; typedef pair<int, int> pii; 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 long long INF = 1LL << 60; const long long MOD = 1000000007; // const double PI = acos(-1); int main() { vector<int> A(3); rep(i, 3) cin >> A[i]; sort(A.begin(), A.end()); if (A[0] == A[1]) { cout << A[2] << endl; } else if (A[1] == A[2]) { cout << A[0] << endl; } else { cout << 0 << endl; } }
#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("fast-math") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define deb(x) cout << #x << " is " << x << "\n" #define int long long #define MOD 1000000007LL #define PI acos(-1) template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using max_heap = priority_queue<T>; template <class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename... T> void read(T &...args) { ((cin >> args), ...); } template <typename... T> void write(T &&...args) { ((cout << args), ...); } template <typename T> void readContainer(T &t) { for (auto &e : t) { read(e); } } template <typename T> void writeContainer(T &t) { for (const auto &e : t) { write(e, " "); } write("\n"); } auto speedup = []() { ios::sync_with_stdio(false); cin.tie(nullptr); return nullptr; }(); void solve(int tc) { long double A, B; read(A, B); long double ans = ((A - B) / A) * 100.0; cout << fixed << setprecision(16) << ans << "\n"; } signed main() { int tc = 1; // read(tc); for (int curr = 1; curr <= tc; ++curr) { solve(curr); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for(ll i=0;i<(ll)(n);i++) #define ALL(a) (a).begin(),(a).end() using namespace std; using ll=long long; typedef pair<ll,ll> P; ll n,q; vector<vector<ll>> G; vector<P> edge; int main(){ cin>>n; G.resize(n); edge.resize(n-1); rep(i,n-1){ ll a,b; cin>>a>>b; a--;b--; G[a].push_back(b); G[b].push_back(a); edge[i]={a,b}; } vector<ll> depth(n,-1); depth[0]=0; queue<ll> que; que.push(0); while(!que.empty()){ ll v=que.front(); que.pop(); for(auto x:G[v]){ if(depth[x]==-1){ depth[x]=depth[v]+1; que.push(x); } } } vector<ll> s(n); cin>>q; rep(i,q){ ll t,e,x; cin>>t>>e>>x; e--; ll a=edge[e].first,b=edge[e].second; if(t==1){ if(depth[a]<depth[b]){ s[0]+=x; s[b]-=x; }else{ s[a]+=x; } }else{ if(depth[a]>depth[b]){ s[0]+=x; s[a]-=x; }else{ s[b]+=x; } } } que.push(0); while(!que.empty()){ ll v=que.front(); que.pop(); for(auto x:G[v]){ if(depth[x]>depth[v]){ s[x]+=s[v]; que.push(x); } } } rep(i,n) cout<<s[i]<<endl; }
#include<bits/stdc++.h> #define int long long using namespace std; const int N = 17; const int mod = 1e9+ 7; int dp[(1<<N)][N]; int fx(vector<array<int,3>>&v, int mask , int last ){ if(dp[mask][last] != -1){ return dp[mask][last]; } int n = v.size(); if(mask == ((1<<n) -1)){ int c = 0; for(int j =0;j<3;j++){ if(j == 2){ c += max(0LL,v[last][j] - v[0][j]); }else{ c += abs(v[last][j] - v[0][j]); } } return c; } int ans = INT_MAX; for(int i = 1; i < n ; i ++){ if( (mask&(1<<i)) == 0){ int c = 0; for(int j =0;j<3;j++){ if(j == 2){ c += max(0LL,v[last][j] - v[i][j]); }else{ c += abs(v[last][j] - v[i][j]); } } int nca = c + fx(v,mask|(1 << i) ,i); ans = min(ans,nca); } } return (dp[mask][last] = ans); } signed main(){ int n; cin>>n; vector<array<int,3>>v; for(int i=0;i<n;i++){ int x,y,z; cin>>x>>y>>z; v.push_back({x,y,z}); } for(int i=0;i<(1<<n);i++){ for(int j=0;j<n;j++){ dp[i][j] = -1; } } int ans = fx(v,1,0); cout<<ans<<endl; return 0; } // // // n = 2; // 1 2 3 4 5 ... n // (1+2) / 2 = > 1 // n = 3 // 1 2 3 // (1+3)/2 => 2 // (2+2)/2 => 2 // n= 4 // () // 1 3 3 // 2 3 // 1 2 3 // 1 3 // 2 // 1 2 3 4 // 1 2 4 // 1 3 // 2
#include <bits/stdc++.h> #define LL long long #define ull unsigned long long #define F(i, j, k) for(int i = j; i <= k; i++) #define DF(i, j, k) for(int i = j; i >= k; i--) using namespace std; template <typename T> inline void read(T &n) { T w = 1; n = 0; char ch = getchar(); while (!isdigit(ch) && ch != EOF) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch) && ch != EOF) { n = (n << 3) + (n << 1) + (ch & 15); ch = getchar(); } n *= w; } template <typename T> inline void write(T x) { T l = 0; ull y = 0; if (!x) { putchar(48); return; } if (x<0) { putchar('-'); x = -x; } while (x) { y = y * 10 + x % 10; x /= 10; l++; } while (l) { putchar(y % 10 + 48); y /= 10; l--; } } template <typename T> inline void writes(T x) { write(x); putchar(' '); } template <typename T> inline void writeln(T x) { write(x); puts(""); } template <typename T> inline void checkmax(T &a,T b) { a = a > b ? a : b; } template <typename T> inline void checkmin(T &a,T b) { a = a < b ? a : b; } int num[26]; int main() { //freopen(".in", "r", stdin); //freopen(".out", "w", stdout); string st; cin >> st; int l = st.size(); LL ans = 0; DF(i, l - 1, 1) { if (st[i] == st[i - 1]) { if (i != l - 1 && st[i] != st[i + 1]) ans += l - 1 - i - num[int(st[i]) - 97]; } if (i != l - 1 && st[i] == st[i + 1]) { F(j, 0, 25) num[j] = 0; num[int(st[i]) - 97] = l - i - 1; } ++num[int(st[i]) - 97]; } writeln(ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); ll N; cin >> N; string S; cin >> S; vector<vector<ll>> cnt(N + 1, vector<ll>(4, 0)); for (ll i = 0; i < S.size(); i++) { for (ll j = 0; j < 4; j++) { cnt[i + 1][j] = cnt[i][j]; } if (S[i] == 'A') cnt[i + 1][0]++; if (S[i] == 'T') cnt[i + 1][1]++; if (S[i] == 'C') cnt[i + 1][2]++; if (S[i] == 'G') cnt[i + 1][3]++; } ll ans = 0; for (ll i = 0; i < N; i++) { for (ll j = i + 1; j <= N; j++) { vector<ll> temp(4, 0); for (ll k = 0; k < 4; k++) { temp[k] = cnt[j][k] - cnt[i][k]; } if (temp[0] == temp[1] && temp[2] == temp[3]) ans++; } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define deb(k) cerr << #k << ": " << k << "\n"; #define size(a) (int)a.size() #define fastcin cin.tie(0)->sync_with_stdio(0); #define st first #define nd second #define pb push_back #define mk make_pair #define int long long typedef long double ldbl; typedef double dbl; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef map<int, int> mii; typedef vector<int> vint; #define MAX 300100 #define MAXLG 20 const int inf = 0x3f3f3f3f; const ll mod = 998244353; const ll linf = 0x3f3f3f3f3f3f3f3f; const int N = 300100; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //knapstack ll fat[MAX+100]; ll ifat[MAX+100]; ll power(ll x, ll y, ll p){ ll res = 1; x = x % p; while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } void init(){ fat[0] = 1; for(ll i=1;i<=MAX;i++){ fat[i] = (fat[i-1]*i)%mod; } ifat[MAX] = power(fat[MAX], mod-2, mod); for(ll i=MAX-1;i>=0;i--) ifat[i] = (ifat[i+1]*(i+1))%mod; } ll choose(int x, int y){ if(y < 0 || y > x || x < 0) return 0; return fat[x]*(ifat[y]*ifat[x-y]%mod)%mod; } int n, m; int dp[5010][20]; int vis[5010][20]; int sol(int x, int y){ if(y == 17){ if(x) return 0; return 1; } if(vis[x][y]) return dp[x][y]; vis[x][y] = 1; int &c = dp[x][y]; for(int i=0;i<=n;i+=2){ if( (1<<y)*i > x) break; c = (c + sol(x - (1<<y)*i, y+1)*choose(n, i)%mod)%mod; } return c; } void solve(){ init(); cin>>n>>m; cout<<sol(m, 0)<<"\n"; // Have you read the problem again? // Maybe you understood the wrong problem } int32_t main(){ fastcin; int t_ = 1; //cin>>t_; while(t_--) solve(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #ifndef aa #define trace(...) #define endl '\n' #endif #define pb push_back #define ub upper_bound #define lb lower_bound #define fi first #define se second //#define int long long typedef long long ll; typedef long double ld; #define pii pair<int,int> #define pll pair<ll,ll> #define sz(x) ((long long)x.size()) #define fr(a,b,c) for(int a=b; a<=c; a++) #define frev(a,b,c) for(int a=c; a>=b; a--) #define rep(a,b,c) for(int a=b; a<c; a++) #define trav(a,x) for(auto &a:x) #define all(con) con.begin(),con.end() #define done(x) {cout << x << endl;return;} #define mini(x,y) x=min(x,y) #define maxi(x,y) x=max(x,y) const ll infl = 0x3f3f3f3f3f3f3f3fLL; const int infi = 0x3f3f3f3f; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); //const int mod = 998244353; const int mod = 1e9 + 7; typedef vector<int> vi; typedef vector<string> vs; typedef vector<vector<int>> vvi; typedef vector<pair<int, int>> vpii; typedef map<int, int> mii; typedef set<int> si; typedef set<pair<int,int>> spii; typedef queue<int> qi; uniform_int_distribution<int> rng(1,1e9); //DEBUG FUNCTIONS START #define cerr cout void __print(int x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE //#define deb(x...) cerr << "[" << #x << "] = "; _print(x) #define deb(x...) _print(x) #else #define deb(x...) #endif // DEBUG FUNCTIONS END int N = 2e5+5; void solve(){ int n, m, t; cin >> n >> m >> t; N = n; vpii a(m); rep(i,0,m) cin >> a[i].fi >> a[i].se; int prev = 0; rep(i,0,m){ n -= (a[i].fi - prev); if(n <= 0) done("No"); n += a[i].se - a[i].fi; mini(n, N); prev = a[i].se; } if(n > t - prev) done("Yes"); done("No"); } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(10); int t = 1; //cin >> t; while (t--) solve(); return 0; } int powm(int a, int b){ int res = 1; while (b) { if (b & 1) res = (res*a) % mod; a = (a * a) % mod; b >>= 1; } return res; } int divide(int a, int b) { return (a % mod) * powm(b % mod, mod - 2) % mod; } int norm(int a) { while (a >= mod) a -= mod; while (a < 0) a += mod; return a; }
#include <iostream> #include <vector> #include <utility> #include<algorithm> #include <string> #include <map> #include <cmath> #include <random> #include <tuple> #include <set> #include <queue> #define ll long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define mod % 1000000007 using namespace std; int main(){ ll n,m,k; cin >> n >> m >> k; ll kk = k; vector<ll> a(n); rep(i,n){ cin >> a[i]; } vector<vector<ll>> x(n,vector<ll>(n,0)); rep(i,n){ x[i][i] = 2*m; } rep(i,m){ ll c,d; cin >> c >> d; x[c-1][d-1] = 1; x[d-1][c-1] = 1; x[c-1][c-1] --; x[d-1][d-1] --; } while(k != 0){ if(k % 2 != 0){ vector<ll> b(n,0); rep(i,n){ rep(j,n){ b[i] = (b[i]+a[j]*x[j][i])mod; } } a = b; } k = k/2; vector<vector<ll>> b(n,vector<ll>(n,0)); rep(i,n){ rep(j,n){ rep(h,n){ b[i][j] = (b[i][j] + x[h][j] * x[i][h])mod; } } } x = b; } ll t = 1; ll s = 1; ll d = 2*m; k = kk; while(k != 0){ if(k % 2 == 1){ s = (s*d)mod; } d = (d*d)mod; k = k/2; } ll rem = 1000000005; while(rem != 0){ if(rem % 2 == 1){ t = (t*s) mod; } s = (s*s)mod; rem = rem/2; } rep(i,n){ ll z = (a[i]*t)mod; cout << z << endl; } return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int mod = 1e9 + 7; int dp[3][100][100]; constexpr ll modpow(ll A, ll B) { ll kotae = 1; while (B > 0) { if (B & 1) kotae = kotae * A % mod; A = A * A % mod; B >>= 1; } return kotae; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M, K; cin >> N >> M >> K; int A[100]; rep(i, N) { cin >> A[i]; } rep(i, N) dp[0][i][i] = 1; rep(i, N) dp[1][i][i] = 1; int are = modpow(2 * M, mod - 2); rep(i, M) { int x, y; cin >> x >> y; x--; y--; (dp[0][x][x] += mod - are) %= mod; (dp[0][x][y] += are) %= mod; (dp[0][y][x] += are) %= mod; (dp[0][y][y] += mod - are) %= mod; } auto mae = dp[1], ato = dp[2]; rep(i, 30) { rep(x, N) rep(y, N) { ll tmp = 0; rep(z, N) { tmp += mae[x][z] * (ll)mae[z][y] % mod; } ato[x][y] = tmp % mod; } swap(mae, ato); if (K >> 29 - i & 1) { rep(x, N) rep(y, N) { ll tmp = 0; rep(z, N) { tmp += mae[x][z] * (ll)dp[0][z][y] % mod; } ato[x][y] = tmp % mod; } swap(mae, ato); } } rep(i, N) { ll kotae = 0; rep(j, N) kotae += (ll)mae[i][j] * A[j] % mod; co(kotae % mod); } Would you please return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for(int i=(a); i<(n); i++) #define rrep(i,a,n) for(int i=(a); i>=(n); i--) #define all(n) begin(n),end(n) #define rall(n) rbegin(n),rend(n) //#define yn(bool) if(bool){cout<<Yes<<endl;}else{cout<<No<<endl;} #define fore(i,a) for(auto &i:a) using ll = long long; using vi = vector<int>; using vl = vector<ll>; ll myceil(ll a,ll b){return (a+(b-1))/b;} ll myfloor(ll a,ll b){return a/b;} 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 _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } void _main() { int a,b; cin>>a>>b; vl e(a+b); if(a >= b) { rep(i,0,a) e[i] = i+1; rep(i,a,a+b-1) e[i] = -(i+1-a); rep(i,0,a+b-1) e[a+b-1] -= e[i]; }else{ rep(i,0,a-1) e[i] = i+1; rep(i,a,a+b) e[i] = -(i+1-a); rep(i,0,a+b){ if(i != a-1) e[a-1] -= e[i]; } } rep(i,0,a+b){ if(i) cout<<" "; cout<<e[i]; } cout<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main(){ int A, B; cin >> A >> B; long long a = 0; if(A > B){ for(int i = 1; i <= A; i++){ cout << i << " "; a += i; } for(int i = 1; i < B; i++){ cout << -i << " "; a -= i; } cout << -a << endl; }else if(A < B){ for(int i = 1; i <= B; i++){ cout << -i << " "; a += i; } for(int i = 1; i < A; i++){ cout << i << " "; a -= i; } cout << a << endl; }else{ for(int i = 1; i <= A; i++){ cout << i << " " << -i; if(i != A){ cout << " "; }else{ cout << endl; } } } }
#include <bits/stdc++.h> // #include <atcoder/all> // #include "icld.cpp" using namespace std; using ll = long long int; using pii = pair<int,int>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using ss = string; using db = double; const int dx[4] = {1,0,-1,0}; const int dy[4] = {0,1,0,-1}; #define V vector #define pi 3.14159265358979 #define rep(i,s,n) for(int i=(s);i<(int)(n);i++) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define ci(x) cin >> x #define cii(x) int x;cin >> x #define cci(x,y) int x,y;cin >> x >> y #define co(x) cout << x << endl void chmax(int &x,int y){x=max(x,y);} void chmin(int &x,int y){x=min(x,y);} int main(){ cci(n,m); int n2=n/2; vi mi(n2),ma(n2),v(n); rep(i,0,n)ci(v[i]); vi tc(m); rep(i,0,m)ci(tc[i]); sort(all(v)); sort(all(tc)); rep(i,0,n-1)(i%2?ma:mi)[i/2]=v[i+1]-v[i]; // rep(i,0,n2)cout<<mi[i]<<" "; vi smi(n2+1,0),sma(n2+1,0); rep(i,1,n2+1)smi[i]=smi[i-1]+mi[i-1]; rep(i,0,n2)sma[n2-i-1]=sma[n2-i]+ma[n2-i-1]; // co("v");for(int x:v)cout<<x<<" ";co(""); // co("tc");for(int x:tc)cout<<x<<" ";co(""); // co("smi");for(int x:smi)cout<<x<<" ";co(""); // co("sma");for(int x:sma)cout<<x<<" ";co(""); int ans=1001001001; rep(i,0,m){ int nx=lower_bound(all(v),tc[i])-v.begin(); nx/=2; int dis=abs(v[nx*2]-tc[i]); int now=smi[nx]+dis+sma[nx]; chmin(ans,now); } co(ans); }
#include <bits/stdc++.h> using namespace std; #define dbg(v) cout << __LINE__ << ": " << #v << " = " << v << endl const int N = 1e5 + 5; int main() { int n, m; scanf("%d %d", &n, &m); vector <int> h(n); for (int i = 0; i < n; i++) { scanf("%d", &h[i]); } sort(h.begin(), h.end()); vector <long long> pre(n), suf(n); for (int i = 1; i < n; i += 2) { pre[i] = h[i] - h[i - 1]; if (i > 1) { pre[i] += pre[i - 2]; } } for (int i = n - 2; i >= 0; i -= 2) { suf[i] = h[i + 1] - h[i]; if (i + 2 < n) { suf[i] += suf[i + 2]; } } long long ans = LLONG_MAX; for (int it = 0; it < m; it++) { int w; scanf("%d", &w); int pos = lower_bound(h.begin(), h.end(), w) - h.begin(); long long now; if (pos & 1) { now = w - h[pos - 1]; if (pos - 2 >= 0) { now += pre[pos - 2]; } if (pos < n) { now += suf[pos]; } } else { now = h[pos] - w; if (pos - 1 >= 0) { now += pre[pos - 1]; } if (pos + 1 < n) { now += suf[pos + 1]; } } ans = min(ans, now); } printf("%lld\n", ans); return 0; }
#include<iostream> #include<vector> #include<map> #include<algorithm> #include<cstring> using namespace std; const int BUF = 200005; const int INF = 1 << 30; class SegTree{ public: vector<long long> v; vector<long long> cnt; SegTree(){ v = vector<long long>(BUF * 4, 0); cnt = vector<long long>(BUF * 4, 0); } pair<long long, int> get(int node, int L, int R, int LQ, int RQ){ if(R<LQ || RQ<L) return make_pair(0, 0); if(LQ<=L && R<=RQ) return make_pair(v[node], cnt[node]); pair<long long, int> s = get(node*2+1,L,(L+R)/2,LQ,RQ); pair<long long, int> t = get(node*2+2,(L+R)/2+1,R,LQ,RQ); return make_pair(s.first + t.first, s.second + t.second); } pair<long long, int> update(int node, int L, int R, int Q, int chVal){ if(R<Q || Q<L) return make_pair(v[node], cnt[node]); if(L==R) { v[node] += chVal; if (chVal > 0) ++cnt[node]; if (chVal < 0) --cnt[node]; return make_pair(v[node], cnt[node]); } pair<long long, int> s = update(node*2+1,L,(L+R)/2,Q,chVal); pair<long long, int> t = update(node*2+2,(L+R)/2+1,R,Q,chVal); v[node] = s.first + t.first; cnt[node] = s.second + t.second; return make_pair(v[node], cnt[node]); } }; int row, col, nQuery; int query[BUF][3]; void read() { cin >> row >> col >> nQuery; for (int i = 0; i < nQuery; ++i) { for (int j = 0; j < 3; ++j) { cin >> query[i][j]; } --query[i][1]; } } void work() { static int row2val[BUF] = {}; static int col2val[BUF] = {}; vector<int> vList; vList.push_back(-1); vList.push_back(0); vList.push_back(INF); for (int i = 0; i < nQuery; ++i) { vList.push_back(query[i][2]); } sort(vList.begin(), vList.end()); vList.erase(unique(vList.begin(), vList.end()), vList.end()); map<int, int> val2idx; for (int i = 0; i < vList.size(); ++i) { val2idx[vList[i]] = i; } SegTree rowStree = SegTree(); SegTree colStree = SegTree(); long long ans = 0; for (int loop = 0; loop < nQuery; ++loop) { int type = query[loop][0], idx = query[loop][1], val = query[loop][2]; if (type == 1) { // row int pre = row2val[idx]; ans -= 1LL * col * pre; ans += colStree.get(0, 0, BUF - 1, val2idx[0], (--val2idx.find(pre))->second).first; ans += 1LL * colStree.get(0, 0, BUF - 1, val2idx[pre], val2idx[INF]).second * pre; ans += 1LL * col * val; ans -= colStree.get(0, 0, BUF - 1, val2idx[0], (--val2idx.find(val))->second).first; ans -= 1LL * colStree.get(0, 0, BUF - 1, val2idx[val], val2idx[INF]).second * val; row2val[idx] = val; rowStree.update(0, 0, BUF - 1, val2idx[val], val); rowStree.update(0, 0, BUF - 1, val2idx[pre], -pre); } else { // col int pre = col2val[idx]; ans -= 1LL * row * pre; ans += rowStree.get(0, 0, BUF - 1, val2idx[0], (--val2idx.find(pre))->second).first; ans += 1LL * rowStree.get(0, 0, BUF - 1, val2idx[pre], val2idx[INF]).second * pre; ans += 1LL * row * val; ans -= rowStree.get(0, 0, BUF - 1, val2idx[0], (--val2idx.find(val))->second).first; ans -= 1LL * rowStree.get(0, 0, BUF - 1, val2idx[val], val2idx[INF]).second * val; col2val[idx] = val; colStree.update(0, 0, BUF - 1, val2idx[val], val); colStree.update(0, 0, BUF - 1, val2idx[pre], -pre); } cout << ans << endl; } } int main() { read(); work(); return 0; }
#include<bits/stdc++.h> #define ll long long using namespace std; const long long N=2e6+6; ll a[N],b[N]; long long n,m,q,ans; struct segment { long long siz[N<<2],sum[N],ls[N<<2],rs[N<<2],cnt=0,root=0; void change(ll l,ll r,ll &p,ll x,ll k) { if(!p) p=++cnt; if(l==r) { sum[p]+=k*x; siz[p]+=k; return; } ll mid=(l+r)>>1; if(x<=mid) change(l,mid,ls[p],x,k); else change(mid+1,r,rs[p],x,k); sum[p]=sum[ls[p]]+sum[rs[p]]; siz[p]=siz[ls[p]]+siz[rs[p]]; } ll query1(ll l,ll r,ll p,ll x) { if(!p) return 0; if(r<=x) return siz[p]; ll mid=(l+r)>>1; if(x<=mid) return query1(l,mid,ls[p],x); else return siz[ls[p]]+query1(mid+1,r,rs[p],x); } ll query2(ll l,ll r,ll p,ll x) { if(!p) return 0; if(l>x) return sum[p]; ll mid=(l+r)>>1; if(x<=mid) return sum[rs[p]]+query2(l,mid,ls[p],x); else return query2(mid+1,r,rs[p],x); } } A,B; signed main() { scanf("%lld %lld %lld",&n,&m,&q); A.change(0,1e8,A.root,0,n),B.change(0,1e8,B.root,0,m); for(long long i=1;i<=q;i++) { long long opt,t,x,y; cin>>opt>>x>>y; if(opt==1) { ans=ans-B.query1(0,1e8,B.root,a[x])*a[x]-B.query2(0,1e8,B.root,a[x]); A.change(0,1e8,A.root,a[x],-1); A.change(0,1e8,A.root,y,1); a[x]=y; ans+=B.query1(0,1e8,B.root,y)*y+B.query2(0,1e8,B.root,y); } else { ans=ans-A.query1(0,1e8,A.root,b[x])*b[x]-A.query2(0,1e8,A.root,b[x]); B.change(0,1e8,B.root,b[x],-1); B.change(0,1e8,B.root,y,1); b[x]=y; ans+=A.query1(0,1e8,A.root,y)*y+A.query2(0,1e8,A.root,y); } cout<<ans<<endl; } return 0; }
#include <iostream> #include<vector> #include<string> #include<algorithm> #include<iomanip> #include<map> #include<complex> #include<functional> #include<stack> #include<queue> #include<set> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define req(i,n) for(int i = 1;i <= n; i++) #define rrep(i,n) for(ll i = n-1;i >= 0;i--) #define ALL(obj) begin(obj), end(obj) #define RALL(a) rbegin(a),rend(a) typedef long long ll; typedef long double ld; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e18; ll mod = 1e9 + 7; int main() { int n, d, s,x,y; cin >> n >> s >> d; bool ok = 0; rep(i, n) { cin >> x >> y; if (x<s && y>d)ok = 1; }if (ok)cout << "Yes" << endl; else cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; #define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define lop ios; ll t; cin>>t; while(t--) #define pb push_back #define vi vector<ll> #define vivi vector<vector<ll>> #define vipa vector<pair<ll,ll>> #define F first #define S second #define M 1000000007 #define kl "\n" #define all(v) v.begin(),v.end() typedef long long ll; ll lcm(ll x, ll y){ll k=x*y; k/=(__gcd(x,y)); return k;} ll power(ll x,ll y){ ll r= 1;x = x % M;if(x==0) return 0; while(y>0) {if(y&1) r=(r*x)%M; y=y>>1; x=(x*x)%M;} return r;} void solve() { ll n,s,d; cin>>n>>s>>d; for(ll i=0;i<n;i++){ ll a,b; cin>>a>>b; if(a < s and b > d){ cout<<"Yes"; return; } } cout<<"No"; } int main() { // lop solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define int long long int mod=1e9+7; signed main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k; cin>>k; int ans=0; for(int i=1;i<=k;i++){ for(int j=1;j<=k;j++){ int x=i*j; if(x>k){ break; } else{ ans=ans+k/x; } } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for(int i=0; i<(n); ++i) #define RREP(i, n) for(int i=(n);i>=0;--i) #define FOR(i, a, n) for (int i=(a); i<(n); ++i) #define RFOR(i, a, b) for(int i=(a);i>=(b);--i) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #define DUMP(x) cerr<<#x<<" = "<<(x)<<endl #define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl; template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { REP(i, SZ(v)) { if (i) os << " "; os << v[i]; } return os; } template<class T> void debug(const vector<T> &v) { cout << "["; REP(i, SZ(v)) { if (i) cout << ", "; cout << v[i]; } cout << "]" << endl; } template<class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << " " << p.second; } template<class T, class U> void debug(const pair<T, U> &p) { cout << "(" << p.first << " " << p.second << ")" << endl; } template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } using ll = long long; using ull = unsigned long long; using ld = long double; using P = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvll = vector<vll>; const ll MOD = 1e9 + 7; const int INF = INT_MAX; const ll LINF = LLONG_MAX; const int inf = INT_MIN; const ll linf = LLONG_MIN; const ld eps = 1e-9; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); ll n; cin >> n; ll cnt = 0; ll l = 1, r = n+1; while (r - l > 1) { ll mid = (l + r) / 2; if (LINF / mid < mid + 1) { r = mid; continue; } ll su = mid * (mid + 1) / 2; if (su <= n + 1) { l = mid; } else { r = mid; } } cnt = l - 1; cout << n - cnt << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> #include <string> #include <algorithm> using namespace std; using vi = vector<int>; // intの1次元の型に vi という別名をつける using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける using vvvi = vector<vvi>; using vb = vector<bool>; using ll = long long; //約9.0*10^18 using vll = vector<ll>; using vvll = vector<vll>; using vvvll =vector<vvll>; using std::reverse; const double PI=3.14159265358979323846; const ll inf=9223372036854775807; #define rep0(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i < (int)(n); i++) #define all(x) (x).begin(),(x).end() ll fac(ll n) {ll ans=1;rep0(i,n){ans=ans*(n-i);};return ans;}//n! ll conb(ll n,ll m){return fac(n)/fac(m)/fac(n-m);}//nCm //小数点以下を指定したい時  →→→→ cout << fixed << setprecision(); //int→string  string str = to_string(num); //string→int int num = atoi(numStr.c_str()); //複数のvector管理→;vector<tuple<string, int, int>> SP(N); //int max = *max_element(all(A));最大値 //int min = *min_element(all(B));最小値 //floor(x)=[x] // *max_element(a.begin(),a.begin()+5);aの最初から6番目までの最大値/計算量に不安あり // *min_element(,); // sort は昇順12345 //普通の割り算がしたい→cout<<(double)3/2/2<<endl; //ll A = lower_bound(a,a+n,x) - a;xになった瞬間のi //ll B = upper_bound(c,c+n,x) - c;xを超えた瞬間のi //ll n;cin>>n;vll a(n);rep0(i,n){cin>>a[i];} int main() { ll n;cin>>n; vll a(n); rep0(i,n){ cin>>a[i]; } sort(all(a)); ll ans = 0; rep0(i,n){ ans+=(2*i-n+1)*a[i]; //cout<<ans<<endl; } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) printf("%d %d\n",(i*2-1)%n+1,(i*2)%n+1); return 0; }
/* author : aryan57 created : 16-May-2021 18:55:19 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 = 2e18; const int32_t M = 1000000007; // const int32_t M = 998244353; const long double pie = acos(-1); int same(vector <int> &a,vector <int> &b) { map<int,int> mp; mp[0]=1; int pa=0; int pb=0; for(int i=0;i<min(sz(a),sz(b));i++) { pa+=a[i]; pb+=b[i]; mp[pa-pb]++; } int cnt=0; for(pair <int,int> p : mp) { int num=p.Y; cnt+=(num)*(num-1)/2; } return cnt; } int other(vector <int> &a,vector <int> &b) { map<int,int> mp1; int n=min(sz(a),sz(b)); vector <int> pa(n+1); vector <int> pb(n+1); mp1[0]=1; for(int i=1;i<=n;i++) { pa[i]=pa[i-1]+a[i-1]; pb[i]=pb[i-1]+b[i-1]; mp1[pa[i]-pb[i]]++; } mp1[pa[n]-pb[n]]--; int cnt=0; // dbg(a); // dbg(b); // dbg(mp1); if(sz(a)>sz(b)) { int dif=pa[n]+a[n]-pb[n]; // dbg(dif,n,a[n],pa[n],pb[n]); cnt+=mp1[dif]; } for(int i=n;i>=2;i--) { mp1[pa[i-1]-pb[i-1]]--; int dif=pa[i]-pb[i-1]; // dbg(i,dif); // dbg(mp1); cnt+=mp1[dif]; } // dbg(cnt); return cnt; } void solve_LOG() { int n; cin>>n; vector <int> a(n); vector <int> v1e,v1o; vector <int> v2e,v2o; F(i,0,n-1) { cin>>a[i]; } for(int i=0;i<n;i+=1) { if(i%2)v1o.push_back(a[i]); else v1e.push_back(a[i]); } for(int i=1;i<n;i+=1) { if(i%2)v2e.push_back(a[i]); else v2o.push_back(a[i]); } int cnt=0; cnt+=same(v1e,v1o); cnt+=same(v2e,v2o); // dbg(cnt); cnt+=other(v1e,v1o); cnt+=other(v2e,v2o); cout<<cnt; } 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; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; //int:2*10**9 typedef long double ld; typedef pair<ll,ll> P; #define REP(i,n) for(ll i = 0; i<(ll)(n); i++) #define FOR(i,a,b) for(ll i=(a);i<=(b);i++) #define FORD(i,a,b) for(ll i=(a);i>=(b);i--) #define pb push_back #define MOD 1000000007 //998244353 #define PI 3.141592653 #define INF 1000000000000000 //14 //cin.tie(0);cout.tie(0);ios::sync_with_stdio(false); int main(){ ll n; cin >> n; vector<ll> a(n); vector<ll> asum(n); map<ll,ll> m; ll tot = 0; REP(i,n) { cin >> a[i]; if (i%2==0) tot+=a[i]; else tot-=a[i]; asum[i]=tot+INF; } REP(i,n) { m[asum[i]]++; } m[INF]++; // REP(i,n) cout << asum[i]-INF << " "; // cout << endl; ll ans = 0; for (auto ite=m.begin();ite!=m.end();ite++) { ll cnt = ite->second; ans += cnt*cnt-cnt; } cout << ans/2 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define M 500000 typedef long long ll; typedef unsigned long long ull; inline ll read() { char ch; ll f = 1, res = 0; while ((ch = getchar()) < '0' || ch > '9') if (ch == '-') f = -1; while (ch >= '0' && ch <= '9') { res = (res << 1) + (res << 3) + (ch ^ 48); ch = getchar(); } return f * res; } inline void pt(ll u,ll mod) { printf("%lld", u); if (mod == 1) putchar(' '); else if (mod == 2) putchar('\n'); } bool con[1010][1010]={false}; ll mat[1010][1010]; vector<ll>e[M][26]; ll m,n; ll ans=1e15; char c; //struct node { // ll x, y; //}; void dfs(ll inx1,ll inx2,ll v ) { // pt(inx1,inx2); if (v >= ans) return; mat[inx1][inx2] = v; if (con[inx1][inx2]) { if (inx1 == inx2) ans = v; else ans = v + 1; } for (ll i = 0; i < 26; i++) { for (auto u:e[inx1][i]) for (auto p:e[inx2][i]) { if (mat[u][p] > v + 2) dfs(u, p, v + 2); } } } int main() { //freopen("../in", "r", stdin); //freopen("../out", "w", stdout); memset(mat, 0x3f, sizeof(mat)); m = read(), n = read(); ll a, b; for (ll i = 0; i < n; i++) { a = read(), b = read(); scanf("%c", &c); con[a][b] = true; con[b][a] = true; e[a][c - 'a'].push_back(b); e[b][c - 'a'].push_back(a); } for (ll i = 1; i <= m; i++) con[i][i] = true; dfs(1, m, 0); if (ans > 1e14) pt(-1, 2); else pt(ans, 2); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ff first #define ss second #define pb push_back #define mp make_pair #define deb(x) cout<< #x << " " << x << "\n"; #define MAX 9223372036854775807 #define MIN -9223372036854775807 #define setbits(n) __builtin_popcountll(n) #define mkunique(a) sort(a.begin(),a.end()); a.resize(unique(a.begin(),a.end())-a.begin()); #define print(s) for(ll u: s) cout<<u<<" "; cout<<"\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll mod=1e9+7; const int N=1e5+10; vector<pair<ll,pair<ll,ll>>> a[N]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll TT=clock(); ll n,m; cin>>n>>m; for(ll i=0;i<m;i++){ ll u,v,c,d; cin>>u>>v>>c>>d; u--,v--; a[u].pb({v,{c,d}}); a[v].pb({u,{c,d}}); } vector<ll> d(n,mod*mod); d[0] = 0; set<pair<ll,ll>> s; s.insert({0,0}); while(!s.empty()){ ll v = s.begin()->ss; s.erase(s.begin()); for(auto ed: a[v]){ ll u = ed.ff, c = ed.ss.ff, dd = ed.ss.ss; ll t = d[v]; ll cost = t + c + dd/(t+1); ll opt = max(t,(ll)sqrt(dd) - 1); cost = min(cost, opt + c + dd/(opt+1)); opt++; cost = min(cost, opt + c + dd/(opt+1)); if(cost < d[u]){ s.erase({d[u],u}); d[u] = cost; s.insert({d[u],u}); } } } if(d[n-1]==mod*mod) cout<<-1; else cout<<d[n-1]; cerr<<"\n\nTIME: "<<(long double)(clock()-TT)/CLOCKS_PER_SEC<<" sec\n"; TT = clock(); return 0; }
#include <iostream> #include <vector> #include <string> #include <array> #include <functional> #include <algorithm> #include <stack> #include <map> #include <set> #include <climits> #include <queue> #include <bitset> #include <cassert> #include <math.h> #include <complex> #include <iomanip> #include <unordered_map> using namespace std; #define ll long long #define pb(x) push_back(x) #ifdef _OLIMPOLOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 1337 #endif void debug_out() { cout << "\n";} template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cout << " " << A; debug_out(B...); } int test = 1; #define out(x) {cout << x << "\n";return;} #define all(N) N.begin(),N.end() #define allr(N) N.rbegin(),N.rend() template<typename T1> void pr(vector<T1> V, int add = 0, int start = -1, int end = -1) { if (start < 0) start = 0; if (end < 0) end = V.size() - 1; for (int i = start; i <= end; i++) { cout << V[i] + add << ((i == end) ? "\n" : " "); } } template<typename T1> T1 chmin(T1 &x, const T1 v) { return x = min(x,v); } template<typename T1> T1 chmax(T1 &x, const T1 v) { return x = max(x,v); } #define rep(i, n) for (int i = 0; i < n; i++) #define reps(i, s, n) for (int i = s; i < n; i++) #define repr(i, n) for (int i = n-1; i >= 0; i--) #define repsr(i, s, n) for (int i = n-1; i >= s; i--) #define PI pair<int,int> template<typename T1> T1 gcd(const T1 &a, const T1 &b) { if (a == 0 || b == 0) return a + b; return gcd(b, a %b); } //-------------------------- ^ DONT TOUCH ^----------------------------------------------------------------- #define MAX 200001 #define MOD 998244353 int N[21]; int bx, n; void comb(int i, int xr, int x) { if (i == -1) { chmin(bx, x ^ xr); return; } if (x > 0) comb(i-1, xr ^ x, N[i]); comb(i-1, xr, x | N[i]); } void solve() { cin >> n; rep(i,n) cin >> N[i]; bx = INT_MAX; comb(n-1,0,0); out(bx); } int main() { ios_base::sync_with_stdio(0);cin.tie(0); int test = 1; #ifdef _OLIMPOLOCAL cin >> test; #endif rep(testCase, test) { solve(); } return 0; }
#include <bits/stdc++.h> #define watch(x) cout << (#x) << " is " << (x) << endl #define debug cout << "hi" << endl #define maxn 3e5+50 using namespace std; typedef long long ll; typedef long long LL; typedef long double ld; typedef pair<int, int> pii; typedef pair<long long ,long long>pll; const ll mod = 1e9+7; const int INF32 = 1<<30; const ll INF64 = 1LL<<60; void solve(){ int n;cin >> n; vector<int>a(n); for(int i = 0;i<n;i++) cin >> a[i]; int ans = INT_MAX; int start = a[0]; for(int mask = 0;mask<(1<<(n-1));mask++){ int tp = start; int tp2 = mask; int cnt = 1; vector<int>b; while(tp2){ if(tp2%2){b.push_back(tp);tp = a[cnt];} else tp |= a[cnt]; cnt++;tp2 /= 2; } for(int i = cnt;i<n;i++) tp |= a[i]; b.push_back(tp); int now = b[0]; for(int i = 1;i<b.size();i++) now ^= b[i]; ans = min(ans,now); } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); // freopen("shuffle.in","r",stdin); // freopen("shuffle.out","w",stdout); solve(); return 0; }
//Author- Vaibhav Singh #include<bits/stdc++.h> /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_multiset tree<long long , null_type,less_equal<long long >, rb_tree_tag,tree_order_statistics_node_update> #define ordered_set tree<long long , null_type,less<long long >, rb_tree_tag,tree_order_statistics_node_update> */ using namespace std; #define ll long long int #define YES cout<<"YES"<<"\n"; #define NO cout<<"NO"<<"\n"; #define ld long double #define yes cout<<"yes"<<"\n"; #define no cout<<"no"<<"\n"; #define No cout<<"No"<<"\n"; #define Yes cout<<"Yes"<<"\n"; #define f(i,a) for(i=0;i<a;i++) #define fo(i,a) for(i=1;i<=a;i++) #define fa(i,a) for(auto i:a) #define r(i,a) for(auto i=a.rbegin();i!=a.rend();i++) #define en cout<<"\n"; #define ull unsigned long long int #define o(x) cout<<x<<"\n"; #define o1(x) cout<<x<<" "; #define pb push_back #define F first #define in insert #define mp make_pair #define S second #define pre(n) cout<<fixed<<setprecision(n); #define gcd(a,b) __gcd(a,b) #define bs binary_search #define fast ios_base::sync_with_stdio(false);cin.tie(NULL); #define test ll t;cin>>t;while(t-->0) const ll Mod = 998244353; #define mod 1000000007 #define pi 3.14159265358979323846 #define all(x) x.begin(),x.end() #define re return 0; #define vl vector<ll> #define vc vector<char> #define ul unordered_map<ll,ll> #define uc unordered_map<char,ll> #define vp vector<pair<ll,ll>> // *#######################################################################################* /* bool isp(ll n) { ll i; for(i=2;i*i<=n;i++) { if(n%i==0) { return false; } } } */ ll powerm(ll x, unsigned ll y, ll p) { ll res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; // y = y/2 x = (x*x) % p; } return res; } long long lcm(long long a, long long b){//????????? return a * b / gcd(a, b); } /* ll po(ll z,ll z1) { ll rer=0; rer= ((z*(z-1))/2+(z1*(z1-1))/2); return rer; } */ /* bool sortbysec(const pair<int,int> &a, const pair<int,int> &b) { return (a.second < b.second); } */ /* bool is_prime(ll n){ for(ll i = 2;i*i<=n;i++){ if(n%i == 0) return false; } return true; } */ /* int inv(int a){ return powerm(a,mod-2,mod); } ll const N=1e5; ll fact[N], invfact[N]; void precompute(){ fact[0] = 1; for(int i=1;i<N;i++) fact[i] = (fact[i-1]*i)%mod; invfact[N-1] = inv(fact[N-1])%mod; for(int i=N-2;i>=0;i--) invfact[i] = (invfact[i+1]*(i+1))%mod; } ll ncr(ll n, ll r){ if(n<r) return 0; return (((fact[n]*invfact[r])%mod)*invfact[n-r])%mod; } */ //**************************************************************************** //**************************************************************************** //**************************************************************************** //**************************************************************************** //**************************************************************************** //**************************************************************************** //**************************************************************************** //*88888*********5555555555555555******88888888$$$$$$$$$$******!!!!!!!!!! int main() { fast ll a,b; cin>>a>>b; ll x=abs(a-b); if(x<=2) { Yes } else { No } //????????? re}
/* Author: Nguyen Tan Bao Status: Idea: */ #include <bits/stdc++.h> #define FI first #define SE second #define EPS 1e-9 #define ALL(a) a.begin(),a.end() #define SZ(a) int((a).size()) #define MS(s, n) memset(s, n, sizeof(s)) #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORE(i,a,b) for (int i = (a); i >= (b); i--) #define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) //__builtin_ffs(x) return 1 + index of least significant 1-bit of x //__builtin_clz(x) return number of leading zeros of x //__builtin_ctz(x) return number of trailing zeros of x using namespace std; using ll = long long; using ld = double; typedef pair<int, int> II; typedef pair<II, int> III; typedef complex<ld> cd; typedef vector<cd> vcd; const ll MODBASE = 1000000007LL; const int MAXN = 510; const int MAXM = 1000; const int MAXK = 16; const int MAXQ = 200010; int n, lab[MAXN], upperL, upperR, lowerL, lowerR; II a[MAXN]; int getRoot(int u) { while (lab[u] > 0) u = lab[u]; return u; } int same(int u, int v) { return getRoot(u) == getRoot(v); } void unionFind(int u, int v) { u = getRoot(u); v = getRoot(v); int x = lab[u] + lab[v]; if (lab[u] < lab[v]) { lab[v] = u; lab[u] = x; } else { lab[u] = v; lab[v] = x; } } int dist(int i, int j) { return (a[i].FI - a[j].FI) * (a[i].FI - a[j].FI) + (a[i].SE - a[j].SE) * (a[i].SE - a[j].SE); } bool check(ld r) { FOR(i,1,n) lab[i] = -1; FOR(i,1,n) FOR(j,i+1,n) { // cout << i << ' ' << j << ' ' << dist(i, j) << endl; if (dist(i, j) <= 4*r*r) if (!same(i, j)) { // cout << i << ' ' << j << endl; unionFind(i, j); } } FOR(i,upperL,upperR) FOR(j,lowerL,lowerR) if (same(i,j)) return false; return true; } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cin >> n; FOR(i,1,n) cin >> a[i].FI >> a[i].SE; FOR(i,-100,100) { n++; if (i == -100) upperL = n; if (i == 100) upperR = n; a[n] = II(i, 100); } FOR(i,-100,100) { n++; if (i == -100) lowerL = n; if (i == 100) lowerR = n; a[n] = II(i, -100); } // cout << check(50) << endl; ld dau = 0, cuoi = 100; FOR(i,1,100) { ld mid = (dau + cuoi) / 2; if (check(mid)) dau = mid; else cuoi = mid; } cout << fixed << setprecision(9) << dau; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long ll a[2000005], b[2000005], tree[4*300000]; void build(ll node, ll lo, ll hi) { if(lo==hi) { tree[node] =a[lo]; return; } ll left = node*2, right = left+1; ll mid = (lo+hi)/2; build(left, lo, mid); build(right, mid+1, hi); tree[node] = tree[left]^tree[right]; } void update(ll node, ll lo, ll hi, ll i, ll val) { if(lo>i||hi<i) return; if(lo==i&&hi==i) { tree[node]^=val; return; } ll left = node*2, right = left+1; ll mid = (lo+hi)/2; update(left, lo,mid, i,val); update(right, mid+1, hi, i,val); tree[node] = tree[left]^tree[right]; } ll query(ll node, ll lo, ll hi, ll i, ll j) { if(lo>j||hi<i) return 0LL; if(lo>=i&&hi<=j) { return tree[node]; } ll left = node*2, right = left+1; ll mid = (lo+hi)/2; ll p = query(left, lo, mid, i,j); ll q = query(right, mid+1, hi, i, j); return p^q; } int main() { ll n, q; cin >> n >> q; for(ll i=1;i<=n;i++) cin >> a[i]; build(1,1,n); while(q--) { ll t, x, y; scanf("%lld%lld%lld",&t,&x,&y); if(t==1) { update(1,1,n,x,y); } else { ll ans = query(1,1,n,x,y); printf("%lld\n",ans); } } }
#include<bits/stdc++.h> using namespace std; #define il inline #define ri register int #define ll long long #define ui unsigned int il ll read(){ bool f=true;ll x=0; register char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=false;ch=getchar();} while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); if(f) return x; return ~(--x); } il int read(char *s){ int len=0; register char ch=getchar(); while(ch==' '||ch=='\n') ch=getchar(); while(ch!=' '&&ch!='\n'&&ch!=EOF) s[++len]=ch,ch=getchar(); return len; } il void write(const ll &x){if(x>9) write(x/10);putchar(x%10+'0');} il void print(const ll &x) {x<0?putchar('-'),write(~(x-1)):write(x);putchar('\n');} il ll max(const ll &a,const ll &b){return a>b?a:b;} il ll min(const ll &a,const ll &b){return a<b?a:b;}ll n; string c; const ll mod =1e9+7; il ll ksm(ll d,ll t){ ll res=1; for(;t;t>>=1,d=d*d%mod) if(t&1) res=res*d%mod; return res; } struct hmap{ static const int P=2e6+3,M=3e6+10; int hed[M],nxt[M],cnt;ll val[M];ll as[M]; bool count(ll x){ int c=hed[x%P]; while(c){ if(val[c]==x) return 1; c=nxt[c]; }return 0; } ll& operator [](ll x){ int c=hed[x%P]; while(c){ if(val[c]==x) return as[c]; c=nxt[c]; } ++cnt;val[cnt]=x;nxt[cnt]=hed[x%P];hed[x%P]=cnt; return as[cnt]; } int size(){return cnt;} }f; ll get_f(ll tmp){ if(f.count(tmp)) return f[tmp]%mod; if(tmp&1) return f[tmp]=((get_f(tmp/2+1)*get_f(tmp/2+1)%mod)+(get_f(tmp/2)*get_f(tmp/2)%mod))%mod; else return f[tmp]=get_f(tmp/2)*((get_f(tmp/2+1)+get_f(tmp/2-1))%mod)%mod; } int main(){ n=read(); c=""; string t; t=getchar(),c=c+t,getchar(); t=getchar(),c=c+t,getchar(); t=getchar(),c=c+t,getchar(); t=getchar(),c=c+t,getchar(); if(n==2||c=="AAAA"||c=="AAAB"||c=="AABA"||c=="AABB"||c=="ABAB"||c=="ABBB"||c=="BBAB"||c=="BBBB"){ print(1); return 0; } else if(c=="ABAA"||c=="BABA"||c=="BABB"||c=="BBAA"){ print(ksm(2,n-3)); return 0; } else if(c=="BAAA"||c=="ABBA"||c=="BBBA"||c=="BAAB"){ f[1]=f[2]=1; print(get_f(n-1)); return 0; } return 0; } /* 1000 B A A A 589888339 */
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;} const char newl = '\n'; bool b[10000]; int main() { int n; cin >> n; vector<P> a(n); for (int i = 0;i < n;++i) cin >> a[i].first; for (int i = 0;i < n;++i) a[i].second = i; sort(a.begin(),a.end()); reverse(a.begin(),a.end()); for (int i = 0;i < n;++i) b[i] = false; int ans = 0; for (int i = 0;i < n;++i) { int l = a[i].second,r = l; b[l] = true; while (l >= 0 && b[l]) l--; while (r < n && b[r]) r++; ans = max(ans,a[i].first*(r-l-1)); } cout << ans << newl; }
#include<iostream> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<unordered_set> #include<unordered_map> #include<map> #include<set> #include<random> #include<algorithm> #include<queue> #include<stack> #include<deque> #include<bitset> #include<cstdio> #include<cassert> #include<sstream> #include<set> #define int long long int using namespace std; signed main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; int ans = 0; for (int l = 0; l < n; ++l) { int minEl = 1e15; for (int r = l; r < n; ++r) { minEl = min(minEl, a[r]); ans = max(ans, (r - l + 1) * minEl); } } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long N; cin >> N; vector<long long> L(N),R(N); for(int i=0;i<N;i++) cin >> L[i]; for(int i=N-1;i>=0;i--) cin >> R[i]; long long ans = 0; vector<long long> V1(N),V2(N); for(int i=0;i<N;i++){ V1[i] = max(L[i],R[i]); V2[i] = min(L[i],R[i]); ans += V1[i]; } priority_queue<long long> P; P.push(V2[0]); for(int i=1;i<N;i++){ long long x = P.top(); if(V1[i]<x){ ans += x - V1[i]; P.pop(); P.push(V1[i]); } P.push(V2[i]); } cout << ans << endl; return 0; }
#include <iostream> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #include <vector> using namespace std; int main() { long long N; cin >> N; vector<long long> V(2 * N); for (int i = 0; i < 2 * N; i++) cin >> V[i]; multiset<long long> Set; long long sum = 0; for (int i = 0; i < N; i++) { Set.insert(V[N - i - 1]); Set.insert(V[N + i]); long long a = *Set.begin(); Set.erase(Set.begin()); sum += a; } long long sum_ = 0; for (int i = 0; i < 2 * N; i++) sum_ += V[i]; cout << sum_ - sum << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define rep(i, a, b) for (int i = a; i <= b; i++) #define rev_rep(i, a, b) for (int i = a; i >= b; i--) #define vi vector<int> #define pb push_back #define pi pair<int, int> #define mod = 1e9 + 7 void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifndef ONLINE_JUDGE #define debug(x...) \ cerr << "[" << #x << "] = ["; \ _print(x) #else #define debug(x...) #endif /* ************************************ CODE STATRTS HERE *************************************/ void solve() { int n; cin >> n; vi v(n); ll sum = 0; rep(i, 0, n - 1) { cin >> v[i]; sum += max(0,v[i]-10); } cout<<sum<<endl; } int main() { int t = 1; // cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<(int)(n);i++) typedef long long ll; int main() { ll N; cin >> N; bitset<200001> flag; ll Ans=0; REP(i,N){ ll p; cin >> p; flag.set(p,1); while(flag.test(Ans)==true){ Ans++; } cout << Ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int m, n; int main() { cin >> m >> n; cout << (n % m ? "No" : "Yes"); return 0; }
#include <bits/stdc++.h> #include <time.h> #include <math.h> using namespace std; // English Motherfucker, Do you speak it? #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); #define nl "\n" #define l long #define ll long long #define dou double #define ld long double #define str string #define vi vector<int> #define vc vector<char> #define vll vector<ll> #define vd vector<dou> #define vstr vector<str> #define si stack<int> #define sc stack<char> #define sll stack<ll> #define sd stack<dou> #define sstr stack<str> #define qi queue<int> #define qc queue<char> #define qll queue<ll> #define qd queue<dou> #define qstr queue<str> #define sei set<int> #define sec set<char> #define sell set<ll> #define sed set<dou> #define sestr set<str> #define msi multiset<int> #define msc multiset<char> #define msll multiset<ll> #define msd multiset<dou> #define msstr multiset<str> #define mii map<int, int> #define mcc map<char, char> #define mllll map<ll, ll> #define mdd map<dou, dou> #define mstrstr map<str, str> #define pii pair<int, int> #define pcc pair<char, char> #define pllll pair<ll, ll> #define pdd pair<dou, dou> #define pstrstr pair<str, str> #define pub push_back #define pob pop_back #define mp make_pair #define min3(a, b, c) min(min(a, b), c) #define max3(a, b, c) max(max(a, b), c) const ll MOD9 = 1000000007; const dou pi = acos(-1.0); const dou PI = 3.1415926535897932384626433832795; const dou EX = 2.7182818284590452353602875; const int INFI = 1e9; ll maxs(set<ll> s) { ll a; a=*(--s.end()); /*for(auto i=s.begin();i!=s.end();++i) { if((*i)>a) { a=(*i); } }*/ //cout<<a<<" "; return a; } ll mexs(set<ll> s) { ll a; a=0; /*while(1) { if(s.find(a)!=s.end()) { a++; } else { break; } }*/ if(*(s.begin())!=0) { a=0; } else if(maxs(s)==(s.size()-1)) { a=maxs(s)+1; } else { ll j=0; for(auto i=s.begin();i!=s.end();i++) { if(*(i)==j) { j++; } else { a=j; break; } } } return a; } void solve() { int m,h; cin>>m>>h; if(h%m==0) { cout<<"Yes"<<nl; } else { cout<<"No"<<nl; } } int main() { fast; ll t; //cin>>t; t=1; while (t--) { solve(); } return 0; }
#include <iostream> #include <vector> using namespace std; vector<vector<int>> tile(50,vector<int>(50)); vector<vector<int>> pile(50,vector<int>(50)); vector<vector<char>> board(50,vector<char>(50,'-')); string route; long long score; void print(vector<vector<int>> t){ for (int i=0;i<50;i++){ for (int j=0;j<50;j++){ if(j==50-1) cout << t[i][j] << endl; else cout << t[i][j] ; } } } void write(int dx ,int dy){ if(dx == -1 && dy == 0) route.push_back('U'); else if(dx ==1 && dy == 0) route.push_back('D'); else if(dx == 0 && dy == -1) route.push_back('L'); else route.push_back('R'); } void check_same_tile(int x,int y){ for(int dx=-1;dx<=1;dx++){ for(int dy=-1;dy<=1;dy++){ if(dx!=dy && dx != -dy){ if(0<=x+dx && x+dx <50 && 0 <= y+dy && y+dy < 50 && tile[x][y] == tile[x+dx][y+dy]){ board[x+dx][y+dy] ='*'; } } } } } bool solve(int x,int y){ score += pile[x][y]; check_same_tile(x,y); // print(tile); for(int dx=-1;dx<=1;dx++){ for(int dy=-1;dy<=1;dy++){ if(dx!=dy && dx != -dy){ if(0<=x+dx && x+dx <50 && 0 <= y+dy && y+dy < 50 && tile[x][y]!=tile[x+dx][y+dy] && board[x+dx][y+dy] != '*'){ board[x+dx][y+dy] ='*'; write(dx,dy); return solve(x+dx,y+dy); } } } } return false; } int main(){ int si,sj; cin >> si >> sj; for (int i=0;i<50;i++){ for (int j=0;j<50;j++){ cin >> tile[i][j]; } } for (int i=0;i<50;i++){ for (int j=0;j<50;j++){ cin >> pile[i][j]; } } solve(si,sj); cout << route << endl; }
#include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cctype> using namespace std; template <typename T> inline void read(T&x){ x=0; char temp=getchar(); bool f=false; while(!isdigit(temp)){if(temp=='-') f=true; temp=getchar();} while(isdigit(temp)){x=(x<<1)+(x<<3)+temp-'0'; temp=getchar();} if(f) x=-x; } template <typename T> void print(T x){ if(x<0) putchar('-'),x=-x; if(x>9) print(x/10); putchar(x%10+'0'); } typedef long long ll; const int MAXN = 1e6+5; //basic int n; ll k; //DP ll f[4][MAXN*3],prefix[4][MAXN*3]; //f[i][j]=\sigma k=1->n f[i-1][j-k]; int main(){ read(n),read(k); f[0][0]=1; for(register int i=0;i<=n;i++) prefix[0][i]=1; for(register int i=1;i<=3;i++){ for(register int j=i;j<=i*n;j++){ int temp=j-min(n,j-i+1)-1; f[i][j]=prefix[i-1][j-1]-(temp>0? prefix[i-1][temp]:0); } for(register int j=1;j<=3*n;j++) prefix[i][j]=prefix[i][j-1]+f[i][j]; } int sum=3; while(f[3][sum]<k) k-=f[3][sum],sum++; for(register int i=1;i<=n;i++){ int l=max(sum-i-n,1),r=min(sum-i-1,n); if(l>r) continue; if(k>r-l+1){k-=r-l+1; continue;} print(i),putchar(' '),print(l+k-1),putchar(' '),print(sum-i-(l+k-1)); break; } return 0; }
#include<iostream> #include<stdio.h> #include<iomanip> #include<vector> #include<string> #include<string.h> #include<queue> #include<math.h> #include<utility> #include<stack> #include<unordered_map> #include<map> #include<set> #include<unordered_set> #include<algorithm> #include<sstream> #include<stdlib.h> #include<bitset> #define ff first #define ss second #define mod 1000000007LL #define PI 3.14159265 using namespace std; typedef long long int ll; typedef unsigned long long int ull; void func(vector<pair<int,int>>&p,vector<pair<int,int>>&c,int k,vector<int>&s,int &mx) { if(k==p.size()) { int a=0; for(int i=0;i<c.size();i++) { if(s[c[i].ff]>=1&&s[c[i].ss]>=1) a++; } if(a>mx) mx=a; } else { s[p[k].ff]++; func(p,c,k+1,s,mx); s[p[k].ss]++; s[p[k].ff]--; func(p,c,k+1,s,mx); s[p[k].ss]--; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin>>n>>m; vector<pair<int,int>>c(m); for(int i=0;i<m;i++) cin>>c[i].ff>>c[i].ss; int k; cin>>k; vector<pair<int,int>>p(k); for(int i=0;i<k;i++) cin>>p[i].ff>>p[i].ss; vector<int>s(n+1,0); int mx=0; func(p,c,0,s,mx); cout<<mx; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long ll; int main() { int H, W, N, M; bool l[1600][1600], f[1600][1600], r[1600][1600]; cin >> H >> W >> N >> M; for (int h = 0; h < H; ++h){ for (int w = 0; w < W; ++w){ l[h][w] = false; f[h][w] = false; r[h][w] = false; } } for (int i = 0; i < N; ++i){ int a, b; cin >> a >> b; l[a-1][b-1] = true; } for (int i = 0; i < M; ++i){ int c, d; cin >> c >> d; f[c-1][d-1] = true; } for (int h = 0; h < H; ++h){ bool light = false; for (int w = 0; w < W; ++w){ if (l[h][w]) { light = true; }else if(f[h][w]){ light = false; } r[h][w] |= light; } } for (int h = 0; h < H; ++h){ bool light = false; for (int w = W-1; w >= 0; --w){ if (l[h][w]) { light = true; }else if(f[h][w]){ light = false; } r[h][w] |= light; } } for (int w = 0; w < W; ++w){ bool light = false; for (int h = 0; h < H; ++h){ if (l[h][w]) { light = true; }else if(f[h][w]){ light = false; } r[h][w] |= light; } } for (int w = 0; w < W; ++w){ bool light = false; for (int h = H-1; h >= 0; --h){ if (l[h][w]) { light = true; }else if(f[h][w]){ light = false; } r[h][w] |= light; } } int res = 0; for (int h = 0; h < H; ++h){ for (int w = 0; w < W; ++w){ if (r[h][w]) res++; } } cout << res << endl; return 0; }
/* Sunday 16 May 2021 05:35:27 PM IST @uthor::astrainL3gi0N */ #include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef std::vector<int> vi; typedef std::pair<int,int> ii; typedef std::vector<ii> vii; typedef std::vector<ll> vl; #define pb push_back #define mp make_pair #define debg(x) std::cerr<<(#x)<<" => "<<x<<'\n'; #define debgg(x,y) std::cerr<<(#x)<<" => "<<x<<'\t'<<(#y)<<' '<<y<<'\n'; #define len(a) (int)(a).size() #define all(x) x.begin(),x.end() const int mod = 1000000007; const int modd = 998244353; const int esc = -1; const string ys = "Yes\n"; const string no = "No\n"; bool comp (int x, int y) { return x > y; } void printarr (int arr[], int n) { for(int i = 0; i < n; ++i) cout<<arr[i]<<(i<n-1?' ':'\n'); } template < typename T> void printv (T &a) { for (auto it = a.begin(); it != a.end(); ++it) cout<<*it<<' '; cout<<'\n'; } int gint() { int n; cin>>n; return n; } //int t[510][510]; ll ans; void solve () { map <int,int> freq; set <int> hsh; ans = 0; bool ok = false; ll n; cin>>n; ll a,b,c; ll num = n; ll res = 1; int cnt = 0; ans = n; while (num > 0) { cnt++; res *= 2; a = n/res; b = cnt; c = n - res*a; ans = min(ans,a+b+c); num /= 2; } cout<<ans; cout<<'\n'; } void sol () { //test } int main () { //cout << setprecision(15) << fixed; int testcases = 1; for (int t = 0; t < testcases; ++t) { //cout<<"Case #"<<t+1<<": "; solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int>; #define rep(i, n) for(int i = 0; i < n; i++) int main() { ll n; cin >> n; ll m = n; ll b2 = 1; ll ans = 1e18+1; for(int b = 0;b2<=n;b++){ ll a = n/b2; ll c = n%b2; ans = min(ans,a+b+c); b2 *= 2; } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=4e3+9; const int MAX=1e2+9; const double ep=1e-10; const int mod=1e9+7; const int INF=0x3f3f3f3f; const double inf=1e20; const double pi=acos(-1); #define debug1 puts("?"); #define debug(x) cout<<"##"<<(x)<<endl; #define mk make_pair #define PII pair<int,int> #define PDI pair<double,int> #define PIII pair<int,PII > #define PIII1 pair<PII,int> #define PIIII pair<PII,PII > #define PIL pair<int,ll> #define PLI pair<ll,int> #define PLIII pair<PLI,PII > #define PLL pair<ll,ll> #define fi first #define se second #define pb push_back #define eb emplace_back #define rep(i,a,b) for(int i=(a);i<=(b);++i) #define dep(i,a,b) for(int i=(a);i>=(b);--i) #define sd(x) scanf("%d",&(x)) #define slld(x) scanf("%lld",&(x)) #define sdd(x,y) scanf("%d%d",&(x),&(y)) #define sc(s) scanf("%s",(s)) #define pd(x) printf("%d\n",(x)) #define plld(x) printf("%lld\n",(x)) #define pdk(x) printf("%d ",(x)) #define plldk(x) printf("%lld ",(x)) #define pdd(x,y) printf("%d %d\n",(x),(y)) #define PR(x) printf("Case %d: ",(x)) /* */ ll dp[maxn][maxn],u[maxn],l[maxn],d[maxn]; int n,m; char s[maxn][maxn]; void solve() { sdd(n,m); rep(i,1,n)sc(s[i]+1); u[1]=l[1]=d[1]=dp[1][1]=1; rep(i,1,n) { rep(j,1,m) { if(i==1&&j==1)continue; int dd; if(j>=i)dd=j-i+1; else dd=i-j+1+m; if(s[i][j]=='#') { u[j]=l[i]=d[dd]=0; continue; } dp[i][j]=(l[i]+u[j]+d[dd])%mod; l[i]=(l[i]+dp[i][j])%mod; u[j]=(u[j]+dp[i][j])%mod; d[dd]=(d[dd]+dp[i][j])%mod; // cout<<i<<' '<<j<<' '<<l[i]<<' '<<u[j]<<' '<<d[dd]<<dp[i][j]<<endl; } } plld(dp[n][m]); } int main() { // ios::sync_with_stdio(false); // cin.tie(0); int T=1; // sd(T); while(T--)solve(); }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author Pine1999 */ #include <bits/stdc++.h> using namespace std; template<int mod> class Modular { public: int x; Modular() : x(0) {} Modular(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} Modular &operator+=(const Modular& p) { if ((x += p.x) >= mod) x -= mod; return *this; } Modular &operator-=(const Modular& p) { if ((x -= p.x) < 0) x += mod; return *this; } Modular &operator*=(const Modular& p) { x = (int) (1LL * x * p.x % mod); return *this; } Modular &operator/=(const Modular& p) { *this *= p.Inverse(); return *this; } Modular operator-() const { return Modular(-x); } Modular operator+(const Modular& p) { return Modular(*this) += p; } Modular operator-(const Modular& p) { return Modular(*this) -= p; } Modular operator*(const Modular& p) { return Modular(*this) *= p; } Modular operator/(const Modular& p) { return Modular(*this) /= p; } bool operator==(const Modular& p) { return x == p.x; } bool operator!=(const Modular& p) { return x != p.x; } friend std::ostream& operator<<(std::ostream& out, const Modular& p) { return out << p.x; } friend std::istream& operator>>(std::istream& in, Modular& a) { long long t; in >> t; a = Modular<mod>(t); return in; } Modular Inverse() const { int a = x, m = mod, u = 1, v = 0, t; while (m > 0) { t = a / m; std::swap(a -= t * m, m); std::swap(u -= t * v, v); } return Modular(u); } Modular Pow(long long a, long long b) { assert(b >= 0); Modular r(1), mul(a); while (b > 0) { if (b & 1) r *= mul; mul *= mul; b >>= 1; } return r; } }; // 入力時に mod を受け取る場合は, template<int& mod> にして, // static int mod; cin >> mod; using mint = Modular<mod>; で良い. const int mod = (int) 1e9 + 7; using mint = Modular<mod>; void solve(std::istream& in, std::ostream& out) { int h, w; in >> h >> w; vector<string> s(h); for (int i = 0; i < h; ++i) in >> s[i]; vector<vector<mint>> sum1(h, vector<mint> (w)), sum2(h, vector<mint> (w)), sum3(h, vector<mint> (w)); vector<vector<mint>> dp(h, vector<mint> (w)); sum1[0][0] = 1; sum2[0][0] = 1; sum3[0][0] = 1; dp[0][0] = 1; for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (s[i][j] == '#') { sum1[i][j] = 0; sum2[i][j] = 0; sum3[i][j] = 0; dp[i][j] = 0; } else { if (i > 0) { dp[i][j] += sum1[i - 1][j]; } if (j > 0) { dp[i][j] += sum2[i][j - 1]; } if (i > 0 && j > 0) { dp[i][j] += sum3[i - 1][j - 1]; } sum1[i][j] = dp[i][j]; sum2[i][j] = dp[i][j]; sum3[i][j] = dp[i][j]; if (i > 0) { sum1[i][j] += sum1[i - 1][j]; } if (j > 0) { sum2[i][j] += sum2[i][j - 1]; } if (i > 0 && j > 0) { sum3[i][j] += sum3[i - 1][j - 1]; } } } } out << dp[h - 1][w - 1] << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); std::istream& in(std::cin); std::ostream& out(std::cout); solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(),(x).end() template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } template<class T> inline bool chmax(T &a, T b){ if(a<b){ a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b){ if(a>b){ a = b; return true; } return false; } int n; vector<int> a; int res = (1<<30); void dfs(int id,int X,int Y){ if(id==n){ chmin(res,X^Y); return; } X|=a[id]; dfs(id+1,0,X^Y); dfs(id+1,X,Y); } int main(){ cin >> n; a.resize(n); rep(i,n) cin >> a[i]; dfs(0,0,0); cout << res << endl; return 0; }
#include<bits/stdc++.h> #include<math.h> using namespace std; /*------------------------------------------------------------------*/ #define ll long long int #define mp make_pair #define pb push_back #define MOD 998244353 bool isvowel(char c) { if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') return true; return false; } ll isprime(ll n) { ll ans=1; for(ll i=2;i<=sqrt(n);i++) { if(n%i==0) return 0; } return 1; } ll power(ll a,ll b,ll m) { if(b==0) return 1; if(b==1) return a%m; ll temp=power(a,b/2,m); if(b%2==0) return ((temp%m)*(temp%m))%m; else return ((((temp%m)*(temp%m))%m)*a%m)%m; } const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; void ncrinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long ncr(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll c(ll n, ll k) { ll i, j; ll C[n + 1][k + 1]; for (i = 0; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = (C[i - 1][j - 1]%MOD + C[i - 1][j]%MOD)%MOD; } } return C[n][k]%MOD; } ll modInverse(ll a, ll m) { ll m0 = m; ll y = 0, x = 1; if (m == 1) return 0; while (a > 1) { ll q = a / m; ll t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } ll fact(ll n) { ll ans=1; for(ll i=1;i<=n;i++) { ans = (ans%MOD * i%MOD)%MOD; } return ans; } /*-----------------------------------------------------------------------------*/ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen("1.txt","rt",stdin); //freopen("output.txt","wwt",stdout); ll t; t=1; //cin>>t; //ll t1=0; ll t1=0; while(t--) { ll n; cin>>n; ll a[n]; for(ll i=0;i<n;i++) cin>>a[i]; ll mn=INT_MAX; for(ll i=0;i< 1<<(n-1);i++) { ll or1 = 0; ll xor1 =0; for(ll j=0;j<=n;j++) { if(j<n) or1=or1|a[j]; if(j==n||(i>>j)&1) { xor1=xor1^or1; or1=0; } } mn=min(mn,xor1); } cout<<mn<<endl; } }
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { int n, ai; cin >> n; ll s = 0, s2 = 0; for (int i = 0; i < n; i++) { cin >> ai; s += ai; s2 += ai * ai; } cout << n * s2 - s * s << '\n'; }
// ConsoleApplication1.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。 // #include <iostream> #include<vector> using namespace std; using ll = long long; void solve() { int N; cin >> N; vector<int> A(N); for (size_t i = 0; i < N; i++) { cin >> A[i]; } int s = 0; vector<ll> as(N); ll ans = 0; for (size_t i = 0; i < N; i++) { s += A[i]; as[i] = s; ans +=(ll) A[i] * A[i] * (N - 1); } for (size_t i = 0; i < N; i++) { ans -=(ll) 2 * A[i] * (s-as[i]); } cout << ans << endl; } void solve2() { int N; cin >> N; vector<int> A(N); for (size_t i = 0; i < N; i++) { cin >> A[i]; } ll ans = 0; //Aの-200から+200までの範囲を0から400までの配列のindexに変換 const int MaxA = 200; vector<int> ac(MaxA * 2 + 1, 0); for (size_t i = 0; i < N; i++) { /* TLE for (size_t j = 0; j < i ; j++) { ans += (ull)((A[i] - A[j]) * (A[i] - A[j])); } */ for (int aj = MaxA * -1; aj <= MaxA; aj++) { int c = ac[MaxA + aj]; int d = A[i] - aj; //ans += (ll)(d * d * c); //WA overflow ans += (ll)d * d * c; } ac[MaxA + A[i]]++; } cout << ans << endl; } int main() { solve(); return 0; } // プログラムの実行: Ctrl + F5 または [デバッグ] > [デバッグなしで開始] メニュー // プログラムのデバッグ: F5 または [デバッグ] > [デバッグの開始] メニュー // 作業を開始するためのヒント: // 1. ソリューション エクスプローラー ウィンドウを使用してファイルを追加/管理します // 2. チーム エクスプローラー ウィンドウを使用してソース管理に接続します // 3. 出力ウィンドウを使用して、ビルド出力とその他のメッセージを表示します // 4. エラー一覧ウィンドウを使用してエラーを表示します // 5. [プロジェクト] > [新しい項目の追加] と移動して新しいコード ファイルを作成するか、[プロジェクト] > [既存の項目の追加] と移動して既存のコード ファイルをプロジェクトに追加します // 6. 後ほどこのプロジェクトを再び開く場合、[ファイル] > [開く] > [プロジェクト] と移動して .sln ファイルを選択します
#include <iostream> #include <vector> #include <algorithm> #include <utility> #include <set> #include <map> #include <stack> #include <queue> #include <cstring> #include <math.h> using namespace std; #define int long long const int N = 200005; int h, w, m, seg[4 * N]; vector<int> row[N], col[N]; void upd(int idx, int start, int end, int id) { if (start > end || end < id || start > id) { return; } else if (start == end) { seg[idx] = min(seg[idx] + 1, 1LL); } else { int mid = (start + end) / 2; upd(2 * idx + 1, start, mid, id); upd(2 * idx + 2, mid + 1, end, id); seg[idx] = seg[2 * idx + 1] + seg[2 * idx + 2]; } } int get(int idx, int start, int end, int l, int r) { if (start > end || end < l || start > r) { return 0; } else if (start >= l && end <= r) { return seg[idx]; } else { int mid = (start + end) / 2; int x = get(2 * idx + 1, start, mid, l, r); int y = get(2 * idx + 2, mid + 1, end, l, r); return x + y; } } int32_t main(int32_t argc, char const *argv[]) { cin >> h >> w >> m; for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; row[x].push_back(y); col[y].push_back(x); } for (int i = 0; i < N; ++i) { sort(row[i].begin(), row[i].end()); sort(col[i].begin(), col[i].end()); } long long ans = 0; for (int i = 1; i <= h; ++i) { if (row[i].empty()) { ans += w; } else if (row[i][0] == 1) { break; } else { ans += row[i][0] - 1; } } for (int i = 1; i <= w; ++i) { if (col[i].empty()) { ans += h; } else if (col[i][0] == 1) { break; } else { ans += col[i][0] - 1; } } for (int i = 1; i <= h; ++i) { int f = 1; for (int &y: row[i]) { if (y == 1) { f = 0; break; } int sum = get(0, 1, N - 1, y, y); if (sum == 0) { upd(0, 1, N - 1, y); } } if (f == 0) { break; } int tot, r; if (row[i].empty()) { tot = r = w; } else { tot = r = row[i][0] - 1; } int mn, oc = 0; if (row[1].empty()) { mn = w + 1; } else { mn = row[1][0]; } oc += max(0LL, r - mn); tot = tot - get(0, 1, N - 1, 1, min(mn, r)) - oc; ans -= tot; } cout << ans << '\n'; return 0; }
#include <vector> #include <iostream> #include <algorithm> #include <unordered_map> #include <functional> #include <cmath> using namespace std; using pos = pair<size_t, size_t>; template<typename T, typename F> class SegTree { size_t size; T zero; F f; vector<T> elms; static size_t calcSize(size_t size) { size_t count = size_t(1) << int(ceil(log2(size))); while (count < size) count <<= 1; return count + (count - 1); } static size_t parent(size_t i) { return (i - 1) / 2; } static size_t leftChild(size_t i) { return i * 2 + 1; } static size_t rightChild(size_t i) { return i * 2 + 2; } public: SegTree(size_t size_, T zero_, F f_) : size(size_), zero(zero_), f(f_), elms(calcSize(size_), zero_) {} void SetAt(size_t i, T val) { i = elms.size() / 2 + i; while (elms[i] != val) { elms[i] = val; if (i == 0) break; i = parent(i); val = f(elms[leftChild(i)], elms[rightChild(i)]); } } T GetRangeRight(size_t i) { size_t a = 0, b = elms.size() / 2 + 1; size_t pos = 0; T ret = zero; while (i != a) { size_t mid = (a + b) / 2; if (i < mid) { b = mid; ret = f(ret, elms[rightChild(pos)]); pos = leftChild(pos); } else { a = mid; pos = rightChild(pos); } } return f(ret, elms[pos]); } }; template<typename T, typename F> SegTree<T, F> makeSegTree(size_t size, T zero, F f) { return SegTree<T, F>(size, zero, f); } int main() { size_t h, w, m; cin >> h >> w >> m; size_t lh = h + 1, lw = w + 1; unordered_map<size_t, size_t> xs, ys; for (size_t i = 0; i < m; ++i) { size_t x, y; cin >> x >> y; if (x == 1) { lw = min(lw, y); continue; } if (y == 1) { lh = min(lh, x); continue; } if (x < lh) { if (!xs.emplace(x, y).second) xs[x] = min(xs[x], y); } if (y < lw) { if (!ys.emplace(y, x).second) ys[y] = min(ys[y], x); } } size_t sum = h * w - (h - lh + 2) * (w - lw + 2) + 1; vector<pos> xss, yss; xss.reserve(xs.size()); for (auto [x, y] : xs) { if (x >= lh) continue; if (y >= lw) sum -= w - y + 1; else xss.emplace_back(x, y); } xs.clear(); yss.reserve(ys.size()); for (auto [y, x] : ys) { if (y >= lw) continue; if (x >= lh) sum -= h - x + 1; else yss.emplace_back(x, y); } ys.clear(); sum -= (w - lw + 1) * xss.size(); sum -= (h - lh + 1) * yss.size(); sort(xss.begin(), xss.end()); sort(yss.begin(), yss.end()); auto tree = makeSegTree(w, size_t(), plus<size_t>()); auto last = yss.begin(); for (auto& e : xss) { for (; last != yss.end() && last->first <= e.first; ++last) tree.SetAt(last->second - 1, 1); sum -= tree.GetRangeRight(e.second - 1); } cout << sum << endl; }
#include<bits/stdc++.h> using namespace std; #define M 400005 char dr[M]; int a[M],pd[10],ls[M],fc[M],inv[M]; int C(int n,int m){ if(m==0||m==n)return 1; if(ls[n]!=ls[n-m]+ls[m])return 0; // cout<<fc[n]<<" "<<inv[m]<<"!!"<<inv[n-m]<<endl; return 1ll*fc[n]%3*inv[m]%3*inv[n-m]%3; } int main(){ // freopen("data.in","r",stdin); // freopen("pro.out","w",stdout); int n;pd[1]=1;pd[2]=2; scanf("%d",&n); scanf("%s",dr+1); for(int i=1;i<=n;++i){ if(dr[i]=='R')a[i]=0; else if(dr[i]=='B')a[i]=1; else a[i]=2; } fc[0]=fc[1]=inv[0]=inv[1]=1; for(int i=2;i<=n;++i){ int t=i; while(t%3==0)ls[i]++,t/=3; fc[i]=1ll*t*fc[i-1]%3; inv[i]=1ll*pd[t%3]*inv[i-1]%3; } for(int i=1;i<=n;++i)ls[i]+=ls[i-1]; int ans=0; int f=1; if((n-1)&1)f=-1; for(int i=1;i<=n;++i){ ans=(ans+1ll*f*C(n-1,i-1)*a[i])%3; }ans=(ans%3+3)%3; if(ans==0)puts("R"); else if(ans==1)puts("B"); else puts("W"); }
#pragma GCC optimize ("Ofast") #include<bits/stdc++.h> using namespace std; 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; } } inline void rd(char &c){ int i; for(;;){ i = my_getchar_unlocked(); if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){ break; } } c = i; } inline int rd(char c[]){ int i; int sz = 0; for(;;){ i = my_getchar_unlocked(); if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){ break; } } c[sz++] = i; for(;;){ i = my_getchar_unlocked(); if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){ break; } c[sz++] = i; } c[sz]='\0'; return sz; } 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(const char c[]){ int i=0; for(i=0;c[i]!='\0';i++){ my_putchar_unlocked(c[i]); } } template<class S, class T> inline S moddw_L(S a, const T b){ a %= b; if(a < 0){ a += b; } return a; } int N; char S[400000+2]; int comb(int n, int k){ int res; if(k < 0 || k > n){ return 0; } if(n < 3){ int i; res = 1; for(i=(0);i<(k);i++){ res *= n-i; } for(i=(0);i<(k);i++){ res /= i+1; } return res % 3; } return comb(n/3, k/3) * comb(n%3, k%3); } int main(){ int i; int sm = 0; rd(N); rd(S); for(i=(0);i<(N);i++){ if(S[i]=='B'){ sm += comb(N-1,i); } if(S[i]=='R'){ sm += comb(N-1,i) * 2; } } if(N%2==0){ sm = -sm; } (sm = moddw_L(sm,3)); if(sm==0){ wt_L("W"); wt_L('\n'); } else if(sm==1){ wt_L("B"); wt_L('\n'); } else{ wt_L("R"); wt_L('\n'); } return 0; } // cLay version 20210405-1 // --- original code --- // int N; char S[4d5+2]; // // int comb(int n, int k){ // int res; // if(k < 0 || k > n) return 0; // if(n < 3){ // res = 1; // rep(i,k) res *= n-i; // rep(i,k) res /= i+1; // return res % 3; // } // return comb(n/3, k/3) * comb(n%3, k%3); // } // // { // int sm = 0; // rd(N,S); // rep(i,N){ // if(S[i]=='B') sm += comb(N-1,i); // if(S[i]=='R') sm += comb(N-1,i) * 2; // } // if(N%2==0) sm = -sm; // sm %%= 3; // wt(if[sm==0, "W", sm==1, "B", "R"]); // }
#include <bits/stdc++.h> #include <iostream> #include <stdio.h> #include <bitset> #include <vector> #include <algorithm> #include <string> #include <random> #include <unordered_set> #include <iterator> // 20210422 // ABC 191以前のを解いていく using namespace std; using ll = long long; void digi() { int H, W; cin >> H >> W; vector<string> input_(H+1); vector<vector<string>> S(H+1, vector<string>(W+1, ".")); vector<int> count_(H+1); for (int i=0 ; i < H; i++) { cin >> input_.at(i); } for (int i=0; i< H; i++) { for (int j=0; j<W; j++) { S.at(i).at(j) = input_.at(i).at(j); } } // 以下解説を読んで実装する // 周囲4マスに注目して頂点ならば自身が#.が3つであることと同値 // 2マスならば地続きなので頂点ではないことを考える int tyouten = 0; for (int i = 0; i < H-1 ;i++) { for (int j = 0; j < W-1; j++) { int coun = 0; if (S.at(i).at(j)=="#") { coun += 1; } if (S.at(i+1).at(j)=="#") { coun += 1; } if (S.at(i).at(j+1)=="#") { coun += 1; } if (S.at(i+1).at(j+1)=="#") { coun += 1; } /* #. .# .. .. .. ..  #. .# # ga 1ko .# #. ## ## ## ##  .# #. # ga 3ko のとき中心の点が頂点であるという */ if (coun ==1 || coun == 3) { tyouten += 1; } } } cout << tyouten <<endl; } int main() { digi(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define vll vector<ll> #define pll pair<ll,ll> #define vpll vector<pll> #define ub upper_bound #define lb lower_bound #define all(v) ((v).begin()),((v).end()) #define allr(v) ((v).rbegin()),((v).rend()) #define ff first #define ss second #define mp make_pair #define pll pair<ll,ll> #define fo(i,n) for(ll i=0;i<n;i++) #define foa(i,s,e) for(ll i=(s);i<=e;i++) #define fod(i,s,e) for(ll i= (s);i>=(e);i--) #define max3(a,b,c) max(max(a,b),c) #define min3(a,b,c) min(min(a,b),c) #define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n' #define sz(x) (ll)(x.size()) #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" const ld pi = 3.14159265358979323846; //ll MOD = 1e9 + 7; ll MOD = 998244353; const char nl = '\n'; const ll inf = 1e15; #define fill(a,b) memset(a, b, sizeof(a)) #define setbits(x) __builtin_popcountll(x) #define print2d(dp,n,m) fo(i, n){fo(j, m)cout<<dp[i][j]<<" ";cout<<"\n";} ll nxt(){ll x; cin >> x; return x;} ll mul(ll x,ll y){ return (1ll* (x%MOD)*(y%MOD)); } ll modpow(ll x,ll y){ll z=1;while(y>0){if(y%2)z=mul(z,x);x =mul(x,x) ;y/=2;}return z;} ll power(ll x,ll y){ll z=1;while(y>0){if(y%2)z=z*x;x =x*x ;y/=2;}return z;} ll gcd(ll a,ll b){if(a<b) return gcd(b,a);if(b==0) return a;return gcd(b,a%b);} ll min(ll a,ll b){if(a<b)return a;return b;} ll max(ll a,ll b){if(a>b)return a;return b;} ll sq(ll a){ ll ans = (1ll*a*a); return ans; } void solve(){ ll n; cin >> n; ll a[n]; fo(i, n){ cin >> a[i]; } ll s = 0; fo(i, n){ ll x; cin >> x; s += x * a[i]; //cout << s << nl; } if(s == 0){ Yes; } else No; } int main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); #ifndef ONLINE_JUDGE freopen("inputf.txt" , "r" , stdin) ; freopen("outputf.txt" , "w" , stdout) ; freopen("error.txt" , "w" , stderr) ; #endif ll TC = 1; //cin>>TC; fo(TT, TC){ // Google,FB cout << "Case #"<< TT + 1 << ": "; solve(); } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
#include <iostream> #include <string> #include <vector> // true : l winner, false : r winner bool winnerRPS(char l, char r) { return l == r || l == 'R' && r == 'S' || l == 'P' && r == 'R' || l == 'S' && r == 'P'; } int main() { int n, k; std::cin >> n >> k; std::string s; std::cin >> s; for (int i = 0; i < k; i++) { std::string stmp = s; if (s.length() % 2 == 1) { stmp += s; } s.clear(); for (int i = 0; i < stmp.size(); i+=2) { char winnerhand = winnerRPS(stmp[i], stmp[i + 1]) ? stmp[i] : stmp[i + 1]; s.push_back(winnerhand); } } std::cout << s[0] << std::endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n,k; scanf("%d%d",&n,&k); string now,tmp; cin>>now; while(k--) { now=now+now;tmp.clear(); for(int i=0;i<now.size();i+=2) { if(now[i]=='R'&&now[i+1]=='P')tmp.push_back('P'); else if(now[i]=='P'&&now[i+1]=='R')tmp.push_back('P'); else if(now[i]=='R'&&now[i+1]=='S')tmp.push_back('R'); else if(now[i]=='S'&&now[i+1]=='R')tmp.push_back('R'); else if(now[i]=='S'&&now[i+1]=='P')tmp.push_back('S'); else if(now[i]=='P'&&now[i+1]=='S')tmp.push_back('S'); else tmp.push_back(now[i]); } swap(tmp,now); } cout<<now[0]; return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second #define pb push_back #define debug(val) cerr << "The value of " << #val << " is = " << val << '\n'; typedef long double ld; typedef long long ll; typedef unsigned long long ull; const ld PI = 4*atan((ld)1); const ll mod = 998244353; const ll inf = 922337203685477; const ll nax = 5005; ll fac[nax], iv[nax]; ll pangkat(ll x, ll y){ if(y == 0) return 1; ll temp = pangkat(x, y / 2); if(y & 1){ return temp * temp % mod * x % mod; } else{ return temp * temp % mod; } } void init(){ fac[0] = 1; for(ll i = 1; i < nax; i++){ fac[i] = fac[i-1] * i % mod; } iv[nax-1] = pangkat(fac[nax-1], mod - 2); for(ll i = nax - 2; i >= 0; i--){ iv[i] = iv[i+1] * (i + 1) % mod; } } ll c(ll x, ll y){ return fac[x] * iv[y] % mod * iv[x-y] % mod; } ll n, m; ll dp[15][nax]; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); init(); cin >> n >> m; dp[0][0] = 1; for(ll mask = 1; mask <= 13; mask++){ ll bit = (1 << (mask - 1)); for(ll i = 0; i <= m; i++){ for(ll j = 0; j <= n && i + j * bit <= m; j += 2){ dp[mask][i+j*bit] += dp[mask-1][i] * c(n, j); dp[mask][i+j*bit] %= mod; } } } cout << dp[13][m] << '\n'; }
#include<vector> #include<iostream> #include<fstream> #include<string> #include<cassert> #include<algorithm> #include<random> #include<map> #include<set> #include <bitset> using namespace std; //int mod = 998244353; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<vector<int>> vvi; typedef vector<vector<char>> vvc; typedef vector<vector<ll>> vvl; typedef vector<pair<ll, ll>> vpll; typedef vector<pair<ld, ld>> vpld; typedef vector<pair<int, int>> vpi; typedef pair<ld, ld> pld; #define mp make_pair #define pb push_back int mod = 998244353; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { ll c = a * 1LL * b; return c % mod; } int ibit(int n, int i) { return ((n >> i) & 1); } void outp1(vi &ou, string s) { cout << s << endl; for (int i = 0; i < ou.size(); i++) { cout << ou[i] << ' '; } cout << endl; } void outp2(vvi &ou, string s) { cout << s << endl; for (int i = 0; i < ou.size(); i++) { for (int j = 0; j < ou[i].size(); j++) { cout << ou[i][j] << ' '; } cout << '\n'; } } int bp(int x, int y) { if (y == 0) { return 1; } int a = 0; if (!(y % 2)) { a = bp(x, y / 2); } return (y % 2) ? mlt(bp(x, y - 1), x) : mlt(a, a); } int obr(int x) { return bp(x, mod - 2); } const int maxn = 312; int fact[maxn], ofact[maxn]; void prec() { fact[0] = 1; ofact[0] = 1; for (int i = 1; i < maxn; i++) { fact[i] = mlt(fact[i - 1], i); } //cerr << "sdsds" << endl; ofact[maxn - 1] = obr(fact[maxn - 1]); for (int i = maxn - 2; i > 0; i--) { ofact[i] = mlt(ofact[i + 1], i + 1); } } int c(int a, int b) { return ((a <= b) && (a >= 0)) ? mlt(fact[b], mlt(ofact[a], ofact[b - a])) : 0; } void solve(istream &cin = std::cin, ostream &cout = std::cout) { int n, k; cin>>n>>k; vector<int>pows(k+2); for(int i=0;i<n; i++) { int d; cin>>d; int val=1; for(int i=0; i<pows.size(); i++) { pows[i]=sum(val, pows[i]); val=mlt(val, d); } } //outp1(pows, " "); for(int i=1; i<=k; i++) { int td=0; for(int j=0; j<=i; j++) { int an=1; an=mlt(an, c(j, i)); an=mlt(an, pows[i-j]); an=mlt(an, pows[j]); td=sum(td, an); } int bn=pows[i]; bn=mlt(bn, bp(2,i)); td=dif(td, bn); cout<<mlt(td, bp(2, mod-2))<<endl; } } int main() { prec(); solve(); int n; cin >> n; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<n; i++) typedef pair<int,int> P; typedef long long ll; const int INF = 1001001001; const ll INFL = 1e17; const int MOD = 1e+9+7; int main(){ int n; cin >> n; vector<vector<int>> arr(n,vector<int>(5)); for(int i = 0; i < n; i++){ for(int j = 0; j < 5; j++){ cin >> arr[i][j]; } } int left = 0, right = 1e9+1; while(right - left > 1){ int mid = (left + right) / 2; bool flg = false; set<int> st; for(int i = 0; i < n; i++){ int bit = 0; for(int j = 0; j < 5; j++){ if(arr[i][j] >= mid) bit += (1<<j); } st.insert(bit); } for(int i : st){ for(int j : st){ for(int k : st){ if((i | j | k) == (1<<5)-1){ flg = true; break; } } } } if(flg) left = mid; else right = mid; } cout << left << endl; return 0; }
// 200c.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。 // #include <iostream> #include <vector> #include <set> using namespace std; void solve() { int N; cin >> N; int m = 5; vector<vector<int>> A(N, vector<int>(m, 0)); for (auto& l : A)for (auto& op : l) cin >> op; auto check = [&](int x)-> bool { set<int> z; for (auto& l : A) { int bit = 0; for (auto& op : l) { bit <<= 1; if (x <= op) { bit += 1; } } z.insert(bit); } int allok = (1 << m) - 1; for(auto& s1 : z)for(auto& s2 :z)for(auto& s3 :z){ int sp = s1 | s2 | s3; if (sp == allok) { return true; } } return false; }; int l, r; l = 0; r = 1e9 + 1; bool jg; while ((r -l) > 1 ) { int m = (r + l) / 2; jg = check(m); if (jg) { l = m; } else r = m; } cout << l << endl; } int main() { solve(); return 0; } // プログラムの実行: Ctrl + F5 または [デバッグ] > [デバッグなしで開始] メニュー // プログラムのデバッグ: F5 または [デバッグ] > [デバッグの開始] メニュー // 作業を開始するためのヒント: // 1. ソリューション エクスプローラー ウィンドウを使用してファイルを追加/管理します // 2. チーム エクスプローラー ウィンドウを使用してソース管理に接続します // 3. 出力ウィンドウを使用して、ビルド出力とその他のメッセージを表示します // 4. エラー一覧ウィンドウを使用してエラーを表示します // 5. [プロジェクト] > [新しい項目の追加] と移動して新しいコード ファイルを作成するか、[プロジェクト] > [既存の項目の追加] と移動して既存のコード ファイルをプロジェクトに追加します // 6. 後ほどこのプロジェクトを再び開く場合、[ファイル] > [開く] > [プロジェクト] と移動して .sln ファイルを選択します
#include <bits/stdc++.h> using namespace std; #define ld long double #define ll long long #define P pair<int,int> #define fi first #define se second #define rep(i,n) for(int i=0;i<n;i++) #define all(v) v.begin(),v.end() #define pb push_back #define eb emplace_back template<class T>void chmax(T &a,T b){if(a<b)a=b;} template<class T>void chmin(T &a,T b){if(a>b)a=b;} constexpr int INF=1000000000; constexpr ll llINF=1000000000000000000; constexpr int mod=1000000007; constexpr double eps=1e-10; const double pi=acos(-1); int dx[]={0,1,0,-1},dy[]={1,0,-1,0}; int Random(int mi,int ma){ random_device rnd; mt19937 mt(rnd());//32bit //[mi,ma] uniform_int_distribution<int>engine(mi,ma); return engine(mt); } int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b); } int lcm(int a,int b){ return a/gcd(a,b)*b; } bool prime(int a){ if(a==1)return false; for(int i=2;i*i<=a;i++){ if(a%i==0)return false; } return true; } int modpow(int a,int b){ if(b==0)return 1; if(b%2)return modpow(a,b-1)*a%mod; int memo=modpow(a,b/2); return memo*memo%mod; } vector<int>kaijo,invkaijo; void init_fact(int n){ kaijo.resize(n+1); invkaijo.resize(n+1); kaijo[0]=1; for(int i=1;i<=n;i++){ kaijo[i]=kaijo[i-1]*i; kaijo[i]%=mod; } rep(i,n+1)invkaijo[i]=modpow(kaijo[i],mod-2); } int comb(int a,int b){ if(a<b)return 0; if(a<0||b<0)return 0; return kaijo[a]*invkaijo[a-b]%mod*invkaijo[b]%mod; } int inv(int x){ x=modpow(x,mod-2); return x; } int main(){ cin.tie(0);ios::sync_with_stdio(false); ll n; cin>>n; set<ll>st; for(ll i=1;i*i<=n;i++){ if(n%i==0){ st.insert(i); st.insert(n/i); } } for(ll i:st)cout<<i<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int64_t> enum_divisors(int64_t N) { vector<int64_t> res; for (int64_t i = 1; i * i <= N; ++i) { if (N % i == 0) { res.push_back(i); // 重複しないならば i の相方である N/i も push if (N/i != i) res.push_back(N/i); } } // 小さい順に並び替える sort(res.begin(), res.end()); return res; } int main() { int64_t N; cin >> N; const auto &res = enum_divisors(N); for (int i = 0; i < res.size(); ++i) cout << res[i] << endl; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0;i < (n); i++) using ll = long long; using P = pair<int,int>; int main(){ int a,b,x,y; int ans; cin >> a >> b >> x >> y; int less = x; if(x > y) less = y; int trueless = y; if(2*x < y) trueless = 2*x; int lastless = x+y; if(3*x < x+y) lastless = 3*x; if(a < b){ int du = b-a; if(2*x < y){ ans = (du-1)*trueless + lastless; }else{ ans = x + du*y; } }else if(a > b){ int du = a-b; if(2*x < y){ if(x < y){ ans = (du-1)*trueless+x; }else{ ans = (du-1)*trueless-x+y; } }else{ ans = x + (du-1)*y; } }else if(a == b){ ans = x; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c,d; cin>>a>>b>>c>>d; int total=a+b+c+d; if(total%2==1){ cout<<"No"; } else{ total=total/2; if(a+b==total || a+c==total || a+d==total ||a+b+c==total ||a+b+d==total ||a+c+d==total){ cout<<"Yes"; } else{ cout<<"No"; } } }
/* ( _ ) /=> ( +____________________/\/\___ / /| .''._____________'._____ / /|/\ : () : :\ ----\| \ ) '..'______________.'0|----| \ 0_0/____/ \ |---- /----\ || -\\ --| \ || || ||\ \ \\____// '| \ Bang! Bang! .'/ | .:/ | :/_________| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BALJOT SINGH EVERYONE HAS THEIR OWN TIME ~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ #include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define mp make_pair #define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL) using namespace std; typedef long long ll; const ll M = 1e9 + 7; const ll iM = 1e5 + 5; const ll inf = 2e18; //modular arithmetic worst part----------------------------------- ll mod(ll x){ return (x%M+M)%M;} ll mod_min(ll a, ll b){ ll ans= (mod(a)-mod(b)); if(ans<0) ans=mod(ans+M); return ans;} ll mod_mul(ll a,ll b){ return mod(mod(a)*mod(b));} ll mod_add(ll a,ll b){ return mod(mod(a)+mod(b));} ll power(ll x,ll y) { ll res = 1;if (x == 0) return 0;while (y > 0){if (y & 1) res=mod_mul(res,x); y = y>>1; x=mod_mul(x,x);} return res;} ll gcd_extend(ll a, ll b, ll& x, ll& y) {if (b == 0) {x = 1;y = 0;return a;}ll x1, y1;ll d = gcd_extend(b, a % b, x1, y1);x = y1;y = x1 - y1 * (a / b);return d;} ll modinverse(ll value) {ll x, y;ll res=gcd_extend(value,M,x,y);if(res!=1)return -1;return mod(x);} //---------------------------------------------------------------------------------------------------------------------------------- int main() { FAST; ll n; cin>>n; vector<ll> v; for(ll i=1;i*i<=n;i++) if(n%i==0) cout<<i<<"\n",v.push_back(n/i); for (ll i = v.size() - 1; i >= 0; i--) if(v[i]!=sqrt(n)) cout<<v[i]<<"\n"; }
#include <bits/stdc++.h> using namespace std; #define fill(a, b) memset(a, b, sizeof(a)) typedef long long ll; typedef long double lld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; typedef pair<int, int> ii; typedef pair<ll, ll> pll; typedef vector<ii> vii; typedef vector<pll> vll; typedef set<int> si; typedef map<string, int> msi; const int N = 1000010; const ll MOD = 1e9 + 7; void solve() { ll n; cin >> n; set<ll> ans; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { ans.insert(i); ans.insert(n / i); } } for (auto x : ans) cout << x << endl; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // int t; // cin >> t; // for (int i = 0; i < t; i++) { // solve(); // } solve(); }
#include<bits/stdc++.h> #define rep(i, n) for (int i = 0, length = n; i < length; i++) #define fi first #define se second #define lb lower_bound #define ub upper_bound #define ep emplace #define epb emplace_back #define scll static_cast<long long> #define sz(x) static_cast<int>((x).size()) #define pfll(x) printf("%lld\n", x) #define ci(x) cin >> x #define ci2(x, y) cin >> x >> y #define ci3(x, y, z) cin >> x >> y >> z #define ci4(w, x, y, z) cin >> w >> x >> y >> z #define co(x) cout << x << endl #define co2(x, y) cout << x << " " << y << endl #define co3(x, y, z) cout << x << " " << y << " " << z << endl using namespace std; typedef long long ll; typedef pair<int, int> P; typedef priority_queue<int> PQ; typedef priority_queue<int, vector<int>, greater<int>> PQG; typedef priority_queue<P> PQP; typedef priority_queue<P, vector<P>, greater<P>> PQPG; const int MAX_N = 3e5, MOD = 1e9 + 7, INF = 1e9; int n, a[MAX_N]; map<ll, int> m; int main() { ci(n); rep(i, n) ci(a[i]); ll sum = 0; m[0]++; rep(i, n) { sum += i % 2 ? a[i] : -a[i]; m[sum]++; } ll ans = 0; for (auto &i: m) ans += scll(i.se - 1) * i.se / 2; co(ans); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) #define rep2(i,s,n) for (int i = (s); i < (int)(n); i++) #define ALL(a) (a).begin(),(a).end() #define PI 3.14159265358979 using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll mod = 1e9+7LL; int main() { ll N; cin >> N; vector<ll> A(N); rep(i, N) cin >> A[i]; map<ll,ll> m; rep(i, N-1){ A[i+1] -= A[i]; if(i%2==1) A[i] *= -1; m[A[i]]++; } if((N-1)%2==1) A[N-1] *= -1; m[A[N-1]]++; m[0]++; ll ans = 0; for (auto p : m) { auto key = p.first; auto value = p.second; ans += value*(value-1)/2; } cout << ans << endl; }
#include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int main() { vector<long long> a; queue<long long> q; long long n, m = 1; cin >> n; for (int i = 1; i <= n; ++i) m *= 2; n = m; for (int i = 0; i < n; ++i) { cin >> m; a.push_back(m); q.push(m); }long long b, c; while (q.size() != 2) { long long x = q.size(); for (int i = 1; i <= x; i += 2) { b = q.front(); q.pop(); c = q.front(); q.pop(); q.push(max(c, b)); } } b = q.front(); q.pop(); c = q.front(); q.pop(); b = min(b, c); for (int i = 0; i < n; ++i) { if (b == a[i]) { cout << i + 1; return 0; } } return 0; }
#include <bits/stdc++.h> #define ll long long #define pii pair<int,int> #define vec vector using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef LOCAL // freopen("in1.txt", "r", stdin); // freopen("in1.txt", "w", stdout); #endif int n; cin >> n; int m = 1<<n; vec<int> a(m); for (int& x : a) cin >> x; vec<vec<int>> b(2); for (int i=0; i<m; i++) b[0].push_back(i); for (int t=0; t<n-1; t++) { bool p = t&1; bool q = 1-p; sort(b[p].begin(), b[p].end()); for (int i=0; i<b[p].size(); i+=2) { if (a[b[p][i]] < a[b[p][i+1]]) b[q].push_back(b[p][i+1]); else b[q].push_back(b[p][i]); } b[p].clear(); } bool p = (n-1)&1; if (a[b[p][0]] < a[b[p][1]]) cout << b[p][0]+1; else cout << b[p][1]+1; cout << '\n'; }
/*input 2 4 1 2 4 */ #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> in(n*2), out(n*2), c(n*2); bool ok = true; rep(i, n) { int a, b; cin >> a >> b; if (a == 2*n || b == 1) { ok = false; continue; } --a; --b; if (a >= 0) { if (in[a]) ok = false; if (b < 0) in[a] = 1; else in[a] = -1; } if (b >= 0) { if (out[b]) ok = false; if (a < 0) out[b] = 1; else out[b] = -1; } if (a >= 0 && b >= 0) { int nc = b-a; for (int i = a; i <= b; ++i) { if (c[i] != 0 && c[i] != nc) ok = false; c[i] = nc; } } } auto f = [&] { rep(i, n*2-1) { if (in[i] == 1) { int j = i+1; for (; j < n*2; ++j) { if (c[j] > 0) { j = i+c[j]; if (out[j] == 1) ok = false; break; } else if (out[j] == 0 && in[j] == 0) { out[j] = 1; break; } } if (j == n*2) { ok = false; continue; } for (int k = i; k <= j; ++k) { if (c[k] != 0 && c[k] != j-i) ok = false; c[k] = j-i; } } } }; reverse(in.begin(), in.end()); reverse(out.begin(), out.end()); reverse(c.begin(), c.end()); swap(in, out); f(); if (ok) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxN=100; int n; int arr[2*maxN+1]; int type[2*maxN+1]={0}; int main() { // your code goes here int n; cin>>n; memset(arr,-1,sizeof(arr)); int a,b; int rem=0; for(int i=0;i<n;i++) { cin>>a>>b; if(a==-1 && b==-1) { rem++; } else if(a==-1 && type[b]==0) { type[b]=1; } else if(b==-1 && type[a]==0) { type[a]=2; } else if(b>a && type[a]==0 && type[b]==0) { type[a]=3; type[b]=3; arr[a]=b; arr[b]=a; } else { cout<<"No"<<endl; return 0; } } bool dp[2*n+2]; dp[2*n+1]=true; int temp[2*n+1]; for(int i=2*n;i>=1;i--) { dp[i]=false; for(int j=i;j<=2*n;j++) { if(j%2!=i%2 && dp[j+1]) { int mid=(i+j)/2; int len=(j-i+1)/2; for(int k=i;k<=mid;k++) { temp[k]=k+len; temp[k+len]=k; } bool ff=1; for(int k=i;k<=j;k++) { if(type[k]==0) { continue; } else if(type[k]==1) { if(type[temp[k]]!=0 || k<=mid) { ff=0; } } else if(type[k]==2) { if(type[temp[k]]!=0 || k>mid) { ff=0; } } else { if(arr[k]!=temp[k]) { ff=0; } } } dp[i] |=ff; } } } cout<<(dp[1] ? "Yes" : "No")<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int lli; void update(lli index,lli value,lli BIT[],lli n) { for(;index<=n;index+=(index&(-index))) { BIT[index]^=value; } } lli query(lli index,lli BIT[]) { lli sum=0; for(;index>0;index-=(index&(-index))) { sum^=BIT[index]; } return sum; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); //cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("error.txt", "w", stderr); freopen("output.txt", "w", stdout); #endif long long i,j,t=1; //cin>>t; while(t--){ lli n,q; cin>>n>>q; lli a[n+1]; lli BIT[n+1]={0}; for(lli i=1;i<=n;i++) { cin>>a[i]; update(i,a[i],BIT,n); } //for(int i=1;i<=n;i++) //cout<<query(i,BIT)<<endl; while(q--) { lli t,x,y; cin>>t>>x>>y; if(t==1) { update(x,y,BIT,n); } if(t==2) { lli z=query(y,BIT)^query(x-1,BIT); cout<<z<<endl; } } } return 0; }
#include <bits/stdc++.h> #define _overload3(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define ll long long #define lld long double #ifdef DEBUG #define line() cout << "[" << __LINE__ << "] "; #define dump(i) cout << #i ": " << i << " "; #define dumpl(i) cout << #i ": " << i << endl; #else #define line(i) #define dump(i) #define dumpl(i) #endif using namespace std; template <typename Monoid> struct SegmentTree { using F = function<Monoid(Monoid, Monoid)>; int sz; vector<Monoid> seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz *= 2; seg.assign(2 * sz, M1); } void set(int k, const Monoid &x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k++) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid &x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; int f(int a, int b) { return a ^ b; } int main() { int N, Q; cin >> N >> Q; SegmentTree<int> seg(N, f, 0); rep(i, N) { int tmp; cin >> tmp; seg.update(i, tmp); } rep(i, Q) { int t, x, y; cin >> t >> x >> y; if (t == 1) { x--; seg.update(x, seg.query(x, x + 1) ^ y); } else { x--; y--; cout << seg.query(x, y + 1) << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; LL M; map<LL, LL> memo; LL solve(LL N) { if (N == M) return 0; if (N-M<M-(N/2)+1) return abs(N-M); if (memo.count(N)) return memo[N]; if (N < M) return M - N; LL l2 = (N/2)*2; LL r2 = ((N+1)/2)*2; LL res = 1e18; if (N < res) res = N; res = min(res, llabs(l2-N) + 1 + solve(l2/2)); res = min(res, llabs(r2-N) + 1 + solve(r2/2)); memo[N] = res; return res; } int main() { LL N; cin >> M >> N; cout << solve(N) << "\n"; }
#include<iostream> #include<stdio.h> #include<vector> #include<algorithm> #include<list> #include<string.h> #include<math.h> #include<set> #include<string> #include<queue> #include<map> #include<unordered_map> #include<stack> //#include<unordered_set> //#pragma GCC optimize (3) //#define segmenttree true #ifdef segmenttree #define lc (rt<<1) #define rc (lc|1) #define mid ((l+r)>>1) #endif #define reg register using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef pair<int, ll> pil; typedef pair<double, double> pdd; typedef pair<char, char> pcc; typedef pair<ll, bool> plb; const int MAX = 2005; const ll INF = 1e10; const ll MOD = 1e9 + 7; const double PI = 3.1415926535; const double EPS = 1e-5; ll n, m, a, Sum; inline ll pow_mod(ll a, ll b, ll mod){ ll ans = 1; a %= mod; while(b) { if(b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } inline ll inv(ll num, ll mod){ return pow_mod(num, mod - 2, mod) % mod; } ll C(ll x, ll y){ ll temp = 1; for(int i = 1; i <= y; ++i){ temp = temp * (x - i + 1) % MOD; temp = temp * inv(i, MOD) % MOD; } return temp; } int main(){ // ios::sync_with_stdio(false); scanf("%lld %lld", &n, &m); for(int i = 1; i <= n; ++i){ scanf("%lld", &a); Sum += a; } printf("%lld", C(m + n, n + Sum)); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long double ld; int n; ld res; int main() { scanf("%d", &n); for (int i = 1; i <= n - 1; i++) res += ld(n) / (n - i); cout << fixed << setprecision(10) << res << endl; return 0; }
#include "iostream" #include "climits" #include "list" #include "queue" #include "stack" #include "set" #include "functional" #include "algorithm" #include "string" #include "map" #include "unordered_map" #include "unordered_set" #include "iomanip" #include "cmath" #include "random" #include "bitset" #include "cstdio" #include "numeric" #include "cassert" #include "ctime" using namespace std; //constexpr long long int MOD = 1000000007; //constexpr int MOD = 1000000007; //constexpr int MOD = 998244353; constexpr long long int MOD = 998244353; constexpr double EPS = 1e-8; //int N, M, K, T, H, W, L, R; long long int N, M, K, T, H, W, L, R; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N; cout << N - 1 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; using pii = pair<int, int>; /* [💭] [💡] [🎈] */ int main() { cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false); #ifdef _SHIFTPSH freopen("_run/in.txt", "r", stdin), freopen("_run/out.txt", "w", stdout); #endif int n, m; cin >> n >> m; map<int, vector<int>> v; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[x].emplace_back(y); } vector<vector<int>> r; for (const auto &kv : v) r.emplace_back(kv.second); set<int> pos; pos.emplace(n); for (auto &x : r) { queue<int> erq, emq; for (auto &y : x) { if (pos.count(y)) { erq.emplace(y); } if (pos.count(y - 1) || pos.count(y + 1)) { emq.emplace(y); } } while (erq.size()) pos.erase(erq.front()), erq.pop(); while (emq.size()) pos.emplace(emq.front()), emq.pop(); } cout << pos.size() << endl; return 0; }
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } std::vector<std::vector<int> > hen; std::vector<int> dist; std::vector<int> stack; std::vector<int> stack_res; void dfs0(int i, int prev, int goal = -1) { stack.push_back(i); if (i == goal) stack_res = stack; for (auto j : hen[i]) if (j != prev) { dist[j] = dist[i] + 1; dfs0(j, i, goal); } stack.pop_back(); } int cnt = 0; std::vector<int> res; std::vector<int> avoid; void dfs1(int i, int prev) { res[i] = cnt; for (auto j : hen[i]) if (j != prev) { if (avoid[i] == j) continue; cnt++; dfs1(j, i); cnt++; } for (auto j : hen[i]) if (j != prev) { if (avoid[i] != j) continue; cnt++; dfs1(j, i); cnt++; } } int main() { int n = ri(); hen.resize(n); for (int i = 1; i < n; i++) { int a = ri() - 1; int b = ri() - 1; hen[a].push_back(b); hen[b].push_back(a); } dist.assign(n, 0); dfs0(0, -1); int t0 = std::max_element(dist.begin(), dist.end()) - dist.begin(); dist.assign(n, 0); dfs0(t0, -1); int t1 = std::max_element(dist.begin(), dist.end()) - dist.begin(); dist.assign(n, 0); dfs0(t0, -1, t1); auto tmp = stack_res; avoid.resize(n, -1); for (int i = 0; i + 1 < (int) tmp.size(); i++) avoid[tmp[i]] = tmp[i + 1]; /* for (auto i : tmp) std::cerr << i << std::endl; */ res.resize(n); dfs1(tmp[0], -1); for (int i = 0; i < n; i++) { if (i) printf(" "); printf("%d", res[i] + 1); } puts(""); return 0; }
#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...);} void solve(){ string s; read(s); int cnt = 0; FOR(i,0,sz(s)){ if(s[i] == 'o') cnt++; } set<string> ans; FOR(i,0,10){ FOR(j,0,10){ FOR(k,0,10){ FOR(l,0,10){ vb v(10); string t = "...."; t[0] = ('0'+i); t[1] = ('0'+j); t[2] = ('0'+k); t[3] = ('0'+l); bool id=0; int check=0; FOR(m, 0, 4){ if(s[t[m]-'0'] == 'o' && !v[t[m]-'0']){ v[t[m]-'0'] = 1; check++; }else if(s[t[m]-'0'] == 'x') id=1; } if(id) continue; if(check == cnt) ans.insert(t); } } } } ps(sz(ans)); } int main(){ ios::sync_with_stdio(0); cin.tie(0); int t=1; //cin>>t; while(t--){ solve(); } }
#include <iostream> #include <cassert> #include <numeric> #include <vector> #include <algorithm> #include <iomanip> #include <queue> #include <map> #include <cmath> #include <set> #include <stdio.h> #include <string> using namespace std; using Graph = vector<vector<int>>; using ll=long long; using pa=pair<ll,ll>; using tup=tuple<ll,ll,ll>; int main() { ll n; cin>>n; vector<ll> a(n); vector<ll> b(n); vector<ll> c(n); vector<ll> anum(n+1); vector<ll> bnum(n); for (int i = 0; i < n; i++) { cin>>a[i]; anum[a[i]]++; } for (int i = 0; i < n; i++) { cin>>b[i]; } for (int i = 0; i < n; i++) { cin>>c[i]; bnum[c[i]-1]++; } ll ans=0; for (int i = 0; i < n; i++) { ans+=bnum[i]*anum[b[i]]; } cout<<ans<<endl; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int SZ = 2010; class UF { public: int P[SZ + 1]; UF() : P() { rep1(i, SZ) P[i] = -1; } int find(int A) { if (P[A] < 0) return A; return P[A] = find(P[A]); } bool unite(int A, int B) { int a = find(A); int b = find(B); if (a == b) return false; if (P[a] > P[b]) swap(a, b); P[a] += P[b]; P[b] = a; return true; } } uf; int main() { cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; uf.unite(1, 1001); uf.unite(1, W + 1000); uf.unite(H, 1001); uf.unite(H, W + 1000); rep1(i, H) { string S; cin >> S; rep(j, W) { if (S[j] == '#') { uf.unite(i, j + 1001); } } } int kotae = 1000; unordered_set<int> ST; rep1(i, H) { ST.insert(uf.find(i)); } kotae = ST.size() - 1; ST.clear(); rep1(j, W) { ST.insert(uf.find(j + 1000)); } chmin(kotae, int(ST.size()) - 1); co(kotae); Would you please return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vc = vector<char>; using vvl = vector<vl>; using vvc = vector<vc>; using pll = pair<ll, ll>; using vpll = vector<pll>; #define rep(i, n) for(ll i = 0; i < (ll)(n); i++) #define rep2(i, m, n) for(ll i = (ll)(m); i < (ll)(n); i++) #define rep_inv(i, n, m) for(ll i = (ll)(n); i > (ll)(m); i--) int main(){ ll H, W, X, Y; cin >> H >> W >> X >> Y; vvc F(H, vc(W)); string S; rep(i, H){ cin >> S; rep(j, W) F[i][j] = S[j]; } ll ans = 1; X--; Y--; rep2(i, 1, H){ if(X - i < 0) break; if(F[X - i][Y] == '#') break; ans++; } rep2(i, 1, H){ if(X + i >= H) break; if(F[X + i][Y] == '#') break; ans++; } rep2(i, 1, W){ if(Y - i < 0) break; if(F[X][Y - i] == '#') break; ans++; } rep2(i, 1, W){ if(Y + i >= W) break; if(F[X][Y + i] == '#') break; ans++; } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // using namespace atcoder; typedef long long ll; typedef long double ld; #define rep(i,n) for(ll i=0;i<(n);i++) #define repr(i,n) for(ll i=(n-1);i>=0;i--) #define all(x) x.begin(),x.end() #define br cout << "\n"; using namespace std; const long long INF = 1e18; const long long MOD = 1e9+7; using Graph = vector<vector<ll>>; template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;} template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;} ll ceilll(ll a, ll b) {return (a + b-1) / b;} // if(a%b != 0) (a/b) + 1 ll get_digit(ll a) {ll digit = 0; while(a != 0){a /= 10; digit++;} return digit;} // a != 0 template<typename T> void vecdbg(vector<T>& v){ rep(i, v.size()){cout << v[i] << " ";} br;} template<typename T> void vecvecdbg(vector<vector<T>>& v){ rep(i, v.size()){rep(j, v[i].size()){cout << v[i][j] << " ";} br;}} ll POW(ll a, ll n){ ll res = 1; while(n > 0){ if(n & 1){ res = res * a; } a *= a; n >>= 1; } return res; } using P = pair<ll, ll>; // 0 false, 1 true // string number to int : -48 or - '0' // a to A : -32 // ceil(a) 1.2->2.0 // c++17 g++ -std=c++17 a.cpp // global vector -> 0 initialization // DONT FORGET TO INTIALIZE // The type of GRID is CHAR. DONT USE STRING // If the result in local and judge is different, USE CODETEST!! // (a * b)over flow? if(a > INF / b){ /* overflow */} const long double PI = (acos(-1)); int main() { std::cout << std::fixed << std::setprecision(15); ld n; cin >> n; ld x, y; cin >> x >> y; ld x2, y2; cin >> x2 >> y2; //中心 ld xm = (x + x2) / 2; ld ym = (y + y2) / 2; //cout << xm << " " << ym << endl; ld tmp_x = x - xm, tmp_y = y - ym; ld rotate = 180 - (180 * (n-2) / n); //cout << rotate << endl; ld rad = (rotate / 180) * PI; ld new_x = tmp_x * cos(rad) - tmp_y * sin(rad); ld new_y = tmp_x * sin(rad) + tmp_y * cos(rad); cout << new_x + xm << " " << new_y + ym << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll x,y,a,b; cin>>x>>y; cin>>a>>b; ll ans=0; while(x<b) { x=x*a; if(x>=y) break; ans++; } if(x>=y) { cout<<ans; return 0; } ans=ans+(y-x+b-1)/b; cout<<ans-1; }
#include <bits/stdc++.h> using namespace std; int main() { double n; cin>>n; n=sqrt(8*n+1); n=(n-1)/2; int x=n; if(n>x) x++; cout<<x<<endl; }
#include <iostream> #include <bits/stdc++.h> #include <string> #include <map> #include <math.h> #include <vector> #include <set> using namespace std; int main(){ int n; cin >> n; int pb = 1; int add = 2; for(int i = 1; i <= n; i++){ if(pb >= n){ cout << i; break; }else { pb = pb + add; add++; } } }
#include<bits/stdc++.h> using namespace std; typedef long long LL; const LL INF = 1e18 + 7; const LL N = 2e5 + 7; LL x, y, ans; map<LL, LL> dp; LL dfs(LL v) { LL &res = dp[v]; if (res) return res; if (v <= x) return res = x - v; res = v - x; if (v % 2 == 0) { res = min(res, dfs(v / 2) + 1); } else { res = min(res, min(dfs(v - 1), dfs(v + 1)) + 1); } return res; } int main() { cin >> x >> y; ans = dfs(y); cout << ans << endl; return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); } putchar('\n'); } template<unsigned MOD_> struct ModInt { static constexpr unsigned MOD = MOD_; unsigned x; void undef() { x = (unsigned)-1; } bool isnan() const { return x == (unsigned)-1; } inline int geti() const { return (int)x; } ModInt() { x = 0; } ModInt(int y) { if (y<0 || (int)MOD<=y) y %= (int)MOD; if (y<0) y += MOD; x=y; } ModInt(unsigned y) { if (MOD<=y) x = y % MOD; else x = y; } ModInt(long long y) { if (y<0 || MOD<=y) y %= MOD; if (y<0) y += MOD; x=y; } ModInt(unsigned long long y) { if (MOD<=y) x = y % MOD; else x = y; } ModInt &operator+=(const ModInt y) { if ((x += y.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(const ModInt y) { if ((x -= y.x) & (1u<<31)) x += MOD; return *this; } ModInt &operator*=(const ModInt y) { x = (unsigned long long)x * y.x % MOD; return *this; } ModInt &operator/=(const ModInt y) { x = (unsigned long long)x * y.inv().x % MOD; return *this; } ModInt operator-() const { return (x ? MOD-x: 0); } ModInt inv() const { return pow(MOD-2); } ModInt pow(long long y) const { ModInt b = *this, r = 1; if (y < 0) { b = b.inv(); y = -y; } for (; y; y>>=1) { if (y&1) r *= b; b *= b; } return r; } friend ModInt operator+(ModInt x, const ModInt y) { return x += y; } friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; } friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; } friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); } friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; } friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; } friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; } }; constexpr LL MOD = 998244353; using Mint = ModInt<MOD>; const int MAX = 1000011; Mint inv[MAX], fact[MAX], fact_inv[MAX]; bool _auto_init() { fact[0] = 1; for (int i=1; i<MAX; i++) fact[i] = fact[i-1] * i; fact_inv[MAX-1] = fact[MAX-1].inv(); for (int i=MAX-2; i>=0; i--) fact_inv[i] = fact_inv[i+1] * (i+1); inv[0] = 0; for (int i=1; i<MAX; i++) inv[i] = fact_inv[i] * fact[i-1]; return true; } const bool _auto_init_done = _auto_init(); Mint nCk(int n, int k) { return fact[n] * fact_inv[k] * fact_inv[n-k]; } int N, K; int A[200011]; Mint ans[611]; Mint S[611]; void MAIN() { scanf("%d%d", &N, &K); REP (i, N) scanf("%d", A+i); REP (i, N) { Mint p = 1; REP (k, K+1) { S[k] += p; p *= A[i]; } } REP (x, K+1) REP (y, K+1-x) { ans[x+y] += S[x] * S[y] * nCk(x+y, x); } REP (i, N) { Mint p = 1; Mint t = 1; REP (k, K+1) { ans[k] -= p * t; S[k] += p; p *= A[i]; t += t; } } Mint inv2 = Mint(1)/2; REP (k, K+1) ans[k] *= inv2; for (int k=1; k<=K; k++) { printf("%d\n", ans[k].geti()); } } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
#include <iostream> #include<algorithm> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s, t, r; cin >> s; string::size_type i = 0; for(string::size_type j; (j = s.find('R', i)) != string::npos; i = j + 1) { t.append(s, i, j - i); swap(t, r); } t.append(s, i); reverse(begin(r), end(r)); s = ' '; for(char c: r + t) { if(s.back() == c) { s.pop_back(); } else { s.push_back(c); } } cout << s << '\n'; return 0; }
#include<bits/stdc++.h> using namespace std; typedef vector<long long int> vi; typedef long long ll; int main(){ // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif float n;cin>>n; if(floor(n*1.08)<206)cout<<"Yay!"; else if(floor(n*1.08)==206)cout<<"so-so"; else if(floor(n*1.08)>206)cout<<":("; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using P = pair<int, int>; int main() { string x; cin >> x; ll m; cin >> m; if (x.size() == 1){ if(stoi(x) <= m) cout << 1 << endl; else cout << 0 << endl; return 0; } ll d = 0; for(char c: x) d = max(d, ll(c - '0')); ll ac = d; ll wa = m + 1; while(wa - ac > 1){ ll wj = (wa + ac) / 2; ll v = 0; for(char c: x){ if (m/wj < v){ v = m + 1; break; } else v = v * wj + ll(c - '0'); } if (v <= m) ac = wj; else wa = wj; } cout << ac - d << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } else { return false; } } template <typename T> bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } else { return false; } } void solve(std::vector<int64_t> X, int64_t M) { int64_t d = *max_element(X.begin(), X.end()); int L = X.size(); auto convert_base = [](int64_t K, int64_t base) -> vector<int64_t> { vector<int64_t> digits; while (K) { digits.push_back(K % base); K /= base; } reverse(digits.begin(), digits.end()); return digits; }; auto calc = [&](int64_t K, int64_t base) -> bool { vector<int64_t> Y = convert_base(K, base); if (X.size() < Y.size()) { return true; } else if (X.size() > Y.size()) { return false; } for (int i = 0; i < X.size(); i++) { if (X[i] < Y[i]) { return true; } else if (X[i] > Y[i]) { return false; } } return true; }; if (L == 1) { if (calc(M, d + 1)) { cout << 1 << endl; } else { cout << 0 << endl; } return; } if (!calc(M, d + 1)) { cout << 0 << endl; return; } int64_t ok = d + 1; int64_t ng = M + 1; while (ok + 1 < ng) { int64_t base = (ok + ng) / 2; if (calc(M, base)) { ok = base; } else { ng = base; } } cout << ok - d << endl; } int main() { std::string X; std::cin >> X; vector<int64_t> x; for (char c : X) { x.push_back(c - '0'); } int64_t M; scanf("%ld", &M); solve(x, M); return 0; }
#include <iostream> #include"bits/stdc++.h" using namespace std; int dx[] ={0,0,1,-1}; int dy[]={1,-1,0,0}; int cost[2001][2001]; char g[2001][2001]; int H,W; vector<vector<pair<int,int>>>v(26); bool valid(int x , int y ){ return x<H && y<W && x>=0 && y>=0 && g[x][y] != '#' && cost[x][y] == -1; } void bfs(int bx, int by){ memset(cost,-1, sizeof cost); queue<pair<int ,int>>q; q.push({bx,by}); cost[bx][by]= 0; while(!q.empty()){ int x = q.front().first, y = q.front().second; q.pop(); if(g[x][y]== 'G') return; for(int i =0;i<4;++i){ int nX = x + dx[i], nY = y+ dy[i]; if(valid(nX,nY)){ cost[nX][nY] = 1 + cost[x][y]; q.push({nX,nY}); } } if(g[x][y] >= 'a' && g[x][y] <= 'z'){ int cur = g[x][y] - 'a' ; for(int i=0;i<(int)v[ cur].size();++i){ if(v[cur][i].first ==x && v[cur][i].second ==y)continue; int nX = v[cur][i].first, nY = v[cur][i].second; if(cost[nX][nY] == -1 ){ cost[nX][nY] = 1 + cost[x][y]; q.push({nX,nY}); } } v[cur].clear(); } } } int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); cin>>H>>W; int sx,sy,ex,ey; for(int i=0;i<H;++i) for(int j=0;j<W;++j){ //cin>>g[i][j]; scanf(" %c",&g[i][j]); if(g[i][j] >= 'a' && g[i][j] <= 'z'){ int cur = g[i][j] - 'a' ; v[cur].push_back({i,j}); } if(g[i][j]== 'S') sx=i,sy=j; else if (g[i][j]== 'G') ex=i,ey=j; } bfs(sx,sy); cout << cost[ex][ey] << endl; return 0; }
#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 200005 int N,M; ll A[SIZE]; vector<int> G[SIZE]; int main(){ scanf("%d %d",&N,&M); for(ll i = 0; i < N; i++){ scanf("%lld",&A[i]); } int from,to; for(ll i = 0; i < M; i++){ scanf("%d %d",&from,&to); from--; to--; G[to].push_back(from); } ll ans = -HUGE_NUM; for(int i = 1; i < N; i++){ ll minimum = HUGE_NUM; for(int k = 0; k < G[i].size(); k++){ minimum = min(minimum,A[G[i][k]]); } ans = max(ans,A[i]-minimum); A[i] = min(A[i],minimum); } printf("%lld\n",ans); return 0; }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <utility> #include <set> #include <map> #include <cmath> #include <queue> #include <cstdio> #include <limits> #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) using namespace std; template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; } template<class T> inline int sz(T &a) { return a.size(); } using ll = long long; using ld = long double; using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; const int inf = numeric_limits<int>::max(); const ll infll = numeric_limits<ll>::max(); //---------------------------- // 抽象化セグ木 // 二項演算と単位元を渡して使ってね ///****例**************************** // auto f = [&](int a,int b){ return a+b;}; // 二項演算:和 // int id = 0; //単位元:0 // SegTree<decltype(f),int> seg(f,id); //************************************ //---------------------------- template <typename F,typename T> struct SegTree{ // 二項演算merge,単位元identify T identity; F merge; int size; vector<T> dat; // 二項演算fと単位元idを渡して宣言する SegTree(F f,T id):merge(f),identity(id){} // データの要素の数nを渡して初期化、sizeはnより大きい2の冪乗 void init(int n){ size = 1; while(size<=n) size *= 2; dat.resize(size*2-1,identity); } // 配列を渡して0(n)で初期化 void build(vector<T> vec){ rep(i,vec.size()) dat[size-1+i] = vec[i]; dfs(0); } T dfs(int k){ if(k>=size-1) return dat[k]; else return dat[k] = merge(dfs(2*k+1),dfs(2*k+2)); } // index kの要素をaに変更 void update(int k,T a){ k += size - 1; dat[k] = a; while(k > 0){ k = (k-1)/2; dat[k] = merge(dat[2*k+1],dat[2*k+2]); } } // index kの要素にaを加算 void add(int k,T a){ k += size - 1; dat[k] += a; while(k > 0){ k = (k-1)/2; dat[k] = merge(dat[2*k+1],dat[2*k+2]); } } // 区間[a,b)に対するクエリに答える。(k,l,r)=(0,0,size) T query(int a,int b,int k,int l,int r){ if(r<=a||b<=l) return identity; if(a<=l&&r<=b) return dat[k]; else return merge(query(a,b,2*k+1,l,(l+r)/2),query(a,b,2*k+2,(l+r)/2,r)); } T query(int a,int b){ return query(a, b, 0, 0, size); } // デバッグ用 void show(){ int index = 0; int num = 1; while(index<size){ rep(i,num){ if(dat[i+index]==identity) cout << "e "; else cout << dat[i+index] << " "; } cout << "\n"; num *= 2; index = index*2+1; } } }; int main() { string s; cin >> s; int n = sz(s); auto f = [&](ll a,ll b){ return a+b;}; // 二項演算:和 ll id = 0; //単位元:0 vector<SegTree<decltype(f),ll>> seg(26, SegTree<decltype(f),ll>(f,id)); rep(i,26) seg[i].init(n); rep(i,n) { int c = s[i] - 'a'; seg[c].add(i, (ll)1); } ll res = 0; ll right = n; int ch = 26; for(ll i = n-1; i > 0; --i) { if(s[i] == s[i-1]) { int c = s[i] - 'a'; res += right - (i+1) - seg[c].query(i+1, right); if(ch != c) res += n - right; ch = c; right = i-1; i--; } // cout << i << res << "\n"; } cout << res << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //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 bit(n,k) ((n>>k)&1) //nのk bit目 #define vec(T) vector<T> #define vvec(T) vector<vector<T>> using ll = long long; using P = pair<int,int>; 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 llINF = 1LL << 60; const int iINF = 1e9; //------------------------------------------------ struct Solver{ // 最大公約数 int gcd(int a, int b){ if (a%b == 0) return(b); else return(gcd(b, a%b)); } void solve(){ int N; cin >> N; map<int,int> mp; auto mpadd = [&](int ai, int xi){ int p = mp[xi]; mp[xi] = gcd(p,ai); }; int amin=iINF; rep(i,N){ int A; cin >> A; chmin(amin,A); for(int x=1; x*x <=A ; x++){ if(A%x) continue; mpadd(A,x); mpadd(A,A/x); } } int ans = 0; for(auto mi:mp){ if(mi.first <= amin && mi.first == mi.second ) ans++; } cout << ans << endl; } }; int main(){ int testcasenum=1; //cin >> testcasenum; rep1(ti,testcasenum){ Solver solver; solver.solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; //#include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; //typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set; // #include <ext/rope> // using namespace __gnu_cxx; typedef long long int ll; typedef long double ld; #define FAST \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define rep(g, i, n) for (ll g = i; g < n; g++) #define rev(g, n, i) for (ll g = n - 1; g >= i; g--) #define all(v) v.begin(), v.end() #define pb push_back #define mxe(v) *max_element(v.begin(), v.end()) #define mne(v) *min_element(v.begin(), v.end()) #define ve vector #define lb lower_bound #define ub upper_bound #define bb break #define cc continue #define rem 1000000007 #define PI 3.141592653589793238462643383279502 ll power(ll x, ll y, ll p) { ll res = 1; // Initialize result x = x % p; // Update x if it is more than or while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } ll modInverse(ll n, ll p) { return power(n, p - 2, p); } struct point { ll x, y; }; ll h, w, n, m; ll travel(ve<ve<ll>> &arr, ll &x, ll &y, ll inc_x, ll inc_y) { x = x + inc_x, y = y + inc_y; if (x < 1 || x > h) { return 1; } if (y < 1 || y > w) { return 1; } if (arr[x][y] == -1) { return 1; } return 2; } void light_up1(ve<ve<ll>> &arr, ve<ve<ll>> &visit, ll x1, ll y1) { if (visit[x1][y1] == 1) { return; } visit[x1][y1] = 1; ll x = x1, y = y1; while (true) { ll val = travel(arr, x, y, 0, 1); if (val == 1 || visit[x][y] == 1) { break; } else if (val == 2) { visit[x][y] = 1; cc; } } x = x1, y = y1; while (true) { ll val = travel(arr, x, y, 0, -1); if (val == 1 || visit[x][y] == 1) { break; } else if (val == 2) { visit[x][y] = 1; cc; } } } void light_up2(ve<ve<ll>> &arr, ve<ve<ll>> &visit, ll x1, ll y1) { if (visit[x1][y1] == 1) { return; } ll x = x1, y = y1; while (true) { ll val = travel(arr, x, y, 1, 0); if (val == 1 || visit[x][y] == 1) { break; } else if (val == 2) { visit[x][y] = 1; cc; } } x = x1, y = y1; while (true) { ll val = travel(arr, x, y, -1, 0); if (val == 1 || visit[x][y] == 1) { break; } else if (val == 2) { visit[x][y] = 1; cc; } } } int main() { FAST cin >> h >> w >> n >> m; ve<ve<ll>> arr(h + 1, ve<ll>(w + 1, 0)); ve<ve<ll>> visit1(h + 1, ve<ll>(w + 1, 0)), visit2(h + 1, ve<ll>(w + 1, 0)); rep(i, 0, n) { ll x, y; cin >> x >> y; arr[x][y] = 1; } rep(i, 0, m) { ll x, y; cin >> x >> y; arr[x][y] = -1; } rep(i, 1, h + 1) { rep(j, 1, w + 1) { if (arr[i][j] == 1 && visit1[i][j] == 0) { light_up1(arr, visit1, i, j); } } } rep(i, 1, h + 1) { rep(j, 1, w + 1) { if (arr[i][j] == 1 && visit2[i][j] == 0) { light_up2(arr, visit2, i, j); } } } ll ans = 0; rep(i, 1, h + 1) { rep(j, 1, w + 1) { if (visit1[i][j] == 1 || visit2[i][j] == 1) { ans++; } } } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; int main() { // your code goes here int h,w,cnt=0; cin>>h>>w; char wi[101][101]; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ cin>>wi[i][j]; } } for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ if(wi[i][j]=='.' && wi[i+1][j]=='.') cnt++; if(wi[i][j]=='.' && wi[i][j+1]=='.') cnt++; } } cout<<cnt<<endl; return 0; }
#include <iostream> #include <iomanip> #include <string> #include <stack> #include <vector> #include <complex> #include <math.h> #include <stdio.h> #include <algorithm> #include <utility> #include <functional> #include <iterator> #include <map> #include <set> #include <queue> #include <list> #include <regex> #include <limits> #include <time.h> #include <cstdint> #include <unordered_map> #include <unordered_set> #include <bitset> #include <limits.h> using namespace std; using pii = pair<int,int>; using ll=long long; using ld=long double; #define pb push_back #define mp make_pair #define sc second #define fr first #define stpr setprecision #define cYES cout<<"YES"<<endl #define cNO cout<<"NO"<<endl #define cYes cout<<"Yes"<<endl #define cNo cout<<"No"<<endl #define rep(i,n) for(ll i=0;i<(n);++i) #define Rep(i,a,b) for(ll i=(a);i<(b);++i) #define rrep(i,n) for(ll i=n-1;i>=0;i--) #define rRep(i,a,b) for(ll i=a;i>=b;i--) #define crep(i) for(char i='a';i<='z';++i) #define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;}); #define ALL(x) (x).begin(),(x).end() #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define endl '\n' int ctoi(const char c){ if('0' <= c && c <= '9') return (c-'0'); return -1; } ll gcd(ll a,ll b){return (b == 0 ? a : gcd(b, a%b));} ll lcm(ll a,ll b){return a*b/gcd(a,b);} constexpr ll MOD=1000000007; constexpr ll INF=1000000011; constexpr ll MOD2=998244353; constexpr ll LINF = 1001002003004005006ll; constexpr ld EPS=10e-8; template <class T, class U> inline bool chmax(T& lhs, const U& rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T& lhs, const U& rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } template<typename T> istream& operator>>(istream& is,vector<T>& v){for(auto&& x:v)is >> x;return is;} template<typename T,typename U> istream& operator>>(istream& is, pair<T,U>& p){ is >> p.first; is >> p.second; return is;} template<typename T,typename U> ostream& operator>>(ostream& os, const pair<T,U>& p){ os << p.first << ' ' << p.second; return os;} template<class T> ostream& operator<<(ostream& os, vector<T>& v){ for(auto i=begin(v); i != end(v); ++i){ if(i !=begin(v)) os << ' '; os << *i; } return os; } int main(){ ll T;cin >> T; rep(QQQ,T){ ll N;cin >> N; map<ll,ll> M; rep(i,N){ ll X;cin >> X; M[X]++; } if(N%2==1){ cout << "Second" << endl; continue; } ll Z=0; for (auto P : M){ if(P.sc%2==1){ Z=1; } } if(Z){ cout << "First"<< endl; } else{ cout << "Second" << endl; } } }
#include <bits/stdc++.h> //#include <atcoder/all> //using namespace atcoder; #define INF 2000000000000000000 #define ll long long #define ld long double #define pll pair<ll, ll> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll T; cin >> T; for (ll testcase = 0; testcase < T; ++testcase) { ll N; cin >> N; vector<ll> A(N); map<ll, ll> mp; for (ll i = 0; i < N; ++i) { cin >> A.at(i); mp[A.at(i)] += 1; } bool even = true; for (const auto& [key, value] : mp){ if (value % 2 != 0) { even = false; } } if (N % 2 == 1) { cout << "Second" << "\n"; } else { if (even) { cout << "Second" << "\n"; } else { cout << "First" << "\n"; } } } }
//By Whisper_Rain #include <iostream> #include <cstdio> using namespace std; #define rep(i,x,y) for(int i=(x);i<=(y);i++) #define per(i,x,y) for(int i=(x);i>=(y);i--) int n; signed main(){ scanf("%d",&n); rep(i,1,n) { int link1=(i<<1)%n+1,link2=((i<<1)|1)%n+1; printf("%d %d\n",link1,link2); } return 0; }
#include <bits/stdc++.h> #define FASTIO using namespace std; using ll = long long; using Vi = std::vector<int>; using Vl = std::vector<ll>; using Pii = std::pair<int, int>; using Pll = std::pair<ll, ll>; constexpr int I_INF = std::numeric_limits<int>::max(); constexpr ll L_INF = std::numeric_limits<ll>::max(); template <typename T1, typename T2> inline bool chmin(T1& a, const T2& b) { if (a > b) { a = b; return true; } return false; } template <typename T1, typename T2> inline bool chmax(T1& a, const T2& b) { if (a < b) { a = b; return true; } return false; } template <std::ostream& os = std::cout> class Prints { private: class __Prints { public: __Prints(const char* sep, const char* term) : sep(sep), term(term) {} template <class... Args> auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) { print(args...); } template <typename T> auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) { for (size_t i = 0; i < sz; i++) os << vec[i] << (i == sz - 1 ? term : sep); } template <typename T> auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) { for (size_t i = 0; i < h; i++) for (size_t j = 0; j < w; j++) os << mat[i][j] << (j == w - 1 ? term : sep); } private: const char *sep, *term; void print() const { os << term; } void print_rest() const { os << term; } template <class T, class... Tail> void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); } template <class T, class... Tail> void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); } }; public: Prints() {} __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); } }; Prints<> prints; Prints<std::cerr> prints_err; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% void solve() { int N; cin >> N; vector<Pii> ans(N); for (ll i = 0; i < N; i++) { ans[i].first = 2 * (i + 1) % N; ans[i].second = (2 * (i + 1) + 1) % N; } for (auto [a, b] : ans) { prints()(a + 1, b + 1); } } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% int main() { #ifdef FASTIO std::cin.tie(nullptr), std::cout.tie(nullptr); std::ios::sync_with_stdio(false); #endif #ifdef FILEINPUT std::ifstream ifs("./in_out/input.txt"); std::cin.rdbuf(ifs.rdbuf()); #endif #ifdef FILEOUTPUT std::ofstream ofs("./in_out/output.txt"); std::cout.rdbuf(ofs.rdbuf()); #endif std::cout << std::setprecision(18) << std::fixed; solve(); std::cout << std::flush; return 0; }
#define LOCAL #include <bits/stdc++.h> using namespace std; template <typename A, typename B> ostream& operator <<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream& operator <<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T> ostream& operator <<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]"; return out; } template <typename T, class Cmp> ostream& operator <<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}"; return out; } template <typename U, typename T, class Cmp> ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}"; return out; } #ifdef LOCAL #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) #else #define trace(...) 42 #endif template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } void solve() { int n; cin >> n; vector<int> a(n); for (int& x : a) { cin >> x; } int nax = 0; int answer = -1; for (int i = 2; i <= 1000; i++) { int cnt = 0; for (int j = 0; j < n; j++) { if (a[j] % i == 0) cnt++; if (cnt > nax) { nax = cnt; answer = i; } } } cout << answer << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int test = 1; // cin >> test; while (test--) { solve(); } return 0; }
#include <bits/stdc++.h> //#include <boost/multiprecision/cpp_int.hpp> //namespace mp = boost::multiprecision; //#include "atcoder/all" using namespace std; const double PI = 3.14159265358979323846; typedef long long ll; const double EPS = 1e-9; #define rep(i, n) for (int i = 0; i < (n); ++i) typedef pair<ll, ll> P; const ll INF = 10e17; #define cmin(x, y) x = min(x, y) #define cmax(x, y) x = max(x, y) #define ret() return 0; bool equal(double a, double b) { // return fabs(a - b) < DBL_EPSILON; return fabs(a - b) < EPS; } template<class T> std::istream &operator>>(std::istream &in, set<T> &o) { T a; in >> a; o.insert(a); return in; } template<class T> std::istream &operator>>(std::istream &in, queue<T> &o) { ll a; in >> a; o.push(a); return in; } template<class T> bool contain(set<T> &s, T a) { return s.find(a) != s.end(); } typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK; map<ll, int> factorize(ll n) { map<ll, int> res; for (ll i = 2; i * i <= n; i++) { if (n % i != 0) { continue; } res[i] = 0; while (n % i == 0) { n /= i; res[i]++; } } if (n != 1) res[n] = 1; return res; } int main() { int n; cin >> n; vector<ll> v(n); rep(i, n) cin >> v[i]; vector<map<ll, int>> f(n); rep(i, n) f[i] = factorize(v[i]); map<int, int> m; rep(i, n) { for (auto &e: f[i]) { m[e.first]++; } } int ma = 0; for (auto &e : m) cmax(ma, e.second); for (auto &e : m) { if (ma == e.second) { cout << e.first << endl; ret(); } } }
#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 100'000'000'000'000'000 using namespace std; // using namespace atcoder; typedef long long ll; int main(){ int N; cin >> N; ll A[N+1]; rep1(i, N) cin >> A[i]; // A[0]は使わない ll sum[N+1]; sum[0] = 0; for(int i = 1; i <= N; i++){ sum[i] = sum[i-1] + A[i]; } ll sumsum[N+1]; ll maxsumsum[N+1]; sumsum[0] = sum[0]; maxsumsum[0] = sumsum[0]; for(int i = 1; i <= N; i++){ sumsum[i] = sumsum[i-1] + sum[i]; } maxsumsum[N-1] = sumsum[N-1]; for(int i = N - 2; i >= 0; i--){ maxsumsum[i] = max(maxsumsum[i+1], sumsum[i]); } // for(int i = 2; i < N; i++){ // maxsumsum[i] = max(maxsumsum[i-1], sumsum[i-1]); // // cout << i << sumsum[i] << " : " << maxsumsum[i] << endl; // } ll ans = -INF; ll temp; for(int i = 1; i <= N; i++){ temp = sumsum[i]; // cout << temp << endl; temp += (maxsumsum[i-1] - sumsum[i-1]); if (temp > ans){ ans = temp; } // cout << i << " : " << temp << endl; } if (ans < 0) ans = 0; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using ll = long long; int mod = 1000000007; const int INF = 1001001001; template<class T> void chmin(T& a, T b) {if (a > b) a = b;} template<class T> void chmax(T& a, T b) {if (a < b) a = b;} int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}; int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using Graph = vector<vector<int>>; int main() { string n; cin >> n ; ll x = n.size(); if (x % 2 == 1) { ll oans = 0; ll y = x / 2; rep(i,y) { oans += 9 * pow(10, i); } cout << oans << endl; return 0; } ll A = 0, B = 0; for (int i = 0; i < x/2; i++) { A += (n[i] - '0') * pow(10, x/2-1-i); } for (int i = 0; i < x/2; i++) { B += (n[x/2+i] - '0') * pow(10, x/2-1-i); } if (A > B) cout << A - 1 << endl; else cout << A << endl; return 0; }
#include <stdio.h> #include <algorithm> #include <assert.h> #include <cmath> #include <deque> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <stdlib.h> #include <string.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <functional> #include <iomanip> #include <cstdint> #define ll long long #define rep2(i,a,b) for(int i=a;i<=b;++i) #define rep(i,n) for(int i=0;i<n;i++) #define rep3(i,a,b) for(int i=a;i>=b;i--) #define REP(e,v) for(auto e:v) #define pii pair<int,int> #define pll pair<ll,ll> #define tpiii tuple<int,int,int> #define mp make_pair #define mt make_tuple #define pq priority_queue<int> #define pqg priority_queue<int,vector<int>,greater<int>> #define pb push_back #define vec vector #define vi vec<int> #define vl vec<ll> #define vivi vec<vi> #define vlvl vec<vl> #define vpii vec<pii> #define vpll vec<pll> #define vbl vec<bool> #define endl "\n" #define ALL(c) (c).begin(),(c).end() #define D(x) cout << (#x) << " is " << x << endl; using namespace std; int in() {int x;scanf("%d",&x);return x;} ll lin() {ll x;scanf("%lld",&x);return x;} string stin(){string s;cin>>s;return s;} void drop(string s){cout<<s<<endl; exit(0);} void drop(int x){cout<<x<<endl; exit(0);} void drop(ll x){cout<<x<<endl; exit(0);} void drop(double x){cout<<x<<endl; exit(0);} ll F(ll a,ll b){ return a xor b; } using namespace std; template<class T,class M> class SegTreeLazy{ int n; vector<T> data,lazy; const T defT; //初期値かつ単位元 const M defM; function<T(T,T)> ftt; function<T(T,T)> update; function<T(T,M)> ftm; function<M(M,M)> fmm; public: SegTreeLazy(size_t _n, T _defT, M _defM, function<T(T,T)> _ftt, function<T(T,T)> _update, function<T(T,M)> _ftm, function<T(M,M)> _fmm) :defT(_defT),defM(_defM),ftt(_ftt),update(_update),ftm(_ftm),fmm(_fmm){ n = 1; while(n < _n)n*=2; data = vector<T>(2*n-1,defT); lazy = vector<M>(2*n-1,defM); } //初期値代入 void set(int i,T x){ data[i+n-1] = x; } void build(){ //セグ木を一通り作ったらこれをする!!! for(int i = n-2; i>=0; i--) data[i] = ftt(data[i*2+1],data[i*2+2]); } //遅延評価 void eval(int k){ if(lazy[k]==defM) return; if(k < n-1){ lazy[k*2+1] = fmm(lazy[k*2+1], lazy[k]); lazy[k*2+2] = fmm(lazy[k*2+2], lazy[k]); } data[k] = ftm(data[k],lazy[k]); lazy[k] = defM; } T _query(int a,int b,int k,int l,int r){ eval(k); if(r<=a || b<=l)return defT; if(a <= l && r <= b)return data[k]; else{ T c1 = _query(a,b,2*k+1,l,(l+r)/2); T c2 = _query(a,b,2*k+2,(l+r)/2,r); return ftt(c1,c2); } } //sum of [a,b) T query(int a,int b){ return _query(a,b,0,0,n); } //change of a range void _change(int a,int b,M x,int k,int l,int r){ eval(k); if(a <= l && r <= b){ lazy[k] = fmm(lazy[k],x); eval(k); }else if (a < r && l < b){ _change(a,b,x,k*2+1,l,(l+r)/2); _change(a,b,x,k*2+2,(l+r)/2,r); data[k] = ftt(data[k*2+1],data[k*2+2]); } } void change(int a,int b,M x){ _change(a,b,x,0,0,n); } //access : SegTreeLazy型の変数stlについて、stl[i]でi番目の葉の値を返す T operator[](int i){ return query(i,i+1); } }; int main(){ int n=in(),q=in(); SegTreeLazy<ll,ll> tree(n,0,0,F,F,F,F); rep(i,n){ tree.set(i,in()); } tree.build(); rep(i,q){ int t=in(),x=in()-1,y=in(); if(t==1){ tree.change(x,x+1,y); }else{ cout<<tree.query(x,y)<<endl; } } }
#include <bits/stdc++.h> using namespace std; template <typename T> class SegmentTree { std::vector<T> arr; const std::size_t size; const std::function<T(const T, const T)> operation; const T initial_value; std::size_t calc_size(const std::size_t N) const { std::size_t n = 1; while (n < N + 1) { n *= 2; } return n; } void initialize_array() { this->arr = std::vector<T>(2 * this->size - 1, this->initial_value); } T query(const std::size_t a, const std::size_t b, const std::size_t k, const std::size_t l, const std::size_t r) const { if (r <= a || b <= l) { return this->initial_value; } if (a <= l && r <= b) { return this->arr[k]; } else { return this->operation( this->query(a, b, 2 * k + 1, l, (l + r) / 2), this->query(a, b, 2 * k + 2, (l + r) / 2, r)); } } public: SegmentTree(const std::size_t N, const std::function<T(const T, const T)> func, const T init) : size(this->calc_size(N)), operation(func), initial_value(init) { this->initialize_array(); } void update(const std::size_t idx, const T value) { std::size_t modify_idx = idx; modify_idx += this->size - 1; this->arr[modify_idx] = value; while (modify_idx > 0) { modify_idx = (modify_idx - 1) / 2; this->arr[modify_idx] = this->operation(this->arr[modify_idx * 2 + 1], this->arr[modify_idx * 2 + 2]); } } T query(const std::size_t idx) const { return this->arr[idx + this->size - 1]; } // [idx_a, idx_b) T query(const std::size_t idx_a, const std::size_t idx_b) const { return this->query(idx_a, idx_b, 0, 0, this->size); } T operator[](const std::size_t idx) const { return this->arr[idx + this->size - 1]; } }; int main() { size_t N, Q; cin >> N >> Q; SegmentTree<int> st( N + 1, [](int a, int b) { return a ^ b; }, 0); int A; for (size_t i = 1; i <= N; i++) { cin >> A; st.update(i, A); } int T, X, Y; for (size_t i = 0; i < Q; i++) { cin >> T >> X >> Y; if (T == 1) { st.update(X, st[X] ^ Y); } if (T == 2) { cout << st.query(X, Y + 1) << endl; } } }
#include <bits/stdc++.h> using namespace std; #define PI 3.141592653589 #define ll long long int #define ld long double #define vi vector<int> #define vl vector<ll> #define ii pair<int,int> #define pb push_back #define mp make_pair #define ff first #define ss second #define pll pair<ll,ll> #define vv vector #define all(v) (v).begin(),(v).end() #define MAXN 300005 int MOD = 1e9 + 7; ll power(ll a, ll b) { //a^b ll res = 1; a = a % MOD; while (b > 0) { if (b & 1) {res = (res * a) % MOD; b--;} a = (a * a) % MOD; b >>= 1; } return res; } ll fermat_inv(ll y) {return power(y, MOD - 2);} ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);} ll min(ll a, ll b) {return (a > b) ? b : a;} ll max(ll a, ll b) {return (a > b) ? a : b;} struct node{ ll s,c; node(){ c=s=0; } node operator +(const node& other){ node sum=node(); sum.s=s+other.s; sum.c=c+other.c; return sum; } }; struct SegTree { vector<node>t; ll n; SegTree(ll n){ this->n = n; t.assign(4*n+10,node()); } node sum(ll v,ll tl,ll tr,ll l,ll r) { if(l>r) return node(); if(l==tl&&r==tr) return t[v]; ll tm=(tl+tr)/2; return sum(v*2,tl,tm,l,min(r,tm))+sum(v*2+1,tm+1,tr,max(l,tm+1),r); } ll sum(ll l,ll r){ return sum(1,1,n,l,r).s; } ll cnt(ll l,ll r){ return sum(1,1,n,l,r).c; } void update(ll v,ll tl,ll tr,ll pos,ll new_val,ll cnt) { if(tl==tr) t[v].s+=new_val,t[v].c+=cnt; else{ ll tm=(tl+tr)/2; if(pos<=tm) update(2*v,tl,tm,pos,new_val,cnt); else update(2*v+1,tm+1,tr,pos,new_val,cnt); t[v]=t[2*v]+t[2*v+1]; } } void update(ll idx,ll a,ll c){ update(1,1,n,idx,a,c); } }; 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 te = 1; //cin >> te; //SieveOfEratosthenes(1000000); //factorial(1000005); while (te--) { int n,m,q; cin>>n>>m>>q; ll ans=0,t[q],x[q],y[q],a[n+1]={0},b[m+1]={0}; map<int,int>mp; for(int i=0;i<q;i++){ cin>>t[i]>>x[i]>>y[i]; mp[y[i]]++; } int c=1; for(auto i:mp) mp[i.ff]=c++; SegTree S(c+5),T(c+5); for(int i=0;i<n;i++) S.update(mp[a[i+1]],a[i+1],1); for(int i=0;i<m;i++) T.update(mp[b[i+1]],b[i+1],1); for(int i=0;i<q;i++){ if(t[i]==1){ ans-=T.sum(mp[a[x[i]]]+1,c); ans-=a[x[i]]*T.cnt(1,mp[a[x[i]]]); //cout<<ans<<' '; S.update(mp[a[x[i]]],-a[x[i]],-1); a[x[i]]=y[i]; S.update(mp[a[x[i]]],a[x[i]],1); ans+=T.sum(mp[a[x[i]]]+1,c); ans+=a[x[i]]*T.cnt(1,mp[a[x[i]]]); } else{ ans-=S.sum(mp[b[x[i]]]+1,c); ans-=b[x[i]]*S.cnt(1,mp[b[x[i]]]); ///cout<<ans<<' '; T.update(mp[b[x[i]]],-b[x[i]],-1); b[x[i]]=y[i]; T.update(mp[b[x[i]]],b[x[i]],1); ans+=S.sum(mp[b[x[i]]]+1,c); ans+=b[x[i]]*S.cnt(1,mp[b[x[i]]]); } cout<<ans<<'\n'; } } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I) template <typename T> class Seg_Tree{ public: // 0-index vector<T> dat; T initial,M; int n; T unite(T a,T b){// return a + b; } void initsize(int n0,T initial_,T M_){ M = M_; initial = initial_; int k=1; while(1){ if(n0<=k){ n=k; dat.resize(2*n-1); for(int i=0;i<2*n-1;i++)dat[i]=initial; break; } k*=2; } } //i banme wo x nisuru void update(int i,T x){ i += n-1; dat[i] = x; while(i>0){ i = (i-1) / 2; dat[i] = unite(dat[i*2+1],dat[i*2+2]); } } //[a,b) T query0(int a,int b,int k,int l,int r){ if(r<=a || b<=l)return initial; if(a<=l && r<=b)return dat[k]; else{ T vl = query0(a,b,k*2+1,l,(l+r)/2); T vr = query0(a,b,k*2+2,(l+r)/2,r); return unite(vl,vr); } } //return [a,b) T query(int a,int b){ return query0(a,b,0,0,n); } }; map<ll,int> ma; int index_ = 1; ll ind(ll y){ if(ma[y]) return ma[y]; ma[y] = index_; index_++; return ma[y]; } int main(){ int N,M,Q; cin >> N >> M >> Q; vector< vector<ll> > A(2); A[0].resize(N,0); A[1].resize(M,0); vector<ll> T(Q),X(Q),Y(Q); FOR(i,0,Q) cin >> T[i] >> X[i] >> Y[i]; vector<ll> useY; useY.push_back(0); FOR(i,0,Q) useY.push_back(Y[i]); sort(useY.begin(),useY.end()); FOR(i,0,useY.size()) ind(useY[i]); vector< Seg_Tree<ll> > num(2);//個数 vector< Seg_Tree<ll> > sum(2);//和 num[0].initsize(index_+3,0,0); num[1].initsize(index_+3,0,0); sum[0].initsize(index_+3,0,0); sum[1].initsize(index_+3,0,0); num[0].update(1,N); num[1].update(1,M); ll ans = 0; FOR(i,0,Q){ int change = T[i]-1; int stay = (change+1) % 2; int j = X[i] - 1; ll min_num = num[stay].query( 0 , ind(A[change][j]) ); ll from = A[change][j]; A[change][j] = Y[i]; ll to = Y[i]; ans -= min_num * from; ans -= sum[stay].query( ind(from) , 400003 ); min_num = num[stay].query( 0 , ind(to) ); ans += min_num * to; ans += sum[stay].query( ind(to) , 400003); int index = ind(from); ll cnt = num[change].query(index,index+1); num[change].update(index , cnt-1); sum[change].update(index , cnt*from-from); index = ind(to); cnt = num[change].query(index,index+1); num[change].update(index , cnt+1); sum[change].update(index , (cnt+1)*to); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; vector<vector<long long>> a(n, vector<long long>(n)); for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { cin >> a[i][j]; } } for (long long i = 1; i < n; i++) { for (long long j = 1; j < n; j++) { if (((a[i][j] - a[i][j - 1]) != (a[i - 1][j] - a[i - 1][j - 1])) || ((a[i][j] - a[i - 1][j]) != (a[i][j - 1] - a[i - 1][j - 1]))) { return cout << "No\n", 0; } } } long long mn = 1e18 + 9, si = 0, sj = 0; for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { if (a[i][j] < mn) { si = i; sj = j; mn = a[i][j]; } } } vector<long long> aa(n), bb(n); bb[0] = 0; for (long long j = 1; j < n; j++) { bb[j] = a[0][j] - a[0][j - 1] + bb[j - 1]; } mn = *min_element(bb.begin(), bb.end()); for (int i = 0; i < n; i++) bb[i] -= mn; for (long long i = 0; i < n; i++) { aa[i] = a[i][0] - bb[0]; } for (long long i = 0; i < n; i++) { for (long long j = 0; j < n; j++) { assert(aa[i] >= 0 && bb[j] >= 0 && a[i][j] == bb[j] + aa[i]); } } cout << "Yes\n"; for (long long i = 0; i < n; i++) { if (i > 0) { cout << " "; } cout << aa[i]; } cout << '\n'; for (long long i = 0; i < n; i++) { if (i > 0) { cout << " "; } cout << bb[i] << " "; } cout << '\n'; return 0; }
#include<bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2") #pragma GCC optimize("unroll-loops") #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=3; 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=2){ll ret=1;while(y>0){if(y&1ll)ret=ret*x;y>>=1ll;x=x*x;}return ret;} /* [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] */ int n,dep[200010],rt,cnt,E[200010],dis[200010]; vector<int> T[200010]; bool vis[200010]; bool cmp(int x,int y){return dep[x]<dep[y];} void getdis(int u,int fa) {for (int v:T[u]) if (v!=fa) dis[v]=dis[u]+1,getdis(v,u);} void _dfs(int x){ if (T[x].size()==1) dep[x]=1; for (int u:T[x]) if (!vis[u]) vis[u]=1,_dfs(u),dep[x]=max(dep[u]+1,dep[x]); } void dfs(int x){ cnt++,E[x]=cnt; for(int u:T[x]) if (!vis[u]) vis[u]=1,dfs(u),cnt++; } int main() { cin>>n; rep(i,1,n-1) { int a,b; rd(a),rd(b); T[a].push_back(b),T[b].push_back(a); } getdis(1,0);rt=1; F(i) if (dis[i]>=dis[rt]) rt=i; vis[rt]=1; _dfs(rt); F(i) sort(T[i].begin(),T[i].end(),cmp); M(vis,0); vis[rt]=1; dfs(rt); F(i) cout<<E[i]<<' '; return 0; }
#include <bits/stdc++.h> #define ll long long #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) #define vi vector<int> #define vvi vector<vector<int> > #define mii map<int,int> #define pb push_back #define pii pair<int,int> #define mkp make_pair #define scan(a,n) for(int i =0 ; i<n ; i++) cin>>a[i] #define print(a,n) for(int i = 0 ; i < n ; i++) cout<<a[i]<<' ' #define mem(a,v) memset(a,v,sizeof(a)) #define loop(i,n) for (int i = 0; i < n; i++) #define loop1(i,n) for (int i = 1; i < n; i++) #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define PRECISION std::cout.unsetf ( std::ios::floatfield );std::cout.precision(15) #define PI 3.14159265 #define endl '\n' using namespace std; int mod = 1000000007; int m_inf = INT_MIN; void init_code() { fast; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif // ONLINE_JUDGE } void solve() { string s; cin>>s; ll n=s.length(); ll ans=0; vector<ll> cnt(26,0); for(ll i=n-1,j=0;i>0;--i,++j) { if(i<n-1 && i>0 && s[i]==s[i-1] && s[i]!=s[i+1]) { ans+=j-cnt[s[i]-'a']; cnt.assign(26,0); cnt[s[i]-'a']=j; } cnt[s[i]-'a']++; } cout<<ans; } int main() { init_code(); int t=1; //cin>>t; while(t--) { solve(); } }
// Hail god Yato #include <bits/stdc++.h> using namespace std; #define hs ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); typedef long long ll; const ll MOD = 1000000007; const ll INF = 1e18; const ll MAX = 100001; // // void solve(){ string str; cin>>str; int arr[26] = {0}; ll n = str.length(), ans = 0; for(int i = n-1; i >= 1; i--){ if(str[i] == str[i-1]){ ans += n-i-1-arr[str[i] - 'a']; // cout<<ans<<"\n"; for(int i = 0; i < 26; i++) arr[i] = 0; arr[str[i] - 'a'] = n-i+1; --i; } else ++arr[str[i] - 'a']; } cout<<ans; } signed main(){ hs; ll t; t=1; // cin>>t; for (int i=1; i<=t; i++){ //cout<<"Case #"<<i<<": "; solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int n; set<int> have; int main() { cin >> n; for (int i = 0; i <= n; i++) have.insert(i); for (int i = 1; i <= n; i++) { int nr; cin >> nr; have.erase(nr); cout << *have.begin() << '\n'; } }
/* このコード、と~おれ! Be accepted! ∧_∧  (。・ω・。)つ━☆・*。 ⊂   ノ    ・゜+.  しーJ   °。+ *´¨)          .· ´¸.·*´¨) ¸.·*¨)           (¸.·´ (¸.·'* ☆ */ #include <cstdio> #include <algorithm> #include <string> #include <cmath> #include <cstring> #include <vector> #include <numeric> #include <iostream> #include <random> #include <map> #include <unordered_map> #include <queue> #include <regex> #include <functional> #include <complex> #include <list> #include <cassert> #include <iomanip> #include <set> #include <stack> #include <bitset> ////多倍長整数, cpp_intで宣言 //#include <boost/multiprecision/cpp_int.hpp> //using namespace boost::multiprecision; // //#pragma GCC target ("avx2") //#pragma GCC optimization ("O3") //#pragma GCC optimization ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define repeat(i, n, m) for(int i = n; i < (m); ++i) #define rep(i, n) for(int i = 0; i < (n); ++i) #define printynl(a) printf(a ? "yes\n" : "no\n") #define printyn(a) printf(a ? "Yes\n" : "No\n") #define printYN(a) printf(a ? "YES\n" : "NO\n") #define printim(a) printf(a ? "possible\n" : "imposible\n") #define printdb(a) printf("%.50lf\n", a) //少数出力 #define printLdb(a) printf("%.50Lf\n", a) //少数出力 #define printdbd(a) printf("%.16lf\n", a) //少数出力(桁少なめ) #define prints(s) printf("%s\n", s.c_str()) //string出力 #define all(x) (x).begin(), (x).end() #define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI) #define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L) #define Please return #define AC 0 #define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */ #define inf numeric_limits<double>::infinity(); #define linf numeric_limits<long double>::infinity() using ll = long long; using ull = unsigned long long; constexpr int INF = 1073741823; constexpr int MINF = -1073741823; constexpr ll LINF = ll(4661686018427387903); constexpr ll MOD = 1e9 + 7; constexpr ll mod = 998244353; constexpr long double eps = 1e-6; const long double PI = acosl(-1.0L); using namespace std; void scans(string& str) { char c; str = ""; scanf("%c", &c); if (c == '\n')scanf("%c", &c); while (c != '\n' && c != -1 && c != ' ') { str += c; scanf("%c", &c); } } void scanc(char& str) { char c; scanf("%c", &c); if (c == -1)return; while (c == '\n') { scanf("%c", &c); } str = c; } double acot(double x) { return PI / 2 - atan(x); } ll LSB(ll n) { return (n & (-n)); } template<typename T> inline T chmin(T& a, const T& b) { if (a > b)a = b; return a; } template<typename T> inline T chmax(T& a, const T& b) { if (a < b)a = b; return a; } ////atcoder library //#include <atcoder/all> //using namespace atcoder; /*-----------------------------------------ここからコード-----------------------------------------*/ int main() { int n; scanf("%d", &n); int ans = 0, a; set<int> st; rep(i, 200001)st.insert(i); rep(i, n) { scanf("%d", &a); st.erase(a); printf("%d\n", *st.begin()); } Please AC; }
#include <bits/stdc++.h> template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}} template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}} #define ll long long #define double long double #define rep(i,n) for(int i=0;i<(n);i++) #define REP(i,n) for(int i=1;i<=(n);i++) #define mod (ll)(1e9+7) #define inf (ll)(3e18+7) #define eps (double)(1e-9) #define pi (double) acos(-1) #define P pair<int,int> #define PiP pair<int,pair<int,int>> #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() using namespace std; ll cost(ll a, ll b, ll c, ll p, ll q, ll r){ return abs(a-p) + abs(b-q) + max(0LL, r-c); } int main() { int n; cin >> n; vector<ll> x(n), y(n), z(n); rep(i, n)cin >> x[i] >> y[i] >> z[i]; vector<vector<ll>> dp(1<<n, vector<ll>(n, inf)); dp[0][0] = 0; rep(i, (1<<n)-1){ rep(j, n){ if(i & (1<<j) == 0)continue; rep(k, n){ chmin(dp[i|(1<<k)][k], dp[i][j] + cost(x[j],y[j],z[j],x[k],y[k],z[k])); } } } vector<vector<ll>> mn(n, vector<ll>(n, inf)); rep(i, n)rep(j, n)mn[i][j] = cost(x[i],y[i],z[i],x[j],y[j],z[j]); rep(k, n)rep(i, n)rep(j, n){ chmin(mn[i][j], mn[i][k] + mn[k][j]); } ll ans = inf; rep(i, n)chmin(ans, dp[(1<<n)-1][i] + mn[i][0]); cout << ans << 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; #define N 25 #define ff first #define ss second #define ll long long #define pb push_back #define mp make_pair #define pii pair<ll,ll> #define vi vector<ll> #define mii map<ll,ll> #define pq_max priority_queue<ll> #define pq_min priority_queue<ll,vi,greater<ll> > #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define inp(arr,n) for(ll x=0;x<n;x++) cin>>arr[x]; #define w(x) ll x; cin>>x; while(x--) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds; void fio(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } ll n, done; vector<vi> xyz; ll dist[N][N], dp[1<<20][20]; ll f(ll mask, ll pos){ if(mask == done) return dist[pos][0]; if(dp[mask][pos] != -1) return dp[mask][pos]; ll ans = inf; for(ll x=0;x<n;x++){ if(! (mask & (1 << x))){ ans = min(ans, f(mask|(1<<x), x) + dist[pos][x]); } } return dp[mask][pos] = ans; } int main(){ fio(); cin>>n; done = (1 << n) - 1; xyz.resize(n, vi(3)); memset(dp, -1, sizeof dp); for(ll x=0;x<n;x++){ cin>>xyz[x][0]>>xyz[x][1]>>xyz[x][2]; } for(ll x=0;x<n;x++){ for(ll y=0;y<n;y++){ ll d = abs(xyz[x][0] - xyz[y][0]) + abs(xyz[x][1] - xyz[y][1]) + max(0ll, xyz[y][2] - xyz[x][2]); dist[x][y] = d; } } cout<<f(1, 0); return 0; }
#include<bits/stdc++.h> #define rep(i, x) for(ll i = 0; i < x; i++) #define rep2(i, x) for(ll i = 1; i <= x; i++) using ll = long long; using namespace std; ll gcd(ll a, ll b) { if (a < b)swap(a, b); if (a % b == 0)return b; return gcd(b, a % b); } // 返り値: a と b の最大公約数 // ax + by = gcd(a, b) を満たす (x, y) が格納される //main関数内に extGCD(a, b, x, y); でx, yに解が格納 ll extGCD(ll a, ll b, ll& x, ll& y) { if (b == 0) { x = 1; y = 0; return a; } ll d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } signed main() { //input ll t; cin >> t; rep(zz, t) { //n(x) - k(y) = s ll n, s, k; cin >> n >> s >> k; ll g = gcd(n, k); if (s % g != 0) { cout << "-1\n"; continue; } ll x, y; extGCD(n, k, x, y); //cout << x << ' ' << y << endl; x *= (s / g); y *= (s / g); y = 0 - y; k /= g; n /= g; if (y > 0) { y -= n * min(x / k, y / n); } else { y += n * max((0 - x + k - 1) / k, (0 - y + n - 1) / n); } cout << y << endl; } return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<queue> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define chmin(a,b) a=min(a,b) #define chmax(a,b) a=max(a,b) #define mod 1000000007 #define ad(a,b) a=(a+b)%mod; ll po(ll x,ll y){ ll res=1; for(;y;y>>=1){ if(y&1)res=res*x%mod; x=x*x%mod; } return res; } ll gcd(ll a,ll b){ return (b?gcd(b,a%b):a); } #define X 200010 ll fac[X],ivf[X]; void initfac(){ fac[0]=1; for(ll i=1;i<X;i++)fac[i]=fac[i-1]*i%mod; for(ll i=0;i<X;i++)ivf[i]=po(fac[i],mod-2); } ll C(ll n,ll k){ return fac[n]*ivf[n-k]%mod*ivf[k]%mod; } int main(){ cin.tie(0); ios::sync_with_stdio(0); ll n,p[200010],place[200010]; cin>>n; rep(i,n){ cin>>p[i]; p[i]--; place[p[i]]=i; } vector<ll> v; bool ok=1; ll s=0; while(1){ ll i=place[s]-1; if(i<s){ok=0;break;} for(;i>=s;i--){ v.push_back(i); swap(p[i+1],p[i]); } s=place[s]; if(s==n-1)break; } rep(i,n)ok&=(p[i]==i); if(ok){ rep(i,v.size())cout<<v[i]+1<<"\n"; } else cout<<-1<<endl; }
#include<iostream> #include<algorithm> #include<string> using namespace std; int ri(){ int n; scanf("%d", &n); return n; } int main(){ int h = ri(); int w = ri(); int x = ri() - 1; int y = ri() - 1; string s[h]; for(auto &i : s) cin >> i; int cnt = -3; for(int i = x; i < h && s[i][y] != '#'; i++) cnt++; for(int i = x; i >= 0 && s[i][y] != '#'; i--) cnt++; for(int j = y; j < w && s[x][j] != '#'; j++) cnt++; for(int j = y; j >= 0 && s[x][j] != '#'; j--) cnt++; printf("%d\n", cnt); return 0; }
/** * author: Takeda Takumi * created: 27.03.2021 21:02:59 **/ #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); ++i) using ll = long long; using P = pair<int, int>; int h, w; ll find(int x, int y, int map[][101]) { ll res = 1; int i, j; i = x - 1; j = y; while (1 <= i && map[i][j] == 0) { res++; i--; } //cout << res << endl; i = x; j = y + 1; while (j <= w && map[i][j] == 0) { res++; j++; } //cout << res << endl; i = x + 1; j = y; while (i <= h && map[i][j] == 0) { res++; i++; } //cout << res << endl; i = x; j = y - 1; while (1 <= j && map[i][j] == 0) { res++; j--; } //cout << res << endl; return res; } int main() { int x, y; cin >> h >> w >> x >> y; int map[101][101]; for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { map[i][j] = 0; } } for (int i = 1; i <= h; i++) { string s; cin >> s; for (int j = 1; j <= w; j++) { if (s[j - 1] == '#') { map[i][j] = 1; } } } /* for (int i = 1; i <= h; ++i) { for (int j = 1; j <= w; ++j) { cout << map[i][j]; } cout << endl; } */ cout << find(x, y, map) << endl; return 0; }
#include<bits/stdc++.h> #include<cstring> //#define Mfc_Tanzim #define ll long long #define ull unsigned long long #define pb push_back #define ff first #define ss second #define all(v) (v.begin(), v.end()) #define rall(v) (v.rbegin(), v.rend()) #define pi acos(-1.0) #define FastRead ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0); #define bug(a) cerr << #a << " : " << a << endl using namespace std; const ll mod = 1e9+7; const ll mx = 2e6+10; int posx[] = {1,-1, 0, 0}; int posy[] = {0, 0, 1,-1}; ll x, y, arr[mx], arr2[mx]; vector<ll>graph[mx]; map<ll, bool>vis; void Dfs(int src) { x+=arr[src], y+=arr2[src]; vis[src] = true; for(auto x: graph[src]){ if(!vis[x]){ Dfs(x); } } } int main() { FastRead #ifdef Mfc_Tanzim freopen("input.txt","r", stdin); // freopen("output.txt","w", stdout); #endif /// Mfc_Tanzim ll t=1, n, k, m; //cin >> t; while(t--){ cin >> n >> m; for(int i=1; i<=n; i++) cin >> arr[i]; for(int i=1; i<=n; i++) cin >> arr2[i]; while(m--){ ll u, v; cin >> u >> v; graph[u].pb(v); graph[v].pb(u); } bool ok = true; for(int i=1; i<=n; i++){ if(!vis[i]){ x = y = 0; Dfs(i); if(x!=y) ok = false; } } cout << (ok? "Yes" : "No") << endl; } return 0; }
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; using namespace std; const long long int INF = 1e18; const long long int MOD = 1e9+7; typedef pair<ll, ll> pairs; typedef vector<pairs> p; // #define ALL(obj) (obj).begin(),(obj).end() struct edge{ ll to, cost; }; 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; } // 数字の桁数 ll keta(ll N){ int tmp{}; while( N > 0 ){ tmp += ( N % 10 ); N /= 10; } N = tmp; return N; } // べき乗の余り ll modpow(ll a, ll n){ ll res = 1; while(n){ if(n & 1){ res = res * a % MOD; } a = a * a % MOD; n = n >> 1; } return res; } // コンビネーション ll MAXN = 2e5+10; vector<ll> inv(MAXN); ll nCk(ll n, ll k) { ll res = 1; for(ll i = 1; i <= k; i++) { res = res*(n-i+1) % MOD; } for(ll i = 1; i <= k; i++) { res = res*inv[i] % MOD; } return res; } // --------------------------------------------- int main(){ ll n, m; cin>>n>>m; vector<ll> a(n), b(n); for(ll i=0;i<n;++i){ cin>>a[i]; } for(ll i=0;i<n;++i){ cin>>b[i]; } vector<ll> g[n]; for(ll i=0;i<m;++i){ ll c, d; cin>>c>>d; c--; d--; g[c].push_back(d); g[d].push_back(c); } vector<bool> d(n, false); queue<ll> q; string ans = "Yes"; for(ll i=0;i<n;++i){ ll sum = 0; if(d[i] == false){ d[i] = true; q.push(i); sum += b[i] - a[i]; }else{ continue; } while(!q.empty()){ ll x = q.front(); q.pop(); for(auto val : g[x]){ if(d[val] == true){ continue; } q.push(val); sum += b[val] - a[val]; d[val] = true; } } if(sum != 0){ ans = "No"; break; } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define mpi map<ll,ll> #define vl vector<ll> #define vi vector<int> #define pl pair<ll,ll> #define pi pair<int,int> #define forn(i,n) for(ll i=0;i<n;i++) #define ff first #define ss second #define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define inf 1000000000000 #define MOD 1000000007 ll binpow(ll a,ll b,ll m){ a%=m; ll res=1; while(b>0){ if(b&1) res=res*a%m; a=a*a%m; b>>=1; } return res; } vl edge[100005]; ll c[100005]; ll vis[100005]; vl ans; map<ll,ll> mp; void dfs(ll n){ vis[n]=1; forn(i,edge[n].size()){ if(vis[edge[n][i]]==0){ if(mp[c[edge[n][i]]]==0){ ans.pb(edge[n][i]); } mp[c[edge[n][i]]]++; dfs(edge[n][i]); mp[c[edge[n][i]]]--; } } } void solve(){ ll n; cin>>n; forn(i,n) cin>>c[i+1]; forn(i,n-1){ ll x,y; cin>>x>>y; edge[x].pb(y); edge[y].pb(x); } forn(i,n) vis[i+1]=0; ans.pb(1); mp[c[1]]++; dfs(1); sort(ans.begin(),ans.end()); forn(i,ans.size()) cout<<ans[i]<<endl; } int main() { ll t=1; while(t--) { solve(); cout<<endl; } return 0; }
// Problem: B - Reversible Cards // Contest: AtCoder - AtCoder Regular Contest 111 // URL: https://atcoder.jp/contests/arc111/tasks/arc111_b // Memory Limit: 1024 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; typedef long long ll; vector<int> g[400002]; int vis[400002]; pair<int, int> dfs1(int u, int v) { if (vis[u]) return {0, 0}; vis[u] = true; int res1 = 1; int res2 = g[u].size(); for (int x : g[u]) { if (x == v || x == u) continue; auto c = dfs1(x, u); res1 += c.first; res2 += c.second; } return {res1, res2}; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } int sum = 0; for (int i = 0; i < 400002; i++) { if (g[i].size() && vis[i] == 0) { auto c = dfs1(i, 0); if (c.second / 2 < c.first) { sum--; } sum += c.first; } } cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, m, n) for (int i = (m); i < (int)(n); i++) using ll = long long; using vl = vector<ll>; using P = pair<ll, int>; ll solve(vl& A, map<P, ll>& m, ll x, int i) { if (i == int(A.size()) - 1) return 1; if (m.count(P(x, i))) return m[P(x, i)]; ll d = x % A[i + 1]; ll ret = solve(A, m, x - d, i + 1); if (d > 0) ret += solve(A, m, x + (A[i + 1] - d), i + 1); return m[P(x, i)] = ret; } int main() { // input ll N, X; cin >> N >> X; vl A(N); rep(i, N) cin >> A[i]; // solve map<P, ll> m; ll ans = solve(A, m, X, 0); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; char grid[15][15]; int numOfV(int x, int y){ if(grid[x][y] != '#') return 0; int vertexs = 0; if(grid[x][y-1] != '#' && grid[x+1][y] != '#' && grid[x-1][y] != '#'){ vertexs += 2; /*cout<<"1"<<endl;*/} if(grid[x][y-1] != '#' && grid[x][y+1] != '#' && grid[x-1][y] != '#') { vertexs += 2;}// cout<<"2"<<endl;} if(grid[x-1][y] != '#' && grid[x+1][y] != '#' && grid[x][y+1] != '#') { vertexs += 2;}// cout<<"3"<<endl;} if(grid[x][y-1] != '#' && grid[x][y+1] != '#' && grid[x+1][y] != '#') { vertexs += 2;}// cout<<"4"<<endl;} if(grid[x+1][y] == '#' && grid[x][y+1] == '#' && grid[x+1][y+1] != '#'){ vertexs++;}// cout<<"5"<<endl;} if(grid[x-1][y] == '#' && grid[x][y+1] == '#' && grid[x-1][y+1] != '#'){ vertexs++;}// cout<<"6"<<endl;} if(grid[x][y-1] == '#' && grid[x+1][y] == '#' && grid[x+1][y-1] != '#'){ vertexs++;}// cout<<"7"<<endl;} if(grid[x-1][y] == '#' && grid[x][y-1] == '#' && grid[x-1][y-1] != '#'){ vertexs++;}// cout<<"8"<<endl;} //if(grid[x+1][y] == '#' && grid[x][y-1] == '#' && grid[x+1][y-1] != '#'){ vertexs++; cout<<"9"<<endl;} if(grid[x+1][y] == '#' && grid[x][y+1] == '#' && grid[x-1][y] != '#' && grid[x][y-1] != '#'){ vertexs++;}// cout<<"9"<<endl;} if(grid[x+1][y] == '#' && grid[x][y-1] == '#' && grid[x-1][y] != '#' && grid[x][y+1] != '#'){ vertexs++;}// cout<<"10"<<endl;} if(grid[x][y-1] == '#' && grid[x-1][y] == '#' && grid[x+1][y] != '#' && grid[x][y+1] != '#'){ vertexs++;}// cout<<"11"<<endl;} if(grid[x-1][y] == '#' && grid[x][y+1] == '#' && grid[x+1][y] != '#' && grid[x][y-1] != '#'){ vertexs++;}// cout<<"12"<<endl;} return vertexs; } int main(){ int n,w; cin>>n>>w; int vertexs = 0; int catCounter = 0; for(int i = 0; i<n; i++){ for(int j = 0; j<w; j++){ cin>>grid[i][j]; if(grid[i][j] == '#') catCounter++; } } for(int i = 1; i<n-1; i++){ for(int j = 1; j<w-1; j++){ int noV = numOfV(i,j); //cout<<"i = "<<i<< " j = "<<j<<" v = "<< noV << endl; vertexs += noV; } } if(catCounter == 1) cout<<"4\n"; else cout<<vertexs<<"\n"; }
#include <bits/stdc++.h> using namespace std ; typedef long long ll ; #define M 100005 #define YES cout << "Yes" << endl #define NO cout << "No" << endl #define rep(i,n) for(int i = 0 ; i < n ; i++) #define syosu setprecision(10) #define inf (ll)1e15 int n , m ; int main(){ cin >> n >> m ; ll A[n] ; map<int,int> conf ; ll val = inf ; for(int i = 0 ; i < n ; i++){ cin >> A[i] ; if(i < m) conf[A[i]]++ ; } for(ll i = 0 ; i <= n ; i++){ if(conf[i] > 0) continue ; val = min(val,i) ; } int l = 0 , r = m - 1 ; while(r < n - 1){ r++ ; conf[A[r]]++ ; if(conf[A[l]] == 1){ val = min(val,A[l]) ; } conf[A[l]]-- ; l++ ; } cout << val << endl ; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i, n) rep2(i, 0, n) #define rep2(i, m, n) for (int i = m; i < (n); i++) #define per(i, b) per2(i, 0, b) #define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--) #define ALL(c) (c).begin(), (c).end() #define SZ(x) ((int)(x).size()) constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template <class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; } template <class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; } template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << "," << p.second << ")"; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "{"; rep(i, v.size()) { if (i) os << ","; os << v[i]; } os << "}"; return os; } #ifdef LOCAL void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } #define debug(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define debug(...) (void(0)) #define dump(x) (void(0)) #endif const ll INF = TEN(18); int main() { int N, M; cin >> N >> M; V<ll> w(N); rep(i, N) cin >> w[i]; V<int> pm(N); iota(ALL(pm), 0); V<pii> br(M); ll mx = 0; for (auto v : w) chmax(mx, v); rep(i, M) cin >> br[i].se >> br[i].fi; rep(i, M) if (mx > br[i].fi) { puts("-1"); return 0; } map<int, int> D; int now = 0; V<int> xs; rep(i, 1 << N) { int s = 0; rep(j, N) if (i >> j & 1) { s += w[j]; } xs.pb(s); } sort(ALL(xs)); sort(ALL(br)); int md = 0; for (auto v : xs) { while (now < M && br[now].fi < v) { chmax(md, br[now++].se); } D[v] = md; } ll ans = INF; do { V<ll> a(N); rep(i, N) a[i] = w[pm[i]]; V<ll> dp(N); rep(i, N) { int s = 0; if (i) chmax(dp[i], dp[i - 1]); for (int j = i; j < N; ++j) { s += a[j]; if (j > i) chmax(dp[j], dp[i] + D[s]); } } chmin(ans, dp.back()); } while (next_permutation(ALL(pm))); cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #define pb push_back #define mp make_pair #define fir first #define sec second const int N = 5005; const int mod = 998244353; using namespace std; int n, m, f[N], ans, pw[N]; template < typename T > inline T read() { T x = 0, w = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); } while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * w; } int fpow(int x, int y) { int res = 1; for( ; y; y >>= 1, x = 1ll * x * x % mod) if(y & 1) res = 1ll * res * x % mod; return res; } int main() { #ifndef ONLINE_JUDGE freopen("cpp.in", "r", stdin); #endif n = read <int> (), m = read <int> (); for(int c = 1; c <= m; c++) for(int res = 1, k = 0; k <= n; k++) f[k] = (f[k] + res) % mod, res = 1ll * res * (m - c) % mod; for(int i = (pw[0] = 1); i <= n; i++) pw[i] = 1ll * pw[i - 1] * m % mod; for(int i = 1; i <= n; i++) for(int k = 0; k <= i - 2; k++) ans = (1ll * f[k] * pw[n - 2 - k] + ans) % mod; ans = (1ll * n * fpow(m, n) - ans + mod) % mod; printf("%d\n", ans); return 0; }
#include "bits/stdc++.h" using namespace std; #define REP(i, n) for(ll i = 0;i < n;i++) #define ll long long //#define MOD 1000000007LL #define MOD 998244353LL #define doublecout(a) cout<<setprecision(16)<<a<<endl; using vi = vector<ll>; // intの1次元の型に vi という別名をつける using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける using vvvi = vector<vvi>; // intの2次元の型に vvi という別名をつける const ll llMAX=9223372036854775807LL; const ll llMIN=-9223372036854775808LL; void myprint1D(vi &data) { REP(i,data.size()) cout<<data[i]<<" "; cout<<endl; } //配列を[y][x]で表示 void myprint2D_T(vvi &data) { REP(i,data.size()) myprint1D(data[i]); } //配列を[x][y]で表示 void myprint2D(vvi &data) { ll l1=data.size(); ll l2=data[0].size(); REP(j,l2){ REP(i,l1) cout<<data[i][j]<<" "; cout<<endl; } } //print(a,b...)って使い方 void print1(ll a){cout<<a<<endl;} void print2(ll a,ll b){cout<<a<<" "<<b<<endl;} void print3(ll a,ll b,ll c){cout<<a<<" "<<b<<" "<<c<<endl;} void print4(ll a,ll b,ll c,ll d){cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;} void print5(ll a,ll b,ll c,ll d,ll e){cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;} //moddivとnCkとgarner ll gcd(ll a,ll b){ ll r = a % b; while(r!=0LL){ a = b; b = r; r = a % b; } return b; } //c剰余下でaをbで割った値 ll moddiv(ll a,ll b,ll c){ ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans; if (t==0LL) return a/b; for(int i=0;i<900;i++){ m=x0/x1; x2=x0-x1*m; n2=(n0-m*n1)%c; if (x2==1LL){ ans=(n2+c)%c; break; } x0=x1;x1=x2; n0=n1;n1=n2; } return (a+((t*ans)%c)*b-t)/b; } //剰余の割ざん //aのn乗 mod c ll expmod(ll a,ll n,ll c){ ll ans=1LL,aa=a,beki=n; for(int i=0;i<64;i++){ if (beki%2==1LL) ans=ans*aa%c; aa=aa*aa%c; beki/=2LL; if (beki==0LL)break; } return ans; } //nCk vi mulmod(500001);//0!,1!,2!,3!,4!,,,, void first_nCk(){ mulmod[0]=1LL; for(ll i=1;i<500001;i++){ mulmod[i]=mulmod[i-1]*i%MOD; } } ll nCk(ll nn,ll kk){ return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD); } ll modinv(ll a,ll b){ return moddiv(1,a%b,b); } //条件ar<a,br<b ll garner(__int128_t a,__int128_t b,__int128_t ar,__int128_t br){ if (gcd(a,b)==1){ if (a==1){ return br; } if (b==1){ return ar; } __int128_t tmp=(br-ar)%b*modinv(a,b)%b; if (tmp<0)tmp+=b; return ar+tmp*a; }else{//299%200=99,299%300=299とかのとき //ありえない場合-1を出力 __int128_t g=gcd(a,b); if (ar>br){ __int128_t rr=ar-br; if (rr%g!=0){ return -1; } __int128_t rrg=rr/g; __int128_t ret=garner(a/g,b/g,rrg,0); return ret*g+br; }else{ __int128_t rr=br-ar; if (rr%g!=0){ return -1; } __int128_t rrg=rr/g; __int128_t ret=garner(a/g,b/g,0,rrg); return ret*g+ar; } } } ll n,m; int main(){ ll ans=0; cin >> n >> m; vi data(m); // N * M の2次元配列 vi gk(m,0); vi expmi1(n); for(ll i=1;i<n;i++){ expmi1[i]=expmod(m,i-1,MOD); } ll lastsmm=0; ll smm=0; for(ll i=1;i<n;i++){ lastsmm=smm%MOD; smm=0; for(ll j=0;j<m;j++){ gk[j]*=m-j-1; gk[j]+=expmi1[i]; gk[j]%=MOD; ll score=lastsmm+gk[j]; data[j]=score%MOD; smm+=data[j]; } } //myprint2D(data); ans=expmod(m,n,MOD)*n; for(ll j=0;j<m;j++){ ans-=data[j]; } ans=(ans%MOD+MOD)%MOD; cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; reverse(s.begin(),s.end()); int s_s=s.size(); for(int i=0;i<s_s;i++){ if(s.at(i)=='6'){ s.at(i)='9'; }else if(s.at(i)=='9'){ s.at(i)='6'; } } cout << s << endl; return 0; }
#include <bits/stdc++.h> #include<stdio.h> #include<math.h> #include<algorithm> #include<queue> #include<deque> #include<stack> #include<string> #include<string.h> #include<vector> #include<set> #include<map> #include<bitset> #include<stdlib.h> #include<cassert> #include<time.h> #include<bitset> #include <numeric> using namespace std; typedef long long ll; #define rep(i,n) for(int i = 0 ; i < n ; i++) #define Rep(i,n) for(int i = 1 ; i <= n ; i++) const int maxn=2e5+5; const bool DEBUG = 0; int main (){ string s; cin >> s; rep(i,s.size()){ if(s[i] == '6'){ s[i] = '9'; }else if(s[i] == '9'){ s[i] = '6'; } } for(int i = s.size()-1 ; i >= 0; i--){ cout << s[i]; } return 0; }