code_file1
stringlengths
87
4k
code_file2
stringlengths
85
4k
#include<iostream> #include<cstdio> #include<cstring> typedef long long ll; typedef unsigned un; typedef std::pair<int,int> pii; typedef std::pair<ll,ll> pll; ll read() { ll f=1,x=0;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 f*x; } ll max(ll a,ll b){return a>b?a:b;} ll min(ll a,ll b){return a<b?a:b;} bool umin(int& a,int t){if(t<a)return a=t,1;return 0;} bool umin(ll& a,ll t){if(t<a)return a=t,1;return 0;} bool umax(int& a,int t){if(t>a)return a=t,1;return 0;} bool umax(ll& a,ll t){if(t>a)return a=t,1;return 0;} #define MAXN 511 bool a[MAXN][MAXN]; char s[MAXN]; int main() { int n=read(); for(int i=1;i<=n;++i) { scanf("%s",s+1); for(int j=1;j<=n;++j)a[i][j]=(s[j]=='1'); a[i][i]=1; } for(int k=1;k<=n;++k) for(int i=1;i<=n;++i) for(int j=1;j<=n;++j)a[i][j]|=(a[i][k]&a[k][j]); double ans=0; for(int u=1;u<=n;++u) { int c=0; for(int s=1;s<=n;++s)c+=a[s][u]; ans+=1.00/c; } printf("%.11lf\n",ans); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<n; i++) #define ll long long #define VV(t) vector<vector<t>> using namespace std; using Graph = VV(int); const int INF = 1<<29; int factorial(int n) { if (n<=1) return 1; else return n*factorial(n-1); } int main() { int N; cin >> N; Graph G(N, vector<int>(N, INF)); rep(i, N) { rep(j, N) { char c; cin >> c; if (c == '1') G[i][j] = 1; } G[i][i] = 0; } rep(k, N) { rep(i, N) { rep(j, N) { G[i][j] = min(G[i][j], G[i][k] + G[k][j]); } } } double v = 0; rep(i, N) { int nonInf = 0; rep(j, N) if (G[j][i] < INF/2) nonInf += 1; v += (double)1 / nonInf; } cout << fixed << setprecision(20) << v << endl; }
// // a.cpp // // // Created by Sagar Singh on 13/12/20. // #include <iostream> #include <fstream> #include <vector> #include <set> #include <map> #include <cstring> #include <string> #include <cmath> #include <cassert> #include <ctime> #include <algorithm> #include <numeric> #include <sstream> #include <list> #include <queue> #include <deque> #include <stack> #include <cstdlib> #include <cstdio> #include <iterator> #include <functional> #include <bitset> #include <unordered_map> #include <unordered_set> typedef unsigned long long ul; typedef long double ld; typedef long long ll; using namespace std; int check(int a,int b,int c,int d){ if( a+b == c+d || a-b == c-d || abs(a-c)+abs(b-d) <= 3 ){ return 1; } return 0; } int main() { ll a,b,c,d; cin >> a >> b >> c >> d; int ans = 2 + ( (a+b)%2 != (c+d)%2 ); for(int dx=-3;dx<=+3;++dx){ for(int dy=-3;dy<=+3;++dy){ int x = a + dx, y = b + dy; if( check(a,b,x,y) && check(x,y,c,d) ){ ans = 2; } } } if( check(a,b,c,d) ){ ans = 1; } if( a == c && b == d ){ ans = 0; } cout << ans << 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>; int main() { ll sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; double a = (double)(sy+gy) / (double)(gx-sx); double b = (double)gy - a*(double)gx; double x = -b / a; cout << fixed << setprecision(10); cout << x; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define YES cout<<"YES" #define NO cout<<"NO" #define Yes cout<<"Yes" #define No cout<<"No" #define yes cout<<"yes" #define no cout<<"no" #define FOR(S , E) for(ll i=S;i<=E;i++) #define ROF(E , S) for(ll i=E;i>=S;i--) // V.push_back() // V.pop_front()// V.top() //V.pop()//V.front() // V.back() // pop_back() //second // first // insert // continue // break // erase // St.push() // V.size() // S.length() // clear() // S.length() // reverse // greater // push bool IsPalind(string S) { int L =0 , R = S.length()-1; while(R > L) { if(S[L++]!=S[R--]) { return 0; } } return 1; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); string S; cin>>S; if(IsPalind(S)) { Yes; return 0; } for(int i=1;i<=15;i++) { string S2 = ""; for(int j=1;j<=i;j++)S2+='0'; for(char C : S)S2+=C; if(IsPalind(S2)) { Yes; return 0; } } No; return 0; }
#include<cstring> #include<iostream> #include<algorithm> #include<cmath> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int N=200010,mod=1e9+7; char s[100][10]; int main() { int n; cin >> n; long long ans = 1; for (int i = 1; i <= n;i++) { string s; cin >> s; if(s=="OR") { ans += 1LL << i; } } printf("%lld", ans); return 0; }
#define DEBUG 0 #include <bits/stdc++.h> using namespace std; #if DEBUG // basic debugging macros int __i__,__j__; #define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl #define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl #define printVar(n) cout<<#n<<": "<<n<<endl #define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl #define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;} #define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;} // advanced debugging class // debug 1,2,'A',"test"; class _Debug { public: template<typename T> _Debug& operator,(T val) { cout << val << endl; return *this; } }; #define debug _Debug(), #else #define printLine(l) #define printLine2(l,c) #define printVar(n) #define printArr(a,l) #define print2dArr(a,r,c) #define print2dArr2(a,r,c,l) #define debug #endif // define #define MAX_VAL 999999999 #define MAX_VAL_2 999999999999999999LL #define EPS 1e-6 #define mp make_pair #define pb push_back // typedef typedef unsigned int UI; typedef long long int LLI; typedef unsigned long long int ULLI; typedef unsigned short int US; typedef pair<int,int> pii; typedef pair<LLI,LLI> plli; typedef vector<int> vi; typedef vector<LLI> vlli; typedef vector<pii> vpii; typedef vector<plli> vplli; // ---------- END OF TEMPLATE ---------- int a[100000]; int main() { int i; int T,N; scanf("%d",&T); while (T--) { scanf("%d",&N); for (i = 0; i < N; i++) scanf("%d",&a[i]); sort(a,a+N); if (!(N & 1)) { for (i = 0; i < N; i += 2) { if (a[i] != a[i+1]) break; } if (i >= N) printf("Second\n"); else printf("First\n"); } else printf((N & 1) ? "Second\n":"First\n"); } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i, a, b) for(int i = a; i <= b; ++i) const int N = 300007; const int mod = 1e9 + 7; int n; int a[N]; int main() { int T; cin >> T; while(T--){ cin >> n; int key = 0; rep(i, 1, n)cin >> a[i], key ^= a[i]; if(n & 1){ puts("Second"); } else { map<int, int>mp; rep(i, 1, n){ if(mp.count(a[i]))mp[a[i]]++; else mp[a[i]] = 1; } int flag = 0; for(auto v : mp)if(v.second % 2 == 1)flag = 1; if(flag)puts("First"); else puts("Second"); } } }
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> using namespace std; #define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i) #define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i) #define fi first #define se second #define mp(a, b) make_pair(a, b) #define Endl putchar('\n') typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template<class T>inline T fab(T x){ return x<0? -x: x; } template<class T>inline T readin(T x){ x=0; int f=0; char c; while((c=getchar())<'0' || '9'<c) if(c=='-') f=1; for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48)); return f? -x: x; } const int maxn=1e5; ll n; int vis[maxn+5]; signed main(){ n=readin(1ll); ll ans=0; vis[1]=1; for(int i=2; 1ll*i*i<=n; ++i) if(!vis[i]){ vis[i]=1; for(ll cur=1ll*i*i; cur<=n; cur=1ll*cur*i){ if(cur<=maxn) vis[cur]=1; ++ans; } } printf("%lld\n", n-ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5; const ll mod = 998244353; /* I should look for * if WA, check the code from very top until bottom. * always write observations if problem is hard. * keep thought process clear. * special cases (n=1?) * stay organized. * DON'T GET STUCK ON ONE APPROACH */ int main() { ios_base :: sync_with_stdio(false); cin.tie(NULL); ll n; cin >> n; set <ll> s; for(ll i = 2;i <= sqrt(n);i++) { ll x = i; while((x * i) <= n) s.insert(x * i), x = x * i; } cout << n - s.size(); return 0; }
#include <bits/stdc++.h> #define rep3(i, s, n, a) for (int i = (s); i < (int)(n); i += a) #define rep2(i, s, n) rep3(i, s, n, 1) #define rep(i, n) rep2(i, 0, n) using namespace std; using ll = long long; using ull = unsigned long long; using P = pair<int, int>; using Pll = pair<ll, ll>; int main() { ll n, w; cin >> n>> w; vector<Pll> v; rep(i, n){ ll s, t, p; cin >> s >> t >> p; v.emplace_back(s, p); v.emplace_back(t, -p); } sort(v.begin(), v.end()); ll now = 0; string res = "Yes"; rep(i, 2*n){ now += v[i].second; if(now > w){ res = "No"; break; } } cout << res << endl; return 0; }
#include <stdio.h> #include <iostream> #include <vector> #include <queue> #include <stack> #include <algorithm> using ll = long long int; const int INF = (1<<30); const ll INFLL = (1ll<<60); const ll MOD = (ll)(1e9+7); #define l_ength size void mul_mod(ll& a, ll b){ a *= b; a %= MOD; } void add_mod(ll& a, ll b){ a = (a<MOD)?a:(a-MOD); b = (b<MOD)?b:(b-MOD); a += b; a = (a<MOD)?a:(a-MOD); } ll v[225816]; int main(void){ int n,s,t,i; ll w,p; std::cin >> n >> w; for(i=0; i<n; ++i){ std::cin >> s >> t >> p; v[s] += p; v[t] -= p; } for(i=1; i<225816; ++i){ v[i] += v[i-1]; } for(i=0; i<225816; ++i){ if(v[i]>w){ std::cout << "No" << std::endl; return 0; } } std::cout << "Yes" << std::endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 2000000 + 5, Mod = 1000000007; int n = 0, m = 0, k = 0; long long arr[N] = {}, x = 0, y = 0; long long f(int num, int cnt){ if(cnt == 0) return 1; long long temp = f(num, cnt / 2); if(cnt % 2 == 0) return (temp * temp) % Mod; else return ((temp * temp) % Mod) * num % Mod; } int main(){ cin >> n >> m >> k; if(n > m + k){ cout << 0; return 0; } arr[0] = 1; for(int i = 1 ; i <= m + n + 3 ; i ++) arr[i] = (arr[i - 1] * i) % Mod; x = (((arr[m + n] * f(arr[m], Mod - 2)) % Mod) * f(arr[n], Mod - 2)) % Mod; if(k != n) y = (((arr[m + n] * f(arr[m + k + 1], Mod - 2)) % Mod) * f(arr[n - k - 1], Mod - 2)) % Mod; cout << (x + Mod - y) % Mod; return 0; }
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> #define rep(i,a,b) for(ll i=a;i<b;i++) #define rrep(i,b,a) for(int i=b;i>=a;i--) #define fori(a) for(auto i : a ) #define all(a) begin(a), end(a) #define set(a,b) memset(a,b,sizeof(a)) #define sz(a) a.size() double pi=acos(-1); #define ll long long #define ull unsigned long long #define pb push_back #define PF push_front //deque // #define mp make_pair #define pq priority_queue const ll mod=1000000007; #define f first #define s second #define pii pair< ll, ll > #define vi vector<int> #define vpii vector<pii> #define debug(v) for(auto i:v) cout<<i<<" "; #define tc int t; cin >> t; while(t--) // using namespace boost::multiprecision; using namespace std; void optimizeIO(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const int N=2000005; ll fact[N],invfact[N]; ll power(ll x,ll y){ if(y<=0) return 1LL; ll z=power(x,y/2); if(y%2) return (((z*z)%mod)*x)%mod; return (z*z)%mod; } void pre(){ fact[0]=invfact[0]=invfact[1]=fact[1]=1; rep(i,2,N) fact[i]=(i*fact[i-1])%mod; rep(i,2,N) invfact[i]=(invfact[i-1]*power(i,mod-2))%mod; } ll nCr(ll n,ll k){ return (((fact[n]*invfact[n-k])%mod)*invfact[k])%mod; } const int N1=1e6+1; vector<int> isprime(N1,1),prime; void seive(){ rep(i,2,sqrt(N1)+1){ if(isprime[i]){ for(int j=i*i;j<N1;j+=i) isprime[j]=0; prime.pb(i); } } rep(i,sqrt(N1)+1,N1) if(isprime[i]) prime.pb(i); } struct dsu { vector<int> par, rank; dsu(int n): par(n+1), rank(n+1) { for (int i = 0; i <= n; i++) { par[i] = i; rank[i]= 1; } } int root(int a) { if (a == par[a]) return a; return par[a] = root(par[a]); } void merge(int a, int b) { a = root(a); b = root(b); if (a == b) return; if (rank[a] > rank[b]) swap(a, b); par[b] = a; } set<int> parent(int n){ set<int> s; for(int i=1;i<=n;i++){ s.insert(root(i)); } return s; } }; ll solve1(ll x,ll y){ if(x<0 || y<0) return 0; return nCr(x+y,x); } void solve(){ int n,m,k; cin>>n>>m>>k; // (0,0) to (m,n) // -k-1,k+1 if(n>m+k) cout<<0<<endl; else cout<<(solve1(n,m)-solve1(n-(k+1),m-(-k-1))+mod)%mod<<endl; } int main(){ optimizeIO(); int r=1; pre(); {solve();} }
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<bitset> #include<cmath> #include<ctime> #include<queue> #include<map> #include<set> #define int long long #define fi first #define se second #define max Max #define min Min #define abs Abs #define lc (x<<1) #define rc (x<<1|1) #define mid ((l+r)>>1) #define lowbit(x) (x&(-x)) #define fan(x) (((x-1)^1)+1) #define mp(x,y) make_pair(x,y) #define clr(f,n) memset(f,0,sizeof(int)*(n)) #define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n)) using namespace std; inline int read() { int ans=0,f=1; char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=(ans<<1)+(ans<<3)+c-'0';c=getchar();} return ans*f; } inline void write(int x) { if(x<0) putchar('-'),x=-x; if(x/10) write(x/10); putchar((char)(x%10)+'0'); } template<typename T>inline T Abs(T a){return a>0?a:-a;}; template<typename T,typename TT>inline T Min(T a,TT b){return a>b?b:a;} template<typename T,typename TT> inline T Max(T a,TT b){return a>b?a:b;} const int N=3e3+5; int n,now,a[N]; signed main() { n=read()-3; printf("10 15 6 "); a[1]=10;a[2]=15;a[3]=6; now=3; for(int i=7;i<=100000&&n;++i) { int flag=1; for(int j=1;j<=now;++j) if(__gcd(a[j],i)==1||a[j]==i) flag=0; if(flag) { a[++now]=i;--n; printf("%lld ",i); } } printf("\n"); return 0; }
#include<bits/stdc++.h> using namespace std; inline int read() { register int x=0,f=1,ch=getchar(); while(!isdigit(ch)) { if(ch=='-') f=-1; ch=getchar(); } while(isdigit(ch)) x=x*10 +ch-'0',ch=getchar(); return x*f; } int n,k; int main() { n=read(),k=read(); printf("%d\n",(n+1)*n/2*k*100+(k+1)*k/2*n); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; vector<int> g(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; --u, --v; g[u] |= 1 << v; g[v] |= 1 << u; } vector<int> dp(1 << n, LLONG_MAX); dp[0] = 1; for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < n; j++) { if (dp[i] == 1 && (g[j] & i) == i) dp[i | 1 << j] = 1; } } for (int i = 1; i < (1 << n); i++) { for (int j = i; --j &= i;) { dp[i] = min(dp[i], dp[i ^ j] + dp[j]); } } cout << dp.back(); }
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using ld = long double; using P = pair<ll, ll>; using Vec = vector<ll>; using VecP = vector<P>; template <class T> using Vec2 = vector<vector<T>>; #define REP(i, m, n) for(int i = (m); i < (n); ++i) #define REPN(i, m, n) for(int i = (m); i <= (n); ++i) #define REPR(i, m, n) for(int i = (m)-1; i >= (n); --i) #define REPNR(i, m, n) for(int i = (m); i >= (n); --i) #define rep(i, n) REP(i, 0, n) #define repn(i, n) REPN(i, 1, n) #define repr(i, n) REPR(i, n, 0) #define repnr(i, n) REPNR(i, n, 1) #define all(s) (s).begin(), (s).end() template <class T1, class T2> bool chmax(T1 &a, const T2 b) { return a < b ? a = b, true : false; } template <class T1, class T2> bool chmin(T1 &a, const T2 b) { return a > b ? a = b, true : false; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; } void _co() { cout << '\n'; } template <class Head, class... Tail> void _co(Head&& head, Tail&&... tail) { cout << ' ' << head; _co(forward<Tail>(tail)...); } template <class Head, class... Tail> void co(Head&& head, Tail&&... tail) { cout << head; _co(forward<Tail>(tail)...); } void ce() { cerr << '\n'; } template <class Head, class... Tail> void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); } void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); } void setp(const int n) { cout << fixed << setprecision(n); } constexpr int64_t INF = 1000000000000000001; constexpr int64_t MOD = 1000000007; constexpr int64_t MOD_N = 998244353; constexpr long double EPS = 1e-11; const double PI = acos(-1); int main(void) { int n, m; cin >> n >> m; const int N = 1 << n; vector<bitset<18>> g(n); rep(i, m) { int a, b; cin >> a >> b; --a, --b; g[a][b] = true; g[b][a] = true; } rep(i, n) g[i][i] = true; vector<int> dp(1 << n, 1e9); dp[0] = 0; rep(bit, N) { bool flg = true; rep(i, n) { rep(j, n) { if ((bit >> i) & (bit >> j) & 1) flg &= g[i][j]; } } if (flg) dp[bit] = 1; } rep(bit, N) { for (int i = bit; --i &= bit; ) { chmin(dp[bit], dp[i] + dp[bit ^ i]); } } co(dp.back()); return 0; }
#include<bits/stdc++.h> using namespace std; #define en '\n' #define endl '\n' #define ll long long signed #define pb push_back #define append push_back #define all(v) (v).begin(),(v).end() #define FOR(i,a,b,k) for(int i=a; k>0?i<b:i>b; i+=k) #define fori(a) for(int i=0; i<a; i++) #define forj(a) for(int j=0; j<a; j++) #define fork(a) for(int k=0; k<a; k++) #define MOD 1000000007 #define MAX 1000010 #define mset(a,s,k) memset(a,k,s*sizeof(a[0])) #define ff first #define ss second #define __ <<" "<< #define _ <<en #define int ll ll pow_(ll a,ll e,ll m=MOD){ll r=1;while(e){if(e&1)r=r*a%m;a=a*a%m;e>>=1;}return r;} int inv(ll a) {return (int)(pow_(a,MOD-2,MOD));}int add(ll a, ll b){return (a+b+MOD)%MOD;} int mul(ll a, ll b) {return (MOD+a*b%MOD)%MOD;}int di(ll a, ll b){return mul(a,inv(b));}ll ceil(ll a,ll b){return a/b+(a%b!=0);} template<typename T1,typename T2>ostream&operator<<(ostream&O,pair<T1,T2>a){O<<"( ";O<<a.ff<<", "<<a.ss;O<<" )";return O;} template<typename T>ostream&operator<<(ostream&O,vector<T>V){O<<"[ ";fori(V.size())O<<V[i]<<(i==V.size()-1?"":", ");O<<" ]";return O;} template<typename T>ostream&operator<<(ostream&O,set<T>S){O<<"{ ";for(T i:S)O<<i<<", ";O<<" }";return O;} template<typename T1,typename T2>ostream&operator<<(ostream&O,map<T1,T2>S){O<<"{ ";for(auto i:S)O<<i<<", ";O<<" }";return O;} template<typename T=int>class print{public:vector<T>V;print(vector<T>v){V=v;cout<<" <"<<V<<"> ";} print(T*x,int N){fori(N)V.push_back(x[i]);cout<<" <"<<V<<"> ";}friend ostream&operator<<(ostream&O,print p){return O;}}; #define vi vector<int> #define tp pair<int,int> int arr[MAX]; int N,M; void reset(); vector<int> G[MAX]; int mi[MAX]; int dfs(int x){ int &r=mi[x]; if(r==INT_MIN){ for(auto i: G[x]){ r=max(r,dfs(i)); } } return max(r,arr[x]); } int solve(){ cin>>N>>M; reset(); fori(N){ cin>>arr[i];} fori(M){ int a,b; cin>>a>>b; G[a-1].push_back(b-1); } int res=INT_MIN; fori(N){ dfs(i);// res=max(res, dfs(i,INT_MAX)); } // cout<<print(mi,N) _; fori(N){ if(mi[i]!=INT_MIN) res=max(res,mi[i]-arr[i]); } cout<<res; return 0; } void reset(){ fori(N) { G[i].clear(); mi[i]=INT_MIN; } // Clear Vectors, maps, sets // Set default values of arr } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t=1; while(t--){ solve(); // cout<<solve()<<en; // cout<<(solve()?"Yes":"No")<<endl; } return 0; }
#include <iostream> #include <vector> using namespace std; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #define MOD 1000000007 #define INF (ll)1e15 #define ll long long int #define pll pair<ll, ll> #define vll vector<ll> #define vpll vector<pll> #define pb push_back #define endl '\n' // Remove if interactive #define ff first #define ss second #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL) //#define all(v) v.begin(), v.end() #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) ll power(ll x, ll y) { ll ans = 1; x %= MOD; while (y) {if (y & 1)ans = (x * ans) % MOD; x = (x * x) % MOD; y >>= 1;} return ans;} const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; //#include <algorithm> //#include <map> //#include <queue> //#include <cstring> //#include <bits/stdc++.h> vll max_cost; vll cost; vector<vll> edges; ll dfs(ll si) { if(max_cost[si] != -1) return max_cost[si]; max_cost[si] = cost[si]; for(ll ei : edges[si]) { max_cost[si] = max(max_cost[si], dfs(ei)); } return max_cost[si]; } void solve() { ll n, m; cin >> n >> m; max_cost.resize(n, -1); cost.resize(n); edges.resize(n); for(int i = 0; i < n; i++) { cin >> cost[i]; } for(int i = 0; i < m; i++) { ll x, y; cin >> x >> y; --x,--y; edges[x].pb(y); } for(int i = 0; i < n; i++) { dfs(i); } ll ans = -INF; //debug(max_cost); for(int i = 0; i < n; i++) { for(ll ei : edges[i]) { ans = max(ans, max_cost[ei] - cost[i]); } } cout << ans << endl; } int main() { fastio; solve(); } //4 //3 //100 200 300 450 //1 2 //2 3 //3 4 // 2 1 10 20 2 1
#include <bits/stdc++.h> #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); typedef long long ll; typedef long double ld; #define pb push_back #define mp make_pair #define ff first #define ss second #define mod 1000000007 #define pii pair<ll,ll> #define inf 1000000000000000000 #define bpc(x) __builtin_popcountll(x) #define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++) #define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++) #define rep(n) for(ll i = 0; i < n; i++) #define repi(i,n) for(ll i = 0; i < n; i++) #define hmap gp_hash_table<ll, ll> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); int main() { FAST/**/ ll n; cin>>n; ll tot = 0; ll cr = 1; while(tot < n) { tot += cr; if(tot >= n) { cout<<cr<<'\n'; return 0; } cr++; } return 0; }
#include <bits/stdc++.h> #define LL long long #define pb push_back using namespace std; char a[15],c[5]="ZONe"; int ans; int main() { scanf("%s",a+1); for(int i=1;i<=12;++i) { int flag=1; for(int j=0;j<4;++j) if(a[i+j]!=c[j]) flag=0; ans+=flag; } cout<<ans; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for (long long i = 0; i < (n); ++i) #define INF LONG_MAX/3 //#define DIV 1000000007 //#define DIV 998244353 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 dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; //Aがindexの時に対応するB ll A[210]; ll B[210]; set<ll> nums; bool dp[205]; bool check(ll start, ll end) { ll l = end - start + 1; l/=2; for(ll i = start; i + l <= end; i++) { ll s = i; ll e = s + l; if(nums.count(s) && nums.count(e)) { if(A[s] == e && B[e] == s) continue; return false; } else if(nums.count(s)) { if(A[s] == 0) return false; if(A[s] == -1) continue;//ok if(A[s] != e) { return false; } } else if(nums.count(e)) { if(B[e] == 0) return false; if(B[e] == -1) continue;//ok if(B[e] != s) { return false; } } else { //-1, -1 ok } } return true; } int main(){ ll N; cin >> N; rep(i, 210) A[i] = 0; rep(i, 210) B[i] = 0; rep(i, N) { ll a, b; cin >> a >> b; A[a] = b; B[b] = a; if(a != -1) { if(nums.count(a)) { cout << "No" << endl; return 0; } nums.insert(a); } if(b != -1) { if(nums.count(b)) { cout << "No" << endl; return 0; } nums.insert(b); } } dp[0] = true; for(ll i = 0; i <= 2*N; i++) { if(!dp[i]) continue; ll start = i + 1; for(ll end = start + 1; end <= 2*N; end += 2) { if(check(start, end)) { dp[end] = true; } } } if(dp[2*N]) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
#include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #pragma GCC optimize("Ofast") using namespace std; #define int long long #define double long double #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define ArrayFill(arr,a) memset(arr,a,sizeof arr) #define endl '\n' #define countBit(x) __builtin_popcountll(x) #define all(x) x.begin() , x.end() #define ln cout<<'\n'; using namespace std; using namespace __gnu_pbds; // typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> oset; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; int ceil(int a,int b){return (a+b-1)/b;} #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_ {~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 << "]";}}; #define db(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define debug(x) debug_() << db(x) int modulo(int a,int b,int c){ int x=1,y=a%c; while(b > 0){ if(b%2 == 1) x=(x*y)%c; y = (y*y)%c; b = b>>1; } return x%c; } // const int mod = 1e9 + 7ll; const int mod = 998244353ll; const int N = 505; const int inf = 1e10; string input[N]; void solve(){ int n,m; cin >> n >> m; for(int i=0;i<n;i++){ cin >> input[i]; } map<int,vector<char>>mp; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ mp[i+j].push_back(input[i][j]); } } int ans=1; for(auto &p:mp){ int r{0},b{0},d{0}; for(auto& i:p.second){ if(i == 'R')r++; else if(i == 'B')b++; else d++; } if(r==0 && b==0){ ans = (ans*2ll)%mod; } else if(r != 0 && b != 0){ ans = 0; } } cout << ans; } signed main(){ fast; int tt{1}; // cin >> tt; for(int ii=1;ii<=tt;ii++){ // cout << "Case #" << ii << ": "; solve(); ln; } return 0; }
#include <bits/stdc++.h> using namespace std; #define ull unsigned long long #define MOD (int)(1e9+7) #define MOD1 998244353 #define ceil(x, y) ((x)%(y))==0? ((x)/(y)) : ((x)/(y)+1) #define FOR(i, N) for(int i = 0; i < N; ++i) #define FOR1(i, N) for(int i = 1; i <= N; ++i) #define vi vector <int> #define pii pair <int, int> #define pb push_back #define mp make_pair #define ff first #define ss second #define mset(a, v) memset(a, v, sizeof(a)) #define all(v) (v).begin(), (v).end() #define INF 2e9 #define EPS 1e-9 #define int long long /*#include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; typedef cc_hash_table<int, int, hash<int>> ht; // while using, comment #define int long long */ void __print(int x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define dbg(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define dbg(x...) #endif using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("in7.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif int T=1; // cin >> T; FOR1(tt, T){ // cout << "Case #" << tt << ": "; int N, C; cin >> N >> C; /*The only points where some changes happen are ai and bi + 1 We need to do range update with compressed coordinates */ set<int> st; int a[N+1], b[N+1], c[N+1]; FOR1(i, N){ cin >> a[i] >> b[i] >> c[i]; st.insert(a[i]); st.insert(b[i] + 1); } // coordinate compression int idx = 0; map<int, int> m; for(int pt : st){ m[pt] = idx; idx++; } // range update on the compressed array -- days /*But what does days[i] denote? Index i corresponds to some uncompressed point, p. i+1 corresponds to the next point to the right of p, say q. days[i] will denote the cost(w/o prime) to avail services in [p, q) */ int compressed_size = idx; assert(m.size() == compressed_size); vector<int> days(compressed_size); for(int i = 1; i <= N; i++){ days[m[a[i]]] += c[i]; if(m[b[i] + 1] < compressed_size) days[m[b[i] + 1]] -= c[i]; } // take prefix-sum and calculate ans for(int i = 1; i < compressed_size; i++){ days[i] += days[i-1]; } int ans = 0; /* create a vector from the set of points In the compressed days array, we only have the cost values but we need to know their duration too i.e decompression */ vector<int> tmp(st.begin(), st.end()); for(int i = 0; i + 1 < compressed_size; i++){ int span = tmp[i+1] - tmp[i]; ans += min(days[i], C) * span; } cout << ans; cout << '\n'; } return 0; }
//全力以赴 #include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define PI 3.141592653L #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); const ll INF = (ll)1e18; const int N = 2e5 + 5; const ll MOD = 1e9+7; int parent[N]; const int dx[4] = {+1, 0, -1, 0}; const int dy[4] = {0, -1, 0, +1}; ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } ll extGcd(ll a, ll b, ll &x, ll &y) { // solves for x and y where ax+by=gcd(a,b) if(b == 0) { x = 1; y = 0; return a; } ll d = extGcd(b, a % b, y, x); y -= a / b * x; return d; } ll modInv(ll a, ll p) { // solve for x=a^-1 where ax + py = 1 % p if(gcd(a, p) != 1) return -1; // modInv only exists if a and p are coprime (gcd(a, p) = 1) ll x, y; extGcd(a, p, x, y); x = (x % p + p) % p; return x; } ll lcm(ll a, ll b) { return (a * b) / gcd(a,b); } /* a^b: if b is even -> a^b = a^(b/2) * a^(b/2) if b is odd -> a^b = a * a^(b-1/2) * a^(b-1/2) */ ll modPow(ll a, ll n, ll mod) { ll res = 1; while (n > 0) { if (n & 1) res = (res * a) % mod; a = (a * a) % mod; n >>= 1; } return res; } int find(int x) { return x == parent[x] ? x : parent[x] = find(parent[x]); } void Union(int x, int y) { int xPar = find(x), yPar = find(y); if(xPar != yPar) parent[xPar] = yPar; } ll nCr(ll n, ll r) { ll res = 1; for(ll i=1;i<=r;i++) { res = res * (n - r + i) / i; } return res; } int ask(int i, int j) { cout << "? " << i + 1 << ' ' << j + 1 << endl; int v; cin >> v; return v; } int msbPos(int n) { if(n == 0) return 0; int msbPos = 0; n /= 2; while(n != 0) { n /= 2; msbPos++; } return msbPos; } void solve() { int n; ll C; cin >> n >> C; vector<pair<ll, ll>> events; for(int i=0;i<n;i++) { ll a, b, c; cin >> a >> b >> c; events.push_back({a, +c}); events.push_back({b + 1, -c}); } sort(events.begin(), events.end()); ll res = 0, now = 0, cost = 0; int m = events.size(); for(int i=0;i<m;i++) { ll dt = events[i].first - now; ll dc = events[i].second; res += dt * min(C, cost); cost += dc; now += dt; } cout << res << endl; } int main() { IOS //int t; //cin >> t; //while(t--) solve(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define mp make_pair #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define sz(x) int(x.size()) #define pcnt __builtin_popcountll template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;} template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;} template <typename T> void print(T a) {cout << a << ' ';} template <typename T> void printe(T a) {cout << a << endl;} template <typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;} template <typename T> void printp(pair<T, T> a) {print(a.first); cout<<a.second<<endl;} template <typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);} template <typename T> using vc = vector<T>; template <typename T> using vv = vc<vc<T>>; template <typename T> using vvv = vc<vv<T>>; using ll = long long; using P = pair<int, int>; using Pl = pair<ll, ll>; using vi = vc<int>; using vvi = vv<int>; using vl = vc<ll>; using vvl = vv<ll>; vi di = {-1, 1, 0, 0, -1, -1, 1, 1}; vi dj = { 0, 0, -1, 1, -1, 1, -1, 1}; int main () { int n; cin >> n; int m = 5; vvi a(n, vi(m)); rep(i, n) rep(j, m) cin >> a[i][j]; vi MX(m, 0); rep(i, n) rep(j, m) chmax(MX[j], a[i][j]); int ans = 0; rep(i, n) { rep(j, i) { vi mx(m); // i,jさんの最大値 rep(k, 5) mx[k] = max(a[i][k], a[j][k]); int mn = 2e9, im = -1; rep(k, 5) if (chmin(mn, mx[k])) im = k; mx[im] = MX[im]; int res = 2e9; rep(k, 5) chmin(res, mx[k]); chmax(ans, res); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define speedup \ ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr), \ cout.precision(16) using namespace std; using ll = long long; #define pb push_back #define all(x) (x).begin(), (x).end() using ld = long double; void solve() { int n; cin>>n; vector<vector<ll>> ar(n,vector<ll>(5)); for(auto& it:ar) { for(auto& iit:it) { cin>>iit; } } ll l=0,r=1e10; while(l<r) { ll md=(l+r)>>1; vector<int> cnt(32); for(auto& it:ar) { int msk=0; for(int i=0;i<5;++i) { if(it[i]>=md) { msk|=1<<i; } } ++cnt[msk]; } int ok=false; for(int i=0;i<32;++i) { for(int j=0;j<32;++j) { for(int k=0;k<32;++k) { int tmp=0; if(cnt[i]>0) tmp|=i; if(cnt[j]>0) tmp|=j; if(cnt[k]>0) tmp|=k; if(tmp==31) { ok=true; break; } } } } if(ok) l=md+1; else r=md; } cout<<r-1<<'\n'; } int main() { speedup; //int t; //cin >> t; //while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; typedef long long ll; // int/long: -2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31) // long/long long: -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 (-2^63 <= long < 2^63) //#define INF (1<<30) #define INF (2147483647) // 2^31 -1 //= 1,073,741,824 *2 -1 //= 536,870,912 *4 -1 #define MOD 1000000007 #define Rep0(i, n) for (auto i=0; i<n; i++) #define Rep1(i, n) for (auto i=1; i<=n; i++) #define Sort(P) sort(P.begin(), P.end()) #define Rev(P) reverse(P.begin(), P.end()) int main() { int N; cin >>N; if (1==N%2) cout <<"Black" <<endl; else cout <<"White" <<endl; return 0; }
//================code===================// //#define TLE #ifdef TLE #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #endif #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> #include <random> #include <ctime> #include <random> #include <chrono> //#include <stdint.h> #define ci(t) cin>>t #define co(t) cout<<t #define LL long long #define ld long double #define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i) #define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i) #define setp pair<int,ld> #define setl pair<LL,LL> #define micro 0.000000000001 using namespace std; LL gcd(LL a, LL b) { if (!(a && b)) return max(a, b); return a % b ? gcd(b, a % b) : b; } #ifdef OHSOLUTION #define ce(t) cerr<<t #define AT cerr << "\n=================ANS=================\n" #define AE cerr << "\n=====================================\n" #define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl; #define __builtin_popcount __popcnt #define __builtin_popcountll __popcnt64 #define LLL LL #else #define AT #define AE #define ce(t) #define LLL __int128 #endif pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{-1,0},{-1,-1}, {0,-1} , {1,-1},{-1,-1} }; //RDLU EWSN template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; } template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; } struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } }; struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } }; const int max_v = 2e3+ 7; const int max_k = 5e2 + 7; const int bsz = (1ll << 10) + 7; const int INF = 1e9 + 7; const LL LNF = (LL)5e18 + 7ll; LL mod = 998244353;//1e9 + 7; template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; }; int main() { #ifdef OHSOLUTION freopen("input.txt", "r", stdin); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; ci(n); vector<LL> vi(n); LL sum = 0; for (auto& x : vi) ci(x), sum+=x; vector<bool> dp(sum + 7, 0); dp[0] = 1; LL cmax = 0; for (auto& x : vi) { fd(i, cmax, -1) if(dp[i]) { dp[i + x] = 1; } cmax += x; } LL ans = LNF; fa(i, 0, sum + 1) if(dp[i]) ckmin(ans, max(i, sum - i)); co(ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using vl=vector<int>; using vvl=vector<vl>; using P=pair<int,int>; const int INF=(1<<30); struct graph{ vvl edges; vvl cost; vl dist; graph(int n):edges(n), cost(n), dist(n, INF){} void add_edge(int from, int to, int c){ edges[from].push_back(to); cost[from].push_back(c); } void dijkstra(int start){ priority_queue<P,vector<P>,greater<P>> q; dist[start]=0; q.push(P(0,start)); while (q.size()!=0) { P p=q.top(); q.pop(); int v=p.second; if(dist[v]<p.first)continue; for (int i = 0; i < edges[v].size(); i++){ int to=edges[v][i]; ll tmp_cost=dist[v]+cost[v][i]; if(dist[to]>tmp_cost){ dist[to]=tmp_cost; q.push(P(dist[to],to)); } } } } }; int R,C; int id(int r, int c){return r*C + c;} int main() { cin>>R>>C; int goal = id(R-1, C-1); vvl A(R, vl(C-1)), B(R-1, vl(C)); 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]; graph g(R*C); for (int r = 0; r < R; r++){ for (int c = 0; c < C; c++){ if(c+1<C)g.add_edge(id(r,c), id(r,c+1), A[r][c]); if(c>0)g.add_edge(id(r,c), id(r,c-1), A[r][c-1]); if(r+1<R)g.add_edge(id(r,c), id(r+1,c), B[r][c]); for (int dr = 1; r-dr >= 0; dr++){ g.add_edge(id(r,c), id(r-dr,c), dr+1); } // if(r>0)g.add_edge(id(r,c), id(r-1,c), 1); } } g.dijkstra(0); cout<<g.dist[goal]<<endl; }
#include <bits/stdc++.h> using namespace std; #define int long long using vec_int = vector<int>; using P = pair<int,int>; using T = tuple<int,int,int>; using T2 = tuple<int,int,int, int>; using ll = long long; #define rep(i, n) for(int i = 0; i < (int)(n); i++) int charToInt(char c){ char zero_num = '0'; return (int)c - (int)zero_num; } signed main(){ int R, C; cin>>R>>C; vector<vec_int> A(R, vec_int(C-1)); rep(i,R)rep(j,C-1)cin>>A.at(i).at(j); vector<vec_int> B(R-1, vec_int (C)); rep(i,R-1)rep(j,C)cin>>B.at(i).at(j); vector<vec_int> visited(R, vec_int(C, 0)); priority_queue<T2, vector<T2>, greater<T2>> pq; pq.emplace(0, 0, 0, 0); while(!pq.empty()){ int cost, r, c, type; tie(cost, r, c, type)=pq.top(); pq.pop(); if(visited.at(r).at(c)==1)continue; if(r==R-1&&c==C-1){ cout<<cost<<endl; return 0; } visited.at(r).at(c) = 1; if(c<C-1){ if(visited.at(r).at(c+1)==0){ pq.emplace(cost+A.at(r).at(c), r, c+1, 1); } } if(c>0){ if(visited.at(r).at(c-1)==0){ pq.emplace(cost+A.at(r).at(c-1), r, c-1, 1); } } if(r<R-1){ if(visited.at(r+1).at(c)==0){ pq.emplace(cost+B.at(r).at(c), r+1, c, 0); } } if(type==1){ for(int i=0;i<r;i++){ if(visited.at(i).at(c)==0){ pq.emplace(cost+(r-i+1), i, c, 0); } } } } return 0; }
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> #include <functional> // for less using namespace std; #define int long long #define pb push_back #define mp make_pair #define st(arr,a) memset(arr,a,sizeof arr) #define nl cout<<'\n' #define bitcount(x) __builtin_popcountll(x) const int MOD = 1000000007ll; #define forn(i,a,b) for(int i=a;i<=b;i++) #define rfor(i,a,b) for(int i=a;i>=b;i--) #define all(x) x.begin() , x.end() #define pi pair<int,int> #define X first #define Y second #define sz(A) (int)A.size() #define N 1014159 #define vi vector<int> #define v vector #define die exit(0) #define alln(arr) arr+1 , arr+1+n using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset; // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). #define in input_int() #define ins input_string() bool sb(int a,int i){return (a&(1LL<<i))>0;} int p2(int a){return (1LL<<a);} int input_int(){int a;cin>>a;return a;} string input_string(){string s;cin>>s;return s;} int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int ceil(int a,int b){return (a+b-1)/b;} int ct,visited[N],ans; vi adj[N]; int n,m,k,a,b,c,d,x,y,arr[N]; void clr() { forn(i,0,n+10)adj[i].clear(),visited[i]=0,arr[i]=0; } string s; void solve() { cin>>n; int sum=0; forn(i,1,n) { cin>>arr[i]; sum+=arr[i]; } sort(alln(arr)); int ans=0; forn(i,1,n) { sum-=arr[i]; ans-=(n-i)*arr[i]; ans+=sum; } cout<<ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; //cin>>t; forn(i,1,t) { //cout<<":Case #"<<i<<": "; solve(); nl; } return 0; } // clang++ c.cpp ; ./a.out < a.txt ;
#include <bits/stdc++.h> #define FLASH ios_base::sync_with_stdio(0); #define ll long long #define debt(x,y)cout<<"#x = "<<(x)<<" and "<<"#y = "<<(y)<<endl; #define deb(x)cout<<"#x = "<<(x)<<endl; #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define endl "\n" #define arr(a,n) for(ll i=1;i<=n;i++) cout<<a[i]<<" "; cout << "\n"; #define vecc(a,n) for(ll i=0;i<n;i++) cout<<a[i]<<" "; cout << "\n"; using namespace std; ll n; string stroka; ll ans=0; int main(){ FLASH; cin>>n; cin>>stroka; for(ll i=0;i<n;i++) { ll a=0,t=0,c=0,g=0; for(ll j=i;j<n;j++) { if(stroka[j]=='A')a++; else if(stroka[j]=='G')g++; else if(stroka[j]=='C')c++; else t++; if(a==t && g==c)ans++; } } cout<<ans<<endl; return 0; }
#include<iostream> #include<map> #include<set> #include<cstdio> using namespace std; const int BUF = 1500005; int nVal, range; int val[BUF]; void read() { scanf("%d%d", &nVal, &range); for (int i = 0; i < nVal; ++i) { scanf("%d", &val[i]); } } void add(int v, int v2cnt[BUF], set<int> &candi) { if (++v2cnt[v] == 1) { candi.erase(candi.find(v)); } } void remove(int v, int v2cnt[BUF], set<int> &candi) { if (--v2cnt[v] == 0) { candi.insert(v); } } void work() { set<int> candi; for (int i = 0; i < BUF; ++i) { candi.insert(i); } static int v2cnt[BUF] = {}; for (int i = 0; i < range; ++i) { add(val[i], v2cnt, candi); } int minV = *candi.begin(); for (int i = range; i < nVal; ++i) { add(val[i], v2cnt, candi); remove(val[i - range], v2cnt, candi); minV = min(minV, *candi.begin()); } cout << minV << endl; } int main() { read(); work(); return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; #define endl "\n" #define ll long long #define IOS ios_base::sync_with_stdio(false); cin.tie(nullptr); const int N = 105; set<int> st; int main() { IOS; int n; cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; set<int> temp; for (int x : st) { temp.insert(x + a); } st.insert(a); for (int x : temp) { st.insert(x); } } int sum = *st.rbegin(); int ans = 1e9; for (int x : st) { ans = min(ans, max(sum - x, x)); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n; cin>>n; set<ll> memo; for(ll i=1; i*i<=n; i++){ if(n%i==0){ memo.insert(i); memo.insert(n/i); } } for(auto x : memo){ cout<<x<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(ll i=0; i<n; ++i) #define rep1(i,n) for(ll i=1; i<=n; ++i) #define revrep(i,n) for(ll i=n-1; i>=0; --i) inline constexpr ll Inf = (1ULL << 62) -1; #define fastio cin.tie(nullptr); ios_base::sync_with_stdio(false); #define endl '\n' #define YN(e) ((e)?"Yes":"No") template <class T> bool updmax(T& a, T b) { if (b > a) { a = b; return true;} return false;} template <class T> bool updmin(T& a, T b) { if (b < a) { a = b; return true;} return false;} int main() { fastio; ll N; cin >> N; ll m = sqrt(N) + 10; vector<ll> d, a; ll i=1; for (; i<m; ++i) { if (N % i == 0 && i*i <= N) { d.push_back(i); } } a = d; rep(i, (int)d.size()) { a.push_back(N / d[i]); } sort(begin(a), end(a)); a.erase(unique(begin(a), end(a)), end(a)); rep(i, (int)a.size()) cout << a[i] << endl; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using vi = vector<int>; using vvi = vector<vector<int>>; using vl = vector<ll>; using vvl = vector<vector<ll>>; using pl = pair<ll,ll>; using pi = pair<int,int>; #define all(x) x.begin(),x.end() #define rep(i,j,n) for (long long i = j; i < (long long)(n); i++) #define _GLIBCXX_DEBUG #define Please return #define AC 0 const ll MOD = 1000000007; 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; } //(a+b-1)/b //priority_queue<ll, vector<ll>, greater<ll>> q; signed main(){ cout << fixed << setprecision(10); double n; cin >> n; double ans = 0; rep(i,1,n+1){ ans += 1 / (n-i+1); } cout << n * ans - 1 << endl; Please AC; }
#include <bits/stdc++.h> using namespace std; using ll =unsigned long long; typedef pair<ll,ll> P; #define SORT(a) sort((a).begin(),(a).end()) #define REV(a) reverse((a).begin(),(a).end()) #define For(i, a, b) for(int i = (a) ; i < (b) ; ++i) #define rep(i, n) For(i, 0, n) #define debug(x) cerr << #x << " = " << (x) << endl; 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; } void coY() {cout <<"Yes"<<endl;} void coN(){cout <<"No"<<endl;} void mswap(ll &a, ll &b){ if(a >= b) swap(a,b); } void rswap(ll &a, ll &b){ if(a <= b) swap(a,b); } const int dy[] = {0,0,1,-1}; const int dx[] = {1,-1,0,0}; const ll mod = 1e9+7; const ll MOD = 998244353; const double PI=3.14159265358979323846; const int inf = 1001001001; const ll INF = 1e18; //Write From this Line int main() { int n; cin >> n; double ans = 0; int tmp = n-1; rep(i,n-1){ ans += double((double)n/(double)tmp); tmp--; } cout << setprecision(15); cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i,N) for (int i = 0; i < (N); ++i) #define all(v) v.begin(), v.end() using namespace std; using ll = long long; using P = pair<int, int>; const int inf{int(1e9)}; int main(void){ int N, M; cin >> N >> M; if (M == 0){ cout << 1 << endl; return 0; } if (N == M){ cout << 0 << endl; return 0; } vector <ll> A(M); rep(i,M){ cin >> A[i]; A[i]--; } sort(all(A)); vector <ll> sa; if (A[0] != 0){ sa.push_back(A[0]); } rep(i,M-1){ if (A[i+1] - A[i] >= 2){ sa.push_back(A[i+1] - A[i] - 1); } } if(A[M-1] != N-1){ sa.push_back(N-1 - A[M-1]); } ll hanko = *min_element(all(sa)); // 使うハンコの大きさ ll ans = 0; rep(i,sa.size()){ ans += (hanko + sa[i] - 1) / hanko; } cout << ans << endl; //rep(i,sa.size()) cout << sa[i] << endl; return 0; }
#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("fast-math") #pragma GCC optimize("trapv") #pragma GCC target("sse4") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define deb(x) cout << #x << " is " << x << "\n" #define int long long #define MOD 1000000007LL #define mod(x) (x % MOD + MOD) % MOD #define PI acos(-1) template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using max_heap = priority_queue<T>; template <class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; template <typename... T> void read(T &... args) { ((cin >> args), ...); } template <typename... T> void write(T &&... args) { ((cout << args), ...); } template <typename T> void readContainer(T &t) { for (auto &e : t) read(e); } template <typename T> void writeContainer(T &t) { for (const auto &e : t) write(e, " "); write("\n"); } void solve(int tc) { int n, m; read(n, m); vector<int> arr(m); readContainer(arr); if (m == 0) { write(1); return; } sort(arr.begin(), arr.end()); vector<int> stamps; if (arr[0] > 1) { stamps.push_back(arr[0] - 1); } for (int i = 1; i < m; i++) { if (arr[i] - arr[i - 1] - 1 > 0) { stamps.push_back(arr[i] - arr[i - 1] - 1); } } if (arr[m - 1] < n) { stamps.push_back(n - arr[m - 1]); } if (stamps.size() == 0) { write(0); return; } sort(stamps.begin(), stamps.end()); int ans = 0, w = stamps[0]; for (int i = 0; i < stamps.size(); i++) { if (stamps[i] % w == 0) { ans += stamps[i] / w; } else { ans += ((stamps[i] / w) + 1); } } write(ans); } signed 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 tc = 1; // read(tc); for (int curr = 1; curr <= tc; curr++) solve(curr); return 0; }
// #include "bits/stdc++.h" using namespace std; #define pr1(a) for(auto b : a) cout << b << " ";cout << endl; #define pr2(a) for(auto b : a){for(auto c:b) cout << c << " ";cout << endl;} #define prc(...) for(auto b:{ __VA_ARGS__ }) cout << b << " ";cout << endl; const int NN=200000; vector<vector<int>> g(NN+1,vector<int>()); vector<bool> visited(NN+1,false); long long ans=0; void dfs(int p){ for(auto e:g[p]){ if (visited[e]==false){ ans++; visited[e]=true; dfs(e); } } } void solve() { fill(g.begin(),g.end(),vector<int>()); fill(visited.begin(),visited.end(),false); ans=0; int a,b,n,val; cin >> n ; vector<int> d(n); for(int &i:d) cin>>i; for(int i=0;i<n/2;i++){ int v1=d[i], v2=d[n-i-1]; if (v1!=v2){ g[v1].push_back(v2); g[v2].push_back(v1); } } for(int i=0;i<=NN;i++){ if (visited[i]==false) visited[i]=true; dfs(i); } cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE stringstream in(R"(3 8 1 5 3 2 5 2 3 1 7 1 2 3 4 1 2 3 1 200000 )"); //ans 2 1 0 std::cin.rdbuf(in.rdbuf()); #endif int tn = 1; //cin >> tn; while (tn--) solve(); }
#include<bits/stdc++.h> using namespace std; #define ll long long ll mod = 1e9+7; ll max(ll a,ll b){ if(a>b){ return a; } return b; } ll min(ll a,ll b){ if(a<b){ return a; } return b; } void solve(){ string s; cin >> s; ll n = s.size(); map<char,ll> mp; ll ans=0; mp[s[n-1]]++; for(int i=n-2;i>=1;i--){ if(s[i]==s[i-1]&&s[i-1]!=s[i+1]){ ans+=(n-i-mp[s[i]]-1); mp.clear(); mp[s[i]]=(n-i); } else{ mp[s[i]]++; } } cout << ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i ++ ) #define N 100010 struct Node { string s; int x; bool operator< (const Node &W) const{ return x > W.x; } }a[N]; int main(void) { int n; cin >> n; rep(i, n) cin >> a[i].s >> a[i].x; sort(a, a + n); cout << a[1].s << endl; return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; // #define int long long template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; } 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; } #define REP(i, n) for (int i = 0; i < (n); ++i) #define RREP(i, n) for (int i = (n); i >= 0; --i) #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define RFOR(i, a, b) for (int i = (a); i >= (b); --i) #define ALL(v) (v).begin(), (v).end() #define RALL(a) (a).rbegin(), (a).rend() #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; typedef vector<int> vi; typedef vector<string> vs; typedef vector<vi> vvi; typedef pair<int, int> pii; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<pair<int, string>> ts(n); REP(i, n) { string tmp_s; int tmp_i; cin >> tmp_s >> tmp_i; ts[i] = make_pair(tmp_i, tmp_s); } sort(ALL(ts), greater<>()); cout << ts[1].second << endl; return 0; }
#include <bits/stdc++.h> #ifdef LOCAL #include "../../Library/DebugTool.cpp" #endif using namespace std; #define ll long long #define pii pair<int,int> //const ll MOD = 1000000007; const ll MOD = 998244353; ll INF_int = 1e9+11;ll INF_ll = 1e18 + 13; #define rep(i,n) for(int i = 0;i<n;i++) #define rep1(i,n) for(int i = 1;i<n;i++) #define per(i,n) for(int i = n-1;i>=0;i--) #define mp make_pair #define All(a) a.begin(),a.end() void in(){} template<typename Head,typename ...Tail>void in(Head&& head,Tail&& ...tail){cin >> head;in(std::forward<Tail>(tail)...);} template<typename T>void vecin(vector<T> &vec){int n = vec.size();rep(i,n) cin >> vec[i];} void out(){cout << "\n";} template<typename Head,typename ...Tail>void out(Head&& head,Tail&& ...tail){cout << head << " ";out(std::forward<Tail>(tail)...);} template<typename T>void vecout(vector<T> a){for(auto x:a)cout << x << " ";cout << endl;} template<typename T>bool umax(T &x,const T &y){if(x<y){x=y;return true;}return false;} template<typename T>bool umin(T &x,const T &y){if(x>y){x=y;return true;}return false;} int main(){ ll n,m;in(n,m); ll t = 1; ll tmp = 10; while(n>0){ if(n%2==1){ t*=tmp;t%=(m*m); } tmp*=tmp;tmp%=(m*m); n/=2; } out(t/m); }
#include<bits/stdc++.h> using namespace std; #define lli long long int #define ulli unsigned long long int #define vi vector<int> #define ii pair<int,int> #define all(v) v.begin(),v.end() #define getunique(v) {v.erase(unique(v.begin(),v.end()),v.end());} #define pb push_back #define ff first #define ss second #define endl "\n" #define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL) const int MS=1000001; const lli INF=1e9+7; template<typename T> bool comp(T a, T b){ if(a<b) return true; return false; } //vector<int> t; int n; void print(vi v){ cout<<v.size()<<" "; for(int i=0; i<v.size(); i++){ cout<<v[i]+1<<" "; } cout<<endl; } int main(){ //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); fast_io; cin>>n; vector<lli> v(n); for(int i=0; i<n; i++) cin>>v[i]; n=min(n,8); int mask=(1<<n); vector<vector<int> > t(200,vi(0)); for(int i=0; i<mask; i++){ lli sum=0; vi mine; for(int j=0; j<n; j++){ int her=(1<<j); if(her&i) sum=(sum+v[j])%200,mine.pb(j); } if(t[sum].size()>0){ cout<<"yes"<<endl; print(t[sum]); print(mine); return 0; } t[sum]=mine; } cout<<"no"; }
#include <bits/stdc++.h> using namespace std; int main(){ int n,a,b; cin >> n >> a >> b; cout << n - a + b << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (int)n ;i++) #define rept(i,k,n) for(int i = (int)(k);i < (int)(n);i++) #define all(vec) vec.begin(),vec.end() #define ll int64_t int main(){ ll a,b,c; cin >> a >> b >> c; ll k,t; int p2,p4; if(b%2 == 0){ p2 = 0; if(c >1){ p4 = 0; } else{ if(b%4 == 0){ p4 = 0; } else{ p4 =2; } } } else if(b %4 == 1){ p2 = 1; p4 = 1; } else{ p2 = 1; if(c%2 == 0){ p4 = 1; } else{ p4 = 3; } } k = a%10; if(k == 0){ t = 0; } if(k == 1){ t =1; } if(k == 5){ t = 5; } else if(k == 6){ t = 6; } else if(k == 4){ if(p2){ t = 4; } else{ t = 6; } } else if(k == 9){ if(p2){ t = 9; } else{ t = 1; } } else if(k == 2){ if(p4 == 0){ t = 6; } else if(p2 == 0){ t = 4; } else if(p4 == 1){ t = 2; } else{ t = 8; } } else if(k == 3){ if(p4 == 0){ t = 1; } else if(p2 == 0){ t = 9; } else if(p4 == 1){ t = 3; } else{ t = 7; } } else if(k == 7){ if(p4 == 0){ t = 1; } else if(p2 == 0){ t = 9; } else if(p4 == 1){ t = 7; } else{ t = 3; } } else if(k == 8){ if(p4 == 0){ t = 6; } else if(p2 == 0){ t = 4; } else if(p4 == 1){ t = 8; } else{ t = 2; } } cout << t << endl; }
#include <bits/stdc++.h> #define ll long long int #define db double #define pb push_back #define mpr make_pair #define andl "\n" #define f first #define s second #define mset(x,y) memset(x,y,sizeof(x)) #define fr(i,n) for(long long int i=0;i<n;i++) #define trace(it,x) for(auto it = (x).begin(); it != (x).end(); it++) #define mod 1000000007 #define fastio ios::sync_with_stdio (false); cin.tie (0); cout.tie (0); #define runtime cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ; using namespace std; // convert number to string to_string(x); // convert string to number stoi(); // ll mulmod(ll x,ll y) // { // return ((x%mod)*(y%mod))%mod; // } // ll binpow(ll x, ll y) // { // ll z = 1; // while(y > 0) // { // if(y % 2 == 1) // z = mulmod(z, x); // x = mulmod(x, x); // y /= 2; // } // return z; // } // ll ncr(ll n,ll r) // { // return mulmod(fact[n],binpow(mulmod(fact[n-r],fact[r]),mod-2)); // } // ll pwr(ll n,ll m) // { // if(n==1 || m==0) // return 1; // ll x=pwr(n,m/2); // if(m%2==0) // return x*x; // else // return n*x*x; // } // ll pwr(ll n,ll m,ll b) // { // if(n==1 || m==0) // return 1; // ll x=pwr(n,m/2,b); // if(m%2==0) // return ((x%b)*(x%b))%b; // else // return ((n%b)*(((x%b)*(x%b))%b))%b; // } vector<pair<ll,ll>>cd,ball; ll ans(vector<ll>v,ll i,ll k) { if(i==k) { ll anss=0; ll ab[101]={0}; fr(i,v.size()) ab[v[i]]=1; fr(i,cd.size()) if(ab[cd[i].f]+ab[cd[i].s]==2) anss++; return anss; } ll x,y; v.pb(ball[i].f); x=ans(v,i+1,k); v.pop_back(); v.pb(ball[i].s); y=ans(v,i+1,k); return max(x,y); } void solve() { ll n,m,x,y; cin >> n >> m; fr(i,m) { cin >> x >> y; cd.pb({x,y}); } ll k; cin >> k; vector<ll>v; fr(i,k) { cin >> x >> y; ball.pb({x,y}); } ll i=0; cout << ans(v,i,k); return ; } int main() { fastio ll t=1; // cin >> t; while(t--) solve(); runtime }
#include<iostream> #include<algorithm> #include<string> #include<iomanip> #include<cmath> #include<vector> #include<deque> #include<queue> #include<map> #include<set> #include<list> #define ll long long #define str string #define ld long double #define vec vector #define vll vec<ll> #define vvll vec<vll> #define vbo vec<bool> #define vvbo vec<vbo> #define vin(a) rep(i,0,a.size())cin>>a[i]; #define rep(i,a,b) for(ll i=(a);i<(b);i++) #define rrep(i,b,a) for(ll i=(b);i>=(a);i--) #define repset(itr,a) for(auto itr=a.begin();itr!=a.end();itr++) #define repnck(bit,n,k) for(ll bit=(1ll<<(k))-1;bit<(1ll<<(n));bit=(bit==0?(1ll<<(n)):nec(bit))) #define bi1(bit,i) ((bit)&1ll<<(i)?true:false) #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define kai "\n" #define prque priority_queue #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define mie min_element #define mae max_element #define tos to_string #define sep setprecision #define lob lower_bound #define upb upper_bound #define bis binary_search #define nep next_permutation #define MOD 1000000007ll #define MAX 2147483647ll #define MIN 0.0000000001l #define equal(a,b) (abs((a)-(b))<MIN) using namespace std; void solve(); struct ll2{ll x,y;bool operator<(const ll2 &l)const{return x!=l.x?x<l.x:y<l.y;};}; struct ll3{ll x,y,z;bool operator<(const ll3 &l)const{if(x!=l.x)return x<l.x;if(y!=l.y)return y<l.y;return z<l.z;};}; class ufset{ public: ufset(ll n) {rank.resize(n,0);rep(i,0,n)p.pub(i);} bool same(ll i,ll j){return find(i)==find(j);} void unite(ll i,ll j){link(find(i),find(j));} ll find(ll i){if(i!=p[i])p[i]=find(p[i]);return p[i];} private: vll p,rank; void link(ll i,ll j){if(rank[i]>rank[j])p[j]=i;else{p[i]=j;if(rank[i]==rank[j])rank[j]++;}} }; class nck{ public: nck(ll maxn,ll mod=MOD){ NUM=maxn+1;p=mod; fac.resize(NUM);finv.resize(NUM);inv.resize(NUM); fac[0]=fac[1]=finv[0]=finv[1]=inv[1]=1; rep(i,2,NUM){fac[i]=fac[i-1]*i%p;inv[i]=p-inv[p%i]*(p/i)%p;finv[i]=finv[i-1]*inv[i]%p;} } ll c(ll n,ll k){return fac[n]*(finv[k]*finv[n-k]%p)%p;} private: ll NUM,p; vll fac,finv,inv; }; ll gcd(vector<ll> a){rep(i,1,a.size()){ll k=a[i-1]%a[i];while(k!=0){a[i-1]=a[i];a[i]=k;k=a[i-1]%a[i];}}return a.back();} ll lcm(vector<ll> a){ll n;rep(i,1,a.size()){n=a[i-1]*a[i];ll k=a[i-1]%a[i];while(k!=0){a[i-1]=a[i];a[i]=k;k=a[i-1]%a[i];}n/=a[i];a[i]=n;}return n;} ll pow2(ll a,ll b,ll p=MOD){if(b==-1)return pow2(a,p-2,p);ll res=1;if(b>0){res=pow2(a,b/2,p);if(b%2==0)res=res*res%p;else res=res*res%p*a%p;}return res;} ll nec(ll bit){ll x=bit&-bit,y=bit+x;return (((bit&~y)/x)>>1)|y;} ll bisz(ll bit){ll size=0;while(bit>0){bit/=2;size++;}return size;} ll ransu(){static unsigned int TX=123456789, TY=362436069, TZ=521288629, TW=88675123;unsigned int tt=(TX^(TX<<11));TX=TY;TY=TZ;TZ=TW;return (TW=(TW^(TW>>19))^(tt^(tt>>8)));} int main(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed; solve(); } ll n; ll f(ll r,ll l){ ll m=(l+r+1)/2; if(r==l)return r; if(m<=MAX&&2*n+2>=(1+m)*m)return f(m,l); else return f(r,m-1); } void solve(){ cin>>n; ll k=f(1,n); cout<<n-k+1<<kai; }
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair using namespace std; template <typename T1, typename T2> bool mini(T1 &a, T2 b) { if (a > b) {a = b; return true;} return false; } template <typename T1, typename T2> bool maxi(T1 &a, T2 b) { if (a < b) {a = b; return true;} return false; } const int N = 1e5 + 5; const int oo = 1e9; const int mod = 1e9 + 7; void add(int &a, int b) { a += b; if (a >= mod) a -= mod; } struct mat { int n; vector <vector <int>> f; mat(int _n) { n = _n; f.assign(n, vector <int> (n, 0)); } mat operator * (const mat &m) { mat res(n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) add(res.f[i][k], (long long) f[i][j] * m.f[j][k] % mod); return res; } mat one() { mat res(n); for (int i = 0; i < n; i++) res.f[i][i] = 1; return res; } mat binpow(int k) { mat n = (*this); mat res = one(); for (; k; k >>= 1, n = n * n) if (k & 1) res = res * n; return res; } void print() { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cerr << f[i][j] << " \n"[j == n]; } }; int deg[N]; int n,m,k; int binpow(int x, int k) { int res = 1; for (; k; k >>= 1, x = (long long) x * x % mod) if (k & 1) res = (long long) res * x % mod; return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; mat base(n); for (int i = 0; i < n; i++) cin >> base.f[0][i]; mat mul(n); int dv = binpow(2 * m, mod - 2); for (int i = 1; i <= m; i++) { int u,v; cin >> u >> v; u--; v--; deg[u]++; deg[v]++; mul.f[u][v] = mul.f[v][u] = dv; } for (int i = 0; i < n; i++) { mul.f[i][i] = (1 - (long long) deg[i] * dv) % mod; add(mul.f[i][i], mod); } base = base * mul.binpow(k); for (int i = 0; i < n; i++) cout << base.f[0][i] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep( i, a, b ) for( int i = (a) ; i <= (b) ; i ++ ) #define per( i, a, b ) for( int i = (a) ; i >= (b) ; i -- ) typedef long long LL; const int mod = 998244353; const int MAXN = 105, MAXS = 1e4 + 5; template<typename _T> void read( _T &x ) { x = 0; char s = getchar(); int f = 1; while( s < '0' || '9' < s ) { f = 1; if( s == '-' ) f = -1; s = getchar(); } while( '0' <= s && s <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar(); } x *= f; } template<typename _T> void write( _T x ) { if( x < 0 ) putchar( '-' ), x = -x; if( 9 < x ) write( x / 10 ); putchar( x % 10 + '0' ); } template<typename _T> inline _T MAX( const _T a, const _T b ) { return a > b ? a : b; } template<typename _T> inline _T MIN( const _T a, const _T b ) { return a < b ? a : b; } template<typename _T> inline _T ABS( const _T a ) { return a < 0 ? -a : a; } int dp[2][MAXN][MAXS << 1]; int fac[MAXN]; int W[MAXN]; int N; inline int Mul( int x, int v ) { return 1ll * x * v % mod; } inline int Sub( int x, int v ) { return ( x -= v ) < 0 ? x + mod : x; } inline int Add( int x, int v ) { return ( x += v ) >= mod ? x - mod : x; } inline void Upt( int &x, const int v ) { x = Add( x, v ); } int main() { read( N ); int su = 0; rep( i, 1, N ) read( W[i] ), su += W[i]; fac[0] = 1; rep( i, 1, N ) fac[i] = Mul( fac[i - 1], i ); int pre = 1, nxt = 0; dp[nxt][0][su] = 1; rep( i, 0, N - 1 ) { pre ^= 1, nxt ^= 1; rep( j, 0, N ) rep( k, 0, su << 1 ) dp[nxt][j][k] = 0; rep( j, 0, i ) rep( k, 0, su << 1 ) if( dp[pre][j][k] ) { Upt( dp[nxt][j][k - W[i + 1]], dp[pre][j][k] ); Upt( dp[nxt][j + 1][k + W[i + 1]], dp[pre][j][k] ); } } int ans = 0; rep( i, 1, N ) ans = Add( ans, Mul( dp[nxt][i][su], Mul( fac[i], fac[N - i] ) ) ); write( ans ), putchar( '\n' ); return 0; }
#include<bits/stdc++.h> using namespace std; #define make_it_fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define mp make_pair #define eb emplace_back #define pb push_back #define pob pop_back #define all(x) (x).begin(),(x).end() #define allr(x) (x).rbegin(),(x).rend() #define ll long long #define ld long double #define endl "\n" #define ff first #define ss second #define imn (ll)-1e16 #define imx (ll)1e16 ld pi=3.14159265358979323846; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) 20 #endif ll mod=1e9+7; ll power(ll a,ll b,ll m) { a%=m; if(b==1) return a; if(b==0) return 1LL; ll ret=power(a,b/2,m); ret=(ret*ret)%m; if(b&1) ret=(ret*a)%m; return ret; } ll power(ll a,ll b) { if(b==1) return a; if(b==0) return 1LL; ll ret=power(a,b/2); ret=(ret*ret); if(b&1) ret=(ret*a); return ret; } ll lcm(ll a,ll b) { return (a*b)/(__gcd(a,b)); } struct DSU { ll connected; vector<ll> par, sz; void init(ll n) { par = sz = vector<ll> (n + 1, 0); for(ll i = 1; i <= n; i++) par[i] = i, sz[i] = 1; connected = n; } ll getPar(ll u) { while(u != par[u]) { par[u] = par[par[u]]; u = par[u]; } return u; } ll getSize(ll u) { return sz[getPar(u)]; } void unite(ll u, ll v) { ll par1 = getPar(u), par2 = getPar(v); if(par1 == par2) return; connected--; if(sz[par1] > sz[par2]) swap(par1, par2); sz[par2] += sz[par1]; sz[par1] = 0; par[par1] = par[par2]; } }; void answer_nikaal() { ll n,i; cin>>n; ll a[n+1]; for(i=1;i<=n;i++){ cin>>a[i]; } DSU d; d.init(200005); for(i=1;i<=n/2;i++){ if(a[i]!=a[n-i+1]){ d.unite(a[i],a[n-i+1]); } } ll ans=0; for(i=1;i<200005;i++){ ans+=max(0LL,d.sz[i]-1); } cout<<ans<<endl; } int main() { make_it_fast; int TEST_CASES=1; // cin>>TEST_CASES; while(TEST_CASES--) { answer_nikaal(); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) #include<iostream> #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<queue> #include<cmath> #include<cstring> #include<bitset> #include<stack> #include<time.h> #define X first #define Y second #define PB push_back #define MP make_pair #define scd(a) scanf("%d",&a) #define scdd(a,b) scanf("%d%d",&a,&b) #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c) #define ALL(x) x.begin(),x.end() #define sz(a) ((int)a.size()) #define getmid ((l+r)>>1) #define mst(var,val) memset(var,val,sizeof(var)) #define IOS ios::sync_with_stdio(false);cin.tie(0) #define lowbit(x) x&(-x) #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<=n;++i) #define ls rt<<1 #define rs rt<<1|1 using namespace std; #ifdef local #define dbg(args...) cout << #args << " -> ", err(args); void err(){ cout << endl; } template<typename T, typename... Args> void err(T a, Args... args){ cout << a << ' '; err(args...); } #else #define dbg(args...) #endif // local typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef pair <int, ll> pil; typedef pair <double, double> pdd; const int inf=0x3f3f3f3f; const long long INF=0x3f3f3f3f3f3f3f3fLL; const double PI=acos(-1.0); const long double eps=1e-8; const int mod=1e9+7; const int maxn=2e5+100; const int N=1e6+100; const int M=(1<<20)+10; const ll mm=(1LL<<32); template <class T> inline void read(T &x) { x = 0; char c = getchar(); bool f = 0; for (; !isdigit(c); c = getchar()) f ^= c == '-'; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); x = f ? -x : x; } template <class T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } T y = 1; int len = 1; for (; y <= x / 10; y *= 10) ++len; for (; len; --len, x %= y, y /= 10) putchar(x / y + 48); } ll qpow(ll a,ll b,ll mod) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%mod; b>>=1; a=(a*a)%mod; } return ans; } int main() { #ifdef local freopen("in.txt","r",stdin); #endif // local IOS;cout.tie(0); int T;cin>>T; while(T--) { map<int,int>mp; int n;cin>>n; rep1(i,n) { int x;cin>>x;mp[x]++; } if(n&1) cout<<"Second\n"; else{ int flag=0; for(pii v:mp) if(v.Y&1) flag=1; if(flag) cout<<"First\n"; else cout<<"Second\n"; } } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mp make_pair #define F first #define S second #define T third typedef vector<int> vi; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<pair<int,int>> vp; typedef vector<long long> vll; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvll; const int C=1e9+7; void yes(){ cout<<"YES\n"; } void no(){ cout<<"NO\n"; } ll binpow(ll a, ll b, ll m=C) { a %= m; ll res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } //Variables******************************************** //Functions******************************************** //***************************************************** void solve(){ string s; cin>>s; int co{},cx{},cq{}; for(int i=0;i<10;i++){ if(s[i]=='o') co++; else if(s[i]=='x') cx++; else if(s[i]=='?') cq++; } if(co+cq==0||co>4){ cout<<0<<endl; return; } int f=co+cq; int T=f*f*f*f; int x{}; int prevf=1; int p=1; for(int i=1;i<=co;i++){ x+=p*(prevf*(co-i+1)/i)*(f-i)*(f-i)*(f-i)*(f-i); prevf=prevf*(co-i+1)/i; p*=-1; } cout<< T-x<<endl; } signed main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1;// cin>>t; int x{}; while(t--){ //cout<<"Case #"<<x<<": "; x++; solve(); } }
#include <bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;++i) #define fod(i,a,b) for(int i=a;i>=b;--i) #define efo(i,q) for(int i=0;i<(int)B[q].size();++i) using namespace std; typedef long long LL; const int N=200500,mo=998244353; int read(int &n) { bool q=0;n=0;char ch=' '; for(;ch!='-'&&(ch<'0'||ch>'9');ch=getchar()); if(ch=='-')ch=getchar(),q=1; for(;ch>='0'&&ch<='9';ch=getchar())n=(n<<3)+(n<<1)+ch-48; return q?n=-n:n; } int n,m,ans; vector<int>B[N]; void link(int q,int w) { B[q].push_back(w); B[w].push_back(q); } bool z[N]; void dfs(int q) { z[q]=1; efo(i,q)if(!z[B[q][i]])dfs(B[q][i]); } int main() { int q,w,_; read(n); fo(i,1,n) { read(q); link(q,i); } fo(i,1,n)if(!z[i]) { ++ans; dfs(i); } LL t=1; fo(i,1,ans)t=(t<<1)%mo; printf("%lld\n",t-1); return 0; }
#include<iostream> #include<algorithm> #include<string> #include<vector> #include<cstdlib> #include<queue> #include<set> #include<cstdio> #include<map> #include<cassert> using namespace std; #define ll long long #define reps(i, a, b) for(int i = a; i < b; i++) #define rreps(i, a, b) for(int i = a-1; i >= b; i--) #define rep(i, n) reps(i, 0, n) #define rrep(i, n) rreps(i, n, 0) #define P pair<int, int> #define vec vector<int> #define mat vector<vec> const ll mod = 1000000007; const int INF = 1001001001; int n, m; int node(int x, int y) { return x * n + y; } P node_inv(int u){ return P(u/n, u%n); } struct edge { int u, v; char c; edge(int u_, int v_, char c_){ u = u_; v = v_; c = c_; } }; int main(){ cin >> n >> m; vector<edge> e; rep(i, m){ int a, b; char c; cin >> a >> b >> c; a--; b--; e.push_back(edge(a, b, c)); } mat g(n*n); rep(i, e.size()){ rep(j, e.size()){ if(e[i].c == e[j].c){ int u1 = node(e[i].u, e[j].u); int v1 = node(e[i].v, e[j].v); int u2 = node(e[i].u, e[j].v); int v2 = node(e[i].v, e[j].u); g[u1].push_back(v1); g[v1].push_back(u1); g[u2].push_back(v2); g[v2].push_back(u2); // cout << u1 << " " << v1 << " " << u2 << " " << v2 << endl; } } } // rep(i, n*n){ // P p = node_inv(i); // cout << p.first << " " << p.second << endl; // rep(j, g[i].size()){ // cout << g[i][j] << " "; // }cout << endl; // cout << endl; // } // 奇数距離 queue<P> que; que.push(P(node(0, n-1), 0)); vec dist(n*n, INF); dist[node(0, n-1)] = 0; int ans = INF; while(que.size() > 0){ int u = que.front().first; int d = que.front().second; que.pop(); rep(i, g[u].size()){ int v = g[u][i]; if(dist[v] <= d+1) continue; dist[v] = d+1; que.push(P(v, d+1)); P p = node_inv(v); if(p.first == p.second){ ans = min(ans, (d+1) * 2); } } // cout << node_inv(u).first+1 << " " << node_inv(u).second+1 << " " << d << endl; } // 偶数距離 rep(i, e.size()){ ans = min(ans, dist[node(e[i].u, e[i].v)]*2+1); ans = min(ans, dist[node(e[i].v, e[i].u)]*2+1); } if(ans == INF) ans = -1; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N, ans; string s; cin >> N >> s; ans = N; auto itr = s.begin(); while (distance(itr, s.end()) >= 3) { if (*itr=='f' && *(itr+1)=='o' && *(itr+2)=='x') { ans -= 3; s.erase(itr, itr+3); itr--; itr--; } else itr++; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int i = 0,c = 0; for(i = 0; i < s.size() ; i++){ if(s.substr(i, 4) == "ZONe") c++; } cout << c ; }
/*------------------------------------------ Author : Suyash Chavan Walchand College of Engineering, Sangli --------------------------------------------*/ #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #pragma GCC target ("sse4") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define ll long long #define ld long double #define endl "\n" #define Endl "\n" #define pb push_back #define mp make_pair #define eb emplace_back #define ff first #define ss second #define loop(i,a,b) for(ll i=a;i<b;i++) #define rloop(i,a,b) for(ll i=a;i>=b;i--) #define fill(a,v) memset(a, v, sizeof a) #define all(a) a.begin(), a.end() #define rall(vec) vec.rbegin(),vec.rend() #define sortv(a) sort(a.begin(),a.end()) #define set_bits(a) __builtin_popcount(a) #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> #define fast_map(mp) mp.reserve(1024);\ mp.max_load_factor(0.25) #define fast_io ios_base::sync_with_stdio(false);\ cin.tie(NULL) #define file_in freopen("input.txt", "r", stdin) #define file_out freopen("output.txt", "w", stdout) #define file_io file_in;\ file_out #define PI 3.1415926535897932384626 #define INF 1e18 #define EPS 1e-9 #define change show = !show bool show = true; const int fx[4][2] = {{0, 1}, {0, -1}, {1, 0}, { -1, 0}}; const int fxx[8][2] = {{0, 1}, {0, -1}, {1, 0}, { -1, 0}, {1, 1}, {1, -1}, { -1, 1}, { -1, -1}}; template <typename T1, typename T2> inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template<typename T> inline std::ostream &operator << (std::ostream & os, const std::vector<T>& v) { bool first = true; if (show) os << "["; for (unsigned int i = 0; i < v.size(); i++) { if (!first) { if (show) os << ", "; else os << " "; } os << v[i]; first = false; } if (show) return os << "]\n"; else return os << "\n"; } template<typename T> inline std::ostream &operator << (std::ostream & os, const std::set<T>& v) { bool first = true; os << "["; for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if (!first) os << ", "; os << *ii; first = false; } return os << "]"; } template<typename T1, typename T2> inline std::ostream &operator << (std::ostream & os, const std::map<T1, T2>& v) { bool first = true; os << "["; for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) { if (!first) os << ", "; os << *ii ; first = false; } return os << "]"; } /*--------------------------- CODE STARTS FROM HERE---------------------------*/ int solve() { string s; cin >> s; ll ans = 0; loop(i, 0, s.size() - 3) { if (s[i] == 'Z' && s[i + 1] == 'O' && s[i + 2] == 'N' && s[i + 3] == 'e') ans++; } cout << ans << endl; return 0; } int main() { fast_io; ll t = 1; // cin >> t; while (t--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL); #define int long long #define lld long double #define inf 1000000000 #define pii pair<int,int> #define fi first #define se second #define pb push_back #define mod 1000000007 #define mod2 998244353 #define all(x) (x).begin(), (x).end() #define rep(i,x,y) for(int i=x; i<y; i++) #define fill(a,b) memset(a, b, sizeof(a)) #define vi vector<int> #define setbits(x) __builtin_popcountll(x) #define w(x) int x; cin>>x; while(x--) #define in(a) for(auto &x: a)cin>>x; int h,w,a,b,cnt=0; pair<int,int> nxt(int i,int j,vector<vector<bool>> &mat){ int I=-1,J=-1; for(int I=i;I<h;I++){ for(int J=0;J<w;J++){ if(I==i && J<=j)continue; if(mat[I][J])continue; return {I,J}; } } return {-1,-1}; } void fun(int i,int j,vector<vector<bool>> &mat,int x,int y){ if(x==0 && y==0){cnt++;return;} if(j+1<w && mat[i][j+1]==0 && x){ mat[i][j]=mat[i][j+1]=1; pii nx=nxt(i,j,mat); fun(nx.fi,nx.se,mat,x-1,y); mat[i][j]=mat[i][j+1]=0; } if(i+1<h && mat[i+1][j]==0 && x){ mat[i][j]=mat[i+1][j]=1; pii nx=nxt(i,j,mat); fun(nx.fi,nx.se,mat,x-1,y); mat[i][j]=mat[i+1][j]=0; } if(y){ mat[i][j]=1; pii nx=nxt(i,j,mat); fun(nx.fi,nx.se,mat,x,y-1); mat[i][j]=0; } } signed main(){ cin>>h>>w>>a>>b; vector<vector<bool>> mat(h,vector<bool>(w,false)); fun(0,0,mat,a,b); cout<<cnt; return 0; }
#include<bits/stdc++.h> #include<chrono> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; using namespace __gnu_pbds; #define ll long long int #define ull unsigned long long int #define FOR(I,a,b) for(int I=a;I<b;I++) #define FORit(it,a) for(auto it=a.begin();it!=a.end();it++) #define ROF(I,a,b) for(int I=a;I>=b;I--) #define vec vector #define vi vec<int> #define vll vec<ll> #define pb push_back #define pp pop_back #define all(x) x.begin(),x.end() #define testcases ll t;cin>>t;while(t--) #define mem(a,k) memset(a,k,sizeof(a)) #define FF first #define SS second #define MP(x,y) make_pair(x,y) #define rt return #define br break #define ct continue #define elif else if #define ii pair<int,int> #define vecin(a,n,index) for(int I=index;I<n;I++)cin>>a[I] #define vecout(a,n,index) for(int I=index;I<n;I++)cout<<a[I]<<" ";cout<<endl; //ll mod = 1000000007; template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template<class T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>; void solve() { int n; cin >> n; vll v(n); vecin(v, n, 0); vll p = v; FOR(i, 1, n)p[i] += p[i - 1]; ll maxe = 0, ans = 0, cur = 0; FOR(i, 0, n) { maxe = max(maxe, p[i]); ans = max(ans, maxe + cur); cur += p[i]; } cout << ans << endl; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // START FROM HERE :) solve(); }
#include<bits/stdc++.h> using namespace std; using lli = long long int; #define pb push_back #define eb emplace_back #define fi first #define se second const int MOD = 1000000007; const int MOD1 = 998244353; const int maxn = 100010; const int lim = (int)1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int h = 0, w = 0, x = 0, y = 0; cin >> h >> w >> x >> y; --x; --y; vector<string> s(h); for (auto &i : s) cin >> i; int res = 0; for (int i = y; i < w && s[x][i] != '#'; ++i) ++res; for (int i = y; i >= 0 && s[x][i] != '#'; --i) ++res; for (int i = x; i < h && s[i][y] != '#'; ++i) ++res; for (int i = x; i >= 0 && s[i][y] != '#'; --i) ++res; cout << res - 3; }
#include<bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ll long long #define db double #define el "\n" #define ld long double #define rep(i,n) for(int i=0;i<n;i++) #define rev(i,n) for(int i=n;i>=0;i--) #define rep_a(i,a,n) for(int i=a;i<n;i++) #define all(ds) ds.begin(), ds.end() #define ff first #define ss second #define pb push_back #define mp make_pair typedef vector< long long > vi; typedef pair<long long, long long> ii; typedef priority_queue <ll> pq; #define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> #define graph vector<vi> const ll mod = 1000000007; const ll INF = (ll)1e18; const ll MAXN = 1000006; ll po(ll x, ll n ){ ll ans=1; while(n>0){ if(n&1) ans=(ans*x)%mod; x=(x*x)%mod; n/=2; } return ans; } vi par(200005); vector<map<ll, ll> > v; ll getP(ll s){ if(par[s]==s) return s; else return (par[s]=getP(par[s])); } void unite(ll x, ll y){ ll a=getP(x); ll b=getP(y); if(a==b) return; if(v[a].size()<v[b].size()) swap(a,b); par[b]=a; for(auto h:v[b]){ if(v[a].find(h.ff)==v[a].end()) v[a].insert(mp(h.ff,0ll)); v[a][h.ff]+=h.ss; } v[b].clear(); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T=1; //cin >> T; while(T--){ ll n,q; cin>>n>>q; ll c[n+1]; rep(i,n) cin>>c[i+1]; rep(i,200005) par[i]=i; v.assign(n+1, map<ll,ll>()); rep_a(i,1,n+1) v[i].insert(mp(c[i],1ll)); ll t,x,y; rep(i,q){ cin>>t>>x>>y; if(t==1) unite(x,y); else{ t = getP(x); if(v[t].find(y)==v[t].end()) cout<<0<<el; else cout<<v[t][y]<<el; } } } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <numeric> #include <utility> #include <tuple> #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false) template<typename T> void PPPPP(T t) { cerr << t; } template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); } #else #define dump(...) do{ } while(false) #endif 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 T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; } template<typename T> void print(T a) { cout << a << '\n'; } template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin, n; vector<ll> a(n), b(n); rep(i, 0, n) { cin, a[i]; } rep(i, 0, n) { cin, b[i]; } ll max1 = 0, max2 = 0; rep(i, 0, n) { chmax(max1, a[i]); chmax(max2, max1 * b[i]); print(max2); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; ll I=167167167167167167; ll Q=1e9+7; int main() { ll N; cin>>N; vector<ll> a(N),b(N); for(int i=0;i<N;i++) cin>>a[i]; for(int i=0;i<N;i++) cin>>b[i]; ll A=0,B=0; for(int i=0;i<N;i++){ A=max(A,a[i]); b[i]*=A; B=max(B,b[i]); cout<<B<<endl; } }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < int(n); i++) #define FOR(i, a, b) for (int i = a; i < int(b); i++) #define FOREACH(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); i++) #define SIZE(v) ((int)(v).size()) #define pb push_back #define mp make_pair #define st first #define nd second #define ll long long #define llint long long #define pii pair<int, int> #define UNVISITED -1 template<typename T> string to_string(const vector<T>& vc, int w) { if(vc.empty()) return ""; if(w + 1 == vc.size()) return to_string(vc[w]); return to_string(vc[w]) + "," + to_string(vc, w + 1); } template<typename T> string to_string(const vector<T>& vc) { return "{" + to_string(vc, 0) + "}"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef DEBUG #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif class DebugStream {}LOG; template <typename T>DebugStream &operator<<(DebugStream &s, const T&) { return s; } #ifdef DEBUG #define LOG clog #endif const int MAX = 1001000; int a[MAX]; int main() { int T; scanf("%d", &T); REP(t, T) { int N; scanf("%d", &N); REP(i, N) scanf("%d", a + i); if(N%2 == 1) { printf("Second\n"); } else { map<int, int> dic; REP(i, N) dic[a[i]]++; bool q = true; for(auto x : dic) if(x.second%2 == 1) q = false; if(q) printf("Second\n"); else { printf("First\n"); } } } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;} const char newl = '\n'; int main() { int q; cin >> q; while (q-- ) { int n; cin >> n; vector<int> a(n); for (int i = 0;i < n;++i) cin >> a[i]; bool fi = false; if (n%2 == 0) { sort(a.begin(),a.end()); for (int i = 0;i < n/2;++i) if (a[i*2] != a[i*2+1]) { fi = true; break; } } cout << (fi ? "First" : "Second") << newl; } }
#include<bits/stdc++.h> using namespace std; #define ll long long const ll Mod=1e9+7; char a[2005][2005]; ll dp[2005][2005][4]; int n,m; int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ cin>>(a[i]+1); } dp[1][1][0]=dp[1][1][1]=dp[1][1][2]=dp[1][1][3]=1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(i==1&&j==1||a[i][j]=='#') continue; dp[i][j][0]=(dp[i-1][j][1]+dp[i][j-1][2]+dp[i-1][j-1][3])%Mod; dp[i][j][1]=(dp[i-1][j][1]+dp[i][j][0])%Mod; dp[i][j][2]=(dp[i][j-1][2]+dp[i][j][0])%Mod; dp[i][j][3]=(dp[i-1][j-1][3]+ dp[i][j][0])%Mod; } } printf("%lld\n",dp[n][m][0]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) ll ans = 0; const ll mo = 1000000007; auto mod = [](ll modx) {modx%=mo;modx+=mo;modx%=mo;return modx; }; int main() { ll h , w ; cin >> h >> w ; vector<vector<ll>> v(h, vector<ll>(w,0) ); vector<vector<ll>> vv(h, vector<ll>(w,0) ); vector<ll> vh(w,0); vector<ll> vw(h,0); vector<ll> vhw(h+w+1,0); rep(i,h)rep(j,w){ char a; cin >> a ; if(a=='#')v[i][j]=1; } vv[0][0]=1; vw[0]=1; vh[0]=1; vhw[w]=1; rep(i,h)rep(j,w){ if(i==0&&j==0)continue; if(v[i][j]==1){ vw[i]=0; vh[j]=0; vhw[w+i-j]=0; } else{ vv[i][j]=mod(vw[i]+vh[j]+vhw[w+i-j]); vw[i]=mod(vw[i]+vv[i][j]); vh[j]=mod(vh[j]+vv[i][j]); vhw[w+i-j]=mod(vhw[w+i-j]+vv[i][j]); } } cout << vv[h-1][w-1] << endl; return 0; }
#include <iostream> #include <sstream> #include <algorithm> #include <cmath> #include <functional> #include <vector> #include <set> #include <map> #include <queue> #include <stack> using namespace std; #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 srep(i, s, n) for (int i = s; i < (n); ++i) #define rng(a) a.begin(), a.end() #define rrng(a) a.rbegin(), a.rend() #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() using ll = long long; using ull = unsigned long long; using P = pair<int, int>; using T3 = tuple<int, int, int>; template<typename T>void print(vector<T> a) { for (T t : a) { cout << t << " "; } cout << endl; } template<typename T>void print2(T a, T b) { cout << a << " " << b << endl; } const ll LINF = 1LL << 60; const int INF = 1 << 30; const int mod = 1e9 + 7; int main() { ll a, b , c; cin >> a >> b >> c; string ans; if (a >= 0 && b >= 0) { // +, + if (a > b) { ans = ">"; } else if (a < b) { ans = "<"; } else { ans = "="; } } else if (a >= 0) { // +, - b = abs(b); if (c % 2 != 0) { ans = ">"; } // odd else if (a > b) { ans = ">"; } else if (a < b) { ans = "<"; } else { ans = "="; } } else if (b >= 0) { // -, + a = abs(a); if (c % 2 != 0) { ans = "<"; } // odd else if (a > b) { ans = ">"; } else if (a < b) { ans = "<"; } else { ans = "="; } } else { // -. - if (c % 2 != 0) { if (a > b) { ans = ">"; } else if (a < b) { ans = "<"; } else { ans = "="; } } else { if (a > b) { ans = "<"; } else if (a < b) { ans = ">"; } else { ans = "="; } } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using p = pair<ll, ll>; #define rep(i,s,t) for(int i = (int)(s); i < (int)(t); i++) int main(){ ll a,b,c; cin >> a >> b >> c; if(a >= 0 && b >= 0){ if(a > b) puts(">"); else if(a == b) puts("="); else puts("<"); } else if(a < 0 && b >= 0){ if(c%2 == 0){ if(abs(a) > abs(b)){ puts(">"); } else if(abs(a) == abs(b)){ puts("="); } else{ puts("<"); } } else{ puts("<"); } } else if(b < 0 && a >= 0){ if(c%2 == 0){ if(abs(b) > abs(a)){ puts("<"); } else if(abs(a) == abs(b)){ puts("="); } else{ puts(">"); } } else{ puts(">"); } }else{ if(c%2 == 0){ if(abs(a) > abs(b)) puts(">"); else if(a==b) puts("="); else puts("<"); } else{ if(a > b) puts(">"); else if(a == b) puts("="); else puts("<"); } } }
#include <iostream> using namespace std; int main(void){ // Your code here! int t; long long int c; cin>>t; for(int i=0;i<t;i++){ cin>>c; if(c%4==0)cout<<"Even"; else if(c%2==0)cout<<"Same"; else cout<<"Odd"; cout<<endl; } }
//Bismillahir Rahmanir Raheem #include<bits/stdc++.h> using namespace std; typedef pair<int,int>pii; #define sf(x) scanf("%d",&x) #define sfl(x) scanf("%lld",&x) #define lli long long int #define ll64 int64_t #define pb push_back #define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define frr(i,a) for(int i=0;i<a;i++) #define frl(i,a) for(lli i=0;i<a;i++) #define fr1(i,a) for(int i=1;i<=a;i++) #define iter(x) x.begin(),x.end() #define Memset(a,x,n) for(int i=0;i<n;i++)a[i]=x; #define fi first #define si second //lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than ‘val’. //upper_bound returns an iterator pointing to the first element in the range [first,last) which has a value greater than ‘val’. typedef pair<lli,lli>pll; void solve() { lli n; cin>>n; if(n%2==1) { cout<<"Odd"<<endl; return; } lli c=0; while(n%2==0) { n/=2; c++; } if(c==1) { cout<<"Same"<<endl; } else { cout<<"Even"<<endl; } } int main() { int t; cin>>t; while(t--) { solve(); } }
#define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (n); i++) #include <bits/stdc++.h> using namespace std; using ll = long long; ll mod = 1000000007; ll INF = 1001001001; ll max(int a, ll b) { if (a < b) { return b; } else { return a; } } ll min(int a, int b) { return a > b ? a : b; } int main() { ll N; cin >> N; vector<vector<ll>> xyz(N, vector<ll>(3)); rep(i, N) { cin >> xyz[i][0] >> xyz[i][1] >> xyz[i][2]; } vector<vector<ll>> dist(N, vector<ll>(N)); rep(i, N) rep(j, N) { dist[i][j] = abs(xyz[j][0] - xyz[i][0]) + abs(xyz[j][1] - xyz[i][1]) + max(0, xyz[j][2] - xyz[i][2]); } vector<ll> pow2(N + 1, 1); for (int i = 1; i < N + 1; i++) { pow2[i] = pow2[i - 1] * 2; } vector<vector<ll>> dp(pow2[N], vector<ll>(N, INF)); rep(i, N) { dp[1 << i][i] = dist[i][0]; } rep(i, pow2[N]) rep(j, N) { if ((i >> j & 1) != 1) continue; rep(k, N) { if ((i >> k & 1) == 1) continue; dp[i | (1 << k)][k] = min(dp[i | 1 << k][k], dp[i][j] + dist[k][j]); } } cout << dp[pow2[N] - 1][0] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define rep1(i,n) for(ll i=1;i<=(ll)(n);i++) // #define LOCAL 1; #ifdef LOCAL #define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl #else #define dbg(x) true #endif template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) { out << "["; size_t last = v.size() - 1; for (size_t i = 0; i < v.size(); ++i) { out << v[i]; if (i != last) out << ", "; } out << "]"; return out; } template<typename F, typename S> ostream& operator<< (ostream& out, const pair<F,S>& p) { out << "[" << p.first << ", " << p.second << "]"; return out; } template< typename T > struct edge { int src, to; T cost, kbai; edge(int to, T cost, T kbai) : src(-1), to(to), cost(cost), kbai(kbai) {} edge(int src, int to, T cost, T kbai) : src(src), to(to), cost(cost), kbai(kbai) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template< typename T > using Edges = vector< edge< T > >; template< typename T > using WeightedGraph = vector< Edges< T > >; using UnWeightedGraph = vector< vector< int > >; template< typename T > using Matrix = vector< vector< T > >; int n,m,x,y; template< typename T > vector< T > dijkstra(WeightedGraph< T > &g, int s) { const auto INF = numeric_limits< T >::max(); // vector< T > dist_kbai(g.size(), INF); vector< T > dist(g.size(), INF); using Pi = pair< T, int >; priority_queue< Pi, vector< Pi >, greater< Pi > > que; dist[s] = 0; // dist_kbai[s] = 0; que.emplace(dist[s], s); while(!que.empty()) { T cost; int idx; tie(cost, idx) = que.top(); que.pop(); // dbg(idx); // dbg(cost); if(dist[idx] < cost) continue; for(auto &e : g[idx]) { T tcost = cost; tcost += (e.kbai - tcost%e.kbai)%e.kbai; auto next_cost = tcost + e.cost; if(dist[e.to] <= next_cost) continue; dist[e.to] = next_cost; que.emplace(dist[e.to], e.to); } } return dist; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> x >> y; --x;--y; WeightedGraph< ll > g(n); rep(i,m) { ll a, b, t, k; cin >> a >> b >> t >> k; --a;--b; g[a].emplace_back(b, t, k); g[b].emplace_back(a, t, k); } auto dist = dijkstra(g, x); if(dist[y]==numeric_limits<ll>::max()) cout << -1 << endl; else cout << dist[y] << endl; return 0; }
#include <iostream> #include <bits/stdc++.h> using namespace std; int main(){ int n, m; vector<int> t(1001, 0); cin >> n >> m; for(int i=0; i<n; i++){ int tmp; cin >> tmp; t[tmp]++; } for(int i=0; i<m; i++){ int tmp; cin >> tmp; t[tmp]++; } for(int i=1; i<1001; i++){ if(t[i] == 1){ cout << i; if(i == 1000){ cout << endl; }else{ cout << " "; } } } return 0; }
#include <iostream> #include <vector> #include <algorithm> template<typename Container> Container SetAnd(const Container &a, const Container &b){ Container res; std::set_intersection( std::begin(a), std::end(a), std::begin(b), std::end(b), std::inserter(res, std::end(res)) ); return res; } template<typename Container> Container SetOr(const Container &a, const Container &b){ Container res; std::set_union( std::begin(a), std::end(a), std::begin(b), std::end(b), std::inserter(res, std::end(res)) ); return res; } template<typename Container> Container SetXor(const Container &a, const Container &b){ Container res; std::set_symmetric_difference( std::begin(a), std::end(a), std::begin(b), std::end(b), std::inserter(res, std::end(res)) ); return res; } template<typename Container> Container SetDiff(const Container &a, const Container &b){ Container res; std::set_difference( std::begin(a), std::end(a), std::begin(b), std::end(b), std::inserter(res, std::end(res)) ); return res; } int main() { int n, m; std::cin >> n >> m; std::vector<int> a(n), b(m); for(auto &x : a) std::cin >> x; for(auto &x : b) std::cin >> x; auto c = SetXor(a,b); for(auto &x : c) std::cout << x << '\n'; }
#include <bits/stdc++.h> #define lc (o<<1) #define rc ((o<<1)|1) #define PB push_back #define MK make_pair using namespace std; #define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl const int maxn = 5000 + 5; const int modu = 998244353; // 1e9 + 7 const int inf = 0x3f3f3f3f; const double eps = 1e-5; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; typedef long long LL; void read(LL &x) { x=0;int f=0;char ch=getchar(); while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} x=f?-x:x; return; } void read(int &x) { LL y; read(y); x = (int)y; } void read(char &ch) { char s[3]; scanf("%s", s); ch = s[0]; } void read(char *s) { scanf("%s", s); } template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); } int n, m; int a[maxn]; LL inv[maxn], c[maxn], f[20][maxn]; int b[20]; int main() { // freopen("my.txt", "w", stdout); read(n, m); inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (modu - (modu/i))*inv[modu%i]%modu; c[0] = 1; for (int i = 1; i <= n; ++i) c[i] = (n-i+1)*inv[i]%modu*c[i-1]%modu; int nb = 0; while (m) { b[nb++] = m%2; m /= 2; } memset(f, 0, sizeof(f)); f[nb-1][0] = 1; int tot = b[nb-1]; for (int i = nb-2; i >= 0; --i) { for (int j = 0; j <= tot*2 + b[i]; ++j) if (j%2 == 0) { for (int k = 0; k <= j && k <= n; k += 2) f[i][j] = (f[i][j] + f[i+1][(j-k)/2]*c[k]) % modu; } tot = tot*2 + b[i]; } printf("%lld\n", f[0][tot]); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(3) #define int long long #define ri register #define mk make_pair #define fi first #define se second #define pb push_back #define eb emplace_back #define is insert #define es erase using namespace std; const int N = 200010; inline int read() { int s = 0, w = 1; ri char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } int n, a[N], pos[N], book[N]; vector<int> Ans; signed main() { n = read(); for (ri int i = 1; i <= n; i++) a[i] = read(), pos[a[i]] = i; for (ri int i = 1; i <= n; i++) { while (i < pos[i]) { if (book[pos[i]]) return puts("-1") & 0; book[pos[i]] = 1; Ans.eb(pos[i]); int x = a[pos[i]], y = a[pos[i] - 1]; int p = pos[i], q = pos[i] - 1; swap(a[pos[i]], a[pos[i] - 1]); pos[x] = q, pos[y] = p; } } if ((int)Ans.size() != n - 1) return puts("-1") & 0; for (auto i : Ans) printf("%lld\n", i - 1); return 0; }
#include<iostream> #include<cstdio> using namespace std; long long x,y; int n,m; int main() { scanf("%d %d",&n,&m); string temp; for(int a=1;a<=n;a++) { int t=0; cin>>temp; for(int b=0;b<temp.size();b++) t+=temp[b]-'0'; if(t%2==0) x++; else y++; //printf("%d\n",t); } printf("%lld",x*y); return 0; }
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> using namespace std; using namespace __gnu_pbds; #define ld long double #define ull unsigned long long int #define ll long long int #define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define rep(i,L,R) for(ll i=L;i<R;i++) #define f(n) rep(i,0,n) #define vll vector<ll> #define pll pair<ll,ll> #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define PI 3.141592653589 #define DEBUG #ifdef DEBUG #define deb2(args...) {dbg,args; cerr<<endl;} #else #define deb2(args...) #endif struct debb{template<typename T> debb& operator,(const T &v){cerr<<v<<" ";return *this;}}dbg; #define deb1(args...) do {cerr << #args << " : "; dbg,args; cerr << "\n";} while(0) #define printa(a,L,R) rep(i,L,R) cout << a[i] <<' ';cout<<"\n"; typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_set; template<typename T>T gcd(T a,T b){if(b==0)return a;else return gcd(b,a%b);} template<typename T>T lcm(T a,T b){return a/gcd(a,b)*b;} template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;} ll power(ll x,ll y,ll p){ll res=1;x=x%p;while(y>0){if(y&1)res=((res%p)*(x%p))%p;y=y>>1;x=(x*x)%p;}return res;} ll pow1(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x);y=y>>1;x=x*x;}return res;} #define sq(X) ((X)*(X)) int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; #define mod 1000000007ll const ll INF = 1e18l+10, MAX = 3e5+10; void solve() { ll n,j,m,i,c = 0; cin >> n >> m; string s; f(n) { cin >> s; ll t = 0; rep(j,0,m) if(s[j]=='1') t++; if(t%2) c++; } cout << c * (n - c) <<"\n"; } int main() { SPEED int t = 1; // cin >> t; while(t--) { solve(); } }
#include<iostream> using namespace std; int main() { int A, B, C; cin >> A >> B >> C; if (C % 2 == 1) { if (A <= 0 && B > 0) { cout << "<"; } else if (B <= 0 && A > 0) { cout << ">"; } else if (A > 0 && B > 0) { if (A > B) cout << ">"; else if (A < B) cout << "<"; else cout << "="; } else if (A <= 0 && B <= 0) { if (A > B) cout << ">"; else if (A < B) cout << "<"; else cout << "="; } } else { if (A <= 0 && B > 0) { if (A + B > 0)cout << "<"; else if (A + B < 0)cout << ">"; else cout << "="; } else if (B <= 0 && A > 0) { if (A + B > 0)cout << ">"; else if (A + B < 0)cout << "<"; else cout << "="; } else if (A > 0 && B > 0) { if (A > B) cout << ">"; else if (A < B) cout << "<"; else cout << "="; } else if (A <= 0 && B <= 0) { if (A > B) cout << "<"; else if (A < B) cout << ">"; else cout << "="; } } }
/* Author : Drigesh Anuragi */ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define rep(i,n) for(int i=0;i<n;i++) #define rep1(i,n) for(int i=1;i<n;i++) #define repl(i,n) for(ll i=0;i<n;i++) #define repl1(i,n) for(ll i=1;i<n;i++) #define rev(i,n) for(int i=n-1;i>=0;i--) #define revl(i,n) for(ll i=n-1;i>=0;i--) #define repn(i,n) for(int i=0;i<=n;i++) #define repln(i,n) for(ll i=0;i<=n;i++) #define pii pair<int,int> #define pll pair<ll,ll> #define vi vector<int> #define vll vector<ll> #define si set<int> #define sll set<ll> #define mpi map<int,int> #define mpll map<ll,int> #define trav(t) t.begin(),t.end() #define lb lower_bound #define up upper_bound #define pb push_back #define Mod 1000000007 #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL); int prime_factors(int n) // Use int instead of ll to avoid TLE { int res=0; while (n % 2 == 0) { res++; n = n/2; } for (int i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { res++; n = n/i; } } if (n > 2) res++; return res; } /////////////////////////////////////////////////////////// Solve here ////////////////////////////////////////////////////// ll ncr(ll n,ll r){ if(r==0) return 1; return n* ncr(n-1,r-1) / r ; } void solve(){ int a,b,c; cin>>a>>b>>c; if(c%2){ if(a==b) cout<<"="; else if(a<b) cout<<"<"; else cout<<">"; } else{ ll aa = a; aa=abs(aa); ll bb = b; bb=abs(bb); if(aa==bb) cout<<"="; else if(aa<bb) cout<<"<"; else cout<<">"; } return ; } /////////////////////////////////////////////////////////////// MAIN FUNCTION ////////////////////////////////////////////////// int main(){ fastio; int tc; tc=1; // cin>>tc; while(tc--){ solve(); if(tc) cout<<"\n"; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pll> vpll; #define FOR(n) for(int i=0;i<n;++i) #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll gcd(ll a, ll b) { return a % b ? gcd(b, a % b) : b; } const ll mod = 1e9 + 7; const ll inf = 0x3f3f3f3f; ll qpow(ll a, ll b) { ll t = 1; while (b != 0) { if (b & 1)t = (t * a) % mod; a = (a * a) % mod; b >>= 1; }return t; } int n; ll a[212345]; ll b[212345]; ll c[212345]; signed main() { cin >> n; FOR(n)cin >> a[i]; b[0] = a[0]; for (int i = 1; i < n; ++i)b[i] = b[i - 1] + a[i]; c[0] = max(0ll, b[0]); for (int i = 1; i < n; ++i)c[i] = max(c[i - 1], b[i]); /*FOR(n)cout << a[i] << " "; cout << endl; FOR(n)cout << b[i] << " "; cout << endl; FOR(n)cout << c[i] << " "; cout << endl;*/ ll mx = 0, ans = 0; FOR(n) { mx = max(ans + c[i], mx); ans += b[i]; } mx = max(mx, ans); cout << mx << endl; return 0; }
//Iamskk// #include <iostream> #include <iosfwd> #include <iomanip> #include <cstdio> #include <cstring> #include <cstdlib> #include <ctime> #include <cmath> #include <cassert> #include <cctype> #include <climits> #include <vector> #include <bitset> #include <set> #include <queue> #include <stack> #include <map> #include <deque> #include <string> #include <list> #include <iterator> #include <sstream> #include <complex> #include <fstream> #include <functional> #include <numeric> #include <utility> #include <algorithm> #include <assert.h> #include <unordered_map> using namespace std; #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); #define endl '\n' #define yes cout<<"YES"<<endl #define no cout<<"NO"<<endl #define pb push_back #define all(c) (c).begin(),(c).end() #define rall(c) (c).rbegin(),(c).rend() #define ai(arr,n) for(int i=0;i<n;i++)cin>>arr[i]; #define ao(arr) for(auto asdfasdfasdf:arr) cout<<asdfasdfasdf<<" "; #define mi(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cin>>arr[i][j];} #define mo(arr,m,n) for(int i=0;i<m;i++){ for(int j=0;j<n;j++) cout<<arr[i][j]<<" "; cout<<endl;} #define countsetbits(x) __builtin_popcount(x) #define ll long long #define debug cout<<"I AM EXECUTING"<<endl #define testcases int asdf; cin>>asdf; while(asdf--) #define vi vector<int> #define si set<int> #define vc vector<char> #define sc set<char> #define vll vector<long long int> #define vs vector<string> #define vpp vector<pair<int,int>> #define vppo(prs) for(auto x:prs){cout<<x.first<<" "<<x.second<<endl;} #define For(__,hajmola,adfdf) for(int __ = hajmola; __<adfdf;__++) string sconvert(ll int n) { stringstream ss; ss<<n; string str = ss.str(); return str; } bool sortbysec(const pair<int,int> &a, const pair<int,int> &b) { return (a.second > b.second); } void single() { ll int n; cin>>n; vector<ll int> arr(n); ai(arr,n); vector<ll int> mtn(n,0); ll int sum=arr[0]; mtn[0] = max(mtn[0],sum); For(i,1,n){ sum+=arr[i]; if(sum>mtn[i-1]){ mtn[i]=sum; } else{ mtn[i]=mtn[i-1]; } } ll int answer = 0; sum=0; ll int lp = 0; For(i,0,n){ sum+=arr[i]; answer = max(answer,lp+mtn[i]); lp=lp+sum; } cout<<answer<<endl; return; } void multiple() { testcases { single(); } } int main() { IOS; // freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); single(); }
#include <bits/stdc++.h> #define LL long long #define ULL unsigned long long #define pb push_back #define st first #define nd second #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3f template <class T> T read(T &a) { a=0;char x=getchar();bool f=0; for(;x<'0'||x>'9';x=getchar())f|=x=='-'; for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0'; if(f)a=-a; return a; } using namespace std; int main() { char s[10]; scanf("%s", s); putchar(s[1]); putchar(s[2]); putchar(s[0]); return 0; }
// Problem : A - 106 // Contest : AtCoder - AtCoder Regular Contest 106 // URL : https://atcoder.jp/contests/arc106/tasks/arc106_a // Memory Limit : 1024 MB // Time Limit : 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> using namespace std; long long N; long long mypow(long long a, long long b){ long long ret = 1; while(b--){ ret *= a; } return ret; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; for(long long i = 1; i<40; i++){ for(long long j = 1; mypow(3, i) + mypow(5, j) <= N; j++){ if(mypow(3, i) + mypow(5, j) == N){ cout << i << " " << j << "\n"; return 0; } } } cout << -1; }
#include <cstdio> #include <cstring> #define int long long const int N = 200005, M = 2*N, p = 1000000007; int num, head[N], next[M], vet[M], len[M], n, x, y, z, ans, dis[N], cnt[2]; void add(int u, int v, int c) { vet[++num] = v, len[num] = c; next[num] = head[u]; head[u] = num; } int Pow(int a, int b) { int an = 1; while(b) { if(b & 1) an = an * a % p; a = a * a % p; b >>= 1; } return an; } void dfs(int u, int fa, int d) { dis[u] = d; for(int i = head[u]; i; i = next[i]) { int v = vet[i]; if(v == fa) continue; dfs(v, u, d ^ len[i]); } } signed main() { scanf("%lld", &n); for(int i = 2; i <= n; i++) { scanf("%lld%lld%lld", &x, &y, &z); add(x, y, z), add(y, x, z); } dfs(1, 0, 0); for(int i = 0; i < 60; i++) { cnt[0] = cnt[1] = 0; for(int j = 1; j <= n; j++) cnt[(dis[j] >> i) & 1] ++; ans = (ans + Pow(2, i) * cnt[0] % p * cnt[1] % p) % p; } printf("%lld", ans); return 0; }
#pragma GCC optimize ("O3") #pragma GCC target ("sse4") #include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } #define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0) template<unsigned MOD_> struct ModInt { static constexpr unsigned MOD = MOD_; unsigned x; void undef() { x = (unsigned)-1; } bool isnan() const { return x == (unsigned)-1; } inline int geti() const { return (int)x; } ModInt() { x = 0; } ModInt(int y) { if (y<0 || (int)MOD<=y) y %= (int)MOD; if (y<0) y += MOD; x=y; } ModInt(unsigned y) { if (MOD<=y) x = y % MOD; else x = y; } ModInt(long long y) { if (y<0 || MOD<=y) y %= MOD; if (y<0) y += MOD; x=y; } ModInt(unsigned long long y) { if (MOD<=y) x = y % MOD; else x = y; } ModInt &operator+=(const ModInt y) { if ((x += y.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(const ModInt y) { if ((x -= y.x) & (1u<<31)) x += MOD; return *this; } ModInt &operator*=(const ModInt y) { x = (unsigned long long)x * y.x % MOD; return *this; } ModInt &operator/=(const ModInt y) { x = (unsigned long long)x * y.inv().x % MOD; return *this; } ModInt operator-() const { return (x ? MOD-x: 0); } ModInt inv() const { return pow(MOD-2); } ModInt pow(long long y) const { ModInt b = *this, r = 1; if (y < 0) { b = b.inv(); y = -y; } for (; y; y>>=1) { if (y&1) r *= b; b *= b; } return r; } friend ModInt operator+(ModInt x, const ModInt y) { return x += y; } friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; } friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; } friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); } friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; } friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; } friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; } }; constexpr LL MOD = 1000000007; using Mint = ModInt<MOD>; int N; int X[200011]; int Y[200011]; LL W[200011]; VI G[200011]; int par[200011]; LL dp[200011]; void MAIN() { scanf("%d", &N); REP (i, N-1) { int x, y; LL w; scanf("%d%d%lld", &x, &y, &w); x--; y--; G[x].push_back(i); G[y].push_back(i); X[i] = x; Y[i] = y; W[i] = w; } VI que; que.push_back(0); REP (i, N) { int v = que[i]; EACH (e, G[v]) { int w = v ^ X[*e] ^ Y[*e]; if (w == par[v]) continue; dp[w] = dp[v] ^ W[*e]; par[w] = v; que.push_back(w); } } Mint ans = 0; for (int t=60; t--;) { LL a = 0; REP (i, N) if (dp[i]>>t&1) a++; LL b = N - a; ans *= 2; ans += a * b; } printf("%d\n", ans.geti()); } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define all(x) (x).begin(),(x).end() template<typename T> std::ostream & operator<<(std::ostream & Str, vector<T> const & vec) { Str << "["; for (int i = 0; i < vec.size(); ++i) { if (i != 0) cout << ", "; cout << vec.at(i); } Str << "]"; return Str; } void solve(long long N, std::vector<long long> A, std::vector<long long> B, std::vector<long long> C){ ll ans = 0; vector<ll> map(200000, 0); rep(j, N) { map.at(B.at(C.at(j) - 1)) += 1; } rep(i, N) { ans += map.at(A.at(i)); } cout << ans << endl; } int main(){ long long N; std::scanf("%lld", &N); std::vector<long long> A(N); for(int i = 0 ; i < N ; i++){ std::scanf("%lld", &A[i]); } std::vector<long long> B(N); for(int i = 0 ; i < N ; i++){ std::scanf("%lld", &B[i]); } std::vector<long long> C(N); for(int i = 0 ; i < N ; i++){ std::scanf("%lld", &C[i]); } solve(N, std::move(A), std::move(B), std::move(C)); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define vl vector<ll> #define ml map<ll,ll> #define sl set<ll> #define vp vector< pair <int,int> > #define pb push_back #define asort sort(a,a+n) #define all(v) v.begin(),v.end() #define vsort(v) sort(v.begin(),v.end()) #define F first #define S second #define mod 1000000007 #define mit map<ll,ll>::iterator #define for0(j,n) for(ll i=j;i<n;i++) #define for1(j,n) for(ll i=j;i<=n;++i) #define MA LLONG_MAX #define MI LLONG_MIN long long power(long long x,ll y) { long long res = 1; x = x % mod; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long modInverse(long long n) { return power(n, mod - 2); } long long fac[200001]; long long ncr(long long n, ll r) { if (n < r) return 0; if (r == 0) return 1; long long fac[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % mod; return (fac[n] * modInverse(fac[r]) % mod*modInverse(fac[n - r])% mod)% mod; } vector<ll> prime; void sieve() { vector<bool> primes(10000000000000,true); primes[0]=primes[1]=false; for(int i=2;i<10000000000000;++i) { if(primes[i]) { for(int j=i*i;j<10000000000000;j+=i) primes[j]=false; } } for(int i=0;i<10000000000000;++i) if(primes[i]) prime.pb(i); } sl factorization; void trial_division3(long long n) { for (int d : {2, 3, 5}) { while (n % d == 0) { factorization.insert(d); n /= d; } } static array<int, 8> increments = {4, 2, 4, 2, 4, 6, 2, 6}; int i = 0; for (long long d = 7; d * d <= n; d += increments[i++]) { while (n % d == 0) { factorization.insert(d); n /= d; } if (i == 8) i = 0; } if (n > 1) factorization.insert(n); } void solve() { ll n;string s; cin>>n; sl p; for(ll i=1;i*i<=n;++i) { if(n%i==0) { p.insert(i);p.insert(n/i); } } for(auto it=p.begin();it!=p.end();++it) cout<<*it<<endl; } int main() { ll t=1; fac[0] = 1; for (int i = 1; i <= 200000; i++) fac[i] = (fac[i - 1] * i) % mod; // cin>>t; while(t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e6; int cnt[MAX+4]; bool isSqr[MAX+4]; using pii = pair<int,int>; long long coprimes(int l, int r){ //[l,r]에서 x<y인 서로소 쌍의 수 //전체에서 k로 동시에 나눠떨어지는걸로 포함배제하면 된다 long long result = 1LL * (r-l+1) * (r-l) / 2; for(int i=2;i<=r;i++)if(!isSqr[i]){ long long tmp = r/i - (l-1)/i; if(cnt[i]&1) result -= tmp*(tmp-1)/2; else result += tmp*(tmp-1)/2; } return result; } int main(){ for(int i=2;i<=MAX;i++)if(cnt[i] == 0){ for(int j=i;j<=MAX;j+=i) cnt[j]++; if(1LL*i*i<=MAX) for(int j=i*i;j<=MAX;j+=i*i) isSqr[j] = 1; } int L, R; scanf("%d%d",&L,&R); long long ans = 1LL*(R-L+1)*(R-L)/2; ans -= coprimes(L,R); for(int i=max(2,L);i<=R;i++) ans -= R/i-1; printf("%lld\n", ans*2); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int H,W; cin>>H>>W; vector<vector<int>> p(H+1,vector<int>(W+1)); for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ char a; cin>>a; if(a=='.'){ p[i][j]++; } } } int Z=0; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(p[i][j]){ Z+=(p[i][j+1]+p[i+1][j]); } } } cout<<Z<<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; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //#define int long long #define len(x) (int)x.length() #define reset(x) memset(x,0,sizeof(x)) #define db1(x) cout<<#x<<"="<<x<<'\n' #define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n' #define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n' #define rep(i,n) for(int i=0;i<(n);++i) #define repA(i,a,n) for(int i=a;i<=(n);++i) #define repD(i,a,n) for(int i=a;i>=(n);--i) #define sz(a) (int)a.size() #define pii pair<int,int> #define all(a) a.begin(),a.end() #define PI 3.1415926535897932384626433832795 #define INF 1000000000 #define MOD 1000000007 #define MOD2 1000000009 #define EPS 1e-6 #define pb push_back #define fi first #define se second #define mp make_pair int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //freopen(".inp", "r", stdin); //freopen(".out", "w", stdout); string s; cin >> s; deque<char> dq; int dir = 0; for (int i = 0; i < s.length(); i++) { if (s[i] == 'R') { dir = 1 - dir; continue; } if (dir == 1) { dq.push_back(s[i]); } else { dq.push_front(s[i]); } } vector<char> final; while(dq.empty() == false) { final.pb(dq.back()); dq.pop_back(); } if (dir == 1) { reverse(all(final)); } stack<char> sc; if (sz(final) == 0) { cout <<""; return 0; } /*for (auto x : final) { cout << x; }*/ sc.push(final[0]); for (int i = 1; i < sz(final); i++) { if (sc.empty() == true) { sc.push(final[i]); } else { if (sc.top() != final[i]) { sc.push(final[i]); } else { bool flag = false; while(sc.empty() == false && i < sz(final) && sc.top() == final[i]) { sc.pop(); i++; flag = true; } if (flag == true) { i--; } } } } stack<char> pp; while(!sc.empty()) { pp.push(sc.top()); sc.pop(); } while(!pp.empty()) { cout << pp.top(); pp.pop(); } }
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <string.h> #include <vector> #include <queue> #include <cmath> #include <complex> #include <functional> #include <numeric> #include <iomanip> #include <cassert> #include <random> #include <chrono> /* #include <atcoder/all> */ /* using namespace atcoder; */ using namespace std; void debug_out(){ cout << "\n"; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << H << " "; debug_out(T...); } #ifdef _DEBUG #define debug(...) debug_out(__VA_ARGS__) #else #define debug(...) #endif #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for(int i=(a);i<(n);++i) #define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=int(n)-1;i>=0;--i) #define IN(a, x, b) (a<=x && x<b) #define OUT(a, x, b) (x<a || b<=x) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; using ull = unsigned long long; using ld = long double; const ll MOD = 1000000007; /* const ll MOD = 998244353; */ const ll INF = (1<<30)-1; const ll INFLL = 1ll<<60; const double PI = acos(-1); struct INIT { INIT(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); }}INIT; int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}; int dy[] = {1, 0, -1, 0, 1, -1, 1, -1}; struct T { int d, x, y; T(int d, int x, int y): d(d), x(x), y(y) {} bool operator < (const T &a) const { return d > a.d; } }; signed main() { int H, W; cin >> H >> W; vector<string> s(H); REP(i, H) cin >> s[i]; int sx, sy, gx, gy; vector<vector<pair<int, int>>> v(26); REP(i, H) REP(j, W){ if(s[i][j] == '.' || s[i][j] == '#') continue; if(s[i][j] == 'S'){ sx = i; sy = j; }else if(s[i][j] == 'G'){ gx = i; gy = j; }else{ v[s[i][j]-'a'].emplace_back(i, j); } } priority_queue<T> q; q.emplace(0, sx, sy); vector<vector<int>> dist(H, vector<int>(W, INF)); dist[sx][sy] = 0; while(!q.empty()){ auto p = q.top(); q.pop(); int d = p.d; int x = p.x; int y = p.y; if(dist[x][y] != d) continue; if(x == gx && y == gy) break; REP(i, 4){ int nx = x+dx[i]; int ny = y+dy[i]; if(OUT(0, nx, H) || OUT(0, ny, W)) continue; if(s[nx][ny] == '#') continue; if(chmin(dist[nx][ny], dist[x][y]+1)){ q.emplace(dist[nx][ny], nx, ny); } } vector<int> A, B; if('a' <= s[x][y] && s[x][y] <= 'z'){ for(auto [nx, ny] : v[s[x][y]-'a']){ if(chmin(dist[nx][ny], dist[x][y]+1)){ q.emplace(dist[nx][ny], nx, ny); A.emplace_back(nx); B.emplace_back(ny); } } } if(A.size() > 0){ REP(i, A.size()){ s[A[i]][B[i]] = '.'; } s[x][y] = '.'; } } int ans = dist[gx][gy]; if(ans == INF) ans = -1; cout << ans << "\n"; return 0; }
#pragma GCC optimize "trapv" #include<bits/stdc++.h> #define int long long #define pb push_back #define ppb pop_back #define ff first #define ss second #define sz(s) (int)(s.size()) #define all(a) (a).begin(),(a).end() #define pii pair<int,int> #define deb(x) cout << #x << " = " << x << endl #define endl '\n' #define M 1000000007 const int INF = 1ll << 60; using namespace std; // set<pair<int, int>> st; vector<int> v[2001]; bool vis[2001]; void dfs(int ind, int &cnt) { vis[ind] = 1; for (auto x : v[ind]) { if (!vis[x]) { // st.insert({ind, x}); cnt++; dfs(x, cnt); } } } void solve() { int n, m; cin >> n >> m; int a, b; while (m--) { cin >> a >> b; v[a].pb(b); } int ans = n; for (int i = 1; i <= n; ++i) { memset(vis, 0, sizeof(vis)); int cnt = 0; dfs(i, cnt); ans += cnt; } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) { solve(); } }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; #define ld long double #define ll long long #define REP(i, a, b) for (ll i = a; i < b; i++) #define REPI(i, a, b) for (ll i = b - 1; i >= a; i--) #define i_os ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define INF (ll)1e18 + 100 #define endl "\n" #define p0(a) cout << a << " " #define p1(a) cout << a << endl #define p2(a, b) cout << a << " " << b << endl #define p3(a, b, c) cout << a << " " << b << " " << c << endl #define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl // SOME BITMASK KNOWLEDGE // 1)x & (x - 1):sets the last one bit of x to zero // power of two exactly when x & (x − 1) = 0. // 2)x & -x:sets all the one bits to zero, except last one bit // 3)x | (x - 1):inverts all the bits after the last one bit #define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> #define o_setll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> typedef tree<pair<ll, ll>,null_type,less<pair<ll, ll>>,rb_tree_tag,tree_order_statistics_node_update> indexed_set; typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_set1; typedef tree<string,null_type,less<string>,rb_tree_tag,tree_order_statistics_node_update> indexed_set2; //1. order_of_key(k) : number of elements strictly lesser than k //2. find_by_order(k) : k-th element in the set // freopen("input.txt","r",stdin); // freopen("output.txt","w",stdout); vector<ll> dp,possible; ll memo(ll mask,ll n){ if(dp[mask] != -1){ return dp[mask]; } ll ct = 0; REP(i,0,n){ if((mask >> i) & 1) ct++; } dp[mask] = ct; for(ll s = mask; s > 0; s = (s - 1) & mask){ if(possible[s]){ dp[mask] = min(dp[mask],1 + memo((mask^s),n)); } } return dp[mask]; } int main() { i_os; ll n,m; cin>>n>>m; set<ll> edges[n]; REP(i,0,m){ ll x,y; cin>>x>>y; x--; y--; edges[x].insert(y); edges[y].insert(x); } dp.resize((1 << n),-1); possible.resize((1 << n),0); possible[0] = 1; dp[0] = 0; REP(i,1,(1 << n)){ vector<ll> v; REP(j,0,n){ if((i >> j) & 1) v.push_back(j); } bool f = 0; REP(j,0,v.size()){ REP(k,j+1,v.size()){ auto it = edges[v[j]].find(v[k]); if(j != k && it == edges[v[j]].end()){ f = 1; break; } } if(f) break; } if(f) possible[i] = 0; else possible[i] = 1; } cout<<memo((1 << n) - 1,n)<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) for (int 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) int main() { lint h,w;cin >> h>>w; vector<string> s(h); REP(i,h){ cin >> s[i]; } vector<int> dx{1, 1, 0, 0}, dy{0, 1, 1, 0}; lint ans = 0; REP(i,h-1)REP(j,w-1){ lint cnt = 0; REP(k,4){ if (s[i+dx[k]][j+dy[k]] == '#') cnt++; } if (cnt == 1||cnt==3){ ans++; } } cout << ans << "\n"; return 0; }
#include<bits/stdc++.h> #define re register #define N 2001001 #define MAX 2001 using namespace std; typedef long long ll; typedef double db; const ll mod=998244353; inline void read(re ll &ret) { ret=0;re char c=getchar();re bool pd=false; while(!isdigit(c)){pd|=c=='-';c=getchar();} while(isdigit(c)){ret=(ret<<1)+(ret<<3)+(c&15);c=getchar();} ret=pd?-ret:ret; return; } ll n,w,x[N],y[N],z[N],a[N]; signed main() { read(n); read(w); for(re int i=1;i<=n;i++) { read(x[i]); read(y[i]); read(z[i]); a[x[i]]+=z[i]; a[y[i]]-=z[i]; } for(re int i=1;i<=200000;i++) a[i]+=a[i-1]; for(re int i=0;i<=200000;i++) { if(a[i]>w) { puts("No"); exit(0); } } puts("Yes"); exit(0); }
#include <bits/stdc++.h> #define rep(a,n) for (ll a = 0; a < (n); ++a) using namespace std; //using namespace atcoder; using ll = long long; typedef pair<ll,ll> P; typedef pair<ll,P> PP; typedef vector<vector<int> > Graph; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a >= b) { a = b; return 1; } return 0; } const ll INF = 1e18; #define debug(v) cout<<#v<<": ",prt(v); template <typename A,typename B> inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";} template <typename A,typename B,typename C> inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";} inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';} template <typename T> inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template<typename T> inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } } template <typename T> inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename A,typename B> inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} template <typename T> inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';} int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); string s; cin >> s; ll ans = 0; rep(i,9){ if(s.substr(i,4)=="ZONe")ans++; } cout << ans << endl; return 0; }
// Problem: C - ORXOR // Contest: AtCoder - AtCoder Beginner Contest 197(Sponsored by Panasonic) // URL: https://atcoder.jp/contests/abc197/tasks/abc197_c // Memory Limit: 1024 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #include<bits/stdc++.h> #include <iostream> #include <string> #include <cstring> #include <cstdio> #include <vector> #define rep(i, n) for(int i = 1;i <= n;++i) #define Rep(i, n) for(int i = n;i >= 1;--i) #define forr(i,m,n) for(int i = m;i <= n;++i) #define debug cout << "---------------------------\n"; #define endl '\n' #define IO ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define YES "YES" #define NO "NO" using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; const int maxn = 1e6 + 100; const int inf = 0x3f3f3f3f; const int mod = 998244353; // -------------- 板子 ----------------------------- int n; ll a[30],ans = 1e18; void dfs(int id,ll p) { // cout << id << ' ' << p << endl; if(id == n + 1) { ans = min(ans,p); return ; } ll cnt = 0; for(int i = id;i <= n;++i) { cnt |= a[i]; dfs(i + 1,cnt ^ p); } } int main() { IO cin >> n; for(int i = 1;i <= n;++i) cin >>a[i]; dfs(1,0); // ll cnt = 0; // for(int i = 1;i <= n;++i) { // cnt |= a[i]; // dfs(i+1,cnt); // cout << i << " " << cnt << ' ' << ans << endl; // } cout << ans <<endl; return 0; }
/*Jai Shree Ram*/ // Never Give Up #include<bits/stdc++.h> 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 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 mt19937 rng(chrono::steady_clock::now().tjhe_since_epoch().count()); //shuffle(arr,arr+n,rng) 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 } mii m; pii child[200001]; int t = 1; int BIT[200001]; void update(int i,int n,int inc) { while(i<=n) { BIT[i] += inc; i += (i & (-i)); } } int query(int i) { int ans = 0; while(i>0) { ans += BIT[i]; i -= (i&(-i)); } return ans; } void updateRange(int l,int r,int inc,int n) { if(l>r) return; update(l,n,inc); update(r+1,n,-inc); } class graph { int V; list<int>* l; public: graph(int V) { this->V = V; l = new list<int>[V+1]; } void addEdge(int x,int y) { l[x].pb(y); l[y].pb(x); } void dfs(int src,int par) { m[src] = t; child[m[src]].ff = t; t++; for(int nbr:l[src]) { if(nbr!=par) { dfs(nbr,src); } } child[m[src]].ss = t-1; } }; int32_t main() { c_p_c(); memset(BIT,0,sizeof(BIT)); int n; cin>>n; graph g(n+1); vector<pii> edges; for(int i=0;i<n-1;i++) { int a,b; cin>>a>>b; g.addEdge(a,b); edges.pb(mp(a,b)); } g.dfs(1,-1); w(q) { int t; cin>>t; int idx,inc; cin>>idx>>inc; idx--; int b = max(m[edges[idx].ff],m[edges[idx].ss]); if((t==1&& m[edges[idx].ss]==b)||(t==2 && m[edges[idx].ff]==b)) { updateRange(1,child[b].ff-1,inc,n); updateRange(child[b].ss+1,n,inc,n); } else { updateRange(child[b].ff,child[b].ss,inc,n); } } for(int i=1;i<=n;i++) cout<<query(m[i])<<endl; }
#include <bits/stdc++.h> using namespace std; // Templates here template<typename T1, typename T2, typename T3> class triple { public: T1 first; T2 second; T3 third; triple() { first = 0; second = 0; third = 0; } }; // Typedefs here typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pll> vpll; typedef set<int> si; typedef set<ll> sl; typedef set<pii> spii; typedef set<pll> spll; typedef map<string, int> mpsi; typedef map<string, ll> mpsl; typedef map<int, int> mpii; typedef map<ll, ll> mpll; // Defines here #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); #define fora(i, a, b) for(int (i)=(a);(i)<(b);(i)++) #define forla(i, a, b) for(ll (i)=(a);(i)<(b);(i)++) #define for0(i, b) fora((i),0,(b)) #define forl0(i, b) forla((i),0,(b)) #define rofa(i, a, b) for(int (i)=(a);(i)>=(b);(i)--) #define rofla(i, a, b) for(ll (i)=(a);(i)>=(b);(i)--) #define rof0(i, a) rofa((i),(a),0) #define rofl0(i, a) rofla((i),(a),0) #define forag(i, a, b, g) for(int (i)=(a);(i)<(b);(i)+=(g)) #define forlag(i, a, b, g) for(ll (i)=(a);(i)<(b);(i)+=(g)) #define for0g(i, b, g) forag((i),0,(b),(g)) #define forl0g(i, b, g) forlag((i),0,(b),(g)) #define rofag(i, a, b, g) for(int (i)=(a);(i)>=(b);(i)-=(g)) #define roflag(i, a, b, g) for(ll (i)=(a);(i)>=(b);(i)-=(g)) #define rof0g(i, a, g) rofag((i),(a),0,(g)) #define rofl0g(i, a, g) roflag((i),(a),0,(g)) #define all(x) (x).begin(),(x).end() #define pb push_back #define MP make_pair #define F first #define S second #define T third #define GraphM1(g, n) int (g)[(n)][(n)] #define GraphM2(g, n) vi (g)[(n)] // Const variables here const int maxn = 2e5 + 10; // Global variables here int n, q, a[maxn], b[maxn], par[maxn]; ll part[maxn]; GraphM2(g, maxn); bool visited[maxn]; // Functions here void dfs(int v, int p) { par[v] = p; visited[v] = true; for (auto u: g[v]) if (!visited[u]) { part[u] += part[v]; dfs(u, v); } } // Main here int main() { IOS cin >> n; fora(i, 1, n) { cin >> a[i] >> b[i]; g[a[i]].pb(b[i]); g[b[i]].pb(a[i]); } dfs(1, 0); cin >> q; for0(i, q) { int t, e, x, u, v; cin >> t >> e >> x; if (t == 1) { u = a[e]; v = b[e]; } else { v = a[e]; u = b[e]; } if (u == par[v]) { part[1] += x; part[v] -= x; } else part[u] += x; } memset(visited, 0, n + 2); dfs(1, 0); fora(i, 1, n + 1)cout << part[i] << "\n"; return 0; }
#pragma region Macros #include <bits/stdc++.h> using namespace std; using ll = long long; #define REP2(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i) #define REP3(i, l, r) for (int i = (l), i##_len = (int)(r); i < i##_len; ++i) #define GET_MACRO_REP(_1, _2, _3, NAME, ...) NAME #define REP(...) GET_MACRO_REP(__VA_ARGS__, REP3, REP2) (__VA_ARGS__) #define RREP2(i, n) for (int i = (n - 1); i >= 0; --i) #define RREP3(i, l, r) for (int i = (r - 1), i##_len = (l); i >= i##_len; --i) #define GET_MACRO_RREP(_1, _2, _3, NAME, ...) NAME #define RREP(...) GET_MACRO_REP(__VA_ARGS__, RREP3, RREP2) (__VA_ARGS__) #define IN(type, n) type n; cin >> n #define INALL(type, v) REP(i, v.size()) { IN(type, _tmp); v.at(i) = _tmp; } #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #ifdef _DEBUG #define DEBUG(x) cout << #x << ": " << x << endl #else #define DEBUG(x) #endif template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } void yes() { cout << "Yes" << endl; } void no() { cout << "No" << endl; } #pragma endregion int main() { IN(int, H); IN(int, W); vector<vector<char> > grid(H, vector<char>(W)); int start_x = 0, start_y = 0; int goal_x = 0, goal_y = 0; vector<vector<pair<int, int> > > portal(26); REP(i, H) { REP(j, W) { IN(char, c); grid.at(i).at(j) = c; if (c == 'S') { start_x = i; start_y = j; } else if (c == 'G') { goal_x = i; goal_y = j; } else if (c != '.' && c != '#') { portal.at(c - 'a').emplace_back(make_pair(i, j)); } } } vector<vector<int> > cost(H, vector<int>(W, INT_MAX)); vector<bool> uncheck(26, true); priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, greater<pair<int, pair<int, int> > > > que; que.push(make_pair(0, make_pair(start_x, start_y))); cost.at(start_x).at(start_y) = 0; while (!que.empty()) { int x, y; int tmp_cost; pair<int, int> xy; tie(tmp_cost, xy) = que.top(); tie(x, y) = xy; que.pop(); // cout << x << ", " << y << ", " << tmp_cost << endl; // int tmp_cost = cost.at(x).at(y); if (grid.at(x).at(y) != 'S' && grid.at(x).at(y) != 'G' && grid.at(x).at(y) != '.') { char tmp_c = grid.at(x).at(y); if (uncheck.at(tmp_c - 'a')) { REP(i, portal.at(tmp_c - 'a').size()) { int tmp_x, tmp_y; tie(tmp_x, tmp_y) = portal.at(tmp_c - 'a').at(i); if (tmp_x == x && tmp_y == y) continue; if (cost.at(tmp_x).at(tmp_y) > tmp_cost + 1) { que.push(make_pair(tmp_cost + 1, make_pair(tmp_x, tmp_y))); cost.at(tmp_x).at(tmp_y) = tmp_cost + 1; } } uncheck.at(tmp_c - 'a') = false; } } if (x - 1 >= 0 && grid.at(x - 1).at(y) != '#') { if (cost.at(x - 1).at(y) > tmp_cost + 1) { que.push(make_pair(tmp_cost + 1, make_pair(x - 1, y))); cost.at(x - 1).at(y) = tmp_cost + 1; } } if (x + 1 < H && grid.at(x + 1).at(y) != '#') { if (cost.at(x + 1).at(y) > tmp_cost + 1) { que.push(make_pair(tmp_cost + 1, make_pair(x + 1, y))); cost.at(x + 1).at(y) = tmp_cost + 1; } } if (y - 1 >= 0 && grid.at(x).at(y - 1) != '#') { if (cost.at(x).at(y - 1) > tmp_cost + 1) { que.push(make_pair(tmp_cost + 1, make_pair(x, y - 1))); cost.at(x).at(y - 1) = tmp_cost + 1; } } if (y + 1 < W && grid.at(x).at(y + 1) != '#') { if (cost.at(x).at(y + 1) > tmp_cost + 1) { que.push(make_pair(tmp_cost + 1, make_pair(x, y + 1))); cost.at(x).at(y + 1) = tmp_cost + 1; } } } if (cost.at(goal_x).at(goal_y) == INT_MAX) { cout << "-1" << endl; } else { cout << cost.at(goal_x).at(goal_y) << endl; } return 0; }
#include <bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int,int> P; int h,w; int fie[2005][2005]; vector<P> pos[29]; int dp[29][29]; int dist[2005][2005]; bool used[29]; int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; int bfs(int v){ memset(dist,-1,sizeof(dist)); queue<P> que; for(int i=0;i<pos[v].size();i++){ int cx=pos[v][i].first; int cy=pos[v][i].second; dist[cy][cx]=0; que.push(P(cx,cy)); } while(que.size()){ P p=que.front(); que.pop(); //printf("%d %d %d\n",p.first,p.second,dist[p.second][p.first]); int nu=fie[p.second][p.first]; if(nu==27){ return dist[p.second][p.first]; } if(nu>=0 && nu<=25 && !used[nu]){ used[nu]=true; for(int i=0;i<pos[nu].size();i++){ int nx=pos[nu][i].first; int ny=pos[nu][i].second; if(dist[ny][nx]==-1){ dist[ny][nx]=dist[p.second][p.first]+1; que.push(P(nx,ny)); } } } for(int i=0;i<4;i++){ int nx=p.first+dx[i]; int ny=p.second+dy[i]; if(nx<0 || nx>=w || ny<0 || ny>=h)continue; if(fie[ny][nx]==-2)continue; if(dist[ny][nx]==-1){ dist[ny][nx]=dist[p.second][p.first]+1; que.push(P(nx,ny)); } } } return -1; } int main(void){ for(int i=0;i<=27;i++){ for(int j=0;j<=27;j++){ dp[i][j]=11451419; } dp[i][i]=0; } scanf("%d%d",&h,&w); for(int i=0;i<h;i++){ string s; cin >> s; for(int j=0;j<w;j++){ fie[i][j]=-1; if(s[j]=='S'){ fie[i][j]=26; pos[26].push_back(P(j,i)); } if(s[j]=='G'){ fie[i][j]=27; } if(s[j]>='a' && s[j]<='z'){ fie[i][j]=(s[j]-'a'); pos[(s[j]-'a')].push_back(P(j,i)); } if(s[j]=='#'){ fie[i][j]=-2; } } } printf("%d\n",bfs(26)); return 0; }
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define all(v) v.begin(), v.end() template<class T>bool chmax(T& a, const T& b) { if (a<b) { a=b; return 1;} return 0;} template<class T>bool chmin(T& a, const T& b) { if (b<a) { a=b; return 1;} return 0;} using pint = pair<int, int>; const int MAX = 10000; int n; vector<vector<int>> xyr; bool flgs[MAX][MAX]; vector<vector<int>> res; void paintOut(int tl_x, int tl_y, int br_x, int br_y) { for (int i = tl_y; i <= br_y; ++i) { for (int j = tl_x; j <= br_x; ++j) { flgs[i][j] = true; } } } int area(int tl_x, int tl_y, int br_x, int br_y) { return (br_x - tl_x + 1) * (br_y - tl_y + 1); } vector<int> topLeft(int sx, int sy, int r) { bool left_f = true, top_f = true; int now_row = sy, now_col = sx; while (left_f || top_f) { if (now_col == 0) left_f = false; if (area(now_col-1, now_row, sx, sy) > r) { left_f = false; } if (left_f) { for (int i = now_row; i <= sy; ++i) { if (flgs[i][now_col-1]) { left_f = false; break; } } if (left_f) now_col--; } if (now_row == 0) top_f = false; if (area(now_col, now_row-1, sx, sy) > r) { top_f = false; } if (top_f) { for (int i = now_col; i <= sx; ++i) { if (flgs[now_row-1][i]) { top_f = false; break; } } if (top_f) now_row--; } } bool right_f = true, bottom_f = true; int ty = now_row, tx = now_col; now_row = sy, now_col = sx; while (right_f || bottom_f) { if (now_col == MAX-1) right_f = false; if (area(tx, ty, now_col+1, now_row) > r) { right_f = false; } if (right_f) { for (int i = ty; i <= now_row; ++i) { if (flgs[i][now_col+1]) { right_f = false; break; } } if (right_f) now_col++; } if (now_row == MAX-1) bottom_f = false; if (area(tx, ty, now_col, now_row+1) > r) { bottom_f = false; } if (bottom_f) { for (int i = tx; i <= now_col; ++i) { if (flgs[now_row+1][i]) { bottom_f = false; break; } } if (bottom_f) now_row++; } } return {tx, ty, now_col, now_row}; } void solve() { rep(i, n) flgs[xyr[i][2]][xyr[i][1]] = true; sort(all(xyr), [](auto& a, auto& b){return a[1] + a[2] - a[3]/8000< b[1] + b[2] - b[3]/8000;}); rep(i, n) { int x = xyr[i][1], y = xyr[i][2], r = xyr[i][3]; vector<int> top_left = topLeft(x, y, r); paintOut(top_left[0], top_left[1], top_left[2], top_left[3]); res[xyr[i][0]] = {top_left[0], top_left[1], top_left[2] + 1, top_left[3] + 1}; } rep(i, n) { rep(j, 4) { if (j) cout << " "; cout << res[i][j]; } cout << endl; } } int main() { cin >> n; xyr.resize(n, vector<int>(4)); rep(i, n) { xyr[i][0] = i; cin >> xyr[i][1] >> xyr[i][2] >> xyr[i][3]; } res.resize(n, vector<int>(3)); solve(); }
#include <bits/stdc++.h> //#include <atcoder/all> #define endl "\n" using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template<class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } const long long INF = 1e18; //const ll mod = 1000000007; ll L, R; ll M[2100000]; bool IsP[2010000]; ll ans = 0; int main() { cin.tie(0); ios::sync_with_stdio(false); for(int i = 2; i <= 2e6; i++) { IsP[i] = true; } for(int i = 2; i <= 2e6; i++) { if(!IsP[i]) continue; for(int j = 2 * i; j <= 2e6; j += i) IsP[j] = false; } for(int i = 2; i <= 2e6; i++) M[i] = -1; for(int p = 2; p <= 2e6; p++) { if(!IsP[p]) continue; for(int i = p; i <= 2e6; i += p) { if(i % (p * p) == 0) { M[i] = 0; } else M[i] *= -1; } } cin >> L >> R; for(ll g = 2; g * 2 <= R; g++) { ll l = (L + g - 1) / g; chmax(l, 2LL); ll r = R / g; if(r < l) continue; ans += (r - l + 1) * (r - l + 1); for(ll i = 2; i <= r; i++) { ll num = r / i - (l - 1) / i; ans -= num * num * M[i]; } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=5e3+5; const int mod=998244353; int n,m; int dp[15][N]; int fac[N],invf[N]; inline int po(int x,int k){ int ret=1; while(k){ if(k&1) ret=1ll*ret*x%mod; x=1ll*x*x%mod; k>>=1; } return ret; } inline int C(int x,int k){ if(x<k) return 0; return 1ll*fac[x]*invf[k]%mod*invf[x-k]%mod; } int main(){ int i,j,k; fac[0]=1; for(i=1;i<N;i++) fac[i]=1ll*fac[i-1]*i%mod; invf[N-1]=po(fac[N-1],mod-2); for(i=N-2;i>=0;i--) invf[i]=ll(i+1)*invf[i+1]%mod; cin>>n>>m; dp[0][0]=1; for(i=0;(1<<i)<=m;i++){ for(j=0;j*(1<<i)<=m&&j<=n;j+=2){ for(k=j*(1<<i);k<=m;k++) dp[i+1][k]=(dp[i+1][k]+1ll*dp[i][k-j*(1<<i)]*C(n,j))%mod; } } cout<<dp[i][m]; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int,int> PII; const int N=2e5+5; int n; char s[N],x[N]; int f[N][8]; int dp(int i,int k){ if(i==n) return (k==0); if(f[i][k]!=-1) return f[i][k]; int ans; if(x[i]=='T') ans=dp(i+1,(k*10+s[i]-'0')%7)|dp(i+1,(k*10)%7); else ans=dp(i+1,(k*10+s[i]-'0')%7)&dp(i+1,(k*10)%7); return f[i][k]=ans; } int main(){ memset(f,-1,sizeof(f)); scanf("%d",&n); scanf("%s%s",s,x); // cout<<s<<endl<<x<<endl; if(dp(0,0)) puts("Takahashi"); else puts("Aoki"); return 0; }
#include<bits/stdc++.h> #include <stdio.h> #include <algorithm> #define all(x) x.begin(),x.end() #define sc(x) scanf("%d",&x) #define scl(x) scanf("%lld",&x) #define LL long long #define LD long double #define pb push_back #define F first #define S second const double PI=3.1415926535897932384626433; const int KL=1e6; const LL MOD=1e9+7; using namespace std; LL n,t,q,x,a[KL],k,m,y,z,sum,b[KL]; vector <pair<pair<LL,LL>,pair<LL,LL>>> vec; int main() { scl(k);scl(n);scl(m); for(int i=1;i<=k;i++){ scl(a[i]); x=a[i]*m; x/=n; sum+=x; y=abs(x*n-a[i]*m); z=abs((x+1)*n-a[i]*m); vec.pb({{y,z},{i,x}}); } sort(all(vec)); reverse(all(vec)); t=m-sum; for(int j=1;j<=k;j++){ int i=j-1; if(j<=t){ b[vec[i].S.F]=vec[i].S.S+1; } else { b[vec[i].S.F]=vec[i].S.S; } } for(int i=1;i<=k;i++)cout<<b[i]<<" ";cout<<endl; return 0; }
// #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") // #pragma GCC optimization ("unroll-loops") #include<iostream> #include<cstdio> #include<algorithm> #include<set> #include<cmath> #include<map> #include<vector> #include<unordered_map> #include<queue> #include<cstring> #include<string> #include<bitset> #define fi first #define se second using namespace std; typedef long long ll; typedef pair<int,int> PII; typedef pair<double,double>PDD; const int INF=0x3f3f3f3f; const int mod = 1e9+7; long long mypow(int x,int y) { long long res=1; for(int i=1;i<=y;i++) res*=x; return res; } ll gcd(int a,int b) { return b?gcd(b,a%b):a; } int getlen(char s[]) { int res = 0; for(int i=1;s[i]!='\0';i++) res++; return res; } ll qmi(ll a,ll b) { ll res = 1 ; while(b) { if(b&1) res = (res * a)%mod ; a = (a * a)%mod; b>>=1; } return res; } // int dx [] = {-2,-1,1,2,2,1,-1,-2}; //马路径 // int dy [] = {1,2,2,1,-1,-2,-2,-1}; // int dx [] = {-1,0,1,0}; // 上右下左 // int dy [] = {0,1,0,-1}; // int dy[] = {1,0,-1,0}; // 直角坐标 // int dx[] = {0,1,0,-1}; int n; const int N = 4e5+10; int qmi(int a, int k, int p) { int res = 1; while (k) { if (k & 1) res = res * a % p; a = a * a % p; k >>= 1; } return res; } int C(int a, int b, int p) { if (b > a) return 0; int res = 1; for (int i = 1, j = a; i <= b; i ++, j -- ) { res = (ll)res * j % p; res = (ll)res * qmi(i, p - 2, p) % p; } return res; } int lucas(int a, int b, int p) { if(b==0) return 1; if (a < p && b < p) return C(a, b, p); return (ll)C(a % p, b % p, p) * lucas(a / p, b / p, p) % p; } char s[N]; char last[N]; int ans = 0; void solve() { scanf("%d",&n); scanf("%s",s+1); for(int i=1;i<=n;i++) { int d = 0; char c = s[i]; if(c=='W') d = 0; else if(c=='R') d = 1; else d = 2; ans = (ans + d*lucas(n-1,i-1,3)% 3) % 3; } if((n-1)&1) ans=(-ans%3+3)%3; char ch ; if(ans==0) ch = 'W'; else if(ans==1) ch = 'R'; else ch = 'B'; cout<<ch<<endl; } int main() { //ios::sync_with_stdio(false); //cin.tie(0); solve(); return 0; }
#include<bits/stdc++.h> #define int int64_t #define pb push_back #define endl '\n' #define pll pair<int,int> #define vll vector<int> #define all(a) (a).begin(),(a).end() #define ff first #define ss second #define sz(x) (int)x.size() #define hell 1000000007 #define lbnd lower_bound #define ubnd upper_bound #define bs binary_search #define mi map<int,int> #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) ((a)*(b)) / gcd((a),(b)) #define rep(i,a,b) for(int i=a;i<b;i++) #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); using namespace std; void solve(){ int a,b,c; cin>>a>>b>>c; cout<<a+b+c-(min(a,min(b,c))); } signed main(){ ios // #ifndef ONLINE_JUDGE // freopen("input(cpp).txt", "r", stdin); // freopen("output(cpp).txt", "w", stdout); // #endif int test=1; //cin>>test; while(test--){ solve(); } return 0; }
// #pragma GCC optimize("Ofast") // #pragma GCC target("avx,avx2,fma") // #pragma GCC optimization("unroll-loops") #include <bits/stdc++.h> using namespace std; #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; /********************************************************************/ #define M1 1000000007 #define M2 998244353 const double pi = 3.14159265; #define ll long long #define lld long double #define pb push_back #define mp make_pair #define vi vector<ll> #define endl '\n' #define Yes cout << "Yes" << endl #define No cout << "No" << endl #define YES cout << "YES" << endl #define NO cout << "NO" << endl #define all(x) x.begin(), x.end() #define sortall(x) sort(all(x)) #define collect(v, n) \ for (ll i = 0; i < n; i++) { \ cin >> v[i]; \ } /* __builtin_popcount(n) // the number of ones in the int __builtin_popcountll(n) // the number of ones in the ll __builtin_parity(n) // the parity (even or odd) of the number of ones __builtin_clz(n) // the number of zeros at the beginning of the number __builtin_clzll(n) // the number of zeros at the beginning of the ll __builtin_ctz(n) // the number of zeros at the end of the number */ // template <typename T> // #define o_set(T) // tree<T, null_type, less_equal<T>, rb_tree_tag, // tree_order_statistics_node_update> // member functions : // 1. order_of_key(k) : number of elements strictly lesser than k // 2. find_by_order(k) : k-th element in the set // MULTISET ==> less_equal // SET ==> less ll powM(ll x, ll y, ll m) { ll ans = 1, r = 1; x %= m; while (r > 0 && r <= y) { if (r & y) { ans *= x; ans %= m; } r <<= 1; x *= x; x %= m; } return ans; } int add(int a, int b) { a += b; if (a >= M2) a -= M2; return a; } int mul(int a, int b) { return a * 1ll * b % M2; } int binpow(int a, int b) { int res = 1; while (b) { if (b & 1) res = mul(res, a); a = mul(a, a); b >>= 1; } return res; } map<long long, long long> factorize(long long n) { map<long long, long long> ans; for (long long i = 2; i * i <= n; i++) { while (n % i == 0) { ans[i]++; n /= i; } } if (n > 1) { ans[n]++; n = 1; } return ans; } ll min(ll a, ll b) { if (a < b) return a; return b; } ll gcd(ll x, ll y) { if (x == 0) return y; return gcd((ll)y % x, (ll)x); } ll modI(ll a, ll m) { ll m0 = m, y = 0, x = 1; if (m == 1) return 0; while (a > 1) { ll q = a / m; ll t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } void amax(ll &a, ll b) { a = max(a, b); } void amin(ll &a, ll b) { a = min(a, b); } ll frac(ll p, ll q) { p = ((p + M1) % M1 + M1) % M1; q = ((q + M1) % M1 + M1) % M1; ll qinv = modI(q, M1); return (p * qinv) % M1; } void solve() { ll a, b, c; cin >> a >> b >> c; vi v = {a, b, c}; sortall(v); cout << v[2] + v[1] << endl; return; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15); ll tt = 1; // cin >> tt; for (ll tc = 1; tc <= tt; tc++) { solve(); } return 0; }
#include<stdio.h> #include<string.h> #define it register int #define ct const int #define il inline using namespace std; #define P 998244353 #define inv2 499122177 typedef long long ll; const int N=1000005; int fac[N],ifac[N],inv[N],ans,a[N],b[N],n,m,pw[N],f[N],g[N]; il int C(ct n,ct m){return n<m||m<0?0:(0ll+fac[n])*ifac[m]%P*ifac[n-m]%P;} il int ksm(it x,it L){it ans=1;while(L) L&1?ans=(0ll+ans)*x%P,0:0,x=(0ll+x)*x%P,L>>=1;return ans;} int main(){ scanf("%d%d",&n,&m);it i; for(i=1;i<=n;++i) scanf("%d",&a[i]),b[i]=1;pw[1]=2;ct lim=(n>m?n:m); for(i=(fac[0]=ifac[0]=inv[0]=fac[1]=ifac[1]=inv[1]=pw[0]=1)+1;i<=lim;++i) fac[i]=(0ll+fac[i-1])*i%P,inv[i]=(0ll+inv[P%i])*(P-P/i)%P,ifac[i]=(0ll+ifac[i-1])*inv[i]%P,pw[i]=pw[i-1]<<1,pw[i]=(pw[i]>=P?pw[i]-P:pw[i]); /*for(it k=1;k<=m;++k){ ct now=C(m,k);it tot=0; // for(i=1;i<=n;++i) tot+=b[i],tot=(tot>=P?tot-P:tot),b[i]=(0ll+b[i])*a[i]%P; for(i=1;i<=n;++i) tot=(tot+(0ll+ksm(a[i],k))*ksm(a[i],m-k))%P; ans=(ans+(0ll+now)*tot)%P,printf("%d\n",ans); }*/ for(it i=0;i<=m;++i) for(it j=1;j<=n;++j) f[i]+=b[j],f[i]=(f[i]>=P?f[i]-P:f[i]),g[i]=(g[i]+(0ll+pw[i])*b[j])%P,b[j]=(0ll+b[j])*a[j]%P; /* for(it i=1;i<=m;++i){ ans=0; for(it j=0;j<=i;++j) ans=(ans+(0ll+f[j])*f[i-j]%P*inv2)%P; printf("%d\n",(ans-g[i]+P)%P); } */ for(it i=1;i<=m;++i){ ans=0; for(it j=0;j<=i;++j) ans=(ans+(0ll+f[j])*f[i-j]%P*C(i,j))%P; ans=(0ll+ans-g[i]+P)*inv2%P,ans=(ans<0?ans+P:ans),printf("%d\n",ans); } return 0; }
//IQ134高知能系Vtuberの高井茅乃です。 //Twitter: https://twitter.com/takaichino //YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw #include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF INT_MAX #define LLINF LLONG_MAX #define REP(i,n) for(int i=0;i<n;i++) #define REP1(i,n) for(int i=1;i<=n;i++) #define MODA 1000000007 #define MODB 998244353 template <typename T> std::istream& operator>>(std::istream& is, std::vector<T>& vec) { for (T& x: vec) { is >> x; } return is; } int main() { ll ans = 0; ll tmp = 0; int n; cin >> n; int m; cin >> m; int q; cin >> q; vector<pair<ll, ll>> ca; vector<ll> bo; ll w, v; REP(i, n){ cin >> w >> v; ca.push_back(make_pair(w, v)); } REP(i, m){ cin >> w; bo.push_back(w); } vector<pair<ll, ll> > nc; vector<ll> nb; int l, r; REP(que, q){ ans = 0; cin >> l >> r; nc.clear(); nb.clear(); REP(i, n) nc.push_back(ca[i]); nc.push_back(make_pair(0,0)); REP(i, m) if(i < l-1 || r-1 < i) nb.push_back(bo[i]); sort(nb.begin(), nb.end()); int now = -1; REP(i, nb.size()){ now = n; REP(j, n){ if(nb[i] >= nc[j].first && nc[now].second <= nc[j].second){ now = j; } } if(now != n){ ans += nc[now].second; nc[now].first = INF; nc[now].second = 0; } } cout << ans << endl; } //cout << ans << endl; }
#include <iostream> #include <map> #include <set> #include <cmath> #include <queue> #include <vector> #include <string> #include <algorithm> #include <functional> using namespace std; #pragma warning (disable: 4996) long long modpow(long long a, long long b, long long m) { long long p = 1, q = a; for (int i = 0; i < 60; i++) { if ((b / (1LL << i)) % 2LL == 1) { p *= q; p %= m; } q *= q; q %= m; } return p; } long long N; long long A[3009], B[3009], C[3009], D[3009], E[3009]; int mask[3009]; int dp[32]; bool solve(long long border) { for (int i = 0; i < N; i++) { mask[i] = 0; if (A[i] >= border) mask[i] += 1; if (B[i] >= border) mask[i] += 2; if (C[i] >= border) mask[i] += 4; if (D[i] >= border) mask[i] += 8; if (E[i] >= border) mask[i] += 16; } for (int i = 0; i < 32; i++) dp[i] = (1 << 30); dp[0] = 0; for (int i = 0; i < N; i++) { for (int j = 31; j >= 0; j--) dp[j | mask[i]] = min(dp[j | mask[i]], dp[j] + 1); } if (dp[31] <= 3) return true; return false; } int main() { cin >> N; for (int i = 0; i < N; i++) cin >> A[i] >> B[i] >> C[i] >> D[i] >> E[i]; long long cl = 0LL, cr = 1200000000LL, cm, maxn = 0; for (int i = 0; i < 37; i++) { cm = (cl + cr) / 2LL; bool I = solve(cm); if (I == true) { maxn = max(maxn, cm); cl = cm; } else { cr = cm; } } cout << maxn << endl; return 0; }
#include <iostream> #include <vector> #include <array> #include <set> using namespace std; int main() { int N; cin >> N; vector<array<int, 5>> A(N); for (auto& a : A) for (int& i : a) cin >> i; int ok = 0, ng = 1001001001; auto check = [&](int x) -> bool { set<int> s; for(auto& a : A) { int bit = 0; for (int i : a) { bit <<= 1; bit |= (i >= x); } s.insert(bit); } for (int i : s) for (int j : s) for (int k : s) { if ((i|j|k)==31) return true; } return false; }; while (abs(ng - ok) > 1) { int x = (ok + ng) / 2; if (check(x)) ok = x; else ng = x; } cout << ok; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mxN = 501; const int INF = 1e9; int a[mxN][mxN]; void solve(){ long double x, y, r; cin >> x >> y >> r; r += 1e-14; ll ans = 0; for(int i = ceil(x - r); i <= x + r; i++){ ans += floor(y + sqrt(r * r - (x - i) * (x - i))) - ceil(y - sqrt(r * r - (x - i) * (x - i))) + 1; } cout << ans << "\n"; } int main(void){ ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define MOD 1000000007 #define PI 3.1415926535897932 #define INF 1e9 #define rep(i, n) for (int i = 0; i < n; i++) #define repe(i, j, n) for (int i = j; i < n; i++) #define repi(i, n) for (int i = 0; i <= n; i++) #define repie(i, j, n) for (int i = j; i <= n; i++) #define all(x) x.begin(), x.end() #define println() cout << endl #define P pair<int, int> #define fi first #define se second typedef long long ll; using Graph = vector<vector<ll>>; long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long 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 dp[2000][2000]; ll nCr(ll n, ll r) { if(n==r) return dp[n][r] = 1; if(r==0) return dp[n][r] = 1; if(r==1) return dp[n][r] = n%MOD; if(dp[n][r]) return dp[n][r]%MOD; return dp[n][r] = nCr(n-1,r)%MOD + nCr(n-1,r-1)%MOD; } ll H(ll n, ll r) { return nCr(n+r-1, r)%MOD; } int prime[10000000]; bool is_prime[100000000 + 1]; int sieve(int n) { int pcnt = 0; for(int i = 0; i <= n; i++) { is_prime[i] = true; } is_prime[0] = is_prime[1] = false; for(int i = 2; i <= n; i++) { if(is_prime[i]) { prime[pcnt++] = i; for(int j = 2*i; j <= n; j += i) { is_prime[j] = false; } } } return pcnt; } struct UnionFind { //自身が親であれば、その集合に属する頂点数に-1を掛けたもの //そうでなければ親のid vector<ll> r; UnionFind(ll N) { r = vector<ll>(N, -1); } ll root(ll x) { if (r[x] < 0) return x; return r[x] = root(r[x]); } bool unite(ll x, ll y) { x = root(x); y = root(y); if (x == y) return false; if (r[x] > r[y]) swap(x, y); r[x] += r[y]; r[y] = x; return true; } ll size(ll x) { return -r[root(x)]; } bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す ll rx = root(x); ll ry = root(y); return rx == ry; } }; void solve1() { long double x, y, r;cin >> x >> y >> r; ll xmin = ceil(x-r), xmax = floor(x+r); long double ymin, ymax; ll ans = 0; r = nextafter(r, INFINITY); for(ll i = xmin; i <= xmax; i++) { ymin = y - sqrt(r*r-(x-i)*(x-i)); ymax = y + sqrt(r*r-(x-i)*(x-i)); ll yi = ceil(ymin); ll ya = floor(ymax); //cout << i << " " << yi << " " << ya << endl; ans += ya-yi+1; } cout << ans << endl; } int main() { solve1(); }
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(x) x.begin(), x.end() int main(){ ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<array<int, 2>> black(m); for(auto &[x, y] : black) cin >> x >> y; set<int> white; white.insert(n); sort(all(black)); for(int i = 0; i < m;){ int cur = black[i][0]; vector<int> rem, ins; while(i < m && cur == black[i][0]){ int l = white.count(black[i][1] - 1), r = white.count(black[i][1] + 1); if(l + r == 0) rem.push_back(black[i][1]); else ins.push_back(black[i][1]); ++i; } for(auto j : ins) white.insert(j); for(auto j : rem) white.erase(j); } cout << (int)white.size() << "\n"; }
#include<bits/stdc++.h> using namespace std; vector< pair< int , int > > s[2003]; int q[2000006]; int main(){ int n,m;cin>>n>>m; for(int i=1;i<=m;++i){ int u,v,w;cin>>u>>v>>w; s[u].push_back(make_pair(v,w)); } for(int i=1;i<=n;++i){ int h=0,t=0; int d[2003];memset(d,0x3f3f3f3f,sizeof(d)); bool v[2003]={0}; for(int j=0;j<s[i].size();++j){ d[s[i][j].first]=min(d[s[i][j].first],s[i][j].second); if(!v[s[i][j].first]){ q[++t]=s[i][j].first; v[q[t]]=1;} } while(++h<=t){ v[q[h]]=0; for(int j=0;j<s[q[h]].size();++j) if(d[s[q[h]][j].first]>d[q[h]]+s[q[h]][j].second){ d[s[q[h]][j].first]=d[q[h]]+s[q[h]][j].second; if(!v[s[q[h]][j].first]){ q[++t]=s[q[h]][j].first; v[q[t]]=1; } } } if(d[i]!=d[0]) cout<<d[i]<<endl; else cout<<"-1\n"; } }
#include<bits/stdc++.h> #define rep(i,n) for(ll i=0;i<(ll)(n);i++) using namespace std; typedef long long ll; vector<ll> ans_vec(500,0); ll H,W,A,B; void dfs(ll a,ll k,ll board[]){ if(k == A){ ans_vec[a]++; } for(ll h=0;h<H;h++){ for(ll w=0;w<W;w++){ if(k < a){ if(w == W-1) continue; if(board[h*H+w] == 1 || board[h*H+w+1] == 1) continue; ll copy_board[500]; rep(i,500) copy_board[i] = board[i]; copy_board[h*H+w] = 1; copy_board[h*H+w+1] = 1; dfs(a,k+1,copy_board); } else{ if(h == H-1) continue; if(board[h*H+w] == 1 || board[h*H+w+H] == 1) continue; ll copy_board[500]; rep(i,500) copy_board[i] = board[i]; copy_board[h*H+w] = 1; copy_board[h*H+w+H] = 1; dfs(a,k+1,copy_board); } } } } ll frac(ll a){ if(a == 0) return 1; else return a*frac(a-1); } int main(){ ll ans = 0; cin >> H >> W >> A >> B; if(H < W){ ll tmp = W; W = H; H = tmp; } //0...縦 1...横 for(ll a = 0; a <= A; a++){ ll board[500]; memset(board,0,sizeof(board)); dfs(a,0,board); } for(ll a = 0; a <= A;a++){ ans += ans_vec[a]/(frac(a)*frac(A-a)); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; vector<vector<int>> vec(N, vector<int>(3)); for(int i = 0; i < N; i++){ cin >> vec[i][0] >> vec[i][1] >> vec[i][2]; } long long answer = 0; for(int i = 0; i < N; i++){ for(int j = i+1; j < N; j++){ if((vec[i][1] < vec[j][1] && vec[j][1] < vec[i][2]) || (vec[j][1] < vec[i][1] && vec[i][1] < vec[j][2])){ answer++; } else if(vec[j][2] == vec[i][1]){ if((vec[j][0] == 1 || vec[j][0] == 3) && (vec[i][0] == 1 || vec[i][0] == 2)){ answer++; } } else if(vec[i][2] == vec[j][1]){ if((vec[i][0] == 1 || vec[i][0] == 3) && (vec[j][0] == 1 || vec[j][0] == 2)){ answer++; } } else if(vec[i][1] == vec[j][1] || vec[i][2] == vec[j][2]){ answer ++; } } } cout << answer << endl; }
#include<bits/stdc++.h> using namespace std; #ifndef ONLINE_JUDGE #define LOCAL #endif typedef long long ll; typedef long double ld; typedef pair<ll,ll> pll; typedef vector<ll> vi; typedef vector<pll> vpll; const long infl=0x3f3f3f3f3f3f3f3fLL; const int infi=0x3f3f3f3f; #define endl '\n' #define pb push_back #define all(a) (a).begin(),(a).end() #define F first #define S second #define rs(v,n) v.resize(n) #define hell 1000000007 //#define hell 998244353 #define peak 9e18 #define pii acos(-1) #define clr(a,x) memset(a,x,sizeof(a)) auto clk=clock(); mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template<typename T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; namespace Ops{ template<class y,class z> ll expo(ll b,y e,z m){ll a=1;b%=m;while(e!=0){if((e&1)==1){a=a*b;a=a%m;}b=b*b;b%=m;e>>=1;}return a%m;} template<class y> ll power(ll b,y e){ll a=1;while(e!=0){if((e&1)==1){a=a*b;}b=b*b;e>>=1;}return a;} template<class x,class z> ll inv(x b,z m){return expo(b,m-2,m);} template<class x,class y> x invGeneral(x a,y b){if(a==0)return b==1?0:-1;x m=invGeneral(b%a,a);return m==-1?-1:((1-(ll)b*m)/a+b)%b;} template<class x,class y> x min(x a,y b){x c;if(a<=b)c=a;else c=b;return c;} template<class x,class y> x max(x a,y b){x c;if(a>=b)c=a;else c=b;return c;} ll mul(ll x,ll y,ll m){x%=m;y%=m;if(x==0)return 0;if(x%2==0)return (mul(x/2,y,m)*2)%m;return ((mul(x/2,y,m)*2)%m+y)%m;} } using namespace Ops; namespace InOp{ template<class x>istream &operator>>(istream &in,vector<x> &v) {for(auto& i:v)in>>i;return in;} template<class x,class y> ostream &operator<<(ostream &out,pair<x,y> &p) {out<<"("<<p.F<<","<<p.S<<")";return out;} template<class x> ostream &operator<<(ostream &out,vector<x> &v) {out<<"Size : "<<v.size()<<endl;for(auto i:v)out<<i<<" ";out<<endl;return out;} } using namespace InOp; namespace Debug{ #ifdef LOCAL #define deb(...) printall(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void printall(const char* name, Arg1&& arg1){cerr<<name<<" : "<<arg1<<endl;} template <typename Arg1, typename... Args> void printall(const char* names,Arg1&& arg1,Args&&... args){const char* comma=strchr(names,',');cerr.write(names,comma-names)<<" : " <<arg1<<" | ";printall(comma+1,args...);} #else #define deb(...) void(0) #endif } using namespace Debug; namespace Bits{ template<class x> constexpr int onbits(x a){return __builtin_popcount(a);} constexpr int bits(long long x){return 64-__builtin_clzll(x);}//take care of 0 constexpr int bits(int x){return 32-__builtin_clz(x);}//take care of 0 constexpr int traz(int a){return __builtin_ctz(a);} constexpr int traz(long long a){return __builtin_ctzll(a);} } using namespace Bits; namespace YesNo{ //#define CAPITAL void yes(){ #ifdef CAPITAL cout<<"YES"<<"\n"; #else cout<<"Yes"<<"\n"; #endif } void no(){ #ifdef CAPITAL cout<<"NO"<<"\n"; #else cout<<"No"<<"\n"; #endif } } using namespace YesNo; void maester(); int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(6); cerr<<fixed<<setprecision(6); ll test=1; //cin>>test; for(ll tt=1;tt<=test;tt++) { //cout<<"Case #"<<tt<<": "; maester(); } #ifdef LOCAL cerr<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl; #endif return 0; } #define N 1000005 void maester() { ll i,j,k,l,r,m,n,x,y; cin>>n; vi a(n),b(n),c(n); cin>>a>>b>>c; for(i=0;i<n;i++)c[i]=b[c[i]-1]; map<ll,ll> ma; for(auto i:c)ma[i]++; ll ans=0; for(auto i:a)ans+=ma[i]; cout<<ans<<endl; }
/* @authors MAGNETO @date 2021-05-24 11:33:20 */ #include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> // Common file //#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update using namespace std; #define FAST ios_base::sync_with_stdio(false);cin.tie(nullptr); #define endl '\n' #define ll long long #define l long #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define all(x) (x).begin(),(x).end() #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define sz(x) (int)((x).size()) #define fr first #define sc second #define pii pair<int,int> #define pll pair<long long, long long> #define rep(i,a,b) for(auto i=a;i<b;i++) #define rev(i,a,b) for(auto i=a;i>=b;i--) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define debug(x) cerr << #x << " = " << x << endl; #define debug2(x, y) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<"\n";} #define debug3(x, y, z) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<"\n";} #define debug4(x, y, z, w) {cerr <<#x<<" = " <<x<<", "<<#y <<" = " <<y <<", "<<#z<<" = "<<z<<", "<<#w << " = " <<w <<"\n";} #define ppc __builtin_popcount #define ppcll __builtin_popcountll #define lcm(a,b) a*b/__gcd(a,b) //__builtin_popcount = int //__builtin_popcountl = long int //__builtin_popcountll = long long //using namespace __gnu_pbds; //template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update>; //__builtin_popcount = int //__builtin_popcountl = long int //__builtin_popcountll = long long template<typename T >T min_ ( T a , T b ) { return a > b ? b : a ; } template < typename T , typename... Ts > T min_( T first , Ts... last ){ return min_(first, min_(last...)); } template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;} template<class T, class S> std::ostream& operator<<(std::ostream &os, const std::pair<T, S> &t) { os<<"("<<t.first<<", "<<t.second<<")"; return os; } template<typename T> ostream& operator<< (ostream& out, const vector<T>& v) { out << "["; size_t last = v.size() - 1; for(size_t i = 0; i < v.size(); ++i) { out << v[i]; if (i != last) out << ", "; } out << "]"; return out; } void solve() { int n; cin >> n; int a[n],b[n],c[n]; rep(i, 0, n) cin >> a[i]; rep(i, 0, n) cin >> b[i]; rep(i, 0, n) cin >> c[i]; int d[n]; memset(d, 0, sizeof d); rep(i, 0, n){ d[c[i]-1] += 1; } map<int, int>mp; rep(i, 0, n){ if(d[i]){ mp[b[i]]+=d[i]; } } ll ans = 0; rep(i, 0, n){ ans += mp[a[i]]; } cout << ans << endl; } int32_t main() { FAST int T=1; // cin>>T; while(T--) { solve(); } }
#include <bits/stdc++.h> #define int long long using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; string s; cin >> n >> s >> q; vector<int> idxs(2*n + 1); for (int i = 1; i <= 2*n; i++) idxs[i] = i; int flag = 0; while (q--) { int t, a, b; cin >> t >> a >> b; if (t == 1) { if (flag) { if (a > n) a -= n; else a += n; if (b > n) b -= n; else b += n; } swap(idxs[a], idxs[b]); } else { flag = !flag; } } if (!flag) { for (int i = 1; i <= 2*n; i++) cout << s[idxs[i] - 1]; } else { for (int i = n + 1; i <= 2*n; i++) cout << s[idxs[i] - 1]; for (int i = 1; i <= n; i++) cout << s[idxs[i] - 1]; } return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 1e6 + 5; set<int> s[N]; map<pair<int,int>, int> mp; int par[N]; int parent(int x) { if(par[x] != x) par[x] = parent(par[x]); return par[x]; } void join(int x, int y) { x = parent(x); y = parent(y); if(x == y) return; if(s[x].size() > s[y].size())swap(x,y); for(int i:s[x]) { s[y].insert(i); mp[{y,i}] += mp[{x,i}]; } par[x] = y; } int main() { int n,q; cin>>n>>q; int a[n+1]; for(int i=1;i<=n;i++) { cin>>a[i]; mp[{i,a[i]}] = 1; s[i].insert(a[i]); par[i] = i; } while(q--) { int t,x,y; cin>>t>>x>>y; if(t == 1)join(x,y); else cout<<mp[{parent(x),y}]<<endl; } }
#include<bits/stdc++.h> #define int long long #define PI pair<int,int> using namespace std; const int maxm=1e3+5; //const int mod=1e9+7; //const int mod=998244353; vector<PI>g2[maxm][maxm]; vector<PI>g[maxm]; int mark[maxm][maxm]; int d[maxm][maxm]; int n,m; void bfs(){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ d[i][j]=1e18; } } queue<PI>q; q.push({1,n}); d[1][n]=0; mark[1][n]=1; while(q.size()){ PI x=q.front();q.pop(); for(auto y:g2[x.first][x.second]){ if(!mark[y.first][y.second]){ mark[y.first][y.second]=1; d[y.first][y.second]=d[x.first][x.second]+1; q.push(y); } } } } void solve(){ cin>>n>>m; for(int i=1;i<=m;i++){ int a,b;cin>>a>>b; char c;cin>>c; g[a].push_back({b,c}); g[b].push_back({a,c}); } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ for(auto x:g[i]){ for(auto y:g[j]){ if(x.second==y.second){ g2[i][j].push_back({x.first,y.first}); } } } } } bfs(); int ans=1e18; //偶数长度路径 for(int i=1;i<=n;i++){ ans=min(ans,d[i][i]*2); } //奇数长度路径,枚举中间的边 for(int i=1;i<=n;i++){ for(auto p:g[i]){ int j=p.first; ans=min(ans,d[i][j]*2+1); } } // if(ans==1e18)ans=-1;//无解 cout<<ans<<endl; } signed main(){ ios::sync_with_stdio(0); solve(); return 0; } /* */
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef tuple<int,int> tpl; typedef pair<int,int> Pair; #define ALL(a) (a).begin(),(a).end() #define SORT(c) sort((c).begin(),(c).end()) #define REVERSE(c) reverse((c).begin(),(c).end()) #define EXIST(m,v) (m).find((v)) != (m).end() #define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i) #define RREP(i,n) RFOR(i,n,0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL<<60; constexpr long long MOD = 1000000007; // 998244353; 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;} struct Edge{ int from, to, id=-1; int cost=1; Edge(int from, int to, int cost=1, int id=-1): from(from), to(to), cost(cost), id(id) {} Edge() {} friend istream &operator>>(istream &input, Edge &e ){ char c; input >> e.from >> e.to >> c; e.from--; e.to--; e.cost = c-'a'; return input; } Edge rev(void){ return Edge(to, from, cost, id); } }; vector<vector<Edge>> edge(1010); bool connect[1010][1010]; int d[1010][1010]; void Main(){ int N,M; cin >> N >> M; vector<Edge> edge2[N][26]; REP(_,M){ Edge e; cin >> e; edge[e.from].emplace_back(e); edge[e.to].emplace_back(e.rev()); connect[e.from][e.to] = connect[e.to][e.from] = true; edge2[e.from][e.cost].emplace_back(e); edge2[e.to][e.cost].emplace_back(e.rev()); } REP(i,N)REP(j,N) d[i][j] = INF; int ans = INF; queue<Pair> q; q.emplace(0,N-1); while(!q.empty()){ auto p = q.front(); q.pop(); int cost = p.first, v = p.second; int x = v/N, y = v%N; if(cost >= ans) break; if(!chmin(d[x][y], cost)) continue; if(x==y){ chmin(ans, cost); break; } if(connect[x][y]){ chmin(ans, cost+1); continue; } REP(i,26){ for(auto& ex : edge2[x][i])for(auto& ey : edge2[y][i]){ q.emplace(cost+2, ex.to*N+ey.to); } } } cout << (ans<INF ? ans : -1) << en; return; } int main(void){ cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15); int t=1; //cin>>t; while(t--) Main(); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long s,p; cin>>s>>p; for(long long i=1;i*i<=p;++i){ if(p%i==0){ if((i+p/i)==s){ cout<<"Yes"; return 0; } } } cout<<"No"; return 0; }
#include<bits/stdc++.h> using namespace std::chrono; using namespace std; #define int long long #define loop(i, start, end) for (int i = start; i <= end; i++) #define rloop(i, start, end) for (int i = start; i >= end; i--) #define read(a,n) loop(i,0,n-1) cin>>a[i]; #define vi vector<int> #define vec(x) vector<x> #define sz(v) (int)v.size() #define UB upper_bound #define LB lower_bound #define all(v) (v).begin(),(v).end() #define arl(v) (v).rbegin(),(v).rend() #define fsp(a) fixed<<setprecision(a) #define mem(a,with) memset(a, with, sizeof (a)) #define vmn(a) (*min_element(a.begin(),a.end())) #define vmx(a) (*max_element(a.begin(),a.end())) #define bs(a,key) binary_search(all(a),key) /// return bool. #define rotl(a,x) rotate(a.begin(),a.begin()+x,a.end()); #define rotr(a,x) rotl(a,a.size()-x); #define nl cout<<endl #define dbg(x) cerr << #x << " :: " << x << endl; #define dbg2(x, y) cerr << #x << " :: " << x << "\t" << #y << " :: " << y << endl; #define MOD 1000000007 int add(int x, int y) {int res = x + y; return (res >= MOD ? res - MOD : res);} int mul(int x, int y) {int res = x * y; return (res >= MOD ? res % MOD : res);} int power(int x, int y) {int res = 1; x %= MOD; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} void solve(){ //code goes here int s,p; cin>>s>>p; loop(x,1,sqrt(max(s,p))){ int y=p/x; if(x*y==p and x+y==s){ cout<<"Yes"; return; } } cout<<"No"; } int32_t main() { auto start = chrono::high_resolution_clock::now(); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL); int t=1; // cin>>t; while (t--) { solve(); } #ifndef ONLINE_JUDGE auto stop = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<microseconds>(stop - start); cout<<"\n\nExecuted In: "<<duration.count()<<" ms"; #endif return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops") #include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int,int> pii; typedef vector<int> vii; typedef vector<pii> vpii; typedef unordered_map<int,int> umap; typedef long double ld; #define fi first #define se second #define pb push_back #define mp make_pairNa #define popcount __builtin_popcount #define case cout<<"Case "<<z-t<<": "; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n,m,i,j,k,t=0,s=0; m=0; cin>>n; for(i=0;i<n;i++){ cin>>j; m=max(m,j); s+=j; t+=s; cout<<t+(i+1)*m<<endl; } cout<<endl; }
#include <bits/stdc++.h> template <class T> T read() { T num = 0; T f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { num = num * 10 + ch - '0'; ch = getchar(); } return num * f; } int main() { int x = read<int>(); int ans = (x - x % 100 + 100) - x; printf("%d\n", ans); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { string C; cin >> C; if(C.at(0)==C.at(1)) { if(C.at(1)==C.at(2)) { cout << "Won" << endl; } else { cout << "Lost" << endl; } } else { cout << "Lost" << endl; } }
#include <bits/stdc++.h> #define ll long long #define MODV 1000000007 // 998244353 #define INFLL (1LL<<62) #define EPS 1e-9 #define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++) #define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++) #define per(i, n) for(ll i=((ll)(n))-1; i>=0; i--) #define all(v) v.begin(), v.end() #define endl "\n" #define vi vector<ll> #define vvi vector<vector<ll>> #define Yes() cout << "Yes" << endl #define YES() cout << "YES" << endl #define No() cout << "No" << endl #define NO() cout << "NO" << endl __attribute__((constructor)) void init(){ std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout<<std::fixed<<std::setprecision(15); } 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; } using namespace std; int main(){ string s; cin >> s; if(s[0] == s[1] && s[1] == s[2] && s[2] == s[0]) cout << "Won" << endl; else cout << "Lost" << endl; }
//Codeforcesで128bit整数を使いたいとき //→__int128_tを使う&GNU C++17 (64)で提出する //インクルードなど #include <bits/stdc++.h> using namespace std; typedef long long 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 //問題による //略記 #define F first #define S second //出力(空白区切りで昇順に) #define coutALL(x) \ for (auto i = x.begin(); i != --x.end(); i++) \ cout << *i << " "; \ cout << *--x.end() << endl; //aをbで割る時の繰上げ,繰り下げ ll myceil(ll a, ll b) { return (a + (b - 1)) / b; } ll myfloor(ll a, ll b) { return a / b; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin >> N >> M; vector<pair<int, int> > a(M+1); REP(i,M){ cin >> a[i].F >> a[i].S; } int K; cin >> K; vector<pair<int, int> > b(K+1); REP(i,K){ cin >> b[i].F >> b[i].S; } int k2 = 1 << K; int ans = 0; REP(i,k2){ vector<int> dist(N+1); REP(j,K){ if(i&(1<<j)) dist[b[j].F]++; else dist[b[j].S]++; } int now = 0; REP(j,M){ if(!dist[a[j].F]) continue; if(!dist[a[j].S]) continue; now++; } ans = max(ans,now); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; // #define int long long // #define double long double #define rep(i,a,b) for(int i=(int)(a); i<(int)(b); ++i) #define repeq(i,a,b) for(int i=(int)(a); i<=(int)(b); ++i) #define rrep(i,a,b) for(int i=(int)(b)-1; i>=(int)(a); --i) #define rrepeq(i,a,b) for(int i=(int)(b); i>=(int)(a); --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; template<typename T> using Graph = vector<vector<T>>; template<typename T> using Spacial = vector<vector<vector<T>>>; using pii = pair<int, int>; using pll = pair<ll, ll>; template<typename T> using greater_priority_queue = priority_queue<T, vector<T>, greater<T>>; const int MOD = 1e9+7; const int MOD2 = 998244353; // const double EPS = 1e-9; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; string interval[2] = {" ", "\n"}; // {" ", "\n"} template<typename T> struct is_plural : false_type{}; template<typename T1, typename T2> struct is_plural<pair<T1, T2>> : true_type{}; template<typename T> struct is_plural<vector<T>> : true_type{}; template<typename T> struct is_plural<complex<T>> : true_type{}; template<> struct is_plural<string> : true_type{}; template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second; } template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << " " << p.second; } template<typename T> istream &operator>>(istream &is, vector<T> &vec) { for(auto itr = vec.begin(); itr != vec.end(); ++itr) is >> *itr; return is; } template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { if(vec.empty()) return os; bool pl = is_plural<T>(); os << vec.front(); for(auto itr = ++vec.begin(); itr != vec.end(); ++itr) os << interval[pl] << *itr; return os; } bool CoutYN(bool a, string y = "Yes", string n = "No") { cout << (a ? y : n) << "\n"; return a; } 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); } long long modpow(int a, long long n, int mod = MOD) { long long ret = 1; do { if(n & 1) ret = ret * a % mod; a = 1LL * a * a % mod; } while(n >>= 1); return ret; } template<typename T1, typename T2> bool CompareBySecond(pair<T1, T2> a, pair<T1, T2> b) { return a.second != b.second ? a.second < b.second : a.first < b.first; } template<typename T1, typename T2> bool CompareByInverse(pair<T1, T2> a, pair<T1, T2> b) { return a.first != b.first ? a.first < b.first : a.second > b.second; } /* -------- <templates end> -------- */ void solve() { int n; ll x; cin >> n >> x; vector<int> a(n); cin >> a; constexpr ll INFTY = 1LL << 60; ll ans = INFTY; for(int z=1; z<=n; ++z) { ll dp[n+1][z+2][z]; fill(dp[0][0], dp[n+1][0], -INFTY); dp[0][0][0] = 0; for(int i=0; i<n; ++i) { for(int j=0; j<=z; ++j) { for(int k=0; k<z; ++k) { if(dp[i][j][k] == -INFTY) continue; chmax(dp[i+1][j][k], dp[i][j][k]); chmax(dp[i+1][j+1][(k + a[i]) % z], dp[i][j][k] + a[i]); } } } if(ll tmp = dp[n][z][x % z]; tmp != -INFTY) { chmin(ans, (x - tmp) / z); } } cout << ans << endl; } /* -------- <programs end> -------- */ signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = 1e18; const int inf = 1e9 + 10; #define rep(i, a, b) for (int i = a; i < b; i++) #define per(i, a, b) for (int i = b - 1; i >= a; i--) using pint = pair<ll, ll>; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int main() { int n; cin >> n; vector<ll> a(n); map<int, int> mp; bool chk = 0; int ans = inf; rep(i, 0, n) { cin >> a[i]; a[i] %= 200; if (a[i] == 0) { chk = 1; ans = i; } mp[a[i]]++; } if (chk) { cout << "Yes\n"; rep(i, 0, n) { if (ans != i) { if (ans > i) { cout << "2 " << i + 1 << " " << ans + 1 << "\n"; cout << "1 " << i + 1 << "\n"; return 0; } else { cout << "2 " << ans + 1 << " " << i + 1 << "\n"; cout << "1 " << i + 1 << "\n"; return 0; } } } } for (auto p : mp) { if (p.second > 1) { chk = 1; ans = p.first; break; } } if (chk) { cout << "Yes\n"; int c = 0; rep(i, 0, n) { if (ans == a[i]) { cout << "1 " << i + 1 << "\n"; c++; if (c == 2) return 0; } } } for (ll bit = 1; bit < (1 << n); bit++) { vector<int> vec; if (__builtin_popcount(bit) == 1) continue; ll sum = 0; rep(i, 0, n) { if (bit & (1 << i)) { vec.push_back(i); sum += a[i]; } } sum %= 200; mp[sum]++; if (mp[sum] == 2) { chk = 1; ans = sum; break; } } if (chk) { cout << "Yes\n"; int cnt = 0; for (ll bit = 1; bit < (1 << n); bit++) { vector<int> vec; ll sum = 0; rep(i, 0, n) { if (bit & (1 << i)) { vec.push_back(i); sum += a[i]; } } sum %= 200; if (sum == ans) { cout << __builtin_popcount(bit) << " "; for (auto i : vec) cout << i + 1 << " "; cout << "\n"; cnt++; if (cnt == 2) return 0; } } } else { cout << "No\n"; } }
// #define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; #define INF 1 << 30 // bool solve1(string S) { // sort(S.begin(), S.end()); // for (int i = 8; i <= 999992; i += 8) { // auto T = to_string(i); // sort(T.begin(), T.end()); // if (S == T) { // return true; // } // } // return false; // } bool solve2(string s) { if (s.size() == 1) return s == "8"; if (s.size() == 2) { if (stoi(s) % 8 == 0) return 1; swap(s[0], s[1]); return stoi(s) % 8 == 0; } vector<int> cnt(10); for (char x : s) cnt[x - '0']++; for (int i = 112; i < 1000; i += 8) { auto c = cnt; for (char x : to_string(i)) c[x - '0']--; if (all_of(c.begin(), c.end(), [](int x) { return x >= 0; })) return 1; } return 0; } int main() { string s; cin >> s; bool ans = solve2(s); cout << (ans?"Yes":"No") << endl; }
#include <bits/stdc++.h> using namespace std; using namespace chrono; #define flash ios_base::sync_with_stdio(0);cin.tie(0); #define ll long long #define ndl '\n' void solve() { ll n;cin>>n; cout<<ceil((-1+sqrtl(1+8*n))/2)<<ndl; } int main() { auto starttime = high_resolution_clock::now(); flash solve(); auto endtime = high_resolution_clock::now(); double duration = duration_cast<microseconds>(endtime - starttime).count(); duration/=1000000; cerr<<"Time Taken : "<<fixed<<setprecision(6)<<duration<<" secs"<<'\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int cur, n, i; cin >> n ; cur = 0; i = 1 ; while(i<=n){ cur += i ; if(cur>=n) break ; ++i ;} cout << i ; return 0; }
#include <bits/stdc++.h> #define pii pair<int,int> #define fi first #define pb push_back #define si second #define int long long // #define ll long long #define mod 1000000007 // #define mod 998244353 #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define double long double #define all(o) o.begin(),o.end() using namespace std; // int mod; int power(int x, int y) { int res = 1LL; x = x % mod; while (y > 0) { if (y & 1) res = (res*x) % mod; y = y>>1; // y = y/2 x = (x*x) % mod; } return res%mod; } int inv(int n) { return power(n,mod-2)%mod; } int isprime(int n){ if(n<2)return 0; int i; for(i=2;i*i<=n;i++) if(n%i==0) return 0; return 1; } int sq(int n){ return n*n; } const int N=2e5+10; vector<int> a[1000005]; int p[1000005]; void solve(){ int mx=0; int i,j,k; int l,r; cin>>l>>r; if(l==1) l++; if(l>r) { cout<<"0\n"; return; } for(i=2;i<=1e6;i++) { for(j=2*i;j<=1e6;j+=i) { a[j].pb(i); mx=max(mx,(int)a[j].size()); } p[i]=i; if(a[i].size()) p[i]=a[i][0]; } int cnt=0,coprime=0; for(i=l;i<=r;i++) { if(a[i].size()){ int lo=0,hi=a[i].size(); while(lo<hi){ int mid=(lo+hi)/2; if(a[i][mid]>=l) hi=mid; else lo=mid+1; } int val=a[i].size()-lo; cnt+=val; } vector<int> pf; int cur=i; while(cur>1){ int cp=p[cur]; pf.pb(cp); while(cur%cp==0) cur/=cp; } int sz=pf.size(); int total=0; for(j=1;j<(1<<sz);j++) { int prod=1; int bc=__builtin_popcount(j); int coprime_cnt=0; for(k=0;k<sz;k++) if(j&(1<<k)) prod*=pf[k]; coprime_cnt=i/prod-(l-1)/prod; if(bc%2==0) coprime_cnt*=-1; total+=coprime_cnt; } coprime+=i-l+1-total; } cnt+=coprime; cnt*=2; int len=r-l+1; cnt+=len; cout<<len*len-cnt<<"\n"; } int32_t main(){ fast int t=1; int tc=0; // cin>>t; while(t--) { tc++; // cout<<"Case #"<<tc<<": " solve(); } }
#include <bits/stdc++.h> #define ll long long #define pii pair<int,int> #define vec vector using namespace std; const int MX = 1e6 + 1; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef LOCAL // freopen("in1.txt", "r", stdin); // freopen("in1.txt", "w", stdout); #endif int l, r; cin >> l >> r; vec<int> lpf(MX); for (int i=2; i<MX; i++) { if (lpf[i] > 0) continue; for (int j=i; j<MX; j+=i) lpf[j] = i; } ll res = 0; for (int i=2; i<=r; i++) { int x = i, prm = 0; bool poss = 1; while (x > 1) { int y = lpf[x], cnt = 0; while (x%y == 0) { x /= y; cnt++; } if (cnt > 1) { poss = 0; break; } prm++; } if (poss) { ll c = r/i - (l-1)/i; if (prm&1) res += c*(c-1) / 2; else res -= c*(c-1) / 2; } } // cout << 2*res << '\n'; for (int i=max(2, l); i<=r; i++) res -= r/i-1; cout << 2*res << '\n'; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int tc; cin >> tc; while (tc--) { int n; cin >> n; set<int> s; for (int i = 0; i < n; ++i) { int a; cin >> a; if (s.find(a) != s.end()) s.erase(a); else s.insert(a); } if (n&1) cout << "Second\n"; else if (s.size()) cout << "First\n"; else cout << "Second\n"; } }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using lint=long long; void solve(){ int n; scanf("%d",&n); vector<lint> a(n); rep(i,n) scanf("%lld",&a[i]); if(n%2==0){ map<int,int> f; rep(i,n) ++f[a[i]]; bool ok=true; for(auto p:f) if(p.second%2==1) ok=false; puts(!ok?"First":"Second"); } else{ puts("Second"); } } int main(){ int q; scanf("%d",&q); rep(_,q) solve(); return 0; }
#pragma GCC optimize("O3") #include<bits/stdc++.h> #define ll long long #define MOD 1000000007LL #define MXN 10005 #define INF 1e9 #define EPS 1e-8 #define endl '\n' #define lowbit(x) (x&-x) using namespace std; mt19937 gen(time(0)); ll n; bool isprime[MXN]; bool v[MXN]; ll ans[MXN]; ll factor[] = {6,10,15}; vector<ll> prime; void sieve(){ for(ll i=2;i<=10000;i++){ if(isprime[i]) continue; prime.push_back(i); for(ll j=i*i;j<=10000;j+=i){ isprime[j]=1; } } } bool isfactor(ll x){ for(int i=0;i<3;i++) if(x%factor[i]==0) return 1; return 0; } bool check(){ ll gcd=ans[0]; for(int i=0;i<n;i++){ gcd=__gcd(gcd,ans[i]); if(ans[i]>10000) return 0; if(ans[i]<=0) return 0; if(v[ans[i]]) return 0; v[ans[i]]=1; } return gcd==1; } int main(){ ios::sync_with_stdio(0);cin.tie(0); sieve(); // cout<<prime.size()<<endl; // for(auto i:prime) cout<<i<<" "; // cout<<endl; cin>>n; ans[0] = 6; ans[1] = 10; ans[2] = 15; ans[3] = 12; for(int i=4,j=16;i<n;i++,j++){ while(!isfactor(j)){ j++; } ans[i]=j; } assert(check()); for(int i=0;i<n;i++) cout<<ans[i]<<" "; cout<<endl; return 0; } /* */
#include<bits/stdc++.h> using namespace std; #define int long long int #define ll long long #define quickie ios_base::sync_with_stdio(false); cin.tie(NULL); #define rep(i, a, b) for(int i=a; i<b; i++) #define rep1(i, a, b) for(int i=a; i<=b; i++) #define repp(i, a, b) for(int i=b-1; i>=a; i--) #define pb push_back #define mp make_pair #define fi first #define se second #define SZ(x) ((int)(x).size()) #define db double #define mi map<int, int> #define vi vector<int> #define qi queue<int> #define MI(x) power(x, mod-2) #define test int t; cin >> t; #define all(a) a.begin(),a.end() // #define mod 1000000007 #define pi 3.141592653589 #define fact(n) rep(i, 1, n+1)ft.pb((ft[i-1]*i)%mod) ; int power(int x, int y) ; int gcd(int a, int b) ; int power(int x, int y) { int res = 1 ; while (y > 0) { if (y & 1) res = (res*x) ; y = y>>1; x = (x*x) ; } return res ; } int gcd(int a,int b) { if(a==0) return b; return gcd(b%a,a); } // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // template<typename T> // using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>; // template<typename T> // using ordered_multiset = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>; //(*****FOR USING ORDERED SET CHANGE INT*******) // struct chash { // const int RANDOM = (long long)(make_unique<char>().get()) ^ chrono::high_resolution_clock::now().time_since_epoch().count(); // static unsigned long long hash_f(unsigned long long x) { // x += 0x9e3779b97f4a7c15; // x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; // x = (x ^ (x >> 27)) * 0x94d049bb133111eb; // return x ^ (x >> 31); // } // static unsigned hash_combine(unsigned a, unsigned b) { return a * 31 + b; } // int operator()(int x) const { return hash_f(x)^RANDOM; } // }; signed main() { quickie int n ; cin >> n ; int a[n] ; rep(i, 0, n) { cin >> a[i] ; } int ans = 0 ; int curs = 0 ; int curel = 0 ; int curmax = 0 ; rep(i, 0, n) { curs += a[i] ; curmax = max(curmax, curs) ; ans = max(ans, curel+curmax) ; curel += curs ; } cout << ans << "\n" ; }
#include<iostream> #include<algorithm> #include<cstdlib> #include<sstream> #include<cstring> #include<cstdio> #include<string> #include<deque> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; const long long MOD = 998244353; int n; long long a[200010]; long long base[200010]; void init() { base[0] = 1; for (int i = 1 ; i < 200010 ; i++) { base[i] = (base[i - 1] * 2) % MOD; } } int main(void) { init(); scanf("%d", &n); for (int i = 0 ; i < n ; i++) scanf("%lld", &(a[i])); if (n == 1) { long long ans = (a[0] * a[0]) % MOD; printf("%lld\n", ans); return 0; } sort(a, a + n); long long sum = 0; long long item = 0; item = a[n - 1]; sum = (sum + a[n - 1] * item) % MOD; item = a[n - 1] + a[n - 2]; sum = (sum + a[n - 2] * item) % MOD; //item4 = a[n-4] + a[n-3] + 2*a[n-2] + 4*a[n-1] //item3 = a[n-3] + a[n-2] + 2*a[n-1] //item2 = a[n-2] + a[n-1] for (int i = n - 3 ; i >= 0 ; i--) { item = item - a[i + 1]; if (item < 0) item = item + MOD; item = (item * 2) % MOD; item = (item + a[i + 1] + a[i]) % MOD; long long curN = (a[i] * item) % MOD; sum = (sum + curN) % MOD; } printf("%lld\n", sum); return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; s.find_by_order(x) xth element in set x.order_of_key(x) number of elements <x*/ #define ll long long #define vi vector<int> #define si set<int> #define mii map<int,int> #define pb push_back #define pf push_front #define PI acos(-1) #define pii pair<int,int> #define extract_word(s) stringstream str(s); while(str>>word) #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define SET(s) cout<<fixed<<setprecision(s) #define set0(a) memset(a,0,sizeof(a)) #define endl "\n" #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define lower_string(s) transform(all(s),s.begin(),::tolower) #define upper_string(s) transform(all(s),s.begin(),::toupper) #define len(s) (int)s.size() #define F first #define S second template<typename T,typename U> bool compare(T x,U y){return(abs(x-y)<=1e-9);} const int MOD=998244353; void solve() { int n; cin>>n; int a[n]; for(auto &x:a) cin>>x; sort(a,a+n); ll ans=0; for(auto &x:a) ans=(ans+(x*x)%MOD)%MOD; ll p=a[0]; for(int i=1;i<n;i++) { ans=(ans+(a[i]*p)%MOD)%MOD; p=(p*2+a[i])%MOD; } cout<<ans; } signed main() { //code fastio int t=1; //cin>>t; while(t--) { solve(); cout<<endl; } }
/** Bismilla- hir rahma-nir rahi-m @uthor Md Hasibur Rahman (Evan) JKKNIU */ #include<bits/stdc++.h> #define ll long long #define FI freopen("input.txt","r",stdin) #define FO freopen("output.txt","w",stdout) #define PrintCase(i) printf("Case %d: ",i) #define sc(a) scanf("%d",&a) #define scl(a) scanf("%lld",&a) #define rep(i,n) for(int i=0;i<n;i++) #define pb push_back #define MAX 1000000 #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define endl "\n" using namespace std; bool isPrime(ll a) { for(ll i=2;i*i<=a;i++) if(a%i==0) return false; return true; } bool isPowerOfTwo(ll a) { if(a==1) return true; if(a&1) return false; while(a) { a/=2; if(a!=1 && a&1) return false; if(a==1) return true; } return true; } void Swap(ll &a, ll &b) { a^=b; b^=a; a^=b; } ll gcd(ll a, ll b) { if(a<0 || b<0) { a = abs(a); b = abs(b); } if(a<b) Swap(a,b); if(b==0) return a; if(a%b==0) return b; else return gcd(b,a%b); } ll lcm(ll a, ll b) { return (a*b)/gcd(a,b); } ll power(ll base, ll exponent) { ll ans = 1; for(ll i=1;i<=exponent;i++) ans*=base; return ans; } bool isPowerOfX(ll x, ll value) { if(value==1) return true; while(value) { value/=x; if(value%x && value!=1) return false; } return true; } bool isPrefectSquare(ll value) { return (ll)sqrtl(value)*(ll)sqrtl(value)==value; } ll fact(ll value) { if(value==0 || value==1) return 1; return value*fact(value-1); } int main() { string s,tmp1,tmp2; cin>>s; int i=0,j=s.size()-1; while(s[i]=='0')i++; while(s[j]=='0')j--; for(int k=i;k<=j;k++)tmp1+=s[k]; tmp2 = tmp1; reverse(tmp2.begin(),tmp2.end()); tmp1==tmp2?cout<<"Yes\n":cout<<"No\n"; return 0; }
#include <iostream> #include <string> using namespace std; int main(){ string n; cin >> n; int zero_counter = 0; for(int i = 0;i < n.size();i++){ if(n[n.size()-1-i] == '0'){ zero_counter += 1; }else{ break; } } for(int i = 0;i < zero_counter;i++){ n = string("0") + n; } int size_n = n.size(); for(int i = 0;i < size_n/2;i++){ if(n[i]!=n[size_n-1-i]){ cout << "No" << endl; return 0; } } cout << "Yes" << endl; return 0; }
#include<bits/stdc++.h> using namespace std; const long long mod = 998244353; long long a [4] , ans; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); //freopen(".in","r",stdin); //freopen(".out","w",stdout); long long n , m; cin >> n >> m; for ( int i = 0 ; i < n ; i ++ ) { string s; cin >> s; int num = 0; for ( int j = 0 ; j < s . size () ; j ++ ) { if ( s [j] == '1' ) num ++; } num %= 2; ans += a [ !num ]; a [num] ++; } cout << ans; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using VI = vector<int>; using VL = vector<ll>; using VS = vector<string>; template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>; using graph = vector<VI>; template<class T = ll> using w_graph = vector<vector<pair<int, T>>>; #define FOR(i,a,n) for(int i=(a);i<(n);++i) #define eFOR(i,a,n) for(int i=(a);i<=(n);++i) #define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i) #define erFOR(i,a,n) for(int i=(n);i>=(a);--i) #define SORT(a) sort(a.begin(),a.end()) #define rSORT(a) sort(a.rbegin(),a.rend()) #define fSORT(a,f) sort(a.begin(),a.end(),f) #define all(a) a.begin(),a.end() #define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x)) #define tp(a,i) get<i>(a) #ifdef _DEBUG #define line cout << "-----------------------------\n" #define stop system("pause") #endif constexpr ll INF = 1000000000; constexpr ll LLINF = 1LL << 60; constexpr ll mod = 1000000007; constexpr ll MOD = 998244353; constexpr ld eps = 1e-10; constexpr ld pi = 3.1415926535897932; template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; } template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; } inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; } template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); } template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); } template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); } template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); } template<class T>inline void print(const T& a) { cout << a << "\n"; } template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); } template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); } template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); } inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; } template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); } template<class T>inline void END(T s) { print(s); exit(0); } void END() { exit(0); } int main() { init(); int n, m; cin >> n >> m; VL cnt(1 << m); FOR(i, 0, n) { string s; cin >> s; int num = 0; FOR(i, 0, m) { num = num * 2 + (s[i] - '0'); } ++cnt[num]; } ll e = 0, o = 0; FOR(bit, 0, 1 << m) { int k = 0; FOR(i, 0, m)if (bit >> i & 1)++k; if (k & 1)o += cnt[bit]; else e += cnt[bit]; } print(ll(n)* (n - 1) / 2 - e * (e - 1) / 2 - o * (o - 1) / 2); return 0; }
#include <bits/stdc++.h> #define int long long #define ll long long #define endl "\n" using namespace std; const int maxn = 1e5; signed main() { int x, y; cin >> x >> y; if (x == y) cout << x << endl; else { cout << 3 - x - y << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif const ll mod = 1e9 + 7; const ll N = 1e5 + 8; int main() { ios_base :: sync_with_stdio(false); cin.tie(NULL); ll n, k; cin >> n >> k; ll res = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= k; j++) { res += 100 * i + j; } } cout << res; return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ long long n; cin>>n; int k=10*10*10*10*10*10; vector<long long> yakusuu(2*k); for(int i=1;i<k+1;i++){ if(n%i==0){ yakusuu[i-1]=i; yakusuu[2*k-i]=n/i; } } sort(yakusuu.begin(),yakusuu.end()); yakusuu.erase(unique(yakusuu.begin(),yakusuu.end()),yakusuu.end()); for(int i=1;i<yakusuu.size();i++) cout<<yakusuu[i]<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { long n; cin>>n; set<long> ans; for(long d=1;d*d<=n;d++) { if(n%d==0) { ans.insert(d); ans.insert(n/d); } } for(auto x:ans) cout<<x<<endl; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++) #define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++) #define setp(n) fixed << setprecision(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 (a>b) { a=b; return 1; } return 0; } #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll,ll> #define pi pair<int,int> #define all(a) (a.begin()),(a.end()) #define rall(a) (a.rbegin()),(a.rend()) #define fi first #define se second #define pb push_back #define ins insert #define debug(a) cerr<<(a)<<endl #define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl #define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;} using namespace std; template<class A, class B> ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";} template<class A, class B> istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;} template<class T> vector<T> make_vec(size_t a){ return vector<T>(a); } template<class T, class... Ts> auto make_vec(size_t a, Ts... ts){ return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } //------------------------------------------------- //--Union Find tree //------------------------------------------------- class UnionFind { private: vector<int> par; vector<int> rank; vector<int> sz; public: UnionFind(int n):par(n),rank(n),sz(n){ for(int i=0; i<n; i++){ par[i] = i; rank[i] = 0; sz[i] = 1; } } int root(int x){ if (par[x]==x) return x; return par[x] = root(par[x]); } bool same(int x, int y){ return root(x)==root(y); } void unite(int x, int y){ int rx = root(x); int ry = root(y); if (rx==ry) return; if (rank[rx]<rank[ry]){ par[rx] = par[ry]; sz[ry] += sz[rx]; }else{ par[ry] = par[rx]; sz[rx] += sz[ry]; if (rank[rx]==rank[ry]) rank[rx]++; } } int size(int x){ return sz[root(x)]; } }; //------------------------------------------------- int main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; vi a(N); rep(i,N) cin>>a[i], a[i]--; UnionFind uf(200000); rep(i,N/2){ if (a[i]==a[N-i-1]) continue; uf.unite(a[i],a[N-i-1]); } set<int> se; rep(i,200000){ se.ins(uf.root(i)); } int ans=0; for(auto e:se){ ans+=uf.size(e)-1; } cout<<ans<<"\n"; return 0; }
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; //#define int long long const int N = 1e6; int n,m; bool pd[N]; inline int read() { int X=0;bool flag=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')flag=0;ch=getchar();} while(ch>='0'&&ch<='9'){X=(X<<1)+(X<<3)+ch-'0';ch=getchar();} if(flag)return X; return ~(X-1); } inline void write(int X) { if(X<0){X=~(X-1);putchar('-');} if(X>9){write(X/10);} putchar(X%10+'0'); } signed main() { n=read(),m=read(); int temp; for(int i=1;i<=n;i++)temp=read(),pd[temp]^=1; for(int i=1;i<=m;i++)temp=read(),pd[temp]^=1; for(int i=1;i<=1000;i++)if(pd[i])printf("%d ",i); return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int #define deb(x) cout << #x << " " << x << endl; #define mod 1000000007 #define fast std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); #define endl "\n" #define all(x) (x).begin(), (x).end() #define rall(v) v.rbegin(), v.rend() #define pb push_back #define lb lower_bound #define ub upper_bound const ll INF = 1e18; const ll NEGINF = -1 * INF; void solve() { int n; cin >> n; ll x, t; ll a = 0; ll b = INT_MIN; ll c = INT_MAX; for (int i = 0; i < n; i++) { cin >> x >> t; if (t == 1) { a += x; b += x; c += x; } else if (t == 2) { b = max(b, x); c = max(c, x); } else { b = min(b, x); c = min(c, x); } } // deb(a); // deb(b); // deb(c); int q; cin >> q; while (q--) { cin >> x; cout << min(c, max(b, a + x)) << endl; } } int main() { fast; #ifndef ONLINE_JUDGE freopen("/home/kalit/Desktop/Data Structures-Algo-Competitive/src/codeforces/input.txt", "r", stdin); freopen("/home/kalit/Desktop/Data Structures-Algo-Competitive/src/codeforces/output.txt", "w", stdout); #endif int T = 1; while (T--) { solve(); } //cout<< "Done in " << clock() / CLOCKS_PER_SEC <<"sec"<< endl; return 0; }
//BISMILLAHIR RAHMANIR RAHIM #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mem(a, b) (memset(a, b, sizeof(a))) #define pb push_back #define mk make_pair #define ff first #define ss second #define PI acos(-1) #define min3(a,b,c) min(a,min(b,c)) #define max3(a,b,c) max(a,max(b,c)) #define min4(a,b,c,d) min(a,min(b,min(c,d))) #define max4(a,b,c,d) max(a,max(b,max(c,d))) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define ROF(i,a,b) for(int i=a;i>=b;i--) #define REP(i,b) for(int i=0;i<b;i++) #define all(v) v.begin(),v.end() #define SORT(v) sort(v.begin(),v.end()) #define REV(v) reverse(v.begin(),v.end()) #define INF 2147483647 #define MOD 1000000007 #define MAX 200005 using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; typedef pair<ii, int> pii; typedef pair<ll, ll> LL; typedef vector<ii> vii; typedef priority_queue<int,vector<int>,greater<int> > PQ; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int setBit(int mask, int pos){return mask = mask | (1<<pos);} bool checkBit(int mask, int pos){return (bool)(mask & (1<<pos));} int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 }; int arr[60]; set<int> s[60]; int vis[60]; void solve() { int n; cin>>n; REP(i, n) { cin>>arr[i]; for(int j = 0; j < 15; j++) { if(arr[i] % primes[j] == 0) { while(arr[i] % primes[j] == 0) { arr[i] /= primes[j]; } s[i].insert(primes[j]); } } if(arr[i] != 1) { s[i].insert(arr[i]); } } ll ans = 1e18; for(int i = 0; i < (1<<15); i++) { ll res = 1; for(int j = 0; j < n; j++) vis[j] = 0; for(int j = 0; j < 15; j++) { if(checkBit(i, j)) { res = (1LL * res * primes[j]); for(int k = 0; k < n; k++) { if(!vis[k]) { if(s[k].count(primes[j])) { vis[k] = 1; continue; } } } } } bool flag = true; for(int j = 0; j < n; j++) { if(!vis[j]) { flag = false; break; } } if(flag) { ans = min(ans, res); } } cout<<ans<<endl; return; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int test = 1; //cin>>test; while(test--) { solve(); } return 0; }
#include <vector> #include <stack> #include <queue> #include <list> #include <bitset> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <algorithm> #include <numeric> #include <iostream> #include <iomanip> #include <string> #include <chrono> #include <random> #include <cmath> #include <cassert> #include <climits> #include <cstring> #include <cstdlib> #include <functional> #include <sstream> using namespace std; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); bool isUpper = false; char s; cin >> s; if (s == 'Y') { isUpper = true; } char t; cin >> t; if (isUpper) { t = toupper(t); } else { t = tolower(t); } cout << t << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef pair<ld,ld> pdd; typedef vector<ll> vll; typedef vector<ld> vld; typedef vector<pll> vpl; typedef vector<vll> vvll; #define ALL(a) a.begin(),a.end() #define SZ(a) ((int)a.size()) #define FI first #define SE second #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 FOR(i,a,b) for(int i=(a);i<(b);i++) #define PB push_back #define EB emplace_back #define MP(a,b) make_pair(a,b) #define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end())))) #define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin()) #define yes cout<<"Yes"<<endl #define YES cout<<"YES"<<endl #define no cout<<"No"<<endl #define NO cout<<"NO"<<endl #define Decimal fixed<<setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL const int inf = 1e9; const ll linf = 1LL << 50; const double eps = 1e-10; const int MOD = 1e9 + 7; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin>>n; vll a((1<<n)); REP(i,(1<<n))cin>>a[i]; vll v; REP(i,(1<<n))v.push_back(i); for(ll i=1; i<n; i++){ vll q; for(ll j=1; j<=(1<<(n-i)); j++){ if(a[v[2*j-2]]>a[v[2*j-1]]) q.push_back(v[2*j-2]); else q.push_back(v[2*j-1]); } sort(ALL(q)); swap(v,q); } ll ans; if(a[v[0]]>a[v[1]]) ans=v[1]; else ans=v[0]; cout<<ans+1<<endl; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);++i) #define FOR(i,n,j) for(int i=(j);i<(n);++i) #define ssort(n) sort((n).begin(),(n).end()) #define rsort(n) sort((n).begin(),(n).end(),greater<int>()) #define mp make_pair using ll=long long; using ld=long double; typedef pair<int,int> P; typedef pair<P,int> COST; #define repl(i,n) for(ll i=0;i<(n);++i) #define Yes cout << "Yes" << endl #define No cout << "No" << endl #define YES cout << "YES" << endl #define NO cout << "NO" << endl using GrafPair=vector<vector<P>>; using Graf=vector<vector<int>>; #define MAX 1000000007 int main() { vector<ll> num(4); cin >> num[0] >> num[1] >> num[2] >> num[3]; ll sum=0; int n=4; rep(i,4)sum+=num[i]; for (int bit = 0; bit < (1<<n); ++bit) { /* bit で表される集合の処理を書く */ ll tmp=0; /* きちんとできていることを確認してみる */ // bit の表す集合を求める vector<int> S; for (int i = 0; i < n; ++i) { if (bit & (1<<i)) { // i が bit に入るかどうか S.push_back(i); } } // bit の表す集合の出力 if(S.size()>=1) for (int i = 0; i < (int)S.size()-1; ++i) { tmp+=num[S[i]]; } if(tmp==sum-tmp){ Yes;return 0; } } No; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; const ll MOD = 1000000007; const ll INF = 1e18; #define REP(i,n) for(int i=0;i<n;i++) #define ALL(v) v.begin(),v.end() int main(){ VI a(4); REP(i,4) cin >> a[i]; ll sum=0; REP(i,4) sum+=a[i]; bool yes=0; REP(i,1<<4){ if(i==0) continue; ll s=0; REP(j,4){ if(i>>j&1) s+=a[j]; } if(s*2==sum) yes=1; } if(yes) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
#include <iostream> #include <math.h> #include <iomanip> #include <bits/stdc++.h> #include <string.h> #include <string> #include <algorithm> #define ll long long int #define pb push_back #define fi first #define se second #define mem(a,b) memset(a,b,sizeof(a)) #define debug(x) cout << (#x) << " = " << x << endl; #define debug2(i,j) cout << (#i) << "=" << i << " , " << (#j) << "=" << j << endl; using namespace std; ll binomialCoeff(ll n,ll k) { ll res = 1; if(k >n - k) k = n - k; for(int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } ll power(ll x,ll y) { ll temp; if(y == 0) return 1; temp = power(x, y/2); if (y%2 == 0) return temp*temp; else return x*temp*temp; } /* nCr % mod ll fact[6*N]; ll inv[6*N],invfac[6*N]; ll mod = 998244353; void factorial() { fact[0] = invfac[0] = fact[1] = invfac[1] = 1; inv[1] = 1; for(int i=2;i<=5*N + 10;i++) { fact[i] = (fact[i-1]*i)%mod; inv[i] = (inv[mod%i]*(mod - mod/i))%mod; invfac[i] = (invfac[i-1]*inv[i])%mod; } }*/ vector<ll> primes; vector<bool> prime; void Sieve(int n) { prime = vector<bool> (n+1,true); //vector<bool> prime(n+1,true); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for (int i=p*p; i<=n; i += p) prime[i] = false; } } for (int p=2; p<=n; p++) if (prime[p]) primes.push_back(p); } ll modInverse(ll a,ll m) { ll m0 = m; ll y = 0, x = 1; if (m == 1) return 0; while (a > 1) { ll q = a / m; ll t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } //****************************************************** CHECK CONSTRAINTS ***************************************************************// const int Nmax = 2e5 + 6; const int N = 105; int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); char s; char t; cin >> s >> t; if(s=='Y') { if(t>=65 && t <= 90) cout << t << '\n'; else { t-=32; cout << t << '\n'; } } else cout << t << "\n"; }
#include <iostream> #include <cmath> #include <vector> #include <algorithm> #include <bitset> #include <queue> using namespace std; using ll = long long int; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) const ll INF = 1LL << 60; ll mod = 1000000007; int main() { char s,t; cin >> s >> t; if(s == 'Y') { t = t - 'a' + 'A'; cout << t << endl; }else if(s == 'N') { cout << t << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < n; i++) #define Rep(i,n) for(int i = 1; i <= n; i++) #define sz(x) int(x.size()) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define YesorNo(a) printf(a ? "Yes\n" : "No\n") #define fin(a) cout << a << endl; return 0 #define endl '\n' #define fi first #define se second using ll = long long; using ld = long double; using P = pair<int,int>; using Pl = pair<ll,ll>; template<class T> using V = vector<T>; template<class T> using priq = priority_queue<T>; template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>; const int dx[] = {0,1,0,-1,1,1,-1,-1}; const int dy[] = {1,0,-1,0,1,-1,-1,1}; ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;} 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; } struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF; template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) { return in >> p.first >> p.second; } template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) { return out << '{' << p.first << ',' << p.second << '}'; } template<class T, class U> pair<T,U> operator+(const pair<T,U> &l, const pair<T,U> &r) { return make_pair(l.first+r.first, l.second+r.second); } template<class T, class U> pair<T,U> operator-(const pair<T,U> &l, const pair<T,U> &r) { return make_pair(l.first-r.first, l.second-r.second); } template<class T, class U> pair<T,U> operator*(const pair<T,U> &l, const pair<T,U> &r) { return make_pair(l.first*r.first, l.second*r.second); } template<class T, class U> pair<T,U> operator/(const pair<T,U> &l, const pair<T,U> &r) { return make_pair(l.first/r.first, l.second/r.second); } template<class T, class U> pair<T,U>& operator+=(pair<T,U> &l, const pair<T,U> &r) { return l = l+r; } template<class T, class U> pair<T,U>& operator-=(pair<T,U> &l, const pair<T,U> &r) { return l = l-r; } template<class T, class U> pair<T,U>& operator*=(pair<T,U> &l, const pair<T,U> &r) { return l = l*r; } template<class T, class U> pair<T,U>& operator/=(pair<T,U> &l, const pair<T,U> &r) { return l = l/r; } template<class T> istream& operator>>(istream &in, vector<T> &v) { for(auto &e : v) in >> e; return in; } template<class T> ostream& operator<<(ostream &out, const vector<T> &v) { for(const auto &e : v) out << e << ' '; return out; } /*----------------------------------------------------------------------------------------------------*/ int main(){ int n, m, t; cin >> n >> m >> t; V<P> v(m); cin >> v; int b = n; bool ok = true; rep(i,m) { if(i == 0) b -= v[i].fi; else b -= (v[i].fi - v[i-1].se); ok &= (b > 0); b = min(n, b + v[i].se-v[i].fi); } b -= (t - v[m-1].se); ok &= (b > 0); YesorNo(ok); }
#include <bits/stdc++.h> #define x first #define y second #define pb push_back #define all(v) v.begin(),v.end() using namespace std; const int INF = 1e9; const int TMX = 1 << 18; const long long llINF = 2e18; const long long mod = 1e9+7; const long long hashmod = 100003; typedef long long ll; typedef long double ld; typedef pair <int,int> pi; typedef pair <ll,ll> pl; typedef vector <int> vec; typedef vector <pi> vecpi; typedef long long ll; int n; ll t,t2,st; pi a[1005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> t >> n >> t2; st = t; for(int i = 1;i <= n;i++) { cin >> a[i].x >> a[i].y; t -= a[i].x-a[i-1].y; if(t <= 0) { cout << "No"; return 0; } t += a[i].y-a[i].x; t = min(t,st); } if(t2-a[n].y < t) cout << "Yes"; else cout << "No"; }
#include<bits/stdc++.h> using namespace std; #define pb push_back #define ll long long #define vi vector <int> #define vl vector <ll> #define vii vector < pair <int,int> > #define vll vector < pair <ll,ll> > #define mii map<int,int> #define mll map<ll,ll> #define si set <int> #define sl set <ll> #define all(a) a.begin(),a.end() #define pii pair<int,int> #define rep(i,a,n) for(ll i=a ; i<n ; i++) #define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); ll binomialCoeff(ll n, ll k) { ll res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1] for (ll i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } struct MyComp { bool operator()(const pair<int,int>& x, const pair<int,int>& y) const { return x.second > y.second || (x.second == y.second && x.first > y.first); } }; ll M=1e9+7; ll mods(ll a,ll b){ a=a%M; b=b%M; return (a+b)%M; } ll modp(ll a,ll b){ a=a%M; b=b%M; return (a*b)%M; } int main(){ fastio int n;cin>>n; cout<<100-(n%100)<<endl; } //n = unique(a, a + n) - a;
/* @uthor: Kashish Gilhotra user: CodeChef, CodeForces, HackerEarth, HackerRank, SPOJ: kashish001 */ #include <bits/stdc++.h> using namespace std; #define int long long int typedef vector<int> vi; typedef vector<pair<int, int>> vpi; typedef vector<vi> vvi; const int mod = 1e9 + 7; #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL) #define debug(...) cerr << "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define EB emplace_back #define ALL(v) v.begin(), v.end() #define size(v) (int)v.size() #define endl '\n' #define UMO unordered_map #define USO unordered_set #define TC int t; cin >> t; while (t--) void Panda() { int x; cin >> x; cout << (x % 100 == 0 ? 100 : ((x - (x % 100) + 100)) - x) << endl; } int32_t main() { FAST; //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); Panda(); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<int,int> P; const int MAX_K = 200005; ll cnt[MAX_K]; int main() { ll K; cin >> K; for (int i = 1; i <= K; ++i) { for (int j = 1; j*j <= i; ++j) { if (i % j == 0) { cnt[i]++; if (i/j != j) cnt[i]++; } } } /* rep(i,K+1) { cout << cnt[i] << endl; } */ rep(i,K) { cnt[i+1] += cnt[i]; } ll ans = 0; for (int i = 1; i <= K; ++i) { ans += cnt[K/i]; //cout << cnt[K/i] << " "; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 0x3f3f3f3f #define rep(i,b,e) for(int i=b;i<e;i++) #define per(i,e,b) for(int i=e;i>b;i--) const int maxn = 5e5+5; void solve() { int x; cin >> x; if (x >= 0) cout << x << '\n'; else cout << "0\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); #ifdef DEBUG freopen("/home/l4wenwen/data/in", "r", stdin); //freopen("~/out", "w", stdout); #endif //int t; cin >> t; while(t--) solve(); return 0; }
#pragma GCC optimize("Ofast,unroll-loops") // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native") #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 int ll; typedef long double ld; typedef pair <ll, ll> pll; #ifdef SINA #define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush(); #else #define dbg(...) 0 #define dbg2(x, j, n) 0 #endif #define SZ(x) ((ll)((x).size())) #define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout); #define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i]; #define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n'); #define kill(x) return cout<< (x) << endl, 0 #define cl const ll #define fr first #define sc second #define lc (v << 1) #define rc (lc | 1) #define mid ((l + r) >> 1) #define All(x) (x).begin(), (x).end() cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353; template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';} template <class A> ostream& operator << (ostream& out, const vector<A> &a) { out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; } template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>; cl N = 5e2 + 7; ll a [N], b [N], n; vector <ll> ans; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); bool check () { for (ll i = 1; i < n; i++) if (a[i] > a[i + 1]) return false; return true; } void solve () { cin>> n; input(1, n, b); do { for (ll i = 1; i <= n; i++) a[i] = b[i]; ans.clear(); // bool f [2]; f[0] = f[1] = true; for (ll o = 1; ; o = 1 - o) { bool fnd = false; for (ll i = 2 - o; i < n; i += 2) if (a[i] > a[i + 1]) { swap(a[i], a[i + 1]); ans.push_back(i); // dbg(i, a[i]); fnd = true; break; } if (!fnd) { if (check()) break; ll x = rng() % n; if (!x) x++; if ((x & 1) != o) { if (x > 1) x--; else x++; } swap(a[x], a[x + 1]); ans.push_back(x); } } } while (SZ(ans) > n * n); // dbg(ans); cout<< SZ(ans) << '\n'; output(0, SZ(ans), ans, 1); } int main () { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll tst; for (cin>> tst; tst--;) solve(); cerr<< "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; return 0; } /* 1 4 1 4 2 3 1 4 2 3 1 4 3 2 1 3 4 2 1 3 2 4 1 2 3 4 1 4 1 4 3 2 1 4 3 2 1 4 2 3 1 2 4 3 1 2 3 4 */
#include <bits/stdc++.h> #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int X; scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second #define RF(x) freopen(x,"r",stdin) #define WF(x) freopen(x,"w",stdout) #define loop(I, A, B) for (int I = (A); I <= (B); ++I) typedef long long LL; using namespace std; typedef pair<LL,LL> PLL; typedef pair<int,int> PII; const LL MOD = (LL)1e9+7; const int SIZE = 2e5+5; const LL INF = 1LL<<60; const double eps = 1e-4; const double PI=3.1415926535897932; int p[100009]; int pp[100009]; int main(){ CASET{ DRI(n); REP(i,n){ RI(p[i]); pp[i]=p[i]; } if(n==2){ if(p[0]==1)puts("0"); else printf("1\n1\n"); continue; } vector<int> ans; REP(i,n-3){//try and find value 0...i-1 positions are all ok. Avoid flipping <i i+1; while(1){ int sp=0;//move this to position i REP(j,n){ if(p[j]==i+1){ sp=j; break; } } if(sp==i)break;//good position if(sp%2!=SZ(ans)%2){//bingo! while(sp>i){ sp--; swap(p[sp],p[sp+1]); ans.PB(sp); } break; } else{//avoid if possible int nx = i; if(nx%2!= SZ(ans)%2){ nx++; } for(int j=nx;j<n-1;j+=2){ if(j!=sp&&j+1!=sp){ nx=j;break; } } swap(p[nx],p[nx+1]); ans.PB(nx); } } } REP(it,10){ bool good = 1; REPP(i,n-3,n){ good&=p[i]==i+1; } if(good)break; int x = n-2; if(x%2!=SZ(ans)%2){ x--; } swap(p[x],p[x+1]); ans.PB(x); } /*for(int i:ans){ swap(pp[i],pp[i+1]); REP(j,n){ printf("%d ",pp[j]); }puts(""); }*/ printf("%d\n",SZ(ans)); REP(i,SZ(ans)){ if(i)printf(" "); printf("%d",ans[i]+1); }puts("");case_n++; } }
#include <bits/stdc++.h> #define rep(i, n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; const int INF = 1 << 30; const ll LLINF = 1LL << 60; int mod = 1000000007; ll dp[100][2]; int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; ll X; cin >> X; vector<ll> A(N); rep(i, N) cin >> A[i]; vector<ll> x(N); for(int i = N-1; i >= 0; i--){ x[i] = X/A[i]; X %= A[i]; } dp[0][0] = 1; for(int i = 0; i < N; i++){ ll kuri = (i == N-1) ? LLINF : A[i+1]/A[i]; for(int j = 0; j <= 1; j++){ if((x[i]+j) % kuri == 0){ //y == 0 <=> z == 0 int nj = (x[i]+j)/kuri; dp[i+1][nj] += dp[i][j]; } else{ dp[i+1][0] += dp[i][j]; dp[i+1][1] += dp[i][j]; } } } cout << dp[N][0] << endl; return 0; }
/** * Dont raise your voice, improve your argument. * --Desmond Tutu */ #include <bits/stdc++.h> using namespace std; const bool ready = [](){ ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(12); return true; }(); using ld=long double; const ld PI = acos((ld)-1); using ll= long long; #define int ll #define all(v) (v).begin(), (v).end() #define fori(n) for(int i=0; i<int(n); i++) #define cini(i) int i; cin>>i; #define cins(s) string s; cin>>s; #define cind(d) ld d; cin>>d; #define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; } #define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; } #define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; } using pii= pair<int, int>; using pdd= pair<ld, ld>; using vd= vector<ld>; using vb= vector<bool>; using vi= vector<int>; using vvi= vector<vi>; using vs= vector<string>; #define endl "\n" int isqrt(int i) { return (int)sqrt(i); } /* Its some kind of dijkstra. * But we cannot iterate all possible waiting times on all edges :/ * But, the earliest time to pass an edge seems to be sqrt(D[i]) * after begin, so it never makes sense to wait longer. * So we need to iterate only that edges where t is smaller * than sqrt(D[i]). * * How to get rid of TLE? */ const int INF=1e18; void solve() { cini(n); cini(m); vector<vector<pii>> adj(n); vi C(m); vi D(m); for(int i=0; i<m; i++) { cini(a); a--; cini(b); b--; adj[a].emplace_back(b,i); adj[b].emplace_back(a,i); cin>>C[i]; cin>>D[i]; } vi dp(n, INF); /* dp[i]=min time possible arriving at vertex i */ priority_queue<pii> q; /* <-mintime,vertex> */ q.emplace(0, 0); dp[0]=0; while(q.size()) { auto [t,v]=q.top(); q.pop(); if(-t!=dp[v]) continue; for(pii chl : adj[v]) { /* without waiting */ int mii=dp[v]+C[chl.second]+D[chl.second]/(dp[v]+1); int optWait=max(dp[v], isqrt(D[chl.second])-1); mii=min(mii, optWait+C[chl.second]+D[chl.second]/(optWait+1)); optWait++; mii=min(mii, optWait+C[chl.second]+D[chl.second]/(optWait+1)); if(mii<dp[chl.first]) { dp[chl.first]=mii; q.emplace(-mii, chl.first); } } } if(dp[n-1]==INF) dp[n-1]=-1; cout<<dp[n-1]<<endl; } signed main() { solve(); }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<n; i++) #define reps(i,s,n) for(int i=s; i<n; i++) #define per(i,n) for(int i=n-1; i>=0; i--) #define pers(i,n,s) for(int i=n-1; i>=s; i--) #define all(v) v.begin(),v.end() #define fi first #define se second #define pb push_back #define si(v) int(v.size()) #define lb(v,n) lower_bound(all(v),n) #define lbi(v,n) int(lower_bound(all(v),n) - v.begin()) #define ub(v,n) upper_bound(all(v),n) #define ubi(v,n) int(upper_bound(all(v),n) - v.begin()) #define mod 1000000007 #define infi 1010000000 #define infl 1100000000000000000 #define outve(v) for(auto i : v) cout << i << " ";cout << endl #define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;} #define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];} #define IN(n,m,v) rep(i,n) rep(j,m){cin >> v[i][j];} #define cyes cout << "Yes" << endl #define cno cout << "No" << endl #define cYES cout << "YES" << endl #define cNO cout << "NO" << endl #define csp << " " << #define outset(n) cout << fixed << setprecision(n); using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using vi = vector<int>; using vvi = vector<vector<int>>; using vd = vector<double>; using vvd = vector<vector<double>>; using vl = vector<ll>; using vvl = vector<vector<ll>>; using vs = vector<string>; using pii = pair<int,int>; using pll = pair<ll,ll>; template<typename T> using ve = vector<T>; template<typename T> using pq2 = priority_queue<T>; template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>; template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;} template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;} template <typename T> T extgcd(T a, T b, T& x, T& y){ T d = a; if(b != 0){ d = extgcd(b,a%b,y,x); y-=(a/b)*x; } else x=1,y=0; return d; } template <typename T> T mod_pow(T m, T n) { if(n == 0) return 1; T res = mod_pow(m*m%mod, n/2); if(n & 1) res *= m; return res%mod; } template <typename T> T mod_inv(T n) { T m = mod; T x,y; extgcd(n,m,x,y); return (m+x%m)%m; } void solve(){ int N,M,K; cin >> N >> M >> K; vl A(N); in(N,A); vvl P(N,vl(N,0)); rep(i,N) P[i][i] = 1; ll mm = mod_inv<ll>((ll)(M*2)); rep(i,M){ int x,y; cin >> x >> y; x--,y--; P[x][x] += mod; P[y][y] += mod; (P[x][x] -= mm) %= mod; (P[x][y] += mm) %= mod; (P[y][x] += mm) %= mod; (P[y][y] -= mm) %= mod; } ve<vvl> vP; vP.pb(P); rep(i,30){ vvl p = vP.back(); vvl pp(N,vl(N,0)); rep(i,N) rep(j,N) rep(k,N) (pp[i][j] += p[i][k]*p[k][j]%mod) %= mod; vP.pb(pp); } vl ans = A; int id = 0; while (K > 0) { if(K & 1){ A = ans; ans.assign(N,0); rep(i,N) rep(j,N) (ans[i] += vP[id][i][j]*A[j]%mod) %= mod; } id++; K = K >> 1; } rep(i,N) cout << ans[i] << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
#include <iostream> using namespace std; using LL = long long; const int kMaxN = 101; const LL kMod = 1e9 + 7; int n, m, k; LL d[kMaxN], a[kMaxN], r[kMaxN][kMaxN], p_n = 1, b[kMaxN][kMaxN], c[kMaxN][kMaxN]; bool l[kMaxN][kMaxN]; LL PM(LL x) { LL s = 1; for (LL i = kMod, b = x; i; i >>= 1, b = b * b % kMod) { if (i & 1) { s = s * b % kMod; } } return s; } void M(LL a[kMaxN][kMaxN], LL b[kMaxN][kMaxN]) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { c[i][j] = 0; for (int k = 1; k <= n; ++k) { c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % kMod; } } } } int main() { cin >> n >> m >> k; for (LL i = kMod - 2, b = 2 * m; i; i >>= 1, b = b * b % kMod) { if (i & 1) { p_n = p_n * b % kMod; } } for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1, x, y; i <= m; ++i) { cin >> x >> y; l[x][y] = l[y][x] = 1; ++d[x], ++d[y]; } for (int i = 1; i <= n; ++i) { b[i][i] = 1; for (int j = 1; j <= n; ++j) { if (i == j) { r[i][j] = (2 * m - d[i]) * p_n % kMod; } else if (l[i][j]) { r[i][j] = p_n; } } } for (int i = k; i; i >>= 1) { if (i & 1) { M(r, b); copy(&c[1][1], &c[n][n] + 1, &b[1][1]); } M(r, r); copy(&c[1][1], &c[n][n] + 1, &r[1][1]); } for (int i = 1; i <= n; ++i) { LL s = 0; for (int j = 1; j <= n; ++j) { s = (s + a[j] * b[j][i]) % kMod; } cout << s << endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int N, Q; cin >> N >> Q; vector<long long> A(N); for (auto& x : A) { cin >> x; } vector<long long> low(N); for (int i = 0; i < N; ++i) { low[i] = A[i] - (i + 1); } while (Q--) { long long K; cin >> K; const int idx = lower_bound(low.begin(), low.end(), K) - low.begin(); if (idx == N) { cout << A[N - 1] + (K - low[N - 1]) << '\n'; } else { cout << A[idx] - (low[idx] - K + 1) << '\n'; } } return 0; }
#include <algorithm> #include <cmath> #include <iostream> #include <map> #include <set> #include <string> #include <unordered_map> #include <vector> using namespace std; using LL = long long; using ULL = unsigned long long; using IntPair = pair<int, int>; using ULLPair = pair<ULL, ULL>; #define scast static_cast #define repeat( itr, init, count ) \ for ( LL itr = ( init ); itr < ( count ); ++itr ) #define repeatu( itr, init, count ) \ for ( ULL itr = ( init ); itr < ( count ); ++itr ) ULL CountOverNums( const vector<ULL> *A, ULL key ) { auto itr = upper_bound( A->begin(), A->end(), key ); if ( itr == A->end() ) { return scast<ULL>( A->size() ); } // else size_t pos = distance( A->begin(), itr ); ULL count = scast<ULL>( pos ); if ( *itr == key ) { count++; } return count; } int main() { int N, Q; cin >> N >> Q; // 1, 2, 3, ... vector<ULL> A; A.resize( N ); ULL startNum = 0; { ULL tmp; ULL sliceLeft = 0; repeatu( i, 0, N ) { cin >> tmp; if ( startNum+1 == tmp ) { startNum++; } A[i] = tmp - sliceLeft; sliceLeft++; } } vector<ULL> K; K.resize( Q ); { ULL tmp; repeatu( i, 0, Q ) { cin >> tmp; // K[i] = startNum + tmp + CountOverNums( &A, tmp ); K[i] = tmp + CountOverNums( &A, tmp ); } } repeatu( i, 0, Q ) { cout << K[i] << endl; } //int _pause_thread_ = 0; cin >> _pause_thread_; return 0; }
#include <bits/stdc++.h> #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*b)/gcd(a,b) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) #define vint vector<int> #define pb push_back #define pf push_front #define ppb pop_back #define all(v) v.begin(),v.end() #define pii pair<int,int> #define pic pair<int,char> #define fr first #define sc second #define mp make_pair #define inarr(a,n) for(ll i=0;i<n;i++)cin>>a[i]; #define PI 3.1415926535897 #define time_passed 1.0 * clock() / CLOCKS_PER_SEC using namespace std; double power(double a,int b) {if(b==0){return 1;}else if(b%2==0){return power(a*a,b/2);}else if(b%2==1){return a*power(a*a,(b-1)/2);}} //bool cmp(a[l] , a[r]){return b[1][l]<b[1][r];} template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;} template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;} template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;} double f(double x,double Nu,double k){return power(x,k)-Nu;} double fd(double x,double Nu,double k){return k*pow(x,k-1);} double root(int Nu,int k){double ans=9;for(int i=1;i<20;i++){ans=ans-f(ans,Nu,k)/fd(ans,Nu,k);cout<<ans<<" ";}return ans;} const ll mod=power(2,32); const int N=3e5+5; const ll inf=1e9+7; int dy[]={-1,0,1,0}; int dx[]={0,1,0,-1}; vector <int >adj[N]; //int vis[201][201]; //int dist[N]; void love_for_infinity() { float a,b,c,d; cin>>a>>b>>c>>d; cout<<fixed<<(b*(c-a)/(d+b))+a<<setprecision(9); } int main() { IOS //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #ifdef SIEVE sieve(); #endif #ifdef NCR init(); #endif ll t;t=1;//cin>>t; while(t--){love_for_infinity();cout<<endl;} return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define forn(i,x,n) for(int i = x;i <= n;++i) #define forr(i,x,n) for(int i = n;i >= x;--i) #define Angel_Dust ios::sync_with_stdio(0);cin.tie(0) int main() { int a,b;scanf("%d%d",&a,&b); printf("%.18lf\n",a * b / 100.0); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vector<int> a(n), b(n); for(int& x: a) cin>>x; for(int&x : b) cin>>x; int res = 0; for(int i = 0; i < n; i++) res += a[i] * b[i]; cout<<(res == 0 ? "Yes\n" : "No\n"); }
#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <cstring> #include <chrono> #include <complex> #define REP(i,a,b) for (auto i = a; i != b; i++) #define maxheap priority_queue < ll, std::vector<ll>, std::less<ll> > #define minheap priority_queue < ll, std::vector<ll>, std::greater<ll> > // mxheap.push(), mxheap.top(), mxheap.pop() #define ll long long int #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 f first #define s second #define pb push_back #define SQ(a) (a)*(a) using namespace std; // using namespace FastIO; void read(int n,vector<int>& x) { x.clear(); x.resize(n); for(int i = 0;i<n;i++) { cin>>x[i]; } } void read(int n,int m,vector<vector<int>>& x) { x.clear(); x.resize(n,vector<int>(m)); for(int i = 0;i<n;i++) { for(int j = 0 ;j<m;j++) cin>>x[i][j]; } } void read(int n,vector<vector<int>>& x) { x.clear(); x.resize(n+1); for(int i = 0;i<n-1;i++) { int a,b; cin>>a>>b; x[a].pb(b); x[b].pb(a); } } void read(int n,vector<vector<int>>& x,int m) { x.clear(); x.resize(n+1); for(int i = 0;i<m;i++) { int a,b; cin>>a>>b; x[a].pb(b); x[b].pb(a); } } void read(int n,vector<ll>& x) { x.clear(); x.resize(n); for(int i = 0;i<n;i++) { cin>>x[i]; } } int main() { std::ios::sync_with_stdio(false); int T = 1; // freopen("mixmilk.in", "r", stdin); // freopen("mixmilk.out", "w", stdout); //cin>>T; for(int t = 1;t<=T;t++) { // cout<<"Case #"<<t<<": "; int n,m; cin>>n; vector<int> a; vector<int> b; read(n,a); read(n,b); ll sum = 0; for(int i = 0;i<n;i++){ sum += a[i] * b[i]; } if(sum == 0){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } return 0; }
#include <bits/stdc++.h> #define int long long #define pb push_back #define vi vector<int> #define vii vector<vi> #define viii vector<vii> #define all(a) a.begin(), a.end() #define sz(a) a.size() const int mod=998244353; using namespace std; void solve() { int n; cin >> n; vi w(n + 1); int summ = 0; for (int i = 1; i <= n; i++) { cin >> w[i]; summ += w[i]; } if (summ % 2==1) cout << 0; else { summ /= 2; vi fact(n + 1); fact[0] = 1, fact[1] = 1; for (int i = 2; i <= n; i++) { fact[i] = i * fact[i - 1]; fact[i] %= mod; } int ans = 0, curr; viii dp(n + 1, vii(summ + 1, vi(n + 1, 0))); dp[0][0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j <= summ; j++) { for (int k = 0; k <= i; k++) { dp[i][j][k] = dp[i - 1][j][k]; dp[i][j][k] %= mod; if (w[i] <= j && k > 0) { dp[i][j][k] += dp[i - 1][j - w[i]][k - 1]; dp[i][j][k] %= mod; } if (i == n && j == summ) { curr = dp[i][j][k] * fact[k]; curr %= mod; curr = curr * fact[n - k]; curr %= mod; ans += curr; ans %= mod; } } } } cout << ans; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> using std::sort; using std::cout; using std::endl; using std::max; using std::min; inline int read(){ int h=0;char c=getchar(); while(c<'0'||c>'9')c=getchar(); while(c>='0'&&c<='9')h=(h<<1)+(h<<3)+c-'0',c=getchar(); return h; } const int p=998244353; inline int add(int x,int y){return x+y>=p?x+y-p:x+y;} //long long ksm()/ const int MAXN=110; int n; int f[2][110][11111]; int nw=0,lst=1; //前i个物品,A有j个,B有i-j个,A-B为w的方案数。 int id(int x){return x+5001;} int a[MAXN]; long long jc[MAXN]; //int jcn[MAXN]; int main(){ jc[0]=1;for(int i=1;i<=100;i++)jc[i]=jc[i-1]*i%p; n=read();for(int i=1;i<=n;i++)a[i]=read(); f[0][0][id(0)]=1; for(int i=1;i<=n;i++){ std::swap(nw,lst); memset(f[nw],0,sizeof(f[nw])); for(int j=0;j<=i;j++){ for(int d=-5000;d<=5000;d++){ if(d+a[i]<=5000)f[nw][j+1][id(d+a[i])]=add(f[nw][j+1][id(d+a[i])],f[lst][j][id(d)]); if(d-a[i]>=-5000)f[nw][j][id(d-a[i])]=add(f[nw][j][id(d-a[i])],f[lst][j][id(d)]); } } } int ans=0; for(int i=0;i<=n;i++){ ans=(ans+f[nw][i][id(0)]*jc[i]%p*jc[n-i])%p; } printf("%d\n",ans); return 0; }
# include <bits/stdc++.h> #define rep(i,l,r)for(int i=(l);i<(r);i++) # define rrep(i,r,l) for(int i=(r); i>=l; --i) # define ALL(x) (x).begin(), (x).end() # define SZ(x) ((int)(x).size()) # define pb push_back # define optimize_cin() cin.tie(0); ios::sync_with_stdio(false) using namespace std; typedef long long lint; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } #define INF ((1LL<<62)-(1LL<<31)) /*オーバーフローしない程度に大きい数*/ #define MOD 1000000007 lint gcd(lint a,lint b){ if(!b)return a; return gcd(b,a%b); } lint lcm(lint a,lint b){ return a*b/gcd(a,b); } struct node{ int parent,cost; }; bool comp(pair<lint,lint> p,pair<lint,lint> q){ return p.first-p.second > q.first - q.second; } void solve() { lint N; cin >> N; vector<pair<lint,lint>> A(N); lint tmp =0; rep(i,0,N){ lint a,b; cin >> a >> b; A[i].first=a+b; A[i].second=-a; tmp+=A[i].second; } sort(A.begin(),A.end(),comp); lint ans=0; int i=0; while(tmp<=0){ tmp+=A[i].first-A[i].second; i++; ans++; } cout << ans << endl; } signed main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define ll long long bool comp(pair<ll, ll> a, pair<ll, ll> b) { return 2 * a.first + a.second > 2 * b.first + b.second; } int main() { int N; ll a, b, sumA = 0, sumB = 0; vector<pair<ll, ll>> C; cin >> N; for (int i = 0; i < N; ++i) { cin >> a >> b; sumA += a; C.emplace_back(a, b); } sort(C.begin(), C.end(), comp); int ans = 0; for (auto[a, b]: C) { if (sumB > sumA) break; sumB += a + b; sumA -= a; ans++; } cout << ans << endl; }
#include<bits/stdc++.h> using namespace std; int main() { int a,b,c; cin>>a>>b>>c; cout<<21-a-b-c; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define mem(a,val) memset(a,(val),sizeof((a))) #define FAST std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define decimal(n) cout << fixed ; cout << setprecision((n)); #define mp make_pair #define eb emplace_back #define f first #define s second #define all(v) v.begin(), v.end() #define endl "\n" #define lcm(m,n) (m)*((n)/__gcd((m),(n))) #define rep(i,n) for(ll (i)=0;(i)<(n);(i)++) #define rep1(i,n) for(ll (i)=1;(i)<(n);(i)++) #define repa(i,n,a) for(ll (i)=(a);(i)<(n);(i)++) #define repr(i,n) for(ll (i)=(n)-1;(i)>=0;(i)--) #define pll pair<ll,ll> #define pii pair<int, int> #define mll map<ll,ll> #define vll vector<ll> #define sz(x) (ll)x.size() #define ub upper_bound #define lb lower_bound #define pcnt(x) __builtin_popcountll(x) const long long N=1e9; const long long NN=1e18; const int32_t M=1e9+7; const int32_t MM=998244353; template<typename T,typename T1>T maxn(T &a,T1 b){if(b>a)a=b;return a;} template<typename T,typename T1>T minn(T &a,T1 b){if(b<a)a=b;return a;} void solve() { //code begins from here// ll a,b,c; cin>>a>>b>>c; ll ans=21-a-b-c; cout<<ans; } signed main() { FAST //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int testcase=1; //cin>>testcase; while(testcase--) solve(); return 0; }
//~ author : Sumit Prajapati #include <bits/stdc++.h> using namespace std; #define ull unsigned long long #define ll long long #define int long long #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define mk make_pair #define ff first #define ss second #define all(a) a.begin(),a.end() #define trav(x,v) for(auto &x:v) #define rep(i,n) for(int i=0;i<n;i++) #define repe(i,n) for(int i=1;i<=n;i++) #define read(a,n) rep(i,n)cin>>a[i] #define reade(a,n) repe(i,n)cin>>a[i] #define FOR(i,a,b) for(int i=a;i<=b;i++) #define printar(a,s,e) FOR(i,s,e)cout<<a[i]<<" ";cout<<'\n' #define curtime chrono::high_resolution_clock::now() #define timedif(start,end) chrono::duration_cast<chrono::nanoseconds>(end - start).count() auto time0 = curtime; const int MD=1e9+7; const int MDL=998244353; const int INF=1e9; const int MX=1e6+2; int LPF[MX]; int mobius[MX]; int ffR[MX]; int ffL[MX]; int l,r; void init(){ for (int i = 2; i < MX; i++) // If it is a prime number if (!LPF[i]) for (int j = i; j < MX; j += i) // For all multiples which are not // visited yet. if (!LPF[j]) LPF[j] = i; mobius[1]=1LL; for (int i = 2LL; i < MX; i++) { if (LPF[i / LPF[i]] == LPF[i]) mobius[i] = 0; else mobius[i] = -1LL * mobius[i / LPF[i]]; } for(int i=1LL;i<MX;i++) for(int j=i;j<MX;j+=i){ ffR[j]+=(mobius[i]*(r/i)); ffL[j]+=(mobius[i]*((j-1)/i)); } } int count(int x){ int divis=r/x; // cout<<divis<<" " // 7-(1+1) // 7 8 9 10 5-(5+()) return (r-x+1LL)-(divis+ffR[x]-ffL[x]); } void solve(){ int ans=0; cin>>l>>r; init(); for(int x=max(2LL,l);x<=r;x++){ // cout<<x<<" -> "<<ffL[x]<<" "<<ffR[x]<<" "<<count(x)<<'\n'; ans+=count(x); } ans<<=1LL; cout<<ans<<'\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); time0 = curtime; srand(time(NULL)); int t=1; // cin>>t; repe(tt,t){ // cout<<"Case #"<<tt<<": "; solve(); } cerr<<"Execution Time: "<<timedif(time0,curtime)*1e-9<<" sec\n"; return 0; }
#include <cstdio> #include <algorithm> #define int long long const int N = 1000005; int l, r, f[N], ans; signed main() { scanf("%lld%lld", &l, &r); for(int i = r; i >= 1; i--) { int an = r/i - (l-1)/i; f[i] = an * an; for(int j = i+i; j <= r; j += i) f[i] -= f[j]; } for(int i = 2; i <= r; i++) { ans += f[i]; if(i >= l) ans -= 2*(r/i - (l-1)/i) - 1; } printf("%lld", ans); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)n;++i) #define irep(i,a,b) for(int i=int(a);i<(int)b;++i) #define rrep(i,a,b) for(int i=int(a);i>=(int)b;--i) #define vi vector<int> #define vvi vector<vector<int>> #define vl vector<ll> #define vvl vector<vector<ll>> #define vvp vector<vector<pair<ll,ll>>> #define vpl vector<pair<ll,ll>> #define vpi vector<pair<int,int>> #define pb push_back #define se second #define fi first #define all(v) v.begin(),v.end() #define v(T) vector<T> #define vv(T) vector<vector<T>> using namespace std; template<typename T> istream& operator>>(istream&i,v(T)&v){rep(j,v.size())i>>v[j];return i;} template<typename T> string join(const v(T)&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);} template<typename T> ostream& operator<<(ostream&o,const v(T)&v){if(v.size())o<<join(v);return o;} using ll = long long; const ll INF = 1e18; const double PI = acos(-1); const ll mod = 1e9 + 7; //998244353; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } ll modpow(ll a,ll b){ if(b == 0){ return 1; } if(b%2 == 0){ ll tmp = modpow(a,b/2); return tmp*tmp%mod; }else{ return modpow(a,b-1)*a%mod; } } vvl g; vl a; bool done[1000000]; ll dp[1000000]; ll dfs(ll x){ if(done[x])return dp[x]; done[x] = true; ll num = -INF; for(ll k : g[x]){ ll tmp = max(a[k],dfs(k)); chmax(num,tmp); } return dp[x] = num; } int main(void) { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); ll n,m; cin >> n >> m; g.resize(n); a.resize(n); cin >> a; rep(i,m){ int x,y; cin >> x >> y; --x;--y; g[x].pb(y); } ll ans = -INF; rep(i,n){ ll benefit = dfs(i)-a[i]; chmax(ans,benefit); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan0     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int L, R; ll cnt[1010101]; //--------------------------------------------------------------------------------------------------- void _main() { cin >> L >> R; rrep(g, 1010100, 2) { cnt[g] = 1LL * (R / g - (L - 1) / g) * (R / g - (L - 1) / g); int x = 2 * g; while (x <= R) { cnt[g] -= cnt[x]; x += g; } } ll ans = 0; rep(g, 2, 1010101) { ans += cnt[g]; if (L <= g && g <= R) { ans -= R / g - (L - 1) / g; ans -= R / g - (L - 1) / g; ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG using namespace std; using Graph = vector<vector<int>>; const long long INF = 1LL << 60; typedef long long ll; const ll MOD = 998244353; int main() { //cout << std::fixed << std::setprecision(15) ; bool frg = false; vector<int> data(4); for(int i=0;i<4;i++){ cin >> data[i]; } // vector で取り込めばsortを使用できるので圧倒的にこちらの解答の方が楽 sort(data.begin(),data.end()); // sort するのでif文だけでできる if(data[0]+data[3]==data[2]+data[1]||data[0]+data[2]==data[1]+data[3]||data[3]==data[0]+data[1]+data[2]) frg = true; if(frg) cout << "Yes" << endl; else cout << "No" << endl; }
#include <iostream> #include <vector> #include <utility> #include <queue> #include <vector> #include <list> #include <cmath> #include <deque> #include <utility> #include <climits> #include <unordered_map> #include <unordered_set> #include <cstdlib> #include <string> #include <stack> #include <set> #include <functional> #include <iterator> #include <algorithm> using namespace std; typedef long long ll; class UnionFind { private: vector<int> f; int _components; public: explicit UnionFind(int sz): f(sz), _components(sz) { for (int i = 0; i < sz; i++) { f[i] = i; } } int find(int x) { if (x == f[x]) return x; int fa = f[x]; return f[x] = find(fa); } void add(int x, int y) { int fx = find(x), fy = find(y); if (fx != fy) { _components--; } f[fx] = fy; } int components() const { return _components; } }; ll gcd(ll a, ll b) { if (b == 0)return a; else return gcd(b, a%b); } int main(int argc, char *argv[]) { std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll n; cin >> n; vector<int> digits; while (n > 0) { digits.push_back(n % 10); n/=10; } reverse(digits.begin(), digits.end()); int r = digits.size() - 1; while (r >= 0 && digits[r] == 0) { r--; } bool flag = true; int l = 0; while (l < r) { if (digits[l] != digits[r]) { flag = false; break; } else { l++; r--; } } if (flag) { cout << "Yes" << endl; } else { cout << "No" << endl; } }