code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include<iostream>
using namespace std;
int N, X;
string S;
int main() {
cin >> N >> X >> S;
int res = X;
for (int i = 0; i < S.size(); i++) {
if(S[i] == 'o')
res++;
if(S[i] == 'x') {
res--;
res = max(0, res);
}
}
cout << res << endl;
} | /* Jai Shree Ram 🚩🚩🚩 */
#include "bits/stdc++.h"
#define ll long long int
#define oo 1000000000000000000
#define forr(i,n) for(int i=0;i<n;i++)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define all(x) x.begin(),x.end()
#define unique(v) sort(all(v)); v.resize(distance(v.begin(),unique(all(v))))
#define eb emplace_back
#define FF first
#define SS second
#define mem(a,v) memset(a,v,sizeof(a))
#define pb push_back
#define popcount(x) __builtin_popcount(x)
using namespace std;
template<typename T>
ostream &operator<<(ostream &output,const vector<T> &v){
if(v.empty()) return output;
for(int i=0;i<v.size()-1;i++) output << v[i] <<" ";
output << v.back();
return output;
}
template<typename T>
istream &operator>>(istream &input,vector<T> &v){
for(auto &i: v) cin >> i;
return input;
}
template <int MOD=1000000007>
struct Modular {
int value;
static const int MOD_value = MOD;
Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}
Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}
friend Modular mexp(Modular a, long long e) {
Modular res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
return res;
}
friend Modular inverse(Modular b, long long m = MOD) {
long long u = 0, v = 1, a = b.value;
while (a != 0) {
long long t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
return Modular(u);
}
Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
friend Modular operator+(Modular a, Modular const b) { return a += b; }
friend Modular operator-(Modular a, Modular const b) { return a -= b; }
friend Modular operator-(Modular const a) { return 0 - a; }
friend Modular operator*(Modular a, Modular const b) { return a *= b; }
friend Modular operator/(Modular a, Modular const b) { return a /= b; }
friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};
using mint = Modular<>;
vector<mint> fac, inv;
void preFac(int _n){
fac.resize(_n, 1);
inv.resize(_n, 1);
for (int i = 1; i < _n; ++i) fac[i] = i * fac[i-1];
inv[_n-1] = mint(1, fac[_n-1].value);
for (int i = _n-2; i >= 0; --i) inv[i] = (i+1) * inv[i+1];
}
mint nCr(int _n, int _r) {
if (_r < 0 || _r > _n) return 0;
return fac[_n] * inv[_r] * inv[_n-_r];
}
mint nPr(int _n, int _r) {
if (_r < 0 || _r > _n) return 0;
return fac[_n] * inv[_n-_r];
}
void __sol(){
preFac(2e6+10);
int n,m,k; cin >> n >> m >> k;
if(n - m > k) cout << 0;
else cout << nCr(n + m , m ) - nCr(n + m , m + k + 1);
return;
}
int main(){
fastio;
int tc=1; // cin >> tc;
while(tc--) __sol();
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
for (T& x : data)
input >> x;
return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
output << "(" << data.x << "," << data.y << ")";
return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
for (const T& x : data)
output << x << " ";
return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
trav(a, x)
cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
ll gcd(ll a, ll b) {
while (b) {
tie(a, b) = mp(b, a % b);
}
return a;
}
signed main() {
#ifdef LOCAL
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
vi a(2 * n);
cin >> a;
vi sign(2 * n);
V <ii> ord;
FOR (i, 2 * n) {
ord.app(mp(a[i], i));
}
sort(all(ord));
FOR (i, n) {
sign[ord[i].y] = -1;
}
FOR (i, n) {
sign[ord[i + n].y] = 1;
}
string ans;
vi S;
trav (e, sign) {
if (S.empty() || S.back() == e) {
ans += '(';
S.app(e);
}
else {
ans += ')';
S.pop_back();
}
}
assert(S.empty());
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
map<int64_t, int64_t> memo;
int64_t solve(int64_t X, int64_t Y) {
if (Y <= X) {
return X - Y;
}
if (Y == X + 1 || Y == 2 * X) {
return 1;
}
if (memo.count(Y) > 0) {
return memo[Y];
}
int64_t ans = abs(X - Y);
//最後にi回2倍を適用した後, 足す or 引くどちらかの連打で終わる
for (int64_t i = 1; i < 63; i++) {
int64_t bit = (1ull << i);
int64_t mask = bit - 1;
//最後に足す連打で終わる場合
int64_t target1 = (Y >> i);
int64_t curr_ans1 = solve(X, target1) + i + (Y & mask);
//最後に引く連打で終わる場合
int64_t target2 = target1 + 1;
int64_t curr_ans2 = solve(X, target2) + i + bit - (Y & mask);
ans = min({ ans, curr_ans1, curr_ans2 });
}
return memo[Y] = ans;
}
int main() {
int64_t X, Y;
cin >> X >> Y;
if (Y <= X) {
cout << X - Y << endl;
return 0;
}
cout << solve(X, Y) << endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
#define MOD 1000000007
#define foru(i,a,b) for(int i=(a); i<(b); i++)
#define forue(i,a,b) for(int i=(a); i<=(b); i++)
#define forb(i,n) for(int i=n-1; i>=0; i--)
#define forall(it,v) for(auto it=v.begin();it!=v.end();++it)
#define pb push_back
#define fst first
#define snd second
#define pt(x) cout << x << "\n"
#define sz(c) ((int)c.size())
#define add(a, b, w) edges[a].pb(make_pair(b, w))
typedef pair<int,int> ii;
typedef long long int ll;
string s;
int l;
int dp[20][200];
int solve(int i, int sum){
if(!sum) return INF;
else if(!(sum%3)) return 0;
if(i>=l) return INF;
else if(dp[i][sum]) return dp[i][sum];
int r1 = 1 + solve(i+1,sum-(s[i]-'0'));
int r2 = solve(i+1,sum);
return dp[i][sum] = min(r1,r2);
}
int main(){
//freopen("input.in", "r", stdin);
//cin.tie(0);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> s;
int actualSum = 0;
l = s.size();
foru(i,0,l){
actualSum += s[i] - '0';
}
int r = solve(0,actualSum);
if(r == INF) pt(-1);
else pt(r);
} | #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll a;
cin >> a;
int sum = 0;
bool mod1 = false, mod2 = false, mod0 = false;
int d = 0;
while (a) {
d++;
int t = a % 10;
sum += t;
if (t % 3 == 1) {
mod1 = true;
}
if (t % 3 == 2) {
mod2 = true;
}
if (t % 3 == 0) {
mod0 = true;
}
a = a / 10;
}
if (d < 3 && sum % 3 != 0 && !mod0) {
cout << -1 << endl;
return 0;
}
if (sum % 3 == 0) {
cout << 0 << endl;
} else if (sum % 3 == 1) {
if (mod1) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
} else {
if (mod2) {
cout << 1 << endl;
} else {
cout << 2 << endl;
}
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define SPEED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define READ freopen("out.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout);
#define pb push_back
#define mem(arr,val) memset(arr,val,sizeof(arr))
#define sf(x) scanf("%d",&x)
#define sf2(x,y) scanf("%d %d",&x,&y)
#define sf3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sl2(x,y) scanf("%lld %lld",&x,&y)
#define sl3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define sd(x) scanf("%lf",&x);
#define pii pair<int,int>
#define pLL pair<long long,long long>
#define pDB pair<double,double>
#define ff first
#define sn second
#define PRINT_CASE printf("Case %d: ",tc++)
#define PRINT_CASENL printf("Case %d:\n",tc++)
#define lnd tree[ind<<1]
#define rnd tree[(ind<<1)+1]
#define cnd tree[ind]
#define lndp b,(b+e)>>1,(ind<<1)
#define rndp ((b+e)>>1)+1,e,(ind<<1)+1
#define IN(a,x,y) (a>=x && a<=y)
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
/// int other=mask^((1<<n)-1);
typedef long long ll;
typedef long long int lln;
typedef unsigned long long int ull;
//typedef long long lld;
//const double pi=acos(-1.0);
int fx[]={1,-1,0,0}; //direction array
int fy[]={0,0,1,-1};
int dir[4][2]={1,0,-1,0,0,-1,0,1};
//int X[8]={0,0,1,-1,-1,1,1,-1};
int Y[8]={1,-1,0,0,-1,-1,1,1};
int knight[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,-1};
const long double EPS=1e-16;
//#define INF 10000
ll lcm(ll a,ll b)
{
return ((a*b)/__gcd(a,b));
}
//struct compare
//{
// bool operator()(const pLL& l, const pLL& r)
// {
//// if(l.ff==r.ff)
//// {
//// return l.sn<r.sn;
//// }
// return l.sn>r.sn;
//// if(l.ff==r.ff)
//// {
//// return l.sn>rnd;
//// }
// }
//};
ll INF=1e18;
const int mx=201050;
//const int mod=998244353;
const int mod=1e9+7;
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
//////cout<<*X.find_by_order(1)<<endl;
////cout<<X.order_of_key(-5)<<endl;
//4352348930241
//const int mod=11092019;
typedef pair<ll,pair<int,int > > pair3;
struct compare
{
bool operator()(const pLL& l, const pLL& r)
{
// if(l.ff==r.ff)
// {
// return l.sn<r.sn;
// }
return l.sn>r.sn;
// if(l.ff==r.ff)
// {
// return l.sn>rnd;
// }
}
};
int main()
{
// double start = clock();
// double tim = (clock() - start)/CLOCKS_PER_SEC;
int t=1,tc=1;
// sf(t);
while(t--)
{
// cout<<INF<<endl;
int n;
sf(n);
ll ans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
int x=i*j;
if(x>n)
break;
int y=n/x;
ans+=y;
}
}
cout<<ans<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main()
{
int k;
cin >> k;
int a=1,b=1,c=1;
ll ans = 0;
while(a*b*c<=k){
while(a*b*c<=k){
while(a*b*c<=k){
if(a!=b&&b!=c){
ans += 6;
}else if(a==b&&b==c){
ans += 1;
}else{
ans += 3;
}
c++;
}
c = ++b;
}
b=++a;
c=b;
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define N 214514
#define all(x) (x).begin(), (x).end()
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
int main() {
vector<ll> cnt(10);
ll k;
cin >> k;
for (int i = 1; i <= 9; i++) cnt[i] = k;
string s, t;
cin >> s >> t;
for (int i = 0; i < 4; i++) {
cnt[s[i] - '0']--;
cnt[t[i] - '0']--;
}
long double sum = (9 * k - 8) * (9 * k - 9), num = 0.0;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if ((i == j && cnt[i] < 2) ||
(i != j && (cnt[i] < 1 || cnt[j] < 1)))
continue;
ll score_a = 0, score_t = 0;
vector<ll> ca(10), ct(10);
for (int i = 0; i < 4; i++) {
ca[t[i] - '0']++;
ct[s[i] - '0']++;
}
ca[i]++;
ct[j]++;
for (int i = 1; i <= 9; i++) {
score_a += pow(10, ca[i]) * i;
score_t += pow(10, ct[i]) * i;
}
if (score_t > score_a) {
if (i == j)
num += (long double)cnt[i] * (cnt[i] - 1.0);
else
num += (long double)cnt[i] * cnt[j];
}
}
}
cout << fixed << setprecision(15) << num / sum << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> P;
typedef tuple<int,int,int> tpl;
#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
struct mint {
long long x;
mint(long long x=0):x((x%MOD+MOD)%MOD){}
long long val(){
return x;
}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD-a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(long long t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime MOD
mint inv() const {
return pow(MOD-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
struct combination {
vector<mint> fact, ifact;
combination(long long n):fact(n+1),ifact(n+1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(long long n, long long k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
};
void Main(){
int N,M; cin >> N >> M;
int S = 0;
VL A(N); REP(i,N) cin >> A[i], S+=A[i];
if(M<S){
cout << 0 << en;
return;
}
mint ans(1);
REP(i,N+S){
ans *= M+N-i;
}
REP(i,N+S){
ans /= i+1;
}
cout << ans.val() << en;
return;
}
int main(void){
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
int t=1; //cin>>t;
REP(_,t) Main();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
//begin of def
#define fastio ios_base::sync_with_stdio(false);cin.tie(0)
#define endl '\n'
using lli = long long int;
using ulli = unsigned long long int;
using Ld = long double;
using pii = pair<int, int>;
using pll = pair<lli, lli>;
using pld = pair<Ld, Ld>;
#define X first
#define Y second
#define rep(I, S, E) for(int I = (S); I < (E); I++)
#define repq(I, S, E) for(int I = (S); I <= (E); I++)
#define pb push_back
#define epb emplace_back
#define ALL(X) X.begin(), X.end()
//end of def
vector<lli> v;
int main(){
fastio;
vector<lli> p;
lli test = 1;
repq(i, 2, 50){
bool f = true;
rep(j, 2, i){
if(i % j == 0){
f = false;
break;
}
}
if(f){
p.pb(i);
test *= i;
}
}
v.resize(p.size());
int n;
cin >> n;
rep(i, 0, n){
int k;
cin >> k;
rep(j, 0, p.size()){
if(k % p[j] == 0)
v[j] |= (1LL << i);
}
}
lli mn = 4e18;
rep(t, 1, 1LL << p.size()){
lli x = 0, mul = 1;
rep(j, 0, p.size()){
if(t & (1LL << j)){
x |= v[j];
mul *= p[j];
}
}
if(x == (1LL << n) - 1){
mn = min(mn, mul);
}
}
cout << mn;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long lint;
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define repp(i,m,n) for(lint (i)=(m);(i)<(n);(i)++)
#define repm(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define INF (1ll<<60)
#define all(x) (x).begin(),(x).end()
const lint MOD =1000000007;
//const lint MOD=998244353;
const lint MAX = 4000000;
using Graph =vector<vector<lint>>;
typedef pair<lint,lint> P;
typedef map<lint,lint> M;
#pragma GCC optimize("Ofast")
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
lint fac[MAX], finv[MAX], inv[MAX];
void COMinit()
{
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (lint i = 2; i < MAX; i++)
{
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
long long COM(lint n, lint k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
lint primary(lint num)
{
if (num < 2) return 0;
else if (num == 2) return 1;
else if (num % 2 == 0) return 0;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2)
{
if (num % i == 0)
{
return 0;
}
}
return 1;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
lint lcm(lint a,lint b){
return a/__gcd(a,b)*b;
}
lint gcd(lint a,lint b){
return __gcd(a,b);
}
int main(){
lint n,d,h;
cin>>n>>d>>h;
P p[n];
double ans=0;
rep(i,n){
lint a,b;
cin>>a>>b;
long double kata=(double)(h-b)/(d-a);
ans=max(ans, double(h-kata*d));
}
printf("%.12f\n",ans);
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<(1<<n)-1<<endl;
for(int i=1;i<(1<<n);i++)
{
for(int j=0;j<(1<<n);j++)
putchar('A'+(__builtin_popcount(i&j)&1));
puts("");
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
vector<string> ret[8];
int main(){
ret[0].push_back("AB");
for(int i=1;i<8;i++){
for(string s: ret[i-1]){
string t="";
for(int j=0;j<s.size();j++){
t+=s[j]; t+=s[j];
}
ret[i].push_back(t);
}
for(int j=0;j<(1<<i);j++){
string t="";
if(j==0){
for(int k=0;k<(1<<i);k++){
t+="AB";
}
}
else {
for(int k=0;k<(1<<i);k++){
if(ret[i-1][j-1][k]=='A'){
t+="AB";
}
else t+="BA";
}
}
ret[i].push_back(t);
}
}
int n;
scanf("%d",&n); n--;
printf("%d\n",(int)ret[n].size());
for(string s: ret[n]){
cout<<s<<"\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount
const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;
vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
struct UnionFind {
vector<ll> par, s;
UnionFind(ll N) : par(N), s(N) { rep(i,N) { par[i] = i; s[i] = 1; } }
ll root(ll x) { return par[x]==x ? x : par[x] = root(par[x]); }
ll size(ll x) { return par[x]==x ? s[x] : s[x] = size(root(x)); }
void unite(ll x, ll y) { ll rx=root(x), ry=root(y); if (rx!=ry) { s[rx] += s[ry]; par[ry] = rx; } }
bool same(ll x, ll y) { ll rx=root(x), ry=root(y); return rx==ry; }
};
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
int main() {
ll H, W; cin >> H >> W;
string S[H]; rep(i,H) cin >> S[i];
UnionFind uf(H+W);
rep(i,H) rep(j,W) {
if ((i==0||i==H-1)&&(j==0||j==W-1)) uf.unite(i,j+H);
if (S[i][j]=='#') uf.unite(i,j+H);
}
set<ll> sh, sw;
rep(i,H) sh.insert(uf.root(i));
rep(i,W) sw.insert(uf.root(i+H));
// for (auto x: sh) cout << x << " "; cout << endl;
// for (auto x: sw) cout << x << " "; cout << endl;
ll result = min(sh.size(),sw.size()) - 1;
cout << result << endl;
return 0;
} | #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <iomanip>
#include <utility>
typedef long long ll;
typedef long double ld;
using namespace std;
int A[100010], B[100010];
int main() {
int N, L;
cin >> N >> L;
A[0]=B[0]=0, A[N+1]=B[N+1]=L+1;
for(int i=1; i<=N; ++i) cin >> A[i];
for(int i=1; i<=N; ++i) cin >> B[i];
vector<int> v;
for(int i=0; i<=N+1; ++i){
if(A[i]==B[i]) v.emplace_back(i);
}
ll ans=0;
for(int t=0; t+1<v.size(); ++t){
int i=v[t]+1;
while(A[i]>B[i]) ++i;
int j=i, k=i+1;
while(j<v[t+1]){
while(k<=v[t+1]){
if(A[k]-B[j]==k-j) break;
++k;
}
if(k>v[t+1]){
cout << -1 << endl;
return 0;
}
ans += k-j;
int m=j+1;
while(m<v[t+1]){
if(B[m]-B[j]>m-j) break;
++m;
}
j=m;
}
j=i-1, k=i-2;
while(j>v[t]){
while(k>=v[t]){
if(B[j]-A[k]==j-k) break;
--k;
}
if(k<v[t]){
cout << -1 << endl;
return 0;
}
ans += j-k;
int m=j-1;
while(m>v[t]){
if(B[j]-B[m]>j-m) break;
--m;
}
j=m;
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
ll n;
cin >> n;
vector<ll> a(n);
rep(i,n){
cin >>a[i];
}
ll ans;
ans = n*(n-1)/2;
sort(a.begin(),a.end());
ll cnt=0;
rep(i,n){
if(i == 0){
continue;
}
if(a[i-1]==a[i]){
cnt++;
ans = ans - cnt;
}
else{
cnt =0;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long L;
cin >> L;
long long res = 1;
for(int i = 1; i <= 11; i++) {
res *= L-i;
res /= i;
}
cout << res << endl;
} |
#include <iostream>
#include <algorithm>
#define rep(i,N) for(int i=0;i<N;i++)
#define REP(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
#define PInt std::pair<int,int>
int main(void){
int H,W,N,M;
cin >> H >> W >> N >> M;
enum kind{
EMPTY,
BLOCK,
LIGHT,
MAX
};
int h,w;
kind stage[1500][1500];
rep(i,H)rep(j,W)stage[i][j]=EMPTY;
rep(i,N){
cin >> h >> w;
stage[h-1][w-1]=LIGHT;
}
rep(i,M){
cin >> h >> w;
stage[h-1][w-1]=BLOCK;
}
int res =0;
int dh[]={-1,0,1,0},dw[]={0,-1,0,1};
rep(i,H)rep(j,W){
if(stage[i][j]==EMPTY){
int flag = 0;
rep(d,4){
h=i,w=j;
while(0<=h && h <H && 0<=w && w < W && stage[h][w]==EMPTY){
h+=dh[d];
w+=dw[d];
}
if(!(0<=h && h <H && 0<=w && w < W) || stage[h][w]==BLOCK){
//届いてないので次
flag++;
}else{
break;
}
}
if(flag == 4){
res++;
}
}
}
cout << ((ll)H*(ll)W-(ll)(M+res)) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B){
vector<vector<int>> e(N);
rep(i, M) {
A[i]--;
B[i]--;
e[A[i]].push_back(B[i]);
}
ll ans = 0;
rep(i, N) {
set<int> seen;
queue<int> q;
seen.insert(i);
q.push(i);
while (!q.empty()) {
int v = q.front(); q.pop();
for (int u: e[v]) {
if (seen.count(u)) continue;
seen.insert(u);
q.push(u);
}
}
ans += seen.size();
}
cout << ans << endl;
}
// Generated by 2.3.1 https://github.com/kyuridenamida/atcoder-tools
int main(){
long long N;
std::scanf("%lld", &N);
long long M;
std::scanf("%lld", &M);
std::vector<long long> A(M);
std::vector<long long> B(M);
for(int i = 0 ; i < M ; i++){
std::scanf("%lld", &A[i]);
std::scanf("%lld", &B[i]);
}
solve(N, M, std::move(A), std::move(B));
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>
const ll mod = 1000000007;
const ll INF = 1001001001001001001;
string x;
ll m;
vector<ll> s;
bool f(ll b){
vector<ll> y;
ll a = m;
while(a > 0){
y.push_back(a%b);
a /= b;
}
reverse(y.begin(), y.end());
// cout << b << endl;
// rep(i, s.size()){
// cout << s[i] << " ";
// }cout << endl;
// rep(i, y.size()){
// cout << y[i] << " ";
// }cout << endl;
// cout << endl;
if(y.size() > s.size()) return true;
else if(y.size() == s.size()){
rep(i, y.size()){
if(y[i] > s[i]) return true;
else if(y[i] < s[i]) return false;
}
return true;
}else return false;
}
int main(){
cin >> x >> m;
int k = 0;
int mx = 1;
while(x[k] != 0) {
int c = x[k]-'0';
mx = max(mx, c);
k++;
s.push_back(c);
}
if(k == 1){
int c = x[0]-'0';
if(c > m) cout << 0 << endl;
else cout << 1 << endl;
return 0;
}
// rep(i, s.size()){
// cout << s[i];
// }cout << endl;
ll l = 1, r = INF;
while(l < r - 1){
ll mid = (l+r) / 2;
if(f(mid)){
l = mid;
}else{
r = mid;
}
}
l = min(l, (ll)1000000000000000000);
// cout << l << " " << mx << endl;
cout << max((ll)0, l - mx) << endl;
} | //#define local
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rep(i,n); for(long long i = 0;i < (n);i++)
using ll = long long;
using P = pair<long long,long long>;
template <class T> using vec = vector<T>;
#ifdef local
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
ll n; cin >> n;
string s; cin >> s;
for(ll i=2;i<s.size();i++){
if(s[i-2]=='f'&&s[i-1]=='o'&&s[i]=='x'){
s.erase(i-2,3);
i-=3;
}
}
cout << s.size() << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define pb push_back
#define mp make_pair
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int MAX_N = 2e5+10;
const string YES = "Takahashi\n", NO = "Aoki\n";
int N;
string s, x;
int dp[MAX_N][7];
int go(int at, int rem)
{
if(at == N){
return (rem == 0)?1:0;
}
int& res = dp[at][rem];
if(res != -1) return res;
if(x[at] == 'T'){
if(go(at+1, (10*rem)%7)) return res=1;
if(go(at+1,(10*rem+(s[at]-'0'))%7)) return res=1;
return res=0;
}else{
if(!go(at+1, (10*rem)%7)) return res=0;
if(!go(at+1,(10*rem+(s[at]-'0'))%7)) return res=0;
return res=1;
}
}
string solve()
{
memset(dp,-1,sizeof(dp));
int res = go(0, 0);
if(res) return YES;
return NO;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> N;
cin >> s >> x;
cout<< solve();
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
// #pragma GCC optimize("-O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
using namespace std;
using namespace __gnu_pbds;
#ifdef ON_LINUX
#include <sys/resource.h>
#define meminc rlimit rlim;if (getrlimit(RLIMIT_STACK, &rlim)) return 1;rlim.rlim_cur = 26843556;if (setrlimit(RLIMIT_STACK, &rlim)) return 2;
#else
#define meminc
#endif
#ifdef LOCAL
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#else
#define dbg(args...)
#endif
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);
}
};
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
template<typename T,typename U> std::ostream& operator<<(std::ostream& out, std::pair<T,U> a) {
out<< a.first << " " << a.second;
return out;
}
template<typename T,typename U> std::istream& operator>>(std::istream& in, std::pair<T,U> &a) {
in >> a.first >> a.second;
return in;
}
#define int long long int
#define ll int
#define x first
#define y second
#define pii pair<int, int>
#define vii vector<pii>
#define vi vector<int >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// -------------------Standard Traversal Moves---------------------
// vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1};
// vi fx = {2, -2, 2, -2, 1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2};
// vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1};
// ----------------------------------------------------------------
#define rep(i, a, b) for(int i=a;i<b;i++)
#define all(a) (a).begin(),(a).end()
#define sz(x) (int )x.size()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define endl '\n'
// const int hell = (int)998244353;
const int hell = (int)1e9 + 7;
const int inf = 3223372036854775807;
const double PI = 3.14159265;
const int N = (int) 1e6 + 5;
int n, m, k, a[N];
int32_t main()
{
// meminc;
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr.precision(10);
cout.precision(25);
cout << fixed;
#ifdef ON_LINUX
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
int tests = 1;
// cin >> tests;
rep(test, 1, tests+1)
{
cin >> n;
rep(i, 0, n)cin >> a[i];
int gcd = a[0];
rep(i, 1, n)gcd = __gcd(a[i], gcd);
cout << gcd;
}
#ifdef LOCAL
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
} |
#include <iostream>
using namespace std;
long long power(long long x, long long k, long long mod){
long long ret = 1;
while(k > 0){
if(k&1) ret *= x, ret %= mod;
x *= x, x %= mod;
k >>= 1;
}
return ret;
}
int main(){
long long N, M;
cin >> N >> M;
long long ans = power(10,N,M*M);
ans /= M;
cout << ans << endl;
}
| #include<bits/stdc++.h>
#define pb push_back
using namespace std;
#define G getchar()
int read()
{
int x=0; bool flg=false; char ch=G;
for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
return flg?-x:x;
}
#undef G
#define fi first
#define se second
typedef long long ll;
const int mod=998244353;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,int y){x=upd(x+y-mod);}
inline void iadd(int &x,int y){x=upd(x-y);}
int qpow(int x,int y){
int res=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1) res=1LL*res*x%mod;
return res;
}
#define rep(i,l,r) for (int i(l);i<=r;i++)
#define per(i,l,r) for (int i(r);i>=l;i--)
ll n; int m;
struct C{
int x,y,z;
};
C f[2][10010]; bool e=0;
void trans(){
e^=1;
// rep(i,0,m-1) f[e][i]=(C){0,0,0};
rep(i,0,m-1){
C &g=f[e^1][i],&h=f[e^1][g.z],&o=f[e][i];
o.x=g.x*h.x%m,o.y=(g.y*h.x+h.y)%m,o.z=h.z;
}
}
void solve(){
scanf("%lld%d",&n,&m);
rep(i,0,m-1){
f[e][i]=(C){10%m,(i*10)/m,i*10%m};
}
int a=0,b=1;
for (ll k=n;k;k>>=1,trans())
if (k&1){
C &t=f[e][b];
a=(a*t.x+t.y)%m,b=t.z;
}
printf("%d\n",a);
}
int main()
{
for (int T=1;T--;) solve();
}
|
#include<bits/stdc++.h>
using namespace std ;
const int maxn = 100 + 10 ;
int dp[maxn][maxn][maxn][maxn] ;
int main()
{
std::ios::sync_with_stdio(false) , cin.tie(0) ;
int n ;
long long x ;
cin >> n >> x ;
vector<int> a(n + 1) ;
for(int i = 1 ; i <= n ; i ++) cin >> a[i] ;
//dp[i][j][k][p]???ǰi???ѡ?j????ģp?k???ֵ
for(int i = 0 ; i < maxn ; i ++)
for(int j = 0 ; j < maxn ; j ++)
for(int k = 0 ; k < maxn ; k ++)
for(int p = 0 ; p < maxn ; p ++)
dp[i][j][k][p] = -1 ;
for(int i = 1 ; i <= n ; i ++) for(int p = 1 ; p <= 100 ; p ++)
{
dp[i][1][a[i] % p][p] = a[i] ;
//cout << "!!! " << i << ' ' << 1 << ' ' << a[i] % p << ' ' << p << ' ' << a[i] << '\n' ;
}
for(int i = 1 ; i < n ; i ++)
for(int j = 1 ; j < i + 1 ; j ++)
for(int p = 1 ; p <= 100 ; p ++)
for(int k = 0 ; k < p ; k ++)
{
//cout << i << ' ' << j << ' ' << k << ' ' << p << ' ' << dp[i][j][k][p] << '\n' ;
if(dp[i][j][k][p] < 0) continue ;
dp[i + 1][j][k][p] = max(dp[i + 1][j][k][p] , dp[i][j][k][p]) ;
dp[i + 1][j + 1][(k + a[i + 1]) % p][p] = max(dp[i + 1][j + 1][(k + a[i + 1]) % p][p] ,
dp[i][j][k][p] + a[i + 1]) ;
// if(i == 1 && j == 1 && k == 1 && p == 2)
// {
// cout << i + 1 << ' ' << j + 1 << ' ' << (k + a[i + 1]) % p << ' ' << dp[i + 1][j + 1][(k + a[i + 1]) % p][p] << '\n' ;
// }
}
//cout << dp[2][2][1][2] << '\n' ;
long long ans = x ;
for(int i = 0 ; i < maxn ; i ++)
for(int j = 0 ; j < maxn ; j ++)
for(int k = 0 ; k < maxn ; k ++)
{
if(dp[i][j][k][j] < 0) continue ;
if((x - k) % j == 0) ans = min(ans , (x - dp[i][j][k][j]) / j) ;
}
cout << ans << '\n' ;
return 0 ;
} | #include<bits/stdc++.h>
using namespace std;
//dengyaotriangle!
const int maxn=100005;
int n,l;
int a[2][maxn];
int v[maxn],li[maxn],ri[maxn],lj[maxn],rj[maxn],t;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>n>>l;
for(int t=0;t<2;t++){
for(int i=1;i<=n;i++)cin>>a[t][i],a[t][i]-=i;
a[t][0]=0;a[t][n+1]=l-n;
}
for(int i=0;i<=n+1;i++){
if(i==0||a[0][i]!=a[0][i-1]){
++t;v[t]=a[0][i];li[t]=ri[t]=i;
lj[t]=-1;
}else ri[t]=i;
}
int sp=0;
for(int i=0;i<=n+1;i++){
while(sp<=0||a[1][i]>v[sp])++sp;
if(a[1][i]!=v[sp]){
cout<< -1;
return 0;
}
if(lj[sp]==-1)lj[sp]=i;
rj[sp]=i;
}
long long ans=0;
for(int i=1;i<=t;i++){
if(lj[i]!=-1)ans+=max(li[i]-lj[i],0)+max(rj[i]-ri[i],0);
}
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define fi(a,b) for(int i=a;i<b;i++)
#define fj(a,b) for(int j=a;j<b;j++)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define ull unsigned long long
#define bp(x) __builtin_popcount(x)
#define pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define getMax(x) max_element(x.begin(),x.end())
#define getMin(x) min_element(x.begin(),x.end())
#define endl "\n"
typedef vector<int> vi;
typedef vector< pair<int, int> > vii;
typedef vector<long long> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector< pair<ll, ll> > vll;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// auto dist = uniform_int_distribution<int>(l, r);
// int dx[] = {1, 0, -1, 0};
// int dy[] = {0, 1, 0, -1};
// int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1};
// int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0};
void omae_wa_mou_shindeiru(int tc) {
ll n;
cin >> n;
vl p(n + 1), f(n), ans;
fi(1, n + 1) {
ll temp;
cin >> temp;
p[i] = temp;
}
vl temp = p;
sort(temp.begin(), temp.end());
set<int>odd, even;
fi(1, n) {
if (p[i] > p[i + 1]) {
if (i % 2)
odd.insert(i);
else
even.insert(i);
}
}
fi(1, n * n + 1) {
if (p == temp) {
break;
}
if (i % 2) {
int kk;
if (odd.size() == 0) {
kk = n - 1 - (n % 2);
}
else {
kk = *odd.begin();
odd.erase(odd.begin());
}
swap(p[kk], p[kk + 1]);
ans.push_back(kk);
if (even.find(kk + 1) != even.end()) {
even.erase(kk + 1);
}
if (even.find(kk - 1) != even.end()) {
even.erase(kk - 1);
}
if (kk + 2 <= n) {
if (p[kk + 1] > p[kk + 2]) {
even.insert(kk + 1);
}
}
if (kk - 1 >= 1) {
if (p[kk - 1] > p[kk]) even.insert(kk - 1);
}
}
else {
int kk;
if (even.size() == 0) {
kk = n - 1 - (n + 1) % 2;
}
else {
kk = *even.begin();
even.erase(even.begin());
}
swap(p[kk], p[kk + 1]);
ans.push_back(kk);
if (odd.find(kk + 1) != odd.end()) {
odd.erase(kk + 1);
}
if (odd.find(kk - 1) != odd.end()) {
odd.erase(kk - 1);
}
if (kk + 2 <= n) {
if (p[kk + 1] > p[kk + 2]) odd.insert(kk + 1);
}
if (kk - 1 >= 1) {
if (p[kk - 1] > p[kk]) odd.insert(kk - 1);
}
}
// pr(p);
}
cout << ans.size() << endl;
pr(ans);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
cin >> tc;
fi(1, tc + 1) {
omae_wa_mou_shindeiru(i);
}
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mod 998244353
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
ll binpow(ll x,ll y)/* (x^y)%p in O(log y) */{ll res=1;while (y > 0){if(y&1)res=(res*x);y = y>>1;x=(x*x);}return res;}
ll binpowmod(ll x,ll y,ll p)/* (x^y)%p in O(log y) */{ll res=1;x=x%p;while (y > 0){if(y&1)res=(res*x)%p;y = y>>1;x=(x*x)%p;}return res;}
ll mod_inverse(ll n,ll p)/* Returns n^(-1) mod p */{return binpowmod(n,p-2,p);}
ll gcd(ll x,ll y)
{
if(y==0)
return x;
return gcd(y,x%y);
}
bool isPowerOfTwo (ll x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}
void swap(int &x,int &y){
int temp = x;
x = y;
y = temp;
}
unsigned int onesComplement(unsigned int n)
{
// Find number of bits in the given integer
int number_of_bits = floor(log2(n))+1;
// XOR the given integer with poe(2,
// number_of_bits-1 and print the result
return ((1 << number_of_bits) - 1) ^ n;
}
bool comp1(pair<int,int> x, pair<int,int> y)
{
return x.second < y.second;
}
// class cmp //comrootator for priority_queue
// { //declaration: priority_queue<int,vector<int>,cmp>
// public:
// bool operator()(E& A,E& B)
// {
// // if(A.first == B.first)
// // return A.second < B.second;
// return A.wt > B.wt;
// }
// };
void solve(){
int n;
cin >> n;
vector<int> p(n + 1);
for(int i = 1; i <= n; ++i)
cin >> p[i];
int par = 1;
vector<int> moves;
while(true) {
bool f = true;
bool f2 = false;
for(int i = 1; i <= n - 1; ++i) {
if(p[i] > p[i + 1]) {
f = false;
if(i % 2 == par) {
f2 = true;
swap(p[i], p[i + 1]);
moves.push_back(i);
par ^= 1;
break;
}
}
}
if(f)
break;
if(f2)
continue;
for(int i = n - 1; i >= 1; --i) {
if(p[i] < p[i + 1] and i % 2 == par) {
moves.push_back(i);
swap(p[i], p[i + 1]);
par ^= 1;
break;
}
}
}
cout << moves.size() << '\n';
for(int x: moves)
cout << x << ' ';
cout << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
int t;
cin >> t;
while(t--){
solve();
}
// solve();
} |
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
//const int MAX=INT_MAX;
//const ll MAX=100000000000;
//const int mod = 1000000007;
//const int mod = 998244353;
//const ll A=100000000000;
const int INF=1000000000;
ll extgcd(ll a,ll b,ll& x,ll& y){
if(b==0){
x=1;
y=0;
return a;
}else{
ll d =extgcd(b,a%b,x,y);
ll prv=x;
x=y;
y=prv-a/b*y;
return d;
}
}
int main(){
int T;
cin>>T;
ll N,S,K;
for(int t=0;t<T;t++){
cin>>N>>S>>K;
ll x=0;
ll y=0;
ll g=extgcd(N,-K,x,y);
if(S%g!=0){
cout<<-1<<endl;
}else{
ll s=S/g;
ll n=N/g;
ll k=K/g;
ll nx=x*s;
ll ny=y*s;
// cout<<"ny:"<<ny<<"n:"<<n<<endl;
ll t;
if(n>0){
if(-double(ny)/double(n)<0){
t=-ny/n;
}else{
t=-ny/n+1;
}
}else{
if(-double(ny)/double(n)<0){
t=-ny/n-1;
}else{
t=-ny/n;
}
}
// cout<<t<<endl;
ll ans=n*t+ny;
// cout<<g<<":"<<s<<":"<<x<<":"<<y<<":"<<ans<<endl;
cout<<ans<<endl;
}
}
}
| #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;
ll gcd (ll a, ll b, ll & x, ll & y) {
if (a == 0) {
x = 0; y = 1;
return b;
}
ll x1, y1;
ll d = gcd (b%a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
void solve()
{
ll n,s,k,gg;
cin>>n>>s>>k;
gg=__gcd(n,k);
if(s%gg!=0)
{
ol("-1\n");
return;
}
n/=gg,s/=gg,k/=gg;
ll x,y;
gcd(k,n,x,y);
x=(x+n)%n;
s=(n-s)%n;
s=(s*x)%n;
s=(s+n)%n;
ol(s);on;
}
int main()
{ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
vector<int> V;
vector<int> P;
long X;
long drink = 0;
int ans;
cin >> N >> X;
int a, b;
for(int i = 0; i < N; ++i){
cin >> a >> b;
V.push_back(a);
P.push_back(b);
}
for(ans = 0; ans < N; ++ans){
drink += V.at(ans) * P.at(ans);
if(drink > X * 100){
cout << ans + 1 << endl;
return 0;
}
}
cout << -1 << endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0;i<n;++i)
#define rep1(i, n) for(int i=1;i<=n;++i)
#define ll long long
int main(){
int n;
ll x, v, p;
cin>>n>>x;
x *= 100;
vector<ll>a;
rep(i, n){
cin>>v>>p;
a.emplace_back(v * p);
}
int ans = -1;
ll sum = 0;
rep(i, n){
sum += a[i];
if(sum > x){
ans = i+1;
break;
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i,n) for(ll i=0;i<n;i++)
#define revf(i,n) for(ll i=n-1;i>=0;i--)
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define rep1(i,b) for(ll i=1;i<=b;i++)
#define inp(a,n) for(ll i=0;i<n;i++)cin>>a[i]
#define outp(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "
#define outp1(a,n) for(ll i=1;i<n;i++)cout<<a[i]<<" "
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (((a)*(b))/gcd((a),(b)))
#define pb push_back
#define mp make_pair
#define BINSC binary_search
#define BITOUT __builtin_popcount
#define mem(x,n) memset(x,n,sizeof(x))
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define test ll t; cin>>t; while(t--)
#define cn cout<<"\n";
int main()
{
fast
ll n,x;
cin>>n>>x;
ll v[n],p[n];
forr(i,n)
cin>>v[i]>>p[i];
ll s=0,s1=-1;
forr(i,n)
{
s+=v[i]*p[i];
if(s>x*100)
{
s1=i;break;
}
}
if(s1!=-1)
cout<<s1+1;
else
cout<<-1;
} | /*
* author: Apiram
* created: 23.01.2021 17:34:22
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int64_t n;
int64_t k;
cin>>n>>k;
k*=100;
vector<int64_t>arr(n);
for (auto i =0;i<n;++i){
int64_t a,b;
cin>>a>>b;
arr[i]=a*b;
}
int64_t i=0;
while(k>=0&&i<n){
k-=arr[i];
++i;
}
if (k<0)
cout<<i;
else cout<<-1;
return 0;} |
#include <iostream>
#include<string>
#include <vector>
#include <iomanip>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
int n;
string s;
cin >> s;
int result = 0;
for (int i = 0; i < 9; i++)
{
if(s[i] == 'Z'){
if(s[i+1] == 'O'){
if(s[i+2] == 'N'){
if(s[i+3] == 'e'){
result++;
}
}
}
}
}
cout << result << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define ll long long int
#define test() int tt; cin>>tt; while(tt--)
#define fst_io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl '\n'
#define lg 1000000000
#define vec vector<int>
void arrInput(int a[],int n){rep(i,0,n) cin>>a[i];}
void solve()
{
string s;
int c=0;
cin>>s;;
for(int i=0; i<=s.size()-4; i++)
{
if(s[i]=='Z' && s[i+1]=='O' && s[i+2]=='N' && s[i+3]=='e')
c++;
}
cout<<c;
}
int main()
{
solve();
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vii = vector<vector<int>>;
using vll = vector<vector<ll>>;
using vcc = vector<vector<char>>;
using vs = vector<string>;
using Pi = pair<int , int>;
using Pl = pair<ll, ll>;
using vpi = vector<Pi>;
using vpl = vector<Pl>;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(n) for (ll i = 0; i < n; i++)
#define repint(i, n) for (int i = 0; i < n; i++)
#define rep2(i, s, n) for (int i = (s); i < n; i++)
#define REP2(s, n) for (int i = (s); i < n; i++)
#define rep3(i ,j, n, m) rep(i, n)rep(j, m)
#define REP3(n, m) rep(i, n)rep(j, m)
#define sort(A) sort(A.begin(),A.end());
#define reverse(A) reverse(A.begin(),A.end());
#define k(s) cout << fixed << setprecision(s);
const long double pi=3.14159265358979323846;
const long long MOD=1000000007;
int main() {
int n, m;
cin >> n >> m;
int t[2] = {0, 0};
while(n--) {
string s;
cin >> s;
int cc = 0;
for(char&c : s) {
cc ^= (c - '0');
}
t[cc]++;
}
cout << 1LL * t[0] * t[1];
} | #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define reps(i,s,n) for(int i=s;i<n;++i)
using ll = long long;
using namespace std;
int main()
{
int n;
cin >> n;
ll ans = 0;
rep(i, n)
{
ll a, b; cin >> a >> b;
ll s1 = b * (b + 1) / 2;
ll s2 = a * (a + 1) / 2;
ans += s1 - s2 + a;
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,k,m,sum(0),avg,ans(-1);
cin >> n >> k >> m;
for(int i = 0;i<n-1;i++){
cin >> avg;
sum += avg;
}
avg = sum/n;
if(m <= avg){
ans = 0;
}else if((m*n-sum) <= k){
ans = m*n - sum;
}
cout << ans << endl;
} | #include <stdio.h>
#include<iostream>
#include <string>
#include <string.h>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <climits>
#include <set>
#include <unordered_map>
#include <map>
#include <stack>
#include <unordered_set>
#define hash hassh
using namespace std;
int main() {
int T=1;
//cin>>T;
while(T--) {
int a,b,k;
cin>>a>>b>>k;
k*=1000;
if(k<a){
puts("UNSATISFIABLE");
continue;
}
int t=k/a;
if(t*b<k){
puts("UNSATISFIABLE");
continue;
}
swap(a,b);
printf("%d %d\n",k/a+(k%a>0),k/b);
}
return 0;
}
//注意检查边界
//注意考虑某些数组/向量可能为空 可能引起错误
//注意各种边界是0还是1
//注意少加不必要的限制条件 避免出错
//不要把前期题想得太难,尝试找简单结论 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf=1e18,mod=1e9+7;
ll fact[1000000],ifact[1000000];
ll power (ll a,ll b){
ll res=1;
while(b){
if(b&1)
res=(res*a)%mod;
b/=2;
a=(a*a)%mod;
}
return res;
}
ll ncr(ll n,ll r){
if(n<=r)
return 1;
ll res=1;
res=fact[n];
res=(res*ifact[n-r])%mod;
res=(res*ifact[r])%mod;
return res;
}
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 arr[3][n];
for(ll i=0;i<n;i++)
cin>>arr[0][i]>>arr[1][i]>>arr[2][i];
ll mi=inf;
for(ll i=0;i<n;i++){
if(arr[2][i]-arr[0][i]>0)
mi=min(mi,arr[1][i]);
}
if(mi==inf)
cout<<-1<<endl;
else
cout<<mi;
}
| #include <iostream>
using namespace std;
int main() {
int n,y,z,w,ab;
cin>>n;
cin>>y>>z>>w;
if(w>y) ab=z;
else ab=0;
for(int i=1;i<n;i++){
int A,P,X;
cin>>A>>P>>X;
if(X>A){
if(ab>P) ab=P;
if(ab==0)ab=P;
}
}
if(ab>0)cout<<ab;
else cout<<-1;
} |
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define ll long long
#define rep(i,N) for(ll i=0; i<N; i++)
const int mx = 2e5;
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main(void){
ll H,W,N,M;
cin >> H >> W >> N >> M;
int field[H+1][W+1];
int field2[H+1][W+1];
rep(i,H+1){
rep(j,W+1){
field[i][j] = 0;
field2[i][j]=0;
}
}
rep(i,N){//BOMB=N:1
int A,B;
cin >> A >> B;
field[A][B] = 1;
}
rep(i,M){//WALL=M:2
int C,D;
cin>> C >> D;
field[C][D] = 2;
}
vector<pair<int,int>> todo;
for(int i=1; i<=H; i++){
bool light = false;
for(int j=1; j<=W; j++){
if(field[i][j]==0){
todo.pb(mp(i,j));
}else if(field[i][j]==1){
todo.pb(mp(i,j));
light = true;
}else if(field[i][j]==2){
if(light){
for(auto& k: todo){
field2[k.first][k.second]+=3;
}
}
todo.clear();
light = false;
}
if(j==W){
if(light){
for(auto& k:todo){
field2[k.first][k.second]+=3;
}
}
todo.clear();
light=false;
}
}
}
for(int i=1; i<=W; i++){
bool light = false;
for(int j=1; j<=H; j++){
if(field[j][i]==0){
todo.pb(mp(j,i));
}else if(field[j][i]==1){
todo.pb(mp(j,i));
light = true;
}else if(field[j][i]==2){
if(light){
for(auto& k: todo){
field2[k.first][k.second]+=3;
}
}
todo.clear();
light = false;
}
if(j==H){
if(light){
for(auto& k:todo){
field2[k.first][k.second]+=3;
}
}
todo.clear();
light=false;
}
}
}
ll ans=0;
rep(i,H+1){
//cout << "\n";
rep(j,W+1){
//cout << field2[i][j] << " " ;
if(field2[i][j]>=3){
ans++;
}
}
}
cout << ans;
return 0;
} | #include<bits/stdc++.h>
#define forn(i, n) for(int i=0;i<(int)(n);i++)
#define for1(i, n) for(int i=1;i<=(int)(n);i++)
#define fore(i, l, r) for(int i=(int)(l);i<=(int)(r);i++)
#define ford(i, n) for(int i=(int)(n)-1;i>=0;i--)
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl "\n"
#define MAX 1000000007
using namespace std;
typedef int64_t ll;
typedef vector<int> vi;
typedef vector<int64_t> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
int main(int argc, char *argv[]){
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
string s;
cin>>n>>s;
int count=0;
int a,t,g,c;
for(int i=0;i<n-1;i++){
a=0;t=0;g=0;c=0;
for(int j=i;j<n;j++){
if(s[j]=='A') ++a;
if(s[j]=='T') ++t;
if(s[j]=='G') ++g;
if(s[j]=='C') ++c;
count+=(a==t)&&(g==c);
}
}
cout<<count;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
//UnionFindテンプレ
class UnionFind {
// まとめる 判定 サイズを知る
public:
// Aから見た親の番号を格納する。rootだったら-1*その集合のサイズ。
vector<int> Parent;
// 初期化
UnionFind(int N) {
Parent = vector<int>(N, -1);
}
// Aのrootを調べる
int root(int A) {
if (Parent[A] < 0) return A; // マイナスならそれはroot
return Parent[A] = root(Parent[A]);
}
// rootの値をプラスに戻して返す(サイズ)
int size(int A) {
return -Parent[root(A)];
}
// くっつける関数
bool connect(int A, int B) {
// AとBのroot同士をくっつける
A = root(A); // ここのAは、"rootの場所"の意味
B = root(B);
if (A == B) return false; // 既にくっついている
if (size(A) < size(B))
swap(A, B); // 大きい方にくっつけるために中身交換
Parent[A] += Parent[B]; // 中身更新
Parent[B] = A;
return true;
}
//連結か調べる関数
bool issame(int A, int B) {
return root(A) == root(B);
}
};
//chmin,chmax関数(DP用)
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
//aのZ/mZでの逆元
int64_t modinv(int64_t a, int64_t m) {
int64_t b = m, u = 1, v = 0;
while (b) {
int64_t t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
//最大公約数
int64_t gcd(int64_t a, int64_t b) {
if (a % b == 0) {
return b;
}
return gcd(b, a%b);
}
//エラストテレスのふるい
vector<bool> make_is_prime(int N) {
vector<bool> prime(N + 1, true);
if (N >= 0) prime[0] = false;
if (N >= 1) prime[1] = false;
for (int i = 2; i * i <= N; i++) {
if (!prime[i]) continue;
for (int j = i * i; j <= N; j += i) {
prime[j] = false;
}
}
return prime;
}
//累乗a^b
int64_t pow(int a, int b) {
int64_t ans = (int64_t)1;
for (int i = 0; i < b; i++) {
ans *= (int64_t)a;
}
return ans;
}
//ここから
int main() {
int N,S,D; cin>>N>>S>>D;
for (int i = 0; i < N; i++) {
int X,Y; cin>>X>>Y;
if (X < S && Y > D) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long llp;
#define mod 1000000007
#define deb(x) cerr << '\t' << "[" << #x << ": "<< x << "]\n";
#define f first
#define s second
#define pb push_back
/*
numeric_limits<llp>::max();
*/
void solution();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
llp tc = 1;
//cin >> tc;
for (llp t = 0; t < tc; ++t) {
//cout << "Case #" << tc+1 << ": ";
solution();
}
//intmax = 2e9 (ten digits)
return 0;
}
vector<int> seive(int n) {
/*Returns a vector<int> with all the prime numbers till n
* - Time complexity: O(nlglg(n))
* - n >= 2
* - if n is less returns a empty vector
*/
vector<bool> is_prime(n+1, 1);
is_prime[0] = is_prime[1] = 0;
for(int i = 2; i*i <= n; ++i) {
if(is_prime[i]) {
for(int j = i*i; j <= n; j+=i) is_prime[j] = 0;
}
}
vector<int> primes;
for(int i = 0; i < n+1; ++i) if(is_prime[i]) primes.push_back(i);
return primes;
}
int fx(int p, int n) {
int c = -1;
while(n) {
c++;
n/=p;
}
return c;
}
void solution() {
int n, s, d;
int x, y;
cin >> n >> s >> d;
bool canwe = 0;
for(int i = 0 ;i < n; ++i) {
cin >> x >> y;
if(x < s and y > d) canwe = 1;
}
cout << "No\0Yes\0"+3*canwe;
}
|
#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>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_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=998244353;
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'));
}
class union_find_tree{
private:
static constexpr int N=100005;
int n;
queue<int> q1;
vector<int> parent;
vector<int> num;
public:
void init(){
parent.assign(n,-1);
num.assign(n,1);
}
union_find_tree(int n_init){
assert(n_init>=0);
n=n_init;
init();
}
union_find_tree(){
n=N;
init();
}
int check_parent(int x){
assert(x>=0 && x<n);
if(parent[x]!=-1){
q1.push(x);
return check_parent(parent[x]);
}
int a;
while(!q1.empty()){
a=q1.front(),q1.pop();
parent[a]=x;
}
return x;
}
bool connect(int x,int y){
assert(x>=0 && x<n);
assert(y>=0 && y<n);
x=check_parent(x),y=check_parent(y);
if(x==y)return true;
if(num[x]>num[y])swap(x,y);
parent[x]=y;
num[y]+=num[x];
return false;
}
int size(int pos){
pos=check_parent(pos);
return num[pos];
}
bool check(int x,int y){//connect:true
assert(x>=0 && x<n);
assert(y>=0 && y<n);
return check_parent(x)==check_parent(y);
}
};
long long int pow_mod(long long int a,long long int n,long long int p=1e9+7){
//a^n mod p
long long int b=1,t=1;
for(;b<=n;b<<=1);
for(b>>=1;b>0;b>>=1){
t*=t;
if(t>=p)t%=p;
if(n&b)t*=a;
if(t>=p)t%=p;
}
return t;
}
namespace sol{
void solve(){
int n,m;
int i,j,k;
int a,b,c;
cin>>n;
union_find_tree ua(n);
for(i=0;i<n;i++){
cin>>a;
a--;
ua.connect(a,i);
}
int s=0;
for(i=0;i<n;i++){
if(ua.check_parent(i)==i)s++;
}
LL ans=pow_mod(2,s,MAX)-1;
if(ans<0)ans+=MAX;
cout<<ans<<endl;
}
}
int main(){
sol::solve();
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll z = 0, mod = 998244353;
ll ruijo(ll a, ll b) {
ll res = 1;
for (a %= mod; b; a = a * a % mod, b >>= 1)
if (b & 1) res = res * a % mod;
return res;
}
int main(){
ll N;
cin >> N;
vector<ll> f(N + 1);
for (ll i = 1; i <= N; i++){
cin >> f[i];
//f[i] = i;
}
vector<ll> par(N + 1, -1);
ll loopnum = 0;
for (ll i = 1; i <= N; i++){
if (par[i] > -1) continue;
ll now = i;
while (par[now] == -1){
par[now] = i;
now = f[now];
}
if (par[now] == i) loopnum++;
}
//cout << loopnum << endl;
cout << ruijo(2, loopnum) - 1 << endl;
} |
#include<bits/stdc++.h>
#define ll long long
#define rep(i,x,y) for(int i=x,i##end=y;i<=i##end;++i)
#define _rep(i,x,y) for(int i=x,i##end=y;i>=i##end;--i)
using pii=std::pair<int,int>;
using u32=unsigned;
using u64=unsigned ll;
inline bool up(int&x,const int y){return x<y?x=y,1:0;}
inline bool down(int&x,const int y){return x>y?x=y,1:0;}
#define N 1005
using vec=int[N];
vec dp,a,b,cpy;
vec l,r;
#define FAIL std::cout<<"No\n"
int main(){
std::ios::sync_with_stdio(0);
int n,cnt=0; std::cin>>n;
rep(i,1,n){
int x,y; std::cin>>x>>y;
if(x>0&&y>0&&x>y){
return FAIL,0;
}
if(x>0) l[x]=1,a[x]=std::max(y,0);
if(y>0) r[y]=1,b[y]=std::max(x,0);
cpy[++cnt]=x,cpy[++cnt]=y;
}
std::sort(cpy+1,cpy+cnt+1);
rep(i,2,cnt){
if(cpy[i]==cpy[i-1]&&cpy[i]>0&&cpy[i-1]>0){
return FAIL,0;
}
}
dp[0]=1;
rep(i,1,n) rep(j,0,i-1) if(dp[j]){
bool ty=1;
rep(k,j+1,i){
if((a[j+k]&&a[j+k]!=i+k)||l[i+k]||(b[i+k]&&b[i+k]!=j+k)||r[j+k])
ty=0;
if(l[j+k]&&r[i+k]&&a[j+k]==0&&b[i+k]==0) ty=0;
}
dp[i]|=ty;
}
std::cout<<(dp[n]?"Yes":"No")<<'\n';
return 0;
} | #include <iostream>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>
#include <complex>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < (long long int)(n); i++)
#define irep(i, n) for (long long int i = 1; i <= (long long int)(n); i++)
#define drep(i, n, diff) for (long long int i = 0; i < (long long int)(n); i += diff)
#define mod 1000000007
#define INF 1001001001001001011
#define ld(val) printf("%s : %lld\n", #val, val);
#define sd(val) printf("%s : %s\n", #val, val.c_str());
typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<P, P> PP;
typedef tuple<ll, ll, ll> TP;
int main() {
string x;
ll m;
cin >> x >> m;
ll md = 0;
rep(i, x.size()) {
ll t = x[i]-'0';
md = max(md, t);
}
if(x.length() == 1) {
if(x[0]-'0'<=m) cout << 1 << endl;
else cout << 0 << endl;
return 0;
}
ll l=md;
ll r=m+1;
while(r-l>1) {
ll v = (l+r)/2;
ll ret = 0;
for(ll i=0; i<x.size(); i++) {
ll t = x[i] - '0';
if (ret > m/v) {
ret = m+1;
break;
} else {
ret = (ret*v) + t;
}
}
if(ret>m) {
r=v;
} else {
l=v;
}
}
cout << l-md << endl;
return 0;
}
|
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
typedef long long ll;
typedef unsigned un;
typedef std::vector<int> P;
typedef std::pair<int,int> pii;
ll read(){ll x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();return f*x;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
template <typename T> bool umax(T& a,T t){if(t>a)return a=t,1;return 0;}
template <typename T> bool umin(T& a,T t){if(t<a)return a=t,1;return 0;}
/**********/
const int MAXN = 200011;
struct one{int t,x,y;}a[MAXN];
int fx[MAXN],diff=0;
int place(int val){return std::lower_bound(fx+1,fx+diff+1,val)-fx;}
struct sgt
{
struct node
{
ll cnt,sum;
}t[MAXN<<2|1];
#define rt t[num]
void modify(un pos,ll k,un l=1,un r=diff,un num=1)
{
rt.cnt+=k,rt.sum+=k*fx[pos];
if(l==r)return;
un mid=(l+r)>>1;
if(pos<=mid)modify(pos,k,l,mid,num<<1);
else modify(pos,k,mid+1,r,num<<1|1);
}
ll Qcnt(un ql,un qr,un l=1,un r=diff,un num=1)
{
if(ql>qr)return 0;
if(ql<=l&&r<=qr)return rt.cnt;
un mid=(l+r)>>1;
ll res=0;
if(ql<=mid)res+=Qcnt(ql,qr,l,mid,num<<1);
if(qr>mid)res+=Qcnt(ql,qr,mid+1,r,num<<1|1);
return res;
}
ll Qsum(un ql,un qr,un l=1,un r=diff,un num=1)
{
if(ql>qr)return 0;
if(ql<=l&&r<=qr)return rt.sum;
un mid=(l+r)>>1;
ll res=0;
if(ql<=mid)res+=Qsum(ql,qr,l,mid,num<<1);
if(qr>mid)res+=Qsum(ql,qr,mid+1,r,num<<1|1);
return res;
}
}t[2];
int seq[2][MAXN];
ll f[2][MAXN];
int main()
{
int n1=read(),n2=read(),m=read();
fx[++diff]=0;
for(int i=1;i<=m;++i)a[i].t=read()-1,a[i].x=read(),fx[++diff]=a[i].y=read();
std::sort(fx+1,fx+diff+1),diff=std::unique(fx+1,fx+diff+1)-fx-1;
t[0].modify(1,n1),t[1].modify(1,n2);
for(int i=1;i<=n1;++i)seq[0][i]=1;
for(int i=1;i<=n2;++i)seq[1][i]=1;
ll ans=0;
for(int i=1;i<=m;++i)
{
int tp=a[i].t,val=place(a[i].y);
int& pre=seq[tp][a[i].x];
///printf("%d to %d(%d)\n",pre,val,a[i].y);
if(pre<=val)
{
//printf("%d\n",a[i].y-fx[pre]);
ans+=t[!a[i].t].Qcnt(1,pre)*(a[i].y-fx[pre])+ t[!a[i].t].Qcnt(pre+1,val)*a[i].y-t[!a[i].t].Qsum(pre+1,val);
}
else
{
ans-=t[!a[i].t].Qcnt(1,val)*(fx[pre]-a[i].y)+ t[!a[i].t].Qcnt(val+1,pre)*fx[pre]-t[!a[i].t].Qsum(val+1,pre);
}
t[a[i].t].modify(pre,-1);
t[a[i].t].modify(pre=val,1);
printf("%lld\n",ans);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int ll
#define fi first
#define se second
#define Mp make_pair
#define pb push_back
typedef long long ll;
typedef double db;
typedef pair<int, int> pii;
typedef vector<int> vi;
mt19937 mrand(time(0));
#define debug(...) fprintf(stderr, __VA_ARGS__)
ll get(ll r) { return ((ll)mrand() * mrand() % r + r) % r; }
ll get(ll l, ll r) { return get(r - l + 1) + l; }
int n, m, dis[100100], head[100100], tot, v[100100];
struct Edge {
int to, nxt, c, d, sd;
int f(int t) {
t = max(t, sd - 1);
return c + d / (t + 1) + t;
}
} e[200100];
void addEdge(int a, int b, int c, int d) {
e[++tot] = (Edge){ b, head[a], c, d, (int)round(sqrt(d))}, head[a] = tot;
e[++tot] = (Edge){ a, head[b], c, d, (int)round(sqrt(d))}, head[b] = tot;
}
signed main() {
scanf("%lld %lld", &n, &m);
for(int i = 1, a, b, c, d; i <= m; i++) {
scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
addEdge(a, b, c, d);
}
memset(dis, 0x3f, sizeof(dis)); dis[1] = 0;
priority_queue<pii> q; q.push(pii(0, 1)); // <-val, pos>
while(q.size()) {
pii x = q.top(); q.pop();
if(v[x.se]) continue; v[x.se] = 1;
for(int i = head[x.se]; i; i = e[i].nxt) {
int y = e[i].to;
if(dis[y] > e[i].f(-x.fi)) {
dis[y] = e[i].f(-x.fi);
q.push(pii(-dis[y], y));
}
}
}
printf("%lld\n", dis[n] > 1e18 ? -1 : dis[n]);
debug("time=%.4f\n", (db)clock()/CLOCKS_PER_SEC);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i =0;i<(n);++i)
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
int main(){
int X,Y,Z;
cin >>X>>Y>>Z;
int tmp=Y*Z;
if(tmp%X==0)cout <<tmp/X-1;
else cout <<tmp/X;
} | #include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<string, int> psi;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
const int MOD = 1e9 + 7;
const long double PI = 3.141592653589793238462643383279502884197;
ll fac[1] = {1}, inv[1] = {1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll mp(ll a,ll b){ll ret=1;while(b){if(b&1)ret=ret*a%MOD;a=a*a%MOD;b>>=1;}return ret;}
ll cmb(ll r, ll c) {return (c>r||c<0) ? 0:fac[r] * inv[c] % MOD * inv[r - c] % MOD;}
int main() {
ll x,y,z;
scanf("%lld %lld %lld", &x, &y, &z);
ll mav = 0;
for (int i = 1; i <= 10000000; i++) {
// y/x > t/z
// x*t > z*y;
if (y*z > i*x) mav = max(mav, (ll)i);
}
printf("%lld", mav);
}
// author: rdd6584
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define N 200100
#define MOD 1000000007 //998244353
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep2(i, a, b) for(int i = a; i <= b; ++i)
#define rep3(i, a, b) for(int i = a; i >= b; --i)
#define eb emplace_back
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
struct Setup_io {
Setup_io() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
}
} setup_io;
int main() {
ll n, m,mm, x, y;
x = (ll)10;
y = (ll)1;
cin >> n >> m;
mm = m * m;
while (n > 0) {
if (n & 1)y = (y*x) % mm;
x = (x*x) % mm;
n /= 2;
}
y /= m;
cout << y << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
long long binpow(long long i, long long x, long long MOD)
{
long long rs=1;
long long a=x;
while(i)
{
if (i&1)
{
i--;
rs*=a;
rs%=MOD;
}
else
{
i/=2;
a*=a;
a%=MOD;
}
}
return rs%MOD;
}
void solve()
{
long long n,m;
cin >> n >> m;
long long MOD = m*m;
long long x = binpow(n,10,MOD);
//cout << x << "\n" ;
//x+=MOD;
x = (x-x%m)/m; //cout << x << "\n";
cout << x-(x%m)/m;
}
int main()
{
int t=1;
//cin >> t;
while (t--) solve();
}
|
#include <bits/stdc++.h>
//#include <atcoder/lazysegtree>
//#include <atcoder/segtree>
//#define rep(i,a,n) for (int i=a;i<n;i++)
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(ll i = 0; i < (n); ++i)
#define rep2(i, n) for(ll i = 0; i < (n); ++i)
#define rep3(i, a, b) for(ll i = (a); i < (b); ++i)
#define rep4(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define rrep(i,a,n) for (int i=n-1;i>=a;i--)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pause "read -p 'Press Enter to continue...' var"
using namespace std;
//using namespace atcoder;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
/*
テスト通りますように
●
/⌒ヽ
| |/⌒ヽ(ヽ
(` ∥ー⌒) |
| ̄|| ̄ ̄ ̄ ̄ ̄|
|―||―――――|
| U |
| ̄ ̄ ̄ ̄ ̄ ̄ ̄|
|_______|
|―――――|
|―――――|
wwWwwWwWWw
*/
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
const ll INF = numeric_limits<ll>::max()/4;
const ll MAX = 200005;
const int MOD = 3;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main(){
ll n,k;
cin >> n >> k;
vpll p;
rep(i,n){
ll a,b;
cin >> a >> b;
p.pb(mp(a,b));
}
sort(ALL(p));
ll ans = k;
rep(i,n){
if(p[i].fi <= ans) ans += p[i].se;
}
cout << ans << endl;
return 0;
}
| /* Goal to be a Master */
#include <algorithm>
#include <bits/stdc++.h>
#include<string>
using namespace std;
#define endl "\n"
#define fo1(i,n) for(int i=1;i<=n;i++)
#define min3(a,b,c) min(a,min(b,c))
#define s(v) (ll)v.size()
#define e(v) v.empty()
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vi vector<int>
#define ll long long
#define fi first
#define se second
#define db double
#define U unsigned
#define P pair<int,int>
#define vll vector<ll>
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fo(i,a,b) for(int i = a;i <= b;++i)
#define ro(i,a,b) for(int i = a;i >= b;--i)
#define ppll pair < pll , pll >
#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d %d",&x,&y)
#define sd3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sd4(x,y,z,u) scanf("%d %d %d %d",&x,&y,&z,&u)
#define sld(x) scanf("%lld",&x)
#define INF 1e18
#define eps 0.00001
#define le length
#define debug(n1) cout << n1 << "\n"
int binex(int base,int pow)
{
int res=1;
while(pow)
{
if(pow%2)
{ res*=base;pow--;}
else
{base*=base;pow=pow/2;}
}
return res;
}
void sieve(int a[],int N)
{
for(int i=1;i<=N;i++)
a[i]=1;
a[1]=0;
for(int i= 2;i*i<=N;i++)
{
if(a[i])
{
for(int j=i*i;j<=N;j+=i)
a[j]=0;
}
}
}
string CONVERT_TO_BINARY(int s) {
string res = "";
while(s != 0) {
res += (char)('0' + s % 2);
s /= 2;
}
reverse(res.begin() , res.end());
return res;
}
int STOI(string s) {
int num = 0;
int po = 1;
for(int i=s.length()-1;i>=0;i--)
{
num += po * (s[i] - '0');
po *= 10;
}
return num;
}
int main()
{
IOS
int n,m;
cin >> n >> m;
ll sum=0;
vector<pair<ll,int>> v;
for(int i=1;i<=n;i++)
{
ll x,y;
cin>>x>>y;
v.pb(mp(x,y));
}
sort(all(v));
ll curr=m;
//int prev=0;
/*for(int i=0;i<v.size();i++)
{
if(curr<(v[i].first-prev))
break;
curr-=(v[i].first-prev);
sum+=(v[i].first-prev);
prev=v[i].first;
curr+=v[i].second;
}*/
for(int i=0;i<v.size();i++)
{
if(v[i].first<=curr)
curr+=v[i].second;
}
cout<<sum+curr;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+7;
const LL MOD=1e9+7;
LL rec[maxn];
int main()
{
int len;
cin >> len;
string arr;
LL ans=1;
rec[3]=1;rec[4]=2;rec[2]==1;
for(int i=0;i<4;++i)
{
char tem; cin >> tem; arr+=tem;
}
if(len<4||arr[0]=='A'&&arr[1]=='A'||arr[1]=='B'&&arr[3]=='B'){cout << 1;return 0;}
for(int i=5;i<=len;i++)
rec[i]=(rec[i-1]+rec[i-2])%MOD;
for(int i=4;i<=len;++i)
ans=(ans<<1)%MOD;
if(arr=="BAAB"||arr=="BBBA"||arr=="ABBA"||arr=="BAAA")
{
cout <<rec[len]; return 0;
}
else
cout << ans;
return 0;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1003;
const ll MOD = int(1e9) + 7;
ll add(ll a, ll b) {
a %= MOD;
b %= MOD;
ll ans = (a + b) % MOD;
return ans;
}
ll sub(ll a, ll b) {
a %= MOD;
b %= MOD;
ll ans = (a - b + MOD) % MOD;
return ans;
}
ll mul(ll a, ll b) {
a %= MOD;
b %= MOD;
ll ans = (a * b) % MOD;
ans += MOD; // in case of negative result
ans %= MOD;
return ans;
}
// Iterative Function to calculate (x^y) % MOD in O(log y)
ll fastPow(ll x, ll y) {
ll res = 1;
x %= MOD;
while(y > 0) {
if(y % 2 == 1) {
res = mul(res, x);
}
y /= 2;
x = mul(x, x);
}
return res;
}
// Returns n^(-1) mod p (Fermat's little theorem)
ll modInverse(ll n) {
return fastPow(n, MOD - 2);
}
ll fact[N];
// Returns nCr % p using Fermat's little theorem.
ll comb(ll a, ll b) {
if(b == 0) return 1;
return mul(mul(fact[a], modInverse(fact[b])), modInverse(fact[a - b]));
}
void initFact() {
fact[0] = 1;
for(int i = 1; i < N; ++i) fact[i] = mul(ll(i), fact[i - 1]);
}
ll one(int n) {
ll ans = 0;
int top = n - 2, bot = 0;
while(top >= bot) {
ans = add(ans, comb(top, bot));
--top;
++bot;
}
return ans;
}
ll two(int n) {
return fastPow(ll(2), ll(n - 3));
}
int main() {
ios_base::sync_with_stdio(false);
initFact();
int n;
char caa, cab, cba, cbb;
cin >> n;
cin >> caa >> cab >> cba >> cbb;
if(n <= 3) {
cout << "1\n";
return 0;
}
/*
one: fibonacci, two: all possible strings (2^n)
AAAA -> 1
AAAB -> 1
AABA -> 1
AABB -> 1
ABAA -> two
ABAB -> 1
ABBA -> one
ABBB -> 1
BAAA -> one
BAAB -> one
BABA -> two
BABB -> two
BBAA -> two
BBAB -> 1
BBBA -> one
BBBB -> 1
*/
// invariant: all generated string starts with A, ends with AB pattern
if(cab == 'A') {
// A..AB
if(caa == 'A') {
cout << "1\n"; // only A...AB (no change)
} else {
// A..ABA..ABA..AB (can insert 1 B between the As)
if(cba == 'A') {
cout << one(n) << "\n"; // only A...ABA...ABA...AB (no change)
} else {
// A..AB..BA..AB..BA..AB (can insert multiple Bs)
// whether cbb is A or B, the pattern will not change
cout << two(n) << "\n";
}
}
} else {
// AB...B
if(cbb == 'B') {
cout << "1\n"; // only AB...B (no change)
} else {
// AB..BAB..BAB..BAB (can insert 1 A between the Bs)
if(cba == 'B') {
cout << one(n) << "\n"; // only AB..BAB..BAB..BAB (no change)
} else {
// AB..BA..AB..BA..AB..BA..AB (can insert multiple As)
// whether caa is A or B, the pattern will not change
cout << two(n) << "\n";
}
}
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007
#define ll_MAX 19223372036859
#define endl "\n"
ll fast_expo(ll x,ll p){
if(p==0) return 1;
else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;}
else return (x*(fast_expo(x,p-1))%MOD)%MOD;}
ll dp[1005][1005], n, m, arr1[1005], arr2[1005];
vector<ll> AdjL[1005];
int main(){
fast
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t_c; t_c = 1;
// cin >> t_c;
for(ll t_i = 0; t_i < t_c; ++t_i){
ll n, m; cin >> n >> m;
ll a[n + 1], b[m + 1];
for(ll i = 1; i <= n; ++i)
cin >> a[i];
for(ll i = 1; i <= m; ++i)
cin >> b[i];
ll dp[n + 1][m + 1];
for(ll j = 0; j <= m; ++j){
dp[0][j] = j;
}
for(ll i = 1; i <= n; ++i){
dp[i][0] = i;
for(ll j = 1; j <= m; ++j){
if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1];
else dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1});
}
}
cout << dp[n][m];
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}
ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
struct Frac {
// a/b
ll a, b;
};
const int mod = 1e9 + 7;
Frac mul_f(Frac x, Frac y) {
ll a = x.a * y.a % mod;
ll b = x.b * y.b % mod;
return (Frac){a, b};
}
Frac plus_f(Frac x, Frac y) {
ll a = (x.a * y.b) % mod + (x.b * y.a) % mod;
ll b = x.b * y.b % mod;
return (Frac){a, b};
}
ll calc(Frac x) {
return x.a * mod_pow(x.b, mod - 2, mod) % mod;
}
vector<vector<Frac>> mul(vector<vector<Frac>> &A, vector<vector<Frac>> &B) {
int N = A.size(), M = B[0].size(), K = A[0].size();
vector<vector<Frac>> C(N, vector<Frac>(K, {0, 1}));
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
Frac sum = {0, 1};
for(int k = 0; k < K; k++) {
sum = plus_f(sum, mul_f(A[i][k], B[k][j]));
}
C[i][j] = sum;
}
}
return C;
}
vector<vector<Frac>> retE(int n) {
vector<vector<Frac>> ret(n, vector<Frac>(n, {0, 1}));
for (int i = 0; i < n; i++) {
ret[i][i] = {1, 1};
}
return ret;
}
vector<vector<Frac>> pow(vector<vector<Frac>> &A, int n) {
if (n == 0) {
return retE(A.size());
}
vector<vector<Frac>> ret = retE(A.size());
if (n % 2 == 1) {
ret = mul(ret, A);
}
vector<vector<Frac>> half = pow(A, n / 2);
ret = mul(ret, half);
ret = mul(ret, half);
return ret;
}
void solve() {
int N, M, K;
cin >> N >> M >> K;
vector<vector<Frac>> A(N, vector<Frac>(1, {0, 1}));
for (int i = 0; i < N; i++) {
int a;
cin >> a;
A[i][0] = {a, 1};
}
vector<vector<int>> G(N, vector<int>(N));
vector<int> V(N);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
a--, b--;
V[a]++; V[b]++;
G[a][b] = 1;
G[b][a] = 1;
}
vector<vector<Frac>> T(N, vector<Frac>(N, {0, 1}));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) {
ll a = 2 * (M - V[i]) + V[i];
ll b = 2 * M;
T[i][j] = {a, b};
} else {
if (G[i][j]) {
T[i][j] = {1, 2 * M};
}
}
}
}
vector<vector<Frac>> TT = pow(T, K);
vector<vector<Frac>> ans = mul(TT, A);
for (int i = 0; i < N; i++) {
cout << calc(ans[i][0]) << endl;
}
return;
}
int main() {
#ifdef LOCAL_ENV
cin.exceptions(ios::failbit);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
solve();
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int,int>;
int main()
{
int n, s, d;
cin >> n >> s >> d;
rep(i, n){
int x, y;
cin >> x >> y;
if(x < s && y > d){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
if(a!=b && a==c){
cout<<b<<endl;
}
else if(a==b && a==c){
cout<<a<<endl;
}else if(a!=b && b!= c){
cout<<0<<endl;
}
else if(a!=b && a!=c){
cout<<a<<endl;
}else{
cout<<c<<endl;
}
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define PI 3.14159265
// 🍪 DEBUG FUNCTIONS START
#define cerr cout
void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
//#define deb(x...) cerr << "[" << #x << "] = "; _print(x)
#define deb(x...) _print(x)
#else
#define deb(x...)
#endif
// 🍪 DEBUG FUNCTIONS END
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
void solve()
{
int n, k;
cin >> n >> k;
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= k; j++){
int sum = i * 100 + j;
ans += sum;
}
}
cout << ans << "\n";
}
signed main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t;
t=1;
while(t--)
{
solve();
}
} | #include <bits/stdc++.h>
//#include <bits/extc++.h>
//#include <immintrin.h>
using namespace std;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
for (auto &i : a)
in >> i;
return in;
}
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
for (auto &i : a) {
out << i << " ";
}
return out;
}
template<typename T, typename D>
istream &operator>>(istream &in, pair<T, D> &a) {
in >> a.first >> a.second;
return in;
}
template<typename T, typename D>
ostream &operator<<(ostream &out, const pair<T, D> &a) {
out << a.first << " " << a.second;
return out;
}
struct LogOutput {
template<typename T>
LogOutput &operator<<(const T &x) {
#ifdef DIVAN
cout << x;
#endif
return *this;
}
} dout, fout;
typedef long long ll;
typedef unsigned long long ull;
typedef double dl;
typedef complex<double> cd;
#define nl '\n'
#define elif else if
#define all(_v) _v.begin(), _v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) (int)(v.size())
#define sqr(_v) ((_v) * (_v))
#define vpi vector<pair<int, int>>
#define eb emplace_back
#define pb push_back
#define mod(x, m) ((x) >= 0 ? ((x) % m) : ((((x) % m) + m) % m))
#define vi vector<int>
#define pi pair<int, int>
#define ti tuple<int, int, int>
#define minq(x, y) x = min((x), (y))
#define maxq(x, y) x = max(x, (y))
#define forn(i, n) for (int i = 0; i < (n); ++i)
const ll INFL = 9187201950435737471;
const ll nINFL = -9187201950435737472;
const int INF = 2139062143;
const int nINF = -2139062144;
const ull ULINF = numeric_limits<ull>::max();
const long double PI = acos(-1);
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(0);
inline void IO() {
#ifdef DIVAN
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
}
#define int ll
const int MAXN = 3e6 + 100;
int sum2[MAXN], pSum2[MAXN], sum3[MAXN];
void Solve() {
int n, k;
cin >> n >> k;
for (int a = 1; a <= n; ++a) {
sum2[a + 1]++;
sum2[a + n + 1]--;
}
int res = 0;
for (int i = 2; i < MAXN; ++i) {
res += sum2[i];
sum2[i] = res;
}
for (int i = 1; i < MAXN; ++i) {
pSum2[i] = pSum2[i - 1] + sum2[i];
}
for (int i = 3; i <= 3 * n; ++i) {
int l = max(2ll, i - n), r = min(2 * n, i - 1);
assert(l <= r);
sum3[i] = pSum2[r] - pSum2[l - 1];
}
int needSum = 3;
while(k > sum3[needSum]) {
k -= sum3[needSum];
needSum++;
}
int a = 1;
while(k > sum2[needSum - a]) {
k -= sum2[needSum - a];
++a;
}
for (int b = 1; b <= n; ++b) {
int c = needSum - a - b;
if (c > n) {continue;}
if (k == 1) {
cout << a << ' ' << b << ' ' << c;
return;
}
--k;
}
}
signed main() {
IO();
int t = 1;
// cin >> t;
int startTime = clock();
for (int i = 1; i <= t; ++i) {
// cout << "Case #" << i << ": ";
Solve();
}
int endTime = clock();
fout << '\n' << "Time: " << (endTime - startTime + 999) / 1000;
return 0;
}
|
#include <iostream>
#include <iomanip> //<<setprecision(12), or <<fixed
#include <algorithm>
#include <functional>
#include <map>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pll=pair<ll,ll>;
int main()
{
ll A,B,C;
cin>>A>>B>>C;
vector<vector<vector<double>>> dp(101,vector<vector<double>>(101,vector<double>(101,-1)));
for(ll i=0; i<=100; i++)
{
for(ll k=0; k<=100; k++)
{
dp[100][i][k]=0;
dp[i][100][k]=0;
dp[i][k][100]=0;
}
}
auto e=[&](auto& e, ll a, ll b, ll c)
{
if(dp[a][b][c]!=-1) return dp[a][b][c];
double res=(e(e,a+1,b,c)+1)*a/(double)(a+b+c)
+(e(e,a,b+1,c)+1)*b/(double)(a+b+c)
+(e(e,a,b,c+1)+1)*c/(double)(a+b+c);
dp[a][b][c]=res;
return res;
};
cout<<fixed<<setprecision(15)<<e(e,A,B,C)<<endl;
// Fear misreading.
return 0;
}
| #include<bits/stdc++.h>
#define ST first
#define ND second
#define ll long long
#define ld long double
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using namespace std;
const ll INF = 1e9 + 9;
//const ll MOD = 1e9 + 7;
const ll MOD = 998244353;
const long long LINF = (ll)1e18 + 3;
// secik.insert({x, t++});
// secik.erase(secik.lower_bound({x,-1}));
// *secik.find_by_order(x)).first << "\n";
// secik.order_of_key(x)
//random_device device;
//mt19937 gener(device());
//uniform_int_distribution<ll > gen(0,n-1);
//gen(gener); // generate random number
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll A,B,C; cin >> A >> B >> C;
ll x = A * (A+1)/2, y = B * (B+1)/2, z = C * (C+1)/2;
x %= MOD;
y %= MOD;
z %= MOD;
cout << max(((x * y) % MOD * z) % MOD,(((x * y) % MOD * z) % MOD + MOD) % MOD);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int h[30][29], v[29][30], hc[30][29], vc[29][30];
int si, sj, ti, tj, prev_si, prev_sj, prev_ti, prev_tj;
int prev_result;
int i=0, j=0;
int border=3;
int repeat_count=1000;
string query(int si, int sj, int ti, int tj){
string path;
while(si!=ti || sj!=tj){
if(si==ti){
if(sj<tj){
path=path+"R";
sj++;
}
else{
path=path+"L";
sj--;
}
}
else if(sj==tj){
if(si<ti){
path=path+"D";
si++;
}
else{
path=path+"U";
si--;
}
}
//左上
else if(si<ti && sj<tj){
if(hc[si][sj]<border || vc[si][sj]<border){
if(hc[si][sj]<vc[si][sj]){
path=path+"R";
sj++;
}
else{
path=path+"D";
si++;
}
}
else if(h[si][sj]<v[si][sj]){
path=path+"R";
sj++;
}
else{
path=path+"D";
si++;
}
}
//左下
else if(si>ti && sj<tj){
if(hc[si][sj]<border || vc[si-1][sj]<border){
if(hc[si][sj]<vc[si-1][sj]){
path=path+"R";
sj++;
}
else{
path=path+"U";
si--;
}
}
else if(h[si][sj]<v[si-1][sj]){
path=path+"R";
sj++;
}
else{
path=path+"U";
si--;
}
}
//右下
else if(si>ti&& sj>tj){
if(hc[si][sj-1]<border || vc[si-1][sj]<border){
if(hc[si][sj-1]<vc[si-1][sj]){
path=path+"L";
sj--;
}
else{
path=path+"U";
si--;
}
}
else if(h[si][sj-1]<v[si-1][sj]){
path=path+"L";
sj--;
}
else{
path=path+"U";
si--;
}
}
//右上
else{
if(hc[si][sj-1]<border || vc[si][sj]<border){
if(hc[si][sj-1]<vc[si][sj]){
path=path+"L";
sj--;
}
else{
path=path+"D";
si++;
}
}
else if(h[si][sj-1]<v[si][sj]){
path=path+"L";
sj--;
}
else{
path=path+"D";
si++;
}
}
}
return path;
}
void sethv(int prev_si, int prev_sj, string prev_path, int prev_result){
int delta=(int)round(prev_result/prev_path.length())-5000;
while(prev_path.empty()==0){
if(prev_path[0]=='R'){
h[prev_si][prev_sj]=(int)round((h[prev_si][prev_sj]*hc[prev_si][prev_sj]+delta)/(hc[prev_si][prev_sj]+1));
hc[prev_si][prev_sj]++;
prev_sj++;
}
else if(prev_path[0]=='D'){
v[prev_si][prev_sj]=(int)round((v[prev_si][prev_sj]*vc[prev_si][prev_sj]+delta)/(vc[prev_si][prev_sj]+1));
vc[prev_si][prev_sj]++;
prev_si++;
}
else if(prev_path[0]=='L'){
h[prev_si][prev_sj-1]=(int)round((h[prev_si][prev_sj-1]*hc[prev_si][prev_sj-1]+delta)/(hc[prev_si][prev_sj-1]+1));
hc[prev_si][prev_sj-1]++;
prev_sj--;
}
else{
v[prev_si-1][prev_sj]=(int)round((v[prev_si-1][prev_sj]*vc[prev_si-1][prev_sj]+delta)/(vc[prev_si-1][prev_sj]+1));
vc[prev_si-1][prev_sj]++;
prev_si--;
}
prev_path.erase(0, 1);
}
}
void read_s_t(){
cin>>si;
cin>>sj;
cin>>ti;
cin>>tj;
}
void set_prev_s_t(){
prev_si=si;
prev_sj=sj;
prev_ti=ti;
prev_tj=tj;
}
void read_result(){
cin>>prev_result;
}
int main(){
//h初期化
for(i=0; i<=29; i++){
for(j=0; j<=28; j++){
h[i][j]=0;
hc[i][j]=0;
}
}
//v初期化
for(i=0; i<=28; i++){
for(j=0; j<=29; j++){
v[i][j]=0;
vc[i][j]=0;
}
}
for(i=0; i<repeat_count; i++){
read_s_t();
set_prev_s_t();
string path=query(si, sj, ti, tj);
cout<<path<<"\n"<<flush;
read_result();
sethv(prev_si, prev_sj, path, prev_result);
}
return 0;
} |
#include <cstdio>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
typedef long long ll;
const int N = 30;
const int MIN_COST = 1000;
// const int MAX_COST = 9000;
const int nQuery = 1000;
enum {
D = 0, L, U, R
};
const int DI[4] = {1, 0, -1, 0};
const int DJ[4] = {0, -1, 0, 1};
const char * const OP = "DLUR";
struct Result {
int dist, num;
Result(int dist, int num) : dist(dist), num(num) {}
double average(void) const { return double(dist) / num; }
};
class Edge {
public:
Edge() {
// 最低値で初期化しておく
const double EFF = 140;
numer = MIN_COST / EFF;
denom = 1 / EFF;
}
void addResult(const Result &r) {
numer += r.average() / r.num;
denom += 1.0 / r.num;
rs.push_back(r);
}
double cost(void) const {
return numer / denom;
}
private:
double numer, denom;
vector<Result> rs;
};
Edge _es[(N-1)*N*2];
Edge *es[N][N][4];
inline void init(void) {
int t = 0;
REP(i, N) REP(j, N) {
if(i+1 < N) {
es[i][j][D] = &_es[t++];
}
if(j+1 < N) {
es[i][j][R] = &_es[t++];
}
}
REP(i, N) REP(j, N) {
if(i-1 >= 0) {
es[i][j][U] = es[i-1][j][D];
}
if(j-1 >= 0) {
es[i][j][L] = es[i][j-1][R];
}
}
assert(t == (N-1)*N*2);
}
inline int query(const vector<int> &ops) {
for(auto op : ops) {
printf("%c", OP[op]);
}
printf("\n");
fflush(stdout);
int res;
scanf("%d", &res);
return res;
}
const double INF = 1e9;
double dist[N][N];
int trace[N][N];
struct K {
int i, j;
double c;
K(int i, int j, double c) : i(i), j(j), c(c) {}
};
bool operator>(const K& a, const K& b) {
return a.c > b.c;
}
int main(void) {
init();
REP(iQuery, nQuery) {
int si, sj, gi, gj;
scanf("%d%d%d%d", &si, &sj, &gi, &gj);
{ // dijkstra
REP(i, N) REP(j, N) {
dist[i][j] = INF;
}
priority_queue<K, vector<K>, greater<K> > q;
q.push(K(si, sj, 0));
dist[si][sj] = 0;
while(!q.empty()) {
K cur = q.top(); q.pop();
int ci = cur.i;
int cj = cur.j;
if(dist[ci][cj] < cur.c) {
continue;
}
if(ci == gi && cj == gj) {
break;
}
REP(d, 4) {
int ni = ci + DI[d];
int nj = cj + DJ[d];
if(ni >= 0 && nj >= 0 && ni < N && nj < N) {
double c = cur.c + es[ci][cj][d]->cost();
if(c < dist[ni][nj]) {
q.push(K(ni, nj, c));
dist[ni][nj] = c;
trace[ni][nj] = d;
}
}
}
}
}
vector<int> ops;
for(int ci = gi, cj = gj; ci != si || cj != sj; ) {
int op = trace[ci][cj];
ops.push_back(op);
ci -= DI[op];
cj -= DJ[op];
}
reverse(ops.begin(), ops.end());
int res = query(ops);
{
int ci = si;
int cj = sj;
for(auto op : ops) {
es[ci][cj][op]->addResult(Result(res, ops.size()));
ci += DI[op];
cj += DJ[op];
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d\n", &n);
vector<int> a(n), b(n), c(n);
vector<long long> d(n + 1, 0), e(n + 1, 0), f(n + 1, 0);
for(int i = 0; i < n; i++) {
scanf("%d", &a.at(i));
d.at(a.at(i))++;
}
for(int i = 0; i < n; i++) {
scanf("%d", &b.at(i));
e.at(b.at(i))++;
}
for(int i = 0; i < n; i++) {
scanf("%d", &c.at(i));
f.at(b.at(c.at(i) - 1))++;
}
long long sum = 0;
for(int i = 1; i <= n; i++) {
sum += d.at(i) * f.at(i);
}
printf("%lld\n", sum);
} | #include <bits/stdc++.h>
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define dbgpr(x) cout << #x << " is " << x << endl
#define endl '\n'
using namespace std; using ll = long long; using ii = pair<int,int>;
void solution(); int main() {ios::sync_with_stdio(0); cin.tie(0); solution();}
const int N = 100003;
int n, a[N], b[N], c[N];
int cnt[N];
void input() {
cin >> n;
REP(i,1,n) cin >> a[i];
REP(i,1,n) cin >> b[i];
REP(i,1,n) cin >> c[i];
}
void solution() {
input();
REP(i,1,n) ++cnt[b[c[i]]];
ll ans = 0;
REP(i,1,n) {
ans += cnt[a[i]];
}
cout << ans;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << (a*d)-(b*c);
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vp;
typedef set<ll> si;
typedef map<string, ll> msi;
#define REP(i,a,b) \
for(ll i=ll(a);i<=ll(b);i++)
#define DEP(i,a,b) \
for(ll i=ll(a);i>=ll(b);i--)
#define TR(c, it) \
for (auto it = (c).begin(); it != (c).end(); it++)
#define mp make_pair
#define pb push_back
#define F first
#define S second
ll mod = 1e9+7;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
T=1;
while(T--)
{
ll n,i,j,k,l,m,v,t,s,d;
cin>>v>>t>>s>>d;
if(v*t<=d&&s*v>=d)cout<<"No";
else cout<<"Yes";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
string S, ret = "", tmp; cin >> S;
for (auto s : S) {
switch (s)
{
case '0': tmp = '0'; break;
case '1': tmp = '1'; break;
case '6': tmp = '9'; break;
case '8': tmp = '8'; break;
case '9': tmp = '6'; break;
}
ret = tmp + ret;
}
cout << ret << endl;
return 0;
} | #include <iostream>
#include<stdio.h>
#include <cmath>
using namespace std;
int main(){
int num;
cin>>num;
if(num%2==0){
cout<<"White";
}else{ cout<<"Black";
}
} |
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl "\n"
#define ll long long
#define pb push_back
#define ff first
#define ss second
int main()
{
fast;
int a[3];
for (int i = 0; i < 3; ++i)
{
cin >> a[i];
}
sort(a, a + 3);
if (2 * a[1] == a[0] + a[2]) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Yes_cout cout <<"Yes"<<endl;
#define No_cout cout <<"No"<<endl;
int main() {
vector<int> v(4);
for (int i = 0; i < 4; i++) {
cin >> v.at(i);
}
int x;
x = v[0]*v[3]-v[1]*v[2];
cout << x <<endl;
}
|
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main(){
long long a, b, c, d;
cin >> a >> b >> c >> d;
long long minv = a;
minv = min(minv, b);
minv = min(minv, c);
minv = min(minv, d);
cout << minv << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int v, t, s, d;
cin >> v >> t >> s >> d;
if(d<v*t || d>v*s)
cout << "Yes" << endl;
else
cout << "No" << endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define F first
#define se second
#define pb push_back
#define SZ(x) x.size()
#define all(v) ((v).begin()), ((v).end())
#define lp(i ,n) for(int i=0;i<(int)(n);++i)
#define S(n) scanf("%d" , &n);
#define SL(n) scanf("%lld" , &n);
#define S2(n , m) scanf("%d %d" , &n , &m);
#define SL2(n , m) scanf("%lld %lld" , &n , &m);
#define dbg1(a) std::cerr<<#a<<"= "<<(a)<<"\n"
#define dbg2(a,b) std::cerr<<#a<<"= "<<(a)<<" "<<#b<<"= "<<(b)<<"\n"
#define dbg3(a,b,c) std::cerr<<#a<<"= "<<(a)<<" "<<#b<<"= "<<(b)<<" "<<#c<<"="<<(c)<<"\n"
#define dbg4(a,b,c,d) std::cerr<<#a<<"= "<<(a)<<" "<<#b<<"= "<<(b)<<" "<<#c<<"="<<(c)<<" "<<#d<<"= "<<(d)<<"\n"
#define Fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
typedef long long LL;
int const N = 1e5+3;
int const mod = 1e9+7;
int dx[]= {-1, 1, 0, 0};
int dy[]= {0, 0, 1, -1};
int n ;
LL ans;
int main()
{
S(n);
vector<pair<double, double>>v(n);
lp(i, n)scanf("%lf %lf",&v[i].F, &v[i].se);
lp(i, n)
{
for(int j = i+1 ; j<n ; j++)
ans += ((v[i].se - v[j].se) /(v[i].F - v[j].F)) >=-1 && ((v[i].se - v[j].se) /(v[i].F - v[j].F))<=1;
}
printf("%lld",ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std ;
#define ios ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr)
#define f first
#define s second
#define endl '\n'
typedef long long int ll ;
void solve() {
int n ;
cin >> n ;
pair <double , double> xy[n] ;
for (int i = 0 ; i < n ; i++) cin >> xy[i].f >> xy[i].s ;
int ans = 0 ;
for (int i = 0 ; i < n ; i++) {
for (int j = i + 1; j < n ; j++) {
double slope = xy[j].s - xy[i].s ;
slope /= (xy[j].f - xy[i].f) ;
if (slope >= -1 && slope <= 1) ans++ ;
}
}
cout << ans << endl ;
}
int main() {
ios ;
solve() ;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#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 DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}
typedef long long ll;
#define int ll
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl;
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {-1, 0, 1, 0}, dy = {0, -1, 0, 1};
// vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
// const ll MOD = 1000000007;
const ll MOD = 998244353;
// #define mp make_pair
//#define endl '\n'
signed main() {
fio();
ll a, b, c;
cin >> a >> b >> c;
vl t = {a, b, c};
ll ans = 1;
for (ll x: t) {
ans *= x * (x + 1) / 2 % MOD;
ans %= MOD;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 998244353;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll a, b, c;
cin >> a >> b >> c;
a = (a * (a + 1) / 2) % mod;
b = (b * (b + 1) / 2) % mod;
c = (c * (c + 1) / 2) % mod;
cout << (((a * b) % mod) * c) % mod;
return 0;
} |
#include <bits/stdc++.h>
//#include <atcoder/all>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a - 1; i >= 0; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a - 1; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll INF = 1LL << 60;
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
template <class T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template <typename T>
T mypow(T x, T n, const T &p = -1)
{ //x^nをmodで割った余り
T ret = 1;
while (n > 0)
{
if (n & 1)
{
if (p != -1)
ret = (ret * x) % p;
else
ret *= x;
}
if (p != -1)
x = (x * x) % p;
else
x *= x;
n >>= 1;
}
return ret;
}
ll gcd(ll n, ll m)
{
ll tmp;
while (m != 0)
{
tmp = n % m;
n = m;
m = tmp;
}
return n;
}
ll lcm(ll n, ll m)
{
return abs(n) / gcd(n, m) * abs(m); //gl=xy
}
using namespace std;
//using namespace atcoder;
struct UF {
vector<int>par;//親
vector<int>sz;//要素数
UF(int n){
sz.resize(n,1);
par.resize(n);
for(int i=0;i<n;++i){
par[i]=i;
}
}
//木の根を求める
int find(int x) {
if (par[x] == x) {
return x;
}
else {
return par[x] = find(par[x]);//経路圧縮
}
}
//xとyの属する集合を併合
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)return false;//すでに同じ集合に属している
if (sz[x] < sz[y]) {//要素数が小さい方を下に
par[x] = y;
sz[y]+=sz[x];
}
else {
par[y] = x;
sz[x]+=sz[y];
}
return true;
}
//xとyが同じ集合に属するかを判定
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int k){//要素数を求める
return sz[find(k)];
}
};
void solve()
{
int n;
cin>>n;
vector<double>x(n),y(n);
rep(i,n)cin>>x[i]>>y[i];
double l=0,r=101;
rep(q,100){
double mid=(l+r)/2;
UF tree(n);
rep(i,n){
REP(j,i+1,n){
double dis=pow(x[i]-x[j],2)+pow(y[i]-y[j],2);
if(mid*mid*4>dis){
tree.unite(i,j);
}
}
}
vector<double>u(n),d(n);
rep(i,n){
chmax(u[tree.find(i)],min(100.0,y[i]+mid*2));
chmin(d[tree.find(i)],max(-100.0,y[i]-mid*2));
}
bool ng=false;
rep(i,n){
if(u[i]==100.0&&d[i]==-100.0){
ng=true;
}
}
if(ng)r=mid;
else l=mid;
}
cout<<l;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
solve();
return 0;
}
| //#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int num[8210];
int kazu[8210];
const int ma = (1 << 13) - 1;
int query(int n) {
int n2 = (n & ma) + 1;
while (num[n2]) {
if (num[n2] == n + 1) return kazu[n2];
n2 = n2 * 2 % 8209;
}
return 0;
}
void add(int n, int b) {
int n2 = (n & ma) + 1;
while (num[n2]) {
if (num[n2] == n + 1) return;
n2 = n2 * 2 % 8209;
}
num[n2] = n + 1;
kazu[n2] = b;
}
int li[300];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
int w[8];
rep(i, N) cin >> w[i];
pair<int, int> lv[100001];
rep(i, M) {
int l, v;
cin >> l >> v;
lv[i] = { v, l };
}
sort(lv, lv + M);
lv[M] = { 1e9,1e9 };
rep(i, 1 << N) {
rep(j, N) if (i >> j & 1) li[i] += w[j];
}
sort(li, li + (1 << N));
int saidai = 0;
int k = 0;
rep(i, 1 << N) {
while (lv[k].first < li[i]) {
if (saidai < lv[k].second) saidai = lv[k].second;
k++;
}
add(li[i], saidai);
}
rep(i, N) {
if (w[i] > lv[0].first) {
printf("-1");
return 0;
}
}
int P[8];
rep(i, N) P[i] = i;
int kotae = 1e9;
do {
int tmp[8] = {};
rep(p, N - 1) {
int omosa = w[P[p]];
for (int q = p + 1; q < N; q++) {
omosa += w[P[q]];
int kari = tmp[p] + query(omosa);
if (tmp[q] < kari) tmp[q] = kari;
}
}
if (kotae > tmp[N - 1]) kotae = tmp[N - 1];
} while (next_permutation(P, P + N));
printf("%d\n", kotae);
Would you please return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define po pop_front
#define mo(o) memset(o,-1,sizeof(o))
#define forn(i,n) for(int i=0;i<n;i++)
#define fore(i,n) for(int i=1;i<=n;i++)
#define mp make_pair
#define ll long long
#define itn int
int main()
{
int n;
cin>>n;
if(n%2) cout<<"Black"<<endl;
else cout<<"White"<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
if (N % 2 == 0){
cout << "White";
} else{
cout << "Black";
}
return 0;
} |
//#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#include<string.h>
#define un unsigned
#define srd srand(time(0))
#define ll long long
#define con continue
#define gtc getchar()
#define ptc putchar
#define dou double
#define eps 0.00000000001
#define opr operator
#define cl(x,a) memset(x,a,sizeof(x))
#define fo0(i,k) for(i=fr[k];i;i=nx[i])
#define fo1(i,l,r) for(i=l;i<=r;i++)
#define fo2(i,l,r) for(i=l;i>=r;i--)
#define fo(i,n) for(i=1;i<=n;i++)
#define ret return
#define x first
#define cint const int
#define y second
#define opi(x) freopen(x,"r",stdin)
#define opo(x) freopen(x,"w",stdout)
#define tpl template<class T>
#define priq priority_queue
#define mp make_pair
#define use using namespace
#define WT while(T--)
#define pb push_back
#define sz size()
use std;
typedef pair<int,int> pii;typedef pair<int,ll> pil;typedef pair<ll,int> pli;typedef pair<ll,ll> pll;
namespace io
{
void _(int &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;}
void _(ll &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;}
void _(char &c){while((c=gtc)==' '||c=='\n');}void _(dou &c){scanf("%lf",&c);}void _(char *s){char c;while((c=gtc)!=EOF&&c!=' '&&c!=10)*s++=c;}
template<class t1,class t2>void _(t1 &a,t2 &b){_(a);_(b);}template<class t1,class t2,class t3>void _(t1 &a,t2 &b,t3 &c){_(a);_(b);_(c);}
template<class t1,class t2,class t3,class t4>void _(t1 &a,t2 &b,t3 &c,t4 &d){_(a);_(b);_(c);_(d);}
template<class t1,class t2,class t3,class t4,class t5>void _(t1 &a,t2 &b,t3 &c,t4 &d,t5 &e){_(a);_(b);_(c);_(d);_(e);}
void _p(dou k){printf("%.6lf",k);}void _p(char *c){for(;*c;ptc(*c++));}void _p(const char *c){for(;*c;ptc(*c++));}void _p(char c){ptc(c);}
tpl void _p0(T k){if(k>=10)_p0(k/10);ptc(k%10+'0');}tpl void _p(T k){if(k<0){ptc('-');_p0(-k);}else _p0(k);}tpl void __p(T k){_p(k);ptc(' ');}
tpl void _pn(T k){_p(k);ptc('\n');}template<class t1,class t2>void _p(t1 a,t2 b){__p(a);_pn(b);}
template<class t1,class t2,class t3>void _p(t1 a,t2 b,t3 c){__p(a);__p(b);_pn(c);}
template<class t1,class t2,class t3,class t4>void _p(t1 a,t2 b,t3 c,t4 d){__p(a);__p(b);__p(c);_pn(d);}
tpl void op(T *a,int n){int i;n--;fo(i,n)__p(a[i]);_pn(a[n+1]);}int gi(){int x;_(x);ret x;}ll gll(){ll x;_(x);ret x;}
}
int gcd(int a,int b){ret b?gcd(b,a%b):a;}void fcl(){fclose(stdin);fclose(stdout);}
void fop(const char *s){char c[256],d[256];cl(c,0);cl(d,0);strcpy(c,s);strcpy(d,s);opi(strcat(c,".in"));opo(strcat(d,".out"));}
int eq(dou a,dou b){return a+eps>=b&&b+eps>=a;}tpl void _ma(T &a,T b){if(a<b)a=b;}tpl void _mi(T &a,T b){if(a>b)a=b;}
cint N=1234567,EE=100000000,GG=1000000000,ima=2147483647,p=998244353;
use io;
int n,m,k,T;
int ksm(int a,int b)
{
if(!b)
return 1;
int t=ksm(a,b/2);
t=(ll)t*t%p;
return b%2?(ll)t*a%p:t;
}
int main()
{
int i,j,a1,a2;
_(n,m,k);
if(n+m==2)
_pn(k);
else if(n==1||m==1)
_pn(ksm(k,n+m-1));
else
{
int an=0;
//m=max(n,m);
//n=t;
fo(i,k)
an=(an+(ll)ksm(i,n)*(ksm(k-i+1,m)-ksm(k-i,m)+p))%p;
_pn(an);
}
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
constexpr int INF = 1e9, MOD = 1e9 + 7;
constexpr int64_t LINF = 5e18, LMOD = 998244353;
// #include <atcoder/all>
// using namespace atcoder;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};
using fint64 = int_fast64_t;
template<fint64 MOD>
struct ModInt {
fint64 x;
ModInt():x(0){}
ModInt(fint64 x):
x(x>=0?x%MOD:(MOD-(-x)%MOD)%MOD)
{}
// 負号
ModInt operator -() const{
return ModInt(-x);
}
// 加算
ModInt &operator +=(const ModInt &rhs){
x+=rhs.x;
if(x>=MOD) x-=MOD;
return (*this);
}
ModInt operator +(const ModInt &rhs) const{
return ModInt(*this)+=rhs;
}
// 減算
ModInt &operator -=(const ModInt &rhs){
x+=MOD-rhs.x;
if(x>=MOD) x-=MOD;
return (*this);
}
ModInt operator -(const ModInt &rhs) const{
return ModInt(*this)-=rhs;
}
// 乗算
ModInt &operator *=(const ModInt &rhs){
x*=rhs.x;
if(x>=MOD) x%=MOD;
return (*this);
}
ModInt operator *(const ModInt &rhs) const{
return ModInt(*this)*=rhs;
}
// 除算
ModInt &operator /=(const ModInt &rhs){
(*this)*=rhs.inverse();
return (*this);
}
ModInt operator /(const ModInt &rhs) const{
return ModInt(*this)/=rhs;
}
// 等号
bool operator ==(const ModInt &rhs){
return x==rhs.x;
}
bool operator !=(const ModInt &rhs){
return x!=rhs.x;
}
// 累乗
ModInt pow(fint64 n) const{
fint64 tmp=x;
fint64 ret=1;
while(n>0){
if(n&1) ret=ret*tmp%MOD;
tmp=tmp*tmp%MOD;
n>>=1ll;
}
return ModInt(ret);
}
// 逆元
ModInt inverse() const{
fint64 a=x,b=MOD,s=1,t=0;
while(b>0){
fint64 u=a/b;
a-=u*b;
s-=u*t;
swap(a,b);
swap(s,t);
}
return ModInt(s);
}
// 入出力
friend istream &operator >>(istream &lhs,ModInt<MOD> &rhs){
fint64 x; lhs>>x;
rhs=ModInt<MOD>(x);
return lhs;
}
friend ostream &operator <<(ostream &lhs,const ModInt<MOD> &rhs){
return lhs<<rhs.x;
}
};
using mint = ModInt<LMOD>;
int main() {
int n, m, k; cin >> n >> m >> k;
if(m == 1) {
cout << ((mint)k).pow(n) << '\n';
return 0;
}
if(n == 1) {
cout << ((mint)k).pow(m) << '\n';
return 0;
}
mint ans = 0;
for(int i = 1; i <= k; ++i) {
ans += (((mint)i).pow(n) - ((mint)(i - 1)).pow(n)) * ((mint)(k - i + 1)).pow(m);
// cerr << ans << '\n';
}
cout << ans << '\n';
return 0;
}
|
#pragma GCC optimize("Ofast")
#pragma GCC target ("sse4")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
const int max_n = 1 << 10;
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];
}
void solve() {
int n, k; cin >> n >> k;
vector<int> a(n);
rep(i, n) {
cin >> a[i];
}
vector<vector<modint>> rsum(k + 1, vector<modint>(n + 1));
rep(i, n) {
modint p = 1;
rep(j, k + 1) {
rsum[j][i + 1] += rsum[j][i] + p;
p *= a[i];
}
}
rep1(x, k) {
modint ans = 0;
rep(j, x + 1) {
ans += comb(x, j) * rsum[j][n] * rsum[x - j][n];
ans -= comb(x, j) * rsum[x][n];
}
ans *= (1 + mod) / 2;
cout << ans << "\n";
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(15);
init_f();
//init();
//expr();
//int t; cin >> t; rep(i, t)
solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define il inline
#define ri register int
#define ll long long
#define ui unsigned int
il ll read(){
bool f=true;ll x=0;
register char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=false;ch=getchar();}
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
if(f) return x;
return ~(--x);
}
il int read(char *s){
int len=0;
register char ch=getchar();
while(ch==' '||ch=='\n') ch=getchar();
while(ch!=' '&&ch!='\n'&&ch!=EOF) s[++len]=ch,ch=getchar();
return len;
}
il void write(const ll &x){if(x>9) write(x/10);putchar(x%10+'0');}
il void print(const ll &x) {x<0?putchar('-'),write(~(x-1)):write(x);putchar('\n');}
il ll max(const ll &a,const ll &b){return a>b?a:b;}
il ll min(const ll &a,const ll &b){return a<b?a:b;}
const int MAXN=2e5+7;
ll a[MAXN],sum[505];
const ll mod =998244353;
il ll ksm(ll d,ll tim){
ll rest=1;
while(tim){
if(tim&1) rest=rest*d%mod;
d=d*d%mod;
tim>>=1;
}
return rest;
}
ll jc[MAXN],inv[MAXN];
void init(int n=MAXN-1){
jc[0]=1;
for(ri i=1;i<=n;++i) jc[i]=i*jc[i-1]%mod;
inv[n]=ksm(jc[n],mod-2);
for(ri i=n-1;~i;--i) inv[i]=inv[i+1]*(i+1)%mod;
}
il ll C(ll x,ll y){
if(x<y) return 0;
return jc[x]*inv[y]%mod*inv[x-y]%mod;
}
ll n,k;
int main(){
init();
n=read(),k=read();
for(ri i=1;i<=n;++i){
a[i]=read();
ll d=1;
for(ri j=0;j<=k;++j){
sum[j]=(sum[j]+d)%mod;
d=d*a[i]%mod;
}
}
for(ri i=1;i<=k;++i){
ll ans=0;
ans=(mod-ksm(2,i)*sum[i]%mod)%mod;
for(ri j=0;j<=i;++j){
ans=(ans+C(i,j)*sum[j]%mod*sum[i-j]%mod)%mod;
}
print(ans*ksm(2,mod-2)%mod);
}
return 0;
} |
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
#define br cout<<endl
#define mem(a) memset(a,0,sizeof(a))
const double pi=3.141592653589793238;
#define d(x) cout<<x<<endl;
#define de(x) cout<<#x<<" "<<x<<endl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define rev(a) reverse(a.begin(),a.end())
//const int inf = 100000000000000000;
#define maxn 1000005
int mod = (int)(1e9 + 7);
#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define all(x) x.begin(),x.end()
//#define f(i,n) for(int i=1;i<=n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i+=1)
#define fn(i,k,n) for(int i=k;i>=n;i-=1)
void pv(vector<int> v){ for (int i = 0; i < v.size(); ++i){cout<<v[i]<<" ";}cout<<endl;}
void pa(int a[],int n,int p){for (int i = p; i < n+p; ++i){cout<<a[i]<<" ";}cout<<endl;}
int modexp(int x,int y) {
int res = 1;
x = x % mod;
while (y > 0){
if (y & 1LL)
res = (res*x) % mod;
y = y>>1;
x = (x*x) % mod;
}
return res;
}
int invmod(int a) {
return modexp(a,mod-2);
}
// vector<int> edge[maxn];
// void ipgraph(int n,int m) {
// fp(i,1,n) edge[i].clear();
// if(m==0) return;
// fp(i,1,m) {
// int a,b;
// cin>>a>>b;
// edge[a].pb(b);
// edge[b].pb(a);
// }
// }
/*
void dfs(int node,int p)
{
for(int u : edge[node])
{
if(u!=p)
{
dfs(u,node);
}
}
}
*/
//////////////////////////////////////////////////////////////////////////////////
void solve()
{
int n;
cin>>n;
int a[n+1];
int xr = 0;
map<int,int> m;
fp(i,1,n)
{
cin>>a[i];
m[a[i]]++;
xr^=a[i];
}
if(n%2) d("Second")
else
{
for(auto p : m)
{
if(p.S%2)
{
d("First")
return ;
}
}
d("Second")
}
}
int32_t main()
{
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
std::cout << std::setprecision(20);
int t=1;
cin>>t;
fp(i,1,t)
{
//cout<<"Case #"<<i<<": ";
solve();
}
} | #include <iostream>
#include <vector>
#include <functional>
#include <string>
using namespace std;
string naive(int N, vector<long long> a)
{
vector<bool> U(N);
vector<long long> T;
function<int(int)> f = [&](int t) -> int
{
bool remain = false;
bool win = false;
for (int i=0; i<N; i++)
if (!U[i])
{
remain = true;
U[i] = true;
for (int j=0; j<(int)T.size(); j++)
{
T[j] += a[i];
if (!f(1-t))
win = true;
T[j] -= a[i];
}
T.push_back(a[i]);
if (!f(1-t))
win = true;
T.pop_back();
U[i] = false;
}
if (remain)
return win;
long long x = 0;
for (int t: T)
x ^= t;
return x!=0;
};
return f(0) ? "First" : "Second";
}
#include <algorithm>
string solve(int N, vector<long long> a)
{
sort(a.begin(), a.end());
if (N%2==1)
return "Second";
long long x = 0;
for (long long t: a)
x ^= t;
if (x!=0)
return "First";
bool f = false;
for (int i=0; i<N; i+=2)
if (a[i]!=a[i+1])
f = true;
if (f)
return "First";
return "Second";
}
int main()
{
/*
N = 1
-> Second
N = 2
xor!=0 -> First
xor==0 -> Second
N = 3
-> Second
N = 4
xor!=0 -> First
xor==0 -> Secnd ?
2 5 3 4 First 0
9 6 8 7 First 0
4 9 8 5 First 0
3 1 4 6 First 0
1 6 2 5 First 0
6 1 3 4 First 0
6 1 2 5 First 0
54 58 84 88 First 0
1 23 65 87 First 0
3 8 48 59 First 0
14 16 72 86 First 0
43 44 56 63 First 0
38 41 51 60 First 0
N = 5
-> Second
N = 6
xor!=0 -> First
xor==0 -> Second ?
9 3 10 2 4 6 First 0
3 3 3 7 5 1 First 0
6 6 5 1 3 7 First 0
11 23 35 38 39 62 First 0
22 22 41 63 65 87 First 0
9 9 33 36 48 53 First 0
5 11 32 45 89 90 First 0
48 58 86 91 99 100 First 0
45 46 60 63 70 70 First 0
1 13 65 71 81 91 First 0
*/
//for (int i=0; i<10000; i++)
//{
// int N = rand()%6+1;
// vector<long long> a(N);
// for (int i=0; i<N; i++)
// a[i] = rand()%10+1;
// /*
// sort(a.begin(), a.end());
// int x = 0;
// for (int t: a)
// x ^= t;
// string ans = naive(N, a);
// bool f = false;
// for (int i=0; i<N; i+=2)
// if (a[i]!=a[i+1])
// f = true;
// if (ans=="Second" && f)
// {
// for (int x: a)
// cout<<" "<<x;
// cout<<" "<<ans<<" "<<x;
// for (int i=0; i<N/2; i++)
// cout<<" "<<a[i]+a[N-i-1];
// cout<<endl;
// }
// */
// string an = naive(N, a);
// string as = solve(N, a);
// if (an!=as)
// cout<<"!"<<endl;
//}
int T;
cin>>T;
for (int i=0; i<T; i++)
{
int N;
cin>>N;
vector<long long> a(N);
for (long long &x: a)
cin>>x;
//cout<<naive(N, a)<<endl;
cout<<solve(N, a)<<endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define repp(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define vvi vector<vector<int> >
const ll mod=998244353;
int n,m=1;
bool A(int i,int j){
int x=i&j,cnt=0;
do if(x&1)cnt++;
while(x>>=1);
return (cnt%2==0);
}
int main(){
cin>>n;
rep(i,n)m*=2;
cout<<m-1<<endl;
repp(i,m-1){
rep(j,m){
if(A(i,j))cout<<'A';
else cout<<'B';
}
cout<<endl;
}
return 0;
}
| #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0'||c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c&&c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
const int MaxN = 200005;
int dp[MaxN][7]; // 1: T wins
char opt[MaxN], s[MaxN];
int dfs(int t,int x){
if(~dp[t][x]) return dp[t][x];
int &res = dp[t][x];
if(opt[t] == 'A'){
if(dfs(t+1,(10*x+s[t]-'0')%7)
*dfs(t+1,(10*x)%7) == 0)
return res = 0;
return res = 1;
}
if(opt[t] == 'T'){
if(dfs(t+1,(10*x+s[t]-'0')%7)
or dfs(t+1,(10*x)%7) == 1)
return res = 1;
return res = 0;
}
return -1; // impossible
}
int main(){
int n = readint();
scanf("%s %s",s+1,opt+1);
memset(dp,-1,sizeof(dp));
rep(i,1,6) dp[n+1][i] = 0;
dp[n+1][0] = 1;
puts(dfs(1,0) ? "Takahashi" : "Aoki");
return 0;
} |
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b)for(ll i=a;i<=b;i++)
#define loopr(i,a,b)for(ll i=b;i>=a;i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define vll vector<ll>
#define vvl vector<vll>
#define vpll vector<pll>
#define pll pair<ll,ll>
#define F first
#define S second
#define MOD 1000000007
ll max(ll a,ll b){if (a>b) return a; else return b;}
ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(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 inverse(ll a, ll p){return fexp(a, p-2);}
using namespace std;
//take care of long long
//fast I/O
auto optimizer = []() { // makes I/O fast
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 0;
}();
//seive
ll N = 1e5;
vll lpf(N+1,0);
void leastPrimeFactor()
{
lpf[1] = 1;
for (ll i = 2; i <= N; i++)
{
if(lpf[i] == 0)
{
lpf[i] = i;
for (ll j = 2*i; j <= N; j += i)
if (lpf[j] == 0)
lpf[j] = i;
}
}
}
int main()
{
ll t=1;
//cin>>t;
while(t--)
{
ll a,b,c;
cin>>a>>b>>c;
if(c%2==0)
{
a=abs(a);
b=abs(b);
if(a>b)
cout<<">"<<endl;
else if(a==b)
cout<<"="<<endl;
else
cout<<"<"<<endl;
}
else
{
if(a<0 && b<0)
{
a=abs(a);
b=abs(b);
if(a<b)
cout<<">"<<endl;
else if(a==b)
cout<<"="<<endl;
else
cout<<"<"<<endl;
}
else
{
if(a>b)
cout<<">"<<endl;
else if(a==b)
cout<<"="<<endl;
else
cout<<"<"<<endl;
}
}
}
return 0;
} | #include <iostream>
#include <cmath>
using namespace std;
int main()
{
int A, B, C;
cin >> A >> B >> C;
bool signFlag1 = (C % 2 == 1) && (A < 0);
bool signFlag2 = (C % 2 == 1) && (B < 0);
char ans;
if (signFlag1 != signFlag2) {
ans = (signFlag1 ? '<' : '>');
}
else {
if (abs(A) == abs(B)) {
ans = '=';
}
else if (abs(A) < abs(B) ^ signFlag1) {
ans = '<';
}
else {
ans = '>';
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long
typedef vector<int> VI;
typedef vector<ll> VLL ;
typedef vector<ull> VULL;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef pair<int,int> PII ;
#define PI 3.141592653589793238
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
inline void debugMode() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b)
{
return (a.second < b.second);
}
int main(){
fast
debugMode();
ll n;
cin>>n;
vector<PII> v(n);
vector<PII> vy(n);
for(ll i=0;i<n;i++){
int x,y;
cin>>x>>y;
v[i]=MP(x,y);
vy[i]=v[i];
}
sort(v.begin(),v.end());
sort(vy.begin(), vy.end(), sortbysec);
if(vy[0]==v[0] && vy[n-1]==v[n-1]){
int ans=max(abs(v[0].F-v[n-2].F),abs(v[1].F-v[n-1].F));
int ans1=max(abs(vy[0].S-vy[n-2].S),abs(vy[1].S-vy[n-1].S));
cout<<max(ans1,ans);
}
else{
int ans3=min(abs(v[0].F-v[n-1].F),abs(vy[0].S-vy[n-1].S));
int ans=max(abs(v[0].F-v[n-2].F),abs(v[1].F-v[n-1].F));
int ans1=max(abs(vy[0].S-vy[n-2].S),abs(vy[1].S-vy[n-1].S));
int ans2=max(ans1,ans);
cout<<max(ans3,ans2);
}
return 0;
}
| /**
* @author: adityasonani
* */
#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
#define int long long
#define ld long double
#define ln "\n"
#define fastio ios_base::sync_with_stdio(0); cin.tie(nullptr)
#define MOD (int) 1000000007
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(x) (x).begin(), (x).end()
#define debug(x) cerr << #x << " = " << x << endl;
#define debugP(x, y)cerr << #x << " = " << x << endl; cerr << #y << " = " << y << endl;
#define debugV(x) cerr << "["; for(auto i: x){ cerr << i << ", ";} cerr << "]" << endl;
#define precise(x) cout << fixed << setprecision(x)
#define MAX (int) 100007 // 10^5
std::vector<int> gv(int n){std::vector<int> v(n);for(auto &x: v){std::cin>>x;}return v;}
using namespace std;
/*
*/
void solve()
{
int N, K;
cin >> N >> K;
vector<pair<int, int>>vp(N);
for (auto& i : vp)cin >> i.first >> i.second;
sort(vp.begin(), vp.end());
for (auto i : vp) {
if (K >= i.first) {
K += i.second;
}
}
cout << K << endl;
}
signed main()
{
fastio;
auto start = std::chrono::high_resolution_clock::now();
int t=1;
// cin>>t;
while(t--)
solve();
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
// cerr << "\nTime taken : " << ((long double)duration.count())/((long double) 1e9) <<" s "<< endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000000;
template<const int MOD> struct modint{
long long val;
modint(): val(0) {}
modint(long long x){
if(x < 0) val = x % MOD + MOD;
else val = x % MOD;
}
modint(const modint &t){
val = t.val;
}
modint& operator =(const modint m){
val = m.val;
return *this;
}
modint operator -(){
return modint(-val);
}
modint& operator-=(const modint &m){
val -= m.val;
if(val < 0) val += MOD;
return *this;
}
modint& operator+=(const modint &m){
val += m.val;
if(val >= MOD) val -= MOD;
return *this;
}
modint& operator*=(const modint &m){
val *= m.val;
val %= MOD;
return *this;
}
modint& operator/=(modint m){
*this *= m.inv();
return *this;
}
modint& inv(){
long long x = 1, y = 0;
long long a = val, b = MOD;
while(b != 0){
long long t = a / b;
a -= t * b;
x -= t * y;
swap(a, b);
swap(x, y);
}
val = x % MOD;
if(val < 0) val += MOD;
return *this;
}
modint pow(long long k){
long long res = 1;
long long v = val;
while(k > 0){
if(k & 1) res = res * v % MOD;
v = v * v % MOD;
k >>= 1;
}
return modint(res);
}
modint operator==(const modint &m){
return val == m.val;
}
modint operator+(const modint &m){
return modint(*this) += m;
}
modint operator-(const modint &m){
return modint(*this) -= m;
}
modint operator*(const modint &m){
return modint(*this) *= m;
}
modint operator/(const modint &m){
return modint(*this) /= m;
}
};
const int MOD = 1000000007;
using mint = modint<MOD>;
int main(){
string S;
int K;
cin >> S >> K;
int N = S.size();
vector<vector<mint>> dp(17, vector<mint>(N));
int bit = 0;
for(int i = 0; i < N; i++){
if(S[i] >= 'A') bit |= (1 << (S[i] - 'A' + 10));
else bit |= (1 << (S[i] - '0'));
}
int cnt = __builtin_popcount(bit);
mint ans = 0;
if(cnt == K) ans += 1;
if(S[0] == '1') dp[0][0] = 1;
else{
dp[0][0] = 1;
if(S[0] >= 'A') dp[1][0] = S[0] - 'A' + 9;
else dp[1][0] = S[0] - '1';
}
bit = 0;
if(S[0] >= 'A') bit |= (1 << (S[0] - 'A' + 10));
else bit |= (1 << (S[0] - '0'));
for(int i = 1; i < N; i++){
int bit2 = 0;
int ma = 0;
if(S[i] >= 'A') ma = (S[i] - 'A' + 10);
else ma = (S[i] - '0');
for(int j = 0; j < ma; j++){
bit2 |= (1 << j);
}
int temp = ((bit & bit2) ^ bit2);
int cnt = __builtin_popcount(temp);
int now = __builtin_popcount(bit);
if(now == 16) dp[now][i] += ma;
else {
dp[now + 1][i] += cnt;
dp[now][i] += ma - cnt;
}
bit |= (1 << ma);
for(int j = 0; j < 17; j++){
if(j == 0){
dp[j + 1][i] += dp[j][i - 1] * 15;
dp[j][i] = 1;
}
else if(j < 16){
dp[j + 1][i] += dp[j][i - 1] * (16 - j);
dp[j][i] += dp[j][i - 1] * j;
}
else{
dp[j][i] += dp[j][i - 1] * j;
}
}
}
ans += dp[K][N - 1];
cout << ans.val << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define gcd __gcd
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)
#define pb(x) push_back(x)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sz(x) ((int)x.size())
#define fir first
#define sec second
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-10;
ll powmod(ll a,ll b) {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;}
const int maxn = 100005;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
vector<int> ans;
vector<ll> fib(100);
fib[1] = fib[2] = 1;
FOR(i, 3, 90) {
fib[i] = fib[i - 1] + fib[i - 2];
}
int k = 0;
map<int, bool> mp;
ROF(i, 90, 1) {
if(n >= fib[i]) {
n -= fib[i];
k = max(k, i);
mp[i] = true;
}
}
int cur = 3;
ROF(i, k, 1) {
cur ^= 1;
ans.pb(cur);
if(mp[i]) {
if(cur == 2) {
ans.pb(0);
}
else {
ans.pb(1);
}
}
}
cout << ans.size() << endl;
for(auto x: ans) {
if(k % 2) cout << x + 1<< endl;
else cout << (x^1) + 1 << endl;
}
return 0;
} |
/* Great things never come from comfort zones,
"whatever the mind of a man can conceive and believe,it can achieve." */
#include <bits/stdc++.h>
using namespace std;
void my_dbg() { cout << endl; }
template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B)
{ cout << ' ' << A; my_dbg(B...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)
#define ll long long
#define dd double
#define scf(n) scanf("%d",&n)
#define lscf(n) scanf("%lld",&n)
#define lpri(n) printf("%lld ",n)
#define pri(n) printf("%d ",(int)n)
#define prin(n) printf("%d\n",(int)n)
#define lprin(n) printf("%lld\n",n)
#define rep(i,ini,n) for(int i=ini;i<(int)n;i++)
#define show(a) for(auto xy: a) pri(xy); printf("\n");
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define tc int tt; scf(tt); while(tt--)
#define inf INT_MAX
#define ninf INT_MIN
#define gcd __gcd
#define bitcount(n) __builtin_popcount(n)
const int mod=1e9+7;
const int N = 1e6+7;
int pos[N];
int main()
{
int m; scf(m);
int n; scf(n);
int x=0;
while(m)
x+=m%10,m/=10;
m=0;
while(n)
m+=n%10,n/=10;
x=max(x,m);
prin(x);
//int a[m];
//rep(i,0,n)scf(a[i]);
}
| // ABC187 - A
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
int calcSum(int num)
{
int sum = 0, count = 0;;
int divide[3] = {100, 10, 1};
while(num != 0)
{
sum += num / divide[count];
num = num % divide[count];
count++;
}
return sum;
}
int main()
{
int input_1, input_2, sum_1, sum_2;
cin >> input_1 >> input_2;
sum_1 = calcSum(input_1);
sum_2 = calcSum(input_2);
if( sum_1 >= sum_2 )
{
printf("%d\n", sum_1);
}
else
{
printf("%d\n", sum_2);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll t[100][100] ; ll RAM ;
ll k ;
void printArr(ll a[], ll n)
{
vector <ll> ye ;
ye.push_back(1);
for (ll i = 0; i < n; i++)
{ye.push_back(a[i]);
// cout << a[i] << " ";
}
ye.push_back(1);
//printf("\n");
ll cost = 0 ;
ll p = ye.size() ;
ll i = 0 ;
while(i<=p-2)
{
ll x = ye[i] ;
ll y = ye[i+1] ;
cost = cost + t[x][y] ;
i++;
}
if(cost==k)
{
RAM++;
}
}
// Generating permutation using Heap Algorithm
void heapPermutation(ll a[], ll size, ll n)
{
// if size becomes 1 then prints the obtained
// permutation
if (size == 1)
{
printArr(a, n);
return;
}
for (ll i = 0; i < size; i++)
{
heapPermutation(a, size - 1, n);
// if size is odd, swap 0th i.e (first) and
// (size-1)th i.e (last) element
if (size % 2 == 1)
swap(a[0], a[size - 1]);
// If size is even, swap ith and
// (size-1)th i.e (last) element
else
swap(a[i], a[size - 1]);
}
}
int main()
{
ll n ;
cin>>n; cin>>k ;
ll a[n] ;
ll i = 0 ;
while(i<=n-1)
{
a[i]=i+2;
i++;
}
ll j ;
i=1;
while(i<=n)
{
j=1;
while(j<=n)
{
cin>>t[i][j] ;
j++;
}
i++;
}
heapPermutation(a, n-1, n-1);
cout<<RAM ;
return 0;
}
| #include<bits/stdc++.h>
using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1<<30;
constexpr int mo = 1e9+7;
constexpr ll infl = 1ll<<60;
int main(){
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int n,k;
cin>>n>>k;
vector<vector<int>> t(n,vector<int>(n));
for(int i=0;i<n;++i)for(int j=0;j<n;++j){
cin>>t[i][j];
}
vector<int> route(n-1);
for(int i=0;i<n-1;++i)route[i] = i+1;
int ret = 0;
do{
ll sum = 0;
sum += t[0][route[0]];
for(int i=0;i<n-2;++i){
sum += t[route[i]][route[i+1]];
}
sum += t[route[n-2]][0];
if(sum == k){
ret ++;
}
}while(next_permutation(route.begin(),route.end()));
cout << ret << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define lld long double
#define ul unsigned long int
#define pii pair<int,int>
#define pll pair<lli, lli>
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vll vector<lli>
#define pb push_back
#define mpr make_pair
#define ss second
#define ff first
#define INF 1000000001
#define inf1 1000000000000000001
#define pie 3.14159265358979323846264338327950L
#define all(x) x.begin(),x.end()
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define testcase() int tc;cin>>tc;while(tc--)
#define clean(x,y) memset(x,y,sizeof(x))
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.ff << ", " << p.ss << ")"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
void caseNumber(int x){printf("Case %d: ",x);}
void yes(){cout<<"YES\n";}
void no(){cout<<"NO\n";}
int gray_code(int n) {return n ^ (n >> 1);}
int turnOn(int x,int pos) {return x | (1<<pos);}
int turnOff(int x,int pos){return x & !(1<<pos);}
bool isOn(int x,int pos) {return (bool)(x & (1<<pos));}
/// RULESSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS////
/// READ THEM ALOUD......................................................................................................
/// 1. Read the problem at least twice
/// 2. Brute force if contraints r low
/// 3. Check base cases or break case if found any T-T
/// 4. Can u force it into some algorithms?
/// 5. Can u relate it to some previous stuff?
/// 6. :D :) :V :3
lli const mod= 1e9+7;
lli const inf=1e9+9;
//lli const mod=998244353;
lli const maxn=3e5+7;
int main()
{
int n;cin>>n;
cout<<n-1<<endl;
}
| #include <bits/stdc++.h>
#define int long long
#define menhera_chan_is_mine ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define pi pair<int, int>
#define BE(i) i.begin(),i.end()
#define fi first
#define se second
#define INF 2147483647
#define mkp make_pair
#define ist insert
#define mod 1000000007
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
//#pragma GCC optimize("O2")
using namespace std;
int min(int a, int b){return a < b ? a : b;}
int max(int a, int b){return a > b ? a : b;}
bool isprime(int k){bool is=1 ; for ( int i = 2 ; i*i <= k ; i++ ) if ( k % i == 0 ) is = 0 ; return k>1?is:0;}
const double PI=acos(-1);
int n;
inline void sol(){
cin >>n;
int ans=n,r=2;
for (int i=1;i<=60;i++){
if (r>n) break;
ans=min(ans,i+n/r+n%r);
r*=2;
}
cout <<ans;
}
signed main(){
menhera_chan_is_mine
int _=1;
//cin >>_;
while (_--) sol();
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 105;
const int MAX_M = 1e4 + 5;
#define u first
#define v second
pair<int, int> edges [MAX_M];
int dir [MAX_M];
vector<int> adj [MAX_N]; // idx of the edge
int arr [MAX_N];
int vis [MAX_N];
void direct (int u) {
vis[u] = 1;
for (int e : adj[u]) {
if (dir[e] == 0) {
if (u == edges[e].u) {
dir[e] = 1;
} else {
dir[e] = -1;
}
int oth = edges[e].u ^ edges[e].v ^ u;
if (!vis[oth]) {
direct(oth);
}
}
}
}
int main () {
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> edges[i].u >> edges[i].v;
adj[edges[i].u].push_back(i);
adj[edges[i].v].push_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 0; i < m; i++) {
if (arr[edges[i].u] > arr[edges[i].v]) {
dir[i] = 1;
} else if (arr[edges[i].u] < arr[edges[i].v]) {
dir[i] = -1;
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
vis[i] = 0;
}
for (int i = 1; i <= n; i++) {
if (!vis[i] && arr[i] == k) {
direct(i);
}
}
}
for (int i = 0; i < m; i++) {
if (dir[i] == 1) {
cout << "->" << '\n';
} else {
cout << "<-" << '\n';
}
}
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using ld = long double;
using vld = vector<ld>;
using vb = vector<bool>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif
template <class T> bool chmin(T& a, T b) {
if(a > b) { a = b; return true; }
else return false;
}
template <class T> bool chmax(T& a, T b) {
if(a < b) { a = b; return true; }
else return false;
}
template <class T> ostream& operator<<(ostream& s, const vector<T>& a) {
for(auto i : a) s << i << ' ';
return s;
}
constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 62;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);
void solve() {
ll n, m;
cin >> n >> m;
using P = pair<ll, char>;
vector<vector<P>> g(n);
while(m--) {
ll a, b;
char c;
cin >> a >> b >> c;
--a, --b;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
vvll dist(n, vll(n, INFL));
dist[0][n-1] = 0;
queue<pll> que;
que.emplace(0, n-1); // v0, v1
ll ans = INFL;
while(!que.empty()) {
ll v0 = que.front().first;
ll v1 = que.front().second;
que.pop();
ll d = dist[v0][v1];
for(auto [nv0, c0] : g[v0]) {
if(nv0 == v1) {
chmin(ans, 2*d + 1);
continue;
}
for(auto [nv1, c1] : g[v1]) {
if(c0 != c1) continue;
if(nv0 == nv1) {
chmin(ans, 2*(d+1));
continue;
}
if(chmin(dist[nv0][nv1], d+1)) {
que.emplace(nv0, nv1);
}
}
}
}
if(ans == INFL) cout << -1 << endl;
else cout << ans << endl;
return;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
solve();
} |
#include <iostream>
#include <vector>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(void) {
int t, n;
string s1, s2, s3;
cout << fixed;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
cin >> s1 >> s2 >> s3;
cout << 1;
for (int j = 0; j < n; j++) {
cout << 0;
}
for (int j = 0; j < n; j++) {
cout << 1;
}
cout << endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
long long n,m,mx=-1e9,a[1000010],i,j,k,l=-1,s,t,p;
int main(){
cin>>n>>m;
for(i=1;i<=n;i++){
cin>>s>>t>>p;
a[s+1]+=p;
a[t+1]=a[t+1]-p;
l=max(l,t);
}
for(i=1;i<=l;i++){
a[i]+=a[i-1];
mx=max(a[i],mx);
}
if(mx>m) cout<<"No";
else cout<<"Yes";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
typedef unsigned long long ull;
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define f(i,a,b) for(ll i=a;i<=b;i++)
#define testcases ll t;cin>>t;while(t--)
#define dec(x) greater<x>()
/*** Define fues ***/
#define mx 200005
#define mod 1000000007
#define PI acos(-1.0)
#define eps 1e-7
#define size1 100005
const char nl = '\n';
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define mem(name, fue) memset(name, fue, sizeof(name))
/*** STLs ***/
typedef vector <ll> vll;
typedef set <ll> sll;
typedef multiset <ll> msll;
typedef queue <ll> qll;
typedef map <ll, ll> mll;
typedef pair <ll, ll> pll;
typedef vector <pair <ll , ll> > vpll;
/*** Sorts ***/
#define all(v) (v).begin(), (v).end()
#define rev(v) reverse(all(v))
#define srt(v) sort(all(v))
#define srtGreat(v) sort(all(v), greater<ll>())
inline bool cmp(pll a, pll b) { if (a.ff == b.ff)return a.ss < b.ss; return a.ff > b.ff; }
#define en cout << '\n';
#define no cout << "NO" << '\n'
#define yes cout << "YES" << '\n'
#define case cout << "Case " << t++ << ": "
/*** Functions
ll BigMod(ll base, ll pow, ll modfue){ if (pow == 0) return 1; ll ans = BigMod(base, pow / 2, modfue);ll total = ((ans % modfue) * (ans % modfue)) % modfue; if(pow % 2 == 0) return total; else{ return (total * (base % modfue) ) % modfue; } }
ll InverseMod(ll base, ll pow) { if(pow == 0) return 1; ll ans = InverseMod(base, pow / 2); ans = (ans * ans) % mod; if(pow & 1){ return (ans * base) % mod; } else{ return ans; } }
bool checkprime(ll num) { if(num < 2) return false; for(ll i = 2; i * i <= num; i++){ if(num % i == 0) return false; } return true; }
ll EularPHI(ll num) { double ans = num; for(ll i = 2; i * i <= num; i++){ if(num % i == 0){ while (num % i == 0) { num /= i; } ans *= (1.0 - (1.0 / (double)i)); } } if(num > 1) ans *= (1.0 - (1.0 / (double)num)); return (ll)ans; }
ll sumofdigit(ll n){ll sum=0;while(n){sum=sum+n%10;n=n/10;}return sum;}
ll countDigit(ll n) { if (n == 0) return 0; return 1 + countDigit(n / 10); }
ll countDigit(ll n) { return floor(log10(n) + 1); } //only positive
***/
template <class T> inline T gcd(T a, T b) {if (b == 0)return a; return gcd(b, a % b);}
template <class T> inline T lcm(T a, T b) {return a * b / gcd<T>(a, b);}
template <class T> inline T power(T b, T p) {ll ans = 1; while (p--) ans *= b; return ans;}
void solve()
{
ll n;cin>>n;
vll a(n);
loop(i,0,n)cin>>a[i];
ll manhattan=0;
ll euclid=0;
loop(i,0,n)manhattan+=abs(a[i]);
cout<<manhattan<<nl;
loop(i,0,n)
{
euclid+=(abs(a[i])*abs(a[i]));
}
cout<<fixed<<setprecision(10) <<sqrt(euclid)<<nl;
ll ans = 0;
loop(i,0,n)ans = max(ans,abs(a[i]));
cout << ans << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll t=1;
// cin>>t;
// ll pppppp=1;
while(t--)
{
// cout<<"CASE #"<<pppppp;
solve();
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(9);
int n;
cin >> n;
vector<int> x(n);
for (auto &i : x) cin >> i, i = abs(i);
long long M = 0;
for (auto i : x) M += i;
double E = 0;
for (auto i : x) E += 1LL * i * i;
E = sqrt(E);
int C = *max_element(x.begin(), x.end());
cout << M << "\n" << E << "\n" << C << "\n";
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
const int inf = 1e9;
int N,M;
int e[20][20];
vector<int> dp;
vector<int> ok;
int perf(int S){
rep(i,N){
rep(j,N){
if(i==j) continue;
if((S&(1<<i))!=0 && (S&(1<<j))!=0){
if(e[i][j]==0){
return 0;
}
}
}
}
return 1;
}
int main(){
cin >> N >> M;
vector<int> A(M), B(M);
rep(i,M){
cin >> A[i] >> B[i];
A[i]--; B[i]--;
e[A[i]][B[i]] = e[B[i]][A[i]] = 1;
}
dp.assign(1<<N, inf);
rep(i,1<<N){
if(perf(i)) dp[i] = 1;
}
rep(i,1<<N){
for(int j=i;--j&=i;){
chmin(dp[i], dp[i-j]+dp[j]);
}
}
cout << dp.back() << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vi = vector<int>;
using vll = vector<long long>;
using pll = pair<long long, long long>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
constexpr long long mod = 1000000007;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()
long long _mod_pow(long long x, long long n, long long p){
long long res=1;
while(n>0){
if(n&1) res=res*x%p;
x=x*x%p;
n>>=1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, m;
cin >> n >> m;
ll t = _mod_pow(10, n, m * m);
t = (t / m) % m;
cout << t << endl;
}
|
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define ll int64_t
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define all(V) V.begin(),V.end()
#define PI 3.14159265359
#define fi fixed
#define se setprecision(10)
using namespace std;
int main() {
ll s,p;cin >> s >> p;
for(ll i=1;i<=sqrt(p);i++){
if(p%i==0){
ll a = p/i;
if(a+i==s){
cout << "Yes" << endl;
return 0;
}
}
}
cout << "No" << endl;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
long long int i,s,p,j;
cin>>s>>p;
for(i=1;;i++){
if(i*(s-i)==p){
cout<<"Yes\n";
return 0;
}
if(i*(s-i)>p){break;}
}
cout<<"No\n";
}
|
//50以内的质数有15个,所以我们穷举Y有哪些质因子,然后求出Y(注意要用long long)再一个个判断是不是互质,直接验证就行,与A题思路相似
#include <iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define maxx 100
#define ll long long
int ans[maxx];
int prime[maxx];
int a[maxx];
vector<ll > p;
int in=0;
bool cmop(ll a,ll b){
return a>b;
}
ll gcd(ll a,ll b){
return a%b==0?b:gcd(b,a%b);
}
//把50以内的素数全部打出来放在prime里面;
void Prime(){
for(int i=2;i<=50;i++){
if(!ans[i]){
prime[in++]=i;
for(int j=i+i;j<=50;j+=i){
ans[j]=1;
}
}
}
}
int main()
{
Prime();
int N;
cin>>N;
for(int i=1;i<=N;i++){
cin>>a[i];
}
for(int i=1;i<=(1<<15);i++){
ll temp=1;
for(int j=0;j<15;j++){//
if((i>>j)&1){
temp*=prime[j];
}
}
p.push_back(temp);
}
sort(p.begin(),p.end());
for(int i=0;i<=p.size();i++){
bool falg=true;
for(int j=1;j<=N;j++){
if(gcd(a[j],p[i])==1){
falg=false;
break;
}
}
if(falg){
cout<<p[i];
return 0;
}
}
return 0;
} | // Author: Vinay Khilwani
// Language: C++
// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB, CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank
// Never Stop Trying.
// Trying to be Better than Myself.
// while(true)
// {
// if(AC)
// {
// break;
// }
// else if(Contest Over)
// {
// Try.
// Check out Editorial.
// Understand.
// Find out your Mistake.
// Learn the topic (if new).
// Solve Problems on that topic (if new).
// Upsolve that problem.
// break;
// }
// else
// {
// Try.
// Use Pen-Paper.
// Find errors, edge cases, etc.
// continue;
// }
// }
// Optimizations
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")
// Libraries
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// Debugging
#define dbg(a) cerr<<a<<"\n";
#define debug_a(a) for(auto x:a) {cerr<<x<<" ";} cerr<<"\n";
#define debug_b(a) for(auto x:a) {cerr<<"["<<x.first<<", "<<x.second<<"]"<<"\n";} cerr<<"\n";
#define debug_c(a) for(auto x:a) {debug_a(x)} cerr<<"\n";
#define debug_d(a) for(auto x:a) {debug_b(x)} cerr<<"\n";
#define debug_e(a) cerr<<"["<<a.first<<", "<<a.second<<"]"<<"\n";
// Defines
#define fast ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define loop(i,a,n) for(int i=a; i<n; i++)
#define rloop(i,a,n) for(int i=a; i>=n; i--)
#define fr(i,a,n,b) for(int i=a; i<n; i+=b)
#define rfr(i,a,n,b) for(int i=a; i>=n; i-=b)
#define IN cin>>
#define OUT cout<<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(),(a).end()
#define each(a,b) for(auto &a:b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a) for(auto x:a) {OUT x<<" ";} OUT nl;
#define read(a) for(auto &x:a) {IN x;}
// #define oset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vi=vector<int>;
const ll mod=(ll)(1e9)+7LL;
const ll M=998244353LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const ld pi=acos(-1);
// General Functions
ll gcd(ll a, ll b)
{
return (b?gcd(b,a%b):a);
}
ll P(ll B, ll power, ll modulo) //Fast Power
{
ll ans=1LL;
while(power>0LL)
{
if(power%2LL==1LL)
{
ans=(ans*B)%modulo;
}
B=(B*B)%modulo;
power/=2LL;
}
return ans;
}
bool isPrime(ll n)
{
if(n<=1LL)
{
return false;
}
if(n<=3LL)
{
return true;
}
if(n%2==0LL || n%3==0LL)
{
return false;
}
for(ll i=5LL; (i*i)<=n; i+=6LL)
{
if(n%i==0LL || n%(i+2LL)==0LL)
{
return false;
}
}
return true;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r)
{
uniform_int_distribution<ll> uid(l,r);
return uid(rng);
}
void vok()
{
fast
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
}
// Global Variables
const int mxN=int(1e5)+100;
// Solver Function(s)
void solve()
{
ld sx,sy,gx,gy;
IN sx>>sy>>gx>>gy;
if(sx>gx)
{
swap(sx,gx);
swap(sy,gy);
}
//(gx-ans)/gy=(ans-sx)/sy
//sy*(gx-ans)=gy*(ans-sx);
ld ans=(gy*sx)+(sy*gx);
ans/=(sy+gy);
OUT ans<<nl;
}
// Main Function
int main()
{
vok();
OUT fixed<<setprecision(10);
int t=1;
// IN t;
while(t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef WA_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
using ll = long long;
using ull = unsigned long long;
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=int(a);i<=(int)(b);i++)
#define per(i,a,b) for(int i=int(a);i>=(int)(b);i--)
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+10;
int main() {
#ifndef WA_DEBUG
ios::sync_with_stdio(false);cin.tie(nullptr);
#endif
int n;
cin>>n;
vector<int> a(n+1);
int Min=inf;
rep(i,1,n) {
cin>>a[i];
Min=min(a[i],Min);
}
set<int> s;
rep(i,1,n) {
rep(j,1,maxn) {
if(j*j>a[i]) break;
if(a[i]%j==0) {
s.insert(j);
if(j*j!=a[i]) s.insert(a[i]/j);
}
}
}
int cnt=0;
for(int x:s) {
if(x>Min) continue;
int ans=-1;
rep(i,1,n) {
if(ans==1) break;
if(a[i]%x==0) {
if(ans==-1) ans=a[i]/x;
else ans=__gcd(ans,a[i]/x);
}
}
if(ans==1&&x<=Min) cnt++;
}
cout<<cnt<<'\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
const int N = 1500005;
ll getgcd(ll x,ll y) {return (y==0) ? x : getgcd(y,x%y);}
int a[N],n,ans;
map <int,int> mp;
void ins(int x,int t)
{
if (!mp[x]) mp[x]=t;
else mp[x]=getgcd(mp[x],t);
}
int main()
{
cin>>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)
{
for (int j=1;j*j<=a[i];++j)
if (a[i]%j==0)
{
ins(j,a[i]);
ins(a[i]/j,a[i]);
}
}
ans=0;
for (auto it:mp) ans+=(it.first==it.second && it.first<=a[1]);
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
// Z algorithm (length of longest common prefix for s[0:N] & s[i:N] for each i)
// Complexity: O(N)
// <http://snuke.hatenablog.com/entry/2014/12/03/214243>
template <typename T> std::vector<int> z_algorithm(const std::vector<T> &s) {
const int N = s.size();
if (N == 0) return {};
std::vector<int> ans(N);
ans[0] = N;
int i = 1, j = 0;
while (i < N) {
while (i + j < N and s[j] == s[i + j]) ++j;
ans[i] = j;
if (!j) {
++i;
continue;
}
int k = 1;
while (i + k < N and k + ans[k] < j) ans[i + k] = ans[k], ++k;
i += k;
j -= k;
}
return ans;
}
std::vector<int> z_algorithm(const std::string &s) {
const int N = int(s.size());
std::vector<int> v(N);
for (int i = 0; i < N; i++) v[i] = s[i];
return z_algorithm(v);
}
int N;
string T;
lint solve(lint n)
{
string S = T;
S += '_';
REP(_, n) S += "110";
vector<int> z = z_algorithm(S);
lint ret = 0;
FOR(i, T.size() + 1, S.size()) ret += (z[i] == N);
return ret;
}
int main()
{
cin >> N >> T;
lint A = 1000000;
lint s1 = solve(A), s2 = solve(A * 2);
lint ret = s1 + (10000000000LL - A) / A * (s2 - s1);
cout << ret << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
#define ANS(ans) cout << (ans) << endl;
#define UNIQUE(v) (v).erase(unique((v).begin(), (v).end()), (v).end());
typedef vector<int> VI;
typedef pair<int, int> P;
void print() { cout << endl; }
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
cout << head;
if (sizeof...(tail) != 0)
cout << " ";
print(forward<Tail>(tail)...);
}
template <class T> void print(vector<T> &vec) {
for (auto &a : vec) {
cout << a;
if (&a != &vec.back())
cout << " ";
}
cout << endl;
}
template <class T> void print(vector<vector<T>> &df) {
for (auto &vec : df) {
print(vec);
}
}
// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) for (long long i = 0; i < (n); ++i)
int main() {
ll n;
cin >> n;
VI v(n);
REP(i,n){
cin>>v[i];
}
ll ans;
ans = n*(n-1)/2;
// print(ans);
SORT(v);
ll prev_key;
ll prev_count=1;
REP(i,n){
if(i==0){
prev_key=v[i];
prev_count=1;
continue;
}
if(v[i]==prev_key){
prev_count++;
} else {
ans -= prev_count * (prev_count - 1) / 2;
// print(prev_key,prev_count);
// print(ans);
prev_key = v[i];
prev_count = 1;
}
}
ans-=prev_count*(prev_count-1)/2;
ANS(ans);
return 0;
}
|
#include<iostream>
using namespace std;
int main(){
int N,ans=0,q;
cin>>N;
for(int i=0;i<N;i++){
cin>>q;
if(q<=10)ans+=0;
else ans+= q-10;
}
cout<<ans<<endl;
} | // // Created by ...Rahul Chandra
// #include <bits/stdc++.h>
// #define db1(x) cout<<#x<<"="<<x<<'\n'
// #define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
// #define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
// #define rep(i,n) for(int i=0;i<(n);++i)
// #define repA(i,a,n) for(int i=a;i<=(n);++i)
// #define repD(i,a,n) for(int i=a;i>=(n);--i)
// using namespace std;
// using ll = long long;
// ll n;
// inline void read() {
// cin >> n;
// }
// inline void solve() {
// }
// int main()
// {
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
// long long a,b,c,d,e,f,g,rahul=0;
// int n;
// cin>>n;
// int arr[n];
// for (int i = 0; i < n; ++i)
// {
// /* code */
// cin>>arr[i];
// }
// sort(arr,arr+n);
// int max1=arr[n-1];
// for(int i=n-1;i>=0;i--){
// if(arr[i]<max1){
// max1=arr[i];
// break;
// }
// }
// if (max1!=arr[n-1])
// {
// cout<<max1<<endl; /* code */
// }
// else
// cout<<"-1"<<endl;
// return 0;
// }
// Created by ...Rahul Chandra
#include <bits/stdc++.h>
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
using namespace std;
using ll = long long;
ll n;
inline void read() {
cin >> n;
ll arr[n];
ll count=0;
for (int i = 0; i < n; ++i)
{
/* code */
cin>>arr[i];
if (arr[i]>10)
{
/* code */
count+=arr[i]-10;
}
}
cout<<count<<endl;
}
inline void solve() {
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long a,b,c,d,e,f,g,rahul=0;
read();
return 0;
} |
#include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false); cin.tie(0), cout << setprecision(15);
#define ll long long int
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, k, n) for (ll i = k; i>=n; i--)
#define repp(i, k, n) for (ll i = k; i < (n); i++)
#define pb push_back
#define all(x) begin(x), end(x)
#define vc vector<ll>
using namespace std;
using namespace std::chrono;
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds Milliseconds;
bool func(const array<ll, 2> &a, const array<ll, 2> &b) {
if (a[0] > b[0]) return true;
if (a[0] == b[0]) {
if (a[1] < b[1]) {
return true;
}
}
return false;
}
bool fun(vc &arr, ll k, ll inc) {
ll s = arr[0] + inc;
repp(i, 1, arr.size()) {
if (arr[i] * 100 > k * s) {
return false;
}
s += arr[i];
}
return true;
}
void solve() {
ll n, k;
cin >> n >> k;
vc arr(n);
rep(i, n) cin >> arr[i];
ll mid;
ll low = 0;
ll high = 2e14;
while (low < high) {
mid = (low + high) / 2;
if (fun(arr, k, mid)) {
high = mid;
} else {
low = mid + 1;
}
}
ll ans = low;
cout << ans << "\n";
}
void lc() {
ll n;
cin >> n;
vc c(n);
rep(i, n) cin >> c[i];
vector<array<ll, 2>> arr(n);
rep(i, n) {
cin >> arr[i][0];
}
rep(i, n) {
cin >> arr[i][1];
}
vector<bool> can(n, true);
repp(i, 1, n) {
ll a = arr[i - 1][0];
ll b = arr[i - 1][1];
if (max(a, b) < min(arr[i][0], arr[i][1]) || min(a, b) > max(arr[i][0], arr[i][1])) {
can[i] = true;
}
}
vc v(n, 0);
rep(i, n) {
ll a = arr[i][0];
ll b = arr[i][1];
if (i == 0) {
v[i] = max(a, b) - min(a, b);
} else {
if (can[i]) {
} else {
}
}
}
}
void at() {
ll n, s, d;
cin>>n>>s>>d;
vector<array<ll, 2>> arr(n);
bool can = false;
rep(i, n){
cin>>arr[i][0]>>arr[i][1];
if(arr[i][0]<s && arr[i][1]>d) can = true;
}
if(can) cout<<"Yes";
else cout<<"No";
}
int main() {
FAST_IO
ll test = 1;
// cin >> test;
while (test-- > 0) {
at();
}
} | #include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005
ll a[MAX_N], b[MAX_N];
const ll INF = 1001001001001001001;
vector<int> G[MAX_N];
int main() {
int n, m;
cin >> n >> m;
rep(i,n) cin >> a[i];
rep(i,m){
int x, y;
cin >> x >> y;
x--; y--;
G[x].push_back(y);
}
ll ans = -INF;
rep(i,n) b[i] = a[i];
rep(i,n){
rep(j,G[i].size()){
int y = G[i][j];
ans = max(ans, a[y] - b[i]);
b[y] = min(b[y], b[i]);
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define INF 1e9
#define INFL 1e18
//#define mod 1000000007
#define mod 998244353
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;
//typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> os_pair;
ll power(ll x,ll n){ll res =1;while(n>0){if(n%2==1){res=res*x;}x=x*x;n=n/2;}return res;}
ll powm(ll a,ll b) {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;}
//cout<< fixed << setprecision(10)
//__builtin_popcountll()
//https://qr.ae/pG7uXl
vector<pii>g[1005];
int main(){
fast;
int n,m,i,j;
cin>>n>>m;
vector<pii>v;
for(i=0;i<m;i++)
{
int x,y;
char c;
cin>>x>>y>>c;
v.pb({x,y});
g[x].pb({y,c-'a'});
g[y].pb({x,c-'a'});
}
queue<pii>q;
q.push({1,n});
int dp[n+1][n+1];
int vis[n+1][n+1];
mem(vis,0);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
dp[i][j]=INF;
}
dp[1][n]=0;
vis[1][n]=1;
int ans=INF;
while(q.size()!=0)
{
auto z=q.front();
q.pop();
for(auto x:g[z.fi])
{
for(auto y:g[z.se])
{
if(x.se==y.se)
{
if(dp[x.fi][y.fi]>dp[z.fi][z.se]+1)
{
dp[x.fi][y.fi]=dp[z.fi][z.se]+1;
q.push({x.fi,y.fi});
vis[x.fi][y.fi]=1;
}
}
}
}
}
for(i=1;i<=n;i++)
{
ans=min(ans,dp[i][i]*2);
}
for(i=0;i<m;i++)
{
ans=min(ans,dp[v[i].se][v[i].fi]*2+1);
ans=min(ans,dp[v[i].fi][v[i].se]*2+1);
}
if(ans==INF)
cout<<-1<<"\n";
else
cout<<ans;
} | #pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
//#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
#define ls(p) p<<1
#define rs(p) p<<1|1
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e6;
const int N=4*1e3+5;//?????????? 4e8
int v[N][N],d[N][N];
int n,m;
struct node
{
int next,ver;
}e[N];
int tot,head[N];
queue < pair < int , int > > q;
void add(int x,int y)
{
e[++tot].ver=y;
e[tot].next=head[x];
head[x]=tot;
}
void addedge(int x,int y)
{
add(x,y);add(y,x);
}
int bfs()
{
while(q.size())
{
int x1=q.front().first;
int x2=q.front().second;
q.pop();
for(int i1=head[x1];i1;i1=e[i1].next) for(int i2=head[x2];i2;i2=e[i2].next)
{
int y1=e[i1].ver;
int y2=e[i2].ver;
if(d[x1][y1]==d[x2][y2]&&v[y1][y2]==-1)//两边扩展
{
v[y1][y2]=v[x1][x2]+2;
q.push(make_pair(y1,y2));
}
}
}
return v[1][n];
}
void solve()
{
cin>>n>>m;
memset(v,-1,sizeof(v));
for(int i=1;i<=n;i++) q.push(make_pair(i,i)),v[i][i]=0;//偶对称,由于是求最小对称,所以要先进队
for(int i=1;i<=m;i++)
{
int x,y;
char z;
cin>>x>>y>>z;
addedge(x,y);
d[x][y]=d[y][x]=z;
if(v[x][y]==-1)
{
q.push(make_pair(x,y));q.push(make_pair(y,x));//奇对称
v[x][y]=v[y][x]=1;
}
}
cout<<bfs();
}
signed main()
{
int T=1;
// cin>>T;
for(int index=1;index<=T;index++)
{
solve();
// puts("");
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r)-1; i >= (int)(l); (i) -= 1)
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { a = (a > b ? a : b); }
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { a = (a < b ? a : b); }
template <typename T1, typename T2> ostream &operator<<(ostream &os,const pair<T1,T2> &p) { os<<p.first<<' '<<p.second; return os;}
template <typename T> ostream &operator<<(ostream &os,const vector<T> &v) { range(i,0,v.size()) {os<<(i?" ":"")<<v[i];} return os;}
using ull = unsigned long long;
using ll = long long;
using Pll = pair<ll, ll>;
using P = pair<int, int>;
constexpr ll INF64 = INT64_MAX / 2;
constexpr int INF32 = INT32_MAX / 2;
constexpr int dy[] = {0,-1,1,0,-1,1,-1,1};
constexpr int dx[] = {-1,0,0,1,-1,-1,1,1};
constexpr int mod998244353 = 998244353;
constexpr int mod1000000007 = (int)1e9 + 7;
constexpr char newl = '\n';
int main() {
int n,D,H;
cin>>n>>D>>H;
double ans=0.0;
vector<P> dh(n);
range(i,0,n){
auto &[d,h]=dh[i];
cin>>d>>h;
chmax(ans,(double)(0-d)*(H-h)/(D-d) + h);
}
cout<<fixed<<setprecision(10);
cout<<ans<<endl;
} | #include <bitset>
#include <iostream>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
// alias, using declaration
using llint = long long; using ldouble = long double;
template <class T = llint> using vector = std::vector<T>;
template <class T = llint> using deque = std::deque<T>;
template <class T = llint, class U = llint, class... V> using map = std::map<T, U, V...>;
template <class T = llint, class... U> using set = std::set<T, U...>;
template <class T = llint, class U = llint> using umap = std::unordered_map<T, U>;
template <class T = llint> using uset = std::unordered_set<T>;
template <class T = llint, class U = T> using pair = std::pair<T, U>;
template <class T = llint> using varray = std::valarray<T>;
template <class T = llint, class... U> using pqueue = std::priority_queue<T, U...>;
using std::array; using std::bitset; using std::string; using std::tuple;
// constant
constexpr llint INF = 0x3FFFFFFFFFFFFFFF;
constexpr llint INVALID = 0x8000000000000000;
namespace io {
// in
template <class... T, size_t... S> auto in(std::index_sequence<S...>) {tuple<T...> t; (..., (std::cin >> std::get<S>(t))); return t;}
template <class... T, size_t... S> auto in(std::index_sequence<S...>, size_t n) {tuple<vector<T>...> t{vector<T>(n)...}; for (size_t i = 0; i < n; i++) (..., (std::cin >> std::get<S>(t)[i])); return t;}
template <size_t N, class T = llint, class... U> auto in() {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(); else return in<T, U...>(std::index_sequence_for<T, U...>());}
template <size_t N, class T = llint, class... U> auto in(size_t n) {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(n); else return in<T, U...>(std::index_sequence_for<T, U...>(), n);}
template <size_t N, class T = llint> auto in(size_t n, size_t m) {static_assert(N == 1); vector<vector<T>> v(n, vector<T>(m)); for (auto&& vi : v) for (auto&& vij : vi) std::cin >> vij; return std::make_tuple(v);}
// out
template <class T, class... U> void out(const T& a, const U&... b) {std::cout << a << (sizeof...(b) ? " " : "\n"); if constexpr (sizeof...(b)) out(b...);}
template <class T, class U = const char*> void out(const vector<T>& a, U d = " ") {for (auto&& b : a) std::cout << b << (&b != &a.back() ? d : "\n");}
}
int main() {
// input
auto [a, b, c] = io::in<3>();
// solve
auto result = (7 - a) + (7 - b) + (7 - c);
// output
io::out(result);
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using pl = pair<ll, ll>;
const ll INF = ll(1e18);
const ll mod = ll(998244353);
const double pi = acos(-1);
#define rep0(i,n) for(ll (i) = 0; (i) < (n); ++(i))
#define rrep0(i,n) for(ll (i) = (n) - 1; (i) >= 0; --(i))
#define rep1(i,n) for(ll (i) = 1; (i) <= (n); ++(i))
#define rrep1(i,n) for(ll (i) = (n); (i) >= 1; --(i))
#define nfor(i,a,b) for(ll (i) = (a); (i) < (b); ++(i))
#define rnfor(i,a,b) for(ll (i) = (b) - 1; (i) >= (a); --(i))
#define pf(x) cout << (x) << endl
#define all(x) (x).begin(),(x).end()
#define yes pf("Yes")
#define no pf("No")
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int dx[8]={1,0,-1,0,1,1,-1,-1};
const int dy[8]={0,1,0,-1,1,-1,1,-1};
int multipf(vector<string>& s){
cout << s[0];
rep1(i, s.size() - 1)cout << " " << s[i];
cout << endl;
return 0;
}
int multipf(vector<ll>& n){
cout << n[0];
rep1(i, n.size() - 1)cout << " " << n[i];
cout << endl;
return 0;
}
ll gcd(ll a,ll b){
if(a < b)swap(a, b);
if(b == 0) return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b){
ll g = gcd(a,b);
return a / g * b;
}
ll factorial(ll n){
ll ans = 1;
rep1(i, n){
ans *= i;
ans %= mod;
}
return ans;
}
ll power(ll a, ll b){
ll ans = 1;
while(b) {
if(b & 1LL) ans = ans * a % mod;
ans %= mod;
a = a * a;
a %= mod;
b >>= 1;
}
return ans % mod;
}
struct seg {
ll n;
vector<ll> dat;
seg(ll m) : n(), dat(m * 4, 0) {
int x = 1;
while (m > x) {
x *= 2;
}
n = x;
}
void update(ll i, ll x) {
i += n - 1;
dat[i] = x;
while (i > 0) {
i = (i - 1) / 2;
dat[i] = dat[i * 2 + 1] ^ dat[i * 2 + 2];
}
}
ll query(ll a, ll b) { return query_sub(a, b, 0, 0, n); }
ll query_sub(ll a, ll b, ll k, ll l, ll r) {
if (r <= a || b <= l) {
return 0;
} else if (a <= l && r <= b) {
return dat[k];
} else {
ll vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
ll vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
return (vl ^ vr);
}
}
};
//modの値の確認をすること
int main(){
ll n,ans = 1e10;
string s;
cin >> n >> s;
if(n == 1 && s[0] == '1'){
pf(ans * 2);
return 0;
}
if(n == 2 && s[0] == '1' && s[1] == '1'){
pf(ans);
return 0;
}
ll c = INF,cnt = 0;
rep0(i, n)if(s[i] == '0'){
c = i;
++cnt;
break;
}
if(c > 2){
ans = 0;
}else{
nfor(i, c + 1, n){
if(s[i] == '0'){
if(i - c != 3){
ans = 0;
}
c = i;
++cnt;
}
}
}
if(ans == 0){
pf(0);
}else if(s[n - 1] == '1'){
pf(ans - cnt);
}else{
pf(ans - cnt + 1);
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 //10^9+7
#define INF LONG_MAX/3
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
vector<pair<ll, ll> > CON[20];
ll dp[20][272144];
int main(){
ll N, M;
cin >> N >> M;
rep(i, M) {
ll x, y, z;
cin >> x >> y >> z;
x--;y--;
CON[x].push_back(make_pair(y, z));
}
dp[0][0] = 1;
rep(i, N) {
rep(mask, (1LL<<N)) {
if(dp[i][mask] == 0) continue;
rep(next, N) {
if((mask>>next)&1) continue;
ll nmask = mask | (1LL<<next);
dp[i+1][nmask] += dp[i][mask];
}
}
rep(mask, (1LL<<N)) {
bool ng = false;
for(auto item: CON[i]) {
ll y = item.first;
ll z = item.second;
ll num = 0;
rep(bit, y+1) if((mask>>bit)&1) num++;
if(num > z) {
ng = true;
break;
}
}
if(ng) dp[i+1][mask] = 0;
}
}
ll ans = 0;
rep(mask, (1LL<<N)) {
ans += dp[N][mask];
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
int main() {
int n, w;
cin >> n >> w;
cout << n / w;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define pb push_back
#define ll long long
#define ld long double
#define sz(x) (ll)x.size()
#define vll vector<long long>
#define infi INT_MAX
#define infl LONG_LONG_MAX
#define infd LDBL_MAX
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi(i,a,b) for(ll i=(b-1);i>=a;i--)
#define F first
#define S second
#define G(a,b) get<a>(b)
#define MP make_pair
#define MT make_tuple
#define pll pair<ll,ll>
#define endl "\n"
#define ALL(v) v.begin(),v.end()
#define nl cout<<"\n";
#define pr(x) cout<<x;
#define pr1(x) cout<<x<<" ";
#define pr2(x,y) cout<<x<<" "<<y;
#define pr3(x,y,z) cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w) cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb cout<<"***************************************************************************************\n";
#define moshi cout<<"moshi moshi ! \n";
#define hi cout<<"hi ! \n";
#define bye cout<<"bye bye ! \n";
#define kawai cout<<"O kawai koto ! \n";
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits __builtin_popcountll
#define eb emplace_back
const ld PI=3.1415926535897932384626433;
auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 <<"\n";
//use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
//********************************************************** Code begins ***********************************************
// infl=2^63-1
// cout.flush()
void solve()
{
ll x,y;
cin>>x>>y;
ll ans=x/y;
cout<<ans<<"\n";
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
cout<<fixed<<setprecision(8);
ll test_cases=1;
//cin>>test_cases;
f(i,1,test_cases+1)
{
solve();
}
#ifdef rd
cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
return 0;
}
|
#include <bits/stdc++.h>
#define MP make_pair
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++ i)
#define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++ itr)
typedef long long LL;
typedef pair<int, int> pii;
int n;
int main()
{
scanf("%d", &n);
if (n == 3){
printf("6 10 15\n");
return 0;
}
for (int i = 1; i <= 10000; ++ i){
if (n == 0) break;
int cnt = 0;
if (i % 2 == 0) cnt ++;
if (i % 3 == 0) cnt ++;
if (i % 5 == 0) cnt ++;
if (cnt > 1) {
printf("%d ", i);
n --;
}
}
printf("\n");
return 0;
} | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define str to_string
#define endl "\n"
#define PI 3.141592653589
using namespace std;
using lint = long long;
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;}
//AC(*'ω'*)AC(*'ω'*)AC(*'ω'*)AC(*'ω'*)AC(*'ω'*)AC(*'ω'*)AC
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n,p=0,tmp;cin>>n;
vector<int> vec(300000);
for(int i=0;i<n;i++){
cin>>tmp;
vec[tmp]=1;
while(true){
if(vec[p])p++;
else break;
}
cout<<p<<endl;
}
}
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
long long N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A[i];
sort(A.begin(), A.end());
long long ans = 0;
long long start = 0, end = 1;
while (end < N) {
if (A[start] != A[end]) {
ans += (end-start)*(N - end);
start = end;
end = start + 1;
continue;
}
end++;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
#define pq_max priority_queue<ll>
#define pq_min priority_queue<ll,vi,greater<ll>>
#define iint int
#define f(i,a,b) for(ll i=a;i<b;i++)
#define f0(i,n) f(i,0,n)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define tc(t) int t;cin >>t;while(t--)
#define lol std::ios::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
#define ss second
#define ff first
#define ll long long
#define lld long double
#define int long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
const long long inf = 1e18;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define arr(a,n) int a[n];f0(i, n) cin >> a[i];
#define ini(a,n) memset(a,n,sizeof(a))
#define printArr(a,n) f0(i,n) cout<<a[i]<<" ";
// #define in cin>>
#define rr return 0;
#define vi vector< int >
#define vs vector<string>
#define l_b lower_bound
#define u_b upper_bound
#define mod 1000000007
#define pi 3.141592653589793238
#define fmap(m) for(auto it:m)
#define deb(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define debarr(arr,a,b) for(int i=(a);i<(b);i++) cout<<(arr[i])<<" ";cout<<endl;
#define long_max numeric_limits<ll>::max()
vs tokenizer(string str, char ch) {std::istringstream var((str)); vs v; string t; while (getline((var), t, (ch))) {v.pb(t);} return v;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
int dx[8] = { +1, +1, +1, 0, 0, -1, -1, -1};
int dy[8] = { +1, 0, -1, +1, -1, +1, 0, -1};
int dx4[4] = { +1 , -1 , +0 , +0};
int dy4[4] = { 0 , 0 , +1 , -1};
int dx2[2] = {0, 1};
int dy2[2] = {1, 0};
const int N = 1e6 + 1;
void solve()
{
int n;
cin >> n;
arr(a, n);
map<int, int>m;
f0(i, n)
m[a[i]]++;
int ans = 0 ;
for (int i = 0; i < n - 1; i++)
{
int element = n - i - m[a[i]];
// deb(element);
m[a[i]]--;
ans += max(0ll , element);
}
cout << ans << "\n";
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
lol;
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
|
//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> p;
const int INF = 1e9;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) \
{ \
cout << x << ' '; \
} \
cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
if (b < a)
{
a = b;
return 1;
}
return 0;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin>>n;
if(n%2){
cout<<"Black"<<"\n";
}
else{
cout<<"White"<<"\n";
}
}
| #include<iostream>
using namespace std;
int main()
{
int n,day;
cin>>n;
day=1;
day=day+n;
if(day%2==1)cout<<"White";
else cout<<"Black";
return 0;
} |
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
using namespace std;
const ll INF=1e19;
const ll mod = 1e9+7;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
// combination mod prime
// https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619
struct combination {
vector<mint> fact, ifact;
combination(ll n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (ll i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (ll i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(ll n, ll k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(ll n, ll k) {
return fact[n]*ifact[n-k];
}
} c(1000005);
int main(){
ll h,w;
cin >> h >> w;
vector<string> s(h);
vector<vector<ll>> a(h,vector<ll>(w)),b(h,vector<ll>(w));
vector<vector<ll>> num(h,vector<ll>(w));
rep(i,h){
cin>> s[i];
}
ll k=0;
rep(i,h){
rep(j,w){
if(s[i][j]=='.') k++;
}
}
rep(i,h){
ll cnt=0;
rep(j,w){
if(s[i][j]=='.') cnt++;
else cnt=0;
a[i][j]=cnt;
}
ll now=0;
bool none=true;
for(ll j=w-1;j>=0;j--){
if(a[i][j]!=0 && none){
now=a[i][j];
none=false;
}
if(a[i][j]==0){
none=true;
now=0;
}
a[i][j]=now;
}
}
rep(i,w){
ll cnt=0;
rep(j,h){
if(s[j][i]=='.') cnt++;
else cnt=0;
b[j][i]=cnt;
}
ll now=0;
bool none=true;
for(ll j=h-1;j>=0;j--){
if(b[j][i]!=0 && none){
now=b[j][i];
none=false;
}
if(b[j][i]==0){
none=true;
now=0;
}
b[j][i]=now;
}
}
rep(i,h){
rep(j,w){
num[i][j]=a[i][j]+b[i][j]-1;
//cout << num[i][j] << endl;
}
}
mint two=2;
mint ans=0;
rep(i,h){
rep(j,w){
if(s[i][j]=='#') continue;
mint x=num[i][j];
mint y=k;
y-=x;
mint z=two.pow(x.x);
mint u=two.pow(y.x);
z-=1;
mint tot=z*u;
ans+=tot;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*
bが入ったら、aの、それ未満を上書き。 それ以上は関係ない
aが入ったら、bの未満を上書き
BIT2つかな
*/
// BIT,index = 1 ~ (0を投げるととまる)
// https://atcoder.jp/contests/abc174/submissions/21572028
//indexにする・・・?ちょうたいへん
#define M 200010
ll dat1[M], dat2[M], dat3[M], dat4[M]; //34=累積いれとく?
void add(ll i, ll x, ll id){
if (id==1)
for(; i<M; i += i&-i) dat1[i] += x;
if (id==2)
for(; i<M; i += i&-i) dat2[i] += x;
if (id==3)
for(; i<M; i += i&-i) dat3[i] += x;
if (id==4)
for(; i<M; i += i&-i) dat4[i] += x;
}
ll sum(ll i, ll id){
ll res = 0;
if (id==1)
for(; i>0; i -= i&-i) res += dat1[i];
if (id==2)
for(; i>0; i -= i&-i) res += dat2[i];
if (id==3)
for(; i>0; i -= i&-i) res += dat3[i];
if (id==4)
for(; i>0; i -= i&-i) res += dat4[i];
return res;
}
ll rangesum(ll L, ll R, ll id){
return sum(R, id) - sum(L-1, id);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
ll n, m, q;
cin >> n >> m >> q;
ll used[3][200010]; //t, x
rep(i, 3) rep(j, 200010) used[i][j]=1;
ll ans = n*m;
add(1, n, 1); //1->0
add(1, m, 2);
add(1, n, 3); //1->0
add(1, m, 4);
//34が累積和
ll T[200010];
ll X[200010];
ll Y[200010];
map<ll, ll> mp1; // val->id
map<ll, ll> mp2; // id->val
set<ll> se;
se.insert(1);
rep(i, q){
cin >> T[i] >> X[i] >> Y[i];
Y[i]++;
se.insert(Y[i]);
}
ll cnt = 1;
for(auto v: se){
mp1[v] = cnt++;
}
cnt=1;
for(auto v: se){
mp2[cnt++]=v;
}
rep(i, q){
ll t=T[i];
ll x=X[i];
ll y=Y[i];
ll z=used[t][x]; //val
//まず除く
ll zz=mp1[z];//id
ans -= z*sum(zz, t^3); // もとあった値をなかったことに
ans += sum(zz, (t^3)+2); // なかった場合の復元
add(zz, -1, t);
add(zz, -z, t+2);//累積
used[t][x]=y;
ll yy=mp1[y]; //id
ans -= sum(yy, (t^3)+2);
ans += y*sum(yy, (t^3));
add(yy, 1, t);
add(yy, y, t+2);
cout << ans -n*m<< endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pi = pair<int, int>;
using pii = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<ll>;
using vip = vector<pair<int, int>>;
const int MAX_N = 1e5 + 1;
const ll mod = 1e9 + 7;
const ll INF = 1e9;
#define PI 3.141592653589793238
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pb push_back
#define mp make_pair
#define loop(i,n,x) for(int i=n;i<x;i++)
#define sz(a) ((int) (a).size())
#define For(n,x) for(int i=1;i<=x;i++)
#define ite iterator it
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;cin>>t; while(t--){solve();}
#define in insert
void oj()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
ll lcm(ll a, ll b)
{
return (a / __gcd(a, b)) * b;
}
ll power(ll x)
{
ll res = 1;
for (int i = 1; i <= x; i++)
{
res *= 2;
}
return res;
}
void solve()
{
int n;
cin >> n;
int ans = 0;
loop(i, 0, n)
{
int x;
cin >> x;
if (x > 10)
ans += (x - 10);
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
oj();
//test;
solve();
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
int ans=0;
cin>>n;
int A[n];
for(int i=0;i<n;i++)
{
cin>>A[i];
if(A[i]>10)
{
ans+=(A[i]-10);
}
}
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
using Graph = vector<vector<int>>;
using vst= vector<string>;
using vin= vector<int>;
using ll=long long;
using ull=unsigned long long;
using vll= vector<ll>;
using P=pair<int ,int>;
using pqi=priority_queue<int, vector<int>, greater<int>>;
using pqp=priority_queue<P, vector<P>, greater<P>>;
//const long double pi=acos(-1);
#define ft first
#define sd second
#define fn front
#define pb push_back
#define eb emplace_back
#define it insert
#define vvi vector<vector<int>>
#define si(v) int((v).size())
#define pq priority_queue
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rell(i,n) for (ll i=0; i< (ll)(n); i++)
#define sot(x) sort(x.begin(), x.end())
#define rese(x) reverse(x.begin(), x.end())
#define gsot(x) sort(x.begin(), x.end(), greater<ll>());
#define vnn(x,y,s,name) vector<vector<int>> name(x, vector<int>(y,s))
#define mse(x) memset(x, 0, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define mii(x,y,z) min(x,min(y,z))
#define maa(x,y,z) max(x,max(y,z))
#define cps CLOCKS_PER_SEC
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define cset(x) cout<<fixed<<setprecision(x)
//const int INF=1001001001;
//const ll INF=1e18;
//998244353 1000000007
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int a,b,c;
cin>>a>>b>>c;
if(a*a+b*b<c*c) yes;
else no;
}
| #include<bits/stdc++.h>
using namespace std;
int main() {
int A, B, C;
cin >> A >> B >> C;
if (A*A + B*B < C*C) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
} |
#include <iostream>
using namespace std;
int main() {
int x, y;
cin >> x >> y;
cout << ((y % x == 0)?"Yes":"No");
return 0;
} | #include<bits/stdc++.h>
#include<cstdio>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> pair;
#define MOD 1000000007
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
int main(void){
int m,h;
cin >> m >> h;
if(h%m==0) puts("Yes");
else puts("No");
} |
#pragma GCC optimize("Ofast")
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
using namespace std;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007LL
typedef long long ll;
typedef pair<ll,ll> P;
struct UF {
vector<int> par; // 親のインデックスを記憶する配列
vector<int> sz; // サイズを記憶する。
vector<map<int, int> > value;
// 初期化
UF(int n):par(n),sz(n),value(n){
for(int i = 0; i < n; i++){
par[i] = i;sz[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(sz[x] > sz[y])swap(x, y);
par[x] = y;
sz[y] += sz[x];
// マージデクはここでやる(x -> y)
for(pair<int, int> const &ele: value[x]) {
value[y][ele.FI] += ele.SE;
}
}
// xとyが同じ集合ならtrue
bool same(int x, int y) { return find(x) == find(y); }
// 素の集合のサイズを求める
int size(int n){return sz[find(n)];}
};
int main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
int N, Q;cin >> N >> Q;
UF uf(N);
REP(i, N) {
int c;cin >> c;c--;
uf.value[i][c]++;
}
REP(i, Q) {
int type, a, b;cin >> type >> a >> b;
a--;b--;
if(type == 1) {
a = uf.find(a);
b = uf.find(b);
if(uf.same(a, b))continue;
uf.unite(a, b);
}
else {
a = uf.find(a);
cout << uf.value[a][b] << endl;
}
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for(ll i = (ll)(m); i < (ll)(n); i++)
#define rep_inv(i, n, m) for(ll i = (ll)(n); i > (ll)(m); i--)
bool compare2nd(vl a, vl b){
if(a[1] <= b[1])
return true;
else
return false;
}
bool pair_same(vl a, vl b){
if(((a[1] == b[1]) && (a[2] == b[2])) || ((a[1] == b[2]) && (a[2] == b[1])))
return true;
else
return false;
}
int main(){
ll N;
cin >> N;
vvl xy(N, vl(3));
rep(i, N){
cin >> xy[i][0] >> xy[i][1];
xy[i][2] = i;
}
vvl xy1(N, vl(3));
vvl xy2(N, vl(3));
stable_sort(xy.begin(), xy.end());
copy(xy.begin(), xy.end(), xy1.begin());
stable_sort(xy.begin(), xy.end(), compare2nd);
copy(xy.begin(), xy.end(), xy2.begin());
/*
cout << "x : \n";
rep(i, N){
cout << xy1[i][0] << ' ' << xy1[i][1] << ' ' << xy1[i][2] << "\n";
}
cout << "y : \n";
rep(i, N){
cout << xy2[i][0] << ' ' << xy2[i][1] << ' ' << xy2[i][2] << "\n";
}
*/
vvl cands;
// x の max - min, max2nd - min, max - min2nd をインデックスと一緒に格納
cands.push_back({xy1[N - 1][0] - xy1[0][0], xy1[N - 1][2], xy1[0][2]});
cands.push_back({xy1[N - 1][0] - xy1[1][0], xy1[N - 1][2], xy1[1][2]});
cands.push_back({xy1[N - 2][0] - xy1[0][0], xy1[0][2], xy1[N - 2][2]});
// y の max - min, max2nd - min, max - min2nd をインデックスと一緒に格納
cands.push_back({xy2[N - 1][1] - xy2[0][1], xy2[N - 1][2], xy2[0][2]});
cands.push_back({xy2[N - 1][1] - xy2[1][1], xy2[N - 1][2], xy2[1][2]});
cands.push_back({xy2[N - 2][1] - xy2[0][1], xy2[0][2], xy2[N - 2][2]});
stable_sort(cands.begin(), cands.end(), greater<vl>());
/*
cout << "\n";
cout << "cands\n";
rep(i, cands.size()){
cout << cands[i][0] << ' ' << cands[i][1] << ' ' << cands[i][2] << "\n";
}
*/
if(!pair_same(cands[0], cands[1]))
cout << cands[1][0] << "\n";
else
cout << cands[2][0] << "\n";
return 0;
}
|
// 4 minuts
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define REP(NAME, NUM) for (int NAME = 0; NAME < int(NUM); ++NAME)
#define BREP(NAME, NUM) for (int NAME = (NUM)-1; NAME >= 0; --NAME)
#define ALL(NAME) (NAME).begin(), (NAME).end()
#define cMOD 1000000007ULL
#define cINF ((1ll<<62)-1)
#define cINFINT ((1<<30)-1)
template <typename T>
void chmin(T& a, T b){ if(a > b) a = b; }
template <typename T>
void chmax(T& a, T b){ if(a < b) a = b; }
int main()
{
cin.tie( 0 ); ios::sync_with_stdio( false );
// ----------------------------------------------------------------
ll n;
cin>>n;
priority_queue<pair<ll, string>> q;
REP(i,n)
{
string s;
ll t;
cin>>s>>t;
q.push({t,s});
}
q.pop();
cout << q.top().second << endl;
// ----------------------------------------------------------------
return 0;
} | #include <iostream>
using namespace std;
int main()
{
int n,k;
long long res=0;
cin>>n>>k;
for (int i=1;i<=n;i++)
for (int j=1;j<=k;j++)
{
res+=100*i+j;
}
cout<<res;
return 0;
}
|
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
int N;
long K;
cin >> N;
cin >> K;
vector<vector<long> > t(N,vector<long>(N,0));
for(int i = 0;i < N; ++i) {
for (int j = 0;j < N; ++j) {
cin >> t[i][j];
}
}
vector<int> city(N-1,0);
for (int i=0; i < N-1;++i) city[i] = i+1;
long long ans = 0;
do {
vector<int> cityTmp(N+1,0);
copy(city.begin(), city.end(), cityTmp.begin()+1);
cityTmp[0] = 0;
cityTmp[N] = 0;
long long tmp = 0;
for (int i = 0;i < N; ++i) {
tmp += t[cityTmp[i]][cityTmp[i+1]];
}
if (tmp == K) ans++;
} while(next_permutation(city.begin(), city.end()));
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int n,x; cin>>n>>x;
int al=0;
for(int i=1;i<n+1;++i){
int v,p; cin>>v>>p;
al+=v*p;
if(al>100*x){
cout<<i<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
} |
#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define int ll
#define each(i, a) for (auto &&i : (a))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using vp = vector<P>;
using int128 = __int128_t;//cin coutはできない
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a / gcd(a, b) * b;};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
template <class T>
void CVEC(const T &v) {
int c = v.size() - 1;
for (int i = 0; i < c; i++) cout << v[i] << ' ';
if (c > -1) cout << v[c];
cout << '\n';
}
#pragma endregion header
signed main(){
vector<int> a(3);
rep(i,3)cin >> a[i];
sort(ALL(a));
if((a[2] - a[1]) == (a[1] - a[0])) cout << "Yes" << endl;
else cout << "No" << endl;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c; cin >> a >> b >> c;
if((a+b==2*c)||(a+c == 2*b)||(b+c==2*a)){
cout << "Yes" << endl;
}
else cout << "No"<< endl;
}
|
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define PB push_back
#define fi first
#define se second
#define MP make_pair
const int P = 998244353;
const int V = 5100;
const int N = 5000;
const int M = 15;
int C[V][V];
int dp[M + 1][V];
int pt[30], n, m;
int main() {
pt[0] = 1;
for (int i = 1; i < 20; ++i) pt[i] = pt[i - 1] * 2;
for (int i = 0; i <= N; ++i) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j) {
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P;
}
}
while (~scanf("%d%d", &n, &m)) {
memset(dp, 0, sizeof(dp));
dp[M][m] = 1;
for (int i = M; i > 0; --i) {
for (int j = 0; j <= m; ++j) {
if (dp[i][j] == 0) continue;
for (int k = 0; k * pt[i - 1] <= j && k <= n; k += 2) {
dp[i - 1][j - k * pt[i - 1]] += (LL) dp[i][j] * C[n][k] % P;
dp[i - 1][j - k * pt[i - 1]] %= P;
}
}
}
printf("%d\n", dp[0][0]);
}
return 0;
}
/*
5 20
10 5
3141 2718
*/
| #include<iostream>
using namespace std;
typedef long long ll;
const int MOD = 998244353;
ll dp[5010];
ll f[5010];
int n , m;
ll Q_power(ll a,ll b){
ll res = 1;
while(b){
if(b & 1) res = res * a % MOD;
b >>= 1;
a = a * a % MOD;
}
return res;
}
ll X_1(ll a){ return Q_power(a , MOD - 2);}
void init(){
f[0] = 1;
for(int i = 1 ; i <= 5000 ; i ++){
f[i] = f[i - 1] * (n - i + 1) % MOD * X_1(i) % MOD;
}
}
int main(){
cin >> n >> m;
if(m & 1){
puts("0");
return 0;
}
init();
dp[0] = 1;
for(int i = 2 ; i <= m ; i += 2){
for(int j = 0 ; 2 * j <= i ; j ++){
dp[i] = (dp[i] + f[2 * j] * dp[(i - 2 * j) / 2] % MOD) % MOD;
}
}
cout << dp[m] << endl;
return 0;
}
|
#include<bits/stdc++.h>
#include<tuple>
typedef long long ll;
#define INF 99999999999999
#define mod 1000000007
#define eps 1e-9
using namespace std;
typedef pair<ll,ll>du;
ll n,x,c,y,l=0,r=0;
long double ans;
int main(){
ios::sync_with_stdio(0);
cin>>n;
for(ll i=1;i<n;i++)
ans+=(long double)n/(n-i);
cout<<setprecision(9)<<fixed<<ans<<endl;
} | #include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;
int main()
{
LL N;
in >> N;
LL ans = N;
std::map<LL, LL>flag;
for (LL a = 2; a * a <= N; ++a)
{
if (flag[a] == 1) continue;
LL m = a * a;
do
{
--ans;
flag[m] = 1;
m *= a;
} while (m <= N);
}
out << ans << std::endl;
}
|
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll p=998244353;
ll n,k,b[200010][301],c[302][302],a[200010],sum[302];
ll ksm(ll x,ll y){
ll sej=1;
while(y){
if(y&1)sej=sej*x%p;
x=x*x%p;
y/=2;
}
return sej;
}
int main(){
ll i,j,x,y,xlh;
scanf("%lld%lld",&n,&k);
for(i=0;i<=k;i++)c[i][0]=1;
for(i=1;i<=k;i++)
for(j=1;j<=i;j++)c[i][j]=(c[i-1][j]+c[i-1][j-1])%p;
for(i=1;i<=n;i++)scanf("%lld",&a[i]);
for(i=1;i<=n;i++)b[i][0]=1;
sum[0]=n;
for(x=1;x<=k;x++){
//printf("%lld\n",x);
for(i=1;i<=n;i++){
b[i][x]=b[i][x-1]*a[i]%p;sum[x]=(sum[x]+b[i][x])%p;
}
//printf("%lld %lld\n",x,sum[x]);
xlh=0;
for(i=0;i<=x;i++){
y=sum[i]*sum[x-i]%p*c[x][i]%p;
xlh=(xlh+y)%p;
y=c[x][i]*sum[x]%p;
//xlh=(xlh+y+p)%p;
}
//for(i=0;i<=x;i++)printf("%lld ",c[x][i]);puts("");
xlh=(xlh-sum[x]*ksm(2,x)%p+p)%p;
xlh=xlh*ksm(2,p-2)%p;
printf("%lld\n",xlh);
}
} | #pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define FORD(i, a, b) for (int i=(a); i>(b); i--)
#define PPC(x) __builtin_popcount(x)
#define MSB(x) (63 - __builtin_clzll(x))
#define SZ(x) ((int)(x).size())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;
const int maxN = 1 << 18, mod = 998244353, maxK = 302;
template <typename T1, typename T2> inline void addMod(T1& a, T2 b) { a = (a + b) % mod; }
template <typename T1, typename T2> inline void multMod(T1& a, T2 b) { a = a * b % mod; }
template <typename T1, typename T2> inline void subMod(T1& a, T2 b) { a = (a + mod - b%mod) % mod; }
long long qpow(long long a, long long b)
{
long long ret = 1ll;
for (; b != 0; b /= 2)
{
if (b & 1) multMod(ret, a);
multMod(a, a);
}
return ret;
}
long long S[maxK], cf[maxK][maxK];
int T[maxN];
void solve()
{
int n, k;
scanf ("%d%d", &n, &k);
cf[0][0] = 1ll;
FOR(i, 1, k+1)
{
cf[i][0] = 1ll;
FOR(j, 1, i+1)
cf[i][j] = (cf[i-1][j-1] + cf[i-1][j]) % mod;
}
FOR(i, 1, n+1)
{
scanf ("%d", T+i);
long long p = 1ll;
FOR(j, 0, k+1)
{
addMod(S[j], p);
multMod(p, T[i]);
}
}
long long pow2 = 2;
FOR(x, 1, k+1)
{
long long res = 0;
FOR(i, 0, x+1)
{
long long temp = cf[x][i];
multMod(temp, S[i]);
multMod(temp, S[x-i]);
addMod(res, temp);
}
subMod(res, S[x] * pow2);
multMod(res, qpow(2, mod-2));
printf("%lld\n", res);
multMod(pow2, 2);
}
}
int main()
{
int t = 1;
//scanf ("%d", &t);
FOR(tid, 1, t+1)
{
//printf("Case #%d: ", tid);
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
#define re register
using namespace std;
inline int read(){
re int t=0;re char v=getchar();
while(v<'0')v=getchar();
while(v>='0')t=(t<<3)+(t<<1)+v-48,v=getchar();
return t;
}
const int M=998244353;
inline void add(re int &x,re int y){
(x+=y)>=M?x-=M:x;
}
int n,m,ans,a,b,c,d;
int main(){
a=read(),b=read(),c=read(),d=read();
if(a+b==c+d||a+c==b+d||a+d==b+c||a==b+c+d||b==a+c+d||c==a+b+d||d==a+b+c)puts("Yes");
else puts("No");
} | /*
* @author: codancer
* @createTime: 2021-01-30, 20:01:13
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 1e9+7;
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define fep(i,a,b) for(int i=(a);i>=(b);i--)
#define deb(x) cerr<<#x<<" = "<<(x)<<"\n"
typedef vector<int> VI;
typedef vector<ll> VII;
typedef pair<int,int> pii;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll B) {
return (ull)rng() % B;
}
int main(){
int a,b,c;
cin>>a>>b>>c;
if(c==0){
if(a<=b){
cout<<"Aoki"<<endl;
}else{
cout<<"Takahashi"<<endl;
}
}else{
if(a>=b){
cout<<"Takahashi"<<endl;
}else{
cout<<"Aoki"<<endl;
}
}
return 0;
} |
#include <iostream>
using namespace std;
void sol(int m, int h){
if(h%m==0)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
int main() {
int m,h;
cin>>m>>h;
sol(m, h);
return 0;
} | #include<bits/stdc++.h>
//#include <atcoder/all>
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define fi first
#define se second
#define sz(x) ((int)x.size())
using namespace std;
//using namespace atcoder;
using ld = long double;
using ll = long long;
using P = pair<ll, ll>;
template<typename T> bool chmin(T& a, const T& b) { if(a > b){ a = b; return 1;} return 0; }
template<typename T> bool chmax(T& a, const T& b) { if(a < b){ a = b; return 1;} return 0; }
const ll ZER = 0;
const ll MOD = 998244353;
const ll INF = 1e18;
int main(){
int m, h;
cin >> m >> h;
cout << (h % m ? "No" : "Yes") << endl;
} |
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
typedef long long ll;
#define F0R(i, x, y) for (int i = x; i < y; i++)
#define FOR(i, n) F0R(i, 0, n)
#define F0RD(i, x, y) for (int i = y - 1; i >= x; i--)
#define FORD(i, n) F0RD(i, 0, n)
#define F first
#define S second
int mp(int x, int c) {
return 1000 * c + x;
}
vector<int> adj[2005];
bool vis[2005];
void dfs(int v) {
vis[v] = true;
for (int to : adj[v]) {
if (!vis[to]) dfs(to);
}
}
int main() {
#ifdef mikey
freopen("a.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
auto ad = [&] (int a, int b, int c, int d) {
adj[mp(a, b)].push_back(mp(c, d));
adj[mp(c, d)].push_back(mp(a, b));
};
auto add = [&] (int a, int b, int c, int d) {
adj[mp(a, b)].push_back(mp(c, d));
};
FOR(i, m) add(i, 1, 0, 0);
FOR(i, m) add(i, 1, n - 1, 0);
FOR(i, n) add(i, 0, 0, 1);
FOR(i, n) add(i, 0, m - 1, 1);
FOR(i, n) {
string s; cin >> s;
FOR(j, m) {
if (s[j] == '#') {
ad(i, 0, j , 1);
}
}
}
int rcc = 0;
FOR(i, n) {
if (!vis[mp(i, 0)]) {
dfs(mp(i, 0));
++rcc;
}
}
memset(vis, false, sizeof(vis));
int ccc = 0;
FOR(i, m) {
if (!vis[mp(i, 1)]) {
dfs(mp(i, 1));
++ccc;
}
}
cout << min(rcc - 1, ccc - 1) << '\n';
}
| #include<bits/stdc++.h>
using namespace std;
int n,m,fa[2005],t1[2005],t2[2005],s1,s2;
char a[1005][1005];
int find(int x){
if (x==fa[x]) return x;
else return fa[x]=find(fa[x]);
}
void Union(int x,int y){
fa[find(x)]=find(y);
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n+m;i++) fa[i]=i;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
a[i][j]=getchar();
while (a[i][j]!='#'&&a[i][j]!='.') a[i][j]=getchar();
if (a[i][j]=='#') Union(i,j+n);
}
}
Union(1,n+1);
Union(1,n+m);
Union(n,n+1);
Union(n,n+m);
for (int i=1;i<=n;i++) t1[find(i)]=1;
for (int i=n+1;i<=n+m;i++) t2[find(i)]=1;
for (int i=1;i<=n+m;i++) s1+=t1[i];
for (int i=1;i<=n+m;i++) s2+=t2[i];
cout<<min(s1,s2)-1;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string solve(ll n, ll w, vector<ll> s, vector<ll> t, vector<ll> p) {
vector<ll> a(2 * pow(10, 5) + 1, 0);
for(ll i = 0; i < n; i++) {
a[s[i]] += p[i];
a[t[i]] -= p[i];
}
if(a[0] > w) {
return "No";
}
for(ll i = 1; i < (ll)a.size(); i++) {
a[i] += a[i - 1];
if(a[i] > w) {
return "No";
}
}
return "Yes";
}
int main() {
ll n, w;
cin >> n >> w;
vector<ll> s(n), t(n), p(n);
for(ll i = 0; i < n; i++) {
cin >> s[i] >> t[i] >> p[i];
}
cout << solve(n, w, s, t, p);
} | #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
inline bool valid(int x, int n) {
return 0 <= x && x < n;
}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
template <typename T>
inline T pop(queue<T>& q) {
T front = q.front();
q.pop();
return front;
}
template <typename T>
inline T gcd(T a, T b) {
for (; b; a %= b, swap(a, b));
return a;
}
template <typename T>
tuple<T, T, T> xgcd(T a, T b) {
if (b == 0) return {1, 0, a};
T x, y, g;
tie(x, y, g) = xgcd(b, a%b);
return {y, x-(a/b)*y, g};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll N, W; cin >> N >> W;
vvi query(N*2);
for (int i = 0; i < N; i++) {
int S, T, P; cin >> S >> T >> P;
query[i*2] = vi{S, P};
query[i*2+1] = vi{T, -P};
}
sort(query.begin(), query.end());
ll sub = 0;
for (int i = 0; i < N*2; i++) {
sub += query[i][1];
if (sub > W) {
cout << "No";
return 0;
}
}
cout << "Yes";
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s,t;
cin>>s>>t;
if(s == 'Y')
{
if(t == 'a'|| t == 'A')
cout<<'A';
else if(t == 'b'|| t == 'B')
cout<<'B';
else if(t == 'c'|| t == 'C')
cout<<'C';
}
else if(s == 'N')
{
cout<<t;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O0")
typedef long long int ll;
typedef long double ld;
const ll mod = 1e9+7;
const ll INF = 1e18;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
#define Rep(i,a,n) for (ll i = (a); i < (n); ++i)
#define All(a) (a).begin(),(a).end()
#define Pi acos(-1)
using Graph = vector<vector<ll>>;
using V = vector<ll>;
using P = pair<ll,ll>;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << setprecision(15) << fixed;
chrono::system_clock::time_point start,end;
start = chrono::system_clock::now();
char s,t;
cin >> s >> t;
if (s == 'Y') cout << char(toupper(t)) << '\n';
else cout << t << '\n';
end = chrono::system_clock::now();
auto elapsed = chrono::duration_cast< chrono::milliseconds >(end - start).count();
cerr << elapsed << "ms" << '\n';
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i:x){cout << i << " ";} cout << endl;}
#define isin(x,l,r) ((l)<=(x) && (x)<(r))
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<typename T>bool chmin(T&x,const T&y) {if(x>y){x=y;return true;} else return false;}
template<typename T>bool chmax(T&x,const T&y) {if(x<y){x=y;return true;} else return false;}
int main() {
int N;
cin >> N;
vector<pii> A;
int ans = 0;
rep(i, N) {
int a;
cin >> a;
int left = i;
while (sz(A) && A.back().first >= a) {
chmax(ans, (i - 1 - A.back().second + 1) * A.back().first);
chmax(ans, (i - A.back().second + 1) * a);
chmin(left, A.back().second);
A.pop_back();
}
A.emplace_back(a, left);
}
for (auto [a, i] : A) {
chmax(ans, a * (N-i));
}
cout << ans << '\n';
return 0;
} | #include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll,ll>
#define mod 1000000007 //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;
}
void solve()
{
string s;
cin>>s;
ll ans=0;
ll n=s.size();
ll a[n+1];
for(ll i=0;i<n;i++){
a[i+1]=(ll)(s[i]-'a');
}
ll cnt[26]={0};
for(ll i=n;i>0;i--){
if(i>n-2){
cnt[a[i]]++;
continue;
}
if(a[i]==a[i+1]){
cnt[a[i+1]]--;
ans+=n-i;
ans--;
ans-=cnt[a[i]];
for(ll j=0;j<26;j++)
cnt[j]=0;
cnt[a[i]]=n-i+1;
}
else
{
cnt[a[i]]++;
}
}
cout<<ans<<endl;
}
int main()
{ ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//Start from Here.
ll t;
t=1;
// cin>>t;
while(t--)
solve();
//Good Bye!
return 0;
} |
/*
* author : rits1272
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define print(x) cerr << "[" << #x << ": " << x << "]";
#define println(x) cerr << "[" << #x << ": " << x << "]\n";
#define show(v) for(int i = 0; i < (int)v.size(); i++) cout << v[i] << (i == (int)v.size()-1 ? "\n" : " ");
#define deb(v) for(int i = 0; i < (int)v.size(); i++) {print(i) println(v[i])}
#define pb push_back
#define INF 1e18 + 5
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
vector<vector<int>> adj[N];
vector<bool> vis(N);
int n, m, x, y;
int wait(int t, int k){
if(t == 0 || t%k == 0) return 0;
return k - t%k;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> m >> x >> y;
--x, --y;
vis.resize(n);
for(int i = 0; i < m; i++){
int a, b, t, k; cin >> a >> b >> t >> k;
--a, --b;
adj[a].pb({b, t, k}); // city, time, departure
adj[b].pb({a, t, k});
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0ll, x}); // time city
vector<int> mn(n, INF);
mn[x] = 0;
while(pq.size()){
int cur_city = pq.top().second, t = pq.top().first;
//print(cur_city) println(t)
pq.pop();
for(auto x: adj[cur_city]){
int nxt_city = x[0], T = x[1], k = x[2];
if(t + T + wait(t, k) < mn[nxt_city]){
mn[nxt_city] = t + T + wait(t, k);
pq.push({mn[nxt_city], nxt_city});
}
}
}
cout << (mn[y] == INF ? -1 : mn[y]) << "\n";
return 0;
}
/*
* Only write code when you are sure of the approach.
* Do optimized thinking (Think which technique/observation can be used).
* Do post analysis after the contest.
* Upsolve the problems.
* Learn a technique and solve some problems if its new to you.
* DON'T GET STUCK ON ONE APPROACH
*/
| #include <bits/stdc++.h>
template <class T>
T read() {
T num = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
num = num * 10 + ch - '0';
ch = getchar();
}
return num * f;
}
constexpr int N = 1e5 + 7;
std::vector<std::tuple<int, int, int>> edge[N];
int n, m;
using ll = long long;
ll dist[N];
bool done[N];
void dijkstra(int s, int t) {
memset(dist, 0x3f, sizeof(dist));
std::priority_queue<std::pair<ll, int>, std::vector<std::pair<ll, int>>,
std::greater<std::pair<ll, int>>>
que;
que.push(std::make_pair(dist[s] = 0, s));
while (!que.empty()) {
auto p = que.top();
que.pop();
int u = p.second;
if (done[u]) continue;
done[u] = 1;
for (auto [v, cost, k] : edge[u]) {
ll new_dist = (dist[u] + k - 1) / k * k + cost;
if (new_dist < dist[v]) {
que.push(std::make_pair(dist[v] = new_dist, v));
}
}
}
printf("%lld\n", dist[t] == 0x3f3f3f3f3f3f3f3f ? -1 : dist[t]);
}
int main() {
n = read<int>(), m = read<int>();
int x = read<int>(), y = read<int>();
for (int i = 0; i < m; i++) {
int A = read<int>(), B = read<int>(), T = read<int>(), K = read<int>();
edge[A].emplace_back(std::make_tuple(B, T, K));
edge[B].emplace_back(std::make_tuple(A, T, K));
}
dijkstra(x, y);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int T,ans,m;
string S;
string S0;
cin >> T ;
S0="atcoder";
rep(k,T) {
cin >> S;
ans=-1;
rep(i,S.size()){
if ('a'!=S.at(i)){
ans=i;
break;
}
}
if (ans<=0){
cout << ans << endl;
continue;
}
if (ans>1){
if ('t'<S.at(ans))ans--;
cout << ans << endl;
continue;
}
if ('t'<S.at(1)){
cout << 0 << endl;
continue;
}
if ('t'>S.at(1)){
cout << 1 << endl;
continue;
}
ans=10;
m=min(S.size(),S0.size());
rep(i,m){
if (S.at(i)!=S0.at(i)){
if(S.at(i)>S0.at(i)) ans=0;
else ans=1;
}
}
if (ans<2){
cout << ans << endl;
continue;
}
if (S.size()>S0.size()) ans=0;
else ans=1;
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define T \
int t; \
cin >> t; \
while (t--)
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define PI 3.141592653589793238462643383
#define pb push_back
#define all(s) s.begin(), s.end()
bool check(string &t) {
int n = t.length();
return n >= 3 and (t[n - 1] == 'x' and t[n - 2] == 'o' and t[n - 3] == 'f');
}
int main() {
fast;
ll n;
string s, t = "";
cin >> n >> s;
ll cnt = 0;
for (int i = 0; i < n; i++) {
t += s[i];
if (check(t)) {
t.pop_back();
t.pop_back();
t.pop_back();
cnt++;
}
}
cout << n - (3 * cnt) << endl;
} |
using namespace std;
#include <bits/stdc++.h>
#define N 105
#define INF 1000000000
int n;
char str[N];
int a[N],b[N],c[N];
int main(){
scanf("%d%s",&n,str+1);
for (int i=0;i<=n;++i)
scanf("%d",&a[i]);
int k=INF;
for (int i=1;i<=n;++i)
if (str[i]=='<')
k=min(k,a[i]-a[i-1]);
else
k=min(k,a[i-1]-a[i]);
// for (int i=0;i<=n;++i){
// b[i]=a[i]/k;
// c[i]=a[i]-b[i]*(k-1);
// }
printf("%d\n",k);
for (int i=1;i<=k;++i,printf("\n"))
for (int j=0;j<=n;++j)
printf("%d ",a[j]/k+(i<=a[j]%k?1:0));
// for (int i=1;i<=k-1;++i,printf("\n"))
// for (int j=0;j<=n;++j)
// printf("%d ",b[j]);
// for (int j=0;j<=n;++j)
// printf("%d ",c[j]);
return 0;
}
| //IQ134高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007
#define MODB 998244353
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
for (T& x: vec) { is >> x; }
return is;
}
int main() {
ll ans = 0;
ll tmp = 0;
int n; cin >> n;
string s; cin >> s;
vector<int> a(n+1); cin >> a;
int mi[n+1] = {};
REP(i, n) if(s[i] == '<') mi[i+1] = max(mi[i+1], mi[i]+1);
for(int i = n; i >= 0; i--) if(s[i] == '>') mi[i] = max(mi[i], mi[i+1]+1);
ans = INF;
//REP(i, n+1) if(mi[i] > 0) ans = min(ans, (ll)(a[i]/mi[i]));
REP(i, n) ans = min(ans, (ll)abs(a[i] - a[i+1]));
cout << ans << endl;
REP(i, ans){ REP(j, n+1){cout << (a[j] + i)/ ans << " ";} cout << endl;}
//REP(i, ans - 1){ REP(j, n+1){cout << mi[j] << " ";} cout << endl;}
//REP(i, n+1){cout << a[i] - mi[i] *(ans - 1) << " ";} cout << endl;
} |
//
// Created by bytedance on 2020/4/10.
//
#include <bits/stdc++.h>
//#include <fstream>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn], b[maxn];
vector<int> even, odd;
int main() {
int n;
scanf("%d", &n);
long long sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
sum += a[i];
}
for (int i = 0; i < n; i++) {
scanf("%d", b + i);
}
even.clear();
odd.clear();
for (int i = 0; i < n; i++) {
if (i & 1) {
odd.push_back(b[i] - a[i]);
} else {
even.push_back(b[i] - a[i]);
}
}
sort(odd.begin(), odd.end());
sort(even.begin(), even.end());
for (int i = 0; i < odd.size(); i++) {
if (odd[i] + even[i] > 0) {
sum += odd[i] + even[i];
}
}
printf("%lld\n", sum);
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
// Acknowledgement: Special thanks to kyomukyomupurin, who developed this
// template.
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
int n = 0;
for (auto e : vec) os << (n++ ? ", " : "{") << e;
return os << (n ? "}" : "{}");
}
template <class T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& st) {
int n = 0;
for (auto e : st) os << (n++ ? ", " : "{") << e;
return os << (n ? "}" : "{}");
}
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) {
int n = 0;
for (auto e : mp) os << (n++ ? ", " : "{") << e;
return os << (n ? "}" : "{}");
}
template <class T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
for (T& e : vec) is >> e;
return is;
}
#ifdef LOCAL
#define debug(...) \
std::cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
void debug_out() { std::cerr << '\n'; }
template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
std::cerr << head;
if (sizeof...(Tail) != 0) std::cerr << ", ";
debug_out(std::forward<Tail>(tail)...);
}
using namespace std;
using int64 = long long;
int64 cnt_diff(string s, char c) {
int64 res = 0;
for (int i = 0; i < (int)s.size(); i++) {
if (s[i] != c) res++;
}
return res;
}
int main() {
string s;
cin >> s;
int N = s.size();
string tmp = "";
tmp += s.back();
int atoha_onazi = N;
char atoha_onazi_c = '*';
int idx = N - 2;
bool consective = false;
int64 cnt = 0;
while (idx >= 0) {
if (s[idx] == atoha_onazi_c && consective == true) {
atoha_onazi--;
idx--;
continue;
}
if (s[idx] == s[idx + 1]) {
char tmp_c = s[idx];
cnt += cnt_diff(tmp, tmp_c);
debug(cnt_diff(tmp, tmp_c));
if (tmp_c != atoha_onazi_c) cnt += N - atoha_onazi;
atoha_onazi = idx;
atoha_onazi_c = tmp_c;
tmp = "";
consective = true;
debug(atoha_onazi_c, atoha_onazi, cnt);
} else {
tmp += s[idx];
consective = false;
}
debug(tmp);
idx--;
}
cout << cnt << '\n';
return 0;
} |
/*
author: l_a_k_s_h_g
*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define f(a,b) for(ll i=a;i<b;i++)
#define mod 1000000007
#define inf 1e9 + 1
#define all(x) (x).begin(),(x).end()
#define prDouble(x,dec) cout << fixed << setprecision(dec) << x
#define triplet pair<ll,pair<ll,ll> >
#define prCase(t) cout << "Case #" << t <<": "
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define endl "\n"
using namespace std;
int main(){
fast
ll n;
ll x;
cin>>n>>x;
x *= 100;
ll k=0;
f(0,n){
ll g,h;
cin>>g>>h;
k += g*h;
if(k > x){
cout<<i+1<<endl;
break;
}
}
if(k <= x){
cout<<-1<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vld vector<ld>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vvld vector<vector<ld>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
ll inf = 1e18;
ll mod = 1e9+7;
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll pwr(ll x, ll y)
{
ll res = 1;
x = x ;
while (y > 0)
{
if (y & 1)
res = (res * x) ;
y = y >> 1;
x = (x * x) ;
}
return res;
}
ll modInverse(ll n, ll p)
{
return power(n, p - 2, p);
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll fact(ll n)
{
if (n <= 1)
return 1;
return n*fact(n-1);
}
ll nPr(ll n, ll r)
{
return fact(n)/fact(n-r);
}
ll ceil(ll n, ll k)
{
if(n%k==0)
return n/k;
else
return n/k + 1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
// cin>>t;
t = 1;
while(t-->0)
{
ll n, x, sum=0, i=0;
cin>>n>>x;
for(i=0; i<n; i++)
{
ll v,p;
cin>>v>>p;
sum+=(v*p);
if(sum>x*100)
{
break;
}
}
if(sum>x*100)
d1(i+1);
else
d1(-1);
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double PI = 4*atan(1);
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const string YES = "Possible";
const string NO = "Impossible";
#define pb push_back
#define mp make_pair
#define eb emplace_back
const int MAX_N = 2e6+10;
ll a, b, c, d;
void solve()
{
if(a == c && b == d){
cout<<0<<'\n';
}else if((abs(a-c)+abs(b-d)<=3)||(a+b==c+d)||(a-b==c-d)){
cout<<1<<'\n';
}else if((a+b)%2==(c+d)%2){
cout<<2<<'\n';
}else{
for(int dx=-3;dx<=3;++dx)for(int dy=-3;dy<=3;++dy){
if(abs(dx)+abs(dy)>3) continue;
int x=a+dx, y=b+dy;
if(abs(x-c)+abs(y-d)<=3||(x+y==c+d)||(x-y==c-d)){
cout<<2<<'\n';
return;
}
}
cout<<3<<'\n';
}
}
int main()
{
ios::sync_with_stdio(false);
cin >> a >> b >> c >> d;
solve();
return 0;
} | #include<bits/stdc++.h>
#define inf 0x3f3f3f3f3f3f3f3fll
typedef unsigned long long ull;
typedef long long ll;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define nep(i,r,l) for(int i=r;i>=l;i--)
void sc(int &x){scanf("%d",&x);}
void sc(int &x,int &y){scanf("%d%d",&x,&y);}
void sc(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void sc(ll &x){scanf("%lld",&x);}
void sc(ll &x,ll &y){scanf("%lld%lld",&x,&y);}
void sc(ll &x,ll &y,ll &z){scanf("%lld%lld%lld",&x,&y,&z);}
void sc(char *x){scanf("%s",x);}
void sc(char *x,char *y){scanf("%s%s",x,y);}
void sc(char *x,char *y,char *z){scanf("%s%s%s",x,y,z);}
void out(int x){printf("%d\n",x);}
void out(ll x){printf("%lld\n",x);}
void out(int x,int y){printf("%d %d\n",x,y);}
void out(ll x,ll y){printf("%lld %lld\n",x,y);}
void out(int x,int y,int z){printf("%d %d %d\n",x,y,z);}
void out(ll x,ll y,ll z){printf("%lld %lld %lld\n",x,y,z);}
using namespace std;
const int N=1005;
int n,m;
vector<int>e[26][N];
bool vis[N][N],updated[N][N];
int dp[N][N];
struct node
{
int u,v,w;
node(int u=0,int v=0,int w=0):u(u),v(v),w(w){}
bool operator<(const node&o)const
{
return w>o.w;
}
};
priority_queue<node>q;
int main()
{
memset(dp,0x3f3f3f3f,sizeof(dp));
//freopen("1.in","r",stdin);freopen("1.out","w",stdout);
sc(n,m);
rep(i,1,m)
{
int u,v;char c;
scanf("%d %d %c",&u,&v,&c);
e[c-'a'][u].push_back(v);
e[c-'a'][v].push_back(u);
vis[u][v]=vis[v][u]=true;
}
dp[1][n]=0;
q.push(node(1,n,0));
while(!q.empty())
{
int u=q.top().u,v=q.top().v;q.pop();
if(updated[u][v]) continue;
updated[u][v]=true;
rep(i,0,25)
{
for(int x:e[i][u])
for(int y:e[i][v])
if(dp[x][y]>dp[u][v]+2)
{
dp[x][y]=dp[u][v]+2;
q.push(node(x,y,dp[x][y]));
}
}
}
int ans=998244353;
rep(i,1,n) ans=min(ans,dp[i][i]);
rep(i,1,n)
rep(j,1,n)
if(vis[i][j]) ans=min(ans,dp[i][j]+1);
if(ans==998244353) ans=-1;
out(ans);
}
|
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define mod 998244353
#define INF 998244353
#define ull unsigned long long
#define pb push_back
#define endl '\n'
#define pi acos(-1)
#define pii pair<ll int,ll int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define SIZE(X) ((int)((X).size()))
#define hell 1000000007
#define rep(i,a,b) for(ll int i=a;i<b;i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define fo(i,s,n) for(int i=s;i<n;i++)
#define FORE(i,s,n) for(int i=s;i<=n;i++)
#define rep(i,n) for(int i=0;i<(n);++i)
#define repf(i,a,b) for(int i=(a);i<=(b);++i)
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) //removes repeated element;
#define Fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
using namespace std;
const int N = 2*100010;
vector <ll> is_prime(N+1 , true);
bool sortinrev(const pair<int,int> &a,const pair<int,int> &b){return (a.first > b.first);}
bool sortbysecdesc(const pair<int,int> &a,const pair<int,int> &b){return a.second>b.second;}
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) {return (a.second < b.second);}
ll powerm(ll a, ll k) { if (!k) {return 1;}ll b = powerm(a, k / 2);b = b * b % INF;if (k % 2) {return a * b % INF;} else {return b;}}
ll int power(ll int a , ll int b) { if(b == 1) return a; if(b == 0) return 1; ll int m1 = power(a,b/2); if(b%2) return m1*m1*a; return m1*m1; }
void seive_of_eras(ll n) { is_prime[0] = 0; is_prime[1] = 0; for(ll i=2;i*i<=n;i++) { if(is_prime[i]) { for(ll j=i*i;j<=n;j+=i) { is_prime[j] = false; } } } }
bool isprime (ll a) { if(a<=1) return false; if(a==2||a==3) return true; if(a%2==0||a%3==0) return false; for(ll i=5;i*i<=a;i=i+6) { if(a%i==0||a%(i+2)==0) return false; } return true;}
vector <ll> adj[N];
ll visited[N];
ll a[N] , b[N];
ll d = 0;
void dfs(ll u)
{
//ll d = 0;
for(ll i=0;i<adj[u].size();i++)
{
if(visited[adj[u][i]]==0)
{
d += b[adj[u][i]]-a[adj[u][i]];
//cout<<" "<<adj[u][i]<<" "<<b[adj[u][i]]<<" "<<a[adj[u][i]]<<" "<<d<<endl;
visited[adj[u][i]] = 1;
dfs(adj[u][i]);
}
}
}
void codeit()
{
ll n , m; cin>>n>>m;
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
for(ll i=1;i<=n;i++)
{
cin>>b[i];
}
for(ll i=0;i<m;i++)
{
ll c , d; cin>>c>>d;
adj[c].pb(d);
adj[d].pb(c);
}
for(ll i=1;i<=n;i++)
{
if(visited[i]==0)
{
visited[i] = 1;
d = b[i]-a[i];
dfs(i);
if(d==0)
{
}
else
{
cout<<"No"<<endl;
//cout<<i<<" "<<d<<endl;
return;
}
}
}
cout<<"Yes"<<endl;
return;
}
int main()
{
//Fast
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ll tt; tt = 1;
//cin>>tt;
for(ll i=0;i<tt;i++)
codeit();
}
| // dont stop until you reach there
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define rep(i,j,k) for(int i=j;i<k;i+=1)
#define rrep(i,j,k) for(int i=n-1;i>=0;i-=1)
#define sorti(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<int>())
#define mm map<int,int>
#define vv vector<int>
#define tmm map<int,map<int,int>>
#define trav(a, x) for(auto& a : x)
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define pb push_back
#define alot(x,y) memset(x,y,sizeof(x))
#define pii pair<int,int>
#define code ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MOD=1e9+7;
const double pi=acos(-1.0);
//memset(array_name,value_to_be_intialized,sizeof(array_name));
// __builtin_popcount(n) count no of set bit in number n
//next_permutation(s.begin(),s.end());
/*int change(string g){
int p=10;
int ans=g[g.length()-1]-'0';
for(int i=g.length()-2;i>=0;i--){
int j=g[i]-'0';
ans=p*ans+j;
}
return ans;
}
int binpow(int a,int b){
int res=1;
while(b>0){
if(b&1)res*=a;
a*=a;
b>>=1;
}
return res;
}
// lower_bound==> if the element is present it return its first occurence else 1st element which is greater than
// upper_bound==> it will always return iterator to element which is greater than searched element
*/
int32_t main ()
{
code;
int tt;
//cin>>tt;
tt=1;
while(tt--){
int n,m;
cin>>n>>m;
vv a;
vv b;
rep(i,0,n){
int p;
cin>>p;
a.pb(p);
}
rep(i,0,n){
int p;
cin>>p;
b.pb(p);
}
int sum1=0,sum2=0;
set<int>st;
rep(i,0,m){
int p,q;
cin>>p>>q;
p--;
q--;
st.insert(p);
st.insert(q);
}
trav(x,st){
sum1+=a[x];
sum2+=b[x];
}
if(m==0){
if(a==b)cout<<"Yes\n";
else cout<<"No\n";
}
else if(sum1==sum2)cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define m_p make_pair
#define sz(x) (int)x.size()
#define line cerr<<"--------------------\n";
#define see(x) cerr<<x<<" "
#define seeln(x) cerr<<x<<endl
#define out(x) cerr<<#x<<" = "<<x<<" "
#define outln(x) cerr<<#x<<" = "<<x<<endl
#define outarr(x,l,r) cerr<<#x"["<<l<<"-"<<r<<"] = "; for (int _i=l;_i<=r;++_i) cerr<<x[_i]<<" ";cerr<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define gc() getchar()
//char buf[1<<23],*p1=buf,*p2=buf;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <class T> void read(T &x)
{
x=0; char c=gc(); int flag=0;
while (c<'0'||c>'9') flag|=(c=='-'),c=gc();
while (c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=gc();
if (flag) x=-x;
}
template <class T> inline T _max(T a,T b){return a>b ? a : b;}
template <class T> inline T _min(T a,T b){return a<b ? a : b;}
template <class T> inline bool checkmax(T &a,T b){return b>a ? a=b,1 : 0;}
template <class T> inline bool checkmin(T &a,T b){return b<a ? a=b,1 : 0;}
int n,k,mod;
int Add(int x,int y){x+=y; return x>=mod ? x-mod : x;}
int Sub(int x,int y){x-=y; return x<0 ? x+mod : x;}
int Mul(int x,int y){return (ll)x*y%mod;}
void add(int &x,int y){x=Add(x,y);}
void sub(int &x,int y){x=Sub(x,y);}
void mul(int &x,int y){x=Mul(x,y);}
int dp[102][128000];
void init()
{
read(n); read(k); read(mod);
dp[0][0]=1;
for (int i=1;i<=n;++i)
{
int tmp=_min(i,50);
for (int r=0;r<=k;++r)
{
for (int j=r*i;j<=tmp*(tmp+1)/2*k;++j)
{
add(dp[i][j],dp[i-1][j-r*i]);
}
}
}
}
int calc(int x)
{
int tmp=_min(x-1,n-x);
int ans=0;
for (int i=0;i<=tmp*(tmp+1)/2*k;++i)
{
if (i) add(ans,Mul(Mul(dp[x-1][i],dp[n-x][i]),k+1));
else add(ans,k);
}
return ans;
}
void solve()
{
for (int i=1;i<=n;++i)
{
printf("%d\n",calc(i));
}
}
int main()
{
init();
solve();
return 0;
}
| #include<cstdio>
#include<cstring>
#define fo(x,a,b) for(int x=(a),_e=(b);x<=_e;x++)
#define fd(x,a,b) for(int x=(a),_e=(b);x>=_e;x--)
#define inc(a,b) (a=a+b-(a+b>=mo?mo:0))
#define ww printf
#define min(a,b) ((a)<(b)?(a):(b))
const int N=550006;
int f[101][N],top[101],n,ti,mo;
int main(){
scanf("%d %d %d",&n,&ti,&mo);
f[0][0]=1;
fo(i,1,n) {
memcpy(f[i],f[i-1],sizeof(f[i-1][0])*(top[i-1]+1));
top[i]=top[i-1];
fd(k,top[i],0) {
fo(j,1,ti) inc(f[i][k+j*i],f[i][k]);
}top[i]+=ti*i;
// fo(j,0,top[i]) ww("%d ",f[i][j]);puts("");
}
long long ans;
fo(i,1,n) {
ans=0;
fo(j,0,min(top[i-1],top[n-i])) ans+=1ll*f[i-1][j]*f[n-i][j]%mo*(ti+1)%mo;
ans=(ans+mo-1)%mo;
printf("%lld\n",ans);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define rep(i,a,b,c) for(ll i=(a);i<=(b);i+=(c))
#define repb(i,a,b,c) for(ll i=(a);i>=(b);i-=(c))
#define MOD 1000000007
#define inf 3e18
#define ld long double
#define mp make_pair
#define vpll vector<pair<ll,ll> >
#define vll vector<ll>
#define vld vector<ld>
#define vvll vector<vector<ll>>
#define vvld vector<vector<ld>>
#define pll pair<ll,ll>
#define vvpll vector<vector<pair<ll,ll>>>
#define pqll priority_queue<ll>
#define mll map<ll,ll>
#define mlc map<ll,char>
#define um unordered_map
#define umll um<ll,ll>
#define umlc um<ll,char>
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define ln cout<<"\n";
#define endl "\n"
#define test ll T;cin>>T;while(T--)
#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define show(w,size) for(ll i=0;i<size;i++) cout<<w[i]<<" ";
#define prll(a) cout<<a<<"\n";
#define mset(dp,no) memset(dp,no,sizeof(dp))
#define input(a,n) rep(i,0,n-1,1) cin>>a[i];
#define countbit __builtin_popcount //Number of setbits in decimal notation
#define lb(v,cur) lower_bound(all(v),cur)-v.begin()
#define ub(v,cur) upper_bound(all(v),cur)-v.begin()
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
long long pows(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll powm(ll x,ll y,ll m=MOD){x=x%m;ll res=1;while(y){if(y&1)res=res*x;res%=m;y=y>>1;x=x*x;x%=m;}return res;}
ll modInverse(ll a, ll m=MOD){return powm(a,m-2,m);}
int main()
{
fastIO
ll n,m,i,j,k,x,y,z,t,e,f,p,q,g,l,r,o,w,h,count1=0,prod=1,ans=0,a,b,c,d,index,x1,x2,y1,y2,diff,sum=0,flag=0,flag1=0,flag2=0;
string s,s1,s2;
cin>>n;
ld ar[n];
ld sum1=0.0,sum2=0.0,sum3=0.0;
for(i=0;i<n;i++)
{
cin>>ar[i];
sum1+=abs(ar[i]);
sum2+=(ar[i]*ar[i]);
sum3=max(sum3,abs(ar[i]));
}
sum2=sqrt(sum2);
cout<<fixed<<setprecision(10)<<sum1<<"\n"<<sum2<<"\n"<<sum3;
}
| #include<bits/stdc++.h>
using namespace::std;
int n;
long long k;
long long C(long long x){
return x * (x + 1) / 2;
}
long long f(long long x){
long long ans = 0;
for(int i = 1; i <= n; i++){
long long a = x - i;
if(a < 2) continue;
long long lim = min(1ll * n, a - 1);
long long lim1 = min(1ll * n, (a + 1) / 2);
ans += 2 * (C(lim1 - 1) + a * (lim - lim1) - C(lim) + C(lim1));
ans += min(1ll * n, a / 2);
}
return ans;
}
void solve(int lo, long long k){
int x = -1, y = -1;
for(int i = 1; i <= n; i++){
int a = lo - i;
int l = a / 2 + 1;
int r = min(a - 1, n);
int here = 2 * max(0, r - l + 1) + (a % 2 == 0 and a / 2 <= n);
if(k <= here){
x = i;
break;
}
k -= here;
}
lo -= x;
for(int j = 1; j <= n; j++){
int v = lo - j;
if(1 <= v and v <= n){
k--;
}
if(k == 0){
y = j;
break;
}
}
printf("%d %d %d\n", x, y, lo - y);
}
int main(){
cin >> n >> k;
int lo = 3, hi = 3 * n;
while(lo < hi){
int mi = lo + (hi - lo) / 2;
if(f(mi) < k) lo = mi + 1;
else hi = mi;
}
k -= f(lo - 1);
solve(lo, k);
return 0;
}
|
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef ENVIRONMENT_LINKED_BOOST
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#endif
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using lpair = std::pair<ll, ll>;
#define REP(i, a, b) for (ll i = a; i < b; ++i)
#define REPREV(i, a, b) for (ll i = a; i > b; --i)
const int _ = []() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(10);
return 0;
}();
template <typename value_t>
void resize(value_t& v, const value_t& val) { v = val; }
template <typename vec_t, typename value_t, typename... arg_t>
void resize(std::vector<vec_t>& v, const value_t& val, int size, arg_t... arg) {
v.resize(size);
for (auto& c : v) resize(c, val, arg...);
}
template <typename A, typename B>
void chmin(A& a, const B& b) { a = min(a, static_cast<A>(b)); };
template <typename A, typename B>
void chmax(A& a, const B& b) { a = max(a, static_cast<A>(b)); };
int main() {
int N, K;
cin >> N >> K;
vector<array<int, 3>> A;
REP(i, 0, N) {
REP(j, 0, N) {
int a;
cin >> a;
A.push_back({a, (int)i, (int)j});
}
}
sort(A.begin(), A.end(), greater<array<int, 3>>());
vector<vector<int>> sum(N + 1, vector<int>(N + 1, 0));
int l = -1;
int r = N * N - 1;
while (r - l > 1) {
for (auto& v : sum) {
for (int& x : v) x = 0;
}
int mid = l + (r - l) / 2;
REP(i, 0, mid + 1) {
auto [a, x, y] = A[i];
int px = max(0, x - K + 1);
int py = max(0, y - K + 1);
sum[px][py] += 1;
sum[px][y + 1] -= 1;
sum[x + 1][py] -= 1;
sum[x + 1][y + 1] += 1;
}
REP(i, 0, N + 1) {
REP(j, 1, N + 1) {
sum[i][j] += sum[i][j - 1];
}
}
REP(i, 1, N + 1) {
REP(j, 0, N + 1) {
sum[i][j] += sum[i - 1][j];
}
}
bool ok = false;
REP(i, 0, N - K + 1) {
REP(j, 0, N - K + 1) {
if (sum[i][j] < K * K / 2 + 1) {
ok = true;
break;
}
}
if (ok) break;
}
(ok ? l : r) = mid;
}
cout << A[r][0] << endl;
return 0;
} | #include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 807
using namespace std;
typedef long long ll;
inline int rd() {
int x = 0;
bool f = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) f |= (c == '-');
for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return f ? -x : x;
}
int n, k, lim, a[N][N], sum[N][N];
inline bool valid(int x) {
memset(sum, 0, sizeof(sum));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (a[i][j] <= x) ++sum[i][j];
sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
if (i >= k && j >= k) {
if (sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k] >= lim) return 1;
}
}
return 0;
}
int main() {
n = rd(); k = rd();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) a[i][j] = rd();
lim = k * k / 2 + (k & 1);
int l = 0;
int r = (1 << 30);
while (l < r) {
int mid = (l + r) / 2;
if (valid(mid)) r = mid;
else l = mid + 1;
}
printf("%d\n", l);
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <iomanip>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
using namespace std;
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(WW,n)cerr << v[WW] << ' ';cerr << endl;}
#define SHOW2d(v,WW,HH) {REP(W_,WW){REP(H_,HH)cerr << v[W_][H_] << ' ';cerr << endl;}}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007LL
typedef long long ll;
typedef pair<ll,ll> P;
ll key(vector<vector<P> > &dp, ll l, ll r) {
if(l + 1 == r)return dp[l][r].FI;
if(dp[l][r].SE != -1)return dp[l][r].FI;
ll ret = dp[l][r].FI;
for(ll i = l + 1;i < r;i++) {
ret = max(ret, key(dp, l, i) + key(dp, i, r));
}
dp[l][r].SE = 0;
return dp[l][r].FI = ret;
}
ll seica(vector<ll> &w, vector<P> &h) {
ll n = w.size();
vector<vector<P> > dp(n, vector<P>(n, MP(0, -1)));
// cout << "in seica" << endl;
// cout << "w: ";SHOW1d(w, n);
REP(i, n) {
ll now = w[i];
for(ll j = i + 1;j < n;j++) {
now += w[j];
ll id = lower_bound(ALL(h), MP(now, 0LL)) - h.begin();
if(id == 0)continue;
// cout << i << " " << now << " " << id << " " << h[id].SE << endl;
dp[i][j].FI = h[id-1].SE;
}
}
ll ret = key(dp, 0, n-1);
// cout << ret << endl;
return ret;
}
int main(){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
ll n, m;cin >> n >> m;
vector<ll> w(n);
REP(i, n)cin >> w[i];
sort(ALL(w));
vector<P> h(m);
ll v_min = LLINF;
REP(i, m) {
cin >> h[i].SE >> h[i].FI;
v_min = min(v_min, h[i].FI);
}
h.PB(MP(LLINF, 0));
sort(ALL(h));
REP(i, m) {
h[i + 1].SE = max(h[i].SE, h[i+1].SE);
}
// REP(i, m + 1)cerr << "sorted: " << h[i].FI << " " << h[i].SE << endl;
if(*max_element(ALL(w)) > v_min) {
cout << -1 << endl;
return 0;
}
ll ans = LLINF;
ll cou = 1;
for(ll i = 1;i <= n;i++)cou *= i;
REP(i, cou) {
ans = min(ans, seica(w, h));
next_permutation(ALL(w));
}
if(ans == LLINF)cout << -1 << endl;
else cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
template<class V, int ME> class BIT {
public:
V bit[1<<ME],val[1<<ME];
V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
void add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
void set(int e,V v) { add(e,v-val[e]);}
int lower_bound(V val) {
V tv=0; int i,ent=0;
for(i=ME-1;i>=0;i--) if(tv+bit[ent+(1<<i)-1]<val) tv+=bit[ent+(1<<i)-1],ent+=(1<<i);
return ent;
}
};
BIT<int,20> TL,TR;;
int N,L;
int A[101010],B[101010];
int T[101010];
map<int,int> le,ri;
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N>>L;
A[0]=B[0]=0;
FOR(i,N) {
cin>>A[i+1];
A[i+1]-=i+1;
}
A[N+1]=B[N+1]=L-N;
FOR(i,N) {
cin>>B[i+1];
B[i+1]-=i+1;
}
N+=2;
FOR(i,N) if(ri.count(A[i])==0) ri[A[i]]=i;
for(i=N-1;i>=0;i--) if(le.count(A[i])==0) le[A[i]]=i;
FOR(i,N) {
if(A[i]==B[i]) T[i]=i;
if(A[i]<B[i]) {
if(ri.count(B[i])==0) return _P("-1\n");
T[i]=ri[B[i]];
TR.set(T[i],1);
}
if(A[i]>B[i]) {
if(le.count(B[i])==0) return _P("-1\n");
T[i]=le[B[i]];
TL.set(T[i],1);
}
}
ll ret=0;
FOR(i,N) {
if(A[i]<B[i]) {
ret++;
ret+=TR(T[i]-1)-TR(i);
}
if(A[i]>B[i]) {
ret++;
ret+=TL(i-1)-TL(T[i]);
}
}
cout<<ret<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
|
Subsets and Splits