code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include <bits/stdc++.h> #define fi first #define se second #define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++) #define mk make_pair #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define IT iterator #define V vector #define TP template <class o> #define TPP template <typename t1, typename t2> #define SZ(a) ((int)a.size()) #define all(a) a.begin(), a.end() #define rep(i, a, b) for (int i = a; i <= b; i++) #define REP(i, a, b) for (int i = b; i >= a; i--) #define FOR(i, n) rep(i, 1, n) #define debug(x) cerr << #x << ' ' << '=' << ' ' << x << endl using namespace std; typedef double db; typedef unsigned ui; typedef long long ll; typedef long double ld; // char buf[1 << 20],*p1=buf,*p2=buf; TP void qr(o& x) { char c = gc; x = 0; int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = gc; } while (isdigit(c)) x = x * 10 + c - '0', c = gc; x *= f; } template <class o, class... O> void qr(o& x, O&... y) { qr(x), qr(y...); } TP void qw(o x) { if (x < 0) putchar('-'), x = -x; if (x / 10) qw(x / 10); putchar(x % 10 + '0'); } TP void pr1(o x) { qw(x), putchar(' '); } template <class o, class... O> void pr1(o x, O... y) { pr1(x), pr1(y...); } TP void pr2(o x) { qw(x), putchar(10); } template <class o, class... O> void pr2(o x, O... y) { pr2(x), pr2(y...); } TP bool cmax(o& x, o y) { return (x < y ? x = y, 1 : 0); } TP bool cmin(o& x, o y) { return (x > y ? x = y, 1 : 0); } const int mod = (int)1e9 + 7; TPP void ad(t1& x, t2 y) { (x += y) >= mod && (x -= mod); } TPP void dl(t1& x, t2 y) { (x -= y) < 0 && (x += mod); } ll gcd(ll a, ll b) { return !a ? b : gcd(b % a, a); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll power(ll a, ll b = mod - 2, ll p = mod) { ll c = 1; while (b) { if (b & 1) c = c * a % p; b /= 2; a = a * a % p; } return c; } ll exgcd(ll a, ll b, ll& x, ll& y) { if (!a) { x = 0; y = 1; return b; } ll d = exgcd(b % a, a, y, x); x -= b / a * y; return d; } ll INV(ll n, ll p = mod) { ll x, y; exgcd(n, p, x, y); return (x % p + p) % p; } const int N = 110; const ll INF = 1e15; int n, m, k, val[N]; struct rec { int a[N][N]; rec() { memset(a, 0, sizeof a); } rec operator*(rec b) const { rec c; FOR(i, n) FOR(j, n) FOR(k, n) ad(c.a[i][k], 1ll * a[i][j] * b.a[j][k] % mod); return c; } } a, c; void solve() { qr(n, m, k); FOR(i, n) a.a[i][i] = c.a[i][i] = 1, qr(val[i]); ll inv = power(2 * m); while (m--) { int x, y; qr(x, y); dl(a.a[x][x], inv); ad(a.a[x][y], inv); dl(a.a[y][y], inv); ad(a.a[y][x], inv); } while (k) { if (k & 1) c = c * a; k /= 2; a = a * a; } FOR(i, n) { ll ans = 0; FOR(j, n) ad(ans, 1ll * c.a[j][i] * val[j] % mod); pr2(ans); } } int main() { #ifndef ONLINE_JUDGE clock_t start_time = clock(); #endif int T = 1; // qr(T); while (T--) solve(); #ifndef ONLINE_JUDGE cerr << 1.0 * (clock() - start_time) / CLOCKS_PER_SEC << ' ' << 's' << endl; #endif return 0; }
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <cmath> #include <set> #include <map> #include <iomanip> using namespace std; #define rep(i, n) for(long long i = 0; i < (long long)(n); i++) #define rrep(i, n) for(long long i = (long long)(n - 1); i >= 0; i--) #define all(x) (x).begin(),(x).end() using ll = long long; using ull = unsigned long long; int main() { char a, b, c; cin >> a >> b >> c; cout << b << c << a << endl; return 0; }
#include<iostream> using namespace std; int main() { int n,i,a[1001],sum=0; cin >> n; for(i=0;i<n;i++) { cin >> a[i]; } for(i=0;i<n;i++) { if(a[i]>10) { sum+=a[i]-10; } } cout << sum; return 0; }
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <iostream> #include <iomanip> #include <string> #include <cmath> #include <algorithm> #include <vector> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <stack> #include <queue> #include <bitset> #include <numeric> #include <cassert> #include <memory> #include <random> #include <functional> #include <complex> #include <immintrin.h> #ifdef DEBUG #include "./CompetitiveProgrammingCpp/debug_VC.hpp" #include "./CompetitiveProgrammingCpp/Timer.hpp" #include "./CompetitiveProgrammingCpp/sample.hpp" #else #define dump(...) #endif /* macro */ #define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i) #define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define REPC(x,c) for(const auto& x:(c)) #define REPI2(it,b,e) for(auto it = (b); it != (e); ++it) #define REPI(it,c) REPI2(it, (c).begin(), (c).end()) #define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend()) #define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);) #define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end()) #define ALL(x) (x).begin(),(x).end() #define cauto const auto& /* macro func */ template<class T> inline auto sort(T& t) { std::sort(ALL(t)); } template<class T> inline auto rsort(T& t) { std::sort((t).rbegin(), (t).rend()); } template<class T> inline auto unique(T& t) { (t).erase(unique((t).begin(), (t).end()), (t).end()); } template<class T, class S> inline auto chmax(T& t, const S& s) { if (s > t) { t = s; return true; } return false; } template<class T, class S> inline auto chmin(T& t, const S& s) { if (s < t) { t = s; return true; } return false; } inline auto BR() { std::cout << "\n"; } /* type define */ using ll = long long; using PAIR = std::pair<ll, ll>; using VS = std::vector<std::string>; using VL = std::vector<long long>; using VVL = std::vector<VL>; using VVVL = std::vector<VVL>; using VD = std::vector<double>; template<class T> using V = std::vector<T>; /* using std */ using std::cout; constexpr char endl = '\n'; using std::cin; using std::pair; using std::string; using std::stack; using std::queue; using std::vector; using std::list; using std::map; using std::unordered_map; using std::multimap; using std::unordered_multimap; using std::set; using std::unordered_set; using std::unordered_multiset; using std::multiset; using std::bitset; using std::priority_queue; /* Initial processing */ struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing; /* Remove the source of the bug */ signed pow(signed, signed) { assert(false); return -1; } /* define hash */ namespace std { template <> class hash<std::pair<ll, ll>> { public: size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } }; } /* input */ template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for (T& x : vec) is >> x; return is; } /* constant value */ constexpr ll MOD = 1000000007; //constexpr ll MOD = 998244353; //============================================================================================= signed main() { ll n; cin >> n; VL v(n); cin >> v; ll ans = 0; REPC(x, v) { ans += std::max(0LL, x - 10); } cout << ans << endl; }
#include<bits/stdc++.h> #define inf INT_MAX #define sc(a) scanf("%d",&a) #define scc(a,b) scanf("%d %d",&a,&b) #define sccc(a,b,c) scanf("%d %d %d",&a,&b,&c) #define lsc(a) scanf("%lld",&a) #define lscc(a,b) scanf("%lld %lld",&a,&b) #define lsccc(a,b,c) scanf("%lld %lld %lld",&a,&b,&c) #define pf(a) printf("%d",a) #define pff(a,b) printf("%d %d",a,b) #define pfff(a,b,c) printf("%d %d %d",a,b,c) #define pb(a) push_back(a) #define em empty() #define fr front() #define cl clear() #define sz size() #define mp(a,b) make_pair(a,b) #define fri(b) freopen(b,"r",stdin) #define fro(b) freopen(b,"w",stdout) using namespace std; vector<string> vec; long long arr[64]; int main(){ //fri("gir.txt"); //fro("cik.txt"); int n; cin>>n; long long add=1; arr[0]=add; for(int i=1;i<=60;i++){ add*=2; arr[i]=add; } string sss="aaa"; vec.pb(sss); long long cnt=0; for(int i=1;i<=n;i++){ string asd; cin>>asd; vec.pb(asd); if(asd=="OR"){ cnt+=arr[i]; } } cnt++; cout<<cnt<<endl; return 0; }
#include "bits/stdc++.h" using namespace std; #ifdef _DEBUG #define dlog(str) cout << "====" << str << endl; #else #define dlog(str) #endif #define INF 999999999 #define MOD 1000000007 #define LLI long long int #define REP(i, n) for(LLI i = 0, i##_l = (n); i < i##_l; i++) #define FOR(i, s, e) for(LLI i = s, i##_l = (e); i < i##_l; i++) #define _min(a,b) ((a<b)?a:b) #define _max(a,b) ((a<b)?b:a) #define chmax(a, b) a = _max(a, b) #define chmin(a, b) a = _min(a, b) #define bit(a, shift) ((a>>shift)&1)) #define pm(a) ((a)?1:-1) #define SORT(v) sort(v.begin(),v.end()) #define RSORT(v) sort((v).rbegin(), (v).rend()) // int 2.14E±9 lli 9.2E±18 double 1.7E±380 int main() { cout << fixed << setprecision(10); LLI n; cin >> n; vector<LLI> a(n), b(n); LLI am = 1, bm = 1, amt = 1; LLI ma = 1; REP(i, n) { cin >> a[i]; } REP(i, n) { cin >> b[i]; } REP(i, n) { chmax(ma, b[i] * am); chmax(am, a[i]); chmax(ma, b[i] * am); cout << ma << endl; } return 0; }
/*Jai Shree Ram*/ // Never Give Up #include<bits/stdc++.h> #include<unordered_map> using namespace std; #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int,int> #define vi vector<int> #define mii map<int,int> #define umii unordered_map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define inf 1e18 #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define w(x) int x; cin>>x; while(x--) #define F(i,s,e,j) for(int i=s;i<=e;i+=j) #define mt19937 rng(chrono::steady_clock::now().tjhe_since_epoch().count()); //shuffle(arr,arr+n,rng) #define ld long double void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int dp[1<<19]; vector<pii> queries[101]; int cnt[19]; int solve(int bits,int n) { if(dp[bits]!=-1) return dp[bits]; int num = setbits(bits); cnt[0] = 0; for(int i=1;i<=n;i++) { cnt[i] = cnt[i-1]; if(bits&(1<<i)) cnt[i]++; } dp[bits] = 0; for(pii q:queries[num]) { if(cnt[q.ff]>q.ss) return 0; } if(num==n) return dp[bits] = 1; for(int i=1;i<=n;i++) { if((bits&(1<<i))==0) { dp[bits] += solve(bits|(1<<i),n); } } return dp[bits]; } int32_t main() { c_p_c(); int n,m; cin>>n>>m; for(int i=0;i<m;i++) { int x,y,z; cin>>x; cin>>y>>z; queries[x].pb(mp(y,z)); } memset(dp,-1,sizeof(dp)); cout<<solve(0,n)<<endl; }
// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator) #include <bits/stdc++.h> #define overload3(_1, _2, _3, name, ...) name #define rep1(i, n) for(i64 i = 0LL; i < (n); ++i) #define rep2(i, a, b) for(i64 i = (a); i < (b); ++i) #define rep(...) overload3(__VA_ARGS__, rep2, rep1)(__VA_ARGS__) #define all(v) v.begin(), v.end() using namespace std; using i64 = long long int; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); constexpr char endl = '\n'; i64 N, M; cin >> N >> M; vector<i64> X(M), Y(M), Z(M); rep(i, M) cin >> X[i] >> Y[i] >> Z[i]; vector<i64> dp(1LL<<N); dp[0] = 1; rep(i, 1LL<<N){ i64 x = __builtin_popcount(i); vector<i64> c(N + 1); rep(k, N){ if((i & (1LL<<k)) != 0) c[k] += 1; if(k != 0) c[k] += c[k - 1]; } rep(k, N){ if((i & (1LL<<k)) != 0) continue; rep(l, N) if(l >= k) c[l]++; bool flag = true; rep(j, M){ if(X[j] == x + 1){ if(c[Y[j] - 1] > Z[j]) flag = false; } } if(flag) dp[i | (1LL<<k)] += dp[i]; rep(l, N) if(l >= k) c[l]--; } } cout << dp[(1LL<<N) - 1] << endl; }
#include <iostream> using namespace std; int main(){ char a,b,c; cin >> a >> b >> c; if(a == b && b == c) cout << "Won" << endl; else cout << "Lost" << endl; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; using cd = complex<double>; #define ll long long #define F first #define S second #define PB push_back #define MP make_pair #define LB lower_bound #define UB upper_bound #define sz(a) int((a).size()) #define REP(i, a, b) for (int i = (int)a; i < (int)b; i++) #define REPD(i, a, b) for (int i = (int)a - 1; i >= (int)b; i--) #define VREP(it, v) for (auto it = v.begin(); it != v.end(); it++) typedef pair<ll,ll> pi; typedef vector<ll> vi; const ll MOD = 1000000009; const double PI = acos(-1); template <typename T> using pbds_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename K, typename V> using pbds_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>; char a, b, c; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> a >> b >> c; if (a == b && b == c) cout << "Won" << endl; else cout << "Lost" << endl; } //cout.flush() //cout << fixed << setprecision(9) << x << endl;
#include<bits/stdc++.h> #define int long long using namespace std; signed main() { int a,b,c,d; cin>>a>>b>>c>>d; int arr[4]={a,b,c,d}; sort(arr,arr+4); // maxi=arr[3]; if(arr[3]==arr[0]+arr[1]+arr[2]){ cout<<"Yes"; } else if(arr[3]+arr[0]==arr[1]+arr[2]){ cout<<"Yes"; } else if(arr[3]+arr[0]+arr[1]==arr[2]){ cout<<"Yes"; } else { cout<<"No"; } }
#include <bits/stdc++.h> #include <math.h> #include <chrono> using namespace std; #pragma GCC optimize("-O3") #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define REP(i, n) for (int i = 0; i < (n); ++i) #define FWD(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) #define what(x) cerr << #x << " is " << x << endl; ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) const int MAXN = 4; int32_t main() { _upgrade; vector<int> X(MAXN); rep(i, MAXN) cin >> X[i]; int sum = accumulate(all(X), 0); rep(S, 1 << MAXN) { int s = 0; rep(i, MAXN) if (S & (1 << i)) s += X[i]; // debug(s, sum); if (2 * s == sum) { cout << "Yes" << endl; exit(0); } } cout << "No\n"; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define all(v) v.begin(), v.end() #define chmax(x,y) (x = max(x,y)) #define chmin(x,y) (x = min(x,y)) #define FOR(i,x,n) for(int i=x;i<(n);i++) using ll = long long; using PAIR = pair<int, int>; using PAIRLL = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vi>; using vc = vector<char>; using vvc = vector<vc>; using vb = vector<bool>; using vvb = vector<vb>; using vll = vector<ll>; using vvll = vector<vll>; using Graph = vector<vector<int>>; ll INF = 10000000000; int main() { ll N; cin >> N; vvll C(N,vll(N)); ll MIN_h = INF, MIN_w = INF,MIN = INF; rep(i,N) { rep(j,N) { cin >> C[i][j]; if(MIN>C[i][j]) { chmin(MIN, C[i][j]); MIN_h = i; MIN_w = j; } } } bool check = true; vll mem(N); ll temp1 = INF; rep(i,N) { chmin(temp1, C[0][i]); } rep(i,N) { mem[i] = C[0][i] - temp1; } rep(i,N) { rep(j,N-1) { if(C[i][j+1]!=C[i][j]+mem[j+1]-mem[j]) check = false; } } vvll ans(N, vll(N)); if(check) { cout << "Yes" << endl; vll A(N), B(N); rep(i,N) { A[i] = C[i][MIN_w] - MIN; } rep(i,N) { B[i] = C[MIN_h][i]; } rep(i,N) { cout << A[i]; if(i!=N-1) cout << " "; } cout << endl; rep(i, N) { cout << B[i]; if (i != N - 1) cout << " "; } cout << endl; } else { cout << "No" << endl; } }
#include <bits/stdc++.h> using namespace std; const int N=510; int a[N],c[N][N],d[N][N]; int main() { int n; cin>>n; memset(a,0x3f,sizeof(a)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { cin>>c[i][j]; a[i]=min(a[i],c[i][j]); d[i][j]=c[i][j]-c[i][1]; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(d[i][j]!=d[1][j]) { puts("No"); return 0; } puts("Yes"); for(int i=1;i<=n;i++) cout<<a[i]<<' '; puts(""); for(int i=1;i<=n;i++) cout<<c[1][i]-a[1]<<' '; return 0; }
#include <bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; const int mod = 998244353; const double pi = acos(-1.0); const int inf = INT_MAX; template<const int M> struct ModInt { int val; ModInt(): val(-1) {} ModInt(ll x) : val(x % M) { if (val < 0) { val += M; }} ModInt pow(ll n) const { ModInt ans = 1, x = val; while (n) { if (n & 1) ans *= x; x *= x; n /= 2; } return ans; } ModInt inv() const { return pow(M - 2); } ModInt operator- () const { return M - val; } ModInt operator+ (ModInt m) const { m.val += val; if (m.val >= M) m.val -= M; return m; } ModInt operator- (ModInt m) const { m.val = val - m.val; if (m.val < 0) m.val += M; return m; } ModInt operator* (ModInt m) const { m.val = m.val * (ll) val % M; return m; } ModInt operator/ (ModInt m) const { return *this * m.inv(); } ModInt operator+=(ModInt m) { return *this = *this + m; } ModInt operator-=(ModInt m) { return *this = *this - m; } ModInt operator*=(ModInt m) { return *this = *this * m; } ModInt operator/=(ModInt m) { return *this = *this / m; } friend ModInt operator+(int lhs, ModInt m) { return ModInt(lhs) + m; } friend ModInt operator-(int lhs, ModInt m) { return ModInt(lhs) - m; } friend ModInt operator*(int lhs, ModInt m) { return ModInt(lhs) * m; } friend ModInt operator/(int lhs, ModInt m) { return ModInt(lhs) / m; } friend ModInt operator+(ll lhs, ModInt m) { return ModInt(lhs) + m; } friend ModInt operator-(ll lhs, ModInt m) { return ModInt(lhs) - m; } friend ModInt operator*(ll lhs, ModInt m) { return ModInt(lhs) * m; } friend ModInt operator/(ll lhs, ModInt m) { return ModInt(lhs) / m; } friend ostream &operator<<(ostream &os, const ModInt &m) { os << m.val; return os; } friend istream &operator>>(istream &is, ModInt &m) { ll x; is >> x; m = x; return is; } }; using mint = ModInt<mod>; const int N = 3000 + 5; mint dp[N][N]; mint recurse(int n, int k) { if (n == 0 && k == 0) { return 1; } if (n == 0 || k == 0 || k > n) { return 0; } if (dp[n][k].val != -1) { return dp[n][k]; } return dp[n][k] = recurse(n - 1, k - 1) + recurse(n, k << 1); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; cout << recurse(n, k) << '\n'; return 0; }
#include <stdio.h> #include <bits/stdc++.h> #define ll long long int #define inf 1000000000000 #define mod 998244353 #define sz(x) (int)x.size() #define all(v) v.begin(), v.end() #define fi first #define se second #define ps(x, y) fixed << setprecision(y) << x using namespace std; void solve() { ll n, kl; cin >> n >> kl; vector<vector<ll>> dp(n + 1, vector<ll>(n + 1, 0)); dp[1][1] = 1; for(int i=2;i<=n;i++) { for(int k=i;k>=1;k--) { dp[i][k] = (dp[i][k] + dp[i - 1][k - 1] + (2 * k <= i ? dp[i][2 * k] : 0)) % mod; } } cout << dp[n][kl] << endl; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int t = 1; // cin>>t; while (t--) { solve(); } }
#include <cmath> #include <vector> #include <iostream> #include <iomanip> #include <fstream> #include <algorithm> #include <set> #include <queue> #include <string> #include <map> #include <stack> #include <climits> #include <array> #include <unordered_set> #include <unordered_map> #include <memory> #include <functional> #include <cfloat> #include <numeric> #include <random> #include <sstream> #include <bitset> #include <complex> #include <chrono> #include <cassert> #include <chrono> constexpr int MOD = 1000000007; long long int power(long long int base, int exp) { long long int result{ 1 }; while (exp > 0) { if (exp & 1) { result = (result * base) % MOD; } exp >>= 1; base = (base * base) % MOD; } return result; } long long int combination(const int n, const int r) { long long int result{ 1 }; for (auto i = 0; i < r; ++i) { result = (result * (n - i)) % MOD; } for (auto i = 1; i <= r; ++i) { result = result * power(i, MOD - 2) % MOD; } return result; } int main() { int n, m; std::cin >> n >> m; std::vector<int> series(n); for (auto& a : series) std::cin >> a; const auto sum = std::accumulate(series.begin(), series.end(), 0); if (sum > m) { std::cout << "0\n"; return 0; } std::cout << combination(n + m, sum + n) << '\n'; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #pragma GCC optimize ("-O3") #define int long long #define ld long double #define endl "\n" #define rep(i,begin,end) for (__typeof(end) i=begin-(begin>end); i!=(end)-(begin>end); i+=1-2*(begin>end)) #define umap unordered_map #define pq priority_queue #define pb push_back #define mp make_pair #define fs first #define sec second #define lb lower_bound #define ub upper_bound #define mii map<int,int> #define pii pair<int,int> #define vc vector #define vi vc<int> #define vvi vc<vi> #define all(v) v.begin(),v.end() #define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower); #define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper); #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) ((a)*(b))/gcd((a),(b)) #define remax(a,b) a = max(a,b) #define remin(a,b) a = min(a,b) #define w(t) int t; cin>>t; rep(tc,0,t) #define clr(a,x) memset(a,x,sizeof a) #define chkbit(x,i) ((x)&(1LL<<(i))) #define setbit(x,i) ((x)|(1LL<<(i))) #define setbits(x) __builtin_popcountll(x) #define zerobits(x) __builtin_ctzll(x) #define ps(x,y) fixed<<setprecision(y)<<x #define print(a, n) rep(i,0,n) cout<<a[i]<<" "; cout<<endl; #define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; const int mod = 1e9+7; const int mod2 = 998244353; const int N = 2e6; const int inf = 1e18; const ld PI = 3.141592653589793; //===================================================== template<typename... T> void in(T&... args) {((cin>>args), ...);} template<typename... T> void out(T&&... args) {((cout<<args<<" "), ...);} template<typename... T> void outln(T&&... args) {((cout<<args<<" "), ...); cout<<endl;} int nxt(){int x;cin>>x;return x;} int add(int a,int b,int mod){int res=(a+b)%mod;return (res<0)?res+mod:res;} int mul(int a,int b,int mod){int res=(a*1LL*b)%mod;return (res<0)?res+mod:res;} int range(int x,int y,int z) {return (x >= y && x <= z);} struct customCompare { bool operator () (int x, int y) { return x>y; } }; //===================================================== int n, m; int ModExp(int x, int y, int m){ int res = 1; while (y > 0) { if (y & 1) res = (res*x) % m; y = y>>1; x = (x*x) % m; } return res; } int ModInverse(int a, int m) { return ModExp(a, m-2, m); } int fun (int n, int k) { int ans = 0; if (k <= n) { if (k > n-k) k = n-k; ans = 1; rep (i, 1, k+1) { ans = (ans * ModInverse(i, mod)) % mod; ans = (ans * (n - i + 1)) % mod; } } return ans; } void solve() { int n = nxt(); int m = nxt(); int sum = 0; rep (i, 0, n) { sum += nxt(); } cout << fun(m+n, sum+n); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> #define Mod 998244353 using namespace std; typedef long long ll; inline int read() { int out = 0; bool flag = false; register char cc = getchar(); while (cc < '0' || cc > '9') { if (cc == '-') flag = true; cc = getchar(); } while (cc >= '0' && cc <= '9') { out = (out << 3) + (out << 1) + (cc ^ 48); cc = getchar(); } return flag ? -out : out; } inline void write(ll x, char ch) { if (x < 0) putchar('-'), x = -x; if (x == 0) putchar('0'); else { int num = 0; char cc[22]; while (x) cc[++num] = x % 10 + 48, x /= 10; while (num) putchar(cc[num--]); } putchar(ch); } int fpow(int x, int y) { int res = 1; while (y) { if (y & 1) res = 1ll * res * x % Mod; x = 1ll * x * x % Mod; y >>= 1; } return res; } const int N = 2e5 + 10; int n, a[N]; long long pre, Max, ans; int main() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) { pre += a[i]; long long las = Max; Max = max(Max, (ll)a[i]); ans += pre, ans = ans - las * (i - 1ll) + Max * i; write(ans, '\n'); } return 0; }
#include<bits/stdc++.h> using namespace std; #define fo(i,a,n) for(i=a;i<n;i++) #define ll long long #define deb(x) cout << #x << "=" << x << endl #define pb push_back #define mp make_pair #define ff first #define ss second #define all(x) x.begin(), x.end() #define clr(x) memset(x, 0, sizeof(x)) #define sortall(x) sort(all(x)) #define sortrev(x) sort(all(x),greater<int>()) #define pi 3.1415926535897932384626 typedef pair<int,int> pii; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef vector<double> vd; const int mod = 1000000007; const int inf = 1000000000; const int N = 100007, M = N; ll i,j,cnt,par; void solve() { int n; cin>>n; cout<<(100 - n%100) <<"\n"; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; //cin >> t; while(t--) { solve(); } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<pair<int,int>> A(2*N); for(int i=0;i<2*N;i++){ int a; cin >> a; A[i] = make_pair(a,i); } sort(A.begin(),A.end()); vector<int> c(2*N); for(int i=0;i<N;i++) c[A[i].second] = 1; for(int i=0;i<N;i++) c[A[N+i].second] = 2; vector<pair<int,int>> s(0); vector<char> ans(2*N); for(int i=0;i<2*N;i++){ if(s.size() == 0) s.push_back(make_pair(c[i],i)); else if(s.back().first == c[i]) s.push_back(make_pair(c[i],i)); else{ int a = s.back().second; int b = i; if(a > b) swap(a,b); s.pop_back(); ans[a] = '('; ans[b] = ')'; } } for(int i=0;i<2*N;i++) cout << ans[i]; cout << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; using ull = unsigned long long; #define rep(i,n) for(int i=0;i<int(n);i++) #define rep1(i,n) for(int i=1;i<=int(n);i++) #define per(i,n) for(int i=int(n)-1;i>=0;i--) #define per1(i,n) for(int i=int(n);i>0;i--) #define all(c) c.begin(),c.end() #define si(x) int(x.size()) #define pb emplace_back #define fs first #define sc second template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;} template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;} template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());} template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();} template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){ return o<<"("<<p.fs<<","<<p.sc<<")"; } template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){ o<<"{"; for(const T& v:vc) o<<v<<","; o<<"}"; return o; } constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } #ifdef LOCAL #define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl void dmpr(ostream& os){os<<endl;} template<class T,class... Args> void dmpr(ostream&os,const T&t,const Args&... args){ os<<t<<" ~ "; dmpr(os,args...); } #define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__) #define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \ for(auto v: x) cerr << v << ","; cerr << "}" << endl; #else #define show(x) void(0) #define dump(x) void(0) #define shows(...) void(0) #endif int main(){ cin.tie(0); ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int N; cin >> N; V<double> A(N); rep(i,N) cin >> A[i]; auto f = [&](double x){ double res = 0; rep(i,N) res += x + A[i] - min(A[i],x+x); return res; }; double lb = 0, ub = 1e9; rep(t,120){ double ma = (lb+lb+ub)/3, mb = (lb+ub+ub)/3; if(f(ma) < f(mb)) ub = mb; else lb = ma; } show(lb); cout << f(lb)/N << endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; string S, T; cin >> S >> T; int sc = 0, tc = 0; vector<int> P(N + 1, N - 1); for (int i = N - 1; i > -1; i--) { if (S[i] == '1') { P[i] = i; sc++; } else if (S[i] == '0') { P[i] = P[i + 1]; } if (T[i] == '1') tc++; if (sc < tc) { cout << -1 << '\n'; return 0; } } if (sc < tc || (sc > tc && (sc - tc) % 2 != 0)) { cout << -1 << '\n'; return 0; } int pos = P.front(); int64_t res = 0; for (int i = 0; i < N; i++) { if (T[i] == '1') { int j = pos; S[j] = '0'; res += j - i; pos = P[j + 1]; } else if (S[i] == '1') { int j = P[i + 1]; S[i] = '0'; S[j] = '0'; res += j - i; pos = P[P[j + 1]]; } } cout << res << '\n'; return 0; }
// “Alhamdulillah for Everything” #include<bits/stdc++.h> using namespace std; #define pai acos(-1) #define ff first #define ss second #define ll long long #define pb push_back #define mp make_pair #define pll pair<ll,ll> #define sz(a) (ll)a.size() #define endl "\n" #define gcd(a, b) __gcd(a, b) #define lcm(a, b) ((a*b)/gcd(a,b)) #define all(vec) vec.begin(),vec.end() #define ms(a, b) memset(a,b,sizeof(a)) #define rep(i,b,n) for(ll i = b;i <= n; i++) #define rev(i,b,n) for(ll i = b;i >= n; i--) #define watch(x) cout<<(#x)<<" = "<<(x)<<'\n' #define TEST_CASE(t) for(ll zz=1;zz<=t;zz++) #define PRINT_CASE cout<<"Case "<<zz<<": "; #define fii freopen("input.txt","r",stdin); #define foo freopen("output.txt","w",stdout); #define boom ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL); const ll mod = 1e9+7; const ll mxn = 1e5+7; const ll inf = 1e18; ll sumd(ll a,ll b){return (((a%mod)+(b%mod))%mod);} ll muld(ll a,ll b){return (((a%mod)*(b%mod))%mod);} ll subd(ll a,ll b){return (((a%mod)-(b%mod)+mod)%mod);} int main() { boom; double a,b,c,d,n,m,x,y,z; cin>>a>>b>>c; d = b/a; n = ceil(d*c); for(ll i=n;i>=0;i--) { m = i/c; if(m<d) { cout<<i<<endl; return 0; } } return 0; }
#include <stdio.h> #include <assert.h> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <string.h> #include <string> #include <vector> #include <numeric> #include <iomanip> #define ll long long #define mint modint<MOD> #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 vmi vec<mint> #define vl vec<ll> #define vvi vec<vi> #define vvl 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 lb(c,x) distance(c.begin(),lower_bound(all(c),x)) #define ub(c,x) distance(c.begin(),upper_bound(all(c),x)) 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;} char chin(){char c;cin>>c;return c;} 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> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";} template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;} 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);} int n; vi p,x; vvi ans; bool f(int k,int q){ //k回目 if(k==n-1){ return (p[n-1]==0) ^ (q!=0 && q!=x[n-1]); } if(ans[k+1][q*10%7] < 0) ans[k+1][q*10%7] = f(k+1,q*10%7); if(ans[k+1][(q*10+x[k+1])%7] < 0) ans[k+1][(q*10+x[k+1])%7] = f(k+1, (q*10+x[k+1])%7); if(p[k] == 0){ return ans[k+1][q*10%7] || ans[k+1][(q*10+x[k+1])%7]; }else{ return !(ans[k+1][(q*10+x[k+1])%7] && ans[k+1][(q*10+x[k+1])%7]); } } int main(){ n=in(); string s=stin(); x.resize(n); rep(i,n)x[i] = (s[i]-'0')%7; s=stin(); p.resize(n); rep(i,n)p[i]=s[i]=='T' ? 1:0; // 1 -> T, 0 -> A vi m(n); m[n-1]=1; rep3(k,n-2,0)m[k] = m[k+1] * 10 % 7; ans.resize(n,vi(7,-1)); rep(q,7){ if(p[n-1] == 1){ ans[n-1][q] = q==0 || q==x[n-1]; }else{ ans[n-1][q] = q == 0 && q==x[n-1]; } } rep3(k,n-2,0){ rep(q,7){ if(p[k] == 1){ ans[k][q] = ans[k+1][q] || ans[k+1][((q - x[k]*m[k])%7+7)%7]; }else{ ans[k][q] = ans[k+1][q] && ans[k+1][((q - x[k]*m[k])%7+7)%7]; } } } if(ans[0][0])drop("Takahashi"); else drop("Aoki"); }
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} const ll INF=1e15; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N,M; cin>>N>>M; vector<ll> W(N); rep(i,N) cin>>W[i]; vector<ll> L(M),V(M); rep(i,M) cin>>L[i]>>V[i]; { ll mi=*min_element(all(V)); ll ma=*max_element(all(W)); if(mi<ma){ cout<<"-1"<<"\n"; return 0; } } vector<ll> v=L; sort(all(v)); v.erase(unique(all(v)),v.end()); int P=v.size(); map<ll,int> mp; rep(i,P) mp[v[i]]=i; vector<ll> dp(P+1,INF); rep(i,M) chmin(dp[mp[L[i]]],V[i]); for(int i=P-1;i>=0;i--) chmin(dp[i],dp[i+1]); ll MA=*max_element(all(L)); vector<int> ord(N,0); rep(i,N) ord[i]=i; ll ans=INF; do{ vector<ll> dist; vector<ll> sum; sum.push_back(0); for(int i=0;i<N;i++){ int now=ord[i]; ll ok=MA,ng=-1; while(abs(ok-ng)>1){ ll mid=(ok+ng)/2; ll d=mid; ll w=W[now]; bool dame=false; for(int j=i-1;j>=0;j--){ int tar=ord[j]; ll nw=w+W[tar]; int z=upper_bound(all(v),d)-v.begin(); if(dp[z]<nw){ dame=true; break; } d+=dist[j]; w=nw; } if(dame) ng=mid; else ok=mid; } dist.push_back(ok); } ll res=0; rep(i,dist.size()) res+=dist[i]; chmin(ans,res); }while(next_permutation(all(ord))); cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll =long long; #define all(v) v.begin(),v.end() int main() { ll N; cin>>N; ll n=sqrt(N); ll ans=N; vector<bool> vec(n,false); for(ll i=2;i<=n;i++) { if(vec[i-1]) { continue; } ll k=i; ans++; while(k<=N) { ans--; if(k<=n) vec[k-1]=true; k*=i; } } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; using P = pair<ll, ll>; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define inf 1001001001 #define mod 1000000007 #define mod2 998244353 #define pi acos(-1) #define all(v) v.begin(),v.end() #define vi vector<int> #define vl vector<ll> int dx[4] = {0,-1,0,1}; int dy[4] = {-1,0,1,0}; ll gcd(ll a,ll b){ if(b>a) swap(a,b); if(b==0) return a; return gcd(b,a%b); } ll lcm(ll a,ll b){ ll g=gcd(a,b); return a/g*b; } ll rep_jijo(ll n,ll x){ if(x==0) return 1; if(x%2==0){ ll t=rep_jijo(n,x/2); return t*t%mod; } return n*rep_jijo(n,x-1)%mod; } int main() { ll n; ll ans = 0; cin >> n; map<ll, int>mp; for(ll i = 2; i * i <= n; i++) { ll now = i * i; while(now <= n) { //cout << now << endl; if(now <= n) { mp[now]++; } else { break; } now = now * i; } } /* for(ll i = 2; i * i <= n; i++) { ll now = i * i * i * i; if(now > n) break; while(now <= n) { if(now <= n) ans--; else break; now = now * i; } } */ cout << n - mp.size() << endl; return 0; }
// Template #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <iomanip> #include <tuple> #include <utility> #include <queue> #include <set> #include <map> #define rep_override(x, y, z, name, ...) name #define rep2(i, n) for (int i = 0; i < (int)(n); ++i) #define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i) #define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; constexpr int inf = 1001001001; constexpr ll infll = 3003003003003003003LL; template <typename T> inline bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template <typename T> inline bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &element : vec) is >> element; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) { os << vec[i] << (i + 1 == vec_len ? "" : " "); } return os; } struct IOSET { IOSET() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } ioset; // Main int main() { int n, m; cin >> n >> m; vector<int> x(m), y(m), z(m); rep(i, m) cin >> x[i] >> y[i] >> z[i]; vector<vector<int>> idx(n + 1); rep(i, m) idx[x[i]].push_back(i); vector<ll> dp(1 << n, 0); dp[0] = 1; rep(i, 1, 1 << n) { bool ok = true; for (int j : idx[__builtin_popcount(i)]) { int cnt = __builtin_popcount(i & ((1 << y[j]) - 1)); if (cnt > z[j]) ok = false; } if (ok) { rep(j, n) { if (i & (1 << j)) { dp[i] += dp[i ^ (1 << j)]; } } } } cout << dp[(1 << n) - 1] << "\n"; }
#include <bits/stdc++.h> int main(){ int N, M; std::cin >> N >> M; std::vector< std::pair< int, std::pair< int, int > > > XYZ(M); for(int i=0; i<M; i++){ std::cin >> XYZ[i].first >> XYZ[i].second.first >> XYZ[i].second.second; //XYZ[i].first--; XYZ[i].second.first--; } std::sort(XYZ.begin(), XYZ.end()); std::vector< std::vector< int > > Y(N+1); std::vector< std::vector< int > > Z(N+1); for(int i=0; i<M; i++){ Y[XYZ[i].first].push_back(XYZ[i].second.first); Z[XYZ[i].first].push_back(XYZ[i].second.second); } int pat = 1 << N; std::vector< std::vector< long long > > num(N+1, std::vector< long long >(pat, 0)); for(int i=0; i<N; i++){ num[i+1] = num[i]; for(int j=0; j<pat; j++){ int mask = 1 << i; if(j & mask){ num[i+1][j]++; } } } std::vector< long long > DP(pat, 0); std::vector< long long > DPnum(pat, 0); DP[0] = 1; for(int i=0; i<pat; i++){ for(int j=0; j<Y[DPnum[i]].size(); j++){ if(num[Y[DPnum[i]][j]+1][i] > Z[DPnum[i]][j]){ DP[i] = 0; } } for(int k=0; k<N; k++){ int mask = 1 << k; if((i & mask) == 0){ DP[i | mask] += DP[i]; DPnum[i | mask]++; } } } //for(int i=0; i<pat; i++){ // std::cout << DP[i] << " "; //} //std::cout << std::endl; std::cout << DP[pat-1] << std::endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int N=2e5+5; inline int read() { int x=0,f=1; char c=getchar(); while(c<'0' || c>'9') { if(c=='-') f=-1; c=getchar(); } while(c>='0' && c<='9') { x=x*10+c-'0'; c=getchar(); } return x*f; } int n,a[N],b[N],c[N*2],re[N*2],len=0; inline int Find(int x) { return lower_bound(re+1,re+len+1,x)-re; } vector<int> re1[N*2],re2[N*2]; int tree[N],p[N]; inline int lowbit(int x) { return x&(-x); } inline void add(int place) { for(int i=place;i<=n;i+=lowbit(i)) tree[i]++; } inline int ask(int place) { int ans=0; for(int i=place;i;i-=lowbit(i)) ans+=tree[i]; return ans; } int main() { n=read(); for(int i=1;i<=n;i++) c[i]=a[i]=read()+i; for(int i=1;i<=n;i++) c[i+n]=b[i]=read()+i; sort(c+1,c+n*2+1); for(int i=1;i<=n*2;i++) if(c[i]!=c[i-1]) re[++len]=c[i]; for(int i=1;i<=n;i++) a[i]=Find(a[i]),b[i]=Find(b[i]); for(int i=1;i<=n;i++) re1[a[i]].push_back(i); for(int i=1;i<=n;i++) re2[b[i]].push_back(i); for(int i=1;i<=len;i++) { int siz1=re1[i].size(),siz2=re2[i].size(); if(siz1!=siz2) { printf("-1\n"); return 0; } for(int j=0;j<siz1;j++) p[re1[i][j]]=re2[i][j]; } long long ans=0; for(int i=n;i>=1;i--) ans+=ask(p[i]), add(p[i]); cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define mod 1000000007 using ll=long long; const int INF=1000000000; const ll LINF=1001002003004005006ll; int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} 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;} struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; template<typename T> ostream &operator<<(ostream &os,const vector<T>&v){ for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" "); return os; } template<typename T> istream &operator>>(istream &is,vector<T>&v){ for(T &x:v)is>>x; return is; } #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") template<ll Mod> struct ModInt{ long long x; ModInt():x(0){} ModInt(long long y):x(y>=0?y%Mod:(Mod-(-y)%Mod)%Mod){} ModInt &operator+=(const ModInt &p){ if((x+=p.x)>=Mod) x-=Mod; return *this; } ModInt &operator-=(const ModInt &p){ if((x+=Mod-p.x)>=Mod)x-=Mod; return *this; } ModInt &operator*=(const ModInt &p){ x=(int)(1ll*x*p.x%Mod); return *this; } ModInt &operator/=(const ModInt &p){ (*this)*=p.inverse(); return *this; } ModInt operator-()const{return ModInt(-x);} ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;} ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;} ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;} ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;} bool operator==(const ModInt &p)const{return x==p.x;} bool operator!=(const ModInt &p)const{return x!=p.x;} ModInt inverse()const{ int a=x,b=Mod,u=1,v=0,t; while(b>0){ t=a/b; swap(a-=t*b,b);swap(u-=t*v,v); } return ModInt(u); } ModInt pow(long long n)const{ ModInt ret(1),mul(x); while(n>0){ if(n&1) ret*=mul; mul*=mul;n>>=1; } return ret; } friend ostream &operator<<(ostream &os,const ModInt &p){return os<<p.x;} friend istream &operator>>(istream &is,ModInt &a){long long t;is>>t;a=ModInt<Mod>(t);return (is);} static int get_mod(){return Mod;} }; using mint=ModInt<998244353>; vector<vector<int>> f(5000,vector<int>(5000,-1)); vector<vector<mint>> dp(5001,vector<mint>(5001,0)); signed main(){ int h,w,k;cin>>h>>w>>k; rep(_,k){ int i,j;char c;cin>>i>>j>>c;i--,j--; if(c=='R') f[i][j]=0; if(c=='D') f[i][j]=1; if(c=='X') f[i][j]=2; } // naive { dp[0][0]=1; rep(i,h){ rep(j,w){ if(f[i][j]<0 and (i!=h-1 or j!=w-1)){ dp[i][j]*=2; dp[i][j]/=3; } if(f[i][j]<0){ if(i+1<h) dp[i+1][j]+=dp[i][j]; if(j+1<w) dp[i][j+1]+=dp[i][j]; } if(f[i][j]==0){ if(j+1<w) dp[i][j+1]+=dp[i][j]; } if(f[i][j]==1){ if(i+1<h) dp[i+1][j]+=dp[i][j]; } if(f[i][j]==2){ if(i+1<h) dp[i+1][j]+=dp[i][j]; if(j+1<w) dp[i][j+1]+=dp[i][j]; } } } } // rep(i,h){ // rep(j,w){ // cout<<dp[i][j]<<" "; // } // cout<<endl; // } cout<<dp[h-1][w-1]*mint(3).pow(h*w-k)<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main() { int n, x; string s; cin >> n >> x; cin >> s; int cnt=x; for(int i=0; i<s.size(); i++) { if(s[i]=='x' && cnt>0)cnt--; else if(s[i]=='o')cnt++; } cout << cnt; }
#include <bits/stdc++.h> //#include "/home/amal/basic.h" using namespace std; #define ll long long #define boost ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define loop(i,n) for(ll i=0;i<n;i++) #define tc() int t;cin>>t;while(t--) int main() { boost #ifndef Amal // freopen("/home/amal/Desktop/programming/c++/practise/input_test1.txt","r",stdin); //freopen("/home/amal/Desktop/programming/c++/practise/output_test1.txt","w",stdout); #endif int n,x; cin>>n>>x; string s; cin>>s; int mark=x; for(int i=0;i<n;i++) { //cout<<mark<<" "; if(s[i]=='o') mark++; else if(s[i]=='x') { if(mark>0) mark--; } } cout<<mark; }
#include <bits/stdc++.h> #define INF 1000000000000 #define MOD 1000000007 //10^9+7:合同式の法 #define MOD2 1000007 using namespace std; typedef long long ll; /* 0のアスキー "48" */ //マクロ #define REP(i,n) for(ll i=0;i<(ll)(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++) #define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--) #define ALL(x) x.begin(),x.end() #define chmax(x,y) (x = max(x,y)) #define chmin(x,y) (x = min(x,y)) typedef std::priority_queue<ll> LLPrioQueue; typedef std::priority_queue<ll, std::vector<ll> , std::greater<ll> > LLReversePrioQueue; int main(){ int N; cin >> N; vector<string> s(N); for(ll i = 0;i < N;i++) cin >> s.at(i); vector<ll> A(N,0); ll cnt = 1; for(ll i = 0;i < N;i++){ if(s.at(i) == "AND"){ continue; }else{ ll a = 1; for(int j = 0;j < i+1;j++) a *= 2; cnt += a; } } cout << cnt << endl; return 0; }
#include <bits/extc++.h> using namespace std; using namespace __gnu_cxx; template<typename T> int ex(T p){ T a = 1; if(p == 0){ return a; } else{ for(T i = 1; i <= p; i++){ a = a*i%1000000007; } return a; } } template<typename T> int eq(T p, T q){ T r,s = 0,nextp,prevp = 0,prev2p = 1; if(q != 1){ while(s != 1){ r = p/q; s = p%q; nextp = prevp; prevp = prev2p - r * prevp; prev2p = nextp; p = q; q = s; } return prevp; } else{ return 0; } } template<typename T> int com(T p,T q){ T a,b,c,mod = 1000000007; a = ex(p); b = ex(q); for(T i = 1; i <= p-q; i++){ b = b*i%mod; } if(b != 1){ c = ((1 - eq(mod,b)*mod )/b + mod) %mod; } else{ c = 1; } return a*c%mod; } template<typename T> int gcd(T p,T q){ T s; while(q != 0){ s = p%q; p = q; q = s; } return p; } int main() { int N; cin >> N; vector<string> S(N); vector<long long> T(N+1),F(N+1); T[0] = 1;F[0] = 1; for(int i = 0; i < N; i++){ cin >> S[i]; if(S[i] == "AND"){ T[i+1] = T[i]; F[i+1] = 2*F[i] + T[i]; } else{ T[i+1] = 2*T[i] + F[i]; F[i+1] = F[i]; } } cout << T[N] << endl; }
/* Problem Name:Graph Smoothing algorithm tag:矩阵快速幂 */ #include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <unordered_map> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-4; typedef pair<int, int> pii; typedef pair<ll, ll> pll; //#define x first //#define y second #define iosf ios::sync_with_stdio(false), cin.tie(0), cout << fixed const int N = 110; typedef long long ll; int n; struct Matrix { int a[N][N]; Matrix() { memset(a, 0, sizeof(a)); } int *operator[](int x) { return a[x]; } Matrix operator*(Matrix b) { Matrix res; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) res[i][j] = (res[i][j] + (ll)a[i][k] * b[k][j]) % mod; return res; } } E; Matrix Pow(Matrix x, int y) { Matrix res = E; for (; y; y >>= 1, x = x * x) if (y & 1) res = res * x; return res; } int Pow(int x, int y) { int res = 1; for (; y; y >>= 1, x = (ll)x * x % mod) if (y & 1) res = (ll)res * x % mod; return res; } int ind[N], A[N]; int main() { int m, k; scanf("%d%d%d", &n, &m, &k); int inv = Pow(2 * m, mod - 2); for (int i = 1; i <= n; i++) scanf("%d", &A[i]); for (int i = 1; i <= n; i++) E[i][i] = 1; Matrix base; for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b), ind[a]++, ind[b]++; base[a][a] = base[b][b] = base[a][b] = base[b][a] = inv; } for (int a = 1; a <= n; a++) { base[a][a] = (ll)(2 * m - ind[a]) * inv % mod; } base = Pow(base, k); for (int i = 1; i <= n; i++) { int res = 0; for (int j = 1; j <= n; j++) res = (res + (ll)A[j] * base[j][i]) % mod; printf("%d\n", res); } }
#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[2][100][100]; int AB[200], *A = AB, *B = AB + 100; ll uke[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; rep(i, N) { cin >> A[i]; } auto mae = dp[0], ato = dp[1]; rep(i, N) uke[i][i] = 1; int are = modpow(2 * M, mod - 2); rep(i, M) { int x, y; cin >> x >> y; x--; y--; uke[x][x] -= are; uke[x][y] += are; uke[y][x] += are; uke[y][y] -= are; } rep(x, N) rep(y, N) mae[x][y] = (uke[x][y] % mod + mod) % mod; const ll ma = (1ll << 32) - 1; rep(i, 30) { if (K >> i & 1) { rep(i, N) { ll t1 = 0, t2 = 0; rep(j, N) { auto tmp = (ll)mae[i][j] * A[j]; t1 += tmp >> 32; t2 += tmp & ma; } B[i] = ((t1 % mod << 32) + t2) % mod; } swap(A, B); } if (!K | i >= 31 - __builtin_clz(K)) break; rep(x, N) rep(y, x + 1) { auto tmp1 = mae[x]; auto tmp2 = mae[y]; ll t1 = 0, t2 = 0; rep(z, N) { auto tmp = *tmp1++ * (ll)*tmp2++; t1 += tmp >> 32; t2 += tmp & ma; } ato[y][x] = (ato[x][y] = ((t1 % mod << 32) + t2) % mod); } swap(mae, ato); } rep(i, N) co(A[i]); Would you please return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long int ll; ll mod = 1e9 + 7; void dfs(ll i, ll p, vector<vector<pair<ll,ll>>>& adj,vector<ll>& xors) { for(auto t : adj[i]) { if(t.first!=p) { xors[t.first] = xors[i]^t.second; dfs(t.first,i,adj,xors); } } } int main() { ll t = 1; // cin>>t; while(t--) { ll n; cin>>n; vector<vector<pair<ll,ll>>> adj(n); for(ll i = 0; i<n-1; i++) { ll x, y, w; cin>>x>>y>>w; x--; y--; adj[x].push_back({y,w}); adj[y].push_back({x,w}); } vector<ll> xors(n,0); dfs(0,-1,adj,xors); ll ans = 0; for(ll i = 0; i<=60; i++) { ll cnt = 0; for(ll j = 0; j<n;j++) { if((xors[j])&(1ll<<i)) cnt++; } ans = (ans + ((cnt*(n-cnt))%mod)*((1ll<<i)%mod))%mod; } cout<<ans<<endl; } }
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for (int i = 0; i < (n); ++i) #define DREP(i,s,n) for(int i = (s); i < (n); i++) template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;} template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;} using ll = long long; using P = pair<int,int>; using Pl = pair<long long,long long>; using veci = vector<int>; using vecl = vector<long long>; using vecveci = vector<vector<int>>; using vecvecl = vector<vector<long long>>; const int MOD = 1000000007; const double pi = acos(-1); ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);} ll lcm(ll a, ll b) {return a*b/gcd(a,b);} struct edge{ int to; ll cost; }; veci seen; vecl dp; void dfs(int i,ll rec,vector<vector<edge>> &G) { seen[i] = 1; dp[i] = rec; for(auto e : G[i]) { int ni = e.to; if(seen[ni]) continue; ll d = e.cost; dfs(ni,rec^d,G); } } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main() { int N; cin >> N; vector<vector<edge>> G(N); REP(i,N-1) { int a,b; cin >> a >> b; a--,b--; ll w; cin >> w; G[a].push_back({b,w}); G[b].push_back({a,w}); } ll ans = 0; seen = veci(N); dp = vecl(N); dfs(0,0LL,G); // REP(i,N) cout << dp[i] << " "; // cout << endl; REP(i,60) { ll cnt1 = 0; ll cnt0 = 0; REP(j,N) { if((dp[j]>>i)&1) cnt1++; else cnt0++; } ans += (cnt1*(cnt0))%MOD * (modpow(2,i,MOD)); ans %= MOD; } ans %= MOD; cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define ft first #define sc second #define pt(sth) cout << sth << "\n" using namespace std; typedef long long ll; typedef pair<ll, ll> pll; template<class T>bool chmax(T &a, const T &b) {if(a<b) {a=b; return 1;} return 0;} template<class T>bool chmin(T &a, const T &b) {if(b<a) {a=b; return 1;} return 0;} static const ll INF=1e18; static const ll MAX=101010; //static const ll MOD=1e9+7; static const ll MOD=998244353; //for(i=0;i<N;i++) cin>>a[i]; class sumSegTree { public: ll n, height; vector<ll> data; sumSegTree(ll _n) { n=1; height=1; while(n<_n) { n*=2; height++; } data=vector<ll>(2*n-1); } void add(ll i, ll x) { i+=n-1; data[i]+=x; while(i>0) { i=(i-1)/2; data[i]+=x; } } //[a, b):要求区間,[l, r):対象区間 ll _sum(ll a, ll b, ll k, ll l, ll r) { if(r<=a || b<=l) return 0; if(a<=l && r<=b) return data[k]; else { ll s=_sum(a, b, 2*k+1, l, (l+r)/2); ll t=_sum(a, b, 2*k+2, (l+r)/2, r); return s+t; } } ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); } }; int main(void) { ll i, j, k; ll H,W,M; cin>>H>>W>>M; vector<vector<ll>> yoko(H,vector<ll>{W}),tate(W,vector<ll>({H})); for(i=0;i<M;i++){ ll x,y; cin>>x>>y;x--;y--; yoko[x].push_back(y); tate[y].push_back(x); } for(i=0;i<H;i++) sort(yoko[i].begin(), yoko[i].end()); for(j=0;j<W;j++) sort(tate[j].begin(), tate[j].end()); ll ans=0; ll f=0; for(j=0;j<yoko[0][0];j++) ans+=tate[j][0]; sumSegTree sst(W+1); for(j=yoko[0][0];j<W;j++) sst.add(j,1); for(i=0;i<tate[0][0];i++){ ans+=sst.sum(0, yoko[i][0]); for(auto u:yoko[i]){ if(sst.sum(u, u+1)==0) sst.add(u, 1); } } pt(ans); }
#include <bits/stdc++.h> using namespace std; #define ll long long vector<ll> ten = { 1,10,100,1000,10000,100000 }; int solve(vector<int> a, vector<int> b) { ll sa = 0, sb = 0; for (ll i = 1; i < 10; i++) { sa += i * ten[a[i]]; sb += i * ten[b[i]]; } return sa > sb; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long res = 0; int k; cin >> k; string s, t; cin >> s >> t; vector<int> a(10, 0); vector<int> b(10, 0); for (auto c : s) { if (c != '#') { a[c - '0']++; } } for (auto c : t) { if (c != '#') { b[c - '0']++; } } int num = 0; for (int i = 1; i <= 9; i++) { int stop = 0; for (int j = 1; j <= 9; j++) { a[i]++, b[j]++; if (a[i] + b[i] <= k && a[j] + b[j] <= k) { ll r = solve(a, b); ll add = 1LL*r * (k - a[i] - b[i] + 1) * (k - a[j] - b[j] + 1); if (i != j) { add = 1LL * r * (k - a[i] - b[i] + 1) * (k - a[j] - b[j] + 1); } else { add = 1LL * r * (k - a[j] - b[j] + 1) * (k - a[j] - b[j] + 2); } res += 1LL * add; num += r; if (r) { //cout << i << ' ' << j << ' ' << res << ' ' << add << '\n'; } else { //cout << "NO\n"; } } a[i]--, b[j]--; } } cout << fixed << setprecision(12); cout << 1.0L * res / ((9.0 * k - 8.0) * (9.0 * k - 9.0)) << '\n'; //cout << num; return 0; } /* 2 1144# 2233# 6 5 4 4 7 5 8 4 7 6 12 4 8 5 16 4 8 6 20 4 8 7 24 4 9 5 28 4 9 6 32 4 9 7 36 4 9 8 40 4 0.444444444444 */
// Problem: A - Vanishing Pitch // Contest: AtCoder - AtCoder Beginner Contest 191 // URL: https://atcoder.jp/contests/abc191/tasks/abc191_a // Memory Limit: 1024 MB // Time Limit: 2000 ms // Parsed on: 06-02-2021 17:32:16 IST (UTC+05:30) // Author: Kapil Choudhary // ******************************************************************** // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन | // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ || // ******************************************************************** #include<bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pb push_back #define mp make_pair #define F first #define S second #define PI 3.1415926535897932384626 #define deb(x) cout << #x << "=" << x << endl #define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<vull> vvull; 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); } const int INF = 0x3f3f3f3f; const int mod = 1e9+7; void solve() { int v, t, s, d; cin >> v >> t >> s >> d; int d1 = t * v, d2 = s * v; if(d < d1 || d > d2) cout << "Yes\n"; else cout << "No\n"; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif int t = 1; // cin >> t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 2e6 + 5, mod = 1e9 + 7; int fact[N], inv[N]; int mul(int a, int b){ a = (a + mod) % mod; b = (b + mod) % mod; return 1ll * a * b % mod; } int add(int a, int b){ a = (a + mod) % mod; b = (b + mod) % mod; return (a + b) % mod; } int fp(int b, int p){ if(!p) return 1; ll ret = fp(b, p >> 1); ret = ret * ret % mod; if(p&1) ret = ret * b % mod; return ret; } void pre(){ fact[0] = inv[0] = 1; for(int i = 1; i < N; i++){ fact[i] = mul(fact[i - 1], i); inv[i] = fp(fact[i], mod - 2); } } int ncr(int n, int r){ if(r > n || r < 0) return 0; int ret = mul(fact[n], inv[n - r]); return mul(ret, inv[r]); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); pre(); int n, m, k; cin >> n >> m >> k; if(n > m + k) return cout << 0, 0; cout << add(ncr(n + m, n), -ncr(n + m, m + k + 1)); return 0; }
//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <cstring> #include <chrono> #include <complex> #define ll long long #define ld long double #define vi vector<int> #define vll vector<ll> #define vvi vector < vi > #define pii pair<int,int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 1000000000000000001; #define all(c) c.begin(),c.end() #define mp(x,y) make_pair(x,y) #define mem(a,val) memset(a,val,sizeof(a)) #define eb emplace_back #define pb push_back #define f first #define s second using namespace std; int main() { std::ios::sync_with_stdio(false); int T; T=1; // cin.ignore(); must be there when using getline(cin, s) while(T--) { int n,i,j; cin>>n; ll ar[n+1],sum=0,maxi=0,ct=0,ans=0; for(i=1;i<=n;++i) { cin>>ar[i]; ans=max(ans,ct+maxi); sum=sum+ar[i]; maxi=max(maxi,sum); ct=ct+sum; ans=max(ans,ct); } cout<<ans<<endl; } return 0; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> #define vll vector<long long> #define vi vector<int> #define ll long long #define mp make_pair #define p push #define pb push_back #define np next_permutation #define mpl map<ll,ll> #define fin(i,a,b) for(int i=a;i<=b;i++) #define finr(i,a,b) for(int i=a;i>=b;--i) #define max3(a,b,c) max(max(a,b),c) #define min3(a,b,c) min(min(a,b),c) #define all(v) v.begin(),v.end() #define allr(v) v.rbegin(),v.rend() #define debug(x) cerr << #x << " = " << x << endl; const ll INF =1e18; const ll mod =1e9+7; using namespace std; int main(){ int n; cin>>n; // vll v; ll pref[n+1]; pref[0]=0; ll end[n+1]; end[0]=0; fin(i,1,n){ ll x; cin>>x; pref[i]=pref[i-1]+x; end[i]=end[i-1]+pref[i]; } ll ans=0; ll mx=0; fin(i,1,n){ mx=max(mx,pref[i]); ans=max(ans,end[i-1]+mx); } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> #include<vector> #include<math.h> #include<string.h> using namespace std; #define MOD 1000000007 #define MAX 200005 #define PMAX 55 #define EPS 0.000001 #define INF 2000000000000000000 #define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL) #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/detail/standard_policies.hpp> pair<double,double> polar_co(double x,double y) { pair<double,double> ans; if(abs(x)<EPS) { if(y<0) ans.second=-acos(-1)/2; else ans.second=acos(-1)/2; } else { ans.second=atan(y/x); if(x<0) { if(y>0) ans.second=ans.second+acos(-1); else ans.second=ans.second-acos(-1); } } ans.first=sqrt(x*x+y*y); return ans; } pair<double,double> cart_co(pair<double,double> p) { pair<double,double> ans=make_pair((p.first)*(cos(p.second)),(p.first)*(sin(p.second))); return ans; } int main() { int n,i; scanf("%d",&n); double x[2],y[2]; for(i=0;i<2;i++) { scanf("%lf %lf",&x[i],&y[i]); } double cx=(x[0]+x[1])/2; double cy=(y[0]+y[1])/2; double angle=acos(-1); angle=angle*2/n; for(i=0;i<2;i++) { x[i]-=cx; y[i]-=cy; } pair<double,double> p=polar_co(x[0],y[0]); //printf("Pol(%f,%f)\n",p.first,p.second); p.second=p.second+angle; p=cart_co(p); //printf("car(%f,%f)\n",p.first,p.second); p.first=p.first+cx; p.second=p.second+cy; printf("%f %f",p.first,p.second); return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> // NOTE: AtCoderライブラリ using namespace std; // using namespace atcoder; // NOTE: AtCoderライブラリ #define fi first #define se second #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; i--) #define dbg(x) cerr << #x << ": " << x << endl; #define dbg2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl; #define dbg3(x, y, z) cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << endl; #define nl() cout << "========================================" << endl; #define yn { puts("Yes"); } else{ puts("No"); } // 使い方: if (条件) yn; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef vector<int> vi; typedef vector<long long> vl; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<string> vs; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvl; typedef vector<vector<bool>> vvb; typedef vector<vector<char>> vvc; typedef vector<pair<int, int>> vpii; typedef vector<pair<long long, long long>> vpll; const long long LINF = 1001002003004005006ll; const int INF = 1001001001; template<typename T> void chmin(T &a, T b) { if (a > b) a = b; } template<typename T> void chmax(T &a, T b) { if (a < b) a = b; } int main() { double N; cin >> N; double x0, y0, x2, y2; cin >> x0 >> y0 >> x2 >> y2; double d = (2.0 * M_PI) / N; double xq = (x0 + x2) / 2.0; double yq = (y0 + y2) / 2.0; dbg2(xq, yq); double resx = (x0 - xq) * cos(d) - (y0 - yq) * sin(d) + xq; double resy = (x0 - xq) * sin(d) + (y0 - yq) * cos(d) + yq; cout << fixed << setprecision(10) << resx << " " << resy << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { string S; cin >> S; string SS = S; sort(SS.rbegin(), SS.rend()); if (SS < "atcoder") { cout << -1 << endl; continue; } int ans = 1005; for (int i = 0; i < S.size(); i++) { if (!i) { if (S > "atcoder") { ans = 0; } } else { string cp = S; for (int j = 0; j < i; j++) { swap(cp[i - j], cp[i - j - 1]); if (cp > "atcoder") { ans = min(ans, j + 1); break; } } } } cout << ans << endl; } return 0; }
#include <iostream> using namespace std; int ans[1000]; int main() { int n; cin>>n; string t,s="atcoder"; for(int i=0;i<n;i++) { cin>>t; int cnt=0; if(t>s){ans[i]=0;continue;} for(int j=0;j<t.size();j++) { if(t[j]!='a') { if(t[j]>'t')cnt--; break; } cnt++; } if(cnt==t.size())ans[i]=-1; else ans[i]=cnt; } for(int i=0;i<n;i++) cout<<ans[i]<<endl; return 0; }
#include<bits/stdc++.h> #define rep(i,n) for(int i = 0 ; i < (n); ++i) using namespace std ; typedef long long ll ; const int mod=1000000007; const int N=1e3 ; void solve(){ int x,y ; cin>>x>>y ; if(abs(x-y)<3){ cout<<"Yes" ; } else{ cout<<"No" ; } } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin) ; freopen("output.txt","w",stdout) ; #endif cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int y ; y=1 ; //cin>>y ; while(y--){ solve() ; } return 0 ; }
#include<bits/stdc++.h> using namespace std; int main() { int a,b; cin>>a>>b; cout<<((2*a)+100)-b<<endl; return 0; }
#include <bits/stdc++.h> #define loop(s, e, i) for (int i = s; i < e; ++i) #define print(s) cout << s << endl; #define DIV 1000000007 #define ll long long using namespace std; const int INF = 1e9+7; ll ceildiv(ll a, ll b) { return (a+b-1)/b; } // 切り上げ ll floordiv(ll a, ll b) { return a/b; } // 切り下げ int show_matrix(vector<vector<int>> &dp) { loop(0, dp.size(), i) { loop(0, dp[i].size(), j) { cout << dp[i][j] << " "; } cout << endl; } return 0; } ll power_mod(ll n, ll k, ll mod) { ll result = 1; while(k > 0) { if ((k & 1) == 1) result = (result * n) % mod; n = n*n % mod; k >>= 1; } return result; } /* 浮動小数点の入力 cout << fixed << setprecision(9) << endl; */ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll N, M; cin >> N >> M; ll M2 = M * M; ll a = 1; ll b = power_mod(10, N, M2); print (b / M); }
#include<bits/stdc++.h> using namespace std; #define D(x) cout<<__LINE__<<" : "<<#x<<" = "<<x<<endl int t, n, _max, m, x, y, tc; int main() { //freopen("input.txt","r",stdin); cin>>n>>m; puts(m%n == 0 ? "Yes" : "No"); return 0; }
#include "bits/stdc++.h" #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ub upper_bound #define lb lower_bound #define isrt insert #define clr clear #define rsz resize #define ff first #define ss second #define lli long long int #define pb push_back #define pf push_front #define mkp make_pair #define pii pair<lli,lli> #define vi vector<int> #define mii map<lli,lli> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 998244353 #define INF 1e9 #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define w(x) int x; cin>>x; while(x--) #define all(v) v.begin(),v.end() #define endl "\n" const double PI = acos(-1); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; lli maxm(lli a, lli b) { return (a >= b ? a : b); } lli minm(lli a, lli b) { return (a <= b ? a : b); } lli power(lli x, lli y, lli p) { lli res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } lli nCrmodp(lli n, lli r, lli p) { if (r == 0) return 1; if (r == 1) return n; lli pro = 1, k = n; while (k > r) { pro *= k; pro %= p; --k; } k = 2; while (k <= (n - r)) { pro *= power(k, (p - 2), p); pro %= p; ++k; } return pro; } lli first_big(lli n, lli k, vector<lli> arr) { lli l, r, ans = n; l = 0; r = n - 1; while (l <= r) { lli m = l + (r - l) / 2; if (arr[m] > k) { ans = m; r = m - 1; } else l = m + 1; } return ans; } lli last_small(lli n, lli k, vector<lli> arr) { lli l, r, ans = -1; l = 0; r = n - 1; while (l <= r) { lli mid = (l + r) / 2; if (arr[mid] <= k) { ans = mid; l = mid + 1; } else r = mid - 1; } return ans; } void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } lli dp[3001][6001]; lli dfs(lli i, lli j) { if (dp[i][j] == -1) { if (i < j || i == 0 || j == 0) dp[i][j] = 0; else dp[i][j] = (dfs(i - 1, j - 1) + dfs(i, 2 * j)) % mod; } return dp[i][j]; } int main() { c_p_c(); lli n, k; cin >> n >> k; memset(dp, -1, sizeof(dp)); dp[0][0] = 1; cout << dfs(n, k) << endl; return 0; }
#include<bits/stdc++.h> #define pb push_back #define sz(x) (int)(x.size()) #define all(x) x.begin(), x.end() #define int long long #define pii pair<int, int> #define inf 0x3f3f3f3f #define ar array #define mod 998244353 #define F first #define S second #define wopen(x) freopen((x),"w",stdout) #define ropen(x) freopen((x),"r",stdin) #define de(x) cout << #x << " = " << x << ' '; #define io ios_base::sync_with_stdio(0); cin.tie(0); using namespace std; const int mxN = 3005; int n, k, f[mxN][mxN]; int dp(int n, int k) { if(k > n) return 0; if(k == 0 or n == 0) return 0; if(f[n][k]) return f[n][k]; return f[n][k] = (dp(n - 1, k - 1) + dp(n, 2 * k)) % mod; } signed main() { io; cin >> n >> k; f[1][1] = 1; cout << dp(n, k); }
#include <bits/stdc++.h> //#define DEBUG #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) typedef long long ll; typedef unsigned long long ull; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3fLL; const int M = 2e5 + 100; using namespace std; inline ll read() { ll x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); }while (isdigit(ch)) { x = x * 10 + ch - 48; ch = getchar(); } return x * f; } void solve() { ll n,k; cin >> n >> k; for (int i = 0; i < k; i++) { if (n % 200)n = n * 1000 + 200; else n /= 200; } cout << n << endl; return; } int main() { IO; #ifdef DEBUG freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif int test; solve(); return 0; }
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; int c0 = 0, c1 = 0; for (int i = 0; i < n; i++) { string s; cin >> s; int c = 0; for (int j = 0; j < m; j++) if (s[j] == '1') c++; if (c & 1) c1++; else c0++; } cout << 1LL * c0 * c1 << '\n'; return 0; }
#include<bits/stdc++.h> #define N 105 #define P 1000000007 using namespace std; int n,m,k,t[N],ni,a[N]; void Add(int &x,int y){ x+=y; if(x>=P)x-=P; } int fpow(int a,int b){ int res=1; while(b){ if(b&1)res=1ll*res*a%P; a=1ll*a*a%P,b>>=1; }return res; } struct S{ int A[N][N]; S operator *(const S &_)const{ S res; for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)res.A[i][j]=0; for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)for(int k=1;k<=n;++k)res.A[i][j]=(res.A[i][j]+1ll*A[i][k]*_.A[k][j])%P; return res; } }ans,I; bool v[N][N]; int main(){ cin>>n>>m>>k;ni=fpow(2*m,P-2); for(int i=1;i<=n;++i)scanf("%d",&a[i]),ans.A[i][i]=1; for(int x,y,i=1;i<=m;++i)scanf("%d%d",&x,&y),v[x][y]=v[y][x]=1,++t[x],++t[y]; for(int i=1;i<=n;++i){ I.A[i][i]=1ll*(m*2-t[i]+P)*ni%P; for(int j=1;j<=n;j++)if(v[i][j])I.A[i][j]=ni; } for(int i=0;k;++i,I=I*I){ if(k&(1<<i))k^=(1<<i),ans=ans*I; }for(int i=1;i<=n;i++){ int res=0; for(int j=1;j<=n;j++)res=(res+1ll*a[j]*ans.A[i][j])%P;cout<<res<<"\n"; } }
//#include<math.h> #include<algorithm> #include<stdlib.h> #include<time.h> #include<stdio.h> #include<string.h> #define un unsigned #define srd srand(time(0)) #define ll long long #define con continue #define gtc getchar() #define ptc putchar #define dou double #define eps 0.00000000001 #define opr operator #define cl(x,a) memset(x,a,sizeof(x)) #define fo0(i,k) for(i=fr[k];i;i=nx[i]) #define fo1(i,l,r) for(i=l;i<=r;i++) #define fo2(i,l,r) for(i=l;i>=r;i--) #define fo(i,n) for(i=1;i<=n;i++) #define ret return #define x first #define cint const int #define y second #define opi(x) freopen(x,"r",stdin) #define opo(x) freopen(x,"w",stdout) #define tpl template<class T> #define priq priority_queue #define mp make_pair #define use using namespace #define WT while(T--) #define pb push_back #define sz size() use std; typedef pair<int,int> pii;typedef pair<int,ll> pil;typedef pair<ll,int> pli;typedef pair<ll,ll> pll; namespace io { void _(int &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;} void _(ll &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;} void _(char &c){while((c=gtc)==' '||c=='\n');}void _(dou &c){scanf("%lf",&c);}void _(char *s){char c;while((c=gtc)!=EOF&&c!=' '&&c!=10)*s++=c;} template<class t1,class t2>void _(t1 &a,t2 &b){_(a);_(b);}template<class t1,class t2,class t3>void _(t1 &a,t2 &b,t3 &c){_(a);_(b);_(c);} template<class t1,class t2,class t3,class t4>void _(t1 &a,t2 &b,t3 &c,t4 &d){_(a);_(b);_(c);_(d);} template<class t1,class t2,class t3,class t4,class t5>void _(t1 &a,t2 &b,t3 &c,t4 &d,t5 &e){_(a);_(b);_(c);_(d);_(e);} void _p(dou k){printf("%.6lf",k);}void _p(char *c){for(;*c;ptc(*c++));}void _p(const char *c){for(;*c;ptc(*c++));}void _p(char c){ptc(c);} tpl void _p0(T k){if(k>=10)_p0(k/10);ptc(k%10+'0');}tpl void _p(T k){if(k<0){ptc('-');_p0(-k);}else _p0(k);}tpl void __p(T k){_p(k);ptc(' ');} tpl void _pn(T k){_p(k);ptc('\n');}template<class t1,class t2>void _p(t1 a,t2 b){__p(a);_pn(b);} template<class t1,class t2,class t3>void _p(t1 a,t2 b,t3 c){__p(a);__p(b);_pn(c);} template<class t1,class t2,class t3,class t4>void _p(t1 a,t2 b,t3 c,t4 d){__p(a);__p(b);__p(c);_pn(d);} tpl void op(T *a,int n){int i;n--;fo(i,n)__p(a[i]);_pn(a[n+1]);}int gi(){int x;_(x);ret x;}ll gll(){ll x;_(x);ret x;} } int gcd(int a,int b){ret b?gcd(b,a%b):a;}void fcl(){fclose(stdin);fclose(stdout);} void fop(const char *s){char c[256],d[256];cl(c,0);cl(d,0);strcpy(c,s);strcpy(d,s);opi(strcat(c,".in"));opo(strcat(d,".out"));} int eq(dou a,dou b){return a+eps>=b&&b+eps>=a;}tpl void _ma(T &a,T b){if(a<b)a=b;}tpl void _mi(T &a,T b){if(a>b)a=b;} cint N=233333,EE=100000000,p=1000000007,ima=2147483647; use io; int n,m,c[22][22],f[N][17][17],v[22],po[22][N],sm; /*int cc(int t,int k) { int rt=0,i; if(k>m) return 0; int d=m-k; fo1(i,0,d) rt=(rt+(1-(d-i)%2*2)*1ll*po[i+k][t]*c[16-k][i]+(ll)p*p)%p; return rt; }*/ char a[N]; int main() { int i,j,k,a1,a2,an=0; scanf(" %s",a+1); n=strlen(a+1); fo(i,n) if(a[i]>='0'&&a[i]<='9') a[i]-='0'; else a[i]-='A'-10; _(m); fo1(i,0,16) c[i][0]=1; fo(i,16) fo(j,16) c[i][j]=c[i-1][j]+c[i-1][j-1]; fo1(i,1,16) { po[i][0]=1; fo(j,n) po[i][j]=(ll)po[i][j-1]*i%p; } fo1(i,0,m) f[0][i][i]=1; fo(i,n) fo(j,m) fo1(k,j,m) f[i][j][k]=((ll)f[i-1][j][k-1]*(17-k)+(ll)f[i-1][j][k]*k)%p; fo(i,n) an=(an+15ll*f[i-1][1][m])%p; fo(i,n) { fo1(j,a[i]+1,15) an=(an-f[n-i][sm+(!v[j])][m]+p)%p; sm+=!v[a[i]]; v[a[i]]=1; } _pn(an); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 998244353; const int N = 5007; int n, m, fn[30]; long long fac[2000006]; // 阶乘表 long long qpow(long long x, long long n) { long long res = 1; for (; n; n >>= 1, x = x * x % mod) if (n & 1) res = res * x % mod; return res; } long long inv(long long a) { // 返回逆元 费马小定理 return qpow(a, mod-2)%mod; } void solve() { // 计算阶乘表 fac[0] = 1; for(int i = 1;i <= 2000006; i++) { fac[i] = (fac[i-1]*i)%mod; } } long long comb(long long n, long long k) { if(k > n) return 0; return (fac[n]*inv(fac[k])%mod * inv(fac[n-k])%mod) % mod; } ll dp[N][N], C[N][N]; int cat[N][N], top = 1; ll dfs(int p, int sum, int cnt) { if (fn[p] > m) { if (sum == m) return 1; return 0; } if (sum > m) return 0; if (cnt > n) return 0; if (cnt * fn[p] > m) return 0; if (cat[p][cnt] == 0) cat[p][cnt] = top++; if (dp[sum][cat[p][cnt]] != -1) return dp[sum][cat[p][cnt]]; ll ans = 0; ans += dfs(p, sum , cnt + 2); ans %= mod; ans += dfs(p + 1, sum + fn[p] * cnt, 0) * C[n][cnt]; ans %= mod; return dp[sum][cat[p][cnt]] = ans; } int main() { solve(); fn[0] = 1; for (int i = 1; i <= 20; i++) { fn[i] = fn[i - 1] * 2; } cin >> n >> m; for (int i = 0; i <= n; i++) { C[n][i] = comb(n, i); } memset(dp, -1, sizeof(dp)); ll ans = dfs(0, 0, 0); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define int long long #define pi (3.141592653589) #define mod 1000000007 #define pb push_back #define mp make_pair #define ff first #define ss second #define all(v) v.begin(), v.end() #define beastslayer ios_base::sync_with_stdio(false), cin.tie(NULL); //typedef pair<int, int> pii; signed main(){ beastslayer; int n; cin>>n; string s; cin>>s; int q; cin>>q; int c=0; while(q--){ int m,a,b; cin>>m>>a>>b; if(m==2){ c++; } else{ if(c%2==1){ if(a>n) a-=n; else a+=n; if(b>n) b-=n; else b+=n; swap(s[a-1], s[b-1]); } else{ swap(s[a-1], s[b-1]); } } } if(c%2==1){ string ans=s.substr(n,2*n)+s.substr(0,n); s=ans; cout<<s<<endl; } else cout<<s<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { ios_base::sync_with_stdio(false);cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n; cin>>n; if(n%2==0){ cout<<"White"; } else{ cout<<"Black"; } return 0; }
#include <bits/stdc++.h> #define repd(i, a, b) for (ll i = (a); i < (b); i++) #define repb(i, n) for (ll i = (n)-1; i >= 0; i--) #define rep(i, n) repd(i, 0, n) using namespace std; using ll = long long; using ul = unsigned long long; using ld = long double; const ul mod = 1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; if (n % 2 == 0) { cout << "White" << endl; } else { cout << "Black" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 35; ll dp[N][N],a,b,k; string dfs(ll a,ll b,ll k){ if (a==0) return string(b,'b'); if (b==0) return string(a,'a'); if (dp[a-1][b]>=k){ return "a"+dfs(a-1,b,k); } else { return "b"+dfs(a,b-1,k-dp[a-1][b]); } } int main(){ cin>>a>>b>>k; dp[0][0]=1; for (int i=0;i<=a;i++){ for (int j=0;j<=b;j++){ if (i>0) dp[i][j]+=dp[i-1][j]; if (j>0) dp[i][j]+=dp[i][j-1]; } } cout<<dfs(a,b,k); return 0; }
#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #define rep(i,n) for(ll i=0;i<(n);++i) #define rep2(i,n) for(ll i=1;i<=(n);++i) #define rep3(i,i0,n) for(ll i=i0;i<(n);++i) #define rrep(i,n) for(ll i=((n)-1); i>=0; --i) #define rrep2(i,n) for(ll i=(n); i>0; --i) #define pb push_back #define mod 1000000007 #define fi first #define se second #define len(x) ((ll)(x).size()) using namespace std; using ll = long long; using ld = long double; using Pi = pair< ll, ll >; using vl = vector<ll>; using vc = vector<char>; using vb = vector<bool>; using vs = vector<string>; using vp = vector<Pi>; using vvc = vector<vector<char>>; using vvl = vector<vector<ll>>; using vvvl = vector<vector<vector<ll>>>; const ll INF = 1LL << 60; const ld PI = 3.1415926535897932385; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a, ll b) {return a/gcd(a,b)*b;} #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define mp make_pair void printb(ll N,ll d=16){ rep(i,d){ cout<<(N/(1<<(d-i-1)))%2; } cout<<endl; } void printv(vector<ll>a){ rep(i,a.size()){ if(i==a.size()-1){ cout<<a[i]<<endl; }else{ cout<<a[i]<<" "; } } } bool In_map(ll y,ll x,ll h,ll w){ if(y<0 || x<0 || y>=h || x>=w){ return 0; }else{ return 1; } } bool compare(Pi a, Pi b) { if(a.first != b.first){ return a.first < b.first; }else{ return a.second < b.second; } } //const vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1}; //const vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1}; const vector<ll> dx{1,0,-1,0}; const vector<ll> dy{0,1,0,-1}; const int MAX_N = 62; // n の最大値 vector<vector<long long>> com; // 前計算の結果を保存 // 動的計画法で前処理 void init() { com.assign(MAX_N, vector<long long>(MAX_N)); com[0][0] = 1; for (int i = 1; i < MAX_N; ++i) { com[i][0] = 1; for (int j = 1; j < MAX_N; j++) { com[i][j] = (com[i - 1][j - 1] + com[i - 1][j]); } } } // nCk を取得 long long nCk(int n, int k) { assert(!(n < k)); assert(!(n < 0 || k < 0)); return com[n][k]; } int main() { init(); ll A,B,K; cin>>A>>B>>K; string ans; K--; while(1){ rep(i,A+1){ //cout<<i<<" "<<K<<" "<<A<<endl; if(K>=com[B-1+i][i]){ K-=com[B-1+i][i]; }else{ rep(j,A-i){ ans.pb('a'); } ans.pb('b'); A-=A-i; B--; //cout<<"b"<<endl; break; } } if( B==0)break; } rep(i,A){ ans.pb('a'); } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define vec vector #define pii pair<int, int> using namespace std; const long long MAXVAL = (long long) 1e18 + 1; //const long long MOD = 1000000007ll; const long long MOD = 998244353ll; const int INF = 1000000001; long long poww(int x, long long pow) { if (pow == 0ll ) return 1; if (pow == 1ll) return x % MOD; long long ans = poww(x, pow >> 1) % MOD; ans = (ans * ans) % MOD; if ((pow & 1) == 1) { ans = (ans * x ) % MOD; } return ans % MOD; } ll gcd(ll x, ll y) { if (x == 0) return y; while (y) { x %= y; swap(x, y); } return x; } int main() { ios::sync_with_stdio(0); cin.tie(0); int q = 1; //cin >> q; while (q-- >0) { int n; cin >> n; int m; cin >> m; vec<int> primes; vec<int> minp(m + 1, -1); for (int i = 2; i <= m; ++i) { if (minp[i] < 0) { primes.push_back(i); } for (int j = 0; j < primes.size() && primes[j] <= i; ++j) { if (primes[j] * 1ll * i > m) break; minp[primes[j] * i] = j; } } vec<int> combs; combs.push_back(1); for (int i = 1; i <= 20; ++i) { int old = combs[i - 1]; int new_ = (old * 1ll * (i + n - 1)) % MOD; new_ = (new_ * 1ll * poww(i, MOD - 2)) % MOD; combs.push_back(new_); } int res = 0; for (int x = m; x > 0; --x) { int curres = 1; int y = x; while (minp[y] >= 0) { int p = primes[minp[y]]; assert (y % p == 0); int deg = 0; while (y % p == 0) { y /= p; ++deg; } curres = (curres * 1ll * combs[deg]) % MOD; } if (y > 1) { curres = (curres * 1ll * combs[1]) % MOD; } res = (res + curres) % MOD; } cout << res << "\n"; } }
#include <bits/stdc++.h> #pragma GCC optimize ("O2,unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<ll, ll> pll; #define debug(x) cerr<<#x<<'='<<(x)<<endl; #define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl; #define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl; #define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;} #define all(x) x.begin(), x.end() #define pb push_back #define kill(x) return cout<<x<<'\n', 0; const int inf=1000000010; const ll INF=1000000000000001000LL; const int mod=998244353; const int MAXN=300010, LOG=20; ll n, m, k, u, v, x, y, t, a, b, ans; ll F[MAXN], I[MAXN]; ll dp[MAXN][LOG]; ll powmod(ll a, ll b){ ll res=1; for (; b; b>>=1, a=a*a%mod) if (b&1) res=res*a%mod; return res; } ll nCr(ll n, ll r){ if (r<0 || r>n) return 0; return F[n]*I[r]%mod*I[n-r]%mod; } int main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); F[0]=1; for (int i=1; i<MAXN; i++) F[i]=F[i-1]*i%mod; I[MAXN-1]=powmod(F[MAXN-1], mod-2); for (int i=MAXN-1; i; i--) I[i-1]=I[i]*i%mod; cin>>n>>m; for (int i=1; i<=m; i++){ dp[i][1]++; for (int j=1; j<LOG; j++) if (dp[i][j]){ ans=(ans + dp[i][j]*nCr(n-1, j-1))%mod; for (int k=2*i; k<=m; k+=i) dp[k][j+1]=(dp[k][j+1] + dp[i][j])%mod; } } cout<<ans<<"\n"; return 0; }
#include<iostream> #include<vector> #include<string> #include<queue> #include<tuple> #define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i) #define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;} template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;} using state = tuple<ll, int, int>; const ll INF = 1LL << 50; int main() { cin.tie(0); ios::sync_with_stdio(false); int R, C; cin >> R >> C; vector<vector<int>> A(R, vector<int>(C - 1)); vector<vector<int>> B(R - 1, vector<int>(C)); rep(i, 0, R) rep(j, 0, C - 1) { cin >> A[i][j]; } rep(i, 0, R - 1) rep(j, 0, C) { cin >> B[i][j]; } priority_queue<state, vector<state>, greater<state>> que; vector<vector<ll>> dp(R, vector<ll>(C, INF)); dp[0][0] = 0; que.emplace(0, 0, 0); while (!que.empty()) { state now = que.top(); que.pop(); ll cost = get<0>(now); int r = get<1>(now); int c = get<2>(now); if (dp[r][c] < cost) { continue; } if (c + 1 < C && chmin(dp[r][c + 1], cost + A[r][c])) { que.emplace(dp[r][c + 1], r, c + 1); } if (c - 1 >= 0 && chmin(dp[r][c - 1], cost + A[r][c - 1])) { que.emplace(dp[r][c - 1], r, c - 1); } if (r + 1 < R && chmin(dp[r + 1][c], cost + B[r][c])) { que.emplace(dp[r + 1][c], r + 1, c); } rep(i, 0, r) { if (chmin(dp[r - i - 1][c], cost + 1 + i + 1)) { que.emplace(dp[r - i - 1][c], r - i - 1, c); } } } cout << dp[R - 1][C - 1] << endl; return 0; }
#include<bits/stdc++.h> #define ll long long #define N 400005 using namespace std; int read() { int x=0,f=1;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int n,m,K,cnt,h[N],c[N],fp[N]; struct Edge { int to,next; }a[N*2]; void Add(int x,int y) { cnt++;a[cnt].to = y;a[cnt].next = h[x];h[x] = cnt; } int ind; ll ans = 0; #define inf 0x7fffffff/2 queue<int>q; int d[20][N],inq[N]; void bfs(int id) { for(int i=1; i<=n; ++i)d[id][i] = inf; q.push(c[id]);d[id][c[id]] = 0; inq[c[id]] = 1; while(!q.empty()) { int x = q.front();q.pop(); for(int i=h[x]; i; i=a[i].next) { int y = a[i].to; if(!inq[y])d[id][y] = d[id][x]+1,q.push(y),inq[y] = 1; } } memset(inq,0,sizeof(inq)); } ll f[1<<17][18]; int sta; int fl; void deal() { // fl++; // if(fl>=20) // { // vector<int>v; // cout<<v[2313132]<<endl; // exit(0); // } for(int i=1; i<=K; ++i)bfs(i); // for(int i=1; i<=K; ++i){ // // for(int j=1; j<=n; ++j) // cout << d[i][j] << ' '; // cout << '\n'; // } memset(f,0x3f,sizeof(f)); for(int i=1; i<=K; ++i)f[1<<i-1][i] = 0; for(int i=0;i<(1<<K);i++) { for(int j=1;j<=K;j++) { if(f[i][j]>1e9)continue; for(int t=1;t<=K;t++) { if(i&(1<<t-1))continue; f[i+(1<<t-1)][t]=min(f[i+(1<<t-1)][t],f[i][j]+d[j][c[t]]); } } } for(int j=1;j<=K;j++) { ans=min(ans,f[(1<<K)-1][j]); } } bool vst[N]; void dfs(int x) { vst[x] = 1; if(fp[x])ind |= 1 << fp[x]-1; for(int i=h[x]; i; i=a[i].next) { int y = a[i].to; if(!vst[y])dfs(y); } } int main() { n = read();m = read(); for(int i=1; i<=m; ++i) { int x = read(),y = read(); Add(x,y);Add(y,x); } K = read();sta = 0; for(int i=1; i<=K; ++i) { sta |= 1<<i-1; } ans = 1LL<<40; for(int i=1; i<=K; ++i)c[i] = read(),fp[c[i]] = i; for(int i=1; i<=n; ++i)if(!vst[i]) { ind = 0; dfs(i); if(ind == sta) { deal(); for(int j=1;j<=K;j++)if(!vst[c[j]])exit(0); } } if(ans >= 1e9)puts("-1"); else cout << ans+1; }
//Bismillahir Rahmanir Rahim #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef vector<int> vi; typedef vector<long long> vll; typedef vector<pair<int, int>> vpi; #define pb push_back #define pf push_front #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define ff first #define ss second #define mp make_pair #define lb lower_bound #define ub upper_bound #define Tcase() int tcc; cin >> tcc; while(tcc--) #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define F0R(i, a) FOR(i, 0, a) const ll MOD = 998244353; const ll INF = 9e18; const int MX = 2e5 + 5; const ld PI = acos((ld) -1); const int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1}; const int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1}; void setIO(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); if(sz(name)){ freopen((name+".in").c_str(), "r", stdin); freopen((name+".out").c_str(), "w", stdout); } } int Dp[5005][5005]; int main(){ setIO(); int n, m; cin >> n >> m; int a[n], b[m]; F0R(i, n) cin >> a[i]; F0R(i, m) cin >> b[i]; F0R(i, n + 1){ F0R(j, m + 1){ Dp[i][j] = MOD; if(!i) Dp[i][j] = j; if(!j) Dp[i][j] = i; } } FOR(i, 1, n + 1){ FOR(j, 1, m + 1){ if(a[i - 1] == b[j - 1]) Dp[i][j] = Dp[i - 1][j - 1]; else Dp[i][j] = min({Dp[i - 1][j], Dp[i][j - 1], Dp[i - 1][j - 1]}) + 1; } } cout << Dp[n][m]; }
//OM TAT SAT #include<bits/stdc++.h> #define N 100010 using namespace std; int a[N],b[N]; int main() { int n; cin>>n; for(int i=0;i<n;i++) { cin>>a[i]>>b[i]; }//cout<<aa<<bb<<endl; int ans=10000000,p; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { //ans=min(ans,i==j? a[i]+b[j] :max(a[i],b[j])); if(i==j) p=a[i]+b[j]; else p=max(a[i],b[j]); ans=min(ans,p); } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int N = 3010; int f[N][N]; int main(){ #ifndef ONLINE_JUDGE freopen("E:\\in.txt","r", stdin); #endif int n, k; cin >> n >> k; f[0][0] = 1; for(int i = 1; i <= n; i++){ for(int j = n; j >= 1; j--){ f[i][j] = f[i-1][j-1]; if(i >= 2 * j) f[i][j] = (f[i][j] + f[i][2*j]) % mod; } } cout << f[n][k] << endl; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(ll i = 0; i < (n); ++i) #define FOR(i, m, n) for(ll i = m; i < (n); i++) #define all(x) (x).begin(),(x).end() using namespace std; typedef long long ll; using vi = vector<int>; using vii = vector<vi>; using pii = pair<int, int>; using vl = vector<ll>; using vll = vector<vl>; using pll = pair<ll, ll>; const ll MOD = 1000000007; //a^n mod ll modpow(ll a, ll n){ ll res = 1; while(n > 0){ if(n & 1) res = res * a % MOD; a = a * a % MOD; n >>= 1; } return res; } int main() { ll N, P; cin>>N>>P; ll ans = (P-1) * modpow(P-2, N-1) % MOD; cout << ans; }
//Somos insignificantes. Por mais que você programe sua vida, a qualquer momento tudo pode mudar. // If you have God on your side,everything becomes clear #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 tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int MAX_N = 1e5 + 5; const int MAX_L = 20; // ~ Log N const long long MOD = 1e9 + 7; const long long INF = 1e9 + 7; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> ii; typedef vector<ii> vii; typedef vector<vi> vvi; #define fi first #define popcount(x) __builtin_popcountll(x) #define se second #define LSOne(S) (S & (-S)) #define isBitSet(S, i) ((S >> i) & 1) /*void Sieve(int n) { //bool prime[n+1]; //memset(prime, true, sizeof(prime)); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for (int i=p*p; i<=n; i += p) prime[i] = false; } } } */ /* struct cmp{ bool operator()(const pair<int,int>& v1,const pair<int,int>& v2) const{ }};*/ bool comp(pair<string,int>a,pair<string,int>b) { if(a.second>b.second) return true; else return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin>>n; vector<pair<string,int>>city; for(int i=0;i<n;i++) { string s; int h; cin>>s>>h; city.push_back({s,h}); } sort(city.begin(),city.end(),comp); cout<<city[1].first<<endl; }
#include <bits/stdc++.h> using namespace std; #define m_p make_pair #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define fi first #define se second typedef long long ll; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); int gcd(int x, int y) { if (x == 0) return y; return gcd(y % x, x); } void solv() { int n; scanf("%d", &n); int ans = 0; while (n--) { int x; scanf("%d", &x); ans = gcd(ans, x); } printf("%d\n", ans); } int main() { #ifdef SOMETHING freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif // SOMETHING //ios_base::sync_with_stdio(false), cin.tie(0); solv(); return 0; } //while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
#include <bits/stdc++.h> using namespace std; #include <math.h> #include <iomanip> #include <cstdint> template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF=1001001001; const int mod = 1000000007; int main() { int N; cin>>N; vector<int>type(2*N);//iが誰かの 始点-1 終点1 なんでもいい 0 になっているか vector<vector<int>>p(2*N,vector<int>(2*N));//i,jを ペアにしていい 1,0 ダメ -1 vector<int>xs,ps; for(int i=0;i<N;i++){ int a,b; cin>>a>>b; if(a!=-1){ a--; if(type[a]!=0){cout<<"No"<<endl;return 0;} type[a]=1; xs.push_back(a); } if(b!=-1){ b--; if(type[b]!=0){cout<<"No"<<endl;return 0;} type[b]=-1; xs.push_back(b); } if(a!=-1&&b!=-1){p[a][b]=1;ps.push_back(a);ps.push_back(b);} } for(int a:xs){//xs:-1ではない物->それ同士は(元からペアになっているものを除いて)ペアにならない。 for(int b:xs){ if(p[a][b]!=1){p[a][b]=-1;} } } for(int a:ps){//ps:元からペアになっているものの片方。!p[a][i]:1ではないもの。元からペアのものがある場合は、その相手以外はペアにできないようにする。 for(int i=0;i<2*N;i++){ if(!p[a][i]){p[a][i]=-1;} if(!p[i][a]){p[i][a]=-1;} } } vector<bool>dp(2*N+1,false); dp[0]=true; for(int i=0;i<2*N;i+=2){ if(!dp[i]){continue;} for(int j=1;j<=N;j++){ int ni=i+j*2; if(ni>2*N){break;} bool ok=true; for(int l=i;l<i+j;l++){ int r=l+j; if(type[l]==-1){ok=false;} if(type[r]==1){ok=false;} if(p[l][r]==-1){ok=false;} } if(ok){dp[ni]=1;} } } if(dp[2*N]){cout<<"Yes"<<endl;} else{cout<<"No"<<endl;} return 0; }//お試し
#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 int main() { int N; cin >> N; V<int> A(N), B(N); int sz = N * 2; V<int> tp(sz), com(sz, -1); bool ng = false; rep(i, N) { cin >> A[i] >> B[i]; if (A[i] != -1) { --A[i]; if (tp[A[i]]) ng = true; tp[A[i]] = i + 1; } if (B[i] != -1) { --B[i]; if (tp[B[i]]) ng = true; tp[B[i]] = -(i + 1); } if (A[i] != -1 && B[i] != -1) { com[A[i]] = B[i]; com[B[i]] = A[i]; } } if (ng) { puts("No"); return 0; } V<bool> dp(sz + 1); dp[0] = true; rep(i, sz) { if (!dp[i]) continue; for (int j = i + 1; j < sz; ++j) { int w = j - i + 1; if (w & 1) continue; w /= 2; bool ok = true; V<bool> exist(N); rep(k, w) { int p = i + k, q = i + k + w; if (com[p] != -1 && !(i <= com[p] && com[p] <= j)) { ok = false; } if (com[q] != -1 && !(i <= com[q] && com[q] <= j)) { ok = false; } bool same = true; if (tp[p] != 0 && tp[q] != 0) { if (tp[p] < 0 || tp[p] + tp[q] != 0) { ok = false; } else same = true; } if (tp[p] < 0 || tp[q] > 0) { ok = false; } else { if (tp[p] != 0) { int v = tp[p] - 1; if (exist[v]) { ok = false; } exist[v] = true; } if (!same && tp[q] != 0) { int v = -tp[q] - 1; if (exist[v]) { ok = false; } exist[v] = true; } } } if (ok) { dp[j + 1] = true; } } } puts(dp[sz] ? "Yes" : "No"); return 0; }
// by googol_S0 #include<bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=998244353; ll P[5050][5050]; int main(){ ll M,N; cin>>M>>N; for(ll i=0;i<=N;i++)P[i][0]=1; for(ll i=0;i<=N;i++){ for(ll j=0;j<M;j++){ P[i][j+1]=P[i][j]*i%mod; } } ll ANS=0; for(ll x=1;x<=N;x++){ ANS+=(P[N-x+1][M]-P[N-x][M]+mod); } ANS%=mod; for(ll r=0;r<M-1;r++){ for(ll x=1;x<=N;x++){ ANS+=(((P[N-x+1][r+1]-P[N-x][r+1]+mod)*(x-1)%mod)*(P[N][M-(r+2)]))%mod; } ANS%=mod; } for(ll l=1;l<M;l++){ for(ll x=1;x<=N;x++){ ANS+=(((P[N-x+1][M-l]-P[N-x][M-l]+mod)*(x-1)%mod)*(P[N][l-1]))%mod; } ANS%=mod; } for(ll k=1;k<M;k++){ ll X=M-1-k; for(ll x=1;x<=N;x++){ ANS+=X*(((P[N-x+1][k]-P[N-x][k]+mod)*(((x-1)*(x-1))%mod)%mod)*(P[N][M-(k+2)])%mod)%mod; } ANS%=mod; } cout<<ANS<<"\n"; }
#include <iostream> constexpr int N = 200005; constexpr int p = 998244353; int add(int x, int y) { return (x += y) >= p ? x - p : x; } int mul(int x, int y) { return (long long)x * y % p; } int power(int x, int y) { int ans = 1; for (; y; y >>= 1) { if (y & 1) ans = mul(ans, x); x = mul(x, x); } return ans; } int n, m; int f[N], primes[N], powcnt[N], inv[N], cnt; bool vis[N]; int main() { std::ios::sync_with_stdio(false), std::cin.tie(nullptr); std::cin >> n >> m; inv[1] = 1; for (int i = 2; i <= m; ++i) inv[i] = mul(inv[p % i], p - p / i); f[1] = 1; vis[1] = true; for (int i = 2; i <= m; ++i) { if (!vis[i]) primes[++cnt] = i, f[i] = n, powcnt[i] = 1; for (int j = 1; j <= cnt && i * primes[j] <= m; ++j) { vis[i * primes[j]] = true; if (!(i % primes[j])) { powcnt[i * primes[j]] = powcnt[i] + 1; f[i * primes[j]] = mul(f[i], mul(n + powcnt[i * primes[j]] - 1, inv[powcnt[i * primes[j]]])); break; } else { powcnt[i * primes[j]] = 1; f[i * primes[j]] = mul(f[i], f[primes[j]]); } } } int ans = 0; for (int i = 1; i <= m; ++i) ans = add(ans, f[i]); std::cout << ans << '\n'; return 0; }
# include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; int main() { ll a, b, c, d; cin >> a >> b >> c >> d; if (b/c >= d) { cout << -1 << '\n'; } else { ll y = (d*c)-b; ll x = (a/y); if (((b*x)+a) <= (d * x * c)) { cout << x << '\n'; } else { cout << x + 1 << '\n'; } } return 0; }
#include<bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { T f = 1; x = 0; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(char(x % 10 + 48)); } template <typename T> void writeln(T x, char ch = '\n') { write(x); putchar(ch); } int n, m, k; int main() { read(n); read(m); read(k); cout << (n - m + k) << endl; return 0; }
#include<algorithm> #include<bitset> #include<cmath> #include<complex> #include<deque> #include<functional> #include<iomanip> #include<iostream> #include<iterator> #include<map> #include<numeric> #include<queue> #include<set> #include<stack> #include<string> #include<unordered_map> #include<unordered_set> #include<utility> #include<vector> #include<chrono> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define FOR(i,x,n) for(int i=x; i<(n); i++) #define vint(a,n) vint a(n); rep(i, n) cin >> a[i]; #define vll(a,n) vll a(n); rep(i, n) cin >> a[i]; #define ALL(n) begin(n),end(n) #define RALL(n) rbegin(n),rend(n) #define MOD (1000000007) // #define MOD (998244353) #define INF (1e9+7) #define INFL (2e18) typedef long long ll; typedef unsigned int ui; typedef unsigned long long ull; using vint=vector<int>; using vll=vector<ll>; using vbool=vector<bool>; using P=pair<ll,ll>; template<class T>using arr=vector<vector<T>>; template<class T>int popcount(T &a){int c=0; rep(i, 8*(int)sizeof(a)){if((a>>i)&1) c++;} return c;} template<class T>void pl(T x){cout << x << " ";} template<class T>void pr(T x){cout << x << endl;} template <class T, class... Ts> inline void pr(T Tar, Ts... ts) { std::cout << Tar << " "; pr(ts...); return; } template<class T>void prvec(vector<T>& a){if(a.size()>0){rep(i, (int)a.size()-1){pl(a[i]);} pr(a.back());}else{pr("");}} template<class T>void prarr(arr<T>& a){rep(i, (int)a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);} template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){fill( (T*)array, (T*)(array+N), val );} int main() { ll n; cin >> n; vll(a, n); ll ans = INF; if(n==1){ pr(a[0]); return 0; } rep(bit, 1<<(n-1)){ // if(bit==0) continue; ll res = 0; ll t = 0; rep(i, n){ t |= a[i]; if(i==n-1) break; if((bit>>i)&1){ res ^= t; t = 0; } } res ^= t; chmin(ans, res); } pr(ans); return 0;}
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- int mod = 1000000007; int add(int x, int y) { return (x += y) >= mod ? x - mod : x; } template<class... T> int add(int x, T... y) { return add(x, add(y...)); } int mul(int x, int y) { return 1LL * x * y % mod; } template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); } int sub(int x, int y) { return add(x, mod - y); } int modpow(int a, long long b) { int ret = 1; while (b > 0) { if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1; } return ret; } int modinv(int a) { return modpow(a, mod - 2); } typedef vector<int> Vec; typedef vector<Vec> Mat; Vec mulMatVec(Mat a, Vec b) { int n = b.size(); Vec ret(n, 0); rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j])); return ret; } Mat mulMatMat(Mat a, Mat b) { int n = a.size(); Mat ret(n, Vec(n, 0)); rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j])); return ret; } Mat fastpow(Mat x, ll n) { Mat ret(x.size(), Vec(x.size(), 0)); rep(i, 0, x.size()) ret[i][i] = 1; while (0 < n) { if ((n % 2) == 0) { x = mulMatMat(x, x); n >>= 1; } else { ret = mulMatMat(ret, x); --n; } } return ret; } void printVec(Vec a) { cout << "[\t"; rep(i, 0, a.size()) cout << a[i] << "\t"; cout << "]" << endl; } void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); } /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan0     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, M, K, A[101]; int X[10101], Y[10101]; int cnt[101]; bool E[101][101]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> M >> K; rep(i, 0, N) cin >> A[i]; rep(i, 0, M) cin >> X[i] >> Y[i], X[i]--, Y[i]--; Mat Ma(N, Vec(N)); rep(i, 0, M) { cnt[X[i]]--; cnt[Y[i]]--; E[X[i]][Y[i]] = E[Y[i]][X[i]] = true; } rep(to, 0, N) { int cnt = M; rep(from, 0, N) if (to != from) { if (E[to][from]) { Ma[to][to] = add(Ma[to][to], mul(modinv(M), modinv(2))); Ma[to][from] = add(Ma[to][from], mul(modinv(M), modinv(2))); cnt--; } } Ma[to][to] = add(Ma[to][to], mul(cnt, modinv(M))); } Vec v(N, 0); rep(i, 0, N) v[i] = A[i]; Ma = fastpow(Ma, K); v = mulMatVec(Ma, v); rep(i, 0, N) cout << v[i] << endl; }
#include <iostream> #include<bits/stdc++.h> using namespace std; #define int long long #define iso ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); int32_t main() { int n; cin >> n; cout<<n/100+(n%100==0?0:1)<<endl ; }
// #include <bits/stdc++.h> #include <iostream> // cout, endl, cin #include <string> // string, to_string, stoi #include <vector> // vector #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <utility> // pair, make_pair #include <tuple> // tuple, make_tuple #include <cstdint> // int64_t, int*_t #include <cstdio> // printf #include <map> // map #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <deque> // deque #include <unordered_map> // unordered_map #include <unordered_set> // unordered_set #include <bitset> // bitset #include <cctype> // isupper, islower, isdigit, toupper, tolower // bits default for atcoder // additional // #include <array> // array #include <numeric> // accumulate // macro #define rep(i, n) for (int (i)=0;(i)<(n);(i)++) using namespace std; using ll = long long; int main() { int N; cin >> N; cout << ((N-1) / 100) + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; #define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define test() int t;cin>>t;for(int test=1;test<=t;test++) #define pb push_back #define nl cout<<"\n" #define F first #define S second #define all(x) x.begin(),x.end() template<class C> void min_self( C &a, C b ){ a = min(a,b); } template<class C> void max_self( C &a, C b ){ a = max(a,b); } const ll MOD = 1000000007; ll mod( ll n, ll m=MOD ){ n%=m,n+=m,n%=m;return n; } const int MAXN = 1e5+5; const int LOGN = 21; const ll INF = 1e14; int dx[] = {1,0,-1,0}; int dy[] = {0,1,0,-1}; template<class T1, class T2> void add( T1 &x, T2 y, ll m = MOD ) { x += y; if( x >= m ) x -= m; } template<class T1, class T2> void sub( T1 &x, T2 y, ll m = MOD ) { x -= y; if( x < 0 ) x += m; } int dp[1002][1002]; int main() { #ifdef gupta_samarth freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastio(); int a,b,w; cin>>a>>b>>w; w *= 1000; int mn = MOD, mx = 0; for(int i=w/b;i<=(w+a-1)/a;i++) { if( a*i <= w && w <= b*i ) { mn = min(mn, i); mx = max(mx, i); } } if( mn == MOD ) { cout<<"UNSATISFIABLE",nl; return 0; } cout<<mn<<" "<<mx,nl; cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <iostream> using namespace std; int A, B; double W; int isin(double N) { if(N >= A and N <= B) { return true; } else { return false; } } int main() { cin >> A >> B >> W; int max = 0; int min = 0; for (int i = 1; i <= W * 1000; i++) { double pre = W * 1000 / i; if(isin(pre)) { min = i; break; } } for (int i = W * 1000; i > 0; i--) { double pre = W * 1000 / i; if(isin(pre)) { max = i; break; } } if (min == 0) { min = max; } if (max == 0) { max = min; } if (min == 0 and max == 0) { cout << "UNSATISFIABLE" << endl; } else { cout << min << " " << max << endl; } }
#include <bits/stdc++.h> #define ll long long #define ls id << 1 #define rs id << 1 | 1 #define mem(array, value, size, type) memset(array, value, ((size) + 5) * sizeof(type)) #define memarray(array, value) memset(array, value, sizeof(array)) #define fillarray(array, value, begin, end) fill((array) + (begin), (array) + (end) + 1, value) #define fillvector(v, value) fill((v).begin(), (v).end(), value) #define pb(x) push_back(x) #define st(x) (1LL << (x)) #define pii pair<int, int> #define mp(a, b) make_pair((a), (b)) #define Flush fflush(stdout) #define vecfirst (*vec.begin()) #define veclast (*vec.rbegin()) #define vecall(v) (v).begin(), (v).end() #define vecupsort(v) (sort((v).begin(), (v).end())) #define vecdownsort(v, type) (sort(vecall(v), greater<type>())) #define veccmpsort(v, cmp) (sort(vecall(v), cmp)) using namespace std; const int N = 500050; const int inf = 0x3f3f3f3f; const ll llinf = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const int MOD = 1e9 + 7; const double PI = acos(-1.0); clock_t TIME__START, TIME__END; void program_end() { #ifdef ONLINE printf("\n\nTime used: %.6lf(s)\n", ((double)TIME__END - TIME__START) / 1000); system("pause"); #endif } ll n; vector<ll> ans; inline void solve() { cin >> n; for (ll i = 1; i * i < n; ++i) { if (n % i == 0) { // cout << i << endl; // cout << n / i << endl; ans.push_back(i), ans.push_back(n / i); } } if ((ll)sqrt(n) * (ll)sqrt(n) == n) { // cout << (ll)sqrt(n) << endl; ans.push_back((ll)sqrt(n)); } vecupsort(ans); for(auto i :ans) cout << i << '\n'; } int main() { TIME__START = clock(); int Test = 1; // scanf("%d", &Test); while (Test--) { solve(); // if (Test) // putchar('\n'); } TIME__END = clock(); program_end(); return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define DB double #define U unsigned #define P std::pair #define LL long long #define LD long double #define pb push_back #define MP std::make_pair #define SZ(x) ((int)x.size()) #define all(x) x.begin(),x.end() #define CLR(i,a) memset(i,a,sizeof(i)) #define FOR(i,a,b) for(int i = a;i <= b;++i) #define ROF(i,a,b) for(int i = a;i >= b;--i) #define DEBUG(x) std::cerr << #x << '=' << x << std::endl int c[(1<<8)+5][(1<<8)+5]; bool ok[(1<<8)+5]; int main(){ int n;scanf("%d",&n); if(n == 1){ printf("1\nAB\n"); return 0; } printf("%d\n",(1<<n)-1); FOR(i,1,(1<<n)-1){ FOR(j,0,(1<<n)-1) ok[j] = __builtin_popcount(i&j)&1; FOR(i,0,(1<<n)-1) printf("%c","AB"[ok[i]]);puts(""); FOR(i,0,(1<<n)-1) FOR(j,i+1,(1<<n)-1) if(ok[i] != ok[j]) ++c[i][j]; } int tt = c[0][1]; FOR(i,0,(1<<n)-1) FOR(j,i+1,(1<<n)-1) assert(c[i][j] == tt); return 0; }
/* Author : Mychecksdead */ #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef unsigned long long int ull; #define MOD (1000000000+7) #define MOD1 (998244353) #define PI 3.1415926535 #define pb push_back #define for0(i, n) for(ll i = 0; i < n; i++) #define for1(i, n) for(ll i = 1; i <= n; i++) #define test int T; cin >> T; #define mid (l+r)/2 const int N = 1e5; int n, q; ll arr[N], el, k; set<pair<ll,ll>> s; void solve(){ cin >> n >> q; for0(i, n) cin >> arr[i]; for(int i = 0; i < n; i++){ s.insert({arr[i] - i - 1, arr[i]}); } while(q--){ cin >> k; auto it = s.upper_bound({k, -1e9}); if(it == s.begin()){ cout << k << '\n'; }else{ it--; pair<ll, ll> p = (*it); cout << p.second + k - p.first << '\n'; } } } int main(){ cin.tie(0); ios::sync_with_stdio(0); // test; int T = 1; while(T--){ solve(); } return 0; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) int main() { ll N,M,T; cin>>N>>M>>T; ll n = N, l=0; rep(i, M){ ll A,B; cin>>A>>B; n = max(n-(A - l), (ll)0); if(n <= 0){ cout << "No" << endl; return 0; } n = min(n+(B-A), N); l = B; } cout << (n-(T-l) <= 0 ? "No" : "Yes") << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; //constexpr ll MOD = 1e9 + 7; constexpr ll MOD = 998244353; //constexpr ll MOD = ; ll mod(ll A, ll M) {return (A % M + M) % M;} constexpr ll INF = 1LL << 60; template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;} template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;} ll divceil(ll A, ll B) {return (A + (B - 1)) / B;} #define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false) ll loopcnt = 0; vector<vector<ll>> G; vector<ll> visited; void dfs(ll sv, ll V) { if (visited.at(V) != -1) { if (visited.at(V) == sv) { //cerr << sv << " " << V << endl; loopcnt++; } return; } visited.at(V) = sv; for (auto nv : G.at(V)) { dfs(sv, nv); } } int main() { ll N; cin >> N; G.resize(N); for (ll u = 0; u < N; u++) { ll v; cin >> v; v--; G.at(u).push_back(v); } visited.resize(N); fill(visited.begin(), visited.end(), -1); for (ll v = 0; v < N; v++) { if (visited.at(v) == -1) dfs(v, v); } ll ans = 1; for (ll i = 0; i < loopcnt; i++) { ans *= 2, ans %= MOD; } ans = mod(ans - 1, MOD); cout << ans << endl; //cerr << loopcnt << endl; }
// We can calculate the number of inversions in the original array in a number of ways, such as with a Fenwick tree or with merge sort. // Now, notice that each of the resulting sequences essentially remove the first element of the previous sequence and insert it at the end of it. // This changes the number of inversions in the new sequence by n−1−2ai for each i from 0 to N−2. // Time complexity: O(NlogN for calculating inversions ) #include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = (int)3e5+5; int n,a[maxn],b[maxn],c[maxn]; ll find_inversions(int l, int r) { if (l == r) return 0; int m = l+ ((r-l)>>1), x = l, y = m+1; ll ans = find_inversions(l, m) + find_inversions(m+1, r); for (int i = l; i <= r; i++) { if (x > m) c[i] = b[y++]; else if (y>r) c[i] = b[x++]; else if (b[x]>b[y]) { c[i] = b[y++]; ans += m-x+1; } else c[i] = b[x++]; } for (int i = l; i <= r; i++) b[i] = c[i]; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } ll cur = find_inversions(0,n-1); cout << cur << "\n"; for (int i = 0; i+1 < n; i++) { cur += n-1 - 2*a[i]; cout << cur << "\n"; } return 0; }
/*      />  フ      |  _  _|      /`ミ _x 彡      /      |     /  ヽ   ?  / ̄|   | | |  | ( ̄ヽ__ヽ_)_)  \二つ */ #include <queue> #include <vector> #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define MP make_pair #define ll long long #define fi first #define se second using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (;!isdigit(c);c=getchar()) if (c=='-') f=1; for (;isdigit(c);c=getchar()) x=x*10+(c^48); if (f) x=-x; } template<typename F> inline void write(F x, char ed = '\n') { static short st[30];short tp=0; if(x<0) putchar('-'),x=-x; do st[++tp]=x%10,x/=10; while(x); while(tp) putchar('0'|st[tp--]); putchar(ed); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } const int N = 5005; char s[N]; int n, T; char t[] = "aatcoder"; int main() { for (read(T); T; T--) { scanf ("%s", s + 1), n = strlen(s + 1); bool fl = 0; int tp = 8; for (int i = 1;i <= min(n, 7); i++) if (s[i] > t[i]) { write(0); fl = 1; break; } else if (s[i] < t[i]) { tp = i; break; } if (fl) continue; fl = 0; for (int i = 1;i <= n; i++) if (s[i] != 'a') fl = 1; if (!fl) { write(-1); continue; } if (tp == 8 && n > 7) { write(0); continue; } if (tp > 2) { write(1); continue; } for (int i = 1;i <= n; i++) if (s[i] != 'a') { write(s[i] > 't' ? i - 2 : i - 1); break; } } return 0; } /* atcodera */
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using Graph = vector<vector<int>>; #define rep(i, n) for (int i = 0; i < n; ++i) ll gcd(ll a, ll b){ if(a%b == 0) return b; else return gcd(b,a%b); } ll lcm(ll a, ll b){ return a*b / gcd(a,b); } int main(){ int h,w,x,y; cin >> h >> w >> x >> y; x--; y--; vector<string> s(h); rep(i,h) cin >> s[i]; int cnt = 0; for(int i=x;i<h;i++){ if(s[i][y] == '#') break; if(s[i][y] == '.') cnt++; } for(int i=x;i>=0;i--){ if(s[i][y] == '#') break; if(s[i][y] == '.') cnt++; } for(int j=y;j<w;j++){ if(s[x][j] == '#') break; if(s[x][j] == '.') cnt++; } for(int j=y;j>=0;j--){ if(s[x][j] == '#') break; if(s[x][j] == '.') cnt++; } cout << cnt - 3 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; typedef vector<ll> vl; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<char> vc; typedef queue<ll> ql; typedef deque<ll> dql; typedef priority_queue<ll> pql; typedef set<ll> sl; typedef pair<ll, ll> pl; typedef vector<vl> vvl; typedef vector<pl> vpl; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++) #define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--) #define all(v) (v).begin(), (v).end() ll mod(ll a, ll b) {if(b == 0) return 0; return (a % b + b) % b;} bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;} bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;} const ll INF = 1LL << 60; const ll MOD = 1e9 + 7; //const ll MOD = 998244353; const ll MAX = 1e3; const ld eps = 1e-9; const char newl = '\n'; vl parent(MAX), depth(MAX); ll find(ll x) {if(parent[x]==x) return x; return parent[x] = find(parent[x]);} void unite(ll x, ll y) { x = find(x); y = find(y); if (x==y) return; if(depth[x] < depth[y]) { parent[x] = y; }else { parent[y] = x; if(depth[x] == depth[y]) depth[x]++; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); ll n; cin >> n; vl x(n), y(n); rep(i, n) cin >> x[i] >> y[i]; ll ok=0, ng=40001; while(ng-ok>1) { ll RR = (ok+ng)/2; rep(i, n) {parent[i] = i; depth[i] = 0;} rep2(i, 200-99, 200+99) {parent[i] = 200; depth[i] = 0;} rep2(i, 400-99, 400+99) {parent[i] = 400; depth[i] = 0;} rep(i, n) { if((100+y[i])*(100+y[i]) < RR) unite(i, x[i]+200); if((100-y[i])*(100-y[i]) < RR) unite(i, x[i]+400); } rep(i, n-1) rep2(j, i+1, n-1) { ll dx = x[j]-x[i], dy = y[j]-y[i]; if(dx*dx+dy*dy < RR) unite(i, j); } if(find(200)==find(400)) ng = (ok+ng)/2; else ok = (ok+ng)/2; } cout << sqrt(ok)/2 << newl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); i++) using namespace std; using ll = long long; using P = pair<int, int>; using Graph = vector<vector<int>>; vector<bool> seen; vector<int> color; vector<int> ans; vector<int> c; Graph G; void dfs(const Graph &G, int v) { // cout << "v = " << v << " " << color[c[v]] << endl; seen[v] = true; if (color[c[v]] == 0) ans.push_back(v+1); color[c[v]]++; // cout << "v = " << v << " " << " c[v] = " << c[v] << " " << " color[v] = " << color[c[v]] << endl; for (auto next_v : G[v]) { if (seen[next_v]) continue; dfs(G, next_v); // 再帰的に探索 } color[c[v]]--; } int main(void) { int n; cin >> n; c.resize(n); rep(i, n) cin >> c[i]; G.resize(n); rep(i, n-1) { int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } seen.assign(n, false); color.assign(100010, 0); dfs(G, 0); sort(ans.begin(), ans.end()); rep(i, (int)ans.size()) { cout << ans[i] << endl; } return 0; }
#line 1 "/workspaces/compro/lib/template.hpp" #line 1 "/workspaces/compro/lib/io/vector.hpp" #include <iostream> #include <vector> #ifndef IO_VECTOR #define IO_VECTOR template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { int size = v.size(); for (int i = 0; i < size; i++) { std::cout << v[i]; if (i != size - 1) std::cout << " "; } return out; } template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) { for (auto &el : v) { std::cin >> el; } return in; } #endif #line 4 "/workspaces/compro/lib/template.hpp" #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(v) (v).begin(), (v).end() #define coutd(n) cout << fixed << setprecision(n) #define ll long long int #define vl vector<ll> #define vi vector<int> #define MM << " " << using namespace std; template <class T> void say(bool val, T yes, T no) { cout << (val ? yes : no) << "\n"; } void say(bool val, string yes = "Yes", string no = "No") { say<string>(val, yes, no); } 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; } // C++ 17に完全移行したら消す // 最大公約数を求める template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; } // 最小公倍数を求める template <class T> T lcm(T n, T m) { T g = gcd(n, m); return (n / g) * m; } // 重複を消す。計算量はO(NlogN) template <class T> void unique(std::vector<T> &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); } #line 2 "main.cpp" // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { int a, b, c, d; cin >> a >> b >> c >> d; cout << min(a, min(b, min(c, d))) << endl; }
#include<iostream> #include<algorithm> using namespace std; int main() { int t,i,arr[4]; for(i=0;i<4;i++) cin>>arr[i]; sort(arr,arr+i); cout<<arr[0]<<"\n"; return 0; }
// Parasparopagraho Jīvānām #pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/rope> using namespace __gnu_pbds ; using namespace __gnu_cxx ; using namespace std ; typedef long long ll ; typedef long double ldb ; typedef pair<int, int> pii ; typedef pair<int, ll> pil ; typedef pair<ll,ll> pll ; typedef vector<int> vi ; typedef vector<ll> vll ; typedef vector<pii> vpi ; typedef vector<pll> vpl ; typedef vector<int,pil> edges ; typedef vector<vi> matrix ; typedef priority_queue<int> pqu ; template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ; #define rep(i,a,b) for (int i = (a); i <= (b); i++) #define per(i,b,a) for (int i = (b); i >= (a); i--) #define mp make_pair #define eb emplace_back #define pb push_back #define pob pop_back #define fi first #define se second #define ins insert #define bk back #define con continue #define lbd lower_bound #define ubd upper_bound #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define pr cout << // I don't like this but my laptop's 'O' key is not functioning well /* Careful when using long long __builtin_clz(int x) {} //: the number of zeros at the beginning of the number __builtin_ctz(int x) {} //: the number of zeros at the end of the number __builtin_popcount(int x) {} //: the number of ones in the number __builtin_parity(int x) {} //: the parity (even or odd) of the number of ones */ const int mod1 = 1000000007 ; const int mod2 = 998244353 ; const ll infl = 4e18 ; const int infi = 2e9 ; const int maxn = 2e5 + 5 ; const int block = 500 ; const int logn = 31 ; const int alpha = 27 ; const ldb pi = acos(-1) ; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) ; int rng_lr(int a, int b) { int ret ; ret = uniform_int_distribution<int>(a, b)(rng) ; return ret ; // For shuffling use shuffle(all, rng) ; } ll modpow(ll a, ll b, int MOD) { ll res = 1 ; while(b) { if(b&1) { res = res*a ; res %= MOD ; } a *= a ; a %= MOD ; b >>= 1 ; } return res%MOD ; } void upmin(int &a, int b) { if(a < b) { a = b ; } } void relax(int &a, int b) { if(a > b) { a = b ; } } ll add(ll a, ll b, int MOD) { a += b ; if(a >= MOD) { a -= MOD ; } return a ; } ll sub(ll a, ll b, int MOD) { a -= b ; if(a < 0) { a += MOD ; } return a ; } ll mul(ll a, ll b, int MOD) { b %= MOD ; a *= b ; a %= MOD ; return a ; } ll inverse(ll a, ll MOD) { a = modpow(a, MOD - 2, MOD) ; return a ; } ll lcm(ll a, ll b) { ll ret ; ll g = __gcd(a, b) ; ret = a/g ; ret = ret*b ; return ret ; } int icast(char ch) { return int(ch-'a') ; } void hyn(int a, int b) { if(a == b) cout << "YES\n" ; else cout << "NO\n" ; } void pyd(int a, int b) { if(a == b) cout << "First\n" ; else cout << "Second\n" ; } // Check constraints + overflows + types in typedef int n ; char g[102][102] ; int main() { ios::sync_with_stdio(false) ; cin.tie(NULL) ; cin >> n ; rep(i,0,n-1) { rep(j,0,n-1) { cin >> g[i][j] ; } } int dist[n + 1][n + 1] ; rep(i,1,n) { rep(j,1,n) { dist[i][j] = infi ; } } rep(i,1,n) { dist[i][i] = 0 ; } rep(i,1,n) { rep(j,1,n) { if(g[i - 1][j - 1] == '1') { dist[i][j] = 1 ; } } } rep(p,1,n) { rep(i,1,n) { rep(j,1,n) { if(dist[i][p] < infi && dist[p][j] < infi) { dist[i][j] = min(dist[i][j], dist[i][p] + dist[p][j]) ; } } } } int ances[n + 1] ; rep(i,1,n) { ances[i] = 0 ; rep(j,1,n) { if(dist[j][i] < infi) { ances[i]++ ; } } } ldb ev = (ldb)0 ; rep(i,1,n) { ldb prob = ((ldb)1)/((ldb)ances[i]) ; ev = ev + prob ; } cout << fixed << setprecision(10) ; pr ev << "\n" ; return 0 ; }
#include <bits/stdc++.h> using namespace std; bool flg[100][100]; int main() { int n; cin >> n; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { char c; cin >> c; flg[i][j] = (c == '1'); } flg[i][i] = true; } for(int k = 0; k < n; ++k) { for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { flg[i][j] |= (flg[i][k] & flg[k][j]); } } } double ans = 0; for(int i = 0; i < n; ++i) { int cnt = 0; for(int j = 0; j < n; ++j) { cnt += flg[j][i]; } ans += 1.0 / (double)cnt; } printf("%.15f\n", ans); return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,mmx,avx") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define PB push_back #define MP make_pair #define endl "\n" #define fst first #define snd second const int UNDEF = -1; const int INF=1<<30; template<typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; } template<typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; } typedef pair<ll,ll> pll;typedef vector<ll> vll;typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;typedef vector<pii> vpii;typedef vector<pll> vpll; template<typename T> void makeunique(vector<T> &vx) {sort(vx.begin(),vx.end());auto it=unique(vx.begin(),vx.end());vx.resize(std::distance(vx.begin(),it));} const int mn=200000+4; ll dfs(vvi const & tree, int u, int p, int depth) { ll res = ((ll) depth << 32) + u; for (int v : tree[u]) { if (v != p) { res = max(res, dfs(tree, v, u, depth + 1)); } } return res; } pii diameter(vvi const & tree) { int furthestVertex = (int) dfs(tree, 1, -1, 0); return MP((int) (dfs(tree, furthestVertex, -1, 0)), furthestVertex); } vi order; vvi g; bitset<mn> special; int id=1; void go(int x, int p) { order[x]=id; ++id; for (auto &y:g[x]) { if (y==p) continue; go(y,x); } if (!special[x]) { ++id; } } vi vpar; void makepar(int x, int p) { vpar[x]=p; for (auto &y:g[x]) { if (y==p) continue; makepar(y,x); } } int getdist(int x, int p, int dest) { if (x==dest) return 0; for (auto &y:g[x]) { if (y==p) continue; int got=getdist(y,x,dest); if (got!=-1) return got+1; } return -1; } void solve(long long n, std::vector<long long> A, std::vector<long long> B){ g.resize(n+1); vpar.resize(n+1); order.resize(n+1); for (int i=0;i<A.size();i++) { int x=A[i],y=B[i]; g[x].PB(y); g[y].PB(x); } pii diam=diameter(g); int src=diam.fst,dest=diam.snd; makepar(src,-1); //printf("src:%d dest:%d\n",src,dest); { int x=dest; while(x!=-1) { //printf("x:%d\n",x); special[x]=true; x=vpar[x]; } } for (int x=1;x<=n;x++) sort(g[x].begin(),g[x].end(),[&](int x,int y) {return MP(special[x],x) < MP(special[y],y);} ); go(src,-1); for (int x=1;x<=n;x++) printf("%d ",order[x]); printf("\n"); //for (int x=1;x<=n;x++) for (int y=1;y<=n;y++) { // if (abs(order[x]-order[y]) < getdist(x,-1,y)) { // printf("x:%d y:%d order:%d %d dist:%d\n",x,y,order[x],order[y],getdist(x,-1,y));fflush(stdout); // assert(0); // } //} } // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template) int main(){ long long N; scanf("%lld",&N); std::vector<long long> A(N-1); std::vector<long long> B(N-1); for(int i = 0 ; i < N-1 ; i++){ scanf("%lld",&A[i]); scanf("%lld",&B[i]); } solve(N, std::move(A), std::move(B)); return 0; }
// g++ -std=c++11 a.cpp #include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<unordered_map> #include<utility> #include<cmath> #include<random> #include<cstring> #include<queue> #include<stack> #include<bitset> #include<cstdio> #include<sstream> #include<random> #include<iomanip> #include<assert.h> #include<typeinfo> #define loop(i,a,b) for(long long i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define FOR(i,a) for(auto i:a) #define pb push_back #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) #define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;} #define show2d(v) {rep(__,v.size())show1d(v[__]);} using namespace std; //kaewasuretyuui typedef long long ll; #define int ll typedef int Def; typedef pair<Def,Def> pii; typedef vector<Def> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef vector<vd> vvd; typedef pair<Def,pii> pip; typedef vector<pip>vip; #define mt make_tuple typedef tuple<int,int,int> tp; typedef vector<tp> vt; typedef vector<vt>vvt; template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;} template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;} const double PI=acos(-1); const long double EPS=1e-9; Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10; int dx[]={-1,0,1,0}; int dy[]={0,1,0,-1}; #define yes cout<<"Yes\n" #define no cout<<"No\n" int n,c; vi d,out; vvi G; void dfs(int a,int p,int t){ d[a]=t; for(auto i:G[a]){ if(i==p)continue; dfs(i,a,t+1); } } void dfs2(int a,int p){ out[a]=c; for(auto i:G[a]){ if(d[i]<d[a])continue; if(i==p)continue; c++; dfs2(i,a); c++; } for(auto i:G[a]){ if(d[i]>d[a])continue; if(i==p)continue; c++; dfs2(i,a); c++; } } signed main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>n; G=vvi(n); rep(i,n-1){ int a,b; cin>>a>>b; a--;b--; G[a].pb(b); G[b].pb(a); } out=d=vi(n,-1); dfs(0,-1,0); int r,ma=0; rep(i,n)cmax(ma,d[i]); rep(i,n)if(ma==d[i]){ r=i; } d=vi(n,-1); c=1; dfs(r,-1,0); ma=0; rep(i,n)cmax(ma,d[i]); rep(i,n)if(ma==d[i]){ r=i; } dfs2(r,-1); show1d(out); }
#pragma region templates //#pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using db = double; using ld = long double; template<typename T> using V = vector<T>; template<typename T> using VV = vector<vector<T>>; template<typename T> using PQ = priority_queue<T>; #define fs first #define sc second #define pb push_back #define mp make_pair #define mt make_tuple #define eb emplace_back #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define all(v) (v).begin(),(v).end() #define siz(v) (ll)(v).size() #define rep(i,a,n) for(ll i=a;i<(ll)(n);++i) #define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i) #define ENDL '\n' typedef pair<int,int> Pi; typedef pair<ll,ll> PL; constexpr ll mod = 1000000007; // 998244353; constexpr ll INF = 1000000099; constexpr ll LINF = (ll)(1e18 +99); const ld PI = acos((ld)-1); constexpr ll dx[4]={-1,0,1,0},dy[4]={0,1,0,-1}; template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;} template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;} template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;} inline void Yes() { cout << "Yes" << ENDL; } inline void No() { cout << "No" << ENDL; } inline void YES() { cout << "YES" << ENDL; } inline void NO() { cout << "NO" << ENDL; } template<typename T,typename Y> inline T mpow(T a, Y n) { T res = 1; for(;n;n>>=1) { if (n & 1) res = res * a; a = a * a; } return res; } template <typename T> vector<T> finddivisor(T x) { //整数xの約数(xを含む) vector<T> divisor; for(T i = 1; (i * i) <= x; i++) { if(x % i == 0) { divisor.push_back(i); if(i * i != x) { divisor.push_back(x / i);} } } sort(divisor.begin(), divisor.end()); return divisor; } template <typename T> V<T> prefix_sum(const V<T>& v) { int n = v.size(); V<T> ret(n + 1); rep(i, 0, n) ret[i + 1] = ret[i] + v[i]; return ret; } template<typename T> T rand(T l,T r){ static random_device rd; static mt19937 g(rd()); return uniform_int_distribution<T>(l,r)(g); } template<typename T> istream& operator >> (istream& is, vector<T>& vec){ for(auto&& x: vec) is >> x; return is; } template<typename T,typename Y> ostream& operator<<(ostream& os,const pair<T,Y>& p){ return os<<"{"<<p.fs<<","<<p.sc<<"}"; } template<typename T> ostream& operator<<(ostream& os,const V<T>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } template<typename ...Args> void debug(Args&... args){ for(auto const& x:{args...}){ cerr<<x<<' '; } cerr<<ENDL; } #pragma endregion templates signed main(){ cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false); cout<<fixed<<setprecision(20); V<int> a(1002,0),b=a; ll n,m;cin>>n>>m; rep(i,0,n){ ll x;cin>>x; a[x]=1; } rep(i,0,m){ ll x;cin>>x; b[x]=1; } rep(i,1,1001)if(a[i]^b[i])cout<<i<<" "; cout<<ENDL; } //(・_・)(・_・)(・_・)(・_・) //CHECK overflow,what to output? //any other simpler approach?
#include <bits/stdc++.h> using namespace std; int main() { int n,m; cin >> n >> m; vector<int> vec (n); for(int i=0; i<n; i++){ int x; cin >> x; vec.at(i) = x; } for(int i=0; i<m; i++){ int x; cin >> x; vec.push_back(x); } sort(vec.begin(),vec.end()); reverse(vec.begin(),vec.end()); stack<int> ans; for(int i=0; i<vec.size(); i++){ if(ans.empty()){ ans.push(vec.at(i)); } else if(vec.at(i) == ans.top()){ ans.pop(); } else{ ans.push(vec.at(i)); } } while(!ans.empty()){ cout << ans.top() << " "; ans.pop(); } }
#include<bits/stdc++.h> using namespace std; #define all(x) (x).begin(),(x).end() #define pb push_back #define lb lower_bound #define ub upper_bound #define fi first #define se second #define pii pair<int,int> #define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); #define mp make_pair #define endl '\n' #define int long long typedef double db; typedef long long ll; const int N = 2e5 + 5; const int MOD = 1e9 + 7; const int INF = 1e9; int ncr(int n, int r) { r = min(r, n - r); int ans = 1; for (int i = 1; i <= r; i++) { ans *= (n - r + i); ans /= i; } return ans; } void solve() { int a, b, k; cin >> a >> b >> k; int n = (a + b); string ans = ""; --k; while (a && b) { //number of remaining string if we put 'a' here int rem = ncr(a + b - 1, b); if (k >= rem) { k -= rem; ans += "b"; --b; } else { ans += "a"; --a; } } ans += string(a, 'a') + string(b, 'b'); cout << ans; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif fastIO int t = 1; // cin >> t; for (int tt = 1; tt <= t; tt++) { solve(); } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) typedef long long ll; ll c[32][32]; signed main(){ rep(i,31){ c[0][i]=1; } rep(i,31){ c[i][0]=1; } rep(i,31) rep(j,31){ if(i==0||j==0) continue; c[i][j]=c[i-1][j]+c[i][j-1]; } ll a,b,k; cin>>a>>b>>k; ll len=a+b; rep(i,len){ //cout<<a<<" "<<b<<" "<<k<<endl; if(b==0){ cout<<"a"; continue; } if(a==0){ cout<<"b"; continue; } if(k>c[a-1][b]){ cout<<"b"; k-=c[a-1][b]; b--; } else{ cout<<"a"; a--; } } cout<<endl; return 0; }
#pragma GCC optimize("Ofast") #include "bits/stdc++.h" typedef long long ll; using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { // #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } // sim dor(const c&) { ris; } }; #ifdef ASJKD } #endif #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define read(x) for(auto &i : (x)){cin>>i;} #define all(x) begin(x), end(x) #define pb push_back #define maxn 1500 ll mod = 998244353; int INF = 1e9; ll INF64 = 1e18; long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1){ res = res * a % m; } a = a * a % m; b >>= 1; } return res; } bool check(vector<pair<int, int>> &a, long double pos, int d, int h){ // myx = 0 // myy = pos int n =a.size(); long double coef = (long double)(h-pos) / ((long double)d); long double c = pos; // debug() << imie(coef) imie(c); bool ok = 1; for(int i=0;i<n;i++){ ok &= ((coef * a[i].first) + c) > (double)a[i].second; if(!ok) break; } return ok; } double eps = 1e-6; void test_case(int tnum){ int n, d, h; cin>>n>>d>>h; vector<pair<int, int>> a(n); for(int i=0;i<n;i++){ cin>>a[i].first>>a[i].second; } double hi = 1000; double lo = 0; while(hi-eps > lo){ double mid = (hi + lo) / 2; if(check(a, mid, d, h)){ hi = mid; } else{ lo = mid; } } // debug() << imie(check(a, 2, d, h)); cout <<setprecision(14) << fixed<< hi << '\n'; } int main(){ cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int t=1; // cin>>t; for(int test=1;test<=t;test++){ test_case(test); } return 0; }
#include <iostream> #include <string> #include <vector> //#include <math.h> /* tan */ #include <cmath> #include <algorithm> // std::max using namespace std; int main(){ int N; long D; long H; cin >> N >> D >> H; vector<vector<long>> q(N,vector<long>(2)); for(int i = 0; i < N; i++){ cin >> q[i][0] >> q[i][1]; } double max = 0; for(auto h : q){ double angle = atan2((H - h[1]), (D - h[0])); double t = D * tan(angle); double tmp = H - t; if(max < tmp){ max = tmp; } } cout << max << endl; return 0; }
#include<bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std ; typedef long long ll ; const int mod=1000000007 ; void solve(){ int r,c ; cin>>r>>c ; char arr[r][c] ; rep(i,r){ rep(j,c){ cin>>arr[i][j] ; } } ll count=0 ; rep(i,r){ rep(j,c-1){ if(arr[i][j]=='.' && arr[i][j+1]=='.'){ count++ ; } } } rep(i,r-1){ rep(j,c){ if(arr[i][j]=='.' && arr[i+1][j]=='.'){ count++ ; } } } cout<<count ; } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout) ; #endif cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int y ; y=1 ; //cin>>y ; while(y--) { solve() ; } }
#include<bits/stdc++.h> using namespace std; char a,b; int main(){ cin>>a>>b; if(a=='Y'&&b=='a')cout<<"A"; if(a=='Y'&&b=='b')cout<<"B"; if(a=='Y'&&b=='c')cout<<"C"; if(a=='Y'&&b=='A')cout<<"A"; if(a=='Y'&&b=='B')cout<<"B"; if(a=='Y'&&b=='C')cout<<"C"; if(a=='N'&&b=='a')cout<<"a"; if(a=='N'&&b=='b')cout<<"b"; if(a=='N'&&b=='c')cout<<"c"; if(a=='N'&&b=='A')cout<<"a"; if(a=='N'&&b=='B')cout<<"b"; if(a=='N'&&b=='C')cout<<"c"; return 0; }
#include <algorithm> #include <cassert> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> #include <limits.h> using namespace std; typedef long long ll; template<class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const ll INF = INT_MAX / 2; // {a, t} vector<pair<ll, int>> fs; ll calc(ll x, const int N) { ll ans = x; for (int i = 0; i < N; i++) { const ll a = fs[i].first; const int t = fs[i].second; if (t == 1) { ans += a; } else if (t == 2) { ans = max(ans, a); } else { ans = min(ans, a); } } return ans; } int main(void) { int N; cin >> N; fs.resize(N); for (int i = 0; i < N; i++) { int a, t; cin >> a >> t; fs[i] = make_pair(a, t); } int Q; cin >> Q; const ll v_min = calc(-INF, N); const ll v_max = calc(INF, N); #ifdef DEBUG printf("v_min: %lld, v_max: %lld\n", v_min, v_max); #endif ll acc = 0; for (int i = 0; i < N; i++) { const ll a = fs[i].first; const int t = fs[i].second; if (t == 1) { acc += a; } } for (int i = 0; i < Q; i++) { ll x; cin >> x; if (x < v_min - acc) { printf("%lld\n", v_min); } else if (v_max - acc < x) { printf("%lld\n", v_max); } else { printf("%lld\n", x + acc); } } return 0; }
#include <iostream> #include <cmath> #include <string> #include <vector> #include <iterator> #include <algorithm> constexpr int N = 100005; int n; std::vector<long long> a[3]; std::string s; int main() { std::ios::sync_with_stdio(false), std::cin.tie(nullptr); std::cin >> n; for (int i = 1; i <= n << 1; ++i) { long long x; std::cin >> x >> s; if (s[0] == 'R') a[0].push_back(x); else if (s[0] == 'G') a[1].push_back(x); else a[2].push_back(x); } int x = -1, y = -1; for (int i = 0; i < 3; ++i) { if (a[i].size() & 1) { if (x != -1) y = i; else x = i; } std::sort(a[i].begin(), a[i].end()); } if (x == -1) return std::cout << "0\n", 0; auto f = [](int x, int y) -> long long { long long ans = 1e18; for (long long i : a[x]) { long long t = std::lower_bound(a[y].begin(), a[y].end(), i) - a[y].begin(); if (t != a[y].size()) ans = std::min(ans, std::abs(a[y][t] - i)); if (t) ans = std::min(ans, std::abs(i - a[y][t - 1])); } return ans; }; int z = 3 - x - y; long long ans = f(x, y); long long t1 = f(x, z), t2 = f(y, z); if (t1 < 1e18) ans = std::min(ans, t1 + t2); std::cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using pll = pair<ll, ll>; #define sz(x) (int)(x).size() template <typename T> bool ckmin (T& a, T b) { return (b < a) ? a = b, 1 : 0; } template <typename T> bool ckmax (T& a, T b) { return (b > a) ? a = b, 1 : 0; } #ifdef XVENOM #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: " << dbg_str(__VA_ARGS__) #else #define dbg(...) ; #endif template <typename U, typename V> string to_string (pair<U, V>); string to_string (const char* e) { return "\"" + string(e) + "\""; } string to_string (string& e) { return "\"" + e + "\""; } string to_string (char e) { return "\'" + string(1, e) + "\'"; } string to_string (bool e) { return e ? "true" : "false"; } template <typename T> string to_string (T e) { string s = "[ "; for (auto& x : e) s += to_string(x) + " "; return s + "]"; } template <typename U, typename V> string to_string (pair<U, V> e) { return "(" + to_string(e.first) + ", " + to_string(e.second) + ")"; } string dbg_str () { return " \n"; } template <typename U, typename... V> string dbg_str (U u, V... v) { return " " + to_string(u) + dbg_str(v...); } /* *** */ const ll Mod = 1000000007; /* --- */ using MT = decay<decltype(Mod)>::type; MT inverse (MT a, MT n) { MT u = 0, v = 1; while (a != 0) { MT q = n / a; swap(u, v); v -= q * u; swap(n, a); a -= q * n; } assert(n == 1); return u; } class mint { private: MT x; public: void normalize() { if (x >= Mod || x < 0) x = (x % Mod) + Mod; if (x >= Mod) x -= Mod; } mint(): x(0) {} mint(MT z): x(z) { normalize(); } const MT& operator() () const { return x; } mint operator+ (const mint& z) { MT r = x + z(); if (r >= Mod) r -= Mod; return mint(r); } mint operator- (const mint& z) { MT r = x - z(); if (r < 0) r += Mod; return mint(r); } mint operator* (const mint& z) { MT r = x * z(); if (r >= Mod) r %= Mod; return mint(r); } mint operator/ (const mint& z) { return (*this) * mint(inverse(z(), Mod)); } mint operator+= (const mint& z) { (*this) = (*this) + z; return (*this); } mint operator-= (const mint& z) { (*this) = (*this) - z; return (*this); } mint operator*= (const mint& z) { (*this) = (*this) * z; return (*this); } mint operator/= (const mint& z) { (*this) = (*this) / z; return (*this); } friend std::istream& operator>> (std::istream&, mint&); }; std::istream& operator>> (std::istream& stream, mint& p) { MT v; stream >> v; p.x = v; p.normalize(); return stream; } std::ostream& operator<< (std::ostream& stream, const mint& p) { return stream << p(); } mint operator+ (const MT& p, const mint& q) { return mint(p) + q; } mint operator- (const MT& p, const mint& q) { return mint(p) - q; } mint operator* (const MT& p, const mint& q) { return mint(p) * q; } mint operator/ (const MT& p, const mint& q) { return mint(p) / q; } string to_string (const mint& p) { return to_string(p()); } mint powMod (mint a, MT b) { mint res = 1, x = a; while (b > 0) { if (b & 1) res *= x; x *= x; b >>= 1; } return res; } /* --- */ const int N = 2000 + 66; mint f[N][N], g[N][N], h[N][N], ans[N][N]; string grid[N]; int n, m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { string t; cin >> t; grid[i] = " " + t; } f[1][1] = g[1][1] = h[1][1] = ans[1][1] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (i == 1 && j == 1) continue; if (grid[i][j] == '#') continue; ans[i][j] = f[i - 1][j] + g[i][j - 1] + h[i - 1][j - 1]; f[i][j] = f[i - 1][j] + ans[i][j]; g[i][j] = g[i][j - 1] + ans[i][j]; h[i][j] = h[i - 1][j - 1] + ans[i][j]; } } cout << ans[n][m] << '\n'; }
#include <bits/stdc++.h> using namespace std; #define m_p make_pair #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define fi first #define se second typedef long long ll; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 100005; int n, d; int a[N], b[N]; int minu[N]; vector<int> v[N]; void solv() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) scanf("%d", &b[i]); a[n + 1] = b[n + 1] = d + 1; for (int i = 1; i <= n; ++i) minu[i] = N; map<int, int> mp; for (int i = 0; i <= n + 1; ++i) { mp[a[i] - i] = i; if (mp.find(b[i] - i) != mp.end()) { if (mp[b[i] - i] != i) v[mp[b[i] - i]].push_back(i); minu[i] = min(minu[i], i - mp[b[i] - i]); } } mp.clear(); for (int i = n + 1; i >= 0; --i) { mp[a[i] - i] = i; if (mp.find(b[i] - i) != mp.end()) { if (mp[b[i] - i] != i) v[mp[b[i] - i]].push_back(i); minu[i] = min(minu[i], mp[b[i] - i] - i); } } for (int i = 0; i <= n + 1; ++i) { if (minu[i] == N) { printf("-1\n"); return; } } ll ans = 0; for (int i = 0; i <= n + 1; ++i) { if (v[i].empty()) continue; sort(all(v[i])); if (v[i][0] < i) ans += (i - v[i][0]); if (v[i].back() > i) ans += (v[i].back() - i); } printf("%lld\n", ans); } int main() { #ifdef SOMETHING freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif // SOMETHING //ios_base::sync_with_stdio(false), cin.tie(0); solv(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; vector<int> C(N); for (int i = 0; i < N; i++) { char c; cin >> c; if (c == 'B') C[i] = 0; else if (c == 'W') C[i] = 1; else C[i] = 2; } vector<vector<int>> ncr = { {1, 0, 0}, {1, 1, 0}, {1, 2, 1} }; int ans = 0; for (int i = 0; i < N; i++) { int r = i; int n = N - 1; int tmp = 1; while (n != 0) { int j = n % 3; int k = r % 3; n /= 3; r /= 3; tmp *= ncr[j][k]; tmp %= 3; } ans += tmp * C[i]; ans %= 3; } if (N % 2 == 0) ans = ans * -1; if (ans < 0) ans += 3; if (ans == 0) cout << "B" << endl; else if (ans == 1) cout << "W" << endl; else cout << "R" << endl; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define vi vector<int> #define mk make_pair #define vll vector<long long > #define loop(i,a,b) for(int i=a;i<=b;i++) #define loopr(i,a,b) for(int i=b;i>=a;i--) #define vii vector<vector<int>> #define ll long long void io() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } int main() { ll n; cin >> n; ll i = 1; ll ans = 0; while (i * i <= (2 * n)) { if ((2ll * n) % i == 0) { ll no = ((2ll * n) / i ) + 1 - n; if (no % 2 == 0) ans += 2; no = i + 1 - n; if (no % 2 == 0) ans += 2; // cout << i << endl; } i++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define m_k make_pair using namespace std; const int N=1e5+100; int T,n,a[N],b[N],w,t[N]; signed main() { scanf("%d",&T); while (T--) { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i]; if (n==1) { printf("Second\n"); continue; } sort(b+1,b+1+n); w=unique(b+1,b+1+n)-b-1; for (int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+1+w,a[i])-b; for (int i=1;i<=n;i++) t[i]=0; for (int i=1;i<=n;i++) t[a[i]]++; int cnt=0; for (int i=1;i<=n;i++) cnt+=(t[i]&1); if (n%2==0) { if (!cnt) printf("Second\n"); else printf("First\n"); } else { if (cnt==1) printf("First\n"); else printf("Second\n"); } } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef long double ld; #define F first #define S second const int MOD = 1e9 + 7;//XXX const int C = 26;//XXX void add(int &x, int y){ x += y; while (x >= MOD) x -= MOD; while (x < 0) x += MOD; } int fix(int x){ while (x >= MOD) x -= MOD; while (x < 0) x += MOD; return x; } int pw(int a, int b){ int ret = 1; while (b){ if (b & 1) ret = 1ll*ret*a%MOD; b >>= 1; a = 1ll*a*a%MOD; } return ret; } const int MAXN = 1e6 + 10; int n, a[MAXN]; set<int> st; vector<int> factor(int X) { vector<int> vec; for(int i = 1; i*i <= X; i++) { if(X % i == 0) { for(int x : {i, X/i}) if(x <= a[0]) vec.push_back(x); } } return vec; } bool check(int x) { int cur = -1; for(int j = 0; j < n; j++) { if(a[j] % x == 0) { if(~cur) { cur = __gcd(cur, a[j]); } else cur = a[j]; } } return cur == x; } void solve() { cin >> n; for(int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a+n); vector<int> vec; for(int i = 0; i < n; i++) { vector<int> sec = factor(a[i]); for(int x : sec) vec.push_back(x); } sort(vec.begin(), vec.end()); vec.resize(unique(vec.begin(), vec.end())-vec.begin()); int ans = 0; for(int x : vec) if(check(x)) ++ans; cout << ans << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //cout << fixed << setprecision(6); int te = 1; //cin >> te; for (int w = 1; w <= te; w++){ //cout << "Case #" << w << ": "; solve(); } return 0; }
//雪花飄飄北風嘯嘯 //天地一片蒼茫 #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/rope> using namespace std; using namespace __gnu_pbds; using namespace __gnu_cxx; #define ll long long #define ii pair<ll,ll> #define iii pair<ii,ll> #define fi first #define se second #define endl '\n' #define debug(x) cout << #x << " is " << x << endl #define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() #define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> //change less to less_equal for non distinct pbds, but erase will bug mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); int n,m; string grid[105]; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); cin>>n>>m; rep(x,0,n) cin>>grid[x]; int ans=0; rep(x,0,n-1){ rep(y,0,m) if (grid[x][y]=='.' && grid[x+1][y]=='.') ans++; } rep(x,0,n){ rep(y,0,m-1) if (grid[x][y]=='.' && grid[x][y+1]=='.') ans++; } cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; int main(void){ int h,w,ans=0; cin >> h >> w; char m[h][w]; rep(i,h)rep(j,w) cin >> m[i][j]; rep(i,h)rep(j,w){ if(m[i][j]=='#') continue; rep(k,4){ int nowx = i+dx[k],nowy = j+dy[k]; if(nowx<0||h<=nowx||nowy<0||w<=nowy) continue; if(m[nowx][nowy]=='.') ans++; } } cout << ans/2 << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1e9+7; const ll INF = 1e18; #define rep(i,m,n) for(ll i = (m); i <= (n); i++) #define zep(i,m,n) for(ll i = (m); i < (n); i++) #define rrep(i,m,n) for(ll i = (m); i >= (n); i--) #define print(x) cout << (x) << endl; #define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll n; cin >> n; ll a[n]; zep(i, 0, n)cin >> a[i]; ll b[n]; zep(i, 0, n)cin >> b[i]; ll mx = INF, mn = 0; zep(i, 0, n){ mx = min(mx, b[i]); mn = max(mn, a[i]); } print(max(mx - mn + 1, 0LL)) return 0; }
#include <bits/stdc++.h> using namespace std; void init(void) { cin.tie(0) ; cin.sync_with_stdio(0) ; } int main() { init() ; int n , temp ; bool flag = true ; cin >> n; int a[n] , b [n] ; for(int i = 0 ; i< n ;i++) cin >> a[i] ; for(int i = 0 ; i< n ;i++) cin >> b[i] ; pair<int,int> minmax ; minmax.first = 0 ; minmax.second = 10001 ; for(int i = 0 ;i<n;i++) { if(b[i] >= minmax.first) { if(a[i]>minmax.first) minmax.first = a[i] ; if(b[i]<minmax.second) minmax.second = b[i] ; }else { flag = false ; cout<<0 ; break; } } if(flag == true ) cout << minmax.second - minmax.first +1 ; return 0; }
#include<bits/stdc++.h> #define maxn 2000000 #define int long long using namespace std; int n,t,a[45]; vector<int> c[2]; void dfs(int now,int r,int s,int k) { if(s>t) return; if(now==r+1) { c[k].push_back(s); return; } dfs(now+1,r,s+a[now],k); dfs(now+1,r,s,k); } signed main() { ios::sync_with_stdio(0); cin>>n>>t; for(int i=1;i<=n;i++) cin>>a[i]; dfs(1,n/2,0,0); if(n/2+1<=n) dfs(n/2+1,n,0,1); c[0].push_back(0); c[1].push_back(0); sort(c[1].begin(),c[1].end()); int ans=0; for(int i=0;i<c[0].size();i++) { int it=upper_bound(c[1].begin(),c[1].end(),t-c[0][i])-c[1].begin()-1; // cout<<it<<' '<<c[1][it]<<endl; ans=max(c[0][i]+c[1][it],ans); } cout<<ans<<endl; }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using ll = long long; using namespace std; int main(){ int n, T; cin >> n >> T; vector<int> a(n); rep(i, n) cin >> a[i]; vector<ll> s, t; s = t = {0}; rep(i,n){ for (int j = s.size()-1; j >= 0; --j){ s.push_back(s[j]+a[i]); } swap(s,t); } sort(s.begin(), s.end()); ll ans = 0; for(ll x : t){ if (x > T) continue; int si = upper_bound(s.begin(), s.end(), T-x) - s.begin(); ans = max(ans, x+s[si-1]); } cout << ans << endl; }
#include "bits/stdc++.h" using namespace std; #define int long long int int N=1e9+7; int32_t main() { int n; cin>>n; vector<int> a(n); for(int i=0;i<n;i++) { cin>>a[i]; } sort(a.begin(),a.end()); int ans=1; int prev=0; for(int i=0;i<n;i++) { int mine=a[i]-prev+1; ans=(ans*mine)%N; prev=a[i]; } cout<<ans<<endl; return 0; }
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") //#include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; #include<iostream> #include<cstring> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } ll tmp = *(ll*)ci; if ((tmp & ma0) ^ ma0) { int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0); tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); } else { tmp = tmp & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += 8; if ((ct = *ci++) >= '0') { tmp = tmp * 10 + ct - '0'; if (*ci++ == '0') { tmp = tmp * 10; ci++; } } } return tmp; } int A[100001]; int tmp[100001]; void pakuri_sort(int N, int A[]) { const int b = 8; rep(k, 4) { int kazu[1 << b] = {}, kazu2[1 << b] = {}; rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i]; for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i]; k++; rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++; rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i]; for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i]; } } const int mod = 1e9 + 7; int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(); rep1(i, N) A[i] = getint(); pakuri_sort(N, A + 1); int kotae = 1; rep(i, N) { kotae = kotae * ll(A[i + 1] - A[i] + 1) % mod; } co(kotae); Would you please return 0; }
// #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") // #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #define int long long int #define ll long long int #define ld long double #define getFaster ios_base::sync_with_stdio(false), cin.tie(nullptr) #define rep(i, init, n) for (int i = init; i < (int)n; i++) #define rev(i, n, init) for (int i = (int)n; i >= init; i--) #define MOD1 1e9 + 7 #define MOD2 998244353 #define f first #define s second #define endl '\n' #define pii pair<int, int> #define tii tuple<int, int> #define all(v) v.begin(), v.end() #define mt make_tuple #define precise(i) cout << fixed << setprecision(i) #define codejam cout << "Case #" << ii + 1 << ": "; #define impossible cout << "IMPOSSIBLE" << endl; #define error(s) throw runtime_error(s) #define hash hash1 #define prev prev1 std::mt19937_64 gen(std::chrono::steady_clock::now().time_since_epoch().count()); std::mt19937 gen1(std::chrono::steady_clock::now().time_since_epoch().count()); //change according to int or long long int int rng(int l, int r) { return std::uniform_int_distribution<int>(l, r)(gen); } const long double PI = atan(1.0) * 4; const int32_t INF32 = 2e9 + 7; const int64_t INF64 = 3e18; const int32_t LOG = 21; int32_t MOD = MOD1; using namespace std; //-------------------DEBUGGING------------------------- void my_debugger(string s, int LINE_NUM) { cerr << endl; } template <typename start, typename... end> void my_debugger(string s, int LINE_NUM, start x, end... y) { if (s.back() != ',') { s += ','; cerr << "LINE(" << LINE_NUM << "): "; } int i = s.find(','); cerr << s.substr(0, i) << " = " << x; s = s.substr(i + 1); if (!s.empty()) cerr << ", "; my_debugger(s, LINE_NUM, y...); } #ifdef AJIT #define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__); #else #define debug(...) ; #endif void setMod(int mod_val) { MOD = mod_val; } void files_init() { freopen("file.in", "r", stdin); freopen("file.out", "w", stdout); } const int N = 1e6 + 5; const int LOGN = 20; //----------------------------------------------------- bool intersect(int l1,int r1,int l2,int r2,int fl1,int fr1,int fl2,int fr2) { debug(l1,r1,l2,r2); if(l1>r2 || l2>r1 || l1>r1 || l2>r2) return false; if(l1==r2) { if(fl1 && fr2) return true; return false; } else if(l2==r1) { if(fl2 && fr1) return true; return false; } return true; } int32_t main() { getFaster; //files_init(); int tests = 1; //cin >> tests; rep(ii, 0, tests) { int n; cin>>n; vector<pii> vals(n); vector<pii> flag(n); rep(i,0,n) { int type,l,r; cin>>type>>l>>r; vals[i]={l,r}; if(type==1) flag[i]={1,1}; else if(type==2) flag[i]={1,0}; else if(type==3) flag[i]={0,1}; else flag[i]={0,0}; } int ans=0; rep(i,0,n) { rep(j,i+1,n) { if(intersect(vals[i].f,vals[i].s,vals[j].f,vals[j].s,flag[i].f,flag[i].s,flag[j].f,flag[j].s)) ans++; } } cout<<ans<<endl; } return 0; }
#include <iostream> using namespace std; struct Interval { int tip; int left; int right; }; int const NMAX = 2000; Interval v[1 + NMAX]; bool isIntersect(Interval a, Interval b) { if(a.right == b.left){ if((a.tip == 1 || a.tip == 3) && (b.tip == 1 || b.tip == 2)){// ] and [ return true; } return false; }else if(a.right < b.left){ return false; } if(b.right == a.left){ if((b.tip == 1 || b.tip == 3) && (a.tip == 1 || a.tip == 2)){// ] and [ return true; } return false; }else if(b.right < a.left){ return false; } return true; } int main() { int n, ans = 0; cin >> n; for(int i = 1;i <= n;i++){ cin >> v[i].tip >> v[i].left >> v[i].right; } for(int i = 1;i <= n;i++) { for(int j = i + 1;j <= n;j++){ if(isIntersect(v[i], v[j])){ ans++; } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; const int N = (int)2e5 + 77; const ll mod = (ll)1e9 + 7; const ll inf = (ll)1e18; int main() { ll n, ans = 1, sp[33] = {0}, cnt[33] = {0}; cin >> n; for (int i = 2; i <= n; i++) { for (int j = i; j <= n; j += i) { if (sp[j] == 0) sp[j] = i; } } for (int i = 2; i <= n; i++) { ll c[33] = {0}, k = i; while (k > 1) { c[sp[k]]++; k /= sp[k]; } for (int j = 2; j <= n; j++) cnt[j] = max(cnt[j], c[j]); } for (int i = 2; i <= n; i++) { int k = cnt[i]; while (k--) ans = ans * i; } cout << ans + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define pi acos(-1.0) #define nl '\n' /**-------Library-------*/ #define mem(a,b) memset(a,b,sizeof(a)) #define all(a) a.begin(),a.end() #define Sort(x) sort(x.begin(),x.end()) #define SortR(x) sort(x.begin(),x.end(),greater<int>()) #define Reverse(x) reverse(x.begin(),x.end()) #define Sum(A,n) accumulate(A,A+n,0) #define SortA(ar,s) sort(ar,ar+s) #define SortD(ar,s) sort(ar,ar+s,greater<int>()) #define maxa(A,n) *max_element(A,A+n) #define mina(A,n) *min_element(A,A+n) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define sq(x) (x)*(x) #define mid(l,r) (l+(r-l)/2) #define trzero(x) __builtin_ctz(x) #define parity(x) __builtin_parity(x) #define countone(x) __builtin_popcount(x) #define left(p) (p<<1) #define right(p) (p>>1) #define min3(a,b,c) min(a,min(b,c)) #define min4(a,b,c,d) min(a,min(b,min(c,d))) #define max3(a,b,c) max(a,max(b,c)) #define max4(a,b,c,d) max(a,max(b,max(c,d))) #define pb(x) push_back(x) #define mod 1000000007 #define precision(x) cout<<fixed<<setprecision(x) #define nel cout<<nl #define yes cout<<"YES"<<nl #define no cout<<"NO"<<nl /**----------Graph Moves----------*/ const int fx[] = {+1,-1,+0,+0}; // 4 X directions const int fy[] = {+0,+0,+1,-1}; // 4 Y directions const int fx8[] = {+0,+0,+1,-1,-1,+1,-1,+1}; // King X Moves const int fy8[] = {-1,+1,+0,+0,+1,+1,-1,-1}; // King Y Moves const int kx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; // Knight X Moves const int ky[] = {-1, 1, -2, 2, -2, 2, -1, 1}; // Knight Y Moves /**----data type----*/ typedef long long int ll; typedef unsigned long long int llu; int main() { int n; cin>>n; n-=3; cout<<6<<" "<<10<<" "<<15<<" "; for(int i=16; ; i++) { if(n==0) return 0; if(i%6==0 || i%15==0 || i%10==0) { cout<<i<<" "; i++; n--; } } return 0; }
#include <bits/stdc++.h> #define fi first #define se second //#define ivorysi #define enter putchar('\n') #define space putchar(' ') #define pii pair<int,int> typedef long long int64; using namespace std; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) out(x / 10); putchar('0' + x % 10); } int N; int64 res = 1; int64 gcd(int64 a,int64 b) { return b == 0 ? a : gcd(b,a % b); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif read(N); for(int i = 2 ; i <= N ; ++i) { res = res * i / gcd(i,res); } out(res + 1);enter; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define vi vector<ll> #define rep(i,a,b) for(ll i=a; i<b; ++i) #define pb push_back #define endl '\n' #define sa(x) sort((x).begin(), (x).end()); #define int long long #define test cout<<"hello\n"; #define disp(a) trav(a) cout<<x<<" "; cout<<endl; template<typename T, typename U> static inline void amin(T &x, U y){ if(y<x) x=y; } template<typename T, typename U> static inline void amax(T &x, U y){ if(x<y) x=y; } template<typename T,typename U> std::ostream& operator<<(std::ostream& out, std::pair<T,U> a) {out<<" ("<<a.fr<<" "<<a.sc<<") "; return out;} template<typename T,typename U> std::istream& operator>>(std::istream& in, std::pair<T,U> &a) {in>>a.fr>>a.sc; return in;} template<typename T> static inline void sd(vector<T> &x) {sort(all(x), greater<T>()) ; } vi a; int solve(){ int n; cin>>n; a.resize(1<<n); int mx=0, id=-1; rep(i,0,(1<<n)){ cin>>a[i]; if(a[i] > mx){ mx=a[i]; id=i; } } int st=0; if(id<(1<<(n-1))){ st += (1<<(n-1)); } int mm=0, ans=-1; // cout<<st<<endl; rep(i,0,(1<<(n-1))){ if(a[st+i] > mm){ mm = a[i+st]; ans=i+st; } } cout<<ans+1<<endl; return 0; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int testcases=1; //cin>>testcases; while(testcases--){ solve(); } return 0; }
#include <bits/stdc++.h> #define isNum(a) (a >= '0' && a <= '9') #define SP putchar(' ') #define EL putchar('\n') #define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout) template <typename T> void read(T &); template <typename T> void write(const T &); typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; const int iinf = 2147483647; const ll llinf = 9223372036854775807ll; const int N = 200005; char ch[N]; int main() { int T; read(T); while (T--) { int n; read(n); scanf("%s", ch + 1); scanf("%s", ch + 1); scanf("%s", ch + 1); for (int i = 1; i <= n; ++i) { putchar('0'); } for (int i = 1; i <= n; ++i) { putchar('1'); } putchar('0'); EL; } return 0; } template <typename T> void read(T &Re) { T k = 0; char ch = getchar(); int flag = 1; while (!isNum(ch)) { if (ch == '-') flag = -1; ch = getchar(); } while (isNum(ch)) { k = (k << 1) + (k << 3) + ch - '0'; ch = getchar(); } Re = flag * k; } template <typename T> void write(const T &Wr) { if (Wr < 0) { putchar('-'); write(-Wr); } else { if (Wr < 10) { putchar(Wr + '0'); } else { write(Wr / 10); putchar((Wr % 10) + '0'); } } }
#pragma GCC optimize(2) #include <bits/stdc++.h> #define all(n) (n).begin(), (n).end() #define se second #define fi first #define pb emplace_back #define mp make_pair #define sqr(n) ((n)*(n)) #define rep(i, a, b) for (int i = (a); i <= (b); ++i) #define per(i, a, b) for (int i = (a); i >= (b); --i) #define precision(a) setiosflags(ios::fixed) << setprecision(a) #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<long long> VL; typedef double db; template <typename T> inline char read(T &x) { x = 0; T fg = 1; char ch = getchar(); while (!isdigit(ch)) {if (ch == '-') fg = -1;ch = getchar();} while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ '0'), ch = getchar(); x = fg * x; return ch; } template <typename T, typename... Args> inline void read(T &x, Args &... args) { read(x), read(args...); } template <typename T> inline void write(T x) { int len = 0; char c[21]; if (x < 0) putchar('-'), x = -x; do{++len; c[len] = x % 10 + '0';} while (x /= 10); for (int i = len; i >= 1; i--) putchar(c[i]); } template <typename T, typename... Args> inline void write(T& x, Args &... args) { write(x), write(args...); } template<class T1, class T2> bool umin(T1& a, T2 b) { return a > b ? (a = b, true) : false; } template<class T1, class T2> bool umax(T1& a, T2 b) { return a < b ? (a = b, true) : false; } template<class T> void clear(T& a) { T().swap(a); } const int N = 3e5 + 5; int n, m, _, k, cas; ll a[N], s[N], ans; int main() { IOS; cin >> n; rep (i, 1, n) { cin >> a[i]; s[i] = s[i - 1] + a[i]; ans += a[i] * a[i] * (n - 1); ans -= 2 * a[i] * s[i - 1]; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; #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 template<class T,class U> inline bool chmax(T &a,U b){ if(a<b){ a=b; return true; } return false; } template<class T,class U> inline bool chmin(T &a,U b){ if(a>b){ a=b; return true; } return false; } constexpr int INF=1000000000; constexpr ll llINF=3000000000000000000; constexpr int mod=1000000007; constexpr double eps=1e-8; 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); } /* vector<vector<ll>>C,sC; void init_comb(int n,int m){ C.resize(n+1,vector<ll>(m+1,0)); sC.resize(n+1,vector<ll>(m+1,0)); C[0][0]=1; for(int i=1;i<=n;i++){ C[i][0]=1; for(int j=1;j<=m;j++){ C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } } rep(i,n+1){ rep(j,m){ sC[i][j+1]=(sC[i][j]+C[i][j])%mod; } } }*/ ll gcd(ll a,ll b){ while(a%b){ a%=b; swap(a,b); } return b; } ll lcm(ll a,ll b){ return a/gcd(a,b)*b; } bool isprime(int a){ if(a==1)return false; for(int i=2;i*i<=a;i++){ if(a%i==0)return false; } return true; } set<int>primes; void init_prime(int n){ primes.insert(2); for(int i=3;i<=n;i+=2){ bool f=true; for(int j:primes){ if(j*j>i)break; if(i%j==0){ f=false; break; } } if(f)primes.insert(i); } } ll modpow(ll a,ll b){ ll res=1; while(b){ if(b&1){ res*=a; res%=mod; } a*=a; a%=mod; b>>=1; } return res; } vector<ll>inv,fact,factinv; void init_fact(int n){ inv.resize(n+1); fact.resize(n+1); factinv.resize(n+1); inv[0]=0; inv[1]=1; fact[0]=1; factinv[0]=1; for(ll i=1;i<=n;i++){ if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod); fact[i]=(fact[i-1]*i)%mod; factinv[i]=factinv[i-1]*inv[i]%mod; } } ll _inv(ll a,ll m=mod){ //gcd(a,m) must be 1 ll b=m,u=1,v=0; while(b){ ll t=a/b; a-=t*b;swap(a,b); u-=t*v;swap(u,v); } u%=m; if(u<0)u+=m; return u; } ll comb(int a,int b){ if(a<b)return 0; if(a<0)return 0; return fact[a]*factinv[a-b]%mod*factinv[b]%mod; } ll multicomb(int a,int b){ return comb(a+b-1,b); } int main(){ cin.tie(0);ios::sync_with_stdio(false); int n,m,a[2000000],last[2000000]; cin>>n>>m; int ans=n; fill(last,last+n,-1); rep(i,n){ cin>>a[i]; if(last[a[i]]+m<i){ chmin(ans,a[i]); } last[a[i]]=i; } int i=n; rep(k,n){ if(last[k]+m<i){ chmin(ans,k); } } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; int main(void) { ll ans = 0; int n, m; cin >> n >> m; ll even = 0, odd = 0; rep (i, n){ string s; cin >> s; ll score = 0; rep (j, m){ if (s[j] == '1') score++; } if (score % 2 == 0) even++; else odd++; } ans = even * odd; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef Dhiraj #include "D:/dhiraj/Programming/debug.h" #else #define d(...) 11 #define cerr if(0) cerr #endif #define ll long long int #define endl '\n' template <typename T, typename U> inline istream& operator >> (istream& in, pair<T, U>& a) { in >> a.first >> a.second; return in; } template <typename T> inline istream& operator >> (istream& in, vector<T>& a) { for(auto& x : a) in >> x; return in; } void solve(int &T) { ll n, m; cin >> n >> m; vector<ll> b(m + 2); for(ll i = 0; i < m; i++) { cin >> b[i + 1]; } b[0] = 0; b[m + 1] = n + 1; if(m == 0) { cout << 1 << endl; return; } if(m == n) { cout << 0 << endl; return; } sort(b.begin(), b.end()); ll mn = 1e10; for(ll i = 1; i < m + 2; i++) { if(b[i] != b[i - 1] + 1) { mn = min(mn, b[i] - b[i - 1] - 1); } } d(mn); if(mn == 1e10) { cout << 0 << endl; return; } ll ans = 0; for(ll i = 1; i < m + 2; i++) { if(b[i] != b[i - 1] + 1) { ans += (b[i] - b[i - 1] - 1 + mn - 1) / mn; } } cout << ans << endl; } int main() { #ifdef Dhiraj freopen("D:/dhiraj/Programming/i1.txt", "r", stdin); freopen("D:/dhiraj/Programming/o1.txt", "w", stdout); // freopen("D:/dhiraj/Programming/e1.txt", "w", stderr); #endif ios::sync_with_stdio(false); cin.tie(0); int tc = 1; for(int i = 1; i <= tc; i++) { cerr << "Case #" << i << "\n"; solve(i); } return 0; }
#include <bits/stdc++.h> using namespace std; #define closeSync ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) typedef long long ll; typedef pair<int,ll> pil; const int MAXN = 100005; const ll MOD = 1e9+7; const ll INF = 1e18; vector<pil> mp[MAXN]; ll dis[MAXN]; int vis[MAXN]; ll ans[MAXN]; struct Edge{ int u,v; ll w; }; struct E{ int v; ll w; bool operator<(const E& x)const { return w > x.w; } }; int n,m; ll dij(int st,int ed) { for (int i=1;i<=n+1;i++) vis[i] = 0,dis[i] = INF; priority_queue<E> q; q.push({st,0}); dis[st] = 0; while (!q.empty()) { E u = q.top(); q.pop(); if (vis[u.v]) continue; vis[u.v] = 1; for (auto c : mp[u.v]) { if (dis[u.v] + c.second < dis[c.first]) { dis[c.first] = dis[u.v] + c.second; q.push({c.first,dis[c.first]}); } } } return dis[ed]; } inline void solve() { cin >> n >> m; vector<Edge> ve; for (int i=1;i<=m;i++) { int u,v;ll w; cin >> u >> v >> w; ve.push_back({u,v,w}); } for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) mp[j].clear(),mp[j].resize(0); for (auto c : ve) { if (c.v == i) mp[c.u].push_back({n+1,c.w}); else mp[c.u].push_back({c.v,c.w}); } ll tmp = dij(i,n+1); ans[i] = (tmp == INF ? -1 : tmp); } for (int i=1;i<=n;i++) cout << ans[i] << endl; } int main() {closeSync; // int T; // cin >> T; // while (T--) solve(); return 0; }
#include <bits/stdc++.h> int ri() { int n; scanf("%d", &n); return n; } bool solve() { int n = ri(); int id[2 * n]; for (auto &i : id) i = -1; int paired[2 * n]; for (auto &i : paired) i = -1; for (int i = 0; i < n; i++) { int a = ri() - 1; int b = ri() - 1; if (a != -2 && b != -2 && a >= b) return false; if (a != -2) { if (id[a] != -1) return false; id[a] = i; } if (b != -2) { if (id[b] != -1) return false; id[b] = i; } if (a != -2 && b != -2) { paired[a] = b; paired[b] = a; } } bool ok[2 * n + 1]; memset(ok, 0, sizeof(ok)); ok[0] = 1; for (int i = 0; i < 2 * n; i++) { if (!ok[i]) continue; for (int j = 1; i + j * 2 <= 2 * n; j++) { bool t = true; for (int k = i; k < i + j; k++) if (id[k] != -1 && id[k + j] != -1 && id[k] != id[k + j]) { t = false; break; } for (int k = i; k < i + j; k++) if (id[k] != -1 && paired[k] != -1 && paired[k] != k + j) { t = false; break; } for (int k = i + j; k < i + j + j; k++) if (id[k] != -1 && paired[k] != -1 && paired[k] != k - j) { t = false; break; } if (t) ok[i + j * 2] = true; } } return ok[2 * n]; } int main() { puts(solve() ? "Yes" : "No"); return 0; }
#include <bits/stdc++.h> using namespace std; #define fr(i,n) for(int i = 0; i<n; i++) #define sz(v) (int)(v.size()) #define prin(a) cout << #a << " = " << a << endl #define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl #define all(v) (v).begin(),(v).end() typedef long long ll; #define rmin(a,b) a = min<ll>(a,b) #define rmax(a,b) a = max<ll>(a,b) #define fi first #define se second const int N = 100; int mat[N][N]; int n, m, a, b; int ans = 0; bool valid(int i, int j){ return i>=0 and i<n and j>=0 and j<m; } bool poe(int cor, int io, int jo, int nn, int mm){ fr(di,nn) fr(dj,mm){ int i = io+di, j = jo+dj; if(!valid(i,j) or mat[i][j]==cor) return 0; } fr(di,nn) fr(dj,mm){ int i = io+di, j = jo+dj; mat[i][j] = cor; } return 1; } void prinmat(){ fr(i,n){ fr(j,m) cout << mat[i][j]; cout << endl; } cout << endl; } void rec(int id = 0){ int achou = 0; //prin(id); fr(i,n) fr(j,m){ if(!mat[i][j]){ if(a and poe(1,i,j,2,1)){ //prinmat(); a--; rec(id+1); poe(0,i,j,2,1); a++; //prinmat(); } if(a and poe(1,i,j,1,2)){ a--; rec(id+1); poe(0,i,j,1,2); a++; } if(b and poe(1,i,j,1,1)){ b--; rec(id+1); poe(0,i,j,1,1); b++; } achou = 1; goto sai; } } sai: if(!achou){ ans++; //prin(id); } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> a >> b; rec(); cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef vector<vpii> vvpii; typedef vector<vpll> vvpll; #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) (int)(x).size() #define fi first #define se second template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;} template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;} namespace debug { void __print(int x) {cerr << x;} void __print(long long 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 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 z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);} #ifdef ljuba #define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x) #else #define dbg(x...) #endif } using namespace debug; const char nl = '\n'; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); vi adj[30]; bool marked[30]; vi adj2[30]; int boje[30]; bool moze = 1; void dfs(int s, int tren = 0) { if(!moze) return; boje[s] = tren; for(auto e : adj2[s]) { if(boje[e] == boje[s]) { moze = 0; return; } if(boje[e] != -1) continue; dfs(e, tren^1); } } void solve() { int n, m; cin >> n >> m; for(int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a, --b; adj[a].pb(b); adj[b].pb(a); } ll ans = 0; for(int i = 0; i < n; ++i) boje[i] = -1; for(int mask = 0; mask < (1<<n); ++mask) { for(int i = 0; i < n; ++i) { marked[i] = (mask>>i)&1; } bool isti = 0; for(int i = 0; i < n; ++i) { for(auto e : adj[i]) { if(marked[e] && marked[i]) { isti = 1; break; } } if(isti) break; } if(isti) continue; for(int i = 0; i < n; ++i) { if(marked[i]) continue; for(auto e : adj[i]) { if(!marked[e]) adj2[i].pb(e); } } int komp = 0; moze = 1; for(int i = 0; i < n; ++i) { if(marked[i]) continue; if(boje[i] == -1) { ++komp; dfs(i); if(!moze) { break; } } } if(moze) { ll tren = 1; for(int i = 0; i < komp; ++i) tren *= 2; ans += tren; //dbg(mask, komp, tren); } for(int i = 0; i < n; ++i) { adj2[i].clear(); boje[i] = -1; } } cout << ans << nl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int testCases = 1; //cin >> testCases; while(testCases--) solve(); }
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<class T> ostream &operator<<(ostream &os, const vector<T> &x){ os << "{"; for(size_t i = 0; i < x.size(); i++){ if(i < x.size()-1) os << x[i] << ", "; else os << x[i]; } os << "}"; return os; } int main(void){ int n, m; cin >> n >> m; vector<vector<int>> g(n); rep(j, m){ int s, t; cin >> s >> t; s--, t--; g[s].push_back(t); g[t].push_back(s); } vector<int> idx(n, -1); auto dfs = [&](auto dfs, int now, array<int, 20> &v)->lli{ for(auto to : g[now]){ if(v[to] == v[now]){ return 0; } } lli res = 1; for(auto to : g[now]){ if(idx[to] == -1) idx[to] = idx[now]+1; if(idx[to] == idx[now]+1){ lli tmp = 0; v[to] = (v[now]+1)%3; tmp += dfs(dfs, to, v); v[to] = (v[now]+2)%3; tmp += dfs(dfs, to, v); v[to] = -1; res *= tmp; if(tmp == 0) break; } } return res; }; lli ans = 1; array<int, 20> v; v.fill(-1); rep(i, n){ if(idx[i] != -1) continue; idx[i] = 0; v[i] = 0; ans *= dfs(dfs, i, v)*3; } cout << ans << endl; return 0; }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for(int i = 0; i < (int)(n); ++i) typedef long long ll; int n; int g[21]; bool memo2[1<<18]; bool visited[1<<18]; bool isOK(int pat) { bool &res = memo2[pat]; if(!visited[pat]) { visited[pat] = true; res = true; REP(i, n) { if(pat & (1 << i)) { if((g[i] & pat) != pat) { res = false; break; } } } } return res; } int memo[1 << 18]; int solve(int rest) { int &res = memo[rest]; if(res < 0) { if(rest == 0) { res = 0; } else if(((rest-1) & rest) == 0) { res = 1; } else { int first = rest & -rest; int rest2 = rest ^ first; res = 1 + solve(rest2); for(int s = rest2; s; s = (s-1) & rest2) { if(isOK(s | first)) { res = min(res, 1 + solve(rest ^ first ^ s)); } } } } return res; } int main(void) { int nEdge; scanf("%d%d", &n, &nEdge); REP(i, n) { g[i] |= 1 << i; } REP(i, nEdge) { int a, b; scanf("%d%d", &a, &b); --a; --b; g[a] |= 1 << b; g[b] |= 1 << a; } // if(nEdge == n*(n-1) / 2) { // cout << 1 << endl; // return 0; // } memset(memo, -1, sizeof memo); int res = solve((1 << n)-1); cout << res << endl; return 0; }
#include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <stack> #include <cassert> #include <map> #include <numeric> #include <cstring> #include <set> #include <ctime> #include <queue> #include <cmath> #include <iomanip> #include <iterator> #include <bitset> using namespace std; clock_t timeStart, timeFinish; void timeBegin() { timeStart = clock(); } void timeEnd() { timeFinish = clock(); } void timeDuration() { double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC); cout << "Time taken by program is : " << fixed << time_taken << setprecision(5); cout << " sec " << endl; } class InputReader { public: InputReader() { input_file = stdin; cursor = 0; fread(buffer, SIZE, 1, input_file); } InputReader(const char *file_name) { input_file = fopen(file_name, "r"); cursor = 0; fread(buffer, SIZE, 1, input_file); } inline InputReader &operator >>(int &n) { while((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') { advance(); } int sign = 1; if (buffer[cursor] == '-') { sign = -1; advance(); } n = 0; while('0' <= buffer[cursor] && buffer[cursor] <= '9') { n = n * 10 + buffer[cursor] - '0'; advance(); } n *= sign; return *this; } inline InputReader &operator >>(long long &n) { while((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') { advance(); } int sign = 1; if (buffer[cursor] == '-') { sign = -1; advance(); } n = 0; while('0' <= buffer[cursor] && buffer[cursor] <= '9') { n = n * 10 + buffer[cursor] - '0'; advance(); } n *= sign; return *this; } private: FILE *input_file; static const int SIZE = 1 << 17; int cursor; char buffer[SIZE]; inline void advance() { ++ cursor; if(cursor == SIZE) { cursor = 0; fread(buffer, SIZE, 1, input_file); } } }; const int MAXN = 500000; char s[1 + MAXN], t[1 + MAXN]; vector<int> ones; int main() { //timeBegin(); //ifstream cin("input.txt"); //ofstream cout("papagali.out"); int n; cin >> n >> (s + 1) >> (t + 1); int s1 = 0, t1 = 0; for (int i = 1; i <= n; i++) { if (s[i] == '1') { s1++; ones.push_back(i); } if (t[i] == '1') { t1++; } } reverse(ones.begin(), ones.end()); if (t1 % 2 != s1 % 2 || t1 > s1) { cout << "-1\n"; return 0; } long long answer = 0; for (int i = 1; i <= n; i++) { if (t[i] == '0') { if (!ones.empty() && ones.back() <= i) { int x = ones.back(); ones.pop_back(); if (ones.empty()) { cout << "-1\n"; return 0; } int y = ones.back(); ones.pop_back(); answer += y - x; } } else { if (ones.empty()) { cout << "-1\n"; return 0; } int x = ones.back(); ones.pop_back(); answer += x - i; } } cout << answer << "\n"; return 0; }
/* author: Kamal Kant Bhardwaj date: */ #include<bits/stdc++.h> using namespace std; #define N 3005 #define ff first #define ss second #define int long long #define pb push_back #define mp make_pair #define pii pair<int,int> #define vi vector<int> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int> > #define setbits(newx) __builtin_popcountll(newx) #define zrobits(newx) __builtin_ctzll(newx) #define mod 1000000007 #define inf 1e18 #define ps(newx,y) finewxed<<setprecision(y)<<newx #define mk(arr,n,type) type *arr=new type[n]; #define w(newx) int newx; cin>>newx; while(newx--) void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int mat[10005][103]; int32_t main() { c_p_c(); int n; cin >> n; string s; cin >> s; int a[103]; for (int i = 0; i <= n; i++) cin >> a[i]; int maxi = mod; for (int i = 1; i <= n; i++) { maxi = min(maxi, abs(a[i] - a[i - 1])); } int sum = 0; for (int i = 0; i < 10005; i++) for (int j = 0; j < 103; j++) mat[i][j] = 0; int div = a[0] / maxi; int rem = a[0] % maxi; for (int j = 0; j < maxi; j++) { mat[j][0] = div ; if (rem > 0) { mat[j][0] += 1; } rem--; sum += mat[j][0]; } for (int i = 1; i <= n; i++) { // int newsum = 0; // int diff = abs(a[i] - sum); int div = a[i] / maxi; int rem = a[i] % maxi; // cout << div << " " << rem << endl; for (int j = 0; j < maxi; j++) { mat[j][i] = div; if (rem > 0) mat[j][i] += 1; rem--; } } cout << maxi << endl; for (int j = 0; j < maxi; j++) { for (int i = 0; i <= n; i++) { cout << mat[j][i] << " "; } cout << endl; } return 0; }
#include <bits/stdc++.h> using i32 = std::int32_t; using u32 = std::uint32_t; using i64 = std::int64_t; using u64 = std::uint64_t; using i128 = __int128_t; using u128 = __uint128_t; using isize = std::ptrdiff_t; using usize = std::size_t; class rep { struct Iter { usize itr; constexpr Iter(const usize pos) noexcept : itr(pos) {} constexpr void operator++() noexcept { ++itr; } constexpr bool operator!=(const Iter& other) const noexcept { return itr != other.itr; } constexpr usize operator*() const noexcept { return itr; } }; const Iter first, last; public: explicit constexpr rep(const usize first, const usize last) noexcept : first(first), last(std::max(first, last)) {} constexpr Iter begin() const noexcept { return first; } constexpr Iter end() const noexcept { return last; } }; template <class T, T Div = 2> constexpr T INFTY = std::numeric_limits<T>::max() / Div; template <class T> bool setmin(T& lhs, const T& rhs) { if (lhs > rhs) { lhs = rhs; return true; } return false; } template <class T> using Vec = std::vector<T>; void A_main() { usize N; std::cin >> N; Vec<char> S(N); for (auto& x : S) { std::cin >> x; } Vec<u32> A(N + 1); for (auto& x : A) { std::cin >> x; } u32 min = INFTY<u32>; for (const auto i : rep(0, N)) { setmin(min, (A[i] < A[i + 1] ? A[i + 1] - A[i] : A[i] - A[i + 1])); } std::cout << min << '\n'; for (const auto k : rep(0, min)) { for (const auto i : rep(0, N + 1)) { std::cout << A[i] / min + (k < (A[i] % min)) << " \n"[i == N]; } } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); A_main(); return 0; }
#include <iostream> #include <cassert> using namespace std; const int K = 4; int main() { int N; string s; cin >> N >> s; for (int i = 0; i < N; ++i) { if (s[i] == 'W') s[i] = '1'; if (s[i] == 'R') s[i] = '2'; if (s[i] == 'B') s[i] = '3'; } int p3 = 1; while (p3 < N) p3 *= 3; p3 /= 3; for (; p3 >= 1; p3 /= 3) { while (s.size() > p3) { string news = ""; for (int j = 0; j < s.size()-p3; ++j) news += (9 - (s[j]-'0' + s[j+p3]-'0' + 1)) % 3 + '1'; // cout << p3 << ' ' << news << '\n'; s = news; } } assert(s.size() == 1); if (s[0] == '1') putchar('W'); if (s[0] == '2') putchar('R'); if (s[0] == '3') putchar('B'); putchar('\n'); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef double db; typedef pair <int,int> Pii; #define reg register #define mp make_pair #define pb push_back #define Mod1(x) ((x>=P)&&(x-=P)) #define Mod2(x) ((x<0)&&(x+=P)) #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i) template <class T> inline void cmin(T &a,T b){ ((a>b)&&(a=b)); } template <class T> inline void cmax(T &a,T b){ ((a<b)&&(a=b)); } char IO; template <class T=int> T rd(){ T s=0; int f=0; while(!isdigit(IO=getchar())) f|=IO=='-'; do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return f?-s:s; } const int N=1e6+10,INF=1e9+10; int n; char s[N]; char ch[]="BWR"; int F[N],cnt[N]; int C(int n,int m){ if(cnt[n]-cnt[m]-cnt[n-m]) return 0; return F[n]*F[m]*F[n-m]%3; } int main(){ rep(i,F[0]=1,N-1) { cnt[i]=cnt[i-1],F[i]=F[i-1]; int x=i; while(x%3==0) x/=3,cnt[i]++; F[i]=F[i]*x%3; } n=rd(),scanf("%s",s+1); int sum=0; rep(i,1,n) { int t=0; if(s[i]=='W') t=1; if(s[i]=='R') t=2; if(~n&1) t=3-t; sum=(sum+C(n-1,i-1)*t)%3; } //if(~n&1) sum=n*3-sum; sum%=3,putchar(ch[sum]); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); ++i) using ll = long long; const ll inf = 1e18; ll solve() { int n; cin >> n; vector<vector<ll>> r(3); rep(i, 2 * n) { ll a; char c; cin >> a >> c; r[string("RGB").find(c)].push_back(a); } rep(i, 3) { r[i].push_back(-inf); r[i].push_back(inf); sort(r[i].begin(), r[i].end()); } auto f = [&](int i, int j) -> ll { ll res = inf; for(auto &x : r[i]) { if(abs(x) == inf) continue; auto it = upper_bound(r[j].begin(), r[j].end(), x); res = min(res, min(*it - x, x - *prev(it))); } return res; }; rep(i, 3) { int j = (i + 1) % 3; int k = (i + 2) % 3; if(r[j].size() % 2 && r[k].size() % 2) return min(f(j, k), f(i, j) + f(i, k)); } return 0; } int main() { cout << solve() << '\n'; return 0; }
#include<cstdio> #include<algorithm> #define N 300010 #define db double #define ll long long #define fo(x,a,b) for(int x=(a);x<=(b);x++) #define fd(x,a,b) for(int x=(a);x>=(b);x--) using namespace std; struct node{ll x; int id;}a[N]; int n,t[4],tot1=0,tot2=0,tot3=0,tot4=0; ll b[N],c[N],d[N],e[N]; ll ans=(1ll<<50),ans1=(1ll<<50),ans2=(1ll<<50); inline ll read() { ll x=0;int f=0;char c=getchar(); while (c<'0'||c>'9') f=(c=='-')?1:f,c=getchar(); while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); return f?-x:x; } int gc() { char c=getchar(); while (c!='R'&&c!='G'&&c!='B') c=getchar(); if (c=='R') return 1; else if (c=='G') return 2; else return 3; } inline bool cmp(node x,node y) {return x.x<y.x;} void calc1() { ans1=ans2=(1ll<<50); int pos=-1; fo(i,1,tot1) { int l=1,r=tot2,mid; ll res=(1ll<<50); while (l<=r) { mid=(l+r)>>1; res=min(res,abs(c[mid]-b[i])); if (c[mid]>=b[i]) r=mid-1; else l=mid+1; } if (res<ans1) ans1=res,pos=i; } fo(i,1,tot1) { int l=1,r=tot3,mid; ll res=(1ll<<50); while (l<=r) { mid=(l+r)>>1; res=min(res,abs(d[mid]-b[i])); if (d[mid]>=b[i]) r=mid-1; else l=mid+1; } if (res<ans2&&i!=pos) ans2=res; } ans=min(ans,ans1+ans2); } void calc2() { ans1=ans2=(1ll<<50); int pos=-1; fo(i,1,tot1) { int l=1,r=tot3,mid; ll res=(1ll<<50); while (l<=r) { mid=(l+r)>>1; res=min(res,abs(d[mid]-b[i])); if (d[mid]>=b[i]) r=mid-1; else l=mid+1; } if (res<ans2) ans2=res,pos=i; } fo(i,1,tot1) { int l=1,r=tot2,mid; ll res=(1ll<<50); while (l<=r) { mid=(l+r)>>1; res=min(res,abs(c[mid]-b[i])); if (c[mid]>=b[i]) r=mid-1; else l=mid+1; } if (res<ans1&&i!=pos) ans1=res; } ans=min(ans,ans1+ans2); } int main() { n=read();n=2*n; fo(i,1,n) { a[i].x=read(),a[i].id=gc(),t[a[i].id]++; if (a[i].id==1) b[++tot1]=a[i].x; else if (a[i].id==2) c[++tot2]=a[i].x; else d[++tot3]=a[i].x; } sort(a+1,a+n+1,cmp); if (t[1]%2==0&&t[2]%2==0&&t[3]%2==0) return 0&printf("0\n"); sort(b+1,b+tot1+1); sort(c+1,c+tot2+1); sort(d+1,d+tot3+1); if (tot2%2==0) { swap(b,c),swap(tot1,tot2); } else if (tot3%2==0) { swap(b,d),swap(tot1,tot3); } fo(i,1,tot2) { int l=1,r=tot3,mid; ll res=(1ll<<50); while (l<=r) { mid=(l+r)>>1; res=min(res,abs(d[mid]-c[i])); if (d[mid]>=c[i]) r=mid-1; else l=mid+1; } ans=min(ans,res); } calc1(); calc2(); printf("%lld\n",ans); return 0; }
#pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include<stdio.h> #include<algorithm> constexpr unsigned long long MOD = 998244353; constexpr unsigned long long IM = 18479187003; constexpr unsigned long long MOD32 = 665496236; char f[5000][5000]; unsigned long long dp[5000]; int main(){ int H=0, W=0, K=0; char c=getchar_unlocked(); while(c<='9'&&c>='0') {H=(H<<1)+(H<<3)+c-'0'; c=getchar_unlocked();} c=getchar_unlocked(); while(c<='9'&&c>='0') {W=(W<<1)+(W<<3)+c-'0'; c=getchar_unlocked();} c=getchar_unlocked(); while(c<='9'&&c>='0') {K=(K<<1)+(K<<3)+c-'0'; c=getchar_unlocked();} unsigned long long y = H * W - K; while(--K>=0){ int h=0, w=0; c=getchar_unlocked(); while(c<='9'&&c>='0') {h=(h<<1)+(h<<3)+c-'0'; c=getchar_unlocked();} c=getchar_unlocked(); while(c<='9'&&c>='0') {w=(w<<1)+(w<<3)+c-'0'; c=getchar_unlocked();} f[h-1][w-1] = getchar_unlocked(); getchar_unlocked(); } --H; --W; dp[0] = 1; for(int i = 0; i < H; ++i){ for (int j = 0; j < W; ++j){ switch(f[i][j]) { case 'R': dp[j + 1] += dp[j]; dp[j] = 0; break; case '\0':{ dp[j] *= MOD32; #ifdef _MSC_VER unsigned long long x; _umul128(dp[j], IM, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64); #endif dp[j] = dp[j] - x * MOD+MOD; } case 'X': dp[j + 1] += dp[j]; } } switch(f[i][W]) { case 'R': dp[W] = 0; break; case '\0':{ dp[W] *= MOD32; #ifdef _MSC_VER unsigned long long x; _umul128(dp[W], IM, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(dp[W])*IM) >> 64); #endif dp[W]= dp[W] - x * MOD+MOD; } } } for (int j = 0; j < W; ++j){ switch(f[H][j]) { case '\0':{ dp[j] *= MOD32; #ifdef _MSC_VER unsigned long long x; _umul128(dp[j], IM, &x); #else unsigned long long x = (unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64); #endif dp[j] = dp[j] - x * MOD+MOD; } case 'R': case 'X': dp[j + 1] += dp[j]; } } unsigned long long x = 3; while(y) { if(y & 1) { dp[W] *= x; #ifdef _MSC_VER unsigned long long x2; _umul128(dp[W], IM, &x2); #else unsigned long long x2 = (unsigned long long)(((unsigned __int128)(dp[W])*IM) >> 64); #endif dp[W] = dp[W] - x2 * MOD+MOD; } { x *= x; #ifdef _MSC_VER unsigned long long x2; _umul128(x, IM, &x2); #else unsigned long long x2 = (unsigned long long)(((unsigned __int128)(x)*IM) >> 64); #endif x = x- x2 * MOD + MOD;} y >>= 1; } printf("%d", dp[W] % MOD); return 0; }
#include <bits/stdc++.h> using namespace std; #define INF 1020304050607080901 long long steps(long long left, long long t) { map<long long,long long> m; m[left] = 0; queue<long long> q; long long td = 1LL<<t; q.push(left); while (!q.empty()) { long long now = q.front(); q.pop(); for (long long i=0;i<=t;i++) { long long d = 1LL<<i; if (now&d) { long long to; to = now-d; if (to >= 0) { if (m.find(to) == m.end()) { m[to] = m[now]+1; q.push(to); } } to = now+d; if (to <= td) { if (m.find(to) == m.end()) { m[to] = m[now]+1; q.push(to); } } break; } } } long long ret = INF; if (m.find(0) != m.end()) ret = min(ret,m[0]); // if (m.find(td) != m.end()) ret = min(ret,m[td]); return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long x,y; cin >> x >> y; long long ans = abs(x-y); long long beg = x; long long t = 0; while (true) { long long kans = t; long long left = abs(beg-y); long long d = 1LL<<t; kans += left/d; kans += steps(left%d,t); //cout << beg << ' ' << kans << endl; ans = min(ans,kans); if (beg > y) break; beg *= 2; t++; } /* for (int i=0;i<7;i++) { for (int j=0;j<(1<<i);j++) { cout << j << ' ' << i << ' ' << steps(j,i) << endl; } }*/ cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define bolt \ ios::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0) #define test \ int tt; \ cin >> tt; \ while (tt--) #define ll long long #define ld long double #define vi vector<ll> #define pb push_back #define F first #define S second #define mod 1000000007 #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() int main(){ bolt; ll a,b,c,d;cin>>a>>b>>c>>d; if(d>=a*b && d<=a*c){ cout<<"No"<<endl; }else{ cout<<"Yes"<<endl; } }
#include <iostream> using namespace std; int main(void) { double v, t, s, d; cin >> v >> t >> s >> d; if(d/v-s > 1e-7 || 1e-7 < t-d/v) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
//tt89 ;) #include<bits/stdc++.h> using namespace std; //typedef long long ll; #define int long long #define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define clock cerr<<"\n"<<(float)clock()/CLOCKS_PER_SEC*1000<<" ms"<<endl; #define ff first #define ss second void testcase(int t) { cout<<"Case #"<<t<<": "; } void online() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif //ONLINE_JUDGE } const int mod= 1e9+7; int n,m,k,x; vector<int> a,b,c,d,arr; int getAns() { int ans=0; for(int i=0;i<m;i++) { if(arr[a[i]]>0 && arr[b[i]]>0) ans++; } return ans; } void getRec(int i) { if(i==k) { int y=getAns(); x= max(x,y); return; } arr[c[i]]++; getRec(i+1); arr[c[i]]--; arr[d[i]]++; getRec(i+1); arr[d[i]]--; } int solve() { x=0; getRec(0); return x; } signed main() { fastIO; int T=1; // cin>>T; for(int t=1;t<=T;t++) { cin>>n>>m; arr.resize(n+1,0); a.resize(m); b.resize(m); for(int i=0;i<m;i++) cin>>a[i]>>b[i]; cin>>k; c.resize(k); d.resize(k); for(int i=0;i<k;i++) cin>>c[i]>>d[i]; cout<<solve()<<"\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define FAST ios_base::sync_with_stdio(false);cin.tie(0); const double EPS = 1e-9; const double PI = acos(-1); const int knightDir[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,-1},{2,1},{-1,-2},{1,-2}}; //const int dx[]={0,0,0,1,1,1,-1,-1,-1}; //const int dy[]={-1,0,1,-1,0,1,-1,0,1}; const int dx[] = {0 , 1 , 0 , -1}; const int dy[] = {1 , 0 , -1 , 0}; //#define MOD 1000000007LL #define MAX 200015 const ll MOD = 998244353; inline ll mod(ll a){return ((a % MOD + MOD) % MOD);} inline ll add(ll a, ll b){return mod(mod(a) + mod(b));} inline ll sub(ll a, ll b){return mod(mod(a) - mod(b));} int l[MAX],r[MAX]; vector<int> graph[MAX]; int depth[MAX] , vis[MAX]; ll c[MAX]; void depthDFS(int now , int d) { depth[now] = d; for(int a : graph[now]) { if(!vis[a]) { vis[a] = 1; depthDFS(a , d + 1); } } } void sumDFS(int now , ll point) { c[now] += point; for(int a : graph[now]) { if(!vis[a]) { vis[a] = 1; sumDFS(a , c[now]); } } } int main() { FAST int t; t = 1; while(t--) { int n; cin >> n; for(int i = 1 ; i < n ; i++) { int a , b; cin >> a >> b; a-- , b--; l[i] = a , r[i] = b; graph[a].push_back(b); graph[b].push_back(a); } vis[0] = 1; depthDFS(0 , 0); int q; cin >> q; for(int i = 0 ; i < q ; i++) { int t , e , x; cin >> t >> e >> x; int yes = l[e], no = r[e]; if(t == 2) { swap(yes , no); } if(depth[yes] < depth[no]) { c[0] += x; c[no] -= x; } else c[yes] += x; } memset(vis , 0 , sizeof(vis)); vis[0] = 1; sumDFS(0 , 0); for(int i = 0 ; i < n ; i++)cout << c[i] << endl; } }
#include <bits/stdc++.h> // #include <atcoder/all> using namespace std; // using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i<n; i++) #define repx(i, l, n) for(int i=l; i<n; i++) #define all(v) v.begin(), v.end() #define show(x) cout << #x << ": " << x << endl; #define list(x) cout << #x << ": " << x << " "; #define pb push_back using vi = vector<lint>; using vvi = vector<vector<lint>>; template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> inline void drop(T x) { cout << x << endl; exit(0); } template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; int N, M, TIME=clock(); vector<pair<int, string>> V; vi C(8); int sum = 0; vector<string> S(20); int total() { string s; int p = 0; set<string> t, u; rep(i, M) t.insert(V[i].second); vector<string> v(20); rep(i, N) v[i] = S[i]; rep(q, 2) { rep(i, N) { s = v[i] + v[i]; for (auto a : t) { bool f = false; rep(j, N) { if (a == s.substr(j, a.size())) { f = true; break; } } if (f) p += a.size(); else u.insert(a); } t.clear(); swap(t, u); } rep(i, N) { v[i].clear(); rep(j, N) v[i] += S[j][i]; } } return p; } int delta(int a, int b, int x, int y) { string s; int p = 0; set<string> t, u; rep(i, M) t.insert(V[i].second); rep(q, 2) { s = S[a] + S[a]; for (auto a : t) { bool f = false; int c = (b+N-a.size()+1)%N; rep(j, a.size()) { if (a == s.substr(c+j, a.size())) { f = true; break; } } if (f) p += a.size(); else u.insert(a); } t.clear(); swap(t, u); swap(a, x); } rep(q, 2) { s = ""; rep(i, N) s += S[i][b]; rep(i, N) s += S[i][b]; for (auto a : t) { bool f = false; int c = (b+N-a.size()+1)%N; rep(j, a.size()) { if (a == s.substr(c+j, a.size())) { f = true; break; } } if (f) p += a.size(); else u.insert(a); } t.clear(); swap(t, u); swap(b, y); } return p; } int delta_1(int a, int b) { string s; int p = 0; set<string> t, u; rep(i, M) t.insert(V[i].second); s = S[a] + S[a]; for (auto a : t) { bool f = false; int c = (b+N-a.size()+1)%N; rep(j, a.size()) { if (a == s.substr(c+j, a.size())) { f = true; break; } } if (f) p += a.size(); else u.insert(a); } t.clear(); swap(t, u); s = ""; rep(i, N) s += S[i][b]; rep(i, N) s += S[i][b]; for (auto a : t) { bool f = false; int c = (b+N-a.size()+1)%N; rep(j, a.size()) { if (a == s.substr(c+j, a.size())) { f = true; break; } } if (f) p += a.size(); else u.insert(a); } t.clear(); swap(t, u); return p; } int main() { cin >> N >> M; string s; rep(i, M) { cin >> s; V.pb({s.size(), s}); rep(j, s.size()) C[s[j]-'A']++; } rep(i, 8) sum += C[i]; rep(i, 7) C[i+1] += C[i]; sort(all(V)); int a=0, b=0, c=0, x, y, z; rep(i, N) { rep(j, N) { x = rand()%sum; rep(k, 8) { if (x < C[k]) { S[i] += 'A'+k; break; } } } } // show(total()) int count = 0; while (true) { count++; a = rand()%N; b = rand()%N; if (count%5 == 0) { y = S[a][b]-'A'; x = y; while (x == y) x = rand()%8; c = delta_1(a, b); S[a][b] = 'A'+x; z = delta_1(a, b); if (c > z) S[a][b] = 'A'+y; } else { x = rand()%N; y = rand()%N; c = delta(a, b, x, y); swap(S[a][b], S[x][y]); z = delta(a, b, x, y); if (c > z) swap(S[a][b], S[x][y]); } // list(c)list(z) show(total()) if (clock()-TIME > 2.95 * CLOCKS_PER_SEC) break; } // show(count) rep(i, N) std::cout << S[i] << '\n'; }
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; void rotate(string &s , int x) { string t = s; for (int i = 0; i < s.size(); i++) { t[(i + x) % s.size()] = s[i]; } s = t; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n , m; cin >> n >> m; vector<string> sb(m); for (int i = 0; i < m; i++) { cin >> sb[i]; } for (int i = 0; i < m - 1; i++) { for (int j = i + 1; j < m; j++) { if (sb[i].find(sb[j]) != -1) { sb[j] = "nothing"; } } } vector<pair<int,string> > s; for (int i = 0; i < m; i++) { if (sb[i] == "nothing") continue; s.push_back(make_pair(sb[i].size() , sb[i])); } sort(s.begin() , s.end()); s.erase(unique(s.begin() , s.end()),s.end()); reverse(s.begin() , s.end()); m = s.size(); vector<string> ans(n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans[i] += '.'; } } const int sz = m/ n; vector<vector<string> > s2(sz); for (int i = 0; i < sz; i++) { for (int j = i * n; j < (i + 1) * n; j++) { s2[i].push_back(s[j].second); } } vector<vector<bool> > used(sz , vector<bool>(n , false)); for (int i = 0; i < sz; i++) { for (int j = 0; j < s2[i].size(); j++) { string t = s2[i][j]; bool ok = false; string t2 = t; int id = 0; for (int k = 0; k < ans[j].size(); k++) { if (ans[j][k] != '.' && id < t2.size()) { if (ans[j][k] == t2[id]) { id++; if (id == t2.size()) { ok = true; } } } if (ans[j][k] == '.') { for (int l = 0; l < t.size(); l++) { if (ok) { used[i][j] = true; break; } if (n - k + 1 < t.size()) { break; } else { ans[j][k + l] = t[l]; used[i][j] = true; } } break; } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (ans[i][j] == '.') { ans[i][j] = 'A'; } } } for (int i = 0; i < n; i++) { rotate(ans[i] , i); } for (int i = 0; i < n; i++) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, s) for (int i = 0; i < s; ++i) #define ALL(v) (v).begin(), (v).end() #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) #define DEBUG //#define int long long #define INF 1e18 template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template<class T> ostream& operator << (ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template<class T> ostream& operator << (ostream &s, set<T> P) { EACH(it, P) { s << "<" << *it << "> "; } return s << endl; } template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";} typedef long long ll; using Edge = pair<int, int>; using Graph = vector<vector<Edge>>; Graph G; int n, d[250010]; // <最短距離, 頂点の番号> using P = pair<int, int>; void dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; fill(d, d + n, INF); d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; for (int i = 0; i < G[v].size(); ++i) { Edge e = G[v][i]; if (d[e.first] > d[v] + e.second) { d[e.first] = d[v] + e.second; que.push(P(d[e.first], e.first)); } } } } int a[510][510]; int b[510][510]; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int r,c; cin>>r>>c; n=r*c; G.assign(r*c , vector<Edge>()); for (int i = 0; i < r; i++) { for (int j = 0; j < c-1; j++) { cin>>a[i][j]; } } for (int i = 0; i < r-1; i++) { for (int j = 0; j < c; j++) { cin>>b[i][j]; } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if(j<c-1) G[c*i + j].push_back({c*i+j+1, a[i][j]}); if(j>=1) G[c*i + j].push_back({c*i+j-1, a[i][j-1]}); if(i<r-1) G[c*i + j].push_back({c*(i+1)+j, b[i][j]}); } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { for (int k = 1; k < r; k++) { if(i - k < 0 ) continue; G[c*i+j].push_back({c*i+j - c*k , 1 + k}); } } } dijkstra(0); cout<<d[r*c-1]<<endl; return 0; }
#include <bits/stdc++.h> typedef unsigned long long ull; using namespace std; const double maxv = 1e100; ull n, k, money, ans, ai, bi; vector <vector <long long> > ab; int main() { cin >> n >> k; money = k; for(int i = 0; i < n; i++) { cin >> ai >> bi; vector <long long> abi; abi.push_back(ai); abi.push_back(bi); ab.push_back(abi); } sort(ab.begin(), ab.end()); for(int i = 0; i < n && money > 0 && ans <= maxv; i++) { if((i > 0 && money < ab[i][0] - ab[i - 1][0]) || (i == 0 && money < ab[i][0])) { ans += money; money = 0; break; } if(i == 0) { money -= ab[i][0]; money += ab[i][1]; ans += ab[i][0]; } else { money -= ab[i][0] - ab[i - 1][0]; money += ab[i][1]; ans += ab[i][0] - ab[i - 1][0]; } } if(money > 0) ans += money; if(money < 0) ans -= 0 - money; cout << ans; return 0; }
#include <iostream> #include <cmath> #include <vector> #include <algorithm> #include <string> #include <numeric> #include <queue> #include <fstream> #include <stdio.h> #include <bitset> #include <set> #include <iomanip> #include <random> #include <iterator> #include <map> using namespace std; typedef long long ll; typedef unsigned long long unll; #define fi first #define se second void dfs(int v, vector<vector<int>>& adj, vector<bool>& used) { used[v] = 1; for (int i = 0; i < adj[v].size(); ++i) { if (used[adj[v][i]]) continue; dfs(adj[v][i], adj, used); } } void solve() { int n, res = 0; cin >> n; vector<int> a(n); for (auto& i : a) cin >> i; vector<vector<int>> adj(200000); for (int i = 0; i < n; ++i) { adj[a[i] - 1].push_back(a[n - 1 - i] - 1); } vector<bool> used(200000, 0); for (int i = 0; i < n; ++i) { if (used[a[i] - 1]) continue; dfs(a[i] - 1, adj, used); --res; } sort(a.begin(), a.end()); for (int i = 1; i < n; ++i) if (a[i - 1] != a[i]) ++res; ++res; cout << res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr << '\n'; } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; err(++it, args...); } #define optimize ios::sync_with_stdio(0); cin.tie(0); #define PI acos(-1.0) #define YES cout<<"YES"<<endl; #define NO cout<<"NO"<<endl; #define RESET(a, b) memset(a, b, sizeof(a)) #define pii pair <int, int> #define min3(a, b, c) min(c, min(a, b)) #define max3(a, b, c) max(c, max(a, b)) const ll MX = 2e5 + 105; vector <int> adj[MX]; int ara[MX]; bool vis[MX]; int c = 0; void dfs(int u) { c++; vis[u] = 1; for(auto v: adj[u]){ if(!vis[v]){ vis[v] = 1; dfs(v); } } } int main() { optimize int n; cin>>n; for(int i = 0; i < n; i++){ cin>>ara[i]; } for(int i = 0; i < n/2; i++){ adj[ara[i]].push_back(ara[n-1-i]); adj[ara[n-1-i]].push_back(ara[i]); } int ans = 0; for(int i = 1; i <= 2e5; i++){ if(!vis[i]){ c = 0; dfs(i); ans += (c-1); } } cout<<ans<<endl; }
#include <bits/stdc++.h> #define all(a) a.begin(), a.end() #define forn(i,n) for(int i = 0; i < (int) n; i++) #define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) using namespace std; int main() { int a, b, x, y; cin >> a >> b >> x >> y; if (a == b) { cout << x << "\n"; return 0; } int stair = min(y, 2 * x); if (a < b) cout << (b - a) * stair + x << "\n"; else cout << (a - b - 1) * stair + x << "\n"; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int,int>; #define rep(i,n) for(int i=0; i<(int)n; i++) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてから使う #define INF 2147483647 #define LLINF 9223372036854775807LL int main(){ int n; cin>>n; ll ans = 0; rep(i,n){ ll a,b; cin>>a>>b; ans += (b-a+1) * (b+a)/2; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; //#include <atcoder/all> //using namespace atcoder; #ifndef ONLINE_JUDGE #define _GLIBCXX_DEBUG #endif #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 Clear(a) a = decltype(a)() #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define vec vector typedef long long ll; typedef pair<ll,ll> P; typedef string::const_iterator state; //構文解析 const double PI=acos(-1); //const ll big=998244353; const ll big=1000000007LL; const ll INF=1e18; const int dx[4]={1,0,-1,0}; const int dy[4]={0,1,0,-1}; 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; } ll expm(ll x,ll y,ll mod=big){ if(y==0)return 1;//0^0=1 if(x==1||x==0)return x; if(y%2==1)return (expm(x,y-1,mod)*x)%mod; ll t=expm(x,y/2,mod); return (t*t)%mod; } ll exp(ll x,ll y){ if(y==0)return 1;//0^0=1 if(x==1||y==0)return x; if(y%2==1)return exp(x,y-1)*x; ll t=exp(x,y/2); return t*t; } struct unionfind{ vector<int> par; vector<int> high; vector<int> size; vector<map<int,int>> num; unionfind(int n):par(n,-1), high(n,1), size(n,1),num(n){ } int find(const int x){ if(par[x]==-1)return x; return par[x]=find(par[x]); } void unite(int x,int y){ x=find(x); y=find(y); if(x==y)return; if(high[x]>high[y])swap(x,y); if(high[x]==high[y])high[y]++; for(auto i:num[x]){//xのグループに属すクラス全部 i.first=class i.secnod=人数 num[y][i.first]+=i.second; } par[x]=y; size[y]+=size[x]; return; } int getsize(const int i){ return size[find(i)]; } void get(const int m){ rep(i,m){ int a,b;cin >> a >> b; unite(a-1,b-1); } return; } }; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(10); /*--------------------------------*/ int n;cin >> n; int q;cin >> q; unionfind uf2(n); rep(i,n){ int cl;cin >> cl; uf2.num[i][cl-1]=1; } //各タイミングで,両方で連結しているのを求めないとだめ. rep(i,q){ int com,a,b;cin >> com >> a >> b; if(com==1){ uf2.unite(a-1,b-1); //このuniteで,同じクラスかつ連結になった個数を求めたい }else{ a=uf2.find(a-1); cout << uf2.num[a][b-1] << '\n'; } } } //・printデバッグ消した?・オーバーフローしない? //・気づいた条件分岐書き忘れてない?.....よし出せ!
#include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) using namespace std; using ll = long long; #define N 1005 int dp[N][N]; vector<int> a, b; int n, m; int solve(int i, int j) { if (i >= n) return abs(m-j); if (j >= m) return abs(n-i); int& curr = dp[i][j]; if (curr+1) return curr; curr = 1e6; curr = min(curr, 1 + solve(i+1, j)); curr = min(curr, 1 + solve(i, j+1)); curr = min(curr, 2 + solve(i+1, j+1)); if (a[i] == b[j]) { curr = min(curr, solve(i+1, j+1)); } else { curr = min(curr, 1 + solve(i+1, j+1)); } return curr; } int main() { FAST; memset(dp, -1, sizeof(dp)); cin >> n >> m; a = vector<int>(n); b = vector<int>(m); int i, j, k, x, y, sz; for (i = 0; i < n; ++i) cin >> a[i]; for (i = 0; i < m; ++i) cin >> b[i]; if (n < m) { swap(n, m); swap(a, b); } cout << solve(0,0); return 0; }
#include<map> #include<ctime> #include<cstdio> #include<cctype> #include<string> #define ll long long const ll N=1e3+10; const ll HgS=1e9+7; using namespace std; ll read() { char c; ll x=0,f=1; while(!isdigit(c=getchar())) f-=2*(c=='-'); while (isdigit(c)){ x=x*10+(c-48)*f; c=getchar(); } return x; } ll n,f,res; ll fib(ll n){ ll a=0,b=1,c=0; for(ll i=1;i<=n;++i){ c=(a+b)%HgS; a=b; b=c; } return a; } int main() { #ifndef ONLINE_JUDGE freopen("D.in","r",stdin); freopen("D.out","w",stdout); #endif clock_t t1=clock(); //-------- n=read(); for(int i=0;i<4;++i){ char c; while(!isalpha(c=getchar())); f<<=1; f|=(c=='B'); } switch(f){ case 0: case 1: case 2: case 3: case 5: case 7: case 13: case 15: res=1; break; case 4: case 10: case 11: case 12: res=1; for(int i=4;i<=n;++i){ res<<=1; res%=HgS; } break; default: res=fib(n-1); break; } printf("%lld",res); //-------- clock_t t2=clock(); fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> iint; typedef pair<ll,ll> llll; #define ALL(x) (x).begin(),(x).end() const ll zero = 0; const ll one = 1; const ll INF = 9223372036854775807; //10^18 const int inINF = 2147483647; //10^9 const ll MOD = 1000000007; //10^9+7 const ll MOD2 = 998244353; void Yes() {printf("Yes\n");} void No() {printf("No\n");} void YES() {printf("YES\n");} void NO() {printf("NO\n");} int main(){ ll N; cin >> N; char aa, ab, ba, bb; cin >> aa >> ab >> ba >> bb; string s; s = aa; s = s + ab; s = s + ba; s = s + bb; if(s == "ABBA" || s == "BAAA" || s == "BAAB" || s == "BBBA"){ vector<ll> a(N+5); a[2] = 1; a[3] = 1; for (int i = 4; i < N+3; i++) { a[i] = (a[i-1] + a[i-2]) % MOD; } printf("%lld\n", a[N]); } else if(s == "ABAA" || s == "BABA" || s == "BABB" || s == "BBAA"){ vector<ll> a(N+5); a[2] = 1; a[3] = 1; for (int i = 4; i < N+3; i++) { a[i] = a[i-1] * 2 % MOD; } printf("%lld\n", a[N]); } else{ printf("%d\n", 1); } }
#include <bits/stdc++.h> #include <random> using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair<lint, lint> plint; typedef pair<double long, double long> pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define endk '\n' template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); } lint ceil(lint a, lint b) { return (a + b - 1) / b; } lint digit(lint a) { return (lint)log10(a); } const lint MOD = 1e9 + 7, INF = 1e9; lint dx[8] = { 1, 0, -1, 0, 1, -1, 1, -1 }, dy[8] = { 0, 1, 0, -1, -1, -1, 1, 1 }; void YN(bool flag) { cout << (flag ? "Yes" : "No") << endk; } typedef pair<lint, string> Pa; typedef pair<lint, plint> tlint; lint N; lint dist[100][100]; int main() { cin >> N; REP(i, N) { REP(j, N) { dist[i][j] = INF; char c; cin >> c; if (c == '1') dist[i][j] = 1; } } REP(k, N) { REP(i, N) { REP(j, N) { chmin(dist[i][j], dist[i][k] + dist[k][j]); } } } long double ans = 0; REP(i, N) { long double sum = 1; REP(j, N) { if (i == j) continue; if (dist[j][i] != INF) sum++; } ans += (long double)1 / sum; } cout << setprecision(16) << ans << endk; }
//wtrl,everybody hangbeat me #include<bits/stdc++.h> /*#include<iostream> #include<string> #include<cmath> #include<cstdio> #include<cctype> #include<cstring> #include<iomanip> #include<cstdlib> #include<ctime> #include<set> #include<map> #include<utility> #include<queue> #include<vector> #include<stack> #include<sstream> #include<algorithm>*/ using namespace std; typedef long long ll; typedef pair<int,int> pii; typedef vector<pii> vii; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef pair<ll,ll> pll; /*=====================================================================*/ #define pb push_back #define rep(i,n) for(int i=0;i<(int)(n);i++) #define all(s) (s).begin(),(s).end() #define repd(i,n) for(int i=n-1;i>=0;i--) #define forn(i,p,n) for(int i=p;i<=n;i++) #define ford(i,p,n) for(int i=n;i>=p;i--) #define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();++i) #define INF 1e9 #define PI acos(-1) /*=====================================================================*/ string int_to_string(ll n) { string s=""; while(n) { ll now=n%10; s+=now+'0'; n/=10; } reverse(s.begin(),s.end()); return s; } ll string_to_int(string s) { ll n=0; rep(i,s.size()) { n*=10; n+=s[i]-'0'; } return n; } /*======================================================================*/ const int dx[]={-1,0,1,0}; const int dy[]={0,-1,0,1}; const int month[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}}; void solve() { int n; cin>>n; int now=0; for(int i=1;now<=1e9;i++) { now+=i; if(now>=n) { cout<<i<<endl; return; } } } /*======================================================================*/ signed main() { std::ios::sync_with_stdio(false); /* freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); */ /*====================================================================*/ int t=1; //cin>>t; while(t--) { solve(); } return 0; } /* 注意数组越界,vector长度为0的时候循环会RE 注意输入顺序 注意循环时int*int爆ll 注意答案存爆ll */
#include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> #define M 100001 #define inf 2147483647 typedef long long ll; int max(int a, int b) { return a > b ? a : b; } int min(int a, int b) { return a < b ? a : b; } inline void read(int &T) { int x = 0; bool f = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = !f; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } T = f ? -x : x; } int a, b, x, y, ans = inf; int main() { read(a), read(b), read(x), read(y); y = min(y ,2 * x); if (a > b) std::cout << x + abs((a - 1) - b) * y; if (a <= b) std::cout << x + abs(a - b) * y; //std::cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> //#include <ext/pb_ds/tree_policy.hpp> //#include <ext/pb_ds/assoc_container.hpp> //#define ordered_set tree<int, null_type, less<int >,rb_tree_tag, tree_order_statistics_node_update> //using namespace __gnu_pbds; #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #define ll long long #define ld long double #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define vii vector<int> #define vll vector<ll> #define clr(v, d) memset(v,d,sizeof(v)) using namespace std; const int N = 2e5 + 9; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll a,b,c; cin>>a>>b>>c; if(c&1){ if(a>b)cout<<">\n"; else if(b>a)cout<<"<\n"; else cout<<"=\n"; } else{ a=abs(a); b=abs(b); if(a>b)cout<<">\n"; else if(b>a)cout<<"<\n"; else cout<<"=\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define each(e, v) for(auto &e: v) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)x.size() using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; //const int MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; struct io_setup{ io_setup(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; struct Union_Find_Tree{ vector<int> data; const int n; Union_Find_Tree(int n) : n(n){ data.assign(n, -1); } int root(int x){ if(data[x] < 0) return x; return data[x] = root(data[x]); } int operator [] (int i) {return root(i);} bool unite(int x, int y){ x = root(x), y = root(y); if(x == y) return false; if(data[x] > data[y]) swap(x, y); data[x] += data[y], data[y] = x; return true; } int size(int x) {return -data[root(x)];} bool same(int x, int y) {return root(x) == root(y);} void clear() {fill(begin(data), end(data), -1);} }; int main(){ int H, W; cin >> H >> W; vector<string> S(H); rep(i, H) cin >> S[i]; vector<vector<int>> xs(H), ys(W); rep(i, H){ xs[i].eb(0); rep2(j, 1, W-2){ if(S[i][j] == '#') xs[i].eb(j); } xs[i].eb(W-1); } rep(j, W){ ys[j].eb(0); rep2(i, 1, H-2){ if(S[i][j] == '#') ys[j].eb(i); } ys[j].eb(H-1); } vector<vector<bool>> used(H, vector<bool>(W, false)); vector<bool> ux(H, false), uy(W, false); queue<pii> que; used[0][0] = true; que.emplace(0, 0); while(!que.empty()){ auto [x, y] = que.front(); que.pop(); ux[x] = true, uy[y] = true; if(y > 0){ auto it = lower_bound(all(xs[x]), y); --it; int ny = *it; if(!used[x][ny]){ used[x][ny] = true; que.emplace(x, ny); } } if(y < W-1){ auto it = upper_bound(all(xs[x]), y); int ny = *it; if(!used[x][ny]){ used[x][ny] = true; que.emplace(x, ny); } } if(x > 0){ auto it = lower_bound(all(ys[y]), x); --it; int nx = *it; if(!used[nx][y]){ used[nx][y] = true; que.emplace(nx, y); } } if(x < H-1){ auto it = upper_bound(all(ys[y]), x); int nx = *it; if(!used[nx][y]){ used[nx][y] = true; que.emplace(nx, y); } } } vector<int> X, Y; rep(i, H) if(!ux[i]) X.eb(i); rep(i, W) if(!uy[i]) Y.eb(i); int A = sz(X), B = sz(Y); if(A == 0 || B == 0) {cout << "0\n"; return 0;} Union_Find_Tree uf(A+B); rep(i, A){ rep(j, B){ if(S[X[i]][Y[j]] == '#') uf.unite(i, A+j); } } int a = 0, b = 0; rep(i, A){ bool flag = true; rep(j, i){ if(uf.same(i, j)) flag = false; } a += flag; } rep(i, B){ bool flag = true; rep(j, i){ if(uf.same(A+i, A+j)) flag = false; } b += flag; } cout << min(a, b) << '\n'; }
#include <stdio.h> #include <vector> std::pair<int,int> next[1010][1010]; std::pair<int,int> find(std::pair<int,int> P) { if(next[P.first][P.second]==P) return P; else return next[P.first][P.second] = find(next[P.first][P.second]); } char x[1010][1010]; int check[1010][1010]; int main() { int a,b; scanf("%d%d",&a,&b); for(int i=1;i<=a;i++) scanf("%s",x[i]+1); x[1][1] = '#'; x[1][b] = '#'; x[a][1] = '#'; x[a][b] = '#'; for(int i=1;i<=a;i++) for(int j=1;j<=b;j++) next[i][j] = std::make_pair(i,j); int count1 = 0, count2 = 0; for(int i=1;i<=a;i++) { int last = -1; for(int j=1;j<=b;j++) { if(x[i][j]=='#') { if(last!=-1) { std::pair<int,int> s = std::make_pair(i,last); std::pair<int,int> t = std::make_pair(i,j); s = find(s); t = find(t); if(s.first != t.first || s.second != t.second) next[s.first][s.second] = t; } last = j; } } if(last==-1) count1++; } for(int j=1;j<=b;j++) { int last = -1; for(int i=1;i<=a;i++) { if(x[i][j]=='#') { if(last!=-1) { std::pair<int,int> s = std::make_pair(last,j); std::pair<int,int> t = std::make_pair(i,j); s = find(s); t = find(t); if(s.first != t.first || s.second != t.second) next[s.first][s.second] = t; } last = i; } } if(last==-1) count2++; } int ans = 0; for(int i=1;i<=a;i++) { for(int j=1;j<=b;j++) { if(x[i][j]=='#') { std::pair<int,int> s = std::make_pair(i,j); s = find(s); if(s==std::make_pair(i,j) && check[i][j]==0) { //printf("%d %d??\n",i,j); ans++; check[i][j] = 1; } } } } int ans2 = count1<count2?count1:count2; if(ans2>0) printf("%d",ans+ans2-1); else printf("%d",ans-1); }
#line 1 "/workspaces/compro/lib/template.hpp" #line 1 "/workspaces/compro/lib/io/vector.hpp" #include <iostream> #include <vector> #ifndef IO_VECTOR #define IO_VECTOR template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { int size = v.size(); for (int i = 0; i < size; i++) { std::cout << v[i]; if (i != size - 1) std::cout << " "; } return out; } template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) { for (auto &el : v) { std::cin >> el; } return in; } #endif #line 4 "/workspaces/compro/lib/template.hpp" #include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define FOR(i, m, n) for (int i = m; i < n; i++) #define ALL(v) (v).begin(), (v).end() #define coutd(n) cout << fixed << setprecision(n) #define ll long long int #define vl vector<ll> #define vi vector<int> #define MM << " " << using namespace std; template <class T> void say(bool val, T yes, T no) { cout << (val ? yes : no) << "\n"; } void say(bool val, string yes = "Yes", string no = "No") { say<string>(val, yes, no); } 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; } // C++ 17に完全移行したら消す // 最大公約数を求める template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; } // 最小公倍数を求める template <class T> T lcm(T n, T m) { int g = gcd(n, m); return n * m / g; } // 重複を消す。計算量はO(NlogN) template <class T> void unique(std::vector<T> &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); } #line 2 "main.cpp" const std::string FIRST = "First"; const std::string SECOND = "Second"; void solve(int n, const std::vector<ll> &a) { if (a.size() % 2 == 0) { map<int, int> mp; REP(i, n) { mp[a[i]]++; } for (const auto [key, val] : mp) { if (val % 2 != 0) { cout << FIRST << endl; return; } } cout << SECOND << endl; } else { cout << SECOND << endl; } } // generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator) int main() { int t; cin >> t; REP(i, t) { int n; cin >> n; vl a(n); cin >> a; solve(n, a); } return 0; }
// Artur Kraska, II UWr #include <bits/stdc++.h> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #define FOR(i, a, b) for(int i=a; i<=b; i++) #define FORD(i, a, b) for(int i=a; i>=b; i--) #define MP make_pair #define PB push_back #define ff first #define ss second #define deb(X) X; #define M 1000000007 #define INF 1000000007LL using namespace std; long long a; int solve() { cin >> a; if(a%4 == 0) printf("Even\n"); else if(a%2 == 0) printf("Same\n"); else printf("Odd\n"); return 0; } int main() { testy() solve(); return 0; }
/****************************************************************************************************************************/ /************************************* My Template Start *************************************/ /****************************************************************************************************************************/ #ifndef LOCAL #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #endif #include<bits/stdc++.h> #define all(x) (x).begin(),(x).end() #define sortall(x) sort(all(x)) #define reverseall(x) reverse(all(x)) #define siz(x) (int)(x).size() #define rep(i, n) for(int i=0;i<(n);i++) #define repi(i, l, r) for(int i=l;i<=(r);i++) #define V1(t) vector<t> #define V2(t) vector<vector<t>>> #define V3(t) vector<vector<vector<t>>> #define VV1(t, a) vector<t>(a) #define VV2(t, a, b) vector<vector<t>>(a, VV1(t, b)) #define VV3(t, a, b, c) vector<vector<vector<t>>>(a, VV2(t, b, c)) #define pb push_back #define eb emplace_back #define fi first #define se second using namespace std; using ull = unsigned long long; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<long long, long long>; using pil = pair<int, long long>; using pli = pair<long long, int>; using vi = vector<int>; using vd = vector<double>; using vl = vector<ll>; using vii = vector<pii>; using vll = vector<pll>; #ifdef LOCAL template<typename Type> void _DEBUG(Type arg) { cerr << arg; } template<typename Type, typename... Types> void _DEBUG(Type arg, Types ...args) { cerr << arg << ", "; _DEBUG(args...); } #define debug(...) cerr << "[ " << #__VA_ARGS__ << " ] : ", _DEBUG(__VA_ARGS__) void _DEBUG_START() { cerr << "\n<===== DEBUG START ======>\n"; } void _DEBUG_END() { cerr << "\n<====== DEBUG END =======>\n"; } #define debug2(...) _DEBUG_START(), debug(__VA_ARGS__), _DEBUG_END() #else #define debug(...) #define debug2(...) #endif constexpr char ln = '\n'; constexpr char sp = ' '; constexpr int dy[] = {-1,1,0,0,-1,-1,1,1}; constexpr int dx[] = {0,0,-1,1,-1,1,-1,1}; const ld PI = acos((ld)-1); template<int fp> struct fastio { fastio() { ios::sync_with_stdio(false); cin.tie(0); if(fp)cout<<fixed<<setprecision(fp); } }; template<typename Type> void input(Type &arg){cin >> arg;} template<typename Type, typename... Types> void input(Type &arg, Types &...args){cin>>arg;input(args...);} template<typename Type> void output(Type arg){cout<<arg;} template<typename Type, typename... Types> void output(Type arg, Types ...args){cout<<arg;output(args...);} template<typename Type> inline ostream& operator<<(ostream &out, const vector<Type> &v) { for(auto &i: v) out<<i<<' '; return out; } template<typename Type> inline istream& operator>>(istream &in, vector<Type> &v) { for(auto &i: v) in>>i; return in; } template<typename T> T power(T a, T b, T mod=numeric_limits<T>::max()){ if(b == 0)return 1; if(b % 2)return a * power(a, b-1, mod) % mod; return power(a * a % mod, b >> 1, mod); } /****************************************************************************************************************************/ /************************************* My Template End *************************************/ /****************************************************************************************************************************/ bool slope(pii a, pii b){ int x = abs(a.fi - b.fi); int y = abs(a.se - b.se); // -1 ≤ y / x ≤ 1 return -x <= y && y <= x; } int main(){ fastio<0>(); int n; cin >> n; vii v(n); rep(i, n){ int x, y; cin >> x >> y; v[i] = pii(x, y); } int cnt = 0; rep(i, n){ repi(j, i + 1, n - 1){ cnt += slope(v[i], v[j]); } } cout << cnt << ln; return 0; }
#include<bits/stdc++.h> //Everything is theoretically impossible untill it is done #define ll long long #define vecto vector<int> #define set set<int> #define stack stack<int> #define io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define pp pair<int,int> #define mk make_pair #define mod 10000007 #define MAX INT_MAX #define MIN INT_MIN #define w(x) int x; cin>>x; while(x--) #define set_built(x) __builtin_popcountll(x) #define zero_built(x) __bultin_ctzll(x) using namespace std; int sum(int n){ int p=0; while(n!=0){ int k; k=n%10; p=p+k; n=n/10; } return p; } // Asli function void solve(){ vector<pair<double,double>>v; int t; cin>>t; while(t--){ double x,y; cin>>x>>y; v.push_back(mk(x,y)); } int count=0; for(int i=0; i<v.size()-1; i++){ for(int j=i+1; j<v.size(); j++){ double t; t=(v[j].second-v[i].second)/(v[j].first-v[i].first); if(t>=-1&&t<=1) count++; } } cout<<count<<endl; } //naam ka function int main(){ solve(); return 0; }
#include "bits/stdc++.h" #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; using namespace std; #define ub upper_bound #define lb lower_bound #define isrt insert #define clr clear #define rsz resize #define ff first #define ss second #define int long long int #define pb push_back #define pf push_front #define mkp make_pair #define mkt make_tuple #define pii pair<int,int> #define tii tuple<int,int,int> #define vi vector<int> #define mii map<int,int> #define pqb priority_queue<int> #define pqs priority_queue<int,vi,greater<int>> #define setbits(x) __builtin_popcountll(x) #define zrobits(x) __builtin_ctzll(x) #define mod 1000000007 #define INF 1e9 #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define w(x) int x; cin>>x; while(x--) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define endl "\n" using cd = complex<double>; const double PI = acos(-1); typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; int maxm(int a, int b) { return (a >= b ? a : b); } int minm(int a, int b) { return (a <= b ? a : b); } int power(int x, int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } #define FMAX 2000005 vi fact(FMAX, 1); vi fact_inv(FMAX, 1); vi lp(FMAX); vi pr; int nCrmodp(int n, int r, int p) { if (r > n) return 0; int pro = (fact[n] * fact_inv[r]) % p; pro = (pro * fact_inv[n - r]) % p; return pro; } void precompute() { int x = 1; for (int i = 1; i < FMAX; ++i) { x *= i; x %= mod; fact[i] = x; fact_inv[i] = power(x, mod - 2, mod); } } void spf_calc() { for (int i = 2; i < FMAX; ++i) { if (lp[i] == 0) { lp[i] = i; pr.push_back (i); } for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] < FMAX; ++j) lp[i * pr[j]] = pr[j]; } } int next_smaller(vector<int>v, int val) { int l = 0, r = v.size() - 1, ans = -1; while (l <= r) { int mid = (l + r) / 2; if (v[mid] <= val) { ans = mid; l = mid + 1; } else r = mid - 1; } return ans; } void c_p_c() { precompute(); //spf_calc(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } signed main() { c_p_c(); int n, k, m; cin >> n >> m >> k; if (n > (m + k)) cout << "0\n"; else { int tot = nCrmodp(n + m, n, mod); tot -= nCrmodp(n + m, m + k + 1, mod); tot = (tot + mod) % mod; cout << tot << endl; } return 0; }
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define REP(a,b) for(int a=0;a<(b);++a) #define REP1(i,n) for(int i=1;i<=(n);++i) #define debug(x) cerr<<#x<<": "<<x<<'\n' #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define YES() printf("YES\n") #define NO() printf("NO\n") #define isYES(x) printf("%s\n",(x) ? "YES" : "NO") #define Yes() printf("Yes\n") #define No() printf("No\n") #define isYes(x) printf("%s\n",(x) ? "Yes" : "No") #define isPossible(x) printf("%s\n",(x) ? "Possible" : "Impossible") #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair // #define INF (1<<9+9) const int INF = 0x3fffffff; // const long long INF = 1LL<<50; #define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl #define vi vector<int> #define vl vector<ll> #define vii vector< vector<int> > #define vll vector< vector<ll> > #define vs vector<string> #define pii pair<int,int> #define pis pair<int,string> #define psi pair<string,int> #define pll pair<ll,ll> #define pie 3.14159265358979323846 using namespace std; typedef pair<int, int> P; typedef pair<ll, ll> LP; typedef pair<int, P> PP; typedef pair<ll, LP> LPP; template<class T=int> T in(){T x;cin>>x;return (x);} template<class T> void print(T& x){cout<<x<<'\n';} const int MOD =(int)998244353; // const int mod =(int)998244353; const int mod =(int)1e9+7; const int MAX =510000; ll fac[MAX],finv[MAX],inv[MAX]; void COMint(){ fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; for(int i=2;i<MAX;i++){ fac[i]=fac[i-1]*i%MOD; inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD; finv[i]=finv[i-1]*inv[i]%MOD; } } ll COM(int n,int k){ if(n<k) return 0; if(n<0||k<0)return 0; return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD; } ll gcd(ll a,ll b){ if(a<0)a=-a; if(b<0)b=-b; if(b==0)return a; if(a>b){ swap(a,b); } return gcd(a,b%a); } ll lcm(ll a,ll b){ if(a<0)a=-a; if(b<0)b=-b; ll g;g=gcd(a,b); return b/g*a; } bool compare_by_b(pair<int, int> a, pair<int, int> b) { if(a.second != b.second){ return a.second < b.second; }else{ return a.first < b.first; } } bool compare_by_a(pair<int, int> a, pair<int, int> b) { if(a.first != b.first){ return a.first < b.first; }else{ return a.second < b.second; } } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } //int N, M, K, T, H, W, L, R; // long long int N, M, K, T, H, W, L, R ll RS(ll N,ll P){ if(P==0){ return 1; }else{ if(P%2==0){ ll t=RS(N,P/2); return t*t; }else{ return N*RS(N,P-1); } } } int main() { ios::sync_with_stdio(false); int N; cin>>N; double x0,y0,xn2,yn2; cin>>x0>>y0>>xn2>>yn2; double ox,oy; ox=(x0+xn2)/2; oy=(y0+yn2)/2; debug(ox);debug(oy); double c,s; c=cos(2*pie/N); s=sin(2*pie/N); double ansx,ansy; ansx=(x0-ox)*c-(y0-oy)*s; ansy=(x0-ox)*s+(y0-oy)*c; ansx+=ox; ansy+=oy; cout<<setprecision(25)<<ansx<<" "<<ansy<<endl; return 0; }
#include <iostream> #include <vector> #include <map> #include <tuple> #define fi first #define se second #define rep(i,n) for(int i = 0; i < (n); ++i) #define rrep(i,n) for(int i = 1; i <= (n); ++i) #define drep(i,n) for(int i = (n)-1; i >= 0; --i) #define srep(i,s,t) for (int i = s; i < t; ++i) #define rng(a) a.begin(),a.end() #define rrng(a) a.rbegin(),a.rend() #define isin(x,l,r) ((l) <= (x) && (x) < (r)) #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() #define pcnt __builtin_popcountll #define uni(x) x.erase(unique(rng(x)),x.end()) #define snuke srand((unsigned)clock()+(unsigned)time(NULL)); #define show(x) cerr<<#x<<" = "<<x<<endl; #define PQ(T) priority_queue<T,v(T),greater<T> > #define bn(x) ((1<<x)-1) #define dup(x,y) (((x)+(y)-1)/(y)) #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) using namespace std; typedef long long int ll; typedef unsigned uint; typedef unsigned long long ull; typedef pair<int,int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; int getInt(){int x;scanf("%d",&x);return x;} template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;} template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;} template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;} template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;} template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;} template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;} const double eps = 1e-10; int main() { ll n, x, v, p; scanf("%lld", &n); scanf("%lld", &x); ll sum = 0; int result = -1; for(int i=0; i<n; i++) { scanf("%lld", &v); scanf("%lld", &p); sum += v * p; if(float(sum) / float(100) > x) { result = i+1; break; } } cout << result << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define FastIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL) typedef long long ll; int main(){ FastIO; ll n,x; cin>>n; int c=0; string s,s1,sf; for(ll i=1;i<=1000000;i++){ s=to_string(i); s1=to_string(i); sf=s+s1; stringstream str(sf); str>>x; if(x<=n){ c++; } } std::cout << c << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pi; //bool vis[1000]; int x[]={1,-1,0,0}; int y[]={0,0,1,-1}; //int f=0; //int t[500000]; //int ll[500000]; //"''; int main(){ int r,c,n,m; cin>>r>>c>>n>>m; int dp[r][c][4]; memset(dp,0,sizeof(dp)); vector<vector<int> > v(r,vector<int>(c,0)); queue<vector<int> > q; int ans=0; for(int i=0;i<n;i++){ int a,b; cin>>a>>b; a--; b--; v[a][b]=1; for(int j=0;j<4;j++){ q.push({a,b,j}); dp[a][b][j]=1; } } for(int i=0;i<m;i++){ int a,b; cin>>a>>b; a--; b--; v[a][b]=-1; } while(!q.empty()){ vector<int> u=q.front(); q.pop(); int na=u[0]+x[u[2]]; int nb=u[1]+y[u[2]]; int nd=u[2]; if(na<r&&na>=0&&nb<c&&nb>=0&&dp[na][nb][nd]==0&&v[na][nb]!=-1){ dp[na][nb][nd]=1; q.push({na,nb,nd}); } } for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ int f=0; for(int k=0;k<4;k++){ f=max(f,dp[i][j][k]); } ans+=f; } } cout<<ans<<endl; }
#include<bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define fo(i,n) for(int i=0;i<n;i++) #define Fo(i,k,n) for(int i = k ;i<=n;i++) #define FOR(i,n,k) for(int i = n;i>=k;i--) #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 mems(a,x) memset(a,x,sizeof(a)) #define sortall(x) sort(all(x)) #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++) #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); } const int mod = 1000000007; const int N = 3e5, M = N; //vi g[N]; //int a[N]; void solve() { int h, w, n, m, i, j; cin >> h >> w >> n >> m; int a[h][w] = {0}; int dp[h][w] = {0}; clr(a); clr(dp); fo(i, n) { int x, y; cin >> x >> y; x--; y--; a[x][y] = 1; } fo(i, m) { int x, y; cin >> x >> y; x--; y--; a[x][y] = -1; } fo(i, h) { fo(j, w) { if (j != 0 && (a[i][j - 1] == 1 || a[i][j - 1] == 2) && a[i][j] == 0) { a[i][j] = 2;} } FOR(j, w - 2, 0) { if ((a[i][j + 1] == 1 || a[i][j + 1] == 2) && a[i][j] == 0) {a[i][j] = 2;} } } /* fo(i, h) { fo(j, w) cout << a[i][j] << " "; cout << endl; }*/ fo(i, w) { fo(j, h) { if (j != 0 && (a[j - 1][i] == 1 || a[j - 1][i] == 3) && (a[j][i] == 0 || a[j][i] == 2)) { //cout << j << " " << i << endl; a[j][i] = 3; } } FOR(j, h - 2, 0) { if ((a[j + 1][i] == 1 || a[j + 1][i] == 3) && (a[j][i] == 0 || a[j][i] == 2)) { //cout << j << " " << i << endl; a[j][i] = 3; } } } int ans = 0; fo(i, h) { fo(j, w) { //cout << a[i][j] << " "; ans += (a[i][j] == 1 || a[i][j] == 2 || a[i][j] == 3); } //cout << endl; } cout << ans << 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; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; #define rep(i,n) for(int i=0;i<(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define rep1(i,n) for(int i=1;i<=(n);i++) #define rrep1(i,n) for(int i=(n);i>0;i--) #define fore(i_in,a) for (auto& i_in: a) #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define cnts(x,c) count(all(x),c) #define fio() cin.tie(nullptr);ios::sync_with_stdio(false); #define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl template<class T> bool chmax(T &a, const T &b) {if (a<b) {a = b; return 1;} return 0;} template<class T> bool chmin(T &a, const T &b) {if (a>b) {a = b; return 1;} return 0;} template<class T> void print(const T &t) {cout<<t<<"\n";} template<class T> void PRINT(const T &t) {rep(i,sz(t)) cout<<t[i]<<" \n"[i==sz(t)-1];} const ll INF = 1LL << 62; const int iINF = 1 << 30; using Graph = vector<vector<int>>; int n,m; int BFS(const Graph &G, int s) { int n = sz(G); vector<int> dist(n,-1); queue<int> que; dist[s]=0; que.push(s); while(!que.empty()) { int v=que.front(); que.pop(); fore(nv,G[v]) { if (dist[nv]!=-1) continue; dist[nv]=dist[v]+1; que.push(nv); } } int cnt=0; fore(x,dist) if(x!=-1) cnt++; return cnt; } int main() { fio(); cin>>n>>m; Graph G(n); rep(i,m) { int a,b; cin>>a>>b;a--;b--; G[a].push_back(b); } int ans=0; rep(i,n) ans+=BFS(G,i); print(ans); }
#include<bits/stdc++.h> using namespace std; #define mp make_pair #define fi first #define se second const int maxn=100000; typedef pair<int,int> P; vector<P>g[maxn+5]; int vis[maxn+5]; int pr[maxn+5]; int col[maxn+5]; void dfs(int x,int pre,int c) { vis[x]=1; pr[c]=1; if(col[pre]!=c) col[x]=c; for(int i=0; i<g[x].size(); i++) { P tmp=g[x][i]; if(vis[tmp.fi]) continue; dfs(tmp.fi,x,tmp.se); } } int main() { int n,m; scanf("%d%d",&n,&m); for(int i=0; i<m; i++) { int u,v,c; scanf("%d%d%d",&u,&v,&c); g[u].push_back(mp(v,c)); g[v].push_back(mp(u,c)); } dfs(1,0,0); int tmp; for(int i=1;i<=n;i++) { if(!pr[i]) { tmp=i; break; } } for(int i=1;i<=n;i++) { if(!col[i]) printf("%d\n",tmp); else printf("%d\n",col[i]); } }
#include <bits/stdc++.h> using namespace std; // a + (a+1) + ... + b long sum(const long &a, const long &b) { return (a + b) * (b - a + 1) / 2; } int main() { int T; cin >> T; while (T--) { long L, R; cin >> L >> R; const long minC = L; const long maxC = R - L; if (minC > maxC) { cout << 0 << endl; } else { cout << sum(R - maxC - L + 1, R - minC - L + 1) << endl; } } return 0; }
//コンパイラ最適化 #pragma GCC optimize("Ofast") //インクルードなど #include<bits/stdc++.h> using namespace std; //エイリアス using ll = long long; using ld = long double; using Graph = vector<vector<ll>>; //イテレーション #define REP(i,n) for(ll i=0;i<ll(n);i++) #define REPD(i,n) for(ll i=n-1;i>=0;i--) #define FOR(i,a,b) for(ll i=a;i<=ll(b);i++) #define FORD(i,a,b) for(ll i=a;i>=ll(b);i--) #define FORA(i,I) for(const auto& i:I) //x:コンテナ #define ALL(x) x.begin(),x.end() #define SIZE(x) ll(x.size()) //定数 #define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf #define MOD 1000000007 //10^9+7:合同式の法 問題による #define MAXR 100000 //10^5:配列の最大のrange #define DDD fixed << setprecision(20) //略記 //#define F first //#define S second //グリッドの各方向に対する差分配列 const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; //myceil : a/b 以上の最小整数(負の数対応) //myfloor: a/b 以下の最大整数(負の数対応) ll myceil(ll a,ll b){if(a%b==0)return a/b;ll cnt=0;if(a<0){a=-a;cnt++;}if(b<0){b=-b;cnt++;}if(cnt%2==0)return(a+(b-1))/b;return-(a/b);} ll myfloor(ll a,ll b){if(a%b==0)return a/b;ll cnt=0;if(a<0){a=-a;cnt++;}if(b<0){b=-b;cnt++;}if(cnt%2==0)return a/b;return-(a+(b-1))/b;} // myupper: nで割り切れる a以上の最小整数(n: 負の数未対応) // mylower: nで割り切れる a以下の最大整数(n: 負の数未対応) ll myupper(ll a, ll n){if(a%n == 0) return a;if(a > 0) return a+n-a%n;if(a < 0) return a-a%n;return a;} ll mylower(ll a, ll n){if(a%n == 0) return a;if(a < 0) return a-n-a%n;if(a > 0) return a-a%n;return a;} /* #include<boost/multiprecision/cpp_dec_float.hpp> // 精度を落とすキャストは明示 : static_cast<型>(変数) using namespace boost::multiprecision; using f50 = cpp_dec_float_50; using f100 = cpp_dec_float_100; */ signed main(){ ll t; cin >> t; REP(i, t){ ll l, r; cin >> l >> r; if(l <= r-l && r-l <= r) cout << (r-2*l+1)*(r-2*l+2)/2 << endl; else cout << 0 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define ff(i,n) for(i=0;i<n;++i) #define fff(i,a,b) for(i=a;i<b;++i) #define pb push_back #define mp make_pair #define N 1000050 ll lpf[N], mobius[N]; ll fre[N]; // Function to calculate least // prime factor of each number void least_prime_factor() { for (ll i = 2; i < N; i++) // If it is a prime number if (!lpf[i]) for (ll j = i; j < N; j += i) // For all multiples which are not // visited yet. if (!lpf[j]) lpf[j] = i; } // Function to find the value of Mobius function // for all the numbers from 1 to n void Mobius() { for (ll i = 1; i < N; i++) { // If number is one if (i == 1) mobius[i] = 1; else { // If number has a squared prime factor if (lpf[i / lpf[i]] == lpf[i]) mobius[i] = 0; // Multiply -1 with the previous number else mobius[i] = -1 * mobius[i / lpf[i]]; } } } // Function to find the number of pairs // such that gcd equals to 1 ll gcd_pairs(vector<ll> a, ll n) { // To store maximum number ll maxi = 0; // Find frequency and maximum number for (ll i = 0; i < n; i++) { fre[a[i]]++; maxi = max(a[i], maxi); } least_prime_factor(); Mobius(); // To store number of pairs with gcd equals to 1 ll ans = 0; // Traverse through the all possible elements for (ll i = 1; i <= maxi; i++) { if (!mobius[i]) continue; ll temp = 0; for (ll j = i; j <= maxi; j += i) temp += fre[j]; ans += temp * (temp - 1) / 2 * mobius[i]; } // Return the number of pairs return ans; } // Driver code vector<ll> arr; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll l,r; cin>>l>>r; if(l==1) l++; if(r==1) r++; ll n = r-l+1,i; for(i=l;i<=r;i++){ arr.pb(i); } ll div=0,gcd1 = gcd_pairs(arr,n); ll j; for(i=l;i<=r;i++){ ll f = i,mult=1; while((f*mult)<=r){ if(mult>1) div++; mult++; } } n = r-l+1; ll ans = (n*(n-1))/2 - gcd1-div; cout<<(2*ans); return 0; }
//GIVE ME AC!!!!!!!!!!!!!!!!! #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define ll int #define ld long double #define floatset() fixed<<setprecision(15) #define all(n) n.begin(),n.end() #define rall(n) n.rbegin(),n.rend() #define rep(i, s, n) for (ll i = s; i < (ll)(n); i++) #define pb push_back #define eb emplace_back #define INT(...) int __VA_ARGS__;scan(__VA_ARGS__) #define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__) #define STR(...) string __VA_ARGS__;scan(__VA_ARGS__) #define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__) #define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__) #define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__) using namespace std; using vl=vector<ll>; using vi=vector<int>; using vs=vector<string>; using vc=vector<char>; using vvl=vector<vl>; using P=pair<ll,ll>; using vvc=vector<vc>; using vd=vector<double>; using vp=vector<P>; using vb=vector<bool>; using P=pair<ll,ll>; const int dx[8]={1,0,-1,0,1,-1,-1,1}; const int dy[8]={0,1,0,-1,1,1,-1,-1}; const ll MOD=1000000007; const ll mod=998244353; const double pi=acos(-1); template<typename T1,typename T2 > ostream &operator<<(ostream&os,const pair<T1,T2>&p) { os<<p.first<<" "<<p.second; return os; } template<typename T1,typename T2> istream &operator>>(istream&is,pair<T1,T2>&p) { is>>p.first>>p.second; return is; } template<typename T> ostream &operator<<(ostream&os,const vector<T>&v) { for(int i=0;i<(int)v.size();i++) { os<<v[i]<<(i+1!=v.size()?" ":""); } return os; } template<typename T> istream &operator>>(istream&is,vector<T>&v) { for(T &in:v)is>>in; return is; } void scan(){} template<class Head,class... Tail> void scan(Head&head,Tail&... tail) { cin>>head; scan(tail...); } template<class T> void print(const T &t) { cout << t << '\n'; } template<class Head, class... Tail> void print(const Head &head, const Tail &... tail) { cout << head << ' '; print(tail...); } template<class... T> void fin(const T &... a) { print(a...); exit(0); } template<typename T1,typename T2> inline bool chmax(T1&a,T2 b){return a<b&&(a=b,true);} template<typename T1,typename T2> inline bool chmin(T1&a,T2 b){return a>b&&(a=b,true);} vector<ll>prime_factorize(ll N) { vector<ll>res; for (ll a = 2; a * a <= N; ++a) { if (N % a != 0) continue; while (N % a == 0) { N /= a; } res.push_back(a); if(N==1)break; } if (N != 1) res.push_back(N); return res; } int main(){ LL(l,r); long long ans=0; rep(i,max(2,l),r+1){ vl p=prime_factorize(i); ll cnt=0; ll s=p.size(); rep(bit,1,(1<<s)){ ll now=1; rep(j,0,p.size()){ if((bit>>j)&1){ now*=p[j]; } } ll res=(r/now)-((i-1)/now); if(__builtin_popcount(bit)%2==1){ cnt+=res; } else{ cnt-=res; } } ans+=cnt; } rep(i,max(2,l),r+1){ ans-=r/i; } fin(ans*2); }
// #include <bits/stdc++.h> // #include <atcoder/all> #include <iostream> #include <vector> #include <algorithm> #include <stdio.h> #include <math.h> #include <string> #include <cctype> #include <map> #include <queue> #include <limits> #include <iomanip> #include <sstream> #include <cstdlib> #include <locale> using namespace std; // using namespace atcoder; const int mod = 1000000007; #define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0])) #define rep(i,n) for (int i = 0; i < (n); i++) #define ll long long #define P pair<int, int> // debug methods // usage: debug(x,y); #define CHOOSE(a) CHOOSE2 a #define CHOOSE2(a0,a1,a2,a3,a4,x,...) x #define debug_1(x1) cout<<#x1<<": "<<x1<<endl #define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl #define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl #define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl #define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl #ifdef _DEBUG #define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__) #else #define debug(...) #endif const double PI = acos(-1); int main() { int n; cin >> n; int total = pow(2, n); vector<ll> a(total); vector<ll> save; for (int i = 0; i < total; i++) { cin >> a[i]; } copy(a.begin(), a.end(), back_inserter(save)); int result = 0; while (true) { vector<int> removeIndexs; for (int i = 0; i < a.size() - 1; i+=2) { if (a[i] - a[i+1] > 0) { removeIndexs.push_back(i+1); } else { removeIndexs.push_back(i); } } if (a.size() == 2) { result = a[removeIndexs.back()]; break; } for (int i = removeIndexs.size() - 1; i != static_cast<decltype(i)>(-1); i--) { a.erase(a.begin() + removeIndexs[i]); } } for (int i = 0; i < save.size() ; i++) { if (save[i] == result) { cout << i+1 << endl; break; } } return 0; }
#include <iostream> #include <algorithm> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208; int main() { int n; cin >> n; vector<int> v(1 << (n+1)); int p = (1 << n); vector<int> a(p); map<int, int> aa; for (int i = 0; i < p; ++i) { scanf("%d", &v[i+p]); aa[v[i+p]] = i; } for (int i = p-1; i > 0; --i) { v[i] = max(v[i*2],v[i*2+1]); } cout << aa[min(v[2], v[3])]+1 << "\n"; return 0; }
#include<bits/stdc++.h> /** ******************* Author:Bisnu sarkar **************************** **/ /* #pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") */ //PBDS /* #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; */ using namespace std; //PBDS_SET //Index of Value - .order_of_key() //Value at Index - .find_by_order() /* typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds_set; typedef tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> pbds_multiset; */ #define fRead(x) freopen(x,"r",stdin) #define fWrite(x) freopen (x,"w",stdout) #define ull unsigned long long #define ll long long #define pii pair<int,int> #define sit set<int> :: iterator #define vrit vector<int> :: reverse iterator #define ff first #define ss second #define endl '\n'; #define sz(s) (int)s.size() #define all(s) s.begin(),s.end() #define IO ios_base::sync_with_stdio(false),cin.tie(NULL); #define what_is(x) cerr << #x << " is " << x << endl; ///*....Debugger....*/// #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {cout << endl;} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); } ///*....Input....*/// template <typename T> inline void Int(T &n) { n = 0; int f = 1; register int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0'; n = n * f; } template <typename T, typename TT> inline void Int(T &n, TT &m) { Int(n); Int(m); } template <typename T, typename TT, typename TTT> inline void Int(T &n, TT &m, TTT &l) { Int(n, m); Int(l);} double ind(){double x; scanf("%lf",&x);return x;} ///*....Bitmask....*/// int set_1(int n,int pos){return n = (n | (1<<pos));} int reset_0(int n,int pos){return n= n & ~(1<<pos);} bool check_bit(int n,int pos){return n = n & (1<<pos);} int dx[8]= {1,0,-1,0,-1,-1,1,1}; int dy[8]= {0,1,0,-1,-1,1,-1,1}; ///*....Constraints....*/// const int N = (int) 1e6 + 5; const int M = (int) 1e9 + 7; const double pi=2* acos(0.0); const double eps=1e-9; int a[N],par[N],cnt[N]; bool vis[N]; int findroot(int u){ if(u==par[u])return u; return par[u]=findroot(par[u]); } void merge(int u,int v){ u=findroot(u); v=findroot(v); if(u!=v){ par[u]=v; cnt[v]+=cnt[u]; } } int main() { int t=1,te=0; //Int(t); while(t--){ int n; Int(n); for(int i=1;i<=n;++i){ Int(a[i]); par[a[i]]=a[i]; cnt[a[i]]=1; } for(int i=1,j=n;i<j;++i,--j){ if(a[i]!=a[j]){ merge(a[i],a[j]); } } int res=0; for(int i=1,j=n;i<j;++i,--j){ if(a[i]!=a[j]){ int u=findroot(a[i]); if(!vis[u]){ res+=cnt[u]-1; vis[u]=true; } } } printf("%d\n", res); //printf("Case %d: ",++te); } //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define stp(var, init, end) for (auto var = init; var < end; ++var) #define stpe(var, init, end) for (auto var = init; var <= end; ++var) #define ll long long int main(void) { int n; ll input; cin >> n; vector<ll> a(n + 1); a[0] = 0; stp(i, 1, n + 1) { cin >> input; if (i % 2 == 0) a[i] = input; else a[i] = -input; } //累積和 stp(i, 1, n + 1) { a[i] += a[i - 1]; } sort(a.begin(), a.end()); //累積和が等しい=和が0 ll count = 1, ans = 0; stp(i, 1, n + 1) { if (a[i] == a[i - 1]) ++count; else { if (count > 1) ans += count * (count - 1) / 2; count = 1; } } if (count > 1) ans += count * (count - 1) / 2; cout << ans; return 0; }
// God put a smile upon your face <3 #include <bits/stdc++.h> #define slld(longvalue) scanf("%lld", &longvalue) #define ll long long #define ull unsigned long long #define pll pair < long long, long long > #define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0) #define pb push_back #define bug printf("BUG\n") #define mxlld LLONG_MAX #define mnlld -LLONG_MAX #define mxd 2e8 #define mnd -2e8 #define pi 3.14159265359 using namespace std; bool check(ll n, ll pos) { return n & (1LL << pos); } ll Set(ll n, ll pos) { return n = n | (1LL << pos); } struct CRT { ll n; vector < ll > m, r; void init() { m.clear(); r.clear(); } pll ext_euclid(ll a, ll b) { pll ret = {0,0}; ll x1 = 0, y1 = 1; ll x2 = 1, y2 = 0; ll r3, r0; for(ll r1 = b, r2 = a; r1 ; r2 = r1, r1 = r3) { r0 = r2 / r1; r3 = r2 % r1; ll x3 = (x2 - (x1 * r0)); ll y3 = (y2 - (y1 * r0)); x2 = x1, y2 = y1; x1 = x3, y1 = y3; } return {x2,y2}; } ll lcm(ll a, ll b) { ll ret = a / __gcd(a,b); return ret * b; } ll mul(__int128 a, __int128 b, __int128 m) { __int128 ret = ((a % m) * (b % m)) % m; return (long long) ret; } pll start() { ll r1 = r[0], m1 = m[0]; ll n = m.size(); for(ll i = 1; i < n; i++) { ll r2 = r[i], m2 = m[i]; ll g = __gcd(m1,m2); if(r2 % g != r1 % g) return {-1,-1}; pll ans = ext_euclid(m1/g,m2/g); ll md = lcm(m1,m2); ll x = (mul(mul(r2,m1/g,md),ans.first,md) + mul(mul(r1,m2/g,md),ans.second,md)) % md; r1 = x, m1 = md; if(r1 < 0) r1 += md; } return {r1,m1}; } }; ll solve() { ll p, q, x, y; cin >> x >> y >> p >> q; CRT crt; ll ans = mxlld; for(ll i = x; i < x + y; i++) { for(ll j = p; j < (p + q); j++) { crt.init(); crt.m.push_back(2 * (x + y)); crt.m.push_back(p + q); crt.r.push_back(i); crt.r.push_back(j); pll ret = crt.start(); if(ret.second == -1) continue; ans = min(ans, ret.first); } } if(ans == mxlld) return -1; return ans; } int main() { ll i, j, k, l, m, n, o, r, q; ll testcase; ll input, flag, tag, ans; // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); slld(testcase); for(ll cs = 1; cs <= testcase; cs++) { ans = solve(); if(ans == -1) cout << "infinity\n"; else cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; long long exgcd(long long a, long long b, long long &x, long long &y) { if (!b) { x = 1, y = 0; return a; } long long ans = exgcd(b, a % b, y, x); y -= a / b * x; return ans; } long long solve(long long a, long long b, long long c) { long long x, y, z; z = exgcd(a, b, x, y); if (c % z) return -1; //不成立 //return x; //不需要最小正整数的话直接返回x x *= c / z; b = abs(b / z); return (x % b + b) % b; } int main() { int T; cin >> T; while (T--) { long long x, y, p, q; cin >> x >> y >> p >> q; long long ans = INT64_MAX; for (long long yy = 0; yy < y; yy++) { for (long long qq = 0; qq < q; qq++) { long long d = solve(2 * x + 2 * y, p + q, p + qq - x - yy); if (d != -1) { ans = min(ans, d * (2 * x + 2 * y) + x + yy); } } } if (ans == INT64_MAX) cout << "infinity" << endl; else cout << ans << endl; } return 0; } /* 2(x+y) x<=( [k(p+q)+p) )%(2x+2y)<x+y [ k*(2x+2y)+x,+x+y ) [ k(p+q)+p , k*(p+q)+p+q ) s*(2x+2y)+x+yy==t*(p+q)+p+qq s*(2x+2y)-t*(p+q)=p+qq-x-yy; ax+by=c 1499999999500000000 1000000000999999999 */
//#define _GLIBCXX_DEBUG #include<bits/stdc++.h> using namespace std; #define endl '\n' #define lfs cout<<fixed<<setprecision(10) #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() #define spa << " " << #define fi first #define se second #define MP make_pair #define MT make_tuple #define PB push_back #define EB emplace_back #define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++) #define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--) using ll = long long; using ld = long double; const ll MOD1 = 1e9+7; const ll MOD9 = 998244353; const ll INF = 1e18; using P = pair<ll, ll>; template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;} ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});} void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;} void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;} void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;} template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;} template<typename T>void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout spa v[i][j];cout<<endl;}}; void debug(vector<string>&v,ll h,ll w){for(ll i=0;i<h;i++){for(ll j=0;j<w;j++)cout<<v[i][j];cout<<endl;}}; template<typename T>void debug(vector<T>&v,ll n){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout spa v[i];cout<<endl;}; template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1}; template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);} template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));} template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;} template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;} //mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); ll res=0,buf=0; bool judge = true; ll n,m;cin>>n>>m; vector<ll>w(n); rep(i,0,n)cin>>w[i]; ll mxw=*max_element(ALL(w)); vector<P>p(m); rep(i,0,m){ cin>>p[i].se>>p[i].fi; if(p[i].fi<mxw){ cout<<-1<<endl; return 0; } } //荷重、長さ //p.EB(INF,INF); p.EB(0,0); sort(ALL(p)); rep(i,0,(ll)p.size()-1){ chmax(p[i+1].se,p[i].se); } //debug(p,p.size()); vector<ll>v(n); iota(ALL(v),0); res=INF; do{ vector<ll>pos(n); rep(i,1,n){ ll nw=w[v[i]]; ll mx=0; rrep(j,0,i){ nw+=w[v[j]]; P q=*(--lower_bound(ALL(p),MP(nw,0LL))); chmax(mx,q.se-(pos[i-1]-pos[j])); } pos[i]=pos[i-1]+mx; } /*if(is_sorted(ALL(v))){ debug(v,3);debug(pos,3);cout<<endl; }*/ chmin(res,pos[n-1]); }while(next_permutation(ALL(v))); cout<<res<<endl; return 0; }
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int n, m, c[10], dl[10], vis[10], ans = 0x3f3f3f3f, lx[10], db[10]; struct judge { int l; int v; }; judge a[100010]; bool tmp(judge x, judge y) { if (x.l != y.l) return x.l < y.l; else return x.v > y.v; } int findl(int x) { int b, e, t; b = 1, e = m, t = b + e >> 1; while (b < e) { if (a[t].v < x) b = t; else e = t - 1; t = b + e +1>> 1; } if (a[b].v >= x) return 0; return a[b].l; } void fact() { memset(lx, 0, sizeof lx); for (int i = 1; i <= n; i++) { db[i] = db[i - 1] + c[dl[i]]; } for (int i = 2; i <= n; i++) for (int j = 1; j < i; j++) { int l = findl(db[i] - db[j - 1]); lx[i] = max(lx[i], lx[j] + l); } ans = min(ans, lx[n]); } void dfs(int i) { if (i == n + 1) { fact(); return; } for (int j = 1; j <= n; j++) { if (!vis[j]) { vis[j] = 1; dl[i] = j; dfs(i + 1); vis[j] = 0; } } } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> c[i]; for (int i = 1; i <= m; i++) cin >> a[i].l >> a[i].v; sort(a + 1, a + m + 1, tmp); for (int i = m - 1; i > 0; i--) a[i].v = min(a[i].v, a[i + 1].v); for (int i = 1; i <= n; i++) if (c[i] > a[1].v) { cout << -1; return 0; } dfs(1); cout << ans; }