code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(int)n; ++i)
using ll=long long;
int main() {
int n; cin >> n;
ll f=0, s=0;
rep(i,n) { ll a; cin >> a; f+=a; s+=a*a; }
cout << n*s-f*f << endl;
} | #include <bits/stdc++.h>
using namespace std;
long long x,y,i,m,c,r;
pair<long long,int> a[11];
int main() {
scanf("%lld%lld",&x,&y);
a[m++]={x-y/2,1};
a[m++]={x+1,-1};
a[m++]={-x-(y-1)/2,1};
a[m++]={-x+(y-1)/2+1,-1};
if (y>1) {
a[m++]={x,1};
a[m++]={x+(y-2)/2+1,-1};
}
sort(a,a+m);
for (c=i=0; i<m; i++) {
if (c>0) r+=a[i].first-a[i-1].first;
c+=a[i].second;
}
printf("%lld\n",r);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<long,long>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pii>
#define Mi map<int,int>
#define mii map<pii,int>
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define ff first
#define ss second
#define sz(x) (int)x.size()
//#define endl '\n'
#define mod 1000000007
//#define mod 998244353
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rem(i,a,b) for(int i=a;i>b;i--)
#define mp(a,b) make_pair(a,b)
#define INF numeric_limits<ll>::max();
#define NINF numeric_limits<ll>::min();
#define vvi(a,b,name) vector<vector<int>> name(a,vector<int>(b,-1))
const long double pi=3.14159265359;
inline ll add(ll a, ll b, ll m)
{
if ((a + b) >= m)
return (a + b) % m;
return a + b;
}
inline ll mul(ll a, ll b, ll m)
{
if ((a * b) < m)
return a * b;
return (a * b) % m;
}
ll power(ll x, ll y, ll m)
{
ll res = 1;
x = x % m;
if (x == 0)
return 0;
while (y > 0)
{
if (y & 1)
res = (res * x) % m;
y = y >> 1;
x = (x * x) % m;
}
return res;
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
int t=1,n;
//cin>>t;
while(t--)
{double r,x,y;
cin>>r>>x>>y;
double d=sqrt(x*x+y*y);
if(d<r){
cout<<2;
}
else
cout<<ceil(d/r);
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ll r,x,y;
cin >> r >> x >> y;
ll euqlid = x*x+y*y;
if(euqlid<r*r){
cout << 2 << endl;
return 0;
}
ll i = 1;
while(1){
if(euqlid<=i*i*r*r) break;
i++;
}
cout << i << endl;
} |
//ΔARC120D
#include<iostream>
#include<cstdio>
#include<fstream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL;
typedef double DB;
const int N = 444444;
int n,a[N],b[N],c[N];
pair<int,int> p[N];
int sz,e[N];
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n*2;i++){
scanf("%d",a+i);
p[i]=make_pair(a[i],i);
}
sort(p+1,p+n*2+1);
for(i=1;i<=n;i++)
b[p[i].second]=1;
for(i=1;i<=n*2;i++){
if(!sz){
e[++sz]=i;
continue;
}
if(b[e[sz]]==b[i])
e[++sz]=i;
else
c[i]=0,c[e[sz]]=1,sz--;
}
for(i=1;i<=n*2;i++){
if(c[i])
cout<<'(';
else
cout<<')';
}
return 0;
} | #include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = long long; using ull = unsigned long long; using pll = pair<ll,ll>; const ll INF = 4e18;
// clang-format on
void print(ll v){
cout << v << endl;
}
ll calc(vector<pll> &task, ll x){
ll y = x;
for(auto tsk: task){
ll a=tsk.first;
ll t=tsk.second;
if(t==1){
y += a;
}
if(t==2){
y = max(y,a);
}
if(t==3){
y = min(y,a);
}
}
return y;
}
int main() {
ll n;
cin >> n;
vector<pll> task;
for (ll i = 0; i < n; i++) {
ll a, t;
cin >> a >> t;
task.push_back({a,t});
}
ll ymin = calc(task, -INF);
ll ymax = calc(task, INF);
// yminと等しくなる最大のx
ll l = -INF;
ll r = INF;
ll xmin = l;
while(l<=r) {
ll x=(l+r)/2;
if(calc(task,x)==ymin){
xmin = max(xmin, x);
l = x+1;
}else{
r = x-1;
}
}
// ymaxと等しくなる最小のx
l = -INF;
r = INF;
ll xmax = r;
while(l<=r) {
ll x=(l+r)/2;
if(calc(task,x)==ymax){
xmax = min(xmax, x);
r = x-1;
}else{
l = x+1;
}
}
if(xmax<xmin) xmax=xmin;
ll q;
cin >> q;
for (ll i = 0; i < q; i++) {
ll x;
cin >> x;
if (x < xmin) {
print(ymin);
} else if (x < xmax) {
print((x - xmin) + ymin);
} else {
print(ymax);
}
}
}
|
#include<iostream>
using namespace std;
int main()
{
// 整数の入力
int N;
cin >> N;
double t[N], l[N], r[N], between[N];
int ans =0;
for (int i=0;i<N;i++) {
cin >> t[i] >> l[i] >> r[i];
if (t[i] == 1) {
}
else if (t[i] == 2) {
r[i]-=0.2;
}
else if (t[i] == 3) {
l[i]+=0.2;
}
else if (t[i] == 4) {
l[i]+=0.2;
r[i]-=0.2;
}
between[i] = r[i] - l[i];
}
for (int i=0;i<N;i++) {
for (int j=i+1;j<N;j++) {
if ((r[i] < l[j]) || (r[j] < l[i])) {
}
else {
ans += 1;
}
}
}
cout << ans <<endl;
return 0;
}
| #include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define Lc(x) x << 1
#define Rc(x) x << 1 | 1
#define Int register int
#define Lowbit(x) ((x) & -(x))
#define Swap(a, b) a ^= b ^= a ^= b
#define Abs(x) ((x) < 0 ? -(x) : (x))
#define Max(x, y) ((x) < (y) ? (y) : (x))
#define Min(x, y) ((x) < (y) ? (x) : (y))
#define Isdigit(ch) (ch >= '0' and ch <= '9')
const double Pi = acos (-1.0);
const int MAXN = 1e4 + 10, MAXM = 2e2 + 10;
const LL Mod = 1e9 + 7, INF = 1LL << 60, Inv2 = 5e8 + 4;
inline LL Read () {
LL f = 0, x = 0;
char ch = getchar ();
while (!isdigit (ch) ) {
f |= (ch == '-'), ch = getchar ();
}
while (isdigit (ch) ) {
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar ();
}
return f ? -x : x;
}
inline void Write (const LL x) {
if (x < 0 ) {
putchar ('-'), Write (-x);
return ;
}
if (x > 9 ) {
Write (x / 10);
}
putchar ((x % 10) ^ 48);
return ;
}
LL Answer;
int BIT[MAXN], Temp[MAXN], Left[MAXN], Right[MAXN];
int n, m, Opt;
inline void Modify (int Index, int Val) {
while (Index <= m ) {
BIT[Index] += Val, Index += Lowbit (Index);
}
return ;
}
inline LL Query (int Index) {
LL Res = 0;
while (Index ) {
Res += BIT[Index], Index -= Lowbit (Index);
}
return Res;
}
signed main () {
n = Read ();
Int i, j;
for (i = 1; i <= n; ++ i ) {
Opt = Read (), Left[i] = Read () * 2, Right[i] = Read () * 2;
if (Opt % 2 == 0 ) {
-- Right[i];
}
if (Opt > 2 ) {
++ Left[i];
}
}
for (i = 1; i <= n; ++ i ) {
for (j = i + 1; j <= n; ++ j ) {
if ((Left[i] <= Left[j] and Right[i] >= Left[j]) or (Left[i] <= Right[j] and Right[i] >= Right[j]) or (Left[i] >= Left[j] and Right[i] <= Right[j]) ) {
++ Answer;
}
}
}
printf ("%lld\n", Answer);
return 0;
}
|
//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#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 std;
#define int long long
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
template<class T>
void g(T &a){
cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
//mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
a+=b;
if(a >= mod) a-=mod;
}
ll modpow(ll x,ll n, ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res;
}
int n, cntv, cnte, deg[400005];
vc<int>edge[400005];
bool vis[400005];
void dfs(int v){
if(vis[v]) return;
vis[v] = 1;
cntv ++;
cnte += deg[v];
for(auto at:edge[v]) dfs(at);
}
void solve(){
cin >> n;
rep(i, n){
int a, b; cin >> a >> b;
edge[a].pb(b);
edge[b].pb(a);
deg[a] ++;
deg[b] ++;
}
int ans = 0;
rep(i, 400005){
if(vis[i]) continue;
cntv = cnte = 0; dfs(i);
if(cntv == cnte/2+1) ans += cntv-1;
else ans += cntv;
}
cout<<ans<<endl;
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
int t; t=1; //cin >> t;
while(t--) solve();
}
| #include <bits/stdc++.h>
using namespace std;
namespace kuro {
template<typename Cap>
struct MaxFlow {
public:
MaxFlow(int n=0) : _n(0) {
add_vertices(n);
}
int add_vertex() {
++_n;
g.resize(_n);
return _n-1;
}
vector<int> add_vertices(int size) {
assert(size >= 0);
vector<int> res(size);
iota(res.begin(), res.end(), _n);
_n += size;
g.resize(_n);
return res;
}
int add_edge(int from, int to, Cap cap) {
assert(0 <= from && from < _n);
assert(0 <= to && to < _n);
assert(0 <= cap);
int m = (int)pos.size();
pos.emplace_back(from, (int)g[from].size());
int from_id = (int)g[from].size();
int to_id = (int)g[to].size();
if (from == to) ++to_id;
g[from].push_back(_edge{to, to_id, cap});
g[to].push_back(_edge{from, from_id, 0});
return m;
}
Cap flow(int s, int t, Cap flow_limit=numeric_limits<Cap>::max()) {
assert(0 <= s && s < _n);
assert(0 <= t && t < _n);
assert(s != t);
vector<int> level(_n), iter(_n);
auto bfs = [&]() {
fill(level.begin(), level.end(), -1);
queue<int> que;
level[s] = 0;
que.emplace(s);
while (!que.empty()) {
int v = que.front(); que.pop();
for (_edge& e : g[v]) {
if (e.cap == 0 || level[e.to] >= 0) continue;
level[e.to] = level[v]+1;
if (e.to == t) return;
que.emplace(e.to);
}
}
};
auto dfs = [&](auto self, int v, Cap up) {
if (v == s) return up;
Cap res = 0;
int level_v = level[v];
for (int& i = iter[v]; i < (int)g[v].size(); ++i) {
_edge& e = g[v][i];
if (level_v <= level[e.to]) continue;
if (g[e.to][e.rev].cap == 0) continue;
Cap d = self(self, e.to, min(up-res, g[e.to][e.rev].cap));
if (d <= 0) continue;
g[v][i].cap += d;
g[e.to][e.rev].cap -= d;
res += d;
if (res == up) return res;
}
level[v] = _n;
return res;
};
Cap flow = 0;
while (flow < flow_limit) {
bfs();
if (level[t] == -1) break;
fill(iter.begin(), iter.end(), 0);
Cap f = dfs(dfs, t, flow_limit-flow);
if (!f) break;
flow += f;
}
return flow;
}
private:
int _n;
struct _edge { int to, rev; Cap cap;};
vector<pair<int, int>> pos;
vector<vector<_edge>> g;
};
} // namespace kuro
using namespace kuro;
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
set<int> s;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
s.insert(a[i]);
s.insert(b[i]);
}
int m = s.size();
MaxFlow<int> g(n);
auto nums = g.add_vertices(m);
int start = g.add_vertex();
int goal = g.add_vertex();
map<int, int> mp;
for (int x : s) {
int id = mp.size();
mp[x] = id;
}
for (int i = 0; i < n; ++i) {
g.add_edge(start, i, 1);
}
for (int i = 0; i < m; ++i) {
g.add_edge(nums[i], goal, 1);
}
for (int i = 0; i < n; ++i) {
g.add_edge(i, nums[mp[a[i]]], 1);
g.add_edge(i, nums[mp[b[i]]], 1);
}
int ans = g.flow(start, goal, n);
cout << ans << '\n';
return 0;
} |
#include <iostream>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<queue>
#include<deque>
#include<regex>
#include<stack>
#include<stdio.h>
#include<vector>
#include<set>
#include<map>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
typedef int long long ll;
using namespace std;
typedef pair<int,int> P;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const ll MOD=1e9+7;
static const int MAX = 100;
static const int INF = (1<<23);
template<class T> T gcd(T a, T b){return b? gcd(b,a%b) : a;}
template<class T> T lcm(T a,T b){return a / gcd(a,b)*b;}
int n;
vector<pair<int,int>> ab;
int nab = 0;
bool dfs(int sti,int tab){
bool ret = false;
// cout<<sti<<endl;
if(sti>=2*n+1){
if(tab==nab)return true;
else{
return false;
}
}
for(int c = 1;c+sti<=2*n;c++){
ll ttab = tab;
int nextsti = sti+c*2;
if(nextsti>2*n+1){
ret = false;
break;
}
bool ok = true;
for(int i=0;i<c;i++){
bool cs = false;
int noru = i+sti;
int ori = i+sti+c;
rep(j,n){
if(ab[j].first==noru){
if(ab[j].second==-1||ab[j].second==ori){
cs=true;
}else{
ok=false;
}
}else if(ab[j].second == ori){
if(ab[j].first==-1||ab[j].first==noru){
cs=true;
}else{
ok=false;
}
}else if(ab[j].first==ori||ab[j].second==noru){
ok = false;
}
}
if(!cs)ttab++;
}
if(ok){
bool s = dfs(nextsti,ttab);
if(s==true){ret = true;break;}
else ret = false;
}else{
ret = false;
}
}
//cout<<"ret"<<ret<<endl;
return ret;
}
int main(){
cin>>n;
set<int> f;
rep(i,n){
int a,b;
cin>>a>>b;
ab.push_back({a,b});
if(a!=-1){
if(f.find(a)!=f.end()){
cout<<"No"<<endl;
return 0;
}else{
f.insert(a);
}
}
if(b!=-1){
if(f.find(b)!=f.end()){
cout<<"No"<<endl;
return 0;
}else{
f.insert(b);
}
}
if(a!=-1&&b!=-1&&a>=b){
cout<<"No"<<endl;
return 0;
}
if(a==-1&&b==-1)nab++;
}
bool ans = dfs(1,0);
if(ans){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
struct d_ {
template<typename T> d_& operator,(const T& x) {
cerr << ' ' << x; return *this;}
template<typename T,typename U> d_& operator,(const pair<T,U>& x) {
cerr << ' ' << x.x << ',' << x.y; return *this;}
template<typename T> d_& operator,(const vector<T>& x) {
for(auto x: x) cerr << ' ' << x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const ll N = 207;
ll n;
ll A[N];
ll B[N];
ll Len[N];
ll in[N],out[N];
void end() {
cout << "No" << endl;
exit(0);
}
bool solve(ll i) {
if(i==n) return 1;
ll len = 1;
while(i+2*len<=n) {
bool ok = 1;
FOR(k,i,i+2*len) {
ok &= Len[k]==-1 || Len[k]==len;
if(in[k]) ok &= k-i<len;
if(out[k]) ok &= k-i>=len;
}
FOR(k,i,i+len) {
if(in[k] && out[k+len]) {
ok &= in[k]==out[k+len];
}
}
if(ok && solve(i+2*len)) return 1;
len++;
}
return 0;
}
int main(void) {
ios_base::sync_with_stdio(false);
CL(Len,-1);
cin >> n;
F(n) {
cin >> A[i] >> B[i];
if(~A[i]) A[i]--;
if(~B[i]) B[i]--;
}
bool ok = 1;
set<ll> vals;
F(n) {
if(~A[i] && ~B[i]) {
ok &= A[i]<B[i];
if(ok) FOR(k,A[i],B[i]+1) Len[k]=B[i]-A[i];
}
if(~A[i]) { in[A[i]]=i+1; ok&=!vals.count(A[i]); vals.insert(A[i]); }
if(~B[i]) { out[B[i]]=i+1; ok&=!vals.count(B[i]); vals.insert(B[i]); }
}
n*=2;
if(!ok) end();
ok &= solve(0);
if(!ok) end();
cout << "Yes" <<endl;
return 0;
}
|
#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;
signed main() {
int k;
cin >> k;
int ans = 0;
for (ll a = 1; a <= k; a++) {
for (ll b = 1; a * b <= k; b++){
for (ll c = 1; a * b * c <= k; c++){
ans++;
}
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int N;
cin >> N;
vector<int64_t> A(N);
rep(i, N) cin >> A.at(i);
sort(A.begin(), A.end());
int64_t ans = 0, min;
rep(i, N) {
if (i == 0) {
min = A.at(i);
} else {
ans += (A.at(i) - min) * (i - (N - i - 1));
}
}
cout << ans << endl;
} |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>
#include <cstddef>
#include <type_traits>
#include <vector>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define rep(i, n) for(int i=0;i<(int)(n);i++)
// トポソ
// O(V + E)
// [入力] 重みなし有効グラフ
// [出力] ソートされた頂点列(0-indexed) DAG じゃ無い場合は空列を返す
// verify http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_4_B
vector<int> toposort(vector<vector<int>> &g){
int n = (int)g.size();
vector<int> d(n, 0); // 入次数
for(int i = 0; i < n; i++){
for(int j = 0; j < (int)g[i].size(); j++){
d[g[i][j]]++;
}
}
// 入次数が 0 の頂点を queue に入れる
queue<int> que;
for(int i = 0; i < n; i++){
if(d[i] == 0) que.push(i);
}
vector<int> ret;
while(!que.empty()){
int cur = que.front();
que.pop();
ret.push_back(cur);
for(int i = 0; i < (int)g[cur].size(); i++){
int nxt = g[cur][i];
d[nxt]--;
if(d[nxt] == 0) que.push(nxt);
}
}
// DAG じゃなかったら、頂点列が不足しているはず
if(ret.size() == n) return ret;
else return vector<int>();
}
int main() {
lint n, m; cin >> n >> m;
vector<vector<lint>> dp(n, vector<lint>(2, -INF));
vector<lint> a(n);
rep (i, n) cin >> a[i];
vector<vector<int>> g(n);
rep (i, m) {
int x, y; cin >> x >> y;
x--;
y--;
g[x].push_back(y);
}
vector<int> nodes = toposort(g);
assert(nodes.size() != 0);
for (auto node : nodes) {
// 買う
dp[node][0] = max(dp[node][0], -a[node]);
for (auto nxt : g[node]) {
// うらない
dp[nxt][0] = max(dp[nxt][0], dp[node][0]);
// 売る
dp[nxt][1] = max(dp[nxt][1], dp[node][0] + a[nxt]);
}
}
lint ans = -INF;
for (int node = 0; node < n; node++) {
ans = max(ans, dp[node][1]);
}
cout << ans << endl;
return 0;
}
| #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 |
// 论证: 差分a即可
// 构造: 把a_i平均分
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> bool chmin(T &x, const T &y) {
return x > y ? (x = y, true) : false;
}
template<class T> bool chmax(T &x, const T &y) {
return x < y ? (x = y, true) : false;
}
#define maxk 10005
#define maxn 105
const int inf = 0x3f3f3f3f;
char s[maxn];
int n, k = inf, a[maxn];
int main() {
scanf("%d", &n);
scanf("%s", s + 1);
for (int i = 0; i <= n; i++)
scanf("%d", a + i);
for (int i = 1; i <= n; i++)
chmin(k, abs(a[i] - a[i - 1]));
printf("%d\n", k);
for (int i = 0; i < k; i++) {
for (int j = 0; j <= n; j++) {
printf("%d%c", (a[j] + i) / k, j == n ? '\n' : ' ');
}
}
return 0;
}
| #include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define inp 200005
#define check exit(0)
#define nl cout<<endl;
#define mod 1000000007
#define ll long long int
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(v) for(int i=0;i<v.size();i++) {cout<<v[i]; (i==v.size()-1) ? cout<<"\n":cout<<" "; }
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
// Pick yourself up, 'cause...
int main()
{
jaldi
int n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++) cin>>v[i];
vector<ll> ans(n);
ordered_set os;
for(int i=0;i<n;i++)
{
ans[0] += v[i] - os.order_of_key(v[i]);
os.insert(v[i]);
}
for(int i=1;i<n;i++)
{
ans[i] = ans[i-1] - v[i-1] + (n - 1 - v[i-1]);
}
for(ll x:ans) cout<<x<<" \n";
return 0;
}
// Try till you are out of ideas |
#include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C;
cin >> A >> B >> C;
if ( A*A + B*B < C*C)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
| #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
for (T& x : data)
input >> x;
return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
output << "(" << data.x << "," << data.y << ")";
return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
for (const T& x : data)
output << x << " ";
return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
trav(a, x)
cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
ll gcd(ll a, ll b) {
while (b) {
tie(a, b) = mp(b, a % b);
}
return a;
}
signed main() {
#ifdef LOCAL
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int a, b, c;
cin >> a >> b >> c;
if (a * a + b * b < c * c) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
} |
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <queue>
#define INF 1010101010LL
#define INFLL 1010101010101010101LL
using namespace std;
const int mod = 1000000007;
//const int mod = 998244353;
int main()
{
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
vector<int> ans;
for (int i = 0; i < n; i++) {
bool ok = true;
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
ok = false;
break;
}
}
if (ok) {
ans.emplace_back(a[i]);
}
}
for (int i = 0; i < m; i++) {
bool ok = true;
for (int j = 0; j < n; j++) {
if (b[i] == a[j]) {
ok = false;
break;
}
}
if (ok) {
ans.emplace_back(b[i]);
}
}
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << (i + 1 == ans.size() ? "\n" : " ");
}
return 0;
} | #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <set>
#include <cmath>
#include <algorithm>
#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, m;
cin >> n >> m;
set<int> a;
set<int> b;
int x;
int sa, sb, ga, gb;
rep(i, n) {
cin >> x;
a.insert(x);
if (i == 0) sa = x;
if (i == n-1) ga = x;
}
rep(i, m) {
cin >> x;
b.insert(x);
if (i == 0) sb = x;
if (i == m-1) gb = x;
}
bool fa, fb;
int count = 0;
for (int i = min(sa, sb); i <= max(ga, gb); i++) {
fa = false; fb = false;
if ((a.count(i) && !b.count(i)) || (!a.count(i) && b.count(i))) {
if (count != 0) cout << " ";
cout << i;
count++;
}
}
cout << endl;
}
|
/***Bismillahhirrahmannirrahim***/
/***coding is fun if u enjoy it***/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
/*** Input-Output ***/
#define pf printf
#define sfi(t) scanf("%d",&t)
#define sfi2(a,b) scanf("%d %d",&a,&b)
#define sfl(t) scanf("%lld",&t)
#define sfl2(a,b) scanf("%lld %lld",&a,&b)
/*** Typedef ***/
typedef unsigned long long ull;
typedef long long ll;
/*** Some Prints ***/
#define en cout << '\n'
#define NO cout << "NO" << '\n'
#define no cout << "no" << '\n'
#define YES cout << "YES" << '\n'
#define yes cout << "yes" << '\n'
#define case(t) cout << "Case " << t << ": "
/*** Grids ***/
int drx[8]={-2,-2,-1,-1,1,1,2,2};
int dry[8]={-1,1,-2,2,-2,2,-1,1};
int dirx[4]={-1,0,1,0};
int diry[4]={0,-1,0,1};
/*** Loops ***/
#define foR0(num) for(int i=0;i<num;i++)
#define foR1(num) for(int i=1;i<=num;i++)
#define foRev(num) for(int i=num-1;i>=0;i--)
#define forIn(arr,num) for(int i=0;i<num;i++) sfi(arr[i]);
#define forIn1(arr,num) for(int i=1;i<=num;i++) sfi(arr[i]);
#define vprt(ans) for(int i=0;i<ans.size();i++) cout<< ans[i] << (i+1<ans.size() ? ' ' : '\n');
#define aprt(arr,num) for(int i=0;i<num;i++) cout<< arr[i] << (i+1<ans.size() ? ' ' : '\n');
/*** Sorts ***/
#define all(v) (v).begin(),(v).end()
#define rev(v) reverse(all(v))
#define srt(v) sort(all(v))
#define srtG(v) sort(all(v),greater<int>())
/*** STLs ***/
typedef vector <ll> vll;
typedef vector <int> vi;
typedef set <ll> sll;
typedef set <int> si;
typedef multiset <ll> msll;
typedef multiset <int> msi;
typedef stack <int> sti;
typedef stack <ll> stll;
typedef queue <ll> qll;
typedef queue <int> qi;
typedef map <ll,ll> mll;
typedef map <int,int> mii;
typedef pair <ll,ll> pll;
typedef pair <int,int> pii;
typedef vector <pll> vpll;
typedef vector <pii> vpii;
/*** Define Values ***/
#define PI 2*acos(0.0)
#define E 2.718281828
#define mod 1000000007
#define eps 1e-7
#define sz 100010
#define mx 200005
#define pb push_back
#define pob pob_back
#define puf push_front
#define pof pop_front
#define F first
#define S second
#define mp make_pair
#define ins insert
#define mem(v,val) memset(v,val,sizeof v)
#define add(v) accumulate(v.begin(),v.end(), 0)
#define debug(x) cout<<#x<<" = "<<x<<endl;
void solve()
{
int a,b,c;
cin>>a>>b>>c;
if((a*a)+(b*b)<(c*c))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
int main()
{
///solve();
int tc=1,cs=0;
///cin>>tc;
while(tc--){
///cout<<"Case "<<++cs<<": ";
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int A,B,C,R;
cin >> A >> B >> C;
R = C*C - (A*A + B*B);
if (R > 0){
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
} |
#include<bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define ll long long
using namespace std ;
ll i , j , c =0 ;
const ll mxn = 2e5 ;
ll arr[mxn] ;
void solve()
{
ll n ; cin >> n ;
ll k =200 ;
for(i=0;i<n;i++){
cin >> arr[i] ;
}
ll cnt =0 ;
for ( i = 0; i < n; i++) {
arr[i] = (arr[i] + k) % k;
}
ll hash[k] = { 0 };
for (i = 0; i < n; i++) {
hash[arr[i]]++;
}
for (i = 0; i < k; i++) {
cnt += (hash[i] * (hash[i] - 1)) / 2;
}
cout << cnt << endl;
}
signed main(){
FastIO ;
int t=1 ;
/// cin>>t ;
while(t--){
solve() ;
}
return 0 ;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
for(auto &x: a) cin >> x;
map<int, vector<int>> graph;
for(int i=0;2*i<n;i++) {
graph[a[i]].push_back(a[n-i-1]);
graph[a[n-i-1]].push_back(a[i]);
}
// for(auto kv: graph){
// cout << kv.first << '\n';
// for(auto x: kv.second)
// cout << x << ' ';
// cout << '\n';
// }
set<int> visited;
int counter = 0;
int cc_counter = 0;
function<void(int)> dfs = [&](int u){
visited.insert(u);
cc_counter++;
for(auto v: graph[u])
if(!visited.count(v))
dfs(v);
};
for(auto u: graph){
if(!visited.count(u.first)){
cc_counter = 0;
dfs(u.first);
counter += cc_counter - 1;
}
}
cout << counter << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debugF(ostream &os) {os << endl;}
template <typename Head, typename... Tail>
void debugF(ostream &os, Head H, Tail... T) { os << " " << H; debugF(os, T...); }
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debugF(cout, __VA_ARGS__)
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); os << (i>0 ? " ":"") << v[i++]); return os;}
void solve(){
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int a,b,x,y;
cin >> a >> b >> x >> y;
int ans = x;
if(a == b || a == (b+1)){
cout << ans << "\n";
return 0;
}
int d = abs(a - b);
if(a > b){
ans += (d-1) * min(2*x, y);
}else{
ans += d * min(2*x, y);
}
cout << ans << "\n";
return 0;
} | /*
このコード、と~おれ!
Be accepted!
∧_∧
(。・ω・。)つ━☆・*。
⊂ ノ ・゜+.
しーJ °。+ *´¨)
.· ´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·'* ☆
*/
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
#include <array>
#include <chrono>
//#pragma GCC target("arch=skylake-avx512")
#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a)
#define printLdb(a) printf("%.50Lf\n", a)
#define printdbd(a) printf("%.16lf\n", a)
#define prints(s) printf("%s\n", s.c_str())
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d))
using ll = long long;
using ull = unsigned long long;
constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);
using namespace std;
void scans(string& str) {
char c;
str = "";
scanf("%c", &c);
if (c == '\n')scanf("%c", &c);
while (c != '\n' && c != -1 && c != ' ') {
str += c;
scanf("%c", &c);
}
}
void scanc(char& str) {
char c;
scanf("%c", &c);
if (c == -1)return;
while (c == '\n') {
scanf("%c", &c);
}
str = c;
}
double acot(double x) {
return PI / 2 - atan(x);
}
ll LSB(ll n) { return (n & (-n)); }
template<typename T>
inline T chmin(T& a, const T& b) {
if (a > b)a = b;
return a;
}
template<typename T>
inline T chmax(T& a, const T& b) {
if (a < b)a = b;
return a;
}
//cpp_int
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost::multiprecision;
#else
using cpp_int = ll;
#endif
//atcoder library
#if __has_include(<atcoder/all>)
#include <atcoder/all>
//using namespace atcoder;
#endif
/*
random_device seed_gen;
mt19937 engine(seed_gen());
uniform_int_distribution dist(1, 100);
*/
/*----------------------------------------------------------------------------------*/
int main() {
int n;
scanf("%d", &n);
vector<ll> a(n);
rep(i, n)scanf("%lld", &a[i]);
map<ll, ll> mp;
ll ans = 0;
++mp[0];
ll r = 0;
rep(i, n) {
r += a[i] * (i % 2 ? -1 : 1);
ans += (mp[r]++);
}
printf("%lld\n", ans);
Please AC;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
return ((T(1) << idx) & b) != 0;
}
int main(int argc, char *argv[]) {
i64 i, j, k;
i64 L, R; cin >> L >> R;
vi F(R+1, 0), G(R+1, 0);
i64 g = R;
auto CT = [&](i64 x, i64 g) {
return max(0LL, x / g - 1);
};
auto DT = [&](i64 x, i64 g) {
return x / g;
};
while(g > 1) {
i64 C = CT(R, g) - CT(L-1, g);
F[g] = C * (C - 1) / 2;
i64 D = DT(R, g) - DT(L-1, g);
G[g] = D * (D - 1) / 2;
//cerr << g << " " << F[g] << " ";
i64 k = 2;
while(k * g <= R) {
F[g] -= G[k * g];
G[g] -= G[k * g];
++k;
}
//cerr << F[g] << endl;
--g;
}
WI(accumulate(all(F), 0LL)*2);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
char x[66];
int l,d;
bool ok(long long ts,long long n)
{
long long now=0;
for(int i=0;i<l;i++)
{
now+=x[i];
if(now>ts)
return 0;
if(i<l-1 && now>ts/n)
return 0;
now=now*n;
}
return 1;
}
int main()
{
long long m,L,R,Mid;
scanf("%s%lld",x,&m);
l=strlen(x);
d=0;
for(int i=0;i<l;i++)
{
x[i]-='0';
d=max(d,x[i]-0);
}
if(l==1)
{
if(d<=m)
printf("1\n");
else
printf("0\n");
}
else
{
L=d+1;
R=m;
while(L<R)
{
Mid=(L+R+1)/2;
if(ok(m,Mid))
L=Mid;
else
R=Mid-1;
}
if(L==R && ok(m,L))
printf("%lld\n",L-d);
else
printf("0\n");
}
return 0;
}
|
// main.cpp
// ervb
//
// Created by Kanak Gautam on 21/04/20.
// Copyright © 2020 Kanak Gautam. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <utility>
#define pb push_back
#define mk make_pair
#define endl "\n"
#define mod 1000000007
#define PI 3.1415927
using namespace std;
typedef long long int lli;
typedef long double ld;
typedef pair<lli,lli> ii;
priority_queue <lli, vector<lli>, greater<lli> > ti;
vector <lli> p[300005],b(100005,0),k;
lli f[100005],d[100005];
//vector<pair<lli,ii>>p[300005];
map<pair<lli,lli>,lli>mp;
//vector<pair<pair<lli, lli>,lli> > st;
map<lli,lli> np;
//queue<lli> qy;
lli gcd(lli a, lli b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
lli bpow(lli a, lli b) {
lli res = 1;
while (b > 0) {
if (b & 1)
res = (res * a)%mod;
a = (a * a)%mod;
b >>= 1;
}
return res%mod;
}
void fact(lli i)
{
f[0]=1;
for(lli k=1;k<=i;k++)
{
(f[k]=f[k-1]*k)%=mod;
}
}
lli isprime(lli n)
{
if(n==1)
return 0;
for(lli i=2;i<=sqrt(n);i++)
if(n%i==0)
return 0;
return 1;
}
lli find(lli x)
{
if(f[x]==x)
return x;
else
return f[x]=find(f[x]);
}
bool cmp(lli x,lli y)
{
return x<y;
}
lli comb(lli i,lli j)
{
lli k=f[i];
lli g=(f[j]*(f[i-j]))%mod;
lli h=bpow(g,mod-2);
return (k*h)%mod;
}
/*void sieve()
{
for(lli i=2;i<=100005;i++)
{
if(b[i]==0)
{
k.pb(i);
for(lli j=2;i*j<=100005;j++)
{
b[i*j]=1;
}
}
}
}*/
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
lli n;cin>>n;
if(n%2)
{
cout<<"Black"<<endl;
}
else
{
cout<<"White"<<endl;
}
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
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; }
typedef long long ll;
using namespace std;
int main(void){
sp(10);
ll n;cin>>n;
long double N=n;
vector<long double> dp(n+1,0);
dp[1]=0;
rep(i,n){
if(i==0)continue;
long double I=i;
dp[i+1]=dp[i]+N/(N-I);
//cout<<N/(N-I)<<endl;
}
cout<<dp[n]<<endl;
} |
/**
* For the brave souls who get this far: You are the chosen ones,
* the valiant knights of programming who toil away, without rest,
* fixing our most awful code. To you, true saviors, kings of men,
* I say this: never gonna give you up, never gonna let you down,
* never gonna run around and desert you. Never gonna make you cry,
* never gonna say goodbye. Never gonna tell a lie and hurt you.
*/
#include<bits/stdc++.h>
#define pb push_back
#define nw cout<<"\n";
#define srt(v) sort((v).begin(),(v).end())
#define rvrs(v) reverse((v).begin(),(v).end())
#define mpr make_pair
#define mt make_tuple
#define loop(i,j,n) for (int i = j; i < n; ++i)
#define loop1(i,j,n) for(int i=j;i<=n;++i)
#define wis(x) std::cerr << #x << " is " << x << std::endl;
using namespace std;
typedef long long ll;
int main ( void )
{
time_t Time;
time(&Time);
cerr<<ctime(&Time);
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
cout<<fixed;
auto starttime=chrono::steady_clock::now();
string str;
cin >> str;
ll m;
cin>> m;
ll d=*max_element(str.begin(),str.end())-'0';
if(str.size()==1) {
if(d<=m)
cout<< 1;
else
cout<<0;
}
else {
ll first = d+1, last = (ll)1e18;
while(first<=last) {
__int128 number =0;
__int128 mid=(first+last)/2;
for(int i=0;i<(int)str.size();i++) {
number*= mid;
number+=str[i]-'0';
if(number > m)
break;
}
if(number<=m)
first = mid+1;
else
last = mid-1;
}
cout<< last - d;
}
auto endtime=chrono::steady_clock::now();
cerr<<endl<<"Elapsed Time: "
<<chrono::duration_cast<chrono::milliseconds> (endtime-starttime).count()
<<" milliseconds"<<endl;
return 0;
}
| #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
bool flag=1;
int x=0;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag=0;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return (flag?x:~(x-1));
}
string s;
int k,maxx;
vector<int> v;
bool judge(__int128 x)
{
__int128 ans=0;
for(int i:v)
{
ans=ans*x+i;
if(ans>k)
return 0;
}
return 1;
}
signed main()
{
cin>>s;
for(int i=0;i<s.size();i++)
{
v.push_back(s[i]-'0');
maxx=max(maxx,(long long)s[i]-'0');
}
cin>>k;
if(v.size()==1)
{
cout<<(v[0]<=k);
return 0;
}
int l=maxx+1;
int r=2e18;
while(l<=r)
{
__int128 mid=(l+r)>>1;
if(judge(mid))
l=mid+1;
else
r=mid-1;
}
cout<<(l-maxx-1);
return 0;
}
|
#include <bits/stdc++.h>
#define XOX
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
#define Time cerr << "Time Taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl
#define ll long long
#define beg() begin()
#define test int t=1; cin>>t; while(t--)
#define vll vector<long long>
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define pb push_back
#define forr(i,a,b) for (ll i = (a); i < (b); ++i)
#define rrof(i,a,b) for (ll i = (b)-1; i >= (a); --i)
#define mod 1000000007
void solve( );
void swap(ll &a, ll &b);
ll min(ll &a, ll &b);
ll max(ll &a, ll &b);
void exp(ll b);
ll sqrt(ll a);
ll GCD(ll a, ll b) ;
struct node{
int freq;
node* child[26];
node(){
freq=0;
for(int i=0;i<26;i++) child[i]=NULL;
}
};
int main() {
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// node *root = new node() ; node *temp = root ;
// string s;
// cin>>s;
// for(int i=0;i<s.length();i++){
// if(temp->child[(s[i]-'a')]==NULL){
// node *t = new node();
// temp->child[(s[i]-'a')] = t;
// temp=t; temp->freq++;
// }else{
// temp = temp->child[(s[i]-'a')];
// temp->freq++;
// }
// }
// test{
// string q; cin>>q;
// if()
// }
solve();
Time;
return 0;
}
void solve(){
ll n,s,d,x,y; cin>>n>>s>>d;
forr(i,0,n){
cin>>x>>y; if(x<s && y>d ){cout<<"Yes"; return; }
}
cout<<"No";
}
void swap(ll &a, ll &b){
if(a==b) return ;
a=a^b;
b=a^b;
a=a^b;
return ;
}
ll min(ll &a, ll &b){
if(a>b) return b;
return a;
}
ll max(ll &a, ll &b){
if(a<b) return b;
return a;
}
ll sqrt(ll a){
ll l=1,h=a, m=l+(h-l+1)/2;
while(l<h){
if(m > a/m ) h=m-1;
else l=m;
m=l+(h-l+1)/2;
}
return m;
}
ll GCD(ll a, ll b) {
if(b==0) return a;
else return GCD(b, a%b) ;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
const ll LINF = 1ll << 55;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
/// cin/cout overloading
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
out << *it << " ";
}
return out;
}
template <typename T>
ostream &operator<<(ostream &out, const pair<T, T> &P) {
out << P.first << " " << P.second;
return out;
}
template <typename T>
istream &operator>>(istream &in, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
in >> *it;
}
return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &P) {
in >> P.first >> P.second;
return in;
}
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
cerr << endl; \
}
void err(istream_iterator<string>) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
#define all(x) (x).begin(), (x).end()
#define coutp(x, y) cout << (x) << " " << (y) << endl
#define coutn(x) cout << (x) << endl
#define coutd(x) cout << setprecision(10) << (x) << endl
/// main函数
int main() {
ll N;
cin >> N;
ll ans = 0;
ll sqsum = 0;
ll sumsq = 0;
ll A;
for (int i = 0; i < N; ++i) {
cin >> A;
sqsum += A * A;
sumsq += A;
}
sumsq *= sumsq;
ans = N * sqsum - sumsq;
coutn(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define P pair<int,char>
#define Tp tuple<int,int,int>
using Graph = vector<vector<P>>;
int main(){
int N,M; cin >> N >> M;
int a,b; char c;
Graph g(N+1);
rep(i,M){
cin >> a >> b >> c;
g[a].push_back(make_pair(b,c));
g[b].push_back(make_pair(a,c));
}
queue<tuple<int,int,int>> go;
bool vis[N+1][N+1];
rep(i,N+1){
rep(j,N+1){
vis[i][j] = false;
}
}
vis[1][N] = true; go.push(make_tuple(0,1,N));
int nowk = 0; bool fin = false;
while(!go.empty()){
auto p = go.front(); go.pop();
a = get<1>(p); b = get<2>(p);
int k = get<0>(p);
if(k>nowk){
nowk = k;
if(fin){
cout << 2*k-1 << endl; return 0;
}
}
if(a==b){
cout << 2*k << endl; return 0;
}
for(auto q:g[a]){
int x = q.first; char c1 = q.second; int y;
for(auto r:g[b]){
y = r.first; char c2 = r.second;
if(x==b&&y==a){
fin = true;
}
if(c1!=c2||vis[x][y]) continue;
go.push(make_tuple(k+1,x,y)); vis[x][y] = true;
}
}
}
cout << -1 << endl;
} | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
const int mod = 998244353;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
os << p.first << " " << p.second;
return os;
}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
is >> p.first >> p.second;
return is;
}
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < (int) v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template<typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &in : v) is >> in;
return is;
}
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template<typename T = int64>
vector<T> make_v(size_t a) {
return vector<T>(a);
}
template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template<typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t) fill_v(e, v);
}
template<typename F>
struct FixPoint : F {
FixPoint(F &&f) : F(forward<F>(f)) {}
template<typename... Args>
decltype(auto) operator()(Args &&... args) const {
return F::operator()(*this, forward<Args>(args)...);
}
};
template<typename F>
inline decltype(auto) MFP(F &&f) {
return FixPoint<F>{forward<F>(f)};
}
int main() {
int N, L;
cin >> N >> L;
vector< int > A(N + 2), B(N + 2);
for(int i = 1; i <= N; i++) cin >> A[i];
for(int i = 1; i <= N; i++) cin >> B[i];
A[N + 1] = L + 1;
B[N + 1] = L + 1;
for(int i = 0; i < N + 2; i++) A[i] -= i;
for(int i = 0; i < N + 2; i++) B[i] -= i;
map< int, int > ls, rs;
for(int i = 0, j; i < N + 2; i = j) {
for(j = i; j < N + 2 and A[i] == A[j]; j++) ;
ls[A[i]] = i;
rs[A[i]] = j;
}
int64 ret = 0;
for(int i = 0, j; i < N + 2; i = j) {
for(j = i; j < N + 2 and B[i] == B[j]; j++) ;
if(!ls.count(B[i])) {
cout << -1 << endl;
exit(0);
}
ret += max(ls[B[i]] - i, 0);
ret += max(j - rs[B[i]], 0);
}
cout << ret << endl;
}
|
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define dbl(k, x) fixed << setprecision(k) << (x)
template <typename _T>
inline void _DBG(const char *s, _T x) {
cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
while (*s != ',') cerr << *s++;
cerr << " = " << x << ',';
_DBG(s + 1, a...);
}
#define _upgrade \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__)
// ********************** CODE ********************** //
bool check_cons_zeros(string t) {
int cons_zeros = 0;
for (auto &c : t) {
if (c == '0') {
cons_zeros++;
} else {
cons_zeros = 0;
}
if (cons_zeros > 1) {
return true;
}
}
return false;
}
bool check_cons_ones(string t) {
int cons_ones = 0;
for (auto &c : t) {
if (c == '1') {
cons_ones++;
} else {
cons_ones = 0;
}
if (cons_ones > 2) {
return true;
}
}
return false;
}
bool check_one_surr(string t) {
for (int i = 1; i < t.length() - 1; i++) {
if (t[i] == '1' && t[i - 1] == '0' && t[i + 1] == '0') {
return true;
}
}
return false;
}
int main() {
_upgrade;
int n;
cin >> n;
string t;
cin >> t;
if (check_cons_zeros(t) || check_cons_ones(t) || check_one_surr(t)) {
// cerr << check_cons_zeros(t) << "\n";
// cerr << check_cons_ones(t) << "\n";
// cerr << check_one_surr(t) << "\n";
cout << 0 << "\n";
return 0;
}
long long res = 1e10; // change
if (t.length() == 1) {
if (t[0] == '0') {
cout << res << "\n";
} else {
cout << 2 * res << "\n";
}
} else {
if (t[0] == '1' && t[1] == '1') {
cout << res - (t.length() + 2) / 3 + 1 << "\n";
} else if (t[0] == '1' && t[1] == '0') {
long long need = 1 + (t.length() - 2 + 2) / 3;
cout << res - (need - 1) << "\n";
} else {
long long need = 1 + (t.length() - 1 + 2) / 3;
cout << res - (need - 1) << "\n";
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define print(x) cout << (x) << '\n'
typedef long long ll;
using P = pair<int,int>;
using Graph = vector<vector<int>>;
//const ll MOD = 1000000007;
const ll MOD = 998244353;
template <typename T> inline bool chmax(T &a, T b) {return a < b ? a = b, true : false;}
template <typename T> inline bool chmin(T &a, T b) {return a > b ? a = b, true : false;}
const int MAX = 500000;
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
ll COM(ll n, ll k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
//xのn乗mod(m)
ll pom(ll x, ll n, ll m) {
ll ret = 1LL;
while (n > 0) {
if (n & 1LL) ret = ret * x % m;
x = x * x % m;
n >>= 1;
}
return ret;
}
void solve() {
ll n, m; cin >> n >> m;
//末項の値
ll ans = 1;
vector<bool> c(m + 1, true);
for(ll i = 2; i <= m; i++){
int tmp = i;
ll k = 0;
ll t = 1;
for(int j = 2; j * j <= m; j++){
if(tmp % j == 0){
ll k = 0;
while(tmp % j == 0){
k++;
tmp /= j;
}
t *= COM(n - 1 + k, k);
t %= MOD;
}
if(tmp == 1) break;
}
if(tmp > 1){
t *= n;
t %= MOD;
}
ans += t;
}
print(ans % MOD);
}
int main() {
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(18);
int t;
//cin >> t;
t = 1;
COMinit();
while(t--) solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define pd push_back
#define sort(v) sort(v.begin(),v.end())
#define Sort(v) sort(v.begin(),v.end(),greater<int>())
#define display(s) for(auto val:s) cout<<val<<" "; cout<<endl;
#define displayMap(mymap) for(auto val:mymap) cout<<val.first<<" -> "<<val.second<<endl;
#define Sortcom(v) sort(v.begin(),v.end(),mycompare)
#define f(a,b,c) for(int i=a;i<b;i+=c)
bool mycompare(int a,int b)
{
return a<b;
}
int mod(int x)
{
return (x%MOD+MOD)%MOD;
}
int mul(int a,int b)
{return mod(mod(a)*mod(b));}
int add(int a,int b)
{return mod(mod(a)+mod(b));}
int power(int a,int b)
{
if(b==0)
return 1;
int smallans = power(a,b/2);
smallans = smallans * smallans;
if(b&1)
smallans = smallans * a;
return smallans;
}
int solve(){
int n,k; cin>>n>>k;
vector<int> v(n,0);
map<int,int> mymap;
for(int i=0;i<n;i++)
{
cin>>v[i];
mymap[v[i]]++;
}
int ans = 0;
int boxes = 0;
while(boxes<k)
{
int val = 0;
bool flag = true;
while(mymap[val]!=0)
{
mymap[val]--;
val++;
flag = false;
}
ans += val;
boxes++;
if(flag)
break;
}
return ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t; t=1;
// int t; cin>>t;
while(t--)
{
// solve();
cout<<solve()<<endl;
}
return 0;
} | #define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
/*
#include<vector>
#include<iostream>
//*/
using namespace std;
#define ll long long
#define yes {cout<<"Yes"<<endl;return 0;}
#define no {cout<<"No"<<endl;return 0;}
#define YES {cout<<"YES"<<endl;return 0;}
#define NO {cout<<"NO"<<endl;return 0;}
#define cyes {cout<<"Yes"<<endl;continue;}
#define cno {cout<<"No"<<endl;continue;}
#define MOD 1000000007
#define MOD2 1000000009
#define MOD3 998244353
#define INF 2147483647
constexpr double PI = 3.141592653589793238462643383279;
#define LINF 9223372036854775807
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep1(i,n) for (int i = 1; i < (n); ++i)
#define PF push_front
#define PB push_back
#define POF pop_front
#define POB pop_back
#define MP make_pair
#define MT make_tuple
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define SZ(a) (int)((a).size())
#define NEXP(a) next_permutation((a).begin(),(a).end())
#define FI first
#define SE second
#define SORT(c) sort((c).begin(),(c).end())
#define SORTR(c) sort((c).rbegin(), (c).rend())
#define REVERSE(c) reverse((c).begin(),(c).end())
typedef long long LL;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<char> VC;
typedef vector<VC> VVC;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const vector<int> dx = {1,0,-1,0,1,1,-1,-1};
const vector<int> dy = {0,1,0,-1,1,-1,1,-1};
int main(void)
{
int i,j;
int n,k;
cin>>n>>k;
VI a(n);
VI cnt(n);
rep(i,n){cin>>a[i];cnt[a[i]]++;}
VI dp(n);
int mini=cnt[0];
rep(i,n)
{
dp[i]=min(mini,cnt[i]);
mini=dp[i];
}
int used=0,ans=0;
for(i=n-1;i>=0;i--)
{
if(min(dp[i],k)-used>0)
{
ans+=(i+1)*(min(k,dp[i])-used);
used=min(dp[i],k);
}
}
cout<<ans<<endl;
}
|
/* bimillahir rahmanir rahim...
* AUTHOR
* mubin_akib */
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair <int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b) - 1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a) - 1; i >= 0; i--)
#define REP(i, a, b) for(int i = a; i <= (b); ++i)
#define trav(a,x) for(auto& a : x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define pi acos(-1)
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7;
const char nl = '\n';
void solve(){
int n, w;
cin >> n >> w;
cout << n / w << nl;
}
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);
solve();
}
| #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#include <math.h>
#define ll long long
#define ull unsigned long long
using namespace std;
int main() {
int N, W;
cin >> N >> W;
cout << N / W << "\n";
return 0;
}
|
//pikuskd_32->42
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long
#define fr(i,s,e) for(ll i=s;i<=e;i++)
#define rfo(i,s,e) for(ll i=s;i>=e;i--)
#define f(x,m) for(auto x : m)
#define ff first
#define ss second
const ll INF = (1LL << 60) - 1;
#define pb push_back
#define pp pop_back
#define pf push_front
#define ppf pop_front
#define m_p make_pair
#define mod 1000000007
#define TIME cerr<<"Time Taken:"<<(float)clock()/CLOCKS_PER_SEC*1000<<"ms"<<endl
// __builtin_popcount(n)
// cout << fixed << setprecision(2) << a << endl; ->'a' is floating point.
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif
ll t = 1; //cin>>t;
while (t--) {
ll n; cin >> n;
vector<pair<ll, ll>> v;
ll x, y;
fr(i, 0, n - 1) {
cin >> x >> y; v.pb(m_p(x, y));
}
ll c = 0;
fr(i, 0, n - 1) {
fr(j, i + 1, n - 1) {
if (abs(v[j].ss - v[i].ss) <= abs(v[j].ff - v[i].ff)) c++;
}
}
cout << c << endl;
}
TIME;
return 0;
} | #include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <map>
#include <assert.h>
#include <stack>
#include <string>
using namespace std;
int power[31]={0};
string cmp(string s)
{
string res="";
for (int j=0; j<s.size(); j++)
{
res+='0'+(1-(s[j]-'0'));
}
return res;
}
vector<string> f(int x)
{
if (x==1)
{
return {"01"};
}
vector<string> p=f(x-1);
vector<string> v;
string s="";
for (int j=1; j<=power[x-1]; j++)
{
s+='0';
}
for (int j=1; j<=power[x-1]; j++)
{
s+='1';
}
v.push_back(s);
for (int j=0; j<p.size(); j++)
{
string r=p[j];
string f = r + r;
r+=cmp(p[j]);
v.push_back(r);
v.push_back(f);
}
return v;
}
void solve()
{
int n;
cin>>n;
vector<string> res = f(n);
cout<<(int)res.size()<<"\n";
for (auto s: res)
{
for (auto i : s)
{
cout << (char)('A' + (i-'0'));
}
cout << "\n";
}
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
int t;
//cin >> t;
t=1;
power[0]=1;
for (int j=1; j<=30; j++) power[j]=power[j-1]*2;
while (t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define save_some_time ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define ll long long int
void V_in(vector <ll> &V, int n) {
for (int i = 0; i < n; i++) {
int a; cin >> a;
V.pb(a);
}
}
void V_out(vector <ll> V) {
for (int i = 0; i < V.size(); i++) {
cout << V[i] << " ";
}
}
int main() {
save_some_time;
int n; cin >> n;
cout << 100-n%100;
} | #include <bits/stdc++.h>
#define ll long long
#define mod 1e9 + 7
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll n;cin>>n;
ll rem = n%100;
if(rem==0){
cout<<100;
}else{
cout<<100-rem;
}
return 0;
}
|
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
void get(ll i, ll arr[], ll n, ll xo, ll o, ll &ans)
{
if(i == n)
{
ans = min(ans, xo^o);
return;
}
get(i+1, arr, n, xo, o|arr[i], ans);
get(i+1, arr, n, xo^o, arr[i], ans);
}
const ld PI = acos(-1);
int main()
{
FAST/**/
ll n;
cin>>n;
ld x0,y0,x1,y1;
cin>>x0>>y0>>x1>>y1;
ld cx = (x0+x1)/2, cy = (y0+y1)/2;
x0-=cx, y0-=cy;
ld ang = (2*PI)/n;
ld fx = (x0*cos(ang) - y0*sin(ang)), fy = (x0*sin(ang) + y0*cos(ang));
fx+=cx, fy+=cy;
cout<<fixed<<setprecision(12)<<fx<<" "<<fy<<'\n';
return 0;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
const int O = 2e5 + 5;
int tree[2][O * 4], t[2][O * 4];
vector <int> val;
void Update(int id, int l, int r, int u, int v, int k){
if (l > u || r < u) return;
if (l == r){
tree[k][id] += val[l] * v;
t[k][id] += v;
return;
}
int mid = (l + r) / 2;
Update(id << 1, l, mid, u, v, k);
Update(id << 1 | 1, mid + 1, r, u, v, k);
tree[k][id] = tree[k][id << 1] + tree[k][id << 1 | 1];
t[k][id] = t[k][id << 1] + t[k][id << 1 | 1];
}
pair <int, int> Get(int id, int l, int r, int u, int v, int k){
if (l > v || r < u) return make_pair(0, 0);
if (u <= l && r <= v) return make_pair(tree[k][id], t[k][id]);
int mid = (l + r) / 2;
auto x = Get(id << 1, l, mid, u, v, k);
auto y = Get(id << 1 | 1, mid + 1, r, u, v, k);
return make_pair(x.first + y.first, x.second + y.second);
}
main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, m, q; cin >> n >> m >> q;
vector <vector <int>> pre(2, vector <int> (max(n, m) + 1));
vector <vector <int>> sum(2, vector <int> (max(n, m) + 1));
vector <tuple <int, int, int>> queries; val.push_back(0);
for (int i = 1; i <= q; ++ i){
int t, x, y; cin >> t >> x >> y;
queries.push_back(make_tuple(t, x, y));
val.push_back(y);
}
sort(val.begin(), val.end());
auto cac = unique(val.begin(), val.end());
val.resize(distance(val.begin(), cac));
int ans = 0, h = val.size() - 1;
Update(1, 0, h, 0, n, 0);
Update(1, 0, h, 0, m, 1);
for (int i = 0; i < q; ++ i){
int t = get<0>(queries[i]);
int r = get<1>(queries[i]);
int x = pre[t - 1][r];
int y = get<2>(queries[i]);
t -= 1;
int idx = upper_bound(val.begin(), val.end(), x) - val.begin() - 1;
int idy = upper_bound(val.begin(), val.end(), y) - val.begin() - 1;
auto X = Get(1, 0, h, 0, idx, t ^ 1);
auto Y = Get(1, 0, h, 0, idy, t ^ 1);
auto greatX = Get(1, 0, h, idx + 1, h, t ^ 1);
auto greatY = Get(1, 0, h, idy + 1, h, t ^ 1);
Update(1, 0, h, idx, -1, t);
Update(1, 0, h, idy, 1, t);
ans = ans - (x * X.second - X.first) + (y * Y.second - Y.first);
pre[t][r] = y;
cout << ans << "\n";
}
}
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i=0; i<(n); i++)
using namespace std;
typedef long long ll;
ll K, N, M, A[(int)1e5], B[(int)1e5];
ll check(ll x) {
ll sumL = 0, sumR = 0;
REP(i, K)
sumL += max(0LL, (M * A[i] - x + N - 1) / N);
REP(i, K)
sumR += (M * A[i] + x) / N;
return (sumL <= M && M <= sumR);
}
void constructer(ll x) {
ll R[K];
REP(i, K)
B[i] = max(0LL, (M * A[i] - x + N - 1) / N);
REP(i, K)
R[i] = (M * A[i] + x) / N;
ll sumB = 0;
REP(i, K) sumB += B[i];
REP(i, K) {
ll x = min(M - sumB, R[i] - B[i]);
B[i] += x;
sumB += x;
}
}
int main() {
cin >> K >> N >> M;
REP(i, K)
cin >> A[i];
ll ng = -1, ok = N * M;
while (ng + 1 < ok) {
ll x = (ng + ok) / 2;
if (check(x))
ok = x;
else
ng = x;
}
constructer(ok);
REP(i, K)
cout << B[i] << " ";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int maxn = 1e5+9;
int k,n,m;
ll A[maxn],B[maxn];
int main(){
cin>>k>>n>>m;
int sum=0;
priority_queue<pair<int,int>>q;
for(int i=1;i<=k;i++){
cin>>A[i];
B[i]=((1.0*m)/n)*A[i];
sum+=B[i];
q.push({abs(n*B[i]-m*A[i]),i});
}
int x=m-sum;
while(x--){
B[q.top().second]++;
q.pop();
}
for(int i=1;i<=k;i++)
cout<<B[i]<<" ";
return 0;
} |
//------------------------------------------
// C++ template
//------------------------------------------
#include <bits/stdc++.h>
#define lint long long int
using namespace std;
//typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;
//REPEAT
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
//container util
//------------------------------------------
#define pb push_back
#define paired make_pair
#define ALL(a) (a).begin(),(a).end()
#define PRINT(V) for (auto v : (V)) cout << v << " "
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
//constant
//------------------------------------------
const int MOD = 1000000007;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI = acos(-1.0);
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
//math
//------------------------------------------
int QP(int a,int b){int ans=1;do{if(b&1)ans=1ll*ans*a%MOD;a=1ll*a*a%MOD;}while(b>>=1);return ans;}
int QP(int a,int b,int MOD){int ans=1;do{if(b&1)ans=1ll*ans*a%MOD;a=1ll*a*a%MOD;}while(b>>=1);return ans;}
int GCD(int a,int b){return b?GCD(b,a%b):a;}
int main(){
int a,b,c;
cin >> a >> b >> c;
bool winner;
if(a-b>=1||(a==b&&c==1)) winner = true;
else winner = false;
if(winner) cout << "Takahashi" << endl;
else cout << "Aoki" << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main (){
ios::sync_with_stdio(0);cin.tie(0);
int a,b,c;
cin>>a>>b>>c;
if (a+c>=b+1) cout<<"Takahashi\n";
else cout<<"Aoki\n";
} |
/*input
4
4 2 3 1
2 3 2 4
*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
const ll maxn=1e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=3.14159265358979323846;
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define MP make_pair
ll mult(ll a,ll b){
return ((a%MOD)*(b%MOD))%MOD;
}
ll mypow(ll a,ll b){
ll res=1LL;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b>>=1;
}
return res;
}
int a[maxn],b[maxn];
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int n;
cin>>n;
REP(i,n){
cin>>a[i];
}
REP(i,n) cin>>b[i];
vector<int> v[2];
ll sum=0;
REP(i,n){
sum+=a[i];
v[i%2].pb(b[i]-a[i]);
}
sort(ALL(v[0]));
sort(ALL(v[1]));
reverse(ALL(v[0]));
reverse(ALL(v[1]));
ll ans=sum;
REP(i,n/2){
sum+=v[0][i]+v[1][i];
MXTO(ans,sum);
}
cout<<ans;
} | #include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define ll long long int
#define sd(x) scanf("%lld",&x)
#define sdi(x) scanf("%d",&x)
#define sdc(c) scanf("%c",&c)
#define inf 1000000000000000000ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define bits(x) __builtin_popcountll(x)
#define ld long double
#define test() ll test; cin>>test; while(test--)
#define fi first
#define se second
#define all(x) x.begin(),x.end()
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
clock_t time_p = clock();
void time_taken()
{
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
inline ll GCD(ll x, ll y) {
if(x<y) swap(x,y);
if(x==0) return y;
if(y==0) return x;
return GCD(x%y,y);
}
ll phi(ll n) {
ll result = n;
for (ll 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;
}
ll power(ll x, ll n, ll mod) {
ll res = 1;
x %= mod;
while(n) {
if(n&1) {
res = ((res*x)%mod+mod)%mod;
}
x = ((x*x)%mod+mod)%mod;
n>>=1;
}
return res;
}
const int MOD = 1e9+7;
inline ll add(ll x, ll y, ll MOD) {
x %= MOD;
y %= MOD;
ll ans = (x+y)%MOD;
return ans;
}
inline ll mul(ll x,ll y, ll MOD) {
x %= MOD;
y %= MOD;
ll ans = ((x*y)%MOD+MOD)%MOD;
return ans;
}
int check(int x, vector<vector<int> > &person) {
set<int> s;
for(int i=0;i<person.size();i++) {
int bit = 0;
for(int j=0;j<5;j++) {
bit <<= 1;
bit |= (person[i][j]>=x);
}
s.insert(bit);
}
vector<int> v;
while(!s.empty()) {
v.pb(*s.begin());
s.erase(s.begin());
}
for(int i=0;i<v.size();i++) {
for(int j=0;j<v.size();j++) {
for(int k=0;k<v.size();k++) {
if((v[i]|v[j]|v[k]) == 31) return 1;
}
}
}
return 0;
}
int main() {
fastio;
int N;
cin>>N;
vector<vector<int> > person(N,vector<int>(5));
int a;
for(int i=0;i<N;i++) {
for(int j=0;j<5;j++) {
cin>>a;
person[i][j] = a;
}
}
int ans = 0;
int l = 1, h = 1000000000;
while(l<=h) {
int mid = (l+h)/2;
if(check(mid,person)) {
ans = mid;
l = mid+1;
} else {
h = mid-1;
}
}
cout<<ans<<endl;
time_taken();
}
|
#include <algorithm>
#include <bitset>
#include <complex>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TU = tuple<int, int, int>;
const int INF = 1000000000;
const int MOD = 1000000007;
int main() {
ll A, B, K;
cin >> A >> B >> K;
vector<vector<ll>> dp(31, vector<ll>(31, 1));
for (ll i = 1;i <= 30;i++) {
dp.at(i).at(1) = i + 1;
dp.at(1).at(i) = i + 1;
}
for (ll i = 2;i <= 30;i++) {
for (ll j = 2;j <= 30;j++) {
dp.at(i).at(j) = dp.at(i - 1).at(j) + dp.at(i).at(j - 1);
}
}
ll Arest = A, Brest = B;
ll cnt = 0;
string ans;
vector<ll> vec;
while (ans.size()<A+B) {
ll buf = 0;
for (ll i = 0;i <= Arest;i++) {
if (Brest == 0) {
for (ll k = 0;k < Arest;k++) {
ans.push_back('a');
}
break;
}
buf += dp.at(i).at(Brest - 1);
if (buf + cnt >= K) {
for (ll j = 0;j < Arest - i;j++) {
ans.push_back('a');
}
ans.push_back('b');
cnt += buf - dp.at(i).at(Brest - 1);
Arest = i;
Brest--;
}
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x;}
ll mulmod(ll a, ll b) {ll res = 0; a %= mod; assert(b >= 0); for (; b; b >>= 1) {if (b & 1)res = (res + a) % mod; a = 2 * a % mod;} return res;}
ll powmod(ll a, ll b) {ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) {if (b & 1)res = res * a % mod; a = a * a % mod;} return res;}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;}
//head
//editorial:https://atcoder.jp/contests/abc202/editorial/1893
const int N = 30;
ll dp[N + 1][N + 1];
string find(int a, int b, ll k) {
if (a == 0) return string(b, 'b'); //一个string,b个 'b'
if (b == 0) return string(a, 'a');
//当前层dp是a个'a',b个'b',多一个b就要少一个a,当前字符串排序大于k就加'a',反之加'b'
if (k <= dp[a - 1][b]) return string("a") + find(a - 1, b, k);
else return string("b") + find(a, b - 1, k - dp[a - 1][b]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int a, b;
ll k;
cin >> a >> b >> k;
//组合数dp[i][j]意思是从长i宽j的方格,走到(i,j)的走法数量
//本质是:C(i+j,j) = C(i+j-1,j) + C(i+j-1, j-1)
//dp[i][j] -> C(i + j, j)
dp[0][0] = 1;
for (int i = 0; i <= a; i++)
for (int j = 0; j <= b; j++) {
if (i > 0) dp[i][j] += dp[i - 1][j];
if (j > 0) dp[i][j] += dp[i][j - 1];
}
cout << find(a, b, k) << endl;
return 0;
} |
#include<bits/stdc++.h>
typedef long long ll;
#define int ll
#define pii pair<int,int>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const double pi=acos(-1.0);
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+10;
const int mod=1e9+7;
signed main()
{
std::ios::sync_with_stdio(false);
int a,b,w;
cin>>a>>b>>w;
w*=1000;
int minn=w/b;
int maxx=w/a;
int flag=1;
int tmp=minn*b;
if(w-tmp>minn*(b-a))
flag=0;
tmp=maxx*a;
if(w-tmp>minn*(b-a))
flag=0;
if(flag)
{
if(minn*b!=w) minn++;
cout<<minn<<" "<<maxx;
}
else
{
cout<<"UNSATISFIABLE";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;
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 get_count(ll N, ll x, VL& sum_two) {
ll count = 0;
FOR(i, 1, N) {
if(i >= x) break;
count += sum_two[x - i];
}
return count;
}
void solve(long long N, long long K) {
VL count_two(3 * N + 1, 0);
VL sum_two(3 * N + 2, 0);
FOR(i, 2, 3 * N) {
count_two[i] = max(0ll, min(N, ll(i - 1)) - max(1ll, ll(i - N)) + 1ll);
};
FOR(i, 2, 3 * N) {
sum_two[i + 1] += sum_two[i] + count_two[i];
}
ll ok = 0;
ll ng = 3 * N + 1;
while(abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if(get_count(N, mid, sum_two) < K) {
ok = mid;
} else {
ng = mid;
}
}
K -= get_count(N, ok, sum_two);
ll x = 1, y = 1, z = 1;
ll tmp_count = 0;
FOR(i, 1, N) {
if(tmp_count + count_two[ok - i] >= K) {
K -= tmp_count;
x = i;
break;
}
tmp_count += count_two[ok - i];
}
tmp_count = 0;
FOR(i, 1, N) {
if(ok - x - i >= 1 && ok - x - i <= N) tmp_count++;
if(tmp_count == K) {
y = i;
break;
}
}
z = ok - x - y;
cout << x << " " << y << " " << z << endl;
}
int main() {
long long N;
scanf("%lld", &N);
long long K;
scanf("%lld", &K);
solve(N, K);
return 0;
}
|
#include <bits/stdc++.h>
template <typename T> inline void rd(T& x) {
int si = 1; char c = getchar(); x = 0;
while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int N = 1e5 + 5, B = 14, Inf = 0x3f3f3f3f, Mod = 998244353;
const ll InfLL = 0x3f3f3f3f3f3f3f3fLL;
int QPow(int a, int b) {
int ret = 1, bas = a;
for(; b; b >>= 1, bas = 1LL * bas * bas % Mod)
if(b & 1) ret = 1LL * ret * bas % Mod;
return ret;
}
int n, m, f[B][N], fac[N], fac_inv[N];
void Init() {
fac[0] = 1;
for(int i = 1; i <= n; ++i) fac[i] = 1LL * fac[i - 1] * i % Mod;
fac_inv[n] = QPow(fac[n], Mod - 2);
for(int i = n - 1; ~i; --i) fac_inv[i] = 1LL * fac_inv[i + 1] * (i + 1) % Mod;
}
int C(int x, int y) {
return 1LL * fac[x] * fac_inv[x - y] % Mod * fac_inv[y] % Mod;
}
void Calc() {
f[0][0] = 1;
for(int i = 1; i < B; ++i) {
for(int j = 0; j <= m; ++j) {
for(int k = 0; k * (1 << (i - 1)) <= j && k <= n; k += 2)
f[i][j] = (1LL * f[i - 1][j - k * (1 << (i - 1))] * C(n, k) + f[i][j]) % Mod;
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int test_case_cnt = 1; //rd(test_case_cnt);
while(test_case_cnt--) {
rd(n, m); Init(); Calc();
printf("%d\n", f[B - 1][m]);
} return 0;
} | # include <bits/stdc++.h>
using namespace std;
int a[10][10];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> a[i][j];
}
}
vector<int> v;
for (int i = 1; i < n; ++i) {
v.push_back(i);
}
int cnt = 0;
do {
int total = 0 ;
total += a[0][v[0]];
for (int i = 1; i < v.size(); ++i) {
total += a[v[i - 1]][v[i]];
}
total += a[0][v[v.size() - 1]];
if (total == k) {
cnt += 1;
}
} while (next_permutation(v.begin(), v.begin() + v.size()));
cout << cnt << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define sd second
#define ft first
#define is insert
#define endl '\n'
#define all(x) x.begin(),x.end()
#define PB push_back
#define MP make_pair
const long double Pi=(ld)acos((ld)-1);
ll int power(ll int x,ll int y){ll int temp;if(y==0)return 1;temp=power(x,y/2);if(y%2==0)return temp*temp;else return x*temp*temp;}
ll int gcd(ll int a,ll int b){if(b==0){return a;}else{return gcd(b,a%b);}}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// ll int t;
// cin>>t;
// while(t--)
// {
ll int n,k,c=0;
cin>>n>>k;
vector<pair<ll int,ll int>>vc;
ll int a=k;
for(ll int i=0;i<n;i++)
{
ll int i1,i2;
cin>>i1>>i2;
vc.PB(MP(i1,i2));
}
sort(vc.begin(),vc.end());
for(ll int i=0;i<n;i++)
{
if(k>=(vc[i].ft))
{
k+=vc[i].sd;
// k-=vc[i].ft;
a+=vc[i].sd;
}
else
{
break;
}
// cout<<k<<" || ";
}
cout<<a;
// }
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素 //遅延セグ木使うときは注意
#define S second //pairの二つ目の要素 //遅延セグ木使うときは注意
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;//10の18乗
#define yn {puts("Yes");}else{puts("No");}
#define dame { puts("-1"); return 0;}
int main() {
ll n,k;
cin >> n >> k;
map<ll,ll> ma;
rep(i,n){
ll x,y;
cin >> x >> y;
ma[x]+=y;
}
ll now=k;
ll ans=0;
for(auto p:ma){
ll x=p.F,y=p.S;
if(ans+now<x){
cout << ans+now << endl;
return 0;
}
else{
now-=(x-ans);
ans=x;
now+=y;
}
}
cout << now+ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int r1, c1, r2, c2;
void solve() {
if (r1 == r2 && c1 == c2) {
printf("0\n");
return;
}
if (r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2 || abs(r1 - r2) + abs(c1 - c2) <= 3) {
printf("1\n");
return;
}
if ((r1 + c1 + r2 + c2) % 2 == 0 || abs(r1 - r2) + abs(c1 - c2) <= 6 || abs((r1 + c1) - (r2 + c2)) <= 3 ||
abs((r1 - c1) - (r2 - c2)) <= 3) {
printf("2\n");
return;
}
printf("3\n");
}
int main() {
cin >> r1 >> c1 >> r2 >> c2;
solve();
} | #include <bits/stdc++.h>
using namespace std;
int r1, c1, r2, c2;
bool is_in(int x, int y) {
if (r1 + c1 == x + y || c1 - r1 == y - x || abs(r1 - x) + abs(c1 - y) <= 3) return true;
return false;
}
int main() {
int ans;
scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
if (r1 == r2 && c1 == c2) ans = 0;
else if (is_in(r2, c2)) ans = 1;
else {
bool flag = false;
for (int i = r2 - 2; i <= r2 + 2; ++i)
for (int j = c2 - 2; j <= c2 + 2; ++j)
if (is_in(i, j)) flag = true;
if (is_in(r2 - 3, c2)) flag = true;
if (is_in(r2 + 3, c2)) flag = true;
if (is_in(r2, c2 - 3)) flag = true;
if (is_in(r2, c2 + 3)) flag = true;
if (((r1 + c1) & 1) == ((r2 + c2) & 1)) flag = true;
if (flag) ans = 2;
else ans = 3;
}
printf("%d", ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin() ,(x).end()
#define prec(x) cout<<fixed<<setprecision(x)
const ll mod = 1e9 + 7;
ll binpow(ll a, ll b , ll mod) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res % mod;
}
const int dx[] = {1 , -1 , 0 , 0};
const int dy[] = {0 , 0 , -1 , 1};
const int N = 3e5 + 5;
const ll inf = 1e16;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
string p = "ATGC";
int fr[n][4];
memset(fr , 0 , sizeof(fr));
for (int i = 0 ; i < n ; i++) {
for (int j = 0 ; j < 4 ; j++) {
fr[i][j] = fr[i - 1][j] * ( i != 0 ) + (p[j] == s[i]);
}
}
int ans = 0;
for (int i = 0 ; i < n ; i++) {
for (int j = i ; j < n ; j++) {
int A = 0 , T = 0 , C = 0 , G = 0;
T = fr[j][1] , A = fr[j][0] , G = fr[j][2] , C = fr[j][3];
if (i > 0)
T -= fr[i - 1][1] , A -= fr[i - 1][0] , G -= fr[i - 1][2] , C -= fr[i - 1][3];
//cout << T << " " << A << " " << G << " " << C << "\n";
if ((T > 0 or C > 0) and T == A and C == G)
ans++;
}
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
} | /*
Author: rafa45
Date: 28 Sep 2020
*/
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define mod 1000000007
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int i=0, j=0, n=0;
int c1=0, c2=0, cnt=0;
string s, sub;
cin >> n >> s;
for(i=0; i<n-1; i++){
c1=0; c2=0;
for(j=i; j<n; j++){
if(s[j]=='A') c1++;
else if(s[j]=='T') c1--;
else if(s[j]=='C') c2++;
else c2--;
if(c1==0 && c2==0) cnt++;
}
}
cout << cnt << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long int
#define lb long long double
#define mp make_pair
#define MOD 1000000007
#define pb push_back
#define ppi pair<ll,pair<ll,ll>>
#define F(i,n) for(ll i=0;i<n;i++)
#define w(t) ll t;cin >> t;while(t--)
using namespace std;
vector<ll> divisor;
void getDivisors(ll n)
{
for (ll i=1; i<=sqrt(n); i++)
{
if (n%i == 0)
{
// If divisors are equal, take only one
if (n/i == i)
divisor.push_back(i);
else // Otherwise take both
{
divisor.push_back(i);
divisor.push_back(n/i);
}
}
}
}
int main()
{
//w(t)
{
//cout << "Codeforces Higher rating" << endl;
ll n;
cin >> n;
vector<ll> a(n),b(n);
for(int i = 0 ; i < n ; i = i + 1){
cin >> a[i] >> b[i];
}
if(n == 1)
{
cout << a[0] + b[0] << endl;
return 0;
}
int min1ind = min_element(a.begin(),a.end()) - a.begin();
int min2ind = min_element(b.begin(),b.end()) - b.begin();
int x = (a[min1ind] + b[min2ind]);
if(min1ind != min2ind) cout << max(a[min1ind],b[min2ind]) << endl;
else
{
sort(a.begin(),a.end());
sort(b.begin(),b.end());
cout << min(x,(int)min(max(a[0],b[1]),max(a[1],b[0]))) <<endl;
}
}
} |
#include "bits/stdc++.h"
using namespace std;
int main()
{
int N;
cin >> N;
vector<int> A(N), B(N);
for (int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
}
int min_A, min_B, min_A2, min_B2;
int min_AB = min(A[0] + B[0], A[1] + B[1]);
int idx_A = -1;
int idx_B = -1;
if (A[0] < A[1]) {
min_A = A[0];
min_A2 = A[1];
idx_A = 0;
}
else {
min_A = A[1];
min_A2 = A[0];
idx_A = 1;
}
if (B[0] < B[1]) {
min_B = B[0];
min_B2 = B[1];
idx_B = 0;
}
else {
min_B = B[1];
min_B2 = B[0];
idx_B = 1;
}
for (int i = 2; i < N; i++) {
if (A[i] < min_A) {
min_A2 = min_A;
min_A = A[i];
idx_A = i;
}
if (A[i] < min_A2) {
min_A2 = A[i];
}
if (B[i] < min_B) {
min_B2 = min_B;
min_B = B[i];
idx_B = i;
}
if (B[i] < min_B2) {
min_B2 = B[i];
}
if (A[i] + B[i] < min_AB) {
min_AB = A[i] + B[i];
}
}
int time = 0;
if (idx_A == idx_B) {
int max1 = max(min_A, min_B2);
int max2 = max(min_A2, min_B);
time = min(max1, max2);
}
else {
time = max(min_A, min_B);
}
time = min(time, min_AB);
cout << time << endl;
}
|
// #pragma GCC target("avx") // CPU 処理並列化
// #pragma GCC optimize("O3") // CPU 処理並列化
// #pragma GCC optimize("unroll-loops") // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
#include<unordered_set>
#include<unordered_map>
#include<complex>
using namespace std;
const long long mod=998244353;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const double EPS=1e-11;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 11100000;
const int UF_SIZE = 3100000;
namespace{
long long fact[C_SIZE];
long long finv[C_SIZE];
long long inv[C_SIZE];
inline long long Comb(int a,int b){
if(a<b||b<0)return 0;
return fact[a]*finv[b]%mod*finv[a-b]%mod;
}
void init_C(int n){
fact[0]=finv[0]=inv[1]=1;
for(int i=2;i<n;i++){
inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
}
for(int i=1;i<n;i++){
fact[i]=fact[i-1]*i%mod;
finv[i]=finv[i-1]*inv[i]%mod;
}
}
long long pw(long long a,long long b){
if(a<0LL)return 0;
if(b<0LL)return 0;
long long ret=1;
while(b){
if(b%2)ret=ret*a%mod;
a=a*a%mod;
b/=2;
}
return ret;
}
long long pw_mod(long long a,long long b,long long M){
if(a<0LL)return 0;
if(b<0LL)return 0;
long long ret=1;
while(b){
if(b%2)ret=ret*a%M;
a=a*a%M;
b/=2;
}
return ret;
}
int pw_mod_int(int a,int b,int M){
if(a<0)return 0;
if(b<0)return 0;
int ret=1;
while(b){
if(b%2)ret=(long long)ret*a%M;
a=(long long)a*a%M;
b/=2;
}
return ret;
}
int ABS(int a){return max(a,-a);}
long long ABS(long long a){return max(a,-a);}
double ABS(double a){return max(a,-a);}
int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
int UF[UF_SIZE];
void init_UF(int n){
for(int i=0;i<n;i++)UF[i]=-1;
}
int FIND(int a){
if(UF[a]<0)return a;
return UF[a]=FIND(UF[a]);
}
void UNION(int a,int b){
a=FIND(a);b=FIND(b);if(a==b)return;
if(UF[a]>UF[b])swap(a,b);
UF[a]+=UF[b];UF[b]=a;
}
}
// ここから編集しろ
int p[210000];
int q[210000];
int cnt[410000];
int main(){
int a;scanf("%d",&a);
for(int i=0;i<a;i++){
scanf("%d%d",p+i,q+i);
p[i]--;q[i]--;
}
init_UF(410000);
for(int i=0;i<a;i++){
UNION(p[i],q[i]);
}
for(int i=0;i<a;i++){
cnt[FIND(p[i])]++;
}
int ret=0;
for(int i=0;i<410000;i++){
if(FIND(i)!=i)continue;
ret+=min(-UF[FIND(i)],cnt[i]);
}
printf("%d\n",ret);
} | #include <iostream>
#include <vector>
#include <utility>
typedef long long ll;
int N,T;
double L,R;
using namespace std;
double eps = 1e-4;
int main() {
cin >> N;
vector<pair<double,double>> intervals;
for(int i = 0; i < N; i++) {
cin >> T >> L >> R;
if(T == 1) {
intervals.emplace_back(L,R);
}
else if(T == 2) {
intervals.emplace_back(L,R-eps);
}
else if (T == 3) {
intervals.emplace_back(L+eps,R);
}
else if (T == 4) {
intervals.emplace_back(L+eps,R-eps);
}
}
int ans = 0;
for(int i = 0; i < N; i++) {
for(int j = i+1; j < N; j++) {
auto interval1 = intervals[i];
auto interval2 = intervals[j];
// cout << max(interval1.first, interval2.first) << " " << min(interval1.second, interval2.second) << endl;
if (max(interval1.first, interval2.first) <= min(interval1.second, interval2.second)) {
ans ++;
}
// if (! (interval1.second < interval2.first || interval1.first > interval2.second)) {
// //cout << interval1.first << "," << interval1.second << " intersects " << interval2.first << "," <<
// //interval2.second << endl;
// ans++;
// } else {
// }
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
long long arr[n];
long long diff[n];
cin >> arr[0];
diff[0] = arr[0] - 1;
for (int i = 1; i < n; ++i) {
cin >> arr[i];
diff[i] = diff[i-1] + arr[i] - arr[i-1] - 1;
}
while (q--) {
long long x;
cin >> x;
if (x <= diff[0]) cout << x << "\n";
else if (x > diff[n-1]) cout << arr[n-1] + x - diff[n-1] << "\n";
else {
bool done = false;
int l = 0, r = n, mid;
while (l <= r) {
mid = (l + r) / 2;
if (x == diff[mid]) {
while (diff[mid - 1] == diff[mid]) mid--;
cout << arr[mid] - 1 + (x - x) << "\n";
done = true;
break;
} else if (x < diff[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
}
if (!done) {
while (diff[mid] < x) mid++;
cout << arr[mid] - 1 + (x - diff[mid]) << "\n";
}
}
}
} | #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PRECISION cout << fixed << setprecision(20);
using namespace std;
template <typename SegNode>
struct SegTree {
vector<SegNode> tree, arr;
int n;
SegTree(int n): n(n) {
tree.assign(4 * n, 0);
}
SegTree(int n, vector<SegNode> arr): n(n) {
tree.assign(4 * n, 0);
arr = arr;
build(0, n - 1, 0);
}
SegNode merge(SegNode l, SegNode r) {
return l + r;
}
void build(int ss, int se, int si) {
if (ss == se) {
tree[si] = arr[ss];
} else {
int mid = ss + se >> 1;
build(ss, mid, 2 * si + 1);
build(mid + 1, se, 2 * si + 2);
tree[si] = merge(tree[2 * si + 1], tree[2 * si + 2]);
}
}
SegNode query(int qs, int qe) {
return query(qs, qe, 0, n - 1, 0);
}
SegNode query(int qs, int qe, int ss, int se, int si) {
if (ss >= qs && se <= qe) {
return tree[si];
}
int mid = ss + se >> 1;
if (qe <= mid) {
return query(qs, qe, ss, mid, 2 * si + 1);
} else if (qs >= mid + 1) {
return query(qs, qe, mid + 1, se, 2 * si + 2);
} else {
return merge(query(qs, qe, ss, mid, 2 * si + 1), query(qs, qe, mid + 1, se, 2 * si + 2));
}
}
void update(int qi, SegNode val) {
update(qi, val, 0, n - 1, 0);
}
void update(int qi, SegNode val, int ss, int se, int si) {
if (ss == se) {
tree[si] += val;
} else {
int mid = ss + se >> 1;
if (qi <= mid)
update(qi, val, ss, mid, 2 * si + 1);
else
update(qi, val, mid + 1, se, 2 * si + 2);
tree[si] = merge(tree[2 * si + 1], tree[2 * si + 2]);
}
}
};
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector<ll> a(n + 1), b(m + 1);
vector<array<ll, 3>> queries(q);
vector<ll> store = {-1, 0};
for (auto &qu: queries) {
cin >> qu[0] >> qu[1] >> qu[2];
store.push_back(qu[2]);
}
sort(store.begin(), store.end());
store.resize(unique(store.begin(), store.end()) - store.begin());
int len = store.size(); // 0 to len - 1
map<ll, int> ind;
for (int i = 0; i < len; i++) ind[store[i]] = i;
ll suma = 0, sumb = 0, ans = 0;
SegTree<ll> tsuma(len), tsumb(len), tcnta(len), tcntb(len);
tcntb.update(ind[0], m);
tcnta.update(ind[0], n);
for (auto &qu: queries) {
ll t = qu[0], x = qu[1], y = qu[2];
if (t == 1) {
// change in a
ll org = sumb - tsumb.query(0, ind[a[x]]) + tcntb.query(0, ind[a[x]]) * a[x];
ans -= org;
tsuma.update(ind[a[x]], -a[x]);
tcnta.update(ind[a[x]], -1);
suma -= a[x];
a[x] = y;
tsuma.update(ind[a[x]], a[x]);
tcnta.update(ind[a[x]], 1);
suma += a[x];
ll act = sumb - tsumb.query(0, ind[a[x]]) + tcntb.query(0, ind[a[x]]) * a[x];
ans += act;
} else {
ll org = suma - tsuma.query(0, ind[b[x]]) + tcnta.query(0, ind[b[x]]) * b[x];
ans -= org;
tsumb.update(ind[b[x]], -b[x]);
tcntb.update(ind[b[x]], -1);
sumb -= b[x];
b[x] = y;
tsumb.update(ind[b[x]], b[x]);
tcntb.update(ind[b[x]], 1);
sumb += b[x];
ll act = suma - tsuma.query(0, ind[b[x]]) + tcnta.query(0, ind[b[x]]) * b[x];
ans += act;
}
cout << ans << "\n";
}
}
int main() {
FASTIO;
PRECISION;
int t = 1;
// cin >> t;
for (int i = 0; i < t; i++) {
solve();
}
}
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define vec vector <ll>
#define vecp vector <pair<ll,ll>>
#define mp make_pair
#define A first
#define B second
#define PattSee ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define all(v) v.begin(),v.end()
#define mpl map<ll,ll>
#define umpl unordered_map<ll,ll>
#define rep(i,a,b) for(i=a; i<b; i++)
using namespace std;
void solve()
{
ll i,j,flag=0;
// Write your code here
ll n;
cin>>n;
vec v(n+1,2);
cout<<"1 ";
for(i=2; i<=n; i++)
{
for(j=2; i*j<=n; j++)
v[i*j]=v[i]+1;
cout<<v[i]<<" ";
}
}
int main()
{
PattSee
ll t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
/*
if lcm of all is not last then +1
els +(no. of non prime)
3 + no. of non-prime divisors
5*3*7 = 21*5 = 105
105
33 35 37
17 19 21 23 25
11 13 15 17 19 21 23
48
23 25
9 11 13 15
*/
| #include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int dp[n + 1];
dp[1] = 1;
for(int i = 2;i <= n;++i) {
int max_div = 1;
for(int d = 2;d <= sqrt(i);++d) {
if(i % d == 0) {
max_div = max(max_div, d);
max_div = max(max_div, i / d);
}
}
dp[i] = dp[max_div] + 1;
}
for(int i = 1;i <= n;++i) {
cout << dp[i] << " ";
}
return 0;
} |
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std ;
typedef long long ll ;
const int mod=1000000007;
const int INF=1e9 ;
const int N=2e5 ;
ll AbsDiff(ll a[], int n)
{
if ( n < 2 ) return 0;
sort(a,a+n);
ll sum = 0;
int i;
for(i=n-1;i>=0;i--)
{
sum += ((ll)a[i]*((ll)i) - (ll)a[i]*((ll)n-i-1));
}
return sum;
}
void solve(){
int n ;
cin>>n ;
ll a[n] ;
rep(i,n){
cin>>a[i] ;
}
cout<<AbsDiff(a,n) ;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin) ;
freopen("output.txt","w",stdout) ;
#endif
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
int y ;
y=1 ;
//cin>>y ;
while(y--){
solve() ;
}
return 0 ;
} | #include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
int read() {
register int x = 0, f = 0, ch = getchar();
for (; !isdigit(ch); f = (ch == 45), ch = getchar()) ;
for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar()) ;
return f ? -x : x;
}
void write(int x) {
if (x < 0) putchar(45), x = -x;
static int sta[35];
int top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) putchar(sta[--top] + 48);
}
const int maxn = 2e5 + 9, maxm = 2e5 + 9, inf = 0x3f3f3f3f;
int n, m;
ll a[maxn], b[maxn], v1[maxn], v2[maxn];
std::vector<int> vec[maxn];
ll c[maxn];
#define lowbit(x) ((x) & (-x))
void add(ll x, ll k) {
for (; x <= n; x += lowbit(x)) c[x] += k;
}
ll query(ll x) {
ll ret = 0;
for (; x; x -= lowbit(x)) ret += c[x];
return ret;
}
int main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
n = read();
for (int i = 1; i <= n; ++i) v1[i] = (a[i] = read() + i);
for (int i = 1; i <= n; ++i) v2[i] = (b[i] = read() + i);
std::sort(v1 + 1, v1 + n + 1); std::sort(v2 + 1, v2 + n + 1);
bool flag = 0;
for (int i = 1; i <= n; ++i) if (v1[i] != v2[i]) {
flag = 1;
break;
}
if (flag) {
puts("-1");
return 0;
}
int tot = std::unique(v2 + 1, v2 + n + 1) - v2 - 1;
for (int i = 1; i <= n; ++i) a[i] = std::lower_bound(v2 + 1, v2 + tot + 1, a[i]) - v2;
for (int i = 1; i <= n; ++i) b[i] = std::lower_bound(v2 + 1, v2 + tot + 1, b[i]) - v2;
for (int i = n; i >= 1; --i) vec[b[i]].push_back(i);
for (int i = 1; i <= n; ++i) {
int x = a[i];
a[i] = vec[x].back();
vec[x].pop_back();
}
ll ans = 0;
for (int i = n; i >= 1; --i) {
ans += query(a[i] - 1);
add(a[i], 1);
}
printf("%lld\n", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
//const ll INF = numeric_limits<ll>::max() / 4;
//const int INF = numeric_limits<int>::max() / 4;
int main() {
// ll N;
ll A, B, C, D;
cin >> A >> B >> C >> D;
ll b = A;
ll r = 0;
ll c = 0;
if(C * D <= B){
cout << -1 << endl;
return 0;
}
while(true){
if(b <= r * D){
cout << c << endl;
return 0;
}
b += B;
r += C;
c++;
}
return(0);
}
| #include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <tuple>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include<iomanip>
using namespace std;
using ll = long long;
using ULL = unsigned long long;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vpl = std::vector<pll>;
using vb = std::vector<bool>;
using db = double;
using vdb = std::vector<db>;
using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending
using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending
using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending
using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending
template<class T>
using vv = std::vector<std::vector<T> >;
using std::cout; using std::string;
using std::endl; using std::cin;
using std::transform; using std::toupper;
#define REPL(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FORL(i, 0, n)
#define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- )
#define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
#define INF pow(10, 10)
void chmin(ll& a, ll b){ if(a > b) a = b; }
void chmax(ll& a, ll b){ if(a < b) a = b; }
int main(void)
{
ll a, b, c, d;
cin >> a >> b >> c >> d;
ll pro = d * c - b;
if(pro <= 0)
{
cout << -1 << endl;
}
else
{
ll Time;
if(a%pro ==0) Time = a/pro;
else Time = a/pro + 1;
cout << Time << endl;
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <sstream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define debug(s, param) std::cerr << s << param << std::endl;
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
const int INF = 1e9;
const ll INFL = 1e18;
const ll MOD = 1000000007;
int f(const vector<vector<int>>& board, vector<vector<int>>& cache, int row, int col) {
int h = board.size(), w = board[0].size();
if (row == h - 1 && col == w - 1) return 0;
if (cache[row][col] != -1) return cache[row][col];
bool takahashi = (row + col) % 2 == 0 ? true : false;
int res = takahashi ? -INF : INF;
if (takahashi) {
if (row < h - 1) res = max(res, f(board, cache, row + 1, col) + board[row+1][col]);
if (col < w - 1) res = max(res, f(board, cache, row, col + 1) + board[row][col+1]);
} else {
if (row < h - 1) res = min(res, f(board, cache, row + 1, col) - board[row+1][col]);
if (col < w - 1) res = min(res, f(board, cache, row, col + 1) - board[row][col+1]);
}
cache[row][col] = res;
return res;
}
int main() {
int h, w; cin >> h >> w;
vector<vector<int>> board(h, vector<int>(w));
rep (row, h) {
string s; cin >> s;
rep (col, w) {
if (s[col] == '+') board[row][col] = 1;
else board[row][col] = -1;
}
}
vector<vector<int>> cache(h, vector<int>(w, -1));
int ans = f(board, cache, 0, 0);
if (ans > 0) cout << "Takahashi" << endl;
else if (ans < 0) cout << "Aoki" << endl;
else cout << "Draw" << endl;
return 0;
} | #include <iostream>
void print_mat(int h, int w, int* mat){
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
std::cout << mat[i*w + j] << " ";
}
std::cout << std::endl;
}
}
int main(){
int h, w;
std::cin >> h >> w;
bool* map = new bool[h*w];
int* dp = new int[h*w];
for(int i=0; i<h*w; ++i){
char c;
std::cin >> c;
map[i] = (c=='+');
}
if(map[h*w -1]){
dp[h*w -1] = -1;
}else {
dp[h*w -1] = 1;
}
for(int sum=h+w-3; 0<=sum; --sum){
for(int i=std::max(0, sum-w+1); i<std::min(h, sum+1); ++i){
int j = sum - i;
int index = w*i + j;
int val;
if(i==h-1){
val = dp[index+1];
}else if(j==w-1){
val = dp[index+w];
}else{
val = std::min(dp[index+1], dp[index + w]);
}
if(map[index]){
val = -1 - val;
}else{
val = 1 -val;
}
dp[index] = val;
}
}
int res = dp[0];
if(map[0]){
res += 1;
}else{
res -= 1;
}
if(res > 0){
std::cout << "Takahashi\n";
}else if(res==0){
std::cout << "Draw\n";
}else{
std::cout << "Aoki\n";
}
// print_mat(h, w, dp);
}
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) begin(x), end(x)
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int n0 = 5003, mod = 998244353;
int h, w, k, dp[n0][n0], p[n0][n0], pw[n0 * n0];
char g[n0][n0];
void add(int & x, int y) {
x += y;
if (x >= mod) x -= mod;
}
int U(int x, int y) {
if ((g[x][y] == 'D') || (g[x][y] == 'X')) return 1;
return (g[x][y] == '?') * 2;
}
int L(int x, int y) {
if ((g[x][y] == 'R') || (g[x][y] == 'X')) return 1;
return (g[x][y] == '?') * 2;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
pw[0] = 1;
for (int i = 1; i < n0 * n0; i++)
pw[i] = (ll)pw[i - 1] * 3 % mod;
cin >> h >> w >> k;
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++)
g[i][j] = '?';
for (int i = 0; i < k; i++) {
int a, b;
char c; cin >> a >> b >> c;
g[a][b] = c;
}
dp[1][1] = 1;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (i == 1 && j == 1) continue;
p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + (g[i][j] == '?');
int row = p[i][j - 1] - p[i - 1][j - 1];
int col = p[i - 1][j] - p[i - 1][j - 1];
int & res = dp[i][j];
add(res, (ll)dp[i - 1][j] * U(i - 1, j) * pw[row] % mod);
add(res, (ll)dp[i][j - 1] * L(i, j - 1) * pw[col] % mod);
}
}
int ans = dp[h][w];
if (g[h][w] == '?') ans = (ll)ans * 3 % mod;
cout << ans;
}
| /*
Auther: ghoshashis545 Ashis Ghosh 😎
College: Jalpaiguri Govt Enggineering College
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include<algorithm>
#include<string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define sp(x) cout << fixed << setprecision(x)
#define endl "\n"
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define ipair pair<int,int>
#define vpi vector<ipair>
#define clr(a,x) memset(a,x,sizeof(a))
#define alt(v) v.begin(),v.end()
#define ralt(v) v.rbegin(),v.rend()
#define pb emplace_back
#define mp make_pair
#define PI 3.1415926535897932384626
#define cntb(x) __builtin_popcount(x)
#define fab(i,a,b) for(int i=(a);i<(b);i++)
#define fba(i,a,b) for(int i=(b);i>=(a);i--)
bool ispoweroftwo(int n){return n&(!(n&(n-1)));}
int mod=1000000007;
// int mod=998244353;
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};
bool test = 0;
const int inf = 1e18;
const int N = 2e5+5;
int n;
int a[N],ans[60],dist[N];
vector<array<int,2>>adj[N];
void dfs(int u,int p,int c){
dist[u] = c;
for(auto it : adj[u]){
int v = it[0];
int w = it[1];
if(v == p)
continue;
dfs(v,u,c^w);
}
}
void solve(int tc = 0)
{
cin >> n;
for(int i = 1,x,y,z; i < n; ++i){
cin >> x >>y >> z;
adj[x].push_back({y,z});
adj[y].push_back({x,z});
}
dfs(1,-1,0);
int res = 0;
for(int i = 0; i < 60; ++i){
vector<int>cnt(2,0);
int cur = (1LL<<i)%mod;
for(int j = 1; j <= n; ++j){
int bit = dist[j]>>i&1;
res += (cnt[bit^1]*cur)%mod;
res %= mod;
cnt[bit]++;
}
}
cout<<res<<"\n";
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
if(test)
cin>>t;
for(int i = 1; i <= t; ++i){
// cout<<"Case #"<<i<<": ";
solve(i);
}
return 0;
} |
#include<bits/stdc++.h>
template <typename _Tp>void read(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=300005;
typedef long long ll;
int a[N];
ll s[N];
std::map<ll,int> mp;
int main(){
int n;read(n);
for(int i=1;i<=n;++i)read(a[i]);
ll ans=0;
++mp[0];
for(int i=1;i<=n;++i){
s[i]=s[i-1]+(i%2?a[i]:-a[i]);
ans+=mp[s[i]]++;
}
printf("%lld\n",ans);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int f[11]={2,3,5,7,11,13,17,19,23,29,31};
int m[11]={0};
int n;
cin>>n;
long long daan=1;
for(int i=2;i<=n;i++)
{
int ans=i;
for(int c=0;f[c]<=n;c++)
{
if(ans%f[c]==0)
{
int flag=0;
while(ans%f[c]==0)
{
ans/=f[c];
flag++;
}
if(flag>m[c])
m[c]=flag;
}
}
}
for(int i=0;i<=10;i++)
{
if(m[i]!=0)
daan*=pow(f[i],m[i]);
}
cout<<daan+1;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
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)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const ll mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
int res = 1;
while(b){
if(b&1)res = mul(res,a);
b >>= 1;
a = mul(a,a);
}
return res;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
ld A,B,W; cin >> A >> B >> W;
W *= 1000;
ll mini = (ll)1e18;
ll maxi = -1;
forn(i,1,W+1){
if(A*i <= W && W <= B*i){
mini = min(mini,ll(i));
maxi = max(maxi,ll(i));
}
}
if(maxi == -1){
cout << "UNSATISFIABLE\n";
return 0;
}
cout << mini << ' ' << maxi << '\n';
return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
| #include <iostream>
using namespace std;
using ll = long long;
int main() {
int A, B, W;
cin >> A >> B >> W;
int W_A, W_B;
W_A = W * 1000 / A;
W_B = W * 1000 / B;
if (W_A == W_B && W * 1000 % A != 0 && W * 1000 % B != 0) {
cout << "UNSATISFIABLE" << endl;
} else {
cout << (W*1000 + (B-1)) / B << " " << W_A << endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int f[1100];
int main()
{
memset(f,0,sizeof(f));
int k;cin>>k;int sum;bool flag=0;
for(int i=1;i<=k;i++)
{
cin>>sum;
if(sum<1||sum>k) {flag=1;continue;}
if(f[sum]==0) f[sum]++;
else flag=1;
}
if(flag==1) cout<<"No";
else cout<<"Yes";
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define x first
#define y second
#define pi pair
#define K 1000000007
using namespace std;
ll a[2000000];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll test_case=1;//cin>>test_case;
while(test_case--)
{
string n;cin>>n;
ll i=0,j=n.size()-1;
while(i<j)swap(n[i++],n[j--]);
string s;
i=0;
while(i<n.size()&&n[i]=='0')i++;
while(i<n.size())s.push_back(n[i++]);
ll flg=0,nt=s.size();
for(ll i=0;i<s.size();i++)
if(s[i]!=s[nt-1-i]){flg=1; break;}
if(flg==0)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{int N, X, i{}, V, P, s{};
for (cin >> N >> X; ++i, cin >> V >> P;) if ((s += V * P) > 100 * X) cout << i, exit(0);
puts("-1");} | // Premature optimization is the root of all evil in programming - Knuth.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod (int) (1e9+7)
void solve(){
ll n,x; cin>>n>>x;
x=x*100; ll curr=0,ans=-1;
for(int i=1; i<=n; i++){
ll a,b; cin>>a>>b;
a=a*b; curr+=a;
if(curr>x && ans==-1){
ans=i;
}
}
cout<<ans<<"\n";
return ;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;//cin>>t;
// clock_t begin, end;
// double time_spent;
// begin = clock(); // Time taken till now.
while(t--){
solve();
}
//end= clock(); // Total Time taken till now.
//time_spent= (end- begin) / CLOCKS_PER_SEC;
//cout<<time_spent<<"\n";
return 0;
}
|
#pragma GCC optimize("Ofast")
#if 1
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
constexpr char newl = '\n';
constexpr double eps = 1e-10;
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cerr << #x << " = " << (x) << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo(x) cout<<((x)?"Yes":"No")<<newl;
#define YESNO(x) cout<<((x)?"YES":"NO")<<newl;
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
template<class A, class B> using umap = unordered_map<A, B>;
template<class A> using uset = unordered_set<A>;
template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
F0R(j, sz(v)) i >> v[j];
return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
o << "(" << v.first << "," << v.second << ")";
return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
F0R(i, v.size()) {
o << v[i] << ' ';
}
o << newl;
return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
for (auto e : v) {
o << e << ' ';
}
o << newl;
return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
for (auto& p : m) {
o << p.first << ": " << p.second << newl;
}
o << newl;
return o;
}
const string target = "atcoder";
int inf = 1e9;
// 文字列のt文字目を、t文字以降とのswapのみであるchar以上にする。
// その後swap回数を返す
int F(string& S, int t, char c) {
int u = -1;
FOR(i, t, sz(S)) {
if (S[i] >= c) {
u = i;
break;
}
}
if (u < 0) return inf;
int ans = u - t;
while (u > t) {
swap(S[u], S[u - 1]);
u--;
}
return ans;
}
ll solve() {
string S; cin >> S;
ll ans = inf;
ll base = 0; // 既にSに加えた処理回数
F0R(i, min(sz(target), sz(S))) {
string s(S);
chmin(ans, base + F(s, i, target[i] + 1));
base += F(S, i, target[i]);
}
if (sz(S) > sz(target)) {
chmin(ans, base);
}
return ans;
}
// INSERT ABOVE HERE
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int T; cin >> T;
F0R(t, T) {
ll ans = solve();
if (ans >= inf) ans = -1;
cout << ans << newl;
}
}
#endif
| #include <bits/stdc++.h>
using namespace std;
string mu = "atcoder", s;
int wei = 0, anss = 0;
int Find(char a) {
for(int i = 0; i < s.size(); ++i) {
if(s[i] > a) return wei;
}
return 0x3f3f3f3f;
}
int main() {
int t; cin >> t;
while(t--) {
anss = 0x3f3f3f3f;
cin >> s;
if(s > mu) anss = 0;
if(s.size() == 1) {
if(anss == 0x3f3f3f3f) cout << -1 << endl;
else cout << anss << endl;
continue;
}
if(s[1] > 'a') anss = min(anss, 1);
// if(s[1] == 'a') {
if(s[0] == 'a') for(int i = 1; i < s.size(); ++i) {
if(s[i] > 't') anss = min(i - 1, anss);
}
for(int i = 0; i < s.size(); ++i) {
if(s[i] > 'a') anss = min(i, anss);
}
// }
if(anss == 0x3f3f3f3f) cout << -1 << endl;
else cout << anss << endl;
}
// cout << "atcodeer" > "atcoder" << endl;
} |
// Created by ...Rahul Chandra
#include <bits/stdc++.h>
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
using namespace std;
using ll = long long;
ll n;
inline void read() {
cin >> n;
}
inline void solve() {
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long a,b,c,d,e,f,g,rahul=0;
string s;
cin>>s;
int i=0;
string k;
for(auto x:s){
if (x=='.')
{
break;
/* code */
}
k+=x;
}
cout<<k<<endl;
return 0;
} | #include<iostream>
using namespace std;
int main(){
string s,ans="";
cin>>s;
for (int i=0;i<s.length();i++){
if (s[i]!='.') ans+=s[i];
else break;
}
cout<<ans<<endl;
} |
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
//マクロ
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define BIT(i, n) for (int i = 0; i < (1 << n); i++)
typedef long long ll;
//const ll INF = 10000000010;
int main() {
int N;
int ans = 0;
cin >> N;
vector<int> a(N), b(N), c(N);
rep(i, N) {
cin >> a.at(i);
}
rep(i, N) {
cin >> b.at(i);
}
rep(i, N) {
c.at(i) = a.at(i) * b.at(i);
ans += c.at(i);
}
if(ans == 0) cout << "Yes" << endl;
else cout << "No" << endl;
}
| #include<bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
std::vector<int> A(N);
std::vector<int> B(N);
for (int i = 0; i < N; ++i){
cin >> A[i];
}
for (int i = 0; i < N; ++i){
cin >> B[i];
}
int result = std::inner_product(
A.begin(), A.end(), // ひとつ目のベクトル
B.begin(), // ふたつ目のベクトル(v以上の次元数を持つこと)
0); // 内積の初期値(この値に対して加算が行われる)
string ans = "No";
if (result == 0) {
ans = "Yes";
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main(){
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int a,b,c;
cin>>a>>b>>c;
int d=min(c,min(a,b));
cout<<a+b+c-d;
return 0;
} | #include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
typedef long long ll;
using namespace std;
int a,b;
template<typename int_t>
void readx(int_t &x)
{
x=0; int_t k=1; char ch=0;
while (ch<'0' || ch>'9') { ch=getchar(); if (ch=='-') k=-1; }
while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
x*=k;
}
int main()
{
readx(a); readx(b);
printf("%d %d\n",(a+b)/2,(a-b)/2);
} |
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
int main()
{
char num[10];
rep(i, 10)
{
cin >> num[i];
}
int ans = 0;
rep(i, 10000)
{
int tmp = i;
bool b[10] = { false };
rep(j, 4)
{
int k = tmp % 10;
b[k] = true;
tmp /= 10;
}
bool flag = true;
rep(j, 10)
{
if (b[j] && num[j] == 'x')
{
flag = false;
break;
}
if (!b[j] && num[j] == 'o')
{
flag = false;
break;
}
}
if (flag) ans++;
}
cout << ans;
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
const int A = 200;
int kk[A + A + 1];
int main() {
int n; cin >> n;
long long ans = 0;
while (n--) {
int a; cin >> a, a += A;
for (int b = 0; b <= A + A; b++)
ans += (long long) ((a - A) - (b - A)) * ((a - A) - (b - A)) * kk[b];
kk[a]++;
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << endl;
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(z, v.size()) cerr << " " << v[z]; \
cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;
template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
rep(j, v.size()) i >> v[j];
return i;
}
template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
return i >> v.first >> v.second;
}
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
return to_string(x).size();
}
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
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;
}
signed main() {
init();
ll N, M;
cin >> N >> M;
vll H(N), W(M);
cin >> H >> W;
SORT(H);
vp d(N);
rep(i, N) {
if (i == 0) continue;
d[i] = d[i - 1];
if (i % 2 == 1) {
d[i].first += abs(H[i - 1] - H[i]);
} else {
d[i].second += abs(H[i - 1] - H[i]);
}
}
ll ans = INF;
rep(i, M) {
ll lb = lower_bound(ALL(H), W[i]) - H.begin();
if (lb % 2 == 1) lb--;
ll a = abs(H[lb] - W[i]);
if (lb != 0) a += d[lb - 1].first;
if (lb != N - 1) a += (d[N - 1].second - d[lb].second);
chmin(ans, a);
}
ANS;
return 0;
} | #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <cstdio>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
ll N, M;
vector<ll> H, W;
const ll INF = 1e17;
ll fpsum[200005];
ll bpsum[200005];
int main()
{
ll ans = INF;
cin >> N >> M;
H.resize(N);
W.resize(M);
for (ll i = 0; i < N; i++)
{
cin >> H[i];
}
for (ll i = 0; i < M; i++)
{
cin >> W[i];
}
sort(H.begin(), H.end());
for (ll i = 0; i < N - 2;i += 2)
{
fpsum[i/2 + 1] = H[i + 1] - H[i] + fpsum[i/2];
bpsum[i/2 + 1] = H[i + 2] - H[i + 1] + bpsum[i/2];
}
/*
for (ll i = 0; i < N - 2; i++)
{
cout << fpsum[i] << " ";
}
cout << endl;
for (ll i = 0; i < N - 2; i++)
{
cout << bpsum[i] << " ";
}
cout << endl;
*/
for (ll i = 0; i < M;i++)
{
auto it = upper_bound(H.begin(), H.end(), W[i]);
ll idx = it - H.begin();
ll tans;
if (idx % 2 == 1)
{
tans = fpsum[idx / 2] + abs(W[i] - H[idx - 1]) + bpsum[N / 2] - bpsum[idx / 2];
}
else
{
tans = fpsum[idx / 2] + abs(W[i] - H[idx]) + bpsum[N / 2] - bpsum[idx / 2];
}
ans = min(ans, tans);
}
cout << ans << endl;
return 0;
} |
/**
* author: longvu
* created: 06/24/21 10:08:58
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
const int INF = numeric_limits<int>::max();
const int nax = (int)(1000001);
const int mod = 1e9 + 7;
int dp[nax];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int l, r;
cin >> l >> r;
for (int i = r; i >= 2; --i) {
dp[i] = (r / i - (l - 1) / i) * (r / i - (l - 1) / i);
for (int j = 2 * i; j <= r; j += i) {
dp[i] -= dp[j];
}
}
int ans = 0;
for (int i = 2; i <= r; ++i) {
if (i >= l) {
dp[i] -= 2 * (r / i) - 1;
}
ans += dp[i];
}
cout << ans;
return 0;
} | // Date: 2020-10-03
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
#define IO freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout)
#define FIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i, a, b) for(int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for(int i = int(b); i >= int(a); --i)
#define D(x) cout << #x << " = " << x << endl;
#define mem(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)x.size())
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const LL INF = 1e18;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
LL qpow(LL x, LL y, LL MOD) {LL a=1; while(y){ if(y&1) a=a*x%MOD; x=x*x%MOD; y>>=1; } return a;}
int main() {
int a, b;
cin >> a >> b;
cout << (a+b)/2 << " " << (a-b)/2;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T> void print(T& v){ for(auto it : v) cout << it << " "; cout << endl; }
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); }
int main(int argc, char** argv) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
int x, y;
cin >> x >> y;
if (x == y)
cout << x << endl;
else
cout << 3-x-y << endl;
if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
return 0;
}
| /*
* @Author: tusikalanse
* @Date: 2021-06-26 19:48:54
* @LastEditTime: 2021-06-26 20:04:29
* @LastEditors: tusikalanse
* @Description:
* @FilePath: /atcoder/abc/207/b.cpp
*/
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL gcd(LL a, LL b) {
return b == 0 ? a : gcd(b, a % b);
}
const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
//-------------------end head--------------
int main() {
long long a, b, c, d;
cin >> a >> b >> c >> d;
if (b >= d * c) {
cout << -1 << endl;
return 0;
}
cout << 1 + (a - 1) / (d * c - b) << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define SZ(v) ((int)(v).size())
using namespace std;
using ll = long long;
template<typename... Args>
void read(Args&... args)
{
((cin >> args), ...);
}
template<typename... Args>
void write(Args... args)
{
((cout << args << " "), ...);
}
template<typename... Args>
void writeln(Args... args)
{
((cout << args << " "), ...); cout << '\n';
}
using T = double;
struct Line
{
T a, b;
T eval(pair<T, T> pt) {return a * pt.first + b;}
Line compose(Line other) const
{
return {a * other.a, a * other.b + b};
}
};
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nbPoints, valMax, nbBloques;
read(nbPoints, valMax, nbBloques);
vector<bool> blocked(nbPoints+1);
while (nbBloques--)
{
int i;
read(i);
blocked[i] = true;
}
vector<Line> dp(nbPoints+1, {0, 0});
dp[0] = {1, 0};
dp[nbPoints] = {0,0};
Line sum = {0, 0};
for (int i(nbPoints-1); i > 0; --i)
{
int furthest = min(nbPoints, i + valMax+1);
sum.a -= dp[furthest].a;
sum.b -= dp[furthest].b;
sum.a += dp[i+1].a;
sum.b += dp[i+1].b;
if (blocked[i]) dp[i] = {1, 0};
else
{
dp[i].a += sum.a / valMax;
dp[i].b += sum.b / valMax;
dp[i].b++;
}
}
Line eq({0, 0});
for (int j(1); j <= valMax; ++j)
{
int nxt = min(nbPoints, j);
eq = {eq.a + dp[nxt].a, eq.b + dp[nxt].b};
}
eq.a /= valMax;
eq.b /= valMax;
eq.b++;
eq.a -= 1;
if (abs(eq.a) < 1e-9)
writeln(-1);
else
cout << setprecision(10) << fixed << -eq.b/eq.a << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "/debug.h"
#else
#define db(...)
#endif
#define all(v) v.begin(), v.end()
#define pb push_back
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;
vector<int> solveCase(int n, vector<int64_t> &a)
{
// TODO: edit here
db(n, a);
vector<int> res;
int idx = 1;
for (size_t i = n; i >= 1;)
{
if (i == 3)
{
if (a[1] == 1 && a[2] == 2 && a[3] == 3)
break;
int id = -1;
for (int j = 1; j + 1 <= i; j++)
if ((j & 1) == (idx & 1))
{
id = j;
break;
}
db(a, idx, id);
swap(a[id], a[id + 1]);
res.pb(id);
db(idx, id, a);
idx = (idx + 1) % 2;
continue;
}
if (a[i] == i)
{
--i;
continue;
}
int id = -1;
for (int j = 1; j + 1 <= i; j++)
if ((j & 1) == (idx & 1))
if ((a[j] == i))
{
id = j;
break;
}
db(i, id);
if (id == -1)
{
for (int j = 1; j + 1 <= i; j++)
if ((j & 1) == (idx & 1))
{
id = j;
break;
}
if (id == -1)
{
cout << "WA\n";
exit(0);
assert(id != -1);
}
}
swap(a[id], a[id + 1]);
res.pb(id);
db(idx, id, a);
idx = (idx + 1) % 2;
}
db(a);
db(res);
return res;
}
// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main()
{
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
constexpr char endl = '\n';
// failed to analyze input format
// TODO: edit here
int t;
cin >> t;
for (size_t i = 0; i < t; i++)
{
int n;
cin >> n;
vector<int64_t> a(n + 1);
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
auto ans = solveCase(n, a);
int M = ans.size();
cout << M << endl;
for (int i = 0; i < M; ++i)
{
cout << ans[i] << ' ';
}
cout << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); }
int main(int argc, char** argv) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
int n;
cin >> n;
vector<int> a(n), b(n);
FOR(i, n) cin >> a[i] >> b[i];
int sol = 1e9;
FOR(i, n){
FOR(j, n){
if (i == j)
umin(sol, a[i] + b[j]);
else
umin(sol, max(a[i], b[j]));
}
}
cout << sol << endl;
if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <utility>
using ll=long long;
using namespace std;
int main(){
int N;
cin>>N;
vector<int> A(N);
vector<int> B(N);
for(int i=0;i<N;i++){
cin>>A[i]>>B[i];
}
int MX=1e6;
int minA=MX;
int minB=MX;
for(int i=0;i<N;i++){
minA=min(minA,A[i]);
minB=min(minB,B[i]);
}
//cout<<minA<<" "<<minB<<endl;
int cnta=0;
int cntb=0;
int cntab=0;
for(int i=0;i<N;i++){
if(A[i]==minA){
cnta++;
}
if(B[i]==minB){
cntb++;
}
if(A[i]==minA && B[i]==minB){
cntab++;
}
}
int ans=0;
if(cnta==1 && cntb==1 && cntab==1){
ans=minA+minB;
int mmA=MX;
int mmB=MX;
for(int i=0;i<N;i++){
if(A[i]!=minA){
mmA=min(mmA,A[i]);
mmB=min(mmB,B[i]);
}
}
ans=min(ans,min(max(mmA,minB),max(minA,mmB)));
}else{
ans=max(minA,minB);
}
cout<<ans<<endl;
return 0;
} |
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define all(container) (container).begin(), (container).end()
#define sz(container) static_cast<int>((container).size())
using namespace std;
class Solver {
public:
void solve(istream& in, ostream& out) {
out.precision(20);
int tests = 1;
for (int test = 1; test <= tests; test++) {
solveOne(in, out);
}
}
private:
void solveOne(istream& in, ostream& out) {
int N;
in >> N;
vector<int64_t> A(N), B(N);
for (auto& a : A) {
in >> a;
}
for (auto& b : B) {
in >> b;
}
vector<int> oddA, evenA, oddB, evenB;
for (int i = 0; i < N; i++) {
if (A[i] >= B[i]) {
if (i % 2 == 0) evenA.push_back(i);
else oddA.push_back(i);
} else {
if (i % 2 == 0) evenB.push_back(i);
else oddB.push_back(i);
}
}
auto byA = [&](int lhs, int rhs) {
return (A[lhs] - B[lhs]) < (A[rhs] - B[rhs]);
};
auto byB = [&](int lhs, int rhs) {
return (B[lhs] - A[lhs]) < (B[rhs] - A[rhs]);
};
sort(all(oddA), byA);
sort(all(evenA), byA);
sort(all(oddB), byB);
sort(all(evenB), byB);
int64_t answer = 0;
while (!oddA.empty() && !evenA.empty()) {
answer += A[oddA.back()];
oddA.pop_back();
answer += A[evenA.back()];
evenA.pop_back();
}
while (!oddB.empty() && !evenB.empty()) {
answer += B[oddB.back()];
oddB.pop_back();
answer += B[evenB.back()];
evenB.pop_back();
}
auto lastMatch = [&](vector<int> aPos, vector<int> bPos) {
assert(sz(aPos) == sz(bPos));
reverse(all(bPos));
int n = sz(aPos);
for (int i = 0; i < n; i++) {
answer += max(A[aPos[i]] + A[bPos[i]], B[aPos[i]] + B[bPos[i]]);
}
};
lastMatch(oddA, evenB);
lastMatch(evenA, oddB);
out << answer << endl;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
Solver solver;
solver.solve(cin, cout);
return 0;
}
| #include"bits/stdc++.h"
using namespace std;
using ll = long long;
const ll mod = 1000000007;
#define rep(i,m,n) for(ll i=m;i<n;i++)
int main() {
ll n;
string s;
cin >> n >> s;
ll ans = n;
vector<bool>f(n);
rep(i, 0, n-2) {
if (f[i] == 0&&f[i+1]==0&&f[i+2]==0) {
if (s[i] == 'f' && s[i + 1] == 'o' && s[i + 2] == 'x') {
f[i] = 1;
f[i + 1] = 1;
f[i + 2] = 1;
ans -= 3;
ll first = i - 2;
ll second = i - 1;
ll third = i + 3;
ll fourth = i + 4;
retry:
while (1) {
if (second <= 0)break;
if (f[second] == 1)second--;
else break;
}
first = second - 1;
while (1) {
if (first < 0)break;
if (f[first] == 1)first--;
else break;
}
while (1) {
if (third >= n - 1)break;
if (f[third] == 1) {
third++;
}
else break;
}
fourth = third + 1;
while (1) {
if (fourth > n - 1)break;
if (f[fourth] == 1)fourth++;
else break;
}
while (1) {
bool flag = 0;
if (first >= 0 && third <= n - 1) {
if (s[first] == 'f' && s[second] == 'o' && s[third] == 'x') {
ans -= 3;
//cout << front - 1 << " " << front << " " << back << endl;
f[first] = 1;
f[second] = 1;
f[third] = 1;
goto retry;
flag = 1;
}
}
if (second >= 0 && fourth <= n - 1) {
if (s[second] == 'f' && s[third] == 'o' && s[fourth] == 'x') {
ans -= 3;
//cout << front << " " << back << " " << back + 1 << endl;
f[second] = 1;
f[third] = 1;
f[fourth] = 1;
goto retry;
flag = 1;
}
}
if (flag == 0)break;
}
}
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_ll.hpp>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
double pi=acos(-1);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
// #define mp make_pair
#define pq priority_queue
const ll mod=1000000007;
#define f first
#define s second
#define pii pair< ll, ll >
#define vi vector<ll>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc ll t; cin >> t; while(t--)
// using namespace boost::multiprecision;
using namespace std;
void optimizeIO(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const ll N=1000005;
ll fact[N],invfact[N];
ll power(ll x,ll y){
if(y<=0) return 1LL;
ll z=power(x,y/2);
if(y%2) return (((z*z)%mod)*x)%mod;
return (z*z)%mod;
}
void pre(){
fact[0]=invfact[0]=invfact[1]=fact[1]=1;
rep(i,2,N) fact[i]=(i*fact[i-1])%mod;
rep(i,2,N) invfact[i]=(invfact[i-1]*power(i,mod-2))%mod;
}
ll nCr(ll n,ll k){ if (k < 0 || k > n) return 0; return (((fact[n]*invfact[n-k])%mod)*invfact[k])%mod; }
const ll N1=1e6+1;
vector<ll> isprime(N1,1),prime;
void seive(){
rep(i,2,sqrt(N1)+1){
if(isprime[i]){
for(ll j=i*i;j<N1;j+=i) isprime[j]=0;
prime.pb(i);
}
}
rep(i,sqrt(N1)+1,N1) if(isprime[i]) prime.pb(i);
}
struct dsu {
vector<ll> par, rank;
dsu(ll n): par(n+1), rank(n+1) {
for (ll i = 0; i <= n; i++) {
par[i] = i;
rank[i]= 1;
}
}
ll root(ll a) {
if (a == par[a]) return a;
return par[a] = root(par[a]);
}
void merge(ll a, ll b) {
a = root(a);
b = root(b);
if (a == b) return;
if (rank[a] > rank[b]) swap(a, b);
par[b] = a;
}
set<ll> parent(ll n){
set<ll> s;
for(ll i=1;i<=n;i++){
s.insert(root(i));
}
return s;
}
};
void solve(){
ll n,m;
cin>>n>>m;
vector<int>h(n),w(m);
rep(i,0,n) cin>>h[i];
rep(i,0,m) cin>>w[i];
sort(all(h));
vector<ll> dp(n+1,0),suf(n+1,0);
for(ll i=0;i+1<n;i+=2){
dp[i]=(i==0?0:dp[i-1])+abs(h[i+1]-h[i]);
dp[i+1]=dp[i];
}
for(ll i=n-1;i-1>=0;i-=2){
suf[i]=(i==n-1?0:suf[i+1])+abs(h[i]-h[i-1]);
suf[i-1]=suf[i];
}
ll ans=LLONG_MAX;
rep(i,0,m){
auto id=lower_bound(all(h),w[i]);
if(id==h.end()) ans=min(ans,dp[n-2]+abs(w[i]-h[n-1]));
else if(id==h.begin()) ans=min(ans,suf[1]+abs(w[i]-h[0]));
else{
ll idx=id-h.begin();
if(idx%2==0) ans=min(ans,dp[idx-1]+abs(w[i]-h[idx])+suf[idx+1]);
else ans=min(ans,(idx-2>=0?dp[idx-2]:0)+abs(w[i]-h[idx-1])+suf[idx]);
}
}
cout<<ans<<endl;
}
int32_t main(){
optimizeIO();
{solve();}
}
| #pragma GCC diagnostic warning "-Wextra"
#pragma GCC diagnostic warning "-Wshadow"
#include <bits/stdc++.h>
using namespace std;
template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
long N, M;
cin >> N >> M;
vector<long> H(N);
for (long i = 0; i < N; i++) cin >> H.at(i);
vector<long> W(M);
for (long i = 0; i < M; i++) cin >> W.at(i);
sort(H.begin(), H.end());
vector<long> L, R;
for (long i = 0; i + 1 < N; i += 2) {
L.push_back(H.at(i + 1) - H.at(i));
}
for (long i = 1; i + 1 < N; i += 2) {
R.push_back(H.at(i + 1) - H.at(i));
}
auto cs = [](auto vec) {
vector<long> ret((long)vec.size() + 1);
partial_sum(vec.begin(), vec.end(), ret.begin() + 1);
return ret;
};
L = cs(L);
R = cs(R);
long mi = LONG_MAX;
for (const auto& a : W) {
// long opp = lower_bound(H.begin(), H.end(), a) - H.begin();
// if (opp & 1) opp--;
// cmin(mi, L.at(opp / 2) + abs(H.at(opp) - a) + (R.back() - R.at(opp / 2)));
long opp = upper_bound(H.begin(), H.end(), a) - H.begin();
if (opp & 1) opp--;
cmin(mi, L.at(opp / 2) + abs(H.at(opp) - a) + (R.back() - R.at(opp / 2)));
}
cout << mi << '\n';
} |
#include<bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9+7;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<int> a(m), b(m);
vector<int> e(n);
rep(i,m) {
cin >> a[i] >> b[i];
a[i]--; b[i]--;
e[a[i]] |= 1 << b[i];
e[b[i]] |= 1 << a[i];
}
vector<int> dp(1 << n, 99999);
dp[0] = 0;
for (int i = 1; i < (1<<n); i++) {
rep(j, n) {
if ((i & (1<<j))==0) continue;
int target = i - (1<<j);
if(dp[target] <= 1 && (e[j] & target) == target) {
dp[i] = 1;
}
// 1つ調べれば十分なのでbreak
break;
}
}
rep(i, (1<<n)) {
for (int j = i; j > 0; j = (j-1) & i) {
// iをjとkの2つのグループに分ける。このi,jのループはO(3^n)
int k = i-j;
// 適切な分け方を探して最小値を更新する
dp[i] = min(dp[i], dp[j] + dp[k]);
}
}
cout << dp[(1 << n) - 1] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair
using namespace std;
const LL MOD=1e9+7;
LL t,N;
int main()
{
cin>>t>>N;
LL n=0;
n+=N/t*100;
N%=t;
LL cur=0;
while(N>0)
{
if(cur+t>=100) --N;
cur=(cur+t)%100;
++n;
}
cout<<(LL)(n+floor((double)t/100.0*(double)n)-1)<<endl;
return 0;
} |
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
// #include<bits/stdc++.h>
#include<iostream>
#include<string>
using namespace std;
string dfs(int n,string s){
if(n == 1)return s;
while(n%3!=1){
string t;
for(int i=1;i<n;i++){
if(s[i]==s[i-1])t+=s[i];
else {
int x = 'R'+'W'+'B';
x -= s[i] + s[i-1];
t += (char)x;
}
}
swap(t,s);
n--;
}
string t;
for(int i=0;i<n;i+=3)t+=s[i];
return dfs((int)t.size(),t);
}
int main(){
int n;
cin>>n;
string s;
cin>>s;
cout << dfs(n,s) << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(n1,n) for(int i=n1;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#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 deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#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
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());
int rng(int lim) {
uniform_int_distribution<int> uid(0, lim - 1);
return uid(rang);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
while (t--) {
ll n; cin >> n;
vl v(n + 1);
ll sum = 0;
fo(1, n + 1) {
cin >> v[i];
sum += v[i];
}
ll dp[n + 1][sum + 1];
memset(dp, 0, sizeof(dp));
fo(0, n + 1)
dp[i][0] = 1;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= sum; j++) {
if (j - v[i] >= 0) {
dp[i][j] = dp[i - 1][j] || dp[i - 1][j - v[i]];
}
else {
dp[i][j] = dp[i - 1][j];
}
}
}
ll max1 = INT_MAX;
for (ll i = 1; i <= sum; i++) {
if (dp[n][i] == 1) {
// cout << i << " ";
ll a = sum - i;
//cout << a << endl;
max1 = min(max1, max(a, i));
}
}
cout << max1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define range(a) a.begin(), a.end()
#define endl "\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define MP make_pair
using P = pair<int, int>;
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL<<60;
void chmin(long long &a, long long b) { if (a > b) a = b; }
void chmax(long long &a, long long b) { if (a < b) a = b; }
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--){
ll N;
cin >> N;
if(N % 2 == 1){
cout << "Odd" << endl;
}else{
N /= 2;
if(N % 2 == 1){
cout << "Same" << endl;
}else{
cout << "Even" << endl;
}
}
}
return 0;
}
| /*
author: Maksim1744
created: 13.02.2021 15:00:56
*/
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T>& v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T>& v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) 42
#define mclock 42
#define shows 42
#define debug if (false)
#endif
void test_case(int test) {
// a >= l * 2
// a = l * 2 + k
// b = l..l+k, c=l+k..l
// sum from k=0 to r-l*2 of (k+1)
// sum from k=1 to r-l*2+1 of k
ll l, r;
cin >> l >> r;
if (l * 2 > r) {
cout << 0 << '\n';
} else {
ll n = r - l * 2 + 1;
cout << n * (n + 1) / 2 << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int T;
cin >> T;
for (int test = 1; test <= T; ++test) {
test_case(test);
}
return 0;
}
|
#include <stdio.h>
#include <iostream>
#include <vector>
// #include <bits/stdc++.h>
#include <queue>
#include <algorithm>
#include <string>
#include <iomanip>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i,k,n) for(ll i=k; i<(ll)(n); i++)
int main(){
ll n; cin >> n;
vector<ll> a(n);
// vector<ll,vector<ll>> v(100);
map<ll,ll> m;
rep(i,0,n) {
cin >> a[i];
}
rep(i,0,n) {
ll index = a[i]%200;
m[index]++;
}
ll ans = 0;
// rep(i,0,200) {
// cout << i << " " << m[i] << endl;
// }
rep(i,0,200) {
ans+=m[i]*(m[i]-1)/2;
}
cout << ans << endl;
return 0;
} | #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
vi p;
for(int i = 2; i <= 50; i++){
int f = 1;
for(int j = 2; (j * j) <= i; j++){
if(i % j == 0) f = 0;
}
if(f) p.pb(i);
}
ll mn = 1e18;
rep(i, 1 << (int)(p.size())){
int f = 1;
ll num = 1LL;
rep(j, p.size()) if(i & (1 << j)) num *= (ll)(p[j]);
rep(j, n) if(gcd(a[j], num) == 1) f = 0;
if(f) mn = min(mn, num);
}
cout << mn << '\n';
return 0;
}
/*
Things to look out for:
- Integer overflows
- Make sure that max is large enough (2e9, 4e18)
- Special cases
Be careful!
*/
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=2e3+5;
ll dp[maxn][maxn],sum[3][maxn*2],belong[maxn][maxn];
char buff[maxn][maxn];
const int mod=1e9+7;
int main() {
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++) cin>>(buff[i]+1);
for(int i=1;i<=m;i++) belong[1][i]=i;
for(int i=2;i<=n;i++) belong[i][1]=m+i-1;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(belong[i][j]) continue;
belong[i][j]=belong[i-1][j-1];
}
}
dp[1][1]=1;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(buff[i][j]=='#') {
sum[0][belong[i][j]]=sum[1][i]=sum[2][j]=0;
}else {
if(i!=1||j!=1) dp[i][j]=(sum[0][belong[i][j]]+sum[1][i]+sum[2][j])%mod;
(sum[0][belong[i][j]]+=dp[i][j])%=mod;
(sum[1][i]+=dp[i][j])%=mod;
(sum[2][j]+=dp[i][j])%=mod;
}
}
}
cout<<dp[n][m]%mod<<endl;
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 <list>
#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 = 2005;
const int mod = 1000000007;
int d[maxn][maxn];
string s[maxn];
vector<pair<int,int>>a[26];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int main() {
#ifdef suiyuan2009
freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
//freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
int n,m;
cin>>n>>m;
int sx,sy,tx,ty;
for(int i=0;i<n;i++){
cin>>s[i];
for(int j=0;j<m;j++){
auto tt = s[i][j];
if(tt>='a'&&tt<='z')a[tt-'a'].push_back(make_pair(i,j));
else if(tt=='S')sx=i,sy=j;
else if(tt=='G')tx=i,ty=j;
}
}
int mask=0;
queue<int>qx;
queue<int>qy;
memset(d,-1,sizeof(d));
d[sx][sy] = 0;
qx.push(sx);
qy.push(sy);
while(!qx.empty()){
int x = qx.front();
qx.pop();
int y = qy.front();
qy.pop();
auto tt = s[x][y];
if(tt>='a'&&tt<='z'&&!(mask&(1<<(tt-'a')))){
mask|=(1<<(tt-'a'));
auto tmp = d[x][y];
for(auto pr: a[tt-'a']){
if(d[pr.first][pr.second]==-1){
d[pr.first][pr.second]=tmp+1;
qx.push(pr.first);
qy.push(pr.second);
}
}
}
for(int i=0;i<4;i++){
int nx = x+dir[i][0];
int ny = y+dir[i][1];
if(nx<0||nx>=n||ny<0||ny>=m||s[nx][ny]=='#'||d[nx][ny]!=-1)continue;
d[nx][ny] = d[x][y] + 1;
qx.push(nx);
qy.push(ny);
}
}
cout<<d[tx][ty]<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N ;
double X=0;
for(double i = 1; i < N; i++){
X += N/i;
}
cout << std::fixed << std::setprecision(12) << X << endl;
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6;
typedef long long ll;
template<class T> T read()
{
T f = 1, k = 0;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
k = k * 10 + c - '0';
c = getchar();
}
return f * k;
}
double n, ans;
vector<int> v[maxn];
int main()
{
n = read<double>();
for (double i = 1; i < n; i++)
{
ans += 1.000000 / i;
}
ans *= n;
printf("%lf\n", ans);
}
|
#include <iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
string x;
ull m;
bool check(ull mid)
{
ull ans=0;
int flag=1;
for(int i=0;i<x.size();i++)
{
if(ans > m / mid) return false;
ans=ans*mid+(x[i]-'0');
}
return ans <= m;
}
int main()
{
char c='0';
cin>>x>>m;
for(int i=0;i<x.size();i++)
{
c=max(c,x[i]);
}
ull t=c-'0';
if(x.size()==1)
{
if(t-1<=m) cout<<1<<endl;
else cout<<0<<endl;
}
else
{
ull l=0,r=1e18;
while(l<r)
{
ull mid=(l+r+1)>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
if(l<t) cout<<0<<endl;
else cout<<l-t<<endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define reps(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define all(obj) begin(obj), end(obj)
#define allr(obj) rbegin(obj), rend(obj)
#define cinv(a) rep(i,(int)a.size()) cin >> a[i]
#define debug(a) rep(i,(int)a.size()) cout << a[i] << " "
#define pb push_back
#define eb emplace_back
#define dame {puts("-1"); return 0;}
typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<Pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
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;}
const int di[] = {0, -1, 0, 1};
const int dj[] = {1, 0, -1, 0};
const int MOD = 1e9+7;
const int INF = 1e9;
const char nl = '\n';
ll power(ll a, ll b) {return b? power(a*a%MOD, b/2)*(b%2?a:1)%MOD:1;}
ll C(int n, int k)
{
ll x = 1, y = 1;
for (int i = 1; i <= k; ++i)
{
x = x*(n-i+1)%MOD;
y = y*i%MOD;
}
return x*power(y, MOD-2)%MOD;
}
struct UF
{
vi d;
UF(int n) : d(n, -1) {}
int root(int x)
{
if (d[x] < 0) return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y)
{
x = root(x); y = root(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -d[root(x)]; }
};
bool f(ll base, string x, ll m)
{
ll sum = 0;
rep(i, x.size())
{
if (log10(sum) + log10(base) >= 18.5) return false;
sum *= base;
sum += x[i]-48;
}
return sum <= m;
}
int main()
{
string x; cin >> x;
ll m ; cin >> m;
if (x.size() == 1)
{
cout << (stoll(x) <= m) << endl;
return 0;
}
int n = 0;
rep(i, x.size()) chmax(n, x[i]-48);
ll left = 1;
ll right = m+1;
while (right - left > 1)
{
ll mid = left + (right-left)/2;
if (f(mid, x, m)) left = mid;
else right = mid;
}
cout << max(0LL, left-n) << endl;
}
|
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>
const ll mod = 1000000007;
const int INF = 1001001001;
int main(){
int a, b;
cin >> a >> b;
int c = a + b;
int ans;
if(c >= 15 && b >= 8){
ans = 1;
}else if(c >= 10 && b >= 3){
ans = 2;
}else if(c >= 3){
ans = 3;
}else{
ans = 4;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int g[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int l, r;
cin >> l >> r;
for (int i = 2; i <= r; i++) {
int x = ceil(1.0 * l / i), y = floor(1.0 * r / i);
g[i] = (y - x + 1) * (y - x + 1);
}
int ans = 0;
for (int i = max(l, 2ll); i <= r; i++)
ans -= r / i * 2 - 1;
for (int i = r; i >= 2; i--) {
for (int j = 2; i * j <= r; j++) {
g[i] -= g[i * j];
}
ans += g[i];
}
cout << ans;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
#define all(x) (x).begin(),(x).end()
#define Sort(x) sort(all((x)))
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl;
#define bug(x) cerr << #x << ": " << x << " ";
#define alog(a, n) for (int i = 0; i < n; i++) cout << a[i] << " "; cout << endl;
#define FOR(n) for (int i = 0; i < n; i++)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll poww(ll a, ll b, ll md) {
if (b == -1) return poww(a, md - 2, md);
return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}
const ll N = 1e6 + 10;
const ll LOG = 22;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;
int n, m, t;
int c[N][2];
int main(){
fast_io
cin >> n >> m >> t;
for(int i = 0; i < m; i++)
cin >> c[i][0] >> c[i][1];
int ch = n, lst = 0;
for(int i = 0; i < m; i++){
if(c[i][0] - lst >= ch)
return cout << "No" << endl, 0;
ch -= c[i][0] - lst;
ch = min(n, ch + c[i][1] - c[i][0]); lst = c[i][1];
}
if(t - c[m - 1][1] >= ch)
return cout << "No" << endl, 0;
cout << "Yes" << endl; return 0;
}
| #include"bits/stdc++.h"
//#include"atcoder.h"
#include<iostream>
using namespace std;
typedef long long ll;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
const ll inf = ll(1e18) + 1;
const ll mod = 1000000007;
int main() {
ll n,m,t;
cin >> n >> m >> t;
vector<ll>a(m);
vector<ll>b(m);
rep(i,m) {
cin >> a[i] >> b[i];
}
ll now = 0;
ll ma = n;
rep(i,m) {
n -= a[i] - now;
if (n <= 0) {
cout << "No" << endl;
return 0;
}
n += b[i] - a[i];
if (n > ma) {
n = ma;
}
now = b[i];
}
if (n > t-now) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const double pi=3.1415926535897932384626;
inline ll read(){
ll 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 ll mod=998244353;
const int maxn=5e2+5;
ll n,m;
char s[maxn][maxn];
ll q_pow(ll x,ll y){
ll ans=1;
while(y){
if(y&1) ans=ans*x%mod;
y>>=1;
x=x*x%mod;
}
return ans;
}
int main(){
n=read(),m=read();
for(int i=1;i<=n;++i) scanf("%s",s[i]+1);
ll cnt=0,flag=1;
for(int i=1;i<=n;++i){
int x=i,y=1,r=0,b=0,tmp=0;
while(x>=1&&y<=m){
if(s[x][y]=='.') ++tmp;
else if(s[x][y]=='R') r=1;
else b=1;
--x;
++y;
}
if(r==1&&b==1) flag=0;
else if(r+b==0&&tmp) ++cnt;
}
for(int i=2;i<=m;++i){
int x=n,y=i,r=0,b=0,tmp=0;
while(x>=1&&y<=m){
if(s[x][y]=='.') ++tmp;
else if(s[x][y]=='R') r=1;
else b=1;
--x;
++y;
}
if(r==1&&b==1) flag=0;
else if(r+b==0&&tmp) ++cnt;
}
if(!flag) printf("0\n");
else printf("%lld\n",q_pow(2,cnt));
return 0;
}
| #include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;
const int maxX = 72, maxPrimes = 20;
bool notPrime[maxX + 1];
vector<lli> primes;
const int maxN = 72;
lli a, b; int n;
int primesMask[maxN + 1];
lli memo[maxN + 1][1LL << maxPrimes];
lli solve(int i = 0, int mask = 0)
{
if (i == n + 1)
return 1;
lli &ans = memo[i][mask];
if (ans != -1)
return ans;
ans = solve(i + 1, mask);
if (!(mask & primesMask[i]))
ans += solve(i + 1, mask | primesMask[i]);
return ans;
}
int main()
{
notPrime[0] = notPrime[1] = true;
for (int i = 2; i <= maxX; i ++)
{
if (!notPrime[i]) primes.push_back(i);
for (int j = 0; i*primes[j] <= maxX; j ++)
{
notPrime[i*primes[j]] = true;
if (i % primes[j] == 0) break;
}
}
while (~scanf("%lld %lld", &a, &b))
{
n = b - a;
for (lli i = a; i <= b; i++)
{
primesMask[i - a] = 0;
for (int j = 0; j < primes.size(); j++)
{
int p = primes[j];
if (i % p == 0)
primesMask[i - a] |= (1LL << j);
}
}
memset(memo, -1, sizeof(memo));
lli ans = solve();
printf("%lld\n", ans);
}
return 0;
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#pragma GCC optimize ("-O3")
#define int long long
#define ld long double
#define endl "\n"
#define rep(i,begin,end) for (__typeof(end) i=begin-(begin>end); i!=(end)-(begin>end); i+=1-2*(begin>end))
#define umap unordered_map
#define pq priority_queue
#define pb push_back
#define mp make_pair
#define fs first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define mii map<int,int>
#define pii pair<int,int>
#define vc vector
#define vi vc<int>
#define vvi vc<vi>
#define all(v) v.begin(),v.end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define w(t) int t; cin>>t; rep(tc,0,t)
#define clr(a,x) memset(a,x,sizeof a)
#define chkbit(x,i) ((x)&(1LL<<(i)))
#define setbit(x,i) ((x)|(1LL<<(i)))
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define ps(x,y) fixed<<setprecision(y)<<x
#define print(a, n) rep(i,0,n) cout<<a[i]<<" "; cout<<endl;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const int mod = 1e9+7;
const int mod2 = 998244353;
const int N = 2e5;
const int inf = 1e18;
const ld PI = 3.141592653589793;
//=====================================================
template<typename... T>
void in(T&... args) {((cin>>args), ...);}
template<typename... T>
void out(T&&... args) {((cout<<args<<" "), ...);}
template<typename... T>
void outln(T&&... args) {((cout<<args<<" "), ...); cout<<endl;}
int nxt(){int x;cin>>x;return x;}
int add(int a,int b,int mod){int res=(a+b)%mod;return (res<0)?res+mod:res;}
int mul(int a,int b,int mod){int res=(a*1LL*b)%mod;return (res<0)?res+mod:res;}
int range(int x,int y,int z) {return (x >= y && x <= z);}
struct customCompare {
bool operator () (int x, int y) {
return x>y;
}
};
//=====================================================
int calc(int x){
return ((x*(x+1))/2)%mod2;
}
void solve() {
int a,b,c;
in(a,b,c);
int ans = calc(a);
ans = mul(ans, calc(b), mod2);
ans = mul(ans, calc(c), mod2);
cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
// int t; cin >> t;
// for (int tc = 1; tc <= t; tc++) {
// cout << "Case #" << tc << ": ";
// solve();
// }
// printclock;
return 0;
} | #include"bits/stdc++.h"
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(x) x.begin(),x.end()
#define MOD 1000000007
#define INF INT_MAX
#define FLOAT_ANS setprecision(10)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } }
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
//最大公約数
template<typename T>
T gcd(T a, T b) {
if (a % b == 0) {
return(b);
}
else {
return(gcd(b, a % b));
}
}
//最小公倍数
template<typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <typename X, typename M>
struct SegTreeLazy {
using FX = function<X(X, X)>;
using FA = function<X(X, M)>;
using FM = function<M(M, M)>;
int n;
FX fx;
FA fa;
FM fm;
const X ex;
const M em;
vector<X> dat;
vector<M> lazy;
SegTreeLazy(int n_, FX fx_, FA fa_, FM fm_, X ex_, M em_)
: n(), fx(fx_), fa(fa_), fm(fm_), ex(ex_), em(em_), dat(n_ * 4, ex), lazy(n_ * 4, em) {
int x = 1;
while (n_ > x) x *= 2;
n = x;
}
void set(int i, X x) { dat[i + n - 1] = x; }
void build() {
for (int k = n - 2; k >= 0; k--) dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
}
/* lazy eval */
void eval(int k) {
if (lazy[k] == em) return; // 更新するものが無ければ終了
if (k < n - 1) { // 葉でなければ子に伝搬
lazy[k * 2 + 1] = fm(lazy[k * 2 + 1], lazy[k]);
lazy[k * 2 + 2] = fm(lazy[k * 2 + 2], lazy[k]);
}
// 自身を更新
dat[k] = fa(dat[k], lazy[k]);
lazy[k] = em;
}
void update(int a, int b, M x, int k, int l, int r) {
eval(k);
if (a <= l && r <= b) { // 完全に内側の時
lazy[k] = fm(lazy[k], x);
eval(k);
} else if (a < r && l < b) { // 一部区間が被る時
update(a, b, x, k * 2 + 1, l, (l + r) / 2); // 左の子
update(a, b, x, k * 2 + 2, (l + r) / 2, r); // 右の子
dat[k] = fx(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
void update(int a, int b, M x) { update(a, b, x, 0, 0, n); }
X query_sub(int a, int b, int k, int l, int r) {
eval(k);
if (r <= a || b <= l) { // 完全に外側の時
return ex;
} else if (a <= l && r <= b) { // 完全に内側の時
return dat[k];
} else { // 一部区間が被る時
X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
return fx(vl, vr);
}
}
X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
};
bool isLarger(string s,string t){
/*SのほうがTより大きければtrue*/
rep(i,min(s.length(),t.length())){
if(s[i]>t[i]){
return true;
}
if(s[i]<t[i]){
return false;
}
}
if(s.length()>t.length()){
return true;
}
else{
return false;
}
}
int main(void) {
ll n;
cin>>n;
vector<vector<ll>>v(n,vector<ll>(2));
rep(i,n){
cin>>v[i][0]>>v[i][1];
}
rep(i,n){
rep(j,n){
rep(k,n){
if(i==j||k==i||j==k){
continue;
}
ll x1=v[i][0]-v[j][0];
ll y1=v[i][1]-v[j][1];
ll x2=v[i][0]-v[k][0];
ll y2=v[i][1]-v[k][1];
if(x1*y2==x2*y1){
cout<<"Yes";
return 0;
}
}
}
}
cout<<"No";
}
/*
*/
|
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define ts to_string
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define dbg(a); cerr<<#a<<"="<<(a)<<endl;
#define dbg2(a,b); cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<endl;
#define dbgarr(a); for(auto elem:a)cerr<<elem<<" ";cerr<<endl;
#define panic(); cerr<<"code executes until here fine"<<endl;
#define out(a); cout<<a<<endl
using namespace std;
int main()
{
int x;cin>>x;
if(x<0) {
cout<<0<<endl;
}
else {
out(x);
}
}
| #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n%2 == 0) {
cout << "White" << endl;
} else {
cout << "Black" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i, a, b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
int main() {
char S[4];
cin >> S;
cout << S[1] << S[2] << S[0] << endl;
return 0;
} | #include <iostream>
using namespace std;
int main(){
string s;
cin>>s;
cout<<s[1]<<s[2]<<s[0];
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define mod ((int)1e9+7)
#define lim 1000000000000000007
#define lim1 18446744073709551615 //Unsigned
#define sq(a) ((a)*(a))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define mms(v,i) memset(v,i,sizeof(v))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define REPN(i,a,b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int,int> pi;
typedef pair<ll,ll> PL;
typedef pair<ll,int> PLI;
typedef pair<int,ll> PIL;
typedef pair<int,pair<int,int> > pii;
typedef pair<double,double> pdd;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
ll power(ll a,ll b) {
a %= mod;
ll res=1;
while(b){
if(b%2==1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll gcdll(ll a,ll b) {
if (b==0) return a;
return gcdll(b,a%b);
}
int gcd(int a,int b) {
if (b==0) return a;
return gcd(b,a%b);
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int N = (int)2e5+5;
const int M = (int)3e3+5;
const int Q = 301;
const int logN = 19;
bool adj[20][20];
vector<int> temp[1<<9];
int dp[1<<18];
void solve() {
int n,m,a,b;
cin >> n >> m;
if(m==0) {
cout << n;
return;
}
REP(i,1,m) {
cin >> a >> b;
--a, --b;
adj[a][b] = adj[b][a] = true;
}
m = (1<<n);
REP(i,1,m-1) {
dp[i] = __builtin_popcount(i);
if(dp[i]<=n/2) {
temp[dp[i]].pb(i);
}
bool ok = true;
REP(j,0,n-1) {
if((i>>j)&1) {
REP(k,0,n-1) {
if((i>>k)&1) {
if(j!=k && !adj[j][k]) {
ok = false;
break;
}
}
}
if(!ok) {
break;
}
}
}
if(ok) {
dp[i] = 1;
}
}
REP(i,1,m-1) {
int n = __builtin_popcount(i);
int k = n/2;
if(dp[i]>1) {
REP(c,1,k) {
for(auto j:temp[c]){
if((i&j)==j) {
dp[i] = min(dp[i], dp[j] + dp[i-j]);
}
}
}
}
}
cout << dp[m-1];
}
int main() {
//freopen("output.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T=1;
// cin>>T;
REP(TC,1,T) {
//cout<<"Case #"<<TC<<": ";
solve();
cout<<"\n";
}
}
| // "C:\Users\ianli\Desktop\CP\template\"
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define MTP make_tuple
typedef long long int ll;
constexpr int kN = 20;
bitset<kN> graph[kN];
int dp[1 << 18];
int main() {
//ios::sync_with_stdio(false);
//cin.tie(0);
int n, m, tot;
vector<int> v;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
graph[a][b] = graph[b][a] = true;
}
for (int i = 0; i < n; i++) graph[i][i] = true;
tot = 1 << n;
for (int i = 1; i < tot; i++) {
int tmp = i ^ (i & -i);
bitset<kN> bs = graph[__lg(i & -i)];
while (tmp) {
bs &= graph[__lg(tmp & -tmp)];
tmp ^= tmp & -tmp;
}
for (int j = 0; j < n; j++) if (i & (1 << j)) {
if (!bs[j]) goto Bad;
}
v.PB(i);
Bad:
continue;
}
for (int i = 0; i < tot; i++) dp[i] = n;
for (int i : v) dp[i] = 1;
for (int i = 0; i < tot; i++)
for (int j = i; j > 0; j = (j - 1) & (i ^ (i & -i))) dp[i] = min(dp[i], dp[j] + dp[i ^ j]);
printf("%d\n", dp[tot - 1]);
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int h,w;
cin>>h>>w;
vector<string> a(h);
vector<vector<int>> dp(h,vector<int>(w,0));
for(int i=0;i<h;i++)
cin>>a[i];
for(int i=h-2;i>=0;i--){
if((i+w-1)&1)
dp[i][w-1] = dp[i+1][w-1] - (a[i+1][w-1]=='+'?1:-1);
else
dp[i][w-1] = dp[i+1][w-1] + (a[i+1][w-1]=='+'?1:-1);
}
for(int j=w-2;j>=0;j--){
if((h-1+j)&1)
dp[h-1][j] = dp[h-1][j+1] - (a[h-1][j+1]=='+'?1:-1);
else
dp[h-1][j] = dp[h-1][j+1] + (a[h-1][j+1]=='+'?1:-1);
}
for(int i=h-2;i>=0;i--){
for(int j=w-2;j>=0;j--){
if((i+j)&1)
dp[i][j] = min(dp[i+1][j] - (a[i+1][j]=='+'?1:-1),dp[i][j+1] - (a[i][j+1]=='+'?1:-1));
else
dp[i][j] = max(dp[i+1][j] + (a[i+1][j]=='+'?1:-1),dp[i][j+1] + (a[i][j+1]=='+'?1:-1));
}
}
if(dp[0][0]>0)
cout<<"Takahashi"<<endl;
else if(dp[0][0]<0)
cout<<"Aoki"<<endl;
else
cout<<"Draw"<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define maxn 5005
int main()
{
int n,m;
cin>>n>>m;
vector<int> h(n),w(m);
for(int i=0;i<n;i++){
cin>>h[i];
}
for(int i=0;i<m;i++){
cin>>w[i];
}
sort(h.begin(),h.end());
ll pref[n]={0};
pref[0]=-h[0];
for(int i=1;i<n;i++){
if(i%2){
pref[i]=pref[i-1]+h[i];
}
else{
pref[i]=pref[i-1]-h[i];
}
}
ll suff[n+1]={0};
for(int i=n-1;i>=0;i--){
if(i%2){
suff[i]=suff[i+1]+h[i];
}
else{
suff[i]=suff[i+1]-h[i];
}
}
ll ans=1e18;
for(int i=0;i<m;i++){
ll tans=0;
auto ind=upper_bound(h.begin(),h.end(),w[i]);
int ind1=upper_bound(h.begin(),h.end(),w[i])-h.begin();
ind1--;
if(ind==h.end()){
if(w[i]<=h[0]){
tans-=w[i];
tans-=suff[0];
}
else{
tans+=pref[n-1];
tans+=w[i];
}
}
else{
tans=pref[ind1];
// cout<<ind1<<" "<<pref[ind1]<<" "<<suff[ind1+1]<<endl;
ind1++;
if(ind1%2){
tans+=w[i];
tans-=suff[ind1];
}
else{
tans-=w[i];
tans-=suff[ind1];
}
}
ans=min(ans,tans);
//cout<<tans<<endl;
}
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll mod = 1000000007;
int main() {
ll T, N, A, B;
ll a, b, ans;
cin >> T;
ll l, r;
rep(i, T) {
cin >> N >> A >> B;
a = max(A, B);
b = min(A, B);
l = (N - a + 1) * (N - a + 1) % mod;
r = (N - b + 1) * (N - b + 1) % mod;
ans = l * r % mod;
l = (N - b + 1) * (N - a + 1) % mod;
ll x = N - a - b;
if (x >= 0) {
r = (x + 1) * (x + 2) % mod;
} else {
r = 0;
}
ans -= (l - r) * (l - r) % mod;
ans %= mod;
if (ans < 0) ans += mod;
cout << ans << endl;
}
}
| #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()
const double EPS = 1e-7;
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};
//-------------------------------------
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
using mint = ModInt<MOD>;
int main() {
int t;
cin >> t;
while(t--) {
ll N, A, B;
cin >> N >> A >> B;
mint ans = mint(N - A + 1).pow(2) * mint(N - B + 1).pow(2);
mint tmp = mint(N - A + 1) * (N - B + 1);
if(N - A - B >= 0) tmp -= mint(N - A - B + 2) * mint(N - A - B + 1);
ans -= tmp.pow(2);
cout << ans << "\n";
}
} |
#include <set>
#include <unordered_set>
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <queue>
#include <bitset>
#define INFL 100100100100100100
#define INF 1100100100
using namespace std;
int main() {
long long b;
cin >> b;
long long c;
cin >> c;
long long br, bl, brr, bll;
br = b + (c-2)/2;
bl = b - c/2;
brr = -b + (c-1)/2;
bll = -b - (c-1)/2;
if (br < brr || bl < bll) {
swap(br, brr);
swap(bl, bll);
}
cout << br - bl + 1 + brr - bll + 1 - (bl <= brr ? brr - bl + 1: 0) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// #define int long long // intで書いたけど心配なときにlong longに変換する
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;
/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)
#define X real()
#define Y imag()
/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto&& i : n)
/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';
/* alias */
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using plglg = pair<ll, ll>;
using tlglglg = tuple<ll, ll, ll>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;
/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; cin >> x; return x;}
inline ll IN() {ll x; cin >> x; return x;}
inline vs split(const string& t, char c) {vs v; stringstream s(t); string b; while(getline(s, b, c)) v.eb(b); return v;}
template <typename T> inline bool chmin(T& a, const T& b) {if (a > b) a = b; return a > b;}
template <typename T> inline bool chmax(T& a, const T& b) {if (a < b) a = b; return a < b;}
template <typename T, typename S> inline void print(const pair<T, S>& p) {cout << p.first << " " << p.second << endl;}
template <typename T> inline void print(const T& x) {cout << x << endl;}
template <typename T, typename S> inline void print(const vector<pair<T, S>>& v) {for (auto&& p : v) print(p);}
template <typename T> inline void print(const vector<T>& v, string s = " ") {REP(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");}
inline void yesno(bool ok, string yes = "Yes", string no = "No") {cout << (ok?yes:no) << endl;}
signed main() {
//my-code
ll N, D, H; cin >> N >> D >> H;
vll d(N), h(N); REP(i, N) cin >> d[i] >> h[i];
double ans = (double) 0.0;
REP(i, N) {
double x = 0.0;
if (d[i] == D) {
x = h[i];
} else {
x = h[i] - (h[i]-H)*d[i]*1.0/(d[i]-D);
}
chmax(ans, x);
}
cout << fixed << ans << endl;
return 0;
}
// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々 |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i++)
#define fr(i,k,n) for(int i=k;i>=n;i--)
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define dbg(x) cout << (#x) << " is " << (x) << '\n'
#define F first
#define S second
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound
#define mems(x) memset(x,0,sizeof(x))
#define all(a) a.begin(),a.end()
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
/*---------------------------------------------------------------------------------------------------*/
void solve()
{
int n;
cin >> n;
map<string,int> mp,mp1;
int ok=0;
string res;
res="";
f(i,n)
{
string x;
cin >> x;
if(x[0]=='!')
{
if((int)x.length()==1)
continue;
string y=x.substr(1,(int)x.length()-1);
mp[y]++;
if(mp[y]>=1 && mp1[y]>=1)
{res=y;ok=1;}
}
else
{
mp1[x]++;
if(mp[x]>=1 && mp1[x]>=1)
{
res=x;ok=1;
}
}
}
if(ok)
cout << res << '\n';
else
cout << "satisfiable\n";
return;
}
signed main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
FAST
int T=1;
// cin >> T;
while(T--)
{
solve();
}
return 0;
} | // Sakhiya07 - Yagnik Sakhiya
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(x) x.begin(),x.end()
#define pll pair<ll,ll>
#define ff first
#define ss second
#define MOD 1000000007
const int N = 100005;
void solve()
{
ll n;
cin >> n;
map<ll,ll> y;
bool chk = 1;
for(ll i=0,x;i<n;i++) {
cin >> x;
y[x]++;
}
for(ll i = 1 ; i <= n ; i++) {
if(y[i] != 1) chk = 0;
}
if(chk) cout <<"Yes\n";
else cout << "No\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
for(int i = 1; i < t + 1; i++) {
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
if(a != b && b != c && c != a){
cout << 0 << endl;
}else if(a == b){
cout << c << endl;
}else if(b == c){
cout << a << endl;
}else if(a == c){
cout << b << endl;
}
} | #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; }
int main(){
cin.tie(0), ios::sync_with_stdio(false);
int a,b,c; cin >> a >> b >> c;
if(c%2 == 0){
a = abs(a);
b = abs(b);
if(a == b) cout << "=\n";
if(a < b) cout << "<\n";
if(a > b) cout << ">\n";
}else{
if(a == b) cout << "=\n";
if(a < b) cout << "<\n";
if(a > b) cout << ">\n";
}
return 0;
}
|
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define inp 200005
#define check exit(0)
#define nl cout<<endl;
#define mod 1000000007
#define ll long long int
#define trace(x) cerr<<#x<<" : "<<x<<endl;
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(v) for(int i=0;i<v.size();i++) {cout<<v[i]; (i==v.size()-1) ? cout<<"\n":cout<<" "; }
#define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
// Pick yourself up, 'cause...
int main()
{
jaldi
int n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;i++) cin>>v[i];
vector<ll> ans(n);
ordered_set os;
for(int i=0;i<n;i++)
{
ans[0] += v[i] - os.order_of_key(v[i]);
os.insert(v[i]);
}
for(int i=1;i<n;i++)
{
ans[i] = ans[i-1] - v[i-1] + (n - 1 - v[i-1]);
}
for(ll x:ans) cout<<x<<" \n";
return 0;
}
// Try till you are out of ideas | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;
template<typename T> class segtree {
private:
int n,sz,h;
vector<T> node, lazy;
void eval(int k) {
if(lazy[k]){
node[k] += lazy[k];
if(k < n) {
lazy[k*2] += lazy[k] / 2, lazy[k*2+1] += lazy[k] / 2;
}
lazy[k] = 0;
}
}
public:
segtree(const vector<T>& v) : n(1), sz((int)v.size()), h(0) {
while(n < sz) n *= 2, h++;
node.resize(2*n, 0), lazy.resize(2*n, 0);
for(int i = 0; i < sz; i++) node[i+n] = v[i];
for(int i = n-1; i >= 1; i--) node[i] = node[2*i] + node[2*i+1];
}
void range(int a, int b, T x, int k=1, int l=0, int r=-1){
if(r < 0) r = n;
eval(k);
if(b <= l || r <= a){
return;
}
if(a <= l && r <= b){
lazy[k] += (r-l)*x;
eval(k);
}else{
range(a, b, x, 2*k, l, (l+r)/2);
range(a, b, x, 2*k+1, (l+r)/2, r);
node[k] = node[2*k] + node[2*k+1];
}
}
T query(int a, int b) {
a += n, b += n - 1;
for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i);
b++;
T res1 = 0, res2 = 0;
while(a < b) {
if(a & 1) eval(a), res1 += node[a++];
if(b & 1) eval(--b), res2 += node[b];
a >>= 1, b >>= 1;
}
return res1 + res2;
}
void _print(){for(int i = 0; i < sz; i++) cout<<query(i,i+1)<< " ";cout<<endl;}
};
int main(){
cin.tie(0); ios::sync_with_stdio(false);
ll n; cin >> n;
ll a[n]; zep(i, 0, n)cin >> a[i];
vector<ll> v(n, 0);
segtree<ll> s(v);
ll ans = 0;
ll cnt[n]; memset(cnt, 0, sizeof(cnt));
zep(i, 0, n){
ans += s.query(a[i], n);
cnt[a[i]] = a[i] - s.query(0, a[i]);
s.range(a[i], a[i] + 1, 1);
}
//printa(cnt, 0, n - 1)
vector<ll> u(n, 0);
segtree<ll> t(u);
print(ans)
zep(i, 0, n - 1){
ans += n - i - 1 - 2 * cnt[a[i]];
//print(ans)
ans += t.query(a[i], n) - t.query(0, a[i]);
print(ans)
t.range(a[i], a[i] + 1, 1);
}
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<(int)(n);++i)
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
vector<int> vec(n);
vector<ll> re_sum(n);
rep(i,n) {
cin >> vec[i];
}
sort(vec.begin(), vec.end());
ll sum = 0, ans = 0;
for (ll i = 0; i < n; ++i) {
ans += vec[i]*i - sum;
sum += vec[i];
}
cout << ans << '\n';
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*
*/
ll N;
bool is_sorted(ll G[])
{
rep(i, N-1){
if (G[i] > G[i+1]) return false;
}
return true;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
ll T;
cin >> T;
rep(i, T){
cin >> N;
ll G[N];
ll cnt = 0;
vector<ll> ans;
rep(j, N) cin >> G[j];
while (!is_sorted(G)){
ll tmp = -1;
if (cnt%2 == 0){
for(ll k=0; k+1<N; k+=2){
if (G[k] < G[k+1]) continue;
tmp = k;
}
if (tmp == -1) tmp = 0;
}
if (cnt%2 == 1){
for(ll k=1; k+1<N; k+=2){
if (G[k] < G[k+1]) continue;
tmp = k;
}
if (tmp == -1) tmp = 1;
}
swap(G[tmp], G[tmp+1]);
ans.push_back(tmp+1);
cnt++;
}
ll asz = ans.size();
cout << asz << endl;
rep(j, asz){
cout << ans[j];
if (j == asz-1) cout << endl;
else cout << " ";
}
}
}
|
//#ifdef DEBUG
//#define _GLIBCXX_DEBUG
//#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
const int maxN = 1005;
int dist[maxN][maxN];
int a[maxN];
int b[maxN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("input.txt", "r", stdin);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
// int ni = i;
// if (i & (1 << r)) ni -= (1 << r);
a[i] = (2 * i) % n;
b[i] = (2 * i + 1) % n;
a[i] = max(a[i], 0);
a[i] = min(a[i], n - 1);
b[i] = max(b[i], 0);
b[i] = min(b[i], n - 1);
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// dist[i][j] = 500;
// }
// dist[i][i] = 0;
// if (i != a[i]) dist[i][a[i]] = 1;
// if (i != b[i]) dist[i][b[i]] = 1;
// }
// for (int k = 0; k < n; k++) {
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
// }
// }
// }
for (int i = 0; i < n; i++) {
cout << a[i] + 1 << " " << b[i] + 1 << '\n';
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
struct A {
int id;
int l, r;
A(int id) : id(id) {
l = r = 0;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<A> v;
for (int i = 1; i <= n; ++i) {
v.push_back(A(i));
if (i * 2 <= n) v.back().l = 2 * i;
if (i * 2 + 1 <= n) v.back().r = 2 * i + 1;
}
reverse(v.begin(), v.end());
for (int i = 0; i < n; ++i) {
if (v[i].r == 0 && 2 * i + 2 < n) v[i].r = v[2 * i + 2].id;
if (v[i].l == 0 && 2 * i + 1 < n) v[i].l = v[2 * i + 1].id;
}
reverse(v.begin(), v.end());
for (auto& x : v) {
if (x.l == 0) x.l = 1;
if (x.r == 0) x.r = n;
cout << x.l << ' ' << x.r << '\n';
}
} |
#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 SZ(c) ((ll)(c).size())
#define BIT(b,i) (((b)>>(i)) & 1)
#define PCNT(b) __builtin_popcountll(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(0);
cout << fixed << setprecision(15);
ll N; cin >> N;
VLL A(N),T(N); REP(i,N) cin >> A[i] >> T[i];
ll Q; cin >> Q;
VLL X(Q); REP(i,Q) cin >> X[i];
ll L = -INF, R = INF;
ll S = 0; // shift
REP(i,N) {
ll a = A[i];
ll t = T[i];
if (t == 1) { // x + a
S += a;
L += a;
R += a;
} else if (t == 2) { // max(x, a)
chmax(L, a);
if (R < L) R = L;
} else if (t == 3) { // min(x, a)
chmin(R, a);
if (R < L) L = R;
}
}
// debug(L); debug(R);
// debug(S);
for (ll x : X) {
if (x + S < L) {
COUT(L);
} else if (R < x + S) {
COUT(R);
} else {
COUT(x + S);
}
}
// REP(_,L) COUT(X[L] + S);
// FOR(i,L,R+1) COUT(X[i] + S);
// REP(_,(Q-1)-R) COUT(X[R] + S);
return 0;
}
| #include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007LL
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define SHIFT(n) (1LL<<((ll)n))
#define ALL(a) (a).begin(),(a).end()
#define Max(a) (*max_element(ALL(a)))
#define Min(a) (*min_element(ALL(a)))
using namespace std;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll n;cin>>n;
ll ma = -2*llINF,mi = 2*llINF,plus = 0;
while(n--){
ll a,t;cin>>a>>t;
if(t == 1){
ma += a;
mi += a;
plus += a;
}else if(t == 2){
ma = max(ma,a);
mi = max(mi,a);
}else{
ma = min(ma,a);
mi = min(mi,a);
}
}
ll q;cin>>q;
while(q--){
ll a;cin>>a;
//cout << ma << " " << mi << endl;
cout << min(mi,max(ma,a+plus)) << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert
#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}
using namespace std;
template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}
/* Some Libraries */
//-------------------------------------------------
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
vll a(4);
rep(i,4) cin>>a[i];
rep(i,1<<4){
ll sum=0, rest=0;
rep(j,4){
if(i>>j&1){
sum+=a[j];
}else{
rest+=a[j];
}
}
if (sum==rest){
cout<<"Yes\n";
return 0;
}
}
cout<<"No\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
vector<int> A(n + m);
for (auto &a: A)
cin >> a;
sort(A.begin(), A.end());
vector<int> B;
for (const auto &a: A){
if (B.size() > 0 and B.back() == a)
B.pop_back();
else
B.push_back(a);
}
for (auto &b: B)
cout << b << " ";
cout << endl;
} |
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
ll gcd(ll x,ll y){
x = abs(x);
y = abs(y);
if(x < y){
swap(x,y);
}
if(y == 0){
return x;
}else{
return gcd(y,x%y);
}
}
//最小公倍数
ll lcm(ll x,ll y){
return (x*y)/gcd(x,y);
}
ll extgcd(ll a,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;
}
ll mod_inverse(ll a,ll m){
ll x,y;
extgcd(a,m,x,y);
return (m+x%m)%m;
}
void func(){
ll N,S,K;
scanf("%lld %lld %lld",&N,&S,&K);
ll common = gcd(K,N);
if(S%common != 0){
printf("-1\n");
return;
}
N /= common;
S /= common;
K /= common;
S = (-S+N)%N;
ll work = mod_inverse(K,N);
ll ans = (S*work)%N;
printf("%lld\n",ans);
}
int main(){
int num_case;
scanf("%d",&num_case);
for(int loop = 0; loop < num_case; loop++){
func();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
long long n;
cin >> n;
vector<int> a(n);
for(auto &nx : a){cin >> nx;}
long long res1=(n*(n-1)/2);
sort(a.begin(),a.end());
a.push_back(-1);
long long cnt=1;
for(int i=0;i<n;i++){
if(a[i]!=a[i+1]){
res1-=((cnt*(cnt-1))/2);
cnt=1;
}
else{cnt++;}
}
cout << res1 << '\n';
//cout << res2 << '\n';
}
|
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> P;
const int INF = 1e9;
const int MOD = 1e9+7;
int main(){
string s;
cin >> s;
rep(i,s.size()){
if(s[i] == '6') s[i] = '9';
else if(s[i] == '9') s[i] = '6';
}
reverse(all(s));
cout << s << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i,m,n) for(int i = m - 1; i >= n; i--)
#define ALL(v) v.begin(), v.end()
#define itn int
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define YES() cout << "YES" << endl
#define NO() cout << "NO" << endl
#define println(x) cout << x << endl
#define print(x) cout << x << " "
template<typename T, typename U>
inline bool CMAX(T &m, U x) { if (m < x) { m = x; return true; } return false; }
template<typename T, typename U>
inline bool CMIN(T &m, U x) { if (m > x) { m = x; return true; } return false; }
typedef long long lint;
typedef long double ldouble;
const int INF = 1e9;
const lint LINF = 1e18;
const int MOD = 1e9+7;
int main(){
string s;
cin >> s;
string ans;
REPR(i, s.size()){
if(s[i] == '0'){
ans += "0";
}
if(s[i] == '1'){
ans += "1";
}
if(s[i] == '6'){
ans += "9";
}
if(s[i] == '8'){
ans += "8";
}
if(s[i] == '9'){
ans += "6";
}
}
println(ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair <int,int>
#define F first
#define S second
#define ever (;;)
const int N = 100100;
int n,m,x,y,par[N];
ll dis[N],inf=1e18;
vector < pair <int,ii> > adj[N];
priority_queue < pair < ll , int > , vector < pair < ll , int > > , greater < pair < ll , int > > > q;
int get(int x) { return par[x] == x ? x : par[x] = get(par[x]); }
void mrg(int x,int y)
{
x = get(x);
y = get(y);
if( x == y )
return;
par[x] = y;
}
void dijkstra(int p)
{
for(int i=1;i<=n;i++)
dis[i] = inf;
dis[p] = 0;
q.push( { 0 , p } );
while( !q.empty() )
{
// ll w = q.top().F;
int u = q.top().S;
ll w = dis [u];
q.pop();
//if( dis[u] < w )
//continue;
for(auto &X:adj[u])
{
int x = X.F;
int t = X.S.F;
int k = X.S.S;
int toreach = (k-w%k)%k;
if( dis[x] > dis[u] + toreach + t )
{
dis[x] = dis[u] + toreach + t;
q.push( { dis[x] , x } );
}
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&x,&y);
for(int i=1;i<=n;i++)
par[i] = i;
for(int i=1;i<=m;i++)
{
int a,b,t,k;
scanf("%d%d%d%d",&a,&b,&t,&k);
adj[a].push_back( { b , { t , k } } );
adj[b].push_back( { a , { t , k } } );
mrg(a,b);
}
if( get(x) != get(y) )
{
printf("-1\n");
return 0;
}
dijkstra(x);
printf("%lld\n",dis[y]);
}
| #include "bits/stdc++.h"
/*
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
// a new data structure defined. Please refer below
// GNU link : https://goo.gl/WVDL6g
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set;
//new_data_set p;
//p.find_by_order(3)
//p.order_of_key(6)
*/
//#include <algorithm>
#define ll long long
#define mod 1000000007
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define lb lower_bound
#define ub upper_bound
#define endll "\n";
#define all(x) (x).begin(), (x).end()
#define forr(it,x) for(auto it=x.begin();it!=x.end();it++)
#define for1(i,a,b) for(int i=a;i<b;i++)
#define for2(i,a,b) for(int i=a;i>=b;i--)
#define for3(i,a,b) for(long long int i=a;i<b;i++)
#define for4(i,a,b) for(long long int i=a;i>=b;i--)
#define pp pair<int,int>
#define ld long double
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpp vector<pp>
#define vi vector<int>
#define vpll vector<pll>
#define vld vector<ld>
#define vvld vector<vld>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvpll vector<vpll>
#define vvpp vector<vpp>
#define I insert
#define mem(a,x) memset(a,x,sizeof(a))
#define p_queue(a) priority_queue<a,vector<a>,greater<a>>
using namespace std;
//const int dx[8]={1,0,-1,0,1,1,-1,-1},dy[8]={0,1,0,-1,-1,1,-1,1};
//const int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
const int N=2e5+5;
const int INF = 1e9+5;
/*vector<bool> p(N+1, true);
void sieve()
{
p[0] = p[1] = false;
int i,j;
for (i = 2; i*i <= N; i++)
{
if (p[i])
{
for (j = i * i; j <=N; j += i)
{
p[j] =false;
}
}
}
}*/
/*ll power(ll a1, ll b1){
ll res = 1;
while(b1 > 0){
if(b1 % 2){
res = 1LL * res * a1 % mod;
}
a1 = 1LL * a1 * a1 % mod;
b1 /= 2;
}
return res;
}
ll invmod(ll a1,ll b1){
return power(a1,b1-2);
}*/
/*ll ncr(int n1,int c1){
if(n1<c1)
return 0;
if(n1==c1||c1==0)
return 1;
ll n=n1,c=c1;
vll fact(n+1,1);
for1(i,1,n+1){
fact[i]=fact[i-1];
fact[i]*=i;
fact[i]%=mod;
}
ll fact1=fact[n];
ll fact2=fact[c];
ll fact3=fact[n-c];
fact1*=invmod(fact2,mod);fact1%=mod;
fact1*=invmod(fact3,mod);fact1%=mod;
return fact1;
}*/
/*int gcd(int a,int b){
if(a<b) swap(a,b);
if(b==0) return a;
a%=b;
return gcd(b,a);
}*/
/*int lcm(int a,int b){
return (a*1LL*b)/gcd(a,b);
}*/
vll dist,vis;
vector<vector<int>> adj[N];
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
int n,m,x,y,u,v,t1,k1;cin>>n>>m>>x>>y;
dist.resize(n+1,1e18);vis.resize(n+1,0);
for1(i,0,m){
cin>>u>>v>>t1>>k1;
adj[u].pb({v,k1,t1});
adj[v].pb({u,k1,t1});
}
dist[x]=0;
p_queue(pll) pq;
for1(i,1,n+1){
pq.push({dist[i],i});
}
while(!pq.empty()){
pair<ll,int> src=pq.top();pq.pop();
//cout<<src.se<<endll;
if(!vis[src.se]){
vis[src.se]=1;
for(auto x:adj[src.se]){
if(!vis[x[0]]){
/*ll t2=(src.fi+x[2]+x[1]-1)/(x[1]);
t2*=x[1];
if(dist[x[0]]>src.fi+x[1]){
dist[x[0]]=src.fi+x[1];pq.push({t2,x[0]});
}
//cout<<x[0]<<" "<<dist[x[0]]<<" "<<t2<<endll;*/
ll t2=(src.fi+x[1]-1)/x[1];
t2*=x[1];t2+=x[2];
if(dist[x[0]]>t2){
dist[x[0]]=t2;pq.push({t2,x[0]});
}
}
}
}
}
//for1(i,1,n+1) cout<<dist[i]<<" ";
//cout<<endll;
if(dist[y]==1e18){
cout<<-1<<endll;
}
else{
cout<<dist[y]<<endll;
}
return 0;
}
/*
*/
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const int MAX_P = 3;
int fact[MAX_P];
int extgcd(int a, int b, int& x, int& y) {
int d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1; y = 0;
}
return d;
}
int mod_inverse(int a, int m) {
int x, y;
extgcd(a, m, x, y);
return (m + x % m) % m;
}
// n!=a p^e としたときのa mod pを求める. O(p)
int mod_fact(int n, int p, int&e) {
e = 0;
if (n == 0) return 1;
// pの倍数の部分を計算
int res = mod_fact(n / p, p, e);
e += n / p;
// (p-1)!=-1なので(p-1)!^(n/p)はn/pの偶奇だけで計算できる
if (n / p % 2 != 0) return res * (p - fact[n % p]) % p;
return res * fact[n % p] % p;
}
// nCk mod pを求める. O(log_p n)
int mod_comb(int n, int k, int p) {
if (n < 0 || k < 0 || n < k) return 0;
int e1, e2, e3;
int a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2), a3 = mod_fact(n - k, p, e3);
if (e1 > e2 + e3) return 0;
return a1 * mod_inverse(a2 * a3 % p, p) % p;
}
int main() {
fact[0] = 1;
fact[1] = 1;
fact[2] = 2;
int N;
string c;
cin >> N >> c;
int s = 0;
rep(i, N) {
int x;
if (c[i] == 'B') x = 0;
if (c[i] == 'W') x = 1;
if (c[i] == 'R') x = 2;
s = (s + x * (2 - (N % 2)) * mod_comb(N - 1, i, 3)) % 3;
//cout << mod_comb(N - 1, i, 3) << " " << s << endl;
}
if (s == 0) printf("B\n");
if (s == 1) printf("W\n");
if (s == 2) printf("R\n");
} | #include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;
const int N=0;
void solve(){
int a,b,c,d;
cin >> a >> b >> c >> d;
cout << a * d - b * c;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T, class U>
void chmin(T& t, const U& u) {
if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
if (t < u) t = u;
}
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 << "{";
rep(i, v.size()) {
if (i) os << ",";
os << v[i];
}
os << "}";
return os;
}
#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
int main() {
int N;
cin >> N;
V<int> A(N), B(N);
rep(i, N) cin >> A[i];
rep(i, N) cin >> B[i];
ll ans = 0;
ll s = 0;
V<ll> v1, v2;
rep(i, N) {
s += A[i];
if (i & 1) {
v1.pb(A[i] - B[i]);
} else {
v2.pb(A[i] - B[i]);
}
}
sort(ALL(v1));
sort(ALL(v2));
ans = s;
rep(i, N / 2) {
s -= v1[i] + v2[i];
chmax(ans, s);
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define ll long long
#define sz(a) ((int) a.size())
#define vi vector<int>
using namespace std;
const int N = 2e5 + 7;
int n, a[N], b[N], r1[N], r2[N];
ll sum;
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
L(i, 1, n) cin >> a[i];
L(i, 1, n) cin >> b[i], a[i] -= b[i], sum += b[i];
n >>= 1;
L(i, 1, n) r1[i] = a[i * 2 - 1];
L(i, 1, n) r2[i] = a[i * 2];
sort(r1 + 1, r1 + n + 1);
sort(r2 + 1, r2 + n + 1);
L(i, 1, n) if(r1[i] + r2[i] > 0) sum += r1[i] + r2[i];
cout << sum << '\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std ;
#define int long long int
const int mod = 998244353;
void TESTCASE(){
int H, W; cin >> H >> W;
vector<vector<char>> grid(H+1, vector<char>(W+1, '.'));
int K; cin >> K;
for(int q = 1, h, w; q <= K; q++){
char c;
cin >> h >> w >> c;
grid[h][w] = c;
}
vector<vector<int>> dp(H+1, vector<int>(W+1, 0));
vector<vector<int>> ways1(H+1, vector<int>(W+1, 1)), ways2(H + 1, vector<int>(W+1, 1));
for(int i = 1; i <= H; i++){
for(int j = 1; j <= W; j++){
if(grid[i][j] == '.') ways1[i][j] = ways1[i][j-1] * 3 % mod;
else ways1[i][j] = ways1[i][j-1];
}
}
for(int j = 1; j <= W; j++){
for(int i = 1; i <= H; i++){
if( grid[i][j] == '.' ) ways2[i][j] = ways2[i-1][j] * 3 % mod;
else ways2[i][j] = ways2[i-1][j];
}
}
for(int i = 1; i <= H; i++){
for(int j = 1; j <= W; j++){
if( i == 1 && j == 1 ){
dp[i][j] = 1;
}
else{
int up = 0, left = 0, sl = 1, su = 1;
if(i - 1 >= 1 && grid[i-1][j] == 'D'){
up = dp[i-1][j];
}
else if( i - 1 >= 1 && grid[i-1][j] == 'X'){
up = dp[i-1][j];
}
else if(i - 1 >= 1 && grid[i-1][j] == '.'){
up = dp[i-1][j] * 2; up %= mod;
}
if(j - 1 >= 1 && grid[i][j-1] == 'R'){
left = dp[i][j-1];
}
else if(j - 1 >= 1 && grid[i][j-1] == 'X'){
left = dp[i][j-1];
}
else if(j - 1 >= 1 && grid[i][j-1] == '.'){
left = dp[i][j-1] * 2; left %= mod;
}
dp[i][j] = ((up * ways1[i][j-1] % mod) + (left * ways2[i-1][j] % mod) % mod) ;
dp[i][j] %= mod;
}
}
}
int c = 1;
if(grid[H][W] == '.') c = 3;
else if(grid[H][W] == 'X') c = 2;
else c = 1;
cout << ((dp[H][W]*c) % mod) << "\n";
return;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
TESTCASE();
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 998244353
#define LLI long long int
#define REP(i, n) for(int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for(int i = s, i##_l = (e); i < i##_l; i++)
#define _min(a,b) ((a<b)?a:b)
#define _max(a,b) ((a<b)?b:a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a)?1:-1)
#define SORT(v) sort(v.begin(),v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9 lli 9.2E±18 double 1.7E±380
LLI power(LLI a, LLI b) { LLI res = 1; while (b > 0) { if (b & 1)res = res * a % MOD; a = a * a % MOD; b >>= 1; }return res; }
int main()
{
cout << fixed << setprecision(10);
int h, w, k;
cin >> h >> w >> k;
vector<vector<int>> m(h, vector<int>(w, '?'));
vector<vector<LLI>> dp(h, vector<LLI>(w, 0));
vector<vector<LLI>> qh(h, vector<LLI>(w + 1));
vector<vector<LLI>> qw(h + 1, vector<LLI>(w));
vector<LLI> powlist(_max(w + 1, h + 1));
powlist[0] = 1;
FOR(i, 1, powlist.size())
{
powlist[i] = (powlist[i - 1] * 3) % MOD;
}
REP(i, k)
{
int x, y;
char c;
cin >> y >> x >> c;
m[y - 1][x - 1] = c;
}
// 区間和
REP(y, h)
{
REP(x, w)
{
qh[y][x + 1] += qh[y][x] + (m[y][x] == '?' ? 1 : 0);
}
}
REP(x, w)
{
REP(y, h)
{
qw[y + 1][x] += qw[y][x] + (m[y][x] == '?' ? 1 : 0);
}
}
dp[0][0] = 1;
REP(y, h)
{
REP(x, w)
{
if (0 < y)
{
LLI mul = powlist[qh[y][x]];
if (m[y - 1][x] == 'D' || m[y - 1][x] == 'X')
{
dp[y][x] = (((dp[y - 1][x]) % MOD) * mul) % MOD + dp[y][x];
}
if (m[y - 1][x] == '?')
{
dp[y][x] = (((dp[y - 1][x] * 2) % MOD) * mul) % MOD + dp[y][x];
}
}
dp[y][x] %= MOD;
if (0 < x)
{
LLI mul = powlist[qw[y][x]];
if (m[y][x - 1] == 'R' || m[y][x - 1] == 'X')
{
dp[y][x] = (((dp[y][x - 1]) % MOD) * mul) % MOD + dp[y][x];
}
if (m[y][x - 1] == '?')
{
dp[y][x] = (((dp[y][x - 1] * 2) % MOD) * mul) % MOD + dp[y][x];
}
}
dp[y][x] %= MOD;
}
}
LLI ans = dp[h - 1][w - 1];
if (m[h - 1][w - 1] == '?')ans *= 3;
ans %= MOD;
cout << ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
using ll = long long;
using P = pair<ll,ll>;
const ll MAX = 1000000000000;
double p;
int main() {
string s;
cin >> s;
int n[10] = {0};
rep(i,s.size()){
n[s[i]-'0'] ++;
}
bool flag = false;
if(s.size() ==1){
if(stoi(s)%8 ==0) flag = true;
else flag = false;
}
else if(s.size() == 2){
if(((s[1]-'0')*10+(s[0]-'0')) %8 == 0 || ((s[0]-'0')*10+(s[1]-'0')) %8 == 0) flag = true;
else flag = false;
}
else{
rep(i,1000){
int a[10];
rep(i,10){
a[i] = n[i];
}
if(i%8 == 0){
a[i%10] --;
a[(i/10)%10] --;
a[(i/100)%10] --;
flag = true;
rep(i,10){
if(a[i] < 0){
flag = false;
}
}
if(flag) break;
}
}
}
if(flag) cout << "Yes" << endl;
else cout << "No" << endl;
} | #include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <stack>
#include <set>
#include <bitset>
#include <functional>
#include <cassert>
using namespace std;
#define MOD (long long int)(1e9+7)
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1e9)
#define LINF (long long int)(1e18)
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
ll mpow(ll a, ll b){
if(b==0) return 1;
else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;}
else return mpow(a,b-1) * a % MOD;
}
ll lpow(ll a, ll b){
if(b==0) return 1;
else if(b%2==0){ll memo = lpow(a,b/2); return memo*memo;}
else return lpow(a,b-1) * a;
}
ll gcd(ll a, ll b){
if(b==0) return a;
else return gcd(b, a%b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n){
if(kaijo_memo.size() > n) return kaijo_memo[n];
if(kaijo_memo.size() == 0) kaijo_memo.push_back(1);
while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD);
return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n){
if(gyaku_kaijo_memo.size() > n) return gyaku_kaijo_memo[n];
if(gyaku_kaijo_memo.size() == 0) gyaku_kaijo_memo.push_back(1);
while(gyaku_kaijo_memo.size() <= n) gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size()-1] * mpow(gyaku_kaijo_memo.size(), MOD-2) % MOD);
return gyaku_kaijo_memo[n];
}
ll nCr(ll n, ll r){
if(n == r) return 1;//0個の丸と-1個の棒みたいな時に時に効く?不安.
if(n < r || r < 0) return 0;
ll ret = 1;
if(n <= 1e7){
ret *= kaijo(n); ret %= MOD;
ret *= gyaku_kaijo(r); ret %= MOD;
ret *= gyaku_kaijo(n-r); ret %= MOD;
}else{
rep(i,r){
ret *= n-i; ret %= MOD;
ret *= mpow(r-i, MOD-2); ret %= MOD;
}
}
return ret;
}
vector<ll> yakusu(ll n){
vector<ll> ret;
for(ll p = 1; p*p <= n; p++){
if(n%p == 0){
ret.push_back(p);
if(p*p != n){
ret.push_back(n/p);
}
}
}
sort(all(ret));
return ret;
}
map<ll,ll> soinsu(ll n){
map<ll,ll> mp;
for(ll p = 2; p*p <= n; p++){
if(n%p == 0){
mp[p]++;
n /= p;
p--;
}
}
if(n > 1){
mp[n]++;
}
return mp;
}
int main(void){
fast_io
cout<<fixed<<setprecision(15);
int n;cin>>n;
vector<ll> A(n);
rep(i,n){
cin>>A[i];
}
ll ans = 0;
rep(i,n){
ans = gcd(ans, A[i]);
}
cout<<ans<<endl;
return 0;
} |
/*
Created by: Amit Kumar at 22:29 on 16 Mar 21
*/
#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define db(...)
#endif
using namespace std;
#define ll long long
#define mod 1000000007
#define S(x, n) memset(x, n, sizeof(x));
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define NIL -1
const int MAXN = 1e5 + 1;
long long power(long long x, long long y, long long md)
{
long long res = 1;
x = x % md;
while (y > 0)
{
if (y & 1)
res = (res * x) % md;
y = y >> 1;
x = (x * x) % md;
}
return res;
}
void solve()
{
int n;
cin >> n;
string t;
cin >> t;
string s;
cin >> s;
// true for Takahasi
vector<vector<bool>> dp(n + 1, vector<bool>(7, 0));
dp[n][0] = true;
for (int i = n - 1; i >= 0; i--)
{
if (s[i] == 'A')
{
for (int j = 0; j < 7; j++)
{
dp[i][j] = dp[i + 1][(j * 10) % 7] && dp[i + 1][((j * 10) + t[i] - '0') % 7];
}
}
else
{
for (int j = 0; j < 7; j++)
{
dp[i][j] = dp[i + 1][(j * 10) % 7] || dp[i + 1][((j * 10) + t[i] - '0') % 7];
}
}
}
db(dp);
if (dp[0][0])
cout << "Takahashi\n";
else
cout << "Aoki\n";
}
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
auto started = std::chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
//cin >> t;
for (int I = 1; I <= t; I++)
{
#ifdef LOCAL
cerr << "TestCase " << I << "\n";
#endif
solve();
}
#ifdef LOCAL
auto done = std::chrono::high_resolution_clock::now();
cerr << "Time = " << std::chrono::duration_cast<std::chrono::milliseconds>(done - started).count() << " ms\n";
#endif
return 0;
} | #include <cstdio>
using namespace std;
const int max_n = 200000;
char s[max_n+1], op[max_n+1];
bool wn[7] = {}, nwn[7];
int main()
{
int n, cur = 1;
bool sl1, sl2;
scanf("%d%s%s", &n, s, op);
wn[0] = true;
for (int i = n - 1; i >= 0; i--)
{
for (int j = 0; j < 7; j++)
nwn[j] = wn[j];
if (op[i] == 'A')
{
for (int j = 0; j < 7; j++)
if (!wn[(j+cur*(s[i]-'0'))%7])
nwn[j] = false;
}
else
{
for (int j = 0; j < 7; j++)
if (wn[(j+cur*(s[i]-'0'))%7])
nwn[j] = true;
}
sl1 = true, sl2 = false;
for (int j = 0; j < 7; j++)
{
wn[j] = nwn[j];
sl1 &= wn[j], sl2 |= wn[j];
}
if (sl1)
{
puts("Takahashi");
return 0;
}
else if (!sl2)
{
puts("Aoki");
return 0;
}
cur = (cur * 10) % 7;
}
if (wn[0])
puts("Takahashi");
else
puts("Aoki");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P = pair<int, int>;
int main(){
long double x,y,r; cin>>x>>y>>r;
r += 1e-14;
int lx = ceil(x-r);
int rx = floor(x+r);
ll ans = 0;
for(int i=lx; i<=rx; i++){
double uy = floor(y + sqrt(r*r - (x-i)*(x-i)));
double dy = ceil(y - sqrt(r*r - (x-i)*(x-i)));
ans += uy - dy + 1;
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai{
#define ms(a) memset(a,0,sizeof(a))
#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define ce(i,r) i==r?'\n':' '
#define pb push_back
#define all(x) x.begin(),x.end()
#define gmn(a,b) a=min(a,b)
#define gmx(a,b) a=max(a,b)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int infi=2e9;//infi??,????inf????int
const ll infl=4e18;
inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
inline int qi(){
int f=0,fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline ll ql(){
ll f=0;int fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline db qd(){
char c=getchar();int flag=1;double ans=0;
while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
if(c=='-') flag=-1,c=getchar();
while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
return ans*flag;
}
}
namespace Read{
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define rai(x,a,b) repi(i,a,b) x[i]=qi()
#define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
#define pi(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)
#define pc(x) printf("%c",x)
#define pe() puts("")
}
namespace DeBug{
#define MARK false
#define DB if(MARK)
#define pr(x) cout<<#x<<": "<<x<<endl
#define pra(x,a,b) cout<<#x<<": "<<endl; \
repi(i,a,b) cout<<x[i]<<" "; \
puts("");
#define FR(a) freopen(a,"r",stdin)
#define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
typedef long double ldb;
int main()
{
ldb x,y,r; cin>>x>>y>>r;
r+=1e-14;
ll ans=0;
repi(i,ceil(y-r),floor(y+r)){
ldb dy=abs(y-i);
if(dy>r) continue;
ldb dx=sqrt(r*r-dy*dy);
ll lx=1ll*ceil(x-dx),rx=1ll*floor(x+dx);
ans+=(rx-lx+1);
}
pl(ans),pe();
return 0;
}
|
#include <bits/stdc++.h>
namespace IO{
char buf[1000000],*p1,*p2;
inline char getc(){
if(p1==p2) p2=(p1=buf)+fread(buf,1,1000000,stdin);
return p1==p2?EOF:*(p1++);
}
template<typename tp>inline void r(tp &n){
n=0;char c=getc();
while(!isdigit(c)) c=getc();
while( isdigit(c)) n=n*10+c-48,c=getc();
}
template<typename tp,typename...Args>inline void r(tp &n,Args&...args){
r(n);r(args...);
}
template<typename tp>inline void write(tp n){
if(n/10) write(n/10);
putchar(n%10+48);
}
template<typename tp>inline void w(tp n,char c='\n'){
write(n);putchar(c);
}
};
using namespace IO;
const int N=3e3+5,Mod=998244353;
using namespace std;
int f[N][N];
int solve(int n,int k){
if(n<=k) return n==k;
if(k==0) return 0;
if(f[n][k]!=-1) return f[n][k];
return f[n][k]=(solve(n-1,k-1)+solve(n,2*k))%Mod;
}
int main(){
// freopen("1.in","r",stdin);
int n,k;r(n,k);
memset(f,-1,sizeof f);
w(solve(n,k));
return 0;
} | #include "bits/stdc++.h"
#define MOD 998244353
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};
ll mod(ll a, ll mod){
ll res = a%mod;
if(res<0)res=res + mod;
return res;
}
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;
}
ll modinv(ll a, ll mod){
ll b=mod, u=1, v=0;
while(b){
ll t=a/b;
a-=t*b; swap(a, b);
u-=t*v; swap(u, v);
}
u%=mod;
if(u<0)u+=mod;
return u;
}
ll gcd(ll a, ll b){
ll r = a%b;
if(r==0) return b;
else return gcd(b, a%b);
}
bool is_prime(ll n){
ll i = 2;
if(n==1)return false;
if(n==2)return true;
bool res = true;
while(i*i <n){
if(n%i==0){
res = false;
}
i = i+1;
}
//if(i==1)res = false;
if(n%i==0)res=false;
return res;
}
ll N, K;
vector<llvec> m;
ll f(ll n, ll k){
//cerr << n << " " << k << " ";
if(k<=0)return 0;
if(n<=0 or k<=0 or n<k){
m[n][k] = 0;
return 0;
}
if(n==k)m[n][k]=1;
if(m[n][k]>=0)return m[n][k];
ll tmp=0;
tmp = mod(f(n-1, k-1) + f(n, 2*k), MOD);
//cerr << tmp << endl;
m[n][k]=tmp;
return tmp;
}
/**************************************
** A main function starts from here **
***************************************/
int main(){
cin >> N >> K;
m = vector<llvec>(N+1, llvec(2*(N+1), -1));
cout << f(N, K);
return 0;
}
|
#include <bits/stdc++.h> //Header Files
#define ll long long int
#define mod 1000000007
#define endl "\n"
using namespace std;
template <class T>
void out(int t, T ans)
{
cout << "Case #" << t << ": " << ans << endl;
}
template <class T>
void out(int t, T ans[])
{
cout << "Case #" << t << ": ";
for (T val : ans)
cout << val << " ";
cout << endl;
}
template <class T>
void out(int t, vector<T> &ans)
{
cout << "Case #" << t << ": ";
for (T val : ans)
cout << val << " ";
cout << endl;
}
void runTest(int o)
{
// Start coding here ...
int n;
cin >> n;
ll a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a, a + n, greater<ll>());
ll pre[n];
pre[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--)
pre[i] = pre[i + 1] + a[i];
ll sum = 0;
for (int i = 0; i < n - 1; i++)
{
sum += abs(pre[i + 1] - (a[i] * (n - i - 1)));
}
cout << sum << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t;
for (int i = 0; i < t; i++)
runTest(i + 1);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n; cin >> n;
vector<ll> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
ll ans=0;
sort(v.begin(), v.end());
for (ll i = 0; i < n; i++) {
ans += (2ll*i - n + 1ll) * v[i];
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
int N, M; cin >> N >> M;
if(N == 1 && M == 0){
cout << 1 << " " << 2 << endl;
return 0;
}
if(M < 0){
cout << -1 << endl;
return 0;
} else if(M == N || M == N-1){
cout << -1 << endl;
return 0;
}
vector<int> L(N), R(N);
int X = N - 1 - M;
X--;
int now = 1;
rep(i, X){
L[i] = now;
now++;
R[i] = now;
now++;
}
L[X] = now;
now++;
R[X] = N * 2;
for (int i = X+1; i < N; i++){
L[i] = now;
now++;
R[i] = now;
now++;
}
rep(i, N){
cout << L[i] << " " << R[i] << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<db> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };
int main(){
int n,m;
scanf("%d%d",&n,&m);
if(m==0){
fup(i,1,n,1)printf("%d %d\n",i*2,i*2+1);
return 0;
}
if(m<0 || m>=n-1)return !printf("-1\n");
fup(i,1,m+1,1){
printf("%d %d\n",2*i,2*i+1);
}
puts("1 500000");
int rem=n-m-2;
fup(i,1,rem,1){
printf("%d %d\n",600000+2*i,600001+2*i);
}
} |
#include <bits/stdc++.h>
#define eprintf(args...) fprintf(stderr, args)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
const int mxn = 1e5 + 5;
const int mod = 1e9 + 7;
int f[mxn];
int n, a[mxn];
int main() {
scanf("%d", &n);
rep(i, n) scanf("%d", &a[i]);
f[0] = 1; f[1] = 2;
for (int i = 1; i < n; ++ i) f[i + 1] = (f[i - 1] + f[i]) % mod;
int ans = 0;
rep(i, n) {
int coef = 1LL * (i ? f[i - 1] : 1) * f[n - 1 - i] % mod;
if (i) coef = (coef + mod - 1LL * (i - 1 ? f[i - 2] : 1) * (n - 1 - i ? f[n - 2 - i] : 1) % mod) % mod;
(ans += 1LL * coef * a[i] % mod) %= mod;
}
printf("%d\n", ans);
return 0;
}
| #include<ctime>
#include<cstdio>
#include<cctype>
#define ll long long
using namespace std;
const ll N=1e5+7;
const ll p=1e9+7;
ll read(){
char c;ll x=0,f=1;
while(!isdigit(c=getchar()))f-=2*(c=='-');
while(isdigit(c))x=x*10+f*(c-48),c=getchar();
return x;
}
ll n,res,x,f[N],g[N],h[N];
int main(){
n=read(),h[1]=0,h[2]=1,f[1]=read();
for(ll i=3;i<=n;++i)h[i]=(h[i-1]+h[i-2])%p;
for(ll i=2;i<=n;++i){
x=read();
f[i]=(f[i-1]+x*h[i]+(i>2)*(g[i-1]+x*h[i-1]))%p;
g[i]=((f[i-1]-x*h[i])%p+p)%p;
}
res=(f[n]+g[n])%p;printf("%lld",res);
return 0;
} |
// Problem: D - Game in Momotetsu World
// Contest: AtCoder - Mynavi Programming Contest 2021(AtCoder Beginner Contest 201)
// URL: https://atcoder.jp/contests/abc201/tasks/abc201_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ld long double
#define pii int
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a,b,sizeof(a))
#define vi vector<int>
#define vii vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define w(x) int x; cin>>x; while(x--)
#define in(a) for(auto &x: a)cin>>x;
#define out(a) for(auto x: a){cout<<x<<' ';}cout<<'\n';
#define inf 1000000000
//#define mm 1000000007 998244353
void print(bool n){if(n)cout<<"YES";else cout<<"NO";cout<<'\n';}
int n,m;
vector<vector<short>> a;
vector<vector<int>> dp;
vector<vector<bool>> vis;
int fun(int i,int j){
if(vis[i][j])return dp[i][j];
int di[]={1,0};
int dj[]={0,1};
int ans=-inf;
for(int k=0;k<2;k++){
int I=i+di[k],J=j+dj[k];
if(I>=n || J>=m)continue;
ans=max(ans,a[I][J]-fun(I,J));
}
vis[i][j]=1;
if(ans==-inf)ans=0;
return dp[i][j]=ans;
}
signed main(){
cin>>n>>m;
a.resize(n,vector<short>(m,-1));
dp.resize(n,vector<int>(m));
vis.resize(n,vector<bool>(m,false));
char c;
rep(i,0,n){
rep(j,0,m){
cin>>c;
if(c=='+')a[i][j]=1;
}
}
int ans=fun(0,0);
/*for(auto z: dp){
for(auto x: z)cout<<x<<' ';
cout<<'\n';
}*/
if(ans==0)cout<<"Draw\n";
if(ans>0)cout<<"Takahashi\n";
if(ans<0)cout<<"Aoki\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}
int f(char a){
if(a=='+') return 1;
return -1;
}
//GCJ頑張る
int main() {
int H,W;
cin>>H>>W;
vector<vector<int>> dp(H,vector<int>(W));
vector<string> S(H);
rep(i,H) cin>>S[i];
for(int i=H-1;i>=0;i--) for(int j=W-1;j>=0;j--){
int a=(i+j)%2;
a*=-2;
a+=1;
if(i+j==H+W-2) continue;
int b,c;
if(j!=W-1){
b=f(S[i][j+1])*a;
dp[i][j]=b+dp[i][j+1];
}
if(i!=H-1){
c=f(S[i+1][j])*a;
dp[i][j]=c+dp[i+1][j];
}
if(i!=H-1&&j!=W-1){
/*cout<<a<<" "<<i<<" "<<j<<" "<<b<<" "<<c<<endl;
cout<<S[i][j+1]<<" "<<S[i+1][j]<<endl;*/
if(a==1){
dp[i][j]=max(b+dp[i][j+1],c+dp[i+1][j]);
}
else dp[i][j]=min(b+dp[i][j+1],c+dp[i+1][j]);
}
}
if(dp[0][0]>0) cout<<"Takahashi";
else if(dp[0][0]<0) cout<<"Aoki";
else cout<<"Draw";
cout<<"\n";
/*rep(i,H){
rep(j,W) cout<<dp[i][j]<<" ";
cout<<"\n";
}*/
}
|
#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<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
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 Per(i,sta,n) for(int i=n-1;i>=sta;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++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
template<int mod>
struct ModInt {
long long x;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
explicit operator int() const {return x;}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const{
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return ModInt(u);
}
ModInt power(long long p) const{
int a = x;
if (p==0) return 1;
if (p==1) return ModInt(a);
if (p%2==1) return (ModInt(a)*ModInt(a)).power(p/2)*ModInt(a);
else return (ModInt(a)*ModInt(a)).power(p/2);
}
ModInt power(const ModInt p) const{
return ((ModInt)x).power(p.x);
}
friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
using modint = ModInt<mod>;
ll n,a,b;
void solve(){
int V=a+b-2,W=a-1;
modint ans=n-b+1;
if(n-a-b>=0){
ans+=(modint)a*(modint)(n-a);
ans+=(modint)(b-1)*(modint)(n-a-b+1);
ans+=(modint)(n-a-b+1)*(modint)(n-a-b)/(modint)2;
}
else{
ans+=(modint)(n-b+1)*(n-a);
}
//cout << ans << endl;
if(a<=n-b){
ans-=(modint)(n-a-b+1)*(modint)(n-b-a+2)/(modint)2;
}
modint all=(modint)(n-a+1)*(modint)(n-a+1)*(modint)(n-b+1)*(modint)(n-b+1);
//cout << all << " " << ans << endl;
cout << all-ans*ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
int t;cin >> t;
rep(i,t){
cin >> n >> a >> b;
solve();
}
} | #include <bits/stdc++.h>
struct __fastio__ { __fastio__ () { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } __fast_io__;
template<class T, class U> bool chmin (T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; }
template<class T, class U> bool chmax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> void print(const T &value) { std::cout << value << '\n'; }
template<class T, class... Args> void print(const T &value, Args... args) { std::cout << value << ' '; print(args...); }
template<class T> using martix = std::vector<std::vector<T>>;
using i32 = std::int_fast32_t;
using u32 = std::uint_fast32_t;
using i64 = std::int_fast64_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
#include <iostream>
#include <cassert>
template<int Modulus>
struct modint {
using value_type = int;
static constexpr int mod = Modulus;
static_assert(mod > 1, "Modulus must be greater than 1");
private :
value_type x;
template<class T>
static constexpr value_type normalize (T value) noexcept {
if (value < 0) {
value = (-value) % mod;
return (value ? value : mod - value);
}
return (value % mod);
}
public :
explicit constexpr modint () noexcept : x(0) { }
template<class T>
constexpr modint (T value) noexcept : x(normalize(value)) { }
constexpr modint operator+ () const noexcept {
return modint(*this);
}
constexpr modint operator- () noexcept {
if (x != 0) x = mod - x;
return modint(*this);
}
constexpr modint &operator+= (const modint &p) noexcept {
if ((x += p.x) >= mod) x -= mod;
return (*this);
}
constexpr modint &operator-= (const modint &p) noexcept {
if ((x += mod - p.x) >= mod) x -= mod;
return (*this);
}
constexpr modint &operator*= (const modint &p) noexcept {
x = (int)(1LL * x * p.x % mod);
return (*this);
}
constexpr modint &operator/= (const modint &p) noexcept {
return ((*this) *= p.inverse());
}
constexpr modint operator+ (const modint &p) const noexcept {
return (modint(*this) += p);
}
constexpr modint operator- (const modint &p) const noexcept {
return (modint(*this) -= p);
}
constexpr modint operator* (const modint &p) const noexcept {
return (modint(*this) *= p);
}
constexpr modint operator/ (const modint &p) const noexcept {
return (modint(*this) /= p);
}
constexpr bool operator== (const modint &p) const noexcept {
return (x == p.x);
}
constexpr bool operator!= (const modint &p) const noexcept {
return (x != p.x);
}
constexpr modint pow (long long exp) const noexcept {
modint value(1), buff(x);
while (exp) {
if (exp & 1) value *= buff;
buff *= buff;
exp >>= 1;
}
return value;
}
constexpr modint inverse () const noexcept {
return pow(mod - 2);
}
friend std::ostream &operator<< (std::ostream &os, const modint &p) {
return (os << p.x);
}
friend std::istream &operator>> (std::istream &is, modint &p) {
long long v;
is >> v;
p = modint(v);
return (is);
}
};
constexpr std::uint_fast32_t MOD = 1'000'000'007;
void solve () {
int n, a, b;
std::cin >> n >> a >> b;
if (n < a + b) {
std::cout << 0 << '\n';
return;
}
const int c = n - a - b + 1;
modint<MOD> cc = c;
modint<MOD> ans = (cc) * (cc + 1) * 2;
ans *= modint<MOD>(n - a + 1) * modint<MOD>(n - b + 1);
modint<MOD> s = cc * (cc + 1) / 2;
s *= s;
s *= 4;
ans -= s;
std::cout << ans << '\n';
return;
}
int main() {
int t;
std::cin >> t;
while (t--) solve();
return 0;
} |
#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(v) v.begin(),v.end()
#define PI acos(-1)
typedef long long ll;
ll MOD=1000000007;
ll gcd(ll x,ll y){
if(y==0) return x;
else return gcd(y,x%y);
}
ll lcm(ll x,ll y){
return x/gcd(x,y)*y;
}
int main(){
int n;
cin>>n;
string s=to_string(n);
bool f=false;
for(int i=0;i<=10;i++){
string r;
rep(j,i){
r+='0';
}
r+=s;
string c;
rep(j,r.size()){
c+=r[r.size()-j-1];
}
bool ff=true;
rep(j,c.size()){
if(r[j]!=c[j]){
ff=false;
break;
}
}
if(ff){
f=true;
break;
}
}
f? cout<<"Yes":cout<<"No";
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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r)
{
uniform_int_distribution<int> uid(l, r);
return uid(rng);
}
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)
const int MOD = 1e9+7;
int mod_pow(int a,int b,int M = MOD)
{
if(a == 0) return 0;
b %= (M - 1); //M must be prime here
int res = 1;
while(b > 0)
{
if(b&1) res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
void solve()
{
string s;
cin >> s;
int res = 0;
int n = s.length();
vi fre(26,0);
for(int i=n-1;i>=0;i--)
{
fre[s[i] - 'a']++;
if(i < n-1 && s[i] == s[i+1])
{
f(j,26)
if(s[i] - 'a' != j)
{
res += fre[j];
fre[s[i] - 'a'] += fre[j];
fre[j] = 0;
}
}
}
cout << res;
}
signed main()
{
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
}
|
#include <stdio.h>
#include <map>
int x[100010],y[100010];
int type[100010];
std::map<int,int> check;
int main()
{
int a,b;
scanf("%d%d",&a,&b);
for(int i=1;i<=a;i++) scanf("%d",&x[i]);
for(int i=1;i<=a;i++) scanf("%d",&y[i]);
x[0] = 0, y[0] = 0;
x[a+1] = b+1, y[a+1] = b+1;
for(int i=1;i<=a;i++)
{
if(x[i]>y[i]) type[i] = 1; // <<
if(x[i]==y[i]) type[i] = 2; // --
if(x[i]<y[i]) type[i] = 3; // >>
}
for(int i=1;i<a;i++)
{
if(type[i]==3&&type[i+1]==1)
{
printf("-1");
return 0;
}
}
// x[j] +(i-j) == y[i]
long long int ans = 0;
check[0] = 0;
for(int i=1;i<=a;i++)
{
if(type[i]==1)
{
if(check.find(y[i]-i)==check.end())
{
printf("-1");
return 0;
}
if(y[i-1]==y[i]-1) ans++;
else
{
int k = check[y[i]-i];
ans += (i-k);
}
check[x[i]-i] = i;
}
else if(type[i]==2)
{
check.clear();
check[x[i]-i] = i;
}
}
check.clear();
//x[j] - (j-i) == y[i]
check[(b+1)-(a+1)] = a+1;
for(int i=a;i>=1;i--)
{
if(type[i]==3)
{
if(check.find(y[i]-i)==check.end())
{
printf("-1");
return 0;
}
if(y[i+1]==y[i]+1) ans++;
else
{
int k = check[y[i]-i];
ans += (k-i);
}
check[x[i]-i] = i;
}
else if(type[i]==2)
{
check.clear();
check[x[i]-i] = i;
}
}
printf("%lld",ans);
} | #include<bits/stdc++.h>
using namespace std;
using lli = long long int;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h = 0, w = 0;
cin >> h >> w;
vector<vector<char> > s(h, vector<char>(w));
for (auto &i : s)
for (auto &j : i)
cin >> j;
int res = 0;
auto isIn = [&](int x, int y)
{
return (x >= 0 && y >= 0 && x < h && y < w);
};
for (int i = 0; i < h; ++i)
for (int j = 0; j < w; ++j)
{
int ct = 0;
if (isIn(i, j) && s[i][j] == '#')
++ct;
if (isIn(i + 1, j) && s[i + 1][j] == '#')
++ct;
if (isIn(i, j + 1) && s[i][j + 1] == '#')
++ct;
if (isIn(i + 1, j + 1) && s[i + 1][j + 1] == '#')
++ct;
if (ct == 1 || ct == 3)
++res;
}
cout << res;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define reg register
#define x1 x11
#define x2 x22
#define y1 y11
#define y2 y22
#define z1 z11
#define z2 z22
const int mod=1e9+7;
const int INF=2e16;
const int maxn=5e5+5;
const double Pi=acos(-1.0);
double Exp=1e-6;
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*10+ch-'0';
ch=getchar();
}
return x*f;
}
int n,m,s,t;
struct node
{
int to;
int nxt;
int dis;
int v;
}edge[maxn];
int wugui=0;
int head1[maxn];
void add(int x,int y,int d,int v)
{
wugui++;
edge[wugui].to=y;
edge[wugui].nxt=head1[x];
edge[wugui].dis=d;
edge[wugui].v=v;
head1[x]=wugui;
}
struct node1
{
int x,y,t,k;
}a[maxn];
struct vode
{
int id;
int dist;
}love;
priority_queue<vode> q;
bool operator<(vode a,vode b)
{
return a.dist>b.dist;
}
int vis[maxn];
int dist[maxn];
void dij()
{
memset(dist,0x3f,sizeof(dist));
//cout<<dist[2]<<endl;
dist[s]=0;
vode p;
p.dist=0,p.id=s;
q.push(p);
while(!q.empty())
{
vode point=q.top();
q.pop();
if(vis[point.id])
continue;
vis[point.id]=1;
for(int i=head1[point.id];i;i=edge[i].nxt)
{
int t=edge[i].to;
int wns=dist[point.id]+edge[i].dis;
if(point.dist%edge[i].v)
{
int tot=point.dist/edge[i].v;
wns=wns+(tot+1)*edge[i].v-point.dist;
}
if(dist[t]>wns)
{
dist[t]=wns;
vode p;
p.dist=dist[t];
p.id=t;
q.push(p);
}
}
}
}
signed main()
{
n=read(),m=read(),s=read(),t=read();
for(int i=1;i<=m;i++)
{
a[i].x=read(),a[i].y=read(),a[i].t=read(),a[i].k=read();
add(a[i].x,a[i].y,a[i].t,a[i].k);
add(a[i].y,a[i].x,a[i].t,a[i].k);
}
dij();
if(dist[t]>=INF)
{
printf("-1\n");
}
else
printf("%lld\n",dist[t]);
return 0;
}
/*
40
1 4
2 3
5 8
6 7
*/
| #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <cmath>
#include <climits>
#include <iomanip>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long ll;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<60)-1;
#define rep(i, n) for (int i = 0; i < n; i++)
template<class T>
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}
template<class T>
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}
typedef pair<ll,ll> P;
struct edge {
int to;
ll cost;
ll k;
edge(int to, ll cost, ll k): to(to), cost(cost), k(k) {};
};
void dijkstra(vector<vector<edge> >&G, int s, vector<ll> &dist) {
dist.resize(G.size());
dist.assign(dist.size(), LINF);
dist[s] = 0;
priority_queue<P, vector<P>, greater<P> > que;
que.push(P(0, s));
while (que.size()) {
P p = que.top(); que.pop();
int u = p.second; ll d = p.first;
if (dist[u] < d) continue;
for (auto e: G[u]) {
int v = e.to; ll c = e.cost, k = e.k;;
if (chmin(dist[v], (d+k-1)/k*k+c))
que.push(P(dist[v], v));
}
}
}
int main() {
int n, m, x, y; cin >> n >> m >> x >> y;
x--; y--;
vector<vector<edge> > G(n);
rep(i, m) {
int a, b; ll t, k; cin >> a >> b >> t >> k;
a--; b--;
G[a].push_back(edge(b, t, k));
G[b].push_back(edge(a, t, k));
}
vector<ll> dist;
dijkstra(G, x, dist);
if (dist[y] < LINF)
cout << dist[y] << endl;
else
cout << -1 << endl;
return 0;
}
//小数点精度
//cout << fixed << std::setprecision(15) << y << endl; |
Subsets and Splits