code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
constexpr int MAX = 30;
ll dp[MAX + 1][MAX + 1];
string find(int A, int B, ll K) {
if (A == 0) return string(B, 'b');
if (B == 0) return string(A, 'a');
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() {
int A, B;
ll K;
cin >> A >> B >> K;
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;
}
| /**
* author: Dooloper
* created: 22.05.2021 20:55:00
**/
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repp(i,k, n) for(int i = (int)(k); i < (int)(n); i++)
#define ALL(a) (a).begin(),(a).end()
using ll = long long;
using P = pair<int, int>;
struct Edge{int to; ll w; Edge(int to, ll w) : to(to), w(w) {}};
using Graph = vector<vector<Edge>>;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};
ll comb(int a, int b){
ll ans = 1;
ll key = a + b;
rep(i,a){
ans *= key;
ans /= (i+1);
key--;
}
return ans;
}
int main(){
ll a,b,k; cin >> a >> b >> k;
string ans;
int n = a+b;
rep(i,n){
if(comb(a-1,b) < k){
ans.push_back('b');
k -= comb(a-1,b);
b--;
if(b == 0){
rep(j,a) ans.push_back('a');
break;
}
}
else{
ans.push_back('a');
//k -= comb(a-1,b);
a--;
if(a == 0){
rep(j,b) ans.push_back('b');
break;
}
}
}
cout << ans << endl;
//cout << comb(30,30) << endl;
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>
#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
#include <iomanip>
#include <cmath>
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin, n;
ll x1 = 0, x2 = 0, xinf = 0;
REP(i, 0, n) {
ll a;
cin, a;
x1 += abs(a);
x2 += a * a;
chmax(xinf, abs(a));
}
print(x1);
cout << fixed << setprecision(15) << sqrt(x2) << endl;
print(xinf);
return 0;
}
| #include<iostream>
using namespace std;
int main(){
int n,index;
cin>>n;
for(int i=1;;i++){
if((i*(i+1))/2 >= n)
{index=i;
break;
}
}
cout<<index<<endl;
} |
#include<iostream>
using namespace std;
typedef long long ll;
int main(){
ll n;cin >> n;
ll ans = 0;
if (n>=1000) ans+=n-999;
if (n>=1000000) ans+=n-999999;
if (n>=1000000000) ans+=n-999999999;
if (n>=1000000000000) ans+=n-999999999999;
if (n>=1000000000000000) ans+=n-999999999999999;
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define pb push_back
#define vec vector<ll>
#define pii pair<ll,ll>
#define pr_que priority_queue<pii,vector<pii>,greater<pii> >
#define itr(i,x,y) for(ll i=x;i<y;i++)
#define mod 1000000007
const long long N=200001,INF=1000000000000000000;
const double err=1e-9;
int main()
{
IOS;
ll n,ans=0,k=1;
cin >> n;
itr(i,1,6)
{
k*=1000;
if(n>=k) ans+=n-k+1;
}
cout <<ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<ll,ll>;
ll solve_sub(ll N, vector<ll>& V) {
priority_queue<pii> q;
ll ans = 0;
for ( int i = 0; i < N; i++ ) {
int i2 = 2*N-1-i;
q.push(pii(V[i],i));
q.push(pii(V[i2],i2));
ans += q.top().first;
q.pop();
}
return ans;
}
ll solve_sub_0(ll N, vector<ll>& V) {
ll ans = 0;
for ( int i = 0; i < N; i++ ) {
int i1 = N-1-i, i2 = N+i;
ans += max(V[i1],V[i2]);
}
return ans;
}
ll solve() {
ll N;
cin >> N;
vector<ll> V(2*N);
for ( int i = 0; i < 2*N; i++ ) cin >> V[i];
ll ans = solve_sub(N, V);
return ans;
}
auto solve_small_1(ll N, vector<ll>& V) {
ll ans = 0;
vector<ll> ans_v;
vector<ll> used(2*N);
auto dfs = [&](auto self, int d, ll s) -> void {
if ( d == N ) {
ans = max(ans, s);
return;
}
int h0, h1, c = 0;
for ( int i = 0; i < 2*N; i++ ) {
if ( used[i] == 0 ) {
c++;
if ( c <= N-d ) {
h0 = i;
} else {
h1 = i;
break;
}
}
}
for ( int i = 0; i < 2*N; i++ ) {
if ( used[i] > 0 ) continue;
int i1 = i;
int i2 = i < h1 ? h1 : h0;
used[i1] = 1;
used[i2] = 1;
ans_v.push_back(i1);
self(self, d+1, s+V[i1]);
ans_v.pop_back();
used[i1] = 0;
used[i2] = 0;
}
};
dfs(dfs,0,0LL);
return ans;
}
struct Data {
int id;
ll N;
vector<ll> V;
};
void test() {
mt19937_64 mt64(0);
uniform_int_distribution<ll> rnd_n(3, 4);
uniform_int_distribution<ll> rnd_v(0, 9);
int id = 0;
vector<Data> ts;
ts.push_back({++id,4,{3,2,8,6,7,6,1,6}});
ts.push_back({++id,3,{1,2,3,4,5,6}});
ts.push_back({++id,4,{1,4,5,8,7,6,3,2}});
for ( int i = 0; i < 100; i++ ) {
int n = rnd_n(mt64);
vector<ll> a;
for ( int j = 0; j < 2*n; j++ ) {
a.push_back(rnd_v(mt64));
}
ts.push_back({++id,n,a});
}
for ( Data& t : ts ) {
auto ans0 = solve_sub(t.N,t.V);
auto ans1 = solve_small_1(t.N,t.V);
if ( ans0 != ans1 ) {
cout << t.id << " " << ans0 << " " << ans1 << "\n";
}
}
cout << "end" << "\n";
}
int main() {
// test();
auto ans = solve();
cout << ans << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr lint B1 = 1532834020;
constexpr lint M1 = 2147482409;
constexpr lint B2 = 1388622299;
constexpr lint M2 = 2147478017;
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }
template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<lint> V1(N), V2(N);
IREP(i, N) cin >> V1[i];
REP(i, N) cin >> V2[i];
multiset<lint> st;
REP(i, N) {
if(st.size() > 0 && *st.begin() < min(V1[i], V2[i])) {
st.erase(st.begin());
st.insert(V1[i]);
st.insert(V2[i]);
} else {
st.insert(max(V1[i], V2[i]));
}
}
lint ans = 0;
for(lint x : st) ans += x;
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
using ll = long long;
const int INF = 1001001001;
const ll LINF = 1002003004005006007ll;
void solve(){
int si,sj;
cin >> si >> sj;
vector<vector<int> > t(50,vector<int>(50)),p(50,vector<int>(50));
rep(i,50)rep(j,50)cin >> t[i][j];
rep(i,50)rep(j,50)cin >> p[i][j];
set<int> sumi;
sumi.insert(t[si][sj]);
while(1){
if(sj+1<50&&!sumi.count(t[si][sj+1])){
cout << 'R';
sj++;
sumi.insert(t[si][sj]);
continue;
}
else if(si+1<50&&!sumi.count(t[si+1][sj])){
cout << 'D';
si++;
sumi.insert(t[si][sj]);
continue;
}
else if(si-1>=0&&!sumi.count(t[si-1][sj])){
cout << 'U';
si--;
sumi.insert(t[si][sj]);
continue;
}
else if(sj-1>=0&&!sumi.count(t[si][sj-1])){
cout << 'L';
sj--;
sumi.insert(t[si][sj]);
continue;
}
break;
}
cout << endl;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(16);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int si, sj; cin >> si >> sj;
int t[50][50];
int p[50][50];
for(int i = 0; i < 50; i++) {
for(int j = 0; j < 50; j++) {
cin >> t[i][j];
}
}
for(int i = 0; i < 50; i++) {
for(int j = 0; j < 50; j++) {
cin >> p[i][j];
}
}
int vis[50][50]; memset(vis, 0, sizeof vis);
int dx[4] = {-1,0,1,0};
int dy[4] = {0, -1, 0, 1};
string dir = "ULDR";
int curx = si;
int cury = sj;
vis[si][sj] = 1;
string ans = "";
set<int> s;
s.insert(t[si][sj]);
while(true) {
vector<pair<int, int> > vec;
for(int i = 0; i < 4; i++) {
int x = curx + dx[i];
int y = cury + dy[i];
if(x < 0 or y < 0 or x >= 50 or y >= 50 ) {
continue;
}
if(t[x][y] == t[curx][cury]) continue;
if(s.count(t[x][y]) > 0) continue;
vec.push_back(make_pair(p[x][y], i));
}
if(vec.size() == 0) break;
std::random_shuffle(vec.begin(), vec.end());
int i = vec[0].second;
curx += dx[i];
cury += dy[i];
ans += dir[i];
vis[curx][cury] = 1;
s.insert(t[curx][cury]);
}
cout << ans << endl;
return 0;
}
|
#define rep(i,n) for (int i=0;i<n;i++)
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
unsigned long long n;
int a,b;
cin >> n;
bool torf=false;
for(unsigned long long i=0;i<37;i++){
for(unsigned long long j=0;j<25;j++){
//for(k,i)
unsigned long long c=1;//=pow(5,j+1);
unsigned long long d=1;//pow(3,i+1);
//cout << pow(5,j+1) << endl;
rep(k,i+1)
c*=3;
rep(l,j+1)
d*=5;
//cout << d << endl;
unsigned long long temp=c+d;
if(temp==n){
torf=true;
a=i+1;
b=j+1;
}
}
}
if(torf==false)
cout << -1 << endl;
else
cout << a << ' ' << b << endl;
} | #include <bits/stdc++.h>
using namespace std;
int64_t I=2e18;
int main() {
int64_t N;
cin>>N;
vector<int64_t> p={1};
vector<int64_t> q={1};
int A=0,B=0;
while(I/3>=p[A]){
p.push_back(p[A]*3);
A++;
}
while(I/5>=q[B]){
q.push_back(q[B]*5);
B++;
}
for(int i=1;i<A;i++){
for(int j=1;j<B;j++){
if(p[i]+q[j]==N){
cout<<i<<" "<<j<<endl;
return 0;
}
}
}
cout<<"-1"<<endl;
} |
// Author: Pawan Raj
// Problem: B - Round Down
// Contest: AtCoder - AtCoder Beginner Contest 196
// URL: https://atcoder.jp/contests/abc196/tasks/abc196_b
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define int long long
#define F(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end()
#define show(A) for (auto i: A) cout << i << " "; cout << '\n';
#define show_(A, n) F(i, n) { show(A[i]) }
using namespace std;
using ld = long double;
using vi = vector < int > ;
using mi = map < int, int > ;
using pi = pair < int, int > ;
const int N = 100005;
const int MOD = 1e9 + 7;
const int inf = 1e18 + 1;
/***************************code begins here*****************************/
void solve() {
string s;
cin >> s;
if(count (all(s),'.') == 0){
cout << s << '\n';
}
else{
string t;
bool ok = 1;
for(char x : s ){
if(x=='.'){
ok = 0;
break;
}
if(ok){
t.pb(x);
}
}
cout << t << '\n';
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
string x; cin >> x;
string ans;
for(char c : x) {
if(c == '.') break;
ans += c;
}
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N; cin >> N;
long long C_add = 0;
long long C_min = 1e18;
long long C_max = -1e18;
for (int i = 0; i < N; i++) {
long long a, t; cin >> a >> t;
if (t == 1) {
C_add += a;
C_min += a;
C_max += a;
} else if (t == 2) {
C_max = max(C_max, a);
C_min = max(C_min, a);
} else {
C_min = min(C_min, a);
C_max = min(C_max, a);
}
}
int Q; cin >> Q;
while (Q--) {
long long x; cin >> x;
cout << min(max(x + C_add, C_max), C_min) << '\n';
}
return 0;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ll long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(time(0));
long long powm(long long a, long long b,long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a %mod;
a = a * a %mod;
b >>= 1;
}
return res;
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int seg[6000055];
void upd(int ver,int tl,int tr,int pos,int val)
{
if(tl>tr)
return;
else if(tl==tr)
seg[ver]+=val;
else
{
int tm=(tl+tr)/2;
if(tm>=pos)
upd(2*ver,tl,tm,pos,val);
else
upd(2*ver+1,tm+1,tr,pos,val);
seg[ver]=min(seg[2*ver],seg[2*ver+1]);
}
}
int get(int ver,int tl,int tr)
{
if(tl>tr)
return 0;
else if(tl==tr)
return tl;
else
{
int tm=(tl+tr)/2;
if(seg[2*ver]>0)
return get(2*ver+1,tm+1,tr);
else
return get(2*ver,tl,tm);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if(fopen("INPUT.txt","r"))
{
freopen("INPUT.txt","r",stdin);
freopen("OUTPUT.txt","w",stdout);
}
#endif
int n;
cin>>n;
int a[n],b[n];
for(int i=0;i<n;i++)
{
cin>>a[i]>>b[i];
}
int mn=1e17;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i==j)
{
mn=min(mn,a[i]+b[j]);
}
else
mn=min(mn,max(a[i],b[j]));
}
}
cout<<mn;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(void){
int h,w;
cin >> h >> w;
char s[h][w];
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin.get(s[i][j]);
if(s[i][j]=='\n'){
cin.get(s[i][j]);
}
}
}
int count=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
int c;
c=((s[i][j]=='#')+(s[i+1][j]=='#')+(s[i][j+1]=='#')+(s[i+1][j+1]=='#'));
if(c==1 || c==3){
count++;
}
}
}
cout << count << endl;
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <ios>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <utility>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
using i32 = int32_t;
using pi64 = pair<i64, i64>;
#define vec vector
#define let const
#define DRi64(x) i64 x; cin >> x;
#define DRS(x) string x; cin >> x;
#define DRVi64(v, n) vec<i64> v(n); { for (i64 i = 0; i < n; ++i) { cin >> v[i]; }}
#define DRpi64(x) pi64 x; cin >> x.first >> x.second;
#ifdef DEBUG
#define P(x) cerr << x << "\n"
#else
#define P(x)
#endif
constexpr i64 MAXN = 3*100*1000LL+5LL;
constexpr i64 MOD = 1000000007LL;
constexpr i64 INF64 = MOD * MOD;
int
main()
{
ios_base::sync_with_stdio(false);
DRi64(R); DRi64(C);
vec<string> G(R + 2, string(C + 2, '.'));
for (i64 r = 1; r <= R; ++r)
{
DRS(S);
for (i64 c = 0; c < C; ++c)
{
G[r][c + 1] = S[c];
}
}
i64 horiz_sides = 0;
for (i64 r = 1; r <= R + 1; ++r)
{
vec<i64> changes;
for (i64 c = 1; c <= C; ++c)
{
let i64 diff = G[r - 1][c] != G[r][c];
if (changes.empty())
{
if (diff == 1)
{
changes.push_back(diff);
}
}
else if (diff != changes.back())
{
changes.push_back(diff);
}
}
horiz_sides += count(changes.begin(), changes.end(), 1);
}
i64 vert_sides = 0;
for (i64 c = 1; c <= C + 1; ++c)
{
vec<i64> changes;
for (i64 r = 1; r <= R; ++r)
{
let i64 diff = G[r][c - 1] != G[r][c];
if (changes.empty())
{
if (diff == 1)
{
changes.push_back(diff);
}
}
else if (diff != changes.back())
{
changes.push_back(diff);
}
}
vert_sides += count(changes.begin(), changes.end(), 1);
}
cout << horiz_sides + vert_sides << "\n";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
int n;
vector<pair<int,int>> shart(100);
vector<pair<int,int>> aadmi(16);
int res=0;
void fun(int pers, int m, int k, vector<int> coin)
{
if(pers==k)
{
bool bartan[101]={};
for(auto x: coin) bartan[x]=true;
int cnt=0;
for(int i=0;i<m;i++) if(bartan[shart[i].X] && bartan[shart[i].Y]) cnt++;
res=max(res,cnt);
}
else
{
coin.push_back(aadmi[pers].X);
fun(pers+1,m,k,coin);
coin.pop_back();
coin.push_back(aadmi[pers].Y);
fun(pers+1,m,k,coin);
coin.pop_back();
}
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int tc=1;
// cin>>tc;
while(tc--)
{
int m,k;
cin>>n>>m;
for(int i=0;i<m;i++) cin>>shart[i].X>>shart[i].Y;
cin>>k;
for(int i=0;i<k;i++) cin>>aadmi[i].X>>aadmi[i].Y;
vector<int> coin;
fun(0,m,k,coin);
cout<<res<<"\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long i = 0; i < (n) ; ++i)
#define orep(i,n) for(long long i = 1; i <= (n); ++i) // one rep
#define drep(i,n) for(long long i = (n)-1; i >= 0; --i) // down rep
#define srep(i,s,t) for(long long i = (s); i < (t); ++i) // set rep
#define rng(x) (x).begin(),(x).end()
// range
#define rrng(a) (a).rbegin(),(a).rend() // reverse range
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define ru(x,y) (((x)+(y)-1)/(y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vvp = vector<vp>;
using vl = vector<ll>;
using rpq = priority_queue<int,vi,less<int>>; // 大きい順
using pq = priority_queue<int,vi,greater<int>>;// 小さい順
template<typename T> void Yes(T flag) {
cout << (flag ? "Yes" : "No") << endl;
}
template<typename T> void pv(vector<T> vec) {
cout << "[";
for(auto& v : vec) { cout << v << ","; }
cout << "]" << endl;
}
template<>
void pv(vector<P> vec) {
cout << "[";
for(auto& v : vec) {
cout << "<" << v.fi << ","<< v.se << ">" << ",";
}
cout << "]" << endl;
}
template<typename T> void pv2(vector<vector<T>> vec) {
for(auto& v : vec) pv(v);
}
int dp[1005][1005];
int main() {
int n,m;cin >> n >> m;
vi a(n,0),b(m,0);
rep(i,n) cin >> a.at(i);
rep(i,m) cin >> b.at(i);
rep(i,n+1) rep(j,m+1) {
dp[i][j] = 1001001001;
}
dp[0][0] = 0;
rep(i,n+1) rep(j,m+1) {
if(i>0 and j > 0) {
if(a.at(i-1) == b.at(j-1)) {
dp[i][j] = min(dp[i][j],dp[i-1][j-1]);
} else {
dp[i][j] = min(dp[i][j],dp[i-1][j-1] + 1);
}
}
if(i>0) dp[i][j] = min(dp[i][j],dp[i-1][j] + 1);
if(j>0) dp[i][j] = min(dp[i][j],dp[i][j-1]+1);
}
cout << dp[n][m] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif
const int MOD=1000000007;
//const int MOD=998244353;
struct ModInt {
unsigned int val;
ModInt(): val(0) {}
ModInt(int v) { norm(v%MOD); }
ModInt(long long v) { norm(v%MOD); }
ModInt& norm(long long v) {
v=v<0?v%MOD+MOD:v; // negative
v=v>=MOD?v-MOD:v; // mod
val=(unsigned int)v;
return *this;
}
explicit operator bool() const { return val!=0; }
ModInt operator-() const { return ModInt(0)-*this; }
ModInt &operator+=(ModInt that) { return norm((long long)val+that.val); }
ModInt &operator-=(ModInt that) { return norm((long long)val-that.val); }
ModInt &operator*=(ModInt that) { val=(unsigned long long)val*that.val%MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this*=that.inv(); }
ModInt operator+(ModInt that) const { return ModInt(*this)+=that; }
ModInt operator-(ModInt that) const { return ModInt(*this)-=that; }
ModInt operator*(ModInt that) const { return ModInt(*this)*=that; }
ModInt operator/(ModInt that) const { return ModInt(*this)/=that; }
ModInt pow(long long n) const {
ModInt x=*this, res=1;
while(n>0) {
if(n&1) res*=x;
x*=x,n>>=1;
}
return res;
}
ModInt inv() const { return (*this).pow(MOD-2); }
bool operator==(ModInt that) const { return val==that.val; }
bool operator!=(ModInt that) const { return val!=that.val; }
friend ostream& operator<<(ostream& os, const ModInt& that) { return os<<that.val; }
};
// $ cp-batch NowhereP | diff NowhereP.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address NowhereP.cpp && ./a.out
/*
4/17/2021
0:24-
*/
LL N,P;
void solve() {
auto res=ModInt(P-1)*ModInt(P-2).pow(N-1);
cout<<res<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout<<setprecision(12)<<fixed;
cin>>N>>P;
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vl;
typedef vector<pair<int,int>> vpi;
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define pb push_back
#define mp make_pair
#define rev(i,a,b) for(int i=a;i<b;i++)
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
const int MOD = 1000000000+7;
ll mul(ll x,ll y){return (x*y)%MOD;}
ll bin_pow(ll x,ll p){if(p==0)return 1;if(p&1)return mul(x,bin_pow(x,p-1));return bin_pow(mul(x,x),p/2);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
bool prime(ll n){for(ll i=2;i*i<=n;i++)if(n%i==0)return 0; return 1;}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n;
cin>>n;
vi v;
ll ans=0;
if(n<1000)
{
cout<<0;
return 0;
}
else
{
// int d=1;
vl p(16);
p[0]=1;
for(int i=1;i<=15;i++)
{
p[i]=p[i-1]*10;
}
for(ll i=3;i<=15;i++)
{
ll x=p[i];
ll t=min(n+1,x*10);
if(t-x>0)
{
ans+=(t-x)*(i/3);
}
else
break;
}
cout<<ans;
}
// cout<<ans<<endl;
}
|
#include<bits/stdc++.h>
#define mod 998244353
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
int sig=0;
for(int i=0;i<n;i++){
cin >> a[i];
sig+=a[i];
}
if(sig%2==1){cout << "0\n";return 0;}
vector<vector<int>> dp(n+1,vector<int>(5005));
dp[0][0]=1;
for(int i=0;i<n;i++){
for(int j=i;j>=0;j--){
for(int k=0;k<=5000-a[i];k++){
dp[j+1][k+a[i]]+=dp[j][k];
dp[j+1][k+a[i]]%=mod;
}
}
}
long long res=0;
for(int i=0;i<=n;i++){
long long cv=dp[i][sig/2];
for(long long j=1;j<=i;j++){cv*=j;cv%=mod;}
for(long long j=1;j<=n-i;j++){cv*=j;cv%=mod;}
res+=cv;res%=mod;
}
cout << res << '\n';
return 0;
}
| // Problem: E. String Reversal
// Contest: Codeforces - Educational Codeforces Round 96 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1430/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n;
int a[N],b[N];
map<int,vector<int>>v;
struct Node {
int l,r;
LL cnt,lazy;
}tr[N<<2];
void pushdown(int u) {
if(!tr[u].lazy) return;
LL lazy=tr[u].lazy; tr[u].lazy=0;
tr[L].lazy+=lazy; tr[R].lazy+=lazy;
tr[L].cnt+=lazy; tr[R].cnt+=lazy;
}
void build(int u,int l,int r) {
tr[u]={l,r,1,0};
if(l==r) {
tr[u].cnt=l;
return;
}
build(L,l,Mid); build(R,Mid+1,r);
}
void change(int u,int l,int r,int c) {
if(tr[u].l>=l&&tr[u].r<=r) {
tr[u].cnt+=c;
tr[u].lazy+=c;
return;
}
pushdown(u);
if(l<=Mid) change(L,l,r,c);
if(r>Mid) change(R,l,r,c);
}
int query(int u,int l,int r) {
if(tr[u].l>=l&&tr[u].r<=r) return tr[u].cnt;
int ans=0;
pushdown(u);
if(l<=Mid) ans+=query(L,l,r);
if(r>Mid) ans+=query(R,l,r);
return ans;
}
LL solve() {
LL ans=0;
build(1,1,n);
for(int i=1;i<=n;i++) {
int pos=v[b[i]].back(); v[b[i]].pop_back();
LL now=query(1,pos,pos);
ans+=now-i;
change(1,1,pos,1);
}
return ans;
}
bool check() {
map<int,int>mp1,mp2;
for(int i=1;i<=n;i++) mp1[a[i]]++;
for(int i=1;i<=n;i++) mp2[b[i]]++;
for(int i=1;i<=n;i++) if(mp1[a[i]]!=mp2[a[i]]) return false;
return true;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int _=1;
while(_--) {
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]),a[i]+=i;
for(int i=1;i<=n;i++) scanf("%d",&b[i]),b[i]+=i;
for(int i=n;i>=1;i--) v[a[i]].pb(i);
if(!check()) {
puts("-1");
return 0;
}
printf("%lld\n",solve());
}
return 0;
}
/*
*/
|
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
#define endl "\n"
#define pb push_back
#define fo(i, start, end) for (ll i = start; i < end; i++)
#define rep(i, start, end, step) for (ll i = start; i < end; i += step)
#define print(x) cout << #x << " is " << x << endl
#define all(arg) arg.begin(), arg.end()
#define sz(arg) (int)arg.size()
using ll = long long;
using vi = vector<int>;
using vll = vector<long long>;
void solve() {
int n;
cin >> n;
vi a(n), b(n);
fo(i, 0, n) cin >> a[i] >> b[i];
int ans = INT_MAX;
int min_index = min_element(all(a)) - a.begin();
ans = min(ans, a[min_index] + b[min_index]);
b[min_index] = INT_MAX;
ans = min(ans, max(a[min_index], *min_element(all(b))));
cout << ans << endl;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
solve();
return 0;
}
| #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 int MAXN = 1e5 + 10;
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 rep(i, begin, end) \
for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#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
ll calc(const string &S) {
ll res = 0;
for (int i = 1; i <= 9; ++i) {
ll base = 1;
for (auto c : S) {
if (c - '0' == i) {
base *= 10;
}
}
res += i * base;
}
return res;
}
/// main函数
int main() {
ll A, B;
cin >> A >> B;
double ans = (double(A) - B) / A * 100;
coutd(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
#define mod(x) (x % MOD + MOD) % MOD
const int dx[4] = {+1, 0, -1, 0};
const int dy[4] = {0, -1, 0, +1};
struct SegmentTreeRSQ
{
ll size;
vector<ll> dat;
const ll INITVAL = 0LL;
SegmentTreeRSQ(ll n)
{
size = 1;
while (size < n)
size *= 2;
dat = vector<ll>(2 * size - 1, INITVAL);
}
// update k th value to a
void update(ll k, ll a)
{
k += size - 1;
dat[k] = a;
while (k > 0)
{
k = (k - 1) / 2;
dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
}
}
ll query(ll a, ll b, ll k = 0, ll l = 0, ll r = -1)
{
if (r < 0)
r = size;
if (r <= a || b <= l)
return INITVAL;
if (a <= l && r <= b)
return dat[k];
else
{
ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
return vl + vr;
}
}
};
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
ll h, w, m;
cin >> h >> w >> m;
vector<ll> minh(h), minw(w);
vector<vector<ll>> obs(h);
fill(minh.begin(), minh.end(), w);
fill(minw.begin(), minw.end(), h);
for (int i = 0; i < m; i++)
{
ll ht, wt;
cin >> ht >> wt;
ht--;
wt--;
minh[ht] = min(minh[ht], wt);
minw[wt] = min(minw[wt], ht);
obs[ht].push_back(wt);
}
SegmentTreeRSQ sg(w);
ll cnt = 0;
for (int j = 0; j < minh[0]; j++)
cnt += minw[j];
//cout << cnt << endl;
for (int j = minh[0]; j < w; j++)
sg.update(j, 1);
for (int i = 1; i < minw[0]; i++)
{
cnt += sg.query(0, minh[i]);
for (ll j : obs[i])
sg.update(j, 1);
}
cout << cnt << endl;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<LL,LL> pii;
typedef pair<LL,pii> ppi;
typedef pair<pii,pii> ppp;
#define FOR(i, n) for(int i = 1; i<=n; i++)
#define F0R(i, n) for(int i = 0; i<n; i++)
#define mp make_pair
#define pb push_back
#define f first
#define s second
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; F0R(i, v.size()) {if (i) cout << ", "; cout << v[i];} return cout << "]";}
//var
const LL INF = 1e18;
LL T, n, a[105], x, dp[105][105];
void filldp(LL g){
F0R(i, g+1) F0R(j, g+1) dp[i][j] = -INF;
dp[0][0] = 0;
FOR(i, n){
for(int j = g; j>=1; j--){
for(int k = 0; k<g; k++){
if(dp[j-1][k] < 0) continue;
dp[j][(k + a[i]) % g] = max(dp[j][(k + a[i])%g],
dp[j-1][k] + a[i]);
}
}
}
//cout << dp[g][0] << '\n';
}
void solve(){
cin >> n >> x;
FOR(i, n) cin >> a[i];
LL ans = INF;
FOR(i, n){
filldp(i);
LL mxval = dp[i][x%i];
if(mxval < 0) continue;
LL tt = (x - mxval)/i;
ans = min(ans, tt);
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
T = 1;
//cin >> T;
FOR(t, T)
solve();
cout.flush();
return 0;
} |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
template<unsigned MOD_> struct ModInt {
static constexpr unsigned MOD = MOD_;
unsigned x;
void undef() { x = (unsigned)-1; }
bool isnan() const { return x == (unsigned)-1; }
inline int geti() const { return (int)x; }
ModInt() { x = 0; }
ModInt(int y) { if (y<0 || (int)MOD<=y) y %= (int)MOD; if (y<0) y += MOD; x=y; }
ModInt(unsigned y) { if (MOD<=y) x = y % MOD; else x = y; }
ModInt(long long y) { if (y<0 || MOD<=y) y %= MOD; if (y<0) y += MOD; x=y; }
ModInt(unsigned long long y) { if (MOD<=y) x = y % MOD; else x = y; }
ModInt &operator+=(const ModInt y) { if ((x += y.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(const ModInt y) { if ((x -= y.x) & (1u<<31)) x += MOD; return *this; }
ModInt &operator*=(const ModInt y) { x = (unsigned long long)x * y.x % MOD; return *this; }
ModInt &operator/=(const ModInt y) { x = (unsigned long long)x * y.inv().x % MOD; return *this; }
ModInt operator-() const { return (x ? MOD-x: 0); }
ModInt inv() const { return pow(MOD-2); }
ModInt pow(long long y) const {
ModInt b = *this, r = 1;
if (y < 0) { b = b.inv(); y = -y; }
for (; y; y>>=1) {
if (y&1) r *= b;
b *= b;
}
return r;
}
friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};
constexpr LL MOD = 1000000007;
using Mint = ModInt<MOD>;
int N;
int X[200011];
int Y[200011];
LL W[200011];
VI G[200011];
int par[200011];
LL dp[200011];
void MAIN() {
scanf("%d", &N);
REP (i, N-1) {
int x, y;
LL w;
scanf("%d%d%lld", &x, &y, &w);
x--;
y--;
G[x].push_back(i);
G[y].push_back(i);
X[i] = x;
Y[i] = y;
W[i] = w;
}
VI que;
que.push_back(0);
REP (i, N) {
int v = que[i];
EACH (e, G[v]) {
int w = v ^ X[*e] ^ Y[*e];
if (w == par[v]) continue;
dp[w] = dp[v] ^ W[*e];
par[w] = v;
que.push_back(w);
}
}
Mint ans = 0;
for (int t=60; t--;) {
LL a = 0;
REP (i, N) if (dp[i]>>t&1) a++;
LL b = N - a;
ans *= 2;
ans += a * b;
}
printf("%d\n", ans.geti());
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int N;
long long mod = 1000000007;
vector<pair<int, long long>> E[200010];
long long A[200010];
void dfs(int v, int p){
int res = 0;
for(int i = 0; i < (int) E[v].size(); i++){
int c = E[v][i].first;
if(c == p) continue;
long long w = E[v][i].second;
A[c] = (A[v] ^ w);
dfs(c, v);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N;
for(int i = 0; i < N-1; i++){
int u, v; long long w;
cin >> u >> v >> w;
u--; v--;
E[u].push_back(make_pair(v, w));
E[v].push_back(make_pair(u, w));
}
long long mul = 1LL;
long long ans = 0;
dfs(0, -1);
for(int d = 0; d < 60; d++){
long long a = 0;
for(int i = 0; i < N; i++){
if((A[i] >> d) & 1) a ++;
}
long long add = a * (N - a) % mod;
ans += mul * add % mod;
mul *= 2; mul %= mod;
ans %= mod;
}
cout << ans << "\n";
} |
#include <iostream>
#include <algorithm>
#include <limits>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <cmath>
using namespace std;
int n;
int a[200010];
long long preSum;
long long sum;
int main() {
scanf("%d", &n);
// vector<int> a(n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
// sort(a.begin(), a.end());
sort(a, a + n);
preSum = a[0];
for (int i = 1; i < n; ++i) {
sum += ((long long)a[i] * i) - preSum;
preSum += a[i];
}
printf("%lld\n", sum);
return 0;
} | // {{{ by unolight
#pragma region
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }
#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif
template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
sort(begin(v),end(v),f);
v.resize(unique(begin(v),end(v))-begin(v));
}
#pragma endregion
// }}}
void main() {
int n;
R(n);
vector<LL> a(n);
REP(i,n) R(a[i]);
LL ans=0;
sort(ALL(a));
vector<LL> pref(n+1);
REP(i,n) pref[i+1]=pref[i]+a[i];
REP(i,n) ans+=pref[n]-pref[i]-a[i]*(n-i);
W(ans);
}
// {{{ main
}}
int main() { unolight::main(); return 0; }
// }}}
|
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30) - 1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = 3.14159265358979;
const double eps = 1e-8;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
if(a>b){
a = b; return true;
}
else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
if(a<=b){
a = b; return true;
}
else return false;
}
template<typename T> inline void print(T &a){
int sz = a.size();
for(auto itr = a.begin(); itr != a.end(); itr++){
cout << *itr;
sz--;
if(sz) cout << " ";
}
cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;
int main(){
int n; cin >> n;
vl t(n),l(n),r(n);
rep(i,n){
cin >> t[i] >> l[i] >> r[i];
}
int ans = 0;
rep(i,n) rep(j,i){
if(l[i] > l[j]){
if(t[i] <= 2 && t[j] % 2){
if(l[i] <= r[j]) ans++;
}else{
if(l[i] < r[j]) ans++;
}
}else{
if(t[j] <= 2 && t[i] % 2){
if(l[j] <= r[i]) ans++;
}else{
if(l[j] < r[i]) ans++;
}
}
}
cout << ans << endl;
} | #pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
using pos = pair<int, int>;
bool canMoveWith1Move(const pos& from, const pos& to){
return (
(from.first + from.second == to.first + to.second) ||
(from.first - from.second == to.first - to.second) ||
(abs(from.first - to.first) + abs(from.second - to.second) <= 3)
);
}
bool canMoveWith2Moves(const pos& from, const pos& to){
auto color = [](const pos& p) -> int {
return ((p.first + p.second) % 2 + 2) % 2;
};
bool flg = false;
for (int dx = -3; dx <= 3; ++dx){
for (int dy = -3; dy <= 3; ++dy){
pos tmp = {from.first + dx, from.second + dy};
flg |= (canMoveWith1Move(from, tmp) && canMoveWith1Move(tmp, to));
}
}
return (
(color(from) == color(to)) ||
(abs(from.first + from.second - to.first - to.second) <= 4) ||
(abs(from.first - from.second - to.first + to.second) <= 4) ||
(flg)
);
}
int solve(const pos& from, const pos& to){
if (from == to) return 0;
if (canMoveWith1Move(from, to)) return 1;
if (canMoveWith2Moves(from, to)) return 2;
return 3;
}
int main(void){
int x, y;
cin >> x >> y;
pos from = {x, y};
cin >> x >> y;
pos to = {x, y};
cout << solve(from, to) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define pb push_back
struct ob {
unsigned long long t,m ;
} ;
bool comp(ob a , ob b)
{
return a.t<b.t ;
}
int main()
{
unsigned long long n,c,x,y,z ;
cin>>n>>c ;
vector<ob> a ;
rep(i,0,n)
{
cin>>x>>y>>z ;
ob t,t1 ;
t.t=x ; t.m=z ;
a.pb(t) ;
t1.t=y+1 ; t1.m=-z ;
a.pb(t1) ;
}
sort(a.begin(),a.end(),comp) ;
unsigned long long t=1 , val=0 , ans=0 , last=0,tmp;
rep(i,0,a.size())
{
if(a[i].t!=t)
{
ans += min(c,val)*(a[i].t-t) ;
t=a[i].t ;
}
val += a[i].m ;
}
cout<<ans<<endl ;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(v) (v).size()
#define all(v) (v).begin(),(v).end()
#define REP(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Time (double)clock()/CLOCKS_PER_SEC
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pi;
template <class T> void chmax(T &x,T y) {x=x>y?x:y;return;}
template <class T> void chmin(T &x,T y) {x=x<y?x:y;return;}
template <class T> T sqr(T x) {return x*x;}
template <class T> T gcd(T x,T y) {return y==0?x:gcd(y,x%y);}
template <class T> T lcm(T x,T y) {return x/gcd(x,y)*y;}
const int inf=1000000000,mod=1000000007;
const ll infll=1000000000000000000ll,modll=ll(mod);
const db maxdb=db(infll),eps=1e-14,PI=acos(-1.0);
//#define mydef
//#define LOCAL
#ifdef mydef
#define foreach(v,it) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#endif
#define debug
#define putnum(x) cout<<#x<<": "<<x<<endl
#define putvec(v) cout<<#v<<": ";REP(i,0,sz(v)) cout<<v[i]<<" ";cout<<endl
template <class T> inline void read(T &x){
T w=1;x=0;char c=getchar();
while(c<'0'||c>'9') {if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+(c&15);c=getchar();}
x*=w;
}
template <class T> inline void write(T x) {
if(x<0) putchar('-'),x=~x+1;
if(x) write(x/10),putchar(x%10+'0');
}
map<int,ll> p;
int main() {
#ifdef LOCAL
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
int n;
ll C,ans=0ll;
cin>>n>>C;
for(int i=1;i<=n;i++) {
int x,y,z;
cin>>x>>y>>z;
p[x-1]+=ll(z);
p[y]-=ll(z);
}
ll cst=0ll,preday=0ll;
for(map<int,ll>::iterator it=p.begin();it!=p.end();it++) {
ll fee;
if(cst>C) fee=C;
else fee=cst;
ans+=((*it).fi-preday)*fee;
cst+=(*it).se;
preday=(*it).fi;
}
cout<<ans<<endl;
return 0;
}
|
// Problem: E - White Pawn
// Contest: AtCoder - AtCoder Beginner Contest 203(Sponsored by Panasonic)
// URL: https://atcoder.jp/contests/abc203/tasks/abc203_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Author: abhidot
#include <bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define w(x) int x; cin>>x; while(x--)
using namespace std;
const long long N=200005, INF=2e9+5; //2000000000000000000;
#define debug(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cout << vars << " = ";
string delim = "";
(..., (cout << delim << values, delim = ", "));
cout<<"\n";
}
int power(int a, int b, int p){
if(a==0)
return 0;
int res=1;
a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
void print(bool n){
if(n){
cout<<"YES\n";
}else{
cout<<"NO\n";
}
}
int32_t main()
{
IOS;
int n,m;
cin>>n>>m;
vector<pii> p;
vector<int> a, b;
set<int>s;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
p.pb({x,y});
}
s.insert(n);
sort(all(p));
int l=0;
while(l<m){
int r=l;
a.clear();
b.clear();
while(r<m&&p[l].ff==p[r].ff) r++;
for(int i=l;i<r;i++){
int y = p[i].ss;
if((s.find(y-1)!=s.end()||s.find(y+1)!=s.end())&&s.find(y)==s.end()) a.pb(y);
if((s.find(y-1)==s.end()&&s.find(y+1)==s.end())&&s.find(y)!=s.end()) b.pb(y);
}
for(auto i:a) s.insert(i);
for(auto i:b) s.erase(i);
l=r;
}
cout<<s.size();
} | #include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,s,e) for(int i=s ; i < e ; i++)
#define rrep(i,s,e) for(int i=s ; i > e ; i--)
#define srep(i,s,e,j) for(int i=s ; i < e ; i+=j)
#define tr(i,x) for(auto i : x)
#define vinp(a) for(int i=0 ; i<a.size() ; i++)cin>>a[i]
#define ainp(a,n) for(int i=0; i<n; i++)cin>>a[i]
#define int long long
#define vi vector<int>
#define vs vector<string>
#define vb vector<bool>
#define vpi vector<pii>
#define maxpqi priority_queue<int>
#define minpqi priority_queue <int, vector<int>, greater<int> >
#define pii pair<int,int>
#define F first
#define S second
#define mk make_pair
#define pb push_back
#define pf push_front
#define endl '\n'
#define br printf("\n")
#define inf 1000000
#define imap map<int,int>
#define uimap unordered_map<int,int>
#define gcd(a,b) __gcd(a,b)
#define mod 1000000007
#define clr(x) memset(x,0,sizeof(x))
#define fill(x,y) memset(x,y,sizeof(x))
#define lb lower_bound
#define ub upper_bound
#define npos string::npos
#define all(x) x.begin(),x.end()
#define descen greater<int>()
// vector<vector<int>> v( n , vector<int> (m, 0));
//bitset<n>(x).to_string() -- return string // here n is bit length.
// x << y ==== x*(2^y)
// x >> y ==== x/(2^y)
// x&1 --- 1 then x is odd
/*
lower_bound: iterator pointing to the first element which has a value not less than ‘val’.
upper_bound: iterator pointing to the first element which has a value greater than ‘val’.
*/
// Driver function to sort the vector elements
// by second element of pairs
// -- IN ASCENDING ORDER --
bool comp(const pair<int,int> &a, const pair<int,int> &b){
return (a.second < b.second);
}
string yes="YES",no="NO";
void solve(){
int tt = 1;
// cin >> tt;
while(tt--){
int n,m;cin>>n>>m;
map<int,vi>mat;
set<int>avai;
int x,y;
rep(i,0,m){
cin>>x>>y;
mat[x].pb(y);
}
avai.insert(n);
tr(i,mat){
set<int>add;
tr(j,mat[i.F]){
if(avai.find(j-1) != avai.end() || avai.find(j+1) != avai.end()){
add.insert(j);
}
}
tr(j,mat[i.F]){
if(avai.find(j) != avai.end()){
avai.erase(j);
}
}
tr(j,add)avai.insert(j);
}
cout << avai.size() << endl;
}
}
int32_t main()
{
fastio
// ios::sync_with_stdio(false);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
solve();
return 0;
}
/*
// Sort the array in descending order
sort(arr, arr + n, greater<int>());
*/ |
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define pb push_back
#define vec vector<int>
#define fo(i, n) for (int i = 0; i < n; i++)
#define foo(i, a, b) for (int i = a; i < b; i++)
#define mp make_pair
#define ll long long
#define mod 1000000007
//#define mod 998244353
#define so(v) sort(v.begin(), v.end())
#define fi first
#define se second
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
//__builtin_popcount
//vector<vector<int>>
// dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int power(int x, unsigned int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int mI(int a, int m)
{
return power(a, m - 2, m);
}
#define double long double
int n;
int dp[1000][100];
int fun(int i,int c){
if(c>11){
return 0;
}
if(i>=n){
if(c==11)
return 1;
else
return 0;
}
if(dp[i][c]!=-1)
return dp[i][c];
return dp[i][c]=fun(i + 1, c + 1) + fun(i + 1, c);
}
void solve(){
cin >> n;
memset(dp, -1, sizeof(dp));
cout << fun(1, 0);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
srand(time(0));
int t = 1;
//cin >> t;
while (t--)
{
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string X;
cin>>X;
string a=X.substr(0,X.find('.'));
cout<<a<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
int mod = 998244353;
int add(int a, int b) { return a + b - (a + b >= mod) * mod; }
int sub(int a, int b) { return a - b + (a - b < 0) * mod; }
int mul(int a, int b) { return (1ll * a * b) % mod; }
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int a[n], pre[n] = {}, ans = 0;
for (int &x : a)
cin >> x;
sort(a, a + n);
for (int i = 0; i < n; i++) {
if (i > 1) {
pre[i - 2] = mul(pre[i - 2], 2);
pre[i - 1] = add(a[i - 1], pre[i - 2]);
}
pre[i] = add(i ? pre[i - 1] : 0, a[i]);
ans = add(ans, mul(a[i], pre[i]));
}
cout << ans << '\n';
} | #include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define all(v) v.begin(), v.end()
#define rep(i, j) for (ll i = 0; i < j; ++i)
#define rep2(i, j, k) for (ll i = j; i <= k; ++i)
#define rep3(i, j, k) for (ll i = j; i >= k; --i)
using namespace std;
using ld = long double;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<long, long>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
constexpr int INF = 1000000000;
constexpr int MAX = 500000;
constexpr int MOD = 998244353;
constexpr ll INFLL = 1000000000000000000;
template <class S, class T> inline bool chmax(S &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class S, class T> inline bool chmin(S &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct mint {
ll x;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator+=(const mint &a) {
x += a.x;
if (x >= MOD)
x -= MOD;
return *this;
}
mint operator-=(const mint &a) {
if (x < a.x)
x += MOD;
x -= a.x;
return *this;
}
mint operator*=(const mint &a) {
x = x * a.x % MOD;
return *this;
}
mint operator/=(const mint &a) { return *this *= a.inv(); }
mint operator+(const mint &a) const { return mint(*this) += a; }
mint operator-(const mint &a) const { return mint(*this) -= a; }
mint operator*(const mint &a) const { return mint(*this) *= a; }
mint operator/(const mint &a) const { return mint(*this) /= a; }
mint inv(void) const { return pow(MOD - 2); }
mint pow(ll a) const {
if (!a)
return 1;
mint t = pow(a >> 1);
t *= t;
if (a & 1)
t *= (*this);
return t;
}
friend ostream &operator<<(ostream &os, const mint &a) {
os << a.x;
return os;
}
};
int main(void) {
int n;
cin >> n;
vl a(n);
rep(i, n) cin >> a[i];
sort(all(a));
mint sum = a[n - 1], ans = 0;
rep3(i, n - 2, 0) {
/* cout << sum << endl; */
ans += sum * a[i];
sum = sum * 2 + a[i];
}
rep(i, n) ans += a[i] * a[i];
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ll n,x; cin>>n>>x;
string s; cin>>s;
for(ll i=0;i<n;i++)
{
if(s[i]=='x') x--;
else x++;
if(x<0) x=0;
}
cout<<x<<endl;
}
| #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REPR(i, n) for (int i = (n); i >= 0; i--)
#define FOR(i, m, n) for (int i = (m); i < (int)(n); i++)
#define INF 1000000000
//配列 vector<型> 配列名(要素数, 初期値);
//二次元配列 vector<vector<型>> 配列名(要素数縦,vector<int>(要素数横, 初期値))
//配列のサイズ(二次元縦) 配列名.size(), 二次元横 配列名[0].size()
// 1秒間のforループは10^8くらい
//最大公約数
int gcd(int a, int b) {
if (a % b == 0) {
return (b);
} else {
return (gcd(b, a % b));
}
}
//最小公倍数
int lcm(int a, int b) { return a * b / gcd(a, b); }
int main() {
int N, X;
string S;
cin >> N >> X >> S;
REP(i, N) {
if (S[i] == 'o') {
X++;
} else {
if (X != 0) X--;
}
}
cout << X << endl;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e3+9;
const int MAX=1e2+9;
const double ep=1e-4;
const double ep1=1e-6;
const int mod=77797;
const double inf=1e20;
const double pi=acos(-1);
#define debug1 puts("?");
#define debug(x) cout<<"##"<<(x)<<endl;
#define mk make_pair
#define PII pair<int,int>
#define PDI pair<double,int>
#define PIII pair<int,PII >
#define PIII1 pair<PII,int>
#define PIIII pair<PII,PII >
#define PIL pair<int,ll>
#define PLI pair<ll,int>
#define PLIII pair<PLI,PII >
#define PLL pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define sd(x) scanf("%d",&(x))
#define slld(x) scanf("%lld",&(x))
#define sdd(x,y) scanf("%d%d",&(x),&(y))
#define sc(s) scanf("%s",(s))
#define pd(x) printf("%d\n",(x))
#define plld(x) printf("%lld\n",(x))
#define pdk(x) printf("%d ",(x))
#define plldk(x) printf("%lld ",(x))
#define pdd(x,y) printf("%d %d\n",(x),(y))
#define PR(x) printf("Case %d: ",(x))
#define INF 0x3f3f3f3f
typedef unsigned long long ull;
/*
*/
ll dp[64][2];
int n;
char s[5];
void solve()
{
sd(n);
dp[0][0]=dp[0][1]=1;
rep(i,1,n)
{
sc(s);
if(s[0]=='A')
{
dp[i][0]=(dp[i-1][0]*2+dp[i-1][1]);
dp[i][1]=dp[i-1][1];
}
else
{
dp[i][1]=(dp[i-1][0]+dp[i-1][1]*2);
dp[i][0]=dp[i-1][0];
}
}
plld(dp[n][1]);
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int T=1;
// sd(T);
while(T--)solve();
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <random>
#include <list>
#include <array>
#include <fstream>
#include <chrono>
typedef long long ll;
typedef long double ld;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<vector<ll> > dp(n + 1, vector<ll>(2, 0));
dp[0][0] = dp[0][1] = 1;
for (int i = 0; i < n; i++)
{
string s;
cin >> s;
for (int prev = 0; prev < 2; prev++)
{
for (int now = 0; now < 2; now++)
{
if (s == "AND")
{
dp[i + 1][prev & now] += dp[i][prev];
}
else
{
dp[i + 1][prev | now] += dp[i][prev];
}
}
}
}
cout << dp[n][1] << "\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+50;
int sum[N],a[N];
ll n;
ll lowbit(int x) { return x&(-x); }
void add(int pos,int val)
{
while(pos<=n)
{
sum[pos]+=val;
pos+=lowbit(pos);
}
}
ll query(int pos)
{
ll res=0;
while(pos)
{
res+=sum[pos];
pos-=lowbit(pos);
}return res;
}
int main()
{
scanf("%lld",&n);
ll flag=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
add(a[i],1);flag+=i-query(a[i]);
}
vector<int>ans;
if(flag!=n-1)
{
puts("-1");
}
else
{
for(int i=1;i<=n;i++)
{
int cnt=(i-query(a[i]));
int j=i-1;
for(int k=0;k<cnt;k++)
{
ans.push_back(j);
j--;
}
}
for(int i=0;i<(int)ans.size();i++) printf("%d\n",ans[i]);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define asort(a,n) sort(a,a+n)
#define vsort(v) sort(v.begin(),v.end())
#define all(v) v.begin(),v.end()
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define mod 1000000007
#define LL_MAX LLONG_MAX
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
int main()
{
run;
ll n,i;
cin>>n;
ll a[n+1]={0},x=1,b[n],j,w;
vector<ll>v1[n+100];
for(i=0;i<n;i++)
{
cin>>a[i];
v1[a[i]].pb(i);
b[i]=i+1;
}
set<ll>s;
vector<ll>v;
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
{
//back==if(a[i]>b[i]);
// aage if(a[i]<b[i]);
w=v1[b[i]][0];
x=b[i]-1;
for(j=w;j>x;j--)
{
v1[a[j]][0]--;
v1[a[j-1]][0]++;
swap(a[j],a[j-1]);
v.push_back(min(j+1,j));
if(s.find(min(j+1,j))==s.end())
s.insert(min(j+1,j));
else
{
cout<<-1;
return 0;
}
if(v.size()>n-1)
{
cout<<-1;
return 0;
}
}
}
}
if(v.size()!=n-1)
{
cout<<-1;
return 0;
}
for(i=0;i<v.size();i++)
cout<<v[i]<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, m, t;
cin >> n >> m >> t;
int charge = n, time = 0;
int c;
c = 1;
while(m--){
int a, b;
cin >> a >> b;
charge -= a - time;
if(charge <= 0) {
cout << "No"<< endl;
return 0;
}
charge += b - a;
if(charge > n) charge = n;
time = b;
}
charge -= t - time;
if(charge <= 0){
cout << "No" << endl;
}
else{
cout << "Yes";
}
return 0;
} | // KNOW NOTHING!
#include <bits/stdc++.h>
#define ll long long int
#define F(i,j,k,in) for(int i=j;i<k;i+=in)
#define DF(i,j,k,in) for(int i=j;i>=k;i-=in)
#define feach(it,l) for (auto it = l.begin(); it != l.end(); ++it)
#define fall(a) a.begin(),a.end()
#define sz(x) (int)x.size()
#define szs(x) (int)x.length()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define eq equal_range
#define fs first
#define ss second
#define ins insert
#define mkp make_pair
#define endl "\n"
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef queue<int> qin;
typedef deque<ll> dqll;
typedef deque<int> dqint;
typedef priority_queue<ll> pqll;
typedef priority_queue<int> pqin;
typedef map<ll,ll> mpll;
typedef map<int,int> mpin;
typedef pair<ll,ll> pll;
typedef pair<int,int> pin;
const ll MOD=1000000007;
const long double PI=3.1415926535897932384626433832795;
ll pwr(ll b,ll p){ll res=1;while(p){if(p&1) {res*=b; p--;}else{b*=b; p>>=1;}}return res;}
int msb (int n) {return 31-__builtin_clz(n);}
ll msbl (ll n) {return 63-__builtin_clzll(n);}
int lsb (int n) {return __builtin_ctz(n);}
ll lsbl (ll n) {return __builtin_ctzll(n);}
int setbit (int n) {return __builtin_popcount(n);}
ll setbitl (ll n) {return __builtin_popcountll(n);}
int main()
{
/*
freopen ("input.txt","r","stdin");
freopen ("output.txt","w","stdout");
*/
ios_base::sync_with_stdio(false) , cin.tie(NULL) , cout.tie(NULL);
ll n,m,t; cin>>n>>m>>t;
vector <array <ll ,2>> a (m);
F (i,0,m,1) {
cin>>a[i][0]>>a[i][1];
}
ll cap =n , pr=0;
int ok = 0;
F (i,0,m,1) {
cap =max (0LL,cap-(a[i][0]-pr));
if (cap==0) ok=1;
cap=min (n,cap+(a[i][1]-a[i][0]));
pr=a[i][1];
}
cap=cap-(t-a[m-1][1]);
if (ok) cout<<"No";
else if (cap>0) cout<<"Yes";
else cout<<"No";
return 0;
}
/* What we know is a drop , but what we don't know is an ocean !*/ |
#include <bits/stdc++.h>
#define ll int
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define deb(x) cout<<#x<<"="<<x<<endl;
#define endl '\n'
#define M 1000000007
// #define int long long
#define F first
#define S second
#define INF 1e18
#define N 200005
using namespace std;
ll n, m;
string g[2005];
ll dp[2005][2005];
ll go(ll x, ll y)
{
if (x >= n || y >= m)return 0;
ll &ans = dp[x][y];
if (ans != -1e9)return ans;
if (x + 1 < n)
ans = max(ans, (g[x + 1][y] == '+' ? 1 : -1) - go(x + 1, y));
if (y + 1 < m)
ans = max(ans, (g[x][y + 1] == '+' ? 1 : -1) - go(x, y + 1));
if (ans == -1e9)ans = 0;
return ans;
}
void solve()
{
cin >> n >> m;
for (int i = 0; i < n; ++i)
{
cin >> g[i];
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
dp[i][j] = -1e9;
}
}
ll ans = go(0, 0);
if (ans > 0)cout << "Takahashi" << endl;
else if (ans < 0)cout << "Aoki" << endl;
else cout << "Draw" << endl;
}
int32_t main()
{
IOS
ll T = 1;
// cin >> T;
for (ll i = 1; i <= T; ++i)
{
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} | //include <atcoder>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
#define pf(dans) printf("%.12lf", dans)
#define pfn(dans) pf(dans); cout << endl;
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9 + 10;
const ll LINF = (ll)1e18 + 10;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)
template <typename T>
T my_abs(T x){
return (x >= 0)? x : -x;
}
template <typename T>
void chmax(T &a, T b){
a = max(a, b);
}
template <typename T>
void chmin(T &a, T b){
a = min(a, b);
}
ll gcd(ll a, ll b){
if (a > b) return gcd(b, a);
if (a == 0) return b;
return gcd(b % a, a);
}
bool incld_bit(ll bit, int i){
return ((bit>>i) & 1) == 1;
}
// --------------------------------------------------------------------------------
int main(void){
int i, j;
ll a, b;
cin >> a >> b;
vector<int> p;
rep0(i, 75){
rep1(j, 2, i){
if (i % j == 0){
break;
}
}
if (j == i){
p.push_back(i);
}
}
int snsbk[75] = {};
rep0(i, b - a + 1){
rep0(j, 20){
if ((a + i) % p[j] == 0){
snsbk[i] += (1<<j);
}
}
}
ll dp[75][(1<<20) + 3] = {};
dp[0][0] = 1;
rep0(i, b - a + 1){
rep0(j, 1<<20){
dp[i + 1][j] = dp[i][j];
if ((j & snsbk[i]) == snsbk[i]){
dp[i + 1][j] += dp[i][j - snsbk[i]];
}
}
}
ll ans;
ans = 0;
rep0(j, 1<<20){
ans += dp[b - a + 1][j];
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
using ll = long long int;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define FASTIO() cin.tie(0); ios::sync_with_stdio(false)
// chmax/min: if update a then true
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 ceil(a,b) ((a) + ((b) - 1)) / (b)
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
//////////////////////////////////////
#define dp(arg) if(KANAILOCAL) { cout << #arg << ":"; dprint(arg); }
template <typename T> void dprint(T arg) { cout << arg << "\n"; }
template <typename T> void dprint(const vector<T>& arg) { for_each(begin(arg), end(arg), [](T value){ cout << value << " "; } ); cout << "\n"; }
template <typename T> void dprint(const vector<vector<T>>& arg) { for_each(begin(arg), end(arg), [=](vector<T> arg2){ dprint(arg2); cout << "\n";} ); }
template <typename T> void dprint(const unordered_set<T>& arg) { for_each(begin(arg), end(arg), [](T value){ cout << value << " "; } ); cout << "\n"; }
template <typename T1, typename T2> void dprint(const unordered_map<T1,T2>& arg) { for_each(begin(arg), end(arg), [](auto & x){ cout << x.first << "_" << x.second << " "; } ); cout << "\n"; }
//////////////////////////////////////
#define Yes std::cout << "Yes" << std::endl;
#define No std::cout << "No" << std::endl;
#define YES std::cout << "YES" << std::endl;
#define NO std::cout << "NO" << std::endl;
//////////////////////////////////////
void inputSample(){
#if 0
string s; cin.ignore();
getline(cin, s);
int n,c;
cin >> n >> c;
int x; vector<int> dat(n); REP(i, n){cin << x; dat.at(i) = x;}
if('a' <= s[i] && s[i] <= 'z'){}
if('A' <= s[i] && s[i] <= 'Z'){}
#endif
}
template<typename T>
class SparseTable {
vector<vector<T>> table;
vector<int> lookup;
function<T(T, T)> operation;
public:
SparseTable(const vector<T> &v, function<T(T, T)> ope): operation(ope) {
int log_len = 0;
while((1 << log_len) <= (int)v.size()) ++log_len;
table = vector<vector<T>>(log_len, vector<int>(1 << log_len));
lookup = vector<int>((int)v.size() + 1);
for(int i = 0; i < (int)v.size(); i++) {
table[0][i] = v[i];
}
for(int i = 1; i < log_len; i++) {
for(int j = 0; j + (1 << i) <= (1 << log_len); j++) {
table[i][j] = operation(table[i-1][j], table[i - 1][j + (1 << (i - 1))]);
}
}
for(int i = 2; i < (int)lookup.size(); i++) {
lookup[i] = lookup[i >> 1] + 1;
}
}
inline T query(int l, int r) {
int b = lookup[r - l];
return operation(table[b][l], table[b][r - (1 << b)]);
}
};
using namespace std;
int main() {
FASTIO();
auto op = [](int a, int b) {return min(a,b);};
int n; cin >> n;
int x; vector<int> dat(n); REP(i, n){cin >> x; dat.at(i) = x;}
int res = 0;
SparseTable<int> st(dat, op);
REP(i,n){
FOR(j, i, n){
res = max(res, st.query(i, j + 1) * (j-i+1));
}
}
cout << res << "\n";
}
| #include <bits/stdc++.h>
typedef long long ll;
#define PI acos(-1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(a) (a).begin(), (a).end()
#define clr(a,h) memset(a, (h), sizeof(a))
#define F first
#define S second
#define fore(i,b,e) for(int i=(int)b;i<(int)e;++i)
#define forr(i,b,e) for(int i=(int)b;i<(int)e;++i)
#define deb(x) cerr << "# " << (#x) << " = " << (x) << endl;
#define sz(x) (int)x.size()
int faster_in(){int r=0,c;for(c=getchar();c<=32;c=getchar());if(c=='-') return -faster_in();for(;c>32;r=(r<<1)+(r<<3)+c-'0',c=getchar());return r;}
using namespace std;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
const int INF = 1234567890;
const int tam = 2010;
char M[2010][2010];
int dist[2010][2010];
vii tele[26];
bool vis[26];
int main()
{
std::ios::sync_with_stdio(false); cin.tie(0);
//freopen("","r",stdin);
//freopen("","w",stdout);
int h, w;
cin >> h >> w;
fore(i, 0, h) fore(j, 0, w) cin >> M[i][j];
int stR, stC;
int enR, enC;
fore(i, 0, h) fore(j, 0, w) {
if (M[i][j] == 'S') stR = i, stC = j;
if (M[i][j] == 'G') enR = i, enC = j;
char c = M[i][j];
if ('a' <= c && c <= 'z') {
tele[c-'a'].pb({i, j});
}
dist[i][j] = INF;
}
deque<ii> Q;
dist[stR][stC] = 0;
Q.push_back({stR, stC});
int dy[4] = {1,-1,0,0};
int dx[4] = {0,0,1,-1};
while (!Q.empty()) {
int y = Q.front().F;
int x = Q.front().S;
Q.pop_front();
char c = M[y][x];
if ('a' <= c && c <= 'z' && !vis[c-'a']) {
vis[c-'a'] = true;
for (ii u : tele[c-'a']) {
int a = u.F;
int b = u.S;
if (dist[a][b] > dist[y][x] + 1) {
dist[a][b] = dist[y][x] + 1;
Q.push_back({a, b});
}
}
}
fore(i, 0, 4) {
int a = y + dy[i];
int b = x + dx[i];
if (a < 0 || b < 0 || a >= h || b >= w) continue;
if (M[a][b] == '#') continue;
if (dist[a][b] <= dist[y][x] + 1) continue;
dist[a][b] = dist[y][x] + 1;
Q.push_back({a,b});
}
}
cout << (dist[enR][enC] == INF ? -1 : dist[enR][enC]) << '\n';
return 0;
}
// Dinosaurs are cool! |
#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() {
int n;
cin >> n;
vector<ll> v(2 * n, 0);
rep(i, 2 * n) {
cin >> v[i];
}
ll sum = 0, aoki = 0;
rep(i, 2 * n) sum += v[i];
priority_queue<ll, vector<ll>, greater<ll>> que;
for(int i = n - 1; i >= 0; i--) {
que.push(v[i]);
que.push(v[2 * n - 1 - i]);
aoki += que.top();
que.pop();
}
cout << sum - aoki << endl;
return 0;
}
| #pragma region Macros
#include <bits/stdc++.h>
#if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG)
#include <atcoder/all>
#endif
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>using vec = vector<T>;
template<class T, class U>using umap = unordered_map<T, U>;
using ll = long long;
using P = pair<ll, ll>;
using vl = vec<ll>;
#define fi first
#define se second
#define el endl
constexpr ll INF = numeric_limits<ll>::max()/2-1;
#pragma endregion
#pragma region IOMacros
template<class T>
istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T>
ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;}
#define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__);
void __i() {}
template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);}
void O() {cout << el;}
template<class T, class... Ts> void O(T&& o, Ts&&... args){cerr << o << " ";O(forward<Ts>(args)...);}
#pragma endregion
void Main();
int main(){
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
}
void Main(){
I(ll, N);
priority_queue<ll> q;
unordered_set<ll> mp;
ll mn = INF;
REP(i, N){
I(ll, a);
if(mp.find(a) == mp.end()){
chmin(mn, a);
q.push(a);
mp.insert(a);
}
}
while(q.size() > 1){
auto a = q.top(); q.pop();
a -= mn;
if (mp.find(a) == mp.end()){
chmin(mn, a);
q.push(a);
mp.insert(a);
}
}
cout << q.top() << el;
}
|
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
#include <iomanip>
#include <algorithm>
#include <chrono>
using namespace std;
int main() {
long long int t,n;
cin>>t>>n;
vector<long long int> skip;
long long int pre = 1;
for(long long int base=1;base<=100;base++){
if(floor(base*(100+t)/100)-pre>1) skip.push_back(pre+1);
pre=floor(base*(100+t)/100);
}
//for(int i=0;i<(int)skip.size();i++){
// cout<<skip[i]<<" ";
//}
long long int period = 100+t;
long long int nSet = n/t;
//cout<<endl<<nSet<<"*"<<period<<endl;
//cout<<"n%t = "<<n%t<<endl;
long long int answer = 0;
if(n%t==0){
answer = period*nSet-1;
}else{
answer = period*nSet+(skip[n%t-1]);
}
cout<<answer<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<(int)(n);i++)
#define si(c) ((int)(c).size())
#define forsn(i,s,n) for(int i = (int)(s); i<((int)n); i++)
#define dforsn(i,s,n) for(int i = (int)(n)-1; i>=((int)s); i--)
#define all(c) (c).begin(), (c).end()
#define D(a) cerr << #a << "=" << a << endl;
#define pb push_back
#define eb emplace_back
#define mp make_pair
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t,n; cin >> t >> n;
ll lo = 0, hi = 1e15;
while (lo + 1 < hi) {
ll mi = (lo+hi)/2;
ll should = mi, reach = (mi * (100+t) / 100);
if (reach - should >= n) hi = mi;
else lo = mi;
}
cout << hi+n-1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
typedef long long int LL;
typedef unsigned long long int ULL;
using namespace std;
// 插入此處
#define D long long
#define F double
#define mmm(a,b) memset(a,b,sizeof(a))
D read(){ D ans=0; char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans; return ans;
}
const int N=10000000;
const F pi=acos(-1);
D n,m,_n,_m,p=1e9+7,mi;
D f[N+9],mu[N+9];
D pri[664600],now;
bool vis[N+9];
void init(){
mmm(vis,0);mi=min(m,n);now=0;
for(D i=1;i<=mi;i++){
f[i]=((n/i)-(_n-1)/i)*((m/i)-(_m-1)/i);
}
mu[1]=1;
for(int i=2;i<=N;i++){
if(!vis[i]){
pri[++now]=i;
mu[i]=-1;
}
for(int j=1;j<=now&&i*pri[j]<=N;j++){
vis[i*pri[j]]=true;
if(i%pri[j])mu[i*pri[j]]=-mu[i];//出现新的素数 mu=-mu
else{
mu[i*pri[j]]=0; //出现相同素数
break;
}
}
}
}
int main(){
LL l, r;
cin >> l >> r;
_n=l,n=r,_m=l,m=r;
init();
D coprime=0;
for(int i=1;i<=mi;i++)
coprime+=f[i]*mu[i];
coprime /= 2;
// printf("coprime = %lld\n", coprime);
LL num = r - l + 1;
LL ans = num * (num - 1) / 2;
// printf("all: %lld\n", ans);
LL times = 0;
for (LL i = l; i <= r; i++) {
times += (r / i - 1);
}
LL ret = 0;
if (l != 1) {
ret = (ans - times - coprime) * 2;
} else {
ret = (ans - coprime - times + r - 1) * 2;
}
// printf("times: %lld\n", times);
printf("%lld\n", ret);
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < (n); ++i)
#define DREP(i,s,n) for(int i = (s); i < (n); i++)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000007;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}
void bfs(vector<vector<int>> &G, int N, int s, vector<int> &dist) {
dist.assign(N,-1);
dist[s] = 0;
queue<int> que;
que.push(s);
while(que.size()) {
int v = que.front();
que.pop();
for(int nv : G[v]) {
if(dist[nv] != -1) continue;
dist[nv] = dist[v] + 1;
que.push(nv);
}
}
}
int main() {
int N,M; cin >> N >> M;
vecveci G(N);
REP(i,M) {
int a,b; cin >> a >> b;
a--, b--;
G[a].push_back(b);
}
ll ans = 0;
REP(s,N) {
veci dist(N,-1);
bfs(G,N,s,dist);
REP(g,N) {
if(dist[g] != -1) ans++;
}
}
cout << ans << endl;
return 0;
} |
#include <cstdio>
#include <algorithm>
const int N = 2e5 + 5;
int a[N];
int b[N];
int ans[N];
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
#endif
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
b[a[i]] = i;
}
int cnt = 0;
for (int i = n; i >= 1; --i) {
for (int k = b[i]; k < i; ++k) {
std::swap(a[k], a[k + 1]);
std::swap(b[a[k]], b[a[k + 1]]);
ans[cnt++] = k;
if (cnt >= n - 1) {
break;
}
}
if (cnt >= n - 1) {
break;
}
}
if (std::is_sorted(a, a +n) && cnt == n - 1) {
for (int i = 0; i < cnt; ++i) {
printf("%d\n", ans[i]);
}
} else {
printf("-1\n");
}
}
return 0;
}
| /*
author : pshishod2645
Created at : 06:05:49 PM, 05 December 2020
*/
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
typedef long long ll;
//#define int long long
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
// os.find_by_order(k) -> find kth element
// os.order_of_key(x) -> find number of elements strictly less than x
#define cerr cout
#define sz(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A> string to_string(A v) {bool f = 1; string r = "{"; for (const auto &x : v) {if (!f)r += ", "; f = 0; r += to_string(x);} return r + "}";}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {cerr << " " << to_string(H); debug_out(T...);}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
inline int add(int a, int b){a += b; if(a >= MOD)a -= MOD; return a;}
inline int sub(int a, int b){a -= b; if(a < 0)a += MOD; return a;}
inline int mul(int a, int b){return (int)((long long) a * b %MOD);}
inline int binpow(int a, int b){int res = 1; while(b > 0){ if(b & 1)res = mul(res, a); a = mul(a, a);b /= 2;} return res;}
inline int inv(int a){return binpow(a, MOD - 2);}
int gcd(int a, int b, int &x, int &y){if(a == 0){x = 0, y = 1; return b;} int x1, y1; int d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}
int gcd(int x, int y){return y? gcd(y, x%y) : x; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution<int> (l, r) (rng) // generates random number in [l, r] inclusive
// shuffle(beginIter, endIter, rng) // randomly shuffles elements in range [beginIter, endIter]
const int N = 2e5 + 5;
int n, a[N], possible = 1, pos[N];
vector<int> ops;
void find(int ind){
// pr(ind); pr(ops);
if(ind == 1){
if(a[ind] != 1)possible = 0; return ;
}
if(a[ind] == ind){
possible = 0; return ;
}
int prv = pos[ind];
for(int i = prv; i < ind; ++i){
ops.push_back(i);
if(i != prv && a[i + 1] != i){
// pr(i, a[i + 1], i);
possible = 0; return ;
}
}
pos[a[prv + 1]] = prv; a[prv] = a[prv + 1];
find(prv);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n;
for(int i = 1;i <= n; ++i){
cin>>a[i]; pos[a[i]] = i;
}
find(n);
if(!possible){
cout<<"-1\n"; return 0;
}
for(int i: ops)cout<<i<<"\n";
return 0;
} |
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cassert>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pi;
const int maxn = 2e5 + 5;
ll a[maxn], b[maxn];
ll c[maxn];
int n;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
ll mx = 0;
for(int i = 1;i <= n;i++){
mx = max(a[i], mx);
cin >> b[i];
c[i] = max(b[i] * mx, c[i - 1]);
}
for(int i = 1;i <= n;i++){
cout << c[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
const long double INF = 1e15;
int N = 30*30;
int L = 30;
// (i,j) i個下 , j個右
int ind(int a,int b){
return a*L + b;
}
int Vi(int v){return v/L;}
int Vj(int v){return v%L;}
bool in(int i,int j,int si,int sj,int ti,int tj){
if(i<min(si,ti) || max(si,ti)<i) return false;
if(j<min(sj,tj) || max(sj,tj)<j) return false;
return true;
}
int length(int v1,int v2){return abs(Vi(v1)-Vi(v2))+abs(Vj(v1)-Vj(v2));}
vector<int> VinRectangle(int si,int sj,int ti,int tj){
// 四角形の中の頂点を始点からマンハッタン距離が小さい順に
vector< pair<int,int> > Vs;
FOR(i,min(si,ti),max(si,ti)+1){
FOR(j,min(sj,tj),max(sj,tj)+1){
Vs.push_back( { abs(i-si)+abs(j-sj) , ind(i,j) } );
}
}
sort(Vs.begin(), Vs.end());
vector<int> res;
for(auto v:Vs) res.push_back(v.second);
return res;
}
//vector< vector<string> > S(N,vector<string>(N));
vector< vector<long double> > Len(N,vector<long double>(N,INF));
//vector< vector<char> > C(N,vector<char>(N,'.'));
vector< vector<string> > route(N,vector<string>(N)); // Len の時のルートを示す文字列
void first_do_in_main(){
FOR(i,0,N){
int ai = Vi(i);
int aj = Vj(i);
if( in(ai+1,aj,0,0,L-1,L-1) ){
Len[i][ind(ai+1,aj)] = 5000.0;
route[i][ind(ai+1,aj)] = "D";
}
if( in(ai-1,aj,0,0,L-1,L-1) ){
Len[i][ind(ai-1,aj)] = 5000.0;
route[i][ind(ai-1,aj)] = "U";
}
if( in(ai,aj+1,0,0,L-1,L-1) ){
Len[i][ind(ai,aj+1)] = 5000.0;
route[i][ind(ai,aj+1)] = "R";
}
if( in(ai,aj-1,0,0,L-1,L-1) ){
Len[i][ind(ai,aj-1)] = 5000.0;
route[i][ind(ai,aj-1)] = "L";
}
}
}
string rev_route(string s){
reverse(s.begin(), s.end());
FOR(i,0,s.size()){
if(s[i] == 'U'){
s[i] = 'D';
}else if(s[i] == 'D'){
s[i] = 'U';
}else if(s[i] == 'R'){
s[i] = 'L';
}else if(s[i] == 'L'){
s[i] = 'R';
}
}
return s;
}
string do_calc(int si,int sj,int ti, int tj){
int start,finish;
vector<long double> d(N,INF); // startからiまでの距離 d[i]
vector<int> pv(N); // pv[i]はiの前の頂点
vector<string> rv(N); // pv[i]からiへどうやって来たか
start = ind(si,sj);
finish = ind(ti,tj);
d[start] = 0;
auto Vs = VinRectangle(si,sj,ti,tj);
int Nvs = Vs.size();
FOR(i,0,Nvs){
FOR(j,0,i){
// a -> b へのルート
int a = Vs[j];
int b = Vs[i];
if(route[a][b].size() > 0 && d[a]+Len[a][b] < d[b]){
d[b] = d[a]+Len[a][b];
pv[b] = a;
rv[b] = route[a][b];
}
}
}
vector<string> s;
while(finish != start){
s.push_back(rv[finish]);
finish = pv[finish];
}
string res;
reverse(s.begin(), s.end());
for(auto s_ : s) res += s_;
return res;
}
bool check(string s,int si,int sj,int ti,int tj){
for(char x:s){
if(x=='U') si--;
if(x=='D') si++;
if(x=='R') sj++;
if(x=='L') sj--;
}
return (si==ti)&&(sj==tj);
}
int main(){
first_do_in_main();
FOR(k,0,1000){
int si,sj,ti,tj;
cin >> si >> sj >> ti >> tj;
string res = do_calc(si,sj,ti,tj);
cout << res << endl;
cout << flush;
long double be;
cin >> be;
//assert(check(res,si,sj,ti,tj));
//assert(check(rev_route(res),ti,tj,si,sj));
/*int st = ind(si,sj);
int fi = ind(ti,tj);
route[st][fi] = res;
Len[st][fi] = be;
route[fi][st] = rev_route(res);
Len[fi][st] = be;
*/
}
} |
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long long ll;
#define G getchar()
int read()
{
int x=0; bool flg=false; char ch=G;
for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
return flg?-x:x;
}
#undef G
#define fi first
#define se second
/*
const int mod=;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,const int &y){x=upd(x+y-mod);}
inline void iadd(int &x,const int &y){x=upd(x-y);}
int qpow(int x,int y){
int res=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1) res=1LL*res*x%mod;
return res;
}
*/
//typedef pair<int,int> P;
#define rep(i,l,r) for (int i(l);i<=int(r);i++)
#define per(i,l,r) for (int i(r);i>=int(l);i--)
#define all(x) (x).begin(),(x).end()
#define forall(x,y) for (const int &y: e[x])
int n;
vector<int> e[200010];
int w[200010],q;
int fat[200010],dep[200010];
void dfs0(int x,int f,int d){
fat[x]=f; dep[x]=d;
forall(x,y) if (y^f) dfs0(y,x,d+1);
}
void dfs1(int x,int f){
w[x]=++q;
forall(x,y) if (y^f) dfs1(y,x),++q;
}
void solve(){
n=read();
rep(i,2,n){
int u=read(),v=read();
e[u].pb(v),e[v].pb(u);
}
dfs0(1,0,1);
const int S=max_element(dep+1,dep+n+1)-dep;
dfs0(S,0,1);
const int T=max_element(dep+1,dep+n+1)-dep;
static int p[200010],m;
for (int i=T;i;i=fat[i]) p[++m]=i;
reverse(p+1,p+m+1);
rep(i,1,m){
const int &x=p[i];
w[x]=++q;
forall(x,y) if (y!=p[i+1]&&y!=p[i-1]) dfs1(y,x),++q;
}
rep(i,1,n) printf("%d ",w[i]);
puts("");
}
int main()
{
for (int T=1;T--;) solve();
return 0;
} | /*
/\ In The Name Of Allah /\
Author : Jawahiir Nabhan
*/
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const char nl = '\n';
const int NM = 2e5 + 10;
int dis[NM],order[NM],vis[NM],depth[NM];
int N,cnt = 1;
vector <int> g[NM];
void dfs1(int u)
{
vis[u] = 1;
for(int v : g[u]){
if(vis[v]) continue;
dfs1(v);
depth[u] = max(depth[u], depth[v] + 1);
}
}
void dfs2(int u)
{
order[u] = cnt;
vis[u] = 1;
vector <pair<int, int>> vec;
for(int v : g[u])
{
if(vis[v]) continue;
vec.pb({depth[v], v});
}
sort(vec.begin(),vec.end());
for(auto &[x, y] : vec)
{
cnt++;
dfs2(y);
cnt++;
}
}
int main()
{
cin>> N;
for(int i = 1;i <= N - 1;i++){
int u,v; cin>> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1;i <= N;i++) dis[i] = (1 << 30);
queue <int> q;
q.push(1);
dis[1] = 0;
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v : g[u]){
if(dis[v] == (1 << 30))
{
q.push(v);
dis[v] = dis[u] + 1;
}
}
}
int pos,mx = -1;
for(int i = 1;i <= N;i++){
if(dis[i] > mx){
mx = dis[i];
pos = i;
}
}
dfs1(pos);
for(int i = 1;i <= N;i++) vis[i] = 0;
dfs2(pos);
for(int i = 1;i <= N;i++) cout<< order[i] << ' ';
cout<< nl;
}
|
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
using ll = long long;
struct Edge
{
ll to;
ll cost;
};
using Graph = vector<vector<Edge>>;
using P = pair<ll, ll>;
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), v.end()
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = LLONG_MAX;
const int MAX = 510000;
bool graph[nmax][nmax];
long long 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(int n, int k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
void warshall_floyd(ll n)
{
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < n; j++)
{
for (size_t k = 0; k < n; k++)
{
dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
}
}
}
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
ll g = gcd(a, b);
return a / g * b;
}
ll mulMod(ll a, ll b)
{
return (((a % MOD) * (b % MOD)) % MOD);
}
ll powMod(ll a, ll p)
{
if (p == 0)
{
return 1;
}
else if (p % 2 == 0)
{
ll half = powMod(a, p / 2);
return mulMod(half, half);
}
else
{
return mulMod(powMod(a, p - 1), a);
}
}
ll ceil(ll a, ll b)
{
return (a + b - 1) / b;
}
int main()
{
ll n, X;
cin >> n >> X;
vector<ll> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
vector<ll> x(n);
for (int i = n - 1; i >= 0; --i)
{
x[i] = X / a[i];
X %= a[i];
}
vector<ll> t(n);
for (int i = 0; i < n - 1; i++)
{
t[i] = a[i + 1] / a[i];
}
t[n - 1] = 1e18;
vector<ll> dp(2);
dp[0] = 1;
for (int i = 0; i < n; i++)
{
vector<ll> p(2); // dp[i-1]
swap(dp, p);
for (int j = 0; j < 2; j++)
{
for (int nj = 0; nj < 2; nj++)
{
// x + z + j = y + nj*t[i]
// z = y + nj*t[i] - x - j;
{
// y == 0
ll z = 0 + t[i] * nj - x[i] - j;
if (0 <= z && z < t[i])
dp[nj] += p[j];
}
{
// z == 0;
ll y = x[i] + 0 + j - nj * t[i];
if (0 < y && y < t[i])
dp[nj] += p[j];
}
}
}
}
cout << dp[0] << endl;
return 0;
}
| //#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#include<string.h>
#define un unsigned
#define srd srand(time(0))
#define ll long long
#define con continue
#define gtc getchar()
#define ptc putchar
#define dou double
#define eps 0.00000000001
#define opr operator
#define cl(x,a) memset(x,a,sizeof(x))
#define fo0(i,k) for(i=fr[k];i;i=nx[i])
#define fo1(i,l,r) for(i=l;i<=r;i++)
#define fo2(i,l,r) for(i=l;i>=r;i--)
#define fo(i,n) for(i=1;i<=n;i++)
#define ret return
#define x first
#define cint const int
#define y second
#define opi(x) freopen(x,"r",stdin)
#define opo(x) freopen(x,"w",stdout)
#define tpl template<class T>
#define priq priority_queue
#define mp make_pair
#define use using namespace
#define WT while(T--)
use std;
typedef pair<int,int> pii;typedef pair<int,ll> pil;typedef pair<ll,int> pli;typedef pair<ll,ll> pll;
namespace io
{
void _(int &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;}
void _(ll &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;}
void _(char &c){while((c=gtc)==' '||c=='\n');}void _(dou &c){scanf("%lf",&c);}void _(char *s){char c;while((c=gtc)!=EOF&&c!=' '&&c!=10)*s++=c;}
template<class t1,class t2>void _(t1 &a,t2 &b){_(a);_(b);}template<class t1,class t2,class t3>void _(t1 &a,t2 &b,t3 &c){_(a);_(b);_(c);}
template<class t1,class t2,class t3,class t4>void _(t1 &a,t2 &b,t3 &c,t4 &d){_(a);_(b);_(c);_(d);}
template<class t1,class t2,class t3,class t4,class t5>void _(t1 &a,t2 &b,t3 &c,t4 &d,t5 &e){_(a);_(b);_(c);_(d);_(e);}
void _p(dou k){printf("%.6lf",k);}void _p(char *c){for(;*c;ptc(*c++));}void _p(const char *c){for(;*c;ptc(*c++));}void _p(char c){ptc(c);}
tpl void _p0(T k){if(k>=10)_p0(k/10);ptc(k%10+'0');}tpl void _p(T k){if(k<0){ptc('-');_p0(-k);}else _p0(k);}tpl void __p(T k){_p(k);ptc(' ');}
tpl void _pn(T k){_p(k);ptc('\n');}template<class t1,class t2>void _p(t1 a,t2 b){__p(a);_pn(b);}
template<class t1,class t2,class t3>void _p(t1 a,t2 b,t3 c){__p(a);__p(b);_pn(c);}
template<class t1,class t2,class t3,class t4>void _p(t1 a,t2 b,t3 c,t4 d){__p(a);__p(b);__p(c);_pn(d);}
tpl void op(T *a,int n){int i;n--;fo(i,n)__p(a[i]);_pn(a[n+1]);}int gi(){int x;_(x);ret x;}ll gll(){ll x;_(x);ret x;}
}
int gcd(int a,int b){ret b?gcd(b,a%b):a;}void fcl(){fclose(stdin);fclose(stdout);}
void fop(const char *s){char c[256],d[256];cl(c,0);cl(d,0);strcpy(c,s);strcpy(d,s);opi(strcat(c,".in"));opo(strcat(d,".out"));}
int eq(dou a,dou b){return a+eps>=b&&b+eps>=a;}tpl void _ma(T &a,T b){if(a<b)a=b;}tpl void _mi(T &a,T b){if(a>b)a=b;}
cint N=1234567,EE=100000000,GG=1000000000,ima=2147483647;
use io;
int n,m,a[N],f[N],T;
char an[666][666];
void ck(int k,int x,int y,int b)
{
if(k==0)
an[x][y]='A'+b;
else
{
ck(k-1,x,y,b);
ck(k-1,x+(1<<(k-1)),y,b);
ck(k-1,x,y+(1<<(k-1)),b);
ck(k-1,x+(1<<(k-1)),y+(1<<(k-1)),b^1);
}
}
int main()
{
int i,j,a1,a2;
_(n);
ck(n,0,0,0);
_pn((1<<n)-1);
fo(i,(1<<n)-1)
puts(an[i]);
}
|
#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define ll long long
#define INF 1LL << 33
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define SHOW(p) \
if (test) \
cout << #p " : " << p << endl;
//bool test = true;
bool test = false;
template <class T>
[[maybe_unused]] bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
};
int N, max_A, max_B, tmp, ans;
vector<pair<int, int>> A, B;
bool same;
signed main()
{
cin >> N;
A.resize(N);
B.resize(N);
REP(i, N)
{
cin >> tmp;
A.at(i) = {tmp, i};
cin >> tmp;
B.at(i) = {tmp, i};
}
sort(A.begin(), A.end());
sort(B.begin(), B.end());
max_A = A.at(0).first;
max_B = B.at(0).first;
ans = max_A + max_B;
REP(i, N)
{
if (ans < A.at(i).first)
break;
REP(j, N)
{
if (test)
cout << "i: " << i << " j: " << j << endl;
if (ans < B.at(i).first)
break;
if (A.at(i).second != B.at(j).second) {
same = false;
chmin(ans, max(A.at(i).first, B.at(j).first));
} else {
same = true;
chmin(ans, A.at(i).first + B.at(j).first);
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);cerr<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
int main() {
ll N;
cin >> N;
vl C(N);
rep(i, N) cin >> C[i], C[i]--;
vvc<ll> G(N);
rep(i, N - 1){
ll a, b;
cin >> a >> b;
a--, b--;
G[a].eb(b), G[b].eb(a);
}
vl ok(N), U(100000, -1);
auto dfs = [&](auto&& dfs, ll now, ll par) -> void {
if(U[C[now]] == -1) {
ok[now] = 1;
U[C[now]] = now;
}
repa(to, G[now]){
if (to == par) continue;
dfs(dfs, to, now);
}
if(U[C[now]] == now){
U[C[now]] = -1;
}
};
dfs(dfs, 0, -1);
rep(i, N){
if(ok[i]){
cout << i + 1 << '\n';
}
}
} |
#ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
template<typename T> bool chmax(T &a,T b) {if(a<b) {a=b; return true;} return false;}
template<typename T> bool chmin(T &a,T b) {if(a>b) {a=b; return true;} return false;}
#define itn int
#define fi first
#define se second
#define intmax numeric_limits<int>::max()
#define llmax numeric_limits<ll>::max()
#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 rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>=1;i--)
#define all(vec) vec.begin(),vec.end()
#define sortt(vec) sort((vec).begin(),(vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef tuple<ll,ll,ll> tlll;
typedef tuple<int,int,int> tiii;
const ll mod=1e9+7;
const int inf=1<<30;
const ll lnf=1ll<<60;
ll rui(ll a,ll n){
ll ans=1;
while(n>0){
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n/=2;
}
return ans;
}
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
mint dp[1010];
int main(){
dp[1]=2;
dp[2]=3;
rep1(i,1e3) dp[i+2]=dp[i+1]+dp[i];
int n; cin >> n;
char a,b,c,d; cin >> a >> b >> c >> d;
if(n==2||n==3){
cout << 1 << endl;
return 0;
}
if(b=='B'){
if(d=='B'){
cout << 1 << endl;
}
else{
if(c=='A'){
cout << rui(2,n-3) << endl;
}
else{
cout << dp[n-3] << endl;
}
}
}
else{
if(a=='A'){
cout << 1 << endl;
}
else{
if(c=='B'){
cout << rui(2,n-3) << endl;
}
else{
cout << dp[n-3] << endl;
}
}
}
} | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
string res = "{";
for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
cerr << " " << to_string(H);
debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
const int MAXN = 100005;
int val[MAXN];
vector<pair<int, int>> E[MAXN];
int n;
void go(int x) {
for (auto& e : E[x]) {
int y = e.first, c = e.second;
if (val[y]) continue;
if (c == val[x]) val[y] = (c == n ? 1 : c + 1);
else val[y] = c;
go(y);
}
}
int main() {
int m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
E[u].push_back({v, c});
E[v].push_back({u, c});
}
val[1] = 1;
go(1);
for (int i = 1; i <= n; ++i) printf("%d\n", val[i]);
}
|
#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 n;
cin >> n;
ll a[n];
rep(i, n) {
cin >> a[i];
}
ll ans = 0;
map<ll, ll> mp;
rep(i, n) {
for(auto p : mp){
ans += (a[i] - p.first) * (a[i] - p.first) * p.second;
}
mp[a[i]]++;
}
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(){
long long int x,y,a,b;
cin >> x >> y >> a >> b;
long long int ans=0;
while((double)a*x<=2e18 && a*x<=x+b && a*x<y ){
ans+=1;
x*=a;
}
cout << ans+(y-1-x)/b <<endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define M 1000000007
#define pb push_back
#define mp make_pair
#define pi 3.1415926536
#define prior priority_queue<int,vector<int>,greater<int>>
#define fi first
#define se second
const int INF =1e9 +5;
const int N=4e6+5;
/*int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
int pw(int a,int b,int p){
if(b==0)return 1;
int t=pw(a,b/2,p);
if(b%2)return (((a*t)%p)*t)%p;
else return ((t*t)%p);
}
int fact[N],invfact[N];
void init(){
fact[0]=1;
int i;
for(i=1;i<N;i++)
{
fact[i]=i*fact[i-1]%M;
}
i--;
invfact[i]=pw(fact[i],M-2,M);
for(i--;i>=0;i--){
invfact[i]=(i+1)*invfact[i+1]%M;
}
}
int ncr(int n,int r,int p){
return (((fact[n]*invfact[n-r])%p)*invfact[r])%p;
}
int f(int r1,int r2,int k){
int ans= ncr(k+1+r2,k,M) - ncr(k+r1,k,M)+M;
ans%=M;
return ans;
}*/
int32_t main()
{
IOS;
int t=1;
//cin>>t;
while(t--)
{
int n;
cin>>n;
cout<<(n/100)+bool (n%100);
}
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0)
#define MOD 100000007
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vp;
typedef vector<ll> vi;
typedef set<ll> sit;
int main()
{
fast_io;
string str, str2;
ll size, elm, tc, lim, n;
//cin>>tc;
tc = 1;
while(tc--)
{
cin>>str;
vi vec(10, 0);
size = str.size();
for(ll x = 0; x < size; x++)
{
vec[str[x] - '0']++;
}
ll ans = 0;
if(size < 3)
{
if(size == 1 && (str[0]-'0')%8 == 0)
cout<<"Yes";
else if(size == 1)
cout<<"No";
else if(size == 2 && (stoi(str) % 8 == 0))
cout<<"Yes";
else if(size == 2)
{
swap(str[0], str[1]);
if((stoi(str) % 8 == 0))
cout<<"Yes";
else
cout<<"No";
}
}
else
{
for(ll x = 104; x < 999; x+=8)
{
vi vec2;
str = to_string(x);
for(ll i=0; i < 10; i++)
vec2.pb(vec[i]);
ll is = 1;
for(ll y = 0; y < str.size(); y++)
{
if(vec2[str[y] - '0'] <= 0)
{
is = 0;
break;
}
else
vec2[str[y] - '0']--;
}
if(is)
{
ans = x;
//cout<<ans;
}
}
if(!ans)
cout<<"No";
else
cout<<"Yes \n";
}
}
return 0;
} |
#include <iostream>
using namespace std;
int main(){
int n, c = 0, jud = 0, max = 0;
int a[100], b[100], judge[100];
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
cin >> b[i];
if(b[i] > max){
max = b[i];
}
}
for(int i = 0; i < max+1; i++){
for(int j = 0; j < n; j++){
if(a[j] <= i && b[j] >= i){
judge[j] = 1;
}
else{
judge[j] = 0;
}
}
for(int i = 0; i < n; i++){
if(judge[i] == 1){
jud = 1;
}
else{
jud = 0;
break;
}
}
if(jud == 1){
c++;
}
}
cout << c << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fs(n) fixed << setprecision(n)
using ll = long long;
int fact(int x) { return x <= 1 ? 1 : x * fact(x - 1); }
int sign(int x){
return (x > 0) - (x < 0);
}
bool isLeapYear(int y, int m){
return (y % 400 == 0 || (y % 4 == 0 && y % 100)) && m == 2;
}
vector<int> AllDivs(int d){
vector<int> resDivs(0);
for(int i = 1; i < (int)sqrt(d); i++){
if(d % i == 0){
resDivs.push_back(i);
resDivs.push_back(d / i);
}
}
sort(resDivs.begin(), resDivs.end());
return resDivs;
}
bool IsPrime(int num){
bool ans = true;
if(num < 2) return false;
else if (num == 2) return true;
else{
for(int i = 2; i <= (int)sqrt(num); i++){
if(num % i == 0){
ans = false;
}
}
}
return ans;
}
int NextPrime(int d){
int ans = d;
while(true){
ans++;
if(IsPrime(ans)) break;
}
return ans;
}
vector<int> Dec2Bin(int x, int len){
vector<int> res(len, 0);
int d = 1, pt = len - 1;
while(d <= x){
res[pt] = (x & d ? 1 : 0);
d *= 2; pt--;
}
return res;
}
int avg(int a, int b){
return (a + b) / 2;
}
int main(){
int n; cin >> n;
vector<int> a(n), b(n);
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++) cin >> b[i];
int amin = *max_element(a.begin(), a.end());
int bmin = *min_element(b.begin(), b.end());
int ans = bmin - amin + 1;
cout << max(0, ans) << endl;
} |
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define erep(i,j) for(int i=hd[j];i;i=nxt[i])
inline int read(){
int x=0,f=1;char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
using namespace std;
const int M=1e6+5;
int mob[M],vis[M],cnt[M];
int E[M*20],hd[M*20],nxt[M*20];
int tot;
void add(int a,int b){
E[++tot]=b,nxt[tot]=hd[a];
hd[a]=tot;
}
long long que(int l,int r){
long long ans=0;
rep(i,1,r)cnt[i]=0;
rep(i,l,r){
erep(j,i){
int t=E[j];
ans+=cnt[t];
cnt[t]+=mob[t];
}
}
return ans;
}
int main() {
int L=read(),R=read();
rep(i,1,M-1)mob[i]=1;
rep(i,2,M-1){
if(vis[i])continue;
mob[i]=-1;
for(int j=i+i;j<M;j+=i){
vis[j]=1;
if(j/i%i==0)mob[j]=0;
else mob[j]=-mob[j];
}
}
rep(i,1,M-1)for(int j=i;j<M;j+=i)add(j,i);
long long ans=0;
rep(i,2,M-1){
int mi=2e9,mx=-2e9;
for(int j=i+i;j<M;j+=i)if(j>=L&&j<=R)mi=min(mi,j/i),mx=max(mx,j/i);
if(mi<=mx)ans+=que(mi,mx);
}
printf("%lld\n",ans*2);
return 0;
} | #include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = 998244353ll;
#define l_ength size
void mul_mod(ll& a, ll b){
a *= b;
a %= MOD;
}
void add_mod(ll& a, ll b){
a = (a<MOD)?a:(a-MOD);
b = (b<MOD)?b:(b-MOD);
a += b;
a = (a<MOD)?a:(a-MOD);
}
ll memo[5050][5050];
ll rwpw(ll a, ll p){
return memo[a][p];
}
int main(void){
int n,m,i,j,v;
ll ans = 0ll, calc, tmp;
std::cin >> n >> m;
for(i=0; i<=m; ++i){
memo[i][0] = 1ll;
for(j=1; j<=n; ++j){
memo[i][j] = memo[i][j-1]*i%MOD;
}
}
for(v=1; v<=m; ++v){
// 端&端
add_mod(ans,rwpw(m-v+1,n));
add_mod(ans,MOD-rwpw(m-v,n));
for(i=1; i<n; ++i){
// 片方が端
calc = 0ll;
add_mod(calc,rwpw(m-v+1,i));
add_mod(calc,MOD-rwpw(m-v,i));
mul_mod(calc,v-1);
tmp = calc;
mul_mod(tmp,rwpw(m,n-i-1));
mul_mod(tmp,2ll);
add_mod(ans,tmp);
if(n-i<2){
continue;
}
tmp = calc;
mul_mod(tmp,v-1);
mul_mod(tmp,rwpw(m,n-i-2));
mul_mod(tmp,n-i-1);
add_mod(ans,tmp);
}
}
std::cout << ans << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string x;
int p;
cin >> x;
p = x.find(".");
if(p == -1){
cout << x << endl;
}
else{
cout << x.substr(0,p) << endl;
}
}
| #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define INF 1e9
#define PI 3.141592653589793238
#define N 1000000010
#define mod 1000000007
#define rep(i,n) for(int i=0; i<n; i++)
typedef long long ll;
int main() {
string s;
cin>>s;
int index=0;
while(s[index] != '.' && index < s.length()){
char c = s[index];
int n = c;
cout<<n-48;
index++;
}
cout<<endl;
return 0;
} |
#include <iostream>
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ull unsigned long long
#define int long long
#define pre(a,b) cout<<fixed<<setprecision(a)<<b<<"\n";//a for precision b for answer
//#define mp make_pair
#define max3(x,y,z) max(x,max(y,z))
#define min3(x,y,z) min(x,min(y,z))
#define pb push_back
#define INF 1e18
#define min(a,b) (a<b?a:b)
#define forn(n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define MAX 1e18;
#define all(x) (x).begin(),(x).end()
#define lb lower_bound
#define ub upper_bound
#define vmin *min_element
#define vmax *max_element
#define pii pair<int,int>
int mod = 1e9+7;
#define N 100005
#define pi 3.141592653589793238
using namespace std;
vector<int>adj[N];
vector<int>vis(N,0);
vector<int>color(N,0);
vector<int>good(N,0);
map<int,int>mp;
void dfs(int s,int par)
{
vis[s]=1;
mp[color[s]]++;
for(int i=0; i<adj[s].size();i++)
{
if(adj[s][i]!=par)
{
dfs(adj[s][i],s);
}
}
if(mp[color[s]]==1)
{
good[s] = 1;
mp[color[s]]--;
}
else
{
mp[color[s]]--;
}
}
void solve(int cc)
{
int n;
cin>>n;
for(int i=1; i<=n;i++)
{
cin>>color[i];
}
int p =n-1;
while(p--)
{
int a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1,0);
for(int i=1; i<=n; i++)
if(good[i]==1)
cout<<i<<endl;
}
signed main()
{
IOS;
int t = 1;
//cin>>t;
int cc =0;
while(t--)
{
// cout<<f(0,0,4,);
cc++;
solve(cc);
cout<<endl;
}
}
//110101
//((()))
//(())() | #include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
using ll = long long;
struct Edge
{
ll to;
ll cost;
};
using Graph = vector<vector<ll>>;
using P = pair<ll, ll>;
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), v.end()
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = LLONG_MAX;
const int MAX = 510000;
bool graph[nmax][nmax];
long long 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(int n, int k)
{
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
// void warshall_floyd(ll n)
// {
// for (size_t i = 0; i < n; i++)
// {
// for (size_t j = 0; j < n; j++)
// {
// for (size_t k = 0; k < n; k++)
// {
// dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
// }
// }
// }
// }
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
ll g = gcd(a, b);
return a / g * b;
}
struct Matrix
{
int sz;
ll x[33][33];
};
Matrix multiply(Matrix A, Matrix B)
{
Matrix C;
C.sz = A.sz;
for (int i = 0; i < C.sz; i++)
{
for (int j = 0; j < C.sz; j++)
{
C.x[i][j] = 0;
}
}
for (int i = 0; i < A.sz; i++)
{
for (int j = 0; j < A.sz; j++)
{
for (int k = 0; k < A.sz; k++)
{
C.x[i][k] += A.x[i][j] * B.x[j][k];
C.x[i][k] %= MOD;
}
}
}
return C;
}
Matrix powers(Matrix A, ll T)
{
Matrix E[64], F;
E[0] = A;
for (int i = 1; i < 62; i++)
{
E[i] = multiply(E[i - 1], E[i - 1]);
}
F.sz = E[0].sz;
for (int i = 0; i < F.sz; i++)
{
for (int j = 0; j < F.sz; j++)
{
if (i == j)
F.x[i][j] = 1;
else
F.x[i][j] = 0;
}
}
for (int i = 62; i >= 0; i--)
{
if ((T & (1LL << i)) != 0LL)
{
F = multiply(F, E[i]);
}
}
return F;
}
ll ceil(ll a, ll b)
{
return (a + b - 1) / b;
}
vector<ll> ans;
vector<bool> seen;
vector<long long> C;
void dfs(Graph &G, int v, map<ll, ll> &color)
{
seen[v] = true;
if (color[C[v]] == 0)
{
ans.push_back(v);
}
color[C[v]]++;
for (auto nv : G[v])
{
if (seen[nv])
continue;
dfs(G, nv, color);
}
color[C[v]]--;
}
int main()
{
long long N;
scanf("%lld", &N);
C.assign(N, 0);
for (int i = 0; i < N; i++)
{
scanf("%lld", &C[i]);
}
Graph G(N);
seen.assign(N, false);
for (int i = 0; i < N - 1; i++)
{
ll a, b;
cin >> a >> b;
--a;
--b;
G[a].push_back(b);
G[b].push_back(a);
}
map<ll, ll> color;
dfs(G, 0, color);
SORT(ans);
for (auto &&i : ans)
{
cout << i + 1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;
using ll = long long;
int main() {
int N;
cin >> N;
string ans;
int x = 108 * N / 100;
if (x < 206) ans = "Yay!";
else if (x == 206) ans = "so-so";
else ans = ":(";
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for(int i = (int)(x); i < (int)(y); ++i)
#define MP make_pair
#define fst first
#define snd second
typedef pair<int, int> pii;
const int maxn = 4e5 + 5;
int n, top = 0;
int a[maxn], ord[maxn], rev[maxn], stk[maxn];
char s[maxn];
inline bool cmp(const int &i, const int &j){ return a[i] < a[j]; }
int main(){
scanf("%d", &n);
FOR(i, 0, n << 1){
scanf("%d", a + i);
ord[i] = i;
}
sort(ord, ord + (n << 1), cmp);
FOR(i, 0, n << 1)
rev[ord[i]] = i;
FOR(i, 0, n << 1){
if(top && (rev[stk[top]] < n) != (rev[i] < n)){
s[stk[top]] = '(', s[i] = ')';
--top;
}
else
stk[++top] = i;
}
puts(s);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// For Policy Based DS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// ---------------------------------------
#define endl "\n"
#define ff first
#define ss second
#define int long long
#define pb emplace_back
#define mp make_pair
#define pll pair<int,int>
#define vll vector<int>
#define vpll vector<pair<int,int>>
#define all(c) (c).begin(), (c).end()
#define bs binary_search
#define pqb priority_queue<int>
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define mem(arr,x) memset(arr,x,sizeof(arr))
#define pi 3.14159265358979323846
#define inf 2000000000000000000
#define ps(x,y) fixed<<setprecision(y)<<x
#define test(x) int x; cin>>x;while(x--)
template<class A> void read(vector<A>& v);
template<class A, size_t S> void read(array<A, S>& a);
template<class T> void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d = stod(t);
}
void read(long double& d) {
string t;
read(t);
d = stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template<class A> void read(vector<A>& x) {
for (auto &a : x)
read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
for (auto &a : x)
read(a);
}
const int d4i[4] = { -1, 0, 1, 0};
const int d4j[4] = {0, 1, 0, -1};
const int d8i[8] = { -1, -1, 0, 1, 1, 1, 0, -1};
const int d8j[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void c_p_c()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
string int_to_string(int n) {
ostringstream str1;
str1 << n;
string sar = str1.str();
return sar;
}
int pow(int a, int b, int m)
{
int res = 1;
while (b > 0)
{
if (b % 2)
res = (res * a) % m;
a = (a * a) % m;
b >>= 1;
}
return res;
}
int ncr(int n, int k)
{ if (k == 0)
return 1;
if (k > n - k)
k = n - k;
int a = 1, b = 1, d;
while (k)
{ a *= n; b *= k;
d = __gcd(a, b);
a /= d; b /= d;
n--; k--;
}
return a;
}
int modinv(int k, int m)
{
return pow(k, m - 2, m);
}
/***************************CODE BEGINS**************************************/
const int N = 102;
double dp[102][102][102];
double cal_dp(int x, int y, int z)
{
if (x == 100 || y == 100 || z == 100) {
return dp[x][y][z] = 0.0;
}
if (dp[x][y][z] != -1.0) {
return dp[x][y][z];
}
int temp = x + y + z;
double t1 = ((double)x / (double)temp) * (cal_dp(x + 1, y, z) + 1.0);
double t2 = ((double)y / (double)temp) * (cal_dp(x, y + 1, z) + 1.0);
double t3 = ((double)z / (double)temp) * (cal_dp(x, y, z + 1) + 1.0);
return dp[x][y][z] = t1 + t2 + t3;
}
void solve ()
{
int a, b, c;
read(a, b, c);
for (int i = 0; i <= 101; i++) {
for (int j = 0; j <= 101; j++) {
for (int k = 0; k <= 101; k++) {
dp[i][j][k] = -1.0;
}
}
}
cout << ps(cal_dp(a, b, c), 8);
}
int32_t main()
{
c_p_c();
// test(x)
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
template <class T> vector<T> make_vec(size_t a, T val) {
return vector<T>(a, val);
}
template <class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
int to;
T cost;
edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 1e9 + 7;
double dfs(int a, int b, int c, vector<vector<vector<double>>> &dp) {
if(dp[a][b][c] != 0)
return dp[a][b][c];
if(a == 100 || b == 100 || c == 100)
return 0;
double ret = 0;
ret += (a / (double)(a + b + c)) * (dfs(a + 1, b, c, dp) + 1.0);
ret += (b / (double)(a + b + c)) * (dfs(a, b + 1, c, dp) + 1.0);
ret += (c / (double)(a + b + c)) * (dfs(a, b, c + 1, dp) + 1.0);
return dp[a][b][c] = ret;
}
int main() {
ll A, B, C;
cin >> A >> B >> C;
auto dp = make_vec(101, 101, 101, 0.0);
cout << fixed << setprecision(10) << dfs(A, B, C, dp) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#include<cstdlib>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
typedef long long ll;
int main(){
int n;
cin >> n;
vector<int> p(n);
rep(i,n) cin >> p.at(i);
int ans=0;
vector<int> check(200005,0);
rep(i,n){
check.at(p.at(i))=1;
if(ans!=p.at(i)) cout << ans << endl;
else{
while(check.at(ans)==1) ans++;
cout << ans << endl;
}
}
} | #include "bits/stdc++.h"
#define pb push_back
#define ll long long
#define ii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define vi vector<int>
#define vii vector<ii>
#define vvi vector<vi>
#define vs vector<string>
#define vl vector<ll>
#define vll vector<vl>
#define all(x) x.begin(), x.end()
#define nl endl
#define rev(x) reverse(all(x))
#define ull unsigned ll
#define vpl vector<pll>
using namespace std;
//ull find(ull x){
// return (x * (x + 1)) / 2;
//}
//vector<int> findCharacters(string s){
// vector<int> a(26);
// for(char el : s)
// a[el - 'a']++;
// return a;
//}
vl read(vl a) {
for (auto &el : a)
cin >> el;
return a;
}
//int req(int l, int r){
// cout << "? " << l + 1 << " " << r + 1 << endl;
// int val;
// cin >> val;
// return val;
//}
void solve() {
string s;
cin >> s;
bool sw = 1;
for(int i = 0; i < s.size(); i++){
char el = s[i];
if(i % 2){
if(el < 'A' || el > 'Z'){
sw = 0;
break;
}
} else {
if(el < 'a' || el > 'z'){
sw = 0;
break;
}
}
}
cout << (sw ? "Yes" : "No") << endl;
}
int main() {
// int t;
// cin >> t;
// while (t--)
solve();
} |
#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
/*
struct Edge
{
int to;
int weight;
Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
*/
using Graph = vector<vector<int>>;
const long long INF = 1LL << 60;
const int INT_INF = 1000000000;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
string S;
cin >> S;
int cnt = 0;
int xcnt = 0;
int scnt = 0;
for (int i = 0; i < S.size(); i++)
{
if (S[i] == 'o')
cnt++;
else if (S[i] == 'x')
xcnt++;
else
scnt++;
}
if (cnt > 4 || xcnt == 10)
{
cout << 0 << endl;
return 0;
}
if (cnt == 4)
{
cout << 4 * 3 * 2 << endl;
return 0;
}
if (cnt == 3)
{
cout << 36 + scnt * 4 * 3 * 2 << endl;
return 0;
}
if (cnt == 2)
{
cout << 14 + scnt * 3 * 4 * 3 * 2 / 2 + scnt * (scnt - 1) / 2 * 4 * 3 * 2 << endl;
return 0;
}
if (cnt == 1)
{
cout << 1 + scnt * 14 + scnt * (scnt - 1) / 2 * 36 + scnt * (scnt - 1) * (scnt - 2) / 6 * 4 * 3 * 2 << endl;
return 0;
}
if (cnt == 0)
{
cout << scnt + scnt * (scnt - 1) / 2 * 14 + scnt * (scnt - 1) * (scnt - 2) / 6 * 36 + scnt * (scnt - 1) * (scnt - 2) * (scnt - 3) / 24 * 4 * 3 * 2 << endl;
return 0;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pi pair<int, int>
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define FIO ios_base::sync_with_stdio(0), cin.tie(0)
#define fi first
#define se second
int fact(int n) {
if (n <= 1) return 1;
if (n == 2) return 2;
if (n == 3) return 6;
if (n == 4) return 24;
return 0;
}
set<vector<int>> num;
void get(vector<int>& a, vector<int>& c, int i) {
if (i == 4) {
vector<int> x = a;
sort(all(x));
num.insert(x);
return;
}
for (auto x : c) {
a.pb(x);
get(a, c, i + 1);
a.pop_back();
}
}
signed main() {
FIO;
string s;
cin >> s;
vector<int> a;
set<int> v;
for (int i = 0; i < 10; i++) {
if (s[i] == 'o') {
a.pb(i);
v.insert(i);
} else if (s[i] == '?') {
v.insert(i);
}
}
if (a.size() > 4) {
cout << "0";
return 0;
}
if (a.size() == 4) {
cout << "24";
return 0;
}
if (v.size() == 0) {
cout << "0";
return 0;
}
vector<int> c;
for (auto it : v) c.pb(it);
int i = a.size();
get(a, c, i);
int ans = 0;
for (auto it : num) {
unordered_map<int, int> freq;
for (auto y : it) {
freq[y]++;
}
int f = 24;
for (auto p : freq) f /= fact(p.se);
ans += f;
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int64_t> A(N);
for (int i = 0; i < N; i++){
cin >> A.at(i);
}
int64_t max = 0;
for (int i = 0; i < N; i++){
int64_t counter = 1;
for (int j = i - 1; j >= 0; j--){
if(A.at(j) >= A.at(i)){
counter++;
}else{
break;
}
}
for (int j = i + 1; j < N; j++){
if(A.at(j) >= A.at(i)){
counter++;
}else{
break;
}
}
if(max < counter * A.at(i)){
max = counter * A.at(i);
}
}
cout << max << endl;
} | #include<iostream>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
int N;
cin >> N;
int as[N];
rep(i,N) cin >> as[i];
int ma=0;
rep(l,N){
int mi=101000;
for(int r=l;r<N;r++){
mi = min(mi, as[r]);
ma = max(ma, mi*(r-l+1));
}
}
cout << ma << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
#define MAX 510000
#define rrep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep(i, n) for(ll i = 1; i <= (ll)(n); i++)
#define dcout cout<<fixed<<setprecision(15);
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
constexpr int MOD = 1e9 + 7;
constexpr ll inf = 1LL << 60;
template< typename S, typename T >
inline void chmax(S &a, const T &b) { if(a < b) a = b; }
template< typename S, typename T >
inline void chmin(S &a, const T &b) { if(a > b) a = b; }
ll gcd(ll x, ll y) { if (x == 0) return y; return gcd(y%x, x);}
ll lcm(ll x, ll y) { return x * y / gcd(x, y); }
ll modpow(ll a, ll n, ll p) { if(n==0) return (ll)1; if (n == 1) return a % p; if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p; ll t = modpow(a, n / 2, p); return (t * t) % p;}
ll modinv(ll a, ll m) { if(m==0)return (ll)1; ll b = m, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u;}
ll 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;}
ll com(ll n,ll m){ if(n<m || n<=0 ||m<0){ return 0; } if( m==0 || n==m){ return 1; } ll k=1; for(ll i=1;i<=m;i++){ k*=(n-i+1); k%=MOD; k*=modinv(i,MOD); k%=MOD; } return k;}
ll rad(ll u, ll p){ ll cnt=0; while(u%p==0){ u/=p; cnt++; } return cnt;}
////////////////////////////////////////////////////////////////////
int main() {
ios::sync_with_stdio(false);
ll n;
cin>>n;
ll a[2*n+1];
rep(i,2*n){
cin>>a[i];
}
vector <pair<ll,ll>> v;
rep(i,2*n){
v.pb(mp(a[i],i));
}
ll bw[2*n+1];
rep(i,2*n)
{bw[i]=0;}
sort(ALL(v));
for(int i=0;i<=n-1;i++){
bw[v[i].second]=1;
}
ll ans[2*n+1];
stack<ll> s;
ll sz=1;
for(int i=1;i<=2*n;i++){
if(i==1){s.push(bw[i]); ans[i]=1;}
else if(i==2*n){ans[i]=2;}
else{
ll t=s.top();
if(sz==0){s.push(bw[i]); ans[i]=1; sz++; continue;}
if(t!=bw[i]){ans[i]=2;s.pop(); sz--;}
if(t==bw[i]){ans[i]=1;s.push(bw[i]); sz++;}
}
}
for(int i=1;i<=2*n;i++){
if(ans[i]==1){cout<<'(';}
else cout<<')';
}
cout<<endl;
return 0;
}
| #include <iostream>
#include <functional>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <climits>
#include <cmath>
#include <cstring>
#include <cassert>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define pb push_back
#define fst first
#define snd second
#ifdef QLEG_DEBUG
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << '{'; string sep; for (const auto& x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
template<class Fun>
class _y_combinator_result {
Fun fun_;
public:
template<class T>
explicit _y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return _y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
template<typename T> using min_queue=priority_queue<T,vector<T>,greater<T>>;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int N; cin >> N;
vector<ll> v(2*N);
for (auto& x : v) cin >> x;
vector<pll> w;
for (int i = 0; i < 2*N; i++) w.pb({v[i], i});
sort(w.begin(), w.end());
for (int i = 0; i < 2*N; i++) {
auto [_, j] = w[i];
v[j] = i < N;
}
vector<int> st;
for (int k : v) {
if (!st.empty() && k != st.back()) {
cout << ')';
st.pop_back();
} else {
cout << '(';
st.pb(k);
}
}
cout << '\n';
}
|
//Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) {virtual,upsolve}
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
new_data_set;
typedef long long ll;
#define rep(a,n) for(int i = a;i<n;i++)
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y);
#define F first
#define S second
#define all(x) x.begin(),x.end()
const int mod = 1e9+7;
const int mxN = 1+100*1000;
inline int lcm(int a,int b)
{
return (a*b)/(__gcd(a,b));
}
signed main()
{
int n;
cin>>n;
vector<ll> a(n),b(n),c(n);
for(int i = 0;i<n;i++) cin>>a[i];
for(int i = 0;i<n;i++) cin>>b[i];
set<ll,greater<ll>> l;
for(int i = 0;i<n;i++)
{
l.insert(a[i]);
if(i==0)
{
c[i] = (*l.begin())*b[i];
}
else {
c[i] = max(c[i-1],b[i]*(*l.begin()));
}
}
for(auto i : c) cout<<i<<endl;
}
| #include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <complex>
#include <vector>
#include <limits>
#include <iomanip>
#include <cassert>
#include <numeric>
#include <chrono>
#include <random>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define debug(x) cerr << #x << " = " << (x) << endl;
#define debug_pii(x) cerr << "(" << x.first << "," << x.second << ")";
#define rep(i, n) for(int i = 0;i < n;i++)
#define pb push_back
#define F first
#define S second
const long double pi = 3.141592653589793;
const int mod = 1e9 + 7;
int gcd(int x, int y) {
return (y == 0 ? x : gcd(y, x % y));
}
int lcm(int x, int y) {
return (x*y)/gcd(x, y);
}
const int nmax = 2e5 + 10;
int n,m,u,v,w;
vector<pii> g[nmax];
ll dist[nmax][2][2];
const ll inf = 1e16;
struct state {
int node;
bool skip;
bool add2;
state(int node, bool skip, bool add2) {
this->node=node;
this->skip=skip;
this->add2=add2;
}
};
bool operator<(state const& lhs, state const& rhs) {
return tie(lhs.node, lhs.skip, lhs.add2) < tie(rhs.node, rhs.skip, rhs.add2);
}
void solve() {
int n;
cin >> n;
vector<ll> a(n), b(n), c(n);
rep(i,n)cin>>a[i];
rep(i,n)cin>>b[i];
ll up = a[0];
ll down = b[0];
c[0] = up*down;
ll maxm = up;
for(int i = 1;i < n;i++) {
c[i] = c[i-1];
maxm = max(maxm, a[i]);
if(b[i]*maxm > c[i]) {
c[i] = b[i]*maxm;
}
}
rep(i,n)cout<<c[i]<<endl;
}
// 10 -10
// 22 -22
// 11 -11
//
// 55 -55
// 19 -19
// k = 4 n = 6
// 1 2 3 |4| 3 2 ---> a
// 1 2 3 4 5 6 7 8 |9| 8 7 6
// p1 p2 p3 p4 p3 p2 --> b (inversion cnt b <= a, b is lex lexi.. maxm)
// 4 3 2 |1| 2 3
int main() {
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
// cout << fixed << setprecision(15);2000000000
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// prime_factors();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
|
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~**\
* Bismillahir Rahmanir Rahim. *
* Imtiaz_rafi *
* PCIU, CHITTAGONG *
\**~~~~~~~~~~~~~~~~~~~~~~~~~~~**/
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll>vi;
typedef map<ll,ll>mi;
typedef pair<ll,ll>pll;
typedef vector<pll> vll;
typedef set<ll>st;
typedef set<char>sc;
ll dx[] = {-1,+1,0,0,+1,-1,+1,-1};
ll dy[] = {0,0,-1,+1,+1,+1,-1,-1};
const ll mx = 1e5+123;
const ll MOD = 1e9+7;
const ll INF = LLONG_MAX;
#define speed() ios::sync_with_stdio(0);cin.tie(0);
#define file() freopen ("input.txt", "r", stdin);freopen ("output.txt", "w", stdout);
#define sl(a) scanf("%lld",&a)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define rep(i,b,e) for(__typeof(e) i=(b)-(b>e);i!=(e)-(b>e);i+=1-2*(b>e))
#define trace(x) cerr << #x << " = " << x << endl
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define pi acos(-1)
#define en '\n' //(1LL<<(3)) *2^3
template <class T> T power(T a,T b){a%=MOD;if(!a) return 0;T p=1;while(b>0){if(b&1){p*=a;p%=MOD;}a*=a;a%=MOD;b=b>>1;}return p;}
template <class T> void print(vector<T> &v){for(T u:v){cout<<u<<" ";}cout<<endl;}
template <class T> T gcd(T a, T b){return (b!=0?gcd<T>(b,a%b):a);}
template <class T> T lcm(T a, T b){return (a/gcd<T>(a,b)*b);}
int main()
{
speed();
ll t,a,i,b,c,ans=0,j,m = INT_MAX;
cin>>a>>b;
ll x[a][b];
rep(i,0,a)
{
rep(j,0,b)
{
cin>>x[i][j];
m = min(x[i][j],m);
}
}
rep(i,0,a)
{
rep(j,0,b)
{
ans+= (x[i][j]-m);
}
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using Pl = pair<ll, ll>;
int main() {
ll N, P; cin >> N >> P;
map<ll, ll> dat;
for (int i = 0; i < N; i++) {
ll A, B, C; cin >> A >> B >> C;
dat[A] += C;
dat[B+1] -= C;
}
ll sum = 0, p = 0;
ll ans = 0;
for (auto &v : dat) {
ll now = v.first;
if (sum > P) ans += (now-p)*P;
else ans += (now-p)*sum;
sum += v.second;
p = now;
}
cout << ans << endl;
}
|
// Jai Shree Ram
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define ll long long
#define int long long
#define pb push_back
#define all(v) v.begin(),v.end()
#define endl "\n"
#define x first
#define y second
#define gcd(a,b) __gcd(a,b)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define hell 1000000007
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.x>>a.y;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.x<<" "<<a.y;return out;}
template<typename T,typename T1>T maxs(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T mins(T &a,T1 b){if(b<a)a=b;return a;}
int solve(){
int n,m; cin >> n >> m;
vector<vector<pii>>vec(n+1);
rep(i,0,m){
int x,y,z; cin >> x >> y >> z;
y--;
vec[x].push_back({y,z});
}
vector<vector<int>>dp(n+1,vector<int>(1 << n));
dp[0][0] = 1;
for(int i = 1; i <= n; i++){
sort(all(vec[i]));
for(int mask = 0; mask < (1 << n); mask++){
int z = __builtin_popcount(mask);
if(z != i){
continue;
}
vector<int>t;
for(int j = 0; j < n; j++){
if((1<<j)&mask)t.push_back(j);
}
int j = 0;
bool bad = 0;
for(auto [y,z]:vec[i]){
while(j < t.size() and t[j] <= y){
j++;
}
if(j > z)bad=1;
}
if(bad)continue;
for(int j = 0; j < n; j++){
if(mask&(1<<j)){
dp[i][mask] += dp[i-1][mask^(1 << j)];
}
}
}
}
cout << dp[n][(1<<n)-1] << endl;
return 0;
}
signed main(){
ios_base::sync_with_stdio(0);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 <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
template <typename T>
ostream &operator<<(ostream &os, vector<T> &v) {
os << "[ ";
for (auto const &x : v) {
os << x;
if (&x != &v.back()) {
os << " : ";
}
}
os << " ]" << endl;
return os;
}
template <typename T>
ostream &operator<<(ostream &os, vector<vector<T>> &v) {
os << "[" << endl;
for (auto const &x : v) {
os << " ";
print_vec(x);
}
os << "]" << endl;
return os;
}
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
struct P {
ll x = 0;
ll y = 0;
P &operator+=(const P &p) {
x += p.x;
y += p.y;
return *this;
}
P &operator-=(const P &p) {
x -= p.x;
y -= p.y;
return *this;
}
P operator+(const P &p) const {
P _p = *this;
return _p += p;
}
P operator-(const P &p) const {
P _p = *this;
return _p -= p;
}
ll operator&(const P &p) const { return x * p.y - y * p.x; }
};
ostream &operator<<(ostream &os, P p) {
os << p.x << " " << p.y;
return os;
}
void rotate(P &p, ll n) {
if (n < 0) {
n = n + ((-n - 1) / 4 + 1) * 4;
}
ll x = p.x;
if (n == 1) {
p.x = p.y;
p.y = -x;
} else if (n == 2) {
p.x = -p.x;
p.y = -p.y;
} else if (n == 3) {
p.x = -p.y;
p.y = x;
}
}
void rotateX(P &p, ll x) { p.x = 2 * x - p.x; }
void rotateY(P &p, ll y) { p.y = 2 * y - p.y; }
void apply(P &p, P &vx, P &vy, P &o) {
ll r = 0;
if (vx.x == 0) {
if (vx.y == 1)
r = 3;
else
r = 1;
} else if (vx.x == -1) {
r = 2;
}
// cout << "apply " << vx << " " << r << endl;
rotate(p, r);
if ((vx & vy) < 0) {
if (vx.x == 0) {
rotateX(p, 0);
} else {
rotateY(p, 0);
}
}
p += o;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, Q, M, a, b;
cin >> N;
vector<P> plist(N);
P o, vx, vy, p;
vx.x = 1;
vy.y = 1;
REP(i, N) { cin >> plist[i].x >> plist[i].y; }
cin >> M;
vector<P> olist(M + 1);
vector<P> vxlist(M + 1);
vector<P> vylist(M + 1);
vxlist[0] = vx;
vylist[0] = vy;
REP(i, M) {
cin >> a;
if (a == 1 || a == 2) {
ll r = a == 1 ? 1 : -1;
rotate(o, r);
rotate(vx, r);
rotate(vy, r);
} else {
cin >> b;
if (a == 3) {
rotateX(o, b);
rotateX(vx, b);
rotateX(vy, b);
} else {
rotateY(o, b);
rotateY(vx, b);
rotateY(vy, b);
}
}
olist[i + 1] = o;
vxlist[i + 1] = vx - o;
vylist[i + 1] = vy - o;
// cout << (i + 1) << " o " << o << " vx " << (vx - o) << " vy " << (vy - o)
// << endl;
}
cin >> Q;
REP(i, Q) {
cin >> a >> b;
o = olist[a];
vx = vxlist[a];
vy = vylist[a];
p = plist[b - 1];
apply(p, vx, vy, o);
cout << p << endl;
}
} |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define INF 2147483647
#define LINF ((1LL << 62) - (1LL << 31))
#define rep(i, n) for (int i = 0; i < n; i = i + 1)
#define lrep(i, n) for (ll i = 0; i < n; i = i + 1)
#define array(N, t) (t*)calloc(N, sizeof(t))
#define max(p, q)((p) > (q) ? (p) : (q))
#define min(p, q)((p) < (q) ? (p) : (q))
#define swap(a, b) { int temp = a; a = b; b = temp; }
#define lswap(a, b) { ll temp = a; a = b; b = temp; }
#define cswap(a, b) { char temp = a; a = b; b = temp; }
void reverse(char * a, int n) {
int l = 0;
int r = n - 1;
while(l < r) {
cswap(a[l], a[r]);
l += 1;
r -= 1;
}
}
int main() {
char s[100010];
scanf("%s",s);
int l=strlen(s);
// printf("%d\n",l);
// puts(s);
reverse(s,l);
// puts(s);
rep(i,l){
// printf("%c\n",s[i]);
// if(s[i]=='0'){}
// if(s[i]=='1'){}
if(s[i]=='6'){
// printf("%c\n",s[i]);
s[i]='9';
// printf("%c\n",s[i]);
} else {
// if(s[i]=='8'){}
if(s[i]=='9'){
s[i]='6';
}
}
}
puts(s);
return 0;
} | #include<bits/stdc++.h>
#define S std
#define f(i, a, b) for(R int i = a; i <= b; i++)
#define R register
int T(int x)
{
if(x == 0||x == 1|| x == 8)return x;
else if(x == 6)return 9;
else if(x == 9)return 6;
}
int main()
{
S::string s;
S::cin >> s;
int l = s.length();
for(int i = l-1; i >= 0; i--)
{
S::cout<<T(s[i]-'0');
}
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++)
#define hmap gp_hash_table<ll, ll>
#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());
int main()
{
FAST/**/
ll n;
cin>>n;
ll arr[n];
rep(n)
cin>>arr[i];// mx = max(mx, arr[i]), sum += arr[i];
ll maxi[n], psum[n];
maxi[0] = psum[0] = arr[0];
for(ll i=1;i<n;i++)
psum[i] = psum[i-1] + arr[i]*(i+1), maxi[i] = max(maxi[i-1], arr[i]);
ll sm = 0;
rep(n)
{
sm += arr[i];
ll ans = (i+1)*(maxi[i]+sm-arr[i]) + sm;
if(i>0)
ans -= psum[i-1];
else ans = 2*arr[0];
cout<<ans<<"\n";
}
return 0;
}
| #include <bits/stdc++.h>
//#include <atcoder/modint>
//using namespace atcoder;
//#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<61);
template<class T> bool chmin(T &a, const T b){
if(a > b) {a = b; return true;}
else return false;
}
template<class T> bool chmax(T &a, const T b){
if(a < b) {a = b; return true;}
else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
if(!v.empty()){
for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
std::cout<<v.back();
}
if(endline) std::cout<<std::endl;
}
void no(){
cout<<"UNSOLVABLE"<<endl;
exit(0);
}
unordered_map<char, int> num;
ll conv(string &s){
ll res = 0;
if(num[s[0]] == 0) return 0;
for(char c : s) (res *= 10) += num[c];
return res;
}
void judge(vector<string> &s){
vec tmp(3);
rep(i, 3) tmp[i] = conv(s[i]);
if(tmp[0] + tmp[1] == tmp[2] && *min_element(ALL(tmp)) > 0){
rep(i, 3) cout<<tmp[i]<<endl;
exit(0);
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
vector<string> s(3);
set<char> cs;
rep(i, 3){
cin>>s[i];
for(char c : s[i]) cs.insert(c);
}
if(cs.size() > 10){
no();
}else{
vec ord(10);
iota(ALL(ord), 0);
do{
int i = 0;
for(char c : cs) num[c] = ord[i++];
judge(s);
}while(next_permutation(ALL(ord)));
}
no();
} |
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int nax = 102;
ll dp[nax][nax * nax * nax];
int mod;
int main()
{
int n,k; cin>>n>>k; cin>>mod;
dp[0][0] = 1;
for(int i=1;i<=n;i++)
{
for(int c=0;c<nax * nax * nax / 2;c++)
{
if(c < i)
{
dp[i][c] = dp[i - 1][c];
}
else
{
dp[i][c] = dp[i][c - i];
dp[i][c] += dp[i - 1][c];
int taken = (c + i) / i;
if(taken > k + 1) dp[i][c] -= dp[i - 1][c - i * (k + 1)];
dp[i][c] += mod;
dp[i][c] %= mod;
}
}
}
for(int x=1;x<=n;x++)
{
ll ways = k + 1;
int ans = 0;
for(int j=1;j<nax * nax * nax / 2;j++)
{
int v1 = x - 1;
int v2 = n - x;
ll cur = ways * dp[v1][j];
cur %= mod;
cur *= dp[v2][j];
cur %= mod;
ans += cur;
if(ans >= mod) ans -= mod;
}
ans += k;
cout<<ans<<"\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
const ll INF = 1LL<<60;//1152921504606846976
const int mod = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
struct unionfind{
vector<int> par,rank;
unionfind(int x){
par.resize(x);
rank.resize(x);
for(int i=0; i<x; i++)par[i]=-1,rank[i]=0;
}
int root(int x){
if(par[x]<0)return x;
return par[x]=root(par[x]);
}
void uni(int x,int y){
x=root(x),y=root(y);
if(x==y)return;
if(rank[x]<rank[y]){par[y]+=par[x];par[x]=y;}
else{
par[x]+=par[y];par[y]=x;
if(rank[x]==rank[y])rank[x]++;
}
}
bool same(int x,int y){
return root(x)==root(y);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll n; cin>>n;
unionfind uf(n);
rep(i,n){
ll f;cin>>f;
uf.uni(i, f-1);
}
set<ll> m;
rep(i,n)m.insert(uf.root(i));
cout<<(mint(2).pow(m.size())-1).x<<endl;
return 0;
} |
#include <bits/stdc++.h>
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define PER(i,a,b) for (int i = (b); i >= (a); --i)
#define log2(x) (31-__builtin_clz(x))
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
#define mup(x,y) x = std::min(x,y)
#define Mup(x,y) x = max(x,y)
#define debug(x) cout << #x << " is " << x << endl
#define endl '\n'
using namespace std; using ll = long long; using ii = pair<int,int>; using iii = tuple<int,int,int>;
void solution(); int main() {ios::sync_with_stdio(0); cin.tie(0); solution();}
const int N = 200003;
int n;
vector<ll> r, g, b;
ll calc(vector<ll>& x, vector<ll>& y) {
ll a = 0, b = 0, r = 10000000000000000;
while (a < SZ(x) && b < SZ(y)) {
if (x[a] < y[b]) mup(r, y[b]-x[a++]);
else if (x[a] > y[b]) mup(r, x[a]-y[b++]);
else return 0;
}
return r;
}
void input() {
cin >> n;
REP(i,1,2*n) {
ll a; char c;
cin >> a >> c;
if (c == 'R') r.push_back(a);
else if (c == 'G') g.push_back(a);
else b.push_back(a);
}
}
void solution() {
input();
sort(ALL(r)); sort(ALL(g)); sort(ALL(b));
bool pr = SZ(r)&1, pg = SZ(g)&1, pb = SZ(g)&1;
if (pr == 0 && pg == 0 && pb == 0) {cout << 0; return;}
auto x = calc(r,g), y = calc(r,b), z = calc(g,b);
if (SZ(r)%2 == 0) swap(x,z);
else if (SZ(g)%2 == 0) swap(x,y);
// cout << x << ' ' << y << ' ' << z << endl;
cout << min(x,y+z);
} | #include<bits/stdc++.h>
#define ll long long
using namespace std;
ll MX=100000000000000000LL;
ll ck(vector<ll> a, vector<ll> b)
{
ll mn=MX;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for(int i=0;i<a.size();i++)
{
auto it=upper_bound(b.begin(), b.end(), a[i]);
if(it!=b.end())
{
mn=min(mn, abs(*it-a[i]));
}
if(it!=b.begin())
{
it=prev(it);
mn=min(mn, abs(*it-a[i]));
}
}
return mn;
}
ll ck1(vector<ll> a, vector<ll> b, vector<ll> c)
{
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
vector<ll> dl(a.size()), dr(a.size());
for(int i=0;i<a.size();i++)
{
ll x=MX;
auto it=upper_bound(b.begin(), b.end(), a[i]);
if(it!=b.end())
{
x=min(x, abs(*it-a[i]));
}
if(it!=b.begin())
{
it=prev(it);
x=min(x, abs(*it-a[i]));
}
if(i>0) dl[i]=min(x, dl[i-1]);
else dl[i]=x;
}
for(int i=a.size()-1;i>=0;i--)
{
ll x=MX;
auto it=upper_bound(b.begin(), b.end(), a[i]);
if(it!=b.end())
{
x=min(x, abs(*it-a[i]));
}
if(it!=b.begin())
{
it=prev(it);
x=min(x, abs(*it-a[i]));
}
if(i!=a.size()-1) dr[i]=min(x, dr[i+1]);
else dr[i]=x;
}
ll mn=MX;
for(int i=0;i<a.size();i++)
{
ll x=MX;
auto it=upper_bound(c.begin(), c.end(), a[i]);
if(it!=c.end())
{
x=min(x, abs(*it-a[i]));
}
if(it!=c.begin())
{
it=prev(it);
x=min(x, abs(*it-a[i]));
}
ll y=MX;
if(i>0) y=min(y, dl[i-1]);
if(i+1<a.size()) y=min(y, dr[i+1]);
mn=min(mn, x+y);
}
return mn;
}
int main()
{
ll n;
cin>>n;
map<char, vector<ll> > mp;
for(int i=1;i<=2*n;i++)
{
ll x;
char c;
cin>>x>>c;
mp[c].push_back(x);
}
if(mp['B'].size()%2==0 and mp['R'].size()%2==0 and mp['G'].size()%2==0)
{
cout<<0<<endl;
return 0;
}
if(mp['B'].size()%2==0)
{
ll x=ck(mp['R'], mp['G']);
if(mp['B'].size()>=2)
x=min(x, ck1(mp['B'], mp['R'], mp['G']));
cout<<x<<endl;
return 0;
}
if(mp['R'].size()%2==0)
{
ll x=ck(mp['B'], mp['G']);
if(mp['R'].size()>=2)
{
x=min(x, ck1( mp['R'], mp['B'], mp['G'] ));
}
cout<<x<<endl;
return 0;
}
if(mp['G'].size()%2==0)
{
ll x=ck(mp['R'], mp['B']);
if(mp['G'].size()>=2)
{
x=min(x, ck1(mp['G'], mp['R'], mp['B']));
}
cout<<x<<endl;
return 0;
}
return 0;
} |
//Codeforcesで128bit整数を使いたいとき
//→__int128_tを使う&GNU C++17 (64)で提出する
//インクルードなど
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//イテレーション
#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--)
#define FORA(i,I) for(const auto& i:I)
//x:コンテナ
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007 //問題による
//略記
#define F first
#define S second
//出力(空白区切りで昇順に)
#define coutALL(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<" ";cout<<*--x.end()<<endl;
//aをbで割る時の繰上げ,繰り下げ
ll myceil(ll a,ll b){return (a+(b-1))/b;}
ll myfloor(ll a,ll b){return a/b;}
#define PI 3.141592653589793
signed main(){
double N,x_min,x_max,y_min,y_max;
cin >> N >> x_min >> y_min >> x_max >> y_max;
double x_center = (x_min+x_max)/2;
double y_center = (y_min+y_max)/2;
double x_1,y_1;
double coses = cos(2/N*PI);
double sines = sin(2/N*PI);
double x_0 = x_min - x_center;
double y_0 = y_min - y_center;
x_1 = coses*x_0 - sines*y_0 + x_center;
y_1 = coses*y_0 + sines*x_0 + y_center;
cout << fixed << setprecision(10) << x_1 << " " << y_1 << endl;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx=1e6+5;
//考虑括号序列的匹配问题
inline int read() {
int x=0,f=1; char c=getchar();
while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(x<<1)+(x<<3)+c-'0';c=getchar();}
return x*f;
}
struct node{
int x,id;
bool operator <(const node &a) {
return x<a.x;
}
}a[mx];
int n,b[mx],s[mx],cnt;
ll res;
char c[mx];
int main() {
// freopen("data.in","r",stdin);
// freopen("ans.out","w",stdout);
n=read(); n*=2;
for(int i=1;i<=n;i++) a[i].x=read(),a[i].id=i;
sort(a+1,a+1+n);
for(int i=n/2+1;i<=n;i++) b[a[i].id]=1;
for(int i=1;i<=n;i++) {
if(b[i]) {
if(cnt&&!b[s[cnt]]) c[s[cnt]]='(',c[i]=')',cnt--;
else s[++cnt]=i;
}
else {
if(cnt&&b[s[cnt]]) c[s[cnt]]='(',c[i]=')',cnt--;
else s[++cnt]=i;
}
}
// for(int i=1;i<=n/2;i++) {
// int x=a[i].id,y=a[n-i+1].id;
// res+=a[n-i+1].x-a[i].x;
// printf("%d %d\n",a[n-i+1].x,a[i].x);
// if(x<y) c[x]='(',c[y]=')';
// else c[x]=')',c[y]='(';
// }
// printf("%d",res);
for(int i=1;i<=n;i++) printf("%c",c[i]);
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
struct dijkstra {
ll N;
struct edge {
ll to, C, D;
ll f(ll x) { return C+D/(x+1); }
ll cost(ll d) {
ll sqd = sqrt(D);
ll result = d+f(d);
repr(i,sqd-2,sqd+3) if (i>=d) result = min(result,i+f(i));
return result;
}
};
vector<vector<edge>> adj;
vector<ll> minlen;
dijkstra(ll n) { N = n; adj.resize(N); minlen.assign(N,LLINF); }
ll operator[](const ll &x) const { return minlen[x]; }
void add_edge(ll from, ll to, ll C, ll D){
adj[from].push_back({to, C, D});
}
vector<ll> calc(ll n=0) {
minlen.assign(N,LLINF);
auto c = [](const p_ll &x, const p_ll &y){return x.second>y.second;};
priority_queue<p_ll, vector<p_ll>, decltype(c)> q(c);
minlen[n] = 0; q.push(make_pair(n,0));
while(q.size()) {
p_ll now = q.top(); q.pop();
ll np = now.first, nd = now.second;
if (nd>minlen[np]) continue;
for (auto x: adj[np]) {
if (minlen[x.to]<=x.cost(minlen[np])) continue;
q.push(make_pair(x.to, x.cost(minlen[np])));
minlen[x.to] = x.cost(minlen[np]);
}
}
return minlen;
}
};
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
int main() {
ll N, M; cin >> N >> M;
ll A[M], B[M], C[M], D[M]; rep(i,M) { cin >> A[i] >> B[i] >> C[i] >> D[i]; A[i]--; B[i]--; }
dijkstra ds(N);
rep(i,M) {
ds.add_edge(A[i],B[i],C[i],D[i]);
ds.add_edge(B[i],A[i],C[i],D[i]);
}
vector<ll> d = ds.calc();
// debug(all(d));
ll result = d[N-1]!=LLINF ? d[N-1] : -1;
cout << result << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())
int main() {
ll t, n;
cin >> t >> n;
set<ll> st;
FOR(r, 1, 100 + 1) {
ll p = (100 + t) * r / 100;
st.insert(p);
}
ll maxi = *st.rbegin();
vector<ll> a;
FOR(x, 1, maxi + 1) {
if (st.find(x) == st.end()) {
a.push_back(x);
}
}
ll s = SZ(a), l = 100 + t;
n--;
ll ans = a[n % s] + l * (n / s);
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define FLASH ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define all(v) ((v).begin()), ((v).end())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) // rep(i, 1, 5) --> {1,2,3,4} // rep(i, 5, 1) --> {4,3,2,1} // rep(it, end(v), begin(v)) --> *it
using namespace std;
signed main()
{
int n;
cin >> n;
vector<pair<double, double>> v(n);
rep(i, 0, n)
cin >> v[i].first >> v[i].second;
int res = 0;
rep(i, 0, n-1)
rep(j, i+1, n)
{
if (v[i].first == v[j].first)
continue;
double s = (v[i].second - v[j].second) / (v[i].first - v[j].first);
if (s <= 1 && s >= -1)
res++;
}
cout << res;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;
vector<ll> fac;
void c_fac(ll x=pow(10,6)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }
int main() {
ll T; cin >> T;
rep(_,T) {
ll N; cin >> N;
ll a[N]; rep(i,N) cin >> a[i];
map<ll, ll> m; rep(i,N) m[a[i]]++;
string result = "Second";
if (N%2==0) {
for (auto x: m) {
if (x.second%2==1) result = "First";
}
}
cout << result << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int A, B;
cin >> A;
int i = 1;
B = 0;
while (B < A) {
B = B + i;
i++;
}
cout << i - 1;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define pob pop_back
#define eb emplace_back
#define ins insert
#define mp make_pair
#define ff first
#define ss second
#define pf push_front
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define sz(a) (ll)a.size()
#define dec(x) fixed<<setprecision(x)
#define lcm(a,b) (a*b/__gcd(a,b))
typedef priority_queue<ll> pq;
typedef priority_queue<ll,vector<ll>,greater<ll> > pqmn;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vector<ll> > vvll;
typedef vector<string> vstr;
typedef set<ll> sll;
typedef set<pll> spll;
typedef map<ll,ll> mll;
const ll inf=LLONG_MAX;
const ll mod=1e9+7;
const ld pi=acos(-1);
const ll N=100001;
void solution()
{
int n,w;
cin>>n>>w;
cout<<n/w;
}
int main()
{
//freopen("t","r",stdin);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
t=1;
//cin>>t;
while(t--){
solution();
cout<<"\n";
}
return 0;
}
|
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <bits/stdc++.h>
#include <vector>
#include <array>
#include <tuple>
#include <algorithm>
using namespace std;
int main() {
int N;
cin >> N;
int x[N], y[N];
int a, b, p;
p=0;
for (int i=0; i<N; i++){
cin >> a >> b;
x[i]=a;
y[i]=b;
}
for (int i=0; i<N-2; i++){
for (int j=i+1; j<N-1; j++){
for (int k=j+1; k<N; k++){
if ((y[j]-y[i])*(x[k]-x[i])==(y[k]-y[i])*(x[j]-x[i])){
p=1;
}
}
}
}
if (p==1){
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
} |
#include <bits/stdc++.h>
#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
#define ll long long
using namespace std;
const int INF = 1e9+7;
/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll N;
cin >> N;
vector<pair<int, int>> A(N);
loop(0, N, i) {
ll a, b;
cin >> a >> b;
A[i] = make_pair(a, b);
}
loop(0, N, i) {
loop(i+1, N, j) {
loop(j+1, N, k) {
double dx = A[j].first - A[i].first;
double dy = A[j].second - A[i].second;
double dx2 = A[k].first - A[i].first;
double dy2 = A[k].second - A[i].second;
if (dx * dy2 == dx2 * dy) {
print("Yes");
return 0;
}
}
}
}
print("No");
}
|
#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);
}
}
map<ll,ll> MAP;
void func(ll num,ll base){
auto at = MAP.find(base);
if(at == MAP.end()){
MAP[base] = num;
}else{
ll tmp = MAP[base];
MAP[base] = gcd(tmp,num);
}
}
int main(){
int N;
scanf("%d",&N);
ll A;
ll mini = HUGE_NUM;
for(int i = 0; i < N; i++){
scanf("%lld",&A);
mini = min(mini,A);
for(ll k = 1; k*k <= A; k++){
if(A%k != 0)continue;
func(A,k);
func(A,A/k);
}
}
int ans = 1;
for(auto at = MAP.begin(); at != MAP.end(); at++){
if(at->first < mini && at->first == at->second){
ans++;
}
}
printf("%d\n",ans);
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i=0; i<n; ++i)
#define all(obj) (obj).begin(),(obj).end()
using namespace std;
typedef long long ll;
long long GCD(long long x, long long y) { return y ? GCD(y, x%y) : x; }
int main(){
ll n;
cin >> n;
if(n<=10){
cout << 0 << endl;
return 0;
}
ll n2 = n;
ll dig = log10(n);
n /= pow(10, dig/2+1);
if(dig%2==0){
cout << pow(10, dig/2)-1 << endl;
return 0;
}
if(n*pow(10, dig/2+1)+n<=n2){
cout << n << endl;
}
else{
cout << n-1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
//const int mod= int(1e9)+7;
const int mod=998244353;
using P = pair<int,int>;
using Pl= pair<ll,ll>;
using ld=long double;
using V=vector<int>;
using Vl=vector<ll>;
using VV=vector<vector<int>>;
using VVl=vector<vector<ll>>;
// modint: mod 計算を int を扱うように扱える構造体
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<mod>;
const int MAX = 1001001;
long long 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;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
ll nPr(int n, int r) {
if (n < 0 || r < 0 || n < r) return 0;
return fac[n] * finv[n - r] % mod;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
COMinit();
int n,k;cin >>n>>k;
vector<vector<mint>>a(n,vector<mint>(k+1,1));
for (int i = 0; i < n; ++i) {
int c;cin >>c;
a[i][1]=c;
}
for (int i = 0; i < n; ++i) {
auto r=a[i][1];
for (int j = 2; j <=k; ++j) {
a[i][j]=a[i][j-1]*r;
}
}
vector<mint>sum(k+1,0);
for (int i = 0; i < n; ++i) {
for (int j = 0; j <=k; ++j) {
sum[j]+=a[i][j];
}
}
for (int x = 1; x <=k; ++x) {
mint ans=0;
for (int i = 0; i <=x; ++i) {
ans+=(sum[i]*sum[x-i]-sum[x])*COM(x,i);
}
ans/=2;
cout <<ans<<"\n";
}
} | #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<string,int> psi;
typedef pair<int,string> pis;
typedef array<int,2> aii;
typedef array<int,3> aiii;
typedef array<int,4> aiiii;
typedef unsigned long long ull;
typedef long long int ll;
typedef array<ll,2> all;
typedef array<ll,3> alll;
typedef array<ll,4> allll;
#define pb push_back
#define ff first
#define ss second
#define MAX 100005
#define MOD 1000000007
#define INF 1e9+100
vector<alll>adj[MAX];
int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };
int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };
int dp[101][101],n,K,arr[101];
ll x;
int main()
{
//freopen("input.in","r",stdin);
//freopen("output.txt","w",stdout);
cin.tie(0),cout.tie(0);
ios_base::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(10);
int TC=1;
int k,m,q;
//cin>>TC;
for(int t1=1; t1<=TC; t1++)
{
cin>>n>>x;
for(int i=1; i<=n; i++)
cin>>arr[i];
ll ans=2e18;
for(int k=1; k<=n; k++)
{
memset(dp,-1,sizeof dp);
dp[0][0]=0;
for(int i=1; i<=n; i++)
{
for(int j=k-1; j>=0; j--)
{
for(int m=0; m<k; m++)
{
if(dp[j][m]!=-1)
dp[j+1][(m+arr[i])%k]=max(dp[j+1][(m+arr[i])%k],dp[j][m]+arr[i]);
}
}
}
ll cur=x%k;
if(dp[k][cur]>0)
ans=min(ans,(x-dp[k][cur])/k);
}
cout<<ans<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios_base::sync_with_stdio(0);cin.tie(0)
/* start */
signed main(){
IOS;
int a, b;
cin >> a >> b;
for(int i = -1000; i <= 1000; i++) {
for(int j = -1000; j <= 1000; j++) {
if(i + j == a and i - j == b) {
cout << i << " " << j << "\n";
return 0;
}
}
}
} | #include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int x, y;
scanf("%d%d", &x, &y);
int a = (x+y)/2;
int b = x-a;
printf("%d %d\n", a, b);
return 0;
} |
// Author: Harshdeep Sharma , IIT Indore
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define lll __int128
// #define mp std::make_pair
#define pii std::pair<int,int>
#define pll std::pair<ll,ll>
#define pli std::pair<ll,int>
#define pil std::pair<int,ll>
#define mtp std::make_tuple
const double PI = atan(1.0) * 4;
const int di[4] = { -1, 0, 1, 0} ;
const int dj[4] = {0, -1, 0, 1} ;
const int maximum = numeric_limits<int>::max();
const int minimum = numeric_limits<int>::min();
const int mod = 1e9 + 7 ;
const int INF = 1e9 ;
ll gcd (ll a, ll b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
// function<void(int, int)> dfs = [&](int u , int p) {
// };
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T ;
// cin >> T ;
T = 1 ;
while (T--) {
int n , m ;
cin >> n >> m ;
int mn = 105 ;
vector<vector<int>> a(n , vector<int>(m)) ;
for (int i = 0 ; i < n ; i++) {
for (int j = 0 ; j < m ; j++) {
cin >> a[i][j] ;
mn = min(mn , a[i][j]) ;
}
}
// cout << mn << "\n" ;
int ans = 0 ;
for (int i = 0 ; i < n; i++) {
for (int j = 0 ; j < m ; j++) {
ans += (a[i][j] - mn) ;
}
}
cout << ans << "\n" ;
}
return 0 ;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
int ans;
signed main()
{
cin>>n>>k;
for(int i=2;i<=2*n;i++)
{
int b=i-k;
if(b>=2&&b<=2*n)
{
int l=0;
if(i>n)
{
int cha=i-n;
l=(n-cha+1);
}
else
{
l=i-1;
}
// if(l%2==0)l--;
// l*=2;
// if(i%2==0)l--;
int r=0;
if(b>n)
{
int cha=b-n;
r=(n-cha+1);
// if(r%2==0)
}
else
{
r=b-1;
}
// if(r%2==0)r--;
// r*=2;
// if(b%2==0)r--;
ans+=l*r;
// printf("%lld %lld\n",l,r);
}
}
cout<<ans;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T,class U> using P = pair<T,U>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
ll M = 998244353;
vector<ll> fac(300001); //n!(mod M)
vector<ll> ifac(300001); //k!^{M-2} (mod M)
//a,bの範囲的にこれだけ配列を用意していけば十分
ll mpow(ll x, ll n){ //x^n(mod M) ←普通にpow(x,n)では溢れてしまうため,随時mod計算
ll ans = 1;
while(n != 0){
if(n&1) ans = ans*x % M;
x = x*x % M;
n = n >> 1;
}
return ans;
}
ll comb(ll a, ll b){ //aCbをmod計算
if(a == 0 && b == 0)return 1;
if(a < b || a < 0)return 0;
ll tmp = ifac[a-b]* ifac[b] % M;
return tmp * fac[a] % M;
}
int main(){
fac[0] = 1;
ifac[0] = 1;
for(ll i = 0; i<300000; i++){
fac[i+1] = fac[i]*(i+1) % M; // n!(mod M)
ifac[i+1] = ifac[i]*mpow(i+1, M-2) % M; // k!^{M-2} (mod M) ←累乗にmpowを採用
}
int n, m; cin >> n >> m;
// if (m % 2 != 0) {
// cout << 0 << endl;
// return 0;
// }
vvec<ll> dp(14, vec<ll>(m+1, 0));
vec<ll> pow2(15, 0);
pow2[0] = 1;
rep(i, 14) pow2[i+1] = pow2[i] * 2;
// dp[0][0] = 1;
rep(j, m + 1) if (j % 2 == 0) if (m >= j) if (n >= j) dp[0][j] = comb(n, j);
for(int i = 1; i < 14; i++) for(int j = 0; j <= m; j++) {
// if (i >= 2) break;
int jn = 0;
while(true) {
if (j - jn * pow2[i + 1] < 0) break;
if (n < jn * 2) break;
// sum = j - jn;
ll add = 0;
add = dp[i-1][j - jn * pow2[i + 1]] * comb(n, jn*2);
add %= M;
// if ((i == 1) && (j == 20)) cout << jn << " " << add << endl;
dp[i][j] += add;
dp[i][j] %= M;
// cout << i << " " << j << " " << jn << " " << add << endl;
jn++;
}
}
// rep(i, 14) {
// rep(j, m + 1) cout << dp[i][j] << " ";
// cout << endl;
// }
// cout << endl;
cout << dp[13][m] << endl;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
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;}
template<class T> void print(const T &t) {cout << t << "\n";}
const ll INF = 1LL << 62;
const int iINF = 1 << 30;
int n;
ll a;
char c;
using vi = vector<int>;
ll check(vl&x, vl&y){
if(x.empty() or y.empty()) return INF/2;
ll res=INF;
fore(z,x){
auto tmp=lower_bound(all(y),z);
ll t1=*tmp,t2=INF;
if(tmp!=y.begin()) t2=*(--tmp);
chmin(res,min(abs(z-t1),abs(z-t2)));
}
return res;
}
int main() {
fio(); cin>>n;
vl R,G,B;
rep(i,2*n) {
cin>>a>>c;
if(c=='R') R.emplace_back(a);
if(c=='G') G.emplace_back(a);
if(c=='B') B.emplace_back(a);
}
sort(all(R));sort(all(G));sort(all(B));
if(sz(R)%2==0 and sz(G)%2==0 and sz(B)%2==0) print(0);
else{
if(sz(G)%2==0) swap(R,G);
else if(sz(B)%2==0) swap(R,B);
print(min(check(G,B),check(B,R)+check(R,G)));
}
}
|
#include<bits/stdc++.h>
using namespace std;
#define vec vector<int>
#define vecp vector<pair<int,int>>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fr first
#define sc second
#define fr1(i,a,b) for(int i=a;i<b;i++)
#define fr2(i,a,b) for(int i=a;i>=b;i--)
#define fr3(i,a,b) for(int i=a;i<=b;i++)
#define umap unordered_map<int,int,custom_hash>
#define omap map<int,int>
#define uset unordered_set<int,custom_hash>
#define oset set<int>
#define pr pair<int,int>
#define mod 1000000007
#define mp make_pair
#define all(v) v.begin(),v.end()
#define ppb pop_back
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);
}
};
void read(vec &v)
{
for(auto &i:v)cin>>i;
}
void solve()
{
string s;
cin>>s;
string ans="";
bool flip=true;
for(char i:s)
{
if(i=='R')flip^=1;
else
{
if(flip)
{
if(ans.empty())
{
ans.pb(i);
}
else if(ans.back()==i)
{
ans.ppb();
}
else
ans.pb(i);
}
else
{
if(ans.empty())
{
string u="";
u+=i;
ans.insert(0,u);
}
else if(ans.front()==i)
{
ans.erase(0,1);
}
else
{
string u="";
u+=i;
ans.insert(0,u);
}
}
}
}
if(!flip)reverse(all(ans));
cout<<ans<<"\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
//cin>>t;
fr3(i,1,t){
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} | // atcoder/zone2021/D/main.cpp
// author: @___Johniel
// github: https://github.com/johniel/
#include <bits/stdc++.h>
#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))
using namespace std;
template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << "," << p.second << ")"; return os; }
template<typename P, typename Q> istream& operator >> (istream& is, pair<P, Q>& p) { is >> p.first >> p.second; return is; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; for (auto& i: v) os << i << ","; os << ")"; return os; }
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, set<T> s) { os << "#{"; for (auto& i: s) os << i << ","; os << "}"; return os; }
template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; for (auto& i: m) os << i << ","; os << "}"; return os; }
template<typename T> inline T setmax(T& a, T b) { return a = std::max(a, b); }
template<typename T> inline T setmin(T& a, T b) { return a = std::min(a, b); }
using lli = long long int;
using ull = unsigned long long;
using point = complex<double>;
using str = string;
template<typename T> using vec = vector<T>;
constexpr array<int, 8> di({0, 1, -1, 0, 1, -1, 1, -1});
constexpr array<int, 8> dj({1, 0, 0, -1, 1, -1, -1, 1});
constexpr lli mod = 1e9 + 7;
int main(int argc, char *argv[])
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.setf(ios_base::fixed);
cout.precision(15);
str s;
while (cin >> s) {
deque<char> q;
bool f = true;
each (c, s) {
if (c == 'R') f = !f;
else {
if (f) q.push_back(c);
else q.push_front(c);
}
}
str t;
if (f) {
while (q.size()) {
t += q.front();
q.pop_front();
}
} else {
while (q.size()) {
t += q.back();
q.pop_back();
}
}
str u;
each (c, t) {
if (u.size() && u.back() == c) u.pop_back();
else u += c;
}
cout << u << endl;
}
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < (long long int)(n); i++)
#define irep(i, n) for (long long int i = 1; i <= (long long int)(n); i++)
#define drep(i, n, diff) for (long long int i = 0; i < (long long int)(n); i += diff)
#define mod 1000000007
#define cdeb(val) cout << #val << ":" << val << endl;
#define pdeb(val) printf("%s : %lld\n", #val, val);
typedef long long int ll;
typedef pair<ll, ll> P;
struct MATRIX {
vector<vector<ll>> a;
vector<ll> b;
MATRIX(const vector<vector<ll>> & _a = {{1,0}, {0,1}},
const vector<ll>& _b = {0,0}) : a(_a), b(_b) {}
MATRIX operator*(const MATRIX& x) const {
MATRIX res({{0,0},{0,0}});
rep(i,2) {
rep(j,2) {
rep(k, 2) {
res.a[i][j] += x.a[i][k]*a[k][j];
}
}
}
res.b = MATRIX(x.a)*b;
rep(i,2) {
res.b[i] += x.b[i];
}
return res;
}
vector<ll> operator*(const vector<ll>& x) const {
vector<ll> res = b;
rep(i,2) {
rep(j,2) {
res[i] += a[i][j]*x[j];
}
}
return res;
}
};
int main(){
ll n;
cin >> n;
vector<vector<ll>> p(n, vector<ll>(2));
rep(i,n) {
cin >> p[i][0] >> p[i][1];
}
ll m;
cin >> m;
vector<MATRIX> d(1);
rep(i,m) {
ll op;
cin >> op;
MATRIX x;
if (op == 1) {
x = MATRIX({{0,1},{-1,0}});
} else if (op == 2) {
x = MATRIX({{0,-1},{1,0}});
} else {
ll p;
cin >> p;
if (op == 3) {
x = MATRIX({{-1,0},{0,1}}, {2*p,0});
} else {
x = MATRIX({{1,0},{0,-1}}, {0,2*p});
}
}
MATRIX y = d.back();
d.push_back(y*x);
}
ll q;
cin >> q;
rep(i,q) {
ll a, b;
cin >> a >> b;
b--;
vector<ll> ans = d[a]*p[b];
printf("%lld %lld\n", ans[0], ans[1]);
}
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
template<typename T>
using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;
const bool DEBUG = 0;
const int MAXN = 200005;
int n,m,Q;
ii a[MAXN];
vector<pair<ii,int>> q;
vector<ii> eve;
ii ans[MAXN];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n;
forn(i,0,n) cin>>a[i].F>>a[i].S;
cin>>m;
forn(i,0,m)
{
int t; cin>>t;
ll p=0;
if(t>=3) cin>>p;
eve.pb({t,p});
}
cin>>Q;
forn(i,0,Q)
{
int a,b; cin>>a>>b; a--; b--;
q.pb({{a,b},i});
}
sort(q.begin(), q.end(), greater<pair<ii,int>>());
bool swapped=0;
bool sign[2]{};
ll off[2]{};
while(q.back().F.F==-1)
{
int p=q.back().F.S, id=q.back().S;
q.pop_back();
ans[id]=a[p];
}
forn(i,0,m)
{
int t=eve[i].F; ll p=eve[i].S;
if(t==1)
{
sign[swapped]^=1;
off[swapped]*=-1;
swapped^=1;
}
if(t==2)
{
sign[swapped^1]^=1;
off[swapped^1]*=-1;
swapped^=1;
}
if(t==3)
{
sign[swapped]^=1;
off[swapped]*=-1;
off[swapped]+=2*p;
}
if(t==4)
{
sign[swapped^1]^=1;
off[swapped^1]*=-1;
off[swapped^1]+=2*p;
}
while(q.size() && q.back().F.F==i)
{
int p=q.back().F.S; int id=q.back().S;
q.pop_back();
ii tmp;
tmp.F=(sign[0]?-1:1)*a[p].F+off[0];
tmp.S=(sign[1]?-1:1)*a[p].S+off[1];
if(swapped) swap(tmp.F, tmp.S);
ans[id]=tmp;
}
}
forn(i,0,Q)
{
cout<<ans[i].F<<" "<<ans[i].S<<'\n';
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
static const ll mod=1000000007;
ll H,W;
char a[2005][2005];
ll dp[2005][2005];
ll sumx[2005][2005];
ll sumy[2005][2005];
ll sumxy[2005][2005];
int main(){
cin>>H>>W;
for(ll i=0;i<H;i++){
string s;cin>>s;
for(ll j=0;j<W;j++)
a[i][j]=s[j];
}dp[1][1]=1;sumx[1][1]=1;sumy[1][1]=1;sumxy[1][1]=1;
for(ll i=1;i<=H;i++)
for(ll j=1;j<=W;j++)
if(a[i-1][j-1]!='#'){
dp[i][j]=(dp[i][j]+sumx[i-1][j])%mod;
dp[i][j]=(dp[i][j]+sumy[i][j-1])%mod;
dp[i][j]=(dp[i][j]+sumxy[i-1][j-1])%mod;
sumx[i][j]=(sumx[i-1][j]+dp[i][j])%mod;
sumy[i][j]=(sumy[i][j-1]+dp[i][j])%mod;
sumxy[i][j]=(sumxy[i-1][j-1]+dp[i][j])%mod;
}cout<<dp[H][W]<<endl;
return 0;
} | #include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long ll;
ll n, m;
bool v[1005][1005];
ll cp[2005];
bool v1[2005], v2[2005];
ll cnt1, cnt2;
ll fnd(ll p)
{
if(cp[p] == p) return p;
return cp[p] = fnd(cp[p]);
}
void uni(ll p, ll q)
{
if(fnd(p) == fnd(q)) return;
cp[fnd(p)] = fnd(q);
}
int main()
{
scanf("%lld%lld", &n, &m);
for(ll i = 0; i < n + m; i++) cp[i] = i;
for(ll i = 0; i < n; i++)
{
string s;
cin>>s;
for(ll j = 0; j < m; j++)
{
if(s[j] == '#') v[i][j] = true, uni(i, j + n);
}
}
uni(0, n - 1);
uni(0, n);
uni(0, m + n - 1);
for(ll i = 0; i < n; i++)
{
if(!v1[fnd(i)]) cnt1++, v1[fnd(i)] = true;
}
for(ll i = 0; i < m; i++)
{
if(!v2[fnd(i + n)]) cnt2++, v2[fnd(i + n)] = true;
}
printf("%lld\n", min(cnt1 - 1, cnt2 - 1));
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,tmp=0;
cin>>n;
ll tm=log10(n);
tm/=2;
tmp=pow(10,tm-1)-1;
ll ans=tmp;
tmp+=1;
//cout<<ans<<" "<<tmp<<"\n";
for(ll i=tmp;i<=tmp*100;i++)
{
ll y=(log10(i)+1);
ll mul=pow(10,y);
ll k=i*mul;
k+=i;
//cout<<k<<" ";
if(k<=n)
ans++;
else
break;
}
cout<<ans;
} |
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
void solve(){
}
int main()
{
fast_cin();
ll n,sum=0;
string s;
int max,fh,sh;
cin >> n;
s = to_string(n);
int len = s.length();
if(len == 1) {
cout << 0;
}
else if(len % 2 == 1) {
for(int i=1; i<=len/2; i++) {
cout << 9;
}
}
else {
fh = stoi(s.substr(0,len/2));
sh = stoi(s.substr(len/2));
if(fh <= sh) {
cout << fh;
}
else {
cout << fh-1;
}
}
} |
#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> P;
#define PI 3.141592653589793
//#define MOD 1000000007
#define MOD 998244353
#define ALL(obj) (obj).begin(),(obj).end()
const ll INF = 1LL << 60;
const int inf = 1 << 30;
//四方向への移動ベクトル
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
struct edge{//グラフに使うヤツ
ll from,to,cost;
};
typedef vector<vector<edge> > G;
ll gcd(ll a,ll b){
if (a%b==0)return(b);
else return(gcd(b,a%b));
}
int main(){
ll n,ans=0,sc=0;
cin >> n;
string s,t;
cin >> s >> t;
queue<ll> que;
queue<ll> que2;
for (ll i=0;i<n;i++){
if (t[i]=='1')que.push(i);
if (s[i]=='1'){
if (!que2.empty()){
ans+=i-que2.front();
que2.pop();
}else if (que.empty()){
que2.push(i);
}else{
ans+=i-que.front();
que.pop();
}
}
}
if (que.empty() and que2.empty()){
cout << ans << endl;
}else{
cout << -1 << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD int(1e9+7)
#define INF int(1e9+7)
#define LINF ll(1e18+7)
#define PI acos(-1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll,ll>
#define chmax(x,y) (x = max(x,y))
#define chmin(x,y) (x = min(x,y))
vector<int> dp[210000];
int main(){
int n;
string s;
string x;
cin>>n>>s>>x;
dp[n].push_back(0);
for(int i=n-1; i>=0; i--){
if(x[i]=='A'){
rep(r,7){
bool a=0, b=0;
for(auto p : dp[i+1]){
if(10*r%7==p) a=1;
if((10*r+s[i]-'0')%7==p) b=1;
}
if(a && b) dp[i].push_back(r);
}
}else{
rep(r,7){
for(auto p : dp[i+1]){
if(10*r%7==p || (10*r+s[i]-'0')%7==p){
dp[i].push_back(r);
break;
}
}
}
}
}
for(auto x : dp[0]){
if(x==0){
cout<<"Takahashi"<<endl;
return 0;
}
}
cout<<"Aoki"<<endl;
} |
#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);
int N, M;
cin >> N >> M;
vector<int> A(N);
for (int i = 0; i < N; i++) cin >> A.at(i);
vector<vector<int>> G(N);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b, a--, b--;
G.at(a).push_back(b);
}
vector<int> DP(N, INT_MIN);
auto dfs = [&](auto&& dfs, int now, int pre, int mi) -> void {
cmin(mi, A.at(now));
for (const auto& next : G.at(now)) {
if (next == pre) continue;
if (cmax(DP.at(next), A.at(next) - mi)) dfs(dfs, next, now, mi);
}
};
for (int i = 0; i < N; i++) {
dfs(dfs, i, -1, INT_MAX);
}
cout << *max_element(DP.begin(), DP.end()) << '\n';
} | //BISMILAHIRAHMANIR RAHIM
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define 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 allr(x) (x).rbegin(),(x).rend()
#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 loop(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
#define br break
#define con continue
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
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;
priority_queue<pii, vector<pii>, greater<pii> >q;
const int mxx = 2e5+5;
vector<vector<int>>adj(mxx);
vector<int>up(mxx,-1e18);
vector<int>v(mxx);
map<int,int>vis;
void dfs(int j){
vis[j]=1;
up[j]=max(up[j],v[j]);
for(int i:adj[j]){
if(vis[i]==0)dfs(i);
up[j]=max(up[i],up[j]);
}
}
void solv(){
int n,m,x,y;
cin>>n>>m;
loop(i,1,n+1)cin>>v[i];
loop(i,0,m){
cin>>x>>y;
adj[x].pb(y);
}
loop(i,1,n+1){
if(vis[i]==0)dfs(i);
}
int mx = -1e18;
loop(i,1,n+1){
for(int j:adj[i])mx = max(mx,up[j]-v[i]);
}
cout<<mx<<endl;
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
solv();
}
///ALHAMDULILLAH// |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;};
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;
int main(){
int n; cin >> n;
VI a(n), b(n), c(n), d(n);
REP(i,n) cin >> a[i] >> b[i], a[i]*=n, b[i]*=n;
REP(i,n) cin >> c[i] >> d[i], c[i]*=n, d[i]*=n;
if(n==1){
cout << "Yes" << endl;
return 0;
}
double csx=0, csy=0, ctx=0, cty=0;
REP(i,n){
csx+=a[i];
csy+=b[i];
ctx+=c[i];
cty+=d[i];
}
csx/=n, csy/=n, ctx/=n, cty/=n;
REP(i,n){
a[i]-=csx;
b[i]-=csy;
c[i]-=ctx;
d[i]-=cty;
}
if(a[0]==0&&b[0]==0){
swap(a[0],a[1]);
swap(b[0],b[1]);
}
REP(i,n){
if(c[i]==0&&d[i]==0)
continue;
double ang=atan2(d[i],c[i])-atan2(b[0],a[0]);
bool ok=1;
REP(j,n){
double x=a[j]*cos(ang)-b[j]*sin(ang);
double y=a[j]*sin(ang)+b[j]*cos(ang);
bool f=0;
REP(k,n){
if(abs(x-c[k])<=1e-6&&abs(y-d[k])<=1e-6)
f=1;
}
if(!f){
ok=0;
break;
}
}
if(ok){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
typedef std::pair<int, int> PII;
const int INF = 1e9;
const int MOD = 1e9+7;
int main(){
int k;
std::cin>>k;
int deck[10], ht[10]={}, ha[10]={};
for(int i=1;i<10;++i)deck[i]=k;
std::string s, t;
std::cin>>s>>t;
ll st, sa;
st=0;
sa=0;
for(int i=0;i<4;++i){
++ht[s[i]-48];
++ha[t[i]-48];
--deck[s[i]-48];
--deck[t[i]-48];
}
for(int i=1;i<10;++i){
st += i * std::pow(10, ht[i]);
sa += i * std::pow(10, ha[i]);
}
double ans = 0;
double total=9*k-8;
for(int i=1;i<10;++i){
if(deck[i]==0)continue;
for(int j=1;j<10;++j){
if(i==j){
if(deck[j]<=1)continue;
}
else{
if(deck[j]==0)continue;
}
if(st+i*std::pow(10, ht[i])*9>sa+j*std::pow(10, ha[j])*9){
if(i==j)ans+=deck[i]/total * (deck[i]-1)/(total-1);
else{
ans += deck[i]/total * (deck[j])/(total-1);
}
}
}
}
std::cout<<std::setprecision(8)<<ans<<"\n";
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
void testcase(){
int l, r;
cin>>l>>r;
int flag = 0;
ll count = 0;
ll inc = 2;
if(r-2*l+1>=1){
ll times = r-2*l+1;
count = (times*(times+1))/2;
}
cout<<count<<endl;
}
int main(){
ios_base::sync_with_stdio();
cin.tie(NULL);
int test;
cin>>test;
while(test-->0){
testcase();
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using Pll = pair<long long,long long>;
using vec = vector<int>;
using vecll = vector<long long>;
using mat = vector<vector<int>>;
using matll = vector<vector<long long>>;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define _GLIBCXX_DEBUG
constexpr int MOD = 1000000007;
const int INF = 1 << 30;
template<typename T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
int main(){
int n;
int max1=-1,max2=-1;
cin >> n;
vector<vector<int>> a,b;
rep(i,n){
int x1,y1;
cin>>x1>>y1;
a.push_back({x1,y1,i+1});
b.push_back({y1,x1,i+1});
}
sort(all(a));
sort(all(b));
set<int> num;
vec x,y;
rep(i,2){
if(!num.count(a[i][2])){
x.push_back(a[i][0]);
y.push_back(a[i][1]);
num.insert(a[i][2]);
}
if(!num.count(a[n-1-i][2])){
x.push_back(a[n-1-i][0]);
y.push_back(a[n-1-i][1]);
num.insert(a[n-1-i][2]);
}
}
rep(i,2){
if(!num.count(b[i][2])){
y.push_back(b[i][0]);
x.push_back(b[i][1]);
num.insert(b[i][2]);
}
if(!num.count(b[n-1-i][2])){
y.push_back(b[n-1-i][0]);
x.push_back(b[n-1-i][1]);
num.insert(b[n-1-i][2]);
}
}
n = x.size();
rep(i,n-1){
for ( int j = i+1; j<n;j++){
int dis = max(abs(x[j]-x[i]),abs(y[j]-y[i]));
if(dis > max1 ){
max2 =max1;
max1 = dis;
}
else if(dis > max2) max2 = dis;
}
}
cout<<max2<<endl;
} |
// Problem: A - Two Sequences 2
// Contest: AtCoder - KEYENCE Programming Contest 2021
// URL: https://atcoder.jp/contests/keyence2021/tasks/keyence2021_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define int long long
#define F(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end()
#define show(A) for (auto i: A) cout << i << " "; cout << '\n';
#define show_(A, n) F(i, n) { show(A[i]) }
using namespace std;
using ld = long double;
using vi = vector < int > ;
using mi = map < int, int > ;
using pi = pair < int, int > ;
const int N = 100005;
const int MOD = 1e9 + 7;
const int inf = 1e18 + 1;
/***************************code begins here*****************************/
void solve() {
int n, ans = 0;
cin >> n;
vi a(n);
F(i, n) cin >> a[i];
show(a);
cout << ans << '\n';
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vi x(n),y(n);
F(i,n) cin>>x[i];
F(i,n) cin>>y[i];
set<int> a,b; int ans=-1;
for(int i=0;i<n;i++){
a.insert(-x[i]);
ans = max(ans ,(-y[i]) * (*a.begin()) );
cout<< ans <<'\n' ;
}
return 0;
} | #include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include<algorithm>
#include<string>
#include <map>
#include <queue>
#include <stack>
#include<set>
//#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;
int main() {
int N; cin >> N;
vector<ll> a(N), b(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
for (int i = 0; i < N; i++) {
cin >> b[i];
}
ll ma = a[0], mb = b[0];
vector<ll> c(N);
c[0] = a[0] * b[0];
for (int i = 1; i < N; i++) {
ma = max(ma, a[i]);
//c[i] = max(c[i - 1], max(ma * b[i], mb * a[i]));
c[i] = max(c[i - 1], ma * b[i]);
mb = max(mb, b[i]);
}
for (int i = 0; i < N; i++) {
cout << c[i] << endl;
}
}
|
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
#define pq(T) priority_queue<T,vector<T>,greater<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
ll gcd(ll a, ll b) {
if (a%b == 0) {
return (b);
} else {
return(gcd(b, a%b));
}
}
int main() {
int n;
cin >> n;
unordered_map<int, int> m;
int x = 1000000000;
rep(i,0,n) {
int a;
cin >> a;
x = min(x, a);
for (int i = 1; i*i <= a; ++i) {
if (a % i != 0) continue;
if (!m.count(i)) {
m[i] = a;
} else {
m[i] = gcd(m[i], a);
}
if (a/i == i) continue;
if (!m.count(a/i)) {
m[a/i] = a;
} else {
m[a/i] = gcd(m[a/i], a);
}
}
}
int ans = 0;
for (auto itr = m.begin(); itr != m.end(); ++itr) {
if (itr->fi == itr->se && itr->fi <= x) {
++ans;
}
}
cout << ans << endl;
return 0;
}
| #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 n = 0;
cin >> n;
vector<int> a(n);
for (auto &i : a)
cin >> i;
int mi = *min_element(a.begin(), a.end());
unordered_map<int, int> mp;
for (auto &i : a)
{
for (int j = 1; j * j <= i && j < mi; ++j)
{
if (i % j == 0)
{
mp[j] = __gcd(i, mp[j]);
if (i / j < mi)
mp[i / j] = __gcd(i, mp[i / j]);
}
}
}
int res = 1;
for (auto &i : mp)
if (i.fi == i.se)
++res;
cout << res;
} |
#include <bits/stdc++.h>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
class rep {
struct Iter {
usize itr;
constexpr Iter(const usize pos) noexcept: itr(pos) { }
constexpr void operator ++ () noexcept { ++itr; }
constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; }
constexpr usize operator * () const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr rep(const usize first, const usize last) noexcept: first(first), last(std::max(first, last)) { }
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
template <class T>
using Vec = std::vector<T>;
void B_main() {
usize N;
std::cin >> N;
Vec<Vec<i32>> C(N, Vec<i32>(N));
for (auto &v: C) {
for (auto &x: v) {
std::cin >> x;
}
}
Vec<i32> Adif(N), Bdif(N);
for (auto i: rep(1, N)) {
Adif[i] = C[i][0] - C[0][0];
for (auto j: rep(1, N)) {
if (C[i][j] - C[0][j] != Adif[i]) {
std::cout << "No\n";
return;
}
}
}
for (auto j: rep(1, N)) {
Bdif[j] = C[0][j] - C[0][0];
for (auto i: rep(1, N)) {
if (C[i][j] - C[i][0] != Bdif[j]) {
std::cout << "No\n";
return;
}
}
}
const auto Amin = -*std::min_element(Adif.begin(), Adif.end());
const auto Bmin = -*std::min_element(Bdif.begin(), Bdif.end());
if (C[0][0] < Amin + Bmin) {
std::cout << "No\n";
return;
}
Vec<i32> A(N), B(N);
A[0] = Amin;
B[0] = C[0][0] - Amin;
for (auto i: rep(1, N)) {
A[i] = A[0] + Adif[i];
B[i] = B[0] + Bdif[i];
}
std::cout << "Yes\n";
for (auto i: rep(0, N)) {
std::cout << A[i] << " \n"[i + 1 == N];
}
for (auto i: rep(0, N)) {
std::cout << B[i] << " \n"[i + 1 == N];
}
return;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
B_main();
return 0;
}
| #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<set>
#define int long long
#define lowbit(x) (x&(-x))
#define mp(x,y) make_pair(x,y)
#define lc (x<<1)
#define rc (x<<1|1)
#define fi first
#define se second
#define mid ((l+r)>>1)
#define fan(x) (((x-1)^1)+1)
#define max Max
#define min Min
#define abs Abs
using namespace std;
inline int read()
{
int ans=0,f=1;
char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){ans=(ans<<1)+(ans<<3)+c-'0';c=getchar();}
return ans*f;
}
inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x/10) write(x/10);
putchar((char)(x%10)+'0');
}
template<typename T>inline T Abs(T a){return a>0?a:-a;};
template<typename T,typename TT>inline T Min(T a,TT b){return a>b?b:a;}
template<typename T,typename TT> inline T Max(T a,TT b){return a>b?a:b;}
const int N=505;
int n,a[N],b[N],c[N][N],fl=1;
signed main()
{
n=read();
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
c[i][j]=read();
for(int i=2;i<=n;++i)
for(int j=2;j<=n;++j)
if(c[i][j]-c[i][j-1]!=c[i-1][j]-c[i-1][j-1])
fl=0;
for(int j=2;j<=n;++j)
for(int i=2;i<=n;++i)
if(c[i][j]-c[i-1][j]!=c[i][j-1]-c[i-1][j-1])
fl=0;
if(!fl)
{
printf("No\n");
return 0;
}
int mn=1e18;
for(int j=1;j<=n;++j)
mn=min(mn,c[1][j]);
for(int j=1;j<=n;++j)
b[j]=c[1][j]-mn;
for(int i=1;i<=n;++i)
a[i]=c[i][1]-b[1];
for(int i=1;i<=n;++i)
{
if(a[i]<0) fl=0;
if(b[i]<0) fl=0;
}
if(!fl)
{
printf("No\n");
return 0;
}
printf("Yes\n");
for(int i=1;i<=n;++i)
printf("%lld ",a[i]);
printf("\n");
for(int i=1;i<=n;++i)
printf("%lld ",b[i]);
return 0;
} |
#include<bits/stdc++.h>
using namespace std ;
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define MAXN 200007
int n , k ;
vector < int > v[ MAXN ] ;
int depth[ MAXN ] ;
int lst[ MAXN ] ;
bool used[ MAXN ] ;
int root ;
vector < int > ord ;
void dfs ( int vertex , int prv ) {
for ( auto x : v[ vertex ] ) {
if ( x == prv ) { continue ; }
depth[ x ] = depth[ vertex ] + 1 ;
lst[ x ] = vertex ;
dfs ( x , vertex ) ;
}
}
void mark ( int vertex , int prv , int lft ) {
used[ vertex ] = true ;
if ( lft == 0 ) { return ; }
for ( auto x : v[ vertex ] ) {
if ( x == prv ) { continue ; }
mark ( x , vertex , lft - 1 ) ;
}
}
bool f ( int sr ) {
for ( int i = 1 ; i <= n ; ++ i ) {
used[ i ] = false ;
}
int cnt = 0 ;
for ( auto x : ord ) {
if ( used[ x ] == true ) { continue ; }
++ cnt ;
if ( cnt > k ) { return false ; }
int h = x ;
for ( int i = 0 ; i < sr ; ++ i ) { h = lst[ h ] ; }
mark ( h , -1 , sr ) ;
}
return true ;
}
void input ( ) {
scanf ( "%d%d" , &n , &k ) ;
for ( int i = 1 ; i < n ; ++ i ) {
int x , y ; scanf ( "%d%d" , &x , &y ) ;
v[ x ].push_back ( y ) ;
v[ y ].push_back ( x ) ;
}
}
vector < pair < int , int > > srt ;
void solve ( ) {
dfs ( 1 , -1 ) ;
int mx = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
if ( mx < depth[ i ] ) {
root = i ;
mx = depth[ i ] ;
}
}
depth[ root ] = 0 ;
lst[ root ] = root ;
dfs ( root , -1 ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
srt.push_back ( { -depth[ i ] , i } ) ;
}
sort ( srt.begin ( ) , srt.end ( ) ) ;
for ( auto [ val , wh ] : srt ) {
ord.push_back ( wh ) ;
}
int l , r , mid ;
l = 0 ;
r = n ;
while ( r - l > 3 ) {
mid = ( l + r ) / 2 ;
if ( f ( mid ) == true ) { r = mid ; }
else { l = mid ; }
}
while ( f ( l ) == false ) { ++ l ; }
printf ( "%d\n" , l ) ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t ;
// cin >> t ;
t = 1 ;
// scanf ( "%d" , &t ) ;
while ( t -- ) {
input ( ) ;
solve ( ) ;
}
return 0 ;
}
| #include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define input_output freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
template <typename Arg1>
void prn(Arg1&& arg1) { cout<<arg1<<"\n";}
template <typename Arg1, typename... Args>
void prn(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prn(args...); }
template <typename Arg1>
void prs(Arg1&& arg1) { cout<<arg1<<" ";}
template <typename Arg1, typename... Args>
void prs(Arg1&& arg1, Args&&... args) { cout<<arg1<<" "; prs(args...); }
template <typename Arg1>
void read(Arg1&& arg1) { cin>>arg1; }
template <typename Arg1, typename... Args>
void read(Arg1&& arg1, Args&&... args) { cin>>arg1; read(args...); }
#define int long long
#define all(ds) ds.begin(), ds.end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define gcd(x,y) __gcd(x,y)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define precise(x) cout<<fixed<<setprecision(x)
#define endl "\n"
const long long INF = 1e18;
const int32_t MOD = 1e9+7;
const int N = 2e5+5;
int n, m, k, q, r, l, x, y, z;
int a[N], b[N], c[N];
string s, t;
int ans ;
unordered_map<int, int>dp;
int recur(int y){
if(dp.find(y) != dp.end())return dp[y];
dp[y] = INF;
int &ans = dp[y];
ans = abs(y-x);
if(y == 1){
return ans;
}
if(y%2 == 0){
ans = min(ans, 1 + recur(y/2));
}
else{
ans = min(ans, 1 + min(recur(y-1), recur(y+1)));
}
return ans;
}
void solve(){
read(x,y);
// ans = abs(x-y);
prn(recur(y));
}
signed main()
{
#ifndef ONLINE_JUDGE
input_output
#else
fastio
#endif
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
int t = 1;
// cin>>t;
while(t--){
solve();
}
return 0;
} |
//QwQcOrZ yyds!!!
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#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 <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define ll __int128
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define R(i,a,b) for (int i=(a);i<(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define go(i,x) for (int i=head[x];i;i=e[i].nx)
#define mp make_pair
#define pb push_back
#define pa pair < int,int >
#define fi first
#define se second
#define re register
#define be begin()
#define en end()
#define sqr(x) ((x)*(x))
#define ret return puts("-1"),0;
#define put putchar('\n')
#define inf 1000000005
#define mod 998244353
#define int ll
#define N 1000005
using namespace std;
inline char gc(){static char buf[100000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}
#define gc getchar
inline ll read(){char c=gc();ll su=0,f=1;for (;c<'0'||c>'9';c=gc()) if (c=='-') f=-1;for (;c>='0'&&c<='9';c=gc()) su=su*10+c-'0';return su*f;}
inline void write(ll x){if (x<0){putchar('-');write(-x);return;}if (x>=10) write(x/10);putchar(x%10+'0');}
inline void writesp(ll x){write(x),putchar(' ');}
inline void writeln(ll x){write(x);putchar('\n');}
int n,f[N],t,a[N],ans;
signed main()
{
n=read();
if (n==1)
{
puts("1\n1");
return 0;
}
f[1]=1,f[2]=1;
for (int i=3;i;i++)
{
f[i]=f[i-1]+f[i-2];
if (f[i]>n)
{
t=i;
break;
}
}
t-=1;
for (int i=t;i>=1;i--)
{
a[i]+=(n/f[i]);
n%=f[i];
}
t=max(t,t^1);
for (int i=1;i<=t;i++)
{
ans+=a[i]+1;
}
writeln(ans);
for (int i=1;i<=t-i+1;i++)
{
swap(a[i],a[t-i+1]);
}
for (int i=1;i<=t;i++)
{
if (i%2) for (int j=1;j<=a[i];j++) writeln(1);
else for (int j=1;j<=a[i];j++) writeln(2);
if (i%2) writeln(3);
else writeln(4);
}
}
/*
1 0
1 0
1 1
2 1
3 4
7 0
*/
| #include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define INF (ll)1e18
#define mod 998244353
#define maxn 110
#define phi ((1 + sqrt(5)) / 2)
ll i, i1, j, k, k1, t, n, m, res, flag[10], a, b;
vector<ll> rs;
void solve(ll a, ll b) {
ll i;
if (rs.size() == 131) return;
if (a == 0) {
for (i = 1; i <= b; i++) rs.pb(2);
return;
}
if (b == 0) {
for (i = 1; i <= a; i++) rs.pb(1);
return;
}
if (a > b) {
rs.pb(3); solve(a - b, b);
} else {
rs.pb(4); solve(a, b - a);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif
cin >> n;
k = round(n / phi);
for (i = max(-k, (ll)-100);; i++) {
rs.clear();
solve(n, k + i);
if (rs.size() <= 130) break;
}
reverse(rs.begin(), rs.end());
cout << rs.size() << nl;
for (auto u : rs) cout << u << nl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep1(i, n) for(int i = 1;i < n+1; i++)
#define all(A) A.begin(),A.end()
typedef long long ll;
int main(){
int n,m;
cin >> n >> m;
vector<ll> a(n+1);
priority_queue<pair<ll,int>> p;
rep1(i,n){
cin >> a[i];
p.push(make_pair(-a[i],i));
}
vector<vector<int>> to(n+1);
rep(i,m){
int x,y;
cin >> x >> y;
to[x].push_back(y);
}
vector<ll> dp(n+1,1e18);
vector<bool> seen(n+1,0);
while(p.size()>0){
int start = p.top().second;
p.pop();
queue<int> q;
q.push(start);
while(q.size()>0){
int now = q.front();
q.pop();
if(seen[now]) continue;
seen[now] = 1;
for(int next : to[now]){
dp[next] = min(dp[next],dp[now]);
dp[next] = min(dp[next],a[now]);
q.push(next);
}//next
}//q
}//p
ll ans = -1e18;
rep1(i,n){
ans = max(ans,a[i]-dp[i]);
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int _____asdfwqerqwdcasdfasd = 0;
#define debug cout << "debug output " << "number: " << ++_____asdfwqerqwdcasdfasd << endl;
#define int long long
#define inf 2e18
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int& x : a) {
cin >> x;
}
int ans = x;
for (int k = 1; k <= n; k++) {
vector<vector<int>> dp(k + 1, vector<int>(k, -1));
dp[0][0] = 0;
for (int x : a) {
for (int c = k - 1; c >= 0; c--) {
for (int r = 0; r < k; r++) {
if (dp[c][r] == -1) {
continue;
}
int nx = dp[c][r] + x;
dp[c + 1][nx % k] = max(dp[c + 1][nx % k], nx);
}
}
}
int st = dp[k][x % k];
if (st == -1) {
continue;
}
ans = min(ans, (x - st) / k);
}
cout << ans << "\n";
return 0;
}
|
/////home/mohammed/.config/sublime-text-3/Packages/User
/*input
*/
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <math.h>
#include <sstream>
#include <iterator>
#include <cstdlib>
#include <unordered_map>
#include <map>
#include <list>
#include <set>
using namespace std;
using bin = std::bitset<8>;
#define endl ("\n")
#define pi (3.141592653589)
#define mod 1000000007
#define int int64_t
#define float double
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min({a,b,c})
#define max3(a, b, c) max({a,b,c})
#define min4(a, b, c, d) min({a,b,c,d})
#define max4(a, b, c, d) max({a,b,c,d})
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define ld long double
#define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i];}
#define coutArray(a,n) for(int i = 0; i < n; i++){cout << a[i] << " ";};cout << endl;
#define input(type, n) type n; cin>>n;
struct debugger
{
template<typename T> debugger& operator , (const T& v)
{
cerr << v << " ";
return *this;
}
} dbg;
bool check_key(map<int, int> m, int key)
{
if (m.find(key) == m.end())
return false;
return true;
}
vector<int> SieveOfEratosthenes(int n)
{
bool prime[n + 1];
memset(prime, true, sizeof(prime));
vector<int> res;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int p = 2; p <= n; p++)
if (prime[p])
res.push_back(p);
//cout << p << " ";
return res;
}
// function to convert decimal to binary
bin decToBinary(int n)
{
// array to store binary number
int binaryNum[32];
// counter for binary array
int i = 0;
while (n > 0) {
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
string x = "";
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--) {
x += to_string(binaryNum[j]);
}
bin result{x};
return result;
}
// for example:
// bin result = decToBinary(2);
// bin result2 = decToBinary(10);
// bin z = result xor result2;
// cout << z;
// return 0;
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n != 0)
{
remainder = n % 10;
n /= 10;
decimalNumber += remainder * pow(2, i);
++i;
}
return decimalNumber;
}
int factorial(int n) {
long long res = 1;
for (int i = 1; i <= n; i++) {
res = ((res * i) % mod + mod) % mod ;
}
return res;
}
int32_t main()
{
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
input(int, num1); input(int, num2);
if (num2 % num1 == 0) {
cout << "Yes";
} else {
cout << "No";
}
} | #pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define mod 1000000007
#define pi 3.1415926535898
#define eps 1e-9
#define fast ios::sync_with_stdio(0); cin.tie(0);cout.tie(0)
#define vt vector
#define ar array
#define fs first
#define sc second
#define pb push_back
#define sp printf(" ")
#define nl '\n'
#define all(a) a.begin(),a.end()
#define unique(c) (c).resize(unique(all(c)) - (c).begin())
#define sz(x) (int)(x).size()
#define revsort(x) sort(all(x));reverse(all(x))
#define REP(i,start,end) for (int i = start; i <=end; i++)
#define RREP(i,end,start) for (int i=end; i>=start ;i--)
#define EACH(x, a) for (auto& x: a)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<LL> vll;
typedef vector<double> vd;
typedef vector<vector<LL> > matrix;
typedef vector<vector<int> > graph;
template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d=stod(t);
}
void read(long double& d) {
string t;
read(t);
d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template<class A> void read(vt<A>& x) {
EACH(a, x)
read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
EACH(a, x)
read(a);
}
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);
}
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);
}
};
int add (LL var1, LL var2) {
return ( (var1 % mod) + (var2 % mod) + mod) % mod;
}
int mul (LL var1, LL var2) {
return (var1*var2) % mod;
}
int powm (LL x, LL y) {
int ans = 1;
while (y) {
if (y & 1) {
ans = mul(ans, x);
}
x = mul(x, x);
y /= 2;
}
return ans;
}
int inv(LL x) {
return powm(x, mod-2);
}
LL INF=1e9;
void solve() {
int n;
read(n);
vi a(n);
read(a);
int x = *min_element(all(a));
map <int, int> mp;
REP (i, 0, n-1) {
for (LL j = 1; j*j <= a[i]; j++) {
if (a[i]%j) continue;
mp[j] = __gcd(a[i], mp[j]);
mp[a[i]/j] = __gcd(a[i], mp[a[i]/j]);
}
}
int ans = 0;
for (auto px : mp) {
if (px.fs > x) break;
if (px.fs == px.sc) ans++;
}
cout << ans << nl;
}
int main() {
fast;
solve();
}
|
#include <bits/stdc++.h>
#define mid(l,r) ((l + r) >> 1)
#define lsb(x) (x & (-x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define FOR(X,L,R) for(int X=L;X<R;++X)
#define endl '\n'
#define int long long
#define MOD ((int)1e9 + 7)
#define INF (0x3f3f3f3f)
#define LLINF (0x3f3f3f3f3f3f3f3fLL)
using namespace std;
template<typename T>
inline void input(vector<T>& v)
{
for(T& x: v)
cin >> x;
}
void solve()
{
int a, b, c; cin >> a >> b >> c;
cout << 21 - (a + b + c) << endl;
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
#if defined(CODEFORCES) || defined(CF)
int t; cin >> t;
while(t--) solve();
#else
solve();
#endif
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
int main(){
ll N;
cin>>N;
for(ll i=1;;i++){
if(stoll(to_string(i)+to_string(i))>N){
cout<<i-1<<endl;
return 0;
}
}
}
|
// Problem : D - AB
// Contest : AtCoder - AtCoder Regular Contest 108
// URL : https://atcoder.jp/contests/arc108/tasks/arc108_d
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
struct splitmix64_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 = std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template <typename K, typename V, typename Hash = splitmix64_hash>
using hash_map = __gnu_pbds::gp_hash_table<K, V, Hash>;
template <typename K, typename Hash = splitmix64_hash>
using hash_set = hash_map<K, __gnu_pbds::null_type, Hash>;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;
int n;
char caa, cab, cba, cbb;
ll dp1[maxN], dp2[maxN][2];
ll solve1(int left) {
if (left == 0) return 1;
if (left == 1) return 0;
if (dp1[left] != -1) return dp1[left];
ll ans = 0;
FOR(i, 2, left) {
ans = (ans + solve1(left - i)) % MOD;
}
dp1[left] = ans;
return ans;
}
ll solve2(int left, int which) {
if (left == 0) {
return 1 - which;
}
if (dp2[left][which] != -1) return dp2[left][which];
ll ans = 0;
FOR(i, 1, left) {
ans = (ans + solve2(left - i, 1 ^ which)) % MOD;
}
dp2[left][which] = ans;
return ans;
}
// sprawdz MODULO!
int main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
cin >> caa >> cab >> cba >> cbb;
RI(i, n) {
dp1[i] = -1;
dp2[i][0] = dp2[i][1] = -1;
}
// if (n == 2) {
// cout << 1 << "\n";
// }
// if(n == 3) {
// cout << 1 << "\n";
// return 0;
// }
if (cab == 'B') {
if (cbb == 'B') {
cout << 1 << "\n";
} else {
if (cba == 'B') {
cout << solve1(n) << "\n";
} else {
cout << solve2(n - 1, 1) << "\n";
}
}
} else {
if (caa == 'A') {
cout << 1 << "\n";
} else {
if (cba == 'A') {
cout << solve1(n) << "\n";
} else {
cout << solve2(n - 1, 1) << "\n";
}
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int N; string S;
cin >> N;
for(int i = 0; i < 4; i++){
char c; cin >> c;
S.push_back(c);
}
long long mod = 1000000007;
if(S.at(0) == 'A' && S.at(1) == 'A'){
cout << 1 << "\n"; return 0;
}
if(S.at(1) == 'B' && S.at(3) == 'B'){
cout << 1 << "\n"; return 0;
}
if(S.at(1) == S.at(2)){
long long ans[1010] = {};
ans[2] = 1; ans[3] = 1;
for(int i = 4; i <= 1005; i++){
ans[i] = (ans[i-1] + ans[i-2]) % mod;
}
cout << ans[N] << "\n";
return 0;
}
if(S.at(1) != S.at(2)){
long long ans[1010] = {};
ans[2] = 1; ans[3] = 1;
for(int i = 4; i <= 1005; i++){
ans[i] = (ans[i-1] + ans[i-1]) % mod;
}
cout << ans[N] << "\n";
return 0;
}
} |
#include<cstdio>
#include<cmath>
using namespace std;
#define ll long long
ll a,b,c,d;
int main()
{
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
if (c*d<=b) printf("-1\n");
else printf("%d",(int)ceil(a*1.0/(c*d-b)));
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double lld;
#define mod 1000000007;
#define INF 1e18
#define pb push_back
#define F first
#define S second
#define pll pair<ll,ll>
#define plll pair<ll,pair<ll,ll>>
// pb,mp,F,S,MOD,INF should not be used as variable names for any instances...
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void google(int t) {cout << "Case #" << t << ": ";}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
ll gcd(ll a,ll b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return a*b/__gcd(a,b);
}
bool isprime(ll x){
if(x <= 1){
return false;
}
if(x % 2 == 0 && x != 2){
return false;
}
for(ll i=3;i<=sqrt(x);i+=2){
if(x%i == 0){
return false;
}
}
return true;
}
ll power(ll x,ll y)
{
if (y == 0)
{ return 1;}
if (y%2==0)
{
return (power(x,y/2)*power(x,y/2))%mod;
}
if(y%2!=0)
{
return (x*power(x,y/2)*power(x,y/2))%mod;
}
}
#define ma 200001
vector<ll>edges[ma];
vector<ll>cat(ma);
ll up[ma][20];
// vector<pair<ll,ll>>downpath(ma);
// vector<ll>diameter(ma,0);
// vector<ll>vis(ma,0);
// int dfs(int src)
// {
// vis[src]=1;
// for(auto i:v[src])
// {
// if(vis[i]==0)
// {
// dfs(i);
// downpath[src].S+=1+downpath[i].S;
// downpath[src].F+=downpath[i].F+downpath[i].S+1;
// }
// }
// }
void binarylift(int src,int par)
{
up[src][0]=par;
for(int i=1;i<20;i++)
{
if(up[src][i-1]!=-1)
{
up[src][i]=up[up[src][i-1]][i-1];
}
else
{
up[src][i]=-1;
}
}
for(auto i:edges[src])
{
if(i!=par)
{
binarylift(i,src);
}
}
}
ll ans=0;
void solve(int r_sum,int edge,int node,int find,int c_sum)
{ // cout<<"fyj"<<endl;
if(c_sum>r_sum)
{
return;
}
if(c_sum==r_sum)
{ //cout<<find<<endl;
if(find!=0)
{
ans=(ans+1)%mod;
}
return;
}
for(int i=1;i<=edge;i++)
{
if(i>=node)
{
solve(r_sum,edge,node,find+1,c_sum+i);
}
else
{
solve(r_sum,edge,node,find,c_sum+i);
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("Error.txt", "w", stderr);
#endif
ll t,n,j,h,r,x,y,k,w,l,m,i,d,A,B,p,q;
t=1;
while(t--)
{
ll a,b,c;
cin>>a>>b>>c>>d;
if(b>=c*d)
{
cout<<-1<<endl;continue;
}
k=0;l=0;
while(k*d<a)
{
k+=c;
a+=b;
l++;
}
cout<<l<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep1(i,n) for (int i=1;i<(n);i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int n;
cin >> n;
map<int,int> circle;
vector<int> f(n);
rep(i,n){
cin >> f[i];
}
int cnt = 0;
rep(i,n){
map<int,int> m;
int x = f[i] - 1;
while(1){
if(m[x] > 0){
cnt++;
break;
}
if(circle[x] > 0){
break;
}
// if(m[x] > 0){
// cnt++;
// while(1){
// if(circle[x]>0){
// break;
// }
// circle[x]++;
// x = f[x] - 1;
// }
// break;
// }
m[x]++;
circle[x]++;
x = f[x] - 1;
}
}
ll ans = 1;
rep(i,cnt){
ans *= 2;
ans %= 998244353;
}
cout << (ans - 1) % 998244353 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int P=998244353;
vector<int> e[200050];
int n,ans=1;
bool v[200050];
void dfs(int u) {
v[u]=1;
for (int i=0; i<e[u].size(); ++i)
if (!v[e[u][i]]) dfs(e[u][i]);
}
int main() {
scanf("%d",&n);
for (int i=1,u; i<=n; ++i) {
scanf("%d",&u);
e[u].push_back(i);
e[i].push_back(u);
}
for (int i=1; i<=n; ++i)
if (!v[i]) dfs(i),ans=ans*2%P;
cout<<(ans+P-1)%P;
} |
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>
#include<string.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<math.h>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<=n;i++)
#define ALLOF(c) (c).begin(), (c).end()
#define Pa pair<ll,ll>
const ll mod=1000000007;
const ll modq=998244353;
const ll INF=1e12;
const ll inf=-1;
ll ABS(ll a){return max(a,-a);}
int main(void){
ll N;
string S,X;
cin>>N>>S>>X;
reverse(ALLOF(S));
reverse(ALLOF(X));
vector<vector<bool>> TW(N+1,vector<bool>(7,false));
TW.at(0).at(0)=true;
ll ima=1;
rep(i,N){
ll num=S.at(i)-'0';
if(X.at(i)=='A'){
rep(j,7) TW.at(i+1).at((j+ima*num)%7)=(TW.at(i).at(j) && TW.at(i).at((j+ima*num)%7));
}
else{
rep(j,7) TW.at(i+1).at((j+ima*num)%7)=(TW.at(i).at(j) || TW.at(i).at((j+ima*num)%7));
}
ima=(ima*10)%7;
}
if(TW.at(N).at(0)) cout<<"Takahashi"<<endl;
else cout<<"Aoki"<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
template<typename T> void dbg_a(T *a,int l,int r){cerr<<" {";for(int i = l;i<r;i++) cerr<<a[i]<<", ";cerr<<a[r]<<"}"<<endl;}
typedef long long ll;
//#define int ll
inline int read(){
int f(1),r(0);char c(getchar());
while(c<'0'||c>'9'){if(c == '-') f = -1; c = getchar();}
while(c>='0'&&c<='9'){ r = (r<<1) + (r<<3) + c - '0';c = getchar();}
return f*r;
}
typedef pair<int,int> PII;
#define fi first
#define se second
#define mst(a,b) memset(a,b,sizeof(a))
#define For(i,a,b) for(int i = a;i<=b;i++)
#define For_(i,a,b) for(int i = a;i>=b;i--)
#define _for(i,a) for(int i = 0;i<a;i++)
#define _sum(a,l,r) accumulate(a + l,a + 1 + r,0)
#define All(x) x.begin(),x.end()
// For(i,1,n) For(j,1,m) printf("f[%lld][%lld] = %lld\n",i,j,f[i][j]);
//const ll INF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10,M = N*2 + 10;
const int mod = 998244353;
int h[N],ne[M],e[M],idx;
void add(int a,int b){
e[++idx] = b,ne[idx] = h[a],h[a] = idx;
}
bool dp[N][10];
void solve(){
int n = read();
string s,x;
cin>>s>>x;
dp[n][0] = 1;
For_(i,n-1,0){
For(j,0,6){
int q = dp[i+1][10*j%7],p = dp[i+1][(10*j + s[i] - '0')%7];
if(x[i] == 'T') dp[i][j] = q|p;
else dp[i][j] = q&p;
}
}
if(dp[0][0]) cout<<"Takahashi\n";
else cout<<"Aoki\n";
}
signed main(){
int T = 1;
//T = read();
while(T--) solve();
system("pause");
return 0;
}
|
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
N--;
string S;
cin >> S;
while (N > 0) {
int K = 1;
int tmp = N;
while (tmp > 0) {
K *= 3;
tmp /= 3;
}
K /= 3;
string T;
rep(i, N - K + 1) {
char a = S[i], b = S[i + K];
if (a > b) swap(a, b);
if (a == b) T.pb(a);
else if (a == 'B' && b == 'R') T.pb('W');
else if (a == 'B' && b == 'W') T.pb('R');
else T.pb('B');
}
S = T;
N -= K;
}
co(S[0]);
Would you please return 0;
} | #include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000007;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<MOD>;
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a%b, y, x);
y -= a/b * x;
return d;
}
ll f(ll a, ll b, ll m) {
ll k,l;
extGCD(a,m,k,l);
if(k < 0) k = m+k;
return (m+b*k)%m;
}
void solve(ll a, ll b, ll m) {
ll g = gcd(a,m);
if(g == 1) {
cout << f(a,b,m) << endl;
} else if(b%g == 0) {
cout << f(a/g,b/g,m/g) << endl;
} else {
cout << -1 << endl;
}
}
int main() {
int T; cin >> T;
while(T--) {
ll N,S,K;
cin >> N >> S >> K;
solve(K,N-S,N);
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
ll N;
set<ll> A;
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N;
for(i=1;1LL*i*i<=N;i++) if(N%i==0) A.insert(i),A.insert(N/i);
FORR(a,A) cout<<a<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define br cout<<endl;
#define pb push_back
#define left(x) x.begin(), x.end()
#define right(x) x.begin(), x.end(), greater<ll>()
#define mp make_pair
#define M -1000000005
void solve(){
ll n;
cin>>n;
set<ll>a;
for(ll i = 1; i <= sqrt(n); ++i){
if(n % i == 0){
a.insert(i);
a.insert(n/i);
}
}
for(auto i:a){cout<<i;br}
}
int main(){
fast;
ll q=1;
while(q--){
solve();br;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char RPS(char a, char b) {
if(a=='R' && b=='S') return a;
if(a=='P' && b=='R') return a;
if(a=='S' && b=='P') return a;
if(a=='R' && b=='P') return b;
if(a=='P' && b=='S') return b;
if(a=='S' && b=='R') return b;
return a;
}
int main() {
ll n,k; cin>>n>>k;
string s; cin>>s;
for(int i=0; i<k; ++i) {
// 2つを比べたときの勝者
string winner;
for(int j=0; j<(int)s.length(); ++j) {
if(j==(int)s.length()-1) winner+=RPS(s[j],s[0]);
else winner+=RPS(s[j],s[j+1]);
}
string s2;
if(s.length()%2==0) {
for(int j=0; j<(int)s.length(); j+=2) s2+=winner[j];
} else {
for(int j=0; j<(int)s.length(); j+=2) s2+=winner[j];
for(int j=1; j<(int)s.length(); j+=2) s2+=winner[j];
}
s = s2;
}
cout << s[0] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
string a;
int n;
cin>>n;
cin>>a;
vector<int> C(n), A(n);
for(int i=0;i<n;i++)
{
if(a[i]=='A')
A[i]++;
else if( a[i]=='C')
C[i]++;
else if(a[i]=='G')
C[i]--;
else
A[i]--;
if(i==0) continue ;
A[i]+=A[i-1];
C[i]+=C[i-1];
}
int cnt=0;
for(int i=1;i<n;i++)
{
for(int j=0;j+i<n;j++)
{
if(j==0)
{
if(A[j+i]==0 && C[j+i]==0)
cnt++;
}
else
{
if(A[j+i]-A[j-1]==0 && C[j+i]-C[j-1]==0)
cnt++;
}
}
}
cout<<cnt;
return 0;
} |
/**
* @FileName a.cpp
* @Author kanpurin
* @Created 2021.05.31 03:48:48
**/
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
int main() {
int n,m;cin >> n >> m;
vector<bool> dp(min(n,m)*2+1);
vector<pair<int,int>> a(m);
for (int i = 0; i < m; i++) {
cin >> a[i].first >> a[i].second;
a[i].second-=max(n-m,0);
}
dp[dp.size()/2] = 1;
sort(a.begin(), a.end());
for (int i = 0; i < m; i++) {
int h = a[i].first;
vector<int> v1,v2;
while(i < m && h == a[i].first) {
if (a[i].second < 0 || a[i].second >= dp.size()) {i++; continue;}
v1.push_back(a[i].second);
if ((a[i].second>=1 && dp[a[i].second-1]) || (a[i].second+1<dp.size() && dp[a[i].second+1])) {
v2.push_back(a[i].second);
}
i++;
}
i--;
for(int v:v1) dp[v]=false;
for(int v:v2) dp[v]=true;
}
cout << accumulate(dp.begin(), dp.end(), 0) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
template <typename T>
void print_vec(const vector<T>& v, bool newline = true) {
for (int i = 0; i < v.size(); ++i) {
cout << v[i] << " ";
}
if (newline) {
cout << "\n";
}
}
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
const int maxn = (int)2e5 + 105;
const int mod = (int)998244353;
#define sz(x) ((int)(x.size()))
#define pb push_back
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repr(i,a,b) for (int i = (a); i >= (b); i--)
int fact_setup=0; int * fact;
int ex (int a, int b){
if(b==0)return 1; int e = ex(a,b/2); e=(1ll*e*e)%mod; if(b&1)e=(1ll*e*a)%mod; return e;
}
int inv (int a){
return ex(a, mod - 2);
}
int choose (int a, int b){
if(!fact_setup){
fact_setup=1;
fact = new int [maxn];
fact[0]=1; rep (i,1,maxn-1) fact[i]=(i*fact[i-1])%mod;
}
if(a<b)return 0;
int num = fact[a];
int denom = (fact[b] * fact[a - b]) % mod;
return (num * inv(denom)) % mod;
}
#define pii pair <int, int>
#define pvi pair <vector <int>, int>
#define pai array <int,2>
#define pdi array <double, 2>
#define pdt array <double, 3>
#define pti array <int,3>
#define pfi array <int,4>
#define all(v) v.begin(), v.end()
main(){
cin.tie(0); ios_base::sync_with_stdio(0);
int n, m; cin >> n >> m;
map<int,vector<int> >coord;
rep (i,1,m){
int a,b;cin>>a>>b;
coord[a].pb(b);
}
map<int,int>vis;vis[n]=1;
for (auto i = coord.begin(); i!=coord.end(); i++){
vector<int> v = i->second;
vector<pai>on;
for (int j : v){
if(vis[j-1]||vis[j+1]) on.pb({j,1});
else if(vis[j])on.pb({j,0});
}
for (auto j : on) vis[j[0]]=j[1];
}
int ans=0;
for (auto i = vis.begin();i!=vis.end(); i++){
if (i->second)ans++;
}
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000
ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}
const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
ll n,m,x,y,z,county,c2=0;
//vector<ll>vec[25];
ll ans=0;
ll arr[20];
ll dp[20][300005];
vector<pii>vec[20];
bool check(ll mask,ll ind)
{
ll ind2=0,count=0;
for(auto itr:vec[ind])
{
y=itr.fi,z=itr.se;
while(ind2<y)
{
ind2++;
ll z=arr[ind2]&mask;
if(z!=0)
count++;
}
if(count>z)
return 0;
}
return 1;
}
ll func(ll ind,ll mask)
{
// cout<<ind<<" "<<mask<<endl;
if(ind==n+1)
return 1;
if(dp[ind][mask]!=-1)
return dp[ind][mask];
ll count=0;
for(int i=1;i<=n;i++)
{
ll z=mask&arr[i];
if(z==0)
{
ll temp=mask+arr[i];
if(check(temp,ind))
{
// cout<<ind<<" "<<temp<<i<<" "<<arr[i]<<endl;
count+=func(ind+1,temp);
}
}
}
return dp[ind][mask]= count;
}
map<ll,pii>mpp;
int main()
{
memset(dp,-1,sizeof(dp));
fio;
ll t,i,j,a,b,c,mini=INT_MAXI,maxi=INT_MINI,q,flag=0;
cin>>n>>m;
arr[0]=1;
for(i=1;i<=20;i++)
arr[i]=2*arr[i-1];
for(i=0;i<m;i++)
{
cin>>x>>y>>z;
vec[x].pb(mp(y,z));
}
for(i=1;i<=n;i++)
sort(vec[i].begin(),vec[i].end());
ll ans=func(1,0);
cout<<ans;
} | #include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
const int MX = 300005;
ll n, k, a[MX], cn[MX], res, acu;
int main () {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> k;
forn (i, n) {
cin >> a[i];
cn[a[i]]++;
}
acu = k;
forn (i, n + 1) {
acu = min(acu, cn[i]);
res += acu;
}
cout << res << endl;
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const int mod = 1e9+7;
using namespace std;
int d[1005][1005];
vector<int>g[1005][26];
void solve() {
int n, m;
cin >> n >> m;
vector<pair<int,int>>e;
f(m) {
int a, b;
char c;
cin >> a >> b >> c;
e.pb({a, b});
g[a][c-'a'].pb(b);
g[b][c-'a'].pb(a);
}
queue<pair<int,int>>q;
q.push({1, n});
memset(d, -1, sizeof(d));
d[1][n] = 0;
while (q.size()) {
auto now = q.front();
q.pop();
for (int i = 0 ; i < 26 ; i++) {
for (auto &j : g[now.F][i]) {
for (auto &k : g[now.S][i]) {
if (d[j][k] == -1) {
d[j][k] = d[now.F][now.S] + 2;
q.push({j,k});
}
}
}
}
}
int mn = 1e9;
f1(n) {
if (~d[i][i])
mn = min(mn, d[i][i]);
}
for (auto &i : e) {
if (~d[i.F][i.S]) mn = min(mn, d[i.F][i.S] + 1);
if (~d[i.S][i.F]) mn = min(mn, d[i.S][i.F] + 1);
}
if (mn >1e6) {
mn = -1;
}
cout << mn << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
} | #include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>
#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif
#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO;
template<typename T> using V = std::vector<T>;
template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; }
/*-*/
int main() {
lnt n,t;
std::cin >> n >> t;
V<lnt> a(n);
fill(a);
V<lnt> x;
x.reserve((int)2e6);
lnt m=n/2;
x.emplace_back(0);
rep(s,1<<m) {
lnt sum=0;
rep(i,m) {
if(s>>i&1) sum+=a[i];
}
if(sum<=t) x.emplace_back(-sum);
}
std::set<lnt> set1(all(x));
lnt max=-*set1.lower_bound(-t);
rep(s,1<<(n-m)) {
lnt sum=0;
rep(i,(n-m)) {
if(s>>i&1) sum+=a[m+i];
}
if(sum<=t) {
auto lb=set1.lower_bound(-t+sum);
max=std::max(max,sum-*lb);
}
}
print(max);
}
|
#include<iostream>
#include<vector>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<stdlib.h>
#include<math.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<sstream>
#define endl "\n"
#define Doura() ios_base::sync_with_stdio(false),cin.tie(NULL), cout.tie(NULL)
#define ll long long
using namespace std;
const long long MXX=1e7;
bool isPalindrome(string s)
{
string s1=s;
reverse(s.begin(),s.end());
return s1==s;
}
void kajelag()
{
string s;
cin>>s;
if(isPalindrome(s))
{
cout<<"Yes"<<endl;
return;
}
int leading0=0,last0=0;
for(int i=0;i<s.size();i++)
{
if(s[i]!='0')
break;
else leading0++;
}
for(int i=s.size()-1;i>=0;i--)
{
if(s[i]!='0')
break;
else last0++;
}
if(last0>leading0)
{
int dif=last0-leading0;
string s1="";
for(int i=0;i<dif;i++)
s1+='0';
s1+=s;
if(isPalindrome(s1))
{
cout<<"Yes"<<endl;
return;
}
cout<<"No"<<endl;
}
else cout<<"No"<<endl;
}
int main()
{
Doura();
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE
int t;
// cin>>t;
// while(t--)
{
kajelag();
}
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
#define ld long double
#define f first
#define s second
#define FSD ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const int mod=998244353;
const int maxN=1e6+1;
const ll inf=1e18+1;
int main(){
int n;
cin>>n;
int nt = 1.08 * n;
if(nt < 206){
cout<<"Yay!\n";
}
else if(nt == 206){
cout<<"so-so\n";
}
else{
cout<<":(\n";
}
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define endl "\n"
#define f first
#define s second
#define ar array
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(X) ((int)(X).size())
#define pcnt __builtin_popcount
#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 all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define ms(c, x) memset(c,x,sizeof c)
#define No(x) cout<<(x?"NO":"No")<<endl;
#define Yes(x) cout<<(x?"YES":"Yes")<<endl;
#define nl cout<<endl;
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = l; i <=r; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; --i)
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
template<typename T>
using osl = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using osg = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
// for greater use greater<T>
template<typename T>
ostream &operator+(ostream &out, const vector<T> &vec) {
for (const auto &x : vec) {
out << x << " ";
}
out << "\n";
return out;
}
template<typename T>
ostream &operator*(ostream &out, const vector<T> &vec) {
for (const auto &x : vec) {
out + x;
}
return out;
}
template<typename T>
istream &operator>>(istream &in, vector<T> &vec) {
for (auto &x : vec) {
in >> x;
}
return in;
}
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; }
#ifdef LOCAL
void debug() { cerr << endl; }
template<class T, class... Args>
void debug(const T &t, const Args &... args) {
cerr << t << " ";
debug(args...);
}
#define dbg(...) {cerr<<__LINE__<<" [[DEBUG]] ";debug(__VA_ARGS__);};
#else
#define dbg(...) void(0)
#endif
const ll mod = 1e9 + 7;
//const int mod = 998244353;
const ll INF = 1e17 + 6;
inline ll power(ll x, ll y) {
ll res = 1;
x = x;
while (y > 0) {
if (y & 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
const int MXN=3e6+5;
void solve() {
int n,m;
cin>>n>>m;
vl h(n);
cin>>h;
vl w(m);
cin>>w;
vi pref((n+1)/2+2);
vi suff((n+1)/2+2);
sort(all(h));
for(int i=0;i<n-1;i+=2) {
pref[i / 2+1] += pref[i / 2] + h[i + 1] - h[i];
}
for(int i=n-2;i>0;i-=2) {
suff[i / 2] += suff[i / 2+1] + h[i+1] - h[i];
}
ll ans=INF;
for(auto i:w){
int pos=get_pos(h,i);
if(pos&1){
pos--;
}
ans=min(pref[pos/2]+suff[pos/2]+abs(i-h[pos]),ans);
}
cout<<ans<<endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(10);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1, tc = 1;
// cin >> t;
while (t--) {
// cout<<"Case #"<<tc<<": " ;
solve();
tc++;
}
#ifdef LOCAL
cerr << endl << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
#endif
return 0;
}
//look if it requires ll | #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
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; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};
const ll INF = (1ll<<62);
// const ld EPS = 1e-10;
// const ld PI = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;
int main(){
V<string> S(3);
REP(i, 3) cin >> S[i];
V<char> C;
REP(i, 3){
REP(j, S[i].size()){
C.push_back(S[i][j]);
}
}
UNIQUE(C);
// dump(C)
if((ll)C.size() > 10){
cout << "UNSOLVABLE" << endl;
return 0;
}
ll sz = C.size();
V<ll> c2num(26, -1);
REP(i, sz) c2num[C[i] - 'a'] = i;
// dump(sz)
V<ll> used(10, -1);
V<ll> vals(sz);
ll cnt = 0;
auto dfs = [&](auto self, ll now) -> void{
if(now == sz){
cnt++;
// dump(vals)
if(vals[c2num[S[0][0]-'a']] != 0 and vals[c2num[S[1][0]-'a']] != 0 and vals[c2num[S[2][0]-'a']] != 0){
V<ll> X(3, 0);
REP(i, 3){
REP(j, S[i].size()){
X[i] *= 10;
ll v = vals[c2num[S[i][j]-'a']];
X[i] += v;
}
}
if(X[0] + X[1] == X[2]){
REP(i, 3){
cout << X[i] << endl;
}
exit(0);
}
}
}else{
REP(i, 10){
if(used[i] == -1){
used[i] = 1;
vals[now] = i;
self(self, now+1);
used[i] = -1;
vals[now] = -1;
}
}
}
return;
};
dfs(dfs, 0);
dump(cnt)
cout << "UNSOLVABLE" << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
void solve(){
int a,b;
cin>>a>>b;
cout<<(a/b);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int T=1;
// cin>>T;
while(T--){
solve();
}
} | // Sky's the limit :)
#include <bits/stdc++.h>
using namespace std;
#define int long long
/*
w / a
*/
void run_case() {
int a, b;
cin >> a >> b;
vector<int> primes = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
};
int n = b - a + 1;
vector<int> bit(n, 0);
for (int i = a; i <= b; i++) {
for (int j = 0; j < primes.size(); j++) {
if (i % primes[j] == 0) {
bit[i - a] |= (1LL << j);
}
}
}
int N = (1 << 20);
vector<vector<int>> dp(n + 1, vector<int>(N, 0));
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < N; j++) {
dp[i + 1][j] += dp[i][j];
if ((j & bit[i]) == 0) {
dp[i + 1][j | bit[i]] += dp[i][j];
}
}
}
int res = 0;
for (int j = 0; j < N; j++) {
res += dp[n][j];
}
cout << res << '\n';
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int T = 1;
// cin >> T;
for (int t = 1; t <= T; t++) {
// cout << "Case #" << t << ": ";
run_case();
}
return 0;
} |
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) (x).begin(), (x).end()
#define ll long long
using namespace std;
int main() {
int n, x;
cin >> n >> x;
int a[n];
REP(i, n) cin >> a[i];
vector<int> ans;
REP(i, n) {
if (a[i] == x) {
continue;
} else {
ans.push_back(a[i]);
}
}
REP(i, ans.size()) {
if (ans.size() == i) {
cout << ans[i] << endl;
} else {
cout << ans[i];
cout << " ";
}
}
} | #include <bits/stdc++.h>
//#include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
//using namespace atcoder;
const int mod = 1000000007;
int main() {
int a,b;cin>>a>>b;
int X=(a+b)/2;
int Y=(a-b)/2;
cout<<X<<" "<<Y<<endl;
}
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
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; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
int main(){
int N; cin >> N;
ll X; cin >> X;
vector<ll> A(N); rep(i,N) cin >> A[i];
reverse(all(A));
//rep(i,N) cout << A[i] << " ";
//cout << endl;
vector<map<ll,ll>> dp(N+1);
dp[0][X]=1;
for(int i=1;i<=N;i++){
for(auto m:dp[i-1]){
ll x = m.first;
//cout << i <<" " << x << " " << m.second << endl;
if(x>=0){
ll t = (x)/A[i-1];
//cout << t << endl;
if(!(i>1&&t>=A[i-2]/A[i-1])){
if(abs(x-A[i-1]*t)<A[i-1]){
dp[i][x-A[i-1]*t]+=m.second;
}
}
if(!(i>1&&t+1>=A[i-2]/A[i-1])){
if(abs(x-A[i-1]*(t+1))<A[i-1]){
dp[i][x-A[i-1]*(t+1)]+=m.second;
}
}
}
else{
ll t=(-x)/A[i-1];
//cout << t << endl;
if(!(i>1&&t>=A[i-2]/A[i-1])){
if(abs(x+A[i-1]*t)<A[i-1]){
dp[i][x+A[i-1]*t]+=m.second;
}
}
if(!(i>1&&t+1>=A[i-2]/A[i-1])){
if(abs(x+A[i-1]*(t+1))<A[i-1]){
dp[i][x+A[i-1]*(t+1)]+=m.second;
}
}
}
}
}
cout << dp[N][0] << endl;;
return 0;
}
| #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>;
using Graph = vector<vector<int>>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(false);
int n;
cin>>n;
vector<ll>a(n);
rep(i,n)cin>>a[i];
ll ans=0;
ll sum=0;
vector<ll>b(n+1);
b[0]=0;
for(int i=1;i<n+1;i++){
b[i]=b[i-1]+a[i-1];
}
int max_point=0;
for(int i=0;i<n;i++){
chmax(ans,sum+b[max_point]);
if(sum+b[max_point]<sum+b[i+1]){
max_point=i+1;
}
sum+=b[i+1];
}
chmax(ans,sum);
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ld eps = 1e-8;
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define be(a) (a).begin(), (a).end()
// #define constexpr(...) (__VA_ARGS__)
// DEBUGING TEMPLETE ////////////////////////////////////////////////////////////////////////{{{
#define db(val) "["#val" = "<<(val)<<"] "
#define CONCAT_(x, y) x##y
#define CONCAT(x, y) CONCAT_(x, y)
#ifdef LOCAL_DEBUG
# define clog cerr << flush << string(__db_level * 2, ' ')
# define DB() debug_block CONCAT(dbbl, __LINE__)
int __db_level = 0;
struct debug_block {
debug_block() { clog << "{" << endl; ++__db_level; }
~debug_block() { --__db_level; clog << "}" << endl; }
};
#else
# define clog if (0) cerr
# define DB(...)
#endif
template<class U, class V> ostream& operator<<(ostream& out, const pair<U, V>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
if constexpr(i == tuple_size<T>::value) return out << ")";
else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U> ostream& operator<<(ostream& out, const tuple<U...>& tup) {
return print_tuple_utils<0, tuple<U...>>(out, tup);
}
template<class Con, class = decltype(begin(declval<Con>()))>
typename enable_if<!is_same<Con, string>::value, ostream&>::type
operator<<(ostream& out, const Con& container) {
out << "{";
for (auto it = container.begin(); it != container.end(); ++it)
out << (it == container.begin() ? "" : ", ") << *it;
return out << "}";
}
// ACTUAL SOLUTION START HERE ////////////////////////////////////////////////////////////////}}}
int main() {
#ifdef LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
freopen(".log", "w", stderr);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll m, h; cin >> m >> h;
if (h % m == 0) cout << "Yes";
else cout << "No";
return 0;
}
// vim: foldmethod=marker
| #include <iostream>
int main()
{
int m, h;
std::cin >> m >> h;
if (h % m == 0) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif
const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 998244353;
void GG(){cout<<"0\n"; exit(0);}
ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
ll re=1;
while (n>0){
if (n&1) re = re*a %mo;
a = a*a %mo;
n>>=1;
}
return re;
}
ll inv (ll b, ll mo = mod){
if (b==1) return b;
return (mo-mo/b) * inv(mo%b,mo) % mo;
}
const int maxn = 1e6+5;
ll dp[3003][6003];
ll sig[6006];
signed main(){
IOS();
int N,K; cin>>N>>K;
dp[0][0] = 1;
sig[0] += 1;
for (int n = 1; n<=N; ++n) {
for (int k = n; k>=0; --k) {
// for (int j = 0; j<=k; ++j) {
// dp[n][k] += dp[n-j][2*(k-j)];
// if (dp[n][k] >= mod) dp[n][k] -= mod;
// }
dp[n][k] = sig[2*n - 2*k];
if (2*n>=k) {
sig[2*n-k] += dp[n][k];
if (sig[2*n-k]>=mod) sig[2*n-k] -= mod;
}
}
}
// for (int j = 0; j<100; ++j) {
// bug(sig[K-j]);
// }
bug(sig[2*N - 2*K]);
bug(2*N-2*K);
cout<<dp[N][K]<<endl;
}
| /*
Normie's Template v2.0
*/
// Standard library in one include.
#include <bits/stdc++.h>
using namespace std;
// ordered_set library.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set(el) tree<el,null_type,less<el>,rb_tree_tag,tree_order_statistics_node_update>
// AtCoder library. (Comment out these two lines if you're not submitting in AtCoder.) (Or if you want to use it in other judges, run expander.py first.)
//#include <atcoder/all>
//using namespace atcoder;
//Pragmas (Comment out these three lines if you're submitting in szkopul.)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//File I/O.
#define FILE_IN "cseq.inp"
#define FILE_OUT "cseq.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
//Fast I/O.
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define nfio cin.tie(0);cout.tie(0)
#define endl "\n"
//Order checking.
#define ord(a,b,c) ((a>=b)and(b>=c))
//min/max redefines, so i dont have to resolve annoying compile errors.
#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))
//Constants.
#define MOD (ll(998244353))
#define MAX 300001
#define mag 320
//Pairs and 3-pairs.
#define p1 first
#define p2 second.first
#define p3 second.second
#define fi first
#define se second
#define pii(element_type) pair<element_type,element_type>
#define piii(element_type) pair<element_type,pii(element_type)>
//Quick power of 2.
#define pow2(x) (ll(1)<<x)
//Short for-loops.
#define ff(i,__,___) for(int i=__;i<=___;i++)
#define rr(i,__,___) for(int i=__;i>=___;i--)
//Typedefs.
#define bi BigInt
typedef long long ll;
typedef long double ld;
typedef short sh;
//---------END-------//
ll dp[3001][3001];
ll n,m,i,j,k,t,t1,u,v,a,b;
int main()
{
cin>>n>>m;
dp[0][0]=1;
for (i=1;i<=n;i++) for (j=1;j<=i;j++)
{
k=j;
while(true)
{
dp[i][j]+=dp[i-1][k-1];
dp[i][j]%=MOD;
k*=2;
if (k>i) break;
}
}
cout<<dp[n][m];
} |
#include <bits/stdc++.h>
using namespace std;
template<typename T, typename U>
using vp = vector<pair<T,U>>;
template<typename T>
using pque = priority_queue<T>;
template<typename T>
using lpque = priority_queue<T,vector<T>,greater<T>>;
using ll = long long;
using pint = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
using vint = vector<int>;
using vll = vector<ll>;
using qint = queue<int>;
using pqint = pque<int>;
using qll = queue<ll>;
using pqll = pque<ll>;
constexpr double PI = 3.141592653589793;
constexpr int INTINF = (1<<30)-1;
constexpr ll LLINF = (1LL<<62)-1;
constexpr int MPRIME = 1000000007;
constexpr int MPRIME9 = 998244353;
constexpr ll MMPRIME = (1LL<<61)-1;
constexpr char newl = '\n';
#define len length()
#define pushb push_back
#define fi first
#define se second
#define all(name) name.begin(),name.end()
#define rall(name) name.rbegin(),name.rend()
#define gsort(vbeg,vend) sort(vbeg,vend,greater<>())
template<typename T>
struct matrix{
private:
vector<vector<T>> mat;
public:
matrix() : matrix(0,0) {}
matrix(int h, int w) { resize(h,w); }
matrix(int h, int w, T init) { resize(h,w,init); }
void resize(int h, int w) {
mat=vector<vector<T>>(h,vector<T>(w));
}
void resize(int h, int w, T init) {
mat=vector<vector<T>>(h,vector<T>(w,init));
};
void in() {
for(int i=0; i<mat.size(); i++) for(int j=0; j<mat[i].size(); j++) {
cin>>mat[i][j];
}
}
void out() {
for(int i=0; i<mat.size(); i++) {
int wm=mat[i].size();
for(int j=0; j<wm; j++) {
cout<<mat[i][j]<<(wm==j+1 ? '\n' : ' ');
}
}
cout<<flush;
}
inline vector<T> &operator[](int idx) {
assert(0<=idx && idx<mat.size());
return mat[idx];
}
};
template<class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline void init(T& v) {
for(auto &a: v) cin>>a;
}
template<class T, class U>
inline void init(vector<pair<T,U>>& v) {
for(auto &a: v) cin>>a.first>>a.second;
}
template<class T, class N>
inline void init(T& v, N n) {
v.resize(n);
for(auto &a: v) cin>>a;
}
template<class T, class U, class N>
inline void init(vector<pair<T,U>>& v, N n) {
v.resize(n);
for(auto &a: v) cin>>a.first>>a.second;
}
template<class T>
inline void out(T a) {
cout<<a<<endl;
}
template<class T, class... U>
inline void out(T a, U... alist) {
cout<<a<<" ";
out(forward<U>(alist)...);
}
template<class N>
void resiz(N n) {
//empty
}
template<class N, class T, class... U>
void resiz(N n, T&& hd, U&&... tl) {
hd.resize(n);
resiz(n,forward<U>(tl)...);
}
long long binpow(long long a, long long ex, long long p=MMPRIME) {
long long res = 1;
while(ex > 0) {
if(ex & 1) (res*=a) %= p;
ex>>=1;
(a*=a) %= p;
}
return res;
}
ll r1,c1,r2,c2;
void input() {
cin>>r1>>c1>>r2>>c2;
}
void solve() {
if(r1==r2 && c1==c2) {
cout<<0<<newl;
return;
}
if(r1+c1==r2+c2 || r1-c1==r2-c2 || abs(r1-r2)+abs(c1-c2)<=3) {
cout<<1<<newl;
return;
}
if((r1+c1)%2==(r2+c2)%2 || abs(r1-r2)+abs(c1-c2)<=6 || abs(r1+c1-r2-c2)<=3 || abs(r1-c1-r2+c2)<=3) {
cout<<2<<newl;
return;
}
cout<<3<<newl;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout<<fixed<<setprecision(15);
int t=1;
while(t--) {
input();
solve();
}
}
| #include <bits/stdc++.h>
//#define int ll
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
const int mod=998244353;
void solve() {
int a,b,c,d;
int sum=0;
cin>>a>>b>>c>>d;
int x=abs(c-a);
int y=abs(d-b);
if(x==0&&y==0) {
cout<<"0\n";
return;
}
if(x==y||x+y<=3) {
cout<<"1\n";
return;
}
if(abs(x+y)%2==0||x+y<=6||abs(x-y)<=3) {
cout<<"2\n";
return;
}
cout<<"3\n";
}
signed main() {
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// freopen("1.in","r",stdin);
int _ = 1;
//cin >> _;
//init();
while (_--) {
solve();
}
return 0;
} |
//
// header_useful.h
// c++_acm
//
// Created by 李弘辰 on 2019/7/23.
// Copyright 2019 李弘辰. All rights reserved.
//
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <vector>
#include <list>
#include <set>
#include <utility> // pair
#include <map>
#include <iostream>
#include <sstream>
#include <algorithm> // sort
#include <functional>
#include <string>
#include <stack>
#include <queue>
#include <fstream>
#include <bitset>
//#include <unordered_map>
//#include <unordered_set>
using namespace std;
#define ll long long
#define lll __int128
#define uchar unsigned char
#define ushort unsigned short
#define uint unsigned int
#define ulong unsigned long
#define ull unsigned long long
#define INT_INF 0x7fffffff
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define memn(a,b,c,n) memset(a,b,sizeof(c)*(n))
#define fre(a) freopen(a,"r",stdin)
#define cio ios::sync_with_stdio(false); // Do not use it with "scanf" and other c input!
#define pb push_back
#define mpair make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define repl(i,a,b) for(ll i=a;i<=b;i++)
#define prel(i,a,b) for(ll i=a;i>=b;i--)
#define reada(a,s,n) rep(i,s,n)scanf("%d",a + i);
#define READa(a,s,n) REP(i,s,n)scanf("%d",a + i);
#define readall(a,s,n) rep(i,s,n)scanf("%lld",a + i);
#define READall(a,s,n) REP(i,s,n)scanf("%lld",a + i);
//template <typename _Tp> inline void read(_Tp&x) {
// char ch;bool flag=0;x=0;
// ch=getchar();
// while(!isdigit(ch)){if(ch=='-')flag=1;ch=getchar();}
// while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
// if(flag)x=-x;
//}
//inline void print_lll(lll x) {
// if(x<0) {x=-x;putchar('-');}
// if(x>9) print_lll(x/10);
// putchar(x%10+'0');
//}
//#define _T_(T) int T;scanf("%d",&T);while(T--)
//#define __T _T_(TTESTCASES)
//#define _E_(T) while(~T)
#define __T int TT;scanf("%d",&TT);for (int T = 1;T <= TT;T ++)
#define _C(a) cout << a << endl;
#define dsci(a) int a;scanf("%d",&a)
#define dscii(a,b) int a,b;scanf("%d%d",&a,&b)
#define dsciii(a,b,c) int a,b,c;scanf("%d%d%d",&a,&b,&c)
#define dscl(a) ll a;scanf("%lld",&a)
#define dscll(a,b) ll a,b;scanf("%lld%lld",&a,&b)
#define dsclll(a,b,c) ll a,b,c;scanf("%lld%lld%lld",&a,&b,&c)
#define dscd(a) double a;scanf("%lf",&a)
#define dscdd(a,b) double a,b;scanf("%lf%lf",&a,&b)
#define dscddd(a,b,c) double a,b,c;scanf("%lf%lf%lf",&a,&b,&c)
#define sci(a) scanf("%d",&a)
#define scii(a,b) scanf("%d%d",&a,&b)
#define sciii(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define scd(a) scanf("%lf",&a)
#define scdd(a,b) scanf("%lf%lf",&a,&b)
#define scddd(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
#define lowbit(x) ((x)&(-(x)))
#define Tprint(a,s,e) REP(i,s,e){if(i!=s)printf(" ");printf("%lld",a[i]);}
#define endl '\n'
#define itn int
#define iny int
#define nit int
#define inr int
#define mian main
#define iman main
#define mina main
#define mian main
#define ednl endl
#define fro for
#define fir for
#define reutrn return
#define retunr return
#define reutnr return
#define re0 return 0
#define re1 return 1
/* header_useful_h */
void solve(){
string a; cin >> a;
int len = a.length();
if(len == 1){
cout << 0 << endl;
return;
}
int l = stoi(a.substr(0, len / 2));
int r = stoi(a.substr(len / 2));
if(l == r) cout << l << endl;
else if(len & 1){
for(int i = 0; i < len / 2; i ++) cout << 9; cout << endl;
}else{
if(l < r) cout << l << endl;
else cout << l - 1 << endl;
}
}
int main()
{
solve();
}
| #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using P = pair<ll,ll>;
#define pb push_back
int main(){
vvl tate(29, vl(30,5000));
vvl yoko(30, vl(29,5000));
rep(_,1000){
int a, b, ato, bto;
cin >> a >> b >> ato >> bto;
int ma = a;
int mb = b;
string ans;
while(a != ato || b != bto){
if (a == ato){
if (b < bto){
b++;
ans.pb('R'); continue; // continueの貼り付けを忘れると
} else { // バグる ato btoが連続するからです
b--; // 全てに張り付けておく必要があるので
ans.pb('L'); continue;
}
}
if (b == bto){
if (a < ato){
a++;
ans.pb('D'); continue;
} else {
a--;
ans.pb('U'); continue;
}
}
if (a < ato && b < bto){
// 下 右
if (tate[a][b] < yoko[a][b]){
a++; // 縦移動優先
ans.pb('D'); continue;
} else {
b++;
ans.pb('R'); continue;
}
}
if (a < ato && b > bto){
// 下 左
if (tate[a][b] < yoko[a][b-1]){
a++; // 縦移動優先
ans.pb('D'); continue;
} else {
b--;
ans.pb('L'); continue;
}
}
if (a > ato && b < bto){
// 上 右
if (tate[a-1][b] < yoko[a][b]){
a--; // 縦移動優先
ans.pb('U'); continue;
} else {
b++;
ans.pb('R'); continue;
}
}
if (a > ato && b > bto){
// 上 左 ここから続き
if (tate[a-1][b] < yoko[a][b-1]){
a--; // 縦移動優先
ans.pb('U'); continue;
} else {
b--;
ans.pb('L'); continue;
}
}
}
cout << ans << endl; // flushを炊く
ll score;
cin >> score;
score /= ans.size(); // 通った数で割り一歩当たりのコスト計算
ll change = (5000 - score) / ans.size();
rep(i,ans.size()){
if (ans[i] == 'U'){
tate[ma-1][mb] += change;
ma--;
}
if (ans[i] == 'D'){
tate[ma][mb] += change;
ma++;
}
if (ans[i] == 'L'){
yoko[ma][mb-1] += change;
mb--;
}
if (ans[i] == 'R'){
yoko[ma][mb] += change;
mb++;
}
}
// for(auto x : tate){ // コスト計算debug
// for(auto y : x){
// cout << y << " ";
// } cout << endl;
// }
// for(auto x : yoko){
// for(auto y : x){
// cout << y << " ";
// } cout << endl;
// }
}
} |