code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#ifdef _LOCAL
# include <dbg_print.h>
#else
# define NDEBUG
# define dprint(...)
#endif
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
#include <assert.h>
using namespace std;
typedef long long ll;
bool connected[32][32] = {0};
int n, m;
int check_hist[1<<18] = {0};
bool check(int nodes)
{
if(check_hist[nodes])
return (check_hist[nodes] == 2);
for(int i=0; i<n; ++i)
{
if(nodes & (1<<i))
{
for(int j=0; j<i; ++j)
{
if(nodes & (1<<j))
{
if(!connected[i][j])
{
check_hist[nodes] = 1;
return false;
}
}
}
}
}
check_hist[nodes] = 2;
return true;
}
int hist[1<<18] = {0};
int solve(int nodes)
{
assert(nodes);
if(hist[nodes])
return hist[nodes];
if(check(nodes))
return 1;
int n1 = __builtin_popcount(nodes);
int res = 999;
for(int submask=1; submask<(1<<n1); ++submask)
{
int mask = 0;
for(int i=0,j=0; i<n; ++i) if(nodes & (1<<i))
{
if((submask>>j)&1)
mask |= (1<<i);
++j;
}
if(!check(mask))
continue;
int r = 1 + solve(nodes & (~mask));
res = std::min(res, r);
}
return hist[nodes] = res;
}
int solve2(int fullMask, int i0, int currMask)
{
if(!fullMask)
return 0;
if(currMask == 0)
{
if(hist[fullMask])
return hist[fullMask];
if(check(fullMask))
return 1;
}
int res = 999;
if(currMask)
res = 1 + solve2(fullMask & (~currMask), 0, 0);
for(int i=i0; i<n; ++i) if(fullMask & (1<<i))
{
currMask |= (1<<i);
if(check(currMask))
{
int r = solve2(fullMask, i+1, currMask);
res = std::min(res, r);
}
currMask &= ~(1<<i);
}
if(currMask == 0)
hist[fullMask] = res;
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i=0; i<m; ++i)
{
int u, v;
cin >> u >> v;
--u, --v;
connected[u][v] = connected[v][u] = 1;
}
int ans = solve2((1<<n)-1, 0, 0);
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define bit(n) (1LL<<(n))
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; }
#define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} }
#define pb push_back
#define fi first
#define se second
#define int long long
#define INF 1000000000000000000
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p){ cout << '(' << p.first << ',' << p.second << ')'; return os; }
template<typename T> void Out(T x) { cout << x << endl; }
template<typename T1, typename T2> void chOut(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); }
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using v_bool = vector<bool>;
using v_Pii = vector<Pii>;
//int dx[4] = {1,0,-1,0};
//int dy[4] = {0,1,0,-1};
//char d[4] = {'D','R','U','L'};
const int mod = 1000000007;
//const int mod = 998244353;
signed main(){
int N, M; cin >> N >> M;
vec A(M), B(M);
bool f[18][18] = {};
REP(i, M){
cin >> A[i] >> B[i];
A[i]--; B[i]--;
f[A[i]][B[i]] = true;
f[B[i]][A[i]] = true;
}
v_bool ok(bit(N), false);
FOR(b, 1, bit(N)){
bool t = true;
REP(i, N) REP(j, i) if((b >> i) & (b >> j) & 1){
if(!f[i][j]) t = false;
}
ok[b] = t;
}
vec dp(bit(N), INF);
dp[0] = 0;
FOR(sup, 1, bit(N)){
int sub = sup;
do{
if(ok[sub]){
chmin(dp[sup], dp[sup ^ sub] + 1);
}
sub = (sub - 1) & sup;
}while(sub != sup);
}
Out(dp[bit(N) - 1]);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9;
signed main()
{
int a,b;
cin>>a>>b;
a=a*2+100;
cout<<max(0ll,a-b)<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if (15<=a+b&&8<=b){
cout<<1;
}
else if (10<=a+b&&3<=b){
cout<<2;
}
else if (3<=a+b){
cout<<3;
}
else{
cout<<4;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(40)
const ll MOD = 7;
const ll INF = (ll) 1e15;
char C[2] = {'T', 'A'};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
string S, X;
cin >> S >> X;
ll c = 0;
vector<bool> U(10, false);
U[0] = true;
ll p = 1;
REP(i, N) {
ll m = (S[N - 1 - i] - '0') * p % MOD;
if (X[N - 1 - i] != C[c]) {
c = (c + 1) % 2;
REP(j, U.size()) {
U[j] = !U[j];
}
}
vector<bool> u(10, false);
REP(j, U.size()) {
u[j] = u[j] || U[j] || U[(j + m) % 7];
}
swap(U, u);
p = p * 10 % MOD;
#if false
REP(j, 7) {
if (U[j])
cout << "O";
else
cout << ".";
}
cout << endl;
#endif
}
bool aoki = false;
if (C[c] == 'T') {
if (!U[0])
aoki = true;
}
else {
if (U[0])
aoki = true;
}
if (aoki)
cout << "Aoki" << endl;
else
cout << "Takahashi" << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int n;
int dp[200001][8] ;
string s, x ;
bool fun(int idx, int num) {
if (idx > n) return (num == 0) ;
int &ret = dp[idx][num] ;
if (ret != -1) return ret ;
int cur = 0 ;
if (x[idx] == 'A') {
cur = fun(idx+1, (num * 10)%7) ;
if (cur == 1) cur = fun(idx+1, (num * 10 + (s[idx] - '0'))%7) ;
}
else {
cur = fun(idx+1, (num * 10)%7) ;
if (cur == 0) cur = fun(idx+1, (num * 10 + (s[idx] - '0'))%7) ;
}
return ret = cur ;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s >> x;
s = "#" + s ;
x = "#" + x ;
memset(dp, -1, sizeof(dp)) ;
bool ok = fun(1, 0) ;
string ans = "Aoki" ;
if (ok) ans = "Takahashi" ;
cout << ans << endl ;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forin(in) for(ll i=0; i<(ll)in.size(); i++) cin>>in[i]
#define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<" "
#define rep(i, n) for(ll i=0; i<(n); i++)
#define rep_up(i, a, n) for (ll i = a; i < n; ++i)
#define rep_down(i, a, n) for (ll i = a; i >= n; --i)
#define P pair<ll, ll>
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 all(v) v.begin(), v.end()
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>
constexpr ll INF = (1ll << 60);
constexpr ll mod = 1000000007;
int main() {
ll N; cin >> N;
vll A(N+1); rep(i,N) cin >> A[i];
A[N] = 0;
sort(all(A));
ll ans = 1;
rep(i,N) {
ans *= (A[i+1]-A[i]+1);
ans %= mod;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr lint B1 = 1532834020;
constexpr lint M1 = 2147482409;
constexpr lint B2 = 1388622299;
constexpr lint M2 = 2147478017;
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }
template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<lint> V1(N), V2(N);
IREP(i, N) cin >> V1[i];
REP(i, N) cin >> V2[i];
multiset<lint> st;
REP(i, N) {
if(st.size() > 0 && *st.begin() < min(V1[i], V2[i])) {
st.erase(st.begin());
st.insert(V1[i]);
st.insert(V2[i]);
} else {
st.insert(max(V1[i], V2[i]));
}
}
lint ans = 0;
for(lint x : st) ans += x;
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define _overload(_1, _2, _3, _4, name, ...) name
#define _rep1(Itr, N) _rep3(Itr, 0, N, 1)
#define _rep2(Itr, a, b) _rep3(Itr, a, b, 1)
#define _rep3(Itr, a, b, step) for (i64(Itr) = a; (Itr) < b; (Itr) += step)
#define repeat(...) _overload(__VA_ARGS__, _rep3, _rep2, _rep1)(__VA_ARGS__)
#define rep(...) repeat(__VA_ARGS__)
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
i64 n, m, q;
cin >> n >> m >> q;
vector<pair<i64, i64>> item(n); // first:w, second: v;
for (auto &vs : item) cin >> vs.first >> vs.second;
vector<pair<i64, i64>> x(m); // first:const, second: idx;
repeat(i, m) {
cin >> x[i].first;
x[i].second = i;
}
sort(x.begin(), x.end());
sort(item.begin(), item.end());
repeat(_, q) {
i64 l, r;
cin >> l >> r;
l--;
i64 ans = 0;
int idx = 0; // item_idx;
priority_queue<i64> que;
for (auto c : x) {
if (l <= c.second && c.second < r) continue;
while (idx < n && item[idx].first <= c.first) {
que.push(item[idx].second);
idx++;
}
if (!que.empty()) {
ans += que.top();
que.pop();
}
}
cout << ans << endl;
}
return 0;
} | #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\n'
#define debug(x) std::cerr << #x << " = " << (x) << endl;
using ll = long long;
using P = std::pair<ll, ll>;
constexpr ll INF = 1ll<<60;
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 S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }
template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }
ll N, M, Q;
ll W[100], V[100];
ll X[100];
std::vector<P> ps;
ll dp[60][60];
int main() {
std::cin >> N >> M >> Q;
rep( i, N ) {
std::cin >> W[i] >> V[i];
ps.emplace_back(W[i], V[i]);
}
std::sort(all(ps));
rep( i, M )
std::cin >> X[i];
rep( q, Q ) {
ll L, R;
std::cin >> L >> R;
std::vector<ll> xs;
rep( i, M ) {
if( i >= L-1 && i < R )
continue;
xs.emplace_back(X[i]);
}
std::sort(all(xs));
ll m = xs.size();
rep( i, N+5 ) rep( j, m+5 )
dp[i][j] = 0;
rep( i, N ) rep( j, m+1 ) {
ll k = std::lower_bound( xs.begin()+j, xs.end(), ps[i].first )-xs.begin();
chmax( dp[i+1][k+1], dp[i][j]+ps[i].second );
chmax( dp[i+1][j], dp[i][j] );
}
ll ans = 0;
rep( j, m+1 )
chmax( ans, dp[N][j] );
std::cout << ans << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define M 1000000007
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define d(x) cout << #x << " " << x << "\n";
#define min(x1, x2) (x1 > x2 ? x2 : x1)
#define max(x1, x2) (x1 < x2 ? x2 : x1)
#define min3(x1, x2, x3) (x3 > min(x1, x2) ? min(x2, x1) : x3)
#define max3(x1, x2, x3) (x3 < max(x1, x2) ? max(x1, x2) : x3)
#define ll long long int
#define ul unsigned long long int
#define p pair<ll, ll>
#define ld long double
#define dv(v) \
cerr << #v << " "; \
for (int i = 0; i < (v).size(); i++) \
cerr << v[i] << " "; \
cerr << "\n";
#define inf INT_MAX
#define mp(x, y) make_pair(x, y)
using namespace std;
vector<tuple<ul, ul, ul>> v;
ul dp[1 << 18][101];
int n;
int m;
ul fact[19];
void fac(ul n)
{
fact[0] = 1;
for (ul i = 1; i <= n; ++i)
{
fact[i] = (fact[i - 1] * i);
}
}
ll rec(ul i, int j)
{
if (dp[i][j] != -1)
{
return dp[i][j];
}
bitset<18> b(i);
if (j == m)
{
int tn = 0;
for (int k = 0; k < n; ++k)
{
if (b[k] == 1)
{
++tn;
}
}
return dp[i][j] = fact[n - tn];
}
ul x = get<0>(v[j]);
ul y = get<1>(v[j]);
ul z = get<2>(v[j]);
int no = 0;
int tn = 0;
for (int k = 0; k < n; ++k)
{
if (b[k] == 1)
{
++tn;
}
if (b[k] == 1 && k + 1 <= y)
{
++no;
}
}
if (no > z)
{
return dp[i][j] = 0;
}
else
{
if (tn == x)
{
return dp[i][j] = rec(i, j + 1);
}
else
{
dp[i][j] = 0;
for (ll k = 0; k < n; ++k)
{
if (b[k] == 0)
{
dp[i][j] += rec(i + (1 << k), j);
}
}
return dp[i][j];
}
}
}
int main()
{
fio;
ll t, x, y, z, k;
cin >> n >> m;
memset(dp, -1, sizeof(dp));
fac(18);
t = m;
while (t--)
{
cin >> x >> y >> z;
v.push_back(make_tuple(x, y, z));
}
sort(v.begin(), v.end());
cout << rec(0, 0) << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1 << 18;
int n, a[N], b[N], p[N], inv[N], ord[N];
vector<pair<int, int>> op;
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
rep(i, 0, n) cin >> a[i];
rep(i, 0, n) cin >> b[i];
rep(j, 0, n) cin >> p[j], --p[j], inv[p[j]] = j;
bool impossible = false;
rep(i, 0, n) if (p[i] != i && a[i] <= b[p[i]]) impossible = true;
if (impossible) return cout << "-1", 0;
rep(i, 0, n) ord[i] = i;
sort(ord, ord + n, [&](int x, int y) { return a[x] < a[y]; });
rep(k, 0, n) {
int i = ord[k];
if (p[i] == i) continue;
int j = inv[i];
op.emplace_back(i, j);
inv[i] = i;
p[j] = p[i];
inv[p[j]] = j;
}
cout << sz(op) << '\n';
for (auto [i, j] : op) cout << i + 1 << ' ' << j + 1 << '\n';
} |
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mset(a,b) memset(a, b, sizeof(a))
#define dbg(x) cout << "[" << #x << "]: " << x << endl;
#define forn(i,n) for(int i=0; i < n;i++)
#define nrof(i,n) for(int i = n-1; i >= 0; i--)
#define fori(i,a,b) for(int i = a; i <= b; i++)
#define irof(i,b,a) for(int i = b; i >= a; i--)
#define each(val, v) for(auto &val : v)
using ll = long long;
using db = long double; //pode ser double
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<string>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const db EPS = 1e-9;
const db PI = acos((db)-1);
void solve(){
int n;
cin >> n;
n -= 1;
cout << n << '\n';
}
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
/*
PRESTA ATENCAO!!!!
NAO FIQUE PRESO EM UMA ABORDAGEM
A SUA INTUICAO PODE ESTAR ERRADA - TENTE OUTRAS COISAS
LIMPOU TUDO PARA O PROXIMO CASO?
caso especial? n == 1
*/ | #include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ll n;
cin>>n;
ll a[n];
ll max=0,res=-1;
for(ll i=0;i<n;i++) cin>>a[i];
for(ll i=2;i<1000;i++)
{
ll cnt=0;
for(ll j=0;j<n;j++){
if(a[j]%i==0) cnt++;
}
if(cnt>max) max=cnt,res=i;
}
cout<<res;
return 0;
} |
/**
* author: kuins_air
* created: 28.05.2021 11:21:10
**/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, r, n) for(int i = r; i < (int)(n); i++)
#define ll long long
string S;
int main() {
cin >> S;
int n = (int)S.size();
rep(i, 1, (int)S.size() + 1) {
if(S[n-i] == '0') cout << 0;
if(S[n-i] == '1') cout << 1;
if(S[n-i] == '6') cout << 9;
if(S[n-i] == '8') cout << 8;
if(S[n-i] == '9') cout << 6;
}
cout << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
const int N=4e5+10;
int T,n;
char s1[N],s2[N],s3[N];
int pos[4][2][N],now[4];
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
scanf("%s%s%s",s1+1,s2+1,s3+1);
for(int i=1;i<=n;++i) printf("0");
for(int i=1;i<=n;++i) printf("1");
printf("0\n");
}
return 0;
} |
//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
if(b==0)return 1;
if(b==1)return(a%mod);
int tem=po(a,b/2);
if(b&1)return(((tem*tem)%mod)*a)%mod;
else return(tem*tem)%mod;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
signed main(){
IOS();
int n;
cin>>n;
int lg=__lg(n)+1;
F(n){
int x=(i+1)*2;
if((x%n)==0)x=n;
else x=(x%n);
cout<<x<<" ";
x=(i+1)*2+1;
if((x%n)==0)x=n;
else x=(x%n);
cout<<x<<endl;
}
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;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
printf("%d %d\n", 2 * i % n + 1, (2 * i + 1) % n + 1);
}
}
|
#include<bits/stdc++.h>
using namespace std;
bitset<101> acc[201];
int dir[201], nxt[201], tmp[201];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n * 2; i++) acc[i] = ~acc[i];
for (int i = 1; i <= n; i++) {
int a, b;
cin >> a >> b;
if (a != -1 && b != -1 && a >= b) {
puts("No");
return 0;
}
if (a != -1) {
if (dir[a]) {
puts("No");
return 0;
}
dir[a] = 1;
}
if (b != -1) {
if (dir[b]) {
puts("No");
return 0;
}
dir[b] = -1;
}
if (a != -1 && b != -1) {
tmp[a] = tmp[b] = 1;
for (int j = 1; j <= n; j++)
if (j != b-a) acc[a][j] = acc[b][j] = 0;
}
}
for (int i = 1; i <= n * 2; i++)
if (!tmp[i] && dir[i]) {
if (dir[i] == 1) {
for (int j = 1; j <= n; j++)
if (i + j > n * 2 || dir[i+j]) acc[i][j] = 0;
}
if (dir[i] == -1) {
for (int j = 1; j <= n; j++)
if (i - j <= 0 || dir[i-j]) acc[i][j] = 0;
}
}
nxt[0] = 1;
for (int i = 2; i <= n * 2; i += 2) {
for (int j = 1; j <= i / 2; j++) {
bool tmp = 1;
for (int k = i; k > i - j; k--)
if (dir[k] == 1 || !acc[k][j]) tmp = 0;
for (int k = i - j; k > i-j*2; k--)
if (dir[k] == -1 || !acc[k][j]) tmp = 0;
if (tmp && nxt[i-2*j]) {
nxt[i] = 1;
break;
}
}
}
if (nxt[n*2]) puts("Yes");
else puts("No");
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<pair<int, int>> p(n);
for(auto& [x, y] : p) cin >> x >> y;
for(int i = n; i--; ) for(int j = i; j--; ) for(int k = j; k--; ){
auto [x1, y1] = p[i];
auto [x2, y2] = p[j];
auto [x3, y3] = p[k];
x1 -= x3;
x2 -= x3;
y1 -= y3;
y2 -= y3;
if(x1 * y2 == x2 * y1){
puts("Yes");
return 0;
}
}
puts("No");
}
|
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v[100000];
pair<int, pair<int, int>> dfs(int x) {
int p1 = 0, p2 = 0, sa = 0, sb = 0, f = 0, i;
vector<pair<int, pair<int, int>>> w;
p1++;
f = 1 - f;
for (i = 0; i < v[x].size(); i++) {
pair<int, pair<int, int>> p = dfs(v[x][i]);
int x = p.second.first;
int y = p.second.second;
if (p.first == 0) {
if (x <= y) {
p1 += x;
p2 += y;
} else {
sa += x;
sb += y;
}
} else {
w.push_back(make_pair(x - y, make_pair(x, y)));
}
}
sort(w.begin(), w.end());
for (i = 0; i < w.size(); i++) {
int x = w[i].second.first;
int y = w[i].second.second;
if (f == 1) {
p1 += x;
p2 += y;
} else {
p1 += y;
p2 += x;
}
f = 1 - f;
}
if (f == 1) {
p1 += sa;
p2 += sb;
} else {
p1 += sb;
p2 += sa;
}
return make_pair(f, make_pair(p1, p2));
}
int main() {
int n, i;
scanf("%d", &n);
for (i = 1; i < n; i++) {
int p;
scanf("%d", &p);
v[p - 1].push_back(i);
}
printf("%d\n", dfs(0).second.first);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// DEBUG BEGIN
#ifdef LOCAL
template<class L, class R> ostream &operator<<(ostream &out, const pair<L, R> &p){
return out << "(" << p.first << ", " << p.second << ")";
}
template<class Tuple, size_t N> struct TuplePrinter{
static ostream &print(ostream &out, const Tuple &t){ return TuplePrinter<Tuple, N-1>::print(out, t) << ", " << get<N-1>(t); }
};
template<class Tuple> struct TuplePrinter<Tuple, 1>{
static ostream &print(ostream &out, const Tuple& t){ return out << get<0>(t); }
};
template<class... Args> ostream &print_tuple(ostream &out, const tuple<Args...> &t){
return TuplePrinter<decltype(t), sizeof...(Args)>::print(out << "(", t) << ")";
}
template<class ...Args> ostream &operator<<(ostream &out, const tuple<Args...> &t){
return print_tuple(out, t);
}
template<class T> ostream &operator<<(class enable_if<!is_same<T, string>::value, ostream>::type &out, const T &arr){
out << "{"; for(auto x: arr) out << x << ", ";
return out << (arr.empty() ? "" : "\b\b") << "}";
}
template<size_t S> ostream &operator<<(ostream &out, const bitset<S> &b){
for(auto i = 0; i < S; ++ i) out << b[i];
return out;
}
void debug_out(){ cerr << "\b\b " << endl; }
template<class Head, class... Tail>
void debug_out(Head H, Tail... T){ cerr << H << ", ", debug_out(T...); }
void debug2_out(){ cerr << "\n"; }
template<class Head, class... Tail>
void debug2_out(Head H, Tail... T){ cerr << "\n"; for(auto x: H) cerr << x << "\n"; debug2_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#define debug2(...) cerr << "[" << #__VA_ARGS__ << "]", debug2_out(__VA_ARGS__)
#else
#define debug(...) 42
#define debug2(...) 42
#endif
// DEBUG END
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
int n;
cin >> n;
vector<vector<int>> adj(n);
for(auto u = 1; u < n; ++ u){
int p;
cin >> p, -- p;
adj[p].push_back(u);
}
vector<int> sz(n, 1);
function<void(int)> dfs_sz = [&](int u){
for(auto v: adj[u]){
dfs_sz(v);
sz[u] += sz[v];
}
};
dfs_sz(0);
vector<int> dp(n, 1);
function<void(int)> dfs = [&](int u){
int sum = 0;
array<vector<array<int, 2>>, 2> t;
for(auto v: adj[u]){
dfs(v);
t[sz[v] & 1].push_back({dp[v], sz[v] - dp[v]});
sum += dp[v];
}
if(t[1].empty()){
dp[u] += sum;
}
else{
if((int)t[1].size() & 1){
for(auto [x, y]: t[0]){
dp[u] += min(x, y);
}
}
else{
for(auto [x, y]: t[0]){
dp[u] += x;
}
}
sort(t[1].begin(), t[1].end(), [&](auto x, auto y){ return x[0] - x[1] < y[0] - y[1]; });
for(auto i = 0; i < (int)t[1].size(); ++ i){
dp[u] += t[1][i][i & 1];
}
}
};
dfs(0);
debug(dp);
cout << dp[0] << "\n";
return 0;
}
/*
*/
////////////////////////////////////////////////////////////////////////////////////////
// //
// Coded by Aeren //
// //
//////////////////////////////////////////////////////////////////////////////////////// |
#include <iostream>
using namespace std;
#define N 805
#define MAX_A 1000000000
#define rep(i, n) for(int i = 0; i < n; ++i)
int main() {
int n, k, lim;
int a[N][N];
int s[N][N];
rep(i, N) {
s[i][0] = 0;
s[0][i] = 0;
}
int ng = -1;
int ok = MAX_A;
int mid;
bool ext;
cin >> n >> k;
lim = ((k * k) / 2) + 1;
rep(i, n) {
rep(j, n) {
cin >> a[i][j];
}
}
while ((ng + 1) < ok) {
mid = (ng + ok) / 2;
rep(i, n) {
rep(j, n) {
s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j];
if (a[i][j] > mid)
s[i + 1][j + 1]++;
}
}
ext = false;
rep(i, n - k + 1) {
rep(j, n - k + 1) {
if ((s[i + k][j + k] + s[i][j] - s[i][j + k] - s[i + k][j]) < lim)
ext = true;
}
}
if (ext)
ok = mid;
else
ng = mid;
}
cout << ok << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair <int, int> pin;
const int N = 805;
const int M = 7e5 + 5;
const int Maxn = 1e9;
const ll P = 998244353LL;
const int inf = 1e9 + 5;
int n, k, pos, a[N][N], sum[N][N], ans = inf;
template <typename T>
inline void read(T &X) {
char ch = 0; T op = 1;
for (X = 0; ch > '9' || ch < '0'; ch = getchar())
if (ch == '-') op = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
X = (X * 10) + ch - '0';
X *= op;
}
inline int getSum(int x1, int y1, int x2, int y2) {
return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
}
inline bool chk(int mid) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
sum[i][j] = 0;
if (a[i][j] <= mid) sum[i][j] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
sum[i][j] += sum[i][j - 1];
for (int j = 1; j <= n; j++)
for (int i = 1; i <= n; i++)
sum[i][j] += sum[i - 1][j];
for (int i = 1; i <= n - k + 1; i++)
for (int j = 1; j <= n - k + 1; j++)
if (getSum(i, j, i + k - 1, j + k - 1) >= pos)
return 1;
return 0;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("sample.in", "r", stdin);
clock_t st_clock = clock();
#endif
read(n), read(k);
int m = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
read(a[i][j]);
pos = k * k - (k * k / 2);
int l = 0, r = inf, mid, res;
for (; l <= r; ) {
mid = (l + r) / 2;
if (chk(mid)) res = mid, r = mid - 1;
else l = mid + 1;
}
printf("%d\n", res);
#ifndef ONLINE_JUDGE
clock_t ed_clock = clock();
printf("time = %f ms\n", (double)(ed_clock - st_clock) / CLOCKS_PER_SEC * 1000);
#endif
return 0;
} |
// youngjinp20
// 2021 06
#include<stdio.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; }
#define byr(x) [](const auto& a, const auto& b) { return a.x > b.x; }
#define smax(a, b) ((a) < (b) ? ((a)=(b), true) : false)
#define smin(a, b) ((a) > (b) ? ((a)=(b), true) : false)
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;
typedef long long ll;
int main() {
int N;
cin >> N;
int i=0;
int ct=0;
for (; N > ct; ct += ++i);
cout << i;
return 0;
}
| #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)...);
}
void OUTX2(){cout << endl;}
template<typename Head, typename... Tail>
void OUTX2(Head&& head, Tail&&... tail)
{
cout << head << ' ';
OUTX2(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)
ll InputDecimalAsLL2(ll base)
{
string s;
INX(s);
int pos = s.find('.');
ll result1 = stoll(s.substr(0, pos));
for(ll i = 0; i < base; i++) result1 *= 10;
ll result2;
if(pos < 0) result2 = 0;
else {
s = s.substr(pos + 1, s.size() - pos);
ll ssize = (ll)s.size();
s.resize(base + 1);
for(ll i = ssize; i < base; i++) s[i] = '0';
s[base] = '\0';
result2 = (pos < 0) ? 0 : stoll(s);
}
return result1 + result2;
}
#define MUL 10000
ll MyCeil(ll x)
{
if(x <= 0)
return MUL * (x / MUL);
else
return MUL * ((x - 1) / MUL + 1);
}
ll MyFloor(ll x)
{
if(x >= 0)
return MUL * (x / MUL);
else
return MUL * ((x + 1) / MUL - 1);
}
int main()
{
ll X, Y, R;
X = InputDecimalAsLL2(4);
Y = InputDecimalAsLL2(4);
R = InputDecimalAsLL2(4);
ll r2 = R * R;
ll result = 0;
for(ll nowx = MyCeil(X - R), endx = MyFloor(X + R); nowx <= endx; nowx += MUL)
{
ll tmp0 = nowx - X;
tmp0 *= tmp0;
ld tmp = sqrt((ld)(r2 - tmp0));
ll uppery = MyFloor(Y + (ll)tmp) / MUL;
ll lowery = MyCeil(Y - (ll)tmp) / MUL;
if(uppery >= lowery)
{
result += uppery - lowery + 1;
}
}
OUTX(result);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int n,t,t1,t2;
string s,s1,s2;
cin >> n;
t1=0;
t2=-1;
s1="a";
s2="b";
rep(i,n){
cin >> s >> t;
if(t>t1){
t2=t1;
s2=s1;
t1=t;
s1=s;
}else if(t>t2){
t2=t;
s2=s;
}
}
cout << s2 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}
int main2() {
string S;
cin >> S;
deque<char> T;
int side = 0;
for (char ch : S) {
if (ch == 'R') {
side = 1 - side;
} else {
if (side == 0) {
T.push_back(ch);
} else {
T.push_front(ch);
}
}
}
if (side == 1) {
side = 0;
reverse(ALL(T));
}
string ans;
REP(i, T.size()) {
if (!ans.empty() && ans.back() == T[i]) {
ans.pop_back();
} else {
ans.push_back(T[i]);
}
}
cout << ans << endl;
return 0;
}
int main() {
#ifdef LOCAL
for (;!cin.eof();cin>>ws)
#endif
main2();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define int ll
#define ld long double
#define reps(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rep(i,n) reps(i,0,n)
#define rreps(i,s,n) for(int i=(int)(s-1);i>=n;i--)
#define rrep(i,n) rreps(i,n,0)
#define all(v) (v).begin(),(v).end()
#define mset(a,n) memset(a,n,sizeof(a))
#define eras(v,n) (v).erase(remove(all(v),n),(v).end())
#define uni(v) sort(all(v));(v).erase(unique(all(v)),(v).end())
#define bcnt(i) __builtin_popcount(i)
#define fi first
#define se second
constexpr ll INF = 1e18;
constexpr ll MOD = 1000000007;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>T min(vector<T> &v) { return *min_element(v.begin(),v.end()); }
template<class T>T max(vector<T> &v) { return *max_element(v.begin(),v.end()); }
template<class T>T modpow(T a, T b, T m) { a %= m; T r = 1; while (b > 0) { if (b & 1) r = (r * a) % m; a = (a * a) % m; b >>= 1; } return r; }
ll modinv(ll a,ll m=MOD) {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;}
template<class T>string radixconv(T n, T k){ string s; while(n/k){ s=to_string(n%k)+s; n/=k; } s=to_string(n%k)+s; return s; }
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int n,m,q;
cin >> n >> m >> q;
vector<pair<int,int>> vw(n);
rep(i,n)cin >> vw[i].se >> vw[i].fi;
sort(all(vw), [](pair<int,int> a, pair<int,int> b){
return a.first>b.first|| (a.first==b.first && a.second<b.second);
});
vector<pair<int,int>> xi(m);
rep(i,m)cin >> xi[i].fi,xi[i].se=i;
sort(all(xi));
vector<int> ans(q,0);
rep(i,q){
int l,r;
cin >> l >> r;
l--;r--;
vector<bool> used(m,0);
rep(j,n){
rep(k,m){
if(l<=xi[k].se&&xi[k].se<=r)continue;
if(vw[j].se<=xi[k].fi&&!used[xi[k].se]){
used[xi[k].se]=1;
ans[i]+=vw[j].fi;
break;
}
}
}
}
rep(i,q)cout << ans[i] << endl;
return 0;
}
| #pragma GCC optimize ("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#include"bits/stdc++.h"
using namespace std;
const int p = 31;
const int M = 998244353;
typedef long long ll;
typedef long double d;
typedef unsigned long long ull;
typedef int in;
#define ar array
#define ff first
#define ss second
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m, q;
cin >> n >> m >> q;
vector<vector<ll>>bag;
for (int i = 0; i < n; ++i) {
int w, v;
cin >> w >> v;
// bag.push_back({w, v});
bag.push_back({v, w});
}
sort(bag.begin(),bag.end());
vector<int>box(m);
for (int i = 0; i < m; ++i)
cin >> box[i];
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l, --r;
vector<int>tmp;
for (int j = 0; j < m; ++j) {
if (j < l || j > r)
tmp.push_back(box[j]);
}
int sz = tmp.size();
sort(tmp.begin(), tmp.end());
vector<int>seen(sz);
ll ans = 0;
for (int k = n-1; k >=0 ; --k) {
for (int j = 0; j < sz; ++j) {
if (seen[j]||tmp[j] < bag[k][1])
continue;
seen[j] = 1;
ans += bag[k][0];
break;
}
}
cout << ans << '\n';
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; //check time
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define sz(a) (int)(a.size())
#define pb push_back
#define all(c) c.begin(),c.end()
#define tr(it,a,c) for(auto it=c.begin()+a;it!=c.end();it++)
#define fr(i,a,n) for(int i=a;i<n;i++)
#define present(c,x) (c.find(x)!=c.end()) //for set/map etc.
#define cpresent(c,x) (find(all(c),x)!=c.end())
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<pair<int,int>> vp;
int N=100001;
int mod=1e9+7;
int min(int a,int b){if(a<b) return a; else return b;}
int max(int a,int b){if(a>b) return a; else return b;}
int power(int a,int b){int res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int gcd(int p, int q){if(p%q==0) return q; else{return gcd(q,p%q);}}
bool comp(int a, int b)
{
return (a > b);
}
bool comp1(pi a, pi b)
{
if(a.first==b.first)return a.second<b.second;
else return a.first>b.first;
}
template<typename T>
istream& operator>>(istream& in, vector<T>& v)
{
for (T& x : v) in >> x; return in;
}
template<typename T>
ostream& operator<<(ostream& out, vector<T>& v)
{
for (T& x : v) out << x<<" "; return out;
}
// template<typename T>
int sum1(vector<int>& v)
{
int sum=0;
// for(auto x: v)sum+=x;
fr(i,0,v.size())sum+=v[i];
return sum;
}
void solve()
{
int n;
cin>>n;
vi v(n);
cin>>v;
if(n==1){cout<<v[0];return;}
vvi min1(n,vi(n,0));
fr(i,0,n)
{
min1[i][i]=v[i];
}
fr(i,0,n-1)
{
fr(j,i+1,n)
{
if(v[j]<min1[i][j-1])
min1[i][j]=v[j];
else
min1[i][j]=min1[i][j-1];
}
}
int ans=0;
fr(i,0,n)
{
fr(j,0,n)
{
min1[i][j]=min1[i][j]*(j-i+1);
ans=max(ans,min1[i][j]);
}
}
cout<<ans;
}
signed main()
{
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
// int t;
// cin>>t;
// while(t--)
// {
// solve();
// cout<<endl;
// }
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
void Main();
using i8 = int8_t; /* -128 ~ 127 */
using u8 = uint8_t; /* 0 ~ 255 */
using i16 = int16_t; /* -32,768 ~ 32,767 */
using u16 = uint16_t; /* 0 ~ 65,535 */
using i32 = int32_t; /* -2,147,483,648 ~ 2,147,483,647 */
using u32 = uint32_t; /* 0 ~ 4,294,967,295 */
using i64 = int64_t; /* -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 */
using u64 = uint64_t; /* 0 ~ 18,446,744,073,709,551,615 */
using f32 = float; /* (-3.4 * 10^38) ~ (3.4 * 10^38) */
using f64 = double; /* (-1.7 * 10^308) ~ (1.7 * 10^308) */
using f80 = __float80;
template <class T> using Vec = vector<T>;
constexpr i64 INF = 1010000000000000017;
constexpr i64 MOD = 998244353;
constexpr f64 EPS = 1e-12;
constexpr f64 PI = 3.14159265358979323846;
#define ALL(v) v.begin(), v.end()
#define YN(a, b, c) ((c)? a : b)
#define GCD(a, b) __gcd(a, b) /* 最大公約数 */
#define LCM(a, b) (i64)(a / (f64)__gcd(a, b) * b) /* 最小公倍数 */
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
Main();
return 0;
}
template<i64 MOD> struct Fp {
i64 val;
constexpr Fp(i64 v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr u64 getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
i64 a = r.val, b = MOD, u = 1, v = 0;
while (b) {
i64 t = a / b;
a -= t * b; swap(a ,b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, i64 n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mi64 = Fp<MOD>;
i64 modpow(i64 a, i64 n, i64 mod) {
i64 res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
void Main() {
i32 H, W;
cin >> H >> W;
Vec<Vec<i32>> M(H + W - 1, Vec<i32>(3));
for (i32 i = 0; i < H; i++) {
for (i32 j = 0; j < W; j++) {
char S;
cin >> S;
i32 index = (S == 'R')? 0 : (S == 'B')? 1 : 2;
M[i + j][index]++;
}
}
mi64 ans = 1;
for (i32 c = 0; c < H + W - 1; c++) {
auto m = M[c];
if (m[0] == 0 && m[1] == 0) ans *= 2;
else if (m[0] != 0 && m[1] != 0) ans = 0;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1505;
int pref[N][N], block[N][N];
int main(){
int h,w,n,m;
scanf("%d %d %d %d",&h,&w,&n,&m);
for(int i=0;i<=h+1;i++){
block[i][0] = block[i][w+1] = -1;
}
for(int i=0;i<=w+1;i++){
block[0][i] = block[h+1][i] = -1;
}
for(int i=0;i<n;i++){
int x,y;
scanf("%d %d",&x,&y);
pref[x][y]++;
}
for(int i=0;i<m;i++){
int x,y;
scanf("%d %d",&x,&y);
block[x][y] = -1;
}
for(int i=1;i<=h+1;i++){
for(int j=1;j<=w+1;j++){
pref[i][j] += pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1];
}
}
for(int i=0;i<=h+1;i++){
for(int j=0;j<=w+1;j++){
// printf("%d ",pref[i][j]);
}
// puts("");
}
for(int i=1;i<=h;i++){
int start = 0;
for(int j=1;j<=w+1;j++){
if(block[i][j] == -1){
int pt = pref[i][j-1]-pref[i][start]-pref[i-1][j-1]+pref[i-1][start];
// printf("%d %d\n",j,pt);
if(pt > 0){
// printf("%d\n",j);
for(int k=start+1;k<j;k++){
block[i][k] = 1;
}
}
start = j;
}
}
}
for(int i=1;i<=w;i++){
int start = 0;
for(int j=1;j<=h+1;j++){
if(block[j][i] == -1){
int pt = pref[j-1][i]-pref[start][i]-pref[j-1][i-1]+pref[start][i-1];
if(pt > 0){
for(int k=start+1;k<j;k++){
block[k][i] = 1;
}
}
start = j;
}
}
}
int ans = 0;
for(int i=0;i<=h+1;i++){
for(int j=0;j<=w+1;j++){
if(block[i][j] == 1){
ans++;
}
// printf("%4d",block[i][j]);
}
// puts("");
}
printf("%d\n",ans);
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define REP(i,m,n) for(ll i = (m); i < (ll)(n); i++)
#define rep(i,n) REP(i, 0, n)
#define rrep(i,n) REP(i,1,n+1)
const int MX = 1505;
const int di[] = {1, 0, -1, 0};
const int dj[] = {0, -1, 0, 1};
int h,w,n,m;
bool light[MX][MX];
bool wall[MX][MX];
bool ok[MX][MX];
bool visited[MX][MX];
bool memo[MX][MX];
bool f(int v, int i, int j) {
if (i < 0 || i >= h || j < 0 || j >= w) return false;
if (wall[i][j]) return false;
if (light[i][j]) return true;
if (visited[i][j]) return memo[i][j];
visited[i][j] = true;
return memo[i][j] = f(v, i + di[v], j + dj[v]);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin >> h >> w >> n >> m;
rep(i,n) {
int r,c;
cin >> r >> c;
--r; --c;
light[r][c] = true;
}
rep(i,m) {
int r,c;
cin >> r >> c;
--r; --c;
wall[r][c] = true;
}
rep(v,4) {
rep(i,h) rep(j,w) visited[i][j] = false;
rep(i,h) rep(j, w) if (f(v,i,j)) ok[i][j] = true;
}
int ans = 0;
rep(i,h) rep(j,w) if (ok[i][j]) ++ans;
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int a,b,c;
int main( ) {
cin>>a>>b>>c;
a%=mod, b%=mod, c%=mod;
a=1ll*a*(a+1)/2%mod;
b=1ll*b*(b+1)/2%mod;
c=1ll*c*(c+1)/2%mod;
cout<<1ll*a*b%mod*c%mod;
return 0;
} | #include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target ("avx2")
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#define ll long long int
#define endl '\n'
#define M 998244353
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define FOR(i,a,b) for(i=a;i<b;i++)
#define RFOR(i,a,b) for(i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
int main()
{
flash;
//sieve();
ll T=1,t=1,n,m,q,k,i,j;
// cin>>T;
while(T--)
{
cin>>n>>m>>k;
ll sn = (n *(n+1))/2;
sn = sn%M;
ll sm = (m *(m+1))/2;
sm = sm%M;
ll sk = (k *(k+1))/2;
sk = sk%M;
ll ans = ((sn%M * sm%M)%M * sk%M)%M;
cout<<ans<<endl;
}
} |
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <complex>
#include <vector>
#include <limits>
#include <iomanip>
#include <cassert>
#include <numeric>
#include <chrono>
#include <random>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << " = " << (x) << endl;
#define debug_pii(x) cerr << "(" << x.first << "," << x.second << ")";
#define rep(i, n) for(int i = 0;i < n;i++)
#define pb push_back
#define F first
#define S second
// template<typename _Ty1, typename _Ty2>
// ostream& operator<<(ostream& _os, const pair<_Ty1, _Ty2>& _p) {
// _os << '(' <<_p.first << ',' << _p.second << ')';
// return _os;
// }
//
// template<typename _Ty1, typename _Ty2>
// istream& operator>>(istream& _is, pair<_Ty1, _Ty2>& _p) {
// _is >> _p.first >> _p.second;
// return _is;
// }
void solve() {
string s;
cin>>s;
int n = s.size();
vector<int> F(26);
int i = n-3, j = n-2, k = n-1;
ll ans = 0;
while (i >= 0) {
if(s[i] == s[j] && s[j] != s[k]) {
ans += n-k-1-F[s[i]-'a']+1;
F.assign(26,0);
F[s[i]-'a']= n-1-j;
} else {
F[s[k]-'a']++;
}
i--;j--;k--;
}
cout<<ans<<endl;
}
// 0123....i=j!=k......n-1
// aababbbbb
// aaaabbbbb
// aaaaabbbb
// d+1, d+2, d+3, ..., d' ,... , M
// 0 [1]
// 1 [1]
// 2 [2,4,8,6,2]
// 3 [3,9,7,1,3]
// 4 [4,6,4]
// 5 [1]
// 6 [1]
// 7 [7,9,3,1,7]
// 8 [8,4,2,6,8]
// 9 [9,1,9]
int main() {
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
// cout << fixed << setprecision(15);2000000000
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// prime_factors();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
| #include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<algorithm>
#include<utility>
#include<tuple>
#include<map>
#include<queue>
#include<deque>
#include<set>
#include<stack>
#include<numeric>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
struct Edge {
int to;
long long weight;
Edge(int to, long long weight) : to(to), weight(weight) {}
};
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
using weightedGraph = vector<vector<Edge>>;
#define BIL ((ll)1e9)
#define MOD ((ll)1e9+7)
#define INF (1LL<<60) //1LL<<63でオーバーフロー
#define inf (1<<29) //1<<29でオーバーフロー
int main(int argc,char* argv[]){
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
string s;
cin >> s;
int n = s.length();
ll ans = 0;
vector<vector<int>> dp(26, vector<int>(n+1, 0));
for(int i=n-1;i>=0;i--){
int num = s[i]-'a';
if(i-1 >= 0 && s[i]==s[i-1]){
ans += n-1-i-dp[num][i+1];
// cout << "s[i]: " << s[i] << " ans: " << n-1-i-dp[num][i+1] << endl;
for(int j=0;j<26;j++){
if(j==num) dp[j][i]=n-i;
else dp[j][i]=0;
}
} else {
for(int j=0;j<26;j++){
if(j==num) dp[j][i] = dp[j][i+1]+1;
else dp[j][i] = dp[j][i+1];
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <climits>
using namespace std;
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ps(x,y) fixed<<setprecision(y)<<x
#define int long long
int solve(int arr[],int n,int sum){
bool dp[n+1][sum+1];
memset(dp,false,sizeof(dp));
for(int i=0;i<=n;i++){
dp[i][0]=true;
}
for(int i=1;i<=n;i++){
dp[0][i]=false;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=sum;j++){
dp[i][j]=dp[i-1][j];
if(arr[i-1]<=j){
dp[i][j] |= dp[i-1][j-arr[i-1]];
}
}
}
for(int j=sum/2;j>=0;j--){
if(dp[n][j]==true){
return (sum-j);
}
}
return -1;
}
int32_t main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
FIO;
int n,sum=0;
cin >> n;
int arr[n];
for(int i=0;i<n;i++) {
cin >> arr[i];
sum+=arr[i];
}
int ans = solve(arr,n,sum);
cout << ans << "\n";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(ll i=ll(a); i<=ll(b); i++)// a<=i<=b i++ のforの開始
#define ALL(a) (a).begin(),(a).end()//全要素,min,max,findなどで使う
#define ITE(e,a,i) auto ite=find(ALL(a),e);\
(i)=distance((a).begin(),ite);\
if(ite==(a).end()) i=-1;
#define EACH(i,a) for(auto (i): (a))
#define ERASE(a,i) (a).erase((a).begin()+i);//iは0始まり
#define ERASES(a,i,j) (a).erase((a).begin()+i,(a).begin()+j);//i,jは0始まり i<= u <jの範囲を消す i!=j
#define SORT(a) sort(begin(a),end(a))//小さい順にsort 1,2,3
#define SORT_(a) sort(begin(a),end(a),greater<ll>{})//大きい順にsort 5,4,3
#define OI(s) cout<<(s)<<endl
#define OII(s) printf("%8.7lf\n",(s));//double 小数点上8桁まで,下7桁まで出力
#define OI2(s0,s1) cout<<(s0)<<" "<<(s1)<<endl
#define OI3(s0,s1,s2) cout<<(s0)<<" "<<(s1)<<" "<<(s2)<<endl
#define OI4(s0,s1,s2,s3) cout<<(s0)<<" "<<(s1)<<" "<<(s2)<<" "<<(s3)<<endl
#define OIV(a) for(ll (iii)=0;(iii)<(a).size();(iii)++){cout<<(a)[(iii)]<<" ";} cout<<""<<endl
#define OIVV(a) for(ll (iii)=0;(iii)<(a).size();(iii)++){for(ll (jjj)=0;(jjj)<(a[(iii)]).size();(jjj)++){cout<<(a)[(iii)][(jjj)]<<" ";}cout<<" "<<endl;}
// vector<vector<ll>> a(5,vector<ll>(3,0));
//vector<vector<vector<double>>> abc(A, vector<vector<double>>(B, vector<double>(C,0)));
//vector<pair<ll,ll>> event;
// 20210606 Dまでいけなくてもいい,今週の成果をみせる
int main()
{
ll N;
cin >>N;
vector<ll> T(N);
ll sum=0;
FOR(i,0,N-1)
{
cin>>T[i];
sum+=T[i];
}
if(N==1)
{
OI(T[0]);
return 0;
}
vector<vector<bool>> dp(N,vector<bool>(sum+1,false));
FOR(i,0,N-1) dp[i][0] = true;
FOR(i,0,N-2)
{
FOR(j,0,sum)
{
if(dp[i][j])
{ // i番目を選ばない
dp[i+1][j] = true;
// i番目を選ぶ
dp[i+1][j+T[i]] = true;
}
}
}
//OIVV(dp);
ll S2;
if(sum%2==0) S2=sum/2;
else S2 = sum/2 +1;
ll ans = 1000000000001;
FOR(i,1,N-1)
{
FOR(j,S2,sum)
{
//OI3(i,j,dp[i][j]);
if(dp[i][j]) ans = min(j,ans);
}
}
OI(ans);
return 0;
} |
/******************************
Author: jhnah917(Justice_Hui)
g++ -std=c++14 -DLOCAL
******************************/
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;
typedef long long ll;
int N, K, A[9][9], R;
int main(){
cin >> N >> K;
for(int i=1; i<=N; i++) for(int j=1; j<=N; j++) cin >> A[i][j];
vector<int> ord(N);
iota(all(ord), 1);
do{
int now = 0, flag = 1;
for(int i=1; i<N; i++){
if(!A[ord[i-1]][ord[i]]) flag = 0;
now += A[ord[i-1]][ord[i]];
}
if(!A[ord.back()][ord[0]]) flag = 0;
now += A[ord.back()][ord[0]];
if(flag && now == K) R++;
}while(next_permutation(ord.begin()+1, ord.end()));
cout << R;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
const int dy[4] = { -1, 0, 1, 0 }, dx[4] = { 0, 1, 0, -1 };
char mp[1000][1000];
vector<int> gph[2000];
bool chk[2000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int r, c;
cin >> r >> c;
gph[0].push_back(r);
gph[r].push_back(0);
gph[0].push_back(r + c - 1);
gph[r + c - 1].push_back(0);
gph[r - 1].push_back(r);
gph[r].push_back(r - 1);
gph[r - 1].push_back(r + c - 1);
gph[r + c - 1].push_back(r - 1);
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
cin >> mp[i][j];
if (mp[i][j] == '#') {
gph[i].push_back(r + j);
gph[r + j].push_back(i);
}
}
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < r + c; ++i)
if (!chk[i]) {
queue<int> q;
chk[i] = true;
q.push(i);
bool chk0 = false, chk1 = false;
while (!q.empty()) {
int now = q.front();
q.pop();
if (now < r)
chk0 = true;
else
chk1 = true;
for (int nxt : gph[now])
if (!chk[nxt]) {
chk[nxt] = true;
q.push(nxt);
}
}
if (chk0)
cnt0++;
if (chk1)
cnt1++;
}
cout << min(cnt0, cnt1) - 1;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)
namespace IO {
const int MAXIOSIZE = 1 << 24 | 1;
unsigned char buf[MAXIOSIZE], *p1, *p2;
// #define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++)
#define gc getchar()
template <typename T> void read(T& x) {
x = 0; char ch = gc; bool flg = false;
for (; ch < '0' || '9' < ch; ch = gc) if (ch == '-') flg |= true;
for (; '0' <= ch && ch <= '9'; ch = gc) x = x * 10 + ch - '0';
flg ? x = -x : 0;
}
template <typename T> void out(const T& x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); }
template <typename T> inline void write(const T& x, const char& ed = ' ') { if (x < 0) putchar('-'), out(-x); else out(x); putchar(ed); }
}
const int MAXN = 5000 + 10;
const int P = 998244353;
int H, W, K, cntrow[MAXN][MAXN], cntcol[MAXN][MAXN], f[MAXN][MAXN][3], tag[MAXN][MAXN][3];
int pow3[MAXN];
int qpower(int a, int x) {
int ret = 1;
for (; x; x >>= 1, a = 1ll * a * a % P) ret = 1ll * ret * a % P;
return ret;
}
int toint(char ch) { return (ch == 'D' ? 0 : (ch == 'R' ? 1 : 2)); }
void add(int& a, int b) { a = a + b >= P ? a + b - P : a + b; }
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
IO::read(H), IO::read(W), IO::read(K);
pow3[0] = 1;
rep(i, 1, max(H, W)) pow3[i] = 1ll * pow3[i - 1] * 3 % P;
rep(i, 1, K) {
int x, y; char c[10]; IO::read(x), IO::read(y); scanf("%s", c + 1);
tag[x][y][toint(c[1])] = true;
}
rep(i, 1, H) rep(j, 1, W) cntrow[i][j] = cntrow[i][j - 1] + ((tag[i][j][0] + tag[i][j][1] + tag[i][j][2]) == 0);
rep(i, 1, W) rep(j, 1, H) cntcol[j][i] = cntcol[j - 1][i] + ((tag[j][i][0] + tag[j][i][1] + tag[j][i][2]) == 0);
int tt = -1;
rep(v, 0, 2) if (tag[1][1][v]) tt = v;
if (tt != -1) f[1][1][tt] = true;
else {
rep(v, 0, 2) f[1][1][v] = true;
}
rep(i, 1, H) rep(j, 1, W) {
if (i == 1 && j == 1) continue;
int tt = -1;
rep(v, 0, 2) {
if (tag[i][j][v]) tt = v;
add(f[i][j][v], 1ll * (f[i - 1][j][2] + f[i - 1][j][0]) * pow3[cntrow[i][j - 1]] % P);
add(f[i][j][v], 1ll * (f[i][j - 1][2] + f[i][j - 1][1]) * pow3[cntcol[i - 1][j]] % P);
}
if (tt != -1) {
rep(v, 0, 2) if (v != tt) f[i][j][v] = 0;
}
}
int ans = 0;
rep(v, 0, 2) add(ans, f[H][W][v]);
IO::write(ans);
return 0;
}
| #include <bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string.h>
using namespace std;
#define pb push_back
#define all(v) v. begin(),v. end()
#define rep(i,n,v) for(i=n;i<v;i++)
#define per(i,n,v) for(i=n;i>v;i--)
#define ff first
#define ss second
#define pp pair<ll,ll>
#define ll long long
#define endl '\n'
string tostr(ll n)
{ stringstream rr;rr<<n;return rr. str();}
ll toint(string s)
{stringstream ss(s);ll x;ss>>x;return x;}
void solve()
{
ll n, a,m=0,b=0, c=0,k=0, i, j,l=998244353;
string s, r, y;
ll hit=0, yog;
cin>>n;
rep(i, 1,n+1)
cout<<(2*i-1)%n+1<<" "<<(2*i)%n+1<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin. tie(0);cout. tie(0);
ll t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#define LL long long
using namespace std;
template <typename T> void read(T &x){
x = 0; int f = 1; char ch = getchar();
while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
x *= f;
}
inline void write(int x){if (x > 9) write(x/10); putchar(x%10+'0'); }
const int P = 998244353;
const int N = 5050;
int n,m;
LL pw[N][N],pre[N][N];
int main(){
// cerr << sizeof(pw)*2.0/1024/1024<<'\n';
register int i,j;
LL ans = 0;
read(n),read(m);
for (i = 1; i <= 5000; ++i)
for (pw[i][0] = j = 1; j <= 5000; ++j) pw[i][j] = pw[i][j-1] * i % P;
pw[0][0] = 1;
for (i = 1; i < n; ++i)
for (j = 0; j <= m; ++j){
if (j) pre[i][j] = (pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1] + pw[j][i-1] * pw[m][n-i-1]) % P;
else pre[i][j] = (pre[i-1][j] + pw[j][i-1] * pw[m][n-i-1]) % P;
}
LL s1,s2,s3 = 0;
for (i = 1; i <= n; ++i){
s1 = pw[m-1][i-1] * pw[m][n-i] % P;
s2 = (pw[m][n] - s1 + P) % P;
ans = (ans + s1 + s2) % P;
s3 = pre[i-1][m-1];
ans = (ans - s3 + P) % P;
}
ans = (ans % P + P) % P;
cout << ans << '\n';
return 0;
} | #include<stdio.h>
int n,m,a[1002],b[1002],dp[1002][1002];
inline int min(int x,int y){return x<y?x:y; }
inline int min(int x,int y,int z){ return min(x,min(y,z)); }
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),dp[i][0]=i;
for(int i=1;i<=m;i++)
scanf("%d",&b[i]),dp[0][i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+(a[i]!=b[j]));
printf("%d",dp[n][m]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 3000005;
int main()
{
int n;
ll k;
ll dp[4][N] = {0};
cin >> n >> k;
dp[0][0] = 1;
for (int i = 0; i < 3; i ++ )
{
for (int j = 0; j <= i * n; j ++ )
{
//只在1~n之间加上
dp[i + 1][j + 1] += dp[i][j];
dp[i + 1][j + n + 1] -= dp[i][j];
}
for (int j = 1; j <= (i + 1) * n; j ++ )
dp[i + 1][j] += dp[i + 1][j - 1];
}
int x;
for (int i = 3; i <= 3 * n; i ++ )
if (k <= dp[3][i]) {x = i; break;}
else k -= dp[3][i];
for (int i = 1; i <= n; i ++ )
{
//现在i是确定的,那么j和k就只会在l到r这个区间变换
//而且,变化的个数也就是r-1+1个
int l = max(1, x - n - i);
int r = min(n, x - i - 1);
if (l > r) continue;
if (k > r - l + 1) {k -= r - l + 1; continue;}
int y = l + k - 1;
int z = x - i - y;
cout << i << " " << y << " " << z << endl;
return 0;
}
} | #include<bits/stdc++.h>
#define pb push_back
#define pf push_front
#define clr(x) memset(x, 0, sizeof(x))
#define all(a) a.begin(),a.end()
#define s second
#define f first
#define forn(i, a,b) for(int i = int(a); i < int(b); ++i)
#define forn_r(i, b, a) for(int i = int(b); i > int(a); i--)
#define trav(a,x) for (auto& a : x)
#define printArray(array, length) for(int i = 0; i < length; i++) cout<< array[i] << (i == length - 1 ? '\n' : ' ');
#define print(x) cout << x << '\n'
#define print2(x, y) cout << x << ' ' << y << '\n'
#define debug(x) cout << #x << "=" << x << endl
#define debug2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
template<class T> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
using namespace std;
typedef long long ll;
const int INF = INT_MAX;
const ll LNF = LONG_LONG_MAX;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline int lcm(int a, int b) { return a * b / gcd(a, b); }
bool test(){
int a[4];
cin>>a[0]>>a[1]>>a[2]>>a[3];
int k=1<<4;
//debug(k);
forn(i,0,k){
int sum1=0,sum2=0;
forn(j,0,4){
if(1<<j&i){
sum1+=a[j];
}
else{
sum2+=a[j];
}
}
//debug2(sum1,sum2);
if(sum1==sum2){
return 1;
}
}
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t =1;
// cin>>t;
while(t--){
if(test()){
print("Yes");
}
else{
print("No");
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// #pragma GCC target("sse2,sse3,ssse3,sse4,popcnt,avx,avx2,tune=native")
// #pragma GCC optimaze("O3")
// #pragma GCC optimaze("unroll-loops")
#define int long long
typedef long long ll;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int mod = 1e9 + 7;
ll mul(ll a, ll b) {
return (a * b) % mod;
}
int pw(int a, int k) {
if (k == 0) return 1;
if (k % 2) return mul(pw(a, k - 1), a);
return pw(mul(a, a), k / 2);
}
int sm(int a, int b) {
if (a + b < mod) return a + b;
return a + b - mod;
}
int df(int a, int b) {
if (a >= b) return a - b;
return a + mod - b;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t; cin >> t;
int n; cin >> n;
int A = (n * 100 + t - 1) / t;
cout << ((A * (100 + t)) / 100) - 1 << endl;
return 0;
}
| #include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
#include <numeric>
#include <iterator>
#include <climits>
using namespace std;
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v)
{
o << "{";
for (int i = 0; i < (int)v.size(); i++)
o << (i > 0 ? ", " : "") << v[i];
o << "}";
return o;
}
int main()
{
int n;
cin >> n;
long long res = 0;
while (n > 0)
{
long long a, b;
cin >> a >> b;
long long sum = (b - a + 1) * (a + b) / 2;//(100000-1+1)*(1+1000000)/2
res += sum;
// cout << "n: " << n << " sum: " << sum << endl;
n--;
}
cout << res;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
////////////////////////////////////////////////////////////////////////////<editor-fold desc="macros">
//#define mod 9999991
long mod=9999991;
#define endl "\n"
#define long long long
#define all(v) (v).begin(),(v).end()
#define makeset(v) (v).resize(unique((v).begin(),(v).end())-(v).begin())
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define filein freopen("input.txt", "r", stdin);
#define fileout freopen("output.txt", "w", stdout);
#define getline(x) getline(cin,x)
#define makelower(s) transform(s.begin(),s.end(),s.begin(),::tolower)
bool sortby(pair<int,int> a,pair<int,int> b){
double x=(double )a.first/a.second;
double y=(double )b.first/b.second;
return x<y;}
long Pow(long x, long y) {
if(y == 0)return 1;
long res=Pow(x,y/2)%mod;
res=(res*res)%mod;
if(y&1)res=(res*x)%mod;
return res;
}
#define printcase(x) cout<<"Case "<<in++<<": "<<x<<endl
#define memset(a,b) memset(a,b,sizeof(a))
#define print1d(ary) cout<<"[";for(auto x:(ary)){cout<<x<<",";}cout<<"]"<<endl;
#define print2d(x) for(int m = 0; m <sizeof(x)/sizeof(x[0]) ; ++m){for(int l = 0; l <sizeof(x[0])/sizeof(x[0][0]) ; ++l){cout<<x[m][l]<<" ";}cout<<endl;}
#define pi acos(-1)
#define esp 1e-9
////////////////////////////////////////////////////////////////////////////</editor-fold>
long t,n,m,f=0,ans,a,b;
long tmp;
int main() {
///<editor-fold desc="ios">
IOS
#ifdef _AD_
filein //fileout
#endif
///</editor-fold>
int in=1,o;
cin>>n>>m;
vector<vector<int>> idx(n+1,{0});
for(int i = 1; i <=n ; ++i){
cin>>tmp;
idx[tmp].push_back(i);
}
for(int i = 0; i <=n ; ++i){
idx[i].push_back(n+1);
}
for(int i = 0; i <= n; ++i){
for(int j = 1; j <idx[i].size() ; ++j){
if(idx[i][j]-idx[i][j-1]-1>=m){
cout<<i<<endl;
return 0;
}
}
}
} | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// #include <boost/rational.hpp>
// using bigint = boost::multiprecision::cpp_int;
// using fraction = boost::rational<bigint>;
using namespace std;
#define pb push_back
#define eb emplace_back
#define unused [[maybe_unused]]
#define tempT template<class T>
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define debug(x) cerr << #x << ": " << x << endl
#define ans_exit(x) { cout << x << endl; exit(0); }
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (ll i = (ll)(n); i < (ll)(m); i++)
using ll unused = long long;
using ld unused = long double;
using vl unused = vector<ll>;
using vvl unused = vector<vl>;
using vvvl unused = vector<vvl>;
using lp unused = pair<ll, ll>;
using lmap unused = map<int, int>;
unused constexpr ll MOD = 998244353;
unused constexpr ll INF = 1 << 30;
unused constexpr ll LINF = 1LL << 62;
unused constexpr int DX[8] = {0, 1, 0,-1, 1, 1,-1,-1};
unused constexpr int DY[8] = {1, 0,-1, 0, 1,-1, 1,-1};
unused inline bool bit(ll b, ll i) { return b & (1LL << i); }
unused inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
unused inline ll mod(ll a, ll m = MOD) { return (a % m + m) % m; }
unused inline ll floordiv(ll a, ll b) { return a / b - (a < 0 && a % b); }
unused inline ll ceildiv(ll a, ll b) { return floordiv(a + b - 1, b); }
tempT unused inline bool in_range(T a, T x, T b) { return a <= x && x < b; }
unused inline bool in_range(ll x, ll b) { return in_range(0LL, x, b); }
tempT unused bool chmin(T &a, T b) { if(a > b) {a = b; return 1;} return 0; }
tempT unused bool chmax(T &a, T b) { if(a < b) {a = b; return 1;} return 0; }
int main() {
ll n, m;
cin >> n >> m;
if(m < 0) ans_exit(-1);
ll cnt = 0;
vector<lp> ans;
if(m != 0) {
ans.pb({++cnt, -1});
rep(i, m+1) {
ans.pb({++cnt, 0});
ans.back().second = ++cnt;
}
ans[0].second = ++cnt;
}
if(n < ans.size()) ans_exit(-1);
while(ans.size() < n) {
ans.pb({++cnt, 0});
ans.back().second = ++cnt;
}
rep(i, n) {
cout << ans[i].first << " " << ans[i].second << endl;
}
} |
#include<cstdio>
using namespace std;
int n, m;
int main() {
scanf("%d %d", &n, &m);
if ((m + 1 == n && m != 0) || m == n || m < 0) {
printf("-1\n");
return 0;
}
printf("1 1000000000\n");
for (int i = 1; i <= m; i++) {
printf("%d %d\n", i * 2, i * 2 + 1);
}
for (int i = m + 1; i < n; i++) {
printf("%d %d\n", i * 2, 1000000000 - i);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define LL_MAX LLONG_MAX
#define ub(v,x) upper_bound(v.begin(),v.end(),x)
#define lb(v,x) lower_bound(v.begin(),v.end(),x)
#define mod 1000000007
int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}
int fac[1000009];int ncr_mod(int n,int k){int ans=fac[n];ans*=modinv(fac[k]);ans%=mod;ans*=modinv(fac[n-k]);ans%=mod;return ans;}
vector<int>v_prime;void Sieve(int n){bool prime[n + 1];memset(prime,true,sizeof(prime));for (int p = 2; p*p <=n;p++){if(prime[p] ==true) {for(int i = p*p; i<= n; i += p)prime[i]=false;}}for(int p = 2;p<= n;p++)if (prime[p])v_prime.pb(p);}
vector<int>v_factor;void factor(int n){ for (int i=1; i<=sqrt(n); i++) {if (n%i == 0) {if (n/i == i) v_factor.pb(i);else { v_factor.pb(i),v_factor.pb(n/i);};} } sort(all(v_factor)); }
int power(int x, int y){int temp;if( y == 0)return 1; temp = power(x, y / 2);if (y % 2 == 0) return temp * temp;else return x * temp * temp;}
int gcd(int a, int b){if (b == 0)return a; return gcd(b, a % b);}
int log2n( int n){ return (n > 1) ? 1 + log2n(n / 2) : 0;}
void out(vector<int>&a){for(int i=0;i<a.size();i++) cout<<a[i]<<" "; cout<<endl;}
void sout(set<int>s){for(auto it=s.begin();it!=s.end();++it)cout<<*it<<" "; cout<<endl;}
void mout(map<int,int>mm){for(auto it=mm.begin();it!=mm.end();++it) cout<<it->fi<<" "<<it->se<<endl;}
#define ms(a,x) memset(a, x, sizeof(a));
#define decimal(n,k) cout<<fixed<<setprecision(k)<<n<<endl
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
// debug functions
#define deb1(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
// In binary search always report l-1
signed main()
{
run;
/*
open for mod factorial
fac[0]=1;
for(int i=1;i<1000009;i++)
{
fac[i]=mul(fac[i-1],i);
}
*/
/*
for sieve open this and use v_prime
int pp=pow(10,6)+100000;
Sieve(pp);
*/
// factor(n) && USE v_factor For calculating factors && don't forget v_factor.clear();
int t=1;
while(t--)
{
// map<int,int>mm; // it->second-->frequency
int n,i,x,y,ok=0,sum=0,ans=0,j,k,cnt=0,m,c=0,q,z;
int h[100009]={0};
cin>>x>>y>>z;
if(z%2==1)
z=1;
else
z=2;
x=pow(x,z);
y=pow(y,z);
if(x>y)
cout<<">";
else if(y>x)
cout<<"<";
else
cout<<"=";
}
} |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <queue>
#define lc u << 1
#define rc u << 1 | 1
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
LL n,s,k;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b)
{
x = 1,y = 0;
return a;
}
LL d = exgcd(b,a % b,y,x);
y -= x * (a / b);
return d;
}
void solve()
{
scanf("%lld%lld%lld",&n,&s,&k);
LL x,y;
LL d = exgcd(k,n,x,y);
n /= d;
if(s % d) puts("-1");
else
{
printf("%lld\n",(-x * (s / d) % n + n) % n);
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
#endif // LOCAL
int T = 1;
scanf("%d",&T);
while(T --)
{
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int T;
long long L, R;
long long solve(int L, int R) {
if (R < 2*L) return 0;
long long c = R - 2*L + 1;
return c * (c+1) / 2;
}
int main() {
scanf("%d", &T);
while (T --) {
scanf("%lld%lld", &L, &R);
printf("%lld\n", solve(L, R));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
#define RST(i,n) memset(i,n,sizeof i)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(),a.end()
#define X first
#define Y second
#define eb emplace_back
#ifdef cold66
#define debug(...) do{\
fprintf(stderr,"LINE %d: (%s) = ",__LINE__,#__VA_ARGS__);\
_do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
_s<<"{";
for(It _it=_ita;_it!=_itb;_it++)
{
_s<<(_it==_ita?"":",")<<*_it;
}
_s<<"}";
return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // cold66
//}
const ll MAXN=1e2+5,MAXlg=__lg(MAXN)+2;
const ll MOD=1000000007;
const ll INF=0x3f3f3f3f;
const int MAXM = 1e4+5;
vector<pii> edge;
int c[MAXN], ans[MAXM]; // 0:-> 1:<-
vector<pii> e[MAXN];
bool vis[MAXN];
void print(int x){
if (x) cout << "<-" << endl;
else cout << "->" << endl;
}
void dfs(int x){
vis[x] = true;
for (auto i:e[x]) {
pii cur = edge[i.X];
int to;
if (i.Y == 0) to = cur.Y;
else to = cur.X;
ans[i.X] = i.Y;
if (vis[to]) continue;
dfs(to);
}
}
signed main(){
IOS();
int n,m;
cin >> n >> m;
for (int i=0;i<m;++i) {
int u,v;
cin >> u >> v;
u--, v--;
edge.eb(u,v);
ans[i] = -1;
}
for (int i=0;i<n;++i) cin >> c[i];
for (int i=0;i<m;++i) {
int u = edge[i].X, v = edge[i].Y;
if (c[u] == c[v]) {
e[u].eb(i,0);
e[v].eb(i,1);
continue;
}
if (c[u] > c[v]) ans[i] = 0;
else ans[i] = 1;
}
for (int i=0;i<n;++i) {
if (vis[i]) continue;
dfs(i);
}
for (int i=0;i<m;++i) print(ans[i]);
}
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
template<typename T>void ckmn(T&a,T b){a=min(a,b);}
template<typename T>void ckmx(T&a,T b){a=max(a,b);}
void rd(int&x){scanf("%i",&x);}
void rd(ll&x){scanf("%lld",&x);}
void rd(char*x){scanf("%s",x);}
void rd(ldb&x){scanf("%lf",&x);}
void rd(string&x){scanf("%s",&x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);}
template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);}
template<typename T>void rd(){T x;rd(x);return x;}
int ri(){int x;rd(x);return x;}
template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;}
template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);}
template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);}
template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);}
void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}}
template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}}
const int mod=998244353;
int add(int a,int b){a+=b;return a>=mod?a-mod:a;}
void ckadd(int&a,int b){a=add(a,b);}
int sub(int a,int b){a-=b;return a<0?a+mod:a;}
void cksub(int&a,int b){a=sub(a,b);}
int mul(int a,int b){return (ll)a*b%mod;}
void ckmul(int&a,int b){a=mul(a,b);}
int powmod(int x,int k){int ans=1;for(;k;k>>=1,ckmul(x,x))if(k&1)ckmul(ans,x);return ans;}
int inv(int x){return powmod(x,mod-2);}
const int N=1000050;
int F[N],I[N];
int binom(int n, int k){ return mul(F[n],mul(I[k],I[n-k]));}
void calc()
{
F[0]=1;
for(int i=1;i<N;i++) F[i]=mul(F[i-1],i);
I[N-1]=inv(F[N-1]);
for(int i=N-2;~i;i--) I[i]=mul(I[i+1],i+1);
}
const int M=5050;
const int L=14;
int dp[L][M];
int main(){
calc();
int n,m;
rd(n,m);
dp[0][0]=1;
for(int i=1;i<L;i++){
for(int j=0;j<=m;j++){
dp[i][j]=0;
for(int take=0;(take<<i)<=j&&take*2<=n;take++){
ckadd(dp[i][j],mul(dp[i-1][j-(take<<i)],binom(n,take*2)));
}
}
}
printf("%i\n",dp[L-1][m]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, M;
cin >> N >> M;
vector<vector<pair<int, int>>> E(N);
for (int i = 0; i < M; i++){
int A, B, C;
cin >> A >> B >> C;
A--;
B--;
E[A].push_back(make_pair(C, B));
}
for (int i = 0; i < N; i++){
vector<int> d(N, -1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push(make_pair(0, i));
while (!pq.empty()){
int dd = pq.top().first;
int v = pq.top().second;
pq.pop();
if (d[v] == -1){
if (dd > 0){
d[v] = dd;
}
for (auto P : E[v]){
int w = P.second;
if (d[w] == -1){
pq.push(make_pair(dd + P.first, w));
}
}
}
}
cout << d[i] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
#define MP make_pair
#define MT make_tuple
typedef int64_t ll;
#define PA pair<ll,ll>
#define TU tuple<ll,ll,ll>
#define vi vector<ll>
#define vii vector<vector<ll> >
#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define rep2(i,m,n) for(ll (i)=(m); (i)<(ll)(n); (i)++)
const ll INF = 1ll<<30;
const ll INF2 = 1ll<<32;
ll n,m;
vector<vector<PA>> graph;
vector<vector<PA>> graph_r;
vi self;
vi dist;
vi dist_r;
void dijkstra(vector<vector<PA>> &graph, ll sta, vi &dist) {
priority_queue<PA,vector<PA>,greater<PA>> pq;
pq.push(MP(0,sta));
dist.at(sta) = 0;
while(!pq.empty()) {
PA state = pq.top();
pq.pop();
if(dist.at(state.second) < state.first) continue;
for(auto nv: graph.at(state.second)) {
if(dist.at(nv.first) > dist.at(state.second) + nv.second) {
dist.at(nv.first) = dist.at(state.second) + nv.second;
pq.push(MP(dist.at(state.second) + nv.second,nv.first));
}
}
}
}
int main() {
cin >>n>>m;
graph.assign(n,vector<PA>(0));
graph_r.assign(n,vector<PA>(0));
self.assign(n,INF);
ll a,b,c;
rep(i,m) {
cin >>a>>b>>c;
if(a==b) self.at(a-1) = min(self.at(a-1),c);
else {
graph.at(a-1).push_back(MP(b-1,c));
graph_r.at(b-1).push_back(MP(a-1,c));
}
}
rep(sta,n) {
dist.assign(n,INF);
dist_r.assign(n,INF);
dijkstra(graph, sta, dist);
dijkstra(graph_r, sta, dist_r);
vi dist_all(n);
rep(i,n) dist_all.at(i) = dist.at(i) + dist_r.at(i);
ll mini = INF2;
rep(i,n) {
if(i!= sta) mini = min(mini,dist_all.at(i));
}
mini = min(self.at(sta),mini);
if(mini<INF) cout << mini << endl;
else cout << -1 << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int n, m, fa[N];
int main() {
int x;
cin >> x;
for (int i = x + 1; ; i++) {
if (i % 100 == 0) {
cout << i - x << endl;
return 0;
}
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define INF ((1LL<<62)-(1LL<<31))
typedef long long ll;
int main()
{
ll n;
cin >> n;
string s=to_string(n);
int x=s.size();
int ans=0;
if(x<=2) ans=n/11;
else if(x==3) ans=9;
else if(x<=4) ans=n/101;
else if(x==5) ans=99;
else if(x<=6) ans=n/1001;
else if(x==7) ans=999;
else if(x<=8) ans=n/10001;
else if(x==9) ans=9999;
else if(x<=10) ans=n/100001;
else if(x==11) ans=99999;
else if(x<=12) ans=n/1000001;
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n;
vector<ll>vec;
int main()
{
string s,s1="";
cin>>s;
if(s.size()==1)
{
cout<<0<<endl;
exit(0);
}
ll n=s.size(),ans=0;
for(ll i=0; i<=n/2; i++)
{
s1+=s[i];
}
ll val=stoll(s1),tar=stoll(s);
for(ll i=1; i<=val; i++)
{
string temp="";
temp=to_string(i);
temp+=temp;
ll now=stoll(temp);
if(now<=tar)
{
ans++;
}
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// DEBUG
#ifdef _DEBUG
#define debug(x) cout << #x << ": " << x << endl
#else
#define debug(x)
#endif
// iter
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i <= (int)(b); i++)
#define FORD(i, a, b) for (int i = a; i >= (int)(b); i--)
#define FORA(i, I) for (const auto& i : I)
// vec
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ((int)(x.size()))
// 定数
// 2.147483647×10^{9}:32bit整数のinf
#define INF32 2147483647
// 9.223372036854775807×10^{18}:64bit整数のinf
#define INF64 9223372036854775807
// 問題による
#define MOD 1000000007
int main() {
// 小数の桁数の出力指定
// cout << fixed << setprecision(10);
// 入力の高速化用のコード
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
// N=3桁のとき 2桁のは全部 9こ
// 11,22,33,..,99
// 1010,1111,1212,...9999
// N=5桁のとき 2桁4桁
// N=6桁のとき 2桁4桁と6桁の途中まで
// 100100,101101,...,555555,...9999999
int ans = 0;
string nst = to_string(N);
int len = nst.length();
for (int i = 1; i <= len / 2; i++) {
ll keta = pow(10, i);
// debug(i); debug(keta);
for (int j = keta / 10; j < keta; j++) {
ll num = j * keta + j;
// debug(num);
if (num <= N) {
ans++;
}
}
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ar array
/*
*/
const int N = (1<<20);
int n, m;
int adj[20][20], dp[N];
bool ok[N];
int INF = 1e9;
void solve(){
cin>>n>>m;
for(int i = 0; i < m; i++){
int a, b; cin>>a>>b; a--, b--;
adj[a][b] = 1;
}
for(int mask = 0; mask < 1<<n; mask++){
ok[mask] = true;
for(int i = 0; i < n; i++){
if(mask >> i & 1){
for(int j = i+1; j < n; j++){
if(mask >> j & 1) ok[mask] &= adj[i][j];
}
}
}
}
for(int mask = 0; mask < 1<<n; mask++){
dp[mask] = ok[mask]? 1 : INF;
for(int sub = (mask-1)&mask; sub; sub = (sub-1)&mask){
dp[mask] = min(dp[mask], dp[sub] + dp[ mask^sub ]);
}
}
cout << dp[ (1<<n)-1 ] << endl;
}
int main()
{
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#define debug(x) print(x)
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }
int main() {
init();
int n, m; cin >> n >> m;
auto e = vec<bool>(n, n);
FOR(i, 0, m) {
int a, b; cin >> a >> b;
--a, --b;
e[a][b] = e[b][a] = true;
}
vector<bool> ok(1 << n, true);
FOR(bit, 0, 1 << n)FOR(i, 0, n)if (bit & (1 << i))FOR(j, i + 1, n) {
if (bit & (1 << j))ok[bit] = ok[bit] && e[i][j];
}
auto dp = defvec<int>(INF, 1 << n);
FOR(bit, 0, 1 << n) {
if (ok[bit])dp[bit] = 1;
for (int sub = bit; sub >= 0; --sub) {
sub &= bit;
int bus = bit ^ sub;
if (ok[sub] && ok[bus])chmin(dp[bit], 2);
chmin(dp[bit], dp[sub] + dp[bus]);
}
}
print(dp[(1 << n) - 1]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, count=0;
cin >> n;
map<long long, int> m;
for(int i=2; i<=sqrt(n); i++){
long long a;
a=i;
a*=i;
while(a<=n){
if(!(m.count(a))){
count++;
m[a]=0;
}
a*=i;
}
}
cout << n-count;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{ int t=1;
//cin>>t;
while(t--)
{
string s,p;
cin>>s;
for(int i=0;i<s.size();i++)
{
if(s[i]!='.')
{
p+=s[i];
}
else
break;
}
cout<<p<<"\n";
}
return 0;
} |
/* -*- coding: utf-8 -*-
*
* f.cc:
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 200000;
/* typedef */
typedef map<int,int> mii;
struct UFT {
int links[MAX_N], ranks[MAX_N], sizes[MAX_N];
UFT() {}
void init(int n) {
for (int i = 0; i < n; i++) links[i] = i, ranks[i] = sizes[i] = 1;
}
int root(int i) {
int i0 = i;
while (links[i0] != i0) i0 = links[i0];
return (links[i] = i0);
}
int rank(int i) { return ranks[root(i)]; }
int size(int i) { return sizes[root(i)]; }
bool same(int i, int j) { return root(i) == root(j); }
int merge(int i0, int i1) {
int r0 = root(i0), r1 = root(i1), mr;
if (r0 == r1) return r0;
if (ranks[r0] == ranks[r1]) {
links[r1] = r0;
sizes[r0] += sizes[r1];
ranks[r0]++;
mr = r0;
}
else if (ranks[r0] > ranks[r1]) {
links[r1] = r0;
sizes[r0] += sizes[r1];
mr = r0;
}
else {
links[r0] = r1;
sizes[r1] += sizes[r0];
mr = r1;
}
return mr;
}
};
/* global variables */
mii cvs[MAX_N];
UFT uft;
/* subroutines */
/* main */
int main() {
int n, qn;
scanf("%d%d", &n, &qn);
for (int i = 0; i < n; i++) {
int ci;
scanf("%d", &ci);
ci--;
cvs[i][ci] = 1;
}
uft.init(n);
while (qn--) {
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
x--, y--;
if (op == 1) {
if (! uft.same(x, y)) {
int rx = uft.root(x), ry = uft.root(y);
int u = uft.merge(rx, ry), v = (u == rx) ? ry : rx;
mii &cvu = cvs[u], &cvv = cvs[v];
if (cvu.size() < cvv.size()) swap(cvu, cvv);
for (mii::iterator mit = cvv.begin(); mit != cvv.end(); mit++)
cvu[mit->first] += mit->second;
}
}
else {
mii &cvr = cvs[uft.root(x)];
mii::iterator mit = cvr.find(y);
if (mit == cvr.end() || mit->first != y) puts("0");
else printf("%d\n", mit->second);
}
}
return 0;
}
| #include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define ndl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
//using namespace __gnu_pbds;
//template<typename T> using indexed_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());//uniform_int_distribution<int> (l, r)
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<int, ull> piu;
typedef vector<vector<int>> matrix;
const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 1e5 + 123;
const int M = 1e6 + 123;
const double pi = 3.14159265359;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
struct edge{
int u, v, c, d, opt;
}e[N];
vector<int> g[N];
ll dis[N];
void solve(){
int n, m;
cin >> n >> m;
for (int i = 1, u, v, c, d; i <= m; i++){
cin >> u >> v >> c >> d;
g[u].pb(i);
g[v].pb(i);
int l = 0;
if (d){
l = sqrt(d)-1;
while (l+(d/(l+1)) > l+1+(d/(l+2))){
l++;
}
}
e[i] = {u, v, c, d, l};
//cout << l << ndl;
}
memset(dis, 0x3f, sizeof dis);
dis[1] = 0;
priority_queue<pli, vector<pli>, greater<pli>> q;
q.push({0, 1});
while (!q.empty()){
ll d;
int v;
tie(d, v) = q.top();
q.pop();
if (dis[v] != d) continue;
for (int i : g[v]){
ll x = max(e[i].opt+0ll, d);
int u = e[i].v+e[i].u - v;
if (dis[u] > x + e[i].c + e[i].d/(x+1)){
dis[u] = x + e[i].c + e[i].d/(x+1);
q.push({dis[u], u});
}
}
}
if (dis[n] > INF){
cout << -1;
}
else{
cout << dis[n];
}
}
int main(){
#ifdef me
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++){
//cout << "Case #" << i << ": ";
solve();
}
#ifdef KAZAKH
// cout << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl;
#endif
return 0;
} |
// SmartCoder
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define minei(x) min_element(x.begin(), x.end()) - (x).begin()
#define maxei(x) max_element(x.begin(), x.end()) - (x).begin()
#define LSOne(S) (S & (-S))
#define uns(v) sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end())
#define acusum(x) accumulate(x.begin(), x.end(), 0)
#define acumul(x) accumulate(x.begin(), x.end(), 1, multiplies<int>());
#define bits(x) __builtin_popcount(x)
#define oo INT_MAX
#define inf 1000000000
#define MAXN 100000007
#define MOD 1000000007
const double pi = acos(-1.0);
const double eps = 1e-11;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
void pv(vector<int> v) {
cout << v[0];
for (int i = 1; i < sz(v); i++) {
cout << " " << v[i];
}
cout << endl;
}
int main() {
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<ll> v(n);
vector<ll> sum(n + 1, 0);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
sort(all(v));
sum[0] = v[0];
for (int i = 1; i < n; i++) {
sum[i] = v[i] + sum[i - 1];
}
ll res = 0;
for (int i = 1; i < n; i++) {
res += ((v[i] * i) - sum[i - 1]);
}
cout << res << "\n";
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll funcy(ll a[], ll n)
{
ll sum = 0;
ll i;
for(i=0;i<n;i++)
{
sum += (a[i]*(i) - a[i]*(n-i-1));
}
return sum;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll i,n;cin>>n;ll v[n+1];
for(i=0;i<n;i++)cin>>v[i];
sort(&v[0],&v[n]);
cout<<funcy(v,n);
return 0;
}
|
/*
author : Aryan Agarwal, IIT KGP
created : 20-February-2021 17:32:16 IST
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mxn = 1e5;
const long long INF = 2e18;
const int32_t M = 1000000007; /*more than 1e9 */ /*7 + 1e9*/
// const int32_t M = 998244353; /*less than 1e9 */ /*1 + 7*17*(1<<23) */
const long double pie = acos(-1);
#define X first
#define Y second
#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define F(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)
#define dbg(...) __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...);
}
void solve_LOG()
{
string s;
cin>>s;
s='#'+s;
F(i,1,sz(s)-1)
{
if(i%2)
{
if(s[i]<'a' || s[i]>'z')
{
cout<<"No";
return;
}
}
if(i%2==0)
{
if(s[i]<'A' || s[i]>'Z')
{
cout<<"No";
return;
}
}
}
cout<<"Yes";
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
#ifdef ARYAN_SIEVE
// defualt mxn_sieve = 1e5
sieve();
#endif
#ifdef ARYAN_SEG_SIEVE
// default [L,R] = [1,1e5]
segmented_sieve();
#endif
#ifdef ARYAN_FACT
// default mxn_fact = 1e5
fact_init();
#endif
// cout<<fixed<<setprecision(10);
int _t=1;
// cin>>_t;
for (int i=1;i<=_t;i++)
{
// cout<<"Case #"<<i<<": ";
solve_LOG();
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
bool b=true;
char a[10000];
cin>>a;
for(int i=0;i<strlen(a);i++)
{
if(i%2==0)
{
if(a[i]<97||a[i]>122)
{
b=false;
break;
}
}
else
{
if(a[i]<65||a[i]>90)
{
b=false;
break;
}
}
}
if(b==true)
{
cout<<"Yes";
}
else
{
cout<<"No";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define mod2 998244353
#define ll long long
#define gi(n) long long n;cin>>n
#define ga(a,n) long long a[n];for(ll i=0;i<n;i++)cin>>a[i]
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define TEST(t) ll t; cin>>t; while(t--)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)
#define flo(i,a,b) for(ll i=a;i<b;i++)
#define floe(i,a,b) for(ll i=a;i<=b;i++)
#define newline cout<<"\n"
#define gis(s) string s;cin>>s
#define p(a) cout<<a
#define pn(a) cout<<a<<endl
char a[1000][1000];
typedef vector<ll> vl;
typedef pair<ll,ll> pll;
typedef map<ll,ll> mll;
ll gcd(ll x, ll y)
{
if(x==0) return y;
return gcd( y%x ,x);
}
ll powM(ll x, ll y, ll m)
{
ll ans=1, r=1;
x=x%m;
while(r>0 && r<=y)
{
if(r & y)
{
ans=ans*x;
ans=ans%m;
}
r=r<<1;
x=x*x;
x=x%m;
}
return ans;
}
ll countbits(ll n)
{
ll cot=0;
while(n)
{
cot++;
n >>= 1;
}
return cot;
}
ll comb2(ll n)
{
return n*(n-1)/2;
}
ll sum_of_digits(ll n){
ll b=0;
while(n!=0){
b=b+(n%10);
n=n/10;
}
return b;
}
ll no_of_digits(ll n){
ll b=0;
while(n!=0){
b=b+1;
n=n/10;
}
return b;
}
bool check(int array[], int n)
{
bool flag = 0;
for(int i = 0; i < n - 1; i++)
{
if(array[i] != array[i + 1])
flag = 1;
}
return flag;
}
long long int factorial(long long int n){
ll ans=1;
flo(i,1,n+1){
ans*=i;
}
return ans;
}
bool spure(int i,int j,int h,int m){
if(a[i][j]=='.'){
return false;
}
else{
if(h==1){
if(a[i][j]=='*'){
return true;
}
return false;
}
else{
if(j-1>=0 && j+1<m){
bool pk=spure(i+1,j+1,h-1,m) && spure(i+1,j,h-1,m) && spure(i+1,j-1,h-1,m);
return pk;
}
else{
return false;
}
}
}
}
int main(){
fastio;
gi(a);
gi(b);
gi(c);
gi(d);
cout<<min(a,min(b,min(c,d)));
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
template<class T>
bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template<class T>
bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<int, int>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
const ll MOD = 1e9 + 7;
const int INF = 1001001001;
const double PI = 3.14159265358979323846;
const int lim = 1e5+5;
int n;
double A[lim];
double func(double x) {
double res = 0;
rep(i, n) {
res += x + A[i] - min(A[i], x*2);
}
return res;
}
void solve() {
cin >> n;
rep(i, n) cin >> A[i];
double low = 0;
double high = 1e10;
int cnt = 500;
while (cnt--) {
double c1 = (low*2 + high) / 3;
double c2 = (low + high*2) / 3;
if (func(c1) > func(c2)) low = c1;
else high = c2;
}
double ans = func(low) / n;
cout << setprecision(20) << fixed << ans << endl;
}
int main() {
solve();
return 0;
}
|
//ΔAGC053B
#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 444444;
int n,a[N];
priority_queue<int,vector<int>,greater<int> > Q;
int main()
{
int i,x,y;
LL s=0;
scanf("%d",&n);
for(i=1;i<=n*2;i=i+1)
scanf("%d",a+i);
for(i=1;i<=n;i=i+1){
x=a[n+i],y=a[n-i+1];
if(x<y)
swap(x,y);
Q.push(x);
if(Q.top()<y){
Q.pop();
Q.push(y);
}
}
while(!Q.empty()){
s+=Q.top();
Q.pop();
}
cout<<s;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
bool check(string S, string pass) {
vector<bool> b(10, false);
b.at(pass.at(0) - '0') = true;
b.at(pass.at(1) - '0') = true;
b.at(pass.at(2) - '0') = true;
b.at(pass.at(3) - '0') = true;
for (int i = 0; i < 10; i++) {
if (S.at(i) == 'o' && b.at(i) == false)
return false;
if (S.at(i) == 'x' && b.at(i) == true)
return false;
}
return true;
}
int main() {
int ans = 0;
string S;
cin >> S;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 10; k++) {
for (int l = 0; l < 10; l++) {
string pass;
pass += i + '0';
pass += j + '0';
pass += k + '0';
pass += l + '0';
if (check(S, pass))
ans++;
}
}
}
}
cout << ans << endl;
} |
//
// Created by David on 2020/10/03 0003.
//
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
string str;
int n;
struct complex {
int real, imagine;
explicit complex(int re = 0, int im = 0) : real(re), imagine(im) {}
friend complex operator+(complex a, complex b) {
complex c;
c.real = a.real + b.real;
c.imagine = a.imagine + b.imagine;
return c;
}
friend complex operator-(complex a, complex b) {
complex c;
c.real = a.real - b.real;
c.imagine = a.imagine - b.imagine;
return c;
}
friend bool operator==(complex a, int b) {
return a.imagine == 0 && a.real == b;
}
};
complex a[5004], s[5004];
int ans;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> str;
for (int i = 1; i <= n; i++) {
char ch = str[i - 1];
if (ch == 'A') a[i] = complex(1, 0);
if (ch == 'T') a[i] = complex(-1, 0);
if (ch == 'C') a[i] = complex(0, 1);
if (ch == 'G') a[i] = complex(0, -1);
s[i] = s[i - 1] + a[i];
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j < i; j++) {
if (s[i] - s[j] == 0 || s[j] - s[i] == 0) ans++;
}
}
cout << ans;
return 0;
}
| /*
_/_/_/_/ _/_/_/_/_/ _/_/_/
_/ _/ _/ _/ _/
_/ _/ _/ _/ _/
_/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/
_/ _/ _/ _/ _/ _/_/
_/_/_/_/ _/_/ _/_/_/_/_/
_/_/_/_/ _/ _/ _/ _/
_/ _/ _/ _/ _/_/ _/_/
_/ _/ _/_/ _/ _/_/ _/
_/ _/ _/ _/ _/ _/
_/ _/ _/_/ _/ _/
_/ _/ _/ _/ _/ _/
_/_/_/_/ _/ _/ _/ _/
_/_/_/_/_/ _/_/_/_/_/ _/_/_/_/_/
_/ _/ _/
_/ _/ _/
_/ _/ _/_/_/_/
_/ _/ _/
_/ _/ _/
_/ _/_/_/_/_/ _/_/_/_/_/
_/_/_/_/_/ _/_/_/_/_/ _/_/_/_/_/
_/ _/ _/
_/ _/ _/
_/ _/ _/_/_/_/
_/ _/ _/
_/ _/ _/
_/ _/_/_/_/_/ _/_/_/_/_/
*/
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <limits>
#include <numeric>
#define LOG(FMT...) fprintf(stderr, FMT)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &x : v)
is >> x;
return is;
}
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
if (!v.empty()) {
os << v.front();
for (int i = 1; i < v.size(); ++i)
os << ' ' << v[i];
}
return os;
}
const int P = 998244353;
int norm(int x) { return x >= P ? (x - P) : x; }
void add(int &x, int y) { if ((x += y) >= P) x -= P; }
void sub(int &x, int y) { if ((x -= y) < 0) x += P; }
void exGcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1;
y = 0;
return;
}
exGcd(b, a % b, y, x);
y -= a / b * x;
}
int inv(int a) {
int x, y;
exGcd(a, P, x, y);
return norm(x + P);
}
const int N = 3010, L = 15;
int A[L], B[L];
int f[L][N];
int ans[N], q[N];
int main() {
#ifdef ELEGIA
freopen("test.in", "r", stdin);
int nol_cl = clock();
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
for (int i = 1; i != L; ++i) {
B[i] = (1 << i) - 1;
A[i] = A[i - 1] + B[i];
}
f[0][0] = 1;
for (int i = 0; i <= n; ++i) {
for (int j = 1; A[j] <= i; ++j)
sub(f[0][i], f[j][i - A[j]]);
int j;
for (j = 1; B[j] <= i; ++j)
f[j][i] = norm(P - f[j - 1][i] + f[j][i - B[j]]);
for (; B[j] <= n; ++j)
f[j][i] = norm(P - f[j - 1][i]);
}
{
int C = 0;
for (int i = 0; C <= n; ++i, C = A[i]) {
if (i)
for (int j = B[i]; j <= n; ++j) add(f[0][j], f[0][j - B[i]]);
memset(q, 0, sizeof(q));
if (i & 1)
for (int j = 0; j <= n - C; ++j) sub(q[j + C], f[0][j]);
else for (int j = 0; j <= n - C; ++j) add(q[j + C], f[0][j]);
for (int j = 1; j <= n >> i; ++j)
add(ans[j], q[n - j * (1 << i)]);
}
}
cout << ans[k] << '\n';
#ifdef ELEGIA
LOG("Time: %dms\n", int ((clock()
-nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = mod * mod;
const int INF_N = 1e+9;
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 rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);
//typedef vector<vector<ll>> mat;
typedef vector<int> vec;
//繰り返し二乗法
ll mod_pow(ll a, ll n, ll m) {
ll res = 1;
while (n) {
if (n & 1)res = res * a%m;
a = a * a%m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n%mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint &a, modint b) { a.n = ((ll)a.n*b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, int n) {
if (n == 0)return modint(1);
modint res = (a*a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
//逆元(Eucledean algorithm)
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p%a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
using mP = pair<modint, modint>;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
void solve() {
char a, b; cin >> a >> b;
if(a == 'Y') cout << char(b-32) << endl;
else cout << b << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//init_f();
//init();
//int t; cin >> t; rep(i, t)solve();
solve();
// stop
return 0;
}
| #include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
using namespace std;
long long int tab[100006];
long long int tab2[100006];
int main(int argc, char** argv)
{
long long int n,m,a;
int sum, k,dif;
sum = 0;
multiset <pair<long long int,int> > ppl;
multiset <pair<long long int, int> > :: iterator itr;
cin>>k>>n>>m;
for(int i=0; i<k; i++){
cin>>tab[i];
long long int np = (tab[i]*m/n);
tab2[i] = np;
sum += np;
pair<long long int , int> x = make_pair(abs(tab[i]*m-(np+1)*n), i);
ppl.insert(x);
}
dif = m-sum;
for(int i=0; i<dif; i++){
pair<long long int,int> tmp = *(ppl.begin());
ppl.erase(ppl.begin());
tab2[tmp.second]++;
int nt = tab2[tmp.second];
pair<long long, int> x = make_pair(abs(tab[tmp.second]*m - (tab2[tmp.second]+1)*n), tmp.second);
ppl.insert(x);
}
for(int i=0; i<k; i++){
cout<<tab2[i]<<" ";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010;
const int MOD = 1e9 + 7;
int n;
ll a[N], fib[N];
inline ll get (int x) {
return x < 0 ? 1 : fib[x + 2];
}
int main() {
fib[1] = 1;
for (int i = 2; i < N; ++i) {
fib[i] = fib[i - 1] + fib[i - 2];
if (fib[i] >= MOD) fib[i] -= MOD;
}
cin >> n;
for (int i = 1; i <= n; ++i) {
scanf("%lld", a + i);
}
ll ans = get(n - 1) * a[1] % MOD;
for (int i = 2; i <= n; ++i) {
ll cur = get(i - 2) * get(n - i) % MOD * a[i] % MOD;
ans += cur;
cur = get(i - 3) * get(n - i - 1) % MOD * (MOD - a[i]) % MOD;
ans += cur;
}
ans %= MOD;
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
int main(){
ll t,n;
cin >> t >> n;
ll nos =0; // nowStart
vector<ll> j;
ll jOfSet = 0;
while(1){
ll jum = (100-nos + t -1) / t; // jump
ll nes = (nos + t* jum) % 100; // nextStart
j.push_back(jum);
jOfSet += jum;
if(nes == 0) break;
nos = nes;
}
ll fracJ = 0;
// rep(i, n%t) fracJ += j[i];
int lcnt = n % (int)j.size();
for(int i=0; i < lcnt; i++) {
fracJ += j[i];
}
ll nA = (ll)jOfSet * (n/(int)j.size()) + fracJ;
ll ans = ((100+t)*nA) / 100 -1;
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int M = 10000;
ll cnt(ll X, ll Y, ll R, ll lim) {
ll a = 0;
ll b = M;
ll res = 0;
auto fn = [&](int t, int i) {
return (t - X) * (t - X) + (i - Y) * (i - Y) <= R * R;
};
for (ll i = (Y + R) - (Y + R) % M; i >= lim; i -= M) {
while (fn(b, i)) {
b += M;
}
while (fn(a, i)) {
a -= M;
}
res += (b - a - M) / M;
}
return res;
}
int main() {
ld x, y, r;
cin >> x >> y >> r;
ll ans = 0;
ll X = round(x * M);
ll Y = round(y * M);
ll R = round(r * M);
X %= M;
Y %= M;
ans += cnt(X, Y, R, M);
ans += cnt(X, -Y, R, 0);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using int32 = int;
using int64 = long long;
using namespace std;
class Solution
{
#define int int64
#define sfor(i, n) for (int i = 1; i <= n; ++i)
#define tfor(i, n) for (int i = 0; i < n; ++i)
int x, y, r, res = 0;
void SHURU()
{
double a, b, c;
cin >> a >> b >> c;
x = round(a * 10000);
y = round(b * 10000);
r = round(c * 10000);
}
void SHUCHU()
{
cout << res << endl;
}
bool inCircle(int dx, int dy)
{
return dx * dx + dy * dy <= r * r;
}
void CHULI()
{
x %= 10000;
y %= 10000;
int l = 0, r = 1;
for (int i = 1000100000; i >= 0; i -= 10000)
{
while (inCircle(x - l * 10000, y + i))
--l;
while (inCircle(x - r * 10000, y + i))
++r;
res += r - l - 1;
}
l = 0;
r = 1;
for (int i = 1000100000; i >= 10000; i -= 10000)
{
while (inCircle(x - l * 10000, y - i))
--l;
while (inCircle(x - r * 10000, y - i))
++r;
res += r - l - 1;
}
}
public:
Solution()
{
SHURU();
CHULI();
SHUCHU();
}
#undef int
#undef tfor
#undef sfor
};
int32 main()
{
#ifdef LILY
while (1)
{
#endif
Solution();
#ifdef LILY
}
#endif
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)6e18
#define pii pair<ll,ll>
#define mod 998244353
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fb(i,a,b) for(ll i=a;i>b;i--)
#define ff first
#define ss second
#define srt(v) if(!v.empty())sort(v.begin(),v.end())
#define rev(v) if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
ll res=1;
while(b!=0)
{
if(b&1)
{
res=(res*a)%mod;
}
a=(a*a)%mod;
b/=2;
}
return res;
}
ll inv_mod(ll x){
return pow_mod(x,mod-2);
}
const ll N=(ll)3e5;
ll inv[N];
ll fac[N];
void init(){
fac[0]=1;
for(ll i=1;i<N;i++)
fac[i]=(i*fac[i-1])%mod;
for(ll i=0;i<N;i++)
inv[i]=inv_mod(fac[i]);
}
ll ncr(ll n,ll r){
if(n<r)
return 0;
if(n==r||r==0)
return 1LL;
ll x=fac[n];
ll y=inv[n-r];
ll z=inv[r];
return ((x*y)%mod*z)%mod;
}
bool test_cases=0;
bool is(string &s){
for(ll i=0,j=(ll)s.size()-1;i<j;i++,j--){
if(s[i]!=s[j])
return false;
}
return true;
}
void solve()
{
ll rad,x,y;
cin>>rad>>x>>y;
ll ans=0;
ll dis_sqr=x*x+y*y;
// k*r<=sqrt()
//(k*r)^2<=
ll k=-1;
ll l=0;
ll r=(ll)6e5;
while(l<=r){
ll mid=(l+r)/2;
if(mid*rad>1000000000)
{
r=mid-1;
continue;
}
if((mid*rad)*(mid*rad)<=dis_sqr){
k=mid;
l=mid+1;
}
else
r=mid-1;
}
if(k*rad*k*rad==dis_sqr)
ans=k;
else
{
ans=k+2;
if(k>0)
ans--;
}
cout<<ans<<endl;
}
int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// init(); //factorial calculations
//Start from Here.
ll t;
t=1;
if(test_cases)
cin>>t;
while(t--)
solve();
//Good Bye!
return 0;
} | #include<bits/stdc++.h>
using namespace std;
inline int read(){
int res=0;
char c;
bool zf=0;
while(((c=getchar())<'0'||c>'9')&&c!= '-');
if(c=='-')zf=1;
else res=c-'0';
while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0';
if(zf)return -res;
return res;
}
signed main(){
double a=read(),b=read(),c=read(),d=read();
printf("%.10lf\n",(c+a*d/b)/(1+d/b));
return 0;
} |
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF ((1<<30)-1)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
class BIT {
int lg;
std::vector<int> bit;
public:
BIT(int size) :bit(size + 1, 0) {
lg = size;
while (lg & lg - 1)lg &= lg - 1;
}
void add(int i, int x) {
i++;
while (i < (int)bit.size()) {
bit[i] ^= x;
i += i & -i;
}
}
int sum(int a)const {
a++;
int res = 0;
while (0 < a) {
res ^= bit[a];
a -= a & -a;
}
return res;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
BIT bit(n);
rep(i, n) {
int a;
cin >> a;
bit.add(i, a);
}
rep(_, q) {
int t, x, y;
cin >> t >> x >> y;
if (t==1) {
bit.add(x - 1, y);
}
else {
int ans = bit.sum(y - 1);
if (1 < x)ans ^= bit.sum(x - 2);
cout << ans << endl;
}
}
return 0;
}
| #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>
#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
using namespace std;
int main() {
int N;
cin >> N;
vector<int> used(2 * N);
auto out = []() {
cout << "No" << endl;
exit(0);
};
REP(i, N) {
int A, B;
cin >> A >> B;
if (A != -1 && B != -1 && A >= B) {
out();
}
if (A != -1) {
if (used[A - 1] != 0) out();
used[A - 1] = i + 1;
}
if (B != -1) {
if (used[B - 1] != 0) out();
used[B - 1] = -(i + 1);
}
}
vector<bool> dp(N + 1);
dp[0] = 1;
FOR(i, 1, N + 1) {
REP(j, i)
if (dp[j]) {
bool ng = 0;
int width = i - j;
REP(k, width) {
int a = used[2 * j + k], b = used[2 * j + k + width];
ng |= (a < 0);
ng |= (b > 0);
ng |= (a > 0 && b < 0 && b != -a);
}
if (!ng) {
dp[i] = 1;
break;
}
}
}
cout << (dp[N] ? "Yes" : "No") << endl;
return 0;
} | #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;
void array_show(int *array,int array_n,char middle=' '){
for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
if(vec_n==-1)vec_n=vec_s.size();
for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
if(vec_n==-1)vec_n=vec_s.size();
for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
int t[220][2];
int p[220][2];
bool solve(){
int n,m;
int i,j,k;
int a,b,c;
cin>>n;
memset(t,-1,sizeof(t));
set<P> s1;
for(i=0;i<n;i++){
cin>>a>>b;
if(a!=-1){
a--;
if(t[a][0]!=-1)return false;
t[a][0]=i,t[a][1]=0;
}
if(b!=-1){
b--;
if(t[b][0]!=-1)return false;
t[b][0]=i,t[b][1]=1;
}
p[i][0]=a,p[i][1]=b;
if(a!=-1 && b!=-1 && a>=b)return false;
}
vector<char> good(2*n+10,0);
good[2*n]=1;
for(i=2*n-1;i>=0;i--){
for(j=1;i+2*j<=2*n;j++){
if(!good[i+2*j])continue;
for(k=0;k<j;k++){
a=t[i+k][0];
if(a==-1)a=t[i+j+k][0];
else if(t[i+j+k][0]!=-1 && a!=t[i+j+k][0])break;
if(a==-1)continue;
if(p[a][0]!=-1 && p[a][0]!=i+k)break;
if(p[a][1]!=-1 && p[a][1]!=i+j+k)break;
}
if(k==j){
good[i]=1;
break;
}
}
}
return good[0];
}
int main(){
if(solve())cout<<"Yes"<<endl;
else cout<<"No"<<endl;
} |
#line 1 "main_b.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
/* template start */
using i64 = std::int_fast64_t;
using u64 = std::uint_fast64_t;
#define rep(i, a, b) for (i64 i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()
#ifdef LOCAL
#define debug(...) \
std::cerr << "LINE: " << __LINE__ << " [" << #__VA_ARGS__ << "]:", \
debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
std::cerr << " " << h;
if (sizeof...(t) > 0) std::cout << " :";
debug_out(t...);
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
return os << pa.first << " " << pa.second;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
for (std::size_t i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
return a > b && (a = b, true);
}
template <typename Num>
constexpr Num mypow(Num a, u64 b, Num id = 1) {
if (b == 0) return id;
Num x = id;
while (b > 0) {
if (b & 1) x *= a;
a *= a;
b >>= 1;
}
return x;
}
template <typename T>
std::vector<std::pair<std::size_t, T>> enumerate(const std::vector<T>& data) {
std::vector<std::pair<std::size_t, T>> ret;
for (std::size_t index = 0; index < data.size(); index++)
ret.emplace_back(index, data[index]);
return ret;
}
/* template end */
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
i64 n;
std::cin>>n;
std::string s;
std::cin>>s;
std::stack<char> st;
st.emplace('0');
st.emplace('0');
i64 ans=n;
for(const auto& e:s){
st.emplace(e);
char f,o,x;
x = st.top();st.pop();
o = st.top();st.pop();
f = st.top();st.pop();
if(f == 'f' && o == 'o' && x == 'x'){
ans-=3;
}else{
st.emplace(f);
st.emplace(o);
st.emplace(x);
}
}
std::cout<<ans<<"\n";
return 0;
}
| /******************************************************/
/******************************************************/
/** **/
/** BISMILLAHIR RAHMANIR RAHIM **/
/** REAZ AHAMMED CHOWDHURY - reaziii **/
/** Department of Computer Science and Engineering **/
/* INSTITUTE OF SCIENCE AND TECHNOLOGY **/
/** **/
/******************************************************/
/******************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pcase(x) printf("Case %d: ",x++)
#define fri(f) for(int i=0;i<f;i++)
#define frj(f) for(int j=0;j<f;j++)
#define reset(x) memset(x,-1,sizeof(x))
#define all(x) x.begin(),x.end()
#define input freopen("input.txt","r",stdin);
#define output freopen("output.txt","w",stdout)
#define infi INT_MAX
#define linfi LLONG_MAX
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mgraph map<int,vector<int> >
#define pb push_back
#define clr(x) memset(x,0,sizeof(x))
#define fro(i,x,y) for(int i=x;i<y;i++)
#define ech(x,a) for(auto &x : a)
#define ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define pi acos(-1.0)
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type,
less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
Ordered_set;
template<class T> void read(T& x) {cin >> x;}
template<class H, class... T> void read(H& h, T&... t) {read(h); read(t...);}
template<class A> void read(vector<A>& x) {for (auto &a : x) read(a);}
template<class H> void print(vector<H> &x) {ech(a, x) cout << a << " "; cout << endl;}
template<class P> void debug(P h) {
#ifndef ONLINE_JUDGE
cerr << h << " ";
#endif
}
template<class W, class... V> void debug(W h, V... t) {
#ifndef ONLINE_JUDGE
debug(h);
debug(t...);
cerr << endl;
#endif
}
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
bool checkbitt(ll num, int pos) {return (num >> pos) & 1;}
ll setbitt(ll num, ll pos) {return (1 << pos) | num;}
ll resetbitt(ll num, int pos) {if (!checkbitt(num, pos)) return num; else return (1 << pos)^num;}
ll bigmod(ll a, ll b, ll mod) {if (b == 0) return 1; if (b == 1) return a; if (b & 1) {return ((a % mod) * (bigmod(a, b - 1, mod) % mod)) % mod;} ll x = bigmod(a, b / 2, mod); return (x * x) % mod;}
ll geti() {ll x; read(x); return x;}
ll cdiv(ll a, ll b) {ll ret = a / b; if (a % b) ret++; return ret;}
const ll mod = 1e9 + 7;
const ll N = 2e5 + 10;
int dx[4] = { +0, +0, +1, -1};
int dy[4] = { -1, +1, +0, +0};
//................................___Start from here___...............................//
//................................_____________________..............................//
int solve() {
ll n, k;
cin >> n >> k;
ll a = bigmod(10, n, k * k);
a /= k;
a %= k;
cout << a << endl;
return 0;
}
int main(int argc, char* argv[]) {
if (argc <= 1) {
#ifndef ONLINE_JUDGE
input;
output;
#endif
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(0);
#endif
}
int cs = 1, cn = 1;
// read(cs);
while (cs--) {
solve();
}
} |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int t;ll n,s,k,d;
inline ll gcd(ll x,ll y){
while(y^=x^=y^=x%=y);
return x;
}
ll exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){x=1,y=0;return a;}
int r=exgcd(b,a%b,y,x);
y-=a/b*x;
return r;
}
signed main(){
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld",&n,&s,&k);
ll c=-s,x,y,d=exgcd(k,n,x,y);
if(c%d)puts("-1");
else{
ll a=c/d,b=n/d;
printf("%lld\n",((a*x)%b+b)%b);
}
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define reps(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define ZERO {puts("0"); return 0;}
#define NO {puts("No"); return 0;}
#define ALL(obj) begin(obj), end(obj)
#define pb push_back
template<class T> void chmin(T& a, T b) { if (a > b) a = b; }
template<class T> void chmax(T& a, T b) { if (a < b) a = b; }
ll Euler (ll x)
{
ll ans = x;
for (ll i = 2; i * i <= x; ++i)
{
if (x % i == 0) ans = ans * (i-1) / i;
while (x % i == 0) x /= i;
}
if (x != 1)
ans = ans * (x-1) / x;
return ans;
}
ll power_mod(ll x, ll k, ll n)
{
ll ans = 1;
while (k > 0)
{
if (k & 1) ans = ans * x % n;
x = x * x % n;
k >>= 1;
}
return ans;
}
ll solve()
{
ll n, s, k; cin >> n >> s >> k;
s = n - s;
ll g = __gcd(k, n);
if (s % g) return -1;
n /= g, s /= g, k /= g;
ll phi = Euler(n);
ll inv_k= power_mod(k, phi-1, n);
return inv_k*s%n;
}
int main()
{
int t; cin >> t;
while (t--)
cout << solve() << endl;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
#define pb push_back
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define TIME 1.0*clock()/CLOCKS_PER_SEC
#define pii pair < int , int >
#define Endl '\n'
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree <T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7;
const int FFTM = 998244353;
const int SX[4] = {0 , 1 , - 1 , 0};
const int SY[4] = {1 , 0 , 0 , - 1};
const int rx[8] = {1, - 1, 0, 0, 1, 1, -1, -1};
const int ry[8] = {0, 0, 1, - 1, 1, - 1, 1, -1};
const int kx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
const int ky[8] = {2, -2, 2, -2, 1, -1, 1, -1};
typedef cc_hash_table< int, int, hash<int>, equal_to<int>, direct_mask_range_hashing<int>, hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<true>, true>> ht;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("railroad.in", "r", stdin);
// freopen("railroad.out", "w", stdout);
#endif
int n;
cin >> n;
int a[n];
for (int i = 0;i < n; ++i)cin >> a[i];
sort(a, a + n);
if (a[0] != 1){
cout << "No" << endl;
return 0;
}
for (int i = 1;i < n; ++i){
if (a[i] - a[i - 1] != 1){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}
| // Powered by CP Editor (https://cpeditor.org)
//a+b = (a^b) + 2*(a&b)
//b^c = (a^b)^(a^c)
//gcd(x,y) = gcd(x-y,y)
//if n = 5000 then complexity cannot be (n^2*log(n)) means no map ,no sets
//check for long long overflow if input is large
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
//#define mod 1000000007
#define mod 998244353
#define pb push_back
#define ll long long
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define mii map<int,int>
#define loop(i,n) for(int i=0;i<n;i++)
#define pp ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define fill(a,b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define en cout<<"\n"
#define trace(x) cout<<#x<<": "<<x<<" "<<endl
#define trace2(x,y) cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x,y,z) cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a,b,c,d) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
const long long INF = 2000000000000000000;
long long power(long long a,long long b){
long long ans=1;
while(b>0){
if(b&1){ans=(ans*a)%mod;}
a=(a*a)%mod;
b>>=1;
}
return ans;
}
void solve()
{
int n;
cin>>n;
int ar[n];
loop(i,n) cin>>ar[i];
sort(ar,ar+n);
for(int i=0;i<n;i++)
{
if(ar[i]!=i+1)
{
cout<<"No";return;
}
}
cout<<"Yes";
}
int32_t main()
{
pp;
int test=1;
// cin>>test;
while(test--)
{
solve();
en;
}
return 0 ;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t,a,b,s,z,r;
cin>>t;
s=0;
while(t--)
{
cin>>a>>b;
z=((b+1)*(b))/2;
r=((a)*(a-1))/2;
s+=z-r;
}
cout<<s<<"\n";
return 0;
} | /*input
1000 1
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T = 1; // cin>>T;
while(T--)
{
int N,W;cin>>N>>W;cout<<(N/W)<<"\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int n;
cin >> n;
if(n<=100) cout << "1";
else
{
int l=n/100;
if(l*100<n) l++;
cout << l;
}
}
| #include <bits/stdc++.h>
#define INT_MINs -2000000000
#define INT_MAXs 1000000001
#define MID int mid=(l+r)/2
#define REP1(i,x,y) for(int i=x;i<y;i++)
#define REP2(i,x,y) for(int i=x;i<=y;i++)
#define ls (2*k)
#define rs (2*k+1)
#define lson l,mid,2*k
#define rson mid+1,r,2*k+1
#define lc u << 1
#define rc u << 1 | 1
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define IOS ios::sync_with_stdio(0);cin.tie(NULL);
#define pb(a) push_back(a)
#define pi acos(-1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=2520;
const int dx[8] = { 0,-1,0,1,-1,-1,1,1}, dy[8] = { 1,0,-1,0,-1,1,-1,1};
void solve(){
int n;
cin>>n;
cout<<(n+99)/100<<endl;
}
int main()
{
IOS;
solve();
return 0;
}
|
#include <iostream>
int main(){
int n, i;
double re=0.0;
std::cin >> n;
for(i=0; i<n-1; i++){
re += (double) (n/(n-1.0-i));
}
std::cout << std::fixed
<< re << std::endl;
}
| #include <bits/stdc++.h>
template <typename T>
T next() {
T temp; std::cin >> temp;
return temp;
}
template <>
int next() {
int temp; scanf("%d", &temp);
return temp;
}
template <>
long long next() {
long long temp; scanf("%lld", &temp);
return temp;
}
template <>
double next() {
double temp; scanf("%lf", &temp);
return temp;
}
template <>
float next() {
float temp; scanf("%f", &temp);
return temp;
}
template <typename T1, typename T2>
std::pair<T1, T2> next() {
std::pair<T1, T2> temp;
temp.first = next<T1>();
temp.second = next<T2>();
return temp;
}
template <typename T>
std::vector<T> next(int n) {
std::vector<T> temp(n);
for(int i = 0; i < n; i++)
temp[i] = next<T>();
return temp;
}
template <typename T1, typename T2>
std::vector<std::pair<T1, T2>> next(int n) {
std::vector<std::pair<T1, T2>> temp(n);
for(int i = 0; i < n; i++)
temp[i] = next<T1, T2>();
return temp;
}
template <typename T>
void print(const T &x, char endc = '\n') {
std::cout << x << endc;
return;
}
void print(const int &x, char endc = '\n') {
printf("%d%c", x, endc);
return;
}
void print(const long long &x, char endc = '\n') {
printf("%lld%c", x, endc);
return;
}
void print(const size_t &x, char endc = '\n') {
printf("%zu%c", x, endc);
return;
}
void print(const float &x, char endc = '\n') {
printf("%f%c", x, endc);
return;
}
void print(const double &x, char endc = '\n') {
printf("%lf%c", x, endc);
return;
}
template<typename T1, typename T2>
void print(const std::pair<T1, T2> &p, char sepc = ' ', char endc = '\n') {
print(p.first, sepc);
print(p.second, endc);
return;
}
template<typename T>
void print(const std::vector<T> &v, char sepc = ' ', char endc = '\n') {
for(const T &e : v) print(e, sepc);
printf("%c", endc);
return;
}
template<typename T>
class SparseGraph {
struct Node {
std::vector<std::pair<int, T>> adj;
T value;
};
private:
std::vector<Node> nodes;
int nodeSize;
int edgeSize = 0;
public:
SparseGraph(int n) {
nodes.resize(n);
nodeSize = n;
}
SparseGraph(const std::vector<T> &values) {
int n = values.size();
nodes.resize(n);
nodeSize = n;
for(int i = 0; i < n; i++)
nodes[i].value = values[i];
}
void addEdge(int u, int v, const T &edgeVal = 0) {
nodes[u].adj.push_back({v, edgeVal});
edgeSize++;
}
const int &getNodeSize() const {
return nodeSize;
}
const int &getEdgeSize() const {
return edgeSize;
}
const T &valueOf(int here) const {
return nodes[here].value;
}
const std::vector<std::pair<int, T>> &adjOf(int here) const {
return nodes[here].adj;
}
};
using namespace std;
double solve(int connected, int disconnected, vector<double> &memo) {
if(disconnected == 0)
return 0.0;
double &ret = memo[connected];
if(!ret) {
ret = solve(connected+1, disconnected-1, memo) + 1.0 + 1.0*connected/disconnected;
}
return ret;
}
void eachTC() {
int N = next<int>();
vector<double> memo(N, 0.0);
double res = solve(1, N-1, memo);
printf("%.9lf", res);
}
int main() {
constexpr bool multipleTC = false;
int T = multipleTC ? next<int>() : 1;
while(T--) eachTC();
return 0;
}
|
//~ author : Sumit Prajapati
#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
// #define int long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define trav(x,v) for(auto &x:v)
#define rep(i,n) for(int i=0;i<n;i++)
#define repe(i,n) for(int i=1;i<=n;i++)
#define read(a,n) rep(i,n)cin>>a[i]
#define reade(a,n) repe(i,n)cin>>a[i]
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define printar(a,s,e) FOR(i,s,e)cout<<a[i]<<" ";cout<<'\n'
#define curtime chrono::high_resolution_clock::now()
#define timedif(start,end) chrono::duration_cast<chrono::nanoseconds>(end - start).count()
auto time0 = curtime;
const int MD=1e9+7;
const int MDL=998244353;
const int INF=1e9;
const int MX=1e5+5;
void solve(){
int n;
string s;
cin>>n;
cin>>s;
if(s[0]!=s[n-1]){
cout<<"1\n";
return;
}
FOR(i,0,n-2)
if(s[i]!=s[0] && s[i+1]!=s[0]){
cout<<"2\n";
return;
}
cout<<"-1\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
time0 = curtime;
srand(time(NULL));
int t=1;
// cin>>t;
repe(tt,t){
// cout<<"Case #"<<tt<<": ";
solve();
}
// cerr<<"Execution Time: "<<timedif(time0,curtime)*1e-9<<" sec\n";
return 0;
} | #include<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 pb push_back
#define mp make_pair
#define lli long long int
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define mii map<lli,lli>
#define msi map<string,lli>
#define pii pair<lli,lli>
#define vi vector<lli>
#define vii vector<pair<lli,lli>>
#define vvi vector< vector<lli> >
#define sz(x) (lli)x.size()
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define MAX1 998244353
#define MAX2 1000000007
// Policy based data structure
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> Ordered_set;
// Ordered_set s;
// *s.find_by_order(a);
// s.order_of_key(a);
// s.erase(s.find_by_order( s.order_of_key(v[i-k]) ));
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#define input(data,n,command) for(lli i=0;i<n;i++){lli tmp;cin>>tmp;data.pb(tmp);command}
#define sort_asc(a) sort(a.begin(),a.end());
//#define sort_desc(a) sort(a.begin(),a.end(),greater<lli>());
#define display(a) for(lli i = 0 ;i<a.size();i++){cout<<a[i]<<" ";} cout<<endl;
#define rep(i,a,b) for(lli i = a;i < b;i++)
#define endl "\n"
#define INF 1000000000000000000
#define MAXN 100010
//Written By MUDIT BHARDWAJ...
/*--------------------------------------------------------------------*/
//vector<pair<lli,lli>>adj[300010];
lli n;
vector<pair<lli,pii>> v;
lli intersect(pair<lli,pii> tmp1,pair<lli,pii> tmp2)
{
lli t1,l1,r1,t2,l2,r2;
t1 = tmp1.F;
l1 = tmp1.S.F;
r1 = tmp1.S.S;
t2 = tmp2.F;
l2 = tmp2.S.F;
r2 = tmp2.S.S;
lli l = max(l1,l2);
lli r = min(r1,r2);
if(l < r)
{
return true;
}
else if(l == r)
{
if(l1 <= l2)
{
if(((t1 != 2)&&(t1 != 4)&&(t2 != 3)&&(t2 != 4)))
{
return true;
}
else
{
return false;
}
}
else
{
if(((t1 != 3)&&(t1 != 4)&&(t2 != 2)&&(t2 != 4)))
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
lli solve()
{
//auto start = chrono::steady_clock::now();
//start from here
cin>>n;
rep(i,0,n)
{
lli a,b,c;
cin>>a>>b>>c;
v.pb(mp(a,mp(b,c)));
}
lli ans = 0;
for(lli i = 0;i<n;i++)
{
for(lli j = i+1;j<n;j++)
{
if(intersect(v[i],v[j]))
{
ans++;
}
}
}
cout<<ans<<endl;
//auto end = chrono::steady_clock::now();
//cout << "Elapsed time in milliseconds : "<< chrono::duration_cast<chrono::milliseconds>(end - start).count()<< " ms" << endl;
return 0;
}
int main()
{
ios
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
lli T=1;
//cin>>T;
while(T--)
{
solve();
}
return 0;
}
/*-----------------------------------------------------------------------*/ |
#include <bits/stdc++.h>
using namespace std;
int ctoi(char);
#define MAX_N 200001
#define l long
#define ll long long
#define f(a, b) for (ll i = a; i < b; i++)
#define f0(a) for (ll i=0;i<a;i++)
#define input(d) cin >> d;
#define inputs(n,d) for(int i=0;i<n;i++){cin >> d[i];}
#define output(d) cout << d << endl;
#define outputs(n,d) for(int i=0;i<n;i++){cout << d[i] << endl;}
int main(void) {
//ll n;
//l n;
l plus, minus;
l ans, tmp;
input(plus);
input(minus);
//vector<l> tate(h,0);
//vector<l> yoko(w,0);
//vector<l> tate_max(2,0);
//vector<l> yoko_max(2,0);
//vector<vector <l> > bakudan(m,vector<l>(2,0));
//
tmp = (plus+minus)/2;
ans = plus - tmp;
//
cout << tmp << " " << ans << endl;
return 0;
}
int ctoi(char c){
switch(c){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default : return -1;
}
}
| #include<bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define mp make_pair
#define endl "\n"
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define lb lower_bound
#define forn(i,n) for(ll i=0 ; i<n ; i++)
#define arr(a,n) for(ll i=0;i<n;i++){ cin>>a[i]; }
#define ub upper_bound
#define pb push_back
#define pi pair<int,int>
#define mod 1000000007
#define vectormax *max_element
#define vectormin *min_element
#define arraysize 1000000
#define setbit _builtin_popcountll
using namespace std;
int main(){
int a,b;
cin>>a>>b;
ll temp=(a+b)/2;
ll hey=(a-b)/2;
cout<<temp<<" "<<hey<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int64_t comb(int x, int y) {
int64_t res(1);
for (int i = 1; i <= x; ++i) {
res *= (x + y + 1 - i);
res /= i;
}
return res;
}
string solve(int64_t A, int64_t B, int64_t K) {
string res;
while (A + B > 0) {
if (K == 0) {
for (int i = 0; i < A; ++i) {
res.push_back('a');
}
for (int i = 0; i < B; ++i) {
res.push_back('b');
}
break;
}
auto v = comb(A-1, B);
// cerr << K << ", " << v << endl;
if (K >= v) {
res.push_back('b');
K -= v;
B -= 1;
} else {
res.push_back('a');
A -= 1;
}
}
return res;
}
// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
constexpr char endl = '\n';
int64_t A, B, K;
cin >> A >> B >> K;
K--;
auto ans = solve(A, B, K);
cout << ans << endl;
return 0;
}
|
// I solemnly swear that I am upto no good //
#include <bits/stdc++.h>
using namespace std;
#define sub freopen("input.txt", "r", stdin);//freopen("output.txt", "w", stdout);
#define ll long long
#define ld long double
#define ttime cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
#define pb push_back
#define unq(a) sort(all(a));(a).resize(unique(all(a)) - (a).begin())
#define sz(x) (int)((x).size())
#define input(arr,n) for(ll i1=0;i1<n;i1++ )cin>>arr[i1]
#define fast ios_base::sync_with_stdio(false);cin.tie(0);
#define all(x) (x).begin(),(x).end()
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define PR(x) cout << #x " = " << (x) << "\n"
#define mp make_pair
#define ff first
#define ss second
#define YY cout<<"Yes"<<endl
#define NN cout<<"No"<<endl
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
const long long INF=1e18;
const long long A=200005;
const long long mod=1000000007;
#define endl "\n"
// #define int ll
typedef pair<ll,ll> pairll;
typedef map<ll,ll> mapll;
typedef map<char,ll> mapch;
bool comp(ll a,ll b){
return a>b;
}
// Function to find the nCr
ll ncr(int n, int r)
{
// p holds the value of n*(n-1)*(n-2)...,
// k holds the value of r*(r-1)...
long long p = 1, k = 1;
// C(n, r) == C(n, n-r),
// choosing the smaller value
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
// gcd of p, k
long long m = __gcd(p, k);
// dividing by gcd, to simplify
// product division by their gcd
// saves from the overflow
p /= m;
k /= m;
n--;
r--;
}
// k should be simplified to 1
// as C(n, r) is a natural number
// (denominator should be 1 ) .
}
else
p = 1;
// if our approach is correct p = ans and k =1
return p;
}
ll n,k,tt=1;
void solve(){
//string s,t;
// ll m,q,x,y,ans=0;
ll a,b,ans=0;
cin>>a>>b>>k;
string s;
ll sz=a+b;
// PR(k);
while(sz && a && b){
// PR(k);
ll p=ncr(sz-1, a-1);
if(p>=k){
s.pb('a');
a--;
}
else{
k-=p;
s.pb('b');
b--;
}
sz--;
}
// cout<<a<<endl<<b<<endl;
while(a){
s.pb('a');a--;
}
while(b){
s.pb('b');b--;
}
cout<<s<<endl;
}
signed main(){
// sub;
fast;
// cout<<"DDDDD";
// pre();
// clock_t clk = clock();
ll t=1;
// cin>>t;
while(t--){
solve();
}
// ttime;
return 0;
}
// Mischief Managed //
|
#include <iostream>
using namespace std;
int main(void){
int A, B, C;
cin >> A >> B >> C;
if (A*A + B*B < C*C){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
}
| #include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int v, t, s, d;
cin >> v >> t >> s >> d;
if(v*t <= d && d <= v*s) cout << "No" << endl;
else cout << "Yes" << endl;
return 0;
} |
#include <set>
#include <iostream>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int32_t main() {
ios::sync_with_stdio(false);
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);
}
while (s.size() > 1) {
int z = *s.begin();
auto it2 = s.end();
--it2;
int x = *it2;
s.erase(it2);
s.insert(x - z);
}
cout << *s.begin();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int a;
cin>>a;
int arr[a];
for(int i=0;i<a;i++){
cin>>arr[i];
}
sort(arr,arr+a);
for(int i=0;i<a;i++){
if(arr[i]!=i+1){
cout<<"No";
return 0;
}
}
cout<<"Yes";
return 0;
} |
#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector< pii >
#define vpll vector< pll >
#define mpii map<int,int>
#define mpll map<ll,ll>
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define s(v) v.size()
#define test ll t;cin>>t;while(t--)
#define vec vector<ll>
#define read0(v,n) for(int i=0;i<n;i++)cin>>v[i];
#define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i];
#define trav(a,x) for (auto& a: x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define cut(x) {cout<<x;return 0;}
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define nl '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll gcd(ll a, ll b)
{
if (b==0)return a;
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;
while(b)
{
if(b&1)
ans=(ans*a)%MOD;
b/=2;
a=(a*a)%MOD;
}
return ans;
}
ll dp[1000006];
int main()
{
fast
ll ans=0;
dp[1]=1;
FOR(i,1,1000002)
{
dp[i]=dp[i-1]+i;
}
test
{
ll a,b;
cin>>a>>b;
ans+=dp[b]-dp[a-1];
}
cout<<ans<<nl;
}
| #include <iostream>
const int LAW = 200;
long long int count[LAW];
int main() {
for (int i=0; i<LAW; ++i) {
count[i] = 0;
}
long long int N, n;
std::cin >> N;
for (long long int i=0; i<N; ++i) {
std::cin >> n;
n %= LAW;
count[n]++;
}
long long int answer = 0;
for (auto c : count) {
if (c <= 1) continue;
answer += c * (c - 1) / 2;
}
std::cout << answer << std::endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
double calculate_expected(double x, ll nums[], ll n, ll sum, unordered_map<double, double>& visited) {
if(visited.find(x) != visited.end()){
return visited[x];
}
double expected_val = sum + x*n;
for(int i=0; i<n; i++) {
expected_val -= min((double)nums[i], 2*x);
}
double exp = expected_val/n;
visited[x] = exp;
//cout << endl << x << " expected val: " << exp << endl;
return exp;
}
double binary_s(double min_v, double max, ll sum, ll n, ll nums[], double err, unordered_map<double, double>& visited) {
//cout << endl << min_v << " " << max << endl;
double mid = (max+min_v)/2;
if(max-min_v < err)
return mid;
double quarter = (mid+min_v)/2;
double three_quarter = (max+mid)/2;
double min_exp = calculate_expected(min_v, nums, n, sum, visited);
double quarter_exp = calculate_expected(quarter, nums, n, sum, visited);
double mid_exp = calculate_expected(mid, nums, n, sum, visited);
double three_quarter_exp = calculate_expected(three_quarter, nums, n, sum, visited);
double max_exp = calculate_expected(max, nums, n, sum, visited);
if(quarter_exp < min_exp && quarter_exp < mid_exp) {
return binary_s(min_v, mid, sum, n, nums, err, visited);
}
if(three_quarter_exp < mid_exp && three_quarter_exp < max_exp) {
return binary_s(mid, max, sum, n, nums, err, visited);
}
return binary_s(quarter, three_quarter, sum, n, nums, err, visited);
}
void solve(ll nums[], ll n) {
ll sum = 0;
ll max = 0;
for(int i=0; i<n; i++) {
sum += nums[i];
if(nums[i]>max)
max = nums[i];
}
double average = (sum*1.0)/n;
unordered_map<double, double> visited;
double val = binary_s(0, average, sum, n, nums, 0.000001, visited);
cout.precision(30);
cout << calculate_expected(val, nums, n, sum, visited) << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll n;
cin >> n;
ll nums[n];
for(int i=0; i<n; i++) {
cin >> nums[i];
}
solve(nums, n);
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
template <typename T>
void print_vec(const vector<T>& v, bool newline = true) {
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
if (newline) {
cout << "\n";
}
}
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
const int maxn = (int)2e5 + 105;
const int mod = (int)1e9 + 7;
#define sz(x) ((int)(x.size()))
#define pb push_back
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repr(i,a,b) for (int i = (a); i >= (b); i--)
int fact_setup=0; int * fact;
int ex (int a, int b){
if(b==0)return 1; int e = ex(a,b/2); e=(1ll*e*e)%mod; if(b&1)e=(1ll*e*a)%mod; return e;
}
int inv (int a){
return ex(a, mod - 2);
}
int choose (int a, int b){
if(!fact_setup){
fact_setup=1;
fact = new int [maxn];
fact[0]=1; rep (i,1,maxn-1) fact[i]=(i*fact[i-1])%mod;
}
if(a<b)return 0;
int num = fact[a];
int denom = (fact[b] * fact[a - b]) % mod;
return (num * inv(denom)) % mod;
}
#define pii pair <int, int>
#define pvi pair <vector <int>, int>
#define pai array <int,2>
#define pdi array <double, 2>
#define pdt array <double, 3>
#define pti array <int,3>
#define pfi array <int,4>
#define all(v) v.begin(), v.end()
int arr [maxn];
main(){
cin.tie(0); ios_base::sync_with_stdio(0);
int n; cin >> n;
double tot=0;
rep (i,1,n) {cin >> arr[i];tot+=arr[i];}
tot/=(double)n;
sort(arr+1,arr+n+1);
double cur=0, ans = 0;
rep (i,1,n){
double tt = ((n - i + 1) * arr[i] + cur) / (double)n - (arr[i]/2.0);
ans = max(ans, tt);
cur+=arr[i];
}
cout<<fixed<<setprecision(12) << tot-ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#define PI 3.14159265358979323846
#define MAXINF (1e18L)
#define INF (1e9L)
#define EPS (1e-9)
#define MOD ((ll)(1e9+7))
#define REP(i, n) for(int i=0;i<int(n);++i)
#define REPA(x, v) for(auto&& x:v)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define ALL(v) v.begin(),v.end()
#define FIND(v,x) (binary_search(ALL(v),(x)))
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(v.rbegin(),v.rend())
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define Yes(n) cout<<((n)?"Yes":"No")<<endl
#define YES(n) cout<<((n)?"YES":"NO")<<endl
#define pb push_back
#define fi first
#define se second
using namespace std;
template<class A>void pr(A a){cout << (a) << endl;}
template<class A,class B>void pr(A a,B b){cout << a << " " ;pr(b);}
template<class A,class B,class C>void pr(A a,B b,C c){cout << a << " " ;pr(b,c);}
template<class A,class B,class C,class D>void pr(A a,B b,C c,D d){cout << a << " " ;pr(b,c,d);}
template<class T> void pr_vec(vector<T> &v, char ep=' '){int n=v.size();REP(ni,n){cout<<v[ni];if(ni!=n-1)cout<<ep;}cout<<endl;};
template<class T> inline bool chmin(T& a, T b){return a>b ? a=b, true : false;}
template<class T> inline bool chmax(T& a, T b){return a<b ? a=b, true : false;}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
double dp[110][110][110];
double f(int a, int b, int c){
if(a==100||b==100||c==100) return 0;
if(dp[a][b][c]!=0) return dp[a][b][c];
return dp[a][b][c] = ((double)a/(a+b+c))*(f(a+1,b,c)+1) + ((double)b/(a+b+c))*(f(a,b+1,c)+1) + ((double)c/(a+b+c))*(f(a,b,c+1)+1);
}
int main(void)
{
int A,B,C;
cin >> A >> B >> C;
printf("%.10f\n", f(A,B,C));
} | /*
/^--^\
\____/
/ \ _____ _ __ __ ____ _ ____ ____ _____
| || ()_)| |\ \/ /| ===|| |__ / (__` / () \|_ _|
\__ __/ |_| |_|/_/\_\|____||____|\____)/__/\__\ |_|
|^|^\ \^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|
| | |\ \| | | | | | | | | | | | | | | | | | | | | | | | |
#####/ /#################################################
| | |\/ | | | | | | | | | | | | | | | | | | | | | | | | |
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|*/
#pragma GCC optimize("O4,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<ll,ll>;
#define int ll
#define double long double
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Forr(i,a,b) for(int i=a;i>=b;i--)
#define F first
#define S second
#define L(id) (id<<1)
#define R(id) (id<<1|1)
#define LO(x) (x&(-x))
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define mkp make_pair
#define MOD (ll)(1e9+7)
#define INF (1e15)
#define EPS (1e-6)
#ifdef LOCALMEOW
#define debug(...) do{\
cerr << __PRETTY_FUNCTION__ << " - " << __LINE__ <<\
" : ("#__VA_ARGS__ << ") = ";\
_OUT(__VA_ARGS__);\
}while(0)
template<typename T> void _OUT(T x) { cerr << x << "\n"; }
template<typename T,typename...I>
void _OUT(T x,I ...tail) { cerr << x << ", "; _OUT(tail...); }
#else
#define debug(...)
#endif
inline void IOS(){ ios::sync_with_stdio(false); cin.tie(0); }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int gcd(int a,int b) { return b==0?a:gcd(b,a%b); }
int lcm(int a,int b) { return a/gcd(a,b)*b; }
int fpow(int b,int p){
int ans=1,now=b;
while(p){
if(p&1) ans=ans*now%MOD;
p/=2; now=now*now%MOD;
}
return ans;
}
void minify(int &a,int b) { if(b<a) a=b; }
void maxify(int &a,int b) { if(b>a) a=b; }
int32_t main(){
IOS();
//code...
int a,b,c; cin>>a>>b>>c;
if((c==0 && a>b) || (c==1 && a>=b)) cout<<"Takahashi\n";
else cout<<"Aoki\n";
return 0;
} |
#include <bits/stdc++.h>
typedef long long int LL;
typedef unsigned long long int ULL;
using namespace std;
// 插入此處
const LL M = 1e9 + 7;
LL a[3005];
LL dp[3005][3005];
LL fix[3005];
// dp[k][r] 代表 k 節,剩餘 r
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
dp[1][0] = 1;
int mk = 1;
for (int i = 2; i <= n; i++) {
for (int k = mk + 1; k >= 2; k--) {
fix[k] += a[i];
fix[k] %= k;
// printf("k = %d, dp[k - 1][k - 2 - fix[k - 1]] = %d\n", k, dp[k - 1][k - 2 - fix[k - 1]]);
if (dp[k - 1][(k - 1 - fix[k - 1]) % (k - 1)]) {
dp[k][(a[i] + k - fix[k]) % k] += dp[k - 1][(k - 1 - fix[k - 1]) % (k - 1)];
dp[k][(a[i] + k - fix[k]) % k] %= M;
mk = max(mk, k);
}
}
// printf("##################### i = %d\n", i);
// for (int k = 1; k <= mk; k++) {
// printf("k = %d, fix[k] = %lld\n", k, fix[k]);
// for (int r = 0; r < k; r++) {
// printf("dp[%d][%d] = %lld\n", k, r, dp[k][r]);
// }
// }
}
LL sum = 0;
for (int k = 1; k <= n; k++) {
sum += dp[k][(k - fix[k]) % k];
sum %= M;
}
printf("%lld\n", sum);
}
| #include<stdio.h>
const int MOD=998244353;
static inline int IN(void)
{
int x=0,f=0,c=getchar();while(c<48||c>57){f^=(c==45),c=getchar();}
while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
static inline void OUT(int x){if(x<0){putchar('-'),x=-x;}if(x>=10){OUT(x/10);}putchar(x%10+48);}
static inline int MPow(int a,int b){return b?1l*MPow(1l*a*a%MOD,b>>1)*(b&1?a:1)%MOD:1;}
int main(void)
{
int N=IN(),M=IN(),K=IN(),total=0,x=0;
if(N==1||M==1){return OUT(MPow(K,N>M?N:M)),0;}
while(++x<=K){total=(1l*total+1l*(1l*MPow(x,N)+MOD-MPow(x-1,N))%MOD*MPow(K-x+1,M)%MOD)%MOD;}
return OUT(total),0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, ll> pii;
#define pb push_back
const int MOD = 1e9 + 7, N = 2e5 + 5, K = 60;
vector<pii> g[N];
int sub[N];
ll add(ll x, ll y)
{
x += y;
while (x >= MOD)
x -= MOD;
while (x < 0)
x += MOD;
return x;
}
ll mul(ll x, ll y)
{
x %= MOD;
return (x * y) % MOD;
}
int subtree_sum(int cur, int last)
{
sub[cur] = 1;
for (pii e : g[cur])
if (e.first != last)
sub[cur] += subtree_sum(e.first, cur);
return sub[cur];
}
void merge(vector<int> &b1, vector<int> &b2)
{
for (int i = 0; i < K; i++)
b1[i] += b2[i];
}
vector<int> precompute(vector<vector<int>> &b, int cur, int last)
{
for (pii e : g[cur])
{
if (e.first != last)
{
vector<int> tmp = precompute(b, e.first, cur);
int sz = sub[e.first] - 1;
for (ll i = 0; i < K; i++)
{
if ((e.second >> i) & 1)
tmp[i] = sz - tmp[i] + 1;
b[cur][i] += tmp[i];
}
}
}
return b[cur];
}
// Binary Exponentiation O(logn)
// n^m mod p
int power(int n, int m, int p)
{
int res = 1;
while (m > 0)
{
if (m & 1)
res = (res * 1ll * n) % p;
n = (n * 1ll * n) % p;
m /= 2;
}
return res;
}
ll dfs(vector<vector<int>> &b, int cur, int last, int n, vector<int> par)
{
// cout << cur << "\n";
// for (int i = 0; i < 3; i++)
// cout << par[i] << " ";
// cout << "\n";
// ll v = 0;
// for (int i = 0; i < K; i++)
// {
// if (par[i] > 0)
// {
// v = add(v, mul(1ll << i, par[i]));
// v %= MOD;
// }
// }
// cout << v << "\n";
ll res = 0;
for (int i = 0; i < K; i++)
{
if (par[i] + b[cur][i] > 0)
{
res = add(res, mul(1ll << i, par[i] + b[cur][i]));
res %= MOD;
}
}
merge(par, b[cur]);
for (pii e : g[cur])
{
if (e.first != last)
{
vector<int> tmp = par, ctmp = b[e.first];
int sz = sub[e.first] - 1;
for (ll i = 0; i < K; i++)
if ((e.second >> i) & 1)
ctmp[i] = sz - ctmp[i] + 1;
for (int i = 0; i < K; i++)
tmp[i] -= ctmp[i];
sz = n - sub[e.first] - 1;
for (ll i = 0; i < K; i++)
if ((e.second >> i) & 1)
tmp[i] = sz - tmp[i] + 1;
res = add(res, dfs(b, e.first, cur, n, tmp));
}
}
return res;
}
ll solve()
{
int n, u, v;
ll w;
cin >> n;
vector<vector<int>> b(n + 1, vector<int>(K));
for (int i = 0; i < n - 1; i++)
{
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
subtree_sum(1, 1);
precompute(b, 1, 1);
vector<int> par(K);
return mul(dfs(b, 1, 1, n, par), 500000004);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << solve() << "\n";
return 0;
} |
/*/ Author : Abhishek Chauhan /*/
#include<bits/stdc++.h>
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace __gnu_pbds;
using namespace std;
// a+b = a^b + 2*(a&b)
// According to Fermat's little theorem, (a^b)%mod = ((a^(b%(mod - 1)))%mod) if mod is a prime number and not the other way round.
// whenever using a comparator return true only if a<b
// make it forward first if it doesnt work do the reverse
// vector a = ax + ay b = bx + by then dot porduct is |a||b|cos@ //and also for eas you can use (ax*bx)+(ay*by);
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define mod 1000000007
#define bpop(x) __builtin_popcountll((x))
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
template<class T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
template<class T>
T power(T x, T y){T res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;}
template<class T>
T powermod(T x, T y, T p){T res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
int n;
vector<array<int,3>>arr;
int dp[(1<<18)][18];
int solve(int mask,int prev){
if(mask==((1<<n)-1)){
int dist = abs(arr[0][0]-arr[prev][0]);
dist+= abs(arr[0][1]-arr[prev][1]);
dist+= max((int)0,arr[0][2]-arr[prev][2]);
return dist;
}
int ans = 1e18;
if(dp[mask][prev]!=-1){
return dp[mask][prev];
}
for(int i=0;i<n;i++){
if((mask>>i)&1){
continue;
}
int dist = abs(arr[i][0]-arr[prev][0]);
dist+= abs(arr[i][1]-arr[prev][1]);
int fuk = dist;
dist+= max((int)0,arr[i][2]-arr[prev][2]);
ans = min(ans,dist+fuk+max((int)0,arr[prev][2]-arr[i][2])+solve(mask^(1<<i),prev));
ans = min(ans,dist+solve(mask^(1<<i),i));
}
return dp[mask][prev]=ans;
}
void snow(){
cin>>n;
arr.resize(n);
rep(i,(1<<(18))){
rep(j,n){
dp[i][j] = -1;
}
}
for(int i=0;i<n;i++){
rep(j,3){
cin>>arr[i][j];
}
}
int ans = solve((1<<0),0);
cout<<ans<<endl;
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
fastio;
int _;
_=1;
for(int i=1;i<=_;i++){
snow();
}
return 0;
}
|
#include<algorithm>
#include<iostream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define neinf 0xc0c0c0c0c0c0c0c0ll
#define inf 0x3f3f3f3f3f3f3f3fll
#define uint unsigned int
#define ull unsigned ll
#define HgS 1000000007
#define ll long long
#define reg register
#define opr operator
#define ret return
#define gc getchar
#define pc putchar
#define cst const
#define db double
#define il inline
il ll rd()
{
reg ll res=0,lab=1;
reg char ch=gc();
while((ch<'0'||ch>'9')&&ch!=EOF)
{if(ch=='-')lab=-lab;ch=gc();}
while(ch>='0'&&ch<='9')
res=(res<<3)+(res<<1)+(ch&15),ch=gc();
return res*lab;
}
il void prt(ll x,char t='\n')
{
static char ch[40];reg int tp=0;
if(!x){fputs("0",stdout);if(t)pc(t);return;}
if(x<0)pc('-'),x=-x;
while(x)ch[++tp]=(x%10)^48,x/=10;
while(tp)pc(ch[tp--]);
if(t)pc(t);
}
il ll umax(ll a,ll b){return a>b?a:b;}
il ll umin(ll a,ll b){return a<b?a:b;}
il ll uabs(ll x){return x>0?x:-x;}
il ll qpow(ll n,ll e=HgS-2,ll p=HgS)
{
reg ll res=1;
while(e){if(e&1)res=res*n%p;n=n*n%p;e>>=1;}
return res;
}
ll n,ans=inf;
int main()
{
n=rd();
for(int i=0;i<=62;++i)ans=umin(ans,i+(n>>i)+n-((n>>i)<<i));
prt(ans);
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define db(x) cout << (#x) << " = " << x << "\n" ;
#define pb push_back
using namespace std;
set <ll> ok;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n;
cin >> n;
ll ans = 0;
for(ll i=7;i<=n;i++){
ll f = 0 , x = i;
while(x){
if(x%10==7){
f = 1;
break;
}
x = x/10;
}
x = i;
while(x){
if(x%8==7){
f = 1;
break;
}
x = x/8;
}
ans += f;
}
cout << n - ans << endl;
return 0;
} |
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define vi vector <int>
#define ff first
#define ss second
#define vp vector <pair<int,int>>
#define vpp vector <pair<int,pair<int,int>>>
#define seti set<int>
#define setbit(x) __builtin_popcountll(x)
#define sov(v) accumulate(all(v),0)
#define fs(it,a) for(auto it=a.begin();it!=a.end();it++)
#define pb push_back
#define pob pop_back
#define mp make_pair
#define pqmax priority_queue <int,vector <int>>
#define pqmin priority_queue <int,vector <int>,greater<int>>
#define dq deque <int>
#define umi unordered_map<int,int>
#define ums unordered_map<string,int>
#define sp(x,y) fixed << setprecision(y) << x
#define all(x) x.begin(),x.end()
#define f(x,y,z) for(x=y;x<z;x++)
#define si size()
#define countdigit(x) floor( log10(x) +1)
#define M 1000000007
#define Z 998244353
#define fill(arr,x) memset(arr,x,sizeof(arr))
//Use (k%M+m)%m always where k is any no
#define PI 3.1415926535
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> pdbs;
// null for set,mapped_type for map;
//less for assending greater for descending
//ceil(a/b)=(a+b-1)/b
#define ghadi() cerr<<"\nTime Taken : "<<(float)(clock()-time_p)/CLOCKS_PER_SEC<<"\n";
clock_t time_p=clock();
#define ee "\n"
#define re return
//Author Rahul Sannigrahi
vector<int> take(int n)
{
int i,j;
vi v;
f(i,0,n)
{
cin>>j;
v.pb(j);
}
return v;
}
bool sortinrev(const pair<int,int> &a, //desc of vp
const pair<int,int> &b)
{
return (a.first > b.first);
}
void show(vector<int>v)
{
int i;
for(i=0;i<v.si;i++)
{
cout<<v[i]<<" ";
}
cout<<ee;
}
int decode()
{
int i,j,k,l,n,x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
l=y1*x2+y2*x1;
n=y1+y2;
// cout<<l<<n;
double d=(float)l/n;
cout<<sp(d,10)<<ee;
re 0;
}
int32_t main()
{
IOS
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
int t=1;
//cin>>t;
while(t--)
decode();
return 0;
} | #include <iostream>
#include <cmath>
#define ll long long int
using namespace std;
int main() {
int n;
cin>>n;
int a[n],b[n];
ll ans[1001]={0};
for(int i=0;i<n;i++)
{cin>>a[i];}
for(int i=0;i<n;i++)
{cin>>b[i];}
for (int i=0;i<n;i++)
{
for (int j=a[i];j<=b[i];j++)
{
ans[j]++;
}
}
ll cn=0;
for (int i=0;i<=1000;i++)
{
if (ans[i]==n) cn++;
}
cout<<cn<<"\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define d(x) cout<<x<<" "
#define nl cout<<endl
#define rep(var,init_val,final_val) for(int var = init_val; var < final_val; ++var)
#define show(a,b) rep(i,0,b) d(a[i]);nl;
#define showv(a) for(auto x : a) d(x);nl;
#define vi std::vector<int>
#define pi pair<int,int>
#define mod 1000000007
void solve(){
int n;
cin>>n;
int lo = INT_MIN , hi = INT_MAX;
int a[n] , b[n];
rep(i,0,n) cin>>a[i];
rep(i,0,n) cin>>b[i];
rep(i,0,n){
lo = max(lo,a[i]);
hi = min(hi,b[i]);
}
cout<<max(0,hi-lo+1)<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin>>t;
while(t--)
solve();
return 0;
}
| #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = int64_t;
using vi = vector<int>;
using vs = vector<string>;
using vc = vector<char>;
using vvi = vector<vi>;
using vvs = vector<vs>;
using vvc = vector<vc>;
using pii = pair<int, int>;
int main() {
int n;
cin >> n;
map<int, ll> a;
ll an;
rep (i, n) {
cin >> an;
an %= 200;
a[an]++;
}
ll ans = 0;
rep (i, 200) {
ans += (a[i]*(a[i]-1)) / 2LL;
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define mod ((int)1e9+7)
#define lim 1000000000000000007
#define lim1 18446744073709551615 //Unsigned
#define sq(a) ((a)*(a))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define mms(v,i) memset(v,i,sizeof(v))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPN(i,a,b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> PL;
typedef pair<ll,int> PLI;
typedef pair<int,ll> PIL;
typedef pair<int,pair<int,int> > pii;
typedef pair<double,double> pdd;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
ll power(ll a,ll b) {
ll res=1;
while(b){
if(b%2==1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll gcdll(ll a,ll b) {
if (b==0) return a;
return gcdll(b,a%b);
}
int gcd(int a,int b) {
if (b==0) return a;
return gcd(b,a%b);
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int N = (int)2e5+5;
const int M = (int)3e3+5;
const int Q = 301;
const int logN = 19;
void solve() {
int n;
string s;
cin >> n >> s;
int ans = 0;
REP(i,0,n-1) {
unordered_map<char,int> freq;
REP(j,i,n-1) {
freq[s[j]]++;
if(freq['A'] == freq['T'] && freq['C'] == freq['G']) {
ans++;
}
}
}
cout << ans;
}
int main() {
//freopen("output.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T=1;
// cin>>T;
REP(TC,1,T) {
//cout<<"Case #"<<TC<<": ";
solve();
cout<<"\n";
}
}
| #include <bits/stdc++.h>
using namespace std;
#define LL long long
//#define MOD 1000000007
#define MOD 998244353
#define INF 1000000000000000000
#define VE vector
#define VL vector<LL>
#define VVL VE<VL>
#define VVVL VE<VVL>
#define LD long double
#define PB push_back
#define POB pop_back
#define FOR(i,a,k) for(LL i=a;i<k;i++)
#define rep(i,k) FOR(i,0,k)
#define ALL(a) a.begin(),a.end()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define coutl cout<<fixed<<setprecision(15)//
#define MI modint<MOD>
template<int mod>struct modint{
int x;
modint():x(0){}
modint(LL n):x(n>=0?n%mod:(mod-(-n)%mod)%mod){}
modint &operator+=(const modint &n){if((x+=n.x)>=mod)x-=mod;return *this;}
modint &operator-=(const modint &n){if((x+=mod-n.x)>=mod)x-=mod;return *this;}
modint &operator++(){*this+=1;return *this;}
modint &operator--(){*this-=1;return *this;}
modint &operator*=(const modint &n){x=(int)((LL)x*n.x%mod);return *this;}
modint &operator/=(const modint &n){*this*=n.inv();return *this;}
modint operator-()const{return modint(-x);}
modint operator+(const modint &n)const{return modint(*this)+=n;}
modint operator-(const modint &n)const{return modint(*this)-=n;}
modint operator++(int){modint ret(*this);*this+=1;return ret;}
modint operator--(int){modint ret(*this);*this-=1;return ret;}
modint operator*(const modint &n)const{return modint(*this)*=n;}
modint operator/(const modint &n)const{return modint(*this)/=n;}
bool operator<(const modint &n)const{return x<n.x;}
bool operator>(const modint &n)const{return x>n.x;}
bool operator<=(const modint &n)const{return x<=n.x;}
bool operator>=(const modint &n)const{return x>=n.x;}
bool operator!=(const modint &n)const{return x!=n.x;}
bool operator==(const modint &n)const{return x==n.x;}
friend istream &operator>>(istream &is,modint &n){LL l;is>>l;n=modint<mod>(l);return is;}
friend ostream &operator<<(ostream &os,const modint &n){return os<<n.x;}
int getmod(){return mod;}
modint inv()const{int a=x,b=mod,c=1,d=0,n;while(b){n=a/b;swap(a-=n*b,b);swap(c-=n*d,d);}return modint(c);}
modint pow(LL n)const{modint ret(1),m(x);while(n){if(n&1)ret*=m;m*=m;n>>=1;}return ret;}
};
VL yakusuu(LL N){
LL M=0;
VL ret;
for(LL i=0; i<N; i++){
M++;
if(M>N/M){
for(LL i=0; i<N; i++){
M--;
if(M==0){
return ret;
}
if(M*M==N){
continue;
}
if(N%M==0){
ret.PB(N/M);
}
}
}
if(N%M==0){
ret.PB(M);
}
}
return ret;
}
LL nanjo(LL a,LL n){//aのn乗 N=nanjo(a,n)A
LL ret=1;
for(LL i=0;i<n;i++){
ret*=a;
}
return ret;
}
void YesNo(bool f){
if(f)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
LL kakuketa(LL n){//各桁の和を求める
LL a=0;
while(n>0){
a=a+n%10;
n=n/10;
}
return a;
}
int main(){
LL M,ans=0,N;
cin>>N;
M=N;
rep(i,20){
if(M>=10){
M=M/10;
}
if(M<10){
M=i+2;
break;
}
}
if(M>=0){
ans=0;
}
if(M>=4){
ans+=N-999;
}
if(M>=7){
ans+=N-999999;
}
if(M>=10){
ans+=N-999999999;
}
if(M>=13){
ans+=N-999999999999;
}
if(M>=16){
ans+=N-999999999999999;
}
cout<<ans<<endl;
/*LL N,ans;
cin>>N;
rep(i,N){
if(N%10==)
}
/*LL K,ans=0;
cin>>K;
ans=K-2;//全部同じ数だった場合
if(K>=4){
ans+=(1+(K-2)/2)*K/2*3;//2つ同じ数があった場合
}
if(K>=6){
ans+=(1+(K/2+1)*K/6)**6;
}
cout<<ans<<endl;
LL N,M,T,ans=0;
cin>>N>>M>>T;
ans=N;
VL A(M),B(M);
rep(i,M){
cin>>A[i]>>B[i];
}
rep(i,M){
ans=ans-A[i]+A[i-1];
if(ans<=0){
cout<<"No"<<endl;
break;
}
ans=ans+(B[i]-A[i]);
if(ans>=M){
ans=N;
}
}
cout<<"Yes"<<endl;*/
}
|
#include <bits/stdc++.h>
#define INF 1000000007
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i < b; i++)
using namespace std;
typedef long long ll;
typedef long long int lli;
typedef long double ld;
typedef vector<int> vi;
typedef unordered_map<int,int> umi ;
typedef unordered_set<int> usi ;
typedef pair<int,int> pi;
#include <bits/stdc++.h>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//declare here -------------------------------------------------------------------------------
const int N =1e5+1 ;
struct edge{
int D ;
int T ;
int K ;
} ;
vector<edge> adj[N] ;
ll dist[N] ;
bool visited[N] ;
// solve here ---------------------------------------------------------------------------------
void solve(){
int n,m,x,y;
REP(i,0,N) dist[i]=LLONG_MAX ;
cin>>n>>m>>x>>y ;
int u,v,t,k ;
edge e1 ;
edge e2 ;
REP(i,0,m){
cin>>u>>v>>t>>k ;
e1.T=t ; e2.T= t ; e1.K=k ; e2.K=k ;
e1.D=v ; e2.D= u ;
adj[u].PB(e1) ;
adj[v].PB(e2) ;
}
dist[x]=0 ;
priority_queue<pair<ll,ll>> q ;
q.push({0,x}) ;
while(!q.empty()){
u=q.top().S ; q.pop() ;
if(visited[u]) continue ;
debug(u,dist[u]) ;
visited[u]=true ;
for(auto e:adj[u]){
v=e.D ;
ll add=e.T ;
if(dist[u]%e.K==0) add+=0 ;
else add += (e.K - (dist[u] % e.K)) ;
if(dist[u]+add < dist[v]){
dist[v]=dist[u]+add ;
q.push({-dist[v] ,v}) ;
}
}
}
if(dist[y]==LLONG_MAX) cout<<-1 ;
else cout<<dist[y] ;
}
//--------------------------------------------------------------------------------------------
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
long long test =1 ;
//cin>>test ;
while(test--){
solve() ;
cout<<"\n" ;
}
return 0 ;
} | #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;
struct train{
long long to;
long long time;
long long inter;
};
int main(){
long long n,m,x,y;
cin>>n>>m>>x>>y;
vector<vector<train>> kankei(n+1);
for(long long i=0;i<m;i++){
long long a,b,t,k;
cin>>a>>b>>t>>k;
train hoge={b,t,k};
train fuga={a,t,k};
kankei.at(a).push_back(hoge);
kankei.at(b).push_back(fuga);
}
vector<long long> graph(n+1,inf*inf);
graph.at(x)=0;
priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>> que;
que.push(make_pair(0,x));
while(!que.empty()){
long long here=que.top().second;
long long now=que.top().first;
que.pop();
if(graph.at(here)!=now) continue;
if(here==y){
cout<<now<<endl;
return 0;
}
for(auto next:kankei.at(here)){
long long nto=next.to;
long long ntime=next.time;
long long ninter=next.inter;
long long hogetime=now;
if(now%ninter!=0) hogetime+=ninter-now%ninter;
hogetime+=ntime;
if(graph.at(nto)>hogetime){
graph.at(nto)=hogetime;
que.push(make_pair(hogetime,nto));
}
}
}
cout<<-1<<endl;
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;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//#define int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;
typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
const int inf = 1e9, maxn = 2e5 + 48, mod = 1e9 + 7, N = 1e6 + 5e5 + 12;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 300;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = acos(-1);
const ll INF = 1e18;
int n, m, a[N], prv[N];
set < int > q, qq;
void add (int i) {
q.insert(i);
qq.erase(a[i]);
}
void del (int i) {
q.erase(i);
qq.insert(a[i]);
}
main () {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 0; i < N; ++i)
qq.insert(i);
int ans = inf;
memset(prv, -1, sizeof(prv));
for (int i = 1; i <= n; ++i) {
if (q.count(prv[a[i]]))
del(prv[a[i]]);
prv[a[i]] = i;
add(i);
while (sz(q) && *q.begin() < i - m + 1)
del(*q.begin());
if (i >= m)
ans = min(ans, *qq.begin());
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define iceil(a, b) (((a) + (b) - 1) / (b))
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
int main(){
StarBurstStream
int n, m;
cin >> n >> m;
vector<vector<int>> pos(n + 1);
for(int i = 1; i <= n; i++){
int a;
cin >> a;
pos[a].eb(i);
}
for(int i = 0; i <= n; i++){
int lst = 0;
pos[i].eb(n + 1);
for(int j : pos[i]){
if(j - lst - 1 >= m){
cout << i << "\n";
return 0;
}
lst = j;
}
}
return 0;
} |
/******************************************************/
/******************************************************/
/** **/
/** BISMILLAHIR RAHMANIR RAHIM **/
/** REAZ AHAMMED CHOWDHURY - reaziii **/
/** Department of Computer Science and Engineering **/
/* INSTITUTE OF SCIENCE AND TECHNOLOGY **/
/** **/
/******************************************************/
/******************************************************/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pcase(x) printf("Case %d: ",x++)
#define fri(f) for(int i=0;i<f;i++)
#define frj(f) for(int j=0;j<f;j++)
#define reset(x) memset(x,-1,sizeof(x))
#define all(x) x.begin(),x.end()
#define input freopen("input.txt","r",stdin);
#define output freopen("output.txt","w",stdout)
#define infi INT_MAX
#define linfi LLONG_MAX
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mgraph map<int,vector<int> >
#define pb push_back
#define clr(x) memset(x,0,sizeof(x))
#define fro(i,x,y) for(int i=x;i<y;i++)
#define ech(x,a) for(auto &x : a)
#define ff first
#define ss second
#define vi vector<int>
#define vl vector<ll>
#define pi acos(-1.0)
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type,
less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
Ordered_set;
template<class T> void read(T& x) {cin >> x;}
template<class H, class... T> void read(H& h, T&... t) {read(h); read(t...);}
template<class A> void read(vector<A>& x) {for (auto &a : x) read(a);}
template<class H> void print(vector<H> &x) {ech(a, x) cout << a << " "; cout << endl;}
template<class P> void debug(P h) {
#ifndef ONLINE_JUDGE
cerr << h << " ";
#endif
}
template<class W, class... V> void debug(W h, V... t) {
#ifndef ONLINE_JUDGE
debug(h);
debug(t...);
cerr << endl;
#endif
}
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
bool checkbitt(ll num, int pos) {return (num >> pos) & 1;}
ll setbitt(ll num, ll pos) {return (1 << pos) | num;}
ll resetbitt(ll num, int pos) {if (!checkbitt(num, pos)) return num; else return (1 << pos)^num;}
ll bigmod(ll a, ll b, ll mod) {if (b == 0) return 1; if (b == 1) return a; if (b & 1) {return ((a % mod) * (bigmod(a, b - 1, mod) % mod)) % mod;} ll x = bigmod(a, b / 2, mod); return (x * x) % mod;}
ll geti() {ll x; read(x); return x;}
ll cdiv(ll a, ll b) {ll ret = a / b; if (a % b) ret++; return ret;}
const ll mod = 1e9 + 7;
const ll N = 2e5 + 10;
int dx[4] = { +0, +0, +1, -1};
int dy[4] = { -1, +1, +0, +0};
//................................___Start from here___...............................//
//................................_____________________..............................//
int solve() {
ll a, b, c, d;
read(a, b, c, d);
b *= a, c *= a;
if (d >= b && d <= c) {
cout << "No" << endl;
}
else cout << "Yes" << endl;
return 0;
}
int main(int argc, char* argv[]) {
if (argc <= 1) {
#ifndef ONLINE_JUDGE
input;
output;
#endif
#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(0);
#endif
}
int cs = 1, cn = 1;
// read(cs);
while (cs--) {
solve();
}
} |
#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<vector>
#include<string>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin>>s;
if(s[0] == s[1] && s[0] == s[2] && s[1]==s[2]){
cout<<"Won";
}else{
cout<<"Lost";
}
return 0;
}
|
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return 1;
}
return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __Setup;
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
void Case(int i) { cout << "Case #" << i << ": "; }
#pragma endregion Macros
using D = long double;
int main() {
int N;
cin >> N;
vector<ll> a(N), sum(N+1, 0);
REP(i, N) cin >> a[i];
sort(ALL(a));
REP(i, N) sum[i+1] = sum[i] + a[i];
D ans = LLINF;
REP(i, N) {
// min(A_i, 2x)のsum
D sum1 = sum[i+1] + a[i] * (N - i - 1);
// xのsum
D sum2 = a[i] * N;
sum2 /= D(2);
D sum3 = sum[N];
chmin(ans, ((sum2 - sum1) + sum3) / N);
}
cout << ans << endl;
} | #include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
using ll = long long;
constexpr int N = 1e6 + 5;
int n;
int a[N];
ll sum[N];
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; ++i) {
sum[i] = sum[i - 1] + a[i];
}
double ret = 1e18;
for(int i = 1; i <= n; ++i) {
double x = a[i] / 2.0;
double tt = 1.0 * sum[n] / n + x;
tt -= (sum[i] + (n - i) * x * 2) / n;
ret = min(ret, tt);
}
printf("%.8lf", ret);
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); ++i)
#define fixed_setprecision(n) fixed << setprecision((n))
#define execution_time(ti) printf("Execution Time: %.4lf sec\n", 1.0 * (clock() - ti) / CLOCKS_PER_SEC);
#define pai 3.1415926535897932384
#define NUM_MAX 2e18
#define NUM_MIN -1e9
using namespace std;
using ll = long long;
using P = pair<int,int>;
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; }
//組み合わせの数nCrを計算
int calcNumOfCombination(int n, int r){
int num = 1;
for(int i = 1; i <= r; i++){
num = num * (n - i + 1) / i;
}
return num;
}
set<string> se;
string str;
void dfs(string s, int c){
if(s.length() >= 4){
se.insert(s);
return;
}
if(str.length() <= c){
return;
}
rep(i, str.length()){
if(str[i] == 'o' || str[i] == '?'){
dfs(s + to_string(i), c+1);
}
}
}
int main() {
cin >> str;
dfs("", 0);
string t;
rep(i, str.length()){
if(str[i] == 'o') t += to_string(i);
}
ll res=0;
for(set<string>::iterator itr = se.begin(); itr != se.end(); itr++){
ll cnt=0;
rep(i, t.length()){
if(itr->find(t[i]) == std::string::npos) break;
cnt++;
}
if(cnt==t.length()) res++;
}
cout << res << endl;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#define endl "\n"
using ll = long long;
const int MOD = 1e9 + 7;
void solve() {
int n; cin >> n;
double ans = 0.0;
for (int i = 1; i < n; i++) {
ans += double(n) / double(i);
}
cout << setprecision(10) << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
} |
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author Kein Yukiyoshi
*/
// clang-format off
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
#define Endl endl
#define itn int
#define fi first
#define se second
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++)
#define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--)
#define FOR(i, a) for (auto &i: a)
#define ALL(obj) begin(obj), end(obj)
#define _max(x) *max_element(ALL(x))
#define _min(x) *min_element(ALL(x))
#define _sum(x) accumulate(ALL(x), 0LL)
#define LOWER_BOUND(A, key) distance(begin(A), lower_bound(ALL(A), key))
#define UPPER_BOUND(A, key) distance(begin(A), upper_bound(ALL(A), key))
const int MOD = 1000000007;
// const int MOD = 998244353;
const int INF = (int)(1e13 + 7);
const double EPS = 1e-14;
const double PI = 3.14159265358979;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T> using VVV = vector<vector<vector<T>>>;
template <class T, class S> using P = pair<T, S>;
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;}
int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); }
int _mod(int &a) {a = a >= 0 ? a % MOD : a - (MOD * _ceil(a, MOD));return a;}
int _pow(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;}
// clang-format on
char zyanken(char a, char b) {
if (a == b) {
return a;
}
if (a > b) swap(a, b);
if (a == 'P' and b == 'R') {
return a;
} else if (a == 'P' and b == 'S') {
return b;
} else {
return a;
}
}
class CLargeRPSTournament {
public:
static void solve(istream &cin, ostream &cout) {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
int n, k;
cin >> n >> k;
string s;
cin >> s;
while (1) {
if (k == 0) {
cout << s[0] << endl;
return;
}
if (k < 10 and _pow(2, k) <= n) {
int m = _pow(2, k);
V<char> list(m);
rep(i, m) list[i] = s[i];
while (list.size() > 1) {
V<char> list2;
rep(i, list.size() / 2) {
list2.emplace_back(zyanken(list[2 * i], list[2 * i + 1]));
}
list = list2;
}
cout << list[0] << endl;
return;
}
if (n % 2 == 0) {
string list2;
rep(i, n / 2) {
list2 += (zyanken(s[(2 * i)%n], s[(2 * i + 1)%n]));
}
n = list2.size();
k--;
s = list2;
} else {
string list2;
rep(i, n ) {
list2+=(zyanken(s[(2 * i)%n], s[(2 * i + 1)%n]));
}
n = list2.size();
k--;
s = list2;
}
}
}
};
signed main() {
CLargeRPSTournament solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| #include <bits/stdc++.h>
#define loop(n) for(ll i=0;i<n;i++)
#define loopj(n) for(ll j=0;j<n;j++)
#define loopR(i,a,b,c) for(ll i=a;i>=b;i-=c)
#define rng(i,a,b,c) for(ll i = a; i<b;i+=c)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define all(s) s.begin(),s.end()
ll mod=10e9+7;
using namespace std;
//--------------------------\\
//code once think twice
void go()
{
ll a,b,v;
cin>>a>>b>>v;
ll x=a*a+b*b;
if(x<(v*v))
cout<<"Yes"<<'\n';
else
cout<<"No"<<'\n';
}
int main()
{
ll t;
fast;
// cin>>t;
//
// loop(t)
go();
}
|
#include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLMAX = (1llu<<(sizeof(ll)*8-1)) - 1;
const int IMAX = (1llu<<(sizeof(int)*8-1)) - 1;
double sx, sy, gx, gy;
int main(){
cin >> sx >> sy >> gx >> gy;
double d = abs((gx - sx) * gy / (sy + gy));
if(sx <= gx){
cout << setprecision(20) << gx - d << endl;
} else {
cout << setprecision(20) << gx + d << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
double Sx, Sy, Gx, Gy;
cin >> Sx >> Sy >> Gx >> Gy;
double x;
x = (10000000000 * Sy * (Gx - Sx) / (Sy + Gy) + 10000000000 * Sx) /10000000000;
cout << std::fixed << std::setprecision(10) << x << endl;
} |
# include <iostream>
# include <vector>
using namespace std;
int main()
{
string s;
cin>>s;
long long t;
char p;
t=0;
p='0';
long long k;
k=0;
vector<char>v;
for(int i=s.size()-2; i>0; i--)
{
if(s[i]==s[i-1] && s[i]!=s[i+1])
{
if(s[i]==p)
{
int h;
h=s.size()-(i+1)-(k+2);
t=t+max(h,0);
k=s.size()-(i+1);
}
else
{
t=t+s.size()-(i+1);
k=s.size()-(i+1);
p=s[i];
}
v.push_back(s[i]);
}
}
char l;
int r;
l='0';
long long w=v.size();
r=0;
int g;
for(int i=0; i<s.size()-2; i++)
{
if(s[i]==s[i+1] && s[i]!=s[i+2])
{
l=v[v.size()-1];
v.pop_back();
i=i+1;
g=i;
}
else
{
if(s[i]==l)
{
r=r+1;
}
}
}
if(g!=s.size()-2)
{
if(s[s.size()-1]==l)
{
r=r+1;
}
if(s[s.size()-2]==l)
{
r=r+1;
}
}
// cout<<r<<endl;
long long z;
z=t-r;
cout<<z;
} | #include<bits/stdc++.h>
#define endl "\n"
#define pb push_back
#define ll long long int
#define f first
#define s second
using namespace std;
ll mod=1e9+7;
int decimal(int x)
{
int f=0;
while(x){if(x%10==7)f=1;x/=10;}
return f;
}
int octal(int x)
{
vector<int>oc;
while(x){oc.pb(x%8);x/=8;}
int f=0;
for(auto i:oc)if(i==7)f=1;
return f;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
int ans=0;
for(int i=1;i<=n;i++)
{
if(decimal(i)||octal(i))ans++;
}
cout<<n-ans<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
int n;cin>>n;
vector<int>arr(n);
for(int i=0;i<n;i++)
cin>>arr[i];
sort(arr.begin(),arr.end());
vector<int>pref(n+1,0);
for(int i=1;i<=n;i++)
pref[i]=pref[i-1]+arr[i-1];
int ans=0;
for(int i=1;i<=n;i++)
{
ans+=pref[n]-pref[i]-(n-i)*arr[i-1];
}
cout<<ans<<endl;
} | #include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long int
#define ll long long
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define mll unordered_map<ll,ll>
#define dec(v) sort(v.rbegin(),v.rend());
#define REP(i, n) for (int i = 0; i < (n); i++)
#define in(v) for(auto &i:v) cin>>i;
#define out(v) for(auto i:v) cout<<i<<" ";
#define nl cout<<endl;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<ll> vll;
#define deb(x) cout << ">" << #x << '=' << x << endl;
#define setbits(n) __builtin_popcountll(n)
#define done {cout<<"-1"<<endl; return;}
#define inf INT_MAX
#define ninf INT_MIN
const ll N = 2e5 + 10;
#define mod 1000000007
vector<int> adj[N];
vector<int> vis(N+1);
int c;
void dfs(int n){
vis[n]=1;
c++;
for(auto i:adj[n]){
if(!vis[i]){
dfs(i);
}
}
}
void solveit(){
int n;
cin>>n;
vi v(n);
in(v);
vpii edg;
for(int i=0;i<n/2;i++){
if(v[i]!=v[n-i-1]){
adj[v[i]].pb(v[n-i-1]);
adj[v[n-i-1]].pb(v[i]);
}
}
int ans=0;
for(int i=0;i<=2e5;i++){
if(!vis[i]){
c=0;
dfs(i);
ans+=c-1;
}
}
cout<<ans<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int testcases =1;
//cin >> testcases;
while (testcases--) solveit();
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
} |
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#include <bits/stdc++.h>
#ifdef __LOCAL
#include <debug/debugger.h>
#endif
#include <ext/pb_ds/assoc_container.hpp>
#define PB push_back
#define PI acos(-1)
#define all(x) x.begin(),x.end()
#define vi vector<ll>
#define ii pair<ll,ll>
#define vii vector<ii>
#define vb vector<bool>
#define vull vector<ull>
#define vvi vector<vector<ll> >
#define vvii vector<vii>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define read(x) long long x;cin>>x;
#define FOR(a,b,c,d) for(int a=b;a<=c;a+=d)
#define MP make_pair
#define F first
#define AS assign
#define S second
#define debug(x) cout<<(#x)<<": "<<(x)<<"\n"
#define SZ(x) x.size()
#define mod 1000000007
#define lg2(x) 63-(__builtin_ctzll(x))
#define RZ resize
#define IN insert
#define X real()
#define Y imag()
#define INF (((1LL<<62LL)-1LL)*2)-1LL
#define FBO find_by_order
#define OFK order_of_key
#define INPUT(x) freopen(x,"r",stdin)
#define OUTPUT(x) freopen(x,"w",stdout)
#define EPS 1e-9
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
typedef unsigned long long ull;
typedef long double d64;
typedef complex<ll>P;
ll modpow(ll x,ll n,ll m) {if(n==0)return 1%m;long long u = modpow(x,n/2,m);u = (u*u)%m;if(n%2==1)u=(u*x)%m;return u;}
ll n;
vector<vector<ll> >G;
vi color;
vb mk;
vi coun;
void dfs(int x,int e){
if(coun[color[x]]==0){
mk[x]=1;
}
coun[color[x]]++;
for(auto c:G[x]){
if(c!=e){
dfs(c,x);
}
}
coun[color[x]]--;
}
int main(){
fast
cin>>n;
G.AS(n+5,vector<ll>());
color.RZ(n+5);
mk.RZ(n+5);
coun.RZ(100040);
for(int i=1;i<=n;i++){
cin>>color[i];
}
for(ll a,b,i=1;i<=n-1;i++){
cin>>a>>b;
G[a].PB(b);
G[b].PB(a);
}
dfs(1,1);
for(int i=1;i<=n;i++){
if(mk[i])cout<<i<<"\n";
}
return 0;
} | #include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair
typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;
const int N = 100005;
int C[N];
vi g[N];
int cols[N];
vi ans;
void dfs(int c, int p) {
if(cols[C[c]] == 0) {
ans.pb(c);
}
cols[C[c]]++;
rep(i,0,g[c].size()) {
int u = g[c][i];
if(u != p) {
dfs(u, c);
}
}
cols[C[c]]--;
}
int main() {
int n;
S(n);
rep(i,1,n+1) {
S(C[i]);
}
rep(i,0,n-1) {
int u,v;
S2(u,v);
g[u].pb(v);
g[v].pb(u);
}
dfs(1, -1);
sort(all(ans));
rep(i,0,ans.size()) {
P(ans[i]);
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
const int MAX_N = 200100;
int par[MAX_N]; // 親
int rank_[MAX_N]; // 木の深さ
int cnt_[MAX_N]; // 属する頂点の個数(親のみ正しい)
// n要素で初期化
void UFinit(){
for(int i=0;i<MAX_N;i++){
par[i] = i;
rank_[i] = 0;
cnt_[i] = 1;
}
}
// 木の根を求める
int find(int x){
if(par[x] == x){
return x;
}else{
return par[x] = find(par[x]);
}
}
// xとyの属する集合を併合
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
if(rank_[x] < rank_[y]){
par[x] = y;
cnt_[y] += cnt_[x];
}else{
par[y] = x;
cnt_[x] += cnt_[y];
if(rank_[x] == rank_[y]) rank_[x]++;
}
}
// xとyが同じ集合に属するか否か
bool same(int x, int y){
return find(x) == find(y);
}
ll mod_pow(ll x, ll n, ll mod){ // x ^ n % mod
ll res = 1;
while(n > 0){
if (n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
int main() {
UFinit();
int n;
cin >> n;
int f[n] = {};
rep(i,n){
cin >> f[i];
f[i]--;
unite(i,f[i]);
}
ll cnt = 0;
rep(i,n){
if(find(i) == i) cnt++;
}
const ll MOD = 998244353;
ll ans = mod_pow(2,cnt,MOD) + MOD - 1;
ans %= MOD;
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <random>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
const int mod = 998244353;
int n;
int w[100];
int fact[101];
int dp[101][101][5101];
signed main() {
int i, j, k;
cin >> n;
rep(i, n) cin >> w[i];
fact[0] = 1;
for (i = 1; i <= n; i++) {
fact[i] = fact[i - 1] * i % mod;
}
int sumW = 0;
rep(i, n) sumW += w[i];
if (sumW % 2 == 1) {
cout << 0 << endl;
return 0;
}
dp[0][0][0] = 1;
rep(i, n) {
for (j = 0; j <= i; j++) {
rep(k, 5001) {
dp[i + 1][j][k] += dp[i][j][k];
dp[i + 1][j][k] %= mod;
dp[i + 1][j + 1][k + w[i]] += dp[i][j][k];
dp[i + 1][j + 1][k + w[i]] %= mod;
}
}
}
int ans = 0;
for (i = 1; i < n; i++) {
int res = dp[n][i][sumW / 2];
ans += res * fact[i] % mod * fact[n - i] % mod;
ans %= mod;
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = int (1e9) + int (1e5);
const ll INFL = ll(2e18) + ll(1e10);
const ui MOD = 1E9 + 7;
const double EPS = 1e-9;
#define FOR(i,n) for (int i=0;i<(n);++i)
#define ROF(i,x) for(int i = (x) ; i >= 0 ; --i)
#define MP make_pair
#define all(a) (a).begin(),(a).end()
#define ODD(x) ( ((x)&1)==0?0:1 )
#define SIGN(x) ( ((x) > 0) - ((x) < 0) )
#define dbg(x) cerr << #x"= " << x << endl
std::mt19937_64 generator(std::chrono::system_clock::now().time_since_epoch().count());
inline ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a, ll b) { return a ? gcd(b%a, a): b; }
ll lcm(ll a, ll b) { return a / gcd(a,b) * b; }
void READ(bool _local){
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _DEBUG
if (_local)
freopen ("in.txt", "r", stdin);
#endif
}
int main() {
READ(0);
int n;cin>>n;
vi a(n);
FOR(i,n) cin>>a[i];
sort(all(a));
ll ret=a[0]+1;
for(int i=1;i<n;++i){
ret = ret * (a[i]-a[i-1]+1) % MOD;
}
cout << ret;
return 0;
}
| #include <iostream>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)
long long mod = 1000000007;
long long N, A[1 << 18], Q[1 << 18];
long long Answer = 1;
int main() {
cin >> N;
for(int i = 1; i <= N; i++) cin >> A[i];
sort(A + 1, A + N + 1);
for(int i = 1; i <= N; i++) Q[i] = A[i] - A[i - 1];
for(int i = 1; i <= N; i++) {
Answer *= (Q[i] + 1LL);
Answer %= mod;
}
cout << Answer << endl;
return 0;
} |
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 2e5 + 7;
const int MOD = 1e9 + 7;
char s[MAX_N];
int d[MAX_N], dp[MAX_N][17];
int n, k, state;
signed main() {
scanf("%s%lld", s, &k);
n = strlen(s);
for (int i = 0; i < n; ++i) {
if (s[i] >= '0' && s[i] <= '9') d[i] = s[i] - '0';
else d[i] = s[i] - 'A' + 10;
}
for (int i = 0; i < n; ++i) {
for (int j = 1; j <= k; ++j) {
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * j % MOD) % MOD;
dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j] * (16 - j) % MOD) % MOD;
}
if (i) dp[i + 1][1] = (dp[i + 1][1] + 15) % MOD;
for (int j = 0; j < d[i]; ++j) {
int nstate = state;
if (i || j) nstate |= (1 << j);
int st = __builtin_popcount(nstate);
dp[i + 1][st] = (dp[i + 1][st] + 1) % MOD;
}
state |= (1 << d[i]);
}
int st = __builtin_popcount(state);
dp[n][st] = (dp[n][st] + 1) % MOD;
printf("%lld\n", dp[n][k]);
return 0;
} | // atcoder/abc194/F/main.cpp
// author: @___Johniel
// github: https://github.com/johniel/
#include <bits/stdc++.h>
#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))
using namespace std;
template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; for (auto& i: v) os << i << ","; os << ")"; return os; }
template<typename T> istream& operator >> (istream& is, vector<T>& v) { for (auto& i: v) is >> i; return is; }
template<typename T> ostream& operator << (ostream& os, set<T> s) { os << "#{"; for (auto& i: s) os << i << ","; os << "}"; return os; }
template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; for (auto& i: m) os << i << ","; os << "}"; return os; }
template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); }
template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); }
using lli = long long int;
using ull = unsigned long long;
using point = complex<double>;
using str = string;
template<typename T> using vec = vector<T>;
constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1});
constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1});
constexpr lli mod = 1e9 + 7;
int main(int argc, char *argv[])
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.setf(ios_base::fixed);
cout.precision(15);
str s;
int kind;
while (cin >> s >> kind) {
const int N = 2 * 1e5 + 5;
const int M = 18;
static lli dp[N][2][2][M];
fill(&dp[0][0][0][0], &dp[N - 1][2 - 1][2 - 1][M - 1] + 1, 0);
dp[0][false][true][0] = 1;
map<char, int> m;
str t = "0123456789ABCDEF";
for (int i = 0; i < t.size(); ++i) {
m[t[i]] = i;
}
set<char> vis;
for (int i = 0; i < s.size(); ++i) {
for (int less = 0; less < 2; ++less) {
for (int prefix = 0; prefix < 2; ++prefix) {
for (int k = 0; k <= kind; ++k) {
const lli curr = dp[i][less][prefix][k];
if (curr == 0) continue;
if (less) {
(dp[i + 1][less][prefix][k] += curr * k) %= mod;
unless (prefix) {
(dp[i + 1][less][prefix][k + 1] += curr * (16 - k)) %= mod;
} else {
(dp[i + 1][less][true][k] += curr) %= mod;
(dp[i + 1][less][false][k + 1] += curr * (15 - k)) %= mod;
}
} else {
for (int p = 0; p < m[s[i]]; ++p) {
if (i == 0 && p == 0) {
(dp[i + 1][true][true][0] += curr) %= mod;
} else {
(dp[i + 1][true][false][vis.size() + 1 - vis.count(t[p])] += curr) %= mod;
}
}
}
}
}
}
const int prev = vis.size();
vis.insert(s[i]);
if (i) {
dp[i + 1][false][false][vis.size()] += dp[i][false][false][prev];
} else {
dp[i + 1][false][false][vis.size()] += dp[i][false][true][prev];
}
}
lli x = 0;
x += dp[s.size()][true][false][kind];
x += dp[s.size()][false][false][kind];
cout << x % mod << endl;
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=2500+5;
int a[N];
vector<int> b;
int vis[10005];
int main()
{
int n;
cin>>n;
for(int i=2;i*6<=10000;i++)
b.push_back(i*6),vis[i*6]=1;
for(int i=2;i*10<=10000;i++)
if(!vis[i*10]) b.push_back(i*10),vis[i*10]=1;
for(int i=2;i*15<=10000;i++)
if(!vis[i*15]) b.push_back(i*15),vis[i*15]=1;
a[1]=6,a[2]=10,a[3]=15;
for(int i=4;i<=n;i++)
a[i]=b.back(),b.pop_back();
for(int i=1;i<=n;i++) printf("%d ",a[i]);
return 0;
} | #include "bits/stdc++.h"
#include <chrono>
#include <random>
#define lli long long int
using namespace std;
#define mod 1000000007
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define INF 1000000000
#define common cout << "Case #" << w+1 << ": "
#define maxn 10010
void setIO(string name) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
template< typename T>
void PVecPrint(vector<T>&v)
{
for(int i=0;i<(int)v.size();i++)
cout << v[i].first << "," << v[i].second << ' ';
cout << '\n';
}
template<class T>
void VecPrint(vector<T>&v)
{
for(int i=0;i<v.size();i++)
cout << v[i] << ' ';
cout << '\n';
}
/*-------------------------------------------------------------------Code-----------------------------------------------------------------*/
int main()
{
int n;
cin >> n;
n-=3;
vector<int>v{6,10,15};
int t=12;
set<int>s;
while (t<10000 and n>0)
{
v.push_back(t);
s.insert(t);
n-=1;
t+=6;
}
// cout << n << ' ' << s.size() << ' ' << v.size() << '\n';
t=20;
while (t<10000 and n>0)
{
if(s.find(t)==s.end())
{
v.push_back(t);
s.insert(t);
n-=1;
}
t+=10;
}
// cout << n << ' ' << s.size() << ' ' << v.size() << '\n';
t=30;
while (t<10000 and n>0)
{
if(s.find(t)==s.end())
{
v.push_back(t);
s.insert(t);
n-=1;
}
t+=15;
}
// cout << s.size() << ' ' << v.size() << '\n';
// cout << n << '\n';
VecPrint<int>(v);
} |
// created: 16.02.2021 03:26:23
#include <iostream>
#include <numeric>
#include <cmath>
#include<vector>
using namespace std;
#define REP(i,n) for(int i=0;i<int(n);i++)
#define REPD(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=int(b);i++)
#define FORD(i,a,b) for(int i=a;i>=int(b);i--)
int decimalNumber(int n){
int tmp = 0;
while (n)
{
tmp = n % 10;
n = n / 10;
if(tmp == 7){
return false;
}
}
return true;
}
int eightBase(int n){
int tmp = 0;
while (n)
{
tmp = n % 8;
n = n / 8;
if(tmp == 7){
return false;
}
}
return true;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N;
int ans=0;
cin >> N;
FOR(i,1,N){
if (decimalNumber(i)){
if (eightBase(i))ans++;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define RREP_(i, a_, b_, a, b, ...) for (Int i = Int(b) - 1, low##i = (a); i >= low##i; i--)
#define RREP(i, ...) RREP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on
struct in {
template <class T> operator T() {
T t;
std::cin >> t;
return t;
}
};
void out() { std::cout << "\n"; }
template <class Head, class... Tail> void out(Head&& h, Tail&&... t) {
std::cout << h << (sizeof...(Tail) == 0 ? "" : " "), out(std::forward<Tail>(t)...);
}
template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; }
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; }
template <class T> using V = std::vector<T>;
/**
* author: knshnb
* created: Sat Dec 19 21:02:02 JST 2020
**/
signed main() {
Int n = in();
Int ans = 0;
REP(x, 1, n + 1) {
bool ok = true;
for (Int d : {10, 8}) {
Int y = x;
while (y) {
if (y % d == 7) ok = false;
y /= d;
}
}
ans += ok;
}
out(ans);
}
|
#include <bits/stdc++.h>
#define LOCAL
#define mkp make_pair
#define ft first
#define sd second
using namespace std;
typedef long long ll;
typedef long double lld;
typedef pair<ll,ll> pll;
const lld pi = 3.14159265358979323846;
const ll maxn=1e5+10;
const ll mod=998244353;
void init(void);
ll pow_mod(ll a,ll k);
void solve() {
ll n,num=0,i=1;
cin >> n;
n*=2;
while(1){
ll x=n-i*(i-1);
if(x<=0)break;
if(x%(2*i)==0)num++;
i++;
}
cout << num*2;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//init();
ll T=1;
//cin >> T;
while(T--){
solve();
}
return 0;
}
void init (void)
{
#ifdef LOCAL
freopen ("data.txt", "r", stdin);
freopen ("out.txt", "w", stdout);
#endif
}
ll pow_mod(ll a,ll k)
{
ll ans = 1;
a %= mod;
while(k)
{
if(k % 2) ans *= a;
a = (a * a) % mod;
k /= 2;
ans %= mod;
}
return ans;
} | #include<bits/stdc++.h>
using namespace std;
#define local
#ifdef local
template<class T>
void _E(T x) { cerr << x; }
void _E(double x) { cerr << fixed << setprecision(6) << x; }
void _E(string s) { cerr << "\"" << s << "\""; }
template<class A, class B>
void _E(pair<A, B> x) {
cerr << '(';
_E(x.first);
cerr << ", ";
_E(x.second);
cerr << ")";
}
template<class T>
void _E(vector<T> x) {
cerr << "[";
for (auto it = x.begin(); it != x.end(); ++it) {
if (it != x.begin()) cerr << ", ";
_E(*it);
}
cerr << "]";
}
template<class T>
void _E(deque<T> x) {
cerr << "[";
for (auto it = x.begin(); it != x.end(); ++it) {
if (it != x.begin()) cerr << ", ";
_E(*it);
}
cerr << "]";
}
void ERR() {}
template<class A, class... B>
void ERR(A x, B... y) {
_E(x);
cerr << (sizeof...(y) ? ", " : " ");
ERR(y...);
}
#define debug(x...) do { cerr << "{ "#x" } -> { "; ERR(x); cerr << "}" << endl; } while(false)
#else
#define debug(...) 114514.1919810
#endif
using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3f3f
#define ll long long
#define db double
#define eps 1e-3
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
#define endl "\n"
const ll mod = 1e9 + 7;
const ll maxn = 2e6 + 10;
vector<ll> f(maxn), invf(maxn);
ll qpow(ll a, ll b) {
ll ret = 1;
for (; b; a = a * a % mod, b >>= 1) {
if (b & 1) {
ret = ret * a % mod;
}
}
return ret;
}
void prework() {
f[0] = 1;
_rep(i, 1, maxn - 1) {
f[i] = f[i - 1] * i % mod;
}
invf[maxn - 1] = qpow(f[maxn - 1], mod - 2);
for (ll i = maxn - 2; i >= 0; i--) {
invf[i] = invf[i + 1] * (i + 1) % mod;
}
}
ll C(ll n, ll m) {
if (n > m || m < 0)return 0;
if (n == 0 || m == n) return 1;
ll res = (f[m] * invf[m - n] % mod * invf[n]) % mod;
return res;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int _;
cin >> _;
while (_--) {
ll x;
cin >> x;
cout << (x & 1 ? "Odd" : (x % 4 == 0 ? "Even" : "Same")) << endl;
}
}
// 2 4 6 5 3 1
// 6 5 4 3 2
// 2 4 5 3 1 6
// 5 4 3 2 1 - 5
// 2 4 3 1 5 6
// 4 3 2 1 1 - 4 |
#include <iostream>
#include <vector>
#include <unordered_set>
int main()
{
long long N;
std::cin >> N;
std::unordered_set<long long> hashset;
for(long long i = 2; (i * i) <= N; i++){
hashset.emplace(i*i);
long long t = i*i;
while(1){
t *= i;
if(t > N){
break;
}
hashset.emplace(t);
}
}
std::cout << N - hashset.size() << std::endl;
return 0;
} | // .-""""-.
// / j \
// :.d; ;
// $P :
// .m._ $$ :
// dSMMSSSss.__$b. __ :
// :MMSMMSSSMMMSS$$b $P ;
// SMMMSMMSMMMSSS$$$$ :b
// dSMMMSMMMMMMSSMM$$b.dP SSb.
// dSMMMMMMMMMMSSMMPT$$=-. /TSSSS.
// :SMMMSMMMMMMMSMMP `b_.' MMMMSS.
// SMMMMMSMMMMMMMMM \ .'\ :SMMMSSS.
// dSMSSMMMSMMMMMMMM \/\_/; .'SSMMMMSSSm
// dSMMMMSMMSMMMMMMMM :.;'" :SSMMMMSSMM;
// .MMSSSSSMSSMMMMMMMM; :.; MMSMMMMSMMM;
// dMSSMMSSSSSSSMMMMMMM; ;.; MMMMMMMSMMM
// :MMMSSSSMMMSSP^TMMMMM ;.; MMMMMMMMMMM
// MMMSMMMMSSSSP `MMMM ;.; :MMMMMMMMM;
// "TMMMMMMMMMM TM; :`.: MMMMMMMMM;
// )MMMMMMM; _/\ :`.: :MMMMMMMM
// dSS$$MMMb. |._\\ :`.: MMMMMMMM
// T$S$$$$$$$$$m;O\\"-;`.:_.- MMMMMMM;
// :$$$$$$$$$$$$$$b_l./\ ;`.: mMMSSMMM;
// :$$$$$$$$$$$$$$$$$$$./\;`.: .$MSMMMMMM
// $$$$$$$$$$$$$$$$$$$$. \`.:.$$$SMSSSMMM;
// $$$$$$$$$$$$$$$$$$$$$. \.:$$$$SSMMMMMMM
// :$$$$$$$$$$$$$$$$$$$$$.//.:$$$SSSSSSSMM;
// :$$$$$$$$$$$$$$$$$$$$$$.`.:$SSSSSSSMMMP
// $$$$$$$$$;"^J "^$$$$;.`.$P `SSSMMMM
// :$$$$$$$$$ :$$$;.`.P'.. TMMM$b
// :$$$$$$$$$; $$$$;.`/ c^' d$$$$S;
// $$$$S$$$$; '^^^:_dg:___.$$$$$SSS
// $$$SS$$$$; $$$$$$$$$$$$$SSS;
// :$$SSSS$$$$ : $$$$$$$$$$$$SSS
// :P"TSSSS$$$ ; $$$$$$$$$$$$SSS;
// j `SSSSS$ : :$$$$$$$$$$$$SS$
// : "^S^' : $$$$$$$$$$$$S$;
// ;.____.-;" "--^$$$$$$$$$$$$P
// '-....-" bug ""^^T$$$P"
#include<bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define ll long long
#define ss second
#define ff first
#define pll pair<ll,ll>
#define vll vector<ll>
#define mll map<ll,ll>
#define mod 1000000007
#define sp " "
#define w(x) ll x; cin>>x; while(x--)
#define ps(x,y) fixed<<setprecision(y)<<x;
#define fo(i, j, k, in) for (ll i=j ; i<k ; i+=in)
#define re(i, j) fo(i, 0, j, 1)
#define pi 3.1415926535897932384626433832795
#define all(cont) cont.begin(), cont.end()
#define countbit(x) __builtin_popcount(x)
#define mod 1000000007
#define lo lower_bound
#define de(n) ll n;cin>>n;
#define def(a,n) ll n;cin>>n;ll a[n];re(i,n){cin>>a[i];}
#define defi(a,n,k) ll n;cin>>n; ll k;cin>>k;ll a[n];re(i,n){cin>>a[i];}
#define deb(x) cout<<#x<<"="<<x<<endl;
#define tr(it,a) for(auto it=a.begin();it!=a.end();it++)
#define nl cout<<endl;
using namespace std;
//KnightMareVoid
const int N=1e6;
bool prime[N+1];
void SieveOfEratosthenes()
{
memset(prime, true, sizeof(prime));
for (int p=2; p*p<=N; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
for (int i=p*p; i<=N; i += p)
prime[i] = false;
}
}
for (int p=2; p<=N; p++)
if (prime[p])
cout << p << " ";
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll n;
cin>>n;
int f=0;
int c=0;
set<ll> s;
for(ll i=2;i*i<=n;i++){
ll j=i*i;
if(s.find(j)!=s.end())continue;
while(j<=n){
s.insert(j);
c++;
j*=i;
}
}
cout<<n-c<<endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
ll c=a%i_7;
if(c>=0)return c;
return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
struct edge{ll to,cost; };
vector<edge> G[500010];
ll d[500010];
void dijkstra(ll s){
fill(d,d+500010,inf);
d[s]=0;
priority_queue<l_l,vector<l_l>,greater<l_l>> que;
que.push(l_l(0,s));
while(!que.empty()){
l_l p=que.top();que.pop();
ll v=p.second;
if(d[v]<p.first)continue;
if(G[v].size()>0){
rep(i,0,G[v].size()-1){
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(l_l(d[e.to],e.to));
}
}
}
}
}
int main(){fastio
ll n,m;cin>>n>>m;
ll a[m],b[m];
rep(i,0,m-1){
cin>>a[i]>>b[i];
edge e;
e.to=b[i];e.cost=1;
G[a[i]].pb(e);
e.to=a[i];e.cost=1;
G[b[i]].pb(e);
}
ll k;cin>>k;
ll c[k];rep(i,0,k-1)cin>>c[i];
ll l[k][k]; //c[i]からc[j]への最短距離
rep(i,0,k-1){
dijkstra(c[i]);
rep(j,0,k-1){
l[i][j] = d[c[j]];
}
}
/*
rep(i,0,k-1){
rep(j,0,k-1){
cout<<l[i][j]<<' ';
}
cout<<endl;
}
*/
ll L=(1<<k);
ll dp[L][k];rep(i,0,L-1)rep(j,0,k-1)dp[i][j]=inf;
rep(i,0,k-1){
dp[(1<<i)][i]=0;
}
rep(i,0,L-1){
rep(j,0,k-1){
if(!((i>>j)&1)){
continue;
}
ll M=i-(1<<j);
rep(ii,0,k-1){
if((M>>ii)&1){
dp[i][j] = min(dp[i][j],dp[M][ii]+l[ii][j]);
}
}
}
}
ll ans=inf;
rep(i,0,k-1){
ans=min(ans,dp[L-1][i]);
}
if(ans>=inf){
cout<<-1<<endl;
}else{
cout<<ans+1<<endl;
}
return 0;
}
| /*************************************************************************
> File Name: solve.cpp
> Author: liupo
> Mail: [email protected]
> Created Time: 2021-02-28 21:37:24
************************************************************************/
#define GOODOJ
#define SYNC 0
#ifdef GOODOJ
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#include <chrono>
#include <random>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#else
#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
#include <limits>
#include <cassert>
#include <sstream>
#include <iterator>
#include <functional>
#endif
using namespace std;
#define endl '\n'
#define fep(i,b,e) for(int i=(b);i<(e);++i)
#define rep(i,x) for(int i=0;i<(x);++i)
#define rap(i,x) for(auto& i : (x))
#define seg(t) (t).begin(), (t).end()
#define ep emplace_back
#define mkp make_pair
#define qxx(i,x) for(int i = head[x]; ~i; i = node[i].nex)
#define F first
#define S second
#define lowbit(x) ((-(x))&(x))
#define RE register
#define getchar() getchar_unlocked()
#ifdef DEBUG
void err(istream_iterator<string>){}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ' ';
err(++it, args...);
}
#define debug(args...) {string _s=#args;replace(seg(_s),',',' ');\
cerr<<"DEBUG:";istringstream it(_s);\
err(istream_iterator<string>(it), args);cerr<<endl;}
#else
#define debug(...)
#endif
template<typename T> inline bool cmax(T& a,const T& b) {return a<b?a=b,1:0;}
template<typename T> inline bool cmin(T& a,const T& b) {return a>b?a=b,1:0;}
#ifdef GOODOJ
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
typedef __gnu_pbds::priority_queue<int> pq;
#endif
typedef std::string str;
typedef long long ll;
typedef double db;
typedef pair<int, int> pa;
const double P = acos(-1.0), eps = 1e-9;
struct point { db x ,y;};
inline int sign(db a) {return a < -eps ? -1 : a > eps;}
#define dot(p1,p2,p3) ((p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y))
#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))
#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))
constexpr int Ma = 1e6, inf = 0x3f3f3f3f, mod = 1e9 + 7;
int who[200];
void solve() {
str s[3]; rep (i, 3) cin >> s[i];
set<int> cnt;
rep (i, 3) rap (j, s[i]) cnt.emplace(j);
if (cnt.size() > 10) {
cout << "UNSOLVABLE" << endl;
return ;
}
vector<int> id(10);
iota(seg(id), 0);
int ct = 0;
rap (i, cnt) who[i] = ct++;
do {
bool show = true;
rep (i, 3)
if (id[who[s[i][0]]] == 0) {
show = false;
break;
}
if (not show) continue;
ll num[3] = {0, 0, 0};
rep (i, 3) {
rap (j, s[i]) num[i] *= 10, num[i] += id[who[j]];
}
if (num[0] + num[1] == num[2]) {
cout << num[0] << endl << num[1] << endl << num[2] << endl;
return ;
}
} while (next_permutation(seg(id)));
cout << "UNSOLVABLE" << endl;
}
signed main() {
#if SYNC==0
ios::sync_with_stdio(false);
cin.tie(0);
#endif
int T = 1;
while (T--) solve();
return 0;
} |
#include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,cc,n) for(int i=cc;i<(n);++i)
#define dreps(i,cc,n) for(int i=cc;i>(n);--i)
#define llreps(i,cc,n) for(long long i=cc;i<(n);++i)
#define ALL(v) begin(v),end(v)
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
int main()
{
int a,b,c;
cin >> a >> b >> c;
if (a*a + b * b < c*c)
{
cout << "Yes" << endl;
return 0;
}
cout << "No" << endl;
return 0;
} | #include <iostream>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
if (a==b){
cout<<c;
return 0;
}
if (a==c){
cout<<b;
return 0;
}
if (c==b){
cout<<a;
return 0;
}
cout<<0;
}
|
// ABC183C
#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <string>
#include <regex>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <vector>
#include <set>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, n, m) for(int i = (int)(n); i < (int)(m); i++)
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
using ll = long long;
using namespace std;
int main()
{
int V, T, S, D;
cin >> V >> T >> S >> D;
if ((D) == (T * V)) {
cout << "No" << endl;
} else if (((D) > (T * V)) && ((D) < (S * V))) {
cout << "No" << endl;
} else if ((D) == (S * V)) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
return 0;
} | #include <iostream>
#include<bits/stdc++.h>
#include <string>
using namespace std;
int main(void){
int h,w,x,y;
std::cin >> h >> w >> x >> y;
x -= 1;
y -= 1;
string s[h];
for (int i= 0;i<h;i++){
std::cin >> s[i];
}
int cnt = -3;
// down
for (int i = x; i < h && s[i][y] != '#'; i++){
cnt++;
}
// up
for (int i = x; i >= 0 && s[i][y] != '#'; i--){
cnt++;
}
// right
for (int i = y; i < w && s[x][i] != '#'; i++){
cnt++;
}
// left
for (int i = y; i >= 0 && s[x][i] != '#'; i--){
cnt++;
}
std::cout << cnt << std::endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
//A
/*int main(){
int n; cin >> n;
int x=n%100;
if(n==0) { cout << 100 << endl; return 0; }
cout << 100-x << endl;
return 0;
} */
//B
/*int main(){
string s; cin >> s;
bool c=1;
for(int i=0; 2*i<s.size(); i++){
if(s[2*i] >='a' && s[2*i]<='z' ) c=1;
else { cout << "No"<< endl; return 0; }
}
for(int i=0; 2*i+1<s.size(); i++){
if(s[2*i+1] >='A' && s[2*i+1]<='Z' ) c=1;
else { cout << "No" << endl; return 0;}
}
cout << "Yes" << endl;
return 0;
} */
//C
string convert (ll n){
ostringstream buff;
buff<<n;
return buff.str();
}
string f(string x){
vector <char> v;
int a=x.size();
for(int i=0; i<a; i++) v.push_back(x[i]);
ll g1=0,g2=0;
sort(v.begin(),v.end());
for(int i=0; i<a; i++) g2+=(v[i]*pow(10,a-i-1));
sort(v.rbegin(),v.rend());
for(int i=0; i<a; i++) {
if(v[i] != 0 ) g1+=( v[i]*pow(10,a-i-1));
else { g1+=pow(10,a-i); break; }
}
return convert(g1-g2);
}
int main(){
string N; cin >> N;
int K; cin >> K;
string res=N;
for(int i=1; i<=K; i++) res=f(res);
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n,k;
cin>>n>>k;
for(int i=0;i<k;i++){
string s1=to_string(n);
string s2=to_string(n);
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end(),greater<char>());
int tmp1=stoi(s1);
int tmp2=stoi(s2);
n=tmp2-tmp1;
}
cout<<n<<endl;
} |
#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 = 200005;
ll sum[maxn << 2];
ll lzd[maxn << 2];
ll lzc[maxn << 2];
vector<pair<ll,ll>>v;
void pushup(int x) {
sum[x] = sum[x<<1] + sum[x<<1|1];
}
void pushdown(int x, int l, int r) {
int mid = (l + r) >> 1;
if (lzc[x]) {
lzc[x<<1] = lzc[x<<1|1] = lzc[x];
lzc[x] = 0;
lzd[x<<1] = lzd[x<<1|1] = 0;
sum[x<<1] = (mid - l + 1) * lzc[x<<1];
sum[x<<1|1] = (r - mid) * lzc[x<<1|1];
}
if (lzd[x]) {
lzd[x<<1] += lzd[x];
lzd[x<<1|1] += lzd[x];
sum[x<<1] += lzd[x] * (mid - l + 1);
sum[x<<1|1] += lzd[x] * (r - mid);
lzd[x] = 0;
}
}
void build(int x, int l, int r) {
if (l == r) {
sum[x] = v[l].F;
return;
}
int mid = (l + r) >> 1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
pushup(x);
}
void update(int x, int l, int r, int ql, int qr, ll v) {
if (ql <= l && qr >= r) {
lzd[x] += v;
sum[x] += v * (r - l + 1);
return;
}
int mid = (l + r) >> 1;
pushdown(x,l,r);
if (ql <= mid) {
update(x<<1,l,mid,ql,qr,v);
}
if (qr > mid) {
update(x<<1|1,mid+1,r,ql,qr,v);
}
pushup(x);
}
void update2(int x, int l, int r, int ql, int qr, ll v) {
if (ql <= l && qr >= r) {
lzc[x] = v;
lzd[x] = 0;
sum[x] = v * (r - l + 1);
return;
}
int mid = (l + r) >> 1;
pushdown(x,l,r);
if (ql <= mid) {
update2(x<<1,l,mid,ql,qr,v);
}
if (qr > mid) {
update2(x<<1|1,mid+1,r,ql,qr,v);
}
pushup(x);
}
ll query(int x, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) {
return sum[x];
}
int mid = (l + r) >> 1;
ll ret = 0;
pushdown(x,l,r);
if (ql <= mid) {
ret += query(x<<1,l,mid,ql,qr);
}
if (qr > mid) {
ret += query(x<<1|1,mid+1,r,ql,qr);
}
pushup(x);
return ret;
}
void solve() {
int m;
cin >> m;
vector<pair<ll,ll>>op;
f(m) {
ll a, b;
cin >> a >> b;
op.pb({a, b});
}
int n;
cin >> n;
f1 (n) {
int a;
cin >> a;
v.pb({a, i});
}
v.pb({-1e10, -1});
sort(all(v));
build(1,1,n);
for (auto &i : op) {
if (i.S == 1) {
update(1,1,n,1, n,i.F);
}
else if (i.S == 2) {
if (query(1,1,n,1,1) >= i.F) {
continue;
}
int l = 1, r = n + 1;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (query(1,1,n,mid,mid) < i.F) {
l = mid;
}
else {
r = mid;
}
}
update2(1,1,n,1,l,i.F);
}
else {
if (query(1,1,n,n,n) <= i.F) {
continue;
}
int l = 0, r = n ;
while (r - l > 1) {
int mid = (l + r) >> 1;
if (query(1,1,n,mid,mid) > i.F) {
r = mid;
}
else {
l = mid;
}
}
update2(1,1,n,r,n,i.F);
}
}
ll ans[n + 5] = {};
f1(n) {
ans[v[i].S] = query(1,1,n,i,i);
}
f1(n) {
cout << ans[i] << '\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--) {
solve();
}
} | #include<bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long n , a [200005] , t [200005] , q , x [200005] , ans [200005] , ad [200005] , anss [200005];
deque < pair < long long , long long > > dq;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
cin >> n;
for ( int i = 0 ; i < n ; i ++ ) cin >> a [i] >> t [i];
cin >> q;
for ( int i = 0 ; i < q ; i ++ )
{
cin >> x [i];
dq . push_back ( { x [i] , i } );
}
sort ( dq . begin () , dq . end () );
long long sum = 0;
multiset < pair < long long , long long > > ms;
for ( int i = 0 ; i < n ; i ++ )
{
if ( t [i] == 1 ) sum += a [i];
if ( t [i] == 2 )
{
long long l = a [i];
while ( ms . size () )
{
pair < long long , long long > p = * ms . begin ();
if ( p . first + sum <= l )
{
ad [ p . second ] = i + 1;
ms . erase ( ms . begin () );
}
else break;
}
ms . insert ( { a [i] - sum , i } );
}
if ( t [i] == 3 )
{
long long r = a [i];
while ( ms . size () )
{
pair < long long , long long > p = * -- ms . end ();
if ( r <= p . first + sum )
{
ad [ p . second ] = i + 1;
ms . erase ( -- ms . end () );
}
else break;
}
ms . insert ( { a [i] - sum , i } );
}
}
sum = 0;
for ( int i = 0 ; i < n ; i ++ )
{
if ( dq . size () == 0 ) break;
if ( t [i] == 1 ) sum += a [i];
if ( t [i] == 2 )
{
long long l = a [i];
while ( dq [0] . first + sum <= l )
{
ans [ dq [0] . second ] = i + 1;
dq . pop_front ();
if ( dq . size () == 0 ) break;
}
}
if ( t [i] == 3 )
{
long long r = a [i];
while ( r <= dq [ dq . size () - 1 ] . first + sum )
{
ans [ dq [ dq . size () - 1 ] . second ] = i + 1;
dq . pop_back ();
if ( dq . size () == 0 ) break;
}
}
}
sum = 0;
for ( int i = n - 1 ; i >= 0 ; i -- )
{
if ( t [i] == 1 ) sum += a [i];
if ( ad [i] == 0 ) anss [i] = a [i] + sum;
else anss [i] = anss [ ad [i] - 1 ];
}
for ( int i = 0 ; i < q ; i ++ )
{
if ( ans [i] ) cout << anss [ ans [i] - 1 ] << endl;
else cout << x [i] + sum << endl;
}
}
|
#include <bits/stdc++.h>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<string> split(const string &s, char delim) {
vector<string> elems;
string item;
for (char ch: s) {
if (ch == delim) {
if (!item.empty()) {
elems.push_back(item);
}
item.clear();
}
else {
item += ch;
}
}
if (!item.empty()) {
elems.push_back(item);
}
return elems;
}
string to_str_with_zero(int i, int w) {
ostringstream sout;
sout << std::setfill('0') << std::setw(w) << i;
string s = sout.str();
return s;
}
int letter_to_int(char c) {
return tolower(c) - 'a';
}
int compare_array(vector<int> a1, vector<int>a2) {
int n1 = a1.size();
int n2 = a2.size();
if (n1 != n2) {
return n1 - n2;
}
for (int i=0; i<n1; i++) {
if (a1.at(i) != a2.at(i)) {
return a1.at(i) - a2.at(i);
}
}
return 0;
}
int gcd(int a, int b) {
if(a % b == 0) {
return b;
}
return gcd(b, a % b);
}
char int_to_char(int a) {
if (a == -1) {
return 'z';
}
else {
return 'a' + a;
}
}
long nCr(int n, int r) {
long ans = 1;
for (int i = n; i > n - r; --i) {
ans = ans*i;
}
for (int i = 1 ; i < r + 1; ++i) {
ans = ans / i;
}
return ans;
}
long modinv(long a, long m) {
long b = m, u = 1, v = 0;
while (b) {
long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
int divide_count(int a, int divider) {
int r = 0;
while(a % divider == 0) {
a /= divider;
r++;
}
return r;
}
bool is_prime(int a) {
int i = 2;
while(i * i <= a) {
if(a % i == 0) {
return false;
}
i++;
}
return true;
}
vector<vector<int>> all_comb(int n, int k) {
vector<vector<int>> combs(nCr(n, k), vector<int>(k));
for(int i=0; i<k; i++) {
combs[0][i] = i;
combs[1][i] = i;
}
for(long i=1; i<nCr(n, k); i++) {
int p = 1;
while(combs[i][k - p] == n - p) {
p++;
if(p > k) {
break;
}
}
combs[i][k - p]++;
int q = combs[i][k - p];
for(int j=1; j<p; j++) {
combs[i][k - p + j] = q + j;
}
if(i < nCr(n, k) - 1) {
for(int j=0; j<k; j++) {
combs[i + 1][j] = combs[i][j];
}
}
}
return combs;
}
struct UnionFind {
vector<int> parent;
UnionFind(int n) : parent(n) {
for(int i=0; i<n; i++) {
parent[i] = i;
}
}
int root(int x) {
if (parent[x] == x) {
return x;
}
return parent[x] = root(parent[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if(rx == ry) return;
parent[rx] = ry;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
template <typename TYPE> void co(TYPE a) {
cout << a << endl;
}
template <typename TYPE> void co_2(TYPE a, TYPE b) {
cout << a << ' ' << b << endl;
}
template <typename TYPE> void co_l(vector<TYPE> as) {
int n = as.size();
for(int i=0; i<n; i++) {
cout << as[i] << endl;
}
}
template <typename TYPE> void co_s(vector<TYPE> as) {
int n = as.size();
for(int i=0; i<n; i++) {
if(i > 0) {
cout << ' ';
}
cout << as[i];
}
cout << endl;
}
int main() {
std::cout << std::setprecision(9);
long mod = 1000000007;
int x;
cin >> x;
if(x >= 0) {
co(x);
}
else {
co(0);
}
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
const long long MOD2 = 998244353LL;
const long long MOD = 1000000007LL;
const long long INF = 1LL<<60;
int main(){
int x;
cin >> x;
if(x >= 0)cout << x << endl;
else cout << 0 << endl;
return 0;
} |
#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <random>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define repp(i,n,m) for (int i = m; i < (n); ++i)
#define repl(i,n) for (long long i = 0; i < (n); ++i)
#define reppl(i,n,m) for (long long i = m; i < (n); ++i)
//#define int long long
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int,int>,int>;
using PL = pair<long long, long long>;
using Pxy = pair<double, double>;
using Tiib = tuple<int, int, bool>;
const int INF = 1001001007;
const int mod = 1000000007;
const int MOD = 998244353;
const ll inf = 1e18;
template <typename AT>
void printvec(vector<AT> &ar){
rep(i,ar.size()-1) cout << ar[i] << " ";
cout << ar[ar.size()-1] << endl;
}
template <typename Q>
void printvvec(vector<vector<Q>> &ar){
rep(i,ar.size()){
rep(j,ar[0].size()-1) cout << ar[i][j] << " ";
cout << ar[i][ar[0].size()-1] << endl;
}
}
template <typename S>
bool range(S a, S b, S x){
return (a <= x && x < b);
}
void yes(){
cout << "Yes" << endl;
}
void no (){
cout << "No" << endl;
}
ll cel (ll a, ll b){
if (a % b == 0) return a / b;
else return a / b + 1;
}
ll gcds(ll a, ll b){
ll c = a % b;
while (c != 0){
a = b;
b = c;
c = a % b;
}
return b;
}
ll len(vector<PL> &ar, ll kg){
int l = ar.size();
if (kg < ar[0].first) return 0LL;
if (kg >= ar[l-1].first) return ar[l-1].second;
int le = 0;
int ri = l-1;
int mid = (le + ri) / 2;
while (true){
if (ar[mid].first <= kg && kg < ar[mid+1].first) return ar[mid].second;
if (kg < ar[mid].first) ri = mid;
else le = mid;
mid = (le + ri) / 2;
}
}
int main(){
int t; cin >> t;
vector<string> ans(t);
rep(i,t){
int n; cin >> n;
map<int,int> m;
map<int,int> ::iterator ite;;
rep(i,n){
int a; cin >> a;
if (m.find(a) == m.end()) m[a] = 1;
else m[a]++;
}
if (n % 2 == 1) ans[i] = "Second";
else {
bool t = true;
for (ite = m.begin(); ite != m.end(); ite++){
if (ite->second % 2 == 1) t = false;
}
if (t) ans[i] = "Second";
else ans[i] = "First";
}
}
rep(i,t) cout << ans[i] <<endl;
} | #include<cstdio>
#include<cctype>
#include<set>
#include<map>
using namespace std;
typedef map<int,int>::iterator Mit;
template<class T>
void read(T& x)
{
x=0;
int f=1,ch=getchar();
while(!isdigit(ch)){
if(ch=='-') f=-1;
ch=getchar();
}
while(isdigit(ch)){
x=x*10+ch-'0';
ch=getchar();
}
x*=f;
}
int t,n;
set<int> a;
map<int,int> b;
int main()
{
read(t);
while(t--)
{
read(n);
int x=0,y;
b.clear();
for(int i=0;i<n;i++)
{
read(y);
b[y]++;
}
Mit it=b.begin();
for(it;it!=b.end();it++)
if(it->second&1) {
x=1;
break;
}
if(x^(n&1)) puts("First");
else puts("Second");
}
return 0;
}
|
#include <bits/stdc++.h>
#define MAXN 1500000
using namespace std;
vector<int>pos[MAXN + 1];
int main() {
int m, n;
cin>>n>>m;
for(int i=0; i<=n; i++)
pos[i].push_back(-1);
for(int i=0; i<n; i++) {
int x;
cin>>x;
pos[x].push_back(i);
}
for(int i=0; i<=n; i++)
pos[i].push_back(n);
for(int ans=0; ans<=n; ans++) {
for(int i=1; i<pos[ans].size(); i++) {
if (pos[ans][i] - pos[ans][i-1] > m) {
cout<<ans<<endl;
return 0;
}
}
}
cout<<n+1<<endl;
}
| #include <cstdio>
#include <cstring>
#include <algorithm>
#define N 1500010
using namespace std;
int n, m;
int tr[4*N], a[N], ans;
void ins(int i, int x, int k, int l, int r){
if (l==r){
tr[i]+=k;
return;
}
int mid=(l+r)/2;
if (x<=mid) ins(i<<1, x, k, l, mid);
else ins(i<<1|1, x, k, mid+1, r);
if (!tr[i<<1] || !tr[i<<1|1]) tr[i]=0;
else tr[i]=1;
}
int find(int i, int l, int r){
if (l==r) return l;
int mid=(l+r)/2;
if (!tr[i<<1]) return find(i<<1, l, mid);
else return find(i<<1|1, mid+1, r);
}
int main(){
scanf("%d%d", &n, &m);
for (int i=1; i<=n; i++) scanf("%d", &a[i]);
for (int i=1; i<=m; i++) ins(1, a[i], 1, 0, n);
ans=find(1, 0, n);
for (int i=m+1; i<=n; i++){
ins(1, a[i], 1, 0, n);
ins(1, a[i-m], -1, 0, n);
ans=min(find(1, 0, n), ans);
}
printf("%d", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> pll;
typedef pair<long long, pll> plp;
priority_queue<plp, vector<plp>, greater<plp> > q;
long long n, m, a[507][507], b[507][507], vis[507][507], dist[507][507], v, x, y;
void check(long long x, long long y, long long d)
{
if(x==0 || y==0 || x>n || y>m) return;
if(dist[x][y]<=d) return;
dist[x][y]=d;
q.push({d, {x, y}});
return;
}
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m-1; j++)
{
dist[i][j]=1000000000000000;
cin>>a[i][j];
}
}
for(int i=1; i<=n-1; i++)
{
for(int j=1; j<=m; j++)
{
dist[i][j]=100000000000000;
cin>>b[i][j];
}
}
dist[1][1]=0;
dist[n][m]=100000000000000000;
q.push({0, {1, 1}});
while(!q.empty())
{
v=q.top().first;
x=q.top().second.first;
y=q.top().second.second;
q.pop();
if(vis[x][y]==1) continue;
vis[x][y]=1;
check(x, y+1, a[x][y]+v);
check(x, y-1, a[x][y-1]+v);
check(x+1, y, b[x][y]+v);
for(int i=1; i<x; i++)
{
check(x-i, y, 1+i+v);
}
}
cout<<dist[n][m];
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using TIII = tuple<int, int, int>;
template<class T> inline bool chmin(T& a, T b) {
return (a > b) ? (a = b, true) : false;
}
const int INF = 1 << 30;
int main(void)
{
int R, C; cin >> R >> C;
vector<vector<int>> A(R, vector<int>(C-1)), B(R-1, vector<int>(C));
rep(i, R) rep(j, C-1) cin >> A.at(i).at(j);
rep(i, R-1) rep(j, C) cin >> B.at(i).at(j);
vector<vector<int>> D(R, vector<int>(C, INF));
D.at(0).at(0) = 0;
priority_queue<TIII, vector<TIII>, greater<TIII>> candidate;
candidate.push(make_tuple(0, 0, 0));
while (!candidate.empty()) {
TIII t = candidate.top(); candidate.pop();
int tD = get<0>(t);
int p1 = get<1>(t);
int p2 = get<2>(t);
if (tD > D.at(p1).at(p2)) continue;
if (p1 == R - 1 && p2 == C - 1) {
cout << tD << endl; return 0;
}
if (p2 < C - 1 && chmin(D.at(p1).at(p2 + 1), tD + A.at(p1).at(p2))) {
candidate.push(make_tuple(tD + A.at(p1).at(p2), p1, p2 + 1));
}
if (p2 > 0 && chmin(D.at(p1).at(p2 - 1), tD + A.at(p1).at(p2 - 1))) {
candidate.push(make_tuple(tD + A.at(p1).at(p2 - 1), p1, p2 - 1));
}
if (p1 < R - 1 && chmin(D.at(p1 + 1).at(p2), tD + B.at(p1).at(p2))) {
candidate.push(make_tuple(tD + B.at(p1).at(p2), p1 + 1, p2));
}
for (int i = 1; i <= p1; i++) {
if (chmin(D.at(p1 - i).at(p2), tD + 1 + i)) {
candidate.push(make_tuple(tD + 1 + i, p1 - i, p2));
}
}
}
} |
/*input
10
-31 -35
8 -36
22 64
5 73
-14 8
18 -58
-41 -85
1 -88
-21 -85
-11 82
*/
#include<bits/stdc++.h>
using namespace std;
int fun(pair<int,int> p1,pair<int,int> p2)
{
int dy = abs(p2.second - p1.second);
int dx = abs(p2.first - p1.first);
// cout<<(dy*1.0/dx*1.0)<<" "<<(dy<=dx && dy>=(-1*dx))<<"\n";
return (dy<=dx);
}
int main()
{
int T = 1; // cin>>T;
while(T--)
{
int n;cin>>n;
vector<pair<int,int>> p(n);
for(int i=0;i<n;i++) cin>>p[i].first>>p[i].second;
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
ans+=fun(p[i],p[j]);
}
}
cout<<ans<<"\n";
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
//#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll mod=998244353;
const ll MAX=140010;
//gcd
ll gcd(ll a,ll b){
if(min(a,b)==0)return max(a,b);
if(max(a,b)%min(a,b)==0)return min(a,b);
return gcd(min(a,b),max(a,b)%min(a,b));
}
signed main(){
ll n;cin>>n;
vector<ll>a(n);
rep(i,n)cin>>a[i];
sort(all(a));
map<ll,pll>ma;
ll ans=0;
rep(i,n){
REP(j,1,sqrt(a[i]+1)){
if(a[i]%j==0){
if(j<=a[0]){
if(ma.find(j)!=ma.end()){
ll g=ma[j].fi;
ll k=ma[j].se;
ma[j]=mp(gcd(g,a[i]),k+1);
}else{
ma[j]=mp(a[i],1);
}
}
if(a[i]/j<=a[0]){
if(ma.find(a[i]/j)!=ma.end()){
ll g=ma[a[i]/j].fi;
ll k=ma[a[i]/j].se;
ma[a[i]/j]=mp(gcd(g,a[i]),k+1);
}else{
ma[a[i]/j]=mp(a[i],1);
}
}
}
}
}
for(auto e:ma){
if((e.fi==e.se.fi&&e.se.se>=2)||e.fi==a[0]){
//cout<<e.fi<<endl;
ans++;
}
}
cout<<ans<<endl;
} |
#include<iostream>
#include<cstdio>
#define mod 998244353
#define N 5000
using namespace std;
long long n,m,ans,dp[N+1][3];
int main()
{
cin>>n>>m;
for (int i=1;i<=m;++i)
{
for (int j=0;j<=n;++j) dp[j][0]=dp[j][1]=dp[j][2]=0;
dp[0][1]=1;
for (int j=1;j<=n;++j) dp[j][2]=(dp[j][2]+dp[j-1][2]*m%mod+dp[j-1][1])%mod,dp[j][1]=(dp[j][1]+dp[j-1][0]*(i-1)%mod+dp[j-1][1]*(m-1)%mod)%mod,dp[j][0]=(dp[j][0]+dp[j-1][0]*(m-i+1)%mod+dp[j-1][1])%mod;
ans=(ans+dp[n][2])%mod;
}
printf("%lld\n",ans);
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = b; i >= a; i--)
#define FOR(i, n) rep(i, 1, n)
#define debug(x) cerr << #x << ' ' << '=' << ' ' << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
const int N = 5010, size = 1 << 20, mod = 998244353, inf = 2e9;
const ll INF = 1e15;
// char buf[size],*p1=buf,*p2=buf;
TP void qr(o& x) {
char c = gc;
x = 0;
int f = 1;
while (!isdigit(c)) {
if (c == '-')
f = -1;
c = gc;
}
while (isdigit(c))
x = x * 10 + c - '0', c = gc;
x *= f;
}
template <class o, class... O> void qr(o& x, O&... y) {
qr(x);
qr(y...);
}
TP void qw(o x) {
if (x / 10)
qw(x / 10);
putchar(x % 10 + '0');
}
TP void pr1(o x) {
if (x < 0)
x = -x, putchar('-');
qw(x);
putchar(' ');
}
template <class o, class... O> void pr1(o x, O... y) {
pr1(x);
pr1(y...);
}
TP void pr2(o x) {
if (x < 0)
x = -x, putchar('-');
qw(x);
putchar(10);
}
template <class o, class... O> void pr2(o x, O... y) {
pr2(x);
pr2(y...);
}
TP void cmax(o& x, o y) {
if (x < y)
x = y;
}
TP void cmin(o& x, o y) {
if (x > y)
x = y;
}
TPP void ad(t1& x, t2 y) {
x += y;
if (x >= mod)
x -= mod;
}
TPP void dl(t1& x, t2 y) {
x -= y;
if (x < 0)
x += mod;
}
int n, m;
ll s[N], p[N];
void solve() {
qr(n, m);
p[0] = 1;
FOR(i, n) p[i] = p[i - 1] * m % mod;
rep(i, 0, m - 1) {
ll t = 1;
FOR(j, n) ad(s[j], t), t = t * i % mod;
}
ll ans = p[n] * n % mod;
FOR(i, n) {
FOR(j, i - 1) { dl(ans, s[i - j] * p[n - (i - j) - 1] % mod); }
}
pr2(ans);
}
int main() {
int T = 1;
// qr(T);
while (T--)
solve();
return 0;
} |
#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;
const int64_t MOD = 1000000007LL;
struct modint {
int64_t x;
modint() { }
modint(int _x) : x(_x) { }
operator int() const { return (int)x; }
modint operator+(int y) { return (x + y + MOD) % MOD; }
modint operator+=(int y) { x = (x + y + MOD) % MOD; return *this; }
modint operator-(int y) { return (x - y + MOD) % MOD; }
modint operator-=(int y) { x = (x - y + MOD) % MOD; return *this; }
modint operator*(int y) { return (x * y) % MOD; }
modint operator*=(int y) { x = (x * y) % MOD; return *this; }
modint operator/(int y) { return (x * modpow(y, MOD - 2)) % MOD; }
modint operator/=(int y) { x = (x * modpow(y, MOD - 2)) % MOD; return *this; }
static modint modinv(int a) { return modpow(a, MOD - 2); }
static modint modpow(int a, int b) {
modint x = a, r = 1;
for (; b; b >>= 1, x *= x) if (b & 1) r *= x;
return r;
}
};
typedef pair<modint, modint> II;
int64_t solve(int64_t N, std::vector<int> A) {
vector<vector<II>> dp(N + 1, vector<II>(2, II(0, 0)));
dp[0][0] = II(1, A[0]);
for (int i = 1; i < N; ++i) {
II& a = dp[i][0];
a = dp[i - 1][0];
a.first += dp[i - 1][1].first;
a.second += dp[i - 1][1].second;
a.second += a.first * A[i];
II& b = dp[i][1];
b = dp[i - 1][0];
b.second -= b.first * A[i];
}
modint ans = dp[N - 1][0].second + dp[N - 1][1].second;
return ans;
}
int main() {
#if DEBUG || _DEBUG
freopen("in.txt", "r", stdin);
// freopen("in_1.txt", "r", stdin);
#endif
int64_t N;
std::cin >> N;
std::vector<int> A(N);
for (int i = 0; i < N; i++) {
std::cin >> A[i];
}
cout << solve(N, std::move(A)) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <int MOD_> struct modnum {
static constexpr int MOD = MOD_;
static_assert(MOD_ > 0, "MOD must be positive");
private:
using ll = long long;
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modnum() : v(0) {}
modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }
friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
modnum inv() const {
modnum res;
res.v = minv(v, MOD);
return res;
}
friend modnum inv(const modnum& m) { return m.inv(); }
modnum neg() const {
modnum res;
res.v = v ? MOD-v : 0;
return res;
}
friend modnum neg(const modnum& m) { return m.neg(); }
modnum operator- () const {
return neg();
}
modnum operator+ () const {
return modnum(*this);
}
modnum& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modnum& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modnum& operator += (const modnum& o) {
v -= MOD-o.v;
v = (v < 0) ? v + MOD : v;
return *this;
}
modnum& operator -= (const modnum& o) {
v -= o.v;
v = (v < 0) ? v + MOD : v;
return *this;
}
modnum& operator *= (const modnum& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modnum& operator /= (const modnum& o) {
return *this *= o.inv();
}
friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};
using num = modnum<1000000007>;
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int N; cin >> N;
vector<int> A(N); for (int& a : A) cin >> a;
array<pair<num, num>, 2> dp{};
dp[0].first = 1;
dp[0].second = A[0];
for (int i = 1; i < N; ++i) {
array<pair<num, num>, 2> ndp{};
// 0 -> 0
ndp[0].first += dp[0].first;
ndp[0].second += dp[0].second + dp[0].first * A[i];
// 0 -> 1
ndp[1].first += dp[0].first;
ndp[1].second += dp[0].second - dp[0].first * A[i];
// 1 -> 0
ndp[0].first += dp[1].first;
ndp[0].second += dp[1].second + dp[1].first * A[i];
dp.swap(ndp);
}
cout << dp[0].second + dp[1].second << '\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) {
cerr << ' ' << p.x << ',' << p.y; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for(auto x: v) os << ' ' << x; return os; }
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
for(auto x: v) os << ' ' << x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) {
for(auto x: v) os << ' ' << x; return os; }
struct d_ {
template<typename T> d_& operator,(const T& x) {
cerr << ' ' << x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const ll MOD = 1e9+7;
ll mod(ll a, ll b=MOD) {
return ((a%b)+b)%b;
}
ll extended_euclid(ll a,ll b,ll &x,ll &y) {
if (a == 0) { x = 0; y = 1; return b;}
ll x1, y1; ll d = extended_euclid(b%a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
ll mod_inverse(ll a, ll n=MOD) {
ll x, y;
ll d = extended_euclid(a, n, x, y);
if (d > 1) return -1;
return mod(x,n);
}
struct Mt {
ll x = 0;
Mt(){}
Mt(ll y):x(mod(y)){}
bool operator==(Mt &y){return x==y.x;}
Mt operator+(Mt y){return mod(x+y.x);}
Mt operator-(){return mod(-x);}
Mt operator-(Mt y){return mod(x-y.x);}
Mt operator*(Mt y){return mod(x*y.x);}
Mt operator/(Mt y){return mod(x*mod_inverse(y.x));}
Mt operator+=(Mt y){return x=(*this+y).x;}
Mt operator-=(Mt y){return x=(*this-y).x;}
Mt operator*=(Mt y){return x=(*this*y).x;}
Mt operator/=(Mt y){return x=(*this/y).x;}
};
ostream& operator<<(ostream& os, const Mt&m){
os << m.x; return os;
}
const ll N = 1e5+7;
ll a[N];
ll n;
ll CNT[N][2];
Mt cnt(ll x, bool lm) {
ll &ret = CNT[x][lm];
if(~ret) return ret;
if(x==n) return 1;
Mt r = cnt(x+1,0);
if(x && !lm) r += cnt(x+1, 1);
return ret=r.x;
}
ll DP[N][2];
Mt dp(ll x, bool lm) {
ll &ret = DP[x][lm];
if(~ret) return ret;
if(x==n) return 0;
Mt r = Mt(a[x])*cnt(x+1,0) + dp(x+1, 0);
if(x && !lm) r += Mt(-a[x])*cnt(x+1,1) + dp(x+1, 1);
return ret=r.x;
}
int main(void) {
ios_base::sync_with_stdio(false);
CL(DP,-1); CL(CNT,-1);
cin >> n;
F(n) cin >> a[i];
cout << dp(0,0) << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define For(i,a,b) for(register int i=a;i<=b;++i)
#define Down(i,a,b) for(register int i=a;i>=b;i--)
#define reg register
namespace yspm{
inline int read(){
int res=0,f=1; char k;
while(!isdigit(k=getchar())) if(k=='-') f=-1;
while(isdigit(k)) res=res*10+k-'0',k=getchar();
return res*f;
}
const int N=1e5+10;
inline int min(int x,int y){return x<y?x:y;}
inline int max(int x,int y){return x>y?x:y;}
inline void swap(int &x,int &y){int t=x; x=y; y=t; return ;}
vector<int>g[N];
int sz[N],f[N],n;
inline void dfs(int x){
sz[x]=1; vector<int> ev,od; f[x]=-1;
for(auto t:g[x]) dfs(t),sz[x]^=sz[t],sz[t]?od.push_back(f[t]):ev.push_back(f[t]);
sort(od.begin(),od.end()); reverse(od.begin(),od.end());
sort(ev.begin(),ev.end()); reverse(ev.begin(),ev.end());
int ne=0,no=0,fl=0;
while(ne<ev.size()&&ev[ne]>0) f[x]+=ev[ne],++ne;
while(no<od.size()) fl?f[x]-=od[no]:f[x]+=od[no],++no,fl^=1;
while(ne<ev.size()) fl?f[x]-=ev[ne]:f[x]+=ev[ne],++ne;
return ;
}
signed main(){
n=read(); for(reg int i=2,tf;i<=n;++i) tf=read(),g[tf].push_back(i); dfs(1);
printf("%lld\n",(n-f[1])/2);
return 0;
}
}
signed main(){return yspm::main();}
|
#include <bits/stdc++.h>
#define REP(i, m, n) for (int(i) = (m); (i) < (n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) (x).begin(), (x).end()
using namespace std;
using Graph = vector<vector<int>>;
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;
}
typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1e9 + 7;
const ll LINF = 1LL << 60;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, q;
cin >> n >> m >> q;
vector<pair<int, int>> vw(n, make_pair(0, 0));
vector<pair<int, int>> x(m, make_pair(0, 0));
rep(i, n)
{
int a, b;
cin >> a >> b;
vw[i] = make_pair(b, a);
}
sort(vw.begin(), vw.end(), greater<pair<int, int>>());
rep(i, m)
{
int a;
cin >> a;
x[i] = make_pair(a, i);
}
sort(x.begin(), x.end());
rep(i, q)
{
int l, r;
cin >> l >> r;
--l, --r;
int ans = 0;
vector<int> seen(m, 0);
rep(i, n)
{
int xmin = INF;
int xmin_idx = -1;
rep(j, m)
{
if (x[j].second >= l && x[j].second <= r)
continue;
if (vw[i].second <= x[j].first && seen[x[j].second] == 0 && x[j].first < xmin)
{
xmin = x[j].first;
xmin_idx = x[j].second;
}
}
if (xmin_idx == -1)
continue;
seen[xmin_idx] = 1;
ans += vw[i].first;
}
cout << ans << endl;
}
return 0;
} | #include"bits/stdc++.h"
#include <inttypes.h>
using namespace std;
#define ll long long
int mod = 1e9+7;
#define int ll
int d, x, y;
void extendedEuclid(int A, int B) {
if(B == 0) {
d = A;
x = 1;
y = 0;
}
else {
extendedEuclid(B, A%B);
int temp = x;
x = y;
y = temp - (A/B)*y;
}
}
int modInverse(int A, int M)
{
extendedEuclid(A,M);
return (x%M+M)%M; //x may be negative
}
void test(){
int n , s , k;
cin >> n >> s >> k;
int g = __gcd(n,k);
if(s%g != 0){
cout<<"-1\n";
return;
}
n/=g , s/=g , k/=g;
int ans = ((n-s) * modInverse(k,n))%n;
cout<<ans<<"\n";
}
int32_t main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t;
cin >> t;
while(t--){
test();
}
} |
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, init, end) for(ll i = init; i < end; i++)
#define REP(i, init, end) for(ll i = init; i < end + 1; i++)
#define rev(i, end, init) for(ll i = init - 1; i >= end; i--)
#define REV(i, end, init) for(ll i = init; i >= end; i--)
#define PI 3.14159265359
// #define EPS 0.0000000001
// #define MOD 1000000007
//cout << std::fixed << std::setprecision(15) << y << endl;
ll getSize(ll N){
ll count = 0;
while(N > 0){
count++;
N /= 10;
}
return count;
}
int main(){
string S[3];
rep(i, 0, 3){
cin >> S[i];
}
ll N[3][26];
ll x;
rep(i, 0, 3){
rep(j, 0, 26){
x = 0;
rep(k, 0, S[i].size()){
x *= 10;
if(S[i][k] - 'a' == j){
x += 1;
}
}
N[i][j] = x;
}
}
vector<ll> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ll p;
ll Nnum[3];
ll Nsize[3];
bool moveP;
bool found = false;
do{
p = 0;
rep(i, 0, 3){
Nnum[i] = 0;
}
rep(j, 0, 26){
moveP = false;
rep(i, 0, 3){
if(N[i][j] != 0){
moveP = true;
}
Nnum[i] += N[i][j] * v[p];
}
if(moveP){
p++;
}
}
if(Nnum[0] + Nnum[1] == Nnum[2]){
found = true;
rep(i, 0, 3){
if(S[i].size() != getSize(Nnum[i])){
found = false;
}
}
if(found){
break;
}
}
}while(next_permutation(v.begin(), v.end()));
if(found){
rep(i, 0, 3){
cout << Nnum[i] << endl;
}
}else{
cout << "UNSOLVABLE" << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, begin, end) for (i=begin;i<end;i++)
#define printint(i0, i1) printf("%d %d\n", i0, i1)
#define MAX_N 1000
using namespace std;
typedef long long int ll;
const int inf = 1000000000;
const int mod = 1000000007;
const int nil = -1;
ll i, j, n, m, k, ans;
ll corr[27], exist[27] = {};
ll a[10], b[10], c[10];
vector<ll> chars;
int main() {
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
rep(i,0,s1.length()){
a[s1.length() - i - 1] = s1[i] - 'a';
exist[s1[i] - 'a'] = 1;
}
rep(i,0,s2.length()){
b[s2.length() - i - 1] = s2[i] - 'a';
exist[s2[i] - 'a'] = 1;
}
rep(i,0,s3.length()){
c[s3.length() - i - 1] = s3[i] - 'a';
exist[s3[i] - 'a'] = 1;
}
rep(i,0,27) if (exist[i] == 1) chars.push_back(i);
if (chars.size() > 10) {
printf("UNSOLVABLE\n");
exit(0);
}
vector<ll> nums;
rep(i,0,10) nums.push_back(i);
do {
bool flag = false;
rep(i,0,chars.size()) corr[chars[i]] = nums[i];
ll n1 = 0, n2 = 0, n3 = 0;
rep(i,0,s1.length()) {
n1 += corr[a[i]] * pow(10,i);
if (i == s1.length()-1 && corr[a[i]] == 0) flag = true;
}
rep(i,0,s2.length()) {
n2 += corr[b[i]] * pow(10,i);
if (i == s2.length()-1 && corr[b[i]] == 0) flag = true;
}
rep(i,0,s3.length()) {
n3 += corr[c[i]] * pow(10,i);
if (i == s3.length()-1 && corr[c[i]] == 0) flag = true;
}
if (n1 + n2 == n3 && !flag) {
printf("%lld\n%lld\n%lld\n", n1, n2, n3);
exit(0);
}
} while (next_permutation(nums.begin(), nums.end()));
printf("UNSOLVABLE\n");
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(int)(n); i++)
using namespace std;
using LL = long long;
using P = pair<int,int>;
double dp[110][110][110];
int main(){
vector<int> A(3);
rep(i,3) cin >> A[i];
dp[A[0]][A[1]][A[2]] = 1.0;
rep(i,100){
rep(j,100){
rep(k,100){
if(i + j + k == 0) continue;
double a = i, b = j, c = k;
double s = a + b + c;
dp[i+1][j][k] += dp[i][j][k] * (a / s);
dp[i][j+1][k] += dp[i][j][k] * (b / s);
dp[i][j][k+1] += dp[i][j][k] * (c / s);
}
}
}
double ans = 0;
int S = A[0] + A[1] + A[2];
rep(j,100){
rep(k,100){
if(j < A[1] or k < A[2]) continue;
double num = 100.0 + j + k - S;
ans += num * dp[100][j][k];
}
}
rep(j,100){
rep(k,100){
if(j < A[0] or k < A[2]) continue;
double num = 100.0 + j + k - S;
ans += num * dp[j][100][k];
}
}
rep(j,100){
rep(k,100){
if(j < A[0] or k < A[1]) continue;
double num = 100.0 + j + k - S;
ans += num * dp[j][k][100];
}
}
cout << setprecision(15) << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int n = 100;
double dp[n + 1][n + 1][n + 1];
int main() {
ios_base::sync_with_stdio(false);
for (int i = n - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
for (int k = n - 1; k >= 0; k--) {
int s = i + j + k;
if (s > 0) {
dp[i][j][k] += (1 + dp[i + 1][j][k]) * i / s;
dp[i][j][k] += (1 + dp[i][j + 1][k]) * j / s;
dp[i][j][k] += (1 + dp[i][j][k + 1]) * k / s;
}
}
}
}
int a, b, c;
cin >> a >> b >> c;
cout << fixed << setprecision(10) << dp[a][b][c] << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define all(x) begin(x), end(x)
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair <int,int> P;
const int INF=1e9;
const int MAX_N=200005;
int par[MAX_N];
int rnk[MAX_N];
map<int,int> cnt[MAX_N];
int c[MAX_N];
void init(int n) {
for (int i=0; i<n; i++) {
par[i]=i;
rnk[i]=0;
cnt[i][c[i]]=1;
}
}
int find(int x) {
if (par[x]==x) {
return x;
}
else {
return par[x]=find(par[x]);
}
}
void unite(int x,int y) {
x=find(x);
y=find(y);
if (x==y) return;
if (rnk[x]<rnk[y]) {
par[x]=y;
for (auto p:cnt[x]) {
cnt[y][p.first]+=p.second;
}
}
else {
par[y]=x;
if (rnk[x]==rnk[y]) rnk[x]++;
for (auto p:cnt[y]) {
cnt[x][p.first]+=p.second;
}
}
}
bool same(int x, int y) {
return find(x)==find(y);
}
int main(){
int n,q;
cin>>n>>q;
rep(i,n) cin>>c[i];
init(n);
while(q--){
int t,a,b;
cin>>t>>a>>b;
if(t==1){
a--; b--;
unite(a,b);
}
else{
a--;
int x=find(a);
cout<<cnt[x][b]<<endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MAXN = 2E5;
int par[MAXN];
int sz[MAXN];
int rep[MAXN];
pair<long long, int> a[MAXN];
pair<long long, int> f[MAXN];
long long ans[MAXN];
int getRoot(int x)
{
if (par[x] != x)
par[x] = getRoot(par[x]);
return par[x];
}
void mge(int x, int y)
{
x = getRoot(x);
y = getRoot(y);
if (x == y)
return;
int r = rep[y];
if (sz[x] > sz[y])
swap(x, y);
par[x] = y;
sz[y] += sz[x];
rep[y] = r;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> f[i].first >> f[i].second;
}
int q;
cin >> q;
for (int i = 0; i < q; i++)
{
cin >> a[i].first;
a[i].second = i;
par[i] = i;
sz[i] = 1;
rep[i] = i;
}
sort(a, a + q);
int left = 0;
int right = q - 1;
long long delta = 0;
for (int i = 0; i < n; i++)
{
if (f[i].second == 1)
{
delta += f[i].first;
}
else if (f[i].second == 2)
{
f[i].first -= delta;
if (a[left].first < f[i].first)
{
while (left + 1 <= right && a[left + 1].first < f[i].first)
{
mge(left, left + 1);
left++;
}
a[left].first = f[i].first;
}
}
else if (f[i].second == 3)
{
f[i].first -= delta;
if (a[right].first > f[i].first)
{
while (right - 1 >= left && a[right - 1].first > f[i].first)
{
mge(right, right - 1);
right--;
}
a[right].first = f[i].first;
}
}
}
//cout << "left " << left << endl;
//cout << "right " << right << endl;
for (int i = 0; i < q; i++)
{
ans[a[i].second] = a[rep[getRoot(i)]].first + delta;
//cout << "setting " << a[i].second << endl;
}
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
return 0;
}
|