code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
const int N=200005;
int n,k;
vector<int> e[N];
int vis[N],q[N],hi=1,ti,mid,up[N][20];
int get_k(int u) {
for(int i=0;i<=19;i++) if(mid&(1<<i)) u=up[u][i];
return u;
}
void dfs(int u,int x) {
// if(u==12) cout<<x<<endl;
// cout<<u<<' '<<x<<endl;
if(x==mid+1 || (vis[u]<=x+1 && vis[u]!=0)) return;
vis[u]=x+1;
for(int i=0;i<e[u].size();i++) {
dfs(e[u][i],x+1);
}
}
void get_fa(int u,int fa) {
up[u][0]=fa;
for(int i=1;i<=19;i++) up[u][i]=up[up[u][i-1]][i-1];
for(int i=0;i<e[u].size();i++) {
if(e[u][i]!=fa) get_fa(e[u][i],u);
}
}
bool check() {
int ans=0;
memset(vis,0,sizeof(vis));
for(int i=n;i>=1;i--) {
if(vis[q[i]]) continue;
ans++;
// cout<<"g"<<get_k(q[i])<<endl;
// cout<<vis[]
dfs(get_k(q[i]),0);
// for(int j=1;j<=n;j++) cout<<vis[]
// exit(0);
}
if(ans<=k) return 1;
else return 0;
}
signed main() {
n=read(),k=read();
for(int i=1,u,v;i<n;i++) {
u=read(),v=read();
e[u].push_back(v),e[v].push_back(u);
}
//求出每个点的距离
get_fa(1,1);
q[++ti]=1; vis[1]=1;
while(hi<=ti) {
int u=q[hi++];
for(int i=0;i<e[u].size();i++) {
if(!vis[e[u][i]]) q[++ti]=e[u][i],vis[e[u][i]]=1;
}
}
// mid=1;
// cout<<check()<<endl;
int l=0,r=2e5;
while(l<r) {
mid=(l+r)>>1;
if(check()) r=mid;
else l=mid+1;
}
cout<<l<<endl;
}
| #include <bits/stdc++.h>
#define lc (o<<1)
#define rc ((o<<1)|1)
#define PB push_back
#define MK make_pair
using namespace std;
#define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl
const int maxn = 2e5 + 5;
const int modu = 998244353; // 1e9 + 7
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
typedef long long LL;
void read(LL &x)
{
x=0;int f=0;char ch=getchar();
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x=f?-x:x;
return;
}
void read(int &x) { LL y; read(y); x = (int)y; }
void read(char &ch) { char s[3]; scanf("%s", s); ch = s[0]; }
void read(char *s) { scanf("%s", s); }
template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); }
int n, k, m;
int mind[maxn], f[maxn], maxd[maxn];
vector<int> g[maxn];
int dfs(int x, int fa) {
mind[x] = inf;
maxd[x] = 0;
f[x] = 0;
for (int y: g[x]) if (fa != y) {
dfs(y, x);
maxd[x] = max(maxd[x], maxd[y]+1);
mind[x] = min(mind[x], mind[y]+1);
f[x] += f[y];
}
if (maxd[x] + mind[x] <= m) {
maxd[x] = -1;
}
if (maxd[x] == m) {
f[x]++;
mind[x] = 0;
maxd[x] = -1;
}
return f[x];
}
int cal(int _m) {
m = _m;
dfs(1, 0);
if (maxd[1] >= 0) return f[1]+1;
else return f[1];
}
int bs(int l, int r) {
while (l < r) {
int mid = l+r >> 1;
if (cal(mid) <= k) r = mid;
else l = mid+1;
}
return l;
}
int main() {
// freopen("my.txt", "w", stdout);
read(n, k);
for (int i = 0; i < n-1; ++i) {
int u, v;
read(u, v);
g[u].PB(v);
g[v].PB(u);
}
printf("%d\n", bs(0, n));
return 0;
} |
#include <bits/stdc++.h> // New Template
using namespace std;
#define fo(i, n) for (i = 0; i < n; i++)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define MI map<int,int>
#define MC map<char,int>
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define endl "\n"
#define deb(x) cout << #x << "=" << x << "\n"
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << "\n"
#define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "\n"
#define deb4(x, y, z, w) cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "," << #w << "=" << w << "\n"
#define input(arr,n) for(i=0;i<n;i++) cin>>arr[i]
#define showarr(arr,n) for( i=0; i<n; i++) cout<<arr[i]<<" "; cout<<endl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi g[N];
ll a[N];
vector<vector<int>> adj;
vector<int> vis;
vector<ll> all_cp;
ll ma = -LLONG_MAX;
void dfs(ll u)
{
for (ll v : adj[u])
{ //deb3(u, v, a[u - 1]);
all_cp[v] = min({all_cp[v], a[u - 1], all_cp[u]});
}
}
void solve()
{
int i = 0, j = 0, n, m, k, flag = 0 , ans = 0, u, v;
cin >> n >> m;
ll CP = LLONG_MAX;
adj.resize(n + 1);
vis.resize(n + 1);
all_cp.resize(n + 1);
input(a, n);
fo(i, m)
{
cin >> u >> v;
adj[u].pb(v);
}
all_cp[0] = LLONG_MAX;
fo(i, n)
all_cp[i + 1] = LLONG_MAX;
// fo(i, n + 1)
// {
// cout << i << "---> ";
// fo(j, adj[i].size())
// cout << adj[i][j] << " ";
// cout << endl;
// }
for (i = 1; i <= n; i++)
{
dfs(i);
}
// fo(i, n) cout << i + 1 << " " << vis[i + 1] << " "; cout << endl;
// fo(i, n) cout << i + 1 << " " << all_cp[i + 1] << " "; cout << endl;
for (i = 1; i <= n; i++)
{
ma = max(a[i - 1] - all_cp[i], ma);
}
if (ma == -LLONG_MAX)
cout << 0;
else
cout << ma;
}
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//cin >> t;
while (t--)
{
solve();
}
return 0;
} | #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>
#define FOR_LT(i, beg, end) for (decltype(end) i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (decltype(end) i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (decltype(beg) i = beg; end <= i; i--)
#define REP(n) for (decltype(n) repeat_index = 0; repeat_index < n; repeat_index++)
using namespace std;
template<typename T, size_t S>
T minval(const T(&vals)[S])
{
T val = numeric_limits<T>::max();
FOR_LT(i, 0, S) {
val = min(val, vals[i]);
}
return val;
}
template<typename T, size_t S>
T maxval(const T(&vals)[S])
{
T val = numeric_limits<T>::min();
FOR_LT(i, 0, S) {
val = max(val, vals[i]);
}
return val;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
int n, m; cin >> n >> m;
vector<int64_t> vs(n + 1);
vector<int64_t> dp(n + 1, INT64_MIN);
FOR_LE(i, 1, n) {
cin >> vs[i];
}
vector<vector<int>> es(n + 1);
FOR_LT(i, 0, m) {
int a, b; cin >> a >> b;
es[a].push_back(b);
}
int64_t ans = INT64_MIN;
FOR_LE(i, 1, n) {
ans = max(ans, vs[i] + dp[i]);
dp[i] = max(dp[i], -vs[i]);
for (auto& p : es[i]) {
dp[p] = max(dp[p], dp[i]);
}
}
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
using ll=long long;
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (106)
ll n;
bitset<MAXN> vis;
vector<int> v[MAXN];
int main() {
FAST
cin>>n;
FOR(i,1,n) FOR(j,1,n) {
char c; cin>>c;
if(c=='1') {
v[i].eb(j);
}
}
ld ans = 0;
FOR(i,1,n) {
ll co = 0;
function<void(ll)>dfs=[&](ll x){
if(vis[x])return;
vis[x]=1;
for(auto i:v[x]) dfs(i);
};
FOR(j,1,n) {
vis.reset();
dfs(j);
co += (int)vis[i];
}
FOR(j,0,n-1) {
if(j>n-co) break;
ld p = 1;
FOR(k,0,j-1) {
p *= ld(n-co-k) / ld(n-k);
}
p *= (ld)1 / (n-j);
ans += p;
}
}
cout<<setprecision(15)<<fixed<<ans<<'\n';
}
| #include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),i##z=(b);i<=i##z;i++)
#define ROF(i,a,b) for(int i=(a),i##z=(b);i>=i##z;i--)
#define temT template<typename T>
using namespace std;
typedef double ld;
const int N=int(1e2)+10;
int n; char f[N][N]; ld res;
void bbfs(int i){//back bfs
static queue<int> que;
static bool vis[N]; int tot=0;
memset(vis,0,sizeof(vis));
que.push(i),vis[i]=1,tot=1;
while(que.size()){
int u=que.front(); que.pop();
FOR(v,1,n) if(f[v][u]=='1' && !vis[v])
tot++,que.push(v),vis[v]=1;
}
res+=1.0/tot;
}
int main(){
scanf("%d",&n);
FOR(i,1,n) scanf("%s",f[i]+1);
FOR(i,1,n) bbfs(i);
printf("%.15lf",res);
return 0;
} |
#include <algorithm>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <stack>
#include <queue>
#include <vector>
static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;
template <typename T> std::vector<T> vectors(std::size_t n, T val) {
return std::vector<T>(n, val);
}
template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}
template <class T> inline bool chmin(T &a, const T &b) {
return (a > b) ? a = b, true : false;
}
template <class T> inline bool chmax(T &a, const T &b) {
return (a < b) ? a = b, true : false;
}
template <class T> inline void chadd(T &a, const T &b) {
a += b, a %= MOD;
// TODO minus case
}
template <class T>
std::ostream &operator<<(std::ostream &stream, const std::vector<T> &vec) {
for (auto val : vec) {
if (std::is_fundamental<T>::value)
stream << " " << val;
else
stream << std::endl << val;
}
return stream;
}
int main() {
int A, B;
std::cin >> A >> B;
long long v = A * B;
double ans = v / 100.0;
std::cout << std::setprecision(10);
std::cout << ans << std::endl;
}
| //ver 8.1
#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;}
template<typename T> bool inside(T a,T b){auto it=a.begin()-1;each(x,b){it=find(it+1,a.end(),x);if(it==a.end())return false;}return true;}
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(a,b);
out(a*b/100.0);
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) vi (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);}
int main(){
cci(h,w);
vvi v(h+2,vi(w+2,-1));
rep(i,1,h+1){
rep(j,1,w+1)vij=0;
}
cci(n,m);
vvi lt(n,vi(2)),bl(m,vi(2));
rep(i,0,n){
rep(j,0,2)ci(lt[i][j]);
}
rep(i,0,m){
rep(j,0,2)ci(bl[i][j]);
v[bl[i][0]][bl[i][1]]=-1;
}
vvi tt=v,yk=v;
rep(i,0,n){
int x=lt[i][0];
int y=lt[i][1];
int nx=x,ny=y;
tt[x][y]=yk[x][y]=1;
while(tt[nx-1][y]==0)tt[--nx][y]=1;
while(yk[x][ny-1]==0)yk[x][--ny]=1;
nx=x,ny=y;
while(tt[nx+1][y]==0)tt[++nx][y]=1;
while(yk[x][ny+1]==0)yk[x][++ny]=1;
}
/*
rep(i,2,h+1){
rep(j,1,w+1){
if(yk[i][j]==-1)continue;
chmax(yk[i][j],yk[i-1][j]);
}
}
rep(i,2,w+1){
rep(j,1,h+1){
if(tt[j][i]==-1)continue;
chmax(tt[j][i],tt[j][i-1]);
}
}
*/
int ans=0;
rep(i,1,h+1){
rep(j,1,w+1){
if(yk[i][j]==1 or tt[i][j]==1){
// cout<<1<<" ";
ans++;
}
// else if(vij==-1)cout<<'x'<<" ";
// else cout<<0<<" ";
}
// co("");
}
co(ans);
} | #include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i < (n); ++i)
#define ll long long
#define pll pair<ll, ll>
using namespace std;
long memo[1001001][2];
vector<pll> colors;
// direction -> 1 = right; 0 = left;
long dp(long unsigned color, int direction){
if (color >= colors.size()) return 0;
if(memo[color][direction] != -1)
return memo[color][direction];
ll l = colors[color].first;
ll r = colors[color].second;
ll dist = r-l;
ll prev_pos = direction ?
colors[color-1].second:
colors[color-1].first;
memo[color][direction] = min(
dp(color+1, 0) + abs(prev_pos - r) + dist,
dp(color+1, 1) + abs(prev_pos - l) + dist
);
return memo[color][direction];
}
int main(){
int n;
cin >> n;
ll INF = LONG_LONG_MAX;
vector<ll> l(n, INF);
vector<ll> r(n, -INF);
ll x, c;
rep(i, n){
cin >> x >> c;
--c;
l[c] = min(l[c], x);
r[c] = max(r[c], x);
}
colors.emplace_back(0,0);
rep(i, n){
if (l[i] != INF)
colors.emplace_back(l[i], r[i]);
}
colors.emplace_back(0,0);
memset(memo, -1, sizeof(memo));
cout << dp(1, 0) << endl;
return 0;
} |
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){
walloc1d(arr, x2-x1, mem);
(*arr) -= x1;
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
sort(a, a+N);
}
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
int i;
pair<T1, T2>*arr;
walloc1d(&arr, N, &mem);
for(i=(0);i<(N);i++){
arr[i].first = a[i];
arr[i].second = b[i];
}
sort(arr, arr+N);
for(i=(0);i<(N);i++){
a[i] = arr[i].first;
b[i] = arr[i].second;
}
}
inline int my_getchar_unlocked(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread_unlocked(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline void rd(long long &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite_unlocked(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar_unlocked(a);
}
inline void wt_L(long long x){
int s=0;
int m=0;
char f[20];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar_unlocked('-');
}
while(s--){
my_putchar_unlocked(f[s]+'0');
}
}
int N;
long long K;
long long A[200000];
long long B[200000];
int main(){
int i;
wmem = memarr;
rd(N);
rd(K);
{
int Lj4PdHRW;
for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
rd(A[Lj4PdHRW]);
rd(B[Lj4PdHRW]);
}
}
sortA_L(N,A,B);
for(i=(0);i<(N);i++){
if(A[i] <= K){
K += B[i];
}
}
wt_L(K);
wt_L('\n');
return 0;
}
// cLay version 20210529-1 [beta]
// --- original code ---
// int N; ll K, A[2d5], B[2d5];
// {
// rd(N,K,(A,B)(N));
// sortA(N,A,B);
// rep(i,N) if(A[i] <= K) K += B[i];
// wt(K);
// }
| #include <bits/stdc++.h>
using namespace std;
int main(){
int n,k; cin >> n >>k;
vector<long long> points;
vector<int> money;
long long temp1;
int temp2;
for (int i = 0; i < n; i++){
cin >> temp1;
points.push_back(temp1);
cin >> temp2;
money.push_back(temp2);
}
vector<size_t> indices(points.size());
iota(indices.begin(), indices.end(), 0);
// ソートする。
sort(indices.begin(), indices.end(), [&points](size_t i1, size_t i2) {
return points[i1] < points[i2];
});
long long p;
p = k;
for (int i = 0; i < n; i++){
if (p < points[indices[i]]){
break;
}else{
p = p+money[indices[i]];
}
}
cout << p <<endl;
return 0;
} |
#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() {
cerr << endl;
}
template <typename H, typename... T>
void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T>
void print(T x) {
cout << x << "\n";
}
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;
ll INF = 1e16;
int inf = 1e9;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%MOD+MOD)%MOD){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD-a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = 1;
mint tmp = 1;
while(t > 0) {
if (t & 1) a *= tmp;
tmp *= *this;
t >>= 1;
}
return a;
}
// for prime MOD
mint inv() const {
return pow(MOD-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
return mint(*this) /= a;
}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
typedef vector<mint> vmi;
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
int h, w; cin >> h >> w;
vector<string> m(h);
REP(i, h) cin >> m[i];
vector<vi> cnt(h, vi(w, 0));
// left - right
REP(i, h) {
int j = 0;
int s = 0;
while(j < w) {
if (m[i][j] == '#') {
for (int k = j - 1; k > -1 && m[i][k] != '#'; --k) {
cnt[i][k] += j;
}
s = j + 1;
} else {
cnt[i][j] -= s;
}
++j;
}
for (int k = j - 1; k > -1 && m[i][k] != '#'; --k) {
cnt[i][k] += j;
}
}
// top
REP(j, w) {
int i = 0;
int s = 0;
while(i < h) {
if (m[i][j] == '#') {
for (int k = i - 1; k > -1 && m[k][j] != '#'; --k) {
cnt[k][j] += i;
}
s = i + 1;
} else {
cnt[i][j] -= s;
}
++i;
}
for (int k = i - 1; k > -1 && m[k][j] != '#'; --k) {
cnt[k][j] += i;
}
}
ll all = 0;
REP(i, h) REP(j, w) all += int(m[i][j] == '.');
mint ans = 0;
vmi t(all + 1, 1);
FOR(i, 1, all + 1) t[i] = t[i - 1] * 2;
REP(i, h) REP(j, w) {
if (m[i][j] == '#') continue;
int c = cnt[i][j] - 1;
ans += t[all] - t[all - c];
}
print(ans);
} | // .-""""-.
// / j \
// :.d; ;
// $P :
// .m._ $$ :
// dSMMSSSss.__$b. __ :
// :MMSMMSSSMMMSS$$b $P ;
// SMMMSMMSMMMSSS$$$$ :b
// dSMMMSMMMMMMSSMM$$b.dP SSb.
// dSMMMMMMMMMMSSMMPT$$=-. /TSSSS.
// :SMMMSMMMMMMMSMMP `b_.' MMMMSS.
// SMMMMMSMMMMMMMMM \ .'\ :SMMMSSS.
// dSMSSMMMSMMMMMMMM \/\_/; .'SSMMMMSSSm
// dSMMMMSMMSMMMMMMMM :.;'" :SSMMMMSSMM;
// .MMSSSSSMSSMMMMMMMM; :.; MMSMMMMSMMM;
// dMSSMMSSSSSSSMMMMMMM; ;.; MMMMMMMSMMM
// :MMMSSSSMMMSSP^TMMMMM ;.; MMMMMMMMMMM
// MMMSMMMMSSSSP `MMMM ;.; :MMMMMMMMM;
// "TMMMMMMMMMM TM; :`.: MMMMMMMMM;
// )MMMMMMM; _/\ :`.: :MMMMMMMM
// dSS$$MMMb. |._\\ :`.: MMMMMMMM
// T$S$$$$$$$$$m;O\\"-;`.:_.- MMMMMMM;
// :$$$$$$$$$$$$$$b_l./\ ;`.: mMMSSMMM;
// :$$$$$$$$$$$$$$$$$$$./\;`.: .$MSMMMMMM
// $$$$$$$$$$$$$$$$$$$$. \`.:.$$$SMSSSMMM;
// $$$$$$$$$$$$$$$$$$$$$. \.:$$$$SSMMMMMMM
// :$$$$$$$$$$$$$$$$$$$$$.//.:$$$SSSSSSSMM;
// :$$$$$$$$$$$$$$$$$$$$$$.`.:$SSSSSSSMMMP
// $$$$$$$$$;"^J "^$$$$;.`.$P `SSSMMMM
// :$$$$$$$$$ :$$$;.`.P'.. TMMM$b
// :$$$$$$$$$; $$$$;.`/ c^' d$$$$S;
// $$$$S$$$$; '^^^:_dg:___.$$$$$SSS
// $$$SS$$$$; $$$$$$$$$$$$$SSS;
// :$$SSSS$$$$ : $$$$$$$$$$$$SSS
// :P"TSSSS$$$ ; $$$$$$$$$$$$SSS;
// j `SSSSS$ : :$$$$$$$$$$$$SS$
// : "^S^' : $$$$$$$$$$$$S$;
// ;.____.-;" "--^$$$$$$$$$$$$P
// '-....-" bug ""^^T$$$P"
#include<bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define ll long long
#define ss second
#define ff first
#define pll pair<ll,ll>
#define vll vector<ll>
#define mll map<ll,ll>
#define mod 1000000007
#define sp " "
#define w(x) ll x; cin>>x; while(x--)
#define ps(x,y) fixed<<setprecision(y)<<x;
#define fo(i, j, k, in) for (ll i=j ; i<k ; i+=in)
#define re(i, j) fo(i, 0, j, 1)
#define pi 3.1415926535897932384626433832795
#define all(cont) cont.begin(), cont.end()
#define countbit(x) __builtin_popcount(x)
#define mod 1000000007
#define lo lower_bound
#define de(n) ll n;cin>>n;
#define def(a,n) ll n;cin>>n;ll a[n];re(i,n){cin>>a[i];}
#define defi(a,n,k) ll n;cin>>n; ll k;cin>>k;ll a[n];re(i,n){cin>>a[i];}
#define deb(x) cout<<#x<<"="<<x<<endl;
#define tr(it,a) for(auto it=a.begin();it!=a.end();it++)
#define nl cout<<endl;
using namespace std;
//KnightMareVoid
ll power(ll x,ll y,ll mad){
ll p=mad;
ll res =1;
x=x%p;
while(y>0){
if(y&1){
res=(res*x)%p;
}
y=y>>1;
x=(x*x)%p;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll a,b,c;
cin>>a>>b>>c;
a%=10;
if(a==5||a==6||a==1){
cout<<a<<endl;
return 0;
}
if(a==2 || a==8){
ll k=power(b,c,4);
if(k==0){
cout<<6<<endl;
}
else if(k==1){
cout<<(6*a)%10<<endl;
}
else if(k==2){
cout<<(6*a*a)%10<<endl;
}
else{
cout<<(6*a*a*a)%10<<endl;
}
}
else if(a==7 || a==3){
ll k=power(b,c,4);
if(k==0){
cout<<1<<endl;
}
else if(k==1){
cout<<(1*a)%10<<endl;
}
else if(k==2){
cout<<(1*a*a)%10<<endl;
}
else{
cout<<(1*a*a*a)%10<<endl;
}
}
else if(a==4){
ll k=power(b,c,2);
if(k==0){
cout<<6<<endl;
}
else if(k==1){
cout<<(6*a)%10<<endl;
}
}
else if(a==0){
cout<<0<<endl;
}
else if(a==9){
ll k=power(b,c,2);
if(k==0){
cout<<1<<endl;
}
else if(k==1){
cout<<(1*a)%10<<endl;
}
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define tej ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define ve vector<int>
#define vl vector<long long int>
#define vp vector<pair<long long int,long long int>>
#define pb push_back
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define bp pop_back
#define fp pop_front
#define mp make_pair
#define fo(i,n) for(i=0;i<n;i++)
#define unq(v) unique(v.begin(), v.end())-v.begin()
#define fnd(v,a) find(v.begin(), v.end(), a)
#define uprb(v,a) upper_bound(v.begin(), v.end(), a)
#define upra(v,n,a) upper_bound(v, v+n, a)
#define lowb(v,a) lower_bound(v.begin(), v.end(), a)
#define lowa(v,n,a) lower_bound(v, v+n, a)
#define newuv(v) v.erase(unique(v.begin(),v.end()),v.end())
#define rev(v) reverse(v.begin(),v.end())
#define rndshf(v) random_shuffle(v.begin(),v.end())
#define all(v) v.begin(),v.end()
ll po(ll a,ll b,ll m)
{
ll t=1;
while(b>0)
{
if(b%2!=0)
{
t=(t*a)%m;
}
a=(a*a)%m;
b=b/2;
}
return t;
}
// ll inv(ll a,ll m)
// {
// return po(a,m-2,m);
// }
// ll vis[100002]={0},x=0;
// vl v[100002];
// ll dfs(ll s)
// {
// ll c=1,i;
// for(i=0;i<v[s].size();i++)
// {
// if(vis[v[s][i]]==0)
// {
// vis[v[s][i]]=1;
// c+=dfs(v[s][i]);
// }
// }
// x+=c-1;
// //cout<<x<<endl;
// return c;
// }
void solve()
{
ll n,p,s=1,x,y;
cin>>n>>p;
if(p==2)
{
if(n==1)
cout<<"1";
else
cout<<"0";
return;
}
cout<<((p-1)*po(p-2,n-1,1000000007))%1000000007;
}
int main()
{
tej
ll t=1;
//cin>>t;
while(t--)
solve();
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;
const int m = 1e9+7;
ll n,p;
ll f(ll n){
if(n==0) return 1;
ll a = f(n>>1);
a = a*a%m;
if(n&1) a = a*(p-2)%m;
return a;
}
int main() {
cin >> n >> p;
ll ans = f(n-1);
ans = ans*(p-1)%m;
cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do { cout << "\033[32;1m " << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cout << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
ll rand_int(ll l, ll r) //[l, r]
{
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class T>
void print(T x,int suc=1)
{
cout<<x;
if(suc==1) cout<<'\n';
else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
for(int i=0;i<v.size();i++)
print(v[i],i==(int)(v.size())-1?suc:2);
}
const int mod=998244353;
const int maxn=2e5+7;
int fa[maxn];
int Find(int x)
{
return x==fa[x]?x:fa[x]=Find(fa[x]);
}
void Union(int x,int y)
{
x=Find(x),y=Find(y);
fa[y]=x;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
for(int i=0;i<n;i++) fa[i]=i;
vi f(n);
for(int i=0;i<n;i++)
{
cin>>f[i];
f[i]--;
Union(i,f[i]);
}
int cc=0;
for(int i=0;i<n;i++)
{
if(fa[i]==i) cc++;
}
int ans=1;
for(int i=0;i<cc;i++) ans=ans*2%mod;
ans=(ans-1+mod)%mod;
print(ans);
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pl;
struct edge {
ll to; // 辺の行き先
ll weight; // 辺の重み
edge(ll t, ll w) : to(t), weight(w) { }
};
typedef vector<vector<ll> > Graph;
typedef vector<vector<edge> > WGraph;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep3(i, k, n) for(int i = k; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define vout(v) for(int index_now=0; index_now < v.size(); i++) cout<<v[index_now]<<' '
#define blank cout<<endl
#define newvar(n) ll n; cin >> n
const ll MOD = 998244353;
const ll INF = MOD*MOD+1;
ll powmod(ll x, ll n, ll mod){
ll ret=1;
while(n>0){
if(n&1) ret = (ret * x)%mod;
x = (x*x)%mod;
n >>= 1;
}
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vl f(n+1);
rep(i,n) cin >> f[i+1];
vl seen(n+1, 0);
ll cnt = 0;
rep3(i, 1, n+1){
if(seen[f[i]]==2) continue;
//cout << i << endl;
ll now = i;
bool check = true; //すでに見られたことがあるかどうか
if(seen[now] == 2) check = false; //スタート地点が既訪だったら、なにもしない
queue<ll> que;
que.push(now);
while(seen[now] == 0){ //今回の道が、既訪頂点に続いているか
seen[now] = 1; //1は、この仮シミュレーションで訪れた場所
now = f[now]; //つぎ
if(seen[now] == 2) check = false; //かつて訪れていたら、なにもしない
que.push(now);
}
if(!check){
while(!que.empty()){
seen[que.front()] = 2;
que.pop();
}
continue;
}
now = i;
while(seen[now] != 2){
seen[now] = 2;
now = f[now];
}
seen[now] = 2;
cnt++;
}
cout << powmod(2, cnt, MOD) - 1;
return 0;
} |
// Template
#include "bits/stdc++.h"
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
struct IOSET {
IOSET() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} ioset;
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (T &element : vec) is >> element;
return is;
}
template <typename T>
vector<T> operator--(vector<T> &vec) {
for (T &element : vec) --element;
return vec;
}
// Main
int main() {
int n;
cin >> n;
vector<int> p(n);
cin >> p;
vector<int> cnt(200020, 0);
int ans = 0;
rep(i, n) {
++cnt[p[i]];
while (cnt[ans]) ++ans;
cout << ans << "\n";
}
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
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(){
int N;
vector<int> p;
cin >> N;
unordered_set<int> us;
for(int i=0; i < N; ++i){
int tmp;
cin >> tmp;
p.push_back(tmp);
}
int min=0;
for(int i=0; i < N; ++i){
us.insert(p[i]);
//int num=i;
while(1){
if(us.find(min) == us.end()){
cout << min <<endl;
break;
}
else{
++min;
}
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
// --------------------------------------------------------
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; }
#define FOR(i,l,r) for (ll i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define COUNT(c,v) count(ALL(c),(v))
#define SZ(c) ((ll)(c).size())
#define BIT(b,i) (((b)>>(i)) & 1)
#define PCNT(b) __builtin_popcountll(b)
#define CLZ(b) __builtin_clzll(b)
#define BITLEN(b) (64LL - CLZ(b))
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n'
ll llceil(ll a, ll b) { return (a + b - 1) / b; }
string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; }
string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; }
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VVS = vector<VS>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VVVLL = vector<VVLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VVVB = vector<VVB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VLD = vector<ld>;
using VVLD = vector<VLD>;
using VVVLD = vector<VVLD>;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
ll N, M; cin >> N >> M;
VLL X(M),Y(M),Z(M); REP(i,M) cin >> X[i] >> Y[i] >> Z[i];
VVLL E(N+1);
REP(i,M) {
E[X[i]].push_back(i);
}
VLL dp(1<<N, 0); dp[0] = 1;
REP(S,1<<N) {
ll p = PCNT(S);
bool ok = true;
for (ll id : E[p]) {
ll cnt = 0;
REP(i,N) if (BIT(S,i)) {
if (i+1 <= Y[id]) cnt++;
}
if (cnt > Z[id]) ok = false;
}
if (!ok) continue;
REP(u,N) if (BIT(S,u)) {
dp[S] += dp[S ^ (1<<u)];
}
}
ll ans = dp[(1<<N)-1];
COUT(ans);
return 0;
}
| #include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <climits>
constexpr int SINT_MAX = std::numeric_limits<int>::max();
constexpr int SINT_MIN = std::numeric_limits<int>::min();
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
template<class T>
T choose(bool b, T t, T f)
{
if (b) return t;
else return f;
}
const char *YesNo(bool b)
{
return choose(b, "Yes", "No");
}
const char *YESNO(bool b)
{
return choose(b, "YES", "NO");
}
template<class NumT>
NumT diffabs(NumT l, NumT r)
{
if (l < r) return r-l;
else return l-r;
}
struct myinout_t {} io;
template<class T>
myinout_t &operator >>(myinout_t &my, T &i)
{
cin >> i;
return my;
}
myinout_t &operator >>(myinout_t &my, int &i)
{
int r = scanf("%d", &i);
if (r != 1) exit(EXIT_FAILURE);
return my;
}
myinout_t &operator >>(myinout_t &my, ll &i)
{
int r = scanf("%lld", &i);
if (r != 1) exit(EXIT_FAILURE);
return my;
}
template<class T>
myinout_t &operator <<(myinout_t &my, const T &i)
{
cout << i;
return my;
}
myinout_t &operator <<(myinout_t &my, int i)
{
printf("%d", i);
return my;
}
myinout_t &operator <<(myinout_t &my, ll i)
{
printf("%lld", i);
return my;
}
myinout_t &operator <<(myinout_t &my, double i)
{
printf("%.20f", i);
return my;
}
constexpr char BR = '\n';
///////////////////////////////////////////////////
int mfind(const vi &a, int N, int i, int p)
{
for (int j = i; j < N; j++)
{
if (a[j] == p) return j;
}
return -1;
}
bool f(const vi &P, vi &A, vi &ans, int N)
{
int ians = 0;
for (int i = 0; i < N; i++)
{
int p = P[i];
int k = mfind(A, N, i, p);
if (i < ians && i < k) return false;
if (k == i) continue;
for (int j = k-1; j >= i; j--)
{
ans[ians++] = j;
swap(A[j], A[j+1]);
}
}
return ians == N - 1;
}
int main()
{
int N;
io >> N;
vi P(N);
for (int i = 0; i < N; i++)
io >> P[i];
vi A(N);
for (int i = 0; i < N; i++)
A[i] = i + 1;
vi ans(N-1);
bool r = f(P, A, ans, N);
if (r)
{
for (auto it = ans.rbegin(); it != ans.rend(); ++it)
{
io << (*it + 1) << BR;
}
}
else
{
io << "-1" << BR;
}
} |
#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int INF = 1001001001;
const int MOD = 1e9+7;
template<class T> inline bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
string solve(){
cin.tie(0), ios::sync_with_stdio(false);
int a,b,c; cin >> a >> b >> c;
if(1 <= abs(a-b)){
return (a<b?"Aoki":"Takahashi");
}else{
return (!c?"Aoki":"Takahashi");
}
}
int main(){
cout << solve();
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
//useful
#define rep(i,n) for(int i = 0; i < (n); i++)
int long long gcd(int long long a, int long long b){
return b?gcd(b,a%b):a;
} //最大公約数
int long long lcm(int long long a, int long long b){
return a/gcd(a,b)*b;
} //最小公倍数
int long long GetDigit(int long long num){
int long long digit=0;
while(num!=0){
num /= 10;
digit++;
}
return digit;
} //桁数
int long long GetDigitN(int long long num, int long long N){
int long long digit=0;
while(num!=0){
num /= N;
digit++;
}
return digit;
} //十進数でnumの際のN進数における桁数
int long long GetDigitSum(int long long n){
if(n < 10){
return n;
}
return GetDigitSum(n/10) + n%10;
} //十進数の桁和
unsigned int long long unsignedGetDigitSum(unsigned int long long n){
if(n < 10){
return n;
}
return unsignedGetDigitSum(n/10) + n%10;
} //十進数の桁和unsigned
int long long divisorcount(int long long n){
int long long ans = 1;
vector<int> x(n + 1);
int long long num = n; // 素因数分解する変数num
for (int long long i = 0; i <= n; i++) {
while (num%i == 0) { // 素数で割り切れなくなるまで割っていく
x.at(i)++; //割った個数を配列に足す
num /= i;
}
}
for (int i = 2; i <= n; i++){
ans *= x.at(i) + 1; //それぞれを+1して掛けていく
}
return ans;
} //約数カウント
int main(){
int A, B, C;
cin >> A >> B >> C;
if(A > B || (A == B && C == 1)){
cout << "Takahashi" << endl;
}
else{
cout << "Aoki" << endl;
}
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
typedef long double ld;
typedef long long ll;
#define sz(x) (int)(x).size()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define f first
#define s second
template<typename T, typename U> bool ckmin(T &a, const U &b){ return b < a ? a = b, true : false; }
template<typename T, typename U> bool ckmax(T &a, const U &b){ return b > a ? a = b, true : false; }
int n;
vector<ld> a;
ld f(ld x){
ld sum = 0;
for(int i = 0; i < n; ++i){
sum += x + a[i] - min(a[i], 2 * x);
}
return sum;
}
ld ternary_search(ld l, ld r) {
ld eps = 1e-9; //set the error limit here
while (r - l > eps) {
ld m1 = l + (r - l) / 3;
ld m2 = r - (r - l) / 3;
ld f1 = f(m1); //evaluates the function at m1
ld f2 = f(m2); //evaluates the function at m2
if (f1 > f2)
l = m1;
else
r = m2;
}
return f(l);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n; a.assign(n, 0);
for(int i = 0; i < n; ++i) cin >> a[i];
cout << setprecision(10) << fixed;
cout << ternary_search(0, 1e9 + 7)/n;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i < n;i++)
typedef long long ll;
int main(){
int n;
cin >> n;
vector<long long> a(n);
rep(i,n){
cin >> a[i];
}
sort(a.begin(),a.end());
vector<long long> stacksum(n);
for(int i=0;i < n;i++){
stacksum[i] = i == 0 ? a[0] : a[i] + stacksum[i-1];
}
double ave = 0;
rep(i,n){
ave += a[i];
}
ave = ave / n;
double lostmin = 1000000000000000.0;
for(int i=0; i<n; i++){
double x = (double)a[i] / 2;
long long al = i == 0 ? 0 : stacksum[i-1];
double lost = x + ave - ((2*x*(n-i) + al) / (double) n);
lostmin = min(lostmin,lost);
}
printf("%.12lf\n",lostmin);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int a,b,c;
int main( ) {
cin>>a>>b>>c;
a%=mod, b%=mod, c%=mod;
a=1ll*a*(a+1)/2%mod;
b=1ll*b*(b+1)/2%mod;
c=1ll*c*(c+1)/2%mod;
cout<<1ll*a*b%mod*c%mod;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
typedef long double ldd;
typedef double ld;
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define INF (int)1e9
#define INF64 (int64)1e18
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define F first
#define S second
const double pie = acos(-1.0);
template <typename T>
T Max(T x, T y)
{
return (x > y) ? x : y;
}
template <typename T>
T Min(T x, T y)
{
return (x > y) ? y : x;
}
int gcd(int n1, int n2)
{
if (n2 != 0) return gcd(n2, n1 % n2);
else return n1;
}
long long binpow(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;
}
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
clock_t time_p = clock();
void rtime() {time_p = clock() - time_p; cerr << "Time Taken : " << (float)1000 * (time_p) / CLOCKS_PER_SEC << "\n";}
int n;
string s, str;
int arr[10000001];
void solve()
{
long long int a, b, c;
cin >> a >> b >> c;
long long int ans1, ans2, ans3;
ans1 = a * (a + 1) / 2;
ans2 = b * (b + 1) / 2;
ans3 = c * (c + 1) / 2;
long long int m = 998244353;
cout << ((((ans1 % m) * (ans2 % m)) % m) * (ans3 % m)) % m;
}
int main()
{ fast;
int t;
t = 1;
while (t--)
{ solve();
}
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
//#pragma GCC target("fpmath=387")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef string str;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define watch(x) cout << (#x) << " is : " << (x) << newl
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vi dirx = {0,0,1,-1};
vi diry = {1,-1,0,0};
char to_upper (char x){
if( 97 <= int(x) && int(x) <= 122) return char(x-32);
if( 65 <= int(x) && int(x) <= 90) return x;
return -1;
}
char to_lower (char x){
if( 97 <= int(x) && int(x) <= 122) return x;
if( 65 <= int(x) && int(x) <= 90) return char(x+32);
return -1;
}
int numerize (char x){
if(48 <= int(x) && int(x) <= 57) return int(x-'0');
if(97 <= int(x) && int(x) <= 122) return int(x-96);
if(65 <= int(x) && int(x) <= 90) return int(x-64);
return -1;
}
bool isect (int l1, int r1, int l2, int r2){ return max(l1,l2) <= min(r1,r2); }
ll quickpow (ll num1, ll num2, ll MOD){
if(num2==0)return 1%MOD;
else if(num2==1)return num1%MOD;
else{
ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
}
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template
int a,b,w;
bool validate(){
int take = w / a;
int rem = w % a;
int p = (rem / take) + !!(rem % take);
if(a + p > b) return false;
return true;
}
int main(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> a >> b >> w; w *= 1000;
if(!validate()) return cout << "UNSATISFIABLE\n", 0;
int mx = w / a;
int mn = w / b + !!(w % b);
cout << mn << ' ' << mx << newl;
return 0;
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
#define readFast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fin cin
#define ll long long
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define output(x) ((int)x && cout << "YES\n") || cout << "NO\n";
#define ld long double
using namespace std;
#ifdef LOCAL
#define read() ifstream fin("date.in.txt")
#else
#define read() readFast
#endif // LOCAL
const int N = 55;
const int MOD = 998244353;
set<char> litere;
map<char, int> toCifra;
void puneLitere(string a) {
for (char x : a)
litere.insert(x);
}
ll createNumber(string a) {
ll nr = 0;
for (int i = 0; i < sz(a); ++i) {
nr = nr * 10 + toCifra[a[i]];
}
return nr;
}
string a, b, c;
int main() {
read();
fin >> a >> b >> c;
puneLitere(a);
puneLitere(b);
puneLitere(c);
if(sz(litere) > 10) {
cout << "UNSOLVABLE"; // to do
return 0;
}
vector<int> cifre(10);
iota(cifre.begin(), cifre.end(), 0);
do {
// primul caracater cu prima cifra ...
int p = 0;
for (char x : litere) {
toCifra[x] = cifre[p];
++p;
}
if(toCifra[a[0]] == 0 || toCifra[b[0]] == 0 || toCifra[c[0]] == 0)
continue;
ll n1 = createNumber(a);
ll n2 = createNumber(b);
ll n3 = createNumber(c);
if(n1 + n2 == n3) {
cout << n1 << '\n' << n2 << '\n' << n3;
return 0;
}
} while(next_permutation(cifre.begin(), cifre.end()));
cout << "UNSOLVABLE";
return 0;
} /*stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
~Benq~*/
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
void INX(){}
template<typename Head, typename... Tail>
void INX(Head&& head, Tail&&... tail)
{
cin >> head;
INX(forward<Tail>(tail)...);
}
void OUTX(){}
template<typename Head, typename... Tail>
void OUTX(Head&& head, Tail&&... tail)
{
cout << head << endl;
OUTX(forward<Tail>(tail)...);
}
//setprecision(10)
#define ADD emplace_back
#define MP make_pair
#define VVEC(type) vector<vector<type>>
struct mydistance
{
ll index1;
ll index2;
ld dist;
};
class UnionFind
{
private:
vll parent;
public:
UnionFind(ll N)
{
parent.resize(N);
for (ll i = 0; i < N; i++)
{
parent[i] = i;
}
}
ll GetParent(ll x)
{
if(parent[x] == x)
{
return x;
}
else
{
return (parent[x] = GetParent(parent[x]));
}
}
void Unite(ll x, ll y)
{
ll rx = GetParent(x);
ll ry = GetParent(y);
if(rx != ry)
{
parent[rx] = ry;
}
}
bool IsUnited(ll x, ll y)
{
return GetParent(x) == GetParent(y);
}
};
int main()
{
ll N;
INX(N);
vector<pair<ld, ld>> kugi(N);
for (ll i = 0; i < N; i++)
{
INX(kugi[i].first, kugi[i].second);
}
// calculate distances; i = N -> wall of y = -100, i = N + 1 -> wall of y = 100
vector<mydistance> vd;
for (ll i = 0; i < N + 1; i++)
{
for (ll j = i + 1; j <= N + 1; j++)
{
mydistance d = {0, 0, 0};
d.index1 = i;
d.index2 = j;
if(i == N)
{
//if(j == N + 1) <- always true in this case
d.dist = 200;
}
else if(j == N)
{
d.dist = kugi[i].second + 100;
}
else if(j == N + 1)
{
d.dist = 100 - kugi[i].second;
}
else
{
ld tmp1 = kugi[i].first - kugi[j].first;
ld tmp2 = kugi[i].second - kugi[j].second;
d.dist = sqrtl(tmp1 * tmp1 + tmp2 * tmp2);
}
vd.ADD(d);
}
}
sort(vd.begin(), vd.end(), [](mydistance x, mydistance y){return x.dist < y.dist;});
UnionFind uf(N + 2);
for (ll i = 0; i < (ll)vd.size(); i++)
{
uf.Unite(vd[i].index1, vd[i].index2);
if(uf.IsUnited(N, N + 1))
{
cout << setprecision(10) << (vd[i].dist / (ld)2.0) << endl;
break;
}
}
return 0;
}
| #include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define deb(x) cout << #x << " " << x << endl;
typedef long long int ll;
typedef pair<int, int> pi;
typedef pair<int, double> pid;
#define UP_Y 100
#define DOWN_Y -100
const double EPS = 0.00001;
double sq(int a) {
return (double) a*a;
}
double dist(pi a, pi b) {
return sqrt(sq(a.first-b.first) + sq(a.second-b.second));
}
double distUp(pi a) {
return dist(make_pair(a.first, UP_Y), a);
}
double distDown(pi a) {
return dist(make_pair(a.first, DOWN_Y), a);
}
bool can(double r, vector<vector<pid>> &g, int n) {
// deb(r);
double d = r+r;
vector<bool> vis(n+2, false);
queue<int> q;
q.push(n);
vis[n] = true;
while (!q.empty()) {
int popped = q.front();
q.pop();
for (auto e: g[popped]) {
if (vis[e.first])
continue;
if (e.second >= d)
continue;
if (e.first == n+1)
return false;
q.push(e.first);
vis[e.first] = true;
// cout << "push " << e.first << endl;
}
}
return true;
}
int main() {
IOS
int n;
cin >> n;
pi p[n];
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
vector<vector<pid>> g(n+2);
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
double dsq = dist(p[i], p[j]);
g[i].push_back({j, dsq});
g[j].push_back({i, dsq});
}
}
for (int i = 0; i < n; i++) {
double dist_up = distUp(p[i]);
g[n].push_back({i, dist_up});
g[i].push_back({n, dist_up});
}
for (int i = 0; i < n; i++) {
double dist_down = distDown(p[i]);
g[n+1].push_back({i, dist_down});
g[i].push_back({n+1, dist_down});
}
// for (int i = 0; i < n+2; i++) {
// cout << i << ": ";
// for (auto x: g[i]) {
// cout << "{" << x.first << "," << x.second << "} ";
// }
// cout << endl;
// }
double l = 0.5, r = 101, mid;
while (r-l > EPS) {
mid = (l+r) / 2;
if (can(mid, g, n))
l = mid;
else
r = mid;
}
cout << l;
return 0;
} |
#include<bits/stdc++.h>
#define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++)
#define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--)
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int, int>
#define mkp make_pair
using namespace std;
const int N = 1e5 + 7;
ll n, m;
int main() {
scanf("%lld%lld", &n, &m);
L(i, 1, sqrt(m + 1)) if(m % i == 0 && m / i + i == n) return puts("Yes"), 0;
puts("No");
return 0;
} | #include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <random>
#include <stack>
#include <set>
#include <unordered_set>
#define bug(x) cout<<"zdongdebug1: "<<x<<endl;
#define bug2(x, y) cout<<"zdongdebug2: "<<x<<" "<<y<<endl;
#define bug3(x, y, z) cout<<"zdongdebug3: "<<x<<" "<<y<<" "<<z<<endl;
using namespace std;
typedef long long ll;
const int maxn = 1005;
const int mod = 998244353;
int main() {
#ifdef suiyuan2009
freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
//freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
int x,y;
cin>>x>>y;
cout<<(x+y)/2<<" "<<(x-y)/2<<endl;
return 0;
} |
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef long int li;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test ll t; cin >> t; while(t--)
const long long int cons = 200005;
const long long int MOD = 1000000007;
// const long long int MOD = 998244353;
const long long int const_INT_MAX = 1000000000000000000;
const long long int const_INT_MIN = -1 * 1000000000000000000;
using namespace std;
// to sort pair vector in reverse order
bool sortinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){
return (a.first > b.first);
}
// to sort pair vector according to the second elements
bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b){
return (a.second < b.second);
}
// to sort pair vector according to the second elements in reverse order
bool sortbysecinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){
return (a.second > b.second);
}
ll lcm(ll x, ll y){
return (ll)((x * y) / __gcd(x, y));
}
ll mod_expo(ll x, ll y, ll p){
// reference from GFG
ll res = 1;
if((x + p) % p == 0){
res = 0;
}
else {
x = (x + p) % p;
while (y > 0){
if (y & 1){
res = (res*x + p) % p;
}
y = y>>1;
x = (x*x + p) % p;
}
}
return res;
}
void usaco(string str = ""){
fastio;
if(str.size()){
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
}
vector<ll> adj[cons];
ll dp[cons];
int main(){
usaco();
ll n, m; cin >> n >> m;
ll a[n];
for(ll i=0;i<n;i++){
cin >> a[i];
}
for(ll i=0;i<m;i++){
ll u, v; cin >> u >> v;
u--; v--;
adj[u].push_back(v);
}
for(ll i=0;i<n;i++){
dp[i] = const_INT_MIN;
}
for(ll i=n-1;i>=0;i--){
for(auto u : adj[i]){
dp[i] = max(dp[i], max(dp[u], a[u]));
}
}
ll fin_ans = const_INT_MIN;
for(ll i=0;i<n;i++){
fin_ans = max(fin_ans, dp[i] - a[i]);
}
cout << fin_ans << "\n";
} | #include<bits/stdc++.h>
#define int long long
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pi;
const int mod = 1e9 + 7;
const int nax = 1e6;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int a[nax], dp[nax], deg[nax], mn[nax];
vector<int> E[nax];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
mn[i] = 2e18;
dp[i] = -2e18;
}
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
E[x].pb(y);
deg[y]++;
}
queue<int> Q;
vector<int> ord;
for(int i = 1; i <= n; i++) {
if(!deg[i]) {
Q.push(i);
}
}
while(Q.size()) {
int node = Q.front();
Q.pop();
ord.pb(node);
for(auto it : E[node]) {
deg[it]--;
if(!deg[it]) {
Q.push(it);
}
}
}
int ans = -2e18;
for(auto x : ord) {
mn[x] = min(mn[x], a[x]);
for(auto it : E[x]) {
ans = max(ans, a[it] - mn[x]);
mn[it] = min(mn[it], mn[x]);
}
}
cout << ans << '\n';
return 0;
}
// 9 |
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
//indexed set is using "int" here--------- use ll ...
// typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef long long int ll;
typedef long double ld;
#define FAST \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ve vector
#define rem 1000000007
#define PI 3.141592653589793238462643383279502
ll power(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
ll modInverse(ll n, ll p)
{
return power(n, p - 2, p);
}
int phi(int n)
{
int result = n;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}
int main()
{
FAST;
// freopen("input1.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
/*ll tests;
cin>>tests;
rep (gg,0,tests)
{}*/
ll a, b, c;
cin >> a >> b >> c;
ll val2 = power(b, c, 4);
ve <ll> A(2),X(2),P(2);
P[0]=2;
P[1]=5;
ll R[2][2];
rep (i,0,2)
{
rep (j,0,2)
{
R[i][j]=modInverse(P[i],P[j]);
}
}
A[0]=(a%2==0?0:1);
A[1] = (a%5==0?0:power(a, val2, 5));
ll k=2;
for (int i = 0; i < k; ++i)
{
X[i] = A[i];
for (int j = 0; j < i; ++j)
{
X[i] = R[j][i] * (X[i] - X[j]);
X[i] = X[i] % P[i];
if (X[i] < 0)
X[i] += P[i];
}
}
ll ans=(X[0])%10+(X[1]*P[0])%10;
cout << (ans) % 10 << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef ARTHUR_LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
using ll = long long;
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define len(x) int((x).size())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define randint(n) uniform_int_distribution<int>(1, (n))(rng)
ll p;
ll mod_exp(ll a, ll b)
{
if(b==0LL) return 1LL;
ll ans = mod_exp(a,ll(b/2LL));
ans*=ans;
ans%=p;
if(b%2LL==1LL) ans*=a;
ans%=p;
return ans;
}
ll prop(ll a, ll b, ll c)
{
p=4;
ll expp = mod_exp(b, c);
p=5;
ll val = mod_exp(a, expp);
if(val % 2LL == a % 2LL) return val;
else return val+5;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef ARTHUR_LOCAL
ifstream cin("input.txt");
// ofstream cout("arcout.txt");
// ifstream cin1("arcout.txt");
// ifstream cin2("arcpyout.txt");
#endif
ll a,b,c;
cin>>a>>b>>c;
if(a%10 == 0){
cout<<0<<endl;
return 0;
}
if(a%5 == 0){
cout<<5<<endl;
return 0;
}
// EVEN CASE!!!
a%=10LL;
b%=4LL;
p=4;
ll fff=mod_exp(b,c);
p=5;
if(mod_exp(a,fff)%2LL == a%2LL) cout<<mod_exp(a,fff)<<endl;
else cout<<5+mod_exp(a,fff)<<endl;
return 0;
// for(int i=0; i<10; i++){
// for(int j=0; j<10; j++){
// p=4;
// cout<<i<<" "<<j<<" "<<mod_exp(i,j)<<endl;
// }
// }
// // cout<<(10)*(4/5)*(1/2)<<endl;
// // return 0;
// // ll a,b,c;
// // cin>>a>>b>>c;
// // a%=10LL;
// // b%=4LL;
// // cout<<prop(a,b,c)<<endl;
// for(int test=1; test<=10000; test++){
// ll a,b,c;
// // cin>>a>>b>>c;
// a=randint(1000000000);
// b=randint(1000000000);
// c=randint(1000000000);
// a%=10LL;
// b%=4LL;
// if(prop(a,b,c)<0LL || prop(a,b,c)>9) cout<<a<<" "<<b<<" "<<c<<endl;
// }
} |
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007; // 998244353;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const ld PI = acos((ld)-1);
const vector<ll> dx={-1,1,0,0},dy={0,0,-1,1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}
inline void yes() { cout << "Yes" << ENDL; }
inline void no() { cout << "No" << ENDL; }
template<typename T,typename Y> inline T mpow(T a, Y n) {
T res = 1;
for(;n;n>>=1) {
if (n & 1) res = res * a;
a = a * a;
}
return res;
}
template <typename T> V<T> prefix_sum(const V<T>& v) {
int n = v.size();
V<T> ret(n + 1);
rep(i, 0, n) ret[i + 1] = ret[i] + v[i];
return ret;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
for(auto&& x: vec) is >> x;
return is;
}
template<typename T,typename Y>
ostream& operator<<(ostream& os,const pair<T,Y>& p){
return os<<"{"<<p.fs<<","<<p.sc<<"}";
}
template<typename T> ostream& operator<<(ostream& os,const V<T>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
template<typename ...Args>
void debug(Args&... args){
for(auto const& x:{args...}){
cerr<<x<<' ';
}
cerr<<ENDL;
}
ll dp[1<<8][100010]={};
signed main(){
cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false);
cout<<fixed<<setprecision(20);
ll n,m;cin>>n>>m;
V<ll> w(n),l(m),v(m);cin>>w;
rep(i,0,m)cin>>l[i]>>v[i];
ll wlim=LINF;rep(i,0,m)chmin(wlim,v[i]);
rep(i,0,n)if(w[i]>wlim){
cout<<-1<<ENDL;
return 0;
}
rep(bit,0,1<<n){
ll wei=0;
rep(i,0,n)if(bit & (1<<i))wei+=w[i];
rep(i,0,m){
dp[bit][i+1]=dp[bit][i];
if(v[i]<wei)chmax(dp[bit][i+1],l[i]);
}
}
V<int> per(n);
iota(all(per),0);
ll ans=LINF;
do{
V<ll> di(n,0);
rep(i,0,n){
ll bit=(1<<per[i]);
rep(j,i+1,n){
bit |= (1<<per[j]);
chmax(di[j],di[i]+dp[bit][m]);
}
}
chmin(ans,di[n-1]);
}while(next_permutation(all(per)));
cout<<ans<<ENDL;
}
//! ( . _ . ) !
//CHECK overflow,vector_size,what to output? | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__);
#else
#define DEBUG(...) 6;
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {cerr << s.substr(0, s.find(',')) << " = " << x << " | "; debug(s.substr(s.find(',') + 2), args...);}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
long long C;
cin >> n >> C;
map<int, long long> mp;
for (int i=0; i<n; i++) {
int a, b, c;
cin >> a >> b >> c;
mp[a] += c;
mp[b+1] -= c;
}
long long ret = 0, pref = 0;
int ti = 0;
for (auto &p : mp) {
ret += (p.first - ti) * min(pref, C);
ti = p.first;
pref += p.second;
}
cout << ret << "\n";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxn 200005
int n;
ll a[maxn],sum[maxn];
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
ll summ=0,maxx=a[1];
for(int i=1;i<=n;i++){
summ+=sum[i-1];maxx=max(maxx,a[i]);
ll ans=summ+i*maxx+sum[i];
printf("%lld\n",ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define p(x,y) pair<x,y>
#define oset(x) tree<x, null_type, less<x>, rb_tree_tag, tree_order_statistics_node_update>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define scan(a) for(auto &it:a)cin>>it;
#define print(a) {for(auto it:a)cout<<it<<" ";cout<<endl;}
#define out(x) cout<<((x)?"YES":"NO")<<endl;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define int ll
void solve()
{
int n;
cin>>n;
vector<pair<double,double>> a;
rep(i,0,n)
{
double t,l,r;
int t1,l1,r1;
cin>>t1>>l1>>r1;
t=t1;r=r1;l=l1;
if(t==2)
r-=0.4;
else if(t==3)
l+=0.4;
else if(t==4)
l+=0.4,r-=0.4;
a.push_back(make_pair(l,r));
// cout<<a[i].first<<" ";
}
int ans=0;
rep(i,0,n)
{
if(a[i].first==-1)
continue;
double l1=a[i].first,r1=a[i].second,l2,r2;
rep(j,i+1,n)
{
l2=a[j].first;r2=a[j].second;
if(r1<l2||r2<l1)
continue;
ans++;
}
}
cout<<ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t=1;
//init();
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define all(a) (a).begin(), (a).end()
using pint = pair<ll, ll>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll seen[200200];
int n;
vector<vector<pint>> g(200200);
int main() {
cin >> n;
rep(i, 0, n - 1) {
ll u, v, w;
cin >> u >> v >> w;
--u, --v;
g[u].push_back({w, v});
g[v].push_back({w, u});
}
rep(i, 0, n) seen[i] = -1;
seen[0] = 0;
queue<ll> q;
q.push(0);
while (!q.empty()) {
int now = q.front();
q.pop();
for (auto p : g[now]) {
ll next = p.second, w = p.first;
if (seen[next] == -1) {
seen[next] = seen[now] ^ w;
q.push(next);
}
}
}
ll ans = 0;
const ll mod = 1e9 + 7;
rep(i, 0, 60) {
ll cnt0 = 0, cnt1 = 0;
rep(j, 0, n) {
if (seen[j] & 1LL << i)
cnt1++;
else
cnt0++;
}
// cout << i << " : " << cnt0 << cnt1 << "\n";
ans = (ans + (1LL << i) % mod * cnt0 * cnt1 % mod) % mod;
}
cout << ans << "\n";
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 400010, mod = 1e9 + 7;
int e[N], ne[N], h[N], idx, w[N][61], n;
ll tar, d[N][2], res;
void add(int a, int b, long long c) {
e[idx] = b, ne[idx] = h[a];
for (int i = 0; i < 60; ++i) {
if (c & 1) w[idx][i] = 1;
c /= 2;
if (!c) break;
}
h[a] = idx++;
}
void go(int x, int fa) {
d[x][0] = d[x][1] = 0;
for (int i = h[x]; i != -1; i = ne[i]) {
if (fa == e[i]) continue;
go(e[i], x);
if (w[i][tar]) {
d[x][0] += d[e[i]][1];
d[x][1] += d[e[i]][0] + 1;
} else {
d[x][0] += d[e[i]][0] + 1;
d[x][1] += d[e[i]][1];
}
}
res += d[x][1];
ll t = 0;
for (int i = h[x]; i != -1; i = ne[i]) {
if (fa == e[i]) continue;
if (w[i][tar]) {
res += (d[e[i]][0] + 1) * (d[x][0] - d[e[i]][1]);
} else {
res += d[e[i]][1] * (d[x][0] - d[e[i]][0] - 1);
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
memset(h, -1, sizeof h);
for (int i = 1; i < n; ++i) {
int a, b;
ll w;
cin >> a >> b >> w;
add(a, b, w);
add(b, a, w);
}
ll ans = 0;
for (int i = 0; i < 60; ++i) {
tar = i;
res = 0;
go(1, -1);
ans = (ans + res % mod * ((1ll << i) % mod) % mod) % mod;
}
cout << ans << '\n';
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
#include <bitset>
#include <chrono>
using namespace std;
typedef long long ll;
const int Inf = 1e9;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int t;
cin >> t;
string go = "atcoder";
while (t--) {
string s;
cin >> s;
int need = 0;
int ans = Inf;
for (int i = 0; i < (int)go.size(); ++i) {
for (int j = i; j < (int)s.size(); ++j) {
if (s[j] > go[i]) {
ans = min(ans, need + j - i);
}
}
int id = -1;
for (int j = i; j < (int)s.size(); ++j) {
if (s[j] == go[i]) {
id = j;
break;
}
}
if (id == -1) break;
s.erase(s.begin() + id);
s.insert(s.begin() + i, go[i]);
need += id - i;
if (i == (int)go.size() - 1) {
if (s.size() > go.size()) {
ans = min(ans, need);
}
}
}
if (ans == Inf) {
cout << "-1\n";
} else {
cout << ans << '\n';
}
}
}
| #include <bits/stdc++.h>
using namespace std;
void solve()
{
string s;
cin>>s;
int n=s.length(),x=0;
while(x<n && s[x]=='a')
x++;
cout<<(x==n?-1:s>"atcoder"?0:x-(s[x]>'t'))<<endl;
}
int main() {
//freopen("t","r",stdin);
int test;
cin>>test;
while(test--)
solve();
return 0;
} |
#include <bits/stdc++.h>
#define endl '\n'
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define LL long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
int readint(){
int x=0,y=1;char c=getchar();
while((c<'0'||c>'9')&&c!='-')c=getchar();
if(c=='-')y=-1,c=getchar();
while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
return x*y;
}
int n;
int a[2510];
int main(){
cin>>n;
if(n==3){
puts("6 10 15");
// 2*2*3
// 2*2
// 3*2
}else if(n==4){
puts("84 60 105 70");
}else{
int a[3]={6,10,15};
cout<<"6 10 15 ";
// for(int i=4;i<=n;i++)cout<<4*i<<' ';
int cnt=4;
for(int i=16;i<=10000&&cnt<=n;i++){
int cur=0;
if(i%2==0)cur++;
if(i%3==0)cur++;
if(i%5==0)cur++;
if(cur>=2)cnt++,cout<<i<<' ';
}
puts("");
}
return 0;
}
| #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 1;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int n; cin >> n;
vector<int> ans;
ans.push_back(6);
ans.push_back(10);
ans.push_back(15);
for (int i = 1; i <= 10000; i++) {
if (i == 6 || i == 10 || i == 15)continue;
if (i % 6 == 0 || i % 10 == 0 || i % 15 == 0)ans.push_back(i);
}
rep(i, n) {
if (i > 0)cout << " ";
cout << ans[i];
}
cout << "\n";
//cout << ans.size() << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//int t; cin >> t; rep(i, t)
solve();
return 0;
} |
#include <bits/stdc++.h>
#define rep(begin, end) for (i=begin;i<end;i++)
#define repj(begin, end) for (j=begin;j<end;j++)
#define init(arr, end, val) for (i=0;i<end;i++) arr[i] = val;
#define printint(i0, i1) printf("%d %d\n", i0, i1)
using namespace std;
typedef long long int ll;
const int inf = 1000000000;
const int mod = 1000000007;
const int nil = -1;
int i, j, n, x, k, ans;
char c;
int main() {
scanf(" %d %d", &n, &x);
rep(0,n+1) {
scanf("%c", &c);
if (c == 'x') x = max(x-1, 0);
else if (c == 'o') x++;
}
printf("%d\n", x);
} | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll MOD = 1000000007;
#ifndef ONLINE_JUDGE
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " (" << x.first << " : " << x.second << ")" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "["; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "]"; return o;}
ostream &operator<<(ostream &o, const string &obj) {o << "\""; o << obj.c_str(); o << "\""; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "(" << obj.first << ", " << obj.second << ")"; return o;}
template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print_sim_py(void) {cout << endl;}
template <class Head> void print_sim_py(Head&& head) {cout << head;print_sim_py();}
template <class Head, class... Tail> void print_sim_py(Head&& head, Tail&&... tail) {cout << head << " ";print_sim_py(forward<Tail>(tail)...);}
#define print(...) print_sim_py(__VA_ARGS__);
#else
#define print(...);
#endif
template <typename... Ts>
std::istream& IN(Ts&... xs){ return (std::cin >> ... >> xs); }
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
int main(void){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n,x;
IN(n,x);
string s;
IN(s);
rep(i,n){
switch(s[i]){
case 'o': x++; break;
case 'x': x=max(0ll,x-1); break;
}
}
cout << x << endl;
return 0;
}
|
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
namespace defines{
typedef long long ll;
typedef pair<ll,ll> P;
#define FOR(i,a,b) for(ll i = a ; i < b ; i++) // for i in range(a,b)
#define REP(i,n) for(ll i = 0 ; i < n ; i++) // for i in range(b)
#define FORD(i,a,b) for(ll i = a ; i > b ; i--) // for i in range(a,b,-1)
#define ALL(x) x.begin(),x.end()
template<class T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<class T, class... Ts> auto make_vec(size_t a, Ts... ts){ return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));}
template<typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val){std::fill( (T*)array, (T*)(array+N), val );}
/* for debug */
#define DEBUG(x) dbg(#x,x)
template<class T> void dbg(string name, T x){cerr<<name<<": "<<x<<"\n";}
template<> void dbg<P>(string name, P x){cerr<<name<<": ("<<x.first<<","<<x.second<<")\n";}
template<class T> void dbg(string name, vector<T> xl){cerr<<name<<": "; for(T x: xl) cerr<<x<<" "; cerr<<'\n';}
template<> void dbg<P>(string name, vector<P> xl){cerr<<name<<": "; for(P x:xl){cerr<<"("<<x.first<<","<<x.second<<"), ";}cerr<<"\n";}
template<class T> void dbg(string name, vector<vector<T>> xl){ cerr<<name<<": \n"; int ml=1;for(vector<T> row: xl){for(T x:row){stringstream sstm; sstm<<x; ml=max(ml,(int)sstm.str().size());}}; for(vector<T> row: xl){{for(T x:row) cerr<<std::right<<std::setw(ml+1)<<x;} cerr << '\n';}}
template<class T> void dbg(string name, set<T> xl){cerr<<name<<": "; for(T x:xl)cerr<<x<<" ";cerr<<'\n';}
template<class T> void dbg(string name, multiset<T> xl){cerr<<name<<": "; for(T x:xl)cerr<<x;cerr<<'\n';}
template<class T> void dbg(string name, unordered_set<T> xl){cerr<<name<<": "; for(T x:xl)cerr<<x<<" ";cerr<<'\n';}
template<class T> void dbg(string name, unordered_multiset<T> xl){cerr<<name<<": "; for(T x:xl)cerr<<x;cerr<<'\n';}
template<class T, class U> void dbg(string name, map<T,U> xl){cerr<<name<<": \n"; for(auto &[k,v]:xl)cerr<<" "<<k<<": "<<v<<'\n';}
template<class T, class U> void dbg(string name, unordered_map<T,U> xl){cerr<<name<<": \n"; for(auto &[k,v]:xl)cerr<<" "<<k<<": "<<v<<'\n';}
}
using namespace defines;
const int IINF = 1'001'001'001;
const ll INF = 1'001'001'001'001'001'001ll;
const int MOD = 1'000'000'007;
// const int MOD = 998244353;
// using mint = modint1000000007;
// using mint = modint998244353;
const ll max2=262145;
// const ll max2=1025;
bool comp[max2];
ll dp[max2];
ll n,m;
vector<unordered_set<ll>> gl(20);
const ll MAX=10000;
ll rec(ll state){
if(dp[state]!=MAX) return dp[state];
if(comp[state]) return 1;
for(ll t=state ; t>0 ; t=(t-1)&state){
if(t==state)continue;
dp[state] = min(rec(t)+rec(t^state), dp[state]);
}
return dp[state];
}
void solve(){
Fill(dp,MAX);
cin>>n>>m;
REP(i,m){
ll a,b; cin >> a >> b;
a-=1;b-=1;
gl[a].insert(b);
gl[b].insert(a);
}
ll p2=pow(2,n);
REP(i,p2){
vector<ll> vl;
REP(j,n){
if((i>>j)%2==1)vl.push_back(j);
}
bool ok=true;
REP(j1, vl.size()){
REP(j2, vl.size()){
if(j1==j2)continue;
if( gl[vl[j1]].find(vl[j2])==gl[vl[j1]].end() ) {
ok=false;
break;
}
}
}
comp[i]=ok;
}
cout<<rec(p2-1)<<endl;
// vector<int> aaa(begin(dp),end(dp));
// DEBUG(aaa);
// vector<int> aa(begin(comp),end(comp));
// DEBUG(aa);
// DEBUG(aa[65]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define y1 bfrsoihgdfnm
#define INF (ll)1e18
#define mod 998244353
#define maxn 210
ll i, i1, j, k, k1, t, n, m, res, flag[10], a[maxn], b;
ll x1, y1, x, y, kn[maxn][maxn];
bool vis[maxn];
vector<ll> parent[maxn][maxn], rs[2];
ll bk(ll x, ll p) {
if (p == 1) return (((x - a[i]) % 200) + 200) % 200;
else return x;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
}
kn[0][0] = 1;
for (i = 1; i <= n; i++) {
for (j = 0; j <= 199; j++) {
k = (j + a[i]) % 200;
kn[i][k] += kn[i - 1][j];
kn[i][k] = min((ll)2, kn[i][k]);
if (kn[i - 1][j] > 0) parent[i][k].pb(1);
k = j;
kn[i][k] += kn[i - 1][j];
kn[i][k] = min((ll)2, kn[i][k]);
if (kn[i - 1][j] > 0) parent[i][k].pb(0);
}
}
m = -1;
for (j = 0; j <= 199; j++) {
if (kn[n][j] == 2) {
m = j; break;
}
}
if (m == -1) {
cout << "NO" << nl; return 0;
}
k = m; i = n;
while (parent[i][k].size() == 1) {
k = bk(k, parent[i][k][0]); i--;
}
x = bk(k, parent[i][k][0]); y = bk(k, parent[i][k][1]);
if (parent[i][k][0] == 1) rs[0].pb(i);
else rs[1].pb(i);
i--;
while (i >= 1) {
x1 = parent[i][x][0]; y1 = parent[i][y][0];
x = bk(x, x1); y = bk(y, y1);
if (x1 == 1) rs[0].pb(i);
if (y1 == 1) rs[1].pb(i);
i--;
}
sort(rs[0].begin(), rs[0].end());
sort(rs[1].begin(), rs[1].end());
if ((ll)rs[0].size() + (ll)rs[1].size() == n) {
if (rs[0].empty()) {
rs[0].pb(rs[1].back()); rs[1].pop_back();
} else if (rs[1].empty()) {
rs[1].pb(rs[0].back()); rs[0].pop_back();
}
} else {
for (auto u : rs[0]) vis[u] = true;
for (auto u : rs[1]) vis[u] = true;
for (i = 1; i <= n; i++) {
if (!vis[i]) {
k = i; break;
}
}
if (rs[0].empty() || rs[1].empty()) {
rs[0].pb(k); rs[1].pb(k);
}
}
sort(rs[0].begin(), rs[0].end());
sort(rs[1].begin(), rs[1].end());
cout << "YES" << nl;
cout << rs[0].size() << ' ';
for (auto u : rs[0]) cout << u << ' ';
cout << nl;
cout << rs[1].size() << ' ';
for (auto u : rs[1]) cout << u << ' ';
return 0;
}
|
/********************************************************************
* @brief https://atcoder.jp/contests/abc145/tasks/abc145_c
* @date 2020/10/16
*********************************************************************/
#include <bits/stdc++.h> //全てのヘッダファイルをインクルード
using namespace std;
//ループ
#define rep(i, n) for(int i = 0; i < (n); i++) //普通のループ
#define repr(i, n) for(int i = n; i >= 0; i--) //逆ループ
//型名省略
typedef long long ll;
//値
static const ll MX = 100005;
static const ll MX_ll = 1e18;
const double PI = acos(-1); //π
using namespace std;
//ソート
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
int main(){
int n;
cin >> n;
vector<int> b(n);
rep(i,n) b[i] = 0;
rep(i,n){
int tmp;
cin >> tmp;
tmp--;
b[tmp]++;
}
int chk = 0;
rep(i,n){
if(b[i]!=1){
chk=1;
break;
}
}
if(chk==1) cout << "No" << endl;
else cout << "Yes" << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ld eps = 1e-8;
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define be(a) (a).begin(), (a).end()
// #define constexpr(...) (__VA_ARGS__)
// DEBUGING TEMPLETE ////////////////////////////////////////////////////////////////////////{{{
#define db(val) "["#val" = "<<(val)<<"] "
#define CONCAT_(x, y) x##y
#define CONCAT(x, y) CONCAT_(x, y)
#ifdef LOCAL_DEBUG
# define clog cerr << flush << string(__db_level * 2, ' ')
# define DB() debug_block CONCAT(dbbl, __LINE__)
int __db_level = 0;
struct debug_block {
debug_block() { clog << "{" << endl; ++__db_level; }
~debug_block() { --__db_level; clog << "}" << endl; }
};
#else
# define clog if (0) cerr
# define DB(...)
#endif
template<class U, class V> ostream& operator<<(ostream& out, const pair<U, V>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
if constexpr(i == tuple_size<T>::value) return out << ")";
else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U> ostream& operator<<(ostream& out, const tuple<U...>& tup) {
return print_tuple_utils<0, tuple<U...>>(out, tup);
}
template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator<<(ostream& out, const Con& container) {
out << "{";
for (auto it = container.begin(); it != container.end(); ++it)
out << (it == container.begin() ? "" : ", ") << *it;
return out << "}";
}
// ACTUAL SOLUTION START HERE ////////////////////////////////////////////////////////////////}}}
int main(void) {
#ifdef LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
freopen(".log", "w", stderr);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
vector<int> a(n);
rep(i, n) cin >> a[i];
vector<vector<int>> pos(n);
rep(i, n) pos[a[i]].push_back(i);
rep(ans, n) {
int prev = -1;
for (auto i: pos[ans]) {
if (i - prev - 1 >= m) {
cout << ans << '\n';
return 0;
}
prev = i;
}
if (n - prev - 1 >= m) {
cout << ans << '\n';
return 0;
}
}
cout << n;
return 0;
}
// vim: foldmethod=marker
|
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int ri(){
int n;
scanf("%d", &n);
return n;
}
int main(){
int h = ri();
int w = ri();
int x = ri() - 1;
int y = ri() - 1;
string s[h];
for(auto &i : s) cin >> i;
int cnt = -3;
for(int i = x; i < h && s[i][y] != '#'; i++) cnt++;
for(int i = x; i >= 0 && s[i][y] != '#'; i--) cnt++;
for(int j = y; j < w && s[x][j] != '#'; j++) cnt++;
for(int j = y; j >= 0 && s[x][j] != '#'; j--) cnt++;
printf("%d\n", cnt);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int h,w,x,y;cin>>h>>w>>x>>y;
--x,--y;
vector<string> s(h);
int ans=1;
for(int i=0;i<h;i++)cin>>s[i];
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
for(int i=0;i<4;i++){
for(int j=1;j<200;j++){
int nx=x+dx[i]*j,ny=y+dy[i]*j;
if(nx<0 or nx>h-1 or ny<0 or ny>w-1)break;
if(s[nx][ny]=='#')break;
ans++;
}
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005
const ll MOD = 998244353;
int main() {
ll n; cin >> n;
ll a[n] = {};
rep(i,n) cin >> a[i];
sort(a,a+n);
ll ans = 0;
ll now = 0;
rep(i,n){
ans += now * a[i] % MOD;
ans += a[i]*a[i];
ans %= MOD;
now = (now*2 + a[i]) % MOD;
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef pair<int,int> II;
typedef vector< II > VII;
typedef vector<int> VI;
typedef vector< VI > VVI;
typedef long long int LL;
typedef vector<LL> VL;
#define si(n) cin>>n
#define dout(n) cout<<n<<"\n"
#define DRT() int t; si(t); while(t--)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//# define M_PI 3.14159265358979323846264338327950288
// DSU
void IO(){
#ifndef ONLINE_JUDGE
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
off;
}
LL n,m;
VL arr;
VL brr;
map<int, VI> occ;
LL dp[1005][1005];
LL rec(LL ind, LL bind){
if(ind == n || bind == m){
return max(n - ind, m - bind);
}
if(dp[ind][bind] != -1)
return dp[ind][bind];
LL ans = 0;
ans = rec(ind+1, bind+1) + (arr[ind] != brr[bind]);
LL tt = 1 + min(rec(ind, bind+1), rec(ind+1, bind));
return dp[ind][bind] = min(ans,tt);
}
int main(){
IO();
cin>>n>>m;
arr.resize(n);brr.resize(m);
rep(i,0,n){
cin>>arr[i];
}
rep(i,0,m){
cin>>brr[i];
// occ[brr[i]].PB(i);
}
rep(i,0,1005){
rep(j,0,1005){
dp[i][j] = -1;
}
}
cout<<rec(0, 0);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
unsigned long long cyanmond;
cin >> cyanmond;
vector<unsigned long long> cyankawaii(cyanmond);
for (unsigned long long mohumohu = 0; mohumohu < cyanmond; mohumohu++) {
cin >> cyankawaii.at(mohumohu);
}
vector<unsigned long long> cyan(cyanmond+1, 0);
for (unsigned long long i = 0; i < cyanmond; ++i){cyan[i+1] = cyan[i] + cyankawaii[i];}
unsigned long long kawaiicyan = 0;
unsigned long long cyankawaisugi = 0;
for (unsigned long long i = 1; i <= cyanmond; i++) {
kawaiicyan=max(kawaiicyan, cyankawaii.at(i - 1));
cyankawaisugi = cyankawaisugi + cyan.at(i - 1);
cout << kawaiicyan * i + cyankawaisugi + cyan.at(i) << endl;
}
} | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define forn(a, e) for (i64 a = 0; a < (i64)(e); a++)
#define forr(a, s, e) for (i64 a = s; a < (i64)(e); a++)
#define fore(e, a) for (auto& e : a)
#ifdef LOCAL
#define logv(a) {cerr << #a << " = "; fore(e, a) {cerr << e << " ";} cerr << "\n";}
#define logvp(a) {cerr << #a << " = "; fore(e, a) {cerr << "(" << e.first << ", " << e.second << ") ";} cerr << "\n";}
#define logvv(a) {cerr << #a << " = \n"; fore(r, a) { fore(e, r) {cerr << e << " ";} cerr << "\n";} }
#define logvf(a, field) {cerr << #a"."#field << " = \n"; fore(e, a) { cerr << e.field << " ";} cerr << "\n"; }
#define logvff(a, f1, f2) {cerr << #a".{"#f1 << ", "#f2 << "} = \n"; fore(e, a) { cerr << "(" << e.f1 <<", " << e.f2 << ") ";} cerr << "\n"; }
#define logs(a) cerr << #a << " = " << (a) << "\n";
#define logss(a, b) cerr << #a << " = " << (a) << ", " << #b << " = " << (b) << "\n";
#define logp(a) cerr << #a << " = " << "(" << a.first << ", " << a.second << ")" << "\n";
#define cond(pred, stmt) if (pred) { stmt }
#else
#define logv(a)
#define logvp(a)
#define logvv(a)
#define logvf(a, field)
#define logvff(a, f1, f2)
#define logs(a)
#define logss(a, b)
#define logp(a)
#define cond(pred, stmt)
#endif
using iip = pair<int, int>;
using llp = pair<i64, i64>;
using ivec = vector<int>;
using llvec = vector<i64>;
using svec = vector<string>;
template<typename T> using vec = vector<T>;
template<typename T, typename Dim>
auto make_vec(T value, Dim dim) { return vector<T>(dim, value); }
template<typename T, typename Dim1, typename... Dim>
auto make_vec(T value, Dim1 dim1, Dim... dims) { return make_vec(make_vec(value, dims...), dim1); }
template<typename T>
bool uax(T& v, const T& newv) { if (v < newv) { v = newv; return true; } else return false; }
template<typename T>
bool uin(T& v, const T& newv) { if (v > newv) { v = newv; return true; } else return false; }
template<typename T>
istream& operator>>(istream& is, vector<T>& c) { for (auto& e : c) is >> e; return is; }
template<typename T, size_t N>
istream& operator>>(istream& is, array<T, N>& c) { for (auto& e : c) is >> e; return is; }
template<typename ...T>
istream& read(T&... args) { return (cin >> ... >> args); }
static mt19937 rande(123123);
template<typename T>
T rand_int(T from, T to) { uniform_int_distribution<T> distr(from, to); return distr(rande); }
// const i64 INF = 2e18;
const int INF = 2e9;
const __int128_t M = 998244353;
const int MXB = 20;
using bin = bitset<MXB + 1>;
const double EPS = 1e-8;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
rande.seed(chrono::steady_clock::now().time_since_epoch().count());
int n;
while (read(n)) {
llvec a(n);
read(a);
auto sa = a;
partial_sum(sa.begin(), sa.end(), sa.begin());
auto ma = a;
partial_sum(ma.begin(), ma.end(), ma.begin(), [](auto a, auto b) {
return std::max(a, b);
});
i64 lv = 0;
forn(i, n) {
auto res = lv + sa[i] + (i + 1) * ma[i];
std::cout << res << "\n";
lv += sa[i];
}
}
} |
#include <bits/stdc++.h>
#include <sys/time.h>
#pragma GCC optimize "O3,omit-frame-pointer,inline"
#define REP(i, n) for(int i = 0;i < n;i++)
#define ll long long
using namespace std;
//typedef vector<unsigned int>vec;
//typedef vector<ll>vec;
//typedef vector<vec> mat;
typedef pair<int, int> P;
typedef pair<ll,ll> LP;
//const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
//const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000;//1e18
const ll MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-10;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
//template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};
constexpr ll CYCLES_PER_SEC = 2800000000;
constexpr double TL = 1990;
double get_ms() { struct timeval t; gettimeofday(&t, NULL); return (double)t.tv_sec * 1000 + (double)t.tv_usec / 1000; }
constexpr int dh[4] = {-1, 0, 1, 0};
constexpr int dw[4] = {0, 1, 0, -1};
constexpr char MOVE[4] = {'U', 'R', 'D', 'L'};
uint32_t XorShift(void) {
static uint32_t x = 123456789;
static uint32_t y = 362436069;
static uint32_t z = 521288629;
static uint32_t w = 88675123;
uint32_t t;
t = x ^ (x << 11);
x = y; y = z; z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
double Prob(void){
double ret = (double)XorShift() / UINT_MAX;
return ret;
}
namespace AHC004{
int N, M;
vector<string> S;
void input(){
cin >> N >> M;
S.resize(M);
REP(i,M) cin >> S[i];
}
void output(){
REP(i,N) cout << string(N, 'A') << endl;
}
void main(){
input();
output();
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
AHC004::main();
} | #include<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
#include<chrono>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define vint(a,n) vint a(n); rep(i, n) cin >> a[i];
#define vll(a,n) vll a(n); rep(i, n) cin >> a[i];
#define ALL(n) begin(n),end(n)
#define RALL(n) rbegin(n),rend(n)
#define MOD (1000000007)
// #define MOD (998244353)
#define INF (2e9)
#define INFL (2e18)
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
using vint=vector<int>;
using vll=vector<ll>;
using vbool=vector<bool>;
template<class T>using arr=vector<vector<T>>;
template<class T>int popcount(T &a){int c=0; rep(i, 8*(int)sizeof(a)){if((a>>i)&1) c++;} return c;}
template<class T>void pl(T x){cout << x << " ";}
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){pl(a[i]);} pr(a.back());}
template<class T>void prarr(arr<T>& a){rep(i, a.size()) if(a[i].empty()) pr(""); else prvec(a[i]);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int main()
{
double a, b, x, y; cin >> a >> b >> x >> y;
double ans = a + (x-a)*b/(y+b);
printf("%.20lf\n", ans);
return 0;} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(v) v.begin( ), v.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
#define rrep(i, n) for(int i=(n); i>=0; i--)
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; }
/*
辞書順でK番目のものを答えよ、という問題と同様の解き方で解く(あってた)
どうやってsumごとの組み合わせ数を数えるか⇒DP+いもす法(わからなかった)
Nが変動して貪欲できないのでDPという発想が自然かもしれない
*/
ll dp[4][3010101];
int main(){
ll N, K; cin >> N >> K;
dp[0][0] = 1;
rep(cnt, 3){
rep(sum, 2010101){ //何故2*1e6なのか? -> dp[2][sum]の最大値が200000だから
//imos法
dp[cnt+1][sum+1] += dp[cnt][sum];
dp[cnt+1][sum+N+1] -= dp[cnt][sum];
}
rept(sum, 1, 3010101){
dp[cnt+1][sum] += dp[cnt+1][sum-1];
}
}
ll cu = 0;
//総和を確定させる
int sum = -1; //3つの数の総和。最大1e6*3
for(int i=3; i<3010101; i++){
if(K <= cu + dp[3][i]){//グループの範囲内かどうか
sum = i;
break;
}
cu += dp[3][i];
}
rep(i, sum) K -= dp[3][i]; //総和未満のグループ合計値を引く
cu = 1;
int ans_a = -1;
//aを全探索
rept(a, 1, N+1){
if(K < cu + dp[2][sum - a]){ //dp[2][sum-a]:aを固定してb,cを選んだときの総和の場合の数
ans_a = a;
break;
}
cu += dp[2][sum-a];
}
rept(i, 1, ans_a) K -= dp[2][sum - i];
cu = 1;
int ans_b = -1;
//bを全探索
rept(b, 1, N+1){
if(K < cu + dp[1][sum - ans_a - b]){
ans_b = b;
break;
}
cu += dp[1][sum - ans_a - b];
}
int ans_c = sum - ans_a - ans_b;
cout << ans_a << " " << ans_b << " " << ans_c << ln;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pb push_back
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define SF S.F
#define SS S.S
const int N=3000006, inf=1e6, P=1e9+7;
int n,m,w,d,k,x,y,q,fac[N],inv[N],ans;
vector<int> v;
inline ll C(int n, int m) {
if (n < 0 || m > n) return 0;
return fac[n] * inv[m] % P * inv[n-m] % P;
}
main(){ios_base::sync_with_stdio(false), cin.tie(0);
inv[0] = fac[0] = inv[1] = fac[1] = 1;
for (int i = 2;i <= N-6; i++)
inv[i] = (P - P / i) * inv[P % i] % P;
for (int i = 2;i <= N-6; i++)
fac[i] = fac[i-1] * i % P, inv[i] = inv[i-1] * inv[i] % P;
cin>>n>>m>>k;
if(n>m+k)return !(cout<<0);
cout<<(C(n+m, n)-C(n+m, n-(k+1))+P)%P;
}
|
/*
ALLAH IS ALMIGHTY ******BISMILLAHIR RAHMANIR RAHIM.****** ALLAH IS ALMIGHTY
```````````````````````````````
AUTHOR: ANISUR RAHAMAN
BSMRSTU(SHIICT)
ID:017
............LET's START OUR JOURNEY.............
*/
#include <bits/stdc++.h>
#define I_O \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
//~~~~~~~~~~~~ Sort Form Of Long~~~~~~~~~~~//
#define ll long long int
#define lls int64_t
#define ld long double
#define db double
#define ull unsigned long long int
//~~~~~~~~~~~~~~Pair~~~~~~~~~~~~~~~~~~//
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<db, db>
#define psi pair<string, int>
#define vi vector<int>
#define vl vector<ll>
#define vd vector<db>
#define vs vector<string>
#define vb vector<bool>
#define vpi vector<pii>
#define vpl vector<pll>
#define vpd vector<pdd>
#define vpsi vector<psi>
//~~~~~~~~~~~~~~Vector~~~~~~~~~~~~~~~~~//
#define pb push_back
#define pf push_front
#define MP make_pair
#define in insert
#define ff first
#define ss second
#define al(v) v.begin(), v.end()
#define alr(v) v.rbegin(), v.rend()
#define srt(v) sort(al(v))
#define srtr(v) sort(al(v), greater<int>());
#define len(x) ((ll)(x).length())
#define sz(x) ((ll)(x).size())
#define rev(v) reverse(al(v))
#define btcnt(n) __builtin_popcount(n)
#define acl(v, n) accumulate(al(v), n)
#define eb emplace_back
#define lb(v, kk) lower_bound(al(v), kk)
#define ub(v, kk) upper_bound(al(v), kk)
#define tpu(str) transform(al(str), str.begin(), ::toupper)
#define tpl(str) transform(al(str), str.begin(), ::tolower)
#define cignr cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define mxv(v) *max_element(al(v))
#define mnv(v) *min_element(al(v))
const int MOD = 1e9 + 7;
const ll INF = 2e18;
const int mxn = 2e9 + 9;
const int mxd = 2e5 + 5;
const int mxa = 1e5 + 5;
//~~~~~~~~~~~~~~~~~~Function~~~~~~~~~~~~~~~~~~~~//
lls gcd(lls a, lls b)
{
if (b == 0LL)
return a;
return gcd(b, a % b);
}
lls lcm(lls a, lls b)
{
return (a / gcd(a, b) * b);
}
//~~~~~~~~~~~~~~~Loops and Short~~~~~~~~~~~~~~~~//
#define PI acos(-1)
#define Cn continue
#define Br break
#define off return 0
#define N '\n'
#define WT(somthing) \
auto az = something; \
cout << az << N;
#define WTS(something) cout << something << N;
#define rep(i, n) for (lls i = 0; (lls)i < n; i++)
#define repn(i, a, b) for (lls i = (lls)(a); i < (lls)(b); i++)
#define repr(i, a, b) for (lls i = (lls)(a)-1; i >= (lls)(b); i--)
#define test_case() int T; T = 1; while (T--)
using namespace std;
lls maxll(lls x, lls y) { return x > y ? x : y; }
lls minll(lls x, lls y) { return x < y ? x : y; }
// ===================================~~~~~~ SOLUTION STARTS HERE ~~~~~~=================================== //
//int a, b, c, d, e, res = 0, ans = 0, prod = 1, n, m, cnt = 0, k, l, r, s, t, x, y, f, i, j, p;
void Run_Case()
{
int n;
cin >> n;
vi a(n), b(n), c(n);
rep (i, n) cin >> a[i];
rep (i, n) cin >> b[i];
rep (i, n) cin >> c[i];
vi va;
map <int, int> ma, mb;
rep (i, n) {
ma[b[c[i] - 1]]++;
}
ull cnt = 0;
rep (i, n) {
if (ma[a[i]]) cnt += ma[a[i]];
}
cout << cnt << N;
}
int main()
{
I_O
test_case()
{
Run_Case();
}
off;
} | #include<bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int N=5e5+10;
int c[N];
vector<ll>p[N];
ll min1(ll a,ll b)
{
if(a<b) return a;
else return b;
}
ll lb(ll x)
{
return x&(-x);
}
void add(int x,int d) {for(;x<N;x+=lb(x))c[x]+=d;}
int geti(ll x)
{
int r=0;
for(;x;x-=lb(x))
r+=c[x];
return r;
}
void sset(int x)
{
if(geti(x)-geti(x-1)==0)
add(x,1);
}
void reset(int x)
{
if(geti(x)-geti(x-1)==1)
add(x,-1);
}
//单点修改 就是变得能走还是不能走
int main()
{
ll n,m,k;
cin>>n>>m>>k;
ll x, y, mx = n + 1, my = m + 1;
for(ll i=1;i<=k;i++)
{
cin>>x>>y;
p[x].push_back(y);
if(x==1) my=min1(y,my);
if(y==1) mx=min1(x,mx);
}
for(ll i=1;i<=mx;i++) sort(p[i].begin(),p[i].end());
// cout<<endl<<"mx="<<mx<<' '<<"my="<<my<<endl;
for(ll i=1;i<=m;i++) sset(i);
for(ll y=my;y<=m;y++) reset(y);
ll ans = 0;
if(!p[1].empty())
{
ans+=p[1][0]-1;
//cout<<' '<<ans<<' '<<endl;
}
else ans+=m;
//cout<<endl;
//cout<<ans<<endl ;
for(ll x=2;x<=n;x++)
{
for (ll y: p[x]) reset(y);
if (x < mx) {
if (p[x].empty()) {
ans += m;
//这一行都可以走
// cout<<x<<' '<<ans<<endl;
}
else {
ans += p[x][0] - 1;
// cout<<x<<' '<<ans<<endl;
ans += geti(m)-geti(p[x][0]);
// cout<<x<<' '<<ans<<endl;
}
}
//上面的都没问题
//reset的位置都能存到下一个
else {
if(p[1].empty())
ans += geti(m);
else ans+=geti(p[1][0]-1);
// cout<<x<<' '<<ans<<endl;
}
}
cout<<ans;
return 0;
}
|
#include<bits//stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <= n; i++)
#define rrep(i,n) for(ll i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
const int MAX = 510000;
const ll MOD = 1000000007;
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 ll INF = 1e18;
int main(){
int n;string s;cin>>n>>s;
stack<char> st;
rep(i,n){
st.push(s[i]);string t;
if(st.size()>=3){
rep(j,3){
t.push_back(st.top());st.pop();
}if(t!="xof"){
rep(i,3) st.push(t[2-i]);
}
}
}cout << st.size()<<endl;
} | #include <bits/stdc++.h>
using namespace std;
main() {
int H, W;
cin >> H >> W;
assert(2<=H && H<=100);
assert(2<=W && W<=100);
vector<string> S(H);
for(string& s: S) cin >> s;
int ans{};
for(string s: S)
for(int j{}; j < W-1; j++)
if(s.substr(j,2) == "..") ans++;
assert(0 <= ans && ans <= H*(W-1));
for(int j{}; j < W; j++)
for(int i{}; i < H-1; i++)
if(S[i][j]=='.' && S[i+1][j]=='.')
ans++;
assert(0 <= ans && ans <= H*(W-1)+(H-1)*W);
cout << ans;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define fef(i,a,b) for(ll i=a;i<=b;i++)
#define rer(i,a,b) for(ll i=b;i>=a;i--)
#define wew while(true)
#define FILE_IN "D.inp"
#define FILE_OUT "D.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD (ll(1000000007))
#define INF (ll(1000000007))
#define MAXN 300011
#define x first
#define y second
#define pii pair<int,int>
#define piii pair<int,pii>
#define pb push_back
#define endl "\n"
template<typename T> tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update> order_set;
typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> order_trie;
typedef long long ll;
class DisjointSet{ public:
vector<int> parent;
DisjointSet(int n): parent(n) { for(int i=0; i<n; i++) parent[i] = i; }
void join(int a, int b) { parent[find(b)] = find(a); }
int find(int a){ return a == parent[a] ? a : parent[a] = find(parent[a]); }
bool check(int a, int b){ return find(a) == find(b); }
};
//---------END-------//
template<typename T>
void read(T &X){
X=0;
char c;
int sign=1;
c=getchar();
if (c=='-') sign=-1;else X=X*10+c-'0';
while ((c=getchar()) && ('0'<=c) && (c<='9'))
X=X*10+c-'0';
}
ll N,x,M,H,T;
vector<ll> A;
int main(){
//ofile;
cin >> N;
for (ll i=1;i*i<=N;i++) if (N%i==0){
A.pb(i);
if (N/i!=i) A.pb(N/i);
}
sort(A.begin(),A.end());
for (ll i:A) cout << i << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
string x;
getline(cin,x);
int v=0,i=0;
while(v!=1&&i<x.size())
{
if(x[i]=='.')
v=1;
i++;
}
if(v==1)
for(int j=0;j<i-1;j++)
cout<<x[j];
else
for(int j=0;j<x.size();j++)
cout<<x[j];
return 0;
} |
//缓缓飘落的枫叶像思念,我点燃烛火温暖岁末的秋天。
//堆排序找top k;
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(0);cin.tie(0);
using namespace std;
typedef long long ll;
const ll N=2e6;
int a[5];
int main()
{
cin>>a[1]>>a[2]>>a[3];
sort(a+1,a+4);
if(a[3]-a[2]==a[2]-a[1])cout<<"Yes";
else
cout<<"No";
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
vector<int> a(3);
int sum = 0;
for (int&x : a) cin >> x, sum += x;
sort(a.begin(), a.end());
if (sum % 3 != 0 || sum/3 != a[1]) cout << "No";
else cout << "Yes";
return 0;
}
|
//====================Template==================
#include <bits/stdc++.h>
// #include <boost/lexical_cast.hpp> // for lexical_cast()
using namespace std;
#define endl '\n'
#define ll long long int
#define ld long double
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define inf 1e18
#define mid(l, r) (l+(r-l)/2)
#define loop(i, a, b) for(int i=(a); i<b; i++)
#define loopr(i, a, b) for(int i=(a); i>b; i--)
#define sz(a) int((a).size())
#define all(c) c.begin(), c.end()
#define allr(c) c.rbegin(), c.rend()
#define tr(container, it) \
for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define present(container, element) \
(container.find(element) != container.end())
#define vpresent(container, element) \
(find(all(container),element) != container.end())
#define print(dp, n); \
loop(i, 0, n){cerr << dp[i] << " ";}cerr << endl;
#define print2(dp, a, n, b, m); \
loop(i, a, n){loop(j, b, m){cerr << dp[i][j] << " ";}cerr << endl;}
#define countetbits(i)\
__builtin_popcount(i)
typedef vector< long long int > vl;
typedef vector< int > vi;
typedef vector< string > vs;
typedef vector< double > vd;
typedef vector< vi > vvi;
typedef vector< vl > vvl;
typedef pair< ll,ll > pll;
typedef pair< int, int> pii;
//sieve of eratosthenes
vector<int> smallest_factor;
vector<bool> prime;
vector<int> primes;
const int P_MAX = int(1e5) + 5;
void sieve(int maximum) {
maximum = max(maximum, 1);
smallest_factor.assign(maximum + 1, 0);
prime.assign(maximum + 1, true);
prime[0] = prime[1] = false;
primes = {};
for (int p = 2; p <= maximum; p++)
if (prime[p]) {
smallest_factor[p] = p;
primes.push_back(p);
for (int64_t i = int64_t(p) * p; i <= maximum; i += p)
if (prime[i]) {
prime[i] = false;
smallest_factor[i] = p;
}
}
}
// __gcd(m, n)
ll gcd(ll a, ll b)
{
while (true)
{
if (a == 0) return b;
b %= a;
if (b == 0) return a;
a %= b;
}
}
ll lcm(ll a, ll b)
{
ll temp = gcd(a, b);
return temp ? (a / temp * b) : 0;
}
template <class A, class B>
auto findMin(A a, B b) -> decltype(a < b ? a : b)
{
return (a < b) ? a : b;
}
//Type Inference refers to automatic deduction of the data type of an expression in a programming language.
//Auto lets you declare a variable with particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.
void file_i_o(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("cerr.txt", "w", stderr);
#endif
}
// lexical_cast() converts a int into string
// string stri = boost::lexical_cast<string>(i_val);
//===========================Template Ends==================================
ll modd = 1000000009;
ll n, k, q;
ll dp[100005];
void run_case(){
cin >> n;
ll rem = n%100;
cout << 100-rem << endl;
}
int main(){
clock_t begin = clock();
// sieve(P_MAX);
file_i_o();
int tests = 1;
// int tests;
// cin >> tests;
while(tests-- > 0)
run_case();
#ifndef ONLINE_JUDGE
clock_t end = clock();
cout << "\n\nExecuted In: " << double(end - begin) /CLOCKS_PER_SEC << " seconds" << endl;
#endif
return 0;
}
//Debug
//1. input for test
//2. look for type conversion, char to int
//3. look for declaration of large arrays.
| #include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
if(n%100 == 0)
{
if(n>=100 || n==0)
cout<<"100";
else
cout<<"0";
}
else
{
cout<<100-(n%100);
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i,n) for(int i = 0; i < (n); i++)
tuple<ll, ll, ll> extgcd(ll a, ll b) {
if(b == 0) return {a, 1, 0};
auto [g, x, y] = extgcd(b, a%b);
return {g, y, x-a/b*y};
}
void solve() {
ll n, s, k;
cin >> n >> s >> k;
auto [g, x, y] = extgcd(k, n);
if(s%g != 0) {
cout << -1 << endl;
return;
}
n /= g;
s /= g;
k /= g;
ll ans = ((x*-s)%n+n)%n;
cout << ans << endl;
}
int main() {
int t;
cin >> t;
rep(i,t) solve();
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define re register
#define INF 2147483647
using namespace std;
inline ll read()
{
ll f=1,x=0;char s=getchar();
while(s<'0'||s>'9')
{
if(s=='-') f=-1;
s=getchar();
}
while(s>='0'&&s<='9')
{
x=x*10+s-48;
s=getchar();
}
return f*x;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b)
{
x=1,y=0;
return a;
}
ll ans=exgcd(b,a%b,y,x);
y-=a/b*x;
return ans;
}
int main()
{
int T=read();
while(T--)
{
ll n=read(),s=read(),k=read();
s=n-s;
ll x,y;
ll GCD=exgcd(n,k,x,y);
if(s%GCD!=0) puts("-1");
else
{
ll res=s/GCD;
y*=res;
y=(y%(n/GCD)+(n/GCD))%(n/GCD);
printf("%lld\n",y);
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define uint unsigned int
#define inf (ll)1e18
#define end1 " "
#define ld long double
#define mp make_pair
#define pb push_back
#define all(x) begin(x),end(x)
#define sf(x) sizeof(x)
#define print_double(d, n) cout<<fixed<<setprecision(n)<<d<<end1;
bool dcmp(int x, int y) {
return x > y;
}
bool acmp(int x, int y) {
return x < y;
}
bool afcmp(pair<int, int> a, pair<int, int> b) {
return a.first < b.first;
}
bool dfcmp(pair<int, int> a, pair<int, int> b) {
return a.first > b.first;
}
bool ascmp(pair<int, int> a, pair<int, int> b) {
return a.second < b.second;
}
bool dscmp(pair<int, int> a, pair<int, int> b) {
return a.second > b.second;
}
int aabs(int a) {
return a < 0 ? -a : a;
}
int tmax(int a, int b, int c) {
return max(max(a, b), c);
}
int tmin(int a, int b, int c) {
return min(min(a, b), c);
}
bool in_range(int x, int l, int r) {
return x >= l && x <= r;
}
inline char to_char(int c) {
if (c >= 10) return c - 10 + 'A';
return c + '0';
}
bool is_prime(int val) {
if (val == 2) {
return true;
}
if (val % 2 == 0) {
return false;
}
for (int i = 3; i * i <= val; i += 2) {
if (val % i == 0) {
return false;
}
}
return true;
}
int gcd(int a, int b) {
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
string tobase(int num, int base) {
string ret;
int div = base;
while (div < num) {
div *= base;
}
for (div /= base;;) {
ret += to_char(num / div);
num = num % div;
if (div < 2) {
break;
}
div /= base;
}
return ret;
}
const int maxN = 3005;
bool finished = false;
int N;
ll a[maxN], pre[maxN], mem[maxN][maxN], idx[maxN][maxN], dp[maxN][maxN], mod = (int) 1e9 + 7;
void reset() {
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
}
void solve(void) {
reset();
if (finished) {
return;
}
memset(idx, -1, sf(idx));
memset(mem, -1, sf(mem));
for (int i = 0; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
idx[i][j] = mem[j][pre[i] % j]+1;
mem[j][pre[i] % j] = i;
}
}
dp[0][0] = 1;
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
if (idx[i][j] != 0) {
dp[i][j] = (dp[idx[i][j]-1][j] + dp[idx[i][j]-1][j - 1]) % mod;
}
}
}
ll ans = 0;
for (int i = 1; i <= N; ++i) {
ans += dp[N][i];
ans %= mod;
}
cout << ans << endl;
}
void open_file() {
freopen("pump.in", "r", stdin);
freopen("pump.out", "w", stdout);
}
int main(void) {
int T = 1;
// open_file();
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
//cin >> T;
while (T--) {
solve();
}
return 0;
}
| #include <iostream>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<string>
#include <cstring>
#include<queue>
#include<set>
#include<stack>
#define MAX_N 100050
#define INF 999999
using namespace std;
typedef long long ll;
ll cnt[300050];
ll a[300050];
ll n;
ll sum;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
int x = 0;
cin >> x;
cnt[x+200]++;
}
for(int i=-200;i<=200;i++)
for (int j = -200; j <= 200; j++)
{
sum += cnt[i + 200] * cnt[j + 200] * (i - j) * (i - j) ;
}
cout << sum / 2;;
} |
#include <bits/stdc++.h>
using namespace std;
// BEGIN LIB
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
#define print_grid(x) for(auto &r:x){for(auto &c:r)cout<<c<<" ";cout<<endl;}cout<<endl;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<vector<ll>> grid;
typedef pair<ll, ll> pll;
// END LIB
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll a, b, c; cin>>a>>b>>c;
c = c%2==1? 1:2;
ll ta, tb;
if(c==1){
ta=a;
tb=b;
} else {
ta = a*a;
tb = b*b;
}
if(ta==tb) cout << "=" << endl;
if(ta>tb) cout << ">" << endl;
if(ta<tb) cout << "<" << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int a,c,b,i;
cin>>a>>b>>c;
if(c==1)
{
if(a>=b)
{
cout<<"Takahashi"<<endl;
return 0;
}
else if(a<b)
{
cout<<"Aoki"<<endl;
}
}
else
{
if(a<=b)
{
cout<<"Aoki"<<endl;
return 0;
}
else cout<<"Takahashi"<<endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define setbits(n) __builtin_popcountll(n)
#define mkunique(a) sort(a.begin(),a.end()); a.resize(unique(a.begin(),a.end())-a.begin());
#define print(s) for(ll u: s) cout<<u<<" "; cout<<"\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod=1e9+7;
ll n;
pair<ll,ll> check(vector<ll> fin){
ll x=0, y=0;
for(ll i=0;i<fin.size();i++){
if(fin[i]==1) x++;
if(fin[i]==2) y++;
if(fin[i]==3) x=(x+y);
if(fin[i]==4) y=(x+y);
//cout<<x<<" "<<y<<"\n";
}
return {x,y};
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll TT=clock();
vector<ll> f(100);
f[0]=0; f[1]=1;
for(ll i=2;i<90;i++)
f[i]=f[i-1]+f[i-2];
cin>>n;
ll cur = n;
vector<ll> ans;
while(cur){
auto it = --upper_bound(f.begin(),f.end(),cur);
ans.pb(it-f.begin());
cur = cur - *it;
}
//print(ans);
ll mx = ans[0];
set<ll> s;
for(ll i=0;i<ans.size();i++){
ans[i] = mx - ans[i];
ans[i]++;
s.insert(ans[i]);
}
assert(s.size()==ans.size());
//print(ans);
vector<ll> fin;
ll j=0;
for(ll i=1;i<mx;i++){
if(j<ans.size() && ans[j]==i){
if(i%2) fin.pb(2);
else fin.pb(1);
j++;
}
if(i%2) fin.pb(3);
else fin.pb(4);
}
pair<ll,ll> v = check(fin);
bool rev = false;
if(v.ss == n) rev = true;
for(ll &u: fin){
if(rev){
if(u==1) u=2;
else if(u==2) u=1;
else if(u==3) u=4;
else u=3;
}
}
assert(check(fin).ff == n);
cout<<fin.size()<<"\n";
for(ll u: fin)
cout<<u<<" ";
cerr<<"\n\nTIME: "<<(long double)(clock()-TT)/CLOCKS_PER_SEC<<" sec\n";
TT = clock();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#ifndef aa
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define pdd pair<double,double>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//const int mod = 998244353;
const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;
uniform_int_distribution<int> rng(1,1e9);
//DEBUG FUNCTIONS START
#define cerr cout
void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) cerr << "[" << #x << "] = "; _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END
const int N = 2e6+5;
void solve(){
int n;
cin>>n;
vi a(n+1), pos(n+1), pre(n+1);
int mx = 0, ans = 0;
fr(i,1,n){
cin>>a[i];
pre[i] = pre[i-1] + a[i];
maxi(mx, pre[i]);
maxi(ans, pos[i-1] + mx);
pos[i] = pos[i-1] + pre[i];
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//srand(chrono::high_resolution_clock::now().time_since_epoch().count());
cout << fixed << setprecision(15);
int t = 1;
//cin >> t;
while (t--)
solve();
#ifdef aa
cout << endl << endl << endl << endl << "Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
int powm(int a, int b){
int res = 1;
while (b) {
if (b & 1)
res = (res*a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int divide(int a, int b) {
return (a % mod) * powm(b % mod, mod - 2) % mod;
}
int norm(int a) {
while (a >= mod) a -= mod;
while (a < 0) a += mod;
return a;
} |
#line 1 "/home/siro53/kyo-pro/compro_library/template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return 1;
}
return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __Setup;
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//-------------------------------------
#line 1 "/home/siro53/kyo-pro/compro_library/util/get_time.hpp"
inline double get_time() {
const double ticks_per_sec = 2500000000;
uint32_t lo, hi;
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
return (((uint64_t)hi << 32) | lo) / ticks_per_sec;
}
#line 1 "/home/siro53/kyo-pro/compro_library/util/randint.hpp"
struct random_number_generator {
mt19937 mt;
random_number_generator():mt(chrono::steady_clock::now().time_since_epoch().count()) {}
int operator()(int a, int b) {
uniform_int_distribution<int> dist(a, b - 1);
return dist(mt);
}
int operator()(int b) {
return (*this)(0, b);
}
};
#line 4 "main.cpp"
struct P{
int x, y;
P(){}
P(int x, int y):x(x), y(y) {}
};
struct Ad {
P p1, p2;
Ad(){}
Ad(int a, int b, int c, int d):p1(a, b), p2(c, d) {}
Ad(P p1, P p2):p1(p1), p2(p2) {}
};
random_number_generator rng;
int N;
int x[200], y[200], r[200];
Ad ad[200];
void input() {
cin >> N;
REP(i, N) cin >> x[i] >> y[i] >> r[i];
}
void output() {
REP(i, N) {
cout << ad[i].p1.x << " " << ad[i].p1.y << " " << ad[i].p2.x << " " << ad[i].p2.y << "\n";
}
}
void solve() {
REP(i, N) {
ad[i] = Ad(x[i], y[i], x[i]+1, y[i]+1);
}
}
int main() {
input();
solve();
output();
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(a,b) for(long long a = 0;a < b;++a)
using ld = long double;
unsigned long xor128() {
static unsigned long x=time(NULL), y=362436069, z=521288629, w=88675123;
unsigned long t=(x^(x<<11));
x=y; y=z; z=w;
return ( w=(w^(w>>19))^(t^(t>>8)) );
}
typedef struct Sponsor{
int x;
int y;
int area;
} Sponsor;
typedef struct AD{
int x;
int y;
int dx;
int dy;
Sponsor sponsor;
} AD;
inline bool check_intersect(AD a,AD b){
bool ans = (min(a.x,b.x) <= max(a.x,b.x) and max(a.x,b.x) < min(a.x+a.dx,b.x + b.dx));
ans &= (min(a.y,b.y) <= max(a.y,b.y) and max(a.y,b.y) < min(a.y+a.dy,b.y + b.dy));
return ans;
}
bool validate(vector<AD> inputs){
for(int i = 0;i < inputs.size();++i){
if(inputs[i].x + inputs[i].dx > 10000 or inputs[i].y + inputs[i].dy > 10000) return false;
for(int q = i+1;q < inputs.size();++q){
if(check_intersect(inputs[i],inputs[q]) == true) return false;
}
}
return true;
}
ld scoring(vector<AD> inputs,ld border){
ld ans = 0;
for(int i = 0;i < inputs.size();++i){
if(inputs[i].x <= inputs[i].sponsor.x and inputs[i].sponsor.x < inputs[i].x + inputs[i].dx
and inputs[i].y <= inputs[i].sponsor.y and inputs[i].sponsor.y < inputs[i].y + inputs[i].dy){
ld real_area = inputs[i].dx * inputs[i].dy;
ld expected_area = inputs[i].sponsor.area;
ans += 1.0L - abs(border - pow(1 - min(real_area,expected_area)/max(real_area,expected_area),2.0));
}else return -1;
}
ans /= (ld)inputs.size();
return ans;
}
int main(){
int n;
cin >> n;
vector<AD> inputs;
REP(i,n){
int a,b,c;
cin >> a >> b >> c;
inputs.push_back(AD{a,b,1,1,Sponsor{a,b,c}});
}
ld now_score = scoring(inputs,0);
for(int i = 0;i < 3000000;++i){
int target = xor128() % inputs.size();
AD tmp = inputs[target];
int dir = xor128() % 6;
int step = xor128() % 200 - 100;
if(i % 20000 != 0){
if(dir == 0){
inputs[target].dx += step;
}else if(dir == 1){
inputs[target].dy += step;
}else if(dir == 2){
inputs[target].x -= step;
inputs[target].dx += step;
}else if(dir == 3){
inputs[target].y -= step;
inputs[target].dy += step;
}else if(dir == 4){
inputs[target].x += step;
}else if(dir == 5){
inputs[target].y += step;
}
}
bool ok = true;
for(int q = 0;q < inputs.size();++q){
if(q != target and check_intersect(inputs[q],inputs[target]) == true){
ok = false;
}
}
if(inputs[target].dx <= 0 or inputs[target].dy <= 0 or inputs[target].x < 0 or inputs[target].y < 0) ok = false;
if(inputs[target].x + inputs[target].dx > 10000 or inputs[target].y + inputs[target].dy > 10000) ok = false;
ld score = 0;
int hoge = (ld)(3000000.0 - i) / 300000.0;
ld prob = hoge / 10.0;
prob *= prob;
score = scoring(inputs,prob) + (1.00L - prob);
if(ok and score >= now_score){
now_score = score;
}else{
inputs[target] = tmp;
}
}
for(int i = 0;i < n;++i){
cout << inputs[i].x << " " << inputs[i].y << " " << inputs[i].x + inputs[i].dx << " " << inputs[i].y + inputs[i].dy << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
int a[n];
for(int i = 0;i < n;i++){
cin >> a[i];
}
int ans = 0;
for(int l = 0;l < n;l++){
int x = a[l];
for(int r = l;r < n;r++){
x = min(x,a[r]);
ans = max(ans,x*(r-l+1));
}
}
cout << ans << endl;
} | #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;
template <typename T> using vt = vector<T>;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define SZ(x) ((int)((x).size()))
#define EACH(x, a) for (auto& x: a)
#define FOR(i,s,n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i,s,l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC int __tc; cin >> __tc; FOR(case_num,1,__tc+1)
#define TEST(x,i) ((x)&(1ll<<(i)))
#define SET(x,i) ((x)|(1ll<<(i)))
#define FLIP(x,i) ((x)^(1ll<<(i)))
#define CLEAR(x,i) ((x)&~(1ll<<(i)))
const double pi = 4 * atan(1);
using ll = long long;
using pll = pair<ll, ll>;
mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
return uniform_int_distribution<ll>(a, b)(mt_rng);
}
template<class T> bool umin(T& a, const T& b) {
return b<a?a=b, 1:0;
}
template<class T> bool umax(T& a, const T& b) {
return a<b?a=b, 1:0;
}
template<class ForwardIterator> void print_vec(ForwardIterator first,
ForwardIterator last, string sep = " ", string end = "\n") {
bool ft = true;
while (first != last) {
if (!ft) {cout << sep;} else {ft = false;}
cout << (*first);
++first;
}
cout << end;
}
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream &o, const pair<T1, T2> &p) {
return o << p.F << " " << p.S;
}
inline ll floorDiv(ll x, ll y) {
ll d = x / y;
ll r = x % y;
return r ? (d - ((x < 0) ^ (y < 0))) : d;
}
ll ceilDiv(ll x, ll y) {
return -floorDiv(-x, y);
}
ll bin_search(ll lo, ll hi, function<bool(ll)> predicate) {
int sign = lo <= hi ? 1 : -1;
lo *= sign;
hi *= sign;
hi++;
while (lo < hi) {
ll mid = lo + floorDiv(hi-lo, 2);
if (!predicate(sign * mid)) {
lo = mid + 1;
} else {
hi = mid;
}
}
return sign * lo;
}
const ll MOD = 1000000007;
const int MAXN = 10005;
int n;
ll num[MAXN];
//Max histogram area from geeksforgeeks
//https://www.geeksforgeeks.org/largest-rectangle-under-histogram/
ll getMaxArea() {
stack<ll> s;
ll max_area = 0;
int tp;
ll area_with_top;
int i = 0;
while (i < n)
{
if (s.empty() || num[s.top()] <= num[i])
s.push(i++);
else
{
tp = s.top();
s.pop();
area_with_top = num[tp] * (s.empty() ? i :
i - s.top() - 1);
if (max_area < area_with_top)
max_area = area_with_top;
}
}
while (s.empty() == false)
{
tp = s.top();
s.pop();
area_with_top = num[tp] * (s.empty() ? i :
i - s.top() - 1);
if (max_area < area_with_top)
max_area = area_with_top;
}
return max_area;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i=0; i < n; i++) {
cin >> num[i];
}
cout << getMaxArea() << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int N, X;
char S;
int cnt;
cin>>N>>X;
cnt = X;
for (int i=0; i<N; i++){
cin>>S;
if (S == 'o') cnt += 1;
else{
if (cnt>0) cnt -= 1;
}
}
cout << cnt << endl;
}
| #include<iostream>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<cmath>
#include<map>
#include<string>
#include<bitset>
using namespace std;
#define rep(i,N) for(ll i=0;i<N;i++) //0から
typedef long long ll;
ll binary_search(vector<ll> a, ll n, ll key){
ll right = n, left = -1;
ll md = (right + left) / 2;
while(right - left > 1){
if(a[md] <= key){
right = md;
}else{
left = md;
}
md = (right + left) / 2;
}
if(left == -1) return -1; //無い場合
return right;
}
vector<ll> prime;
void Prime(ll n){ //線形篩,素数列挙
vector<ll> p(n,0);
p[0]=1;
p[1]=1;
for(ll i=2;i<n;i++){
if(p[i]==0){
prime.push_back(i);
p[i]=i;
}
ll k=prime.size();
for(ll j=0;j<k && i*prime[j]<n && prime[j]<=p[i];j++){
p[i*prime[j]]=prime[j];
}
}
}
ll gcd(ll a,ll b){
if(a<b){
swap(a,b);
}
//a>=b
ll r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;
}
return b;
}
ll modexp(ll x, ll a, ll m){ //x^a mod m
ll ret = 1;
while (a > 0) {
if (a & 1) ret = ret * x % m; // n の最下位bitが 1 ならば x^(2^i) をかける
x = x * x % m;
a >>= 1; // n を1bit 左にずらす
}
return ret;
}
ll modinv(ll a, ll m){ //a^{-1} mod m
ll b=m,u=1,v=0;
while(b){
ll t=a/b;
a-=t*b; swap(a,b);
u-=t*v; swap(u,v);
}
u=(u+m)%m;
return u;
}
ll nCr(ll n, ll r) {
r = min(r, n-r);
ll ans = 1;
for (ll i = n; i > n - r; --i) {
ans = ans*i;
}
for (ll i = 1 ; i < r + 1; ++i) {
ans = ans / i;
}
return ans;
}
double pi=3.14159265358979323846;
//next_purmitation!!!!!!!!!!!!!
//https://atcoder.jp/contests/abc198/tasks/abc198_d
//cout<<fixed<<setprecision(18);
int main(){
ll n;
cin>>n;
ll ans=1;
for(ll i=1;i<=11;i++){
ans*=n-i;
ans/=i;
}
cout<<ans<<endl;
return 0;
} |
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){
a = b; return true;
}
return false;
}
int main(){
long double A, B; std::cin >> A >> B;
std::cout << std::fixed << std::setprecision(10) << (A - B) / A * 100 << std::endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pc cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n";
int mod = 1000000000 + 7;
bool cmp(const pair<int,int> &a,const pair<int,int> &b)
{
return max(a.second,a.first)<max(b.second,b.first);
}
int Q(int l,int r){
cout<<"? "<<l<<" "<<r<<endl;
int sum;
cin>>sum;
return sum;
}
void solve()
{
int i, j, m,k;
int n;
cin>>n;
cout<<(n+100-1)/100<<endl;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--)
{
// int k,n,i,j,m;
// cin>>n;
solve();
}
pc;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
int powmod(int a,int l, int md){a%=md; int res=1;while(l){if(l&1)res=res*a%md;l/=2;a=a*a%md;}return res;}
int binpow(int a,int l){int res=1;while(l){if(l&1)res=res*a;l/=2;a=a*a;}return res;}
int invmod(int a, int md){return powmod(a,md-2,md);}
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
typedef vector<int> vi; typedef pair<int, int> ii; typedef vector< ii > vii;
#define pb push_back
int __set(int b, int i) {return b|(1LL<<i);} //set ith bit
int __unset(int b, int i) {return b&(~(1UL<<i));}
int __check(int b, int i) {return b&(1LL<<i);} //returns 0 if ith bit is 0
int mulmod(int a, int b, int md) {return (((a%md)*(b%md))%md+md)%md;}
int addmod(int a, int b, int md) {return ((a%md+b%md)%md+md)%md;}
int submod(int a, int b, int md) {return (((a%md-b%md)%md)+md)%md;}
int divmod(int a, int b, int md) {return mulmod(a, powmod(b, md-2, md), md);} //if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL; //very large number
priority_queue<int, vector<int>, greater<int> > pq; //for min priority_queue
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int MOD=998244353;
signed main(void)
{
IOS;
int n,k; cin>>n>>k;
int vv[2*n+1]={};
for(int i = 2; i <= 2*n; i++) {
int lo=max(1ll,i-n);
int hi = i-lo;
vv[i] = hi-lo+1;
}
// for(int i = 2; i<=2*n; i++)
// cout<<vv[i]<<" ";
// cout<<endl;
int ans=0;
for(int i = 2; i <= 2*n; i++) {
int s = i-k;
if(s<2 || s > 2*n)
continue;
ans += vv[s]*vv[i];
// ans %= MOD;
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// #include <boost/rational.hpp>
// using bigint = boost::multiprecision::cpp_int;
// using fraction = boost::rational<bigint>;
using namespace std;
#define pb push_back
#define eb emplace_back
#define unused [[maybe_unused]]
#define tempT template<class T>
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define debug(x) cerr << #x << ": " << x << endl
#define ans_exit(x) { cout << x << endl; exit(0); }
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (ll i = (ll)(n); i < (ll)(m); i++)
using ll unused = long long;
using ld unused = long double;
using vl unused = vector<ll>;
using vvl unused = vector<vl>;
using vvvl unused = vector<vvl>;
using lp unused = pair<ll, ll>;
using lmap unused = map<int, int>;
unused constexpr ll MOD = 1e9 + 7;
unused constexpr ll INF = 1 << 30;
unused constexpr ll LINF = 1LL << 62;
unused constexpr int DX[8] = {0, 1, 0,-1, 1, 1,-1,-1};
unused constexpr int DY[8] = {1, 0,-1, 0, 1,-1, 1,-1};
unused inline bool bit(ll b, ll i) { return b & (1LL << i); }
unused inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
unused inline ll mod(ll a, ll m = MOD) { return (a % m + m) % m; }
unused inline ll floordiv(ll a, ll b) { return a / b - (a < 0 && a % b); }
unused inline ll ceildiv(ll a, ll b) { return floordiv(a + b - 1, b); }
tempT unused inline bool in_range(T a, T x, T b) { return a <= x && x < b; }
unused inline bool in_range(ll x, ll b) { return in_range(0LL, x, b); }
tempT unused bool chmin(T &a, T b) { if(a > b) {a = b; return 1;} return 0; }
tempT unused bool chmax(T &a, T b) { if(a < b) {a = b; return 1;} return 0; }
int main() {
ll n, m;
cin >> n >> m;
vl a(n), b(m);
rep(i, n) cin >> a[i];
rep(i, m) cin >> b[i];
vvl dp(n+1, vl(m+1, LINF));
rep(i, n + 1) dp[i][0] = i;
rep(j, m + 1) dp[0][j] = j;
rep(i, n) {
rep(j, m) {
if(a[i] == b[j]) {
chmin(dp[i+1][j+1], dp[i][j]);
} else {
chmin(dp[i+1][j+1], min(dp[i+1][j], dp[i][j+1]) + 1);
chmin(dp[i+1][j+1], dp[i][j] + 1);
}
}
}
cout << dp[n][m] << endl;
} |
// ABC180-E
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
template <typename T>
T TSP(int s, vector<vector<pair<int, T>>>& G, const T INF = 1e9) {
int V = G.size();
vector<vector<T>> dp(1 << V, vector<T>(V, INF));
dp[0][s] = 0;
for (int bit = 0; bit < 1 << V; bit++) {
for (int v = 0; v < V; v++) {
for (auto p : G[v]) {
int to = p.first, dist = p.second;
if (bit & 1 << to) continue;
dp[bit | 1 << to][to] =
min(dp[bit | 1 << to][to], dp[bit][v] + dist);
}
}
}
return dp[(1 << V) - 1][s];
}
signed main() {
int n;
cin >> n;
int x[n], y[n], z[n];
rep(i, n) {
cin >> x[i] >> y[i] >> z[i];
}
vector<vector<pair<int, ll>>> G(n);
rep(i, n) {
rep(j, n) {
G[i].emplace_back(
j, abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0, z[j] - z[i]));
}
}
cout << TSP(0, G, LINF) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define io ios::sync_with_stdio(false); cin.tie(0)
void print_vec(const vector<int>& vec) {
int n = vec.size();
rep(i, n) {
cout << vec[i];
if (i != n-1) cout << " ";
}
cout << endl;
}
int main()
{
io;
int n; cin >> n;
vector<int> a(n), b(n);
vector<vector<int>> c(n, vector<int>(n, 0));
rep(i, n) rep(j, n) {
cin >> c[i][j];
}
b = c[0];
a[0] = 0;
bool flag = true;
for (int i = 1; i < n; ++i) {
for (int j = 1; j < n; ++j) {
int diff = c[i][0] - c[0][0];
if (c[i][j] - c[0][j] != diff) {
flag = false;
break;
}
a[i] = diff;
}
}
int mn = *min_element(a.begin(), a.end());
if (mn < 0) {
rep(i, n) {
a[i] -= mn;
b[i] += mn;
if (b[i] < 0) {
flag = false;
break;
}
}
}
if (flag) {
cout << "Yes" << endl;
print_vec(a);
print_vec(b);
} else cout << "No" << endl;
return 0;
} |
#include<algorithm>
#include<cstdio>
using namespace std;
int n,m;
int main()
{
scanf("%d",&n);
printf("6 10 15 ");m=3;
for (int i=16;i<=10000&&m<n;i++){
int cnt=0;
if (i%2==0)cnt++;
if (i%3==0)cnt++;
if (i%5==0)cnt++;
if (cnt>=2){m++;printf("%d ",i);}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
template<class T,class U>
inline bool chmax(T &a,U b){
if(a<b){
a=b;
return true;
}
return false;
}
template<class T,class U>
inline bool chmin(T &a,U b){
if(a>b){
a=b;
return true;
}
return false;
}
constexpr int INF=1000000000;
constexpr ll llINF=1000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
random_device rnd;
mt19937 mt(rnd());//32bit
//[mi,ma]
uniform_int_distribution<int>engine(mi,ma);
return engine(mt);
}
/*
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
C.resize(n+1,vector<ll>(m+1,0));
sC.resize(n+1,vector<ll>(m+1,0));
C[0][0]=1;
for(int i=1;i<=n;i++){
C[i][0]=1;
for(int j=1;j<=m;j++){
C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
}
}
rep(i,n+1){
rep(j,m){
sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
}
}
}*/
ll gcd(ll a,ll b){
while(a%b){
a%=b;
swap(a,b);
}
return b;
}
ll lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
bool prime(int a){
if(a==1)return false;
for(int i=2;i*i<=a;i++){
if(a%i==0)return false;
}
return true;
}
vector<int>primes;
void init_prime(int n){
primes.push_back(2);
for(int i=3;i<=n;i+=2){
bool f=true;
for(int j:primes){
if(j*j>i)break;
if(i%j==0){
f=false;
break;
}
}
if(f)primes.push_back(i);
}
}
ll modpow(ll a,ll b){
ll res=1;
while(b){
if(b&1){
res*=a;
res%=mod;
}
a*=a;
a%=mod;
b>>=1;
}
return res;
}
vector<ll>inv,fact,factinv;
void init_fact(int n){
inv.resize(n+1);
fact.resize(n+1);
factinv.resize(n+1);
inv[0]=0;
inv[1]=1;
fact[0]=1;
factinv[0]=1;
for(ll i=1;i<=n;i++){
if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod);
fact[i]=(fact[i-1]*i)%mod;
factinv[i]=factinv[i-1]*inv[i]%mod;
}
}
ll _inv(ll a,ll m=mod){
//gcd(a,m) must be 1
ll b=m,u=1,v=0;
while(b){
ll t=a/b;
a-=t*b;swap(a,b);
u-=t*v;swap(u,v);
}
u%=m;
if(u<0)u+=m;
return u;
}
ll comb(int a,int b){
if(a<b)return 0;
if(a<0)return 0;
return fact[a]*factinv[a-b]%mod*factinv[b]%mod;
}
ll multicomb(int a,int b){
return comb(a+b-1,b);
}
void solve(){
int n;
cin>>n;
if(n==3){
cout<<"6 10 15"<<endl;
return;
}
vector<int>v{6,10,12,15,18,20,24,30};
rep(i,n){
cout<<(i/8)*30+v[i%8]<<" ";
}
cout<<endl;
}
int main(){
cin.tie(0);ios::sync_with_stdio(false);
//int t;cin>>t;while(t--)
solve();
return 0;
} |
#include <iostream>
#include <iomanip>
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <tuple>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <map>
#include <numeric>
#include <cassert>
#include <string>
#include <list>
using ull = unsigned long long;
using ll = long long;
#define REP(i, n) for(ll i=0;i<(ll)n;i++)
#define REPab(i,a,b)for(ll i=(ll)a;i<(ll)b;i++)
constexpr bool bitp(ull bits, ull pos) { return bits & (1ULL << pos); }
constexpr ll mod(ll val, ll m) { return (val >= 0) ? (val % m) : (val % m + m); }
template <class T> void chmin(T& a, T b) { a = std::min(a, b); }
template <class T> void chmax(T& a, T b) { a = std::max(a, b); }
ll extgcd(const ll a, const ll b, ll& x, ll& y) {ll d = a;if (b != 0) { d = extgcd(b, a % b, y, x);y -= (a / b) * x;} else { x = 1; y = 0; } return d;}
int main() {
ll N, M;
std::cin >> N >> M;
std::vector<std::vector<ll>> edges(N);
REP(i, M) {
ll a, b;
std::cin >> a >> b;
edges[a - 1].push_back(b - 1);
}
ll sum = 0;
REP(i, N) {
std::vector<bool> visited(N, false);
const auto search = [&](auto&& search, ll node) -> void {
for (const auto& edge : edges[node]) {
if (!visited[edge]) {
visited[edge] = true;
sum++;
search(search, edge);
}
}
};
visited[i] = true;
sum++;
search(search, i);
}
std::cout << sum << std::endl;
}
| #include <bits/stdc++.h>
using namespace std;
const int MAXN=2000;
vector<vector<int>> g;
bool temp[MAXN];
void dfs(int v){
if(temp[v]) return;
temp[v]=1;
for(auto vv:g[v]) dfs(vv);
return;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n,m;
cin>>n>>m;
g.resize(n);
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
g[a-1].push_back(b-1);
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++) temp[j]=0;
dfs(i);
for(int j=0;j<n;j++)
if(temp[j]) ans++;
}
cout<<ans;
return 0;
}
|
#include<bits/stdc++.h>
// no of bits to store no 2e5 is 19
// 2e5 ka 5 times square root lene se 2 or 3 ke pass pahunch jate hai
using namespace std;
#define int long long
#define mod 1000000007
#define MOD 1000000007
#define MAX 1000000000
#define inf 1e18
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define mii map<int, int>
#define all(x) (x).begin(), (x).end()
#define fill(a,b) memset(a, b, sizeof(a))
#define sp(x,y) fixed<<setprecision(y)<<x
#define setbits(x) __builtin_popcount(x)
#define fast_in_out ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define rep(i,a,b) for(int i=a;i<b;i++)
#define ll long long
// powermod(a,b,m) == powermod(a,b%(m-1),m) when m is prime fermat's theorem a^(p-1) %p=1 p is prime
int powermod(int a,int b,int c) // gives (a^b)%c
{
int ans = 1,tempans=a%c;
while(b>0)
{
if(b&1==1)
{
ans=(ans*tempans)%c;
}
tempans=(tempans*tempans)%c;
b=b>>1;
}
return ans;
}
int modinverse(int a,int m) // gives a inverse wrt m and m is prime
{
return powermod(a,m-2,m);
}
ll add(ll x, ll y) {
x += y;
if (x >= mod) return x - mod;
return x;
}
ll sub(ll x, ll y) {
x -= y;
if (x < 0) return x + mod;
return x;
}
ll mult(ll x, ll y) {
return (x * y) % mod;
}
// my template m
// prime factorization pf
// ncr and factorials ncr
// matrix exponentiation of fibonacci series in log(n) matexpofib
// cycles in a graph cycle
// prime factorization in log(n) spf
vector<pair<int,pii>> edges[100005];// city
void solve()
{
int n,m,st,en;
cin>>n>>m>>st>>en;
while(m--)
{
int x,y,ti,ki;
cin>>x>>y>>ti>>ki;
edges[x].pb({y,{ti,ki}});
edges[y].pb({x,{ti,ki}});
}
// dis ind
priority_queue<pii> q;
vector<int> dis(n+1,inf);
vector<bool > vis(n+1,false);
dis[st]=0;
q.push({0,st});
while(!q.empty())
{
pii p= q.top();
q.pop();
if(vis[p.ss])
continue;
vis[p.ss]=true;
// cout<<p.ss<<endl;
for(auto ch: edges[p.ss])
{
// cout<<"and " <<ch.ff<<" "<<ch.ss.ff<<" "<<ch.ss.ss<<endl;
int k= ch.ss.ss;
int z= dis[p.ss]%k;
// cout<<dis[p.ss]<<" "<<k<<" "<<z<<endl;
if(z!=0)
{
z= k-z;
}
z+=(dis[p.ss]+ch.ss.ff);
if(!vis[ch.ff]&&z<dis[ch.ff])
{
dis[ch.ff]=z;
q.push({-z,ch.ff});
}
}
}
if(dis[en]==inf)
dis[en]=-1;
cout<<dis[en]<<endl;
}
int32_t main()
{
fast_in_out;
int t=1;//cin>>t;
int count=0;
while(t--)
{
count++;
solve();
}
}
| #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rrep(i, n) for(ll i = n - 1; i > -1; i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define fi first
#define se second
template <class X> void print(X x) { cout << x << endl; }
#define in(A, n) \
rep(i, n) { \
cin >> f; \
A.push_back(f); \
}
void print(vl x) {
for(ll i : x) {
cout << i << " ";
}
cout << endl;
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007 /*998244353*/;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, i, j, u, v, m;
ll codeforces = 1;
string S, T;
vector<vl> g(MAX_N);
vector<vl> A;
vl C, D;
ll co[1 << 17][17];
void input() {
cin >> n;
rep(i, n) {
cin >> x >> y >> z;
A.pb({x, y, z});
}
}
void solve() {
rep(i, 1 << n) {
if(i) {
rep(j, n) { co[i][j] = INF; }
}
}
co[1][0] = 0;
k = 1 << n;
rep(i, k) {
C.clear();
D.clear();
rep(j, n) {
if(i & (1 << j)) {
C.pb(j);
} else {
D.pb(j);
}
}
for(ll x : C) {
for(ll y : D) {
co[i + (1 << y)][y] =
min(co[i + (1 << y)][y],
co[i][x] + abs(A[x][0] - A[y][0]) + abs(A[x][1] - A[y][1]) + max(0ll, A[y][2] - A[x][2]));
}
}
}
ans = INF;
rep(i, n) {
if(i)
ans =
min(ans, co[k - 1][i] + abs(A[0][0] - A[i][0]) + abs(A[0][1] - A[i][1]) + max(0ll, A[0][2] - A[i][2]));
}
print(ans);
}
int main() {
// cout<<fixed<<setprecision(15);
cin.tie(0);
ios::sync_with_stdio(false);
input();
while(codeforces--) {
solve();
}
} |
/*input
3 4 3
1 9
5 3
7 8
1 8 6 9
4 4
1 4
1 3
*/
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) is(65-90) and 32 for space
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long int
#define pb push_back
#define pii pair<ll ,ll >
#define vpii vector< pii >
#define vi vector<ll >
#define vs vector< string >
#define vvi vector< vector< ll > >
#define inf (ll)1e18
#define all(it,a) for(auto it=(a).begin();it!=(a).end();it++)
#define F first
#define S second
#define sz(x) (ll )x.size()
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define mp make_pair
#define whatis(x) cout << #x << " is " << x << "\n";
#define graph(n) adj(n,vector< ll > () )
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define debug(x) cout << #x << " " << x << endl;
#define debug_p(x) cout << #x << " " << x.F << " " << x.S << endl;
#define debug_v(x) {cout << #x << " "; for (auto ioi : x) cout << ioi << " "; cout << endl;}
#define debug_vp(x) {cout << #x << " "; for (auto ioi : x) cout << '[' << ioi.F << " " << ioi.S << ']'; cout << endl;}
#define debug_v_v(x) {cout << #x << "/*\n"; for (auto ioi : x) { for (auto ioi2 : ioi) cout << ioi2 << " "; cout << '\n';} cout << "*/" << #x << endl;}
bool cmp(pii p1,pii p2){
if(p2.S==p1.S) return p1.F < p2.F;
else return p1.S > p2.S;
}
int solve()
{
ll N,M,Q; cin>>N>>M>>Q;
vector<pii> a(N);
vi X(M);
rep(i,0,N) cin>>a[i].F>>a[i].S;
rep(i,0,M) cin>>X[i];
sort(a.begin(), a.end(),cmp);
rep(q,0,Q){
ll L,R;cin>>L>>R;
L--,R--;
multiset<ll> curX;
for(ll i=0; i<L; i++) curX.insert(X[i]);
for(ll i=R+1; i<M; i++) curX.insert(X[i]);
ll ans = 0;
for(ll i=0; i<N; i++){
auto it = curX.lower_bound(a[i].F);
if(it != curX.end()) {
ans += a[i].S;
curX.erase(it);
}
}
cout<<ans<<"\n";
}
return 0;
}
int main()
{
auto start = chrono::high_resolution_clock::now();
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll test_cases=1;
//cin>>test_cases;
while(test_cases--)
solve();
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop-start);
//cout<<"\nduration: "<<(double)duration.count()<<" milliseconds";
} | #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PI;
const int maxm=2e6+5;
PI e[maxm];
PI ee[maxm];
int n,m,q;
int main(){
cin>>n>>m>>q;
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
e[i]={x,y};
}
for(int i=1;i<=m;i++){
int x;
cin>>x;
ee[i]={x,i};
}
sort(e+1,e+1+n);
sort(ee+1,ee+1+m);
while(q--){
int l,r;
cin>>l>>r;
priority_queue<int,vector<int>,less<int> >q;
int ans=0;
int j=1;
for(int i=1;i<=m;i++){
if(ee[i].second>=l&&ee[i].second<=r)continue;
while(j<=n&&e[j].first<=ee[i].first){
q.push(e[j].second);
j++;
}
if(q.size()){
ans+=q.top();
q.pop();
}
}
cout<<ans<<endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
const int N = 5e6;
int binpow[N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int h, w;
cin >> h >> w;
vector<string> s(h);
for (int i = 0; i < h; i++)
cin >> s[i];
binpow[0] = 1;
for (int i = 1; i <= h * w; i++) {
binpow[i] = (binpow[i - 1] * 2) % mod;
}
vector<vector<pair<int, int> > > col(h, vector<pair<int, int>>(w, make_pair(-1, h))),
row(h, vector<pair<int, int>>(w, make_pair(-1, w)));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (s[i][j] == '#') {
row[i][j].first = j;
} else if (j > 0) {
row[i][j].first = row[i][j - 1].first;
}
}
for (int j = w - 1; j >= 0; j--) {
if (s[i][j] == '#') {
row[i][j].second = j;
} else if (j < w - 1) {
row[i][j].second = row[i][j + 1].second;
}
}
}
int k = 0;
for (int j = 0; j < w; j++) {
for (int i = 0; i < h; i++) {
k += s[i][j] == '.';
if (s[i][j] == '#') {
col[i][j].first = i;
} else if (i > 0) {
col[i][j].first = col[i - 1][j].first;
}
}
for (int i = h - 1; i >= 0; i--) {
if (s[i][j] == '#') {
col[i][j].second = i;
} else if (i < h - 1) {
col[i][j].second = col[i + 1][j].second;
}
}
}
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (s[i][j] == '#')
continue;
int p = col[i][j].second - col[i][j].first + row[i][j].second - row[i][j].first - 3;
ans = (ans + (binpow[k] - binpow[k - p] + mod) % mod) % mod;
}
}
cout << ans << '\n';
}
| //~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define shandom_ruffle random_shuffle
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int n, L;
cin >> n >> L;
map<int, vector<pii>> move_intvs;
vi ai(n), bi(n);
for (auto &x : ai) { cin >> x; }
for (auto &x : bi) { cin >> x; }
vector<bool> found(n);
map<int, int> pos_start;
for (int i = 0; i < n; ++i) {
pos_start[i] = ai[i];
}
pos_start[-1] = 0;
pos_start[n] = L + 1;
map<int, int> M;
M[L + 1 - n] = n;
for (int i = 0; i < n; ++i) {
found[i] = (ai[i] == bi[i]);
}
for (int i = n - 1; i >= 0; --i) {
const int seek = bi[i] - i;
debug() << imie(seek);
auto iter = M.find(seek);
if (iter == M.end()) { M[ai[i] - i] = i; continue; }
found[i] = true;
const int who = iter->nd;
debug() << imie(i) << imie(who) << imie("R");
if (pos_start[who] - pos_start[i] > 1) {
move_intvs[who].emplace_back(i, who - 1);
}
M[ai[i] - i] = i;
}
M.clear();
M[1] = -1;
for (int i = 0; i < n; ++i) {
const int seek = bi[i] - i;
auto iter = M.find(seek);
if (iter == M.end()) { M[ai[i] - i] = i; continue; }
found[i] = true;
const int who = iter->nd;
debug() << imie(i) << imie(who) << imie("L");
if (abs(pos_start[who] - pos_start[i]) > 1) {
move_intvs[who].emplace_back(who + 1, i);
}
M[ai[i] - i] = i;
}
debug() << imie(found);
if (count(ALL(found), false)) {
cout << "-1\n";
return 0;
}
ll ans = 0;
for (auto &info : move_intvs) {
debug() << imie(info.second);
const int x = info.first;
int L = x, R = x;
for (auto &intv : info.second) {
if (intv.second == x - 1) {
L = min(L, intv.first);
} else if (intv.first == x + 1) {
R = max(R, intv.second);
} else {
assert(false);
}
}
ans += R - L;
}
cout << ans << "\n";
}
|
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#define rg register
template<typename T>void read(T &x){
x=0;rg int 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();
}
x*=fh;
}
const int maxn=1e6+5;
int n;
double f[maxn];
int main(){
read(n);
rg double tmp;
for(rg int i=n-1;i>=1;i--){
tmp=1.0-1.0*i/n;
f[i]=(f[i+1]*tmp+1.0)/tmp;
}
printf("%.10f\n",f[1]);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1e9 + 7;
void solve() {
int ar[] = {0, 6, 5, 4, 3, 2, 1};
int a, b, c;
cin >> a >> b >> c;
cout << ar[a] + ar[b] + ar[c];
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(12);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n, k;
cin >> n >> k;
ll sum = 0;
for (ll i = 1; i <= n; i++)
{
for (ll j = 1; j <= k; j++)
{
sum += i * 100 + j;
}
}
cout << sum;
} | #include <bits/stdc++.h>
//#include <chrono>
#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);
template<class T> bool chmin(T &a, const T b){
if(a > b) {a = b; return true;}
else return false;
}
template<class T> bool chmax(T &a, const T b){
if(a < b) {a = b; return true;}
else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
if(!v.empty()){
for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
std::cout<<v.back();
}
if(endline) std::cout<<std::endl;
}
vec prime;
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin>>A>>B;
N = B - A + 1;
reps(i, 2, 72){
bool pr = true;
reps(j, 2, i) if(i%j == 0) pr = false;
if(pr) prime.push_back(i);
}
vector<int> pelem(N, 0);
rep(i, 20){
ll p = prime[i];
rep(j, N){
pelem[j] |= (((A + j)%p == 0) ? 1 : 0)<<i;
}
}
vec dp(1<<20, 0), ndp(1<<20, 0);
dp[0] = 1;
rep(i, N){
int a_s = pelem[i];
//cout<<bitset<20>(a_s)<<endl;
rep(s, 1<<20){
if((s&a_s) == 0){
ndp[s | a_s] += dp[s];
}
}
rep(s, 1<<20) {
dp[s] += ndp[s];
ndp[s] = 0;
}
}
cout<<accumulate(ALL(dp), 0)<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pint pair<int, int>
#define pll pair<ll, ll>
const ll INF = 1LL << 60;
// const int MOD = 1000000007;
const ll MOD = 998244353;
ll dp[5100][5100];
char field[5100][5100];
ll modpow(ll m, ll n)
{
if (n == 1)
return m;
if (n == 0)
return 1;
ll res;
ll t = modpow(m, n / 2);
ll tmp;
if (n % 2 == 0)
{
tmp = (t * t) % MOD;
}
else
{
tmp = (((t * t) % MOD) * m) % MOD;
}
return tmp;
}
ll pow(ll a, ll n)
{
ll ret = 1;
for (; n > 0; n >>= 1, a = a * a % MOD)
{
if (n % 2 == 1)
{
ret = ret * a % MOD;
}
}
return ret;
}
ll inv(ll a)
{
return pow(a, MOD - 2);
}
int main()
{
int h, w, k;
cin >> h >> w >> k;
for (int i = 0; i < k; i++)
{
int x, y;
char c;
cin >> y >> x >> c;
x--;
y--;
field[y][x] = c;
}
dp[0][0] = modpow(3, h * w - k);
ll inv3 = inv(3);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
char c = field[y][x];
if (c == 'X')
{
dp[y][x + 1] += dp[y][x];
dp[y + 1][x] += dp[y][x];
}
else if (c == 'R')
{
dp[y][x + 1] += dp[y][x];
}
else if (c == 'D')
{
dp[y + 1][x] += dp[y][x];
}
else
{
dp[y][x + 1] += dp[y][x] * 2 * inv3;
dp[y + 1][x] += dp[y][x] * 2 * inv3;
}
dp[y][x + 1] %= MOD;
dp[y + 1][x] %= MOD;
}
}
cout << dp[h - 1][w - 1] << endl;
}
| #include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9;
int MOD = 1e9 + 7;
const ll LINF = 1e18;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
return 0 <= x && 0 <= y && x < n && y < m;
}
template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }
inline int mul (int a, int b, int m = MOD) {
return ((ll)a * b) % m;
}
inline int binpow (int a, int n, int m = MOD) {
int ans = 1;
for (; n; n >>= 1) {
if (n & 1) ans = mul(ans, a, m);
a = mul(a, a, m);
}
return ans;
}
inline void add (int &a, int x, int m = MOD) {
a += x;
if (a >= m) a -= m;
if (a < 0) a += m;
}
inline int sum (int a, int b, int m = MOD) {
a += b;
if (a >= m) a -= m;
if (a < 0) a += m;
return a;
}
inline int inv (int x, int m = MOD) {
return binpow(x, m - 2, m); /// only if m is prime
}
vector <int> f, fi;
inline void precalc_f (int n, int m = MOD) {
f.resize(n);
fi.resize(n);
f[0] = 1;
for (int i = 1; i < n; i++) {
f[i] = mul(f[i - 1], i, m);
}
fi[n - 1] = inv(f[n - 1], m);
for (int i = n - 2; i >= 0; i--) {
fi[i] = mul(fi[i + 1], i + 1, m);
}
}
inline int A (int n, int k, int m = MOD) {
return mul(f[n], fi[n - k], m);
}
inline int C (int n, int k, int m = MOD) {
return mul(A(n, k), fi[k], m);
}
const int N = 101;
int dp[N][N * N * (N + 1) / 2] = {};
main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k >> MOD;
dp[0][0] = 1;
for (int x = 1; x <= n; x++) {
vector <int> S(x);
for (int s = 0, y = 0; s <= k * n * (n + 1) / 2; s++) {
add(S[y], dp[x - 1][s]);
if ((k + 1) * x <= s) {
add(S[y], -dp[x - 1][s - (k + 1) * x]);
}
dp[x][s] = S[y];
add(y, 1, x);
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 0; j <= k * n * (n + 1) / 2; j++) {
// cout << dp[i][j] << ' ';
// }
// cout << '\n';
// }
for (int x = 1; x <= n; x++) {
int ans = 0;
for (int s = 1; s <= k * n * (n + 1) / 2; s++) {
add(ans, mul(dp[x - 1][s], dp[n - x][s]));
}
ans = mul(ans, k + 1);
add(ans, k);
cout << ans << '\n';
}
}
|
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
int N, K, M;
cin >> N >> K >> M;
int sumA = 0;
for(int i = 0; i < N-1; ++i){
int tmp ;
cin >> tmp;
sumA += tmp;
}
for(int i = 0; i <= K; ++i){
if((sumA + i) / N >= M){
cout << i << endl;
return 0;
}
}
cout << "-1" << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for(int (i)=0; (i)<(n); (i)++)
#define REP2(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1,) (__VA_ARGS__)
#define MOD 1000000007
template <class T> ostream& operator<<(ostream& os, const vector<T>& v){ os << "{"; for(size_t i=0; i<v.size(); i++) os << v[i] << (i+1==v.size() ? "" : ", "); os << "}"; return os; }
template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ return os << "{" << p.first << ", " << p.second << "}"; }
int main(){
int N, K, M;
cin >> N >> K >> M;
vector<int> A(N-1);
REP(i, N-1) cin >> A[i];
int sm = 0;
REP(i, N-1) sm += A[i];
int rem = max(0, M*N - sm);
if(rem > K) cout << -1 << endl;
else cout << rem << endl;
return 0;
} |
//BISMILLAHIR RAHMANIR RAHIM
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a, b) (memset(a, b, sizeof(a)))
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define PI acos(-1)
#define INF 2147483647
#define MOD 1000000007
#define MAX 200005
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ii, int> pii;
typedef vector<ii> vii;
typedef priority_queue<int,vector<int>,greater<int> > PQ;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int setBit(int mask, int pos){return mask = mask | (1<<pos);}
bool checkBit(int mask, int pos){return (bool)(mask & (1<<pos));}
ll arr[45];
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
ll t;
cin>>n>>t;
for(int i = 0; i < n; i++)
cin>>arr[i];
int k = n / 2;
set<ll> s1;
ordered_set s2;
for(int i = 0; i < (1<<k); i++) {
ll sum = 0;
for(int j = 0; j < k; j++) {
if(checkBit(i, j))
sum += arr[j];
}
s1.insert(sum);
}
s1.insert(0);
int rem = n - k;
for(int i = 0; i < (1<<rem); i++) {
ll sum = 0;
for(int j = 0; j < rem; j++) {
if(checkBit(i, j))
sum += arr[k+j];
}
s2.insert(sum);
}
s2.insert(0);
ll ans = 0;
for(set<ll> :: iterator it = s1.begin(); it != s1.end(); it++) {
ll tx = *it;
if(tx <= t) {
ll rm = t - tx;
int pos = s2.order_of_key(rm);
if(s2.find_by_order(pos) != s2.end()) {
if(*s2.find_by_order(pos) == rm) {
ans = max(ans, tx + rm);
}
}
if(pos > 0) {
pos--;
ans = max(ans, tx + *s2.find_by_order(pos));
}
}
else {
break;
}
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define ll long long
ll fact(int a, int b){
ll res = 1;
rep(i, b) res *= a;
return res;
}
int main(){
double k; cin >> k;
string s, t;
cin >> s;
cin >> t;
//二人のスコア
ll tscore = 0;
ll ascore = 0;
//枚数の情報
vector<ll> ta(10, 0);
vector<ll> ao(10, 0);
vector<ll> used(10, 0);//二人の情報の合計
rep(i, 4){
ta[s[i] - '0']++;
used[s[i] - '0']++;
ao[t[i] - '0']++;
used[t[i] - '0']++;
}
//現在の得点
rep(i, 10){
tscore += i * fact(10, ta[i]);
ascore += i * fact(10, ao[i]);
}
double win = 0.0;
double sum = 9.0*k - 8.0;//残りカード枚数
for(int i = 1; i <= 9; i++){
ll tak = tscore;
if(used[i] == k) continue;//1枚も残っていない
int x = k - used[i];//iの残り枚数
used[i]++;
tak -= i * fact(10, ta[i]);
tak += i * fact(10, ta[i] + 1);
for(int j = 1; j <= 9; j++){
if(used[j] == k) continue;
int y = k - used[j];
used[j]++;
ll aok = ascore;
aok -= j * fact(10, ao[j]);
aok += j * fact(10, ao[j] + 1);
if(tak > aok) {
win += (double)(x) / (double) sum * (double)(y);
//cout << tak << ", " << aok << " (i, j) = " << i << " " << j << endl;
}
//sum++;
used[j]--;
}
used[i]--;
}
//cout << win << endl;
//cout << sum << endl;
cout << fixed << setprecision(15) << (double)(win) / (double)(sum-1.0) << endl;
return 0;
} |
//================code===================//
//#define TLE
#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>
#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<int,int>
#define micro 0.000001
using namespace std;
LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }
#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#else
#define AT
#define AE
#define ce(t)
#endif
pair <int, int> vu[9] = { {0,1},{1,0},{0,1} ,{-1,0},{1,1},{1,-1}, {-1,1} , {-1,-1},{-1,-1} }; //RDLU EWSN
template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };
const int max_v = 2e5 + 7;
const int max_k = 5e2 + 7;
const int bsz = (1ll << 10) + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = 1e9 + 7;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; }
vector<int> adj[max_v];
vector<int> edge[max_v];
int t=-1;
setl in[max_v];
void dfs(int u,int d=0)
{
in[u].first = ++t;
edge[d].push_back(in[u].first);
for (auto& v : adj[u]) dfs(v,d+1);
in[u].second = t;
}
int main()
{
#ifdef OHSOLUTION
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int v; ci(v);
fa(i, 1, v)
{
int x; ci(x); --x;
adj[x].push_back(i);
}
dfs(0);
fa(i, 0, v) sort(edge[i].begin(), edge[i].end());
int q; ci(q);
while (q--)
{
int x, y; ci(x >> y); --x;
int ret = upper_bound(edge[y].begin(), edge[y].end(), in[x].second) - lower_bound(edge[y].begin(), edge[y].end(), in[x].first);
co(ret << "\n");
}
return 0;
}
| //#include <bits/stdc++.h>
//using namespace std;
#include <cstdio>
#include <vector>
using namespace std;
#define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)
//#define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
//template <typename T> void Out(T x) {cerr<<x<<"\n";}
//template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}
template <class T> inline T read(const T sample) {
T x=0; int f=1; char s;
while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
return x*f;
}
template <class T> inline void write(const T x) {
if(x<0) return (void) (putchar('-'),write(-x));
if(x>9) write(x/10);
putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}
const int maxn=2e5+5;
int n,siz[maxn],dep[maxn],idx,id[maxn];
vector <int> g[maxn],t[maxn];
void dfs(int u) {
siz[u]=1; id[u]=++idx;
// printf("co %d %d\n",u,id[u]);
t[dep[u]].push_back(id[u]);
for(int i=0;i<g[u].size();++i) {
int v=g[u][i]; dep[v]=dep[u]+1;
dfs(v);
siz[u]+=siz[v];
}
}
int cal(int op,int L,int R) {
int l=0,r=t[op].size()-1,mid,ansl=-1,ansr=-1;
// rep(i,l,r) print(t[op][i],'\n');
// printf("HHH %d %d\n",L,R);
while(l<=r) {
mid=l+r>>1;
if(t[op][mid]>=L) ansl=mid,r=mid-1;
else l=mid+1;
}
l=0,r=t[op].size()-1;
while(l<=r) {
mid=l+r>>1;
if(t[op][mid]<=R) ansr=mid,l=mid+1;
else r=mid-1;
}
if(ansl==-1 || ansr==-1) return 0;
return ansr-ansl+1;
}
int main() {
n=read(9);
rep(i,2,n) g[read(9)].push_back(i);
dfs(1);
rep(q,1,read(9)) {
int x=read(9),y=read(9);
print(cal(y,id[x],id[x]+siz[x]-1),'\n');
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define MOD 1000000007
int main(void) {
string s;
cin >> s;
int num[10]; rep(i,10) num[i]=0;
int ans[111][10];
for(int i=0;i<111;i++) {
for(int j=0;j<10;j++)
ans[i][j]=0;
}
int e=112,tmp;
for(int i=0; i<112; i++) {
if(e>=1000)break;
tmp=e;
ans[i][tmp/100]++;
ans[i][(tmp-tmp/100*100)/10]++;
ans[i][tmp%10]++;
e+=8;
}
if(s.length()==1) {
if(s[0]-'0'==8) cout << "Yes" << endl;
else cout << "No" << endl;
} else if(s.length()==2) {
int ss[2]; ss[0]=(int)(s[0]-'0'); ss[1]=(int)(s[1]-'0');
if((ss[0]*10+ss[0])%8==0) cout << "Yes" << endl;
else if((ss[0]*10+ss[1])%8==0) cout << "Yes" << endl;
else cout << "No" << endl;
} else {
for (int i = 0; i < s.length(); i++) {
num[(int)(s[i]-'0')]++;
}
//for(int i=0;i<10;i++) cout << num[i] << endl;
//104 112 120 128 136 144 152 160 ...992
//(992-104)/8+1=112
int key=1;
int i,j;
for( i =0;i<111;i++) {
key=1;
for( j=0; j<10; j++) {
if(ans[i][j]>num[j]) {key=-1;}
}
if(key==1) {cout << "Yes" << endl;
//cout << "i==" << i << endl;
break;}
}
if(key!=1){cout << "No" << endl;}
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, j, k) for(int i = j; i < k; i++)
#define print(x) cout << x
#define newline cout << endl
#define space cout << ' '
#define INF 1000000007
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
int main(){
string s;
cin >> s;
vl v(10,0);
rep(i,s.size()){
ll tmp=s[i]-'0';
v[tmp]++;
}
ll ev=v[2]+v[4]+v[6]+v[8];
ll od=v[1]+v[3]+v[5]+v[7]+v[9];
if(s.size()==1){
if(v[8]){
print("Yes");
return 0;
}
}
else if(s.size()==2){
if(v[2]>0&&(v[3]+v[4]+v[7]>0)){
print("Yes");
return 0;
}
if(v[4]>0&&(v[2]+v[6]+v[8]>0)){
print("Yes");
return 0;
}
if(v[6]>0&&(v[1]+v[4]+v[5]+v[9]>0)){
print("Yes");
return 0;
}
if(v[8]==2){
print("Yes");
return 0;
}
}
else{
if(v[2]>0&&(v[3]+v[7]>0)&&(ev>1)){
print("Yes");
return 0;
}
if(v[2]>0&&(v[4]>0)&&(ev>2)){
print("Yes");
return 0;
}
if(v[4]>0&&(v[2]+v[6]+v[8]>0)&&(ev>2)){
print("Yes");
return 0;
}
if(v[6]>0&&(v[1]+v[5]+v[9]>0)&&(ev>1)){
print("Yes");
return 0;
}
if(v[8]>1&&(ev>2)){
print("Yes");
return 0;
}
if(v[2]>0&&(v[1]+v[5]+v[9]>0)&&(od>1)){
print("Yes");
return 0;
}
if(v[2]>0&&v[8]>0&&ev>0){
print("Yes");
return 0;
}
if(v[4]>1&&od>0){
print("Yes");
return 0;
}
if(v[8]>0&&(v[4]+v[6]>0)&&od>0){
print("Yes");
return 0;
}
if(v[6]>0&&v[3]+v[7]>0&&od>1){
print("Yes");
return 0;
}
}
print("No");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
long long n;
cin >> n;
double N = sqrt(n);
int count = 0;
/*vector<long long> vec(n, 2000000000000);*/
priority_queue<long long, vector<long long>, greater<long long>> Q1;
for (int i = 1; i <= N; i++){
if (n % i == 0 && i != n /i){
Q1.push(i);
Q1.push(n / i);
/*vec[i] = i;
vec[n / i] = n / i;*/
count += 2;
}
if (n % i == 0 && i == n /i){
Q1.push(i);
count++;
}
}
/*sort(vec.begin(), vec.end());*/
/*for(int i = 0; i < count; i++){*/
while(count > 0){
cout << Q1.top() << endl;
Q1.pop();
count--;
}
} | #include <bits/stdc++.h>
using namespace std;
// clang-format off
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353
using ll = int64_t;
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto& a : v) cin >> a;
return is;
}
template <class T>
istream& operator>>(istream& is, vector<pair<T, T>>& v) {
for (auto& a : v) cin >> a.first >> a.second;
return is;
}
template <class T>
istream& operator>>(istream& is, vector<tuple<T, T, T>>& v) {
for (auto& a : v) {
T a1, a2, a3;
cin >> a1 >> a2 >> a3;
a = {a1, a2, a3};
}
return is;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
for (auto& a : v) os << a << " ";
os << endl;
return os;
}
template<class T>
bool chmin(T &a, T b) {
if (a > b) { a = b; return true; }
return false;
}
template<class T>
bool chmax(T &a, T b) {
if (a < b) { a = b; return true; }
return false;
}
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define TRC1(a) cout << #a ":" << a
#define TRC2(a, b) TRC1(a); cout << " "; TRC1(b)
#define TRC3(a, b, c) TRC2(a, b); cout << " "; TRC1(c)
#define TRC4(a, b, c, d) TRC3(a, b, c); cout << " "; TRC1(d)
#define GET_NAME(_1,_2,_3,_4,NAME,...) NAME
#define TRC(...) GET_NAME(__VA_ARGS__, TRC4, TRC3, TRC2, TRC1) (__VA_ARGS__) << endl
using veci = vector<int>;
using vecll = vector<ll>;
using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
const int IINF = INT32_MAX;
const ll LINF = INT64_MAX;
// cout << fixed << setprecision(15);
void solve();
int main() {
cin.tie(0); ios::sync_with_stdio(0);
cout << fixed << setprecision(15); solve(); return 0; }
// clang-format on
void calc(int n) {
int i = 1;
veci a;
while (n > 0) {
if (n & 1) a.push_back(i);
++i;
n >>= 1;
}
cout << a.size() << " ";
for (int x : a) {
cout << x << " ";
}
cout << endl;
}
void solve() {
int N;
cin >> N;
veci A(N);
cin >> A;
N = min(N, 8);
veci s(200);
rep(i, 1, 1 << N) {
int sum = 0;
rep(j, N) if (i >> j & 1) sum = (sum + A[j]) % 200;
if (s[sum] == 0) s[sum] = i;
else {
cout << "Yes" << endl;
calc(s[sum]);
calc(i);
return;
}
}
cout << "No" << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
long long int power(long long int x,long long int y,long long int p)
{
long long int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
long long int gcdExtended(long long int a,long long int b, long long int *x,long long int *y)
{
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
long long int x1, y1;
long long int gcd = gcdExtended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
long long int modInverse( long long int b,long long int m)
{
long long int x, y;
long long int g = gcdExtended(b, m, &x, &y);
if (g != 1)
return -1;
return (x%m + m) % m;
}
long long modDivide(long long int a,long long int b,long long int m)
{
a = power(10,a,m);
long long int inv = modInverse(b, m);
return (inv*a)%m;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int mod=1000000007;
int t=1;
// cin>>t;
while(t--){
int n;
cin>>n;
vector<int> arr(n);
for(int i=0;i<n;i++){
cin>>arr[i];
}
int sum=0;
for(int i=0;i<n;i++){
int b;
cin>>b;
arr[i]*=b;
sum+=arr[i];
}
if(sum==0){
cout<<"Yes\n";
}
else
cout<<"No\n";
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define I inline int
#define V inline void
#define ll long long int
#define isnum(ch) ('0'<=ch&&ch<='9')
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
#define REP(u) for(int i=h[u],v;v=e[i].t,i;i=e[i].n)
#define gc (_op==_ed&&(_ed=(_op=_buf)+fread(_buf,1,100000,stdin),_op==_ed)?EOF:*_op++)
char _buf[100000],*_op(_buf),*_ed(_buf);
I getint(){
int _s=0,_f=1;char _ch=gc;
while(!isnum(_ch))(_ch=='-')&&(_f=-1),_ch=gc;
while(isnum(_ch))_s=_s*10+_ch-48,_ch=gc;
return _s*_f;
}
const int N=2e5+1,mod=998244353;
V check(int&x){x-=mod,x+=x>>31&mod;}
V cmax(int&x,int y){if(x-y>>31)x=y;}
V cmin(int&x,int y){if(y-x>>31)x=y;}
ll n,m;
V input(){
cin>>n;
m=sqrt(n);
FOR(i,1,m)if(n%i==0)
cout<<i<<'\n';
if(n/m==m)m--;
ROF(i,m,1)if(n%i==0)
cout<<n/i<<'\n';
}
V init(){
}
V work(){
}
int main(){
// for(scanf("%d",&T);T--;){
input();
init();
work();
// }
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define ll long long
#define F(type, i, a, b, incr) for (type i = a; i <= (type)(b); i += (type)(incr))
#define RF(type, i, a, b, decr) for (type i = a; i >= (type)(b); i -= (type)(decr))
#define sz(a) sizeof(a)
#define deb(a) cerr << " [" << #a << "->" << a << "] "
#define next_line cerr << '\n'
#define all(a) a.begin(), a.end()
#define iter(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define setbits(x) __builtin_popcountll(x)
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
void solve()
{
int k, n, m;
cin >> k >> n >> m;
vector<int> A(k);
for(int &x: A)
cin >> x;
vector<int> Aux(k);
int s = 0;
vector<pii> Ps_B(k);
F(int, i, 0, k - 1, 1){
s += ((A[i] * m) % n);
Ps_B[i].first = ((A[i]*m) % n);
Ps_B[i].second = i;
}
s /= n;
sort(all(Ps_B));
RF(int, i, s, 1, 1){
Ps_B[k - i].first -= n;
}
vector<int> fin_ans(k);
F(int,i, 0, k - 1, 1){
fin_ans[Ps_B[i].second] = ((A[Ps_B[i].second] * m) - Ps_B[i].first) / n;
}
for(int x: fin_ans){
cout << x << ' ';
}
cout << '\n';
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// freopen("Debug.txt", "w", stderr);
#else
#endif
// cout << fixed << setprecision(10);
// int _t;
// cin>>_t;
// F(int, i, 1, _t, 1){
// cout << "Case #" << i << ": ";
solve();
// }
}
| #include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 998244353LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vvvi = vector<vvi>; // intの2次元の型に vvi という別名をつける
const ll llMAX=9223372036854775807LL;
const ll llMIN=-9223372036854775808LL;
void myprint1D(vi &data)
{
REP(i,data.size())
cout<<data[i]<<" ";
cout<<endl;
}
//配列を[y][x]で表示
void myprint2D_T(vvi &data)
{
REP(i,data.size())
myprint1D(data[i]);
}
//配列を[x][y]で表示
void myprint2D(vvi &data)
{
ll l1=data.size();
ll l2=data[0].size();
REP(j,l2){
REP(i,l1)
cout<<data[i][j]<<" ";
cout<<endl;
}
}
//print(a,b...)って使い方
void print1(ll a){cout<<a<<endl;}
void print2(ll a,ll b){cout<<a<<" "<<b<<endl;}
void print3(ll a,ll b,ll c){cout<<a<<" "<<b<<" "<<c<<endl;}
void print4(ll a,ll b,ll c,ll d){cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;}
void print5(ll a,ll b,ll c,ll d,ll e){cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;}
//moddivとnCkとgarner
//c剰余下でaをbで割った値
ll moddiv(ll a,ll b,ll c){
ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
if (t==0LL) return a/b;
for(int i=0;i<900;i++){
m=x0/x1;
x2=x0-x1*m;
n2=(n0-m*n1)%c;
if (x2==1LL){
ans=(n2+c)%c;
break;
}
x0=x1;x1=x2;
n0=n1;n1=n2;
}
return (a+((t*ans)%c)*b-t)/b;
}
//剰余の割ざん
//aのn乗 mod c
ll expmod(ll a,ll n,ll c){
ll ans=1LL,aa=a,beki=n;
for(int i=0;i<64;i++){
if (beki%2==1LL) ans=ans*aa%c;
aa=aa*aa%c;
beki/=2LL;
if (beki==0LL)break;
}
return ans;
}
//nCk
vi mulmod(500001);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
mulmod[0]=1LL;
for(ll i=1;i<500001;i++){
mulmod[i]=mulmod[i-1]*i%MOD;
}
}
ll nCk(ll nn,ll kk){
return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD);
}
ll modinv(ll a,ll b){
return moddiv(1,a,b);
}
ll garner(__int128_t a,__int128_t b,__int128_t ar,__int128_t br){
__int128_t tmp=(br-ar)%b*modinv(a,b)%b;
if (tmp<0)tmp+=b;
return ar+tmp*a;
}
ll a_,b_,c_;
int main(){
ll ans=0;
cin >> a_>>b_>>c_;
/*
a_=moddiv(a_*(a_+1LL)%MOD,2LL,MOD);
b_=moddiv(b_*(b_+1LL)%MOD,2LL,MOD);
c_=moddiv(c_*(c_+1LL)%MOD,2LL,MOD);
*/
a_=(a_*(a_+1)/2)%MOD;
b_=(b_*(b_+1)/2)%MOD;
c_=(c_*(c_+1)/2)%MOD;
ans=(((a_*b_)%MOD)*c_)%MOD;
cout<<ans<<endl;
return 0;
} |
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
constexpr ll MOD = 998244353;
#ifndef ONLINE_JUDGE
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " (" << x.first << " : " << x.second << ")" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "["; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "]"; return o;}
ostream &operator<<(ostream &o, const string &obj) {o << "\""; o << obj.c_str(); o << "\""; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "(" << obj.first << ", " << obj.second << ")"; return o;}
template <template <class tmp> class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print_sim_py(void) {cout << endl;}
template <class Head> void print_sim_py(Head&& head) {cout << head;print_sim_py();}
template <class Head, class... Tail> void print_sim_py(Head&& head, Tail&&... tail) {cout << head << " ";print_sim_py(forward<Tail>(tail)...);}
#define print(...) print_sim_py(__VA_ARGS__);
#else
#define print(...);
#endif
template <typename... Ts>
std::istream& IN(Ts&... xs){ return (std::cin >> ... >> xs); }
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define Rrepr(i, a, b) for (int i = b; i >= a; i--)
#define Rrep(i, n) for (int i = n-1; i >= 0; i--)
bool dfs(ll p, vector<vector<ll>>& v,vector<ll>& fin_list, vector<ll>& cv,ll c, ll depth){
bool closed = false;
if(fin_list[p]){
if(depth >= 3) return true;
else return false;
}
cv[p] = c;
fin_list[p] = 1;
rep(i, v[p].size()){
if(v[p][i]){
closed |= dfs(i, v, fin_list, cv, c, depth+1);
}
}
return closed;
}
class Vsum2{
public:
vector<vector<ll>> vsum;
Vsum2(vector<vector<ll>>& a,
function<ll (ll&)> func=[](auto& a){ return a; }){
ll h = a.size();
ll w = a[0].size();
vsum.resize(h+1, vector<ll>(w+1, 0));
repr(i,0,h){
ll jsum = 0;
rep(j,w){
jsum += func(a[i][j]);
vsum[i+1][j+1] = jsum + vsum[i][j+1];
}
}
//print("#", vsum)
}
ll range(ll y1,ll x1, ll y2,ll x2){
return (vsum[y2][x2] - vsum[y2][x1] - vsum[y1][x2] + vsum[y1][x1]);
}
};
int main(void){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n,k;
IN(n,k);
vector<vector<ll>> a(n, vector<ll>(n, 0));
ll amax = 0;
rep(i,n){
rep(j,n){
IN(a[i][j]);
amax = max(amax, a[i][j]);
}
}
ll l = -1 ,r = 1e9;
while(l+1<r){
ll m = (l+r)/2;
bool ok = false;
Vsum2 asum(a, [&](auto& a){ return a > m; });
rep(i,n-k+1){
rep(j, n-k+1){
ll sumr = asum.range(i,j, i+k,j+k);
if(sumr < k*k/2+1){
ok = true;
}
}
}
if(ok){
r=m;
}else{
l=m;
}
}
ll ans = r;
cout << ans << endl;
return 0;
}
| #pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5+3;
#define fi first
#define se second
#define pb push_back
#define wzh(x) cerr<<#x<<'='<<x<<endl;
int n,a[N],b[N],f[N];
LL s[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
LL sum=0;
for(int i=1;i<=n;i++)cin>>a[i]>>b[i],f[i]=i,sum+=a[i]+b[i];
sort(f+1,f+1+n,[](int x,int y){
return 2ll*a[x]+b[x]<2ll*a[y]+b[y];
});
int tot=n;
//sum - b -2*x
for(int i=1;i<=n;i++){
if(sum-b[f[i]]-2ll*a[f[i]]>0){
sum-=b[f[i]]+2ll*a[f[i]];
tot--;
// cout<<sum<<endl;
}else break;
}
cout<<tot<<'\n';
return 0;
} |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define int long long
#define ll long long
#define all(a) a.begin(),a.end()
#define rev(a) a.rbegin(),a.rend()
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; //less_equal for multiset
#define ar array
#define pb push_back
#define fi(a,b) for(int i=a;i<(b);i++)
#define fj(a,b) for(int j=a;j<(b);j++)
#define fk(a,b) for(int k=a;k<(b);k++)
//const double pi=acosl(-1);
void solve()
{
int n;
cin>>n;
vector<pair<int,int>> a(n);
fi(0,n) cin>>a[i].first>>a[i].second;
int m;
cin>>m;
vector<pair<int,int>> op(m);
fi(0,m)
{
cin>>op[i].first;
if(op[i].first>2) cin>>op[i].second;
}
int Q;
cin>>Q;
vector<ar<int,3>> q(Q);
fi(0,Q)
{
cin>>q[i][0]>>q[i][1];
--q[i][0],--q[i][1];
q[i][2]=i;
}
sort(all(q));
vector<pair<int,int>> ans(Q);
int j=0;
while(j<Q&&q[j][0]==-1)
{
ans[q[j][2]]=a[q[j][1]];
j++;
}
int rot=0;
int flipx=0,flipy=0;
int sx=1,sy=1;
fi(0,m)
{
if(op[i].first>2)
{
if(op[i].first==3)
{
sx=-sx;
flipx=2*op[i].second-flipx;
}
else
{
sy=-sy;
flipy=2*op[i].second-flipy;
}
}
else
{
if(op[i].first==1)
{
// rot++;
swap(flipx,flipy);
swap(sx,sy);
rot=!rot;
sy=-sy;
flipy=-flipy;
}
else
{
// rot--;
swap(flipx,flipy);
swap(sx,sy);
rot=!rot;
sx=-sx;
flipx=-flipx;
// if(rot<0) rot+=4;
}
}
while(j<Q&&q[j][0]==i)
{
int x=a[q[j][1]].first;
int y=a[q[j][1]].second;
//apply
if(rot==1)
{
swap(x,y);
}
ans[q[j][2]]={x*sx+flipx,y*sy+flipy};
j++;
}
}
for(auto temp:ans)
{
cout<<temp.first<<' '<<temp.second<<'\n';
}
}
signed main()
{
FAST;
int tt=1;
//cin>>tt;
while(tt--)
{
solve();
}
}
| ///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
#define pi 2*acos(0.0)
#define scan(i) scanf("%lld",&i)
#define scann(i,j) scanf("%lld%lld",&i,&j)
#define print(i) printf("%lld\n",i)
#define printt(i,j) printf("%lld %lld\n",i,j)
#define loop(i,n) for(int i = 0 ; i <n ; i++)
#define loopp(i,n) for(int i = 0 ; i <n-1 ; i++)
#define rep(i,n) for(int i=n-1;i>=0;i--)
#define rev(v) reverse(v.begin(),v.end())
#define reset(a,b) memset(a,b,sizeof(a))
#define vect vector<ll>
#define vec_it vector<ll>::reverse_iterator vi
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<>())
#define sorted(v) is_sorted(v.begin(),v.end())
#define mapp map<ll,ll>
#define rmap map<ll,ll,greater<>>m
#define pairr pair<ll,ll>
#define sett set<ll>
#define rset set<ll,greater<>>s
#define set_it set<ll>::reverse_iterator si
#define map_it map<ll,ll> :: reverse_iterator mi
#define digit(n) floor(log10(n)+1)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define ff first
#define ss second
#define mx INT_MAX
#define lmx LLONG_MAX
#define mn INT_MIN
#define lmn LLONG_MIN
#define inf 0x3f3f3f3f
#define inff 10e18
#define GCD __gcd(a,b)
#define LCM (a/GCD)*b
template <typename T> T gcd(T a,T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}
ll modulo (ll a, ll b) { return a >= 0 ? a % b : ( b - abs ( a%b ) ) % b; }
void solve()
{
// freopen ("(input).txt" , "r", stdin) ;
string st,stt;
ll low=0,high=0;
ll l,r; char ch;
ll n,k,t,x,y,z,d=0,cnt=0,temp=0,sum=0,f=0,flag=0;
cin>>n>>k>>x>>y ; vect v ,a ; sett s ; mapp m,mm ; pairr p ;
if(n==k)
cout<<x<<endl;
else if(n>k)
{
sum+=x;d=n;
d--;
if(2*x<=y)
sum+=(d-k)*2*x;
else
sum+=(d-k)*y;
cout<<sum<<endl;
}
else
{
sum+=x;
if(2*x<=y)
sum+=(k-n)*2*x;
else
sum+=(k-n)*y;
cout<<sum<<endl;
}
}
main()
{
int t;
// cin>>t;
// while(t--)
solve();
}
|
#include <bits/stdc++.h>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
#define pq_max priority_queue<ll>
#define pq_min priority_queue<ll,vi,greater<ll>>
#define iint int
#define f(i,a,b) for(ll i=a;i<b;i++)
#define f0(i,n) f(i,0,n)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define tc(t) int t;cin >>t;while(t--)
#define lol std::ios::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
#define ss second
#define ff first
#define ll long long
#define lld long double
#define int long long
#define pii pair< int,int >
#define pll pair< ll,ll >
#define sz(a) a.size()
const long long inf = 1e18;
#define all(a) a.begin(),a.end()
#define arr(a,n) int a[n];f0(i, n) cin >> a[i];
#define ini(a,n) memset(a,n,sizeof(a))
#define printArr(a,n) f0(i,n) cout<<a[i]<<" ";
#define in cin>>
#define rr return 0;
#define vi vector< int >
#define vs vector<string>
#define l_b lower_bound
#define u_b upper_bound
#define mod 1000000007
#define pi 3.141592653589793238
#define fmap(m) for(auto it:m)
#define deb(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define debarr(arr,a,b) for(int i=(a);i<(b);i++) cout<<(arr[i])<<" ";cout<<endl;
#define long_max numeric_limits<ll>::max()
vs tokenizer(string str, char ch) {std::istringstream var((str)); vs v; string t; while (getline((var), t, (ch))) {v.pb(t);} return v;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
int dx[8] = { +1, +1, +1, 0, 0, -1, -1, -1};
int dy[8] = { +1, 0, -1, +1, -1, +1, 0, -1};
int dx4[4] = { +1 , -1 , +0 , +0};
int dy4[4] = { 0 , 0 , +1 , -1};
int dx2[2] = {0, 1};
int dy2[2] = {1, 0};
const int N = 1e4 + 10;
void solve()
{
vector<vi>ncr(61 , vi(61, 0));
ncr[0][0] = 1 ;
for (int i = 1; i < 61; i++)
{
ncr[i][0] = 1;
for (int j = 1 ; j < i; j++)
ncr[i][j] = ncr[i - 1][j - 1] + ncr[i - 1][j];
ncr[i][i] = 1;
}
int a, b, k;
cin >> a >> b >> k;
int n = a + b;
f0(i, n)
{
int cnt = ncr[a - 1 + b][b];
if (cnt >= k) //we put a
{
cout << 'a';
a--;
}
else
{
cout << 'b';
b-- ;
k -= cnt;
}
}
cout << '\n';
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
lol;
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
} | #include<bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
typedef std::vector<ll> vi;
typedef std::vector<std::vector<ll> > vv;
typedef std::vector<std::pair<ll,ll> > pii;
typedef std::pair<ll,ll> pi;
#define mod 1000000007
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fo(i,a,b) for(i=a;i<b;i++)
#define forr(i,a,b) for(i=a;i>=b;i--)
#define mp make_pair
#define pb(x) push_back(x)
#define fi first
#define se second
#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;
#define all(a) a.begin(),a.end()
#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];
#define ms(a,val) memset(a,val,sizeof(a))
using namespace std;
const int N = 1e5+ 5;
ll expo_pow(ll x, ll y) {
if (y == 0)
return 1;
y = y % (mod - 1);
x %= mod;
if (y == 0)
y = mod - 1;
ll res = 1;
while (y) {
if (y & 1)
res = (res * x) % mod;
x = (x * x) % mod;
y >>= 1;
}
return res;
}
ll modInverse(ll a, ll m = mod) {
return expo_pow(a, m - 2);
}
void solve(){
ll i,j,n,m,k;
cin>>n;
vi t(n,0);
input(t,0,n);
ll pos[N] = {0};
pos[0] = 1;
fo(j,0,n)
{
fo(i,0,N)
{
if(i + t[j] >= N)
continue;
if(pos[i] == 1 && pos[i+t[j]] == 0)
pos[i+t[j]] = 2;
}
fo(i,0,N)
{
if(i + t[j] >= N)
continue;
if(pos[i+t[j]] == 2)
pos[i+t[j]] = 1;
}
}
ll sum = 0;
fo(i,0,n)
sum+=t[i];
ll val = ceil(1.0*sum/2);
fo(i,val,N-1)
{
if(pos[i] == 1)
{
cout<<i<<endl;
return;
}
}
}
int main()
{
//IO;
ll t=1,i;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
|
#include <cstdio>
#include <algorithm>
using namespace std;
int a[200005];
int main()
{
int n, m;
scanf("%d%d", &n, &m);
a[0] = 0;
a[m + 1] = n + 1;
for (int i = 1; i <= m; i++) {
scanf("%d", &a[i]);
}
sort(a + 1, a + 1 + m);
int g = 1e9;
for (int i = 1; i <= m + 1; i++) {
if (a[i] - a[i - 1] - 1) {
g = min(g, a[i] - a[i - 1] - 1);
}
}
int ans = 0;
for (int i = 1; i <= m + 1; i++) {
if (a[i] - a[i - 1] - 1) {
ans += (a[i] - a[i - 1] - 1 + g - 1) / g;
}
}
printf("%d\n", ans);
return 0;
}
| /* How To Solve It
1st. Understanding The Problem
未知なもの、データ(与件), 条件 は何か?
その条件は未知を決定するために十分か?
条件を分解せよ, 図を描け
2nd. Devising A Plan
類似、関連のある問題、典型問題を知らないか?
役立ちそうな定理やアルゴリズムを知らないか?
問題の一部を解決できるか、一部だけ残して他を切り捨ててみろ
既知のものやデータを変えることはできるか?
3rd. Carrying Out The Plan
各ステップで誤りがないか、正しいと説明できるか?
4th. Looking Back
別解、他の問題に使えそうなものはあるか?
*/
/*{{{ #include */
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <tuple>
#include <algorithm>
#include <utility>
#include <cmath>/*}}}*/
/*{{{ namespace R2357 */
namespace R2357 {
inline void IN(void){return;}
template<class F,class... R>inline void IN(F& f,R&... r){std::cin>>f;IN(r...);}
template <class T>inline void OUT(T x){std::cout<<x<<'\n';}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T=int>T read(){
T x=0; char c;
while(((c=getchar())>'9'||c<'0')&&c!='-');
const bool f=(c=='-')&&(c=getchar());
while(x=x*10-48+c,(c=getchar())>='0'&&c<='9');
return f?-x:x;
}
template<class T>std::istream& operator>>(std::istream& is,std::vector<T>& V){
for(auto& v:V)is>>v; return is;
}
template<class T1,class T2>
std::istream& operator>>(std::istream& is,std::pair<T1,T2>& P){
return is>>P.first>>P.second;
}
template<class T>inline T ceil_div(T a,T b){
return (a+(b-1))/b;
}
namespace debug {
using namespace std;
void Write(void){cerr<<endl;}
template<class F,class...R>void Write(F f,R...r){cerr<<' '<<f;Write(r...);}
template<class T>void Write(vector<T>& V,int n=-1){
if(n==-1) n = V.size();
for(int i=0;i<n;i++)cerr<<' '<<V[i]; cerr<<endl;
}
template<class T,class U> void Write(pair<T,U>& p){cerr<<' '<<'{'<<p.first<<','<<p.second<<'}'<<endl;}
#include <cxxabi.h>
template<class T>string TypeName(T& x){
return string(abi::__cxa_demangle(typeid(T).name(), 0, 0, 0));
}
}
}
/*}}}*/
/*{{{ using */
using namespace std; using namespace R2357;
using ll=long long; using ld=long double;
using pint=pair<int,int>;/*}}}*/
/*{{{ #define */
#define rep(i,a,b) for(ll i=a;i<ll(b);i++)
#define repr(i,a,b) for(ll i=a;i>=ll(b);i--)
#define each(x,v) for(auto& x:v)
#define el '\n'
#define ALL(x) x.begin(),x.end()
#define ALLR(x) x.rbegin(),x.rend()
#define Unique(x) x.erase(unique(x.begin(),x.end()),x.end())
#define Fill(v,n) fill(v,v+sizeof(v)/sizeof(*v),n)
#define max_element(x) *max_element(x.begin(),x.end())
#define min_element(x) *min_element(x.begin(),x.end())
#define INF 1073741824 // 2e30
#define INFL 2305843009213693952ll // 2e61
#define _FASTIO_ ios::sync_with_stdio(false),cin.tie(nullptr);
/*}}}*/
/* NOTE
0.
*/
// using namespace debug;
vector<pair<char, int>> RunLength_encode(const string& s) {
vector<pair<char, int>> res;
ll len = s.size();
ll l = 0;
while(l < len){
int r = l + 1;
while(r < len && s[l] == s[r]) r++;
res.push_back({s[l], r-l});
l = r;
}
return res;
}
int main(){ _FASTIO_
int n, m; IN(n, m);
string f(n, '.');
rep(_, 0, m) f[read()-1] = '#';
auto r = RunLength_encode(f);
int k = INF;
each(x, r) if(x.first == '.') chmin(k, x.second);
ll ans = 0;
each(x, r) if(x.first == '.') ans += ceil_div(x.second, k);
OUT(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(register int (i) = (a); (i) <= (int)(b); (i)++)
#define repd(i, b, a) for(register int (i) = (b); (i) >= (int)(a); (i)--)
#define mset(a, x) memset((a), (x), sizeof(a))
#define x first
#define y second
#define int long long // int 变成 long long
#define LOCAL
typedef pair<int, int> pii;
typedef long long ll;
// 重载to_string
template <typename A, typename B>string to_string(pair<A, B> p);template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);string to_string(const string& s) { return '"' + s + '"';}string to_string(const char* s) { return to_string((string) s);}string to_string(bool b) { return (b ? "true" : "false");}string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res;}template <size_t N>string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res;}template <typename A>string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}template <typename A, typename B>string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
// 声明debug 不能打印普通数组, 自定义结构体
void debug_out() { cout << endl; }template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...);}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 404
#endif
// read --- 只能输入整数数字(int , ll)
template<typename T> void read(T &x){
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
read(first);
read(args...);
} // read end
/////////////////////////////// ABOUT ///////////////////////////
/*
*/
////////////////////////////// START CODE ///////////////////////
const int mod = 1e9 + 7;
const int maxn = 1000 + 10;
int n, m;
int a[maxn], b[maxn];
int vis[maxn];
void solve(){
read(n, m);
rep(i, 1, n){
read(a[i]);
vis[a[i]] ++;
}
rep(i, 1, m){
read(b[i]);
vis[b[i]] ++;
}
rep(i, 1, maxn){
if(vis[i] == 1){
cout << i << ' ';
}
}
}
signed main(){
#ifdef LOCAL
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// debug("LOCAL");
#endif
int T = 1;
// cin >> T;
rep(i, 1, T){
// cout << "Case #" << i << ": ";
solve();
}
return 0;
} | #include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define ndl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
//using namespace __gnu_pbds;
//template<typename T> using indexed_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());//uniform_int_distribution<int> (l, r)
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<int, ull> piu;
typedef vector<vector<int>> matrix;
const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 1003;
const int M = 1e6 + 123;
const double pi = 3.14159265359;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
char c[2][2];
int n;
void add(int &a, int b){
a += b;
if (a >= MOD) a -= MOD;
}
map<string, bool> w;
int cnt = 0;
void go(string s){
if (w[s]) return;
w[s] = 1;
if (sz(s) == min(n, 10)){
cnt++;
return;
}
for (int i = 1; i < sz(s); i++){
string t = s;
t.insert(t.begin()+i, c[s[i-1]-'A'][s[i]-'A']);
go(t);
}
}
int f[N];
void solve(){
cin >> n;
f[3] = f[2] = 1;
for (int i = 4; i <= n; i++){
f[i] = (f[i-1] + f[i-2]) % MOD;
}
//cout << n;
for (int i = 0; i < 2; i++){
for (int j = 0; j < 2; j++){
char x;
cin >> c[i][j];
}
}
go("AB");
if (n <= 10){
cout << cnt;
return;
}
if (cnt == f[10]){
cout << f[n];
return;
}
if (cnt == 1){
cout << 1;
return;
}
int ans = 1;
for (int i = 4; i <= n; i++){
ans = (ans+ans)%MOD;
}
cout << ans;
return;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++){
//cout << "Case #" << i << ": ";
solve();
}
#ifdef KAZAKH
// cout << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl;
#endif
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define fi first
#define se second
#define mset(a, b) memset(a, b, sizeof(a))
using ll = long long;
using pii = pair<int, int>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class Ty> Ty randint(Ty a, Ty b) { return uniform_int_distribution<Ty>(a, b)(rng); }
template<class T> void DBG(T&& x) { cerr << x << " "; }
template<class T, class...Args> void DBG(T&& x, Args&&... args) { DBG(x); DBG(args...); }
#define DBG(...) { cerr << "[" << #__VA_ARGS__ << "]: "; DBG(__VA_ARGS__); cerr << endl; }
template<class num> inline void rd(num& x) { cin >> x; }
template <class Ty, class... Args> inline void rd(Ty& x, Args&... args) { rd(x); rd(args...); }
template<class num> inline void print(num&& x) { cout << x; }
template <class Ty, class... Args> inline void print(Ty&& x, Args&&... args) { print(x); print(' '); print(args...); }
#define print(...) { print(__VA_ARGS__); print('\n'); }
struct Solver {
const int MAXN = 2e5 + 5, INF = 0x3f3f3f3f;
inline void run_test(int test_case) {
int n; rd(n);
vector<pair<string, bool>> a(n);
for (int i = 0; i < n; i++) {
string s; rd(s);
if (s[0] == '!') {
s.erase(s.begin(), s.begin()+1);
a[i] = {s, true};
} else {
a[i] = {s, false};
}
}
sort(a.begin(), a.end());
string ans = "satisfiable";
for (int i = 0; i + 1 < n; i++) {
if (a[i].fi == a[i+1].fi && a[i].se != a[i+1].se) ans = a[i].fi;
}
print(ans);
}
inline void prepare() { // first thing to run
ios::sync_with_stdio(false); cin.tie(nullptr);
}
};
/* **************************************************** */
// CHANGE HERE if there are multiple test cases
const enum TEST_TYPE { SINGLE, MULTIPLE, MULTIPLE_EOF } test_type = SINGLE;
inline void go_for_it() {
if (test_type == SINGLE) {
Solver solver;
solver.prepare();
solver.run_test(1);
} else if (test_type == MULTIPLE) {
Solver solver;
solver.prepare();
int test_count; rd(test_count);
for (int test_number = 1; test_number <= test_count; test_number++)
solver.run_test(test_number);
} else {
Solver solver;
solver.prepare();
for (int test_number = 1;; test_number++)
solver.run_test(test_number);
}
}
int main() {
go_for_it();
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define INF LONG_MAX/3
//#define DIV 1000000007
//#define DIV 998244353
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
struct UnionFind
{
// par[i]:データiが属する木の親の番号。i == par[i]のとき、データiは木の根ノードである
vector<int> par;
// sizes[i]:根ノードiの木に含まれるデータの数。iが根ノードでない場合は無意味な値となる
vector<int> sizes;
UnionFind(int n) : par(n), sizes(n, 1) {
// 最初は全てのデータiがグループiに存在するものとして初期化
for(int i = 0; i < n; i++){
par[i] = i;
}
}
// データxが属する木の根を得る
int find(int x) {
if (x == par[x]) return x;
return par[x] = find(par[x]); // 根を張り替えながら再帰的に根ノードを探す
}
// 2つのデータx, yが属する木をマージする
void unite(int x, int y) {
// データの根ノードを得る
x = find(x);
y = find(y);
// 既に同じ木に属しているならマージしない
if (x == y) return;
// xの木がyの木より大きくなるようにする
if (sizes[x] < sizes[y]) swap(x, y);
// xがyの親になるように連結する
par[y] = x;
sizes[x] += sizes[y];
// sizes[y] = 0; // sizes[y]は無意味な値となるので0を入れておいてもよい
}
// 2つのデータx, yが属する木が同じならtrueを返す
bool same(int x, int y) {
return find(x) == find(y);
}
// データxが含まれる木の大きさを返す
int size(int x) {
return sizes[find(x)];
}
};
int main(){
ll N;
cin >> N;
vector<double> X(N);
vector<double> Y(N);
rep(i, N) cin >> X[i] >> Y[i];
double ok = 0;
double ng = 200;
while(ok + 0.00000001 < ng) {
double mid = (ok + ng)/2.0;
UnionFind uf(200);
rep(i, N) if(Y[i] + mid >= 100) uf.unite(i, 100);
rep(i, N) if(Y[i] - mid <= -100) uf.unite(i, 101);
rep(i, N) rep(j, N) if( (X[i] - X[j]) * (X[i] - X[j]) + (Y[i] - Y[j]) * (Y[i] - Y[j]) <= mid * mid) uf.unite(i, j);
if(uf.find(100) == uf.find(101)) {
ng = mid;
} else {
ok = mid;
}
}
printf("%.10f\n", ok/2);
} |
#include<iostream>
#include<vector>
using namespace std;
int N,M;
vector<int>G[20];
int col[20];
bool dfs(int u) {
for(int v:G[u]) {
if(col[v]==-1) {
col[v]=3-col[u];
if(!dfs(v))return false;
}
else if(col[u]==col[v]) {
return false;
}
}
return true;
}
int main() {
cin>>N>>M;
vector<pair<int,int> >E(M);
for(int i=0;i<M;i++) {
int a,b;cin>>a>>b;
a--,b--;
E[i]=make_pair(a,b);
G[a].push_back(b);
G[b].push_back(a);
}
long ans=0;
for(int k=0;k<1<<N;k++) {
for(int i=0;i<N;i++) {
col[i]=-1;
if(k>>i&1)col[i]=0;
}
bool ok=true;
for(pair<int,int>e:E)if(col[e.first]==0&&col[e.second]==0) {
ok=false;
break;
}
if(!ok)continue;
int cnt=0;
for(int i=0;i<N;i++)if(col[i]==-1) {
col[i]=1;
if(!dfs(i))
{
ok=false;
break;
}
cnt++;
}
if(ok)ans+=1<<cnt;
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<typename... T> void rd(T&... args) {((cin>>args), ...);}
template<typename... T> void wr(T... args) {((cout<<args<<" "), ...);cout<<endl;}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin>>n>>m;
vector<int> a(m), b(m);
for (int i=0; i<m; i++) {
cin>>a[i]>>b[i];
a[i]--;
b[i]--;
}
vector<int> c(n);
for (auto& i : c) cin>>i;
vector<bool> ab(m);
vector<vector<pii>> g(n);
for (int i=0; i<m; i++) {
if (c[a[i]]>c[b[i]]) ab[i]=true;
else if (c[a[i]] < c[b[i]]) ab[i]=false;
else {
g[a[i]].emplace_back(b[i], i);
g[b[i]].emplace_back(a[i], i);
}
}
vector<int> dep(n, -1);
auto dfs=[&](auto& dfs, int u, int p) -> void {
for (auto [v, e] : g[u]) {
if (v==p) continue;
if (dep[v]==-1) {
dep[v]=dep[u]+1;
ab[e]=a[e]==v;
dfs(dfs, v, u);
} else if (dep[u]>dep[v]) ab[e]=a[e]==v;
}
};
for (int i=0; i<n; i++) {
if (dep[i]==-1) {
dep[i]=0;
dfs(dfs, i, i);
}
}
for (auto i : ab) cout<<(i ? "->" : "<-")<<'\n';
return 0;
}
|
#include <iostream>
#include <math.h>
#include <vector>
#include <string>
typedef long long ll;
using namespace std;
#define prt(var) cout << (var) << endl;
#define rep(i,n) for(int i=0; i<int(n); i++)
int main() {
int n;
string s;
cin>>n>>s;
ll ans=0;
for(int i=0; i<n-1; i++){
int l=i, r=i+1;
int ca=0, ct=0, cg=0, cc=0;
while(l>=0 && r<=n-1){
if(s[l]=='A') ca++;
if(s[l]=='T') ct++;
if(s[l]=='G') cg++;
if(s[l]=='C') cc++;
if(s[r]=='A') ca++;
if(s[r]=='T') ct++;
if(s[r]=='G') cg++;
if(s[r]=='C') cc++;
if(ca==ct && cc==cg){
ans++;
}
l--; r++;
}
}
prt(ans);
} | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, ans = 0;
string s;
cin >> n >> s;
for (int i = 0; i < n; i++) {
int cnt[26] = {};
for (int j = i; j < n; j++) {
cnt[s[j] - 'A']++;
if (cnt[0] == cnt['T' - 'A'] && cnt['C' - 'A'] == cnt['G' - 'A'])
ans++;
}
}
cout << ans << '\n';
} |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll> subsets(vector<int> a){
int n = a.size();
vector<ll> ret;
for(int mask = 0; mask < (1<<n); mask++){
ll sum = 0;
for(int i=0; i<n; i++){
if( (mask&(1<<i)) != 0){
sum += a[i];
}
}
ret.push_back(sum);
}
return ret;
}
int main() {
int n, t;
cin>>n>>t;
int a[n];
for(int i=0; i<n; i++){
cin>>a[i];
}
vector<int> s1(a, a+(n/2));
vector<int> s2(a+(n/2), a+n);
vector<ll> sum1 = subsets(s1), sum2 = subsets(s2);
sort(sum1.begin(), sum1.end());
sort(sum2.begin(), sum2.end());
ll mn = 1e15;
for(ll sum: sum1){
if(sum > t) continue;
ll rem = t - sum;
auto it = upper_bound(sum2.begin(), sum2.end(), rem);
it--;
rem -= *it;
mn = min(mn, rem);
}
cout<<t-mn<<'\n';
} | #include<bits/stdc++.h>
#define Fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define Freed freopen ("0in.txt","r",stdin);
#define Fout freopen ("0out.txt","w",stdout);
#define ll long long int
#define pb push_back
#define mp make_pair
#define pi acos(-1.0)
#define Mod 1000000007
#define limit 48
using namespace std;
ll D[limit],W;
void middle(ll pos,ll n,ll sum,vector<ll> &A)
{
if(pos>n)
{
if(sum<=W) A.pb(sum);
return ;
}
middle(pos+1,n,sum,A);
middle(pos+1,n,sum+D[pos],A);
}
void solve(int t)
{
ll n,i,j;
scanf("%lld %lld",&n,&W);
for(ll i=1; i<=n; i++)
{
scanf("%lld",&D[i]);
}
sort(D,D+n+1);
vector<ll>A,B;
A.pb(0);
middle(1,n/2,0,A);
middle(n/2+1,n,0,B);
sort(A.begin(),A.end());
ll mx=0,sz=A.size();
for(i=B.size()-1; i>=0; i--)
{
n = (W-B[i]);
ll l=0,h=sz-1,m;
while(l<=h)
{
m = (l+h)/2;
if(n>=A[m])
{
if((B[i]+A[m])<=W && mx<(B[i]+A[m]))
mx = B[i]+A[m];
l = m+1;
}
else
{
h = m-1;
}
}
}
printf("%lld\n",mx);
return ;
}
int main()
{
// Fast
// Freed
// Fout
int t,tt=1;
// cin >> tt;
for(t=1; t<=tt; t++)
solve(t);
return 0;
}
|
#include<bits/stdc++.h>
#define mod 1000000007
#define pi pair<int, int>
#define ll long long int
#define pll pair<ll, ll>
#define ppll pair<pll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define vppll vector<pair<pll, ll>>
#define vvll vector<vector<ll>>
#define vvpll vector<vector<pll>>
#define repi(i,a,n) for(ll i=a;i<n;i++)
#define repr(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define pr(i,v) for(auto i:v)
static int row[4] = {-1, 0, 0, 1};
static int col[4] = {0, -1, 1, 0};
static int dagb[4] = {1, -1, 1, -1};
static int daga[4] = {1, -1, -1, 1};
//its not the same code
// why dd you not understand;
using namespace std;
int main( )
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt" , "r" ,stdin);
freopen("ott.txt" , "w" ,stdout);
freopen("error.txt" , "w" ,stderr);
#endif
ll t=1;
// cin>>t;
while(t--){
string s;
ll l,n,m,r,w=0,y=0,z,x=0,flag=0,k=-1, mn=1e10,e=0,cnt=0,f=INT_MAX,sum=0,de=1,mx=0,ans=0;
cin>>x>>y>>z>>w;
if(w*z-y<=0)cout<<-1<<endl;
else{
if(x%(w*z-y)!=0)cout<<x/(w*z-y)+1<<endl;
else cout<<x/(w*z-y)<<endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll lo = ll(1LL), hi = ll(1e6);
ll r = -1LL;
while(lo <= hi){
ll x = (lo + hi) >> 1LL;
if(a + x * b <= d * x * c){
r = x;
hi = x - 1;
}else{
lo = x + 1;
}
}
printf("%lld\n", r);
return 0;
} |
# include <bits/stdc++.h>
using namespace std;
const int maxn=4000000+5;
map<pair<int,int>,int> P;
int c[15][15]={{1,0,0,0,0},{1,1,0,0,0},{1,2,1,0,0},{1,3,3,1,0},{0,0,0,0,0},{0,0,0,0,0}};
int n;
char s[maxn];
int lucas(int a,int b){
pair<int,int> d(a,b);
if(P[d]!=0) return P[{a,b}];
return P[d]=(b>0?(lucas(a/3,b/3)*c[a%3][b%3]%3):1);
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>s;
int fl=(n%2?1:-1);
int res=0;
for(int i=0;i<n;i++){
int p;
if(s[i]=='B') p=0;
if(s[i]=='R') p=1;
if(s[i]=='W') p=2;
res=(res+fl*p*lucas(n-1,i))%3;
res=(res+3)%3;
}
if(res==0) cout<<"B";
else if(res==1) cout<<"R";
else cout<<"W";
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
const int N=4e5+1e3+7;
int C(int n,int m)
{
if(n<m||n<0||m<0)
return 0;
long long ret=1;
for(int i=n;i>=n-m+1;i--)
ret=ret*i;
for(int i=1;i<=m;i++)
ret=ret/i;
return ret%3;
}
int Lucas(int n,int m)
{
if(n<3||m<3)
return C(n,m);
return C(n%3,m%3)*Lucas(n/3,m/3)%3;
}
int get(char c)
{
return c=='R'?0:c=='B'?1:2;
}
int n;
char s[N];
string ans="RBW";
int main()
{
scanf("%d",&n);
scanf("%s",s);
n--;
int r=0;
for(int i=0;i<=n;i++)
(r+=Lucas(n,i)*get(s[i]))%=3;
if(n&1)
r=(3-r)%3;
putchar(ans[r]);
} |
#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
while(n--)
{
string s;
cin >> s;
string s1 = "atcoder";
int ans1 = 1e9;
for(int i = 0; i <= min(7, sz(s)); i++)
{
vector <bool> used(sz(s));
int j = 0;
int ans = 0;
while(j < i)
{
bool fl = 0;
for(int p = 0; p < sz(s); p++)
{
if(s1[j] == s[p] && !used[p])
{
used[p] = 1;
fl = 1;
break;
}
if(!used[p])
{
ans++;
}
}
if(!fl)
{
break;
}
j++;
}
if(j < i)
{
continue;
}
if(i == 7)
{
if(i != sz(s))
{
ans1 = min(ans1, ans);
}
}
else
{
bool fl = 0;
for(int p = 0; p < sz(s); p++)
{
if(!used[p] && s[p] > s1[i])
{
fl = 1;
break;
}
if(!used[p])
{
ans++;
}
}
if(fl)
{
ans1 = min(ans1, ans);
}
}
}
if(ans1 == 1e9)
{
ans1 = -1;
}
cout << ans1 << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int T;
cin >> T;
for(int i = 0; i < T; i++){
string S;
cin >> S;
int rest = 0;
string atcoder = "atcoder";
int Slength = S.length();
for(int j = 0; j < min(7, Slength); j++){
rest += (S[j] - atcoder[j]) * 26 * (min(7, Slength) - j);
}
if(rest > 0 || (rest == 0 && Slength > 7)){
cout << 0 << endl;
}else{
bool no = true;
for(int j = 0; j < Slength; j++){
if(S[j] != 'a'){
no = false;
if(S[j] > 116){
cout << j - 1 << endl;
}else{
cout << j << endl;
}
break;
}
}
if(no){
cout << -1 << endl;
}
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sc(x) scanf("%lld",&(x));
#define fi first
#define se second
#define pb push_back
#define endl '\n'
int n, k;
int me;
int A[808][808];
int B[808][808];
vector<int> w;
vector<int> v;
bool check(int x){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
B[i][j] = B[i-1][j] + B[i][j-1] - B[i-1][j-1] + (A[i][j] <= x);
if(i >= k && j >= k){
//cout << i << " "<<j<<" "<<x <<" "<< B[i][j] + B[i-k][j-k] - B[i-k][j] - B[i][j-k] << "\n";
if(B[i][j] + B[i-k][j-k] - B[i-k][j] - B[i][j-k] >= me) {
return true;
}
}
}
}
return false;
}
signed main() {
sc(n)
sc(k)
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
sc(A[i][j])
v.pb(A[i][j]);
}
}
if (n == k) {
sort(v.begin(), v.end());
cout << v[n * n - n * n / 2 - 1];
return 0;
}
int ans = -1;
v.clear();
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= k; j++) {
v.pb(A[i][j]);
}
}
sort(v.begin(),v.end());
me = k * k - k * k / 2;
ans = v[k * k - k * k / 2 - 1];
int l = 0, r = ans;
while(l < r){
int mid = (l+r)/2;
if(check(mid)){
r = mid;
}else {
l = mid + 1;
}
}
ans = l;
cout << ans << '\n';
} | #include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i,k,j) for(int i=(k);i>=(j);i--)
#define For(i,k,j) for(int i=(k);i<=(j);i++)
#define Foe(i,u) for(int i=lst[u],v=e[i].v;i;i=e[i].nxt,v=e[i].v)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define Fin(s) freopen(s,"r",stdin)
#define Fout(s) freopen(s,"w",stdout)
#define file(s) Fin(s".in"),Fout(s".out")
#define INF ((1<<30)-1)
//#define int long long
const int P=998244353; //
using namespace std;
template<typename T>inline void ckmax(T &a,T b) {(a<b)&&(a=b);}
template<typename T>inline void ckmin(T &a,T b) {(a>b)&&(a=b);}
inline int mul(int a,int b) {return 1ull*a*b%P;}
inline int add(int a,int b) {return a+b>=P?a+b-P:a+b;}
inline int sub(int a,int b) {return a-b>=0?a-b:a-b+P;}
inline void mulmod(int &a,int b) {a=mul(a, b);}
inline void addmod(int &a,int b) {((a+=b)>=P)&&(a-=P);}
inline void submod(int &a,int b) {((a-=b)<0)&&(a+=P);}
inline int ksm(int a,int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline void fprint(const int &x,char c=' ') {fprintf(stderr,"%d%c",x,c);}
inline void fprint(const pii &x,char c='\n') {fprintf(stderr,"%d %d%c",x.first,x.second,c);}
inline void fprint(const int *f,const int &n,char c='\n') {for(int i=1;i<=n;i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline void fprint(const vector<int> &f,char c='\n') {for(int i=0;i<(int)f.size();i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline int inv(int a) {return ksm(a,P-2);}
namespace FastIO {
const int SIZE=1<<16; char buf[SIZE],obuf[SIZE],str[64]; int bi=SIZE,bn=SIZE,opt;
int read(char *s) {
while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
}
bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
void write(int x) {
if(!x) obuf[opt++]='0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}
}
void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}}
void Fflush() {if (opt) fwrite(obuf,1,opt,stdout); opt=0;}
};
inline int read() {int x; FI(x); return x;}
int A,B;
signed main() {
#ifndef ONLINE_JUDGE
freopen("pro.in","r",stdin);
freopen("pro.out","w",stdout);
#endif
A=read(),B=read();
if(A>B) {
int sa=0,sb=0;
For(i,1,A) printf("%d ",i),sa+=i;
For(i,1,B-1) printf("%d ",-i),sb+=i;
printf("%d\n",-(sa-sb));
} else {
int sa=0,sb=0;
For(i,1,A-1) printf("%d ",i),sa+=i;
For(i,1,B) printf("%d ",-i),sb+=i;
printf("%d\n",(sb-sa));
}
return FastIO::Fflush(),0;
}
/*
*/ |
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll =long long;
typedef pair<int,int> P;
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
#define For(i, a, b) for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n) For(i, 0, n)
#define debug(x) cerr << #x << " = " << (x) << endl;
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 coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}
const int dy[] = {0,0,1,-1};
const int dx[] = {1,-1,0,0};
const ll mod = 1e9+7;
const ll MOD = 998244353;
const double PI=3.14159265358979323846;
const int INF = 1001001001;
//Write From this Line
// now += mint(2).pow(s) - 1; こんな感じで、2のs乗を求められrう
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
int main()
{
int n, m;
cin >> n >> m;
vector<int> a(n);
rep(i,n) cin >> a[i];
ll sum = 0;
rep(i,n){
sum += a[i];
}
mint ans = 1;
for(ll i = 0; i < sum+n; i++){
ans *= (m+n-i);
ans /= (i+1);
}
cout << ans << endl;
}
| #include <iostream>
#include <string>
using namespace std;
int main(){
int m,h;cin>>m>>h;
string ans = "Yes";
if(h%m)ans = "No";
cout<<ans<<endl;
return 0;
}
|
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
int N;
LL A[101];
LL X;
LL dp[101][101];
void MAIN() {
scanf("%d%lld", &N, &X);
REP (i, N) scanf("%lld", A+i);
LL ans = 1LL<<60;
REP (i, N) {
amin(ans, X - A[i]);
}
for (int B=2; B<=N; B++) {
memset(dp, 0xc0, sizeof dp);
dp[0][0] = 0;
REP (i, N) {
int v = A[i] % B;
for (int c=i; c>=0; c--) {
REP (j, B) {
amax(dp[c+1][(j+v) % B], dp[c][j] + A[i]);
}
}
}
REP (j, B) if (dp[B][j] >= 0 && (X-j) % B == 0) {
assert((X-dp[B][j]) % B == 0);
LL tmp = (X-dp[B][j]) / B;
amin(ans, tmp);
}
}
if (ans == (1LL<<60)) {
puts("-1");
} else {
printf("%lld\n", ans);
}
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = 1e18;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
ll X;
cin >> N >> X;
vector<ll> A(N);
for (int i=0;i<N;i++) cin >> A[i];
ll ans = INF;
for (int i=1;i<=N;i++) {
// dp[a][b][c]: a番目まで見て、b個とって、val = c(mod i) のときの値の最大値
vector<vector<vector<ll>>> dp(N+1, vector<vector<ll>>(i+1, vector<ll>(i, -1)));
dp[0][0][0] = 0LL;
for (int a=0;a<N;a++) {
for (int b=0;b<=i;b++) {
for (int c=0;c<i;c++) {
if (dp[a][b][c] == -1) continue;
dp[a+1][b][c] = max(dp[a+1][b][c], dp[a][b][c]);
if (b+1 > i) continue;
dp[a+1][b+1][(c+A[a])%i] = max(dp[a+1][b+1][(c+A[a])%i], dp[a][b][c]+A[a]);
}
}
}
if (dp[N][i][(X%i)] == -1) continue;
ans = min(ans, (X-dp[N][i][(X%i)])/i);
}
cout << ans << "\n";
return 0;
} |
#include <math.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;
using ll = long long;
struct Edge {
int to;
ll w;
Edge(int to, ll w) : to(to), w(w) {}
};
using Graph = vector<vector<Edge>>;
template <class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
} else {
return false;
}
}
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;
const ll INF_ll = 1e17;
const int INF = 1e8;
struct UnionFind {
vector<int> par, siz;
UnionFind(int n) : par(n, -1), siz(n, 1) {}
int root(int x) {
if (par[x] == -1) {
return x;
} else {
return par[x] = root(par[x]);
}
}
bool issame(int x, int y) { return root(x) == root(y); }
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return false;
}
if (siz[x] < siz[y]) {
swap(x, y);
}
par[y] = x;
siz[x] += siz[y];
return true;
}
int size(int x) { return siz[root(x)]; }
};
ll modpow(ll x, ll n, ll m) {
if (n == 0) return 1;
if (n == 1) return x % m;
if (n % 2 == 1) return (x * modpow(x, n - 1, m)) % m;
ll res = modpow(x, n / 2, m);
return (res * res) % m;
}
int main() {
const ll MOD = 998244353;
ll N;
cin >> N;
UnionFind uf(N);
for (int i = 0; i < N; i++) {
int f;
cin >> f;
f--;
uf.unite(i, f);
}
ll g = 0;
for (int i = 0; i < N; i++) {
if (uf.root(i) == i) {
g++;
}
}
cout << modpow(2, g, MOD) - 1 << endl;
return 0;
} | /*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" )
void solve(){
inl(n);
V<string> s(3);
in(s[0],s[1],s[2]);
rep(i,n)cout<<'0';
rep(i,n)cout<<'1';
cout<<'0'<<endl;
}
int main(){
init();
inl(t);
rep(i,t)solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define test(t) int t;cin>>t;while(t--)
#define lli long long int
#define pb push_back
#define MAX 10000007
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
//a.push_back(i+'0');convert i to char;
//cout << fixed << setprecision(2) << pi ;
const lli infl=0x3f3f3f3f3f3f3f3fLL;
int main()
{ fast;
// int t;
//cin>>t ;
// while(t--)
{
lli i,j,n,q,k,sum=0,cnt=0,ans=0,num,x,y;
cin>>n;
if(n%2)cout<<"Black";
else
cout<<"White";
//cout<<ans<<"\n";
cout<<endl;
}
}
| #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30) - 1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
if(a>b){
a = b; return true;
}
else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
if(a<b){
a = b; return true;
}
else return false;
}
template<typename T> inline void print(T &a){
int sz = a.size();
for(auto itr = a.begin(); itr != a.end(); itr++){
cout << *itr;
sz--;
if(sz) cout << " ";
}
cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const int mod = 1e9 + 7;
const int mod = 998244353;
int main(){
int n; cin >> n;
rep(i,n) cout << i/2+1 << " " << (i+n)/2+1 << "\n";
} |
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
# define M_PI 3.14159265358979323846
const int M=1e9+7;
long long mod(long long x){
return ((x%M + M)%M);
}
long long add(long long a, long long b){
return mod(mod(a)+mod(b));
}
long long mul(long long a, long long b){
return mod(mod(a)*mod(b));
}
ll modPow(ll a, ll b){
if(b==0)
return 1LL;
if(b==1)
return a%M;
ll res=1;
while(b){
if(b%2==1)
res=mul(res,a);
a=mul(a,a);
b=b/2;
}
return res;
}
vector<int>adj[200005];
vector<bool>vis(200005,false);
vector<int>a(200005);
vector<int>b(200005);
long long suma=0;
long long sumb=0;
void dfs(int i){
if(!vis[i]){
vis[i]=true;
suma+=(long long)a[i];
sumb+=(long long)b[i];
for(auto v : adj[i]){
dfs(v);
}
}
}
void solve (){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cin>>b[i];
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
--x;
--y;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i=0;i<n;i++){
if(!vis[i]){
suma=0;
sumb=0;
dfs(i);
if(suma!=sumb){
cout<<"No";
return;
}
}
}
cout<<"Yes";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout<<fixed;
cout<<setprecision(10);
// freopen("timber_input.txt", "r", stdin);
// freopen("timber_output.txt", "w", stdout);
int t=1;
// cin>>t;
for(int i=1;i<=t;i++){
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
constexpr int INF = 1e9, MOD = 1e9 + 7;
constexpr int64_t LINF = 5e18, LMOD = 998244353;
// #include <atcoder/all>
// using namespace atcoder;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};
vector<int> edge[200010];
int64_t dp[200010];
int main() {
int n, m; cin >> n >> m;
int64_t a[n]; rep(i,n) cin >> a[i];
while(m--) {
int x, y; cin >> x >> y;
--x, --y;
edge[y].push_back(x);
}
int64_t ans = -MOD;
for(int i = 0; i < n; ++i) {
dp[i] = MOD;
if(edge[i].empty()) {
dp[i] = a[i];
} else {
for(int to : edge[i]) {
dp[i] = min(dp[to], dp[i]);
}
ans = max(ans, a[i] - dp[i]);
dp[i] = min(a[i], dp[i]);
}
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
int n;
string s,t;
cin >> n >> s >> t;
vector<int> a,b;
for(int i = 0;i < n;i++){
if(s.at(i) == '0') a.push_back(i);
if(t.at(i) == '0') b.push_back(i);
}
if((int)a.size() != (int)b.size()){
cout << -1 << endl;
return 0;
}
int res = 0;
for(int i = 0;i < (int)a.size();i++){
if(a.at(i) != b.at(i)) res++;
}
cout << res << endl;
} | #include <bits/stdc++.h>
constexpr int DEBUG = 0;
using namespace std;
using int64 = long long;
int64 Solve() {
int n; cin >> n;
string s0; cin >> s0;
string s1; cin >> s1;
vector<int> x0s;
vector<int> x1s;
for (int i = 0; i < n; i++) {
if (s0[i] == '0') {
x0s.push_back(i);
}
if (s1[i] == '0') {
x1s.push_back(i);
}
}
if (x0s.size() != x1s.size()) return -1;
int c = 0;
for (int i = 0; i < x0s.size(); i++) {
if (x0s[i] != x1s[i]) {
c++;
}
}
return c;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << Solve() << endl;
} |
#include <bits/stdc++.h>
template <typename InputIterator>
typename InputIterator::value_type summation(InputIterator first,
InputIterator last) {
return std::accumulate(first, last, typename InputIterator::value_type());
}
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v);
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p);
template <typename T>
std::istream &operator>>(std::istream &stream, std::vector<T> &v) {
for (auto &i : v) {
stream >> i;
}
return stream;
}
template <typename T1, typename T2>
std::istream &operator>>(std::istream &stream, std::pair<T1, T2> &p) {
stream >> p.first >> p.second;
return stream;
}
int main() {
int64_t n, k, m;
std::cin >> n >> k >> m;
auto r = m * n;
std::vector<int64_t> a(n - 1);
std::cin >> a;
r -= summation(a.cbegin(), a.cend());
if (k < r) {
std::cout << -1 << std::endl;
} else {
std::cout << std::max(static_cast<int64_t>(0), r) << std::endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int INF = 1e9;
const long long inf = 1LL<<60;
//cout << fixed << setprecision(10) << << endl;
char judge(char a, char b) {
if (a == b) return a;
if (a == 'P') {
if (b == 'R') {
return a;
} else {
return b;
}
} else if (a == 'R') {
if (b == 'S') {
return a;
} else {
return b;
}
} else {
if (b == 'P') {
return a;
} else {
return b;
}
}
}
int main() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
auto run = [&]() -> void {
string t = s + s;
s = "";
for (int i=0; i<n*2; i+=2) {
s += judge(t.at(i), t.at(i+1));
}
};
for (int i=0; i<k; i++) {
run();
}
cout << s.at(0) << endl;
}
|
#include <math.h>
#include <assert.h>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <stack>
#include <array>
#include <random>
#include <chrono>
#include <climits>
#include <bitset>
using namespace std ;
#define rep(i, a, b) for (int i=a; i<(b); i++)
#define forn(i, a) for (int i=0; i<(a); i++)
#define repd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define ford(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define int long long
#define ii pair<int,int>
#define ar array
#define FAST_IO ios_base::sync_with_stdio(false) ;cin.tie(NULL);cout.tie(NULL)
#define ms(x,a) memset(x,(int)(a),sizeof(x))
#define sz(x) ((int)(x).size())
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define ff first
#define ss second
//const int INF = 1e18 ;
//const int MOD = 1e9 + 7 ;
const int N = 2e5+5 ;
int a[N] ;
bool done[N] ;
vector<int> fact;
vector<int> ifact;
vector<int> inv;
vector<int> pow2;
const int MOD = 998244353;
int add(int a, int b)
{
a+=b;
while(a>=MOD) a-=MOD;
return a;
}
void radd(int &a, int b)
{
a=add(a,b);
}
int mult(int a, int b)
{
return (a*1LL*b)%MOD;
}
void rmult(int &a, int b)
{
a=mult(a,b);
}
int modpow(int a, int b)
{
int r=1;
while(b)
{
if(b&1) r=mult(r,a);
a=mult(a,a);
b>>=1;
}
return r;
}
int choose(int a, int b)
{
if(a<0||b<0) return 0;
if(a<b) return 0;
if(b==0) return 1;
if(a==b) return 1;
return mult(fact[a],mult(ifact[b],ifact[a-b]));
}
int inverse(int a)
{
return modpow(a,MOD-2);
}
void init(int _n)
{
fact.clear(); ifact.clear(); inv.clear(); pow2.clear();
fact.resize(_n+1);
ifact.resize(_n+1);
inv.resize(_n+1);
pow2.resize(_n+1);
pow2[0]=1;
ifact[0]=1;
fact[0]=1;
for(int i=1;i<=_n;i++)
{
pow2[i]=add(pow2[i-1],pow2[i-1]);
fact[i]=mult(fact[i-1],i);
//ifact[i]=mult(ifact[i-1],inv[i]);
}
ifact[_n] = inverse(fact[_n]);
for(int i=_n-1;i>=1;i--)
{
ifact[i] = mult(ifact[i + 1], i + 1);
}
for(int i=1;i<=_n;i++)
{
inv[i] = mult(fact[i-1],ifact[i]);
}
}
vector<vector<int>> graph;
int cycles;
vector<int> vis ;
void dfs(int node) {
if(vis[node] == 2) return ;
if(vis[node] == 1) {
cycles++ ;
return ;
}
vis[node] = 1 ;
for(int x : graph[node]) {
dfs(x) ;
}
vis[node] = 2 ;
}
void solve(){
int n ;
cin >> n ;
graph.resize(n) ;
forn(i,n) {
cin >> a[i] ;
--a[i] ;
graph[i].push_back(a[i]) ;
}
cycles = 0;
vis.assign(n, 0) ;
forn(i,n) dfs(i) ;
int ans = pow2[cycles] ;
radd(ans, MOD-1) ;
cout << ans << endl ;
}
int32_t main(){
FAST_IO ;
int t = 1 ;
init(N) ;
// cin >> t ;
while(t--){
solve() ;
}
}
| #include <bits/stdc++.h>
using namespace std;
const long long MOD=1000000007;
// const long long MOD=998244353;
#define LOCAL
#pragma region Macros
typedef long long ll;
typedef __int128_t i128;
typedef unsigned int uint;
typedef unsigned long long ull;
#define ALL(x) (x).begin(),(x).end()
const int INF=1e9;
const long long IINF=1e18;
const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const char dir[4]={'D','R','U','L'};
template<typename T>
istream &operator>>(istream &is,vector<T> &v){
for (T &x:v) is >> x;
return is;
}
template<typename T>
ostream &operator<<(ostream &os,const vector<T> &v){
for (int i=0;i<v.size();++i){
os << v[i] << (i+1==v.size()?"": " ");
}
return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const pair<T,U> &p){
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template<typename T,typename U,typename V>
ostream&operator<<(ostream &os,const tuple<T,U,V> &t){
os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ')';
return os;
}
template<typename T,typename U,typename V,typename W>
ostream&operator<<(ostream &os,const tuple<T,U,V,W> &t){
os << '(' << get<0>(t) << ',' << get<1>(t) << ',' << get<2>(t) << ',' << get<3>(t) << ')';
return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const map<T,U> &m){
os << '{';
for (auto itr=m.begin();itr!=m.end();){
os << '(' << itr->first << ',' << itr->second << ')';
if (++itr!=m.end()) os << ',';
}
os << '}';
return os;
}
template<typename T,typename U>
ostream &operator<<(ostream &os,const unordered_map<T,U> &m){
os << '{';
for (auto itr=m.begin();itr!=m.end();){
os << '(' << itr->first << ',' << itr->second << ')';
if (++itr!=m.end()) os << ',';
}
os << '}';
return os;
}
template<typename T>
ostream &operator<<(ostream &os,const set<T> &s){
os << '{';
for (auto itr=s.begin();itr!=s.end();){
os << *itr;
if (++itr!=s.end()) os << ',';
}
os << '}';
return os;
}
template<typename T>
ostream &operator<<(ostream &os,const multiset<T> &s){
os << '{';
for (auto itr=s.begin();itr!=s.end();){
os << *itr;
if (++itr!=s.end()) os << ',';
}
os << '}';
return os;
}
template<typename T>
ostream &operator<<(ostream &os,const unordered_set<T> &s){
os << '{';
for (auto itr=s.begin();itr!=s.end();){
os << *itr;
if (++itr!=s.end()) os << ',';
}
os << '}';
return os;
}
template<typename T>
ostream &operator<<(ostream &os,const deque<T> &v){
for (int i=0;i<v.size();++i){
os << v[i] << (i+1==v.size()?"": " ");
}
return os;
}
void debug_out(){cerr << '\n';}
template<class Head,class... Tail>
void debug_out(Head&& head,Tail&&... tail){
cerr << head;
if (sizeof...(Tail)>0) cerr << ", ";
debug_out(move(tail)...);
}
#ifdef LOCAL
#define debug(...) cerr << " ";\
cerr << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << '\n';\
cerr << " ";\
debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
template<typename T> T gcd(T x,T y){return y!=0?gcd(y,x%y):x;}
template<typename T> T lcm(T x,T y){return x/gcd(x,y)*y;}
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;
}
#pragma endregion
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll n; cin >> n;
auto c=[](ll x){return x*(x+1)/2;};
ll lb=0,ub=2*INF;
while (ub-lb>1LL){
ll mid=(ub+lb)>>1LL;
(c(mid)<=n+1?lb:ub)=mid;
}
cout << n-lb+1 << '\n';
} |
#include <bits/stdc++.h>
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define endl "\n"
#define pi 3.14159265
const ll mod = 1000 * 1000 * 1000 + 7;
const ll mod1 = 998244353;
const ll INF = 1ll*1000*1000*1000*1000*1000*1000 + 7;
using namespace std;
ll power(ll x, ll y)
{
ll res = 1;
while (y > 0)
{
if (y & 1)
res = (long long)(res*x);
y = y>>1;
x = (long long)(x*x);
//cout<<x<<'\n';
}
return res;
}
ll pre[200005];
ll suf[200005];
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
fastio();
ll n,m;
cin>>n>>m;
vector <ll> a(n); vector <ll> b(m);
For(i,0,n)
cin>>a[i];
For(i,0,m)
cin>>b[i];
sort(all(a));
sort(all(b));
for(int i=0;i<n;i+=2) {
if (i>0)
pre[i]=a[i+1]-a[i]+pre[i-2];
else
pre[i]=a[i+1]-a[i];
}
for(int i=n-1;i>=1;i-=2) {
if (i<n-1)
suf[i]=a[i]-a[i-1]+suf[i+2];
else
suf[i]=a[i]-a[i-1];
}
//cout<<pre[2]<<endl;
ll ans=INF;
for(int i=0;i<m;i++) {
ll pos=lower_bound(all(a),b[i])-a.begin();
if (pos&1) {
ans=min(ans,suf[pos+1]+(pos-3>=0?pre[pos-3]:0LL)+abs(b[i]-a[pos-1]));
} else {
ans=min(ans,suf[pos+2]+(pos-2>=0?pre[pos-2]:0LL)+abs(b[i]-a[pos]));
}
}
cout<<ans;
return 0;
}
// check all product based operations for integer overflow
| #include<iostream>
#include<algorithm>
#include<cstdlib>
#include<climits>
using namespace std;
int main(){
int n,m;
long long children[200005];
long long teacher[200005];
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>children[i];
}
for(int i=1;i<=m;i++){
cin>>teacher[i];
}
sort(children+1,children+n+1);
long long left_sum[100005]={0};
long long right_sum[100005]={0};
for(int i=1;i<n;i+=2){
left_sum[(i+1)/2]=llabs(children[i]-children[i+1])+left_sum[(i+1)/2-1];
right_sum[(i+1)/2]=llabs(children[i+1]-children[i+2])+right_sum[(i+1)/2-1];
}
/*
cout<<endl<<"left"<<endl;
for(int i=0;i<=n/2;i++){
cout<<left_sum[i]<<endl;
}
cout<<endl<<"right"<<endl;
for(int i=0;i<=n/2;i++){
cout<<right_sum[i]<<endl;
}
*/
long long minimum=LLONG_MAX;
for(int i=1;i<=m;i++){
long long sum=0;
int index=upper_bound(children+1,children+n+1,teacher[i])-1-children;
if(index%2==0){
sum+=llabs(teacher[i]-children[index+1])+right_sum[n/2]-right_sum[index/2]
+left_sum[index/2];
}else{
sum+=llabs(teacher[i]-children[index])+right_sum[n/2]-right_sum[index/2]
+left_sum[index/2];
}
/*
cout<<"target="<<teacher[i]<<endl;
cout<<"sum="<<sum<<endl<<endl;
*/
minimum=min(minimum,sum);
}
cout<<minimum<<endl;
return(0);
} |
#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
//#include "atcoder/all"
using namespace std;
const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;
double equal(double a, double b) {
return fabs(a - b) < DBL_EPSILON;
}
std::istream &operator>>(std::istream &in, set<int> &o) {
int a;
in >> a;
o.insert(a);
return in;
}
std::istream &operator>>(std::istream &in, queue<int> &o) {
ll a;
in >> a;
o.push(a);
return in;
}
bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }
typedef priority_queue<ll, vector<ll>, greater<ll> > PQ_ASK;
template<class T, class F>
struct SegmentTree {
F f;
T ti;
vector<T> dat;
int sz;
SegmentTree(const F &f, const T &ti) : f(f), ti(ti) {}
void build(const vector<T> &v) {
assert(v.size());
sz = 1;
while (sz < v.size())sz <<= 1;
dat.resize(sz << 1, ti);
for (int i = 0; i < v.size(); i++)dat[sz - 1 + i] = v[i];
for (int i = sz - 2; i >= 0; i--)dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
}
inline void update(int k, T x) {
k += sz - 1;
dat[k] = x;
while (k) {
k = (k - 1) / 2;
dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
inline void add(int k, int x) {
k += sz - 1;
dat[k] = f(dat[k], x);
while (k) {
k = (k - 1) / 2;
dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
inline T query(int a, int b) {
return query(a, b, 0, 0, sz);
}
T query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)return ti;
if (a <= l && r <= b)return dat[k];
return f(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r));
}
};
int main() {
int n;
cin >> n;
vector<int> v(n);
rep(i, n) cin >> v[i];
auto f = [](ll i, ll j) { return min(i, j); };
SegmentTree<ll, decltype(f)> segmentTree(f, INF);
vector<ll> u(400000);
rep(i, 400000) u[i] = i;
segmentTree.build(u);
rep(i, n) {
segmentTree.update(v[i], INF);
cout << segmentTree.query(0, 400000) << endl;
}
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;
#define INF 1e18
#define MAX 200005
int main() {
int n;
cin >> n;
set<int> a;
rep(i,n+1) a.insert(i);
vector<int> ans;
rep(i,n) {
int p;
cin >> p;
a.erase(p);
ans.push_back(*(a.begin()));
}
for (auto a : ans) cout << a << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll> pql;
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef map<ll, ll> ml;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {return (a % b + b) % b;}
ll quo(ll a, ll b) {return (a - mod(a, b)) / b;}
template <typename T, typename U> bool chmin(T &a, const U b) {if(a > b) {a = b; return 1;} return 0;}
template <typename T, typename U> bool chmax(T &a, const U b) {if(a < b) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 2e5;
const ld eps = 1e-9;
const char newl = '\n';
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
using P = pair<ll, string>;
ll n, t;
string s;
cin >> n;
priority_queue<P, vector<P>, greater<P>> pique;
rep(_, n) {
cin >> s >> t;
pique.push(make_pair(t, s));
if(pique.size() > 2) pique.pop();
}
cout << pique.top().second << newl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int T; cin >> T;
while(T--){
int N; cin >> N;
set<int> se;
rep(i,N){
int a; cin >> a;
if(se.find(a) != se.end()){
se.erase(a);
}else{
se.insert(a);
}
}
cout << (N & 1 || (N % 2 == 0 && se.size() == 0) ? "Second" : "First") << '\n';
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD,dp[101][150000];
int main(){
for(int i=0;i<101;i++)for(int j=0;j<150000;j++)dp[i][j]=0;
ll N,K,MOD;
cin>>N>>K>>MOD;
if(N==1){
cout<<K<<endl;
return 0;
}
dp[1][0]=1,dp[1][K+1]=-1;
for(int i=1;i<50;i++){
for(int j=0;;j++){
if(j>K*i*(i+1)/2)break;
dp[i][j+i]+=dp[i][j];
dp[i][j+1]%=MOD;
}
for(int j=0;;j++){
if(j>K*i*(i+1)/2)break;
dp[i+1][j]+=dp[i][j];
dp[i+1][j]%=MOD;
dp[i+1][j+(i+1)*(K+1)]+=MOD-dp[i][j];
dp[i+1][j+(i+1)*(K+1)]%=MOD;
}
}
if(N>=50){
for(int i=50;i<N;i++){
for(int j=0;j<127510;j++){
dp[i][j+i]+=dp[i][j];
dp[i][j+1]%=MOD;
}
for(int j=0;j<127510;j++){
dp[i+1][j]+=dp[i][j];
dp[i+1][j]%=MOD;
dp[i+1][j+(i+1)*(K+1)]+=MOD-dp[i][j];
dp[i+1][j+(i+1)*(K+1)]%=MOD;
}
}
}
ll ans=0,cnt=0;
cout<<K<<endl;//c_1
for(int i=2;i<N;i++){
cnt=K;
for(int j=1;j<127510;j++){
cnt+=dp[i-1][j]*dp[N-i][j]%MOD*(K+1);
cnt%=MOD;
}
cout<<cnt<<endl;
}
cout<<K<<endl;//c_N
} | #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <chrono>
#include <random>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
// typedef pair<int,P> IP;
// typedef pair<P,P> PP;
double const PI=3.141592653589793;
int const INF=1001001001;
ll const LINF=1001001001001001001;
ll const MOD=1000000007;
int N;
double x[2], y[2];
int main(){
cin>>N;
rep(i,2) cin>>x[i]>>y[i];
double cx,cy;
cx=(double)(x[0]+x[1])/2;
cy=(double)(y[0]+y[1])/2;
x[0]-=cx;
y[0]-=cy;
double a=2*PI/N;
double ansx,ansy;
ansx=x[0]*cos(a)-y[0]*sin(a);
ansy=x[0]*sin(a)+y[0]*cos(a);
ansx+=cx;
ansy+=cy;
cout<<setprecision(16)<<ansx<<' '<<ansy<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
bool ok(long long N, std::string &S, std::vector<long long> &A, int m) {
rep (i, m) {
vector<ll> tmp(N+1);
rep(j, N+1) {
tmp[j] = A[j] / m;
if (i < A[j] % m) {
tmp[j]++;
}
}
rep(j, N) {
if (
(S[j] == '>' && !(tmp[j] > tmp[j+1])) ||
(S[j] == '<' && !(tmp[j] < tmp[j+1]))
) {
return 0;
}
}
}
return 1;
}
void solve(long long N, std::string S, std::vector<long long> A){
int l = 1, r = 10000+1;
while (r - l > 1) {
int m = (l + r) / 2;
if (ok(N, S, A, m)) {
l = m;
} else {
r = m;
}
}
cout << l << endl;
rep(i, l) {
rep(j, N+1) {
int tmp = A[j] / l;
if (i < A[j] % l) {
tmp++;
}
cout << tmp << ((j == N) ? "\n" : " ");
}
}
// vector<ll> B(N+1);
// ll mn = 0;
// vector<int> gts(N+1);
// vector<int> lts(N+1);
// rep(i, N) {
// if (S[i] == '<') {
// lts[i+1] = lts[i] + 1;
// } else {
// lts[i+1] = 0;
// }
// }
// for (int i = N-1; i >= 0; i--) {
// if (S[i] == '>') {
// gts[i] = gts[i+1] + 1;
// } else {
// gts[i] = 0;
// }
// }
// rep(i, N+1) {
// B[i] = max(gts[i], lts[i]);
// }
// vector<vector<ll>> ans;
// while (1) {
// vector<ll> rest = A;
// int ok = 1;
// rep(i, N+1) {
// if (A[i] >= B[i]) {
// A[i] -= B[i];
// } else {
// ok = 0;
// break;
// }
// }
// if (ok) {
// rep(i, N) {
// if (
// (S[i] == '>' && !(A[i] > A[i+1])) ||
// (S[i] == '<' && !(A[i] < A[i+1]))
// ) {
// ok = 0;
// break;
// }
// }
// }
// if (!ok) {
// ans.push_back(rest);
// break;
// }
// ans.push_back(B);
// }
// cout << ans.size() << endl;
// rep(i, ans.size()) {
// rep(j, ans[i].size()) {
// cout << ans[i][j] << ((j == ans[i].size() - 1) ? "\n" : " ");
// }
// }
}
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
int main(){
long long N;
scanf("%lld",&N);
std::string S;
std::cin >> S;
std::vector<long long> A(N-0+1);
for(int i = 0 ; i < N-0+1 ; i++){
scanf("%lld",&A[i]);
}
solve(N, S, std::move(A));
return 0;
}
| #include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 }; // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };
const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n;
cin >> n;
string s;
cin >> s;
vector<int> a(n+1);
rep(i,n+1)cin >> a[i];
int k = 1e9+7;
for(int i=0;i<n;i++){
chmin(k,abs(a[i] - a[i+1]));
}
cout << k << endl;
for(int j=0;j<k;j++){
for(int i=0;i<=n;i++){
if(j < a[i] % k){
cout << (a[i] + k - 1)/k << " ";
}else{
cout << a[i]/k << " ";
}
}
cout << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
#define vint2(v,n,m,init) vector<vector<int>> v(n, vector<int>(m, init))
#define vll2(v,n,m,init) vector<vector<ll>> v(n, vector<ll>(m, init))
#define rep(i,n) for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n) for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n) vint var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n) vll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n) vstr var(n); rep(i,n){cin >> var[i];}
#define ALL(var) (var).begin(), (var).end()
#define sortall(var) sort(ALL(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define prt(var) cout << (var) << "\n"
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prtall(v) rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
template <typename T>
bool chmax(T &a, const T& b){if(a<b){a=b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T& b){if(a>b){a=b; return true;} return false;}
ll gcd(ll a, ll b){ return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){ return a/gcd(a,b)*b;}
//----------------------------------------------------------------------------
#define prt4(v1,v2,v3,v4) cout<<(v1)<<" "<<(v2)<<" "<<(v3)<<" "<<(v4)<<"\n";
int main(void) {
arrll(a,4);
ll k = a[0] + a[1] + a[2] + a[3];
bool ok = false;
rep(i,4){
REP(j,i+1,4){
if(a[i]+a[j]==k-a[i]-a[j]) ok = true;
}
}
rep(i,4){
if(a[i]==k-a[i]) ok = true;
}
prt(ok ? "Yes" : "No");
}
| /// kazuki08
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef long long ll;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
#define ar array
//#define inf 1000000000
#define mod 1000000007
#define dmp(x) cerr<<"line "<<__LINE__<<" "<<#x<<":"<<x<<endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define mt make_tuple
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
string s,t="atcoder";
int n;
int match(int x){
if(x==0){
int res = mod;
for(int j=0;j<s.size();j++){
if(s[j]>'a')return j;
}
return res;
}
int done[n+1]={0};
int res = 0;
for(int i=0;i<x;i++){
int cnt = 0;
bool f = false;
for(int j=0;j<s.size();j++){
if(done[j])continue;
if(s[j]==t[i]){
done[j]=1;
res+=cnt;
f = true;
break;
}
cnt++;
}
if(!f)return mod;
}
if(x<t.size()){
bool f = false;
int cnt = 0;
for(int j=0;j<s.size();j++){
if(done[j])continue;
if(t[x]<s[j]){
res+=cnt;
f = true;
break;
}
cnt++;
}
if(!f)return mod;
}
else {
if(s.size()>x)return res;
else return mod;
}
return res;
}
signed main(){
IOS;
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int T;
cin >> T;
while(T--){
cin >> s;
n = s.size();
int ans = mod;
for(int i=0;i<=t.size();i++){
ans = min(ans,match(i));
// cerr << i << " " << ans << endl;
}
if(ans>=mod){
ans = -1;
}
cout << ans << endl;
}
return 0;
}
///....
|
// D - Game in Momotetsu World
#include "bits/stdc++.h"
using namespace std;
#ifndef DEBUG
#define fundri 108
#define debug(...) 1729
#define endl '\n'
#endif
#define int int64_t
typedef pair<int, int> pii;
typedef vector<int> vi;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);}
bool in_range(int x, int l, int r) {return l <= x && x <= r;}
template<typename H, typename ...T>void inp(H &head) {cin >> head;}
template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);}
template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;}
template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;}
// Multi-Dimension Vector
// Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type())
template<int D, typename T> struct vec : public vector<vec<D - 1, T>> {
static_assert(D >= 1, "Vector dimensions must be greater than zero !!");
template<typename... Args>
vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){}
};
template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}};
const int inf = 1e15;
const bool testcases = false;
const bool ready = []() -> bool {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DHRUV_GHEEWALA
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
#endif
return true;
}();
void solve(int tc);
int32_t main(int32_t argc, char **argv) {
int TC = 1;
if(testcases) cin >> TC;
// Pre-Processing goes here...
for(int tc = 1; tc <= TC; ++tc) {
solve(tc);
fundri;
}
exit(0);
}
int n, m;
vec<2, int> dp;
vec<2, char> grid;
int get(int i, int j) {
if(!in_range(i, 0, n - 1) || !in_range(j, 0, m - 1)) {
return -inf;
}
return grid[i][j] == '+' ? 1 : -1;
}
int rec(int i, int j) {
if(i == n - 1 && j == m - 1)
return 0;
int &res = dp[i][j];
if(res != -inf) return res;
if(i + 1 >= n) {
return res = get(i, j + 1) - rec(i, j + 1);
}
if(j + 1 >= m) {
return res = get(i + 1, j) - rec(i + 1, j);
}
int down = get(i + 1, j) - rec(i + 1, j);
int right = get(i, j + 1) - rec(i, j + 1);
return res = max(down, right);
}
void solve(int tc) {
cin >> n >> m;
grid = vec<2, char>(n, m);
cin >> grid;
if(n == 1 && m == 1) {
cout << "Draw" << endl;
return;
}
dp = vec<2, int>(n, m, -inf);
int diff = rec(0, 0);
debug(dp);
if(diff == 0) {
cout << "Draw" << endl;
} else if(diff > 0) {
cout << "Takahashi" << endl;
} else {
cout << "Aoki" << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree < pairs , null_type , greater<pairs>, rb_tree_tag , tree_order_statistics_node_update >
#define ll long long
#define lld long double
#define vc vector<ll>
const ll MOD=(1e9 +7);
typedef pair<ll,ll>pairs;
ll power(ll a, ll b){ll res=1;a=a%MOD;while(b>0){if(b&1){res=(res*a)%MOD;b--;}a=(a*a)%MOD;b>>=1;}
return res;}
int main() {
// your code goes here
std::ios::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
//Read the qstn. properlyyyyy!!!
ll t,n,i,j,k,c,f,h,w;
cin>>h>>w;
vector<vc>a(h+1,vc(w+1)),dp(h+1,vc(w+1)),down(h+1,vc(w+1)),diag(h+1,vc(w+1)),rt(h+1,vc(w+1));
char ch;
for(i=1;i<=h;i++)
{
for(j=1;j<=w;j++){
cin>>ch;
if(ch=='.')
{
a[i][j]=1;
}
else a[i][j]=0;
}
}
dp[1][1]=1;
for(i=1;i<=h;i++)
{
for(j=1;j<=w;j++)
{
if(a[i][j])
{
dp[i][j]+=(rt[i][j-1]+diag[i-1][j-1]+down[i-1][j])%MOD;
diag[i][j]=(dp[i][j]+diag[i-1][j-1])%MOD;
down[i][j]=(dp[i][j]+down[i-1][j])%MOD;
rt[i][j]=(dp[i][j]+rt[i][j-1])%MOD;
}
}
}
cout<<dp[h][w];
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/*---------------------DEBUGGING--------------------------------------------*/
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
/*-------------------------------------------------------------------------------------*/
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pcc pair<char,char>
#define F first
#define S second
#define int long long
#define pi 3.141592653589793238462643383279502
#define M 1000000007 //998244353
#define rep(i,a,n) for(int i=a;i<n;i++)
#define INF 1e18
#define N 100005
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define endl "\n"
//typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
void solve()
{ int n;
cin>>n;
vi a(n);
rep(i,0,n)cin>>a[i];
sort(all(a));
bool f = true;
for(int i=0;i<n;i++)
{
if(a[i] != (i+1))f=false;
}
if(f)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int tests;
tests=1;
//cin>>tests;
for(int i=0;i<tests;i++)
{
solve();
}
}
| #include<bits/stdc++.h>
using namespace std;
int n;
int chk[100001];
int main(){
scanf("%d" , &n);
for(int i = 0; i<n;i++){
int tmp;
scanf("%d" , &tmp);
if(chk[tmp]){
printf("No");
return 0;
}
chk[tmp]=1;
}
printf("Yes");
}
|
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>
#include <array>
#include <complex>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(i, a) for (auto &i: (a))
#define sz(x) (int)(x).size()
#define all(v) v.begin(), v.end()
#define fr(i, a, b) for (int i = (a); i <= (b); ++i)
typedef long long ll;
typedef long double ld;
void run_case();
int32_t main () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(3);
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
#endif
int t = 1;
// cin >> t;
while (t--) {
run_case();
}
return 0;
}
const int N = 25;
vector<int> adj[N];
vector<int> nadj[N];
int n, m;
vector<int> vis;
bool dfs (int v, int c = 1) {
vis[v] = c;
bool ok = true;
for (auto &i: nadj[v]) {
if (vis[i] == 0) ok &= dfs(i, 3 ^ c);
else if (vis[i] == vis[v]) return false;
}
return ok;
}
void run_case() {
cin >> n >> m;
vis.resize(n);
for (int i = 0; i < m; ++i) {
int a, b; cin >> a >> b; --a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int ans = 0;
for (int mask = 0; mask < (1 << n); ++mask) {
fill(all(vis), 0);
for (int b = 0; b < n; ++b) {
nadj[b].clear();
if ((1 << b) & mask) {
for (auto &j: adj[b]) {
if ((1 << j) & mask)
nadj[b].push_back(j);
}
}
}
int cnt = 0;
bool ok = true;
for (int b = 0; b < n; ++b) {
if (vis[b]) continue;
if ((1 << b) & mask) {
if (dfs(b)) ++cnt;
else {
ok = false; break;
}
}
}
if (!ok) continue;
for (int b = 0; b < n; ++b) {
if (!vis[b]) {
for (auto j: adj[b]) {
if (!vis[j])
ok = false;
}
}
}
if (!ok) continue;
int pw = 1;
for (int j = 0; j < cnt; ++j)
pw *= 2;
ans += pw;
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false)
#define rep(i, n) for(ll i=0; i<(ll)n; i++)
#define ALL(obj) begin(obj), end(obj)
typedef long long ll;
vector<vector<int> > G(22);
vector<int> color(22, -1), ts;
vector<bool> vis(22, 0);
int n, m, a, b;
void dfs(int now){
ts.push_back(now);
vis[now]=1;
for(int i: G[now]){
if(vis[i])
continue;
dfs(i);
}
}
ll DFS(int i){
if(i==ts.size())
return 1;
ll res=0;
rep(j, 3){
bool f=1;
for(int x:G[ts[i]]){
if(color[x]==j){
f=0;
break;
}
}
if(!f)
continue;
color[ts[i]]=j;
res+=DFS(i+1);
color[ts[i]]=-1;
}
return res;
}
int main()
{
IOS;
cin>>n>>m;
rep(i, m){
cin>>a>>b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
ll ans=1;
rep(i, n){
if(vis[i])
continue;
dfs(i);
fill(ALL(color), -1);
ans*=DFS(0);
ts.clear();
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
constexpr ll mod = 1000000007;
void solve() {
ll n;
cin >> n;
vector<ll> a(n);
rep(i, 0, n) cin >> a[i];
if(n % 2) {
cout << "Second\n";
return;
}
map<ll,ll> mp;
rep(i, 0, n) mp[a[i]]++;
bool f = true;
for(auto e: mp) {
if(e.second % 2) f = false;
}
if(f) cout << "Second\n";
else cout << "First\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll t;
cin >> t;
while(t--) solve();
return 0;
} | #include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string>
#include <iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
using namespace std;
int main() {
int n;
cin >> n;
int i;
int num;
int count = 0;
int check = 0;
for (i = 1; i <= n; i++) {
num = i;
while (num/10>0||num%10!=0) {
if (num%10==7) {
check = 1;
break;
}
num = num / 10;
}
num = i;
while (num / 8 > 0 || num % 8 != 0) {
if (num % 8 == 7) {
check = 1;
break;
}
num = num / 8;
}
if (check == 0) {
count++;
}
check = 0;
}
cout << count;
return 0;
} |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <unordered_map>
using namespace std;
#define debug(x) cout << #x << '=' << x << '\n'
#define rep(i, b, e) for (int i = b; i <= e; i++)
const int N = 212345;
int n, a[N], b[N], c[N];
map<int, queue<int>> mp;
namespace Bit {
int c[N], size;
inline int lowbit(int x) {
return x & (-x);
}
inline void add(int loc, int val) {
for (; loc <= size; loc += lowbit(loc)) {
c[loc] += val;
}
}
inline int query(int loc) {
int ret = 0;
for (; loc; loc -= lowbit(loc)) {
ret += c[loc];
}
return ret;
}
}
int main(){
cin >> n;
rep (i, 1, n) {
scanf("%d", a + i);
a[i] += i;
mp[a[i]].push(i);
}
rep (i, 1, n) {
scanf("%d", b + i);
b[i] += i;
if (mp[b[i]].size() == 0) {
puts("-1");
return 0;
}
c[i] = mp[b[i]].front();
mp[b[i]].pop();
}
//rep (i, 1, n) {
// debug(c[i]);
//}
Bit::size = n;
long long ans = 0;
for (int i = n; i > 0; i--) {
ans += Bit::query(c[i] - 1);
Bit::add(c[i], 1);
}
cout << ans;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int a[200010],b[200010];
queue<int>pos[200010];
map<int,int>id;
int tot,n;
int T[200010<<2];
int q(int rt,int l,int r,int pos){
if(l==r)return T[rt];
int mid=l+r>>1;
if(pos<=mid)return T[rt]+q(rt<<1,l,mid,pos);
else return T[rt]+q(rt<<1|1,mid+1,r,pos);
}
void ad(int rt,int l,int r,int L,int R){
if(l==L&&r==R){
T[rt]++;return;
}
int mid=l+r>>1;
if(R<=mid)ad(rt<<1,l,mid,L,R);
else if(L>mid)ad(rt<<1|1,mid+1,r,L,R);
else ad(rt<<1,l,mid,L,mid),ad(rt<<1|1,mid+1,r,mid+1,R);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
a[i]+=i;
int t=id[a[i]];
if(!t)id[a[i]]=t=++tot;
pos[t].push(i);
}
int flag=1;
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
b[i]+=i;
int t=id[b[i]];
if(t&&!pos[t].empty()){
b[i]=pos[t].front();
pos[t].pop();
}
else flag=0;
}
if(!flag)printf("-1");
else{
long long ans=0;
for(int i=1;i<=n;i++)ans+=q(1,1,n,b[i]),ad(1,1,n,1,b[i]);
printf("%lld",ans);
}
} |
// Template
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <tuple>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <array>
#include <cassert>
#include <cmath>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll infll = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (T &element : vec) is >> element;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) {
os << vec[i] << (i + 1 == vec_len ? "" : " ");
}
return os;
}
struct IOSET {
IOSET() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} ioset;
// Main
int main() {
int n;
cin >> n;
vector<ll> a(n);
cin >> a;
ll mx = 0, sum = 0, sum2 = 0;
rep(i, n) {
chmax(mx, a[i]);
sum += a[i];
cout << sum + mx * (i + 1) + sum2 << '\n';
sum2 += sum;
}
}
| #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){
static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
(*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] );
(*arr)=(T*)(*mem);
(*mem)=((*arr)+x);
}
template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){
sort(a, a+N);
}
inline int my_getchar_unlocked(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread_unlocked(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline int rd_int(void){
int x;
rd(x);
return x;
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite_unlocked(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar_unlocked(a);
}
inline void wt_L(const char c[]){
int i=0;
for(i=0;c[i]!='\0';i++){
my_putchar_unlocked(c[i]);
}
}
int N;
int A[100000];
int main(){
int Lj4PdHRW;
wmem = memarr;
int KL2GvlyY = rd_int();
for(Lj4PdHRW=(0);Lj4PdHRW<(KL2GvlyY);Lj4PdHRW++){
rd(N);
{
int cTE1_r3A;
for(cTE1_r3A=(0);cTE1_r3A<(N);cTE1_r3A++){
rd(A[cTE1_r3A]);
}
}
sortA_L(N,A);
if(N%2==1){
wt_L("Second");
wt_L('\n');
}
else{
int i;
int fg = 1;
for(i=(0);i<(N);i+=(2)){
if(A[i]!=A[i+1]){
fg = 0;
}
}
if(fg){
wt_L("Second");
wt_L('\n');
}
else{
wt_L("First");
wt_L('\n');
}
}
}
return 0;
}
// cLay varsion 20201003-1
// --- original code ---
// int N, A[1d5];
// {
// REP(rd_int()){
// rd(N,A(N));
// sortA(N,A);
// if(N%2==1){
// wt("Second");
// } else {
// int fg = 1;
// rep(i,0,N,2) if(A[i]!=A[i+1]) fg = 0;
// wt(if[fg, "Second", "First"]);
// }
// }
// }
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, d[N], ans[N], last, fa[N], rt, st, mxd[N];
vector <int> e[N];
void dfs1_(int u) {
mxd[u] = d[u] = d[fa[u]] + 1;
for (int v, i = 0; i < (int) e[u].size(); i++) {
v = e[u][i];
if (v == fa[u]) continue;
fa[v] = u, dfs1_(v), mxd[u] = max(mxd[u], mxd[v]);
}
if (d[u] > d[rt]) rt = u;
}
bool cmp_(int u, int v) {
return mxd[u] < mxd[v];
}
void dfs2_(int u) {
last = u;
sort(e[u].begin(), e[u].end(), cmp_);
for (int v, i = 0; i < (int) e[u].size(); i++) {
v = e[u][i];
if (v == fa[u]) continue;
ans[v] = ans[last] + d[last] + d[v] - d[u]*2;
dfs2_(v);
}
}
int main() {
scanf("%d", &n);
for (int u, v, i = 1; i < n; i++)
scanf("%d%d", &u, &v), e[u].push_back(v), e[v].push_back(u);
dfs1_(1), st = rt;
fa[st] = 0, dfs1_(st);
ans[st] = 1, dfs2_(st);
for (int i = 1; i <= n; i++)
printf("%d ", ans[i]);
} | /**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define ld double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define F(i,a,b) for(i=a;i<b;i++)
//#include <ext/pb_ds/assoc_container.hpp> // policy based data structure header files
//#include <ext/pb_ds/tree_policy.hpp> // policy based data structure header files
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds; // for pbds
//#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // have functions like order_of_key, find_by_order
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e18;
const ll MAXN=200200;
ll i,j,mask;
vector<vector<ll> > v(MAXN);
vector<ll> dist(MAXN), par(MAXN), ans(MAXN), vis(MAXN);
ll val=1;
void dfs0(ll p)
{
for(auto x:v[p]) {
if(x!=par[p]) {
par[x]=p;
dist[x]=dist[p]+1;
dfs0(x);
}
}
}
void dfs(ll p, ll parent, ll f)
{
if(ans[p]==0) {
ans[p]=val;
val++;
}
if(f) {
for(auto x:v[p]) {
if(x!=parent) {
dfs(x, p,1);
}
}
val++;
return ;
}
ll node=0;
for(auto x:v[p]) {
if(x!=parent) {
if(vis[x]) {
node=x;
} else {
dfs(x, p, 1);
//val++;
}
}
}
if(node!=0) {
dfs(node, p, 0);
}
}
void solve()
{
ll n;
cin>>n;
F(i,0,n-1) {
ll x,y;
cin>>x>>y;
v[x].pb(y);
v[y].pb(x);
}
dfs0(1);
ll node=0, maxx=0;
F(i,1,n+1) {
if(dist[i]>maxx) {
maxx=dist[i];
node=i;
}
}
//cout<<node;nl
dist[node]=0;
par[node]=0;
dfs0(node);
maxx=0;
F(i,1,n+1) {
if(dist[i]>maxx) {
maxx=dist[i];
node=i;
}
}
//cout<<node;nl
while(par[node]!=0) {
vis[node]=1;
node=par[node];
}
//cout<<node;nl
vis[node]=1;
dfs(node, -1, 0);
F(i, 1, n+1) {
cout<<ans[i]<<" ";
}
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE*/
ll t,test=1;
//cin>>test;
F(t,1,test+1) {
//cout<<"Case #"<<t<<": ";
solve();
nl
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main() {
ll N;
cin >> N;
vl A(N);
rep(i, N) cin >> A[i];
sort(all(A));
ll ans = 0;
vl acc(N + 1), p2(N + 1, 1), invp2(N + 1, 1);
rep(i, N) p2[i + 1] = p2[i] * 2 % MOD;
invp2[N] = modpow(p2[N], MOD - 2, MOD);
repr(i, N) invp2[i] = (invp2[i + 1] * 2) % MOD;
rep(i, N) acc[i + 1] = (acc[i] + p2[i] * A[i]) % MOD;
rep(i, N){
ans += A[i] * A[i] % MOD;
(ans += A[i] * (acc[N] - acc[i + 1]) % MOD * invp2[i + 1] % MOD) %= MOD;
}
cout << (ans + MOD) % MOD << endl;
} | #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int mod = 998244353;
int A[200010];
int main() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d", &A[i]);
}
sort(A + 1, A + 1 + n);
int res = 0, ans = 0;
A[n + 1] = 0;
for(int i = n; i >= 1; --i) {
res = (long long)res * 2 % mod + A[i + 1];
res %= mod;
ans += ((long long) A[i] * res % mod);
ans %= mod;
ans += ((long long)A[i] * A[i] %mod);
ans %= mod;
}
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = 1e18+7;
ll ceildiv(ll a, ll b) { return (a+b-1)/b; } // 切り上げ
ll floordiv(ll a, ll b) { return a/b; } // 切り下げ
int show_matrix(vector<vector<int>> &dp) {
loop(0, dp.size(), i) {
loop(0, dp[i].size(), j) {
cout << dp[i][j] << " ";
}
cout << endl;
}
return 0;
}
/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll N;
cin >> N;
N--;
N /= 100;
print(N+1);
}
| #include<bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
long int n;
cin>>n;
if(n%100==0){
cout<<n/100;
}else{
cout<<((n/100)+1);
}
return 0;
}
|
#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<iostream>
#include<cstdlib>
#include<utility>
#include<tuple>
#include<string>
#include<vector>
#include<numeric>
#include<algorithm>
#include<queue>
#include<deque>
#include<bitset>
#include<cmath>
#include<map>
#include<string>
#include<iomanip>
#include<set>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
const double PI = acos(-1);
#define rep(i, a, b) for(ll i = a; i < b; i++)
ll repeat(ll x) {
ll res = 1;
rep(i, 0, x)res *= 10;
return res;
}
int main() {
string s;
cin >> s;
ll n = s.size();
bool ans = false;
if (n == 1) {
if (stoi(s) == 8) ans = true;
}else if (n == 2) {
if (stoi(s) % 8 == 0) ans = true;
swap(s[0], s[1]);
if (stoi(s) % 8 == 0) ans = true;
}
else {
vector<ll> x(11, 0);
rep(i, 0, n) {
x[s[i] - '0']++;
}
if (x[0] >= 2 && x[8] >= 1) ans = true;
else if (x[0] >= 1) {
for (ll i = 16; i < 100; i += 8) {
s = to_string(i);
if (x[s[0]] >= 1 && x[s[1]] >= 1) ans = true;
}
}
else {
for (ll i = 104; i < 1000; i += 8) {
s = to_string(i);
x[s[0] - '0']--; x[s[1] - '0']--; x[s[2] - '0']--;
if (x[s[0] - '0'] >= 0 && x[s[1] - '0'] >= 0 && x[s[2] - '0'] >= 0)
ans = true;
x[s[0] - '0']++; x[s[1] - '0']++; x[s[2] - '0']++;
}
}
}
if (ans)cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPS(i, n) for(int i = 1; i <= (n); i++)
#define RREP(i, n) for(int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for(int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;
//const int MOD = 1e9 + 7;
const int MOD = 998244353;
const int INF = 1e9 + 7;
const ll INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;
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; }
signed main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll N; cin >> N;
ll ans = 0;
REP(i, N)
{
ll A, B; cin >> A >> B;
A--;
ans += B*(B+1)/2;
ans -= A*(A+1)/2;
}
cout << ans << endl;
} | #include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
long long A[N], B[N];
long long sum = 0;
for (int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
sum = sum + 0.5 * (B[i] - A[i] + 1)*(A[i] + B[i]);
}
cout << sum << endl;
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.