code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include<iostream>
#include<cstring>
#include<set>
using namespace std;
int n;
string ss;
set<string> s;
int main()
{
cin>>n;
for(int i=0;i<n;i++){
cin>>ss;
if(ss[0]=='!'){
string t=ss;
t.erase(t.begin());
if(s.count(t)){
cout<<t;
return 0;
}
}
else {
string t="!"+ss;
if(s.count(t))
{
cout<<ss<<endl;
return 0;
}
}
s.insert(ss);
}
cout<<"satisfiable"<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)
void solve() {
int N;
cin >> N;
map<string, int> mp;
REP(i, N) {
string t;
cin >> t;
mp[t]++;
}
for (auto &x : mp) {
if (x.first[0] != '!') {
if (mp["!" + x.first] > 0) {
cout << x.first << endl;
return;
}
}
}
cout << "satisfiable\n";
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main () {
ll p, q, r;
ll inf = 1e8 + 7;
inf = inf * inf;
p = -inf;
q = inf;
r = 0;
int N;
cin >> N;
for (int i = 0; i < N; i ++) {
ll a, t;
cin >> a >> t;
ll al, bt, gm;
if (t == 1) {
gm = a;
al = -inf;
bt = inf;
}
if (t == 2) {
al = a;
bt = inf;
gm = 0;
}
if (t == 3) {
bt = a;
al = -inf;
gm = 0;
}
ll nep = max(al, min(p + gm, bt));
ll neq = min(q + gm, bt);
ll ner = r + gm;
p = nep;
q = neq;
r = ner;
}
int Q;
cin >> Q;
for (int i = 0; i < Q; i ++) {
ll x;
cin >> x;
cout << max(p, min(x + r, q)) << endl;
}
} | #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PRECISION cout << fixed << setprecision(20);
using namespace std;
const int mod = 998244353;
struct Mint {
ll v; explicit operator ll() const { return v; }
Mint() { v = 0; }
Mint(ll _v) {
v = (-mod < _v && _v < mod) ? _v : _v % mod;
if (v < 0) v += mod;
}
friend bool operator==(const Mint& a, const Mint& b) { return a.v == b.v; }
friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
friend bool operator<(const Mint& a, const Mint& b) { return a.v < b.v; }
Mint& operator+=(const Mint& m) { if ((v += m.v) >= mod) v -= mod; return *this; }
Mint& operator-=(const Mint& m) { if ((v -= m.v) < 0) v += mod; return *this; }
Mint& operator*=(const Mint& m) { v = v*m.v%mod; return *this; }
Mint& operator/=(const Mint& m) { return (*this) *= inv(m); }
friend Mint pow(Mint a, ll p) {
Mint ans = 1; assert(p >= 0);
for (; p; p /= 2, a *= a) if (p & 1) ans *= a;
return ans;
}
friend Mint inv(const Mint& a) { assert(a.v != 0); return pow(a, mod-2); }
Mint operator-() const { return Mint(-v); }
Mint& operator++() { return *this += 1; }
Mint& operator--() { return *this -= 1; }
Mint operator++(int) { Mint temp; temp.v = v++; return temp; }
Mint operator--(int) { Mint temp; temp.v = v--; return temp; }
friend Mint operator+(Mint a, const Mint& b) { return a += b; }
friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
friend ostream& operator<<(ostream& os, const Mint& m) {
os << m.v; return os;
}
friend istream& operator>>(istream& is, Mint& m) {
ll x; is >> x;
m.v = x;
return is;
}
};
const int N = 200000 + 100;
Mint fact[N];
int ff[N];
void pre() {
fact[0] = Mint(1);
for (int i = 1; i < N; i++) fact[i] = Mint(i) * fact[i - 1];
for (int i = 2; i < N; i++) {
if (!ff[i]) {
for (int j = i; j < N; j += i) {
if (!ff[j]) ff[j] = i;
}
}
}
}
Mint ncr(int n, int r) {
if (n < r || n < 0 || r < 0) return 0;
return fact[n] / (fact[n - r] * fact[r]);
}
void solve() {
int n, m;
cin >> n >> m;
Mint ans = 0, ans2 = 0;
// vector<Mint> dp(m + 1, 1);
// for (int itr = 2; itr <= n; itr++) {
// vector<Mint> ndp(m + 1, 0);
// for (int i = 1; i <= m; i++) {
// for (int j = i; j <= m; j += i) {
// ndp[j] += dp[i];
// }
// }
// dp.swap(ndp);
// }
for (ll i = 1; i <= m; i++) {
ll x = i;
Mint curr = 1;
while (x > 1) {
int cnt = 0;
int f = ff[x];
while (x % f == 0) {
x /= f;
cnt++;
}
curr *= ncr(cnt + n - 1, n - 1);
}
ans += curr;
}
cout << ans << "\n";
}
int main() {
FASTIO;
PRECISION;
pre();
int t = 1;
// cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
} |
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> II;
typedef pair<LL, II> III;
typedef priority_queue<III, vector<III>, greater<>> Queue;
static const LL INF = 1LL << 60;
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; }
bool solve(long long N, std::vector<long long> &a, std::vector<long long> &b, std::vector<long long> &p) {
map<int, int> m;
vector<int> seq(N);
iota(seq.begin(), seq.end(), 0);
sort(seq.begin(), seq.end(), [&](int lhs, int rhs) {
return a[lhs] < a[rhs];
});
for (int i = 0; i < N; ++i) {
if (p[i] != i && a[i] <= b[p[i]]) {
return false;
}
m[p[i]] = i; // 荷物p[i]はiが持っている
}
vector<II> ans;
for (int i = 0; i < N; ++i) {
int j = seq[i];
if (m[j] == j) continue;
int target = m[j];
ans.emplace_back(II(j + 1, target + 1));
m[p[j]] = target;
m[j] = j;
swap(p[j], p[target]);
}
cout << ans.size() << endl;
for (auto kv : ans) {
cout << kv.first << " " << kv.second << endl;
}
return true;
}
int main() {
long long N;
std::cin >> N;
std::vector<long long> a(N), b(N), p(N);
for (int i = 0; i < N; i++) {
std::cin >> a[i];
}
for (int i = 0; i < N; i++) {
std::cin >> b[i];
}
for (int i = 0; i < N; i++) {
std::cin >> p[i]; p[i]--;
}
if (!solve(N, a, b, p)) {
cout << -1 << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b)for(ll i=a;i<=b;i++)
#define loopr(i,a,b)for(ll i=b;i>=a;i--)
#define vll vector<ll>
#define vvl vector<vll>
#define pii pair<ll,ll>
#define F first
#define S second
#define endl "\n"
#define M 1000000007
ll max(ll a,ll b){if (a>b) return a; else return b;}
ll min(ll a,ll b){if(a<b) return a; return b;}
ll po(ll x, ll y){
if(y==0)
return 1;
if(y%2){
return ((x)*(po(x,y/2)%M)*(po(x,y/2)%M) + M)%M;
}
return ((po(x,y/2)%M)*(po(x,y/2)%M) + M)%M;
}
void seive(bool prime[]){
//bool prime[100002];
for (ll p=2; p*p<=1000001; p++)
{
if (prime[p])
{
for (ll i=p*p; i<=1000001; i+=p)
prime[i] = false;
}
}
}
ll powermod(ll x,ll y,ll p)
{ ll res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{ if (y & 1)
res = (res*x) % p;
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
ll gcd(ll x, ll y){
if (y == 0)
return x;
else
return gcd(y, x % y);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t=1;
//cin>>t;
while(t--){
ll n; cin>>n;
vll a(n),b(n);
vll mx(n);
fo(i,n){
cin>>a[i];
if(i==0)
mx[i]=a[i];
else
mx[i]=max(a[i],mx[i-1]);
}
fo(i,n)
cin>>b[i];
vll c(n);
c[0]=a[0]*b[0];
for(ll i=1; i<n; i++){
c[i]=max(c[i-1],b[i]*mx[i]);
}
fo(i,n)
cout<<c[i]<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
const ll MAX = 1000000000000000000LL;
const ll md = 998244353;
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL)
ll pw(ll a, ll n)
{
ll result = 1;
while (n)
{
if (n&1) result = (result*a)%md;
a = (a*a)%md;
n >>= 1;
}
return result;
}
vector<int> primes;
void sieve(int n)
{
bool *isp = new bool[n+1];
for(int i=0; i<=n; ++i)
isp[i] = i>1;
for(int i=2; i*i <= n; ++i)
{
if (isp[i])
{
for(int j=i*i; j<=n; j+=i)
{
isp[j] = false;
}
}
}
for(int i=0; i<=n; ++i)
{
if (isp[i])
{
primes.push_back(i);
}
}
}
inline ll ad(ll a, ll b, ll mod = md) {
return (a%mod + b%mod + mod)%mod;
}
inline ll ml(ll a, ll b, ll mod = md) {
return ((a%mod) * (b%mod))%mod;
}
vl fac(2,1), ifac(2,1);
ll C(ll n, ll r, ll mod = md) {
if(n < r) return 0;
return ml(fac[n], ml(ifac[r], ifac[n-r], mod), mod);
}
void cachefact(ll lim, ll mod = md) {
for(int i = 2; i <= lim; i++) {
ifac.push_back((ifac[mod%i] * (mod - mod/i) % mod)%mod);
}
for(ll i = 2; i <= lim; i++) {
fac.push_back((fac[i-1] * i)%mod);
ifac[i] = (ifac[i-1] * ifac[i])%mod;
}
}
int main()
{
FAST_IO;
int n, m;
cin >> n >> m;
cachefact(m+n);
sieve(sqrt(m)+2);
ll result = 0;
int ps = primes.size();
for (int i=1; i<=m; ++i)
{
ll I = i, run = 1;
for(int j=0; j < ps && primes[j]*primes[j] <= I; ++j)
{
int e=0;
while (I%primes[j] == 0)
{
I /= primes[j];
e++;
}
run = ml(run, C(n+e-1, e));
}
if (I > 1) run = ml(run, n);
result = ad(result, run);
}
cout << result << "\n";
return 0;
} | // Problem: D - Powers
// Contest: AtCoder - AtCoder Regular Contest 106
// URL: https://atcoder.jp/contests/arc106/tasks/arc106_d
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i,x,n) for(int i = x;i <= n;++i)
const int N = 2e5+7,M = 305,MOD = 998244353;
ll fx[M],fc[M],fsa[M],fsb[M];
int a[N],b[N];
ll fa[N][M],fb[N][M];
ll qmul(ll a, ll b, ll P)
{
ll L = a * (b >> 25LL) % P * (1LL << 25) % P;
ll R = a * (b & ((1LL << 25) - 1)) % P;
return (L + R) % P;
}
ll qpow(ll a,ll b,ll p)
{
ll res = 1 % p;
while(b)
{
if(b & 1) res = qmul(res,a,p);
a = qmul(a,a,p);
b >>= 1;
}
return res;
}
ll C[M][M];
int main()
{
int n,k,rev2 = qpow(2,MOD - 2,MOD);scanf("%d%d",&n,&k);
forn(i,1,n) scanf("%d",&a[i]),b[i] = 2 * a[i];
C[0][0] = 1;
forn(i,1,k) forn(j,0,i) C[i][j] = (!j) ? 1 : (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
fsa[0] = n,fsb[0] = n;
forn(i,1,n)
{
ll _a = 1,_b = 1;
forn(j,1,k)
{
_a = _a * a[i] % MOD,_b = _b * b[i] % MOD;
fsa[j] = (fsa[j] + _a) % MOD,fsb[j] = (fsb[j] + _b) % MOD;
}
}
forn(x,1,k)
{
ll lf = 0;
forn(i,0,k)
{
ll t = fsa[i] * fsa[x - i] % MOD;
t = t * C[x][i] % MOD;
lf = (lf + t) % MOD;
}
// cout << lf << endl;
lf = ((lf - fsb[x]) % MOD + MOD) % MOD;
printf("%lld\n",rev2 * lf % MOD);
}
return 0;
}
|
#pragma comment(linker, "/STACK:128777216")
#pragma GCC optimize("fast-maths")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <bitset>
#include <stack>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <climits>
#include <ctime>
#include <random>
#include <complex>
#include <functional>
#ifdef ORSET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using ordered_set = tree<T, null_type, std::less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
#endif
using namespace std;
#define forx(_name, _from, _to, _value) for (int _name = _from; _name < _to; _name += _value)
#define rforx(_name, _from, _to, _value) for (int _name = _from; _name > _to; _name -= _value)
#define all(_STL_NAME) _STL_NAME.begin(), _STL_NAME.end()
#define rall(_STL_NAME) _STL_NAME.rbegin(), _STL_NAME.rend()
#define mp(_FIRST,_SECOND) make_pair(_FIRST, _SECOND)
typedef long long ll;
typedef unsigned long long llu;
typedef long double ld;
const ld eps = 1e-7;
mt19937_64 rndm;
void start() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout.precision(8); cout.setf(ios::fixed);
string FILENAME = "\-__-/";
rndm.seed(time(0));
#ifdef _DEBUG
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#else
//freopen((FILENAME + ".in").c_str(), "rt", stdin);
//freopen((FILENAME + ".out").c_str(), "wt", stdout);
#endif
}
#ifdef NEEDFFAST
inline int FastIn() {
int val = 0;
char c = ' ';
while (!isdigit(c))
c = _getchar_nolock();
while (isdigit(c)) {
val *= 10;
val += c - '0';
c = _getchar_nolock();
}
return val;
}
inline void FastOut(int val) {
bool fst = true;
const int degree = 1000000000;
//const ll degree = 10000000000000000ll;
for (int i = degree; i > 0; i /= 10) {
int dl = val / i;
if (dl || !fst) {
_putchar_nolock('0' + dl);
fst = false;
}
val %= i;
}
if (fst)
_putchar_nolock('0');
}
#endif
const ll mod = 1000000007;
ll binpow(ll val, ll deg) {
ll res = 1;
ll dg = val;
while (deg) {
if (deg & 1) res = (res * dg) % mod;
deg >>= 1;
dg = (dg * dg) % mod;
}
return res;
}
int main() {
start();
/* (P - 1) * (P - 2) ^ (N - 1) */
ll n, p;
cin >> n >> p;
cout << (p - 1) * binpow(p - 2, n - 1) % mod;
#ifdef _DEBUG
cout << "\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << endl;
#endif
return 0;
} | #pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h>
#include <cmath>
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double EPS = 1e-10;
constexpr ll mod = 1000000007;
// constexpr ll mod = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const char eol = '\n';
// --------------------------------------------------------------------------
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n,p;
cin >> n >> p;
ll ans = p-1;
n--;
ll sum = p-2;
for(ll k=0; k<40; k++){
if(n & (1LL<<k)){
ans *= sum;
ans %= mod;
}
sum = sum*sum;
sum %= mod;
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(v) (v).size()
#define all(v) (v).begin(),(v).end()
#define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Time (double)clock()/CLOCKS_PER_SEC
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pi;
template <class T> void chmax(T &x,T y) {x=x>y?x:y;return;}
template <class T> void chmin(T &x,T y) {x=x<y?x:y;return;}
template <class T> T sqr(T x) {return x*x;}
template <class T> T gcd(T x,T y) {return y==0?x:gcd(y,x%y);}
template <class T> T lcm(T x,T y) {return x/gcd(x,y)*y;}
const int inf=1000000000,mod=1000000007;
const ll infll=1000000000000000000ll,modll=ll(mod);
const db maxdb=db(infll),eps=1e-14,PI=acos(-1.0);
//#define mydef
//#define LOCAL
#ifdef mydef
#define foreach(v,it) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#endif
#define debug
#define putnum(x) cout<<#x<<": "<<x<<endl
#define putvec(v) cout<<#v<<": ";REP(i,0,sz(v)) cout<<v[i]<<" ";cout<<endl
template <class T> inline void read(T &x){
T w=1;x=0;char c=getchar();
while(c<'0'||c>'9') {if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
x*=w;
}
template <class T> inline void write(T x) {
if(x<0) putchar('-'),x=~x+1;
if(x) write(x/10),putchar(x%10+'0');
}
const int maxn=700005;
int n,ans;
int a[maxn],l[maxn],r[maxn];
set<pi,greater<pi> > num;
void Get(int f,int *arr) {
num.clear();
for(int i=(f==1?1:n);i&&i<=n;i+=f) {
while(!num.empty()&&(*num.begin()).fi>a[i]) {
arr[(*num.begin()).se]=f==1?i-1:i+1;
num.erase(num.begin());
}
num.insert(mp(a[i],i));
}
while(!num.empty()) {
arr[(*num.begin()).se]=(f==1?n:1);
num.erase(num.begin());
}
// if(f==-1) cout<<arr[2]<<endl;
}
int main() {
#ifdef LOCAL
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
Get(1,r);
Get(-1,l);
for(int i=1;i<=n;i++)
ans=max(ans,(r[i]-l[i]+1)*a[i]);
printf("%d\n",ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N, ret = 0; cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
int m = 0;
for (int i = 0; i < N; i++) {
m = A[i];
for (int j = i; j < N; j++) {
m = min(m, A[j]);
ret = max(ret, m * (j - i + 1));
}
}
cout << ret << endl;
return 0;
} |
#include <bits/stdc++.h>
//#include <atcoder/all>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
template <class T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template <typename T>
T mypow(T x, T n, const T &p = -1)
{ //x^nをmodで割った余り
T ret = 1;
while (n > 0)
{
if (n & 1)
{
if (p != -1)
ret = (ret * x) % p;
else
ret *= x;
}
if (p != -1)
x = (x * x) % p;
else
x *= x;
n >>= 1;
}
return ret;
}
ll gcd(ll n, ll m)
{
ll tmp;
while (m != 0)
{
tmp = n % m;
n = m;
m = tmp;
}
return n;
}
ll lcm(ll n, ll m)
{
return abs(n) / gcd(n, m) * abs(m); //gl=xy
}
using namespace std;
//using namespace atcoder;
void solve()
{
int n;
cin>>n;
string s,t;
cin>>s>>t;
ll ans=0;
int pos=0;
rep(i,n){
chmax(pos,i+1);
if(s[i]=='1'&&t[i]!='1'){
for(;pos<n;++pos){
if(s[pos]=='1'){
s[i]='0';
s[pos]='0';
ans+=pos-i;
break;
}
}
}
else if(s[i]!='1'&&t[i]=='1'){
for(;pos<n;++pos){
if(s[pos]=='1'){
s[i]='1';
s[pos]='0';
ans+=pos-i;
break;
}
}
}
}
if(s==t){
cout<<ans;
}
else{
cout<<-1;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
solve();
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, j, k) for(int i = j; i < k; i++)
#define print(x) cout << x
#define newline cout << endl
#define space cout << ' '
#define INF 1000000000007
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
int main(){
int h,w,x,y;
cin >> h >>w >>x>>y;
vector<string> s(h);
rep(i,h) cin >> s[i];
int ans=1;
for(int i=x;i<h;i++){
if(s[i][y-1]=='#') break;
ans++;
}
for(int i=x-2;i>-1;i--){
if(s[i][y-1]=='#') break;
ans++;
}
for(int i=y;i<w;i++){
if(s[x-1][i]=='#') break;
ans++;
}
for(int i=y-2;i>-1;i--){
if(s[x-1][i]=='#') break;
ans++;
}
print(ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
pair<int,int> points[n];
for(int i=0;i<n;i++){
cin>>points[i].first>>points[i].second;
}
int freq=0;
for(int i=0;i<n;i++){
int j=n-1;
while(j>i){
float a=(points[j].second)-(points[i].second);
float b=(points[j].first)-(points[i].first);
float s=a/b;
if(s<=1&&s>=-1){freq++;}
j--;}}
cout<<freq;
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)
int main() {
int n;
cin >> n;
ll ans = 0;
rep(i, n) {
int a, b;
cin >> a >> b;
ans += (ll)(a + b) * (b - a + 1) / 2;
}
cout << ans << endl;
return 0;
}
|
#ifdef LOCAL
//#define _GLIBCXX_DEBUG
#endif
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<char> Vc;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
typedef vector<vector<char>> VVc;
typedef vector<vector<vector<ll>>> VVV;
typedef vector<vector<vector<vector<ll>>>> VVVV;
#define endl '\n'
#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF=1e18+18;
const ll MOD=1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a,b)
#define Each(a,b) for(auto &a :b)
#define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i)
#ifdef LOCAL
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;}
#define dbgset(st) cerr << #st << ":"<<endl; for (auto i = st.begin(); i != st.end(); ++i) { cerr << *i <<" ";}cerr<<endl;
#define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl;
#else
#define dbg(...)
#define dbgmap(...)
#define dbgset(...)
#define dbgarr(...)
#define dbgdp(...)
#endif
#define out(a) cout<<a<<endl
#define out2(a,b) cout<<a<<" "<<b<<endl
#define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se 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; }
template<typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,v.size())i>>v[j];return i;}
// vector
template<typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
int len=v.size();
for(int i=0; i<len; ++i) {
s<<v[i];
if(i<len-1) s<<" ";
}
return s;
}
// 2 dimentional vector
template<typename T>
ostream &operator<<(ostream &s, const vector<vector<T> > &vv) {
s<<endl;
int len=vv.size();
for(int i=0; i<len; ++i) {
s<<vv[i]<<endl;
}
return s;
}
int solve(){
ll x,y;
cin>>x>>y;
Yes(abs(x-y)<3);
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout<<std::setprecision(10);
// ll T;
// cin>>T;
// while(T--)
solve();
}
| #include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<iomanip>
#include<vector>
#include<queue>
//#include<atcoder/segtree>
#define rep(i, n) for(int i=0; i<n; i++)
using namespace std;
typedef long long ll;
using P = pair<int,int>;
using T = tuple<int,int,int>;
const int INF = 1e9;
//using namespace atcoder;
int main(){
int h, w; cin >> h >> w;
vector<vector<P> > alp(30);
string a[h];
P s, g;
int cost[h][w];
queue<P> q;
rep(i,h) cin >> a[i];
rep(i,h)rep(j,w){
if(a[i][j]=='S'){ s.first=i; s.second=j;}
else if(a[i][j]=='G'){
//P g(i,j);
g.first=i; g.second=j;
}
else if(a[i][j]=='#') cost[i][j]=-2;
else if(a[i][j]=='.') continue;
else{
alp[a[i][j]-'a'].push_back(P(i,j));
}
}
rep(i,h)rep(j,w) cost[i][j]=-1;
P tmp;
q.push(s);
cost[s.first][s.second] = 0;
int already[30]={};
while(!q.empty()){
tmp = q.front(); q.pop();
int x = tmp.first, y = tmp.second;
if(x>0){
if(cost[x-1][y]==-1){
if(a[x-1][y]!='#'){
cost[x-1][y] = cost[x][y]+1;
q.push(P(x-1,y));
}
}
}
if(x<h-1){
if(cost[x+1][y]==-1){
if(a[x+1][y]!='#'){
cost[x+1][y] = cost[x][y]+1;
q.push(P(x+1,y));
}
}
}
if(y>0){
if(cost[x][y-1]==-1){
if(a[x][y-1]!='#'){
cost[x][y-1] = cost[x][y]+1;
q.push(P(x,y-1));
}
}
}
if(y<w-1){
if(cost[x][y+1]==-1){
if(a[x][y+1]!='#'){
cost[x][y+1] = cost[x][y]+1;
q.push(P(x,y+1));
}
}
}
if(a[x][y]!='S' && a[x][y]!='.' && a[x][y]!='#' && a[x][y]!='G'){
if(already[a[x][y]-'a']==0){
already[a[x][y]-'a']=1;
rep(i, alp[a[x][y]-'a'].size()){
P teleport = alp[a[x][y]-'a'][i];
if(cost[teleport.first][teleport.second]==-1){
cost[teleport.first][teleport.second] = cost[x][y]+1;
q.push(P(teleport.first,teleport.second));
}
}
}
}
}
//cout <<g.first<<" " <<g.second << endl;
cout << cost[g.first][g.second] << endl;
} |
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 5;
int mod = 998244353;
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
n *= 2;
int a[n], tag[n];
vector<pair<int, int>> v;
for (int i = 0; i < n; i++) cin >> a[i], v.push_back({a[i], i});
sort(v.begin(), v.end());
for (int i = 0; i < n / 2; i++) tag[v[i].se] = 1;
for (int i = n / 2; i < n; i++) tag[v[i].se] = -1;
int ans[n]{}, pre[n + 1]{};
map<int, queue<int>> v2;
for (int i = 0; i < n; i++) pre[i + 1] = pre[i] + tag[i], v2[pre[i + 1]].push(i);
for (int i = 0; i < n; i++){
if (ans[i]) continue;
ans[i] = 1;
v2[pre[i + 1]].pop();
int p = v2[pre[i]].front();
v2[pre[i]].pop();
ans[p] = 2;
//cout << pre[i] << ' ' << p << '\n';
}
for (int i = 0; i < n; i++) cout << (ans[i] == 1 ? '(' : ')');
} | #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;
#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i,k,N) for(int i=k;i<N;i++)
#define MP make_pair
#define MT make_tuple //tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(),(x).end()
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main(){
ll N;
cin>>N;
N*=2;
v A(N);
rep(i,0,N)cin>>A[i];
v As=A;
sort(all(As));
ll mid = -1;
ll midl = 0, midr = 0;
if(As[N/2] == As[N/2-1]){
mid = As[N/2];
}
map<ll,ll> m;
rep(i,0,N){
if(As[i]!=mid){
if(i<N/2)m[As[i]]--;
else m[As[i]]++;
}
else{
m[As[i]] = 999999;
if(i<N/2)midl++;
else midr++;
}
}
//cout<<m[1]<<m[4]<<endl;
ll now = 0;
ll nowL = 0;
rep(i,0,N){
//cout<<A[i]<<m[A[i]]<<endl;
if(m[A[i]] == 999999){
if(now > 0){
if(midl > 0){
cout<<")";
nowL--;
if(nowL == 0)now = 0;
midl--;
}
else{
now = 1;
cout<<"(";
nowL++;
midr--;
}
}
else if(now < 0){
if(midr > 0){
cout<<")";
nowL--;
if(nowL == 0)now = 0;
midr--;
}
else{
now = -1;
cout<<"(";
nowL++;
midl--;
}
}
else{
cout<<"(";
nowL++;
if(midr==0){
now = -1;
midl--;
}
else if(midl == 0){
now = 1;
midr--;
}
else{
if(m[A[i+1]] == 999999 || m[A[i+1]] > 0){
now = -1;
midl--;
}
else{
now = 1;
midr--;
}
}
}
}
else if(m[A[i]] < 0){
if(now <= 0){
now = -1;
cout<<"(";
nowL++;
}
else{
cout<<")";
nowL--;
if(nowL == 0)now = 0;
}
}
else{
if(now >= 0){
now = 1;
cout<<"(";
nowL++;
}
else {
cout<<")";
nowL--;
if(nowL == 0)now = 0;
}
}
}
return 0;
} |
#include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll,ll>
#define mod 998244353
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fb(i,a,b) for(ll i=a;i>b;i--)
#define ff first
#define ss second
#define srt(v) if(!v.empty())sort(v.begin(),v.end())
#define rev(v) if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
ll res=1;
while(b!=0)
{
if(b&1)
{
res=(res*a)%mod;
}
a=(a*a)%mod;
b/=2;
}
return res;
}
ll inv_mod(ll x){
return pow_mod(x,mod-2);
}
const ll N=(ll)3e5;
ll inv[N];
ll fac[N];
ll ncr(ll n,ll r){
if(n<r)
return 0;
if(n==r||r==0)
return 1LL;
ll x=fac[n];
ll y=inv[n-r];
ll z=inv[r];
return ((x*y)%mod*z)%mod;
}
void solve()
{
ll n;
cin>>n;
double ans=0.00;
double prev=0.00;
for(ll i=1;i<n;i++){
ans= prev+(double)n/(double)i;
prev=ans;
}
cout<<fixed<<setprecision(6)<<ans<<endl;
}
int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//Start from Here.
ll t;
t=1;
// cin>>t;
while(t--)
solve();
fac[0]=1;
for(ll i=1;i<N;i++)
fac[i]=(i*fac[i-1])%mod;
for(ll i=0;i<N;i++)
inv[i]=inv_mod(fac[i]);
//Good Bye!
return 0;
}
| // Umang_US
#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define maxx 2*100005
#define sec second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(),v.end());
#define sortr(v) sort(v.rbegin(),v.rend());
#define w() ll _;cin>>_;while(_--)
#define edge pair<ll,ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\n"
#define cy cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define lb(v,x) std::lower_bound(v.begin(),v.end(),x)
#define ub(v,x) std::upper_bound(v.begin(),v.end(),x)
using namespace std;
void init_code(){
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
void solve()
{
ll x,y;
cin>>x>>y;
if(x==y)
{
cout<<x<<endl;
}
else
{
map<ll,ll>m;
m[x]++;
m[y]++;
for(int i=0;i<=2;i++)
{
if(m[i]==0)
{
cout<<i<<endl;
return;
}
}
}
}
int main()
{
init_code();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD ll(998244353)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cerr<<#x<<": "<<x<<endl
int main(){
ll h, w;
cin >> h >> w;
vector<vector<ll>> board(h, vector<ll>(w));
vector<vector<ll>> score(h, vector<ll>(w));
for(int i = 0; i < h; i++){
string s;
cin >> s;
for(int j = 0; j < w; j++){
board[i][j] = s[j] == '+' ? 1 : -1;
}
}
score[h-1][w-1] = 0;
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((i+j)%2==1){//aoki
if(i<h-1 && j<w-1){
score[i][j] = min(score[i+1][j] - board[i+1][j], score[i][j+1] - board[i][j+1]);
}else if(i<h-1 && j==w-1){
score[i][j] = score[i+1][j] - board[i+1][j];
}else if(i==h-1 && j<w-1){
score[i][j] = score[i][j+1] - board[i][j+1];
}
}else{
if(i<h-1 && j<w-1){
score[i][j] = max(score[i+1][j] + board[i+1][j], score[i][j+1] + board[i][j+1]);
}else if(i<h-1 && j==w-1){
score[i][j] = score[i+1][j] + board[i+1][j];
}else if(i==h-1 && j<w-1){
score[i][j] = score[i][j+1] + board[i][j+1];
}
}
}
}
if(score[0][0] > 0){
cout << "Takahashi" << endl;
}else if(score[0][0] == 0){
cout << "Draw" << endl;
}else{
cout << "Aoki" << endl;
}
return 0;
}
| #include<iostream>
#include<algorithm>
using namespace std;
int H,W;
string A[2000];
int dp[2000][2000];
main()
{
cin>>H>>W;
for(int i=0;i<H;i++)cin>>A[i];
int ans;
for(int i=H;i--;)for(int j=W;j--;)
{
int L=-1e9,R=-1e9;
if(i<H-1)
{
L=-dp[i+1][j];
}
if(j<W-1)
{
R=-dp[i][j+1];
}
ans=dp[i][j]=max(L,R);
if(i==H-1&&j==W-1)ans=dp[i][j]=0;
if(A[i][j]=='+')dp[i][j]--;
else dp[i][j]++;
}
cout<<(ans>0?"Takahashi":ans<0?"Aoki":"Draw")<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
int a1,a2,a3;
int main(){
cin>>a1>>a2>>a3;
if(a1>a2)swap(a1,a2);
if(a1>a3)swap(a1,a3);
if(a2>a3)swap(a2,a3);
cout<<(a3-a2==a2-a1?"Yes":"No");
} | #include <math.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() { int a, b, x, y;
cin >> a >> b >> x >> y;
if (a == b) cout << x << endl;
else if(a>b){
if (2 * x < y) cout << x * (2 * (a - b) - 1) << endl;
else
cout << (a - b - 1) * y + x << endl;
}
else {
if (2 * x < y) cout << x * (2 * (b - a) + 1) << endl;
else
cout << (b - a) * y + x << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,L=10000000000;
string s,t;
int main(void){
cin>>n>>s;
for(int i=0;i<n;i++)t+="110";
if(s=="1")cout<<L*2<<endl;
else if(n%3==1 && t.substr(0,n)==s)cout<<L-((n-1)/3)<<endl;
else if(n%3==2 && t.substr(0,n)==s)cout<<L-((n-2)/3)<<endl;
else if(n%3==0 && t.substr(0,n)==s)cout<<L-((n-3)/3)<<endl;
else if(n%3==0 && t.substr(1,n)==s)cout<<L-(n/3)<<endl;
else if(n%3==1 && t.substr(1,n)==s)cout<<L-((n-1)/3)<<endl;
else if(n%3==2 && t.substr(1,n)==s)cout<<L-((n-2)/3)<<endl;
else if(n%3==2 && t.substr(2,n)==s)cout<<L-((n+1)/3)<<endl;
else if(n%3==0 && t.substr(2,n)==s)cout<<L-(n/3)<<endl;
else if(n%3==1 && t.substr(2,n)==s)cout<<L-((n-1)/3)<<endl;
else cout<<0<<endl;
}
| #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <tuple>
#include <cmath>
#include <numeric>
#include <set>
#include <map>
#include <array>
#include <complex>
#include <iomanip>
#include <cassert>
#include <random>
using ll = long long;
using std::cin;
using std::cout;
using std::endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;
template<int mod>
struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int) (1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
std::swap(a -= t * b, b);
std::swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend std::ostream &operator<<(std::ostream &os, const ModInt &p) {
return os << p.x;
}
friend std::istream &operator>>(std::istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt< mod >(t);
return (is);
}
static int get_mod() { return mod; }
};
constexpr int mod = (int)1e9 + 7;
using mint = ModInt<mod>;
void solve()
{
ll n, P; cin >> n >> P;
if(P == 2)
{
if(n == 1)
{
cout << 1 << "\n";
}
else
{
cout << 0 << "\n";
}
}
else
{
mint res = mint(P - 1) * mint(P - 2).pow(n - 1);
cout << res << "\n";
}
}
int main()
{
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int kkt = 1;
//cin >> kkt;
while(kkt--)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int n; cin >> n;
int ans = n*108;
ans = ans/100;
if(ans<206) cout << "Yay!" << endl;
if(ans==206) cout << "so-so"<<endl;
if(ans>206) cout << ":(" << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;
template <typename T>
bool chmax(T& a, const T& b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
if(a > b) {
a = b;
return true;
}
return false;
}
void solve(long long N) {
ll tmp = double(N) * 1.08;
ll price = 206;
string ans;
if(tmp > price) ans = ":(";
if(tmp == price) ans = "so-so";
if(tmp < price) ans = "Yay!";
cout << ans << endl;
}
int main() {
long long N;
std::scanf("%lld", &N);
solve(N);
return 0;
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define INF 9223372036854775807ll
#define endl '\n'
#define pii pair<ll int,ll int>
#define vi vector<ll int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i,a,b) for(ll int i=a;i<b;i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define lower(u) transform(u.begin(), u.end(), u.begin(), ::tolower);//convert string u to lowercase;
#define upper(u) transform(u.begin(), u.end(), u.begin(), ::toupper);
#define time cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define N 100005
ll n;
ll dp[17][(1<<17)];
ll x[17],y[17],z[17];
ll dps(ll cur,ll mask)
{
if(mask==((1<<n)-1))
{
return abs(x[0]-x[cur])+abs(y[0]-y[cur])+max(0ll,z[0]-z[cur]);
}
if(dp[cur][mask]!=-1)
return dp[cur][mask];
ll ans=hell;
rep(i,0,n)
{
if(((1<<i)&mask)==0)
{
ll c=abs(x[i]-x[cur])+abs(y[i]-y[cur])+max(0ll,z[i]-z[cur]);
ans=min(ans,c+dps(i,mask|(1<<i)));
}
}
return dp[cur][mask]=ans;
}
void solve()
{
cin>>n;
rep(i,0,n)
{
cin>>x[i]>>y[i]>>z[i];
}
memset(dp,-1,sizeof(dp));
cout<<dps(0,1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int TESTS=1;
// cin>>TESTS;
while(TESTS--)
{
solve();
}
time
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int maxn = 1e5+5;
int l[maxn], r[maxn];
int p[maxn];
bool ok[205][205];
void build(int L,int R) {
int sz = (R - L + 1) / 2;
for (int i = L, j = i + sz ; j <= R ; i++,j++) {
if (p[i] && p[j] && p[i] + p[j]) return;
if (p[i] > 0)return;
if (p[j] < 0) return;
}
ok[L][R] = 1;
}
void go() {
int n;
cin >> n;
f1(n) {
cin >> l[i] >> r[i];
if (~l[i] && ~r[i] && l[i] > r[i]) {
cout << "No\n";
exit(0);
}
if (~l[i]) {
if (p[l[i]]) {
cout << "No\n";
exit(0);
}
p[l[i]] = -i;
}
if (~r[i]) {
if (p[r[i]]) {
cout << "No\n";
exit(0);
}
p[r[i]] = i;
}
}
n *= 2;
for (int i = 1 ; i <= n ; i += 2) {
for (int j = i + 1 ; j <= n ; j++) {
build(i,j);
//cout << i <<' '<<j<<' '<<ok[i][j] << '\n';
}
}
int dp[n+5] = {};
dp[0] = 1;
for (int i = 2 ; i <= n ; i+= 2) {
for (int j = 1 ; j <= i ; j += 2) {
if (ok[j][i] && dp[j-1]) dp[i]= 1;
}
}
if (dp[n]) {
cout << "Yes\n";
}
else {
cout << "No\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int c = 0;
int t;
if (!c) {
t = 1;
}
else {
cin >> t;
}
while (t--) {
go();
}
} |
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<pll,pll> ppp;
typedef vector<ll> vll;
typedef vector<pll> vp;
typedef priority_queue<pll,vp,greater<pll> > pqpll;
typedef priority_queue<ll, vll, greater<ll> > pqll;
#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n-1; i >= a;i--)
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second
#define EPS 1e-10
#define ALL(a) (a.begin(), a.end())
template<typename T>
ll sz(vector<T> &vec){ return (ll)vec.size(); }
template<typename T>
ll sz(priority_queue<T, vector<T> > &pq) {return (ll)pq.size(); }
template<typename T>
ll sz(priority_queue<T, vector<T>, greater<T> > &pq) {return (ll)pq.size(); }
ll sz(string &s) {return (ll)s.size(); }
ll gcd(ll a,ll b){ return ((!b) ?a :gcd(b, a%b)); }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b; }
bool checkindex(ll i,ll n){ return (i < n && i >= 0); }
ll dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
#define SEG_LEN (1 << 19)
ll seg[SEG_LEN*2];
void add(ll ind,ll v){
ind += SEG_LEN;
seg[ind] ^= v;
while(1){
ind /= 2;
if(ind <= 0) break;
seg[ind] = seg[ind*2] ^ seg[ind*2+1];
}
return;
}
ll sum(ll l, ll r){
l += SEG_LEN; r += SEG_LEN;
ll res = 0;
while(l < r){
if(l % 2 == 1){
res ^= seg[l];
l++;
}
l /= 2;
if(r % 2 == 1){
res ^= seg[r-1];
r--;
}
r /= 2;
}
return res;
}
int main(){
ll n,q;
cin >> n >> q;
vll a(n);
rep(i,0,n) cin >> a[i];
rep(i,0,n) add(i+1,a[i]);
ll t,x,y;
rep(i,0,q){
cin >> t >> x >> y;
if(t == 1){
add(x,y);
}
else{
cout << sum(x,y+1) << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long N, Q; cin >> N >> Q;
vector<long long> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
vector<pair<long long, long long>> vec;
for (int i = 0; i < N; i++) {
vec.emplace_back(A[i], i);
}
sort(vec.begin(), vec.end());
for (int i = 0; i < Q; i++) {
long long K; cin >> K;
long long left = 0, right = 1100000000000000000;
while (right - left > 1) {
long long mid = left + (right - left) / 2;
long long left_ = -1, right_ = N;
bool flag = false;
while (right_ - left_ > 1) {
long long mid_ = left_ + (right_ - left_) / 2;
if (A[mid_] > mid) right_ = mid_;
else if (A[mid_] < mid) left_ = mid_;
else {
flag = true;
right_ = mid_; left_ = mid_;
}
}
if (!flag) {
if (right_ + K > mid) left = mid;
else right = mid;
}
else {
if (right_ + K >= mid) left = mid;
else right = mid;
}
}
cout << right << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<(1<<n)-1<<endl;
for(int i=1;i<(1<<n);i++)
{
for(int j=0;j<(1<<n);j++)
putchar('A'+(__builtin_popcount(i&j)&1));
puts("");
}
return 0;
} | //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
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 main() {
ll N; cin>>N;
ll sum = 0;
for(ll i=1; i<1e9; i++){
sum += i;
if(sum >= N){
cout << i << endl;
return 0;
}
}
} |
#include<bits/stdc++.h>
using namespace std;
long long dp1[200010],dp2[200010],tr[200010],n,m,t,ans=0,vis[200010];
vector<long long>f[200010];
void change(long long x){
for(long long i=x;i<=m;i+=i&-i)
tr[i]++;
}
long long ask(long long x){
long long s=0;
for(long long i=x;i;i-=i&-i)
s=s+tr[i];
return s;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m>>t;
long long s1=n+1,s2=m+1;
for(long long i=1;i<=n;i++)
dp1[i]=m+1;
for(long long i=1;i<=m;i++)
dp2[i]=n+1;
for(long long i=1;i<=t;i++){
long long x,y;
cin>>x>>y;
dp1[x]=min(dp1[x],y);
dp2[y]=min(dp2[y],x);
f[x].push_back(y);
}
for(long long i=1;i<dp2[1];i++)
ans=ans+dp1[i]-1;
for(long long j=1;j<dp1[1];j++)
ans=ans+dp2[j]-1;
for(long long i=1;i<dp2[1];i++){
for(long long j=0;j<f[i].size();j++){
long long tt=f[i][j];
if(!vis[tt]){
vis[tt]=true;
change(tt);
}
}
long long tt=min(dp1[i],dp1[1]-1);
ans=ans-(tt-ask(tt));
}
cout<<ans<<"\n";
return 0;
} | #include<iostream>
#include<string>
#include<vector>
#include<set>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<queue>
#include<stack>
#include<utility>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<tuple>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define FOR(i, a, b) for(ll i=(a); i<(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define NREP(i, n) FOR(i, 1, n+1)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
struct Edge{
ll to, cost;
Edge(ll t, ll c) : to(t), cost(c) { }
};
using Graph = vector<vector<Edge>>;
ll MODpow(ll x, ll n, ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*x%mod;
x=x*x%mod;
n >>= 1;
}
return res;
}
const ll MOD=1e9+7;
const ll nmax=200000+10;
ll N;
Graph G(nmax);
ll solve(){
vvl color(nmax,vl(60,-1));
vl cnt1(60,0);
vl seen(nmax,0);
queue<ll> q;
q.push(0);
REP(i,60) color[0][i]=0;
while(!q.empty()){
ll v=q.front();
q.pop();
seen[v]=1;
for(auto e:G[v]){
if(seen[e.to]) continue;
ll et=e.to, ec=e.cost;
q.push(e.to);
REP(i,60){
bool eci=ec&1;
ec>>=1;
color[et][i]=color[v][i]^eci;
if(color[et][i]) cnt1[i]++;
}
}
}
ll res=0,rec=1;
REP(i,60){
ll mid1=rec;
rec=rec*2%MOD;
ll mid2=(N-cnt1[i])*cnt1[i]%MOD;
ll mid=mid1*mid2%MOD;
res+=mid;
res%=MOD;
}
return res;
}
int main(void){
cin>>N;
REP(i,N-1){
ll u,v,w; cin>>u>>v>>w;
u--,v--;
G[u].push_back(Edge{v,w});
G[v].push_back(Edge{u,w});
}
ll res=solve();
cout<<res<<endl;
return 0;
}
|
#ifdef _LOCAL
#include "local_include.hpp"
#else
#include <bits/stdc++.h>
using namespace std;
#endif
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define fto(i, s, e) for (int i = (s); i <= (e); ++i)
#define fto1(i, s, e) for (int i = (s); i < (e); ++i)
#define fdto(i, s, e) for (int i = (s); i >= (e); --i)
#define fit(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define fat(i, a) for (auto i : (a))
#define ll long long
#define ii pair<int, int>
#define pll pair<ll, ll>
template<class T, class Cmp = less<T>> using oss = tree<T, null_type, Cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define bc __builtin_popcountll
#define endl '\n'
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define buga(a, s, e) cout << '{'; if (e < s) cout << '}'; else fto (__i, s, e) cout << a[__i] << " }"[__i == e]; cout << endl
template<typename T>
void bug(T const &var) { cout << var << endl; }
template<typename T, typename... Args>
void bug(T const &var, Args const &... args) {
cout << var << ' ';
bug(args...);
}
double const pi = acos(-1);
#define oo 1000000007
#define OO 1000000000000000003LL
int const maxn = 2e5+3;
int n;
int vmin[maxn], vmax[maxn];
#define multi_test 0
void _main() {
cin >> n;
fto (c, 1, 2e5) vmin[c] = oo, vmax[c] = -oo;
set<int> a;
fto (i, 1, n) {
int x, c;
cin >> x >> c;
vmin[c] = min(vmin[c], x);
vmax[c] = max(vmax[c], x);
a.insert(c);
}
ii cur = {0, 0};
ll fmin = 0, fmax = 0;
fat (c, a) {
ll dt = vmax[c] - vmin[c];
ll fmin2 = dt + min(fmin + abs(cur.first - vmax[c]), fmax + abs(cur.second - vmax[c]));
ll fmax2 = dt + min(fmin + abs(cur.first - vmin[c]), fmax + abs(cur.second - vmin[c]));
fmin = fmin2; fmax = fmax2;
cur = {vmin[c], vmax[c]};
}
bug(min(fmin + abs(cur.first), fmax + abs(cur.second)));
}
int main() {
#ifdef _LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t = 1;
if (multi_test) cin >> t;
while (t--) {
_main();
}
#ifdef _LOCAL
cerr << 0.001*clock() << endl;
#endif
return 0;
} | //sudo apt install bcmwl-kernel-source
#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
typedef long double ld;
ll P=1e9+7;
ll mmod=998244353;
vector<ll> tenx(11);
/*----------------------------------------------------------------------------------------------------------------------------*/
bool comp(pair<int,int> a,pair<int,int> b){
if(a.first==b.first)
return a.second<b.second;
return a.first<b.first;
}
int find_mmn(vector<int> &arr,int n,int start,int end){
int i;int mmn=INT_MAX;
for(i=start;i<=end;i++){
mmn=min(mmn,arr[i]);
}
return mmn;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin>>n;
vector<ll> arr(n);
ll i,j;
for(i=0;i<n;i++){
cin>>arr[i];
}
i=0;j=0;int mmn;
ll ans=LLONG_MIN;ll now;ll k;
for(i=0;i<n;i++){
j=i;now=0;k=i-1;
while(j<n && arr[j]>=arr[i]){
now+=arr[i];
j++;
}
while(k>=0 && arr[k]>=arr[i]){
now+=arr[i];
k--;
}
ans=max(ans,now);
}
cout<<ans<<"\n";
}
|
#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
using namespace std;
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
typedef long long lli;
typedef pair<int,int> ii;
typedef vector<int> vi;
#define deb(x) cout << #x": " << (x) << endl;
lli gcd(lli a, lli b){return (b?gcd(b,a%b):a);}
lli lcm(lli a, lli b){ if(!a || !b) return 0; return a * b / gcd(a, b); }
int popcount(lli x) { return __builtin_popcountll(x); }
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int rnd(int n){return uniform_int_distribution<int>(0, n-1)(rng);}
lli poww(lli a, lli b){
lli res =1;
while(b){ if(b&1) res = res * a; a = a*a; b/=2; }
return res;
}
vvc<int> graph(int n, int m, bool dir=1){
vv(int,v,n+1,0);
fore(i,0,m){
int a,b; cin>>a>>b;
v[a].pb(b);
if(dir)v[b].pb(a);
}
return v;
}
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
// ---- コーディングはここから! ('-')7
void solve(){
int n,m; cin>>n>>m;
vc<string>s(n);
fore(i,0,n)cin>>s[i];
vv(int,v,n,m,0);
fore(i,0,n)fore(j,0,m)v[i][j]=s[i][j]=='#';
int tot = 0;
fore(i,0,n-1)fore(j,0,m-1)tot+=(v[i][j]+v[i][j+1]+v[i+1][j]+v[i+1][j+1])%2;
cout<<tot<<ENDL;
}
int main(){_
//int t; cin>>t; while(t--)
solve();
}
| #include <iostream>
#include <cmath>
using namespace std;
long long N, M;
long long calc_expand_mod(long long n, long long b, long long m){
long long ans = 1;
while (n > 0) {
if ((n & 1) == 1) ans = (ans * b) % m;
n >>= 1;
b = (b * b) % m;
}
return ans;
}
int main(){
cin >> N >> M;
long long a = calc_expand_mod(N,10,M*M);
long long b = calc_expand_mod(N,10,M)%(M*M);
cout << (a - b) /M;
} |
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
template<typename T>
long long modpow(const T n,const T p,const T mod);
template<typename T>
long long modinv(const T n,const T mod);
template<typename T>
bool chmax(T &a,const T &b);
template<typename T>
bool chmin(T &a,const T &b);
long long inf=1000000007;
int main(){
int a,b;
cin>>a>>b;
for(int i=-1000;i<=1000;i++){
for(int j=-1000;j<=1000;j++){
if(i+j==a && i-j==b){
cout<<i<<" "<<j<<endl;
return 0;
}
}
}
return 0;
}
template<typename T>
long long modpow(const T n,const T p,const T mod){
if(p==0) return 1;
if(p%2==0){
long long a=modpow(n,p/2,mod);
return a*a%mod;
}
if(p%2==1) return (modpow(n,p-1,mod)*n)%mod;
cerr<<"ERROR"<<endl;
return 1;
}
template<typename T>
long long modinv(const T n,const T mod){
return modpow(n,mod-2,mod);
}
template<typename T>
bool chmax(T &a,const T &b){
if(a<b){
a=b;
return 1;
}
return 0;
}
template<typename T>
bool chmin(T &a,const T &b){
if(a>b){
a=b;
return 1;
}
return 0;
}
| #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
int N, M;
vector<pair<int, int> > G[2011];
LL D[2011];
void MAIN() {
scanf("%d%d", &N, &M);
REP (i, M) {
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
x--; y--;
G[x].emplace_back(y, c);
}
REP (s, N) {
memset(D, 0x3f, sizeof D);
const int INF = D[0];
D[s] = 0;
priority_queue<pair<LL, int> > Q;
Q.emplace(0, s);
while (!Q.empty()) {
int v = Q.top().second;
LL cst = -Q.top().first;
Q.pop();
if (cst != D[v]) continue;
EACH (e, G[v]) {
int w = e->first;
LL tmp = cst + e->second;
if (D[w] > tmp) {
D[w] = tmp;
Q.emplace(-tmp, w);
}
}
}
LL ans = INF;
REP (v, N) EACH (e, G[v]) {
int w = e->first;
int c = e->second;
if (w == s) amin(ans, D[v] + c);
}
if (ans == INF) puts("-1");
else printf("%lld\n", ans);
}
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, k;
cin >> n >> k;
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
ans += i * 100 + j;
}
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define mod 1000000007
int main(){
ll n,k;
cin>>n>>k;
cout<<100*k*(n*(n+1)/2)+n*k*(k+1)/2<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// union by size + path having
class UnionFind {
public:
vector <ll> par; // 各元の親を表す配列
vector <ll> siz; // 素集合のサイズを表す配列(1 で初期化)
// Constructor
UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout << fixed << setprecision(10);
/*--------------------------------*/
int n;cin>>n;
// n=10000000;
vector<ll> p(n);
for(int i=0;i<n;i++)cin>>p[i];
// for(int i=0;i<n;i++)p[i]=i+1;
vector<pair<ll,ll> >vec(n);
for(int i=0;i<n;i++){
vec[i].first=p[i];vec[i].second=i;
}
sort(vec.begin(),vec.end());
reverse(vec.begin(),vec.end());
UnionFind tree(n);
vector<int> flag(n);
ll ans=-1;
ll ans_name=-1,ans_size=-1;
for(int i=0;i<n;i++){
ll name=vec[i].second,value=vec[i].first;
flag[name]=1;
if(name!=0 && flag[name-1]==1){
tree.merge(name,name-1);
if(ans<tree.size(name)*value){
ans=tree.size(name)*value;
ans_name=value;
ans_size=tree.size(name);
}
}
if(name!=n-1 && flag[name+1]==1){
tree.merge(name,name+1);
if(ans<tree.size(name)*value){
ans=tree.size(name)*value;
ans_name=value;
ans_size=tree.size(name);
}
}
}
ll res=ans_size*ans_name;
// cout<<ans_size<<" "<<ans_name<<endl;
for(int i=0;i<n;i++){
res=max(res,(ll)p[i]);
// cout<<p[i]<<endl;
}
cout<<res<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (lli i = a; i < b; i++)
#define lli long long int
#define ld long double
#define all(v) v.begin(), v.end()
#define hell 1000000000000000
#define pb push_back
#define vi vector<lli>
#define vip vector<pair<lli, lli>>
#define F first
#define S second
#define mod 998244353
lli n = 10000000;
vector<bool> p(n + 1, true);
void sieve()
{
for (long long int i = 2; i * i <= n; i++)
{
if (p[i])
{
for (long long int j = i + i; j <= n; j = j + i)
{
p[j] = false;
}
}
}
}
lli Gcd(lli a, lli b)
{
if (a % b == 0)
{
return b;
}
return Gcd(b, a % b);
}
lli power(lli a, lli n)
{
lli res = 1;
while (n > 0)
{
if (n & 1)
res = ((res % mod) * (a % mod)) % mod, n--;
else
a = ((a % mod) * (a % mod)) % mod, n /= 2;
}
return res;
}
void solve()
{
lli n,ans=0;
cin>>n;
lli a[n];
rep(i,0,n)
{
cin>>a[i];
}
rep(l,0,n)
{
lli x=a[l];
rep(r,l,n)
{
x=min(x,a[r]);
ans=max(ans,x*(r-l+1));
}
// cout<<ans<<" ";
}
cout<<ans;
cout<<endl;
// cout << (double)clock()/CLOCKS_PER_SEC << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
// sieve();
lli t = 1;
while (t--)
{
solve();
}
} |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef vector <ll> vl;
typedef vector <vl> vvl;
typedef vector <char> vc;
typedef vector <vc> vvc;
typedef vector <int> vi;
typedef vector <vi> vvi;
const ll MOD = 998244353;
int mat[5001][5001];
ll dp[5001][5001];
char c[5001][5001];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
vl pow3(5001);
pow3[0] = 1;
for (int i = 0; i <= 5000; i++)
for (int j = 0; j <= 5000; j++)
c[i][j] = ' ';
for (int i = 1; i <= 5000; i++)
pow3[i] = pow3[i - 1] * 3 % MOD;
int n, m;
cin >> n >> m;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
mat[i][j] = dp[i][j] = 0;
int k;
cin >> k;
while (k--) {
int i, j;
cin >> i >> j;
i--, j--;
cin >> c[i][j];
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
mat[i][j] = (i ? mat[i - 1][j] : 0) + (j ? mat[i][j - 1] : 0) - ((i && j) ? mat[i - 1][j - 1] : 0) + (c[i][j] == ' ');
dp[0][0] = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
dp[i][j] %= MOD;
if (c[i][j] == ' ') {
dp[i + 1][j] += dp[i][j] * (pow3[mat[i + 1][j] - mat[i][j] - (c[i + 1][j] == ' ')]);
dp[i][j + 1] += dp[i][j] * (pow3[mat[i][j + 1] - mat[i][j] - (c[i][j + 1] == ' ')]);
}
if (c[i][j] != 'R')
dp[i + 1][j] += dp[i][j] * (pow3[mat[i + 1][j] - mat[i][j] - (c[i + 1][j] == ' ')]);
if (c[i][j] != 'D')
dp[i][j + 1] += dp[i][j] * (pow3[mat[i][j + 1] - mat[i][j] - (c[i][j + 1] == ' ')]);
}
if (c[n - 1][m - 1] == ' ')
dp[n - 1][m - 1] = dp[n - 1][m - 1] * 3 % MOD;
cout << dp[n - 1][m - 1];
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, b) for (ll i = a; i < b; ++i)
#define rep_down(i, a, b) for (ll i = a; i > b; --i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
constexpr ll INF = (1ll << 60);
constexpr ll mod = 998244353;
constexpr double pi = 3.14159265358979323846;
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;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
int gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; }
int main(){
ll h,w,k,a,b,p;
char c;
cin>>h>>w>>k;
vector<vector<ll>> x(h+1, vector<ll>(w+1,0));
vector<vector<char>> y(h+1, vector<char>(w+1,'N'));
rep(i,k){
cin>>a>>b>>c;
y[a-1][b-1]=c;
}
x[0][0]=modpow(3,h*w-k,mod);
rep(i,h){
rep(j,w){
x[i][j]%=mod;
if(y[i][j]=='X'){
x[i+1][j]+=x[i][j];
x[i][j+1]+=x[i][j];
}else if(y[i][j]=='R'){
x[i][j+1]+=x[i][j];
}else if(y[i][j]=='D'){
x[i+1][j]+=x[i][j];
}else{
p=x[i][j]*2*modinv(3,mod)%mod;
x[i+1][j]+=p;
x[i][j+1]+=p;
}
}
}
cout<<x[h-1][w-1]<<endl;
} |
#define LOCAL
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++)
#define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
// #define M_PI 3.14159265358979323846 // CF
#define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI))
#define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank
#define endl "\n"
#define Case(x) printf("Case #%d: ", x); // gcj
typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<char> vch;
typedef vector<vector<char>> vvch;
constexpr ll INF = 1001002003004005006ll;
constexpr int n_max = 2e5+10;
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; };
template<class T, class U>
T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;};
// debug
template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';};
string to_string(const char c) {return to_string((string) &c);};
string to_string(bool b) {return (b ? "true" : "false");};
template <size_t N>
string to_string(bitset<N> v){
string res = "";
for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
return res;
};
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for(const auto &x : v) {
if(!first) res += ", ";
first = false; res += to_string(x);
}
res += "}";
return res;
};
template <typename A, typename B>
string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
void debug_out() {cerr << endl;};
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); };
void LINE_OUT() {
cout << "--------------" << endl;
};
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define LINE LINE_OUT();
#else
#define debug(...) 71
#define LINE 71;
#endif
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);
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M; cin >> N >> M;
vi mem(1005);
rep(i,0,N) {
int a; cin >> a; mem[a]++;
}
rep(i,0,M) {
int b; cin >> b; mem[b]--;
}
vi ans;
rep(i,0,1005) {
if (mem[i] != 0) ans.pb(i);
}
rep(i,0,ans.size()) {
cout << ans[i];
if (i < ans.size()-1) cout << " ";
}
cout << endl;
return 0;
};
| /**
* author: otera
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60;
#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++)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template <class Abel> struct BIT {
private:
vector<Abel> node; int n;
Abel UNITY_SUM = 0; //to be set
public:
BIT(int n_) {
n = n_; node.resize(n, UNITY_SUM);
}
//0-indexed
void add(int a, Abel w) {
for (int i = a; i < n; i |= i + 1)node[i] += w;
}
//[0,a)
Abel sum(int a) {
Abel ret = UNITY_SUM;
for (int i = a - 1; i >= 0; i = (i&(i + 1)) - 1)ret += node[i];
return ret;
}
//[a,b)
Abel sum(int a, int b) {
return sum(b) - sum(a);
}
//k-th number (k is 0-indexed)
int get(int k) {
++k;
int res = 0;
int n = 1; while(n < (int)node.size()) n *= 2;
for(int i = n / 2; i > 0; i /= 2) {
if(res + i <= (int)node.size() && node[res + i - 1] < k) {
k -= node[res + i - 1];
res += i;
}
}
return res; //0-indexed
}
//debug
void print() {
for(int i = 0; i < n; ++i) cout << sum(i, i + 1) << ",";
cout << endl;
}
};
void solve() {
int h, w, m; cin >> h >> w >> m;
ll ans = h * w;
vector<int> x(m), y(m);
int a = w, b = h;
rep(i, m) {
cin >> x[i] >> y[i];
-- x[i], -- y[i];
if(x[i] == 0) chmin(a, y[i]);
if(y[i] == 0) chmin(b, x[i]);
}
for(int i = b; i < h; ++ i) {
x.push_back(i);
y.push_back(0);
++ m;
}
for(int i = a; i < w; ++ i) {
x.push_back(0);
y.push_back(i);
++ m;
}
vector<int> ids(m);
iota(all(ids), 0);
sort(all(ids), [&](int i, int j){
return (y[i] != y[j] ? y[i] < y[j] : x[i] > x[j]);});
BIT<int> bit(h);
vector<int> co(w, 0);
int prev = -1;
rep(i, m) {
int id = ids[i];
if(bit.sum(x[id], x[id] + 1) == 0) bit.add(x[id], 1);
if(co[y[id]] == 0) ans -= bit.sum(x[id], h);
else ans -= bit.sum(x[id], prev);
prev = x[id];
++ co[y[id]];
}
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(20);
//int t; cin >> t; rep(i, t)solve();
solve();
return 0;
} |
/* Bismillahirrahmanirrahim */
/* Author : Jehian Norman Saviero (@Reiva5) */
#include <bits/stdc++.h>
// MACROS SAMPAH
#define Jehian using
#define Norman namespace
#define Saviero std
Jehian Norman Saviero;
// MACROS TYPE
typedef long l;
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<l,l> pl;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<l> vl;
typedef vector<ll> vll;
// MACROS FOR PRINT
#define nl printf("\n")
// MACROS FOR ATTRIBUTE
#define fi first
#define se second
// MACROS FOR FUNCTION AND METHOD
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ALL(x) (x).begin(), (x).end()
#define ALLN(x,n) (x), (x)+(n)
#define CUBE(x) (SQR(x)*(x))
#define SET(x,y) for (int _qwe = sizeof(x)/sizeof(x[0]); _qwe; ) x[--_qwe] = (y)
#define SQR(x) ((x)*(x))
#define SSORT(x,sz) sort(ALLN(x,sz))
#define RESET(x) memset((x), 0, sizeof(x))
#define VSORT(x) sort(ALL(x))
// MACROS READ INPUT
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%ld", &x)
#define scll(x) scanf("%lld", &x)
#define scd(x) scanf("%lf", &x)
#define scld(x) scanf("%Lf", &x)
#define scul(x) scl(x)
#define scull(x) scll(x)
int main() {
int N, K, M;
sci(N), sci(K), sci(M);
ll sum = 0;
for (int i = 0; i < N-1; ++i) {
int curr;
sci(curr);
sum += curr;
}
ll tot = M * N;
ll diff = tot - sum;
if (0 <= diff && diff <= K) {
printf("%lld\n", diff);
} else if (diff < 0) {
printf("0\n");
} else {
printf("-1\n");
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, m;
cin >> n >> k >> m;
int s = 0;
int A;
for(int i = 0; i < n - 1; i++){
cin >> A;
s += A;
}
int B = n*m - s;
if(B > k){
cout << -1;
}
else if(B < 0){
cout << 0;
}
else{
cout << B;
}
} |
#include<bits/stdc++.h>
#define ll long long
#define re register
#define INF 2147483647
using namespace std;
inline ll read()
{
ll f=1,x=0;char s=getchar();
while(s<'0'||s>'9')
{
if(s=='-') f=-1;
s=getchar();
}
while(s>='0'&&s<='9')
{
x=x*10+s-48;
s=getchar();
}
return f*x;
}
ll C[100][100];
int main()
{
int a=read(),b=read();
int n=a+b;
ll k=read();
for(int i=0;i<=n;i++) C[i][i]=C[i][0]=1;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
C[j][i]=C[j-1][i-1]+C[j-1][i];
for(int i=1;i<=n;i++)
{
// cout<<a<<" "<<b<<" "<<k<<endl;
if(a==0)
{
for(int j=1;j<=b;j++) putchar('b');
break;
}
if(b==0)
{
for(int j=1;j<=a;j++) putchar('a');
break;
}
ll res=C[a+b-1][a-1];
// cout<<res<<endl;
if(k>res)
{
putchar('b');
k-=res;
b--;
}
else
{
putchar('a');
a--;
}
}
return 0;
} | /* dont stick to an approach */
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#include <numeric>
#include <cstdlib>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long int ull;
typedef long double ldb;
#define PB push_back
#define For(i, n) for (ll i = 0; i < n; i++)
#define trav(a,x) for (auto& a : x)
#define PYES cout<<"YES\n"
#define PNO cout<<"NO\n"
#define PYes cout<<"Yes\n"
#define PNo cout<<"No\n"
#define endl '\n'
#define sq(x) (x*x)
#define vll vector<ll>
#define pll pair<ll,ll>
#define ff first
#define ss second
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<ll>())
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define all(v) v.begin(),v.end()
#define ACC(v) accumulate(v.begin(),v.end(),0ll)
#define Fas ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
//--------------------------------------------functions-------------------------------------------------//
ll power(ll a,ll b){ll result=1;while(b>0){if(b%2 == 1){result *= a;} a *= a;b /= 2;}return result;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
ll countSetBits(ll x){ll Count=0;while(x>0){if(x&1) Count++;x=x>>1;}return Count;}
bool isPerfectSquare(ll n){ll sr = sqrt(n);if (sr * sr == n)return true;else return false;}
ll mod(ll x,ll M){return ((x%M + M)%M);}
ll add(ll a, ll b,ll M){return mod(mod(a,M)+mod(b,M),M);}
ll mul(ll a, ll b,ll M){return mod(mod(a,M)*mod(b,M),M);}
ll powerM(ll a,ll b,ll M){
ll res=1ll;
while(b){
if(b%2ll==1ll){
res=mul(a,res,M);
}
a=mul(a,a,M);b/=2ll;
}
return res;
}
//------------------------------------sieve of erantothenes-----------------------------------------------//
//ll MAXN=1e7+150;
//vector<ll> fact(MAXN+1);
//void sieve_of_erantothenes()
//{
// fact[1] = 1;
// for (ll i = 2; i <= MAXN; i++)
// fact[i] = i;
// for (ll i = 4; i <= MAXN; i += 2)
// fact[i] = 2;
// for (ll i = 3; i * i <= MAXN; i++){
// if (fact[i] == i){
// for (ll j = i * i; j <= MAXN; j += i)
// if (fact[j] == j)
// fact[j] = i;
// }
// }
//}
//----------------------------------------nCr mod------------------------------------------------------//
ll nCr(ll n, ll k){
if(n<k) return 0;
if(k==0) return 1;
ll res = 1;
if (k > n - k) k = n - k;
for(ll i = 0; i < k; ++i){
res *= (n - i);
res /= (i + 1);
}
return res;
}
ll modInverse(ll n,ll M){
return powerM(n,M-2,M);
}
ll nCrM(ll n,ll r,ll M){
if(n<r) return 0;
if(r==0) return 1;
vector<ll> fact(n+1);
fact[0]=1;
for(ll i=1;i<=n;i++){
fact[i]=mul(fact[i-1],i,M);
}
return mul(mul(fact[n],modInverse(fact[r],M),M),modInverse(fact[n-r],M),M);
}
//-----------------------------------------------solve-----------------------------------------------------//
void solve(){
ll a,b,k;cin>>a>>b>>k;
vll v(a+b);
for(ll i=b-1;i>=0;i--){
ll curr=i;
for(ll j=i;j<=i+a;j++){
if(nCr(j,i)<k){
k-=nCr(j,i);
curr++;
}else{
break;
}
}
v[curr]=1;
}
rev(v);
for(ll i=0;i<a+b;i++){
if(v[i]==1) cout<<'b';
else cout<<'a';
}
cout<<endl;
}
int main(){
Fas;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll t=0;
// cin>>t;
// while(t--)
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>
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 rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P = pair<ll,ll>;
const int INF=1001001001;
const int mod=1e9+7;
ll dp[100][100];
int main() {
int N;
cin>>N;
vector<string>s(N);
rep(i,N){cin>>s[i];}
dp[0][0]=1;
dp[0][1]=1;
rep(i,N){
if(s[i]=="AND"){
dp[i+1][1]+=dp[i][1];
dp[i+1][0]+=dp[i][1]+2*dp[i][0];
}
else{
dp[i+1][1]+=2*dp[i][1]+dp[i][0];
dp[i+1][0]+=dp[i][0];
}
}
cout<<dp[N][1]<<endl;
return 0;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// template <class c, class cmp = less<c> > using ordered_set = tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
template<class T> ostream& operator<<(ostream &os, vector<T> V) {os << "[ "; for(auto v : V) os << v << " "; return os << "]";}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {return os << "(" << P.first << "," << P.second << ")";}
template <typename T,typename U>pair<T,U> operator+(const pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second};}
typedef long long int ll;
const ll mod = 1e9 + 7;
const ll maxn = 2e5 + 5;
#define endl '\n'
#define ld long double
#define int ll
#define all(x) (x).begin(),(x).end()
int32_t main(){
IOS
int n;
cin >> n;
vector<int> a(n), pref(n + 1, 0);
vector<vector<int> > dp(n + 1, vector<int>(n + 1, 0)), sum(n + 2, vector<int>(n + 2, 0));
for(int i = 0; i < n;i++) cin >> a[i], pref[i + 1] = pref[i] + a[i];
dp[0][0] = 1;
sum[1][0] = 1;
for(int i = 1;i <= n;i++){
for(int k = 1;k<=i;k++){
int x = pref[i]%k;
dp[i][k] += sum[k][x];
dp[i][k]%=mod;
}
for(int k = 1;k<=n + 1;k++){
sum[k][pref[i]%(k)] += dp[i][k - 1];
sum[k][pref[i]%k]%=mod;
}
}
int ans = 0 ;
for(int i = 1;i<=n;i++){
ans += dp[n][i];
ans%=mod;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
void printmat(const vector<vector<long long>>& mat) {
for (auto row : mat) {
for (int elem : row)
cout << elem << ", ";
cout << endl;
}
}
void printv(const vector<int>& v) {
for (int elem : v)
cout << elem << " ";
cout << endl;
}
void printvp(const vector<pair<int, int>>& vp) {
for (auto row : vp)
cout << row.first << ", " << row.second << " ; ";
cout << endl;
}
int main() {
int T=1, caseIdx=0;
//cin >> T;
while (T--) {
caseIdx++;
int n;
long long ans=0, second=0;
cin >> n;
vector<long long> a(n), s(n+1);
for (int i=0; i<n; i++) {
cin >> a[i];
s[i+1] = s[i] + a[i];
ans += (a[i]*a[i]);
}
ans *= (n-1LL);
for (int i=0; i<n-1; i++) {
second += (a[i] * (s[n]-s[i+1]));
//cout << (s[n]-s[i+1]) << endl;
}
ans = ans - 2LL*second;
cout << ans << endl;
//cout << "Case #" << caseIdx << ": " << ans << endl;
}
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005
int n;
vector<int> G[MAX_N];
int par[MAX_N];
int visit_[MAX_N];
priority_queue<int> que1[MAX_N], que2[MAX_N];
int dfs(int x){
int res = 0;
if(visit_[x] == 0){
res++;
visit_[x] = 1;
res -= dfs(x);
return res;
}
if(visit_[x] == 1){
rep(i,G[x].size()){
int tmp = -dfs(G[x][i]);
if(tmp % 2 == 0) que2[x].push(tmp);
else que1[x].push(tmp);
}
visit_[x] = 2;
}
while(!que2[x].empty() && que2[x].top()>=0){
res += que2[x].top();
que2[x].pop();
}
if(!que1[x].empty()){
res += que1[x].top();
que1[x].pop();
res -= dfs(x);
}
while(!que2[x].empty()){
res += que2[x].top();
que2[x].pop();
}
return res;
}
int main() {
par[0] = -1;
cin >> n;
srep(i,1,n){
cin >> par[i];
par[i]--;
G[par[i]].push_back(i);
}
int tmp = dfs(0);
int ans = (n-tmp) / 2 + tmp;
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
int A, B;
cin >> A >> B;
int* E;
E = (int*)malloc((A + B) * sizeof(int));
int sum = 0;
int j = 1;
if (A > B) {
for (int i = 0; i < A; i++) {
E[i] = i + 1;
sum += E[i];
}
for (int i = A; i < A + B - 1; i++) {
E[i] = -j;
j++;
sum += E[i];
}
E[A + B - 1] = -sum;
}
else {
for (int i = 0; i < B; i++) {
E[i] = -(i + 1);
sum += E[i];
}
for (int i = B; i < A + B - 1; i++) {
E[i] = j;
j++;
sum += E[i];
}
E[A + B - 1] = -sum;
}
for (int i = 0; i < A + B; i++) cout << E[i] << " ";
free(E);
return 0;
} | #include <bits/stdc++.h>
#include <cstdint>
using namespace std;
#define endl "\n"
#define pb push_back
#define fo(i, start, end) for (ll i = start; i < end; i++)
#define rep(i, start, end, step) for (ll i = start; i < end; i += step)
#define print(x) cout << #x << " is " << x << endl
#define all(arg) arg.begin(), arg.end()
#define sz(arg) (int)arg.size()
using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;
void solve() {
int a, b;
cin >> a >> b;
int ans;
if (a + b >= 15 && b >= 8)
ans = 1;
else if (a + b >= 10 && b >= 3)
ans = 2;
else if (a + b >= 3)
ans = 3;
else
ans = 4;
cout << ans << endl;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(ll)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
int main() {
int n; cin>>n;
ll C; cin>>C;
vector<P> e;
rep(i,n) {
ll a,b,c; cin>>a>>b>>c;
e.emplace_back(a-1,c);
e.emplace_back(b,-c);
}
sort(e.begin(),e.end());
ll ans=0,cost=0,now=0;
for(auto [x,y]:e) {
if(x!=now) {
ans+=min(C,cost)*(x-now);
now=x;
}
cost+=y;
}
cout<<ans;
}
| #include <bits/stdc++.h>
using namespace std;
#define mp(a, b) make_pair(a, b)
typedef pair<int, int> pii;
typedef long long ll;
const int maxn = 2e5 + 10;
int a[maxn], b[maxn], c[maxn], d[maxn];
ll ans;
int n, l;
int main()
{
scanf("%d%d", &n, &l);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for(int i = 1; i <= n; ++i) scanf("%d", &b[i]);
c[1] = a[1] - 1;
for(int i = 2; i <= n; ++i) c[i] = a[i] - a[i - 1] - 1;
c[n + 1] = l - a[n];
d[1] = b[1] - 1;
for(int i = 2; i <= n; ++i) d[i] = b[i] - b[i - 1] - 1;
d[n + 1] = l - b[n];
/*for(int i = 1; i <= n + 1; ++i) cout << c[i] << ' '; cout << endl;
for(int i = 1; i <= n + 1; ++i) cout << d[i] << ' '; cout << endl; */
int l = 1;
for(int i = 1; i <= n + 1; ++i)
if(d[i])
{
while(l <= n + 1 && !c[l]) ++l;
int r = l;
while(r <= n + 1 && d[i] > 0)
{
d[i] -= c[r];
++r;
}
if(d[i] < 0)
{
puts("-1");
return 0;
}
//cout << l << ' ' << r - 1 << endl;
if(i < l) ans += r - 1 - i;
else if(i >= r) ans += i - l;
else ans += r - l - 1;
l = r;
}
printf("%lld\n", ans);
return 0;
} |
#include <cstdio>
using namespace std;
int main() {
int i, j;
long long n, x = 3, y;
scanf("%lld", &n);
for (i = 1; x <= n; i++, x *= 3) {
y = 5;
for (j = 1; x + y <= n; j++, y *= 5) {
if (x + y == n) {
printf("%d %d\n", i, j);
return 0;
}
}
}
puts("-1");
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,int> pli;
typedef pair<int,int> pii;
#define rep(i,a,b) for(ll i=a ; i<b ; i++)
#define qrep(que, ite) for(auto ite=begin(que) ; ite!=end(que) ; ite++)
const int max_n = 1e5;
const ll mod = 1e9+7;
const ll INF = 1e17;
const int inf = 1e5;
typedef long double ld;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; }
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
ll mo = 998244353;
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) { }
void init(int n) { par.assign(n, -1); }
int root(int x) {
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool issame(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y) {
x = root(x); y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) {
return -par[root(x)];
}
};
struct SegmentTree{
int N;
vector<int> node;
public :
void intit(vector<int>v){
int sz = v.size();
N=1;
while(N<sz) N*=2;
node.resize(N);
for(int i=0 ; i<sz ; i++) node[i+N-1] = v[i];
for(int i=N-2 ; i>=0 ; i--) node[i] = min(node[i*2+1], node[i*2+2]);
}
void update(int x, int val){
x += N-1;
node[x+N-1] = val;
while(x>0){
x = (x-1)/2;
node[x] = min(node[x*2+1], node[x*2+2]);
}
}
int getmin(int a, int b, int k, int l, int r){
if(b<=l || r<=a) return inf;
else if(a<=l && r<=b) return node[k];
else{
int vl = getmin(a, b, 2*k+1, l, (l+r)/2);
int vr = getmin(a, b, 2*k+2, (l+r)/2, r);
return min(vl, vr);
}
}
};
vector<ll> divisor(ll n){
vector<ll> res;
for(ll i=1; i*i<=n ; i++){
while(n%i==0){
++res[i];
n /= i;
}
}
if(n!=1) res[n]=1;
return res;
}
int main(){
ll N; cin>>N;
ll now = 1;
ll jj = 1;
rep(i,1,1000000){
now *= 3;
if(N-now<=0) break;
if((N-now)%5==0){
ll k = N-now;
ll cnt = 0;
int ok=1;
while(k>1){
cnt++;
if(k%5!=0){
ok=0;
break;
}
k/=5;
}
if(!ok) continue;
if(cnt==0)continue;
cout<<i<<" "<<cnt<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void fun()
{
ll a[3];
cin>>a[0]>>a[1]>>a[2];
sort(a,a+3);
cout<<a[2]+a[1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t=1;
// cin>>t;
while(t--)
fun();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
long long x,y,a,b,ans;
int main(){
cin>>x>>y>>a>>b;
bool flag=0;
while(x<y/a&&x<(x+b)/a){
x*=a;
ans++;
}
ans+=(y-x-1)/b;
printf("%lld\n",ans);
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int64_t n,m,x,y;
cin>>n>>m>>x>>y;
x--,y--;
vector<vector<vector<int64_t>>>trn(n);
for(int64_t i=0,a,b,c,d;i<m;i++){
cin>>a>>b>>c>>d;
a--,b--;
trn[a].push_back(vector<int64_t>{b,c,d});
trn[b].push_back(vector<int64_t>{a,c,d});
}
priority_queue<pair<int64_t,int64_t>,vector<pair<int64_t,int64_t>>,greater<pair<int64_t,int64_t>>>dijk;
vector<int64_t>dist(n,9e18);
dist[x]=0;
dijk.push({(int64_t)0,x});
while(!dijk.empty()){
auto i=dijk.top();
dijk.pop();
if(i.first>dist[i.second])continue;
for(auto j:trn[i.second]){
int64_t e=j[2];
int64_t d=((dist[i.second]+e-1)/e+0)*e+j[1];
if(dist[j[0]]>d){
dist[j[0]]=d;
dijk.push({d,j[0]});
}
}
}
cout<<(dist[y]==9e18?(int64_t)-1:dist[y])<<'\n';
} | #include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
//debug
#ifdef _DEBUG
#define debug(var) do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
int solve(){
int a, b, c, d;
cin >> a >> b >> c >> d;
int x = c - a;
int y = d - b;
//0
if(x == 0 && y == 0) return 0;
//1
if( x - y == 0 || x + y == 0) return 1;
if(abs(x)+abs(y) <= 3) return 1;
//2
//斜め移動 2
if((x+y)%2 == 0) return 2;
//斜め1,縦横1
for(int i = -3; i <= 3; i++){
for(int j = -3; j <= 3; j++){
if(abs(i)+abs(j) > 3) continue;
if((x+i) - (y+j) == 0 || (x+i) + (y+j) == 0 ) return 2;
}
}
//縦横2
if(abs(x)+abs(y) <= 3*2) return 2;
//3: 0,1,2以外なら3手
return 3;
}
int main(){
cout << solve() << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int mpow(int base, int exp);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define fo(i,n) for(i = 0;i < n; i++)
#define Fo(i,k,n) for(i = k;k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define deb(x) cout << #x << " = " << x << endl
#define deb2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define deb3(x, y, z) cout << #x << " = " << x << ", " << #y << " = " << y << ", "<< #z << " = " << z << endl
ll randGen(ll lower, ll upper) { return (rand() % (upper - lower + 1)) + lower; }
ll dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
// cout << fixed;
// cout.precision(10);
//****************** Function here *********************//
void test_case(){
int n, a, b;
cin >> n >> a >> b;
cout << n - a + b << endl;
}
//*********************************************************************//
int main()
{
#ifndef ONLINE_JUDGE
string inputFile, outputFile;
inputFile = "input.txt";
outputFile = "output.txt";
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while(t-- > 0){
test_case();
}
return 0;
}
/********************** Utility Functions *****************************/
int mpow(int base, int exp) {
base %= MOD;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % MOD;
base = ((ll)base * base) % MOD;
exp >>= 1;
}
return result;
}
/***********************************************************************/
| #include <bits/stdc++.h>
#define pb(x) push_back(x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F(w,x,y) for(ll w=x; w<y; w++)
#define endl "\n"
#define mod 1000000007
#define INF 1000000000
typedef long long ll;
typedef long double ld;
using namespace std;
void play()
{
ll n,m;
cin>>n>>m;
string s;
ll arr[2]={0,0};
F(i,0,n)
{
cin>>s;
ll sum=0;
F(j,0,m)
{
sum+=s[j]-'0';
}
arr[sum%2]++;
}
cout<<arr[0]*arr[1]<<endl;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
t=1;
//cin>>t;
F(i,1,t+1)
{
//cout<<"Case #"<<i<<": ";
play();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
string res = "{";
for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
cerr << " " << to_string(H);
debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
struct Mint {
int val;
Mint() { val = 0; }
Mint(const ll& x) {
val = (-MOD <= x && x < MOD) ? x : x % MOD;
if (val < 0) val += MOD;
}
template <typename U>
explicit operator U() const { return (U)val; }
friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
friend Mint modex(Mint a, ll p) {
assert(p >= 0);
Mint ans = 1;
for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
return ans;
}
Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
Mint& operator++() { return *this += 1; }
Mint& operator--() { return *this -= 1; }
Mint operator++(int) { Mint result(*this); *this += 1; return result; }
Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
Mint operator-() const { return Mint(-val); }
friend Mint operator+(Mint a, const Mint& b) { return a += b; }
friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
friend string to_string(const Mint& b) { return to_string(b.val); }
};
int main() {
int n;
scanf("%d", &n);
vector<string> C(2, string(2, '_'));
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
scanf(" %c", &C[i][j]);
if (C[0][1] == 'B') {
auto D = C;
int sum = (int)'A' + 'B';
D[0][0] = sum - C[1][1];
D[1][1] = sum - C[0][0];
D[0][1] = sum - C[0][1];
D[1][0] = sum - C[1][0];
swap(D, C);
}
assert(C[0][1] == 'A');
Mint ans = 0;
if (n == 2) ans = 1;
else if (C[0][0] == 'A') ans = 1;
else if (C[1][0] == 'B') ans = modex(Mint(2), n - 3);
else {
Mint a = 1, b = 0;
for (int i = 1; i + 1 < n; ++i) {
Mint na = a + b;
Mint nb = a;
swap(na, a);
swap(nb, b);
}
ans += a;
}
printf("%d\n", ans.val);
}
| #include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int n,f[1005]={0,1,1},g;
//A 0 B 1
void upd(int &x,int y){
x=(x+y)%mod;
}
int main() {
cin>>n;
char c;
for(int i=0;i<4;i++)cin>>c,g+=(1<<(3-i))*(c-'A');
if(g==0)cout<<1;
if(g==1)cout<<1;
if(g==2)cout<<1;
if(g==3)cout<<1;
if(g==4){
int a=1;
for(int i=1;i<=n-3;i++)a=a*2%mod;
cout<<a;
}
if(g==5)cout<<1;
if(g==6){
for(int i=3;i<n;i++)f[i]=(f[i-1]+f[i-2])%mod;
cout<<f[n-1];
}
if(g==7)cout<<1;
if(g==8){
for(int i=3;i<n;i++)f[i]=(f[i-1]+f[i-2])%mod;
cout<<f[n-1];
}
if(g==9){
for(int i=3;i<n;i++)f[i]=(f[i-1]+f[i-2])%mod;
cout<<f[n-1];
}
if(g==10){
int a=1;
for(int i=1;i<=n-3;i++)a=a*2%mod;
cout<<a;
}
if(g==11){
int a=1;
for(int i=1;i<=n-3;i++)a=a*2%mod;
cout<<a;
}
if(g==12){
int a=1;
for(int i=1;i<=n-3;i++)a=a*2%mod;
cout<<a;
}
if(g==13)cout<<1;
if(g==14){
for(int i=3;i<n;i++)f[i]=(f[i-1]+f[i-2])%mod;
cout<<f[n-1];
}
if(g==15)cout<<1;
} |
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int main(){
ll n;
cin >> n;
ll bit=1;
rep(i,n){
bit*=2;
}
vector<ll> a(bit),c(bit);
rep(i,bit){
cin >> a.at(i);
}
c=a;
ll ans=0;
for(int i=0;i<n;i++){
vector<ll> b;
for(ll j=0;j<bit;j+=2){
ll p=max(a.at(j),a.at(j+1));
b.push_back(p);
if(i==n-1){
ans=min(a.at(j),a.at(j+1));
}
}
bit/=2;
a=b;
}
ll ct=1;
for(auto q: c){
if(q==ans){
cout << ct;
}
ct++;
}
}
|
#ifdef _DEBUG
#include "../../../library/src/debug_template.cpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif
#include <cassert>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <functional>
using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;
struct init {
init() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} init_;
int main() {
int N;
cin >> N;
N = 1 << N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
auto l = max_element(A.begin(), A.begin() + N / 2);
auto r = max_element(A.begin() + N / 2, A.end());
cout << (*l < *r ? l : r) - A.begin() + 1 << '\n';
return 0;
}
|
#include <numeric>
#include <bits/stdc++.h>
#include <utility>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <math.h>
#include <string>
#define ll long long
#define ull unsigned long long
#define make(type,x) type x; cin>>x
#define make2(type,x,y) type x,y; cin>>x>>y
#define fr(x,y) for(long long x=0;x<y;x++)
#define makevec(x,y,n) vector <x> y(n); for(ll i=0;i<n;i++) cin>>y[i]
#define M (ll)1000000007
#define MM (ll) 1e8
#define INF (ll) 1e18
# define IOS ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
template <typename T>
T modpow(T base, T exp, T modulus) {
base %= modulus;
T result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % modulus;
base = (base * base) % modulus;
exp >>= 1;
}
return result;
}
bool sortbysec(const ll &a, const ll &b){
return abs(a)>abs(b);
}
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;
}
};
struct c_h {
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
x ^= FIXED_RANDOM;
return x ^ (x >> 16);
}
};
ll calcpow(ll x, ll y)
{
ll temp;
if(y == 0)
return 1;
temp = calcpow(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
ll gcd(ll a, ll b){
if(b==0) return a;
return gcd(b,a%b);
}
ll egcd(ll a, ll b, ll &x,ll &y){
if(b==0){
x=1;
y=0;
return a;
}
ll x1;
ll y1;
ll g=egcd(b,a%b,x1,y1);
x=y1;
y=x1-y1*(a/b);
return g;
}
bool isprime(ll a){
bool flag=1;
for(ll i=2;i*i<=a;i++){
if(a%i==0) {flag=0; break;}
}
return flag;
}
ll dx8[8]={1,0,-1,0,1,-1,1,-1};
ll dy8[8]={0,1,0,-1,1,-1,-1,1};
ll dx4[4]={0,0,1,-1};
ll dy4[4]={1,-1,0,0};
const ll N=100;
double dp[N][N][N];
double solve(ll a, ll b, ll c){
if(a==100 || b==100 || c==100) return 0;
if(dp[a][b][c]!=-1) return dp[a][b][c];
dp[a][b][c]=1+(1.0*a/(a+b+c))*solve(a+1,b,c) + (1.0*b/(a+b+c))*solve(a,b+1,c) + (1.0*c/(a+b+c))*solve(a,b,c+1);
return dp[a][b][c];
}
int main(){
IOS;
fr(i,100) fr(j,100) fr(k,100) dp[i][j][k]=-1;
make2(ll,a,b); make(ll,c);
cout<<setprecision(10)<<solve(a,b,c)<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define int long long
#define double long double
#define x first
#define y second
#define pb push_back
#define rt return
using namespace std;
double dp[110][110][110];
double solve(int a,int b,int c){
if (a==100||b==100||c==100) return dp[a][b][c]=0.0;
if (dp[a][b][c]!=-1.0) return dp[a][b][c];
double m1=solve(a+1,b,c)+1.0,m2=solve(a,b+1,c)+1.0,m3=solve(a,b,c+1)+1.0;
double s=0.0+a+b+c;
return dp[a][b][c]=m1*a/s+m2*b/s+m3*c/s;
}
signed main(){
for (int i=0; i<110; i++){
for (int j=0; j<110; j++){
for (int k=0; k<110; k++) dp[i][j][k]=-1.0;
}
}
int a,b,c;
cin>>a>>b>>c;
cout<<fixed<<setprecision(8)<<solve(a,b,c);
} |
/*
AUTHOR: nit1n
CREATED: 21.03.2021 21:41:12
*/
#include<bits/stdc++.h>
#define int long long
using namespace std ;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL) ;
int n ;
cin >> n ;
int l = 0 , h = 1e6 +10 ;
while(l<h){
int mid = (l+h)/2 ;
string s = to_string(mid) + to_string(mid) ;
int m = stoll(s) ;
if( m >=n){
h = mid ;
}else{
l = mid + 1;
}
}
string s = to_string(h) + to_string(h) ;
int m = stoll(s) ;
if(m > n){
h-- ;
}
cout << h ;
}
| #include<bits/stdc++.h>
#define int long long
#define rint regester int
const int maxn = 1e6 + 10;
const int INF = 1e9;
using std::ios;
using std::cin;
using std::cout;
using std::max;
using std::min;
using std::sort;
using std::unique;
using std::lower_bound;
using std::swap;
using std::abs;
using std::acos;
using std::queue;
using std::map;
using std::string;
int read(){int x = 0,f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-')f = -1; ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} return x * f;}
int n, a[maxn], q, k;//find i -->> a_i < k + i < a_{i+1}
signed main(){
n = read(), q = read();
for(int i = 1; i <= n; i++)a[i] = read();
while(q--){
k = read();
int l = 0, r = n, mid, ans = 0;
while(l <= r){
mid = (l + r) >> 1;
if(k + mid <= a[mid]){
r = mid - 1;
}else
if(k + mid >= a[mid + 1]){
l = mid + 1;
}else
{ans = mid; break;}
}
cout << mid + k << "\n";
}
} |
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
#define int long long
//typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
signed main() {
FAST
int n,q;
cin>>n>>q;
vi v(n+1);
for (int i = 1; i <= n; ++i) cin>>v[i];
while (q--) {
int k; cin>>k;
int lo = 1, hi = n, mid,ans=-1;
while (lo <= hi) {
mid = lo + (hi - lo) / 2;
int x=v[mid]-mid;
if(x<k) lo=mid+1;
else {
int y = v[mid - 1] - (mid - 1);
int tmp = k - y;
int z = v[mid] - v[mid - 1] - 1;
if (tmp>=1 and tmp <= z) {
ans=v[mid-1]+tmp;
break;
}
hi=mid-1;
}
}
if(~ans) cout<<ans<<endl;
else {
int yy=v[n]-n;
k-=yy;
cout<<v[n]+k<<endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPS(i, n) for(int i = 1; i <= (n); i++)
#define RREP(i, n) for(int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for(int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const ll INF = 1e9 + 7;
const ll INFL = 1e18;
const double PI = 3.14159265359;
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(a > b) { a = b; return true; } return false; }
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll N, Q; cin >> N >> Q;
vl A(N); REP(i, N) cin >> A[i];
vl K(Q); REP(i, Q) cin >> K[i];
sort(ALL(A));
vl s;
vl t;
t.pb(0);
if(A[0] != 1)
{
s.pb(1);
t.pb(A[0]-1);
}
REP(i, N-1)
{
if(A[i]+1 != A[i+1]) s.pb(A[i]+1), t.pb(A[i+1]-A[i]-1);
}
s.pb(A[N-1]+1);
t.pb((ll)2e18);
REP(i, (int)t.size()-1) t[i+1] += t[i];
REP(i, Q)
{
int idx = lower_bound(ALL(t), K[i]) - t.begin() -1;
cout << s[idx]+K[i]-1-t[idx] << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n, m; cin >> n >> m;
vector<int> H(n), L((n+1)/2), R((n+1)/2);
for (int i = 0; i < n; ++i)
cin >> H[i];
sort(H.begin(), H.end());
for (int i = 0; i+1 < n; i+=2)
L[i/2+1] = L[i/2] + H[i+1] - H[i];
for (int i = n-2; i >= 0; i-=2)
R[i/2] = R[i/2+1] + H[i+1] - H[i];
int ans = INT_MAX;
for (int i = 0; i < m; ++i)
{
int w; cin >> w;
auto it = lower_bound(H.begin(), H.end(), w) - H.begin();
if (it & 1) it ^= 1;
ans = min(ans, L[it/2] + R[it/2] + abs(w - H[it]));
}
cout << ans << endl;
}
| //#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) \
{ \
cout << x << ' '; \
} \
cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
if (b < a)
{
a = b;
return 1;
}
return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
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 main()
{
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
vector<ll> a(n), b(m);
rep(i, n) cin >> a[i];
rep(i, m) cin >> b[i];
sort(ALL(a));
sort(ALL(b));
vector<ll> odd(n), even(n);
ll sumo = 0;
ll sume = 0;
rep(i, n)
{
if (i % 2)
{
sumo += a[i];
}
else
{
sume += a[i];
}
odd[i] = sumo;
even[i] = sume;
}
//debug(odd);
//debug(even);
ll ans = LINF;
rep(i, n)
{
ll sum = 0;
int x = upper_bound(ALL(b), a[i])-b.begin();
if (x == 0)
{
sum += abs(b[x] - a[i]);
}
else if (x == b.size())
{
sum += abs(b[x - 1] - a[i]);
}
else
{
sum += min(abs(b[x] - a[i]), abs(b[x - 1] - a[i]));
}
ll plus = 0;
ll minus = 0;
if (i % 2)
{
minus += even[i - 1];
minus += odd[n - 1] - odd[i];
if (i >= 2)
;
plus += odd[i - 2];
plus += even[n - 1] - even[i - 1];
}
else
{
if (i >= 2)
minus += even[i - 2];
if (i >= 1)
minus += odd[n - 1] - odd[i - 1];
else
{
minus += odd[n - 1];
}
if (i >= 1)
plus += odd[i - 1];
plus += even[n - 1] - even[i];
}
sum += plus - minus;
chmin(ans, sum);
}
cout << ans << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define x1 first
#define y1 second
#define frb(i,a,b) for(ll (i)=(a);(i) <= (b); (i)+=(i)&-(i))
#define rfrb(i,a) for(ll (i)=(a);(i) > 0;(i)-=(i)&-(i))
#define fr(i,a,b) for(ll (i) = (a); (i) <= (b); (i)++)
#define rep(i,c) for(auto (i) : (c))
#define mini(a,b) ((a) < (b)) ? (a) : (b)
#define maxi(a,b) ((a) > (b)) ? (a) : (b)
#define par pair<ll,ll>
#define vp vector<par>
#define vi vector<ll>
const ll mod = 1e9+7;
ll n;
vi g;
map<ll,ll>ar;
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
cout.setf(ios::fixed);cout.precision(0);
cin>>n;
ll a;
g.push_back(0);
fr(i,1,n){
cin>>a;
ar[a]++;
if(ar[a]==1)g.push_back(a);
}
sort(g.begin(),g.end());
ll id = 0,sol=1;
rep(v,g){
sol = ((v-id+1)*(sol))%mod;
sol%=mod;
id = v;
}
cout<<sol;
return 0;
}
| /*
Washief Hossain Mugdho
12 April 2021
AtCoder ARC_116 arc116_c
*/
#ifndef DEBUG
#pragma GCC optimize("O2")
#endif
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define fastio ios_base::sync_with_stdio(0)
#define untie cin.tie(0)
#define rep(i, n) for (int i = 0; i < n; i++)
#define repe(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrepe(i, n) for (int i = n; i > 0; i--)
#define ms(a, b) memset(a, b, sizeof a)
#define MOD 998244353
#define MAX 200005
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
using vi = vector<int>;
using vl = vector<ll>;
using vvb = vector<vector<bool>>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vpii = vector<pair<int, int>>;
using mii = map<int, int>;
using umii = unordered_map<int, int>;
using seti = set<int>;
using useti = unordered_set<int>;
/***********IO Utility**************/
template <typename... ArgTypes>
void print(ArgTypes... args);
template <typename... ArgTypes>
void input(ArgTypes &...args);
template <>
void print() {}
template <>
void input() {}
template <typename T, typename... ArgTypes>
void print(T t, ArgTypes... args)
{
cout << t;
print(args...);
}
template <typename T, typename... ArgTypes>
void input(T &t, ArgTypes &...args)
{
cin >> t;
input(args...);
}
int modPow(int a, int b)
{
int res = 1;
while (b)
{
if (b & 1)
res = 1LL * res * a % MOD;
a = 1LL * a * a % MOD;
b >>= 1;
}
return res;
}
int main()
{
fastio;
#ifdef LOCAL_OUTPUT
freopen(LOCAL_OUTPUT, "w", stdout);
#endif
#ifdef LOCAL_INPUT
freopen(LOCAL_INPUT, "r", stdin);
#endif
int n, m;
cin >> n >> m;
vi fact(n + 20), inv(n + 20);
fact[0] = 1;
repe(i, n + 19) fact[i] = 1LL * fact[i - 1] * i % MOD;
inv[n + 19] = modPow(fact[n + 19], MOD - 2);
rrep(i, n + 19) inv[i] = 1LL * inv[i + 1] * (i + 1) % MOD;
vi spf(m + 1);
repe(i, m)
spf[i] = (i % 2 ? i : 2);
for (int i = 3; i * i <= m; i += 2)
{
if (spf[i] != i)
continue;
for (int j = i * i; j <= m; j += (i << 1))
spf[j] = min(spf[j], i);
}
int res = 0;
repe(i, m)
{
umii facPow;
int now = i;
int ways = 1;
while (now != 1)
{
facPow[spf[now]]++;
now /= spf[now];
}
for (auto x : facPow)
{
ways = 1LL * ways * fact[n - 1 + x.sc] % MOD;
ways = 1LL * ways * inv[n - 1] % MOD;
ways = 1LL * ways * inv[x.sc] % MOD;
}
res = (res + ways) % MOD;
}
cout << res << endl;
}
|
#pragma GCC optimize("O3")
//#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
#define re return
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define make_unique(x) sort(all(x)),x.resize(unique(all(x))-x.begin())
#define fi first
#define se second
#define ss second.second
#define sf second.first
#define ff first.first
#define fs first.second
#define sqrt(x) sqrt(abs(x))
#define mp make_pair
#define PI 3.14159265358979323846
#define E 2.71828182845904523536
#define er erase
#define in insert
#define fo(i,n) for((i)=0;(i)<(n);(i)++)
#define ro(i,n) for((i)=n-1;(i)>=0;(i)--)
#define fr(i,j,n) for((i)=(j);(i)<(n);(i)++)
#define rf(i,j,n) for((i)=((n)-1);(i)>=(j);(i)--)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
void eras(map<int,int> &m,int x)
{
m[x]--;
if (!m[x])
m.erase(x);
}
const int N=(int)3e5+100;
const int M=(int)2e6+100;
const int inf=(int)1e9+100;
#define filename ""
vector<int> prost;
vector<int> v;
bool was[73][(1<<20)];
ll table[73][(1<<20)];
ll f(int x,int mask)
{
if (x==v.size()) re 1;
if (was[x][mask]) re table[x][mask];
was[x][mask]=1;
ll o=f(x+1,mask);
if ((mask&v[x])==0) o+=f(x+1,mask|v[x]);
re table[x][mask]=o;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//freopen(filename".in","r",stdin);
//freopen(filename".out","w",stdout);
//freopen("ans.txt","w",stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
srand(time(0));
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
ll a,b;
int i,j;
for(i=2;i<72;i++)
{
for(j=2;j<i;j++)
{
if (i%j==0) break;
}
if (j==i)
{
prost.pb(j);
}
}
cin>>a>>b;
while(a<=b)
{
int mask=0;
fo(i,prost.size())
{
j=prost[i];
if (a%j==0)
{
mask|=(1<<i);
}
}
v.pb(mask);
a++;
}
cout<<f(0,0);
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int n,k;
cin>>n>>k;
for(int i=0;i<k;i++){
string s = to_string(n);
sort(s.begin(),s.end());
string s1 = s;
sort(s.rbegin(),s.rend());
string s2 = s;
n = stoi(s2) - stoi(s1);
}
cout<<n;
}
|
#include <bits/stdc++.h>
#define f(a, n) for (int a = 0; a < n; a++)
#define F(a, n) for (int a = 1; a <= n; a++)
using namespace std;
/*
We declare an array of size 2^20. We will pretend N = 2^19. (sort of)
The i-th element is stored in A[N+i]. (0 indexed)
for i in [1, N-1], A[i] = A[2i] + A[2i+1].
When we update element i, let j = i + N, then we update A[j], A[j/2], A[j/4],...
When we query for the sum of [1, i], if the last bit is 1, we add A[j]. Either way, we set j = j / 2. Proceed until j == 0.
*/
int A[1<<20];
constexpr int N = 1 << 19;
int sumto(int i){ // sums to i - 1 inclusive
int ret = 0;
int j = i + N;
while (i > 0){
if (i & 1) ret ^= A[j - 1];
i >>= 1;
j >>= 1;
}
//i = 6 = 110, we want [0, 3] + [4, 5] = A[2] + A[6]
//i = 7 = 111, we want A[2]+A[6]+A[14]
//i = 5 = 101, we want A[2]+A[12]
return ret;
}
int main(){
int n, q;
cin >> n >> q;
f(i, n){
cin>>A[i + N];
}
for (int i = N - 1; i > 0; i--){
A[i] = A[2*i] ^ A[2*i+1];
}
f(i, q){
int x, y, z;
cin>>x>>y>>z;
if (x == 1){
int j = y-1 + N;
while (j > 0){
A[j] ^= z;
j >>= 1;
}
}else{
//cout<<sumto(z)<<" "<<sumto(y-1)<<"!"<<endl;
cout << (sumto(z) ^ sumto(y-1)) << endl;
}
}
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type , less<ll> , rb_tree_tag , tree_order_statistics_Node_update>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define inf 3e18
#define mk make_pair
#define ld long double
#define mod 1000000007
#define fi first
#define se second
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test ll t; cin>>t; while(t--)
#define setbits __builtin_popcount
#define endl '\n'
#define LOCAL
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i));}
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define maxn 300001
vector<ll>a(maxn);
ll seg[4*maxn];
void build(ll v,ll l,ll r)
{
if(l==r)
{
seg[v]=a[l];
return;
}
ll mid=(l+r)/2;
build(2*v,l,mid);
build(2*v+1,mid+1,r);
seg[v]=seg[2*v]^seg[2*v+1];
}
void update(ll v,ll l,ll r,ll pos,ll val)
{
if(l==r)
{
seg[v]^=val;
return;
}
ll mid=(l+r)/2;
if(pos<=mid)
{
update(2*v,l,mid,pos,val);
}
else
{
update(2*v+1,mid+1,r,pos,val);
}
seg[v]=seg[2*v]^seg[2*v+1];
}
ll query(ll v,ll l,ll r,ll x,ll y)
{
if(y<x)
{
return 0;
}
if(x<=l && r<=y)
{
return seg[v];
}
ll mid=(l+r)/2,val,val1;
val=query(2*v,l,mid,x,min(y,mid));
val1=query(2*v+1,mid+1,r,max(mid+1,x),y);
return val^val1;
}
int main()
{
fastIO;
ll n,q;
cin>>n>>q;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
build(1,0,n-1);
ll x,y,z,val;
while(q--)
{
cin>>x>>y>>z;
if(x==1)
{
y--;
update(1,0,n-1,y,z);
}
else
{
y--;
z--;
val=query(1,0,n-1,y,z);
cout<<val<<endl;
}
}
} |
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
#define P pair<int,int>
const ll MOD=1e9+7;
const int INF=2e9;
const double PI=acos(-1);
int main() {
ios_base::sync_with_stdio(false);
int n,m;
cin >> n >> m;
vector<vector<P>> r(n);
rep(i,m) {
int u,v,c;
cin >> u >> v >> c;
r[u-1].push_back({v-1,c});
r[v-1].push_back({u-1,c});
}
queue<int> q;
q.push(0);
vector<int> ans(n),visited(n);
ans[0]=1;
visited[0]=1;
while(!q.empty()) {
int p=q.front();
q.pop();
int rs=r[p].size();
rep(i,rs) {
int np=r[p][i].first,c=r[p][i].second;
if (visited[np]==0) {
visited[np]=1;
if (ans[p]==c) ans[np]=c%n+1;
else ans[np]=c;
q.push(np);
}
}
}
bool flag=1;
rep(i,n) {
if (visited[i]!=1) flag=0;
}
if (flag) {
rep(i,n) cout << ans[i] << endl;
} else cout << "No" << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7,INF=1e18;
#define ll long long
#define line cout<<"\n";
#define flush cout<<endl;
#define pll pair<ll,ll>
#define x first
#define y second
#define p_all(arr) for(auto i:arr){cout<<i<<" ";}cout<<"\n";
vector<ll> par,sze,val;
vector<vector<pll> > tree;
ll root(ll u)
{
if(par[u]==u)
return u;
return par[u]=root(par[u]);
}
void union1(ll u,ll v)
{
u=root(u),v=root(v);
if(u==v)
return ;
if(sze[u]<sze[v])
swap(u,v);
sze[u]+=sze[v];
par[v]=u;
}
void dfs(ll u,ll pa,ll value)
{
for(auto v:tree[u])
{
if(v.x!=pa)
{
if(value!=v.y)
{
val[v.x]=v.y;
}
dfs(v.x,u,val[v.x]);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
ll t=1;
// cin>>t;
for(ll test=1;test<=t;test++)
{
ll n,m;
cin>>n>>m;
par.resize(n);
sze.resize(n,1);
tree.resize(n);
val.resize(n,-1);
for(int i=0;i<n;i++)
par[i]=i;
vector<pair<pll,ll> > edges(m);
for(int i=0;i<m;i++)
{
ll u,v,w;
cin>>u>>v>>w;
u--,v--;
edges[i]={{u,v},w};
}
vector<vector<ll> > val1(n);
for(int i=0;i<m;i++)
{
if(root(edges[i].x.x)!=root(edges[i].x.y))
{
union1(edges[i].x.x,edges[i].x.y);
val1[edges[i].x.x].push_back(edges[i].y);
val1[edges[i].x.y].push_back(edges[i].y);
tree[edges[i].x.x].push_back({edges[i].x.y,edges[i].y});
tree[edges[i].x.y].push_back({edges[i].x.x,edges[i].y});
}
}
dfs(0,-1,-1);
for(int i=0;i<n;i++)
{
if(val[i]==-1)
{
sort(val1[i].begin(),val1[i].end());
for(int j=1;j<=n;j++)
{
if(j>val1[i].size()||val1[i][j-1]!=j)
{
val[i]=j;
break;
}
}
}
}
for(int i=0;i<n;i++)
{
cout<<val[i]<<"\n";
}
}
return 0;
} |
// < Rahil Malhotra / ViciousCoder >
#include "bits/stdc++.h"
using namespace std;
template <typename T> void print(T t) { cout<<t<<endl; }
template<typename T, typename... Args> void print(T t, Args... args) { cout<<t<<" "; print(args...); }
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define int long long
#define double long double
int32_t main()
{
IOS;
int n;
cin>>n;
int arr[n][2];
for(int i=0;i<n;i++)
cin>>arr[i][0]>>arr[i][1];
int fl=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
for(int k=j+1;k<n;k++)
{
int a=arr[i][0]*(arr[j][1]-arr[k][1]);
a+=arr[j][0]*(arr[k][1]-arr[i][1]);
a+=arr[k][0]*(arr[i][1]-arr[j][1]);
if(a==0)
fl=1;
}
if(fl)
print("Yes");
else
print("No");
} | //always use
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i=0; i<n; i++)
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long ll;
#define MOD 1000000007
#define HASHMOD 1275341791
//2 147 483 647 (2^31-1) int max
//9 223 372 036 854 775 807 (2^63-1) ll max
//codeforces and USACO
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);}
//USACO
void file_io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();}
//Printing pairs and vectors
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}
//main code
int main(){
fast_io();
int n;
cin >> n;
vector<pair<int,int>>a(n);
forn(i,n){
int x,y;
cin >> x >> y;
a[i]=mp(x,y);
}
bool mybool=false;
forn(i,n){
forn(j,n){
forn(k,n){
if (i==j or i==k or j==k){
continue;
}
vector<pair<int,int>>points;
points.pb(a[i]);
points.pb(a[j]);
points.pb(a[k]);
sort(points.begin(),points.end()); //sorting by increasing x
if (((double)points[2].s-points[0].s)/(points[2].f-points[0].f)==((double)points[1].s-points[0].s)/(points[1].f-points[0].f)){
mybool=true;
}
}
}
}
if (mybool){
cout << "Yes";
}
else{
cout << "No";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int x = (a + b) / 2;
int y = a - x;
cout << x << " " << y << endl;
} | #include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include <functional>
#include<time.h>
#include <stack>
#include<iomanip>
#include<functional>
using namespace std;
#define rep(i,a,n) for(ll i=(a);i<(n);i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans) if(ans)cout<<"Yes"<<endl; else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl; else cout<<"NO"<<endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int,int>
#define Pll pair<llint,llint>
#define keta(a) fixed << setprecision(a)
const ll mod = 1000000007;
//辞書順はnext_permutation( begin( v ), end( v ) );やで!
const ll INF = 1000;
class UnionFind {
public:
//親の番号を格納する
vector<int> par;
//最初は全てが根であるとして初期化
UnionFind(int N) {
par.resize(N);
for (int i = 0; i < N; i++)par[i] = i;
}
//Aがどのグループに属しているか調べる
int root(int A) {
if (par[A] == A) return A;
return par[A] = root(par[A]);
}
//AとBをくっ付ける
bool unite(int A, int B) {
//AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
//すでにくっついてるからくっ付けない
return false;
}
//Bの親をAに変更する
par[B] = A;
return true;
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
template<class T>
class SegTree {
public:
T size;
vector<T> seg; //配列
T e; //単位元
function <T(T, T)> f; //演算
function<T(T, T)> updata; //更新
SegTree(T n, T _e, function<T(T, T)> _f, function<T(T, T)> _updata) { //コンストラクタ(初期化)
f = _f;
e = _e;
updata = _updata;
size = 1;
while (size < n)size *= 2;
seg.resize(size * 2 - 1, e);
}
void change(T n, T k) { //一点更新
n += size - 1;
seg[n] = updata(seg[n],k);
while (n > 0) {
n = (n - 1) / 2;
seg[n] = f(seg[n * 2 + 1], seg[n * 2 + 2]);
}
}
T query(T a, T b, T l, T r, T k) {
if (b <= l || r <= a)return e;
if (a <= l && r <= b)return seg[k];
T v1 = query(a, b, l, (l + r) / 2, k * 2 + 1);
T v2 = query(a, b, (l + r) / 2, r, k * 2 + 2);
return f(v1, v2);
}
};
int main() {
int a, b;
cin >> a >> b;
int ans = a + b;
ans /= 2;
int x = a - ans;
cout << ans <<" "<<x<< endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif
// $ cp-batch VariousDistances | diff VariousDistances.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address VariousDistances.cpp && ./a.out
/*
10/17/2020
4:01-4:04
*/
const int MAX_N=1e6+1;
LL X[MAX_N];
int N;
void solve() {
LL a=0,b=0,c=0;
REP(i,N) a+=abs(X[i]),b+=X[i]*X[i],SMAX(c, abs(X[i]));
cout<<a<<endl;
cout<<sqrt(b)<<endl;
cout<<c<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout<<setprecision(12)<<fixed;
cin>>N;
REP(i,N) cin>>X[i];
solve();
return 0;
}
|
// Problem : B - Various distances
// Contest : AtCoder - AtCoder Beginner Contest 180
// URL : https://atcoder.jp/contests/abc180/tasks/abc180_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// Macros
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int) x.size()
#define mod 1000000007
#define EPS 1e-9
#define inf 1e10
#define PI acos(-1.0)
#define int long long
#define N 100005
// Solution
void solve() {
int n;
cin >> n;
int m = 0, c = 0;
db e = 0;
for(int i=0;i<n;i++){
int x;
cin >> x;
m += abs(x);
c = max(c, abs(x));
e += x*x;
}
e = sqrt(e);
cout << fixed << setprecision(12);
cout << m << '\n';
cout << e << '\n';
cout << c << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
int tests;
tests = 1;
// cin>>tests;
for (int test = 1; test <= tests; test++) {
// cout<<"Case #"<<test<<": ";
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
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
const unsigned long long mod = 1e9 + 7;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
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; }
struct Edge {
long long to;
long long cost;
};
using Graph = vector<vector<Edge>>;
using P = pair<long, int>;
const long long INF = 1LL << 60;
/* dijkstra(G,s,dis)
入力:グラフ G, 開始点 s, 距離を格納する dis
計算量:O(|E|log|V|)
副作用:dis が書き換えられる
*/
ll dijkstra(const Graph &G, int s) {
vector<long long> dis;
int N = G.size();
dis.resize(N, INF);
priority_queue<P, vector<P>, greater<P>> pq; // 「仮の最短距離, 頂点」が小さい順に並ぶ
dis[s] = 0;
pq.emplace(dis[s], s);
bool f = true;
while (!pq.empty()) {
P p = pq.top();
pq.pop();
int v = p.second;
if (dis[v] < p.first) { // 最短距離で無ければ無視
continue;
}
for (auto &e : G[v]) {
if (dis[e.to] > dis[v] + e.cost) { // 最短距離候補なら priority_queue に追加
dis[e.to] = dis[v] + e.cost;
pq.emplace(dis[e.to], e.to);
}
}
if(f){
dis[s] = INF;
f = !f;
}
}
return dis[s];
}
int main(){
int N, M;
cin >> N >> M;
Graph G(N);
vector<ll> self(N, INF);
while(M--){
ll a, b, c;
cin >> a >> b >> c;
--a;
--b;
if(a==b){
self.at(a) = min(self.at(a), c);
continue;
}
Edge e = {b, c};
G.at(a).push_back(e);
}
FOR(i,0,N){
ll g = dijkstra(G, i);
if(g==INF&&self[i]==INF){
cout << -1 << endl;
}else{
ll ans = min(g, self[i]);
cout << ans << endl;
}
}
}
| #pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse4,fma")
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
const int IN_SZ = 20<<20, OUT_SZ = 20<<20;
int _i0, _o0; char _, _i[IN_SZ], _o[OUT_SZ];
template <typename T> inline void re(T &x) {
for(x=_i[_i0++]^48;(_=_i[_i0++])&16;x=x*10+(_^48)); }
template <typename T> inline void wr(T x) {
char s[11];int i=0;do s[i++]='0'+x%10,x/=10; while(x);
while (i--) _o[_o0++]=s[i]; _o[_o0++]='\n'; }
inline void wr(const string &s){for(char c:s)_o[_o0++]=c;}
const int maxn = 2'005;
vector<pii> adj[maxn];
int dis[maxn];
int main() {
fread(_i,1,IN_SZ,stdin);
int N, M; re(N), re(M);
for (int i = 0; i < M; ++i) {
int a,b,c; re(a),re(b),re(c);
adj[--a].emplace_back(--b, c);
}
for (int st = 0; st < N; ++st) {
minpq<pii> pq; pq.emplace(0, st);
for (int i = 0; i < N; ++i) dis[i] = 2e9;
bool is_loop = false;
while (!pq.empty()) {
const auto [d, c] = pq.top(); pq.pop();
if (c == st && d > 0) {
wr(d);
is_loop = true;
break;
}
if (dis[c] <= d) continue;
dis[c] = d;
for (const auto &[n, e]: adj[c]) {
pq.emplace(d+e, n);
}
}
if (!is_loop) {
wr(string("-1\n"));
}
}
fwrite(_o,1,_o0,stdout);
} |
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
// a^n (mod.MOD)を求める。計算量はO(logn)
ll modpow(ll a, ll n, ll MOD = mod) {
if(n == 0) {
return 1;
}
if(n%2 == 1) {
return (a * modpow(a, n-1, MOD)) % MOD;
}
return (modpow(a, n/2, MOD) * modpow(a, n/2, MOD)) % MOD;
}
// nCrをO(r)で求める。
ll Comb(ll n, ll r) {
if(n < r) {
return 0;
}
if(r > n-r) {
return Comb(n, n-r);
}
ll Mul = 1, Div = 1;
rep(i, r) {
Mul *= (n-i); Div *= (i+1);
Mul %= mod; Div %= mod;
}
return Mul * modpow(Div, mod - 2) % mod;
}
int main() {
ll n, m;
cin >> n >> m;
ll s = 0;
rep(i, n) {
ll a;
cin >> a;
s += a;
}
out(Comb(n+m, n+s));
re0;
} | #include <bits/stdc++.h>
using namespace std;
#define flush cout.flush
using ll = int;
using ull = unsigned long long;
using ld = long double;
using pl = pair<ll, ll>;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-9;
const ld PI = acos(-1);
ll n, m;
vector<vector<char>> mx;
vector<char> used(26, 0);
vector<vector<pl>> to(26);
queue<pl> q;
vector<vector<ll>> d;
void go(pl a, pl b) {
if (a.first < 0 || a.first >= n)return;
if (a.second < 0 || a.second >= m)return;
if (mx[a.first][a.second] == '#')return;
if (d[a.first][a.second] != -1)return;
d[a.first][a.second] = d[b.first][b.second] + 1;
q.push(a);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
mx.assign(n, vector<char>(m));
d.assign(n, vector<ll>(m, -1));
pl st, en;
for (ll i = 0; i < n; ++i) {
for (ll j = 0; j < m; ++j) {
cin >> mx[i][j];
if (mx[i][j] == 'S') {
st = {i, j};
}
if (mx[i][j] == 'G') {
en = {i, j};
}
if (mx[i][j] >= 'a' && mx[i][j] <= 'z') {
to[mx[i][j] - 'a'].push_back({i, j});
}
}
}
d[st.first][st.second] = 0;
q.push(st);
while (!q.empty()) {
pl v = q.front();
q.pop();
if (mx[v.first][v.second] >= 'a' && mx[v.first][v.second] <= 'z' && !used[mx[v.first][v.second] - 'a']) {
ll x = mx[v.first][v.second] - 'a';
used[x] = 1;
for (auto t : to[x]) {
go(t, v);
}
}
go({v.first - 1, v.second}, v);
go({v.first + 1, v.second}, v);
go({v.first, v.second - 1}, v);
go({v.first, v.second + 1}, v);
}
cout << d[en.first][en.second] << "\n";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define f(i,n) for(int i=0; i<n; ++i)
#define fi(i,st,n) for(int i=st; i<=n; ++i)
#define veci vector<int>
#define vecp vector<pair<int,int> >
#define vecl vector<ll>
int prime[100000+10];
ll lcm(ll a, ll b) {
return a*b/__gcd(a,b);
}
ll power(ll a, ll n, ll mod) {
ll res = 1;
while(n > 0) {
if(n&1) {
res = (res*a)%mod;
}
a = (a*a)%mod;
n = n/2;
}
return res;
}
ll sum(int arr[], int n) {
ll res = 0;
f(i,n) {
res += arr[i];
}
return res;
}
void seive() {
prime[1] = 0;
for(int i=2; i<=100000; i++) {
prime[i] = 1;
}
for(ll i=2; i<=100000; i++) {
if(prime[i]) {
for(ll j=i*i; j<=100000; j+=i) {
prime[j] = 0;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin>>t;
while(t--){
ll n;
cin>>n;
ll arr[n];
f(i,n) cin>>arr[i];
ll count[200] = {0};
f(i,n) {
count[arr[i]%200]++;
}
ll ans = 0;
for(int i=0; i<200; i++) {
ans += count[i]*(count[i]-1)/2;
}
cout<<ans<<"\n";
}
return 0;
}
| #include <iostream>
#include <vector>
#include <algorithm>
int main(){
long long n, ans = 0;
std::cin >> n;
std::vector <int> a(n);
std::vector <std::vector<int> > list(200, std::vector<int>(0));
for (int i = 0; i < n; ++i){
std::cin >> a[i];
}
for (int i = 0; i < n; ++i){
int k;
k = a[i] % 200;
list[k].push_back(a[i]);
}
// std::sort(a.begin(), a.end());
// for (int i = 0; i < n; ++i){
// for (int j = i+1; j < n; ++j){
// if ((a[j] - a[i]) % 200 == 0) ans++;
// }
// }
for (int i = 0; i < 200; ++i){
long long k = list[i].size();
if (k > 1){
ans += ((k * (k - 1)) / 2);
}
}
std::cout << ans << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e17 + 5);
const int MAX_N = (int)(2e5 + 5);
int n;
ll x[MAX_N];
int c[MAX_N];
ll dp[MAX_N][2]; // dp[i][0]: end color i at left, dp[i][1]: end color i at right
int main(void) {
// Here your code !
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%lld %d", &x[i], &c[i]);
}
set<int> col_set;
for (int i = 0; i < n; ++i) {
col_set.insert(c[i]);
}
int cnt = 0;
unordered_map<int, int> dense;
for (auto c : col_set) {
cnt += 1;
dense[c] = cnt;
}
int last_col = cnt;
for (int i = 0; i < n; ++i) {
c[i] = dense[c[i]];
}
ll most_left[MAX_N];
ll most_right[MAX_N];
fill(most_left, most_left + n + 5, INF);
fill(most_right, most_right + n + 5, -INF);
for (int i = 0; i < n; ++i) {
most_left[c[i]] = min(most_left[c[i]], x[i]);
most_right[c[i]] = max(most_right[c[i]], x[i]);
}
most_left[0] = 0;
most_right[0] = 0;
dp[0][0] = 0;
dp[0][1] = 0;
for (int col = 1; col <= last_col; ++col) {
ll last_right = most_right[col - 1];
ll last_left = most_left[col - 1];
ll cur_right = most_right[col];
ll cur_left = most_left[col];
// last right to current left / right
if (last_right < cur_left) {
dp[col][0] = dp[col - 1][1] + (cur_right - last_right) + (cur_right - cur_left);
dp[col][1] = dp[col - 1][1] + (cur_right - last_right);
} else if (cur_left < last_right && last_right < cur_right) {
dp[col][0] = dp[col - 1][1] + (cur_right - last_right) + (cur_right - cur_left);
dp[col][1] = dp[col - 1][1] + (last_right - cur_left) + (cur_right - cur_left);
} else if (cur_right < last_right) {
dp[col][0] = dp[col - 1][1] + (last_right - cur_left);
dp[col][1] = dp[col - 1][1] + (last_right - cur_left) + (cur_right - cur_left);
}
// last left to current left / right
if (last_left < cur_left) {
dp[col][0] = min(dp[col][0], dp[col - 1][0] + (cur_right - last_left) + (cur_right - cur_left));
dp[col][1] = min(dp[col][1], dp[col - 1][0] + (cur_right - last_left));
} else if (cur_left < last_left && last_left < cur_right) {
dp[col][0] = min(dp[col][0], dp[col - 1][0] + (cur_right - last_left) + (cur_right - cur_left));
dp[col][1] = min(dp[col][1], dp[col - 1][0] + (last_left - cur_left) + (cur_right - cur_left));
} else if (cur_right < last_left) {
dp[col][0] = min(dp[col][0], dp[col - 1][0] + (last_left - cur_left));
dp[col][1] = min(dp[col][1], dp[col - 1][0] + (last_left - cur_left) + (cur_right - cur_left));
}
}
ll cand_last_left = abs(most_left[last_col] - 0) + dp[last_col][0];
ll cand_last_right = abs(most_right[last_col] - 0) + dp[last_col][1];
ll ans = min(cand_last_left, cand_last_right);
printf("%lld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
#define FOR(i,m,n) for(ll i=ll(m); i<ll(n); i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define VI vector<int>
#define VP vector<pair<ll,ll>>
#define VPP vector<pair<int,pair<int,int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i,a) for(auto &i:a)
typedef pair <int, int> P;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = (1 << 30) - 1;
const ll INFL = 1LL << 61;
const ll mod =998244353;
int main() {
int n;
cin >> n;
vector<pair<ll, ll>> p(200005, { INF,-INF });
REP(i, n) {
ll x, c;
cin >> x >> c;
chmin(p[c].first, x);
chmax(p[c].second, x);
}
ll cura = 0;
ll curb = 0;
ll dista = 0;
ll distb = 0;
REP(i, 200005) {
if(p[i].first == INF)continue;
ll distaa = min(dista + abs(p[i].second - cura) + p[i].second - p[i].first,
distb + abs(p[i].second - curb) + p[i].second - p[i].first);
distb= min(distb + abs(p[i].first - curb) + p[i].second - p[i].first,
dista + abs(p[i].first - cura) + p[i].second - p[i].first);
cura = p[i].first;
curb = p[i].second;
dista = distaa;
//if (i < 6)cerr << cura << ' ' << curb << ' ' << dista << ' ' << distb << endl;
}
ll res = 0;
cout << min(abs(cura) + dista, abs(curb) + distb) << endl;
} |
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
int x=0;
bool flag=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag=0;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return (flag?x:~(x-1));
}
int n;
string s;
signed main()
{
cin>>n;
cin>>s;
if(s[0]!=s[s.size()-1])
{
puts("1");
return 0;
}
for(int i=1;i+2<s.size();i++)
{
if(s[0]!=s[i]&&s[i+1]!=s[s.size()-1])
{
puts("2");
return 0;
}
}
puts("-1");
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
int findMaxGCD(int arr[], int n)
{
// Calculating MAX in array
int high = 0;
for (int i = 0; i < n; i++)
high = max(high, arr[i]);
// Maintaining count array
int count[high + 1] = {0};
for (int i = 0; i < n; i++)
count[arr[i]]++;
// Variable to store the
// multiples of a number
int counter = 0;
// Iterating from MAX to 1
// GCD is always between
// MAX and 1. The first
// GCD found will be the
// highest as we are
// decrementing the potential
// GCD
for (int i = high; i >= 1; i--)
{
int j = i;
counter = 0;
// Iterating from current
// potential GCD
// till it is less than
// MAX
while (j <= high)
{
// A multiple found
if(count[j] >=2)
return j;
else if (count[j] == 1)
counter++;
// Incrementing potential
// GCD by itself
// To check i, 2i, 3i....
j += i;
// 2 multiples found,
// max GCD found
if (counter == 2)
return i;
}
}
}
void solve() {
int a, b; cin >> a >> b;
int n = b - a + 1;
int arr[n];
for (int i = a, j = 0; i <= b; i++, j++) arr[j] = i;
cout << findMaxGCD(arr, n) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9 + 7;
int32_t main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
set<int> s;
for(int i=0;i<n;i++){
int x;
cin>>x;
s.insert(x);
}
int prev = 0;
int ans = 1;
for(int i : s){
int extra = i - prev + 1;
ans = (ans*extra)%MOD;
prev = i;
}
cout<<ans<<"\n";
return 0;
} | #pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
// warning: pragmas don't work on USACO and just set to O0
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //gp_hash_table<T, U, chash>;
#include <ext/pb_ds/tree_policy.hpp> //tree<T, U, cmp, rb_tree_tag,tree_order_statistics_node_update>;
using namespace std; using namespace __gnu_pbds;
// shorter template (CF specialization, because compile time better)
inline int RAND(int l, int r) {
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> unifd(l, r); return unifd(mt);
}
template <class T> using pq = priority_queue<T, vector<T>, greater<T>>;
using ll = long long; using db = double; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>;
using vll = vector<ll>; const int xd[4] = {0, 1, 0, -1}, yd[4] = {1, 0, -1, 0};
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define eb emplace_back
#define pb push_back
#define pf push_front
//#define TC
const ll INF = 1e18; const db EPS = 1e-8;
const int MOD = 1e9 + 7, // 998244353
_ = 2e5 + 5;
void solve() {
int n;
cin>>n;
vll a(n);
for(int i=0;i<n;i++) {
cin>>a[i];
}
sort(all(a));
ll ans = 1;
for(int i=1;i<n;i++) {
ll res = (a[i]-a[i-1]+1);
ans *= res;
ans%=MOD;
}
ans *= (a[0]+1);
ans%=MOD;
cout<<ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
cin.tie(0)->sync_with_stdio(0); //cout << setprecision(3) << fixed << showpoint;
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
} |
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
struct state
{
int x;
int y;
int score;
string path;
vector<bool> used;
};
int main()
{
struct timeval tmp_time;
gettimeofday(&tmp_time, NULL);
long long program_init_time = tmp_time.tv_sec*1000000+tmp_time.tv_usec;
long long fin_time, current_time;
fin_time = program_init_time + 1200000;
int si, sj, count;
cin >> si >> sj;
vector<vector<int>> tiling(50,vector<int>(50)), grids(50,vector<int>(50));
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++) cin >> tiling.at(i).at(j);
}
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++) cin >> grids.at(i).at(j);
}
vector<int> dx{1,-1,0,0}, dy{0,0,1,-1};
string dir = "DURL";
vector<bool> used(2500,false);
used.at(tiling.at(si).at(sj)) = true;
queue<state> que;
state now_state, best_state;
now_state.x = si, now_state.y = sj;
now_state.score = grids.at(si).at(sj);
now_state.path = "";
now_state.used = used;
best_state = now_state;
que.push(now_state);
count = 0;
while (!que.empty())
{
count++;
now_state = que.front();
que.pop();
for (int i = 0; i < 4; i++)
{
if (now_state.x+dx.at(i) < 0 || now_state.x+dx.at(i) >= 50 || now_state.y+dy.at(i) < 0 || now_state.y+dy.at(i) >= 50)
continue;
if (now_state.used.at(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i))))
continue;
now_state.score += grids.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i));
now_state.path += dir.at(i);
now_state.used.at(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i))) = true;
now_state.x += dx.at(i), now_state.y += dy.at(i);
if (best_state.score < now_state.score)
best_state = now_state;
que.push(now_state);
now_state.x -= dx.at(i), now_state.y -= dy.at(i);
now_state.score -= grids.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i));
now_state.path.pop_back();
now_state.used.at(tiling.at(now_state.x+dx.at(i)).at(now_state.y+dy.at(i))) = false;
}
if (count % 100 == 0)
{
gettimeofday(&tmp_time, NULL);
current_time = tmp_time.tv_sec*1000000 + tmp_time.tv_usec;
if (current_time > fin_time)
break;
}
}
cout << best_state.path << endl;
}
| // ██████ ██ ██ ██████ ██ ██ ██ ██████
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██ ███ ██ ██ ██████ ██ █████ ██ ██
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
// ██████ ██████ ██ ██ ██ ██ ██ ██████
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
#define P pair<int,int>
const ll MOD=1e9+7;
const int MAX=1e5+10;
const int INF=1e9;
const double PI=acos(-1);
vector<vector<int>> t(50,vector<int>(50)), p(50,vector<int>(50));
vector<vector<P>> ti(2500);
vector<int> di={-1,0,0,1},dj={0,-1,1,0};
string dir="ULRD";
mt19937 mt(1);
pair<int,string> res(int si, int sj, vector<vector<int>> used) {
int sc=p[si][sj];
string ans;
int tmp=t[si][sj];
rep(m,ti[tmp].size()) {
used[ti[tmp][m].first][ti[tmp][m].second]=1;
}
vector<P> tmp_p;
rep(k,4) {
int ni=si+di[k],nj=sj+dj[k];
if (ni>=0 && ni<50 && nj>=0 && nj<50) {
if (used[ni][nj]==0) {
int pp=min(min(49-ni,ni),min(49-nj,nj));
// tmp_p.push_back({pp,k});
// tmp_p.push_back({p[ni][nj],k});
tmp_p.push_back({p[ni][nj]+pp^3,k});
}
}
}
sort(tmp_p.begin(),tmp_p.end(),greater<>());
vector<int> thresh={0,90,95,95};
int max_sc=-1,max_ind=-1;
string max_str="";
rep(m,tmp_p.size()) {
int k=tmp_p[m].second;
int ni=si+di[k],nj=sj+dj[k];
int r=mt()%100;
// cout << r << ' ' << thresh[m] << endl;
if (r>=thresh[m]) {
pair<int,string> tmp=res(ni,nj,used);
int tmp_sc=tmp.first;
if (tmp_sc>max_sc) {
max_sc=tmp_sc;
max_ind=k;
max_str=tmp.second;
}
}
}
if (max_sc==-1) return {0, ""};
else {
sc+=max_sc;
si+=di[max_ind];
sj+=dj[max_ind];
ans.push_back(dir[max_ind]);
ans+=max_str;
}
// cout << "si=" << si << ",sj=" << sj << ans << endl;
return {sc, ans};
}
int main() {
int si,sj;
cin >> si >> sj;
rep(i,50) {
rep(j,50) {
int tmp;
cin >> tmp;
t[i][j]=tmp;
P pp={i,j};
ti[tmp].push_back(pp);
// cout << tmp << ' ' << i << ' ' << j << endl;
}
}
rep(i,50) {
rep(j,50) cin >> p[i][j];
}
string ans="";
int max_score=0;
// cout << "-------------------------------------" << endl;
vector<vector<int>> used(50,vector<int>(50));
pair<int,string> r=res(si,sj,used);
cout << r.second << endl;
return 0;
}
|
#include<iostream>
using namespace std;
typedef long long ll;
long long int gcd(long long a,long long b){
if(b==0){
return a;
}
else{
return gcd(b,a%b);
}
}
long long int lcm(long long a,long long b){
return a/gcd(a,b)*b;
}
int main(){
int a,b;
cin >> a >> b;
int as,bs;
as=a%100%10+a/10%10+a/100;
bs=b%100%10+b/10%10+b/100;
cout << max(as,bs) << endl;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define double long double
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define gcd(x,y) __gcd(x,y)
#define MOD (int)(1e9 + 7)
#define INF (int)9e18
#define ps(x,y) fixed<<setprecision(y)<<x
int fastpow (int a, int b, int m = MOD) { int res = 1; a %= m;
while (b > 0) { if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1; } return res;}
#define inv(a) fastpow(a, MOD - 2)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
int32_t main()
{
fastio;
int a,b;
cin>>a>>b;
int sum1=0,sum2=0;
while(a)
{
sum1+=a%10;
a=a/10;
}
while(b)
{
sum2+=b%10;
b=b/10;
}
if(sum2>sum1)
cout<<sum2<<"\n";
else
cout<<sum1<<"\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(c i) \
{
sim > struct rge
{
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug
{
#ifdef LOCAL
~debug()
{
cerr << endl;
}
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d)
{
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d)
{
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &)
{
ris;
}
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define rfor(n, i) for (ll i = (n - 1); i >= 0; --i)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
#define len(V) (int)V.size()
#define ll long long
#define pb(n) push_back(n)
#define ld long double
#define ff first
#define ss second
#define mp make_pair
#define endl '\n'
#define M_PI 3.14159265358979323846
#define EPS 1e-7
void solve()
{
int n; cin >> n;
if(n < 0) cout << 0;
else cout << n;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
} | #include <bits/stdc++.h>
// #pragma GCC optimize("O3")
#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc int t; cin >> t; for(int tt = 0; tt < t; ++tt)
#define sz(x) (int)(x).size()
#define sort_unique(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()));
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/**
order_of_key(k) : nums strictly smaller than k
find_by_order(k): index from 0
**/
template<typename T> istream& operator>>(istream &is, vector<T> &v) { for(auto &it : v) is >> it; return is; }
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 A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void dbg_out() { cerr << " end.\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
const int N = 3e3 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
/**
Read problem statement carefully
**/
LL dp[N][N]; // dp[i][j], number of ways i, into j pieces
LL arr[N];
void solve(int T){
int n;
cin >> n;
for(int i = 0; i < n; ++i){
cin >> arr[i];
}
dp[0][0] = 1;
// dp[i][j]: use i-1 elements, use j blocks
for(int k = 0; k < n; ++k){ // block cnt
vi mp(k+1);
LL s = 0;
mp[0] = dp[0][k];
for(int i = 0; i < n; ++i){ // element cnt
s += arr[i];
s %= (k+1);
dp[i+1][k+1] = mp[s];
mp[s] = (mp[s] + dp[i+1][k]) % mod;
}
}
LL res = 0;
for(int k = 0; k < n; ++k){
res = (res + dp[n][k+1]) % mod;
}
cout << res << "\n";
}
int main()
{
Fast;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// louisfghbvc
solve(1);
return 0;
}
/**
Enjoy the problem.
**/ |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "atcoder";
int T; cin >> T;
while (T--) {
string t; cin >> t;
int len = t.size();
int ans = INT_MAX, sum = 0;
for (auto cs : s) {
int cnt = 0;
for (auto it = t.begin(); it != t.end(); it++, cnt++) {
if (*it > cs) {
ans = min(ans, sum + cnt);
break;
}
}
cnt = 0;
bool flag = false;
for (auto it = t.begin(); it != t.end(); it++, cnt++) {
if (*it > cs) break;
else if (*it == cs) {
flag = true;
sum += cnt;
t.erase(it);
break;
}
}
if (!flag) break;
}
if (t.size() && len - t.size() == s.size()) ans = min(ans, sum);
printf("%d\n", ans == INT_MAX ? -1 : ans);
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
vi ans;
string a = "atcoder";
rep(tt,T){
string S;
cin >> S;
int an = 0;
rep(i,len(a)){
if(a[i] < S[i]) {
an = 0;
break;
}else if(a[i] == S[i]){
if(i == len(a)-1 && len(a) == len(S)) an = 1;
if(i == len(S)-1 && i >= 1){
an = 1;
break;
}
}else{
if(i >= 2) an = 1;
else{
int aa = infi;
reps(j,i,len(S)){
if(S[j] > 't') {
aa = j-i;
break;
}
}
rep(j,len(S)){
if(S[j] > 'a') {
chmin(aa,j);
break;
}
}
if(aa == infi) an = -1;
else an = aa;
}
break;
}
}
ans.pb(an);
}
rep(i,T) cout << ans[i] << endl;
return 0;
}
|
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int a,b,wk;
cin >> a >> b >> wk;
int w = wk * 1000;
int dif = b - a;
int min = 0;
if(w%b==0){
min = w/b;
}else{
min = w/b + 1;
}
int mmin = b * min - w;
if(mmin > min * dif){
min += 1;
}
int max = w/a;
int mmax = w - a*min;
if(mmax > dif * max){
max -= 1;
}
if(min > max){
cout << "UNSATISFIABLE" << endl;
}else{
cout << min << " " << max << endl;
}
} | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (int)(n); i ++)
vector <int> dy = {0, 1, 0, -1};
vector <int> dx = {1, 0, -1, 0};
const int INF = 1000000000;
const ll INFLL = 100000000000000000;
long long pow(long long x, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret *= x; // n の最下位bitが 1 ならば x^(2^i) をかける
x *= x;
n >>= 1; // n を1bit 左にずらす
}
return ret;
}
int main(){
int x, y, z; cin >> x >> y >> z;
for(int ans = 1; ans <= 10000000; ans ++){
if(x * ans < z * y){
continue;
}
cout << ans - 1 << endl;
return 0;
}
} |
#include <bits/stdc++.h>
#define F first
#define S second
#define MAX 10000003
using namespace std;
#define ll long long int
#include<fstream>
#define MOD 1000000007
#define fast ios_base::sync_with_stdio(false);
#define io cin.tie(NULL);
#define inp(arr,n) for(ll i=0;i<n;i++){cin>>arr[i];}
#define print(arr,n) for(ll i=0;i<n;i++){cout<<arr[i]<<" ";}
ll power(ll a, ll b) { //a^b
ll res = 1;
a = a % MOD;
while (b > 0) {
if (b & 1) {res = (res * a) % MOD; b--;}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
ll multiply(ll a, ll b, ll mod)
{
return ((a % mod) * (b % mod)) % mod;
}
ll func(ll arr[],ll siz)
{
ll sum=0;
ll ma=0;
for(ll i=0;i<siz;i++)
{
sum+=arr[i];
ma=max(sum,ma);
}
return ma;
}
long long gcd(long long int a, long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to return LCM of two numbers
long long lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
std::vector<ll> adj[400005];
std::vector<ll> vis(400005,0);
ll coun;
int f;
void dfs(ll x,ll parent)
{
vis[x]=1;
coun+=1;
for(ll k=0;k<adj[x].size();k++)
{
if(vis[adj[x][k]]==0)
dfs(adj[x][k],x);
else
{
if(adj[x][k]!=parent)
f=1;
}
}
}
int main()
{
ll t;
t=1;
while(t--)
{
ll n;cin>>n;set<ll> s;
for(ll i=0;i<n;i++)
{
ll a,b;
cin>>a>>b;
s.insert(a);
s.insert(b);
adj[a].push_back(b);
adj[b].push_back(a);
}
ll ans=0;
for(auto it=s.begin();it!=s.end();it++)
{
if(vis[*it]==0)
{
coun=0;
f=0;
dfs(*it,-1);
if(f==0)
ans+=coun-1;
else
ans+=coun;
}
}
cout<<ans<<endl;
}
return 0;
} | //DUEL
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#pragma GCC optimize("unroll-loops")
#define shandom_ruffle(a, b) shuffle(a, b, rng)
#define vi vector<int>
#define vl vector<ll>
#define popcnt __builtin_popcount
#define popcntll __builtin_popcountll
#define all(a) begin(a),end(a)
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
int MOD=1000000007;
int MOD2=998244353;
vector<int> bases;
const ll LLINF=1ll<<60;
const char en='\n';
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void yes() {cout<<"YES"<<en; exit(0);}
void no() {cout<<"NO"<<en; exit(0);}
inline int rund() {int x576363482791fuweh=rng();return abs(x576363482791fuweh)%RAND_MAX;}
template<class T>
void prVec(vector<T> w,bool fl=false)
{
cout<<w.size()<<en;
for (int i=0;i<int(w.size())-1;++i) cout<<w[i]<<' ';
if (w.size()) cout<<w[w.size()-1]<<en;
if (fl) cout<<flush;
}
void M998()
{
swap(MOD,MOD2);
}
ll raand()
{
ll a=rund();
a*=RAND_MAX;
a+=rund();
return a;
}
#define rand raand
ll raaand()
{
return raand()*(MOD-7)+raand();
}
template<class T>
vi compress(vector<T>&v)
{
set<T> s;
for (auto a: v) s.insert(a);
vector<T> o(all(s));
vi nv;
for (int i=0;i<(int)v.size();++i) nv.pb(lower_bound(all(o),v[i])-o.begin());
return nv;
}
string to_upper(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
return a;
}
string to_lower(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
return a;
}
ll sti(string a,int base=10)
{
ll k=0;
for (int i=0;i<(int)a.size();++i)
{
k*=base;
k+=a[i]-'0';
}
return k;
}
template<class T>
void eras(vector<T>& a,T b)
{
a.erase(find(a.begin(),a.end(),b));
}
string its(ll k,int base=10)
{
if (k==0) return "0";
string a;
while (k)
{
a.push_back((k%base)+'0');
k/=base;
}
reverse(a.begin(),a.end());
return a;
}
ll min(ll a,int b)
{
if (a<b) return a;
return b;
}
ll min(int a,ll b)
{
if (a<b) return a;
return b;
}
ll max(ll a,int b)
{
if (a>b) return a;
return b;
}
ll max(int a,ll b)
{
if (a>b) return a;
return b;
}
ll gcd(ll a,ll b)
{
if (b==0) return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return a/gcd(a,b)*b;
}
template<class T,class K>
pair<T,K> mp(T a,K b)
{
return make_pair(a,b);
}
inline int mult(ll a,ll b)
{
return (a*b)%MOD;
}
inline int pot(int n,int k)
{
if (k==0) return 1;
ll a=pot(n,k/2);
a=mult(a,a);
if (k%2) return mult(a,n);
else return a;
}
int divide(int a,int b)
{
return mult(a,pot(b,MOD-2));
}
inline int sub(int a,int b)
{
if (a-b>=0) return a-b;
return a-b+MOD;
}
inline int add(int a,int b)
{
if (a+b>=MOD) return a+b-MOD;
return a+b;
}
void ad(int&a,int b)
{
a+=b;
if (a>=MOD) a-=MOD;
}
bool prime(ll a)
{
if (a==1) return 0;
for (int i=2;i<=round(sqrt(a));++i)
{
if (a%i==0) return 0;
}
return 1;
}
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
const int N=400010,M=1<<18;
int t,n,h[N];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
for (int i=0;i<10;++i) bases.push_back(rand()%(MOD-13893829*2)+13893829);
cin>>n;
multiset<int> s;
ll an=0;
for (int i=0;i<2*n;++i) cin>>h[i],an+=h[i];
for (int i=1;i<=n;++i)
{
s.insert(h[n-i]);
s.insert(h[n+i-1]);
an-=*s.begin();
s.erase(s.begin());
}
cout<<an<<en;
}
|
#include <bits/stdc++.h>
#define lc (o<<1)
#define rc ((o<<1)|1)
#define PB push_back
#define MK make_pair
using namespace std;
#define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl
const int maxn = 2e5 + 5;
const int modu = 1e9 + 7; // 1e9 + 7
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
typedef long long LL;
void read(LL &x)
{
x=0;int f=0;char ch=getchar();
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x=f?-x:x;
return;
}
void read(int &x) { LL y; read(y); x = (int)y; }
void read(char &ch) { char s[3]; scanf("%s", s); ch = s[0]; }
void read(char *s) { scanf("%s", s); }
template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); }
char N[maxn];
int k, n;
LL f[maxn][16][2];
int num(char ch) {
if ('0' <= ch && ch <= '9') return ch - '0';
return ch - 'A' + 10;
}
int main() {
// freopen("my.txt", "w", stdout);
read(N, k);
n = strlen(N);
if (n < k) printf("0\n");
else {
int d = num(N[0]), s = 0;
memset(f, 0, sizeof(f));
f[0][1][0] = d-1;
f[0][1][1] = 1;
f[0][0][0] = 1;
s = (1<<d);
for (int i = 0; i < n-1; ++i) {
d = num(N[i+1]);
f[i+1][1][0] = 15;
for (int j = 1; j <= k; ++j) {
f[i+1][j][0] = (f[i+1][j][0] + f[i][j][0]*j) % modu;
for (int l = 0; l < d; ++l) if ((s&(1<<l)))
f[i+1][j][0] = (f[i+1][j][0] + f[i][j][1]) % modu;
if (s&(1<<d))
f[i+1][j][1] = (f[i+1][j][1] + f[i][j][1]) % modu;
if (j+1 <= k) {
f[i+1][j+1][0] = (f[i+1][j+1][0] + f[i][j][0]*(16-j)) % modu;
for (int l = 0; l < d; ++l) if ((s&(1<<l)) == 0)
f[i+1][j+1][0] = (f[i+1][j+1][0] + f[i][j][1]) % modu;
if ((s&(1<<d)) == 0)
f[i+1][j+1][1] = (f[i+1][j+1][1] + f[i][j][1]) % modu;
}
}
s |= (1<<d);
}
printf("%lld\n", (f[n-1][k][0] + f[n-1][k][1])%modu);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RREP(i, n) for(int i=(n);i>=0;--i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, b) for(int i=(a);i>=(b);--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) {
REP(i, SZ(v)) {
if (i) os << " ";
os << v[i];
}
return os;
}
template <class T>
void debug(const vector<T> &v) {
cout << "[";
REP(i, SZ(v)) {
if(i) cout << ", ";
cout << v[i];
}
cout << "]" << endl;
}
template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << p.first << " " << p.second;
}
template <class T, class U>
void debug(const pair<T, U> &p) {
cout << "(" << p.first << " " << p.second << ")" << endl;
}
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 ll MOD998 = 998244353;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int inf = INT_MIN;
const ll linf = LLONG_MIN;
const ld eps = 1e-9;
template<int m>
struct mint {
int x;
mint(ll x = 0) : x(((x % m) + m) % m) {}
mint operator-() const { return x ? m - x : 0; }
mint &operator+=(mint r) {
if ((x += r.x) >= m) x -= m;
return *this;
}
mint &operator-=(mint r) {
if ((x -= r.x) < 0) x += m;
return *this;
}
mint &operator*=(mint r) {
x = ((ll) x * r.x) % m;
return *this;
}
mint inv() const { return pow(m - 2); }
mint &operator/=(mint r) { return *this *= r.inv(); }
friend mint operator+(mint l, mint r) { return l += r; }
friend mint operator-(mint l, mint r) { return l -= r; }
friend mint operator*(mint l, mint r) { return l *= r; }
friend mint operator/(mint l, mint r) { return l /= r; }
mint pow(ll n) const {
mint ret = 1, tmp = *this;
while (n) {
if (n & 1) ret *= tmp;
tmp *= tmp, n >>= 1;
}
return ret;
}
friend bool operator==(mint l, mint r) { return l.x == r.x; }
friend bool operator!=(mint l, mint r) { return l.x != r.x; }
friend ostream &operator<<(ostream &os, mint a) {
return os << a.x;
}
friend istream &operator>>(istream &is, mint &a) {
ll x;
is >> x;
a = x;
return is;
}
};
using Int = mint<MOD>;
ll modpow(ll x, ll n, ll m) {
ll res = 1;
while(n > 0) {
if(n & 1) {
res *= x;
res %= m;
}
x *= x;
x %= m;
n /= 2;
}
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
ll a, b, c; cin >> a >> b >> c;
vll v = {a%10};
ll now = a%10;
while(1) {
now *= a%10;
now %= 10;
if(now == a%10) break;
v.push_back(now);
}
ll val = modpow(b, c, SZ(v));
cout << v[(val+SZ(v)-1)%SZ(v)] << endl;
return 0;
}
|
//#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
const int bm = 200010;
int BIT[bm];
void add(int A) {
while (A <= bm) {
BIT[A]++;
A += A & -A;
}
}
int query(int A) {
int ret = 0;
while (A > 0) {
ret += BIT[A];
A -= A & -A;
}
return ret;
}
int Q[200001], ne[200001], he[200001];
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int H = getint(), W = getint(), M = getint();
int X[200001], Y[200001];
rep1(i, H) X[i] = W + 1;
rep1(i, W) Y[i] = H + 1;
rep(i, M) {
int x = getint(), y = getint();
if (y < X[x]) X[x] = y;
if (x < Y[y]) Y[y] = x;
}
ll kotae = 1;
int k = 1;
for (int i = 2; i <= H; i++) {
if (X[i] == 1) break;
kotae += X[i] - 1;
Q[k] = i;
ne[k] = he[X[i] - 1];
he[X[i] - 1] = k++;
}
int atta = W;
if (X[1] <= atta) atta = X[1] - 1;
for (int i = W; i > atta; i--) {
for (int q = he[i]; q; q = ne[q]) {
add(Q[q]);
}
}
for (int i = atta; i >= 2; i--) {
for (int q = he[i]; q; q = ne[q]) {
add(Q[q]);
}
int are = query(Y[i] - 1);
kotae += Y[i] - 1 - are;
}
printf("%lld\n", kotae);
Would you please 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};
ll N;
ll num;
bool done[25];
ll col[25];
vector<pair<ll, ll> >path;
vector<ll> to[25];
//void dfs1(ll cur) {
//
// queue<ll> Q;
// Q.push(cur);
//
//
// while(!Q.empty()) {
//
//
//
// }
// done[cur] = true;
//
// for(ll next: to[cur]) {
// if(done[next]) continue;
// path.push_back(make_pair(cur, next));
// dfs1(next);
// }
// return;
//}
void dfs0(ll cur) {
queue<pair<ll, ll> > Q;
Q.push(make_pair(-1, cur));
while(!Q.empty()) {
ll f = Q.front().first;
ll t = Q.front().second;
Q.pop();
if(done[t]) continue;
done[t] = true;
if(f != -1)
path.push_back(make_pair(f, t));
for(ll next: to[t]) {
if(done[next]) continue;
Q.push(make_pair(t, next));
}
}
return;
}
bool check() {
rep(p0, N) {
for(ll p1: to[p0]) {
if(col[p0] != -1 && col[p0] == col[p1]) return false;
}
}
return true;
}
void dfs2(ll idx) {
if(idx == path.size()) {
if(check()) {
num++;
}
return;
}
ll F = path[idx].first;
ll T = path[idx].second;
rep(i, 3) {
if(i == col[F]) continue;
col[T] = i;
dfs2(idx+1);
col[T] = -1;
}
}
int main(){
ll M;
cin >> N >> M;
rep(i, M) {
ll a, b;
cin >> a >> b;
a--;b--;
to[a].push_back(b);
to[b].push_back(a);
}
ll ans = 1;
rep(i, N) {
if(done[i]) continue;
path.clear();
dfs0(i);
//cout << "i = " << i << endl;
//for(auto item: path) cout << item.first << " -> " << item.second << endl;
num = 0;
rep(i, N) col[i] = -1;
col[i] = 0;
dfs2(0);
ans *= num * 3;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
long a, b, c, d, e;
cin >> a >> b;
c = b - a;
for(int i = c; i > 0; i--){
d = (a + (i - 1)) / i;
e = b / i;
if(e - d >= 1){
cout << i << endl;
break;
}
}
} | #include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<math.h>
using namespace std;
const int INF = 1e9+5;
#define ll long long int
#define vi vector<int>
#define vb vector<bool>
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int a,b;
cin>>a>>b;
int c = a+b;
if(c>=15 && b>=8)
cout<<1;
else if(c>=10 && b>=3)
cout<<2;
else if(c>=3)
cout<<3;
else
cout<<4;
} |
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1e9+7;
const double pi = acos(-1);
const ll INF = 1LL<<60;
veci dx = {-1,0,1,0};
veci dy = {0,1,0,-1};
int h,w;
void bfs(P s, vector<string> &G, vecvecl &dist, vector<vector<P>> &tel) {
queue<P> q;
q.push(s);
dist[s.first][s.second] = 0;
veci seen(26);
while(!q.empty()) {
P p = q.front();
q.pop();
int y = p.first;
int x = p.second;
if('a' <= G[y][x] && G[y][x] <= 'z') {
if(!seen[G[y][x]-'a']) {
for(P to : tel[G[y][x]-'a']) {
int ny = to.first;
int nx = to.second;
if(y == ny && x == nx) continue;
if(dist[ny][nx] != -1) continue;
dist[ny][nx] = dist[y][x]+1;
q.push({ny,nx});
}
seen[G[y][x]-'a'] = 1;
}
}
for(int dir = 0; dir < 4; dir++) {
int ny = p.first+dy[dir];
int nx = p.second+dx[dir];
if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
if(G[ny][nx] == '#') continue;
if(dist[ny][nx] != -1) continue;
dist[ny][nx] = dist[y][x] + 1;
q.push({ny,nx});
}
}
}
int main() {
cin >> h >> w;
vector<string> G(h);
REP(i,h) cin >> G[i];
vector<vector<P>> tel(26);
int sx,sy,gx,gy;
REP(i,h) REP(j,w) if(G[i][j] == 'S') sx = j, sy = i;
else if(G[i][j] == 'G') gy = i, gx = j;
else if('a' <= G[i][j] && G[i][j] <= 'z') tel[G[i][j]-'a'].push_back({i,j});
vecvecl dist(h,vecl(w,-1));
bfs({sy,sx}, G , dist, tel);
cout << dist[gy][gx] << endl;
}
| #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<climits>
#include<utility>
#include<vector>
#include<queue>
#define int long long
using namespace std;
const int MaxN = 500005;
int n, ans;
char s[MaxN], t[MaxN];
queue<int> pos;
inline int Read(){
int num = 0, op = 1;
char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') op = -1;
ch = getchar();
}
while(isdigit(ch)){
num = num * 10 + ch - '0';
ch = getchar();
}
return num * op;
}
signed main(){
n = Read();
scanf("%s%s", s+1, t+1);
for(int i=1; i<=n; i++)
if(s[i] == '1') pos.push(i);
for(int i=1; i<=n; i++){
if(s[i] == t[i]){
if(s[i] == '1') pos.pop();
continue;
}
else if(s[i] == '1' && t[i] == '0'){
if(pos.empty()){
printf("-1");
return 0;
}
int p1 = pos.front();
pos.pop();
if(pos.empty()){
printf("-1");
return 0;
}
int p2 = pos.front();
pos.pop();
ans += (p2 - p1);
s[p1] = s[p2] = '0';
}
else if(s[i] == '0' && t[i] == '1'){
if(pos.empty()){
printf("-1");
return 0;
}
int p = pos.front();
pos.pop();
ans += (p - i);
s[p] = '0', s[i] = '1';
}
}
printf("%lld", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mk make_pair
#define mod 998244353
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define ff first
#define ss second
#define prec(y,x) fixed<<setprecision(y)<<x
#define inf 1e18
#define pi 3.1415926535
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int a,b,c;
cin>>a>>b>>c;
a=21-a-b-c;
cout<<a;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define pys cout<<"YES"<<endl
#define pyn cout<<"NO"<<endl
#define loop(i,n) for(int i=0;i<n;i++)
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define f first
#define nl cout<<endl
#define s second
typedef vector<int> vi;
typedef long long ll;
void solve(){
int a,b,c;
cin>>a>>b>>c;
int sum=a+b+c;
cout<<21-sum;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
//cin >> t;
while(t--) {
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
#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;
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
int main(){
string X;
cin >> X;
int size = X.size();
string ans = "";
rep(i,0,size){
if(X[i] == '.'){
break;
}else{
ans += X[i];
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
long long int mod = 998244353;
typedef long long int lli;
typedef pair<lli,lli> pii;
typedef vector<int> vi;
typedef vector<lli> vlli;
typedef vector<bool> vb;
typedef unsigned long long int ulli;
long long int max(long long int a,long long int b){
if(a>b)
return a;
else
return b;
}
long long int min(long long int a,long long int b){
if(a<b)
return a;
else
return b;
}
long long int 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;
}
long long binpow(long long a, long long b) {
if (b == 0)
return 1;
long long res = binpow(a, b / 2);
if (b % 2)
return res * res * a;
else
return res * res;
}
/*
DONT SUBMIT WITHOUT PROOF
*/
//############################ENDOFTEMPLATE############################
struct FenwickTree {
vector<int> bit; // binary indexed tree
int n;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
void solve(){
int n;
cin>>n;
vi a(n);
for(int i = 0;i<n;i++){
cin>>a[i];
}
FenwickTree ft(n);
lli inv = 0;
for(int i = n-1;i>=0;i--){
inv+= (lli)ft.sum(0,a[i]);
ft.add(a[i],1);
}
vlli ans(n);
ans[0] = inv;
for(int i = 1;i<n;i++){
inv-=a[i-1];
inv+=n-1-a[i-1];
ans[i] = inv;
}
for(int i = 0;i<n;i++){
cout<<ans[i]<<endl;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
//#ifndef ONLINE_JUDGE
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//#endif
int t = 1;
//cin>>t;
for(int zz = 1;zz<=t;zz++){
//cout<<"Case #"<<zz<<":"<<" ";
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using Vec = vector<int>;
using Graph = vector<vector<int>>;
int MOD=1000000007;
ll ll_max=9223372036854775807;
bool IsPrime(int num)
{
if (num < 2) return false;
else if (num == 2) return true;
else if (num % 2 == 0) return false;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2)
{
if (num % i == 0)
{
// 素数ではない
return false;
}
}
// 素数である
return true;
}
int Gcd(int x,int y) //最大公約数
{
int large = max(x,y);
int small = min(x,y);
while(small!=0){
int amari =large%small;
large = small;
small = amari;
}
return large;
}
vector<bool> seen;
void dfs(const Graph &G, int v) //深さ優先探索
{ seen[v] = true;
for (auto next_v : G[v]) {
if (seen[next_v]) continue;
dfs(G, next_v); // 再帰的に探索
}
}
ll combi(ll n, ll r){
if (r == 0) {
return 1;
}
return (n - r + 1) * combi(n, r - 1) / r;
}
//メイン関数--------------------------------------------
int main(){
int n;
cin>>n;
vector<string> v(n);
for(int i=0;i<n;i++){
string s;
cin>>s;
if(s[0]=='!'){
s.erase(0,1);
s.push_back('A');
}
v[i]=s;
}
sort(v.begin(),v.end());
bool x=true;
for(int i=0;i<n-1;i++){
if(v[i]+'A'==v[i+1]){
cout<<v[i]<<endl;
x=false;
break;
}
}
if(x) cout<<"satisfiable"<<endl;
}
| /*ver 7*/
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : v)
#define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO" )
#define Yes(n) ((n) ? "Yes" : "No" )
#define yes(n) ((n) ? "yes" : "no" )
int main(){
init();
set<string> st;
inl(n);
rep(i,n){
ins(s);
st.insert(s);
}
for(auto &x:st){
if(x[0]!='!')continue;
if(st.count(x.substr(1))==1)die(x.substr(1));
}
out("satisfiable");
return 0;
} |
#include<iostream>
#include<array>
#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<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#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 Per(i,sta,n) for(int i=n-1;i>=sta;i--)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
int n;
int a[2010];
map<int,int> ma;
template<typename T>
vector<T> divisor_list(T x){
vector<T> res;
for(T i=1;i*i<=x;i++){
if (x%i==0) {
res.push_back(i);
if (i*i!=x) res.push_back(x/i);
}
}
return res;
}
int gcd(int a,int b){
if(a<b)swap(a,b);
if(b==0)return a;
return gcd(b,a%b);
}
void solve(){
cin >> n;
int minA=mod;
rep(i,n) {
cin >> a[i];
chmin(minA,a[i]);
}
rep(i,n){
vector<int> ds=divisor_list(a[i]);
for(int d:ds){
ma[d]=gcd(ma[d],a[i]);
}
}
int ans=0;
for(auto p:ma){
if(p.first==p.second && p.first<=minA) ans++;
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | /*
Author: Nguyen Tan Bao
Status:
Idea:
*/
#include <bits/stdc++.h>
#define FI first
#define SE second
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define TRAV(x, a) for (auto &x : a)
using namespace std;
using ll = long long; using ld = double;
using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<ld, ld>;
using cd = complex<ld>; using vcd = vector<cd>;
using vi = vector<int>; using vl = vector<ll>;
using vd = vector<ld>; using vs = vector<string>;
using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; // vector<pair>
template<class T> using min_pq = priority_queue<T, vector<T>, greater<T> >;
template<class T> inline int ckmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; }
template<class T> inline int ckmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sort(ALL(v)); v.erase(unique(ALL(v)), end(v)); }
constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set
constexpr int bits(int x) { return x == 0 ? 0 : 31-__builtin_clz(x); } // floor(log2(x))
constexpr int p2(int x) { return 1<<x; }
constexpr int msk2(int x) { return p2(x)-1; }
ll ceilDiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
ll floorDiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down
void setPrec(int x) { cout << fixed << setprecision(x); }
// TO_STRING
#define ts to_string
string ts(char c) { return string(1, c); }
string ts(const char* s) { return (string) s; }
string ts(string s) { return s; }
string ts(bool b) { return ts((int)b); }
template<class T> string ts(complex<T> c) { stringstream ss; ss << c; return ss.str(); }
template<class T> using V = vector<T>;
string ts(V<bool> v) {string res = "{"; FOR(i,0,SZ(v)-1) res += char('0'+v[i]); res += "}"; return res; }
template<size_t sz> string ts(bitset<sz> b) { string res = ""; FOR(i,0,SZ(b)-1) res += char('0'+b[i]); return res; }
template<class T, class U> string ts(pair<T,U> p);
template<class T> string ts(T v) { // containers with begin(), end()
bool fst = 1; string res = "";
for (const auto& x: v) { if (!fst) res += " "; fst = 0; res += ts(x); }
return res;
}
template<class T, class U> string ts(pair<T,U> p) { return "("+ts(p.FI)+", "+ts(p.SE)+")"; }
// OUTPUT
template<class T> void pr(T x) { cout << ts(x); }
template<class T, class ...U> void pr(const T& t, const U&... u) { pr(t); pr(u...); }
void ps() { pr("\n"); } // print w/ spaces
template<class T, class ...U> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); }
// DEBUG
void DBG() { cerr << "]" << endl; }
template<class T, class ...U> void DBG(const T& t, const U&... u) { cerr << ts(t); if (sizeof...(u)) cerr << ", "; DBG(u...); }
#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \
<< __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0);
const ld PI = acos(-1.0);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
const ld EPS = 1e-9;
const ll MODBASE = 1000000007LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 110;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
ll r, x, y;
cin >> r >> x >> y;
r = r * r;
ll len = x * x + y * y;
if (len < r) {
cout << 2;
return 0;
}
ll res = 1;
while (r * res * res < len) {
res++;
}
cout << res;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int A = 1000000000;
void read(long long &x) {
double s;
cin >> s;
x = round(s * 10000);
}
long long sqrtll(long long x) {
long long l = 0, r = A;
while (l < r) {
long long m = (l + r + 1) / 2;
if (m * m <= x) {
l = m;
} else {
r = m - 1;
}
}
return r;
}
long long floor_div(long long x, long long y) {
return x / y - (x % y < 0);
}
long long ceil_div(long long x, long long y) {
return x / y + (x % y > 0);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long x, y, r;
read(x), read(y), read(r);
long long ans = 0;
for (long long i = -2 * A; i <= 2 * A; i += 10000) {
long long s = r * r - (x - i) * (x - i);
if (s < 0) {
continue;
}
s = sqrtll(s);
ans += floor_div(y + s, 10000) - ceil_div(y - s, 10000) + 1;
}
cout << ans << "\n";
}
| #include <bits/stdc++.h>
#define lli long long int
#define pb push_back
#define mod 1000000007
#define inf 1000000000000000000
#define mp make_pair
using namespace std;
int main()
{
cout.precision(16);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long double x,y,r;
cin>>x>>y>>r;
lli start=ceil(x-r), rear=floor(x+r), ans=0;
for(lli i=start; i<=rear; i++)
{
long double z=y+sqrt(r*r-(i-x)*(i-x)+1e-9);
long double l=y-sqrt(r*r-(i-x)*(i-x)+1e-9);
ans+=(floor(z)-ceil(l)+1);
}
cout<<ans;
return 0;
} |
#include"bits/stdc++.h"
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) int((a).size())
#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)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int LARGE_INT = 1e9+100;
const int INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const int MOD = 1e9+7;
//debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
LL modpow(LL a, LL n) {
LL res = 1;
while (n > 0) {
if (n & 1) res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
void Main()
{
int h,w;cin>>h>>w;
VS s(h);
REP(i,h){
cin>>s[i];
}
LL result = 0;
REP(i,h){
REP(j,w){
if(i+1 < h){
if(s[i][j] == '.' && s[i+1][j] == '.'){
result++;
}
}
if(j+1 < w){
if(s[i][j] == '.' && s[i][j+1] == '.'){
result++;
}
}
}
}
cout<<result<<endl;
return;
}
int main()
{
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
ll MOD = 1000000007;
int main(){
int H,W;
cin>>H>>W;
vector<vector<char>> s(H,(vector<char>(W)));
for(int i=0;i<H;i++) for(int j=0;j<W;j++) cin>>s[i][j];
vector<vector<ll>> dp(H,(vector<ll>(W)));
vector<vector<ll>> dpx(H,(vector<ll>(W)));
vector<vector<ll>> dpy(H,(vector<ll>(W)));
vector<vector<ll>> dpxy(H,(vector<ll>(W)));
dp[H-1][W-1] = 1;
for(int i=H-1;i>=0;i--){
for(int j=W-1;j>=0;j--){
if(s[i][j]=='#') continue;
dp[i][j] += (dpx[i][j]%MOD + dpy[i][j]%MOD + dpxy[i][j]%MOD)%MOD;
if(j!=0) dpx[i][j-1] = dpx[i][j]+dp[i][j];
if(i!=0) dpy[i-1][j] = dpy[i][j]+dp[i][j];
if(i!=0&&j!=0) dpxy[i-1][j-1] = dpxy[i][j]+dp[i][j];
}
}
cout<<dp[0][0]<<"\n";
/*
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
printf("%11lld",dp[i][j]);
}
cout<<"\n";
}
*/
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)
template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}
const char newl = '\n';
array<array<ll,1100000>,2> dp0,dp1;
int main() {
int n,k,mod,m = 510000,z = 8;
cin >> n >> k >> mod;
vector<int> ans(n+1);
for (int x = 0;x < ((n+1)/2+z-1)/z;++x) {
int cnt[200];
for (int i = 0;i < 200;++i) cnt[i] = 0;
int s = min(z,(n+1)/2-x*z);
for (int i = 1;i <= s;++i) for (int j = 1;j <= n;++j) cnt[j-(x*z+i)+100]++;
dp0[0].fill(0);
dp0[1].fill(0);
dp0[0][m] = k+1;
int p = 0,l = m,h = m;
for (int i = 1;i <= n;++i) {
if (cnt[i+100] == s) {
h += i*k;
for (int j = l;j <= h-i;++j) (dp0[(p)&1][j+i] += dp0[(p)&1][j]) %= mod;
for (int j = l;j <= h;++j) (dp0[(p+1)&1][j] = dp0[(p)&1][j]-dp0[(p)&1][j-i*(k+1)]) %= mod;
p++;
}
if (cnt[-i+100] == s) {
l -= i*k;
for (int j = l;j <= h+i*k;++j) (dp0[(p)&1][j+i] += dp0[(p)&1][j]) %= mod;
for (int j = l;j <= h;++j) (dp0[(p+1)&1][j] = dp0[(p)&1][j+i*k]-dp0[(p)&1][j-i]) %= mod;
for (int j = h;j <= h+i*(k+1);++j) dp0[(p)&1][j+i] = 0;
p++;
}
}
for (int y = x*z+1;y <= x*z+s;++y) {
dp1[1].fill(0);
dp1[0] = dp0[p&1];
P a[100];
for (int i = 1;i <= n;++i) a[i-1] = {abs(i-y),i-y};
sort(a,a+n);
int ll = l,hh = h,pp = 0;
for (int i = 0;i < n;++i) {
if (cnt[a[i].second+100] == s) {
continue;
} else if (a[i].second > 0) {
hh += a[i].first*k;
for (int j = ll;j <= hh-a[i].first;++j) (dp1[(pp)&1][j+a[i].first] += dp1[(pp)&1][j]) %= mod;
for (int j = ll;j <= hh;++j) (dp1[(pp+1)&1][j] = dp1[(pp)&1][j]-dp1[(pp)&1][j-a[i].first*(k+1)]) %= mod;
} else {
ll -= a[i].first*k;
for (int j = ll;j <= hh+a[i].first*k;++j) (dp1[(pp)&1][j+a[i].first] += dp1[(pp)&1][j]) %= mod;
for (int j = ll;j <= hh;++j) (dp1[(pp+1)&1][j] = dp1[(pp)&1][j+a[i].first*k]-dp1[(pp)&1][j-a[i].first]) %= mod;
for (int j = hh;j <= hh+a[i].first*k;++j) dp1[(pp)&1][j+a[i].first] = 0;
}
pp++;
}
ans[y] = ans[n-y+1] = (dp1[(pp)&1][m]-1+mod)%mod;
}
}
for (int i = 1;i < n+1;++i) cout << ans[i] << newl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;
int MOD = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%MOD+MOD)%MOD){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD-a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime MOD
mint inv() const { return pow(MOD-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
// combination MOD prime
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[n-k];
}
}; //comb(10000007); // ←MOD-1が上限
// comb(5, 2) → 10, comb.p(5, 2) → 20
// comb.fact[4] → 24, 2のN乗 → mint(2).pow(N)
int main() {
int N, K, M;
cin >> N >> K >> M;
int MX = (101) * 100 / 2 * 100;
MOD = M;
vector<vector<mint>> dp(101, vector<mint>(MX+1));
dp[0][0] = 1;
for (int i = 1; i <= N; i++) {
// i個目まで使う
for (int j = 0; j <= MX; j++) {
// 合計がjになる個数
dp[i][j] = dp[i-1][j]; // i個目を使わない場合
if (j >= i) {
dp[i][j] += dp[i][j-i];
}
if (j >= i * (K+1)) {
dp[i][j] -= dp[i-1][j - i * (K+1)];
}
}
}
for (int i = 1; i <= N; i++) {
mint ans = K;
int left = i - 1;
int right = N - i;
for (int j = 1; j < MX; j++) {
ans += dp[left][j] * dp[right][j] * (K+1);
}
printf("%d\n", ans);
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(0);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll x,y; cin >> x >> y;
ll a = min(x,y);
ll b = max(x,y);
if((a+3)>b) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
} | // Author :- Sarvesh Chaudhary
#include<bits/stdc++.h>
using namespace std;
#define w(x) int x; cin>>x; while(x--)
#define ll long long int
#define sidha(i,a,b) for(int i=a;i<b;i++)
#define set_built(x) __builtin_popcountll(x)
#define zero_built(x) __bultin_ctzll(x)
#define pairi pair<int,int>
#define vi vector<int>
#define mod 1000000007
#define MAX INT_MAX
#define MIN INT_MIN
#define pb push_back
#define po pop_back
#define all(c) c.begin(),c.end()
#define io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//Global Variable
map<int,int> m;
//Functions
//Driver Function
int main(){
io;
ll a,b;cin>>a>>b;
if(a>b){
swap(a,b);
}
if((a+3)>b){
cout<<"Yes"<<endl;
}
else {
cout<<"No"<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll m = 1e9 + 7;
ll mpow(ll a, ll n) {
ll ret = 1;
while (n) {
if (n % 2) {
ret *= a;
ret %= m;
}
a = (a * a) % m;
n /= 2;
}
return ret;
}
int main () {
int N, M, K;
cin >> N >> M >> K;
if (N - M > K) {
cout << 0 << endl;
return 0;
}
ll kai[2000004];
ll fkai[2000002];
kai[0] = 1;
for (int i = 1; i <= N + M; i ++) {
kai[i] = kai[i - 1] * (ll)i;
kai[i] %= m;
}
fkai[N + M] = mpow(kai[N + M], m - 2);
for (int i = N + M; i > 0; i --) {
fkai[i - 1] = fkai[i] * (ll)i;
fkai[i - 1] %= m;
}
ll ans = (kai[N + M] * fkai[M]) % m;
ans *= fkai[N];
ans %= m;
if (K >= N) {
cout << ans << endl;
return 0;
}
ll kj = kai[N + M] * fkai[M + K + 1];
kj %= m;
kj *= fkai[N -K - 1];
kj %= m;
ans -= kj;
if (ans < 0) ans += m;
cout << ans << 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)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
return ModInt(u); }
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
typedef long long ll;
int N;
vector<pair<int, ll>> E[301010];
//---------------------------------------------------------------------------------------------------
ll xo[301010];
void dfs(int cu, int pa = -1) {
fore(p, E[cu]) if (p.first != pa) {
xo[p.first] = xo[cu] ^ p.second;
dfs(p.first, cu);
}
}
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N;
rep(i, 0, N - 1) {
int a, b; ll c; cin >> a >> b >> c;
a--; b--;
E[a].push_back({ b, c });
E[b].push_back({ a, c });
}
dfs(0);
mint ans = 0;
rep(d, 0, 71) {
mint a = 1;
ll mask = 1;
rep(i, 0, d) a *= 2, mask *= 2;
mint cnt0 = 0, cnt1 = 0;
rep(i, 0, N) {
if (xo[i] & mask) cnt1 += 1;
else cnt0 += 1;
}
mint cnt = 0;
rep(i, 0, N) {
if (xo[i] & mask) cnt += cnt0;
else cnt += cnt1;
}
cnt /= 2;
ans += a * cnt;
}
cout << ans << endl;
} |
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#define rep(i,n) for(ll i = 0; i < (n); i++)
using ll = long long;
using namespace std;
int main(){
ll n, j = 0;
cin >> n;
vector<ll>a;
ll b = (ll)sqrt(n);
if(n == 1){
cout << 1 << endl;
return 0;
}else if(n == 2){
cout << 1 << endl << 2 << endl;
return 0;
}else if(n == 3){
cout << 1 << endl << 3 << endl;
return 0;
}else if(n == 4){
cout << 1 << endl << 2 << endl << 4 << endl;
return 0;
}
rep(i,b){
if(n % (i+1) == 0){
a.push_back(i+1);
if((i+1) * (i+1) != n) a.push_back(n / (i+1));
}
}
sort(a.begin(), a.end());
rep(i,a.size()) cout << a[i] << endl;
return 0;
} | #include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <utility>
#include <numeric>
#include <queue>
#include <stack>
#include <functional>
#include <iomanip>
using ll = long long;
using namespace std;
constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);
template<typename T>
vector<T> Divisors(T n) {
vector<T> ret;
for(T i = 1; i * i <= n; ++i) {
if( n % i == 0 ) {
ret.push_back(i);
if( i < n / i ) ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end());
return ret;
}
int main(void) {
ll n;
cin >> n;
vector<ll> Div = Divisors(n);
for(auto& ans : Div) {
cout << ans << endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
const int MOD = 1000000007;
int main() {
int N, P;
cin >> N >> P;
int dgt = (int)log2(N);
long long ans = 1;
vector<long long> modulo(40);
modulo.at(0) = 1;
modulo.at(1) = P - 2;
for (int i = 2; i < 40; i++) {
modulo.at(i) = (modulo.at(i - 1) * modulo.at(i - 1)) % MOD;
}
/*for (int i = 0; i < 40; i++) {
cout << i << " " << modulo.at(i) << endl;
}*/
for (int i = 0; i < dgt+1; i++) {
if (((N-1) >> i) & 1 == 1) {
ans *= modulo.at(i+1);
ans %= MOD;
}
}
ans *= P - 1;
ans %= MOD;
cout << ans << endl;
} | #include<iostream>
#define mod 1000000007
long long pow(long long x, long long n);
using namespace std;
int main(){
long long N,P,res;
cin >> N>>P;
res=pow(P-2,N-1);
res*=P-1;
res%=mod;
cout << res << endl;
}
long long pow(long long x, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret = ret * x %mod;
x = x * x %mod;
n >>= 1;
}
return ret;
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_multiset tree<long long , null_type,less_equal<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_set tree<long long , null_type,less<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define fill(arr,c) memset(arr,c,sizeof(arr))
#define perm(r) next_permutation(all(r))
//it gives bool value ans permute the string lexoggraphically
#define sortdesc greater<int>()
#define ll long long int
//std::setfill('0') << std::setw(5) << 25; it adds leaing zeroes;
#define f(i,a) for(int i=0;i<a;i++)
#define fo(i,a) for(int i=1;i<=a;i++)
#define foo(i,x,n,c) for(ll i=x;i<n;i+=c)
#define foi(i,x,n,c) for(ll i=x;i>=n;i+=c)
#define fauto(i,m) for(auto i:m)
#define forall(a) for( auto itr=a.begin();itr!=a.end();itr++)
#define ld long double
#define in push_back
#define mll map<ll,ll>
#define mcl map<char,ll>
#define msl map<string,ll>
#define vi vector<int >
#define vl vector<ll >
#define vp vector< pair<int,int> >
#define vs vector <string>
#define vc vector <char>
#define o(a) cout<<a
#define os(a) cout<<a<<" "
#define en cout<<"\n";
#define testcase ll t ;cin>>t; while(t--)
#define ff first
#define ss second
#define pl pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a).begin(), (a).end()
#define sz(a) (a).size()
#define prec(n) fixed<<setprecision(n)
#define mp make_pair
#define bitcount __builtin_popcountll
#define imin o("imin");en;
using namespace std;
bool comp(const pair<int,int> &a , const pair<int,int> &b){
if(a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
bool comps(const pair<int,int> &a , const pair<int,int> &b){
if(a.second == b.second) return a.first< b.first;
return a.second < b.second;
}
//ll pi=acos(0.0)*2;
void fastio()
{
ios::sync_with_stdio(0);
cin.tie(0);
}
void inout()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main()
{
fastio();
vl l;
string s;
cin>>s;
ll sum=0;
ll a[3];
f(i,3)
a[i]=0;
f(i,sz(s))
{
sum+=s[i]-'0';
a[(s[i]-'0')%3]++;
}
ll ans=-1;
if(sum%3==0)
ans=0;
else if(sum%3==1)
{
if(a[1]>=1)
ans=1;
else if(a[2]>=2)
ans=2;
}
else if(sum%3==2)
{
if(a[2]>=1)
ans=1;
else if(a[1]>=2)
ans=2;
}
if(ans<sz(s))
o(ans);
else
o(-1);
return 0;
}
| #include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll,ll>
#define mod 998244353
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fb(i,a,b) for(ll i=a;i>b;i--)
#define ff first
#define ss second
#define srt(v) if(!v.empty())sort(v.begin(),v.end())
#define rev(v) if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
ll res=1;
while(b!=0)
{
if(b&1)
{
res=(res*a)%mod;
}
a=(a*a)%mod;
b/=2;
}
return res;
}
ll inv_mod(ll x){
return pow_mod(x,mod-2);
}
const ll N=(ll)3e5;
ll inv[N];
ll fac[N];
ll ncr(ll n,ll r){
if(n<r)
return 0;
if(n==r||r==0)
return 1LL;
ll x=fac[n];
ll y=inv[n-r];
ll z=inv[r];
return ((x*y)%mod*z)%mod;
}
void solve()
{
ll n;
cin>>n;
string s[3];
for(ll i=0;i<3;i++)
cin>>s[i];
for(ll i=0;i<3;i++)
s[i]+=s[i];
string ans;
/*
1
0101
0101
1010
=> ans=010
01010101
00110011
11001100
=> ans=11011
10011001
01100110
=> ans=10000
A binary string of length 2*N can be easily constructed easily
by taking all the 1's or 0's.
*/
for(ll i=0;i<n;i++)
cout<<"0";
for(ll i=0;i<n;i++)
cout<<"1";
cout<<"0";
cout<<endl;
}
int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//Start from Here.
ll t;
t=1;
cin>>t;
while(t--)
solve();
fac[0]=1;
for(ll i=1;i<N;i++)
fac[i]=(i*fac[i-1])%mod;
for(ll i=0;i<N;i++)
inv[i]=inv_mod(fac[i]);
//Good Bye!
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; // その文字に割り当てられたものを何倍するか
set<char> heads; // 先頭の文字
rep(i, 3) {
heads.insert(s[i][0]);
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; // 各文字に何の文字入れるか
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;
if (heads.count(x.first) && p[now] == 0) sum = 1e18; // 先頭は0じゃダメ
Mp2[x.first] = p[now];
now++;
}
if (sum == 0) {
rep(i, 3) {
ll ans = 0;
rep(j, sz(s[i])) {
ans *= 10;
ans += Mp2[s[i][j]];
}
cout << ans << endl;
}
return 0;
}
Mp2.clear();
} while (next_permutation(all(p)));
cout << "UNSOLVABLE" << endl;
return 0;
} | //#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4", "avx")
#ifdef __APPLE__
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstdint>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>
# include <tuple>
# include <unordered_map>
# include <unordered_set>
# include <thread>
#else
# include <bits/stdc++.h>
#endif
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int max_n = 100+10, inf = 1000111222;
int dp[2*max_n];
bool is_start[2*max_n];
bool is_end[2*max_n];
bool is_pair_present[2*max_n][2*max_n];
bool in_some_pair[2*max_n];
void add_start(int a)
{
if (is_start[a] || is_end[a]){
cout<<"No"<<"\n";
exit(0);
}
is_start[a]=1;
}
void add_end(int b)
{
if (is_start[b] || is_end[b]){
cout<<"No"<<"\n";
exit(0);
}
is_end[b]=1;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n;
cin>>n;
for (int i=1;i<=n;i++){
int a,b;
cin>>a>>b;
if (a==-1 && b==-1){
}
else if (a==-1){
add_end(b);
}
else if (b==-1){
add_start(a);
}
else{
if (a>=b){
cout<<"No"<<"\n";
exit(0);
}
add_start(a);
add_end(b);
is_pair_present[a][b]=1;
in_some_pair[a]=1;
in_some_pair[b]=1;
}
}
dp[0]=1;
for (int f=0;f<2*n;f++){
if (dp[f]){
// cout<<"dp f :: "<<f<<"\n";
int l=f+1;
for (int r=l+1;r + ((r-l)-1)<=2*n;r++){
bool ok=1;
for (int i=l;i<r;i++){
if (is_pair_present[i][r+(i-l)]){
continue;
}
// cout<<i<<" "<<is_start[i]<<" "<<r+(i-l)<<" "<<is_end[r+(i-l)]<<"\n";
if (is_start[i] && is_end[r+(i-l)]){
ok=0;
}
}
for (int i=l;i<r;i++){
if (is_pair_present[i][r+(i-l)]){
continue;
}
if (in_some_pair[i]){
ok=0;
}
if (is_end[i]){
ok=0;
}
}
for (int i=r;i<r+(r-l);i++){
if (is_pair_present[l+(i-r)][i]){
continue;
}
if (in_some_pair[i]){
ok=0;
}
if (is_start[i]){
ok=0;
}
}
// cout<<r<<" "<<ok<<"\n";
if (ok){
dp[r + ((r-l)-1)]=1;
}
}
}
}
cout<<(dp[2*n]?"Yes":"No")<<"\n";
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll m, ll n) {
if (n == 0) return m;
else return gcd(n, m % n);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
int main() {
int T;
cin >> T;
ll N, S, K;
while (T--) {
cin >> N >> S >> K;
ll g = gcd(K, N);
if (g > 1 && (N - S) % g != 0) {
cout << "-1\n";
} else {
ll x, y;
extgcd(K, N, x, y);
if (x > 0) {
x = x - (-x / N + 1) * N;
}
//cout << x << "\n";
x = -x * (S / g);
N /= g;
cout << x % N << "\n";
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) int(x.size())
//const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;
const ll ll0 = 0 * 1ll;
ll n, s, k;
ll mod;
ll gcd (ll a, ll b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
ll euler (ll n) {
ll d = 2;
ll ans = n;
while (d * d <= n) {
if (n % d == 0) {
ans /= d;
ans *= d - 1;
}
while (n % d == 0) {
n /= d;
}
d++;
}
if (n > 1) {
ans /= n;
ans *= n - 1;
}
return ans;
}
ll mull(ll a, ll b) {
return (a * b) % mod;
}
ll binpow (ll a, ll b) {
if (b == 0) {
return 1;
}
if (b % 2) {
return mull(a, binpow(a, b - 1));
}
ll half = binpow(a, b / 2);
return mull(half, half);
}
void init () {
}
void input () {
cin >> n >> s >> k;
s = n - s;
}
void solve() {
ll g = gcd(n, k);
if (s % g) {
cout << -1 << endl;
return;
}
n /= g;
s /= g;
k /= g;
ll inst = n;
ll phi = n;
for (ll d = 2; d * d <= inst; d++) {
if (inst % d == 0) {
phi /= d;
phi *= d - 1;
}
while (inst % d == 0) {
inst /= d;
}
}
if (inst > 1) {
phi /= inst;
phi *= inst - 1;
}
mod = n;
ll inv = binpow(k, phi -1);
cout << mull(s, inv) << endl;
}
void output() {
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int number_of_testcases = 1;
cin >> number_of_testcases;
while (number_of_testcases--) {
init();
input();
solve();
output();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[15],tot;
int main(){
int n;
scanf("%d",&n);
if(n==0){
printf("Yes");
return 0;
}
while(n%10==0)n/=10;
while(n){
a[++tot]=n%10;
n/=10;
}
int flag=1;
for(int i=1;i<=tot;i++)if(a[i]!=a[tot+1-i])flag=0;
printf(flag?"Yes":"No");
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert
#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}
using namespace std;
template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}
/* Some Libraries */
//-------------------------------------------------
bool ispelin(const string &s)
{
int N = s.size();
bool res=true;
rep(i,N/2){
if (s[i]!=s[N-i-1]) res=false;
}
return res;
}
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
string S; cin>>S;
int N = S.size();
bool yes=false;
rep(i,N){
if (ispelin(S)){
yes=true;
break;
}
S="0"+S;
}
cout<<(yes?"Yes":"No")<<"\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fst ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endk cout<<"\n";
typedef long long int ll;
typedef long int l;
#define l(i,a,b) for(i=a;i<b;i++)
#define lo(i,a,b) for(i=a;i>=b;i--)
#define s(v) sort(v.begin(),v.end());
#define pb push_back;
#define vll vector<ll>
#define vint vector<int>
#define test() ll t;cin>>t;while(t--){solve();};
#define test1() int t=1;while(t--){solve();};
bool isprime(ll x)
{
if(x==1) return false;
if(x==2 || x==3) return true;
if(x%2==0 || x%3==0) return false;
for(ll i=5;i*i<=x;i+=6)
{
if(x%i==0 || x%(i+2)==0) return false;
}
return true;
}
ll power(ll x,ll y)
{
ll ans=1;
while(y>0)
{
if(y&1) ans=ans*x;
x=x*x;
y=y>>1;
}
return ans;
}
int solve()
{
int h,w,x,y,i,j,ans=0;
cin>>h>>w>>x>>y;
vector<string> v(h);
for(i=0;i<h;i++)
{
cin>>v[i];
}
for(i=y-1;i<w;i++)
{
if(v[x-1][i]=='#') break;
ans++;
}
for(i=y-2;i>=0;i--)
{
if(v[x-1][i]=='#') break;
ans++;
}
for(i=x;i<h;i++)
{
if(v[i][y-1]=='#') break;
ans++;
}
for(i=x-2;i>=0;i--)
{
if(v[i][y-1]=='#') break;
ans++;
}
cout<<ans;
return 0;
}
int main()
{
fst;
test1();
return 0;
}
| #include <bits/stdc++.h>
#define M 1000000007
#define pii 3.14159265359
#define pb push_back
#define pob pop_back
#define forr(i,n) for(i=0;i<n;i++)
#define fors(i,j,n) for(i=j;i<n;i++)
#define pi pair<int,int>
#define ff first
#define ss second
#define tcase 0
#define all(v) v.begin(),v.end()
typedef long long int ll;
using namespace std;
#define sc(n) scanf("%lld",&n)
#define scn(n) scanf("%d",&n)
#define prt(n) printf("%lld ",n)
#define prtln(n) printf("%lld\n",n)
vector<pi> edges;
vector<vector<int>> v;
vector<int> prt;
vector<ll> val;
vector<ll> ans;
void assign_prt(int node,int p) {
prt[node] = p;
for(auto x : v[node]) {
if(x==prt[node]) continue;
assign_prt(x,node);
}
}
void find_ans(int node,int p,ll c) {
ans[node] += c + val[node];
for(auto x : v[node]) {
if(x==prt[node]) continue;
find_ans(x,node,c+val[node]);
}
}
void solve() {
int n,i;
scn(n);
v.clear(); edges.clear(); prt.clear(); val.clear(); ans.clear();
v.resize(n+1); prt.resize(n+1); val.resize(n+1,0ll);
forr(i,n-1) {
int a,b;
scn(a); scn(b);
edges.pb({a,b});
v[a].pb(b); v[b].pb(a);
}
assign_prt(1,-1);
int q;
scn(q);
forr(i,q) {
int a,b; ll value;
scn(a); scn(b); sc(value);
int v1,v2;
if(a==1) {
v1 = edges[b-1].ff;
v2 = edges[b-1].ss;
}
else {
v1 = edges[b-1].ss;
v2 = edges[b-1].ff;
}
if(v2==prt[v1]) {
val[v1] += value;
}
else {
val[1] += value;
val[v2] += -value;
}
}
ans.resize(n+1,0ll);
find_ans(1,-1,0);
fors(i,1,n+1) prtln(ans[i]);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t = 1;
if(tcase) scn(t);
while(t--) solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll ans = 2;
for (ll i = 3; i <= n; i++)
ans = (((i * ans)) / (__gcd(i, ans)));
cout << ans + 1;
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
string S;
cin >> S;
long long m, b, q;
m = 0;
b = 0;
q = 10;
for(int i=0; i<=9; i++){
if(S.at(i) == 'o') m++;
if(S.at(i) == 'x') b++;
}
q -= m;
q -= b;
long long ans = 0;
if(m==4){
ans = 24;
}
if(m==3){
ans = 36 + 24*q;
}
if(m==2){
ans = 14 + 36*q + 12*q*(q-1);
}
if(m==1){
ans = 1 + 14*q + 4*q*(q-1)*(q-2) + 18*q*(q-1);
}
if(m==0){
ans = q + 7*q*(q-1) + 6*q*(q-1)*(q-2) + q*(q-1)*(q-2)*(q-3);
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define fo(i,n) for(int i=0;i<n;i++)
#define fod(i,n) for(int i=n-1;i>=0;i--)
#define For(i,a,b) for(int i=a;i<=b;i++)
#define all(x) x.begin(),x.end()
#define SET(x) memset(x,-1,sizeof(x))
#define si set<int>
#define pb(i) emplace_back(i)
#define sll set<long long>
#define vs vector<string>
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vpii vector<pair<int,int>>
#define ll long long int
#define vi vector<ll>
#define mod 1000000007
#define tc(t) int t; cin>>t; while(t--)
#define sfm(a,m,n) ll a[m][n]; fo(i,m) fo(j,n) cin>>a[i][j]
#define sfarr(a,n) ll a[n]; fo(i,n) cin>>a[i]
#define sfv(v,n) vi v(n); for(ll &i:v) cin>>i
int f[1000001];
int prime[1000001];
void sieve(){
prime[0]=prime[1]=0;
For(i,2,1000000) prime[i]=1;
for(int i=2;i*i<=1000000;i++){
if(prime[i]){
for(ll j=i*i;j<=1000000;j+=i)
prime[j]=0;
}
}
}
int power(int a,int b){
int r=1;
while(b){
if(b&1){
r=(r*1ll*a)%mod;
b--;
}
else{
a=(a*1ll*a)%mod;
b>>=1;
}
}
return r;
}
void factorial(){
f[0]=f[1]=1;
For(i,2,1000000) f[i]=(f[i-1]*1ll*i)%mod;
}
int nCr(int a,int b){
factorial();
int r=f[a];
r=(r*1ll*power(f[b],mod-2))%mod;
r=(r*1ll*power(f[a-b],mod-2))%mod;
return r;
}
vi z_algo(string s){
ll n=s.length(),l=0,r=0;
vi z(n);
For(i,1,n-1){
if(i<=r) z[i]=min(r-i+1,z[i-l]);
while(z[i]+i<n && s[z[i]]==s[i+z[i]]) z[i]++;
if(i+z[i]-1>r) l=i,r=i+z[i]-1;
}
return z;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,a,b; cin>>n>>a>>b;
cout<<n-a+b;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for(int i=(a); i<(n); ++i)
#define per(i, a, n) for(int i=(a); i>(n); --i)
#define pb emplace_back
#define mp make_pair
#define clr(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(),(x).end()
#define lowbit(x) (x & -x)
#define fi first
#define se second
#define lson o<<1
#define rson o<<1|1
#define gmid l[o]+r[o]>>1
using LL = long long;
using ULL = unsigned long long;
using pii = pair<int,int>;
using PLL = pair<LL, LL>;
using UI = unsigned int;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double EPS = 1e-8;
const double PI = acos(-1.0);
const int N = 2e5 + 10;
int n;
LL k;
pair<LL, int> p[N];
int main(){
scanf("%d %lld", &n, &k);
rep(i, 0, n) scanf("%lld %d", &p[i].fi, &p[i].se);
sort(p, p+n);
LL cur = 0, d;
rep(i, 0, n){
if(cur + k < p[i].fi){
break;
}
k = k - (p[i].fi - cur) + p[i].se;
cur = p[i].fi;
}
printf("%lld\n", cur + k);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int h, w;
cin >> h >> w;
int n, m;
cin >> n >> m;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
vector<int> c(m), d(m);
for (int i = 0; i < m; i++)
cin >> c[i] >> d[i];
vector<vector<int>> grid(h, vector<int>(w, 0));
for (int i = 0; i < n; i++) {
--a[i];
--b[i];
grid[a[i]][b[i]] = 1;
}
for (int i = 0; i < m; i++) {
--c[i];
--d[i];
grid[c[i]][d[i]] = 2;
}
vector<vector<bool>> rowp(h, vector<bool>(w));
vector<vector<bool>> rows(h, vector<bool>(w));
vector<vector<bool>> colp(h, vector<bool>(w));
vector<vector<bool>> cols(h, vector<bool>(w));
for (int i = 0; i < h; i++) {
bool ok = false;
for (int j = 0; j < w; j++) {
if (grid[i][j] == 2) {
ok = false;
} else if (grid[i][j] == 1) {
ok = true;
}
rowp[i][j] = ok;
}
ok = false;
for (int j = w - 1; j >= 0; j--) {
if (grid[i][j] == 2) {
ok = false;
} else if (grid[i][j] == 1) {
ok = true;
}
rows[i][j] = ok;
}
}
for (int i = 0; i < w; i++) {
bool ok = false;
for (int j = 0; j < h; j++) {
if (grid[j][i] == 2) {
ok = false;
} else if (grid[j][i] == 1) {
ok = true;
}
colp[j][i] = ok;
}
ok = false;
for (int j = h - 1; j >= 0; j--) {
if (grid[j][i] == 2) {
ok = false;
} else if (grid[j][i] == 1) {
ok = true;
}
cols[j][i] = ok;
}
}
int res = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (grid[i][j] == 0 || grid[i][j] == 1) {
int cur = rowp[i][j] || rows[i][j] || colp[i][j] || cols[i][j];
res += cur;
}
}
}
cout << res << '\n';
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> graph;
vector<bool> visit(2000);
void dfs(int now_town){
if(visit[now_town]){
return;
}
visit[now_town] = true;
for(auto next_town : graph[now_town]){
dfs(next_town);
}
}
int main(void){
int N, M;
cin >> N >> M;
graph.resize(N);
for(int i = 0; i < M; i++){
int A, B;
cin >> A >> B;
graph[A-1].push_back(B-1);
}
int ans = 0;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
visit[j] = false;
}
dfs(i);
for(int j = 0; j < N; j++){
if(visit[j]) ans++;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using Tuple = tuple<int, int, int, int>;
using V1 = vector<int>;
using V2 = vector<V1>;
using V3 = vector<V2>;
using PQueue = priority_queue<Tuple, vector<Tuple>, greater<Tuple>>;
constexpr int INF = 1e+9;
template<typename T>
bool chmin(T &a, T b)
{
if (a > b) {
a = b;
return true;
}
return false;
}
int calc_dist(int idx, V2 &A, V2 &B, int x1, int y1, int z1)
{
if (idx == 0)
return A[x1][y1];
if (idx == 1)
return A[x1][y1 - 1];
if (idx == 2)
return B[x1][y1];
if (idx == 3)
return (z1 == 0) ? 2 : 1;
if (idx == 4)
return 1;
assert(false);
return INF;
}
int main()
{
int R, C;
cin >> R >> C;
V2 A(R, V1(C - 1)), B(R - 1, V1(C));
for (auto &Ai : A) {
for (auto &Aij : Ai)
cin >> Aij;
}
for (auto &Bi : B) {
for (auto &Bij : Bi)
cin >> Bij;
}
V3 cost(R, V2(C, V1(2, INF)));
cost[0][0][0] = 0;
PQueue pq;
pq.push({0, 0, 0, 0});
int dx[5] = {0, 0, 1, -1, 0};
int dy[5] = {1, -1, 0, 0, 0};
while (!pq.empty()) {
auto [cost1, x1, y1, z1] = pq.top(); pq.pop();
if (cost[x1][y1][z1] < cost1)
continue;
for (int i = 0; i < 5; ++i) {
auto x = x1 + dx[i], y = y1 + dy[i], z = (i >= 3) ? 1 : 0;
if (x < 0 || R <= x || y < 0 || C <= y)
continue;
if (i == 3 && z1 == 0)
continue;
auto d = calc_dist(i, A, B, x1, y1, z1);
auto cost2 = cost1 + d;
if (chmin(cost[x][y][z], cost2))
pq.push({cost2, x, y, z});
}
}
auto &costRC = cost[R - 1][C - 1];
cout << *min_element(costRC.begin(), costRC.end()) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i,n) for(long i=0; i<(long)(n);i++)
#define REP(i,s,t) for(long i=s; i<t; i++)
#define RNG(i,s,t,u) for(long i=s; i<t; i+=u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()
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; }
vvc d(2000+2,vector<char>(2000+2, '#'));
vector<vvl> dp(4,vvl(2000+2, vl(2000+2,-1)));
//URDL
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
/*void dfs(int x, int y, int dir){
if(dp[dir][x][y] != -1) return;
rep(i,4){
int nx = x + dx[i];
int ny = y + dy[i];
if(d.at(nx).at(ny) == '#'){
dp[i][x][y] = 0;
}else{
if(dp[i][nx][ny] != -1){
dp[i][nx][ny] = dp[i][x][y] + 1;
}else{
dfs()
}
}
}
if(d.at(x).at(y) == '#'){
dp[dir][x][y] = 0;
return;
}
}*/
long dfs(int x, int y, int dir){
if(d.at(x).at(y) == '#') return 0;
if(dp[dir][x][y] != -1) return dp[dir][x][y];
int nx = x + dx[dir];
int ny = y + dy[dir];
if(d.at(nx).at(ny) == '#') return dp[dir][x][y] = 0;
else return dp[dir][x][y] = dfs(nx,ny,dir) + 1;
}
int main(){
long h,w;
cin >> h >> w;
long shiro = 0;
rep(i,h){
rep(j,w) {
cin >> d.at(i+1).at(j+1);
if(d.at(i+1).at(j+1) == '.') shiro++;
}
}
vl two(5000000, 1);
REP(i,1,5000000){
two.at(i) = two.at(i-1)*2%MOD;
}
long ans = 0;
rep(i,h){
rep(j,w){
if(d.at(i+1).at(j+1) == '#') continue;
long cnt = 1;
rep(k,4){
cnt += dfs(i+1,j+1,k);
}
long tmp = two.at(shiro-cnt) * (two.at(cnt) + MOD - 1) % MOD;
ans = ( ans + tmp ) % MOD;
}
}
cout << ans << endl;
} |
#include<cstdio>
typedef long long ll;
struct SegmentTree {
int num,rt;
ll size[2000005],sum[2000005];
int lson[2000005],rson[2000005];
SegmentTree() {num=0; rt=0;}
inline void _change(int &p,int l,int r,int x,int d) {
if(!p) p=++num;
size[p]+=d; sum[p]+=x*1ll*d;
if(l==r) return;
int mid=l+r>>1;
if(x<=mid) _change(lson[p],l,mid,x,d);
else _change(rson[p],mid+1,r,x,d);
}
inline void change(int x,int d) {_change(rt,1,100000000,x,d);}
inline ll _ask(int p,int l,int r,int L,int R,ll *arr) {
if(L<=l&&r<=R) return arr[p];
int mid=l+r>>1; ll res=0;
if(L<=mid&&lson[p]) res+=_ask(lson[p],l,mid,L,R,arr);
if(R>mid&&rson[p]) res+=_ask(rson[p],mid+1,r,L,R,arr);
return res;
}
inline ll ask(int L,int R,int op) {return op==1? _ask(rt,1,100000000,L,R,size):_ask(rt,1,100000000,L,R,sum);}
}t[2];
int a[200005][2];
inline int read() {
register int x=0,f=1;register char s=getchar();
while(s>'9'||s<'0') {if(s=='-') f=-1;s=getchar();}
while(s>='0'&&s<='9') {x=x*10+s-'0';s=getchar();}
return x*f;
}
int main() {
int n=read(),m=read(),Q=read(); ll ans=0;
t[0].change(0,n); t[1].change(0,m);
while(Q--) {
int op=read()-1,x=read(),val=read();
t[op].change(a[x][op],-1);
ans-=t[op^1].ask(1,a[x][op],1)*a[x][op]+t[op^1].ask(a[x][op]+1,1e8,2);
t[op].change(val,1);
ans+=t[op^1].ask(1,val,1)*val+t[op^1].ask(val+1,1e8,2);
a[x][op]=val;
printf("%lld\n",ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long N, C, x, y, c, cur, pr1, pr2, total;
vector<pair<long, long>> a;
bool sortbc(const pair<int,int> &a,
const pair<int,int> &b)
{
if (a.first == b.first)
return (a.second > b.second);
return a.first < b.first;
}
int main() {
cin>>N>>C;
for(int i=0; i<N; i++) {
cin>>x>>y>>c;
a.push_back(make_pair(x, c));
a.push_back(make_pair(y, -1*c));
}
sort(a.begin(), a.end(), sortbc);
int i = 0;
while( i < 2*N ) {
int day = a[i].first;
int both = 0;
while(a[i].first == day && a[i].second > 0) {
cur += a[i].second;
i++;
}
if( a[i].first == day ) {
total += min(C, cur);
both = 1;
}
while( a[i].first == day && a[i].second < 0 ) {
cur += a[i].second;
i++;
}
total += (a[i].first - day - both) * min(C, cur);
}
cout<<total;
} |
#include <bits/stdc++.h>
using namespace std;
typedef int_fast32_t int32;
typedef int_fast64_t int64;
const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;
#define YES(n) cout << ((n) ? "YES\n" : "NO\n" )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n" )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n" )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;
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;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int32 n,a,b;
cin >> n >> a >> b;
cout << n - a + b << endl;
return 0;
} | #include <iostream>
#include<stdio.h>
#include<iostream>
#include <cmath>
using namespace std;
int main(){
int n,plus,minus;
cin>>n>>minus>>plus;
int sum=(n-minus)+plus;
cout<<sum;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define poly vector<ll>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
inline ll read(){
ll x=0; char ch=getchar(); bool d=1;
for(;!isdigit(ch);ch=getchar()) if(ch=='-') d=0;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return d?x:-x;
}
inline ull rnd(){
return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=2e5+5;
int sum[N][26];
char s[N];
int main(){
scanf("%s",s+1);
int n=strlen(s+1),la=n+1;
ll ans=0;
Rep(i,n,1){
For(j,0,25) sum[i][j]=sum[i+1][j];
sum[i][s[i]-'a']++;
if(i>n-2) continue;
if(s[i]==s[i+1]){
int ssw=la-i-sum[i][s[i]-'a']+sum[la][s[i]-'a'];
if(s[la]!=s[i]) ssw+=n-la+1;
la=i; ans+=ssw;
}
}
cout<<ans;
} | #include <bits/stdc++.h>
using namespace std;
//UnionFindテンプレ
class UnionFind {
// まとめる 判定 サイズを知る
public:
// Aから見た親の番号を格納する。rootだったら-1*その集合のサイズ。
vector<int> Parent;
// 初期化
UnionFind(int N) {
Parent = vector<int>(N, -1);
}
// Aのrootを調べる
int root(int A) {
if (Parent[A] < 0) return A; // マイナスならそれはroot
return Parent[A] = root(Parent[A]);
}
// rootの値をプラスに戻して返す(サイズ)
int size(int A) {
return -Parent[root(A)];
}
// くっつける関数
bool connect(int A, int B) {
// AとBのroot同士をくっつける
A = root(A); // ここのAは、"rootの場所"の意味
B = root(B);
if (A == B) return false; // 既にくっついている
if (size(A) < size(B))
swap(A, B); // 大きい方にくっつけるために中身交換
Parent[A] += Parent[B]; // 中身更新
Parent[B] = A;
return true;
}
//連結か調べる関数
bool issame(int A, int B) {
return root(A) == root(B);
}
};
//chmin,chmax関数(DP用)
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
//aのZ/mZでの逆元
int64_t modinv(int64_t a, int64_t m) {
int64_t b = m, u = 1, v = 0;
while (b) {
int64_t t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
//累乗a^b
int64_t pow(int a, int b) {
int64_t ans = (int64_t)1;
for (int i = 0; i < b; i++) {
ans *= (int64_t)a;
}
return ans;
}
//最大公約数
int gcd(int a, int b) {
if (a % b == 0) {
return b;
}
return gcd(b, a%b);
}
char jan(char x, char y) {
if (x == y) {
return x;
}
if (x == 'R') {
if (y == 'S') {
return 'R';
} else {
return 'P';
}
}
if (x == 'S') {
if (y == 'R') {
return 'R';
} else {
return 'S';
}
}
if (x == 'P') {
if (y == 'R') {
return 'P';
} else {
return 'S';
}
}
return x;
}
//ここから
int main() {
int n,k;
cin >> n >> k;
string s;
cin >> s;
s += s;
int a = s.size();
for (int i = 0; i < k; i++) {
string S;
for (int j = 0; j < a/2; j++) {
S += jan(s.at(2*j), s.at(2*j+1));
}
s = S+S;
}
cout << s.at(0) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
string S;
cin >> S;
int o=0,x=0,q=0;
for(int i=0;i<10;i++){
if(S[i]=='o') o++;
if(S[i]=='x') x++;
if(S[i]=='?') q++;
}
int ans = 0;
for(int i=0;i<10000;i++){
vector<int> V(10,0);
int now = i;
for(int j=0;j<4;j++){
V[now%10]++;
now /= 10;
}
bool ok = true;
for(int j=0;j<o;j++){
if(V[j]==0) ok = false;
}
for(int j=o;j<o+x;j++){
if(V[j]!=0) ok = false;
}
if(ok) ans++;
}
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
string s;
cin >> s;
int ans_o = 0, ans_p = 0;
for(int i = 0; i < s.length(); ++i){
if(s[i] == 'o') ans_o++;
if(s[i] == '?') ans_p++;
}
if(ans_o > 4) {
cout << 0 << endl;
}
else if(ans_o == 4){
cout << 24 << endl;
}
else if(ans_o == 3){
cout << ans_p*24+36<<endl;
}
else if(ans_o == 2){
cout << 14+ans_p*36+ans_p*(max(0,ans_p-1))*12<<endl;
}
else if(ans_o == 1){
cout << 1+ans_p*14+ans_p*(max(0,ans_p-1))*18+ans_p*(max(0, ans_p-1))*(max(0,ans_p-2))*4<<endl;
}
else{
cout << ans_p+ans_p*(max(0,ans_p-1))*7+ans_p*(max(0, ans_p-1))*(max(0,ans_p-2))*6+ans_p*(max(0, ans_p-1))*(max(0,ans_p-2))*(max(0,ans_p-3))<<endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char dp[105][105];
string s;
ll Mod;
ll mypow(ll a,ll b){
ll ans=1;
while(b){
if(b&1)ans=ans*a%Mod;
a=a*a%Mod;
b>>=1;
}
return ans;
}
char chos(char s1,char s2){
if(s1==s2)return s1;
if(s1=='P'&&s2=='R')return s1;
if(s1=='P'&&s2=='S')return s2;
if(s1=='R'&&s2=='P')return s2;
if(s1=='R'&&s2=='S')return s1;
if(s1=='S'&&s2=='P')return s1;
if(s1=='S'&&s2=='R')return s2;
}
int main(){
int n,k;
cin>>n>>k;
cin>>s;
s='='+s;
Mod=n;
for(int i=1;i<=n;++i)dp[i][0]=s[i];
for(int j=1;j<=k;++j){
ll f=mypow(2,j-1);
for(int i=1;i<=n;++i){
int a=(i+f)%Mod;
if(a==0)a=n;
dp[i][j]=chos(dp[i][j-1],dp[a][j-1]);
}
}
cout<<dp[1][k];
return 0;
}
| #include <bits/stdc++.h>
#define ll unsigned long long
//#define fin cin
using namespace std;
int n, k;
string s;
char winner(char a, char b) {
if(a == 'R' && b == 'S')
return a;
if(a == 'P' && b == 'R')
return a;
if(a == 'S' && b == 'P')
return a;
return b;
}
int main() {
//ifstream fin("date.in.txt");
/*
R beats S;
P beats R;
S beats P.
*/
cin >> n >> k >> s;
for (int i = 0; i < k; ++i) {
s += s;
string new_s;
for (int j = 0; j < n; ++j) {
new_s += winner(s[2*j], s[2*j+1]);
}
s = new_s;
}
cout << s.front() << '\n';
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
template<typename T_vector>
void output_vector(const T_vector &v, bool addOne = false, int start = -1, int end = -1) {
if (start < 0) start = 0;
if (end < 0) end = (int) v.size();
for(int i = start; i < end; i++)
cout<< v[i] + (addOne ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;
void solve() {
int n;
cin>>n;
vector<int> v(n);
rep(i,0,n) {
cin>>v[i];
}
int sum = 0;
sort(all(v));
for(int i = sz(v) - 1; i >= 0; i--){
sum += (v[i]*(i)- v[i]*(n-i-1));
}
cout<<sum<<"\n";
}
signed main() {
fast
#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);
cout<<setprecision(12);
cout<<fixed;
#endif
int t=1;
//~ cin>>t;
while(t--) solve();
return 0;
}
| #include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define mm(arr) memset(arr, 0, sizeof(arr))
#define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i];}
#define pb push_back
#define PI 3.141592653589793
#define MOD 1000000007
using namespace std;
int main(){
FAST
int n; cin >>n;
double x1,y1,x2,y2,x3,y3;
double x[n];
double y[n];
bool result=false;
for(int i=0;i <n; i++){
cin >> x[i];
cin >> y[i];
}
for(int i=0; i<n ;i++){
for(int j=i+1; j<n; j++){
for(int k=j+1; k<n; k++){
x1=x[i];x2=x[j];x3=x[k];y1=y[i];y2=y[j];y3=y[k];
if(x3-x2==0&& x3-x1==0){
result=true;}
else{
if((y3-y2)/(x3-x2)==(y3-y1)/(x3-x1)){
result=true;
break;
}
}
}
}
}
if(result)
cout << "Yes";
else
cout << "No";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int V,T,S,D;
cin>>V>>T>>S>>D;
if (D<V*T || D>V*S){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
} | #define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
clock_t T;
#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << endl
#define all(v) ((v).begin()), ((v).end())
#define sz(v) v.size()
#define endl "\n"
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
constexpr auto EPS = 1e-7;
const int mod = (int)1e9 + 7;
int dxn[] = { -1,-2,-2,-1,1,2,2,1 };
int dyn[] = { -2,-1,1,2,2,1,-1,-2 };
int dx[] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 , 0 };
int dy[] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 , 0 };
ll gcd(ll a, ll b) { return (a) ? gcd(b % a, a) : b; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
void COME_BACK()
{
std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("input.txt", "w", stdout);
#endif
}
int main()
{
COME_BACK();
int a, b, c, d;
cin >> a >> b >> c >> d;
cout <<( a * d) - (c * b) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
typedef vector<bool> vbl;
typedef vector<vector<bool>> vvbl;
void INX(){}
template<typename Head, typename... Tail>
void INX(Head&& head, Tail&&... tail)
{
cin >> head;
INX(forward<Tail>(tail)...);
}
void OUTX(){}
template<typename Head, typename... Tail>
void OUTX(Head&& head, Tail&&... tail)
{
cout << head << endl;
OUTX(forward<Tail>(tail)...);
}
#define ADD emplace_back
#define MP make_pair
#define VVEC(type) vector<vector<type>>
#define __LONG_LONG_MIN__ (-__LONG_LONG_MAX__ - 1)
//#define MOD 1000000007 // 10^9 + 7
//setprecision(10)
int main()
{
ll N, X;
INX(N, X);
X *= 100;
bool yotta = false;
ll suma = 0;
for (ll i = 1; i <= N; i++)
{
ll v,p;
INX(v,p);
suma += v * p;
if(suma > X)
{
yotta = true;
OUTX(i);
break;
}
}
if(!yotta)
{
OUTX(-1);
}
return 0;
}
| #include <iostream>
#include <vector>
#include <string>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
int main()
{
string str;
cin >> str;
int ans = 0;
rep(i,12){
if(str.compare(i,4,"ZONe")==0){
ans++;
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// using mint = long double;
// using mint = modint998244353;
// using mint = modint1000000007;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P> T;
typedef pair<ll, vector<ll>> Pd;
const ll INF = 1e18;
const ll fact_table = 320000;
priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
priority_queue<ll, vector<ll>, greater<ll>> pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop
ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
//↓,→,↑,←
#define p(x) cout << x << "\n";
#define el cout << "\n";
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(25) << x << endl;
#define pu(x) cout << (x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pc(x) cout << x << ",";
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = a; i <= (b); i++)
#define rep3(i, a, b) for (ll i = a; i >= (b); i--)
#define all(c) begin(c), end(c)
#define sorti(v) sort(all(v))
#define sortd(v) \
sort(all(v)); \
reverse(all(v));
#define SUM(v) accumulate(all(v), 0LL)
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
typedef vector<ll> vec;
typedef vector<vector<ll>> mat;
// vec v(n) -> 長さnのベクトルを宣言
// mat dp(h, vec(w)) -> h * w の行列を宣言
// const ll mod = 1000000007ll;
const ll mod = 998244353ll;
ll mypow(ll a, ll b, ll m = mod) {
ll x = 1;
while (b) {
while (!(b & 1)) {
(a *= a) %= m;
b >>= 1;
}
(x *= a) %= m;
b--;
}
return x;
}
vec rv(ll read) {
vec res(read);
for (int i = 0; i < read; i++) {
cin >> res[i];
}
return res;
}
/*
ll fact[fact_table + 5], rfact[fact_table + 5];
void c3_init() {
fact[0] = rfact[0] = 1;
for (ll i = 1; i <= fact_table; i++) {
fact[i] = (fact[i - 1] * i) % mod;
}
rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod);
for (ll i = fact_table; i >= 1; i--) {
rfact[i - 1] = rfact[i] * i;
rfact[i - 1] %= mod;
}
return;
}
ll c3(ll n, ll r) {
return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod;
}
*/
bool icpc = false;
bool multicase = false;
ll n;
bool used[400005];
map<ll, vec> mp;
bool solve() {
cin >> n;
n *= 2;
vector<P> table;
for (int i = 0; i < n; i++) {
int value;
cin >> value;
table.pb({value, i});
}
sortd(table);
for (int i = 0; i < n / 2; i++) {
int idx = table[i].second;
used[idx] = true;
}
vec v;
for (int i = 0; i < n; i++) {
int now = (used[i]) ? 1 : 0;
bool push = false;
if (v.size() == 0) {
push = true;
} else if (v.back() == now) {
push = true;
} else {
push = false;
}
if (push) {
pu("(");
v.pb(now);
} else {
pu(")");
v.pop_back();
}
}
el;
return true;
}
/*
*/
int main() {
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
if (icpc) {
while (solve())
;
return 0;
}
ll q, testcase = 1;
if (multicase) {
cin >> q;
} else {
q = 1;
}
while (q--) {
// cout << "Case #" << testcase << ": ";
solve();
testcase++;
}
// solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define FI freopen("/home/empathy/in.txt", "r", stdin)
#define FO freopen("/home/empathy/out.txt", "w", stdout)
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 << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef QUICKRUN
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int, int> ii;
bool check(vector<int> arr, ll base, ll M) {
__int128_t res = 0;
for (int i = 0; i < arr.size(); ++i) {
if (res > (M - arr[i]) / base) return true;
res = res * base + arr[i];
if (res > M || res < 0) return true;
}
return false;
}
__int128_t binarySearch(vector<int> arr, ll l, ll M) {
ll r = M + 1;
while (r - l > 1) {
ll mid = l + (r - l) / 2;
if (check(arr, mid, M)) r = mid;
else l = mid;
dbg(r);
}
return r;
}
void solve() {
string X;
cin >> X;
ll M;
cin >> M;
int ma = 0;
int n = int(X.length());
vector<int> arr(n);
for (int i = 0; i < n; ++i) {
arr[i] = X.at(i) - '0';
if (arr[i] > ma) ma = arr[i];
}
ll cnt = 0;
if (n != 1) {
cnt = binarySearch(arr, 1, M);
dbg(cnt);
cnt -= ma + 1;
if (cnt < 0) cnt = 0;
}
else {
if (arr[0] > M) cnt = 0;
else cnt = 1;
}
cout << cnt << '\n';
}
int main() {
ios::sync_with_stdio(false);
#ifndef QUICKRUN
cin.tie(nullptr);
#endif
#ifdef QUICKRUN
FI;
// FO;
#endif
solve();
return 0;
}
|
// AUTHOR:- Saumya Rangani
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi (3.141592653589)
#define mod 1000000007
#define float double
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vii vector<pair<int,int>>
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define it(a) for(auto &i:a)
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d) max(a,max(b,max(c,d)))
#define read(v) for(auto &i:v)cin>>i;
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define in cin>>
#define out cout<<
#define default(a,n,b) for(int i=0;i<n;i++)a[i]=b;
#define YNans(poss) cout << (poss ? "YES" : "NO" ) << endl;
#define inarray(a,n) for(int i=0;i<n;i++)cin>>a[i];
#define fast ios_base :: sync_with_stdio(false),cin.tie(NULL);
#define MAXN 2005
int gcd(int a, int b){return (b?gcd(b,a%b):a);}
bool isPrime(int n){if(n==1) return false;if(n==2) return true;for(int i=2;i*i<=n;i++){if(n%i==0)return false;}return true;}
int power(long long x, unsigned int y, int p=mod){int res = 1;x = x % p; if (x == 0) return 0;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1;x = (x*x) % p;}return res;}
int digit(int n){int ans=0;while(n){ans++;n/=10;}return ans;}
int lcm(int a,int b){return a*b/gcd(a,b);}
vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
vector<vector<int>> grp(MAXN);
bool vis[MAXN];
int dfs(int node){
int ans=1;
if(vis[node]){
return 0;
}
vis[node]=1;
// cout<<node<<endl;
for(auto i:grp[node]){
ans+=dfs(i);
}
return ans;
}
void solve(){
int n;in n;
int m;in m;
for(int i=0;i<m;i++){
int x,y;in x>>y;
grp[x].pb(y);
}
int ans=0;
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
cout<<ans<<endl;
}
int32_t main(){
fast
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
| #pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <algorithm>
#include <iostream>
#include <array>
using namespace std;
#define f(x,y,z) for(int x=y;x<=z;++x)
int n, m, q, l, r, h, mn, imn;
array<int, 51> bx, kt;
pair<int, int> bg[51];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
f(i,1,n) cin >> bg[i].second >> bg[i].first;
sort(bg+1, bg+n+1, greater<pair<int, int>>());
f(i,1,m) cin >> kt[i];
while(q--) {
cin >> l >> r;
h = 0, bx = kt;
f(i,1,n) {
imn = 0, mn = 1e9;
f(j,1,m) if(j < l || j > r) {
if(bx[j] >= bg[i].second && bx[j] < mn) {
mn = bx[j], imn = j;
}
}
if(imn) h += bg[i].first, bx[imn] = 0;
}
cout << h << '\n';
}
} |
/*************************************************************************
> File Name: 1.cpp
> Author: Knowledge_llz
> Mail: [email protected]
> Blog: https://blog.csdn.net/Pig_cfbsl
> Created Time: 2020/10/11 22:45:26
************************************************************************/
#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define pb push_back
#define pr pair<int,int>
#define fi first
#define se second
#define LL long long
#define mk(a,b) make_pair(a,b)
using namespace std;
int read(){
char x=getchar(); int u=0,fg=0;
while(!isdigit(x)){ if(x=='-') fg=1; x=getchar(); }
while(isdigit(x)){ u=(u<<3)+(u<<1)+(x^48); x=getchar(); }
return fg?-u:u;
}
const int maxx=1e5+10;
int n,m,sum=0,cnt=0,mx;
int w[10],tmp[10],s[10],dis[10],v[maxx],l[maxx];
int p[10]={0,1,2,3,4,5,6,7,8};
pr a[maxx];
bool cmp(pr x,pr y){
if(x.fi!=y.fi) return x.fi<y.fi;
return x.se>y.se;
}
int iso(int x){
x=lower_bound(v+1,v+cnt+1,x)-v-1;
return l[x];
}
int solve(){
s[0]=0;
For(i,1,n){ tmp[i]=w[p[i]]; s[i]=s[i-1]+tmp[i]; dis[i]=0;}
For(i,2,n) For(j,1,i-1) dis[i]=max(dis[i],dis[j]+iso(s[i]-s[j-1]));
// cout<<dis[3]<<endl;
return dis[n];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
int u1,u2;
n=read(); m=read();
For(i,1,n){ w[i]=read(); mx=max(w[i],mx);}
For(i,1,m){
u1=read(); u2=read();
a[i]=mk(u2,u1);
if(u2<mx){ printf("-1\n"); return 0; }
}
sort(a+1,a+m+1,cmp);
v[++cnt]=a[1].fi; l[cnt]=a[1].se;
For(i,2,m)
if(a[i].se>l[cnt]){
v[++cnt]=a[i].fi;
l[cnt]=a[i].se;
}
int ans=1e9;
do{
ans=min(ans,solve());
// return 0;
}while(next_permutation(p+1,p+n+1));
printf("%d\n",ans);
return 0;
}
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Graph= vector<vector<int>>;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define fi first
#define se second
#define mpa make_pair
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
#define set20 cout<<fixed<<setprecision(20) ;
// ミント
//int mod ; cin>>mod ;
const ll mod = //1e9+7 ;//924844033;
998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
//昆布
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < mod); //任意modではここ消す
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(int n,int k){
return fact[n]*ifact[n-k] ; //kは個数
}
} c(1000005);
mint modpow(ll a,ll b){
if(b==0) return 1 ;
mint c= modpow(a,b/2) ;
if(b%2==1) return c*c*a ;
else return c*c ;
}
mint komb(ll n,ll m){
mint x=1 ;mint y=1 ;
rep(i,m){
x*= n-i ;
y*= i+1 ;
}
return x/y ;
}
map<ll,ll> factor(ll n){ //素因数とオーダーをマップで管理
map <ll,ll> ord ;
for(ll i=2;i*i<=n;i++){
if(n%i==0){
int res=0;
while(n%i==0){
n/=i;
res++;
}
ord[i]=res;
}
}
if(n!=1) ord[n]++;
return ord ;
}
mint dp[105][10005] ;
mint ep[105][10005] ;
int main(){
ios::sync_with_stdio(false) ;
cin.tie(nullptr) ;
ll n ; cin>>n ;
vector<ll> A(n) ;
rep(i,n) cin>>A[i] ;
ll sum=0 ;
rep(i,n){
sum+=A[i] ;
}
if(sum%2!=0){
cout<<0<<endl ;
return 0 ;
}
ll g=sum/2 ;
dp[0][0]=1ll ;
rep(i,n){
ll now=A[i] ;
//mint ep[105][10005] ;
rep(x,105)rep(y,10005) ep[x][y]=0ll ;
rep(k,i+1){
rep(j,10005){
if(j+now<=g) ep[k+1][j+now]+=dp[k][j] ;
else break ;
}
}
rep(x,105)rep(y,10005) dp[x][y]+=ep[x][y] ;
}
vector<mint> p(n+5) ;
p[0]=1ll ;
rep(i,n+4) p[i+1]=p[i]*(i+1);
mint ans=0 ;
rep(i,n+1){ // cout<<dp[i][g]<<endl ;
ans+=dp[i][g]*p[i]*p[n-i] ;
}
cout<<ans<<endl ;
return 0 ;
} |
#include<iostream>
#include<algorithm>
#include<ctime>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#include<numeric>
#include<queue>
#include<deque>
#include<cfloat>
#include<functional>
#include<tuple>
#include<math.h>
#include<bitset>
#include<stack>
#include<set>
#include<random>
#include<stdlib.h>
#define rip(i,n) for(int i=0;i<n;i++)
#define Rip(i,n) for(int i=1;i<=n;i++)
#define RIP(i,a,b) for(int i=a;i<b;i++)
#define all(V) V.begin(),V.end()
#define sec setprecision;
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops");
constexpr double eps = 1e-9;
constexpr double pi = 3.141592653589793238462643383279;
using namespace std;
using ll = long long;
using ld = long double;
using Pa = pair<ll, ll>;
const ll MOD = 998244353;
ll gcd(ll a, ll b)
{
if (a % b == 0) return(b);
else return(gcd(b, a % b));
}
ll lcm(ll a, ll b)
{
return a * b / gcd(a, b);
}
ll pow(ll a, ll b) {
ll ans = 1;
if (b < 0)return 1;
else if (b % 2 == 0)return(pow(a * a, b / 2));
else return (a * pow(a, b - 1));
}
ll modpow(ll x, ll n, ll mod) {
x %= mod;
if (n == 0)
return 1;
if (n % 2 == 0)
return (modpow((x * x) % mod, n / 2, mod) % mod);
else
return (x * modpow(x, n - 1, mod)) % mod;
}
template<typename T>
vector<T> primes(T n) {//primes[i]でiを割り切る最初の素数を返す
vector<T> spf(n + 1);
for (int i = 0; i <= n; i++) spf[i] = i;
for (T i = 2; i * i <= n; i++) {
if (spf[i] == i) {
for (T j = i * i; j <= n; j += i) {
if (spf[j] == j) {
spf[j] = i;
}
}
}
}
return spf;
}
void solveA() {
int a, b, c; cin >> a >> b >> c;
if (c == 0) {
if (b >= a)cout << "Aoki" << endl;
else cout << "Takahashi" << endl;
}
else {
if (b > a)cout << "Aoki" << endl;
else cout << "Takahashi" << endl;
}
}
void solveB() {
int n, s, d; cin >> n >> s >> d;
vector<int>x(n);
vector<int>y(n);
rip(i, n)cin >> x[i] >> y[i];
ll sum = 0;
rip(i, n) {
if (x[i]<s && y[i]>d)sum += y[i];
}
if (sum > 0)cout << "Yes" << endl;
else cout << "No" << endl;
}
void solveC() {
int n, m; cin >> n >> m;
vector<int>a(m);
vector<int>b(m);
rip(i, m)cin >> a[i] >> b[i], a[i]--, b[i]--;
int k; cin >> k;
vector<int>c(k);
vector<int>d(k);
rip(i, k)cin >> c[i] >> d[i], c[i]--, d[i]--;
int ans = 0;
for (int i = 0; i < pow(2, k); i++) {
vector<bool>e(n, false);
for (int j = 0; j < k; j++) {
int m = i / (pow(2, j));
if (m % 2 == 1) {
//d[j]を使う
e[d[j]] = true;
}
else {
//c[j]を使う
e[c[j]] = true;
}
}
int count = 0;
for (int r = 0; r < m; r++) {
if (e[a[r]] && e[b[r]])count++;
}
ans = max(ans, count);
}
cout << ans << endl;
}
void solveD() {
ll n; cin >> n;
//正のみで考えて弐倍すればok
int count = 0;
for (int i = 1; (ll)i * i + i <= 2 * n; i++) {
if ((2 * n) % i == 0 && ((2 * n / i) + 1 - i) % 2 == 0) {
count += 2;
}
}
cout << count << endl;
}
void solveE() {
int n, m; cin >> n >> m;
vector<int>a(m); vector<int>b(m);
rip(i, m)cin >> a[i] >> b[i];
int k; cin >> k;
vector<int>c(k);
rip(i, k)cin >> c[i];
}
int main() {
int n;cin>>n;
cout<<max(0,n)<<endl;
} |
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define null NULL
typedef long double ld;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define vc vector
#define pb push_back
#define um unordered_map
#define us unordered_set
#define inf LONG_LONG_MAX
#define MOD (int)((1e9)+7)
vc<int>primes;
vc<int>spf;
void generatePrimes(int n){
vc<bool>isPrime(n+1, true);
isPrime[1] = false;
isPrime[0] = false;
for(int i = 2; i<= n; i++){
if(isPrime[i]){
primes.push_back(i);
spf[i] = i;
}
for(int j = 0; j<primes.size() &&
i*primes[j]<n && primes[j]<=spf[i];
j++){
isPrime[i*primes[j]] = false;
spf[i*primes[j]] = primes[j];
}
}
}
bool myComp(pair<string, int>a, pair<string, int>b){
return a.second < b.second;
}
int gcd(int a, int b){
if(a==0){
return b;
}
return gcd(b%a, a);
}
int mod(int a, int m){
return (a%m + m) % m;
}
int power(int x, int n, int m){
int res = 1;
x = x%m;
if(x==0){
return 0;
}
while(n){
if(n%2 == 1){
res = (res*x)%m;
}
x = (x*x)%m;
n /= 2;
}
return res;
}
int nCr(int n, int r){
int p=1, k=1;
if(n-r < r){
r = n-r;
}
if(r != 0){
while(r){
p *= n;
k *= r;
int m = gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else{
p=1;
}
return p;
}
void testcase(){
int n;
cin >> n;
vc<int>arr(n);
rep(i, 0, n){
cin >> arr[i];
}
sort(all(arr));
ld x;
ld res = 0;
if(n%2){
x = (ld)arr[n/2]/2;
}
else{
x = (ld)(arr[n/2] + arr[(n/2)-1])/4;
}
for(auto i: arr){
res += (x + i - min((ld)i, 2*x));
}
cout <<fixed <<res/n;
cout << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(null);
//cout.tie(null);
//generatePrimes(1000001);
int t;
//cin >> t;
//while(t--){
testcase();
//}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll n,x;
cin>>n>>x;
vector<ll> v(n,0),p(n,0);
for(ll i=0;i<n;i++)
{
cin>>v[i]>>p[i];
}
ll a=-1;
ll k=0;
for(ll i=0;i<n;i++)
{
k+=v[i]*p[i];
if(k>x*100)
{
a=i+1;
break;
}
}
cout<<a;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
using Graph = vector<vector<int>>;
using Graphl = vector<vector<ll>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, k, n) for (int i = k; i < (int)(n); i++)
#define RRep(i, k, n) for (int i = k; i > (int)(n); i--)
#define FOR(itr, v) for (auto itr = (v).begin(); itr != (v).end(); ++itr)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define ERASE(a) (a).erase(unique((a).begin(), (a).end()), (a).end()); // sortしてから
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> void PrintVector(const vector<T> &vec) {for (auto val : vec) cout << val << " "; cout << endl;}
const long long INF = 1LL << 60;
const long long minusINF = -(1LL << 40);
const int MOD = 1000000007;
const double PI = acos(-1); //3.14~
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#define pu(x) cout << (x) << endl
int main()
{
int n, x; cin >> n >> x;
int v, p;
x *= 100;
rep(i, n)
{
cin >> v >> p;
x = x - v * p;
if (x < 0)
{
pu(i + 1);
return (0);
}
}
pu(-1);
}
|
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep2(i, x, n) for(int i = x; i < (n); i++)
using namespace std;
using ll = long long;
//const double PI = 3.14159265358979323846;
int main() {
ll a, b, c;
cin >> a >> b >> c;
if(c % 2 == 0) {
a = a * a;
b = b * b;
if(a > b)
cout << ">" << endl;
else if(a < b)
cout << "<" << endl;
else
cout << "=" << endl;
} else {
if(a >= 0 && b >= 0) {
if(a > b)
cout << ">" << endl;
else if(a < b)
cout << "<" << endl;
else
cout << "=" << endl;
} else if(a >= 0 && b < 0) {
cout << ">" << endl;
} else if(a < 0 && b >= 0) {
cout << "<" << endl;
} else if(a < 0 && b < 0) {
if(a > b)
cout << ">" << endl;
else if(a < b)
cout << "<" << endl;
else
cout << "=" << endl;
}
}
return 0;
} | #include <iostream>
using namespace std;
typedef long long ll;
ll abso(ll a){
if(a < 0){
return -a;
}else{
return a;
}
}
int main(){
ll A, B, C;
cin >> A >> B >> C;
if(A == B){
cout << "=";
}else if(C%2 == 0){
if(abso(A) == abso(B)){
cout << "=";
goto end;
}
cout << ((abso(A) < abso(B)) ? "<" : ">");
}else{
cout << ((A < B) ? "<" : ">");
}
end:
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define N 200005
char s[N];int l,i,st[N],hd;
int main(){
scanf("%d%s",&l,s+1);
for(i=1;i<=l;++i){
st[++hd]=s[i];
while(hd>2&&st[hd-2]=='f'&&st[hd-1]=='o'&&st[hd]=='x')hd-=3;
}
cout<<hd;
} | #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");}
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define debugbit(x, n) cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
//#define int long long
//using mint = atcoder::modint;
typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;
void solve(long long N, std::string s){
if (s.size() < 3) {
cout << s.size() << endl;
return;
}
deque<char> queue;
rep(i,0,N) queue.push_back(s[i]);
auto itr = queue.begin();
int cnt = 0;
while (!queue.empty() && itr != queue.end()) {
auto itr2 = itr+1;
if (itr2 == queue.end()) break;
auto itr3 = itr+2;
if (itr3 == queue.end()) break;
if (*itr == 'f' && *itr2 == 'o' && *itr3 == 'x') {
++cnt;
itr = queue.erase(itr);
itr = queue.erase(itr);
auto tmp = queue.erase(itr);
if (queue.size() < 3) break;
if (tmp == queue.end()) {
itr = queue.end() - 3;
} else if (tmp == queue.begin() || (tmp-1) == queue.begin() || (tmp-2) == queue.begin()) {
itr = queue.begin();
} else {
itr = tmp-2;
}
} else {
itr++;
}
}
cout << s.size() - 3 * cnt << endl;
}
signed main(){
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
long long N;
scanf("%lld",&N);
std::string s;
std::cin >> s;
solve(N, s);
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.