code_file1
stringlengths
80
4k
code_file2
stringlengths
91
4k
similar_or_different
int64
0
1
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int k, x; cin >> k >> x; for (int i = 1; i < 2 * k; i++) cout << x - k + i << ' '; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; int main() { cin.tie(0); ios::sync_with_stdio(false); int sum = 0; int m = 10000; for (int i = 0;i < 4;i++){ int tmp; cin >> tmp; sum += tmp; m = min(m,tmp); } int a,b; cin >> a >> b; cout << sum-m+max(a,b) << endl; return 0; }
0
#include <functional> #include <string> #include <algorithm> #include <iostream> #include <vector> #include <utility> #include <type_traits> namespace loquat { template <int MOD> class modulus_integer { public: typedef modulus_integer<MOD> self_type; private: int m_value; static self_type unsafe_construct(int x) noexcept { self_type y; y.m_value = x; return y; } public: modulus_integer() noexcept : m_value(0) { } modulus_integer(int x) noexcept : m_value(x % MOD) { if(m_value < 0){ m_value += MOD; } } int operator*() const noexcept { return m_value; } self_type& operator=(const self_type& x) noexcept { m_value = x.m_value; return *this; } self_type operator+(const self_type& x) const noexcept { const int y = m_value + x.m_value; return unsafe_construct(y >= MOD ? y - MOD : y); } self_type operator*(const self_type& x) const noexcept { return unsafe_construct( static_cast<long long>(m_value) * x.m_value % MOD); } self_type& operator+=(const self_type& x) noexcept { return (*this = *this + x); } self_type& operator*=(const self_type& x) noexcept { return (*this = *this * x); } }; template <int MOD> inline std::ostream& operator<<( std::ostream& os, const modulus_integer<MOD>& x) { os << *x; return os; } } using namespace std; static const int MOD = 1000000007; using mint = loquat::modulus_integer<MOD>; int main(){ ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; ++i){ cin >> a[i]; } vector<int> b(n); b[0] = a[0]; for(int i = 1; i < n; ++i){ b[i] = b[i - 1] ^ a[i]; } vector<int> c(n + 1); for(int i = 0; i < n; ++i){ c[i + 1] = c[i] + (b[i] == 0 ? 1 : 0); } vector<mint> dp0(1 << 20), dpx(1 << 20); vector<int> last(1 << 20); for(int i = 0; i < n; ++i){ const int k = c[i] - c[last[b[i]]]; dp0[b[i]] += dpx[b[i]] * k; dpx[b[i]] += dp0[b[i]] + 1; last[b[i]] = i; } mint answer = 0; if(b.back() == 0){ for(int i = 1; i < (1 << 20); ++i){ answer += dpx[i]; } mint z = 1; for(int i = 1; i < c.back(); ++i){ z *= 2; } answer += z; }else{ answer = dp0[b.back()] + 1; } cout << answer << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define repp(i,a,b) for(int i = (int)(a) ; i < (int)(b) ; ++i) #define repm(i,a,b) for(int i = (int)(a) ; i > (int)(b) ; --i) const LL mod = 1e9+7; int main(){ int N; cin >> N; vector<int> A(N); int x = 0, y = 0; vector<vector<vector<int>>> name(1<<20,vector<vector<int>>(2,vector<int>(2,0))); vector<vector<LL>> dp(1<<20,vector<LL>(3,0)); repp(i,0,N){ cin >> A[i]; x ^= A[i]; if(x == 0){ ++y; } else { if(dp[x][2] < y){ (dp[x][0] += dp[x][1]*(y-dp[x][2])) %= mod; dp[x][2] = y; } (dp[x][1] += dp[x][0]+1) %= mod; } } if(x == 0){ LL ans = 1; repp(i,1,y) ans = ans*2%mod; repp(k,1,1<<20){ (ans += dp[k][1]) %= mod; } cout << ans << endl; } else { cout << dp[x][0]+1 << endl; } return 0; }
1
#include<bits/stdc++.h> const int MN=100000+5,P=1000000007,inv2=P+1>>1; using namespace std; typedef long long ll; template<typename T>inline T&IN(T&in){ in=0;char c=getchar();int f=1; while(!isdigit(c)){if(c=='-')f=-1;c=getchar();} while(isdigit(c))in=in*10+c-'0',c=getchar(); return in*=f; } int n; int a[4][MN],c[2]; int t[MN]; int lowbit(int x){return x&(-x);} void add(int p,int d){while(p<=n)t[p]+=d,p+=lowbit(p);} int sum(int p){int c=0;while(p)c+=t[p],p-=lowbit(p);return c;} void input(){ IN(n); for(int i=1;i<=3;++i)for(int j=1;j<=n;++j)IN(a[i][j]); for(int i=1;i<=n;++i){ if(a[2][i]%3!=2||abs(a[2][i]-a[1][i])!=1||abs(a[3][i]-a[2][i])!=1||(a[2][i]/3)&i&1)return puts("No"),void(); c[i&1]^=(a[1][i]>a[3][i]); } for(int i=1;i<=n;i+=2)c[0]^=(sum(n)-sum(a[2][i]/3+1))&1,add(a[2][i]/3+1,1); memset(t,0,sizeof t); for(int i=2;i<=n;i+=2)c[1]^=(sum(n)-sum(a[2][i]/3+1))&1,add(a[2][i]/3+1,1); if(c[0]==0&&c[1]==0)puts("Yes"); else puts("No"); } int main(){ input(); return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 90; int n, a[3][N], c[N], p[N], m, S, t[N * 3], bl[N * 3]; bool r[N]; void Ji (bool x) { if (!x) puts("No"), exit(0); } bool Pd () { bool ret = 0; for (int i = 1; i <= S; ++i) t[i] = 0; for (int i = 1; i <= m; ++i) ++t[p[i]]; for (int i = 1, j = 0; i <= S; ++i) if (t[i]) bl[i] = ++j; for (int i = 1; i <= m; ++i) while (i != bl[p[i]]) std :: swap(p[i], p[bl[p[i]]]), ret ^= 1; return ret; } int main () { scanf("%d", &n); S = 3 * n; for (int i = 0; i < 3; ++i) for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]); for (int i = 1; i <= n; ++i) { if (a[0][i] > a[1][i]) { Ji(a[0][i] == a[1][i] + 1); Ji(a[1][i] == a[2][i] + 1); Ji(a[1][i] % 3 == 2); c[i] = a[1][i] / 3; Ji((c[i] ^ i) & 1); ++c[i]; r[i & 1] ^= 1; } else { Ji(a[0][i] + 1 == a[1][i]); Ji(a[1][i] + 1 == a[2][i]); Ji(a[1][i] % 3 == 2); c[i] = a[1][i] / 3; Ji((c[i] ^ i) & 1); ++c[i]; } } for (int i = 1; i <= n; ++i, ++i) p[++m] = c[i]; Ji(Pd() == r[0]); m = 0; for (int i = 2; i <= n; ++i, ++i) p[++m] = c[i]; Ji(Pd() == r[1]); puts("Yes"); return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; using PLL = pair<ll, ll>; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define repn(i,n) for(int i = 0; i <= (int)(n); i++) #define srep(i,l,n) for(int i = l; i < (int)(n); i++) #define srepn(i,l,n) for(int i = l; i <= (int)(n); i++) #define drep(i,n) for(int i = (int)(n-1); i >= 0; i--) #define drepn(i,n) for(int i = (int)(n); i >= 0; i--) #define size(s) (int)s.size() #define debug(var) do{std::cout << #var << " : ";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){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;} template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } } template<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;} template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template<class T> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);} template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;} template<class T = int> using V = vector<T>; template<class T = int> using VV = vector<V<T>>; bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;} void Yes(){cout << "Yes" << endl;} void No(){cout << "No" << endl;} void YES(){cout << "YES" << endl;} void NO(){cout << "NO" << endl;} void err() {cout << -1 << endl;} #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define pb push_back #define ep emplace_back const int MOD = 1000000007; const int INF = 1e9; #define PI acos(-1); int dx[4] = {1,-1,0,0}; int dy[4] = {0,0,1,-1}; int ddx[8] = {1,1,1,-1,-1,-1,0,0}; int ddy[8] = {0,1,-1,0,1,-1,1,-1}; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; V<> a(n+2); a[0] = a[n+1] = 0; for(int i = 1; i <= n; i++) cin >> a[i]; ll sum = 0; for(int i = 0; i < n + 1; i++) { sum += abs(a[i] - a[i + 1]); } for(int i = 1; i <= n; i++) { ll ans = sum; ans -= abs(a[i - 1] - a[i]); ans -= abs(a[i + 1] - a[i]); ans += abs(a[i - 1] - a[i + 1]); cout << ans << endl; } }
#include <iostream> #include <string> #include <vector> #include <queue> #include <map> #include <unordered_map> #include <utility> #include <algorithm> #include <cmath> #include <array> #include <bitset> using namespace std; typedef long long ll; const ll mod=1000000007; #define rep(i,n) for(int i=0;i<n;i++) #define Rep(i,n) for(int i=1;i<n+1;i++) int main(){ int n,m; ll x1,x2,X=0,y1,y2,Y=0; cin>>n>>m; //x cin>>x1; rep(i,n-1){ cin>>x2; X=(X+(x2-x1)*(i+1)*(n-i-1))%mod; x1=x2; } //y cin>>y1; rep(i,m-1){ cin>>y2; Y=(Y+(y2-y1)*(i+1)*(m-i-1))%mod; y1=y2; } cout<<(X*Y)%mod<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pll pair <ll , ll> #define pb push_back #define pf push_front #define pob pop_back #define pof pop_front #define mp make_pair #define X first #define Y second #define LB(x) ((x) & -(x)) #define BIT(a , b) (((a)>>(b)) & 1) const ll maxn = 1e3 + 10; ll a[maxn]; int main() { ios_base :: sync_with_stdio(false); cin.tie(0); ll n , res = 0; cin >> n; for (ll i = 1; i <= 2 * n; i++) { cin >> a[i]; } sort(a + 1 , a + 2 * n + 1); for (ll i = 1; i <= n; i++) { res += a[2 * i - 1]; } cout << res; }
#include <iostream> #include <algorithm> signed main() { int n; std::cin >> n; n = 2*n; int l[n]; for(int i = 0; i < n; i++) std::cin >> l[i]; std::sort(l, l+n); int ans = 0; for(int i = n-2; i >= 0; i -= 2) ans += l[i]; std::cout << ans << std::endl; return 0; }
1
#include<iostream> using namespace std; int main() { string str; cin >> str; //0から4文字分 str.replace(0, 4, "2018"); printf("%s", str.c_str()); return 0; }
#include <stdio.h> int main (){ int year; int month; int date; int x; scanf ("%d/%d/%d", &year, &month, &date); x = year + 1; printf ("%d/%02d/%02d\n", x, month, date); return 0; }
1
#include <iostream> #include <algorithm> using namespace std; int main(){ int n,p; while(cin >> n >> p, n!=0 && p!=0){ int s[n]; int index = 0; int stone = p; for(int i=0;i<n;i++){ s[i] = 0; } while(true){ if(stone > 0){ s[index]++; stone--; }else if(stone == 0){ stone = s[index]; s[index] = 0; } if(s[index] == p) break; if(index == n-1){ index = 0; }else{ index++; } } cout << index << endl; } }
#include <iostream> #include <cmath> using std::cin; using std::cout; using std::endl; int main(){ int n; int p; int a[100]; int b[100]; int flag = 0; int i = 0; int pp; while(cin >> n >> p){ pp = p; for(int j = 0;j < n;j++){ a[j] = 0; } i = 0; flag = 0; if(n == 0 && p == 0){ break; } while(flag == 0){ if(p > 0){ a[i] += 1; p -= 1; }else{ p = a[i]; a[i] = 0; } if(a[i] == pp){ cout << i << endl; flag = 1; } i++; if(i == n){ i = 0; } } } }
1
#include <bits/stdc++.h> #define fi first #define se second #define pii pair<int,int> #define mp make_pair #define pb push_back #define space putchar(' ') #define enter putchar('\n') #define MAXN 10005 #define eps 1e-12 //#define ivorysi using namespace std; typedef long long int64; typedef unsigned int u32; typedef double db; template<class T> void read(T &res) { res = 0;T f = 1;char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); } res *= f; } template<class T> void out(T x) { if(x < 0) {x = -x;putchar('-');} if(x >= 10) { out(x / 10); } putchar('0' + x % 10); } int N,M; struct node { int to,next,val; }E[1005]; int sumE,head[25],pos[(1 << 15) + 5]; int f[16][(1 << 15) + 5],h[16][(1 << 15) + 5],sum[(1 << 15) + 5]; void add(int u,int v,int c) { E[++sumE].to = v; E[sumE].next = head[u]; E[sumE].val = c; head[u] = sumE; } inline int lowbit(int x) { return x & (-x); } void Init() { read(N);read(M); int u,v,c; for(int i = 1 ; i <= M ; ++i) { read(u);read(v);read(c); add(u,v,c);add(v,u,c); h[u][1 << v - 1] = c; h[v][1 << u - 1] = c; } for(int i = 1 ; i <= N ; ++i) { for(int j = 1 ; j < (1 << N) ; ++j) { if(lowbit(j) == j) continue; h[i][j] = h[i][j - lowbit(j)] + h[i][lowbit(j)]; } } for(int i = 1 ; i <= N ; ++i) pos[(1 << i - 1)] = i; for(int i = 1 ; i < (1 << N) ; ++i) { sum[i] = sum[i - lowbit(i)] + h[pos[lowbit(i)]][i - lowbit(i)]; } } void Solve() { for(int i = 1 ; i <= N ; ++i) { for(int j = 0 ; j < (1 << N) ; ++j) { f[i][j] = -1e9; } } f[1][1] = 0; for(int j = 0 ; j < (1 << N - 1) ; ++j) { for(int i = 1 ; i <= N ; ++i) { int S = j << 1 | 1; if(!(S & (1 << i - 1))) continue; int L = S ^ (1 << i - 1); for(int T = L; T ; T = (T - 1) & L) { if(f[i][S ^ T] >= 0) f[i][S] = max(f[i][S],f[i][S ^ T] + sum[T ^ (1 << i - 1)]); } if(f[i][S] >= 0) { for(int k = head[i] ; k ; k = E[k].next) { int v = E[k].to; if(!(S & (1 << v - 1))) { f[v][S ^ (1 << v - 1)] = max(f[v][S ^ (1 << v - 1)],f[i][S] + E[k].val); } } } } } out(sum[(1 << N) - 1] - f[N][(1 << N) - 1]);enter; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Init(); Solve(); }
#include <bits/stdc++.h> using namespace std; template <typename T> struct segtree_lazy { int n; vector<T> tree, lazy; T def = numeric_limits<T>::max(), none = -1; segtree_lazy(int n_) { for (n = 1; n < n_; n *= 2) { } tree.assign(2 * n, def); lazy.assign(2 * n, none); } T merge(T a, T b) { return max(a, b); } void set_lazy(int index, T x) { tree[index] = x; lazy[index] = x; } void push(int index) { if (lazy[index] == none) { return; } set_lazy(2 * index, tree[index]); set_lazy(2 * index + 1, tree[index]); lazy[index] = none; } void fix(int index) { tree[index] = merge(tree[2 * index], tree[2 * index + 1]); } void update(int a, int b, int index, int l, int r, T x) { if (r <= a || b <= l) { return; } if (a <= l && r <= b) { set_lazy(index, x); return; } push(index); update(a, b, 2 * index, l, (l + r) / 2, x); update(a, b, 2 * index + 1, (l + r) / 2, r, x); fix(index); } void update(int l, int r, T x) { update(l, r, 1, 0, n, x); } T query(int a, int b, int index, int l, int r) { if (r <= a || b <= l) { return none; } if (a <= l && r <= b) { return tree[index]; } push(index); T puni = query(a, b, 2 * index, l, (l + r) / 2); T muni = query(a, b, 2 * index + 1, (l + r) / 2, r); return merge(puni, muni); } T query(int l, int r) { return query(l, r, 1, 0, n); } }; int main() { int n, q; cin >> n >> q; segtree_lazy<int> seg(n); for (int i = 0; i < q; i++) { bool b; cin >> b; if (b) { int x; cin >> x; cout << seg.query(x, x + 1) << endl; } else { int s, t, x; cin >> s >> t >> x; seg.update(s, t + 1, x); } } return 0; }
0
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #pragma comment (linker, "/STACK:526000000") #include "bits/stdc++.h" using namespace std; typedef string::const_iterator State; #define eps 1e-8L #define MAX_MOD 1000000007LL #define GYAKU 500000004LL #define MOD 998244353LL #define pb push_back #define mp make_pair typedef long long ll; typedef long double ld; #define REP(a,b) for(long long (a) = 0;(a) < (b);++(a)) #define ALL(x) (x).begin(),(x).end() #define int ll int costs(int now) { for (int i = 0; i < 62; ++i) { if (now < (1 << i)) { return i + 1; } } } int secret_d = 10; int n; int interact(int a) { int ans = 0; cout << "? " << a << endl; if (true) { int r = 1; for (int i = 0; i < 60; ++i) { if ((secret_d & (1LL << i))) { ans += costs(r) * costs(a); r = r * a % n; } ans += costs(a) * costs(a); a = a * a % n; } } else { cin >> ans; } return ans; } void init() { iostream::sync_with_stdio(false); cout << fixed << setprecision(20); } long double comb(int a, int b) { ld ans = 1; for (int q = 0; q < b; ++q) { ans *= (ld)(a - q); ans /= (ld)(q + 1); } return ans; } void solve(){ int n; cin >> n; vector<int> inputs; int ans = 0; REP(i, n) { int a; cin >> a; inputs.push_back(a); } if (inputs[0] != 0) { cout << -1 << endl; return; } for (int q = 1; q < n; ++q) { if (inputs[q] > inputs[q - 1] + 1) { cout << -1 << endl; return; } if (inputs[q] == inputs[q - 1] + 1) { ans++; } else { ans += inputs[q]; } } cout << ans << endl; } #undef int int main() { init(); solve(); }
#include <bits/stdc++.h> #define itn int #define REP(i, n) for (ll i = 0; i < n; i++) #define IREP(i, n) for (ll i = n - 1; i >= 0; i--) #define FOR(i, a, b) for (ll i = a; i < b; i++) #define all(v) v.begin(), v.end() #define SENTINEL 2000000000 #define NIL -1 using namespace std; typedef long long ll; const ll MAX = 510000; const ll INF = 1LL << 60; const ll MOD = 1000000007; 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 main() { int n;cin >> n; vector<ll> a(n); REP(i,n){ cin >> a[i]; if(i==0){ if(a[i]!=0){ cout << -1 << endl; return 0; } } else{ if(a[i]-a[i-1]>=2){ cout << -1 << endl; return 0; } } } ll ans=0; REP(i,n){ if(i==0)continue; if(a[i]-a[i-1]==1){ ans++; } else{ ans+=a[i]; } } cout << ans << endl; }
1
#include <iostream> using namespace std; int main(){ long long n, a, b; cin >> n >> a >> b; long long x[n]; for(int i = 0; i < n; ++i) cin >> x[i]; long long dp[n+1]; dp[0] = 0; dp[1] = 0; for(int i =2; i <= n; ++i){ dp[i] = min(a*(x[i-1]-x[i-2]), b) + dp[i-1]; } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, ns, ne) for (int i = ns; i < ne; ++i) using namespace std; using ULL = unsigned long long; int main() { ULL N, a, b, west, east, chk, ans = 0; cin >> N >> a >> b >> west; rep(i, 1, N) { cin >> east; chk = (east - west) * a; if (chk > b) { ans += b; } else { ans += chk; } west = east; } cout << ans << endl; getchar(); }
1
#include <stdio.h> int e; int x, y, z; int m; int main(void) { while(1) { m = 1000000; y = 0; z = 0; scanf("%d", &e); if (e == 0) break; while (z * z * z <= e) { while (y * y <= e - z * z * z) { x = e - y * y - z * z * z; if (m > x + y + z) { m = x + y + z; } y++; } y = 0; z++; } printf("%d\n", m); } }
#include<iostream> #include<vector> using namespace std; int main(void){ int a,b,c,n; while(cin>>a>>b>>c,a+b+c){ cin>>n; vector<int> in(a+b+c+1,2); vector<vector<int> > jud(n,vector<int>(4,0)); for(int l = 0 ; l < n ; l ++){ int i , j , k , r; cin>>i>>j>>k>>r; if(r == 1){ in[i] = in[j] = in[k] = 1; } jud[l][0] = i ; jud[l][1] = j ; jud[l][2] = k ; jud[l][3] = r; } for(int i = 0 ; i < jud.size() ; i ++){ if(jud[i][3] == 0){ if(in[jud[i][0]] == 1 && in[jud[i][1]] == 1) in[jud[i][2]] = 0; if(in[jud[i][2]] == 1 && in[jud[i][0]] == 1) in[jud[i][1]] = 0; if(in[jud[i][2]] == 1 && in[jud[i][1]] == 1) in[jud[i][0]] = 0; } } for(int i = 1 ; i < in.size() ; i ++) cout<<in[i]<<endl; } return 0; }
0
#include <iostream> #include <algorithm> #include <cmath> #include <bitset> #include <cstring> #include <string.h> #include <sstream> #include <iomanip> #include <queue> #include <stack> #include <deque> #include <map> #include <set> #include <unordered_set> #include <unordered_map> #include <tuple> #include <vector> #include <stdio.h> #include <stdlib.h> using namespace std; #define F first #define S second #define siz(v) ((int)v.size()) #define rs(n) resize(n) #define ALL(v) v.begin(),v.end() #define reset(v) memset((v),0,sizeof(v)) #define EB emplace_back #define MP make_pair #define rep(i,n) for(int i=0;i<(n);i++) #define rep1(i,n) for(int i=1;i<=(n);i++) #define REP(i,a,b) for(int i=(a);i<=(b);i++) #define debug(x) cout << " > " << #x << ':' << x << endl; #define kiyohime ios::sync_with_stdio(false);cin.tie(0); #define endl '\n' using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; template<typename T> using vec = vector<T>; template<typename T> using Prior = priority_queue<T>; template<typename T> using prior = priority_queue<T, vec<T>, greater<T>>; const int INF = 1e9; const ll LLINF = (ll)4*1e18; const ll MOD = 1e9+7; const double PI = 3.14159265358; const double EPS = 1e-8; const int xx[8] = {0,1,0,-1,1,1,-1,-1}; const int yy[8] = {1,0,-1,0,1,-1,-1,1}; void pmod(ll &a, ll b) {a = (a+b)%MOD;} void mmod(ll &a, ll b) {a = (a-b+MOD)%MOD;} void tmod(ll &a, ll b) {a = (a*b)%MOD;} ll POW(ll a, ll b) {ll res=1; do{if(b%2)tmod(res,a);tmod(a,a);}while(b>>=1); return res;} template<typename T> void amax(T &a, T b) {if(a < b) a = b;} template<typename T> void amin(T &a, T b) {if(a > b) a = b;} #define N 20 #define lowbit(x) (x&(-x)) void solve(){ int n; ll a[N][N], dp[(1<<16) + 10] = {}; cin >> n ; rep(i, n) rep(j, n) cin >> a[i][j]; rep1(i, (1<<n)-1){ rep(j, n) REP(u, j+1, n-1) if((i & (1<<j)) && (i & (1<<u))) dp[i] += a[j][u]; int tmp = (i-1) & i; while(tmp){ amax(dp[i], dp[i-tmp] + dp[tmp]); tmp = (tmp - 1) & i; } } cout << dp[(1<<n) - 1] << endl; } int main(){ // kiyohime int T = 1; // cin >> T; while(T--){ solve(); } }
#include <bits/stdc++.h> #include <boost/range/algorithm.hpp> #include <boost/range/numeric.hpp> #include <boost/range/irange.hpp> #include <boost/range/adaptor/indexed.hpp> using namespace std; using namespace boost::adaptors; using namespace std::string_literals; using ll = int64_t; using vecint = vector<int>; using vecll = vector<ll>; using boost::irange; int main() { int n,m,q; cin>>n>>m>>q; vector<string> t(n+2); for(int i:irange(0,n)) { string s; cin>>s; t[i+1] = "0" + s + "0"; } t[0] = t[n+1] = string(m+2, '0'); vector<vecint> connects_v(n, vecint(m+1)); vector<vecint> connects_h(n+1, vecint(m)); for(int i:irange(0,n)) { for (int j:irange(0,m+1)) { bool left = t[i+1][j] == '1'; bool right = t[i+1][j+1] == '1'; if (left && right) { connects_v[i][j] = 1; } } } for (int i:irange(0,n+1)) { for (int j:irange(0,m)) { bool top = t[i][j+1] == '1'; bool bottom = t[i+1][j+1] == '1'; if (top && bottom) { connects_h[i][j] = 1; } } } vector<vecint> psumcv(n+1, vecint(m+2)); vector<vecint> psumch(n+2, vecint(m+1)); for(int i:irange(0,n)) { for (int j:irange(0,m+1)) { psumcv[i+1][j+1] = psumcv[i][j+1] + connects_v[i][j]; } } for (int i:irange(0,n+1)) { for (int j:irange(0,m+1)) { psumcv[i][j+1] += psumcv[i][j]; } } for (int i:irange(0,n+1)) { for (int j:irange(0,m)) { psumch[i+1][j+1] = psumch[i+1][j] + connects_h[i][j]; } } for (int i:irange(0,n+1)) { for (int j:irange(0,m+1)) { psumch[i+1][j] += psumch[i][j]; } } vector<vecint> surfs(n+1, vecint(m+1)); for (int i:irange(0,n)) { for (int j:irange(0,m)) { if (t[i+1][j+1] == '1') { surfs[i+1][j+1] = 1; } } } for (int i:irange(0,n+1)) { for (int j:irange(0,m)) { surfs[i][j+1] += surfs[i][j]; } } for (int i:irange(0,n)) { for (int j:irange(0,m+1)) { surfs[i+1][j] += surfs[i][j]; } } for(int query : irange(0,q)) { int x1,y1,x2,y2; cin>>y1>>x1>>y2>>x2; int connects = 0; connects += psumcv[y2][x2] - psumcv[y1-1][x2] - psumcv[y2][x1] + psumcv[y1-1][x1]; connects += psumch[y2][x2] - psumch[y2][x1-1] - psumch[y1][x2] + psumch[y1][x1-1]; --y1;--x1; int s = surfs[y2][x2] - surfs[y2][x1] - surfs[y1][x2] + surfs[y1][x1]; int res = s - connects; cout<<res<<endl; } return 0; }
0
#include <bits/stdc++.h> #define FOR(i, begin, end) for(int i=(begin);i<(end);i++) #define REP(i, n) FOR(i,0,n) #define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--) #define IREP(i, n) IFOR(i,0,n) #define SORT(a) sort(a.begin(), a.end()) #define REVERSE(a) reverse(a.begin(), a.end()) #define int long long #define INF 1000000000000000 using namespace std; typedef vector<int> vec; typedef vector<vec> mat; typedef pair<int, int> Pii; template<typename T> void readvec(vector<T> &a); void readindex(vector<int> &a); int extgcd(int a, int b, int &x, int &y){ int d = a; if(b != 0){ d = extgcd(b, a % b, y, x); y -= (a / b) * x; }else{ x = 1; y = 0; } return d; } int gcd_multi(vec a){ int x, y, g; g = a[0]; FOR(i, 1, a.size()) g = extgcd(g, a[i], x, y); return g; } signed main(){ int N; cin >> N; vec A(N); readvec(A); int odd = 0, oddi; REP(i, N){ if(A[i] % 2 == 1){ odd++; oddi = i; } } if((N - odd) % 2 == 1){ cout << "First"; return 0; } if(!(odd == 1 && A[oddi] > 1)){ cout << "Second"; return 0; } bool f = true; while(true){ A[oddi]--; int g = gcd_multi(A); REP(i, N) A[i] /= g; odd = 0, oddi; REP(i, N){ if(A[i] % 2 == 1){ odd++; oddi = i; } } if((N - odd) % 2 == 1){ f = !f; break; } if(!(odd == 1 && A[oddi] > 1)) break; f = !f; } if(f){ cout << "First"; }else{ cout << "Second"; } return 0; } template<typename T> void readvec(vector<T> &a){ REP(i, a.size()){ cin >> a[i]; } } void readindex(vector<int> &a){ REP(i, a.size()){ cin >> a[i]; a[i]--; } }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <string> #include <tuple> #include <utility> #include <vector> using namespace std; using ll = long long; int main() { int x; cin >> x; int ans = 0; ans += (x / 500) * 1000; x %= 500; ans += (x / 5) * 5; cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define ll long long int #define pb push_back typedef pair<ll,ll> ipair; #define F first #define S second /* //functions from gfg long long int power(long long int x,long long int y,long long int p) { long long int res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) {//d=(long double)x/p; res = (res*x) % p; } // y must be even now y = y>>1; // y = y/2 x=(x*x)%p; } return res%p; } long long int gcd(long long int a,long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int binomialCoeff(long long int n, ll k) { ll res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] for (ll i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } void sieve(int n, vector<ll> &primes) { vector<int> prime(n,1); ll p; for ( p=2; p*p<=n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == 1) { for (int i=p*p; i<=n; i += p) prime[i] = 0; } } for(p=2;p<=n;p++) { if(prime[p]) primes.pb(p); } } void dfs(vector<ll> adj[] ,ll src,vector<ll> &vis) { vis[src]=1; for(ll i=0;i<adj[src].size();i++) { if(vis[adj[src][i]]==0) dfs(adj,adj[src][i],vis) } } */ int main() { ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); ll n,i,j,p,k,m,t,ans=0; cin>>n>>t; ll dp[3005][3005]; memset(dp, 0, sizeof(dp)); vector<ll> a(n),b(n); vector<pair<ll,ll> > val(n); for(i=0;i<n;i++) { cin>>a[i]>>b[i]; val[i].F=a[i]; val[i].S=b[i]; } sort(val.begin(),val.end()); for(i=0;i<n;i++) { a[i]=val[i].F; b[i]=val[i].S; } for(i=0;i<t;i++) { for(j=0;j<n;j++) { if(i+a[j]<t) { dp[j+1][i+a[j]] = max(dp[j][i]+b[j],dp[j+1][i+a[j]]); } dp[j+1][i] = max(dp[j][i],dp[j+1][i]); } } for(i=0;i<n;i++) { ans=max(ans,dp[i][t-1]+b[i]); } cout<<ans<<endl; // for(i=0;i<n;i++) // { // cout<<a[i]<<" "; // } // cout<<endl; return 0; }
#include<iostream> using namespace std; int angka [10], brpkali, total; int main (){ for (int i=1; i<=3; i++){ cin>>angka[i]; } cin>>brpkali; for (int i=1; i<=3; i++){ for (int j=1; j<=3; j++){ if (j==i){ }else if (angka [i]>angka [j]){ //sort; swap(angka [i], angka [j]); } } } for (int i=1; i<=brpkali; i++){ //multipy big num angka [1]*=2; } for (int i=1; i<=3; i++){ //sum total total+=angka [i]; } cout<<total<<endl; }
0
#include<cstdio> #include<algorithm> using namespace std; long long K; long long po[20], C[20], res; void Do(int b, int e, long long S, long long c) { if (!c)return; if (b >= e) { if (S == K) { if (b == e)res += 10*c; else res+=c; } return; } if (abs(S - K) > po[e + 1])return; int i; for (i = -9; i <= 9; i++) { int t = 10 - abs(i); if (i >= 0 && b==0)t--; Do(b+1, e-1, S+i*C[b], t*c); } } void Calc(int L) { int i; for (i = 0; i < L; i++) { C[i] = po[L - i - 1] - po[i]; } Do(0, L - 1, 0, 1); } int main() { int i; po[0] = 1; for (i = 1; i <= 18; i++)po[i] = po[i - 1] * 10; scanf("%lld", &K); for (i = 2; i <= 18; i++) { Calc(i); } printf("%lld\n", res); }
#include <bits/stdc++.h> #include <unordered_map> #include <unordered_set> #define pb push_back #define mpr make_pair #define pii pair<int, int> #define ll long long #define ld long double #define all(arr) arr.begin(), arr.end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define fo(i, l, r) for (int i = l; i <= r; i++) #define MOD 1000000007 #define mod 998244353 #define pie 3.14159265358979323846264338327950L #define N 200009 #define M 2000009 #define mid(l, r) l + (r - l) / 2 #define vec vector<int> #define umap unordered_map<int, int> #define yes cout << "Yes" << endl; #define no cout << "No" << endl; #define endl "\n" #define int long long using namespace std; int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1}; int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1}; ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);} ll lcm(ll a, ll b) { return (a*b)/ gcd(a,b);} vector<pair<int,int> > arr(10); map<pair<int,int>,ld> mp; ld ans = 0; int n; void fun(vector<int> temp, ld curr) { if(temp.size() == n) { ans+= curr; // for(auto it : temp) { // cout<<it<<" "; // } // cout<<endl; return; } for(int i=0; i<n; i++) { if(find(all(temp),i) == temp.end()) { int prev = temp.back(); temp.push_back(i); fun(temp,curr + mp[mpr(i,prev)]); temp.pop_back(); } } } const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } void test_case() { cin>>n; COMinit(); rep(i,n) { int x,y; cin>>x>>y; arr[i] = mpr(x,y); } for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { ld d= pow(arr[i].first - arr[j].first,2) + pow(arr[i].second -arr[j].second,2); d = sqrt(d); // cout<<d<<endl; mp[mpr(i,j)] = d; mp[mpr(j,i)] = d; } } vec temp; for(int i=0; i<n; i++) { temp.push_back(i); fun(temp,0.0); temp.pop_back(); } ld fin = ans/fac[n]; // cout<<ans<<endl; cout<<fin<<endl; } signed main () { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(20); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int t = 1; // cin >> t; while (t--) { test_case(); } }
0
#include <iostream> #include <string> #include <algorithm> #include <cmath> #include <deque> #include <queue> #include <stack> #include <vector> #include <utility> #include <iomanip> #include <bitset> #include <set> #include <map> #include <stdio.h> #include <numeric> #include <cstring> #define rep(i,n) for (int i = 0; i < (n); i++) #define rep1(i,n) for (int i = 1; i < (n); i++) #define FOR(i,a,b) for (int i=(a); i < (b); i++) #define MOD 1000000007 //10^9+7 #define ALL(a) (a).begin(),(a).end() using namespace std; using ll = long long; using PII = pair<int, int>; using PLL = pair<long long, long long>; const int INF = numeric_limits<int>::max(); constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); } // 負の数にも対応した % 演算 long long mod(long long val, long long m) { long long res = val % m; if (res < 0) res += m; return res; } //greatest common divisor long long gcd(ll a, ll b) { if (a % b == 0) { return b; } else { return gcd(b, a % b); } } //least common multiple long long lcm(ll a, ll b) { return a / gcd(a, b) * b ; } bool is_prime(ll n) { for (ll i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return n != 1; } vector<vector<int>> d(10, vector<int>(10, 1000000)); void warshall_floyd(int n) { rep(k,n) rep(i,n) { rep(j,n) { d[i][j] = min(d[i][j], d[i][k]+d[k][j]); } } } int main() { ll n; cin >> n; vector<ll> a; int ans = 20; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { a.push_back(i); a.push_back(n/i); } } for (int i = 0; i < a.size(); i += 2) { int tmp1 = to_string(a[i]).length(); int tmp2 = to_string(a[i+1]).length(); ans = min(ans, max(tmp1, tmp2)); } cout << ans << endl; return 0; }
#include<iostream> #include<algorithm> #include<cmath> using namespace std; typedef long long ll; int main() { ll n,m,t,i,res=0; cin >> n; m=(ll)sqrt(n); for(i=m;i>0;i--) if(n%i==0){ t=n/i; while(t>0){ t/=10; res++; } cout << res << endl; return 0; } return 0; }
1
#include <bits/stdc++.h> #define debug(...) fprintf(stderr, __VA_ARGS__) #define pi std::pair<int, int> #define mkp std::make_pair #define ll long long #define pb push_back #define fi first #define se second //{{{IO namespace IO{ template <class I> void read(I &x){ x = 0; char ch = getchar(); bool f = false; for(; !isdigit(ch); ch = getchar()) f |= (ch == '-'); for(; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + ch - '0'; return ; } int read_int(){ int x; return read(x), x;} ll read_ll(){ ll x; return read(x), x;} template <class I> void chmax(I &x, I y){x = x > y ? x : y;} template <class I> void chmin(I &x, I y){x = x < y ? x : y;} #define ri read_int() #define rl read_ll() } using IO::read_int; using IO::read_ll; using IO::chmax; using IO::chmin; //}}} //{{{Mod namespace Mod{ // const int mod = 1e9 + 7; // const int mod = 1004535809; const int mod = 998244353; inline void MOD(int &x){ x -= x >= mod ? mod : 0;} inline void add(int &x, int y){ x = x + y >= mod ? x + y - mod : x + y;} inline int Add(int x, int y){ return x + y >= mod ? x + y - mod : x + y;} inline void mul(int &x, int y){ x = 1ll * x * y % mod;} inline int Mul(int x, int y){ return 1ll * x * y % mod;} } //}}} #define N 100005 int n, s, cnt; int x[N], num[N]; ll a[N]; void solve(int l, int r){ if(l > r) return ; if(l == r) return num[++cnt] = l, void(); if(x[l] > s){ while(r >= l) num[++cnt] = r--; return ; } if(x[r] < s){ while(r >= l) num[++cnt] = l++; return ; } if(a[l] >= a[r]){ num[++cnt] = r; a[l] += a[r]; solve(l, r - 1); } else{ num[++cnt] = l; a[r] += a[l]; solve(l + 1, r); } return ; } int main(){ n = ri; s = ri; for(int i = 1; i <= n; ++i) x[i] = ri, a[i] = ri; solve(1, n); ll res = 0; // for(int i = cnt; i >= 1; --i) // debug("%d ", num[i]); for(int i = cnt; i >= 1; --i) res += abs(x[num[i]] - s), s = x[num[i]]; printf("%lld\n", res); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main() { int n, m; cin >> n >> m; vector<int> ac(n, 0); vector<int> wa(n, 0); rep(i, m) { int p; string s; cin >> p >> s; p--; if (ac[p] == 1) continue; if (s == "AC") ac[p] = 1; else wa[p]++; } rep(i, n) wa[i] *= ac[i]; cout << accumulate(ac.begin(), ac.end(), 0) << ' ' << accumulate(wa.begin(), wa.end(), 0) << endl; return 0; }
0
#include <iostream> #include <vector> #include <utility> #include <queue> #include <stdio.h> #include <math.h> #include <algorithm> #define llint long long #define mod 1000000007 #define inf 1e18 using namespace std; typedef pair<double, llint> P; typedef pair<llint, P> E; llint n; llint a, b, c, d; llint dp[1005][1005]; const int FACT_MAX = 2005; llint fact[FACT_MAX], fact_inv[FACT_MAX]; llint modpow(llint a, llint n) { if(n == 0) return 1; if(n % 2){ return ((a%mod) * (modpow(a, n-1)%mod)) % mod; } else{ return modpow((a*a)%mod, n/2) % mod; } } void make_fact() { llint val = 1; fact[0] = 1; for(int i = 1; i < FACT_MAX; i++){ val *= i; val %= mod; fact[i] = val; } fact_inv[FACT_MAX-1] = modpow(fact[FACT_MAX-1], mod-2); for(int i = FACT_MAX-2; i >= 0; i--){ fact_inv[i] = fact_inv[i+1] * (i+1) % mod; } } llint comb(llint n, llint k) { llint ret = 1; ret *= fact[n]; ret *= fact_inv[k], ret %= mod; ret *= fact_inv[n-k], ret %= mod; return ret; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); make_fact(); cin >> n; cin >> a >> b >> c >> d; dp[a-1][0] = 1; for(int i = a-1; i < b; i++){ for(int j = 0; j <= n; j++){ dp[i+1][j] += dp[i][j], dp[i+1][j] %= mod; llint mul = 1; for(int k = 1; k <= d && j+k*(i+1) <= n; k++){ mul *= comb(n-j-(k-1)*(i+1), i+1), mul %= mod; if(k >= c){ (dp[i+1][j+k*(i+1)] += dp[i][j] * mul % mod * fact_inv[k] % mod) %= mod; } } } } cout << dp[b][n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; ll dp[1010]; ll F[1010], FI[1010]; ll n, a, b, c, d; ll pow(ll a, ll b) { return b? pow(a * a % mod, b >> 1) * (b & 1? a : 1) % mod : 1; } int main() { ll i, j, k; scanf("%lld%lld%lld%lld%lld", &n, &a, &b, &c, &d); F[0] = FI[0] = 1; for(i=1; i<=n; i++){ F[i] = F[i - 1] * i % mod; FI[i] = pow(F[i], mod - 2); } dp[0] = 1; for(i=a; i<=b; i++){ for(j=n; j>=0; j--){ for(k=c; k<=d && j-k*i>=0; k++){ dp[j] = (dp[j] + dp[j - k * i] * pow(FI[i], k) % mod * FI[k]) % mod; } } } printf("%lld\n", dp[n] * F[n] % mod); return 0; } ///////////////////////////////////////////////////////////////////////////////
1
/*solved by RAKIB JOY*/ #include<stdio.h> #include<stdbool.h> bool dislike[10]; int main() { int n,k,a; scanf("%d%d",&n,&k); while(k--) { scanf("%d",&a); dislike[a]=true; } for(int i=n;;i++) { int maybe=i; bool ok=true; while(maybe) { int last_digit=maybe%10; if(dislike[last_digit]) { ok=false; break; } maybe/=10; } if(ok) { printf("%d\n",i); return 0; } } return 0; }
#include<bits/stdc++.h> #include<unordered_set> #include<unordered_map> using namespace std; #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef long double ld; const ll inf=1e9+7; const ll mod=998244353; int main(){ ll n,m;cin>>n>>m; vector<ll>par(n+1); vector<ll>dim(n+1); vector<vector<ll> >G(n+1); rep(i,n+m-1){ ll a,b;cin>>a>>b; dim[b]++; G[a].pb(b); } queue<ll>q; ll p; REP(i,1,n+1){ if(dim[i]==0)p=i; } q.push(p); while(!q.empty()){ ll now=q.front(); q.pop(); for(auto e:G[now]){ dim[e]--; if(dim[e]==0){ par[e]=now; q.push(e); } } } REP(i,1,n+1){ cout<<par[i]<<endl; } }
0
#include <bits/stdc++.h> #include <string> #include <algorithm> using namespace std; //std::の省略のため using ll = long long; int main() { string s[3]; for (int i = 0; i < 3; i++) cin >> s[i]; int count = 0; for (;;) { char tmp = s[count][0]; //cout << tmp << " " << s[count] << endl; if (s[count].size() == 0) break; if (tmp == 'a') { s[count].erase(0, 1); count = 0; } else if (tmp == 'b') { s[count].erase(0, 1); count = 1; } else if (tmp == 'c') { s[count].erase(0, 1); count = 2; } } char ans; for (int i = 0; i < 3; i++) { if (count == i) { ans = 'A' + i; cout << ans << endl; break; } } }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define FOR(i,x,n) for(int i=x; i<(n); i++) #define ALL(n) begin(n),end(n) #define MOD 1000000007 #define INF 1e9 #define INFL 1e12 typedef long long ll; typedef unsigned int ui; typedef unsigned long long ull; template<class T>void pr(T x){cout << x << endl;} template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} cout << a[a.size()-1] << endl;} template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } typedef pair<int, bool> edge; int v, e; vector<vector<edge>> G; vector<int> ord, low; set<int> ans; vector<bool> seen; int k; int zero_degree; void dfs(int u){ // pr(u); seen[u] = true; ord[u] = k; k++; low[u] = ord[u]; for(edge &ed: G[u]){ int v = ed.first; if(!seen[v]){ ed.second = true; dfs(v); chmin(low[u], low[v]); if(u!=0 && ord[u] <= low[v] && ans.find(u)==ans.end()){ // printf("(%d %d) is pushed\n", u, v); ans.insert(u); } // printf("(%d: %d %d) ",u, ord[u], low[u]); // printf("(%d %d)", ord[u], low[u]); }else if(ed.second==false){ // if(ord[u]==low[v]) continue; chmin(low[u], ord[v]); } } // printf("(%d: %d %d) ",u, ord[u], low[u]); } int main() { cin >> v >> e; // cout << v << " " << e << endl; G.resize(v); ord.resize(v), low.resize(v); seen.resize(v); int a, b; rep(i, e){ cin >> a >> b; G[a].push_back({b, false}); G[b].push_back({a, false}); } // cout << G.size(); // rep(i, v){ // for(auto ed: G[i]){ // printf("(%d %d) ", i, ed.first); // } // printf("\n"); // } rep(i, v) seen[i]=false; int k = 0; dfs(0); // cout << endl; // rep(i, v){ // printf("(%d: %d %d) ",i, ord[i], low[i]); // } // cout << ans.size() << endl; int tmp=0; // printf("\n"); for(edge ed: G[0]){ // printf("%d\n", ed.second); if(ed.second) tmp++; } if(tmp>1) ans.insert(0); for(int i: ans){ pr(i); } return 0; }
0
#include <iostream> #include<vector> #include<cstring> using namespace std; int n, m; vector <int> g[15], c[15]; void add_edge(int u, int v, int w) { g[u].push_back(v); c[u].push_back(w); } int dp[1 << 15][15]; int prep[1 << 15][15]; void upd(int &x, int y) { if (y < x) x = y; } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; ++ i) { int u, v, w; scanf("%d %d %d", &u, &v, &w); -- u; -- v; add_edge(u, v, w); add_edge(v, u, w); } memset(dp, 0x3f, sizeof(dp)); int full = (1 << n) - 1; for (int msk = 1; msk < (1 << n); ++ msk) { for (int i = 0; i < n; ++ i) if (!(msk >> i & 1)) { for (int e = 0; e < g[i].size(); ++ e) { if (msk >> g[i][e] & 1) prep[msk][i] += c[i][e]; } } } dp[1][0] = 0; for (int msk = 1; msk < (1 << n); ++ msk) { for (int u = 0; u < n; ++ u) if (msk >> u & 1) { for (int e = 0; e < g[u].size(); ++ e) { int v = g[u][e]; if (!(msk >> v & 1)) upd(dp[msk | 1 << v][v], dp[msk][u] + prep[msk ^ 1 << u][v]); } for (int s = full ^ msk; s; s = (s - 1) & (full ^ msk)) { int cur = 0; for (int i = 0; i < n; ++ i) if (s >> i & 1) cur += prep[msk ^ 1 << u][i]; upd(dp[msk | s][u], dp[msk][u] + cur); } } } printf("%d\n", dp[full][n - 1]); return 0; }
#include<iostream> #include<iomanip> #include<vector> #include<string> #include<cstring> #include<map> #include<set> #include<queue> #include<stack> #include<algorithm> #include<cmath> #include<cstdio> #include<cstdlib> #include<climits> #include<fstream> #define MOD (1000000007) using namespace std; typedef long long int Int; constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n-1); } set<pair<int,pair<int,int> > > query; //time, q, x. q=0:delete or 1: insert or 2:query Int N,Q; int main(void) { cin>>N>>Q; for(int i = 0;i < N;i++) { int s,t,x; cin>>s>>t>>x; query.insert({s-x, make_pair(1, x)}); query.insert({t-x, make_pair(0, x)}); } for(int i = 0;i < Q;i++) { int d; cin>>d; query.insert({d, make_pair(2, i)}); } vector<int> D(Q); multiset<int> se; for(auto it: query) { int time = it.first; int q = it.second.first; int x = it.second.second; if(q == 0) { se.erase(se.find(x)); } else if(q == 1) { se.insert(x); } else { if(se.empty()) { D[x] = -1; } else { D[x] = *se.begin(); } } } for(int i = 0;i < Q;i++) { cout<<D[i]<<endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; #define dbg(a) cerr << __LINE__ << ": " << #a << " = " << a << '\n' template<class T> inline istream& operator>>(istream& str, vector<T> &a) { for (auto &i : a) str >> i; return str; } typedef long long ll; int main(){ ios::sync_with_stdio(0), cin.tie(0); int n, x; cin >> n >> x; vector<int> a(n); cin >> a; ll ans = max(0, a[0]-x); for (int i = 0; i < n-1; ++i){ ll now = max (min(a[i]+a[i+1]-x, a[i+1]), 0); ans += now; a[i+1] -= now; } cout << ans << "\n"; return 0; }
#include<iostream> #include<cmath> #include<algorithm> #include<string> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<iomanip> #include<bitset> using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef pair<ll,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define fi first #define sc second #define rep(i,x) for(ll i=0;i<x;i++) #define repn(i,x) for(ll i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) const int MAX = 510000; const int MOD = 1000000007; int main(){ ll N; cin >> N; vector<ll> a(N); vector<ll> b(N); rep(i,N) cin >> a[i] >> b[i]; ll ans = 0; ll hu = 0, k = 0; rep(i,N) hu += a[i] * b[i]; rep(i,N) k += b[i]; ans = (k - 1) + (hu - 1) / 9; cout << ans << endl; }
0
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int main() { ll s; cin >> s; int cnt = 0; ll S = s; while(S > 0){ S /= 10; cnt++; } const int f = 753; int ans = 1000; for(int i = 3; i <= cnt; i++){ int x = s/pow(10,cnt -i); ans = min(abs(x-f), ans); ll p = pow(10,cnt+2-i); s %= p; } cout << ans << endl; }
#include <bits/stdc++.h> #include <math.h> #define _GLIBCXX_DEBUG using namespace std; int main() { string S; cin>>S; int ans=100000; for(int i=0;i<=S.size()-3;i++){ string res; res[0]=S[i]; res[1]=S[i+1]; res[2]=S[i+2]; int X=stoi(res); ans=min(abs(X-753),ans); } cout<<ans<<endl; }
1
/* * src.cpp * * Created on: 2016/08/04 * Author: joi */ #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,a) FOR(i,0,a) int N; int cnt[10]; int main(){ do{ cin>>N; if (N){ fill(cnt,cnt+10,0); REP(i,N){ int a; cin>>a; cnt[a]++; } REP(i,10){ if (cnt[i]==0){ cout<<'-'<<endl; }else{ REP(j,cnt[i]){ cout<<'*'; } cout<<endl; } } } }while (N); return 0; }
#include <stdio.h> int main(void) { int a[10]={0}; int n,i,j,k,l,x; for(j=0;j<20;j++) { //printf("?????°???"); for(l=0;l<10;l++) a[l]=0; scanf("%d",&n); if(n==0) break; else { for(i=0;i<n;i++) { scanf("%d",&x); a[x]=a[x]+1; } } for(k=0;k<10;k++) { if(a[k]) { for(l=0;l<a[k];l++) { printf("*"); } printf("\n"); } else printf("-\n"); } } return 0; }
1
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <complex> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> using namespace std; typedef pair<int,int> P; typedef long long ll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 2000000000 #define EQ(a,b) (abs((a)-(b))<EPS) int main() { ll u[4010]; bool flag; vector<int> v; while(1) { for(int i=0;i<4010;i++) { u[i]=0; } flag=true; int n; cin >> n; if(n==0)break; v.clear(); for(int i=0;i<n;i++) { int x; ll y,z; cin >> x >> y >> z; if(!(u[x])) { v.pb(x); } u[x]+=y*z; } for(int i=0;i<v.size();i++) { if(u[v[i]]>=1000000) { flag=false; cout << v[i] << endl; } } if(flag) { cout << "NA" << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; struct Graph { int n; int time; vector<vector<int>> g; vector<int> num, low; vector<vector<int>> scc; stack<int, deque<int>> S; vector<bool> inS; Graph(int n) : n(n), g(n), num(n, -1), low(n, -1), inS(n) {} void addEdge(int u, int v) { g[u].push_back(v); } void dfs(int u) { num[u] = low[u] = time++; S.push(u); inS[u] = true; for (auto&& v : g[u]) { if (num[v] == -1) { dfs(v); low[u] = min(low[u], low[v]); } else if (inS[v]) { low[u] = min(low[u], num[v]); } } if (low[u] == num[u]) { scc.push_back({}); while (true) { int v = S.top(); S.pop(); inS[v] = false; scc.back().push_back(v); if (u == v) break; } } } void stronglyConnectedComponents() { for (int i = 0; i < n; ++i) { if (num[i] == -1) dfs(i); } } }; int main() { int n, m; cin >> n >> m; Graph g(n); while (m--) { int s, t; cin >> s >> t; g.addEdge(s, t); } g.stronglyConnectedComponents(); vector<int> id(n); for (int i = 0; i < g.scc.size(); ++i) { for (auto&& x : g.scc[i]) id[x] = i; } int q; cin >> q; while (q--) { int u, v; cin >> u >> v; cout << (id[u] == id[v]) << endl; } return 0; }
0
#pragma GCC optimize("O2") #include<bits/stdc++.h> #define ll long long #define mod 1000000007LL #define eps 1e-8 using namespace std; string a3[3]={"aa.","..b","..b"}; string a4[4]={"aabc","ddbc","efgg","efhh"}; string a5[5]={"aabbc","dde.c","ffegg","..hij","..hij"}; string a6[6]={"aabbcc","ddeeff","ghi...","ghi...","...jkl","...jkl"}; string a7[7]={"aabbcc.","ddeef..","gg..fhh","..ij..k","..ij..k","....lmn","....lmn"}; string *ap[8]={NULL,NULL,NULL,a3,a4,a5,a6,a7}; string a[1005]; int main() { cin.tie(0),ios::sync_with_stdio(0); int n; cin>>n; if(n<3) cout<<"-1\n"; else if(n==3){ for(int i=0;i<3;i++) cout<<a3[i]<<"\n"; cout<<"\n"; } else{ fill(a,a+n,string(n,'.')); while(n>=8){ for(int i=0;i<4;i++){ for(int j=0;j<4;j++) a[i+n-4][j+n-4]=a4[i][j]; } n-=4; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++) a[i][j]=ap[n][i][j]; } n=a[0].size(); for(int i=0;i<n;i++) cout<<a[i]<<"\n"; } return 0; }
#include<iostream> #include<vector> #include<algorithm> #include<cstdio> #include<map> using namespace std; int main(){ for(;;){ vector<int> lot(5); int ans = 0; if(scanf("%d",&lot[0]) == EOF) break; for(int i=1; i<5; i++) scanf(",%d",&lot[i]); sort(lot.begin(),lot.end()); map<int,int> card; for(int i=0; i<5; i++) card[lot[i]]++; for(map<int,int>::iterator it = card.begin(); it!=card.end(); it++){ if(it->second == 2) ans+=2; if(it->second == 3) ans+=3; if(it->second == 4) ans+=6; } if(ans == 2) cout <<"one pair"<<endl; if(ans == 3) cout <<"three card"<<endl; if(ans == 4) cout <<"two pair"<<endl; if(ans == 5) cout <<"full house"<<endl; if(ans == 6) cout <<"four card"<<endl; if(lot[0]+1 == lot[1] && lot[1]+1 == lot[2] && lot[2]+1 == lot[3] && lot[3]+1 == lot[4]) cout <<"straight"<<endl; else if(lot[0] == 1 && lot[1] == 10 && lot[2] == 11 && lot[3] == 12 && lot[4] == 13) cout <<"straight"<<endl; else if(ans == 0) cout<<"null"<<endl; } return 0; }
0
#include <bits/stdc++.h> using intl = long long; using itnl = long long;//typo用 using uintl = unsigned long long; using itn = int;//typo用 using ld = long double; using namespace std; #define rep(i, n) for(intl i = 0; i < (intl)(n); i++) #define rrep(i, n) for(intl i = (intl)(n) - 1; i >= 0; i--) #define repi(i, a, b) for(intl i = (intl)(a); i < (intl)(b); i++) #define rrepi(i, a, b) for(intl i = (intl)(a) - 1; i >= (intl)(b); i--) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define m0(x) memset(x,0,sizeof(x)) #define m1(x) memset(x,1,sizeof(x)) #define fill(x,y) memset(x,y,sizeof(x)) #define alength(a) (sizeof(a) / sizeof(a[0])) #define debug(x) cout << #x << ":" << x << endl #define rdebug(i,x) rep(i, alength(x))cout << #x << "[" << i << "]:" << x[i] << endl//1次元配列の出力 #define rrdebug(i, j, x) rep(i, alength(x))rep(j, alength(x[i]))cout << #x << "[" << i << "][" << j << "]:" << x[i][j] << endl//二次元配列の出力 #define pb push_back #define mp make_pair #define pii pair<intl,intl> const intl INF = 1e9; const intl MOD = 1e9+7; const ld EPS = 1.0e-14;//-9 const ld PI = acos(-1); void fp(bool f){cout << (f ? "Yes" : "No") << endl;} void fp(bool f, string s, string t){cout << (f ? s : t) << endl;} template<class T> inline bool chmin(T& a, T b) { if (a > b) {a = b; return true;} return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) {a = b; return true;} return false; } intl gcd(intl a, intl b){ if(!b)return a; return gcd(b, a % b); } intl lcm(intl a,intl b){ return a / gcd(a, b) * b; } intl kai(intl k){//k!を求める関数, intl a = 1; for(int i = 2; i <= k; i++){ a *= i; } return a; } intl digit10(intl a){//aを10進数で表したときの桁数を求める関数 intl b = 0; do{ a /= 10; b++; }while(a); return b; } signed main(){ cout << fixed << setprecision(10); string s; cin >> s; intl len = s.length(); if(len < 26){ bool f[26]; m0(f); rep(i,len)f[s[i] - 'a'] = true; rep(i,26){ if(!f[i]){ s += (char)(i + 'a'); break; } } } else{ vector<char> v; rep(i,len)v.pb(s[i]); next_permutation(all(v)); string t = ""; rep(i,26){ t += v[i]; if(s[i] != v[i])break; } s = t; if(s == "a")s = "-1"; } cout << s << endl; return 0; }
#include <iostream> #include <utility> #include <map> #include <vector> #include <algorithm> #include <queue> #include <math.h> #include <stack> #include <set> #include <deque> #include <cmath> using namespace std; typedef long long ll; ll mod = 1e9+7; //ll mod = 998244353; #define rep(i,n) for(int i = 0; i < (n); ++i) int main() { string s; cin >> s; int n = s.size(); if (n != 26) { vector<bool> flg(26, false); rep(i, n) flg[s[i]-'a'] = true; cout << s; rep(i, 26) { if (!flg[i]) { cout << (char)(i + 'a') << endl; return 0; } } } else { string t = s; if (!next_permutation(t.begin(), t.end())) { cout << -1 << endl; return 0; } rep(i, n) { cout << t[i]; if (t[i] != s[i]) { cout << endl; break; } } } return 0; }
1
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using st=string; using ch=char; typedef pair<ll,ll> P; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<P> vP; typedef vector<ch> vc; typedef vector<vc> vvc; #define FOR(i,a,b) for(ll i=a;i<b;i++) #define rep(i,n) FOR(i,0,n) #define ROF(i,a,b) for(ll i=a;i>=b;i--) #define per(i,a) ROF(i,a,0) #define pb push_back #define mp make_pair const ll MOD=1000000007; const ll MOD2=998244353; const ld PI=acos(-1); const ll INF=1e18; st abc="abcdefghijklmnopqrstuvwxyz"; st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; struct edge{ll to,cost;}; ll n; st s; vc a; bool b; void srch(ll z,ll f){ if(z==0){ a[0]='S'; } else{ a[0]='W'; } if(f==0){ a[1]='S'; } else{ a[1]='W'; } FOR(i,1,n-1){ if((a[i]=='S'&&s[i]=='o')||(a[i]=='W'&&s[i]=='x')){ a[i+1]=a[i-1]; } else{ if(a[i-1]=='W'){ a[i+1]='S'; } else{ a[i+1]='W'; } } } b=true; rep(i,2){ ll cnt=0; if(a[(n-1+i)%n]=='W'){ cnt++; } if(s[(n-1+i)%n]=='x'){ cnt++; } if(a[(n-2+i)%n]!=a[i%n]){ cnt++; } if(cnt%2==1){ b=false; } } } int main(){ cin >> n >> s; a.resize(n); b=false; srch(0,0); if(b){ rep(i,n){ cout << a[i]; } return 0; } srch(0,1); if(b){ rep(i,n){ cout << a[i]; } return 0; } srch(1,0); if(b){ rep(i,n){ cout << a[i]; } return 0; } srch(1,1); if(b){ rep(i,n){ cout << a[i]; } return 0; } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1E5; bool isSheep[MAXN]; bool o[MAXN]; bool options[2] = {true, false}; bool isValid(int n) { bool firstSheep = isSheep[0]; for (int i = 1; i < n; i++) { if (isSheep[i] && o[i] || !isSheep[i] && !o[i]) { isSheep[(i + 1) % n] = isSheep[i - 1]; } else if (isSheep[i] && !o[i] || !isSheep[i] && o[i]) { isSheep[(i + 1) % n] = !isSheep[i - 1]; } } if (isSheep[0] != firstSheep) return false; if (firstSheep && o[0] || !firstSheep && !o[0]) { return isSheep[n - 1] == isSheep[1]; } else { return isSheep[n - 1] != isSheep[1]; } } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { char c; cin >> c; o[i] = c == 'o'; } bool valid = false; for (bool s1 : options) { for (bool s2 : options) { isSheep[0] = s1; isSheep[1] = s2; if (isValid(n)) { valid = true; break; } } if (valid) break; } if (valid) { stringstream ss; for (int i = 0; i < n; i++) { if (isSheep[i]) ss << "S"; else ss << "W"; } cout << ss.str() << endl; } else cout << -1 << endl; return 0; }
1
#include<iostream> #include<math.h> using namespace std; int maxn=1000; int main(){ int A[maxn]; double a,b=0,c=0,d=0,e=0,f=0,g=0,w=0,x=0,y=0,z=0; for(int i=0;i!=6;i++){ cin>>a; A[i]=a; } for(int i=4;i!=0;i--){ b=abs(A[0]-A[i]); if(b>A[5]){cout<<":("; return 0;} b=0; } for(int i=4;i!=1;i--){ b=abs(A[1]-A[i]); if(b>A[5]){cout<<":("; return 0;} b=0; } for(int i=4;i!=2;i--){ b=abs(A[2]-A[i]); if(b>A[5]){cout<<":("; return 0;} b=0; } for(int i=4;i!=3;i--){ b=abs(A[3]-A[i]); if(b>A[5]){cout<<":("; return 0;} b=0; } cout<<"Yay!"; return 0; }
#include <iostream> #include <vector> #include <string.h> #include <algorithm> #include <cstring> #include <queue> #include <assert.h> #include <cmath> #include <stack> #include <deque> #include <set> #include <unordered_map> #include <complex> #include <fstream> #include <map> #include <numeric> #include <functional> using namespace std; const int mxn = 1e6+10; const int mod = 1e9+7; const int inf = 2e9+10; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int x, y; cin>>x>>y; long long ans1 = y>=x? y-x:inf, ans2 = -y>=x? -y-x+1:inf, ans3 = y>=-x? y+x+1:inf, ans4 = -y>=-x? -y+x+2:inf; cout << min(min(ans1, ans2), min(ans3, ans4)) <<'\n'; return 0; } //comparator function in sorting //swapping arguments //missing cases in dp //dividing in modulo //Beware of overflow and modulo //Be mindful of overflow //s is a matrix but forget //length of ranges //working with bit using ull
0
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < (n); i++) #define MOD 1000000007 #define INF 1e9 #define All(x) (x).begin(), (x).end() typedef long long ll; using namespace std; int main() { int h,w,n; cin>>h>>w>>n; if(w>h)swap(w,h); int ans=ceil((double)n/h); cout<<ans<<endl; return 0; }
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; const double EPS = 1e-9; int main(){ int l[10], v[2]; while(scanf("%d,", &l[0])!=EOF){ for(int i=1; i<10; i++){ scanf("%d,", &l[i]); } scanf("%d,%d", &v[0], &v[1]); int total=0; for(int i=0; i<10; i++){ total += l[i]; } double t = (double)total/(v[0]+v[1]); double d = v[0]*t -EPS; int sum=0; for(int i=0; i<10; i++){ sum += l[i]; if(sum > d){ cout << i+1 << endl; break; } } } return 0; }
0
#include<bits/stdc++.h> using namespace std; #define int long long map<int, map<int, int> > dp1; map<int, map<int, int> > dp2; int mo=1000000007; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int n,m; cin>>n>>m; int x[n+5]; int y[m+5]; int dpx[n+5]; int dpy[m+5]; for(int i=0;i<n;i++) cin>>x[i]; for(int j=0;j<m;j++) cin>>y[j]; sort(x,x+n); int mo=1000000007; int sx=0,sy=0; for(int i=1;i<n;i++) sx=(sx + (((((x[i]-x[i-1])*i)%mo)*(n-i))%mo) )%mo; sort(y,y+m); for(int i=1;i<m;i++) sy=(sy + (((((y[i]-y[i-1])*i)%mo)*(m-i))%mo) )%mo; //cout<<sx<<" "<<sy<<" "; cout<<(sx*sy)%mo; }
#include <bits/stdc++.h> #define CHOOSE(a) CHOOSE2 a #define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x #define dump_1(x1) cerr << #x1 << ": " << x1 << endl #define dump_2(x1, x2) \ cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl #define dump_3(x1, x2, x3) \ cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \ << x3 << endl #define dump_4(x1, x2, x3, x4) \ cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \ << x3 << ", " #x4 << ": " << x4 << endl #define dump_5(x1, x2, x3, x4, x5) \ cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \ << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl #define dump(...) \ CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__) #define check(s) cerr << s << endl #define rep(i, n) for (int i = 0; i < n; i++) #define repr(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define all(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) using namespace std; using ll = long long; vector<int> dx = {0, 1, 0, -1}; vector<int> dy = {1, 0, -1, 0}; const ll LINF = 2e18; const int INF = 1e9; double e(ll x) { ll sum = x * (x + 1) / 2; return (double)sum / (double)x; } void solve(ll N, ll K, std::vector<ll> p) { vector<ll> acc(N + 1); acc.at(0) = 0; rep(i, N) acc.at(i + 1) = acc.at(i) + p.at(i); ll idx = -1; ll mx = -1; rep(i, N - K + 1) { ll s = acc.at(i + K) - acc.at(i); if (s > mx) { mx = s; idx = i; } } double ans = 0.0; FOR(i, idx, idx + K) { ans += e(p.at(i)); } cout << ans << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); ll N; scanf("%lld", &N); ll K; scanf("%lld", &K); std::vector<ll> p(N); for (int i = 0; i < N; i++) { scanf("%lld", &p[i]); } solve(N, K, std::move(p)); return 0; }
0
#include <algorithm> #include <complex> #include <cstdlib> #include <ctime> #include <time.h> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #include <numeric> #include <limits> #include <type_traits> #include <locale> #include <omp.h> using namespace std; #define SAY_YES cout << "YES" << endl; #define SAY_Yes cout << "Yes" << endl; #define SAY_NO cout << "NO" << endl; #define SAY_No cout << "No" << endl; #define IFYES(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) \ { \ cout << "YES" << endl; \ } \ else \ { \ cout << "NO" << endl; \ } #define IFYes(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) \ { \ cout << "Yes" << endl; \ } \ else \ { \ cout << "No" << endl; \ } #define IFyes(TRUE_OR_FALSE) \ if (TRUE_OR_FALSE) \ { \ cout << "yes" << endl; \ } \ else \ { \ cout << "no" << endl; \ } #define DEBUG_OUTPUT_ARRAY(XXX, ONE) \ for (int i = 0; i < (ONE); i++) \ { \ cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \ } #define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO) \ for (int i = 0; i < (ONE); i++) \ { \ cout << "<<< i = " << i << " >>>" << endl; \ for (int j = 0; j < (TWO); j++) \ { \ cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \ } \ } #define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \ for (int i = 0; i < (ONE); i++) \ { \ cout << i << " "; \ for (int j = 0; j < (TWO); j++) \ { \ cout << XXX[i][j] << " "; \ } \ cout << endl; \ } typedef pair<long long int, long long int> pll; typedef pair<long long int, pll> lpll; const long long int mod = 998244353; const long long int INF = 1e18; const long double PI=3.14159265358979323; //const long long int pl=1000000; long long int N,x,a[200200]={},b[200200],res=0,now=0; string str[20][20]; bool used[200200]={}; int main(){ cout << fixed << setprecision(18); cin>>N>>x; for(long long int i = 1; i <= N; i++){ cin>>a[i]; } for(long long int i = 1; i <= N; i++){ if(a[i]+a[i-1]>x){ res+=a[i]+a[i-1]-x; a[i]-=a[i]+a[i-1]-x; } } cout<<res<<endl; }
#include<iostream> using namespace std; int main(){ while(1){ int n,m,b,c,cnt=0,a[101]={}; cin>>n>>m; if(n+m==0)break; for(int i=0;i<n;i++){ cin>>b; for(int j=0;j<b;j++){ cin>>c; a[c]++; } } int x=0,max=0; for(int i=0;i<101;i++){ if(max<a[i]&&m<=a[i]){ max=a[i]; x=i; } } cout<<x<<endl; } return 0; }
0
#include<iostream> #include<vector> #include<algorithm> #include<cmath> using namespace std; const long long MOD = (long long)1e9 + 7; int main(){ int n, m; cin >> n >> m; vector<long long> x(n), y(m); for(int i = 0; i < n; i++){ cin >> x[i]; } for(int i = 0; i < m; i++){ cin >> y[i]; } long long a = 0, b = 0; for(int i = 1; i < n; i++){ a += i * x[i] - (n - i) * x[i - 1]; if(a < 0){ a += MOD * (long long)((abs(a) + MOD - 1) / MOD); } a %= MOD; } for(int i = 1; i < m; i++){ b += i * y[i] - (m - i) * y[i - 1]; if(b < 0){ b += MOD * (long long)((abs(b) + MOD - 1) / MOD); } b %= MOD; } cout << (a * b) % MOD << endl; return 0; }
/*-- ILSH --*/ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define pb push_back #define pop pop_back #define mp make_pair #define vii vector < int > #define vll vector < ll > #define dt cout<<"HERE\n"; #define pii pair < int , int > #define pll pair < ll , ll > #define vpi vector < pll > #define vpl vector < pll > #define fi first #define se second #define sz size #define len length const ll inf=1e18+1e17; const ll mod =1e9+7; //const ll mod = 998244353; ll modInverse(ll a,ll m){ll m0=m;ll y=0,x=1;if(m == 1)return 0;while(a> 1){ll q=a/m;ll t=m;m=a%m,a=t;t=y;y=x-q*y;x=t;}if(x<0)x+=m0;return x;} ll powm(ll a,ll b){a=a%mod;ll res=1;while(b){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return (res%mod);} const ll N =5e5+5; void solve(){ ll n,m; cin>>n>>m; ll arr[n], brr[m],crr[n],drr[m]; for ( int i=0;i<n;i++) cin>>arr[i]; for ( int j=0;j<m;j++) cin>>brr[j]; for ( int i=1;i<n;i++){ crr[i]=arr[i]-arr[i-1]; } for ( int i=1;i<m;i++) drr[i]=brr[i]-brr[i-1]; for ( int i=2;i<n;i++) crr[i]=(crr[i]*i+crr[i-1])%mod; for ( int i=2;i<m;i++) drr[i]=(drr[i]*i+drr[i-1])%mod; ll tot1=0,tot2=0,ans=0; for ( int i=1;i<n;i++) tot1=tot1+crr[i]; for ( int j=1;j<m;j++) tot2=tot2+drr[j]; tot2=tot2%mod; tot1=tot1%mod; ans=(tot1*tot2)%mod; cout<<ans<<'\n'; } int main(){ ios_base::sync_with_stdio(false); cin.tie( NULL); cout.tie(NULL); // #ifndef // freopen( "INPUT.txt", "r" , stdin); // freopen( "OUTPUT.txt","w", stdout); // #endif int t=1; //cin>>t; for ( int i=1;i<=t;i++){ // cout<<"Case #"<<i<<": "; solve(); } }
1
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<stdio.h> #include<deque> #include<map> #include<queue> #include<cmath> typedef long long ll; #define debug(x) cout << #x << '=' << x << endl; #define debug_arr(a, n) for(ll i = 0; i < n; i++)cout << a[i] << ' ' #define inf 100000000000 #define loop(i,n) for(ll i = 0; i < n; i++) #define graph vector<vector<ll>> #define P pair<ll,ll> using namespace std; int main(){ int n,k; cin >> n >> k; vector<int> v(n); loop(i,n){ cin >> v[i]; } ll result = 0; // i個の宝石を取る loop(i,min(n,k)+1){ // j個だけ左から取る loop(j,i+1){ vector<int> minusJewel = {}; ll tmp = 0; loop(l,j){ tmp += v[l]; if(v[l] < 0)minusJewel.push_back(v[l]); } loop(l,i-j){ tmp += v[n-1-l]; if(v[n-l-1] < 0)minusJewel.push_back(v[n-l-1]); } sort(minusJewel.begin(),minusJewel.end()); ll minusNum = minusJewel.size(); loop(l,min(k-i, minusNum)){ tmp -= minusJewel[l]; } result = max(tmp, result); } } cout << result << endl; }
// 2020-07-15 23:13:36 #include<bits/stdc++.h> #ifdef LOCAL #include "lib/debug.hpp" #else #define debug(...) 1 #endif #define ALL(a) (a).begin(), (a).end() #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--) using namespace std; using ll = long long; using ld = long double; using pr = pair<ll, ll>; using vll = vector<ll>; using vpr = vector<pr>; 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; } vector<int> vs; int dp[55][55][105]; int rec(int l, int r, int k) { if(dp[l][r][k] >= 0) return dp[l][r][k]; chmax(dp[l][r][k], 0); if(l == r) return dp[l][r][k]; if(k >= 1) { chmax(dp[l][r][k], rec(l+1, r, k-1) + vs[l]); chmax(dp[l][r][k], rec(l, r-1, k-1) + vs[r-1]); } if(k >= 2) { chmax(dp[l][r][k], rec(l+1, r, k-2)); chmax(dp[l][r][k], rec(l, r-1, k-2)); } return dp[l][r][k]; } void answer() { int n, k; cin >> n >> k; vs.resize(n); rep(i, n) cin >> vs[i]; repc(i, n) repc(j, n) repc(ki, k) dp[i][j][ki] = -1; cout << rec(0, n, k) << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); answer(); return 0; }
1
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define all(a) a.begin(), a.end() #define pb push_back #define mp make_pair typedef long long ll; typedef pair<ll,ll> P; #define rep(i,a,b) for(ll i=a ; i<b ; i++) const int max_n = 1e5; const ll mod = 1e9+7; const ll INF = 1LL<<60; typedef long double ld; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int dx[4] = {0,0,1,-1}; int dy[4] = {1,-1,0,0}; ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; } bool betemp(string sleft, string sright, int left, int right){ if(sleft==sright){ return (right-left)%2 == 1; }else{ return (right-left)%2 == 0; } } void solve(int N){ string vct = "Vacant"; string sleft, sright, str; int left=0, right=N/2; cout << left << endl; cin >> sleft; if(sleft==vct) return ; cout << right << endl; cin >> sright; if(sright==vct) return ; if(!betemp(sleft, sright, left, right)){ swap(sleft, sright), left = right, right = N; } while(left+1<right){ int mid = (left+right)/2; cout << mid << endl; cin >> str; if(str==vct) return; if(betemp(sleft, str, left, mid)){ sright = str, right = mid; }else{ left = mid, sleft = str; } } } int main(){ int N; while(cin>>N) solve(N); return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (long long i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<ll,ll>; const ll MOD = 1000000007; int main(){ ll N,M; cin >> N >> M; if(M%2==0){ rep(i,M/2){ cout << i+1 << " " << M-i+1 << endl; cout << M+i+2 << " " << 2*M-i+1 << endl; } }else{ rep(i,M/2){ cout << i+1 << " " << M-i+1 << endl; cout << M+i+2 << " " << 2*M-i+1 << endl; } cout << M/2+1 << " " << M/2+2 << endl; } }
0
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <cmath> #include <queue> #include <set> #include <map> #include <iomanip> #include <fstream> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int mod = 1000000007; vector<string> a; bool cmp(string s, string t) { if (s.length() != t.length()) return s.length() < t.length(); return s < t; } void sol(string s) { a.push_back(s); if (s.length() == 10) return; if (s.back() != '0') { s += (s.back() - 1); sol(s); s.pop_back(); } s += (s.back()); sol(s); s.pop_back(); if (s.back() != '9') { s += (s.back() + 1); sol(s); s.pop_back(); } } int main() { ios::sync_with_stdio(false); int a, b, c; cin >> a >> b >> c; cout << c << ' ' << a << ' ' << b; }
#include<bits/stdc++.h> using namespace std; #define F first #define S second #define PB push_back #define MP make_pair #define REP(i,a,b) for(inti=a;i<=b;i++) typedef long long int ll; typedef vector<int>vi; typedef pair<int,int>pi; int main() { int arr[3]; cin>>arr[0]>>arr[1]>>arr[2]; cout<<arr[2]<<" " <<arr[0]<<" "<<arr[1]<<endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; #define Inv(x) QPow(x,Mod-2); const int maxN=2005; const int maxM=maxN*maxN; const int Mod=1e9+7; int n,K; int Fc[maxM],Ifc[maxM]; int F[maxN][maxN]; int QPow(int x,int cnt); int C(int n,int m); int main(){ Fc[0]=Ifc[0]=1;for (int i=1;i<maxM;i++) Fc[i]=1ll*Fc[i-1]*i%Mod; Ifc[maxM-1]=Inv(Fc[maxM-1]);for (int i=maxM-2;i>=1;i--) Ifc[i]=1ll*Ifc[i+1]*(i+1)%Mod; scanf("%d%d",&n,&K); if (K==1){ puts("1");return 0; } F[0][0]=1; for (int i=1;i<=n;i++) for (int j=n;j>=0;j--){ if (j<=i&&j>=0) F[i][j]=1ll*F[i-1][j-1]*C(i*K-j-1,K-2)%Mod; F[i][j]=(F[i][j]+F[i][j+1])%Mod; } /* for (int i=0;i<=n;i++){ for (int j=0;j<=n;j++) cout<<F[i][j]<<" "; cout<<endl; }//*/ int Ans=1ll*F[n][0]*Fc[n]%Mod; printf("%d\n",Ans);return 0; } int QPow(int x,int cnt){ int ret=1; while (cnt){ if (cnt&1) ret=1ll*ret*x%Mod; x=1ll*x*x%Mod;cnt>>=1; } return ret; } int C(int n,int m){ if (n<0||m<0||n<m) return 0; return 1ll*Fc[n]*Ifc[m]%Mod*Ifc[n-m]%Mod; }
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) typedef long long ll; using namespace std; int main(){ int L,R; cin>>L>>R; ll x=2000000000; if(R-L+1>=2019){ cout<<0<<endl; } else{ ll w=0; for(ll i=L;i<R;i++){ for(ll j=L+1;j<=R;j++){ ll a=i%2019; ll b=j%2019; x=min(x,a*b%2019); } } cout<<x<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string a,b; cin>>a>>b; cout<<b<<a; }
#include <iostream> #include <math.h> #include <string> using namespace std; int main() { int a,b,c,temp=0,temp2=0; cin>>a>>b>>c; swap(a,b); swap(a,c); cout<<a<<" "<<b<<" "<<c<<endl; return 0; }
0
#include <iostream> #include <string> #include <vector> #include <cmath> #include <algorithm> #include <cstdlib> #include <ctime> #include <cstdio> #include <functional> #include <set> #include <sstream> #include <map> #include <queue> using namespace std; const double eps=1e-10; const int dy[]={-1,-1,0,1,1, 1, 0,-1}; const int dx[]={ 0, 1,1,1,0,-1,-1,-1}; class rect{ public: int h,w,d2; rect(int _h,int _w){h=_h; w=_w; d2=h*h+w*w;} bool operator<(const rect& r) const{ return d2<r.d2 || d2==r.d2 && h<r.h; } }; int main() { vector< rect > vr; for(int h=1;h<=150;h++){ for(int w=h+1;w<=150;w++) vr.push_back(rect(h,w)); } sort(vr.begin(),vr.end()); int h,w; while(cin>>h>>w,h){ rect r(h,w); for(int i=0;i<vr.size();i++){ if(r<vr[i]){ cout<<vr[i].h<<" "<<vr[i].w<<endl; break; } } } return 0; }
#include<iostream> #include<string> #include<math.h> using namespace std; int main(){ int h,w,s; while(true){ cin>>h>>w; if(h==0&&w==0) break; bool t=true; s=h*h+w*w; h++; while(true){ if(h*h>=s-h*h) break; if(s==h*h+(int)sqrt((double)s-h*h+0.0)*(int)sqrt((double)s-h*h+0.0)){ t=false; break; } h++; } s++; if(t==true){ while(true){ h=1; while(true){ if(h*h>=s-h*h) break; if(s==h*h+((int)sqrt((double)s-h*h+0.0)*(int)sqrt((double)s-h*h+0.0))){ t=false; break; } h++; } if(t==false) break; s++; } } cout<<h<<" "<<(int)sqrt((double)s-h*h+0.0)<<endl; } return 0; }
1
#include <bits/stdc++.h> #define loop(times) for(int i=0;i<(times);i++) using namespace std; int main() { while(1<2){ int n; long long max; cin>>n; if(n==0)break; int a[n]; int s[n]; loop(n)cin>>a[i]; max=a[0]; s[0]=a[0]; loop(n)s[i+1]=s[i]+a[i+1]; for(int k=0;k<n;k++){ loop(n-k){ if(max<=s[k+i]-s[k-1])max=s[k+i]-s[k-1]; } } cout<<max<<endl; } }
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; typedef pair<ll,ll> pll; const ll mod=1e9+7; //const ll mod=998244353; const ll inf=1LL<<61; const ll N=2e5+50; vec l(N),r(N); vec a(N); ll cal(ll x) { if(x<3) { return 0; } return max({l[x-1],r[x-1],cal(x-2)+a[x]}); } int main() { ll n; cin >> n; for(ll i=1;i<=n;i++) cin >> a[i]; for(ll i=2;i<=n;i+=2) { l[i]=l[i-2]+a[i-1]; r[i]=max(r[i-2],l[i-2])+a[i]; } if(n%2) { cout << cal(n) << endl; } else { cout << max(l[n],r[n]) << endl; } }
0
#include <bits/stdc++.h> using namespace std; #define ll long long #define vi vector<int> #define vvi vector<vector<int>> #define all(a) (a).begin(), (a).end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) const int MOD = 1e9+7; const ll LINF = 1e18; const int INF = 1e9; ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z; string S, T; int main() { cin >> S; smatch ssmm; if(regex_search(S,ssmm,regex("^A[a-z]+C[a-z]+$"))) { cout << "AC"; return 0; } cout << "WA"; return 0; }
#include<iostream> #include<string> #include<algorithm> using namespace std; int main() { string s; cin >> s; int l = s.length(); if(s[0]!='A'){ cout << "WA\n"; return 0; } int i, cnt = 0; for(i=1; i<l; i++){ if(isupper(s[i])){ if(i==1 || i==l-1 || s[i]!='C'){ cout << "WA\n"; return 0; } cnt++; } } if(cnt==1) cout << "AC\n"; else cout << "WA\n"; return 0; }
1
#include<bits/stdc++.h> using namespace std; constexpr int64_t MAX_LEN = 200000; set<int64_t> up[MAX_LEN + 1], right_st[MAX_LEN + 1], down[MAX_LEN + 1], left_st[MAX_LEN + 1]; set<int64_t> downs[MAX_LEN * 2 + 1], lefts[MAX_LEN * 2 + 1]; int main() { int64_t N; cin >> N; vector<int64_t> X(N), Y(N), dir_num(N); vector<char> U(N); map<char, int64_t> dirChar2dirNum = { { 'U', 0 }, { 'R', 1 }, { 'D', 2 }, { 'L', 3 } }; for (int64_t i = 0; i < N; i++) { cin >> X[i] >> Y[i] >> U[i]; dir_num[i] = dirChar2dirNum[U[i]]; } int64_t ans = LLONG_MAX; //まず正面衝突があるか調べる for (int64_t i = 0; i < N; i++) { if (U[i] == 'U') { up[X[i]].insert(Y[i]); } else if (U[i] == 'R') { right_st[Y[i]].insert(X[i]); } else if (U[i] == 'D') { down[X[i]].insert(Y[i]); } else if (U[i] == 'L') { left_st[Y[i]].insert(X[i]); } } //下向きの一番大きい位置と,上向きの一番小さい位置を比べて交差するかを検証 for (int64_t x = 0; x <= MAX_LEN; x++) { if (down[x].empty() || up[x].empty()) { continue; } for (int64_t up_p : up[x]) { //これより大きいのがあれば一番近いやつを見る auto itr = down[x].lower_bound(up_p); if (itr != down[x].end()) { ans = min(ans, 5 * (*itr - up_p)); } } } for (int64_t y = 0; y <= MAX_LEN; y++) { if (left_st[y].empty() || right_st[y].empty()) { continue; } for (int64_t right_p : right_st[y]) { auto itr = left_st[y].lower_bound(right_p); if (itr != left_st[y].end()) { ans = min(ans, 5 * (*itr - right_p)); } } } //90度での衝突を調べる for (int64_t j = 0; j < 4; j++) { //downとleftの衝突だけを調べる //x + yが一致しているものがぶつかりそう for (int64_t xy = 0; xy <= MAX_LEN * 2; xy++) { downs[xy].clear(); lefts[xy].clear(); } for (int64_t i = 0; i < N; i++) { if (dir_num[i] == dirChar2dirNum['D']) { downs[X[i] + Y[i]].insert(Y[i]); } else if (dir_num[i] == dirChar2dirNum['L']) { lefts[X[i] + Y[i]].insert(Y[i]); } } // for (int64_t xy = 0; xy <= MAX_LEN * 2; xy++) { for (int64_t left_p : lefts[xy]) { auto itr = downs[xy].lower_bound(left_p); if (itr != downs[xy].end()) { ans = min(ans, 10 * (*itr - left_p)); } } } //90度回転 for (uint64_t i = 0; i < X.size(); i++) { swap(X[i], Y[i]); Y[i] = MAX_LEN - Y[i]; (++dir_num[i]) %= 4; } } if (ans == LLONG_MAX) { cout << "SAFE" << endl; } else { cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } using ll = long long; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const int INF = 1<<30; const ll mod = 1000000007LL; template<class T>struct dijkstra{//T=int or ll struct Node{ vector<T> edges_to; vector<T> edges_cost; bool done; T cost=-1; }; vector<Node> graph; void init(T N){ graph.resize(N); } void add(T from, T to, T cost){ graph[from].edges_to.push_back(to); graph[from].edges_cost.push_back(cost); } void shortest(T start){ priority_queue<pair<T,T>,vector<pair<T,T>>,greater<pair<T,T>>> d; d.push(make_pair(0,start)); while(!d.empty()){ pair<T,T> p = d.top(); d.pop(); Node &point = graph[p.second]; if(point.done) continue; point.done = true; point.cost = p.first; for(T i = 0; i < (T)point.edges_to.size(); i++){ if(graph[point.edges_to[i]].done)continue; d.push(make_pair(point.cost+point.edges_cost[i],point.edges_to[i])); } } } }; int main() { ll N; cin>>N; dijkstra<ll> D; D.init(N); for(ll i = 0; i < N-1; i++) { ll x,y,z; cin>>x>>y>>z; D.add(x-1,y-1,z); D.add(y-1,x-1,z); } ll Q,K; cin>>Q>>K; D.shortest(K-1); for(ll i = 0; i < Q; i++){ ll x,y; cin>>x>>y; cout<<D.graph[x-1].cost+D.graph[y-1].cost<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int const MX = 2e5 + 2, MOD = 1e9 + 7; int main() { int t = 1; // cin>>t; while(t--){ int n; cin>>n; int l[101]; for(int i = 0; i < n; i++){ cin>>l[i]; } int cnt = 0; for(int i = 0; i < n - 2; i++){ for(int j = i + 1; j < n-1; j++){ for(int k = j + 1; k < n; k++){ if(l[i] == l[j] || l[i] == l[k] || l[j] == l[k]) continue; if(l[i] + l[j] > l[k] && l[i] + l[k] > l[j] && l[k] + l[j] > l[i]) cnt++; } } } cout<<cnt; } }
#include <iostream> #include <functional> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <set> #include <queue> #include <map> #include <iomanip> #include <complex> #include <random> #include <bitset> #include <list> // #include <prettyprint.hpp> using namespace std; #define repi(i,n) for(int i=0;i<n;++i) #define rep(i,n) for(ll i=0;i<n;++i) #define repinvi(i,n) for(int i=n-1;i>=0;--i) #define sll(n) scanf("%lld", &n); #define sii(n) scanf("%d", &n); #define slf(n) scanf("%lf", &n); #define pll pair<ll,ll> #define pii pair<int,int> #define psi pair<si,si> typedef long long ll; typedef double lf; typedef short int si; void Main(){ ll N, l; sll(N); // vector<ll> ls(N, 0); map<ll, ll> counter; rep(i, N){ sll(l); counter[l] ++; } ll ans = 0; for(auto p1:counter){ for(auto p2:counter){ if(p1.first >= p2.first){ continue; } for(auto p3:counter){ if(p1.first >= p3.first){ continue; }else if(p2.first >= p3.first){ continue; } if(p3.first >= p1.first + p2.first){ continue; } ans += (p1.second * p2.second * p3.second); } } } cout << ans <<endl; } int main(){ Main(); }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ int h, w, n; cin >> h >> w >> n; cout << (n + max(h, w)-1) / max(h, w) << endl; return 0; }
#include <iostream> #include <cmath> #include <vector> #include <bitset> #include <string> #include <utility> #include <queue> #include <algorithm> #include<set> using namespace std; long long int mpown(long long int m, int n); long long int bign=1000000007; int bitCount(int bits); int main() { long long int n,m,k; cin >> n >> m >> k; // vector<vector<long long int>> comb(n*m,vector<long long int>(k-1,0)); // vector<long long int> comb(max(n,m)); long long int comb = 1; if(k!=2){ vector<long long int> inv(k-1); inv.at(1)=1; // cout << "a"<<endl; for(int i=2;i<k-1;i++){ inv.at(i) = bign-inv.at(bign%i)*(bign/i)%bign; } for(int i=1;i<=k-2;i++){ comb = (((comb*(n*m-2-i+1))%bign)*inv.at(i))%bign; } } long long int ans=0; long long int temp=0; // comb.at(0).at(0)=1; // for(long long int i=1;i<n*m-1;i++){ // for(long long int j=0;j<=min(k-2,i);j++){ // // cout << "a " << endl; // comb.at(i).at(j)=(j>0? (comb.at(i-1).at(j-1)+comb.at(i-1).at(j))%bign:comb.at(i-1).at(j)); // // cout << i << "," << j << "," << comb.at(i).at(j) << endl; // } // } for(int d=1;d<=n-1;d++){ temp = ((((((comb*d)%bign)*m)%bign)*m)%bign)*(n-d); temp %= bign; ans=(ans+temp)%bign; } for(int d=1;d<=m-1;d++){ temp = ((((((comb*d)%bign)*n)%bign)*n)%bign)*(m-d); temp %= bign; ans=(ans+temp)%bign; } cout << ans << endl; return 0; } int bitCount(int bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); // 2bitごとに計算 bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); // 4bitごとに計算 bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); // 8bitごとに計算 bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); // 16ビットごとに計算 return (bits & 0x0000ffff) + (bits >> 16); // 32ビット分を計算 } long long int mpown(long long int m, int n) { if (n == 0) return 1; else if (n == 1) return m; else { long long int temp = mpown(m, n / 2); return temp * temp * (n & 1 ? m : 1); } }
0
//Author : Sukarna Paul #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/trie_policy.hpp> #include<ext/pb_ds/detail/standard_policies.hpp> #define pi acos(-1) #define pb push_back #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define ll long long #define MAX 300005 #define INF 0x3f3f3f3f template <class T> inline T bigmod(T p,T e,T M){ll ret = 1LL;for(; e > 0LL; e >>= 1LL){if(e & 1LL) ret = (ret * p) % M;p = (p * p) % M;}return (T)ret;} template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);} // M is prime} using namespace std; using namespace __gnu_pbds; typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>pref_trie; typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset; int main(){ IOS //freopen("output.txt","w",stdout); string s; cin>>s; int last [26]; for(int i=0;i<26;i++)last[i]=-11; for(int i=0;i<s.length();i++){ if(i-last[s[i]-'a']<3){ cout<<last[s[i]-'a']+1<<" "<<i+1<<"\n"; return 0; } last[s[i]-'a']=i; } cout<<"-1 -1\n"; }
#include <bits/stdc++.h> using namespace std; int main(void) { int R; cin >> R; if(R < 1200) { cout << "ABC\n"; } else if(R<2800) { cout << "ARC\n"; } else { cout << "AGC\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; //long long using ll = long long; //出力系 #define print(x) cout << x << endl #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl // begin() end() #define all(x) (x).begin(),(x).end() //for #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) //最大公約数(ll) ll gcd(ll x, ll y) { return y ? gcd(y,x%y) : x;} //素因数分解 vector<pair<ll, int>>factorize(ll n){ vector<pair<ll, int>> res; for(ll i = 2; i * i <= n; ++i){ if(n % i) continue; res.emplace_back(i, 0); while(n % i == 0){ n /= i; res.back().second++; } } if(n != 1) res.emplace_back(n, 1); return res; } int main() { string S; cin >> S; for(int i = 0; S.size() > i; i++){ if(S.at(i) == 'A' && S.at(i + 1) == 'C') { cout << "Yes" << endl; return 0; } } cout << "No" << endl; }
#include <bits/stdc++.h> using namespace std; int main() { //long long a,b; //int *num=new int[4]; //char *s=new char[3]; string s; cin >> s; if (s.find("AC") != std::string::npos) { cout << "Yes" << "\n"; }else cout << "No" << "\n"; }
1
#include<iostream> #include<string> using namespace std; int main() { char s[5000]; long long n,i,j,k,total=0,red[5000]={0},green[5000]={0},blue[5000]={0},r=0,g=0,b=0,x,y; cin>>n; for(i=1;i<=n;i++) { cin>>s[i]; } for(i=1;i<=n;i++) { if(s[i]=='R') { r++; red[i]=red[i-1]+1; blue[i]=blue[i-1]; green[i]=green[i-1]; } else if(s[i]=='G') { g++; green[i]=green[i-1]+1; red[i]=red[i-1]; blue[i]=blue[i-1]; } else if(s[i]=='B') { b++; blue[i]=blue[i-1]+1; red[i]=red[i-1]; green[i]=green[i-1]; } } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if((s[i]=='R'&&s[j]=='G')||(s[i]=='G'&&s[j]=='R')) { x=b-blue[j]; y=(j-i)+j; if(s[y]=='B'&&x>0&&y<=n) { x--; } if(x>0) { total=total+x; } } else if((s[i]=='R'&&s[j]=='B')||(s[i]=='B'&&s[j]=='R')) { x=g-green[j]; y=(j-i)+j; if(s[y]=='G'&&x>0&&y<=n) { x--; } if(x>0) { total=total+x; } } else if((s[i]=='B'&&s[j]=='G')||(s[i]=='G'&&s[j]=='B')) { x=r-red[j]; y=(j-i)+j; if(s[y]=='R'&&x>0&&y<=n) { x--; } if(x>0) { total=total+x; } } } } cout<<total<<endl; return 0; }
#include <iostream> #include <string> using i64 = long long; int main() { int n; std::string s; std::cin >> n >> s; i64 cnt[3] {}; for (auto c : s) cnt[c == 'R' ? 0 : c == 'G' ? 1 : 2]++; i64 ret = cnt[0] * cnt[1] * cnt[2]; for (int i = 0; i < n; i++) for (int j = 1; i - j >= 0 && i + j < n; j++) { if (s[i] != s[i - j] && s[i] != s[i + j] && s[i - j] != s[i + j]) ret--; } std::cout << ret << std::endl; return 0; }
1
#include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) using namespace std; const int N = 16; int n, m, tot, sz, dp[N][1 << N], e[N][N], val[1 << N]; // val: 预处理每个联通块所有边的总值 void pre() { rep(s, 0, sz) rep(i, 1, n) if ((s >> (i - 1)) & 1) rep(j, i + 1, n) if ((s >> (j - 1)) & 1) val[s] += e[i][j]; } void chkmax(int &x, int y) { x = max(x, y); } int main() { cin >> n >> m; sz = (1 << n) - 1; rep(i, 1, m) { int x, y, z; scanf("%d%d%d", &x, &y, &z); e[x][y] = e[y][x] = z; tot += z; } pre(); memset(dp, -1, sizeof(dp)); dp[1][1] = 0; rep(s, 1, sz) rep(i, 1, n) if (((s >> (i - 1)) & 1) && dp[i][s] != -1) { rep(j, 1, n) if (!((s >> (j - 1)) & 1) && e[i][j]) chkmax(dp[j][s | (1 << (j - 1))], dp[i][s] + e[i][j]); // e[i][j] 作为桥边 int S = (sz ^ s) | (1 << (i - 1)); for (int j = S; j; j = (j - 1) & S) if (j & (1 << (i - 1))) chkmax(dp[i][s | j], dp[i][s] + val[j]); } printf("%d\n", tot - dp[n][sz]); return 0; }
#include <bits/stdc++.h> using namespace std; #define rint register int #define gc() getchar() inline int read(){int r=0,s=0,c=gc();for(;!isdigit(c);s=c,c=gc());for(;isdigit(c);(r*=10)+=c-48,c=gc());return s^45?r:-r;} int f[16][32768], g[32768], w[16][16], n, E; int main() { n = read(), E = 1<<n; for(rint m = read(), a, b; m--; a = read()-1, b = read()-1, w[a][b] = w[b][a] = read()); for(rint i = 0; i < n; memset(f[i++],-1,E<<2)); for(rint S = 1, T, i, j; S < E; S++) for(g[S] = g[T=S-(S&-S)], i = ilogb(S&-S), j = 0; j < n; j++) if(T&1<<j) g[S] += w[i][j]; f[0][1] = 0; for(rint S = 1, U = ~-E^1, T, i, j; S < E; S++, U = ~-E^S) for(i = 0; i < n; i++) if(f[i][S]>=0) {for(j = 0; j < n; w[i][j]&&U&1<<j?f[j][S|1<<j]=max(f[j][S|1<<j],f[i][S]+w[i][j]):0, j++); for(T = U; T; f[i][S|T] = max(f[i][S|T],f[i][S]+g[T|1<<i]), --T&=U);} printf("%d\n",g[~-E]-f[~-n][~-E]); return 0; }
1
#include <iostream> using namespace std; int main() { int n, i = 3, tmp; cin >> n; while (i <= n) { if (i % 3 == 0) { cout << ' '<< i ; } else{ for (tmp = i; tmp != 0; tmp /= 10) { if (tmp % 10 == 3) { cout << ' '<< i ; break; } } } i++; } cout << endl; return 0; }
#include<stdio.h> #include<string.h> int main(void) { int a[10000],n,b=0,i,x,d=3; char r[10000]; char s[10000]; scanf("%d", &n); sprintf(s, "%d", d); for(i = 1; i <= n; i++) { sprintf(r,"%d", i); if (i % 3 == 0) { a[b] = i; b++; } else if (strstr(r,s) != NULL) { a[b] = i; b++; } } for (x = 0; x < b; x++) { if (x == 0) { printf(" "); } printf("%d",a[x]); if (x == b - 1) { printf("\n"); } else { printf(" "); } } return 0; }
1
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <stack> #include <tuple> #include <cmath> #include <iomanip> #include <map> #include <cstring> //memset(dp,0,sizeof(dp)) #define ll long long #define rep(i,n) for(int i=0;i<(n);i++) #define rrep(i,n) for(int i=n-1;i>=0;i--) #define fi first #define se second #define pb push_back #define ALL(a) (a).begin(),(a).end() using namespace std; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} typedef pair<ll,ll> P; typedef pair<ll,ll> Pll; //小数点以下 cout << fixed << setprecision(10) << num << endl; //桁数はlog10(mid)でやれ log(mid)/log(10)はWA //long*long<0で判定するな //開区間と閉区間 //アルファベットが何番目かは x=s[i]-'a' //bfsはmain()内で rep(i,h)rep(j,w)rep(i,4)でバグ //切り上げ:(a+(b-1))/b //32bit以上は 1ull<<j // map 初期値0 for(auto itr=mp.begin();itr!=mp.end();++itr) itr->fi itr->se で全探索 // for(auto u:mp) u.fi u.se も可能 //n個のgcd,lcmは一つずつみて更新していく //大きな数を扱うときは素因数分解した形で持っておく // (l/a[i]) = l*powmod(a[i],MOD-2)%MOD; //auto itr=lower_bound(ALL(a),key) a[itr-a.begin()] //根は片が入ってないやつ int main(){ int n,m; cin >> n >> m; vector<vector<int>> to(n); //to[a]=b a->b vector<vector<int>> from(n); vector<bool> pre(n,1); rep(i,n+m-1){ int a,b; cin >> a >> b; a--; b--; to[a].pb(b); from[b].pb(a); pre[b]=0; } int res[n]; int st; rep(i,n){ if(pre[i]){ res[i]=-1; st=i; break; } } queue<int> q; vector<bool> visit(n,0); visit[st]=1; q.push(st); while(!q.empty()){ int v=q.front(); q.pop(); for(auto u:to[v]){ if(visit[u]) continue; bool ok=1; //pre(u)=v? for(auto w:from[u]){ if(!visit[w] || res[w]==v) ok=0; } if(ok){ visit[u]=1; q.push(u); res[u]=v; } } } rep(i,n){ cout << res[i]+1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int,int>> ls[100000]; int cost[100000]; int main(){ int v,e,r; cin>>v>>e>>r; for(int i=0;i<e;i++){ int s,t,d; cin>>s>>t>>d; ls[s].push_back(make_pair(t,d)); } for(int i=0;i<100000;i++) cost[i]=INT_MAX; cost[r]=0; priority_queue<pair<int,int>> pq; pq.push(make_pair(0,r)); while(!pq.empty()){ auto cur=pq.top(); pq.pop(); int dist=-cur.first,now=cur.second; if(dist>cost[now]) continue; for(int i=0;i<(int)ls[now].size();i++){ int to=ls[now][i].first,d=ls[now][i].second; if(dist+d<cost[to]){ cost[to]=dist+d; pq.push(make_pair(-cost[to],to)); } } } for(int i=0;i<v;i++){ if(cost[i]==INT_MAX) cout<<"INF"<<endl; else cout<<cost[i]<<endl; } return 0; }
0
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define REP(i,n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define debug(var) do{cout << #var << " : "; view(var);}while(0) 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;} using namespace std; template<class T> void view(T e) {cout << e << endl;} template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;} template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}} using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int,int>; const int inf = 1e9; const ll inf_l = 1e18; const int MAX = 1e5; int main() { int n; cin >> n; cout << (n-2)*180 << endl; }
#include<iostream> #include<algorithm> using namespace std; #define long long long long sol(long); int main(){ long a=0; cin>>a; cout<<sol(a)<<endl; return 0; } long pow10(long a){ if(a<0)return 0; long r=1,c=10; while(a>0){ if(a&1)r*=c; c*=c; a>>=1; } return r; } long will[50]; long dfs(long d,long pos,long n,long ned){ if(abs(d-ned)>pow10(n-pos+1)-pow10((n+1)/2)-pow10(n/2)+pow10(pos-1))return 0; if(pos>(n+1)/2)return ned==d; long ans=0; for(will[pos]=-9;will[pos]<=9;will[pos]++){ if(pos==1&&will[pos]==9)continue; if(pos==n/2+1&&will[pos]!=0)continue; if(will[pos]>=0)ans+=(9-will[pos]+(pos!=1))*dfs(d+(pow10(n-pos)-pow10(pos-1))*will[pos],pos+1,n,ned); else ans+=(10+will[pos])*dfs(d+(pow10(n-pos)-pow10(pos-1))*will[pos],pos+1,n,ned); } will[pos]=0; return ans; } long sol(long d){ if(d%9)return 0; long ans=0; for(long i=2;pow10((i+1)/2)-pow10((i-1)/2)<=d;i++) ans+=dfs(0,1,i,d); return ans; }
0
#include<iostream> #include<algorithm> #include<vector> #include<numeric> #include<string> #include<cmath> #include<set> #include<queue> #include<deque> #include<bitset> #include<iomanip> #include<cctype> #include<map> #include<cstring> #include<bitset> #include<cassert> #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define rep2(i,a,b) for (int (i)=a;(i)<(b);(i)++) #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long int; const int inf = 1001001000; const long long int Inf = 1001001001001001000; void print(vector<vector<int>> a){ for (int i = 0; i < a.size(); i++) { for (int j=0;j<a[i].size();j++){ cout << a[i][j] << " "; } cout << endl; } } void print(vector<vector<long long int>> a){ for (int i=0;i<a.size();i++){ for (int j=0;j<a[i].size();j++){ cout << a[i][j] << " "; } cout << endl; } } void print(vector<int> a){ int n = a.size(); for (int j=0;j<n;j++) { if (j != n-1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(vector<long long int> a){ int n = a.size(); for (int j=0;j<n;j++) { if (j != n-1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(set<int> a){ for (auto x:a) cout << x << " "; cout << endl; } int main() { string a, b, c; cin >> a >> b >> c; reverse(all(a)); reverse(all(b)); reverse(all(c)); char now = 'a'; while (true) { if (now == 'a') { if(a.size() == 0){ cout << "A" << endl; return 0; } now = a.back(); a.pop_back(); } if(now == 'b'){ if(b.size() == 0){ cout << "B" << endl; return 0; } now = b.back(); b.pop_back(); } if(now == 'c'){ if(c.size() == 0){ cout << "C" << endl; return 0; } now = c.back(); c.pop_back(); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ vector<queue<char>> s(3); for(int ix=0;ix<3;ix++){ string tmp; cin >> tmp; for(int ixx=0;ixx<tmp.size();ixx++) s.at(ix).push(tmp.at(ixx)); } char card; int turnplayer = 0; while(!s.at(turnplayer).empty()){ card = s.at(turnplayer).front(); s.at(turnplayer).pop(); turnplayer = card - 'a'; } if(turnplayer == 0) cout << "A" << endl; if(turnplayer == 1) cout << "B" << endl; if(turnplayer == 2) cout << "C" << endl; }
1
#include <iostream> #include <algorithm> using namespace std; bool solve() { int M, Nmin, Nmax; cin >> M >> Nmin >> Nmax; if (M == 0 && Nmin == 0 && Nmax == 0) { return false; } int p[200]; for (int j = 0; j < M; j++) { cin >> p[j]; } int ind = Nmin-1; for (int j = Nmin; j < Nmax; ++j) { if (p[j]-p[j+1] >= p[ind]-p[ind+1]) { ind = j; } } cout << ind+1 << endl; return true; } int main() { for(;solve();); return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long int n, a, b; cin>>n>>a>>b; long long int x[n]; for(int i=0; i<n; i++) cin>>x[i]; long long int walk_max=b/a; long long int tired=0; for(int i=0; i<n-1; i++){ if(x[i+1]-x[i]>walk_max) tired+=b; else if(x[i+1]-x[i]<=walk_max){ tired+=a*(x[i+1]-x[i]); } } cout<<tired<<endl; }
0
#include <bits/stdc++.h> using namespace std; #define finish(x) return cout << x << endl, 0 #define ll long long int n, X; vector <ll> b, l, r; ll calc(int day, int a){ return (a <= b[day] ? l[day] : r[day]) * (a - b[day]); } ll sum(int day, int len){ if(len <= b[day]) return len * l[day]; return b[day] * l[day] + (len - b[day]) * r[day]; } ll calc(ll h){ int q = h / X; int r = h % X; vector <pair <ll, int> > c; for(int i = 0 ; i < n ; i++){ c.emplace_back(sum(i, X), i); } sort(c.rbegin(), c.rend()); ll s = 0; for(int i = 0 ; i < q ; i++){ s += c[i].first; } if(r == 0) return s; ll ret = 0; for(int i = 0 ; i < q ; i++){ ret = max(ret, s - sum(c[i].second, X) + sum(c[i].second, r) + sum(c[q].second, X)); } for(int i = q ; i < n ; i++){ ret = max(ret, s + sum(c[i].second, r)); } return ret; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> X; b.resize(n); l.resize(n); r.resize(n); for(int i = 0 ; i < n ; i++){ cin >> b[i] >> l[i] >> r[i]; } ll D = 0; for(int i = 0 ; i < n ; i++){ D += calc(i, 0); } ll l = 0, r = 1LL * n * X; while(l <= r){ ll mid = (l + r) / 2; if(D + calc(mid) >= 0) r = mid - 1; else l = mid + 1; } cout << l << endl; }
#include<bits/stdc++.h> using namespace std; #define ll long long int n,m,u,v,w; ll bismarck[(1<<15)][15],e[15][15],sum[(1<<15)]; int main() { cin>>n>>m; while(m--) { cin>>u>>v>>w; u--,v--; e[u][v]=e[v][u]=w; } for(int i=0;i<1<<n;i++) { for(int j=0;j<n;j++){ if((i>>j)&1) { for(int k=j+1;k<n;k++) if((i>>k)&1) sum[i]+=e[j][k]; } bismarck[i][j]=-1e18;} } bismarck[1][0]=0; for(int i=1;i<(1<<n);i++) { for(int j=0;j<n;j++) if((i>>j)&1) { int l=i^(1<<j); for(int k=0;k<n;k++) if((i>>k)&1&&e[j][k]) bismarck[i][j]=max(bismarck[i][j],bismarck[l][k]+e[k][j]); for(int k=l;k>0;k=(k-1)&l) bismarck[i][j]=max(bismarck[i][j],bismarck[i^k][j]+sum[k^(1<<j)]); } } cout<<sum[(1<<n)-1]-bismarck[(1<<n)-1][n-1]; }
0
#include<iostream> #include<algorithm> #define MOD 1000000007 using namespace std; typedef long long ll; signed main(){ ll n,m,xsum=0,ysum=0,ans=0; cin >> n >> m; ll tx[n],ty[m],x[n],y[m]; for(ll i=0;i<n;i++){ cin >> tx[i]; } for(ll i=0;i<m;i++){ cin >> ty[i]; } sort(tx,tx+n); sort(ty,ty+m); n--; m--; for(ll i=0;i<n;i++){ x[i]=tx[i+1]-tx[i]; x[i]%=MOD; } for(ll i=0;i<m;i++){ y[i]=ty[i+1]-ty[i]; y[i]%=MOD; } for(ll i=0;i<n;i++){ if(i==0||i==n-1){ xsum+=x[i]*n; }else{ xsum+=((((i+1)*(n-i))%MOD)*x[i])%MOD; } xsum%=MOD; } for(ll i=0;i<m;i++){ if(i==0||i==m-1){ ysum+=y[i]*m; }else{ ysum+=((((i+1)*(m-i))%MOD)*y[i])%MOD; } ysum%=MOD; } ans = xsum*ysum; ans%=MOD; cout << ans<<endl; }
#include<bits/stdc++.h> #define int long long using namespace std; const int mod=1000000007; signed main(){ int n,m,x1,y1; cin>>n>>m>>x1; int ans1=0,ans2=0; for(int i=0;i<n-1;i++){ int x2; cin>>x2; ans1=(ans1+(x2-x1)*(i+1)%mod*(n-1-i)%mod)%mod; x1=x2; } cin>>y1; for(int i=0;i<m-1;i++){ int y2; cin>>y2; ans2=(ans2+(y2-y1)*(i+1)%mod*(m-1-i)%mod)%mod; y1=y2; } cout<<ans1*ans2%mod; }
1
#include <bits/stdc++.h> using namespace std; int main() { string S; cin>>S; int N=S.size(); if(S=="zyxwvutsrqponmlkjihgfedcba"){ cout<<"-1"<<endl; } else if(N==26){ vector<char> v(26); for(int i=0;i<26;i++){ v[i]=S[i]; } next_permutation(begin(v),end(v)); for(int i=0;i<26;i++){ cout<<v[i]; if(v[i]!=S[i]){ cout<<endl; break; } } } else{ for(int j=0;j<26;j++){ char a='a'+j; int X=0; for(int i=0;i<N;i++){ if(S[i]==a){ X++; break; } } if(X==0){ cout<<S<<a<<endl; break; } } } }
#include<bits/stdc++.h> using namespace std; signed main(){ string s,t; cin>>s; t = s; vector<int> alp(26); for(auto c:s)alp[c-'a']++; string ans = "zzzzzzzzzzzzzzzzzzzzzzzzzzz"; auto chmin = [](auto& a,auto b){if(a>b)a=b;}; while(1){ for(int i=0;i<26;++i)if(alp[i]==0){ string tmp = s; tmp += ('a'+i); if(t<tmp){ chmin(ans,tmp); } } if(s.empty())break; alp[s.back()-'a']--; s.pop_back(); } cout<<(ans=="zzzzzzzzzzzzzzzzzzzzzzzzzzz"?"-1":ans)<<endl; }
1
#include <algorithm> #include <array> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <cstring> #include <functional> #include <fstream> #include <iomanip> #include <iostream> #include <istream> #include <map> #include <math.h> #include <numeric> #include <ostream> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <stdint.h> namespace asl { template <typename T> using vec = std::vector<T>; template <typename T> std::istream &operator>>(std::istream &is, std::vector<T> &vec) { for (auto &value : vec) is >> value; return is; } template <typename T> struct fenwick { int n; std::vector<T> f; fenwick(int n) : n(n), f(n + 1) {} T query(int p) { T ret = 0; for (++p; p > 0; p -= p & -p) ret += f[p]; return ret; } void update(int p, T x) { for (++p; p <= n; p += p & -p) f[p] += x; } }; template <typename T> using BIT = fenwick<T>; } #include <experimental/optional> #include <tuple> namespace asl { typedef long long i64; } #include <random> #include <utility> #define endl '\n' using namespace std; using namespace asl; void solve() { int n, q; cin >> n >> q; vec<int> val(n); cin >> val; BIT<i64> bit(n); for (int i = 0; i < n; ++i) bit.update(i, val[i]); while (q--) { int t; cin >> t; if (t == 0) { int p, x; cin >> p >> x; bit.update(p, x); } else { int l, r; cin >> l >> r; cout << bit.query(r - 1) - bit.query(l - 1) << endl; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; for (int i = 1; i <= t; ++i) { solve(); } return 0; }
#include <iostream> #include <string> #include <vector> #include <atcoder/all> using ll=long long; using namespace std; using namespace atcoder; int main(){ int N,Q; cin>>N>>Q; fenwick_tree<ll> fw(N); ll a; for(int i=0;i<N;i++){ cin>>a; fw.add(i,a); } int q,x,y; for(int i=0;i<Q;i++){ cin>>q>>x>>y; if(q){ cout<<fw.sum(x,y)<<endl; }else{ fw.add(x,y); } } return 0; }
1
#include <algorithm> #include <cstdlib> #include <iostream> #include <string> #include <vector> using namespace std; typedef long long LL; int main() { int A, B; cin >> A >> B; int cnt = 0; for (int i = A; i <= B; ++i) { string s = to_string(i); int n = s.size(); bool ok = true; for (int i = 0; i < n/2; ++i) { if (s[i] != s[n-i-1]) { ok = false; break; } } if (ok) ++cnt; } cout << cnt << endl; }
#include<bits/stdc++.h> using namespace std; int main(){ int A,B; int a=0; cin >> A >> B; for(int i=A;i<=B;i++){ if((i%10==i/10000)&&((i/10)%10==(i/1000)%10)){ a++; } else {} } cout << a <<endl; }
1
#include <bits/stdc++.h> #define repl(i, l, r) for (int i = l; i < r; i++) #define rep(i, n) repl(i, 0, n) using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> l(2 * n); rep(i, 2 * n) { cin >> l[i]; } sort(l.begin(), l.end()); int ans = 0; for(int i = 0; i < 2 * n; i += 2) { ans += l[i]; } cout << ans << endl; return 0; }
#include<stdio.h> #include<algorithm> #define reg register #define ri reg int #define rep(i, x, y) for(ri i = x; i <= y; ++i) #define nrep(i, x, y) for(ri i = x; i >= y; --i) #define DEBUG 1 #define ll long long #define max(i, j) (i) > (j) ? (i) : (j) #define min(i, j) (i) < (j) ? (i) : (j) struct IO { #define MAXSIZE (1 << 20) #define isdigit(x) (x >= '0' && x <= '9') char buf[MAXSIZE], *p1, *p2; char pbuf[MAXSIZE], *pp; #if DEBUG #else IO() : p1(buf), p2(buf), pp(pbuf) {} ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); } #endif inline char gc() { #if DEBUG return getchar(); #endif if(p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin); return p1 == p2 ? ' ' : *p1++; } inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } template <class T> inline void read(T &x) { register double tmp = 1; register bool sign = 0; x = 0; register char ch = gc(); for(; !isdigit(ch); ch = gc()) if(ch == '-') sign = 1; for(; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0'); if(ch == '.') for(ch = gc(); isdigit(ch); ch = gc()) tmp /= 10.0, x += tmp * (ch - '0'); if(sign) x = -x; } inline void read(char *s) { register char ch = gc(); for(; blank(ch); ch = gc()); for(; !blank(ch); ch = gc()) *s++ = ch; *s = 0; } inline void read(char &c) { for(c = gc(); blank(c); c = gc()); } inline void push(const char &c) { #if DEBUG putchar(c); #else if(pp - pbuf == MAXSIZE) { fwrite(pbuf, 1, MAXSIZE, stdout); pp = pbuf; } *pp++ = c; #endif } template <class T> inline void write(T x) { if(x < 0) { x = -x; push('-'); } static T sta[35]; T top = 0; do{ sta[top++] = x % 10; x /= 10; }while(x); while(top) push(sta[--top] + '0'); } template <class T> inline void write(T x, char lastChar) { write(x); push(lastChar); } } io; int a[110], n, ans; int main() { io.read(n); rep(i, 1, n * 2) io.read(a[i]); std::sort(a + 1, a + 2 * n + 1); rep(i, 1, n) ans += a[2 * i - 1]; io.write(ans); }
1
#include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; unsigned long long int n; cin>>n; cin>>s; if(n>=s.size()) cout<<s; else{ s.erase(s.begin()+n,s.end()); s.append("..."); cout<<s; } }
#include <iostream> using namespace std; int main() { //入力を受け取る int D, T, S; cin >> D >> T >> S; //高橋君の移動時間を計算する int dist = S * T; //Dとdistの大小関係に応じて出力をする if (dist >= D) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
0
#include <bits/stdc++.h> using namespace std; #define rep(i, a) for(int i=0; i<(a); i++) typedef long long ll; #ifdef _DEBUG inline void dump() { cerr << endl; } template<typename Head> void dump(Head&& head) { cerr << head; dump(); } template<typename Head, typename... Tail> void dump(Head&& head, Tail&&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); } #define debug(...) do {cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false) #else #define dump(...) #define debug(...) #endif template<typename T> struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template<typename T> using Edges = vector<edge<T>>; template<typename T> using WeightedGraph = vector<Edges<T>>; using UnWeightedGraph = vector<vector<int>>; template<typename T> using Matrix = vector<vector<T>>; ///////////////////////////////////////////////////////////////////// const ll inf = 1LL<<60; int main() { ll n,l; cin>>n>>l; ll sum = n*(n-1)/2 + l*n; ll ms = inf; for (int i=0; i<n; i++) { if (abs(ms) > abs(i+l)) ms = i+l; } cout << sum - ms << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n,l; cin >> n >> l; int a[n]; int all = 0; for(int i = 1; i <= n; i++){ a[i] = l + i - 1; all += a[i]; } int minu = 0,maxmin = 2000000; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(j != i){ minu += a[j]; }//if }//for if(abs(minu - all) < abs(maxmin - all)){ maxmin = minu; } minu = 0; } cout << maxmin << endl; }
1
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define endl "\n" typedef long long ll; const double pi=3.14159265358979323846; int ctoi(const char c) { if ('0' <= c && c <= '9') return (c - '0'); return -1; } vector<int> input(int n) { vector<int> vec(n); for (int i = 0; i < n; i++) { cin >> vec.at(i); } return vec; } void output(vector<int> vec) { for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } return; } vector<vector<int>> input(int n, int m) { vector<vector<int>> table(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> table. at(i).at(j); } } return table; } void output(vector<vector<int>> table) { for (int i = 0; i < table.size(); i++) { for (int j = 0; j < table.at(0).size(); j++) { cout << table.at(i).at(j) << " "; } cout << endl; } } long long perm(int n, int r) { if (n < r) { cout << "error" << endl; return 0; } long long perm = 1; for (int i = n; i > n - r; i--) { perm *= i; } return perm; } long long comb(int n, int r) { if (n < r) { cout << "error" << endl; return 0; } long long comb = perm(n,r); for (int i = r; i > 0; i--) { comb /= i; } return comb; } long long homo(int n, int r) { return comb(n + r - 1, n - 1); } long long fact(int n) { long long fact = 1; for (int i = n; i > 0; i--) { fact *= i; } return fact; } int gcd(int a, int b){ if (a % b == 0){ return(b); }else{ return(gcd(b, a % b)); } } int lcm(int a, int b) { return a * b / gcd(a, b); } bool isprime(int n){ if (n < 2) return false; else if (n == 2) return true; else if (n % 2 == 0) return false; for (int i = 3; i <= sqrt(n); i += 2){ if (n % i == 0){ return false; } } return true; } vector<long long> divisors(long long N) { vector<long long> res; for (long long i = 1; i * i <= N; ++i) { if (N % i == 0) { res.push_back(i); // 重複しないならば i の相方である N/i も push if (N/i != i) res.push_back(N/i); } } // 小さい順に並び替える sort(res.begin(), res.end()); return res; /*long long N; cin >> N; vector<long long> res = divisors(N); for (int i = 0; i < res.size(); ++i) { cout << res[i] << " "; } cout << endl;*/ } vector<pair<long long, long long> > prime_factorize(long long N) { vector<pair<long long, long long> > res; for (long long a = 2; a * a <= N; ++a) { if (N % a != 0) continue; long long ex = 0; while (N % a == 0) { ++ex; N /= a; } res.push_back({a, ex}); } if (N != 1) res.push_back({N, 1}); return res; } void sayYes(bool f) { if (f) { cout << "Yes" << endl; } else { cout << "No" << endl; } } void sayYES(bool f) { if (f) { cout << "YES" << endl; } else { cout << "NO" << endl; } } int main() { ll a,b,c; cin >> a >> b >> c; rep(i,c/2){ if(a%2==1){ a--; } b+=(a/2); a/=2; if(b%2==1){ b--; } a+=(b/2); b/=2; } if(c%2==1){ if(a%2==1){ a--; } b+=(a/2); a/=2; } cout << a << " " << b << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) using namespace std; typedef long long ll; typedef pair<int, int> P; const ll INF = 1LL << 60; ll A, B, K; int main() { cin >> A >> B >> K; for (int i = 1; i <= K; i++) { if (i % 2 == 1) { // 高橋 if (A % 2 == 1) { A--; } ll tmp = A / 2; B += tmp; A -= tmp; } else { // 青木 if (B % 2 == 1) { B--; } ll tmp = B / 2; A += tmp; B -= tmp; } } cout << A << " " << B << endl;; }
1
#include <bits/stdc++.h> using namespace std; int main(void){ string s; int w; cin>>s>>w; for (int i=0;i+w<=s.size();i+=w) { cout<<s[i]; } if (s.size()%w!=0) { reverse(s.begin(),s.end()); string tmp=s.substr(0,s.size()%w); cout<<tmp[tmp.size()-1]; } cout<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { string str; int n; cin>>str>>n; string adj; int at = 0; while(at < str.size()) { adj.push_back(str[at]); at+=n; } cout<<adj<<endl; return 0; }
1
#include <stdio.h> int main(){ long long n, sum = 0; scanf("%lld", &n); for(long long i = 1 ; i <= n ; i++){ if(i%3 != 0 && i%5 != 0){ sum+=i; } } printf("%lld\n", sum); return 0; }
#include<bits/stdc++.h> using namespace std; #define LL long long #define N 20003 int n; int a[N]; string S,X,Y; map<pair<string,string>,LL>mp; LL ans; inline int read(){ int X=0,w=0; char ch=0; while (ch<'0' || ch>'9') w|=ch=='-',ch=getchar(); while (ch>='0' && ch<='9') X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w ? -X : X; } int main(){ n=read(); cin>>S; X=S.substr(0,n); Y=S.substr(n,n); reverse(Y.begin(),Y.end()); for (int i=0;i<1<<n;i++){ int res=i; string s1="",s2=""; for (int j=0;j<n;j++){ if (res%2==0) s1+=X[j]; else s2+=X[j]; res/=2; } mp[make_pair(s1,s2)]++; } for (int i=0;i<1<<n;i++){ int res=i; string s1="",s2=""; for (int j=0;j<n;j++){ if (res%2==0) s1+=Y[j]; else s2+=Y[j]; res/=2; } ans+=mp[make_pair(s1,s2)]; } printf("%lld\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll N; cin>>N; vector <ll> ans; // N--; while(N){ ll rest=(N-1)%26; N--; N/=26; ans.push_back(rest); } ll len=ans.size(); for(ll i=len-1;i>=0;i--){ char c='a'+ans[i]; cout<<c; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL <<60; void func(long long x){ if(x==0){ cout<<"a"; } else if(x==1){ cout<<"b"; } else if(x==2){ cout<<"c"; } else if(x==3){ cout<<"d"; } else if(x==4){ cout<<"e"; } else if(x==5){ cout<<"f"; } else if(x==6){ cout<<"g"; } else if(x==7){ cout<<"h"; } else if(x==8){ cout<<"i"; } else if(x==9){ cout<<"j"; } else if(x==10){ cout<<"k"; } else if(x==11){ cout<<"l"; } else if(x==12){ cout<<"m"; } else if(x==13){ cout<<"n"; } else if(x==14){ cout<<"o"; } else if(x==15){ cout<<"p"; } else if(x==16){ cout<<"q"; } else if(x==17){ cout<<"r"; } else if(x==18){ cout<<"s"; } else if(x==19){ cout<<"t"; } else if(x==20){ cout<<"u"; } else if(x==21){ cout<<"v"; } else if(x==22){ cout<<"w"; } else if(x==23){ cout<<"x"; } else if(x==24){ cout<<"y"; } else{ cout<<"z"; } } int main(void){ long long i,j; long long tmp=0; long long res; long k; cin>>k; long long base[13]; long long order[13]; base[0]=1; order[0]=0; for(i=1;i<13;i++){ base[i]=pow(26,i); order[i]=order[i-1]+base[i]; if(k<order[i]){ break; } else if(k==order[i]){ for(;i>0;i--){ cout<<"z"; } cout<<endl; return 0; } } tmp=k-order[i-1]-1; for(i=i-1;i>=0;i--){ res=tmp/base[i]; if(i==0){ func(res); } else{ func(res); } tmp=tmp%base[i]; } cout<<endl; return 0; }
1
#include <bits/stdc++.h> #include <iostream> #include <queue> #include <stack> #include <vector> #include <string> #include <set> #include <map> #include <random> #define rep(i,n) for (int i = 0; i < (n); ++i) #define repp(i,n,m) for (int i = m; i < (n); ++i) #define repl(i,n) for (long long i = 0; i < (n); ++i) #define reppl(i,n,m) for (long long i = m; i < (n); ++i) using namespace std; using ll = long long; using ld = long double; using P = pair<int, int>; using PL = pair<long long, long long>; using Pxy = pair<double, double>; using T = tuple<int, int, bool>; const int INF = 1001001007; const int mod = 1000000007; const int MOD = 998244353; const ll inf = 1e18; template <typename AT> void printvec(vector<AT> &ar){ rep(i,ar.size()-1) cout << ar[i] << " "; cout << ar[ar.size()-1] << endl; } template <typename Q> void printvvec(vector<vector<Q>> &ar){ rep(i,ar.size()){ rep(j,ar[0].size()-1) cout << ar[i][j] << " "; cout << ar[i][ar[0].size()-1] << endl; } } template <typename S> bool range(S a, S b, S x){ return (a <= x && x < b); } void yes(){ cout << "Yes" << endl; } void no (){ cout << "No" << endl; } ll cel (ll a, ll b){ if (a % b == 0) return a / b; else return a / b + 1; } ll gcds(ll a, ll b){ ll c = a % b; while (c != 0){ a = b; b = c; c = a % b; } return b; } int main() { int n, m; cin >> n >> m; if (n % m == 0) cout << 0 << endl; else cout << 1 << endl; }
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; #define ll long long #define ld long double #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define PI 3.14159265358979323846 #define sz(x) ((int)(x).size()) #define chmin(x,y) x = min(x,y) #define chmax(x,y) x = max(x,y) #define all(x) (x).begin(),(x).end() const int INF = 1e9; const int MOD = 1e9 + 7; const ll LINF = 1e18; int main() { int n,r;cin >> n >> r; if(n>=10) { cout << r << endl; return 0; }else { cout << r+100*(10-n) << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; int hund, ten, one, ans; for (int i=n ; ; i++){ hund = i/100; ten = (i-hund*100)/10; one = i-hund*100-ten*10; if (hund == ten && ten == one){ ans = i; break; } } cout << ans << endl; }
#include<bits/stdc++.h> // DEEP using namespace std; #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define fwd(i,a,n) for(int i=a;i<=n;i++) #define bak(i,a,n) for(int i=a;i>=n;i--) #define all(v) v.begin(),v.end() #define pb push_back #define lop '\n' #define mp make_pair typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<long long> vll; typedef pair<int,int> pi; typedef set<int> si; const ll mod = 1e9 + 7; const ll mod1=998244353; //power ull power(ull x,ull y){ if(y==0) return 1; else{ if(y%2==0) return power(x*x,y/2); else return x*power(x*x,(y-1)/2); } } // Modular Exponentiation ll mod_power(ll x, ll y, ll m) { ll r = 1; // Initialize result x = x % m; // Update x if it is more than or equal to m if (x == 0) return 0; // In case x is divisible by m; while (y > 0) { // If y is odd, multiply x with result if (y & 1) r = (r*x) % m; // y must be even now y = y/2; x = (x*x) % m; } return r; } //GCD ull gcd(ull x,ull y){ if(y==0)return x; return gcd(y,x%y); } // EXTENDED_EUCLID vector<ll>extended_Euclid(ll a,ll b){ vector<ll>v(3); if(b==0){ v[0]=a;v[1]=1;v[2]=0; return v; } else{ vector<ll>v1 = extended_Euclid(b,a%b); //recursive call v[0]=v1[0]; v[1]=v1[2]; v[2]=v1[1]-(a/b)*v1[2]; return v; } } // DFS ( number of connected components ) //vector<vector<ll> >adj(200001); //bool visited[200001]; /*ll dfs(int x,int c){ // x - > node c - > number of edges in the component if(visited[x])return c; else{ visited[x]=true; int y=c; for(int i=0;i<adj[x].size();i++){ if(!visited[adj[x][i]]){ c+=1; int d=dfs(adj[x][i],y); c+=d; } } return c; } } */ // SIEVE /* bool isPrime[100001]; void sieve(int N) { for(int i = 0; i <= N;++i) { isPrime[i]=true; } isPrime[0]=false; isPrime[1]=false; for(int i = 2; i * i <= N; ++i) { if(isPrime[i] == true) { for(int j = i * i; j <= N ;j += i) isPrime[j] = false; } } } */ // Binary Search /* ll bin_search(ll y){ while(lo<hi){ x=lo+(hi-lo)/2; if(condition){ ..... } else{ // increase lo or decrease hi suitably } } } */ int main(){ boost; int TESTS=1; //cin>>TESTS; while(TESTS--){ string s; cin>>s; if(s[0]==s[1] && s[0]==s[2])cout<<s<<endl; else{ if(s[1]>s[0]){ char x=s[0]; x++; cout<<x<<x<<x<<endl; } else{ if(s[1]==s[0]){ if(s[2]<s[1])cout<<s[0]<<s[0]<<s[0]<<endl; else{ char x=s[0]; x++; cout<<x<<x<<x<<endl; } } else{ cout<<s[0]<<s[0]<<s[0]<<endl; } } } } } /* Be careful about integer ranges; Check the corner cases before submitting. */
1
#include <iostream> #include <vector> #include <string> #include <list> #include <queue> #include <algorithm> #define rep(i, n) for(i = 0; i < (n); i++) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define MOD 1000000007 #define PI 3.14159265358979323846 #define INF 1 << 30 using namespace std; typedef long long ll; typedef pair<int, int> pp; int main(void) { int num, i, k = 0, ans = 0; string s; cin >> s; vector<vector<ll>> dp(s.size() + 1, vector<ll>(2, 0)); dp[0][1] = 1; rep(i, s.size()) { int n = s[i] - '0'; dp[i + 1][0] = min(dp[i][0] + n, dp[i][1] + 10 - n); dp[i + 1][1] = min(dp[i][0] + n + 1, dp[i][1] + 10 - n - 1); } cout << dp[s.size()][0] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define REP(i, n) for(int (i) = 0; (i) < (n); ++(i)) #define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i)) #define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i)) constexpr int INF = 1e9; // constexpr ll INF = 1LL<<61; constexpr ll mod = 1e9+7; int main(){ string S; cin >> S; ll ans = 0; int N = S.size(); bool flag = false; REPR(i, N-1){ int d = S[i] - '0'; if(flag) d++; flag = false; if(d < 5){ ans += d; } else if(d > 5){ ans += 10 - d; flag = true; } else{ if(i != 0 && S[i-1] >= '5'){ ans += d; flag = true; } else{ ans += d; } } } if(flag) ans++; cout << ans << endl; return 0; }
1
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int n,hang[8],lie[8],zuo[15],you[15];//标记行,列,斜下,斜上 int book[8][8];//控制输出 void print() { for(int i=0; i<8; i++) { for(int j=0; j<8; j++) { if(book[i][j]) { if(hang[i]!=j) return ; } } } for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { if(hang[i]==j) printf("Q"); else printf("."); } printf("\n"); } } void dfs(int x) { if(x==8)//判断输出 { print();//输出后直接返回 return ; } for(int i=0; i<8; i++) { if(lie[i]==1||zuo[i+x]==1||you[x-i+8-1]==1) { continue; } hang[x]=i; lie[i]=zuo[i+x]=you[x-i+8-1]=1;//标记列,斜下,斜上 dfs(x+1);//往下递归 hang[x]=lie[i]=zuo[x+i]=you[x-i+8-1]=-1;//取消标记 } return ; } int main() { while(~scanf("%d",&n)) { memset(hang,-1,sizeof(hang)); memset(lie,-1,sizeof(lie)); memset(zuo,-1,sizeof(zuo)); memset(you,-1,sizeof(you)); memset(book,0,sizeof(book)); int a,b; for(int i=0; i<n; i++) { scanf("%d%d",&a,&b); book[a][b]=1; } dfs(0); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #define int long long #define dotimes(i, n) for (int i : range(n)) using namespace std; class range { class iterator { int i, s; public: iterator(int i, int s): i(i), s(s) {} int& operator*() { return i; } void operator++() { i += s; } bool operator!=(iterator const& that) { return i < that.i; } }; iterator b, e; public: range(int e): b({0, 1}), e({e, 1}) {} range(int b, int e): b({b, 1}), e({e, 1}) {} range(int b, int e, int s): b({b, s}), e({e, s}) {} iterator begin() { return b; } iterator end() { return e; } }; int rint() { int n; scanf("%lld", &n); return n; } void wint(int n) { printf("%lld\n", n); } template<typename T> inline int size(T container) { return static_cast<int>(container.size()); } template<typename T> inline bool maxs(T& a, T const& b) { if (a < b) { a = b; return true; } return false; } template<typename T> inline bool mins(T& a, T const& b) { if (a > b) { a = b; return true; } return false; } signed main() { const int k = rint(); int rf[8] = {}, p[8 - k]; bool cf[8] = {}; dotimes(i, k) { int r = rint(); int c = rint(); rf[r] = c + 1; cf[c] = true; } for (int i = 0, j = 0, c = 0; i < 8; i++) if (!rf[i]) { while (cf[c]) c++; p[j++] = ++c; } int a[8]; for (;; next_permutation(p, p + 8 - k)) { for (int i = 0, j = 0; i < 8; i++) a[i] = rf[i] ? rf[i] : p[j++]; dotimes(i, 8) dotimes(j, i) if (abs(a[j] - a[i]) == abs(j - i)) goto failed; break; failed: ; } dotimes(i, 8) { dotimes(j, 8) putchar(a[i] == j + 1 ? 'Q' : '.'); putchar('\n'); } return 0; }
1
#include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define LL long long #define int LL #define FOR(i,a,b) for(int i = (a); i <= (b); i++) #define RE(i,n) FOR(i,1,n) #define REP(i,n) FOR(i,0,(int)(n)-1) #define R(i,n) REP(i,n) #define VI vector<int> #define PII pair<int,int> #define VPII vector<PII> #define LD long double int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); int d,t,s; cin >> d >> t >> s; if((((d%s) == 0) ? (d/s) : ((d+(s-(d%s)))/s)) > t){ cout << "No" << endl; }else{ cout << "Yes" << endl; } }
#include<iostream> using namespace std; int main(void) { int D,S,T; cin>>D>>T>>S; int s=T*S; if(s>=D){ cout<<"Yes"; } else{ cout<<"No"; } return 0; }
1
// clang-format off #include <bits/stdc++.h> #define int long long #define main signed main() // #define main int main() #define loop(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) loop(i, 0, n) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define prec(n) fixed << setprecision(n) #define stlice(from, to) substr(from, (to) - (from) + 1) #define pb push_back #define mp make_pair #define mt make_tuple #define fi first #define se second using namespace std; using pii = pair<int, int>; using vi = vector<int>; using vd = vector<double>; using vc = vector<char>; using vb = vector<bool>; using vs = vector<string>; using vpii = vector<pii>; using vvi = vector<vi>; using vvb = vector<vb>; using vvpii = vector<vpii>; template<typename A> using fn = function<A>; constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000; constexpr int MOD = 1000000007; constexpr double PI = acos(-1); template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; } template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; } constexpr bool odd(const int &n) { return n & 1; } constexpr bool even(const int &n) { return !odd(n); } void solve(); main { solve(); return 0; } // clang-format on vector<int> eratosthenes(const int &n) { vector<int> v(n); v[0] = v[1] = false; loop(i, 2, v.size()) v[i] = true; for (int i = 2; i * i < n; i++) { if (v[i]) { for (int j = i * 2; j < n; j += i) { v[j] = false; } } } return v; } void solve() { int n; while (cin >> n, n) { vi v = eratosthenes(n + 1); vi w; rep(i, v.size()) if (v[i]) w.pb(i); reverse(all(w)); loop(i, 1, w.size()) { if (w[i - 1] == w[i] + 2) { cout << w[i] << ' ' << w[i - 1] << endl; break; } } } }
#include <iostream> #include <algorithm> #include <string.h> #include <vector> #include <map> using namespace std; int main(){ int h,w; int i,j; int flag; while(cin>>h>>w,h|w){ int taikaku; int tmph=1111,tmpw=1111; taikaku=h*h+w*w; flag=false; for(i=1;i<150;i++){ for(j=1;j<150;j++){ //if(i==4&&j==8) cout<<"a"<<endl; if(i>=j||(i==h&&j==w)) continue; if(i*i+j*j==taikaku) { if(h<i){ tmph=i; tmpw=j; flag=true; break; } } else if(i*i+j*j>taikaku&&i*i+j*j<tmph*tmph+tmpw*tmpw) { tmph=i; tmpw=j; } } if(flag) break; } cout<<tmph<<" "<<tmpw<<endl; } return 0; }
0
#include <iostream> #include <vector> #include <algorithm> int main() { int n = 1; std::cin >> n; while (n) { std::vector<int32_t> num_list{}; while (n--) { int tmp = 0; std::cin >> tmp; num_list.push_back(tmp); } std::vector<int32_t> comfarm_list{}; std::vector<int32_t> sum_list{}; for( auto num : num_list ) { for (auto& sum : sum_list) { if (num < 0) { comfarm_list.push_back(sum); } sum += num; } sum_list.push_back(num); } for (auto sum : sum_list) { comfarm_list.push_back(sum); } int32_t max_sum = *( std::max_element(comfarm_list.begin(),comfarm_list.end()) ); std::cout << max_sum << std::endl; std::cin >> n; } return 0; }
#include <cstdio> #include <vector> #include <algorithm> using namespace std; int do_stuff(int n) { vector<int> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } int max_so_far = 0; int max_ending_here = 0; for (int i = 0; i < n; ++i) { int curr = a[i]; max_ending_here = max(max_ending_here + curr, 0); max_so_far = max(max_so_far, max_ending_here); } if (max_so_far == 0) { max_so_far = *max_element(a.begin(), a.end()); } return max_so_far; } int main(int argc, char* argv[]) { //freopen("input.txt", "r", stdin); int n; scanf("%d", &n); while (n != 0) { int ans = do_stuff(n); printf("%d\n", ans); scanf("%d", &n); } return 0; }
1
#include <stdio.h> #include <string.h> #include <limits.h> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <map> #include <set> #include <queue> typedef unsigned long long ULLONG; typedef long long LLONG; static const LLONG MOD_NUM = 1000000007; template<class _T> static void getint(_T& a) { const char* fmt = " %d"; if (sizeof(_T) == sizeof(long long)) { fmt = " %lld"; } if (scanf(fmt, &a) < 0) { printf("g1int Error\n"); } } template<class _T> static void getint(_T& a, _T& b) { const char* fmt = " %d %d"; if (sizeof(_T) == sizeof(long long)) { fmt = " %lld %lld"; } if (scanf(fmt, &a, &b) < 0) { printf("g2int Error\n"); } } template<class _T> static void getint(_T& a, _T& b, _T& c) { const char* fmt = " %d %d %d"; if (sizeof(_T) == sizeof(long long)) { fmt = " %lld %lld %lld"; } if (scanf(fmt, &a, &b, &c) < 0) { printf("g3int Error\n"); } } static void AGC005A(); #if 1 int main() { AGC005A(); fflush(stdout); return 0; } #endif static void AGC005A() { std::string orgs; std::cin >> orgs; int sCount = 0, eraseCount = 0; int len = orgs.length(); for (int i = 0; i < len; i++) { if (orgs[i] == 'S') { sCount++; } else { if (sCount) { sCount--; eraseCount += 2; } } } printf("%d\n", len - eraseCount); }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; int main() { string s; cin >> s; int ans = 0; int c = 0; rep(i, s.size()) { if (s[i] == 'T') ++c; else --c; ans = max(ans, 2 * c); } cout << ans << endl; return 0; }
1
#include <iostream> #include <iomanip> #include <string> #include <algorithm> #include <vector> #include <cmath> #include <vector> using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rep2(i,s,n) for(int i = (s);i<(int)(n);i++) int main(){ int n , k; cin >> n >> k; int s , t; s = k + n; t = k - n + 1; rep2(i,t,s)cout << i << " " ; }
#include <iostream> #include <complex> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <numeric> #include <cstring> #include <ctime> #include <cstdlib> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <cmath> #include <bitset> #include <cassert> #include <queue> #include <stack> #include <deque> #include <random> #include <iomanip> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; typedef long long int ll; typedef long double ld; typedef pair<ll,ll> P; typedef pair<P,P> PP; const ll MOD=1000000007; const ll MAX_N=500010; const ll INF=9999999999; int main(){ int n,y; cin>>n>>y; int ans[]={-1,-1,-1}; bool f=true; for(int i=0;i<=n;i++){ for(int j=0;j<=n;j++){ if(i+j<=n){ int k=n-i-j; int val=i*10000+j*5000+1000*k; if(val==y){ ans[0]=i; ans[1]=j; ans[2]=k; f=false; break; } } } if(!f) break; } cout<<ans[0]<<" "<<ans[1]<<" "<<ans[2]<<endl; }
0
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) using namespace std; #define INF ((1<<30)-1) #define LINF (1LL<<60) #define EPS (1e-10) typedef long long ll; typedef pair<ll, ll> P; const int MOD = 1000000007; const int MOD2 = 998244353; // ref : https://atcoder.jp/contests/jsc2019-qual/submissions/16521703 int main(){ int n; cin >> n; string s; cin >> s; string t; char c = 'W'; rep(i, 2 * n){ if (s[i] != c){ t += 'L'; if (c == 'W') c = 'B'; else c = 'W'; } else { t += 'R'; if (c == 'W') c = 'B'; else c = 'W'; } } int cnt = 0; rep(i, 2 * n) cnt += (t[i] == 'L'); if (cnt != n) { cout << 0 << endl; return 0; } ll ans = 1; ll l = 0; rep(i, 2 * n){ if (t[i] == 'L'){ l++; } else { ans *= l; ans %= MOD; l--; } } for(ll i = 1; i <= n; i++){ ans *= i; ans %= MOD; } cout << ans << endl; return 0; }
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef int_fast64_t ll; template<int N, typename T, T M> struct ModFact { T fact[N+1]; constexpr ModFact() : fact() { fact[0] = 1; for (int i=0; i<N; ++i) fact[i+1] = (fact[i]*(i+1))%M; } }; ModFact<100000, ll, MOD> fact; ll N, M, ans = 1; int tmp, cnt = 0; constexpr ll th = 1LL<<45; char S[200000]; int main() { scanf("%lld\n", &N); M = N << 1; fread_unlocked(S, 1, M, stdin); for(size_t i = 0; i < M;) { tmp = (S[i++]=='B') + cnt; if(tmp&1) cnt++; else { ans *= cnt; if(ans > th) ans %= MOD; if(--cnt < 0) break; } } ans = cnt == 0 ? ((ans % MOD) * fact.fact[N]) % MOD : 0; printf("%lld\n", ans); return 0; }
1
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<math.h> #include<string> #include<string.h> #include<stack> #include<queue> #include<vector> #include<utility> #include<set> #include<map> #include<stdlib.h> #include<iomanip> using namespace std; #define ll long long #define ld long double #define EPS 0.0000000001 #define INF 1e9 #define MOD 1000000007 #define rep(i,n) for(i=0;i<n;i++) #define loop(i,a,n) for(i=a;i<n;i++) #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) #define first F #define second S typedef vector<int> vi; typedef pair<int,int> pii; int main(void) { int i,j; int n; cin>>n; vi v(n); rep(i,n) cin>>v[i]; sort(all(v)); for(i=1;i<=v[0];i++){ int c=0; rep(j,n)if(v[j]%i==0)c++; if(n==c)cout<<i<<endl; } }
#include <cstdio> int n,a[3]; int gdb(int a,int b){ if(b == 0){ return a; } return gdb(b,a%b); } int main(){ scanf("%d",&n); for(int i = 0; i < n; i++){ scanf("%d",&a[i]); } int g; if(n == 2){ g = gdb(a[0],a[1]); } else{ g = gdb(gdb(a[0],a[1]),a[2]); } for(int i = 1; i <= g/2; i++){ if(g % i == 0){ printf("%d\n",i); } } printf("%d\n",g); return 0; }
1
#include <iostream> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <queue> #include <stack> #include <vector> #include <algorithm> #include <map> #include <set> #include <deque> #include <numeric> using lint = long long; int main() { lint h, w, a, b; scanf("%lld", &h); scanf("%lld", &w); scanf("%lld", &a); scanf("%lld", &b); for (int i = 0; i < h; ++i) { if (i < h - b) printf("%s\n", (std::string((w - a), '1') + std::string(a, '0')).c_str()); else printf("%s\n", (std::string((w - a), '0') + std::string(a, '1')).c_str()); } return 0; }
#include <bits/stdc++.h> #define ll long long #define ulli unsigned long long int #define ld long double #define endl "\n" using namespace std; void solve() { int a, b, m; cin >> a >> b >> m; vector<int> price_a(a); vector<int> price_b(b); int min_a = INT_MAX; for (int& x: price_a) { cin >> x; min_a = min(min_a, x); } int min_b = INT_MAX; for (int& x: price_b) { cin >> x; min_b = min(min_b, x); } int ans = min_a + min_b; int x, y ,z; for (int i = 0; i < m; i++) { cin >> x >> y >> z; ans = min((price_a[x-1] + price_b[y-1] - z), ans); } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; //cin >> t; while (t--) { solve(); } cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; return 0; }
0
#include<stdio.h> #include <algorithm> #include <cassert> #include <cctype> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <iomanip> #include <stack> #include <string> #include <vector> using namespace std; #define ll long long #define ld long double #define rep(a,t) for(int a=0;a<t;a++) #define forever while(true) #define Sort(a) sort(a.begin(),a.end()) #define Reverse(a) reverse(a.begin(),a.end()) #define pb push_back int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; rep(i, n) { int base[3]; int cnt = 0; int out = 0; rep(i123, 3) { base[i123] = 0; } while (true) { string thistime; cin >> thistime; if (thistime == "HIT") { cnt += base[2]; base[2] = base[1]; base[1] = base[0]; base[0]=1; } else if (thistime == "OUT") { out++; if (out == 3) { break; } } else { cnt += base[2] + base[1] + base[0] + 1; base[2] = base[1] = base[0] = 0; } } cout << cnt << endl; } return 0; }
#include <bits/stdc++.h> #define x first #define y second using namespace std; using i64 = long long; using f64 = double; using pll = pair<i64, i64>; using pii = pair<int, int>; using ptx = pair<f64, f64>; i64 mp[256]; int main() { #ifdef HOME freopen("agc019b.in", "r", stdin); freopen("agc019b.out", "w", stdout); #endif ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); string str; cin >> str; for (auto i: str) mp[i]+= 1; i64 ans = str.size() * (str.size() - 1) / 2; for (char ch = 'a'; ch <= 'z'; ++ch) ans-= mp[ch] * (mp[ch] - 1) / 2; cout << 1 + ans << endl; return 0; }
0
#include<iostream> #include<iomanip> #include<vector> #include<map> #include<set> #include<stack> #include<queue> #include<string> #include<utility> #include<numeric> #include<algorithm> #include<cmath> #include<climits> using namespace std; using ll = long long; class Stream{ public: Stream(){ } //配列以外の入力を受け取る void read(){ } template<typename First, typename... Rest> void read(First& first, Rest&... rest){ cin >> first; read(rest...); } //配列を区切って出力する template<typename T> void write_vec(vector<T>& v, char divider){ for(size_t i = 0; i < v.size(); i++){ cout << v[i] << divider; } } }; int main(){ Stream io; int N, M, X, Y; io.read(N, M, X, Y); vector<int> x(N); vector<int> y(M); for(int i = 0; i < N; i++){ io.read(x[i]); } for(int i = 0; i < M; i++){ io.read(y[i]); } sort(x.begin(), x.end()); sort(y.begin(), y.end()); if(X < Y && x.back() < y.front() && X < y.front() && x.back() < Y) cout << "No War"; else cout << "War"; return 0; }
#include <bits/stdc++.h> typedef long long ll; #define rep(i, n) for (ll i = 0; i < (ll)n; ++i) const int MOD = 1000000007; using namespace std; int main(void) { int n, m, X, Y; cin >> n >> m >> X >> Y; vector<int> x(n), y(m); rep(i, n) cin >> x[i]; rep(i, m) cin >> y[i]; int mx = *max_element(x.begin(), x.end()); int mn = *min_element(y.begin(), y.end()); string ans = "War"; for (int z = -99; z <= 100; z++) { if (X < z && z <= Y && mx < z && z <= mn) ans = "No War"; } cout << ans << endl; }
1
#include <algorithm> #include <iostream> #include <vector> #include <queue> #include <tuple> using namespace std; const long long INF = 1LL << 60; // 1.15x10^18 template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using edge = pair<int, long long>; using Graph = vector<vector<edge>>; vector<long long> dijkstra(const Graph &g, int s) { vector<long long> dist(g.size(), INF); using Pi = pair<long long, int>; priority_queue<Pi, vector<Pi>, greater<Pi>> que; dist[s] = 0; que.emplace(dist[s], s); while (!que.empty()) { long long cost; int u; tie(cost, u) = que.top(); que.pop(); if (dist[u] < cost) continue; for (auto &e: g[u]) { int v; long long nc; tie(v, nc) = e; if (chmin(dist[v], dist[u] + nc)) que.emplace(dist[v], v); } } return dist; } int main() { int n, k; while (cin >> n >> k, n) { Graph g(n); while (k--) { int com; cin >> com; if (com == 0) { int a, b; cin >> a >> b; a--, b--; auto res = dijkstra(g, a)[b]; cout << (res == INF ? -1 : res) << endl; } else if (com == 1) { int c, d, e; cin >> c >> d >> e; c--, d--; g[c].emplace_back(d, e); g[d].emplace_back(c, e); } } } return 0; }
#include<stdio.h> #define INF 100000001 int n,m; int i,j,k; int t[100][100]; int r,a,b,c; bool used[100]; int d[100]; int ans; int min(int a,int b){ if(a<=b)return a; else return b; } int solve(int a,int b){ int v; while(1){ v=-1; for(k=0;k<n;k++)if(!used[k]&&(v==-1||d[k]<d[v]))v=k; if(d[v]==INF||v==-1)break; used[v]=true; for(k=0;k<n;k++){ if(t[v][k]!=INF)d[k]=min(d[k],d[v]+t[v][k]); } } return d[b]; } int main(){ while(1){ scanf("%d %d",&n,&m); if(n==0&&m==0)return 0; for(i=0;i<n;i++)for(j=0;j<n;j++)t[i][j]=INF; for(i=0;i<m;i++){ scanf("%d",&r); if(r==1){ scanf("%d %d %d",&a,&b,&c); t[a-1][b-1]=min(t[a-1][b-1],c); t[b-1][a-1]=min(t[b-1][a-1],c); } if(r==0){ scanf("%d %d",&a,&b); for(j=0;j<n;j++){ used[j]=false; d[j]=INF; } d[a-1]=0; ans=solve(a-1,b-1); if(ans==INF)printf("-1\n"); else printf("%d\n",ans); } } } }
1
#include <bits/stdc++.h> #define l first #define r second using namespace std; typedef pair<int, int> ii; ii p[100005]; int main() { int N; cin >> N; for (int i = 0; i < N; ++i) cin >> p[i].l >> p[i].r; int l = 0, r = 1e9; int x, y; for (int i = 0; i < N; ++i) { if (p[i].l > l) { l = p[i].l; x = i; } if (p[i].r < r) { r = p[i].r; y = i; } } int ans = 0; for (int i = 0; i < N; ++i) { ans = max(max(p[i].r - p[i].l + 1, 0), ans); } ans += max(r - l + 1, 0); if (x == y) { cout << ans << '\n'; return 0; } sort(p, p + N, [](const ii& a, const ii& b) { return a.r < b.r || (a.r == b.r && a.l > b.l); }); int s = 0; for (int i = 0; i < N - 1; ++i) { s = max(p[i].l, s); ans = max(max(r - s + 1, 0) + max(p[i + 1].r - l + 1, 0), ans); } cout << ans << '\n'; }
#include<bits/stdc++.h> #include<tr1/unordered_map> #define ri register int using namespace std; #define fi first #define se second typedef long long ll; typedef pair<int,int> pii; typedef vector<int> poly; #define pb push_back const int rlen=1<<18|1,inf=0x3f3f3f3f; const ll Inf=1e18; char buf[rlen],*ib=buf,*ob=buf; #define gc() (((ib==ob)&&(ob=(ib=buf)+fread(buf,1,rlen,stdin)),ib==ob)?-1:*ib++) inline int read() { int ans=0; bool f=1; char ch=gc(); while(!isdigit(ch)) f^=ch=='-',ch=gc(); while(isdigit(ch)) ans=((ans<<2)+ans<<1)+(ch^48),ch=gc(); return f?ans:-ans; } inline ll readl() { ll ans=0; bool f=1; char ch=gc(); while(!isdigit(ch)) f^=ch=='-',ch=gc(); while(isdigit(ch)) ans=((ans<<2)+ans<<1)+(ch^48),ch=gc(); return f?ans:-ans; } inline int Read(char*s) { int tp=0; char ch=gc(); while(!isdigit(ch)&&!isalpha(ch)) ch=gc(); while(isdigit(ch)||isalpha(ch)) s[++tp]=ch,ch=gc(); return tp; } namespace modular { const int mod=1e9+7; inline int add(int a,int b) { return a+b<mod?a+b:a+b-mod; } inline int dec(int a,int b) { return a<b?a-b+mod:a-b; } inline int mul(int a,int b) { return (ll)a*b%mod; } inline void Add(int&a,int b) { a=a+b<mod?a+b:a+b-mod; } inline void Dec(int&a,int b) { a=a<b?a-b+mod:a-b; } inline void Mul(int&a,int b) { a=(ll)a*b%mod; } inline int ksm(int a,int p) { int ret=1;for(;p;p>>=1,Mul(a,a)) (p&1)&&(Mul(ret,a),1);return ret; } inline int Inv(int a) { return ksm(a,mod-2); } inline int sqr(int a) { return mul(a,a); } inline int cub(int a) { return (ll)a*a%mod*a%mod; } } using namespace modular; template<typename T> inline void ckmax(T&a,T b) { a<b?a=b:0; } template<typename T> inline void ckmin(T&a,T b) { a>b?a=b:0; } template<typename T> inline T gcd(T a,T b) { T t;while(b)t=a,a=b,b=t-t/a*a;return a; } template<typename T> inline T Abs(T x) { return x<0?-x:x; } inline int rd() { return rand()|(rand()<<15); } const int N=5e5+5; int n,pre[N],suf[N]; pii a[N]; int main() { #ifdef ldxcaicai freopen("lx.in","r",stdin); #endif n=read(); for(ri i=1;i<=n;++i) a[i].fi=read(),a[i].se=read(); sort(a+1,a+n+1); pre[0]=0x3f3f3f3f,suf[n]=0x3f3f3f3f; for(ri i=1;i<n;++i) pre[i]=min(pre[i-1],a[i].se); for(ri i=n-1;i;--i) suf[i]=min(suf[i+1],a[i].se); int res=0; for(ri mn,r1,r2,t,i=1;i<n;++i) { r1=a[i].se,r2=min(max(suf[i+1],a[n].fi-1),a[n].se); t=(r1-a[i].fi+1)+(r2-a[n].fi+1); mn=pre[i-1]; ckmax(res,t-max(0,min(r1-max(a[i].fi-1,mn),r2-max(a[n].fi-1,mn)))); } cout<<res; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define ll long long #define mo 1000000007 int i,j,k,m,n,x,y,z,S,Ans,ans,dp[41][1<<17]; inline ll Pow(ll x,ll d) { ll ans = 1, y = x; for (; d; y = y * y % mo, d >>= 1)d & 1 ? ans = ans * y % mo : 0; return ans; } int main() { cin >> n >> x >> y >> z; S = (1 << x + y + z) - 1; ans = (1 << x - 1) | (1 << x + y - 1) | (1 << x + y + z - 1); dp[0][0] = 1; for (i = 1; i <= n; ++i) for (j = 0; j <= S; ++j) if (dp[i - 1][j]) for (k = 1; k <= 10; ++k) { int o = ((j << k) | (1 << k - 1)) & S; if ((o & ans) == ans)(Ans += 1ll * dp[i - 1][j] * Pow(10, n - i) % mo) %= mo; else (dp[i][o] += (dp[i - 1][j]) % mo) %= mo; } printf("%d\n", Ans); }
#include<bits/stdc++.h> using namespace std; #define MOD 1000000007 long long N, M, K; long long res = 0; // mod. m での a の逆元 a^{-1} を計算する 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; } long long ModComb(long long n, long long a, long long mod){ long long i = 0; long long res = 1; while(a > 0){ res *= ((n-i) * Modinv(a, MOD)) % MOD; res %= MOD; --a; ++i; } return res; } int main(){ cin >> N >> M >> K; long long MC = ModComb(N * M - 2, K - 2, MOD); for(int d = 1; d < N; d++) res = ((d * ((((N-d) * M * M) * MC) % MOD)) % MOD + res) % MOD; for(int d = 1; d < M; d++) res = ((d * ((((M-d) * N * N) * MC) % MOD)) % MOD + res) % MOD; cout << res << endl; return 0; }
0
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair<int, int>; int const MOD = 998244353; long long modpow(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res = res * a % MOD; a = a * a % MOD; n >>= 1; } return res; } int main() { ll N; cin >> N; ll MAX = 0; vector<ll> D(N, 0); rep(i, N) { ll d; cin >> d; if ((i == 0 && d != 0) || (i != 0 && d == 0)) { cout << 0 << endl; return 0; } MAX = max(MAX, d); ++D[d]; } ll ans = 1; rep(i, MAX + 1) { if (i == 0) continue; if (D[i] == 0) { ans = 0; break; } ans = (ans * modpow(D[i - 1], D[i])) % MOD; } cout << ans << endl; }
#include<bits/stdc++.h> #define mp make_pair #define pb push_back #define fs first #define sc second using namespace std; typedef long long ll; int N; map<ll, int> h; vector<ll> v; string s; pair<ll, ll> frac[200100]; ll cnt[110][110]; pair<ll, ll> getFrac(string &s) { ll x = 0, y = 1; bool dec = false; for(int i=0;i<s.size();++i) { if(s[i] == '.') { dec = true; continue; } x = x*10 + (s[i] - '0'); if(dec) y = y*10; } ll d = __gcd(x, y); x /= d; y /= d; if(!h[y]) { h[y] = 1; v.pb(y); } return mp(x, y); } int main() { cin>>N; ll ret = 0; for(int i=0;i<N;++i) { cin>>s; frac[i] = getFrac(s); } sort(v.begin(), v.end()); for(int i=0;i<v.size();++i) h[v[i]] = i; for(int i=0;i<N;++i) { ll a = frac[i].fs, b = frac[i].sc; for(int j=0;j<v.size();++j) { if(a % v[j] == 0) { ret += cnt[h[b]][j]; } } for(int j=0;j<v.size();++j) { if(a % v[j] == 0) { ++cnt[j][h[b]]; } } } cout<<ret<<"\n"; }
0
#include<bits/stdc++.h> #define _ ios_base::sync_with_stdio(0);cin.tie(0); #define REP(i,n) for(int i=0;i<(int)(n);i++) using namespace std; class dice{ int* me; public: dice(int me_[6]){ me = me_; } int Top(){ return me[0]; } void East(){ swap(me[0], me[3]); swap(me[3], me[5]); swap(me[5], me[2]); } void West(){ swap(me[0], me[2]); swap(me[2], me[5]); swap(me[5], me[3]); } void North(){ swap(me[0], me[1]); swap(me[1], me[5]); swap(me[5], me[4]); } void South(){ swap(me[0], me[4]); swap(me[4], me[5]); swap(me[5], me[1]); } }; int main(){ _; int me[6]; REP(i,6) cin>>me[i]; dice d{me}; char c; while(cin>>c){ if(c=='E'){ d.East(); }else if(c=='W'){ d.West(); }else if(c=='N'){ d.North(); }else if(c=='S'){ d.South(); } } cout<<d.Top()<<endl; }
#include<iostream> #include<string> using namespace std; struct dice{ int top; int front; int left; int right; int back; int bottom; }; int main(){ //????????????????????¢?????°????????\????????? struct dice X; cin >> X.top; cin >> X.front; cin >> X.right; cin >> X.left; cin >> X.back; cin >> X.bottom; //?????¢??????????????\????????? string Rotation; cin >> Rotation; //????????????????????¢????????? for(int i = 0; i < Rotation.size(); i++){ if(Rotation[i] == 'N'){ struct dice Y; Y.top = X.front; Y.front = X.bottom; Y.left = X.left; Y.right = X.right; Y.back = X.top; Y.bottom = X.back; X = Y; } if(Rotation[i] == 'E'){ struct dice Y; Y.top = X.left; Y.front = X.front; Y.left = X.bottom; Y.right = X.top; Y.back = X.back; Y.bottom = X.right; X = Y; } if(Rotation[i] == 'S'){ for(int j = 0; j < 3; j++){ struct dice Y; Y.top = X.front; Y.front = X.bottom; Y.left = X.left; Y.right = X.right; Y.back = X.top; Y.bottom = X.back; X = Y; } } if(Rotation[i] == 'W'){ for(int j = 0; j < 3; j++){ struct dice Y; Y.top = X.left; Y.front = X.front; Y.left = X.bottom; Y.right = X.top; Y.back = X.back; Y.bottom = X.right; X = Y; } } } //?????¢??????????????¨????????? cout << X.top << endl; return 0; }
1
#include<iostream> using namespace std; int n,res=0; bool map[20][20]; bool fh[10],fc[10],fl[20],fr[20]; void print() { for(int i=0;i!=8;++i) { for(int j=0;j!=8;++j) { if(map[i][j]) cout<<'Q'; else cout<<'.'; } cout<<endl; } } bool check(int x,int y) { return !(fh[x] || fc[y] || fr[x+y] || fl[x+n-y+1]); } void pcfz(int x,int y,bool type) { fh[x]=type; fc[y]=type; fr[x+y]=type; fl[x+n-y+1]=type; map[x][y]=type; } void dfs(int r) { if(r==8) print(); else if(fh[r]) dfs(r+1); else { for(int i=0;i!=8;++i) if(check(r,i)) { pcfz(r,i,1); dfs(r+1); pcfz(r,i,0); } } } int main() { cin>>n; for(int i=0;i!=n;++i) { int tx,ty; cin>>tx>>ty; pcfz(tx,ty,1); } dfs(0); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> A(5); int k; for(int &i:A) cin>>i; cin>>k; sort(A.begin(),A.end()); if(A.at(4)-A.at(0)>k) cout<<":("<<endl; else cout<<"Yay!"<<endl; }
0
#include <cstdio> #include <cstring> using namespace std; int n, k; int array[10]; int c[4]; bool use[10]; char str[10000][100]; int m; int number[10000]; int p; void Search(int i, int j) { if (j == k){ for (int l = 0; l < k; l++){ char nstr[3]; sprintf(nstr, "%d", c[l]); strcat(str[m], nstr); } int num; sscanf(str[m], "%d", &num); // printf("%d\n", num); bool f = false; for (int l = 0; l < p; l++){ if (number[l] == num){ f = true; break; } } if (!f){ number[p] = num; p++; } m++; return; } if (i == n){ return; } Search(i + 1, j); if (use[i]){ return; } c[j] = array[i]; use[i] = true; Search(0, j + 1); use[i] = false; } int main() { while (scanf("%d %d", &n, &k), n + k){ memset(array, 0, sizeof(array)); memset(c, 0, sizeof(c)); memset(use, 0, sizeof(use)); memset(str, 0, sizeof(str)); memset(number, 0, sizeof(number)); m = 0; p = 0; for (int i = 0; i < n; i++){ scanf("%d", &array[i]); } Search(0, 0); printf("%d\n", p); } return 0; }
#include <iostream> #include <string> using namespace std; class Dice { private: int *numbers; public: Dice(int *numbers) { this->numbers = numbers; } int getCurrentNumber() { return this->numbers[0]; } void turn(char direction) { switch (direction) { case 'N': this->turnNorth(); break; case 'S': this->turnSouth(); break; case 'E': this->turnEast(); break; case 'W': this->turnWest(); break; } } void turnNorth() { swap(this->numbers[0], this->numbers[1]); swap(this->numbers[1], this->numbers[5]); swap(this->numbers[5], this->numbers[4]); } void turnSouth() { swap(this->numbers[0], this->numbers[4]); swap(this->numbers[4], this->numbers[5]); swap(this->numbers[5], this->numbers[1]); } void turnEast() { swap(this->numbers[0], this->numbers[3]); swap(this->numbers[3], this->numbers[5]); swap(this->numbers[5], this->numbers[2]); } void turnWest() { swap(this->numbers[0], this->numbers[2]); swap(this->numbers[2], this->numbers[5]); swap(this->numbers[5], this->numbers[3]); } }; int main() { int numbers[6]; char direction; string directions; for (int i = 0; i < 6; i++) { cin >> numbers[i]; } cin >> directions; Dice dice(numbers); for (int i = 0; i < directions.size(); i++) { dice.turn(directions[i]); } cout << dice.getCurrentNumber() << endl; return 0; }
0
#include<bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using namespace std; ll gcd(ll C, ll D) { if (C < D) gcd(D, C); if (C%D == 0)return D; else return gcd(D, C%D); } const int MOD = 1000000007; int main() { int N; cin >> N; vector<int> A(N); ll sum = 0; rep(i, N) { cin >> A[i]; sum += A[i]; } ll ans = 0; rep(i, N) { sum -= A[i]; ans += ((A[i]%MOD) * (sum%MOD))%MOD; ans %= MOD; } cout << ans << endl; return 0; }
#include<iostream> using namespace std; long N,A[2<<17],ans; main() { cin>>N; for(int i=0;i<N;i++)cin>>A[i]; int i=0; while(i<N) { while(i+1<N&&A[i]+1==A[i+1])i++; if(A[i]>i||i+1<N&&A[i]+1<A[i+1]) { cout<<-1<<endl; return 0; } ans+=A[i]; i++; } cout<<ans<<endl; }
0
#include <bits/stdc++.h> #define fr first #define sc second #define OK puts("OK"); #define pb push_back #define mk make_pair using namespace std; typedef long long ll; const int inf = (int)1e9 + 7; const int N = (int)1e5 + 7; int n,m; int top,mx; int st[N],b[N]; int t[2001][2001]; char a[2001][2001]; int f (int i,int j) { return (a[i][j] == '#') ? 1 : 0; } int main () { cin >> n >> m; mx = max(n,m); for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++) cin >> a[i][j]; n --; m --; for (int i = 1; i <= n; i ++) for (int j = 1; j <= m; j ++) t[i][j] = f(i,j) ^ f(i + 1,j) ^ f(i,j + 1) ^ f(i + 1,j + 1); for (int i = 1; i <= n; i ++) { for (int j = 1; j <= m; j ++) if(t[i][j]) b[j] = 0; else b[j] ++; top = 0; for (int j = 1; j <= m; j ++) { while (top && b[st[top]] >= b[j]) { mx = max (mx,(j - st[top - 1]) * (b[st[top]] + 1)); top --; } st[++top] = j; } while (top) { mx = max(mx,(m - st[top - 1] + 1) * (b[st[top]] + 1)); top --; } } cout << mx << endl; }
#include <cstdio> #include <algorithm> using namespace std; int p[1010]; int sum[1002010]; int main(void){ int n,m; while(scanf("%d%d",&n,&m) && (n||m)){ for(int i=0;i<n;i++){ scanf("%d",&p[i]); } p[n++] = 0; //0ツ点ツづ個スツコツアツづーツ催ャツづゥ int size=0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ sum[size++] = p[i] + p[j]; } } sort(sum,sum+size); int ans = 0; for(int i=0;i<size;i++){ int rem = m - sum[i]; if(rem < 0) continue; int left = 0, right = size; while(left < right){ int center = (left + right) / 2; if(rem > sum[center]){ left = center + 1; } else if(rem < sum[center]){ right = center; } else { break; } } ans = max(ans, sum[i] + sum[right-1]); } printf("%d\n",ans); } return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using vec = vector<ll>; using mat = vector<vec>; using pll = pair<ll,ll>; #define INF (1LL << 60) #define MOD 1000000007 #define PI 3.14159265358979323846 #define REP(i,m,n) for(ll (i)=(m),(i_len)=(n);(i)<(i_len);++(i)) #define FORR(i,v) for(auto (i):v) #define ALL(x) (x).begin(), (x).end() #define PR(x) cout << (x) << endl #define PS(x) cout << (x) << " " #define SZ(x) ((ll)(x).size()) #define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b)) #define REV(x) reverse(ALL((x))) #define ASC(x) sort(ALL((x))) #define DESC(x) ASC((x)); REV((x)) #define pb push_back #define eb emplace_back int main() { ll H, W; cin >> H >> W; mat A(H, vec(W, 0)); REP(i,0,H) { REP(j,0,W) { cin >> A[i][j]; A[i][j] %= 2; } } mat Z; REP(i,0,H) { bool f = false; REP(j,0,W-1) { vec z = {i+1, j+1, i+1, j+2}; if(A[i][j] == 1) { if(!f) { A[i][j] = 0; f = true; } else { A[i][j] = 0; f = false; } } if(f) Z.pb(z); } if(f) A[i][W-1] = 1 - A[i][W-1]; } bool f = false; REP(i,0,H-1) { vec z = {i+1, W, i+2, W}; if(A[i][W-1] == 1) { if(!f) { A[i][W-1] = 0; f = true; } else { A[i][W-1] = 0; f = false; } } if(f) Z.pb(z); } if(f) A[H-1][W-1] = 1 - A[H-1][W-1]; PR(SZ(Z)); REP(i,0,SZ(Z)) { PS(Z[i][0]); PS(Z[i][1]); PS(Z[i][2]); PR(Z[i][3]); } return 0; } /* */
#include <iostream> #include <fstream> #include <set> #include <map> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <algorithm> #include <climits> #include <cmath> #include <iomanip> using namespace std; #define ll long long int #define rep(i,n) for( int i = 0; i < n; i++ ) #define rrep(i,n) for( int i = n; i >= 0; i-- ) #define REP(i,s,t) for( int i = s; i <= t; i++ ) #define RREP(i,s,t) for( int i = s; i >= t; i-- ) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 typedef pair<int, int> P; typedef pair<P, P> PP; vector<PP> ans; int main(void) { cin.tie(0); ios::sync_with_stdio(false); int h, w; cin >> h >> w; int a[510][510]; rep(i, h) { rep(j, w) { cin >> a[i][j]; } } rep(i, h) { rep(j, w - 1) { if(a[i][j] % 2 == 1) { a[i][j]--; a[i][j + 1]++; ans.push_back({{i , j}, {i, j + 1}}); } } } rep(i, h - 1) { if(a[i][w - 1] % 2 == 1) { a[i][w - 1]--; a[i + 1][w - 1]++; ans.push_back({{i , w - 1}, {i + 1, w - 1}}); } } cout << ans.size() << endl; for(auto e: ans) { cout << e.first.first + 1 << " " << e.first.second + 1 << " " << e.second.first + 1 << " " << e.second.second + 1 << endl; } return 0; }
1
#include <iostream> #include <algorithm> #include <vector> #include <set> #include <map> using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) typedef long long ll; int main() { int N; cin>>N; vector<int>A(N+2); rep(i,0,N) cin>>A[i+1]; int sum=0; rep(i,0,N+2){ sum+=abs(A[i]-A[i+1]); } rep(i,0,N){ int tmp=sum-abs(A[i+1]-A[i])-abs(A[i+1]-A[i+2])+abs(A[i]-A[i+2]); cout<<tmp<<endl; } return 0; }
#include <stdio.h> #include <stdlib.h> int n,k,card[10],count; int check[10001]; // used[i]==1‚È‚ç‚Î i‚ÍŽg‚Á‚Ä‚¢‚é // used[i]==1‚È‚ç‚Î card[i]‚ÍŽg‚Á‚Ä‚¢‚é void func(int a[],int now) { int i,j,temp,keta=0,flg; if(now == k){ char str[9]; for(i=0;i<k;i++){ temp=sprintf(str+keta,"%d",a[i]); keta+=temp; } temp=atoi(str); check[count]=temp; count++; } else { int used[10] = { 0 }; for(i=0; i<now; i++){ for(j=0;j<n;j++){ if(a[i]==card[j] && used[j]==0){ used[j]=1; break; } } } for(i=0;i<n;i++){ if(used[i] == 0){ a[now] = card[i]; func(a,now+1); } } } } int main(void) { int i,j,array[5]; while(scanf("%d",&n),n){ scanf("%d",&k); for(i=0;i<n;i++){ scanf("%d", &card[i]); } func(array,0); for(i=0;i<count;i++){ for(j=0;j<count;j++){ if(check[i]==check[j] && i!=j && check[i]!=0){ check[j]=0; } } } count=0; for(i=0;i<10000;i++){ if(check[i]!=0){ count++; } } printf("%d\n",count); for(i=0;i<10001;i++){ check[i]=0; } count=0; for(i=0;i<10;i++){ card[i]=0; } } return 0; }
0
#include<bits/stdc++.h> //#include<atcoder/all> using namespace std; //using namespace atcoder; int main(){ int a,b,c; cin>>a>>b>>c; cout<<a+b+c-max(a,max(b,c))<<endl; }
#include<iostream> #include<string> using namespace std; #define DICETOP 0 #define DICEFRONT 1 #define DICERIGHT 2 #define DICELEFT 3 #define DICEBACK 4 #define DICEBOTTOM 5 class Dice { private: int *diceface; public: Dice( int top, int front, int right, int left, int back, int bottom ) { diceface = new int[6]; diceface[DICETOP] = top; diceface[DICEFRONT] = front; diceface[DICERIGHT] = right; diceface[DICELEFT] = left; diceface[DICEBACK] = back; diceface[DICEBOTTOM] = bottom; } ~Dice( ) { delete [ ] diceface; } void roll( string cmd ) { int t; for( int i=0; i<cmd.length(); i++ ) { if( 'E' == cmd[i] ) { t = diceface[DICETOP]; diceface[DICETOP] = diceface[DICELEFT]; diceface[DICELEFT] = diceface[DICEBOTTOM]; diceface[DICEBOTTOM] = diceface[DICERIGHT]; diceface[DICERIGHT] = t; } else if( 'N' == cmd[i] ) { t = diceface[DICETOP]; diceface[DICETOP] = diceface[DICEFRONT]; diceface[DICEFRONT] = diceface[DICEBOTTOM]; diceface[DICEBOTTOM] = diceface[DICEBACK]; diceface[DICEBACK] = t; } else if( 'S' == cmd[i] ) { t = diceface[DICETOP]; diceface[DICETOP] = diceface[DICEBACK]; diceface[DICEBACK] = diceface[DICEBOTTOM]; diceface[DICEBOTTOM] = diceface[DICEFRONT]; diceface[DICEFRONT] = t; } else if( 'W' == cmd[i] ) { t = diceface[DICETOP]; diceface[DICETOP] = diceface[DICERIGHT]; diceface[DICERIGHT] = diceface[DICEBOTTOM]; diceface[DICEBOTTOM] = diceface[DICELEFT]; diceface[DICELEFT] = t; } } return; } int getDiceFace( int face ) { return diceface[ face ]; } }; int main( ) { int t, f, r, l, ba, bo; cin>>t>>f>>r>>l>>ba>>bo; Dice *dice = new Dice( t, f, r, l, ba, bo ); string cmd; cin>>cmd; dice->roll( cmd ); cout<<dice->getDiceFace( DICETOP )<<endl; delete dice; return 0; }
0
#include <bits/stdc++.h> using namespace std; //#define cerr if (false) cerr #define db(x) cerr << #x << "=" << x << endl #define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl #define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl #define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl #define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl typedef long long ll; typedef long double ld; const ll MOD = 998244353; void add(ll& x, ll y) { x += y; if (x >= MOD) x -= MOD; } void sub(ll& x, ll y) { x -= y; if (x < 0) x += MOD; } int main() { int n; scanf("%d", &n); vector<ll> ndp(90001), dp(90001); vector<ll> ndp2(90001), dp2(90001); dp[0] = dp2[0] = 1; ll sum = 0; for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); sum += a; fill(ndp.begin(), ndp.end(), 0); fill(ndp2.begin(), ndp2.end(), 0); for (int s = 0; s <= i * 300; ++s) { add(ndp[s], dp[s]); add(ndp[s], dp[s]); add(ndp[s + a], dp[s]); add(ndp2[s], dp2[s]); add(ndp2[s + a], dp2[s]); } // for (int s = 0; s <= 10; ++s) db2(s, ndp[s]); swap(dp, ndp); swap(dp2, ndp2); } ll ans = 1; for (int i = 1; i <= n; ++i) ans = ans * 3 % MOD; for (int i = (sum + 1) / 2; i <= sum; ++i) { // db2(i, dp[i]); sub(ans, dp[i] * 3 % MOD); } if (sum % 2 == 0) { add(ans, dp2[sum / 2] * 3 % MOD); } printf("%lld\n", ans); }
#include <iostream> #include <vector> #include <map> #include <algorithm> #include <string> #include <queue> #include <stack> #include <math.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> ii; typedef pair<ll, ll> P; typedef vector<int> vi; typedef vector<vi> vvi; #define REP(i, n) for (ll i = 0; i < (n); ++i) #define REPR(i, n) for (ll i = (n) - 1; i >= 0; --i) #define FOR(i, n, m) for (ll i = (n); i < (m); ++i) #define FORR(i, n, m) for (ll i = (m) - 1; i >= (n); --i) #define FORE(x, xs) for (auto &x: (xs)) #define ALL(v) v.begin(), v.end() #define ZERO(a) memset(a,0,sizeof(a)) const int INF = 1000000009; const int MOD = 998244353; const int HALF = (MOD + 1) / 2; const double EPS = 1e-14; const double PI = acos(-1); const int MAX = 310; const int MAX_VAL = MAX*MAX; int N; int A[MAX]; ll dp[MAX][MAX_VAL]; // dp[i][x] : i個目まで使ってxをつくるパターン数 ll dp1[MAX_VAL]; // dp1[i] : iをつくるパターン数 ll pw (ll n, ll k) { ll r = 1; while (k) { if (k&1) r = r*n%MOD; k >>= 1; n = n*n%MOD; } return r; } int main() { cin >> N; REP (i, N) cin >> A[i]; ll ans = pw(3, N); int sm = A[0]; dp[0][0] = 2; dp[0][A[0]] = 1; FOR (i, 1, N) { REP (j, sm+1) if (dp[i-1][j] > 0) { dp[i][j] = (dp[i][j] + 2*dp[i-1][j]) % MOD; dp[i][j+A[i]] = (dp[i][j+A[i]] + dp[i-1][j]) % MOD; } sm += A[i]; } int half = sm/2; if (sm&1) half++; FOR (i, half, sm+1) { ans = (ans + 3ll * MOD - 3*dp[N-1][i]) % MOD; } dp1[0] = 1; int mx = 0; REP (i, N) { REPR (j, mx+1) if (dp[j]) { dp1[j+A[i]] = (dp1[j+A[i]] + dp1[j]) % MOD; } mx += A[i]; } if (sm%2 == 0) ans = (ans + 3*dp1[sm/2]) % MOD; cout << ans << endl; }
1
#include <bits/stdc++.h> #define debug(x) cout << #x << " = " << x << endl #define REP(i , n) for(Long i = 0; i < (Long)n ; i++) #define fore(i, a, b) for (Long i = a, to = b; i < to; i++) #define foref(i, a, b) for (Long i = b - 1, to = a; i >= to; i--) #define all(v) v.begin(), v.end() #define SZ(v) (int)v.size() #define pb push_back using namespace std; typedef long long Long; typedef pair<Long, Long> Pair; const int MX=(2e5); const Long INF=(1e18); Long A[MX+2]; Long acum[MX+2]; Long acum2[MX+2]; int n; set<Long> S1,S2; Long ans = INF; void solve(vector<Pair> izq,vector<Pair> der){ fore(i,0,SZ(izq)){ fore(j,0,SZ(der)){ vector<Long> asd; asd.pb(izq[i].first); asd.pb(izq[i].second); asd.pb(der[j].first); asd.pb(der[j].second); //fore(k,0,4)cout<<asd[k]<<(char)(k+1==4?10:32); sort(all(asd)); ans = min(ans,asd[3] - asd[0]); } } } void show(){ cout<<"der"<<endl; for(auto x:S1){ cout<<x<<" "; } cout<<endl; cout<<"izq"<<endl; for(auto x:S2){ cout<<x<<" "; } cout<<endl; } void prueba(Long current){ //show(); Long search1 = current/2; auto it = S1.lower_bound(search1); vector<Pair> izq; if(it!=S1.end()){ Long p = (*it); Long q = current - p; izq.pb({p,q}); } if(it!=S1.begin()){ it--; Long p = (*it); Long q = current - p; izq.pb({p,q}); } Long total = acum[n-1] - current; Long search2 = total/2; auto it2 = S2.lower_bound(search2); vector<Pair> der; if(it2!=S2.end()){ Long p = (*it2); Long q = total - p; der.pb({p,q}); } if(it2!=S2.begin()){ it2--; Long p = (*it2); Long q = total - p; der.pb({p,q}); } solve(izq,der); } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n; fore(i,0,n) cin>>A[i]; acum[0] = A[0]; fore(i,1,n) acum[i]=A[i] + acum[i-1]; acum2[n-1] = A[n-1]; foref(i,0,n-1){ acum2[i] = acum2[i+1] + A[i]; } fore(i,0,n) S2.insert(acum2[i]); fore(i,0,n){ S1.insert(acum[i]); S2.erase(acum2[i]); if(i>0 && i<n-2)prueba(acum[i]); } cout<<ans<<endl; return 0; }
#include <iostream> #include <array> #include <map> using namespace std; typedef unsigned long ul; typedef array<ul, 6> dice; bool f(int n) { static const array<dice, 6> tbl = { 0,1,2,3,4,5, 1,5,2,3,0,4, 2,1,5,0,4,3, 3,1,0,5,4,2, 4,0,2,3,5,1, 5,1,3,2,4,0}; dice d; map<ul, bool> db; array<ul, 24> r; while (n--) { for (ul &a: d) cin >> a; int i = 0; for (dice t: tbl) { for (int j = 0; j < 4; j++) { ul u = 0; for (int k = 0; k < 6; k++) u += (d[t[k]] + 1) << k * 10; if (db.count(u)) return true; r[i * 4 + j] = u; ul x = t[1]; t[1] = t[2]; t[2] = t[4]; t[4] = t[3]; t[3] = x; } i++; } for (ul x : r) db[x] = true; } return false; } int main() { int n; cin >> n; cout << (f(n) ? "No" : "Yes") << endl; }
0
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <bitset> // bitset #include <deque> // deque #include <iomanip> //setprecsion #include <iostream> // cout, endl, cin #include <map> // map #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <string> // string, to_string, stoi #include <tuple> // tuple, make_tuple #include <utility> // pair, make_pair #include <vector> // vector #include <cctype> // isupper, islower, isdigit, toupper, tolower #include <cmath> //sin, cos, tan using namespace std; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rrep(i, n) for (ll i = (n - 1); i >= 0; --i) #define blank(ans) cout << (ans) << " "; #define mp(p, q) make_pair(p, q) #define mt(p, q, r) make_tuple(p, q, r) #define pb(n) push_back(n) #define all(a) a.begin(), a.end() typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef string str; typedef vector<ll> vll; typedef vector<ld> vd; typedef vector<bool> vb; typedef vector<char> vc; typedef vector<str> vs; typedef vector<vector<ll>> vvll; typedef vector<vector<ld>> vvd; typedef vector<vector<bool>> vvb; typedef vector<vector<char>> vvc; typedef vector<vector<str>> vvs; typedef vector<pair<ll, ll>> vpll; typedef vector<tuple<ll, ll, ll>> vtlll; const ld PI = acos(-1.0); const ll MAX = 9000000000000000000; const ll MIN = -9000000000000000000; const ld DMAX = 4500; const ld DMIN = -4500; const ll MOD = 1000000007; template <typename T> void fin(T a) { cout << a << endl; exit(0); } //桁数 ll GetDigit(ll num) { return log10(num) + 1; } //約数全列挙 vector<ll> enum_div(ll n) { vector<ll> ret; for (ll i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.push_back(i); if (i * i != n) { ret.push_back(n / i); } } } sort(begin(ret), end(ret)); return ret; } //累乗(xのn乗%mod) ll modpow(ll x, ll n, ll mod = MAX) { ll res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } //階乗 ll factorial(ll n) { if (n > 0) return n * factorial(n - 1); else return 1; } ll modinv(ll a, ll mod) { ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } void Main() { str s; cin >> s; set<char> flag; rep(i, s.size()) { flag.insert(s[i]); } for (char i = 'a'; i <= 'z'; i++) { if (!flag.count(i)) fin(s + i); } if (s == "zyxwvutsrqponmlkjihgfedcba") fin(-1); vc SS; SS.pb(s[s.size() - 1]); s.pop_back(); while(1) { sort(all(SS)); for(ll i = 0;i < SS.size();i++) { if(s[s.size() - 1] < SS[i]) { s.pop_back(); cout << s << SS[i] << endl; return; } } SS.pb(s[s.size() - 1]); s.pop_back(); } } //前処理 int main() { //ios::sync_with_stdio(false); //cin.tie(0); cout << fixed << setprecision(20); //高精度少数表示 //cout << setfill('0') << internal << setw(4); 0埋め4桁 Main(); return 0; }
#include <bits/stdc++.h> using namespace std; void input(int& rnTrgSu, int& rnTrgKin) { cin >> rnTrgSu >> rnTrgKin; } void searchMoney(int nTrgSu, int nTrgKin) { vector<int> cnvnMoney{10000, 5000, 1000}; int nCalKin; int i, j; for (i = 0; i <= nTrgSu; i++) { for (j = 0; j <= (nTrgSu - i); j++) { nCalKin = nTrgKin; nCalKin -= cnvnMoney[0] * i; nCalKin -= cnvnMoney[1] * j; nCalKin -= cnvnMoney[2] * (nTrgSu - i - j); if ( !nCalKin ) break; } if ( !nCalKin ) break; } if ( !nCalKin ) cout << i << " " << j << " " << (nTrgSu - i - j) << endl; else cout << -1 << " " << -1 << " " << -1 << endl; } int main() { int nTrgSu, nTrgKin; input(nTrgSu, nTrgKin); searchMoney(nTrgSu, nTrgKin); return 0; }
0
#include<bits/stdc++.h> using namespace std; #define li long long int #define uli unsigned long long int #define rep(i,to) for(li i=0;i<((li)(to));i++) #define repp(i,start,to) for(li i=(li)(start);i<((li)(to));i++) #define pb push_back #define sz(v) ((li)(v).size()) #define bgn(v) ((v).begin()) #define eend(v) ((v).end()) #define allof(v) (v).begin(), (v).end() #define dodp(v,n) memset(v,(li)n,sizeof(v)) #define bit(n) (1ll<<(li)(n)) #define mp(a,b) make_pair(a,b) #define rin rep(i,n) #define EPS 1e-12 #define ETOL 1e-8 #define MOD 1000000007 typedef pair<li, li> PI; #define INF bit(60) #define DBGP 1 #define idp if(DBGP) #define F first #define S second #define p2(a,b) idp cout<<a<<"\t"<<b<<endl #define p3(a,b,c) idp cout<<a<<"\t"<<b<<"\t"<<c<<endl #define p4(a,b,c,d) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl #define p5(a,b,c,d,e) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl #define p6(a,b,c,d,e,f) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl #define p7(a,b,c,d,e,f,g) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl #define p8(a,b,c,d,e,f,g,h) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl #define p9(a,b,c,d,e,f,g,h,i) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl #define p10(a,b,c,d,e,f,g,h,i,j) idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl #define foreach(it,v) for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it) #define p2p(x) idp p2((x).F, (x).S) #define dump(x,n) idp{rep(i,n){cout<<x[i]<<" ";}puts("");} #define dump2(x,n) idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");} #define dumpi(x) idp{foreach(it, x){cout<<(*it)<<" ";}puts("");} #define dumpi2(x) idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");} #define read2d(a,w,h) rep(i,h)rep(j,w)cin>>a[i][j] #define dump2d(a,w,h) rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");} typedef pair<li, li> PI; li n, m; li a[333][333]; inline void process(deque<li>& dq, set<li>& banned) { while (!dq.empty()) { if (banned.count(dq.front()) > 0) { dq.pop_front(); } else { return; } } } inline bool check(li x) { vector<deque<li>> qs(n); rin{ rep(j, m) { qs[i].pb(a[i][j]); } } set<li> banned; while (true) { map<li, li> cnt; rin{ process(qs[i], banned); if (qs[i].empty()) { return false; } cnt[qs[i].front()]++; } bool ok = true; for (auto& kv : cnt) { if (kv.S > x) { banned.insert(kv.F); ok = false; } } if (ok) { return true; } } return false; } int main() { cin >> n >> m; rin{ rep(j, m) { cin >> a[i][j]; a[i][j]--; } } li l = 0; li r = n; while (l + 1 < r) { li med = (l + r) / 2; if (check(med)) { r = med; } else { l = med; } } cout << r << endl; return 0; }
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> using namespace std; int n,m,h[2010][2010],l[2010][2010],r[2010][2010],ans; char s[2010][2010]; int main() { scanf("%d %d",&n,&m);getchar(); ans=max(n,m); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(j==1||i==1) h[i][j]=1; else h[i][j]=((s[i][j]==s[i][j-1])==(s[i-1][j]==s[i-1][j-1]))?h[i-1][j]:i; } for(int i=1;i<=n;i++) { if(i==1) for(int j=1;j<=m;j++) l[i][j]=1,r[i][j]=m; else { l[i][1]=1;for(int j=2 ;j<=m;j++) l[i][j]=(s[i][j]==s[i-1][j])==(s[i][j-1]==s[i-1][j-1])?l[i][j-1]:j; r[i][m]=m;for(int j=m-1;j>=1;j--) r[i][j]=(s[i][j]==s[i-1][j])==(s[i][j+1]==s[i-1][j+1])?r[i][j+1]:j; } for(int j=1;j<=m;j++) { if(h[i][j]==i) l[i][j]=1,r[i][j]=m; else l[i][j]=max(l[i][j],l[i-1][j]),r[i][j]=min(r[i][j],r[i-1][j]); int N=i-h[i][j]+1,M=r[i][j]-l[i][j]+1; ans=max(ans,N*M); } } printf("%d",ans); }
0
#include <stdio.h> int main(void){ int l[10]; int v1, v2; int len, tl; float p; while(scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", l, l+1, l+2, l+3, l+4, l+5, l+6, l+7, l+8, l+9, &v1, &v2) != EOF){ len = 0; for(int i=0;i<10; ++i){ len += l[i]; } p = v1 * len / (float)(v1 + v2); if(p == 0.0){ puts("0"); break;} tl = 0; for(int i=0; i<10; ++i){ tl += l[i]; if(tl == p){ printf("%d\n", i+1); break; }else if(tl > p){ printf("%d\n", i+1); break; } } } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n,a,b,c; while(1){ cin>>n; if(n==0) break; for(int i=0;i<n;i++){ cin>>a>>b>>c; if(a==100 || b==100 || c==100){ cout<<"A"<<endl; }else if((a+b)/2 >= 90){ cout<<"A"<<endl; }else if((a+b+c)/3 >= 80){ cout<<"A"<<endl; }else if((a+b+c)/3 >= 70){ cout<<"B"<<endl; }else if((a+b+c)/3 >= 50 && a >= 80 || (a+b+c)/3 >= 50 && b >= 80){ cout<<"B"<<endl; }else{ cout<<"C"<<endl; } } } return 0; }
0
#include <iostream> #include <algorithm> using namespace std; int main(){ int road1[111][111]; int road2[111][111]; int n,m; int a,b,cost,time; int p,q,r; int x; while(1){ for(int i=0;i<111;i++){ for(int j=0;j<111;j++){ road1[i][j]=road2[i][j]=111111111; } } cin >> n >> m; if(n==0 && m==0)break; for(int i=0;i<n;i++){ cin >> a >> b >> cost >> time; road1[a][b]=road1[b][a]=cost; road2[a][b]=road2[b][a]=time; } for(int k=1;k<=m;k++){ for(int i=1;i<=m;i++){ for(int j=1;j<=m;j++){ if(road1[i][j]>road1[i][k]+road1[k][j]){ road1[i][j]=road1[i][k]+road1[k][j]; } if(road2[i][j]>road2[i][k]+road2[k][j]){ road2[i][j]=road2[i][k]+road2[k][j]; } } } } cin >> x; for(int i=0;i<x;i++){ cin >> p >> q >> r; cout << (r==0 ? road1[p][q] : road2[p][q]) << endl; } } }
#include<bits/stdc++.h> using namespace std; typedef pair<int,int> P; struct Graph{ Graph(int n):E(n),d(n){} vector<vector<tuple<int,int,int>>>E; vector<int>d; void add_edge(int a,int b,int c,int d){ E[a].push_back(make_tuple(b,c,d)); } void dijkstra(int s,int f){ priority_queue<P,vector<P>,greater<P>>q; fill(d.begin(),d.end(),1e9); q.push({d[s]=0,s}); while(q.size()){ auto p=q.top();q.pop(); if(p.first>d[p.second])continue; for(auto& a:E[p.second]){ int x=get<0>(a),y=get<1>(a)+p.first; if(f)y=get<2>(a)+p.first; if(y<d[x])d[x]=y,q.push({y,x}); } } } }; main(){ int n,m,a,b,c,d,k; while(cin>>n>>m,n){ Graph E(m); for(int i=0;i++<n;E.add_edge(--a,--b,c,d),E.add_edge(b,a,c,d))cin>>a>>b>>c>>d; cin>>k; for(int i=0;i++<k;cout<<E.d[--b]<<endl){ cin>>a>>b>>c; E.dijkstra(--a,c); } } }
1
#include <bits/stdc++.h> using namespace std; #define LL long long #define DD long double #define M 1000000007 #define INF 1e18 void setIO(string s = "") { cin.sync_with_stdio(0); cin.tie(0); if (s.size()) s += ".in", freopen(s.c_str(), "r", stdin); } int tt = 1; void solve() { set<int> nums; int a; while (cin >> a) nums.insert(a); cout << ((nums.size() == 2) ? "Yes" : "No") << endl; } int main() { setIO(); while (tt--) { solve(); } return 0; }
#include <bits/stdc++.h> #include <iostream> #include <iomanip> #include <algorithm> #include <math.h> #define fi first #define se second #define rep(i,n) for(ll i=0;i<(n);i++) #define rrep(i,n) for(ll i = 1; i <= (n); ++i) #define drep(i,n) for(ll i = (n)-1; i >= 0; --i) #define all(v) v.begin(),v.end() #define len(x) (ll)(x).length() #define maxs(x,y) x = max(x,y) #define mins(x,y) x = min(x,y) #define pb push_back #define sz(x) (ll)(x).size() #define v(T) vector<T> #define vv(T) vector<vector<T>> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; ll gcd(ll a,ll b){if(a%b==0){return b;}else{return(gcd(b,a%b));}} ll lcm(ll a,ll b){return a*b/gcd(a,b);} const int INF=1e9; const ll MX = 1e18; const int MOD=INF+7; const int di[] = {-1,0,1,0}; const int dj[] = {0,-1,0,1}; const double PI=acos(-1); int main() { vi a(3); map<int,int> mp; rep(i,3) { cin>>a[i]; mp[a[i]]++; } bool ja=true; for(auto x:mp){ if(x.second==2)ja=false; } if(ja) cout<<"No"<<endl; else cout<<"Yes"<<endl; }
1
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <deque> #include <iostream> #include <iterator> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i)) #define rep(i,n) FOR(i,0,n) #define all(v) begin(v),end(v) #define debug(x) std::cerr<<#x<<": "<<x<<"\n" #define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n" #define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n" using ll=long long; using vi=std::vector<int>; using vvi=std::vector<vi>; using vll=std::vector<ll>; using vvll=std::vector<vll>; template<typename T> using vvec=std::vector<std::vector<T>>; template<typename T> auto make_v(size_t sz){return std::vector<T>(sz);} template<typename T,typename... Ts> auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));} template<typename T> void fill_v(T&var,const T&x){var=x;} template<typename V,typename T> void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}} template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){ int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){ for(auto&& w:v){s<<w;}return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){ int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;} template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){ for(auto&& w:v){s<<w;}return s;} template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){ s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;} inline void scan(int&a){scanf("%d",&a);} inline void scan(ll&a){scanf("%lld",&a);} inline void scan(char&a){scanf(" %c",&a);} inline void scan(double&a){scanf("%lf",&a);} inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);} template<typename T> inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}} template<typename First,typename...Args> inline void scan(First&f,Args&...args){scan(f);scan(args...);} inline void print(int a){printf("%d\n",a);} inline void print(ll a){printf("%lld\n",a);} inline void print(double a){printf("%.12f\n",a);} inline void print(std::string s){std::cout<<s<<"\n";} using namespace std; int mod = 1000000007; ll calc(int x, set<int>& s) { if (s.empty()) { return x; } auto s_ = s; ll ans = 0; for (int elm : s) { s_.erase(elm); ans = (ans + calc(x % elm, s_)) % mod; s_.insert(elm); } return ans; } ll po(ll k, ll x) { if (x == 0) { return 1; } if (x == 1) { return k % mod; } ll y = po(k, x/2); y = y * y % mod; if (x % 2 == 1) { y = y * k % mod; } return y; } int n; vi s; vvll dp; vll frac, finv; ll solve(int m, int x) { if (dp[m][x] >= 0) { return dp[m][x]; } if (m == 0) { dp[m][x] = x; return x; } ll ret = 0; rep (i, m) { ret = (ret + solve(i, x % s[i]) * frac[m-1] % mod * finv[i] % mod) % mod; } dp[m][x] = ret; return ret; } int main() { int x; scan(n, x); s.resize(n); scan(s); sort(all(s)); int n_max = 100001; dp.assign(n+1, vll(n_max, -1)); frac.resize(n_max); finv.resize(n_max); frac[0] = 1; finv[0] = 1; FOR (i, 1, n_max) { frac[i] = frac[i-1] * i % mod; finv[i] = po(frac[i], mod-2); } ll ans = solve(n, x); print(ans); return 0; }
#include <bits/stdc++.h> #define ll long long #define MODV 1000000007 #define INFLL LLONG_MAX // 9223372036854775807 #define EPS 1e-9 #define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++) #define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++) #define all(v) v.begin(), v.end() #define endl "\n" #define vi vector<ll> #define vvi vector<vector<ll>> #define Yes() cout << "Yes" << endl #define YES() cout << "YES" << endl #define No() cout << "No" << endl #define NO() cout << "NO" << endl #define Init() std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout<<fixed<<setprecision(15); template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; } using namespace std; int main(){ Init(); ll n, x; cin >> n >> x; vi a(n); rep(i, n) cin >> a[i]; sort(all(a)); ll ans = 0; for(ll i=0; i<n; i++){ if(x >= a[i]){ if(i == n-1){ x -=a[i]; if(x == 0) ans++; } else{ x -= a[i]; ans++; } } else break; } cout << ans << endl; }
0
#include<bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using P = pair<int,int>; #define rep(i,n) for(ll i = 0;i < (ll)n;i++) #define ALL(x) (x).begin(),(x).end() #define MOD 1000000007 int main(){ int T,A,n; cin >> n >> T >> A; T--,A--; vector<vector<int>> v(n); rep(i,n-1){ int a,b;cin >> a >> b; a--,b--; v[a].push_back(b); v[b].push_back(a); } ll res = 0; bool is = true; vector<int> next(n,T); { vector<int> dt(n,-1); dt[T] = 0; queue<int> que; que.push(T); while(!que.empty()){ int ov = que.front();que.pop(); for(int nv : v[ov]){ if(dt[nv] != -1)continue; next[nv] = ov; dt[nv] = dt[ov] + 1; que.push(nv); } } if(dt[A] & 1){ int d = dt[A]; while(dt[next[A]] >= d-d/2)A = next[A]; T = A; while(dt[next[T]] >= d/2 && next[T] != T)T = next[T]; res += d/2; }else{ int d = dt[A]; while(dt[next[A]] >= d-(d-2)/2)A = next[A]; T = A; while(dt[next[T]] >= (d-2)/2 && next[T] != T)T = next[T]; res += (d-2)/2; is = false; } } int L = -1; { for(int ov : v[T])for(int nv : v[ov])if(nv == A){ L = ov; break; } } queue<int> que; que.push(T); vector<int> d(n,-1); d[T] = 0; while(!que.empty()){ int ov = que.front();que.pop(); for(int nv : v[ov]){ if(nv == A || d[nv] != -1 || nv == L)continue; d[nv] = d[ov]+1; que.push(nv); } } int K = T; rep(i,n)if(d[i] > d[K])K = i; if(is)res += d[K]; else res += d[K]+1; cout << res << endl; /*//////////// cout << "L :" << L << endl; printf("T :%d A :%d\n",T+1,A+1); *///////////// return 0; } /* 7 4 7 1 2 1 3 2 4 2 5 3 6 3 7 */
#include <cstdio> #include <cmath> using namespace std; const int N=1e5+5; const int E=N<<1; const int mod=1e9+7; int n,u,v; int a[N],b[N]; int d1[N],d2[N]; int h[N],nx[E],to[E]; int ec; int ans; void ae(int u,int v) { ec++; nx[ec]=h[u]; to[ec]=v; h[u]=ec; } void dfs(int u,int f,int d[]) { d[u]=d[f]+1; for (int i=h[u];i;i=nx[i]) if (to[i]!=f) dfs(to[i],u,d); } int main() { scanf("%d%d%d",&n,&u,&v); for (int i=1;i<n;i++) { scanf("%d%d",&a[i],&b[i]); ae(a[i],b[i]); ae(b[i],a[i]); } dfs(u,0,d1); dfs(v,0,d2); for (int i=1;i<=n;i++) if (d1[i]<d2[i]&&d2[i]-2>ans) ans=d2[i]-2; printf("%d\n",ans); return 0; }
1