code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
static const ll mod=1000000007;
int pow_mod(long long n,long long m,long long p){
if(p==0) return 1;
else if(p%2==1){
return pow_mod(n,m,p-1)*n%m;
}
else{
long long t=pow_mod(n,m,p/2);
return t*t%m;
}
}
signed main() {
ll na,pa;
cin>>na>>pa;
ll ans=pa-1;
ans*=pow_mod(pa-2,mod,na-1);
ans%=mod;
cout<<ans<<endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define DONTSYNC ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) //dont use stdio with iostream functions //input and output are out of order now!
#define TEST unsigned long long T; cin>>T; while(T--) //loop over each testcase
#define endl "\n"
#define fori(a,start,end) for(int a=start;a<end;a++)
#define forll(a,start,end) for(long long a=start;a<end;a++)
#define forull(a,start,end) for(unsigned long long a=start;a<end;a++)
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
const double PI = acos(-1);
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll mod=1e9+7;
void solve(){
/* code */
ll n,p; cin>>n>>p;
cout<< (p-1)*binpow(p-2,n-1,mod)%mod;
}
int main()
{
DONTSYNC;
// TEST
solve();
return 0;
}
|
//https://oeis.org/search?q=0%2C0%2C1%2C2%2C4%2C6&language=english&go=Search
//https://smallpdf.com/result#r=dc43c99933feaa1c7f3a2f2e2036bc6a&t=pdf-to-word
#include<iostream>
#include <bits/stdc++.h>
#include<stdio.h>
#include <string.h>
using namespace std;
#define ll long long
#define INF 1000007
#define MAX 500007
#define nl "\n"
#define F first
#define S second
typedef vector<ll> vi;
typedef priority_queue<ll , vector<ll> , greater<ll> > pqmx;
typedef priority_queue<ll> pqmn;
typedef pair<ll , ll> pi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector< pair<int , int> > vpi;
#define fr(i,n) for(i=0;i<n;i++)
#define rep(i,a,n) for(i=a;i<n;i++)
#define yeS(GOOD) GOOD ? cout<<"YES\n" : cout<<"NO\n"
#define all(a) a.begin() , a.end()
#define pb push_back
#define ar array
ll mod = LLONG_MAX;
ll binpow(ll a, ll b){ll res=1;a%=mod;while(b>0){if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res;}
ll binmul(ll a, ll b){ll res=0;a%=mod;while(b>0){if(b&1)res=(res + a)%mod;a=(a + a)%mod;b>>=1;}return res;}
ll area(pi a, pi b,pi c){return abs(a.F * b.S + b.F * c.S + c.F*a.S - a.S * b.F - b.S * c.F - c.S * a.F);}
ll gcd (ll a,ll b){if(b==0)return a;else return gcd (b, a % b);}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll min(ll a,ll b){if(a < b){return a;}return b;}
ll max(ll a,ll b){if(a > b){return a;}return b;}
double intlog(ll n ,ll base){return (double)log(n)/log(base);}
ll n , t , m , temp , temp2, root , p ,q , k , i , j , r , u , v , w;
struct comp {
bool operator()( pair<int, int> const& a, pair<int, int> const& b) {
return a.first < b.first || (a.first == b.first && a.second > b.second);
}
};
bool is_valid(int i , int j , vector<vc> & a){
if(i < 0 || j < 0 || i >= n || j >= m){
return false;
}
return a[i][j] == '.';
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt" , "r" , stdin);
freopen("output.txt" , "w" , stdout);
#endif
cin>>n>>m;
cin>>p>>q;
vector<vc> a(n + 1, vc(m + 1));
fr(i , n){fr(j , m)cin>>a[i][j];}
p--;q--;
ll res = 0;
if(a[p][q] == '.')res++;
int i = p;int j = q;
int k = 1;
while(is_valid(i - k, j , a)){k++;res++;}
k = 1;
// cout<<"yes"<<nl;
while(is_valid(i , j - k , a)){k++;res++;}
k = 1;
while(is_valid(i + k, j , a)){k++;res++;}
k = 1;
while(is_valid(i , j + k , a)){k++;res++;}
cout<<res<<nl;
#ifndef ONLINE_JUDGE
cout << "Running Time: " << 1.0 * clock() / CLOCKS_PER_SEC << " s .\n";
#endif
} | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
void solve()
{
int n,m,x,y;
cin>>n>>m>>x>>y;
x--,y--;
string s[n];
for(int i=0;i<n;i++) cin>>s[i];
int ans=0;
if(s[x][y]=='.')
{
ans++;
int x1 = x;
x--;
while(x>=0 && s[x][y]=='.') x--,ans++;
x=x1;
x++;
while(x<n && s[x][y]=='.') x++,ans++;
int y1 = y;
y--;
x=x1;
while(y>=0 && s[x][y]=='.') y--,ans++;
y=y1;
y++;
while(y<m && s[x][y]=='.') y++,ans++;
}
cout<<ans;
}
int main()
{
int t=1;
while(t--)
{
solve();
}
} |
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;
template <typename T> std::vector<T> vectors(std::size_t n, T val) {
return std::vector<T>(n, val);
}
template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}
template <class T> inline bool chmin(T &a, const T &b) {
return (a > b) ? a = b, true : false;
}
template <class T> inline bool chmax(T &a, const T &b) {
return (a < b) ? a = b, true : false;
}
template <class T> inline void chadd(T &a, const T &b) {
a += b, a %= MOD;
// TODO minus case
}
template <class T>
std::ostream &operator<<(std::ostream &stream, const std::vector<T> &vec) {
for (auto val : vec) {
if (std::is_fundamental<T>::value)
stream << " " << val;
else
stream << std::endl << val;
}
return stream;
}
struct Edge {
int to;
long long C;
long long D;
Edge(int to, long long c, long long d) : to(to), C(c), D(d) {}
};
using Graph = std::vector<std::vector<Edge>>;
int main() {
int N, M;
std::cin >> N >> M;
Graph G(N);
for (int i = 0; i < M; ++i) {
int a, b;
long long c, d;
std::cin >> a >> b >> c >> d;
a--, b--;
G[a].push_back(Edge(b, c, d));
G[b].push_back(Edge(a, c, d));
}
std::vector<long long> dist(N, LINF);
using qval = std::pair<long long, int>;
std::priority_queue<qval, std::vector<qval>, std::greater<qval>> pque;
int s = 0;
dist[s] = 0;
pque.push(std::make_pair(dist[s], s));
while (!pque.empty()) {
// 未確定のノードの中から暫定値が最小のものを取り出す
int v = pque.top().second;
long long d = pque.top().first;
pque.pop();
// dist[v] < d はゴミを意味する
if (dist[v] < d)
continue;
// 選択されたノードから出ている辺を緩和する
for (auto &e : G[v]) {
int nv = e.to;
long long C = e.C;
long long D = e.D;
// weight の計算
long long w = LINF;
long long val = int(std::sqrt(D)) - 1 - d;
long long l = val - 5;
if (l < 0)
l = 0;
for (int k = 0; k < 10; ++k) {
long long x = l + k;
long long tmp = x + C + (long long)(D / (1 + d + x));
w = std::min(w, tmp);
}
if (dist[v] + w < dist[nv]) {
dist[nv] = dist[v] + w;
pque.push(std::make_pair(dist[nv], nv));
}
}
}
if (dist[N-1] == LINF)
std::cout << -1 << std::endl;
else
std::cout << dist[N-1] << std::endl;
}
| #include <bitset>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
#include <locale>
#include <memory>
#include <stdexcept>
#include <utility>
#include <string>
#include <fstream>
#include <ios>
#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <streambuf>
#include <complex>
#include <numeric>
#include <valarray>
#include <exception>
#include <limits>
#include <new>
#include <typeinfo>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <climits>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdlib>
#include <cstddef>
#include <cstdarg>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cwchar>
#include <cwctype>
using namespace std;
static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);
using ll = long long;
using ull = unsigned long long;
#define ALL(c) (c).begin(), (c).end()
#define CLEAR(v) memset(v,0,sizeof(v))
#define MP(a,b) make_pair((a),(b))
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ABS(a) ((a)>0?(a):-(a))
template<class T> T MIN(const T& a, const T& b) { return a < b ? a : b; }
template<class T> T MAX(const T& a, const T& b) { return a > b ? a : b; }
template<class T> void MIN_UPDATE(T& a, const T& b) { if (a > b) a = b; }
template<class T> void MAX_UPDATE(T& a, const T& b) { if (a < b) a = b; }
constexpr int mo = 1000000007;
int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
struct Edge {
int to;
ll C, D;
};
ll Cost(ll t, ll C, ll D) {
ll left = t;
ll right = t + 2000000000ll;
while (left + 2 < right) {
ll middle_low = (left * 2 + right) / 3;
ll cost_middle_low = middle_low + C + D / (middle_low + 1);
ll middle_high = (left + right * 2) / 3;
ll cost_middle_high = middle_high + C + D / (middle_high + 1);
if (cost_middle_low < cost_middle_high) {
right = middle_high;
}
else {
left = middle_low;
}
}
ll result = numeric_limits<ll>::max();
for (ll tt = max(t, left - 500); tt <= left + 500; ++tt) {
MIN_UPDATE(result, tt + C + D / (tt + 1));
}
return result;
}
int main() {
std::ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<vector<Edge>> graph(N + 1);
REP(m, M) {
int A, B, C, D;
cin >> A >> B >> C >> D;
graph[A].push_back({ B, C, D });
graph[B].push_back({ A, C, D });
}
// -cost, current
priority_queue<pair<ll, int> > q;
q.emplace(0, 1);
vector<ll> dp(N + 1, numeric_limits<ll>::max() / 2);
dp[1] = 0;
while (!q.empty()) {
ll cost = -q.top().first;
int current = q.top().second;
q.pop();
if (dp[current] != cost) {
continue;
}
for (auto& edge : graph[current]) {
ll next_cost = Cost(cost, edge.C, edge.D);
if (dp[edge.to] > next_cost) {
dp[edge.to] = next_cost;
q.emplace(-next_cost, edge.to);
}
}
}
if (dp[N] == numeric_limits<ll>::max() / 2) {
cout << -1 << endl;
}
else {
cout << dp[N] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r)
{
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)
const int MOD = 1e9+7;
int mod_pow(int a,int b,int M = MOD)
{
if(a == 0) return 0;
b %= (M - 1); //M must be prime here
int res = 1;
while(b > 0)
{
if(b&1) res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
void solve()
{
int n;
cin >> n;
string s,t;
cin >> s >> t;
int states[n+1][7];
f(i,n+1) f(j,7) states[i][j] = 0;
states[n][0] = 1;
int pw = 1;
for(int i=n-1;i>=0;i--)
{
int one = (pw*(s[i] - '0'))%7;
int two = 0;
f(j,7)
{
if(t[i] == 'T')
{
if(states[i+1][(j + one)%7] || states[i+1][(j + two)%7])
states[i][j] = 1;
}
else
{
if(states[i+1][(j + one)%7] && states[i+1][(j + two)%7])
states[i][j] = 1;
}
}
pw = (pw*10)%7;
}
if(states[0][0]) cout <<"Takahashi\n";
else cout <<"Aoki";
}
signed main()
{
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define MOD(a, m) ((a % m + m) % m)
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; }
const ll MOD = 1e9+7;
int main(){
ll M, H;
cin >> M >> H;
if(H % M == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <numeric>
typedef long long ll;
ll stringToFixedPoint(const std::string s, const int digitAfterDecimalPoint)
{
ll fp = std::stoi(s) * pow(10, digitAfterDecimalPoint);
const auto pos = s.find('.');
if (pos != std::string::npos) {
std::string afterDecimalPoint = s.substr(pos + 1);
fp += std::stoi(afterDecimalPoint) * pow(10, digitAfterDecimalPoint - afterDecimalPoint.length());
}
return fp;
}
int main(int argc, char *argv[])
{
std::string strX, strY, strR;
std::cin >> strX >> strY >> strR;
ll cx, cy, cr;
const int digitAfterDecimalPoint = 4;
const ll k = pow(10, digitAfterDecimalPoint);
cx = stringToFixedPoint(strX, digitAfterDecimalPoint);
cy = stringToFixedPoint(strY, digitAfterDecimalPoint);
cr = stringToFixedPoint(strR, digitAfterDecimalPoint);
ll ans = 0;
for (ll y = (((cy + cr) / k) * k); y >= cy - cr; y -= k) {
ll dy = y - cy;
ll dxp = sqrt(cr * cr - dy * dy) + k;
bool xmaxFound = false;
ll xmax;
for (ll x = (((cx + dxp) / k) * k); x >= cx - dxp; x -= k) {
ll dx = x - cx;
ll r2 = dy * dy + dx * dx;
if (r2 <= cr * cr) {
xmaxFound= true;
xmax = x;
break;
}
}
if (!xmaxFound) {continue;}
ll xmin;
for (ll x = (((cx - dxp) / k) * k); x <= cx + dxp; x += k) {
ll dx = x - cx;
ll r2 = dy * dy + dx * dx;
if (r2 <= cr * cr) {
xmin = x;
break;
}
}
ll ansAdd = (xmax - xmin) / k + 1;
ans += ansAdd;
}
std::cout << ans << std::endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
int main() {
long long N, K, M, COUNT = 0, A;
cin >> N >> K >> M;
COUNT = N * M;
for (int i = 0; i < N - 1; i++) cin >> A, COUNT -= A;
cout << (COUNT > K ? -1 : max(0LL, COUNT)) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long ;
const int N = 4e6 + 7;
const int mod = 1e9 + 7 ;
int n , m, k ;
long long fact[N] ;
ll faspow(ll x, ll y, ll MOD = mod)
{
ll ret = 1ll;
while (y)
{
if (y & 1)
ret = 1ll * ret * x % MOD;
x = 1ll * x * x % MOD;
y >>= 1ll;
}
return ret;
}
ll inv(ll x) { return faspow(x, mod - 2); }
ll ncr(ll x, ll y) { return 1ll * fact[x] * inv(fact[y]) % mod * inv(fact[x - y]) % mod; }
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
fact[0] = 1ll ;
for(int i = 1;i < N ; ++ i)fact[i] = 1ll * i * fact[i -1 ] % mod ;
cin >> n >> m >> k ;
if(n > m + k)return cout << 0 , 0;
cout << (ncr(n + m , n) - ncr(n + m , m + k + 1) + mod) % mod ;
return 0;
}
| //#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 <<23;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int n, m, k; cin >> n >> m >> k;
if (n > m + k) {
cout << 0 << "\n";
return;
}
modint ans = comb(n + m, n);
ans -= comb(n+m, n-k-1);
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
init_f();
//init(); cout << ps.size() << "\n";
//expr();
//int t; cin >> t; rep(i, t)
solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
double dp[101][101][101];
bool vis[101][101][101];
double func(int a,int b,int c){
if(a==100 || b==100 || c==100)
return 0;
if(vis[a][b][c]==1)
return dp[a][b][c];
vis[a][b][c]=1;
int cnt=a+b+c;
double ans=0;
if(a>0)
ans+=(1+func(a+1,b,c)*1.0)*a/cnt;
if(b>0)
ans+=(1+func(a,b+1,c)*1.0)*b/cnt;
if(c>0)
ans+=(1+func(a,b,c+1)*1.0)*c/cnt;
return dp[a][b][c]=ans;
}
int main()
{
int a,b,c;
cin>>a>>b>>c;
memset(vis,0,sizeof(vis));
cout<<setprecision(10)<<func(a,b,c)<<endl;
}
| #include<iostream>
#include<cmath>
using namespace std;
int main() {
long long x, y, a, b;
cin >> x >> y >> a >> b;
long long now = 0;
long long tar = b / (a - 1);
while (x < tar && x * a < y) {
x *= a;
now++;
}
if (y - x - 1 > 0)
now += (y - x - 1) / b;
cout << now;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define INF LONG_MAX/3
//#define DIV 1000000007
//#define DIV 998244353
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main(){
ll N;
cin >> N;
vector<ll> A(N);
rep(i, N) cin >> A[i];
sort(A.begin(), A.end());
ll sum = 0;
rep(i, N) sum += A[i];
ll ans = 0;
rep(i, N) {
sum -= A[i];
ans += max(0LL, sum - A[i] * (N - 1 - i));
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '{' << p.first << ", " << p.second << '}'; }
template<typename T> ostream& operator<<(ostream &os, const set<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &mp) { os << '['; string sep; for (const auto &x : mp) os << sep << x, sep = ", "; return os << ']'; }
template<typename T> ostream& operator<<(ostream &os, const multiset<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] =", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define int long long
#define mod 1000000007
#define inf 1e18
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)(x).size()
#define sb(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()
#define rall(x) (x).rbegin(), (x).rend()
int sumPairs(vector<int> &arr, int n)
{
int sum = 0;
for (int i = n - 1; i >= 0; i--)
sum += i * arr[i] - (n - 1 - i) * arr[i];
return sum;
}
void solve()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//cout << fixed << setprecision(10);
int n; cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(all(v));
cout << sumPairs(v, n) << endl;
}
int32_t main()
{
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template < typename T_container, typename T = typename enable_if < !is_same<T_container, string>::value, typename T_container::value_type >::type >
ostream & operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << "\b\b]\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ", "; dbg_out(T...);}
#define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
int ans = 0;
if (n >= 2)
ans = n - 1;
cout << ans;
return 0;
}
/*
*/ | #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double n,d;
cin>>n;
cout<<n-1;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)//iをaからn
#define per(i,a,n) for (int i=a-1;i>=n;i--)//iをnからa
#define db(x) cout << #x << " = " << x << endl
#define db2(x, y) cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; //デバッグ用
#define all(x) (x).begin(), (x).end()
#define INF 1000000000000 //10^12:∞
#define MOD 1000000007 //10^9+7:合同式の法
//sort(all(a)); ソート
//sort(all(a),greater<int>()); 逆順ソート
int main() {
int h,w;
cin >>h>>w;
vector<string> s(h);
int ans=0;
rep(i,0,h){
string ss;
cin>>ss;
s.at(i)=ss;
}
rep(i,0,h){
rep(j,0,w-1){
if(s.at(i).at(j)=='.'&&s.at(i).at(j+1)=='.') {
ans++;
}
}
}
rep(i,0,h-1){
rep(j,0,w){
if(s.at(i).at(j)=='.'&&s.at(i+1).at(j)=='.') {
ans++;
}
}
}
cout<<ans;
return 0;
} | #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<stdio.h>
#include<vector>
#include<algorithm>
constexpr unsigned long long MOD = 998244353;
constexpr unsigned long long IM = 18479187003;
constexpr unsigned long long MOD32 = 665496236;
unsigned long long dp[5001];
int main(){
int H=0, W=0, K=0;
char c=getchar_unlocked();
while(c<='9'&&c>='0') {H=(H<<1)+(H<<3)+c-'0'; c=getchar_unlocked();}
c=getchar_unlocked();
while(c<='9'&&c>='0') {W=(W<<1)+(W<<3)+c-'0'; c=getchar_unlocked();}
c=getchar_unlocked();
while(c<='9'&&c>='0') {K=(K<<1)+(K<<3)+c-'0'; c=getchar_unlocked();}
std::vector<std::vector<std::pair<int, char>>> f(H);
unsigned long long y = H * W - K;
while(--K>=0){
int h=0, w=0;
c=getchar_unlocked();
while(c<='9'&&c>='0') {h=(h<<1)+(h<<3)+c-'0'; c=getchar_unlocked();}
c=getchar_unlocked();
while(c<='9'&&c>='0') {w=(w<<1)+(w<<3)+c-'0'; c=getchar_unlocked();}
--h; --w;
f[h].emplace_back(w, getchar_unlocked());
getchar_unlocked();
}
--H; --W;
dp[0] = 1;
for(int i = 0; i < H; ++i){
std::sort(f[i].begin(), f[i].end());
auto itr = f[i].begin();
for (int j = 0; j <= W; ++j){
if(itr != f[i].end() and itr->first == j){
char c = itr->second;
if(c != 'D') dp[j + 1] += dp[j];
if(c == 'R') dp[j] = 0;
++itr;
}
else{
{
dp[j] *= MOD32;
#ifdef _MSC_VER
unsigned long long x;
_umul128(dp[j], IM, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64);
#endif
dp[j] = dp[j] - x * MOD+MOD;
}
dp[j + 1] += dp[j];
}
}
}
{
std::sort(f[H].begin(), f[H].end());
auto itr = f[H].begin();
for (int j = 0; j < W; ++j){
if(itr != f[H].end() and itr->first == j){
char c = itr->second;
if(c != 'D') dp[j + 1] += dp[j];
++itr;
}
else{
{
dp[j] *= MOD32;
#ifdef _MSC_VER
unsigned long long x;
_umul128(dp[j], IM, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64);
#endif
dp[j] = dp[j] - x * MOD+MOD;
}
dp[j + 1] += dp[j];
}
}
}
unsigned long long x = 3;
while(y) {
if(y & 1) {
dp[W] *= x;
#ifdef _MSC_VER
unsigned long long x2;
_umul128(dp[W], IM, &x2);
#else
unsigned long long x2 =
(unsigned long long)(((unsigned __int128)(dp[W])*IM) >> 64);
#endif
dp[W] = dp[W] - x2 * MOD+MOD;
}
{
x *= x;
#ifdef _MSC_VER
unsigned long long x2;
_umul128(x, IM, &x2);
#else
unsigned long long x2 =
(unsigned long long)(((unsigned __int128)(x)*IM) >> 64);
#endif
x = x- x2 * MOD + MOD;}
y >>= 1;
}
printf("%d", dp[W] % MOD);
return 0;
}
|
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(z, v.size()) cerr << " " << v[z]; \
cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;
template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
rep(j, v.size()) i >> v[j];
return i;
}
template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
return i >> v.first >> v.second;
}
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
return to_string(x).size();
}
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
template <class T>
void chmax(T& a, const T& b) {
if (a < b) a = b;
}
template <class T>
void chmin(T& a, const T& b) {
if (b < a) a = b;
}
ll N, M;
vll done, keiro;
vvll to;
void dfs(ll i) {
if (done[i]) return;
done[i] = 1;
keiro.pb(i);
for (ll t : to[i]) dfs(t);
}
vll colors;
ll cnt = 0;
void dfs2(ll i) {
if (i == (ll)keiro.size()) {
cnt++;
return;
}
ll index = keiro[i];
vll ng(3, 0);
for (ll t : to[index])
if (colors[t] != 0) ng[colors[t] - 1] = 1;
rep(c, 3) {
if (ng[c]) continue;
colors[index] = c + 1;
dfs2(i + 1);
colors[index] = 0;
}
}
signed main() {
init();
cin >> N >> M;
done.resize(N);
to.resize(N);
rep(i, M) {
ll a, b;
cin >> a >> b;
a--;
b--;
to[a].pb(b);
to[b].pb(a);
}
ll ans = 1;
colors = vll(N, 0);
rep(i, N) {
if (done[i]) continue;
keiro = vll();
dfs(i);
cnt = 0;
dfs2(0);
ans *= cnt;
}
ANS;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int n,m;
vector <int> f[233];
long long Ans = 1;
bool vis[233];
vector <int> v;
long long Now = 0;
void Find(int x)
{
v.push_back(x);
vis[x] = true;
for(auto X : f[x])
{
if(vis[X]) continue;
Find(X);
}
}
int c[233];
void DFS(int Ind)
{
if(Ind == v.size())
{
Now++;
return ;
}
int x = v[Ind];
for(int i=1;i<=3;i++)
{
bool flag = false;
for(auto X : f[x])
{
if(c[X] == i) flag = true;
}
if(flag) continue;
c[x] = i;
DFS(Ind + 1);
c[x] = 0;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
f[x].push_back(y);
f[y].push_back(x);
}
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
v.clear();
Find(i);
DFS(0);
Ans = Ans * Now;
Now = 0;
}
cout << Ans;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout << fixed << setprecision(15);
int n;
cin >> n;
vector<int>x(n);
for(int i=0;i<n;i++)cin >> x[i];
long ans1=0;
for(int i=0;i<n;i++)ans1+=abs(x[i]);
cout << ans1 << endl;
long ans2=0;
for(int i=0;i<n;i++)ans2+=abs(x[i])*(long)abs(x[i]);
cout << sqrt(ans2) << endl;
int ans3=0;
for(int i=0;i<n;i++)ans3=max(ans3,abs(x[i]));
cout << ans3 << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 1000000007
#define inf 1e18
#define endl "\n"
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define pii pair<ll,ll>
#define ump unordered_map
#define mp make_pair
#define pq_max priority_queue<ll>
#define pq_min priority_queue<ll,vi,greater<ll> >
#define all(n) n.begin(),n.end()
#define ff first
#define ss second
#define mid(l,r) (l+(r-l)/2)
#define bitc(n) __builtin_popcount(n)
#define loop(i,a,b) for(long long i=(a);i<=(b);i++)
#define loopr(i,a,b) for(long long i=(a);i>=(b);i--)
const ll N = 1e5 + 5 ;
ll prime[N];
vector<long long>ans;
void sieve()
{
for ( int i = 0 ; i < N; i++)
prime[i] = i;
prime[1] = 1 ;
for (long long i = 2 ; i < N ; i++ ) // always use long long here !!
{
if (prime[i] == i)
{
ans.push_back(i);
for (long long j = i*i ; j < N; j += i)
{
prime[j] = i ;
}
}
}
}
long long powr(int x, int y)
{
if (y == 0) return 1;
long long res=powr(x,y/2);
if (y % 2) return (res*res*x );
return (res*res);
}
void file_i_o()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void solve()
{ ll n ;
cin>>n ;
ll a[n];
ll sum =0 ;
loop(i,0,n-1)
{
cin>>a[i];
if(a[i]>10)
{
sum+= (a[i]-10);
}
}
cout<<sum;
return ;
}
int main() {
clock_t begin = clock();
file_i_o();
sieve();
int t=1;
//cin>>t;
while(t--)
{
solve();
}
#ifndef ONLINE_JUDGE
clock_t end = clock();
cout<<"\n\nExecuted In: "<<double(end - begin) / CLOCKS_PER_SEC*1000<<" ms";
#endif
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
int main(){
ld x,y,r;
cin >> x >> y >> r;
r += 1e-14;
long long ans = 0;
for(int i=ceil(x-r);i<=floor(x+r);i++){
ld sq = (x - i) * (x - i);
ld a = floor(y + sqrt(r * r - sq));
ld b = ceil(y - sqrt(r * r - sq));
ans += (a - b) + 1;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using ll = long long;
using ld = long double;
#define all(s) (s).begin(),(s).end()
#define vcin(n) for(ll i=0;i<ll(n.size());i++) cin>>n[i]
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define rever(vec) reverse(vec.begin(), vec.end())
#define sor(vec) sort(vec.begin(), vec.end())
#define fi first
#define se second
#define P pair<ll,ll>
//const ll mod = 998244353;
const ll mod = 1000000007;
const ll inf = 2000000000000000000ll;
static const long double pi = 3.141592653589793;
void YesNo(bool a){if(a){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}}
void YESNO(bool a){if(a){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}}
template<class T,class U> void chmax(T& t,const U& u){if(t<u) t=u;}
template<class T,class U> void chmin(T& t,const U& u){if(t>u) t=u;}
ll modPow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
int main() {
/* mod は 1e9+7 */
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout<< fixed << setprecision(10);
ld a,b,r;
cin>>a>>b>>r;
r=nextafter(r,1000000.0);
ll ans=0;
for(ll x=ceil(a-r);x<=floor(a+r);x++){
ld d=sqrt(r*r-(x-a)*(x-a));
ans+=floor(b+d)-ceil(b-d)+1;
}
cout<<ans<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
//#define int unsigned int
//#define pb push_back
//#define mp make_pair
#define ff first
#define ss second
//#define lb lower_bound
//#define ub upper_bound
//#define bs binary_search
#define mod 1000000007 // mod+2
#define double long double
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\n"
const int inf=1e18;
const int maxu=200005;
//int d;
//vector <int> adj[maxu];
//vector <int> vis(maxu);
//vector <vector <int>> v(maxu);
/*
int gcd(int a, int b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
int fun(int a,int b){
if(b==0)return 1;
else if(a==1)return 1;
int temp=fun(a,b/2)%mod;
temp=(temp*temp)%mod;
if(b%2==1)temp=(temp*a)%mod;
return temp%mod;
}
int inv(int a,int b){
return (a*fun(b,mod-2))%mod;
}
void dfs(int s){
if(vis[s])return;
vis[s]=1;
for(auto u:adj[s]){
dfs(u);
}
}*/
signed main()
{ /*
#ifndef ONLINE_JUDGE
freopen("inputhsr.txt", "r", stdin);
freopen("outputhsr.txt", "w", stdout);
#endif
*/
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int i,j,k=0,n,t=1,m,l=0;
//cin>>t;
while(t--){
cin>>n;
vector <pair<int,string>> v(n);
for(i=0;i<n;i++){
cin>>v[i].second>>v[i].first;
v[i].first*=-1;
}
sort(all(v));
cout<<v[1].second<<"\n";
/*
for(i=0;i<=n;i++)vis[i]=0;
for(i=0;i<=n;i++){
adj[i].clear();
}
*/
/*string s;
cin>>s;
n=s.length();*/
//memset(a,0,sizeof(a));
//cout<<fixed<<setprecision(10)<<ans<<"\n";
}
return 0;
}
| #include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
int t[n];
string s[n];
string a="string";
string ans;
int b=-1;
int c=-1;
rep(i,n){
cin >> s[i] >> t[i];
if(i==0){
b=t[i];
a=s[i];
} else if(i>0 && b<t[i]){
c=b;
b=t[i];
ans=a;
a=s[i];
}else if(c<t[i]&&b>t[i]){
c=t[i];
ans=s[i];
}
}
cout << ans << endl;
return 0;
} |
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
int main(){
long a,b,c;
cin >> a >> b >> c;
a %= 10;
b %= 4;
long iIndex = 0;
if(b == 0){
iIndex = 4;
} else if (b == 1){
iIndex = 1;
} else if (b == 2){
iIndex = (c > 1) ? 4 : 2;
} else {
iIndex = (c % 2 == 0) ? 1 : 3;
}
long nAns = 1;
for(long i = 0; i < iIndex; i++){
nAns *= a;
nAns %= 10;
}
cout << nAns << endl;
return 0;
}
| #include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <bitset>
#include <cstring>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define FOR(i, a, b) for(int i=a, _n=b; i<=_n; ++i)
#define FORD(i, a, b) for(int i=a, _n=b; i>=_n; --i)
using namespace std;
int mod = 0, m, p, k, e[20], vis[20];
int pw(int b, int c) {
if (c == 1) {
if (b > k) b %= m, mod = 1;
return b;
}
int tmp = pw(b, c >> 1);
tmp = tmp * tmp * (c&1 ? b : 1);
if (mod || tmp > k) tmp %= m, mod = 1;
return tmp;
}
int main()
{
ios::sync_with_stdio();
cin.tie(0); cout.tie(0);
///freopen("a.inp","r",stdin);
int a, b, c;
cin >> a >> b >> c;
a %= 10;
memset(vis, -1, sizeof(vis));
e[0] = 1; vis[1] = 0;
k = 0; while (-1 == vis[(e[k] * a) % 10]) {
e[k+1] = (e[k] * a) % 10;
k += 1; vis[e[k]] = k;
}
p = vis[(e[k] * a) % 10];
m = k - p + 1;
int tmp = pw(b, c);
if (mod) while (tmp < p) tmp += m;
cout << e[tmp];
}
|
#include <bits/stdc++.h>
using namespace std;
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;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __Setup;
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define repl(i, a, b) for(int i = a; i < int(b); i++)
#define rep(i, n) repl(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//-------------------------------------
int main() {
int N;
ll X;
cin >> N >> X;
vector<ll> a(N);
rep(i, N) cin >> a[i];
vector<map<ll, ll>> dp(60);
dp[0][X] = 1;
rep(i, N) {
for(const auto &[j, val] : dp[i]) {
if(i < N - 1) {
ll r = j % a[i + 1];
dp[i + 1][j - r] += val;
if(r > 0) dp[i + 1][j + a[i + 1] - r] += val;
} else {
dp[i + 1][0] += val;
}
}
}
cout << dp[N][0] << "\n";
} | #include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, m;
char s[N][N];
pair<int, int> st, en;
vector<pair<int, int>> teleports[26];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int dist[N][N];
bool used[26];
bool vis[N][N];
void bfs() {
queue<pair<int, int>> q;
q.push(st);
vis[st.first][st.second] = true;
dist[st.first][st.second] = 0;
while (q.size()) {
int x, y;
tie(x, y) = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int nx = x + dx[k], ny = y + dy[k];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (s[nx][ny] == '#') continue;
if (vis[nx][ny]) continue;
vis[nx][ny] = true;
dist[nx][ny] = dist[x][y] + 1;
q.emplace(nx, ny);
}
if ('a' <= s[x][y] && s[x][y] <= 'z' && !used[s[x][y] - 'a']) {
used[s[x][y] - 'a'] = true;
for (pair<int, int> p : teleports[s[x][y] - 'a']) {
int nx, ny;
tie(nx, ny) = p;
if (vis[nx][ny]) continue;
vis[nx][ny] = true;
dist[nx][ny] = dist[x][y] + 1;
q.emplace(nx, ny);
}
}
}
}
int solve() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%s", s[i]);
for (int j = 0; j < m; ++j) {
if (s[i][j] == 'S') {
st = make_pair(i, j);
} else if (s[i][j] == 'G') {
en = make_pair(i, j);
} else if ('a' <= s[i][j] && s[i][j] <= 'z') {
teleports[s[i][j] - 'a'].emplace_back(i, j);
}
}
}
bfs();
if (!vis[en.first][en.second]) printf("-1\n");
else printf("%d\n", dist[en.first][en.second]);
return 0;
}
int main() {
int t = 1;
// scanf("%d", &t);
for (int tc = 0; tc < t; ++tc) {
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
int x;
scanf("%d",&x);
printf("%d",100-x%100);
return 0;
} | #include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
char c=getchar();int x=0,f=1;
for(;c<'0'||c>'9';c=getchar())if(c=='-')f=0;
for(;c<='9'&&c>='0';c=getchar())x=(x<<1)+(x<<3)+(c^48);
return f?x:-x;
}
inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;}
int a[4];
int main(){
for(int i=1;i<=3;i++)a[i]=read();
sort(a+1,a+4);
printf("%d",a[2]+a[3]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main(){
int N;
cin >> N;
vector<int> W(N);
for (int i = 0; i < N; i++){
cin >> W[i];
}
int S = 0;
for (int i = 0; i < N; i++){
S += W[i];
}
if (S % 2 == 1){
cout << 0 << endl;
} else {
vector<vector<long long>> dp(1, vector<long long>(S / 2 + 1, 0));
dp[0][0] = 1;
for (int i = 0; i < N; i++){
vector<vector<long long>> dp2(i + 2, vector<long long>(S / 2 + 1, 0));
for (int j = 0; j <= i; j++){
for (int k = 0; k <= S / 2; k++){
if (dp[j][k] > 0){
dp2[j][k] += dp[j][k];
dp2[j][k] %= MOD;
if (k + W[i] <= S / 2){
dp2[j + 1][k + W[i]] += dp[j][k];
dp2[j + 1][k + W[i]] %= MOD;
}
}
}
}
swap(dp, dp2);
}
vector<long long> cnt(N + 1, 0);
for (int i = 0; i <= N; i++){
cnt[i] = dp[i][S / 2];
}
vector<long long> fact(N + 1, 1);
for (int i = 1; i <= N; i++){
fact[i] = fact[i - 1] * i % MOD;
}
long long ans = 0;
for (int i = 0; i <= N; i++){
ans += fact[i] * fact[N - i] % MOD * cnt[i] % MOD;
}
ans %= MOD;
cout << ans << endl;
}
} | # pragma GCC optimize ("O3")
# pragma GCC optimize ("Ofast")
# pragma GCC optimize ("unroll-loops")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define rep2(i,a,b) for(int i = (b) - 1; i >= (a); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define em emplace
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define x first
#define y second
using namespace std;
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using ti = tuple<int, int, int>;
using pl = pair<ll, ll>;
using tl = tuple<ll, ll, ll>;
const int MOD = 998244353;
int N, W[111], tot;
template<class T> struct mi {
T val;
mi() { val = 0; }
mi(const ll& v) {
val = (-MOD <= v && v <= MOD) ? v : v % MOD;
if (val < 0) val += MOD;
}
mi operator + (const mi& m) {
T tmp = (val + m.val) % MOD;
return tmp + (tmp < 0 ? MOD : 0);
}
mi operator - (const mi& m) {
T tmp = (val - m.val) % MOD;
return tmp + (tmp < 0 ? MOD : 0);
}
mi operator * (const mi& m) {
T tmp = (val * m.val) % MOD;
return tmp;
}
mi& operator += (const mi& m) {
if ((val += m.val) >= MOD) val -= MOD;
return *this;
}
mi& operator -= (const mi& m) {
if ((val -= m.val) < 0) val += MOD;
return *this;
}
mi& operator *= (const mi& m) {
val = (val * m.val) % MOD;
return *this;
}
};
mi<ll> f[111], dp[111][11111], ans;
int main() {
cin.tie(0) -> sync_with_stdio(false); cout.tie(0);
cin >> N;
rep(i, 0, N) {
cin >> W[i];
tot += W[i];
}
if(tot & 1){cout << "0\n"; return 0;}
tot >>= 1;
f[0] = 1;
rep(i, 1, N + 1) f[i] = f[i - 1] * i;
dp[0][0] = 1;
rep(i, 0, N) rep2(j, 0, i + 1) rep(k, W[i], 100 * N + 1) {
dp[j + 1][k] += dp[j][k - W[i]];
}
rep(j, 0, N + 1) ans += f[j] * f[N - j] * dp[j][tot];
cout << ans.val << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int f[3][3] = {
{1, 0, 0},
{1, 1, 0},
{1, 2, 1}
};
const int mod = 3;
int lucas(int x, int y) {
int res = 1;
while (x) {
res = res * f[x % mod][y % mod] % mod;
x /= mod, y /= mod;
}
return res;
}
int mp[200];
void solve() {
mp['B'] = 0, mp['W'] = 1, mp['R'] = 2;
int n, res = 0; cin >> n; --n;
for (int i = 0; i <= n; ++i) {
char c; cin >> c;
int x = mp[c];
res = (res + lucas(n, i) * x) % mod;
}
if (n & 1) res = -res;
res = (res + mod) % mod;
if (res == 0) putchar('B');
if (res == 1) putchar('W');
if (res == 2) putchar('R');
}
int main() {
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i,n) for (int i = 0; i < (int)(n) ; i++)
#define rep2(i,a,b) for (int i = (a); i < (int)(b); i++)
#define repR(i,n) for (int i = (int)(n)-1; i >= 0; i--)
#define ENDL '\n'
#define pb push_back
#define SIZE(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define UNIQUE(a) a.erase(unique(all(a)),a.end());
#define debug(x) cout<<(#x)<<" is : "<<x<<endl;
#define debugc(vec) {cout<<(#vec)<<" is below; ---"<<endl;for(auto c:vec)cout<<c<<" ";cout<<endl<<"----------------"<<endl;}
const int INF = (1<<30)-1;
const ll INFLL = (1LL<<62)-1;
const ld PI = 3.14159265358979323846L;
const ll MOD = 1000000007;
ll modpow(ll x,ll y,ll mod) { //繰り返し二乗法
if(mod==1) return 1;//特殊処理
if(y==0) return 1;
if(y==1) return x%mod;
if(y%2==1) return x*modpow(x,y-1,mod)%mod;
ll rt = modpow(x,y/2,mod);
return rt*rt%mod;
}
int main() {
cout << fixed << setprecision(15);
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
ll n,p; cin>>n>>p;
ll ans=p-1;
ans = ans*modpow(p-2,n-1,MOD)%MOD;
cout<<ans<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define f first
#define s second
const int N = 20;
int n , m , ans , cur , dp[1<<N];
bool edge[N][N] , good[1<<20];
vector<int> v;
void solve(int i)
{
if (i == n)
{
for(int j=0; j<v.size(); j++)
{
for(int jj=j+1; jj<v.size(); jj++)
{
if (!edge[v[j]][v[jj]]) return;
}
}
good[cur] = 1;
return;
}
v.push_back(i);
cur |= (1<<i);
solve(i+1);
v.pop_back();
cur ^= (1<<i);
solve(i+1);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i=1; i<=m; i++)
{
int x,y;
cin >> x >> y;
x-- , y--;
edge[x][y] = 1 , edge[y][x] = 1;
}
solve(0);
ll tmp = 1<<n;
for(int i=1; i<=tmp; i++)
{
dp[i] = 1e9;
}
for(int i=1; i<tmp; i++)
{
if (good[i]) dp[i] = min(dp[i] , 1);
for(int j=i; j>0; j = ((j-1)&i) )
{
if (good[i^j])
dp[i] = min(dp[i] , dp[j]+1);
}
}
cout << dp[tmp-1] << '\n';
return 0;
} | #include <cstdio>
#include <algorithm>
const int Maxn=200000;
const int Inf=0x3f3f3f3f;
int n,k;
int head[Maxn+5],arrive[Maxn<<1|5],nxt[Maxn<<1|5],tot;
void add_edge(int from,int to){
arrive[++tot]=to;
nxt[tot]=head[from];
head[from]=tot;
}
int dis;
int num;
int f_1[Maxn+5],f_2[Maxn+5];
void work_dfs(int u,int fa){
f_1[u]=-Inf;
f_2[u]=Inf;
for(int i=head[u];i;i=nxt[i]){
int v=arrive[i];
if(v==fa){
continue;
}
work_dfs(v,u);
f_1[u]=std::max(f_1[u],f_1[v]+1);
f_2[u]=std::min(f_2[u],f_2[v]+1);
}
if(f_2[u]>dis){
f_1[u]=std::max(f_1[u],0);
}
if(f_1[u]+f_2[u]<=dis){
f_1[u]=-Inf;
}
if(f_1[u]>=dis){
num++;
f_2[u]=0;
f_1[u]=-Inf;
}
}
bool check(int x){
dis=x;
num=0;
work_dfs(1,0);
if(f_1[1]>=0){
num++;
}
return num<=k;
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
add_edge(u,v);
add_edge(v,u);
}
int left=1,right=n;
while(left<right){
int mid=(left+right)>>1;
if(check(mid)){
right=mid;
}
else{
left=mid+1;
}
}
printf("%d\n",left);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
//#define cccgift
#define lowbit(x) ((x)&-(x))
#define rep(i,l,r) for(res i=l,_r=r;i<=_r;++i)
#define per(i,r,l) for(res i=r,_l=l;i>=_l;--i)
#define mkp make_pair
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem0n(a,n) memset(a,0,n*sizeof(a[0]))
#define iter(x,v) for(res v,_p=head[x];v=ver[_p],_p;_p=nxt[_p])
#ifdef cccgift //by lqh
#define SHOW(x) cerr<<#x"="<<(x)<<endl
#else
#define SHOW(x) 0
#endif
//#define getchar()(ip1==ip2&&(ip2=(ip1=ibuf)+fread(ibuf,1,1<<21,stdin),ip1==ip2)?EOF:*ip1++)
//char ibuf[1<<21],*ip1=ibuf,*ip2=ibuf;
template<typename T>
inline void read(T &x)
{
static char ch;bool f=1;
for(x=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') f=0;
for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=getchar());x=f?x:-x;
}
template<typename T>
void print(T x)
{
if (x<0) x=-x,putchar('-');
if (x>9) print(x/10);
putchar(x%10+48);
}
template<typename T>
inline void print(T x,char ap) {print(x);if (ap) putchar(ap);}
template<typename T>
inline void chkmax(T &x,const T &y) {x=x<y?y:x;}
template<typename T>
inline void chkmin(T &x,const T &y) {x=x<y?x:y;}
int n,k;
char s[201];
inline char doit(char ch1,char ch2) {
if(ch1==ch2) return ch1;
if(ch1>ch2) swap(ch1,ch2);
if(ch1=='P'&&ch2=='R') return 'P';
if(ch1=='P'&&ch2=='S') return 'S';
return 'R';
}
int main()
{
read(n),read(k),scanf("%s",s+1);
rep(i,1,k) {
if(n&1) {
rep(i,n+1,n+n) s[i]=s[i-n];
n*=2;
}int len=0;
for(res j=1;j<=n;j+=2) s[++len]=doit(s[j],s[j+1]);
n=len;
}
putchar(s[1]),puts("");
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?), set tle
* do something instead of nothing and stay organized
*/
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ff first
#define ss second
#define rsz resize
#define ins insert
#define mp make_pair
#define pf push_front
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rep(i,a,b) for (int i = (a); i < (b); ++i)
#ifndef ONLINE_JUDGE
#define deb(x) cerr<< #x <<" "; __print(x); cerr<<endl;
#else
#define deb(x)
#endif
const ll mod = 1e9+7; // 998244353;
const ll inf = 1e18;
const ld pie = acos((ld)-1);
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void __print(int x) {cerr<<x;}
void __print(char x) {cerr<<x;}
void __print(string x) {cerr<<x;}
void __print(float x) {cerr<<x;}
void __print(double x) {cerr<<x;}
void __print(bool x) {cerr<<x;}
void __print(ll x) {cerr<<x;}
template<class T, class U> void __print(pair<T,U> p) {cerr<<"{"<<p.ff<<", "<<p.ss<<"}";}
template<class T> void __print(vector<T> arr) { cerr<<"[ "; for(auto x:arr) { __print(x);cerr<<" "; } cerr<<"]"; }
template<class T> void __print(set<T> st) { cerr<<"{ "; for(auto x:st) { __print(x); cerr<<" "; } cerr<<"}";}
template<class T, class U> void __print(vector<pair<T,U>> arr) { cerr<<"[ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"]";}
template<class T, class U> void __print(unordered_map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<class T, class U> void __print(map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<typename... T> void read(T&... args) { ((cin>>args),...);}
template<typename... T> void write(T&&... args){((cout<<args<<" "),...);cout<<'\n';}
int N;
vector<pair<ll,ll>> gr[200001];
vector<ll> bfs(ll src) {
vector<ll> dis(N, -1);
queue<ll> q;
q.push(src);
dis[src] = 0;
while(!q.empty()) {
ll node = q.front();
q.pop();
for(auto x:gr[node]) {
if(dis[x.ff] == -1) {
dis[x.ff] = dis[node] ^ x.ss;
q.push(x.ff);
}
}
}
return dis;
}
int main(){
scanf("%d", &N);
rep(i, 0, N - 1) {
ll X,Y,W;
scanf("%lld%lld%lld", &X, &Y, &W);
X--;Y--;
gr[X].pb({Y, W});
gr[Y].pb({X, W});
}
vector<ll> dis = bfs(0);
ll ans = 0;
for(int i = 0; i < 60; ++i) {
vector<int> cnt(2);
for(int j = 0; j < N; ++j) {
cnt[dis[j]>>i&1]++;
}
ans += (1ll<<i)%mod*cnt[0]%mod*cnt[1]%mod;
ans %= mod;
}
printf("%lld\n", ans);
return 0;
/*STUFF TO LOOK:
1. Check the constraints
2. Corner cases(n==0||n==1)
3. Variables in loops
4. Make sure two ints aren’t multiplied to get a long long
5. Don't use scanf or printf while using ios_base
6. using the local variable of the same name when global variable was required to be used
*/
}
|
#include <bits/stdc++.h>
using namespace std;
double eps = 1e-9;
double pi = acos(-1);
int fx[8] = {1, -1, 0, 0, 1, -1, -1, 1};
int fy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
#define fast_io ios_base::sync_with_stdio(false); cin.tie(0);
#define ll long long
#define en "\n"
#define ff first
#define ss second
#define sp(x) fixed << setprecision(x)
#define VECT(v) vector<int>v
#define SCAN(v) int temp; for(int i=0; i<n; i++) {cin>>temp; v.push_back(temp);}
#define PRINT(v) for(int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << en;
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.begin(), v.end(), greater<>())
#define CASEP(v) cout<<"Case "<<tc<<": "<<v<<"\n"
#define DEBUG(v) cout << v << " "; cout << en;
#define mem(a, b) memset(a, b, sizeof(a))
#define valid(nx, ny, row, col) nx >= 0 && nx < row && ny >= 0 && ny < col
ll gcd(ll a, ll b)
{
if(b == 0)
return a;
else
return gcd(b, a % b);
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
fast_io
int n, val, mx = 0;
cin >> n;
VECT(v);
for(int i = 0; i < n; i++)
{
cin >> val;
v.push_back(val);
mx = max(mx, val);
}
ll ans;
ll num = 0;
for(int i = 2; i <= mx; i++)
{
int cnt = 0;
for(int j = 0; j < n; j++)
{
if(v[j] % i == 0)
{
cnt++;
}
}
if(cnt > num)
{
num = cnt;
ans = i;
}
}
cout << ans << en;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int N, c, r, i{2}, m;
int main()
{cin >> N; int A[N]; for (int &a: A) cin >> a;
for (; c = 0, i <= 1000; m = c > m ? r = i, c : m, ++i) for (int a : A) c += !(a % i);
cout << r;} |
// abc199_b
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../../debug_util/cxx-prettyprint/prettyprint.hpp"
#include "../../debug_util/rng.hpp"
#include "../../debug_util/timer.hpp"
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define REP(i, n) for (int i = 0 ; i < (int)(n) ; ++i)
#define REPN(i, m, n) for (int i = m ; i < (int)(n) ; ++i)
#define REP_REV(i, n) for (int i = (int)(n) - 1 ; i >= 0 ; --i)
#define REPN_REV(i, m, n) for (int i = (int)(n) - 1 ; i >= m ; --i)
#define ALL(x) x.begin(), x.end()
#define INF (ll)(1e15)
#define MOD (1000000007)
#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << "\n";}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;
int main() {
#ifdef LOCAL
ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif
int N;
cin >> N;
vector<int> A(N), B(N);
REP(i, N) cin >> A[i];
REP(i, N) cin >> B[i];
int a = *max_element(ALL(A));
int b = *min_element(ALL(B));
int ans = max(0, b - a + 1);
print(ans);
return 0;
}
| //@formatter:off
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < int(n); ++i)
#define rrep(i,n) for (int i = int(n)-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, a, b;
cin >> n >> a >> b;
cout << n - a + b << endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define N 400005
struct node{int pos,val;}b[N];
int a[N],n,i,val[N],st[N],hd;
char ans[N];
inline bool cmp(node aa,node bb){return aa.val<bb.val;}
int main(){
cin>>n;
for(i=1;i<=n+n;++i)cin>>a[i],b[i].pos=i,b[i].val=a[i];
sort(b+1,b+n+n+1,cmp);
for(i=n+1;i<=n+n;++i){
val[b[i].pos]=1;
}
for(i=1;i<=n+n;++i){
if(hd&&val[st[hd]]!=val[i]){
ans[st[hd]]='(';
ans[i]=')';
--hd;
}
else st[++hd]=i;
}
for(i=1;i<=n+n;++i)cout<<ans[i];
} | #include <iostream>
#include <map>
using namespace std;
void test_case()
{
int N,curr;
cin>>N;
map<int,int> freq;
for (int index=0; index<N;index+=1){
cin>>curr;
freq[curr] += 1;
}
string res = "Second";
if (N%2 == 0){
// even
// A goal is to break symmerty and B goal is to build symmetry of 2 columns
// Symmetry can be achieved only if all num have duplicates
for (auto &[_,value]:freq){
if (value % 2 == 1){
// no duplicate
res = "First";
}
}
}else{
//odd
// A goal is maintain symmerty of 2 columns
// (possible if a+b == c, but on taking a, B can take b making it asymmetric)
// therefore always B wins
}
cout<<res<<"\n";
}
int main()
{
int N;
cin>>N;
while(N-->0){
test_case();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define pb push_back
#define fi first
#define se second
using ll = long long;
using G = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 1e8;
const ll MOD = 1000000007;
ll GCD(ll x, ll y){
return y ? GCD(y, x%y) : x;
}
ll LCM(ll x, ll y){
return x/GCD(x, y)*y;
}
int main() {
int n; cin >> n;
string s[100];
rep(i, 0, n) cin >> s[i];
ll ans = 1;
for(int i=n-1;i>=0;i--){
if(s[i]=="AND"){
continue;
}else{
ll addans = 1;
rep(j, 0, i+1){
addans *= 2;
}
ans += addans;
}
}
cout << ans << endl;
return 0;
} | #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define tf(x) get<0>(x)
#define ts(x) get<1>(x)
#define tt(x) get<2>(x)
#define endl '\n'
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define tup tuple<int,int,int>
#define ve vector <int>
#define vep vector<pii >
#define mod 1000000007
#define maxn 2e5+5
#define ld long double
#define mem(x) memset(x,0,sizeof(x))
#define in insert
#define fr(i,a,b) for(int i=a;i<b;i++)
#define forn(i,n) for(int i=0;i<n;i++)
#define fora(m) for(auto it:m)
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define input_array int n;cin>>n;int a[n];forn(i,n) cin>>a[i];
#define oset tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int mull(int a,int b) {
return ((a%mod)*(b%mod))%mod;
}
int power(int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
int modInverse(int n, int p)
{
return power(n, p-2, p);
}
struct cmp {
bool operator() (const pair<int, int> &a, const pair<int, int> &b) const {
int lena = a.second - a.first + 1;
int lenb = b.second - b.first + 1;
if (lena == lenb) return a.first < b.first;
return lena > lenb;
}
};
//--------------------------------SIEVE---------------------------//
/*bool prime[maxn];
int spf[maxn];
void sieve() {
forn(i,maxn) {
prime[i]=true;
spf[i]=i;
}
fr(i,2,maxn) {
if(prime[i]){
for(int j=2*i;j<maxn;j+=i) {
prime[j]=false;
spf[j]=i;
}
}
}
}*/
//-----------------------------End---------------------------------//
//------------------------------------------------DFS------------------------//
/*bool stat[maxn];
int depth[maxn],par[maxn];
void init_graph() {
memset(stat,false,sizeof(stat);
memset(depth,0,sizeof(depth);
for(int i=0;i<maxn;i++) par[i]=i;
}
void dfs(int u,int p) {
stat[u]=true;
depth[u]=depth[p]+1;
par[u]=p;
for(auto it:g[u]) {
if(stat[it]==false)
dfs(it,u);
}
}*/
//-------------------------------------DFS-END--------------------------------//
//-----------------------------------nCr%p-----------------------------------//
/*int nCr(int n, int r)
{
if (r==0)
return 1;
int fac[n+1];
fac[0] = 1;
for (int i=1 ; i<=n; i++)
fac[i] = fac[i-1]*i%mod;
return (fac[n]* modInverse(fac[r], mod) % mod *
modInverse(fac[n-r], mod) % mod) % mod;
} */
//---------------------------------EnD---------------------------------------//
void solve() {
int n;
cin>>n;
int a[n+1];
fr(i,1,n+1) cin>>a[i];
int dp[n+1][n+1],pre[n+1][n+1];
mem(dp);
memset(pre,-1,sizeof(pre));
for(int i=1;i<=n;i++) pre[i][0] = 0;
dp[1][1] = 1;
// pre[1][0] = 1;
int sum = a[1];
for(int i=1;i<=n;i++) pre[i][sum%i] = 1;
for(int i=2;i<=n;i++) {
sum+=a[i];
for(int j=1;j<=n;j++) {
int mo = sum%j;
if(pre[j][mo]==-1) {
pre[j][mo] = i;
continue;
}
// if(pre[j][mo]==0) {
// dp[i][j] = (dp[i-1][j-1] + dp[i-1][j])%mod;
// pre[j][mo] = i;
// continue;
// }
dp[i][j] = (dp[pre[j][mo]][j-1] + dp[pre[j][mo]][j])%mod;
pre[j][mo] = i;
// cout<<dp[i][j]<<" ";
}
// cout<<endl;
}
// for(int i=1;i<=n;i++) {
// for(int j=1;j<=i;j++) cout<<dp[i][j]<<" ";
// cout<<endl;
// }
int ans= 0;
for(int i=1;i<=n;i++) ans = (ans+dp[n][i])%mod;
cout<<ans;
}
signed main() {
IOS
int t=1;
// cin>>t;
while(t--) {
solve();
}
return 0;
} |
#include <bits/stdc++.h>
int n;
int main(){
std::scanf("%d", &n);
for(int i = 1; i <= n; ++i)
std::printf("%d %d\n", i * 2 % n + 1, (i * 2 - 1) % n + 1);
} | #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define MTP make_tuple
typedef long long int ll;
constexpr int kN = int(1E3 + 10), kInf = int(1E9 + 10);
int a[kN], b[kN];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) a[i] = ((i - 1) << 1) % n + 1;
for (int i = 1; i <= n; i++) b[i] = ((i - 1) << 1 | 1) % n + 1;
for (int i = 1; i <= n; i++) printf("%d %d\n", a[i], b[i]);
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n; cin >> n;
vector<pair<int, int>> z;
pair<int, int> c;
for (int i=0; i<n; i++)
{
cin >> c.first >> c.second;
z.push_back(c);
}
bool flag = false;
for (int i1=0; i1<n; i1++)
{
for (int i2=i1+1; i2<n; i2++)
{
for (int i3=i2+1; i3<n; i3++)
{
int p = (z[i1].first - z[i2].first) * (z[i1].second - z[i3].second);
int q = (z[i1].first - z[i3].first) * (z[i1].second - z[i2].second);
if (p == q)
{
flag = true;
break;
}
}
if (flag) break;
}
if (flag) break;
}
if (flag) cout << "Yes" << endl;
else cout << "No" << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll t;
scanf("%lld",&t);
while(t--){
ll l,r;
scanf("%lld %lld",&l,&r);
ll t = (r-l)-l+1;
ll res;
if(t>0) res = (t+1)*t/2;
else res = 0;
printf("%lld\n",res);
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using ull=unsigned long long;
const ll LL_MAX=LLONG_MAX;
struct In{
template<typename T>
operator T(){
T x;
cin >> x;
return x;
}
};
In in;
template <typename T,typename U>
void forin(T* x,U n){
for (U i=0;i<n;i++) cin >> x[i];
}
template <typename T>
int div1(T x) {
return x%1000000007;
}
template <typename T>
void out(T x){
cout << x << endl;
}
template <typename T>
T gcd(T x,T y){
if (y == 0) return x;
return gcd(y,x%y);
}
template <typename T>
T lcm(T x,T y){
return x*y/gcd(x,y);
}
int main(){
ull n=in,a,b;
ull ans=0;
for (int i=0;i<n;i++){
a=in,b=in;
ans+=(b*b+b-a*a+a)/2;
}
out(ans);
}
| #include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(0), cin.tie(NULL)
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl '\n'
#define mkp make_pair
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
constexpr int INF = INT_MAX;
using namespace std;
int main(){
fastIO;
int N;
cin >> N;
ll sum = 0;
while (N--)
{
ll a, b;
cin >> a >> b;
sum += ((b * (b + 1)) / 2) - ((a * (a + 1)) / 2) + a;
}
cout << sum;
return 0;
} |
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 4020000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
if(a>b){
a = b; return true;
}
else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
if(a<b){
a = b; return true;
}
else return false;
}
template<typename T> inline void print(T &a){
int sz = a.size();
for(auto itr = a.begin(); itr != a.end(); itr++){
cout << *itr;
sz--;
if(sz) cout << " ";
}
cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const int mod = 1e9 + 7;
const int mod = 998244353;
template<typename T> struct BIT{
vector<T> dat;
ll sz;
//all 1-indexed
BIT(ll sz) : sz(sz){
dat.assign(++sz, 0);
}
T sum(ll k){
T ret = 0;
for(++k; k > 0; k -= k & -k) ret += dat[k];
return (ret);
}
void add(ll k, T x){
for(++k; k < dat.size(); k += k & -k) dat[k] += x;
}
ll get(T k){
if(k <= 0) return 0;
ll ret = 0;
int n = 1; while(n < sz) n *= 2;
for(int i=n/2; i>0; i/=2){
if(ret+i < sz && dat[ret+i] < k){
k -= dat[ret+i];
ret += i;
}
}
return ret;
}
};
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,m; cin >> n >> m;
vl a(n); rep(i,n) cin >> a[i];
vl cnt(n,0);
BIT<int> bit(n);
rep(i,m){
cnt[a[i]]++;
if(cnt[a[i]] == 1){
bit.add(a[i],1);
}
}
int mn = n;
rep(i,n-m+1){
int ok = m, ng = -1;
while(ok-ng > 1){
int mid = (ok+ng) / 2;
if(bit.sum(mid) < mid+1) ok = mid;
else ng = mid;
}
chmin(mn,ok);
cnt[a[i]]--;
if(cnt[a[i]] == 0){
bit.add(a[i],-1);
}
if(i<n-m){
cnt[a[i+m]]++;
if(cnt[a[i+m]] == 1){
bit.add(a[i+m],1);
}
}
}
cout << mn << "\n";
} | #include <bits/stdc++.h>
using namespace std;
int arr[1500002];
int krr[1500002];
int tree[4*1500002];
void update(int node,int b,int e,int i,int val)
{
if(b==e&&b==i){
tree[node]=val;
return;
}
int mid=(b+e)>>1;
int left=node*2;
int right=left+1;
if(i<=mid)update(left,b,mid,i,val);
else update(right,mid+1,e,i,val);
tree[node]=tree[left]+tree[right];
}
int query(int node,int b,int e)
{
if(b==e){
return b;
}
int mid=(b+e)>>1;
int left=node<<1;
int right=left+1;
if(tree[left]>0)return query(left,b,mid);
else return query(right,mid+1,e);
}
int main()
{
int n,m;
map<int,int>maps;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&arr[i]);
arr[i]++;
}
n+=10;
for(int i=1;i<=n;i++)update(1,1,n,i,1);
int ans=INT_MAX;
for(int i=1;i<=m;i++){
maps[arr[i]]++;
if(maps[arr[i]]>0)update(1,1,n,arr[i],0);
}
ans=min(ans,query(1,1,n));
for(int i=m+1;i<=n-10;i++){
int l=i-m;
int r=i;
maps[arr[l]]--;
if(maps[arr[l]]<=0)update(1,1,n,arr[l],1);
maps[arr[r]]++;
if(maps[arr[r]]>0)update(1,1,n,arr[r],0);
ans=min(ans,query(1,1,n));
}
printf("%d\n",ans-1);
return 0;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef aa
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//const int mod = 998244353;
const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;
uniform_int_distribution<int> rng(10,1e8);
//DEBUG FUNCTIONS START
#define cerr cout
void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
//#define deb(x...) cerr << "[" << #x << "] = "; _print(x)
#define deb(x...) _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END
const int N = 1e5 + 5;
pii f(int b, int c){
int n = c / 2;
if(c & 1){
return {-b-n, -b+n};
}else{
if(!c)
return {b, b};
return {b-n, b+n-1};
}
}
void solve(){
int b, c;
cin >> b >> c;
auto [x, y] = f(b, c);
auto [p, q] = f(b, c-1);
cout << y-x+1 + q-p+1 - max(0ll, min(y, q) - max(x, p) + 1) << endl;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
int powm(int a, int b){
int res = 1;
while (b) {
if (b & 1)
res = (res*a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int divide(int a, int b) {
return (a % mod) * powm(b % mod, mod - 2) % mod;
}
int norm(int a) {
while (a >= mod) a -= mod;
while (a < 0) a += mod;
return a;
} | #include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define int long long
#define pb push_back
#define pii pair<int,int>
#define endl "\n"
#define gok ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9 + 7,mxn=1e5 + 5;
signed main(){
gok
int t=1;
//cin>>t;
while(t--)
{
int b,c;
cin>>b>>c;
if(c==1 && b!=0)
{
cout<<2;
return 0;
}
int st = b - (c/2);
int en = b + ((c-1)/2);
if(c%2==0)
st++;
else
en--;
int an = 2*(en-st+1);
int mi = abs(min(abs(st),abs(en)));
if(st<=0 && en>=0){
an -= 2*mi + 1;
if(c%2==0)
{
if(abs(st-1) > abs(en))
an++;
}
else
{
if(abs(en+1) > abs(st))
an++;
}
}
else
an++;
/*if(c%2==0 && st!=0)
an--;
if((c-1)%2==0 && en!=0)
an--;*/
//cout<<st<<" "<<en<<endl;
cout<<an<<endl;
}
return 0;
}
|
//{{{
#include<algorithm>
#include<cmath>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator >> (istream& in, V<E>& v){ for(auto& e : v){ in >> e; } return in; }
struct Init { Init(){
cin.tie(0); ios::sync_with_stdio(false);
} } init;
//}}}
int main(){
int n; cin >> n;
string s; cin >> s;
string s2;
for(auto& c : s){
s2 += c;
int n2 = s2.size();
if(n2 >= 3 && s2[n2 - 3] == 'f' && s2[n2 - 2] == 'o' && s2[n2 - 1] == 'x'){
for(int i = 0; i < 3; i++){
s2.pop_back();
}
}
}
cout << s2.size() << '\n';
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int i,N; cin>>N;
string S,kariS; cin>>S;
int Q; cin>>Q;
int T[300005],A[300005],B[300005];
for(i=0;i<Q;i++) cin>>T[i]>>A[i]>>B[i];
string S1,S2;
S1=S.substr(0,N);
S2=S.substr(N);
for(i=0;i<Q;i++){
if(2==T[i]){
swap(S1,S2);
}
if(1==T[i]){
if(B[i]<=N) swap(S1[A[i]-1],S1[B[i]-1]);
else if(A[i]>N)
swap(S2[A[i]-N-1],S2[B[i]-N-1]);
else swap(S1[A[i]-1],S2[B[i]-N-1]);
}
}
cout<<S1+S2;
} |
#include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;
const int maxSz = 2e5, maxCnt = 16;
char s[maxSz + 1];
int n[maxSz]; int sz, k;
const lli mod = 1e9 + 7;
lli memo[maxSz][maxCnt + 1][2][2];
lli solve(int mask = 0, int i = 0, bool equal = true, bool started = false)
{
int cnt = __builtin_popcount(mask);
if (cnt > k)
return 0;
if (i == sz)
{
return cnt == k && started;
}
lli &ans = memo[i][cnt][equal][started];
if (ans != -1)
return ans;
ans = 0;
for (int d = 0; d <= (equal ? n[i] : 15); d++)
ans = (ans + solve(d == 0 && !started ? mask : mask | (1 << d), i + 1, equal & (d == n[i]), started | (d > 0))) % mod;
return ans;
}
int main()
{
while (~scanf(" %s %d", s, &k))
{
sz = strlen(s);
for (int i = 0; i < sz; i++)
{
if (s[i] >= '0' && s[i] <= '9')
n[i] = s[i] - '0';
else
n[i] = s[i] - 'A' + 10;
}
DEBUG
{
printf("%s\n", s);
for (int i = 0; i < sz; i++)
printf("%d ", n[i]);
printf("\n");
}
memset(memo, -1, sizeof(memo));
lli ans = solve();
printf("%lld\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, x, y;
while (cin >> a >> b >> x >> y)
{
int a2 = 0x7fffffff, a3 = 0x7fffffff, a4 = 0x7fffffff;
int a1 = abs(a - b) * y + x;
if (a > b)
a2 = ((a - b) * 2 - 1) * x, a4 = (a - b - 1) * y + x;
else
a3 = ((b - a) * 2 + 1) * x;
cout << min({a1, a2, a3, a4}) << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define si(n) scanf("%d",&n);
#define PI acos(-1.0)
#define sl(n) scanf("%lld",&n);
#define sf(n) scanf("%lf",&n);
#define ss(n) scanf("%s",n);
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define forc(i,j,n) for(ll i=j;i<n;i++)
#define pb push_back
#define sor(n) sort(n.begin(),n.end())
#define sorcmp(n) sort(n.begin(),n.end(),cmp)
#define rev(n) reverse(n.begin(),n.end())
#define pi(x) printf("%d",x);
#define pl(x) printf("%lld",x);
#define pf(x) printf("%.10lf",x);
#define ps(x) printf("%s",x);
#define en() printf("\n");
#define random_shuffle(r...)random_shuffle(r,[](int _){return rand()%_;})
#define SQR(a) (a)*(a)
#define sp(x) fixed<<setprecision(x)
#define mod 1000000007
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define bug cout<<"Here"<<endl
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
template <typename T> T POW(T B,T P){ if(P==0) return 1; if(P&1) return B*POW(B,P-1); else return SQR(POW(B,P/2));}
ll powmod(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%mod;a = a*a%mod;b >>= 1;}return ans;}
int main()
{
int a,b,c;
cin>>a>>b>>c;
cout<<a-b+c<<endl;
return 0;
}
| #include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int a;
cin >> a;
int b;
cin >> b;
int c;
c = n - a + b;
cout << c;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
size_t N, M;
cin >> N >> M;
vector<int> A(M + 2, 0);
for (size_t i = 1; i <= M; i++)
{
cin >> A[i];
}
A.back() = N + 1;
sort(A.begin(), A.end());
map<int, size_t> count;
int minimum = N;
for (size_t i = 0; i <= M; i++)
{
if (A[i] + 1 == A[i + 1])
{
continue;
}
minimum = min(A[i + 1] - A[i] - 1, minimum);
count[A[i + 1] - A[i] - 1]++;
}
int ans = 0;
for (pair<int, size_t> c : count)
{
ans += (c.first + (minimum - 1)) / minimum * c.second;
}
cout << ans << endl;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vi vector<ll>
#define pb push_back
#define ff first
#define ss second
#define inf 2e18
#define ull unsigned long long
#define pi acos(-1.0)
#define mod 1000000007
#define lop0(n) for(ll i=0;i<n;i++)
#define lop(j,n) for(ll j=0;j<n;j++)
#define lop1(i,n) for(ll i=1;i<=n;i++)
#define all(v) v.begin(),v.end()
ll Set(ll N,ll pos){ return N=N | (1LL<<pos); }
ll reset(ll N,ll pos){ return N= N & ~(1LL<<pos); }
bool check(ll N,ll pos){ return (bool)(N & (1LL<<pos)); }
ll dx[] = { 1,0,-1,0 };
ll dy[] = { 0,1,0,-1 };
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
ll arr[200005];
main()
{
ll n,m;
cin>>n>>m;
lop1(i,m)
{
cin>>arr[i];
}
if(n==m)
{
cout<<0<<endl;
return 0;
}
sort(arr+1,arr+m+1);
ll mn=inf;
ll x=0;
lop1(i,m)
{
ll z=arr[i]-x-1;
if(z>0) mn=min(mn,z);
x=arr[i];
}
if(arr[m]!=n)
{
mn=min(mn,n-arr[m]);
}
if(mn==0 or mn==inf)
{
cout<<n;
return 0;
}
x=0;
ll ans=0;
lop1(i,m)
{
ll z=arr[i]-x-1;
if(z>0)
{
ll k=z+mn-1;
ans+=(k/mn);
}
x=arr[i];
}
if(arr[m]!=n)
{
ll k=n-arr[m]+mn-1;
ans+=(k/mn);
}
cout<<ans;
}
|
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define tr(x, it) for(auto (it) = (x).begin();it != (x).end();it++)
#define fo(i, k, n) for(int i = (k);i < (n);i++)
#define Fo(i, k, n) for(int i = (k);i >= (n);i--)
#define el "\n"
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
//#define pqb priority_queue<int>
//#define pqs priority_queue<int, vi, greater<int>>
//#define pc(x) putchar(x);
//#define setbits(x) __builtin_popcountll(x)
//#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define deb(x) cout<<#x<< " = "<< x<<"\n";
//#define ps(x, y) fixed<<setprecision(y)<<x
//#define mk(arr, n, type) type *arr = new type[n];
//#define w(x) int x; cin>>x; while(x--)
void solve(){
int n;
cin>>n;
double ar[n][2];
fo(i, 0, n){
cin>>ar[i][0]>>ar[i][1];
}
ll ans = 0;
fo(i, 0, n){
fo(j, i+1, n){
double k = (ar[j][1] - ar[i][1])/(ar[j][0] - ar[i][0]);
if(k <= 1 && k >= -1){
ans++;
}
}
}
cout<<ans;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin>>t;
while(t--){
solve();
cout<<el;
}
return 0;
} | #include <bits/stdc++.h>
//#include <atcoder/modint>
#define inc(i, n) for (int i = 0; i < (n); i++)
#define incs(i, s, n) for (int i = (s); i < (n); i++)
#define yes printf("Yes\n")
#define no printf("No\n")
using namespace std;
//using mint = atcoder::modint998244353;
using pii = pair<int, int>;
using ll = long long;
const int ARR_SIZE = 2e5+5;
const double epsilon = 1e-8;
char arr[ARR_SIZE];
int main(int argc, char const *argv[])
{
scanf("%s", arr);
int cnts[10];
memset(cnts, 0, sizeof cnts);
int arrLen = strlen(arr);
if (arrLen == 1) {
if(arr[0] == '8') {
yes;
return 0;
}
no;
return 0;
}
if (arrLen == 2) {
char narr[3] = {0};
narr[0]=arr[1], narr[1]=arr[0];
if (stoi(arr)%8==0 || stoi(narr)%8==0) {
yes;
return 0;
}
no;
return 0;
}
inc(i, arrLen) {
cnts[arr[i]-'0']++;
}
int ncnts[10];
for (int i = 104; i < 1000; i += 8) {
int num = i;
memcpy(ncnts, cnts, sizeof cnts);
bool flag = true;
while(num > 0) {
if(--ncnts[num%10] < 0) {
flag = false;
break;
}
num/=10;
}
if (flag) {
yes;
return 0;
}
}
for (int i = 104; i < 1000; i += 8) {
int num = i;
memcpy(ncnts, cnts, sizeof cnts);
bool flag = true;
while(num > 0) {
if(--ncnts[num%10] < 0) {
flag = false;
break;
}
num/=10;
}
if (flag) {
yes;
return 0;
}
}
no;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rrep(i,a,b) for(ll i=a;i>=b;--i)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323846
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll INF = 1e18+10;
const ll mod = 1e9+9;
const ll MAXN = 2e5+10;
ll poww(ll a, ll b) {
if(b<0) return 0LL;
ll ans = 1;
while (b) {
if (b & 1)ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
ll binpow(ll a, ll b, ll m)
{
if (b < 0) return 0LL;
if (a <= 0)return 0LL;
a %= m;
ll ans = 1LL;
while (b) {
if (b & 1)ans = ans * a % m;
a = a * a % m;
b >>= 1;
}
return ans;
}
ll modinv(ll n) {
return binpow(n, mod - 2, mod);
}
void solve() {
ll a,b,c; cin>>a>>b>>c;
ll x = a%10,og=a%10;
set<ll> st; vector<ll> v;
st.insert(x); v.pb(x); x*=x; x%=10;
while(st.find(x)==st.end()) {
st.insert(x); v.pb(x); x*=og; x%=10;
}
ll sz = st.size();
ll r = binpow(b,c,sz); r = (r-1+sz)%sz;
cout<<v[r];
}
int main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//ll no_of_test_cases; cin>>no_of_test_cases;
ll no_of_test_cases = 1;
//cout<<setprecision(10);
for(ll i=1; i<=no_of_test_cases; i++) {
solve();
}
return 0;
}
| #include <iostream>
#include <string>
using namespace std;
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <set>
#include <map>
#include <cassert>
#include <queue>
#define MOD1 1000000007
#define MOD2 998244353
#define LIMIT1 200010
#define LIMIT2 500010
#define LIMIT3 1000010
#define INF ((1<<30)-1)
#define LLINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef long double ld;
typedef const void cv;
typedef pair<ll,ll> P;
#define rep(i,n) for((i)=0;(i)<(n);(i)++)
#define per(i,n) for((i)=(n)-1;(i)>=0;(i)--)
#define ALL(a) (a).begin(),(a).end()
#define ALL2(a, n) (a),(a)+(n)
template<class T,class C> T max(T a,C b){ return std::max(a, (T)b); }
template<class T,class C> T min(T a,C b){ return std::min(a, (T)b); }
#define zt(a,b) (max((a),(b))-min((a),(b)))
#define setpre(n) fixed << setprecision(n)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return (a/gcd(a,b))*b;}
int dx[8]={1,0,-1,0,1,-1,-1,1},dy[8]={0,1,0,-1,1,1,-1,-1};
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1){
res *= a; if(mod>=1) res %= mod;
}
a *= a; if(mod>=1) a %= mod;
n >>= 1;
}
return res;
}
void initialize(){
}
ll n;
map<ll,ll> que[LIMIT1];
ll all_add=0;
vector<ll> edge[LIMIT1];
ll res[LIMIT1]={0};
vector<bool> visited(LIMIT1,false);
void dfs(ll i,ll parent,ll addd){
visited[i] = true;
res[i] += addd;
for(auto p : edge[i]){
if(!visited[p]){
if(que[p].count(i)){
addd += que[p][i];
}
if(que[i].count(p)){
addd -= que[i][p];
all_add += que[i][p];
}
dfs(p,i,addd);
if(que[p].count(i)){
addd -= que[p][i];
}
if(que[i].count(p)){
addd += que[i][p];
}
}
}
}
int main(void){
initialize();
ll m,i,j,k,result=0,q,t,e,x;
int a[LIMIT2]={0},b[LIMIT1]={};
string s;
queue<ll> path;
cin >> n;
rep(i,n-1){
cin >> a[i] >> b[i];
a[i]--;b[i]--;
edge[a[i]].push_back(b[i]);
edge[b[i]].push_back(a[i]);
}
cin >> q;
rep(i,q){
cin >> t >> e >> x;
e--;
if(t==1){
que[a[e]][b[e]] += x;
}else{
que[b[e]][a[e]] += x;
}
}
dfs(0,-1,0);
rep(i,n){
res[i] += all_add;
cout << res[i] << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
#define ull unsigned long long
void testcase(){
ll n,ans=0;
cin>>n;
for(int i=1;i<=n;i++){
bool st=true;
int j=i;
while(j){
if(j%10==7){
st=false;
break;
}
j/=10;
}
j=i;
while(j){
if(j%8==7){
st=false;
break;
}
j/=8;
}
if(st){
ans++;
}
}
cout<<ans;
}
int main() {
ll t=1;
//cin>>t;
while(t--){
testcase();
//cout<<'\n';
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int n;
cin >> n;
cout << (1<<n) - 1 << '\n';
vector<string> ans = {"AB"};
for (int i = 1; i < n; ++i) {
for (int j = 0; j < (1<<i) - 1; ++j) {
string tmp = ans[j];
for (int k = 0; k < (int)tmp.size(); ++k) {
tmp[k] = (tmp[k] == 'A' ? 'B' : 'A');
}
ans.push_back(ans[j] + tmp);
ans[j] += ans[j];
}
ans.push_back(string ((1<<i), 'A') + string ((1<<i), 'B'));
}
for (auto i : ans) cout << i << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};
const ll INF = (1ll<<62);
// const ld EPS = 1e-10;
// const ld PI = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;
int main(){
ll A, B;
cin >> A >> B;
ll res = 1;
for(ll g=1;g<=B;g++){
ll cnt = 0;
for(ll x=g;x<=B;x+=g){
if(A <= x and x <= B){
cnt++;
}
}
if(cnt >= 2){
res = g;
}
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll findMaxGCD(vector<ll> &arr) {
ll n=arr.size();
ll high = 0;
for (ll i = 0; i < n; i++)
high = max(high, arr[i]);
ll divisors[high + 1] = { 0 };
for (ll i = 0; i < n; i++) {
for (ll j = 1; j <= sqrt(arr[i]); j++) {
if (arr[i] % j == 0) {
divisors[j]++;
if (j != arr[i] / j)
divisors[arr[i] / j]++;
}
}
}
for (ll i = high; i >= 1; i--)
if (divisors[i] > 1)
return i;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll l;
cin >> l;
ll r;
cin >> r;
vector<ll> arr;
for(ll i=l; i<=r; i++) {
arr.push_back(i);
}
cout << findMaxGCD(arr) << '\n';
return 0;
}
|
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define hackcyborg shresth_walia
#define all(a) a.begin(), a.end()
#define ll long long
#define ld long double
#define pb push_back
#define mod 1000000007
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int,null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
ll bp(ll a,ll b,int Mod=mod)
{
ll res=1;
a%=Mod;
while(b>0)
{
if(b&1)
res=(a*res)%Mod;
a=(a*a)%Mod;
b/=2;
}
return res;
}
main()
{
IO
int n;
cin>>n;
int a[n];
int g=1e18;
for(int x=0;x<n;x++)
{cin>>a[x];
g=min(g,a[x]);}
set<int> as;
as.insert(1);
for(int x=0;x<n;x++)
{
int h=sqrt(a[x]);
for(int y=2;y<=h;y++)
{
if(a[x]%y==0)
{
as.insert(y);
if(a[x]/y!=y)
as.insert(a[x]/y);
}
}
}
int f=0;
for(auto i : as)
{ if(i>g)break;
int w=0;
for(int x=0;x<n;x++)
{
if(a[x]%i==0)
{w=__gcd(w,a[x]);
if(w<i)break;}
}
if(w==i)++f;
}
if(!as.count(g))++f;
cout<<f;
}
| #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repd(i,n) for(int i = n-1; i >= 0; i--)
#define ll long long
#define P pair<int, int>
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
// debug methods
// usage: debug(x,y);
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
const double PI = acos(-1);
unsigned GetDigit(unsigned num) { return log10(num)+1; }
void chmin(int& a, int b){ if (a > b) a = b; }
void chmax(int& a, int b){ if (a < b) a = b; }
void chmin(double& a, double b){ if (a > b) a = b; }
void chmax(double& a, double b){ if (a < b) a = b; }
int main()
{
int n;
cin >> n;
vector<string> s(n);
vector<int> t(n);
rep(i, n) cin >> s[i] >> t[i];
map<int, string> m;
rep(i, n) m.insert(make_pair(t[i], s[i]));
sort(t.begin(), t.end(), greater<int>());
cout << m.at(t[1]) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define deb(...)
#endif
#define endl "\n"
#define pb push_back
#define int long long
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define all(c) c.begin(),c.end()
#define read(v) for(auto &it:v) cin>>it;
const int inf = 1e18;
const int N = 2e5 + 5;
const int mod = 1000000007;
void testCase() {
int a,b,c;
cin>>a>>b>>c;
if(a*a+b*b < c*c){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--)testCase();
return 0;
} | //#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<vector>
#define swap(x,y) (y -= (x = ((y += x) - x)))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define reps(i,n) for(int i = 1, i <= (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define rreps(i,n) for(int i = (int)(n); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
using ll = long long;
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; }
int digit(ll n, int a) { int s = 0; for (int i = 0; n > 0; i++) { s++; n /= a; } return s; }
int imax(int a[], int n) { int s = a[0]; rep(i, n) { s = max(s, a[i]); } return s; }
int imin(int a[], int n) { int s = a[0]; rep(i, n) { s = min(s, a[i]); } return s; }
int cmax(string a, int n) { int s = 0; rep(i, n) { s = max(s, (int)a[i] - '0'); } return s; }
int cmin(string a, int n) { int s = 9; rep(i, n) { s = min(s, (int)a[i] - '0'); } return s; }
//quick sort(int)
int ascfunc(const void* n1, const void* n2) {
if (*(int*)n1 > *(int*)n2) {
return 1;
}
if (*(int*)n1 < *(int*)n2) {
return -1;
}
if (*(int*)n1 == *(int*)n2) {
return 0;
}
}
int main(void) {
ll n, s, d; cin >> n >> s >> d;
ll* x = new ll[n];
ll* y = new ll[n];
bool can = false;
rep(i, n) {
cin >> x[i];
cin >> y[i];
if (x[i]<s && y[i]>d) {
can = true;
}
}
if (can) {
cout << "Yes";
}
else {
cout << "No";
}
cout << endl;
delete[] x; delete[] y;
return 0;
} |
#include <bits/stdc++.h>
#include <vector>
using namespace std;
typedef long long int64;
struct Edge {
int to;
int cost;
};
using Graph = vector<vector<Edge>>;
int ans[200010]={0};
int N=0;
// 深さ優先探索
vector<bool> seen;
void dfs(const Graph &G, int v,int prev_label) {
//printf("%d is seen prev_label=%d\n",v,prev_label);
seen[v] = true; // v を訪問済にする
// v から行ける各頂点 next_v について
for (auto next_v : G[v]) {
if (seen[next_v.to]) continue; // next_v が探索済だったらスルー
int label=next_v.cost;
if(prev_label!=label)ans[next_v.to]=label;
else
{
if(label+1<=N)label++;
else label--;
ans[next_v.to]=label;
}
dfs(G, next_v.to,label); // 再帰的に探索
//break;
}
if(ans[v]==0)
{
//printf("%d is ume\n",v);
if(prev_label+1<=N)ans[v]=prev_label+1;
else ans[v]=prev_label-1;
}
}
int main() {
// 頂点数と辺数、s と t
int M;
cin >> N >> M;
// グラフ入力受取
Graph G(N);
for (int i = 0; i < M; ++i) {
int a, b,c;
cin >> a >> b >> c;
Edge e,f;
e.to = b-1;
e.cost = c;
f.to = a-1;
f.cost = c;
G[a-1].push_back(e);
G[b-1].push_back(f);
}
// 頂点 s をスタートとした探索
seen.assign(N, false); // 全頂点を「未訪問」に初期化
ans[0]=1;
dfs(G, 0,1);
for(int i=0;i<N;i++)
{
if(ans[i]==0)printf("%d\n",N);
else printf("%d\n",ans[i]);
}
} | // *********************************************************************************
// * MURTAZA MUSTAFA KHUMUSI *
// * NIT-DGP,CSE - 2019-2023 *
// *********************************************************************************
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define si set<int>
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
const int maxm = 100001;
// ll m = 1e9+7;
ll m = 998244353;
vector<vector<pi>> adj;
vi h, sz, in,vis,ans;
vector<ll> fact;
//modular arithmentic
ll add(ll a, ll b, ll mod = m)
{
return ((a % mod) + (b % mod)) % mod;
}
ll mul(ll a, ll b, ll mod = m)
{
return ((a % mod) * (b % mod)) % mod;
}
ll sub(ll a, ll b, ll mod = m)
{
return ((a % mod) - (b % mod) + mod) % mod;
}
ll modmul(ll a, ll b, ll mod = m)
{
ll ans = 0;
a = a % mod;
while (b)
{
if (b & 1)
ans = add(ans, a, mod);
a = mul(a, 2, mod);
b >>= 1;
}
return ans;
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll fe(ll base, ll exp, ll mod = m)
{
ll ans = 1;
while (exp)
{
if (exp & 1)
ans = mul(ans, base, mod);
base = mul(base, base, mod);
exp >>= 1;
}
return ans;
}
ll modin(ll a, ll mod = m)
{
return fe(a, mod - 2, mod);
}
ll nCr(ll a, ll b, ll mod = m)
{
if (a < b)
return 0;
return mul(fact[a], mul(modin(fact[b], mod), modin(fact[a - b], mod)));
}
//graph theory
void dfs(int cur)
{
vis[cur] = 1;
for (const auto &v : adj[cur])
{
if (vis[v.ff])continue;
if(v.ss==ans[cur]){
if(v.ss==1)
ans[v.ff] = 2;
else
ans[v.ff] = 1;
}else{
ans[v.ff] = v.ss;
}
dfs(v.ff);
}
}
void buildGraph(int n, int m)
{
adj = vector<vector<pi> >(n + 1);
ans = vi(n+1);
vis = vi(n+1);
loop(i, 0, m)
{
int a, b, c;
cin >> a >> b>>c;
adj[a].pb(MP(b,c));
adj[b].pb(MP(a,c));
}
ans[1] = 1;
dfs(1);
}
struct cmp
{
bool operator()(const pi &a, const pi &b)
{
return a.ff < b.ff;
}
};
void solve()
{
int n,m;cin>>n>>m;
buildGraph(n, m);
loop1(i,1,n){
cout<<ans[i]<<"\n";
}
// cout<<"\n";
}
int main()
{
io;
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int test;
// cin >> test;?
test=1;
while (test--)
{
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define mp make_pair
#define ll long long
#define pb push_back
#define INF 1e6
vector<int>ans,a,b;
void dfs(int u, vector<int>&visited, vector<vector<pair<int,int> >>&adj)
{
visited[u] =1;
for(auto p:adj[u])
{
int v = p.first;
int id = p.second;
if(a[id] == u && b[id]==v)ans[id] = 1;
if(a[id] == v && b[id]==u)ans[id] = -1;
if(!visited[v])dfs(v,visited,adj);
}
}
void solve(int test)
{
int n,m;
cin>>n>>m;
a.resize(m);
b.resize(m);
int i,j;
vector<vector<pair<int,int> >>adj(n);
for(i=0;i<m;i++){cin>>a[i]>>b[i];a[i]--;b[i]--;}
vector<int>c(n);
for(i=0;i<n;i++)cin>>c[i];
ans.resize(m,0);
for(i=0;i<m;i++)
{
if(c[a[i]] > c[b[i]]){ans[i] = 1;continue;}
if(c[a[i]] < c[b[i]]){ans[i] = -1;continue;}
adj[a[i]].pb(mp(b[i],i));
adj[b[i]].pb(mp(a[i],i));
}
vector<int>visited(n,0);
for(i=0;i<n;i++)
{
if(!visited[i])dfs(i,visited,adj);
}
for(i=0;i<m;i++)
{
if(ans[i] < 0)cout<<"<-"<<endl;
else
cout<<"->"<<endl;
}
}
int main()
{
int t = 1;
//cin>>t;
for(int x=1;x<=t;x++)
{
solve(x);
}
return 0;
}
| #include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, y) \
distance(v.begin(), lower_bound(v.begin(), v.end(), y))
#define Upper_bound(v, y) \
distance(v.begin(), upper_bound(v.begin(), v.end(), y))
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;
ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
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;
}
/* Macros reg. ends here */
struct UnionFind {
vector<ll> par, sizes;
ll cnt;
UnionFind(ll N) : par(N), sizes(N, 1), cnt(N) {
for (ll i = 0; i < N; i++) par[i] = i;
}
ll find(ll x) { return par[x] == x ? x : par[x] = find(par[x]); }
bool same(ll x, ll y) { return find(x) == find(y); }
ll size(ll x) { return sizes[find(x)]; }
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y) return;
if (sizes[x] < sizes[y]) swap(x, y);
par[y] = x;
sizes[x] += sizes[y];
cnt--;
}
ll count() const { return cnt; }
};
const ll INF = 1LL << 50;
using vvT = vector<vT>;
vll ans;
vvT to;
vector<bool> seen;
void dfs(ll u){
if(seen[u]) return;
seen[u] = true;
for(auto [id, v, dir] : to[u]){
if(ans[id] != -1) continue;
ans[id] = dir;
dfs(v);
}
}
int main() {
// ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
ll n, m;
cin >> n >> m;
ans.assign(m, -1);
to.resize(n);
seen.resize(n);
rep(i, m){
ll a, b;
cin >> a >> b;
a--, b--;
to[a].emplace_back(i, b, 0);
to[b].emplace_back(i, a, 1);
}
vll cs(n);
rep(i, n) cin >> cs[i];
rep(u, n){
for(auto [id, v, dir] : to[u]){
if(cs[u] > cs[v]) {
assert(ans[id] == -1);
ans[id] = dir;
}
}
}
rep(u, n) dfs(u);
rep(i, m) cout << ((ans[i] == 0) ? "->" : "<-") << endl;
return 0;
}
|
#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = int64_t; using ull = uint64_t; const ll INF = 9e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
// clang-format on
class iunionfind {
public:
// 連続するuintにのみ使える。vectorであるため定数時間がとれる。
// 基本的にこれだけあればよい。任意の構造体を扱うときは、IDと構造体のマッピングをとるvectorを別途用意する。
// libs-test.cppの iuf_map あたりを検索。
vector<ull> partition;
vector<ull> rank;
iunionfind(ull n) {
partition.resize(n);
rank.resize(n);
for (ull x = 0; x < n; x++) {
partition[x] = x;
rank[x] = 0;
}
}
ull find(ull x) {
if (partition[x] == x) {
return x;
} else {
partition[x] = find(partition[x]);
return partition[x];
}
}
void unite(ull x, ull y) {
x = find(x);
y = find(y);
if (x == y) {
return;
}
if (rank[x] < rank[y]) {
partition[x] = y;
} else {
partition[y] = x;
if (rank[x] == rank[y]) {
rank[x]++;
}
}
}
bool same(ull x, ull y) {
return find(x) == find(y);
}
};
const ll MODULO = 998244353L;
int main() {
ll N, K;
cin >> N >> K;
vector<vector<ll>> a(N, vector<ll>(N, 0));
// vector<ll> rtotal(N, 0);
// vector<ll> ctotal(N, 0);
for (ll r = 0; r < N; r++) {
for (ll c = 0; c < N; c++) {
cin >> a[r][c];
// rtotal[r] += a[r][c];
// ctotal[c] += a[r][c];
}
}
auto rswap = iunionfind(N);
for (ll r = 0; r < N; r++) {
for (ll rr = r + 1; rr < N; rr++) {
bool swapok = true;
for (ll c = 0; c < N; c++) {
if (a[r][c] + a[rr][c] > K) {
swapok = false;
}
}
if (swapok) {
rswap.unite(r, rr);
}
}
}
auto cswap = iunionfind(N);
for (ll c = 0; c < N; c++) {
for (ll cc = c + 1; cc < N; cc++) {
bool swapok = true;
for (ll r = 0; r < N; r++) {
if (a[r][c] + a[r][cc] > K) {
swapok = false;
}
}
if (swapok) {
cswap.unite(c, cc);
}
}
}
vector<ll> facttbl(N + 1, 1);
for (ll i = 2; i <= N; i++) {
facttbl[i] = (facttbl[i - 1] * i) % MODULO;
}
vector<ll> rcluster(N, 0);
for (ll r = 0; r < N; r++) {
rcluster[rswap.find(r)]++;
}
ll rnum = 1;
for (ll r = 0; r < N; r++) {
ll clsize = rcluster[r];
if (clsize >= 2) {
rnum = (rnum * facttbl[clsize]) % MODULO;
}
}
vector<ll> ccluster(N, 0);
for (ll c = 0; c < N; c++) {
ccluster[cswap.find(c)]++;
}
ll cnum = 1;
for (ll c = 0; c < N; c++) {
ll clsize = ccluster[c];
if (clsize >= 2) {
cnum = (cnum * facttbl[clsize]) % MODULO;
}
}
//print(cnum, rnum);
ll ans = (rnum * cnum) % MODULO;
//ll ans = fact(rswap + 1) * fact(cswap + 1) % MODULO;
print(ans);
}
| #define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#define MOD 998244353
int dp[3001][3001];
int main() {
int N,K;
cin >> N >> K;
int i,j;
for (i = 0; i <= N; i++) {
for (j = 0; j <= N; j++) {
if ((i == 0) || (j == 0)) dp[i][j] = 1;
else {
if (j > 1) dp[i][j] = dp[i][j-1];
if ((i >= j) && (2*j <= N)) dp[i][j] += dp[i-j][2*j];
if (dp[i][j] >= MOD) dp[i][j] -= MOD;
}
}
}
cout << dp[N-K][K] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,x,n) for(int i=x;i<(int)n;++i)
#define sp(p) cout<<setprecision(16)<<fixed<<p<<endl;
#define out(a) cout<<a<<endl;
#define vout(v) rep(i,0,v.size())cout<<v[i]<<(i==v.size()-1?"\n":" ");
#define vvout(vv) rep(j,0,vv.size()){vout(vv[j])}
#define mout(m) for(auto&e:m)cout<<e.first<<": "<<e.second<<endl;
#define _CHK_ 1
#define chk(a) if constexpr (_CHK_){cout<<#a<<": ";out(a)}
#define vchk(v) if constexpr (_CHK_){cout<<#v<<": ";vout(v)}
#define vvchk(vv) if constexpr (_CHK_){cout<<#vv<<": "<<endl;vvout(vv)}
#define mchk(m) if constexpr (_CHK_){cout<<#m<<": "<<endl;mout(m)}
int main(){
long long N;
scanf("%lld",&N);
long long M;
scanf("%lld",&M);
std::vector<long long> H(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld",&H[i]);
}
std::vector<long long> W(M);
for(int i = 0 ; i < M ; i++){
scanf("%lld",&W[i]);
}
sort(H.begin(), H.end());
sort(W.begin(), W.end());
// vchk(H)
// vchk(W)
vector<long long> diff1((N+1)/2);
vector<long long> diff2((N+1)/2);
for (long long i = 0; i+1 < (N+1)/2; i++) {
diff1[i]=H[2*i+1]-H[2*i];
diff2[i]=H[2*i+2]-H[2*i+1];
}
// vchk(diff1)
// vchk(diff2)
vector<long long> acc1((N+1)/2);
vector<long long> acc2((N+1)/2);
acc1[0]=diff1[0];
acc2[0]=diff2[0];
for (long long i = 1; i < (N+1)/2; i++) {
acc1[i]=acc1[i-1]+diff1[i];
acc2[i]=acc2[i-1]+diff2[i];
}
// vchk(acc1)
// vchk(acc2)
long long ret=LLONG_MAX;
for (long long i = 0; i < M; i++) {
long long sum=0;
auto itr = upper_bound(H.begin(), H.end(), W[i]);
long long pos = itr - H.begin();
// chk(W[i])
// chk(pos)
if(pos%2){
sum+=W[i]-H[pos-1];
if(pos/2-1>=0) {
sum+=acc1[pos/2-1];
}
sum+=acc2[(N+1)/2-1];
if(pos/2-1>=0) {
sum-=acc2[pos/2-1];
}
}else{
sum+=H[pos]-W[i];
if(pos/2-1>=0) {
sum+=acc1[pos/2-1];
}
if(pos/2<N/2){
sum+=acc2[N/2-1];
if(pos/2-1>=0) {
sum-=acc2[pos/2-1];
}
}
}
ret=min(ret, sum);
}
out(ret)
return 0;
}
| #include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 mrand(42);
#define ii for(int i=1;i<=n;++i)
#define ji for(int j=1;j<=n;++j)
#define jj for(int j=1;j<=m;++j)
#define ij for(int i=1;i<=m;++i)
#define sz(x) ((ll)x.size())
#define all(x) x.begin(),x.end()
#define al(x) x+1,x+1+n
#define asd cout<<"ok"<<endl;
#define asdd cout<<"okok"<<endl;
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define pr(v) for(auto i:v) cout<<i<<" ";cout<<endl;
#define prt(a, l, r) for(auto i=l;i<=r;++i) cout<<a[i]<<" ";cout<<endl;
#define pc(x) __builtin_popcount(x)
#define pb push_back
#define PS string qqwwee;cin>>qqwwee;
const int maxn = 200005;
ll a[maxn], b[maxn], pre[maxn], suf[maxn];
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, m;
cin >> n >> m;
ii cin >> a[i];
sort(a+1, a+1+n);
for(int i=2;i<=n;++i) pre[i] = pre[i-2] + abs(a[i] - a[i-1]);
for(int i=n-1;i>=1;--i) suf[i] = suf[i+2] + abs(a[i] - a[i+1]);
for(int i=1;i<=m;++i) cin >> b[i];
sort(b+1, b+1+m);
auto go = [&](int x) -> ll {
int pos = lower_bound(b+1,b+1+m,x)-b;
ll ret = 1e18;
for(int i=max(1,pos-1);i<=min(m,pos+1);++i) ret = min(ret, abs(b[i] - x));
return ret;
};
ll ans = 1e18;
for(int i=1;i<=n;++i) {
if(i % 2 == 0) ans = min(ans, pre[i-2] + suf[i+2] + abs(a[i-1] - a[i+1]) + go(a[i]));
else ans = min(ans, pre[i-1] + suf[i+1] + go(a[i]));
}
cout << ans << endl;
} |
#include<iostream>
using namespace std;
int main()
{
int m,h;
cin>>m>>h;
if(h%m==0)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
} | #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int M, H;
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> M >> H;
cout << ((H % M == 0) ? "Yes" : "No") << endl;
}
|
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <cassert>
#include <numeric>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
typedef long long int int64;
const int N = 105;
char s[N];
char dp[N][N];
int getWin(char a, char b)
{
if (a == b)
return a;
if (a == 'R')
{
if (b == 'S')
return a;
return b;
}
if (a == 'P')
{
if (b == 'R')
return a;
return b;
}
if (a == 'S')
{
if (b == 'P')
return a;
return b;
}
}
int main(int, char **)
{
#ifdef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, k;
scanf("%d%d", &n, &k);
scanf("%s", s);
for (int sh = 0; sh < n; sh++)
dp[0][sh] = s[sh];
int deg = 1;
for (int i = 0; i < k; i++)
{
for (int sh = 0; sh < n; sh++)
{
dp[i + 1][sh] = getWin(dp[i][sh], dp[i][(sh + deg) % n]);
}
deg = (deg * 2) % n;
}
printf("%c\n", dp[k][0]);
return 0;
}
| //#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
//#include<ctime>
//#include<algorithm>//next_permutation
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
ll gcd(ll a,ll b){
if(b == 0) return a;
return gcd(b , a % b);
}
const ll MOD = 1000000007;
const ll MAX = 2000001;
ll mod(ll a){
return a % MOD;
}
ll lcm(ll a,ll b){
return (a*b)/gcd(a,b);
}
//ここに適切な判断条件を書く
bool isOK(ll index,ll n) {
if(((long double)(index+1) <= 2.0*(long double)(n+1)/(long double)(index))
and (index*(index+1))/2 <= n+1) return true;
return false;
}
//臨機応変に引数を書き換える
int binary_search(ll n) {
ll ok = 0;
ll ng = min(n,ll(1000000002));
while (abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if (isOK(mid,n)) ok = mid;
else ng = mid;
}
return ok;
}
int main(){
ll n,k; string s; cin >> n >> k >> s;
if(n % 2 == 1) s += s;
ll now = n;
//cout << s << endl;
ll cnt = 0;
queue<char> q;
for(char c : s){
q.push(c);
}
while(cnt < k){
if(q.size() % 2 == 1){
queue<char> pq;
rep(i,q.size()){
pq.push(q.front());
q.push(q.front());
q.pop();
}
while(!pq.empty()){
q.push(pq.front());
pq.pop();
}
}
//cout << q.size() << endl;
queue<char> pq;
/*rep(i,q.size()){
pq.push(q.front());
q.pop();
}
while(!pq.empty()){
cout << pq.front();
q.push(pq.front());
pq.pop();
}
cout << endl;*/
now = q.size();
rep(i,now/2){
char first = q.front(); q.pop(); char second = q.front(); q.pop();
if((first == 'P' and second == 'S') or (first == 'S' and second == 'P')){
q.push('S');
}
else if((first == 'R' and second == 'S') or (first == 'S' and second == 'R')){
q.push('R');
}
else if(first == second) q.push(first);
else q.push('P');
}
cnt++;
}
cout << q.front() << endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define f first
#define s second
#define M 1000000007
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
const int N = 1e5 , inf = 1e9;
int n , m , k , c[17] , dist[17][N] , id[N] , dp[17][1<<17] , vis[N];
vector<int> g[N];
void bfs(int srce){
memset(vis , 0 , sizeof vis);
vis[srce] = 1;
queue<int> q;
q.push(srce);
while(!q.empty()){
int x = q.front();
q.pop();
for(auto u : g[x]){
if(vis[u]) continue;
vis[u] = 1;
dist[id[srce]][u] = dist[id[srce]][x] + 1;
q.push(u);
}
}
}
int main() {
scanf("%d%d",&n,&m);
for (int i = 0,u,v; i < m; ++i){
scanf("%d%d",&u,&v);
--u,--v;
g[u].pb(v);
g[v].pb(u);
}
scanf("%d",&k);
for (int i = 0; i < k; ++i){
scanf("%d", &c[i]);
--c[i];
id[c[i]] = i;
}
for (int i = 0; i < k; ++i){
bfs(c[i]);
}
for (int j = 0; j < k; ++j){
if(vis[c[j]] == 0) return puts("-1") , 0;
}
for (int i = 0; i < k; ++i){
for (int j = 1; j < (1<<k); ++j){
dp[i][j] = inf;
}
}
vector<vector<int>> msk(k+1);
for (int i = 0; i < (1 << k); ++i){
msk[__builtin_popcount(i)].pb(i);
}
for (int i = 0; i < k; ++i) dp[i][1<<i] = 1;
for (int i = 2; i <= k; ++i){
for(auto mask : msk[i-1]){
for (int j = 0; j < k; ++j){
for (int o = 0; o < k; ++o){
if(mask >> j & 1) continue;
if(mask >> o & 1){
dp[j][mask|(1<<j)] = min(dp[j][mask|(1<<j)] , dp[o][mask] + dist[o][c[j]]);
}
}
}
}
}
int ans = inf;
for (int i = 0; i < k; ++i){
ans = min(ans , dp[i][(1 << k) - 1]);
}
printf("%d\n", ans);
} | #include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <cmath>
#include <set>
#include <stack>
#include <queue>
#include <numeric>
#include <deque>
#include <cstring>
#include <iterator>
#include <map>
#include <cstdlib>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <assert.h>
#include <array>
using namespace std;
template<typename M, typename N>
istream& operator>>(istream&is, pair<M,N> & p){is >> p.first >> p.second; return is;}
template<typename M>
istream& operator>>(istream&is, vector<M> & v){for(auto &it:v) is >> it; return is; }
template<typename M>
istream& operator>>(istream&is, vector<vector<M>> & v){for(auto &it:v) is >> it; return is; }
template<typename M, typename N>
ostream& operator<<(ostream&os, const pair<M,N> &p){os << p.first << ' ' << p.second << '\n';return os;}
template<typename M>
ostream& operator<<(ostream&os, const vector<M> &v){for(auto it: v)os << it << ' ';cout << '\n';return os;}
template<typename M>
ostream& operator<<(ostream&os, const vector<vector<M>> &v){for(auto it: v)os << it;return os;}
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define mn(x) *min_element((x).begin(),(x).end())
#define mx(x) *max_element((x).begin(),(x).end())
#define acc(x) accumulate((x).begin(),(x).end(),0ll)
#define unique(x) sort(all(x)); x.erase(unique(all(x)),x.end());
#define eb emplace_back
#define el '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef complex<long double> cd;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<string> vs;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ld pi=acos(-1);
const ll inf=(ll)1e9+10;
const int mod=1000000007;
//const int mod=998244353;
int n, m, k;
const int N=1e2+10;
vvi g(N);
set<pii> ee;
vi mark(N);
void dfs(int node, int par){
mark[node]=1;
for(int child: g[node]){
if(child!=par){
if(!mark[child]){
dfs(child, node);
}
if(ee.find({child,node})==ee.end())
ee.insert({node,child});
}
}
};
int main()
{ ios_base::sync_with_stdio(false);cin.tie(NULL);
int n, m;
cin >> n >> m;
vpi e(m);
for(int i=0; i<m; i++){
cin >> e[i].fi >> e[i].se;
e[i].fi--; e[i].se--;
}
vi c(n);
cin >> c;
vi todo(n);
for(int i=0; i<m; i++){
if(c[e[i].fi]==c[e[i].se]){
todo[e[i].fi]=1;
todo[e[i].se]=1;
g[e[i].fi].pb(e[i].se);
g[e[i].se].pb(e[i].fi);
}
}
for(int i=0; i<n; i++){
if(todo[i] && !mark[i]){
dfs(i,i);
}
}
for(int i=0; i<m; i++){
if(c[e[i].fi]==c[e[i].se]){
if(ee.find({e[i].fi,e[i].se})!=ee.end()){
// cout << e[i].fi+1 << ' ' << e[i].se+1 << el;
cout << "->\n";
}
else{
assert(ee.find({e[i].se,e[i].fi})!=ee.end());
cout << "<-\n";
}
}
else if(c[e[i].fi]>c[e[i].se]){
cout << "->\n";
}
else{
cout << "<-\n";
}
}
return 0;
}
|
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<cstdint>
#include<algorithm>
#include<cstddef>
#include<vector>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = size_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
template<typename T>
using vec = vector<T>;
#define rep(i, n) for (i64 i = 0; i < (i64)(n); ++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using P = pair<i64,i64>;
void solve(){
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
u64 n,m;
cin >> n >> m;
vec<u64> a(n),b(m);
rep(i,n)cin >> a[i];
rep(i,m)cin >> b[i];
vec<vec<u64>> dp(n + 1,vec<u64> (m + 1,1e18));
dp[0][0] = 0;
rep(i,n + 1){
rep(j,m + 1){
if(i && j){
if(a[i - 1] == b[j - 1])dp[i][j] = min(dp[i][j],dp[i - 1][j - 1]);
else dp[i][j] = min(dp[i][j],dp[i - 1][j - 1] + 1);
}
if(i)dp[i][j] = min(dp[i][j],dp[i - 1][j] + 1);
if(j)dp[i][j] = min(dp[i][j],dp[i][j - 1] + 1);
}
}
cout << dp.back().back() << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n);
vector<int> b(m);
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < m; i++) cin >> b[i];
// either remove this character from b, or remove it from a
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 1 << 30));
dp[0][0] = 0;
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= m; j++) {
int here = dp[i][j];
if(here == 1 << 30) continue;
// so given that we are considering position i, j in both strings
// we can either either remove from i => + 1
// remove from j => + 1
// we dont remove from either, then + whether or not a_i != b_j
if(i + 1 <= n)
dp[i + 1][j] = min(1 + here, dp[i + 1][j]);
if(j + 1 <= m)
dp[i][j + 1] = min(1 + here, dp[i][j + 1]);
if(i + 1 <= n && j + 1 <= m)
dp[i + 1][j + 1] = min((a[i] == b[j] ? 0 : 1) + here, dp[i + 1][j + 1]);
}
}
cout << dp[n][m] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
int main(){
ll a, b, w; cin >> a >> b >> w; w*=1000;
ll ma = -1; ll mi = INF;
FOR(i,1,1000100) {
if (a*i<=w && b*i>=w) {
ma = max(ma,i); mi = min(mi,i);
}
}
if (ma==-1 || mi==INF) cout << "UNSATISFIABLE" << endl;
else cout << mi << " " << ma << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int a, b, w;
cin >> a >> b >> w;
w *= 1000;
int ca = w / a, cb = (w + b - 1) / b;
if (cb <= ca) {
cout << cb << " " << ca << endl;
} else {
cout << "UNSATISFIABLE" << endl;
}
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
int h,w,x,y;
cin >> h >> w >> x >> y;
vector<string> s(h);
for(int i = 0;i < h;i++){
cin >> s[i];
}
x--;
y--;
int ans = 1;
for(int i =y+1;i < w;i++){
if(s[x][i] == '.'){
ans ++;
}else{
break;
}
}
for(int i = y-1;0 <= i;i--){
if(s[x][i] == '.'){
ans ++;
}else{
break;
}
}
for(int i = x+1;i < h;i++){
if(s[i][y] == '.'){
ans ++;
}else{
break;
}
}
for(int i = x-1;0 <= i;i--){
if(s[i][y] == '.'){
ans ++;
}else{
break;
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>
using namespace std;
//using namespace atcoder;
#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
REP(i, SZ(v)) {
if (i) os << ", ";
os << v[i];
}
return os << "]";
}
template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "(" << p.first << " " << p.second << ")";
}
template<class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;
// edit
template<typename T>
class graph {
struct edge {
int src, to;
T cost;
};
public:
int n;
vector<vector<edge>> edges;
graph(int n_) : n(n_), edges(n_) {
}
void add_edge(int src, int to, T cost) {
edges[src].push_back({src, to, cost});
}
};
template<typename T>
pair<vector<T>, vector<int>> dijkstra(const graph<T> &g, int s, T inf) {
vector<T> d(g.edges.size(), inf);
vector<int> p(g.edges.size());
using Pi = pair<T, int>;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
que.emplace(0, s);
d[s] = 0;
p[s] = -1;
while (!que.empty()) {
T cost;
int v;
tie(cost, v) = que.top();
que.pop();
if (d[v] < cost) continue;
for (const auto &e : g.edges[v]) {
T ncost = cost + e.cost;
int nv = e.to;
if (ncost < d[nv]) {
d[nv] = ncost;
p[nv] = v;
que.emplace(ncost, nv);
}
}
}
return make_pair(d, p);
}
void solve() {
int N, M;
cin >> N >> M;
graph<int> G(N + 1);
REP(i, M) {
int a, b;
cin >> a >> b;
G.add_edge(a, b, 0);
}
int ans = 0;
for (int s = 1; s <= N; ++s) {
auto tapu = dijkstra(G, s, INF).first;
int cnt = 0;
for (int i = 1; i <= N; ++i) {
if (tapu[i] == 0) {
cnt++;
}
}
ans += cnt;
}
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
const ll INF = 1e18;
struct edge {
ll to;
ll cost;
ll d;
};
using P = pair<ll, ll>;
vector<vector<edge>> G;
vector<ll> d;
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>> que;
fill(d.begin(), d.end(), INF);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
rep(i, G[v].size()) {
edge e = G[v][i];
ll sq = sqrt(e.d);
if (sq * sq != e.d) sq++;
sq--;
ll cost = 0;
if (d[v] < sq)
cost = sq - d[v] + e.cost + e.d / (sq + 1);
else
cost = e.cost + e.d / (d[v] + 1);
if (d[e.to] > d[v] + cost) {
d[e.to] = d[v] + cost;
que.push(P(d[e.to], e.to));
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
G.resize(N);
d.resize(N);
rep(i, M) {
ll a, b, c, d;
cin >> a >> b >> c >> d;
a--, b--;
G[a].push_back({b, c, d});
G[b].push_back({a, c, d});
}
dijkstra(0);
ll ans = d[N - 1];
if (ans == INF)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
int n,m;
vector<int> a,b;
vector<ll> c,d;
vector<vector<int>> links;
vector<ll> earliest;
priority_queue<pair<ll,int>, vector<pair<ll,int>>,greater<pair<ll,int>>> q;
void initialize(){
cin >> n >> m;
links.resize(n,vector<int>(0));
earliest.resize(n,-1);
a.resize(m);
b.resize(m);
c.resize(m);
d.resize(m);
for(int i=0; i<m; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
links[a[i]-1].push_back(i);
links[b[i]-1].push_back(i);
}
}
/*
template <typename T> void print_vv(vector<vector<T>> &vv){
for(int i=0;i<vv.size();i++){
cout << i << " * ";
for(auto& j:vv[i]) cout << j << ' ';
cout << endl;
}
}
template <typename T> void print_v(vector<T> &a){
cout << "{ ";
for(auto& i:a) cout << i << ' ';
cout << '}' << endl;
}*/
ll fastest(ll di,ll start){
//cout << "fastest " << di << ' ' << start << " : ";
ll t = max(start,(ll)round(sqrt(di))-2);
ll i=di/(t+1)+t;
while(i>di/(t+2)+t+1){
t ++;
i = di/(t+1)+t;
}
//cout << i << endl;
return i;
}
int main(){
initialize();
/*
print_vv(links);
print_v(c);
print_v(d);
print_v(earliest);
*/
q.push( {0, 0} );
while(q.size()>0){
auto node = q.top();
q.pop();
if(earliest[node.second] != -1) continue;
earliest[node.second] = node.first;
//cout << node.second << ' ' << node.first << endl;
for(auto& i : links[node.second]){
int tmp = (a[i]-1 == node.second ? b[i]-1 : a[i]-1);
q.push( {c[i]+fastest(d[i], node.first), tmp} );
}
//print_v(earliest);
}
cout << earliest[n-1] << endl;
}
|
#include<iostream>
using namespace std;
int main(){
int A,B,C;
cin >> A >> B >> C;
int AB,AC,BC;
AB = A + B;
AC = A + C;
BC = B + C;
if(AB >= AC && AB >= BC){
cout << AB;
}
else if(AC >= AB && AC > BC){
cout << AC;
}
else{
cout << BC;
}
return 0;
} | ///******* In the name of Allah *******///
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false), cin.tie(NULL);
#define nl '\n'
void Ok()
{
long int a[13],b,c, d, e, f, g, h, t, i, j, k, l, m, n, Mn, Mx;
long int ans_1, ans_2, ans_3, cnt_1, cnt_2, cnt_3, flag_1, flag_2, flag_3, temp, sum;
double aa, bb, cc, dd, ee, ff, gg;
char aaa, bbb, ccc, ddd;
string spc, in, keyword;
fastio;
cin>>a[1]>>a[2]>>a[3];
sort(a+1,a+3+1);
sum = a[2] + a[3];
cout<<sum<<nl;
return;
}
int main()
{
Ok();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}
constexpr int mod = 1e9 + 7;
#include <cstdint>
template <std::uint_fast64_t Modulus> class modint {
using u64 = std::uint_fast64_t;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
constexpr u64 &value() noexcept { return a; }
constexpr const u64 &value() const noexcept { return a; }
constexpr modint operator+(const modint rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint rhs) noexcept {
a += rhs.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr modint &operator-=(const modint rhs) noexcept {
if (a < rhs.a) {
a += Modulus;
}
a -= rhs.a;
return *this;
}
constexpr modint &operator*=(const modint rhs) noexcept {
a = a * rhs.a % Modulus;
return *this;
}
constexpr modint &operator/=(modint rhs) noexcept {
u64 exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= rhs;
}
rhs *= rhs;
exp /= 2;
}
return *this;
}
};
ll solve(modint<mod> N, modint<mod> A, modint<mod> B) {
if (A.a + B.a > N.a) return 0;
// ensure A <= B
if (A.a > B.a) swap(A, B);
// we think the problem of 1d version.
modint<mod> p1 = (B + A - 1) * ((N - B + 1 - (A - 1) * 2));
modint<mod> p2 = (((A - 1) * B + (A - 1) * A) - (A - 1) - A * (A - 1) / 2) * 2;
//cout << p1 << ", " << p2 << endl;
modint<mod> A2 = (N - A + 1) * (N - A + 1);
modint<mod> B2 = (N - B + 1) * (N - B + 1);
modint<mod> ans_p = p1 + p2;
return (A2 * B2 - ans_p * ans_p).a;
}
void solve() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
ll N, A, B;
cin >> N >> A >> B;
cout << solve(N, A, B) << endl;
}
}
int main() {
#ifdef LOCAL_ENV
cin.exceptions(ios::failbit);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
solve();
} | #pragma warning(disable:4996)
#include<iostream>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_map>
#include<numeric>
#include<fstream>
#include<iomanip>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<stack>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include <assert.h>
#define pb push_back
#define INF 0x3F3F3F3F //int 下较大值
#define inf 0x3F3F3F3F3F3F3F3F // ll 下较大值
#define moD 1000000003
#define pii pair<int,int>
#define eps 1e-10
#define equals(a,b) (fabs(a-b)<eps)
#define bug puts("bug")
#define re register
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1e9 + 7;
const ll Mod = 998244352;
const int maxn = 2e5 + 5;
const double Inf = 10000.0;
const double PI = acos(-1.0);
using namespace std;
int bitcount(int x) {
return x == 0 ? 0 : bitcount(x / 2) + (x & 1);
}
int _popcount(int x) {
int cnt = 0;
for (int i = 0; i < 32; i++)
if (x & (1 << i)) cnt++;
return cnt;
}
ll mul(ll a, ll b, ll m) {
ll res = 0;
while (b) {
if (b & 1) res = (res + a) % m;
a = (a + a) % m;
b >>= 1;
}
return res % m;
}
ll quickPower(ll a, ll b, ll m) {
ll base = a;
ll ans = 1ll;
while (b) {
if (b & 1) ans = mul(ans, base, m);
base = mul(base, base, m);
b >>= 1;
}
return ans;
}
ll ksm(ll a, ll b, ll m) {
ll base = a % m;
ll ans = 1ll;
while (b) {
if (b & 1) ans *= base, ans %= m;
base *= base, base %= m;
b >>= 1;
}
return ans;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
ll Lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
int readint() {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
ll readll() {
ll x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
ull readull() {
ull x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
void Put(ll x) {
if (x < 0) putchar('-'), x *= -1;
if (x > 9) Put(x / 10);
putchar(x % 10 + '0');
}
int main() {
int T = readint();
while (T--) {
ll n = readll();
ll a = readll();
ll b = readll();
if (a + b > n) {
puts("0");
continue;
}
if (a > b) swap(a, b);
ll c1 = n - a + 1;
ll c2 = n - b + 1;
ll c3 = n - a - b + 1;
ll res = c1 * c1 % MOD * c2 % MOD * c2 % MOD;
ll f = c1 * c2 % MOD - c3 * (c3 + 1) % MOD + MOD;
f %= MOD;
res = res - f * f % MOD + MOD;
res = res % MOD + MOD;
res %= MOD;
Put(res);
puts("");
}
} |
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vii = vector<int>;
using vvii = vector<vector<int>>;
using vvvii = vector<vector<vector<int>>>;
using vdd = vector<double>;
using vvdd = vector<vector<double>>;
using vvvdd = vector<vector<vector<double>>>;
#define pb push_back
#define mp make_pair
#define sc second
#define fr first
#define endl '\n'
#define stpr std::fixed << setprecision
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define drep(i, a, b, d) for (ll i = (a); i <= (b); i += d)
#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)
#define drrep(i, a, b, d) for (ll i = (a); i >= (b); i -= d)
#define rRep(i, a, b) for (ll i = a; i >= b; i--)
#define crep(i) for (char i = 'a'; i <= 'z'; ++i)
#define Crep(i) for (char i = 'A'; i <= 'Z'; ++i)
#define ALL(x) (x).begin(), (x).end()
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
constexpr ll MOD = 1000000007;
constexpr ll INF = 1000000011;
constexpr ll MOD2 = 998244353;
constexpr ll LINF = 1001002003004005006ll;
constexpr ld EPS = 10e-10;
template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first;
is >> p.second;
return is;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto &&x : v) is >> x;
return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << p.first << ' ' << p.second;
return os;
}
template <class T>
ostream &operator<<(ostream &os, vector<T> &v) {
for (auto i = begin(v); i != end(v); ++i) {
if (i != begin(v)) os << ' ';
os << *i;
}
return os;
}
int main(){
ll n, k, ans, a, b;
cin >> n >> k;
map<ll, ll> T;
rep(i,n){
cin >> a >> b;
if(T.find(a)==T.end()){
T.insert(make_pair(a,b));
}
else{
T[a] += b;
}
}
ans = k;
while(1){
auto itr = T.begin();
if(ans >= itr->first){
ans += itr->second;
T.erase(itr);
}
else {break;}
if(T.size()==0){break;}
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define task "SKILEVEL"
#define pii pair<ll, pll>
using namespace std;
using ll = long long;
const int N = 5e5+5;
const ll mod = 1e9+7;
const ll base = 350;
const ll cs = 331;
ll m, n, t, k, tong, q, ans, a[N], b[N], fe[N], c[N*20];
vector<ll> adj[N];
vector<pll> kq;
string s;
void add(ll id)
{
for(; id <= k; id += id & -id)++fe[id];
}
ll get(ll id)
{
ll total = 0;
for(; id; id -= id & -id)total += fe[id];
return total;
}
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
pll p[N];
void sol()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++)cin >> p[i].fi >> p[i].se;
sort(p+1, p+1+n);
for(int i = 1; i <= n; i ++)
{
if(p[i].fi - p[i-1].fi > k)
{
cout << p[i-1].fi+k;
return;
}
k -= p[i].fi - p[i-1].fi;
k += p[i].se;
}
cout << p[n].fi+k;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
4
ANTON
NAAN
AAAAAA
OAANTTON
*/
|
#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
const ll INF = 1000000000000000000;
ll dfs(ll z, int j, ll y, map<ll, ll> &mp){
if(j == 0){
return mp[z] = abs(z - y);
}
if(mp.count(z)) return mp[z];
ll res = INF;
if(((z + 1) << j) <= y){
res = min(res, dfs((z + 1) * 2, j - 1, y, mp) + 2);
}
else if((z << j) <= y){
res = min(res, dfs((z + 1) * 2, j - 1, y, mp) + 2);
res = min(res, dfs(z * 2, j - 1, y, mp) + 1);
}
if(((z - 1) << j) >= y){
res = min(res, dfs((z - 1) * 2, j - 1, y, mp) + 2);
}
else if((z << j) >= y){
res = min(res, dfs((z - 1) * 2, j - 1, y, mp) + 2);
res = min(res, dfs(z * 2, j - 1, y, mp) + 1);
}
return mp[z] = res;
}
int main()
{
ll x, y;
cin >> x >> y;
ll ans = abs(y - x);
map<ll, ll> mp;
for(int i = 0; i <= 60; i++){
ll l = y, r = y;
for(int j = 0; j < i; j++){
l = l / 2;
r = (r + 1) / 2;
}
if(l == 0) break;
ll z = x;
ll s = 0;
if(z < l){
s += l - z;
z = l;
}
if(z > r){
s += z - r;
z = r;
}
ans = min(ans, dfs(z, i, y, mp) + s);
}
cout << ans << endl;
}
| #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <functional>
using ll = long long;
using namespace std;
template<typename A, typename B>
bool chmin(A &a, const B b) {
if (a <= b) return false;
a = b;
return true;
}
template<typename A, typename B>
bool chmax(A &a, const B b) {
if (a >= b) return false;
a = b;
return true;
}
#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);
template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
out << '{';
for (const T &item : v) out << item << ", ";
out << "\b\b}";
return out;
}
void _tostr_rec(ostringstream &oss) {
oss << "\b\b \b";
}
template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
oss << head << ", ";
_tostr_rec(oss, forward<Tail>(tail)...);
}
template<typename... T>
string _tostr(T &&...args) {
ostringstream oss;
int size = sizeof...(args);
if (size > 1) oss << "{";
_tostr_rec(oss, forward<T>(args)...);
if (size > 1) oss << "}";
return oss.str();
}
#endif
constexpr int mod = 1'000'000'007; //1e9+7(prime number)
constexpr int INF = 1'000'000'000; //1e9
constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18
constexpr int SIZE = 200010;
int main() {
ll X, Y;
ll ans = LLINF;
cin >> X >> Y;
set<ll> visited;
queue<pair<ll, int>> que;
que.push({Y, 0});
while (que.size()) {
auto p = que.front();
que.pop();
if (visited.count(p.first)) continue;
visited.insert(p.first);
chmin(ans, abs(p.first - X) + p.second);
if (p.first % 2) {
que.push({p.first + 1, p.second + 1});
que.push({p.first - 1, p.second + 1});
} else {
que.push({p.first / 2, p.second + 1});
}
}
cout << ans << endl;
return 0;
} |
// JG GAP GG
#include <iostream>
#include <cstring>
#include<vector>
#include <algorithm>
#include<cstdlib>
#include<set>
#include<math.h>
#include<map>
#include<unordered_map>
#include<iomanip>
#include<queue>
using namespace std;
using ll = long long;
using ull = unsigned long long;
ll amari = 1000000007;
#define rep(n,x) for(ll i=0;i<n;i++) cin>>x[i];
#define forr(a,b) for(ll i=a;i<b;i++);
#define vecsort(myVec,x,y) sort(myVec.begin(),myVec.end(),[](const vector<y> &alpha,const vector<y> &beta){return alpha[x] < beta[x];});
ll modpow(ll a, ll n, ll mod)
{
ll res = 1;
while (0 < n)
{
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll gcd(ll x, ll y)
{
if (x % y == 0) return y;
else return gcd(y, x % y);
}
ll lcm(ll x, ll y)
{
return (y / gcd(x, y)) * x;
}
// 1or素数だと1を返す
ll primebool(ll x)
{
for (ll i = 2; i * i <= x; i++)
{
if (x % i == 0) return 0;
}
return 1;
}
//下のnck関数のための準備
const ll MOD = amari;
vector<long long> fact_inv, inv, Com;
void init_nCk(ll n, ll SIZE) {
fact_inv.resize(SIZE + 5);
inv.resize(SIZE + 5);
fact_inv[0] = fact_inv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < SIZE + 1; i++) {
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD;
}
Com.resize(SIZE + 5);
Com[0] = 1;
for (ll i = 1; i < SIZE + 1; i++) {
Com[i] = Com[i - 1] * ((n - i + 1) * inv[i] % MOD) % MOD;
}
}
//nCkを計算し、10^9+7で割ったあまりを返す。
ll nck(ll n, ll k)
{
init_nCk(n, k);
return Com[k];
}
//ここから
int main()
{
char s;
cin >> s;
char ans;
char t;
cin >> t;
if (s == 'Y')
{
if (t == 'a')
{
ans = 'A';
}
else
{
if (t == 'b')
{
ans = 'B';
}
else
{
ans = 'C';
}
}
}
else
{
ans = t;
}
cout << ans << "\n";
return 0;
} | #include <iostream>
#include <cstring>
using namespace std;
int N;
long long dp[105][105][105];
int a[101];
long long X;
int len;
// f(i, j, k) -> minimo tiempo para alcanzar X tomando a i pociones y llevando j pociones actualmente con suma k
long long f(int i, int j, int k)
{
if (i == 0)
return k == X%len? 0LL : -1e18;
if (j == N)
return -1e18;
if (dp[i][j][k] != -1)
return dp[i][j][k];
long long ans = 0;
ans = max(f(i, j + 1, k), f(i - 1, j + 1, (k + a[j])%len) + a[j]);
return dp[i][j][k] = ans;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> X;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
long long ans = 1e18;
for (int i = 1; i <= N; i++) {
memset(dp, -1, sizeof dp);
len = i;
long long res = f(i, 0, 0);
if (res > 0)
ans = min(ans, (X - res)/i);
}
cout << ans << '\n';
} |
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if (a == b) {
cout << c << endl;
return 0;
} else if (a == c) {
cout << b << endl;
return 0;
} else if (b == c) {
cout << a << endl;
return 0;
} else {
cout << 0 << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int a,b,c,d;
cin >> a >> b >> c >> d;
cout << b - c << endl;
} |
#include <bits/stdc++.h>
#define int long long
#define endl "\n"
#define mod 1000000007
#define inf 0xFFFFFFFFFFFFFFFL
using namespace std;
void solve()
{
int n, q;
cin >> n >> q;
vector<int> v(n);
int mn = INT_MAX, mx = INT_MIN;
for (int i = 0; i < n; i++)
cin >> v[i], mn = min(v[i], mn), mx = max(v[i], mx);
vector<vector<int>> a;
vector<int> temp;
for (int i = 0; i < n; i++)
{
temp.push_back(v[i]);
if (i == n - 1 or v[i] + 1 != v[i + 1])
{
a.push_back(temp);
temp.clear();
}
}
vector<int> till;
int cnt = 0;
for (auto x : a)
{
cnt++;
if (cnt == 1)
{
till.push_back(x.front() - 1);
}
else
{
till.push_back((x.front() - temp.back()) + till.back() - 1);
}
temp = x;
// cout << till.back() << endl;
}
while (q--)
{
int k;
cin >> k;
int in;
int l = 0, r = till.size();
while (r - l > 1)
{
int mid = (r + l) / 2;
if (till[mid] >= k)
r = mid;
else if (till[mid] < k)
l = mid;
}
in = l;
// cout << in << endl;
if (k <= till[0])
cout << k << endl;
else if (in == till.size() - 1)
cout << k + n << endl;
else
cout << (a[in].back() + (k - till[in])) << endl;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
srand(chrono::steady_clock::now().time_since_epoch().count());
cout << fixed << setprecision(3) << endl;
// int tt;
// cin >> tt;
// while (tt--)
solve();
cerr << endl
<< "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define fr(i,k,n) for (int i = k; i < n; ++i)
#define fri(i,k,n) for (int i = k; i >= n; --i)
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(),arr.end()
#define ff first
#define ss second
const double pi=3.1415926535897932384626433832795;
const int inf=1e9;
const ll inf2=1e18;
const int mod=1e9+7;
void boost(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N=2e5+5;
vi adj[N],ex(N,0),ans(N,0),vis(N,0);
int mxd=0,root,timer=0,r1,r2;
void dfs(int u,int p,int d,int dd=0){
if(d>mxd){
root=u;
mxd=d;
}
for(auto v: adj[u]){
if(v!=p){
if(dd){
ex[v]=u;
}
dfs(v,u,d+1,dd);
}
}
}
void traverse(int u,int p){
ans[u]=++timer;
vis[u]=true;
for(auto v: adj[u]){
if(v==p || v==ex[u] || vis[v])
continue;
traverse(v,u);
}
if(ex[u]!=0 && vis[ex[u]]==false)
traverse(ex[u],u);
timer++;
}
void solve(){
int n,u,v;
cin>>n;
fr(i,0,n-1){
cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1,0,1);
r1=root;
mxd=0;
dfs(r1,0,1,1);
r2=root;
traverse(r2,0);
fr(i,1,n+1){
cout<<ans[i]<<" ";
}
return;
}
int main()
{
boost();
int tc=1;
//cin>>tc;
while(tc--)
solve();
return 0;
} |
#include <bits/stdc++.h>
#include <random>
#include <chrono>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> void _do(T x){cerr<<x<<"\n";}
template<typename T,typename ...U> void _do(T x,U ...y){cerr<<x<<", ";_do(y...);}
#define dbg(...) cerr<<#__VA_ARGS__<<" = ";_do(__VA_ARGS__);
const int MOD1=1e9+7;
const int MOD2=998244353;
const ll INF=2e18;
const ld PI=3.14159265358979323846;
ll gcd(ll a,ll b){if(b==0) return a; return gcd(b,a%b);}
ll fpow(ll a,ll b,ll m)
{
if(!b) return 1;
ll ans=fpow(a*a%m,b/2,m);
return (b%2?ans*a%m:ans);
}
ll inv(ll a,ll m) {return fpow(a,m-2,m);}
#define MottoHayaku ios::sync_with_stdio(false);cin.tie(0);
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define repk(i,m,n) for(int i=m;i<n;i++)
#define F first
#define S second
#define pb push_back
#define SZ(a) ((ll)a.size())
#define reset(a) memset(a,0,SZ(a))
#define cd(a,b) ((a+b-1)/b)
#define pow2(a) (1LL<<a)
#define LB(a,x) (lower_bound(a.begin(),a.end(),x))
#define LPOS(a,x) (lower_bound(a.begin(),a.end(),x)-a.begin())
#define UB(a,x) (upper_bound(a.begin(),a.end(),x))
#define UPOS(a,x) (upper_bound(a.begin(),a.end(),x)-a.begin())
#define uni(c) c.resize(distance(c.begin(),unique(c.begin(),c.end())))
#define unisort(c) sort(c.begin(),c.end()),uni(c)
ll a[105],dp[105][100005];
signed main()
{
MottoHayaku
ll n,sum=0;
cin>>n;
rep1(i,n)
{
cin>>a[i];
sum+=a[i];
dp[i][0]=1;
rep1(j,100000)
{
dp[i][j]=dp[i-1][j];
if(j>=a[i]) dp[i][j]|=dp[i-1][j-a[i]];
}
}
ll ans=0;
for(int i=sum/2;i>=0;i--)
{
if(dp[n][i])
{
ans=sum-i;
break;
}
}
cout<<ans<<"\n";
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//#include <atcoder/convolution>
//#include <atcoder/modint>
//using Modint = atcoder::modint998244353;
using namespace std;
typedef long long ll;
void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
ex_gcd(b, a % b, d, y, x);
y -= a / b * x;
}
}
ll inv(ll a, ll n) {
ll d, x, y;
ex_gcd(a, n, d, x, y);
return d == 1 ? (x % n + n) % (n / d) : -1;
}
ll gcd(ll x, ll y) {
if (y == 0) return x;
return gcd(y, x % y);
}
const int maxn = 1000005;
const int mod = 998244353;
vector<int>prim;
bool b[maxn];
void init(){
for(int i=2;i<maxn;i++){
if(!b[i]){
prim.push_back(i);
for(int j=i+i;j<maxn;j+=i)b[j]=1;
}
}
}
vector<int>p,c;
void dfs(int pos, int cur, int cnt, int l, int r, int& sum){
if(cur>r)return;
if(pos==p.size()){
if(cnt==0)return;
int tt = r/cur-(l-1)/cur;
if(cnt&1)sum += tt;
else sum -= tt;
return;
}
dfs(pos+1, cur * p[pos], cnt+1, l,r,sum);
dfs(pos+1, cur, cnt, l, r, sum);
}
void dfs2(int pos, int cur, int l, int r, int&sum){
if(cur>r)return;
if(pos==p.size()){
if(cur>=l&&cur<=r)sum++;
return;
}
int tt = 1;
for(int i=0;i<=c[pos];i++){
dfs2(pos+1, cur*tt, l,r,sum);
tt*=p[pos];
}
}
int solve(int l, int x) {
if(l>=x)return 0;
p.clear();
c.clear();
int cur = x;
for(int i=0;prim[i]*prim[i]<=cur;i++){
int tt = prim[i];
if(cur%tt==0){
int cnt = 0;
while(cur%tt==0){
cur/=tt;
cnt++;
}
p.push_back(tt);
c.push_back(cnt);
}
}
if(cur>1){
p.push_back(cur);
c.push_back(1);
}
int sum = 0;
dfs(0,1,0,l,x-1,sum);
int sum2=0;
dfs2(0,1,l,x-1,sum2);
return sum-sum2;
}
int main() {
#ifdef suiyuan2009
freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
init();
int l,r;
cin>>l>>r;
ll ret = 0;
for(int i=l+1;i<=r;i++){
ret += solve(max(2, l), i);
}
cout<<ret*2<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define inf 1e18
#define mod 1000000007
#define mod1 998244353
#define mod2 999983
#define N 200000
//***DON'T GET STUCK ON ONE APPROACH
ll power(ll a, ll n, ll m);
ll power(ll a, ll n, ll m)
{
if (n == 0)
return 1;
ll x = power(a, n / 2, m);
if (n % 2 != 0)
return (((a * x) % m) * (x) % m) % m;
else
return (x * x) % m;
}
int32_t main()
{
ll i, n, j, x, y, m, k, t, sum, count, cnt, b, c, d, j1, z, a, o, temp;
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
cnt = 0;
for (i = 1; i <= n; i++)
{
for (j = 1; j * i <= n; j++)
{
for (k = 1; k * j * i <= n; k++)
{
// cout << i << " " << j << " " << k << " ";
cnt++;
}
}
}
cout << cnt;
return 0;
}
| /// ||====================================================================||
/// || Name: Ezaz Hossain ||
/// || CodeChef: ezaz21025 ||
/// || NIT Durgapur ||
/// ||====================================================================||
#include<bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
#define mod 1000000007
#define MAX 1000000000000000000
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define lb lower_bound
#define ub upper_bound
#define ff first
#define ss second
#define in insert
#define con continue
#define br break;
#define pi pair<int,int>
#define pl pair<ll,ll>
#define ppl pair<pair<ll,ll>,ll>
#define plp pair<ll,pair<ll,ll>>
#define mem(x,y) memset(x,y,sizeof(x))
#define lcm(a,b) (a*b)/__gcd(a,b)
#define inverse_cos(x) acos(x)
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define set_bit_count(x) __builtin_popcount(x)
#define leading_zero_count(x) __builtin_clz(x)
#define tailing_zero_count(x) __builtin_ctz(x)
#define str_rev(str) reverse(str.begin(),str.end())
#define loop(i,x,y) for(ll i=x;i<y;i++)
#define rloop(i,x,y) for(ll i=x-1;i>=y;i--)
using namespace std;
int main()
{
ll n;
cin>>n;
if(n>=0)
cout << n << endl;
else
cout << 0 << endl;
} |
#include<bits/stdc++.h>
using namespace std;
int n,k,mod,dp[110][1000010],sum[110];
int main() {
ios::sync_with_stdio(0);cin.tie(0);
cin>>n>>k>>mod;
dp[0][0]=1;
for(int i=1;i<=n;i++) {
memset(sum,0,4*i);
for(int j=0,lim=n*n*k;j<=lim;j++) {
int r=j%i;
sum[r]=(sum[r]+dp[i-1][j])%mod;
if(j-(k+1)*i>=0) sum[r]=(sum[r]-dp[i-1][j-(k+1)*i]+mod)%mod;
dp[i][j]=sum[r];
}
}
for(int x=1;x<=n;x++) {
int ans=0,l=x-1,r=n-x;
for(int i=0;i<=n*n*k;i++) ans=(ans+1ll*dp[r][i]*dp[l][i])%mod;
ans=(1ll*ans*(k+1)-1+mod)%mod;
cout<<ans<<'\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 repl(i,l,r) for(int i=l;i<r;i++)
const int inf=2e9+10;
const int INF=9e18+10;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const ll MOD=1e9+7;
const ll tmax=200000;
int main(){
int h,w;cin>>h>>w;
char s[h][w];
rep(i,h)rep(j,w){
cin>>s[i][j];
}
ll dp[h][w];
rep(i,h)rep(j,w)dp[i][j]=0;
dp[h-1][w-1]=1;
ll plusw=1;
ll plush[w];
rep(i,w)plush[i]=0;
plush[w-1]=1;
int plusd[h+w-1];
rep(i,h+w-1)plusd[i]=0;
plusd[w-1]=1;
for(int i=h-1;i>=0;i--)for(int j=w-1;j>=0;j--){
if(i==h-1&&j==w-1)continue;
if(s[i][j]=='#'){
plusw=0;
plush[j]=0;
plusd[j+h-1-i]=0;
continue;
}
dp[i][j]+=plusw;
dp[i][j]%=MOD;
/*for(int k=j+1;k<w&&s[i][k]=='.';k++){
dp[i][j]+=dp[i][k];
dp[i][j]%=MOD;
}
*/
dp[i][j]+=plush[j];
dp[i][j]%=MOD;
dp[i][j]+=plusd[j+h-1-i];
dp[i][j]%=MOD;
plusw+=dp[i][j];
plusw%=MOD;
plush[j]+=dp[i][j];
plush[j]%=MOD;
if(j==0)plusw=0;
plusd[j+h-1-i]+=dp[i][j];
plusd[j+h-1-i]%=MOD;
}
/*
rep(i,h){
rep(j,w)cout<<" "<<dp[i][j];
cout<<endl;
}
*/
cout<<dp[0][0]<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> P;
typedef tuple<int,int,int> tpl;
#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
void Main(){
int N; cin >> N;
VL X(N), Y(N), Z(N); REP(i,N) cin >> X[i] >> Y[i] >> Z[i];
ll d[N][N];
REP(i,N)REP(j,N){
ll x = abs(X[i]-X[j]), y = abs(Y[i]-Y[j]), z = max((ll)0, Z[j]-Z[i]);
d[i][j] = x + y + z;
}
REP(k,N)REP(i,N)REP(j,N) chmin(d[i][j], d[i][k]+d[k][j]);
VI p(1<<N,0);
REP(i,1<<N){
int j = i<<1, k = (i<<1)+1;
if(j<(1<<N)){
p[j] = p[i];
}
if(k<(1<<N)){
p[k] = p[i]+1;
}
}
ll dp[1<<N][N]; REP(i,1<<N)REP(j,N) dp[i][j] = LINF;
dp[0][0] = 0;
REP(i,1<<N){REP(j,N){
if(dp[i][j]==LINF) continue;
REP(k,N){
if((i>>k)&1) continue;
int nxt = i | (1<<k);
chmin(dp[nxt][k], dp[i][j]+d[j][k]);
}
}
}
ll ans = dp[(1<<N)-1][0];
cout << ans << en;
return;
}
int main(void){
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
int t=1; //cin>>t;
REP(_,t) Main();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<string,int> psi;
typedef pair<int,string> pis;
typedef array<int,2> aii;
typedef array<int,3> aiii;
typedef array<int,4> aiiii;
typedef unsigned long long ull;
typedef long long int ll;
typedef array<ll,2> all;
typedef array<ll,3> alll;
typedef array<ll,4> allll;
#define pb push_back
#define ff first
#define ss second
#define MAX 300005
#define MOD 1000000007
#define INF 1e9
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<int>adj[MAX];
int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };
int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };
vector<aiii> vec;
int dp[1<<17][17][2],n;
int solve(int mask, int last, int visited_repeated)
{
if(mask==(1<<n)-1)
{
return abs(vec[0][0]-vec[last][0])+abs(vec[0][1]-vec[last][1])+max(0,vec[0][2]-vec[last][2]);
}
if(dp[mask][last][visited_repeated]!=-1)
return dp[mask][last][visited_repeated];
int ans=INF;
for(int i=0; i<n; i++)
{
if(!(mask&(1<<i)))
ans=min(ans,abs(vec[i][0]-vec[last][0])+abs(vec[i][1]-vec[last][1])+max(0,vec[i][2]-vec[last][2])+solve(mask|(1<<i),i,0));
}
if(visited_repeated==0)
for(int i=0; i<n; i++)
{
if(i!=last and (mask&(1<<i)))
ans=min(ans,abs(vec[i][0]-vec[last][0])+abs(vec[i][1]-vec[last][1])+max(0,vec[i][2]-vec[last][2])+solve(mask,i,visited_repeated+1));
}
return dp[mask][last][visited_repeated]=ans;
}
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.txt","w",stdout);
cin.tie(0),cout.tie(0);
ios_base::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(10);
int TC=1;
int m,k;
//cin>>TC;
for(int t1=1; t1<=TC; t1++)
{
cin>>n;
vec.resize(n);
for(auto &[a,b,c]: vec)
cin>>a>>b>>c;
memset(dp,-1,sizeof dp);
cout<<solve(1,0,0)<<"\n";
}
return 0;
}
|
#include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();
template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <std::ostream& os = std::cout>
class Prints {
private:
class __Prints {
public:
__Prints(const char* sep, const char* term) : sep(sep), term(term) {}
template <class... Args>
#if __cplusplus >= 201703L
auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) {
#else
void operator()(const Args&... args) const {
#endif
print(args...);
}
template <typename T>
#if __cplusplus >= 201703L
auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) {
#else
void pvec(const T& vec, size_t sz) const {
#endif
for (size_t i = 0; i < sz; i++)
os << vec[i] << (i == sz - 1 ? term : sep);
}
template <typename T>
#if __cplusplus >= 201703L
auto pmat(const T& mat, size_t h, size_t w) const -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
#else
void pmat(const T& mat, size_t h, size_t w) const {
#endif
for (size_t i = 0; i < h; i++)
for (size_t j = 0; j < w; j++)
os << mat[i][j] << (j == w - 1 ? term : sep);
}
private:
const char *sep, *term;
void print() const { os << term; }
void print_rest() const { os << term; }
template <class T, class... Tail>
void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); }
template <class T, class... Tail>
void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); }
};
public:
Prints() {}
__Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};
Prints<> prints;
Prints<std::cerr> prints_err;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void solve() {
int A, B;
cin >> A >> B;
int ans = 0;
for (ll i = 1; i <= B; i++) {
int x = (A + i - 1) / i;
int y = B / i;
if (x < y) chmax(ans, i);
}
prints()(ans);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
std::cin.tie(nullptr), std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
std::ifstream ifs("./in_out/input.txt");
std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
std::ofstream ofs("./in_out/output.txt");
std::cout.rdbuf(ofs.rdbuf());
#endif
std::cout << std::setprecision(18) << std::fixed;
solve();
std::cout << std::flush;
return 0;
} | #include <iostream>
using namespace std;
int a[110][110];
int main(){
int i,j,h,w,mn = 1000; cin >> h >> w;
for(i=0;i<h;i++){
for(j=0;j<w;j++){
cin >> a[i][j];
mn = min(mn,a[i][j]);
}
}
int sum = 0;
for(i=0;i<h;i++){
for(j=0;j<w;j++){
sum += a[i][j] - mn;
}
}
cout << sum << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 2e5 + 10, MOD = 1e9 + 7;
int id[N], P[N], F[N], n; ll inv = 0;
void update(int pos, int x) {
for (pos++; pos < N; pos += pos & -pos) F[pos] += x;
}
ll get(int pos, ll res = 0) {
for (pos++; pos; pos -= pos & -pos) res += F[pos];
return res;
}
int main() {
scanf("%d", &n); vector<int> vec;
for (int i = 1; i <= n; i++) scanf("%d", &P[i]), inv += get(N - 2) - get(P[i]), update(P[i], 1), id[P[i]] = i;
if (inv != n - 1) return !printf("-1\n");
for (int i = 1; i <= n; i++) {
for (int j = id[i] - 1; j >= i; j--) {
vec.push_back(j);
id[P[j]] = j + 1, id[P[j + 1]] = j;
swap(P[j], P[j + 1]);
}
}
for (int x : vec) printf("%d\n", x);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long i64;
void test_case() {
int n;
while (scanf("%d", &n) != EOF) {
vector<int> p(n+1);
vector<bool> used(n+1);
for (int i = 1; i <= n; i++) {
scanf("%d", &p[i]);
}
vector<int> a;
int k = 0;
for (int i = 2; i <= n; i++) {
k++;
if (p[i-1] > p[i]) {
a.push_back(i-1);
used[i-1] = true;
swap(p[i-1], p[i]);
k--;
}
for (int j = i-1; j > 1 && k > 0; j--) {
if (p[j-1] > p[j]) {
if (used[j-1]) {
break;
}
a.push_back(j-1);
used[j-1] = true;
swap(p[j-1], p[j]);
k--;
} else {
break;
}
}
}
bool sorted = true;
for (int i = 2; i <= n; i++) {
if (p[i-1] > p[i]) {
sorted = false;
break;
}
}
bool bad = !sorted || (int) a.size() != n-1;
if (bad) {
printf("-1\n");
} else {
for (int i : a) {
printf("%d\n", i);
}
}
}
}
int main() {
test_case();
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000007
using namespace std;
int main(){
long n;
cin >> n;
vector<long> ans;
for(int i=1;i<sqrt(n)+1;i++){
if(n%i==0){
ans.push_back(i);
if(i*i != n)ans.push_back(n/i);
}
}
sort(ans.begin(), ans.end());
rep(i,(int)ans.size()){
cout << ans[i] << endl;
while(i+1 < (int)ans.size() && ans[i] == ans[i+1])i++;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long n;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
vector<long long> ans;
for(long long i = 1; i*i <= n; i++) {
if(n%i == 0) {
cout << i << '\n';
if(i*i != n) ans.push_back(n/i);
}
}
for(int i = ans.size()-1; i >= 0; i--) cout << ans[i] << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
int main(void){
// Your code here!
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i,j,ans,m,n;
cin >> n;
int a[n];
for(i=0;i<n;i++)
cin >> a[i];
ans=0;
for(i=0;i<n;i++)
{
m=INT_MAX;
for(j=i;j<n;j++)
{
m=min(m,a[j]);
ans=max(ans,(j-i+1)*m);
}
}
cout << ans;
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define print(x) if(x)cout<<"YES\n";else cout<<"NO\n";
#define pb push_back
#define F first
#define S second
//#define mod 1000000007
#define mod 998244353
using namespace __gnu_pbds;
using namespace std;
void solve() {
int n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
ll res = (ll)1e14;
for (int i = 0; i <= (1 << n); i++) {
ll x = 0, ans = 0;
for (int j = 0; j < n; j++) {
if (j == 0) {
x = a[j];
}
else {
int curbit = ((i >> j) & 1);
int lastbit = ((i >> (j - 1) & 1));
if (curbit == lastbit) {
x |= a[j];
}
else {
ans ^= x;
x = a[j];
}
}
}
ans ^= x;
res = min(res, ans);
}
cout << res << "\n";
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
fastio;
int T = 1;
// cin>>T;
while (T--) {
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
void solveOne()
{
vector<string> strings(3);
for(int i = 0; i < 3; i++)
cin >> strings[i];
set<char> letters;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < strings[i].length(); j++)
{
letters.insert(strings[i][j]);
}
}
if(letters.size() > 10)
{
cout << "UNSOLVABLE\n";
return;
}
// translate to first 10 digits in alphabet
char nc = '0';
for(char c : letters)
{
for(int i = 0; i < 3; i++)
{
replace(strings[i].begin(), strings[i].end(), c, nc);
}
nc++;
}
int n = letters.size();
vector<int> values(n);
vector<int> numbers(10);
for(int i = 0; i < 10; i++)
numbers[i] = i;
vector<string> resultstrings(3);
for(int i = 0; i < 3; i++)
resultstrings[i] = strings[i];
do
{
for(int i = 0; i < n; i++)
{
values[i] = numbers[i];
}
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < strings[i].size(); j++)
{
resultstrings[i][j] = '0' + values[strings[i][j] - '0'];
}
}
bool flag = true;
for(int i = 0; i < 3; i++)
{
if(resultstrings[i][0] == '0')
flag = false;
}
if(!flag)
continue;
if(stoll(resultstrings[0]) + stoll(resultstrings[1]) == stoll(resultstrings[2]))
{
cout << resultstrings[0] << '\n';
cout << resultstrings[1] << '\n';
cout << resultstrings[2] << '\n';
return;
}
} while(next_permutation(numbers.begin(), numbers.end()));
cout << "UNSOLVABLE\n";
}
void setupIo()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
#ifndef ONLINE_JUDGE
// For getting input from input.txt file
freopen("input.txt", "r", stdin);
// Printing the Output to output.txt file
freopen("output.txt", "w", stdout);
#endif
}
int main()
{
setupIo();
int tc;
//cin >> tc;
tc = 1;
while(tc--)
solveOne();
return 0;
} | /*
Author : MatsuTaku
Date : 03/21/21
Certificate:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDF9T447OEo8XSQ6O1AznN5tKC8mzvYc4Zs3+oOKfMqgLXfOwQnpfcoxKs4MAP1eICBD13PkcIezJ9IlV6nKQZKs1BQmvjSXJ+zKK8YCKvAueNPuNO0Bim43IBaNHNFWcMvcmUvHgRb6hUSO0V8I7OsjiFo20KDBj3gAznu9tir0Q== CompetitiveProgrammingCertification:[email protected]
*/
#include <bits/stdc++.h>
using namespace std;
#include <x86intrin.h>
#define REP(i, f, n) for (auto i = (f), i##_len = (n); i < i##_len; i++)
#define rep(i, n) REP(i, (int) 0, n)
#define RREP(i, f, n) for (auto i = (n)-1, i##_left = (f); i >= i##_left; i--)
#define rrep(i, n) RREP(i, (int) 0, n)
using lint = long long int;
using ulint = unsigned long long int;
template<typename T>
using vvec = vector<vector<T>>;
template<typename T>
vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); }
template<typename T>
T& chmax(T& dst, T x) { return dst = max(dst, x); }
template<typename T>
T& chmin(T& dst, T x) { return dst = min(dst, x); }
class Solver {
public:
Solver();
void solve();
};
Solver::Solver() {}
struct Mex {
unordered_set<int> s;
int mex = 1;
void insert(int x) {
s.insert(x);
if (mex == x) {
++mex;
while (s.count(mex))
++mex;
}
}
};
void Solver::solve() {
int n; cin>>n;
vector<int> A(n);
vector<Mex> M(n);
REP(m, 2, n+1) {
A[m-1] = M[m-1].mex;
for (int j = m*2; j-1 < n; j += m) {
M[j-1].insert(A[m-1]);
}
}
A[0] = *max_element(A.begin(), A.end()) + 1;
for (auto a:A)
cout<<a<<' ';
cout<<endl;
}
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
Solver solver;
int t = 1;
// cin>>t;
while (t--) {
solver.solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define manytests int TT;cin >> TT; while (TT--)
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define FORd(i,a,b) for (int i = (a); i >= (b); --i)
int dx[8] = {-1, 1, 0, 0, 1, 1, -1, -1}, dy[8] = {0, 0, 1, -1, 1, 1, -1, -1};
#define ll long long
#define ld long double
#define fi first
#define se second
#define rev reverse
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
#define vd vector<double>
#define vs vector<string>
#define vld vector<ld>
#define vb vector<bool>
#define vvi vector<vi>
#define pii pair<int, int>
#define pl pair<ll, ll>
#define pld pair<ld, ld>
#define vpi vector<pii>
#define vpl vector<pl>
#define vpld vector<pld>
void ckmin(ll &a, ll b) { if (a > b) a = b; }
void ckmax(ll &a, ll b) { if (a < b) a = b; }
const int mxn = 1e5+5;
int c[mxn];
int vis[mxn];
vi ans;
vvi g;
void dfs(int v, int p = -1) {
if (!vis[c[v]]) ans.pb(v);
vis[c[v]]++;
for (int u : g[v]) if (u != p) dfs(u, v);
vis[c[v]]--;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
FOR(i,0,n) cin >> c[i];
g.resize(n);
FOR(i,0,n-1) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].pb(y);
g[y].pb(x);
}
dfs(0);
sort(all(ans));
for (auto x : ans) cout << x + 1 << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define No return puts("-1"), 0
using namespace std;
typedef long long i64;
const int N = 2e5+10;
int n, m, a[N], b[N], c[N], to[N], bt[N];
vector<int> da[N], db[N];
void add(int x, int v=1) { for(; x <= n; x += x&-x) bt[x] += v; }
int ask(int x) { int y = 0; for(; x; x -= x&-x) y += bt[x]; return y; }
signed main() {
cin >> n;
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]), a[i] += i, c[i] = a[i];
for(int i = 1; i <= n; i ++) scanf("%d", &b[i]), b[i] += i;
sort(c+1, c+1+n); m = unique(c+1, c+1+n) - c-1;
for(int i = 1; i <= n; i ++) {
a[i] = lower_bound(c+1, c+1+m, a[i]) - c;
int p = lower_bound(c+1, c+1+m, b[i]) - c;
if(c[p] != b[i]) No;
da[a[i]].push_back(i), db[p].push_back(i);
}
for(int i = 1; i <= m; i ++) {
if(da[i].size() != db[i].size()) No;
for(int j = 0; j < da[i].size(); j ++)
to[da[i][j]] = db[i][j];
}
i64 ans = 0;
for(int i = n; i; i --) {
ans += ask(to[i]);
add(to[i]);
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;
const int MOD = 1e9 + 7;
const LL MX = 1e9;
const LL INF = 1e9;
int main()
{
//ofstream out("output.txt");
//ifstream in("input.txt");
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
int a[n], mx = 0;
loop(i,0,n)
{
cin>>a[i];
mx = max(mx,a[i]);
}
int mxc = 0, c, ans;
loop(i,2,mx+1)
{
c = 0;
loop(j,0,n)
{
if(a[j] % i == 0) ++c;
}
if(c > mxc) {mxc = c; ans = i;}
}
cout<<ans<<'\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll d = 16;
ll dp[220000][2][17];
ll to_num(char c) {
ll x = c - '0';
if (0<=x && x<10) return x;
ll y = c - 'A';
return 10 + y;
}
ll bitcnt(ll bit) {
ll res = 0;
rep(i,d) if ((bit>>i)&1) res++;
return res;
}
void addmodp(ll &x, ll y) {x = (x + y) % mod;}
int main() {
string str; cin >> str;
ll N = str.size();
ll K; cin >> K;
vector<ll> S(N);
rep(i,N) S[i] = to_num(str[i]);
ll bit = 1<<S[0];
dp[0][0][1] = 1LL;
dp[0][1][1] = S[0] - 1;
for (ll i=1; i<N; i++) {
ll from = bitcnt(bit);
bit |= 1<<S[i];
ll now = bitcnt(bit);
dp[i][0][now] = 1;
for (ll j=1; j<=d; j++) {
addmodp(dp[i][1][j], dp[i-1][1][j] * j % mod);
addmodp(dp[i][1][j], dp[i-1][1][j-1] * (d-j+1) % mod);
}
rep(j,S[i]) {
if ((bit>>j)&1) addmodp(dp[i][1][from],1LL);
else addmodp(dp[i][1][from+1],1LL);
}
addmodp(dp[i][1][1],15);
}
ll ans = 0LL;
rep(i,2) addmodp(ans,dp[N-1][i][K]);
Cout(ans);
} |
#include<bits/stdc++.h>
using namespace std;
int n,a,ans;
int main() {
cin>>n;
while(n--) {
cin>>a;
ans+=max(0,a-10);
}
printf("%d\n",ans);
return 0;
}
| #pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define max(a, b) (a < b? b : a)
#define min(a, b) ((a>b)?b:a)
#define MOD 1000000007
#define FOR(a,c) for ( ll (a)=0; (a)<(c); (a)++)
#define FORL(a,b,c) for ( ll (a)=(b); (a)<(c); (a)++)
#define FORR(a,b,c) for ( ll (a)=(b); (a)>=(c); (a)--)
#define INF 9223372036854775807
//18446744073709551615
#define abs llabs
#define by_AYUSH ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define no cout<<"NO\n";
#define yes cout<<"YES\n";
#define Clear( a, b ) memset( a, b, sizeof( a ) )
#define run if(1)cout<<__LINE__<<endl;
#define pr3(a,b,c) cerr<<#a<<"="<<a<<"\t"<<#b<<"="<<b<<"\t"<<#c<<"="<<c<<"\n";
#define pr2(a,b) cerr<<#a<<"="<<a<<"\t"<<#b<<"="<<b<<"\n";
#define pr1(a) cerr<<#a<<"="<<a<<"\n";
#define debug(x) cerr<< #x <<" : "<< x << endl;
#define debuga(A,N) cerr<< #A <<" : [";for(int i = 0; i<N;i++) cerr<<A[i]<<" "; cerr<<"]\n";
#define debuga2(A,N,M) cerr<< #A << " : \n"; for(int i=0;i<N;i++){cerr<<"[";for(int j=0;j<M;++j) cerr<<A[i][j]<<" ";cerr<<"]\n";}
#define debugp(p) cerr<< #p <<" : "<<"("<<(p).first<<","<<(p).second<<")\n";
#define debugv(v) cerr<< #v <<" : "<<"[";for(int i = 0; i< (v).size(); i++) cerr<<v[i]<<" "; cerr<<"]\n";
#define debugv2(v) cerr<< #v << " : \n"; for(int i=0;i<v.size();i++){cerr<<"[";for(int j=0;j<(v[0].size());++j) cerr<<v[i][j]<<" ";cerr<<"]\n";}
#define debugs(m) cerr<< #m <<" : [ "; for(auto itr = m.begin(); itr!=m.end();itr++) cerr<<*itr<<" "; cerr<<"]\n";
#define debugm(m) cerr<< #m <<" : [ "; for(auto itr = m.begin();itr!=m.end(); itr++) cerr<<"("<<itr->first<<","<<itr->second<<") ";cerr<<"]\n";
typedef unsigned long long ull;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
inline long long max3(long long a, long long b,long long c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long min3(long long a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}
#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair
void solve()
{
int n;cin>>n;
int s=0;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]>=10)
s+=a[i]-10;
}
cout<<s;
}
int main()
{
//int t;cin>>t;
// while(t--)
// {
solve();
// }
return 0;
}
// █████╗ ██╗ ██╗██╗ ██╗███████╗██╗ ██╗
// ██╔══██╗╚██╗ ██╔╝██║ ██║██╔════╝██║ ██║
// ███████║ ╚████╔╝ ██║ ██║███████╗███████║
// ██╔══██║ ╚██╔╝ ██║ ██║╚════██║██╔══██║
// ██║ ██║ ██║ ╚██████╔╝███████║██║ ██║
// ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝
//
//
//
//
//
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define sz(x) int(x.size())
#define pcnt __builtin_popcountll
template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;}
template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;}
template <typename T> void print(T a) {cout << a << ' ';}
template <typename T> void printe(T a) {cout << a << endl;}
template <typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;}
template <typename T> void printp(pair<T, T> a) {print(a.first); cout<<a.second<<endl;}
template <typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);}
template <typename T> using vc = vector<T>;
template <typename T> using vv = vc<vc<T>>;
template <typename T> using vvv = vc<vv<T>>;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vc<int>;
using vvi = vv<int>;
using vl = vc<ll>;
using vvl = vv<ll>;
vi di = {-1, 1, 0, 0, -1, -1, 1, 1};
vi dj = { 0, 0, -1, 1, -1, 1, -1, 1};
int main () {
vc<string> s(3);
rep(i, 3) cin >> s[i];
map<char, ll> Mp; // その文字に割り当てられたものを何倍するか
rep(i, 3) {
ll scale = 1;
for (int j = sz(s[i])-1; j >= 0; j--) {
if (i == 2) {
Mp[s[i][j]] -= scale;
}
else {
Mp[s[i][j]] += scale;
}
scale *= 10;
}
}
if (sz(Mp) > 10) {
cout << "UNSOLVABLE" << endl;
return 0;
}
map<char, int> Mp2; // 各文字に何の文字入れるか
auto restore = [&] (string t) {
ll res = 0;
rep(i, sz(t)) {
res *= 10;
res += Mp2[t[i]];
}
return res;
};
vi p(10);
rep(i, 10) p[i] = i;
do {
int now = 0; // 今何番目の要素か
ll sum = 0;
for (auto x : Mp) {
sum += p[now] * x.second;
Mp2[x.first] = p[now];
now++;
}
if (sum == 0) {
vl n(3);
bool flag = true;
rep(i, 3) {
n[i] = restore(s[i]);
if (n[i] <= 0 || sz(to_string(n[i])) < sz(s[i])) flag = false;
}
if (flag) {
rep(i, 3) cout << n[i] << endl;
return 0;
}
else {
continue;
}
}
Mp2.clear();
} while (next_permutation(all(p)));
cout << "UNSOLVABLE" << endl;
return 0;
} | /* हरे कृष्ण हरे कृष्ण कृष्ण कृष्ण हरे हरे
हरे राम हरे राम राम राम हरे हरे */
#include<bits/stdc++.h>
using namespace std;
#define pb emplace_back
#define pob pop_back
typedef long long int lli;
#define test lli t; cin>>t; while(t--)
#define ff first
#define ss second
#define F(n) for(lli i=0;i<n;i++)
#define pf pop_front
#define lb lower_bound
#define ub upper_bound
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define bs binary_search
#define all(x) x.begin(),x.end()
#define nl "\n"
#define loop(i,s,n) for(lli i=s;i<n;i++)
#define pp(a) for(auto x : a) cout<<x<<" "; cout<<nl;
#define mem(arr,x) memset(arr,x,sizeof(arr))
#define mod 1000000007
#define inf 1e18
#define tt(n) cin>>n; lli a[n]; loop(i,0,n) cin>>a[i];
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ps(x,y) fixed<<setprecision(y)<<x
int main()
{
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
lli n,i;
string s1,s2,s3;
cin>>s1>>s2>>s3;
set<char> s;
loop(i,0,s1.length()) s.insert(s1[i]);
loop(i,0,s2.length()) s.insert(s2[i]);
loop(i,0,s3.length()) s.insert(s3[i]);
if(s.size()>10) cout<<"UNSOLVABLE";
else
{
std::vector<char> a;
n=s.size();
for(auto x: s) a.pb(x);
lli b[10]={0,1,2,3,4,5,6,7,8,9},f=0;
//for(auto x: a) cout<<x<<" ";
//set<lli> sm;
do
{
map<char,lli> m;
lli k=0;
loop(i,0,n) m[a[i]]=b[k++];
//for(auto x: m) cout<<x.ff<<" "<<x.ss<<nl;
// break;
//sm.insert(m['s']);
lli c1=0,c2=0,c3=0;
if(m[s1[0]]==0 || m[s2[0]]==0 || m[s3[0]]==0) ;
else
{
loop(i,0,s1.length())
{
c1=(c1*10)+m[s1[i]];
}
loop(i,0,s2.length())
{
c2=(c2*10)+m[s2[i]];
}
loop(i,0,s3.length())
{
c3=(c3*10)+m[s3[i]];
}
if(c1+c2==c3 && c1>0 && c2>0 && c3>0)
{
f=1;
loop(i,0,s1.length()) cout<<m[s1[i]];
cout<<nl;
loop(i,0,s2.length()) cout<<m[s2[i]];
cout<<nl;
loop(i,0,s3.length()) cout<<m[s3[i]];
break;
}
}
}while(next_permutation(b,b+10));
//for(auto x: sm) cout<<x<<" ";
if(f==0) cout<<"UNSOLVABLE";
}
return 0;
} |
#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
//#define file
using namespace std;
int n,i,j,k,l,m,sum;
ll s[2];
char st[21];
int main()
{
#ifdef file
freopen("a.in","r",stdin);
#endif
scanf("%d%d",&n,&m);
fo(i,1,n)
{
scanf("%s",st+1),sum=0;
fo(j,1,m) sum+=st[j]=='1';
++s[sum&1];
}
printf("%lld\n",s[0]*s[1]);
fclose(stdin);
fclose(stdout);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define Rep(i, a, b) for(ll i = a; i <= b; i++)
#define repr(i, a, b) for(ll i = b-1; i >= a; i--)
// #define _GLIBCXX_DEBUG
template <class T> using V = vector<T>;
#define ALL(v) (v).begin(),(v).end()
#define endl '\n'
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
// const ll MOD = 1000000007LL;
const ll MOD = 998244353LL;
const ll INF = 1LL << 60;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
const int dy2[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx2[] = {1, 1, 0, -1, -1, -1, 0, 1};
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
int main(){
ll n, m;
cin >> n >> m;
V<ll> h(n), w(m);
rep(i, 0, n) cin >> h[i];
sort(ALL(h));
rep(i, 0, m) cin >> w[i];
V<ll> l(n), r(n);
rep(i, 2, n){
if(i%2 == 0){
l[i]+=h[i-1]-h[i-2];
l[i]+=l[i-2];
}
}
repr(i, 0, n-2){
if(i%2==0){
r[i]+=h[i+2]-h[i+1];
r[i]+=r[i+2];
}
}
// rep(i, 0, n) cout << l[i] << " \n"[i==n-1];
// rep(i, 0, n) cout << r[i] << " \n"[i==n-1];
ll ans = INF;
rep(i, 0, m){
ll id = lower_bound(ALL(h), w[i]) - h.begin();
// cout << w[i] << " " << id << endl;
if(id == 0){
chmin(ans, abs(h[0]-w[i])+r[0]);
}else if(id == n){
chmin(ans, abs(h[n-1]-w[i])+l[n-1]);
}else{
if(id%2==0){
chmin(ans, l[id]+r[id]+abs(h[id]-w[i]));
}else{
chmin(ans, l[id-1]+r[id-1]+abs(h[id-1]-w[i]));
}
}
}
cout << ans << endl;
return 0;
} |
#pragma region Region_1
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VP = vector<P>;
#define MOD 1000000007
const int INF = 1e9 + 10;
template <class T, class C>
void chmax(T& a, C b) {
if (a <= b) a = b;
}
template <class T, class C>
void chmin(T& a, C b) {
if (a >= b) a = b;
}
template <class T>
T sum(const vector<T>& v) {
T res = 0;
for (size_t i = 0; i < v.size(); ++i) res += v[i];
return res;
}
/////////////////////////////////////////////////////////
// print like python
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed
/////////////////////////////////////////////////////////
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
cout << head;
if (sizeof...(tail) != 0) cout << " ";
print(forward<Tail>(tail)...);
}
template <class T>
void print(vector<T>& vec) {
for (auto& a : vec) {
cout << a;
if (&a != &vec.back()) cout << " ";
}
cout << endl;
}
template <class T>
void print(vector<vector<T>>& df) {
for (auto& vec : df) {
print(vec);
}
}
#pragma endregion Region_1
/////////////////////////////////////////////////////////
/**
* 3角形の面積
*/
ll triangle_area(ll x1, ll y1, ll x2, ll y2, ll x3, ll y3) {
x2 -= x1;
x3 -= x1;
y2 -= y1;
y3 -= y1;
return abs(x2 * y3 - y2 * x3);
}
int main() {
//入力の高速化用のコード
ios::sync_with_stdio(false);
cin.tie(nullptr);
std::cout << std::setprecision(15);
//////////////////////////////////////////
int n;
cin >> n;
VI x(n);
VI y(n);
rep(i, n) {
cin >> x[i] >> y[i];
}
rep(i, n) srep(j, i + 1, n) srep(k, j + 1, n) {
if (triangle_area(x[i], y[i], x[j], y[j], x[k], y[k])==0) {
print("Yes");
return 0;
}
}
print("No");
return 0;
}
| #include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();
template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
class Prints {
private:
class __Prints {
public:
__Prints(const char* sep, const char* term) : sep(sep), term(term) {}
template <class... Args>
auto operator()(const Args&... args) const -> decltype((std::cout << ... << std::declval<Args>()), void()) { print(args...); }
template <typename T>
auto pvec(const T& vec, size_t sz) const -> decltype(std::cout << std::declval<decltype(std::declval<T>()[0])>(), void()) {
for (size_t i = 0; i < sz; i++)
std::cout << vec[i] << (i == sz - 1 ? term : sep);
}
template <typename T>
auto pmat(const T& mat, size_t h, size_t w) -> decltype(std::cout << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
for (size_t i = 0; i < h; i++)
for (size_t j = 0; j < w; j++)
std::cout << mat[i][j] << (j == w - 1 ? term : sep);
}
private:
const char *sep, *term;
void print() const { std::cout << term; }
void print_rest() const { std::cout << term; }
template <class T, class... Tail>
void print(const T& head, const Tail&... tail) const { std::cout << head, print_rest(tail...); }
template <class T, class... Tail>
void print_rest(const T& head, const Tail&... tail) const { std::cout << sep << head, print_rest(tail...); }
};
public:
Prints() {}
__Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};
Prints prints;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void solve() {
int N;
cin >> N;
using Pos = array<ll, 2>;
vector<Pos> ps(N);
for (ll i = 0; i < N; i++) {
int a, b;
cin >> a >> b;
ps[i] = {a, b};
}
for (ll i = 0; i < N; i++) {
for (ll j = i + 1; j < N; j++) {
for (ll k = j + 1; k < N; k++) {
Pos b1, b2;
b1[0] = ps[j][0] - ps[i][0];
b1[1] = ps[j][1] - ps[i][1];
b2[0] = ps[k][0] - ps[i][0];
b2[1] = ps[k][1] - ps[i][1];
if (b1[0] * b2[1] - b1[1] * b2[0] == 0) {
prints()("Yes");
return;
}
}
}
}
prints()("No");
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
std::cin.tie(nullptr), std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
std::ifstream ifs("./in_out/input.txt");
std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
std::ofstream ofs("./in_out/output.txt");
std::cout.rdbuf(ofs.rdbuf());
#endif
std::cout << std::setprecision(18) << std::fixed;
solve();
std::cout << std::flush;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define All(a) (a).begin(),(a).end()
#define INF numeric_limits<ll>::max()
int main(){
ll n,i,ans;
cin >>n;
n++;
for(i=(ll)sqrt(2*n);i*(i+1)>2*n;i--){
}
cout << n-i << endl;
return 0;
}
| #include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{ ios_base::sync_with_stdio(false); cin.tie(0);
long long n, min, max;
cin>>n;
min = 0;
max = n+1;
while(max > min){
min++;
max-=min;
}
cout<<n-min+1<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (ll i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
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; }
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
using Graph = vector<vector<ll>>;
const long long INF = 1LL<<60;
const long long MOD = 1000000007;
// Sieve of Eratosthenes
// https://youtu.be/UTVg7wzMWQc?t=2774
struct Sieve {
int n;
vector<ll> f, primes;
Sieve(int n=1):n(n), f(n+1) {
f[0] = f[1] = -1;
for (ll i = 2; i <= n; ++i) {
if (f[i]) continue;
primes.push_back(i);
f[i] = i;
for (ll j = i*i; j <= n; j += i) {
if (!f[j]) f[j] = i;
}
}
}
bool isPrime(int x) { return f[x] == x;}
vector<int> factorList(int x) {
vector<int> res;
while (x != 1) {
res.push_back(f[x]);
x /= f[x];
}
return res;
}
vector<P> factor(int x) {
vector<int> fl = factorList(x);
if (fl.size() == 0) return {};
vector<P> res(1, P(fl[0], 0));
for (int p : fl) {
if (res.back().first == p) {
res.back().second++;
} else {
res.emplace_back(p, 1);
}
}
return res;
}
vector<pair<ll,int>> factor(ll x) {
vector<pair<ll,int>> res;
for (int p : primes) {
int y = 0;
while (x%p == 0) x /= p, ++y;
if (y != 0) res.emplace_back(p,y);
}
if (x != 1) res.emplace_back(x,1);
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n;cin>>n;
vec a(n),t(n);
rep(i,n)cin>>a[i]>>t[i];
ll q;cin>>q;
multiset<ll> x;
map<ll,ll> mp;
rep(i,q){
ll X;cin>>X;
x.insert(X);
mp[i]=X;
}
vec ans(q);
vec it(q,-1);
ll tot=0;
map<ll,ll> M;
rep(i,n){
if(t[i]==1)tot+=a[i];
else if(t[i]==2){
auto itr=x.lower_bound(a[i]-tot+1);
if(itr==x.begin())continue;
itr--;
while(1){
if(itr==x.begin()){M[*itr]=a[i]-tot;x.erase(itr);break;}
else{
M[*itr]=a[i]-tot;
x.erase(itr--);
}
}
x.insert(a[i]-tot);
M[a[i]-tot]=a[i]-tot;
}
else{
auto itr=x.lower_bound(a[i]-tot);
if(itr==x.end())continue;
while(itr!=x.end()){
M[*itr]=a[i]-tot;
x.erase(itr++);
}
x.insert(a[i]-tot);
M[a[i]-tot]=a[i]-tot;
}
}
rep(i,q){
ll ans=mp[i];
vec memo;
while(1){
if(M.count(ans)){
if(M[ans]!=ans){
memo.push_back(ans);
ans=M[ans];
}
else break;
}
else break;
}
rep(j,memo.size())M[memo[j]]=ans;
cout<<ans+tot<<endl;
}
return 0;
} | //#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#define rep(i,n) for(ll (i)=0;(i)<(ll)(n);(i)++)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007LL
#define INF (1LL<<60LL)
#define int long long
typedef long long ll;
using namespace std;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k, m; cin >> n >> k >> m;
int ans = n * m;
rep(i, n - 1) {
int a; cin >> a;
ans -= a;
}
if (ans < 0) ans = 0;
else if (ans > k) ans = -1;
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pii pair <int,int>
#define pll pair <ll,ll>
#define testcase() int t; cin>>t; while(t--)
#define forn(i,n) for(int i=0;i<n;i++)
#define forn1(i,n) for(int i=1;i<=n;i++)
#define vll vector <ll>
#define vi vector <int>
#define all(v) v.begin(),v.end()
ll M = 1e9 + 7;
double pi = acos(-1.0);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll powerm(ll x,ll y){ ll res=1; while(y){ if(y&1) res=(res*x)%M; y=y>>1; x=(x*x)%M;} return res%M; }
ll power(ll x,ll y){ ll res=1; while(y){ if(y&1) res=(res*x); y=y>>1; x=(x*x);} return res; }
ll gcd(ll a,ll b){if(b>a)return gcd(b,a); if(b==0)return a; return gcd(b,a%b);}
////////////////////////////////////////////////////
// Function to return the sum of arr[0..index]
// This function assumes that the array is preprocessed
// and partial sums of array elements are stored in BITree[]
int getSum(int BITree[], int index)
{
int sum = 0; // Initialize result
// Traverse ancestors of BITree[index]
while (index > 0) {
// Add current element of BITree to sum
sum += BITree[index];
// Move index to parent node in getSum View
index -= index & (-index);
}
return sum;
}
// Updates a node in Binary Index Tree (BITree) at given index
// in BITree. The given value 'val' is added to BITree[i] and
// all of its ancestors in tree.
void updateBIT(int BITree[], int n, int index, int val)
{
// Traverse all ancestors and add 'val'
while (index <= n) {
// Add 'val' to current node of BI Tree
BITree[index] += val;
// Update index to that of parent in update View
index += index & (-index);
}
}
///////////////////////////////////////////////////
int main() {
//ifstream cin("input.txt");
//ofstream cout("output.txt");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
vi a(n);
forn(i,n) cin>>a[i];
// vi s;
int BIT[n + 1];
for (int i = 1; i <= n; i++)
BIT[i] = 0;
vi suff(n,0);
// s.pb(a[n-1]);
ll ans = 0;
for (int i = n - 1; i >= 0; i--) {
// Get count of elements smaller than arr[i]
suff[i] = getSum(BIT, a[i]);
ans += suff[i];
// Add current element to BIT
updateBIT(BIT, n, a[i]+1, 1);
}
// for(int i=n-2;i>=0;i--)
// {
// auto it = lower_bound(all(s),a[i]);
// int h = it - s.begin();
// suff[i] = h;
// // it--;
// ans += h;
// s.insert(it,a[i]);
// // for(auto it:s) cout<<it<<':'; cout<<endl;
// }
// forn(i,n) cout<<suff[i]<<' ';
cout<<ans<<'\n';
int sz = 0;
forn(i,n-1)
{
// auto it = lower_bound(all(s),a[i]);
int h = a[i] - suff[i];
ans += (n-1-i-2*suff[i]);
ans -= h;
ans += (sz-h);
// s.insert(it,a[i]);
sz++;
// for(auto it:s) cout<<it<<':'; cout<<endl;
cout<<ans<<'\n';
}
return 0;
}
| #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>
// #include <atcoder/all>
using namespace std;
using ll = long long int;
using vi = vector<int>;
using vll = vector<ll>;
using ss = string;
using db = double;
template<class T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define pii pair<int,int>
#define pll pair<ll,ll>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define pb push_back
#define eb emplace_back
#define rz resize
#define sz(x) int(x.size())
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}
template<class T>void allv(V<T> v){for(T NUM:v)printf("%d ",NUM);printf("\n");}
int main(){
ll a, b ; cin>> a >> b ;
ll c, d ; cin>> c >> d ;
ll cdb=c*d-b;
ll ans=1001001001;
if(cdb<=0)ans=-1;
else ans=(a+cdb-1)/cdb;
cout<< ans <<endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define asll unsigned long long
#define ll long long
#define ld long double
#define imt int
#define pii pair<int,int>
#define st first
#define nd second
#define psbk(x) push_back(x);
#define For(x,a,b) for(int x=(a);x!=(b+((b-a)/abs(b-a)));x+=(b-a)/abs(b-a))
#define FOR(x,n) for(int x=0;x<(n);x++)
#define m_p make_pair
#define kj <<" "<<
#define kjb <<" ";
#define dg <<","<<
#define ml <<endl;
#define co cout<<
#define ter cout<<endl;
#define sibi cout<<"TES"<<endl;
#define tes(x) cout<<"TES"<<x<<endl;
#define kpr(x) cout<<"("<<x.st dg x.nd <<")" ml
#define kkor(p) cout<<"("<<p.x dg p.y <<")" ml
#define kera(x,m) for( int i = 0;i < m ; i++ ){ cout<<"x["<< i <<"] =" kj x[i] ml}
asll M=1e9+7;
int n,m,k,tc;
int p,q,r;
int js,js1,js2,js3,js4,js5;
int lj1,lj2;
int la,la1,la2;
string s;
stack<char> st;
string cek=" ";
int main()
{
cin>>p;
cin.ignore();
getline(cin,s);
FOR(i,s.size()){
st.push(s[i]);
if(st.size()>=3){
FOR(j,3){
cek[j]=st.top();
st.pop();
}
if(cek!="xof"){
For(j,2,0){
st.push(cek[j]);
}
}
}
}
co st.size() ml
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;
template<typename T> class segtree {
private:
int n,sz,h;
vector<T> node, lazy;
void eval(int k) {
if(lazy[k]){
node[k] += lazy[k];
if(k < n) {
lazy[k*2] += lazy[k] / 2, lazy[k*2+1] += lazy[k] / 2;
}
lazy[k] = 0;
}
}
public:
segtree(const vector<T>& v) : n(1), sz((int)v.size()), h(0) {
while(n < sz) n *= 2, h++;
node.resize(2*n, 0), lazy.resize(2*n, 0);
for(int i = 0; i < sz; i++) node[i+n] = v[i];
for(int i = n-1; i >= 1; i--) node[i] = node[2*i] + node[2*i+1];
}
void range(int a, int b, T x, int k=1, int l=0, int r=-1){
if(r < 0) r = n;
eval(k);
if(b <= l || r <= a){
return;
}
if(a <= l && r <= b){
lazy[k] += (r-l)*x;
eval(k);
}else{
range(a, b, x, 2*k, l, (l+r)/2);
range(a, b, x, 2*k+1, (l+r)/2, r);
node[k] = node[2*k] + node[2*k+1];
}
}
T query(int a, int b) {
a += n, b += n - 1;
for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);
b++;
T res1 = 0, res2 = 0;
while(a < b) {
if(a & 1) eval(a), res1 += node[a++];
if(b & 1) eval(--b), res2 += node[b];
a >>= 1, b >>= 1;
}
return res1 + res2;
}
void _print(){for(int i = 0; i < sz; i++) cout<<query(i,i+1)<< " ";cout<<endl;}
};
int main(){
cin.tie(0); ios::sync_with_stdio(false);
ll n; cin >> n;
ll a[n]; zep(i, 0, n)cin >> a[i];
vector<ll> v(n, 0);
segtree<ll> s(v);
ll ans = 0;
ll cnt[n]; memset(cnt, 0, sizeof(cnt));
zep(i, 0, n){
ans += s.query(a[i], n);
cnt[a[i]] = a[i] - s.query(0, a[i]);
s.range(a[i], a[i] + 1, 1);
}
//printa(cnt, 0, n - 1)
vector<ll> u(n, 0);
segtree<ll> t(u);
print(ans)
zep(i, 0, n - 1){
ans += n - i - 1 - 2 * cnt[a[i]];
//print(ans)
ans += t.query(a[i], n) - t.query(0, a[i]);
print(ans)
t.range(a[i], a[i] + 1, 1);
}
return 0;
} |
#include <iostream>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
using namespace std;
typedef long long int ll;
typedef pair <int,int> pii;
typedef pair<ll,ll> pll;
/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
return a.first < b.first;
}*/
// use case
//sort(V.begin(), V.end(), compare_function);
/*struct compare
{
bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
if (a.second-a.first==b.second-b.first)
{
return a.first>b.first;
}
return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/
void print(vector <ll> X)
{
for (int i=0; i<X.size(); i++)
{
cout<<X[i]<<" ";
}
cout<<endl;
return;
}
ll keisk(char x) {
if (x=='B') {return 0LL;}
if (x=='R') {return 1LL;}
return 2LL;
}
ll kiek(ll a) {
//cout<<a<<endl;
ll k=0;
while (a>0) {
k+=a/3;
a=a/3;
}
//cout<<a<<" "<<k<<endl;
return k;
}
ll find(int n, int k, vector <ll> &F, vector <ll> &K) {
if (K[n]>K[k]+K[n-k]) {return 0LL;}
if (k==0 || k==n) {return 1LL;}
return (F[n]*F[k]*F[n-k])%3;
}
char keisk2(ll ats) {
if (ats==0) {return 'B';}
if (ats==1) {return 'R';}
return 'W';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vector <ll> A(n);
vector <ll> F(n,1);
vector <ll> K(n,0);
for (int i=0;i<n;i++) {
K[i]=kiek(i);
}
for (int i=1;i<n;i++) {
int j=i;
while (j%3==0) {j=j/3;}
F[i]=(F[i-1]*j)%3;
}
//print(K);
//print(F);
for (int i=0;i<n;i++) {
char x;
cin>>x;
A[i]=keisk(x);
}
ll ats=0;
vector <ll> X(n);
for (int i=0;i<n;i++) {
X[i]=find(n-1,i,F,K);
ats+=X[i]*A[i];
ats=ats%3;
}
//cout<<ats<<endl;
//print(X);
if (n%2==0 && ats!=0) {ats=3-ats;}
char z=keisk2(ats);
//cout<<ats<<endl;
cout<<z<<endl;
return 0;
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end
int C[5][5];
int nCr(int n,int r){
if(C[0][0]==0){
rep(i,0,5){
C[i][0]=C[i][i]=1;
rep(j,1,i){
C[i][j]=(C[i-1][j-1]+C[i-1][j])%3;
}
}
}
int res=1;
rep(_,0,30){
res*=C[n%3][r%3];
n/=3,r/=3;
}
return res;
}
int main(){
int n;
cin>>n;
string s;
cin>>s;
int res=0;
rep(i,0,n){
if(s[i]=='R')continue;
if(s[i]=='W')res=(res+nCr(n-1,i))%3;
if(s[i]=='B')res=(res+nCr(n-1,i)*2)%3;
}
if(!(n&1))res=(3-res)%3;
if(res==0)puts("R");
if(res==1)puts("W");
if(res==2)puts("B");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
std::string to_string(long long);
using std::stol;
int main() {
ll n;
cin >> n;
vector<double> x(n);
vector<double> y(n);
ll ans = 0;
rep(i,n) {
cin >> x[i] >> y[i];
}
for(ll i = 0;i < n;i++) {
double cnt;
for(ll j = i+1;j < n;j++) {
double cnt = (y[i]-y[j]) /(x[i]-x[j]);
if (cnt >= -1 && cnt <= 1) ans++;
}
}
cout << ans << endl;
return 0;
}
| #include<iostream>
#include<vector>
using namespace std;
float findslope(vector<long> point1, vector<long> point2)
{
double x1 = point1[0];
double y1 = point1[1];
double x2 = point2[0];
double y2 = point2[1];
return ((y2-y1)/(x2-x1));
}
int main(){
long N;
cin >> N;
vector<vector<long>> coordinates;
coordinates.resize(N);
long x,y;
for(int i=0;i<N;++i)
{
cin >> x >> y;
coordinates[i].push_back(x);
coordinates[i].push_back(y);
}
int count=0;
float slope;
for(int i=0;i<N;++i)
{
for(int j=0;j<N;++j)
{
if(i==j) continue;
slope = findslope(coordinates[i], coordinates[j]);
if(slope >= -1 && slope <= 1)
{
++count;
}
}
}
cout << count/2;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define lb lower_bound
#define ub upper_bound
#define fe(x) (*(x.begin()))
#define le(x) (*(x.rbegin()))
#define pb push_back
#define load(node,n) bool operator < (const node &n) const
#define fbo(x,id) (*(x.find_by_order(id)))
#define ook(y) order_of_key(y)
#define all(x) (x).begin(),(x).end()
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define sl(a) scanf("%lld",&a)
#define sll(a,b) scanf("%lld %lld",&a,&b)
#define slll(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define rep(i,a,b) for(ll i = a; i<=b; i++)
#define un(x) x.erase(unique(all(x)), x.end())
#define TT long long TC; scanf("%lld",&TC); while(TC--)
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define set(x) tree<x,null_type,less<x>,rb_tree_tag,tree_order_statistics_node_update>
#define assign(v,n,x) v.clear(); for(int I = 0; I<n; I++) v.pb(x);
#define SZ(x) (int)x.size()
#define ff first
#define ss second
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define mx 23
ll ar[mx];
ll MX = 1e12,n;
ll ans = MX;
void fn(ll pos,ll w = 0,ll Or = 0){
if(pos==n){
w^=Or;
ans = min(ans,w);
return;
}
ll p = Or|ar[pos];
fn(pos+1,w^p,0);
fn(pos+1,w,p);
}
main(){
//read;
sl(n);
for(ll i = 0; i<n; i++){
sl(ar[i]);
}
fn(0);
cout<<ans;
return 0;
}
|
// Problem: C - ORXOR
// Contest: AtCoder - AtCoder Beginner Contest 197(Sponsored by Panasonic)
// URL: https://atcoder.jp/contests/abc197/tasks/abc197_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define pi 3.141592653589793238462643383279
#define int long long
#define lld long double
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=(int)x; i<y; i++)
#define VI vector <int>
#define VVI vector <VI>
#define pii pair<int, int>
#define ppi pair<pii, int>
#define mii map<int, int>
#define mci map<char, int>
#define miv map<int, VI>
#define mis map<int, set<int>>
#define high_functioning_sociopath ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const unsigned int M = 998244353;
/*_____________________________THE GAME IS ON_____________________________*/
void solve()
{
int n;
cin >> n;
int res = 1e17;
vector<int>arr(n);
for(int i=0;i<n;i++) cin >> arr[i];
for(int mask=0;mask<(1<<(n-1));mask++)
{
int x = 0;
int o = 0;
for(int i=0;i<n;i++)
{
if(!(mask&(1<<i)))
{
o|=arr[i];
}
else
{
o|=arr[i];
x^=o;
o=0;
}
}
x^=o;
res = min(x,res);
}
cout << res;
}
int32_t main()
{
high_functioning_sociopath
//pre();
int t=1;
//cin>>t;
while(t--)
{
solve();
}
} |
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
using namespace std;
const int max_n = 2010;
const int Mod = 1e9+7;
const int inf = 1e18;
const int lim = 3e3+20;
char a[max_n][max_n];
int dist[max_n][max_n],visited[max_n][max_n];
int v_char[26];
vector<pair<int,int> > adj[26];
pair<int,int> dir[4] = {{-1,0},{1,0},{0,1},{0,-1}};
signed main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n,m;cin>>n>>m;
char a[n][m];
pair<int,int> s,e;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>a[i][j];
if(a[i][j]=='S') s={i,j};
if(a[i][j]=='G') e={i,j};
if(a[i][j]>='a' && a[i][j]<='z'){
adj[a[i][j]-'a'].push_back({i,j});
}
}
}
queue<pair<int,int> > q;
q.push(s);
while(q.size()>0){
pair<int,int> c = q.front();
q.pop();
for(int i=0;i<4;i++){
pair<int,int> t(c.first+dir[i].first,c.second+dir[i].second);
if(t.first>=0 && t.first<n && t.second>=0 && t.second<m){
if(!visited[t.first][t.second] && a[t.first][t.second]!='#'){
q.push(t);
dist[t.first][t.second]=dist[c.first][c.second]+1;
visited[t.first][t.second]=1;
}
}
}
char X = a[c.first][c.second];
if(X>='a' && X<='z'){
if(!v_char[X-'a']){
v_char[X-'a']=1;
for(pair<int,int> t:adj[X-'a']){
if(!visited[t.first][t.second] && a[t.first][t.second]!='#'){
q.push(t);
dist[t.first][t.second]=dist[c.first][c.second]+1;
visited[t.first][t.second]=1;
}
}
}
}
}
if(dist[e.first][e.second]==0){
cout<<-1<<endl;
}else{
cout<<dist[e.first][e.second]<<endl;
}
} | #include <bits/stdc++.h>
#define rep(i,N) for (int i = 0; i < (N); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int inf{int(1e9)};
int main(void){
int N;
string T;
cin >> N >> T;
// N == 1 or 2を例外処理
if (N == 1){
if (T == "0") cout << 10000000000 << endl;
else if (T == "1") cout << 20000000000 << endl;
return 0;
}
else if (N == 2){
if (T == "00") cout << 0 << endl;
else if (T == "01") cout << 9999999999 << endl;
else if (T == "10" || T == "11") cout << 10000000000 << endl;
return 0;
}
// 一つ以上含まれるかどうか確かめる
rep(i,N){
if (i%3 == 0 && T[i] != T[0] || i%3 == 1 && T[i] != T[1] || i%3 == 2 && T[i] != T[2]){
cout << 0 << endl;
return 0;
}
else if(T.substr(0,3) != "110" && T.substr(0,3) != "101" && T.substr(0,3) != "011"){
cout << 0 << endl;
return 0;
}
}
// ここまできたら一個以上含まれることが保証される
ll ans;
if (T.substr(0,3) == "110"){
ans = (30000000000 - N) / 3;
ans ++;
}
else if (T.substr(0,3) == "101"){
ans = (30000000000 - (N+1)) / 3;
ans ++;
}
else if (T.substr(0,3) == "011"){
ans = (30000000000 - (N+2)) / 3;
ans ++;
}
cout << ans << endl;
return 0;
}
|
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define SET0(a) memset(a,0,sizeof(a))
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define DWN(i,a,b) for(int i=(a);i>=(b);i--)
typedef long long ll;
const ll INF=LLONG_MAX;
typedef pair<ll,ll> PII;
const int N=1e5+5;
ll d[N];
struct node{
ll to,next,w,k;
}e[N<<1];
int head[N],tot;
void add(ll u,ll v,ll w,ll k){e[tot].to=v;e[tot].w=w;e[tot].k=k;e[tot].next=head[u];head[u]=tot++;}
bool vis[N];
int n,m;
int s,t;
ll dijk(){
for(int i=1;i<=n;i++) d[i]=INF;
priority_queue<PII,vector<PII>,greater<PII> > pque;
pque.push({0,s});
d[s]=0;
while(pque.size()){
auto hd=pque.top(); pque.pop();
int ver=hd.second;
if(vis[ver]) continue;
vis[ver]=true;
for(int i=head[ver];~i;i=e[i].next){
int go=e[i].to;
int w=e[i].k-(d[ver]%e[i].k==0?e[i].k:d[ver]%e[i].k)+e[i].w;
if(d[go]>d[ver]+w){
d[go]=d[ver]+w;
pque.push({d[go],go});
}
}
}
if(d[t]==INF) return -1;
else return d[t];
}
int main(){
memset(head,-1,sizeof head);
cin>>n>>m>>s>>t;
while(m--){
ll u,v,w,k;
cin>>u>>v>>w>>k;
add(u,v,w,k);
add(v,u,w,k);
}
cout<<dijk()<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)
using T = tuple<int,ll,ll>;
using P = pair<ll,int>;
int main(){
int n,m,x,y;
cin>>n>>m>>x>>y;
x--;y--;
vector<vector<T>> graph(n);
rep(i,0,m){
int a,b;
ll t,k;
cin>>a>>b>>t>>k;
a--;b--;
graph[a].pb({b,t,k});
graph[b].pb({a,t,k});
}
vll dist(n,1e18);
dist[x]=0;
priority_queue<P,vector<P>,greater<P>> que;
que.push({0,x});
while(!que.empty()){
auto [time,now] = que.top();
que.pop();
if(dist[now]<time){
continue;
}
for(auto [next,time2,k]:graph[now]){
ll start=time+(k-time%k)%k;
if(dist[next]>start+time2){
que.push({start+time2,next});
dist[next]=start+time2;
}
}
}
if(dist[y]>=1e17){
cout<<-1<<endl;
}
else{
cout<<dist[y]<<endl;
}
} |
#include <bits/stdc++.h>
#define fi first
#define se second
//#define ivorysi
#define enter putchar('\n')
#define space putchar(' ')
#define pii pair<int,int>
#define MOD 998244353
#define MAXN 100005
typedef long long int64;
using namespace std;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
int T,N,a[MAXN];
string nm[2] = {"First","Second"};
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);
}
sort(a + 1,a + N + 1);
int t = 0;t ^= (N & 1);
if(N % 2 == 0) {
bool f = 1;
for(int i = N ; i >= 1 ; i -= 2) {
if(a[i] != a[i - 1]) {f = 0;break;}
}
t ^= f;
}
cout << nm[t] << endl;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
read(T);
while(T--) {
Solve();
}
return 0;
}
| #include<bits/stdc++.h>
#define ll long long int
using namespace std;
void solve(){
ll n;
cin>>n;
vector<ll>v;
for(ll i=2; i*i<=n; i++){
for(int j=2; j<=50;j++){
ll x = pow(i,j);
if(x>n){
break;
}
else{
v.push_back(pow(i,j));
}
}
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
cout<<n-v.size()<<endl;
return;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("/home/shubham/Desktop/codes/input.txt", "r", stdin);
freopen("/home/shubham/Desktop/codes/output.txt", "w", stdout);
#endif
ll t;
// cin>>t;
t = 1;
while(t--){
solve();
}
return 0;
} |
#include <iostream>
using namespace std;
int main() {
int a,b;cin>>a>>b;
cout<<(a+b)/2<<" "<<(a-b)/2<<endl;
return 0;
} | #include <bits/stdc++.h>
#include <fstream>
using namespace std;
typedef long long ll;
double pi = 3.141592653589793238;
const ll LL_INF_BIG = 1e18 + 7;
const ll LL_INF = 1e9 + 7;
const int INF = 1e9 + 7;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
template <class T> void chmin(T& a, T b) {
if (a > b) a = b;
return;
}
template <class T> void chmax(T& a, T b) {
if (a < b) a = b;
return;
}
vector<pair<ll, ll>> prime_factorize(ll N) {
vector<pair<ll, ll>> res;
for (ll i = 2; i * i <= N; i++) {
if (N % i != 0) continue;
ll exp = 0;
while (N % i == 0) {
exp++;
N /= i;
}
res.push_back({i, exp});
}
if (N != 1) res.push_back({N, 1});
return res;
}
ll mod_pow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll modinv(ll a, ll m) {
ll b = m;
ll u = 1;
ll v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
const int MAX_COM = 10000000;
const int MOD = 1000000007;
ll fac[MAX_COM], finv[MAX_COM], inv[MAX_COM];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX_COM; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll COM(int n, int k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct UnionFind {
vector<int> parents, siz;
UnionFind(int N) : parents(N, -1), siz(N, 1) {}
int root(int x) {
if (parents.at(x) == -1) return x;
return parents.at(x) = root(parents.at(x));
}
bool unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry) return false;
if (siz.at(rx) > siz.at(ry)) swap(rx, ry);
parents.at(rx) = ry;
// Update the size only at root.
siz.at(ry) += siz.at(rx);
return true;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
// If the size is needed, we need to see it at root.
int size(int x) { return siz.at(root(x)); }
};
struct Edge {
ll to;
ll weight;
Edge(ll t, ll w) : to(t), weight(w) {}
};
// using Edge = pair<int, pair<int, int>>; // w, u, v
using Graph = vector<vector<Edge>>;
Graph G;
vector<Edge> edge;
ll N,K;
vector<ll> A,CNT;
ll answer;
vector<ll> answers;
void input() {
cin >> N >> K;
A.resize(N);
CNT.resize(N);
for (int i = 0; i < N; i++) cin >> A[i];
}
void solve() {
for (int i = 0; i < N; i++) CNT[A[i]]++;
for (int i = 0; i < N; i++) {
if(CNT[i] < K) {
ll dif = K - CNT[i];
answer += i * (dif);
K -= dif;
if (K == 0) break;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// ifstream in("input.txt");
// cin.rdbuf(in.rdbuf());
input();
solve();
// cout << fixed << setprecision(15);
cout << answer << "\n";
// for (auto answer : answers) cout << answer << "\n";
return 0;
}
|
# include<bits/stdc++.h>
using namespace std;
# define ll long long int
double amount(ll v,ll p)
{
return (v*p);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n; cin>>n;
double x ; cin>>x;
pair <int,int> pr[n];
for(ll i=0;i<n;i++)
{
ll v,p; cin>>v>>p;
pr[i]={v,p};
}
double sum=0;
ll i;
for( i=0;i<n;i++)
{
sum+=amount(pr[i].first,pr[i].second);
if(sum>(x*100))
{
cout<<i+1;
break;
}
}
if(sum<=x*100)
cout<<-1;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++i)
#define per(i, a, b) for (int i = (a) - 1, i##end = (b); i >= i##end; --i)
#define REP(i, a) rep(i, 0, a)
#define PER(i, a) per(i, a, 0)
namespace IO {
const int MAXIOSIZE = 1 << 24 | 1;
unsigned char buf[MAXIOSIZE], *p1, *p2;
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++)
template <typename T> void read(T& x) {
x = 0; char ch = gc; bool flg = false;
for (; ch < '0' || '9' < ch; ch = gc) if (ch == '-') flg |= true;
for (; '0' <= ch && ch <= '9'; ch = gc) x = x * 10 + ch - '0';
flg ? x = -x : 0;
}
template <typename T> void out(const T& x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); }
template <typename T> inline void write(const T& x, const char& ed = ' ') { if (x < 0) putchar('-'), out(-x); else out(x); putchar(ed); }
}
typedef long long ll;
const int MAXVAL = 50 + 10;
const int Prime[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
int n;
ll fans = LONG_LONG_MAX;
bool tag[MAXVAL];
vector<int> frac[MAXVAL];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
IO::read(n);
REP(i, n) {
ll x; IO::read(x);
for (ll p = 2; p <= x; ++p) if ((x % p) == 0) {
frac[i].push_back(p);
while ((x % p) == 0) x /= p;
}
}
REP(S, 1 << 15) {
ll ans = 1; int curtag[MAXVAL];
memset(curtag, 0, sizeof curtag);
REP(i, 15) if ((S >> i) & 1) ans *= Prime[i], curtag[Prime[i]] = true;
if (ans > fans) continue;
bool OK = true;
REP(i, n) {
bool ok = false;
for (int f : frac[i]) if (curtag[f]) {
ok = true;
break;
}
if (!ok) {
OK = false;
break;
}
}
if (OK) fans = ans;
}
IO::write(fans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef string str;
typedef long long ll;
typedef double db;
typedef pair<int,int> pi;
#define fi first
#define se second
#define int ll
#define all(x) begin(x), end(x)
typedef vector<int> vi;
typedef vector<str> vs;
#define pb push_back
#define rall(x) rbegin(x), rend(x)
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define sz(x) (int)x.size()
#define endl "\n"
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
void IO(){
#ifndef ONLINE_JUDGE
freopen("D:///docs///coding///c++///codes///input.txt", "r", stdin);
freopen("D:///docs///coding///c++///codes///output.txt", "w", stdout);
#endif
}
int32_t main(){
// IO();
boost;
int n,s,d; cin>>n>>s>>d;
FOR(i,0,n)
{
int a,b; cin>>a>>b;
if(a<s && b>d)
{
cout<<"Yes"<<endl;
return 0;
}
}
cout<<"No"<<endl;
return 0;
}
// check data type
// return function at recursions | #include <iostream>
using namespace std;
int main()
{
int n, s, d;
cin >> n >> s >> d;
bool ans = false;
for (int i = 0; i < n; i++)
{
int x, y;
cin >> x >> y;
if (x < s && y > d)
{
ans = true;
}
}
if (ans)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define LL long long
using namespace std;
template <typename T> void read(T &x){
x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
x *= f;
}
inline void write(int x){if (x > 9) write(x/10); putchar(x%10+'0'); }
const int N = 505;
inline void gg(){
cout << "No\n",exit(0);
}
int n;
LL c[N][N];
LL a[N],b[N];
int main(){
int i,j;
cin >> n;
for (i = 1; i <= n; ++i) for (j = 1; j <= n; ++j) cin >> c[i][j];
LL mn = c[1][1];
for (i = 1; i <= n; ++i) a[i] = c[i][1],mn = min(mn,a[i]);
for (i = 1; i <= n; ++i) a[i] -= mn;
for (i = 1; i <= n; ++i) b[i] = c[1][i] - a[1];
for (i = 1; i <= n; ++i) for (j = 1; j <= n; ++j) if (a[i] + b[j] != c[i][j]) gg();
for (i = 1; i <= n; ++i) if (a[i] < 0 || b[i] < 0) gg();
cout << "Yes\n";
for (i = 1; i <= n; ++i) cout << a[i] << (i<n?' ':'\n');
for (i = 1; i <= n; ++i) cout << b[i] << (i<n?' ':'\n');
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define range(a) a.begin(), a.end()
#define endl "\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define MP make_pair
using P = pair<int, int>;
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL<<60;
void chmin(long long &a, long long b) { if (a > b) a = b; }
void chmax(long long &a, long long b) { if (a < b) a = b; }
// @param m `1 <= m`
// @return x mod m
constexpr ll safe_mod(ll x, ll m) {
x %= m;
if (x < 0) x += m;
return x;
}
constexpr pair<ll, ll> inv_gcd(ll a, ll b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
ll s = b, t = a;
ll m0 = 0, m1 = 1;
while (t) {
ll u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
if (m0 < 0) m0 += b / s;
return {s, m0};
}
// (rem, mod)
pair<ll, ll> crt(const vector<ll>& r,
const vector<ll>& m) {
assert(r.size() == m.size());
int n = int(r.size());
// Contracts: 0 <= r0 < m0
ll r0 = 0, m0 = 1;
for (int i = 0; i < n; i++) {
assert(1 <= m[i]);
ll r1 = safe_mod(r[i], m[i]), m1 = m[i];
if (m0 < m1) {
swap(r0, r1);
swap(m0, m1);
}
if (m0 % m1 == 0) {
if (r0 % m1 != r1) return {0, 0};
continue;
}
// assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1)
// (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1));
// r2 % m0 = r0
// r2 % m1 = r1
// -> (r0 + x*m0) % m1 = r1
// -> x*u0*g % (u1*g) = (r1 - r0) (u0*g = m0, u1*g = m1)
// -> x = (r1 - r0) / g * inv(u0) (mod u1)
// im = inv(u0) (mod u1) (0 <= im < u1)
ll g, im;
tie(g, im) = inv_gcd(m0, m1);
ll u1 = (m1 / g);
// |r1 - r0| < (m0 + m1) <= lcm(m0, m1)
if ((r1 - r0) % g) return {0, 0};
// u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1)
ll x = (r1 - r0) / g % u1 * im % u1;
// |r0| + |m0 * x|
// < m0 + m0 * (u1 - 1)
// = m0 + m0 * m1 / g - m0
// = lcm(m0, m1)
r0 += x * m0;
m0 *= u1; // -> lcm(m0, m1)
if (r0 < 0) r0 += m0;
}
return {r0, m0};
}
void solve(){
ll x, y, p, q;
cin >> x >> y >> p >> q;
ll ans = INF;
FOR(i,x,x+y){
FOR(j,p,p+q){
auto re = crt({i, j}, {2 * (x + y), (p + q)});
if(re.first == 0 && re.second == 0){
continue;
}
ans = min(ans, re.first);
}
}
if(ans==INF){
cout << "infinity" << endl;
}else{
cout << ans << endl;
}
}
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--){
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
template<class T> void read(T &x){
int f=0; x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar()) f|=(ch=='-');
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
if(f) x=-x;
}
int n;
int main(){
read(n);
REP(i,n){
int A=i*2%n;
int B=(i*2+1)%n;
printf("%d %d\n",A+1,B+1);
}
return 0;
} | #include <bits/stdc++.h>
int n;
inline int highbit(int a){
for(int i = 10; ~i; --i)
if(a & (1 << i))
return 1 << i;
return 0;
}
int main(){
std::scanf("%d", &n);
for(int i = 1; i <= n; ++i){
int a = i * 2, b = i * 2 + 1;
while(a > n) a -= highbit(a);
while(b > n) b -= highbit(b);
if(!a) a = 1;
if(!b) b = 1;
std::printf("%d %d\n", a, b);
}
} |
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define lln long long int
#define ld long double
#define all(x) (x).begin(),(x).end()
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int MOD = 1e9 + 7;
int main(){
IOS;
double a, b;
cin >> a >> b;
cout << ((a - b) * 100) / a << endl;
return 0;
} | // g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const long double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
int dp[200200][17][2];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
double a,b;
cin>>a>>b;
cout<<shosu(9)<<100-100*b/a<<endl;
}
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <string>
#include <list>
#include <map>
#include <unordered_map>
using namespace std;
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
return modpow(a, mod - 2, mod);
}
void f(vector<vector<int>>& a,vector<vector<int>>& b,int N,int K){
for(int i=0;i<N-1;i++){
for(int j=i+1;j<N;j++){
int l=0;
for(int k=0;k<N;k++){
if(a[k][i]+a[k][j]>K){
break;
}
l+=1;
}
if(l==N){
b[i][j]=1;
b[j][i]=1;
}
}
}
}
void g(vector<vector<int>>& a,vector<vector<int>>& c,int N,int K){
for(int i=0;i<N-1;i++){
for(int j=i+1;j<N;j++){
int l=0;
for(int k=0;k<N;k++){
if(a[i][k]+a[j][k]>K){
break;
}
l+=1;
}
if(l==N){
c[i][j]=1;
c[j][i]=1;
}
}
}
}
int h(vector<vector<int>>& b,vector<int>& x,int N){
int M=1;
vector<int> a(N);
for(int i=0;i<N;i++){
a[i]=0;
}
stack<int> d;
for(int i=0;i<N;i++){
int j=0;
if(a[i]==0){
d.push(i);
a[i]=1;
}
while(!d.empty()){
int k=d.top();
d.pop();
j+=1;
for(int l=0;l<N;l++){
if(b[k][l]==1 && a[l]==0){
d.push(l);
a[l]=1;
}
}
}
if(j>1){
x.push_back(j);
M=max(M,j);
}
}
return M;
}
char jank(char a,char b){
if(a=='R' && b=='P'){
return b;
}else if(a=='P' && b=='S'){
return b;
}else if(a=='S' && b=='R'){
return b;
}else{
return a;
}
}
/*
sort(b.begin(),b.end(),[](const vector<int> &alpha,const vector<int> &beta){return alpha[0] < beta[0];});
第一成分で昇順
sort(r.begin(),r.end(),[](const vector<int> &alpha,const vector<int> &beta){return alpha[1] > beta[1];});
第二成分で降順
*/
long long int gcd(long long int a, long long int b)
{
if (a < b) {
a ^= b;
b ^= a;
a ^= b;
}
return b ? gcd(b, a % b) : a;
}
long long int lcm(long long int a, long long int b)
{
return a * b / gcd(a, b);
}
int main(){
long long int N,W;
cin>>N>>W;
cout<<N/W<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define fo(i,n) for(i = 0 ; i < n ; i++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define nl "\n"
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define PI 3.141592653589793238
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
typedef pair<lli, lli> plli;
typedef priority_queue<lli> pqb;
typedef priority_queue<lli, vector<lli>, greater<lli>> pqs;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
void GO_GO_GO() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void BYE_BYE() {cerr << "Time elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";}
bool primeArr[1000001] = {false};
lli fact[1000001];
lli lazy[1000001] = {0};
vector<lli> primes;
/****************************************************************************************/
int main() {
GO_GO_GO();
lli t = 1;
// cin >> t;
while (t--) {
lli i = 0;
string n;
lli k;
cin >> n >> k;
while (k--) {
string t1 = n;
sortall(t1);
string t2 = t1;
reverse(all(t1));
n = to_string(stoi(t1) - stoi(t2));
}
cout << n << nl;
}
BYE_BYE();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=(0);(i)<(int)(n);++(i))
using ll = long long;
using P = pair<int, int>;
using namespace std;
#define INF ((1<<30)-1)
#define LLINF (1LL<<60)
#define EPS (1e-10)
const ll mod = 1e9 + 7;
ll f1(ll n) {
if (n < 0) return 0;
ll res = n * (n + 1) / 2;
return res % mod;
}
ll f2(ll n) {
if (n < 0) return 0;
ll res = f1(n);
res %= mod;
res *= res;
res %= mod;
return res;
}
void solve() {
int n, a, b;
cin >> n >> a >> b;
ll x = ((f1(n - a - b + 1) * (n - b + 1) % mod) * (n - a + 1)) % mod;
ll y = f2(n - a - b + 1) % mod;
ll ans = (x*4) % mod - (y*4) % mod;
cout << (ans + mod) % mod << endl;
}
int main() {
int T;
cin >> T;
rep(t, T) {
solve();
}
}
| #include <iostream>
#include <algorithm>
using namespace std;
const long long waru = 1000000007;
int main()
{
long long t;
cin >> t;
long long ans[100000];
for (long long q = 0; q < t; q++) {
long long n, a, b;
cin >> n >> a >> b;
if (a < b) swap(a, b);
long long answer = 0;
long long m_answer = 0;
if (n < a + b) {
ans[q] = 0;
continue;
}
answer = ((n - a + 1) * (n - b + 1) * 4) % waru;
long long pk = ((n - a - b + 1) * (n - a - b + 2) / 2) % waru;
answer = (answer * pk) % waru;
long long f = ((n - a - b + 1) * (n - a - b + 2) / 2) % waru;
m_answer = (f * f * 4) % waru;
answer = (answer - m_answer + waru) % waru;
ans[q] = answer;
}
for (long long i = 0; i < t; i++) {
cout << ans[i] << endl;
}
} |
//#include <atcoder/mincostflow>
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
using namespace std;
typedef pair<long long,long long> ii;
typedef long long lint;
signed main(){
//freopen("i.txt","r",stdin);
ios_base::sync_with_stdio(false); cin.tie(0);
vector<int> v(4);
for(int i = 0;i < 4;i++) cin >> v[i];
sort(all(v));
do{
if(v[0] + v[1] == v[2] + v[3]){
cout << "Yes\n";
return 0;
}
if(v[0] == v[1] + v[2] + v[3]){
cout << "Yes\n";
return 0;
}
} while(next_permutation(all(v)));
cout << "No\n";
}
| #include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define f(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
#define pb push_back
using namespace std;
void test(){
string s;
cin>>s;
int a[3]={0};
int n = s.length();
int sum = 0;
f(i,n){
int x = s[i]-'0';
a[x%3]++;
sum = sum + x;
}
sum = sum%3;
if(sum==0)
cout<<"0\n";
else if(n==1)
cout<<"-1\n";
else if(sum==1){
if(a[1]>0){
cout<<"1\n";
}else{
if(a[0]>0 or a[2]>2){
cout<<"2\n";
}else
cout<<"-1\n";
}
}else{
if(a[2]>0){
cout<<"1\n";
}else {
if(a[0]>0 or a[1]>2){
cout<<"2\n";
}else
cout<<"-1\n";
}
}
}
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests=1;
// cin>>tests;
while(tests--){
test();
}
}
|
#include <bits/stdc++.h>
using namespace std;
//ofstream cout("output.out");
#define fast() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define mii map<int, int>
#define pii pair<int, int>
#define qii priority_que<int, vector<int>, greater<int>>
#define For(i, n) for(int i = 0; i < n; ++i)
#define For1(i, n) for(int i = 1; i <= n; ++i)
#define Forlr(i, l, r) for(int i = l; i <= r; ++i)
#define Forrl(i, r, l) for(int i = r; i >= l; --i)
#define o(a) { cout << #a << ":" << (a) <<" ";}
#define ov(a) {For(i, a.size()) cout << a[i] <<" ";}
#define ovv(a) {For(i, a.size()) {For(j, a[i].size()) cout << a[i][j] <<" ";cout <<"\n";}}
using ll = long long;
int n, m, k, q;
const ll INF = 1e18 + 5;
const int nax = 1e5 + 5;
const ll mod = 1e9 + 7;
void solve(){
cin >> n;
vl a(n);
For(i, n) cin >> a[i];
sort(a.begin(), a.end());
auto it = unique(a.begin(), a.end());
a.resize(distance(a.begin(), it));
//For(i, a.size()) cout << a[i] <<" ";
ll rs = 1ll;
ll pr = 0;
For(i, a.size()){
rs = rs * (a[i] - pr + 1ll) % mod;
pr = a[i];
}
//rs = rs * 2 ;
cout << rs <<"\n";
}
int main ()
{
fast();
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
| #include <bits/stdc++.h>
using namespace std;
const int N=1e6+5,mod=1e9+7;
int n,a[N];
int main() {
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
sort(a+1,a+1+n);
long long ans=1;
for(int i=n-1;~i;i--) ans*=(a[i+1]-a[i]+1),ans%=(mod);
cout<<ans;
}
|
#include<bits/stdc++.h>
struct em {
using type = std::pair<unsigned long, unsigned long>;
static type id(){ return {}; }
static type op(const type& a, const type& b){return {a.first + b.first, a.second + b.second};}
};
// from https://kazuma8128.hatenablog.com/entry/2018/11/29/093827
template <typename M> // Mは 型type, 単位元id(), 演算op() を持つモノイド
class dynamic_segment_tree {
using T = typename M::type;
struct node {
int pos;
T val, all;
node *l, *r;
node(int p, T v)
: pos(p), val(v), all(v), l(nullptr), r(nullptr) {}
};
private:
const int n;
node *root;
public:
dynamic_segment_tree(int n_) :
n(1 << (int)ceil(log2(n_))), root(nullptr) {}
void update(int p, T val) {
root = change(root, p, val, 0, n);
}
T find(int l, int r) {
return get(l, r, root, 0, n);
}
private:
T value(node *t) {
return t ? t->all : M::id();
}
T get(int l, int r, node* t, int lb, int ub) {
if (!t || ub <= l || r <= lb) return M::id();
if (l <= lb && ub <= r) return t->all;
int c = (lb + ub) / 2;
T res = get(l, r, t->l, lb, c);
if (l <= t->pos && t->pos < r) res = M::op(res, t->val);
return M::op(res, get(l, r, t->r, c, ub));
}
node *change(node* t, int p, T val, int lb, int ub) {
if (!t) return new node(p, val);
if (t->pos == p) {
t->val = val;
t->all = M::op(value(t->l), M::op(t->val, value(t->r)));
return t;
}
int c = (lb + ub) / 2;
if (p < c) {
if (p > t->pos) std::swap(p, t->pos), std::swap(val, t->val);
t->l = change(t->l, p, val, lb, c);
}
else {
if (p < t->pos) std::swap(p, t->pos), std::swap(val, t->val);
t->r = change(t->r, p, val, c, ub);
}
t->all = M::op(value(t->l), M::op(t->val, value(t->r)));
return t;
}
};
int main(){
using namespace std;
unsigned long N, M, Q;
cin >> N >> M >> Q;
vector<unsigned long> A(N), B(M);
dynamic_segment_tree<em> a(200000000), b(200000000);
a.update(0, {0, N});
b.update(0, {0, M});
const auto dec{[](const auto a, unsigned long x) -> pair<unsigned long, unsigned long> {
return {a.first - x, a.second - 1};
}};
const auto enc{[](const auto a, unsigned long x) -> pair<unsigned long, unsigned long> {
return {a.first + x, a.second + 1};
}};
const auto diff{[](auto& sgt, unsigned long X){
const auto [sum, cnt]{sgt.find(0, X)};
return cnt * X - sum;
}};
unsigned long ans{0};
for(unsigned long i{0}, t, x, y; i < Q; ++i){
cin >> t >> x >> y;
auto& v{t == 1 ? A[x - 1] : B[x - 1]};
auto& now{t == 1 ? a : b}, oppo{t == 1 ? b : a};
ans -= diff(oppo, v);
now.update(v, dec(now.find(v, v + 1), v));
swap(v, y);
now.update(v, enc(now.find(v, v + 1), v));
ans += diff(oppo, v);
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
#define mod2 998244353
#define pi 3.1415926535897932384626
#define endl '\n'
#define all(x) x.begin(), x.end()
#define deb(x) cout << #x << " : " << x << endl;
#define deb2(x, y) cout << #x << " : " << x << ", " << #y << " : " << y << endl;
#define lmax LLONG_MAX
#define lmin LLONG_MIN
/************** segment tree code starts ***************/
const int N = 2e5 + 100;
int tree[4 * N];
void build(int i, int s, int e, vector<int>& a) {
if(s == e) {
tree[i] = a[s];
return;
}
int mid = (s + e) / 2;
build(2 * i, s, mid, a);
build(2 * i + 1, mid + 1, e, a);
tree[i] = tree[2 * i] + tree[2 * i + 1];
}
void update(int i, int s, int e, int ind, int val, vector<int>& a) {
if(s == e) {
a[ind] = val;
tree[i] = val;
return;
}
int mid = (s + e) / 2;
if(ind <= mid)
update(2 * i, s, mid, ind, val, a);
else
update(2 * i + 1, mid + 1, e, ind, val, a);
tree[i] = tree[2 * i] + tree[2 * i + 1];
}
int query(int i, int s, int e, int l, int r) {
if(s > r or e < l) return 0;
if(s >= l and e <= r) return tree[i];
int mid = (s + e) / 2;
int left = query(2 * i, s, mid, l, r);
int right = query(2 * i + 1, mid + 1, e, l, r);
return left + right;
}
/************** segment tree code ends ***************/
void testcase() {
int r, c, x;
cin >> r >> c >> x;
if(x == 0) {
cout << r * c << endl;
return;
}
vector<int> a(r, c), b(c, r);
vector<vector<int>> p(x, vector<int>(2));
for(int i = 0; i < x; i++) {
int d, e;
cin >> d >> e;
d--, e--;
a[d] = min(a[d], e);
b[e] = min(b[e], d);
p[i][0] = e, p[i][1] = d;
}
sort(p.begin(), p.end());
vector<int> g(r + 2);
build(1, 0, r - 1, g);
set<int> s;
int ans = 0, row = 0;
for(int i = 0; i < r; i++) {
if(a[i] == 0) break;
ans += a[i];
row = i;
}
row++;
int j = 0;
for(int i = 0; i < c; i++) {
if(b[i] == 0) break;
while(j < x and p[j][0] == i) {
if(s.count(p[j][1]) == 0 and p[j][1] < row) {
update(1, 0, r - 1, p[j][1], 1, g);
s.insert(p[j][1]);
}
j++;
}
int val = query(1, 0, r - 1, 0, b[i] - 1);
ans += b[i];
ans -= (min(row, b[i]) - val);
}
cout << ans;
}
signed main() {
#ifdef SP
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(NULL);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++) {
// cout << "Case #" << i << ": ";
testcase();
cout << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define nl cout<<endl
#define pii pair<ll,ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define ff first
#define ss second
#define hell 1000000007
#define test4(x,y,z,a) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<" a is "<<a<<endl;
#define test3(x,y,z) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<endl;
#define test2(x,y) cout<<"x is "<<x<<" y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
#define N 300009
ll power(ll a,ll b,ll m)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%m;
b/=2;
a=(a*a)%m;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,m;cin>>n>>m;
if(n==1 and m==0) {
cout << "1 2"<<endl;
return 0;
}
if(m >= (n-1) ) {
cout<< -1<<endl;
return 0;
}
if(m == 0) {
ll curr=1;
rep(i,1,n+1) {
cout<<curr<<" "<<curr+1<<endl;
curr+=2;
}
}
else if(m > 0 ) {
cout << 1<< " "<< 1000000000<<endl;
ll diff = m + 1;
ll curr = 2;
rep(i,0,diff-1) {
cout<< curr <<" "<< curr+1<<endl;
curr += 2;
}
ll curr2 = 1e8;
ll more = 1 + diff - 1;
more = n- more;
rep(i,0,more) {
cout<< curr <<" "<<curr2<<endl;
curr++,curr2++;
}
}
else
cout<< -1 << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mk make_pair
int main(){
int n,m;
scanf("%d%d",&n,&m);
if(n==1&&m==0){
printf("%d %d\n",1,2);
return 0;
}
if(m<0||m==n||m==n-1){
printf("-1\n");
}
else if(m==0){
for (int i = 1; i <= n; ++i)
printf("%d %d\n", i, i + n);
}
else{
printf("%d %d\n",1,1000000);
int idx=2;
for(int i=1;i<=m+1;i++){
printf("%d %d\n",idx,idx+1);
idx+=2;
}
idx=1000010;
for(int i=m+2;i<n;i++){
printf("%d %d\n",idx,idx+1);
idx+=2;
}
}
} |
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <numeric>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\n';
#define PRINTVEC(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i] << ' '; std::cout << '\n';
#define PRINTPAIR(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i].first << ' ' << x[i].second << '\n'; std::cout << '\n';
#define PRINTSET(x) for (auto it = x.begin(); it != x.end(); it++) { std::cout << *it << ' '; } std::cout << '\n';
#define io std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
const int INF = 1<<29;
const double PI = acos(-1.0);
typedef long long ll;
const std::vector<int> GRID = {0,1,0,-1,0}; // for(k < 4) r=x+GRID[k],c=y+GRID[k+1]; if(0<=r&&r<m&&0<=c&&c<n) do smthg;
using namespace std;
int main()
{
io;
#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
ll s,p; cin >> s >> p;
for (int i = 0; i <= sqrt(p); i++) {
if (i*(s-i) == p) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
#ifndef ONLINE_JUDGE
std::cout << '\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define rep2(i,n) for (int i=1; i<(n); i++)
using ll = long long;
using P = pair<int,int>;
int main() {
ll s,p; cin >> s >> p;
rep2(i,1000100) {
if(i*(s-i) == p) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
} |
#include <bits/stdc++.h>
#include <string>
#include <sstream>
using namespace std;
int main(){
long n;
cin >> n;
string s;
long seven = 0;
stringstream ss;
std::string o;
for(int i = 1; i <= n; i++){
ss.str("");
ss << std::dec << i;
s = ss.str();
ss.str("");
ss << std::oct << i;
o = ss.str();
for(int j = 0; j < 6; j++){
if(s[j] == '7' || o[j] == '7'){
seven++;
break;
}
}
}
cout << n - seven << endl;
} | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/rope>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;
#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) begin(x),end(x)
#define Fixed fixed << setprecision(12)
//#define int int_fast64_t
using pii = pair<int,int>;
constexpr int32_t INF = 0x3f3f3f3f;
constexpr int_fast64_t LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9+7;
constexpr int mod2 = 998244353;
template <class Func>
class FixPoint : Func {
public:
explicit constexpr FixPoint(Func&& f) noexcept : Func(forward<Func>(f)) {}
template <class... Args>
constexpr decltype(auto) operator()(Args&&... args) const {
return Func::operator()(*this, std::forward<Args>(args)...);
}
};
template <class Func>
static inline constexpr decltype(auto) makeFixPoint(Func&& f) noexcept {
return FixPoint<Func>{forward<Func>(f)};
}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
template <class T> using max_heap = priority_queue<T>;
template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class A, class B> using umap = unordered_map<A,B>;
template <class T> using Max_Heap = __gnu_pbds::priority_queue<T, less<T>, __gnu_pbds::rc_binomial_heap_tag>;
template <class T> using Min_Heap = __gnu_pbds::priority_queue<T, greater<T>, __gnu_pbds::rc_binomial_heap_tag>;
template <class T> using Set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
template <class K, class V> using Umap = __gnu_pbds::gp_hash_table<K, V>;
template <class T> using Rope = __gnu_cxx::rope<T>;
using Trie = __gnu_pbds::trie<string, __gnu_pbds::null_type, __gnu_pbds::trie_string_access_traits<>, __gnu_pbds::pat_trie_tag, __gnu_pbds::trie_prefix_search_node_update>;
inline int square(int a){ return a * a;}
inline int updiv(int a,int b){ return (a + b - 1) / b; }
constexpr int dx[] = {1,0,-1,0,1,1,-1,-1};
constexpr int dy[] = {0,-1,0,1,1,-1,-1,1};
signed main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout.setf(ios_base::fixed);
cout.precision(12);
int n;
cin >> n;
int res = 0;
for(int i = 1; i <= n; ++i){
int tmp = i;
while(tmp){
if(tmp % 10 == 7){
goto ng;
}
tmp /= 10;
}
tmp = i;
while(tmp){
if(tmp % 8 == 7){
goto ng;
}
tmp /= 8;
}
++res;
ng:;
}
cout << res << '\n';
return 0;
} |
#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 998244353
using namespace std;
using ll = long long;
template<int M>
class ModInt {
public:
ll value;
constexpr ModInt(ll v = 0) { value = v % M; if (value < 0) value += M; }
constexpr ModInt pow(ll n) const { if (!n) return 1; ModInt a = pow(n >> 1); a *= a; if (n & 1) a *= *this; return a; }
constexpr ModInt inv() const { return this->pow(M - 2); }
constexpr ModInt operator + (const ModInt &rhs) const { return ModInt(*this) += rhs; };
constexpr ModInt operator - (const ModInt &rhs) const { return ModInt(*this) -= rhs; };
constexpr ModInt operator * (const ModInt &rhs) const { return ModInt(*this) *= rhs; };
constexpr ModInt operator / (const ModInt &rhs) const { return ModInt(*this) /= rhs; };
constexpr ModInt &operator += (const ModInt &rhs) { value += rhs.value; if (value >= M) value -= M; return *this; };
constexpr ModInt &operator -= (const ModInt &rhs) { value -= rhs.value; if (value < 0) value += M; return *this; };
constexpr ModInt &operator *= (const ModInt &rhs) { value = value * rhs.value % M; return *this; };
constexpr ModInt &operator /= (const ModInt &rhs) { return (*this) *= rhs.inv(); };
constexpr bool operator == (const ModInt &rhs) { return value == rhs.value; }
constexpr bool operator != (const ModInt &rhs) { return value != rhs.value; }
friend ostream &operator << (ostream &os, const ModInt &rhs) { os << rhs.value; return os; }
};
using mint = ModInt<MOD>;
int main() {
int H, W; cin >> H >> W;
vector<string> S(H);
REP(i, 0, H) cin >> S[i];
auto diag = [&](int h, int w) -> int {
int c = 0;
while (h < H && w >= 0) {
int nc = 0;
if (S[h][w] == 'R') nc = 1;
else if (S[h][w] == 'B') nc = 2;
if (c && nc && c != nc) return -1;
if (!c) c = nc;
h++; w--;
}
return c;
};
int cnt = 0;
REP(i, 0, W) {
int c = diag(0, i);
if (c == -1) {
cout << 0 << endl;
return 0;
} else if (c == 0) {
cnt++;
}
}
REP(i, 1, H) {
int c = diag(i, W - 1);
if (c == -1) {
cout << 0 << endl;
return 0;
} else if (c == 0) {
cnt++;
}
}
cout << mint(2).pow(cnt) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define BE(x) x.begin(), x.end()
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
for (auto &x : s) cin >> x;
vector<bitset<2>> cnt;
for (int j = 0; j < w; j++) {
bitset<2> bs;
int jj = j;
for (int i = 0; jj >= 0 && i < h; i++, jj--) {
if (s[i][jj] == 'R') bs[0] = true;
else if (s[i][jj] == 'B') bs[1] = true;
}
cnt.push_back(bs);
}
for (int i = 1; i < h; i++) {
bitset<2> bs;
int ii = i;
for (int j = w-1; ii < h && j >= 0; ii++, j--) {
if (s[ii][j] == 'R') bs[0] = true;
else if (s[ii][j] == 'B') bs[1] = true;
}
cnt.push_back(bs);
}
ll ans = 1;
for (auto &x : cnt) {
if (x[0] && x[1]) {
cout << 0 << endl;
return 0;
}
else if (!(x[0] || x[1])) {
ans = ans * 2LL % 998244353LL;
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
/*-------------------------------------------*/
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
string s;
cin >> s;
cout << (s[0] == s[1] && s[0] == s[2] ? "Won\n" : "Lost\n");
return 0;
} | using namespace std;
#include<string>
#include<cmath>
#include<list>
#include <map>
#include <unordered_map>
#include <set>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ml map<ll,ll>
#define pb push_back
#define mp make_pair
#define ppl pair<ll,ll>
#define F first
#define S second
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define fr(i,a,b) for(ll i=a;i<b;i++)
#define ffr(i,a,b) for(ll i=a;i>=b;i--)
#include<bits/stdc++.h>
#include<vector>
#define vl vector<ll>
#define PI 3.141592654
const ll N = 1e9 + 7;
#include<iterator>
#define EPSILON numeric_limits<double>::epsilon()
// vl b;
// vector<vl> v;
// ll n, m;
// vl link, val;
// vector<char>a;
// vector<bool> visited;
// void dfs(ll pnt, ll lin) {
// link[pnt] = lin;
// visited[pnt] = true;
// fr(i, 0, v[pnt].size()) {
// if (!visited[v[pnt][i]])dfs(v[pnt][i], lin);
// }
// }
// vl pr(N, 0);
// void precompute() {
// fr(i, 2, N + 1) {
// if (pr[i] == 0)
// for (ll j = i * i; j <= N; j += i)pr[j] = 1;
// }
// }
// void prime() {
// pr[1] = 1;
// fr(i, 2, N + 1)pr[i] = i;
// fr(i, 4, N + 1) {pr[i] = 2; i++;}
// fr(i, 3, N + 1) {
// if (pr[i] == i) {
// for (ll j = i * i; j <= N; j += i) {if (pr[j] == j)pr[j] = i;}
// }
// }
// }
ll gcd(ll a, ll b) {
if (a == b)return a;
if (a % b == 0)return b;
if (b % a == 0)return a;
if (b > a)return gcd(a, b % a);
if (a > b)return gcd(a % b, b);
return 0;
}
// ll fact(ll n) {
// ll res = 1;
// fr (i, 2, n + 1) res = (res * i) % N;
// return res;
// }
// ll nCr(ll n, ll r) {
// return fact(n) / (fact(r) * fact(n - r));
// }
void solve() {
char a, b, c; cin >> a >> b >> c;
if (a == b && a == c)cout << "Won";
else cout << "Lost";
}
int main() {
fio;
#ifndef ONLINE_JUDGE
// for getting input
freopen("input.txt", "r", stdin);
//for writing output
freopen("jout.txt", "w", stdout);
#endif
// prime();
// precompute();
ll t;
if (1)
t = 1;
else
cin >> t;
while (t--) {
//cout << "Case #" << h << ": ";
solve();
//h++;
cout << '\n';
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a,b;
cin >> a >> b;
cout << (a+b)/2 << endl;
cout << (a-b)/2 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vld vector<ld>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vvld vector<vector<ld>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
ll inf = 1e18;
ll mod = 1e9+7;
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll pwr(ll x, ll y)
{
ll res = 1;
x = x ;
while (y > 0)
{
if (y & 1)
res = (res * x) ;
y = y >> 1;
x = (x * x) ;
}
return res;
}
ll modInverse(ll n, ll p)
{
return power(n, p - 2, p);
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t = 1;
while(t-->0)
{
int a, b;
cin>>a>>b;
d2((a+b)/2,(a-b)/2);
}
} |
Subsets and Splits