code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
/*debug-------------------------------------------------------------------------------*/
#define _GLIBCXX_DEBUG
#ifdef _DEBUG
#define debug(...) COUT(__VA_ARGS__)
#else
#define debug(...)
#endif
/*include&using-----------------------------------------------------------------------*/
#include <bits/stdc++.h>
using namespace std;
/*typename----------------------------------------------------------------------------*/
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using P = pair<T,T>;
template <class T> using M = map<T,T>;
template <class T> using S = set<T>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T,V<T>,greater<T>>;
using ll = long long;
using st = string;
using vl = V<ll>;
using vvl = V<V<ll>>;
using vc = V<char>;
using vvc = V<V<char>>;
using vs = V<st>;
using vvs = V<V<st>>;
using vb = V<bool>;
using vvb = V<V<bool>>;
/*constant----------------------------------------------------------------------------*/
const ll INF = 1e9;
const ll MOD = 1e9+7;
const vl dx = {1,0,-1,0,1,0,-1,0};
const vl dy = {0,1,0,-1,0,1,0,-1};
/*macro-------------------------------------------------------------------------------*/
#define re(n) for(ll _ = 0; _ < (ll) n; _++)
#define rep(i,n) for(ll i = 0; i < (ll) n; i++)
#define rrep(i,n) for(ll i = (ll) n - 1; i >= 0;i--)
#define repp(i,x,n) for(ll i = (ll) x; i < (ll) n; i++)
#define bitrep(i,n) for(ll i = 0; i < (1<<(ll)n); i++)
#define each(x,A) for(auto &(x): (A))
#define all(A) A.begin(), A.end()
#define len(A) ll(A.size())
#define pb(a) push_back(a)
#define mp make_pair
#define pc cout << setprecision(15) << fixed
/*input-------------------------------------------------------------------------------*/
void CIN(){}
template <class Head,class... Tail>
void CIN(Head&& h,Tail&&... t){cin>>h; CIN(t...);}
#define CINL(...) ll __VA_ARGS__;CIN(__VA_ARGS__)
#define CINS(...) string __VA_ARGS__;CIN(__VA_ARGS__)
#define CIND(...) double __VA_ARGS__;CIN(__VA_ARGS__)
/*output------------------------------------------------------------------------------*/
void COUT(){cout << endl;}
template <class Head, class... Tail>
void COUT(Head h, Tail... t){cout << h << " "; COUT(t...);}
/*function----------------------------------------------------------------------------*/
void Yes(bool ans){cout << (ans? "Yes" : "No") << endl;}
void YES(bool ans){cout << (ans? "YES" : "NO") << endl;}
template <class T> T ceil(T a,T b){return (a+b-1)/b;}
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(a>b){a=b;}}
template <class T> T gcd(T a,T b){if(a<b)swap(a,b); if(a%b==0)return b; return gcd(b,a%b);}
template <class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template <class T> T modpow(T x,T n,T mod){T res=1; for(ll i=0;i<n;i++){res=res*x%mod;}return res;}
bool range(ll ny, ll nx, ll H, ll W) {if(ny < 0 || ny >= H || nx < 0 || nx >= W) return false; return true;}
/*------------------------------------------------------------------------------------*/
int main() {
CINL(h,w,x,y);
x--;
y--;
vs s(h);
rep(i,h){
CIN(s[i]);
}
ll ans = 1;
rep(d,4){
ll nx = x + dx[d];
ll ny = y + dy[d];
while(range(ny,nx,w,h)){
if(s[nx][ny] == '#') {
break;
}
else {
ans++;
debug(nx, ny);
}
nx += dx[d];
ny += dy[d];
}
}
COUT(ans);
} | //Har Har Mahadev
using namespace std;
#include <bits/stdc++.h>
#define booga cerr << "booga" << endl
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define safai(...) Clearing_out(__VA_ARGS__)
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(char c) {
string s;
s += c;
return s;
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "1" : "0");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
void Clearing_out() { return; }
template <typename Head, typename... Tail>
void Clearing_out(Head &H, Tail & ... T) {
H.clear();
Clearing_out(T...);
}
void testcase(){
int n;
cin >> n;
vector<vector<int>> a(n, vector<int> (5));
for(int i = 0; i < n; i++){
for(int j = 0; j < 5; j++){
cin >> a[i][j];
}
}
int low = 1, high = 1000000000;
int ans = 1;
while(low <= high){
int mid = (low+high)/2;
set<int> all;
for(int i = 0; i < n; i++){
int kk = 1;
int curr = 0;
for(int j = 0; j < 5; j++){
if(a[i][j] >= mid) curr += kk;
kk *= 2;
}
all.insert(curr);
}
int f = 0;
//debug(all);
for(auto i : all) for(auto j : all) for(auto k : all) if((i | j | k) == 31){
f = 1;
ans = mid;
}
if(f) ans = mid, low = mid+1;
else high = mid-1;
}
cout << ans << '\n';
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
//cin >> tt;
while(tt--){
testcase();
}
return 0;
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define endl '\n'
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define inf 1e8
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define range(a,b) substr(a,b-a+1)
#define w(x) int x; cin>>x; while(x--)
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl;
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve()
{
int a,b,c;
cin>>a>>b>>c;
if(c%2==0) c=2;
else c=1;
int val1,val2;
val1=(int)pow(a,c);
val2=(int)pow(b,c);
char ans;
if(val1==val2)
{
ans='=';
}
else if(val1>val2)
{
ans='>';
}
else
{
ans='<';
}
cout<<ans;
}
int32_t main()
{
FIO;
// w(t)
solve();
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 num[8210];
int kazu[8210];
const int ma = (1 << 13) - 1;
int query(int n) {
int n2 = (n & ma) + 1;
while (num[n2]) {
if (num[n2] == n + 1) return kazu[n2];
n2 = n2 * 2 % 8209;
}
return 0;
}
void add(int n, int b) {
int n2 = (n & ma) + 1;
while (num[n2]) {
if (num[n2] == n + 1) return;
n2 = n2 * 2 % 8209;
}
num[n2] = n + 1;
kazu[n2] = b;
}
int li[300];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
int w[8];
rep(i, N) cin >> w[i];
pair<int, int> lv[100001];
rep(i, M) {
int l, v;
cin >> l >> v;
lv[i] = { v, l };
}
sort(lv, lv + M);
lv[M] = { 1e9,1e9 };
rep(i, 1 << N) {
rep(j, N) if (i >> j & 1) li[i] += w[j];
}
sort(li, li + (1 << N));
int saidai = 0;
int k = 0;
rep(i, 1 << N) {
while (lv[k].first < li[i]) {
if (saidai < lv[k].second) saidai = lv[k].second;
k++;
}
add(li[i], saidai);
}
rep(i, N) {
if (w[i] > lv[0].first) {
printf("-1");
return 0;
}
}
int P[8];
rep(i, N) P[i] = i;
int kotae = 1e9;
do {
int tmp[8] = {};
rep(p, N - 1) {
int omosa = w[P[p]];
for (int q = p + 1; q < N; q++) {
omosa += w[P[q]];
int kari = tmp[p] + query(omosa);
if (tmp[q] < kari) tmp[q] = kari;
}
}
if (kotae > tmp[N - 1]) kotae = tmp[N - 1];
} while (next_permutation(P, P + N));
printf("%d\n", kotae);
Would you please return 0;
} |
#include<iostream>
#include <algorithm>
using namespace std;
int main(){
string s;
cin >> s;
for(int i = 0; i < s.length(); i++){
if(s.at(i) == '6'){
s.at(i) = '9';
}else if(s.at(i) == '9'){
s.at(i) = '6';
}
}
reverse(s.begin(),s.end());
cout << s << endl;
} | #include<stdio.h>
#include<string.h>
int main(void){
char str[100000+1];
scanf("%s",str);
for(int i=0;i<strlen(str);i++){
switch(str[strlen(str)-1-i]){
case '6':
printf("%c",str[strlen(str)-1-i]+3);
break;
case '9':
printf("%c",str[strlen(str)-1-i]-3);
break;
default:
printf("%c",str[strlen(str)-1-i]);
break;
}
}
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cctype>
#include <cmath>
#include <vector>
#include <set>
#include <bitset>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#include <cassert>
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long LL;
inline int read_int(){
int t=0;bool sign=false;char c=getchar();
while(!isdigit(c)){sign|=c=='-';c=getchar();}
while(isdigit(c)){t=(t<<1)+(t<<3)+(c&15);c=getchar();}
return sign?-t:t;
}
inline LL read_LL(){
LL t=0;bool sign=false;char c=getchar();
while(!isdigit(c)){sign|=c=='-';c=getchar();}
while(isdigit(c)){t=(t<<1)+(t<<3)+(c&15);c=getchar();}
return sign?-t:t;
}
inline char get_char(){
char c=getchar();
while(c==' '||c=='\n'||c=='\r')c=getchar();
return c;
}
inline void write(LL x){
char c[21],len=0;
if(!x)return putchar('0'),void();
if(x<0)x=-x,putchar('-');
while(x)c[++len]=x%10,x/=10;
while(len)putchar(c[len--]+48);
}
inline void space(LL x){write(x),putchar(' ');}
inline void enter(LL x){write(x),putchar('\n');}
LL cal(LL n,LL p){
int pos=3;
LL ans=0;
while(p>0){
LL t=n;
while(t>=p){
t-=p;
ans++;
}
if(pos==3)pos=4;
else pos=3;
n=p;
p=t;
}
return ans+n;
}
int main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
LL n=read_LL();
LL v=n*2/(sqrt(5)+1);
for(LL i=max(v-20,0LL);i<=min(v+20,n);i++){
if(cal(n,i)<125){
LL p=i;
stack<int> s;
int pos=3;
while(p>0){
LL t=n;
while(t>=p){
t-=p;
s.push(pos);
}
if(pos==3)pos=4;
else pos=3;
n=p;
p=t;
}
enter(s.size()+n);
LL tn=0,tp=0;
_for(i,0,n){
if(pos==3){
tn++;
}
else
tp++;
enter(pos-2);
}
while(!s.empty()){
enter(s.top());
if(s.top()==3)
tn=tn+tp;
else
tp=tn+tp;
s.pop();
}
return 0;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string a = "";
int si, sj, x;
cin >> si >> sj;
set<int> s;
map<pair<int, int>, pair<int, int>> m;
map<int, int> t;
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
cin >> x;
m[make_pair(i, j)] = make_pair(x, 0);
if (t.count(x)) {
t.at(x)++;
}
else {
t[x] = 1;
}
}
}
s.insert(m.at(make_pair(si, sj)).first);
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
cin >> x;
m.at(make_pair(i, j)).second = x;
}
}
if (si > 24 && sj > 24) {
for (int i = 0; i < 2499; i++) {
if (si != 0 && !(s.count(m.at(make_pair(si - 1, sj)).first))) {
s.insert(m.at(make_pair(si - 1, sj)).first);
a += "U";
si--;
}
else if (sj != 0 && !(s.count(m.at(make_pair(si, sj - 1)).first))) {
s.insert(m.at(make_pair(si, sj - 1)).first);
a += "L";
sj--;
}
else if (si != 49 && !(s.count(m.at(make_pair(si + 1, sj)).first))) {
s.insert(m.at(make_pair(si + 1, sj)).first);
a += "D";
si++;
}
else if (sj != 49 && !(s.count(m.at(make_pair(si, sj + 1)).first))) {
s.insert(m.at(make_pair(si, sj + 1)).first);
a += "R";
sj++;
}
else {
break;
}
}
}
else if (si > 24 && sj < 25) {
for (int i = 0; i < 2499; i++) {
if (si != 0 && !(s.count(m.at(make_pair(si - 1, sj)).first))) {
s.insert(m.at(make_pair(si - 1, sj)).first);
a += "U";
si--;
}
else if (sj != 49 && !(s.count(m.at(make_pair(si, sj + 1)).first))) {
s.insert(m.at(make_pair(si, sj + 1)).first);
a += "R";
sj++;
}
else if (si != 49 && !(s.count(m.at(make_pair(si + 1, sj)).first))) {
s.insert(m.at(make_pair(si + 1, sj)).first);
a += "D";
si++;
}
else if (sj != 0 && !(s.count(m.at(make_pair(si, sj - 1)).first))) {
s.insert(m.at(make_pair(si, sj - 1)).first);
a += "L";
sj--;
}
else {
break;
}
}
}
else if (si < 25 && sj < 25) {
for (int i = 0; i < 2499; i++) {
if (si != 49 && !(s.count(m.at(make_pair(si + 1, sj)).first))) {
s.insert(m.at(make_pair(si + 1, sj)).first);
a += "D";
si++;
}
else if (sj != 49 && !(s.count(m.at(make_pair(si, sj + 1)).first))) {
s.insert(m.at(make_pair(si, sj + 1)).first);
a += "R";
sj++;
}
else if (si != 0 && !(s.count(m.at(make_pair(si - 1, sj)).first))) {
s.insert(m.at(make_pair(si - 1, sj)).first);
a += "U";
si--;
}
else if (sj != 0 && !(s.count(m.at(make_pair(si, sj - 1)).first))) {
s.insert(m.at(make_pair(si, sj - 1)).first);
a += "L";
sj--;
}
else {
break;
}
}
}
else {
for (int i = 0; i < 2499; i++) {
if (si != 49 && !(s.count(m.at(make_pair(si + 1, sj)).first))) {
s.insert(m.at(make_pair(si + 1, sj)).first);
a += "D";
si++;
}
else if (sj != 0 && !(s.count(m.at(make_pair(si, sj - 1)).first))) {
s.insert(m.at(make_pair(si, sj - 1)).first);
a += "L";
sj--;
}
else if (si != 0 && !(s.count(m.at(make_pair(si - 1, sj)).first))) {
s.insert(m.at(make_pair(si - 1, sj)).first);
a += "U";
si--;
}
else if (sj != 49 && !(s.count(m.at(make_pair(si, sj + 1)).first))) {
s.insert(m.at(make_pair(si, sj + 1)).first);
a += "R";
sj++;
}
else {
break;
}
}
}
cout << a << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int) x.size()
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define FOR(i, a, b) for(auto i=(a); i<(b); i++)
#define ROF(i, a, b) for(auto i=(b)-1; i>=(a); i--)
#define F0R(i, n) FOR(i, 0, n)
#define R0F(i, n) ROF(i, 0, n)
using ll=long long;
using ld=long double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using vi=vector<int>;
using vl=vector<ll>;
using vpii=vector<pii>;
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 b>a?a=b,1:0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxN=1e5+10;
const int MOD=1e9+7;
const ll infLL=1e18;
const ld eps=1e-6;
template<int mod> struct mint {
int v; explicit operator int() const{return v;}
mint() {v=0;}
mint(ll _v) { v =int((-mod< _v&&_v<mod)?_v:_v%mod);
if(v < 0) v+=mod;}
friend bool operator==(const mint& a, const mint& b){
return a.v==b.v;}
friend bool operator!=(const mint& a, const mint& b){
return !(a==b);}
friend bool operator<(const mint& a, const mint& b){
return a.v<b.v;}
mint& operator+=(const mint& m) {
if((v+=m.v)>=mod) v-=mod;
return *this;}
mint& operator-=(const mint& m) {
if((v-=m.v)<0) v+=mod;
return *this;}
mint& operator*=(const mint& m){
v=int((ll)v*m.v%mod); return *this;}
mint& operator/=(const mint& m) {return (*this)*=inv(m);}
friend mint pow(mint a, ll p) {
mint ans = 1; assert(p >= 0);
for (;p;p/=2,a*=a) if(p&1) ans*=a;
return ans;}
friend mint inv(const mint& a){assert(a.v!=0);
return pow(a,mod-2);}
mint operator-() const{return mint(-v);}
friend mint operator+(mint a, const mint& b) {return a+=b;}
friend mint operator-(mint a, const mint& b) {return a-=b;}
friend mint operator*(mint a, const mint& b) {return a*=b;}
friend mint operator/(mint a, const mint& b) {return a/=b;}
};
using mi=mint<MOD>;
vector<mi> invs, fac, ifac;
void genFac(int SZ=mxN) {
invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); vi Invs(SZ);
Invs[1]=1; invs[1]=fac[0]=ifac[0]=1;
FOR(i,2,SZ) Invs[i] = int(MOD-(ll)MOD/i*Invs[MOD%i]%MOD), invs[i]=mi(Invs[i]);
FOR(i,1,SZ) {
fac[i]=fac[i-1]*i;
ifac[i]=ifac[i-1]*invs[i];
}
}
mi choose(int n, int k){
if(k>n) return 0;
return fac[n]*ifac[n-k]*ifac[k];
}
int n;
int a[mxN];
mi dp[mxN][2];
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n;
F0R(i, n) cin >> a[i];
dp[0][0]=1;
FOR(i, 1, n+1){
dp[i][0]=dp[i-1][0]+dp[i-1][1];
dp[i][1]=dp[i-1][0];
}
mi ans=0;
F0R(i, n){
if(i==0){
ans+=(dp[n-1][0]+dp[n-1][1])*a[i];
}
else{
ans+=(dp[n-1-i][0]+dp[n-1-i][1])*(dp[i-1][0]+dp[i-1][1])*a[i];
ans-=dp[n-1-i][0]*dp[i-1][0]*a[i];
}
}
cout << ans.v << "\n";
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll,ll>
#define tii tuple<ll,ll,ll>
#define all(v) (v).begin(),(v).end()
#define ll long long
#define ull unsigned long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define eps 1e-9
#define endl "\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getRand(int l, int r)
{
return uniform_int_distribution<int>(l, r)(rng);
}
void run_case() {
ll n;
cin>>n;
vector<ll> vec(n);
for(auto& it : vec) cin>>it;
ll dp[n+100][2];
memset(dp, 0, sizeof(dp));
dp[1][0] = 1;
dp[1][1] = 1;
for(int i = 2; i <= n+5; ++i) {
(dp[i][0] = dp[i-1][0] + dp[i-1][1]) %= mod;
(dp[i][1] = dp[i-1][0]) %= mod;
}
dp[0][0] = dp[0][1] = 0;
dp[0][0] = 1;
ll sum = (vec[0] * ((dp[n-1][0] + dp[n-1][1])%mod))%mod;
for(int i = 1; i < n; ++i) {
ll cur1 = (dp[i-1][0] + dp[i-1][1])%mod;
(cur1 *= (dp[n - i - 1][0] + dp[n - i - 1][1])) %= mod;
(sum += cur1 * vec[i]) %= mod;
ll cur2 = (dp[i-1][0])%mod;
(cur2 *= dp[n-i-1][0]) %= mod;
(sum -= cur2*vec[i]) %= mod;
(sum += mod) %= mod;
//cout<<cur1<<" "<<cur2<<" "<<dp[n-i-1][1]<<" "<<sum<<endl;
}
cout<<sum%mod<<endl;
}
int32_t main() {
fastio
int t = 1;
//cin>>t;
for(int i = 0; i < t; ++i) {
run_case();
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define FORR(i,a,b) for(ll i=a;i>=b;i--)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define rep(i,n) for(ll i=0;i<n;i++)
#define vl vector<ll>
#define vi vector<int>
#define ld long double
#define vld vector<ld>
#define vvl vector<vector<ll> >
#define vvi vector<vector<int> >
#define vvld vector<vector<ld> >
#define pll pair<ll,ll>
#define vpll vector<pll>
#define fi first
#define se second
#define pb push_back
#define pf push_front //only for deque
#define mp make_pair
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end())
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rsortv(vec) sort(vec.begin(),vec.end(), greater<ll>())
#define mset(a,val) memset(a,val,sizeof(a));
#define PI 3.14159265358979323846264338327950288419716939937510
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
#define FAST std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
const ll N = 2e5 + 5;
const ll INF = 10000000000000;
const ll mod= 1e9 +7;
ll mind(ll x, ll y)
{
if (x < y) {
return x;
}
else
{
return y;
}
}
ll poww(ll a, ll b) {
if (b < 0 || a <= 0)return 0;
ll ans = 1LL;
while (b) {
if (b & 1)ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
ll gcd(ll a,ll b){
if(b==0)
return a;
return gcd(b,a%b);
}
ll getSum(ll num)
{
ll sum = 0;
while (num != 0) {
sum = sum + num % 10;
num = num / 10;
}
return sum;
}
ll isPrime(ll num){
if(num<=1)return 0;
for(ll i=2; i*i<=num; i++){
if(num%i==0){
return 0;
}
}
return 1;
}
int retMaxDig(int num){
string s= to_string(num);
int max= 0;
rep(i,s.size()){
if(s[i]-'0'>max)max=s[i]-'0';
}
return max;
}
ll decimalToBinary(ll num)
{
// To store the binary number
ll B_Number = 0;
ll cnt = 0;
while (num != 0) {
ll rem = num % 2;
ll c = poww(10, cnt);
B_Number += rem * c;
num /= 2;
// Count used to store exponent value
cnt++;
}
return B_Number;
}
ll binomialCoeff(ll n, ll k)
{
ll res = 1;
if(n<k){
res= 0;
return res;
}
// Since C(n, k) = C(n, n-k)
if (k > n - k)
k = n - k;
// Calculate value of
// [n * (n-1) *---* (n-k+1)] / [k * (k-1) *----* 1]
for (ll i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
ll isPalindrome(string str){
ll num= str.size();
FOR(i,0,(num-1)/2){
if(str[i]!=str[num-i-1]){
return 0;
}
}
return 1;
}
ll factorial(ll n)
{
if(n == 0)
return 1;
ll i = n, fact = 1;
while (n / i != n) {
fact = fact * i;
i--;
}
return fact;
}
//////////////////////////////END OF TEMPLATE//////////////////////////////
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
FAST;
ll a,b,c;cin>>a>>b>>c;
if(a*a+b*b<c*c)cout<<"Yes";
else cout<<"No";
return 0;
}
| #include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define fo(i,n) for (int i = 0; i <n; i++)
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
t = 1;
// cin >> t;
while(t--)
{
ll n=0,c1=0,i=0,sum=0,k1=0,j=0,k2=0;
string s="";
ll a,b,c;
cin>>a>>b>>c;a*=a;b*=b;c*=c;
cout<<((a+b)<c?"Yes":"No");
cout<<endl;
}} |
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>
#include <cassert>
#include <random>
#include <chrono>
/* #include <atcoder/all> */
/* using namespace atcoder; */
using namespace std;
void debug_out(){ cout << "\n"; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
/* const ll MOD = 998244353; */
const ll INF = 1ll<<60;
const double PI = acos(-1);
struct INIT { INIT(){
cin.tie(0); ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}}INIT;
signed main() {
string s;
cin >> s;
REP(i, s.size()){
if(s[i] == '.') break;
cout << s[i];
}
cout << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
vector<int> mod(3, 0);
int dig=0;
while(n > 0) {
mod[n%10ll%3ll]++;
n /= 10ll;
dig++;
}
int ans=dig;
for(int i=0; i<=min(mod[1], mod[2]); i++) {
ans = min(ans, ((mod[1]-i)%3) + ((mod[2]-i)%3));
}
cout << (ans==dig ? -1 : ans) << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vvp = vector<vector<pair<int,int>>>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a,b;
cin >> a >> b;
cout << (a+b)/2 csp (a-b)/2 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T> using v2 = vector<vector<T>>;
template<typename T> inline v2<T> fill(int r, int c, const T& t){ return v2<T>(r, vector<T>(c, t)); }
#define F first
#define S second
void solve(){
int a, b;
cin >> a >> b;
cout << (a+b)/2 << " " << (a-b)/2 << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} |
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main(){
int a,b; cin >> a >> b;
int dis = b - a + 1;
int ans = 0;
while(dis > 1){
int tmp = b / dis;
if(a <= dis * (tmp -1)){
cout << dis;
return 0;
}
dis--;
}
cout << dis;
} | #include <bits/stdc++.h>
using namespace std;
int bs(vector<int> &arr, int n)
{
int left = 0, right = arr.size() - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(arr[mid] == n) {
return mid;
}
else if(arr[mid] > n) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int a, b;
cin >> a >> b;
long long int ans = abs(a - b);
if(ans >= 3) {
cout << "No\n";
}
else {
cout << "Yes\n";
}
}
|
#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
int N, M; cin >> N >> M;
vector<vector<vector<int>>> G(N, vector<vector<int>>(26));
vector<vector<bool>> adj(N, vector<bool>(N, false));
REP(i, 0, M) {
int a, b; char c; cin >> a >> b >> c; a--; b--; c -= 'a';
G[a][c].push_back(b);
G[b][c].push_back(a);
adj[a][b] = adj[b][a] = true;
}
int ans = 1 << 30;
queue<pair<int, int>> Q;
Q.push({0, N - 1});
vector<vector<int>> dist(N, vector<int>(N, -1));
dist[0][N - 1] = dist[N - 1][0] = 0;
while (!Q.empty()) {
auto [u, v] = Q.front(); Q.pop();
if (ans <= dist[u][v]) break;
if (u == v) {
ans = min(ans, 2 * dist[u][v]);
continue;
}
if (adj[u][v]) {
ans = min(ans, 2 * dist[u][v] + 1);
continue;
}
REP(i, 0, 26) {
for (auto x : G[u][i]) {
for (auto y : G[v][i]) {
if (dist[x][y] != -1) continue;
dist[x][y] = dist[y][x] = dist[u][v] + 1;
Q.push({x, y});
}
}
}
}
if (ans < (1 << 30)) cout << ans << endl;
else cout << -1 << endl;
return 0;
}
| #include<bits/stdc++.h>
#define maxn 1005
using namespace std;
vector<int> edge[1005][26];
int dp[1001][1001][27];
int mp[1001][1001];
int n,m;
int u,v; char c;
struct nod{
int u,v,c;
nod() {}
nod(int x,int y,int z) {u=x; v=y; c=z;}
};
nod q[27000005];
int head,tail;
void bfs()
{
dp[1][n][26]=1;
head=tail=1; q[1]=nod(1,n,26);
while (head<=tail)
{
nod tt=q[head++];
int u,v,c;
u=tt.u; v=tt.v; c=tt.c;
// cout<<u<<" "<<v<<" "<<c<<" "<<dp[u][v][c]<<endl;
if (c!=26)
{
int siz=edge[v][c].size();
for (int j=0;j<siz;j++)
{
int temp=edge[v][c][j];
if (dp[u][temp][26]==0)
{
dp[u][temp][26]=dp[u][v][c]+1;
q[++tail]=nod(u,temp,26);
}
}
}
else
{
for (int o=0;o<=25;o++)
{
int siz=edge[u][o].size();
for (int j=0;j<siz;j++)
{
int temp=edge[u][o][j];
if (dp[temp][v][o]==0)
{
dp[temp][v][o]=dp[u][v][c]+1;
q[++tail]=nod(temp,v,o);
}
}
}
}
}
long long ans=99999999;
for (int i=1;i<=n;i++)
if (dp[i][i][26]!=0)
{
ans=min(ans,dp[i][i][26]-1ll);
}
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (dp[i][j][26]!=0 && mp[i][j]==1)
{
ans=min(ans,dp[i][j][26]+0ll);
}
if (ans==99999999) cout<<-1<<endl; else cout<<ans<<endl;
return;
}
int main()
{
cin>>n>>m;
for (int i=1;i<=m;i++)
{
cin>>u>>v>>c;
int C=c-='a';
edge[u][C].push_back(v);
edge[v][C].push_back(u);
mp[u][v]=mp[v][u]=1;
}
bfs();
}
|
/*input
3
0 0
0 1
1 0
2 0
3 0
3 1
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("O2","unroll-loops","no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<ld,ld>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=1e18+1;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-3;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
inline ll mult(ll a,ll b){
if(a>=MOD) a%=MOD;
if(b>=MOD) b%=MOD;
return (a*b)%MOD;
}
inline ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b>>=1;
}
return res;
}
const int maxn=3e3+5;
const int maxlg=__lg(maxn)+2;
ll get(pii a){
return (a.f*a.f+a.s*a.s);
}
bool cmp(pii a,pii b){
if(atan2(a.s,a.f)==atan2(b.s,b.f)) return get(a)<get(b);
return atan2(a.s,a.f)<atan2(b.s,b.f);
}
ld cross(pii a,pii b){
if(sqrt((a.f*a.f+a.s*a.s)*(b.f*b.f+b.s*b.s))==0) return 1;
return (a.f*b.f+a.s*b.s)/sqrt((a.f*a.f+a.s*a.s)*(b.f*b.f+b.s*b.s));
}
int n;
void shift(pii a[]){
int x=0,y=0;
REP(i,n){
x+=a[i].f,y+=a[i].s;
a[i].f*=n,a[i].s*=n;
}
REP(i,n) a[i].f-=x,a[i].s-=y;
}
pii a[maxn],b[maxn];
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0);
cin>>n;
REP(i,n){
cin>>a[i].f>>a[i].s;
}
REP(i,n) cin>>b[i].f>>b[i].s;
shift(a),shift(b);
REP(i,n) if(a[i].f or a[i].s){
swap(a[i],a[0]);
swap(b[i],b[0]);
break;
}
REP(i,n){
ld z=atan2(b[i].s,b[i].f)-atan2(a[0].s,a[0].f);
bool wk=1;
ld s=sin(z),cc=cos(z);
REP(j,n){
pii tmp;
tmp.f=a[j].f*cc-a[j].s*s;
tmp.s=a[j].f*s+a[j].s*cc;
bool ok=0;
REP(k,n){
if(abs(tmp.f-b[k].f)<=eps and abs(tmp.s-b[k].s)<=eps) ok=1;
}
wk&=ok;
}
if(wk){
cout<<"Yes";
return 0;
}
}
cout<<"No";
} | #include <iostream>
#include <vector>
#include <set>
#include <math.h>
#include <algorithm>
#define MAXA 1000000
#define ll long long
using namespace std;
int GetNorm(int x,int y) {
return x*x+y*y;
}
int A[111];
int B[111];
int C[111];
int D[111];
bool Check(int N) {
if (N == 1) return true;
vector<pair<ll,ll>> right_pts;
for(int i=1;i<=N;i++) {
right_pts.push_back({C[i],D[i]});
}
sort(right_pts.begin(),right_pts.end());
int xans, yans, denomans;
denomans=-1;
for(int i=1;i<=N;i++) {
for(int j=1;j<=N;j++) {
if (i == j) continue;
ll dA = A[1]-A[2];
ll dB = B[1]-B[2];
ll dC = C[i]-C[j];
ll dD = D[i]-D[j];
// A[i]-A[j], B[j]-B[j]
if (GetNorm(dA, dB) != GetNorm(dC, dD)) {
continue;
}
ll x = dC*dA+dD*dB;
ll y = dA*dD-dC*dB;
ll denom = GetNorm(dA, dB);
// x/denom+j*y/denom
// (A[1]+j*B[1])*(x+j*y) + delta = C[i]+j*D[i]
ll delta_1 = C[i]*denom-(A[1]*x-y*B[1]);
ll delta_2 = D[i]*denom-(A[1]*y+x*B[1]);
vector<pair<ll,ll>> pts;
bool ok=true;
for(int k=1;k<=N;k++) {
// (A[i]+j*B[i])*(x+j*y) + delta
ll X = A[k]*x-B[k]*y+delta_1;
ll Y = A[k]*y+B[k]*x+delta_2;
if (X%denom != 0 || Y%denom != 0) ok=false;
pts.push_back({X/denom,Y/denom});
}
if (!ok) continue;
sort(pts.begin(),pts.end());
if (pts==right_pts) return true;
}
}
return false;
}
int main() {
int N;
cin>>N;
for(int i=1;i<=N;i++) {
cin>>A[i]>>B[i];
}
for(int i=1;i<=N;i++) {
cin>>C[i]>>D[i];
}
if (Check(N)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXNUM = 1000000000;
const int MaxSize = 1e5*3;
ll A[211000];
ll p[211000];
struct node {
int x,y;
};
node point[MaxSize];
bool cmp(node a,node b) {
if(a.x!=b.x) return a.x<b.x;
return a.y<b.y;
}
vector<int>vec[MaxSize];
int main() {
ll ans = 0;
int n; scanf("%d",&n);
// for(int i=1;i<=n;i++) scanf("%d",&A[i]);
// for(int i=1;i<=n;i++) {
// for(int j=1+i;j<=n;j++) {
// ans += abs(A[i]-A[j]);
// }
// }
for(int i=1;i<=n;i++) scanf("%lld",&A[i]);
sort(A+1,A+n+1);
p[1]=0;
for(int i=2;i<=n;i++) {
p[i] = (i-1)*(A[i]-A[i-1])+p[i-1];
}
for(int i=1;i<=n;i++) ans += p[i];
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
struct io_setup{
io_setup(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
}
} io_setup;
int main(){
int N; cin >> N;
vector<ll> A(2*N);
rep(i, 2*N) cin >> A[i];
vector<ll> B(2*N);
rep(i, N){
B[2*i] = A[N-1-i], B[2*i+1] = A[N+i];
}
priority_queue<ll, vector<ll>, greater<ll>> que;
ll S = accumulate(all(A), 0LL);
rep(i, N){
que.emplace(B[2*i]), que.emplace(B[2*i+1]);
S -= que.top(); que.pop();
}
cout << S << '\n';
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=100002;
int n,m;
int a[maxn];
signed main()
{
scanf("%lld%lld%lld",&a[1],&a[2],&a[3]);
sort(a+1,a+4);
cout<<a[2]+a[3];
return 0;
} | #include <bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
using ll = long long;
#define INF32 2147483647
const long long INF = 1LL << 60;
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;} return 0;}
template<class T> bool chmin(T &a,const T &b){if(b<a){a=b;return 1;} return 0;}
int main(){
double R,X,Y;
cin >> R >> X >> Y;
// // double RR = R * R;
// // double length = sqrt(X * X + Y * Y);
ll lengthlength = X * X + Y * Y;
// // cout << length << endl;
// if(lengthlength / (R * R) == 1){
// cout << 1 << endl;
// // cout << sqrt(lengthlength / (R * R)) << endl;
// }else if(lengthlength <= (R * R) * 4){
// cout << 2 << endl;
// }else{
// //cout << sqrt(lengthlength / (R * R)) << endl;
// cout << ceil(sqrt(lengthlength / (R * R))) << endl;
// }
// int max = sqrt(X * X + Y * Y) / R + 1;
// // cout << max << endl;
// for(int i=1;i<=max;i++){
// if(R * i <= length && length < R * (i + 1)){
// cout << i << endl;
// return 0;
// }
// }
// cout << "------------------------" << endl;
int ans = 0;
double max2 = sqrt(X * X + Y * Y) / R + 1;
// // cout << max2 << endl;
for(int i=1;i<=max2;i++){
if(R * R * i * i == lengthlength){
ans = i;
cout << ans << endl;
return 0;
}else if(lengthlength < R * R * (i + 1) * (i + 1)){
ans = i + 1;
cout << ans << endl;
return 0;
}
}
cout << ans << endl;
// double distance = sqrt(X * X + Y * Y);
// double distancedistance = X * X + Y * Y;
// if(distancedistance == R * R){
// cout << 1 << endl;
// }else if(distancedistance <= (2 * R) * (2 * R)){
// cout << 2 << endl;
// }else{
// cout << ceil(sqrt(distancedistance / (R * R))) << endl;
// }
// if(distance == R){
// cout << 1 << endl;
// }else if(distance <= (R + R)){
// cout << 2 << endl;
// }else{
// cout << ceil(distance / R) << endl;
// }
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++)cin >> a[i];
int ans = 1<<30;
for(int i = 0; i < 1<<(n-1); i++){
int orr = 0;
int xorr = 0;
for(int j = 0; j < n; j++){
orr |= a[j];
if(i>>j&1){
xorr ^= orr;
orr = 0;
}
}
xorr ^= orr;
ans = min(xorr,ans);
}
cout << ans << endl;
} | #include<bits/stdc++.h>
#define ll long long int
#define me9 1000000007
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin>>n;
ll a[n];
map<ll,ll> mp;
for(ll i=0;i<n;i++)
{
cin>>a[i];
mp[a[i]%200]++;
}
ll ans=0;
for(auto u: mp)
{
ans+=u.second*(u.second-1)/2;
}
cout<<ans;
return 0;
} |
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>
using namespace std;
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(12);
int n;
cin >> n;
vector<int> res(n + 1, 1);
for (int i = 1; i <= n; ++i) {
for (int j = i + i; j <= n; j += i) {
if (res[j] == res[i]) {
++res[j];
}
}
}
// for (int i = 1; i <= n; ++i) {
// for (int j = i + i; j <= n; j += i) {
// assert(res[j] != res[i]);
// }
// }
for (int i = 1; i <= n; ++i) {
cout << res[i] << ' ';
}
cout << '\n';
return 0;
} | #include<bits/stdc++.h>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define per(i,a,b) for(ll i=(a);i>=(b);i--)
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define next Cry_For_theMoon
#define il inline
#define pb(x) push_back(x)
#define mapit map<int,int>::iterator
#define vit vector<int>::iterator
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double db;
using namespace std;
const int MAXN=1e5+10;
int n,size[MAXN],ans;
int color[MAXN];
vector<int>v[MAXN];
int main(){
cin>>n;
rep(i,1,n){
sort(v[i].begin(),v[i].end());
color[i]=1;int pre=-1;
for(vit it=v[i].begin();it!=v[i].end();it++){
if(*it==pre)continue;
pre=*it;
if(color[i]==*it)color[i]++;
else break;
}
rep(j,2,n){
if(j*i>n)break;
v[j*i].pb(color[i]);
}
}
rep(i,1,n)printf("%d ",color[i]);
return 0;
}
|
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template<typename... T> void rd(T&... args) {((cin>>args), ...);}
template<typename... T> void wr(T... args) {((cout<<args<<" "), ...);cout<<endl;}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a, b;
cin>>a>>b;
auto cal=[](int a) {
int sum=0;
while (a) {
sum+=a%10;
a/=10;
}
return sum;
};
cout<<max(cal(a), cal(b));
return 0;
}
| // check whether to use mod or not while using power
// if string is given initialise n with length
// cout << fixed << setprecision(12) << ans << endl;... to print decinmal values accurately
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define float long double
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define all(a) (a).begin(), (a).end()
#define deb(x) cout<<#x<<" "<<x<<endl;
#define mem(v,x) memset(v,x,sizeof(v))
#define line cout<<endl;
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;
typedef set<ll> sll;
typedef priority_queue<ll> pqll;
typedef queue<ll> qll;
const ll mod = 1e9 + 7;
const ll inf = 1e16 + 7;
// const ll mxn = 1e3 + 7;
// vll adj[mxn];
ll power(ll a, ll b);
ll flcm(ll n, ll m);
ll fgcd(ll n, ll m);
ll modinv(ll x);
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
IOS
ll n, m, k, u, v, t = 1, temp, ans = 0, flag = 1, c = 0, min_el, max_el, sum_el;
string s, s1, s2;
// cin >> t;
while (t--) {
ans = 0;
c = 0;
flag = 1;
sum_el = 0;
cin >> n >> m;
ll sum1 = 0, sum2 = 0;
while (n != 0) {
sum1 += (n % 10);
n /= 10;
}
while (m != 0) {
sum2 += (m % 10);
m /= 10;
}
cout << max(sum1, sum2) << endl;
}
return 0;
}
ll power(ll a, ll b) {
ll res = 1;
while (b) {
if (b % 2) {
res = (res * a) % mod;
}
a = (a * a) % mod;
b /= 2;
}
return res;
}
ll flcm(ll n, ll m) {
ll temp = fgcd(n, m);
return (n * m) / temp;
}
ll fgcd(ll n, ll m) {
ll temp;
while (m != 0) {
temp = n;
n = m;
m = temp % m;
}
return n;
}
ll modinv(ll x) {
return power(x, mod - 2);
}
// ctrl j to combine lines
// ctrl l to select line
// ctrl shift arrows for moving selected lines |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define SP << " "
#define LLi long long int
using namespace std;
int imax=2147483647;
long long int llimax=9223372036854775807;
void PV(vector<int> pvv) {
rep(i, pvv.size()) cout << pvv[i] SP;
cout << endl;
}
int main(){
int h, w, n, m, a, b, prv, ans=0;
bool lt;
vector< vector<int> > v;
set<int> st;
cin>> h >> w >> n >> m;
v.resize(h+1);
rep(i, h){
v[i].resize(w+1);
v[i][w]=-1;
}
v[h].resize(w+1, -1);
rep(i, n){
cin >> a >> b;
v[a-1][b-1]=1;
}
rep(i, m){
cin >> a >> b;
v[a-1][b-1]=-1;
}
//rep(i, h+1) PV(v[i]);
//cout<< endl;
rep(i, h){
prv=0;
lt=false;
rep(j, w+1){
if(v[i][j]==1){
lt=true;
}
if(v[i][j]==-1){
if(lt){
for(int k=prv;k<j;k++){
if(v[i][k]!=1) v[i][k]=2;
}
}
lt=false;
prv=j+1;
}
}
}
//rep(i, h+1) PV(v[i]);
//cout<< endl;
rep(i, w){
prv=0;
lt=false;
rep(j, h+1){
//cout<< j << " " << i << " " << v[j][i] << " " << lt <<endl;
if(v[j][i]==1){
lt=true;
}
if(v[j][i]==-1){
if(lt){
for(int k=prv;k<j;k++){
if(v[k][i]!=1) v[k][i]=2;
}
}
lt=false;
prv=j+1;
}
}
}
//rep(i, h+1) PV(v[i]);
rep(i, h){
rep(j, w){
if(v[i][j]>0) ans++;
}
}
cout<< ans <<endl;
return 0;
} | #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
const ll mod = 1000000007;
int main(){
int n, m;
cin >> n >> m;
vector<P> q[n];
rep(i, m){
int u, v, c;
cin >> u >> v >> c;
u--; v--;
q[u].push_back(P(v, c));
q[v].push_back(P(u, c));
}
vector<int> visited(n);
vector<int> ans(n);
visited[0] = 1;
ans[0] = 1;
queue<P> que;
que.push(P(1, 0));
while(que.size() > 0){
P p = que.front();
que.pop();
int v = p.second;
int num = p.first;
rep(i, q[v].size()){
int u = q[v][i].first;
int nxt = q[v][i].second;
if(visited[u] == 1) continue;
if(ans[v] == nxt){
if(nxt == 1) nxt++;
else nxt--;
visited[u] = 1;
ans[u] = nxt;
que.push(P(nxt, u));
}else{
ans[u] = nxt;
visited[u] = 1;
ans[u] = nxt;
que.push(P(nxt, u));
}
}
}
rep(i, n){
cout << ans[i] << endl;
}
} |
//Never stop trying
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef string str;
typedef long long ll;
#define int ll
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
const int MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const int MX = 2e5 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
#define dbg(x) cerr << " - " << #x << " : " << x << endl;
#define dbgs(x,y) cerr << " - " << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << " - " << #v << " : " << endl << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
void IO() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int N,M;
int X[10][10];
int memo[10];
int solve(int i){
if(memo[i]!=-1) return memo[i];
int ans=0;
FOR(j,i+1,N) ckmax(ans,solve(j)+X[i][j]);
return memo[i]=ans;
}
int32_t main() {
boost; IO();
cin>>N>>M;
vi w(N);
FOR(i,0,N) cin>>w[i];
sort(all(w));
vpi vec;
int mn_v=INF;
FOR(i,0,M){
int l,v; cin>>l>>v;
ckmin(mn_v,v);
vec.pb({v,l});
}
vec.pb({0,0});
sort(all(vec));
int mx[sz(vec)];
FOR(i,0,sz(vec)){
mx[i]=vec[i].se;
if(i) ckmax(mx[i],mx[i-1]);
}
if(mn_v<w[N-1]){
cout << -1 << endl;
return 0;
}
int ans=INF;
do{
int pref[N];
FOR(i,0,N){
pref[i]=w[i];
if(i) pref[i]+=pref[i-1];
}
FOR(i,0,N) FOR(j,i+1,N){
int W=pref[j]; if(i) W-=pref[i-1];
int idx; //max i such that vec[i].fi<W
int l=0,r=sz(vec)-1;
while(l<=r){
int m=(l+r)/2;
if(vec[m].fi<W){
idx=m; l=m+1;
}
else r=m-1;
}
X[i][j]=mx[idx];
}
memset(memo,-1,sizeof(memo));
ckmin(ans,solve(0));
}while(next_permutation(all(w)));
cout << ans << endl;
return 0;
}
/* Careful!!!
.Array bounds
.Infinite loops
.Uninitialized variables / empty containers
.Order of input
Some insights:
.Binary search
.Graph representation
.Write brute force code
.Change your approach
*/
| #include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<ctime>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<bitset>
#define sqr(x) ((x)*(x))
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define fzin fz1(i,n)
#define fzim fz1(i,m)
#define fzjn fz1(j,n)
#define fzjm fz1(j,m)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();++itr)
#define pb push_back
#define mk make_pair
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
#define spln(i,n) (i==n?'\n':' ')
#define fac_init(n){fac[0]=fac[1]=inv[1]=fi[0]=fi[1]=1;fz(i,2,n){fac[i]=1ll*fac[i-1]*i%mod;inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;fi[i]=1ll*fi[i-1]*inv[i]%mod;}}
using namespace std;
inline void read(int &x)
{
char c;int f=1;
while(!isdigit(c=getchar()))if(c=='-')f=-1;
x=(c&15);while(isdigit(c=getchar()))x=(x<<1)+(x<<3)+(c&15);
x*=f;
}
int n,m,i,j,k,g[105][105];
double ans;
int main()
{
cin>>n;
fz1(i,n){
fz1(j,n){
char c;
cin>>c;
g[i][j]=((i==j)||(c&1));
}
}
fz1(k,n){
fz1(i,n)fz1(j,n)g[i][j]|=(g[i][k]&g[k][j]);
}
fz1(i,n){
int s=0;
fz1(j,n)s+=g[j][i];
ans+=1.0/s;
}
printf("%.12lf\n",ans);
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for(int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for(int i = int(ini); i >= int(lim); i--)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
const int MAX = 2e5 + 5;
vector<int> adj[MAX];
int h[MAX], order[MAX], p[MAX];
bool covered[MAX];
int n, k;
void calc_h(int s) {
for (int u : adj[s]) {
if (u != p[s]) {
h[u] = h[s] + 1;
p[u] = s;
calc_h(u);
}
}
}
void cover(int u, const int max_d, int cur_d = 0, int prev = -1) {
covered[u] = true;
if (cur_d == max_d) return;
for (int v : adj[u]) {
if (v == prev) continue;
cover(v, max_d, cur_d + 1, u);
}
}
bool ok(int d) {
memset(covered, false, sizeof covered);
int need = 0;
fori (i, 0, n) {
int u = order[i];
if (covered[u]) continue;
int y = u;
while (y != 1 && h[u] - h[y] < d) {
y = p[y];
}
cover(y, d);
need++;
if (need > k) return false;
}
return true;
}
void solve() {
cin >> n >> k;
fori (i, 0, n - 1) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
calc_h(1);
iota(order, order + n, 1);
sort(order, order + n, [&] (int lhs, int rhs) {
return h[lhs] > h[rhs];
});
int left = 0, right = n;
int ans = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (ok(mid)) {
right = mid - 1;
ans = mid;
}
else {
left = mid + 1;
}
}
assert(ans != -1);
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| #include<bits/stdc++.h>
#define N 200005
#define Ms(a,b) memset(a,b,sizeof a)
#define db(x) cerr<<#x<<"="<<x<<endl;
#define db2(x,y) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define db3(x,y,z) cerr<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl;
using namespace std;
int rd(){
int res=0,c,f=0; while(!isdigit(c=getchar()))f=c=='-';
do res=(res<<1)+(res<<3)+(c^48);
while(isdigit(c=getchar()));
return f?-res:res;
}
struct edge{int to,nxt;}e[N<<1];
int n,knum,head[N];
void link(int s,int t,int n){
e[n]=(edge)<%t,head[s]%>;
head[s]=n;
}
int v[N],dep[N];
struct cmp{bool operator ()(const int &a,const int &b){return dep[a]>dep[b];}};
int ffa[N][18];
void dfs(int x,int f,int d){
ffa[x][0]=f,dep[x]=d;
for(int i=1;1<<i<=n;i++)ffa[x][i]=ffa[ffa[x][i-1]][i-1];
for(int i=head[x];i;i=e[i].nxt){
int to=e[i].to;
if(to==f)continue;
dfs(to,x,d+1);
}
}
int dis[N];
int Fa(int x,int d){
for(int i=0;1<<i<=d;i++)if(1<<i&d)x=ffa[x][i];
return !x?1:x;
}
void Mark(int x,int d){
if(dis[x]==d)return;
for(int i=head[x];i;i=e[i].nxt){
int to=e[i].to;
if(~dis[to]&&dis[to]<=dis[x]+1)continue;
dis[to]=dis[x]+1,Mark(to,d);
}
}
bool check(int k){
int now=0;
Ms(dis,-1);
for(int i=1;i<=n;i++){
int x=v[i];
if(~dis[x])continue;
++now,x=Fa(x,k),dis[x]=0,Mark(x,k);
}
return now<=knum;
}
int main(){
n=rd(),knum=rd();
for(int i=1;i<n;i++){
int s=rd(),t=rd();
link(s,t,i<<1);
link(t,s,i<<1|1);
}
dfs(1,0,1);
for(int i=1;i<=n;i++)v[i]=i;
sort(v+1,v+1+n,cmp());
int l=1,r=n,res=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid))r=mid-1,res=mid;
else l=mid+1;
}
printf("%d\n",res);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
//constexpr long long MOD = 1000000007;
constexpr long long MOD = 998244353;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(){
ll n, k; cin >> n >> k;
vector<ll> C(n);
rep(i, n){
ll a; cin >> a; C[a]++;
}
vector<ll> box(k); ll last = k;
rep(i, n){
last = min(last, C[i]);
rep(j, last)box[j] = i+1;
}
ll res = accumulate(all(box), 0LL);
cout << res << ln;
} | #include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
map<ll,ll> mp;
ll a[maxn],f[maxn];
int main(){
ll n,k;
cin >> n >> k;
ll cnt = 1;
ll res=0,st=0;
for(ll i=1;i<=n;i++){
ll k;
cin>>k;
if(f[k]==0){
a[cnt++] =k;
f[k] =1;
}
mp[k]++;
}
sort(a+1,a+cnt);
while(mp[0]){
ll i=0;
while(1){
if(mp[i]!=0){
mp[i]--;
}
else{
res+=i;
st++;
break;
}
i++;
}
if(st == k) break;
}
cout << res;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep( i, n, m ) for(int i = ( n ); i < ( m ); i++)
#define rep_d( i, n, m ) for(int i = ( n ) - 1; i >= ( m ); i--)
#define sort_asc( X ) sort(( X ).begin(), ( X ).end())
#define sort_desc( X ) sort(( X ).begin(), ( X ).end(), greater <>())
#define endl "\n"
template <class T> bool chmax( T& a, T b );
template <class T> bool chmin( T& a, T b );
int* eratosthenes( int N );
bool bit_search( int pattern, int N );
void initialize( void );
const ll INF = 1LL << 60;
const int k_mod = 1e9 + 7;
template <typename T, typename K> struct P {
T a; K b;
};
typedef struct {
vector <int> to;
int cost;
} edge;
int main( void ){
// initialize();
int N;
cin >> N;
map<string, int> mp;
rep(i,0,N){
string s; cin >> s;
if (s[0] == '!'){
s = s.substr(1);
mp[s] |= 1;
}
else
mp[s] |= 2;
}
for(auto m:mp){
if (m.second == 3){
cout << m.first << endl;
return 0;
}
}
cout << "satisfiable\n";
}
template <class T> bool chmax( T& a, T b ) {
if (a < b){
a = b; return true;
}
return false;
}
template <class T> bool chmin( T& a, T b ) {
if (a > b){
a = b; return true;
}
return false;
}
int* eratosthenes( int N ){
int* prime_array = new int[ N + 1 ];
int lim = N;
prime_array[ 0 ] = 0;
prime_array[ 1 ] = 0;
rep( i, 2, N + 1 ){
prime_array[ i ] = 1;
}
rep( i, 2, lim ){
if (prime_array[ i ] == 0)
continue;
lim = N / i;
for(int j = i * 2; j < N + 1; j += i){
prime_array[ j ] = 0;
}
}
return prime_array;
}
bool bit_search( int pattern, int N ){
int cnt = 0;
rep( bit, 0, N ){
if (pattern & ( 1 << bit )){
cnt++;
}
}
return true;
}
void initialize( void ){
cout << fixed << setprecision( 10 );
}
| #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
ll L[200200], R[200200];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep1(i, N) L[i] = 1e12;
rep1(i, N) R[i] = -1e12;
rep(i, N) {
ll x, c;
cin >> x >> c;
chmin(L[c], x);
chmax(R[c], x);
}
ll dp0 = 0, dp1 = 0;
ll za0 = 0, za1 = 0;
rep1(i, N) {
if (L[i] > 1e10) continue;
ll tugi0 = 1e18, tugi1 = 1e18;
chmin(tugi1, dp0 + abs(L[i] - za0) + abs(L[i] - R[i]));
chmin(tugi0, dp0 + abs(R[i] - za0) + abs(L[i] - R[i]));
chmin(tugi1, dp1 + abs(L[i] - za1) + abs(L[i] - R[i]));
chmin(tugi0, dp1 + abs(R[i] - za1) + abs(L[i] - R[i]));
dp0 = tugi0;
dp1 = tugi1;
za0 = L[i];
za1 = R[i];
}
co(min(dp0 + abs(za0), dp1 + abs(za1)));
Would you please return 0;
} |
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main ()
{
int N;
cin >> N;
vector<int> data(N);
rep(i, N)
{
cin >> data[i];
}
int ans = (1 << 30) + 1;
for (int tmp = 0; tmp < (1 << N-1); tmp++)
{
bitset<20> S(tmp);
S = (S << 1);
vector<int> V(N);
int cnt = 0;
for (int i = 0; i < N; i++)
{
if (S.test(i))
{
cnt++;
}
V[cnt] |= data[i];
}
int XOR_V = 0;
rep(i, N)
{
XOR_V ^= V[i];
}
ans = min(ans, XOR_V);
}
cout << ans << endl;
return 0;
} | /*
author : aryan57
created : 27-June-2021 17:04:11 IST
*/
#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...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define int long long
#define X first
#define Y second
#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define F(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)
const int mxn = 1e5;
const long long INF = 2e18;
const int32_t M = 1000000007;
// const int32_t M = 998244353;
const long double pie = acos(-1);
void solve_LOG()
{
int n;
cin>>n;
vector <int> v(n+1);
vector <int> pre(n+1);
F(i,1,n)
{
cin>>v[i];
pre[i]+=pre[i-1]+v[i];
}
vector< vector<int> > dp_sum(n+1,vector <int> (n));
vector< vector<int> > dp(n+1,vector <int> (n+1));
dp[1][1]=1;
int ans=0;
F(i,1,n)
{
F(j,1,n)
{
if(j==1)
{
dp[i][j]=1;
}
else
{
dp[i][j]=dp_sum[j-1][pre[i]%j];
}
if(i==n)
{
ans+=dp[i][j];
ans%=M;
}
}
F(j,1,n-1)
{
dp_sum[j][pre[i]%(j+1)]+=dp[i][j];
dp_sum[j][pre[i]%(j+1)]%=M;
}
}
cout<<ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
#ifdef ARYAN_SIEVE
sieve();
#endif
#ifdef ARYAN_SEG_SIEVE
segmented_sieve();
#endif
#ifdef ARYAN_FACT
fact_init();
#endif
// cout<<fixed<<setprecision(10);
int _t=1;
// cin>>_t;
for (int i=1;i<=_t;i++)
{
// cout<<"Case #"<<i<<": ";
solve_LOG();
}
return 0;
}
// parsed : 27-June-2021 17:03:51 IST |
#include <bits/stdc++.h>
using namespace std;
int n,m=INT_MAX,cnt;
vector<int>a;
set<int>d;
int main(){
ios::sync_with_stdio(0);
cin>>n;
for(int i=1,x;i<=n;i++)cin>>x,a.push_back(x);
for(int i=0;i<a.size();i++){
m=min(m,a[i]);
for(int j=1;j*j<=a[i];j++)
if(a[i]%j==0)d.insert(j),d.insert(a[i]/j);
}
for(set<int>::iterator it=d.begin();it!=d.end();it++){
int i=*it;
if(i<=m){
int g=0;
for(int j=0;j<a.size();j++)
if(a[j]%i==0)g=__gcd(g,a[j]);
if(g==i)cnt++;
}
}
cout<<cnt<<endl;
return 0;
} | #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
#define rep(i, N) for (int i = 0; i < N; i++)
#define repi(i, ini, N) for(int i = ini; i < N; i++)
#define chmax(i, j) i = max(i, j)
#define chmin(i, j) i = min(i, j)
typedef long long ll;
int N;
int main() {
vector<pair<int, string> > v;
cin >> N;
rep(i, N){
int T;
string S;
cin >> S >> T;
v.push_back(make_pair(T, S));
}
sort(v.begin(), v.end());
cout << v[v.size()-2].second << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N, K;
cin >> N >> K;
// vector<ll> A(N), B(N);
vector<pair<ll, ll>> friends(N);
for (int i = 0; i < N; ++i) {
cin >> friends.at(i).first >> friends.at(i).second;
// cin >> A.at(i) >> B.at(i);
}
sort(friends.begin(), friends.end());
ll money = K;
ll pos = 0;
for (int i = 0; i < N; ++i) {
// if(pos + money < A.at(i)){
// cout << pos + money << endl;
// return 0;
// }else{
// money = money - (A.at(i) - pos) + B.at(i);
// pos = A.at(i);
// }
if(pos + money < friends.at(i).first){
cout << pos + money << endl;
return 0;
}else{
money = money - (friends.at(i).first - pos) + friends.at(i).second;
pos = friends.at(i).first;
}
}
cout << pos + money << endl;
return 0;
}
| #include <bits/stdc++.h>
#define pb(x) push_back(x)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F(w,x,y) for(ll w=x; w<y; w++)
#define endl "\n"
#define mod 1000000007
typedef long long ll;
typedef long double ld;
using namespace std;
void play()
{
ll n,k;
cin>>n>>k;
ll sum=k;
vector <pair<ll,ll>> v(n);
F(i,0,n)
{
cin>>v[i].first>>v[i].second;
}
sort(all(v));
F(i,0,n)
{
if(sum<v[i].first)
break;
else
sum+=v[i].second;
}
cout<<sum<<endl;
}
int main()
{
//freopen("milkvisits.in","r",stdin);
//freopen("milkvisits.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//pre();
ll t;
t=1;
//cin>>t;
F(i,1,t+1)
{
//cout<<"Case #"<<i<<": ";
play();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define For(i,a,b) for(register int i=a;i<=b;++i)
#define Down(i,a,b) for(register int i=a;i>=b;i--)
#define reg register
inline int min(int x,int y){return x<y?x:y;}
inline int max(int x,int y){return x>y?x:y;}
inline void swap(int &x,int &y){int t=x; x=y; y=t; return ;}
namespace yspm{
inline int read(){
int res=0,f=1; char k;
while(!isdigit(k=getchar())) if(k=='-') f=-1;
while(isdigit(k)) res=res*10+k-'0',k=getchar();
return res*f;
}
int mod;
int p[10]={1,1,4,4,2,1,1,4,4,2};
int bas[10]={0,1,6,1,6,5,6,1,6,1};
inline int ksm(int x,int y){
int res=1%mod; x%=mod; for(;y;y>>=1,x=x*x%mod) if(y&1) res=res*x%mod;
return res;
}
signed main(){
int a=read()%10,b=read(),c=read();
mod=p[a]; int ans=bas[a];
int res=ksm(b,c);
for(reg int i=1;i<=res;++i) ans=ans*a%10;
cout<<ans<<endl;
return 0;
}
}
signed main(){return yspm::main();}
| #include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 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--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 400005
int n;
int a[MAXN];
priority_queue<ii> pq;
char type[2];
bool impt[MAXN];
string ans;
int main() {
scanf("%d", &n);
REP (i, 0, n * 2) {
scanf("%d", &a[i]);
pq.push(MP(a[i], i));
}
REP (i, 0, n) {
auto [_, id] = pq.top(); pq.pop();
impt[id] = 1;
}
int cnt = 0;
REP (i, 0, n * 2) {
if (cnt == 0) {
type[impt[i]] = '(';
type[1 - impt[i]] = ')';
}
ans += type[impt[i]];
if (type[impt[i]] == '(') {
cnt++;
} else {
cnt--;
}
}
printf("%s\n", ans.c_str());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = a; i < (b);++i)
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
typedef long long ll;
const int MAX_N = 1e5+1;
ll a[MAX_N];
int main(){
cin.tie(0)->sync_with_stdio(false);
cin.exceptions(cin.failbit);
int n,q;
cin>>n>>q;
for (int i = 0; i < n; i++){
cin>>a[i];
}
sort(a,a+n);
ll k;
for (int i = 0; i < q; i++){
cin>>k;
ll lb = 0;ll ub = 1e18+1e6;
while (lb < ub -1){
ll mid = (lb+ub)/2;
ll d = lower_bound(a,a+n,mid) - a;
if(mid - d > k){
ub = mid;
}else{
lb = mid;
}
}
cout<<lb<<endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
void solve() {
ll N, Q;
cin >> N >> Q;
vector<ll> A(N);
for (size_t i = 0; i < N; i++)
{
cin >> A[i];
}
vector<ll> exceptA(N);
ll pre = 0;
for (ll i = 0; i < N; i++)
{
exceptA[i] = A[i] - i - 1;
}
vector<ll> ans;
for (size_t i = 0; i < Q; i++)
{
ll k;
cin >> k;
if (k < A[0]) {
ans.emplace_back(k);
continue;
}
ll index = lower_bound(exceptA.begin(), exceptA.end(), k) - exceptA.begin();
ll plus = k - exceptA[index - 1];
ll ex_num = A[index - 1] + plus;
ans.emplace_back(ex_num);
}
for (auto v : ans)cout << v << endl;
//return 0;
}
int main()
{
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#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)
int main()
{
lint v,t,s,d;
cin >> v >> t >> s >> d;
cout << (!(v*t<=d&&d<=v*s)?"Yes":"No") << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int V, T, S, D;
int main() {
scanf("%d%d%d%d", &V, &T, &S, &D);
printf(D < V * T || D > V * S ? "Yes" : "No");
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
ll N,C;
cin >> N >> C;
map<int,ll> P;
for(int i=0;i<N;i++){
int A,B,C;
cin >> A >> B >> C;
P[A]+=C;
P[B+1]-=C;
}
ll S=0;
ll ans=0;
ll prev=0;
for(auto it=P.begin();it!=P.end();it++){
ll A=(*it).first;
ll B=(*it).second;
ans+=min(C,S)*(A-prev);
prev=A;
S+=B;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <vector>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <chrono>
#include <thread>
#include <iomanip>
#include <typeinfo>
//define
#define trav(i,v) for(auto i: v)
#define rep(i,n) for(int i=0;i<n;i++)
#define repu(i,k,n) for(int i=k;i<=n;i++)
#define repd(i,k,n) for(int i=k;i>=n;i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define itn int
//typedef
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector< pii > vii;
typedef std::vector< ll > vll;
typedef std::vector< std::pair < pii, int > > vpp;
const long long MOD = 1000000007;
// const long long MOD = 998244353;
const long long inf = 1000000000000000000;
using namespace std;
ll modpow(ll x,ll n){
ll res=1;
while(n>0){
if(n&1) res=res*x%MOD;
x=x*x%MOD;
n>>=1;
}
return res;
}
ll power(ll x, ll n)
{
ll res=1;
while(n>0){
if(n&1) res=res*x;
x=x*x;
n>>=1;
}
return res;
}
void init()
{
}
void solve()
{
int n;
cin>>n;
ll c;
cin>>c;
map<int, ll> mm1, mm2;
vi v1, v2;
vector<pair<ll, ll>> v;
// vecot v1;
rep(i,n)
{
int x, y, z;
cin>>x>>y>>z;
if(mm1[x]==0)
v1.pb(x);
mm1[x]+=z;
if(mm1[y+1]==0)
v1.pb(y+1);
mm1[y+1]-=z;
}
trav(x,v1)
{
v.pb(mp(x,mm1[x]));
}
sort(all(v));
ll cost = v[0].se;
ll ans = 0;
repu(i,1,v.size()-1)
{
ans += (v[i].fi-v[i-1].fi)*min(cost,c);
cost += v[i].se;
}
cout<<ans<<endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int t = 1, tt=1;
// cin>>t;
init();
while(tt<=t)
{
// cout<<"Case #"<<tt<<": ";
solve();
tt++;
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
} |
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<bitset>
#include<functional>
#include <fstream>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define rep3(i, x, y) for(ll i = x; i < y; i++)
#define rep4(i, x) for(ll i = x; i >= 0; i--)
#define all(a) (a).begin(),(a).end()
#define puts(x) cout << (x) << "\n"
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
//const ld EPS = 1e-9;
bool isprime(int p) {
if (p == 1) return false;
for (int i = 2; i < p; i++) {
if (p % i == 0) return false;
}
return true;
}
ll gcd(ll a, ll b) {
if (b == 0) { return a; }
return gcd(b, a % b);
}
//ax + by = cが整数解をもつための必要十分条件は c が gcd(a,b) で割り切れること。
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
//main関数内に extGCD(a, b, x, y); でx, yに解が格納
ll extGCD(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll keta(ll n) {
ll res = 0;
while (n >= 1) {
res += n % 10; n /= 10;
}
return res;
}
ll modpow(ll x, ll y, ll m) {
ll res = 1;
while (y) {
if (y % 2) { res *= x; res %= m; }
x = x * x % m; y /= 2;
}
return res;
}
ll kaijyo[600005];
void nCkinit(ll n, ll m) {
ll cnt = 1; kaijyo[0] = 1;
for (int h = 1; h <= n; h++) { cnt *= h; cnt %= m; kaijyo[h] = cnt; }
}
ll nCk(ll n, ll k, ll m) {
/*ll a = 1, b = 1;
for (int h = 1; h <= n; h++) { a *= h; a %= m; }
for (int h = 1; h <= k; h++) { b *= h; b %= m; }
for (int h = 1; h <= n - k; h++) { b *= h; b %= m; }*/
ll a = kaijyo[n], b = kaijyo[k] * kaijyo[n - k] % m;
return a * modpow(b, m - 2, m) % m;
}
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
typedef pair <ll, string> pp;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
struct edge { ll to, cost; };
struct Point {
ll x, y;
};
bool operator<(const Point& a1, const Point& a2) {
if (a1.x < a2.x) return true;
if (a1.x > a2.x) return false;
if (a1.y < a2.y) return true;
return false;
}
struct status {
ll cost;
ll x;
ll y;
bool operator<(const status& rhs) const { return cost < rhs.cost; };
bool operator>(const status& rhs) const { return cost > rhs.cost; };
};
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
//cout << fixed << setprecision(15);
ll a, b; cin >> a >> b;
if (a == b) { cout << a << endl; }
else { cout << 3 - a - b << endl; }
return 0;
} | // Problem: D - Kth Excluded
// Contest: AtCoder - AtCoder Beginner Contest 205
// URL: https://atcoder.jp/contests/abc205/tasks/abc205_d
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
/*
███████╗██╗░░░██╗░█████╗░██╗░░██╗██╗███╗░░██╗ ███╗░░██╗░█████╗░██████╗░░█████╗░██████╗░██╗░░░██╗
██╔════╝██║░░░██║██╔══██╗██║░██╔╝██║████╗░██║ ████╗░██║██╔══██╗██╔══██╗██╔══██╗██╔══██╗╚██╗░██╔╝
█████╗░░██║░░░██║██║░░╚═╝█████═╝░██║██╔██╗██║ ██╔██╗██║██║░░██║██████╦╝██║░░██║██║░░██║░╚████╔╝░
██╔══╝░░██║░░░██║██║░░██╗██╔═██╗░██║██║╚████║ ██║╚████║██║░░██║██╔══██╗██║░░██║██║░░██║░░╚██╔╝░░
██║░░░░░╚██████╔╝╚█████╔╝██║░╚██╗██║██║░╚███║ ██║░╚███║╚█████╔╝██████╦╝╚█████╔╝██████╔╝░░░██║░░░
╚═╝░░░░░░╚═════╝░░╚════╝░╚═╝░░╚═╝╚═╝╚═╝░░╚══╝ ╚═╝░░╚══╝░╚════╝░╚═════╝░░╚════╝░╚═════╝░░░░╚═╝░░░
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define pb push_back
#define fi first
#define se second
#define endl "\n"
#define all(v) v.begin(), v.end()
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<VI> VVI;
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
void YES(bool t = 1) { cout << YESNO[t] << endl; }
void Yes(bool t = 1) { cout << YesNo[t] << endl; }
void yes(bool t = 1) { cout << yesno[t] << endl; }
const int mod = 1e9+7;
const int mxN = 2e6 + 5;
int n,q,m;
int a[mxN],b[mxN];
string s,t;
void code()
{
cin>>n>>q;
// code goes here
for(int i=0;i<n;i++) cin>>a[i];
while(q--){
cin>>m;
int numbers = 0;
int prev = 0;
while(upper_bound(a,a+n,m+numbers)-a!=prev){
prev = upper_bound(a,a+n,m+numbers)-a;
numbers = prev;
}
cout<<m+prev<<endl;
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
// cin>>t;
while(t--) code();
} |
#include <bits/stdc++.h>
using namespace std;
bool chk(string &s,int index){
int i=0;
int j= index;
while(j>=i){
if(s[i] != s[j])
return false;
i++;
j--;
}
return true;
}
int main() {
int n;
cin>>n;
string s = to_string(n);
if(chk(s, s.size()-1))
cout<<"Yes";
else{
int i=s.size()-1;
for(;i>=0;i--){
if(s[i]!='0')
break;
}
if(i == s.size()-1)
cout<<"No";
else{
if(chk(s,i))
cout<<"Yes";
else
cout<<"No";
}
}
}
| #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}
//おちこんだりもしたけれど、私はげんきです。
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin>>N;
vector<ll> fib={1,1};
int k=1;
while(fib[k]+fib[k-1]<=N){
fib.push_back(fib[k]+fib[k-1]);
k++;
}
int M=fib.size();
vector<int> c(M);
for(int i=M-1;i>0;i--){
while(N>=fib[i]){
c[i]++;
N-=fib[i];
}
}
vector<ll> ans;
for(int i=1;i<M;i++){
int a=1,b=4;
if((M-i)%2==1){
a=2;
b=3;
}
rep(j,c[M-i]) ans.push_back(a);
ans.push_back(b);
}
cout<<ans.size()<<"\n";
rep(i,ans.size()){
cout<<ans[i]<<"\n";
}
/*ll X=0,Y=0;
rep(i,ans.size()){
if(ans[i]==1) X++;
if(ans[i]==2) Y++;
if(ans[i]==3) X+=Y;
if(ans[i]==4) Y+=X;
cout<<X<<" "<<Y<<endl;
}*/
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt=1;
//cin >> tt;
while(tt--)
{
int n;
cin >> n;
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];
pair<int,int> odd[n/2], even[n/2];
for(int i=0;i<n;i++)
{
if(i%2==0)
even[i/2]={a[i]-b[i],i};
else
odd[i/2]={a[i]-b[i],i};
}
sort(even,even+n/2);
sort(odd,odd+n/2);
int ans=0;
for(int i=n/2-1;i>=0;i--)
{
if(even[i].first+odd[i].first>=0)
{
ans+=a[even[i].second]+a[odd[i].second];
}
else
{
ans+=b[even[i].second]+b[odd[i].second];
}
}
cout << ans << endl;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
namespace Ruri{
#define ms(a,b) memset(a,b,sizeof(a))
#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define ce(i,r) i==r?'\n':' '
#define pb push_back
#define all(x) x.begin(),x.end()
#define gmn(a,b) a=min(a,b)
#define gmx(a,b) a=max(a,b)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int infi=1e9;//infi较大,注意涉及inf相加时爆int
const ll infl=4e18;
inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Ruri;
namespace Read{
#define ss(a) scanf("%s",a)
inline int ri(){ int x; scanf("%d",&x); return x; }
inline ll rl(){ ll x; scanf("%lld",&x); return x; }
inline db rd(){ db x; scanf("%lf",&x); return x; }
}
namespace DeBug{
#define pr(x) cout<<#x<<": "<<(x)<<endl
#define pra(x,a,b) cout<<#x<<": "<<endl; \
repi(i,a,b) cout<<x[i]<<" "; \
puts("");
#define FR(a) freopen(a,"r",stdin)
#define FO(a) freopen(a,"w",stdout)
}
using namespace Read;
using namespace DeBug;
const int MAX_N=1e5+5;
int n;
ll a[MAX_N],b[MAX_N];
vector<ll> rec[2];
int main()
{
n=ri();
ll ans=0;
repi(i,1,n) a[i]=ri();
repi(i,1,n) b[i]=ri(),ans+=b[i];
repi(i,1,n) rec[i&1].pb(a[i]-b[i]);
sort(all(rec[0])),sort(all(rec[1]));
repi(i,0,n/2-1) ans+=max(0ll,rec[0][i]+rec[1][i]);
printf("%lld\n",ans);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
int main()
{
cin>>n>>a>>b;
cout<<n-a+b;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int sled0[500005];
int sled1[500005];
int mvs[500005];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
string s, t;
cin >> s >> t;
string aa = s, bb = t;
sort(aa.begin(), aa.end());
sort(bb.begin(), bb.end());
if(aa != bb){
cout << -1;
return 0;
}
vector <int> p;
vector <int> d;
for(int i=0; i<n; i++){
if(s[i] == '0') p.push_back(i);
if(t[i] == '0') d.push_back(i);
}
int res = 0;
for(int i=0; i<p.size(); i++){
if(p[i] != d[i]) res++;
}
cout << res;
return 0;
}
|
//==================================================================//
#define _CRT_SECURE_NO_WARNINGS
//==================================================================//
#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 sz(s) (int)s.size()
#define clr(x,y) memset(x,y,sizeof(x))
#define all(v) ((v).begin()), ((v).end())
#define RT(x) return cout<<x,0;
#define endl '\n'
#define INF 0x3f3f3f3f3f3f3f3fLL
#define OO 0x3f3f3f3f
#define watch(x) cout<<#x<<" = { "<<x<<" }\n"
#define popcount(n) __builtin_popcount(n)
#pragma optimization_level 3
#define lft n<<1
#define ryt n<<1|1
#pragma GCC optimize("Ofast,no-stack-protector,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//==================================================================//
//==================================================================//
const int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
const int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
template<typename T>
using Graph = vector<vector<T>>;
typedef long long ll;
typedef unsigned long long ull;
//==================================================================//
//==================================================================//
void printDouble(double f, int p) {
cout << fixed;
cout << setprecision(p) << f << endl;
}
int Set(int N, int cur) { return N = N | (1 << cur); }
int Reset(int N, int cur) { return N = N & ~(1 << cur); }
bool Check(int N, int cur) { return (bool)(N & (1 << cur)); }
ll GCD(ll a, ll b) { if (b == 0) return a; return GCD(b, a % b); }
ll LCM(ll a, ll b) { return (a / GCD(a, b)) * b; }
ll POW(ll a, ll p) {
ll res = 1, x = a; while (p) {
if (p & 1)
res = (res * x); x = (x * x); p >>= 1;
} return res;
}
//==================================================================//
//==================================================================//
void run() {
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#else
//freopen("restore.in", "r", stdin);
#endif
}
#define rep(i, n) for (int i = 0; i < (n); ++i)
//==================================================================//
const int N = 1e5 + 5, M = 40, mod = 1e9 + 7;
int main() {
run();
double x,y;
cin>>x>>y;
double ans = x-y;
ans/=x;
cout<<fixed<<setprecision(3);
cout<<ans*100.0;
}
| // Author: wlzhouzhuan
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fir first
#define sec second
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, t) memset(s, t, sizeof(s))
#define mcpy(s, t) memcpy(s, t, sizeof(t))
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
print(x), putchar(let);
}
vector<int> ans;
int main() {
ans.pb(6), ans.pb(10), ans.pb(15);
for (int i = 12; i <= 10000; i += 6) ans.pb(i);
for (int i = 20; i <= 10000; i += 10) {
if (i % 6 != 0) ans.pb(i);
}
for (int i = 30; i <= 10000; i += 15) {
if (i % 6 != 0 && i % 10 != 0) {
ans.pb(i);
}
}
int n = read();
for (int i = 0; i < n; i++) print(ans[i], ' ');
} |
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
void get(ll i, ll arr[], ll n, ll xo, ll o, ll &ans)
{
if(i == n)
{
ans = min(ans, xo^o);
return;
}
get(i+1, arr, n, xo, o|arr[i], ans);
get(i+1, arr, n, xo^o, arr[i], ans);
}
const ld PI = acos(-1);
int main()
{
FAST/**/
ll n;
cin>>n;
pii arr[n];
rep(n)
cin>>arr[i].ff>>arr[i].ss;
vector<ll> hold[n+1];
rep(n)
hold[arr[i].ss].pb(arr[i].ff);
rep(n+1)
sort(hold[i].begin(), hold[i].end());
hold[0].pb(0);
map<ll,ll> ma[n+1];
ma[0][0] = 0;
map<ll, ll> prev = ma[0];
for(ll i=1;i<=n;i++)
{
if(hold[i].size() == 0)
continue;
ll x1 = hold[i][0], y1 = hold[i].back();
ma[i][x1] = ma[i][y1] = inf;
autoit(prev, it)
{
//cout<<"for "<<i<<" , "<<it->ff<<" = "<<it->ss<<'\n';
ll curr = it->ff, cd = it->ss;
ma[i][x1] = min(ma[i][x1], cd + abs(curr-y1) + abs(y1-x1));
ma[i][y1] = min(ma[i][y1], cd + abs(curr-x1) + abs(y1-x1));
}
prev = ma[i];
}
ll ans = inf;
autoit(prev,it)
ans = min(ans, it->ss + abs(it->ff));
cout<<ans<<'\n';
return 0;
}
| #define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#include <climits>
#include <cmath>
#include <utility>
#include <iomanip>
#include <tuple>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long int;
// 任意長型
//namespace mp = boost::multiprecision;
//using Real = mp::number<mp::cpp_dec_float<128>>;
//using Bint = mp::cpp_int;
int main(int argc, char *argv[])
{
//input
int n, k;
cin >> n >> k;
string s;
cin >> s;
//calc
while (1 <= k){
string nextS;
s = s + s;
if (s.size() % 2){
s = s + s;
}
for (unsigned int i = 0; i < s.size(); i += 2){
if (s[i] == s[i + 1]){
nextS.push_back(s[i]);
}
else if (s[i] == 'R' && s[i + 1] == 'S'){
nextS.push_back('R');
}
else if (s[i] == 'P' && s[i + 1] == 'R'){
nextS.push_back('P');
}
else if (s[i] == 'S' && s[i + 1] == 'P'){
nextS.push_back('S');
}
else if (s[i] == 'S' && s[i + 1] == 'R'){
nextS.push_back('R');
}
else if (s[i] == 'R' && s[i + 1] == 'P'){
nextS.push_back('P');
}
else if (s[i] == 'P' && s[i + 1] == 'S'){
nextS.push_back('S');
}
}
s = nextS;
k--;
}
//ans
cout << s[0] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,k,m;
cin >> n >> k >> m;
int sum = 0;
for(int i = 0; i < n-1; ++i){
int a;
cin >> a;
sum += a;
}
int ans = n*m-sum;
if(ans > k){
ans = -1;
}else if(ans < 0){
ans = 0;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#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)
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec){ os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template<typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template<typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); }
template<typename T> bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template<typename T> bool mmin(T &m, const T q) { if (q < m) {m = q; return true;} else return false; }
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 dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
using namespace __gnu_pbds; // find_by_order(), order_of_key()
template<typename TK> using pbds_set = tree<TK, null_type, less<TK>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename TK, typename TV> using pbds_map = tree<TK, TV, less<TK>, rb_tree_tag, tree_order_statistics_node_update>;
*/
int main()
{
lint N;
cin >> N;
lint K;
cin >> K;
lint M;
cin >> M;
vector<lint> A(N - 1);
cin >> A;
lint ret = M * N - accumulate(ALL(A), 0LL);
if (ret > K) puts("-1");
else if (ret <= 0) puts("0");
else cout << ret << endl;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0)
#define setbit0(n, i) ((n) & (~(1LL << (i))))
#define setbit1(n, i) ((n) | (1LL << (i)))
#define togglebit(n, i) ((n) ^ (1LL << (i)))
#define lastone(n) ((n) & (-(n)))
char gap = 32;
template<typename T>
ostream& operator<<(ostream &os, const vector<T> &v) {
os << '{';
for (const auto &x : v) os << gap << x;
return os << '}';
}
template<typename A, typename B>
ostream& operator<<(ostream &os, const pair<A, B> &p) {
return os << '(' << p.first << gap << p.second << ')';
}
template <class T>
void read(T &x) {
char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar())
;
for (x = 0; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + (c & 15);
}
#define ll long long
#define lll __int128_t
#define pb push_back
#define mp make_pair
typedef pair < int,int> ii;
typedef vector<ii> vii;
typedef vector<ll>vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashPrime = 1610612741;
#define mod 1000000007
long gcdExtended(long a, long b, long *x, long *y)
{
// Base Case
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
long x1, y1; // To store results of recursive call
long gcd = gcdExtended(b%a, a, &x1, &y1);
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
long modInverse(long a, long m)
{
long x, y;
long g = gcdExtended(a, m, &x, &y);
long res = (x%m + m) % m;
return res;
}
void solve(){
ll i,j,k,l,n,m,s;
cin>>n>>s>>k;
ll kola = n-s;
ll GCD=__gcd(__gcd(kola,k),n);
kola/=GCD;
k/=GCD;
n/=GCD;
if(__gcd(k,n)!=1){
cout<<"-1\n";
return;
}
ll ans = (kola*modInverse(k,n))%n;
cout<<ans<<"\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
ll t;
cin>>t;
while(t--){
solve();
}
return 0;
} | #include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <sstream>
#include <set>
#include <functional>
#include <map>
#include <unordered_map>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long ll;
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,a) repi(i,0,a)
#define all(u) u.begin(),u.end()
using Interval = pair<ll,ll>;
using Graph = vector<vector<ll> >;
#define fi first
#define se second
struct Edge{
ll to;
ll w;
Edge(ll to, ll w):to(to),w(w){}
};
using wGraph = vector<vector<Edge> >;
template<class T> void chmax(T& a, T b){
if (a < b){
a=b;
}
}
template<class T> void chmin(T& a, T b){
if (a > b){
a=b;
}
}
template<class T> bool chmaxb(T& a, T b){
if (a < b){
a=b;
return true;
}
return false;
}
template<class T> bool chminb(T& a, T b){
if (a > b){
a = b;
return true;
}
return false;
}
const ll INF = 998244353;
string N;
int main(){
cin >> N;
ll add = 0;
while(true){
bool ok = true;
rep(i,N.size()/2){
if (N[i] != N[N.size()-1-i]){
ok = false;
}
}
if (ok){
cout << "Yes" << endl;
return 0;
}
else{
N = "0"+N;
add++;
}
if (add >= 15) break;
}
cout << "No" << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
const ll mod=998244353;
const int N=2001;
ll n,m;
ll a[N];
vector<pair<ll,int> >pf;
vector<int>ans;
ll duh[100001];
ll mg[100001];
int d[31];
bool inc(int t){
if(t==0) return true;
d[pf.size()-1]++;
int x=pf.size()-1;
while(x>=0 && d[x]>pf[x].se){
d[x]=0;
x--;
if(x<0) break;
d[x]++;
}
return(x>=0);
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin >> n;
//n=2000;
for(int i=1; i<=n ;i++){
cin >> a[i];
//a[i]=2162160;
}
sort(a+1,a+n+1);
if(a[1]==1){
cout << "1\n";
return 0;
}
int work=0;
for(int i=1; i<=n ;i++){
//if(i%100==0) cout << "doing " << i << ' ' << work << endl;
ll z=a[i];
pf.clear();
for(int j=2; j*j<=a[i] ;j++){
if(z%j!=0) continue;
pf.push_back({j,0});
while(z%j==0){
pf.back().se++;
z/=j;
//work++;
}
}
if(z!=1) pf.push_back({z,1});
int mx=0;
for(int k=0; inc(k) ;k++){
duh[k]=0;
mx=max(mx,k);
}
//cout << mx << endl;
for(int j=1; j<=n ;j++){
int y=0;
int cnt=0;
for(auto c:pf){
ll z=c.fi;
y*=(c.se+1);
for(int p=1; p<=c.se ;p++){
if(a[j]%z!=0) break;
z=z*c.fi;y++;
//work++;
cnt++;
}
}
//if(j==1) cout << i << ' ' << cnt << endl;
duh[y]++;
}/*
cout << "doing " << i << ' ' << a[i] << endl;
for(int k=0; inc(k) ;k++){
cout << duh[k] << ' ' << d[0] << ' ' << d[1] << endl;
}*/
ll step=1;
//cout << pf.size() << endl;
for(int j=pf.size()-1; j>=0 ;j--){
for(int k=0; inc(k) ;k++){
work++;
if(d[j]==0){
if(j==pf.size()-1){
for(int p=k+step*pf[j].se ;p>=k ;p-=step){
mg[p]=1;
//work++;
}
}
for(int p=k+step; p<=k+step*pf[j].se ;p+=step){
mg[p]=mg[p-step]*pf[j].fi;//work++;
}
for(int p=k+step*pf[j].se ;p>k ;p-=step){
duh[p-step]+=duh[p];
//work++;
}
}
}
step*=pf[j].se+1;
}/*
cout << endl;
for(int k=0; inc(k) ;k++){
cout << duh[k] << ' ' << d[0] << ' ' << d[1] << endl;
}*/
for(int k=0; inc(k) ;k++){
ll step=1;
bool ok=true;
for(int j=pf.size()-1; j>=0 ;j--){
work++;
if(d[j]==pf[j].se){
step*=pf[j].se+1;continue;
}
if(duh[k]==duh[k+step]) ok=false;
step*=pf[j].se+1;
}
if(ok) ans.push_back(mg[k]);
}
}
//cout << ans.size() << endl;
sort(ans.begin(),ans.end());
ans.erase(unique(ans.begin(),ans.end()),ans.end());
while(ans.back()>a[1]) ans.pop_back();
cout << ans.size() << '\n';
} | #include <iostream>
#include <vector>
#include <limits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <algorithm>
#include <random>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <chrono>
#include <sstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
typedef vector<pii > vii;
typedef vector<vector<ll> > vvi;
typedef vector<vvi > vvvi;
typedef vector<vector<pair<ll, ll> > > vvii;
typedef ll func_ii_i(ll, ll);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,s,e) for(ll i=(s);i<(e);++i)
#define repr(i,s,e) for(ll i=(e);i>(s);--i)
#define io do { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); } while (0)
#define endl '\n'
#define newl cout << '\n';
#define fill0(a) memset(a, 0, sizeof(a))
#define fill1(a) memset(a, -1, sizeof(a))
#define all(v) v.begin(), v.end()
#define bg begin()
#define ed end()
#define tr(it, v) for (auto it = v.bg; it != v.ed; it++)
#define prv(v) do { tr(it, v) { cout << *it << " "; }; cout << endl; } while (0)
#define prm(m) tr(it, m) { cout << it->first << " -> " << it->second << endl; }
#define scan(a, n) rep(i,0,n) cin >> a[i];
#define minv(v) *min_element(all(v))
#define maxv(v) *max_element(all(v))
#define sumv(v) accumulate(all(v), 0ll)
#define ub(a, x) upper_bound(all(a), x)
#define lb(a, x) lower_bound(all(a), x)
#define tcT template<class T
ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); }
ll max3(ll a, ll b, ll c) { return max(a, max(b, c)); }
ll mid3(ll a, ll b, ll c) { return a + b + c - min3(a, b, c) - max3(a, b, c); }
ll ceil1(ll x, ll y) { if(x % y) return (x + y) / y; else return x / y; }
tcT> void rmdup(vector<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.ed); }
tcT> ll fst1(ll l, ll h, T f) {
++h; // f is increasing
while(l<h) {
ll mi = l + ((h - l) >> 1);
f(mi) ? h = mi : l = mi + 1;
}
return l;
}
tcT> ll lst1(ll l, ll h, T f) {
--l; // f is decreasing
while(l<h) {
ll mi = l + ((h - l + 1) >> 1);
f(mi) ? l = mi : h = mi - 1;
}
return l;
}
const ll MOD = 1e9+7;
const ll INF = 1e16;
const ll TE3 = 1005;
const ll TE5 = 300005;
const ll dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
const string YN[2] = {"NO", "YES"};
using namespace std;
void divisors(ll x,vi &u)
{
if(x==1) {
return;
}
for (ll d = 2; d * d <= x; ++d)
{
ll c = 0;
if(x%d==0) {
u.pb(d);
if((d*d)!=x) {
u.pb(x/d);
}
}
}
}
// computes gcd(a,b)
ll gcd(ll a, ll b) {
if(a<b)
swap(a, b);
while (b)
{
ll t = a % b;
a = b;
b = t;
}
return a;
}
int main()
{
io;
ll n;
cin >> n;
vi v(n);
scan(v, n);
sort(all(v));
set<ll> ans;
map<ll,ll> z;
for (ll q: v) {
vi u;
divisors(q, u);
u.pb(1);
u.pb(q);
rmdup(u);
for(ll d: u) {
if(z.count(d)) {
z[d]=gcd(z[d], q);
} else {
z[d]=q;
}
}
}
for(auto it: z) {
if(it.se && it.se<=v[0]) {
ans.insert(it.se);
}
}
cout<<ans.size()<<endl;
// for(ll q: ans) {
// cout<<q<<" ";
// }
// cout<<endl;
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long ll;
typedef std::vector<int> P;
typedef unsigned un;
ll read()
{
ll 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*10+c-'0',c=getchar();
return f*x;
}
using std::min;
using std::max;
template<typename T>bool umin(T& a,T t){if(a>t)return a=t,1;return 0;}
template<typename T>bool umax(T& a,T t){if(a<t)return a=t,1;return 0;}
const int MAXN = 111;
int a[MAXN],b[MAXN];
char s[MAXN];
int main()
{
int n=read();
scanf("%s",s+1);
for(int i=1;i<=n+1;++i)a[i]=read();
for(int k=10000;k;--k)
{
bool fail=0;
for(int i=1;i<=k&&!fail;++i)
{
b[1]=(a[1]/k+(i<=a[1]%k));
for(int j=2;j<=n+1;++j)
{
b[j]=(a[j]/k+(i<=a[j]%k));
if(s[j-1]=='>')fail|=b[j-1]<=b[j];
else fail|=b[j-1]>=b[j];
}
}
if(!fail)
{
printf("%d\n",k);
for(int i=1;i<=k;puts(""),++i)
for(int j=1;j<=n+1;++j)printf("%d ",a[j]/k+(i<=a[j]%k));
return 0;
}
}
puts("0");
return 0;
}
| /*
author: Maksim1744
created: 10.04.2021 15:02:52
*/
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
template<typename T> vector<T>& operator-- (vector<T>& v){for (auto& i : v) --i; return v;}
template<typename T> vector<T>& operator++ (vector<T>& v){for (auto& i : v) ++i; return v;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){for (auto& i : v) is >> i; return is;}
template<typename T> ostream& operator<<(ostream& os, vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator-- (pair<T, U> &p){--p.first; --p.second; return p;}
template<typename T, typename U> pair<T,U>& operator++ (pair<T, U> &p){++p.first; ++p.second; return p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second; return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}
#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...) 42
#define mclock 42
#define shows 42
#define debug if (false)
#endif
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n;
string s;
cin >> n >> s;
vector<int> a(n + 1);
cin >> a;
int k = 1e9;
for (int i = 0; i < n; ++i)
k = min(k, abs(a[i] - a[i + 1]));
vector<vector<int>> b(k);
for (int i = 0; i < k; ++i) {
b[i].pb(a[0] / k);
}
for (int i = 0; i < a[0] % k; ++i) {
b[i].back()++;
}
for (int i = 0; i < n; ++i) {
if (a[i] < a[i + 1]) {
int d = a[i + 1] - a[i];
sort(b.begin(), b.end(), [&](const auto &a, const auto &b) {
return a.back() < b.back();
});
for (int i = 0; i < k; ++i) {
b[i].pb(b[i].back() + 1);
}
d -= k;
for (int i = 0; i < d; ++i) {
b[i % k].back()++;
}
} else {
int d = a[i] - a[i + 1];
sort(b.begin(), b.end(), [&](const auto &a, const auto &b) {
return a.back() > b.back();
});
for (int i = 0; i < k; ++i) {
b[i].pb(b[i].back() - 1);
}
d -= k;
for (int i = 0; i < d; ++i) {
b[i % k].back()--;
}
}
}
cout << k << '\n';
for (auto v : b)
cout << v << '\n';
return 0;
}
|
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx,avx2,popcnt,tune=native")
#pragma GCC optimize("fast-math")
//#pragma GCC optimize("Ofast")
/*#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")*/
//#pragma GCC optimize("inline")
//#pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS
#include <chrono>
#include <set>
#include <map>
#include <deque>
#include <string>
#include <cstdint>
#include <cmath>
#include <queue>
#include <cassert>
#include <random>
#include <bitset>
#include <iomanip>
#include <numeric>
#include <time.h>//////////////
#include <ctime>
#include <climits>
#include <string>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
//#define endl '\n'
#define mp make_pair
#define pbc push_back
#define pob pop_back()
#define empb emplace_back
#define queuel queue<long long>
#define sqr(a) ((a) * (a))
#define lsqr(a) (ld(a) * a)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pin(p) cin >> p.first >> p.second;
#define uniq(a) sort(all(a));(a).resize(unique(all(a)) - a.begin());
#define rev(v) reverse(v.begin(), v.end());
#define sout(s, c) for (auto i : s) cout << i << c;
#define pout(p) cout << p.first << " " << p.second;
#define er(v, l, r) erase(v.begin() + l, v.begin() + r);
#define vin(v) for (ll i = 0; i < v.size(); ++i) cin >> v[i];
#define vout(v, c) for (int i = 0; i < v.size(); ++i) cout << v[i] << c;
#define pushi(v, a) for (int i = 0; i < a.size(); ++i) v.push_back(a[i]);
#define fastio() ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); srand(time(NULL))
#define dab(v) for(auto i:v)cout<<i<<' ';
#define sp system("pause")
#define left left228
#define calc calc228
#define next next228
#define type type1337
#define right right228
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
using namespace std;
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
const ld EPS = 1e-14 ;
const ld EPS2 = 1e-12;
const ld PI = acos(-1);
int mod = (int)998244353;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int a228 = 18;
const ll INF = 1e18;
const int inf = 1e9;
mt19937 rnd(time(0));
const ll kekmod = 1791791791;
const ll bestmod = 1148822869;
const int MAXN = 101;
vector<int> rg[MAXN];
vector<int> used(MAXN);
int cnt = 0;
void dfs(int v)
{
cnt++;
used[v] = 1;
for (int i : rg[v])
if (!used[i]) dfs(i);
}
signed main()
{
fastio();
int n;
cin >> n;
for (int i = 0; i < n; ++i)
{
string s;
cin >> s;
for (int j = 0; j < n; ++j)
{
if (s[j] == '1')
{
rg[j].pbc(i);
}
}
}
ld ans = 0;
for (int i = 0; i < n; ++i)
{
used.assign(MAXN, 0);
cnt = 0;
dfs(i);
ans += (ld)1 / cnt;
}
cout << fixed << setprecision(20) << ans;
sp;
} | #include<bits/stdc++.h>
using namespace std;
int n,a[105][105],b[105][105];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
b[i][i]=1;
for(int j=1;j<=n;j++)
if(s[j-1]=='1')
a[i][j]=b[j][i]=1;
}
for(int t=1;t<=n;t++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
b[i][j]=b[i][j]|(b[i][t]&b[t][j]);
double ans=0;
for(int i=1;i<=n;i++)
{
int cnt=0;
for(int j=1;j<=n;j++)
if(b[i][j]) cnt++;
ans+=1.0/cnt;
}
printf("%.10lf",ans);
}
|
//標準関数をすべてインクルードする。
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int main() {
int A, B, W; cin >> A >> B >> W;
int kW = W * 1000;
int maxres =kW / A; int minres = (kW + (B - 1)) / B;
if (maxres < minres) cout << "UNSATISFIABLE" << endl;
else cout << minres << " " << maxres << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define fastio() ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long int
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define inf 1e9
#define pi 3.1415926536
#define mod 1000000007
#define fr(i,a,b,in) for(int i=a;i<b;i+=in)
#define frn(i,a,b,dc) for(int i=a;i>=b;i-=dc)
#define test(t) int t; cin>>t; while(t--)
int main()
{
fastio();
int a,b;
cin>>a>>b;
int w;
cin>>w;
double w1=w*1000.0, a1=a*1.0, b1=b*1.0;
int ans1=-1,ans2=1e8;
// fr(i,a,b+1,1){
// int m1=(w)/(i);
// int n1=w%i;
// double c1=(n1*1.0)/(m1*1.0);
// if(double(i)+c1 <= double(b)){
// ans1=max(ans1,int(m1));
// ans2=min(ans2,int(m1));
// }
// }
fr(i,1,1e6+1,1){
double d=w1/double(i);
if(d>=a1 && d<=b1){
ans1=max(ans1,i);
ans2=min(ans2,i);
}
}
if(ans1==-1) cout<<"UNSATISFIABLE"<<endl;
else cout<<ans2<<' '<<ans1<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#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 COUT(x) cout<<(x)<<endl
using vi = vector<int>;
using vvi = vector<vi>;
const int mod = 1e9+7;
int modPow(int a, int n) {
if (n == 0) return 1; // 0乗にも対応する場合
if (n == 1) return a % mod;
if (n % 2 == 1) return (a * modPow(a, n - 1)) % mod;
int t = modPow(a, n / 2);
return (t * t) % mod;
}
signed main(){
int L; cin >> L; L--;
int N = L;
int K = 11;
vi comb(N+1);
comb[0]=1;
rep1(i,K+1){
comb[i] = (comb[i-1]*(N-i+1)/i);
}
cout << comb[K] << endl;
return 0;
} | #include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>
#include <locale>
#include <omp.h>
#include <string.h>
using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "YES" << endl; \
} \
else \
{ \
cout << "NO" << endl; \
}
#define IFYes(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "Yes" << endl; \
} \
else \
{ \
cout << "No" << endl; \
}
#define IFyes(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "yes" << endl; \
} \
else \
{ \
cout << "no" << endl; \
}
#define DEBUG_OUTPUT_ARRAY(XXX, ONE) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
}
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << "<<< i = " << i << " >>>" << endl; \
for (int j = 0; j < (TWO); j++) \
{ \
cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
} \
}
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << i << " "; \
for (int j = 0; j < (TWO); j++) \
{ \
cout << XXX[i][j] << " "; \
} \
cout << endl; \
}
typedef pair<long long int, long long int> pll;
typedef pair<long long int, pll> lpll;
bool overflow_checker(long long int input_number1,long long int input_number2){
if(5000000000000000000/input_number1<input_number2){
return true;
}else{
return false;
}
}
const long long int mod = 998244353;
const long long int INF = 2e18;
const long double PI=3.14159265358979323;
const long long int pl=1100;
const long double eps =0.00001;
long long int N,A[200200],B[200200],C;
long long int dp[402][402]={};
string S;
long long int mymin=INF,num=INF;
int main(){
cout << fixed << setprecision(18);
cin>>N;
for (long long int i = 1; i < 300; i++)
{
dp[i][0]=1;
dp[i][i]=1;
for (long long int j = 1; j < i; j++)
{
dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
}
}
cout<<dp[N-1][11]<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
class Edge{
public:
int from, to;
long long value;
Edge(int a, int b, long long c){
from = a;
to = b;
value = c;
}
Edge(int a,int b){
from = a;
to = b;
}
};
std::vector<long long> dijkstra(int s, int N, std::vector<Edge> E){
std::vector<long long> ans(N, 1e18);
std::vector<std::vector<Edge>> Edges(N);
for(auto e : E){
Edges.at(e.from).push_back(e);
}
std::priority_queue<std::pair<long long, int>, std::vector<std::pair<long long, int>>, std::greater<std::pair<long long, int>>> que;
ans.at(s)= 0;
que.push(std::make_pair(0, s));
while(!que.empty()){
std::pair<long long, int> p = que.top();
que.pop();
if(ans.at(p.second) < p.first)continue;
for(int i = 0;i < Edges.at(p.second).size();i++){
if(ans.at(p.second) + Edges.at(p.second).at(i).value < ans.at(Edges.at(p.second).at(i).to)){
ans.at(Edges.at(p.second).at(i).to) = ans.at(p.second) + Edges.at(p.second).at(i).value;
que.push(std::make_pair(ans.at(Edges.at(p.second).at(i).to),Edges.at(p.second).at(i).to));
}
}
}
return ans;
}
void solve(long long a, long long b, long long x, long long y){
vector<Edge> Edges;
for(int i = 0;i < 100;i++){
Edges.push_back(Edge(i, i + 100, x));
}
for(int i = 0;i < 99;i++){
Edges.push_back(Edge(i + 1, i + 100, x));
}
for(int i = 0;i < 99;i++){
Edges.push_back(Edge(i, i + 1, y));
Edges.push_back(Edge(i + 100, i + 100 + 1, y));
}
int q = Edges.size();
for(int i = 0;i < q;i++){
Edges.push_back(Edge(Edges[i].to, Edges[i].from, Edges[i].value));
}
vector<ll> dist = dijkstra(a - 1, 2 * 100, Edges);
cout<<dist[(b - 1) + 100]<<endl;
}
int main(){
long long a;
scanf("%lld",&a);
long long b;
scanf("%lld",&b);
long long x;
scanf("%lld",&x);
long long y;
scanf("%lld",&y);
solve(a, b, x, y);
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>
#include <cassert>
using namespace std;
//#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif
#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
//#define int long long
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
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 <typename T>
std::string printVector(const std::vector<T> &data)
{
std::stringstream ss;
std::ostream_iterator<T> out_it(ss, ", ");
ss << "[";
std::copy(data.begin(), data.end() - 1, out_it);
ss << data.back() << "]";
return ss.str();
}
template <typename T>
void print_array(const T &ary, int size){
REP(i,size){
cout << ary[i] << " ";
}
cout << endl;
}
const int mod = 1e9+7;
//const int mod = 998244353;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
LL N,K;
bool check(LL mid, const VVL& A){
dump(mid)
LL half = (K*K+1) / 2;
VVL acc(N,VL(N));
REP(i,N) REP(j,N){
if(i == 0 && j == 0){
if(A[0][0] <= mid) acc[i][j] = 1;
else acc[i][j] = 0;
}else if(i == 0){
if(A[i][j] <= mid){
acc[i][j] = acc[i][j-1] + 1;
}else{
acc[i][j] = acc[i][j-1];
}
}else if(j == 0){
if(A[i][j] <= mid){
acc[i][j] = acc[i-1][j] + 1;
}else{
acc[i][j] = acc[i-1][j];
}
}else{
if(A[i][j] <= mid){
acc[i][j] = acc[i-1][j] + acc[i][j-1] - acc[i-1][j-1] + 1;
}else{
acc[i][j] = acc[i-1][j] + acc[i][j-1] - acc[i-1][j-1];
}
}
}
//REP(i,N){
// REP(j,N){
// cerr << acc[i][j] << " ";
// }
// cerr << endl;
//}
REP(i,N) REP(j,N){
if(i-K < -1 or j-K < -1) continue;
LL tx,ty,tz;
if(i-K == -1){
tx = 0;
}else{
tx = acc[i-K][j];
}
if(j-K == -1){
ty = 0;
}else{
ty = acc[i][j-K];
}
if(i-K == -1 or j-K == -1){
tz = 0;
}else{
tz = acc[i-K][j-K];
}
//LL cnt = acc[i][j] - acc[i-K][j] - acc[i][j-K] + acc[i-K][j-K];
LL cnt = acc[i][j] - tx - ty + tz;
dump(cnt)
dump(half)
if(cnt >= half) {
return true;
}
}
return false;
}
signed main(int argc, char const *argv[])
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(12);
//LL N,K;
cin >> N >> K;
vector<VL> A(N,VL(N));
REP(i,N) REP(j,N){
cin >> A[i][j];
}
LL ok = INF;
//LL ng = INF;
LL ng = -1;
while(abs(ok-ng) > 1){
LL mid = (ok + ng) / 2;
if(check(mid,A)){
dump("ok")
ok = mid;
}else{
dump("ng")
ng = mid;
}
}
cout << ok << endl;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
ll mod = 1e9+7;
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
ll N; cin >> N;
vector<ll> A(N);
rep(i,N) cin >> A[i];
sort(A.begin(), A.end());
ll ans = A[0] + 1;
rep(i,N-1) ans = (ans * (A[i + 1] - A[i] + 1)) % mod;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define rep(i, n) for(int i=0;i<(int)(n);++i)
template <typename T> ll gcd(T a, T b) {return b?gcd(b, a%b):a;}
template <typename T> ll lcm(T a, T b) {return a * b / gcd(a, b);};
template <typename T> inline void chmin(T &a, const T& b){if(a>b)a=b;}
template <typename T> inline void chmax(T &a, const T& b){if(a<b)a=b;}
#ifndef LOCAL
#define debug(...)
#endif
const int MaxA = 200;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
rep(i,n) cin >> a[i];
vector<ll> cnt(MaxA * 2 + 1);
rep(i,n) ++(cnt[a[i] + MaxA]);
ll ans = 0;
for (int i = -MaxA; i <= MaxA; ++i) {
for (int j = -MaxA; j < i; ++j) {
ll x = i-j;
ans += cnt[i+MaxA] * cnt[j+MaxA] * x * x;
}
}
cout << ans << '\n';
}
|
//#include <atcoder/math>
#include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
#define INF_LL 100000000000000000LL
#define INF 2000000000
#define MOD 998244353
#define ll long long
#define all(x) x.begin(), x.end()
#define REP(i, a, b) for(int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
// typedef float double;
// typedef priority_queue prique;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<ll> vl;
typedef vector<vi> matrix;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int sign[2] = {1, -1};
template <class T> bool chmax(T &a, T b) {
if(a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, T b) {
if(a > b) {
a = b;
return 1;
}
return 0;
}
ll modpow(ll a, ll b, ll m) {
if(b == 0)
return 1;
ll t = modpow(a, b / 2, m);
if(b & 1) {
return (t * t % m) * a % m;
} else {
return t * t % m;
}
}
struct edge {
int to;
ll cost;
edge(int t, ll c) { to = t, cost = c; }
};
typedef vector<vector<edge>> graph;
// using mint = modint998244353;
char inv(char c){
if(c == 'A') return 'B';
return 'A';
}
vector<string> solve(int n){
if(n == 1){
return {"AB"};
}
vector<string> t = solve(n - 1);
vector<string> res(t.size() * 2 + 1);
rep(i, t.size()){
rep(j, t[i].size()){
res[i * 2] += t[i][j];
res[i * 2] += t[i][j];
res[i * 2 + 1] += t[i][j];
res[i * 2 + 1] += inv(t[i][j]);
}
}
rep(i, t.size() * 2 + 2){
int cnt = 0;
rep(j, res.size() - 1){
if(res[j][i] == 'A') cnt++;
}
if(cnt != t.size()){
res[res.size()-1]+='A';
}else{
res[res.size()-1]+='B';
}
}
return res;
}
int main() {
ll n;
cin >> n;
vector<string> ans = solve(n);
int k = ans.size();
vector<vector<int>> c(ans[0].size(), vector<int>(ans[0].size()));
cout << k << endl;
rep(i, k){
string res = ans[i];
cout << res << endl;
rep(i, res.size()) REP(j, i + 1, res.size()) {if(res[i] == res[j])c[i][j]++;}
}
// int t = c[0][1];
// rep(i, n) REP(j, i + 1, n) {
// if(c[i][j] != t){
// cout << i << " " << j << " " << c[i][j] << endl;
// }
// }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
vector<string> gao(int n)
{
vector<string> res;
string s = string(1<<(n-1), 'A') + string(1<<(n-1), 'B');
res.push_back(s);
if(n==1) return res;
auto tmp = gao(n-1);
for(auto s : tmp)
{
res.push_back(s+s);
string t = s;
for(auto &c : t) c = char('B' - c + 'A');
res.push_back(s+t);
}
return res;
}
int main()
{
int n;
scanf("%d", &n);
auto res = gao(n);
printf("%d\n", (int)res.size());
for(auto s : res) printf("%s\n", s.c_str());
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1010000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}
template <typename T> T gcd(T a, T b) {if(b == 0) return a;return gcd(b,a%b);}
template <typename T> T lcm(T a, T b) {return a*b/gcd(a,b);}
template <typename T> T extgcd(T a, T b, T& x, T& y){
T d = a;
if(b != 0){
d = extgcd(b,a%b,y,x);
y-=(a/b)*x;
}
else x=1,y=0;
return d;
}
template <typename T> T modpow(T x, T n, T m) {
if(n==0) return 1;
T resp = modpow(x*x%m,n/2,m);
if(n&1) resp = resp*x%m;
return resp;
}
template <typename T> T modinverse(T a, T m) {
T x,y;
extgcd(a,m,x,y);
return (m+x%m)%m;
}
struct combination {
ll M = mod;
vector<ll> fact,ifact;
combination(ll n):fact(n+1),ifact(n+1){
fact[0]=1;ifact[0]=1;
for (ll i = 0; i<n; i++) {
fact[i+1] = fact[i]*(i+1) % M;
ifact[i+1] = ifact[i]*modpow(i+1, M-2, M) % M;
}
}
ll com(ll a, ll b){
if(a == 0 && b == 0)return 1;
if(a < b || a < 0)return 0;
ll tmp = ifact[a-b] * ifact[b] % M;
return tmp * fact[a] % M;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N,M;
cin >> N >> M;
ll c = modpow<ll>(10LL,N,M*M);
ll b = modpow<ll>(10LL,N,M);
cout << (c-b)/M << endl;
return 0;
}
| #include <bits/stdc++.h>
struct __fastio__ { __fastio__ () { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); } } __fast_io__;
template<class T, class U> bool chmin (T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; }
template<class T, class U> bool chmax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> void print(const T &value) { std::cout << value << '\n'; }
template<class T, class... Args> void print(const T &value, Args... args) { std::cout << value << ' '; print(args...); }
template<class T> using martix = std::vector<std::vector<T>>;
using i32 = std::int_fast32_t;
using u32 = std::uint_fast32_t;
using i64 = std::int_fast64_t;
using u64 = std::uint_fast64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
int main() {
int h, w;
std::cin >> h >> w;
std::vector<std::string> s(h);
for (auto &e : s) std::cin >> e;
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i + 1 < h) {
if (s[i][j] == '.' and s[i + 1][j] == '.') ans++;
}
if (j + 1 < w) {
if (s[i][j] == '.' and s[i][j + 1] == '.') ans++;
}
}
}
std::cout << ans << '\n';
} |
#include<bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9+7;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<ll> a(n);
rep(i,n) cin >> a[i];
vector<ll> dp(n, -LINF);
vector<vector<int>> from(n);
int x, y;
rep(i,m) {
cin >> x >> y;
x--; y--;
dp[x] = 0;
from[y].push_back(x);
}
rep(i,n) {
for (int j: from[i]) {
dp[i] = max(dp[i], dp[j]+a[i]-a[j]);
}
}
ll ans = -LINF;
rep(i,n) {
// cout << dp[i] << endl;
if (from[i].size()>0) ans = max(ans, dp[i]);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define f(i,j,n) for(int i = j; i <= n; i++)
#define r(i,n,j) for(int i = n; i >= j; i--)
#define all(container) container.begin() , container.end()
#define sz(container) (int)container.size()
#define ff first
#define ss second
#define pii pair <int , int>
#define sp(x) setprecision(x)
#define mod 1000000007
#define endl "\n"
#define pb push_back
#define mp make_pair
#define T int ttt; cin >> ttt; while(ttt--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
int a[200005],b[200005],ans=-1e18;
bool vis[200005];
vector<int>v[200005];
void dfs(int r){
vis[r]=1;
for(auto i : v[r]){
if(!vis[i])
dfs(i);
b[r]=max(b[r],b[i]);
b[r]=max(b[r],a[i]);
}
}
int32_t main()
{
fast
int n,m,x,y; cin>>n>>m;
f(i,1,n) cin>>a[i];
f(i,1,n) b[i]=-1e16;
f(i,1,m){
cin>>x>>y;
v[x].pb(y);
}
f(i,1,n)
if(!vis[i])
dfs(i);
f(i,1,n)
ans=max(ans,b[i]-a[i]);
cout<<ans;
} |
//#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
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 N, M;
int to[400005], ne[400005], c[200005], he[100001];
int kotae[100001];
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
const int MAX = 1000;
class shuturyoku_unko {
public:
char C[MAX * 4];
constexpr shuturyoku_unko() : C() {
rep(i, MAX) {
int X = i;
rep(j, 3) {
C[i * 4 + 2 - j] = '0' + X % 10;
X /= 10;
}
C[i * 4 + 3] = ' ';
}
}
};
constexpr shuturyoku_unko f;
const int dm = 1 << 17;
char* dn = cn, * di = dn, * owad = dn + dm - 20;
void putint(int A) {
if (owad < di) {
fwrite(dn, 1, di - dn, stdout);
di = dn;
}
if (A >= 1000) {
int dig = 1;
if (A >= 100000) dig = 3;
else if (A >= 10000) dig = 2;
memcpy(di, f.C + A / 1000 * 4 + 3 - dig, dig);
memcpy(di + dig, f.C + A % 1000 * 4, 4);
di += dig + 4;
}
else {
int dig = 1;
if (A >= 100) dig = 3;
else if (A >= 10) dig = 2;
memcpy(di, f.C + A * 4 + 3 - dig, dig + 1);
di += dig + 1;
}
}
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
N = getint();
M = getint();
int k = 2;
rep1(i, M) {
int u = getint(), v = getint();
c[i] = getint();
to[k] = v;
ne[k] = he[u];
he[u] = k++;
to[k] = u;
ne[k] = he[v];
he[v] = k++;
}
int Q[100001], q = 0;
Q[q++] = 1;
kotae[1] = 1;
while (q) {
int tmp = Q[--q];
int oya = kotae[tmp];
for (int k = he[tmp]; k; k = ne[k]) {
int t = to[k];
if (!kotae[t]) {
int e = c[k >> 1];
kotae[t] = e + (oya == e);
Q[q++] = t;
}
}
}
rep1(i, N) putint(kotae[i] - ((kotae[i] > N) << 1));
fwrite(dn, 1, di - dn, stdout);
Would you please return 0;
} | #include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
using LL = long long;
int main() {
// freopen("C:\\Users\\dna049\\cf\\in", "r", stdin);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m, k;
std::cin >> n >> m >> k;
std::vector<int> a(k);
for (auto &x: a) std::cin >> x;
for (int i = m - 1; i < k; ++i) if (a[i] - a[i - m + 1] == m - 1) {
std::cout << "-1\n";
return 0;
}
std::vector<double> b(n + m), c(n + m);
double sb = 0, sc = 0;
for (int i = n - 1; i >= 0; --i) {
if (a.size() && i == a.back()) {
b[i] = 0;
c[i] = 1;
a.pop_back();
} else {
b[i] = sb / m + 1;
c[i] = sc / m;
}
sb += b[i] - b[i + m];
sc += c[i] - c[i + m];
}
std::cout.precision(8);
std::cout << std::fixed << b[0] / (1 - c[0]) << std::endl;
return 0;
} |
#include <bits/stdc++.h>
#include <unistd.h>
#define FAST_IO ios::sync_with_stdio(0);cin.tie(NULL)
#define all(v) (v).begin(),(v).end()
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
typedef long long Long;
typedef long double Double;
typedef unsigned long long ULong;
typedef pair<Double, Double> Pair;
const int N = 1e6;
const Double EPS = 1e-9;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
Long random(Long a, Long b) { return uniform_int_distribution<Long> (a, b) (rng); }
Pair operator + (const Pair& a, const Pair& b) {
return {a.first + b.first, a.second + b.second};
}
Pair operator - (const Pair& a, const Pair& b) {
return {a.first - b.first, a.second - b.second};
}
Pair operator / (const Pair& a, Double k) {
return {a.first / k, a.second / k};
}
void Solve(void) {
Long n, m, k;
cin >> n >> m >> k;
if (k == 0) {
vector<Double> dp(n + m + 2, 0.0);
vector<Double> suffix(n + m + 2, 0.0);
for (int i = n - 1; i >= 0; i--) {
Double sum = suffix[i + 1] - suffix[i + m + 1];
dp[i] = sum / m + 1;
suffix[i] = dp[i] + suffix[i + 1];
}
cout << dp[0] << '\n';
} else {
vector<bool> is_hole(n + m + 2, false);
for (int i = 0; i < k; i++) {
int a;
cin >> a;
is_hole[a] = true;
}
vector<int> prefix(n + m + 2, 0);
for (int i = 1; i <= n; i++) {
prefix[i] = prefix[i - 1];
if (is_hole[i]) prefix[i]++;
}
for (int i = 1; i < n - m + 1; i++) {
if (prefix[i + m - 1] - prefix[i - 1] == m) {
cout << -1 << '\n';
return;
}
}
vector<Pair> dp(n + m + 2, {0.0, 0.0});
vector<Pair> suffix(n + m + 2, {0.0, 0.0});
Pair one = {0.0, 1.0};
for (int i = n - 1; i >= 0; i--) {
if (is_hole[i]) {
dp[i] = {1.0, 0};
} else {
Pair sum = suffix[i + 1] - suffix[i + m + 1];
dp[i] = sum / m + one;
}
suffix[i] = dp[i] + suffix[i + 1];
}
Double ans = dp[0].second / (1.0 - dp[0].first);
cout << fixed << setprecision(10) << ans << '\n';
}
}
int main(void) {
FAST_IO;
int t = 1;
//cin >> t;
while (t--) Solve();
return 0;
}
| #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}
bool cmp(pii a, pii b) {
return 2 * a.ff + a.ss > 2 * b.ff + b.ss;
}
void solve() {
rd(int, n);
int score = 0;
vpii a(n);
rep(i, 0, n) {
cin >> a[i].ff >> a[i].ss;
score -= a[i].ff;
}
sort(all(a), cmp);
rep(i, 0, n) {
score += 2 * a[i].ff + a[i].ss;
if (score > 0) err2(i + 1);
}
assert(false);
}
void init_test() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int qq = 1;
// cin >> qq;
while (qq--) solve();
} |
#include <bits/stdc++.h>
using namespace std;
// template {{{
#define range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
constexpr i32 mod = 1e9 + 7;
// constexpr i32 mod = 998244353;
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;
constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};
struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;
template <typename T = i64> T input() { T x; cin >> x; return x; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}
void solve() {
int n = input();
vector< int > as(n), bs(n), ps(n);
cin >> as >> bs >> ps;
for (auto &p : ps) p--;
vector< int > used(n, false);
bool valid = true;
vector< pair< int, int > > ans;
for (int v = 0; v < n; v++) {
if (used[v]) continue;
vector< int > us;
us.emplace_back(v);
used[v] = true;
for (int u = ps[v]; u != v; u = ps[u]) {
us.emplace_back(u);
used[u] = true;
}
if ((int)us.size() == 1) continue;
int min_b = inf;
int idx_b = -1;
for (int i = 0; i < (int)us.size(); i++) {
int u = us[i];
if (as[u] <= bs[ps[u]]) {
valid = false;
}
if (bs[ps[u]] < min_b) {
min_b = bs[ps[u]];
idx_b = i;
}
}
int sz = us.size();
for (int i = (idx_b - 1 + sz) % sz; i != idx_b; i = (i - 1 + sz) % sz) {
ans.emplace_back(us[i] + 1, us[(i + 1) % sz] + 1);
}
}
if (not valid) {
cout << -1 << endl;
return;
}
cout << (int)ans.size() << endl;
for (auto &t : ans) {
cout << t.first << " " << t.second << endl;
}
}
signed main() {
solve();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i)
#define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i)
#define revrep(i,n) for(ll i=(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 60) -123456789;
#define fastio cin.tie(0); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(10);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;}
template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
#define dbg(a) cout << #a << ": " << a << endl;
#define dbg1(a,n) cout<<#a<<": "; rep(i,n) cout<<a[i]<<" "; cout<<endl;
#define dbg2(m,h,w) cout<<#m<<":"<<endl; rep(i,h){ rep(j,w)cout<<m[i][j]<<" "; cout<<endl; }
int rng(int n) { return rand()/(RAND_MAX+1.0)*n; }
int main() {
fastio;
ll ans{Inf};
ll N,M;
cin >> N >> M;
vector<ll> a(N);
rep(i,N) cin >> a[i];
vector<vector<int>> pos(1500005);
rep(i,N) pos[a[i]].push_back(i);
rep(i,1500005) {
int p = -1;
pos[i].push_back(N);
for (int j : pos[i]) {
if (j-p > M) updmin(ans, i);
p = j;
}
}
cout << ans << endl;
}
|
//#include <atcoder/all>
//using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPR(i, n) for(int i=n-1; i>=0; i--)
#define FOR(i, m, n) for(int i=m; i<n; i++)
#define ALL(v) v.begin(), v.end()
#define bit(n) (1LL<<(n))
#define FIX(d) fixed << setprecision(d)
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1e9;
const ll LINF = (1LL<<60);
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x,y,z;
cin >> x >> y >> z;
REPR(i, 1000000){
double p1 = (double)y / x;
double p2 = (double)i / z;
if(p1>p2){
cout << i << endl;
return 0;
}
}
return 0;
}
| #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 fi first
#define se 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>
#define omap map<int,int>
#define uset unordered set<int>
#define oset set<int>
#define pr pair<int,int>
#define endl "\n"
void solve()
{
int a,b;
cin>>a>>b;
a=(2*a)+100;
cout<<a-b<<endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
//cin>>t;
fr3(i,1,t){
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << ", " << p.x << ", " << p.y << ")"; return dd; }
#define ll long long
#define ld long double
void test_case() {
int n;
cin >> n;
set<int> a;
for(int i = 0; i < n; ++i) {
int x;
cin >> x;
if(x <= 0 || x > n) {
} else {
a.insert(x);
}
}
if(a.size() == n) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int T = 1;
// cin >> T;
while(T--) {
test_case();
}
return 0;
} | #include <iostream>
#include<string>
#include <math.h>
#include<vector>
#include<algorithm>
#include <iomanip>
#include<map>
using ll = long long;
using namespace std;
int main() {
int n; cin >> n;
vector<int>a(n + 1);
for (int i = 1; i <= n; i++) {
int x; cin >> x;
a[x]++;
}
bool ok = true;
for (int i = 1; i <= n; i++) {
if (a[i] > 1)ok = false;
}
if (ok)cout << "Yes" << endl;
else cout << "No" << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N ;
vector<int> A(N,1), P(N,-1);
P[0] = 0;
for (int i = 0; i < N; i++) {
if (P[i] == 1) continue;
else if (P[i] == -1) {
for (int j = i; j < N; j += i+1) {
P[j] = 1;
A[j]++;
}
if (i <= 1000) {
int i2 = (i+1)*(i+1);
while(i2 <= N) {
for (int j = i2; j <= N; j += i2) {
A[j-1]++;
}
i2 *= (i+1);
}
}
}
}
for (int i = 0; i < N; i++) {
cout << A[i];
if (i == N-1) cout << endl;
else cout << " ";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int x;
cin >> x;
if(x>=0) cout << x<< endl;
else cout << 0<< endl;;
}
|
//Date 16/06/2021
//practice
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define all(x) (x).begin(),(x).end()
#define rep(i,a,b) for(ll i = a; i<b ; i++)
#define sz(x) (int)((x).size())
#define PI 3.141592653589
#define mod 1000000007
#define MAX 1000005 //1e6+5
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
long long const INF = 1e13;
ll power(ll a, ll b)
{ ll result =1;
while(b)
{ if(b%2) result=(result*a);
a=(a*a);
b/=2;}
return result;
}
void sol()
{
int n; cin>>n;
vector<int> arr(n);
int sum = 0;
rep(i,0,n) {cin>>arr[i];sum+=arr[i];}
//SUBSET SUM PROBLEM
bool dp[n+1][sum+1];
for(int i=0;i<=n;i++)
{
dp[i][0] = true;
}
for(int i=1;i<=sum;i++)
{
dp[0][i] = false;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=sum;j++)
{
if(dp[i-1][j] == true) dp[i][j] = true; //1st condition
else if(j-arr[i-1]>=0 && dp[i-1][j-arr[i-1]]) dp[i][j] = true; //2nd condition
else dp[i][j] = false;
}
}
int str = ceil(sum*1.0/2);
for(int i = str;i<=sum;i++)
{
if(dp[n][i]) {cout<<i<<" "; return;}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int T=1; //cin>>T;
while(T--) sol();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
bitset<100100> b;
b.set(0);
int n;
cin >> n;
int s = 0;
for(int i = 0; i < n; i++){
int x;
cin >> x;
b |= b<<x;
s += x;
}
int ans = s;
for(int i = 0; i <= s; i++)
if(b[i])
ans = min(ans, max(i, s - i));
cout << ans << '\n';
return 0;
} |
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>
#include <cstddef>
#include <type_traits>
#include <vector>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define rep(i, n) for(int i=0;i<(int)(n);i++)
int main() {
lint b, c; cin >> b >> c;
map<lint, lint> imos;
// -1 しまくる
{
lint l = min(b - c / 2, b);
lint r = max(b - c / 2, b);
imos[l]++;
imos[r + 1]--;
// cerr << l << " " << r << endl;
}
// 最後に -1 倍する
{
lint l = min(-(b - (c - 1) / 2), -b);
lint r = max(-(b - (c - 1) / 2), -b);
imos[l]++;
imos[r + 1]--;
// cerr << l << " " << r << endl;
}
// -1 倍して、-1 しまくる
{
lint l = min(-b - (c - 1) / 2, -b);
lint r = max(-b - (c - 1) / 2, -b);
imos[l]++;
imos[r + 1]--;
// cerr << l << " " << r << endl;
}
// 最初と最後に -1 倍する
{
lint l = min(b + (c - 2) / 2, b);
lint r = max(b + (c - 2) / 2, b);
imos[l]++;
imos[r + 1]--;
}
lint pre = -1e18 * 3;
lint num = 0;
lint ans = 0;
for (auto e : imos) {
lint cur = e.first;
// cerr << "cur = " << cur << " num = " << num << endl;
if (num > 0) {
// cerr << "add: " << cur - pre << endl;
ans += (cur - pre);
}
pre = cur;
num += e.second;
}
cout << ans << endl;
return 0;
}
| #include <set>
#include <unordered_set>
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <queue>
#include <bitset>
#define INFL 100100100100100100
#define INF 1100100100
using namespace std;
int main() {
long long b;
cin >> b;
long long c;
cin >> c;
long long br, bl, brr, bll;
br = b + (c-2)/2;
bl = b - c/2;
brr = -b + (c-1)/2;
bll = -b - (c-1)/2;
if (br < brr || bl < bll) {
swap(br, brr);
swap(bl, bll);
}
cout << br - bl + 1 + brr - bll + 1 - (bl <= brr ? brr - bl + 1: 0) << endl;
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb emplace_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
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(){
ll r,x,y;
cin>>r>>x>>y;
ll euc = x*x+y*y;
ll r2 = r*r;
if (euc >= r2)cout<<ceil(sqrt(euc/(long double)r2))<<endl;
else cout<<2<<endl;
} | #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define rep2(i, j, n) for(int i=j;i<(int)(n);i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using P = pair<int, int>;
int main(){
char a;
int res1 =0 ,res2 = 0;
rep(i, 3){
cin >> a;
res1 += (int)(a - '0');
}
rep(i, 3){
cin >> a;
res2 += (int)(a - '0');
}
cout << max(res1, res2) << endl;
}
|
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <numeric>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\n';
#define PRINTVEC(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i] << ' '; std::cout << '\n';
#define PRINTPAIR(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i].first << ' ' << x[i].second << '\n'; std::cout << '\n';
#define PRINTSET(x) for (auto it = x.begin(); it != x.end(); it++) { std::cout << *it << ' '; } std::cout << '\n';
#define io std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
const int INF = 1<<29;
const double PI = acos(-1.0);
typedef long long ll;
const std::vector<int> GRID = {0,1,0,-1,0}; // for(k < 4) r=x+GRID[k],c=y+GRID[k+1]; if(0<=r&&r<m&&0<=c&&c<n) do smthg;
using namespace std;
// 3, 7, 13, 61, 61, 421, 841, 2521
ll lcm(ll a, ll b) {
//return (a * b) / gcd(a,b);
return (a / __gcd(a,b)) * b;
}
int main()
{
io;
#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
ll n; cin >> n;
ll ans = 1;
for (ll i = 2; i <= n; i++) {
ans = lcm(ans,i);
}
cout << ans + 1 << endl;
#ifndef ONLINE_JUDGE
std::cout << '\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
#endif
return 0;
}
| // #pragma GCC target ("avx2")
// #pragma GCC optimization ("O2")
// #pragma GCC optimization ("unroll-loops")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define int ll
#define pb push_back
#define mp make_pair
#define pob pop_back
#define __bp __builtin_popcount
using pii = pair<int,int>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
using pll = pair<ll,ll>;
using pdd = pair<ld,ld>;
using vint = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;
#define F first
#define S second
const int N = 1e5+5;
const int Inf = 1e9+7;
// const int Inf = 998244353;
const ll INF = 1e18+7;
const int Mod = 998244353;
const ld eps = 1e-9;
#ifndef M_PI
#define M_PI 3.1415926535
#endif
// const int M = 1e7+5;
void io()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
// freopen("sparse.in", "r", stdin);
// freopen("sparse.out", "w", stdout);
// cout << setprecision(9) << fixed;
}
int n;
ll gc(ll x, ll y)
{
if (y == 0)
return x;
return gc(y, x % y);
}
void solve()
{
ll ans = 1;
cin >> n;
for (int i = 2; i <= n; ++i)
{
ll x = ans * i / gc(ans, i);
ans = x;
}
cout << ans + 1;
}
signed main()
{
io();
int tt = 1;
// cin >> tt;
while (tt--) {
solve();
// cerr << '\n';
}
} |
#include <bits/stdc++.h>
//#pragma once
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define F first
#define S second
#define vi vector<int>
#define pii pair<int,int>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORN(i, a, b) for(int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = a; i >=b; --i)
#define pb push_back
#define mp make_pair
#define yes cout <<"YES"<<"\n"
#define no cout << "NO"<<"\n"
#define MOD 1000000007
#define maxn 1000005
#define MIN(x,y) if (x > (y)) x = (y)
#define MAX(x,y) if (x < (y)) x = (y)
#define reset(x) memset(x, 0, sizeof(x))
#define all(x) (x.begin(),x.end())
#define ll long long
#define db double
#define ull unsigned long long
#define udb unsigned double
#define ui unsigned int
#define ldb long double
#define ulli unsigned long long int
#define lli long long int
#define str string
//#define fl Team Flash a nham float
#define fl float
#define TASK "A"
const int INF = 1e9+5;
using namespace std;
// ATTEMPED 1
int n, a[1001];
int c = 0;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp","r",stdin);
freopen(TASK".out","w",stdout);
}
scanf("%d", &n);
FOR(i,1,n) scanf("%d", &a[i]);
FOR(i,1,n){
if(a[i] > 10) c += a[i] - 10;
}
printf("%d", c);
}
// Designed and Coded by YasashiSensei.
| #include <bits/stdc++.h>
using namespace std;
#define short int
int main() {
short n; cin >> n;
short a;
int sum = 0;
for(short i = 0; i < n; i++) {
cin >> a;
if(a > 10) {
sum += a-10;
}
}
cout << sum;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#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;
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> inline int sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
int main()
{
string s; cin >> s;
ll m; cin >> m;
int n = sz(s);
ll mx = 0;
vl arr(n);
rep(i,n) {
ll tmp = s[i] - '0';
arr[i] = tmp;
chmax(mx, tmp);
}
if(n == 1) {
if(mx <= m) cout << 1 << "\n";
else cout << 0 << "\n";
return 0;
}
ll lb = mx, ub = 1000000000000000001;
ll an = s[0] - '0';
while(ub - lb > 1) {
ll B = (ub + lb) / 2;
// cout << B << "\n";
ll x = an;
int cnt = 0;
while(x <= m/B) {
x *= B;
// cout << x << "\n";
cnt++;
if(cnt == n-1) break;
}
if(cnt==n-1) {
ll sum = 0;
ll buf = 1;
for(int j = n-1; j >= 1; --j) {
sum += arr[j] * buf;
buf *= B;
}
if(an*buf <= m - sum) {
lb = B;
}
else ub = B;
}
else ub = B;
}
cout << lb-mx << "\n";
return 0;
}
| #include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ch() getchar()
#define pc(x) putchar(x)
template<typename T>inline void read(T&x){
int f;char c;
for(f=1,c=ch();c<'0'||c>'9';c=ch())if(c=='-')f=-f;
for(x=0;c<='9'&&c>='0';c=ch())x=x*10+(c&15);x*=f;
}
template<typename T>inline void write(T x){
static char q[64];int cnt=0;
if(!x)pc('0');if(x<0)pc('-'),x=-x;
while(x)q[cnt++]=x%10+'0',x/=10;
while(cnt--)pc(q[cnt]);
}
const int maxn=1005,mod=1000000007;
int mo(const int x){
return x>=mod?x-mod:x;
}
int fac[maxn],iac[maxn];
int binom(int n,int m){
return 1ll*iac[m]*iac[n-m]%mod*fac[n]%mod;
}
char caa[5],cab[5],cba[5],cbb[5];
int main(){
int n;read(n);if(n==2)return puts("1"),0;fac[0]=fac[1]=iac[0]=iac[1]=1;
for(int i=2;i<=n;++i)iac[i]=1ll*(mod-mod/i)*iac[mod%i]%mod;
for(int i=2;i<=n;++i)iac[i]=1ll*iac[i-1]*iac[i]%mod,fac[i]=1ll*fac[i-1]*i%mod;
scanf("%s%s%s%s",caa,cab,cba,cbb);int ans=0;
if(cab[0]=='A'){
if(caa[0]=='A')ans=1;
else{
if(cba[0]=='A'){
ans=1;
for(int i=1;n-3-(i-1)>=i;++i)
ans=mo(ans+binom(n-3-(i-1),i));
}
else{
for(int i=0;i<=n-3;++i)
ans=mo(ans+binom(n-3,i));
}
}
}
else{
if(cbb[0]=='B')ans=1;
else{
if(cba[0]=='B'){
ans=1;
for(int i=1;n-3-(i-1)>=i;++i)
ans=mo(ans+binom(n-3-(i-1),i));
}
else{
for(int i=0;i<=n-3;++i)
ans=mo(ans+binom(n-3,i));
}
}
}
write(ans),pc('\n');
return 0;
}
|
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
bool chmin(int& a, int b){ if(a > b){ a = b; return 1; } return 0; }
bool chmax(int& a, int b){ if(a < b){ a = b; return 1; } return 0; }
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
const ll MOD = 1000000007LL;
int main() {
int N; ll C;
cin >> N >> C;
vector<ll> as(N),bs(N),cs(N);
set<int> abs;
for (int i = 0; i < N; ++i) {
cin >> as[i] >> bs[i] >> cs[i];
bs[i]++;
abs.insert(as[i]);
abs.insert(bs[i]);
}
unordered_map<ll,int> inds;
vector<ll> inds2(abs.size()+1);
int cnt = 0;
for (ll x : abs) {
inds[x] = ++cnt;
inds2[cnt] = x;
}
vector<ll> dps(cnt+1,0);
for (int i = 0; i < N; ++i) {
int ia = inds[as[i]];
int ib = inds[bs[i]];
dps[ia] += cs[i];
dps[ib] -= cs[i];
}
ll ans = 0;
for (int i = 1; i < cnt; ++i) {
dps[i] += dps[i-1];
ans += min(dps[i],C) * (inds2[i+1]-inds2[i]);
}
cout << ans << endl;
return 0;
} | #include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
readb(n, fix);
vpi v;
FOR (i, 1, n)
{
readc(a, b, c);
v.pb({a, c});
v.pb({b + 1, -c});
}
sort(all(v));
int cost = 0, sum = 0, prev = 0;
for (pi i: v)
{
cost += min(fix, sum)*(i.fi - prev);
sum += i.se;
prev = i.fi;
}
print(cost);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int A1 = 0, A2 = 0, A3 = 0,tm;
cin >> A1 >> A2 >> A3;
if(A1 > A2) {
tm = A1;
A1 = A2;
A2 = tm;
}
if (A2 > A3) {
tm = A2;
A2 = A3;
A3 = tm;
}
if (A1 > A2)
{
tm = A1;
A1 = A2;
A2 = tm;
}
if(A3 - A2 == A2 - A1) {
cout << "Yes" << endl;
}
else cout << "No" << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i, a, b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
int main() {
vector<int> A(3);
REP(i, 3) cin >> A[i];
sort(A.begin(), A.end());
if (A[1] - A[0] == A[2] - A[1]) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
} |
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<stack>
#include<queue>
#include<deque>
#include<algorithm>
#include<cmath>
#include<functional>
#include<bitset>
#include<iomanip>
#define _GLIBCXX_DEBUG
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define rep(i,n) for(int i=0;i<n;++i)
#define rep2(i,l,r) for(int i=l;i<=r;++i)
#define rep3(i,l,r) for(int i=l;i>=r;--i)
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
const long long inf = 1ll << 60;
const long long mod=1000000007;
const double PI = acos(-1);
int main(){
int n;
cin >> n;
V<ll> a(n);
for(int i=0;i<n;++i){cin >> a[i];}
ll proc = 0,start = 0;
ll mx = 0, ans = 0;
for(int i=0;i<n;++i){
proc += a[i];
mx = max(mx, proc);
ans = max(ans, start+mx);
start += proc;
}
cout << ans << endl;
return 0;
} | /*in the name of Aintah the most beneficent the most merciful.*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
// #define MOD 1000000007
//#define mod2 20071027
#define MOD 998244353
// #define base 100001
//#define mod 1000074259
#define endl "\n"
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FILE freopen("input.txt", "r", stdin); freopen("out.txt", "w", stdout);
using namespace std;
const int N = 300010;
ll t, n, a[N], res=0, sum;
ll pre[N], fp[N], nxt;
int solve() {
in1(n);
pre[0]=MIN;
for (int i = 1; i <= n; i++) {
in1(a[i]);
sum += a[i];
pre[i] = max(pre[i - 1], sum);
fp[i] = sum;
res=max(res, nxt+pre[i]);
nxt += fp[i];
}
printf("%lld\n", res);
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
return solve();
} |
#include<bits/stdc++.h>
using namespace std;
#define _ 0
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
long long a[maxn],b[maxn],ans;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[a[i]%200]++;
}
for(int i=0;i<=199;i++)
ans+=b[i]*(b[i]-1)/2;
cout<<ans<<endl;
return ~~(0^_^0);
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define FOR(i,f,n) for(int i=f;i<n;i++)
#define FORI(i,f,n) for(int i=f;i>=n;i--)
#define sz(a) ((int)(a).size())
#define ff first
#define ss second
#define all(a) (a).begin(),(a).end()
#define alli(a) (a).rbegin(),(a).rend()
#define approx(a) fixed<<setprecision(a)
#define trav(a,x) for(auto& a : x)
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<int,bool> pib;
typedef pair<ll,bool> plb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> void ckmin(T &a, const T &b) { a = min(a, b); }
template <class T> void ckmax(T &a, const T &b) { a = max(a, b); }
template <class T> void read(vector<T> &v);
template <class F, class S> void read(pair<F, S> &p);
template <class T> void read(T &x) {cin>>x;}
template <class R, class... T> void read(R& r, T&... t){read(r); read(t...);};
template <class T> void read(vector<T> &v) {trav(x, v) read(x);}
template <class F, class S> void read(pair<F, S> &p) {read(p.ff, p.ss);}
template <class F, class S> void pr(const pair<F, S> &x);
template <class T> void pr(const T &x) {cout<<x;}
template <class R, class... T> void pr(const R& r, const T&... t) {pr(r); pr(t...);}
template <class F, class S> void pr(const pair<F, S> &x) {pr("{", x.ff, ", ", x.ss, "}");}
void ps() {pr("\n");}
template <class T> void ps(const T &x) {pr(x); ps();}
template <class T> void ps(vector<T> &v) {trav(x, v) pr(x, ' '); ps();}
template <class F, class S> void ps(const pair<F, S> &x) {pr(x.ff, ' ', x.ss); ps();}
template <class R, class... T> void ps(const R& r, const T &...t) {pr(r, ' '); ps(t...);}
void solve(){
ll n; read(n);
map<ll, ll> m;
vl a(n), b(n), c(n);
read(a);
read(b);
read(c);
FOR(i,0,n){
m[b[c[i]-1]]++;
}
ll ans = 0;
FOR(i,0,n){
ans+=m[a[i]];
}
ps(ans);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin>>t;
while(t--){
solve();
}
} |
#include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
using ll = long long;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i,n) for(long i=0; i<(long)(n);i++)
#define REP(i,s,t) for(long i=s; i<t; i++)
#define RNG(i,s,t,u) for(long i=s; i<t; i+=u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
int main(){
ll b,c;
cin >> b >> c;
ll h = c/2;
ll odd = c % 2;
ll sum = 0;
if(b > 0){
if(b - h > 0){
//sum += (h+1)*2 - (1 - odd);
//ll r = max((ll)0,c-1);
//sum += r-1;
sum += c+1;
sum += max((ll)0, c-2);
}else{
// sum += 2*b + 1;
// ll r = max((ll)0,c-1);
// sum += r-1;
sum += b*2 + 1;
sum += max((ll)0, c-2);
}
}else if(b < 0){
ll rb = -1*b;
if(rb*2 + 1 > c){
sum += c;
sum += max((ll)1, c);
}else{
sum += 2*rb;
sum += max((ll)1, c);
}
}else{
sum += max((ll)1, c);
}
cout << sum << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
static long long INF = (1LL<<62);
// b>0が前提
LL g(LL b, LL c){
if(c == 1) return 2;
if(c == 2) return 3;
LL l = -b - ((c-1)/2);
LL r = -l;
if((c-1)%2 == 0){ r--; }
LL e = b - (c/2);
LL ret;
if(e > 0){
if(c%2 == 0){ e++; }
// l~rのうち、-e~eを除く
ret = r - l + 1 - (2*(e-1) + 1);
if(c%2 == 0){ ret++; } //eの分をカウントする
}
else{
// l~r全てが対象
ret = r - l + 1;
}
return ret;
}
// b<0が前提
LL h(LL b, LL c){
if(b != 0 && c == 1){ return 2; }
if(b == 0 && c == 1){ return 1; }
LL l = b - (c/2);
LL r = -l;
if(c%2 == 0){ r--; }
LL e = -b - ((c-1)/2);
LL ret;
if(e > 0){
if((c-1)%2 == 0){ e++; }
// l~rのうち、-e~eを除く
ret = r - l + 1 - (2*(e-1) + 1);
if((c-1)%2 == 0){ ret++; } //eの分をカウントする
}
else{
// l~r全てが対象
ret = r - l + 1;
}
return ret;
}
LL f(LL b, LL c){
if(c == 0){ return 1; }
if(b < 0){ return h(b, c); }
else if(b == 0){
if(c == 1) return 1;
if(c == 2) return 2;
LL ret = h(-1, c-2);
if((1-(c-2))/2 >= 0) ret++; //hの結果が0を含まない場合、0を加算しておく
return ret;
}
else{
return g(b, c);
}
}
int main(int argc, char* argv[]){
cin.tie(0); ios::sync_with_stdio(false);
LL B, C; cin >> B >> C;
LL ans = f(B, C);
printf("%lld\n", ans);
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
for (T& x : data)
input >> x;
return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
output << "(" << data.x << "," << data.y << ")";
return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
for (const T& x : data)
output << x << " ";
return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
trav(a, x)
cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
ll gcd(ll a, ll b) {
while (b) {
tie(a, b) = mp(b, a % b);
}
return a;
}
signed main() {
#ifdef LOCAL
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
string s;
cin >> s;
vi a(n + 1);
cin >> a;
auto check = [&] (vi a) {
trav (e, a) {
if (e < 0)
return 0;
}
FOR (i, n) {
if (s[i] == '<') {
if (a[i] >= a[i + 1])
return 0;
}
else {
if (a[i] <= a[i + 1])
return 0;
}
}
return 1;
};
const int INF = 1e4+7;
int l = 0, r = INF;
V <vi> ans;
while (l < r - 1) {
int m = (l + r) / 2;
V <vi> d(m, vi (n + 1));
FOR (i, n + 1) {
FOR (j, m) {
d[j][i] = a[i]/m;
}
FOR (j, a[i]%m) {
d[j][i]++;
}
}
bool ok = 1;
FOR (i, m) {
ok &= check(d[i]);
}
if (ok) {
l = m;
ans = d;
}
else {
r = m;
}
}
cout << l << endl;
trav (e, ans) {
cout << e << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
vector<int> a(n + 1);
for (int i = 0; i <= n; i++) {
cin >> a[i];
}
int k = 1 << 30;
for (int i = 0; i < n; i++) {
k = min(k, abs(a[i + 1] - a[i]));
}
cout << k << '\n';
for (int l = 0; l < k; l++) {
for (int i = 0; i <= n; i++) {
cout << a[i] / k + (l < a[i] % k) << " \n"[i == n];
}
}
//差の絶対値の最小がkになりそうだけど 微分する?
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PII;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
/*const*/ ll mod = 1e9+7;
#define mst(x, a) memset( x,a,sizeof(x) )
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)
ll read() {
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
void out(ll x) {
int stackk[40];
if (x < 0) {
putchar('-');
x = -x;
}
if (!x) {
putchar('0');
return;
}
int top = 0;
while (x) stackk[++top] = x % 10, x /= 10;
while (top) putchar(stackk[top--] + '0');
}
ll qpow(ll a,ll b) {
ll ans=1;
while(b) {
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
ll n,a[maxn],m,f[121][121][121],ans;
int main() {
n=read(),m=read();
rep(i,1,n) a[i] = read();
ans = m;
for(int len =1; len<=n ; len++) {
mst(f,0xcf);
f[0][0][0] =0 ;
for(int i=1 ; i<=n ; i++) {
f[i][0][0] = 0;
for(int j=1 ; j<=len ; j++) {
for(int k=0 ; k<len ; k++) {
f[i][j][k] = f[i-1][j][k];
f[i][j][k] = max(f[i][j][k],f[i-1][j-1][(((k-a[i])%len)+len)%len]+a[i]);
}
}
}
if(f[n][len][m%len]>=0) ans = min(ans,(m-f[n][len][m%len])/len);
}
out(ans);
return 0;
}
/*
*/ | #include <iostream>
#include <algorithm>
using namespace std;
constexpr int Z = 1e9+7;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
bool s[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
s[i][j] = c == '.';
}
}
int dp[n][m], ps[n][m][3];
fill(dp[0], dp[n], 0);
fill(ps[0][0], ps[n][0], 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!s[i][j]) {
continue;
}
if (i == 0 && j == 0) {
dp[i][j]++;
}
if (i > 0) {
dp[i][j] = (dp[i][j] + ps[i-1][j][0]) % Z;
}
if (j > 0) {
dp[i][j] = (dp[i][j] + ps[i][j-1][1]) % Z;
}
if (i > 0 && j > 0) {
dp[i][j] = (dp[i][j] + ps[i-1][j-1][2]) % Z;
}
ps[i][j][0] = ((i == 0 ? 0 : ps[i-1][j][0]) + dp[i][j]) % Z;
ps[i][j][1] = ((j == 0 ? 0 : ps[i][j-1][1]) + dp[i][j]) % Z;
ps[i][j][2] = ((i == 0 || j == 0 ? 0 : ps[i-1][j-1][2]) + dp[i][j]) % Z;
}
}
cout << dp[n-1][m-1] << endl;
return 0;
}
|
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <bitset>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
typedef long long ll;
//infの設定
#define INF32 2147483647
#define INF64 9223372036854775807
//repマクロ
#define rep(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
int main(){
ll H,W;
vector<string> S(1010),x(1010);
cin>>H>>W;
rep(i,0,H) cin>>S.at(i);
ll line=H+W-1;
int hantei=0;
rep(i,0,H+W-1){
hantei=0;
rep(j,0,W){
if(0<=i-j&&i-j<H){
if(S.at(i-j).at(j)=='R'){
if(hantei==2){
cout<<"0"<<endl;
return 0;
}else{
hantei=1;
}
}else if(S.at(i-j).at(j)=='B'){
if(hantei==1){
cout<<"0"<<endl;
return 0;
}else{
hantei=2;
}
}
}
}
if(hantei!=0) line--;
}
ll ans=1;
rep(i,0,line) ans=(ans*2)%998244353;
cout<<ans<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second
#define int ll
int dp[105][105][105];
int n, k, x;
int a[105];
int calc(int ind, int taken, int sum){
if(ind == n){
if(taken == k && sum == x%k){
return 0;
}
else{
return -1e18;
}
}
if(taken == k){
if(sum == x%k){
return 0;
}
else{
return -1e18;
}
}
if(dp[ind][taken][sum] != -1){
return dp[ind][taken][sum];
}
int ch1 = calc(ind + 1, taken + 1, (sum + a[ind])%k) + a[ind];
int ch2 = calc(ind + 1, taken, sum);
return dp[ind][taken][sum] = max(ch1, ch2);
}
int32_t main(){
FAST
cin >> n >> x;
for(int i = 0; i < n; i++){
cin >> a[i];
}
sort(a, a + n);
int ans = 1e18;
for(int i = 1; i <= n; i++){
memset(dp, -1, sizeof(dp));
k = i;
int x2 = x - calc(0, 0, 0);
if(x2 > x)continue;
if(x2%i == 0){
ans = min(ans, x2/i);
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define mp make_pair
#define pb push_back
#define all(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = n; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n); (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int INF = 1080000000;
constexpr long long LINF = 4611686015206162431;
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vc<int> X(N), C(N);
rep (i, N) cin >> X[i] >> C[i];
vc<int> cmin(N, INF), cmax(N, -INF);
rep (i, N) {
chmin(cmin[C[i]-1], X[i]);
chmax(cmax[C[i]-1], X[i]);
}
int l = 0, r = 0;
ll lt = 0, rt = 0;
rep (i, N) {
if (cmin[i] == INF) continue;
ll tlt = min(abs(l - cmax[i]) + lt, abs(r - cmax[i]) + rt) + cmax[i] - cmin[i];
ll trt = min(abs(l - cmin[i]) + lt, abs(r - cmin[i]) + rt) + cmax[i] - cmin[i];
lt = tlt, rt = trt;
l = cmin[i];
r = cmax[i];
}
cout << min(lt + abs(l), rt + abs(r)) << endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[2][2];
int main(){
int n;
scanf("%d",&n);
ll a,b;
map<int,vector<ll> > mapa;
for(int i = 0; i < n; i++){
scanf("%lld %lld",&a,&b);
mapa[b].push_back(a);
}
a = b = 0;
ll izq,der; izq = 0; der = 0;
int op = 1;
for(auto x : mapa){
sort(x.second.begin(), x.second.end());
int y = x.second.size();
dp[op][0] = min(dp[1-op][0]+abs(x.second[0]-a),dp[1-op][1] + abs(x.second[0]-b)) + abs(x.second[y-1]-x.second[0]);
dp[op][1] = min(dp[1-op][0]+abs(x.second[y-1]-a),dp[1-op][1] + abs(x.second[y-1]-b)) + abs(x.second[y-1]-x.second[0]);
a = x.second[y-1];
b = x.second[0];
op = 1-op;
}
printf("%lld\n", min(dp[1-op][0] + abs(a), dp[1-op][1] + abs(b)));
return 0;
} |
#include <bits/stdc++.h>
#define endl '\n'
#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back
using namespace std;
template<class T, class T1> int chkmin(T &x, const T1 &y) { return x > y ? x = y, 1 : 0; }
template<class T, class T1> int chkmax(T &x, const T1 &y) { return x < y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);
int n;
int a[MAXN];
int64_t sum = 0;
void read() {
cin >> n;
for(int i = 0; i < 2 * n; i++) {
cin >> a[i];
sum += a[i];
}
}
void solve() {
int r = 2 * n - 1;
int64_t ans = 0;
priority_queue<int> q;
for(int i = n; i < 2 * n; i++) {
ans += a[i];
}
for(int i = 0; i < n; i++) {
q.push(a[r--]);
if(q.top() > a[i]) {
ans -= q.top();
ans += a[i];
q.pop();
q.push(a[i]);
}
}
cout << sum - ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read();
solve();
return 0;
}
| #include<bits/stdc++.h>
#define pb push_back
template <typename _Tp>void read(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=21;
std::vector<int> e[N];
int n,col[N];
long long ans;
void dfs(int dep,long long coef){
if(dep==n){ans+=coef;
return;}
if(e[dep].empty())dfs(dep+1,coef*3LL);
else{
for(int i=0;i<3;++i){
bool flag=true;
for(auto v:e[dep]){
if(col[v]==i){flag=false;break;}
}
if(flag)col[dep]=i,dfs(dep+1,coef),col[dep]=-1;
}
}
}
int main(){
memset(col,-1,sizeof(col));
int m;read(n,m);
for(int i=1,x,y;i<=m;++i)read(x,y),--x,--y,e[x].pb(y),e[y].pb(x);
dfs(0,1);
printf("%lld\n",ans);
return 0;
} |
#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()
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;
struct BIT{
int N;
VI dat;
BIT(int N):N(N){
dat.resize(N+1,0);
}
ll sum(int k){
k++;
if(k==0)
return 0;
ll res=0;
for(;k>0;k-=k&-k)
res+=dat[k];
return res;
}
ll sum(int l, int r){
return sum(r)-sum(l-1);
}
void add(int k, ll x){
k++;
for(;k<=N;k+=k&-k)
dat[k]+=x;
}
int lowerBound(int w){
if (w<=0) return 0;
int x=0;
int k=1;
while(k*2<=N) k*=2;
for(;k>0;k/=2){
if(x+k<=N&&dat[x+k]<w){
w-=dat[x+k];
x+=k;
}
}
return x;
}
};
int main(){
ll n, m, q; cin >> n >> m >> q;
VI t(q), x(q), y(q);
REP(i,q){
cin >> t[i] >> x[i] >> y[i];
}
VI sy=y; sy.push_back(0); sort(ALL(sy));
map<int,int> mp; int cnt=1;
REP(i,q+1){
if(mp[sy[i]]==0){
mp[sy[i]]=cnt;
cnt++;
}
}
ll ans=0;
VI a(n,0), b(m,0);
BIT bta(q+10), btb(q+10);
BIT btax(q+10), btbx(q+10);
btax.add(0,n);
btbx.add(0,m);
REP(i,q){
if(t[i]==1){
int j=x[i]-1;
ll pre=btbx.sum(mp[a[j]]-1)*a[j]+btb.sum(mp[a[j]],q+3);
ll ne =btbx.sum(mp[y[i]]-1)*y[i]+btb.sum(mp[y[i]],q+3);
ans+=ne-pre;
cout << ans << endl;
bta.add(mp[a[j]],-a[j]);
bta.add(mp[y[i]],y[i]);
btax.add(mp[a[j]],-1);
btax.add(mp[y[i]],1);
a[j]=y[i];
}
else{
int j=x[i]-1;
ll pre=btax.sum(mp[b[j]]-1)*b[j]+bta.sum(mp[b[j]],q+3);
ll ne =btax.sum(mp[y[i]]-1)*y[i]+bta.sum(mp[y[i]],q+3);
ans+=ne-pre;
cout << ans << endl;
btb.add(mp[b[j]],-b[j]);
btb.add(mp[y[i]],y[i]);
btbx.add(mp[b[j]],-1);
btbx.add(mp[y[i]],1);
b[j]=y[i];
}
}
return 0;
} | #include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x, y) make_pair(x, y)
#define mem(dp, a) memset(dp, a, sizeof dp);
#define all(a) a.begin(), a.end()
#define sall(a) sort(all(a))
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll, ll> pp;
#define debug(x) cout << #x << " :: " << x << "\n";
#define debug2(x, y) cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\n";
#define debug3(x, y, z) cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z << " :: " << z << "\n";
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pp, null_type, less<pp>, rb_tree_tag, tree_order_statistics_node_update>
ll power(ll b, ll exp)
{
ll res = 1;
while (exp > 0)
{
if (exp % 2)
{
res = 1LL * res * b % mod;
}
b = 1LL * b * b % mod;
exp /= 2;
}
return res;
}
map<ll, ll> ma1,ma2; // ma2->reverse
ll n, m;
const ll maxN = 1000002;
ll bit[maxN][2][2];
void update(ll i, ll val,ll j,ll k)
{
while (i < maxN )
{
bit[i][j][k] += val;
i += (i & (-i));
}
}
ll query(ll i,ll j,ll k)
{
ll ans = 0;
while (i > 0)
{
ans += bit[i][j][k];
i -= (i & (-i));
}
return ans;
}
ll query_range(ll l, ll r,ll j,ll k)
{
return query(r, j, k) - query(l - 1, j, k);
}
int main()
{
boost
ll q;
cin >> n >> m >> q;
pair<ll, pp> Q[q];
ma1[0] = 0;
for (ll i = 0; i < q; i++)
{
ll t, x, y;
cin >> t >> x >> y;
ma1[y] = 0;
Q[i] = {t, {x, y}};
}
ll cnt = 1;
vector<ll> a(n + 1), b(m + 1);
for (auto &&temp : ma1) {
temp.second = cnt++;
ma2[cnt - 1] = temp.first;
}
ll ans = 0;
update(ma1[0], n, 0, 0); // [x][y] -> {a or b, freq or sum}
update(ma1[0], m, 1, 0);
for (ll i = 0; i < q; i++) {
ll t = Q[i].first, x = Q[i].second.first, y = Q[i].second.second;
if (t==1) {
ll prev = (query(ma1[a[x]] - 1, 1, 0) * a[x]) + query_range(ma1[a[x]], maxN - 1, 1, 1);
ans -= prev;
update(ma1[a[x]], -1, 0, 0);
update(ma1[a[x]], -a[x], 0, 1);
a[x] = y;
update(ma1[a[x]], 1, 0, 0);
update(ma1[a[x]], a[x], 0, 1);
ll cur = (query(ma1[a[x]] - 1, 1, 0) * a[x]) + query_range(ma1[a[x]], maxN - 1, 1, 1);
ans += cur;
}
else {
ll prev = (query(ma1[b[x]] - 1, 0, 0) * b[x]) + query_range(ma1[b[x]], maxN - 1, 0, 1);
ans -= prev;
update(ma1[b[x]], -1, 1, 0);
update(ma1[b[x]], -b[x], 1, 1);
b[x] = y;
update(ma1[b[x]], 1, 1, 0);
update(ma1[b[x]], b[x], 1, 1);
ll cur = (query(ma1[b[x]] - 1, 0, 0) * b[x]) + query_range(ma1[b[x]], maxN - 1, 0, 1);
ans += cur;
}
cout << ans << "\n";
}
return 0;
}
|
//abc187_f.cpp
//Sat Jan 9 15:11:53 2021
#include <bits/stdc++.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i,n) for (int i=0;i<(n);++i)
using namespace std;
using ll=long long;
typedef pair<int,int> P;
int main(){
int n,m;
cin >> n >> m;
vector<int> e(n,0);
rep(i,m){
int a,b;
cin >> a >> b;
a--;b--;
e[a] += 1<<b;
e[b] += 1<<a;
}
vector<int> dp(1<<n,99999);
dp[0] = 0;
rep(i,1<<n){
if (i==0)continue;
rep(j,n){
if (1<<j&i){
int target = i-(1<<j);
if (dp[target]<=1 && (e[j]&target)==target){
dp[i] = 1;
}
break;
}
}
}
rep(i,1<<n){
for (int j=i;j>0;j=(j-1)&i){
int k = i-j;
dp[i] = min(dp[i],dp[j]+dp[k]);
}
}
cout << dp[(1<<n)-1] << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Vec = vector<ll>;
using VecP = vector<P>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
template <class T1, class T2>
bool chmax(T1 &a, const T2 b) { return a < b ? a = b, true : false; }
template <class T1, class T2>
bool chmin(T1 &a, const T2 b) { return a > b ? a = b, true : false; }
template <class T>
istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; }
void _co() { cout << '\n'; }
template <class Head, class... Tail>
void _co(Head&& head, Tail&&... tail) { cout << ' ' << head; _co(forward<Tail>(tail)...); }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) { cout << head; _co(forward<Tail>(tail)...); }
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); }
void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); }
void setp(const int n) { cout << fixed << setprecision(n); }
constexpr int64_t INF = 1000000000000000001;
constexpr int64_t MOD = 1000000007;
constexpr int64_t MOD_N = 998244353;
constexpr long double EPS = 1e-11;
const double PI = acos(-1);
int main(void) {
ll n, m;
cin >> n >> m;
const int N = 1 << n;
vector<bitset<18>> g(n);
rep(i, m) {
ll a, b;
cin >> a >> b;
--a, --b;
g[a][b] = true;
g[b][a] = true;
}
rep(i, n) g[i][i] = true;
Vec dp(1 << n, INF);
dp[0] = 0;
rep(bit, N) {
bool flg = true;
rep(i, n) {
rep(j, n) {
if ((bit >> i) & (bit >> j) & 1)
flg &= g[i][j];
}
}
if (flg)
dp[bit] = 1;
}
rep(bit, N) {
for (int64_t i = bit; --i &= bit; ) {
chmin(dp[bit], dp[i] + dp[bit ^ i]);
}
}
co(dp.back());
return 0;
} |
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
#pragma GCC optimize("Ofast")
using namespace std;
#define int long long
#define double long double
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ArrayFill(arr,a) memset(arr,a,sizeof arr)
#define endl '\n'
#define countBit(x) __builtin_popcountll(x)
#define all(x) x.begin() , x.end()
#define ln cout<<'\n';
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> oset;
int ceil(int a,int b){return (a+b-1)/b;}
#define sim template < class c
#define ris return * this
#define dor > debug_ & operator <<
#define eni(x) sim > typename enable_if<sizeof dud<c>(0) x 1, debug_&>::type operator<<(c i) {
sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...);
struct debug_ {~debug_() { cerr << endl; }eni(!=) cerr << boolalpha << i; ris; }eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {ris << "(" << d.first << ", " << d.second << ")";}
sim dor(rge<c> d){*this << "[";for (auto it = d.b; it != d.e; ++it)*this << ", " + 2 * (it == d.b) << *it;ris << "]";}};
#define db(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define debug(x) debug_() << db(x)
const int MOD = 1000000007ll;
const int N = 1e5+100;
const int inf = 1e18;
vector<array<int,3>> graph[N];
signed main(){
fast;
int n,m,x,y;
cin >> n >> m >> x >> y;
for(int i=0;i<m;i++){
int a,b,c,d;
cin >> a >> b >> c >> d;
graph[a].push_back({b,c,d});
graph[b].push_back({a,c,d});
}
set<array<int,2>> q;
q.insert({0,x});
vector<int> dist(n+1,inf);
dist[x] = 0;
while(!q.empty()){
auto cur = *q.begin();
q.erase(cur);
int cost = cur[0];
for(auto u: graph[cur[1]]){
int nx = u[0];
int t = u[1];
int md = u[2];
int cst = (md - (dist[cur[1]]%md))%md;
t+=cst;
if(dist[cur[1]] + t < dist[nx]){
q.insert({dist[cur[1]] + t, nx});
dist[nx] = dist[cur[1]] + t;
}
}
}
if(dist[y]>1e17)dist[y]=-1;
cout << dist[y];ln;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FORI(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define FORD(i, a, b) for (int i = (int)a; i >= (int)b; i--)
#define PB push_back
#define ALL(v) v.begin(),v.end()
#define OJ \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vpii;
const ll MAX = 1e16;
struct Edge{
ll to, t, k;
};
ll dijkstra(vector<vector<Edge>>& graph, int x, int y) {
vll distTo(graph.size(), MAX);
set<pair<ll, ll>> st;
distTo[x] = 0;
st.insert({0, x});
while (st.size() > 0) {
// cout << "in side\n";
pair<ll,ll> cur = *st.begin();
st.erase(st.begin());
if (cur.second == y) break;
int curVertex = cur.second;
ll curDis = cur.first;
// cout << "ver: " << curVertex << endl;
// cout << "dis: " << curDis << endl;
for (auto adj : graph[curVertex]) {
ll distToAdj = distTo[adj.to];
ll waitTime = 0;
if (curDis % adj.k != 0) {
waitTime = adj.k - curDis % adj.k;
}
if (curDis + adj.t + waitTime <= distTo[adj.to]) {
distTo[adj.to] = curDis + adj.t + waitTime;
if (st.count({distToAdj, adj.to})) {
st.erase({distToAdj, adj.to});
}
st.insert({distTo[adj.to], adj.to});
}
}
}
// FORI(i, 0, distTo.size() - 1) {
// cout << "dist to " << i << " : " << distTo[i] << endl;
// }
// cout << "Here\n";
return distTo[y];
}
int main() {
int n, m, x, y; cin >> n >> m >> x >> y;
x--, y--;
vector<vector<Edge>> graph(n, vector<Edge>());
FORI(i, 1, m) {
int a, b, c, d; cin >> a >> b >> c >> d;
a--, b--;
graph[a].push_back({b, c, d});
graph[b].push_back({a, c, d});
}
ll ans = dijkstra(graph, x, y);
if (ans == MAX) cout << -1;
else cout << ans;
return 0;
} |
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int,int>;
using PL = pair<ll,ll>;
using PPL = pair<pair<ll,ll>,ll>;
#define INF 1000000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)
#define mp make_pair
#define tp make_tuple
#define ALL(vec) vec.begin(), vec.end()
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll INFLL = numeric_limits<ll>::max();
ll t;cin>>t;
rep(_,0,t){
ll x,y,p,q;cin>>x>>y>>p>>q;
ll mod1 = (2*x+2*y)%(p+q);
ll ans = INFLL;
rep(i,x,x+y){
if (mod1==0 && p<=i%(p+q) && i%(p+q) <p+q){
ans = min(ans, (ll)i);
continue;
}
rep(j,p,p+q){
ll num = i%(p+q);
ll num2 = j%(p+q);
ll num3 = (num2-num+p+q)%(p+q);
ll d = __gcd(__gcd((ll)num3,mod1),p+q);
ll mod3 = mod1 / d;
num3/=d;
ll mod2 = (p+q)/d;
if (__gcd(mod3,mod2)==1){
ll a1 = modinv(mod3, mod2);
ans = min(ans, (a1*num3)%mod2*(2*x+2*y)+i);
}
}
}
if (ans==INFLL){
cout<<"infinity"<<endl;
continue;
}
cout<<ans<<endl;
}
} | #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;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i,n) for(int i=0;i<n;i++)
#define ALL(v) v.begin(),v.end()
ll GCD(ll a, ll b) {
if(a<b) swap(a,b);
ll r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;
}
return b;
}
int main(){
int n; cin >> n;
VI a(n); REP(i,n) cin >> a[i];
ll ans=a[0];
REP(i,n){
ans=GCD(ans,a[i]);
}
cout << ans << endl;
return 0;
} | #include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=1e5+100;
int a[maxn],n;
int num1=0x3f3f3f3f;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
num1=min(num1,a[i]);
}
while(1){
int y=num1;
for(int i=1;i<=n;i++){
if(a[i]%num1==0) a[i]=num1;
else{
int x=a[i]%num1;
a[i]=x;
num1=x;
}
}
if(y==num1)
{
printf("%d\n",y);
break;
}
}
} |
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+100;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)
int n,m;
int t;
int a[N];
map<int,int> m1;
void solve(){
m1.clear();
scanf("%d",&n);
fr(i,1,n)scanf("%d",a+i);
int res1 = 0,res2 = 0;
fr(i,1,n){
res1 ^= a[i]; m1[a[i]] ^= 1;
}
fr(i,1,n)res2 |= m1[a[i]];
if(res1){
if(n%2 == 0)puts("First");
else puts("Second");
} else if(res2){
if(n%2 == 0)puts("First");
else puts("Second");
} else {
if(n%2 == 0)puts("Second");
else puts("First");
}
return ;
}
int main(){
scanf("%d",&t);
while(t--)solve();
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
const long long INF = 1e18;
//const ll mod = 1000000007;
void solve() {
ll N;
cin >> N;
map<ll, ll> mp;
for(int i = 0; i < N; i++) {
ll a;
cin >> a;
mp[a]++;
}
if(N % 2 == 1) {
cout << "Second" << endl;
return;
}
for(auto tmp : mp) {
if(tmp.second % 2 == 1) {
cout << "First" << endl;
return;
}
}
cout << "Second" << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll T;
cin >> T;
while(T--) solve();
return 0;
}
|
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
#define M 1000000007
#define pb push_back
#define bg begin
#define all(x) x.bg(),x.end()
#define pob pop_back
#define rep(x,y,j) for(ll j = x;j < y ;j++)
#define sz size
#define ss second
#define ff first
#define SUM accumulate
#define INF 2000000000000000000
#define SPACE " "
#define vr std::vector
#define que queue
#define dqu deque
#define stk stack
#define pqu priority_queue
#define mst multiset
#define pf push_front
#define pof pop_front
#define sqr(x) ((x)*(x))
#define cube(x) (x)*sqr(x)
// graph class
void solve(){
// std::cout << std::setprecision(9) << std::showpoint << std::fixed;
ll a,b,c;
cin >> a>>b>>c;
cout << max(a+b,max(b+c,c+a))<<endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tt= 1;
// cin >> tt;
while (tt--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
void printmat(const vector<vector<char>>& mat) {
for (auto row : mat) {
for (auto elem : row)
cout << elem << " ";
cout << endl;
}
}
void printv(const vector<long long>& v) {
for (auto elem : v)
cout << elem << " ";
cout << endl;
}
void printht(const unordered_map<long long, int>& ht) {
for (auto elem : ht)
cout << elem.first << " : " << elem.second << endl;
}
void printmp(const map<int, int>& ht) {
for (auto elem : ht)
cout << elem.first << " : " << elem.second << endl;
}
void printst(const set<int>& st) {
for (auto elem : st)
cout << elem << " ";
cout << endl;
}
int main() {
int T=1, caseIdx=0;
//cin >> T;
while (T--) {
//caseIdx++;
vector<int> v(3);
cin >> v[0] >> v[1] >> v[2];
cout << (accumulate(v.begin(), v.end(), 0) - *min_element(v.begin(), v.end())) << endl;
//cout << "Case #" << caseIdx << ": " << ans << endl;
}
}
|
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 998244353;
const ll MAX_N = 5;
// a^n (mod.MOD)を求める。計算量はO(logn)
ll modpow(ll a, ll n, ll MOD = mod) {
if(n == 0) {
return 1;
}
if(n%2 == 1) {
return (a * modpow(a, n-1, MOD))%MOD;
}
return (modpow(a, n/2, MOD) * modpow(a, n/2, MOD))%MOD;
}
ll inv[MAX_N]; // inv[i] : iの逆数のmod
void init() {
inv[0] = inv[1] = 1;
for(ll i = 1; i < MAX_N; i++) {
if(i >= 2) {
inv[i] = mod - inv[mod%i]*(mod/i)%mod;
}
}
}
//--- main関数でinit()を呼び出すのを忘れるな ---//
int main() {
ll h, w, k;
cin >> h >> w >> k;
vvl board(h, vl(w, -1));
rep(i, k) {
ll x, y;
char c;
cin >> x >> y >> c;
x--; y--;
if(c == 'R') {
board[x][y] = 0;
}
else if(c == 'D') {
board[x][y] = 1;
}
else if(c == 'X') {
board[x][y] = 2;
}
}
init();
vvl dp(h, vl(w)); // dp[i][j] : (i, j)への移動経路数の総和
dp[0][0] = modpow(3LL, h*w - k);
exrep(j, 1, w-1) {
if(board[0][j-1] == -1) {
dp[0][j] = 2*inv[3]*dp[0][j-1]%mod;
}
else if(board[0][j-1] == 1) {
dp[0][j] = 0;
}
else {
dp[0][j] = dp[0][j-1];
}
}
exrep(i, 1, h-1) {
if(board[i-1][0] == -1) {
dp[i][0] = 2*inv[3]*dp[i-1][0]%mod;
}
else if(board[i-1][0] == 0) {
dp[i][0] = 0;
}
else {
dp[i][0] = dp[i-1][0];
}
}
exrep(i, 1, h-1) {
exrep(j, 1, w-1) {
if(board[i-1][j] == -1) {
dp[i][j] += 2*inv[3]*dp[i-1][j];
}
else if(board[i-1][j] == 1 || board[i-1][j] == 2) {
dp[i][j] += dp[i-1][j];
}
if(board[i][j-1] == -1) {
dp[i][j] += 2*inv[3]*dp[i][j-1];
}
else if(board[i][j-1] == 0 || board[i][j-1] == 2) {
dp[i][j] += dp[i][j-1];
}
dp[i][j] %= mod;
}
}
out(dp[h-1][w-1]);
re0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define INF 0x7f7f7f7f
#define LL_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 998244353
vector<ll> three(5000*5000+1);
void solve() {
int n, m, k; cin >> n >> m >> k;
vector<vector<int>> r(n+1, vector<int>(m+1));
vector<vector<int>> d(n+1, vector<int>(m+1));
vector<vector<int>> x(n+1, vector<int>(m+1));
vector<vector<int>> a(n+1, vector<int>(m+1));
vector<vector<int>> num(n+1, vector<int>(m+1));
for(int i=1;i<=n*m;i++) three[i] = (three[i-1] * 3) % MOD;
for(int i=0;i<k;i++) {
int x, y;
char c;
cin >> x >> y;
cin >> c;
if(c == 'R') {
a[x][y] = 1;
}
else if(c == 'D') {
a[x][y] = 2;
}
else if(c == 'X') {
a[x][y] = 3;
}
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
num[i][j] = num[i-1][j] + num[i][j-1] - num[i-1][j-1] + (a[i][j] == 0);
}
}
if(a[1][1] == 0) {
r[1][1] = d[1][1] = x[1][1] = 1;
} else if(a[1][1] == 1) {
r[1][1] = 1;
} else if(a[1][1] == 2) {
d[1][1] = 1;
} else if(a[1][1] == 3) {
x[1][1] = 1;
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
if(i == 1 && j == 1) continue;
r[i][j] = (((r[i][j-1] + x[i][j-1]) % MOD) * three[num[i][j] - num[i][j - 1] - (a[i][j] == 0)]) % MOD;
r[i][j] += (((d[i-1][j] + x[i-1][j]) % MOD) * three[num[i][j] - num[i - 1][j] - (a[i][j] == 0)]) % MOD;
r[i][j] %= MOD;
x[i][j] = d[i][j] = r[i][j];
if(a[i][j] == 1) {
d[i][j] = x[i][j] = 0;
}
else if(a[i][j] == 2) {
r[i][j] = x[i][j] = 0;
}
else if(a[i][j] == 3) {
r[i][j] = d[i][j] = 0;
}
}
}
cout << ((ll)r[n][m] + (ll)d[n][m] + (ll)x[n][m]) % MOD << endl;
}
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
three[0] = 1;
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int N,T;
cin >> N >> T;
int x = N/2;
int y = N-x;
vector<int>a(x),b(y);
for(int i=0; i<x; i++){
cin >> a[i];
}
for(int i=0; i<y; i++){
cin >> b[i];
}
vector<int>c;
for(int bit=0; bit<(1<<x); bit++){
int sum = 0;
for(int j=0; j<x; j++){
if(bit&(1<<j)){
sum += a[j];
}
}
c.push_back(sum);
}
vector<int>d;
for(int bit=0; bit<(1<<y); bit++){
int sum = 0;
for(int j=0; j<y; j++){
if(bit&(1<<j)){
sum += b[j];
}
}
d.push_back(sum);
}
sort(c.begin(),c.end());
sort(d.begin(),d.end());
int ans = 0;
int point = 1;
for(int i=0; i<(int)c.size(); i++){
if(c[i] > T){
continue;
}
int a = upper_bound(d.begin(),d.end(),T-c[i])-d.begin();
a--;
if(a>=0){
ans = max(ans,c[i]+d[a]);
}
}
cout << ans << endl;
} | #pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
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 to[400001], ne[400001], he[200001];
int Q[200001], pl[200001], dl[200001];
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int N = getint(), K = getint();
int k = 1;
rep(i, N - 1) {
int u = getint(), v = getint();
to[k] = v;
ne[k] = he[u];
he[u] = k++;
to[k] = u;
ne[k] = he[v];
he[v] = k++;
}
int q = 0, p = 0;
Q[q++] = 1;
while (p < N) {
int u = Q[p++];
int tmp = 0;
int ind = he[u];
he[u] = 0;
while (ind) {
int v = to[ind];
tmp = ne[ind];
if (he[v] > 0) {
Q[q++] = v;
ne[ind] = he[u];
he[u] = -ind;
}
ind = tmp;
}
}
int are = 1000000, pd0 = are;
int L = 0, R = (N + K - 1) / K;
while (L + 1 < R) {
int wj = (L + R) / 2;
int num = 0;
for (int i = N - 1; i >= 0; i--) {
int po = pd0 - 1;
int dd = pd0;
int u = Q[i];
for (int ind = -he[u]; ind; ind = -ne[ind]) {
int v = to[ind];
if (po < pl[v]) po = pl[v];
if (dd < dl[v]) dd = dl[v];
}
if (po >= dd) dd = 0;
if (dd >= pd0 + wj) {
num++;
po = pd0 + wj;
dd = 0;
}
pl[u] = po - 1;
dl[u] = dd + 1;
}
int are = num + (dl[1] > pd0);
if (are > K) L = wj;
else R = wj;
pd0 += are;
}
printf("%d", R);
Would you please return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).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 (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int a,b;cin >> a >> b;
if(a+b>=15&&b>=8) cout << 1 << endl;
else if(a+b>=10&&b>=3) cout << 2 << endl;
else if(a+b>=3) cout << 3 << endl;
else cout << 4 << endl;
} | #pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
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
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
int kazu[1500001];
int A[1500001];
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int N = getint(), M = getint();
auto a = A;
rep(i, M) {
kazu[(*a++ = getint())]++;
}
int kotae = 0;
while (kazu[kotae]) kotae++;
auto b = A;
for (int i = M; i < N; i++) {
kazu[(*a++ = getint())]++;
kazu[*b]--;
if (kazu[*b] == 0) {
if (*b < kotae) kotae = *b;
}
b++;
}
printf("%d", kotae);
Would you please 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;
// agr set ki tarah krna hai to less vrna agr multiset ki tarah to less_equal
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
long long int mod97 = 1000000007;
long long int modg = 998244353;
double pi = 3.1415926536;
bool comp(pair<long long int, long long int>a, pair<long long int, long long int>b)
{
if (a.first == b.first)
{
if (a.second <= b.second)
return true;
else
return false;
}
if (a.first > b.first)
return true;
else return false;
}
bool rev(long long int a, long long int b)
{
if (a > b)return true;
else return false;
}
bool compa(pair<string, pair<int, int>>a, pair<string, pair<int, int>>b)
{
if (a.first == b.first)
{
if (a.second.first > b.second.first)
return true;
else
return false;
}
if (a.first < b.first)
return true;
else
return false;
}
long long int gcd(long long int bdda, long long int chota)
{
if (chota == 0)
return bdda;
return gcd(chota, bdda % chota);
}
long long int lcm(long long int a, long long int b)
{
return a * b / gcd(max(a, b), min(a, b));
}
long long int powerm(long long int a, long long int n, long long int modn)
{
if (n == 0)return 1;
if (n % 2 == 0)
{
long long int val = powerm(a, n / 2, modn);
val = (val * val) % modn; return val;
}
else
{
long long int val = powerm(a, n / 2, modn);
val = ((val * val) % modn * a) % modn; return val;
}
}
long long int power(long long int a, long long int n)
{
if (n == 0)return 1;
if (n % 2 == 0)
{
long long int val = power(a, n / 2);
val = (val * val); return val;
}
else
{
long long int val = power(a, n / 2);
val = ((val * val) * a) ; return val;
}
}
bool comppp(string a, string b)
{
if (a.size() > b.size())return true;
else return false;
}
bool compp(pair<long long int, long long int>a, pair<long long int, long long int>b)
{
if (a.second < b.second)
return true;
else if (a.second > b.second)
return false;
else
{
if (a.first < b.first)return true;
else return false;
}
}
bool compar(pair<long long int, long long int>a, pair<long long int, long long int>b)
{
if (a.second < b.second)return true;
else if (a.second > b.second)return false;
else
{
if (a.first >= b.first)return true; else return false;
}
}
void solve()
{
long long int n; cin >> n; pair<long long int, long long int>a[n];
for (long long int i = 0; i < n; i++)cin >> a[i].first >> a[i].second;
long long int ans = 0; sort(a, a + n);
for (long long int i = 0; i < n; i++)
{
for (long long int j = i + 1; j < n; j++)
{
long long int dx = a[j].first - a[i].first, dy = a[j].second - a[i].second;
if (abs(dx) >= abs(dy))ans++;
}
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
t = 1;
//cin >> t;// long long int count = 1;
while (t--)
{
//cout << "Case #" << count << ": ";
solve();
//count++;
}
} | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
int N; cin >> N;
vector<int> A(N);
REP(i, 0, N) cin >> A[i];
int ans = 1 << 30;
REP(i, 0, 1 << N) {
int x = 0, d = i & 1, o = A[0];
REP(j, 1, N) {
if (i & (1 << j)) {
if (d) {
o |= A[j];
} else {
x ^= o;
o = A[j];
d = 1;
}
} else {
if (d) {
x ^= o;
o = A[j];
d = 0;
} else {
o |= A[j];
}
}
}
x ^= o;
ans = min(ans, x);
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <tuple>
#include <math.h>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <map>
#include <queue>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define DEBUG
#define fi first
#define se second
#define pqueue priority_queue
#define pb(x) push_back(x)
#define endl '\n'
#define all(x) x.begin(), x.end()
#define int long long
#define mk(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ull> vull;
typedef vector<ll> vll;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vector<ll> > vvll;
typedef vector<char> vc;
const int inf = 1e9 + 228;
const ll infll = 1e18;
const ll MOD = 1e9 + 7;
//static const int maxn = 1e6 + 228;
const ld eps = 1e-5;
const int K = 31;
const ld eps2 = 1e-9;
const ll MOD2 = 998244353;
const ll dosz = 5e5;
const ll SZ = (1 << 18);
const ld PI = 3.1415926535;
void fast_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("a.in", "r", stdin);
// freopen("262144.out", "w", stdout);
}
void solve() {
string s;
cin >> s;
string temp = "atcoder";
if(s>temp){
cout << 0 << endl;
return;
}
int n = s.size();
for(int i=1; i<n; i++){
if(s[i] > 't'){
cout << max(i-1, 0ll) << endl;
return;
} if(s[i] > 'a'){
cout << i << endl;
return;
}
}
cout << -1 << endl;
}
signed main() {
fast_io();
srand(time(NULL));
// cout << fixed << setprecision(3);
int q = 1;
cin >> q;
while (q--)
solve();
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF_L = 1LL << 60;
const int INF_I = 1 << 30;
const int MOD = (int)1e9 + 7;
const double PI = acos(-1);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int main()
{
int n;
cin >> n;
vector<int> a(n), p(n), x(n);
rep(i, n) cin >> a[i] >> p[i] >> x[i];
rep(i, n)
{
x[i] -= a[i];
x[i] = max(x[i], 0);
}
int ans = 1001001001;
rep(i, n)
{
if (x[i])
ans = min(p[i], ans);
}
cout << (ans == 1001001001 ? -1 : ans) << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 7;
struct Node {
LL a, b;
} in[N];
bool cmp(Node o1, Node o2) {
return (2 * o1.a + o1.b) > (2 * o2.a + o2.b);
}
int main(void) {
int n;
LL sa = 0, sb = 0;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> in[i].a >> in[i].b;
sa += in[i].a;
}
sort(in + 1, in + n + 1, cmp);
for (int i = 1; i <= n; ++i) {
LL a = in[i].a, b = in[i].b;
sa -= a;
sb += a + b;
if (sb > sa) {
cout << i;
return 0;
}
}
return 0;
}
| #include <bits/stdc++.h>
#define rep2(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) rep2(i,0,n)
#define rep1(i,n) rep2(i,1,n+1)
#define FOR(i) for(cin>>i;i>0;i--)
#define elif else if
#define pb push_back
#define ll long long
#define mp make_pair
#define all(x) x.begin(),x.end()
#define re(x) {cout<<x<<endl;return 0;}
//#define x first
//#define y second
using namespace std;
ll n,a[222222],b,ans=0,l=-1,r;
int main()
{
cin>>n;
r=n;
a[0]=0;
rep1(i,n)
{
cin>>b>>a[i];
ans+=b;
a[i]+=b*2;
}
sort(a+1,a+n+1);
reverse(a+1,a+n+1);
rep1(i,n) a[i]+=a[i-1];
while(l<r-1)
{
ll mid=(l+r)/2;
if(a[mid]>ans) r=mid;
else l=mid;
}
re(r)
return 0;} |
#include<bits/stdc++.h>
using namespace std;
int h,w,g[3000][3000],f[3000][3000],u[3000][3000];
int T(int x,int y){
return ((x+y)%2)==1;
}
int dfs(int x,int y){
if(u[x][y]){
return f[x][y];
}
u[x][y]=1;
if(x+1>h){
f[x][y]=dfs(x,y+1)+g[x][y];
}else if(y+1>w){
f[x][y]=dfs(x+1,y)+g[x][y];
}else{
if(T(x,y)){
//Turn of Aoki
f[x][y]=min(dfs(x+1,y),dfs(x,y+1))+g[x][y];
}else{
//Turn of Takahashi
f[x][y]=max(dfs(x+1,y),dfs(x,y+1))+g[x][y];
}
}
return f[x][y];
}
int main(){
cin>>h>>w;
for(int i=1;i<=h;i++){
string s;
cin>>s;
for(int j=1;j<=w;j++){
if(s[j-1]=='+')g[i][j]=1;
else g[i][j]=-1;
if(!T(i,j))g[i][j]*=-1;
}
}
u[h][w]=1;
f[h][w]=g[h][w];
int ans=dfs(1,1)-g[1][1];
if(ans==0)puts("Draw");
if(ans>0)puts("Takahashi");
if(ans<0)puts("Aoki");
} | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h, w;
cin >> h >> w;
vector<string> grid(h);
for (int i=0; i<h; i++)
cin >> grid[i];
auto get = [&] (int r, int c) -> int {
return grid[r][c] == '+' ? 1 : -1;
};
vector<vector<vector<int>>> dp(h, vector<vector<int>>(w, vector<int>(2)));
for (int i=h-1; i>=0; i--)
for (int j=w-1; j>=0; j--) {
if (i == h - 1 && j == w - 1) continue;
dp[i][j][0] = max(i + 1 < h ? dp[i+1][j][1] + get(i + 1, j) : -1e9, j + 1 < w ? dp[i][j+1][1] + get(i, j + 1) : -1e9);
dp[i][j][1] = min(i + 1 < h ? dp[i+1][j][0] - get(i + 1, j) : 1e9, j + 1 < w ? dp[i][j+1][0] - get(i, j + 1) : 1e9);
}
if (dp[0][0][0] > 0) cout << "Takahashi\n";
else if (dp[0][0][0] < 0) cout << "Aoki\n";
else cout << "Draw\n";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fr(i,n) for(int i=0;i<n;i++)
#define fro(i,j,n) for(int i=j;i<n;i++)
#define ifr(i,n) for(int i=n-1;i>=0;i--)
int main() {
int n;
cin >> n;
cout <<"6 10 15 ";
int count=3;
fro(i,16,10001){
if(count==n)break;
if(i%6==0||i%10==0||i%15==0){
cout <<i<<" ";
count++;
}
}
} | #include <bits/stdc++.h>
using namespace std;
const int kN = 10010;
vector<int> pr;
bool notp[kN];
bool vis[kN];
void Preprocess() {
for(int i = 2; i < kN; i++) {
if(!notp[i]) pr.push_back(i);
for(int p : pr) {
if(i * p >= kN) break;
notp[i * p] = 1;
if(i % p == 0) break;
}
}
}
vector<int> ans;
bool check(int x) {
return x % 6 == 0 || x % 10 == 0 || x % 15 == 0;
}
void Dfs(int id, int x) {
if(ans.size() >= 2500) return;
if(x * pr[id] > 10000) return;
if(!check(x * pr[id])) printf("x = %d, p = %d\n", x, pr[id]);
Dfs(id + 1, x);
int tot = x;
while(tot * pr[id] <= 10000) {
if(!vis[tot * pr[id]]) {
ans.push_back(tot * pr[id]), vis[tot * pr[id]] = 1;
}
Dfs(id + 1, tot * pr[id]);
tot *= pr[id];
}
}
void Solve() {
Dfs(0, 6);
Dfs(0, 10);
Dfs(1, 15);
//for(int j = 4; j <= 30; j++) {
// for(int mask = 0; mask < (1 << (j - 3)); j++) {
// int tmp = 1;
// for(int i = 3; i < j; i++) if(mask & (1 << (i - 3))) {
// tmp *= pr[i];
// if(tmp > 10000) break;
// }
// if(tmp * 6 <= 10000) Dfs(j + 1, tmp * 6);
// if(tmp * 10 <= 10000) Dfs(j + 1, tmp * 10);
// if(tmp * 15 <= 10000) Dfs(j + 1, tmp * 15);
// if(tmp * 30 <= 10000) Dfs(j + 1, tmp * 15);
// }
//}
}
int main() {
Preprocess();
int n; scanf("%d", &n);
ans.push_back(6), ans.push_back(10), ans.push_back(15);
Solve();
//printf("size = %d\n", int(ans.size()));
//for(int x : ans) printf("%d ", x); puts("");
//for(int x : ans) {
// if(x % 6 != 0 && x % 10 != 0 && x % 15 != 0) printf("x = %d\n", x);
//}
for(int i = 0; i < n; i++) printf("%d ", ans[i]); puts("");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int a,b;cin>>a>>b;
cout<<2*a+100-b<<endl;
} | #include <iostream>
#include <numeric>
#include <cmath>
#include <limits>
#include <stdio.h>
#include <iomanip>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
using namespace std;
using ll = long long;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
ll Max(ll(a), ll(b), ll(c)) {
return max(max(a, b), c);
}
ll Min(ll(a), ll(b), ll(c)) {
return min(min(a, b), c);
}
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
for (int i = 0; i < N; i++) par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); //xの根をrx
int ry = root(y); //yの根をry
if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
ll Xor(ll a, ll b) {
ll A = a, B = b;
ll C = 0;
ll k = 1;
while (A > 0 || B > 0) {
if (A % 2 != B % 2) {
C += k;
}
k *= 2;
A /= 2;
B /= 2;
}
return C;
}
ll mod = 1e9 + 7;
int main() {
string S,T;
cin >> S>>T;
if (S == "Y") {
if (T == "a" || T == "A")cout << "A" << endl;
else if (T == "b" || T == "B")cout << "B" << endl;
else if (T == "c" || T == "C")cout << "C" << endl;
}
else cout << T << endl;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i:x){cout << i << " ";} cout << endl;}
#define isin(x,l,r) ((l)<=(x) && (x)<(r))
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<typename T>bool chmin(T&x,const T&y) {if(x>y){x=y;return true;} else return false;}
template<typename T>bool chmax(T&x,const T&y) {if(x<y){x=y;return true;} else return false;}
struct UnionFind {
vector<int> d;
UnionFind(int n): d(n,-1) {}
int root(int x) {
if (d[x] < 0) return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) {
x = root(x); y = root(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x,y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y);}
int size(int x) { return -d[root(x)];}
}; // UnionFind uf(N+1); // 1-indexed
int main() {
// 連結成分ごとに計算、頂点数と出てきている色の種類のmin
int N;
cin >> N;
vector<int> colTop(400005, -1);
UnionFind uf(N); // 0-indexed
vector<pii> AB;
rep(i, N) {
int a, b;
cin >> a >> b;
AB.emplace_back(a, b);
if (colTop[a] == -1) colTop[a] = i;
else {
int c = colTop[a];
uf.unite(i, c);
}
if (colTop[b] == -1) colTop[b] = i;
else {
int c = colTop[b];
uf.unite(i, c);
}
}
vector<set<int>> st(N);
rep(i, N) {
int r = uf.root(i);
auto [a, b] = AB[i];
st[r].insert(a);
st[r].insert(b);
}
ll ans = 0;
rep(i, N) {
if (uf.root(i) == i) {
int s = uf.size(i);
int c = sz(st[i]);
ans += min(s, c);
}
}
cout << ans << '\n';
return 0;
} | #include<bits/stdc++.h>
#define L(i, j, k) for(int i = j, i##E = k; i <= i##E; i++)
#define R(i, j, k) for(int i = j, i##E = k; i >= i##E; i--)
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int, int>
#define mkp make_pair
using namespace std;
const int N = 1e4;
int n, k, p[N], pw[N], ss[N];
char s[N], t[N];
ll all = 0, ans;
bool calc() {
int sum1 = 0, sum2 = 0;
L(i, 1, 9) ss[i] = 0;
L(i, 1, 5) ss[s[i] - '0'] ++;
L(i, 1, 9) sum1 += pw[ss[i]] * i;
L(i, 1, 9) ss[i] = 0;
L(i, 1, 5) ss[t[i] - '0'] ++;
L(i, 1, 9) sum2 += pw[ss[i]] * i;
return sum1 > sum2;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
pw[0] = 1,
pw[1] = 10,
pw[2] = 100,
pw[3] = 1000,
pw[4] = 10000,
pw[5] = 100000;
cin >> k >> (s + 1) >> (t + 1);
L(i, 1, 4) p[s[i] - '0'] ++;
L(i, 1, 4) p[t[i] - '0'] ++;
all = (ll) (k * 9 - 8) * (k * 9 - 9) ;
L(i, 1, 9) L(j, 1, 9) {
s[5] = i + '0';
t[5] = j + '0';
ll pnt = 0;
if(i == j) pnt = (ll) (k - p[i]) * (k - p[i] - 1);
else pnt = (ll) (k - p[i]) * (k - p[j]);
ans += pnt * calc();
}
cout.precision(12), cout << fixed;
cout << (db) ans / all << "\n";
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;
//エラストテネスの篩(O(nloglogn))
struct sieve {
int n;
vector<int> f, primes;
sieve(int n = 1) : n(n), f(n+1) {
f[0] = f[1] = -1;
for (ll i = 2; i <= n; i++) {
if (f[i]) continue;
primes.push_back(i);
f[i] = i;
for (ll j = i * i; j <= n; j += i) {
if(!f[j]) f[j] = i;
}
}
}
bool is_prime(int x) {return f[x] == x;}
// 素因数分解したものを返す(12=2x2x3)
vector<int> factor_list(int x) {
vector<int> res;
while (x != 1) {
res.push_back(f[x]);
x /= f[x];
}
return res;
}
// xが小さいときはエラストテネスの篩を用いて素因数分解
vector<P> factor(int x) {
vector<int> fl = factor_list(x);
if (fl.size() == 0) return {};
vector<P> res(1,P(fl[0],0));
for (int p : fl) {
if (res.back().first == p) {
res.back().second++;
} else {
res.emplace_back(p,1);
}
}
return res;
}
vector<pair<ll,int>> factor(ll x) {
vector<pair<ll,int>> res;
for (int p : primes) {
int y = 0;
while (x % p == 0) {
x /= p;
y++;
}
if (y != 0) res.emplace_back(p,y);
}
if (x != 1) res.emplace_back(x,1);
return res;
}
};
ll gcd(ll a, ll b) {return(b ? gcd(b, a % b) : a);}
int main() {
int n; cin >> n;
vint x(n);
rep(i,n) cin >> x[i];
vint primes;
sieve sv(55);
for (int v = 1; v <= 50; v++) if (sv.is_prime(v)) primes.emplace_back(v);
int sz = primes.size();
ll ans = inf_l;
rep(i,1<<sz) {
ll ans_tmp = 1;
bool ok = true;
rep(j,sz) if (i >> j & 1) ans_tmp *= primes[j];
rep(j,n) {
if (gcd(x[j], ans_tmp) == 1) ok = false;
}
if (ok) ans = min(ans, ans_tmp);
}
cout << ans << endl;
} | #define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){
a = b; return true;
}
return false;
}
int main(){
std::string S; std::cin >> S;
std::reverse(std::begin(S), std::end(S));
char prv(0);
int prvidx(0);
std::vector<int> cnt(26);
++cnt[S[0]- 'a'];
long long ans(0);
for(int i(1), i_len(S.length()); i < i_len; ++i){
if(S[i] == S[i-1]){
if(prv == S[i]){
ans += i - prvidx - cnt[S[i] - 'a'] - 1;
}else{
ans += i - cnt[S[i] - 'a'];
}
prv = S[i];
prvidx = i;
for(int j(0); j < 26; ++j) cnt[j] = 0;
}else{
++cnt[S[i] - 'a'];
}
}
std::cout << ans << std::endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
const ll mod=1e9+7;
const ll N=1e5+2;
ll n,l;
ll a[N],b[N];
vector<ll>va,vb;
queue<pair<ll,ll> >q;
int main(){
ios::sync_with_stdio(false);cin.tie(0);
cin >> n >> l;
for(int i=1; i<=n+1 ;i++){
if(i!=n+1) cin >> a[i];
else a[i]=l+1;
va.push_back(a[i]-a[i-1]-1);
}
for(int i=1; i<=n+1 ;i++){
if(i!=n+1) cin >> b[i];
else b[i]=l+1;
vb.push_back(b[i]-b[i-1]-1);
}
for(int i=0; i<=n ;i++){
if(va[i]!=0) q.push({va[i],i});
}
q.push({1e18,0});
ll ans=0;
for(int i=0; i<=n ;i++){
if(vb[i]==0) continue;
ll mn=i;
ll mx=i;
while(vb[i]>0){
auto c=q.front();q.pop();
vb[i]-=c.fi;
mn=min(mn,c.se);
mx=max(mx,c.se);
}
ans+=mx-mn;
//cout << i << ' ' << mn << ' ' << mx << endl;
if(vb[i]<0) return cout << "-1\n",0;
}
cout << ans << '\n';
} | #include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
#include <stack>
#include <string>
using namespace std;
typedef long long int ll;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
//GOOD LUCK
int n;
cin >> n;
string ar[n];
map<string, int> mep;
for (int i = 0; i < n; i ++) {
cin >> ar[i];
}
sort(ar, ar+n);
for (int i = 0; i < n; i ++) {
if (ar[i][0] == '!') {
mep[ar[i].substr(1, ar[i].length()-1)] += 1;
}
else {
mep[ar[i]] *= -1;
}
}
for (pair<string, int> me: mep) {
if (me.second < 0) {
cout << me.first << endl;
return 0;
}
}
cout << "satisfiable" << endl;
/* CHECK:
int vs. ll
array bounds: n+1
don't get it wrong
actually read what's here
*/
} |
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <class T1, class T2>
ostream& operator << (ostream& out, const pair <T1, T2> p)
{
return out << '(' << p.first << ',' << p.second << ')';
}
template <class T1, class T2>
istream& operator >> (istream& in, pair<T1, T2> &p)
{
return in >> p.first >> p.second;
}
template <class T>
istream& operator >> (istream& in, vector<T> &v)
{
for (T &x : v)
in >> x;
return in;
}
template <class T>
ostream& operator << (ostream& out, const vector<vector<T>> &v)
{
for (const vector<T> &x : v)
out << x << '\n';
return out;
}
template <class T>
ostream& operator << (ostream& out, const vector<T> &v)
{
for (const T &x : v)
out << x << ' ';
return out;
}
long long gcd (long long a, long long b)
{
if (b > a)
swap(a, b);
return (b ? gcd(b, a % b) : a);
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define per(i,b,a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error ("Found the error.");
const int h = 1000000007;
signed main()
{
ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
cin.tie(nullptr);
cerr.setstate(ios::failbit);
#endif
int n;
cin >> n;
ll x;
cin >> x;
vl v(n);
cin >> v;
ll ans = LLONG_MAX;
rep(num,1,n+1)
{
vvl dp(num + 1, vl(num, -1));
dp[0][0] = 0;
for(ll x : v)
per(i,num-1,-1)
rep(j,0,num)
if(dp[i][j] != -1)
dp[i + 1][(j + x) % num] = max(dp[i + 1][(j + x) % num], dp[i][j] + x);
if(dp.back()[x % num] != -1)
ans = min(ans, (x - dp.back()[x % num]) / num);
}
cout << (ans == LLONG_MAX ? -1 : ans);
}
| #include <iostream>
using namespace std;
using ll = long long;
const ll INF = 1e18+20;
ll d[101][101][101][101];
ll a[101];
int main() {
int n;
ll x;
cin >> n >> x;
for (int i = 1; i <= n; i++){cin>>a[i];}
for (int i=0; i<=n; i++) {
for (int k=0; k<=n;k++) {
for (int p=0; p<=n; p++) {
for (int r=0; r<=n; r++) {
d[i][k][p][r]=-1;
}
}
}
}
for (int p=1; p<=n; p++) {
d[0][0][p][0] = 0;
}
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= i; k++) {
for (int p = 1; p <= n; p++) {
for (int r = 0; r < p; r++) {
d[i][k][p][r] = d[i-1][k][p][r];
ll from=d[i-1][k-1][p][(r-a[i]%p+p)%p];
if (from != -1 && from + a[i] > d[i][k][p][r]) {
d[i][k][p][r] = from + a[i];
}
}
}
}
}
ll result = INF;
for (int k=1; k<=n; k++) {
if (d[n][k][k][x%k] == -1) {
continue;
}
ll cur = (x - d[n][k][k][x%k]) / k;
if (cur < result) {
// cout << "hey " << k << " " << x % k << " " << d[n][k][k][x%k] << "\n";
result = cur;
}
}
cout << result;
// cout << "\n===================\n";
// cout << "r=1: " << d[3][2][[1] << "\n";
// cout << "\n=====\n";
// cout << d[1][1][1][0] << "\n";
// cout << d[0][];
return 0;
} |
#include<iostream>
using namespace std;
int main(){
int A[4];
cin>>A[0]>>A[1]>>A[2]>>A[3];
int t=100;
for(int i=0; i<4; i++){
if(t>A[i]){t=A[i];}
}
cout<<t<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std ;
int main(){
int a,b,c,d;
cin>>a>>b;
cin>>c>>d;
cout<<b-c<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define D(a) cerr << #a << " = " << a << endl
#else
#define D(a) 8
#endif
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define dforsn(i,s,n) for(int i=int(n-1);i>=int(s);i--)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define all(a) (a).begin(),(a).end()
#define dforn(i,n) dforsn(i,0,n)
#define forn(i,n) forsn(i,0,n)
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
using pii = pair<int,int>;
using vi = vector<int>;
using ll = long long;
int main() {
fastio;
int x, y; cin >> x >> y;
if (x > y) swap(x, y);
cout << (x + 3 > y ? "Yes" : "No") << endl;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define lchild 2*node
#define rchild 2*node+1
#define mid (l+r)/2
#define mod 1000000007
#define mx 300007
#define pii pair<int,int>
#define pci pair<int,int>
#define ff first
#define ss second
#define piii pair<int,pii>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int a,b;
cin>>a>>b;
if( min(a,b)+3>max(a,b) )
cout<<"Yes\n";
else
cout<<"No\n";
} |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define P pair<ll,ll>
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
#define per(i,n) for(ll (i)=(n)-1;(i)>=0;--(i))
#ifdef LOCAL
#define debug(x) cerr<<__LINE__<<": "<<#x<<"="<<x<<endl;
#define debugALL(x) cerr<<__LINE__<<": "<<#x<<"=";for(auto i=(x).begin();i!=--(x).end();i++)cerr<<*i<<" ";cerr<<*--(x).end()<<endl;
#else
#define debug(x) true
#define debugALL(x) true
#endif
using namespace std;
int main(){
ll n;
cin>>n;
vector<P> a;
rep(i,2*n){
ll ai;
cin>>ai;
a.emplace_back(ai,i);
}
sort(a.begin(),a.end());
V d(2*n,0);
rep(i,n){
d[a[i].second]=0;
d[a[i+n].second]=1;
}
debugALL(d);
string ans;
ll res=0;
stack<ll> sta;
rep(i,2*n){
if(res==0){
ans.push_back('(');
res++;
sta.push(d[i]);
}else{
if(sta.top()==d[i]){
sta.push(d[i]);
res++;
ans.push_back('(');
}else{
sta.pop();
res--;
ans.push_back(')');
}
}
}
cout<<ans<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a,b,sizeof(a))
#define vi vector<int>
#define vii vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define w(x) int x; cin>>x; while(x--)
#define in(a) for(auto &x: a)cin>>x;
#define out(a) for(auto x: a){cout<<x<<' ';}cout<<'\n';
#define inf 1000000000
//#define mm 1000000007 998244353
void print(bool n){if(n)cout<<"YES";else cout<<"NO";cout<<'\n';}
signed main(){
fastio
int n;
cin>>n;
int p[n];
fill(p,0);
bool a[n];
bool b[n];
char x;
vi aa,bb;
rep(i,0,n){
cin>>x;
a[i]=(x=='1');
if(!a[i])aa.pb(i);
}
rep(i,0,n){
cin>>x;
b[i]=(x=='1');
if(!b[i])bb.pb(i);
}
if(aa.size()!=bb.size()){
cout<<-1;return 0;
}
int r=0;
bool arr[n];
fill(arr,0);
rep(i,0,aa.size()){
if(aa[i]==bb[i])continue;
if(aa[i]<bb[i]){
rep(j,aa[i],bb[i]+1){
if(a[j]==0)arr[j]=1;
}
}
if(aa[i]>bb[i]){
for(int j=aa[i];j>=bb[i];j--){
if(a[j]==0)arr[j]=1;
}
}
}
int sum=0;
rep(i,0,n)sum+=arr[i];
cout<<sum;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e6+13;
int sum[maxn<<2];
int sum2[maxn<<2];
int a[maxn];
void up(int x){
sum[x]=sum[x<<1]+sum[x<<1|1];
}
void modify(int rt,int l,int r,int pos,int op){
if(l==r){
if(op) sum2[rt]-=1;
else sum2[rt]+=1;
if(sum2[rt]>0) sum[rt]=1;
else sum[rt]=0;
return;
}
int mid=l+r>>1;
if(pos<=mid) modify(rt<<1,l,mid,pos,op);
else modify(rt<<1|1,mid+1,r,pos,op);
up(rt);
}
int query(int rt,int l,int r){
if(l==r){
return l;
}
else{
int mid=l+r>>1;
if(sum[rt<<1]<mid-l+1){
return query(rt<<1,l,mid);
}
else{
return query(rt<<1|1,mid+1,r);
}
}
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
int ans=1e9;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
if(i<=m)
{
modify(1,0,n,a[i],0);
if(i==m) ans=min(ans,query(1,0,n));
}
else{
modify(1,0,n,a[i-m],1);
modify(1,0,n,a[i],0);
ans=min(ans,query(1,0,n));
}
}
printf("%d",ans);
} | #include<bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
#define ll long long int
#define ld double
#define pb push_back
#define rep(i , j , n) for(ll i = j ; i < n ; i++)
#define pre(i , j , n) for(ll i = j ; i >= n ; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef pair<ll,ll> pll;
#define br "\n"
#define ff first
#define ss second
#define gcd __gcd
#define debug(a...) cout<<#a<<": ";for(auto it:a)cout<<it<<" ";cout<<endl;
ll MAX = 1e7 + 1;
#define MAXIM 200005
ll mod = 1e9 + 7;
vll v;
ll n,q;
void add(ll ind,ll val){
while(ind <= n){
v[ind] ^= val;
ind += (ind & (-1*ind));
}
}
ll sum(ll ind){
ll x = 0;
while(ind > 0){
x ^= v[ind];
ind -= (ind & (-1*ind));
}
return x;
}
void solve(){
cin >> n >> q;
v.resize(n + 1);
rep(i,1,n + 1){
ll x;
cin >> x;
add(i,x);
}
rep(i,0,q){
ll t,a,b;
cin >> t >> a >> b;
if(t == 1) add(a,b);
else cout << (sum(b)^sum(a - 1)) << br;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t = 1;
// cin >> t;
// calc();
rep(i,0,t){
// cout << "Case #" << i + 1 << ": ";
solve();
// test();
}
} |
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define REP(a,b) for(int a=0;a<(b);++a)
#define REP1(i,n) for(int i=1;i<=(n);++i)
#define debug(x) cerr<<#x<<": "<<x<<'\n'
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isPossible(x) printf("%s\n",(x) ? "Possible" : "Impossible")
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
// #define INF (1<<9+9)
const int INF = 0x3fffffff;
// const long long INF = 1LL<<50;
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define pie 3.14159265358979323846
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
template<class T=int>
T in(){T x;cin>>x;return (x);}
template<class T>
void print(T& x){cout<<x<<'\n';}
const int MOD =(int)998244353;
// const int mod =(int)998244353;
const int mod =(int)1e9+7;
const int MAX =510000;
ll fac[MAX],finv[MAX],inv[MAX];
void COMint(){
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;
}
ll gcd(ll a,ll b){
if(a<0)a=-a;
if(b<0)b=-b;
if(b==0)return a;
if(a>b){
swap(a,b);
}
return gcd(a,b%a);
}
ll lcm(ll a,ll b){
if(a<0)a=-a;
if(b<0)b=-b;
ll g;g=gcd(a,b);
return b/g*a;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
if(a.second != b.second){
return a.second < b.second;
}else{
return a.first < b.first;
}
}
bool compare_by_a(pair<int, int> a, pair<int, int> b) {
if(a.first != b.first){
return a.first < b.first;
}else{
return a.second < b.second;
}
}
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
//int N, M, K, T, H, W, L, R;
// long long int N, M, K, T, H, W, L, R
ll RS(ll N,ll P){
if(P==0){
return 1;
}else{
if(P%2==0){
ll t=RS(N,P/2);
return t*t;
}else{
return N*RS(N,P-1);
}
}
}
int main() {
ios::sync_with_stdio(false);
int N;cin>>N;
vector<int>v(N);
REP(i,N)cin>>v[i];
int ans=INF;
REP(i,(1<<N-1)){
int ans_temp=0;
int or_temp=v[0];
REP(j,N-1){
if((i>>j)&1){
ans_temp=ans_temp^or_temp;
or_temp=v[j+1];
}else{
or_temp|=v[j+1];
}
}
ans_temp=ans_temp^or_temp;
chmin(ans,ans_temp);
}
cout<<ans<<endl;
return 0;
}
| //FIRST THINK THEN CODE.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define FOR(i,n) for(ll i=0;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vld vector<ld>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vvld vector<vector<ld>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
const ll M = 1000000007;
const ll MM = 998244353;
ll begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define trace(...)
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
template<typename T, typename F>
void chmax( T &a, F b) {
if (b > a)a = b;
}
template<typename T, typename F>
void chmin( T &a, F b) {
if (b < a)a = b;
}
/* if you want to multiply 2 64 bit numbers mod c.
i.e a*b%c.
this function will help in preventing "overflow".
*/
ll mulmod(ll a, ll b, ll c) {
ll ans = 0;
ll y = a % c;
while (b) {
if (b & 1) {
(ans += y) %= c;
}
y = y * 2 % c;
b >>= 1;
}
return ans;
}
ll powM(ll a, ll b, ll m)
{
if (a <= 0)return 0;
a %= m;
ll ans = 1LL;
while (b)
{
if (b & 1)ans = ans * a % m;
//ans = mulmod(ans, a, m);
a = a * a % m;
//a = mulmod(a, a, m);
b >>= 1;
}
return ans;
}
ll powMbig(ll a, ll b, ll m)
{
if (a <= 0)return 0;
a %= m;
ll ans = 1LL;
while (b)
{
if (b & 1)//ans = ans * a % m;
ans = mulmod(ans, a, m);
//a = a * a % m;
a = mulmod(a, a, m);
b >>= 1;
}
return ans;
}
ll poww(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
string tostring(ll x) {
stringstream sss;
sss << x;
string ans = sss.str();
return ans;
}
const ll N = 2e5 + 5;
int main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
ll h, w;
cin >> h >> w;
string g[h];
FOR(i, h)cin >> g[i];
ll ans = 0;
vl a;
ll c;
for (ll i = 0; i < h; i++) {
c = 0;
for (ll j = 0; j < w; j++) {
if (g[i][j] == '.')c++;
else {
// d1(c);
ans += max(0ll, c - 1);
c = 0;
}
}
ans += max(0ll, c - 1);
}
for (ll j = 0; j < w; j++) {
c = 0;
for (ll i = 0; i < h; i++) {
if (g[i][j] == '.')c++;
else {
//cout << "h" << " " << c << endl;
ans += max(0ll, c - 1);
c = 0;
}
}
ans += max(0ll, c - 1);
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <functional>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <queue>
#include <bitset>
#include <cassert>
#include <math.h>
#include <complex>
#include <iomanip>
#include <unordered_map>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#ifdef _OLIMPOLOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 1337
#endif
void debug_out() { cout << "\n";}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cout << " " << A; debug_out(B...); }
int test = 1;
#define out(x) {cout << x << "\n";return;}
#define all(N) N.begin(),N.end()
#define allr(N) N.rbegin(),N.rend()
template<typename T1>
void print(vector<T1> V, int add = 0, int start = -1, int end = -1) {
if (start < 0) start = 0;
if (end < 0) end = V.size() - 1;
for (int i = start; i <= end; i++) {
cout << V[i] + add << ((i == end) ? "\n" : " ");
}
}
template<typename T1>
T1 chmin(T1 &x, const T1 v) { return x = min(x,v); }
template<typename T1>
T1 chmax(T1 &x, const T1 v) { return x = max(x,v); }
#define rep(i, n) for (int i = 0; i < n; i++)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define repsr(i, s, n) for (int i = n-1; i >= s; i--)
#define PI pair<int,int>
//-------------------------- ^ DONT TOUCH ^-----------------------------------------------------------------
#define MAX 100001
#define MOD 1000000007ll
vector<int> G[MAX];
bool cmp(const pair<int, int> a,const pair<int, int> b) {
return a.first - a.second < b.first - b.second;
}
pair<int, int> dfs(int n) {
pair<int, int> r = {1,0};
pair<int, int> pp = {0,0};
vector<pair<int, int>> P;
for(auto &g : G[n]) {
auto p = dfs(g);
if ((p.first + p.second) % 2 == 0) {
if (p.second > p.first) {
r.first +=p.first;
r.second += p.second;
} else {
pp.first +=p.first;
pp.second += p.second;
}
} else
{
P.push_back(p);
}
}
if (P.size() % 2 == 0) {
r.first +=pp.first;
r.second += pp.second;
} else {
r.first +=pp.second;
r.second += pp.first;
}
sort(P.begin(), P.end(), cmp);
for(int i = 0; i < P.size(); i++) {
if (i % 2 == 0) {
r.first +=P[i].first;
r.second += P[i].second;
} else {
r.first += P[i].second;
r.second += P[i].first;
}
}
return r;
}
void solve() {
int n, x;
cin >> n;
rep(i,n) G[i].clear();
reps(i, 1, n) {
cin >> x;
G[x-1].push_back(i);
}
auto p = dfs(0);
out(p.first);
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int test = 1;
//cin >> test;
rep(testCase, test) {
#ifdef _OLIMPOLOCAL
cout << "------------------ TESTCASE: " << testCase << " -------------\n";
#endif
solve();
}
return 0;
} | //Code Written by Pinaki Bhattacharjee
#include<bits/stdc++.h>
using namespace std;
// typedef
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
// #define
#define LOCAL
#define INF 1e18
#define MAX 10000
#define PI 2*acos(0.0)
#define endl "\n"
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define set_bits __builtin_popcountll
#define DEBUG(x) cerr << #x << " = " << x << endl;
#define pinakipb2 ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// constants
const int MOD = (int) 1e9 + 7;
// debugger
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " | ";
__f(comma + 1, args...);
}
/*--------------------------------------------------------------------------------------------------------------------------*/
#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(ld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << 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.first); cerr << ","; _print(p.second); 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 << "]";}
void google(int t) {cout << "Case #" << t << ": ";}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
/*--------------------------------------------------------------------------------------------------------------------------*/
int main()
{
pinakipb2;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
// Code begins
ll n;
cin>>n;
ll arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
ll ans = 0;
ll a = 0, b = 0;
for(int i=0;i<n;i++)
{
a+=arr[i]*arr[i];
b+=arr[i];
}
ans = (n*a)-(b*b);
cout<<ans<<endl;
// Code ends
return 0;
} |
/* -*- coding: utf-8 -*-
*
* f.cc: F - Max Matrix
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 200000;
const int MAX_E2 = 1 << 19; // = 524288
const int MAX_QN = 200000;
/* typedef */
typedef long long ll;
struct Qry {
int t, x, y;
Qry() {}
Qry(int _t, int _x, int _y): t(_t), x(_x), y(_y) {}
void read() { scanf("%d%d%d", &t, &x, &y); x--; }
void print() { printf("Qry(%d,%d,%d)\n", t, x, y); }
};
template <typename T, const int MAX_E2>
struct SegTreeSum {
int e2;
T nodes[MAX_E2];
SegTreeSum() {}
void init(int n) {
for (e2 = 1; e2 < n; e2 <<= 1);
//fill(nodes, nodes + MAX_E2, 0);
}
T &geti(int i) { return nodes[e2 - 1 + i]; }
void set(int i, T v) {
int j = e2 - 1 + i;
nodes[j] = v;
while (j > 0) {
j = (j - 1) / 2;
nodes[j] = nodes[j * 2 + 1] + nodes[j * 2 + 2];
}
}
void add(int i, T v) { set(i, geti(i) + v); }
T sum_range(int r0, int r1, int k, int i0, int i1) {
if (r1 <= i0 || i1 <= r0) return 0;
if (r0 <= i0 && i1 <= r1) return nodes[k];
int im = (i0 + i1) / 2;
T v0 = sum_range(r0, r1, k * 2 + 1, i0, im);
T v1 = sum_range(r0, r1, k * 2 + 2, im, i1);
return v0 + v1;
}
T sum_range(int r0, int r1) { return sum_range(r0, r1, 0, 0, e2); }
};
/* global variables */
Qry qs[MAX_QN];
int as[MAX_N], bs[MAX_N], uas[MAX_N + 1], ubs[MAX_N + 1];
SegTreeSum<int,MAX_E2> asti, bsti;
SegTreeSum<ll,MAX_E2> astl, bstl;
/* subroutines */
inline int get_index(int un, int us[], int v) {
return lower_bound(us, us + un, v) - us;
}
/* main */
int main() {
int n, m, qn;
scanf("%d%d%d", &n, &m, &qn);
int uan = 0, ubn = 0;
uas[uan++] = ubs[ubn++] = 0;
for (int i = 0; i < qn; i++) {
qs[i].read();
if (qs[i].t == 1) uas[uan++] = qs[i].y;
else ubs[ubn++] = qs[i].y;
}
sort(uas, uas + uan);
sort(ubs, ubs + ubn);
uan = unique(uas, uas + uan) - uas;
ubn = unique(ubs, ubs + ubn) - ubs;
asti.init(uan), astl.init(uan);
asti.set(0, n);
bsti.init(ubn), bstl.init(ubn);
bsti.set(0, m);
ll sum = 0;
for (int i = 0; i < qn; i++) {
Qry &qi = qs[i];
//qi.print();
if (qi.t == 1) {
int pa = as[qi.x];
if (pa != qi.y) {
int i0 = get_index(ubn, ubs, pa);
sum -= (ll)pa * bsti.sum_range(0, i0) + bstl.sum_range(i0, ubn);
int j0 = get_index(uan, uas, pa);
asti.add(j0, -1), astl.add(j0, -pa);
int i1 = get_index(ubn, ubs, qi.y);
sum += (ll)qi.y * bsti.sum_range(0, i1) + bstl.sum_range(i1, ubn);
int j1 = get_index(uan, uas, qi.y);
asti.add(j1, 1), astl.add(j1, qi.y);
as[qi.x] = qi.y;
}
}
else {
int pb = bs[qi.x];
if (pb != qi.y) {
int i0 = get_index(uan, uas, pb);
sum -= (ll)pb * asti.sum_range(0, i0) + astl.sum_range(i0, uan);
int j0 = get_index(ubn, ubs, pb);
bsti.add(j0, -1), bstl.add(j0, -pb);
int i1 = get_index(uan, uas, qi.y);
sum += (ll)qi.y * asti.sum_range(0, i1) + astl.sum_range(i1, uan);
int j1 = get_index(ubn, ubs, qi.y);
bsti.add(j1, 1), bstl.add(j1, qi.y);
bs[qi.x] = qi.y;
}
}
printf("%lld\n", sum);
}
return 0;
}
| #include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),i##end=(b);i<i##end;i++)
#define REP(i,a,b) for(int i=(a),i##end=(b);i<=i##end;i++)
#define RFOR(i,a,b) for(int i=(a),i##end=(b);i>i##end;i--)
#define RREP(i,a,b) for(int i=(a),i##end=(b);i>=i##end;i--)
typedef long long LL;
const int maxn=100+5;
int n,A[maxn];
char s[maxn];
int main() {
scanf("%d%s",&n,s);
REP(i,0,n) scanf("%d",&A[i]);
int k=std::abs(A[1]-A[0]);
REP(i,1,n) k=std::min(k,std::abs(A[i]-A[i-1]));
printf("%d\n",k);
FOR(i,0,k) {
REP(j,0,n) printf("%d ",(A[j]+i)/k);
putchar('\n');
}
return 0;
}
|
#include <iostream>
#include <algorithm>
using namespace std;
int n, w[19], l[100009], v[100009], lp[309], pr[19];
long long ans = 1e18, d[19];
bool vs[19];
void ps()
{
d[0] = 0;
for (int i = 1; i < n; i++) {
int s = (1 << pr[i]);
d[i] = d[i - 1];
for (int j = i - 1; j >= 0; j--) {
s += (1 << pr[j]);
d[i] = max(d[i], d[j] + 1LL * lp[s]);
}
}
ans = min(ans, d[n - 1]);
}
void fg(int pi)
{
if (pi == n) {
ps();
return;
}
for (int i = 0; i < n; i++) {
if (vs[i]) continue;
vs[i] = true; pr[pi] = i;
fg(pi + 1);
vs[i] = false;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int m; cin >> n >> m;
int mx = 0;
for (int i = 0; i < n; i++) {
cin >> w[i];
mx = max(mx, w[i]);
}
bool f = true;
for (int i = 0; i < m; i++) {
cin >> l[i] >> v[i];
if (v[i] < mx) f = false;
}
if (!f) {
cout << -1 << '\n';
return 0;
}
for (int i = 0; i < (1 << n); i++) {
long long s = 0;
for (int j = 0; j < n; j++)
if ((1 << j) & i)
s += w[j];
for (int j = 0; j < m; j++)
if (v[j] < s)
lp[i] = max(lp[i], l[j]);
}
fg(0);
cout << ans << '\n';
return 0;
} | #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 MOD 998244353
#define EPS 0.000000001
using namespace std;
struct Info{
Info(){
len = value = 0;
}
Info(ll arg_len,ll arg_value){
len = arg_len;
value = arg_value;
}
bool operator<(const struct Info& arg) const{
if(len != arg.len){
return len < arg.len;
}else{
return value > arg.value;
}
}
ll len,value;
};
ll N,M;
ll W[10],work[10];
ll max_dist[10][10];
Info info[100005];
vector<Info> V;
int main(){
scanf("%lld %lld",&N,&M);
ll max_W = 0;
for(ll i = 0; i < N; i++){
scanf("%lld",&W[i]);
max_W = max(max_W,W[i]);
}
for(ll i = 0; i < M; i++){
scanf("%lld %lld",&info[i].len,&info[i].value);
if(info[i].value < max_W){
printf("-1\n");
return 0;
}
}
sort(info,info+M);
ll mini = HUGE_NUM;
for(int i = M-1; i >= 0; i--){
if(info[i].value < mini){
mini = info[i].value;
//printf("len:%lld value:%lld\n",info[i].len,info[i].value);
V.push_back(info[i]);
}
}
ll ans = HUGE_NUM;
vector<int> vec;
for(int i = 0; i < N; i++){
vec.push_back(i);
}
do{
for(ll i = 0; i < N; i++){
work[i] = 0;
}
ll table[10];
for(ll i = 0; i < N; i++){
table[i] = W[vec[i]];
if(i == 0)continue;
table[i] += table[i-1];
}
for(ll i = 1; i < N; i++){
for(ll base = 0; base <= i-1; base++){
//総重量
ll sum_W = 0;
if(base == 0){
sum_W = table[i];
}else{
sum_W = table[i]-table[base-1];
}
int left = 0,right = (int)V.size()-1,mid = (left+right)/2;
int loc = -1;
//耐えられない最小のindexを求める
while(left <= right){
if(V[mid].value < sum_W){
loc = mid;
right = mid-1;
}else{
left = mid+1;
}
mid = (left+right)/2;
}
ll add_len = 0;
if(loc == -1){ //全ての橋がOK
add_len = 0;
}else{
add_len = V[loc].len;
}
//printf("i:%lld base:%lld add_len:%lld sum_W:%lld loc:%d\n",i,base,add_len,sum_W,loc);
work[i] = max(work[i],work[base]+add_len);
}
}
ans = min(ans,work[N-1]);
}while(next_permutation(vec.begin(),vec.end()));
printf("%lld\n",ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#if DEBUG && !ONLINE_JUDGE
ifstream input_from_file("input.txt");
#define cin input_from_file
#else
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0); // Togliere nei problemi con query online
int N;
cin >> N;
string S, T;
cin >> S >> T;
LL res = 0;
int id = 0;
for (int i = 0; i < N; i++) {
if (S[i] == T[i]) continue;
id = max(i+1, id);
while (id < N and S[id] == '0') id++;
if (id == N) {
cout << -1 << "\n";
return 0;
}
res += id - i;
S[i] = T[i], S[id] = '0';
}
cout << res << "\n";
} | #include<cstdio>
#include<cstring>
#define rg register
const int maxn=1e6+5;
char s[maxn],t[maxn];
int n,cnt1,cnt2,sta1[maxn],sta2[maxn],now;
long long ans;
int main(){
scanf("%d",&n);
scanf("%s%s",s+1,t+1);
for(rg int i=1;i<=n;i++){
if(s[i]=='1'){
sta1[++cnt1]=i;
}
}
for(rg int i=1;i<=n;i++){
if(t[i]=='1'){
sta2[++cnt2]=i;
}
}
if(cnt1<cnt2 || (cnt1-cnt2)%2){
printf("-1\n");
return 0;
}
now=1;
for(rg int i=1;i<=cnt2;i++){
if(now>cnt1){
printf("-1\n");
return 0;
}
while(sta1[now]<sta2[i]){
if(now+2>cnt1){
printf("-1\n");
return 0;
}
ans+=sta1[now+1]-sta1[now];
now+=2;
}
ans+=sta1[now]-sta2[i];
now++;
}
while(now<cnt1){
ans+=sta1[now+1]-sta1[now];
now+=2;
}
printf("%lld\n",ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define pii pair<int,int>
int N;
int U[210], L[210];
int U_id[210], L_id[210];
bool dp[400];
bool unit(int s, int d)
{
for(int i = 0; i < d; i++)
{
if(U_id[s + i] != 0)
{
return false;
}
if(L_id[s + i + d] != 0)
{
return false;
}
/*
if(L_id[s + 1] != 0 && U[L_id[s + i]] != -1 && U[L_id[s + i]] != s + i + d)
{
return false;
}
if(U_id[s + i + d] != 0 && L[U_id[s + i + d]] != -1 && L[U_id[s + i + d]] != s + i)
{
return false;
}
*/
if(L_id[s + i] != 0 && U_id[s + i + d] != 0 && L_id[s + i] != U_id[s + i + d])
{
return false;
}
}
return true;
}
signed main()
{
cin >> N;
rep(i, N)
{
int a, b;
cin >> a >> b;
a, b;
L[i + 1] = a;
U[i + 1] = b;
if(a != -1 && b != -1 && a >= b)
{
cout << "No" << endl;
return 0;
}
if(a != -1)
{
if(L_id[a] != 0 || U_id[a] != 0)
{
cout << "No" << endl;
return 0;
}
L_id[a] = i + 1;
}
if(b != -1)
{
if(U_id[b] != 0 || L_id[b] != 0)
{
cout << "No" << endl;
return 0;
}
U_id[b] = i + 1;
}
}
dp[1] = true;
for(int i = 1; i <= 2 * N; i++)
{
if(dp[i])for(int j = 1; i + j * 2 <= 2 * N + 1; j++)
if(unit(i, j))dp[i + j * 2] = true;
}
if(dp[2 * N + 1])cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
| #include <cstdio>
#include <utility>
long double abs(long double a)
{
if (a < 0) return -a;
return a;
}
const int N = 100000;
int n, m, k;
std::pair<long double, long double> e[N];
bool u[N];
int main()
{
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; ++i) {
int v;
scanf("%d", &v);
u[v] = true;
}
std::pair<long double, long double> s;
for (int i = n - 1; i >= 0; --i) {
if (u[i]) e[i].second = 1;
else {
e[i].first = 1 + s.first / m;
e[i].second = s.second / m;
}
s.first += e[i].first;
s.second += e[i].second;
if (n - i > m) {
s.first -= e[i + m].first;
s.second -= e[i + m].second;
}
}
if (abs(e[0].second - 1) > 1e-10) printf("%.15lf\n", (double)(e[0].first / (1 - e[0].second)));
else printf("-1\n");
return 0;
}
|
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,l,r,mid;
int ans,a[105],b[105],c[105];
string st,t;
int main()
{
cin>>n;
for (int i=2;i<=n;i++)
{
m=i;
for (int j=2;j<=30;j++) b[j]=0;
int j=2;
while (m!=1)
{
if (m%j==0) while (m!=1&&m%j==0) {b[j]++;m=m/j;}
j++;
}
for (int j=2;j<=n;j++)
a[j]=max(a[j],b[j]);
}
long long ans;
ans=1;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=a[i];j++)
ans*=i;
}
cout<<ans+1;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
class IN{
public:
template<typename T>
IN& operator >>(T &x){read(x);return *this;}
IN& operator >>(double &x){scanf("%lf",&x);return *this;}
IN& operator >>(char *s){scanf("%s",s);return *this;}
IN& operator >>(char &c){c=getchar();return *this;}
private:
template<typename T>
void read(T &x){
T f=1;int c=getchar();x=0;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
x*=f;
}
}in;
class NL{}nl;
class BK{}bk;
class OUT{
public:
template<typename T>
OUT& operator <<(T x){write(x);return *this;}
OUT& operator <<(const char *s){printf("%s",s);return *this;}
OUT& operator <<(char c){putchar(c);return *this;}
OUT& operator <<(BK x){putchar(' ');return *this;}
OUT& operator <<(NL x){putchar('\n');return *this;}
private:
template<typename T>
void write(T x){
if(x<0)putchar('-'),x=-x;
if(x>=10)write(x/10);
putchar(x%10+48);
}
}out;
#define pb(x) push_back(x)
#define fi first
#define se second
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define mem(a,x) memset(a,x,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;
// Smax
//int Smax() { return -INF; }
template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) { return max(a, Smax(args...)); }
// Smin
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) { return min(a, Smin(args...)); }
/*
//邻接表不带边权
const int N=1e5+10;
const int M=1e5+10;
int head[N],ver[2*M],nex[2*M],tot=1;
inline void add(int x,int y) {
ver[++tot]=y,nex[tot]=head[x],head[x]=tot;
}
//邻接表带边权
const int N=1e5+10;
const int M=1e5+10;
int head[N],ver[2*M],edge[2*M],nex[2*M],tot=1;
inline void add(int x,int y,int z) {
ver[++tot]=y,edge[tot]=z,nex[tot]=head[x],head[x]=tot;
}
*/
/*****************************************************************************************************/
ll Pow(ll a,ll b,ll mod){ll res=1;a%=mod;while(b){if(b&1)(res*=a)%=mod;(a*=a)%=mod;b>>=1;}return res%mod;}
ll inv(ll a,ll p){if(a==1)return 1;return (p-p/a)*inv(p%a,p)%p;}//p为素数,a<p
ll C(ll n,ll m,ll p){ll res=1;for(ll i=1,j=n-m+1;i<=m;++i,++j){res=res*inv(i,p)%p*j%p;}return res;}
/*****************************************************************************************************/
/*ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&-x;return res;}
void add(ll v,int x){while(x<=n)c[x]+=v,x+=x&-x;}*/
const int p=1e9+7;
const int N=2e5+10;
int a[50][50];
void f(int id,int x){
for(int i=2;x>1;i++){
while(x%i==0){
a[id][i]++;
x/=i;
}
}
}
int main(){
int n;
ll ans=1;
in>>n;
rep(i,2,n){
f(i,i);
}
rep(i,2,30){
int Max=0;
rep(j,2,n)
Max=max(Max,a[j][i]);
rep(x,1,Max)ans*=i;
}
ans+=1;
out<<ans;
return 0;
}
|
#include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();
template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
class Prints {
private:
class __Prints {
public:
__Prints(const char* sep, const char* term) : sep(sep), term(term) {}
template <class... Args>
void operator()(const Args&... args) const { print(args...); }
template <typename T>
void pvec(const T& vec, size_t sz) const {
for (size_t i = 0; i < sz; i++)
std::cout << vec[i] << (i == sz - 1 ? term : sep);
}
template <typename T>
void pmat(const T& mat, size_t h, size_t w) {
for (size_t i = 0; i < h; i++)
for (size_t j = 0; j < w; j++)
std::cout << mat[i][j] << (j == w - 1 ? term : sep);
}
private:
const char *sep, *term;
void print() const { std::cout << term; }
void print_rest() const { std::cout << term; }
template <class T, class... Tail>
void print(const T& head, const Tail&... tail) const { std::cout << head, print_rest(tail...); }
template <class T, class... Tail>
void print_rest(const T& head, const Tail&... tail) const { std::cout << sep << head, print_rest(tail...); }
};
public:
Prints() {}
__Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};
Prints prints;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void solve() {
ll N, L;
cin >> N >> L;
Vl A(N + 2), B(N + 2);
for (ll i = 1; i <= N; i++) {
cin >> A[i];
A[i] -= i;
}
for (ll i = 1; i <= N; i++) {
cin >> B[i];
B[i] -= i;
}
A[N + 1] = B[N + 1] = (L + 1) - (N + 1);
Vl pos = B;
pos.erase(unique(pos.begin(), pos.end()), pos.end());
ll ans = 0;
for (auto p : pos) {
int idxl_a = lower_bound(A.begin(), A.end(), p) - A.begin();
int idxr_a = upper_bound(A.begin(), A.end(), p) - A.begin();
int idxl_b = lower_bound(B.begin(), B.end(), p) - B.begin();
int idxr_b = upper_bound(B.begin(), B.end(), p) - B.begin();
if (idxr_a - idxl_a == 0) {
prints()(-1);
return;
}
ans += max(idxr_b - idxr_a, 0) + max(idxl_a - idxl_b, 0);
}
prints()(ans);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
std::cin.tie(nullptr), std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
std::ifstream ifs("./in_out/input.txt");
std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
std::ofstream ofs("./in_out/output.txt");
std::cout.rdbuf(ofs.rdbuf());
#endif
std::cout << std::setprecision(18) << std::fixed;
solve();
std::cout << std::flush;
return 0;
} | #include "bits/stdc++.h"
#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};
ll mod(ll a, ll mod){
ll res = a%mod;
if(res<0)res=res + mod;
return res;
}
ll modpow(ll a, ll n, ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
ll modinv(ll a, ll mod){
ll b=mod, u=1, v=0;
while(b){
ll t=a/b;
a-=t*b; swap(a, b);
u-=t*v; swap(u, v);
}
u%=mod;
if(u<0)u+=mod;
return u;
}
ll gcd(ll a, ll b){
ll r = a%b;
if(r==0) return b;
else return gcd(b, a%b);
}
bool is_prime(ll n){
ll i = 2;
if(n==1)return false;
if(n==2)return true;
bool res = true;
while(i*i <n){
if(n%i==0){
res = false;
}
i = i+1;
}
//if(i==1)res = false;
if(n%i==0)res=false;
return res;
}
/**************************************
** A main function starts from here **
***************************************/
int main(){
ll N, X;
cin >> N >> X;
llvec a(N);
rep(i, N){
cin >> a[i];
}
ll ans = 1e18;
for(ll C=1;C<=N;C++){
vector<vector<llvec>> dp(N+1, vector<llvec>(C+1, llvec(C, -1)));
dp[0][0][0] = 0;
rep(i, N){
rep(j, C+1){
rep(k, C){
if(dp[i][j][k]==-1)continue;
if(j+1<=C)dp[i+1][j+1][mod(k+a[i], C)] = max(dp[i+1][j+1][mod(k+a[i], C)], dp[i][j][k] + a[i]);
dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k]);
}
}
}
if(dp[N][C][mod(X, C)]==-1)continue;
ans = min(ans, (X - dp[N][C][mod(X, C)])/C);
}
cout << ans << endl;
return 0;
}
|