code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pii pair<int, int>
#define rep(i, a, b) for(int i = a; i < b; i++)
#define rfor(i, b, a) for(int i = b; i >= a; i--)
#define X first
#define Y second
#define pb push_back
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
//const int mod = 1e9 + 7;
int n, m;
const int template_size_array = 1e6 + 5;
int a[template_size_array];
int b[template_size_array];
int c[template_size_array];
string s;
void run_case(){
cin >> n;
int mn = INT_MAX;
rep(i, 0, n){
cin >> a[i];
cin >> b[i];
cin >> c[i];
}
rep(i, 0, n){
if(c[i] - a[i] > 0){
mn = min(mn, b[i]);
}
}
if(mn == INT_MAX){
cout << "-1";
return;
}
cout << mn;
}
int32_t main()
{
ios::sync_with_stdio(0); cin.tie(nullptr);
cout << setprecision(12) << fixed;
int t = 1;
//cin >> t;
while(t--){
run_case();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#ifndef ONLINE_JUDGE
#define dbg(x...) do{cout << "\033[32;1m" << #x << "->" ; err(x);} while(0)
void err(){cout << "\033[39;0m" << endl;}
template<template<typename...> class T,typename t,typename... A>
void err(T<t> a,A... x){for (auto v:a) cout << v << ' '; err(x...);}
template<typename T,typename... A>
void err(T a,A... x){cout << a << ' '; err(x...);}
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
template<class T>
void print(T x,int suc=1)
{
cout<<x;
if(suc==1) cout<<'\n';
else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
for(int i=0;i<v.size();i++)
print(v[i],i==(int)(v.size())-1?suc:2);
}
#define int long long
int a[200050];
//3 5 8 11
int f[200050];
const double eps=1e-6;
const int mod=1e9+7;
int n;
double check(double x) {
double res=0;
rep(i,1,n) {
res+=x+a[i]-min(1.0*a[i],2.0*x);
}
return res;
}
signed main() {
cin>>n;
rep(i,1,n) cin>>a[i];
double L=0,R=1e9;
double ans=0;
while(R-L>eps) {
double midl=L+(R-L)/3;
double midr=R-(R-L)/3;
if(check(midl)<=check(midr)) {
R=midr;
ans=check(midl);
}
else L=midl;
//cout<<L<<" "<<R<<endl;
}
cout<<fixed<<setprecision(10)<<ans/n<<endl;
} |
#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;
ll INF = 1e16;
int inf = 1e9;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() {
cerr << endl;
}
template <typename H, typename... T>
void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T>
void print(T x) {
cout << x << "\n";
}
template <typename T>
void print(vector<T>& x) {
int N = x.size();
REP(i, N) {
if (i > 0) cout << " ";
cout << x[i];
}
cout << "\n";
}
const ll MOD = 1000000007;
//const ll MOD = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%MOD+MOD)%MOD){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD-a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
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(ll t) const {
if (!t) return 1;
mint a = 1;
mint tmp = *this;
while(t > 0) {
if (t & 1) a *= tmp;
tmp *= tmp;
t >>= 1;
}
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;}
typedef vector<mint> vmi;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll n, p; cin >> n >> p;
print(mint(p - 1) * mint(p - 2).pow(n - 1));
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
long long n, c;
cin >> n >> c;
map<int, vector<int>> m;
for (int i = 0; i < n; ++i) {
int a, b, ci;
cin >> a >> b >> ci;
m[a].push_back(ci);
m[b+1].push_back(-ci);
}
long long ans = 0, sum = 0, last = 0;
for (auto i : m) {
ans += min(sum, c) * (i.first - last);
for (auto j : i.second) {
sum += j;
}
last = i.first;
}
cout << ans << '\n';
}
|
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
ll c[500][500];
int main() {
int n;
cin >> n;
rep(i,n)rep(j,n) cin >> c[i][j];
vector<ll> a(n),b(n);
rep(i,n-1){
ll d = c[i+1][0]-c[i][0];
rep(j,n) if(c[i+1][j]-c[i][j]!=d){
cout << "No" << endl;
return 0;
}
a[i+1] = d;
}
rep(j,n-1){
ll d = c[0][j+1]-c[0][j];
rep(i,n) if(c[i][j+1]-c[i][j]!=d){
cout << "No" << endl;
return 0;
}
b[j+1] = d;
}
rep(_,2){
ll s=0,m=0;
rep(i,n) s += a[i], m = min(m,s), a[i] = s;
rep(i,n) a[i] -= m;
swap(a,b);
}
rep(i,n)rep(j,n){
if(c[i][j]<a[i]+b[j]){
cout << "No" << endl;
return 0;
}
}
rep(i,n) a[i] = c[i][0]-b[0];
cout << "Yes" << endl;
rep(i,n) printf("%lld%c",a[i],i==n-1?'\n':' ');
rep(i,n) printf("%lld%c",b[i],i==n-1?'\n':' ');
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define fin(a) { cout << a << endl; return 0; }
#define debug(a) { cerr << #a << ": " << a << endl; }
#define endl '\n'
#define fi first
#define se second
using lint = long long;
using ld = long double;
using P = pair<int,int>;
using Pl = pair<lint,lint>;
template<class T> using V = vector<T>;
template<class T> using priq = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
template<class T> T ceil(const T &a, const T &b) { return ((a)+(b)-1)/b; }
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; }
template<class T> void YesorNo(T x) { printf(x ? "Yes\n" : "No\n"); }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;
template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
return out << '{' << p.first << ',' << p.second << '}';
}
template<class T> istream& operator>>(istream &in, vector<T> &v) {
for(auto &&e : v) in >> e;
return in;
}
template<class T> ostream& operator<<(ostream &out, const vector<T> &v) {
for(const auto &e : v) out << e << ' ';
return out;
}
/*----------------------------------------------------------------------------------------------------*/
int main(){
int n;
cin >> n;
V<V<int>> c(n,V<int>(n));
cin >> c;
bool ok = true;
rep(i,n)rep(j,n-1) {
ok &= (c[i][j+1]-c[i][j] == c[0][j+1]-c[0][j]);
}
rep(j,n)rep(i,n-1) {
ok &= (c[i+1][j]-c[i][j] == c[i+1][0]-c[i][0]);
}
if(!ok) fin("No");
V<int> d(n-1);
Rep(i,n-1) d[i-1] = c[0][i]-c[0][i-1];
int l = -1, r = INF;
while(r-l > 1) {
int mid = (l+r)/2;
int now = mid;
bool ok = true;
rep(i,n-1) {
now += d[i];
if(now < 0) ok = false;
}
if(ok) r = mid;
else l = mid;
}
V<int> b(n); b[0] = r;
rep(i,n-1) b[i+1] = b[i] + d[i];
V<int> a(n);
rep(i,n) {
a[i] = c[i][0]-b[0];
if(a[i] < 0) fin("No");
}
cout << "Yes" << endl;
cout << a << endl << b << endl;
} |
#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};
int64_t dp[200010][2];
map<int, set<int>> mp;
int main() {
rep(i,200010)rep(j,2) dp[i][j] = LINF;
dp[0][0] = dp[0][1] = 0;
int n; cin >> n;
rep(i,n) {
int x, c; cin >> x >> c;
mp[c].insert(x);
}
mp[n + 1].insert(0);
int preL = 0, preR = 0;
int pre_idx = 0;
for(auto [idx, st] : mp) {
int L = *st.begin();
int R = *prev(st.end());
if(L == R) {
dp[idx][0] = dp[idx][1] = min(dp[pre_idx][0] + abs(preL - L), dp[pre_idx][1] + abs(preR - R));
} else {
dp[idx][0] = min(dp[pre_idx][0] + abs(preL - R) + abs(R - L), dp[pre_idx][1] + abs(preR - R) + abs(R - L));
dp[idx][1] = min(dp[pre_idx][0] + abs(preL - L) + abs(R - L), dp[pre_idx][1] + abs(preR - L) + abs(R - L));
}
preL = L;
preR = R;
pre_idx = idx;
}
cout << dp[n + 1][0] << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int a,b,c;
double f[105][105][105],ans;
int main() {
cin>>a>>b>>c;
f[a][b][c]=1;
for(int i=0; i<100; i++) {
for(int j=0; j<100; j++) {
for(int k=0; k<100; k++) {
if (i && (i + j + k) > 0)
f[i + 1][j][k] += f[i][j][k] * (i) / (i + j + k);
if (j && (i + j + k) > 0)
f[i][j + 1][k] += f[i][j][k] * (j) / (i + j + k);
if (k && (i + j + k) > 0)
f[i][j][k + 1] += f[i][j][k] * (k) / (i + j + k);
}
}
}
for(int i=0;i<=100;i++){
for(int j=0;j<=100;j++){
if(i>=b&&j>=c) ans+=(double)((i-b)+(100-a)+(j-c))*f[100][i][j];
if(i>=a&&j>=c) ans+=(double)((i-a)+(100-b)+(j-c))*f[i][100][j];
if(i>=a&&j>=b) ans+=(double)((i-a)+(j-b)+(100-c))*f[i][j][100];
}
}
printf("%.9lf",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
#define rep(i, n) for (int i=0; i<(n); ++i)
#define rrep(i, n) for (int i=0; i<=(n); ++i)
#define each(v, i) for (auto i : v)
#define pb push_back
#define sz (int)(x).size()
#define fi first
#define se second
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#define pcnt __builtin_popcount
#define rng(a) a.begin(), a.end()
template<typename T> using vc = vector<T>;
template<typename T> using vv = vector<vector<T>>;
using vi = vc<int>;
using vvi = vv<int>;
template<int mod>
struct modint {
const static int _mod = mod;
int _v;
// constructor
modint() { _v = 0; }
modint(const modint& o) { _v = o._v; }
modint(int v) { _v = ((v%_mod)+_mod)%_mod; }
// operator
modint& operator+=(const modint& rhs) {
_v = ((_v + rhs._v) % _mod + _mod) % _mod;
return *this;
}
modint& operator-=(const modint& rhs) {
_v = ((_v - rhs._v) % _mod + _mod) % _mod;
return *this;
}
modint& operator*=(const modint& rhs) {
_v = (int)(((long long)_v * rhs._v) % _mod);
return *this;
}
modint& operator/=(const modint& rhs) {
return *this = *this * rhs.inv();
}
modint pow(int n) const {
modint x = *this, ret = 1;
while (n) {
if (n & 1) ret *= x;
x *= x;
n >>= 1;
}
return ret;
}
modint inv() const {
return pow(_mod - 2);
}
friend modint operator+(const modint& a, const modint& b) {
return modint(a)+=b;
}
friend modint operator-(const modint& a, const modint& b) {
return modint(a)-=b;
}
friend modint operator*(const modint& a, const modint& b) {
return modint(a)*=b;
}
friend modint operator/(const modint& a, const modint& b) {
return modint(a)/=b;
}
friend bool operator==(const modint& a, const modint& b) {
return a._v == b._v;
}
friend bool operator!=(const modint& a, const modint& b) {
return a._v != b._v;
}
friend ostream& operator<<(ostream& out, const modint& rhs) {
out << rhs.val();
return out;
}
int val() const {
return _v;
}
};
using mint = modint<1000000007>;
const int M = 100005;
mint fib[M];
void init() {
rep(i, M) {
if (i <= 3) fib[i] = i;
else fib[i] = fib[i-1] + fib[i-2];
}
}
mint get(int len) {
return fib[max(1, len)];
}
int main() {
int N; cin >> N;
vi a(N); rep(i, N) cin >> a[i];
init();
mint ans = 0;
rep(i, N) {
int left = i, right = N-i;
ans += get(left) * get(right) * a[i];
if (i > 0) {
ans -= get(left-1) * get(right-1) * a[i];
}
}
cout << ans << endl;
return 0;
} | #include <stdio.h>
typedef long long ll;
int main(void) {
ll i, n, now, ans = 0, t;
scanf("%lld", &n);
for(i = 1; i <= n; ++i) {
t = 1;
now = i;
while(now) {
if(now % 8 == 7) {
t = 0;
break;
}
now /= 8;
}
now = i;
while(now) {
if(now % 10 == 7) {
t = 0;
break;
}
now /= 10;
}
ans += t;
}
printf("%lld", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rrep(i,a,b) for (int i = (a); i < (b); ++i)
#define erep(i,a,b) for (int i = (a); i <= (b); ++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),rend()
#define yn(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define fore(i,a) for(auto &i:a)
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
ll myceil(ll a,ll b){return (a+(b-1))/b;}
ll myfloor(ll a,ll b){return a/b;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
int main() {
string s;
cin >>s;
reverse(all(s));
fore(a,s) {
if(a=='6') cout << '9';
else if(a=='9') cout << '6';
else cout << a;
}
cout << endl;
return 0;
} | #include <iostream>
#include <cstring>
using namespace std;
int main() {
char s[100001];
cin>>s;
int n;
n=strlen(s);
for(int i=n-1;i>=0;i--){
if(s[i]=='6'){
cout<<'9';
}
else if(s[i]=='9'){
cout<<'6';
}
else{
cout<<s[i];
}
}
} |
#include<bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i,n) for(ll i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
int main() {
int n, k, m;
cin >> n >> k >> m;
vector<int> a(n);
rep(i, n-1)cin >> a.at(i);
int ans = accumulate(all(a), 0);
m *= n;
if(m <= ans)cout << 0;
else {
if(m-ans <= k)cout << m-ans;
else cout << -1;
}
}
| // Created by ...
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define rep(i,a,b) for(int i=a; i<b; i++)
#define repe(i,a,b) for(int i=a; i<=b; i++)
#define ll long long
#define ui unsigned int
#define pb push_back
#define deb(x) cout << #x << '=' << x << endl
#define deb2(x,y) cout << #x << '=' << x << << #y << '=' << y << endl
#define all(x) x.begin(), x.end()
#define endl "\n"
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define INF (int)1e9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define hcf(x,y) __gcd(x,y)
#define lcm(x,y) (x*y)/(__gcd(x,y))
#define CountOne(x) __builtin_popcount(x)
#define parity(x) __builtin_parity(x) //Funtion return (true) if number of set bits is odd(odd parity) else false..
#define LeadZero(x) __builtin_clz(x) //Counts leading zero in binary representation of x ....
#define TrailZero(x) __builtin_ctz(x)
#define fastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef map<int, int> mii;
typedef unordered_map<int, int> umap_ii;
typedef unordered_map<int, int> umap_ii;
typedef unordered_map<string, int> umap_si;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
//alt + ctrl + N
//===============================================================================//
ll power(ll x, ll y) {
ll v = 1; while (y > 0) { if (y & 1)v = v * x; y = y >> 1; x = x * x;} return v;
}
bool isPowerOfTwo(ll x){
return x && (!(x & (x-1))); //check if the number is power of two or not
}
ll ceil(ll a,ll b){ return (a+b-1)/b; }
//===============================================================================//
int main(){
fastIO;
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll test = 1;
//cin >> test;
while(test--){
vi a(4);
rep(i,0,4) cin >> a[i];
cout << *min_element(all(a)) << endl;
}
return 0;
} |
#include<map>
#include<cmath>
#include<list>
#include<queue>
#include<bitset>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define lbw 1000000009
#define inf 2147483647
using namespace std;
ll read(){ll ans=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9')ans=ans*10+c-'0',c=getchar();return ans*f;}
ll n,m,v[100005],first[100005],cnt,fa[100005],root=1,dp[100005][2],x,y,z,now,now_val;
struct edge{
ll to,nxt;
}e[200005];
void add(ll x,ll y){
e[++cnt].nxt=first[x];
e[cnt].to=y;
first[x]=cnt;
}
void dfs(ll now,ll f){
dp[now][1]=1;
for(int i=first[now],v;i;i=e[i].nxt){
v=e[i].to;
if(v==f)continue;
dfs(v,now);
dp[now][0]+=dp[v][1];
dp[now][1]+=min(dp[v][1],dp[v][0]);
}
}
ll sum(ll n){
return n*(n+1)/2;
}
int main(){
n=read();ll ans=0;
for(int i=1;i<=n;i++){
ll l=read(),r=read();
ans+=sum(r)-sum(l-1);
}
cout<<ans;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
#define rep(i,n) for(ll i=0;i<(ll)n;++i)
int main(){
ll n;cin >> n;
ll ans = 0;
rep(i,n){
ll a,b;cin >> a >> b;
ans += b*(b+1)/2 - (a-1)*a/2;
}
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
constexpr int N = 5005;
constexpr long long MOD = 998244353;
int n, m, k;
vector< string> s;
long long dp[N][N];
long long power(long long a, int b) {
long long rv = 1;
while (b) {
if (b & 1) rv = rv * a % MOD;
(a *= a) %= MOD;
b /= 2;
}
return rv;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m >> k;
s.resize(n, string(m, '.'));
for (int i = 0; i < k; i++) {
int x, y; char c;
cin >> x >> y >> c;
x--; y--;
s[x][y] = c;
}
dp[0][0] = power(3, n * m - k);
long long val = 2 * power(3, MOD - 2) % MOD;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (i == 0 && j == 0) continue;
if (j > 0) {
if (s[i][j - 1] == 'R' || s[i][j - 1] == 'X') dp[i][j] += dp[i][j - 1];
if (s[i][j - 1] == '.') dp[i][j] += dp[i][j - 1] * val % MOD;
}
if (i > 0) {
if (s[i - 1][j] == 'D' || s[i - 1][j] == 'X') dp[i][j] += dp[i - 1][j];
if (s[i - 1][j] == '.') dp[i][j] += dp[i - 1][j] * val % MOD;
}
dp[i][j] %= MOD;
}
}
cout << dp[n - 1][m - 1] << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
struct edge {int to, cost; };
int main(){
int h, w, n, m;
cin >> h >> w >> n >> m;
vector<vector<int>> mp1(h, vector<int>(w,0));
vector<vector<int>> mp2(h, vector<int>(w,0));
vector<P> a;
for(int i=0; i<n; i++){
int x, y;
cin >> x >> y;
x--; y--;
a.push_back(make_pair(x,y));
}
for(int i=0; i<m; i++){
int x, y;
cin >> x >> y;
x--; y--;
mp1[x][y] = -1;
mp2[x][y] = -1;
}
for(int i=0; i<n; i++){
int x, y;
x = a[i].first;
y = a[i].second;
if(mp1[x][y] == 1) continue;
for(int j=x; j<h; j++){
if(mp1[j][y] == -1) break;
mp1[j][y] = 1;
}
for(int j=x; j>=0; j--){
if(mp1[j][y] == -1) break;
mp1[j][y] = 1;
}
}
for(int i=0; i<n; i++){
int x, y;
x = a[i].first;
y = a[i].second;
if(mp2[x][y] == 1) continue;
for(int j=y; j<w; j++){
if(mp2[x][j] == -1) break;
mp2[x][j] = 1;
}
for(int j=y; j>=0; j--){
if(mp2[x][j] == -1) break;
mp2[x][j] = 1;
}
}
int ans = 0;
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
if(mp1[i][j]>0 || mp2[i][j]>0) ans++;
}
}
cout << ans << endl;
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define vpii vector<pair<int,int> >
#define all(x) x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y) for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x) (int)x.size()
#define db(x) cout<<x<<"\n";
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//insert,find_by_order,order_of_key,lower_bound,upper_bound;
#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
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 deb(...)
#endif
//////////////////////////////code//////////////////////////////
const int N = 2e5 + 5;
int ans , n;
int dp[N];
vpii adj[N];
void dfs(int node,int p,int w)
{
dp[node] = w;
for(auto &c :adj[node])
{
if(c.ff == p)
continue;
dfs(c.ff , node , c.ss ^ dp[node]);
}
}
void solve()
{
cin >> n;
for(int i = 0;i < n - 1; i++)
{
int u , v , w;
cin >> u >> v >> w;
adj[u].pb({v , w});
adj[v].pb({u , w});
}
ans = 0;
dfs(1 , -1 , 0);
int bit[62] = {};
for(int i = 1;i <= n ; i++)
{
int num = dp[i];
int j = 0;
while(num)
{
if(num&1)
bit[j]++;
num>>=1;
j++;
}
}
int curr = 1;
for(int i = 0;i < 62 ; i++)
{
int now = bit[i];
now *= (n - bit[i]);
now%=mod;
int p = curr%mod;
now*=p;
now%=mod;
ans += now;
ans%=mod;
// deb(ans, now);
curr<<=1;
}
db(ans)
}
int32_t main()
{
LETS_GET_SCHWIFTY;
#ifndef ONLINE_JUDGE
freopen("INP.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
//cin >> t;
while (t--)
solve();
}
// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values
| #include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define pi 3.14159265358979323846264338327950288419716939937510
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for(i=n-1;i>=0;i--)
#define rep(i,n) for(i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define at(s,pos) *(s.find_by_order(pos))
#define set_ind(s,val) s.order_of_key(val)
long long int M = 1e9 + 7 ;
long long int inf = 9 * 1e18;
//CLOCK
ll begtime = clock();
#define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//CLOCK ENDED
ll n, m;
// modular exponentiation
ll binpow(ll val, ll deg)
{
if (deg < 0)
return 0;
if (!deg)
return 1 % M;
if (deg & 1)
return binpow(val, deg - 1) * val % M;
ll res = binpow(val, deg >> 1);
return (res * res) % M;
}
//binomial
ll modinv(ll n) {
return binpow(n, M - 2);
}
ll gcd(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll x1, y1;
ll d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0, ll &g) {
g = gcd(abs(a), abs(b), x0, y0);
if (c % g) {
return false;
}
x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
return true;
}
void shift_solution(ll & x, ll & y, ll a, ll b, ll cnt) {
x += cnt * b;
y -= cnt * a;
}
ll find_all_solutions(ll a, ll b, ll c, ll minx, ll maxx, ll miny, ll maxy) {
ll x, y, g;
if (!find_any_solution(a, b, c, x, y, g))
return inf;
a /= g;
b /= g;
ll sign_a = a > 0 ? +1 : -1;
ll sign_b = b > 0 ? +1 : -1;
shift_solution(x, y, a, b, (minx - x) / b);
if (x < minx)
shift_solution(x, y, a, b, sign_b);
if (x > maxx)
return inf;
ll lx1 = x;
shift_solution(x, y, a, b, (maxx - x) / b);
if (x > maxx)
shift_solution(x, y, a, b, -sign_b);
ll rx1 = x;
shift_solution(x, y, a, b, -(miny - y) / a);
if (y < miny)
shift_solution(x, y, a, b, -sign_a);
if (y > maxy)
return inf;
ll lx2 = x;
shift_solution(x, y, a, b, -(maxy - y) / a);
if (y > maxy)
shift_solution(x, y, a, b, sign_a);
ll rx2 = x;
if (lx2 > rx2)
swap(lx2, rx2);
ll lx = max(lx1, lx2);
ll rx = min(rx1, rx2);
if (lx > rx)
return inf;
return lx;
}
int main() {
// your code goes here
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll i, j, t, k, x, y, z, N;
cin >> t;
while (t--) {
ll p, q;
cin >> x >> y >> p >> q;
ll a = 2 * (x + y), b = -(p + q);
ll mn = inf;
for (i = x; i < x + y; i++) {
for (j = p; j < p + q; j++) {
ll c = j - i;
z = find_all_solutions(a, b, c, 0, 1e17, 0, 1e17);
if (z != inf) {
mn = min(mn, a * z + i);
}
}
}
if (mn == inf) cout << "infinity\n";
else cout << mn << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef int_fast32_t int32;
typedef int_fast64_t int64;
const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;
#define YES(n) cout << ((n) ? "YES\n" : "NO\n" )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n" )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n" )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;
template<class T> inline bool chmax(T& a, T b) {
if (a < b) { a = b; return true; } return false;
}
template<class T> inline bool chmin(T& a, T b) {
if (a > b) { a = b; return true; } return false;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
vector<int32> a(3);
REP(i,3)cin >> a[i];
sort(all(a));
Yes(a[1] - a[0] == a[2] - a[1]);
return 0;
} | #include<cstdio>
#include<cstring>
#define N 300001
using namespace std;
bool chose[N];
int a[N],answer;
int n,left[N],right[N];
void dfs(int k)
{
if (k==n)
{
int num=1;
left[num]=1;
for (int i=1;i<n;i++)
{
if (chose[i])
{
right[num]=i;
left[++num]=i+1;
}
}
right[num]=n;
int ans=0;
for (int i=1;i<=num;i++)
{
int cnt=0;
for (int j=left[i];j<=right[i];j++) cnt|=a[j];
ans^=cnt;
}
if (ans<answer) answer=ans;
return;
}
chose[k]=false;
dfs(k+1);
chose[k]=true;
dfs(k+1);
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(chose,false,sizeof(chose));
answer=2e9;
dfs(1);
printf("%d\n",answer);
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
auto f = [&](string s) {
int res = 0;
for (char c : s) res += c-'0';
return res;
};
int ans = max(f(a), f(b));
cout << ans << '\n';
return 0;
} | /**
*
* @Author : Sindys
* Language : C++
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <iomanip>
#include <map>
#include <set>
#include <limits>//numeric_limits<int>::max() ... numeric_limits<int>::min()
#include <cmath>
#include <bitset>
#include <tuple>
#include <deque>
#include <stack>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define vpii vector<pii>
#define vi vector<int>
#define vd vector<double>
#define vll vector<ll>
#define INF 1000000000
#define pb push_back
#define FOR(i,a,b,p) for (int i = a; i < b; i += p)
#define ROF(i,b,a,p) for (int i = b-1; i >= a; i -= p)
#define mp make_pair
#define f first
#define s second
#define FORX(a,b,n) for (; a < b; a += n)//for every +n operation
#define ROFX(a,b,n) for (; a > b; a -= n)//for every -n operation
const int M = 1000000007;
//memset(memo, -1, sizeof memo); // initialize DP memoization table with -1
//memset(arr, 0, sizeof arr); // to clear array of integers
// index = (index + 1) % n; // index++; if (index >= n) index = 0;
// index = (index + n - 1) % n; // index--; if (index < 0) index = n - 1;
// int ans = (int)((double)d + 0.5); // for rounding to nearest integer
ll sumdig(int a){
ll res = 0;
while(a>0) {
res += a%10;
a /= 10;
}
return res;
}
void solve() {
int a, b;
cin>>a>>b;
if (sumdig(a) > sumdig(b)) {
cout<<sumdig(a)<<'\n';
}
else {
cout<<sumdig(b)<<'\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
/*stuff you should look for:
-->overflow,array bounds
-->special cases n=1..
-->use printf for precision or setprecision on cout*/ |
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 2e5 + 100;
int n, q;
int fa[N], clas[N];
map<int, map<int, int>> son_num;
int Find(int x) {
// return fa[x] == x ? fa[x] : fa[x] = Find(fa[x]);
if (fa[x] == x) return fa[x];
else return fa[x] = Find(fa[x]);
}
void Union(int x, int y) {
x = Find(x);
y = Find(y);
if (x != y) {
if (son_num[x].size() >= son_num[y].size()) {
fa[y] = x;
for (const auto &[_class, num] : son_num[y]) {
son_num[x][_class] += num;
}
} else {
fa[x] = y;
for (const auto &[_class, num] : son_num[x]) {
son_num[y][_class] += num;
}
}
}
}
void Init() {
for (int i = 0; i < N; i++) {
fa[i] = i;
}
}
int main() {
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);
Init();
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> clas[i];
son_num[i][clas[i]] = 1;
}
for (int i = 0; i < q; i++) {
int op, x, y;
cin >> op >> x >> y;
if (op == 1) {
Union(x, y);
} else {
cout << son_num[Find(x)][y] << "\n";
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// #define int long long
inline int read() {
int x = 0, f = 1; char c = getchar();
while (!isdigit(c)) { if (c == '-') f = - f; c = getchar(); }
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
return x * f;
}
const int inf = 1e9 + 1e7, N = 2e5 + 1e1;
const long long INF = 1e18 + 1e9;
struct Edge { int nxt, to; } e[N << 1];
struct Node { int ls, rs, w; } ;
int head[N], fa[N], rt[N], c[N], n, q, tot;
struct WSegmentTree {
#define ls(x) tree[x].ls
#define rs(x) tree[x].rs
Node tree[N << 5];
int nodecnt;
void Pushup(int x) { tree[x].w = tree[ls(x)].w + tree[rs(x)].w; return ; }
void Insert(int l, int r, int p, int &x) {
if (!x) x = (++ nodecnt);
if (l == r) tree[x].w = 1;
else {
int mid = l + r >> 1;
if (p <= mid) Insert(l, mid, p, ls(x));
else Insert(mid + 1, r, p, rs(x)); Pushup(x);
} return ;
}
void Merge(int l, int r, int u, int &x) {
if (!u || !x) { x += u; return ; }
if (l == r) tree[x].w += tree[u].w;
else {
int mid = l + r >> 1;
Merge(l, mid, ls(u), ls(x)), Merge(mid + 1, r, rs(u), rs(x)), Pushup(x);
} return ;
}
int Query(int l, int r, int p, int x) {
if (!x) return 0;
if (l == r) return tree[x].w;
else {
int mid = l + r >> 1, res = 0;
if (p <= mid) res += Query(l, mid, p, ls(x));
else res += Query(mid + 1, r, p, rs(x));
return res;
} return 0;
}
} wst;
void Addedge(int u, int v) { e[++ tot] = (Edge){ head[u], v }, head[u] = tot; return ; }
int Find(int x) { return (fa[x] == x ? x : fa[x] = Find(fa[x])); }
signed main() {
n = read(), q = read();
for (int i = 1; i <= n; i ++) fa[i] = i, c[i] = read();
for (int i = 1; i <= n; i ++) wst.Insert(1, n, c[i], rt[i]);
while (q --) {
int op = read(), x = read(), y = read();
if (op == 1) {
int u = Find(x), v = Find(y);
if (u != v) wst.Merge(1, n, rt[u], rt[v]), fa[u] = v;
}
else printf("%d\n", wst.Query(1, n, y, rt[Find(x)]));
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int (i) = 0; (i) < (int)(n); (i)++)
int N, K;
vector<pair<long long, long long>> AB;
long long solve() {
long long tmp = 0;
long long balance = K;
rep(i, N) {
long long A = AB[i].first; long long B = AB[i].second;
if (A > tmp + balance) return tmp + balance;
balance -= A - tmp;
tmp = A; balance += B;
}
return tmp + balance;
}
int main() {
cin >> N >> K; AB.resize(N);
rep(i, N) cin >> AB[i].first >> AB[i].second;
sort(AB.begin(), AB.end());
cout << solve() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, start, n) for (int i = start; i < (n); i++)
using ll = long long;
int main()
{
int N;
cin >> N;
string S;
string T;
cin >> S >> T;
int Szero = 0;
int Tzero = 0;
rep(i,0,N){
if(S.at(i) == '0') Szero++;
if(T.at(i) == '0') Tzero++;
}
if(Szero != Tzero){
cout << -1 << endl;
return 0;
}
int ans = 0;
Szero = 0;
Tzero = 0;
rep(i,0,N){
if(Szero == Tzero && S.at(i) == '0' && T.at(i) == '0') ans--;
if(S.at(i) == '0') Szero++;
if(T.at(i) == '0') Tzero++;
}
ans += Szero;
cout << ans << endl;
return 0;
} |
#include <iostream>
int main(){
int A, B, W, Wg;
int min=1000001, max=0;
std::cin >> A >> B >> W;
Wg = W * 1000;
for (int i = 1; i <= 1000000; i++) {
if ((A * i <= Wg) && (B * i >= Wg)) {
if (i < min) min = i;
if (i > max) max = i;
}
}
if (min == 2001 || max == 0) {
std::cout << "UNSATISFIABLE" << std::endl;
} else {
std::cout << min << " " << max << std::endl;
}
} | /*
As fire is shrouded in smoke, a mirror by dust and a child by the womb,
so is the universe enveloped in desire.
It is the wise man’s constant enemy; it tarnishes the face of wisdom.
It is as insatiable as a flame of fire.
It works through the senses, the mind and the reason;
and with their help destroys wisdom and confounds the soul.
Therefore, O Arjuna, first control thy senses and then slay desire,
for it is full of sin, and is
the destroyer of knowledge and of wisdom.
-- "SHRI BHAGVAT GITA"
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define popb pop_back
#define mp make_pair
#define ve vector
#define vii vector < int >
#define vll vector < ll >
#define pii pair < int , int >
#define pll pair < ll , ll >
#define vpl vector < pll >
#define fi first
#define sz size
#define len length
#define se second
//const ll mod=998244353;
const ll mod = 100000000000000000+7;
const ll N=10000000+6;
#define M_PI 3.14159265358979323846
//-------------
void ps(string st)
{
int n = st.size();
int z = 0 , d = 0;
for(char l : st)
if(l == '0')
z++;
else
if(l == '-')
d++;
if(d + z < n)
{
for(char k : st)
if(k != '-')
cout << k;
return;
}
cout << 0;
}
//--------------------------------------------
bool remone(int mo , string &st){
int n = st.size();
for(int i = n-1; i >= 0 ; i--)
{
int v = st[i] - '0';
if(v % 3 == mo)
{
st[i] = '-';
return true;
}
}
return false;
}
bool remtwo(int mo , string &st)
{
int f = 0;
for(int i = st.size() - 1 ; i >= 0; i--)
{
int v = st[i] - '0';
if(v % 3 == mo)
{
st[i] = '-';
f++;
}
if(f == 2) break;
}
if(f == 2)
return true;
return false;
}
bool ans(int a , int b , int c){
if(a == (b + c))
return true;
return false;
}
//============================================
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen ("INPUT.txt" , "r" , stdin);
freopen ("OUTPUT.txt", "w" , stdout);
#endif
int a , b ,c ;
cin >> a >> b >> c;
if(ans(2*a , b , c) || ans(2*a , c , b) || ans(2*b , a , c) || ans(2*b , c , a) || ans(2*c , a , b) || ans(2*c , b , a) )
cout << "Yes";
else
cout << "No";
return 0;
} |
#line 1 "c.cpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return 1;
}
return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __Setup;
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
void Case(int i) { cout << "Case #" << i << ": "; }
#pragma endregion Macros
#line 1 "/home/siro53/kyo-pro/compro_library/math/ceil_floor_div.hpp"
template<class T>
T ceil_div(T a, T b) {
if(b < 0) a = -a, b = -b;
return (a >= 0 ? (a + b - 1) / b : a / b);
}
template<class T>
T floor_div(T a, T b) {
if(b < 0) a = -a, b = -b;
return (a >= 0 ? a / b : (a - b + 1) / b);
}
#line 70 "c.cpp"
int main() {
int A, B;
cin >> A >> B;
int ans = 1;
FOR(g, 1, 200000 + 1) {
int l = (A-1)/g;
int r = B/g;
if(r - l >= 2) chmax(ans, g);
}
cout<<ans<<endl;
}
| // Problem: C - Max GCD 2
// Contest: AtCoder - Japanese Student Championship 2021
// URL: https://atcoder.jp/contests/jsc2021/tasks/jsc2021_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// Problem: C. The Sports Festival
// Contest: Codeforces - Codeforces Round #715 (Div. 2)
// URL: https://codeforces.com/contest/1509/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
/*
authored by
aden_frost
on 07-03-2021
Go Corona Corona Go!!
hurray! mission - 1 - completed;
mission - 2
____________
40 more to go
*/
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define lli long long int
#define MOD1 998244353
#define MOD 1000000007
using namespace std;
using namespace std::chrono;
void fastaf(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void solve(void)
{
lli A,B;
cin>>A>>B;
map<lli,lli> mp;
lli maxx=INT_MIN;
for(lli i=A;i<=B;i++)
{
for(lli j=1;j<=sqrt(i);j++)
{
if(i%j==0)
{
if(i/j==j)
{
if(mp.find(j)!=mp.end())
maxx=max(maxx,j);
mp[j]++;
}
else
{
if(mp.find(j)!=mp.end())
maxx=max(maxx,j);
if(mp.find(i/j)!=mp.end())
maxx=max(maxx,i/j);
mp[j]++;
mp[i/j]++;
}
}
}
}
cout<<maxx;
}
/*
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
int main()
{
fastaf();
// int Testcases;
// cin>>Testcases;
// //freopen("input.txt","r",stdin);
// //freopen("output.txt","w",stdout);
// while(Testcases--)
// {
solve();
cout<<"\n";
// }
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
int A[3];
for (int i = 0; i < 3; ++i)
cin >> A[i];
sort(A, A + 3);
if (A[0] + A[2] - 2 * A[1] == 0)
cout << "Yes\n";
else
cout << "No\n";
}
| #include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include <cmath>
#include <stdio.h>
using namespace std;
#define mod 1000000007
#define mod2 998244353
#define ten5 100005
#define ten52 200005
#define ten53 300005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define mkpr(x1,x2) make_pair(x1,x2)
#define _debug 1
#if _debug
#endif // _debug
typedef long long int ll;
typedef unsigned long long int ull;
ll num[100];
void solve()
{
for(int i=0;i<3;i++)
{
for(int j=i+1;j<3;j++)
{
if(num[i] > num[j])
swap(num[i],num[j]);
}
}
}
int main(void)
{
ll m,n,i=0,j=0,k=1;
while(i<3)
{
cin>>num[i++];
}
solve();
if(num[2] - num[1] == num[1] - num[0]) cout<<"Yes";
else cout<<"No";
return 0;
}
|
//================code===================//
//#define TLE
#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>
#include <random>
#include <chrono>
//#include <stdint.h>
#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld long double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<LL,LL>
#define micro 0.000000000001
using namespace std;
LL gcd(LL a, LL b)
{
if (!(a && b)) return max(a, b);
return a % b ? gcd(b, a % b) : b;
}
#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#else
#define AT
#define AE
#define ce(t)
#endif
pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{-1,0},{-1,-1}, {0,-1} , {1,-1},{-1,-1} }; //RDLU EWSN
template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };
const int max_v = 1e4 + 7;
const int max_k = 8e2 + 7;
const int bsz = (1ll << 10) + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = LNF; //998244353
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a < 0) a = a % mod + mod; if (a >= mod) a -= mod; };
LL arr[max_k][max_k];
LL p[max_k][max_k];
int main()
{
#ifdef OHSOLUTION
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, k; ci(n >> k);
struct node
{
LL r, c;
LL v;
};
vector<node> vi;
fa(i, 1, n + 1) fa(j, 1, n + 1) ci(arr[i][j]), vi.push_back({ i,j,arr[i][j] });
sort(vi.begin(), vi.end(), [&](node& a, node& b) {
return a.v < b.v;
});
int l = 0;
int r = vi.size();
int tar = k*k - (k * k / 2ll + 1)+1;
auto ck = [&](int m)
{
memset(p, 0, sizeof(p));
fa(i, 0, m+1)
{
if (i >= vi.size()) continue;
p[vi[i].r][vi[i].c] = 1;
}
fa(i, 1, n + 1) fa(j, 1, n + 1) p[i][j] += p[i][j - 1];
fa(j, 1, n + 1) fa(i, 1, n + 1) p[i][j] += p[i-1][j];
auto f = [&](int ar, int ac, int nr, int nc)
{
return p[nr][nc] + p[ar - 1][ac - 1] - p[nr][ac - 1] - p[ar - 1][nc];
};
fa(i, 1, n - k + 2) fa(j, 1, n - k + 2)
{
if (f(i, j, i + k-1, j + k-1) >= tar) return true;
}
return false;
};
while (l ^ r)
{
int mid = l + r >> 1;
if (ck(mid)) r = mid;
else l = mid + 1;
}
co(vi[l].v);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i <= n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
ll n, m;
cin >> n >> m;
ll a, b;
vvll g(n);
REP(i, m) {
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
ll k;
cin >> k;
vll c(k);
REP(j, k) {
cin >> c[j];
c[j]--;
}
auto BFS = [&](int s){
vll cost(n, INF);
cost[s] = 0;
queue<int> q;
q.push(s);
while(q.size()){
int x = q.front();
q.pop();
for(int y : g[x]) if(chmin(cost[y], cost[x] + 1)) q.push(y);
}
for(int i = 0; i < k; i++) cost[i] = cost[c[i]];
cost.resize(k);
return cost;
};
vvll d(k);
REP(i, k) d[i] = BFS(c[i]);
vvll dp(k, vll(1 << k, INF));
REP(i, k) dp[i][0] = 0;
for(int bit = 1; bit < (1 << k); bit++){
REP(j, k) if(bit >> j & 1) {
REP(x, k){
ll cost = dp[j][bit - (1 << j)] + d[j][x];
if (bit == ((1 << k) - 1)) chmin(dp[x][bit], dp[j][bit - (1 << j)]);
chmin(dp[x][bit], cost);
}
}
}
ll ans = INF;
REP(i, k) chmin(ans, dp[i][(1 << k) - 1]);
if(ans == INF) ans = -2;
cout << ans + 1 << endl;
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for(int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for(int i = int(ini); i >= int(lim); i--)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
void solve() {
int x;
cin >> x;
int k = floor(x * 1.08);
if (k < 206) cout << "Yay!";
else if (k == 206) cout << "so-so";
else cout << ":(";
cout << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| #include<iostream>
#include<stdio.h>
#include<vector>
#include<string.h>
#include<stdint.h>
#include<queue>
using namespace std;
void _setup()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
}
int main() {
_setup();
int a;
cin >> a;
int res = a * 108 / 100;
if(res < 206) cout << "Yay!\n";
else if(res == 206) cout << "so-so\n";
else cout << ":(\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD9 998244353
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
template<class T> bool chmax(T &x, const T &y){if (x < y) {x = y; return true;} return false;}
template<class T> bool chmin(T &x, const T &y){if (x > y) {x = y; return true;} return false;}
template<class T> bool chgeq(T &x, const T &y){if (x <= y) {x = y; return true;} return false;}
template<class T> bool chleq(T &x, const T &y){if (x >= y) {x = y; return true;} return false;}
template<class T> void print(T x, string end = "\n"){cout << x << end;}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int H, W;
cin >> H >> W;
vector<vector<int>> A(H, vector<int>(W));
int minn = 1000;
int ans = 0;
for (int i = 0; i < H; i++){
for (int j = 0; j < W; j++){
cin >> A[i][j];
minn = min(minn, A[i][j]);
ans += A[i][j];
}
}
print(ans - minn * H * W);
} | #include <bits/stdc++.h>
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<x<<endl
using namespace std;
using ll = long long;
const ll MOD = 1e9+7; // 998244353
const ll LINF = LLONG_MAX; // ~ 10^18 ~ 1<<60
const int INF = INT_MAX;
const double PI = acos(-1);
int main(){
ll n, m; cin >> n >> m;
vector<ll> h(n);
vector<ll> w(m);
rep(i,0,n){
cin >> h[i];
}
rep(i,0,m){
cin >> w[i];
}
sort(all(h));
vector<ll> L(n, 0);
vector<ll> R(n, 0);
for(int i = 0; i <= n - 3; i += 2){
ll x = abs(h[i] - h[i + 1]);
L[i] = x;
L[i + 1] = x;
if(i != 0){
L[i] += L[i - 1];
L[i + 1] += L[i - 1];
}
}
for(int i = n - 1; i >= 1; i -= 2){
ll x = abs(h[i] - h[i - 1]);
R[i] = x;
R[i - 1] = x;
if(i != n - 1){
R[i] += R[i + 1];
R[i - 1] += R[i + 1];
}
}
ll ans = LINF;
for(int i = 0; i < m; i++){
ll it = lower_bound(all(h), w[i]) - h.begin();
ll temp = 0;
if(it % 2 == 1){
temp = abs(w[i] - h[it - 1]);
if(it - 2 >= 0) temp += L[it - 2];
if(it + 1 < n) temp += R[it + 1];
}else{
temp = abs(w[i] - h[it]);
if(it - 1 >= 0) temp += L[it - 1];
if(it + 1 < n) temp += R[it + 1];
}
ans = min(ans, temp);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << fixed << setprecision(20);
int K;
cin >> K;
string S;
cin >> S;
string T;
cin >> T;
vector<int> cnt(10, K);
for (int i = 0; i < 4; i++){
cnt[S[i] - '0']--;
cnt[T[i] - '0']--;
}
long long ans1 = 0, ans2 = 0;
for (int i = 1; i < 10; i++){
for (int j = 1; j < 10; j++){
vector<int> cnt1(10, 0);
for (int k = 0; k < 4; k++){
cnt1[S[k] - '0']++;
}
cnt1[i]++;
int s1 = 0;
for (int k = 1; k <= 9; k++){
int add = k;
for (int l = 0; l < cnt1[k]; l++){
add *= 10;
}
s1 += add;
}
vector<int> cnt2(10, 0);
for (int k = 0; k < 4; k++){
cnt2[T[k] - '0']++;
}
cnt2[j]++;
int s2 = 0;
for (int k = 1; k <= 9; k++){
int add = k;
for (int l = 0; l < cnt2[k]; l++){
add *= 10;
}
s2 += add;
}
long long a;
if (i == j){
a = (long long) cnt[i] * (cnt[i] - 1);
} else {
a = (long long) cnt[i] * cnt[j];
}
if (s1 > s2){
ans1 += a;
}
ans2 += a;
}
}
cout << (double) ans1 / ans2 << endl;
} | #include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#define ll long long int
#define REP(i, n) for(ll i = 0; i < n; i++)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOR(i, m, n) for(ll i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
#define PI 3.141592653589793
using namespace std;
int main() {
int N,K,M,sum=0;
cin >> N >> K >> M;
int A;
REP(i,N-1) {
cin >> A;
sum += A;
}
int ans = N * M - sum;
if(ans > K) ans = -1;
else if(ans < 0) ans = 0;
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
unsigned long long n;
cin >> n;
unsigned long long a = 5;
for (int i = 0; i < 26; i++) {
unsigned long long b = 3;
for (int j = 0; j < 38; j++) {
if (n == a + b){
cout << j+1 << " " << i+1 << endl;
return 0;
}
b *= 3;
}
a *= 5;
}
cout << -1 << endl;
return 0;
}
| #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
//#define ACL
#ifdef ACL
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#if __has_include("acl-all.h")
#include "acl-all.h" //on Wandbox
using namespace atcoder;
#endif
#endif //#ifdef ACL
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<ll, ll> > vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
typedef vector<vector<bool> > vvb;
typedef pair<ll, ll> pll;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = 1; i <= int(n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = int(n)-1; i >= 0; --i)
#define MES(a) MES2 a
#define MES2(a0,a1,a2,a3,a4,x,...) x
#define mes_1(x1) cout<<x1<<endl
#define mes_2(x1,x2) cout<<x1<<" "<<x2<<endl
#define mes_3(x1,x2,x3) cout<<x1<<" "<<x2<<" "<<x3<<endl
#define mes_4(x1,x2,x3,x4) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<endl
#define mes_5(x1,x2,x3,x4,x5) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<" "<<x5<<endl
#define mes(...) CHOOSE((__VA_ARGS__,mes_5,mes_4,mes_3,mes_2,mes_1,~))(__VA_ARGS__)
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#define Ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define Rev(a) reverse(a.begin(), a.end())
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } };
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
ll fact_mod(ll n, ll mod) {
ll f = 1; for (ll i = 2; i <= n; i++) f = f * (i % mod) % mod;
return f;
}
ll modpow(ll x, ll n, ll mod) {
if(n == 0) return 1;
ll res = modpow((x * x) % mod, n / 2 , mod);
if(n & 1) res = (res * x) % mod;
return res;
}
ll modncr(ll n, ll r, ll mod) {
if(r > n-r) r = n-r;
if(r == 0) return 1;
ll a = 1;
rep(i, r) a = a * ((n-i) % mod) % mod;
ll b = modpow(fact_mod(r, mod), mod-2, mod);
return (a % mod) * (b % mod) % mod;
}
ll r = 0;
string s;
void dfs(ll i, ll cnt, ll div) {
if (cnt > 4) return;
if (i == 10) {
if (cnt != 4) return;
r += 24 / div;
return;
}
if (s[i] == 'x') {
dfs(i+1, cnt, div);
} else {
if (s[i] == '?') dfs(i+1, cnt, div);
dfs(i+1, cnt+1, div);
dfs(i+1, cnt+2, div*2);
dfs(i+1, cnt+3, div*6);
dfs(i+1, cnt+4, div*24);
}
}
signed main() {
cin >> s;
dfs(0, 0, 1);
mes(r);
}
|
#include <bits/stdc++.h>
using namespace std;
int main (void){
vector<int> hoge(3);
cin >> hoge[0] >> hoge[1] >> hoge[2];
sort(hoge.begin(), hoge.end());
cout << hoge[1] + hoge[2] << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define gc() (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin), p1 == p2 ? EOF : *p1++) : *p1++)
#define read() ({ register int x = 0, f = 1; register char c = gc(); while(c < '0' || c > '9') { if (c == '-') f = -1; c = gc();} while(c >= '0' && c <= '9') x = x * 10 + (c & 15), c = gc(); f * x; })
char buf[1 << 20], *p1, *p2;
int main(){
int n = read(),a = read(),b = read();
cout<<n-a+b<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vi nums(n);
for_(i, 0, n) cin >> nums[i];
vi init = nums;
vi ans;
for_(m, 0, n * n) {
bool srt = true;
for_(i, 0, n-1) if (nums[i] > nums[i+1]) {
srt = false;
break;
}
if (srt) break;
for (int i = m % 2; i < n - 1; i += 2) {
if (i + 2 >= n - 1 or nums[i] > nums[i + 1]) {
swap(nums[i], nums[i + 1]);
ans.push_back(i + 1);
break;
}
}
}
vi srt = nums;
sort(srt.begin(), srt.end());
if (srt != nums) {
for (auto i: init) cout << i << " ";
cout << endl;
cout << "final: ";
for (auto i: nums) cout << i << " ";
cout << endl;
assert(false);
}
cout << ans.size() << endl;
for (auto i: ans) cout << i << " ";
}
}
| #include <bits/stdc++.h>
using namespace std;
template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); }
template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); }
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
string base = "atcoder";
while(t--){
string s;
cin >> s;
int at = 0, n = s.size();
while(at < base.size() && at < n && base[at] == s[at]) at++;
if((at < base.size() && at < n && base[at] < s[at]) || (at == base.size() && at < n)) cout << 0 << '\n';
else if(at == 1){
int ans = 1<<30;
while(at < n && s[at] == 'a') at++;
if(at == n) ans = -1;
else {
if(s[at] > 't') ans = at - 1;
else ans = at;
}
cout << ans << '\n';
} else {
assert(at > 1);
cout << 1 << '\n';
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#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 io ios::sync_with_stdio(false); cin.tie(0)
int a[3003][5];
int main()
{
io;
int n; cin >> n;
rep(i, n) rep(j, 5) cin >> a[i][j];
int ok = 0, ng = 1e9+1;
auto check = [&](int x) {
set<int> s;
rep(i, n) {
int bit = 0;
rep(j, 5) {
bit <<= 1;
bit |= (a[i][j] >= x);
}
s.insert(bit);
}
for (int x : s) for (int y : s) for (int z : s) {
if ((x | y | z) == 31) return true;
}
return false;
};
while (abs(ok-ng) > 1) {
int mid = (ok + ng) / 2;
(check(mid) ? ok : ng) = mid;
}
cout << ok << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define N 10005
bitset<N>p[5],f[3];
int n,i,l,r,cnt,mid,a[N][10],c[10],fl,b[5][5],e[10];
bool check(){
int i;
for(i=1;i<=n;++i)f[0][i]=f[1][i]=f[2][i]=1;
for(i=0;i<5;++i){
f[e[i]]&=p[i];
}
if(f[0].count()&&f[1].count()&&f[2].count())return 1;
return 0;
}
void dfs(int now){
if(now==5){fl|=check();return ;}
e[now]=0;dfs(now+1);e[now]=1;dfs(now+1);e[now]=2;dfs(now+1);
}
int check(int x){
for(int i=0;i<5;++i){
for(int j=1;j<=n;++j){
if(a[j][i]>=x)p[i][j]=1;else p[i][j]=0;
}
}
fl=0;dfs(1);if(fl)return 1;return 0;
}
int main(){
cin>>n;
for(i=1;i<=n;++i)cin>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3]>>a[i][4];
l=0;r=1e9;
while(l<r){
mid=r-(r-l>>1);
if(check(mid))l=mid;
else r=mid-1;
} cout<<l;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14
template <typename T>
bool chmax(T &a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
mint mpow(mint x, ll y){
if(y == 0){
return 1;
}
if(y % 2 == 0){
mint a = mpow(x, y / 2);
return a * a;
}
else{
mint a = mpow(x, y / 2);
return a * a * x;
}
}
int main()
{
ll H, W;
cin >> H >> W;
vector<string> S(H);
rep(i, H){
cin >> S.at(i);
}
mint ans = 0;
vector<vector<ll>> clut_r(H, vector<ll>(1, -1));
rep(i, H){
rep(j, W){
if(S.at(i).at(j) == '#'){
clut_r.at(i).push_back(j);
}
}
clut_r.at(i).push_back(W);
}
vector<vector<ll>> clut_c(W, vector<ll>(1, -1));
rep(i, W){
rep(j, H){
if(S.at(j).at(i) == '#'){
clut_c.at(i).push_back(j);
}
}
clut_c.at(i).push_back(H);
}
vector<vector<ll>> space(H, vector<ll>(W, 0)); //各マスを照らす照明を置けるマスの数
rep(i, H){
ll p = 0;
rep(j, W){
ll left = clut_r.at(i).at(p);
ll right = clut_r.at(i).at(p+1);
if(S.at(i).at(j) == '#'){
p++;
continue;
}
space.at(i).at(j) += right-left-1;
}
}
rep(i, W){
ll p = 0;
rep(j, H){
ll left = clut_c.at(i).at(p);
ll right = clut_c.at(i).at(p+1);
if(S.at(j).at(i) == '#'){
p++;
continue;
}
space.at(j).at(i) += right-left-2;
}
}
ll K = 0;
rep(i, H){
rep(j, W){
if(S.at(i).at(j) == '.'){
K++;
}
}
}
rep(i, H){
rep(j, W){
if(S.at(i).at(j) == '.'){
ans += mpow(2, K) - mpow(2, K-space.at(i).at(j)); //全体 - (i,j)を照らさない置き方
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e3+5,MOD=1e9+7;
int n,m,tot,ans,k;
int u[N][N],d[N][N],l[N][N],r[N][N];
char str[N][N];
inline int pow(int a,int n)
{
int res=1;
while (n)
{
if (n&1) res=res*a%MOD;
a=a*a%MOD;
n>>=1;
}
return res;
}
signed main(){
scanf("%lld%lld",&n,&m);
for (register int i=1; i<=n; ++i) scanf("%s",str[i]+1);
for (register int i=1; i<=n; ++i)
for (register int j=1; j<=m; ++j)
if (str[i][j]=='.') u[i][j]=u[i-1][j]+1,l[i][j]=l[i][j-1]+1;
else u[i][j]=l[i][j]=0;
for (register int i=n; i>=1; --i)
for (register int j=m; j>=1; --j)
if (str[i][j]=='.') d[i][j]=d[i+1][j]+1,r[i][j]=r[i][j+1]+1;
for (register int i=1; i<=n; ++i)
for (register int j=1; j<=m; ++j) if (str[i][j]=='.') k++;
ans=k*pow(2ll,k)%MOD;
for (register int i=1; i<=n; ++i)
for (register int j=1; j<=m; ++j)
if (str[i][j]=='.')
{
tot=l[i][j]+r[i][j]+u[i][j]+d[i][j]-4+1;
ans=((ans-pow(2ll,k-tot))%MOD+MOD)%MOD;
}
printf("%lld\n",ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const int N=3e5+5;
const int M=1e5+5;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-8;
const long double pi=acos(-1.0L);
#define ls (i<<1)
#define rs (i<<1|1)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mem(a,b) memset(a,b,sizeof(a))
LL read()
{
LL x=0,t=1;
char ch;
while((ch=getchar())<'0'||ch>'9') if(ch=='-') t=-1;
while(ch>='0'&&ch<='9'){ x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); }
return x*t;
}
int a[N],ans[N];
int main()
{
int T=read();
while(T--)
{
//并不需要考虑去将每一个数字归位,而是可以直接考虑减少逆序对(交换相邻的逆序数字)
int n=read(),tot=0;
for(int i=1;i<=n;i++) a[i]=read();
for(int k=0;;k^=1){
int flag=0;
for(int i=1;i<n;i++)
if(a[i]>a[i+1]) flag=1;
if(!flag) break;
int i;
for(i=k+1;i<n&&a[i]<a[i+1];i+=2);
if(i>=n) i-=2;
ans[++tot]=i;
swap(a[i],a[i+1]);
}
printf("%d\n",tot);
for(int i=1;i<=tot;i++) printf("%d%c",ans[i],i==tot?'\n':' ');
}
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>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
int mod = 1000000007;
int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int x, T... y) { return add(x, add(y...)); }
int mul(int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }
int sub(int x, int y) { return add(x, mod - y); }
int modpow(int a, long long b) {
int ret = 1; while (b > 0) {
if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1;
} return ret;
}
int modinv(int a) { return modpow(a, mod - 2); }
typedef vector<int> Vec;
typedef vector<Vec> Mat;
Vec mulMatVec(Mat a, Vec b)
{
int n = b.size(); Vec ret(n, 0);
rep(i, 0, n) rep(j, 0, n) ret[i] = add(ret[i], mul(a[i][j], b[j]));
return ret;
}
Mat mulMatMat(Mat a, Mat b)
{
int n = a.size(); Mat ret(n, Vec(n, 0));
rep(i, 0, n) rep(j, 0, n) rep(k, 0, n) ret[i][j] = add(ret[i][j], mul(a[i][k], b[k][j]));
return ret;
}
Mat fastpow(Mat x, ll n)
{
Mat ret(x.size(), Vec(x.size(), 0));
rep(i, 0, x.size()) ret[i][i] = 1;
while (0 < n) { if ((n % 2) == 0) { x = mulMatMat(x, x); n >>= 1; } else { ret = mulMatMat(ret, x); --n; } }
return ret;
}
void printVec(Vec a) { cout << "[\t"; rep(i, 0, a.size()) cout << a[i] << "\t"; cout << "]" << endl; }
void printMat(Mat a) { rep(i, 0, a.size()) printVec(a[i]); }
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int N, M, K, A[101];
int X[10101], Y[10101];
int cnt[101];
bool E[101][101];
//---------------------------------------------------------------------------------------------------
void _main() {
cin >> N >> M >> K;
rep(i, 0, N) cin >> A[i];
rep(i, 0, M) cin >> X[i] >> Y[i], X[i]--, Y[i]--;
Mat Ma(N, Vec(N));
rep(i, 0, M) {
cnt[X[i]]--;
cnt[Y[i]]--;
E[X[i]][Y[i]] = E[Y[i]][X[i]] = true;
}
rep(to, 0, N) {
int cnt = M;
rep(from, 0, N) if (to != from) {
if (E[to][from]) {
Ma[to][to] = add(Ma[to][to], mul(modinv(M), modinv(2)));
Ma[to][from] = add(Ma[to][from], mul(modinv(M), modinv(2)));
cnt--;
}
}
Ma[to][to] = add(Ma[to][to], mul(cnt, modinv(M)));
}
Vec v(N, 0);
rep(i, 0, N) v[i] = A[i];
Ma = fastpow(Ma, K);
v = mulMatVec(Ma, v);
rep(i, 0, N) cout << v[i] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define REP2(i, a, n) for (int i = a; i < n; i++)
#define REPR(i, n) for (int i = n - 1; 0 <= i; i--)
#define REPR2(i, n, a) for (int i = n; a <= i; i--)
using V = vector<int>;
using P = pair<int, int>;
using S = string;
using ll = long long;
using namespace std;
int ctoi(const char c)
{
switch (c)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return -1;
}
}
int powi(int a, int n)
{
int res = 1;
REP(i, n)
{
res *= a;
}
return res;
}
int main()
{
S s, t;
int k;
cin >> k >> s >> t;
V s2(9, 0), t2(9, 0);
vector<ll> card(9, k);
REP(i, 4)
{
int tmp = ctoi(s[i]);
card[tmp - 1]--;
s2[tmp - 1]++;
tmp = ctoi(t[i]);
card[tmp - 1]--;
t2[tmp - 1]++;
}
int valueS = 0, valueT = 0;
REP(i, 9)
{
valueS += (i + 1) * powi(10, s2[i]);
valueT += (i + 1) * powi(10, t2[i]);
}
ll ans = 0, sum = 0;
REP(i, 9)
{
int tmpS, tmpT;
REP(j, 9)
{
tmpS = valueS;
tmpT = valueT;
if (i != j && 0 <= card[i] - 1 && 0 <= card[j] - 1)
{
tmpS += (i + 1) * pow(10, s2[i] + 1) - (i + 1) * pow(10, s2[i]);
tmpT += (j + 1) * pow(10, t2[j] + 1) - (j + 1) * pow(10, t2[j]);
sum += card[i] * card[j];
if (tmpS > tmpT)
{
ans += card[i] * card[j];
}
}
if (i == j && 0 <= card[i] - 2)
{
tmpS += (i + 1) * pow(10, s2[i] + 1) - (i + 1) * pow(10, s2[i]);
tmpT += (j + 1) * pow(10, t2[j] + 1) - (j + 1) * pow(10, t2[j]);
sum += card[i] * (card[i] - 1);
if (tmpS > tmpT)
{
ans += card[i] * (card[i] - 1);
}
}
}
}
cout << (double)ans / sum << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define pll pair<long long,long long>
#define vl vector<long long>
#define vll vector<pll>
#define vi vector<int>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define tcase int tt; cin>>tt; while(tt--)
#define tcase2 int tt; cin>>tt; for(int qq=1;qq<=tt;qq++)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
const int MAXN = 1000002;
const int MOD2 = 998244353;
const int MOD = 1000000007;
const int INF = 1e8;
const ld EPS = 1e-7;
// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define trav(a, x) for(auto& a : x)
#define fill(x,y) memset(x,y,sizeof(x))
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
ll mul(ll x, ll y)
{
return (x * 1ll * y) % MOD;
}
ll fastpow(ll x, ll y)
{
ll z = 1;
while(y)
{
if(y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
ll modinv(ll n,ll p)
{
return fastpow(n,p-2) ;
}
struct Comp {
bool operator()(const std::pair<int, int> &a, const std::pair<int, int> &b) {
if (a.first != b.first) {
return a.first > b.first;
}
return a.second >b.second;
}
};
int main(int argc, char** argv)
{ sws ;
cout<<setprecision(10);
//tcase
{
ll n;
cin>>n;
//cout<<1<<"\n";
vector<ll>v ;
for(int i=1;i<=sqrt(n);i++)
{
if(n%i==0)
{
if(n/i==i)
v.pb(i);
else
{
//cout<<i<<"\n"<<n/i<<"\n";
v.pb(i);
v.pb(n/i);
}
}
}
sort(v.begin(),v.end());
for(auto x:v)
cout<<x<<"\n" ;
}
return 0 ;
} | //BY: YASH JAIN, CF: yashjain0333
#include <bits/stdc++.h>
using namespace std;
#define May_the_fork_be_with_you ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define deb(x) cout<<#x<<": "<<x<<endl;
#define MOD 1000000007
#define INF 1e18
#define ll long long
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define endl "\n"
#define int long long
#define N 100005
#define mod 1e8
set<int> s;
void printDivisors(int n) {
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
s.insert(i);
else {
s.insert(i);
s.insert(n / i);
}
}
}
}
void solve() {
int n; cin >> n;
printDivisors(n);
for (auto x : s) cout << x << endl;
}
int32_t main() {
#ifndef ONLINE_JUDGE
// for geting input form input.txt
freopen("input.txt", "r", stdin);
// for wrting output to output.txt
freopen("output.txt", "w", stdout);
#endif
May_the_fork_be_with_you
int t = 1;
// cin >> t;
cout << setprecision(12);
while (t--) {
solve();
}
cerr << "Time Taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
} |
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD (1LL<<63)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define ld long double
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define SUBS(s,f,t) ((s).substr((f),(t)-(f)))
#define ALL(a) (a).begin(),(a).end()
#define Max(a) (*max_element(ALL(a)))
#define Min(a) (*min_element(ALL(a)))
using namespace std;
vvi vis(2222,vi(2222,llINF));
ll vx[4] = {1,0,-1,0};
ll vy[4] = {0,1,0,-1};
ll h,w;
vi ch(33);
bool check(ll x,ll y){
return x>=0 && x<h && y>=0 && y<w;
}
int main(){
cin>>h>>w;
string s[h];
vector<vector<pair<ll,ll>>>E(33);
queue<pair<ll,ll>>que;
for(int i=0;i<h;i++){
cin>>s[i];
for(int j=0;j<w;j++){
if(s[i][j] == 'S'){
que.push(mp(i,j));
vis[i][j] = 0;
}
if('a' <= s[i][j] && s[i][j] <= 'z'){
E[s[i][j]-'a'].pb(mp(i,j));
}
}
}
while(!que.empty()){
ll x,y;
tie(x,y) = que.front();
que.pop();
//cout<<x<<" "<<y<<" "<<vis[x][y]<<endl;
if(s[x][y] == 'G'){
cout<<vis[x][y]<<endl;
return 0;
}
if('a' <= s[x][y] && s[x][y] <= 'z'){
if(ch[s[x][y]-'a']){}
else{
ch[s[x][y]-'a'] = 1;
for(auto e:E[s[x][y]-'a']){
//cout<<e.F<<" "<<e.S<<endl;
if(vis[e.F][e.S] > vis[x][y]+1){
vis[e.F][e.S] = vis[x][y] + 1;
que.push(mp(e.F,e.S));
}
}
}
}
for(int i=0;i<4;i++){
ll nx = x+vx[i],ny = y+vy[i];
if(check(nx,ny) && s[nx][ny] != '#'&& vis[nx][ny] > vis[x][y]+1){
//cout<<nx<<" "<<ny<<endl;
vis[nx][ny] = vis[x][y]+1;
que.push(mp(nx,ny));
}
}
}
cout<<-1<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define spu ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
// ~ ~ ~ ~ minmedian ~ ~ ~ ~
// ~ ~ ~ ~ median ~ ~ ~ ~
//dp+二分
//dp策略:遍历每一个格子,以当前格子为右下角的方阵有多少个格子小于等于给定值
//二分策略: 对于mid 如果存在一种sqare cnt>= (k*k+1)/2 在左面找,else 在右面找
const int N=810;
int k,n;
int g[N][N];
int dp[N][N];
bool check(int mid)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[i][j]=g[i][j]<=mid;
dp[i][j]+=dp[i-1][j];
dp[i][j]+=dp[i][j-1];
dp[i][j]-=dp[i-1][j-1];
}
}
for(int i=k;i<=n;i++)
{
for(int j=k;j<=n;j++)
{
int cnt=dp[i][j];
cnt-=dp[i][j-k];
cnt-=dp[i-k][j];
cnt+=dp[i-k][j-k];
if(cnt>=(k*k+1)/2) return true;
}
}
return false;
}
int main()
{
spu;
cin>>n>>k;
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin>>g[i][j];
int l=-1,r=1e9;
while(r-l>1)
{
int mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid;
}
cout<<r<<endl;
} |
#include<bits/stdc++.h>
#define reg register
typedef long long ll;
using namespace std;
inline int qr(){
int x=0,f=0;char ch=0;
while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return f?-x:x;
}
const int maxn=2e5+100;
const int mod=998244353;
int n;
struct node{
int next,to;
}edge[maxn<<1];
int head[maxn],_tot;
void add(int u,int v){
edge[_tot].next=head[u],edge[_tot].to=v;head[u]=_tot++;
}
int qkpow(int a,int b){
ll res=1;
while(b){
if(b&1) res=(res*a)%mod;
a=((ll)a*a)%mod;b>>=1;
}
return res;
}
int dfn[maxn],low[maxn],dfncnt;
int cnt;
int st[maxn],top;
bool vis[maxn];
void tarjan(int x){
low[x]=dfn[x]=++dfncnt;
st[++top]=x;vis[x]=1;
for(reg int i=head[x];~i;i=edge[i].next){
int y=edge[i].to;
if(!dfn[y]){
tarjan(y);
low[x]=min(low[x],low[y]);
}else if(vis[y]){
low[x]=min(low[x],dfn[y]);
}
}
if(dfn[x]==low[x]){
cnt++;int z;
do{
z=st[top--];vis[z]=0;
}while(z!=x);
}
}
int main(){
n=qr();
memset(head,-1,sizeof head);
for(reg int i=1;i<=n;i++){
int x=qr();
add(i,x);add(x,i);
}
for(reg int i=1;i<=n;i++){
if(!dfn[i]) top=0,tarjan(i);
}
printf("%d\n",qkpow(2,cnt)-1);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define _ <<" "<<
#define sz(x) ((int) (x).size())
typedef pair<int, int> pii;
typedef long long ll;
const int maxn = 5e5 + 5;
template<class T> void print(T & x){ cout << x; }
template<class T,class U> void print(pair<T,U> & p){cout << "("; print(p.first); cout << ", "; print(p.second); cout << ")"; }
template<class T> void print(vector<T> & v) {
cout << "{";
if (sz(v)) print(v[0]);
for (int i = 1; i < sz(v); i++) cout << ", ", print(v[i]);
cout << "}\n";
}
ll n, m;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
ll ans = 0;
for (int i = 1;i <= 1e6; ++i) {
int len = 0;
ll x = i;
ll t = 1;
while (x) {
x /= 10;
len++;
t *= 10;
}
if (t * i + i <= n) ans++;
}
cout << ans << endl;
return 0;
} |
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define isNum(a) (a >= '0' && a <= '9')
#define SP putchar(' ')
#define EL putchar('\n')
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
template <class T>
void read(T &);
template <class T>
void write(const T &);
typedef long long ll;
typedef unsigned long long ull;
typedef const long long &cll;
typedef const int &ci;
typedef std::pair<int, int> pii;
const int iinf = 2147483647;
const ll llinf = 9223372036854775807ll;
using std::abs;
using std::max;
using std::min;
constexpr int N = 3005;
constexpr int Mod = 998244353;
void add(int &a, int b) { (a += b) >= Mod ? a -= Mod : a; }
int dp[N][N];
int n, k;
int main() {
read(n), read(k);
dp[0][0] = 1;
for (int j = 1; j <= n; ++j) {
for (int i = 1; i <= j; ++i) {
dp[i][j] = dp[i - 1][j - 1];
if (2 * i <= j - i) {
add(dp[i][j], dp[2 * i][j - i]);
}
if (2 * i - 1 <= j - i) {
add(dp[i][j], dp[2 * i - 1][j - i]);
}
}
}
int ans = 0;
for (int i = 0; i <= k; ++i) {
add(ans, dp[i][n - k]);
}
write(ans), EL;
return 0;
}
template <class T>
inline void read(T &Re) {
T k = 0;
char ch = getchar();
int flag = 1;
while (!isNum(ch)) {
if (ch == '-') {
flag = -1;
}
ch = getchar();
}
while (isNum(ch)) {
k = (k << 1) + (k << 3) + ch - '0';
ch = getchar();
}
Re = flag * k;
}
template <class T>
inline void write(const T &Wr) {
if (Wr < 0) {
putchar('-');
write(-Wr);
} else {
if (Wr < 10) {
putchar(Wr + '0');
} else {
write(Wr / 10);
putchar((Wr % 10) + '0');
}
}
}
| #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);
int n, k;
cin >> n >> k;
const int mod = 998244353;
vector<int> dp(1, 1);
for (int i = 1; i < n; i++) {
vector<int> temp(i + 1, 0);
for (int j = i; j >= 0; j--) {
if (j > 0) {
temp[j] = dp[j - 1];
}
if (2 * j < i) {
temp[j] += temp[2 * j + 1];
}
temp[j] = temp[j] % mod;
//cout << i << j << temp[j] << endl;
}
dp = temp;
}
cout << dp[k - 1] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define MOD 1000000007
#define INF LLONG_MAX
#define MINF LLONG_MIN
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL); cout.tie(NULL);
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define mii map<int, int>
#define si set<int>
int pow(int x, int y, int mod) {
int temp;
if (y == 0)
return 1;
temp = (pow(x, y / 2, mod)) % mod;
if (y % 2 == 0)
return (temp * temp) % mod;
else
return (((x * temp) % mod) * temp) % mod;
}
void solve(int tno) {
int n; cin >> n;
n = (int)(1.08 * n);
if (n < 206)
cout << "Yay!" << endl;
else if(n == 206)
cout << "so-so" << endl;
else
cout << ":(" << endl;
}
int32_t main() {
FASTIO
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
// cin >> t;
t = 1;
for (int i = 1; i <= t; i ++)
solve(i);
} | #include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ff first
#define ss second
#define fo(i,n) for(int i=0;i<n;i++)
#define fod(i,n) for(int i=n-1;i>=0;i--)
#define For(i,a,b) for(int i=a;i<=b;i++)
#define all(x) x.begin(),x.end()
#define SET(x,v) memset(x,v,sizeof(x))
#define si set<int>
#define pb(i) push_back(i)
#define sll set<long long>
#define vs vector<string>
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vpii vector<pair<int,int>>
#define ll long long int
#define vi vector<ll>
#define mod 1000000007
#define tc(t) int t; cin>>t; while(t--)
#define sfm(a,m,n) ll a[m][n]; fo(i,m) fo(j,n) cin>>a[i][j]
#define sfarr(a,n) ll a[n]; fo(i,n) cin>>a[i]
#define sfv(v,n) vi v(n); for(ll &i:v) cin>>i
// int f[1000001];
// int prime[1000001];
// void sieve(){
// prime[0]=prime[1]=0;
// For(i,2,1000000) prime[i]=1;
// for(int i=2;i*i<=1000000;i++){
// if(prime[i]){
// for(ll j=i*i;j<=1000000;j+=i)
// prime[j]=0;
// }
// }
// }
// int power(int a,int b){
// int r=1;
// while(b){
// if(b&1){
// r=(r*1ll*a)%mod;
// b--;
// }
// else{
// a=(a*1ll*a)%mod;
// b>>=1;
// }
// }
// return r;
// }
// void factorial(){
// f[0]=f[1]=1;
// For(i,2,1000000) f[i]=(f[i-1]*1ll*i)%mod;
// }
// int nCr(int a,int b){
// if(b>a) return 0;
// factorial();
// int r=f[a];
// r=(r*1ll*power(f[b],mod-2))%mod;
// r=(r*1ll*power(f[a-b],mod-2))%mod;
// return r;
// }
// vi z_algo(string s){
// ll n=s.length(),l=0,r=0;
// vi z(n);
// For(i,1,n-1){
// if(i<=r) z[i]=min(r-i+1,z[i-l]);
// while(z[i]+i<n && s[z[i]]==s[i+z[i]]) z[i]++;
// if(i+z[i]-1>r) l=i,r=i+z[i]-1;
// }
// return z;
// }
// int parent[1000001];
// int ranker[1000001];
// int findpar(int node){
// if(node==parent[node])
// return node;
// return parent[node]=findpar(parent[node]);
// }
// void connect(int u,int v){
// u=findpar(u);
// v=findpar(v);
// if(u==v) return;
// if(ranker[u]<ranker[v])
// parent[u]=v;
// else if(ranker[u]>ranker[v])
// parent[v]=u;
// else{
// parent[v]=u;
// ranker[u]++;
// }
// }
// vector<bool> vis(1000001,0);
// vector<int> g[1000001];
// void graph(vector<int> g[],int e,bool t){
// fo(i,e){
// int u,v; cin>>u>>v;
// g[u].pb(v);
// if(!t)
// g[v].pb(u);
// }
// }
// void dfs(int node){
// vis[node]=1;
// for(int child:g[node]){
// if(!vis[child])
// dfs(child);
// }
// }
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin>>n;
double x=1.08*n;
int y=x;
if(y<206) cout<<"Yay!";
else if(y>206) cout<<":(";
else cout<<"so-so";
return 0;
}
|
//@formatter:off
#include<bits/stdc++.h>
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(i,n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i,s,n) for (ll i = ll(s); i < ll(n); ++i)
#define rep3(i,s,n,d) for(ll i = ll(s); i < ll(n); i+=d)
#define rep(...) overload4(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep(i,n) for (ll i = ll(n)-1; i >= 0; i--)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define popcount(x) __builtin_popcount(x)
#define pb push_back
#define eb emplace_back
#ifdef __LOCAL
#define debug(...) { cout << #__VA_ARGS__; cout << ": "; print(__VA_ARGS__); cout << flush; }
#else
#define debug(...) void(0)
#endif
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int,int>;
using LP = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vs = vector<string>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vp = vector<P>;
using vvp = vector<vector<P>>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
void scan(){}
template <class Head, class... Tail> void scan(Head& head, Tail&... tail){ cin >> head; scan(tail...); }
template<class T> void print(const T& t){ cout << t << '\n'; }
template <class Head, class... Tail> void print(const Head& head, const Tail&... tail){ cout<<head<<' '; print(tail...); }
template<class... T> void fin(const T&... a) { print(a...); exit(0); }
const string yes[] = {"no","yes"};
const string Yes[] = {"No","Yes"};
const string YES[] = {"NO","YES"};
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
INT(n, s, d);
rep(i, n) {
INT(x, y);
if (x >= s) continue;
if (y <= d) continue;
fin(Yes[1]);
}
fin(Yes[0]);
}
| #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long add(long long a, long long b) {
long long res = a + b;
if (res >= mod) res -= mod;
return res;
}
long long sub(long long a, long long b) {
long long res = a - b + mod;
if (res >= mod) res -= mod;
return res;
}
long long mul(long long a, long long b) {
return (((a % mod) * (b % mod)) % mod);
}
long long potolok(long long a, long long b){
return (a + b - 1) / b;
}
long long gcd(long long x, long long y){
if(y == 0){
return x;
}
if(x == 0){
return y;
}
if(x > y){
return gcd(y, x % y);
}
else{
return gcd(x, y % x);
}
}
long long bin_pow(long long base, long long power, long long m){
if(power == 1){
return base;
}
if(power % 2 == 0){
long long t = bin_pow(base, power / 2, m);
return t * t % m;
}
else{
return bin_pow(base, power - 1, m) * base % m;
}
}
struct segtree{
struct node{
long long seg, pref, suf, sum;
};
node combine(node a, node b){
return {
max(a.seg, max(b.seg, a.suf + b.pref)),
max(a.pref, a.sum + b.pref),
max(b.suf, b.sum + a.suf),
a.sum + b.sum
};
}
node one_element(long long a){
return {
max(a, 0 * 1LL),
max(a, 0 * 1LL),
max(a, 0 * 1LL),
a
};
}
node ZERO = {0, 0, 0, 0};
int razmer;
vector<node> tree;
void Init(vector<long long> &v){
int n = (int)v.size();
razmer = 1;
while(razmer < n) razmer *= 2;
tree.assign(2 * razmer - 1, {0, 0, 0, 0});
}
void build(vector<long long> &v, int x, int lx, int rx){
if(rx - lx == 1){
if(lx < (int)v.size()){
tree[x] = one_element(v[lx]);
}
}else{
int m = (lx + rx) / 2;
build(v, 2 * x + 1, lx, m);
build(v, 2 * x + 2, m, rx);
tree[x] = combine(tree[2 * x + 1], tree[2 * x + 2]);
}
}
void build(vector<long long> &v){
build(v, 0, 0, razmer);
}
void cet(int i, int v, int x, int lx, int rx){
if(rx - lx == 1){
tree[x] = one_element(v * 1LL);
return;
}else{
int m = (lx + rx) / 2;
if(i < m){
cet(i, v, 2 * x + 1, lx, m);
}else{
cet(i, v, 2 * x + 2, m, rx);
}
tree[x] = combine(tree[2 * x + 1], tree[2 * x + 2]);
}
}
void cet(int i, int v){
cet(i, v, 0, 0, razmer);
}
void show(){
for(auto i : tree){
cout << i.seg << " " << i.pref << " " << i.suf << " " << i.sum << '\n';
}
}
};
void solve(){
//std::freopen("ancestor.in", "r", stdin);
//std::freopen("ancestor.out", "w", stdout);
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);
long long n, s, d;
cin >> n >> s >> d;
vector<pair<long long, long long>> v(n);
for(int i = 0;i < n;i++){
cin >> v[i].first >> v[i].second;
}
for(int i = 0;i < n;i++){
if(v[i].first < s && v[i].second > d){
cout << "Yes" << endl;
return;
}
}
cout << "No" << endl;
}
int main(){
int t;
t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
#include <unordered_set>
// https://atcoder.jp/contests/abc192/tasks/abc192_c
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
int main(){
string N;
ll K;
cin >> N >> K;
ll tmp = stoll(N);
int digit = N.size();
rep(i, K){
int num;
vi t(digit);
rep(j, digit){
num = tmp % 10;
t.at(j) = num;
tmp /= 10;
}
vi g1(t.size()), g2(t.size());
g1 = t;
sort(ALL(g1));
g2 = g1;
reverse(ALL(g2));
ll gn1 = 0, gn2 = 0;
int d = 1;
rep(j, g1.size()){
gn1 += d * g1.at(j);
gn2 += d * g2.at(j);
d *= 10;
}
tmp = gn1 - gn2;
string str = to_string(tmp);
digit = str.size();
}
cout << tmp << endl;
} | #include <bits/stdc++.h>
#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc int t; cin >> t; while(t--)
#define sz(x) (int)(x).size()
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;
template<typename T> istream& operator>>(istream &is, vector<T> &v) { for(auto &it : v) is >> it; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep = ""; for(const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void dbg_out() { cerr << " end.\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
const int N = 1e6+5;
const int mod = 1e9+7;
/**
Read problem statement carefully
5
2 6
0 0
1000000 1000000
12345 67890
0 1000000
**/
LL f(int a, int l){
return a - 2*l + 1;
}
void solve(){
int l, r;
cin >> l >> r;
LL res = 0;
// (2*l + r) * (r - 2*l + 1) / 2
/*for(int a = 2*l; a <= r; ++a){
res += a - 2*l + 1;
}*/
if(2*l > r){
cout << 0 << "\n";
return;
}
cout << (f(2*l, l) + f(r, l)) * (r-2*l+1) / 2 << "\n";
}
int main()
{
Fast;
//init();
louisfghbvc
solve();
return 0;
}
/**
enjoy the problem.
**/
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
typedef long long ll;
const ll mod = 1000 * 1000 * 1000 + 7;
vector<string> v;
vector<vector<int>> row, col;
ll power(ll x, ll n) {
ll res = 1;
for (; n; n >>= 1, x = (x * x) % mod)
if (n & 1) res = (res * x) % mod;
return res;
}
int main() {
int h, w; cin >> h >> w; v.resize(h);
row.resize(h, vector<int>(w));
col.resize(h, vector<int>(w));
for (auto &x : v) { cin >> x; }
int K = 0; for (int i = 0; i < h; i++) {
K += count(v[i].begin(), v[i].end(), '.');
int j = 0, k = 0; while (k != w) {
if (v[i][j] == '#') { j++; k++; continue; }
if (v[i][k] == '.') { k++; continue; }
for (int x = j; x < k; x++) row[i][x] = k - j;
j = k = k + 1;
}
for (int x = j; x < k; x++) row[i][x] = k - j;
}
for (int j = 0; j < w; j++) {
int i = 0, k = 0; while (k != h) {
if (v[i][j] == '#') { i++; k++; continue; }
if (v[k][j] == '.') { k++; continue; }
for (int x = i; x < k; x++) col[x][j] = k - i;
i = k = k + 1;
}
for (int x = i; x < k; x++) col[x][j] = k - i;
}
ll all = power(2, K), res = 0;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++) if (v[i][j] == '.')
res = (res + all - power(2, K - row[i][j] - col[i][j] + 1) + mod) % mod;
cout << res << '\n'; cin.ignore(2); return 0;
} | #include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long LL;
const int N=500500;
int read(int &n)
{
bool q=0;n=0;char ch=' ';
for(;ch!='-'&&(ch<'0'||ch>'9');ch=getchar());
if(ch=='-')ch=getchar(),q=1;
for(;ch>='0'&&ch<='9';ch=getchar())n=(n<<3)+(n<<1)+ch-48;
return q?n=-n:n;
}
int n,m;
int a[N],a0[N];
int Ans[N];
int JS(int q,int w,int i,int j)
{
return abs(q-i)+abs(w-j);
}
int main()
{
int q,w,_;
read(_);
while(_--)
{
read(n);
fo(i,1,n)read(a[i]),a0[i]=a[i];
Ans[0]=0;
fo(I,1,n)
{
int nw;
fo(i,1,n)if(a[i]==I){nw=i;break;}
if(nw==I)continue;
if(((nw-1)&1)==(Ans[0]&1))
{
fod(i,n-1,1)if((i&1)!=(Ans[0]&1)){q=i;break;}
if(q==nw)
{
Ans[++Ans[0]]=q,swap(a[q],a[q+1]);
Ans[++Ans[0]]=q-1,swap(a[q],a[q-1]);
++nw;
fod(i,nw-1,I)Ans[++Ans[0]]=i,swap(a[i],a[i+1]);
}else if(q<I)
{
fo(i,1,n)if((i&1)!=(Ans[0]&1)){q=i;break;}
Ans[++Ans[0]]=q,swap(a[q],a[q+1]);
fod(i,nw-1,I)Ans[++Ans[0]]=i,swap(a[i],a[i+1]);
if(q%2!=Ans[0]%2)
{
Ans[++Ans[0]]=q,swap(a[q],a[q+1]);
}else{
Ans[++Ans[0]]=q+3,swap(a[q+3],a[q+4]);
Ans[++Ans[0]]=q,swap(a[q],a[q+1]);
Ans[++Ans[0]]=q+3,swap(a[q+3],a[q+4]);
}
}
else{
Ans[++Ans[0]]=q,swap(a[q],a[q+1]);
fod(i,nw-1,I)Ans[++Ans[0]]=i,swap(a[i],a[i+1]);
}
}else{
fod(i,nw-1,I)Ans[++Ans[0]]=i,swap(a[i],a[i+1]);
}
}
// fo(i,1,Ans[0])
// {
// q=Ans[i];
// if(Ans[i]%2 !=(i&1))printf("dfwefwefe\n");
// swap(a0[q],a0[q+1]);
// }
// fo(i,1,n-1)if(a0[i]>a0[i+1])printf("wefverbrtbrtbrterth\n");
printf("%d\n",Ans[0]);
fo(i,1,Ans[0])printf("%d ",Ans[i]);printf("\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define INF 1000000007
//0=48,A=65,a=97
int main() {
int n,k;cin >> n >> k;
string s;cin >> s;
for(int i = 0; i < k; i++) {
s += s;
string t;
for(int j = 0; j < 2*n; j+=2) {
if(s[j]=='R'){
if(s[j+1]=='P') t += 'P';
else t += 'R';
}
if(s[j]=='S'){
if(s[j+1]=='R') t += 'R';
else t += 'S';
}
if(s[j]=='P'){
if(s[j+1]=='S') t += 'S';
else t += 'P';
}
}
s=t;
}
cout << s[0] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template<typename T>
struct Doubling {
using F = function<int(int,int)>;
private:
const int LOG, n;
vector<vector<int>> index, DP;
vector<vector<T>> sum;
public:
Doubling(const int n_, const int LOG_ = (int)log2(numeric_limits<long long>::max()))
: LOG(LOG_+1), n(n_), index(LOG, vector<int>(n)), DP(LOG, vector<int>(n)), sum(LOG, vector<T>(n, 0)) {
for(int i = 0; i < n; i++) index[0][i] = (i+1)%n;
iota(DP[0].begin(), DP[0].end(), 0);
}
Doubling(const vector<int> move, const int LOG_ = (int)log2(numeric_limits<long long>::max()))
: Doubling(move.size(), LOG_) {
index[0] = move;
}
int operator() (const int i, const int j) { return DP[i][j]; }
//costはその点の通過コスト
//toは次にどこを含めるか
void AddVertex(const int from, const int to, const T cost = 0){
index[0][from] = to; sum[0][from] = cost;
}
//O(NlogN)
void Build(){
for(int k = 0; k+1 < LOG; k++){
for(int i = 0; i < n; i++){
index[k+1][i] = index[k][index[k][i]];
sum[k+1][i] = sum[k][i]+sum[k][index[k][i]];
}
}
}
void Build(F win){
for(int k = 0; k+1 < LOG; k++){
for(int i = 0; i < n; i++){
index[k+1][i] = index[k][index[k][i]];
DP[k+1][i] = win(DP[k][i], DP[k][index[k][i]]);
}
}
}
//O(logN)
int IndexQuery(const int from, long long cnt){
int ret = from;
for(int k = 0; cnt > 0; k++, cnt >>= 1){
if(cnt & 1){
if(index[k][ret] == -1){
while(index[0][ret] >= 0) ret = index[0][ret];
return ret;
} else ret = index[k][ret];
}
}
return ret;
}
T SumQuery(const int from, long long cnt){
int id = from; T ret = 0;
for(int k = 0; cnt > 0; k++, cnt >>= 1){
if(cnt & 1){
ret += sum[k][id];
id = index[k][id];
}
}
return ret;
}
};
void ARC109_C_LargeRPSTournament(){
int n,k;
cin >> n >> k;
string s;
cin >> s;
auto win = [&](int a, int b){
if(s[a] < s[b] && !(s[a] == 'P' && s[b] == 'S')) return a;
else if(s[a] < s[b]) return b;
else if(s[a] > s[b] && !(s[a] == 'S' && s[b] == 'P')) return b;
else if(s[a] > s[b]) return a;
else return a;
};
Doubling<int> table(n, k);
table.Build(win);
cout << s[table(k, 0)] << endl;
}
void ABC167_D_Teleporter(){
int n; long long k;
cin >> n >> k;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i], a[i]--;
Doubling<int> table(a);
table.Build();
cout << table.IndexQuery(0, k)+1 << endl;
}
void ABC179_E_SequenceSum(){
long long n,x,m;
cin >> n >> x >> m;
Doubling<long long> table(m);
long long y = x;
for(int i = 0; i < m; i++, y = y*y%m) table.AddVertex(y%m,y*y%m,y%m);
table.Build();
cout << table.SumQuery(x, n) << endl;
}
int main(){
ARC109_C_LargeRPSTournament();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define nline "\n"
const ll M1=1e9+7;
const ll M2=998244353;
const ll N=100005;
const ll INF_MUL=1e13;
const ll INF_ADD=1e18;
#define f first
#define s second
#define pll pair<ll,ll>
#ifndef ONLINE_JUDGE
#define debug(x) cerr<<#x<<" "; _print(x); cerr<<nline;
#else
#define debug(x);
#endif
void _print(ll x){cerr<<x;}
template<class T,class V> void _print(pair<T,V> p) {cerr<<"{"; _print(p.first);cerr<<","; _print(p.second);cerr<<"}";}
template<class T>void _print(vector<T> v) {cerr<<" [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";}
template<class T>void _print(set<T> v) {cerr<<" [ "; for (T i:v){_print(i); cerr<<" ";}cerr<<"]";}
template<class T>void _print(multiset<T> v) {cerr<< " [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";}
template<class T,class V>void _print(map<T, V> v) {cerr<<" [ "; for(auto i:v) {_print(i);cerr<<" ";} cerr<<"]";}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
vector<ll> a;
ll n; set<ll> check;
ll query(ll k){
if(-a.back()>k)
return k;
ll start=1,end=1e18+1e6,ans=k;
while(start<=end){
ll mid=(start+end)/2;
ll pos=a.end()-lower_bound(a.begin(),a.end(),-mid);
ll left=mid-pos;
ll found=0;
if(check.count(mid))
found=1;
if(left>k){
end=mid-1;
}
else if(left<k)
start=mid+1;
else{
if(found){
ans=mid-1;
end=mid-1;
}
else
return mid;
}
}
return ans;
}
void solve(){
ll q; cin>>n>>q;
for(ll i=1;i<=n;i++){
ll x; cin>>x;
a.pb(-x);
check.insert(x);
}
sort(a.begin(),a.end());
while(q--){
ll k; cin>>k;
cout<<query(k)<<nline;
}
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
ll t=1;
//cin>>t;
while(t--){
solve();
}
/*
Correct data type?
Infinity?
Base case?
*/
cerr<<"Time:"<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
} | #include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
readb(n, q);
readarr(a, n);
vi v(n + 2);
FOR (i, 1, n) v[i] = a[i] - i;
v[n + 1] = inf;
FOR (i, 1, q)
{
read(k);
auto it = lower_bound(all(v), k);
int x = it - v.begin() - 1;
print(a[x] + k - v[x]);
}
}
|
#include <iostream>
using namespace std;
const int mn=2e5;
long arr[mn+1]={};
long prec[mn+1]={};
long bestcp[mn+1]={};
int main() {
int N;
cin>>N;
for(int i=1;i<=N;i++){
cin>>arr[i];
prec[i]=arr[i]+prec[i-1];
bestcp[i]=max(bestcp[i-1],prec[i]);
//cout<<bestcp[i]<<' ';
}
//cout<<'\n';
long ans=0;
for(int i=1;i<=N;i++){
prec[i]+=prec[i-1];
ans=max(ans,prec[i-1]+bestcp[i]);
ans=max(ans,prec[i]);
//cout<<prec[i]<<' ';
}
//cout<<'\n';
cout<<ans<<'\n';
}
/*
2 -1 -2
2 1 -1
2 3 2
*/ | #include <iostream>
#include <vector>
#include <map>
using namespace std;
using lint = long long;
void solve() {
int n;
cin >> n;
lint ans = 0, sum = 0;
map<lint, lint> cnt{{0, 1}};
for (int i = 0; i < n; ++i) {
lint x;
cin >> x;
if (i % 2) x = -x;
sum += x;
ans += cnt[sum];
++cnt[sum];
}
cout << ans << "\n";
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PP;
//#define MOD 1000000007
#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define all1(i) begin(i),end(i)
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug false
#define debug2 false
class unionfind{
/*
Copyright (c) 2020 0214sh7
https://github.com/0214sh7/library/
*/
private:
std::vector<int> UF,rank;
public:
void init(int N){
UF.clear();
rank.clear();
for(int i=0;i<N;i++){
UF.push_back(i);
rank.push_back(0);
}
}
int root(int k){
if(UF[k]==k){
return k;
}else{
UF[k]=root(UF[k]);
return UF[k];
}
}
bool same(int p,int q){
return root(p)==root(q);
}
void unite(int P,int Q){
int p=root(P);
int q=root(Q);
if(p==q)return;
if(rank[p]<rank[q])std::swap(p,q);
UF[q]=p;
if(rank[p]==rank[q])rank[p]++;
}
};
int main(void){
ll N,K;
vector<vector<ll>> A;
cin >> N >> K;
REP(i,N){
vector<ll> B;
ll b;
REP(j,N){
cin >> b;
B.push_back(b);
}
A.push_back(B);
}
unionfind UFx,UFy;
UFx.init(N);
UFy.init(N);
REP(i,N){
REP(j,N){
if(i>=j)continue;
bool ok = true;
REP(k,N){
if(A[i][k]+A[j][k]>K){
ok = false;
}
}
if(ok){
UFx.unite(i,j);
}
}
}
REP(i,N){
REP(j,N){
if(i>=j)continue;
bool ok = true;
REP(k,N){
if(A[k][i]+A[k][j]>K){
ok = false;
}
}
if(ok){
UFy.unite(i,j);
}
}
}
vector<ll> perm;
perm.push_back(1);
REP(i,N){
ll p = perm[i]*(i+1);
p %= MOD;
perm.push_back(p);
}
vector<ll> Cx(N,0),Cy(N,0);
REP(i,N){
Cx[UFx.root(i)]++;
Cy[UFy.root(i)]++;
}
ll X=1,Y=1;
REP(i,N){
X = (X*perm[Cx[i]])%MOD;
Y = (Y*perm[Cy[i]])%MOD;
}
ll Ans = (X*Y)%MOD;
cout << Ans << endl;
return 0;
}
| #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
#define mod 998244353ll
#define maxn (100)
int a[maxn][maxn], exp[maxn], pa[maxn], siz[maxn], vis[maxn];
int findset(int v) {
return pa[v] = pa[v] == v? v: findset(pa[v]);
}
signed main()
{
int n, K; cin >> n >> K;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
scanf("%lld", &a[i][j]);
}
}
exp[1] = 1;
for(int i = 2; i <= 60; i++) {
exp[i] = (exp[i-1] * i) % mod;
}
for(int i = 1; i <= n; i++) {
pa[i] = i; siz[i] = 1; vis[i] = 0;
}
int ans = 1, cnt1 = 0, cnt2 = 0;
for(int i = 1; i < n; i++) {
for(int j = i + 1; j <= n; j++) {
int ok = 1;
for(int k = 1; k <= n; k++) {
if(a[i][k] + a[j][k] > K) {ok = 0; break;}
}
if(ok) {
int x = findset(i), y = findset(j);
if(x != y) {
pa[y] = x;
siz[x] += siz[y];
}
}
}
}
for(int i = 1; i <= n; i++) {
int x = findset(i);
if(vis[x]) continue;
ans = (ans * exp[siz[x]]) % mod;
vis[x] = 1;
}
for(int i = 1; i <= n; i++) {
pa[i] = i; siz[i] = 1; vis[i] = 0;
}
for(int i = 1; i < n; i++) {
for(int j = i + 1; j <= n; j++) {
int ok = 1;
for(int k = 1; k <= n; k++) {
if(a[k][i] + a[k][j] > K) {ok = 0; break;}
}
if(ok) {
int x = findset(i), y = findset(j);
if(x != y) {
pa[y] = x;
siz[x] += siz[y];
}
}
}
}
for(int i = 1; i <= n; i++) {
int x = findset(i);
if(vis[x]) continue;
ans = (ans * exp[siz[x]]) % mod;
vis[x] = 1;
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define ll long long
#define ld long double
#define test ll t=0;cin>>t; while(t--)
#define loop(i,a,b) for(i=a;i<=b;i++)
#define loopr(i,a,b) for(i=a;i>=b;i--)
#define lb lower_bound
#define ub upper_bound
#define mid(l,r) mid=l+(r-l)/2
#define endl "\n"
#define yess cout<<"YES\n"
#define noo cout<<"NO\n"
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
fastio;
ll n,k;cin>>n>>k;
vector<ll>a(n);
ll i,y=0;
ll s[1000000]={0};
ll ans=0;
for(i=0;i<n;i++)
{
cin>>a[i];
s[a[i]]++;
}
for(i=0;i<1e6;)
{
if(s[i]==0){
ans=ans+i;y++;
if(y==k)break;
if(i==0)break;
i=0;continue;
}
s[i]--;i++;
}
cout<<ans;
}
| #include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<algorithm>
#define int long long int
using namespace std;
signed main(){
int n, k, temp;
cin >> n >> k;
vector<int> arr(n);
for(int i=0;i<n;i++) cin >> arr[i];
sort(arr.begin(), arr.end());
vector<int> count(arr[arr.size()-1]+2);
for(int i : arr) count[i]++;
int ans = 0;
int current = min(k, count[0]);
for(int i=1;i<count.size();i++){
if(count[i] < current){
ans = ans + i*(current-count[i]);
current = count[i];
}
}
cout << ans << endl;
} |
#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 = 10007;
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) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 1;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve(){
deque<char> deq;
string s; cin >> s;
bool b = true;
rep(i, s.size()) {
if (s[i] == 'R')b = !b;
else {
if (b) {
if (deq.size() && deq.back() == s[i]) {
deq.pop_back();
}
else {
deq.push_back(s[i]);
}
}
else {
if (deq.size() && deq.front() == s[i]) {
deq.pop_front();
}
else {
deq.push_front(s[i]);
}
}
}
}
string ans;
while (deq.size()) {
ans.push_back(deq.front()); deq.pop_front();
}
if (!b)reverse(all(ans));
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//int t; cin >> t;rep(i,t)
//while(cin>>n,n)
//solve();
solve();
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using ll = long long;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
deque<char> d;
int t = 0;
for(char ch: s){
if(ch == 'R'){
t ^= 1;
}
else if(t == 0){
d.push_back(ch);
}
else{
d.push_front(ch);
}
}
if(t == 1){
reverse(d.begin(), d.end());
}
stack<char> ans;
for(char ch: d){
if(ans.empty() or ans.top() != ch){
ans.push(ch);
}
else{
ans.pop();
}
}
string res;
while(not ans.empty()){
res.push_back(ans.top());
ans.pop();
}
reverse(res.begin(), res.end());
cout << res << '\n';
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<int,int>;
typedef unsigned long long ull;
typedef long double ldouble;
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<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}
const ll INF=1e18;
const int mx=200005;
ll t[55][55];
ll p[55][55];
bool check(int i,int j){
return 0<=i&&i<50&&0<=j&&j<50;
}
int main(){
//オーバーフローは大丈夫ですか??
cin.tie(0);ios::sync_with_stdio(false);
int si,sj;
cin>>si>>sj;
rep(i,50)rep(j,50)cin>>t[i][j];
rep(i,50)rep(j,50)cin>>p[i][j];
queue<P> que;
V<int> seen(3000,0);
int f=1;
que.push({si,sj});
seen[t[si][sj]]=1;
V<int> dx={-1,0,1,0};
V<int> dy={0,1,0,-1};
V<int> C={'U','R','D','L'};
string ans="";
int num=0;
rep(qq,500000){
int cur=0;
string S="";
while(que.size()){
P q=que.front();
que.pop();
V<pair<P,pair<char,int>>> v;
int i=q.ft,j=q.sc;
rep(k,4){
if(check(i+dx[k],j+dy[k])&&!seen[t[i+dx[k]][j+dy[k]]]){
v.pb({{i+dx[k],j+dy[k]},{C[k],k}});
}
}
if(v.empty()) break;
if(f==30){
sort(all(v),[&](auto a,auto b){
return p[a.ft.ft][a.ft.sc]>p[b.ft.ft][b.ft.sc];
});
S+=v[0].sc.ft;
que.push(v[0].ft);
seen[t[v[0].ft.ft][v[0].ft.sc]]=1;
cur+=p[v[0].ft.ft][v[0].ft.sc];
}else if(f!=0){
V<int> cnt(4,0);
rep(x,50){
rep(y,50){
if(!seen[t[x][y]]){
if(y>=-x+i+j&&y>x-i+j) cnt[0]++;
else if(y>-x+i+j) cnt[1]++;
else if(y<x-i+j) cnt[2]++;
else cnt[3]++;
}
}
}
V<P> Z(4);
rep(k,4){
Z[k]={cnt[k],k};
}
sort(all(Z));
reverse(all(Z));
rep(k,4){
int dir=Z[k].sc;
for(auto pp:v){
if(pp.sc.sc==dir){
que.push(pp.ft);
S+=pp.sc.ft;
seen[t[pp.ft.ft][pp.ft.sc]]=1;
cur+=p[v[0].ft.ft][v[0].ft.sc];
goto L;
}
}
}
L:;
}else{
srand(time(NULL));
int sz=v.size();
int k=rand()%sz;
S+=v[k].sc.ft;
que.push(v[k].ft);
seen[t[v[k].ft.ft][v[k].ft.sc]]=1;
cur+=p[v[0].ft.ft][v[0].ft.sc];
}
//f=(f+1)%5;
}
if(cur>num){
num=cur;
ans=S;
}
}
out(ans);
} | //#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
#define rep2(x,fr,to) for(int x=(fr);x<(to);x++)
#define rep(x,to) for(int x=0;x<(to);x++)
#define repr(x,fr,to) for(int x=(fr);x>=(to);x--)
#define all(c) c.begin(),c.end()
#define sz(v) (int)v.size()
typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii; typedef vector<ll> VL; const int MD = 1e9+7; //998244353;
void dbg(){cerr<<"\n";} template <class F,class ...S> void dbg(const F& f, const S&...s){cerr <<f <<": "; dbg(s...);}
//using mint = atcoder::modint1000000007;
int main(){
cin.tie(0); ios_base::sync_with_stdio(false);
typedef long double DBL;
int n, m, k;
cin >>n >>m >>k;
VI a(n+1);
rep(i, k){ int q; cin >>q; a[q] =1;}
vector<DBL> dp(n+4), sm(n+4);
DBL l=0, r=4e12;
rep(ii, 200){
DBL md = (l+r) /2;
int ck = k;
dp[n] = sm[n] =0;
repr(i, n, 0){
if(a[i]==1) dp[i] = md;
else dp[i] = (sm[i+1] - sm[min(n, i+m+1)]) /m +1;
sm[i] = sm[i+1] + dp[i];
}
(dp[0] < md? r: l) = md;
}
if(l>=1e12) cout <<"-1\n";
else cout << fixed << setprecision(10) <<(l+r)/2 <<"\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
//#define int long long
template <typename T>
vector <T> uniq(vector <T> v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
return v;
}
int32_t main() {
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin >> n;
vector <int> a(n), d;
for(int &x: a) {
cin >> x;
for(int j = 1; j * j <= x; j++) if(x % j == 0) {
d.push_back(j);
d.push_back(x / j);
}
}
d = uniq(d);
int ans = 0;
for(int g: d) {
int gx = 0, flag = true;
for(int x: a) {
if(x % g) flag &= x > g;
else gx = __gcd(gx, x);
}
flag &= (gx == g);
ans += flag;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define fi first
#define se second
#define kill _z_kill
#define y0 _z_y0
#define y1 _z_y1
#define x0 _z_x0
#define x1 _z_x1
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long double LD;
typedef vector <int> vi;
typedef pair <int,int> pii;
void enable_comma(){}
string tostring(char c){
string s="";
s+=c;
return s;
}
string tostring(string s){
return "\""+s+"\"";
}
string tostring(const char *c){
return tostring((string)c);
}
string tostring(long long x){
if (x<0)
return "-"+tostring(-x);
if (x>9)
return tostring(x/10)+tostring(char('0'+x%10));
else
return tostring(char('0'+x));
}
string tostring(int x){
return tostring((long long)x);
}
string tostring(unsigned long long x){
if (x>9)
return tostring((long long)(x/10))+tostring(char('0'+x%10));
else
return tostring(char('0'+x));
}
string tostring(unsigned x){
return tostring((long long)x);
}
string tostring(double x){
static char res[114];
sprintf(res,"%lf",x);
string s=tostring(res);
return s.substr(1,(int)s.size()-2);
}
string tostring(long double x){
return tostring((double)x);
}
template <class A,class B> string tostring(pair <A,B> p){
return "("+tostring(p.fi)+","+tostring(p.se)+")";
}
template <class T> string tostring(T v){
string res="";
for (auto p : v)
res+=(res.size()?",":"{")+tostring(p);
return res.size()?res+"}":"{}";
}
template <class A> string tostring(A* a,int L,int R){
return tostring(vector <A>(a+L,a+R+1));
};
template <class A> string tostring(A a,int L,int R){
return tostring(a.data(),L,R);
}
string tostrings(){
return "";
}
template <typename Head,typename... Tail>
string tostrings(Head H,Tail... T){
return tostring(H)+" "+tostrings(T...);
}
#define User_Time ((double)clock()/CLOCKS_PER_SEC)
#ifdef zzd
#define outval(x) cerr<<#x" = "<<tostring(x)<<endl
#define outvals(...) cerr<<"["<<#__VA_ARGS__<<"]: "<<\
tostrings(__VA_ARGS__)<<endl
#define outtag(x) cerr<<"--------------"#x"---------------"<<endl
#define outsign(x) cerr<<"<"#x">"<<endl
#define outarr(a,L,R) cerr<<#a"["<<(L)<<".."<<(R)<<"] = "<<\
tostring(a,L,R)<<endl
#else
#define outval(x) enable_comma()
#define outvals(...) enable_comma()
#define outtag(x) enable_comma()
#define outsign(x) enable_comma()
#define outarr(a,L,R) enable_comma()
#endif
#ifdef ONLINE_JUDGE
#ifdef assert
#undef assert
#endif
#define assert(x) (!(x)?\
cout<<"Assertion failed!"<<endl<<\
"function: "<<__FUNCTION__<<endl<<\
"line: "<<__LINE__<<endl<<\
"expression: "<<#x<<endl,exit(3),0:1)
#endif
LL read(){
LL x=0,f=0;
char ch=getchar();
while (!isdigit(ch))
f=ch=='-',ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
template <class T> void ckmax(T &x,const T y){
if (x<y)
x=y;
}
template <class T> void ckmin(T &x,const T y){
if (x>y)
x=y;
}
//int Pow(int x,int y){
// int ans=1;
// for (;y;y>>=1,x=(LL)x*x%mod)
// if (y&1)
// ans=(LL)ans*x%mod;
// return ans;
//}
//void Add(int &x,int y){
// if ((x+=y)>=mod)
// x-=mod;
//}
//void Del(int &x,int y){
// if ((x-=y)<0)
// x+=mod;
//}
//int Add(int x){
// return x>=mod?x-mod:x;
//}
//int Del(int x){
// return x<0?x+mod:x;
//}
//int md(LL x){
// return (x%mod+mod)%mod;
//}
const int N=100005;
int n;
int a[N];
void solve(){
n=read();
For(i,1,n)
a[i]=read();
sort(a+1,a+n+1);
if (n&1){
puts("Second");
}
else {
LL s0=0,s1=0;
For(i,1,n){
if (i&1)
s0+=a[i];
else
s1+=a[i];
}
if (s0==s1)
puts("Second");
else
puts("First");
}
}
int main(){
int T=read();
while (T--)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<vector<vector<long long>>> vvvl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef long long ll;
const long long MOD=1000000007;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
int main(){
int n,m;
cin>>n>>m;
vi a(n);
vi b(m);
rep(i,n) cin>>a[i];
rep(i,m) cin>>b[i];
vi check(1001,0);
rep(i,n){
check[a[i]]++;
}
rep(i,m){
check[b[i]]++;
}
for (int i=1; i<=1000; i++){
if (check[i]==1) cout<<i<<" ";
}
cout<<endl;
} | #include <iostream>
#include <algorithm>
#include <tuple>
#include <vector>
#include <string>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <cassert>
using namespace std;
using ll = long long;
int num[1005];
int main()
{
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < m; i++){
cin >> b[i];
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i] == b[j]) num[a[i]] = 1;
else{
if(num[a[i]] != 1) num[a[i]] = 2;
if(num[b[j]] != 1) num[b[j]] = 2;
}
}
}
vector<int> ans;
for(int i = 1; i <= 1000; i++){
if(num[i] == 2) ans.push_back(i);
}
if(ans.size() == 0){
cout << "" << endl;
}else{
for(int i = 0; i < ans.size(); i++){
cout << ans[i] << " ";
}
cout << endl;
}
return 0;
} |
#include <iostream>
#include <string>
using namespace std;
int main() {
int i,l_S;
char c;
string S,T="",U;
cin >> S ;
l_S=S.size();
for(i=0;i<l_S ;i++){
c=S[i];
if(c=='6'){
T +="9";
}
else if(c=='9'){
T +="6";
}
else{
T+=c;
}
}
for(i=0;i<l_S;i++){
U=U+T[l_S-i-1];
}
cout << U << endl;
} | #include<bits/stdc++.h>
#define MAX 5000
#define MOD 1000000007
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a,b,c;
cin>>a>>b>>c;
cout<<21-(a+b+c)<<endl;
return 0;
} |
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <numeric>
#include <cmath>
#include <math.h>
#define cPI 3.14159265358979323846
using namespace std;
void func()
{
int64_t N;
cin >> N;
double m=0, e=0, c=0;
for (int64_t i = 0; i < N; ++i)
{
double x,absX;
cin >> x;
absX = fabs(x);
m += absX;
e += absX * absX;
c = max(absX, c);
}
e = sqrt(e);
cout << std::fixed << std::setprecision(15) << m << endl;
cout << std::fixed << std::setprecision(15) << e << endl;
cout << std::fixed << std::setprecision(15) << c << endl;
}
int main() {
// while (1)
func();
return 0;
}
| /* author:hellojim */
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
short negative=1;
x=0;
char c=getchar();
while(c<'0' || c>'9')
{
if(c=='-')
negative=-1;
c=getchar();
}
while(c>='0' && c<='9')
x=(x<<3)+(x<<1)+(c^48),c=getchar();
x*=negative;
}
ll quickpower(ll n,ll k){
ll ans=1;
while(k){
if(k%2){
ans*=n;
ans%=mod;
}
n*=n;
n%=mod;
k/=2;
}
return ans;
}
string int_to_string(int n)
{
string s="";
while(n)
{
int now=n%10;
s+=now+'0';
n/=10;
}
reverse(s.begin(),s.end());
return s;
}
int string_to_int(string s)
{
int n=0;
rep(i,s.size())
{
n*=10;
n+=s[i]-'0';
}
return n;
}
ll n;
ll a[100100];
int main()
{
ios::sync_with_stdio(0);
//think twice,code once
cin>>n;
rep(i,n)cin>>a[i];
ll sum=0;
rep(i,n)sum+=abs(a[i]);
ll mx=-INF;
rep(i,n)mx=max(mx,abs(a[i]));
long double x=0;
rep(i,n)x+=a[i]*a[i];
cout<<sum<<endl;
cout<<fixed<<setprecision(15)<<sqrt(x)<<endl;
cout<<mx<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#include<sstream>
#include<string>
#include<vector>
#include <set>
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define ff first
#define ss second
#define decimal(ans,w) cout<<fixed<<setprecision(w)<<ans
long long M=1000000007;
using namespace std;
ll fact[10000];
ll power(ll x, unsigned ll y, unsigned ll m)
{
if (y == 0)
return 1;
ll p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
unsigned long long modInverse(unsigned long long n,
ll p)
{
return power(n, p - 2, p);
}
unsigned long long nCrModPFermat(unsigned long long n,
ll r, ll p)
{
// If n<r, then nCr should return 0
if (n < r)
return 0;
// Base case
if (r == 0)
return 1;
return (fact[n] * modInverse(fact[r], p) % p
* modInverse(fact[n - r], p) % p)
% p;
}
int32_t main()
{
IOS
string s;
cin>>s;
ll n=s.length(),x=0;
deque<char>t;
for(ll i=0;i<n;i++)
{
if(s[i]=='R')
{
x^=1;
}
else if(x)t.push_front(s[i]);
else t.push_back(s[i]);
}
if(x)reverse(t.begin(),t.end());
string s1;
for(auto it:t)
{
if(s1.length()>0 && s1.back()==it)
{
s1.pop_back();
}
else s1.push_back(it);
}
cout<<s1;
return 0;
}
| #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define PI 3.141592653589793238462643383279502884L
#define make_unique(vec) vec.resize(distance(vec.begin(),unique(vec.begin(),vec.end())))
#define Sort(vec) sort(vec.begin(),vec.end())
#define RSort(vec) sort(vec.rbegin(),vec.rend())
#define endl "\n"
#define tr1(i) cout<<i<<endl;
#define tr2(i,j) cout<<i<<" "<<j<<endl;
#define tr3(i,j,k) cout<<i<<" "<<j<<" "<<k<<endl;
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define all(st) st.begin(),st.end()
#define vpl vector<pair<ll,ll> >
#define vpi vector<pair<int,int> >
#define pi pair<int,int>
#define pl pair<ll,ll>
#define al vector<list<int> >
#define vs vector<string>
#define vb vector<bool>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
#define rep(i,l,r) for(int i=l;i<r;i++)
#define repit(st) for(auto it=st.begin();it!=st.end();it++)
#define cs(ans); cout<<setprecision(10)<<fixed<<ans<<endl;
#define ull unsigned long long int
#define eb emplace_back
#define pb push_back
#define ll long long int
#define inf 1e16
#define f first
#define se second
const ll mod=1e9;
using namespace std;
using namespace __gnu_pbds;
ll mi(ll n,ll m){ll pw=n%m;ll ex=m-2;ll ans=1;while(ex){if(ex&1) ans = ans*pw%m;pw = pw*pw%m;ex>>=1;}return ans%mod;}
ll pw(ll a,ll n){ll pw=a;ll ex=n;ll ans=1;while(ex){if(ex&1) ans = ans*pw;pw = pw*pw;ex>>=1;}return ans;}
ll pwm(ll a,ll n){ll pw=a%mod;ll ex=n;ll ans=1;while(ex){if(ex&1) ans = ans*pw%mod;pw = pw*pw%mod;ex>>=1;}return ans%mod;}
template<typename T>istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; }
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) {os << t <<" ";} os << '\n'; return os; }
template<typename T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
//st.order_of_key(num)->number of elements < num
//st.find_by_order(i)->iterator of ith element in sorted order starting from zero
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
ll Abs(ll x){return x>0?x:(-x);}
ll Ceil(ll a,ll b){return a/b+(a%b!=0);}
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define mkp make_pair
//#define int long long int
//Binary_Search->dp->greedy
//auto check = [&](ll x){};
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
char move[]={'D','R','U','L'};
int X[]={2,1,-1,-2,-2,-1,1,2};
int Y[]={1,2,2,1,-1,-2,-2,-1};
struct comp{
bool operator()(const pair<int,int>&a,const pair<int,int>&b) const {
if(a.first>b.first) return true;
if(a.first<b.first) return false;
if(a.second<b.second) return true;
return false;
}
};
const int nax=1e5;
void solve(){
//ifstream fin("f.txt");
//ofstream fout("f1.txt");
ll n;
cin>>n;
ll ans=0;
map<ll,bool> vis;
for(ll i=2;i*i<=n;i++){
if(vis.count(i)==0){
ll curr=i*i;
while(curr<=n) vis[curr]=true,ans++,curr*=i;
}
}
cout<<(n-ans)<<endl;
}
int32_t main(){
int t=1;
//scanf("%d",&t);
while(t--){
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define elif else if
#define rep(i,s,e) for(int i=s;i<e;++i)
#define ret(i,c) for(auto i:c)
#define all(a) a.begin(),a.end()
#define len(a) (a.size())
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
using ll=long long;
int h,w,ans;
void solve(){
cin>>h>>w;
vector<string> ss(h);
rep(i,0,h)cin>>ss[i];
rep(i,0,h){
rep(j,0,w){
if(ss[i][j]=='#'){
//..
//.X
ans+=ss[i][j-1]=='.'&&ss[i-1][j]=='.'&&ss[i-1][j-1]=='.';
//..
//X.
ans+=ss[i][j+1]=='.'&&ss[i-1][j]=='.'&&ss[i-1][j+1]=='.';
//.X
//..
ans+=ss[i][j-1]=='.'&&ss[i+1][j]=='.'&&ss[i+1][j-1]=='.';
//X.
//..
ans+=ss[i][j+1]=='.'&&ss[i+1][j]=='.'&&ss[i+1][j+1]=='.';
//.#
//#X
ans+=ss[i][j-1]=='#'&&ss[i-1][j]=='#'&&ss[i-1][j-1]=='.';
//#.
//X#
ans+=ss[i][j+1]=='#'&&ss[i-1][j]=='#'&&ss[i-1][j+1]=='.';
//#X
//.#
ans+=ss[i][j-1]=='#'&&ss[i+1][j]=='#'&&ss[i+1][j-1]=='.';
//X#
//#.
ans+=ss[i][j+1]=='#'&&ss[i+1][j]=='#'&&ss[i+1][j+1]=='.';
}
}
}
cout<<ans<<endl;
}
int main(){
fastio;
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#include<cstdlib>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
typedef long long ll;
int main(){
int n,x;
cin >> n >> x;
string s;
cin >> s;
rep(i,n){
if(s.at(i)=='o') x++;
else x = max(x-1, 0);
}
cout << x;
} |
#include <iostream>
using namespace std;
int main() {
char c1;
char c2;
char c3;
cin >> c1;
cin >> c2;
cin >> c3;
if ( c1 == c2 && c2 == c3 ) cout << "Won";
else cout << "Lost";
return 0;
}
| #include <stdio.h>
constexpr unsigned char H[16] = {0, 1, 0, 3, 1, 1, 1, 1, 0, 1, 2, 2, 3, 1, 2, 3};
int main() {
unsigned char n, k;
scanf("%hhd %hhd", &n, &k);
getchar_unlocked();
unsigned char h[101], h2[101];
for (unsigned char i = 0; i < n; i++) h[i] = getchar_unlocked() - 80;
for (unsigned char i = 0; i < k; i++) {
unsigned char *cur = (i & 1) ? h2 : h, *nxt = (i & 1) ? h : h2;
for (unsigned char j = 0; j < n; j++) {
nxt[j] = H[(cur[(j * 2) % n] * 4) + cur[((j * 2) + 1) % n]];
}
}
putchar_unlocked(((k & 1) ? (*h2) : (*h)) + 80);
}
|
#include<bits/stdc++.h>
#define ll long long
#define int long long
using namespace std;
const int MOD = 998244353;
int n, m;
const int M = 5e3 + 5;
int dp[15][M], fact[M];
ll exp(ll a,ll n){
if(a==0) return 0;
if(n==0) return 1;
if(n==1) return a;
ll ans = exp(a,n/2);
ans*=ans; ans%=MOD;
if(n&1) ans*=a,ans%=MOD;
return ans;
}
int ncr(int n,int r){
return (fact[n] * exp((fact[r]*fact[n-r])%MOD, MOD-2)) % MOD;
}
int func(int i,int m){
if(i==13){
return (!m)?1:0;
}
int &ans = dp[i][m];
if(ans!=-1) return ans;
ans = 0;
for(int k=0;(1ll<<i)*k<=m && k<=n;k+=2){
ans += (ncr(n, k) * func(i+1, m - (1ll<<i)*k)) % MOD;
ans %= MOD;
}
return ans;
}
void solve(){
fact[0]=1;
for(int i=1;i<M;i++)
fact[i] = fact[i-1]*i%MOD;
memset(dp, -1, sizeof(dp));
cin>>n>>m;
cout<<func(0, m)<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
#define debug(e) cerr << #e << ": " << e << endl;
#define fast_io; ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const ll MOD=998244353;//;//1e9+9;
//ll tavmd(ll a,ll b){i1f(b==0){return 1;}if(b%2==0){ll x=tavmd(a,b/2);return(x*x)%MOD;}else{return(a%MOD*tavmd(a,b-1)%MOD)%MOD;}}
//void mkay(ll &a){if(a>=MOD){a-=MOD;}else if(a<0){a+=MOD;}}
const ll MAXN=5e3+10;
const ll INF=8e18;
const ll LOG=30;
ll dp[MAXN],c[MAXN][MAXN];
inline void khayam_bozorgvar(){
c[0][0]=1;
for(int i=1;i<MAXN;i++){
c[0][i]=1;
// c[1][i]=i;
}
for(int j=1;j<MAXN;j++){
for(int i=1;i<MAXN;i++){
c[i][j]=(c[i][j-1]+c[i-1][j-1])%MOD;
}
}
}
int main(){
fast_io;
ll n,m;
cin>>n>>m;
khayam_bozorgvar();
dp[0]=1;
for(int i=2;i<=m;i+=2){
for(int j=0;j<=n;j++){
if(i>=2*j){
ll mm=(c[2*j][n]*dp[(i-2*j)/2])%MOD;
dp[i]+=mm;
dp[i]%=MOD;
}
}
}
cout<<dp[m];
return 0;
}
|
// Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int ll
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define ll long long
#define pi pair<int,int>
#define pii pair<pi,int>
#define vi vector<int>
#define pb push_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
if(b==0)return a;
return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
ll res = 1;
while(a>0){
if(a&1){
res = (res*x)%mod;
}
a=a>>1;
x=(x*x)%mod;
}
return res;
}
ll inverse(ll n){
return fastexp(n,mod-2);
}
template <typename T>
void add(T &a, T b){
a += b;
if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b){
a -= b;
if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b){
a *= b;
if(a >= mod)a %= mod;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,x;
cin>>n>>x;
vi v(n);
rep(i,n){
cin>>v[i];
}
map<int,int> mp;
mp[x] = 1;
rep(i,n-1){
map<int,int> newmp;
for(auto x:mp){
// x.fi ke sabse pass vaale 2 numbers which
// are divisible by v[i+1] (might be 1)
int num = x.fi;
int r = num%v[i+1];
if(r == 0){
newmp[num] += x.sec;
}
else{
newmp[num-r] += x.sec;
newmp[num+v[i+1]-r] += x.sec;
}
}
mp = newmp;
}
int ans = 0;
for(auto x:mp){
ans += x.sec;
}
cout<<ans;
return 0;
} | #include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
long long a[105],l,k,n,m,f[105][2],c[105],b[105];
int main()
{
scanf("%lld%lld",&n,&m);
for (int i=1;i<=n;i++)
scanf("%lld",&a[i]);
sort(a+1,a+1+n);l=m;
for (int i=n;i>=1;i--)
{
b[i]=l/a[i];
l=l%a[i];
}
for (int i=1;i<n;i++)
c[i]=a[i+1]/a[i];
f[0][0]=1;
for (int i=1;i<=n;i++)
{
if (b[i]==c[i]-1) {f[i][1]=f[i-1][1]+f[i-1][0];f[i][0]=f[i-1][0];}
else if (b[i]==0) {f[i][1]=f[i-1][1];f[i][0]=f[i-1][0]+f[i-1][1];}
else {f[i][1]=f[i-1][0]+f[i-1][1];f[i][0]=f[i-1][1]+f[i-1][0];}
}
cout<<f[n][0];
return 0;
} |
#include<string>
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
// driver program
int main()
{
int n, x;
string s;
cin >> n >> x;
cin >> s;
for (int i = 0; i < n; i++)
{
if (x < 0) { x = 0;}
else if (s[i] == 'x') { x += -1; }
else if (s[i] == 'o') { x += 1; }
}
if (x < 0) { x = 0; }
cout << x;
}
| #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
int main(){
int H, W;
cin >> H >> W;
vector<string> T(H);
rep(i, H) cin >> T[i];
vector<vector<int>> S(H, vector<int>(W, 0));
rep(i, H){
rep(j, W){
if(T[i][j] == '.')
S[i][j] = 0;
else
S[i][j] = 1;
}
}
S[0][0] = 1;
S[0][W-1] = 1;
S[H-1][0] = 1;
S[H-1][W-1] = 1;
vector<int> P(H+W, 1);
int i = 0;
int column = W, row = H;
int k;
int c;
int count = 0;
queue<int> q;
while(i < H + W){
if(P[i] == 0){
i ++;
continue;
}
q.push(i);
c = 0;
while(q.size() > 0){
k = q.front();
q.pop();
if(k < H){
rep(j, W){
if(S[k][j] == 1 && P[H+j] == 1){
q.push(H+j);
P[H+j] = 0;
c = 1;
}
}
}
else{
rep(j, H){
if(S[j][k-H] == 1 && P[j] == 1){
q.push(j);
P[j] = 0;
c = 1;
}
}
}
}
if(c == 1)
P[i] = 0;
count ++;
i ++;
}
int a = 0, b = 0;
rep(s, H){
if(P[s] == 1)
a ++;
}
for(int s = H; s < H + W; s ++){
if(P[s] == 1)
b ++;
}
cout << (count - max(a, b) - 1) << endl;
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define fi first
#define se second
#define pi 3.141592653589793
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
#define sleep for (int i = 1, a;i < 100000000;i++, a = a * a)
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map;
int main() {
fio;
int n;
cin >> n;
vl a(n + 1);
for (int i = 1;i <= n;i++)
cin >> a[i];
sort(all(a));
ll ans = 1;
for (int i = 1;i <= n;i++)
ans = ans * (a[i] - a[i - 1] + 1) % mod;
cout << ans << '\n';
}
| #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#define N 201000
template<typename T> inline void read(T &x) {
x = 0; char c = getchar(); bool flag = false;
while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); }
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
if (flag) x = -x;
}
using namespace std;
int n;
int a[N], b[N], h[N], htot;
long long ans;
inline void sol(int L, int R) {
if (L + 1 == R) return ;
// printf("sol(%d, %d) : \n", L, R);
int ptr = R;
for (int i = L + 1; i < R; ++i) if (b[i] > a[i]) { ptr = i; break; }
// printf("ptr = %d\n", ptr);
int tol = a[ptr - 1];
for (int i = ptr - 1; i > L; --i) {
if (b[i] < tol) {
int p = upper_bound(a + L, a + R + 1, b[i]) - a - 1;//bug
// printf("i = %d, p = %d\n", i, p);
ans += i - p;
tol = b[i];
}
}
int tor = a[ptr];
for (int i = ptr; i < R; ++i) {
if (b[i] > tor) {
int p = lower_bound(a + L, a + R + 1, b[i]) - a;//bug bug
// printf("i = %d, p = %d\n", i, p);
ans += p - i;
tor = b[i];
}
}
}
int main() {
read(n);
int L;
read(L);
++n; ++n;
a[0] = b[0] = 0; a[n - 1] = b[n - 1] = L + 2 - n;//0 ... L + 1
for (int i = 1; i < n - 1; ++i) read(a[i]), a[i] -= i;
for (int i = 1; i < n - 1; ++i) read(b[i]), b[i] -= i;
// for (int i = 0; i < n; ++i) printf("%d ", a[i]); puts("");
// for (int i = 0; i < n; ++i) printf("%d ", b[i]); puts("");
for (int i = 0; i < n; ++i) h[++htot] = a[i];
htot = unique(h + 1, h + 1 + htot) - h - 1; h[htot + 1] = -12341;
for (int i = 0; i < n; ++i) a[i] = lower_bound(h + 1, h + 1 + htot, a[i]) - h;
for (int i = 0; i < n; ++i) {
int v = lower_bound(h + 1, h + 1 + htot, b[i]) - h;
if (h[v] != b[i]) return puts("-1"), 0;
b[i] = v;
}
// for (int i = 0; i < n; ++i) printf("%d ", a[i]); puts("");
// for (int i = 0; i < n; ++i) printf("%d ", b[i]); puts("");
int lst = 0;
for (int i = 1; i < n; ++i) if (a[i] == b[i]) {
// printf("\ni = %d : \n", i);
sol(lst, i); lst = i;
// printf("ans = %lld\n", ans);
}
printf("%lld\n", ans);
return 0;
}
|
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author
*/
#include <iostream>
#include <fstream>
#ifndef PRELUDE_H
#define PRELUDE_H
#define M_PI 3.14159265358979323846
#define EPS 1e-9
#include <algorithm>
#include <cmath>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> ii;
typedef std::pair<ll, ll> llll;
typedef std::pair<int, ll> ill;
typedef std::pair<double, double> dd;
typedef std::vector<int> vi;
typedef std::vector<ii> vii;
typedef std::vector<vi> vvi;
typedef std::vector<vvi> vvvi;
typedef std::vector<vii> vvii;
typedef std::vector<ill> vill;
typedef std::vector<vill> vvill;
typedef std::vector<bool> vb;
typedef std::vector<char> vc;
typedef std::vector<std::vector<char>> vvc;
typedef std::vector<ll> vll;
typedef std::vector<vll> vvll;
typedef std::vector<llll> vllll;
typedef std::vector<double> vd;
typedef std::vector<dd> vdd;
typedef std::string ss;
typedef std::vector<ss> vss;
typedef std::vector<vss> vvss;
typedef std::set<int> si;
typedef std::set<ii> sii;
typedef std::set<ll> sll;
typedef std::map<int, int> mii;
typedef std::map<char, int> mci;
typedef std::queue<int> qi;
typedef std::vector<std::pair<int, ii>> EdgeList;
#define REP(i,n) for(int i=0;i<n;++i)
#define DEBUG(a) #a << ": " << (a) << " " <<
#define ENDL "\n"
#define REPLL(l,n) for(ll l=0;l<n;++l)
#define FOR(i,s,n) for(int i=s;i<=n;++i)
#define FOREACH(it,a) for(auto it=a.begin(); it != a.end(); ++it)
#endif //PRELUDE_H
class DKthExcluded {
public:
void solve(std::istream& in, std::ostream& out) {
int n, q;
in >> n >> q;
vll a(n);
REP(i, n) {
in >> a[i];
}
std::sort(a.begin(), a.end());
vllll k(q);
REP(i, q) {
ll ki;
in >> ki;
k[i] = llll(ki, i);
}
std::sort(k.begin(), k.end());
vll ans(q);
int extra = 0;
REP(i, q) {
auto [query, index] = k[i];
while (extra < n && query+extra >= a[extra]) {
extra++;
}
ans[index] = query+extra;
}
FOREACH(it, ans) {
out << *it << ENDL;
}
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout << std::fixed;
std::cout << std::setprecision(15);
DKthExcluded solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<long long> Vll;
ll mm=1000000000;ll MM=mm+7;
#define rep(i, n) for(int i=0;i<n;i++)
#define PI 3.141592653589793
int main(){
ll n,q;
cin >> n >> q;
Vll a(n);
rep(i,n)cin >> a[i];
sort(a.begin(),a.end());
Vll ans(q);
rep(i,q){
ll k;
cin >> k;
ll r=n-1;
ll l=0;
ll cs;
if(k<a[0])ans[i]=k;
else if(k>=a[n-1])ans[i]=k+n;
else{
while(r-l>1){
cs=(r+l)/2;
if(a[cs]<=k)l=cs;
else r=cs;
}
ll nund=l;
ll und=-1;
while(und!=nund){
k+=nund-und;
und=nund;
if(k>=a[n-1]){
k+=n-1-und;
break;
}
ll r=n-1;
ll l=0;
ll cs;
while(r-l>1){
cs=(r+l)/2;
if(a[cs]<=k)l=cs;
else r=cs;
}
nund=l;
}
ans[i]=k;
}
}
rep(i,q)cout << ans[i] << endl;
}
|
#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
constexpr ll mod = 1000000007LL;
int N;
char caa, cab, cba, cbb;
void input(void){
cin >> N;
cin >> caa >> cab >> cba >> cbb;
}
ll modpow(ll a, ll b){
if (b == 0) return 1LL;
ll tmp = modpow(a, b / 2);
tmp = tmp * tmp % mod;
if (b % 2) tmp = tmp * a % mod;
return tmp;
}
ll solve(void){
if (N <= 3) return 1LL;
if (
(cab == 'A' && caa == 'A') ||
(cab == 'B' && cbb == 'B')
) return 1LL;
if (
(cab == 'A' && cba == 'A') ||
(cab == 'B' && cba == 'B')
){
ll tmpa = 1, tmpb = 0;
for (int i = 2; i <= N; ++i){
ll new_a = (tmpa + tmpb) % mod;
ll new_b = tmpa;
tmpa = new_a, tmpb = new_b;
}
return tmpb;
}
return modpow(2, N - 3);
}
int main(void){
input();
cout << solve() << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=100010,mod=1000000007;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#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 MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=0,f=0;
while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return f?-x:x;
}
inline int qmo(int x){return x+(x>>31?mod:0);}
int n,f[maxn][2];
char c[2][2];
int main(){
n=read()-2;
FOR(i,0,1) FOR(j,0,1) while(c[i][j]!='A' && c[i][j]!='B') c[i][j]=getchar();
if(c[0][1]=='A'){
if(c[0][0]=='A') puts("1");
else{
if(c[1][0]=='A'){
f[0][0]=1;
FOR(i,1,n){
f[i][0]=(f[i-1][0]+f[i-1][1])%mod;
f[i][1]=f[i-1][0];
}
printf("%d\n",f[n][0]);
}
else{
if(!n) puts("1");
else{
int pw=1;
FOR(i,1,n-1) pw=2*pw%mod;
printf("%d\n",pw);
}
}
}
}
else{
if(c[1][1]=='B') puts("1");
else{
if(c[1][0]=='B'){
f[0][0]=1;
FOR(i,1,n){
f[i][0]=(f[i-1][0]+f[i-1][1])%mod;
f[i][1]=f[i-1][0];
}
printf("%d\n",f[n][0]);
}
else{
if(!n) puts("1");
else{
int pw=1;
FOR(i,1,n-1) pw=2*pw%mod;
printf("%d\n",pw);
}
}
}
}
} |
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("no-stack-protector")
#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 ld long double
#define ll long long
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPI(i, a, b) for (ll i = b - 1; i >= a; i--)
#define i_os ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define INF (ll)1e18 + 100
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
// SOME BITMASK KNOWLEDGE
// 1)x & (x - 1):sets the last one bit of x to zero
// power of two exactly when x & (x − 1) = 0.
// 2)x & -x:sets all the one bits to zero, except last one bit
// 3)x | (x - 1):inverts all the bits after the last one bit
#define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define o_setll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
typedef tree<pair<ll, ll>,null_type,less<pair<ll, ll>>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_set1;
typedef tree<string,null_type,less<string>,rb_tree_tag,tree_order_statistics_node_update> indexed_set2;
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll power(ll a,ll n,ll c){
if(n == 0) return 1 % c;
ll s = power(a,n/2,c);
s = (s * s) % c;
if(n % 2) s = (s * a) % c;
return s;
}
int main()
{
i_os;
ll n,x;
cin>>n>>x;
x *= 1000;
ll curr = 0;
ll ans = -1;
REP(i,0,n){
ll a,b;
cin>>a>>b;
curr += (a*b)*10;
if(curr > x && ans == -1){
ans = i+1;
}
}
cout<<ans<<endl;
return 0;
}
| /*
* ────────────── •✵•✵• ──────────────
* | In The Name of Allah |
* | Author: Amir Hossein Farhadi |
* | Created: 2021-04-28 |
* ────────────── •✵•✵• ──────────────
*/
// ju't ujnf up spbs !
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <set>
#include <map>
#include <vector>
#include <iomanip>
#define fi first
#define se second
#define pb push_back
#define si(s) s.size()
#define MAXI 2147483647
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
vector <int> a;
int h, w;
void get_input() {
cin >> h >> w;
}
void solve() {
for (int i = 0; i < h * w; i ++){
int kom;
cin >> kom;
a.pb(kom);
}
int mini = *min_element(all(a));
ll ans = 0;
for (auto x : a){
ans += x - mini;
}
cout << ans;
}
void output() {
}
int main () {
ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tc = 1;
// cin >> tc;
while (tc --) {
get_input(), solve(), output();
}
return 0;
}
/* Thanks Allah */
|
#include <bits/stdc++.h>
#define ll long long int
#define db double
#define pb push_back
#define mpr make_pair
#define andl "\n"
#define f first
#define s second
#define mset(x,y) memset(x,y,sizeof(x))
#define fr(i,n) for(long long int i=0;i<n;i++)
#define trace(it,x) for(auto it = (x).begin(); it != (x).end(); it++)
#define mod 10000000
#define fastio ios::sync_with_stdio (false); cin.tie (0); cout.tie (0);
#define runtime cerr<< '\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n" ;
using namespace std;
// convert number to string to_string(x);
// convert string to number stoi();
vector<ll>v;
vector<bool>prime(mod+10,true);
void simpleSieve()
{
for (int p=2; p*p<=mod; p++)
{
// If p is not changed, then it is a prime
if (prime[p] == true)
{
for (int i=p*p; i<=mod; i+=p)
prime[i] = false;
}
}
for(int i=2;i<=mod;i++)
if(prime[i])
v.pb(i);
}
void solve()
{
ll n,k;
cin >> n >> k;
map<ll,ll>m;
ll a,b;
fr(i,n)
{
cin >> a >> b;
m[a]+=b;
}
ll prev=0;
for(auto it=m.begin();it!=m.end();it++)
{
if(k>=it->first-prev)
{
k=k-(it->first-prev)+it->second;
prev=it->first;
}
}
cout << k+prev;
return ;
}
int main()
{
fastio
ll t=1;
// cin >> t;
while(t--)
solve();
runtime
} | //x<<y=x*2^y,x>>y=x/2^y
//1e5+3 is prime
// in a matrix, all elements of a primary diagonal have constant diff of coordinates
// and a secondary diagonal has constant sum of coordinates
//use ll() for using an integer in self-built fn
//(x&-x) bitwise and of (x and its 2's complement) returns (last set bit)
//eg if x=1010 then it will return 0010
// careful dont print empty container(run time error)
//v.erase O(n)
//use ("\n") instead of endl as endl flushes the output buffer
//every time so takes more time than \n (TLE)
//stoll() and to_string((less than 1e19))
//INT_MAX is approx 3e10
//For sets use set_name.lower_bound(x)(strictly O(logn))
//NOT lb(all(s),x)(O(logn) for random access (eg vector) but for sets it is O(n));
#include<bits/stdc++.h>
typedef long long int ll;
#define ull unsigned long long int
#define lld long double
#define endl "\n"
#define fi first
#define sec second
#define sp setprecision
#define lb lower_bound
#define ub upper_bound
#define For(i, a, b) for(long long int i = (a); i <= (b); i++)
#define Forr(i, a, b) for(long long int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define vll vector<ll>
#define pll pair<ll,ll>
#define plld pair<lld,lld>
#define vlld vector<lld>
#define vi vector<int>
#define vch vector<char>
#define sll set<ll>
#define sch set<ch>
#define vpll vector< pair<ll,ll> >
#define vpii vector< pair<int,int> >
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define mll map<ll, ll>
#define mcll map<char,ll>
#define sz(container) ll((container).size())
#define fill(a,b) memset(a, b, sizeof(a))
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
#define PI 3.1415926535897932384626
const ll mod=1000000007;
//const ll dx[4]={-1, 0, 1, 0} , dy[4]={0, 1, 0, -1};
const ll dxx[8]={-1, -1, 0, 1, 1, 1, 0, -1} , dyy[8]={0, 1, 1, 1, 0, -1, -1, -1};
string ds="RLDU";
ll lcm(ll a, ll b)
{
ll g=__gcd(a, b);
return a/g*b;
}
ll binpow(ll a, ll b,ll m)
{
a %= m;
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll Pow(ll a, ll b)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a;
a = a * a ;
b >>= 1;
}
return res;
}
ll modinv(ll n)
{
return binpow(n, mod - 2,mod);
}
/*******************************************coding********/
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast_io;
ll T=1;//cin>>T;
For(i,1,T)
{
lld a,b,c,d;cin>>a>>b>>c>>d;
cout<<sp(15)<<(b*c+a*d)/(b+d);
}
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n=0;
long long int a=0,s=0,mx=0,x=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&x);
mx=max(mx,x);
a+=x;
s+=a;
printf("%lld\n",s+i*mx);
}
return 0;
} | #include<stdio.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static inline int IN(void)
{
int x=0,f=1,c=getchar();while(c<48||c>57){if(c==45){f=-f;}c=getchar();}
while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f*x;
}
static inline void OUT(long x){if(x<0){putchar('-'),x=-x;}if(x>=10){OUT(x/10);}putchar(x-x/10*10+48);}
int main(void)
{
int N=IN(),A=0,B[200]={};long count=0;rep(i,N){A=IN();B[A-A/200*200]++;}
rep(i,200){count+=1l*B[i]*(B[i]-1)/2l;}OUT(count);
} |
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define int long long
#define P pair<int,int>
template<class T>int chmin(T&a,const T&b){if(b<a){a=b;return 1;}return 0;}
void dijkstra()
{
int n,m;cin>>n>>m;
int cnt=0;
vector<vector<P>>g(n);
for(int i=0;i<m;i++)
{
int a,b,cst=1;cin>>a>>b;a--;b--;
g[a].push_back(make_pair(b,cst));
}
for(int s=0;s<n;s++)
{
vector<int>dist(n,(1LL<<60));dist[s]=0;
priority_queue<P,vector<P>,greater<P>>pq;
pq.push(make_pair(dist[s],s));
while(!pq.empty())
{
auto[d,v]=pq.top();
pq.pop();
if(d>dist[v])continue;
for(auto e:g[v])if(chmin(dist[e.first],dist[v]+e.second)) pq.push(make_pair(dist[e.first],e.first));
}
for(auto p:dist)if(p<1LL<<60)cnt++;
}
cout<<cnt<<endl;
}
main(){dijkstra();} | #pragma region Macros
#include <bits/stdc++.h>
#if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG)
#include <atcoder/all>
#endif
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>using vec = vector<T>;
template<class T, class U>using umap = unordered_map<T, U>;
template<class T>using uset = unordered_set<T>;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//using T = tuple<ll, ll, ll>;
using vl = vec<ll>;
#define fi first
#define se second
#define el endl
constexpr ll INF = numeric_limits<ll>::max()/2-1;
#pragma endregion
#pragma region IOMacros
template<class T>
istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T>
ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;}
#define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__);
void __i() {}
template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);}
void O() {cout << el;}
template<class T, class... Ts> void O(T&& o, Ts&&... args){cerr << o << " ";O(forward<Ts>(args)...);}
#pragma endregion
void Main();
int main(){
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
}
#pragma region graph
struct edge{
ll from, to, cost;
bool operator<(const edge& r) const{return cost<r.cost;}
bool operator>(const edge& r) const{return cost>r.cost;}
};
struct graph{
ll V;
vector<vector<edge> > G;
graph(ll n){
init(n);
}
void init(ll n){
V = n;
G.resize(V);
}
// 無向グラフ
void add_edge(ll s, ll t, ll cost = 1){
add_diedge(s, t, cost);
add_diedge(t, s, cost);
}
// 有向グラフ
void add_diedge(ll s, ll t, ll cost = 1){
if(s < 0 || t < 0 || s >= V || t >= V) return;
G[s].push_back({s, t, cost});
}
auto pos2d(ll height, ll width){
return [height, width](ll y, ll x){
return
(y < 0 || y >= height || x < 0 || x >= width)
? -1
: y*width + x;
};
}
};
#pragma endregion
// O(V+E)
umap<ll, ll> bfs(const graph& g, ll s, ll limit = INF){
vector<ll> d(g.V, INF);
umap<ll, ll> ret;
d[s] = 0;
queue<P> que;
que.push({0, s});
ret[s] = 0;
while(!que.empty()){
auto [c, v] = que.front(); que.pop();
if(d[v]<c) continue;
for(auto e : g.G[v]){
if(d[e.to]>d[v]+1 && limit>=d[v]+1){
d[e.to] = d[v]+1;
que.push({d[e.to],e.to});
ret[e.to] = d[e.to];
}
}
}
return ret;
}
void Main(){
I(ll, N, M);
graph g(N);
REP(i, M){
I(ll, a, b);
a--,b--;
g.add_diedge(a, b);
}
ll ans = 0;
REP(i, N){
auto d = bfs(g, i);
ans += d.size();
}
cout << ans << el;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REPP(i,a,b,d) for(int i=(a);i<=(int)(b);i+=(d))
#define REP(i,a,b) REPP(i,a,b,1)
#define REVV(i,a,b,d) for(int i=(a);i>=(int)(b);i-=(d))
#define REV(i,a,b) REVV(i,a,b,1)
#define FOR(i,a) for(int i=0; i<(int)(a); i++)
#define FORD(i,a) for(int i=(int)(a)-1; i>=0; i--)
#define pb push_back
typedef long long ll;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while(t--) {
int n; cin >> n;
vector<string> s(3);
FOR(i, 3) cin >> s[i];
FOR(i, n) cout << 0;
FOR(i, n) cout << 1;
cout << 0 << '\n';
}
return 0;
} | #include<bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#define endl "\n"
#define pb push_back
#define ll long long
#define d1(x) cerr << #x << "--> " << x << endl
#define d2(x,y) cerr << #x << "--> " << x << " | " << #y << "--> " << y <<endl
#define d3(x,y,z) cerr << #x << "--> " << x << " | " << #y << "--> " << y <<" | " << #z << "--> "<< z<< endl
#define d4(x,y,z,w) cerr << #x << "--> " << x << " | " << #y << "--> " << y <<" | " << #z << "--> "<< z << " | "<< #w << "--> " << w <<endl
#define vpll vector<pair<ll,ll>>
#define F first
#define S second
using namespace std;
ll mode = 1e9 + 7;
const ll maxn = 2*1e5 + 5;
const ll inf = 1e18;
ll __lcm(ll a, ll b){ return (a*b)/__gcd(a,b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getRand(ll l, ll r){ uniform_int_distribution<int> uid(l, r); return uid(rng); }
template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) { return out << "(" << a.F << ", " << a.S << ")"; }
template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
out << "["; for (int i=0;i<v.size();i++) { if(i) out << ", "; out << v[i]; } return out << "]"; }
void solve(){
ll n,k;
cin>>n>>k;
map<ll,ll> mp;
for (int i=0;i<n;i++){
ll a,b;
cin>>a>>b;
mp[a]+=b;
}
vpll v;
for (auto i:mp){
v.push_back({i.F, i.S});
}
n = v.size();
ll pos = 0, cur = k;
for (int i=0;i<n;i++){
if (pos+cur>=v[i].F){
cur -= (v[i].F - pos);
pos = v[i].F;
cur += v[i].S;
}
else{
pos += cur;
break;
}
if (i==n-1){
pos += cur;
}
}
cout<<pos<<endl;
}
int main(){
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
srand(time(0));
ios::sync_with_stdio(0);
cin.tie(0);
ll test_case = 1;
//cin>>test_case;
for (int i=1;i<=test_case;i++){
solve();
}
return 0;
} |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define LL long long
#define DB double
#define MAXN 100
#define Pr pair<int,int>
#define X first
#define Y second
#define INF 1000000001
#define mem(x,v) memset(x,v,sizeof(x))
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 x*F;
}
int MOD;
inline int add(int a,int b){return (a+b>=MOD)?a+b-MOD:a+b;}
inline int dec(int a,int b){return (a-b<0)?a-b+MOD:a-b;}
int mul(int a,int b){return (1LL*a*b)%MOD;}
int fst_pow(int a,int b){
int res=1;
while(b){
if(b&1)res=mul(res,a);
a=mul(a,a),b>>=1;
}return res;
}
inline void upd(int &a,int b){a=add(a,b);}
int n,k,lim,res,dp[MAXN+5][MAXN*MAXN*MAXN+5];
int main(){
n=read(),k=read(),MOD=read();
lim=k*n*(n-1)/2;
dp[0][0]=1;
for(int i=1;i<n;i++){
for(int j=0;j<=lim;j++){
upd(dp[i][j],dp[i-1][j]);
if(j>=i)upd(dp[i][j],dp[i][j-i]);
}
for(int j=lim;j>=i*(k+1);j--)
upd(dp[i][j],dec(0,dp[i][j-i*(k+1)]));
}
for(int i=1;i<=n;i++){
res=0;
for(int j=0;j<=lim;j++)
res=add(res,mul(dp[i-1][j],dp[n-i][j]));
printf("%d\n",dec(mul(res,k+1),1));
}
} | #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define mp make_pair
#define pb push_back
#define ll long long
//#define debug(x) ;
#define debug(x) cerr << #x << " = " << x << "\n";
ostream& operator<<(ostream& cerr, vector<ll> aux) {
cerr << "[";
for (auto e : aux) cerr << e << ' ';
cerr << "]";
return cerr;
}
const int maxSum = 1000011;
ll n, k, mod;
ll pos[maxSum], neg[maxSum], aux[maxSum];
ll ans[111];
void addGroup(ll *data, ll val) {
ll limit = k * val * val + 1;
memcpy(aux, data, limit * sizeof(ll));
for (int i = 0; i < limit; i++) {
if (i - val >= 0)
data[i] += data[i - val];
if (i - val * (k + 1) >= 0)
data[i] += mod - aux[i - val * (k + 1)];
data[i] %= mod;
}
}
void removeGroup(ll *data, ll val) {
ll limit = k * val * val + 1;
memcpy(aux, data, limit * sizeof(ll));
for (int i = 0; i < limit; i++) {
if (i - val >= 0)
data[i] += mod - aux[i - val];
if (i - val * (k + 1) >= 0)
data[i] += data[i - val * (k + 1)];
data[i] %= mod;
}
}
int main()
{
//freopen("test.in", "r", stdin);
cin >> n >> k >> mod;
// build full positive up to + (n - 1)
pos[0] = k + 1;
for (int i = 1; i < n; i++)
addGroup(pos, i);
// build empty negative
neg[0] = 1;
for (int i = 1; i <= n; i++) {
if (n - i + 1 < i) {
cout << ans[n - i + 1] << '\n';
continue;
}
ll answer = 0;
ll limit = k * (i * i + (n - i) * (n - i)) + 1;
for (int j = 0; j < limit; j++)
answer += pos[j] * neg[j] % mod;
ans[i] = (mod + answer - 1) % mod;
cout << ans[i] << '\n';
removeGroup(pos, n - i);
addGroup(neg, i);
}
return 0;
}
|
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
bool vis[200001] = {false};
vector<int> graph[200001];
void dfs(int node, int par , int& temp){
vis[node] = true;
temp++;
for(auto& c : graph[node]){
if(vis[c] == false)
dfs(c , node , temp);
}
}
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
unordered_set<int> s;
vector<pair<int,int>> v;
int lo = 0;
int hi = n-1;
while(lo < hi){
if(a[lo] != a[hi]){
v.push_back({a[lo],a[hi]});
s.insert(a[lo]);
s.insert(a[hi]);
}
lo++;
hi--;
}
if(v.size() == 0)
{
cout<<"0"<<endl;
}
else{
for(pair<int,int>& p : v){
graph[p.first].push_back(p.second);
graph[p.second].push_back(p.first);
}
long long int ans = 0;
for(auto& item : s){
if(vis[item] == false){
int temp = 0;
dfs(item , 0 , temp);
ans += max(0 , temp-1);
}
}
cout<<ans<<endl;
}
return 0;
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
// avoid unordered_map and unordered_set!!!
#include <vector>
#include <utility>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
using i32 = int32_t;
using pi64 = pair<i64, i64>;
#define vec vector
#define let const
#define DRi64(x) i64 x; cin >> x;
#define DRS(x) string x; cin >> x;
#define DRVi64(v, n) vec<i64> v(n); { for (i64 i = 0; i < n; ++i) { cin >> v[i]; }}
#define DRpi64(x) pi64 x; cin >> x.first >> x.second;
#ifdef DEBUG
#define P(x) cerr << x << "\n"
#else
#define P(x)
#endif
constexpr i64 MAXN = 2*100*1000LL+5LL;
constexpr i64 MOD = 1000000007LL;
constexpr i64 INF64 = MOD * MOD;
template<bool one_indexed>
struct union_find_t
{
union_find_t(const i64 N)
: parents_(N + one_indexed), sizes_(N + one_indexed, 1)
{
iota(parents_.begin(), parents_.end(), 0LL);
}
i64 Find(const i64 x)
{
if (parents_[x] != x)
{
parents_[x] = Find(parents_[x]);
}
return parents_[x];
}
void Union(const i64 a, const i64 b)
{
let i64 r_a = Find(a);
let i64 r_b = Find(b);
if (r_a == r_b)
{
return;
}
if (sizes_[r_a] < sizes_[r_b])
{
Union(r_b, r_a);
return;
}
sizes_[r_a] += sizes_[r_b];
parents_[r_b] = r_a;
}
vec<i64> parents_;
vec<i64> sizes_;
};
int
main()
{
ios_base::sync_with_stdio(false);
// fast io: see 1423K
cin.tie(nullptr);
cout.tie(nullptr);
DRi64(N);
DRVi64(A, N);
union_find_t<true> uf(MAXN);
set<i64> distinct;
for (i64 i = 0; i < N / 2; ++i)
{
if (A[i] == A[N - 1 - i])
{
continue;
}
uf.Union(A[i], A[N - 1 - i]);
distinct.insert(A[i]);
distinct.insert(A[N - 1 - i]);
}
i64 acc = 0;
set<i64> roots_seen;
for (let i64 x : distinct)
{
let i64 r = uf.Find(x);
if (roots_seen.count(r))
{
continue;
}
roots_seen.insert(r);
acc += uf.sizes_[r] - 1;
}
cout << acc << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=int(b)-1;i>=int(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
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; }
void Main()
{
int h, w;
cin >> h >> w;
vector<string> a(h);
rep(i, h) cin >> a[i];
vector<vector<int>> d(h, vector<int>(w));
rrep(i, h) {
rrep(j, w) {
int t;
if (i == h-1 && j == w-1){
t = 0;
}else if ((i+j)%2 != 0){
t = 100000000;
if (i<h-1) chmin(t, d[i+1][j]+(a[i+1][j]=='+'?-1:1));
if (j<w-1) chmin(t, d[i][j+1]+(a[i][j+1]=='+'?-1:1));
}else{
t = -100000000;
if (i<h-1) chmax(t, d[i+1][j]+(a[i+1][j]=='+'?1:-1));
if (j<w-1) chmax(t, d[i][j+1]+(a[i][j+1]=='+'?1:-1));
}
d[i][j] = t;
}
}
if (d[0][0] > 0) cout << "Takahashi" << endl;
if (d[0][0] < 0) cout << "Aoki" << endl;
if (d[0][0] == 0) cout << "Draw" << endl;
}
int main(int argc, char **argv)
{
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// int t; cin >> t; rep(i, t)
Main();
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define pb(a) push_back(a)
#define vll vector<ll>
#define loop(i, n) for(ll i=1;i<=n;i++)
#define loop0(i, n) for(ll i=0;i<n;i++)
#define in(i) scanf("%lld", &i);
#define out(i) printf("%lld", i)
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define mp(i, j) make_pair(i, j);
const ll mod = 1000000007;
const ll big = 2999999999999999999;
const ll small = -2999999999999999999;
void in2(ll& a, ll& b) { in(a); in(b); }
void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); }
void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; }
void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); }
ll h, w, dp[2001][2001][2];
ll grid[2001][2001];
ll solve(ll i, ll j, ll turn)
{
if(i == h-1 && j == w-1)
return 0;
if(dp[i][j][turn] != -1)
return dp[i][j][turn];
if(i == h-1)
{
ll c = solve(i, j+1, 1^turn) + (turn ? -1 : 1)*grid[i][j+1];
return dp[i][j][turn] = c;
}
if(j == w-1)
{
ll c = solve(i+1, j, 1^turn) + (turn ? -1 : 1)*grid[i+1][j];
return dp[i][j][turn] = c;
}
ll c1 = solve(i+1, j, 1^turn) + (turn ? -1 : 1)*grid[i+1][j], c2 = solve(i, j+1, 1^turn) + (turn ? -1 : 1)*grid[i][j+1];
if(turn == 0)
return dp[i][j][turn] = max(c1, c2);
else
return dp[i][j][turn] = min(c1, c2);
}
int main()
{
memset(dp, -1, sizeof(dp));
in2(h, w);
char c;
loop0(i, h)
{
loop0(j, w)
{
cin>>c;
if(c == '+')
grid[i][j] = 1;
else
grid[i][j] = -1;
}
}
ll ans = solve(0, 0, 0);
if(ans > 0)
cout<<"Takahashi\n";
else if(ans < 0)
cout<<"Aoki\n";
else
cout<<"Draw\n";
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef pair<int,int> pii;
#define lll int64_t //need to use g++ -std=c++11 or more ...
#define pb push_back
#define For(i,n) for(int i=0;i<n;i++)
#define fi first
#define se second
#define all(x) x.begin(), x.end()
template<typename T>
void enter(vector<T>& a, int n){
For(i,n) cin >> a[i];
}
template<typename T>
void enter(T a[], int n){
For(i,n) cin >> a[i];
}
template<typename T>
void enter(vector<T>& a){
For(i,a.size()) cin >> a[i];
}
template<typename T>
void print(vector<T>& a){
For(i,a.size()) cout<<a[i]<<' ';
cout << endl;
}
template<typename T>
void print(T a[], int n){
For(i,n) cout<<a[i]<<' ';
cout << endl;
}
template<typename T>
void printRev(vector<T>& a){
For(i,a.size()) cout<<a[a.size() - 1 - i]<<' ';
cout << endl;
}
bool check(int a, int b, int c, int d){
return (a+b == c+d) || (a-b==c-d) || (abs(a-c)+abs(b-d)<=3);
}
void solve(){
int a,b,c,d;
cin >> a >> b >> c >> d;
c -= a; d-=b;
a = 0; b = 0;
c = abs(c); d = abs(d);
if(a == c && b == d){
cout << 0;
return;
}
else if(check(a,b,c,d)){
cout << 1;
return;
}
else if(c + d <= 6 || c + d <= 3 || abs(c-d) <=3 || (c*1ll+d*1ll)%2 == 0){
cout << 2;
return;
}
cout << 3;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t=1;
//cin >> t;
while(t--) solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
void fastio() {
cin.tie(nullptr);
cin.sync_with_stdio(false);
}
using LL = long long;
using LD = long double;
const LL MOD = 1e9+7;
const LL INF = 1e18;
const LL N = 3e5+1;
const LD eps = 1e-9;
#define S second
#define F first
#define vt vector
int main () {
fastio();
LL r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
LL ans = INF;
if (r1 == r2 && c1 == c2) {
ans = 0;
}
else if (abs(r1-r2)+ abs(c1-c2) <= 3) {
ans = 1;
}
else {
for (LL i = -3; i <= 3; ++i) {
for (LL j = -3; j <= 3; ++j) {
if (abs(i) + abs(j) <= 3) {
LL a1 = 1;
if (i == 0 && j == 0) {
a1 = 0; // same point
}
if ((r1+i+c1+j) == (r2+c2)) {
a1++;
}
else if ((r1+i-c1-j) == (r2-c2)) {
a1++;
}
else {
if (abs((r1+i+c1+j)%2) != abs((r2+c2)%2)) {
a1 += 3;
}
else {
a1 += 2;
}
}
ans = min(ans, a1);
}
}
}
}
if (abs(r1-r2)+abs(c1-c2) <= 6) {
ans = min(ans, 2LL);
}
cout << ans << "\n";
//cout << (-1)%2 << "?";
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>
#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
cin, n, m;
vector<ll> h(n), w(m);
REP(i, 0, n) {
cin, h[i];
}
REP(i, 0, m) {
cin, w[i];
}
sort(h.begin(), h.end());
sort(w.begin(), w.end());
int half = (n + 1) / 2;
vector<ll> hodd(half + 1, 0), heven(half + 1, 0);
REP(i, 0, half) {
hodd[i + 1] = hodd[i] + h[i * 2];
if (i + 1 != half) {
heven[i + 1] = heven[i] + h[i * 2 + 1];
}
}
ll ans = 1LL << 60;
auto calc = [&](int pos, ll ww) -> ll {
if (pos % 2) {
pos /= 2;
return -hodd[pos + 1] + hodd[half] - hodd[pos + 1] + heven[pos] - heven[half - 1] + heven[pos] + ww;
} else {
pos /= 2;
return -hodd[pos] + hodd[half] - hodd[pos] - heven[half - 1] + heven[pos] + heven[pos] - ww;
}
};
REP(i, 0, m) {
int pos = lower_bound(h.begin(), h.end(), w[i]) - h.begin();
chmin(ans, calc(pos, w[i]));
}
print(ans);
return 0;
}
| #pragma region
#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define repc(i, m, n) for (int i = m; i < n; i++)
#define reprc(i, m, n) for (int i = m; i > n; i--)
#define all(v) v.begin(), v.end()
#define INF 1e9
#define EPS 1e-10
using namespace std;
typedef long long ll;
#pragma endregion
int main()
{
int a,b,c;
cin >> a >> b >> c;
cout << a + b + c - min(a, min(b, c)) << endl;
} |
//----AUTHOR: eeshan_deo_iitkgp----/
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define ppb pop_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vb vector<bool>
#define vd vector<double>
#define vll vector<ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define vs vector<string>
#define vc vector<char>
#define vvi vector<vi>
#define vvb vector<vb>
#define vvd vector<vd>
#define vvll vector<vll>
#define vvc vector<vc>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define w(x) \
int x; \
cin >> x; \
for (ll tc = 1; tc <= x; tc++)
#define in(i, x, y) for (ll i = x; i < y; i++)
#define rin(i, x, y) for (ll i = x; i >= y; i--)
#define all(a) begin(a), end(a)
#define rall(a) rbegin(a), rend(a)
#define lb lower_bound
#define ub upper_bound
ll fxp(ll a, ll b) {
assert(b >= 0);
a %= mod;
ll res = 1;
while (b > 0) {
if (b % 2 == 1)
res = (res * a) % mod;
a = (a * a) % mod;
b /= 2;
}
return res % mod;
}
inline ll minv(ll x) { return fxp(x, mod - 2); }
void c_p_c() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const int mXn = 1;
const ll inf = 1e18;
signed main() {
c_p_c();
int n;
cin >> n;
int val = n + (n * 8) / 100;
if (val < 206)
cout << "Yay!";
else if (val > 206)
cout << ":(";
else
cout << "so-so";
} | #include <bits/stdc++.h>
using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;
#define ALL(x) x.begin(), x.end()
constexpr llong mod = 1e9+7;
constexpr llong inf = mod * mod;
int main() {
llong N;
cin >> N;
N *= 1.08;
cout << (N < 206 ? "Yay!": N == 206 ? "so-so": ":(") << endl;
return 0;
} |
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define pll pair<LL,LL>
#define INF 0x7f3f3f3f
using namespace std;
const int MAX = 2e5+10;
double f[110][110][110];
int main()
{
//freopen("in.txt","r",stdin);
int Cas = 1;
//scanf("%d",&Cas);
while(Cas--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
f[a][b][c]=1;
for (int i = a;i < 100;i++)
for (int j = b;j < 100;j++)
for (int k = c;k < 100;k++)
{
if (i>0)
f[i+1][j][k] += f[i][j][k]*1.0*i/(i+j+k);
if (j>0)
f[i][j+1][k] += f[i][j][k]*1.0*j/(i+j+k);
if (k>0)
f[i][j][k+1] += f[i][j][k]*1.0*k/(i+j+k);
}
double p=0;
for (int i = 0;i <= 100;i++)
for (int j = 0;j <= 100;j++)
for (int k = 0;k <= 100;k++)
if (((i==100)+(j==100)+(k==100)==1) && f[i][j][k] != 0)
p+=f[i][j][k]*(i+j+k-a-b-c);
printf("%.9f\n",p);
}
return 0;
}
| #include<bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define int long long
#define pb push_back
#define ins insert
#define rs resize
#define br break;
#define cont continue;
#define vi vector<int>
#define vll vector<long long>
#define vvi vector<vector<int>>
#define si set<ll>
#define sll set<long long>
#define pii pair<ll,ll>
#define pll pair<long long, long long>
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);
#define vec vector
#define cend cout<<endl;
#define sc second
#define fr first
#define retz return 0;
#define ll_MAX (ll)(1e9*1e9)
#define ll_MIN (ll)(-1e9*1e9)
#define lb lower_bound
#define ub upper_bound
#define rep(i,a,b) for(int i=a; i<=b; i++)
#define repd(i,a,b) for(int i=a; i>=b; i--)
#define ps(arr, n) for(int i=1; i<n; i++)arr[i]+=arr[i-1];
#define inp(arr, n)for(int i=0; i<n; i++)cin>>arr[i];
#define out(arr, n)for(int i=0; i<n; i++)cout<<arr[i];
#define inp1(arr,n,m)for(int i=0; i<n; i++)for(int j=0;j<m;j++)cin>>arr[i][j];
#define out1(arr,n,m)for(int i=0; i<n; i++)for(int j=0;j<m;j++)cin>>arr[i][j];
#define loop(q) for(auto it:q)
#define loop1(q) for(auto&it:q)
#define db1 cout << "*" << endl
#define db2 cout << "YES" << endl
using namespace std;
using namespace __gnu_pbds;
#define oset(X) tree<X,null_type,less<X>, rb_tree_tag, tree_order_statistics_node_update>
#define roset(X) tree<X,null_type,greater<X>, rb_tree_tag, tree_order_statistics_node_update>
#define okey order_of_key //lower_bound_set
#define ofind find_by_order
#define cyes cout << "Yes" << endl;
#define cno cout << "No" << endl;
#define mod (long long)1000000007
#define mod2 (long long)998244353
#define INT_MAX1 900000000
#define MAXN 2000000
#define INF INT_MAX
double dp[101][101][101];
bool v[101][101][101];
double f1(int a, int b, int c)
{
if(a == 100 || b == 100 || c == 100)
return 0;
if(v[a][b][c])
return dp[a][b][c];
v[a][b][c] = 1;
int tot = a+b+c;
return dp[a][b][c] = 1 + (a*f1(a+1, b, c) + b*f1(a, b+1, c) + c*f1(a, b, c+1))/tot;
}
void solve()
{
int a, b, c;
cin >> a >> b >> c;
cout << setprecision(25) << f1(a, b, c) << endl;
}
signed main()
{
speed;
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
|
#include <iostream>
using namespace std;
long long int N;
long long int A[100000];
long long int P[100000];
long long int X[100000];
long long int cost_min = 1000000000;
void solve() {
cin >> N;
for (int i = 0; i < N; ++i) {
cin >> A[i] >> P[i] >> X[i];
long long int zaiko = X[i] - A[i];
if (zaiko > 0) {
if (cost_min > P[i]) {
cost_min = P[i];
}
}
}
if (cost_min == 1000000000) {
cout << -1 << endl;
} else {
cout << cost_min << endl;
}
}
int main() {
solve();
return 0;
} | #include<bits/stdc++.h>
namespace imzzy{
#define endl '\n'
#define rgi register int
typedef long long ll;
class fastin{private:int _ch,_f;
public:inline fastin&operator>>(char&c){c=getchar();return*this;}
template<typename _Tp>inline fastin&operator>>(_Tp&_x){
_x=0;while(!isdigit(_ch))_f|=(_ch==45),_ch=getchar();
while(isdigit(_ch))_x=(_x<<1)+(_x<<3)+(_ch^48),_ch=getchar();
_f&&(_x=-_x,_f=0);return*this;}fastin(){_ch=_f=0;}
}fin;class fastout{private:int _num[32],_head;
public:inline fastout&operator<<(char c){putchar(c);return*this;}
template<typename _Tp> inline fastout&operator<<(_Tp _x){
_Tp _k;if(_x==0){putchar('0');return *this;}if(_x<0)putchar('-'),_x=-_x;
while(_x>0)_k=_x/10,++_head,_num[_head]=(_x-(_k<<1)-(_k<<3))^48,_x=_k;
while(_head>0)putchar(_num[_head]),--_head;return*this;}fastout(){_head=0;}
}fout;inline void P_INIT(){
#ifdef D_STDOUT_UNBUFFERED
setbuf(stdout,NULL);
#endif
}}using namespace imzzy;
// ----------------------------
// #define int ll
// using namespace std;
const int mod=1000000007,inf=1201201201;
const int maxn=200004;
std::string s;
signed main()
{P_INIT();
int t;
fin>>t;
while(t--) {
std::cin>>s;
if("atcoder"<s) {puts("0"); continue;}
int ans=-1;
for(rgi i=1,len=s.length();i<len&&ans==-1;++i) {
if(s[i]>'a') ans=i;
if(s[i]>'t') ans=i-1;
} fout<<ans<<endl;
}
return 0;
}
// ----------------------------
// by imzzy |
#include<iostream>
using namespace std;
int main() {
int a,b;cin>>a>>b;
cout << a*2+100-b<<endl;
return 0;
} | #include "algorithm"
#include "iostream"
#include "numeric"
#include "iomanip"
#include "cstring"
#include "math.h"
#include "bitset"
#include "string"
#include "vector"
#include "random"
#include "chrono"
#include "ctime"
#include "queue"
#include "stack"
#include "map"
#include "set"
#include "ext/pb_ds/assoc_container.hpp" // Common file
#include "ext/pb_ds/tree_policy.hpp" // Including tree_order_statistics_node_update
#include "ext/pb_ds/detail/standard_policies.hpp"
using namespace std;
using namespace __gnu_pbds;
#define f first
#define lgn 25
#define endl '\n'
#define sc second
#define pb push_back
#define N (int)2e5+5
#define PI acos(-1.0)
#define int long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define eb emplace_back
#define mii map<int,int>
#define vpii vector<pii>
#define pii pair<int,int>
#define pq priority_queue
#define BLOCK (int)sqrt(N)
#define test(x) while(x--)
#define all(x) begin(x),end(x)
#define allr(x) x.rbegin(),x.rend()
#define fo(i,a,n) for(int i=a;i<n;i++)
#define rfo(i,n,a) for(int i=n;i>=a;i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define time() cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n"
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update >
OS ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
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...);
}
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
int n,m,k,q;
string s;
vi adj[N];
int vis[N],par[N],a[N];
void go()
{
cin >> n >> m;
cout << 2 * n + 100 - m << endl;
}
int32_t main()
{
FAST;
int t=1;
// cin>>t;
test(t) go();
}
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <string>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <numeric>
#define inf 0x3f3f3f3f
#define MAXN 50005
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
int mod=1e9+7;
ll gcd(ll a,ll b){
if (b==0) return a;
if (a%b==0) return b;
return gcd(b,a%b);
}
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(false);
cin.tie(0);
string str;
cin>>str;
deque<char>q;
int f=0;
for(int i=0;i<str.size();i++){
if(str[i]!='R'){
if(f%2==0) q.push_back(str[i]);
else q.push_front(str[i]);
}else f++;
}
string ans;
while(!q.empty()){
if(!ans.empty()&&ans.back()==q.front()) ans.pop_back();
else ans+=q.front();
q.pop_front();
}
if(f%2) reverse(ans.begin(),ans.end());
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
#define ff first
#define ss second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const v(T)&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
#define has(x,y) ((x).find(y) != (x).end())
#define int ll
int lcm(int a , int b){
return (a/__gcd(a,b)*b);
}
bool is_prime(int n){
for(int i = 2 ; i <= sqrt(n) ; i++){
if(n%i == 0)
return false;
}
return true;
}
const int dx[4] = {1,0,-1,0} , dy[4] = {0,1,0,-1};
int cases = 0;
bool compare(pair<vector<int>,int> &a, pair<vector<int>,int> &b){
return a.first.size() < b.first.size();
}
int countDivisors(int n)
{
int cnt = 0;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
cnt++;
else
cnt = cnt + 2;
}
}
return cnt;
}
bool divisorsSame(int n){
return (n-2)%4 == 0;
}
int power(int x,int n,int M)
{ if(n < 0)
return 0;
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
int getSum(int l){
int sum = 0;
while(l){
sum += l;
l/=10;
}
return sum;
}
struct Solver {
void solve(){
string s;
cin >> s;
deque<char> d;
bool rev = false;
for(char c : s){
if(c == 'R'){
rev = !rev;
}
else{
if(rev)
d.push_front(c);
else
d.push_back(c);
}
}
string ans;
for(char c : d){
if(ans.size() != 0 && ans.back() == c){
ans.pop_back();
}
else{
ans+=c;
}
}
if(rev)
reverse(ans.begin(),ans.end());
cout<<ans<<"\n";
}
};
int32_t main() {
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
cout<<fixed<<setprecision(10);
int ts = 1;
// cin >> ts;
rrep(ti,ts) {
cases++;
Solver solver;
solver.solve();
}
return 0;
}
// 1 - 30
// 1 - 6 - 1
//
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
using ll = long long;
//<< fixed << setprecision(n) 小数第n位まで表示
template <class T> void chmin(T& a, T b) {
if(a > b) {
a = b;
}
}
template <class T> void chmax(T& a, T b) {
if(a < b) {
a = b;
}
}
int main() {
int N, K;
cin >> N >> K;
vector<pair<ll,int>> data(N);
rep(i, N) {
cin >> data[i].first >> data[i].second;
}
sort(data.begin(),data.end());
//rep(i, N) cout << data[i].second << endl;
ll ans = K;
rep(i, N) {
if(data[i].first <= ans) {
ans += data[i].second;
//cout << "ok" << endl;
}
//cout << data[i].first << endl;
}
cout << ans << endl;
return 0;
} | /*
Mbak Sana Dong
Mbak Chaeyoung Dong
Mbak Seulgi Dong
*/
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
int n,i,j,k,x,y,ans,temp,curr,m;
int tc;
string s;
cin >> n >> s;
s = " " + s;
ll seulgi = 0;
map<pair<int,int>, int> mp;
mp[{0, 0}] = 1;
x = 0, y = 0;
for(i = 1; i <= n; i++) {
char c = s[i];
if(c == 'A') x++;
if(c == 'T') x--;
if(c == 'G') y++;
if(c == 'C') y--;
seulgi += mp[{x, y}];
mp[{x, y}]++;
}
cout << seulgi << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int inf = 1001001001;
ll INF = 1001001001001001001;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define rep(i, n) FOR(i, 0, n)
#define all(x) x.begin(), x.end()
#define pb push_back
#define pf push_front
ll MOD = 1000000007;
using P = pair<ll, ll>;
int main() {
int H, W;cin >> H >> W;
string s[15];
rep(i, H) cin >> s[i];
int ans = 0;
rep(i, H-1) rep(j, W-1) {
int cnt = 0;
if (s[i][j]=='#') cnt++;
if (s[i][j+1]=='#') cnt++;
if (s[i+1][j]=='#') cnt++;
if (s[i+1][j+1]=='#') cnt++;
if (cnt == 1||cnt == 3) ans++;
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int h,w,ans;
cin>>h>>w;
ans=(h-1)*(w-1);
vector<vector<char>> a(h,vector<char>(w));
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
cin>>a.at(i).at(j);
}
}
for(int i=0;i<h-1;i++)
{
for(int j=0;j<w-1;j++)
{
int black=0;
if(a.at(i).at(j)=='#')
black++;
if(a.at(i+1).at(j)=='#')
black++;
if(a.at(i).at(j+1)=='#')
black++;
if(a.at(i+1).at(j+1)=='#')
black++;
if(black==0||black==2||black==4)
ans--;
}
}
cout<<ans<<endl;
} |
/*
This too shall pass
*/
#pragma GCC optimize "trapv"
#include <bits/stdc++.h>
using namespace std;
#define MOD (int)(1e9+7)
#define MOD1 998244353
#define pii pair <int, int>
#define INF 2e18
#define int long long
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("in19.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int T=1;
// cin >> T;
for(int tt = 1; tt <= T; tt++){
// cout << "Case #" << tt << ": ";
int n; cin >> n;
int a[n+1];
for(int i = 1; i <= n; i++) cin >> a[i];
int b[n+1];
for(int i = 1; i <= n; i++) cin >> b[i];
int c[n+1];
for(int i = 1; i <= n; i++) cin >> c[i];
map<int, int> m;
for(int i = 1; i <= n; i++){
m[b[c[i]]]++;
}
int ans = 0;
for(int i = 1; i <= n; i++){
ans += m[a[i]];
}
cout << ans;
cout << '\n';
}
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//optimal subsequences-2 technocup round-3 2020
using namespace std;
using namespace __gnu_pbds;
const int N=2e5+10;
//#define d double
#define F first
#define S second
#define ll long long int
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(6)
const int M=1e9+7;
//const int M=998244353;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
//#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
//int MOD=998244353;
const int MOD=1e9+7;
#define endl "\n"
const double PI = 3.141592653589793238460;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
//ll d1=304933,d2=4395853;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//bitset<N>bs;
//bitset<512>bb[512];
//vector<vector<int>>arr;
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r" , stdin);
//freopen("output.txt", "w", stdout);
//#endif
ll power(ll a,ll b)
{
ll res=1;
if(a==1)return 1;
for(;b>0;b>>=1)
{
if(b&1)res=(res*a);
if(res>MOD)res%=MOD;
a=(a*a);
if(a>MOD)a=a%MOD;
}
return res;
}
void solve()
{
int n;cin>>n;
int a[n+1],b[n+1],c[n+1];
f(n)cin>>a[i+1];
f(n)cin>>b[i+1];
f(n)cin>>c[i+1];
map<int,ll>m;
f(n)m[a[i+1]]++;
ll ans=0;
f(n)
{
int id=c[i+1];
int x=b[id];
ans+=m[x];
}
cout<<ans;
}
int main()
{
FastIO;
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)
{
//if(i!=t)cout<<endl;
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for(int i=0; i<n; i++)
//global parameter
int main(void){
int a, b, c;
cin >> a >> b >> c;
a = 7 - a;
b = 7 - b;
c = 7 - c;
cout << a+b+c << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
cout << 21-a-b-c << endl;
} |
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int N=2e5+100;
int n,stk[N],top; char str[N];
int main() {
scanf("%d%s",&n,str+1);
for(int i=1;i<=n;++i) {
if(top >= 2 && str[stk[top-1]] == 'f' && str[stk[top]] == 'o' && str[i] == 'x')
top -= 2;
else stk[++top]=i;
}
cout<<top;
return 0;
} | #include<stdio.h>
#include<iostream>
int main() {
int x;
int y;
int z;
std::cin >> x;
std::cin >> y;
if(x==y){
z = x;
}
else{
z = 3 - x - y;
}
std::cout << z;
return 0;
} |
#include <bits/stdc++.h>
// 1. dp
typedef long double ld;
#define int long long
#define gcd __gcd
#define endl "\n"
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define mod2 998244353
#define maxe *max_element
#define mine *min_element
#define inf 1e18
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define sz(x) (int)(x).size()
#define mk make_pair
#define deci(x, y) fixed<<setprecision(y)<<x
#define w(t) int t; cin>>t; while(t--)
#define nitin ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define PI 3.141592653589793238
using namespace std;
void solve() {
int n,m;
cin>>n>>m;
int a[n],b[m];
for(int i=0;i<n;i++)
cin>>a[i];
for(int j=0;j<m;j++)
cin>>b[j];
int dp[n][m];
memset(dp,0,sizeof dp);
if(a[0]!=b[0])
dp[0][0]=1;
for(int i=1;i<n;i++)
{
if(a[i]==b[0])
{
dp[i][0]=i;
}
else{
dp[i][0]=i+1;
}
}
for(int i=1;i<m;i++)
{
if(b[i]==a[0])
{
dp[0][i]=i;
}
else{
dp[0][i]=i+1;
}
}
for(int i=1;i<n;i++)
{
for(int j=1;j<m;j++)
{
if(a[i]==b[j])
{
dp[i][j]=min({dp[i-1][j-1],dp[i-1][j]+1,dp[i][j-1]+1});
}
else{
dp[i][j]=min({dp[i-1][j-1],dp[i-1][j],dp[i][j-1]})+1;
}
}
}
int ans=inf;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
dp[i][j]+=(n+m-(i+1)-(j+1));
ans=min(ans,dp[i][j]);
}
}
cout<<ans<<endl;
}
int32_t main() {
nitin;
solve();
}
| //#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
namespace fastIO {
template<class T>
inline void read(T &x) {
x=0;
bool fu=0;
char ch=0;
while(ch>'9'||ch<'0') {
ch=getchar();
if(ch=='-')fu=1;
}
while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
if(fu)x=-x;
}
inline int read() {
int x=0;
bool fu=0;
char ch=0;
while(ch>'9'||ch<'0') {
ch=getchar();
if(ch=='-')fu=1;
}
while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
return fu?-x:x;
}
char _n_u_m_[40];
template<class T>
inline void write(T x ) {
if(x==0){
putchar('0');
return;
}
T tmp = x > 0 ? x : -x ;
if( x < 0 ) putchar('-') ;
register int cnt = 0 ;
while( tmp > 0 ) {
_n_u_m_[ cnt ++ ] = tmp % 10 + '0' ;
tmp /= 10 ;
}
while( cnt > 0 ) putchar(_n_u_m_[ -- cnt ]) ;
}
}
using namespace fastIO;
int n,m,a[1002],b[1002],dp[1002][1002];
int main() {
cin>>n>>m;
F(i,1,n)read(a[i]);
F(i,1,m)read(b[i]);
memset(dp,0x3f,sizeof(dp));
F(i,1,n)dp[i][0]=i;
F(j,1,m)dp[0][j]=j;
dp[0][0]=0;
F(i,1,n){
F(j,1,m){
if(a[i]==b[j])dp[i][j]=dp[i-1][j-1];
else dp[i][j]=dp[i-1][j-1]+1;
dp[i][j]=min(dp[i][j],dp[i-1][j]+1);
dp[i][j]=min(dp[i][j],dp[i][j-1]+1);
}
}cout<<dp[n][m];
return 0;
}
|
#include <cstdio>
const long long mod = 998244353;
int w[105];
long long f[105][105][20005];
int main(){
int n;
scanf("%d",&n);
for(int i = 1; i <= n; i++) scanf("%d",&w[i]);
f[0][0][10000] = 1;
long long ans = 0;
for(int i = 1; i <= n; i++){
int u = w[i];
for(int j = 0; j <= i; j++){
for(int k = 0; k <= 20000; k++){
f[i][j][k] = 0;
if(j>0 && k-u>=0){
long long add = f[i-1][j-1][k-u]*(long long)(j)%mod;
f[i][j][k] = (f[i][j][k] + add)%mod;
}
if(k+u<=20000){
int b = i-j-1;
long long add = f[i-1][j][k+u]*(long long)(b+1)%mod;
f[i][j][k] = (f[i][j][k] + add)%mod;
}
if(i==n && k==10000) ans = (ans + f[i][j][k])%mod;
}
}
}
printf("%lld\n",ans);
return 0;
}
| //clear adj and visited vector declared globally after each test case
//check for long long overflow
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Incase of close mle change language to c++17 or c++14
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 998244353ll
#define lld long double
#define mii map<int, int>
#define pii pair<int, int>
#define ll long long
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
int g=__gcd(a, b);
return a/g*b;
}
int power(int a, int b, int p)
{
if(a==0)
return 0;
int res=1;
a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
int dp[105][10005];
int fact[N],inv[N];
void pre()
{
fact[0]=1;
inv[0]=1;
for(int i=1;i<N;i++)
fact[i]=(i*fact[i-1])%mod;
for(int i=1;i<N;i++)
inv[i]=power(fact[i], mod-2, mod);
}
int32_t main()
{
IOS;
pre();
int n;
cin>>n;
int a[n+1], s=0;
rep(i,1,n+1)
cin>>a[i], s+=a[i];
if(s&1)
{
cout<<0;
return 0;
}
dp[0][0]=1;
int lim=10004;
rep(i,1,n+1)
{
for(int j=n;j>0;j--)
{
for(int k=lim;k>=a[i];k--)
{
dp[j][k]+=dp[j-1][k-a[i]];
if(dp[j][k]>=mod)
dp[j][k]-=mod;
}
}
}
int ans=0;
rep(i,1,n+1)
{
int op=n-i;
if(op<i)
break;
if(i!=op)
ans+=(((dp[i][s/2]*fact[i])%mod)*fact[op]*2ll)%mod;
else
ans+=(((dp[i][s/2]*fact[i])%mod)*fact[op])%mod;
ans%=mod;
}
cout<<ans;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int sum=0;
int a[n+1][k+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++){
sum+=(i*100)+j;
}
}
cout<<sum;
}
| #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;
int main(){
int x, y;
cin >> x >> y;
if (x == y) cout << x << endl;
else {
cout << 3-x-y << endl;
}
return 0;
} |
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif
namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); }
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
template <class Con>
string concat(const Con &c, const string &spr){
stringstream ss;
typename Con::const_iterator it = c.begin(), en = c.end();
bool fst = true;
for(; it != en; ++it){
if(!fst){ ss << spr; }
fst = false;
ss << *it;
}
return ss.str();
}
template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
vector<typename Con::value_type> ret;
typename Con::const_iterator it = c.begin(), en = c.end();
for(; it != en; ++it){
if(f(*it)){
ret.emplace_back(*it);
}
}
return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
vector<decltype(f(*c.begin()))> ret;
ret.reserve(c.size());
for(const auto &x: c){
ret.emplace_back(f(x));
}
return ret;
}
#endif
#if __cplusplus >= 201402L
#define lambda(e) ([&](const auto &_){ return (e); })
#define lambda2(e) ([&](const auto &_a, const auto &_b){ return (e); })
#endif
#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()
#define tget(t,i) get<i>(t)
#define MOD 1000000007LL
#define EPS 1e-8
void mainmain(){
const LL INF = 1LL << 61;
int n;
LL x;
cin >> n >> x;
vll as(n);
for(LL &a : as){ cin >> a; }
LL ans = INF;
for(int k = 1; k <= n; ++k){
VV(LL) dp;
initvv(dp, k + 1, k, -INF);
dp[0][0] = 0;
for(LL a : as){
for(int i = k - 1; i >= 0; --i)
for(int j = 0; j < k; ++j){
if(dp[i][j] >= 0){
chmax(dp[i + 1][(j + a) % k], dp[i][j] + a);
}
}
}
LL t = dp[k][x % k];
if(t > 0){
chmin(ans, (x - t) / k);
}
}
cout << ans << endl;
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(4);
mainmain();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define mp make_pair
int p = 998244353;
int mul(int a, int b) {
return (1LL * a * b) % p;
}
int add(int a, int b) {
int s = (a+b);
if (s>=p) s-=p;
return s;
}
int sub(int a, int b) {
int s = (a+p-b);
if (s>=p) s-=p;
return s;
}
int po(int a, int deg)
{
if (deg==0) return 1;
if (deg%2==1) return mul(a, po(a, deg-1));
int t = po(a, deg/2);
return mul(t, t);
}
int inv(int n)
{
return po(n, p-2);
}
mt19937 rnd(time(0));
/*
const int N = 1200000;
vector<int> facs(N), invfacs(N);
void init()
{
facs[0] = 1;
for (int i = 1; i<N; i++) facs[i] = mul(facs[i-1], i);
invfacs[N-1] = inv(facs[N-1]);
for (int i = N-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}
int C(int n, int k)
{
if (n<k) return 0;
if (n<0 || k<0) return 0;
return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}
*/
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n, k;
cin>>n>>k>>p;
if (n==1)
{
cout<<k; return 0;
}
vector<vector<int>> dp(n);
for (int i = 1; i<n; i++) dp[i].resize(k*i*(i+1)/2 + 1);
for (int i = 0; i<=k; i++) dp[1][i] = 1;
for (int step = 2; step<n; step++)
{
for (int i = 0; i<dp[step].size(); i++)
{
if (i>=step) dp[step][i] = dp[step][i-step];
if (i<dp[step-1].size()) dp[step][i]= add(dp[step][i], dp[step-1][i]);
if (i-(k+1)*step >= 0 && i-(k+1)*step <dp[step-1].size()) dp[step][i] = sub(dp[step][i], dp[step-1][i-(k+1)*step]);
}
}
for (int x = 1; x<=n; x++)
{
if (x==1 || x==n) {cout<<k<<endl;
continue;}
int res = 0;
for (int j = 0; j<min( (int)(dp[x-1].size()), (int)(dp[n-x].size()) ); j++) res = add(res, mul(dp[x-1][j], dp[n-x][j]));
res = mul(res, k+1);
cout<<sub(res, 1)<<endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using LD = long double;
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> void coutall(T v) { if(v.empty()){cout << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; }
void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; }
void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; }
void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; }
ll myceil(ll a, ll b) { return a >= 0 ? (a+b-1)/b : -((-a)/b); }
ll myfloor(ll a, ll b) { return a >= 0 ? a/b : -myceil(-a, b); }
void Main(){
int n; cin >> n;
LD ans = 0;
rep3(i, 1, n){
ans += (LD)1/i;
}
ans *= n;
cout << ans - 1 << endl;
return;
}
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
vector<double> dp(n + 1, 0);
dp[1] = 0;
for(ll i = 2; i <= n; i++)
{
dp[i] = dp[i - 1] + 1.0 * n / (n - i + 1);
}
printf("%.11f\n", dp[n]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
// 2D Vector: vector<vector<int>> nums(n,vector<int>(m,0));
int main(){
int n;
cin>>n;
vector<int>a;
vector<int>b;
for(int i=0;i<n;i++){
int q;
cin>>q;
a.pb(q);
}
for(int i=0;i<n;i++){
int q;
cin>>q;
b.pb(q);
}
int ans=0;
for(int i=0;i<n;i++){
ans+=(a[i]*b[i]);
}
if(ans==0){
cout<<"Yes"<<"\n";
}
else{
cout<<"No"<<"\n";
}
}
| #include <bits/stdc++.h>
using namespace std;
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
#define setp(x) fixed << setprecision(x)
#define ordered_set tree<pair<long double, int> , null_type,less<pair<long double, int> >, rb_tree_tag,tree_order_statistics_node_update>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
const int inf = int(1e9);
const ll INF = ll(1e16);
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);
ll bp(ll a, ll n, ll cm){
ll r = 1;
while(n){
if(n & 1) r = r * a % cm;
a = a * a % cm;
n >>= 1;
}
return r;
}
template<typename T_vector> // neal
void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {
if (start < 0) start = 0;
if (end < 0) end = int(v.size());
for (int i = start; i < end; i++)
cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}
void solve(){
int n;
cin >> n;
ll a[n], b[n];
ll S = 0;
rep(i, n) cin >> a[i];
rep(i, n) {
cin >> b[i];
S += b[i] * a[i];
}
cout << (S == 0 ? "Yes" : "No");
}
int main()
{
// freopen("input.txt", "r", stdin)
// freopen("output.txt", "w", stdout);
Red;
int T = 1;
// cin >> T;
for(int i = 1; i <= T; ++i){
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
int main()
{
int N;
cin >> N;
int T[N];
rep(i, 0, N) cin >> T[i];
int tot = accumulate(T, T + N, 0);
bool dp[N + 1][900011];
fill((int *)dp, (int *)dp + sizeof(dp) / sizeof(int), false);
dp[0][0] = true;
rep(i, 0, N)
{
rep(j, 0, 100011)
{
if (dp[i][j])
{
dp[i + 1][j] = true;
dp[i + 1][j + T[i]] = true;
}
}
}
int minv = INT_MAX;
rep(j, 0, 100011)
{
if (dp[N][j])
{
minv = min(minv, abs(tot - j * 2));
}
}
/*
vector<int> vec;
vec.push_back(0);
rep(i,0,N){
int l = vec.size();
rep(j,0,l){
// cout <<i << " a " << a << " T " << T[i] << endl;
vec.push_back(vec[j]+T[i]);
}
}
*/
//for(auto a:vec){
/*rep(i, 0, vec.size())
{
// cout << minv << " tot " << tot << " a " << a << endl;
minv = min(minv, abs(tot - vec[i] * 2));
}
*/
cout << (tot + minv) / 2 << endl;
}
| // fuml namshebaazi
#include <bits/stdc++.h>
#define endl "\n";
using namespace std;
typedef long long ll;
int dp[100005][105];
void solve()
{
int n;cin>>n;
vector<int> v(n);
int sum=0;
for(int i=0;i<n;i++) {
cin>>v[i];
sum+=v[i];
}
memset(dp,0,sizeof(dp));
for(int i=0;i<=n;i++) dp[0][i]=1;
for(int i=1;i<=sum;i++)
{
for(int j=1;j<=n;j++)
{
if(i>=v[j-1]) dp[i][j]=max(dp[i][j-1],dp[i-v[j-1]][j-1]);
dp[i][j]=max(dp[i][j],dp[i][j-1]);
}
}
int ans;
for(int i=sum/2;i<=sum;i++)
{
if(dp[i][n]==1)
{
ans=i;
break;
}
}
cout<<max(ans,sum-ans);
cout<<endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;t=1;
while(t--)
{
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s, t; cin >> s;
deque<char> deq;
bool rev = false;
for(int i = 0; i < (int)s.size(); ++i){
if(s[i] == 'R'){rev = !rev;}
else if(s[i] != 'R' && rev == false){deq.push_back(s[i]);}
else{deq.push_front(s[i]);}
}
if(rev == true){reverse(deq.begin(), deq.end());}
stack<char> stk;
char w;
if(deq.size() == 0){cout << endl; return(0);}
while(!deq.empty()){
w = deq.front(); deq.pop_front();
if(stk.empty() || stk.top() != w){stk.push(w);}
else{stk.pop();}
}
int loopnum = stk.size();
string ans;
for(int i = 0; i < loopnum; ++i){
ans.push_back(stk.top());
stk.pop();
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep2(i,k,n) for (int i = k; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
const ll INF = (ll)1e16;
void chmin(ll &a, ll b) { a = min(a, b); }
void chmax(ll &a, ll b) { a = max(a, b); }
void solve() {
int n;
cin >> n;
vector<string> S(n);
rep(i,n) cin >> S[i];
vector<ll> T(n+1), F(n+1);
T[0] = 1;
F[0] = 1;
rep2(i,1,n+1) {
if (S[i-1] == "OR") {
T[i] = (1ll<<(i+1)) - F[i-1];
} else {
T[i] = T[i-1];
}
F[i] = (1ll<<(i+1)) - T[i];
}
cout << T[n] << endl;
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// inline ll hhme(ll a, ll b){
// return 300005LL * a + b;
// }
// const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
// struct chash {
// int operator()(int x) const { return x ^ RANDOM; }
// };
//gp_hash_table<ll, ll, chash> mpp;
#define eb emplace_back
using ll = long long;
const int inf = 1e9+5;
const ll INF = 1e18+5;
const int nax = 2e5+500;
//#define int long long
bool ip(int n) {
for(int i=2;i<=sqrt(n);i++) {
if(n % i == 0) return 0;
}
return 1;
}
int32_t main() {
//freopen("g:/coding/cpp/input.txt","r",stdin);
//freopen("g:/coding/cpp/output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
map<int,int> occur;
for(int i=2;i<=n;i++) {
map<int,int> f;
int num = i;
for(int j=2;j<=i;j++) {
if(num == 1) break;
while(num > 1 && num % j == 0) {
num /= j;
f[j]++;
}
}
for(auto it:f) {
occur[it.first] = max(it.second, occur[it.first]);
}
}
ll ans = 1;
for(auto it:occur) {
ans *= pow(it.first, it.second);
}
ans++;
debug()<<imie(ans);
for(int i=2;i<=n;i++) assert(ans % i == 1);
cout << ans;
}
| #include<bits/stdc++.h>
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define fs first
#define sd second
#define rept(i,n) for(ll i=0;i<n;i++)
#define rrept(i,n) for(ll i=n-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define mpit map<ll,ll>::iterator
#define stit set<ll,ll>::iterator
#define quit queue<ll,ll>::iterator
#define maxt(a,b,c) max(a,max(b,c))
#define mint(a,b,c) min(a,min(b,c))
#define rsort(v) sort(v.rbegin(),v.rend())
#define pb push_back
#define prnt(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" ";
using namespace std;
typedef long long int ll;
void solve()
{
ll n;
cin>>n;
ll a[n];
for(ll i = 0;i<n;i++)
cin>>a[i];
ll val = 0;
for(ll i = 0;i<n;i++)
{
ll mini = a[i];
for(ll j = i;j<n;j++)
{
mini = min(mini,a[j]);
val = max(val,(j-i+1)*mini);
}
}
cout<<val<<"\n";
}
int main()
{
speed;
ll t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
|
/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define ld double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define F(i,a,b) for(i=a;i<b;i++)
//#include <ext/pb_ds/assoc_container.hpp> // policy based data structure header files
//#include <ext/pb_ds/tree_policy.hpp> // policy based data structure header files
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds; // for pbds
//#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // have functions like order_of_key, find_by_order
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e18;
const ll MAXN=100200;
ll i,j,mask;
void solve()
{
ll n;
cin>>n;
vector<pair<ll,ll> > x(n), y(n);
F(i,0,n) {
ll a, b;
cin>>a>>b;
x[i]= {a, i};
y[i]= {b, i};
}
sort(all(x));
sort(all(y));
ll val=0, cx=0, cy=0;
val=x[n-1].fi-x[0].fi;
cx=x[0].se;
cy=x[n-1].se;
if(y[n-1].fi-y[0].fi>val) {
val=y[n-1].fi-y[0].fi;
cx=y[0].se;
cy=y[n-1].se;
}
priority_queue<pair<ll, pair<ll,ll> > > pq;
pq.push({x[n-1].fi-x[0].fi, {x[0].se, x[n-1].se} });
pq.push({x[n-1].fi-x[1].fi, {x[1].se, x[n-1].se} });
pq.push({x[n-2].fi-x[0].fi, {x[0].se, x[n-2].se} });
pq.push({y[n-1].fi-y[0].fi, {y[0].se, y[n-1].se} });
pq.push({y[n-1].fi-y[1].fi, {y[1].se, y[n-1].se} });
pq.push({y[n-2].fi-y[0].fi, {y[0].se, y[n-2].se} });
while(!pq.empty()) {
pair<ll, pair<ll,ll> > p=pq.top();
pq.pop();
if(p.se.fi!=cx || p.se.se!=cy) {
cout<<p.fi;
return ;
}
}
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE*/
ll t,test=1;
//cin>>test;
F(t,1,test+1) {
//cout<<"Case #"<<t<<": ";
solve();
nl
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
//typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
//typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};
template <typename T>
T powmod(T n, T k, T mod) {
if (k == 0) {
return 1;
} else if (k % 2 == 0) {
return powmod(n * n % mod, k / 2, mod);
} else {
return n * powmod(n, k-1, mod) % mod;
}
}
void dfs(int v, std::vector<std::vector<int>> &edges, std::vector<bool> &used) {
if (used[v]) return;
used[v] = true;
for (int c : edges[v]) {
dfs(c, edges, used);
}
}
int main() {
// use scanf in CodeForces!
cin.tie(0);
ios_base::sync_with_stdio(false);
int N; std::cin >> N;
std::vector<int> F(N); REP(i, N) std::cin >> F[i], F[i]--;
std::vector<std::vector<int>> edges(N);
REP(i, N) {
edges[i].emplace_back(F[i]);
edges[F[i]].emplace_back(i);
}
std::vector<bool> used(N, false);
int n_comp = 0;
REP(i, N) {
if (not used[i]) {
n_comp++;
dfs(i, edges, used);
}
}
const ll mod = 998244353;
ll ans = powmod<ll>(2, n_comp, mod);
std::cout << (ans + mod - 1) % mod << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int _ = 1;
void _solve() {
int n;
cin>>n;
vector< vector<int> >v(n , vector<int>(2));
for(int i = 0 ; i <n ;i++) {
cin>>v[i][0]>>v[i][1];
}
int ans = 0;
for(int i = 0 ; i <n ;i++) {
for(int j = i+1 ; j < n ;j++) {
int dx = v[j][0] - v[i][0];
int dy = v[j][1] - v[i][1];
if(dx < 0){
dx *= -1;
dy *= -1;
}
int slope = dy;
if(slope >= -dx && slope <= dx){
ans ++;
}
}
}
cout<<ans<<'\n';
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
while(_--)_solve();
return 0;
}
| #include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define po pop_back
#define mp make_pair
#define fi first
#define se second
#define LLM LONG_LONG_MAX
#define LLm LONG_LONG_MIN
#define hello ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define MOD 1000000007
using namespace std;
bool comp(ll a,ll b){if(a>b)return true;else return false;}
int isP(ll n){if(n==1)return 0;if(n==2 || n==3)return 1;for(ll i=2;i<=sqrt(n);i++){if(n%i==0)return 0;}return 1;}
ll power(ll a,ll b){if(b==0)return 1;if(b==1) return a;ll t=power(a,b/2);if(b%2==1) return t*t*a;else return t*t;}
ll powMOD(ll x,ll y){if(y==0)return 1;if(y==1)return x;ll t=powMOD(x,y/2);if(y%2==1)return t*t%MOD*x%MOD;else return t*t%MOD;}
int main()
{
/*//----Hello World----//*/
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
hello;
ll t;
// cin>>t;
t=1;
while(t--){
ll n;
cin>>n;
pair<ll,string> p[n+1];
for(ll i=1;i<=n;i++) cin>>p[i].se>>p[i].fi;
sort(p+1,p+n+1);
string ans=p[n-1].se;
cout<<ans<<"\n";
}
/*//----Hello World----//*/
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main() {
int a,b,x,y;
cin>>a>>b>>x>>y;
if(a>b) {
cout<<x+min(y,2*x)*(a-b-1);
} else {
cout<<min(y,2*x)*(b-a)+x;
}
return 0;
} | #include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<cstring>
using namespace std;
queue<pair<pair<int, int>, int>> que;
int dist[2][110];
bool in_que[2][110];
bool in_range(int y)
{
return y >= 1 && y <= 100;
}
int main()
{
int a, b, x, y;
int bu, cur, d;
int nbu, nxt, nd;
cin >> a >> b >> x >> y;
memset(dist, 100, sizeof(dist));
que.push(make_pair(make_pair(0, a), 0));
dist[0][a] = 0;
while (!que.empty())
{
bu = que.front().first.first;
cur = que.front().first.second;
d = que.front().second;
que.pop();
in_que[bu][cur] = false;
nbu = bu;
nxt = cur + 1;
nd = d + y;
if (in_range(nxt) && nd < dist[nbu][nxt])
{
dist[nbu][nxt] = nd;
if (!in_que[nbu][nxt])
{
que.push(make_pair(make_pair(nbu, nxt), nd));
in_que[nbu][nxt] = true;
}
}
nbu = bu;
nxt = cur - 1;
nd = d + y;
if (in_range(nxt) && nd < dist[nbu][nxt])
{
dist[nbu][nxt] = nd;
if (!in_que[nbu][nxt])
{
que.push(make_pair(make_pair(nbu, nxt), nd));
in_que[nbu][nxt] = true;
}
}
nbu = !bu;
nxt = cur;
nd = d + x;
if (in_range(nxt) && nd < dist[nbu][nxt])
{
dist[nbu][nxt] = nd;
if (!in_que[nbu][nxt])
{
que.push(make_pair(make_pair(nbu, nxt), nd));
in_que[nbu][nxt] = true;
}
}
nbu = !bu;
if (bu == 0) nxt = cur - 1;
else nxt = cur + 1;
nd = d + x;
if (in_range(nxt) && nd < dist[nbu][nxt])
{
dist[nbu][nxt] = nd;
if (!in_que[nbu][nxt])
{
que.push(make_pair(make_pair(nbu, nxt), nd));
in_que[nbu][nxt] = true;
}
}
}
cout << dist[1][b];
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define vec vector
#define rep(i,n) for (int i=0; i<(n); i++)
#define print(a) cout << a << endl
#define fix(n) fixed << setprecision(n)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define Yes "Yes"
#define YES "YES"
#define No "No"
#define NO "NO"
int main() {
ll N,W,S,T,P;
cin >> N >> W;
map<ll,ll> m;
rep(i,N) {
cin >> S >> T >> P;
S--;
T--;
m[S] += P;
m[T] -= P;
}
ll res = 0, sum = 0;
for (auto &e: m) {
sum += e.second;
res = max(res,sum);
}
print(((res>W) ? No : Yes));
return 0;
} | // Username: WillTheBill
// Time of creation: 2020-10-29 13:25
// Original filename: m.cp.cpp
#include<bits/stdc++.h>
using namespace std;
// shortcuts
#define pb push_back
#define fi first
#define se second
#define all(_v) _v.begin(),_v.end()
#define ll long long
// IO
#define multitest signed __T; cin >> __T; while(__T--)
template<typename T>
void _read(T& t);
template<typename T>
void _read(vector<T>&v);
template<typename T1, typename T2>
void _read(pair<T1,T2>&p);
template<typename T>
void _read(T& t){cin >> t;}
template<typename T>
void _read(vector<T>&v){for(unsigned _i=0;_i<v.size();_i++)_read(v[_i]);}
template<typename T1, typename T2>
void _read(pair<T1,T2>&p){_read(p.first);_read(p.second);}
void _masterread(){}
template<typename T,typename... V>
void _masterread(T& t, V&... v){_read(t);_masterread(v...);}
#define re(...)_masterread(__VA_ARGS__)
template<typename T>
void _print(T t);
template<typename T>
void _print(vector<T>&v);
template<typename T1, typename T2>
void _print(pair<T1,T2>&p);
template<typename T>
void _print(T t){cout<<t;}
template<typename T>
void _print(vector<T>&v){for(unsigned _i=0;_i<v.size();_i++)_print(v[_i]),cout<<(_i==v.size()-1?"":" ");}
template<typename T1, typename T2>
void _print(pair<T1,T2>&p){_print(p.first);cout<<" ";_print(p.second);}
void _masterprint(){cout<<endl;}
template<typename T,typename... V>
void _masterprint(T t, V... v){_print(t);if(sizeof...(v))cout<<" ";_masterprint(v...);}
#define pr(...)_masterprint(__VA_ARGS__)
// DEBUG
// colored output???
template <typename T> // start forward declaration
void _debug(T t);
template<typename T1,typename T2>
void _debug(pair<T1,T2> p);
template<typename T>
void _debug(vector<T>v);
template <typename T> // end forward declaration
void _debug(T t){cerr<<t;}
template<typename T1,typename T2>
void _debug(pair<T1,T2> p){cerr<<"{";_debug(p.first);cerr<<", ";_debug(p.second);cerr<<"}";}
template<typename T>
void _debug(vector<T>v){cerr<<"(";for(unsigned _i=0;_i<v.size();_i++)_debug(v[_i]),cerr<<(_i==v.size()-1?"":", ");cerr << ")";}
void _masterdebug(){cerr<<"]\n";}
template<typename T,typename... V>
void _masterdebug(T t,V... v){_debug(t);if(sizeof...(v))cerr<<", ";_masterdebug(v...);}
#ifdef __local__
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"] = [";_masterdebug(__VA_ARGS__)
#else
#define debug(...)
#endif
// TYPES
#define int long long
int n;
vector<vector<int>> nodes;
int RES = LLONG_MAX;
map<pair<int, int>, int> mem;
int dist(int ai, int bi){
auto& a = nodes[ai];
auto& b = nodes[bi];
return abs(a[0] - b[0]) + abs(a[1] - b[1]) + max(0ll, b[2] - a[2]);
}
int rec(int idx, int v, int pre){
if(mem.count({v, pre})) {
return mem[{v, pre}];
}
int res = LLONG_MAX;
if(idx == n - 1){
res = dist(pre, 0);
}else{
for(int i = 1; i < n; i++){
if(((v >> i) & 1) == 0){
v |= (1 << i);
res = min(dist(pre, i) + rec(idx + 1, v, i), res);
v &= ~(1 << i);
}
}
}
mem[{v,pre}] = res;
return res;
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
re(n);
nodes = vector<vector<int>> (n, vector<int> (3)); re(nodes);
pr(rec(0, 0, 0));
}
|
#include<bits/stdc++.h>
using namespace std;
#define MAX_N 200200
#define MAX_M 400400
int vis[MAX_N], used[MAX_N], match[MAX_M], dist[MAX_N], n, m, ans;
queue<int> q;
vector<int> v[MAX_N];
void bfs(){
while(!q.empty()){
int u = q.front(); q.pop();
for(auto e : v[u]){
if(match[e] && !dist[match[e]]){
dist[match[e]] = dist[u]+1;
q.push(match[e]);
}
}
}
}
int dfs(int u){
vis[u] = 1;
for(auto e : v[u]){
if(!match[e] || (!vis[match[e]] && dist[u]+1 == dist[match[e]] && dfs(match[e]))){
match[e] = u;
used[u] = 1;
return 1;
}
}
return 0;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
int a, b; scanf("%d %d",&a,&b);
v[i].push_back(a), v[i].push_back(b);
}
while(1){
for(int i=1;i<=n;i++) dist[i] = !used[i];
for(int i=1;i<=n;i++) if(!used[i]) q.push(i);
bfs();
int f = 0;
for(int i=1;i<=n;i++) vis[i] = 0;
for(int i=1;i<=n;i++) if(!used[i]) f+=dfs(i);
if(!f) break;
ans += f;
}
printf("%d",ans);
} | #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
//#define mydeb(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
#define mydeb(fmt, ...)
map<int,int>union_find_parent;
int union_find_add(int node);
int union_find_unite(int a,int b);
int union_find_root(int a);
int main()
{
int n;
cin >> n;
vector<int> a(n),b(n);
rep(i,n)
{
cin >> a[i] >> b[i];
}
map<int,int> numnode;
map<int,int> is_cycle;
rep(i,n)
{
if (union_find_root(a[i])<0)
{
mydeb("add %d\n",a[i]);
union_find_add(a[i]);
numnode[a[i]]=1;
is_cycle[a[i]]=0;
}
if (union_find_root(b[i])<0)
{
mydeb("add %d\n",b[i]);
union_find_add(b[i]);
numnode[b[i]]=1;
is_cycle[b[i]]=0;
}
int roota=union_find_root(a[i]);
int rootb=union_find_root(b[i]);
if (roota!=rootb)
{
mydeb("merge %d and %d\n",a[i],b[i]);
union_find_unite(roota,rootb);
// -> a's new root = b
numnode[rootb]+=numnode[roota];
is_cycle[rootb]|=is_cycle[roota];
numnode.erase(roota);
is_cycle.erase(roota);
}
else
{
// found cycle
mydeb("cycle contains %d and %d\n",a[i],b[i]);
is_cycle[roota]=1;
}
}
int total=0;
for (auto p: numnode)
{
total+=p.second-1;
}
for (auto p: is_cycle)
{
total+=p.second;
}
cout << total << endl;
}
int union_find_add(int node)
{
assert(union_find_parent.count(node)==0);
union_find_parent[node]=node;
return 0;
}
int union_find_unite(int a,int b)
{
assert( (union_find_parent.count(a)!=0) && (union_find_parent.count(b)!=0) );
a=union_find_root(a);
b=union_find_root(b);
if (a!=b)
{
union_find_parent[a]=b;
}
return 0;
}
int union_find_root(int a)
{
if (union_find_parent.count(a)==0)
{
return -1;
}
if (union_find_parent[a]==a)
{
return a;
}
else
{
return union_find_parent[a]=union_find_root(union_find_parent[a]);
}
}
|
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mod 1000000007
#define one(x) __builtin_popcountll(x)
#define zero(x) __builtin_ctzll(x)
#define pp pair<ll,ll>
#define all(x) (x).begin(), (x).end()
#define removeDuplicates(a) a.resize(unique(all(a))-a.begin())
template<class T> using oset =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
typedef long long int ll;
const int mxn = 2e5 + 10;
/*
ll n,q;
ll ar[200001];
ll t[800004];
void build(ll index,ll l, ll r){
if(l==r){
t[index]=ar[l];
return ;
}
ll mid=(l+r)/2 ;
build(index*2,l,mid);
build(index*2+1,mid+1,r);
t[index]=t[index*2]^t[index*2+1];
}
void update(ll index, ll l ,ll r, ll pos, ll val){
if(pos<l || pos>r){
return;
}
if(l==r){
t[index]=val;
ar[l]=val;
return ;
}
ll mid=(l+r)/2 ;
update(index*2,l,mid,pos,val);
update(index*2+1,mid+1,r,pos,val);
t[index]=min(t[index*2],t[index*2+1]);
}
ll query(ll index,ll l, ll r, ll lq, ll rq){
if(rq<l || lq>r){
return 0;
}
if(lq<=l && r<=rq){
return t[index];
}
ll mid=(l+r)/2 ;
return query(index*2,l,mid,lq,rq)^query(index*2+1,mid+1,r,lq,rq);
}*/
//ll a[mxn];
void subMain(){
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll blue = a, red = 0;
for(ll i = 1; i <= a; i++){
blue+=b;
red+=c;
if(blue<=red*d){
cout << i << "\n";
return;
}
}
cout << -1 << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
/*ll t, i = 0;
cin >> t;
while(t--){
//cout << "Case #" << i+1 << ": ";
subMain();
//i++;
}*/
subMain();
cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
return 0;
}
| #include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
double Sx, Sy, Gx, Gy;
cin >> Sx >> Sy >> Gx >> Gy;
double a = (Gy + Sy) / (Gx - Sx);
double c = Gy - (a * Gx);
double b = (-1 * c) / a;
cout << fixed << setprecision(10) << b << endl;
return 0;
}
|
// #pragma GCC optimize("Ofast")
#include <vector>
#include <iostream>
#include <utility>
#include <cmath>
#include <set>
#include <algorithm>
#include <queue>
#include <string>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<int,int> pii;
// #define int ll
#define fi first
#define se second
#define Sort(a) sort(a.begin(),a.end())
#define rep(i,n) for(int i = 0;i < (n) ; ++i)
#define REP(i,n) for(int i = 0;i < (n) ; ++i)
#define MP(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define INF LLONG_MAX/4
#define INTINF INT_MAX/2
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
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;}
/*----------------------------------------------------------------*/
inline unsigned long long xor128() {
static unsigned long long rx = 123456789, ry = 986960440, rz = 738905609, rw = 23140692;
unsigned long long rt = (rx ^ (rx << 11));
rx = ry;ry = rz;rz = rw;
return (rw = (rw ^ (rw >> 19)) ^ (rt ^ (rt >> 8)));
}
inline float rand128(float min = 0, float max = 1){
return (float)(xor128() % 0xFFFF) / 0xFFFF * (max - min) + min;
}
inline uint64_t rdtsc(){
uint32_t hi,lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ((uint64_t)lo) | (((uint64_t)hi)<<32);
}
inline double GetSeconds() {
// return rdtsc()/2.8e9; //Topcoder
return rdtsc()/3.0e9; // Atcoder
}
double start_time;
const double TL = 1.90;
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
int h[30][29];
int v[29][30];
const int Q = 1000;
const int H = 30;
const int W = 30;
/*----------------------------------------------------------------*/
void input(){
cerr << "in h" << endl;
cerr << H << endl;
cerr << W << endl;
rep(i,H)rep(j,W-1){
cin >> h[i][j];
cerr << h[i][j] << endl;
}
cerr << "in v" << endl;
rep(i,H-1)rep(j,W)cin >> v[i][j];
}
void solve(){
cerr << "solve" << endl;
int si,sj,ti,tj,a;
rep(q,Q){
cin >> si >> sj >> ti >> tj;
if(si > ti)rep(i,si-ti)cout << 'U';
if(ti > si)rep(i,ti-si)cout << 'D';
if(sj > tj)rep(i,sj-tj)cout << 'L';
if(tj > sj)rep(i,tj-sj)cout << 'R';
cout << endl;
cin >> a;
}
}
/*----------------------------------------------------------------*/
int main(){
cerr << "s" << endl;
start_time = GetSeconds();
solve();
} | #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
#define int long long
#define endl "\n"
constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;
struct fast_io {
fast_io(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
};
} fio;
vector<int> x, y, z;
int dis(int j, int k){
return abs(x[j] - x[k]) + abs(y[j] - y[k]) + max(0ll, z[k] - z[j]);
}
signed main(){
cout<<fixed<<setprecision(10);
int N, M;
vector<vector<int>> dp;
cin>>N;
dp.resize(N, vector<int>(1<<N, INF));
x.resize(N);
y.resize(N);
z.resize(N);
for(int i = 0; i < N; i++){
cin>>x[i]>>y[i]>>z[i];
}
dp[0][0] = 0;
for(int i = 1; i < 1<<N; i++){
for(int j = 0; j < N; j++){
for(int k = 0; k < N; k++){
if(!((i>>k)&1)) continue;
dp[k][i] = min(dp[k][i|(1<<k)], dp[j][i^(1<<k)] + dis(j, k));
}
}
}
cout<<dp[0][(1<<N)-1]<<endl;
return 0;
} |
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using i64 = int64_t;
using v64 = vector<i64>;
#define rep(i, s, n) for (i64 i = (s); i < (n); ++i)
template <typename T>
ostream &operator<<(ostream &os, const std::unordered_set<T> &x) {
os << "unordered_set{";
for (const auto &v : x) {
os << v << ", ";
}
os << "}";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const std::vector<T> &x) {
os << "vector{";
for (const auto &v : x) {
os << v << ", ";
}
os << "}";
return os;
}
template <typename F, typename S>
std::ostream &operator<<(std::ostream &os, const std::pair<F, S> &x) {
os << "pair{" << x.first << ", " << x.second << "}";
return os;
}
template <typename T1>
void dbg(const T1 &x1) {
std::cerr << x1 << std::endl;
}
template <typename T1, typename T2>
void dbg(const T1 &x1, const T2 &x2) {
std::cerr << x1 << " " << x2 << std::endl;
}
template <typename T1, typename T2, typename T3>
void dbg(const T1 &x1, const T2 &x2, const T3 &x3) {
std::cerr << x1 << " " << x2 << " " << x3 << std::endl;
}
template <typename T1, typename T2, typename T3, typename T4>
void dbg(const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4) {
std::cerr << x1 << " " << x2 << " " << x3 << " " << x4 << std::endl;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5>
void dbg(const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5) {
std::cerr << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5
<< std::endl;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6>
void dbg(const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5,
const T6 &x6) {
std::cerr << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " "
<< x6 << std::endl;
}
template <typename T>
std::vector<pair<std::int64_t, std::int64_t>> factor(T n) {
assert(1 <= n);
std::vector<pair<std::int64_t, std::int64_t>> ret{};
std::int64_t i{2};
while (i * i <= n) {
std::int64_t count{0};
while (n % i == 0) {
n /= i;
++count;
}
if (count) {
ret.push_back(std::make_pair(i, count));
}
if (i % 2) {
++i;
}
++i;
}
if (1 < n) {
ret.push_back(std::make_pair(n, 1));
}
return ret;
}
template <typename T>
constexpr T ipow(T x, T n) {
assert(0 <= n);
T ret{1};
while (0 < n) {
if (n % 2 == 1) {
ret *= x;
--n;
} else {
x *= x;
n /= 2;
}
}
return ret;
}
void run() {
i64 n;
cin >> n;
auto fs = factor(n);
i64 nret{1};
for (const auto &[f, c] : fs) {
nret *= c + 1;
}
v64 ret(nret, 1);
i64 chunk{1};
for (const auto &[f, c] : fs) {
rep(ir, 0, nret / (chunk * (c + 1))) {
i64 s{1};
rep(ic, 1, c + 1) {
s *= f;
rep(ichunk, 0, chunk) {
ret[ir * chunk * (c + 1) + ic * chunk + ichunk] *= s;
}
}
}
chunk *= c + 1;
}
sort(begin(ret), end(ret));
for (const auto r : ret) {
cout << r << endl;
};
}
int main() {
cin.tie(nullptr);
cout.precision(std::numeric_limits<double>::max_digits10);
run();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector <int>
#define vl vector <ll>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ff first
#define ss second
#define MOD 1000000007
ll power(ll a, ll b) { //a^b
ll res = 1;
a = a % MOD;
while (b > 0) {
if (b & 1) {res = (res * a) % MOD; b--;}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}
int main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vl fact;
for (ll i = 1; i * i <= n; i++) {
if (n % i)
continue;
if (i * i != n)
fact.pb(n / i);
fact.pb(i);
}
sort(all(fact));
for (auto i : fact)
cout << i << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
using namespace std;
int n,a[100005],dp[100005][2],cnt[100005][2];
const int mod=1e9+7;
int M(int x){return x>=mod?x-mod:x;}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
dp[1][0]=a[1];cnt[1][0]=1;
for(int i=2;i<=n;i++)
{
cnt[i][0]=M(cnt[i-1][0]+cnt[i-1][1]);
dp[i][0]=1ll*(1ll*M(dp[i-1][0]+dp[i-1][1])+1ll*a[i]*cnt[i][0])%mod;
cnt[i][1]=cnt[i-1][0];
dp[i][1]=1ll*(1ll*dp[i-1][0]+1ll*(mod-a[i])*cnt[i][1])%mod;
}
printf("%d\n",M(dp[n][0]+dp[n][1]));
return 0;
} | #include <bits/stdc++.h>
#define int long long
#define f first
#define s second
#define rep(i,a,b)for(int i=a;i<b;i++)
#define pb push_back
const int mod = 1e9 + 7;
const int INF = 1e18;
const int SIZE = 1e5 + 5;
using namespace std;
void solve() {
int n;
cin >> n;
int a[n];
rep(i, 0, n) {cin >> a[i]; a[i] %= 200;}
int ans = 0;
map<int, int> mp;
rep(i, 0, n) {
mp[a[i]]++;
}
for (auto it : mp) {
int k = it.second;
ans += (k * (k - 1)) / 2;
}
cout << ans << "\n";
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin >> T;
// sieve();
// op();
while (T--)
{
solve();
}
return 0;
} |
/**
* @brief atcoder
* @author yao
*/
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <utility>
#include <algorithm>
#include <functional>
#include <climits>
#define ft first
#define sd second
#ifdef DBG
# define dbg_pri(x...) fprintf(stderr,x)
#else
# define dbg_pri(x...) 0
# define NDEBUG
#endif //DBG
#include <cassert>
typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;
#define N 262144
int a[N];
lli b[N];
lli pre[N];
lli ans[N];
int main()
{
int n;
scanf("%d", &n);
for(int i=0;i<n;++i) scanf("%d", &a[i]);
b[0] = a[0];
for(int i=1;i<n;++i)
b[i] = b[i-1] + a[i];
for(int i=0;i<n;++i)pre[i+1] = pre[i]+b[i];
int m = 0;
for(int i=0;i<n;++i)
{
m = std::max(m, a[i]);
ans[i] = pre[i+1] + (lli)m*(i+1);
}
for(int i=0;i<n;++i) printf("%lld\n", ans[i]);
return 0;
}
| // Author :: <Hitesh_Saini>
#include<bits/stdc++.h>
#define __speed() ios_base::sync_with_stdio(false), cin.tie(nullptr);
#define dbg(x) cout << "(" << __LINE__ << ": "<< #x << " = " << x << ")\n"
#define Yes(x) print((x) ? "Yes" : "No")
#define tt int t; for (cin >> t; t--; )
#define f0(i, n) for (i = 0; i < (int)(n); i++)
#define f1(i, n) for (i = 1; i <=(int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x.size())
#define EB emplace_back
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define endl "\n"
#define S second
#define F first
using namespace std;
using mii = map<int, int>;
using pii = pair<int, int>;
using ll = int64_t;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
const pair<int, int> DD[] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int mod = 1e9 + 7, mxN = 1e5 + 5, INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <typename... T> void print(T... args) { ((cout << args << " "), ...), cout << endl; }
template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.F >> p.S; return in; }
template <typename T1, typename T2> ostream& operator<<(ostream& ot, pair<T1, T2>& p) { ot << p.F << ' ' << p.S; return ot; }
template <typename T1, typename T2> bool cmax(T1& a, T2 b) { if (b > a) { a = b; return true;} return false; }
template <typename T1, typename T2> bool cmin(T1& a, T2 b) { if (b < a) { a = b; return true;} return false; }
template <typename T> istream& operator>>(istream& in, vector<T>& v) { for (T& x:v) in >> x; return in; }
template <typename T> ostream& operator<<(ostream& ot, vector<T>& v) { for (T& x:v) ot << x << ' '; return ot; }
void solve() {
int n, m;
cin >> n >> m;
vi A(n), B(m);
cin >> A >> B;
mii mp;
for (int x : A) mp[x]++;
for (int x : B) mp[x]++;
for (auto& [x, f] : mp) {
if (f == 1)
cout << x << ' ';
}
}
signed main() {
__speed() solve();
// int t, i; for (cin >> t, i = 1; i <= t; i++) cout << "Case #" << i << ": ", solve();
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
int main() {
double a,b;
cin >> a >> b;
double ans;
ans = ((a-b)/a)*100;
cout << std::fixed << std::setprecision(18) << ans;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <numeric>
#include <math.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#if __cplusplus >= 201103L
#include <unordered_set>
#include <unordered_map>
#include <chrono>
#include <random>
#include <bits/stdc++.h>
struct pairhash {
template<class T, class U>
size_t operator() (const pair<T,U> &p) const {
static const uint64_t FIXED_RANDOM = (uint64_t)chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(p.first + FIXED_RANDOM) ^ splitmix64(p.second+ FIXED_RANDOM);
}
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
inline int randint(int l, int r) {
return uniform_int_distribution<int>(l,r)(rng);
}
inline double randdouble(double l, double r) {
return uniform_real_distribution<double>(l,r)(rng);
}
#endif
#define fastio ios::sync_with_stdio(false); cin.tie(0);
#define ll long long
#define ll128 __int128_t
#define PI 3.14159265358979323846
#define min(a,b) ((a<b)?(a):(b))
#define max(a,b) ((a<b)?(b):(a))
#define abs(a) ((a>0)?a:-(a))
#define pii pair<int,int>
#define pll pair<ll,ll>
template <typename T>
inline void read(T& x) {
x = 0;
bool f = 0;
char ch = (char)getchar();
while (ch < '0' || ch > '9') { // ch 不是数字时
if (ch == '-') f = 1; // 判断是否为负
ch = (char)getchar(); // 继续读入
}
while (ch >= '0' && ch <= '9') { // ch 是数字时
x = x*10 + (ch&15); // 将新读入的数字’加’在 x 的后面, ch&15等同于ch-'0'
ch = (char)getchar(); // 继续读入
}
if (f) x = -x;
}
template <typename T>
inline void write(T x) {
static int st[35];
int top = 0;
do {
st[top++] = x % 10; x /= 10;
} while (x);
while (top) putchar(st[--top] + '0');
}
const double pi = acos(-1.0);
const double eps = (double)1e-4;
const int mod = 1e9+7;
const int maxn = 1e5+5;
const int maxm = 2e5+10;
int main() {
fastio;
double a,b; cin >> a >> b;
cout << (a-b) / a * 100 << endl;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
template<class T>
bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template<class T>
bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<int, int>;
using vvi = vector<vector<int>>;
using vi = vector<int>;
const ll MOD = 1e9 + 7;
const int INF = 1001001001;
const double PI = 3.14159265358979323846;
void solve() {
int n;
cin >> n;
vector<int> A(n);
rep(i, n) cin >> A[i];
int ans = 0;
rep(i, n) {
if (A[i] > 10) ans += A[i] - 10;
}
cout << ans << endl;
}
int main() {
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
string S;
cin >> S;
bool flag=true;
deque<char> ans;
for(int i=0;i<S.size();i++){
if(S[i]=='R')flag=(!flag);
else if(flag){
if(!ans.empty() && S[i]==ans.back())ans.pop_back();
else ans.push_back(S[i]);
}
else{
if(!ans.empty() && S[i]==ans.front())ans.pop_front();
else ans.push_front(S[i]);
}
}
string ANS="";
for(auto c:ans)ANS+=c;
if(!flag)reverse(ANS.begin(),ANS.end());
cout << ANS << endl;
return 0;
} |
#include<iostream>
using namespace std;
int H,W,A;
int dp[17][1<<16][9];
main()
{
cin>>H>>W>>A;
dp[0][0][A]=1;
for(int i=0;i<H;i++)for(int j=0;j<=W;j++)for(int k=0;k<1<<W;k++)
{
int t=(1<<W)-k-1;
if(j==W)
{
int a=__builtin_popcount(t);
for(int l=a;l<=A;l++)dp[i+1][t][l-a]+=dp[i][k][l];
}
if(t>>j&1)
{
for(int a=0;a<=A;a++)
{
dp[i][k|1<<j][a]+=dp[i][k][a];
if(a&&t>>j+1&1)dp[i][k|3<<j][a-1]+=dp[i][k][a];
}
}
}
cout<<dp[H][0][0]<<endl;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
void recursive(vector<vector<int>>& arr, int h, int w, int& ans,int a_count, int a)
{
if(a==a_count)
{
ans++;
return;
}
if(a_count<a){
for(int i=0;i<h;i++)
{
for(int j=0;j<w-1;j++)
{
if(arr[i][j] == 0 && arr[i][j+1]==0)
{
arr[i][j] = 1;
arr[i][j+1] = 1;
recursive(arr, h, w, ans , a_count+1,a);
arr[i][j] = 0;
arr[i][j+1] = 0;
}
}
}
for(int i=0;i<h-1;i++)
{
for(int j=0;j<w;j++)
{
if(arr[i][j] == 0 && arr[i+1][j]==0)
{
arr[i][j] = 1;
arr[i+1][j] = 1;
recursive(arr, h, w, ans , a_count+1, a);
arr[i][j] = 0;
arr[i+1][j] = 0;
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int h,w,a,b;
int ans=0;
cin>>h>>w>>a>>b;
vector<vector<int>> arr(h, vector<int>(w,0));
recursive(arr, h, w, ans , 0, a);
long fact=1;
for(int i = 1; i <=a ; ++i){
fact*=i;
}
cout<<ans/fact;
}
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <cstring>
#include <utility>
#include <numeric>
#include <set>
using namespace std;
int main(){
long long N;
cin >> N;
set<long long> numSet;
for(long long i = 2; i <= 100000; i++){
if(i * i > N){
break;
}
if(numSet.find(i) == numSet.end()){
long long temp = i * i;
while(temp <= N){
numSet.insert(temp);
temp *= i;
}
}
}
long long ans = N - (int)numSet.size();
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define all(v) begin(v), end(v)
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; ++i)
#define rep3(i, n, m) for (ll i = n; i >= m; --i)
using namespace std;
using ll = int_fast64_t;
using ld = long double;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> using pq = priority_queue<T>;
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; }
constexpr ll INF = (1LL << 30);
constexpr ll INFLL = (1LL << 60);
constexpr ll MAX = 500005;
constexpr ll MOD = 1000000007;
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(15);
ll n;
cin >> n;
rep2(i, 1, 1e6) {
string s = to_string(i);
s += s;
ll t = stol(s);
if (t > n) {
cout << i-1 << endl;
return (0);
}
}
return (0);
}
|
#include <bits/stdc++.h>
using namespace std ;
int main (){
char a,b,c;
cin>>a>>b>>c;
cout<<b<<c<<a;
return 0;
} | //x<<y=x*2^y,x>>y=x/2^y
//1e5+3 is prime
// in a matrix, all elements of a primary diagonal have constant diff of coordinates
// and a secondary diagonal has constant sum of coordinates
//use ll() for using an integer in self-built fn
//(x&-x) bitwise and of (x and its 2's complement) returns (last set bit)
//eg if x=1010 then it will return 0010
// careful dont print empty container(run time error)
//v.erase O(n)
//use ("\n") instead of endl as endl flushes the output buffer
//every time so takes more time than \n (TLE)
//stoll() and to_string((less than 1e19))
//INT_MAX is approx 3e10
//For sets use set_name.lower_bound(x)(strictly O(logn))
//NOT lb(all(s),x)(O(logn) for random access (eg vector) but for sets it is O(n));
#include<bits/stdc++.h>
typedef long long int ll;
#define ull unsigned long long int
#define lld long double
#define endl "\n"
#define fi first
#define sec second
#define sp setprecision
#define lb lower_bound
#define ub upper_bound
#define For(i, a, b) for(long long int i = (a); i <= (b); i++)
#define Forr(i, a, b) for(long long int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define vll vector<ll>
#define pll pair<ll,ll>
#define vlld vector<lld>
#define vi vector<int>
#define vch vector<char>
#define sll set<ll>
#define sch set<ch>
#define vpll vector< pair<ll,ll> >
#define vpii vector< pair<int,int> >
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define mll map<ll, ll>
#define mcll map<char,ll>
#define sz(container) ll((container).size())
#define fill(a,b) memset(a, b, sizeof(a))
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
lld pi=3.1415926535897932;
const ll mod=1000000007;
const ll dx[4]={-1, 0, 1, 0} , dy[4]={0, 1, 0, -1};
const ll dxx[8]={-1, -1, 0, 1, 1, 1, 0, -1} , dyy[8]={0, 1, 1, 1, 0, -1, -1, -1};
string ds="RLDU";
ll lcm(ll a, ll b)
{
ll g=__gcd(a, b);
return a/g*b;
}
ll binpow(ll a, ll b,ll m)
{
a %= m;
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll Pow(ll a, ll b)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a;
a = a * a ;
b >>= 1;
}
return res;
}
ll modinv(ll n,ll mod)
{
return binpow(n, mod - 2,mod);
}
/*******************************************coding********/
ll gcdExtended(ll a, ll b, ll &x, ll &y){
if(a == 0){
x = 0;
y = 1;
return b;
}
ll x1, y1;
ll gcd = gcdExtended(b%a, a, x1, y1);
x = y1 - (b/a) * x1;
y = x1;
return gcd;
}
ll modInverse(ll a, ll m){
ll x, y;
ll g = gcdExtended(a, m, x, y);
ll res = (x%m + m) % m;
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast_io;
ll T=1;cin>>T;
For(i,1,T)
{
ll n,s,k;cin>>n>>s>>k;
s=n-s;
ll d=__gcd(__gcd(s,k),n);
s/=d;k/=d;n/=d;
if(__gcd(k,n)==1)
{
cout<<(s*modInverse(k,n))%n<<endl;
}
else cout<<-1<<endl;
}
} |
Subsets and Splits