code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
typedef long long ll;
const ll mod = 1e9 + 7;
string s;
int k, a[N], n;
int work(int x) {
int ans = 0;
while (x) {
if (x & 1) ans++;
x = x / 2;
}
return ans;
}
ll dp[N][20];
ll dfs(int p, int stat, int limit) {
int cnt = work(stat);
// cout << p << " " << cnt << " " << stat << endl;
if (cnt > k) return 0;
if (p == n) {
if (cnt == k) return 1;
return 0;
}
if (!limit && dp[p][cnt] != -1) return dp[p][cnt];
int maxn = 15;
if (limit) {
maxn = a[p];
}
ll ans = 0;
for (int i = 0; i <= maxn; i++) {
ans += dfs(p + 1, stat | (1 << i), limit && i == maxn);
ans = ans % mod;
}
if (!limit) dp[p][cnt] = ans;
return ans;
}
void solve() {
ll ans = 0;
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++) {
int maxn = 15;
if (i == 0) {
maxn = a[0];
}
// cout << "Maxn " << maxn << endl;
for (int j = 1; j <= maxn; j++) {
ans += dfs(i + 1, (1 << j), i == 0 && j == maxn);
ans = ans % mod;
}
}
cout << ans << endl;
}
int main() {
cin >> s >> k;
n = s.length();
for (int i = 0; i < s.length(); i++) {
if (s[i] >= '0' && s[i] <= '9') {
a[i] = s[i] - '0';
} else {
a[i] = s[i] - 'A' + 10;
}
}
solve();
}
| #include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
const int mod=1e9+7;
char s[maxn];int n,vis[300],ans=0,cnt=0;
int dp[maxn][17],f[maxn][17][17];
int mp[300];
void dfs(int pos)
{
if(pos>n) return;
for(int i=1;i<=16;i++){
dp[pos][i]=(dp[pos][i]+1LL*i*dp[pos-1][i]%mod)%mod;
dp[pos][i]=(dp[pos][i]+1LL*(17-i)*dp[pos-1][i-1]%mod)%mod;
}
if(pos!=1){
dp[pos][1]=(dp[pos][1]+15)%mod;
}else{
dp[pos][1]=(dp[pos][1]+(mp[s[pos]]-1))%mod;
}
if(pos!=1){
for(int i=0;i<mp[s[pos]];i++){
if(vis[i]==0)
dp[pos][cnt+1]=(dp[pos][cnt+1]+1)%mod;
else
dp[pos][cnt]=(dp[pos][cnt]+1)%mod;
}
}
if(vis[mp[s[pos]]]==0) cnt++,vis[mp[s[pos]]]=1;
//cout<<pos<<" "<<dp[pos][1]<<endl;
dfs(pos+1);
}
int main()
{
for(int i=0;i<10;i++) mp[i+'0']=i;
for(int i=0;i<6;i++) mp[i+'A']=i+10;
scanf("%s",s+1);n=strlen(s+1);int k;scanf("%d",&k);
dfs(1);if(cnt==k) dp[n][k]++;
cout<<dp[n][k]<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair <int, int> pin;
const int N = 2e5 + 5;
const ll P = 998244353LL;
const int inf = 1 << 30;
int n, k, tot, cnt, head[N], f[N];
struct Edge {
int to, nxt;
} e[N << 1];
inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
}
template <typename T>
inline void read(T &X) {
char ch = 0; T op = 1;
for (X = 0; ch > '9' || ch < '0'; ch = getchar())
if (ch == '-') op = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
X = (X * 10) + ch - '0';
X *= op;
}
void dfs(int x, int fat, int mid) {
int mx = -inf, mn = inf;
for (int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == fat) continue;
dfs(y, x, mid);
mx = max(mx, f[y]);
mn = min(mn, f[y]);
}
if (mx == -inf) mx = 0;
++mn;
// if (mn <= 0) {
// if (mn + mx > 0) {
// ++cnt;
// f[x] = -mid;
// } else f[x] = mn;
// } else {
// if (mx + 1 > mid) {
// ++cnt;
// f[x] = -mid;
// } else f[x] = mx + 1;
// }
if (mx + 1 > mid) {
++cnt;
f[x] = -mid;
} else if (mn + mx <= 0) f[x] = mn;
else f[x] = mx + 1;
}
inline bool chk(int mid) {
cnt = 0;
for (int i = 1; i <= n; i++) f[i] = 0;
dfs(1, 0, mid);
if (f[1] > 0) ++cnt;
// printf("%d:\n", mid);
// for (int i = 1; i <= n; i++) printf("%d%c", f[i], " \n"[i == n]);
return cnt <= k;
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("sample.in", "r", stdin);
// clock_t st_clock = clock();
// #endif
read(n), read(k);
for (int x, y, i = 1; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
}
int l = 1, r = n, mid, res = n;
for (; l <= r; ) {
mid = (l + r) / 2;
if (chk(mid)) res = mid, r = mid - 1;
else l = mid + 1;
}
printf("%d\n", res);
// #ifndef ONLINE_JUDGE
// clock_t ed_clock = clock();
// printf("time = %f ms\n", (double)(ed_clock - st_clock) / CLOCKS_PER_SEC * 1000);
// #endif
return 0;
} | #include<cstdio>//JZM YYDS!!!
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<ctime>
#define ll long long
#define MAXN 100005
#define uns unsigned
#define MOD 1000000007ll
#define INF 1e18
using namespace std;
inline ll read(){
ll x=0;bool f=1;char s=getchar();
while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
return f?x:-x;
}
inline ll ksm(ll a,ll b,ll mo){
ll res=1;
for(;b;b>>=1,a=a*a%mo)if(b&1)res=res*a%mo;
return res;
}
ll n;
ll dp[MAXN];
vector<int>f;
stack<int>pt;
signed main()
{
n=read();
ll x=1,y=0;
dp[0]=1;
int m=0;
for(m=1;m<=130;m++){
y+=x,x+=y;
dp[m]=x;
if(dp[m]>n)break;
}
ll l=0;
for(int i=m;i>=0;i--){
if(l+dp[i]<=n)l+=dp[i],f.push_back(i);
if(l+dp[i]<=n)l+=dp[i],f.push_back(i);
}
while(l<n)l+=dp[0],f.push_back(0);
int w=0;
for(int i=f.size()-1;i>=0;i--){
int x=f[i];
while(w<x)w++,pt.push(3),pt.push(4);
pt.push(1);
}
printf("%d\n",(int)pt.size());
while(!pt.empty())printf("%d\n",pt.top()),pt.pop();
return 0;
} |
#include <bits/stdc++.h>
int main(){
int n, l;
std::cin >> n >> l;
std::vector<std::pair<int, int>> vec(n + 2);
vec[0] = std::make_pair(0, 0);
vec[n + 1] = std::make_pair(l - n, l - n);
for(int i = 1; i <= n; i++) std::cin >> vec[i].first, vec[i].first -= i;
for(int i = 1; i <= n; i++) std::cin >> vec[i].second, vec[i].second -= i;
long long res = 0;
for(int i = 1; i <= n;){
if(vec[i].first < vec[i].second){
int iter = std::lower_bound(vec.begin() + i, vec.end(), std::make_pair(vec[i].second, -1)) - vec.begin();
if(vec[i].second == vec[iter].first){
res += (long long)(iter - i);
while(i < iter && vec[i].second == vec[iter].first){
vec[i].first = vec[i].second;
i++;
}
}
else{
std::cout << "-1\n";
return 0;
}
}
else i++;
}
for(int i = n; i >= 1;){
if(vec[i].first > vec[i].second){
int iter = std::upper_bound(vec.begin() + 1, vec.begin() + i + 1, std::make_pair(vec[i].second, 0x7f7f7f7f)) - vec.begin();
iter--;
if(vec[i].second == vec[iter].first){
res += (long long)(i - iter);
while(i > iter && vec[i].second == vec[iter].first){
vec[i].first = vec[i].second;
i--;
}
}
else{
std::cout << "-1\n";
return 0;
}
}
else i--;
}
std::cout << res << "\n";
}
| #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int,int>;
void debug(map<int,pii> a) {
for (auto p : a) {
cout << p.first << ": " << "(" << p.second.first << "," << p.second.second << ")\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N,L;
cin >> N >> L;
auto read = [](int N, int L) {
vector<int> v{0};
for (int i = 1; i <= N+1; i++) {
int ai = L+1;
if (i <= N)
cin >> ai;
v.push_back(ai-i);
}
map<int,pii> interval;
for (int i = 0; i < v.size(); i++) {
if (i==0 || v[i] != v[i-1])
interval[v[i]].first = i;
interval[v[i]].second = i;
}
return interval;
};
auto A = read(N,L);
auto B = read(N,L);
ll ans = 0;
for (auto p : B) {
int val = p.first;
if (!A.count(val)) {
cout << -1 << '\n';
return 0;
}
ans += max(A[val].first-B[val].first,0) + max(B[val].second-A[val].second,0);
}
cout << ans << '\n';
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
scanf("%d",&t);
string T="atcoder";
while(t--){
string S;
cin>>S;
int now=0,ans=1e9;
bool op=S.size()>T.size();
for(int i=0;i<S.size()&&i<T.size();++i){
if(S[i]<=T[i]){
bool ok=0;
for(int j=i;j<S.size();++j){
if(S[j]>T[i]){
ans=min(ans,now+j-i);
}
}
if(S[i]<T[i])
for(int j=i;j<S.size();++j){
if(S[j]>T[i])ans=min(ans,now+j-i);
else if(S[j]==T[i]){
ok=1;
for(int k=j;k>i;--k)
swap(S[k],S[k-1]);
now+=j-i;
break;
}
}
// cerr<<"FK: "<<now<<" "<<ans<<endl;
if(S[i]<T[i]&&!ok){now=1e9;break;}
}
else if(S[i]>T[i]){
op=1;
ans=min(ans,now);
break;
}
}
if(!op)now=1e9;
if(min(ans,now)>1e8)puts("-1");
else printf("%d\n",min(ans,now));
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
string s,AT="atcoder";
int T;
void solve(){
int ans=1e9,cur=0;
for(int i=0;i<7;i++){
int minpos=s.size(),minnp=s.size();
for(int j=i;j<(int)s.size();j++){
if(AT[i]<s[j]&&minpos==(int)s.size())minpos=j;
if(AT[i]==s[j]&&minnp==(int)s.size())minnp=j;
}
if(minpos<s.size())ans=min(ans,cur+minpos-i);
if(minnp<s.size()){
cur+=minnp-i;
for(int j=minnp;j>i;j--)s[j]=s[j-1];
}else cur=1e9;
}
if((int)s.size()>7)ans=min(ans,cur);
if(ans==1e9)puts("-1");
else cout<<ans<<"\n";
}
int main(){
cin>>T;
while(T--){
cin>>s;
if(AT<s){
puts("0");
continue;
}
solve();
}
} |
/*ver 7*/
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : v)
#define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO" )
#define Yes(n) ((n) ? "Yes" : "No" )
#define yes(n) ((n) ? "yes" : "no" )
int main(){
init();
inl(n);
V<ll> a(n);
in1(a);
V<ll> goal(n);
V<ll> l(n),r(n);
ll now=0;
rep(i,n){
now+=a[i];
goal[i]=now;
if(i==0){
l[i]=min(0LL,now);
r[i]=max(0LL,now);
}else{
l[i]=min(l[i-1],now);
r[i]=max(r[i-1],now);
}
}
ll ansMax=0,ansMin=0;
now=0;
rep(i,n){
chmin(ansMin,now+l[i]);
chmax(ansMax,now+r[i]);
now+=goal[i];
}
out(ansMax);
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<(a)<<endl
#define dumparr(a,n) cerr<<#a<<"["<<(n)<<"]="<<(a[n])<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;
pll operator+(pll a,pll b){
return pll(a.first+b.first,a.second+b.second);
}
pll operator-(pll a,pll b){
return pll(a.first-b.first,a.second-b.second);
}
pll operator*(ll a,pll b){
return pll(b.first*a,b.second*a);
}
const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
a=abs(a);
b=abs(b);
if(a==0)return b;
if(b==0)return a;
if(a < b) return gcd(b, a);
ll r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
if(a<b){
a=b;
return true;
}
return false;
}
template<class T>
bool chmin(T& a,const T& b){
if(a>b){
a=b;
return true;
}
return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
os << "(" << a.first << "," << a.second << ")";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
os << "[ ";
REP(a.size()){
os<< a[i] << " ";
}
os<< "]";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,set<T> a){
os << "{ ";
for(auto itr=a.begin();itr!=a.end();++itr){
os<< *itr << " ";
}
os<< "}";
return os;
}
template<class T>
void ans_array(T begin,T end){
auto itr=begin;
cout<<*itr;
++itr;
for(;itr!=end;++itr){
cout<<' '<<*itr;
}
cout<<endl;
}
template<class T>
void ans_array_newline(T begin,T end){
for(auto itr=begin;itr!=end;++itr){
cout<<*itr<<endl;
}
}
void solve(long long N, std::vector<long long> A){
REP(N){
if(i%2){
A[i]=-A[i];
}
}
vector<ll> sumA(N+1);
REP(N){
sumA[i+1]=sumA[i]+A[i];
}
map<ll,ll> mp;
REP(N+1){
mp[sumA[i]]++;
}
ll ans=0;
for (auto &&i : mp)
{
ans+=i.second*(i.second-1)/2;
}
cout<<ans<<endl;
}
int main(){
cout<<setprecision(1000);
long long N;
scanf("%lld", &N);
std::vector<long long> A(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld", &A[i]);
}
solve(N, std::move(A));
return 0;
}
|
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b)for(ll i=a;i<=b;i++)
#define loopr(i,a,b)for(ll i=b;i>=a;i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define vll vector<ll>
#define vvl vector<vll>
#define vpll vector<pll>
#define pll pair<ll,ll>
#define F first
#define S second
#define MOD 1000000007
ll max(ll a,ll b){if (a>b) return a; else return b;}
ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
using namespace std;
//take care of long long
//fast I/O
auto optimizer = []() { // makes I/O fast
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
return 0;
}();
//seive
ll N = 1e5;
vll lpf(N+1,0);
void leastPrimeFactor()
{
lpf[1] = 1;
for (ll i = 2; i <= N; i++)
{
if(lpf[i] == 0)
{
lpf[i] = i;
for (ll j = 2*i; j <= N; j += i)
if (lpf[j] == 0)
lpf[j] = i;
}
}
}
int main()
{
ll t=1;
//cin>>t;
while(t--)
{
ll a,b;
cin>>a>>b;
double c=a*b;
c/=100;
cout<<c<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;
ll mod = 998244353;
//累乗 aのb乗、正しmを法として求める
long long pw(long long a,long long b,long long m){
if(b==0) return 1;
else if(b%2==0){
long long x = pw(a,b/2,m);
return (x*x)%m;
}
else{
long long x = pw(a,b-1,m);
return (a*x)%m;
}
}
int main() {
ll H,W; cin >> H >> W;
char gr[H][W];
rep(i,H){
rep(j,W) cin >> gr[i][j];
}
bool ok = true; ll fr = 0;
for(int i=0;i<=H+W-2;i++){
bool fixed = false; char col; ll cnt = 0;
rep(j,H){
if(i-j<0) break;
if(i-j>=W) continue;
int k = i-j;
if(gr[j][k]=='.'){
cnt++;
}
else{
if(!fixed){
fixed = true; col = gr[j][k];
}
else{
if(col!=gr[j][k]){
ok = false;
}
}
}
}
if(!fixed) fr++;
}
ll ans = pw(2LL,fr,mod);
if(!ok) ans = 0;
cout << ans << endl;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define real long double
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pi M_PI
#define elif else if
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const int INF = 2e18, MOD = 998244353, RANDOM = chrono::steady_clock::now().time_since_epoch().count();
const real EPS = 1e-12;
mt19937 rng(RANDOM);
struct chash {
int operator() (int x) const { return (x ^ RANDOM) % MOD; }
};
int n;
vector<vector<int>> people;
vector<int> ind;
vector<pair<int, int>> answer;
int search(){
int cnt = 0;
for (int i=0; i<n - 1; ++i){
if (people[i][2] == people[i][3]) continue;
if (people[i][1] >= people[i][0]) return -1;
int j = ind[people[i][3]];
if (people[j][1] >= people[j][0]) return -1;
ind[people[i][2]] = j;
ind[people[i][3]] = people[i][3];
swap(people[i][1], people[j][1]);
swap(people[i][2], people[j][2]);
answer.push_back({people[i][3], people[j][3]});
++cnt;
}
return cnt;
}
void debug(){
cerr << "\n";
cerr << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// cout.precision(6);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n;
people.assign(n, vector<int>(4, 0));
ind.assign(n, 0);
vector<int> b(n);
for (int i=0; i<n; ++i) cin >> people[i][0];
for (int i=0; i<n; ++i) cin >> b[i];
for (int i=0; i<n; ++i){
cin >> people[i][2];
--people[i][2];
people[i][1] = b[people[i][2]];
people[i][3] = i;
}
sort(all(people));
for (int i=0; i<n; ++i) ind[people[i][2]] = i;
int res = search();
cout << res << "\n";
if (res != -1) for (auto i: answer) cout << i.f + 1 << " " << i.s + 1 << "\n";
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else
#endif
const int MAXN = 2e5+10;
int a[MAXN];
int b[MAXN];
int p[MAXN];
int q[MAXN];
int main() {
ios::sync_with_stdio(false);
cin.tie(0); // Togliere nei problemi con query online
int N;
cin >> N;
for (int i = 1; i <= N; i++) cin >> a[i];
for (int i = 1; i <= N; i++) cin >> b[i];
for (int i = 1; i <= N; i++) cin >> p[i], q[p[i]] = i;
for (int i = 1; i <= N; i++) {
if (a[i] <= b[p[i]] and p[i] != i) {
cout << -1 << "\n";
return 0;
}
}
vector<int> o;
for (int i = 1; i <= N; i++) o.push_back(i);
sort(o.begin(), o.end(), [&](int i, int j) {return b[i] > b[j];});
//for (int x : o) cout << x << " " << b[x] << endl;
vector<pair<int,int>> op;
for (int x : o) {
if (p[x] == x) continue;
int y = q[x];
op.push_back({x,y});
q[p[x]] = y;
p[y] = p[x];
q[x] = x;
p[x] = x;
}
cout << op.size() << "\n";
for (auto pp : op) cout << pp.first << " " << pp.second << "\n";
}
|
// Sakhiya07 - Yagnik Sakhiya
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>
#define ff first
#define ss second
#define MOD 1000000007
const int N = 100005;
void solve()
{
ll n;
cin >> n;
map<ll,ll> y;
ll ans = 0;
for(ll i = 1 ; i <= n ; i++) {
ll x;
cin >> x;
ans += (i-1-y[x]);
y[x]++;
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
for(int i = 1; i < t + 1; i++) {
solve();
}
} | #include <bits/stdc++.h>
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define PER(i,a,b) for (int i = (b); i >= (a); --i)
#define log2(x) (31-__builtin_clz(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) (int)(x).size()
#define PB push_back
#define FI first
#define SE second
#define mup(x,y) x = min(x,y)
#define Mup(x,y) x = max(x,y)
#define debug(x) cout << #x << " is " << x << el
#define el '\n'
using namespace std; using ll = long long; using ii = pair<int,int>; using iii = tuple<int,int,int>;
void solution(); int main() {ios::sync_with_stdio(0); cin.tie(0); solution();}
const int N = 30'0005;
ll n;
int a[N];
void input() {
cin >> n;
REP(i,1,n) cin >> a[i];
}
void solution() {
input();
sort(a+1,a+n+1);
ll x = n*(n+1)/2;
ll cnt = 0;
REP(i,1,n) {
++cnt;
if (a[i] != a[i+1]) {
x -= cnt*(cnt+1)/2;
cnt = 0;
}
}
cout << x;
} |
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#define fast_IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define rep(i,a,b) for (int i = a; i < b; i++)
#define rev(i,n) for(int i = n-1; ~i; i--)
#define pii pair<int,int>
#define ar(n) array<int,n>
#define sz(n) (int)n.size()
#define uniq(v) (v).erase(unique(v.begin(), v.end()),(v).end())
#ifndef ONLINE_JUDGE
#define dbg(x) cerr<<#x<<" "<<x<<endl;
#else
#define dbg(x)
#endif
template<typename T>istream& operator>>(istream& in,vector<T>& a){for(auto& i:a) in>>i; return in;}
template<typename T>ostream& operator<<(ostream& out,vector<T>& a){for(auto& i:a) out<<i<<" "; return out;}
auto clk = clock();
mt19937_64 rang(high_resolution_clock::now().time_since_epoch().count());
void run_time() { cerr<<endl; cerr<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl; return; }
const int inf=1e18; const int32_t M=1e9+7; const int32_t mxn=1e6+1;
/*-------------------------------------solve-----------------------------------------*/
void solve()
{
double a,b,c; cin>>a>>b;
c=100;
double ans = (a*b);
ans/=c;
cout<<ans;
}
int32_t main()
{
fast_IO
#ifndef ONLINE_JUDGE
freopen("error.txt","w",stderr);
#endif
int t = 1;
// cin >> t;
while (t--)
solve();
run_time();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int A,B;
cin>>A>>B;
double ans=(double) A*B/100;
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
#include <string>
#define rep(i,n) for(int i=0;i < (n);i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
const ll MOD=1000000007ll;
const int MAX=5100000;
using namespace std;
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T& b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
int inputValue(){
int a;
cin >> a;
return a;
};
void inputArray(int * p, int a){
for(int i=0;i<a;i++){
cin >> p[i];
}
};
void inputVector(vector<int> * p, int a){
for(int i=0;i<a;i++){
int input;
cin >> input;
p -> push_back(input);
}
}
ll fact[MAX], fact_inv[MAX];
ll power(ll a, ll b){
ll res=1;
while(b>0){
if(b&1) res=res*a%MOD;
a=a*a%MOD;
b>>=1;
}
return res;
}
// nCr
ll comb(ll n, ll r){
ll t=1000000;
fact[0]=1;
for(int i=0;i<t;i++){ fact[i+1]=fact[i]*(i+1)%MOD;}
fact_inv[t]=power(fact[t], MOD-2);
for(int i=0;i<t;i++){ fact_inv[i]=fact_inv[i+1]*(i+1)%MOD;}
return (fact[n]*fact_inv[r])%MOD*fact_inv[n-r]%MOD;
}
ll i,j,k,tmp;
ll ans = 0;
int main()
{
cin.tie(0); ios::sync_with_stdio(false);
int N,K; cin >> N >> K;
int A[N]={0};
rep(i,N){cin >> tmp; A[tmp]++;}
ans = 0; int idx = -1;
rep(i,N+1){
if(A[i]==0) break;
idx = i;
}
chmin(K,A[0]);
if(idx==-1){cout << 0 << endl; return 0;}
int cnt = 0;
rep(i,K){
ans += idx + 1; cnt++;
if(A[0]<=cnt){break;}
for(j=0;j<=idx;j++){
if(A[j]<=cnt){idx=j-1;break;}
}
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
#define mset(A,val) memset(A,val,sizeof(A))
#define fi(a,b) for(int i=a;i<=b;++i)
#define fj(a,b) for(int j=a;j<=b;++j)
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define pii pair<int,int>
#define int long long
// ---------------------------------------------------------------------------
const int mod = 1e9+7;
const int maxn = 2e5 + 9;
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void test_case(int tc)
{
// cout<<"Case #"<<tc<<": ";
int n,w;cin>>n>>w;
int hash[maxn+100];
mset(hash,0);
fi(1,n){
int l,r,c;cin>>l>>r>>c;
l++,r++;
hash[r]-=c;
hash[l]+=c;
}
fi(1,n+9){
hash[i]+=hash[i-1];
if(hash[i]>w){
cout<<"No";
return;
}
}
cout<<"Yes";
}
int32_t main()
{
FASTIO;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
//cin>>t;
for(int tc=1;tc<=t;++tc)test_case(tc);
} |
/******************************
Author: jhnah917(Justice_Hui)
g++ -std=c++17 -DLOCAL
******************************/
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define IDX(v, x) (lower_bound(all(v), x) - v.begin())
using namespace std;
using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
ll S3(ll N, ll S){
ll md = (3 + 3*N) >> 1;
if(S > md) S = 3*N - S + 3;
S -= 2;
if(S <= N) return S*(S+1) / 2;
ll res = N*(N+1) / 2;
S -= N;
res += (S*N - S*(S+1));
return res;
}
ll S2(ll N, ll S){
if(S == 1 || S > 2*N) return 0;
if(S <= N+1) return S - 1;
return 2*N - S + 1;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
ll N, K; cin >> N >> K;
ll S = -1;
for(int i=3; i<=3*N; i++){
if(S3(N, i) < K) K -= S3(N, i);
else{ S = i; break; }
}
ll A;
for(int i=1; i<=N; i++){
if(S2(N, S-i) < K) K -= S2(N, S-i);
else{ A = i; break; }
}
for(int i=1; i<=N; i++){
int a = i, b = S-A-i;
if(a < 1 || b < 1 || a > N || b > N) continue;
if(!--K) cout << A << " " << a << " " << b;
}
} | //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}
const unsigned int mod = 1000000007;
//const unsigned int mod = 998244353;
struct mint {
unsigned int x;
mint():x(0) {}
mint(int64_t x_) {
int64_t v = int64_t(x_ % mod);
if(v < 0) v += mod;
x = (unsigned int)v;
}
static mint row(int v) {
mint v_;
v_.x = v;
return v_;
}
mint operator-() const { return mint(-int64_t(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) {
uint64_t z = x;
z *= a.x;
x = (unsigned int)(z % 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;}
friend bool operator==(const mint &a, const mint &b) {return a.x == b.x;}
friend bool operator!=(const mint &a, const mint &b) {return a.x != b.x;}
mint &operator++() {
x++;
if(x == mod) x = 0;
return *this;
}
mint &operator--() {
if(x == 0) x = mod;
x--;
return *this;
}
mint operator++(int) {
mint result = *this;
++*this;
return result;
}
mint operator--(int) {
mint result = *this;
--*this;
return result;
}
mint pow(int64_t t) const {
mint x_ = *this, r = 1;
while(t) {
if(t&1) r *= x_;
x_ *= x_;
t >>= 1;
}
return r;
}
//for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) {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;}
string to_string(mint a) {
return to_string(a.x);
}
//head
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vi e(n);
rep(i, m) {
int a, b;
cin >> a >> b;
a--; b--;
e[a] ^= 1<<b;
e[b] ^= 1<<a;
}
vi I(1<<n);
I[0] = 1;
for(int i = 1; i < 1<<n; i++) {
int u = __builtin_ctz(i);
I[i] = I[i^(1<<u)] + I[e[u]&i];
}
vector<mint> f(1<<n, mint(1));
for(int k = 1; k < n; k++) {
rep(i, 1<<n) f[i] *= I[i];
mint now;
rep(i, 1<<n) {
if(__builtin_popcount(i)&1) now += f[i];
else now -= f[i];
}
if(now.x) {
cout << k << endl;
return 0;
}
}
cout << n << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
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;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
class UF{
vi data;stack<P> st;
public:
UF(ll n){
data=vi(n,1);
}
bool heigou(ll x,ll y,bool undo=false){
x=root(x);y=root(y);
if(data[x]>data[y])swap(x,y);
if(undo){st.emplace(y,data[y]);st.emplace(x,data[x]);}
if(x==y)return false;
data[y]+=data[x];data[x]=-y;
return true;
}
ll root(ll i){if(data[i]>0)return i;return root(-data[i]);}
ll getsize(ll i){return data[root(i)];}
bool same(ll a,ll b){return root(a)==root(b);}
void undo(){rep(i,2){data[st.top().fi]=st.top().se;st.pop();}}
};
int main(){
ll n,m;cin>>n>>m;
vi a(n),b(n);
UF uf(n);
rep(i,n)cin>>a[i];
rep(i,n)cin>>b[i];
rep(i,m){
ll c,d;cin>>c>>d;
uf.heigou(c-1,d-1);
}
vi ans(n);
rep(i,n)ans[uf.root(i)]+=a[i]-b[i];
rep(i,n)if(ans[i]!=0)dame("No");
out("Yes");
} | # pragma GCC optimize ("O3")
# pragma GCC optimize ("Ofast")
# pragma GCC optimize ("unroll-loops")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define rep2(i,a,b) for(int i = (b) - 1; i >= (a); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb push_back
#define x first
#define y second
using namespace std;
using ll = long long;
using tl = tuple<ll, ll, ll, ll>;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
using ld = long double;
const int MAX = 1e5;
int N, C[MAX], u, v;
vector<int> adj[MAX], ans;
map<int, int> chk;
void dfs(int u, int p){
if(chk[C[u]] == 0) ans.pb(u);
chk[C[u]]++;
for(auto v: adj[u]){
if(v == p) continue;
dfs(v, u);
}
chk[C[u]]--;
}
int main() {
cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
cin >> N;
rep(i, 0, N) cin >> C[i];
rep(i, 1, N){
cin >> u >> v; u--; v--;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(0, -1);
sort(all(ans));
for(auto u: ans) cout << u + 1 << "\n";
}
|
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (int)(n); i ++)
vector <int> dy = {0, 1, 0, -1};
vector <int> dx = {1, 0, -1, 0};
const int INF = 1000000000;
const ll INFLL = 100000000000000000;
long long pow(long long x, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret *= x; // n の最下位bitが 1 ならば x^(2^i) をかける
x *= x;
n >>= 1; // n を1bit 左にずらす
}
return ret;
}
int main(){
int t; cin >> t;
ll n; cin >> n;
int now = 0;
vector <int> now2next(105, -1);
vector <ll> dist;
ll ans = 0;
ll b = 0;
ll pre_b = 0;
ll all = 0;
ll cnt = 0;
while(n > 0){
int tmp = (100 - now + t - 1) / t;
int next = (t * tmp + now) - 100;
if(now2next.at(now) != -1){
break;
}
now2next.at(now) = next;
now = next;
b = tmp;
n --;
all += b;
dist.push_back(b);
pre_b = b;
cnt ++;
//cout << b << endl;
}
//cout << n << endl;
ans = all;
ans += all * (n / cnt);
//cout << ans << endl;
n %= cnt;
for(auto d: dist){
if(n == 0) break;
n--;
//cout << "jfalk" << d << endl;
ans += d;
}
//cout << ans << endl;
cout << ((100 + t) * ans / 100) - 1 << endl;
} | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++)
#define all(x) x.begin(),x.end()
#define STRINGIFY(n) #n
#define TOSTRING(n) STRINGIFY(n)
#define PREFIX "#" TOSTRING(__LINE__) "| "
#define debug(x) \
{ \
std::cout << PREFIX << #x << " = " << x << std::endl; \
}
std::ostream& output_indent(std::ostream& os, int ind) {
for(int i = 0; i < ind; i++) os << " ";
return os;
}
template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p);
template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
template<class S, class T> std::ostream& operator<<(std::ostream& os, const std::pair<S, T>& p) {
return (os << "(" << p.first << ", " << p.second << ")");
}
template<class T> std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for(int i = 0;i < v.size();i++) os << v[i] << ", ";
return (os << "]");
}
template<class T>
static inline std::vector<T> ndvec(size_t&& n, T val) { return std::vector<T>(n, std::forward<T>(val)); }
template<class... Tail>
static inline auto ndvec(size_t&& n, Tail&&... tail) {
return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(n, ndvec(std::forward<Tail>(tail)...));
}
template<class Cond> struct chain {
Cond cond; chain(Cond cond) : cond(cond) {}
template<class T> bool operator()(T& a, const T& b) const { if(cond(a, b)) { a = b; return true; } return false; }
};
template<class Cond> chain<Cond> make_chain(Cond cond) { return chain<Cond>(cond); }
int main() {
i64 N, M;
cin >> N >> M;
vector<i64> H(N);
rep(i,0,N) {
cin >> H[i];
}
sort(all(H));
vector<i64> W(M);
rep(i,0,M) {
cin >> W[i];
}
sort(all(W));
vector<i64> left(N + 1);
for(int i = 2; i < N; i += 2) {
left[i - 1] = left[i - 2];
left[i] = left[i - 2] + H[i - 1] - H[i - 2];
}
left[N] = left[N - 1];
vector<i64> right(N + 1);
for(int i = N - 2; i >= 0; i -= 2) {
right[i + 1] = right[i + 2];
right[i] = right[i + 2] + H[i + 1] - H[i];
}
right[0] = right[1];
i64 res = 1e18;
rep(i,0,N) {
i64 h = H[i];
i64 ans = left[i] + right[i + 1];
if(i % 2 == 1) {
ans += H[i + 1] - H[i - 1];
}
i64 MIN = 1e18;
auto iter = lower_bound(all(W), h);
if(iter != W.end()) {
MIN = std::min(MIN, abs(h - *iter));
}
if(iter != W.begin()) {
MIN = std::min(MIN, abs(h - *(--iter)));
}
res = std::min(res, ans + MIN);
}
cout << res << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
long long n;
struct node
{
int sum;
bool p[30];
int jian;
};
int main()
{
cin>>n;
long long m=n;
int len=0;
int s[25];
while(m>0)
{
s[++len]=m%10;
m/=10;
}
queue<node>q;
node node1;
node1.sum=0;
for(int i=1;i<=len;i++)
{
node1.sum+=s[i];
node1.p[i]=false;
}
node1.jian=0;
q.push(node1);
while(q.size())
{
node node2=q.front();
q.pop();
if( node2.sum!=0 && node2.sum%3==0)
{
cout<<node2.jian<<endl;
return 0;
}
for(int i=1;i<=len;i++)
{
if(node2.p[i]==true) continue;
node node3;
node3.jian=node2.jian+1;
node3.sum=node2.sum-s[i];
memcpy(node3.p,node2.p,sizeof node2.p);
node3.p[i]=true;
q.push(node3);
}
}
cout<<-1<<endl;
} | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i,a,n) for(ll i=a;i<(ll)(n);i++)
int main() {
ll a; cin>>a;
string s = to_string(a);
ll sum = 0;
if(a%3 == 0){
cout << 0 << endl;
return 0;
}
rep(i, s.size()){
sum += s[i] - '0';
}
ll n = s.size();
ll ans = n;
for (ll bit = 0; bit < (1<<n); ++bit) {
ll ss = sum;
ll count = 0;
for (ll i = 0; i < n; ++i) {
if (bit & (1<<i)) {
ss -= s[i]-'0';
count++;
}
}
if(ss != 0 && ss%3 == 0 && count != n){
ans = min(ans, count);
}
}
cout << (ans == n ? -1 : ans) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define loops(i, s, n) for (ll i = s; i < (ll)n; i++)
#define loop(i, n) loops(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define pub push_back
#define pob pop_back
#define mp make_pair
#define dump(x) cerr << #x << " = " << (x) << endl;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vl;
// for dp
// 第一引数と第二引数を比較し、第一引数(a)をより大きい/小さい値に上書き
template <typename T>
inline bool chmin(T &a, const T &b)
{
bool compare = a > b;
if (a > b)
a = b;
return compare;
}
template <typename T>
inline bool chmax(T &a, const T &b)
{
bool compare = a < b;
if (a < b)
a = b;
return compare;
}
#define in_v(type, name, cnt) \
vector<type> name(cnt); \
loop(i, cnt) cin >> name[i];
#define sort_v(v) std::sort(v.begin(), v.end())
#define unique_v(v) v.erase(std::unique(v.begin(), v.end()), v.end()) //必ずソート後に実行
#define set_fix(x) ((std::cerr << std::fixed << std::setprecision(x)), (std::cout << std::fixed << std::setprecision(x)))
//cout for vector
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v)
{
o << "{";
for (int i = 0; i < (int)v.size(); i++)
o << (i > 0 ? ", " : "") << v[i];
o << "}";
return o;
}
// gcd
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return a * b / gcd(a, b);
}
//prime numbers
bool IsPrime(ll num)
{
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false;
double sqrtNum = sqrt(num);
for (ll i = 3; i <= sqrtNum; i += (ll)2)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
vector<ll> Eratosthenes(ll N)
{
int arr[N];
vector<ll> res;
for (int i = 0; i < N; i++)
{
arr[i] = 1;
}
for (int i = 2; i < sqrt(N); i++)
{
if (arr[i])
{
for (int j = 0; i * (j + 2) < N; j++)
{
arr[i * (j + 2)] = 0;
}
}
}
for (int i = 2; i < N; i++)
{
if (arr[i])
{
res.push_back(i);
}
}
return res;
}
//digit number
int GetDigit(ll num, ll radix)
{
unsigned digit = 0;
while (num != 0)
{
num /= radix;
digit++;
}
return digit;
}
int digsum(ll n)
{
ll res = 0;
while (n > 0)
{
res += n % (ll)10;
n /= (ll)10;
}
return res;
}
int main()
{
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(ALL(a));
dump(a);
dump(n / 2);
dump(a[n / 2]);
ld x = n % 2 == 0 ? (double)(a[n / 2 - 1] + a[n / 2]) / (double)4 : a[n / 2] / (double)2;
// cout << "x: " << x << endl;
ld res = 0;
for (int i = 0; i < n; i++)
{
// cout << "res: " << res << endl;
// cout << "min((double)2 * x, (ld)a[i]): " << min((double)2 * x, (ld)a[i]) << endl;
// cout << "a[i]: " << a[i] << endl;
res += x + (double)a[i] - min((double)2 * x, (ld)a[i]);
}
set_fix(20) << (double)res / (double)n << endl;
return 0;
}
| #include <iostream>
#include <iomanip>
#include <assert.h>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<long long, long long> Pll;
typedef std::vector<int> vi;
typedef std::vector<long long> vl;
typedef std::vector<std::string> vs;
typedef std::vector<std::pair<long long, long long>> vpll;
typedef std::vector<std::vector<ll>> vvl;
typedef std::vector<std::vector<std::vector<ll>>> vvvl;
template<class T> using V = std::vector<T>;
template<class T> using VV = std::vector<std::vector<T>>;
template<class T> using PQue = std::priority_queue<T>;
template<class T> using RPQue = std::priority_queue<T, std::vector<T>, std::greater<T>>;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
static const int MAX_INT = std::numeric_limits<int>::max(); // 2147483647 = 2^31-1
static const ll MAX_LL = std::numeric_limits<long long>::max();
static const int INF = (1 << 29); // cf) INT_MAX = 2147483647 = 2^31-1
static const ll INFLL = (1ll << 61); // cf) LLONG_MAX = 9223372036854775807 = 2^63 - 1
#define rep(i,n) REP(i,0,n)
#define REP(i,x,n) for(ll i=(ll)(x);i<(ll)(n);++i)
#define rrep(i,n) RREP(i,0,n)
#define RREP(i,x,n) for(ll i=(ll)(n)-1ll;i>=(ll)(x);--i)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define SUM(x) accumulate(ALL(x), 0ll)
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define debug(var) do{std::cout << #var << " : ";view(var);}while(0)
// view
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
// change min/max
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; }
void Main() {
//ll M; cin >> M;
//ll K; cin >> K;
int N = 3;
vl A(N); rep(i,N) cin >> A[i];
sort(RALL(A));
ll ans = A[0] + A[1];
cout << ans << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed << std::setprecision(15);
Main();
double tmp;
cin >> tmp;
return 0;
}
|
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <map>
#include <assert.h>
#include <stack>
#include <string>
#include <ctime>
#include <chrono>
#include <random>
#define int long long int
using namespace std;
int exp(int x, int n, int p) //x^n%p in log(n)
{
if (n==0)
{
return 1;
}
else if (n==1)
{
return x%p;
}
else
{
if (n%2==0)
{
return (exp((x*x)%p, n/2, p));
}
else
{
return (x*exp((x*x)%p, n/2, p))%p;
}
}
}
int get(int d, int idx)
{
int res=1;
while (idx--)
{
res*=d;
res%=10;
}
return res;
}
int period(int d)
{
int res=d*d;
int p=1;
while (res%10!=d)
{
p++;
res*=d;
}
return p;
}
void solve()
{
int a, b, c;
cin>>a>>b>>c;
cout<<get(a%10, exp(b, c, period(a%10)) ? exp(b, c, period(a % 10) ) : period(a%10))<<"\n";
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
int t;
//cin >> t;
t=1;
while (t--)
{
solve();
}
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>
using namespace std;
using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
{ 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
ll a, b, c;
cin >> a >> b >> c;
if(a % 10 <= 1) {
cout << a % 10 << endl;
return 0;
}
// next[i][j]: 1の位が i の数に a を 2^j 回かけると 1 の位が next[i][j] になる
vector<vector<ll>> next(10, vector<ll>(60, -1));
// next2[i][j]: 1の位が i になるものを 2^j 回かけると 1 の位が next2[i][j] になる
vector<vector<ll>> next2(10, vector<ll>(60, -1));
for(int i=0;i<10;++i) {
next[i][0] = (i * a) % 10;
next2[i][0] = i;
}
for(int j=0;j<59;++j) {
for(int i=0;i<10;++i) {
if(next[i][j] != -1) next[i][j+1] = next[next[i][j]][j];
if(next2[i][j] != -1) next2[i][j+1] = (next2[i][j] * next2[i][j]) % 10;
}
}
// next3[i][j]: 1の位が i になるものを b 乗するのを 2^j 回繰り返す
vector<vector<ll>> next3(10, vector<ll>(60, -1));
for(int i=0;i<10;++i) {
next3[i][0] = 1;
for(int j=59;j>=0;--j) {
if(b & (1LL << j)) next3[i][0] = (next3[i][0] * next2[i][j]) % 10;
}
}
for(int j=0;j<59;++j) {
for(int i=0;i<10;++i) {
if(next3[i][j] != -1) next3[i][j+1] = next3[next3[i][j]][j];
// if(j <= 3) cerr << i << ", " << j << ": " << next3[i][j] << endl;
}
}
// a に ^b を c 回適用
int ans = a % 10;
for(int j=59;j>=0;--j) {
if (c & (1LL << j)) {
ans = next3[ans][j];
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// DEBUG
#ifdef _DEBUG
#define debug(x) cout << #x << ": " << x << endl
#else
#define debug(x)
#endif
// iter
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i <= (int)(b); i++)
#define FORD(i, a, b) for (int i = a; i >= (int)(b); i--)
#define FORA(i, I) for (const auto& i : I)
// vec
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ((int)(x.size()))
// 定数
// 2.147483647×10^{9}:32bit整数のinf
#define INF32 2147483647
// 9.223372036854775807×10^{18}:64bit整数のinf
#define INF64 9223372036854775807
// 問題による
#define MOD 1000000007
int main() {
// 小数の桁数の出力指定
// cout << fixed << setprecision(10);
// 入力の高速化用のコード
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<vector<int>> A(N, vector<int>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> A[i][j];
}
}
vector<int> nums(N - 1);
iota(nums.begin(), nums.end(), 1);
int ans = 0;
do {
int prev = 0;
ll sum = 0;
for (int n : nums) {
sum += A[prev][n];
prev = n;
}
sum += A[prev][0];
debug(sum);
if (sum == K) {
ans++;
}
} while (next_permutation(ALL(nums)));
cout << ans << endl;
return 0;
} | #include <stack>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#define rep1(i, a, n) for (int i = a; i <= n; i++)
#define rep2(i, a, n) for (int i = a; i >= n; i--)
#define mm(a, b) memset(a, b, sizeof(a))
#define elif else if
typedef long long ll;
void mc(int *aa, int *a, int len) { rep1(i, 1, len) * (aa + i) = *(a + i); }
const int INF = 0x7FFFFFFF;
const double G = 10;
const double eps = 1e-6;
const double PI = acos(-1.0);
const int mod = 1e9 + 7;
using namespace std;
struct node
{
int time;
int flag;
} no_a[1010], no_b[1010];
bool cmp(node a,node b)
{
return a.time < b.time;
}
int main()
{
int n;
cin >> n;
rep1(i, 1, n) cin >> no_a[i].time >> no_b[i].time, no_a[i].flag = no_b[i].flag = i;
sort(no_a + 1, no_a + 1 + n, cmp);
sort(no_b + 1, no_b + 1 + n, cmp);
if(no_a[1].flag!=no_b[1].flag)
cout << max(no_a[1].time, no_b[1].time);
else
cout << min(min(max(no_a[1].time, no_b[2].time), max(no_a[2].time, no_b[1].time)), no_a[1].time + no_b[1].time);
return 0;
}
|
#include <bits/stdc++.h>
#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 drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vs = vector<string>;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int mod = 1000000007;
// const int mod = 998244353;
int main() {
int n, q;
cin >> n >> q;
vl a(n);
rep(i, n) cin >> a[i];
vl c(n);
rep(i, n) c[i] = a[i] - i - 1;
rep(_, q) {
ll k;
cin >> k;
ll x = lower_bound(ALL(c), k) - c.begin();
ll ans;
if (x == 0) ans = k;
else ans = a[x-1] + k-c[x-1];
cout << ans << endl;
}
} | #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define read freopen ("in.txt","r",stdin);
#define out freopen ("out.txt","w",stdout);
#define sz(x) int((x).size())
#define rep0(i,k) for (int i=0 ; i<k ; i++)
#define rep(p,i,k) for (int i=p ; i<=k ; i++)
#define reset(a,b) memset(a, (b), sizeof(a))
#define sortv(k) sort(k.begin(),k.end())
#define sortg(k) sort(k.begin(),k.end(),greater<int>())
#define rev(k) reverse(k.begin(),k.end())
#define umin(a,b) a=min(a,b)
#define umax(a,b) a=max(a,b)
#define pyes cout<<"YES"<<endl
#define pno cout<<"NO"<<endl;
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define mpr make_pair
#define pi acos(-1.0)
#define limit 100005
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
constexpr ll imax = 1e9;
constexpr ll imin =-1e9;
constexpr ll mod = 1e9+7;
void solution()
{
int a,b,c;
cin>>a>>b>>c;
cout<<21-(a+b+c);
return;
}
int main()
{
//fast;
//read;
//out;
int tc=1;
//cin>>tc;
while(tc--) solution();
return 0;
}
|
/*
_oo0oo_
o8888888o
88" . "88
(| -_- |)
0\ = /0
___/`---'\___
.' \\| |// '.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' |_/ |
\ .-\__ '-' ___/-. /
___'. .' /--.--\ `. .'___
."" '< `.___\_<|>_/___.' >' "".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `_. \_ __\ /__ _/ .-` / /
=====`-.____`.___ \_____/___.-`___.-'=====
`=---='
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jaspreet singh @jsar3004 codechef/codeforces/leetcode
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include<bits/stdc++.h>
#define needforspeed ios_base::sync_with_stdio(false)
#define ll long long int
#define ld long double
#define ff1(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#define pp pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define in insert
#define PI 3.1415926535
#define mod 1000000007
#define coutd(x) cout << fixed << setprecision(x)
#define DECIMAL(n) std::cout << std::fixed << std::setprecision(n);
using namespace std;
int maxint = numeric_limits<int>::max();
bool sortbysec(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.second < b.second);
}
bool cmp(pair<char, ll>& a,
pair<char, ll>& b)
{
return a.second > b.second;
}
long double distance1(ll x1, ll y1, ll x2, ll y2)
{
return sqrt(pow(x2 - x1, 2)*1.0 +
pow(y2 - y1, 2) * 1.0);
}
long long binpoww(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
long long int binpow(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll digSum(ll n)
{
ll sum = 0;
while(n > 0 || sum > 9)
{
if(n == 0)
{
n = sum;
sum = 0;
}
sum += n % 10;
n /= 10;
}
return sum;
}
bool prime[1000001];
void SieveOfEratosthenes(ll n)
{
memset(prime, true, sizeof(prime));
for (ll p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (ll i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
void solve()
{
ll n,m;
cin>>n>>m;
ll ans=0;
ff1(i,1,n+1)
{
ff1(j,1,m+1)
{
ans+=100*i +j;
}
}
cout<<ans<<endl;
}
int main()
{
needforspeed;
ll t=1;
//SieveOfEratosthenes(1000001);
// cin>>t;
while(t--)
{
solve();
}
return 0;
} | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define lli long long int
#define llu unsigned long long int
#define pb push_back
#define rt return 0
#define endln "\n"
#define all(x) x.begin(), x.end()
#define sz(x) (lli)(x.size())
const lli MOD = 1e9 + 7;
const double PI = 2 * acos(0.0);
// cout << fixed << setprecision(0) << pi <<endl;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update>
// new_data_set;
// for multiset
// typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
// tree_order_statistics_node_update>
// new_data_set;
// order_of_key(val): returns the number of values less than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)
void solve() {
lli n, a, t;
cin>>n>>t;
vector<lli> v;
for (lli i = 0; i < n; i++) {
cin >> a;
v.pb(a);
}
lli ans = 0;
if(n <= 20){
for (lli i = 0; i < pow(2, n); i++) {
lli tmp = i, val = 0, cnt = 0;
while(tmp > 0){
if(tmp % 2){
val += v[cnt];
}
tmp /= 2;
cnt++;
}
if(val <= t){
ans = max(ans, val);
}
}
cout<<ans<<endl;
return;
}
vector<lli> store1, store2;
for (lli i = 0; i < pow(2, 20); i++) {
lli tmp = i, val = 0, cnt = 0;
while(tmp > 0){
if(tmp % 2){
val += v[cnt];
}
tmp /= 2;
cnt++;
}
if(val <= t){
store1.pb(val);
}
}
for (lli i = 0; i < pow(2, n - 20); i++) {
lli tmp = i, val = 0, cnt = 20;
while(tmp > 0){
if(tmp % 2){
val += v[cnt];
}
tmp /= 2;
cnt++;
}
if(val <= t){
store2.pb(val);
}
}
sort(all(store2));
for (lli i = 0; i < sz(store1); i++) {
lli val = store1[i];
auto it = upper_bound(store2.begin(), store2.end(), t - val);
lli ind = it - store2.begin();
ind--;
if(it == store2.end()){
ind = sz(store2) - 1;
}
if(ind >= 0){
val += store2[ind];
}
ans = max(ans, val);
}
cout<<ans<<endl;
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// lli t; cin >> t; while(t--)
solve();
rt;
} |
#include <bits/stdc++.h>
using namespace std;
#define fo(i, x, y) for (int i = (x); i <= (y); ++i)
#define fd(i, x, y) for (int i = (x); i >= (y); --i)
const int maxn = 2e5 + 5;
struct edge{int to, w; edge *nxt;}*arc1[maxn], *arc2[maxn], pool[maxn << 2], *pt = pool;
int n, m;
int ans[maxn];
bool vis[maxn];
int getint()
{
char ch;
int res = 0, p;
while (!isdigit(ch = getchar()) && (ch ^ '-'));
p = ch == '-'? ch = getchar(), -1 : 1;
while (isdigit(ch))
res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return res * p;
}
void addedge(edge *arc[], int u, int v, int w)
{
*pt = (edge){v, w, arc[u]};
arc[u] = pt++;
}
void gettr(int x)
{
vis[x] = true;
for (edge *e = arc1[x]; e; e = e->nxt)
if (!vis[e->to])
{
addedge(arc2, x, e->to, e->w);
gettr(e->to);
}
}
void dfs(int x)
{
for (edge *e = arc2[x]; e; e = e->nxt)
{
if (e->w != ans[x])
ans[e->to] = e->w;
else
ans[e->to] = e->w - 1? e->w - 1 : e->w + 1;
dfs(e->to);
}
}
int main()
{
n = getint(); m = getint();
fo(i, 1, m)
{
int u, v, w;
u = getint(); v = getint(); w = getint();
addedge(arc1, u, v, w);
addedge(arc1, v, u, w);
}
gettr(1);
ans[1] = 1;
dfs(1);
fo(i, 1, n) printf("%d\n", ans[i]);
return 0;
} | #include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
long long n, m, c, u, v, l;
vector<long long>g[100000];
int b[100000];
int x[100000];
int y[100000];
void dfs(int a, int p, int q) {
b[a] = 1;
int wa, wp, wq;
for (int i = 0; i < g[a].size(); i++) {
wa = g[a][i] % n;
wp = g[a][i] / n;
if (b[wa])continue;
if (p == wp)wq = q + 1;
else wq = 0;
if (wq % 2 == 0) {
x[wa] = wp;
}
dfs(wa, wp, wq);
}
}
int main() {
scanf("%lld%lld", &n, &m);
for (long long i = 0; i < m; i++) {
scanf("%lld%lld%lld", &u, &v, &c);
c--;
u--;
v--;
g[u].push_back(c * n + v);
g[v].push_back(c * n + u);
}
for (long long i = 0; i < n; i++)x[i] = -1;
dfs(0, -1, 0);
for (int i = 0; i < n; i++) {
if (x[i] >= 0)y[x[i]]++;
}
while (y[l])l++;
for (int i = 0; i < n; i++) {
if (x[i] == -1)x[i] = l;
}
for (int i = 0; i < n; i++) {
printf("%d\n", x[i] + 1);
}
} |
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<int>(1000,10000)(rng)
ll binpow(ll a, ll b)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll gcd(ll a,ll b)
{
if (b==0) return a;
return gcd(b,a%b);
}
string to_upper(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
return a;
}
string to_lower(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
return a;
}
constexpr long long safe_mod(long long x, long long m) {
x %= m;
if (x < 0) x += m;
return x;
}
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
// Contracts:
// [1] s - m0 * a = 0 (mod b)
// [2] t - m1 * a = 0 (mod b)
// [3] s * |m1| + t * |m0| <= b
long long s = b, t = a;
long long m0 = 0, m1 = 1;
while (t) {
long long u = s / t;
s -= t * u;
m0 -= m1 * u; // |m1 * u| <= |m1| * s <= b
// [3]:
// (s - t * u) * |m1| + t * |m0 - m1 * u|
// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
// = s * |m1| + t * |m0| <= b
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
// by [3]: |m0| <= b/g
// by g != b: |m0| < b/g
if (m0 < 0) m0 += b / s;
return {s, m0};
}
std::pair<long long, long long> crt(const std::vector<long long>& r,
const std::vector<long long>& m) {
assert(r.size() == m.size());
int n = int(r.size());
// Contracts: 0 <= r0 < m0
long long r0 = 0, m0 = 1;
for (int i = 0; i < n; i++) {
assert(1 <= m[i]);
long long r1 = safe_mod(r[i], m[i]), m1 = m[i];
if (m0 < m1) {
std::swap(r0, r1);
std::swap(m0, m1);
}
if (m0 % m1 == 0) {
if (r0 % m1 != r1) return {0, 0};
continue;
}
// assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1)
// (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1));
// r2 % m0 = r0
// r2 % m1 = r1
// -> (r0 + x*m0) % m1 = r1
// -> x*u0*g = r1-r0 (mod u1*g) (u0*g = m0, u1*g = m1)
// -> x = (r1 - r0) / g * inv(u0) (mod u1)
// im = inv(u0) (mod u1) (0 <= im < u1)
long long g, im;
std::tie(g, im) = inv_gcd(m0, m1);
long long u1 = (m1 / g);
// |r1 - r0| < (m0 + m1) <= lcm(m0, m1)
if ((r1 - r0) % g) return {0, 0};
// u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1)
long long x = (r1 - r0) / g % u1 * im % u1;
// |r0| + |m0 * x|
// < m0 + m0 * (u1 - 1)
// = m0 + m0 * m1 / g - m0
// = lcm(m0, m1)
r0 += x * m0;
m0 *= u1; // -> lcm(m0, m1)
if (r0 < 0) r0 += m0;
}
return {r0, m0};
}
void solve()
{
ll x,y,p,q,ans=LLONG_MAX;
cin>>x>>y>>p>>q;
pair<ll,ll> rv;
vector<ll> r(2),m(2);
m[0]=2*(x+y);
m[1]=p+q;
for (int i=x;i<x+y;++i)
{
r[0]=i;
for (int j=p;j<p+q;++j)
{
r[1]=j;
rv=crt(r,m);
if (rv.first==0&&rv.second==0)
continue;
if (rv.first==0)
ans=min({ans,rv.second});
else
ans=min({ans,rv.first});
}
}
if (ans==LLONG_MAX)
cout<<"infinity\n";
else
cout<<ans<<"\n";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int t;
cin>>t;
while (t--)
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin >> N;
string s;
vector<string> S(0);
vector<string> aS(0);
int i = 0;
int j = 0;
bool imp = false;
for(int i = 0; i < N; i++)
{
cin >> s;
if(s.at(0) == '!') aS.push_back(s.substr(1));
else S.push_back(s);
}
if(N == 1) cout << "satisfiable" << endl;
else
{
sort(S.begin(), S.end());
sort(aS.begin(), aS.end());
for( ; ; )
{
if(S.size() == i || aS.size() == j)
{
imp = true;
break;
}
if(S.at(i) == aS.at(j))
{
cout << S.at(i) << endl;
break;
}
else
{
if(S.at(i) < aS.at(j)) i++;
else j++;
}
}
if(imp) cout << "satisfiable" << endl;
}
} |
#include <bits/stdc++.h>
#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 drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int mod = 1000000007;
// const int mod = 998244353;
int trans(string s) {
int res = 0;
rep(i, s.size()) {
res += pow(10, (int)s.size() -1 - i) * (s[i] - '0');
}
return res;
}
int main() {
int n, k;
cin >> n >> k;
while (k--) {
string g = to_string(n);
sort(ALL(g));
string g2 = g;
reverse(ALL(g));
string g1 = g;
int f = trans(g1) - trans(g2);
n = f;
}
cout << n << endl;
} | #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ri int
typedef long long ll;
const int maxn=110;
int a[maxn],b[maxn][maxn],n;
ll ans,x;
int main(){
scanf("%d%lld",&n,&x);
for(ri i=1;i<=n;++i)scanf("%d",a+i);
sort(a+1,a+n+1);
ll ans=x-a[n];
for(ri i=2;i<=n;++i){
memset(b,0,sizeof b);
for(ri j=1;j<=n;++j){
int tmp=a[j]%i;
for(ri l=i;l>1;--l)
for(ri k=0;k<i;++k)
if(b[k][l-1])
b[(k+tmp)%i][l]=max(b[(k+tmp)%i][l],b[k][l-1]+a[j]);
b[tmp][1]=max(b[tmp][1],a[j]);
}
if(b[x%i][i])ans=min(ans,(x-b[x%i][i])/i);
}
printf("%lld",ans);
return 0;
} |
//IQ134高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007
#define MODB 998244353
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
for (T& x: vec) { is >> x; }
return is;
}
set<int> col;
vector<int> cs;
map<int, ll> mi, ma;
int main() {
ll ans = 0;
ll tmp = 0;
int n; cin >> n;
ll x; int c;
REP(i, n){
cin >> x >> c;
if(col.find(c) == col.end()){
col.insert(c);
cs.push_back(c);
mi[c] = x;
ma[c] = x;
}
else{
mi[c] = min(x, mi[c]);
ma[c] = max(x, ma[c]);
}
}
col.insert(0);
cs.push_back(0);
mi[0] = 0;
ma[0] = 0;
col.insert(200001);
cs.push_back(200001);
mi[200001] = 0;
ma[200001] = 0;
ll dp[2][2] = {};
sort(cs.begin(), cs.end());
REP(i, cs.size() - 1){
dp[1][0] = min( dp[0][0] + abs(mi[cs[i]]-ma[cs[i+1]]) + abs(ma[cs[i+1]]-mi[cs[i+1]]),
dp[0][1] + abs(ma[cs[i]]-ma[cs[i+1]]) + abs(ma[cs[i+1]]-mi[cs[i+1]]) );
dp[1][1] = min( dp[0][0] + abs(mi[cs[i]]-mi[cs[i+1]]) + abs(ma[cs[i+1]]-mi[cs[i+1]]),
dp[0][1] + abs(ma[cs[i]]-mi[cs[i+1]]) + abs(ma[cs[i+1]]-mi[cs[i+1]]) );
swap(dp[0][0], dp[1][0]);
swap(dp[0][1], dp[1][1]);
}
cout << min(dp[0][0], dp[0][1]) << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
const int maxn = 1e6 + 7;
const ll inf = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
map<ll,set<ll>> mp;
for (int i = 0; i < n; i++) {
ll x,c;
cin >> x >> c;
mp[c].insert(x);
}
vector<ll> dp = {0,0};
vector<ll> at = {0,0};
mp[inf].insert(0);
for (auto p: mp) {
ll lval = *p.second.begin();
ll rval = *p.second.rbegin();
vector<ll> ndp = {inf,inf};
vector<ll> nat = {lval,rval};
ll totval = rval-lval;
for (int i = 0; i < 2; i++) { // cur
for (int j = 0; j < 2; j++) { // prev
ndp[i] = min(ndp[i], dp[j]+totval+abs(nat[i^1]-at[j]));
}
}
dp = ndp;
at = nat;
}
ll ans = min(dp[0],dp[1]);
cout << ans << "\n";
return 0;
}
|
#pragma GCC optimize("Ofast")
//#ifndef ONLINE_JUDGE
//#define _GLIBCXX_DEBUG
//#endif
#ifdef ONLINE_JUDGE
#include <atcoder/all>
#endif
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#include <math.h>
#include <complex>
using namespace std;
#ifdef ONLINE_JUDGE
using namespace atcoder;
#endif
#define rep(i,n) for (int i = 0;i < (int)(n);i++)
using ll = long long;
#ifdef ONLINE_JUDGE
//using mint = modint998244353;
//using mint = modint;
using mint = modint1000000007;
#endif
const ll MOD=1000000007;
//const ll MOD=998244353;
const long long INF = 1LL << 60;
const double pi=acos(-1.0);
int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
int dy[9] = {0, 1, 0, -1, 1, -1, -1, 1, 0};
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; }
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
// cout << fixed << setprecision(15);
ll H,W; cin>>H>>W;
vector<vector<int>> A(H,vector<int>(W));
rep(i,H) rep(j,W) {
char c; cin>>c;
A[i][j]=2*(c=='+')-1;
}
vector<vector<ll>> dp(H,vector<ll>(W,-INF));
for(int i=H-1;i>=0;i--) for(int j=W-1;j>=0;j--) {
if(i==H-1&&j==W-1) {
dp[i][j]=0;
continue;
}
if(i<H-1) chmax(dp[i][j],-dp[i+1][j]+A[i+1][j]);
if(j<W-1) chmax(dp[i][j],-dp[i][j+1]+A[i][j+1]);
}
if(dp[0][0]>0) cout<<"Takahashi\n";
else if(dp[0][0]<0) cout<<"Aoki\n";
else cout<<"Draw\n";
/*
rep(i,H) {
rep(j,W) cout<<dp[i][j]<<' ';
cout<<'\n';
}
*/
return 0;
} | #include <iostream> // cout, endl, cin
#include <cmath> //sqrt pow
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // dequef
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <climits>
#define rep(i, n) for (int i = 0; i < n; i++)
using ll = long long;
using ld = long double;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define mod 1000000007
using namespace std;
const int inf = -1000000000;
int main(){
int n,m;
cin >> n >> m;
vi a(n);
vector<pii> small(n);
rep(i, n){
cin >> a[i];
small[i] = make_pair(a[i], i);
}
vvi root(n);
rep(i, m){
int a, b;
cin >> a >> b;
root[a - 1].push_back(b - 1);
}
sort(all(small));
int ans = inf;
queue<int> que;
vi checked(n, -1);
rep(i, n){
int buy, start;
tie(buy, start) = small[i];
if(checked[start] != -1) continue;
checked[start] = 0;
que.push(start);
while(!que.empty()){
int pos = que.front();
que.pop();
for(int x : root[pos]){
if(checked[x] != -1) continue;
checked[x] = 1;
ans = max(ans, a[x] - buy);
que.push(x);
}
}
}
if(ans != inf){
cout << ans << endl;
return 0;
}
//頂点の番号が小さい<=>値が大きい
checked = vi(n, -1);
for(int i = n - 1; i >= 0; i--){
int buy, start;
tie(buy, start) = small[i];
if(checked[start] != -1) continue;
checked[start] = 0;
que.push(start);
while(!que.empty()){
int pos = que.front();
que.pop();
for(int x : root[pos]){
ans = max(ans, a[x] - buy);
que.push(x);
}
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
double EPS=1e-9;
int INF=1000000005;
long long INFF=1000000000000000005ll;
double PI=acos(-1);
int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 };
ll MOD = 1000000007;
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << '\n'
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl
template<typename T_vector>
void output_vector(const T_vector &v, bool line_break = false, 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) << (line_break ? '\n' : i < end - 1 ? ' ' : '\n');
}
}
vvi g, l;
vi in, out, depth;
int timer;
void dfs(int u) {
in[u] = timer++;
l[depth[u]].pb(in[u]);
EACH(v, g[u]) {
depth[v] = depth[u] + 1;
dfs(v);
}
out[u] = timer++;
}
void solve() {
int n; cin >> n;
g = l = vvi(n);
in = out = depth = vi(n);
timer = 0;
REP(i, n - 1) {
int p; cin >> p;
g[p - 1].pb(i + 1);
}
dfs(0);
int q; cin >> q;
REP(i, q) {
int u, d; cin >> u >> d;
--u;
auto v = l[d];
auto l = lower_bound(ALL(v), in[u]);
auto r = lower_bound(ALL(v), out[u]);
OUT(r - l);
}
}
int main()
{
FAST_INP;
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r", stdin);
// freopen("output.txt","w", stdout);
// #endif
// int tc; cin >> tc;
// TC(tc) solve();
solve();
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;
pll operator+(pll a,pll b){
return pll(a.first+b.first,a.second+b.second);
}
pll operator-(pll a,pll b){
return pll(a.first-b.first,a.second-b.second);
}
pll operator*(ll a,pll b){
return pll(b.first*a,b.second*a);
}
const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
a=abs(a);
b=abs(b);
if(a==0)return b;
if(b==0)return a;
if(a < b) return gcd(b, a);
ll r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
if(a<b){
a=b;
return true;
}
return false;
}
template<class T>
bool chmin(T& a,const T& b){
if(a>b){
a=b;
return true;
}
return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
os << "(" << a.first << "," << a.second << ")";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
os << "[ ";
REP(a.size()){
os<< a[i] << " ";
}
os<< " ]";
return os;
}
const string YES = "Yes";
const string NO = "No";
class UF{
vector<long> par,rank,size_uf;
long _chunk_size;
public:
UF(long size):_chunk_size(size){
for (long i = 0; i < size; i++) {
par.push_back(i);
rank.push_back(0);
size_uf.push_back(1);
}
}
long find(long x){
if(par[x]==x)return x;
return par[x]=find(par[x]);
}
void unite(long x,long y){
x=find(x);
y=find(y);
if(x==y)return;
--_chunk_size;
if(rank[x]<rank[y]){
par[x]=y;
size_uf[y]+=size_uf[x];
}
else{
par[y]=x;
size_uf[x]+=size_uf[y];
if(rank[x]==rank[y])rank[x]++;
}
}
bool same(long x,long y){
return find(x)==find(y);
}
long operator [](long x){
return this->find(x);
}
long size(long x){
x=find(x);
return size_uf[x];
}
long chunk_size(){
return this->_chunk_size;
}
};
void solve(long long N, long long M, std::vector<long long> a, std::vector<long long> b, std::vector<long long> c, std::vector<long long> d){
UF uf(N);
REP(M){
uf.unite(c[i],d[i]);
}
vector<ll> suma(N),sumb(N);
REP(N){
suma[uf.find(i)]+=a[i];
sumb[uf.find(i)]+=b[i];
}
if(suma==sumb){
cout<<YES<<endl;
}else{
cout<<NO<<endl;
}
}
int main(){
cout<<setprecision(1000);
long long N;
scanf("%lld",&N);
long long M;
scanf("%lld",&M);
std::vector<long long> a(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld",&a[i]);
}
std::vector<long long> b(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld",&b[i]);
}
std::vector<long long> c(M);
std::vector<long long> d(M);
for(int i = 0 ; i < M ; i++){
scanf("%lld",&c[i]);
scanf("%lld",&d[i]);
--c[i];--d[i];
}
solve(N, M, std::move(a), std::move(b), std::move(c), std::move(d));
return 0;
}
|
#include<iostream>
using namespace std;
int main()
{
string s;
cin>>s;
int c=0;
for(int i=0;i<s.length()-1;i++)
{
if(s[i]==s[i+1])
{
c++;
}
}
if(c==2)
{
cout<<"Won";
}else
{
cout<<"Lost";
}
} | #include <iomanip>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::string str;
std::cin >> str;
for (auto c : str) {
if (c == '.')
break;
std::cout << c;
}
std::cout << "\n";
return 0;
}
|
/*
ALLAH is Almighty....
*/
#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 pi acos( -1.0 )
#define rep( i, a, n ) for ( ll i = a; i < n; i++ )
#define per( i, a, n ) for ( ll i = n - 1; i >= a; i-- )
#define ll long long
#define all( x ) ( x ).begin(), ( x ).end()
typedef tree < ll, null_type, less < ll >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
const ll N = 2e5 + 9;
const ll MOD = 1e9 + 7;
ll sum( string x )
{
return ( x[ 0 ] - '0' ) + ( x[ 1 ] - '0' ) + ( x[ 2 ] - '0' );
}
void solve( int t )
{
string A, B;
cin >> A >> B;
cout << max( sum( A ), sum( B ) );
}
int main()
{
ios_base::sync_with_stdio( false );
cin.tie( NULL );
cout.tie( NULL );
cout << setprecision( 12 );
int t = 1;
//cin >> t;
for ( int i = 1; i <= t; ++i ) solve( i );
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;
ll MOD=1000000007;
int main(){
int n;
cin>>n;
vector<int> a(n);
rep(i,n) cin>>a[i];
sort(all(a));
int m=a[n-1];
vector<int> d(m+1);
rep(i,n){
rep2(j,2,a[i]+1){
if(a[i]%j==0) d[j]++;
}
}
ll max=0;
rep(i,m+1){
if(d[i]>max) max=d[i];
}
rep(i,m+1){
if(d[i]==max){
cout<<i;
return 0;
}
}
return 0;
} |
// D - Send More Money
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define vec vector
#define rep(i,e) for(int i=0;i<(e);++i)
#define sz(a) int(a.size())
ll INF = 1e18;
string in(){ string s; cin>>s; return s; }
vec<int> reversed(vec<int> s){ reverse(s.begin(), s.end()); return s; }
vec<vec<int>> S(3);
vec<ll> A(26, INF), sign{1, 1, -1};
vec<int> C;
vec<ll> ans(3);
bool dfs(set<int>&t, string P){
if(sz(P) == sz(C)){
ll tot = 0;
rep(i, sz(C)){
if(P[i] == '0')
for(auto&s:S) if(s[0] == C[i]) return false;
tot += (P[i]-'0') * A[C[i]];
}
if(tot) return false;
rep(i, sz(C)) A[C[i]] = P[i]-'0';
rep(i, 3) for(auto&c:S[i]) ans[i] = ans[i]*10 + A[c];
return true;
}
set<int> u = t;
for(auto&x:t){
u.erase(x);
if(dfs(u, P + to_string(x))) return true;
u.insert(x);
}
return false;
}
int main(){
rep(i, 3){
for(auto&c:in()) S[i].push_back(c-'a');
int w = 1;
for(auto&c:reversed(S[i])){
if(A[c] == INF){
A[c] = 0;
C.push_back(c);
}
A[c] += sign[i] * w;
w *= 10;
}
}
if(sz(C) > 10){ puts("UNSOLVABLE"); return 0; }
set<int> t{0,1,2,3,4,5,6,7,8,9};
if(dfs(t, "")) for(auto&x:ans) cout<< x <<endl;
else puts("UNSOLVABLE");
}
| #include <bits/stdc++.h>
#include <unordered_set>
#include <cmath>
#include <algorithm>
// URL: https://atcoder.jp/contests/abc198/tasks/abc198_d
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 dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n)? "YES": "NO") << endl
#define Yes(n) cout << ((n)? "Yes": "No") << endl
#define FOR(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fore(x, a) for(auto& (x) : (a))
#define FORL(i, a, b) for(ll i = (ll)(a); i < (ll)(b); i++)
#define repll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define VECCIN(x) for(auto& youso_: (x)) cin >> youso_
#define VECCOUT(x) for(auto& youso_: (x)) cout << youso_ << " ";cout << endl
#define pb push_back
#define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
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 INFL = numeric_limits<ll>::max() / 4;
const int INF = 1e9;
const int MOD = 1e9 + 7;
string S1, S2, S3;
vector<char> chars;
char used[10] = {0};
// ll pow10(int n){
// ll ans = 1;
// rep(i, n) ans *= 10;
// return ans;
// }
bool check() {
map<char, char> mp;
rep(i, 10){
if(used[i] != 0) mp[used[i]] = char('0' + i);
}
string SS1, SS2, SS3;
fore(c, S1) SS1 += mp[c];
fore(c, S2) SS2 += mp[c];
fore(c, S3) SS3 += mp[c];
if(to_string(stoll(SS1)) != SS1) return false;
if(to_string(stoll(SS2)) != SS2) return false;
if(to_string(stoll(SS3)) != SS3) return false;
if(stoll(SS1) + stoll(SS2) != stoll(SS3)) return false;
if(stoll(SS1) == 0) return false;
if(stoll(SS2) == 0) return false;
if(stoll(SS3) == 0) return false;
cout << SS1 << endl;
cout << SS2 << endl;
cout << SS3 << endl;
return true;
}
bool dfs(int cn){
if(cn == chars.size()) return check();
rep(i, 10) if(used[i] == 0){
used[i] = chars[cn];
if(dfs(cn + 1)) return true;
used[i] = 0;
}
return false;
}
int main(){
optimize_cin();
cin >> S1 >> S2 >> S3;
fore(a, S1) chars.pb(a);
fore(a, S2) chars.pb(a);
fore(a, S3) chars.pb(a);
sort(ALL(chars));
chars.erase(unique(ALL(chars)), chars.end());
if(chars.size() > 10) {
cout << "UNSOLVABLE" << endl;
return 0;
}
// dump(chars.size());
bool res = dfs(0);
if(res == false) cout << "UNSOLVABLE" << endl;
// cout << pow10(0) << endl;
return 0;
} |
#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\n"
typedef long long ll;
using namespace std;
int static fast = [](){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0); return 0;
}();
// freopen("input.txt", "r", stdin);
int main() {
int n, m;
ll mod = 1e9 + 7;
cin >> n >> m;
vector<string> grid;
string s;
for(int i = 0; i < n; i++) {
cin >> s;
grid.push_back(s);
}
vector<vector<ll>> dp(n, vector<ll>(m, 0));
vector<vector<ll>> v(n, vector<ll>(m, 0));
vector<vector<ll>> h(n, vector<ll>(m, 0));
vector<vector<ll>> d(n, vector<ll>(m, 0));
dp[0][0] = v[0][0] = h[0][0] = d[0][0] = 1;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if (grid[i][j] == '.') {
if (i-1 >= 0 && grid[i-1][j] == '.') {
dp[i][j] += h[i-1][j];
dp[i][j] %= mod;
}
if (j-1 >= 0 && grid[i][j-1] == '.') {
dp[i][j] += v[i][j-1];
dp[i][j] %= mod;
}
if (i-1 >= 0 && j-1 >= 0 && grid[i-1][j-1] == '.') {
dp[i][j] += d[i-1][j-1];
dp[i][j] %= mod;
}
h[i][j] = v[i][j] = d[i][j] = dp[i][j];
if (i-1 >= 0 && grid[i-1][j] == '.') {
h[i][j] += h[i-1][j];
h[i][j] %= mod;
}
if (j-1 >= 0 && grid[i][j-1] == '.') {
v[i][j] += v[i][j-1];
v[i][j] %= mod;
}
if (i-1 >= 0 && j-1 >= 0 && grid[i-1][j-1] == '.') {
d[i][j] += d[i-1][j-1];
d[i][j] %= mod;
}
}
// cout << dp[i][j] << " ";
}
// cout << endl;
}
cout << dp[n-1][m-1] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int M = 998244353;
const int N = 3001;
long dp[N][N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
for (int i = 0; i < N; ++i) {
dp[i][i] = 1;
}
for (int o = 1; o < N; ++o) {
long s = 0;
for (int i = 1; i+o < N; ++i) {
if (2*i <= o+i) s = (s+dp[o+i][2*i])%M;
dp[o+i][i] = s;
}
}
cout << dp[n][k] << endl;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define IO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
using namespace __gnu_pbds;
using namespace std;
using ld = long double;
using ll = long long;
const int mod = 998244353;
int n, m, k;
ll dp[5000][5000];
string a[5000];
ll modpow(ll x, ll y){
if(!y) return 1;
ll res = modpow(x, y / 2);
res = (res * res) % mod;
if(y % 2) res = (res * x) % mod;
return res;
}
int main(){
//IO("input.txt", "output.txt");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m >> k;
for(int i = 0; i < n; i++) a[i] = string(m, ' ');
for(int i = 0; i < k; i++){
int h, w;
char c;
cin >> h >> w >> c;
a[h - 1][w - 1] = c;
}
dp[0][0] = modpow(3, n * m - k);
int inv = modpow(3, mod - 2);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(i == n - 1 && j == m - 1) cout << dp[i][j] << '\n';
else if(a[i][j] == ' '){
if(i + 1 < n)
dp[i + 1][j] = (dp[i + 1][j] + ((dp[i][j] * 2) % mod * inv) % mod) % mod;
if(j + 1 < m)
dp[i][j + 1] = (dp[i][j + 1] + ((dp[i][j] * 2) % mod * inv) % mod) % mod;
}else{
if(i + 1 < n && (a[i][j] == 'X' || a[i][j] == 'D'))
dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod;
if(j + 1 < m && (a[i][j] == 'X' || a[i][j] == 'R'))
dp[i][j + 1] = (dp[i][j + 1] + dp[i][j]) % mod;
}
return 0;
}
/*
for a path:
3 ^ (nm - k - empty_in_path) * 2 ^ (empty_in_path)
dp[i + 1][j]
XD
D D
R
*/ | #include <iostream>
#include <vector>
#define ll long long
#define mod % 998244353
#define UNUSE 998244354991
using namespace std;
vector<ll> multi(int a){
vector<ll> ans(a+1);
ans[0] = 1;
for(int i = 0;i < a;i ++){
ans[i+1] = (ans[i]*3) mod;
}
return ans;
}
int main(void){
int high,wide;
cin >> high;
cin >> wide;
int kaki;
cin >> kaki;
vector<vector<char>> bo(high,vector<char>(wide,'N'));
for(int i = 0;i < kaki;i ++){
int p,q;
char r;
cin >> p;
cin >> q;
cin >> r;
bo[p-1][q-1] = r;
}
vector<vector<ll>> ans(high+1,vector<ll>(wide+1,0));
vector<vector<ll>> rem(high+1,vector<ll>(wide+1,UNUSE ));
ans[0][0] = 1;
rem[0][0] = high*wide-kaki;
vector<ll> multis = multi(high*wide-kaki);
for(int i = 0;i < high;i ++){
for(int j = 0;j < wide;j ++){
if(i == high-1 and j == wide-1){
break;
}
if(ans[i][j] == 0){
continue;
}
if(bo[i][j] == 'N'){
rem[i][j] --;
ans[i][j] = (ans[i][j] * 2) mod;
bo[i][j] = 'X';
}
if(bo[i][j] == 'D' or bo[i][j] =='X'){
rem[i+1][j] = rem[i][j];
ans[i+1][j] = ans[i][j] mod;
}
if(bo[i][j] == 'R' or bo[i][j] == 'X'){
ll mult;
if(rem[i][j+1] != UNUSE){
if(rem[i][j] > rem[i][j+1]){
mult = multis[rem[i][j]-rem[i][j+1]];
ans[i][j+1] = (mult * ans[i][j] + ans[i][j+1]) mod;
}else{
mult = multis[rem[i][j+1] - rem[i][j]];
rem[i][j+1] = rem[i][j];
ans[i][j+1] = (mult*ans[i][j+1] + ans[i][j]) mod;
}
}else{
rem[i][j+1] = rem[i][j];
ans[i][j+1] = ans[i][j] mod;
}
}
}
}
ll fina;
if(rem[high-1][wide-1] == UNUSE){
fina = 0;
}else{
fina = (ans[high-1][wide-1] * multis[rem[high-1][wide-1]]) mod;
}
cout << fina << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int arr[300005];
map<ll, ll> st;
int main() {
int N;
scanf("%d", &N);
for (int i = 1; i <= N; ++i)
scanf("%d", &arr[i]);
ll mv = 0;
ll ans = 0;
for (int i = 1; i <= N; ++i) {
if (i % 2 == 1) {
st[-mv]++;
mv -= arr[i];
ans += st[-mv];
}
else {
st[-mv]++;
mv += arr[i];
ans += st[-mv];
}
}
printf("%lld\n", ans);
return 0;
} | //...Bismillahir Rahmanir Rahim. . .
#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;
// typedefs...
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>pref_trie;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
// constants...
const double PI = acos(-1);
const ll mod = 1000000007; // 998244353;
const int MXS = 2e5+5;
const ll MXI = 1e9+5;
const ll MXL = 1e18+5;
const ll INF = 1e9+5;
const ll INFLL = 1e18+5;
const ll EPS = 1e-9;
// defines...
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define ordered_set tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define boost_ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define iter_(i,n) for (int i = 0; i < int(n); i++)
#define for_n(i, n) for (int i = 1; i <= int(n); i++)
#define print_array(a) for(int i=0;i<n;i++) cout<<a[i]<<" ";
#define rev(i,n) for(int i=n;i>=0;i--)
#define itr ::iterator
#define s_sort(s) sort(s.begin(),s.end())
#define n_sort(a, n) sort(a,a+n)
#define precise_impact cout<<setprecision(10)<<fixed;
#define endl "\n"
// functions...
ll gcd(ll a, ll b){ while (b){ a %= b; swap(a, b);} return a;}
ll lcm(ll a, ll b){ return (a/gcd(a, b)*b);}
ll ncr(ll a, ll b){ ll x = max(a-b, b), ans=1; for(ll K=a, L=1; K>=x+1; K--, L++){ ans = ans * K; ans /= L;} return ans;}
ll bigmod(ll a,ll b){ if(b==0){ return 1;} ll tm=bigmod(a,b/2); tm=(tm*tm)%mod; if(b%2==1) tm=(tm*a)%mod; return tm;}
ll egcd(ll a,ll b,ll &x,ll &y){ if(a==0){ x=0; y=1; return b;} ll x1,y1; ll d=egcd(b%a,a,x1,y1); x=y1-(b/a)*x1; y=x1; return d;}
int main()
{
ll n;
cin>>n;
map<ll,ll> m;
ll ans=0,sum=0;
m[0]=1;
for(int i=0;i<n;i++)
{
ll x;
cin>>x;
if(i%2==0) sum+=x;
else sum-=x;
ans=ans+m[sum];
m[sum]++;
}
cout<<ans<<endl;
}
|
//Code by Ritik Agarwal
#include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)(x).size()
#define int long long int
#define loop(i,a,b) for(int i=a;i<b;i++)
#define scan(arr,n) for (int i = 0; i < n; ++i) cin >> arr[i]
#define vi vector<int>
#define si set<int>
#define pii pair <int, int>
#define sii set<pii>
#define vii vector<pii>
#define mii map <int, int>
#define pb push_back
#define ff first
#define ss second
#define all(aa) aa.begin(), aa.end()
#define rall(a) a.rbegin() , a.rend()
#define read(a,b) int a,b; cin>>a>>b
#define readt(a,b,c) int a,b,c; cin>>a>>b>>c
#define readf(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d;
#define print(v) for(auto x:v) cout<<x<<" ";cout<<endl
#define printPair(res) for(pair<int,int>& p:res) cout<<p.first<<" "<<p.second<<endl;
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
//***** Constants *****
const int mod = 1000000007; /* 1e9 + 7*/
const int MAXN = 1500005; /*1e6 +5 */
const int mod1= 998244353;
const int NAX=2E5+5;
int vis[MAXN]={0};
int fac[300001];
void fact(int m){fac[0]=1;for(int i=1;i<=300000;i++) fac[i]=(fac[i-1]%m * i%m)%m;}
/* Iterative Function to calculate (x^y)%p in O(log y)*/
long long power( long long x, int y, int p){ long long res = 1;
x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p;} return res;}
// Returns n^(-1) mod p
long long modInverse( long long n, int p) { return power(n, p - 2, p); }
long long nCr( long long n,int r, int p) { if (r == 0) return 1; if(n<r) return 0;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; }
int binarySearch(vi arr, int l, int r, int x) {
if (r >= l) { int mid = l + (r - l) / 2; if (arr[mid] == x) return mid;
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x); } return -1; }
bool sortGrt(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.second > b.second);
}
void solve(int test)
{
int n , ans=0;cin>>n;
vi a(n);
scan(a,n);
sort(rall(a));
loop(i,0,n)
{
ans+= ((a[i])*(n-1-i) - (a[i]*i));
}
cout<<ans;
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
faster;
fact(mod);
int t=1;
//cin>>t;
for(int test=1;test<=t;test++)
{
solve(test);
}
} | #include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#define rg register
inline int read(){
rg int x=0,fh=1;
rg char ch=getchar();
while(ch<'0' || ch>'9'){
if(ch=='-') fh=-1;
ch=getchar();
}
while(ch>='0' && ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*fh;
}
const int maxn=2e5+5;
int n,a[maxn];
long long ans=0,sum=0;
int main(){
n=read();
for(rg int i=1;i<=n;i++){
a[i]=read();
}
std::sort(a+1,a+1+n);
for(rg int i=1;i<=n;i++){
ans+=1LL*a[i]*(i-1)-sum;
sum+=a[i];
}
printf("%lld\n",ans);
return 0;
}
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
const int INF = 0x3f3f3f3f;
#define FOR(i, b, e) for(int i = (b); i < (e); i++)
#define TRAV(x, a) for(auto &x: (a))
#define SZ(x) ((int)(x).size())
#define PB push_back
#define PR pair
#define X first
#define Y second
void solve(){
vector<ll> vec;
ll akt = 0;
int n;
cin >> n;
FOR(i, 0, n){
int a, b;
cin >> a >> b;
akt -= a;
vec.PB(2ll*a+b);
}
sort(vec.rbegin(), vec.rend());
int ans = 0;
TRAV(x, vec){
if(akt <= 0){
ans++;
akt += x;
}
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
// int tt; cin >> tt;
// FOR(te, 0, tt){
// // cout << "Case #" << te+1 << ": ";
// solve();
// }
solve();
return 0;
}
| // Problem D
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <cassert>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <iomanip>
#define MAXN 1000
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
double p[101][101][101];
int a0, b0, c0;
int main() {
cin >> a0 >> b0 >> c0;
for (int a=0; a<=100; a++)
for (int b=0; b<=100; b++)
for (int c=0; c<=100; c++) {
if (a<a0 || b<b0 || c<c0) p[a][b][c] = 0;
else if (a == a0 && b == b0 && c == c0) p[a][b][c] = 1;
else {
if (a > a0 && b!=100 && c!=100) p[a][b][c] += ((a-1)*p[a-1][b][c] / (a+b+c-1));
if (b > b0 && a!=100 && c!=100) p[a][b][c] += ((b-1)*p[a][b-1][c] / (a+b+c-1));
if (c > c0 && a!=100 && b!=100) p[a][b][c] += ((c-1)*p[a][b][c-1] / (a+b+c-1));
//~ cout << a << " " << b << " " << c << " " << p[a][b][c] << endl;
}
}
double res = 0;
int a, b, c;
a = 100;
for (b = b0; b<100; b++)
for (c=c0; c<100; c++)
res += p[a][b][c] * (a+b+c-a0-b0-c0);
b = 100;
for (a = a0; a<100; a++)
for (c=c0; c<100; c++)
res += p[a][b][c] * (a+b+c-a0-b0-c0);
c = 100;
for (b = b0; b<100; b++)
for (a=a0; a<100; a++)
res += p[a][b][c] * (a+b+c-a0-b0-c0);
cout << std::setprecision(10) << res << endl;
return 0;
}
|
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <math.h>
#include <string>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(n) for( int i = 0 ; i < n ; i++ )
#define REP(n) for( int i = 1 ; i <= n ; i++ )
#define repll(n) for( ll i = 0 ; i < n ; i++ )
#define REPll(n) for( ll i = 1 ; i <= n ; i++ )
#define rep2(n) for( int j = 0 ; j < n ; j++ )
#define REP2(n) for( int j = 1 ; j <= n ; j++ )
#define repll2(n) for( ll j = 0 ; j < n ; j++ )
#define REPll2(n) for( ll j = 1 ; j <= n ; j++ )
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n , w;
cin >> n >> w;
cout << n / w;
} | #include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
ll fastpow(ll x,ll y,ll p){
ll res=1,tmp=x;
while(y){
if(y&1){
res=res*tmp%p;
}
tmp=tmp*tmp%p;
y>>=1;
}
return res;
}
int main(){
ll n,m;
cin>>n>>m;
cout<<int(floor(fastpow(10,n,m*m)/m))%m<<endl;
return 0;
} |
/*
* -----Joy Matubber ----10/6/2021----
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dl;
typedef string St;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define all(a) (a).begin(),(a).end()
#define sz(x) (int)x.size()
const double PI = acos(-1);
const double eps = 1e-9; //(abs(a-b)<eps) check the two double value is equal or not;
const int inf = 2000000000;
const ll infLL = 9000000000000000000;
#define mem(a,b) memset(a, b, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define sqr(a) ((a) * (a))
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define fraction(a) cout.unsetf(ios::floatfield); cout.precision(a); cout.setf(ios::fixed,ios::floatfield); //dosomik er por koy ghor ta dekhabe
//**************************************************D E B U G G E R OR OUTPUT ******************************************************************;
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
return os << "(" << p.first << ", " << p.second << ")";
}
template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
os << "{";
for(auto it = v.begin(); it != v.end(); ++it) {
if( it != v.begin() ) os << ", ";
os << *it;
}
return os << "}";
}
template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
os << "[";
for(auto it = v.begin(); it != v.end(); ++it) {
if( it != v.begin() ) os << ", ";
os << *it;
}
return os << "]";
}
template < typename T >
ostream &operator << ( ostream & os, const multiset< T > &v ) {
os << "[";
for(auto it = v.begin(); it != v.end(); ++it) {
if( it != v.begin() ) os << ", ";
os << *it;
}
return os << "]";
}
template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
os << "[";
for(auto it = v.begin(); it != v.end(); ++it) {
if( it != v.begin() ) os << ", ";
os << it -> first << " = " << it -> second ;
}
return os << "]";
}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
void faltu () {
cerr << endl;
}
template <typename T>
void faltu( T a[], int n ) {
for(int i = 0; i < n; ++i) cerr << a[i] << ' ';
cerr << endl;
}
template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) {
cerr << arg << ' ';
faltu(rest...);
}
//************************************************************* C O D E ***********************************************************************
int main() {
optimize();
ll a,b,c;
cin>>a>>b>>c;
if(c==0)
{
if(a==b)
{
cout<<"Aoki"<<endl;
}
else if(a>b)
{
cout<<"Takahashi"<<endl;
}
else
{
cout<<"Aoki"<<endl;
}
}
else
{
if(a==b)
{
cout<<"Takahashi"<<endl;
}
else if(a>b)
{
cout<<"Takahashi"<<endl;
}
else
{
cout<<"Aoki"<<endl;
}
}
}
| #include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
if(c)
if(a >= b) cout << "Takahashi\n";
else cout << "Aoki\n";
else if(a > b) cout << "Takahashi\n";
else cout << "Aoki\n";
} |
#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d\n", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
#define int long long
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
typedef pair <int, PII> PIII;
template <typename T>
inline T gi()
{
T f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f, N = 300013, M = N << 1;
int n, w;
namespace BIT
{
int tr[N];
int lowbit(int i) {return i & (-i);}
void add(int u, int val)
{
++u;
for (int i = u; i <= 300001; i += lowbit(i)) tr[i] += val;
}
int query(int u)
{
int res = 0;
++u;
for (int i = u; i; i -= lowbit(i)) res += tr[i];
return res;
}
} //namespace BIT
signed main()
{
//File("");
n = gi <int> ();
cout << max(0ll, n) << endl;
return 0;
}
| #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;
int main() {
int n,k;
cin >> n >> k;
int x = 0;
rep(i,n)rep(j,k) x += (i+1)*100+j+1;
cout << x << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)
const ll MOD = 1000000007;
const ll INF = (ll) 1e15;
ll N;
ll A[100005];
ll B[100005];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N;
REP(i, N) {
cin >> A[i];
}
REP(i, N) {
cin >> B[i];
}
ll ans = 0;
priority_queue<ll> q0, q1;
REP(i, N) {
ans += A[i];
ll d = B[i] - A[i];
if (i % 2 == 0)
q0.push(d);
else
q1.push(d);
}
while (!q0.empty() && !q1.empty()) {
ll d = q0.top() + q1.top();
q0.pop();
q1.pop();
if (d < 0)
break;
ans += d;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
typedef vector < vector<ll> > Mat;
#define fi first
#define se second
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);
void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}
int N, M;
string S[int(1e5 + 5)];
ll cntOdd, cntEven;
int main(){
cin >> N >> M;
for (int i = 0; i < N; i++){
cin >> S[i];
int cnt = 0;
for (int j = 0; j < M; j++){
if (S[i][j] == '0') cnt++;
}
if (cnt % 2 != 0) cntOdd++;
else cntEven++;
}
cout << cntOdd * cntEven << endl;
return 0;
} |
// for(int i = 0; i < n; i++) {cin >> a[i];}
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define gcd __gcd
#define F first
#define S second
#define pii pair<int,int>
using ll = long long;
bool isSubsequence(string s, string t) {
int index = 0;
if(s.length() == 0) {
return true;
}
for(int i = 0; i < (int) t.length(); i++) {
if(s.at(index) == t.at(i)){
index++;
}
if(index == (int) s.length()) {
return true;
}
}
return false;
}
bool isPrime(int n) {
for(int i = 2; i < int(sqrt(n))+1; i++) {
if(n%i==0) {
return false;
}
}
return true;
}
bool isPrime(ll n) {
for(ll i = 2; i < ll(sqrt(n))+1; i++) {
if(n%i==0) {
return false;
}
}
return true;
}
bool isInt(double n) {
if(floor(n) == ceil(n)) {
return true;
}
return false;
}
bool isPalindrome(string s) {
int n = s.length();
for(int i = 0; i < n/2; i++) {
if(s.at(i) != s.at(n-i-1)) {
return false;
}
}
return true;
}
bool distinctDigits(int n) {
string s = to_string(n);
for(int i = 0; i < (int) s.length(); i++) {
for(int j = i+1; j < (int) s.length(); j++) {
if(s.at(i)==s.at(j)) {
return false;
}
}
}
return true;
}
bool distinctDigits(string s) {
for(int i = 0; i < (int) s.length(); i++) {
for(int j = i+1; j < (int) s.length(); j++) {
if(s.at(i)==s.at(j)) {
return false;
}
}
}
return true;
}
int digitSum(int n) {
int ans = 0;
string s = to_string(n);
for(int i = 0; i < (int) s.length(); i++) {
ans += stoi(s.substr(i, 1));
}
return ans;
}
int swapBits(int n, int p1, int p2) {
int bit1 = (n >> p1) & 1;
int bit2 = (n >> p2) & 1;
int x = bit1 ^ bit2;
x = (x << p1) | (x << p2);
return n ^ x;
}
void display(vector<auto> a) {
for(auto element : a) {
cout << element << ' ';
}
cout << endl;
}
void solve()
{
ll n;
cin >> n;
vector<ll> x(n);
vector<ll> y(n);
for(int i = 0; i < n; i++) {
ll xi, yi;
cin >> xi >> yi;
x[i] = xi;
y[i] = yi;
}
vector<vector<ll>> houses;
for(int i = 0; i < n; i++) {
houses.pb({x[i], y[i], i});
}
vector<vector<ll>> maxes;
sort(houses.begin(), houses.end(),
[] (vector<ll> a, vector<ll> b) {
return a[0] > b[0];
});
// for(int i = 0; i < n; i++) {
// display(houses[i]);
// }
maxes.pb({houses[0][0] - houses[n-1][0], houses[0][2], houses[n-1][2]});
maxes.pb({houses[0][0] - houses[n-2][0], houses[0][2], houses[n-2][2]});
maxes.pb({houses[1][0] - houses[n-1][0], houses[1][2], houses[n-1][2]});
sort(houses.begin(), houses.end(),
[] (vector<ll> a, vector<ll> b) {
return a[1] > b[1];
});
// for(int i = 0; i < n; i++) {
// display(houses[i]);
// }
maxes.pb({houses[0][1] - houses[n-1][1], houses[0][2], houses[n-1][2]});
maxes.pb({houses[0][1] - houses[n-2][1], houses[0][2], houses[n-2][2]});
maxes.pb({houses[1][1] - houses[n-1][1], houses[1][2], houses[n-1][2]});
sort(maxes.begin(), maxes.end(),
[] (vector<ll> a, vector<ll> b) {
return a[0] > b[0];
});
// for(int i = 0; i < 6; i++) {
// display(maxes[i]);
// }
if(maxes[1][1] == maxes[0][1] && maxes[1][2] == maxes[0][2]) {
cout << maxes[2][0];
} else {
cout << maxes[1][0];
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// int t;
// cin >> t;
// for(int i = 1; i <= t; i++) {
// // cout << "Case #" << i << ": ";
// solve();
// }
solve();
cout.flush();
return 0;
} |
/*
Saturday 29 May 2021 05:32:07 PM IST
@uthor::astrainL3gi0N
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef std::vector<int> vi;
typedef std::pair<int,int> ii;
typedef std::vector<ii> vii;
typedef std::vector<ll> vl;
#define pb push_back
#define mp make_pair
#define debg(x) std::cerr<<(#x)<<" => "<<x<<'\n';
#define debgg(x,y) std::cerr<<(#x)<<" => "<<x<<'\t'<<(#y)<<' '<<y<<'\n';
#define len(a) (int)(a).size()
#define all(x) x.begin(),x.end()
const int mod = 1000000007;
//const int mod = 998244353;
bool comp (ii x, ii y) {
if (x.second == y.second)
return x.first < y.first;
return x.second < y.second;
}
template < typename T> void printv (T &a) {
cout<<'\n';
for (auto it = a.begin(); it != a.end(); ++it)
cout<<it->first<<' '<<it->second<<'\n';
}
int gint() {
int n; cin>>n;
return n;
}
//int t[510][510];
ll ans;
void solve () {
map <ii,int> hmp;
map <int,ii> mph;
int n = gint();
vii vpr;
for (int i = 0;i < n;++i) {
int x = gint();
int y = gint();
vpr.pb(mp(x,y));
hmp[mp(x,y)] = i+1;
mph[i+1] = mp(x,y);
}
vi vr;
set <int> hsh;
sort(all(vpr));
hsh.insert(hmp[vpr[0]]);
hsh.insert(hmp[vpr[1]]);
hsh.insert(hmp[vpr[n-1]]);
hsh.insert(hmp[vpr[n-2]]);
for (auto &v: vpr) {
swap(v.first,v.second);
}
sort(all(vpr));
hsh.insert(hmp[mp(vpr[0].second,vpr[0].first)]);
hsh.insert(hmp[mp(vpr[1].second,vpr[1].first)]);
hsh.insert(hmp[mp(vpr[n-1].second,vpr[n-1].first)]);
hsh.insert(hmp[mp(vpr[n-2].second,vpr[n-2].first)]);
for (auto &v: vpr) {
swap(v.first,v.second);
}
vii vrr;
for (auto v: vpr) {
if (hsh.count(hmp[v]))
vrr.pb(v);
}
for (int i = 0; i < len(vrr); ++i) {
for (int j = i+1; j < len(vrr); ++j) {
vr.pb(max(abs(vrr[i].first-vrr[j].first),
abs(vrr[i].second-vrr[j].second)));
}
}
sort(all(vr));
cout<<vr[len(vr)-2];
}
void sol () {
//test
//cout << setprecision(15) << fixed;
}
int main () {
int testcases = 1;
for (int t = 0; t < testcases; ++t) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll a,b,c;
cin>>a>>b>>c;
if(a*a+b*b<c*c)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define deb(x) cerr << #x << ":" << x << "\n"
#define all(x) x.begin(),x.end()
#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>
void solve()
{
ll a,b,c;
cin>>a>>b>>c;
ll M=998244353;
a=((a*(a+1))/2)%M;
b=((b*(b+1))/2)%M;
c=((c*(c+1))/2)%M;
ll ans=a;
ans=(ans*b)%M;
ans=(ans*c)%M;
cout<<ans<<"\n";
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// #define int long long
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define irreps(i, m, n) for (int i = ((int)(n)-1); i > (int)(m); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) (int)(x.size())
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;
using ld = long double;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
const ll INF = 1e9+10;
// const ll MOD = 1e9+7;
const ll MOD = 998244353;
const ll LINF = 1e18;
// 3人選ぶ
// 3最大値で圧縮 => 5つの内の最小値を最大化
// ある1個の値に着目した時、その値が最終値に成り得るか
// 2数の和を求める
// xが作れるか =>
// 最大値をとって最小値をとる
// 2組を決める
// もう1人は、5つのパラメータそれぞれについてソートしておけば
// 最小値について降順にソートしておく idxももつ
// 2組を決めた時、5つのうちどれを最小値とするか
int n;
vc<vc<int>> a;
bool isOk(ll mid){
vc<int> used(1<<5), X;
rep(i,n){
int x=0;
rep(j,5){
if(a[i][j]>=mid) x+=(1<<j);
}
if(!used[x]){
used[x]=1;
X.push_back(x);
}
}
// cout<<X.size()<<endl;
int all=(1<<5)-1;
rep(i,X.size()){
int x=X[i];
if(x==all) return 1;
irep(j,i+1,X.size()){
int y=x|X[j];
if(y==all) return 1;
irep(k,j+1,X.size()){
int z=y|X[k];
if(all==z) return 1;
}
}
}
return 0;
}
signed main()
{
cin.tie( 0 ); ios::sync_with_stdio( false );
cin>>n;
a = vc<vc<int>>(n,vc<int>(5));
rep(i,n) rep(j,5) cin>>a[i][j];
ll ng=1e9+1, ok=1;
while(abs(ok-ng)>1){
ll mid=(ok+ng)/2;
// cout<<ok<<' '<<ng<<' '<<mid<<endl;
if(isOk(mid)) ok=mid;
else ng=mid;
}
cout<<ok<<endl;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
int n, a[N];
inline int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
set <int> s;
set <int> :: iterator it;
inline void divide(int num)
{
for (int i = 1; i <= sqrt(num); i++)
if (num % i == 0) {
s.insert(i);
s.insert(num / i);
}
}
int main()
{
scanf ("%d", &n);
int minv = 0x3f3f3f3f;
for (int i = 1; i <= n; i++) {
scanf ("%d", &a[i]);
minv = min(minv, a[i]);
divide(a[i]);
}
int ans = 1;
for (it = s.begin(); it != s.end(); it++)
{
int num = (*it);
if (num >= minv) break;
int val = 0;
for (int i = 1; i <= n; i++)
if (a[i] % num == 0) val = gcd(val, a[i]);
if (val == num) ans++;
}
printf ("%d\n", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
bool ABC(char c) {
int i = c - 'A';
return 0<=i && i<26;
}
bool abc(char c) {
int i = c - 'a';
return 0<=i && i<26;
}
int main() {
string S; cin >> S;
ll N = S.size();
rep(i,N) {
bool OK = false;
if (i%2) OK = ABC(S[i]);
else OK = abc(S[i]);
if (!OK) No();
}
Yes();
} | #include <bits/stdc++.h>
using namespace std;
string f(string s){
for(int i=0;i<s.size();i+=2){
if(s[i]<'a' || s[i]>'z') return "No";
}
for(int i=1;i<s.size();i+=2){
if(s[i]<'A' || s[i]>'Z') return "No";
}
return "Yes";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin>>s;
cout<<f(s)<<endl;
}
|
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define eps 1e-18
#define pi acos(-1)
using namespace std;
const int M = 5;
void done(int tc){
int a,b,c; cin >> a >> b >> c;
if(c==0){
if(a==b)cout<<"Aoki\n";
else if(a>b)cout<<"Takahashi\n";
else cout<<"Aoki\n";
}
if(c==1){
if(a==b)cout<<"Takahashi\n";
else if(a<b)cout<<"Aoki\n";
else cout<<"Takahashi\n";
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t; t = 1;
// cin >> t;
for(int i=1;i<=t;++i)done(i);
}
| #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#define amax(a, b) a = std::max(a, b)
#define amin(a, b) a = std::min(a, b)
using ll = long long;
int main() {
int a, b, c;
std::cin >> a >> b >> c;
for ( ; a >= 0 && b >= 0; c ^= 1) {
if (c) b--;
else a--;
}
std::cout << (c ? "Aoki" : "Takahashi") << '\n';
return 0;
} |
/*HAR HAR MAHADEV
ヽ`、ヽ``、ヽ`ヽ`、、ヽ `ヽ 、ヽ`🌙`ヽヽ`ヽ、ヽ`
ヽ`、ヽ``、ヽ 、``、 `、ヽ` 、` ヽ`ヽ、ヽ `、ヽ``、
ヽ、``、`、ヽ``、 、ヽヽ`、`、、ヽヽ、``、 、 ヽ`、
ヽ``、 ヽ`ヽ`、、ヽ `ヽ 、 🚶ヽ````ヽヽヽ`、、ヽ`、、ヽ*/
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;//typedef for datatype and #define for macro
# define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
# define MOD 1000000007
# define endl '\n'
# define FOR(i, a, b) for (int i=a; i<(b); i++)
# define F0R(i, a) for (int i=0; i<(a); i++)
# define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
# define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
# define INF 9e18
# define PI 3.14159265358979323846
# define lb lower_bound
# define ub upper_bound
# define mp make_pair
# define pb push_back
# define fi first
# define se second
# define all(a) a.begin(), a.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
fast;
ll t = 1;
// cin >> t;
while (t--)
{
ll n, sum = 0;
cin >> n;
while (n--)
{
ll a, b;
cin >> a >> b;
sum += ((b * (b + 1)) / 2) - ((a * (a + 1)) / 2) + a;
}
cout << sum << endl;
return 0;
}
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/ | #include<bits/stdc++.h>
/*
shinchanCoder Here!!
*/
using namespace std;
#define endll "\n"
#define pb push_back
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define forn(i,n) for(ll i=0;i<(n);i++)
#define sz(x) (int)(x).size()
#define ff first
#define ss second
#define mpr make_pair
#define len(x) x.length()
#define ll long long
#define ld long double
#define lli long long int
#define mod 1000000007
ll powmod(ll a, ll b){
if(b==0){
return 1 ;
}
ll x = powmod(a,b/2);
x = (x*x)%mod ;
if(b%2){
x = (a*x)%mod ;
}
return x;
}
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = res*x;
y = y>>1;
x = x*x;
}
return res;
}
vector<ll> prm;
void Sieve(ll n) {
bool prime[n+1];
memset(prime,true, sizeof(prime));
for(ll p=2;p*p<=n;p++) {
if(prime[p]==true) {
for(ll i=p*p;i<=n;i+=p) {
prime[i]=false;
}
}
}
if(!prm.empty()) {
prm.clear();
}
for(ll p=2;p<=n;p++) {
if(prime[p]) {
prm.pb(p);
}
}
}
ll fact(ll n) {
ll res = 1;
for(ll i=2;i<=n;i++) {
res*=i;
}
return res;
}
ll _lcm(ll x,ll y) {
return x*y/__gcd(x,y);
}
ll _gcd(ll a,ll b) {
return a%b==0 ? b : _gcd(b,a%b);
}
ll nCr(ll n,ll r) {
return (ld)fact(n)/(ld)(fact(r)*fact(n-r));
}
bool isCoPrime(ll z,ll a[],ll n) {
for(ll i=0;i<n;i++) {
if(_gcd(a[i],z)==1) {
return 1;
}
}
return 0;
}
ll solve(ll prm[15],ll i,ll res,ll a[],ll n) {
if(i==15) {
if(!isCoPrime(res,a,n)) {
return res;
}else {
return 1e18;
}
}
return min(solve(prm,i+1,res*prm[i],a,n),solve(prm,i+1,res,a,n));
}
int main() {
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//Sieve(1000000);
ll prm[15]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;i++) {
cin>>a[i];
}
cout<<solve(prm,0,1,a,n);
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
using namespace std;
int main()
{
fastread();
//freopen("input.txt","r", stdin);
int n,a[1001];
cin>>n;
for(int i=0; i<n; i++){
cin>>a[i];
}
sort(a,a+n);
map<int,int>mp;
map<int,int>:: iterator itr;
for(int i=2; i<=1000; i++){
for(int j=0; j<n; j++){
if(a[j] % i == 0){
mp[i]++;
}
}
}
int ans = 0,freq = 0;
for(itr = mp.begin(); itr!=mp.end(); itr++){
if(itr->second > freq){
freq = itr->second;
ans = itr->first;
}
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ms(s, n) memset(s, n, sizeof(s))
#define f(i, a, b) for (int i = (a); i <=(b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<ll> vi;
typedef vector<pi> vii;
const int MOD = (int) 1e9 + 7;
const int MOD2 = MOD + 2; //1007681537;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
const ld PI = acos((ld) - 1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
int modulo(int a, int b) { return (a % b + b) % b; }
#define db(x) cerr << #x << " = " << (x) << " ";
#define endln cerr << "\n";
int main()
{
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n;
cin >> n;
vi a(n);
f(i, 0, n - 1) {
cin >> a[i];
}
ll cur = 0, ans;
f(i, 2, 1000) {
ll cnt = 0;
f(j, 0, n - 1) {
if (a[j] % i == 0) {
cnt++;
}
}
if (cnt > cur) {
cur = cnt;
ans = i;
}
}
cout << ans << "\n";
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* use int when constraints limits are tight
* Time taken : global variable < pass by refernce < pass by value
* It takes significant amt. of time in pass by value
* Avoid Using endl
*/
|
#include <bits/stdc++.h>
using namespace std;
int sol(string s, string t){
int res = 0;
int q = 0;
for (int i = 0; i < s.size(); ++ i){
if (s[i] == '0'){
if (t[i] == '0'){
if (q != 0){
res += 1;
}
}
else{
q += 1;
}
}
else{
if (t[i] == '0'){
q -= 1;
res += 1;
}
}
}
return res + q;
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); srand(time(NULL));
int n; cin >> n;
string s, t; cin >> s >> t;
vector <int> cntt(2), cnts(2);
for (char c : s) cnts[c - '0'] += 1;
for (char c : t) cntt[c - '0'] += 1;
int ans = sol(s, t);
/*for (char &c : s) c = char(((c - '0') ^ 1) + '0');
for (char &c : t) c = char(((c - '0') ^ 1) + '0');
ans = min(ans, sol(s, t));*/
if (cntt[0] != cnts[0] || cntt[1] != cnts[1]) return cout << -1, 0;
cout << ans;
}
| //#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <array>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<complex>
#include<fstream>
#include<assert.h>
#include<stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
//typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int get_gcd(int n, int m) {
if (n < m) swap(n, m);
while (m) {
swap(n, m);
m %= n;
}
return n;
}
int get_lcm(int n, int m) {
return n / get_gcd(n, m)*m;
}
struct BIT //0-indexed
{
vector<int> node;
int mn;
BIT(int mn)
{
this->mn = mn;
node.resize(mn + 1, 0);
}
void add(int i, int x)
{
i++;
while (i <= mn)
{
node[i] += x;
i += (i&-i);
}
}
int sum(int i)
{
i++;
int res = 0;
while (i > 0)
{
res += node[i];
i -= (i&-i);
}
return res;
}
//k:0-indexed
int get(long long k) {
++k;
int res = 0;
int N = 1; while (N < (int)node.size()) N *= 2;
for (int i = N / 2; i > 0; i /= 2) {
if (res + i < (int)node.size() && node[res + i] < k) {
k = k - node[res + i];
res = res + i;
}
}
return res;
}
};
signed main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int H, W, M;
cin >> H >> W >> M;
int ans = H * W;
vector<int> R(H, W), C(W, H);
int X, Y;
vector<pii> P(M);
rep(i, 0, M)
{
cin >> Y >> X;
Y--, X--;
chmin(R[Y], X);
chmin(C[X], Y);
P[i] = mp(X, Y);
}
sort(all(P));
vector<bool> USED(H, false);
int pi = 0;
BIT bit(H + 3);
int mi = INF;
rep(i, 0, W)
{
while (pi < M&&P[pi].first <= i)
{
if (!USED[P[pi].second])
{
USED[P[pi].second] = true;
bit.add(P[pi].second, 1);
}
pi++;
}
chmin(mi, C[i]);
int ma = max(C[0], mi == 0 ? 0 : C[i]);
int res = H - ma;
res += bit.sum(ma - 1);
if (mi != 0 && C[i] > 0)res -= bit.sum(C[i] - 1);
ans -= res;
}
/*int mi = W;
rep(i, 0, H)
{
chmin(mi, R[i]);
if (mi == 0)break;
ans += R[i];
}
mi = H;
rep(i, 0, W)
{
chmin(mi, C[i]);
if (mi == 0)break;
ans += C[i];
}
mi = W;
rep(i, 0, H)
{
chmin(mi, R[i]);
ans -= mi;
}*/
cout << ans << endl;
return 0;
} |
/***
* Author : SUARABH UPADHAYAY
* Institution : IET LUCKNOW
***/
#include<bits/stdc++.h>
//#include <boost/math/common_factor.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
using namespace std;
#define lll cpp_int
#define ull unsigned long long
#define ll long long
#define ui unsigned int
#define ldb long double
//#define lcm(o,g) boost::math::lcm(o,g)
#define mod 1000000007
#define mod1 1000003
#define mod2 998244353
#define pi acos(-1)
#define full INT_MAX
#define llfull LLONG_MAX
#define V vector
#define VL vector<ll>
#define L list
#define LL list<ll>
#define D deque
#define DL deque<ll>
#define PQL priority_queue<ll>
#define SL set<ll>
#define SC set<char>
#define USL unordered_set<ll>
#define M map
#define MLL map<ll,ll>
#define SS stringstream
#define mkp make_pair
#define mkt make_tuple
#define er equal_range
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define np next_permutation
#define ln length()
#define DO greater<ll>()
#define DM greater<ll>
#define pb push_back
#define in insert
#define pob pop_back()
#define bg begin()
#define ed end()
#define sz size()
#define all(o) ((o).bg,(o).ed)
#define F first
#define S second
#define stf shrink_to_fit()
#define ig cin.ignore(1,'\n');
#define cg(g) getline(cin,(g))
#define f(g,h,o) for(ll g=h;g<o;g++)
#define f1(g,h,o) for(ll g=h;g>o;g--)
#define f2(g) for(auto E : (g))
#define fm(g) for(auto [X,Y] : (g))
#define it(g) ::iterator E=(g).bg
#define AI(g,o) ll g[o]; f(i,0,o)cin>>g[i]
#define AO(g,o) f(i,0,o)cout<<g[i]<<" "
#define T ui t; cin>>t; while(t--)
#define T1(g) ui g; cin>>g; while(g--)
int main(){
/* freopen("input.txt", "r" , stdin);
freopen("output.txt", "w" , stdout); */
ios_base::sync_with_stdio(false);
cin.tie(0);
//cout<<fixed<<setprecision(10)
ll n;
cin>>n;
string s;
cin>>s;
if(n==1&&s=="1"){
cout<<20000000000;
return 0;
}
string k;
ll p=0;
while(p<200011){
k.in(p,"110");
p+=3;
}
ll ans=0;
ll q;
if(n%3==0)
q=((n/3)+1)*3;
else
q=((n/3)+2)*3;
f(i,0,q-n+1){
if(k.substr(i,n)==s)
ans++;
}
if(ans>0)
cout<<ans+10000000000-(q/3);
else
cout<<0;
} | constexpr bool isDebugMode = false;
int testcase = 1;
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge{long long to,cost;};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007; //998244353;
const long long dy[] = {-1, 0, 0, 1};
const long long dx[] = {0, -1, 1, 0};
const string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e) (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e) (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
long long mpow(long long a, long long n, long long mod = MOD){long long res = 1; while(n > 0){if(n & 1)res = res * a % mod; a = a * a % mod; n >>= 1;} return res;}
void pt(){cout << endl; return;}
template<class Head> void pt(Head&& head){cout << head; pt(); return;}
template<class Head, class... Tail> void pt(Head&& head, Tail&&... tail){cout << head << " "; pt(forward<Tail>(tail)...);}
void dpt(){if(!isDebugMode) return; cout << endl; return;}
template<class Head> void dpt(Head&& head){if(!isDebugMode) return; cout << head; pt(); return;}
template<class Head, class... Tail> void dpt(Head&& head, Tail&&... tail){if(!isDebugMode) return; cout << head << " "; pt(forward<Tail>(tail)...);}
template<class T> void enu(T v){rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void enu2(T v){rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T> void debug(T v){if(!isDebugMode) return; rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void debug2(T v){if(!isDebugMode) return; rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2> inline bool chmax(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2> long long recgcd(T1 a, T2 b){return a % b ? recgcd(b, a % b) : b;}
bool valid(long long H, long long W, long long h, long long w) { return 0 <= h && h < H && 0 <= w && w < W; }
void solve();
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// cin >> testcase;
while(testcase--) solve();
return 0;
}
void solve(){
char s, t; cin >> s >> t;
if (s == 'Y' && t == 'a')
pt("A");
if (s == 'Y' && t == 'b')
pt("B");
if (s == 'Y' && t == 'c')
pt("C");
if(s == 'N') pt(t);
} |
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test lli t;cin>>t;while(t--)
#define vll vector<lli>
#define mll map<lli,lli>
#define vvll vector<vector<lli>>
#define vpll vector<pair<lli,lli>>
#define vvpll vector<vector<pair<lli,lli>>>
#define mpll map<pair<lli,lli>,lli>
#define pll pair<lli,lli>
#define sll stack<lli>
#define qll queqe<lli>
#define pb push_back
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ff first
#define ss second
#define mod 1000000007
#define ma 1000000000000000000
#define mi -1000000000000000000
lli h, w, n, m, i, j, k, l, d = 0, x, y;
queue<pll>q;
lli b[1501][1501] = {0};
lli mp[1501][1501] = {0};
vpll v;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fastio
cin >> h >> w >> n >> m;
while (n--)
{
cin >> i >> j;
v.pb({i, j});
b[i][j] = 1;
d++;
//cout << i << " " << j << endl;
}
while (m--)
{
cin >> i >> j;
mp[i][j] = 1;
}
for (auto k : v)
{
i = k.ff;
j = k.ss;
x = i + 1; y = j;
while (x <= h)
{
if ( b[x][y] == 0 && mp[x][y] == 0)
{
b[x][y] = 1;
d++;
//cout << x << " " << y << endl;
x++;
}
else
break;
}
x = i - 1; y = j;
while (x > 0)
{
if ( b[x][y] == 0 && mp[x][y] == 0)
{
b[x][y] = 1;
d++;
//cout << x << " " << y << endl;
x--;
//cout << i + 1 << " a" << j << endl;
}
else
break;
}
}
for (auto k : v)
{
i = k.ff;
j = k.ss;
x = i; y = j + 1;
while (y <= w)
{
if ( b[x][y] != 2 && mp[x][y] == 0)
{
if (b[x][y] == 0)
d++;
b[x][y] = 2;
//cout << x << " " << y << endl;
y++;
//cout << i + 1 << " a" << j << endl;
}
else
break;
}
x = i; y = j - 1;
while (y > 0)
{
if ( b[x][y] != 2 && mp[x][y] == 0)
{
if (b[x][y] == 0)
d++;
b[x][y] = 2;
// cout << x << " " << y << endl;
y--;
//cout << i + 1 << " a" << j << endl;
}
else
break;
}
}
cout << d;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, e) for (int i = 0; i < (e); ++i)
#define REP3(i, b, e) for (int i = (b); i < (e); ++i)
#define RREP(i, b, e) for (int i = (b)-1; i >= e; --i)
inline void print(void) {
cout << '\n';
}
template <class T, class... U>
inline void print(const T &x, const U &...y) {
cout << x;
if (sizeof...(U) != 0) {
cout << ' ';
}
print(y...);
}
int main() {
int n;
cin >> n;
vector<long long> a(n);
vector<int> t(n);
REP(i, n) cin >> a[i] >> t[i];
int q;
cin >> q;
vector<long long> x(q);
REP(i, q) cin >> x[i];
long long lo = -1000000000LL;
long long hi = 1000000000LL;
long long s = 0;
REP(i, n) {
if (t[i] == 1) {
s += a[i];
lo += a[i];
hi += a[i];
} else if (t[i] == 2) {
lo = max(lo, a[i]);
hi = max(hi, a[i]);
} else if (t[i] == 3) {
lo = min(lo, a[i]);
hi = min(hi, a[i]);
}
}
REP(i, q) {
if (x[i] + s <= lo) {
print(lo);
} else if (x[i] + s >= hi) {
print(hi);
} else {
print(x[i] + s);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
int main() {
int n, k;
cin >> n >> k;
long long ans = 0;
for(long long ab = 2; ab <= 2 * n; ++ab) {
long long cd = ab - k;
if(cd < 2) continue;
long long A = ab - 1 - (max(0LL, (ab - 1) - n)) * 2;
long long C = cd - 1 - (max(0LL, (cd - 1) - n)) * 2;
if(A <= 0 || C <= 0) continue;
ans += A * C;
}
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll ways(ll c, ll &n)
{
if (c < 2 || c > 2*n) return 0;
if (c <= n+1) return c-1;
return 2*n-c+1;
}
int main()
{
ll n, k;
scanf("%lld%lld", &n, &k);
ll ans = 0;
for (ll z = 2; z <= 2*n; ++z) {
ans += ways(z, n)*ways(z+k, n);
}
printf("%lld\n", ans);
} |
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 998244353
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#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;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll a,b,c;cin>>a>>b>>c;
a = (a*(a+1)/2)%MOD;
b = (b*(b+1)/2)%MOD;
c = (c*(c+1)/2)%MOD;
cout<<(((a*b)%MOD)*c)%MOD<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int MAX_N = 46;
ll A[MAX_N];
vector<ll> B;
int main()
{
ll N,T;
cin >> N >> T;
rep(i,N)
{
cin >> A[i];
}
for (int i = 0; i < (1 << (N/2)); ++i)
{
ll temp = 0;
rep(j,N/2)
{
if (i & (1 << j)) temp += A[j];
}
B.push_back(temp);
}
sort(B.begin(), B.end());
ll ans = 0;
for (int i = 0; i < (1 << (N-N/2)); ++i)
{
ll temp = 0;
rep(j,N-N/2)
{
if (i & (1 << j)) temp += A[j+N/2];
}
auto it = upper_bound(B.begin(), B.end(), T-temp);
if (it != B.begin())
{
it--;
temp += *it;
ans = max(ans,temp);
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using vi = vector<int>;
using si = set<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define P pair<int,int>
#define PLL pair<ll,ll>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)
#define reciv(v,n) vll (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) ll x;cin >> x
#define cci(x,y) ll x,y;cin >> x >> y
#define co(x) cout << x << endl
#define pb push_back
#define eb emplace_back
#define rz resize
#define pu push
#define sz(x) int(x.size())
#define vij v[i][j]
// ll p = 1e9+7;
// ll p = 998244353;
// n do -> n*pi/180
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}
vi bt;
int m=1;
// i -> vi
void init(int i,int x){
// place
int now=i+m-1;
// operate
bt[now]^=x;
while(now){
int par=(now-1)/2;
int chi=par*2+1;
if(now==chi)chi++;
bt[par]=bt[now]^bt[chi];
now=par;
}
}
int f(int s,int t,int now=0,int l=0,int r=m){
// 完全に違う時
if(r<=s or t<=l)return 0;
// 完全に収まりきってる時
if(s<=l and r<=t)return bt[now];
int hf=(l+r)/2;
int cl=f(s,t,now*2+1,l,hf);
int cr=f(s,t,now*2+2,hf,r);
return cl^cr;
}
int main(){
cci(n,q);
reciv(v,n);
while(m<n)m*=2;
// 初期化
bt.rz(m*2-1,0);
rep(i,0,n){
// 代入と同時にテーブル造り
// 2回更新されていくことになるが確実
init(i,v[i]);
}
vi res;
rep(i,0,q){
cii(t);
cci(x,y);
x--;
if(t==1)init(x,y);
else {
int ans=f(x,y,0,0,m);
res.pb(ans);
}
}
for(int ans:res)co(ans);
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>pll;
typedef pair<ll,pair<ll,ll>>plll;
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
#define vll(v) v.begin(),v.end()
#define all(x) x.rbegin(),x.rend()
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define F first
#define S second
#define in freopen("input.txt", "r", stdin)
#define out freopen("output.txt", "w", stdout)
#define minheap int,vector<int>,greater<int>
#define pb push_back
#define eb emplace_back
#define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
#define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
#define bug cout<<"BUG"<<endl;
const int Max = 2e6 + 10;
const int Mod = 1e9 + 7;
const double PI =3.141592653589793238463;
bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
return (a.first > b.first);
}
ll lcm(ll a,ll b)
{
if(a==0 || b==0)return 0;
return a/__gcd(a,b)*b;
}
void input(ll ara[],ll n)
{
for(ll i=0; i<n; i++)cin>>ara[i];
}
void print(ll ara[],ll n)
{
for(ll i=0; i<n; i++)
cout<<ara[i]<<" ";
cout<<endl;
}
ll tree[300010];
ll query(ll idx)
{
ll sum=0;
while(idx>0)
{
sum^=tree[idx];
idx-= idx & (-idx);
}
return sum;
}
void update(ll idx,ll x,ll n)
{
while(idx<=n)
{
tree[idx]^=x;
idx+= idx & (-idx);
}
}
int main()
{
fastread();
ll i,j,n,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r,ans=0;
bool flag=false;
string str;
cin>>n>>t;
ll ara[n+2];
for(i=1; i<=n; i++)
{
cin>>ara[i];
update(i,ara[i],n);
}
ll test;
while(t--)
{
cin>>test>>a>>b;
if(test==1)
{
update(a,ara[a],n);
ara[a]=(ara[a]^b);
update(a,ara[a],n);
}
else
{
cout<<(query(b)^query(a-1))<<endl;
}
}
}
|
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll INF = 1LL << 60;
struct C{
int x, y;
C( int x = 0, int y = 0 ): x(x), y(y) {}
C operator*( const C& a ) const {
return C( x*a.x - y*a.y, x*a.y+y*a.x );
}
C operator-( const C&a ) const{
return C( x-a.x , y-a.y );
}
bool operator<( const C& a ) const{
if( x == a.x ) return y < a.y;
return x < a.x;
}
bool operator==( const C& a ) const{
return x == a.x && y == a.y;
}
int norm() const {
return x*x+y*y;
}
};
vector<C> MoveRot( vector<C> p, C move, C rot ){
int N = p.size();
for( int i = 0; i < N; i++ ){
p[i] = p[i] - move;
}
for( int i = 0; i < N; i++ ){
p[i] = p[i] * rot;
}
sort( p.begin(), p.end() );
return p;
}
int main(){
ll N;
cin >> N;
vector<C> a(N), b(N);
for( int i = 0; i < N; i++ ){
int x, y;
cin >> x >> y;
a[i] = C(x,y);
}
for( int i = 0; i < N; i++ ){
int x, y;
cin >> x >> y;
b[i] = C(x,y);
}
if( N == 1 ){
cout << "Yes" << endl;
return 0;
}
for( int i = 0; i < N; i++ ){
for( int j = 0; j < N; j++ ){
if( i == j ) continue;
if( (a[1]-a[0]).norm() != (b[j]-b[i]).norm() ) continue;
vector<C> ap = MoveRot( a, a[0], b[j]-b[i] );
vector<C> bp = MoveRot( b, b[i], a[1]-a[0] );
if( ap == bp ){
cout << "Yes" << endl;
return 0;
}
}
}
cout << "No" << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
struct io_setup{
io_setup(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
}
} io_setup;
int dot(int x1, int y1, int x2, int y2){
return x1*x2+y1*y2;
}
int det(int x1, int y1, int x2, int y2){
return x1*y2-x2*y1;
}
int main(){
int N; cin >> N;
if(N == 1) {cout << "Yes\n"; return 0;}
vector<int> a(N), b(N), c(N), d(N);
rep(i, N) cin >> a[i] >> b[i];
rep(i, N) cin >> c[i] >> d[i];
int x1 = a[1]-a[0], y1 = b[1]-b[0];
int r = dot(x1, y1, x1, y1);
vector<pii> p;
rep2(i, 2, N-1){
int x2 = a[i]-a[0], y2 = b[i]-b[0];
p.eb(dot(x1, y1, x2, y2), det(x1, y1, x2, y2));
}
sort(all(p));
rep(i, N) rep(j, N){
if(i == j) continue;
int X1 = c[j]-c[i], Y1 = d[j]-d[i];
int R = dot(X1, Y1, X1, Y1);
if(r != R) continue;
//cout << i << ' ' << j << '\n';
vector<pii> q;
rep(k, N){
if(k == i || k == j) continue;
int X2 = c[k]-c[i], Y2 = d[k]-d[i];
q.eb(dot(X1, Y1, X2, Y2), det(X1, Y1, X2, Y2));
}
sort(all(q));
if(p == q) {cout << "Yes\n"; return 0;}
}
cout << "No\n";
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,a,b) for (ll i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (ll i = (b), _a = (a); i >= _a; i--)
#define rep(i,n) for (ll i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (ll i = (n) - 1; i >= 0; i--)
#define debug(x) cout<<"debug : "<<x<<endl
#define debug2(x,y) cout<<"debug : "<<x<<" | "<<y<<endl
#define debug3(x,y,z) cout<<"debug : "<<x<<" | "<<y<<" | "<<z<<endl
#define debug4(x,y,z,w) cout<<"debug : "<<x<<" | "<<y<<" | "<<z<<" | "<<w<<endl
#define all(ar) ar.begin(), ar.end()
#define rall(ar) ar.rbegin(), ar.rend()
#define sz(x) ((long long)x.size())
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
#define mp make_pair
#define ff first
#define ss second
#define M 1000000007 // 1e9+7
#define INF 1e9 // 1e9
typedef pair<ll, ll> ii;
typedef pair<ll, ii> iii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<int> vint;
typedef vector<string> vs;
typedef vector<vi> vvi;
bool cus(pair<int,int> a, pair<int,int> b){
if(a.ff<b.ff)
return true;
if(a.ff>b.ff)
return false;
return a.ss<b.ss;
}
ll gcd(ll a, ll b){
if(b==0)
return a;
return gcd(b,a%b);
}
bool edg[19][19];
bool valid[1 << 18];
void solve(){
ll n, m;
cin >> n >> m;
ll a, b;
rep(i, m){
cin >> a >> b;
--a, --b;
edg[a][b] = true;
edg[b][a] = true;
}
for(ll i = 1; i < (1 << n); ++i){
vi tmp;
rep(j, 18){
if(i & (1 << j)) tmp.pb(j);
}
bool flg = true;
rep(j, tmp.size()){
rep(k, j){
if(!edg[tmp[j]][tmp[k]]) flg = false;
}
}
valid[i] = flg;
}
//debug("here");
ll dp[1 << n];
rep(i, 1 << n) dp[i] = INF;
dp[0] = 0;
fr(i, 1, (1<<n)-1){
if(valid[i]) dp[i] = 1;
else {
for(ll j = i;j;j = (j-1)&i){
dp[i] = min(dp[i], dp[j]+dp[i^j]);
}
}
}
cout << dp[(1 << n)-1] << endl;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout<<setprecision(11);
//ll t = 1;
//cin>>t;
//while(t--)
solve();
return 0;
}
| #include <bits/stdc++.h>
#define INF 10000000000000000LL
constexpr long long MOD = 1000000007;
//Get-Content input | ./a.exe > output
using namespace std;
void redirect_io() {
freopen("input", "r", stdin);
// freopen("output", "w", stdout);
}
void fast_io() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
template <typename Head>
void print(Head&& h) {
std::cout << h << std::endl;
}
template <typename Head, typename... Tail>
void print(Head&& h, Tail&&... t) {
std::cout << h << " ";
print(std::forward<Tail&&>(t)...);
}
int main() {
// redirect_io();
fast_io();
int n, q, k, t, m, a, b;
cin >> n >> m;
vector<vector<int>> gr(n, vector<int>(n));
for(int i = 0; i < m; i++) {
cin >> a >> b;
a--;b--;
gr[a][b] = gr[b][a] = 1;
}
int mx = (1 << n);
vector<int> f(mx, MOD);
for(int i = 1; i < mx; i++) {
vector<int> nodes;
for(int j = 0; j < n; j++) {
if((i >> j) & 1) {nodes.push_back(j);}
}
f[i] = 1;
for(int j = 0; j < nodes.size(); j++) {
for(int k = j + 1; k < nodes.size(); k++) {
if(gr[nodes[j]][nodes[k]] == 0) {f[i] = MOD; break;}
}
}
}
f[0] = 0;
for(int i = 0; i < mx; i++) {
for(int j = i; j > 0; j = (j - 1) & i) {
f[i] = min(f[i], f[j] + f[i ^ j]);
}
}
cout << f[mx - 1] << "\n";
return 0;
} |
// E - Count Descendants
#include <bits/stdc++.h>
using namespace std;
#define MAX 200000
vector<int> adj[MAX], T[MAX];
int L[MAX], R[MAX];
int depth, timer;
void dfs(int p){
L[p] = timer++;
T[depth].push_back(L[p]);
depth++;
for(int&c:adj[p]) dfs(c);
depth--;
R[p] = timer++;
}
int f(int d, int t){
return lower_bound(T[d].begin(), T[d].end(), t) - T[d].begin();
}
int main(){
int n; cin>>n;
for(int i=0; i<n-1; ++i){
int p; cin>>p;
adj[p-1].push_back(i+1);
}
dfs(0);
int q; cin>>q;
while(q--){
int u, d; cin>>u>>d;
cout<< f(d, R[u-1]) - f(d, L[u-1]) <<endl;
}
}
|
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MODADD(ADD_X,ADD_Y) (ADD_X+ADD_Y)%MOD
#define MODSUB(SUB_X,SUB_Y) ((SUB_X-SUB_Y)+MOD)%MOD
#define MODMUL(MUL_X,MUL_Y) (MUL_X*MUL_Y)%MOD
#define LCM(LCM_X,LCM_Y) (LCM_X/__gcd(LCM_X,LCM_Y))*LCM_Y
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
int dx[]= {1,0,-1,0,1,-1,1,-1};
int dy[]= {0,1,0,-1,1,-1,-1,1};
ll st[MAX],en[MAX],lv[MAX],sz[MAX],idx=1;
vector<int>adj[MAX],v[MAX];
void dfs(int src,int par)
{
st[src]=idx++;
v[lv[src]].push_back(st[src]);
int f=0;
for(auto i:adj[src])
{
if(i==par)
continue;
lv[i]=lv[src]+1;
dfs(i,src);
}
en[src]=idx++;
}
int main()
{
FastRead
//freopen("output.txt", "w", stdout);
int n;
cin>>n;
for(int i=2; i<=n; i++)
{
int u=i,v;
cin>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1,0);
int q;
cin>>q;
while (q--)
{
int u,d;
cin>>u>>d;
int ans=lower_bound(all(v[d]),en[u])-lower_bound(all(v[d]),st[u]);
cout<<ans<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define set_precision(ans,l) cout << fixed << setprecision(l)<<ans;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<ll>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<ll>())
#define init(a) memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
long double EPS = 1e-9;
/*struct comp {
bool operator() (const int& lhs, const int& rhs) const
{return lhs<rhs;}
};*/
ll cel(ll a,ll b){return((a-1)/b+1);}
ll gcd(ll a, ll b){
if (a < b)swap(a, b);
return (b == 0)? a: gcd(b, a % b);
}
ll MIN(ll a,int b){ll ans;(a>=b)?ans=b:ans=a;return ans;}
ll MAX(ll a,int b){ll ans;(a>=b)?ans=a:ans=b;return ans;}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll po(ll x,ll y){
ll ans=1;
while(y){
if(y&1){ans=(ans*x)%mod;}
y>>=1;x=(x*x)%mod;
}
return ans;
}
int main()
{
FIO;
ll n,t,x,y,m,k;
cin>>n>>k;
ll ans=0,a,b;
rep(i,2,2*n+1){
x=k+i;
if(x<2||(x>2*n)){continue;}
a=n-abs(n+1-i);
b=n-abs(n+1-x);
//cout<<i<<" "<<x<<" "<<a<<" "<<b<<"\n";
ans+=(a*b);
}
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (ll i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define pb push_back
using ll = long long;
using ld=long double;
using vin=vector<int>;
using vvin=vector<vin>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vst=vector<string>;
using P = pair<ll,ll>;
const int inf=1e9+7;
const ll INF=9e18;
const long double PI = acos(-1.0);
template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;}
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
bool ok[4][2000][2000];//migi hidari ue shita
bool light[2200][2200];
bool block[2200][2200];
int main(){//cout<<fixed<<setprecision(20);
ll h,w,n,m;
cin>>h>>w>>n>>m;
rep(i,0,n){
int a,b;
cin>>a>>b;
a--;b--;
light[a][b]=true;
}
rep(i,0,m){
int c,d;
cin>>c>>d;
c--;d--;
block[c][d]=true;
}
rep(i,0,h){
bool check=false;
rep(j,0,w){
if(light[i][j])check=true;
if(block[i][j])check=false;
if(check)ok[0][i][j]=true;
}
}
rep(i,0,h){
bool check=false;
for(int j=w-1;j>=0;j--){
if(light[i][j])check=true;
if(block[i][j])check=false;
if(check)ok[1][i][j]=true;
}
}
rep(j,0,w){
bool check=false;
rep(i,0,h){
if(light[i][j])check=true;
if(block[i][j])check=false;
if(check)ok[2][i][j]=true;
}
}
rep(j,0,w){
bool check=false;
for(int i=h-1;i>=0;i--){
if(light[i][j])check=true;
if(block[i][j])check=false;
if(check)ok[3][i][j]=true;
}
}
int ans=0;
rep(i,0,h){
rep(j,0,w){
bool q=0;
rep(k,0,4){
if(ok[k][i][j]){
q=true;
}
}
if(q)ans++;
}
}
cout<<ans<<endl;
} |
#include<bits/stdc++.h>
#define rep(i, n) for (Int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(Int i=(n)-1;i>=0;i--)
#define FOR(i,a,b) for(Int i=a;i<=Int(b);i++)
#define __MAGIC__ ios::sync_with_stdio(false);cin.tie(nullptr);
//#include <atcoder/all>
//using namespace atcoder;
typedef long long Int;
const long long INF = 1ll << 60;
using namespace std;
using p = pair<int,int>;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int main(){
__MAGIC__
int n;
cin >> n;
int day = 0;
int total = 0;
while (total < n) {
day++;
total += day;
}
cout << day << endl;
return 0;
// cout << ans << endl;
// cout << (ans ? "Yes" : "No") << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pb push_back
typedef long long ll;
const double pi = acos(-1.0);
#define memset(a,b) memset(a,b,sizeof(a))
const int mod = (1 ? 1000000007 : 998244353);
const int inf = (1 ? 0x3f3f3f3f : 0x7fffffff);
const int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 };
const ll INF = (1 ? 0x3f3f3f3f3f3f3f3f : 0x7fffffffffffffff);
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll poww(ll a, ll b) { ll s = 1; while (b) { if (b & 1)s = (s * a) % mod; a = (a * a) % mod; b >>= 1; }return s % mod; }
struct quickread {
template <typename _Tp> inline quickread& operator >> (_Tp& x) {
x = 0; char ch = 1; int fh = 1;
while (ch != '-' && (ch > '9' || ch < '0')) ch = getchar();
if (ch == '-') { fh = -1, ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
x *= fh; return *this;}
}cinn;
const int N = 1e5 + 11;
ll n,m;
void solve()
{
ll a,b,c,d;
cin>>a;
if(a%2==0)cout<<"White"<<endl;
else cout<<"Black"<<endl;
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
ll T = 1;
//cin >> T;
while (T--)solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
int i,all = 1;
cin >> n;
while (all < n) all <<= 1;
all -= 1;
for (i = 0; i < n; ++i){
int lc = all&(i<<1),rc = all&(i<<1|1);
if (lc >= n) lc -= (all+1)>>1;
if (rc >= n) rc -= (all+1)>>1;
++lc,++rc;
cout << lc << ' ' << rc << '\n';
}
return 0;
} | #define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int n;
string s1,s2,s3;
cin>>n>>s1>>s2>>s3;
rep(i,n) cout<<0;
rep(i,n) cout<<1;
cout<<0<<endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,first = 0,second = 0,findex = 0,sindex;
cin >> n;
vector<string>s(n);
vector<int>t(n);
for(int i = 0;i < n;i++){
cin >> s.at(i) >> t.at(i);
if(first < t.at(i)){
second = first;
first = t.at(i);
sindex = findex;
findex = i;
}else if(second < t.at(i)){
second = t.at(i);
sindex = i;
}
}
cout << s.at(sindex) << endl;
} |
/* AUTHOR
(_|_)
\_/
_______________________ÃMIT SÃRKER KISHÕR_______________________
----------------------------------------------------------------
_____DEPARTMENT OF CSE, CITY UNIVERSITY, DHAKA, BANGLADESH______
----------------------------------------------------------------
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ci std ::cin
#define co std ::cout
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sl(n) scanf("%lld",&n)
#define sll(n,m) scanf("%lld%lld",&n,&m)
#define pd(n) printf("%d",n)
#define pdd(n,m) printf("%d%d",n,m)
#define pl(n) printf("%lld",n)
#define pll(n,m) printf("%lld%lld",n,m)
#define p_line printf("\n")
#define line cout<<"\n"
#define cas(n) printf("Case %d: ",n++)
#define task return
#define loop(x,n) for(int x = 0 ; x < n ; x++)
#define constloop(x,a,n) for(int x = a ; x < n ; x++)
#define revloop(x,a,n) for(int x = a ; x >= n ; x--)
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define F first
#define S second
#define pb push_back
#define mkp make_pair
#define pi acos(-1)
const ll mx=100000;
const ll mod=1e9+7;
/*
ll P[mx+5],cnt=1;
bool nP[mx+5];
void sieve(void)
{
P[0]=2;
nP[0]=true;
nP[1]=true;
for(ll i=3 ; i<=mx ; i+=2)
{
if(nP[i]==true)
continue;
for(ll j=i+i ; j<=mx ; j+=i)
nP[j]=true;
P[cnt++]=i;
}
}
*/
/*
ll bigMod(ll a, ll b)
{
if(b<=0)
{
return 1;
}
ll value=bigMod(a,b/2);
value=(value*value)%mod;
if(b%2==1)
value=(value*a)%mod;
return value;
}
*/
/*
struct st
{
int value;
int pos;
};
*/
/*
bool cmp(st n,st m)
{
return n.a>m.a;
}
*/
/*
bool cmp(int n,int m)
{
return m>n;
}
*/
void solve()
{
//Code here
int t=1;
// ci>>t;
while(t--)
{
int n;
cin>>n;
priority_queue<pair<int,string>>pq;
for(int i=0 ; i<n ; i++)
{
string name;
int high;
cin>>name>>high;
pq.push({high,name});
}
pq.pop();
auto it=pq.top();
cout<<it.second;
line;
}
}
void I_O()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
I_O();
solve();
//line;
return 0;
} |
#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int,int> pii;
typedef pair<long long int,long long int> plli;
typedef vector< vi > vvi ;
typedef vector< vlli > vvlli ;
#define fi(i,a,b) for(int i=a;i<=b;i++)
#define flli(i,a,b) for(long long int i=a;i<=b;i++)
#define bi(i,a,b) for(int i=a;i>=b;i--)
#define blli(i,a,b) for(long long int i=a;i>=b;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define pi 2*acos(0.0)
#define pb push_back
#define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++)
#define present(v,num) (v.find(num)!=v.end())
#define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end())
#define pq priority_queue
#define mp make_pair
const int inf=INT_MAX;
const lli INF =LLONG_MAX;
const lli mod = 1e9+7;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast;
lli n;cin>>n;
vlli sum2(2*n+1,0);
flli(i,1,n)
{
sum2[i+1]++;
if(i+n+1<=2*n)
sum2[i+n+1]--;
}
flli(i,1,2*n)
sum2[i]=sum2[i-1]+sum2[i];
vlli sum3;
sum3.pb(0);
sum3.pb(0);
sum3.pb(0);
flli(i,3,n+2)
{
lli temp=((i-2)*(i-1))/2;
sum3.pb(temp);
}
for(lli i=n-2;i>0;i-=2)
{
lli temp=sum3.back();
temp=temp+i;
sum3.pb(temp);
}
//now just copy everything
lli idx=sz(sum3)-1;
if(n%2)idx--;
while(idx>2)
{
sum3.pb(sum3[idx]);
idx--;
}
lli szsum3=sz(sum3);
flli(i,1,szsum3-1)sum3[i]+=sum3[i-1];
lli k;cin>>k;
auto findi=lower_bound(all(sum3),k);
lli sumfind=findi-sum3.begin();
//find first number
k=k-sum3[sumfind-1];
flli(beauty,1,n)
{
if(beauty+2*n<sumfind)continue;
if(beauty+2>sumfind)continue;
if(sum2[sumfind-beauty]>=k)
{
//we found the beauty no
cout<<beauty<<" ";
//now we need to find taste and popularity
lli remsum=sumfind-beauty;
flli(taste,1,n)
{
lli newremsum=remsum-taste;
if(newremsum>=1 && newremsum<=n && k==1)
{
cout<<taste<<" "<<newremsum;
return 0;
}
else if(newremsum>=1 && newremsum<=n)
{
k--;
}
}
assert(false);
}
else
{
k-=sum2[sumfind-beauty];
}
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
static const ld pi = 3.141592653589793L;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
template <class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
//グラフ関連
struct Edge {
int to, rev;
ll cap;
Edge(int _to, ll _cap, int _rev) : to(_to), cap(_cap), rev(_rev) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &G, int from, int to, ll cap, bool revFlag, ll revCap) {
G[from].push_back(Edge(to, cap, (int)G[to].size()));
if(revFlag)
G[to].push_back(Edge(from, revCap, (int)G[from].size() - 1));
}
void solve() {
ll n, k;
cin >> n >> k;
ll num = 0;
ll pre = 0;
k--;
rep(s, 3, n * 3 + 1) {
//合計
ll tmp = pre;
if(s <= 2 * n + 1) {
ll r = min(s - 1 - 1, n);
ll l = max(1LL, s - 1 - n);
tmp += r - l + 1;
}
if(s >= n + 2) {
ll r = min(s - (n + 1) - 1, n);
ll l = max(1LL, s - (n + 1) - n);
tmp -= r - l + 1;
}
pre = tmp;
//cout << s << " " << num << en;
if(num + tmp <= k) {
num += tmp;
continue;
}
rep(i, 1, n + 1) {
ll r = min(s - i - 1, n);
ll l = max(1LL, s - i - n);
ll nxt = max(0LL,r - l + 1);
if(num + nxt <= k) {
//cout<<num<<" "<<nxt<<" "<<k<<en;
num += nxt;
continue;
}
ll j = k - num + l;
cout << i << " " << j << " " << s - i - j << en;
return;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// ll t;
// cin >> t;
// REP(i, t - 1) {
// solve();
// }
solve();
return 0;
}
|
#pragma GCC optimize("Ofast")
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
vector<pair<int,int>> g[205];
int dis[205];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
int main(){
int i,n,a,b,x,y;
scanf("%d%d%d%d",&a,&b,&x,&y);
for(i=1;i<=100;i++){
g[i].emplace_back(i+100,x);
g[i+100].emplace_back(i,x);
}
for(i=2;i<=100;i++){
g[i].emplace_back(i-1+100,x);
g[i+99].emplace_back(i,x);
}
for(i=1;i<=99;i++){
g[i].emplace_back(i+1,y);
g[i+1].emplace_back(i,y);
}
for(i=101;i<=199;i++){
g[i].emplace_back(i+1,y);
g[i+1].emplace_back(i,y);
}
pq.push({0,a});
for(i=1;i<=200;i++)
dis[i]=1000000000;
dis[a]=0;
while(pq.size()){
auto v=pq.top();
pq.pop();
for(auto x:g[v.second]){
if(dis[x.first]>dis[v.second]+x.second){
dis[x.first]=dis[v.second]+x.second;
pq.push({dis[x.first],x.first});
}
}
}
printf("%d\n",dis[100+b]);
}
| #include<bits/stdc++.h>
using namespace std;
inline int read()
{
int sum=0,nega=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')nega=-1;ch=getchar();}
while(ch<='9'&&ch>='0')sum=sum*10+ch-'0',ch=getchar();
return sum*nega;
}
int a,b,x,y,sum;
int main()
{
a=read(),b=read(),x=read(),y=read();
sum=abs(b-a);
if(a>b)cout<<min((sum*2-1)*x,x+(sum-1)*y)<<endl;
else cout<<min((sum*2+1)*x,sum*y+x)<<endl;
//cout<<min((sum*2-1)*x,x+)<<endl;
return 0;
}
|
//C++ 17
#include "bits/stdc++.h"
#define ull unsigned long long
#define ll signed long long
#define ld long double
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define vt vector
#define vi vector<int>
#define vvi vt<vi>
#define vl vector<ll>
#define vs vector<string>
#define pii pair<int,int>
#define pll pair<ll ,ll>
#define pb push_back
#define sz(x) int((x).size())
#define inf INT_MAX
#define linf LLONG_MAX
#define M(a,b) (((a-1)%b)+1)
#define ys(a) {cout<<((a)?"YES":"NO");return ;}
#define yes(a) {cout<<((a)?"YES":"NO")<<endl;}
#define cdiv(a,b) ((a+b-1)/b)
#define cfind(sr,el) (sr.find(el)!=sr.end())
#define ar array
#define EACH(iterable) for(auto & itr : iterable)
#define FOR_MAIN(i,s,e,u) for(int i = s ; i < e;i+=u)
#define FOR_1(n) FOR_MAIN(i,0,n,1)
#define FOR_2(i,n) FOR_MAIN(i,0,n,1)
#define FOR_3(i,s,e) FOR_MAIN(i,s,e,1)
#define FOR_4(i,s,e,u) FOR_MAIN(i,s,e,u)
#define GET_LOOP(a,b,c,d,e,...) e
#define FOR(...) GET_LOOP(__VA_ARGS__,FOR_4,FOR_3,FOR_2,FOR_1)(__VA_ARGS__)
#define GI_1(x) >>x
#define GI_2(a,b) GI_1(a)GI_1(b)
#define GI_3(a,b,c) GI_2(a,b)GI_1(c)
#define GI_4(a,b,c,d) GI_3(a,b,c)GI_1(d)
#define GI(...) int __VA_ARGS__ ;cin GET_LOOP(__VA_ARGS__,GI_4,GI_3,GI_2,GI_1)(__VA_ARGS__)
#define GLL(...) ll __VA_ARGS__ ;cin GET_LOOP(__VA_ARGS__,GI_4,GI_3,GI_2,GI_1)(__VA_ARGS__)
using namespace std;
#ifdef LOCAL
#define DBG_1(x) <<" | "<<#x<<":"<<setw(4)<<x
#define DBG_2(a,b) DBG_1(a)DBG_1(b)
#define DBG_3(a,b,c) DBG_2(a,b)DBG_1(c)
#define DBG_4(a,b,c,d) DBG_3(a,b,c)DBG_1(d)
#define DBG_5(a,b,c,d,e) DBG_4(a,b,c,d)DBG_1(e)
#define DBG_6(a,b,c,d,e,f) DBG_5(a,b,c,d,e)DBG_1(f)
#define GET_DBG(a,b,c,d,e,f,g,...) g
#define dbg(...) cerr<<"\033[00;33m"<<__LINE__<<"\033[01;34m" GET_DBG(__VA_ARGS__,DBG_6,DBG_5,DBG_4,DBG_3,DBG_2,DBG_1)(__VA_ARGS__)<<"\033[00m"<<endl
template<class T> void dbgarr(T arr[] , int n){ cerr<<arr[0]; FOR(i,1,n)cerr<<" "<<arr[i]; }
#else
#define dbg(...)
#define dbgarr(...)
#endif
const int mod = 1'000'000'007;//998'244'353;
const int UL5 = 1'00'001,UL6 = 1'000'001,UL25 = 2'00'001,UL8 = 1'00'000'001;
template<class T1,class T2> inline bool umax(T1 &a,const T2 &b){return (a < b)?a = b,1:0;}
template<class T1,class T2> inline bool emax(T1 &a,const T2 &b){return (a < b)?a = b,1:a==b;}
template<class T1,class T2> inline bool umin(T1 &a,const T2 &b){return (a > b)?a = b,1:0;}
template<class T1,class T2> inline bool emin(T1 &a,const T2 &b){return (a > b)?a = b,1:a==b;}
template<class T> void input(T arr[] , int n){ FOR(n) cin>>arr[i]; }
template<class T> void print(T arr[] , int n){ cout<<arr[0]; FOR(i,1,n)cout<<" "<<arr[i]; }
template<class T1,class T2> istream& operator >> (istream& in ,pair<T1,T2>& p){ in>>p.first>>p.second; return in; }
template<class T> istream& operator >> (istream& in ,vector<T>& arr){ EACH(arr) in>>itr; return in; }
template<class T1, class T2> ostream& operator << (ostream& out ,pair<T1,T2>& p){ out<<p.first<<" "<<p.second; return out; }
template<class T> ostream& operator << (ostream& out ,deque<T>& arr){ EACH(arr) out<<itr<<" "; return out; }
template<class T> ostream& operator << (ostream& out ,vector<T>& arr){ EACH(arr) out<<itr<<" "; return out; }
int arr[UL25]; int n;
ll k,ans;
void solve(){
string str;
cin>>str;
reverse(all(str));
EACH(str){
if(itr=='6' || itr=='9')
itr^='6'^'9';
cout<<itr;
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1;
if (0) cin>>tc;
for(int i = 1;i<=tc ;i++){
#ifdef LOCAL
cerr<<"\033[01;31mCase #"<<i<<": \n\033[00m";
#endif
//cout<<"Case #"<<i<<": ";
solve();
cout<<endl;
}
}
/*
Integer overflow,BS,dp,TP
*/
| #include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
#define tc(t) int t; cin >> t;while(t--)
#define lp(i, x, y) for(ll i = x; i <= y; i++)
#define lpr(i, x, y) for(ll i = x; i >= y; i--)
#define in1(x) ll x; cin >> x
#define in2(x, y) ll x, y; cin >> x >> y
#define in3(x, y, z) ll x, y, z; cin >> x >> y >> z
#define mem(x, y) memset(x, (y), sizeof(x))
#define ite(it, l) for (auto it = l.begin(); it != l.end(); it++)
const int MAX = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
int main()
{
// cin.tie(0); cout.tie(0); freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
string a;
cin >> a;
int n = a.size();
lpr(i, n-1, 0)
{
if(a[i] == '6')
cout << "9";
else if(a[i] == '9')
cout << "6";
else
cout << a[i];
}
cout << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for(int i=0; (i)<(n); (i)++)
#define REP2(i, a, b) for(int i=(a); (i)<(b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1,) (__VA_ARGS__)
#define MOD 1'000'000'007
template <class T> ostream& operator<<(ostream& os, const vector<T>& v){ os << "{"; for(size_t i=0; i<v.size(); i++) os << v[i] << (i+1==v.size() ? "" : ", "); os << "}"; return os; }
template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }
int main(){
int N, M;
cin >> N >> M;
vector<int> A(N+2), B(M+2);
REP(i, N) cin >> A[i+1];
REP(i, M) cin >> B[i+1];
vector<vector<int>> dp(N+2, vector<int>(M+2, 1<<30));
dp[0][0] = 0;
for(int i=0; i<=N; i++){
for(int j=0; j<=M; j++){
dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1);
dp[i+1][j] = min(dp[i+1][j], dp[i][j] + 1);
int tmp = (A[i+1] == B[j+1]) ? 0 : 1;
dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + tmp);
}
}
cout << dp[N][M] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define deb(k) cerr << #k << ": " << k << "\n";
#define size(a) (int)a.size()
#define fastcin cin.tie(0)->sync_with_stdio(0);
#define st first
#define nd second
#define pb push_back
#define mk make_pair
#define int long long
typedef long double ldbl;
typedef double dbl;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vint;
#define MAXN 300100
#define MAXLG 20
const int inf = 0x3f3f3f3f;
const ll mod = 1000000007;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const int N = 300100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int f(char c){
if('A' <= c && c <= 'F') return 10 + c - 'A';
return c - '0';
}
int dp[200100][18];
bool w[16];
void solve(){
string s;
int k;
cin>>s>>k;
int n = size(s);
int cnt = 0;
for(int i=0;i<n;i++){
for(int j=0;j<=16;j++){
dp[i+1][j] += j * dp[i][j];
dp[i+1][j] %= mod;
dp[i+1][j+1] += (16 - j)*dp[i][j];
dp[i+1][j+1] %= mod;
}
if(i){
dp[i+1][1] += 15;
dp[i+1][1] %= mod;
}
for(int j=0;j<f(s[i]);j++){
if( i == 0 && j == 0 ) continue;
if(w[j]){
dp[i+1][cnt] += 1;
dp[i+1][cnt] %= mod;
}
else{
dp[i+1][cnt+1] += 1;
dp[i+1][cnt+1] %= mod;
}
}
if(!w[f(s[i])]){
w[f(s[i])] = 1;
cnt++;
}
}
if(cnt == k){
dp[n][k] += 1;
dp[n][k] %= mod;
}
cout<<dp[n][k]<<"\n";
// Have you read the problem again?
// Maybe you understood the wrong problem
}
int32_t main(){
fastcin;
int t_ = 1;
//cin>>t_;
while(t_--)
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fo(i, x, y) for (int i = (x); i <= (y); ++i)
#define fd(i, x, y) for (int i = (x); i >= (y); --i)
const int maxn = 2e5 + 5;
struct edge{int to, w; edge *nxt;}*arc1[maxn], *arc2[maxn], pool[maxn << 2], *pt = pool;
int n, m;
int ans[maxn];
bool vis[maxn];
int getint()
{
char ch;
int res = 0, p;
while (!isdigit(ch = getchar()) && (ch ^ '-'));
p = ch == '-'? ch = getchar(), -1 : 1;
while (isdigit(ch))
res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return res * p;
}
void addedge(edge *arc[], int u, int v, int w)
{
*pt = (edge){v, w, arc[u]};
arc[u] = pt++;
}
void gettr(int x)
{
vis[x] = true;
for (edge *e = arc1[x]; e; e = e->nxt)
if (!vis[e->to])
{
addedge(arc2, x, e->to, e->w);
gettr(e->to);
}
}
void dfs(int x)
{
for (edge *e = arc2[x]; e; e = e->nxt)
{
if (e->w != ans[x])
ans[e->to] = e->w;
else
ans[e->to] = e->w - 1? e->w - 1 : e->w + 1;
dfs(e->to);
}
}
int main()
{
n = getint(); m = getint();
fo(i, 1, m)
{
int u, v, w;
u = getint(); v = getint(); w = getint();
addedge(arc1, u, v, w);
addedge(arc1, v, u, w);
}
gettr(1);
ans[1] = 1;
dfs(1);
fo(i, 1, n) printf("%d\n", ans[i]);
return 0;
} | #include<iostream>
#include<sstream>
#include<iomanip>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
#include<string>
#include<numeric>
#define rep(i,p) for(long long int i=0;i<p;i++)
#define reep(i,p) for(long long int i=1;i<=p;i++)
#define ll long long
#define mod 1000000007
using namespace std;
ll int kfj=0;
long long int sech(vector<ll int>& A,vector<vector<ll int> >& root,vector<bool>& bo,ll int n, ll int min, ll int sa){
ll int ans;
ll int kari;
ans=A[n]-min;
if(ans<sa){
ans=sa;
}
if(min>A[n]){
min=A[n];
}
for(ll int i:root[n]){
kari=sech(A,root,bo,i-1,min,ans);
bo[i-1]=true;
ans=std::max(kari,ans);
}
return ans;
}
int main(){
ll int N,M;
cin >> N >> M;
vector<vector<ll int> > root(N);
vector<ll int> A(N);
rep(i,N){
cin >> A[i];
}
ll int X,Y;
rep(i,M){
cin >> X >> Y;
root[X-1].push_back(Y);
}
vector <ll int> dp(N,100000000000000);
for(ll int i=0;i<N;i++){
for(ll int j:root[i]){
dp[j-1]=min({A[i],dp[i],dp[j-1]});
}
}
ll int ans=-1000000000000000;
rep(i,N){
ans=max(ans,A[i]-dp[i]);
// cout << dp[i] << " " ;
}
cout << ans;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int n,m;
int vdnmkhcmkxmh;
cin>>n>>m;
vector<pair<int,int>>a(m);
for(int i=0;i<m;i++)
{
cin>>a[i].first>>a[i].second;
}
int k;
cin>>k;
vector<pair<int,int>>b(k);
for(int j=0;j<k;j++)
{
cin>>b[j].first>>b[j].second;
}
ll l=0;
ll tot=1<<k;
for(ll mask=0;mask<tot;mask++)
{
vector<int>v(n+1);
ll ans=0;
for(int i=0;i<k;i++)
{
if(mask & (1<<i))
v[b[i].second]=1;
else
v[b[i].first]=1;
}
for(int i=0;i<m;i++)
{
if(v[a[i].first] && v[a[i].second])
ans++;
}
l=max(l,ans);
}
cout<<l<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>
ostream& operator<<(ostream &os, vector<T> &v){
string sep = " ";
if(v.size()) os << v[0];
for(int i=1; i<v.size(); i++) os << sep << v[i];
return os;
}
template<typename T>
istream& operator>>(istream &is, vector<T> &v){
for(int i=0; i<v.size(); i++) is >> v[i];
return is;
}
#ifdef DBG
void debug_(){ cout << endl; }
template<typename T, typename... Args>
void debug_(T&& x, Args&&... xs){
cout << x << " "; debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif
int main() {
ios_base::sync_with_stdio(false);
cout << setprecision(20) << fixed;
int n;
cin >> n;
vector<ll> a(n);
vector<int> t(n);
for(int i=0; i<n; i++) cin >> a[i] >> t[i];
int q;
cin >> q;
vector<ll> x(q);
cin >> x;
ll xl = -1e18, xr = 1e18;
ll yl = -1e18, yr = 1e18;
ll b = 0;
for(int i=0; i<n; i++){
if(t[i]==1){
yl += a[i];
yr += a[i];
b += a[i];
} else if(t[i]==2){
if(yr<=a[i]){
xl = xr = 1e18;
yl = yr = b = a[i];
} else if(yl<=a[i]){
yl = a[i];
xl = a[i]-b;
}
} else if(t[i]==3){
if(a[i]<=yl){
xl = xr = -1e18;
yl = yr = b = a[i];
} else if(a[i]<=yr){
yr = a[i];
xr = a[i]-b;
}
}
}
for(int i=0; i<q; i++){
if(x[i]<=xl){
cout << yl << endl;
} else if(x[i]<=xr){
cout << x[i]+b << endl;
} else {
cout << yr << endl;
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
int MOD;
struct ModInt {
long long v;
ModInt(long long _v = 0) {v = (-MOD < _v && _v < MOD) ? _v : _v % MOD; if (v < 0) v += MOD;}
ModInt& operator += (const ModInt &other) {v += other.v; if (v >= MOD) v -= MOD; return *this;}
ModInt& operator -= (const ModInt &other) {v -= other.v; if (v < 0) v += MOD; return *this;}
ModInt& operator *= (const ModInt &other) {v = v * other.v % MOD; return *this;}
ModInt& operator /= (const ModInt &other) {return *this *= inverse(other);}
bool operator == (const ModInt &other) const {return v == other.v;}
bool operator != (const ModInt &other) const {return v != other.v;}
friend ModInt operator + (ModInt a, const ModInt &b) {return a += b;}
friend ModInt operator - (ModInt a, const ModInt &b) {return a -= b;}
friend ModInt operator * (ModInt a, const ModInt &b) {return a *= b;}
friend ModInt operator / (ModInt a, const ModInt &b) {return a /= b;}
friend ModInt operator - (const ModInt &a) {return 0 - a;}
friend ModInt power(ModInt a, long long b) {ModInt ret(1); while (b > 0) {if (b & 1) ret *= a; a *= a; b >>= 1;} return ret;}
friend ModInt inverse(ModInt a) {return power(a, MOD - 2);}
friend istream& operator >> (istream &is, ModInt &m) {is >> m.v; m.v = (-MOD < m.v && m.v < MOD) ? m.v : m.v % MOD; if (m.v < 0) m.v += MOD; return is;}
friend ostream& operator << (ostream &os, const ModInt &m) {return os << m.v;}
};
ModInt dp[105][1000005];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k >> MOD;
dp[0][0] = 1;
int sum = n * (n + 1) / 2 * k;
for (int i=1; i<=n; i++)
for (int s=0; s<i; s++) {
ModInt cur = 0;
for (int j=s; j<=sum; j+=i) {
cur += dp[i-1][j];
if (j - i * (k + 1) >= 0)
cur -= dp[i-1][j-i*(k+1)];
dp[i][j] = cur;
}
}
for (int x=1; x<=n; x++) {
ModInt ret = 0;
for (int j=0; j<=sum; j++)
ret += dp[x-1][j] * dp[n-x][j] * (k + 1);
cout << ret - 1 << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
#define REPR(i,n) for (ll i = (n)-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = (m); i < (n); ++i)
#define FORR(i,m,n) for (ll i = (n)-1; i >= (m); --i)
#define FORE(x,xs) for (const auto& x : (xs))
#define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define SORT(v)) sort(ALL(v))
#define REV(v)) reverse(ALL(v))
#define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)),end(v));
#define CHMIN(x,y) x = min(x, (y))
#define CHMAX(x,y) x = max(x, (y))
#define BIT(x,i) (((x) >> (i)) & 1)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl
const int MAX = 44;
int N, T;
int A[MAX];
int solve() {
int N1 = N/2, N2 = N-N1;
vi t1, t2;
REP (s, 1<<N1) {
ll time = 0;
REP (i, N1) if ((s >> i) & 1) time += A[i];
if (time <= T) t1.push_back(time);
}
REP (s, 1<<N2) {
ll time = 0;
REP (i, N2) if ((s >> i) & 1) time += A[i+N1];
if (time <= T) t2.push_back(time);
}
UNIQ(t1);
UNIQ(t2);
ll ret = 0;
int j = t2.size()-1;
FORE(t, t1) {
while (j > 0 && t + t2[j] > T) j--;
CHMAX(ret, t + t2[j]);
}
return ret;
}
int main() {
cout << fixed << setprecision(15);
cin >> N >> T;
REP (i, N) cin >> A[i];
cout << solve() << endl;
}
|
/*
これを入れて実行
g++ code.cpp
./a.out
*/
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long INF = 1LL << 61;
const ll MOD = 1e9 + 7;
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
return f.second > s.second;
}
ll gcd(ll a, ll b){
if (b == 0)return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b){
return a / gcd(a, b) * b;
}
ll conbinationMemo[61][31];
void cmemoInit(){
rep(i, 61){
rep(j, 31){
conbinationMemo[i][j] = -1;
}
}
}
ll nCr(ll n, ll r){
if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
if(r == 0 || r == n){
return 1;
} else if(r == 1){
return n;
}
return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
ll nPr(ll n, ll r){
r = n - r;
ll ret = 1;
for (ll i = n; i >= r + 1; i--) ret *= i;
return ret;
}
//-----------------------ここから-----------
ll n;
vector<ll> t;
ll dp[110][100100];
int main(void){
cin >> n;
t.resize(n);
rep(i, n) cin >> t[i];
rep(i, 110){
rep(j, 100100){
dp[i][j] = INF;
}
}
dp[0][0] = 0;
rep(i, n){
rep(j, 100100){
if(j + t[i] < 100100) dp[i + 1][j + t[i]] = min(dp[i + 1][j + t[i]], dp[i][j]);
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + t[i]);
}
}
ll ans = INF;
rep(i, 100100){
ans = min(ans, max((ll)i, dp[n][i]));
}
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i,n,m) for(int i=(n);i<(m);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define REPR(i,n) for(int i=(n);i>=0;i--)
#define all(vec) vec.begin(),vec.end()
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using P=pair<ll,ll>;
using PP=pair<ll,P>;
using vp=vector<P>;
using vpp=vector<PP>;
using vs=vector<string>;
#define fi first
#define se second
#define pb push_back
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename A,typename B>istream&operator>>(istream&is,pair<A,B> &p){is>>p.fi>>p.se;return is;}
template<typename A,typename B>ostream&operator<<(ostream&os,const pair<A,B> &p){os<<"("<<p.fi<<","<<p.se<<")";return os;}
template<typename T>istream&operator>>(istream&is,vector<T> &t){REP(i,t.size())is>>t[i];return is;}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&t){os<<"{";REP(i,t.size()){if(i)os<<",";os<<t[i];}cout<<"}";return os;}
const ll MOD=1000000007LL;
const int INF=1<<30;
const ll LINF=1LL<<60;
int main(){
string st;
cin>>st;
REP(i,st.size()){
if(st[i]=='.'){
break;
}
cout<<st[i];
}
cout<<endl;
return 0;
} |
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;
int main(){
string S;
cin>>S;
int N=S.size();
int d[10]={0};
int flag=0;
for(int i=0;i<N;++i){
d[S[i]-'0']++;
}
if(N==1){
if(d[8]==1){
flag=1;
}
}
else if(N==2){
if((d[2]==1)&&(d[3]+d[7]==1)){
flag=1;
}
if((d[4]==1)&&(d[2]+d[6]==1)){
flag=1;
}
if((d[6]==1)&&(d[1]+d[5]+d[9]==1)){
flag=1;
}
if((d[8]==2)||((d[4]==1)&&(d[8]==1))){
flag=1;
}
}
else{
if(d[2]){
if(d[1]+d[5]+d[9]){
if(d[1]+d[3]+d[5]+d[7]+d[9]-1){
flag=1;
}
}
if(d[3]+d[7]){
if(d[2]+d[4]+d[6]+d[8]-1){
flag=1;
}
}
}
if(d[4]){
if(d[4]+d[8]-1){
if(d[1]+d[3]+d[5]+d[7]+d[9]){
flag=1;
}
}
if(d[2]+d[6]){
if(d[2]+d[4]+d[6]+d[8]-2){
flag=1;
}
}
}
if(d[6]){
if(d[3]+d[7]){
if(d[1]+d[3]+d[5]+d[7]+d[9]-1){
flag=1;
}
}
if(d[1]+d[5]+d[9]){
if(d[2]+d[4]+d[6]+d[8]-1){
flag=1;
}
}
}
if(d[8]){
if(d[2]+d[6]){
if(d[1]+d[3]+d[5]+d[7]+d[9]){
flag=1;
}
}
if(d[4]+d[8]-1){
if(d[2]+d[4]+d[6]+d[8]-2){
flag=1;
}
}
}
}
if(flag==1){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
return 0;
} | #include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("inline")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #define DEBUG
#ifdef DEBUG
#pragma region
#endif
using namespace std;
#define LL long long
#define PII pair<int, int>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define newl '\n'
#ifdef DEBUG
#define debug(...) cerr << '[', _debug(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9;
template <typename T>
void _debug(T a) {
cerr << a << ']' << endl;
}
template <typename T, typename... Args>
void _debug(T a, Args... args) {
cerr << a << ", ";
_debug(args...);
}
#ifdef DEBUG
#pragma endregion
#endif
string s;
int a[10];
void solve() {
cin >> s;
for (char c : s) {
++a[c - '0'];
}
if (s == "8") {
cout << "Yes";
return;
}
for (int i = 12; i < 100; i += 4) {
int ten = i / 10, digit = i % 10;
if (a[ten] >= 1) {
--a[ten];
if (a[digit] >= 1) {
debug(ten, digit);
--a[digit];
if (s.size() == 2) {
cout << "Yes";
return;
} else {
if (i % 8 == 0) {
if (a[2] >= 1 || a[4] >= 1 || a[6] >= 1 || a[8] >= 1) {
cout << "Yes";
return;
}
} else {
if (a[1] >= 1 || a[3] >= 1 || a[5] >= 1 || a[7] >= 1 || a[9] >= 1) {
cout << "Yes";
return;
}
}
}
++a[digit];
}
++a[ten];
}
}
cout << "No";
}
int main() {
// freopen("live.in", "r", stdin);
// freopen("live.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
// cout << fixed << setprecision(7);
// int n;
// cin >> n;
// rep(i, n)
// //
solve();
cout << flush;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int ll
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define endl "\n"
typedef long long ll;
#define MX 200002
int dp[MX];
void solve()
{
int n;
cin>>n;
if(n<0)
cout<<0<<endl;
else
cout<<n<<endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
while(t--)
{
solve();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int x;
cin>>x;
cout<<max(0,x);
return 0;} |
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
#define readFast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fin cin
#define ll long long
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define output(x) ((int)x && cout << "YES\n") || cout << "NO\n";
#define ld long double
using namespace std;
#ifdef LOCAL
#define read() ifstream fin("date.in.txt")
#else
#define read() readFast
#endif // LOCAL
const int N = 3e5 + 5;
const ll MOD = 998244353;
long double n;
int main() {
read();
fin >> n;
long double act = 0;
for (double i = 1; i < n; ++i) {
act += n / i;
}
cout << setprecision(6) << fixed << act;
return 0;
} /*stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
~Benq~*/
| // Problem: D - Journey
// Contest: AtCoder - AtCoder Beginner Contest 194
// URL: https://atcoder.jp/contests/abc194/tasks/abc194_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define vi vector<int>
#define vll vector<ll>
#define all(x) (x).begin() , (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rnd(x, y) uniform_int_distribution<ll>(x, y)(rng)
void dbg(){
cerr << endl;
}
template<typename Head , typename... Tail>
void dbg(Head h , Tail... t){
cerr << h << " ";
dbg(t...);
}
#ifdef EMBI_DEBUG
#define debug(...) cerr << "(" << #__VA_ARGS__ << "): ", dbg(__VA_ARGS__)
#else
#define debug(...)
#endif
const int max_n = 1e5 + 9;
const int mod = 1e9 + 7;
const int inf = 1e9;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
ll power(ll a , ll b)
{
ll prod = 1;
while(b)
{
if(b&1)
prod = (prod*a)%mod;
a = (a*a)%mod;
b >>= 1;
}
return prod;
}
void solve(){
int n;
cin >> n;
long double dp[n+1];
dp[n] = 0;
for(int i = n-1 ; i >= 1 ; i--) {
dp[i] = dp[i+1] + (1.0*n)/(n-i);
}
cout << fixed << setprecision(11) << dp[1] << "\n";
}
signed main(){
int t = 1;
// cin >> t;
for(int i = 1 ; i <= t ; i++){
solve();
}
}
|
/* -*- coding: utf-8 -*-
*
* c.cc: C - Camels and Bridge
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 8;
const int MAX_M = 100000;
const long long LINF = 1LL << 62;
/* typedef */
typedef long long ll;
typedef pair<int,int> pii;
/* global variables */
int wis[MAX_N], ps[MAX_N], cls[MAX_N];
int vs[MAX_M], mls[MAX_M + 1];
pii vls[MAX_M];
/* subroutines */
/* main */
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%d", wis + i);
sort(wis, wis + n);
for (int i = 0; i < m; i++)
scanf("%d%d", &vls[i].second, &vls[i].first);
sort(vls, vls + m);
if (wis[0] > vls[0].first) { puts("-1"); return 0; }
for (int i = 0; i < m; i++) vs[i] = vls[i].first;
mls[0] = 0;
for (int i = 0; i < m; i++) mls[i + 1] = max(mls[i], vls[i].second);
for (int i = 0; i < n; i++) ps[i] = i;
ll minl = LINF;
do {
cls[0] = 0;
for (int i = 1; i < n; i++) {
int wsum = wis[ps[i]];
ll lsum = 0;
cls[i] = 0;
for (int j = i - 1; j >= 0; j--) {
wsum += wis[ps[j]];
int k = lower_bound(vs, vs + m, wsum) - vs;
if (mls[k] > lsum + cls[i]) cls[i] = mls[k] - lsum;
lsum += cls[j];
}
}
ll sum = 0;
for (int i = 1; i < n; i++) sum += cls[i];
if (minl > sum) minl = sum;
} while (next_permutation(ps, ps + n));
printf("%lld\n", minl);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
//cin >>t;
while(t--){
int n,q,x=0,y;
cin>>n>>q;
int a[n],b[n];
for(int i=0;i<n;i++){
cin >>a[i];
b[i]=a[i]-x-1;
x=a[i];
}
for(int i=1;i<n;i++)
b[i]=b[i]+b[i-1];
while(q--){
int l=0,r=2e18,m;
cin>>x;
while(l<=r){
m=(l+r)/2;
if(m-(lower_bound(a,a+n,m)-a)>=x+1)
r=m-1;
else
l=m+1;
}
cout<<r<<endl;
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) {
cerr << ' ' << p.x << ',' << p.y; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for(auto x: v) os << ' ' << x; return os; }
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
for(auto x: v) os << ' ' << x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) {
for(auto x: v) os << ' ' << x; return os; }
struct d_ {
template<typename T> d_& operator,(const T& x) {
cerr << ' ' << x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
ll n;
gr g;
pll rec(ll u) { // ret (what takes Taker, odd/even)
ll ret = 1, cnt = 1;
vector<vll> o,e;
for(auto v: g[u]) {
auto [val, sz] = rec(v);
if(sz&1) o.pb({2*val-sz, val,sz-val});
else e.pb({2*val-sz, val,sz-val});
cnt += sz;
}
sort(all(o));
F(o.size()) ret += o[i][1+(i&1)];
F(e.size()) if(e[i][0]<=0) {
ret += e[i][1];
} else {
ret += e[i][1+(o.size()&1)];
}
return {ret, cnt};
}
int main(void) {
ios_base::sync_with_stdio(false);
cin >> n;
g = gr(n);
F(n-1) {
ll x; cin >> x; x--;
g[x].pb(i+1);
}
cout << rec(0).x << endl;
return 0;
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
vector<pll> RG;
vector<pll> GB;
vector<pll> RB;
/*
*/
ll A[200010];
char C[200010];
vector<pll> P(0);
ll N, r, g, b, dog;
ll ans = oo;
bool used[200010] = {};
void dfs(ll depth, ll pre, ll score)
{
if (r%2 == 0 && g%2 == 0 && b%2 == 0){
chmin(ans, score);
return ;
}
if (depth == 2) return ;
ll k, s;
rep(i, 3){
if (i==pre) continue;
if (i==0){//okになるid探す
ll sz = RG.size();
bool ok = false;
rep(j, sz){
k=RG[j].second;
s=RG[j].first;
if (used[k] || used[k+1]) continue;
else{
ok = true;
break;
}
}
if (!ok) continue;
r--, g--;
}
if (i==1){//okになるid探す
ll sz = GB.size();
bool ok = false;
rep(j, sz){
k=GB[j].second;
s=GB[j].first;
if (used[k] || used[k+1]) continue;
else{
ok = true;
break;
}
}
if (!ok) continue;
g--, b--;
}
if (i==2){//okになるid探す
ll sz = RB.size();
bool ok = false;
rep(j, sz){
k=RB[j].second;
s=RB[j].first;
if (used[k] || used[k+1]) continue;
else{
ok = true;
break;
}
}
if (!ok) continue;
r--, b--;
}
used[k] = true;
used[k+1] = true;
dfs(depth+1, i, score + s);
// 復元
if (i==0) r++, g++;
if (i==1) g++, b++;
if (i==2) r++, b++;
used[k] = false;
used[k+1] = false;
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
cin >> N;
dog = N*2;
rep(i, dog){
ll a;
char c;
cin >> a >> c;
A[i] = a;
C[i] = c;
if (c == 'R') r++;
if (c == 'G') g++;
if (c == 'B') b++;
P.push_back({a, c});
}
if (r%2 == 0 && g%2 == 0 && b%2 == 0)
{
cout << 0 << endl;
return 0;
}
sort(all(P));
rep(i, dog-1){
ll pr=0, pg=0, pb=0;
if (P[i].second == P[i+1].second) continue;
if (P[i].second == 'R') pr++;
if (P[i].second == 'G') pg++;
if (P[i].second == 'B') pb++;
if (P[i+1].second == 'R') pr++;
if (P[i+1].second == 'G') pg++;
if (P[i+1].second == 'B') pb++;
ll dif = P[i+1].first - P[i].first;
if (pr && pg) RG.push_back({dif, i});
if (pg && pb) GB.push_back({dif, i});
if (pr && pb) RB.push_back({dif, i});
}
sort(all(RG));
sort(all(GB));
sort(all(RB));
dfs(0, -1, 0);
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
using ll=long long;
int main(){
cin.tie(0);ios::sync_with_stdio(false);
int k;
string s,t;
cin>>k>>s>>t;
vector<int>cnt(10),cs(10),ct(10);
rep(i,4){
cnt[s[i]-'0']++;
cnt[t[i]-'0']++;
cs[s[i]-'0']++;
ct[t[i]-'0']++;
}
double ans=0.0;
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
double prob=1.0;
cnt[i]++;
if(cnt[i]<=k){
prob*=1.0*(k-cnt[i]+1)/(9*k-8);
cnt[j]++;
if(cnt[j]<=k){
prob*=1.0*(k-cnt[j]+1)/(9*k-9);
cs[i]++,ct[j]++;
int ss=0,st=0;
for(int l=1;l<=9;l++){
ss+=l*pow(10,cs[l]);
st+=l*pow(10,ct[l]);
}
if(ss>st)ans+=prob;
cs[i]--,ct[j]--;
}
cnt[j]--;
}
cnt[i]--;
}
}
printf("%.12lf\n",ans);
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)
using namespace std;
typedef long long ll;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int X, Y, Z;
cin >> X >> Y >> Z;
cout << (Y * Z % X == 0 ? Y * Z / X - 1 : Y * Z / X) << endl;
}
|
#include<bits/stdc++.h>
#define rep(i,n) for(int i =0;i<n;++i)
#define reps(i,s,n) for(int i =s;i<n;++i)
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main()
{
int n, m;
cin >> n >> m;
if (m == 0)
{
cout << 1 << endl;
return 0;
}
vector<int> a(m);
rep(i, m) cin >> a[i];
sort(a.begin(), a.end());
vector<int> d(m+1);
int mn = 1e9 + 5;
rep(i, m)
{
if (i == 0) d[0] = a[0] - 1;
else d[i] = a[i] - a[i - 1] - 1;
if(d[i] != 0) mn = min(mn, d[i]);
}
d[m] = n - a.back();
int ans = 0;
rep(i, m+1)
{
if (d[i] == 0) continue;
ans += (d[i] + mn - 1) / mn;
}
cout << ans << endl;
return 0;
} | /*
Name:
Author: xiaruize
Date:
*/
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define form(i,j,n) for(int i=int(j);i<=int(n);i++)
#define pb push_back
#define mk make_pair
#define pii pair<int,int>
#define pis pair<int,string>
#define sec second
#define ll long long
#define fir first
#define sz(a) int((a).size())
int cnt[4][5005];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int n;
cin>>n;
string s;
cin>>s;
for(int i=0;i<n;i++)
{
if(i==0)
{
cnt[0][i]=0;
cnt[1][i]=0;
cnt[2][i]=0;
cnt[3][i]=0;
}
cnt[0][i]=cnt[0][i-1];
cnt[1][i]=cnt[1][i-1];
cnt[2][i]=cnt[2][i-1];
cnt[3][i]=cnt[3][i-1];
if(s[i]=='A')
cnt[0][i]++;
else if(s[i]=='G')
cnt[1][i]++;
else if(s[i]=='C')
cnt[2][i]++;
else
cnt[3][i]++;
}
int ans=0;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(cnt[0][j]-cnt[0][i-1]\
==cnt[3][j]-cnt[3][i-1]&&\
cnt[1][j]-cnt[1][i-1]\
==cnt[2][j]-cnt[2][i-1])
ans++;
}
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ls n << 1
#define rs n << 1 | 1
#define PB push_back
#define MP make_pair
#define LD double
#define int long long
#define LL long long
#define pii pair<int, int>
#define fi first
#define se second
const int N = 500;
const int M = 300;
const int mod = 1e9 + 7;
const LL inf = 1e18;
// inline char nc() {
// static char buf[1000000], *p1 = buf, *p2 = buf;
// return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++;
// }
// #define getchar nc
inline int read() {
int s = 0;
register bool neg = 0;
register char c = getchar();
for (; c < '0' || c > '9'; c = getchar())
neg |= (c == '-');
for (; c >= '0' && c <= '9'; s = s * 10 + (c ^ 48), c = getchar())
;
s = (neg ? -s : s);
return s;
}
int a;
inline int calc(int n) {
int res = 0;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
while (n % i == 0) n /= i, res++;
}
}
if (n != 1) res++;
return res;
}
signed main() {
// freopen("in1.in", "r", stdin);
// freopen("out.out", "w", stdout);
a = read();
for (int i = 1; i <= a; i++) {
printf("%lld ", calc(i) + 1);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long; using P = pair<int,int>;
using vl = vector<ll>; using vvl = vector<vl>;
#define mp make_pair
#define pb push_back
#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) for (int i=(n-1);i >= 0;i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)((v).size())
#define MAX(v) *max_element(all(v))
#define MIN(v) *min_element(all(v))
#define Sort(v) sort(all(v))
#define Rev(v) reverse(all(v))
#define vec(type,name,...) vector<type> name(__VA_ARGS__)
#define VEC(type,name,size) vector<type> name(size);in(name)
#define vv(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define VV(type,name,h,w) vector<vector<type>>name(h,vector<type>(w));in(name)
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
for (std::size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
inline void IN(void){return;}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){cin >> first;IN(rest...);return;}
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 INF = 1LL << 60;
const ll mod = 1000000007;
const int dx[8] = {1,0,-1,0,1,1,-1,-1}; const int dy[8] = {0,1,0,-1,1,1,-1,-1};
int main(){
cin.tie(0); ios::sync_with_stdio(false);
int n; cin >> n;
vector<char> s(n);
vector<int> A(n+1), T(n+1), G(n+1), C(n+1);
rep(i,n) cin >> s[i];
rep(i, 1, n+1) {
A[i] += A[i-1]; T[i] += T[i-1]; G[i] += G[i-1]; C[i] += C[i-1];
if(s[i-1] == 'A') A[i]++;
else if(s[i-1] == 'T') T[i]++;
else if(s[i-1] == 'G') G[i]++;
else C[i]++;
}
ll ans = 0;
rep(i, n) rep(j, i+1, n) {
if((j-i)%2 == 0) continue;
int a = A[j+1]-A[i];
int t = T[j+1]-T[i];
int g = G[j+1]-G[i];
int c = C[j+1]-C[i];
if(a == t && g == c) ans++;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin>>n;
n*=2;
int a[n],b[n];
for(int i=0;i<n;i++){
cin>>a[i];
b[i]=a[i];
}
sort(b,b+n);
vector<pair<int,int>> ai;
unordered_map<int,vector<int>> mp;
unordered_map<int,int> cnt;
for(int i=0;i<n;i++){
mp[b[i]].push_back(i);
}
for(int i=0;i<n;i++){
ai.push_back(make_pair(a[i],mp[a[i]][cnt[a[i]]]));
cnt[a[i]]++;
}
string ans(n,' ');
int khaali=n,open=0;
int right[n];
// for(int i=0;i<n;i++){
// cout<<ai[i].first<<' '<<ai[i].second<<'\n';
// }
for(int i=n-1;i>=0;i--){
// cout<<i<<' '<<khaali<<'\n';
if(ai[i].second<(n/2)){
if(khaali<n){
ans[i]='(';
ans[khaali]=')';
khaali=right[khaali];
}
else{
ans[i]=')';
open++;
}
}
else{
if(open>0){
ans[i]='(';
open--;
}
else{
right[i]=khaali;
khaali=i;
}
}
}
// assert(open==0);
cout<<ans<<'\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
} | #include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(int i=a; i<b; ++i)
#define FORR(i, a, b) for(int i=a; i>b; --i)
#define all(x) (x).begin(), (x).end()
#define printCon(con) for(const auto &x: con) cout<<x<<" "
#define MOD 1000000007
// operator overloading
template<typename T>
istream& operator>>(istream &istream, vector<T>& a)
{
for (auto &it : a)
istream >> it;
return istream;
}
template<typename T>
ostream& operator<<(ostream &stream, vector<T>& v)
{
for (const auto &it : v)
stream << it << " ";
return stream;
}
// Mathematical functions
int GCD(int a, int b) {
if(b==0) return a;
return GCD(b, a%b);
}
// #####START YOUR CODE HERE#####
void solve(int tc) {
int n; cin>>n;
int range = 0, arr[n];
for(int i=0; i<n; ++i) {
cin>>arr[i]; range += arr[i];
}
int sum = range/2;
bool dp[n+1][sum+1];
for(int i=0; i<=n; ++i) dp[i][0] = true;
for(int j=1; j<=sum; ++j) dp[0][j] = false;
for(int i=1; i<=n; ++i) {
for(int j=1; j<=sum; ++j) {
if(j - arr[i-1] >= 0) dp[i][j] = dp[i-1][j] || dp[i-1][j-arr[i-1]];
else dp[i][j] = dp[i-1][j];
}
}
int ans;
for(int j=sum; j>=0; --j) {
if(dp[n][j]) {
ans = j; break;
}
}
cout<<range-ans;
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t = 1;
// cin>>t;
FOR(i, 1, t+1) {
solve(i);
}
return 0;
}
|
#include <bits/stdc++.h>
#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))
#ifdef LOCAL
#define log(val) std::cerr << std::setw(3) << __LINE__ << "| " << #val << " = " << (val) << std::endl
#define dbg(proc) proc
#else
#define log(val) 0
#define dbg(proc) 0
#endif
template <typename T>
std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){os << "{";if(!v.empty()){os << v[0];for(std::size_t i=1;i<v.size();++i){os << ", " << v[i];}}return os << "}";}
using ll = std::int64_t;
using P = std::tuple<ll,int>;
int N, Q;
int a[200100], t[200100];
P p[200100];
ll res[200100];
int main(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> N;
for(int i=0;i<N;++i){
std::cin >> a[i] >> t[i];
}
std::cin >> Q;
for(int i=0;i<Q;++i){
int x;
std::cin >> x;
p[i] = std::make_tuple(x, i);
}
std::sort(p, p + Q);
// [0, l) 内はすべて同じ値 vl を取る
// (r, N-1] 内はすべて同じ値 vr を取る
// [l, r] には一様に acc が加えられている
int l = 0, r = Q - 1;
constexpr ll INF = 1001001001;
ll vl = -INF, vr = INF, acc = 0;
for(int i=0;i<N;++i){
if(t[i] == 1){
vl += a[i];
acc += a[i];
vr += a[i];
}else if(t[i] == 2){
while(l < Q && fst(p[l]) + acc <= a[i]){
l += 1;
}
vl = std::max<ll>(vl, a[i]);
vr = std::max<ll>(vr, a[i]);
}else{
while(r >= 0 && fst(p[r]) + acc >= a[i]){
r -= 1;
}
vl = std::min<ll>(vl, a[i]);
vr = std::min<ll>(vr, a[i]);
}
}
for(int i=0;i<l;++i){
res[snd(p[i])] = vl;
}
for(int i=l;i<=r;++i){
res[snd(p[i])] = fst(p[i]) + acc;
}
for(int i=r+1;i<Q;++i){
res[snd(p[i])] = vr;
}
for(int i=0;i<Q;++i){
std::cout << res[i] << std::endl;
}
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=20000010;
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll n;cin>>n;
vector<ll>a(n),t(n);
rep(i,n)cin>>a[i]>>t[i];
ll su=inf,ma=inf;
ll de=-inf,mi=-inf;
ll now=0;
rep(i,n){
if(t[i]==1){
now+=a[i];
ma+=a[i];
mi+=a[i];
}
if(t[i]==2){
if(mi>a[i])continue;
if(ma<=a[i]){
de=inf;
su=-inf;
ma=a[i],mi=a[i];
continue;
}
de=a[i]-now;
mi=a[i];
}
if(t[i]==3){
if(ma<a[i])continue;
if(mi>=a[i]){
de=inf;
su=-inf;
ma=a[i],mi=a[i];
continue;
}
su=a[i]-now;
ma=a[i];
}
//cout<<de<<' '<<su<<' '<<mi<<' '<<ma<<endl;
}
ll q;cin>>q;
rep(i,q){
ll x;cin>>x;
if(su<=de){
cout<<ma<<endl;
}else{
if(x>=su){
cout<<ma<<endl;
}else if(x<=de){
cout<<mi<<endl;
}else{
cout<<x+now<<endl;
}
}
}
} |
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
int main() {
char s, t;
cin >> s >> t;
if(s == 'Y') {
char c = toupper(t);
out(c);
}
else {
out(t);
}
re0;
} | #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rep(i,a,b) for(int i = (a) ; i<(b) ; ++i)
#define ar array
#define ll long long
#define pb push_back
#define sz(v) (int)(v).size()
#define endl '\n'
#define dbg(x) cerr << #x << " = " << x << endl
const int N = int(2e5) + 99;
const int INF = int(1e9) + 99;
const int mxN = 1000005;
const int MOD = 998244353;
const int MASK = 4;
ll binpow(ll a, ll p, ll m) {
a%=m;
ll ans = 1;
while(p>0) {
if(p&1) ans *= a, ans%m;
p>>=1;
a*=a;
a%=m;
}
return ans;
}
int h,w;
string s[1000];
bool valid(int x, int y) {
return min(x,y) >=0 && x<h && y<w;
}
bool isvalidr(int x, int y) {
int i=x,j=y;
while(valid(i+1,j-1)) {
++i,--j;
if(s[i][j]=='B') return false;
}
i=x,j=y;
while(valid(i-1,j+1)) {
--i,++j;
if(s[i][j]=='B') return false;
}
return true;
}
bool isvalid(int x, int y) {
int i=x,j=y;
while(valid(i+1,j-1)) {
++i,--j;
if(s[i][j]!='.') return false;
}
i=x,j=y;
while(valid(i-1,j+1)) {
--i,++j;
if(s[i][j]!='.') return false;
}
return true;
}
bool vis[2000] = {false};
void solve() {
cin>>h>>w;
rep(i,0,h) cin>>s[i];
ll ans = 1;
set<char> st[h+w-1];
rep(i,0,h) rep(j,0,w) {
if(s[i][j]!='.') st[i+j].insert(s[i][j]);
}
rep(i,0,h+w-1) if(sz(st[i])==2) ans=0;else ans *= (st[i].empty())?2:1,ans%=MOD;
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin>>t;
while(t--) {
solve();
}
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int inf = 1e18, mxn = 2e5;
// Global
void solve() {
int n;
cin >> n;
vector<pair<int, string>> v(n);
for (auto &i : v) {
cin >> i.second >> i.first;
}
sort(v.begin(), v.end(), greater<>());
cout << v[1].second << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// int t;
// cin >> t;
// while (t--)
solve();
return 0;
} | #include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
const int N=1000005;
int h[N],nxt[N],adj[N],u,v,t,a[N],n,dep[N],fa[N],mn[N][20],path[N],dfn[N],cnp,ans[N];
/*
il int MIN(ct p,ct q){return dep[p]<dep[q]?p:q;}
il void lca(it u,it v){
u=dfn[u],v=dfn[v],u>v?std::swap(u,v),0:0;
ct k=LG[v-u+1];
lc=MIN(mn[u][k],mn[v-(1<<k)+1][k]);
}
il int D(ct u,ct v){lca(u,v);return dep[u]+dep[v]-dep[lc]-dep[lc];}
il void ck(){
// for(it i=1;i<=n;++i) printf("%d ",a[i]);puts("");
for(it i=1;i<=n;++i)
for(it j=i+1;j<=n;++j)
if(D(i,j)>std::abs(a[i]-a[j])) return;
for(it i=1;i<=n;++i) printf("%d ",a[i]);puts("");
//exit(0);
}
il void Dfs(ct x){
if(x>n) return ck();
for(it i=1;i<=9;++i) a[x]=i,Dfs(x+1);
}
il void dfs(ct x){
path[++cnp]=x,dep[x]=dep[fa[x]]+1,dfn[x]=cnp;
for(it i=h[x],j;i;i=nxt[i])
if((j=adj[i])^fa[x])
fa[j]=x,dfs(j),path[++cnp]=x;
}
il void Pre(){
LG[0]=-1;for(it i=1;i<=cnp;++i) mn[i][0]=path[i],LG[i]=LG[i>>1]+1;LG[0]=0;
for(it j=1;j<=20;++j)
for(it i=1;i+(1<<j)<=cnp;++i)
mn[i][j]=MIN(mn[i][j-1],mn[i+(1<<j-1)][j-1]);
}*/
int mxd,id;
int son[N];
bool flag[N];
il void dfs(ct x){
path[++cnp]=x,dep[x]=dep[fa[x]]+1,dfn[x]=cnp;
for(it i=h[x],j;i;i=nxt[i])
if(((j=adj[i])^fa[x])&&(j^son[x]))
fa[j]=x,dfs(j);
if(son[x]) fa[son[x]]=x,dfs(son[x]);
path[++cnp]=x;
}
il void predfs(ct x){
flag[x]=(x==id);
for(it i=h[x],j;i;i=nxt[i])
if((j=adj[i])^fa[x]){
fa[j]=x,predfs(j);
if(flag[j]) son[x]=j,flag[x]=1;
}
}
il void DFS(ct x,ct fa,ct dep){
if(dep>mxd) mxd=dep,id=x;
for(it i=h[x],j;i;i=nxt[i])
if((j=adj[i])^fa)
DFS(j,x,dep+1);
}
il void add(){nxt[++t]=h[u],h[u]=t,adj[t]=v,nxt[++t]=h[v],h[v]=t,adj[t]=u;}
int main(){
scanf("%d",&n);it i;
for(i=1;i<n;++i) scanf("%d%d",&u,&v),add();
DFS(1,0,0),mxd=0,i=id,id=0,DFS(i,0,0),std::swap(i,id),predfs(i),dfs(i);
for(i=cnp;i;--i) ans[path[i]]=i;
for(i=1;i<=n;++i) printf("%d ",ans[i]);
//dfs(1),Pre();
//for(it i=1;i<=n;++i) for(it j=i+1;j<=n;++j) printf("i=%d j=%d D(i,j)=%d\n",i,j,D(i,j));
//Dfs(1);
//for(it i=1;i<=cnp;++i) printf("%d ",path[i]);puts("");
return 0;
}
|
#include <bits/stdc++.h>
//#include <bits/extc++.h>
//#include <immintrin.h>
using namespace std;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
for (auto &i : a)
in >> i;
return in;
}
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
for (auto &i : a) {
out << i << " ";
}
return out;
}
template<typename T, typename D>
istream &operator>>(istream &in, pair<T, D> &a) {
in >> a.first >> a.second;
return in;
}
template<typename T, typename D>
ostream &operator<<(ostream &out, const pair<T, D> &a) {
out << a.first << " " << a.second;
return out;
}
struct LogOutput {
template<typename T>
LogOutput &operator<<(const T &x) {
#ifdef DIVAN
cout << x;
#endif
return *this;
}
} dout, fout;
typedef long long ll;
typedef unsigned long long ull;
typedef double dl;
typedef complex<double> cd;
#define nl '\n'
#define elif else if
#define all(_v) _v.begin(), _v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) (int)(v.size())
#define sqr(_v) ((_v) * (_v))
#define eb emplace_back
#define pb push_back
#define mod(x, m) ((x) >= 0 ? ((x) % m) : ((((x) % m) + m) % m))
#define minq(x, y) x = min((x), (y))
#define maxq(x, y) x = max(x, (y))
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define vpi vector <pair <int, int, int>>
#define vi vector<int>
#define pi pair <int, int>
#define ti tuple <int, int, int>
const ll INFL = 9187201950435737471;
const ll nINFL = -9187201950435737472;
const int INF = 2139062143;
const int nINF = -2139062144;
const ull ULINF = numeric_limits<ull>::max();
const long double PI = acos(-1);
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(seed);
inline void IO() {
#ifdef DIVAN
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
}
#define int ll
const int MOD = 998244353;
const int MAXN = 100 + 15;
const int MAXE = 1e4 + 15;
int dp[MAXN][MAXE];
int ndp[MAXN][MAXE];
void Add(int &a, int b) {
a = (a + b) % MOD;
}
int Factorial(int x) {
if (x == 0) {
return 1;
}
return (Factorial(x - 1) * x) % MOD;
}
void Solve() {
int n;
cin >> n;
vi a(n);
cin >> a;
int need = 0;
for (auto u : a) {
need += u;
}
if (need & 1ll) {
cout << "0";
return;
}
need >>= 1ll;
dp[0][0] = 1;
for (auto u : a) {
memcpy(ndp, dp, sizeof(dp));
forn(i, MAXN) {
forn(j, MAXE) {
if (dp[i][j]) {
Add(ndp[i + 1][j + u], dp[i][j]);
}
}
}
memcpy(dp, ndp, sizeof(dp));
}
int sum = 0;
forn(i, MAXN) {
if (dp[i][need] != 0) {
Add(sum, (dp[i][need] * ((Factorial(i) * Factorial(n - i)) % MOD)) % MOD);
}
}
cout << sum;
}
signed main() {
auto startTime = chrono::high_resolution_clock::now();
IO();
int t = 1;
// cin >> t;
for (int i = 1; i <= t; ++i) {
// cout << "Case #" << i << ": ";
Solve();
}
auto endTime = chrono::high_resolution_clock::now();
chrono::duration<long double> duration = endTime - startTime;
cout << setprecision(6) << fixed;
fout << '\n' << "Time: " << duration.count();
return 0;
}
| #include <cctype>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <ios>
#include <iostream>
#include <iterator>
#include <string_view>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <climits>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <regex>
#include <set>
#include <bitset>
#include <list>
const int64_t infl = 1LL << 60;
const int64_t cc = pow(10, 9) + 7;
bool sort_pair(std::pair<int64_t, char> origin,std::pair<int64_t, int64_t> another) {return origin.first < another.first; }
template <typename T> bool chmin(T &a, const T& b) {if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b) {if(a < b) {a = b;return true; } return false; }
int main()
{
int64_t q,k,n,s,h,w,d,e,x;
std::cin>> n;
std::string str;
std::vector< int64_t > list( n );
int64_t dp[2][n+1];
k = 1;
for( int i = 0; i < n; i++ )
{
std::cin>> str;
if( str == "AND" )
{
list[i] = 1;
}
else
{
list[i] = 0;
}
}
dp[0][0] = 1;//False
dp[1][0] = 1;//True
for( int i = 0; i < n; i++ )
{
if( list[i] == 1 )//And
{
dp[0][i+1] = dp[0][i] * 2 + dp[1][i];
dp[1][i+1] = dp[1][i];
}
else
{
dp[0][i+1] = dp[0][i];
dp[1][i+1] = dp[1][i] * 2 + dp[0][i];
}
}
std::cout<< dp[1][n];
}
|
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <cmath>
typedef long long ll;
#define rep(i,n) for(i=0;i<n;i++)
#define pub push_back
#define pob pop_back
using namespace std;
int gcd(int a,int b){int r;while((r=a%b)!=0){a=b;b=r;}return b;}//最大公約数
int lcm(int a,int b){return (a*b)/gcd(a,b);}//最小公倍数
using P=pair<int,int>;
typedef struct time{
int score;
string col;
} tm_t;
int main() {
ll i,n;
cin>>n;
if(n==2){
cout<<'3'<<endl;
return 0;
}
ll ans=1;
if(n<=12){
for(i=2;i<=n;i++){
ans*=i;
}
cout<<ans+1<<endl;
return 0;
}else{
cout<<(ll)4*4*5*5*7*3*9*11*13*17*19*23*29+1<<endl;
return 0;
}
}
| #include <vector>
#include <array>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>
using namespace std;
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(12);
int n;
cin >> n;
vector<long long> A(n);
for (int i = 0; i < n; ++i) {
cin >> A[i];
}
long long sum = 0;
long long pre = 0;
long long res = 0;
long long mx = 0;
for (int i = 0; i < n; ++i) {
mx = max(mx, A[i]);
res = mx * (i + 1);
pre += A[i];
sum += pre;
res += sum;
cout << res << '\n';
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int x=floor(1.08*n);
if(x==206) cout<<"so-so"<<endl;
else if(x<206) cout<<"Yay!"<<endl;
else cout<<":("<<endl;
return 0;
}
| #include <bits/stdc++.h>
#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 chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define even(x) (x % 2 == 0 ? true : false)
#define odd(x) (x % 2 ? true : false)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = pair<int, int>;
//const ll INF = 1e18L + 5;
//const int INF = 1e9 + 5;
//const double pi = 3.14159265358979323846;
//const int mod = 1000000007;
//vector<vector<int>> dp(m, vector<int>(n));
void solve()
{
double n;
cin >> n;
n *= 1.08;
int x = (int)n;
if (x < 206) puts("Yay!");
else if (x == 206) puts("so-so");
else puts(":(");
}
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
}
|
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
ll a[100],b[100];
int main()
{
ll n,x,i,j;
scanf("%lld%lld",&n,&x);
for(i=0;i<n;i++)
scanf("%lld",&a[i]);
for(i=n-1;i>=0;i--)
{
b[i]=x/a[i];
x%=a[i];
}
ll ci=1,k=0,h;
for(i=0;i<n;i++)
{
h=ci;
if((b[i]+1)*a[i]!=a[i+1])
ci+=k;
if(b[i]!=0)
k+=h;
}
printf("%lld\n",ci);
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
int main() {
int N;
long long X;
cin >> N >> X;
vector<long long>A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
vector<vector<long long>> dp(N + 1, vector<long long>(2, 0));
dp[0][0] = 1;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < 2; ++j) {
long long d = (X / A[i] + j);
if (i != N - 1) {
d %= (A[i + 1] / A[i]);
}
if (0 == d) {
dp[i + 1][j] += dp[i][j];
}
else {
dp[i + 1][0] += dp[i][j];
dp[i + 1][1] += dp[i][j];
}
}
}
cout << dp[N][0] << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
if(n%4==0) cout<<"Even"<<endl;
else if(n%2==0) cout<<"Same"<<endl;
else cout<<"Odd"<<endl;
}
}//I refer to other commentaries. | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<vector<vector<int> > > vvvi;
typedef vector<vector<int> > vvi;
int main(){
int n;
cin>>n;
cout<<1;
for(int i =2; i<=n; i++){
int tmp = i;
int ans = 0;
while(tmp>0){
tmp /=2;
ans++;
}
cout<<" "<<ans;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define LL long long
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n; LL k; cin >> n >> k;
LL dp[4][3000005] = {0};
dp[0][0] = 1;
#if 0
// A slower way of calculating dp[i][j] is:
// Will give TLE: https://atcoder.jp/contests/abc200/submissions/22497898
for (int i = 0; i < 3; i ++) {
// O(N^2)
for (int j = 0; j <= 3 * n; j ++) {
for (int k = 1; k <= n; k ++) {
dp[i + 1][j + k] += dp[i][j];
}
}
}
// first of all, we know dp[i][j] > 0 if j <= i * n;
// next, dp[i][j] will be added to dp[i+1][j+1:j+n+1)
// so we can basically use dp[i+1][j] as a prefix to dp[i+1][j+1]
#else
for (int i = 0; i < 3; i ++) {
// O(N)
for (int j = 0; j <= i * n; j ++) {
dp[i+1][j+1] += dp[i][j];
dp[i+1][j+n+1] -= dp[i][j]; // remove from accumulation.
}
// accumulate / prefix sum
for (int j = 1; j <= (i + 1) * n; j ++)
dp[i+1][j] += dp[i+1][j-1];
}
#endif
// we accumulate the counts of sums until we reach k.
// sum_{i=3}^{x} dp[3][i] <= k. we want to find x.
int x;
for (int i = 3; i <= 3 * n; i ++) {
if (k <= dp[3][i]) { x = i; break; }
else { k -= dp[3][i]; }
}
// finally, we can iterate the beauty of the cake
for (int i = 1; i <= n; i ++) {
// i is the beauty.
// the maximum of the taste we can get is
// x (the sum) - i (beauty) - n (pop).
// the minimum is
// x - i - 1.
// we should constrain them between [1, n].
int jmi = max(1, x - i - n), jma = min(n, x - i - 1);
if (jmi > jma) continue; // in this case i is too small.
if (k > (jma - jmi + 1)) {
k -= (jma - jmi + 1); // these are the choices available for taste.
continue;
}
int y = jmi + k - 1;
int z = x - i - y;
cout << i << ' ' << y << ' ' << z << '\n';
return 0;
}
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)
using namespace std;
typedef long long ll;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
ll n, m, k;
vector<ll> a;
set<ll> st;
int cnt = 1600;
double calcerr(double x){
vector<double> dp(n + m + 1, 0);
double tmp = m;
double mm = (double) m;
for(int i = n-1; i >= 0; i--){
if(st.count(i) == 1){
dp[i] = x;
}else{
dp[i] = 1.0 / mm * tmp;
}
tmp += dp[i]+1;
tmp -= dp[i+m]+1;
}
return abs(dp[0] - x);
}
double sanbun(){
double l = 0.0, r = 1e100;
rep(i, cnt){
double m1 = (2.0 * l + r) / 3.0, m2 = (l + 2.0 * r) / 3.0;
double e1 = calcerr(m1), e2 = calcerr(m2);
if(e1 > e2){
l = m1;
}else{
r = m2;
}
}
assert((r - l) < 1e-3);
return l;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m >> k;
a.resize(k);
rep(i, k) cin >> a[i];
int cont = 0;
int mx_cont = 0;
rep(i, k){
if(i == 0) cont++;
else if(a[i] > a[i-1] + 1){
cont = 1;
}else{
cont++;
}
chmax(mx_cont, cont);
}
if(mx_cont >= m){
cout << -1 << endl;
exit(0);
}
rep(i, k) st.insert(a[i]);
cout << fixed << setprecision(10) << sanbun() << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int count=0;
for(int i=0;i<9;i++){
if(s[i]=='Z'&&s[i+1]=='O'&&s[i+2]=='N'&&s[i+3]=='e'){
count++;
i=i+3;
}
}
cout<<count<<endl;
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize ("-O3")
#define int long long
#define ld long double
#define endl "\n"
#define rep(i,begin,end) for (__typeof(end) i=begin-(begin>end); i!=(end)-(begin>end); i+=1-2*(begin>end))
#define umap unordered_map
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define fs first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define mii map<int,int>
#define pii pair<int,int>
#define vc vector
#define vi vc<int>
#define vvi vc<vi>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define w(t) int t; cin>>t; rep(tc,0,t)
#define clr(a,x) memset(a,x,sizeof a)
#define chkbit(x,i) ((x)&(1LL<<(i)))
#define setbit(x,i) ((x)|(1LL<<(i)))
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define ps(x,y) fixed<<setprecision(y)<<x
#define print(a, n) rep(i,0,n) cout<<a[i]<<" "; cout<<endl;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const int mod = 1e9+7;
const int mod2 = 998244353;
const long long inf = 1e18;
const long double PI = 3.141592653589793;
template<typename... T>
void in(T&... args) {((cin>>args), ...);}
template<typename... T>
void out(T&&... args) {((cout<<args<<" "), ...);}
template<typename... T>
void outln(T&&... args) {((cout<<args<<" "), ...); cout<<endl;}
int nxt(){int x;cin>>x;return x;}
int add(int a,int b,int mod=mod){int res=(a+b)%mod;return (res<0)?res+mod:res;}
int mul(int a,int b,int mod=mod){int res=(a*1LL*b)%mod;return (res<0)?res+mod:res;}
struct customCompare {
bool operator () (int x, int y) {
return x>y;
}
};
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
const int N = 2e5;
int codejam = 0, testcases = 0;
void solve() {
string s; cin >> s;
int n = s.length();
int cnt = 0;
rep (i,3, n) {
if (s.substr(i-3, 4) == "ZONe") {
cnt++;
}
}
cout << cnt << endl;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
assert(freopen("input.txt", "r", stdin));
// assert(freopen("output.txt", "w", stdout));
#endif
int t = 1;
if (testcases)
cin >> t;
for (int tc = 1; tc <= t; ++tc) {
if (codejam)
cout << "Case #" << tc << ": ";
solve();
}
// printclock;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;
int main(void)
{
ll n;
cin >> n;
if(n%2==0)
{
cout << "White" << endl;
}
else
{
cout << "Black" << endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define omii map<int,int>
#define umii unordered_map<int,int>
#define pqi priority_queue<int>
#define pqd priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define tc(t) int t; cin>>t; while(t--)
#define fl(n) for(int i=0; i<n; i++)
#define twodvect(m,n, intit) vector<vector<int>> v(m, vector<int>(n,0))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int32_t main()
{
c_p_c();
int n;
cin>>n;
if(n%2==0)
cout<<"White"<<"\n";
else cout<<"Black"<<"\n";
return 0;
} |
#include <iostream>
#include <algorithm>
constexpr int N = 105;
int n;
long long x;
int a[N];
int f[N][N];
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
std::cin >> n >> x;
for (int i = 1; i <= n; ++i) std::cin >> a[i];
long long ans = 1e18;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= n; ++j)
for (int k = 0; k < i; ++k)
f[j][k] = -1;
f[0][0] = 0;
for (int pos = 1; pos <= n; ++pos)
for (int j = i; j; --j)
for (int k = 0; k < i; ++k)
if (~f[j - 1][k])
f[j][(k + a[pos]) % i] = std::max(f[j][(k + a[pos]) % i], f[j - 1][k] + a[pos]);
if (~f[i][x % i]) ans = std::min(ans, (x - f[i][x % i]) / i);
}
std::cout << ans << '\n';
return 0;
}
| #include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
//#include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, y) \
distance(v.begin(), lower_bound(v.begin(), v.end(), y))
#define Upper_bound(v, y) \
distance(v.begin(), upper_bound(v.begin(), v.end(), y))
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;
ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
/* Macros reg. ends here */
const ll INF = 1LL << 50;
int main() {
// ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
ll n, x;
cin >> n >> x;
vvvll dp(n + 1, vvll(n + 1, vll(n + 1, -1)));
repLRE(i, 1, n) dp[0][i][0] = 0;
rep(_, n) {
ll a;
cin >> a;
vvvll ndp = dp;
rep(i, n) repLRE(j, 1, n) rep(k, j) {
if (dp[i][j][k] != -1) {
ll ni = i + 1;
ll nk = (dp[i][j][k] + a) % j;
chmax(ndp[ni][j][nk], dp[i][j][k] + a);
}
}
swap(ndp, dp);
}
ll ans = x;
repLRE(i, 1, n) rep(k, i) {
ll v = x - dp[i][i][k];
if (dp[i][i][k] != -1 && v % i == 0) {
ll tmp = v/i;
chmin(ans, tmp);
}
}
cout << ans << endl;
return 0;
}
|
/**
* author: pankaj_m05
* created: 02.01.2021 17:42:10
**/
#include<bits/stdc++.h>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
map<string, bool> mp1, mp2;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
mp1[s] = true;
if (s[0] != '!') {
mp2[s] = true;
}
}
for (auto& it : mp2) {
string t1 = it.first, t2 = "!" + it.first;
if (mp1[t1] && mp1[t2]) {
cout << t1;
return 0;
}
}
cout << "satisfiable";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) FOR(i,0,n)
#define req(i,n) for(int i = 1;i <=n;i++)
#define pai 3.14159265358979323846
const int INF = 1001001001;
typedef long long ll;
int A[3][3], N;
bool punched[3][3];
//using Graph = vector<vector<int>>;
const int MOD = 1000000007;
typedef pair<int,int> P;
//最大公約数
ll gcd(ll a,ll b){
if (a%b == 0){
return b;
}
else{
return gcd(b,a%b);
}
}
//最小公倍数
ll lcm(ll a,ll b){
return a /gcd(a,b) * b;
}
//素数判定
bool is_prime(long long N) {
if (N == 1) return false;
for (long long i = 2; i * i <= N; ++i) {
if (N % i == 0) return false;
}
return true;
}
// 素因数分解
vector<pair<long long, long long> > prime_factorize(long long n) {
vector<pair<long long, long long> > res;
for (long long p = 2; p * p <= n; ++p) {
if (n % p != 0) continue;
int num = 0;
while (n % p == 0) { ++num; n /= p; }
res.push_back(make_pair(p, num));
}
if (n != 1) res.push_back(make_pair(n, 1));
return res;
}
// 10進数から2進数
int binary(int bina){
int ans = 0;
for (int i = 0; bina>0 ; i++)
{
ans = ans+(bina%2)*pow(10,i);
bina = bina/2;
}
return ans;
}
//Union-Find
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
for(int i = 0; i < N; i++) par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); //xの根をrx
int ry = root(y); //yの根をry
if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
ll powmod(ll x,ll y){
ll res=1;
for(ll i=0;i<y;i++){
res=res*x%MOD;
}
return res;
}
bool ok(int v, int b) {
while (v) {
if (v % b == 7) return false;
v /= b;
}
return true;
}
int main() {
int n;
cin >> n;
string s[n];
set <string> ans1,ans2;
rep (i,n) {
cin >> s[i];
if (s[i][0] != '!') ans1.insert(s[i]);
else ans2.insert(s[i]);
}
int d = ans2.size();
string ans = "satisfiable";
for (auto x:ans1) {
string s = "!";
s += x;
ans2.insert(s);
if (d == ans2.size()){
ans = x ;
break;
}
else {
d = ans2.size();
continue;
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
vector<long long> primes, X;
long long dp[73][(1<<20)];
int isPrime(int n)
{
for (int i=2; i*i<=n; i++)
if (n % i == 0)
return 0;
return 1;
}
long long solve(int n, int mask)
{
if (n < 0)
return 1;
if (dp[n][mask] != -1)
return dp[n][mask];
int canTake = 1;
int newMask = mask;
long long ans = solve(n-1, mask);
for (int i=0; i<primes.size(); i++)
{
if (X[n] % primes[i] == 0)
{
if (mask & (1 << i))
canTake = 0;
newMask |= (1 << i);
}
}
if (canTake)
ans += solve(n-1, newMask);
return dp[n][mask ] = ans;
}
int main()
{
for (int i=2; i<=71; i++)
if (isPrime(i))
primes.push_back(i);
long long a, b;
cin >> a >> b;
while (a <= b)
X.push_back(a++);
memset(dp, -1, sizeof(dp));
cout << solve(X.size() - 1, 0) << "\n";
return 0;
}
| #define rep(i, a, b) for (int i = a; i <= b; ++i)
#define rep2(i, a, b) for (int i = a; i < b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
void solve() {
int n, res = 0;
cin >> n;
rep (i, 1, n) {
int x;
cin >> x;
res += max(x - 10, 0);
}
cout << res << '\n';
}
signed main(int argc, char *argv[]) {
#ifdef ONLINE_JUDGE
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
int cas = 1;
// cin >> cas;
while (cas--) solve();
#ifndef ONLINE_JUDGE
system("pause");
#endif
return 0;
} |
#include<iostream>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll, P> State;
int closed[17][1<<17];
int main(){
int n;
cin >> n;
ll t[17][3];
for (int i = 0; i < n; i++) {
cin >> t[i][0] >> t[i][1] >> t[i][2];
}
memset(closed, -1, sizeof(closed));
closed[0][0] = 0;
priority_queue< State, vector<State>, greater<State> > open;
open.push(State(0, P(0, 0)));
while (!open.empty()) {
State st = open.top(); open.pop();
ll cost = st.first;
int pos = st.second.first;
int used = st.second.second;
if (pos == 0 && used == (1 << n) - 1) {
cout << cost << endl;
break;
}
if (closed[pos][used] != -1 && closed[pos][used] < cost) {
continue;
}
for (int nextPos = 0; nextPos < n; nextPos++) {
if (nextPos == pos) {
continue;
}
int nextUsed = used | (1 << nextPos);
ll nextCost = cost + abs(t[nextPos][0] - t[pos][0]) + abs(t[nextPos][1] - t[pos][1]) + max(0LL, t[nextPos][2] - t[pos][2]);
if (closed[nextPos][nextUsed] == -1 || closed[nextPos][nextUsed] > nextCost) {
open.push(State(nextCost, P(nextPos, nextUsed)));
closed[nextPos][nextUsed] = nextCost;
}
}
}
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
ll mod=1e9 +7;
ll dp[17][(1<<17)],n;
ll cost(pair<pair<ll,ll>,ll> &p1,pair<pair<ll,ll>,ll> &p2)
{
return max((ll)0,p2.second-p1.second)+abs(p1.first.first-p2.first.first)+abs(p1.first.second-p2.first.second);
}
ll solve(vector<pair<pair<ll,ll>,ll> > v,ll idx,ll mask)
{
if(mask== (1<<n)-1)
{
return cost(v[idx],v[0]);
}
if(dp[idx][mask]!=-1) return dp[idx][mask];
dp[idx][mask]=1e9;
for(int i=0;i<n;i++)
{
if(mask&(1<<i)) {}
else
{
dp[idx][mask]=min(dp[idx][mask],cost(v[idx],v[i])+solve(v,i,mask|(1<<idx)));
}
}
// cout<<idx<<" "<<mask<<" "<<dp[idx][mask]<<endl;
return dp[idx][mask];
}
int main()
{
cin>>n;
vector<pair<pair<ll,ll>,ll> > v(n);
for(int i=0;i<n;i++)
cin>>v[i].first.first>>v[i].first.second>>v[i].second;
memset(dp,-1,sizeof dp);
cout<<solve(v,0,0);
} |
/**
* author: Dooloper
* created: 26.10.2020 13:52:11
**/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i=0;i<n;i++)
typedef pair<int,int> P;
#define Mod 998244353
int main() {
ll h,w,ans=1;
cin >> h >> w;
vector <vector <char>> s(h,vector <char>(w));
rep(i,h){
rep(j,w){
cin >> s.at(i).at(j);
}
}
rep(i,h+w){
ll j=(i<h?i:h-1),k=i-j,co=0;
char tmp='o';
//cout << j << k << endl;
while(j>=0 && k<w){
//cout <<i<< j << k << endl;
if((tmp=='R' && s.at(j).at(k)=='B') || (tmp=='B' && s.at(j).at(k)=='R')){
cout << 0 << endl;
return 0;
}
//cout << 222 << endl;
//cout << j << k << endl;
if(s.at(j).at(k)=='.'){
co++;
}else if(s.at(j).at(k)=='R'){
tmp='R';
}else {
tmp='B';
}
j--;
k++;
//cout << 333 << endl;
}
if(co!=0 && tmp=='o')
ans=(ans*2)%Mod;
}
cout << ans << endl;
} | #include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;
int main(void){
int h,w,i,j;
long long ans = 1,mod=998244353;
vector<vector<int>> board;
cin >> h >> w;
board.assign(h, vector<int>(w,0));
for(i=0;i<h;i++) {
string s;
cin >> s;
for(j=0;j<w;j++) {
if(s[j] == 'R') board[i][j] = 1;
else if (s[j] == 'B') board[i][j] = 2;
else board[i][j] = 0;
}
}
for(i=0;i<h+w-1;i++) {
//long long tmp=0;
int color = 0;
//bool isupdate = false;
for(j=0;j<=i;j++) {
if(i-j >= w) continue;
if(j >= h) break;
//cout << j << " " << i-j << endl;
if(board[j][i-j]>0){
if(color==0) {
color = board[j][i-j];
} else {
if(color != board[j][i-j]){
color = -1;
break;
}
}
}
}
if(color == -1){
ans = 0;
break;
}
if(color==0){
ans *= 2;
ans %= mod;
}
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri();
printf("%d\n", (1 << n) - 1);
for (int i = 1; i < 1 << n; i++) {
std::string res;
for (int j = 0; j < 1 << n; j++) {
if (__builtin_popcount(i & j) & 1) res.push_back('A');
else res.push_back('B');
}
std::cout << res << std::endl;
}
return 0;
}
| #include <iostream>
#include <vector>
#include <algorithm>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
int n; cin >> n;
vector<int> a(n);
vector<int> b(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
int max_a = *max_element(a.begin(), a.end());
int min_b = *min_element(b.begin(), b.end());
int ans;
if (max_a > min_b) ans = 0;
else ans = min_b - max_a + 1;
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#include <set>
#include <stack>
using ll = long long;
using ull = unsigned long long;
using namespace std;
constexpr int inf = 1<<30;
constexpr int mo = 1e9+7;
constexpr ll infl = 1ll<<60;
constexpr int mo2 = 998244353;
vector<pair<int,int>> d = {{-1,0},{0,-1},{1,0},{0,1}};
int main(){
cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int h,w,x,y;
cin>>h>>w>>y>>x;
x--;y--;
vector<string> f(h);
for(auto & a :f)cin>>a;
int count = 0;
for(auto [dy,dx] : d){
int toy = y + dy;
int tox = x + dx;
while(true){
if(toy >= h or toy < 0 or tox >= w or tox < 0 or f[toy][tox] == '#')break;
count++;
toy += dy;
tox += dx;
}
}
count++;
cout << count << endl;
return 0;
} | //Making love with his ego Ziggy sucked up into his mind
#include<bits/stdc++.h>
using namespace std;
#define sz(x) int(x.size())
#define pb push_back
#define ff first
#define ss second
#define ter(x) cerr << #x << " = " << x << endl
typedef long long ll;
typedef pair<ll,ll> ii;
const int N=2e6;
int val[N],col[N];
int n,m;
char s[40];
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%s",s);
int pw=1;
int here=0;
for(int j=m-1;j>=0;j--){
if(s[j]=='1')
here|=pw;
pw<<=1;
}
val[i]=here;
}
col[0]=1;
int w=1;
for(int i=1;i<n;i++){
if((__builtin_popcount(val[0]^val[i]))%2==0)
val[i]=1,w++;
}
int b=n-w;
//ter(b);
//ter(w);
cout<<1LL*w*b<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
/* * * * * * * * * * */
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
/* * * * * * * * * * */
/* *
*
* Too many mind, no mind.
*
* */
bool isPrime(ll n) {
for (ll i = 2; i * i <= n; ++i) if (n % i == 0) return 0;
return 1;
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
vector <ll> primes;
for (ll i = 2; i <= 50; ++i) if (isPrime(i)) primes.push_back(i);
int n; cin >> n;
vector <ll> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
ll ans = LLONG_MAX;
for (int mask = 0; mask < (1 << (int)primes.size()); ++mask) {
int can = 0; ll cur = 1;
for (int i = 0; i < primes.size(); ++i) if (mask >> i & 1) cur *= primes[i];
for (int i = 0; i < n; ++i) {
for (int bit = 0; bit < primes.size(); ++bit) if (mask >> bit & 1) {
if (a[i] % primes[bit] == 0) {
can++;
break;
}
}
}
if (can == n) ans = min(ans, cur);
}
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin>>N;
vector<vector<int>> vec(N,vector<int>(3));
for(int i=0; i<N; i++){
for(int j=0; j<3; j++){
cin>>vec.at(i).at(j);
}
}
int min=0;
int64_t ans=1000000000;
for(int i=0; i<N; i++){
min=vec.at(i).at(0);
if(vec.at(i).at(2)-min>0&&ans>=vec.at(i).at(1))
ans=vec.at(i).at(1);
}
if(ans!=1000000000)
cout<<ans<<endl;
else
cout<<"-1"<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int rev = 1;
int a, b; scanf("%d%d", &a, &b);
if (a < b) swap(a, b), rev = -1;
int cur = 0;
for (int i = 1; i <= a; ++i) printf("%d ", i * rev), cur += i * rev;
for (int i = 1; i < b; ++i) printf("%d ", -i * rev), cur -= i * rev;
printf("%d\n", -cur);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int a,b;
int main(){
cin>>a>>b;
int ans=2*a+100-b;
cout<<ans;
} |
#include<iostream>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
int d;
if(n>=m) d=n-m;
else d=m-n;
if(d>=3) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define memf(a) memset(a,false,sizeof(a))
#define all(v) (v).begin(),(v).end()
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define mll map<ll,ll>
#define endl "\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define DEC(x) fixed<<setprecision(x)
#define FAST ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/__gcd((a),(b))
const int M=1000000007;
const int MM=998244353;
const long double PI = acos(-1);
const long long INF=2e18;
template<typename T,typename T1>void amax(T &a,T1 b){if(b>a)a=b;}
template<typename T,typename T1>void amin(T &a,T1 b){if(b<a)a=b;}
ll power( ll b, ll e)
{
if(e==0) return 1;
if(e&1) return b*power(b*b,e/2);
return power(b*b,e/2);
}
ll power(ll b,ll e,ll m)
{
if(e==0) return 1;
if(e&1) return b*power(b*b%m,e/2,m)%m;
return power(b*b%m,e/2,m);
}
ll modinv(ll a,ll m)
{
return power(a,m-2,m);
}
// #define _NCR_
int TLE_TERROR()
{
ll a,b;
cin>>a>>b;
ll n=abs(a-b);
if(n<3)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}
int main()
{
FAST
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll int TESTS=1;
// cin>>TESTS;
#ifdef _NCR_
initialvalues();
#endif
while(TESTS--)
TLE_TERROR();
return 0;
} |
//int max = 2 147 483 647 (2^31-1)
//ll max = 9 223 372 036 854 775 807 (2^63-1)
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i=0; i<n; i++)
#define mp make_pair
#define pb push_back
#define f first
#define s second
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
//Fast input and output
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);}
//Printing pairs and vectors
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";}
int main(){
int n;
cin >> n;
vector<pi>a(n);
forn(i,n){
int x,y;
cin >> x >> y;
a[i]=mp(x,y);
}
int ans=0;
forn(i,n){
for(int j=i+1;j<n;j++){
if(i==j){
continue;
}
pi p=a[i],q=a[j];
if(p.f>q.f){
swap(p,q);
}
int k = q.f-p.f;
if(q.s<=p.s+k and q.s>=p.s-k){
ans++;
}
}
}
cout << ans;
}
| /* Clearink */
#include <cstdio>
#include <cstdlib>
#include <assert.h>
const int MAXN = 200;
int n, match[MAXN + 5];
bool f[MAXN + 5], vis[MAXN + 5];
inline bool check ( const int l, const int r ) {
int stp = r - l + 1 >> 1; // i -> i + stp.
for ( int i = l, j; ( j = i + stp ) <= r; ++ i ) {
bool acci = 1 <= match[i] && match[i] <= n << 1;
bool accj = 1 <= match[j] && match[j] <= n << 1;
if ( match[i] == -1 || ( acci && match[i] ^ j )
|| match[j] == ( n << 1 | 1 ) || ( accj && match[j] ^ i )
|| ( !acci && !accj && match[i] && match[j] ) ) {
return false;
}
}
return true;
}
int main () {
scanf ( "%d", &n );
for ( int i = 1, a, b; i <= n; ++ i ) {
scanf ( "%d %d", &a, &b );
if ( ~a && ~b && a >= b ) return puts ( "No" ), 0;
if ( ~a && ~b ) match[a] = b, match[b] = a;
else if ( ~a ) match[a] = n << 1 | 1;
else if ( ~b ) match[b] = -1;
if ( ~a ) {
if ( vis[a] ) return puts ( "No" ), 0;
vis[a] = true;
}
if ( ~b ) {
if ( vis[b] ) return puts ( "No" ), 0;
vis[b] = true;
}
}
f[0] = true;
for ( int i = 2; i <= n << 1; i += 2 ) {
for ( int j = 0; j < i && !f[i]; j += 2 ) {
f[i] = f[j] && check ( j + 1, i );
}
}
puts ( f[n << 1] ? "Yes" : "No" );
return 0;
} |
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long LL;
const int MAXN = 2e5 + 100;
const int MOD = 1E9 + 7;
int n;
char c[MAXN];
template <class T>
void read(T& x) {
x = 0;
T f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
x *= f;
}
template <class T, class... Args>
void read(T& x, Args&... args) {
read(x), read(args...);
}
template <class T>
void write(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
LL qpow(LL a, LL b, LL MOD) {
LL ans = 1, base = a;
while (b) {
if (b & 1) ans = ans * base % MOD;
b >>= 1, base = base * base % MOD;
}
return ans;
}
void solve(int testNum) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> c[i];
for (int i = 1; i <= n; i++) {
if (c[i] == '0') {
if (i + 1 <= n && c[i + 1] != '1') {
cout << 0;
return;
}
if (i + 2 <= n && c[i + 2] != '1') {
cout << 0;
return;
}
if (i + 3 <= n && c[i + 3] != '0') {
cout << 0;
return;
}
} else {
if (i + 1 <= n) {
if (c[i + 1] == '0') {
if (i + 2 <= n && c[i + 2] != '1') {
cout << 0;
return;
}
if (i + 3 <= n && c[i + 3] != '1') {
cout << 0;
return;
}
} else {
if (i + 2 <= n && c[i + 2] != '0') {
cout << 0;
return;
}
if (i + 3 <= n && c[i + 3] != '1') {
cout << 0;
return;
}
}
}
}
}
if (n == 1) {
if (c[1] == '1')
cout << 2ll * (LL)pow(10ll, 10);
else
cout << (LL)pow(10ll, 10);
return;
}
if (c[1] == '1' && c[2] == '1')
cout << (LL)ceil(((LL)pow(10ll, 10) * 3 - (n - 1)) / 3.0);
else if (c[1] == '1' && c[2] == '0')
cout << (LL)ceil(((LL)pow(10ll, 10) * 3 - 1 - (n - 1)) / 3.0);
else
cout << (LL)ceil(((LL)pow(10ll, 10) * 3 - 2 - (n - 1)) / 3.0);
}
signed main() {
// freopen("1.in", "r", stdin);
int testCase = 1;
// read(testCase);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// cin >> testCase;
for (int i = 1; i <= testCase; i++) solve(i);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll=int64_t;
int main(){
ll n,ans,i,j;
string t;
cin>>n>>t;
if(n==1){
if(t[0]=='0') ans=1e10;
else if(t[0]=='1') ans=2e10;
}
else if(n==2){
if(t=="00") ans=0;
else if(t=="01") ans=1e10-1;
else ans=1e10;
}
else if(n==3){
if(t=="110") ans=1e10;
else if(t=="011"||t=="101") ans=1e10-1;
else ans=0;
}
else if(n==4){
if(t=="1101"||t=="1011"||t=="0110") ans=1e10-1;
else ans=0;
}
else{
i=0;
while(i<3&&(t[i]!='1'||t[i+1]!='1'||t[i+2]!='0')) i++;
if(i>2) ans=0;
else if(i==0){
j=3;
while(j<n/3*3&&t[j]=='1'&&t[j+1]=='1'&&t[j+2]=='0') j+=3;
if(j<n/3*3) ans=0;
else if(n%3==0) ans=1e10-n/3+1;
else if(n%3==1){
if(t[n-1]!='1') ans=0;
else if(t[n-1]=='1') ans=1e10-n/3;
}
else if(n%3==2){
if(t[n-2]!='1'||t[n-1]!='1') ans=0;
else ans=1e10-n/3;
}
}
else if(i==1){
if(t[0]!='0') ans=0;
else{
j=4;
while(j<n-2&&t[j]=='1'&&t[j+1]=='1'&&t[j+2]=='0') j+=3;
if(j<n-2) ans=0;
else if(n%3==1) ans=1e10-n/3;
else if(n%3==2){
if(t[n-1]!='1') ans=0;
else if(t[n-1]=='1') ans=1e10-n/3-1;
}
else if(n%3==0){
if(t[n-2]!='1'||t[n-1]!='1') ans=0;
else ans=1e10-n/3;
}
}
}
else if(i==2){
if(t[0]!='1'||t[1]!='0') ans=0;
else{
j=5;
while(j<n-2&&t[j]=='1'&&t[j+1]=='1'&&t[j+2]=='0') j+=3;
if(j<n-2) ans=0;
else if(n%3==2) ans=1e10-n/3;
else if(n%3==0){
if(t[n-1]!='1') ans=0;
else if(t[n-1]=='1') ans=1e10-n/3;
}
else if(n%3==1){
if(t[n-2]!='1'||t[n-1]!='1') ans=0;
else ans=1e10-n/3;
}
}
}
}
cout<<ans<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i = j; i < (n); i++)
int main() {
long long n; cin >> n;
int b = log(n) / log(2);
long long abc = 100100101101110;
for(;b >= 0; b--){
long long B = pow(2, b);
long long a = n/B;
long long c = n - a*B;
long long tmpabc = a + b + c;
if(abc > tmpabc){
abc = tmpabc;
}
}
cout << abc << endl;
return 0;
}
| //c++ テンプレ
#include<bits/stdc++.h>
using namespace std;
typedef long long llint;
typedef long double ld;
#define inf 1e18
#define mod 1000000007
#define sort(v) sort(v.begin(),v.end())
#define reverse(v) reverse(v.begin(),v.end())
#define rep(i,a,n) for(int i=a;i<n;i++)
priority_queue<llint,vector<llint>,greater<llint> > que;
priority_queue<llint> Que;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
void solve(){
llint n;
cin >> n;
llint a=0,b=0,c=0;
llint x=1;
llint ans=inf;
if(n==1)ans=1;
while(n>1){
if(n%2==1){
n-=1;
c+=x;
}
n/=2;
b+=1;
x*=2;
if(ans>n+b+c){
ans=n+b+c;
}
}
//printf("%ld %ld %ld\n",a,b,c);
cout << ans << endl;
}
int main(){
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define x first
#define y second
#define endl '\n'
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
typedef pair<double, double> PDD;
const int INF = 0x3f3f3f3f;
// const double INF = 1e20;
const double PI = acos(-1);
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
void quick_read() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
void solve() {
map<char, int> mp;
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
for (char c : s1) mp[c]++;
for (char c : s2) mp[c]++;
for (char c : s3) mp[c]++;
if (mp.size() > 10) {
cout << "UNSOLVABLE" << endl;
return;
}
vector<int> p(10);
iota(p.begin(), p.end(), 0);
do {
string n1, n2, n3;
int cnt = 0;
for (auto& it : mp) it.y = p[cnt++];
for (char c : s1) n1.push_back(mp[c] + '0');
for (char c : s2) n2.push_back(mp[c] + '0');
for (char c : s3) n3.push_back(mp[c] + '0');
LL a = stoll(n1), b = stoll(n2), c = stoll(n3);
if (n1[0] != '0' && n2[0] != '0' && n3[0] != '0' && a + b == c) {
cout << a << endl << b << endl << c << endl;
return;
}
} while (next_permutation(p.begin(), p.end()));
cout << "UNSOLVABLE" << endl;
}
int main() {
quick_read();
solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<int(n); i++)
#define print(x) cout << (x) << endl
int main() {
int n,k,m; cin >> n >> k >> m;
vector<int> a(n-1);
rep(i,n-1) cin >> a[i];
auto asum = accumulate(a.begin(), a.end(),0);
int ans = -1;
rep(i,k+1) {
if ((asum+i)/n >= m) {
ans = i;
break;
}
}
cout << ans << endl;
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using pairint = pair<int, int>;
template <typename T>
struct SegmentTree{
SegmentTree(unsigned int n, function<T(T, T)> AssociativeFunction, T ZeroElement=0){
length = 1;
fn = AssociativeFunction;
z = ZeroElement;
while(length < n*2){
length <<= 1;
}
Nodes.assign(length, z);
}
void assign(unsigned int index, T value){
unsigned int initindex = (length>>1) + index;
Nodes[initindex] = value;
for(unsigned int i=initindex>>1; i>0; i>>=1){
Nodes[i] = fn(Nodes[i*2], Nodes[i*2+1]);
}
}
T element(unsigned int index){
return Nodes[(length>>1) + index];
}
T query(unsigned int lindex, unsigned int rindex){
int l = (length>>1) + lindex;
int r = (length>>1) + rindex;
T res = z;
while(l <= r){
if(r==l){
if(res==z){
res = Nodes[l];
}else res = fn(res, Nodes[l]);
break;
}
if(l%2==1){
if(res==z){
res = Nodes[l];
}else res = fn(res, Nodes[l]);
l += 2;
}
if(r%2==0){
if(res==z){
res = Nodes[r];
}else res = fn(res, Nodes[r]);
r -= 2;
}
l>>=1;
r>>=1;
}
return res;
}
private:
T z;
unsigned int length;
vector<T> Nodes;
function<T(T, T)> fn;
};
int main(void){
int H, W, M, Xbuf, Ybuf;
long long res = 0;
vector<int> leftmost, uppermost;
vector<vector<int>> Obstructions;
cin >> H >> W >> M;
leftmost.assign(H, W);
uppermost.assign(W, H);
Obstructions.assign(W, {});
for(int i=0; i<M; i++){
cin >> Xbuf >> Ybuf;
Xbuf--;
Ybuf--;
leftmost[Xbuf] = min(leftmost[Xbuf], Ybuf);
uppermost[Ybuf] = min(uppermost[Ybuf], Xbuf);
Obstructions[Ybuf].push_back(Xbuf);
}
for(int i=0; i<uppermost[0]; i++)res += leftmost[i];
SegmentTree<int> st(H, plus<int>());
for(int i=uppermost[0]; i<H; i++)st.assign(i, 1);
for(int i=0; i<leftmost[0]; i++){
for(int obstruction: Obstructions[i])st.assign(obstruction, 1);
res += st.query(0, uppermost[i]-1);
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
template <class T1, class T2>
bool chmax(T1 &a, const T2 b) { if (a < b) { a = b; return true; } return false; }
template <class T1, class T2>
bool chmin(T1 &a, const T2 b) { if (a > b) { a = b; return true; } return false; }
template <class T>
istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; }
void co() { cout << '\n'; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) { cout << head << ' '; co(forward<Tail>(tail)...); }
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); }
void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); }
void setp(const int n) { cout << fixed << setprecision(n); }
constexpr int64_t LINF = 1000000000000000001;
constexpr int64_t MOD = 1000000007;
constexpr int64_t MOD_N = 998244353;
constexpr long double EPS = 1e-11;
const double PI = acos(-1);
template <typename T>
struct BIT {
int64_t N;
vector<T> data;
BIT(int64_t n) {
init(n);
}
const T &operator[](const int64_t i) const {
return at(i);
}
T at(int64_t k) { return sum(k + 1) - sum(k); }
void init(int64_t n) {
N = 1;
while (N < n) N <<= 1;
data.assign(N + 1, 0);
}
void build(vector<T> v) {
init(N);
for (int i = 0; i < v.size(); ++i) {
add(i, v[i]);
}
}
// v[k] += w
void add(int64_t k, T w) {
for (int64_t x = k + 1; x <= N; x += x & -x) {
data[x] += w;
}
}
// v[k] = x
void update(int64_t k, T x) {
add(k, x - at(k));
}
// v[0] + ... + v[k - 1]
T sum(int64_t k) {
T res = 0;
for (int64_t x = k; x > 0; x -= x & -x) {
res += data[x];
}
return res;
}
// v[a] + ... + v[b - 1]
T sum(int64_t a, int64_t b) { return sum(b) - sum(a); }
T lower_bound(T k) {
int64_t l = 0, r = N;
while (r - l > 1) {
int64_t m = (l + r) / 2;
if (sum(m) >= k) r = m;
else l = m;
}
return r;
}
};
int main(void) {
ll h, w;
cin >> h >> w;
ll m;
cin >> m;
VecP ps(m);
rep(i, m) cin >> ps[i].first >> ps[i].second;
sort(all(ps));
Vec my(h + 1, w + 1);
rep(i, m) chmin(my[ps[i].first], ps[i].second);
repn(i, h) {
if (my[i - 1] == 1) my[i] = 1;
}
BIT<ll> bit(w + 1);
set<ll> st;
if (my[1] <= w) {
REPN(i, my[1], w) {
bit.add(i, 1);
st.insert(i);
}
}
ce(st.size());
ll ans = 0;
ll pos = 0;
repn(i, h) {
while (pos < m && ps[pos].first == i) {
if (st.find(ps[pos].second) == st.end()) {
bit.add(ps[pos].second, 1);
st.insert(ps[pos].second);
}
++pos;
}
ans += bit.sum(my[i], w + 1);
}
co(h * w - ans);
return 0;
} |