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;
} |
End of preview. Expand
in Dataset Viewer.
No dataset card yet
New: Create and edit this dataset card directly on the website!
Contribute a Dataset Card- Downloads last month
- 10