code_file1
stringlengths
87
4k
code_file2
stringlengths
82
4k
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; // -------------------------------------------------------- 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 FOR(i,l,r) for (ll i = (l); i < (r); ++i) #define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) RFOR(i,0,n) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define MIN(c) *min_element(ALL(c)) #define MAX(c) *max_element(ALL(c)) #define SUMLL(c) accumulate(ALL(c), 0LL) #define COUNT(c,v) count(ALL(c),(v)) #define SZ(c) ((ll)(c).size()) #define BIT(b,i) (((b)>>(i)) & 1) #define PCNT(b) __builtin_popcountll(b) #define CIN(c) cin >> (c) #define COUT(c) cout << (c) << '\n' #define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n' ll llceil(ll a, ll b) { return (a + b - 1) / b; } ll bitlen(ll b) { if (b <= 0) { return 0; } return (64LL - __builtin_clzll(b)); } string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; } string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; } using P = pair<ll,ll>; using VP = vector<P>; using VVP = vector<VP>; using VS = vector<string>; using VVS = vector<VS>; using VLL = vector<ll>; using VVLL = vector<VLL>; using VVVLL = vector<VVLL>; using VB = vector<bool>; using VVB = vector<VB>; using VVVB = vector<VVB>; using VD = vector<double>; using VVD = vector<VD>; using VVVD = vector<VVD>; using VLD = vector<ld>; using VVLD = vector<VLD>; using VVVLD = vector<VVLD>; static const double EPS = 1e-10; static const double PI = acos(-1.0); static const ll MOD = 1000000007; // static const ll MOD = 998244353; static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1 // -------------------------------------------------------- // #include <atcoder/all> // using namespace atcoder; // Editorial AC int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); ll N; cin >> N; VLL A(N); REP(i,N) cin >> A[i]; VLL B(N); REP(i,N) cin >> B[i]; VLL E,O; // Even,Odd REP(i,N) { if (i % 2 == 0) { E.push_back(B[i] - A[i]); } else { O.push_back(B[i] - A[i]); } } RSORT(E); RSORT(O); ll score = SUMLL(A); ll ans = score; REP(i,N/2) { score += E[i] + O[i]; chmax(ans, score); } COUT(ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); ++i) using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> a(2 * n); for(auto &e : a) cin >> e; vector<P> ps; rep(i, 2 * n) ps.emplace_back(a[i], i); sort(ps.rbegin(), ps.rend()); vector<int> p(2 * n, -1); rep(i, n) p[ps[i].second] = 1; string ans(2 * n, 'x'); int l = 0, r = 0; int cnt_p = 0, cnt_m = 0; string st = ""; rep(i, 2 * n) { if(st.empty()) { st +='('; ans[i] = '('; l++; if(p[i] == 1) cnt_p++; else cnt_m++; } else { if(cnt_p) { if(p[i] == 1) { cnt_p++; st += '('; ans[i] = '('; } else { cnt_p--; st.pop_back(); ans[i] = ')'; } } else if(cnt_m) { if(p[i] == -1) { cnt_m++; st += '('; ans[i] = '('; } else { cnt_m--; st.pop_back(); ans[i] = ')'; } } } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using vll = vector<ll>; using vi = vector<int>; using vb = vector<bool>; using vs = vector<string>; using pll = pair<ll, ll>; using pii = pair<int, int>; using vpii = vector<pii>; using vpll = vector<pll>; const ll LINF = 1ll << 55; const ll INF = 0x3f3f3f3f; const ll MOD = 1e9 + 7; const int MAXN = 1e5 + 10; const ll dx[] = {1, 0, -1, 0}; const ll dy[] = {0, 1, 0, -1}; /// cin/cout overloading template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) { for (auto it = vec.begin(); it != vec.end(); ++it) { out << *it << " "; } return out; } template <typename T> ostream &operator<<(ostream &out, const pair<T, T> &P) { out << P.first << " " << P.second; return out; } template <typename T> istream &operator>>(istream &in, vector<T> &vec) { for (auto it = vec.begin(); it != vec.end(); ++it) { in >> *it; } return in; } template <typename T> istream &operator>>(istream &in, pair<T, T> &P) { in >> P.first >> P.second; return in; } #define error(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ cerr << endl; \ } void err(istream_iterator<string>) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); } #define rep(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define all(x) (x).begin(), (x).end() #define coutp(x, y) cout << (x) << " " << (y) << endl #define coutn(x) cout << (x) << endl #define coutd(x) cout << setprecision(10) << (x) << endl int argmax(const vi &A, int start, int end) { int res = 0; int max = 0; rep(i, start, end) { if (A[i] > max) { max = A[i]; res = i; } } return res; } /// main函数 int main() { ll N, C; cin >> N >> C; vpll pay; ll ai, bi, ci; rep(i, 0, N) { cin >> ai >> bi >> ci; pay.emplace_back(ai - 1, ci); pay.emplace_back(bi, -ci); } sort(pay.begin(), pay.end()); ll ans = 0, p = 0, curr = 0; for (auto &[v, c] : pay) { ans += min(C, curr) * (v - p); p = v; curr += c; } coutn(ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (n); i++) #define endl "\n" typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;} template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;} void solve() { int X, Y, Z; cin >> X >> Y >> Z; // t / Z < Y / X // t * X < Y * Z if ((Y * Z) % X == 0) { cout << Y * Z / X - 1 << endl; } else { cout << Y * Z / X << endl; } } int main() { #ifdef LOCAL_ENV cin.exceptions(ios::failbit); #endif cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); }
//ver 8.1 #include <bits/stdc++.h> using namespace std; void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);} using ll = long long; using ld = long double; using vl = vector<ll>; using vd = vector<ld>; using vs = vector<string>; using vb = vector<bool>; using vvl = vector<vector<ll>>; using vvd = vector<vector<ld>>; using vvs = vector<vector<string>>; using vvb = vector<vector<bool>>; using pll = pair<ll,ll>; using mll = map<ll,ll>; template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; #define each(x,v) for(auto& x : (v)) #define reps(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++) #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define pb push_back #define eb emplace_back #define fi first #define se second #define mp make_pair const ll INF = 1LL << 60; #define CLR(mat,f) memset(mat, f, sizeof(mat)) #define IN(a, b, x) ((a)<=(x)&&(x)<=(b)) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除 #define debug cout << "line : " << __LINE__ << " debug" << endl; #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ind(...) long double __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}} template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}} template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}} template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}} template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}} void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);} void die(){cout << endl;exit(0);} template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);} template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;} template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}} template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}} template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}} template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}} template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}} #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;} template<typename T> bool inside(T a,T b){auto it=a.begin()-1;each(x,b){it=find(it+1,a.end(),x);if(it==a.end())return false;}return true;} ll ceilDiv(ll a,ll b) {return (a+b-1)/b;} ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離 ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a,ll b){ return a / gcd(a,b) * b;} template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } #define YES(n) ((n) ? "YES" : "NO" ) #define Yes(n) ((n) ? "Yes" : "No" ) #define yes(n) ((n) ? "yes" : "no" ) int main(){ init(); inl(n); ins(s); ins(t); ll ans=0; V<ll> a,b; rep(i,n)if(s[i]=='0')a.push_back(i); rep(i,n)if(t[i]=='0')b.push_back(i); if(a.size()!=b.size())die(-1); rep(i,a.size())if(a[i]!=b[i])ans++; out(ans); return 0; }
#include<bits/stdc++.h> using namespace std; int n,b[404040]; pair<int,int>a[404040]; stack<pair<int,int> >st; int main() { string ans=""; cin>>n; for(int i=0;i<2*n;i++) { cin>>a[i].first; a[i].second=i; ans+='('; } sort(a,a+2*n); for(int i=0;i<n;i++) { b[a[i].second]=1; } for(int i=0;i<2*n;i++) { if(st.size()==0 || st.top().first==b[i]) { st.push(make_pair(b[i],i)); } else { st.pop(); ans[i]=')'; } } cout<<ans<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long LL; typedef unsigned long long uLL; typedef pair<int,int> pii; typedef pair<double,double> pdd; const int N=2e5+5; const int M=1e5+5; const int inf=0x3f3f3f3f; const int mod=1e9+7; const double eps=1e-8; const long double pi=acos(-1.0L); #define ls (i<<1) #define rs (i<<1|1) #define fi first #define se second #define pb push_back #define eb emplace_back #define mk make_pair #define mem(a,b) memset(a,b,sizeof(a)) LL read() { LL x=0,t=1; char ch; while((ch=getchar())<'0'||ch>'9') if(ch=='-') t=-1; while(ch>='0'&&ch<='9'){ x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); } return x*t; } vector<LL> a[3]; vector<pair<LL,int> >c; int id(char ch) { if(ch=='R') return 0; if(ch=='G') return 1; return 2; } int main() { int n=read(); for(int i=1;i<=2*n;i++) { LL x=read(); int s[2]; scanf("%s",s); a[id(s[0])].eb(x); } int tot=0; for(int i=0;i<3;i++) { if((int)a[i].size()&1) { for(auto x:a[i]) c.eb(x,tot); tot++; } } if(c.empty()) return 0*printf("0\n"); sort(c.begin(),c.end()); LL ans=1e16; for(int i=1;i<(int)c.size();i++) if(c[i].se!=c[i-1].se) ans=min(ans,c[i].fi-c[i-1].fi); for(int i=0;i<3;i++) { if((int)a[i].size()&1) continue; for(auto x:a[i]) c.eb(x,tot); } sort(c.begin(),c.end()); LL t1=1e16,t2=1e16; for(int i=0;i<(int)c.size();i++) if(c[i].se==2) { if(i>0&&c[i-1].se==0) t1=min(t1,c[i].fi-c[i-1].fi); if(i<(int)c.size()-1&&c[i+1].se==0) t1=min(t1,c[i+1].fi-c[i].fi); if(i>0&&c[i-1].se==1) t2=min(t2,c[i].fi-c[i-1].fi); if(i<(int)c.size()-1&&c[i+1].se==1) t2=min(t2,c[i+1].fi-c[i].fi); } // printf("%lld %lld\n",t1,t2); printf("%lld\n",min(ans,t1+t2)); return 0; } /* 2 100 R 2 B 99 G 1 G */
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) #define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i]; #define ft first #define sc second #define pb push_back #define lb lower_bound #define ub upper_bound #define all(v) (v).begin(),(v).end() #define LB(a,x) lb(all(a),x)-a.begin() #define UB(a,x) ub(all(a),x)-a.begin() //#define mod 1000000007 #define mod 998244353 #define FS fixed<<setprecision(15) using namespace std; typedef long long ll; const double pi=3.141592653589793; template<class T> using V=vector<T>; using P=pair<ll,ll>; typedef unsigned long long ull; typedef long double ldouble; 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; } template<class T> inline void out(T a){ cout << a << '\n'; } void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;} //void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;} const ll INF=1e18; const int mx=200005; //arc114 ll dp[200005][4]; class UnionFind{ public: vector<ll> par,num; int find(ll v){ return (par[v]==v)? v: (par[v]=find(par[v])); } explicit UnionFind(ll N):par(N),num(N,1){ iota(par.begin(),par.end(),0); } void unite(ll u,ll v){ u=find(u),v=find(v); if(u==v)return ; if(num[u]<num[v])swap(u,v); num[u] += num[v]; par[v] = u; } bool same(ll u,ll v){ return find(u) == find(v); } bool ispar(ll v){ return v=find(v); } ll size(ll v){ return num[find(v)]; } }; int main(){ //オーバーフローは大丈夫ですか?? cin.tie(0);ios::sync_with_stdio(false); ll n; cin>>n; V<ll> a(n); V<V<ll>> G(n); UnionFind uf(n); rep(i,n){ cin>>a[i]; a[i]--; uf.unite(i,a[i]); } set<ll> st; rep(i,n) st.insert(uf.find(i)); ll x=st.size(); ll ans=1; rep(i,x) ans=ans*2%mod; ans--; if(ans<0) ans+=mod; out(ans); } //ペナルティ出しても焦らない ACできると信じろ!!! //どうしてもわからないときはサンプルで実験 何か見えてくるかも //頭で考えてダメなら紙におこせ!!
#include <bits/stdc++.h> #define max0(x,y) (x<y?y:x) #define min0(x,y) (x>y?y:x) using namespace std; const double eps=1e-14,down=0.9997; const int mn=3e3+7; int n; int w[mn][6],q1[4],q2[mn]; int fx() { int ans=1e9+7; for(int j=1;j<=5;++j) { int mx=0; for(int i=1;i<=3;++i) mx=max0(w[q1[i]][j],mx); ans=min0(ans,mx); } return ans; } int main() { srand(154051); scanf("%d",&n); for(int i=1;i<=n;++i) for(int j=1;j<=5;++j) scanf("%d",&w[i][j]); q1[1]=1;q1[2]=2;q1[3]=3; int cnt=0; for(int i=4;i<=n;++i) q2[++cnt]=i; int best,rs; best=rs=fx(); if(n==3) { cout<<best; return 0; } for(double t=13000;t>eps;t*=down) { int l=rand()%3+1,r=rand()%cnt+1; swap(q1[l],q2[r]); int tmp=fx(); if(tmp>best) best=tmp; if(tmp>rs||exp((double)(tmp-rs)/t)*RAND_MAX>rand()) rs=tmp; else swap(q1[l],q2[r]); } cout<<best; return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author */ #include <iostream> #include <fstream> #include <bits/stdc++.h> using namespace std; #define ll long long inline int in_int(istream& in) {int x; in >> x; return x;} inline ll in_ll(istream& in) {ll x; in >> x; return x;} inline string in_str(istream& in) {string x; in >> x; return x;} #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) class CMADTEAM { public: void solve(istream& in, ostream& out) { int N = in_int(in); // vector A(N, array<int, 5>{}); vector<array<int, 5>> A(N); for (int i = 0; i < N; ++i) { for (int j = 0; j < 5; ++j) { A[i][j] = in_ll(in); } } auto check = [&](int x) -> bool{ set<int> s; for (int i = 0; i < N; ++i) { int bit = 0; for (int j = 0; j < 5; ++j) { bit <<= 1; if(A[i][j] >= x){ bit |= 1; } } s.insert(bit); } for (int i : s) { for (int j : s) { for (int k : s) { if((i|j|k) == 31)return true; } } } return false; }; int ok = 0; int ng = 1e9 + 1; while(abs(ok - ng) > 1){ int cen = (ok + ng) / 2; (check(cen) ? ok : ng) = cen; } out << ok << endl; } }; int main() { CMADTEAM solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vs = vector<string>; using vb = vector<bool>; using pii = pair<int, int>; #define endl " \n" #define size(x) int((x).size()) #define all(a) a.begin(), a.end() const int mod = 1e9+7, inf = INT_MAX, ninf = INT_MIN; void setIO(string name = "") { cin.tie(0)->sync_with_stdio(0); if (!name.empty()) { freopen((name+".in").c_str(), "r", stdin); freopen((name+".out").c_str(), "w", stdout); } } void solve(); int main() { setIO(); solve(); return 0; } void solve() { int n, k; cin >> n >> k; int s = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { int temp = i*100+j; s += temp; } } cout << s << endl[1]; }
#include<bits/stdc++.h> using namespace std; int main(){ int N,K;cin>>N>>K; int ans=0; for(int i=1;i<=N;i++){ ans+=K*i*100; ans+=K*(K+1)/2; } cout<<ans<<endl; }
#include<bits/stdc++.h> #define ll long long using namespace std; const int MAXN=1e5+10; int k,n,m,a[MAXN],b[MAXN]; ll ans[2*MAXN],ans2[2*MAXN]; bool check(int mid){ int i,x,minb=0,maxb=0; for(i=1;i<=k;i++){ x=(ll)m*a[i]/n; if(ans[2*i-1]>ans2[mid]&&ans[2*i]>ans2[mid])return 0; if(ans[2*i-1]<=ans2[mid]&&ans[2*i]>ans2[mid]){ minb=minb+x; maxb=maxb+x; } if(ans[2*i-1]>ans2[mid]&&ans[2*i]<=ans2[mid]){ minb=minb+x+1; maxb=maxb+x+1; } if(ans[2*i-1]<=ans2[mid]&&ans[2*i]<=ans2[mid]){ minb=minb+x; maxb=maxb+x+1; } } if(minb<=m&&m<=maxb)return 1; else return 0; } inline int read(){ int sum=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=getchar(); return f*sum; } int main(){ k=read();n=read();m=read(); int i,x; for(i=1;i<=k;i++){ a[i]=read(); x=(ll)m*a[i]/n; ans[2*i-1]=abs((ll)n*x-(ll)m*a[i]); ans[2*i]=abs((ll)n*(x+1)-(ll)m*a[i]); } for(i=1;i<=2*k;i++)ans2[i]=ans[i]; sort(ans2+1,ans2+2*k+1); int l=1,r=2*k,mid; while(l<r){ mid=(l+r)>>1; if(check(mid))r=mid; else l=mid+1; } int minb=0; for(i=1;i<=k;i++){ x=(ll)m*a[i]/n; if(ans[2*i-1]<=ans2[l]&&ans[2*i]>ans2[l]){ minb=minb+x; b[i]=x; } if(ans[2*i-1]>ans2[l]&&ans[2*i]<=ans2[l]){ minb=minb+x+1; b[i]=x+1; } if(ans[2*i-1]<=ans2[l]&&ans[2*i]<=ans2[l])minb=minb+x; } int d=m-minb; for(i=1;i<=k;i++){ x=(ll)m*a[i]/n; if(ans[2*i-1]<=ans2[l]&&ans[2*i]<=ans2[l]){ if(d){b[i]=x+1;d--;} else b[i]=x; } } for(i=1;i<=k;i++)printf("%d ",b[i]); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) (v).begin(), (v).end() using ll = long long; constexpr int INF = 1e9; constexpr long long LINF = 1e18; constexpr long long MOD = 1e9 + 7; signed main() { int k, n, m; cin >> k >> n >> m; ll a[k]; rep(i, k) { cin >> a[i]; } int use = 0; priority_queue<pair<ll, ll>> que; rep(i, k) { ll t = (a[i] * m + n - 1) / n; que.emplace(t * n - a[i] * m, i); use += t; } while(use > m) { auto p = que.top(); que.pop(); p.first -= n; que.emplace(p); use--; } ll b[k]; while (!que.empty()) { auto p = que.top(); que.pop(); b[p.second] = (p.first + m * a[p.second]) / n; } rep(i, k) { cout << b[i] << endl; } return 0; }
#include<bits/stdc++.h> #define ll long long int #define mk make_pair #define pb push_back #define INF (ll)1e18 #define pii pair<ll,ll> #define mod 998244353 #define f(i,a,b) for(ll i=a;i<b;i++) #define fb(i,a,b) for(ll i=a;i>b;i--) #define ff first #define ss second #define srt(v) if(!v.empty())sort(v.begin(),v.end()) #define rev(v) if(!v.empty())reverse(v.begin(),v.end()) #define PI 3.141592653589793238 #define pqr priority_queue<ll,vector<ll>,greater<ll>()> using namespace std; ll pow_mod(ll a,ll b) { ll res=1; while(b!=0) { if(b&1) { res=(res*a)%mod; } a=(a*a)%mod; b/=2; } return res; } ll inv_mod(ll x){ return pow_mod(x,mod-2); } const ll N=(ll)3e5; ll inv[N]; ll fac[N]; void init(){ fac[0]=1; for(ll i=1;i<N;i++) fac[i]=(i*fac[i-1])%mod; for(ll i=0;i<N;i++) inv[i]=inv_mod(fac[i]); } ll ncr(ll n,ll r){ if(n<r) return 0; if(n==r||r==0) return 1LL; ll x=fac[n]; ll y=inv[n-r]; ll z=inv[r]; return ((x*y)%mod*z)%mod; } bool test_cases=0; ll grid[10004][101]; void solve() { ll n; cin>>n; string s; cin>>s; ll ans=-1; ++n; ll a[n]; for(ll i=0;i<n;i++) cin>>a[i]; ll l=1; ll r=(ll)1e4; while(l<=r){ ll mid=(l+r)/2; for(ll i=0;i<n;i++){ ll have=a[i]/mid; for(ll j=0;j<mid;j++){ grid[j][i]=have; } ll rem=a[i]%mid; for(ll j=0;j<mid&&rem>0;j++,--rem) grid[j][i]++; } bool ok=true; for(ll i=0;i<mid;i++){ for(ll j=1;j<n;j++){ if(s[j-1]=='<'&&grid[i][j-1]>=grid[i][j]){ ok=false; break; } if(s[j-1]=='>'&&grid[i][j-1]<=grid[i][j]){ ok=false; break; } } } if(ok){ ans=mid; l=mid+1; } else r=mid-1; } cout<<ans<<endl; for(ll i=0;i<n;i++){ ll have=a[i]/ans; for(ll j=0;j<ans;j++){ grid[j][i]=have; } ll rem=a[i]%ans; for(ll j=0;j<ans&&rem>0;j++,--rem) grid[j][i]++; } for(ll i=0;i<ans;i++){ for(ll j=0;j<n;j++){ cout<<grid[i][j]<<" "; } cout<<endl; } } int main() { // init(); //factorial calculations //Start from Here. ll t; t=1; while(t--) solve(); //Good Bye! return 0; }
#include<bits/stdc++.h> //#include<atcoder/all> using namespace std; using ll = long long; const ll MOD = 1e9+7; ll dp[2001][2001]; int main() { int h,w; cin >> h >> w; map<ll,ll> sumx,sumy,sumxy; vector<string> S(h); for(auto &i:S)cin >> i; for(int i = 0;i<h;i++){ for(int j = 0;j<w;j++){ if(i==0&&j==0){ dp[i][j]=1; sumx[i] += 1; sumy[j] += 1; sumxy[i-j]+=1; continue; } if(S[i][j]=='.'){ (dp[i][j]+=sumx[i])%=MOD; (dp[i][j]+=sumy[j])%=MOD; (dp[i][j]+=sumxy[i-j])%=MOD; (sumx[i]+=dp[i][j])%=MOD; (sumy[j]+=dp[i][j])%=MOD; (sumxy[i-j]+=dp[i][j])%=MOD; }else{ dp[i][j] = 0; sumx[i]=0; sumy[j]=0; sumxy[i-j]=0; } } } cout<<dp[h-1][w-1]<<endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) using ll = long long; using P = pair<int,int>; int main() { const ll LINF = 1LL<<62; int n,t; cin >> n >> t; vector<int> a(n); rep(i,n) cin >> a[i]; int l = n/2; int r = n-l; set<ll> sa; for (int s=0; s<(1<<l); s++) { ll sum = 0; rep(i,l) { if ((s>>i)%2 == 1) sum += a[i]; } sa.insert(sum); } set<ll> sb; for (int s=0; s<(1<<r); s++) { ll sum = 0; rep(i,r) { if ((s>>i)%2 == 1) sum += a[l+i]; } sb.insert(sum); } vector<ll> vb; vb.push_back(-LINF); for (ll e : sb) vb.push_back(e); ll ans = 0; for (ll x : sa) { int j = upper_bound(vb.begin(), vb.end(), t-x) - vb.begin() - 1; ll y = vb[j]; ans = max(ans, x+y); } cout << ans << endl; return 0; }
#include<bits/stdc++.h> namespace my_std{ using namespace std; #define pii pair<int,int> #define fir first #define sec second #define MP make_pair #define rep(i,x,y) for (int i=(x);i<=(y);i++) #define drep(i,x,y) for (int i=(x);i>=(y);i--) #define go(x) for (int i=head[x];i;i=edge[i].nxt) #define templ template<typename T> #define sz 202020 typedef long long ll; typedef double db; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);} templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;} templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;} templ inline void read(T& t) { t=0;char f=0,ch=getchar();double d=0.1; while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar(); while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar(); if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();} t=(f?-t:t); } template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);} char __sr[1<<21],__z[20];int __C=-1,__zz=0; inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;} inline void print(int x) { if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x; while(__z[++__zz]=x%10+48,x/=10); while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n'; } void file() { #ifdef NTFOrz freopen("a.in","r",stdin); #endif } inline void chktime() { #ifdef NTFOrz cerr<<clock()/1000.0<<'\n'; #endif } #ifdef mod ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;} ll inv(ll x){return ksm(x,mod-2);} #else ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;} #endif // inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;} } using namespace my_std; int n; vector<ll>a[3]; int main() { file(); map<char,int>mp; mp['R']=0,mp['G']=1,mp['B']=2; read(n); ll x; char cc; rep(i,1,n*2) read(x),cin>>cc,a[mp[cc]].push_back(x); rep(i,0,2) sort(a[i].begin(),a[i].end()); auto qq=[&](int id,ll w) { if (!a[id].size()) return ll(1e18); int t=lower_bound(a[id].begin(),a[id].end(),w)-a[id].begin(); ll res=1e18; if (t) chkmin(res,w-a[id][t-1]); if (t!=(int)a[id].size()) chkmin(res,a[id][t]-w); return res; }; int c=0,p=0; rep(i,0,2) if (a[i].size()%2==0u) ++c,p=i; if (c==3) return puts("0"),0; ll ans=1e18; rep(i,0,2) if (i!=p) for (auto x:a[i]) chkmin(ans,qq(3-i-p,x)); int p1=(p==0?1:0),p2=3-p-p1; auto wk=[&](){ll mn=1e18;for (auto x:a[p]) chkmin(ans,mn+qq(p2,x)),chkmin(mn,qq(p1,x));}; wk(); swap(p1,p2); wk(); cout<<ans; return 0; }
#include <algorithm> #include <iostream> #include <iterator> #include <sstream> #include <utility> #include <cstdarg> #include <cassert> #include <climits> #include <cstdlib> #include <cstring> #include <cstdio> #include <cctype> #include <vector> #include <stack> #include <queue> #include <cmath> #include <set> #include <map> #define space char(32) #define line char(10) #define debug(x) cerr << '[' << #x << " = " << x << ']' << line #define speedup ios_base::sync_with_stdio(false); cin.tie(NULL) #define output_array(arr) for(auto &i: arr) cout << i << line #define input_array(arr) for(auto &i: arr) cin >> i #define For(n) for(int i = 0; i < n; i++) #define hell 1000000007 typedef long long ll; using namespace std; int main() { speedup; ll str, y, a, b, exp = 0; cin >> str >> y >> a >> b; while(str < y){ if(str <= (str + b) / a) // && str * a < LLONG_MAX) str *= a; else break; ++exp; // debug(str); } if(str >= y){ --exp; } else{ ll temp; temp = (y - str) / b; str += temp * b; exp += temp; if(str == y) exp--; } cout << exp << line; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0; i<(n);i++) using ll = long long; const ll INF = 1e9+7; const ll mod=1000000007; using P = pair<ll, ll>; ll x, y, a, b; int main(){ cin >> x >> y >> a >> b; ll ans2 = 0; while((double)a*x< y && a*x < x+b){ x *= a; ans2++; } ans2 += (y-x-1)/b; cout << ans2 << endl; }
#include<bits/stdc++.h> #define FastRead \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define ll long long #define endl "\n" #define f for #define ml ll t,g; cin>>t; f(g=0;g<t;g++) #define pi acos(-1) using namespace std; bool cmp(const pair<string,ll> &a, const pair<string,ll> &b) { if(a.second!=b.second) return (a.second > b.second); else return (a.first < b.first); } int main() { FastRead ll n,i,j,k; cin>>n; ll a[n+5],b[n+5],c=0; double ab,bc,ac; f(i=0; i<n; i++) { cin>>a[i]>>b[i]; } f(i=0; i<n-2; i++) { f(j=i+1; j<n-1; j++) { f(k=j+1; k<n; k++) { if(((b[j]-b[i])*(a[k]-a[i]))==((b[k]-b[i])*(a[j]-a[i]))) { c=1; break; } } if(c==1) break; } if(c==1) break; } if(c==1) cout<<"Yes"; else cout<<"No"; return 0; }
/*ver 7*/ #include <bits/stdc++.h> using namespace std; void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);} using ll = long long; using ld = long double; using vl = vector<ll>; using vd = vector<ld>; using vs = vector<string>; using vb = vector<bool>; using vvl = vector<vector<ll>>; using vvd = vector<vector<ld>>; using vvs = vector<vector<string>>; using vvb = vector<vector<bool>>; using pll = pair<ll,ll>; using mll = map<ll,ll>; template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; #define each(x,v) for(auto& x : v) #define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++) #define rep(i,n) for(ll i=0;i<(ll)n;i++) #define pb push_back #define eb emplace_back #define fi first #define se second #define mp make_pair const ll INF = 1LL << 60; #define CLR(mat,f) memset(mat, f, sizeof(mat)) #define IN(a, b, x) (a<=x&&x<=b) #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除 #define debug cout << "line : " << __LINE__ << " debug" << endl; #define ini(...) int __VA_ARGS__; in(__VA_ARGS__) #define inl(...) long long __VA_ARGS__; in(__VA_ARGS__) #define ind(...) long double __VA_ARGS__; in(__VA_ARGS__) #define ins(...) string __VA_ARGS__; in(__VA_ARGS__) #define inc(...) char __VA_ARGS__; in(__VA_ARGS__) void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);} template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}} template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}} template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}} template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}} template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}} void out(){cout << endl;} template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);} void die(){cout << endl;exit(0);} template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);} template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;} template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}} template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}} template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}} template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}} template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}} #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;} ll ceilDiv(ll a,ll b) {return (a+b-1)/b;} ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離 ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; } ll lcm(ll a,ll b){ return a / gcd(a,b) * b;} template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); } #define YES(n) ((n) ? "YES" : "NO" ) #define Yes(n) ((n) ? "Yes" : "No" ) #define yes(n) ((n) ? "yes" : "no" ) bool cheak(V<ll> &x,V<ll> &y,ll a,ll b,ll c){ ll s; s=(x[a]-x[b])*(y[b]-y[c])-(y[a]-y[b])*(x[b]-x[c]); if(s==0)return true; return false; } int main(){ init(); inl(n); V<ll> x(n),y(n); in2(x,y); set<pair<ll,ll>> s; rep(i,n)reps(j,i+1,n)reps(k,j+1,n){ if(cheak(x,y,i,j,k))die("Yes"); } out("No"); return 0; }
#include <bits/stdc++.h> #define FOR(ii,aa,bb) for(int ii=aa;ii<bb;ii++) #define for0(ii,bb) FOR(ii,0,bb) #define for1(ii,bb) FOR(ii,1,bb+1) #define pb push_back #define ppb pop_back #define mp make_pair #define st first #define nd second #define pii pair<int,int> #define piii pair<int,pii> #define pdi pair<double,int> #define vi vector<int> #define sp " " #define nl "\n" #define all(x) x.begin(),x.end() #define fastio() ios_base::sync_with_stdio(0);cin.tie(0); #define ll long long #define int ll using namespace std; const int N = 200005; const int INF = 1e9+5; const int mod = 1e9+7; int n,ans; vector<vector<pii>> g; vector<int> dist; int topla(int x,int y){ return (x+y)%mod; } int carp(int x,int y){ return x*y%mod; } void dfs(int u,int d,int p=-1){ dist[u]=d; for(auto e:g[u]){ int v=e.st,w=e.nd; if(v==p)continue; dfs(v,d^w,u); } } signed main(){ #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif fastio() cin >> n; g.resize(n+1); dist.resize(n+1); for(int i=1;i<n;i++){ int u,v,w; cin >> u >> v >> w; g[u].pb({v,w}); g[v].pb({u,w}); } dfs(1,0); for(int i=0;i<60;i++){ vector<int> cnt(2); for(int j=1;j<=n;j++){ cnt[(dist[j]>>i)&1ll]++; } ans=topla(ans,carp(carp(cnt[0],cnt[1]),(1ll<<i)%mod)); } cout << ans << nl; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; using Vi = vector<int>; using VVi = vector<Vi>; using Vl = vector<ll>; using VVl = vector<Vl>; using P = pair<int, int>; using Vp = vector<P>; using VVp = vector<Vp>; using pl = pair<ll, int>; using Vpl = vector<pl>; using VVpl = vector<Vpl>; ll gcd(ll a, ll b) { while (a) { b %= a; swap(a, b); } return b; } ll dsum(ll a) { ll num = 0; while (a) { num += a % 10ll; a /= 10ll; } return num; } bool is_pal(string S) { bool ok = true; for (int i = 0; i < (int)S.size(); i++) { int j = (int)S.size() - i - 1; ok = ok && (S[i] == S[j]); } return ok; } ll m = 998244353; ll mpow(ll a, ll n) { ll ret = 1; while (n) { if (n % 2) { ret *= a; ret %= m; } a = (a * a) % m; n /= 2; } return ret; } int calc(int a, int N) { if (a > N) { return a - N; } else { return a + N; } } int main() { int N, M; cin >> N >> M; VVp cond(N + 1); for (int i = 0; i < M; i++) { int x, y, z; cin >> x >> y >> z; cond[x].push_back(make_pair(y, z)); } ll dp[1 << 19]; dp[0] = 1; for (int i = 1; i < (1 << N); i++) { Vi num(N + 1, 0); dp[i] = 0; int di = 0; for (int j = 0; j < N; j++) { if ((i >> j) & 1) { dp[i] += dp[i - (1 << j)]; num[j + 1]++; di++; } } for (int j = 1; j <= N; j++) { num[j] += num[j - 1]; } bool ok = true; for (P con : cond[di]) { int y, z; tie(y, z) = con; if (num[y] > z) { ok = false; } } if (!ok) { dp[i] = 0; } } cout << dp[(1 << N) - 1] << endl; }
#include <algorithm> #include <iostream> using namespace std; const int MD = 1000000007; long long inv(int n) { return n == 1 ? 1 : inv(n - MD % n) * (MD / n + 1) % MD; } long long choose(int n, int k) { return n < k ? 0 : k == 0 ? 1 : choose(n - 1, k - 1) * n % MD * inv(k) % MD; } int main() { int n, m, k; cin >> n >> m >> k; if (n - m >= k + 1) { cout << "0\n"; return 0; } int ans = choose(n + m, n) - choose(n + m, n + (k + 1 - (n - m))); if (ans < 0) ans += MD; cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define deb(k) cerr << #k << ": " << k << "\n"; #define size(a) (int)a.size() #define fastcin cin.tie(0)->sync_with_stdio(0); #define st first #define nd second #define pb push_back #define mk make_pair #define int long long typedef long double ldbl; typedef double dbl; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef map<int, int> mii; typedef vector<int> vint; #define MAX 4000100 #define MAXLG 20 const int inf = 0x3f3f3f3f; const ll mod = 1000000007; const ll linf = 0x3f3f3f3f3f3f3f3f; const int N = 300100; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll fat[MAX+100]; ll ifat[MAX+100]; ll power(ll x, ll y, ll p){ ll res = 1; x = x % p; while (y > 0){ if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } void init(){ fat[0] = 1; for(ll i=1;i<=MAX;i++){ fat[i] = (fat[i-1]*i)%mod; } ifat[MAX] = power(fat[MAX], mod-2, mod); for(ll i=MAX-1;i>=0;i--) ifat[i] = (ifat[i+1]*(i+1))%mod; } ll choose(int x, int y){ if(y < 0 || y > x || x < 0) return 0; return fat[x]*(ifat[y]*ifat[x-y]%mod)%mod; } void solve(){ init(); int n, m, k; cin>>n>>m>>k; if(n > m + k){ cout<<"0\n"; return; } int ans = choose(n+m, n); for(int i=k+1;i<=n;i++){ int j = i - k - 1; int l = j; int a = k, b = 0; int aux = (choose(2*l + a - b, l) - choose(2*l + a - b, l - 1)) * choose(n+m-i-j, n-i) % mod; ans -= aux; ans %= mod; } ans = (ans + mod) % mod; cout<<ans<<"\n"; // Have you read the problem again? // Maybe you understood the wrong problem } int32_t main(){ fastcin; int t_ = 1; //cin>>t_; while(t_--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (long long i = 0, max_i = (n); i < max_i; i++) typedef long long ll; const int NUM = 2e5+10; ll x[NUM], y[NUM]; ll x00[NUM], y00[NUM]; ll x01[NUM], y01[NUM]; ll x10[NUM], y10[NUM]; int main() { int n; cin >> n; rep(i, n) cin >> x[i + 1] >> y [i + 1]; int m; cin >> m; x10[0] = y01[0] = 1; rep(i, m) { int o; cin >> o; if(o == 1) { x00[i+1]=y00[i]; y00[i+1]=-x00[i]; x01[i+1]=y01[i]; y01[i+1]=-x01[i]; x10[i+1]=y10[i]; y10[i+1]=-x10[i]; } if(o == 2) { x00[i+1]=-y00[i]; y00[i+1]=x00[i]; x01[i+1]=-y01[i]; y01[i+1]=x01[i]; x10[i+1]=-y10[i]; y10[i+1]=x10[i]; } if(o == 3) { int p; cin >> p; x00[i+1]=2*p-x00[i]; y00[i+1]=y00[i]; x01[i+1]=2*p-x01[i]; y01[i+1]=y01[i]; x10[i+1]=2*p-x10[i]; y10[i+1]=y10[i]; } if(o == 4) { int p; cin >> p; x00[i+1]=x00[i]; y00[i+1]=2*p-y00[i]; x01[i+1]=x01[i]; y01[i+1]=2*p-y01[i]; x10[i+1]=x10[i]; y10[i+1]=2*p-y10[i]; } } int q; cin >> q; rep(i, q) { int a, b; cin >> a >> b; ll x_ = (x10[a]-x00[a])*x[b] + (x01[a]-x00[a])*y[b] + x00[a]; ll y_ = (y10[a]-y00[a])*x[b] + (y01[a]-y00[a])*y[b] + y00[a]; printf("%lld %lld\n", x_, y_); } return 0; }
//kyoprosaikoooooooooooo!! #include <bits/stdc++.h> #define ll long long #define MOD 1000000007 double pai = 3.141592653589793238; using namespace std; //素数判定 O(√N) ll prime (ll abc) { if (abc < 2) { return 0; } else if (abc == 2) { return 1; } else if (abc % 2 == 0) { return 0; } double sqrtabc = sqrt(abc); for (int i = 3; i <= sqrtabc; i++) { if (abc % i == 0) { return 0; } } return 1; } //素因数分解(約数列挙) O(√N) vector<ll> divisor(ll n) { vector<long long> ret; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); return ret; } //小さい方に揃える 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; } //繰り返し二乗法 O(logN) ll pow(ll x, ll n) { ll ret = 1; while (n > 0) { if (n & 1) ret *= x; // n の最下位bitが 1 ならば x^(2^i) をかける x *= x; //ret = ret * x % mod / x = x * x % mod でMODとれる n >>= 1; // n を1bit 左にずらす } return ret; } struct Edge { ll to; ll w; Edge(ll to, ll w) : to(to) , w(w) {} }; using Graph = vector<vector<Edge>>; int main() { ll N, Q; cin >> N >> Q; set<ll> X; for (ll i = 0; i < N; i++) { ll a; cin >> a; X.insert(a); } vector<ll> A; for (ll v : X) { A.push_back(v); } ll SA = A.size(); vector<ll> P; P.push_back(A[0] - 1); for (ll i = 1; i < SA; i++) { P.push_back(A[i] - A[i - 1] - 1); } vector<ll> L; ll sum = 0; for (ll n : P) { sum += n; L.push_back(sum); } for (ll i = 0; i < Q; i++) { ll j; cin >> j; ll O = lower_bound(L.begin(), L.end(), j) - L.begin(); cout << j + O << endl; } }
#include <iostream> #include <cstdio> #include <cmath> #include <ctime> #include <cstdlib> #include <cassert> #include <vector> #include <list> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <bitset> #include <string> #include <algorithm> #include <utility> #include <complex> #define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++) #define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++) #define chmin(x, y) (x) = min((x), (y)) #define chmax(x, y) (x) = max((x), (y)) #define sz(x) ((ll)(x).size()) #define ceil(x, y) (((x)+(y)-1) / (y)) #define all(x) (x).begin(),(x).end() #define outl(...) dump_func(__VA_ARGS__) #define inf 1e18 using namespace std; typedef long long llint; typedef long long ll; typedef pair<ll, ll> P; bool exceed(ll x, ll y, ll m){return x >= m / y + 1;} struct edge{ ll to, cost; edge(){} edge(ll a, ll b){ to = a, cost = b; } }; template<typename T> ostream& operator << (ostream& os, vector<T>& vec) { for(int i = 0; i<vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : " "); } return os; } template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U>& pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& map_var) { for(typename map<T, U>::iterator itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if(itr != map_var.end()) os << ","; itr--; } return os; } template<typename T> ostream& operator << (ostream& os, set<T>& set_var) { for(typename set<T>::iterator itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if(itr != set_var.end()) os << " "; itr--; } return os; } void dump_func() {cout << endl;} template <class Head, class... Tail> void dump_func(Head &&head, Tail &&... tail) { cout << head; if(sizeof...(Tail) > 0) cout << " "; dump_func(std::move(tail)...); } ll n; string s, x; ll dp[200005][7]; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s >> x; s = "#" + s, x = "#" + x; dp[n][0] = 1; rep(i, 1, 6) dp[n][i] = -1; for(int i = n-1; i >= 0; i--){ rep(j, 0, 6){ if(x[i+1] == 'T') dp[i][j] = max(dp[i+1][j*10%7], dp[i+1][(j*10+s[i+1]-'0')%7]); if(x[i+1] == 'A') dp[i][j] = min(dp[i+1][j*10%7], dp[i+1][(j*10+s[i+1]-'0')%7]); } } if(dp[0][0] > 0) outl("Takahashi"); if(dp[0][0] < 0) outl("Aoki"); return 0; }
#include <bits/stdc++.h> #define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define ll long long #define lol __int128 #define ld long double #define pii pair<int, int> #define dbg(x) cerr << #x << " = " << x << "\n" using namespace std; ll a, b, c; ll power(ll x, ll y, ll m) { x %= m; ll res = 1; while(y) { if(y & 1) res = res * x % m; x = x * x % m; y >>= 1; } return res; } int32_t main() { srand(chrono::steady_clock::now().time_since_epoch().count()); boost; cin >> a >> b >> c; a %= 10; if(a <= 1 || a == 5 || a == 6) cout << a; else if(a == 2 || a == 3 || a == 7 || a == 8) { cout << power(a, (power(b, c, 4) == 0 ? 4 : power(b, c, 4)), 10); } else { cout << power(a, (power(b, c, 2) == 0 ? 2 : power(b, c, 2)), 10); } }
/****************************************** * AUTHOR : RTG * ******************************************/ #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE // For getting input from input.txt file FILE *input = freopen("/home/tharun/Documents/cpg/input.txt", "r", stdin); // Printing the Output to output.txt file FILE *output = freopen("/home/tharun/Documents/cpg/output.txt", "w", stdout); int start_time = (int)clock(); #endif int n,x; cin>>n>>x; for(int i=0;i<n;i++){ int tmp; cin>>tmp; if(tmp==x); else cout<<tmp<<" "; } #ifndef ONLINE_JUDGE // Time taken for execution cout<<"\nTime = "<<clock() - start_time<<"ms"; #endif return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n,x,a; cin >> n >> x; while(cin >> a){ if(a != x) cout << a << ' '; } }
#include<bits/stdc++.h> #define int ll #define sz(x) int((x).size()) #define all(x) (x).begin(),(x).end() #define pb push_back #define x first #define y second using namespace std; using ll = long long; using pi = pair<int,int>; const int inf = 0x3f3f3f3f3f3f3f3f; const int minf = 0xc0c0c0c0c0c0c0c0; vector<int> adj[200200]; int D; int cnt; int dfs(int cur, int p=-1) { int mn=0, mx=0; for (auto &nxt : adj[cur]) { if (nxt == p) continue; int r = dfs(nxt, cur); mn = min(mn, r); mx = max(mx, r); } if (mx + mn >= 1) return mx - 1; if (mn == -D) { ++cnt; return D; } return mn - 1; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int n,k; cin>>n>>k; for (int i=1; i<n; i++) { int a,b; cin>>a>>b; adj[a].pb(b); adj[b].pb(a); } int here = -1; for (int le=1,ri=n-1; le<=ri;) { D = le+ri>>1; cnt = 0; int r = dfs(1); if (r < 0) ++cnt; if (cnt <= k) { here = D; ri = D - 1; } else { le = D + 1; } } assert(~here); cout<<here<<'\n'; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pii pair<long long,long long> #define mp make_pair #define pb push_back int n,c[100005],vis[100005],nowc[100005]; vector <int> g[100005]; vector <int> ans; void dfs(int now) { /* cout<<now<<endl; for(int i=1;i<=n;i++) cout<<vis[i]<<" "; puts(""); for(int i=1;i<=n;i++) cout<<nowc[i]<<" "; puts(""); system("pause"); */ vis[now]=1; if(nowc[c[now]]==0) ans.pb(now); nowc[c[now]]++; for(int i=0;i<g[now].size();i++) { if(!vis[g[now][i]]) dfs(g[now][i]); } nowc[c[now]]--; } void solve() { cin>>n; for(int i=1;i<=n;i++) cin>>c[i]; for(int i=1;i<n;i++) { int a,b; cin>>a>>b; g[a].pb(b); g[b].pb(a); } dfs(1); sort(ans.begin(),ans.end()); for(int i=0;i<ans.size();i++) cout<<ans[i]<<"\n"; } signed main() { int _=1; //cin>>_; while(_--) solve(); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false);cin.tie(0); int a, b, w; cin>>a>>b>>w; w*=1000; int l=1e6+1, r=-(1e6+1); for(int i=1;i<=w;i++){ if(a*i<=w && w<=b*i){ l=min(l, i); r=max(r, i); } } if(l==1e6+1) cout<<"UNSATISFIABLE"<<endl; else cout<<l<<" "<<r<<endl; }
#include <bits/stdc++.h> long long mod = 1e9 + 7; long long modinv(long long dividend, long long divisor, long long modnum){ long long b = mod, u = 1, v = 0; while (b) { long long t = divisor / b; divisor -= t * b; std::swap(divisor, b); u -= t * v; std::swap(u, v); } u %= mod; if (u < 0) u += mod; long long ans = dividend * u % mod; return ans; } int main(){ int N, M, K; std::cin >> N >> M >> K; if(N > M + K){ std::cout << 0 << std::endl; return 0; } long long mult = 1, d1_1 = 1, d1_2 = 1, d2_1 = 1, d2_2 = 1; for(int i=N+M; i>0; i--){ mult = (mult * i) % mod; } for(int i=N; i>0; i--){ d1_1 = (d1_1 * i) % mod; } for(int i=M; i>0; i--){ d1_2 = (d1_2 * i) % mod; } for(int i=N-K-1; i>0; i--){ d2_1 = (d2_1 * i) % mod; } for(int i=M+K+1; i>0; i--){ d2_2 = (d2_2 * i) % mod; } long long ans1 = modinv(mult, d1_1, mod); ans1 = modinv(ans1, d1_2, mod); long long ans2 = modinv(mult, d2_1, mod); ans2 = modinv(ans2, d2_2, mod); if(N == K){ std::cout << ans1 << std::endl; return 0; } if(ans1 < ans2){ ans1 += mod; } std::cout << ans1 - ans2 << std::endl; return 0; }
#include <iostream> #include <array> #include <algorithm> #include <vector> using namespace std; #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++) #define prl(a) cout << (a) << endl #define allrange(a) a.begin(),a.end() bool solve_translated(vector<pair<int,int>> &S,vector<pair<int,int>> &T){ int N = S.size(); int dx = S[0].first-T[0].first; int dy = S[0].second-T[0].second; bool flg = true; reps(i,1,min(N,20)){ if(!((S[i].first==T[i].first+dx) && (S[i].second==T[i].second+dy))){flg = false; break;} } return flg; } vector<pair<int,int>> Pitagora_rot(vector<pair<int,int>> &S,int a, int b , int c){ int N = S.size(); vector<pair<int,int>> PS(N); auto pt0 = S[0]; PS[0] = pt0; bool flg = true; int dx,dy,x,y; reps(i,1,N){ dx = S[i].first - pt0.first; dy = S[i].second - pt0.second; x=dx*a-dy*b; y=dx*b+dy*a; if(((x%c)!=0) || ((y%c)!=0)) {flg = false; break;} PS[i] = make_pair(x/c+pt0.first, y/c+pt0.second); } if(flg) return PS; else return vector<pair<int,int>>(); } int main(){ std::cin.tie(0); std::ios::sync_with_stdio(false); int N;cin >> N; vector<pair<int,int>> S(N),T(N); rep(i,N) { int x,y; cin >> x >> y; S[i].first = x;S[i].second = y; } rep(i,N) { int x,y; cin >> x >> y; T[i].first = x; T[i].second = y; } sort(allrange(T)); /*ピタゴラ三角形 5 12 13 8 15 17 3 4 5 */ constexpr int tri[12][3] ={ {3,4,5}, {4,3,5}, {-3,4,5}, {-4,3,5}, {0,1,1}, {1,0,1}, {-1,0,1}, {0,-1,1}, {-3,-4,5}, {-4,-3,5}, {3,-4,5}, {4,-3,5} }; /* vector<vector<int>> tri(0); tri.push_back(vector<int>({3,4,5})); tri.push_back(vector<int>({4,3,5})); tri.push_back(vector<int>({-3,4,5})); tri.push_back(vector<int>({-4,3,5})); tri.push_back(vector<int>({0,1,1})); tri.push_back(vector<int>({1,0,1})); tri.push_back(vector<int>({-1,0,1})); tri.push_back(vector<int>({0,-1,1})); tri.push_back(vector<int>({-3,-4,5})); tri.push_back(vector<int>({-4,-3,5})); tri.push_back(vector<int>({3,-4,5})); tri.push_back(vector<int>({4,-3,5})); // tri.push_back(vector<int>({-5, -12, 13})); // tri.push_back(vector<int>({-12, -5, 13})); // tri.push_back(vector<int>({-8,-15,17})); // tri.push_back(vector<int>({-15,-8,17})); // tri.push_back(vector<int>({5, 12, 13})); // tri.push_back(vector<int>({12, 5, 13})); // tri.push_back(vector<int>({8,15,17})); // tri.push_back(vector<int>({15,8,17})); // tri.push_back(vector<int>({-5, 12, 13})); // tri.push_back(vector<int>({-12, 5, 13})); // tri.push_back(vector<int>({-8,15,17})); // tri.push_back(vector<int>({-15,8,17})); // tri.push_back(vector<int>({5, -12, 13})); // tri.push_back(vector<int>({12, -5, 13})); // tri.push_back(vector<int>({8,-15,17})); // tri.push_back(vector<int>({15,-8,17})); */ bool flg; if(N==2){ auto sx = S[0].first-S[1].first; auto sy = S[0].second-S[1].second; auto tx = T[0].first-T[1].first; auto ty = T[0].second-T[1].second; flg= (sx*sx+sy*sy==tx*tx+ty*ty); } else { rep(j,12){ auto S2 = Pitagora_rot(S,tri[j][0],tri[j][1],tri[j][2]); if(S2.empty()) continue; sort(allrange(S2)); flg = solve_translated(S2,T); if(flg) break; } } if(flg) prl("Yes"); else prl("No"); }
#include<iostream> #include<iomanip> #include<string> #include<vector> #include<algorithm> #include<utility> #include<tuple> #include<map> #include<queue> #include<deque> #include<set> #include<stack> #include<numeric> #include<complex> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #include<cstdarg> using namespace std; #define DEBUG(var) cout << #var << ": " << var << " "; #define DEBUG_EN(var) cout << #var << ": " << var << endl; struct Edge { int to; long long weight; Edge() : to(0), weight(0) {} Edge(int to, long long weight) : to(to), weight(weight) {} Edge(const Edge& e) { to = e.to; weight = e.weight; } bool operator>(const Edge &e) const { return weight > e.weight; } bool operator<(const Edge &e) const { return weight < e.weight; } bool operator==(const Edge &e) const { return weight == e.weight; } bool operator<=(const Edge &e) const { return weight <= e.weight; } bool operator>=(const Edge &e) const { return weight >= e.weight; } }; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using Graph = vector<vector<int>>; using weightedGraph = vector<vector<Edge>>; using heap = priority_queue<int, vector<int>, greater<int>>; const ll BIL = 1e9; const ll MOD = 1e9 + 7; const ll INF = 1LL << 60; const int inf = 1 << 29; const ld PI = 3.141592653589793238462643383; int main(int argc,char* argv[]){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); int n; cin >> n; vector<complex<double>> s(n), t(n); for(int i=0;i<n;i++) { double a, b; cin >> a >> b; s[i] = {a, b}; } for(int i=0;i<n;i++) { double c, d; cin >> c >> d; t[i] = {c, d}; } if(n == 1) { cout << "Yes" << endl; return 0; } using vc = vector<complex<double>>; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(i == j) continue; auto a = t[i], b = t[j]; if(norm(s[1]-s[0]) != norm(b-a)) continue; vc na = s, nb = t; auto f = [&](vc& p, complex<double> q, complex<double> r) { for(int i=0;i<n;i++) p[i] -= q; for(int i=0;i<n;i++) p[i] *= r; sort(p.begin(), p.end(), [](complex<double> lhs, complex<double> rhs) { if(lhs.real() == rhs.real()) return lhs.imag() < rhs.imag(); return lhs.real() < rhs.real(); }); }; f(na, s[0], b-a); f(nb, a, s[1]-s[0]); bool flag = true; for(int i=0;i<n;i++) { if(abs(na[i] - nb[i]) > 1e-6) flag = false; } if(flag) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }
// ※※※ 解答不能 ※※※ // https://atcoder.jp/contests/abc207/editorial/2153 // C++(GCC 9.2.1) #include <bits/stdc++.h> using namespace std; using vd = vector<double>; #define repex(i, a, b, c) for(int i = a; i < b; i += c) #define repx(i, a, b) repex(i, a, b, 1) #define rep(i, n) repx(i, 0, n) #define repr(i, a, b) for(int i = a; i >= b; i--) int a0[101], b0[101], a1[101], b1[101], a2[101], b2[101], a3[101], b3[101], c[101], d[101]; int main(){ int N; scanf("%d", &N); vd a(N), b(N), c(N), d(N); rep(_, 2){ rep(i, N) scanf("%lf %lf", &a[i], &b[i]); int x = 0, y = 0; rep(i, N){ x += a[i]; y += b[i]; a[i] *= N; b[i] *= N; } rep(i, N){ a[i] -= x; b[i] -= y; } swap(a, c); swap(b, d); } rep(i, N){ if(a[i] != 0 || b[i] != 0){ swap(a[i], a[0]); swap(b[i], b[0]); } } string ans = "No"; const double eps = 1e-6; rep(i, N){ double angle = atan2(d[i], c[i]) - atan2(b[0], a[0]); bool ok = true; rep(j, N){ double A = a[j] * cos(angle) - b[j] * sin(angle); double B = a[j] * sin(angle) + b[j] * cos(angle); bool ok2 = false; rep(k, N){ if(abs(A - c[k]) <= eps && abs(B - d[k]) <= eps) ok2 = true; } ok &= ok2; } if(ok) ans = "Yes"; } printf("%s\n", ans.c_str()); }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const ll modn = 1000000007; const double eps = 1e-3; const double pi = acos(-1); struct pii{ double x,y; double len2() const{ return x*x+y*y; } double arc(){ return atan2(y,x); } pii rot(double th){ return pii{cos(th)*x-sin(th)*y,sin(th)*x+cos(th)*y}; } }; bool comp(const pii& a,const pii &b){ return fabs(a.len2()-b.len2())<1e-6; } pii s[200],t[200]; pii temp_t[200]; int n; pii operator -(const pii& a,const pii &b){ return pii{a.x-b.x,a.y-b.y}; } bool check(int a,int b){ pii dis = t[a]; for(int i=0;i<n;i++)temp_t[i]=t[i]-dis; double th = -(temp_t[b]).arc(); for(int i=0;i<n;i++)temp_t[i]=temp_t[i].rot(th); for(int i=0;i<n;i++){ int flag=0; for(int j=0;j<n;j++){ if((temp_t[i]-s[j]).len2()<eps){ flag=1; break; } } if(flag==0)return 0; } return 1; //temp_t[b] } int main() { cin>>n; if(n==1){ cout<<"Yes"<<endl; return 0; } for(int i=0;i<n;i++)cin>>s[i].x>>s[i].y; for(int i=1;i<n;i++){ s[i]=s[i]-s[0]; } s[0]=s[0]-s[0]; double th = -s[1].arc(); for(int i=1;i<n;i++){ s[i]=s[i].rot(th); }//for(int i=0;i<n;i++)cout<<s[i].x<<" "<<s[i].y<<endl; for(int i=0;i<n;i++)cin>>t[i].x>>t[i].y; ll ans = 0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(j==i)continue; if(!comp(t[j]-t[i],s[1]))continue; if(check(i,j)){ans=1;} if(ans)break; } if(ans)break; } if(ans)cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
#include <iostream> #include<cmath> #include<vector> #include<set> #include <algorithm> #include<map> #include<string> using namespace std;typedef long long ll; #define rep(i,n) for(i=0;i<n;i++) #define pb push_back #define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf #define MOD 1000000007 //問題による #define FORA(i,I) for(const auto& i:I) //aをbで割る時の繰上げ,繰り下げ ll myceil(ll a,ll b){return (a+(b-1))/b;} ll myfloor(ll a,ll b){return a/b;} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll lcm(ll c,ll d){ll g=gcd(c,d);return (c/g)*d;} int main() { ll i,n,k; vector<ll>a,b; cin>>n; for(i=1;i<=sqrt(n);i++){ if(n%i==0){ if(i!=n/i){ a.pb(i); b.pb(n/i); }else{ a.pb(i); } } } ll cnt1=a.size(); ll cnt2=b.size(); for(i=0;i<cnt1;i++){ cout<<a[i]<<endl; } for(i=cnt2-1;i>=0;i--){ cout<<b[i]<<endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int N; cin >> N; vector<int> A(N); rep(i, N) cin >> A.at(i); int ans = 2, count = 0; rep(i, 1010) { if (i < 2) continue; int j = 0; rep(k, N) if (!(A.at(k) % i)) j++; if (j >= count) { ans = i; count = j; } } cout << ans << endl; }
#include <algorithm> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <memory> #include <queue> #include <deque> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #include <cassert> #include <iostream> #include <stdio.h> #include <time.h> using namespace std; typedef long long ll; #define sz size() #define pb push_back #define mp make_pair #define fi first #define se second #define all(c) (c).begin(), (c).end() #define rep(i,a,b) for(ll i=(a);i<(b);++i) #define per(i,a,b) for(ll i=b-1LL;i>=(a);--i) #define clr(a, b) memset((a), (b) ,sizeof(a)) #define ctos(c) string(1,c) #define MOD 1000000007 int main(){ ll n; cin>>n; vector<ll> v; rep(i,0,n){ ll a; cin>>a; v.pb(a); } reverse(all(v)); ll p = v[0]; ll m = MOD-v[0]; ll dp = 1; ll dm = 1; rep(i,1,v.sz){ ll dp1 = (dp+dm)%MOD; ll dm1 = dp; ll p1 = (p+m+v[i]*dp1%MOD)%MOD; ll m1 = (MOD+p-v[i]*dm1%MOD)%MOD; p = p1; m = m1; dp = dp1; dm = dm1; } cout << p%MOD << endl; }
#include<bits/stdc++.h> using namespace std; using ll = long long; int main(){ int n; cin >> n; ll a[401]; for(int i = 0; i < 401; ++i){ a[i] = 0; } for(int i = 0; i < n; ++i){ ll tmp; cin >> tmp; ++a[tmp+200]; } ll sum = 0; for(int i = 0; i < 401; ++i){ for(int j = i; j < 401; ++j){ sum += a[i]*a[j]*(i-j)*(i-j); } } cout << sum << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define fi first #define se second #define rng(i, a, b) for (int i=int(a);i<int(b);++i) #define rep(i, b) rng(i, 0, b) #define gnr(i, a, b) for (int i=int(b)-1;i>=int(a);--i) #define per(i, b) gnr(i, 0, b) #define all(obj) begin(obj), end(obj) #define allr(obj) rbegin(obj), rend(obj) #define cinv(a) rep(i,(int)a.size()) cin >> a[i] #define debug(a) rep(i,(int)a.size()) cout << a[i] << " " #define show(x) cerr<<#x<<" = "<<x<<endl; #define pb push_back #define eb emplace_back #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> Pi; typedef vector<Pi> vp; typedef vector<vp> vvp; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<vb> vvb; template<typename T> bool chmax(T& a, const T& b){if(a < b){a=b;return true;}return false;} template<typename T> bool chmin(T& a, const T& b){if(a > b){a=b;return true;}return false;} template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; const int di[] = {0, -1, 0, 1}; const int dj[] = {1, 0, -1, 0}; const int MOD = 1e9+7; const int INF = 1e9; const ll LINF = 1e15; const double eps = 1e-4; const char nl = '\n'; ll power(ll a, ll b) {return b? power(a*a%MOD, b/2)*(b%2?a:1)%MOD:1;} ll nCk(int n, int k) { ll x = 1, y = 1; for (int i = 1; i <= k; ++i) { x = x*(n-i+1)%MOD; y = y*i%MOD; } return x*power(y, MOD-2)%MOD; } struct UF { vi d; UF(int n) : d(n, -1) {} int root(int x) { if (d[x] < 0) return x; return d[x] = root(d[x]); } bool unite(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return -d[root(x)]; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int a, b, c; cin >> a >> b >> c; cout << (a*a + b*b < c*c ? "Yes" : "No") << nl; }
#include <iostream> using namespace std; int main(){ long long a, b, c; cin >> a >> b >> c; if (a * a + b * b < c * c){ cout << "Yes" << endl; } else{ cout << "No" << endl; } }
#include<bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using db = double; using vi = vector<int>; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define debug(x) cout << #x <<" "<< x <<endl const int inf = 0x3f3f3f3f; const db eps = 1e-8; const int mod = 1e9+7; ll qpow(ll a, ll b){ ll ret = 1; while(b){ if(b&1)ret = ret*a%mod; a = a*a%mod; b>>=1; } return ret; } const int maxn = 1<<10; int n; int a[maxn], b[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i=1; i<=n; ++i){ a[i] = (2*i)%n+1; b[i] = (2*i+1)%n+1; } for(int i=1; i<=n; ++i)cout << a[i] <<" " << b[i] <<"\n"; return 0; }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; 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);} 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;} //head int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int now = 1; auto get = [&]() { if(now == n) return now = 1; return ++now; }; //cout << n << endl; rep(i, n) { cout << get() << ' ' << get() << '\n'; } }
#include <bits/stdc++.h> using namespace std; #define FREP(i,a,b) for(long long i=a;i<b;i++) #define REP(i,n) FREP(i,0,n) #define PB push_back #define ll long long #define MP make_pair #define mod 1000000007 #define endl "\n" #define vi vector<ll> #define si set<ll> #define mi map<ll,ll> #define bs binary_search #define pri(a,n) REP(i,n)cout<<a[i]<<" "; #define all(x) x.begin(),x.end() #define sz(a) (ll)a.size() #define sa(a,n) sort(a,a+n) #define sra(a,n) sort(a,a+n,greater<ll>()) #define sv(v) sort(all(v)) #define srv(v) sort(v.rbegin(),v.rend()) #define sp(x) fixed<<setprecision(x) #define mem(a) memset(a,-1,sizeof(a)) #define ff first #define ss second #define deb(x) cout<<#x<<' '<<x<<endl; #define FASTIO ios_base::sync_with_stdio(false);\ cin.tie(NULL);\ cout.tie(NULL); void _a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z() { ll n, k, q = 0, c = 0; string s; cin >> n >> k; while (n) { c += (n % 10); n /= 10; } while (k) { q += (k % 10); k /= 10; } cout << max(c, q); } int main() { FASTIO; int t = 1; // cin >> t; // Test Case :-||-: // while (t--) { _a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z(); } return 0; } // Thanks for stalking :P
#include<iostream> using namespace std; #include<vector> #include <algorithm> #define re(i,n) for(int i = 0; i < n; i++) const int MOD = 998244353; long long mod(long long val, long long m) { long long res = val % m; if (res < 0) res += m; return res; } 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; } 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; } int ctoi(const char c) { if ('0' <= c && c <= '9') return (c - '0'); return -1; } int main() { string sa, sb; cin >> sa >> sb; int a = ctoi(sa[0]) + ctoi(sa[1]) + ctoi(sa[2]); int b = ctoi(sb[0]) + ctoi(sb[1]) + ctoi(sb[2]); if(a > b) cout << a << endl; else cout << b << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) long long a[60]; long long ans; map<long long,long long> dp[60]; long long coin(int n,long long x) { long long p,q,r; if (dp[n][x]>0) return dp[n][x]; p=x/a[n]; q=x%a[n]; if (q==0){ dp[n][x]=1; return dp[n][x]; } if (x>a[n+1]-a[n]) { dp[n][x]=coin((n-1),(x-a[n+1]+a[n])); return dp[n][x]; } if (x<a[n]-a[n+1]) { dp[n][x]=coin((n-1),(x+a[n+1]-a[n])); return dp[n][x]; } r=coin((n-1),q); if (x<0){ r+=coin((n-1),(q+a[n])); dp[n][x]=r; return r; } if (x>0){ r+=coin((n-1),(q-a[n])); dp[n][x]=r; return r; } } int main() { int n; long long x,r; cin >> n >> x ; rep(i, n) cin >> a[i] ; ans=0; n--; x%=a[n]; if (x==0) { cout << 1 << endl; return 0; } ans=coin((n-1),x); if (x<0)ans+=coin((n-1),(x+a[n])); if (x>0)ans+=coin((n-1),(x-a[n])); cout << ans << endl; return 0; }
#include <complex> #include <iomanip> #include <iostream> #include <vector> using namespace std; using Expr = complex<double>; int main() { int n, m, k; cin >> n >> m >> k; vector<bool> portal(n + 1, false); for (int i = 0; i < k; i += 1) { int pos; cin >> pos; portal[pos] = true; } vector<Expr> dp(n + 1, Expr{0, 0}); Expr sum = dp[n]; auto max_portals = 0; auto curr_portals = 0; for (int i = n - 1; i >= 0; i -= 1) { if (portal[i]) { dp[i] = {1, 0}; curr_portals += 1; max_portals = max(max_portals, curr_portals); } else { dp[i] = Expr{0, 1} + sum / (1.0 * m); curr_portals = 0; } sum += dp[i]; if (i + m <= n) { sum -= dp[i + m]; } } if (max_portals >= m) { cout << "-1\n"; } else { auto res = -dp[0].imag() / (dp[0].real() - 1.0); cout << fixed << setprecision(4) << res << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<long long, long long>; constexpr char ln = '\n'; constexpr long long MOD = 1000000007; //constexpr long long MOD = 998244353; constexpr long long INF = 1e9+100; constexpr long long LINF = 1e18+100; #define all(x) (x).begin(),(x).end() #define rep(i,n) for(int i=0;i<(n);i++) #define rept(i, j, n) for(int i=(j); i<(n); i++) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int main(){ int a,b,x,y; cin >> a >> b >> x >> y; int res = 0; if(a==b)res = x; else if(a<b){ res = min(y, x*2)*abs(a-b)+x; } else{ res = min(y, x*2)*(abs(a-b)-1)+x; } cout << res << ln; }
#include<bits/stdc++.h> using namespace std; int main() { int a,b,x,y; while(cin>>a>>b>>x>>y) { long long ans1,ans2; if(a==b) cout<<x<<endl; else if(a>b) { ans1=(a-b-1)*y+x; ans2=(a-b-1)*2*x+x; cout<<min(ans1,ans2)<<endl; } else { ans1=x+(b-a)*y; ans2=(b-a)*2*x+x; cout<<min(ans1,ans2)<<endl; } } }
#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 = (b)-1; i >= (a); --i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<pii> vii; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll mod = 998244353; ll modpow(ll b, ll e) { ll ans = 1; for (; e; b = b * b % mod, e /= 2) if (e & 1) ans = ans * b % mod; return ans; } vl fac; ll inv(ll x) { return modpow(x, mod-2); } ll c(ll n, ll r) { if (r > n or r < 0) return 0; return (fac[n] * inv(fac[r]) % mod) * inv(fac[n-r]) % mod; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int n, k; cin >> n >> k; fac.pb(1); rep(i,1,212345) { fac.pb((fac.back()*i)%mod); } vl a(n); rep(i,0,n) cin >> a[i]; vl s(k+1); rep(i,0,k+1) { rep(j,0,n) { s[i] += modpow(a[j], i); s[i] %= mod; } } rep(x,1,k+1) { ll ans = 0; rep(i,0,x+1) { ll tans = c(x, i); tans *= s[i]; tans %= mod; tans *= s[x-i]; tans %= mod; ans += tans; ans %= mod; } rep(i,0,n) { ans -= modpow(2*a[i], x); ans %= mod; ans += mod; ans %= mod; } ans *= inv(2); ans %= mod; ans += mod; ans %= mod; cout << ans << '\n'; } }
#include <bits/stdc++.h> const int K = 300; const int N = 200000; const int P = 998244353; inline int add(int x, int y) {x += y; return x >= P ? x - P : x;} inline int sub(int x, int y) {x -= y; return x < 0 ? x + P : x;} inline int mul(int x, int y) {return (int)(1ll * x * y % P);} int s[K + 5], t[K + 5], c[K + 5][K + 5]; int p[N + 5], a[N + 5], n, k; int main() { scanf("%d%d", &n, &k); for(int i=1;i<=n;i++) scanf("%d", &a[i]), p[i] = 1; for(int j=0;j<=k;j++) for(int i=1;i<=n;i++) s[j] = add(s[j], p[i]), t[j] = add(t[j], mul(p[i], p[i])), p[i] = mul(p[i], a[i]); for(int i=0;i<=k;i++) for(int j=0;j<=i;j++) c[i][j] = (j ? add(c[i - 1][j - 1], c[i - 1][j]) : 1); int iv2 = (P + 1) >> 1; for(int i=1;i<=k;i++) { int ans = 0; for(int j=0;j<=i;j++) { int del = mul(iv2, sub(mul(s[j], s[i - j]), s[i])); ans = add(ans, mul(c[i][j], del)); } printf("%d\n", ans); } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define speed ios_base::sync_with_stdio(false);cin.tie(NULL) #define mod 1000000007 const int N = 100005; ll sigma(ll n) { return (n*(n+1))/2; } signed main() { speed; int n,k; cin>>n>>k; ll ans=100*k*sigma(n); ans+=sigma(k)*n; cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int main() { fast_io; int n, k; cin >> n >> k; string res = ""; ll sum = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { res.clear(); res += to_string(i); res += '0'; res += to_string(j); sum += stoll(res); } } cout << sum << endl; return 0; }
#include <bits/stdc++.h> #define FOR(i,n) for(lli i=0;i<(lli)(n);++i) #define FORU(i,j,k) for(lli i=(j);i<=(lli)(k);++i) #define FORD(i,j,k) for(lli i=(j);i>=(lli)(k);--i) #define pb push_back #define mt make_tuple using namespace std; using lli = long long int; using pll = pair<lli, lli>; using vi = vector<lli>; using vvi = vector<vi>; using pii = tuple<lli, lli>; using vii = vector<pii>; using vvii = vector<vii>; #define X(a) get<0>(a) #define Y(a) get<1>(a) #define Z(a) get<2>(a) string TYPE(const int*) { return "%d"; } string TYPE(const lli*) { return "%lld"; } string TYPE(const size_t*) { return "%u"; } string TYPE(const double*) { return "%lf"; } string TYPE(const char*) { return "%c"; } string TYPE(const char**) { return "%s"; } string TYPE(const unsigned int*) { return "%u"; } const int MAX_BUF = 100*1000+42; char buf[MAX_BUF]; void RD() {} template<typename T, typename... Args> void RD(T* v, Args... args) { scanf((" " + TYPE(v)).c_str(), v); RD(args...); } template<typename... Args> void RD(string* v, Args... args) { scanf(" %s", buf); (*v) = buf; RD(args...); } template<class A, class B, typename... Args> void PR(bool nl, pair<A, B> p, Args... args); void PR(bool nl = true) { if(nl) printf("\n"); } template<typename T, typename... Args> void PR(bool nl, T v, Args... args) { printf((TYPE(&v) + " ").c_str(), v); PR(nl, args...); } template<typename... Args> void PR(bool nl, string& v, Args... args) { printf("%s", v.c_str()); PR(nl, args...); } template<class A, class B, typename... Args> void PR(bool nl, pair<A, B> p, Args... args) { PR(false, "{", p.first, ",", p.second, "}"); PR(nl, args...); } template<typename... Args> void PR(Args... args) { PR(true, args...); } const long long int oo = 1000*1000*1000; struct Coord { int x, y; Coord(int x = 0, int y = 0) : x(x), y(y) {} Coord operator + (const Coord& droite) const { return Coord(x + droite.x, y + droite.y); } }; struct AB { int k; vector<lli> arbre; AB(int _k = 20, lli def = 0) { k = _k; FOR(i, 1 << k) arbre.push_back(i < (1 << (k-1)) ? 0LL : def); FORD(i, ((1 << (k-1)) - 1), 1) arbre[i] = arbre[i << 1] + arbre[(i << 1) ^ 1]; } void set(int i, lli x) { int feuille = i + (1 << (k-1)); arbre[feuille] = x; iset(feuille >> 1); } void iset(int noeud) { if(noeud) { arbre[noeud] = arbre[noeud << 1] + arbre[(noeud << 1) ^ 1]; iset(noeud >> 1); } } lli sum(int deb, int fin, int noeud = 1, int p = 0, int q = -1) { if(q < p) q = 1 << (k-1); if(deb <= p && q <= fin) return arbre[noeud]; if(deb >= q || fin <= p) return 0LL; int mil = (p + q) / 2; return sum(deb, fin, noeud << 1, p, mil) + sum(deb, fin, (noeud << 1) ^ 1, mil, q); } }; const int MAX_M = 10'000+42; int dv[MAX_M][MAX_M]; int main() { lli m, n; RD(&n, &m); if(m == 1 || m == 3) { PR(0); return 0; } lli k = 0, r = 1; int cnt = 0; lli period = 0, offset = 0; if(m == 9) { period = 9; } else { while(dv[k][r] == 0) { ++cnt; dv[k][r] = cnt; r = 10*r; k = (10*k + r/m) % m; r = r % m; //PR(cnt, k, r); } period = cnt - dv[k][r]+1; offset = dv[k][r]-1; //PR(k, r); } //PR(period, offset); if(n <= offset) { k = 0, r = 1; FOR(i, n) { r = 10*r; k = (10*k + r/m) % m; r = r % m; } PR(k); return 0; } //PR(cnt); n = ((n-offset) % period) + offset; k = 0, r = 1; FOR(i, n) { r = 10*r; k = (10*k + r/m) % m; r = r % m; } PR(k); return 0; }
#include <bits/stdc++.h> using namespace std; int64_t ans=0; int sx,sy; int H,W; vector<vector<bool> > flag; vector<vector<int> > vv; void dfs(int x,int y,int num,int64_t count,char dir){ if(num==0){ if(sy==y-1){ if(dir!='L'){ count++; } }else if(sy==y+1){ if(dir!='R'){ count++; } }else if(sx==x-1){ if(dir!='U'){ count++; } }else if(sx==x+1){ if(dir!='D'){ count++; } } ans=max(ans,count); return; } for(auto &tmp:vv.at(x*W+y)){ int nx=tmp/W; int ny=tmp%W; if(flag.at(nx).at(ny)){ flag.at(nx).at(ny)=false; bool judge=true; char c=dir; if(ny==y-1){ if(dir!='L'){ judge=false; c='L'; } }else if(ny==y+1){ if(dir!='R'){ judge=false; c='R'; } }else if(nx==x-1){ if(dir!='U'){ judge=false; c='U'; } }else if(nx==x+1){ if(dir!='D'){ judge=false; c='D'; } } if(judge){ dfs(nx,ny,num-1,count,c); }else{ dfs(nx,ny,num-1,count+1,c); } } } } int main(){ cin >> H >> W; vector<string> S(H); flag.resize(H+1,vector<bool>(W+1,false)); vv.resize(H*W+W+1,vector<int>(H*W+W+1)); for(int i=0;i<H;i++){ cin >> S.at(i); } int num=0; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(S.at(i).at(j)=='#'){ if(S.at(i).at(j-1)=='.'){ flag.at(i).at(j)=true; flag.at(i+1).at(j)=true; vv.at(i*W+j).emplace_back((i+1)*W+j); vv.at((i+1)*W+j).emplace_back(i*W+j); } if(S.at(i).at(j+1)=='.'){ flag.at(i).at(j+1)=true; flag.at(i+1).at(j+1)=true; vv.at(i*W+j+1).emplace_back((i+1)*W+j+1); vv.at((i+1)*W+j+1).emplace_back(i*W+j+1); } if(S.at(i-1).at(j)=='.'){ flag.at(i).at(j)=true; flag.at(i).at(j+1)=true; vv.at(i*W+j).emplace_back(i*W+j+1); vv.at(i*W+j+1).emplace_back(i*W+j); } if(S.at(i+1).at(j)=='.'){ flag.at(i+1).at(j)=true; flag.at(i+1).at(j+1)=true; vv.at((i+1)*W+j).emplace_back((i+1)*W+j+1); vv.at((i+1)*W+j+1).emplace_back((i+1)*W+j); } } } } for(int i=0;i<H+1;i++){ for(int j=0;j<W+1;j++){ if(flag.at(i).at(j)){ sx=i; sy=j; num++; } } } flag.at(sx).at(sy)=false; dfs(sx,sy,num-1,0,' '); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int; #define fast ios_base::sync_with_stdio; cin.tie(NULL); cout.tie(NULL); #define rloop(i, a, b) for(int i=a; i>=b; i--) #define loop(i, a, b) for(int i=a; i<b; i++) int main() { fast; { int n, y; cin>>n; map<int, string>m; string x; loop(i, 0, n) { cin>>x>>y; m.insert(pair<int, string>(y, x)); } int c = m.size(); for(auto it: m) { // c--; // cout<<c<<" "; if(c==2) { cout<<it.second; break; } c--; } } }
#define _GLIBCXX_DEBUG //配列外参照の検出 #include <bits/stdc++.h> //標準ライブラリをインクルード using namespace std; //std::の省略 typedef long long ll; //long longをllに省略 bool compare_second(pair<string, ll> a, pair<string, ll> b) { if(a.second != b.second){ return a.second < b.second; }else{ return a.first < b.first; } } int main() { ll N; cin>>N; vector<pair<string, ll>> S(N); for(int i=0; i<N; i++){ cin>>S[i].first>>S[i].second; } sort(S.rbegin(), S.rend(), compare_second); cout << S[1].first << endl; }
/* * @author: codancer * @createTime: 2020-11-16, 22:37:41 */ #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <chrono> #include <random> #include <bitset> using namespace std; typedef long long ll; typedef unsigned long long ull; const ll mod = 1e9+7; #define pb push_back #define fi first #define se second #define SZ(x) ((int)(x).size()) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define fep(i,a,b) for(int i=(a);i>=(b);i--) #define deb(x) cerr<<#x<<" = "<<(x)<<"\n" typedef vector<int> VI; typedef vector<ll> VII; typedef pair<int,int> pii; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll Rand(ll B) { return (ull)rng() % B; } int main(){ int n; cin>>n; if(n<0){ n=0; } cout<<n<<endl; return 0; }
#include<bits/stdc++.h> #define ll long long #define test(t) int t;cin>>t;while(t--) using namespace std; int main() { ll x; cin>>x; if(x<0) cout<<0<<"\n"; else cout<<x<<"\n"; return 0; }
#include<bits/stdc++.h> using namespace std; #define reg register typedef long long ll; #define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++) static char buf[100000],*p1=buf,*p2=buf; inline ll read(void){ reg bool f=false; reg char ch=getchar(); reg ll res=0; while(!isdigit(ch))f|=(ch=='-'),ch=getchar(); while(isdigit(ch))res=10ll*res+(ch^'0'),ch=getchar(); return f?-res:res; } ll n; int cnt[3]; int main(void){ n=read(); reg int tmp=n%3; while(n){ ++cnt[(n%10)%3]; n/=10; } reg int sum=cnt[0]+cnt[1]+cnt[2]; if(!tmp) puts("0"); else if(tmp==1) if(cnt[1]>0&&sum>1) puts("1"); else if(cnt[2]>1&&sum>2) puts("2"); else puts("-1"); else if(cnt[2]>0&&sum>1) puts("1"); else if(cnt[1]>1&&sum>2) puts("2"); else puts("-1"); return 0; }
#include <iostream> #include <iomanip> #include <algorithm> #include <array> #include <cassert> #include <optional> #include <utility> #include <vector> #include <cmath> // #include <atcoder/all> template <class InputIterator> std::ostream& range_output(std::ostream& os_arg, InputIterator first_arg, InputIterator last_arg){ if(first_arg != last_arg){ do{ os_arg << *(first_arg++); if(first_arg == last_arg) break; os_arg << ' '; } while(true); } return os_arg; } template <class Tp> std::ostream& operator << (std::ostream& os_arg, const std::vector<Tp>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); } template <class Tp, std::size_t Size> std::ostream& operator << (std::ostream& os_arg, const std::array<Tp, Size>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); } template <class S, class T> std::ostream& operator << (std::ostream& os_arg, const std::pair<S, T>& pair_arg){ return os_arg << '(' << pair_arg.first << ", " << pair_arg.second << ')'; } #ifndef ONLINE_JUDGE template <typename Head> void dump_out(Head head_arg){ std::cerr << head_arg << '\n'; } template <typename Head, typename... Tail> void dump_out(Head head_arg, Tail... tail_args){ std::cerr << head_arg << ", "; dump_out(tail_args...); } #define dump(...) do { std::cerr << "[in line " << __LINE__ << "] " << #__VA_ARGS__ << " : "; dump_out(__VA_ARGS__); } while(false) #else #define dump(...) (void(0)) #endif template <class S, class T> bool chmax(S& x, const T& y){ if(x < y){ x = y; return true; } return false; } template <class S, class T> bool chmin(S& x, const T& y){ if(x > y){ x = y; return true; } return false; } struct nimotsu { int size, value; }; int main(void){ std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(16); int n; std::string S, X; std::cin >> n >> S >> X; // 確実にできるか constexpr std::array<char, 7> D = {0 % 7, 10 % 7, 20 % 7, 30 % 7, 40 % 7, 50 % 7, 60 % 7}; const auto seven_mod = [](char a){ return (a < 7) ? a : a - 7; }; auto dfs = [&](auto self, int index) -> char { if(index == n) return 1; const char np = self(self, index + 1); if(np == 0) return 0; S[index] -= '0'; if(S[index] >= 7) S[index] -= 7; short res = 0; // Aoki doesn't want to make the number to be nine times if(X[index] == 'A'){ for(char i = 0; i < 7; ++i) if( (np >> D[i]) & (np >> seven_mod(D[i] + S[index])) & 1) res |= (1 << i); } else{ for(char i = 0; i < 7; ++i) if( ((np >> D[i]) | (np >> seven_mod(D[i] + S[index]))) & 1) res |= (1 << i); } return res; }; const short res = dfs(dfs, 0); std::cout << ((res & 1) ? "Takahashi" : "Aoki" ) << '\n'; return 0; }
#include<cstdio> #include<cstring> #include<iostream> using namespace std; #define N 150300 #define M 301300 #define INF 0x3f3f3f3f int A[N],B[N],C[N],fa[N],tag[N],que[N],dis[N],n,D[20][20],f[20][N],vis[N]; struct node{ int to,next; }q[M]; int head[N],ss; void addedge(int x,int y) { q[++ss]=(node){y,head[x]};head[x]=ss; q[++ss]=(node){x,head[y]};head[y]=ss; } void dij(int S) { int f=1,e=0; que[++e]=S; for (int i=1;i<=n;++i) vis[i]=0,dis[i]=INF; dis[S]=0; vis[S]=true; while(f<=e) { int u=que[f++]; for (int j=head[u];j;j=q[j].next) { int t=q[j].to; if (vis[t]) continue; que[++e]=t; dis[t]=dis[u]+1; vis[t]=true; } } } int main() { int m,k,ans=INF; scanf("%d%d",&n,&m); for (int i=1;i<=m;++i) scanf("%d%d",&A[i],&B[i]),addedge(A[i],B[i]); scanf("%d",&k); for (int i=1;i<=k;++i) scanf("%d",&C[i]),tag[C[i]]=i; for (int i=1;i<=k;++i) { dij(C[i]); for (int j=1;j<=k;++j) D[i][j]=dis[C[j]]; } memset(f,0x3f,sizeof(f)); for (int i=1;i<=k;++i) f[i][1<<i-1]=0; for (int S=0;S<(1<<k);++S) { for (int i=1;i<=k;++i) { if (f[i][S]==INF) continue; for (int j=1;j<=k;++j) { if (i==j||S&(1<<j-1)) continue; f[j][S|(1<<j-1)]=min(f[j][S|(1<<j-1)],f[i][S]+D[i][j]); } } } for (int i=1;i<=k;++i) ans=min(ans,f[i][(1<<k)-1]); printf("%d",ans==INF?-1:ans+1); return 0; } //by qlwpc
#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(ll i = (m); i < (n); ++i) #define REPN(i, m, n) for(ll i = (m); i <= (n); ++i) #define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i) #define REPNR(i, m, n) for(ll 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 LINF = 1000000000000000001; constexpr int64_t MOD = 1000000007; constexpr int64_t MOD_N = 998244353; constexpr long double EPS = 1e-11; const double PI = acos(-1); template <class T> struct Graph { struct edge { int64_t from, to; T dist; bool operator<(const edge &rhs) const { return dist < rhs.dist; } bool operator>(const edge &rhs) const { return dist > rhs.dist; } edge &operator+=(const edge &rhs) { to = rhs.to; dist += rhs.dist; return *this; } edge operator+(const edge &rhs) { return edge(*this) += rhs; } }; int64_t V; vector<T> dist; vector<vector<edge>> edges; Graph() {} Graph(int64_t v) : V(v) { edges = vector<vector<edge>>(V); dist = vector<T>(V, LINF); } void add_edge(int64_t a, int64_t b, T d = 1, bool is_dual = false) { edges[a].push_back(edge{a, b, d}); if (is_dual) edges[b].push_back(edge{b, a, d}); } void dijkstra(int64_t s) { dist.assign(V, LINF); priority_queue<edge, vector<edge>, greater<edge>> p_que; p_que.push(edge{s, s, T()}); while (!p_que.empty()) { edge e = p_que.top(); p_que.pop(); if (e.dist >= dist[e.to]) continue; dist[e.to] = e.dist; for (auto i : edges[e.to]) p_que.push(e + i); } } }; int main(void) { ll n, m; cin >> n >> m; Graph<ll> g(n); rep(i, m) { ll a, b; cin >> a >> b; g.add_edge(a - 1, b - 1, 1, true); } ll k; cin >> k; Vec c(k); cin >> c; vector<Graph<ll>> gs(k); rep(i, k) gs[i] = g; rep(i, k) gs[i].dijkstra(c[i] - 1); Vec2<ll> dp(1 << k, Vec(k, LINF)); rep(i, k) { dp[1 << i][i] = 1; } rep(i, 1 << k) { rep(j, k) { rep(l, k) { if (!(i >> j & 1) || (i >> l & 1)) continue; chmin(dp[i | 1 << l][l], dp[i][j] + gs[j].dist[c[l] - 1]); } } } ll ans = LINF; rep(i, k) chmin(ans, dp[(1 << k) - 1][i]); if (ans == LINF) ans = -1; co(ans); return 0; }
#include<bits/stdc++.h> //#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)? EOF:*p1++) using namespace std; typedef long long ll; const int N=2e5+5,mod=1e9+7; int n,k; ll f[N][2][2][17]; bool vs[N][2][2][17]; int cnt[N],bk[N][17]; char s[N]; int gt(char x) { if(x<='9') return x-'0'; return x-'A'+10; } ll dfs(int st,int f1,int f2,int nm) { if(st>n) return f2&&(nm==k); if(vs[st][f1][f2][nm]) return f[st][f1][f2][nm]; vs[st][f1][f2][nm]=1; ll &lp=f[st][f1][f2][nm],i,j; if(f1) { lp=(lp+dfs(st+1,1,1,cnt[st]))%mod; for(i=0,j=gt(s[st]);i<j;i++) lp=(lp+dfs(st+1,0,f2|!!i,nm+(!bk[st-1][i])))%mod; return lp; } if(!f2) return lp=(15*dfs(st+1,0,1,1)+dfs(st+1,0,0,0))%mod; return lp=((16-nm)*dfs(st+1,0,1,nm+1)+nm*dfs(st+1,0,1,nm))%mod; } int main() { int i,j; scanf("%s%d",s+1,&k),n=strlen(s+1); for(i=1;i<=n;i++) cnt[i]=cnt[i-1]+(!bk[i-1][gt(s[i])]),memcpy(bk[i],bk[i-1],sizeof(bk[i])),bk[i][gt(s[i])]=1; cout<<dfs(1,1,0,0); return 0; }
#include <iostream> using namespace std; using ll = long long; const int kMod = 1e9 + 7; const int kMaxN = 2e5 + 1; string s; int a[kMaxN]; ll c[20][20], dp[kMaxN][20]; ll ans, k, n, fl, b[20]; ll ksm(ll a, ll b) { ll x = 1; for (; b; b /= 2, a = a * a % kMod) { if (b % 2 == 1) { x = x * a % kMod; } } return x; } int q() { int cnt = 0, i; for (i = 0; i <= 15; i++) { cnt += (b[i] > 0); } return cnt; } int main() { int i, t1, j, l; cin >> s; n = s.size(); for (i = 0; i < n; i++) { if (s[i] >= 'A') { a[i + 1] = s[i] - 'A' + 10; } else { a[i + 1] = s[i] - '0'; } } cin >> k; dp[1][1] = a[1] - 1; for (i = 1; i < n; i++) { dp[i + 1][1] = 15, b[a[i]]++; for (j = 1; j <= k; j++) { dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * j) % kMod; dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * (16 - j)) % kMod; } t1 = q(); for (j = 0; j < a[i + 1]; j++) { if (b[j] == 0) { dp[i + 1][t1 + 1]++; } else { dp[i + 1][t1]++; } } } b[a[n]]++, dp[n][q()]++; cout << dp[n][k] % kMod; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization ("unroll-loops") #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define ll long long #define ld long double #define endl '\n' #define all(A) (A).begin(),(A).end() #define traceA(A,x) cerr<<#A<<": ";for(int i = 0; i < (x) ; ++i) cerr<<A[i]<<' ';cerr<<endl #define traceV(A) cerr<<#A<<": ";for(auto i:A)cerr<<i<<' ';cerr<<endl #define traceM(A,n,m) cerr<<#A<<": "<<endl;for(int i = 0; i < (n) ; ++i){for(int j = 0; j < (m) ; ++j) cerr << A[i][j] << ' ';cerr << endl;} #define TRACE #ifdef TRACE #define trace(...) __f__(#__VA_ARGS__,__VA_ARGS__) template<typename Arg1> void __f__(const char* name,Arg1&& arg1){cerr<<name<<" : "<<arg1<<endl;} template <typename Arg1,typename... Args> void __f__(const char* names,Arg1&& arg1,Args&&... args){const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" | ";__f__(comma+1,args...);} #else // #define trace(...) 1 #endif const ll mod = 1e9+7; inline ll add(ll a,ll b){a+=b;if(a>=mod)a-=mod;return a;} inline ll sub(ll a,ll b){a-=b;if(a<0)a+=mod;return a;} inline ll mul(ll a,ll b){return (a*1ll*b)%mod;} inline ll power(ll a,ll b){ll rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;} inline ll inv(ll a){return power(a,mod-2);} const ll inf = 1e18+77; const int MOD = 1e9+7; const int N = 2e5 + 77; int A[N]; void sol(){ int n; cin >> n; for(int i = 0 ; i < n ; ++i) cin >> A[i]; sort(A,A+n); for(int i = 0 ; i < n ; ++i){ if(A[i] != i+1){ cout << "No" << endl; return; } } cout << "Yes" << endl; } int32_t main(){ fast; int t = 1; // cin >> t; while(t--) sol(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; int count =0; cin>>N; vector<int> vec(N); for (int i = 0; i < N; i++) {cin >> vec.at(i);} sort(vec.begin(), vec.end()); for(int n=0;n<N;n++){if(vec.at(n)!=n+1){count++;break;}} if(count==0){cout<<"Yes"<<endl;} else if(count==1){cout<<"No"<<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; typedef long long ll; template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count()); #define printArr(arr, n) cerr << #arr << " = ["; for(int i=0; i<(n); i++){if (i) cerr << ", "; cerr << arr[i];} cerr << "]\n" template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}; template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";}; void _print() {cerr << "]\n";} template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);} #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #define fi first #define se second #define SZ(x) (int)((x).size()) #define pii pair<int,int> int main() { ios::sync_with_stdio(0); cin.tie(0); ll n; cin >> n; ll cur = 1; for (ll q = 2; q <= n; q++) { cur = cur*q/__gcd(cur,q); } cout << cur+1 << "\n"; return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; constexpr ll MOD = 1000000007; #ifndef ONLINE_JUDGE template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " (" << x.first << " : " << x.second << ")" << ","; o << " }"; return o;} template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "["; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "]"; return o;} template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "(" << obj.first << ", " << obj.second << ")"; return o;} template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} void print_sim_py(void) {cout << endl;} template <class Head> void print_sim_py(Head&& head) {cout << head;print_sim_py();} template <class Head, class... Tail> void print_sim_py(Head&& head, Tail&&... tail) {cout << head << " ";print_sim_py(forward<Tail>(tail)...);} #define print(...) print_sim_py(__VA_ARGS__); #else #define print(...); #endif template <typename... Ts> std::istream& IN(Ts&... xs){ return (std::cin >> ... >> xs); } #define repr(i, a, b) for (int i = a; i < b; i++) #define rep(i, n) for (int i = 0; i < n; i++) int main(void){ ios::sync_with_stdio(false); cin.tie(nullptr); double sx,sy,gx,gy; IN(sx,sy,gx,gy); double a = (-gy-sy)/(gx-sx); double b = -a*gx-gy; print(a,b) double ans = -b / a; printf("%.10lf\n", ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int d = floor(1.08 * N) - 206; if(d > 0) cout << ":("; else if(d == 0) cout << "so-so"; else if(d < 0) cout << "Yay!"; cout << endl; }
#line 1 "main.cpp" #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; void solve() { int n; cin >> n; cout << (1 << n) - 1 << "\n"; for (int b = 1; b < (1 << n); ++b) { for (int x = 0; x < (1 << n); ++x) { cout << "AB"[__builtin_popcount(x & b) % 2]; } cout << "\n"; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define MAXN 500005 char s[MAXN],t[MAXN]; int n,cnt1,cnt2; int main() { scanf("%d",&n); scanf("%s %s",s,t); for(int i=0;i<n;i++)if(s[i]=='0')cnt1++; for(int i=0;i<n;i++)if(t[i]=='0')cnt2++; if(cnt1!=cnt2)printf("-1\n"); else { int i=0,j=0; while(1) { while(i<n&&s[i]=='1')i++; while(j<n&&t[j]=='1')j++; if(i>=n)break; i++,j++; if(i==j)cnt1--; } printf("%d\n",cnt1); } return 0; }
#include <algorithm> #include <chrono> #include <cmath> #include <ctime> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #include <cassert> #include <assert.h> //#pragma GCC optimize("O3") using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; const ll MOD = 998244353; const ld EPS = 1e-11; const ld MEPS = EPS * 1e2; const ll INFL = 2e18; const int INF = 1e9 + 7; const ld PI = acos(-1); const ld E = exp(1); mt19937 rnd((unsigned)chrono::steady_clock::now().time_since_epoch().count()); int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int n; cin >> n; string s, t; cin >> s >> t; vector<int> is, it; for (int i = 0; i < n; ++i) { if (s[i] == '0') { is.push_back(i); } if (t[i] == '0') { it.push_back(i); } } if (is.size() != it.size()) { cout << "-1\n"; return 0; } int ans = 0; for (int i = 0; i < is.size(); ++i) { if (is[i] != it[i]) { ++ans; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define MAXN 100005 #define INF 1000000000000000005 using namespace std; int main (){ int a,b,ms,mf; cin>>a>>b; ms = a+b; mf = b; //cout<<ms<<" "<<mf<<endl; if ( ms >= 15 && mf >=8 ) { cout<<1; return 0; } else if ( ms >= 10 && mf >=3 ){ cout<<2; return 0; } else if ( ms >=3 ){ cout<<3; return 0; } else { cout<< 4; return 0; } }
#include <bits/stdc++.h> using namespace std; long myPow(long x, long n, long m){ if(n == 0) return 1; if(n % 2 == 0) return myPow(x * x % m, n / 2, m); else return x * myPow(x, n - 1, m) % m; } int main() { long N, M; cin >> N >> M; cout << myPow(10,N,M*M) / M << endl; system("pause"); return 0; }
#include <iostream> #include <algorithm> using namespace std; int main(){ string s; cin>>s; reverse(s.begin(),s.end()); for(auto& a:s){ if (a=='6') { a='9'; } else if(a=='9') { a='6'; } } cout<<s<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { string s, S; cin >> s; int n; n = s.size(); for (int i = n - 1; -1 < i; i--){ if (s.at(i) == '6') S += '9'; if (s.at(i) == '9') S += '6'; if (s.at(i) != '9' && s.at(i) != '6') S += s.at(i); if (i == 0) cout << S << endl; } }
#include<iostream> #include<iomanip> #include<string> #include<vector> #include<algorithm> #include<utility> #include<tuple> #include<map> #include<queue> #include<deque> #include<set> #include<stack> #include<numeric> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> using namespace std; struct Edge { int to; long long weight; Edge() : to(0), weight(0) {} Edge(int to, long long weight) : to(to), weight(weight) {} Edge(const Edge& e) { to = e.to; weight = e.weight; } bool operator>(const Edge &e) const { return weight > e.weight; } bool operator<(const Edge &e) const { return weight < e.weight; } bool operator==(const Edge &e) const { return weight == e.weight; } bool operator<=(const Edge &e) const { return weight <= e.weight; } bool operator>=(const Edge &e) const { return weight >= e.weight; } }; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using Graph = vector<vector<int>>; using weightedGraph = vector<vector<Edge>>; using heap = priority_queue<int, vector<int>, greater<int>>; const ll BIL = 1e9; const ll MOD = 1e9 + 7; const ll INF = 1LL << 60; const int inf = 1 << 29; const ld PI = 3.141592653589793238462643383; int main(int argc,char* argv[]){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); int l, r; cin >> l >> r; vector<int> sp(r+1, -1); vector<vector<ll>> p(r+1); for(int i=2;i<=r;i++) { if(sp[i] < 0) { for(int j=1;j*i<=r;j++) sp[i*j] = i; } } for(int i=2;i<=r;i++) { int k = i; set<int> ck; while(sp[k] != k) { if(ck.find(sp[k]) == ck.end()) p[i].push_back(sp[k]); ck.insert(sp[k]); k /= sp[k]; } if(ck.find(k) == ck.end()) p[i].push_back(k); sort(p[i].begin(), p[i].end()); } ll ans = 0; for(int i=l;i<=r;i++) { if(i < 2) continue; ll t = 0; vector<ll>& q = p[i]; int sz = q.size(); for(int b=1;b<(1<<sz);b++) { int cnt = 0; int k = 1; for(int j=0;j<sz;j++) { if((1<<j) & b) { cnt++; k *= q[j]; } } if(k == 1) continue; ll s = r/k - (i-1)/k; if(cnt % 2 == 0) s *= -1; t += s; } t -= r/i; ans += t; } cout << 2*ans << endl; return 0; }
#include<bits/stdc++.h> using namespace std; const int Mod=998244353; namespace modular{ int add(int a,int b){return a+b>=Mod?a+b-Mod:a+b;} int mul(int a,int b){return 1ll*a*b%Mod;} }using namespace modular; #define cout cerr #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; typedef pair<int,int> pii; #define FR first #define SE second inline void rd(int &x){ x=0;char ch=getchar();int f=1; while(ch<'0'||ch>'9'){ if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=x*10+ch-'0'; ch=getchar(); } x*=f; } int n,m; #define Maxn 5005 pii F[Maxn][2]; pii addt(pii a,pii b){return pii(add(a.FR,b.FR),add(a.SE,b.SE));} pii mult(pii a,pii b){return pii(mul(a.FR,b.FR),add(mul(a.FR,b.SE),mul(a.SE,b.FR)));} int DP(int x){ F[0][0]=pii(1,0); rep(i,1,n){ F[i][0]=F[i][1]=pii(0,0); F[i][0]=addt(F[i-1][0],F[i-1][1]);F[i][0]=mult(F[i][0],pii(x,0)); F[i][1]=mult(F[i-1][0],pii(1,1)); F[i][1]=addt(F[i][1],mult(F[i-1][1],pii(1,0))); F[i][0]=addt(F[i][0],mult(F[i-1][0],pii(m-1-x,0))); F[i][1]=addt(F[i][1],mult(F[i-1][1],pii(m-1-x,0))); } return add(F[n][1].SE,F[n][0].SE); } int main(){ rd(n);rd(m); int res=0; for(int i=0;i<m;++i)res=add(res,DP(i)); printf("%d\n",res); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int N; long long X; cin >> N >> X; vector<long long> A(N); for(int i = 0; i < N; i++) cin >> A[i]; long long ans = X; for(int i = 1; i <= N; i++){ vector<vector<vector<long long>>> dp(N + 1, vector<vector<long long>> (i + 1, vector<long long>(i, -1))); dp[0][0][0] = 0; for(int j = 0; j < N; j++){ for(int k = 0; k <= i; k++){ for(int l = 0; l < i; l++){ if(k == i){ dp[j + 1][k][l] = max(dp[j + 1][k][l], dp[j][k][l]); continue; } if(dp[j][k][l] == -1) continue; dp[j + 1][k + 1][(l + A[j]) % i] = max(dp[j + 1][k + 1][(l + A[j]) % i], dp[j][k][l] + A[j]); dp[j + 1][k][l] = max(dp[j + 1][k][l], dp[j][k][l]); } } } int ind = X % i; if(dp[N][i][ind] == -1) continue; long long t = (X - dp[N][i][ind]) / i; ans = min(ans, t); } cout << ans << endl; }
#include<bits/stdc++.h> #define pb push_back #define pl pair<ll,ll> #define pll pair<ll,pair<ll,ll>> #define ll long long #define vl vector<ll> #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); #define mp make_pair #define w(t) int t;cin>>t;while(t--) #define inf 1e18 #define fi(a,n) for(ll i=a;i<n;i++) #define fr(i,a,b) for(ll i=a;i>=b;i--) #define fj(a,n) for(ll j=a;j<n;j++) #define k(a,n) for(ll k=a;k<n;k++) #define endl "\n" #define all(v) (v).begin(),(v).end() #define set1(x) __builtin_popcount(x) #define gcd(a,b) __gcd(a,b) #define mem1(a) memset(a,-1,sizeof(a)) #define mem0(a) memset(a,0,sizeof(a)) using namespace std; const ll mod= 1000000007; const ll N =1000005; void solve() { ll h,w; cin>>h>>w; ll a[h][w]; fi(0,h) { fj(0,w) { char k; cin>>k; if(k=='#') a[i][j]=1; else a[i][j]=0; } } ll ans=0; fi(0,h-1) { fj(0,w-1) { ll s=a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1]; if(s&1) { ans+=1; } } } cout<<ans<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("input.txt", "r", stdin); // for writing output to output.txt freopen("output.txt", "w", stdout); #endif // w(t) solve(); }
/* これを入れて実行 g++ code.cpp ./a.out */ #include <iostream> #include <cstdio> #include <stdio.h> #include <vector> #include <string> #include <cstring> #include <queue> #include <deque> #include <stack> #include <algorithm> #include <utility> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cmath> #include <math.h> #include <tuple> #include <iomanip> #include <bitset> #include <functional> #include <cassert> #include <random> #define all(x) (x).begin(),(x).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; typedef long double ld; int dy4[4] = {-1, 0, +1, 0}; int dx4[4] = {0, +1, 0, -1}; int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const long long INF = 1LL << 61; const ll MOD = 1e9 + 7; bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){ return f.second > s.second; } ll gcd(ll a, ll b){ if (b == 0)return a; return gcd(b, a % b); } ll lcm(ll a, ll b){ return a / gcd(a, b) * b; } ll conbinationMemo[61][31]; void cmemoInit(){ rep(i, 61){ rep(j, 31){ conbinationMemo[i][j] = -1; } } } ll nCr(ll n, ll r){ if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r]; if(r == 0 || r == n){ return 1; } else if(r == 1){ return n; } return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1)); } ll nPr(ll n, ll r){ r = n - r; ll ret = 1; for (ll i = n; i >= r + 1; i--) ret *= i; return ret; } //-----------------------ここから----------- ll n; ll q; vector<vector<ll>> g; vector<vector<ll>> l; ll in[200200]; ll out[200200]; void dfs(ll now, ll pa, ll d, ll &time){ in[now] = time; l[d].push_back(time); time++; rep(i, g[now].size()){ ll next = g[now][i]; if(next == pa) continue; ll nextd = d + 1; dfs(next, now, nextd, time); } out[now] = time; time++; } int main(void){ cin >> n; g.resize(n); l.resize(n); rep(i, n - 1){ ll p; cin >> p; p--; g[p].push_back(i + 1); } ll time = 0; dfs(0, -1, 0, time); cin >> q; rep(i, q){ ll u, d; cin >> u >> d; u--; auto itr1 = lower_bound(all(l[d]), in[u]); auto itr2 = lower_bound(all(l[d]), out[u]); cout << itr2 - itr1 << endl; } }
#if _MSC_VER #define _CRT_SECURE_NO_WARNINGS #endif #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> using namespace std; #ifndef ONLINE_JUDGE const bool DEBUG = true; #else const bool DEBUG = false; #endif // Reads a 1D vector template <class T> void read1d(vector<T> & arg) { const int l = arg.size(); for (int i = 0; i < l; ++i) cin >> arg[i]; } void read() {} // Reads arbitrary values template <class T1, class... T2> void read(T1& var1, T2&... var2) { cin >> var1; read(var2...); } void pln() { cout << (DEBUG ? "]\n" : "\n"); } template <class T1, class... T2> void pln(const T1& var1, const T2& ... var2) { cout << var1 << " "; pln(var2...); } // Prints arbitrary values and goes to next line template <class T1, class... T2> constexpr void print(const T1& var1, const T2& ... var2) { cout << (DEBUG ? " [ " : ""); pln(var1, var2...); } // Prints arbitrary values and goes to next line ONLY IF IN DEBUG MODE template <class T1, class... T2> constexpr void debug(const T1& var1, const T2& ... var2) { if (!DEBUG) return; cout << " debug : [ "; pln(var1, var2...); } using ll = long long; using ull = unsigned long long; using ld = long double; #define all(x) begin(x), end(x) const bool MULTIPLE_TEST_CASES = 0; void exec() { int a, b; read(a, b); int ans = 4; if (a + b>= 3) ans = 3; if (a + b >= 10 && b >= 3) ans = 2; if (a + b >= 15 && b >= 8) ans = 1; print(ans); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (!MULTIPLE_TEST_CASES) exec(); else { int n; cin >> n; while (n--) exec(); } return 0; }
#include<bits/stdc++.h> using namespace std; int k; long long n, a[50]; long long g1(long long x){ int len = 0; while(x){ a[len ++] = x%10; x /= 10; } sort(a, a+len); for(int i = 0; i < len; ++ i){ x *= 10; x += a[i]; } // cout << x << endl; return x; } long long g2(long long x){ int len = 0; while(x){ a[len ++] = x%10; x /= 10; } sort(a, a+len); for(int i = len-1; i >= 0; -- i){ x *= 10; x += a[i]; } // cout << x << endl; return x; } int main(){ cin >> n >> k; while(k --){ n = g2(n) - g1(n); } cout << n <<endl; }
#include <iostream> #include <climits> #include <set> #include <string> #include <algorithm> #include <vector> #define MAX 1000000007 using namespace std; #define ll long long #define dbg if(0) #define ISRANGE(val,lo,hi) ((lo<=val)&&(val<hi)) int main(){ ll ans,a,b; cin>>a>>b; ans=(3-(a+b)%3)%3; cout <<ans<<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(a) int(a.size()) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define debug(a) { cerr << #a << ':' << a << endl; } #define endl '\n' #define fi first #define se second using lint = long long; using P = pair<int,int>; 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}; template<class T> T ceil(const T &a, const T &b) { return (a+b-1)/b; } template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } 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> 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; cin >> n; n *= 108; n /= 100; if(n < 206) puts("Yay!"); if(n == 206) puts("so-so"); if(n > 206) puts(":("); }
#include <bits/stdc++.h> using namespace std; int main() { string S; cin >>S; int ans=0; for(int i=0;i<10000;i++) { vector<bool> used(10); int x=i; for(int j=0;j<4;j++) { used[x%10] = true; x /=10; } bool able = true; for(int j=0;j<10;j++) { if(S[j] == 'o' && !used[j]) { able=false; } if(S[j] == 'x' && used[j]) { able=false; } } ans += able; } cout<<ans<<endl; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (n); ++i) #define replr(i, l, r) for (int i = (l); i < (r); ++i) const int mod = 1e9 + 7; vector<int> T[202020]; ll ans[202020], C[202020]; void rec(int x, ll c, int par) { c += C[x]; for (int to : T[x]) { if (to == par) continue; rec(to, c, x); } ans[x] = c; } int main() { int n, q; cin >> n; vector<int> A(n), B(n), X(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--, b--; A[i] = a; B[i] = b; T[a].push_back(b); T[b].push_back(a); } queue<P> Q; Q.push({1, 0}); while (!Q.empty()) { P p = Q.front(); Q.pop(); if (X[p.second] > 0) continue; X[p.second] = p.first; for (int to : T[p.second]) Q.push({p.first + 1, to}); } cin >> q; rep(i, q) { ll t, e, x; cin >> t >> e >> x; e--; int a = A[e], b = B[e]; if (X[a] > X[b]) { if (t == 1) C[a] += x; else { C[0] += x; C[a] -= x; } } else { if (t == 1) { C[0] += x; C[b] -= x; } else C[b] += x; } } rec(0, 0, -1); rep(i, n) cout << ans[i] << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" map<ll,vector<ll>>adjlist; void dfs(ll curr,ll P,vector<ll>&parent){ parent[curr]=P; for(auto i:adjlist[curr]){ if(i!=P){ dfs(i,curr,parent); } } return; } void Sum(ll rt,ll P,ll val,vector<ll>&ans){ ans[rt]+=val; for(auto i:adjlist[rt]){ if(i!=P){ Sum(i,rt,ans[rt],ans); } } return; } void solve(){ ll n; cin>>n; vector<pair<ll,ll>>edge(n); for(ll i=1;i<n;i++){ ll x,y; cin>>x>>y; adjlist[x].push_back(y); adjlist[y].push_back(x); edge[i]={x,y}; } vector<ll>parent(n+1); dfs(1,1,parent); ll q; cin>>q; vector<ll>ans(n+1,0); for(ll i=0;i<q;i++){ ll t,e,x; cin>>t>>e>>x; ll a=edge[e].first; ll b=edge[e].second; ll p=parent[a]; if(t==1){ if(p!=b){ ans[1]+=x; ans[b]-=x; } else{ ans[a]+=x; } } else{ if(p!=b){ ans[b]+=x; } else{ ans[1]+=x; ans[a]-=x; } } } Sum(1,1,0,ans); for(ll i=1;i<=n;i++){ cout<<ans[i]<<endl; } return; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long int main(){ ll i,j,k,n,m,t,c=0; cin>>n>>m>>t; k = n; ll a[m][2]; for(i=0;i<m;i++){ cin>>a[i][0]>>a[i][1]; } n -= a[0][0]; if(n<1){ c = 1; } n+=a[0][1]-a[0][0]; if(n>k){ n = k; } for(i=1;i<m;i++){ n -= a[i][0]-a[i-1][1]; if(n<1){ c = 1; break; } n += a[i][1] - a[i][0]; if(n>k){ n = k; } } n -= t-a[m-1][1]; if(n<1){ c = 1; } if(c==1){ cout<<"No"; } else{ cout<<"Yes"; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define _GLIBCXX_DEBUG typedef long long int ll; typedef long double ld; const int inf = 1e8; const ll llinf = 1e18; const double PI = acos(-1); #define yes "Yes" #define no "No" #define all(n) n.begin(), n.end() template <class T> void chmin(T &a, T b) { if (a > b) a = b; } template <class T> void chmax(T &a, T b) { if (a < b) a = b; } struct UnionFind { vector<int> par, siz; UnionFind(int n) : par(n, -1), siz(n, 1) {} int root(int x) { if (par[x] == -1) return x; return par[x] = root(par[x]); } bool isSame(int x, int y) { return root(x) == root(y); } bool unite(int x, int y) { x = root(x), y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); par[y] = x; siz[x] += siz[y]; return true; } int size(int x) { return siz[root(x)]; } }; using Graph = vector<vector<int>>; vector<int> visited(false); const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; // const int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; // const int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; void dfs(const Graph &G, int v) { visited[v] = true; for (auto next : G[v]) { if (visited[next]) continue; dfs(G, next); } } bool isPrime(ll n) { if (n < 2) return false; for (ll i = 2; i * i <= n; ++i) if (n % i == 0) return false; return true; } // 最大公約数 ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最小公倍数 ll lcm(ll a, ll b) { return abs(a) / gcd(a, b) * abs(b); } ll gcd2(const vector<ll> &v) { ll val = v[0]; for (ll i = 1; i < v.size(); ++i) val = gcd(val, v[i]); return val; } ll lcm2(const vector<ll> &v) { ll val = v[0]; for (ll i = 1; i < v.size(); ++i) val = lcm(val, v[i]); return val; } vector<ll> divisors(ll n) { vector<ll> a; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { a.push_back(i); if (n / i != i) a.push_back(n / i); } } sort(a.begin(), a.end()); return a; } // pairを要素に持つvectorをsecondを基準にソートする比較関数 bool comp(pair<ll, ll> a, pair<ll, ll> b) { return a.second < b.second; } vector<int> eratosthenes(int n) { vector<bool> is_prime(n + 1, true); vector<int> p; for (int i = 2; i <= n; ++i) { if (is_prime[i]) { for (int j = i * 2; j <= n; j += i) { is_prime[j] = false; } p.push_back(i); } } return p; } int main() { vector<int> a(3); rep(i, 3) cin >> a[i]; sort(all(a)); if ((a[0] == a[1] && a[1] == a[2]) || (a[2] - a[1] == a[1] - a[0])) cout << yes << endl; else cout << no << endl; // cout << fixed << setprecision(9) << ans << endl; }
#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(v) for (auto &e : v) { cin >> e; } #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, N); IN(int, M); vector<ll> As(N); INALL(As); vector<vector<int> > citis(N, vector<int>()); REP(i, M) { IN(int, X); IN(int, Y); X--; Y--; citis.at(Y).emplace_back(X); } vector<ll> maxs(N, -10000000000); RREP(i, N) { for (auto &city : citis.at(i)) { chmax(maxs.at(city), max(As.at(i), maxs.at(i))); } } ll ans = -10000000000; REP(i, N) { chmax(ans, maxs.at(i) - As.at(i)); // cout << "i: " << i << ", A: " << As.at(i) << ", max: " << maxs.at(i) << ", cal: " << maxs.at(i) - As.at(i) << ", ans: " << ans << endl; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define f(i,j,n) for(int i = j; i <= n; i++) #define r(i,n,j) for(int i = n; i >= j; i--) #define all(container) container.begin() , container.end() #define sz(container) (int)container.size() #define ff first #define ss second #define pii pair <int , int> #define sp(x) setprecision(x) #define mod 1000000007 #define endl "\n" #define pb push_back #define mp make_pair #define T int ttt; cin >> ttt; while(ttt--) #define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL); int power(int x, int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p; } return res; } int n,e,a[18][3],dp[18][600005]; int solve(int i,int mask){ if(mask==e) return ( abs(a[0][0]-a[i][0]) + abs(a[0][1]-a[i][1]) + max(0LL ,a[0][2]-a[i][2])); int &x = dp[i][mask]; if(x!=1e18) return x; f(j,0,n-1) { if( (mask & (1LL<<j)) ==0){ int c= abs(a[j][0]-a[i][0]) + abs(a[i][1]-a[j][1]) + max(0LL ,a[j][2]-a[i][2]); x =min(x,solve(j,mask ^ (1LL<<j) ) + c); } } return x; } int32_t main() { fast cin>>n; e= 1ll<<n; e--; f(i,0,n) f(j,0,e) dp[i][j]=1e18; f(i,0,n-1) cin>>a[i][0]>>a[i][1]>>a[i][2]; cout<<solve(0,1LL<<0LL)<<endl; }
#include <bits/stdc++.h> using namespace std; const int INF = 1000000001; void chmin(int& x, int y){ x = min(x,y); } int main() { int N; cin >> N; vector<int> X(N); vector<int> Y(N); vector<int> Z(N); for (int i = 0; i < N; ++i) { cin >> X[i] >> Y[i] >> Z[i]; } int N2 = 1<<N; vector<vector<int>> dp(N2, vector<int>(N, INF)); vector<vector<int>> dist(N, vector<int>(N)); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { int current = abs(X[i]-X[j]); current += abs(Y[i]-Y[j]); current += max(0, Z[j]-Z[i]); dist[i][j] = current; } } for (int i = 0; i < N; ++i) { if (i == 0) { continue; } dp[1<<i][i] = dist[0][i]; } for (int i = 0; i < N2; ++i) { for (int j = 0; j < N; ++j) { if (~i>>j&1) { continue; } for (int k = 0; k < N; ++k) { if (i>>k&1) { continue; } chmin(dp[i|1<<k][k], dp[i][j] + dist[j][k]); } } } cout << dp[N2 - 1][0] << endl; return 0; }
#include "bits/stdc++.h" #define rep(i,b) for(ll i=0;i<b;i++) #define ll long long using namespace std; /*--Input//////////////////////////////////////////////////*/ inline void IN(void){return;} template <typename First, typename... Rest> void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return; } #define SS(T, ...) T __VA_ARGS__; IN(__VA_ARGS__); #define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i; /*--Output/////////////////////////////////////////////////*/ #define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl #define First(n) cout << ((n) ? "First" : "Second" ) << endl inline int p(void){cout << endl; return 0;} template<class Head> int p(Head&& head){cout << head;p();return 0;} template<class Head,class... Tail> int p(Head&& head,Tail&&... tail){cout<<head<<" ";p(forward<Tail>(tail)...);return 0;} struct ProconInit { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; ProconInit() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); if(AUTOFLUSH) cout << unitbuf; } } PROCON_INIT; #define vl vector<ll> #define debug(x) cerr<<#x<<" "<<x<<'\n'; #define all(x) (x).begin(),(x).end() ll GCD(ll a, ll b) { if(a < b) return GCD(b, a); if(b==0){ return a; }else{ return GCD(b,a%b); } } template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } bool cmp(pair<ll,ll> a, pair<ll,ll> b) { return a.second < b.second; } ll MOD=1e9+7; const int MAXN=17; int n; int d[MAXN][MAXN]; int dp[1<<MAXN][MAXN]; int rec(int S, int v) { if(dp[S][v] >= 0) return dp[S][v]; if(S==(1<<n)-1 && v==0) return dp[S][v]=0; int tmp=MOD; rep(u,n) if(!(S>>u & 1)) tmp=min(tmp, rec(S|1<<u,u)+d[v][u]); return dp[S][v]=tmp; } int main() { cin>>n; vl x(n); vl y(n); vl z(n); rep(i,n)cin>>x[i]>>y[i]>>z[i]; rep(i,n) rep(j,n) d[i][j]=d[j][i]=MOD; rep(i,n){ rep(j,n){ if(i!=j)d[i][j]=abs(x[j]-x[i])+abs(y[j]-y[i])+max(0LL,z[j]-z[i]); } } fill((int *)dp, (int *)(dp+(1<<MAXN)), -1); cout << rec(0,0) << endl; }
#include <bits/stdc++.h> #include <math.h> #include <cmath> using namespace std; using ll =long long; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; using vs = vector<string>; using vvs = vector<vs>; using vc = vector<char>; using vvc = vector<vc>; using vb = vector<bool>; using vvb = vector<vb>; 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; } const long long INF = 1LL << 60; typedef pair<int, int> pint; typedef pair<ll, ll> pll; #define rep(i,n) ;for( int i =0; i < n ; i ++) #define all(a) a.begin(),a.end() #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define fr(i,a,b) for(int i=a;i<b;i++) #define pri(a) printf("%.14lf\n",a); #define MOD 1000000007 bool is_int_lround(double x){ return std::lround(x)==x; } ll keta(ll x){ ll n=0; while(x>0){ x /=10 ; n ++; } return n; } ll conbi(int n,int m){ cin>>n>>m; vector<ll> a(100); a[0] =1; for(int i=0;i<14;i++){ a[i+1]=a[i]*(i+1); } return a[n] /(a[m] *a[n-m]); } long long modpow(long long a, long long n, long long mod) { long long res = 1;//繰り返し二乗法 while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } ll kaijo(ll x){ ll z=1; if(x==0){ return 1; } while(x>0){ z *=x; z %=MOD; x--; } return z; } ll yakusu_num(ll n){ vl yakusu(n+1,1); for(ll i=2;i*i<=n;i++){ while(n%i==0){ n /=i; yakusu[i]++; } } if(n!=1)yakusu[n]++; ll num=1; for(ll i=0;i <=n;i++){ num*=yakusu[i]; } return num; } vi dx= {0,-1,0,1}; vi dy= {1,0,-1,0}; struct UnionFind { vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化 for(int i = 0; i < N; i++) par[i] = i; } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { // xとyの木を併合 int rx = root(x); //xの根をrx int ry = root(y); //yの根をry if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; //cout<<""<<endl; int main(){ ll N;cin>>N; vl a(1000004); ll cnt=0; for(ll i=1;i*i<=2*N;i++){ if((2*N)%i!=0)continue; ll A=i,B=2*N/i; if((B+A-1)%2==0&&(B-A-1)%2==0)cnt++; } cout<<2*cnt<<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; #define int long long #define endl '\n' #define pb push_back #define fi first #define se second #define all(c) (c).begin(),(c).end() typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define TRACE #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1){ cout << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args){ const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); } #else #define trace(...) #endif const ll inf = 2e18; const int mod = 1e9 + 7; const int N = 2e5 + 10; signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; int ans = 0; for(int i = 1; i * i <= 2 * n; i++) { if((2 * n) % i == 0 && ((i + ((2 * n) / i)) & 1)) { ans += 2; } } cout << ans; return 0; }
#include <bits/stdc++.h> #define MAX 1000005 #define M 1000000000 using namespace std; #define ll long long int // bool prime[MAX+1]; // ll ans[MAX]; // void seive() // { // memset(prime, true, sizeof(prime)); // for (int p=2; p*p<=MAX; p++) // { // if (prime[p] == true) // { // for (int i=p*p; i<=MAX; i += p) // prime[i] = false; // } // } // } // void solve() // { // } int main() { int n, ans = -1; ll x, c = 0; cin >> n >> x; x *= 100; for (int i = 1; i <= n; i++) { ll v, p; cin >> v >> p; c += v * p; if (c > x && ans == -1) ans = i; } cout << ans << endl; return 0; }
#include <iostream> #include <sstream> #include <cstdio> #include <vector> #include <cmath> #include <queue> #include <string> #include <cstring> #include <cassert> #include <iomanip> #include <algorithm> #include <set> #include <map> #include <ctime> #include <cmath> using namespace std; #define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define int long long #define endl '\n' #define sz(v) int(v.size()) #define all(v) v.begin(), v.end() #define pb push_back #define mp make_pair #define F first #define S second #define pt pair<int, int> #define forf(i, a, b) for (int i = (a); i < (b); i++) #define forb(i, a, b) for (int i = (b); i >= (a); i--) #define fo(i, n) forf(i, 0, n) #define fob(i, n) forb(i, 0, n - 1) template<typename X> inline X abs(const X& a) { return a < 0? -a: a; } template<typename X> inline X sqr(const X& a) { return a * a; } typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<vector<int>> vvi; typedef vector<pair<int, int>> vpii; using namespace std; const int INF = 1000 * 1000 * 1000; const int EPS = 1e-9; const int PI = acos(-1.0); int32_t main () { fastio #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif cerr << setprecision(10) << fixed; int n,x; cin >> n >> x; x = x*100; vi a; forf(i,0,n) { int v,p; cin >> v >> p; a.pb(v*p); } int sum = 0; forf(i,0,n) { sum += a[i]; if(sum > x) { cout << i + 1 << endl; return 0; } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; #define RST(i,n) memset(i,n,sizeof i) #define SZ(a) (int)a.size() #define ALL(a) a.begin(),a.end() #define X first #define Y second #define eb emplace_back #ifdef cold66 #define debug(...) do{\ fprintf(stderr,"LINE %d: (%s) = ",__LINE__,#__VA_ARGS__);\ _do(__VA_ARGS__);\ }while(0) template<typename T>void _do(T &&_x){cerr<<_x<<endl;} template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);} template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";} template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb) { _s<<"{"; for(It _it=_ita;_it!=_itb;_it++) { _s<<(_it==_ita?"":",")<<*_it; } _s<<"}"; return _s; } template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));} template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;} #define IOS() #else #define debug(...) #define pary(...) #define endl '\n' #define IOS() ios_base::sync_with_stdio(0);cin.tie(0); #endif // cold66 //} const ll MAXN=1e2+5,MAXlg=__lg(MAXN)+2; const ll MOD=1000000007; const ll INF=0x3f3f3f3f; const int MAXM = 1e4+5; vector<pii> edge; int c[MAXN], ans[MAXM]; // 0:-> 1:<- vector<pii> e[MAXN]; bool vis[MAXN]; void print(int x){ if (x) cout << "<-" << endl; else cout << "->" << endl; } void dfs(int x){ vis[x] = true; for (auto i:e[x]) { pii cur = edge[i.X]; int to; if (i.Y == 0) to = cur.Y; else to = cur.X; ans[i.X] = i.Y; if (vis[to]) continue; dfs(to); } } signed main(){ IOS(); int n,m; cin >> n >> m; for (int i=0;i<m;++i) { int u,v; cin >> u >> v; u--, v--; edge.eb(u,v); ans[i] = -1; } for (int i=0;i<n;++i) cin >> c[i]; for (int i=0;i<m;++i) { int u = edge[i].X, v = edge[i].Y; if (c[u] == c[v]) { e[u].eb(i,0); e[v].eb(i,1); continue; } if (c[u] > c[v]) ans[i] = 0; else ans[i] = 1; } for (int i=0;i<n;++i) { if (vis[i]) continue; dfs(i); } for (int i=0;i<m;++i) print(ans[i]); }
#include <bits/stdc++.h> using namespace std; const int max_n = 11, inf = 1000111222; const int max_m = 100011; int n, m, a[max_n], dp[max_n][max_n]; pair<int, int> b[max_m]; vector<pair<int, int>> v; int solve() { for (int i = 0; i < n; ++i) { if (a[i] > v[0].first) { return inf; } } for (int len = 1; len <= n; ++len) { for (int l = 0; l + len <= n; ++l) { const int r = l + len - 1; int tot = accumulate(a + l, a + r + 1, 0); auto it = upper_bound(v.begin(), v.end(), make_pair(tot, -1)); dp[l][r] = 0; if (it != v.begin()) { --it; dp[l][r] = it->second; } for (int i = l + 1; i < r; ++i) { dp[l][r] = max(dp[l][r], dp[l][i] + dp[i][r]); } //cout << l << " " << r << ": " << dp[l][r] << endl; } } return dp[0][n - 1]; } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < m; ++i) { cin >> b[i].second >> b[i].first; } sort(b, b + m); for (int i = 0; i < m; ++i) { if (v.empty() || v.back().second < b[i].second) { v.push_back(b[i]); } } sort(a, a + n); int ans = inf; do { ans = min(ans, solve()); } while (next_permutation(a, a + n)); if (ans == inf) { ans = -1; } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> #include <cstdlib> using namespace std; #define FS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ll long long int #define pb push_back #define S second #define F first //#define int long long int #define endl "\n" ll MOD=1e9+7; void solve() { ll x,y; cin>>x>>y; ll k=min(x,y)+3; ll m=max(x,y); if(k>m) cout<<"Yes"<<endl; else cout<<"No"; } signed main(){ FS; int t=1; //cin >> t; while(t--) { solve(); } return 0; }
#include "bits/stdc++.h" using namespace std; int main(){ int A, B; cin>>A>>B; if(A<B&&A+3>B||B<A&&B+3>A) cout<<"Yes"<<endl; else cout<<"No"<<endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; ll n; ll f(ll k){ return k*(k+1)/2; } bool ok(ll k){ return f(k) <= n+1 and n+1 < f(k+1); } int main(void){ cin >> n; ll left = 1, right = n, k; while(true){ ll m = (left+right)/2; if(m<=2*(n+1)/(m+1) and 2*(n+1)/(m+1)<m+2){ k = m; break; } else if(m+1<2*(n+1)/m){ left = m+1; } else { right = m-1; } } cout << n-k+1 << endl; return 0; } /* * 109109109109109109 * 109109108641970782 */
#include <bits/stdc++.h> using namespace std; /* エイリアス */ #define ll long long #define ld long double #define vi vector<int> #define vll vector<ll> #define pii pair<int,int> #define pll pair<ll,ll> #define vpi vector<pii> #define vpll vector<pll> #define endl '\n' #define fi first #define se second #define pb push_back #define eb emplace_back #define em emplace #define mp make_pair /* 関数マクロ */ #define rep(i, n) for (ll i = 0; i < n; ++i) #define rep2(i, n, m) for (ll i = n; i <= m; ++i) #define rep3(i, n, m) for (ll i = n; i >= m; --i) #define all(v) v.begin(), v.end() #define si(v) int(v.size()) #define UNIQUE(v) sort(all(v)), v.erase(unique(all(v)),v.end()) /* 定数 */ const ll mod = 1e9 + 7; const ll infll = (1LL << 62) - 1; const ll inf = (1LL << 30) - 1; /* その他 */ template<class S, class T> inline bool chmax(S &a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class S, class T> inline bool chmin(S &a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T> using pq = priority_queue<T>; template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); vi a(4); rep(i, 4) cin >> a[i]; sort(all(a)); int sum = accumulate(all(a),0); do { int s = 0; rep(i, 4) { s += a[i]; int eat = sum - s; if (s == eat) { cout << "Yes" << endl; return 0; } } } while (next_permutation(all(a))); cout << "No" << endl; }
#include<bits/stdc++.h> #define rep(i, n) for (int i = 0, length = n; i < length; i++) #define fi first #define se second #define lb lower_bound #define ub upper_bound #define ep emplace #define epb emplace_back #define scll static_cast<long long> #define sz(x) static_cast<int>((x).size()) #define pfll(x) printf("%lld\n", x) #define ci(x) cin >> x #define ci2(x, y) cin >> x >> y #define ci3(x, y, z) cin >> x >> y >> z #define ci4(w, x, y, z) cin >> w >> x >> y >> z #define co(x) cout << x << endl #define co2(x, y) cout << x << " " << y << endl #define co3(x, y, z) cout << x << " " << y << " " << z << endl using namespace std; typedef long long ll; typedef pair<int, int> P; typedef priority_queue<int> PQ; typedef priority_queue<int, vector<int>, greater<int>> PQG; typedef priority_queue<P> PQP; typedef priority_queue<P, vector<P>, greater<P>> PQPG; const int MAX_N = 2e5, MOD = 998244353, INF = 1e9; int h, w, k, ans[5000][5000], cntx[5000][5000], cnty[5000][5000], po3[5001]; P p[MAX_N]; char c[MAX_N]; int main() { ci3(h, w, k); rep(i, k) { int x, y; char z; ci3(x, y, z); cntx[x - 1][y - 1]--; cnty[x - 1][y - 1]--; p[i].fi = (x - 1) * w + y - 1; p[i].se = i; c[i] = z; } rep(i, h) rep(j, w) { cntx[i][j]++; cnty[i][j]++; } po3[0] = 1; rep(i, 5000) po3[i + 1] = 3L * po3[i] % MOD; rep(i, w) rep(j, h - 1) cntx[j + 1][i] += cntx[j][i]; rep(i, h) rep(j, w - 1) cnty[i][j + 1] += cnty[i][j]; sort(p, p + k); int ind = 0; ans[0][0] = 1; rep(i, h) { rep(j, w) { if (ind < k && i == p[ind].fi / w && j == p[ind].fi % w) { if (c[p[ind].se] != 'D' && j < w - 1) { ans[i][j + 1] = (ans[i][j + 1] + scll(ans[i][j]) * po3[cntx[h - 1][j] - cntx[i][j]] % MOD) % MOD; } if (c[p[ind].se] != 'R' && i < h - 1) ans[i + 1][j] = scll(ans[i][j]) * po3[cnty[i][w - 1] - cnty[i][j]] % MOD; ind++; continue; } if (j < w - 1) ans[i][j + 1] = (ans[i][j + 1] + 2L * ans[i][j] * po3[cntx[h - 1][j] - cntx[i][j]] % MOD) % MOD; if (i < h - 1) ans[i + 1][j] = 2L * ans[i][j] * po3[cnty[i][w - 1] - cnty[i][j]] % MOD; } } if (k == 0 || p[k - 1].fi != h * w - 1) ans[h - 1][w - 1] = 3L * ans[h - 1][w - 1] % MOD; co(ans[h - 1][w - 1]); return 0; }
// Author: wlzhouzhuan #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pb push_back #define fir first #define sec second #define rep(i, l, r) for (int i = l; i <= r; i++) #define per(i, l, r) for (int i = l; i >= r; i--) #define mset(s, t) memset(s, t, sizeof(s)) #define mcpy(s, t) memcpy(s, t, sizeof(t)) template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar(); return f ? -x : x; } template<typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } template<typename T> void print(T x, char let) { print(x), putchar(let); } const int N = 5005; const int mod = 998244353; const int inv3 = (mod + 1) / 3; char a[N][N]; // 0->R, 1->D, 2->X int dp[N][N][3]; int n, m, k; void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } int mul(int x, int y) { return 1ll * x * y % mod; } int qpow(int a, int b) { int res = 1; while (b > 0) { if (b & 1) res = 1ll * res * a % mod; a = 1ll * a * a % mod; b >>= 1; } return res; } int main() { n = read(), m = read(), k = read(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { a[i][j] = '?'; } } for (int i = 1; i <= k; i++) { int x = read(), y = read(); char str[3]; scanf("%s", str); a[x][y] = str[0]; } if (a[1][1] == 'X') dp[1][1][2] = 1; else if (a[1][1] == 'R') dp[1][1][0] = 1; else if (a[1][1] == 'D') dp[1][1][1] = 1; else dp[1][1][0] = dp[1][1][1] = dp[1][1][2] = 1; if (a[1][1] == '?') k++; dp[1][1][0] = mul(dp[1][1][0], qpow(3, n * m - k)); dp[1][1][1] = mul(dp[1][1][1], qpow(3, n * m - k)); dp[1][1][2] = mul(dp[1][1][2], qpow(3, n * m - k)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == '?') { if (i > 1) { add(dp[i][j][0], 1ll * dp[i - 1][j][1] * inv3 % mod); add(dp[i][j][1], 1ll * dp[i - 1][j][1] * inv3 % mod); add(dp[i][j][2], 1ll * dp[i - 1][j][1] * inv3 % mod); add(dp[i][j][0], 1ll * dp[i - 1][j][2] * inv3 % mod); add(dp[i][j][1], 1ll * dp[i - 1][j][2] * inv3 % mod); add(dp[i][j][2], 1ll * dp[i - 1][j][2] * inv3 % mod); } if (j > 1) { add(dp[i][j][0], 1ll * dp[i][j - 1][0] * inv3 % mod); add(dp[i][j][1], 1ll * dp[i][j - 1][0] * inv3 % mod); add(dp[i][j][2], 1ll * dp[i][j - 1][0] * inv3 % mod); add(dp[i][j][0], 1ll * dp[i][j - 1][2] * inv3 % mod); add(dp[i][j][1], 1ll * dp[i][j - 1][2] * inv3 % mod); add(dp[i][j][2], 1ll * dp[i][j - 1][2] * inv3 % mod); } } else { if (i > 1) { add(dp[i][j][0], dp[i - 1][j][1]); add(dp[i][j][1], dp[i - 1][j][1]); add(dp[i][j][2], dp[i - 1][j][1]); add(dp[i][j][0], dp[i - 1][j][2]); add(dp[i][j][1], dp[i - 1][j][2]); add(dp[i][j][2], dp[i - 1][j][2]); } if (j > 1) { add(dp[i][j][0], dp[i][j - 1][0]); add(dp[i][j][1], dp[i][j - 1][0]); add(dp[i][j][2], dp[i][j - 1][0]); add(dp[i][j][0], dp[i][j - 1][2]); add(dp[i][j][1], dp[i][j - 1][2]); add(dp[i][j][2], dp[i][j - 1][2]); } } if (a[i][j] == 'R') dp[i][j][1] = dp[i][j][2] = 0; if (a[i][j] == 'D') dp[i][j][0] = dp[i][j][2] = 0; if (a[i][j] == 'X') dp[i][j][0] = dp[i][j][1] = 0; //printf("dp[%d][%d] = %d %d %d\n", i, j, dp[i][j][0], dp[i][j][1], dp[i][j][2]); } } printf("%d\n", (((long long)dp[n][m][0] + dp[n][m][1]) % mod + dp[n][m][2]) % mod); return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; template<typename T> ostream& operator<<(ostream &os, vector<T> &v){ string sep = " "; if(v.size()) os << v[0]; for(int i=1; i<v.size(); i++) os << sep << v[i]; return os; } template<typename T> istream& operator>>(istream &is, vector<T> &v){ for(int i=0; i<v.size(); i++) is >> v[i]; return is; } #ifdef DBG void debug_(){ cout << endl; } template<typename T, typename... Args> void debug_(T&& x, Args&&... xs){ cout << x << " "; debug_(forward<Args>(xs)...); } #define dbg(...) debug_(__VA_ARGS__) #else #define dbg(...) #endif void solve(){ string s; cin >> s; int ans = 1e9; int m = 0; for(auto c: "atcoder") { int x=-1, y=-1; for(int i=0; i<s.size(); i++){ if(s[i]>=c+1){ x=i; break; } } if(x>=0) ans = min(ans, x+m); for(int i=0; i<s.size(); i++){ if(s[i]>=c){ y=i; break; } } if(y>=0){ m += y; s = s.substr(0, y) + s.substr(y+1); } else { break; } } if(ans==1e9) cout << -1 << endl; else cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cout << setprecision(20) << fixed; int t; cin >> t; for(int i=0; i<t; i++) solve(); return 0; }
#include <iostream> #include <fstream> #include <iomanip> #include <cstdio> #include <cstdlib> #include <algorithm> #include <bitset> #include <set> #include <map> #include <vector> #include <string> #include <cstring> #include <queue> #include <memory.h> #include <cmath> using namespace std; #define min(a,b) ((a)<(b)?(a):(b)) #define max(a,b) ((a)>(b)?(a):(b)) #define FOR0(i,n) for( i = 0 ; i < n ; ++i ) #define FOR1(i,n) for( i = 1 ; i <= n ; ++i ) #define sys_p system( "pause" ) #define pb push_back #define mp make_pair #define FI first #define SE second #define sz(a) (int)a.size() typedef long long LL; const LL MOD = (LL)(1e9 + 7); int t, i, j, found[7], ans1, ans2; string s, x = "atcoder"; int main() { cin >> t; while (t--) { cin >> s; FOR0(i, 7) { found[i] = -1; } ans1 = ans2 = 1e9; // option 1: atcoderXXX if (s.length() > 7) { FOR0(i, s.length()) { char c = s[i]; FOR0(j, 7) { if (c == x[j] && ((found[j] < 0) || abs(i - j) < abs(i - found[j]))) { found[j] = i; } } } ans1 = 0; FOR0(j, 7) { if (found[j] < 0) { ans1 = 1e9; break; } else { ans1 += abs(found[j] - j); } } } // option 2: put larger letter somewhere int prev_cost = 0; FOR0(j, 7) { for (i = j; i < s.length(); ++i) { if (s[i] > x[j]) { ans2 = min(ans2, prev_cost + i - j); break; } } for (i = j; i < s.length(); ++i) { if (s[i] == x[j]) { s.erase(i, 1); s.insert(j, 1, x[j]); prev_cost += i - j; break; } } if (i == s.length()) { break; } } if (min(ans1, ans2) > 1e8) { cout << -1 << endl; } else { cout << min(ans1, ans2) << endl; } } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const double pi=3.1415926535897932384626; inline ll read(){ ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } const ll mod=998244353; const int maxn=5e2+5; ll n,m; char s[maxn][maxn]; ll q_pow(ll x,ll y){ ll ans=1; while(y){ if(y&1) ans=ans*x%mod; y>>=1; x=x*x%mod; } return ans; } int main(){ n=read(),m=read(); for(int i=1;i<=n;++i) scanf("%s",s[i]+1); ll cnt=0,flag=1; for(int i=1;i<=n;++i){ int x=i,y=1,r=0,b=0,tmp=0; while(x>=1&&y<=m){ if(s[x][y]=='.') ++tmp; else if(s[x][y]=='R') r=1; else b=1; --x; ++y; } if(r==1&&b==1) flag=0; else if(r+b==0&&tmp) ++cnt; } for(int i=2;i<=m;++i){ int x=n,y=i,r=0,b=0,tmp=0; while(x>=1&&y<=m){ if(s[x][y]=='.') ++tmp; else if(s[x][y]=='R') r=1; else b=1; --x; ++y; } if(r==1&&b==1) flag=0; else if(r+b==0&&tmp) ++cnt; } if(!flag) printf("0\n"); else printf("%lld\n",q_pow(2,cnt)); return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int h, w; cin >> h >> w; const int mod = 998244353; string tmp; vector<vector<char>> grid(h, vector<char>(w)); for(int i = 0; i < h; ++i){ cin >> tmp; for(int j = 0; j < w; ++j){grid[i][j] = tmp[j];} } vector<vector<int>> state(h+w-1, vector<int>(3)); // ["R", "B", "."] for(int i = 0; i < h; ++i){ for(int j = 0; j < w; ++j){ if(grid[i][j] == 'R'){state[i+j][0] += 1;} if(grid[i][j] == 'B'){state[i+j][1] += 1;} if(grid[i][j] == '.'){state[i+j][2] += 1;} } } int ans = 1; for(int i = 0; i < h+w-1; ++i){ if(state[i][0] == 0 && state[i][1] == 0){ans *= 2;} if(state[i][0] != 0 && state[i][1] != 0){ans *= 0;} ans %= mod; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) (v).begin(), (v).end() using ll = long long; constexpr int INF = 1e9; constexpr long long LINF = 1e18; constexpr long long MOD = 1e9 + 7; constexpr int MAX_N = 100000; int n, m; vector<pair<int, int>> G[MAX_N]; int ans[MAX_N]; bool reached[MAX_N]; void dfs(int cur) { reached[cur] = true; for (auto p : G[cur]) { if (reached[p.first]) continue; if (ans[cur] != p.second) ans[p.first] = p.second; else ans[p.first] = (p.second + 1) % n + 1; dfs(p.first); } } signed main() { cin >> n >> m; int u, v, c; rep(i, m) { cin >> u >> v >> c; u--; v--; G[u].emplace_back(v, c); G[v].emplace_back(u, c); } ans[0] = 1; dfs(0); rep(i, n) { cout << ans[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; vector<pair<int, int>> g[N]; void solve() { int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v, c; cin >> u >> v >> c; g[u].emplace_back(v, c); g[v].emplace_back(u, c); } vector<int> color(n + 1); set<int> colors; for (int i = 1; i <= n; ++i) { colors.insert(i); } vector<int> used(n + 1); queue<int> q; used[1] = 2; for (auto &[u, c] : g[1]) { q.push(u); used[u] = 1; } while (!q.empty()) { int v = q.front(); q.pop(); used[v] = 2; for (auto& [u, c] : g[v]) { if (used[u] == 2 && color[u] != c) { color[v] = c; } else if (!used[u]) { q.push(u); used[u] = 1; } } if (!color[v]) { color[v] = *colors.begin(); } colors.erase(color[v]); } color[1] = *colors.begin(); for (int i = 1; i <= n; ++i) { cout << color[i] << "\n"; } } int main() { ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); cout.setf(ios::fixed), cout.precision(20); solve(); }
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ long long n, m, q; long long w[100],v[100]; vector<long long> x(100); vector<long long> tempx(100); long long max = 0; long long l,r; vector<pair<long long, long long>> p(100); cin >> n >> m >> q; for (long long i = 0; i < n; i++){ p[i] = make_pair(0,0); } for (long long i = 0; i < n; i++){ cin >> w[i] >> v[i]; p[i].first = v[i]; p[i].second = -w[i]; // p[i] = make_pair(v[i],-w[i]); } for (long long i = 0; i < m; i++){ cin >> x[i]; } sort(p.begin(),p.end()); for (long long i = 0; i < q ; i++){ max = 0; cin >> l >> r; for (long long j = 0; j < 100;j++){ //壊れた箱の処理 if ((j < (l-1)) || (j > (r-1))){ tempx[j] = x[j]; } else{ tempx[j] = 0; } } sort(tempx.begin(),tempx.end()); for (long long j = 0; j < n; j++){ for (long long k = 0; k < 100;k++){ if (tempx[k] >= (-p[100-j-1].second)){ tempx[k] =0; max += p[100-j-1].first; break; } } } cout << max << 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; #define MV 200001 #define LMV 21 #define ff first #define ss second #define pb push_back #define eb emplace_back #define emp emplace #define mp make_pair #define ins insert #define sz(x) (int)x.size() #define whoami(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); whi(_it, args); } void whi(istream_iterator<string> it) { cerr<<"\n"; } template<typename T, typename... Args> void whi(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<" "<<a<<" "; whi(++it, args...); } void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);} void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);} void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);} typedef long long ll; typedef long double ld; typedef vector<int> VI; typedef vector<ll> VL; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<PII, int> PPII; typedef pair<PLL, ll> PPLL; typedef map<int, int> MII; const int MOD = 1000000007; const ll INF = 4e18; 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>; struct h_llint { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; struct h_pair{ size_t operator()(const PLL&x)const{ return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32)); } }; typedef map<ll, ll> MLL; typedef map<PII, int> MPII; typedef map<PLL, ll> MPLL; typedef set<int> SI; typedef set<ll> SL; //ordered_set = order_of_key(.) //ordered_set = find_by_order(.) typedef ordered_set<int> OSI; typedef ordered_set<ll> OSL; typedef ordered_multiset<int> MOSI; typedef ordered_multiset<ll> MOSL; typedef unordered_map<ll, int, h_llint> UMLI; typedef unordered_map<ll, ll, h_llint> UMLL; typedef unordered_map<PLL, int, h_pair> UMPI; typedef unordered_map<PLL, ll, h_pair> UMPL; int ar[MV]; ll arr[MV]; PII bag[MV]; void solve(int T) { int n,m,q; cin>>n>>m>>q; for(int i=1;i<=n;i++) cin>>bag[i].ff>>bag[i].ss; sort(bag+1, bag+n+1, [](PII A, PII B){ return (A.ss > B.ss); }); for(int i=1;i<=m;i++) cin>>ar[i]; while(q--) { int l,r; cin>>l>>r; multiset<int> M; for(int i=1;i<l;i++) M.ins(ar[i]); for(int i=r+1;i<=m;i++) M.ins(ar[i]); int tt = 0; for(int i=1;i<=n;i++) { if(M.lower_bound(bag[i].ff) != M.end()) { tt += bag[i].ss; M.erase(M.lower_bound(bag[i].ff)); } } cout<<tt<<"\n"; } return; } int main(void) { FLASH(); //freopen("cowjog.in", "r", stdin); //freopen("cowjog.out", "w", stdout); int T; T = 1; #ifndef ONLINE_JUDGE time_t time_t1, time_t2; time_t1 = clock(); #endif //cin>>T; while(T--) solve(T); #ifndef ONLINE_JUDGE time_t2 = clock(); SETF(); cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n"; UNSETF(); #endif return 0; }
// 2021-04-19 00:32:20 // clang-format off #include <bits/stdc++.h> #ifdef LOCAL #include "lib/debug.hpp" #else #define debug(...) 1 #endif #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define rep(i, n) REP(i, 0, (n)) #define repc(i, n) REPC(i, 0, (n)) #define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++) #define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; } template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; // clang-format on const int MX = 400040; int n, c2[MX], c3[MX], x2, x3, y2, y3, z2, z3; string s; void answer() { cin >> n >> s; REPC(i, 1, n) { int j = i; while (j % 3 == 0) { c3[i]++; j /= 3; } if (j % 3 == 2) c2[i]++; } rep(i, n) { x2 += c2[i]; x3 += c3[i]; y2 += c2[i]; y3 += c3[i]; } int ans = 0; rep(i, n) { if (x3 == y3 + z3) { int color = 0; if (s[i] == 'B') color = 0; if (s[i] == 'W') color = 1; if (s[i] == 'R') color = 2; if ((x2 + y2 + z2) % 2) { ans += color * 2; } else { ans += color * 1; } ans %= 3; } y2 -= c2[n - i - 1]; y3 -= c3[n - i - 1]; z2 += c2[i + 1]; z3 += c3[i + 1]; } rep(i, n - 1) { (ans *= 2) %= 3; } if (ans == 0) cout << 'B' << '\n'; if (ans == 1) cout << 'W' << '\n'; if (ans == 2) cout << 'R' << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); answer(); return 0; }
#include<iostream> #include<vector> #include<queue> #include<cstring> #include<cmath> #include<map> #include<set> #include<cstdio> #include<algorithm> #define debug(a) cout<<#a<<"="<<a<<endl; using namespace std; const int maxn=4e5+1000; typedef long long LL; const LL mod=3; inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();} return x*f;} map<char,LL>map1; LL fac[maxn]; LL ksm(LL a,LL k){ LL res=1; while(k>0){ if(k&1) res=res*a%mod; k>>=1; a=a*a%mod; }return res%mod; } LL C(LL n,LL m){ if(m>n) return 0; return (fac[n]*ksm(fac[m],mod-2)%mod*ksm(fac[n-m],mod-2)%mod)%mod; } LL lucas(LL n,LL m){ if(!m) return 1; return C(n%mod,m%mod)*lucas(n/mod,m/mod)%mod; } int main(void){ cin.tie(0);std::ios::sync_with_stdio(false); fac[0]=1; for(LL i=1;i<=3;i++) fac[i]=fac[i-1]*i%mod; map1['B']=0;map1['W']=1;map1['R']=2; LL n;cin>>n; LL ans=0; for(LL i=0;i<n;i++){ char op;cin>>op; LL d=map1[op]; ans=(ans+d*lucas(n-1,i))%mod; } if(n%2==0) ans=(3-ans)%3;///ans*=(-1)^(n-1) if(ans==0){ cout<<"B"<<"\n"; } else if(ans==1){ cout<<"W"<<"\n"; } else if(ans==2){ cout<<"R"<<"\n"; } return 0; }
#include<bits/stdc++.h> #define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i) #define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i) #define foreach(i, n) for(auto &i:(n)) #define all(x) (x).begin(), (x).end() #define bit(x) (1ll << (x)) #define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE #define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__) using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; //const ll MOD = (ll)1e9+7; const ll MOD = 998244353; const int INF = (ll)1e9+7; const ll INFLL = (ll)1e18; template<class t> using vvector = vector<vector<t>>; template<class t> using vvvector = vector<vector<vector<t>>>; template<class t> using priority_queuer = priority_queue<t, vector<t>, greater<t>>; template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;} template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;} #ifdef DEBUG #define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl; #else #define debug(x) (void)0 #endif namespace templates{ ll modpow(ll x, ll b){ ll res = 1; while(b){ if(b&1)res = res * x % MOD; x = x * x % MOD; b>>=1; } return res; } ll modinv(ll x){ return modpow(x, MOD-2); } bool was_output = false; template<class t> void output(t a){ if(was_output)cout << " "; cout << a; was_output = true; } void outendl(){ was_output = false; cout << endl; } ll in(){ ll res; cin >> res; return res; } template<class t> istream& operator>>(istream&is, vector<t>&x){ for(auto &i:x)is >> i; return is; } template<class t, class u> istream& operator>>(istream&is, pair<t, u>&x){ is >> x.first >> x.second; return is; } template<class t> t in(){ t res; cin >> res; return res; } template<class t> void out(t x){ cout << x; } template<class t> vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){ sort(line.begin(),line.end(),comp); return line; } template<class t> vector<t> reversed(vector<t> line){ reverse(line.begin(),line.end()); return line; } } using namespace templates; int main(){ cout << (in()%2?"Black":"White") << endl; return 0; }
#include <iostream> #include <string> using namespace std; using ll = int64_t; ll n, k; ll numar(ll x) { ll g1 = 0, g2 = 0; int c[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; while(x) c[x % 10]++, x /= 10; for(int i = 0 ; i < 10 ; i++) for(int j = 1 ; j <= c[i] ; j++) g1 = g1 * 10 + i; for(int i = 9 ; i >= 0 ; i--) for(int j = 1 ; j <= c[i] ; j++) g2 = g2 * 10 + i; return g2 - g1; } int main() { cin >> n >> k; for(int i = 1 ; i <= k ; i++) n = numar(n); cout << n; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long double ld; typedef long long ll; using P = pair<ll,int>; const char EOLN = '\n'; #define rep(i,n) for (int i = 0; i < (n); ++i) // DP 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 INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf #define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf #define MOD 1000000007 //問題による const double pi = 3.14159265359; // メモ // vector二次元配列 // vector<vector<ll>> cnt(h,vector<ll> (w)); // bit演算 // for (int bit = 0; bit < (1<<k); ++bit) // bit & (1 << i) // 複数変数をいっきに代入 // tie(a,b,c) = make_tuple(10, 7.7, "hello"); // index が条件を満たすかどうか struct Edge{ int to,t,k; Edge(int to,int t,int k): to(to),t(t),k(k){} }; int main() { int N,M,XX,YY; cin>>N>>M>>XX>>YY; XX--;YY--; vector<vector<Edge>> G(N); for(int i=0;i<M;i++){ int 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); } vector<ll> dist(N,INF64); //dp priority_queue<P,vector<P>,greater<P>> X; X.push({0,XX}); //queueが空になるまでroop while(!X.empty()){ ll curcost=X.top().first; int from=X.top().second; X.pop(); //queueから1つ取り出す if(dist[from]<curcost) continue; for(auto x:G[from]){ //Gの行ける場所を調査 ll nx = (curcost+x.k-1)/x.k*x.k + x.t; if(chmin(dist[x.to],nx)){ //Update //X.push({nx,x.to}); //調査対象の取り出し X.push({dist[x.to],x.to}); } } } ll ans = dist[YY]; if(ans==INF64) ans=-1; cout <<ans; return 0; }
#if _MSC_VER #define _CRT_SECURE_NO_WARNINGS #endif #include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> using namespace std; #ifndef ONLINE_JUDGE const bool DEBUG = true; #else const bool DEBUG = false; #endif // Reads a 1D vector template <class T> void read1d(vector<T> & arg) { const int l = arg.size(); for (int i = 0; i < l; ++i) cin >> arg[i]; } void read() {} // Reads arbitrary values template <class T1, class... T2> void read(T1& var1, T2&... var2) { cin >> var1; read(var2...); } void pln() { cout << (DEBUG ? "]\n" : "\n"); } template <class T1, class... T2> void pln(const T1& var1, const T2& ... var2) { cout << var1 << " "; pln(var2...); } // Prints arbitrary values and goes to next line template <class T1, class... T2> constexpr void print(const T1& var1, const T2& ... var2) { cout << (DEBUG ? " [ " : ""); pln(var1, var2...); } // Prints arbitrary values and goes to next line ONLY IF IN DEBUG MODE template <class T1, class... T2> constexpr void debug(const T1& var1, const T2& ... var2) { if (!DEBUG) return; cout << " debug : [ "; pln(var1, var2...); } using ll = long long; using ull = unsigned long long; using ld = long double; #define all(x) begin(x), end(x) const bool MULTIPLE_TEST_CASES = 0; struct Edge { ll wt; ll mul; Edge() {} Edge(ll wt, ll mul) : mul(mul), wt(wt) { } }; void exec() { int n, m; read(n, m); int x, y; read(x, y); --x; --y; vector<unordered_map<int, vector<Edge>>> graph(n, unordered_map<int, vector<Edge>>()); for (int i = 0; i < m; ++i) { int from, to, time, mul; read(from, to, time, mul); --from; --to; graph[from][to].push_back(Edge(time, mul)); graph[to][from].push_back(Edge(time, mul)); } auto cmp = [&](pair<ll, int>& a, pair<ll, int>& b) { return a < b; }; vector<bool> vis(n, false); vector<ll> T(n, LLONG_MAX); priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> djk; //vis[x] = true; T[x] = 0; djk.push({ 0, x }); for (int i = 0; i < n; ++i) { while (!djk.empty() && vis[djk.top().second]) djk.pop(); if (djk.empty()) break; int root = djk.top().second; ll _T = djk.top().first; djk.pop(); vis[root] = true; //if (root == y) break; for (auto& [node, edges] : graph[root]) { if (vis[node]) continue; ll reachingAt = LLONG_MAX; for (auto& edge : edges) { // when is the next train coming? ll nextTrain = (_T % edge.mul == 0 ? _T : ((_T / edge.mul) + 1) * edge.mul); // when we reaching? reachingAt = min(reachingAt, nextTrain + edge.wt); } if (reachingAt < T[node]) { T[node] = reachingAt; djk.push({ reachingAt, node }); } } } if (T[y] == LLONG_MAX) print(-1); else print(T[y]); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (!MULTIPLE_TEST_CASES) exec(); else { int n; cin >> n; while (n--) exec(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<(int)(n); i++) #define rep1(i, n) for(int i=1; i<=(int)(n); i++) #define rep2(i, n, m) for(int i=(int)n; i<=(int)m; i++) typedef long long ll; typedef vector<int> vi; typedef vector<vi> wi; typedef vector<ll> vl; typedef vector<vl> wl; const ll inf=1LL<<60; const ll mod=1000000007; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n, m, t; cin >> n >> m >> t; vi a(m), b(m); rep(i, m)cin >> a[i] >> b[i]; int bt=n, f=0, e=0; rep(i, m){ bt-=a[i]-e; if(bt<=0){ f=1; break; } bt+=b[i]-a[i]; bt=min(n, bt); e=b[i]; } if(f==0&&bt-(t-e)>0)cout << "Yes\n"; else cout << "No\n"; return 0; }
#include <stack> #include <queue> #include <bitset> #include <unordered_set> #include <unordered_map> #include <map> #include <set> #include <tuple> #include <cmath> #include <random> #include <algorithm> #include <vector> #include <iostream> #include <fstream> #include <chrono> #include <numeric> #define fio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr) #define rng mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count()) #define ri(...) i64 __VA_ARGS__; rx(__VA_ARGS__) #define rc(...) char __VA_ARGS__; rx(__VA_ARGS__) #define rs(...) string __VA_ARGS__; rx(__VA_ARGS__) #define rvi(x, n) vi x(n); cin >> x #define rvvi(x, n, m) vvi x(n, vi(m)); cin >> x #ifndef ONLINE_JUDGE #define dbg(...) cerr << '[' << #__VA_ARGS__ << "]: "; dbg_log(__VA_ARGS__) #else #define dbg(...) ;; #endif #define fa(i, v) for (auto &i : v) #define fi(i, n) for (i32 i = 0; i < n; i++) #define fp(i, n) for (i32 i = 1; i < n; i++) #define fr(i, n) for (i32 i = n - 1; i >= 0; i--) #define fu(_i) ri(i); fi(_i, i) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() using namespace std; typedef uint32_t u32; typedef int32_t i32; typedef uint64_t u64; typedef int64_t i64; #ifdef __SIZEOF_INT128__ typedef __uint128_t u128; typedef __int128_t i128; #endif typedef vector<i64> vi; typedef vector<vi> vvi; template<typename T> inline istream &operator>>(istream &in, vector<T> &v) { fa(x, v) in >> x; return in; } template<typename T> inline ostream &operator<<(ostream &out, const vector<T> &v) { fa(x, v) out << x << ' '; return out; } template<typename T> inline void rx(T &x) { cin >> x; } template<typename T, typename... R> inline void rx(T &x, R &... l) { cin >> x; rx(l...); } template<typename T> inline void dbg_log(const T &x) { cerr << x << endl; } template<typename T, typename... R> inline void dbg_log(const T &x, const R &... l) { cerr << x << ", "; dbg_log(l...); } template<typename T> inline void wl(const T &x) { cout << x << '\n'; } template<typename T, typename... R> inline void wl(const T &x, const R &... l) { cout << x << ' '; wl<R...>(l...); } int main() { fio; ri(n, m, q); vector<tuple<i32, i32>> vs(n); fi(i, n) { ri(w, v); vs[i] = {v, w}; } sort(rall(vs)); vector<tuple<i32, i32>> xs(m); fi(i, m) { ri(x); xs[i] = {x, i}; } sort(all(xs)); vi used(m); fi(t, q) { used.clear(); used.resize(m, false); ri(l, r); dbg(l, r); i64 result = 0; for (auto &[v, w] : vs) { dbg(v, w); for (auto &[x, i] : xs) { if ((i + 1 < l || i + 1 > r) && w <= x && !used[i]) { dbg(i, x); result += v; used[i] = true; break; } } } dbg(used); wl(result); } return 0; }
#include <iostream> #include <string> #include <vector> #include <set> #include <map> #include <queue> #include <stack> #include <algorithm> #include <math.h> #include <cassert> #define rep(i,n) for(int i = 0; i < n; ++i ) using namespace std; using ll = long long; // auto mod int const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) {if ((x += a.x) >= mod) x -= mod; return *this;} mint& operator-=(const mint a) {if ((x += mod-a.x) >= mod) x -= mod; return *this;} mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} const int N=200005,K=17; mint dp[N][K]; bool u[N][K]; int n,k; mint f(int i,int j){ if(i==0) return j==k; if(j>k) return 0; if(u[i][j]) return dp[i][j]; u[i][j] = true; return dp[i][j] = f(i-1,j)*j+f(i-1,j+1)*(16-j); } int main() { string s; cin >> s >> k; n = s.size(); mint ans; set<int> t; rep(i,n){ int a = [&]{ auto c=s[i]; if(isdigit(c)) return c-'0'; return c-'A'+10; }(); rep(ai,a){ int ki = t.size()+1; if(t.count(ai)) --ki; if(ki==1&&ai==0) continue; ans += f(n-1-i,ki); } t.insert(a); } rep(i,n-1) ans += f(i,1)*(16-1); if(t.size()==k) ans += 1; cout << ans << endl; }
#include <algorithm> #include <iostream> using namespace std; typedef long long ll; const ll MOD = 1000000007; ll p[20][200005]; ll dp[200005][20][2][2]; ll com[20][20]; ll sub(int r, int k, bool *b){ int c = 0; for(int i = 0; i < 16; i++) c += b[i]; if(c > k || r + c < k) return 0; ll res = 0; for(int i = 0; i <= k - c; i++){ if(i % 2 == 0) res = (res + com[k - c][i] * p[k - i][r]) % MOD; else res = (res + MOD - com[k - c][i] * p[k - i][r] % MOD) % MOD; } return res * com[16 - c][k - c] % MOD; } int main() { string n; int k; cin >> n >> k; reverse(n.begin(), n.end()); int l = n.size(); int a[200005]; for(int i = 0; i < l; i++){ if(n[i] >= '0' && n[i] <= '9') a[i] = n[i] - '0'; else a[i] = n[i] - 'A' + 10; } a[0]++; for(int i = 1; i < l; i++){ if(a[i - 1] == 16){ a[i - 1] = 0; a[i]++; } } if(a[l - 1] == 16){ a[l - 1] = 0; a[l++] = 1; } reverse(a, a + l); for(int j = 0; j <= k; j++){ p[j][0] = 1; for(int i = 1; i < l; i++) p[j][i] = p[j][i - 1] * j % MOD; } for(int i = 0; i <= 16; i++){ com[i][0] = 1; for(int j = 1; j <= i; j++) com[i][j] = (com[i - 1][j - 1] + com[i - 1][j]) % MOD; } dp[0][0][0][0] = 1; for(int i = 1; i < l; i++){ for(int j = 1; j <= 16; j++){ dp[i][j][0][0] = (dp[i - 1][j][0][0] * j + dp[i - 1][j - 1][0][0] * (16 - j)) % MOD; dp[i][j][1][0] = (dp[i - 1][j][1][0] * (j - 1) + dp[i - 1][j - 1][1][0] * (16 - j + 1) + dp[i - 1][j][1][1] * (j - 1) + dp[i - 1][j - 1][1][1] * (16 - j + 1)) % MOD; dp[i][j][1][1] = (dp[i - 1][j - 1][0][0] + dp[i - 1][j][1][0] + dp[i - 1][j][1][1]) % MOD; } } bool b[20]{0}; ll ans = 0; for(int i = 0; i < l; i++){ for(int j = 0; j < a[i]; j++){ if(i == 0 && j == 0){ for(int o = 0; o < l; o++) ans = (ans + dp[o][k][0][0] + dp[o][k][1][0]) % MOD; continue; } bool t = b[j]; b[j] = true; ans = (ans + sub(l - 1 - i, k, b)) % MOD; b[j] = t; } b[a[i]] = true; } cout << ans << endl; }
#include <bits/stdc++.h> #define ll long long #define F first #define S second #define FF first.first #define FS first.second #define pb push_back using namespace std; const ll N=1000006, INF=1e18, MOD=1e9+7; ll n, m, t, a[N][2], f[N], q=1, e, o, u, x, y, k, ans, Ans0[2], Ans1[2], mx; vector <ll> v; string s[101]; int main(){ios_base::sync_with_stdio(false), cin.tie(0); // cin>>q; while(q--){ cin>>n; for (int i=1; i<=n; i++)a[i][0]=INF,a[i][1]=-INF; for (int i=1; i<=n; i++){ cin>>x>>y; a[y][0]=min(x, a[y][0]); a[y][1]=max(x, a[y][1]); } x=0; for (int i=1; i<=n+1; i++){ if(a[i][0]<INF){ Ans1[0]=min(Ans0[0]+abs(a[x][0]-a[i][1]), Ans0[1]+abs(a[x][1]-a[i][1]))+a[i][1]-a[i][0]; Ans1[1]=min(Ans0[0]+abs(a[x][0]-a[i][0]), Ans0[1]+abs(a[x][1]-a[i][0]))+a[i][1]-a[i][0]; Ans0[0]=Ans1[0]; Ans0[1]=Ans1[1]; x=i; } // cout<<Ans0[0]<<" "<<Ans0[1]<<"____\n"; } cout<<min(Ans0[0], Ans0[1]); } }
// Always remember that you are absolutely unique, just like everyone else! #include <iostream> #include <vector> using namespace std; // DEBUGGING SECTION 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 fastio ios_base::sync_with_stdio(false);cin.tie(NULL) #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #define MOD 1000000007 #define INF (ll)1e18 #define ll long long int #define pll pair<ll, ll> #define vll vector<ll> #define vvll vector<vll> #define vpll vector<pll> #define pb push_back #define endl '\n' // Remove if interactive #define ff first #define ss second #define all(x) x.begin(), x.end() #define FOR(i, a, b) for(int i = a; i < b; i++) 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 <cstring> //#include <map> //#include <set> //#include <queue> //#include <bits/stdc++.h> // comparator(A, B) -> should return true if A needs to come before B! // ALWAYS RETURN FALSE IF A == B vector<pll> vec; vvll arr; vvll dp, dp2; ll helper(ll in, int end); ll helper2(ll in, int end) { if (in == arr.size() - 1) return 0; if (dp2[in][end] != -1) return dp2[in][end]; ll one = helper(in + 1, 0); ll two = helper(in + 1, 1); ll ans = min(abs(arr[in][end] - arr[in + 1][0]) + one, abs(arr[in][end] - arr[in + 1][1]) + two);; dp2[in][end] = ans; return ans; } ll helper(ll in, int end) { if (in == arr.size() - 1) { return 0; } if (dp[in][end] != -1) return dp[in][end]; ll one = helper2(in, 1 - end); ll two = helper2(in, 1 - end); ll dist = abs(arr[in][1] - arr[in][0]); ll ans = dist + min(one, two); dp[in][end] = ans; return ans; } void solve() { int n; cin >> n; vec.resize(n + 10, {INF, -INF}); dp.resize(n + 10, vll(2, -1)); dp2.resize(n + 10, vll(2, -1)); ll x, c; FOR(i, 0, n) { cin >> x >> c; --c; vec[c].ff = min(vec[c].ff, x); vec[c].ss = max(vec[c].ss, x); } FOR(i, 0, n) { if (vec[i].ff != INF) { arr.pb({vec[i].ff, vec[i].ss}); } } arr.pb({0, 0}); //debug(arr); cout << min(abs(arr[0][0]) + helper(0, 0), abs(arr[0][1]) + helper(0, 1)); //debug(dp); } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); // freopen("error.txt", "w", stderr); freopen("output.txt", "w", stdout); #endif fastio; int t = 1; //cin >> t; while (t--) { solve(); } }
#include <string> #include <vector> #include <iostream> #include <algorithm> using namespace std; int score(int N, vector<int> A, string S) { vector<int> st; int ans = 0; for (int i = 0; i < 2 * N; ++i) { if (S[i] == '(') st.push_back(i); else { ans += abs(A[st.back()] - A[i]); st.pop_back(); } } return ans; } string solve(int N, vector<int> A) { vector<int> p(2 * N); for (int i = 0; i < 2 * N; ++i) { p[i] = i; } sort(p.begin(), p.end(), [&](int va, int vb) { return A[va] < A[vb]; }); vector<int> ip(2 * N); for (int i = 0; i < 2 * N; ++i) { ip[p[i]] = i; } vector<int> sa, sb; string str(2 * N, '?'); for (int i = 0; i < 2 * N; ++i) { if (ip[i] < N) { sa.push_back(i); } else { sb.push_back(i); } if (sa.size() >= 1 && sb.size() >= 1) { int ta = sa.back(); sa.pop_back(); int tb = sb.back(); sb.pop_back(); if (ta > tb) swap(ta, tb); str[ta] = '('; str[tb] = ')'; } } return str; } string solve_easy(int N, vector<int> A) { string perm = string(N, '(') + string(N, ')'); int opt = -1; string ans; do { int d = 0; bool ok = true; for (int i = 0; i < 2 * N; ++i) { if (perm[i] == '(') ++d; else --d; if (d < 0) { ok = false; break; } } if (ok) { int res = score(N, A, perm); if (res > opt) { opt = res; ans = perm; } } } while (next_permutation(perm.begin(), perm.end())); return ans; } #include <random> mt19937_64 mt(2105272032); int rand_int(int l, int r) { uniform_int_distribution<int> p(l, r - 1); return p(mt); } string to_string(vector<int> arr) { string res = "["; for (int i = 0; i < arr.size(); ++i) { if (i) res += ", "; res += to_string(arr[i]); } res += "]"; return res; } void random_check() { for (int N = 1; N <= 7; ++N) { for (int testcase = 1; testcase <= 10000; ++testcase) { vector<int> A(2 * N); for (int i = 0; i < 2 * N; ++i) { A[i] = rand_int(1, 10); } string res1 = solve(N, A); string res2 = solve_easy(N, A); if (score(N, A, res1) != score(N, A, res2)) { cout << "N = " << N << " / Case #" << testcase << ":" << endl; cout << "A = " << to_string(A) << endl; cout << "Returns: " << res1 << endl; cout << "Answer: " << res2 << endl; cout << endl; } } cout << "N = " << N << " Completed!" << endl; cout << endl; } } int main() { cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; vector<int> A(2 * N); for (int i = 0; i < 2 * N; ++i) { cin >> A[i]; } cout << solve(N, A) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> a(2 * n); for (auto &x : a) cin >> x; vector<int> ind(2 * n); iota(ind.begin(), ind.end(), 0); sort(ind.begin(), ind.end(), [&](int i, int j) { return a[i] < a[j]; }); vector<bool> big(2 * n); for (int i = n; i < 2 * n; i++) { big[ind[i]] = true; } string ans(2 * n, 0); vector<int> st; for (int i = 0; i < 2 * n; i++) { if (!st.empty() && (big[st.back()] ^ big[i])) { ans[i] = ')'; st.pop_back(); } else { ans[i] = '('; st.push_back(i); } } cout << ans << '\n'; return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int INF = 1001001001; const long long LINF = 1001002003004005006; const double PI = acos(-1); int d4r[] = {1, 0, -1, 0}; int d4c[] = {0, 1, 0, -1}; int d8r[] = {1, 1, 0, -1, -1, -1, 0, 1}; int d8c[] = {0, 1, 1, 1, 0, -1, -1, -1}; long long GCD(long long x, long long y) { if (y == 0) return x; return GCD(y, x % y); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<long long> x(n); for(int i = 0;i < n;++i) { cin >> x[i]; } vector<long long> prime = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47}; long long ans = LINF; for(long long i = 0;i < (1 << prime.size());++i) { long long now = 1; for(int j = 0;j < prime.size();++j) { if(i >> j & 1) { now *= prime[j]; } } bool flag = true; for(int i = 0; i < n;++i) { if(GCD(now,x[i]) == 1) { flag = false; } } if(flag == false) { continue; } ans = min(ans, now); } cout << ans << '\n'; return 0; }
#include<bits/stdc++.h> #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define ll long long int #define fi first #define se second #define pb push_back #define mp make_pair #define vec vector<ll> #define be begin() #define en end() #define MOD 1000000007 #define ull unsigned long long #define pll pair<ll,ll> #define mll map<ll,ll> #define sll set<ll> #define MAXN 500005 #define ll_max 5000000000000000000 using namespace std; int main() {fastio ll t=1;//cin>>t; while(t--) { ll n=0,m=0,k=0,i=0,j=0,p=0,q=0,x=0,y=0,z=0,maxm=INT_MIN,minm=INT_MAX,ans=0,cnt=0,l=0,r=0,mid=0,lo=0,hi=0;string s;bool flag=false; cin>>n;ll a[n]; for(i=0;i<n;i++)cin>>a[i]; sort(a,a+n); for(i=0;i<n;i++){ if(a[i]==i+1){ cnt++; } } if(cnt==n)cout<<"Yes"<<endl; else cout<<"No"<<endl; // } return 0; }
#include <bits/stdc++.h> using namespace std; // [email protected] #define rep(i, a, b) for(lli i = a; i < b; i++) #define lli long long int #define ld long double #define all(v) v.begin(), v.end() #define hell 1000000000000000 #define pb push_back #define pf push_front #define vi vector<lli> #define vip vector<pair<lli, lli>> #define F first #define S second #define pi 2*acos(0.0) #define sz(s) s.size() #define atmod (1000000000+7) #define mod 998244353 // auto _C=clock(); // lli n = 10000000; // int dx[]={1,-1,0,0}; // int dy[]={0,0,1,-1}; /*vector<bool> p(n + 1, true); void sieve() { for (long long int i = 2; i * i <= n; i++) { if (p[i]) { for (long long int j = i + i; j <= n; j = j + i) { p[j] = false; } } } } */ /* WINNERS NEVER QUIT AND QUITTERS NEVER WIN!! Falling down is an accident, Staying down is a choice. */ /*lli binpow(lli a, lli n,lli m) { lli r = 1; a %=m ; while (n > 0) { if (n & 1) { r = (r * a)%m ; } a = (a * a)%m ; n = n / 2; } return r; }*/ /*bool cmp(pair<lli,lli> a,pair<lli,lli> b) { if(a.F<b.F) return 1; else if(a.F==b.F) return(a.S>b.S); else return 0; }*/ /*lli WaysFromOnetoOther(lli start,lli end,lli k) { lli res = 0; if(k==1) { return 1; } rep(i,0,4) { if(i!=end&&i!=start) { res+=WaysFromOnetoOther(i,end,k-1); } } return res; }*/ /*lli npr(lli n,lli r) { lli res = 1; rep(i,1,r+1) { res=res%atmod*(n-r+i)%atmod; res%=atmod; // res/=i; } return res; }*/ void solve() { lli a , b; cin>>a>>b; cout<<(a*b/(double)100); } int main() { // ios_base::sync_with_stdio(false); // cin.tie(0); // cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); // sieve(); lli t = 1; // cin >> t; rep(i,1,t+1) { solve(); } // cerr<<"\n\n\nTime elapsed: "; // cout<<"\n"<<(double)(clock()-_C)*1000.0/CLOCKS_PER_SEC<<"ms\n"; }
#include <iostream> //#include <vector> //#include <string> //#include <algorithm> //#include <math.h> //#include <queue> //#include <stack> //#include <iomanip> // sometimes used //#include <set> //#include <map> //#include <numeric> //#include <list> //#include <deque> //#include <unordered_map> typedef long long LL; //typedef long double LD; //typedef pair<LL, LL> P; using namespace std; //using Graph = vector<vector<LL>>; //const LL MOD=1000000007; //const LL MOD=998244353; //const LL MAX=100100; //const LL NIL=-1; //const LL INF=2000000000000000000; //const LL SEG_VAL = 1 << 19; int main(){ cin.tie(0); ios::sync_with_stdio(0); LL n; LL k; cin >> n >> k; LL ans=0; for(LL i=1; i<=n; i++){ for(LL j=1; j<=k; j++){ ans+=100*i+j; } } cout << ans << endl; return 0; }
/*g++ main.cpp -o main.out*/ /*./main.out*/ #include<bits/stdc++.h> using namespace std; #define LL long long #define LD long double #define Mod 1000000007 #define L_Mod 17100000013 const double eps = 1e-9; const int INF = 0x3f3f3f3f; const double PI = 3.1415926535; #define NO_TLE std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define ree(a,n) memset(a,n,sizeof(a)); #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define red(i,l,u) for(int (i)=(int)(l);(i)>=(int)(u);--(i)) int a[105]; int main() { int n,k,m; cin>>n>>k>>m; int ans=n*m; rep(i,n-1) { cin>>a[i]; ans-=a[i]; } if(ans<=k&&ans>0)cout<<ans<<endl; else if(ans<=0)cout<<0<<endl; else cout<<-1<<endl; return 0; }
#include <iostream> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; if(a + c > b) { cout << "Takahashi" << endl; } else { cout << "Aoki" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int get() { int x = 0; char s = getchar(); while (s < '0' || s > '9') s = getchar(); while (s >= '0' && s <= '9') x = (x << 3) + (x << 1) + (s ^ 48), s = getchar(); return x; } char gc() { char s = getchar(); while (s < 'A' || s > 'B') s = getchar(); return s; } const int mo = 1e9 + 7; ll pow(ll x, int k) { ll ans = 1; while (k) { if(k & 1) ans = ans * x % mo; x = x * x % mo; k >>= 1; } return ans; } int main() { int n = get(); char aa = gc(), ab = gc(), ba = gc(), bb = gc(); if(n < 4) return !printf("1"); if((ab == 'A' && aa == 'A') || (ab == 'B' && bb == 'B')) return !printf("1"); if(ab ^ ba) printf("%lld", pow(2ll, n - 3)); else { if(n == 4) return !printf("2"); if(n == 5) return !printf("3"); ll p = 3, q = 2; for (int i = 3; i <= n - 3; ++i) { p = (p + q) % mo; q = (p - q + mo) % mo; } printf("%lld", p); } return 0; }
#include <bits/stdc++.h> #define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0); #define F first #define S second #define V vector #define PB push_back #define MP make_pair #define EB emplace_back #define ALL(v) (v).begin(), (v).end() #define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << (x) << endl using namespace std; typedef long long ll; typedef pair<int, int> pi; typedef V<int> vi; const int INF = 1e9 + 7, M = 1e9 + 7; signed main() { IO_OP; int n; cin >> n; if(n == 2) return cout << 1 << endl, 0; n -= 2; V<vi> a(2, vi(2)); for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) { char c; cin >> c; a[i][j] = c - 'A'; } if(a[0][1]) { auto b = a; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) b[i][j] = a[j][i]; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) a[i][j] = b[i ^ 1][j ^ 1] ^ 1; } if(a[0][0] == 0) return cout << 1 << endl, 0; if(a[1][0] == 0) { int a = 1, b = 1; for(int i = 0; i < n - 1; i++) { int aa = (a + b) % M; int bb = a; a = aa, b = bb; } cout << a << endl; } else { int ans = 1; for(int i = 0; i < n - 1; i++) ans = ans * 2 % M; cout << ans << endl; } }
#include <vector> #include <functional> using namespace std; template<typename T> class SegmentTree { int n; function<T(T,T)> op; vector<T> V; T q(int l, int r) { if (l >= r) return V[0]; T a = q((l+1)/2, r/2); if (l%2 != 0) a = op(a, V[l]); if (r%2 != 0) a = op(a, V[r-1]); return a; } public: SegmentTree(int n_, function<T(T,T)> op, T init): op(op) { n = 1; while (n < n_) n *= 2; V = vector<T>(2*n, init); for (int i=n-1; i>0; i--) V[i] = op(V[i*2], V[i*2+1]); } T get(int p) const { return V[p+n]; } void set(int p, T v) { p += n; V[p] = v; for (p/=2; p>0; p/=2) V[p] = op(V[p*2], V[p*2+1]); } // init OP v[l] OP v[l+1] OP ... OP v[r-1] T query(int l, int r) { return q(l+n, r+n); } }; #include <iostream> #include <algorithm> using namespace std; int main() { int N; cin>>N; vector<int> A(N); for (int &a: A) cin>>a; int MA = 0; for (int a: A) MA = max(MA, a+1); vector<int> L(N); SegmentTree<int> STL(MA, [&](int x, int y){return max(x, y);}, -1); for (int i=0; i<N; i++) { L[i] = STL.query(0, A[i]); STL.set(A[i], i); } vector<int> R(N); SegmentTree<int> STR(MA, [&](int x, int y){return min(x, y);}, N); for (int i=N-1; i>=0; i--) { R[i] = STR.query(0, A[i]); STR.set(A[i], i); } int ans = 0; for (int i=0; i<N; i++) ans = max(ans, A[i]*(R[i]-L[i]-1)); cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+105; const ll mod=1e9+7; int a[maxn]; int main(){ int n,ans=0; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++){ int tp=a[i]; for(int j=i+1;j<=n;j++) { if(a[j]<a[i]){ break; } tp=tp+a[i]; } for(int j=i-1;j>=1;j--){ if(a[j]<a[i]){ break; } tp=tp+a[i]; } ans=max(ans,tp); } printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> #define rep(begin, end) for (i=begin;i<end;i++) #define repj(begin, end) for (j=begin;j<end;j++) #define init(arr, end, val) for (i=0;i<end;i++) arr[i] = val; #define printint(i0, i1) printf("%d %d\n", i0, i1) using namespace std; typedef long long int ll; const ll inf = 1000000000; const ll mod = 1000000007; const ll nil = -1; ll i, m, k, t, ans; ll n[100000], a[100000], b[100000]; int main() { scanf(" %lld", &t); rep(0,t) { scanf(" %lld %lld %lld", &n[i], &a[i], &b[i]); } rep(0,t) { if (n[i] < a[i] + b[i]) { printf("0\n"); continue; } ll anstmp = 0; ll X = n[i] - a[i] - b[i] + 1; ll Y = n[i] - a[i] + 1; ll Z = n[i] - b[i] + 1; anstmp = (X * (X+1)) % mod; ll YY = (((2 * Y) % mod) * Z) % mod - (X * (X+1)) % mod; YY = (YY + mod) % mod; anstmp = (anstmp * YY) % mod; printf("%lld\n", anstmp); } }
/* @Author Sujeet Kushwaha */ #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 int #define fast ios_base::sync_with_stdio(false) #define fast_input cin.tie(NULL) #define fast_output cout.tie(NULL) #define vi vector<long long int> #define vp vector<pair<long long int ,long long int>> #define pb push_back #define mp make_pair #define pp pop_back #define iter vector<int>::iterator #define pa pair<long long int ,long long int> #define f(a,b) for(int a=0;a<b;a++) #define mod 1000000007 #define F first #define S second #define sett set<long long int> #define um unordered_map<ll,ll> #define ordered_set tree<pa, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> // ll mul(ll a,ll b) // { // return (a%mod*b%mod)%mod; // } // ll add(ll a,ll b) // { // ll res=a+b; // if(res>=mod) // { // res-=mod; // } // return res; // } // ll sub(ll a,ll b) // { // ll res=a-b; // if(res<0) // res+=mod; // return res; // } // ll powmod(ll a, ll b) // { // ll res = 1; // while (b) { // if (b&1) { // res =mul(res,a); // } // b >>= 1; // a=mul(a,a); // } // return res; // } // ll modinv(ll a) // { // return powmod(a, mod - 2); // } // unordered_map<ll,ll>facti; // ll fact(ll n) // { // if(n==0)return 1; // if(n<=1)return 1; // if(facti[n]!=0)return facti[n]; // return facti[n]=mul(n,fact(n-1)); // } // ll ncr(ll n,ll r) // { // ll h=fact(n); // ll v=fact(r); // ll j=fact(n-r); // v=mul(v,j); // v=powmod(v,mod-2); // return mul(h,v); // } // int fastread(){ // int x=0; // int c = getchar_unlocked(); // while(c<48) // c = getchar_unlocked(); // while(c>=48){ // x = x*10 + (c - 48); // c = getchar_unlocked(); // } // return x; // } // bool comp(pa a,pa b) // { // if(a.F==b.F) // { // return v[a.S]<v[b.S]; // } // returna.S<b.S; // } vector<pa>g[1000001]; ll dp[1000001]; void dfs(ll v,ll par=-1) { for(pa d:g[v]) { if(d.first-par) { dp[d.first]=dp[v]^d.second; dfs(d.first,v); } } } void solve() { ll n; cin>>n; f(i,n-1) { ll x,y,w; cin>>x>>y>>w; x--,y--; g[x].pb({y,w}); g[y].pb({x,w}); } dfs(0); ll ans=0; f(j,61) { ll ones,zeros; ones=zeros=0; f(i,n) { if(dp[i]&(1ll<<j)) { ones++; } else { zeros++; } } zeros*=ones; zeros%=mod; ans+=((1ll<<j)%mod*zeros%mod); ans%=mod; } cout<<ans<<endl; } int main() { fast; fast_input; fast_output; // ll t; // cin>>t; // while(t--) // { solve(); // } return 0; }
#include "bits/stdc++.h" #define int long long using namespace std; using ll = long long; using P = pair<ll, ll>; const ll INF = (1LL << 61); ll mod = 998244353; template <typename T> struct BIT { int n; // 配列の要素数(数列の要素数+1) vector<T> bit; // データの格納先 BIT(int n_) : n(n_ + 1), bit(n, 0) {} void add(int i, T x) { for (int idx = i; idx < n; idx += (idx & -idx)) { bit[idx] += x; } } T sum(int i) { T s(0); for (int idx = i; idx > 0; idx -= (idx & -idx)) { s += bit[idx]; } return s; } }; signed main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int>A(N), B(N); for (int i = 0; i < N; i++)cin >> A[i]; for (int i = 0; i < N; i++)cin >> B[i]; map<int, vector<int>>mp; for (int i = N - 1; i >= 0; i--)mp[i + A[i]].push_back(i); vector<int>c(N); BIT<int>bit(N + 100); for (int i = 0; i < N; i++) { if (mp[i + B[i]].empty()) { cout << -1 << endl; return 0; } int idx = mp[i + B[i]].back(); mp[i + B[i]].pop_back(); c[i] = idx + 1; } int ans = 0; for (int i = 0; i < N; i++) { ans += i - bit.sum(c[i]); bit.add(c[i], 1); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() int main(){ ll n;cin >>n; vector<vector<ll>> c(n,vector<ll>(n)); rep(i,n)rep(j,n)cin >>c[i][j]; bool y_n=true; vector<ll> dc(n-1); rep(i,n-1)dc[i]=c[0][i+1]-c[0][i]; rep(i,n-1)rep(j,n-1)if(dc[j]!=c[i+1][j+1]-c[i+1][j])y_n=false; if(!y_n){ cout <<"No"; return 0; } vector<ll> cn0(n); rep(i,n) cn0[i]=c[i][0]; vector<ll> a=cn0; sort(all(a)); rep(i,n){ cn0[i]-=a[0]; if(cn0[i]<0)y_n=false; } if(!y_n){ cout <<"No"; return 0; } cout <<"Yes" <<endl; ll num; rep(i,n)if(c[i][0]==a[0])num=i; rep(i,n){ cout <<cn0[i]; if(i!=n-1)cout <<" "; } cout <<endl; rep(i,n){ cout <<c[num][i]; if(i!=n-1)cout <<" "; } }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, a[N], b[N], p[N], rp[N], id[N]; void gao(int x, int y) { if(a[x]<=b[p[x]] || a[y]<=b[p[y]]) { puts("-1"); exit(0); } rp[p[y]] = x, rp[p[x]] = y; swap(p[x], p[y]); } int main() { scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", a+i); for(int i=1; i<=n; i++) scanf("%d", b+i); for(int i=1; i<=n; i++) scanf("%d", p+i), rp[p[i]] = i; iota(id+1, id+n+1, 1); sort(id+1, id+n+1, [&](int x, int y) { return a[x] < a[y]; }); vector<pair<int, int>> seq; for(int i=1; i<=n; i++) { int j = id[i], k = rp[j]; if(j==k) continue; if(a[k]>b[p[j]]) { gao(j, k); seq.emplace_back(j, k); } else { gao(k, n); seq.emplace_back(k, n); gao(j, n); seq.emplace_back(j, n); } } for(int i=1; i<=n; i++) assert(i==p[i]); printf("%d\n", (int)seq.size()); for(auto &it : seq) printf("%d %d\n", it.first, it.second); return 0; }
#include<bits/stdc++.h> #define int long long #define rint regester int const int maxn = 1e6 + 10; const int INF = 1e9; using std::ios; using std::cin; using std::cout; using std::max; using std::min; using std::sort; using std::unique; using std::lower_bound; using std::swap; using std::abs; using std::acos; using std::queue; using std::map; using std::string; int read(){int x = 0,f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-')f = -1; ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} return x * f;} int n, ispri[maxn], m, pri[maxn], cnt, a[maxn]; void work(int nm){ for(int i = 2; i <= nm; i++){ if(!ispri[i])pri[++cnt] = i; for(int j = 1; j <= cnt && i * pri[j] <= nm; j++)ispri[i * pri[j]] = 1; } } signed main(){ n = read(); work(n); for(int i = 1; i <= n; i++){ int ii = i; for(int j = 1; j <= cnt && pri[j] <= ii; j++){ int cc = 0; while(ii % pri[j] == 0 && ii){ ++cc, ii /= pri[j]; } a[i] += cc; } ++a[i]; } for(int i = 1; i <= n; i++)cout << a[i] << " "; }
#include<bits/stdc++.h> using namespace std; int main(){ long long a,b; cin>>a>>b; float c = (float)((b/100.0)*a); int d = c; if(c == d){ cout<<d; }else{ cout<<c; } //cout<<(b/100)*a; }
#include <bits/stdc++.h> using namespace std; int main(){ double a,b; double x; cin >> a >> b; x = a*b/100; cout << x << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for (int i = (a); i < (b); i++) #define REP(i,n) rep(i,0,n) void solve() { int n,m;cin>>n>>m; int a[n],b[m],dp[n+1][m+1]; for(auto &i:a)cin>>i; for(auto &i:b)cin>>i; REP(i,n+1)dp[i][0]=i; REP(i,m+1)dp[0][i]=i; rep(i,1,n+1)rep(j,1,m+1)dp[i][j]=min({dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+(a[i-1]==b[j-1]?0:1)}); cout<<dp[n][m]<<endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; int main() { int N, M; cin >> N >> M; vector<int> A(N), B(M); rep(i, N) cin >> A[i]; rep(i, M) cin >> B[i]; vector<vector<int>> dp(N, vector<int>(M, 0)); rep(i, N) { rep(j, M) { if (i == 0 && j == 0) { if (A[i] == B[j]) dp[i][j] = 0; else dp[i][j] = 1; continue; } int x = 1e9, y = 1e9, z = 1e9; if (j > 0) { x = dp[i][j - 1] + 1; if (A[i] != B[j - 1]) x--; if (A[i] != B[j]) x++; } if (i > 0) { y = dp[i - 1][j] + 1; if (A[i - 1] != B[j]) y--; if (A[i] != B[j]) y++; } if (i > 0 && j > 0) { z = dp[i - 1][j - 1]; if (A[i] != B[j]) z++; } dp[i][j] = min({x, y, z}); } } /* rep(i, N) { rep(j, M) { cout << dp[i][j] << " "; } cout << "\n"; } */ int ans = 1e9; rep(i, N) { rep(j, M) { ans = min(ans, dp[i][j] + N - 1 - i + M - 1 - j); } } cout << ans << "\n"; }
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define fz(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) #define ffe(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++) #define fill0(a) memset(a,0,sizeof(a)) #define fill1(a) memset(a,-1,sizeof(a)) #define fillbig(a) memset(a,63,sizeof(a)) #define pb push_back #define ppb pop_back #define mp make_pair typedef pair<int,int> pii; typedef long long ll; ll n; int main(){ scanf("%lld",&n); ll l=1,r=2e9,ans=0; while(l<=r){ int mid=(l+r)>>1; if(1ll*mid*(mid+1)/2ll<=n+1) ans=mid,l=mid+1; else r=mid-1; } printf("%lld\n",n+1-ans); return 0; }
#include <bits/stdc++.h> using namespace std; typedef int64_t lint; #define rep(i, n) for(lint i=0; i<n; i++) int main() { lint N; cin >> N; lint a=0, b=0, c=N+1, x, y, z; rep(i, N) { c -= i+1; if (c >= 0) a++; else break; } std::cout << N+1-a << '\n'; }
#include <bits/stdc++.h> // clang-format off using namespace std; using i64 = int64_t; using pii = pair<i64, i64>; #define rep(i, n) for (i64 i = 0; i < i64(n); i++) #define per(i, n) for (i64 i = n - 1; i >= 0; i--) #define REP(i, a, b) for (i64 i = a; i < i64(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define fi first #define se second #ifdef LOCAL #define dbg(...) cerr<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debug(__VA_ARGS__) #else #define dbg(...) #endif template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (auto& x : vec) { os << x << ' '; } return os; } template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &mat) { os << endl; for (auto& vec : mat) { os << vec << endl; } return os; } void debug() { cerr << endl; } template <typename Head, typename... Tail> void debug(Head H, Tail... T) { cerr << " " << H; debug(T...); } template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const int INF = (1 << 30) - 1; const i64 LINF = (1LL << 60) - 1; // clang-format on template <typename T> vector<T> compress(vector<T>& vec) { auto ret = vec; sort(all(vec)); vec.erase(unique(all(vec)), vec.end()); for (T& a : ret) a = lower_bound(all(vec), a) - vec.begin(); return ret; } void solve() { int n; cin >> n; vector<i64> A(n); rep(i, n) cin >> A[i]; auto X = compress(A); vector<int> cnt(X.size()); rep(i, n) { cnt[X[i]]++; } if (n % 2 == 0) { bool latter = 1; for (auto x : cnt) { latter &= x % 2 == 0; } cout << (latter ? "Second" : "First") << endl; } else { cout << "Second" << endl; } } int main() { cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; while (t--) { solve(); } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <iomanip> #include <cmath> #include <set> #include <map> #include <stack> #include <queue> #define ll long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define P pair<ll, ll> using namespace std; int main(){ int n; cin >> n; vector<ll> a(n); rep(i, n) cin >> a[i]; ll ans = 1e18; for(ll i = 0; i <= 1 << (n - 1); i++){ ll x = 0, y = 0; vector<ll> v; rep(j, n){ x |= a[j]; if(j == n - 1) break; if(i >> j & 1){ v.push_back(x); x = 0; } } v.push_back(x); for(auto& e: v){ y ^= e; } ans = min(ans, y); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; // DEBUG #ifdef _DEBUG #define debug(x) cout << #x << ": " << x << endl #else #define debug(x) #endif // iter #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define REPD(i, n) for (int i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (int i = a; i <= (int)(b); i++) #define FORD(i, a, b) for (int i = a; i >= (int)(b); i--) #define FORA(i, I) for (const auto& i : I) // vec #define ALL(x) x.begin(), x.end() #define SIZE(x) ((int)(x.size())) // 定数 // 2.147483647×10^{9}:32bit整数のinf #define INF32 2147483647 // 9.223372036854775807×10^{18}:64bit整数のinf #define INF64 9223372036854775807 // 問題による #define MOD 1000000007 int main() { // 小数の桁数の出力指定 // cout << fixed << setprecision(10); // 入力の高速化用のコード ios::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector<vector<int>> C(N, vector<int>(N)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> C[i][j]; } } bool exists = false; vector<int> A(N), B(N); REP(i, N) { bool can = true; REP(j, N) { if (i == j) { continue; } debug(i); debug(j); ll num = C[i][0] - C[j][0]; debug(num); REP(k, N) { ll tmp = C[i][k] - C[j][k]; debug(tmp); if (tmp != num) { can = false; break; } } if (!can) { break; } } debug(can); if (can) { B = C[i]; bool allPlus = true; REP(a, N) { int tmp = C[a][0] - B[0]; debug(C[a][0]); debug(tmp); if (tmp < 0) { allPlus = false; break; } A[a] = tmp; } if (allPlus) { exists = true; break; } } } if (exists) { cout << "Yes" << endl; REP(i, N) { cout << A[i]; if (i < N - 1) { cout << " "; } } cout << endl; REP(i, N) { cout << B[i]; if (i < N - 1) { cout << " "; } } cout << endl; } else { cout << "No" << endl; } return 0; }
#include <bits/stdc++.h> //#include <atcoder/all> using namespace std; //using namespace atcoder; using ll=long long; using ld=long double; //using mint = modint1000000007; #define rep(i,n) for (ll i=0; i<n; ++i) #define all(c) begin(c),end(c) #define PI acos(-1) #define oo 2e18 template<typename T1, typename T2> bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;} template<typename T1, typename T2> bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;} //priority_queue<ll, vector<ll>, greater<ll>> Q; /* 250000 2ぶたん? 最初のAさえ決まればあとは全部? */ ll C[550][550]; int main(){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); ll N; cin >> N; ll low = 1e9; ll high = 0; rep(i, N) rep(j, N)cin >> C[i][j]; rep(i, N) rep(j, N){ chmin(low, C[i][j]); chmax(high, C[i][j]); } // lowより小さい数だけが候補 // さが同じかどうか ll dif[550] = {}; rep(i, N-1){ dif[i] = C[i+1][0] - C[i][0]; rep(j, N){ if (C[i+1][j] - C[i][j] != dif[i]){ cout << "No" << endl; return 0; } } } ll bmin=1e9; // Bは一番小さい数がある行 ll B[550]={}; rep(i, N) chmin(bmin, C[i][0]); rep(i, N){ if (C[i][0] == bmin) { rep(j, N) B[j] = C[i][j]; break; } } ll A[550] = {}; rep(i, N){ A[i] = C[i][0] - bmin; } cout << "Yes" << endl; rep(i, N){ cout << A[i]; if (i==N-1) cout << endl; else cout << " "; } rep(i, N){ cout << B[i]; if (i==N-1) cout << endl; else cout << " "; } }
#include<bits/stdc++.h> using namespace std; #define ll long long #define ull long long #define pll pair<ll,ll> #define ff first #define ss second #define pb push_back #define endl "\n" const ll maxn =5e5+5; const ll mod=998242361 ; const ll base=1e18; map<ll,vector<ll>> mp; ll a[maxn]; ll b[maxn]; ll fwt[2*maxn]; ll n; ll cnt; void update(ll l, ll r, ll d) { for (int p = l; p <= cnt; p += p & -p) fwt[p] = (fwt[p] + d) ; ++r; for (int p = r; p <= cnt; p += p & -p) fwt[p] = (fwt[p] - d) ; } ll get(ll p) { ll ret = 0; for (; p; p -= p & -p) ret = (ret + fwt[p]) ; return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen("t.inp","r")) { freopen("test.inp","r",stdin); freopen("test.out","w",stdout); } cin>>n ; cnt=n; for (int i=1;i<=n;i++) { cin>>a[i]; update(i,i,i); } for (int i=n;i>=1;i--) mp[a[i]+i-1].pb(i); for (int i=1;i<=n;i++) { cin>>b[i]; } ll ans=0; for (int i=1;i<=n;i++) { if (mp[b[i]+i-1].size()) { ans=(ans+abs(i-get(mp[b[i]+i-1].back()))); update(1,mp[b[i]+i-1].back(),1); mp[b[i]+i-1].pop_back(); } else { //cout <<i<<endl; cout <<-1; return 0; } } cout <<ans; }
#include "bits/stdc++.h" #include <chrono> #include <random> #include <ext/pb_ds/assoc_container.hpp> #define INF 1000000007 #define F first #define S second #define PB push_back #define MP make_pair #define REP(i,a,b) for (int i = a; i < b; i++) #pragma GCC optimize("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,fma,mmx,avx,tune=native") using namespace __gnu_pbds; using namespace std; typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef unordered_map<int,int> umi ; typedef unordered_set<int> usi ; typedef pair<int,int> pi; typedef tree< pair<ll,ll>,null_type, less<pair<ll,ll>> ,rb_tree_tag,tree_order_statistics_node_update > indexed_set; struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; bool ldequal(ld a ,ld b){ return abs(a-b) < 1e-9 ; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // shuffle(permutation.begin(), permutation.end(), rng); for permute // uniform_int_distribution<int>(0, i)(rng) for rand 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 int N =2e5+1 ; ll a[N] ,b[N] ; indexed_set s1 ; set<pair<ll,ll>> s2 ; bool tests=false ; void gen(){} ; void solve(){ int n ; cin>>n ; REP(i,0,n){ cin>>a[i] ; s1.insert({i,a[i]+i}) ; s2.insert({a[i]+i,i}) ; } REP(i,0,n) cin>>b[i] ; ll res=0 ; REP(i,0,n){ ll val =b[i]+i ; auto l = s2.lower_bound({val,0}) ; if(l==s2.end()){ cout<<-1 ; return ; } pair<ll,ll> temp = MP((*l).S ,(*l).F) ; auto r =s1.lower_bound(temp) ; debug(val,*l ,*r , s1.order_of_key(*r) ) ; res+=s1.order_of_key(*r) ; s2.erase(l) ; s1.erase(r) ; } cout<<res ; } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios::sync_with_stdio(0); cin.tie(0); gen() ; long long test =1 ; if(tests) cin>>test ; REP(i,1,test+1){ debug(i) ; //cout<<"Case #"<<i<<": " ; solve() ; cout<<"\n" ; } return 0 ; }
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} void solve(){ ll N; cin>>N; int num=0; while(N%2==0){ N/=2; num++; } if(num==0) cout<<"Odd"<<"\n"; else if(num==1) cout<<"Same"<<"\n"; else cout<<"Even"<<"\n"; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int T; cin>>T; rep(i,T) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; //↓AC-library使うなら //型関係 using Graph = vector<vector<int>>; typedef long long ll; typedef pair<int, int> P; typedef pair<ll, ll> pll; typedef priority_queue<int, vector<int>, greater<int>> PQ;//昇順priority_queue typedef unsigned long long int ull; typedef vector<int> vi; typedef vector<char> vc; typedef vector<bool> vb; typedef vector<double> vd; typedef vector<string> vs; typedef vector<ll> vll; typedef vector<P > vpii; typedef vector<pll > vpll; typedef vector<vector<int> > vvi; typedef vector<vector<char> > vvc; typedef vector<vector<string> > vvs; typedef vector<vector<ll> > vvll; //repマクロ #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define rrep(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 SIZE(x) ((ll)(x).size()) #define MAX(x) *max_element(ALL(x)) #define MIN(x) *min_element(ALL(x)) #define ALL(x) (x).begin(), (x).end() //定数 #define inf 1000000000000 #define mod 1000000007 #define MAXR 100000 #define MATHPI acos(-1) const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int INF = INT_MAX; const ll LLINF = 1LL<<60; //操作 #define PB push_back #define PF push_front #define MP make_pair #define F first #define S second //数学関係, a<=x<=b bool updown(ll x, ll a, ll b) { return x >= a && x <= b; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } #define SUM(x) accumulate(ALL(x), 0) //chmin, chmax 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;} //小数出力(f...出力したい小数, n...小数点以下の桁数) void printd(double f, int n) { cout << fixed << setprecision(n) << f << endl; } //指定した桁で小数を出力 //YES/NO, Yes/No, yes/no出力 void YES(bool c = true) { cout << (c ? "YES" : "NO") << endl; } //YES(条件式)で記述 void Yes(bool c = true) { cout << (c ? "Yes" : "No") << endl; } //Yes(条件式)で記述 void yes(bool c = true) { cout << (c ? "yes" : "no") << endl; } //yes(条件式)で記述 //if use atcoder library:g++ main.cpp -std=c++17 -I /home/k0gane/ac-library //累積和 //https://qiita.com/drken/items/56a6b68edef8fc605821 //point...n+1の容量のリストを作る // //深さ優先...push_front //幅優先...push_backP int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; rep(i, t){ ll n; cin >> n; int cnt = 0; while(n % 2 == 0){ cnt++; n = n / 2; } if(cnt == 1) cout << "Same" << endl; else if(cnt >= 2) cout << "Even" << endl; else cout << "Odd" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; /* short */ #define pb push_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) rbegin(v), rend(v) /* REPmacro */ #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define FORR(i, a, b) for(int i = (a); i >= (b); i--) #define REP(i, n) for(int i = 0; i < (n); i++) #define FOREACH(x, a) for(auto x : a) /* exchange */ #define CHMIN(a, b) (a) = min((ll)(a), (ll)(b)) #define CHMAX(a, b) (a) = max((ll)(a), (ll)(b)) /* function */ #define IN(x) cin >> x #define DEBUG(x) cerr << (x) << " " #define LN() cerr << "\n" #define PRINT(x) cout << (x) << endl #define BR cout << endl /* const */ const int ARRAY = 100005; const int INF = 1001001001; // 10^9 const ll LINF = 1001001001001001001; // 10^18 const int MOD = 1e9 + 7; ll N = 0; ll K = 0; ll M; ll t; ll ret = 0; string s; ll total = 0; void input() { IN(N); IN(K); IN(M); REP(i, N-1) { IN(t); total += t; } } void solve() { ll goal = N * M; if (goal - total < 0) { PRINT(0); } else if (goal - total > K) { PRINT(-1); } else { PRINT(goal - total); } } int main(void){ input(); solve(); }
#include<bits/stdc++.h> using namespace std; #define lli long long int #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define FORD(i,a,b) for(int i=(a);i>=(b);i--) inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; } const int INF = 1<<29; typedef long long ll; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n>>b)&1; } inline void set_bit(int & n, int b) { n |= two(b); } inline void unset_bit(int & n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res;} int main() { lli n,k,m; cin>>n>>k>>m; lli avg=0; for(int i=1;i<n;i++) {lli x; cin>>x; avg+=x;} lli ans=n*m-avg; if(ans<=0) cout<<0; else if(ans<=k) cout<<ans; else cout<<-1; }
#include <bits/stdc++.h> using namespace std; #define set_precision(ans,l) cout << fixed << setprecision(l)<<ans; #define rep(i, a, b) for (int i = a; i < b; i++) #define repb(i, a, b) for (int i = a; i >= b; i--) #define vi vector<int> #define vl vector<long long int> #define Vi vector<vector<int>> #define vpi vector<pair<int,int>> #define seti set<int> #define setl set<ll> #define dseti set<int, greater<int>> #define dsetl set<ll, greater<ll>> #define mseti multiset<int> #define msetl multiset<ll> #define dmseti multiset<int, greater<int>> #define dmsetl multiset<ll, greater<ll>> #define sortA(arr) sort(arr.begin(), arr.end()) #define dsortA(arr) sort(arr.begin(), arr.end(), greater<ll>()) #define ssort(arr) stable_sort(arr.begin(), arr.end()) #define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end()) #define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<ll>()) #define init(a) memset((a),0,sizeof(a)) #define pi pair<int,int> #define pb push_back #define pl pair<ll,ll> #define ll long long #define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define mod 998244353 long double EPS = 1e-9; /*struct comp { bool operator() (const int& lhs, const int& rhs) const {return lhs<rhs;} };*/ ll cel(ll a,ll b){return((a-1)/b+1);} ll gcd(ll a, ll b){ if (a < b)swap(a, b); return (b == 0)? a: gcd(b, a % b); } ll MIN(ll a,int b){ll ans;(a>=b)?ans=b:ans=a;return ans;} ll MAX(ll a,int b){ll ans;(a>=b)?ans=a:ans=b;return ans;} ll lcm(ll a,ll b){return (a*b)/gcd(a,b);} ll po(ll x,ll y){ ll ans=1; while(y){ if(y&1){ans=(ans*x)%mod;} y>>=1;x=(x*x)%mod; } return ans; } ll fac(ll x){ if(x==0)return 1; ll ans=1; rep(i,1,x+1){ ans=(ans*i)%mod; } return ans%mod; } vi adj[51],adj1[51],vis(51,0),vis1(51,0); void dfs(int u,ll& l){ vis[u]=1;l++; for(int t:adj[u]){ if(vis[t]){continue;} dfs(t,l); } } void dfs1(int u,ll& l){ vis1[u]=1;l++; for(int t:adj1[u]){ if(vis1[t]){continue;} dfs1(t,l); } } int main() { FIO; ll n,t,x,y,m,s; cin>>n>>s; int A[n][n];x=0;y=0; rep(i,0,n){ rep(j,0,n){ cin>>A[i][j]; } } rep(i,0,n){ rep(j,0,n){ if(j==i)continue; bool ok=true; rep(k,0,n){ if(A[i][k]+A[j][k]>s){ok=false;break;} } if(ok){adj[i].pb(j);} } } rep(i,0,n){ rep(j,0,n){ if(j==i)continue; bool ok=true; rep(k,0,n){ if(A[k][i]+A[k][j]>s){ok=false;break;} } if(ok){adj1[i].pb(j);} } } ll ans=1; rep(i,0,n){ if(vis[i])continue; x=0; dfs(i,x); ans=(ans*fac(x))%mod; //cout<<i<<" "<<x<<" "<<ans<<"\n"; } rep(i,0,n){ if(vis1[i])continue; x=0; dfs1(i,x); ans=(ans*fac(x))%mod; //cout<<x<<" "<<ans<<"\n"; } cout<<ans; return 0; }
/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author tatsumack */ #include <iostream> #include <fstream> #include <bits/stdc++.h> #define int long long #define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i) #define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i) #define REV(i, a, b) for (int i = (a); i >= (b); --i) #define CLR(a, b) memset((a), (b), sizeof(a)) #define DUMP(x) cout << #x << " = " << (x) << endl; #define INF 1001001001001001001ll #define fcout cout << fixed << setprecision(12) using namespace std; class CARCWrecker2 { public: void solve(std::istream& cin, std::ostream& cout) { int N; cin >> N; vector<int> A(N); REP(i, N) cin >> A[i]; REP(i, N) { if (i % 2 == 1) A[i] *= -1; } int cur = 0; map<int, int> m; int res = 0; m[0]++; REP(i, N) { cur += A[i]; res += m[cur]; m[cur]++; } cout << res << endl; } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); std::istream& in(std::cin); std::ostream& out(std::cout); CARCWrecker2 solver; solver.solve(in, out); return 0; }
#include <bits/stdc++.h> using namespace std; int main(void){ int n; cin>>n; vector<long long> data(n); for(int i=0;i<n;i++){ if(i==0){ cin>>data.at(i); continue; } if(i%2==1){ int a; cin>>a; data.at(i)=data.at(i-1)-a; continue; } if(i%2==0){ int a; cin>>a; data.at(i)=data.at(i-1)+a; continue; } } map<long long,long long> ma; ma[0]=1; long long count=0; for(int i=0;i<n;i++){ if(ma.count(data.at(i))){ count+=ma.at(data.at(i)); ma.at(data.at(i))++; }else{ ma[data.at(i)]=1; } } cout<<count<<endl; }
#include <iostream> #include <string> #include <vector> #include <deque> #include <algorithm> #include <math.h> #include <iomanip> #include <map> #include <sstream> #define INF 1000000009 #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using p = pair<int, int>; int main() { ll n; cin >> n; ll ans = 0; vector<ll> a(n); map<ll,ll> x; rep(i,n) { ll an; cin >> an; if(i==0) a[i] = an; else a[i] = an - a[i-1]; if(a[i]==0) ans++; if(i%2==0) x[a[i]]++; else x[-1*a[i]]++; } for (const auto &i : x){ ans += (i.second*(i.second-1)) / 2; } cout << ans << endl; return 0; }
//int a = s - '0'; 文字から数字 //小文字から大文字 //transform(a.begin(), a.end(), a.begin(), ::toupper); //map 全探索 //auto begin = p.begin(), end = p.end(); //for (auto it = begin; it != end; it++) {} //mapのキー:it->first mapのバリュー:it->second //大文字判定 isupper(文字) 小文字判定 islower(文字) //do{}while(next_permutation(ALL(配列))) //小文字に対応する文字コード:S[i] - 'a' //文字コード→小文字:(char)(数字+'a') //グラフの距離:隣接行列で扱う //bool型 初期値はTrue //島渡りの問題:中間ノードに着目 //数が大きい時の比較はstring型で行う //全て0になったか調べたい->0になるたびにcntする //例外処理は最初にする //x = p^m + q^n...の約数の個数:(n+1)*(m+1).... //N!のどの素因数で何回割れるか //⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和 //パズルの問題->一般化して全探索 //stack<ll> s; //s.push(要素);s.top();s.pop(); //queue<ll> q; //q.push(要素);q.front();q.pop(); //同じ作業繰り返す系の問題:収束先を見つける //過半数:N/2.0で判定 //優先度付きキュー //priority_queue< //ll, //vector<ll> //> q; #include <bits/stdc++.h> #define rep(i,N) for(int i = 0; i < N;i++) #define ALL(a) (a).begin(),(a).end() #define ll long long int #define PI 3.14159265358979323846264338327950L using namespace std; //割るやつ const ll MOD = (pow(10, 9) + 7); // K進数でのNの桁数 ll dig(ll N, ll K) { ll dig = 0; while (N) { dig++; N /= K; } return dig; } // a,bの最大公約数 ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a%b); } //a,bの最小公倍数 ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } //階乗計算 ll f(ll n) { if (n == 0 || n == 1) return 1; else return (n * f(n - 1)); } //Nのd桁目の数 ll dignum(ll N, ll d) { ll x = pow(10, d); N %= x; ll y = pow(10, d - 1); N /= y; return N; } //Nをdで何回割れるか ll divcnt(ll N, ll d) { ll ans = 0; while (1) { if (N%d == 0) { ans++; N /= d; } else break; } return ans; } //素数判定 bool prime(ll num) { if (num < 2) return false; else if (num == 2) return true; else if (num % 2 == 0) return false; double sqrtNum = sqrt(num); for (int i = 3; i <= sqrtNum; i += 2) { if (num % i == 0) return false; } return true; } //フィボナッチ数列 vector<ll> memo(pow(10, 6) + 1); ll fibo(ll n) { if (n == 1) return 1; else if (n == 2) return 1; else if (memo[n] != 0) return memo[n]; else return memo[n] = fibo(n - 1) + f(n - 2); } ll RS(ll N, ll P, ll M) { if (P == 0) return 1; if (P % 2 == 0) { ll t = RS(N, P / 2, M); return t * t % M; } return N * RS(N, P - 1, M); } vector<int> IntegerToVector(int bit, int N) { vector<int> S; for (int i = 0; i < N; ++i) { if (bit & (1 << i)) { S.push_back(i); } } return S; } int main() { ll N; cin >> N; unordered_set<ll> s; for(ll a = 2; a*a<=N; a++){ ll x = a*a; while(x<=N){ s.insert(x); x *= a; } } cout << N - s.size() << endl; }
#pragma GCC target ("avx2") #pragma GCC optimize ("unroll-loops") #pragma GCC optimize ("O3") #include "bits/stdc++.h" #include <unordered_set> #include <unordered_map> #include <random> using namespace std; typedef long long ll; typedef unsigned long long ull; constexpr ll MOD = 1'000'000'007LL; /*998'244'353LL;*/ #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define rep(i, n) for(int (i)=0; (i)<(n); (i)++) template<class T> bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; } template<class T> bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; } constexpr int dy[4]={-1,0,1,0}; constexpr int dx[4]={0,-1,0,1}; template<typename T> struct SegmentTree{ private: int n, N; vector<T> node; function<T(T, T)> F; T E; public: void init(int _n, function<T(T, T)> f, T e){ F = f; E = e; n = _n; N = 1; while(N < n) N = (N<<1); node.assign(2*N-1, e); } void init(vector<T> v, function<T(T, T)> f, T e){ F = f; E = e; n = v.size(); N = 1; while(N < n) N = (N<<1); node.assign(2*N-1, e); for(int i=0; i<n; i++) node[N-1+i] = v[i]; for(int i=N-2; i>=0; i--) node[i] = F(node[(i<<1)+1], node[(i<<1)+2]); } T& operator [](int a){ return node[N-1+a]; } void update(int a, T x){ a += N-1; node[a] = x; while(a > 0){ a = (a-1)>>1; node[a] = F(node[(a<<1)+1], node[(a<<1)+2]); } } T query(int a, int b, int k=0, int l=0, int r=-1){ if(r == -1) r = N; if(b <= l || r <= a) return E; if(a <= l && r <= b) return node[k]; return F(query(a, b, (k<<1)+1, l, (l+r)>>1), query(a, b, (k<<1)+2, (l+r)>>1, r)); } int find_right(function<bool(T)> g, int a){ if(!g(E)) return -1; T t = E; return min(find_right(g, a, 0, 0, N, t), n); } int find_right(function<bool(T)> g, int a, int k, int l, int r, T &t){ if(r-l == 1){ t = F(t, node[k]); return g(t) ? r : a; } int m = (l + r) >> 1; if(m <= a) return find_right(g, a, (k<<1)+2, m, r, t); if(a <= l && g(F(t, node[k]))){ t = F(t, node[k]); return r; } int L = find_right(g, a, (k<<1)+1, l, m, t); if(L < m) return L; int R = find_right(g, a, (k<<1)+2, m, r, t); return max(L, R); } int find_left(function<bool(T)> g, int b){ if(!g(E)) return n + 1; T t = E; return find_left(g, b, 0, 0, N, t); } int find_left(function<bool(T)> g, int b, int k, int l, int r, T t){ if(r-l == 1){ t = F(node[k], t); return g(t) ? l : b; } int m = (l + r) >> 1; if(b <= m) return find_left(g, b, (k<<1)+1, l, m, t); if(r <= b && g(F(node[k], t))){ t = F(node[k], t); return l; } int R = find_left(g, b, (k<<1)+2, m, r, t); if(m < R) return R; int L = find_left(g, b, (k<<1)+1, l, m, t); return min(L, R); } }; int N; int a[300000]; SegmentTree<int> st; signed main(){ cin >> N; rep(i, N) cin >> a[i]; st.init(N, [](int a, int b){ return a+b; }, 0); ll ans = 0; rep(i, N){ ans += st.query(a[i], N); st.update(a[i], 1); } rep(i, N){ cout << ans << endl; ans -= a[i]; ans += N - 1 - a[i]; } }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <utility> #include <set> #include <map> #include <cmath> #include <queue> #include <cstdio> #include <limits> #include <chrono> #include <ctime> #include <random> #define rep(i,n) for(int i = 0; i < n; ++i) #define rep1(i,n) for(int i = 1; i <= n; ++i) using namespace std; using namespace chrono; template<class T>bool chmax(T &a, const T &b) { if(a < b){ a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if(a > b){ a = b; return 1; } return 0; } template<class T> inline int sz(T &a) { return a.size(); } using ll = long long; using ld = long double; using pi = pair<int,int>; using pl = pair<ll,ll>; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; using vvl = vector<vl>; const int inf = numeric_limits<int>::max(); const ll infll = numeric_limits<ll>::max(); int si,sj; vvi t; vvi p; // そのマスを使ったか? vvi flag_mass; // そのタイル番号を使ったか? vi flag_tile; vi dx = {0, 1, 0, -1}; vi dy = {1, 0, -1, 0}; map<int,char> mp = {{0, 'R'}, {1, 'D'}, {2, 'L'}, {3, 'U'}}; void Input() { cin >> si >> sj; t.assign(50, vi(50)); p.assign(50, vi(50)); rep(i,50) { rep(j,50) { cin >> t[i][j]; } } rep(i,50) { rep(j,50) { cin >> p[i][j]; } } } string res; string keiro; int max_point = 0; int point = 0; auto startClock = system_clock::now(); int step = 0; double END_TIME = 1.95; vi junban = {0, 1, 2, 3}; int num; double X = 1./8.; int bot = 0; int flag = 0; void Dfs(int x, int y) { point += p[x][y]; if(chmax(max_point, point)) { res = keiro; num = bot; } flag_tile[t[x][y]] = 1; step++; if(step % 1000 == 0) { double time = duration_cast<microseconds>(system_clock::now() - startClock).count() * 1e-6; if(time >= END_TIME * X) { if(flag) { cout << res << "\n"; exit(0); } flag_tile.assign(2501, 0); keiro.clear(); point = 0; X += 1./8.; bot++; if(bot == 4) { X = 1; bot = num; flag = 1; } Dfs(si, sj); } } // random_device seed_gen; // mt19937 engine(seed_gen()); // shuffle(junban.begin(), junban.end(), engine); rep(i,4) { int j = (i+bot) % 4; int nx = x + dx[j], ny = y + dy[j]; if(0 <= nx && nx < 50 && 0 <= ny && ny < 50) { int t_num = t[nx][ny]; if(flag_tile[t_num]) continue; keiro.push_back(mp[j]); Dfs(nx, ny); keiro.pop_back(); } } point -= p[x][y]; flag_tile[t[x][y]] = 0; } void Solve() { flag_tile.assign(2501, 0); startClock = system_clock::now(); Dfs(si, sj); } int main() { Input(); Solve(); 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 vvb= vector<vb>; using vi= vector<int>; using vvi= vector<vi>; using vs= vector<string>; #define endl "\n" const int N=50; const vi di= { -1, 1, 0, 0}; const vi dj= { 0, 0, -1, 1}; const vector<char> dc= { 'U', 'D', 'L', 'R' }; vvb vis0(N, vb(N)); /* direct vis */ vvb vis1(N, vb(N)); /* also vis */ vvi t(N, vi(N)); vvi p(N, vi(N)); inline void alsoset(int i, int j) { for(int k=0; k<4; k++) { const int ii=i+di[k]; const int jj=j+dj[k]; if(ii>=0 && ii<N && jj>=0 && jj<N && t[i][j]==t[ii][jj]) { vis1[ii][jj]=vis0[i][j]; break; } } }; const int T=2e7; int cnt=0; ll ans=0; /* best answer so far */ string apath; /* path of best answer */ ll sum=0; /* sum in current path */ string path; /* current path */ void go(int i, int j) { if(cnt++>T) return; //assert(!vis0[i][j] && !vis1[i][j]); vis0[i][j]=true; alsoset(i,j); sum+=p[i][j]; if(sum>ans) { ans=sum; apath=path; } using t3=tuple<int,int,int>; vector<t3> next; for(int d=0; d<4; d++) { const int ii=i+di[d]; const int jj=j+dj[d]; if(ii>=0 && ii<N && jj>=0 && jj<N && !vis0[ii][jj] && !vis1[ii][jj]) { next.emplace_back(ii,jj,d); } } /* Now sort the next steps by some criteria. * 1. Use a simple heuristic where we want to stay on * maximize the distance between the middle of the map and the * borders. */ sort(all(next), [](t3 &p1, t3 &p2) { auto [p1x,p1y,p1d]=p1; auto [p2x,p2y,p2d]=p2; /* dist from center quad */ const int dc1=abs(N/2-p1x)*abs(N/2-p1x)+abs(N/2-p1y)*abs(N/2-p1y); const int dc2=abs(N/2-p2x)*abs(N/2-p2x)+abs(N/2-p2y)*abs(N/2-p2y); /* dist from border quad */ const int x1=min(p1x, N-p1x); const int y1=min(p1y, N-p1y); const int d1=min(dc1, min(x1,y1)*min(x1,y1)); const int x2=min(p2x, N-p2x); const int y2=min(p2y, N-p2y); const int d2=min(dc2, min(x2,y2)*min(x2,y2)); /* maximize both distances */ return d1 > d2; }); for(auto [iii,jjj,ddd] : next) { path+=dc[ddd]; go(iii,jjj); path.pop_back(); } vis0[i][j]=false; alsoset(i,j); sum-=p[i][j]; }; /* There is a more or less simple brute force, * first implement that. */ void solve() { cini(si); cini(sj); for(int i=0; i<N; i++) for(int j=0; j<N; j++) cin>>t[i][j]; for(int i=0; i<N; i++) for(int j=0; j<N; j++) cin>>p[i][j]; go(si,sj); cerr<<ans<<endl; cout<<apath<<endl; } signed main() { solve(); }
#include<bits/stdc++.h> typedef long long ll; //#define ll long long #define int long long //#define ull unsigned long long //#define PI pair<int,int> //#define PII pair<int,PI> //#define PI pair<ll,int> //#define endl "\n" using namespace std; const int maxm=2e3+5; const int p=1e4; int read(){ string s;cin>>s; int t=4; int f=0; int ans=0; for(auto i:s){ if(i=='.'){ f=1; continue; }else{ ans=ans*10+(i-'0'); if(f)t--; } } while(t){ ans=ans*10; t--; } return ans; } signed main(){ // ios::sync_with_stdio(0); // int X=read(); int Y=read(); int R=read(); //定义横坐标左右边界 int lc=X-R; int rc=X+R; while(lc%p)lc++; while(rc%p)rc--; //定义上下界指针 int up=Y,down=Y; while(up%p)up++;//取整 while(down%p)down--; // int ans=0; for(int i=lc;i<=rc;i+=p){//枚举横坐标 int ix=abs(X-i); // while(ix*ix+(up-Y)*(up-Y)>R*R)up-=p;//up下移 while(up<=Y||ix*ix+(up-Y)*(up-Y)<=R*R)up+=p;//up上移 // while(ix*ix+(Y-down)*(Y-down)>R*R)down+=p;//down上移 while(down>=Y||ix*ix+(Y-down)*(Y-down)<=R*R)down-=p;//down下移 // int add=(up-down)/p-1; if(add>0)ans+=add; } cout<<ans<<endl; return 0; } /* */
#pragma GCC optimize ("O2") #pragma GCC target ("avx2") #include<bits/stdc++.h> //#include<atcoder/all> //using namespace atcoder; using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please int main() { cin.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; int A[1001], B[1001]; char C[1001]; rep(i, M) { cin >> A[i] >> B[i] >> C[i]; A[i]--; B[i]--; } vector<int> E[1000010]; rep(i, M) rep(j, M) { if (C[i] != C[j]) continue; if (i == j) continue; E[A[i] * N + A[j]].pb(B[i] * N + B[j]); E[A[i] * N + B[j]].pb(B[i] * N + A[j]); E[B[i] * N + A[j]].pb(A[i] * N + B[j]); E[B[i] * N + B[j]].pb(A[i] * N + A[j]); } int D[1000100] = {}; rep(i, N* N) D[i] = 1e9; queue<int> que; que.push(N - 1); D[N - 1] = 0; while (que.size()) { int u = que.front(); que.pop(); for (auto to : E[u]) { if (D[to] < 1e8) continue; chmin(D[to], D[u] + 1); que.push(to); } } //if (D[(N - 1) * N] > 1e8) co(-1); //else co(D[(N - 1) * N]); int kotae = 1e9; rep(i, N) chmin(kotae, D[i * N + i] * 2); rep(i, M) chmin(kotae, D[A[i] * N + B[i]] * 2 + 1); rep(i, M) chmin(kotae, D[B[i] * N + A[i]] * 2 + 1); if (kotae > 1e8) co(-1); else co(kotae); Would you please return 0; }
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define fi first #define se second #define pb push_back #define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k)) #define forn(i,a,b) for(int i = a; i < b; i++) #define forr(i,a,b) for(int i = a; i >= b; i--) #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int,int> ii; typedef vector<int> vi; typedef vector<ii> vii; const ll mod = 1e9+7; int sum(int a, int b){return (a+b) % mod;} int sub(int a, int b){return (a + mod - b) % mod;} int mul(int a, int b){return (1ll * a * b) % mod;} int power(int a,int b){ int res = 1; while(b){ if(b&1)res = mul(res,a); b >>= 1; a = mul(a,a); } return res; } const int maxn = 400000+10; vi graph[maxn]; int N,M,seen[maxn]; int dfs(int u,int p,int &ciclo){ int res = 1; seen[u] = 1; int cnt = 0; for(auto &v : graph[u]){ if(v == u)ciclo = 1; if(v == p){ cnt++; continue; } if(!seen[v]){ res += dfs(v,u,ciclo); }else{ ciclo = 1; } } if(cnt > 1){ ciclo = 1; } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> N; forn(i,0,N){ int u,v; cin >> u >> v; u--; v--; graph[u].pb(v); graph[v].pb(u); } int res = 0; forn(i,0,maxn){ if(seen[i])continue; int ciclo = 0; int cnt = dfs(i,-1,ciclo); if(ciclo) res += cnt; else res += cnt-1; } cout << res << '\n'; return 0; } /* __builtin_mul_overflow(x,y,&x) -fsplit-stack */
#include <bits/stdc++.h> #define debug(var) do{std::cout << #var << " :";std::cout << std::endl;view(var);}while(0) template<typename T> void view(T e){ std::cout << e << std::endl;} template<typename T> void view(const std::vector<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; } template<typename T, typename U> void view(const std::pair<T, U>& v){ std::cout << v.first << ", " << v.second << std::endl; } template<typename T> void view(const std::set<T>& v){ int c = v.size(); std::cout << "{"; for(const auto& e : v){ std::cout << e; if (--c) std::cout << ", "; } std::cout << "}" << std::endl; } template<typename T, typename U> void view(const std::map<T, U>& v){ std::cout << "{" << std::endl; for(const std::pair<T, U>& e : v){ std::cout << e.first << ", " << e.second << std::endl; } std::cout << "}" << std::endl; } template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cout << "{" << std::endl; for(const auto& v : vv){ view(v); } std::cout << "}" << std::endl; } using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; typedef pair<int, int> pii; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1LL << 60; // const long long MOD = 1000000007; const double PI = acos(-1); const long long MOD = 998244353; int H, W; vector<string> field; bool is_valid_move(int h, int w) { if (h < 0 || h >= H || w < 0 || w >= W) return false; return true; } int main() { cin >> H >> W; field.resize(H); rep(i, H) cin >> field[i]; ll res = 1; bool flag = false; for (int h = 1; h < H; h++) { int tmp = h; int w = 0; int p; if (field[h][w] == '.') p = 0; else if (field[h][w] == 'R') p = 1; else p = 2; while (is_valid_move(tmp - 1, w + 1)) { tmp--; w++; if (field[tmp][w] == 'B') { if (p == 1) { res = 0; flag = true; break; } p = 2; } if (field[tmp][w] == 'R') { if (p == 2) { res = 0; flag = true; break; } p = 1; } } if (flag) break; if (p == 0) { res *= 2; res %= MOD; } } for (int w = 1; w < W - 1; w++) { int tmp = w; int h = H - 1; int p; if (field[h][w] == '.') p = 0; else if (field[h][w] == 'R') p = 1; else p = 2; while (is_valid_move(h - 1, tmp + 1)) { tmp++; h--; if (field[h][tmp] == 'B') { if (p == 1) { res = 0; flag = true; break; } p = 2; } if (field[h][tmp] == 'R') { if (p == 2) { res = 0; flag = true; break; } p = 1; } } if (flag) break; if (p == 0) { res *= 2; res %= MOD; } } if (field[0][0] == '.') { res *= 2; res %= MOD; } if (field[H - 1][W - 1] == '.') { res *= 2; res %= MOD; } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long int lld; typedef pair<int,int> pi; typedef pair<lld,lld> pl; typedef pair<int,lld> pil; typedef pair<lld,int> pli; typedef vector<int> vit; typedef vector<vit> vitt; typedef vector<lld> vlt; typedef vector<vlt> vltt; typedef vector<pi> vpit; typedef vector<vpit> vpitt; typedef long double ld; #define x first #define y second #define pb push_back #define all(v) v.begin(), v.end() #define sz(x) (int)x.size() #define mk(a,b) make_pair(a,b) bool isrange(int y,int x,int n,int m){ if(0<=y&&y<n&&0<=x&&x<m) return true; return false; } int dy[4] = {1,0,-1,0},dx[4]={0,1,0,-1},ddy[8] = {1,0,-1,0,1,1,-1,-1},ddx[8] = {0,1,0,-1,1,-1,1,-1}; const int MAX = 5050; const lld mod = 998244353; lld dp[12][MAX],fact[MAX],nCr[MAX][MAX]; void solve(int tc){ int n,m; scanf("%d%d",&n,&m); for(int e=0;e<MAX;e++){ nCr[e][0] = nCr[e][e] = 1; } for(int e=2;e<MAX;e++){ for(int p=1;p<e;p++){ nCr[e][p] = (nCr[e-1][p]+nCr[e-1][p-1])%mod; } } lld ans = 0; for(int e=0;e<=m;e+=2) dp[0][e] = nCr[n][e]; for(int e=1;e<12;e++){ for(int p=0;p<=m;p+=(1<<e)*2){ for(int q=0;q<=m-p;q++){ dp[e][q+p] = (dp[e][q+p]+dp[e-1][q]*nCr[n][p/(1<<e)])%mod; } } } printf("%lld\n",dp[11][m]); } int main(void){ /* ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); */ int tc = 1; /* cin >> tc; */ for(int test_number=1;test_number<=tc;test_number++){ solve(test_number); } return 0; }
#include <bits/stdc++.h> using namespace std; #define co(n) cout<<n<<endl; const double eps=1e-10,pi=3.1415926535898; const int mod=998244353,maxn=5e3+10; int T,n,m,k,x,y; long long dp[maxn][15],vis[maxn][15]; long long fac[maxn]={1,1},inv[maxn]={1,1},fac_inv[maxn]={1,1}; void init() { for (int i=2;i<maxn;++i) { fac[i] = i*fac[i-1]%mod; inv[i] = (mod-mod/i)*inv[mod%i]%mod; fac_inv[i] = fac_inv[i-1]*inv[i]%mod; } } long long C(int n, int m) { if (n < m or m < 0) return 0; return fac[n]*fac_inv[n-m]%mod*fac_inv[m]%mod; } long long dfs(int x, int y) { if (vis[x][y]) { return dp[x][y]; } vis[x][y] = 1; if (y == 0) { return dp[x][y] = C(n, x); } long long cnt=0; int z=1<<y; for (int i=0;i*z<=x and i<=n;i+=2) { cnt += C(n, i)*dfs(x-i*z, y-1)%mod; } return dp[x][y]=cnt%mod; } int main(int argc, char const *argv[]) { init(); scanf("%d %d", &n, &m); if (m&1) { printf("0\n"); return 0; } dfs(m, 12); printf("%lld\n", dp[m][12]); return 0; } /* #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") clock_t clk; clk = clock(); if(clock() - clk > CLOCKS_PER_SEC * 0.9) T W L A C M E R E L E E */
// Template #include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <iomanip> #include <tuple> #include <utility> #include <queue> #include <set> #include <map> #define rep_override(x, y, z, name, ...) name #define rep2(i, n) for (int i = 0; i < (int)(n); ++i) #define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i) #define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; constexpr int inf = 1001001001; constexpr ll infll = 3003003003003003003LL; template <typename T> inline bool chmin(T &x, const T &y) { if (x > y) { x = y; return true; } return false; } template <typename T> inline bool chmax(T &x, const T &y) { if (x < y) { x = y; return true; } return false; } template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &element : vec) is >> element; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) { os << vec[i] << (i + 1 == vec_len ? "" : " "); } return os; } struct IOSET { IOSET() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); } } ioset; // Main pair<int, int> di(const vector<vector<int>> &g) { int u = 0, v = 0; { vector<int> d1(g.size(), -1); queue<int> q; q.push(0); d1[0] = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int y : g[x]) { if (d1[y] == -1) { d1[y] = d1[x] + 1; q.push(y); } } } int mx = 0; rep(i, g.size()) if (chmax(mx, d1[i])) u = i; } { vector<int> d1(g.size(), -1); queue<int> q; q.push(u); d1[u] = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int y : g[x]) { if (d1[y] == -1) { d1[y] = d1[x] + 1; q.push(y); } } } int mx = 0; rep(i, g.size()) if (chmax(mx, d1[i])) v = i; } return {u, v}; } int main() { int n; cin >> n; vector<vector<int>> g(n); rep(i, n - 1) { int a, b; cin >> a >> b; --a; --b; g[a].push_back(b); g[b].push_back(a); } auto [u, v] = di(g); vector<int> mark(n, 0); vector<int> prv(n, -1); { queue<int> q; q.push(u); while (!q.empty()) { int x = q.front(); q.pop(); for (int y : g[x]) { if (y != prv[x]) { prv[y] = x; q.push(y); } } } int cur = v; mark[cur] = 1; while (cur != u) { cur = prv[cur]; mark[cur] = 1; } } vector<int> ans(n); { int cur = 1; auto dfs = [&](auto &&dfs, int x) -> void { ans[x] = cur; for (int y : g[x]) { if (y != prv[x] && !mark[y]) { ++cur; dfs(dfs, y); ++cur; } } for (int y : g[x]) { if (y != prv[x] && mark[y]) { ++cur; dfs(dfs, y); ++cur; } } }; dfs(dfs, u); } cout << ans << "\n"; }
#include <bits/stdc++.h> #define fi first #define se second #define sz(v) ((int)v.size()) #define all(v) (v).begin(), (v).end() #define pb push_back using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> pii; int n; int S,E; int ans[200010],cur; bool chk[200010]; vector<int> g[200010]; pii dfs(int p,int u) { pii r={0,u}; for(auto& v:g[u]) { if(v==p)continue; pii rr=dfs(u,v); r=max(r,{rr.fi+1,rr.se}); } return r; } bool F(int p,int u) { chk[u]=true; if(u==E)return true; for(auto& v:g[u]) { if(v==p)continue; if(F(u,v))return true; } chk[u]=false; return false; } void G(int p,int u) { ans[u]=++cur; for(auto& v:g[u]) { if(v==p||chk[v])continue; G(u,v); ++cur; } for(auto& v:g[u]) { if(v==p)continue; if(chk[v]) { G(u,v); ++cur; } } } int main() { ios::sync_with_stdio(false); cin.tie(0); int i,j; cin>>n; for(i=0;i<n-1;i++) { int u,v;cin>>u>>v; g[u].pb(v); g[v].pb(u); } S=dfs(1,1).se; E=dfs(S,S).se; F(S,S); G(S,S); for(i=1;i<=n;i++) { cout<<ans[i]<<' '; } return 0; }
#include <bits/stdc++.h> #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 ALL(x) (x).begin(), (x).end() using namespace std; constexpr int inf = 1 << 30; constexpr long long llinf = 1LL << 62; constexpr int mod = 1000000007; // 998244353; constexpr int dy[4] = {-1, 0, 1, 0}, dx[4] = {0, -1, 0, 1}; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; inline int popcount(ll x) { return __builtin_popcountll(x); } inline int div2num(ll x) { return __builtin_ctzll(x); } inline bool bit(ll x, int b) { return (x >> b) & 1; } 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; } namespace FastIO { struct Pre { char ptr[10000 * 4]; int rd[1 << 16]; int tenpow[4]; constexpr Pre() : ptr(), rd(), tenpow() { for (int i = 0; i < 10000; ++i) { int j = i; int temp = 0; for (int t = 3; t >= 0; --t) { ptr[i * 4 + t] = j % 10 + '0'; temp += (1 << ((3 - t) * 4)) * (j % 10); j /= 10; } rd[temp] = i; } tenpow[0] = 1; tenpow[1] = 10; tenpow[2] = 100; tenpow[3] = 1000; } } constexpr ptr; static constexpr size_t buf_size = 1 << 20; char buf_in[buf_size], buf_out[buf_size]; size_t pt_in = 0, pt_out = 0, tail_in = 0; inline size_t num_digits(long long x) { if (x >= (long long)1e9) { if (x >= (long long)1e18) return 19; if (x >= (long long)1e17) return 18; if (x >= (long long)1e16) return 17; if (x >= (long long)1e15) return 16; if (x >= (long long)1e14) return 15; if (x >= (long long)1e13) return 14; if (x >= (long long)1e12) return 13; if (x >= (long long)1e11) return 12; if (x >= (long long)1e10) return 11; return 10; } else { if (x >= (long long)1e8) return 9; if (x >= (long long)1e7) return 8; if (x >= (long long)1e6) return 7; if (x >= (long long)1e5) return 6; if (x >= (long long)1e4) return 5; if (x >= (long long)1e3) return 4; if (x >= (long long)1e2) return 3; if (x >= (long long)1e1) return 2; return 1; } } inline void load() { memcpy(buf_in, buf_in + pt_in, tail_in - pt_in); size_t width = tail_in - pt_in; tail_in = width + fread(buf_in + width, 1, buf_size - width, stdin); pt_in = 0; } inline void flush() { fwrite(buf_out, 1, pt_out, stdout); pt_out = 0; } inline void scan(char& c) { c = buf_in[pt_in++]; } template <class T> inline void scan(T& x) { if (pt_in + 32 > tail_in) load(); char c; bool minus = 0; x = 0; scan(c); if (c == '-') { minus = 1; scan(c); } while (c >= '0') { x = x * 10 + (c & 15); scan(c); } if (minus) x = -x; } inline void print(char c) { buf_out[pt_out++] = c; } template <class T> inline void print(T x) { if (pt_out > buf_size - 32) flush(); if (x < 0) { print('-'); x = -x; } size_t digits = num_digits(x); int i; for (i = pt_out + digits - 4; i > (int)pt_out; i -= 4) { memcpy(buf_out + i, ptr.ptr + (x % 10000) * 4, 4); x /= 10000; } memcpy(buf_out + pt_out, ptr.ptr + x * 4 + (pt_out - i), 4 + i - pt_out); pt_out += digits; } template <class T> inline void println(T x) { print(x); print('\n'); } struct SetUp { SetUp() { load(); } ~SetUp() { flush(); } } setup; } // namespace FastIO using FastIO::print; using FastIO::println; using FastIO::scan; constexpr int Nmax = 200001; ll A[Nmax], acc[Nmax], maxs[Nmax]; int main() { int N; scan(N); REP(i, N) scan(A[i]); acc[0] = A[0]; maxs[0] = max(0LL, A[0]); FOR(i, 1, N) { acc[i] = acc[i - 1] + A[i]; maxs[i] = max(maxs[i - 1], acc[i]); } ll ans = 0, t = 0; REP(i, N) { chmax(ans, t + maxs[i]); t += acc[i]; } println(ans); return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int64_t N; cin >> N; vector<int64_t> A(N), F(N, 0); int64_t curPos = 0, curMoves = 0, maxPos = 0; for (int64_t i = 0; i < N; ++i) { cin >> A[i]; curMoves += A[i]; F[i] = max((int64_t)0, curMoves); if (i) F[i] = max(F[i], F[i - 1]); maxPos = max(maxPos, curPos + F[i]); if (i) A[i] += A[i - 1]; curPos += A[i]; } cout << maxPos << endl; return 0; }
/* Author : Chandan Agrawal College : Poornima College of Engg. jaipur, Raj Mail : [email protected] " when you are not practicing someone else is , and the day u meet them u will lose " */ #include<bits/stdc++.h> #include<stdio.h> using namespace std; #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define MAX 10000050 #define ll long long #define ld long double #define lli long long int #define pb push_back #define INF 1000000000000 #define mod 1000000007 // trignometric function always give value in Radians only #define PI acos(-1) //3.1415926535897932384626433832795028 #define dsin(degree) sin(degree*(PI/180.0)) #define dcos(degree) cos(degree*(PI/180.0)) #define dtan(degree) tan(degree*(PI/180.0)) #define rsin(radian) sin(radian) #define rcos(radian) cos(radian) #define rtan(radian) tan(radian) #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define memf(a) memset(a,false,sizeof(a)) #define loop(i,n) for (lli i = 0; i < n; i++) #define FOR(i,a,b) for (lli i = a; i < b; i++) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define makeuniq(v) v.resize(unique(all(v)) - v.begin()); //only uniq element in vector after this #define sz(x) int(x.size()) #define F first #define S second #define mii map<lli,lli> #define pii pair<lli,lli> #define vi vector<lli> #define vvi vector<vi> #define vpi vector<pii> #define vbool vector<bool> #define seti set<lli> #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) (a/gcd(a,b))*b #define abs(x) ((x < 0)?-(x):x) #define endl '\n' template <typename Head> void print(Head&& head) { cout<<head<<endl; } template <typename Head, typename... Tail> void print(Head&& head, Tail... tail) { cout<<head<<" "; print(tail...); } #define scanarr(a,n) for(lli i=0;i<n;i++) cin>>a[i]; #define scanvec(a,n) for(lli i=0;i<n;i++){ lli x ; cin>>x; a.pb(x);} #define printarr(a,n) for(lli i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; #define printvec(vec) for(auto xt : vec) cout<<xt<<" "; cout<<"\n"; #define FD(N) fixed<<setprecision(N) #define deb(x) cout<<#x<<" "<<x<<endl; /* 1D vector - vi dp(n,value); 2D vector - vvi dp(n,vi(n,value)); */ // chandan1,2 void chandan1(){int y=1;return;} void chandan2(){ loop(i,10){ lli x=1; } return(chandan1()); } //---------------------------------------------------BIT---------------------------------------------------------------------- lli BIT[MAX], a[MAX], n; void update(lli x , lli val) { while(x<MAX) { BIT[x] += val; x += (x&-x); } } lli query(lli x) { lli sum = 0; while(x>0) { sum += BIT[x]; x -= (x&-x); } return sum; } //--------------------------------------------------BIT------------------------------------------------------------------------ int main(){ fastio lli t=1; //cin>>t; chandan2(); while(t--) { cin>>n; lli maxi = -1; FOR(i,1,n+1) { cin>>a[i]; a[i]++; maxi = max(maxi ,a[i]); } lli cnt =0; FOR(i , 1, n+1) { cnt += (query(MAX)-query(a[i])); update(a[i],1); } FOR(i,1,n+1) { print(cnt); cnt = cnt - (a[i]-1) + (n-a[i]); /* after shifing a element K from front to back how many inversion pairs we loose ? K-1 as there are K-1 values less than K and how mnany new inversion pair get ? (N-K) as there are N-K values greater than K */ } mem0(BIT); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200000 + 9; int a[N], b[N], p[N], sk[N], top; bool vis[N]; inline bool check(int j){ return !(a[j] <= b[p[j]] && p[j] != j); } int main(){ int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) scanf("%d", b + i); for (int i = 1; i <= n; ++i) scanf("%d", p + i); bool f = true; vector<pair<int, int>> ans; for (int i = 1; i <= n; ++i){ if (!vis[i]){ int j = i; if (!check(j)){ f = false; break; } sk[++top] = j; vis[j] = true; while (!vis[p[j]]){ j = p[j]; if (!check(j)){ f = false; break; } if (a[j] <= a[sk[top]]){ ans.push_back({j, sk[top]}); p[sk[top]] = p[j]; } else { sk[++top] = j; } vis[j] = true; } if (!f) break; for (j = 1; j < top; ++j){ ans.push_back({sk[j], sk[top]}); } top = 0; } } if (f){ printf("%d\n", (int)ans.size()); for (auto it : ans){ printf("%d %d\n", it.first, it.second); } } else { printf("-1\n"); } return 0; }
#include<bits/stdc++.h> using namespace std; #define M 998244353 #define N 200000 typedef long long int LL; LL fact[301],invfact[301],sum[301],sum2[301]; LL ap[N][301],p2[301]; LL mult(LL a, LL b){ return a*b%M; } LL add(LL a, LL b){ return (a+b+M)%M; } LL c(LL n, LL k){ return mult(fact[n],mult(invfact[k],invfact[n-k])); } LL p(LL b, LL x){ if(x==0) return 1LL; LL sub=p(b,x/2); sub=mult(sub,sub); if(x&1) sub=mult(sub,b); return sub; } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int n,k; cin>>n>>k; int a[n]; for(LL i=0;i<n;i++){ scanf("%d",a+i); } for(LL i=0;i<n;i++){ ap[i][0]=1; } for(LL i=0;i<n;i++){ for(LL j=1;j<=k;j++){ ap[i][j]=mult(ap[i][j-1],a[i]); } } fact[0]=invfact[0]=1; for(LL i=1;i<301;i++){ fact[i]=mult(fact[i-1],i),invfact[i]=p(fact[i],M-2); } p2[0]=1; for(LL i=1;i<301;i++){ p2[i]=mult(2,p2[i-1]); } for(LL i=0;i<n;i++){ for(LL j=0;j<=k;j++){ sum[j]=add(sum[j],ap[i][j]); } } LL tinv=p(2,M-2); for(LL X=1;X<=k;X++){ LL ans=0; for(LL x=0;x<=X;x++){ ans=add(ans,mult(c(X,x),mult(sum[X-x],sum[x]))); } ans=add(ans,-mult(p2[X],sum[X])); ans=mult(ans,tinv); cout<<ans<<"\n"; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,n) for (long long i = 0; i < (n); ++i) #define DIV 998244353 #define INF LONG_MAX/3 #define bit(n) (1LL<<(n)) template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; ll memo1[305]; ll memo2[305]; long long kaidan[10000000]; void kaical(long long n){ kaidan[0] = 1; for(long long i = 1; i < n; i++){ kaidan[i] = kaidan[i-1] * i; kaidan[i] %= DIV; } } long long modpow(long long ori, long long po){ long long res = 1; while(po > 0){ if(po&1){ res *= ori; res %= DIV; } ori *= ori; ori %= DIV; po >>= 1; } return res; } int main(){ ll N, K; cin >> N >> K; vector<ll> A(N); rep(i, N) scanf("%lld", &A[i]); kaical(K+5); for(ll k = 0; k <= K; k++) { ll cur = 0; ll kai = modpow(kaidan[k], DIV-2); rep(i, N) { ll tmp = modpow(A[i], k); tmp *= kai; tmp %= DIV; cur += tmp; cur %= DIV; } memo1[k] = cur; cur = 0; rep(i, N) { cur -= modpow(2*A[i], k); cur += DIV; cur %= DIV; } memo2[k] = cur; } ll mm = modpow(2, DIV-2); for(ll X = 1; X <= K; X++) { ll ans = kaidan[X]; //1こうめ ll tmp = 0; for(ll i = 0; i <= X; i++) { tmp += memo1[i] * memo1[X-i]; tmp %= DIV; } ans *= tmp; ans %= DIV; ans += memo2[X]; ans %= DIV; ans *= mm; ans %= DIV; cout << ans << endl; } }
// Pratiyush Mishra #include <bits/stdc++.h> #define ull unsigned long long int #define ll long long int #define LL_MAX 9223372036854775807 #define pb push_back #define pf push_front #define mp make_pair #define popb pop_back #define vl vector<ll> #define pl pair<ll,ll> #define bs(v, x) binary_search(v.begin(), v.end(), x) #define mem1(a) memset(a, -1, sizeof(a)) #define popf pop_front #define p0(x) cout << x << " " #define p1(x) cout << x << '\n' #define p2(x, y) cout << x << " " << y << '\n' #define p3(x, y, z) cout << x << " " << y << " " << z << '\n' #define printv(v) \ for (ll i = 0; i < v.size(); ++i) \ cout << v[i] << " "; \ cout << '\n' #define pr1(x) cout << fixed << setprecision(15) << x << '\n' #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> // ordered_set s ; s.order_of_key(val) no. of elements strictly less than val // s.find_by_order(i) itertor to ith element (0 indexed) #define mod 1000000007 #define mod1 998244353 #define fio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define get(n) \ ll n; \ cin >> n #define getvec(v, n) \ vector<ll> v(n); \ for (ll i = 0; i < n; i++) \ cin >> v[i]; #define getstr(s) \ string s; \ cin >> s #define all(x) x.begin(), x.end() #define countBits(x) __builtin_popcount(x) using namespace std; ll n, m; vl a, b; vector<vl> dp; ll solve(ll i, ll j) { if (i >= n) return m - j; if (j >= m) return n - i; if (dp[i][j] != -1) return dp[i][j]; ll ans1 = (a[i] != b[j]) + solve(i + 1, j + 1); ll ans2 = solve(i + 1, j) + 1; ll ans3 = solve(i, j + 1) + 1; ll ans = min(ans1, min(ans2, ans3)); dp[i][j] = ans; return ans; } void mainSolve() { cin >> n >> m; dp.resize(n, vl(m, -1)); for (int i = 0; i < n; i++) { get(x); a.pb(x); } for (int i = 0; i < m; i++) { get(x); b.pb(x); } ll ans = solve(0, 0); p1(ans); } int main() { fio; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif //get(t); ll t = 1; while (t--) { mainSolve(); } return 0; }
#include<iostream> #define minx(a,b) ((a)<(b)?(a):(b)) using namespace std; int n,m,i,j,f[2000][2000],a[2000],b[2000]; int main(){ cin>>n>>m; f[0][0]=0; for(i=1;i<=n;++i)cin>>a[i],f[i][0]=i; for(i=1;i<=m;++i)cin>>b[i],f[0][i]=i; for(i=1;i<=n;++i){ for(j=1;j<=m;++j){ f[i][j]=minx(f[i-1][j],f[i][j-1])+1; f[i][j]=minx(f[i-1][j-1]+(a[i]==b[j]?0:1),f[i][j]); } } cout<<f[n][m]; }
#include<bits/stdc++.h> #define rep(i,j,n) for(int i = (j); i < (n); ++i) using namespace std; #define pri(str) cout << str << endl using ll = long long; using P = pair<int, int>; const ll MX = 1e18; const long double PI = acos(-1); template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int N, T[101]; int dp[101][102010]; const int inf = 1000100; int main() { cin >> N; rep(i, 0, N) cin >> T[i]; rep(i, 0, N + 1) rep(t, 0, 101010) dp[i][t] = inf; dp[0][0] = 0; rep(i, 0, N) rep(t, 0, 101010) if (dp[i][t] != inf) { chmin(dp[i + 1][t + T[i]], dp[i][t]); chmin(dp[i + 1][t], dp[i][t] + T[i]); } int ans = inf; rep(t, 0, 101010) chmin(ans, max(t, dp[N][t])); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; // clang-format off // #include <atcoder/all> // using namespace atcoder; // using mint = modint1000000007; // using mint = modint998244353 using ll = int64_t; template <class T> istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v) cin >> a; return is; } template <class T> istream& operator>>(istream& is, vector<pair<T, T>>& v) { for (auto& a : v) cin >> a.first >> a.second; return is; } template <class T> istream& operator>>(istream& is, vector<tuple<T, T, T>>& v) { for (auto& a : v) { T a1, a2, a3; cin >> a1 >> a2 >> a3; a = {a1, a2, a3}; } return is; } template <class T> ostream& operator<<(ostream& os, vector<T>& v) { for (auto& a : v) os << a << " "; os << endl; return os; } 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; } #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) #define TRC1(a) cout << #a ":" << a #define TRC2(a, b) TRC1(a); cout << " "; TRC1(b) #define TRC3(a, b, c) TRC2(a, b); cout << " "; TRC1(c) #define TRC4(a, b, c, d) TRC3(a, b, c); cout << " "; TRC1(d) #define GET_NAME(_1,_2,_3,_4,NAME,...) NAME #define TRC(...) GET_NAME(__VA_ARGS__, TRC4, TRC3, TRC2, TRC1) (__VA_ARGS__) << endl using veci = vector<int>; using vecll = vector<ll>; using Pi = pair<int, int>; using Ti = tuple<int, int, int>; #define _overload3(_1,_2,_3,name,...) name #define _rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=int(a);i<int(b);++i) #define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__) const int IINF = INT32_MAX; const ll LINF = INT64_MAX; // cout << fixed << setprecision(15); void solve(); int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(15); solve(); return 0; } // clang-format on void solve() { int N; cin >> N; bitset<100001> bit; int tot = 0; bit[0] = 1; rep(i, N) { int t; cin >> t; tot += t; bit |= bit << t; } int ans = 1e9; rep(i, tot + 1) if (bit[i]) ans = min(ans, max(i, tot - i)); cout << ans << endl; }
#include <stdio.h> int main(){ int N; scanf("%d",&N); int A[N],P[N],X[N]; int a,b,c; long long d=1000000000001; int e=0; for(a=0;a<N;a++) { scanf("%d %d %d\n",&A[a],&P[a],&X[a]); } for(b=0;b<N;b++) { if(X[b]-A[b]>0) { if(d>P[b]) { d=P[b]; } } else e++; } if(e!=N) printf("%lld",d); else printf("-1"); return 0; }
//CODED BY SUMIT KUMAR PRAJAPATI #include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pl; #define PI 3.1415926535897932384626 #define pb push_back #define mk make_pair #define ff first #define ss second #define watch(x) cerr<<#x<<" = "<<x<<'\n' #define rep(i,n) for(ll i=0;i<n;i++) #define repe(i,n) for(ll i=1;i<=n;i++) #define FOR(i,a,b) for(ll 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() const int INF=1e9; const int MX=1e5+5; const int MD=1e9+7; const int MDL=99824453; auto time0 = curtime; void solve(){ int N; cin>>N; int A[N+1],P[N+1],X[N+1]; int ans=MD; repe(i,N){ cin>>A[i]>>P[i]>>X[i]; if(X[i]-(A[i]-0.5+1)>0) ans=min(ans,P[i]); //t a-=(t-0.5+1) } if(ans==MD) cout<<"-1\n"; else cout<<ans<<'\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); srand(time(0)); time0 = curtime; ll t=1; //cin>>t; repe(tt,t){ //cout<<"Case #"<<tt<<": "; solve(); } cerr<<"Execution Time: "<<timedif(time0,curtime)*1e-9<<" sec\n"; return 0; }
/* code of Ayush Tiwari codeforces- servermonk codechef- ayush572000 */ #include <bits/stdc++.h> #define ll long long //STL #define pb push_back #define lb lower_bound #define ub upper_bound #define mp make_pair #define all(v) v.begin(), v.end() //loops #define forn(i,a,b) for(int i=a; i<b; i++) #define rforn(i,a,b) for(int i=a; i>=b; i--) // defined values #define maxn 200004 #define Mod 1000000007 // fast io #define FIO() ios_base::sync_with_stdio(0);cin.tie(0); using namespace std; vector<int> graph[maxn]; map<int,int> m; int a[maxn]; int vis[maxn]; void dfs(int node,int flag){ vis[node]=flag; m[a[node-1]]+=1; for(auto child: graph[node]){ if(vis[child]) continue; if(!m[a[child-1]]) dfs(child,1); else dfs(child,2); } m[a[node-1]]-=1; } void solution(){ // This is the main code int n; cin>>n; forn(i,0,n) cin>>a[i]; forn(i,0,n-1){ int u,v; cin>>u>>v; graph[u].pb(v); graph[v].pb(u); } dfs(1,1); forn(i,1,n+1){ if(vis[i]==1) cout<<i<<endl;; } } int main(){ #ifndef ONLINE_JUDGE freopen("D:/competitive-programming/input.txt","r",stdin); freopen("D:/competitive-programming/output.txt","w",stdout); #endif FIO() ll t=1; // cin>>t; while (t--) { solution(); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define Vi vector<int> #define for_(i, a, b) for(int i = (a);i < (b);++i) #define ford_(i, a, b) for(int i = (b)-1;i >= (a);--i) #define rep(i, n) for_(i, 0, n) #define repd(i, n) rfor_(i, 0, n) int ctoi(const char c){ if('0' <= c && c <= '9') return (c-'0'); return -1; } int main(){ string s,t; cin >> s >> t; cout << max(ctoi(s[0])+ctoi(s[1])+ctoi(s[2]), ctoi(t[0])+ctoi(t[1])+ctoi(t[2])); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for(int i=0; i<(n); i++) using namespace std; using vi = vector<int>; int main(){ int a, b; cin >> a >> b; int plus = 2000*b - a + 1; int minus = -2000*a - b + 1; rep(i,a){ printf("%d ", plus + 2*i); } rep(i,b){ printf("%d%c", minus + 2*i, i==b-1?'\n':' '); } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n; long long k; cin >> n >> k; long long dp[4][3000005]={0}; dp[0][0]=1; for(int i=0;i<3;i++){ for(int j=0;j<=i*n;j++){ dp[i+1][j+1]+=dp[i][j]; dp[i+1][j+n+1]-=dp[i][j]; } for(int j=1;j<=(i+1)*n;j++){ dp[i+1][j]+=dp[i+1][j-1]; } } int x; for(int i=3;i<=3*n;i++){ if(k<=dp[3][i]){x=i;break;} else{k-=dp[3][i];} } for(int i=1;i<=n;i++){ int jmi=max(1,x-i-n); int jma=min(n,x-i-1); if(jmi>jma){continue;} if(k>(jma-jmi+1)){k-=(jma-jmi+1);continue;} int y=jmi+k-1; int z=x-i-y; cout << i << ' ' << y << ' ' << z << '\n'; return 0; } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double int main() { ll mod = 1e9+7; int N; cin >> N; vector<ll> A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector<vector<ll>> sum(N, vector<ll>(2, 0)), cnt(N, vector<ll>(2, 0)); sum[0][0] = A[0]; cnt[0][0] = 1; for (int i = 1; i < N; i++) { sum[i][0] = (sum[i-1][0]+sum[i-1][1]+A[i]*(cnt[i-1][0]+cnt[i-1][1]))%mod; sum[i][1] = (sum[i-1][0]+(mod-A[i])*cnt[i-1][0])%mod; cnt[i][0] = (cnt[i-1][0]+cnt[i-1][1])%mod; cnt[i][1] = cnt[i-1][0]; } cout << (sum[N-1][0]+sum[N-1][1])%mod << endl; return 0; }
#include <iostream> #include <algorithm> #include <bitset> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #include <complex> #include <deque> #include <valarray> #include <unordered_map> #include <unordered_set> #include <array> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <chrono> #include <random> #include <numeric> using namespace std; using ll = long long; using ld = long double; using str = string; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<vs> vvs; typedef vector<vc> vvc; typedef vector<vb> vvb; using P = pair<ll,ll>; #define REP(i, n) for (ll i = 0; i < (int)(n); i++) #define RREP(i, n) for (ll i = (int)(n)-1; i > -1 ; i--) #define IN(T, x) T x;cin >> x; #define AIN(T, a, n) vector<T> a(n);REP(i, n){cin >> a[i];} #define A2IN(T1, a, T2, b, n) vector<T1> a(n);vector<T2> b(n);REP(i, n){cin >> a[i] >> b[i];} #define ALL(a) (a).begin(),(a).end() #define SORT(a) sort(ALL(a)) #define RSORT(a) SORT(a);reverse(ALL(a)) #define PB push_back #define MP make_pair #define PF first #define PS second template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;} template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;} template<typename A, size_t N, typename T>void Fill(A (&array)[N], const T &val){fill( (T*)array, (T*)(array+N), val );} #define OUT(x) cout << x << endl; #define OUTD(x) cout << fixed << x << endl; #define BOUT(b) if(b){OUT("Yes");}else{OUT("No");} const ll MOD = 1000000007; const ll INF = 1e9; const ld PI = 3.14159265369; ll dx[4] = {1,-1,0,0}; ll dy[4] = {0,0,1,-1}; ll ddx[8] = {1,1,1,-1,-1,-1,0,0}; ll ddy[8] = {0,1,-1,0,1,-1,1,-1}; unsigned GetDigit(unsigned num){ return std::to_string(num).length(); } int main() { cin.tie(0); ios::sync_with_stdio(false); IN(ll,n); AIN(ll,a,n); vvll dp(2,vector<ll>(n,0)); dp[0][0]=1,dp[1][0]=0; REP(i,n-1){ dp[0][i+1]=(dp[0][i]+dp[1][i])%MOD; dp[1][i+1]=dp[0][i]; } ll ans = 0; REP(i,n){ ans+=a[i]*(dp[0][i]*(dp[0][n-i-1]+dp[1][n-i-1])%MOD-dp[1][i]*dp[0][n-i-1]%MOD)%MOD; ans%=MOD; } OUT((ans+MOD)%MOD); }