code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)
void solve() {
int R, X, Y;
cin >> R >> X >> Y;
double D = hypot(X, Y);
if (D == R) {
cout << 1 << endl;
} else if (D <= 2 * R) {
cout << 2 << endl;
} else {
cout << ceil(D / R) << endl;
}
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
solve();
}
| //#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
const int mod = 1e9 + 7;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int T;
cin >> T;
rep(t, T) {
ll N, A, B;
cin >> N >> A >> B;
ll a = min(A, N - B + 1);
ll b = min(A + B - 1, N - B + 1);
ll are = (a + b) * (b - a + 1) + b * (N - A + 1 - (b - a + 1) * 2);
ll atti = (N - A + 1) * (N - B + 1);
ll kotae = (atti + are) % mod * ((atti - are) % mod) % mod;
co(kotae);
}
Would you please return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(int _t)
{
int n; cin >> n;
while (n && n % 10 == 0)
n /= 10;
string s = to_string(n);
if (equal(s.begin(), s.end(), s.rbegin()))
cout << "Yes";
else
cout << "No";
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int T = 1;
for (int t = 1; t <= T; t++) solve(t);
} | #include <bits/stdc++.h>
using namespace std;
//Macros
#define ll long long
#define nl "\n"
#define gl " "
#define pf(x) for(auto a:x) cout<<a.first<<gl<<a.second<<nl;
#define af(x) for(auto a:x) cout<<a<<gl;
//functions
int check_palindrome(string s);
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin>>s;
for(int i=0; i<20; i++){
if(check_palindrome(s)){
cout<<"Yes"<<nl;
return 0;
}
s="0"+s;
}
cout<<"No"<<nl;
}
int check_palindrome(string s){
for(int i=0,j=s.size()-1; i<s.size(); i++,j--){
if(s[i]!=s[j]) return 0;
}
return 1;
}
|
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(x) x.begin() , x.end()
const int N = 3e5 + 99 ;
int seg[4*N] , a[N] , n , q;
void build(int idx = 1 , int l = 0 , int r = n - 1){
int lf = (idx << 1) , mid = (l + r) >> 1 , ri = lf + 1 ;
if ( l == r ) seg[idx] = a[l];
else{
build( lf , l , mid );
build( ri , mid + 1 , r);
seg[idx] = seg[lf] ^ seg[ri];
}
}
void update( int pos , int val , int idx = 1 , int l = 0 , int r = n - 1 ){
int lf = (idx << 1) , mid = (l + r) >> 1 , ri = lf + 1 ;
if ( l == r ){
seg[idx] = val;
}
else{
if ( pos <= mid ){
update( pos , val , lf , l , mid );
}
else{
update( pos , val , ri , mid + 1 , r);
}
seg[idx] = seg[lf] ^ seg[ri];
}
}
int ask( int s , int e , int idx = 1 , int l = 0 , int r = n - 1 ){
int lf = (idx << 1) , mid = (l + r) >> 1 , ri = lf + 1 ;
if ( s > r || e < l ) return 0;
else if ( l >= s && r <= e ) return seg[idx];
else{
return ask(s,e,lf,l,mid) ^ ask(s,e,ri,mid+1,r);
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef CLion
freopen("input.txt" , "r" , stdin);
// freopen("output.txt", "w" , stdout);
#endif
cin >> n >> q;
for ( int i = 0; i < n; ++i )
cin >> a[i];
build();
while ( q-- ){
int t , x , y;
cin >> t >> x >> y;
if ( t == 1 ){
update(x - 1 , ask(x-1,x-1) ^ y);
}
else{
cout << ask(x - 1 , y - 1) << "\n";
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define R register
#define LL long long
inline int read() {
int x = 0, f = 1; char a = getchar();
for(; a > '9' || a < '0'; a = getchar()) if(a == '-') f = -1;
for(; a <= '9' && a >= '0'; a = getchar()) x = x * 10 + a - '0';
return x * f;
}
const int N = 20;
int n, m;
int ok[N][N];
int dp[1 << 18];
int stk[N], top;
inline int check(int S) {
top = 0;
for(R int i = 1; i <= n; i ++)
if(S & (1 << (i - 1))) stk[++ top] = i;
for(R int i = 1; i <= top; i ++)
for(R int j = i + 1; j <= top; j ++)
if(ok[stk[i]][stk[j]] == 0) return 0;
return 1;
}
#define min(x,y) (x) < (y) ? (x) : (y)
signed main() {
#ifdef IN
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
#endif
n = read(); m = read();
for(R int i = 1; i <= m; i ++) {
int x = read(), y = read();
ok[x][y] = ok[y][x] = 1;
}
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
int tot = 0;
for(R int S = 1; S < (1 << n); S ++) {
if(check(S)) { dp[S] = 1; continue; }
if((S & -S) == S) { dp[S] = 1; continue; }
for(R int T = S & (S - 1); T; T = (T - 1) & S)
dp[S] = min(dp[S], dp[T] + dp[S ^ T]);
}
cout << dp[(1 << n) - 1] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, k;
cin >> n >> k;
int t[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cin >> t[i][j];
}
vector<int> p(n);
for (int i = 0; i < n; i++)
p[i] = i;
int ans = 0;
do
{
int cur = 0;
for (int i = 0; i < n - 1; i++)
cur += t[p[i]][p[i + 1]];
cur += t[p[n - 1]][0];
if (cur == k)
ans++;
} while (next_permutation(p.begin() + 1, p.end()));
cout << ans;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<vector<pair<int, int>>> X(N + 1);
rep(i, M) {
int x, y, z;
cin >> x >> y >> z;
X[x].push_back({y, z});
}
int n = 1 << N;
vector<ll> dp(n);
dp[0] = 1;
vector<int> mask(N + 1);
rep(i, N) mask[i + 1] = mask[i] << 1 | 1;
rep(s, n) {
int cnt = __builtin_popcount(s);
for (auto p : X[cnt]) {
if (__builtin_popcount(s & mask[p.first]) > p.second) dp[s] = 0;
}
rep(j, N) {
if (~s >> j & 1) dp[s | 1 << j] += dp[s];
}
}
cout << dp[n - 1] << endl;
return 0;
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <cmath>
#include <stack>
#include <map>
#include <functional>
#include <queue>
#include <set>
using namespace std;
int64_t min(int64_t a,int64_t b) {
if (a > b)
{
return b ;
}else
{
return a ;
}
}
void boolflip(bool &a){
a = !a ;
return ;
}
int main(){
int64_t n,m ;
cin >> n >> m ;
set<int> can ;
can.insert(n) ;
vector<pair<int,int> > black(m,pair<int,int>()) ;
int x,y ;
for (int i = 0; i < m; i++)
{
cin >> x >> y ;
black.at(i) = make_pair(x,y) ;
}
sort(black.begin(),black.end()) ;
int j = 0 ;
int nowx ;
if (j < m)
{
nowx = black.at(j).first ;
}
while (j < m)
{
vector<int> next(0) ;
while(j < m){
if (black.at(j).first == nowx)
{
next.push_back(black.at(j).second) ;
}else
{
break ;
}
j ++ ;
}
set<int> ok ;
set<int> stop ;
for (int i = 0; i < next.size(); i++)
{
int t = next.at(i) ;
if (t == 0)
{
if (can.count(t+1))
{
ok.insert(t) ;
}
}else if (t == 2*n)
{
if (can.count(t-1))
{
ok.insert(t) ;
}
}else
{
if (can.count(t+1) || can.count(t-1))
{
ok.insert(t) ;
}
}
}
for (int i = 0; i < next.size(); i++)
{
int t = next.at(i) ;
if (can.count(t))
{
if (ok.count(t) == 0)
{
stop.insert(t) ;
}
}
}
for(set<int>::iterator itr = ok.begin(); itr != ok.end(); ++itr) {
can.insert(*itr) ;
}
for(set<int>::iterator itr = stop.begin(); itr != stop.end(); ++itr) {
can.erase(*itr) ;
}
if (j < m)
{
nowx = black.at(j).first ;
}
}
cout << can.size() << endl;
} | #include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int MOD = 998244353;
const int INF = 1e18;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
signed main(){
int A, B;
cin >> A >> B;
int C = 2*A+100;
if( B < C ) cout << C-B << endl;
else cout << 0 << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int N = 2005, mod = 1e9 + 7;
char c[N][N];
int h, w, s, p[N*N], d[N][N], r[N][N], l[N][N], u[N][N], ans;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
p[0] = 1;
for(int i = 1; i < N*N; i++) p[i] = p[i-1] * 2 % mod;
cin >> h >> w;
for(int i = 1; i <= h; i++) {
for(int j = 1; j <= w; j++) {
cin >> c[i][j];
if(c[i][j] == '#') continue;
u[i][j] = u[i-1][j] + 1, l[i][j] = l[i][j-1] + 1;
s++;
}
}
for(int i = h; i > 0; i--) {
for(int j = w; j > 0; j--) {
if(c[i][j] == '#') continue;
d[i][j] = d[i+1][j] + 1, r[i][j] = r[i][j+1] + 1;
int x = u[i][j] + r[i][j] + l[i][j] + d[i][j] - 3;
ans += (1LL * p[s-x] * ((p[x] - 1 + mod) % mod)) % mod;
ans %= mod;
}
}
cout << ans;
} | #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;
template<int M>
class ModInt {
public:
ll value;
constexpr ModInt(ll v = 0) { value = v % M; if (value < 0) value += M; }
constexpr ModInt pow(ll n) const { if (!n) return 1; ModInt a = pow(n >> 1); a *= a; if (n & 1) a *= *this; return a; }
constexpr ModInt inv() const { return this->pow(M - 2); }
constexpr ModInt operator + (const ModInt &rhs) const { return ModInt(*this) += rhs; };
constexpr ModInt operator - (const ModInt &rhs) const { return ModInt(*this) -= rhs; };
constexpr ModInt operator * (const ModInt &rhs) const { return ModInt(*this) *= rhs; };
constexpr ModInt operator / (const ModInt &rhs) const { return ModInt(*this) /= rhs; };
constexpr ModInt &operator += (const ModInt &rhs) { value += rhs.value; if (value >= M) value -= M; return *this; };
constexpr ModInt &operator -= (const ModInt &rhs) { value -= rhs.value; if (value < 0) value += M; return *this; };
constexpr ModInt &operator *= (const ModInt &rhs) { value = value * rhs.value % M; return *this; };
constexpr ModInt &operator /= (const ModInt &rhs) { return (*this) *= rhs.inv(); };
constexpr bool operator == (const ModInt &rhs) { return value == rhs.value; }
constexpr bool operator != (const ModInt &rhs) { return value != rhs.value; }
friend ostream &operator << (ostream &os, const ModInt &rhs) { os << rhs.value; return os; }
};
using mint = ModInt<MOD>;
int main() {
int H, W; cin >> H >> W;
vector<string> S(H);
REP(i, 0, H) cin >> S[i];
int tot = 0;
vector<vector<int>> R(H, vector<int>(W, 0)), C(H, vector<int>(W, 0));
REP(i, 0, H) REP(j, 0, W) {
if (S[i][j] == '#') continue;
R[i][j] = 1 + ((j > 0) ? R[i][j - 1] : 0);
C[i][j] = 1 + ((i > 0) ? C[i - 1][j] : 0);
tot++;
}
REP(i, 0, H) for (int j = W - 1; j > 0; j--) if (R[i][j] && R[i][j - 1]) R[i][j - 1] = R[i][j];
REP(j, 0, W) for (int i = H - 1; i > 0; i--) if (C[i][j] && C[i - 1][j]) C[i - 1][j] = C[i][j];
// mint ans = 0;
mint ans = (mint)tot * mint(2).pow(tot);
REP(i, 0, H) REP(j, 0, W) {
if (S[i][j] == '#') continue;
// cout << "#(" << i << "," << j << ") : " << R[i][j] << ", " << C[i][j] << endl;
int cnt = R[i][j] + C[i][j] - (R[i][j] && C[i][j]);
ans -= mint(2).pow(tot - cnt);
// ans += (mint(2).pow(cnt) - 1) * mint(2).pow(tot - cnt);
}
cout << ans << endl;
return 0;
} |
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
// clang-format off
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
#define FOREACH(i,c) for(__typeof((c).begin())i = (c).begin();i!=(c).end(); ++i)
//v5
int cond = 1;string to_string(char c){return'\''+string(1,c)+'\'';}string to_string(const string&s){return'"'+s+'"';}string to_string(const char*s){return to_string((string)s);}string to_string(bool b){return(b?"true":"false");}string to_string(std::vector<bool>::reference&r){return to_string((bool)r);}template<typename A,typename B>string to_string(const pair<A,B>&p){return"("+to_string(p.first)+", "+to_string(p.second)+")";}template<typename A>string to_string(A v){int f=1;string r="{";for(auto x:v){if(!f){r+=", ";}f=0;r+=to_string(x);}r+="}";return r;}template<typename A,typename B>string to_string(map<A,B>v){int f=1;string r="{";for(auto x:v){if(!f){r+=", ";}f=0;r+=to_string(x.first)+" -> "+to_string(x.second);}r+="}";return r;}void __db(int i,const vector<string>&s){cerr<<endl;}template<typename T,typename... A>void __db(int i,const vector<string>&s,T t,A... a){if(i)cerr<<", ";cerr<<s[i]<<" = "<<to_string(t);__db(i+1,s,a...);}string __cl(string s){while(!s.empty()&&s[0]==' ')s.erase(0,1);while(!s.empty()&&s.back()==' ')s.pop_back();return s;}vector<string>__vars(string s){vector<string>r;s+=",";string n="";int d=0;for(auto x:s)if(x==','&&d==0){if(!n.empty())r.push_back(__cl(n));n="";}else{n+=x;if(x=='('||x=='[')d++;else if(x==')'||x==']')d--;}return r;}
#define DB(...){if(cond){cerr<<"Line:"<<__LINE__<<" ";__db(0,__vars(#__VA_ARGS__),__VA_ARGS__);}}
// clang-format on
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
printf("%d\n", a - b + c);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, a, b;
cin >> n >> a >> b;
cout << n - a + b << "\n";
return 0;
} |
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#include <random>
#include <chrono>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
const int MAX=1e4;
int N;
vector<int> X,Y,R;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin>>N;
X.resize(N);
Y.resize(N);
R.resize(N);
rep(i,N) cin>>X[i]>>Y[i]>>R[i];
vector<int> ord(N);
rep(i,N) ord[i]=i;
sort(all(ord),[&](int a,int b){
if(X[a]!=X[b]) return X[a]<X[b];
if(Y[a]!=Y[b]) return Y[a]<Y[b];
return a<b;
});
auto getScore = [](int r,const vector<int> &len)->double{
int s=(len[0]+len[2]+1)*(len[1]+len[3]+1);
double p = 1.0-(1.0-min(r,s)/(double)max(r,s))*(1.0-min(r,s)/(double)max(r,s));
return p;
};
vector<vector<int>> length(N,vector<int> (4,0));
vector<int> completed(N,0);
int lastChanged=-1;
for(int t=0;;t++){
if(lastChanged+4<t) break;
bool no_changed=true;
auto duplicated = [&](int a,int b) ->bool{
if(a==b) return false;
if(Y[b]+length[b][0]<Y[a]-length[a][2]) return false;
if(X[b]+length[b][1]<X[a]-length[a][3]) return false;
if(Y[b]-length[b][2]>Y[a]+length[a][0]) return false;
if(X[b]-length[b][3]>X[a]+length[a][1]) return false;
return true;
};
for(auto now:ord){
//if(completed[now]) continue;
length[now][t%4]++;
bool outOfRange=false;
if(t%4==0&&Y[now]+length[now][0]>=MAX) outOfRange=true;
if(t%4==1&&X[now]+length[now][1]>=MAX) outOfRange=true;
if(t%4==2&&Y[now]-length[now][2]<0) outOfRange=true;
if(t%4==3&&X[now]-length[now][3]<0) outOfRange=true;
if(outOfRange){
length[now][t%4]--;
continue;
}
/*bool improved=false;
{
double nex_p=getScore(R[now],length[now]);
length[now][t%4]--;
double pre_p=getScore(R[now],length[now]);
length[now][t%4]++;
if(pre_p<=nex_p) improved=true;
}
if(improved==false){
length[now][t%4]--;
continue;
}*/
int s=(length[now][0]+length[now][2]+1)*(length[now][1]+length[now][3]+1);
if(s>R[now]){
//completed[now]=1;
length[now][t%4]--;
continue;
}
bool ok=true;
for(auto tar:ord){
if(now==tar) continue;
if(duplicated(now,tar)){
ok=false;
break;
}
}
if(ok==false) length[now][t%4]--;
else no_changed=false;
}
if(no_changed==false) lastChanged=t;
}
vector<int> A(N),B(N),C(N),D(N);
rep(i,N){
A[i]=X[i]-length[i][3];
B[i]=Y[i]-length[i][2];
C[i]=X[i]+length[i][1]+1;
D[i]=Y[i]+length[i][0]+1;
}
rep(i,N) cout<<A[i]<<" "<<B[i]<<" "<<C[i]<<" "<<D[i]<<"\n";
auto calcS = [&](const vector<int> &len)->int{
return (len[0]+len[2]+1)*(len[1]+len[3]+1);
};
//rep(i,N) cout<<i<<": "<<calcS(length[i])<<" "<<R[i]<<" "<<getScore(R[i],length[i])<<"\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1010;
int a[N], b[N], n, m, t;
set<int> odd, even;
int res[N * N], cnt;
void swa(int x) {
res[cnt ++] = x;
int l = a[x], r = a[x + 1];
swap(a[x], a[x + 1]);
swap(b[l], b[r]);
t ^= 1;
}
void sol(int x, int y) {
for (int i = x; i < y; i ++) swa(i);
}
int get(int x, int y) {
for (int i = y; i < 5; i ++) {
if (a[i] != x && a[i + 1] != x) return i;
}
}
int main() {
int T;
cin >> T;
while (T --) {
cnt = 0;
t = 1;
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
b[a[i]] = i;
}
for (int i = n; i >= 5; i --) {
int x = b[i];
if (i == x) continue;
if ((x & 1) ^ t) {
int tt = get(i, t == 0 ? 2 : 1);
swa(tt);
}
sol(x, i);
}
if (n >= 4 && a[4] != 4) {
if ((b[4] & 1) ^ t) {
if (b[4] == 1) swa(2);
else if (b[4] == 2) swa(3);
else if(b[4] == 3) swa(2), swa(3);
}
sol(b[4], 4);
}
if (n >= 3 && a[3] != 3) {
if ((b[3] & 1) ^ t) {
if (b[3] == 1) swa(2);
else if (b[3] == 2) swa(1), swa(2);
}
sol(b[3], 3);
}
if (n >= 2 && b[2] == 1) {
if (t == 1) swa(1);
else {
swa(2), swa(1), swa(2), swa(1), swa(2);
}
}
cout << cnt << endl;
for (int i = 0; i < cnt; i ++) printf("%d ", res[i]);
puts("");
}
return 0;
}
|
// * Author : Debapriya010 : DEBAPRIYA CHANDRA
// * Jalpaiguri Government Engineering College
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long int
#define dbl long double
#define loop(i,n) for(int i=0;i<n;i++)
#define test(t) int t;cin>>t;while(t--)
#define debo ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define vi vector<ll>
#define sz size()
#define iss is_sorted
#define bs binary_search
#define mod 1000000007
#define c1_Bin __builtin_popcountll
#define yos cout<<"YES"<<endl
#define nos cout<<"NO"<<endl
#define pb push_back
#define mp map<ll,ll>
#define fmax(v) *max_element(v.begin(),v.end())
#define fmin(v) *min_element(v.begin(),v.end())
#define tos to_string
using namespace std;
bool isPrime(ll n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool IsPalindrome( string str)
{
if (str.empty())
return false;
ll i = 0; // first characters
ll j = str.length() - 1; // last character
while (i < j)
{
if (str[i] != str[j])
{
return false;
}
i++;
j--;
}
return true;
}
// Binary Exponentiation fun to calculate pow in 0(logN)
ll binpow(ll a, ll b) {
if (b == 0)
return 1;
ll res = binpow(a, b / 2);
if (b % 2)
return (res * res * a)%mod;
else
return (res * res)%mod;
}
signed main(){
debo;
// test(t){
ll k;
cin>>k;
ll a=0,g=-1;
for(ll i=0;i<=k;i++)
{
a+=i;
if(a>=k)
{
g=i;
break;
}
}
cout<<g;
// }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, n) for (ll i = a; i < n; ++i)
#define rep_down(i, a, n) for (ll i = a; i >= n; --i)
#define P pair<ll, ll>
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>
constexpr ll INF = (1ll << 60);
constexpr ll mod = 1000000007;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll mypow(ll a, ll n) {
ll ret = 1;
rep(i, n) {
if (ret > (ll)(1e18 + 10) / a) return -1;
ret *= a;
}
return ret;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main(){
int n;
cin >> n;
set<int> s;
s.insert(6);
s.insert(10);
s.insert(15);
for(int x = 6; x <= 10000; x += 6){
if(s.size()==n)break;
s.insert(x);
}
for(int x = 10; x <= 10000; x += 10){
if(s.size()==n)break;
s.insert(x);
}
for(int x = 15; x <= 10000; x += 15){
if(s.size()==n)break;
s.insert(x);
}
for(auto e : s)cout << e << " ";
cout << endl;
return 0;
} |
/*
Author: Zcus
Blog: https://cnblogs.com/zcus
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define debug(x) cout << #x << " = " << x << endl
#define For(i, x, y) for (int i = x; i <= y; i++)
#define Rep(i, x, y) for (int i = x; i >= y; i--)
#define file(FILE_NAME) freopen(FILE_NAME".in", "r", stdin), freopen(FILE_NAME".out", "w", stdout)
#define filein(FILE_NAME) freopen(FILE_NAME".in", "r", stdin);
#define fileout(FILE_NAME) freopen(FILE_NAME".out", "w", stdout);
template<class T> inline bool Chkmax(T& x, const T& y) { return x < y ? x = y, true : false; }
template<class T> inline bool Chkmin(T& x, const T& y) { return x > y ? x = y, true : false; }
#define I int
inline I ri() {
I s = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {if (c == '-') w = -1; c = getchar();}
while (c >= '0' && c <= '9') { s = (s << 1) + (s << 3) + (c ^ 48); c = getchar();}
return s * w;
}
#undef I
#define int long long
const int maxn = 2e5 + 50;
int k;
int tong[maxn];
int calc(int k) {
int res = 0;
for (int l = 1, r; l <= k; l = r + 1) {
r = k / (k / l);
res = res + (k / l) * (r - l + 1);
}
return res;
}
signed main() {
cin >> k;
int ans = 0;
for (int i = 1; i <= k; i++) {
ans += calc(k / i);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FastIO() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define read() freopen("in.txt","r",stdin)
#define write() freopen("out.txt","w",stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef double dbl;
typedef float flt;
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
#define pie acos(-1)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define MAX 300005
#define MOD 1000000007
#define INF 1000000100
#define for0(i, n) for(ll i=0; i<n; i++)
#define for1(i, n) for(ll i=1; i<=n; i++)
#define forab(i, a, b) for(ll i=a; i<=b; i++)
#define forabx(i, a, b, x) for(ll i=a; i<=b; i+=x)
#define forinv(i, a, b) for(ll i=a; i>=b; i--)
#define clr0(a) memset(a, 0, sizeof(a))
#define ceil(x, y) (x+y-1) / (y)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a * b) / __gcd(a, b))
#define sort1(a, x, y) sort(a+x, a+y+1)
#define debug(x) cout << "DEBUG " << x << endl;
#define Y() cout << "YES" << endl;
#define N() cout << "NO" << endl;
#define inArr(a, x, y) forab(i, x, y) { cin >> a[i]; }
#define outArr(a, x, y) forab(i, x, y) { cout << a[i] << " "; } cout << endl;
void solve() {
ll n;
cin >> n;
char a[n+10], b[n+10];
cin >> a;
cin >> b;
vl a0, b0;
ll ca=0, cb=0;
for0(i, n) {
if (a[i]=='0') {
ca ++;
a0.pb(i);
}
if (b[i]=='0') {
cb++;
b0.pb(i);
}
}
ll ans = 0;
if (ca!=cb) {
ans = -1;
}
else {
for0(i, ca) {
if (a0[i]!=b0[i]) {
ans ++;
}
}
}
cout << ans << endl;
}
void testcase() {
ll t;
cin >> t;
while (t--) {
solve();
}
}
int main () {
FastIO()
//testcase();
solve();
return 0;
}
|
//============================================================================
// Name : B.cpp
// Author : NewtonGn
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;
#define R >>
#define W <<
#define F first
#define S second
#define ALL(v) v.begin(), v.end()
#define Pb push_back
#define EMPB emplace_back
#define MKP make_pair
#define FOR(i,a,n) for(int i = (int)a; i< (int)n; i++)
#define rFOR(i,n,a) for(int i = (int)n; i>=(int)n; i--)
#define ACCELERATE ios::sync_with_stdio(0); cin.tie(0);
#define inf (int)1e9
#define infLL (ll)1e18
#define nl '\n'
#define ll long long
/*/-----------------------Modular Arithmetic---------------/*/
const long long mod = 998244353;
ll ch(ll x, ll n){
x--;
return max( 0ll, x -max(0ll, 2*(x-n)));
}
bool srt(pair<pair<ll,ll>,ll> a, pair<pair<ll,ll>,ll> b){
if(a.F.F == b.F.F) return a.F.S < b.F.S;
return a.F.F < b.F.F ;
}
void solve(){
double n, a, b,k, w, d, c,h, m, ans;
cin R n R w;
ll x ,y;
d = sqrt( n*n/4 - w);
x = ( ll) (n/2 + d);
y = (ll) (n/2 -d);
if(x+y == (ll) n && x*y == (ll) w) cout W "Yes";
else cout W "No";
}
int main(){
ACCELERATE;
int t=1;
//cin R t;
while(t--){
// auto start = clock();
solve();
// start = clock() - start;
// cout W "it took " W fixed W setprecision(20) W double(start) W '\n';
}
return 0;
}
| #include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
#include<utility>
#include<tuple>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<bitset>
#include<math.h>
#include<numeric>
using namespace std;
using ll = int64_t;
using Graph = vector<vector<int> >;
const ll M = 1000000007;
int main(){
int n;
cin >> n;
vector<ll> a(n);
for(int i=0;i<n;i++) cin >> a[i];
ll sum=0;
vector<ll> s(n);
for(int i=0;i<n;i++){
sum+=a[i];
s[i]=sum;
}
vector<ll> ss(n);
ll ssum=0;
for(int i=0;i<n;i++){
ssum+=s[i];
ss[i]=ssum;
}
ll tmp=a[0];
ll ans=0;
cout << 2*a[0] << endl;
for(int i=1;i<n;i++){
tmp=max(tmp,a[i]);
ll x=tmp*(i+1);
//cout << s[i] << ' ' << ss[i-1] << ' ' << x << endl;
cout << s[i]+ss[i-1]+x << endl;
}
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 998244353;
const int N = 3000 + 10;
LL dp[N][N];
LL f(LL n, LL k) {
if(n < k) return 0;
if(n == k) return 1;
if(!n) return 0;
if(!k) return 0;
if(dp[n][k] != -1) return dp[n][k];
return dp[n][k] = (f(n - 1, k - 1) + f(n, 2 * k)) % mod;
}
void solve() {
memset(dp, -1, sizeof dp);
LL n, k;
scanf("%lld%lld", &n, &k);
printf("%lld\n", f(n, k));
}
int main() {
// freopen("in.txt", "r", stdin);
solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pair<long long int, long long int>, null_type,less<pair<long long int, long long int> >, rb_tree_tag,tree_order_statistics_node_update>
#define iOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define ppf pop_front
//#define mp make_pair
#define L(x) x.length()
#define B(x) x.begin()
#define E(x) x.end()
#define F(x) x.front()
#define SZ(x) x.size()
#define CLR(x) x.clear()
#define SORT(x) sort(x.begin(),x.end())
#define REV(x) reverse(x.begin(),x.end())
#define FOR(i,x,y) for(int i=x;i<y;i++)
#define S(x) scanf("%d",&x)
#define SL(x) cin>>x
#define SD(x) scanf("%lf",&x)
#define SC(x) scanf("%1s",&x)
#define SS(x) scanf("%s",x)
#define DUM() scanf("%c",&dum)
#define READ(x) freopen(x,"r",stdin)
#define WRITE(x) freopen(x,"w",stdout)
#define FILL(x,y) memset(x,y,sizeof(x))
#define IT iterator
#define ret(x) return cout<<x,0;
#define rety return cout<<"YES",0;
#define retn return cout<<"NO",0;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repn(i,a,b) for(int i=a;i>=b;i--)
//#define ld long double
using namespace std;
typedef long long int lli;
typedef long double ld;
typedef unsigned long long int llu;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef vector< pii > vpii;
typedef vector< vpii > vvpii;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef map<int,int> mapii;
long long int mod=998244353;
#define all(c) c.begin(),c.end()
lli dp[3001][3001];
lli solve(int n, int k)
{
if(k>n)
return 0;
if(n==0 || k==0)
return 0;
if(dp[n][k]!=-1)
return dp[n][k];
dp[n][k]=(solve(n-1,k-1)+solve(n,2*k))%mod;
return dp[n][k];
}
int main()
{
/*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
iOS;
lli test=1;
//factcalc();
//cin>>test;
while(test--)
{
int n,k;
cin>>n>>k;
for(int i=0;i<=n;++i)
{
for(int j=0;j<=n;++j)
dp[i][j]=-1;
}
dp[1][1]=1;
lli t=(solve(n,k))%mod;
/*for(int i=0;i<=n;++i)
{
for(int j=0;j<=n;++j)
cout<<dp[i][j]<<" ";
cout<<endl;
}*/
cout<<t;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int index = 0;
long long n,count = 0;
cin>>n;
long long tem = n;
while(n>0){
index++;
n/=1000;
}
long long base = 1;
for (int i=1;i<index;i++) base *=1000;
base--;
count = (tem-base)*(index-1);
switch (index){
case 1:case 2: break;
case 3: count += 999000; break;
case 4:count += 999000+999000000*2; break;
case 5:count += 999000+999000000*2+999000000000*3; break;
case 6: count += 999000+999000000*2+999000000000*3+999000000000000*4; break;
}
cout<<count;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n; cin >> n;
int n_digits = to_string(n).size();
long long ans = 0;
for (int d = 1; d < n_digits; d++) {
ans += (d - 1) / 3 * 9 * pow(10, d - 1);
}
ans += (n_digits - 1) / 3 * (n - pow(10, n_digits - 1) + 1);
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int h,w,a,b;
long long dp[(1 << 17) + 10][20];
long long f(int mask, int da) {
if(da == a)
return 1;
long long &ans = dp[mask][da];
if(ans != -1) return ans;
ans = 0;
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
int pos = w * i + j;
int next = w * i + j + 1;
if(!(mask & (1 << pos)) and j + 1 < w and !(mask & (1 << next))) {
ans += f(mask | (1 << pos) | (1 << next), da + 1);
}
next = w * (i + 1) + j;
if(!(mask & (1 << pos)) and i + 1 < h and !(mask & (1 << next))) {
ans += f(mask | (1 << pos) | (1 << next), da + 1);
}
}
}
return ans;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> h >> w >> a >> b;
long long ft = 1;
for (int i = 1; i <= a; ++i)
ft *= i;
cout << f(0, 0) / ft << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)
const int MOD = 1e9+7;
int mod_pow(int a,int b,int M = MOD)
{
if(a == 0) return 0;
b %= (M - 1); //M must be prime here
int res = 1;
while(b > 0)
{
if(b&1) res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
const int N = 1e5 + 10;
const int K = 17;
int dp[K][(1<<K)];
int dis[K][(1<<K)];
vi g[N];
int n,m;
void solve()
{
cin >> n >> m;
int u,v;
f(i,m)
{
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
int k;
cin >> k;
int a[k];
f(i,k) cin >> a[i];
f(i,k)
{
f(j,n+1) dis[i][j] = N;
queue<pii> q;
q.push({0,a[i]});
while(!q.empty())
{
auto x = q.front();
q.pop();
if(dis[i][x.S] != N) continue;
dis[i][x.S] = x.F;
for(auto v : g[x.S])
if(dis[i][v] > x.F + 1)
q.push({x.F + 1,v});
}
}
f(i,k) f(j,(1<<k)) dp[i][j] = N;
f(i,k) dp[i][0] = 0;
f(i,(1<<k))
{
f(j,k) if(i & (1<<j))
{
f(v,k) if(i & (1<<v))
{
dp[j][i] = min(dp[j][i] , dp[v][i^(1<<j)] + dis[j][a[v]]);
}
}
}
int res = N;
f(i,k) res = min(res,dp[i][(1<<k)-1]);
if( res == N) res = -2;
cout << res + 1;
}
signed main()
{
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
}
|
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pll = pair <ll, ll>;
#define FIO() ios_base::sync_with_stdio(0);cin.tie(NULL);
#define endl '\n'
const ll MOD = 1e9 + 7;
const int mx = 2009;
string g[mx];
int h, w;
ll val[mx][mx][3];
ll dp[mx][mx];
ll solve(int r, int c) {
ll& ans = dp[r][c];
if (ans != -1) return ans;
if (r == h - 1 and c == w - 1) {
val[r][c][0] = val[r][c][1] = val[r][c][2] = 1;
return ans = 1;
}
ans = 0;
if (c + 1 < w and g[r][c + 1] == '.') {
solve(r, c + 1);
val[r][c][0] += val[r][c + 1][0];
ans += val[r][c][0];
}
if (r + 1 < h and g[r + 1][c] == '.') {
solve(r + 1, c);
val[r][c][1] += val[r + 1][c][1];
ans += val[r][c][1];
}
if (r + 1 < h and c + 1 < w and g[r + 1][c + 1] == '.') {
solve(r + 1, c + 1);
val[r][c][2] += val[r + 1][c + 1][2];
ans += val[r][c][2];
}
for (int i = 0; i < 3; i++) {
val[r][c][i] += ans;
val[r][c][i] %= MOD;
}
ans %= MOD;
return ans;
}
int main() {
FIO();
cin >> h >> w;
for (int i = 0; i < h; i++) cin >> g[i];
memset(dp, -1, sizeof dp);
cout << solve(0, 0) << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = int (1e9) + int (1e5);
const ll INFL = ll(2e18) + ll(1e10);
const ui MOD = 1E9 + 7;
const double EPS = 1e-9;
#define FOR(i,n) for (int i=0;i<(n);++i)
#define ROF(i,x) for(int i = (x) ; i >= 0 ; --i)
#define MP make_pair
#define all(a) (a).begin(),(a).end()
#define ODD(x) ( ((x)&1)==0?0:1 )
#define SIGN(x) ( ((x) > 0) - ((x) < 0) )
#define dbg(...) cerr << #__VA_ARGS__ << ": " << (__VA_ARGS__) << endl
std::mt19937_64 generator(std::chrono::system_clock::now().time_since_epoch().count());
inline ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll lcm(ll a, ll b) { return a / __gcd(a,b) * b; }
void READ(bool _local){
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _DEBUG
if (_local)
freopen ("in.txt", "r", stdin);
#endif
}
#define SI() (cout << "Yes\n")
#define NO() (cout << "No\n")
void solve(){
}
int main() {
READ(0);
int n,k;cin>>n>>k;
vvi a(n,vi(n));
vi b(n*n);
FOR(i,n){
FOR(j,n) {
cin>>a[i][j];
b[i*n+j]=a[i][j];
}
}
sort(all(b));
int lo=0,hi=n*n-1;
const int A=(k*k+1)/2;
while(lo<hi){
int mid=(lo+hi)/2;
vvi x(n,vi(n));
FOR(i,n)FOR(j,n) x[i][j]=a[i][j]<=b[mid];
for(int j=1;j<n;++j) x[0][j] += x[0][j-1];
for(int i=1;i<n;++i) x[i][0] += x[i-1][0];
for(int i=1;i<n;++i){
for(int j=1;j<n;++j){
x[i][j] += x[i-1][j] + x[i][j-1] - x[i-1][j-1];
}
}
int cnt=-1;
for(int i=k-1;i<n && cnt<A;++i){
for(int j=k-1;j<n;++j){
cnt = x[i][j];
if (i-k>=0) cnt -= x[i-k][j];
if (j-k>=0) cnt -= x[i][j-k];
if (i-k>=0 && j-k>=0) cnt += x[i-k][j-k];
if (cnt>=A) break;
}
}
if (cnt>=A) hi=mid;
else lo=mid+1;
}
cout << b[lo];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vpii=vector<pair<int,int>>;
using vpll=vector<pair<ll,ll>>;
using vs=vector<string>;
using vvc=vector<vector<char>>;
const ll INF=1e18;
const int inf=1e9;
const double eps=1e-10;
const double pi=acos(-1);
const ll mod=1e9+7;
const ll MOD=998244353;
//数学関連
int num_order(ll N){
int res=0;
while(N>0){
N/=10;
res++;
}
return res;
}
ll GCD(ll A,ll B){
if(B==0){
return A;
}
else{
return GCD(B,A%B);
}
}
ll LCM(ll A,ll B){
ll g=GCD(A,B);
return A/g*B;
}
vpll prime_factorize(ll N){
vpll res;
for(ll i=2;i*i<=N;i++){
if(N%i!=0){
continue;
}
ll ex=0;
while(N%i==0){
ex++;
N/=i;
}
res.push_back({i,ex});
}
if(N!=1){
res.push_back({N,1});
}
return res;
}
ll modpow(ll x,ll p,ll y){
ll res=1;
while(p>0){
if(p&1) res=res*x%y;
x=x*x%y;
p>>=1;
}
return res;
}
ll small_nCk(ll n,ll k){
ll res=1,fact=1;
for(int i=0;i<k;i++){
res=res*(n-i)/fact;
fact++;
}
return res;
}
//データ構造
struct UnionFind {
//自身が親であれば、その集合に属する頂点数に-1を掛けたもの
//そうでなければ親のid
vector<int> r;
UnionFind(int N){
r=vector<int>(N,-1);
}
int root(int x){
if (r[x]<0) return x;
return r[x]=root(r[x]);
}
bool unite(int x,int y){
x=root(x);
y=root(y);
if(x==y) return false;
if(r[x]>r[y]) swap(x,y);
r[x]+=r[y];
r[y]=x;
return true;
}
int size(int x){
return -r[root(x)];
}
};
int main(){
int N,X;
cin>>N>>X;
vpii vec(N);
for(int i=0;i<N;i++) cin>>vec[i].first>>vec[i].second;
int sum=0;
for(int i=0;i<N;i++){
sum+=(vec[i].first*vec[i].second);
if(sum>X*100){
cout<<i+1<<endl;
return 0;
}
}
cout<<-1<<endl;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)
constexpr int INF = 0x3f3f3f3f;
int main() {
int n, m;
cin >> n >> m;
vector<int> w(n);
for (int& x : w) cin >> x;
int maxw = *max_element(w.begin(), w.end());
vector<int> cost(1 << n);
for (int i = 0; i < m; ++i) {
int l, v;
cin >> l >> v;
if (maxw > v) {
cout << -1 << '\n';
return 0;
}
for (int bit = 0; bit < 1 << n; ++bit) {
int s = 0;
for (int j = 0; j < n; ++j)
if (bit >> j & 1) s += w[j];
if (s > v) cost[bit] = max(cost[bit], l);
}
}
vector<int> p(n);
iota(p.begin(), p.end(), 0);
// らくだの集合を考えるとこれ以上血がづいてはいけないという制約がたくさん
// 蟻本によるとグラフの辺と同義っぽい
// らくだが数直線上にいるとして、pos[i]に位置が入ってるとする
// らくだiとjがl離れていないといけない → pos[i] + l <= pos[j]
// 一方グラフの辺は最短経路を考えたときにv[from] + cost >= v[to]
// みたいな感じになってる 蟻本より
// pos[i] + l <= pos[j] <=> pos[j] - l >= pos[i]
// なのでj -> iに-lの辺を張ったのと同じ
// 並び順を維持するので
// 0<= i < j < nに対してpos[i] <= pos[j] <=> pos[j] + 0 >= pos[i]
// j -> iにコスト0の辺を張る
int ans = INF;
using tii = tuple<int, int, int>;
do {
// 後ろ側から見て、前に辺を張っていく
vector<tii> es;
for (int r = 1; r < n; ++r) {
int s = 1 << p[r];
for (int l = r - 1; l >= 0; --l) {
s |= 1 << p[l];
es.emplace_back(r, l, -cost[s]);
}
}
for (int i = 0; i < n - 1; ++i) es.emplace_back(i + 1, i, 0);
// べるまん・フォード]]
vector<int> d(n, INF);
d[n - 1] = 0;
for (int i = 0; i <= n; ++i)
for (auto& [f, t, c] : es)
if (d[f] != INF && d[t] > d[f] + c) d[t] = d[f] + c;
if (d[0] != INF) ans = min(ans, -d[0]);
} while (next_permutation(p.begin(), p.end()));
cout << ans;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <math.h>
#include <map>
#include <set>
#include<vector>
#include<random>
#include<string>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
using lli = long long int;
int main(){
lli n;
cin>>n;
ll ans=0;
if(n>=1000) ans+=n-999;
if (n>=1000000) ans+=n-999999;
if (n>=1000000000) ans+=n-999999999;
if (n>=1000000000000) ans+=n-999999999999;
if (n>=1000000000000000) ans+=n-999999999999999;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); }
const ll inf = 1e10;
ll powx(ll a, ll b){
ll ret = 1;
FOR(i, b){
if (ret > inf / a)
return inf+1;
ret *= a;
}
return ret;
}
int main(int argc, char** argv) {
//ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
ll n;
cin >> n;
set<ll> S;
for(int b=2; b<=60; b++){
ll a = 2;
while(1){
ll ab = powx(a, b);
if (ab <= n)
S.insert(ab);
else
break;
a++;
}
}
cout << n-S.size() << endl;
if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define PI acos(-1)
#define int long long
bool gt(string s,string t){
vector<int>cnt1(10,0),cnt2(10,0);
for(auto u:s)cnt1[u-'0']++;
int a=0;
for(int i=1;i<=9;i++)a+=i*pow(10,cnt1[i]);
for(auto u:t)cnt2[u-'0']++;
int b=0;
for(int i=1;i<=9;i++)b+=i*pow(10,cnt2[i]);
return a>b;
}
int32_t main(){
IOS;
int k;
cin>>k;
string s,t;
cin>>s>>t;
vector<int>cnt(10,k);
for(int i=0;i<4;i++){
cnt[s[i]-'0']--;
cnt[t[i]-'0']--;
}
int scorea=0;
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
s.back()=i+'0';
t.back()=j+'0';
//cout<<temp1<<" "<<temp2<<endl;
if (gt(s,t))scorea+=cnt[i]*(cnt[j]-(i==j));
}
}
//cout<<scorea<<" "<<scoreb<<endl;
cout<<fixed<<setprecision(12)<<1.0*scorea/(9*k-8)/(9*k-9)<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O0")
typedef long long ll;
typedef long double ld;
const ll mod = 1e9+7;
const ll INF = 1e18;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
#define Rep(i,a,n) for (ll i = (a); i < (n); ++i)
#define All(a) (a).begin(),(a).end()
#define Pi acos(-1)
using V = vector<ll>;
using P = pair<ll,ll>;
using Graph = vector<V>;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << setprecision(15) << fixed;
ll n, m, t;
cin >> n >> m >> t;
V a(m);
V b(m);
rep(i,m) {
cin >> a[i] >> b[i];
}
ll N = n;
rep(i,m) {
if (i == 0) {
n -= a[i];
}
else {
n -= a[i];
n += b[i-1];
}
if (n <= 0) {
cout << "No\n";
return 0;
}
n += (b[i] - a[i]);
n = min(n,N);
}
if (n - (t-b[m-1]) <= 0) {
cout << "No\n";
}
else {
cout << "Yes\n";
}
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<vector<int>> t(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> t.at(i).at(j);
}
}
int ans = 0;
vector<int> order(n);
for (int i = 0; i < n; i++) order.at(i) = i;
do {
if (order.at(0) != 0) continue;
int keep = 0;
for (int i = 0; i < n; i++) keep += t.at(order.at(i)).at(order.at((i + 1) % n));
if (keep == k) ans++;
} while (next_permutation(order.begin(), order.end()));
cout << ans << '\n';
} | //
// main.cpp
//
#include <algorithm>
#include <array>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <limits>
#include <inttypes.h>
#include <map>
#include <math.h>
#include <memory>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <atcoder/dsu>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
// using mint = modint;
using namespace std;
using ll = int64_t;
using ull = uint64_t;
[[maybe_unused]] constexpr ll LL_MAX = numeric_limits<ll>::max();
[[maybe_unused]] constexpr ll LL_MIN = numeric_limits<ll>::min();
[[maybe_unused]] constexpr ull ULL_MAX = numeric_limits<ull>::max();
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (a)-1; i >= (b); i--)
template<typename T>
void chmin(T& x, T y) {
x = min(x, y);
}
template<typename T>
void chmax(T& x, T y) {
x = max(x, y);
}
ll csum(ll x, ll y) {
if (x > 0 && y > 0 && x > LL_MAX - y) {
return LL_MAX;
}
if (x < 0 && y < 0 && x < LL_MIN - y) {
return LL_MIN;
}
return x + y;
}
ll readi() {
ll ret;
scanf("%" PRId64 "", &ret);
return ret;
}
vector<ll> readvi(ll n) {
vector<ll> ret(n);
rep(i, 0, n) { ret[i] = readi(); }
return ret;
}
double readf() {
double ret;
scanf("%lf", &ret);
return ret;
}
string reads() {
string s;
cin >> s;
return s;
}
void writei(ll x) {
printf("%" PRId64 "\n", x);
}
void writevi(const vector<ll>& xs) {
rep(i,0,xs.size()) {
if (i < xs.size() - 1) {
printf("%" PRId64 " ", xs[i]);
} else {
printf("%" PRId64 "\n", xs[i]);
}
}
}
void writes(const string& s) {
cout << s.c_str() << endl;
}
template<typename T>
vector<T> make_vec_nd(T init, ll size) {
return vector<T>(size, init);
}
template<typename T, typename... Args>
auto make_vec_nd(T init, ll size, Args... rest) {
auto inner = make_vec_nd(init, rest...);
return vector<decltype(inner)>(size, inner);
}
struct P {
ll x;
ll y;
};
int main() {
ll N = readi();
ll M = readi();
vector<ll> hs = readvi(N);
vector<ll> ws = readvi(M);
sort(hs.begin(), hs.end());
sort(ws.begin(), ws.end());
struct HW {
bool isH;
ll idx;
ll height;
bool operator<(const HW& rhs) const {
return height < rhs.height || (height == rhs.height && isH < rhs.isH);
}
};
vector<HW> hws;
rep(i,0,N) {
hws.push_back({true, i, hs[i]});
}
rep(i,0,M) {
hws.push_back({false, i, ws[i]});
}
sort(hws.begin(), hws.end());
vector<ll> leftCnt(M);
ll cnt = 0;
for (HW& hw : hws) {
if (hw.isH) {
cnt++;
} else {
leftCnt[hw.idx] = cnt;
}
}
auto makePairs = [N, &hs]() {
vector<ll> leftPairs(N+1);
rep(n,1,N+1) {
if (n%2 == 0) {
leftPairs[n] = leftPairs[n-1] + abs(hs[n-1] - hs[n-2]);
} else {
leftPairs[n] = leftPairs[n-1];
}
}
return leftPairs;
};
vector<ll> leftPairs = makePairs();
reverse(hs.begin(), hs.end());
vector<ll> rightPairs = makePairs();
reverse(hs.begin(), hs.end());
// writevi(leftPairs);
// writevi(rightPairs);
ll ans = LL_MAX;
rep(i,0,M) {
ll ansLoc = 0;
if (leftCnt[i] % 2 == 1) {
ansLoc += leftPairs[leftCnt[i]];
ansLoc += ws[i] - hs[leftCnt[i]-1];
ansLoc += rightPairs[N-leftCnt[i]];
} else {
ansLoc += leftPairs[leftCnt[i]];
ansLoc += hs[leftCnt[i]] - ws[i];
ansLoc += rightPairs[N-leftCnt[i]-1];
}
chmin(ans, ansLoc);
}
writei(ans);
} |
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long long ll;
#define G getchar()
int read()
{
int x=0; bool flg=false; char ch=G;
for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
return flg?-x:x;
}
#undef G
#define fi first
#define se second
const int mod=998244353;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,const int &y){x=upd(x+y-mod);}
inline void iadd(int &x,const int &y){x=upd(x-y);}
int qpow(int x,int y){
int res=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1) res=1LL*res*x%mod;
return res;
}
//typedef pair<int,int> P;
#define rep(i,l,r) for (int i(l);i<=int(r);i++)
#define per(i,l,r) for (int i(r);i>=int(l);i--)
#define all(x) (x).begin(),(x).end()
int n,p[200010];
int f[200010];
int find(int x){
return f[x]?f[x]=find(f[x]):x;
}
void merge(int x,int y){
x=find(x),y=find(y);
if (x^y) f[y]=x;
}
void solve(){
n=read();
rep(i,1,n) p[i]=read(),merge(i,p[i]);
int tot=0;
rep(i,1,n) tot+=find(i)==i;
printf("%d\n",qpow(2,tot)-1);
}
int main()
{
for (int T=1;T--;) solve();
return 0;
} | #include<iostream>
#include<cstdio>
#include<queue>
#define ll long long
struct P{
ll to,next;
}e[400005];
int s = 0,t;
int out[200005],in[200005];
int cnt,head[200005];
int vis[200005];
void add(int a,int b){
e[++cnt].to = b;
e[cnt].next = head[a];
head[a] = cnt;
}
ll n,ans = 1;
bool last[200005];
ll dcnt;
void dfs(ll now){
if(!vis[now])
vis[now] = 1;
else{
dcnt ++ ;
return ;
}
for(int i = head[now];i;i = e[i].next){
if(!last[e[i].to] || (e[i].to == now))
dfs(e[i].to);
last[now] = 1;
}
}
ll mod = 998244353;
ll pow(ll now){
ll ans = 1;
ll a = 2;
while(now){
if(now & 1) ans = ans * a % mod;
a = a * a % mod;
now >>= 1;
}
return ans;
}
int main(){
scanf("%lld",&n);
t = n + 1;
for(int i = 1;i <= n;++i){
ll a;
scanf("%lld",&a);
add(i,a);
}
for(int i = 1;i <= n;++i)
if(!vis[i])
dfs(i);
std::cout<<pow(dcnt) - 1<<std::endl;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n) \
cerr << __LINE__ << ": " << #x << " = {"; \
for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
cerr << ((hoge) ? "," : "") << x[hoge]; \
cerr << "}" << '\n'
#define debugMatrix(x, h, w) \
cerr << __LINE__ << ": " << #x << " =\n"; \
for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) { \
cerr << ((hoge ? " {" : "{{")); \
for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga)) \
cerr << ((fuga ? ", " : "")) << x[hoge][fuga]; \
cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \
}
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif
template <typename T>
struct BinaryIndexedTree {
std::vector<T> dat;
BinaryIndexedTree(int n) : dat(n + 1, T(0)) {}
BinaryIndexedTree(int n, T a) : BinaryIndexedTree(std::vector<T>(n, a)) {}
BinaryIndexedTree(std::vector<T> y) : dat(y.size() + 1, 0) {
for (size_t i = 0; i < y.size(); ++i) dat[i + 1] = y[i];
for (int i = 1; i < (int)dat.size(); ++i)
if (i + (i & -i) < (int)dat.size()) dat[i + (i & -i)] += dat[i];
}
void add(int i, T a = 1) {
for (++i; i < (int)dat.size(); i += i & -i) dat[i] += a;
}
T sum(int i) { // sum [0,i)
T s = 0;
for (; i > 0; i &= i - 1) s += dat[i];
return s;
}
// sum [l,r)
T sum(int l, int r) { return sum(r) - sum(l); }
T operator[](size_t k) { return sum(k + 1) - sum(k); }
// min { i : sum(i+1) > k } -> kth element(0-indexed)
int find(T k) const {
int i = 0;
for (int p = 1 << (std::__lg(dat.size() - 1) + 1); p > 0; p >>= 1)
if (i + p < (int)dat.size() && dat[i + p] <= k) k -= dat[i += p];
return i + 1 == (int)dat.size() ? -1 : i; // -1 -> no solutions
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N, M, Q;
cin >> N >> M >> Q;
const int MAX_Y = 1e6;
BinaryIndexedTree<long long> bita1(MAX_Y), bita2(MAX_Y), bitb1(MAX_Y),
bitb2(MAX_Y);
for (int i = 0; i < N; i++) bita1.add(0);
for (int i = 0; i < M; i++) bitb1.add(0);
long long ans = 0;
long long a[N] = {}, b[M] = {};
int T[Q], X[Q], Y[Q];
for (int i = 0; i < Q; i++) cin >> T[i] >> X[i] >> Y[i];
vector<int> v(Y, Y + Q);
v.emplace_back(0);
sort(begin(v), end(v));
v.erase(unique(begin(v), end(v)), end(v));
auto zip = [&](int hoge) {
return lower_bound(begin(v), end(v), hoge) - begin(v);
};
for (int i = 0; i < Q; i++) {
int t = T[i], x = X[i] - 1;
if (t == 1) {
ans -= a[x] * bitb1.sum(zip(a[x]));
ans -= bitb2.sum(zip(a[x]), MAX_Y);
ans += Y[i] * bitb1.sum(zip(Y[i]));
ans += bitb2.sum(zip(Y[i]), MAX_Y);
bita1.add(zip(a[x]), -1);
bita2.add(zip(a[x]), -a[x]);
bita1.add(zip(Y[i]), 1);
bita2.add(zip(Y[i]), Y[i]);
a[x] = Y[i];
} else {
ans -= b[x] * bita1.sum(zip(b[x]));
ans -= bita2.sum(zip(b[x]), MAX_Y);
ans += Y[i] * bita1.sum(zip(Y[i]));
ans += bita2.sum(zip(Y[i]), MAX_Y);
bitb1.add(zip(b[x]), -1);
bitb2.add(zip(b[x]), -b[x]);
bitb1.add(zip(Y[i]), 1);
bitb2.add(zip(Y[i]), Y[i]);
b[x] = Y[i];
}
cout << ans << '\n';
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 105, mod = 1e9+7;
struct matrix {
int n, v[sz][sz];
matrix(int n) : n(n) {
memset(v, 0, sizeof(v));
}
matrix operator *(const matrix &b) {
matrix ans = matrix(n);
for(int k=0; k<n; k++) {
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
int &w = ans.v[i][j];
w += (ll) v[i][k] * b.v[k][j] % mod;
w >= mod ? w -= mod : 1;
}
}
}
return ans;
}
matrix operator ^(ll p) {
matrix ans = matrix(n), b = *this;
for(int i=0; i<n; i++) ans.v[i][i] = 1;
while(p) {
if(p & 1) ans = ans * b;
p >>= 1;
b = b * b;
}
return ans;
}
};
ll bigmod(ll b, ll n) {
if(n == -1) n = mod - 2;
ll ans = 1;
while(n) {
if(n & 1) ans = ans * b % mod;
n >>= 1;
b = b * b % mod;
}
return ans;
}
ll a[sz];
bool g[sz][sz];
int main() {
int n, m, k;
cin >> n >> m >> k;
for(int i=0; i<n; i++) cin >> a[i];
matrix mat(n);
ll two = bigmod(2, -1), minv = bigmod(m, -1);
for(int i=0; i<m; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--, v--;
g[u][v] = g[v][u] = 1;
}
for(int i=0; i<n; i++) {
int c = 0;
for(int j=0; j<n; j++) c += g[i][j];
mat.v[i][i] = ((m - c) * minv + c * minv % mod * two) % mod;
for(int j=0; j<n; j++) {
if(g[i][j]) mat.v[i][j] = minv * two % mod;
}
}
mat = mat ^ k;
for(int i=0; i<n; i++) {
ll ans = 0;
for(int j=0; j<n; j++) ans += mat.v[i][j] * a[j] % mod;
printf("%lld\n", ans % mod);
}
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
string res = "{";
for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
cerr << " " << to_string(H);
debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
struct Mint {
int val;
Mint() { val = 0; }
Mint(ll x) {
val = (-MOD <= x && x < MOD) ? x : x % MOD;
if (val < 0) val += MOD;
}
template <typename U>
explicit operator U() const { return (U)val; }
friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
friend Mint modex(Mint a, ll p) {
assert(p >= 0);
Mint ans = 1;
for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
return ans;
}
Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
Mint& operator++() { return *this += 1; }
Mint& operator--() { return *this -= 1; }
Mint operator++(int) { Mint result(*this); *this += 1; return result; }
Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
Mint operator-() const { return Mint(-val); }
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; }
friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
friend string to_string(const Mint& b) { return to_string(b.val); }
};
vector<Mint> fac(1, 1), invfac(1, 1);
Mint binom(int n, int k) {
if (k < 0 || k > n) return 0;
while (fac.size() <= n) {
fac.push_back(fac.back() * fac.size());
invfac.push_back(1 / fac.back());
}
return fac[n] * invfac[k] * invfac[n - k];
}
char n[200005];
int main() {
binom(16, 0);
int k;
scanf("%s%d", n, &k);
// [alr][...l ?...], want exactly k distinct.
// each non-alr has to appear at least once.
auto calc = [&](int l, int alr) -> Mint {
if (alr > k) return 0;
Mint ret = 0;
for (int i = 0; i <= k - alr; ++i) {
Mint v = modex(Mint(k - i), l) * binom(k - alr, i);
if (i & 1) ret -= v;
else ret += v;
}
return ret;
};
int m = strlen(n);
vector<bool> has(16);
int dis = 0;
Mint ans = 0;
for (int i = 1; i < m; ++i) {
ans += calc(i, 0) * binom(16, k);
ans -= calc(i - 1, 1) * binom(15, k - 1);
}
for (int i = 0; i < m; ++i) {
int val = n[i] >= '0' && n[i] <= '9' ? n[i] - '0' : n[i] - 'A' + 10;
for (int j = (i == 0 ? 1 : 0); j < val; ++j) {
int ndis = dis;
if (!has[j]) ndis++;
ans += calc(m - i - 1, ndis) * binom(16 - ndis, k - ndis);
}
if (!has[val]) {
dis++;
has[val] = true;
}
}
if (dis == k) ans++;
printf("%d\n", ans.val);
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
string s;
LL dp[N][20], v[N];
LL const mod = 1e9 + 7;
int k;
int main() {
cin >> s >> k;
for (int i = 0; i < s.size(); i++) {
if (s[i] >= '0' && s[i] <= '9')
v[i] = s[i] - '0';
else
v[i] = 10+s[i] - 'A';
}
LL state = 0;
for (LL i = 0; i < s.size(); i++) {
for (LL j = 1; j <= 16; ++j) {
dp[i + 1][j] = (dp[i + 1][j] + (LL)dp[i][j] * j % mod) % mod;
dp[i + 1][j + 1] =
(dp[i + 1][j + 1] + (LL)dp[i][j] * (16 - j) % mod) % mod;
}
dp[i + 1][1] = (dp[i + 1][1] + (LL)dp[i][0] * 15 % mod) % mod;
dp[i + 1][0] = (dp[i + 1][0] + dp[i][0]) % mod;
for (LL j = 0; j < v[i]; ++j) {
LL nstate = state;
if (i || j) nstate |= (1 << j);
dp[i + 1][__builtin_popcount(nstate)] += 1;
}
state |= (1 << v[i]);
}
cout << dp[s.size()][k] + (__builtin_popcount(state) == k) << endl;
return 0;
} |
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int func(int v,vector<vector<int>>& T,vector<bool>& F) {
priority_queue<int> q1,q2,q3;
int i;
F[v] = true;
for(i=0;i<T[v].size();i++) {
int temp = func(T[v][i],T,F);
if(F[T[v][i]])q2.push(temp);
else if(temp > 0)q1.push(temp);
else q3.push(temp);
if(F[T[v][i]])F[v] = !F[v];
}
int ans = -1;
while(!q1.empty()) {
ans += q1.top();
q1.pop();
}
for(i=0;!q2.empty();i++) {
ans += (i%2>0?-1:1)*q2.top();
q2.pop();
}
while(!q3.empty()) {
ans += (i%2>0?-1:1)*q3.top();
q3.pop();
}
return ans;
}
int main() {
int N;
cin >> N;
vector<vector<int>> T(N);
vector<bool> F(N);
int i,p;
for(i=1;i<N;i++) {
cin >> p;
T[p-1].push_back(i);
}
cout << ((N-func(0,T,F))/2) << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
int charToInt(char c){
char zero_num = '0';
return (int)c - (int)zero_num;
}
signed main(){
int N; cin>>N;
int M; cin>>M;
vec_int a(M);
vec_int b(M);
rep(i,M)cin>>a.at(i)>>b.at(i);
vec_int c(N); rep(i,N)cin>>c.at(i);
vector<vec_int> G(N+1);
rep(i,M){
G.at(a.at(i)).push_back(b.at(i));
G.at(b.at(i)).push_back(a.at(i));
}
map<P, int> path_map;
rep(i, M){
path_map[make_pair(a.at(i), b.at(i))] = i;
path_map[make_pair(b.at(i), a.at(i))] = i;
}
vector<string> ans(M,"");
rep(i,M){
if(c.at(a.at(i)-1)>c.at(b.at(i)-1)){
ans.at(i) = "->";
}else if(c.at(a.at(i)-1)<c.at(b.at(i)-1)){
ans.at(i) = "<-";
}
}
rep(i,M){
if(ans.at(i)==""){
vec_int visited(N+1, 0);
visited.at(a.at(i)) =1;
stack<P> st;
st.push(make_pair(b.at(i), a.at(i))); // node, parent
int node_val = c.at(a.at(i)-1);
while(!st.empty()){
int node = st.top().first;
int parent = st.top().second;
st.pop();
int ind = path_map[make_pair(parent, node)];
if(ans.at(ind)!="")continue;
if(a.at(ind)==parent && b.at(ind)==node){
ans.at(ind) = "->";
}else{
ans.at(ind) = "<-";
}
for(auto next_node: G.at(node)){
if(next_node==parent)continue;
if(c.at(next_node-1)!=node_val)continue;
st.push(make_pair(next_node, node));
}
}
}
}
rep(i,M){
cout<<ans.at(i)<<endl;
}
return 0;
} |
#include <stdio.h>
#include <utility>
#include <vector>
#include <numeric>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri();
std::vector<int> a(1 << n);
for (auto &i : a) i = ri();
std::vector<int> remaining(1 << n);
std::iota(remaining.begin(), remaining.end(), 0);
for (int i = 1; i <= n; i++) {
std::vector<int> next_remaining;
for (int j = 0; j < 1 << (n - i); j++) {
if (a[remaining[j * 2]] > a[remaining[j * 2 + 1]]) next_remaining.push_back(remaining[j * 2]);
else next_remaining.push_back(remaining[j * 2 + 1]);
}
if (i == n) {
printf("%d\n", 1 + (remaining[0] == next_remaining[0] ? remaining[1] : remaining[0]));
return 0;
}
remaining = next_remaining;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
int64_t num = 1 << N;
vector<int> A(num), B(num);
for(int i=0; i<num; i++){
cin >> A[i];
B[i] = i;
}
for(int i=1; i<N; i++){
int split = 1 << (i-1);
for(int j=(1<<i); j<=num; j+=(1<<i)){
if(A[j-1] < A[j-split-1]){
A[j-1] = A[j-split-1];
B[j-1] = B[j-split-1];
}
}
}
if(A[num/2-1] > A[num-1]){
cout << B[num-1]+1 << endl;
} else {
cout << B[num/2-1]+1 << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-8;
long double eq(long double a, long double b){
return fabs(a - b) < EPS;
}
struct point {
long double x, y;
point(long double _x=0, long double _y=0) :
x(_x), y(_y) {}
bool operator == (point &o) const {
return eq(x, o.x) and eq(y, o.y);
}
bool operator < (point &o) const {
if(!eq(x, o.x)) return x < o.x;
if(!eq(y, o.y)) return y < o.y;
return false;
}
bool operator <= (point &o) const {
if(*this == o) return true;
return *this < o;
}
};
void rotate_set(vector<point> &P, long double angle){
long double sin_angle = sin(angle);
long double cos_angle = cos(angle);
for(point &p : P){
p = point(p.x * cos_angle - p.y * sin_angle,
p.x * sin_angle + p.y * cos_angle);
}
}
vector<point> normalize(int a[], int b[], int N, int i1, int i2){
vector<point> P(N);
for(int i = 0; i < N; i++){
P[i] = point(a[i], b[i]);
}
double pivot_x = P[i1].x;
double pivot_y = P[i1].y;
for(point &p : P){
p.x -= pivot_x;
p.y -= pivot_y;
}
long double angle = atan2(P[i2].y, P[i2].x);
rotate_set(P, -angle);
return P;
}
bool are_the_same(vector<point> &S, vector<point> &T){
sort(S.begin(), S.end());
sort(T.begin(), T.end());
for(int i = 0; i < S.size(); i++){
if(!(S[i] == T[i])) return false;
}
return true;
}
int main(){
int N;
while(scanf("%d", &N) > 0){
int a[N], b[N], c[N], d[N];
for(int i = 0; i < N; i++){
scanf("%d%d", &a[i], &b[i]);
}
for(int i = 0; i < N; i++){
scanf("%d%d", &c[i], &d[i]);
}
if(N == 1){
printf("Yes\n");
continue;
}
vector<point> T = normalize(c, d, N, 0, 1);
bool test = false;
for(int i = 0; i < N and !test; i++){
for(int j = 0; j < N and !test; j++){
if(i == j) continue;
vector<point> S = normalize(a, b, N, i, j);
if(are_the_same(S, T)){
test = true;
}
}
}
printf("%s\n", test ? "Yes" : "No");
}
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define DB double
#define U unsigned
#define LL long long
#define LD long double
#define pb emplace_back
#define MP std::make_pair
#define SZ(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 100 + 5;
const DB EPS = 1e-9;
inline int sgn(DB x){
if(std::fabs(x) <= EPS) return 0;
return x > 0 ? 1 : -1;
}
struct P{
DB x,y;
P(DB x=0,DB y=0) : x(x),y(y) {}
P operator + (const P &t) const {return P(x+t.x,y+t.y);}
P operator - (const P &t) const {return P(x-t.x,y-t.y);}
DB dot(const P &t){return x*t.x+y*t.y;}
DB det(const P &t){return x*t.y-y*t.x;}
DB len2(){return x*x+y*y;}
DB len(){return std::sqrt(len2());}
inline bool operator < (const P &t) const {
if(sgn(x-t.x)) return sgn(x-t.x) < 0;
return sgn(y-t.y) < 0;
}
inline bool operator == (const P &t) const {
return !sgn(x-t.x) && !sgn(y-t.y);
}
}a[MAXN],b[MAXN],c[MAXN],t[MAXN];
int n;
inline P rotate(P v,DB sin,DB cos){
return P(v.x*cos-v.y*sin,v.x*sin+v.y*cos);
}
int main(){
// freopen("A.in","r",stdin);
scanf("%d",&n);
if(n <= 2){
puts("Yes");
return 0;
}
FOR(i,1,n) scanf("%lf%lf",&a[i].x,&a[i].y);
FOR(i,1,n) scanf("%lf%lf",&b[i].x,&b[i].y);
std::sort(b+1,b+n+1);
// a[i]*A-b[i] = a[j]*A-b[j]
// (a[i]-a[j])*A = b[i]-b[j]
FOR(i,1,n) FOR(j,1,n){
if(i == j) continue;
P u = a[1]-a[2],v = b[i]-b[j];
// if(!sgn(u.len()) || !sgn(v.len())) continue;
DB Cos = u.dot(v)/u.len()/v.len(),Sin = u.det(v)/u.len()/v.len();
FOR(k,1,n) c[k] = rotate(a[k],Sin,Cos);
FOR(k,1,n){
P vec = b[k]-c[1];
FOR(l,1,n) t[l] = c[l]+vec;
std::sort(t+1,t+n+1);
bool flag = 1;
FOR(l,1,n) flag &= t[l]==b[l];
if(flag){
puts("Yes");
return 0;
}
}
}
puts("No");
return 0;
}
|
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int>
#define vec(...) vector<__VA_ARGS__>
#define rsz(a,n) a.resize(n)
#define rszv(a,n,v) a.resize(n,v)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine
const int max_n = 500000;
using namespace std;
int n;
vi adj[max_n];
vec(pii) qry[max_n];
int ans[max_n];
int depth[max_n],siz[max_n],ep[max_n];
void dfsfsiz(int x,int par=-1) {
siz[x]=1;
for(auto y : adj[x]) {
if(y==par)continue;
depth[y]=depth[x]+1;
dfsfsiz(y,x);
siz[x]+=siz[y];
}
}
map <int,int> mp[max_n];
void dfs(int x,int par=-1) {
int mx=0,vtk=x;
for(auto y : adj[x]) {
if(y == par)continue;
dfs(y,x);
if(siz[y]>mx) {
mx=siz[y];
vtk=ep[y];
}
}
mp[vtk][depth[x]]++;
for(auto y : adj[x]) {
if(ep[y]==vtk)continue;
for(auto p : mp[ep[y]]) {
mp[vtk][p.fi]+=p.se;
}
}
for(auto p : qry[x]) {
ans[p.se]=mp[vtk][p.fi];
}
ep[x]=vtk;
}
int main(){
fcin;
cin>>n;
rep(i,n-1) {
int x;cin>>x;x--;
adj[x].pb(i+1);
adj[i+1].pb(x);
}
dfsfsiz(0);
int q;
cin>>q;
rep(_,q) {
int x,d;
cin>>x>>d;
x--;
qry[x].pb({d,_});
}
dfs(0);
rep(i,q)cout<<ans[i]<<"\n";
/*
*/
return 0;
} | #include <bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
vector<int> ans;
for(int t=0;;t++){
bool up=true;
for(int i=0;i+1<n;i++) if(a[i]>a[i+1]) up=false;
if(up) break;
int s=t%2;
while(s+1<n && a[s]<a[s+1]) s+=2;
if(s+1>=n){
ans.push_back(s-2);
swap(a[s-2],a[s-1]);
} else{
ans.push_back(s);
swap(a[s],a[s+1]);
}
}
cout << ans.size() << "\n";
for(int i=0;i<ans.size();i++) cout << ans[i]+1 << " ";
cout << "\n";
}
int main(){
int t;
cin>>t;
while(t--) solve();
} |
#include <bits/stdc++.h>
#define FOR(i, j, k) for(int i = j; i < k; i ++)
#define FORE(i, j, k) for(int i = j; i <= k; i ++)
#define FORD(i, j, k) for(int i = j; i >= k; i --)
#define mp make_pair
#define ll long long
//#define f first
//#define s second
#define int long long
using namespace std;
const int MOD=1e9+7;
const int N = 200005;
int k, n, m;
int a[N], b[N];
#undef int
int main()
{
cin >> k >> n >> m;
vector< pair < int, int > > lst(k);
int cnt = m;
FOR(i, 0, k)
{
cin >> a[i];
b[i] = (m * a[i]) / n;
cnt -= b[i];
lst[i] = mp(b[i] * n - a[i] * m, i);
}
sort(lst.begin(), lst.end());
FOR(i, 0, k)
{
if(cnt != 0)
{
-- cnt;
b[lst[i].second] ++;
}
}
FOR(i, 0, k)
{
cout << b[i] << " ";
}
return 0;
}
| #pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
#include<algorithm>
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 A[100000];
ll are[100000];
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;
if ((tmp & ma0) ^ ma0) {
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);
}
else {
tmp = tmp & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 8;
if ((ct = *ci++) >= '0') {
tmp = tmp * 10 + ct - '0';
if (*ci++ == '0') {
tmp = tmp * 10;
ci++;
}
}
}
return tmp;
}
const int MAX = 100000;
class shuturyoku_unko {
public:
char C[MAX * 5];
constexpr shuturyoku_unko() : C() {
rep(i, MAX) {
int X = i;
rep(j, 5) {
C[i * 5 + 4 - j] = '0' + X % 10;
X /= 10;
}
}
}
};
constexpr shuturyoku_unko f;
const int dm = 1 << 17;
char *dn = cn, * di = dn, * owad = dn + dm - 20;
void putint(ll A) {
if (owad < di) {
fwrite(dn, 1, di - dn, stdout);
di = dn;
}
int dig = 1;
if (A >= 100000) {
if (A >= 1000000000) dig = 5;
else if (A >= 100000000) dig = 4;
else if (A >= 10000000) dig = 3;
else if (A >= 1000000) dig = 2;
memcpy(di + dig, f.C + A % 100000 * 5, 5);
A /= 100000;
memcpy(di, f.C + A * 5 + 5 - dig, dig);
di += 5 + dig;
}
else {
if (A >= 10000) dig = 5;
else if (A >= 1000) dig = 4;
else if (A >= 100) dig = 3;
else if (A >= 10) dig = 2;
memcpy(di, f.C + A * 5 + 5 - dig, dig);
di += dig;
}
*di++ = ' ';
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int K = getint(), N = getint(), M = getint();
int kazu = 0;
rep(i, K) {
ll a2 = ll(getint()) * M;
A[i] = a2 / N;
are[i] = a2 - ll(A[i]) * N << 32 | i;
kazu += A[i];
}
nth_element(are, are + K - M + kazu, are + K);
for(int i = K - 1; i >= K - M + kazu; i--) A[are[i] & (1 << 20) - 1]++;
rep(i, K) putint(A[i]);
fwrite(dn, 1, di - dn, stdout);
Would you please return 0;
} |
#include<bits/stdc++.h>
#include <iostream>
#include <ctype.h>
#include <math.h>
#include <set>
#include<string.h>
#include<stdio.h>
using namespace std;
#define boost std::ios::sync_with_stdio(false);
typedef long long ll;
int t;
const int mod = 1000000007;
const int N = 1e3 + 6;
const int MOD = 2019;
int GCD(int a, int b)
{
if(b == 0)
return a;
else
return GCD(b,a%b);
}
int pow(int a, int b, int m)
{
int ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%m;
b/=2;
a=(a*a)%m;
}
return ans;
}
int modinv(int k)
{
return pow(k, MOD-2, MOD);
}
int pl[1000003];
void sieve(ll n) {
bool prime[n+1];
memset(prime, 1, sizeof prime);
for(ll p = 2; p*p <= n; p++) {
if(prime[p]) {
for(ll i = p*p; i <= n; i+= p) {
prime[i] = false;
}
}
}
pl[0] = 0;
for(ll i = 3; i <= n; i++) {
pl[i] = pl[i-1];
if(prime[i]) pl[i]++;
}
}
int primeDivisorCount(int x)
{
int count = 0;
for(ll i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
count+=2;
}
else
{
count++;
}
}
}
return count;
}
int findGCD(vector<int> &arr, int n)
{
int result = arr[0];
for (int i = 1; i < n; i++)
{
result = __gcd(arr[i], result)%mod;
if(result == 1)
{
return 1%mod;
}
}
result %= mod;
return result;
}
int main()
{
boost;
string s;
cin>>s;
int x = s.rfind(".");
string r = s.substr(0,x);
cout<<r<<endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
int main() {
string x, ans;
cin >> x;
for (int i = 0; i < x.length(); i++) {
if (x[i] == '.') {
break;
}
else {
ans += x[i];
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b, ll& x, ll &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
ll x1, y1;
ll g = gcd(b, a%b, x1, y1);
x = y1;
y = x1 - (a/b)*y1;
return g;
}
ll crt(const vector<pair<ll, ll>> &vet){
ll ans = 0, lcm = 1;
ll a, b, g, x, y;
for(const auto &p : vet) {
tie(a, b) = p;
ll x, y;
ll g = gcd(lcm, b, x, y);
if((a - ans) % g != 0) return -1; // no solution
ans = ans + x * ((a - ans) / g) % (b / g) * lcm;
lcm = lcm * (b / g);
ans = (ans % lcm + lcm) % lcm;
}
return ans;
}
int main(){
int T,x,y,p,q;
scanf("%d", &T);
while(T--) {
scanf("%d%d%d%d", &x, &y, &p, &q);
ll best = -1;
for(int a = x; a < x+y; a++) {
for(int b = p; b < p+q; b++) {
vector<pair<ll,ll>> aux;
aux.emplace_back(a, 2LL*x + 2LL*y);
aux.emplace_back(b, p+q);
ll cand = crt(aux);
if(cand == -1) continue;
if(best == -1 or cand < best) best = cand;
}
}
if(best == -1) printf("infinity\n");
else printf("%lld\n", best);
}
return 0;
} | #include<iostream>
#include<vector>
#include<algorithm>
#include<random>
#include<map>
long long int realOdometer = 0;
long long int manhattanOdometer = 0;
long long int generalAverage = 0;
using namespace std;
int main(){
pair<int, int>s, t;
map<pair<pair<int, int>, pair<int, int>>, pair<int, int>>distance;
int loopCount = 0;
while(cin >> s.first >> s.second >> t.first >> t.second){
string str = "";
for(int i=0; i<abs(t.first-s.first); i++){
str = str + (t.first-s.first>0? "D": "U");
}
for(int i=0; i<abs(t.second-s.second); i++){
str = str + (t.second-s.second>0? "R": "L");
}
mt19937 g(loopCount);
shuffle(str.begin(), str.end(), g);
if(loopCount<2){
cout << str << endl;
}else{
str.clear();
pair<int, int>currentPos = s;
while(currentPos!=t){
if(currentPos.first==t.first||currentPos.second==t.second){
if(currentPos.first==t.first){
str.push_back(currentPos.second<t.second? 'R': 'L');
}else{
str.push_back(currentPos.first<t.first? 'D': 'U');
}
}else{
pair<int, int>component0 = make_pair(currentPos.first, currentPos.second + (currentPos.second<t.second? +1: -1));
pair<int, int>component1 = make_pair(currentPos.first + (currentPos.first<t.first? +1: -1), currentPos.second);
pair<pair<int, int>, pair<int, int>>candidate0 = make_pair(currentPos, component0);
pair<pair<int, int>, pair<int, int>>candidate1 = make_pair(currentPos, component1);
if(candidate0.first>candidate0.second){
swap(candidate0.first, candidate0.second);
}
if(candidate1.first>candidate1.second){
swap(candidate1.first, candidate1.second);
}
int d0 = distance[candidate0].first!=0? distance[candidate0].first: generalAverage;
int d1 = distance[candidate1].first!=0? distance[candidate1].first: generalAverage;
if(d0<d1){
str.push_back(currentPos.second<t.second? 'R': 'L');
}else{
str.push_back(currentPos.first<t.first? 'D': 'U');
}
}
currentPos.first+= (str.back()=='D') - (str.back()=='U');
currentPos.second+= (str.back()=='R') - (str.back()=='L');
}
cout << str << endl;
}
long long int D;
cin >> D;
realOdometer+= D;
manhattanOdometer+= str.length();
generalAverage = realOdometer/manhattanOdometer;
int manhattan_d = str.length();
vector<pair<pair<int, int>, pair<int, int>>>toDoList;
for(int i=0; i<manhattan_d; i++){
pair<int, int>temp1, temp2;
if(i!=0){
temp1 = toDoList.back().second;
}else{
temp1 = s;
}
auto convert = [&](char c){
pair<int, int> val = temp1;
if(c=='R'||c=='L'){
val = make_pair(val.first, val.second + (c=='R'? +1: -1));
}else{
val = make_pair(val.first + (c=='D'? +1: -1), val.second);
}
return val;
};
temp2 = convert(str.at(i));
toDoList.push_back(make_pair(temp1, temp2));
}
while(toDoList.size()!=0){
if(toDoList.back().first>toDoList.back().second){
swap(toDoList.back().first, toDoList.back().second);
}
distance[toDoList.back()].second++;
distance[toDoList.back()].first =
(distance[toDoList.back()].first * (distance[toDoList.back()].second-1)
+ D/manhattan_d)/distance[toDoList.back()].second;
toDoList.pop_back();
}
loopCount++;
}
return 0;
} |
/*_____________________
|Author : canhnam357|
|___________________|
*/
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <string> vS;
typedef vector <vector <int>> vvi;
typedef vector <vll> vvll;
typedef map<int, int> mi;
typedef map<string, int> ms;
typedef map<char, int> mc;
typedef map <int, bool> mb;
typedef map<ll, ll> mll;
typedef unordered_map<int, int> umi;
typedef unordered_map<string, int> ums;
typedef unordered_map<char, int> umc;
typedef unordered_map <int, bool> umb;
typedef unordered_map<ll, ll> umll;
typedef string S;
typedef vector <ld> vld;
typedef vector <bool> vb;
typedef pair <int, int> pii;
typedef pair<ll, ll> pll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
#define FOR(i,N) for(ll i = 0 ; i < N;i++)
#define eFOR(i,a,b) for(ll i = a; i <=b;i++)
#define dFOR(i,N) for(ll i = N - 1; i>=0;i--)
#define edFOR(i,b,a) for(ll i = b ; i >=a;i--)
#define all(x) x.begin(),x.end()
#define SORT(x) sort(all(x))
#define RSORT(x) sort(x.rbegin(),x.rend())
#define UNQ(x) unique(all(x))
#define mine(x) min_element(all(x))
#define maxe(x) max_element(all(x))
#define pb push_back
#define PI 3.141592653589793
#define FIX cout << fixed << setprecision(15);
const ll mod = 998244353;
int CASE = 1;
const int mxn = 2e4 + 1;
const ll INF = 1e18;
int __gcd(int a, int b)
{
return !b ? a : __gcd(b, a % b);
}
ll sum(ll n)
{
return (n * (n + 1) / 2) % mod;
}
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
string t = "";
for (char c : s)
{
t.push_back(c);
if (t.length() > 2 && t.substr(t.length() - 3) == "fox")
{
t.pop_back();
t.pop_back();
t.pop_back();
}
}
cout << t.length();
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
while (T--)
{
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
//const int mod=998244353;
using P = pair<int,int>;
using Pl= pair<ll,ll>;
using ld=long double;
using V=vector<int>;
using Vl=vector<ll>;
using VV=vector<vector<int>>;
using VVl=vector<vector<ll>>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;cin >>n;
string s;cin >>s;
vector<string>tmp;
ll ans=0;
for (int i = 0; i < n;) {
if(s[i]!='f' and s[i]!='o' and s[i]!='x'){
tmp.clear();
i++;
}
else if(s.substr(i,3)=="fox"){
ans+=3;
i+=3;
}
else if(s.substr(i,2)=="fo"){
tmp.push_back("fo");
i+=2;
}
else if(s[i]=='f'){
tmp.push_back("f");
i++;
}
else if(s.substr(i,2)=="ox"){
if(tmp.empty()){
i+=2;
}
else{
if(tmp.back()=="f"){
ans+=3;
i+=2;
tmp.pop_back();
}
else{
tmp.clear();
i+=2;
}
}
}
else if(s[i]=='x'){
if(tmp.empty()){
i++;
}
else{
if(tmp.back()=="fo") {
ans += 3;
i++;
tmp.pop_back();
}
else{
tmp.clear();
i++;
}
}
}
else if(s[i]=='o'){
if(tmp.empty()) {
i++;
}
else if(tmp.back()=="f"){
tmp.pop_back();
tmp.push_back("fo");
i++;
}
else {
tmp.clear();
i++;
}
}
else {
tmp.clear();
i++;
}
}
cout <<n-ans<<"\n";
return 0;
}
|
#include <algorithm>
#include <iomanip>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <numeric>
#include <vector>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
struct C
{
int x, y;
C(int x = 0, int y = 0) : x(x), y(y) {}
C operator*(const C& a) const {
return C(x * a.x - y * a.y, x * a.y + y * a.x);
}
C operator-(const C& a) const {
return C(x - a.x, y - a.y);
}
bool operator<(const C& a) const {
if (x == a.x) return y < a.y;
return x < a.x;
};
bool operator==(const C& a) const {
return x == a.x && y == a.y;
};
int norm() const { return x*x + y*y;}
};
int main()
{
int n;
cin >> n;
vector<C> a(n), b(n);
rep(i, n) {
int x,y;
cin >> x >> y;
a[i] = C(x, y);
}
rep(i, n) {
int x, y;
cin >> x >> y;
b[i] = C(x, y);
}
if(n == 1) {
cout << "Yes" << endl;
return 0;
}
rep(i, n) {
rep(j, n) {
// a[0] -> b[i]
// a[1] -> b[j]
if(i == j) continue;
if((a[1] - a[0]).norm() != (b[j] - b[i]).norm()) continue; // 長さが違えばアウト
auto f = [&](vector<C> p, C o, C r) {
rep(i, n) p[i] = p[i] - o; // 平衡移動
rep(i, n) p[i] = p[i] * r; // 回転
sort(p.begin(), p.end());
return p;
};
vector<C> na = f(a, a[0], b[j] - b[i]); // a[0] を原点にして,回転
vector<C> nb = f(b, b[i], a[1] - a[0]); // a[0] を原点にして,回転
if(na == nb) {
cout << "Yes" << endl;
return 0;
}
}
}
cout << "No" << endl;
return 0;
} | #include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
#endif
ll b, c;
cin>>b>>c;
pll ara[4];
if(b > 0){
ara[0] = {-b - (c - 1) / 2, -b};
ara[1] = {b, max(b, - (- b - c / 2) - 1)};
ara[2] = {b - c / 2, b};
ara[3] = {-b, -(b - (c - 1) / 2)};
}else if(b < 0){
ara[0] = {b - c / 2, b};
ara[1] = {-b, -b + (c - 1) / 2};
ara[2] = {-b - (c - 1) / 2, -b};
ara[3] = {b, - (-b - c / 2 + 1)};
}else if(b == 0){
ara[0] = {-c / 2, 0};
ara[1] = {0, (c - 1) / 2};
ara[2] = {0, 0};
ara[3] = {0, 0};
}
sort(ara, ara + 4);
ll cur = -3e18;
ll ans = 0;
for(pll u: ara){
//cerr<<u.ff<<" "<<u.ss<<endl;
if(u.ff > cur){
ans += u.ss - u.ff + 1;
cur = u.ss;
}else{
ans += max(0LL, u.ss - cur);
cur = max(cur, u.ss);
}
}
cout<<ans<<endl;
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
using graph = vector<VI>;
template<class T = ll> using w_graph = vector<vector<pair<int, T>>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }
int main() {
init();
VI p;
const int lim = 72;
FOR(i, 2, lim) {
bool isP = true;
for (int j = 2; j * j <= i; ++j) {
if (i % j == 0)isP = false;
}
if (isP)p.push_back(i);
}
int s = p.size();
ll a, b; cin >> a >> b;
int n = b - a + 1;
auto dp = vec<ll>(n + 1, 1 << s);
dp[0][0] = 1;
FOR(i, 0, n) {
ll x = a + i;
int nxtbit = 0;
FOR(j, 0, s) {
if (x % p[j] == 0)nxtbit |= (1 << j);
}
FOR(bit, 0, 1 << s) {
dp[i + 1][bit] += dp[i][bit];
if(!(bit&nxtbit))dp[i + 1][bit|nxtbit] += dp[i][bit];
}
}
ll ans = 0;
FOR(bit, 0, 1 << s)ans += dp[n][bit];
print(ans);
return 0;
} | #include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <queue>
#include <iomanip>
#include <numeric>
#include <cmath>
using namespace std;
int main() {
string n;
cin >> n;
int k;
cin >> k;
int nn = n.size();
vector<vector<long long int>> v(nn + 1, vector<long long int>(k + 1, 0));
unordered_set<int> us;
long long int res = 0;
const int mod = 1e9 + 7;
for (int i = 1; i <= nn; i++) {
// v[already checked][how many used]
if (i > 1) {
v[i][1] += 15;
}
for (int j = 0; j <= k; j++) {
// v[i - 1][j] -> v[i][j or j + 1];
v[i][j] += v[i - 1][j] * j;
if (j > 0) v[i][j] += v[i - 1][j - 1] * (16 - j + 1);
v[i][j] %= mod;
}
char c = n[i - 1];
int nc = (c <= '9' and c >= '0' ? c - '0' : c - 'A' + 10);
for (int j = (i == 1 ? 1 : 0); j < nc; j++) {
int e = 0;
if (us.count(j) == 0) e = 1;
if (us.size() + e <= k) {
v[i][us.size() + e]++;
}
}
us.insert(nc);
}
if (us.size() == k) res++;
for (int i = k; i <= k; i++) {
res += v[nn][i];
}
res %= mod;
cout << res << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define lli long long int
#define vi vector<int>
#define vlli vector<lli>
#define vvi vector<vi >
#define vvl vector<vlli >
#define str string
#define vs vector<str>
#define vb vector<bool>
#define mp make_pair
#define pii pair<int, int>
#define pll pair<lli, lli>
#define vpii vector<pii>
#define vpll vector<pll>
#define pb push_back
#define all(a) a.begin(),a.end()
#define rev(a) reverse(all(a))
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define ft first
#define sd second
#define loop(i, s, l, j) for(int i = s; i < l; i += j)
#define rloop(i, s, l, j) for(int i = s; i >= l; i -= j)
#define rep(i, n) loop(i,0,n,1)
#define rrep(i, n) rloop(i,n-1,0,1)
#define inp(a,n) rep(i,n)cin >> a[i]
#define fastio() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define inf 10000000
#define limit 1000005
void solve()
{
int n;
str s;
cin >> n >> s;
int ans = 0;
rep(i,n)
{
int fa = 0, ft = 0, fc = 0, fg = 0;
loop(j,i,n,1)
{
if(s[j] == 'A')fa++;
if(s[j] == 'T')ft++;
if(s[j] == 'C')fc++;
if(s[j] == 'G')fg++;
if(fa == ft && fc == fg)
ans++;
}
}
cout << ans << '\n';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q=1;
//cin >> q;
while(q--)
solve();
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
typedef long long ll;
const ll INF = (1LL << 62) - (1LL << 31); /*オーバーフローしない程度に大きい数*/
const ll MOD = 1000000007;
using namespace std;
ll gcd(ll a, ll b) {
if (a % b == 0) return (b);
return (gcd(b, a % b));
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
template <class T>
void chmin(T& a, T b) { // choose minimum.
if (a > b) {
a = b;
}
}
template <class T>
void chmax(T& a, T b) {
if (a < b) {
a = b;
}
}
const int mod = 1e9 + 7;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2); }
mint& operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
// MODのときの計算に使うのでmintになっている.
mint choose(int n, int a) {
mint x = 1, y = 1;
rep(i, a) {
x *= n - i;
y *= i + 1;
}
return x / y;
}
struct UnionFind {
vector<int> par, siz;
UnionFind(int n) : par(n, -1), siz(n, 1) {}
int root(int x) {
if (par[x] == -1)
return x;
else
return par[x] = root(par[x]);
}
bool issame(int x, int y) { return root(x) == root(y); }
bool unite(int x, int y) {
// x,yを根まで移動する
x = root(x);
y = root(y);
// すでに同じグループのときは何もしない
if (x == y) return false;
/// union by size (y側のサイズが小さくなるようにする)
if (siz[x] < siz[y]) swap(x, y);
// yをxの子とする
par[y] = x;
siz[x] += siz[y];
return true;
}
// xを含むグループのサイズ
int size(int x) {
int ret = siz[root(x)];
return ret;
}
};
// 辺を表す型,ここでは重みを表す型を long long 型とする
struct Edge {
int to; // 隣接頂点番号
long long w; // 重み
Edge(int to, long long w) : to(to), w(w) {}
};
// 重み付きグラフを表す型
using Graph = vector<vector<Edge>>;
// 緩和を実施する関数
template <class T>
bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int ctoi(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
return 0;
}
int main() {
#ifdef LOCAL
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
int n;
cin >> n;
vector<int> x(n), y(n);
rep(i, n) cin >> x[i] >> y[i];
int ans = 0;
rep(i, n - 1) repi(j, i + 1, n) {
int dx = x[j] - x[i];
int dy = y[j] - y[i];
if (abs(dy) <= abs(dx)) ans++;
}
cout << ans;
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i,k,j) for(int i=(k);i>=(j);i--)
#define For(i,k,j) for(int i=(k);i<=(j);i++)
#define Foe(i,u) for(int i=lst[u],v=e[i].v;i;i=e[i].nxt,v=e[i].v)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define Fin(s) freopen(s,"r",stdin)
#define Fout(s) freopen(s,"w",stdout)
#define file(s) Fin(s".in"),Fout(s".out")
#define INF ((1ll<<60)-1)
#define int long long
const int P=998244353; //
using namespace std;
template<typename T>inline void ckmax(T &a,T b) {(a<b)&&(a=b);}
template<typename T>inline void ckmin(T &a,T b) {(a>b)&&(a=b);}
inline int mul(int a,int b) {return 1ull*a*b%P;}
inline int add(int a,int b) {return a+b>=P?a+b-P:a+b;}
inline int sub(int a,int b) {return a-b>=0?a-b:a-b+P;}
inline void mulmod(int &a,int b) {a=mul(a, b);}
inline void addmod(int &a,int b) {((a+=b)>=P)&&(a-=P);}
inline void submod(int &a,int b) {((a-=b)<0)&&(a+=P);}
inline int ksm(int a,int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline void fprint(const int &x,char c=' ') {fprintf(stderr,"%d%c",x,c);}
inline void fprint(const pii &x,char c='\n') {fprintf(stderr,"%d %d%c",x.first,x.second,c);}
inline void fprint(const int *f,const int &n,char c='\n') {for(int i=1;i<=n;i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline void fprint(const vector<int> &f,char c='\n') {for(int i=0;i<(int)f.size();i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline int inv(int a) {return ksm(a,P-2);}
namespace FastIO {
const int SIZE=1<<16; char buf[SIZE],obuf[SIZE],str[64]; int bi=SIZE,bn=SIZE,opt;
int read(char *s) {
while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
}
bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
void write(int x) {
if(!x) obuf[opt++]='0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}
}
void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}}
void Fflush() {if (opt) fwrite(obuf,1,opt,stdout); opt=0;}
};
inline int read() {int x; FI(x); return x;}
const int MN=3e5+5;
int n,a[MN],sum,pw[MN];
signed main() {
#ifndef ONLINE_JUDGE
freopen("pro.in","r",stdin);
freopen("pro.out","w",stdout);
#endif
n=read();
pw[0]=1; For(i,1,n) a[i]=read(),pw[i]=mul(pw[i-1],2);
sort(a+1,a+1+n,greater<int>());
foR(i,n,1) {
mulmod(sum,2); addmod(sum,a[i]);
}
int ans=0;
For(i,1,n) {
submod(sum,a[i]);
mulmod(sum,inv(2));
addmod(ans,mul(a[i],sum));
}
For(i,1,n) {
addmod(ans,mul(a[i],a[i]));
}
cout<<ans<<endl;
return FastIO::Fflush(),0;
}
/*
*/
| //Author : RISHABH (still under construction)
//Don't give up if you can't solve problem !
//TIPS : //copied from Benq
/*
--> check out for integer overflows
--> special cases like n==1
--> do something instead of just sitting and fooling around
--> write everything before you forget
--> think of various approaches
--> think if all the cases are similar by taking one case
--> "NEVER USE FLOAT OR DOUBLE IF YOU HAVE NOT BEEN GIVEN THE ANS AS FLOAT OR DOUBLE"
*/
//includes all headder files :
#include <bits/stdc++.h>
//standard namespace :
using namespace std ;
//defining macros :
#define ll long long
#define fo(i , k , n) for(int i = k ; i < n ; i++)
#define pb push_back
#define eb embrace_back
#define rtn return
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin() , x.end() //forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal
#define sz(a) int((a).size()) // Avoiding wrap around of size()-1 where size is a unsigned int.
#define sortall(x) sort(all(x))
#define rep(it , v) for(auto it = v.begin() ; it != v.end() ; it++)
#define each(a,x) for(auto& a : x)
#define ft front
#define bk back
#define print(x) cout << (x) ;
#define printarr(x) cout << (x) << " " ; //Credit - Harsh (cf handle : silvermirror)
#define nline endl ;
#define max(a,b,c) fmax(fmax(a,b) , c)
#define min(a,b,c) fmin(fmin(a,b) , c)
#define square(x) x*x
#define cube(x) x*x*x
#define cin(x) cin >> x ;
#define cin2(a,b) cin >> a >> b ;
#define cin3(a,b,c) cin >> a >> b >> c ;
#define cin4(a,b,c,d) cin >> a >> b >> c >> d ;
#define gline(s) getline(cin , a) ;
#define sortarr(a,n) sort(a , a+n) ;
#define Ignore cin.ignore() ;
#define mem(n,i) memset(n , i , sizeof(n)) ;
#define deci(n) cout << fixed << setprecision(n) ;
#define clr clear()
#define yes cout << "YES"
#define no cout << "NO"
//inevilability is the unavailability of vernability - Credit - Harsh (cf handle : silvermirror)
//constants : 1'000'000'007 (can be used this type too)
const long long int mod = 1e9 + 7 ;
const double PI = 3.1415926535897932384626 ;
const long long int max = INT_MAX ;
const long long int INF = 1e18 ;
//using typedef
typedef string str ;
typedef vector<ll , ll> vi ;
typedef vector<str> vs ;
typedef pair<ll , ll> pii ;
typedef vector<pii> vpii ;
typedef map<ll , ll> mii ;
typedef map<str , ll> msi ;
typedef pair<ll , ll> pii ;
typedef set<ll> si ;
typedef set<str> ss ;
//No that's not me ! Credit - Harsh (cf handle : silvermirror)
//I don't know what the hell this is
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std::chrono ;
using namespace __gnu_pbds ;
//solution starts here :
void solve(){
ll n ;
ll D , H;
cin >> n >> D >> H ;
long double x , d[n] , h[n] , max = 0 ;
fo(i , 0 , n){
cin2(d[i] , h[i]) ;
x = h[i] - (d[i]*(H-h[i]))/(D-d[i]) ;
(x > max)?max = x : max ;
}cout << fixed << setprecision(9) << max ;
}
//main function :
int main(){
//fast IO and clock :
ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
srand(chrono::high_resolution_clock::now().time_since_epoch().count()) ;
//t is no. of testcase :
int t = 1 , tc = 1;
// cin >> t ;
while(t--){
solve() ;
cout << nline ;
// for Google competition and single ans use this :
// cout << "Case #" << tc << ": " << ans << endl ;
}
}
|
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define sz(x) (int)(x).size()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<char> vc;
typedef vector<bool> vb;
const int N = 17;
int n,e;
vector<pii> a;
void escribir (vector<pii> a) {
for (auto e: a) {
cout << e.F << " ";
}
cout << endl;
}
int main () {
cin >> n;
FOR(i,1,1<<n) {
cin >> e;
a.PB({e,i});
}
while (a.size()>2) {
// cout << "a:";
// escribir(a);
vector<pii> b;
// b.clear();
for (int i=0;i<a.size();i+=2) {
if (a[i].F>a[i+1].F) {
b.PB(a[i]);
}
else {
b.PB(a[i+1]);
}
}
// cout << "b:";
// escribir(b);
a = b;
}
// cout << "a:";
// escribir(a);
if (a[0].F<a[1].F) {
cout << a[0].S;
}
else {
cout << a[1].S;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define ll long long
#define pi (3.141592653589)
#define mod 1000000007
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rfo(i, a, b) for (int i = (int)a; i >= (int)b; i--)
#define fo(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define see(x) cout << (x) << endl
#define hh cout << endl
#define INF 1e18
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
template <typename C,
typename T = std::decay_t<decltype(*begin(std::declval<C>()))>,
typename std::enable_if<!std::is_same<C, std::string>::value>::type * = nullptr>
std::ostream &operator<<(std::ostream &os, const C &container)
{
bool first = true;
std::stringstream ss;
ss << '[';
for (const auto &x : container)
{
if (!first)
{
ss << ", ";
}
first = false;
ss << x;
}
ss << ']';
return os << ss.str();
}
template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p)
{
os << '{' << p.first << ", " << p.second << '}';
return os;
}
ll mod_pow(ll a, ll b)
{
if (b < 0)
return 0;
a %= mod;
if (b == 0)
return 1;
if (b == 1)
return a % mod;
if (b == 2)
return ((a % mod) * (a % mod)) % mod;
return (mod_pow(mod_pow(a, b / 2), 2) * mod_pow(a, b % 2)) % mod;
}
bool isPrime(int n)
{
if (n == 1)
return false;
if (n == 2)
return true;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Keep calm and Keep coding //
void solve()
{
int n, m;
cin >> n;
cin >> m;
int t;
cin >> t;
cout << max(n + m, max(m + t, t + n));
return;
}
int32_t main()
{
fast int tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
#define TO_STRING(VariableName) # VariableName
#define debug(x) cerr<<TO_STRING(x)<<" : "<<x<<" "<<endl;
void debugs() { cerr << endl; }
template <class T, class... Args>
void debugs(const T& x, const Args &... args) {
cerr << x << " ";
debugs(args...);
}
template<class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>& p) {
ost << "{ " << p.first << ", " << p.second << " }";
return ost;
}
template<class T>
ostream& operator<<(ostream& ost, const vector<T>& v) {
ost << "{ ";
for (int i = 0; i < v.size(); i++) {
if (i)ost << ", ";
ost << v[i];
}
ost << " }";
return ost;
}
template<class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>& v) {
ost << "{ ";
for (auto p : v) {
ost << "{ " << p.first << ", " << p.second << " }";
}
ost << " }";
return ost;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//
std::vector<ll> find_divisor(ll n) {
std::vector<ll> divisor;
ll i = 1;
for (; i * i <= n; i++) if (n % i == 0) divisor.push_back(i);
i--;
if (i * i == n) i--;
for (; i >= 1; i--) if (n % i == 0) divisor.push_back(n / i);
return divisor;
}
int main() {
ll n;
cin >> n;
vector<ll> a(n+1);
cout << "1";
a[1] = 1;
for(ll i = 2; i <= n; i++){
a[i] = 0;
vector<ll> d = find_divisor(i);
rep(j, d.size()-1){
a[i] = max(a[i], a[d[j]]);
}
a[i]++;
cout << " " << a[i];
}
cout << endl;
return 0;
}
|
// Problem: F - Silver Woods
// Contest: AtCoder - AtCoder Beginner Contest 181
// URL: https://atcoder.jp/contests/abc181/tasks/abc181_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i,x,n) for(int i = x;i <= n;++i)
typedef pair<int,int> pii;
#define x first
#define y second
const int N = 215;
const double eps = 1e-6;
int fa[N],n;
pii pts[N];
int find(int x)
{
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
double dist(int a,int b)
{
int dx = pts[a].x - pts[b].x,dy = pts[a].y - pts[b].y;
return sqrt(dx * dx + dy * dy);
}
bool check(double r)
{
forn(i,1,N - 1) fa[i] = i;
forn(i,1,n)
{
if(abs(pts[i].y - 100) < 2 * r)
{
int fax = find(N - 1),fay = find(i);
fa[fax] = fay;
}
if(abs(pts[i].y + 100) < 2 * r)
{
int fax = find(N - 2),fay = find(i);
fa[fax] = fay;
}
forn(j,1,n)
{
if(dist(i,j) < 2 * r)
{
int fax = find(i),fay = find(j);
fa[fax] = fay;
}
}
}
return find(N - 1) != find(N - 2);
}
int main()
{
scanf("%d",&n);
forn(i,1,n) scanf("%d%d",&pts[i].x,&pts[i].y);
double l = 0,r = 205;
while(r - l > eps)
{
double mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
printf("%.18lf",l);
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int MOD = 1000000007;
int N, M;
ll expo(ll a, ll b)
{
if (b == 0)
return 1;
if (b == 1)
return a % MOD;
if (b % 2 == 0)
return expo(a*a % MOD, b/2);
return a*expo(a*a % MOD, (b-1)/2) % MOD;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N >> M;
int sum = 0;
for (int i = 0, a; i < N; i++)
{
cin >> a;
sum += a;
}
ll num = 1, dem = 1;
for (int i = 0; i < sum + N; i++)
{
num = num*(M + N - i) % MOD;
dem = dem*(i + 1) % MOD;
}
cout << num*expo(dem, MOD - 2) % MOD << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
int n;
cin >> n;
vector<ll> a(n);
for (ll &i : a)
cin >> i;
sort(a.begin(), a.end());
ll ans = 0;
for (int i = 0; i != n; i++)
ans += i * a[i] - (n - (i + 1)) * a[i];
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define pi pair<lli,lli>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for(lli i=a;i<b;i++)
#define all(x) x.begin(), x.end()
const lli inf=10000000000000000;
lli mod = 1e9+7;
//const long double pi = 3.14159265358979323846264338;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t=1;
// cin>>t;
while(t--) {
lli n;
cin>>n;
vector <pi> a(n),b(n);
FOR(i,0,n){
cin>>a[i].fi>>a[i].se;
b[i].fi = a[i].se;
b[i].se = a[i].fi;
}
sort(all(a));sort(all(b));
lli m = inf;
if(a[0].se==b[0].fi){
m = min(m,a[0].fi+a[0].se);
m = min(m,max(a[0].fi,b[1].fi));
m = min(m,max(a[1].fi,b[0].fi));
}
else{
m = min(m,max(a[0].fi,b[0].fi));
}
cout<<m<<endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> veca(n);
vector<int> vecb(n);
int cost = 1000000;
int costt = 1000000;
for (int i = 0; i < n; i++) {
cin >> veca.at(i) >> vecb.at(i);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) cost = min(cost, max(veca.at(i), vecb.at(j)));
if (i == j) costt = min(costt, veca.at(i) + vecb.at(j));
}
}
int ans = min(cost, costt);
cout << ans;
} |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 100000;
const long long inf = 2e18;
#define ll long long
#define PLI pair<ll, int>
int n, m;
ll dis[max_n + 15];
struct E {
int v;
ll c, d;
E () {}
E (int v, ll c, ll d) : v(v), c(c), d(d) {}
};
vector<E> edges[max_n + 15];
priority_queue<PLI, vector<PLI>, greater<PLI> > que;
inline ll f(ll x, ll d) { return x + d / (x + 1); }
ll cal(ll start_t, ll d) {
ll s = start_t, e = 1e9, ans;
while (s <= e - 1000) {
ll ml = (2 * s + e) / 3;
ll mr = (s + 2 * e) / 3;
ll fl = f(ml, d), fr = f(mr, d);
if (fl < fr) e = mr;
else s = ml;
//cout << s << ' ' << e << endl;
}
ans = f(s, d);
for (ll i = s + 1; i <= e; ++i) {
ll res = f(i, d);
if (res < ans) ans = res;
}
return ans - start_t;
}
ll Dij() {
for (int i = 2; i <= n; ++i) dis[i] = inf;
que.push(PLI(dis[1] = 0, 1));
while (que.size()) {
PLI pp = que.top(); que.pop();
ll t = pp.first; int u = pp.second;
if (dis[u] < t) continue;
for (auto e : edges[u]) {
int v = e.v; ll c = e.c, d = e.d;
ll delta = c + cal(t, d);
if (dis[u] + delta < dis[v]) {
que.push(PLI(dis[v] = dis[u] + delta, v));
}
}
}
return inf == dis[n] ? -1 : dis[n];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v, c, d;
scanf("%d%d%d%d", &u, &v, &c, &d);
edges[u].push_back(E(v, c, d));
edges[v].push_back(E(u, c, d));
}
printf("%lld\n", Dij());
return 0;
} | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
int main() {
ll x, y; cin >> x >> y;
if (x >= y) {
cout << x - y << '\n';
return 0;
}
map<ll, int> dp{{y, 0}};
using P = pair<int, ll>;
priority_queue<P, vector<P>, greater<P>> que;
que.emplace(0, y);
ll ans = y - x;
while (!que.empty()) {
auto [cost, z] = que.top(); que.pop();
if (cost > dp[z] || cost >= ans) continue;
if (z % 2 == 0) {
if (dp.count(z / 2) == 0 || dp[z / 2] > dp[z] + 1) {
dp[z / 2] = dp[z] + 1;
chmin(ans, dp[z / 2] + abs(z / 2 - x));
if (z / 2 > x) que.emplace(dp[z / 2], z / 2);
}
} else {
if (dp.count((z + 1) / 2) == 0 || dp[(z + 1) / 2] > dp[z] + 2) {
dp[(z + 1) / 2] = dp[z] + 2;
chmin(ans, dp[(z + 1) / 2] + abs((z + 1) / 2 - x));
if ((z + 1) / 2 > x) que.emplace(dp[(z + 1) / 2], (z + 1) / 2);
}
if (dp.count((z - 1) / 2) == 0 || dp[(z - 1) / 2] > dp[z] + 2) {
dp[(z - 1) / 2] = dp[z] + 2;
chmin(ans, dp[(z - 1) / 2] + abs((z - 1) / 2 - x));
if ((z - 1) / 2 > x) que.emplace(dp[(z - 1) / 2], (z - 1) / 2);
}
}
}
cout << ans << '\n';
return 0;
}
|
/*
* A.cpp
*
* Created on: Feb 6, 2021
* Author: jrantor
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
double v,t,s,d;
cin >> v >> t >> s >> d;
double x = d/v;
if(x >= t && x <= s){
cout << "No";
}
else cout << "Yes";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pi = pair<int, int>;
using pii = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<ll>;
using vip = vector<pair<int, int>>;
const int MAX_N = 1e5 + 1;
const ll mod = 1e9 + 7;
const ll INF = 1e9;
#define PI 3.141592653589793238
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pb push_back
#define mp make_pair
#define loop(i,n,x) for(int i=n;i<x;i++)
#define sz(a) ((int) (a).size())
#define For(n,x) for(int i=1;i<=x;i++)
#define ite iterator it
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;cin>>t; while(t--){solve();}
void oj()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
bool isPowerOfTwo(ll n)
{
if (n == 0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
void solve()
{
float v, t, s, d;
cin >> v >> t >> s >> d;
float result = d / v;
if (result >= t && result <= s)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
oj();
//test;
solve();
}
|
#include<cstdio>
#define ll long long
using namespace std;
int main(){
ll a,b,c,d;
scanf("%lld %lld\n%lld %lld",&a,&b,&c,&d);
ll ans = 0;
ans = b-c;
printf("%lld\n",ans);
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll a,b;
cin>>a>>b;
cout<<(2*a+100)-b<<endl;
}
|
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
typedef long long lint;
using namespace std;
int main(){
string S[3];
REP(i,3)cin>>S[i];
char SF[3];
REP(i,3)SF[i]=S[i][0];
REP(i,3)reverse(ALL(S[i]));
set<char> s;
REP(i,3)for(auto c:S[i])s.insert(c);
if(s.size()>10){
cout<<"UNSOLVABLE"<<endl;
return 0;
}
map<char,int> c;
int i=0;
for(auto e:s){
c[e]=i;
i++;
}
vector<int> v(10);
REP(i,10)v[i]=i;
do {
bool f=false;
REP(i,3)if(v[c[SF[i]]]==0)f=true;
if(f)continue;
lint N[3]={0};
REP(i,3){
lint base=1;
for(auto e:S[i]){
N[i]+=v[c[e]]*base;
base*=10;
}
}
if(N[0]+N[1]==N[2]){
REP(i,3)cout<<N[i]<<endl;
// for(auto e:c){
// cerr<<e.first<<":"<<v[e.second]<<endl;
// }
// cerr<<endl;
return 0;
}
} while( next_permutation(v.begin(), v.end()) ); // 次の順列を生成
cout<<"UNSOLVABLE"<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define INF 1000000000000
typedef long long ll;
int main()
{
int a, b, c, ans = 0;
cin >> a >> b >> c;
if (a != b && b != c && c != a)
{
cout << 0 << endl;
}
else if (a == b)
{
cout << c << endl;
}
else if (b == c)
{
cout << a << endl;
}
else
{
cout << b << endl;
}
} |
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <unordered_map>
#include <cstdio>
#include <climits>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long ll;
ll gcd(ll x, ll y) {
if (y == 0)return x;
return gcd(y, x%y);
}
ll lcm(ll x, ll y) {
return x / gcd(x, y)*y;
}
ll kai(ll x, ll y, ll m) {
ll res = 1;
for (ll i = x - y + 1; i <= x; i++) {
res *= i; res %= m;
}
return res;
}
ll mod_pow(ll x, ll y, ll m) {
ll res = 1;
while (y > 0) {
if (y & 1) {
res = res * x % m;
}
x = x * x % m;
y >>= 1;
}
return res;
}
ll comb(ll x, ll y, ll m) {
if (y > x)return 0;
return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}
const ll INF = 1000000000000000000;
int n, m, a[200010], x[200010], y[200010];
vector<int> vec[200010];
ll d[200010];
ll ans = -INF;
signed main() {
std::random_device rnd;
std::mt19937_64 mt(rnd());
cin >> n >> m;
for (int i = 1; i <= n; i++)cin >> a[i];
fill(d, d + n + 1, INF);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i];
vec[y[i]].push_back(x[i]);
}
d[1] = a[1];
for (int i = 2; i <= n; i++) {
ll mi = INF;
for (int j = 0; j < (int)vec[i].size(); j++)mi = min(mi, d[vec[i][j]]);
ans = max(ans, (ll)a[i] - mi);
d[i] = min(mi, (ll)a[i]);
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> Pair;
typedef vector<Pair> VP;
typedef vector<string> VS;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, n) for(int i = (int)(s); i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n); i >= 0; i--)
#define RREP(i, n, e) for(int i = (int)(n); i>= e; i--)
#define all(a) (a).begin(), (a).end()
#define VEC(n, x) VI n(x); rep(i, x) cin >> n[i];
#define MAT(m, x, y) VVI m(x, VI(y)); rep(i, x) rep(j, y) cin >> m[i][j];
#define FIX cout << fixed << setprecision(10);
#define out(x) cout << (x) << endl;
#define fin(x) { out(x); return 0; }
#define IN(x, l, r) ((l) <= (x) && (x) < (r))
template<class T> bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
void solve(VVI &G, VI &A, int v, int m, VI &res) {
if (chmax(res[v], A[v] - m)) {
for(auto nv: G[v]) solve(G, A, nv, min(m, A[v]), res);
}
}
int main () {
int N, M; cin >> N >> M;
VEC(A, N);
VI X(M), Y(M); rep(i, M) cin >> X[i] >> Y[i], X[i]--, Y[i]--;
VVI G(N); rep(i, M) G[X[i]].push_back(Y[i]);
vector<bool> start(N, true); rep(i, M) start[Y[i]] = false;
VI res(N, INT_MIN);
rep(i, N) if(start[i]) solve(G, A, i, INT_MAX, res);
int ans = INT_MIN; rep(i, N) chmax(ans, res[i]);
out(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std ;
const int MAX = 1000 + 10 ;
char arr[MAX][MAX] ;
int n , m ;
int par[MAX] , sz[MAX] ;
void init()
{
for(int i = 0 ; i < max(n , m) ; ++i)
par[i] = i , sz[i] = 1 ;
}
int root(int node)
{
if(par[node] == node)
return node ;
return (par[node] = root(par[node])) ;
}
void Union(int x , int y)
{
int a = root(x) ;
int b = root(y) ;
if(sz[a] < sz[b])
swap(a , b) ;
par[b] = a ;
sz[a] += sz[b] ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < m ; ++j)
cin>>arr[i][j] ;
}
init() ;
arr[0][0] = arr[0][m-1] = arr[n-1][0] = arr[n-1][m-1] = '#' ;
for(int i = 0 ; i < n ; ++i)
{
int last = -1 ;
for(int j = 0 ; j < m ; ++j)
{
if(arr[i][j] != '#')
continue ;
if(last != -1)
Union(j , last) ;
last = j ;
}
}
set<int>s ;
for(int j = 0 ; j < m ; ++j)
s.insert(root(j)) ;
int ans = s.size() - 1 ;
init() ;
s.clear() ;
for(int j = 0 ; j < m ; ++j)
{
int last = -1 ;
for(int i = 0 ; i < n ; ++i)
{
if(arr[i][j] != '#')
continue ;
if(last != -1)
Union(i , last) ;
last = i ;
}
}
for(int i = 0 ; i < n ; ++i)
s.insert(root(i)) ;
ans = min(ans , (int)(s.size())-1) ;
return cout<<ans<<"\n" , 0 ;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
int h, w, p[2005];
char c;
int find(int x){return (p[x] < 0 ? x : p[x] = find(p[x]));}
set<int> hh, ww;
void uni(int x, int y){
x = find(x); y = find(y);
if(x != y){
if(p[x] > p[y]) swap(x, y);
p[x] += p[y];
p[y] = x;
}
}
int main(){
scanf("%d%d", &h, &w);
fill(p, p+h+w, -1);
uni(0,h); uni(0,h+w-1); uni(h-1,h);
rep(i,h) rep(j,w+1){
c = getchar();
if(c == '#') uni(i,h+j-1);
}
rep(i,h) hh.insert(find(i));
rep(i,w) ww.insert(find(i+h));
printf("%d\n", min((int)hh.size(),(int)ww.size())-1);
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define INF 2147483647
#define INFLL 1000000000000
#define MOD 998244353
#define PI 3.14159265359
#define dij priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>
#define cout_all(v) for(auto e:v)cout<<e<<" ";cout<<endl
#define smart(v) v.erase(unique(all(v)),v.end())
typedef vector<vector<int>> graph;
typedef long long ll;
typedef pair<int,int> pii;
typedef map<int,int> mii;
typedef pair<ll,ll> pll;
//
int main(){
int v,t,s,d;cin>>v>>t>>s>>d;
int a=v*t,b=v*s;
a<=d&&d<=b? cout<<"No"<<endl:cout<<"Yes"<<endl;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000;//10の12乗
int main() {
int t;
cin >> t;
string at="atcoder";
rep(i,t){
string s;
cin >> s;
if(s>at){
cout << 0 << endl;
continue;
}
int len=min(2,(int)s.length());
int len2=s.length();
ll ans=inf;
rep(j,len){
rep(k,len2-j){
if(at[j]<s[j+k]){
chmin(ans,(ll)k);
}
}
if(at[j]>s[j]) break;
}
if(ans==inf) cout << -1 << endl;
else cout << ans << endl;
}
} |
///////////////////////Loading Payloads.......////////////////////////////
#include<bits/stdc++.h>
#define f(i,a,b) for(int i = a; i < b; i++)
#define fd(i,a,b) for(int i = a; i > b;i--)
#define fld(i,a,b) for(ll i = a ; i > b;i--)
#define ll long long
#define fl(i,a,b) for(ll i = a; i < b; i++)
#define VI vector<int>
#define VP vector<pair<int,int>>
#define VPL vector<pair<ll,ll>>
#define VS vector<string>
#define VL vector<long long>
#define VC vector<char>
#define VF vector<float>
#define VD vector<double>
#define VB vector<bool>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define fa(i,skill) for(auto i : skill)
#define fr(i,skill) for(VI :: reverse_iterator i = skill.rbegin(); i != skill.rend(); i++)
#define PI pair<int,int>
#define mi map<int,int>
#define ml map<ll,ll>
#define mci map<char,int>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define MAX 1000000007
#define INF 1000000000
#define debug(x) cout<<"This side ----> "<<#x<<" -> "<<x<<endl
using namespace std;
///////////////////////Engine Check.......////////////////////////////
void edit(VI &skill,int n)
{
f(i,0,n)
{
int x;
cin >> x;
skill.PB(x);
}
}
bool cmp(pair<ll,ll>p1,pair<ll,ll>p2)
{
if(p1.S > p2.S)
return true;
if(p1.S < p2.S)
return false;
if(p1.F > p2.F)
return true;
else
return false;
}
void editl(VL &skill,ll n)
{
fl(i,0,n)
{
ll x;
cin >> x;
skill.PB(x);
}
}
void editpl(VPL &skill,ll n)
{
fl(i,0,n)
{
ll x,y;
cin >> x >> y;
skill.PB({x,y});
}
}
ll nextPowerOf2(ll n)
{
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
bool isGoogles = 0;
bool testcase = 0;
void achilles()
{
ll n;cin >> n;
VPL arr,arr2;
fl(i,0,n)
{
ll x,y;cin >>x >> y;
arr.PB({x,y});
arr2.PB({y,x});
}
sort(all(arr));
sort(all(arr2));
ll mxx1 = abs(arr[n-2].F - arr[0].F);
ll mxx2 = abs(arr[n-1].F - arr[1].F);
ll mxy1 = abs(arr2[n-2].F - arr2[0].F);
ll mxy2 = abs(arr2[n-1].F - arr2[1].F);
ll maxx = abs(arr[n-1].F - arr[0].F);
ll maxy = abs(arr2[n-1].F - arr2[0].F);
// if(maxx > maxy)
// debug(arr2[n-1].F);debug(arr2[0].F);
// debug(maxx);
// debug(maxy);
// debug(mxx1);
// debug(mxx2);
// debug(mxy1);
// debug(mxy2);
// cout << max(max(mxx1,mxx2),max(mxy1,mxy2)) << "\n";
if(arr[n-1] == arr2[n-1])
{
// cout << arr[n].F << ' ' << arr[n].S << "\n";
// cout << arr2[n].F << ' ' << arr2[n].S << "\n";
cout << max(max(mxx1,mxx2),max(mxy1,mxy2)) << "\n";
}
else
{
VL ex;
ex.PB(maxx);
ex.PB(maxy);
ex.PB(mxx1);
ex.PB(mxx2);
ex.PB(mxy1);
ex.PB(mxy2);
sort(rall(ex));
cout << ex[1] << "\n";
}
}
///////////////////////3....2....1...Ignition////////////////////////////
int main()
{
ios :: sync_with_stdio(0);
cin.tie(0);
ll t=1,w=1;
if(testcase)
cin >>t;
while(t--)
{ if (isGoogles) {cout << "Case #" << w << ": ";}
achilles();
w++;
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vc> vvc;
typedef vector<vs> vvs;
typedef vector<vll> vvll;
typedef pair<int, int> P;
typedef map<int, int> mii;
typedef set<int> si;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define fi first
#define se second
// #include <atcoder/all>
// using namespace atcoder;
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T, class U>
inline bool chmax(T& a, U b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U>
inline bool chmin(T& a, U b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
cout << "(" << p.first << ", " << p.second << ")";
return os;
}
template <class T>
inline void dump(T& v) {
if (v.size() > 100) {
cout << "ARRAY IS TOO LARGE!!!" << endl;
} else {
irep(i, v) { cout << (*i) << ((i == --v.end()) ? '\n' : ' '); }
}
}
template <class T, class U>
inline void dump(map<T, U>& v) {
if (v.size() > 100) {
cout << "ARRAY IS TOO LARGE!!!" << endl;
} else {
irep(i, v) { cout << i->first << " " << i->second << '\n'; }
}
}
template <class T, class U>
inline void dump(pair<T, U>& p) {
cout << p.first << " " << p.second << '\n';
}
inline void yn(const bool b) { b ? fin("yes") : fin("no"); }
inline void Yn(const bool b) { b ? fin("Yes") : fin("No"); }
inline void YN(const bool b) { b ? fin("YES") : fin("NO"); }
const int INF = INT_MAX;
constexpr ll LLINF = 1LL << 61;
constexpr ll MOD = 1000000007; // 998244353; //
constexpr ld EPS = 1e-11;
void Case(int i) { printf("Case #%d: ", i); }
/* -------------------- ここまでテンプレ -------------------- */
int main() {
int n;
cin >> n;
// vi v(n);
// rep(i, n) cin >> v[i];
cout<<n-1<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<vs> vvs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
int main() {
int H, W; cin >> H >> W;
int minA = INF;
vvi As(H, vi(W));
rep(i, H)rep(j, W){
cin >> As[i][j];
minA = min(minA, As[i][j]);
}
int ans = 0;
rep(i, H)rep(j, W){
ans += As[i][j] - minA;
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
template<typename T> void read(T &x) {
x=0;int f=1;
char c=getchar();
for(;!isdigit(c);c=getchar())if(c=='-') f=-1;
for(;isdigit(c);c=getchar())x=x*10+c-'0';
x=x*f;
}
const int mod=1e9+7;
int n,p;
/*
(p-1)(p-2)^(n-1)
*/
int qpow(int a,int b){
int res=1;
while(b){
if(b&1) res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res%mod;
}
signed main(){
cin>>n>>p;
cout<<qpow(p-2,n-1)%mod*(p-1)%mod<<endl;
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cstring>
#include<vector>
#include<queue>
#define ll long long
const int N=2e5+10;
const ll mod=1e9+7;
ll read(){
ll s = 0, f = 1; char ch = getchar();
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)) s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar();
return s * f;
}
using namespace std;
struct edge{
int u,v;
ll time,K;
edge(int u,int v,ll time, ll K){
this->u=u;
this->v=v;
this->time=time;
this->K=K;
}
};
struct node{
int u;
ll time;
bool operator <(const node &a) const{
return time>a.time;
}
node(int u,ll time){
this->u=u;
this->time=time;
}
};
vector<edge> e[N];
bool vis[N];
ll arr[N];
ll dj(int n,int x,int y){
//³õʼ»¯
for(int i=1;i<=n;i++){
arr[i]=-1;
vis[i]=false;
}
arr[x]=0;
priority_queue<node> q;
node t(x,0);
q.push(t);
while(!q.empty()){
t=q.top();
q.pop();
int u=t.u;
if(vis[u]) continue;
vis[u]=true;
for(int i=0;i<e[u].size();i++){
int v=e[u][i].v;
if(vis[v]) continue;
ll ans=(arr[u]+e[u][i].K-1)/e[u][i].K*e[u][i].K+e[u][i].time;
if(ans<arr[v]||arr[v]==-1){
arr[v]=ans;
q.push(node(v,ans));
}
}
}
return arr[y];
}
int main (){
int n,m,x,y;
n=read();
m=read(); x=read(); y=read();
int u,v,time,k;
for(int i=1;i<=m;i++){
u=read();v=read();
time=read();
k=read();
e[u].push_back(edge(u,v,time,k));
e[v].push_back(edge(v,u,time,k));
}
cout<<dj(n,x,y)<<endl;
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long int ll;
typedef pair<int,int> Pai;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
void P(int x) {cout << x << endl;}
void P(long x) {cout << x << endl;}
void P(double x) {cout << x << endl;}
void P(ll x) {cout << x << endl;}
void P(string x) {cout << x << endl;}
void P(char x) {cout << x << endl;}
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define drep(i, n) for(int i = n - 1; i >= 0; i--)
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
struct to {
int b;
ll t;
ll k;
};
struct queIn {
int now;
ll time;
};
int main() {
int n, m, x, y;
cin >> n >> m >> x >> y;
x--; y--;
vector< vector<struct to> > v(n);
rep (i, m) {
int a, b, t, k;
cin >> a >> b >> t >> k;
a--; b--;
struct to tmp;
tmp.b = b;
tmp.t = t;
tmp.k = k;
v[a].push_back(tmp);
tmp.b = a;
v[b].push_back(tmp);
}
// rep (i, n) {
// rep (j, v[i].size()) {
// cout << i << " => " << "b: " << v[i][j].b << ", t: " << v[i][j].t << ", k: " << v[i][j].k << endl;
// }
// }
ll fast[n];
rep (i, n) fast[i] = LINF;
fast[x] = 0;
struct queIn que;
que.now = x;
que.time = 0;
// queue<struct queIn> q;
auto compare = [](struct queIn a, struct queIn b) {
return a.time > b.time;
};
priority_queue<
struct queIn,
vector<struct queIn>,
decltype(compare) // 比較関数オブジェクトを指定
> q {compare};
q.push(que);
while (!q.empty()) {
que = q.top(); q.pop();
// cout << "now: " << que.now << endl;
rep (i, v[que.now].size()) {
ll bai = que.time / v[que.now][i].k;
if (que.time % v[que.now][i].k != 0) {
bai++;
}
// cout << que.now << " => " << "b: " << v[que.now][i].b << ", t: " << v[que.now][i].t << ", k: " << v[que.now][i].k << endl;
if (fast[v[que.now][i].b] > bai * v[que.now][i].k + v[que.now][i].t) {
fast[v[que.now][i].b] = bai * v[que.now][i].k + v[que.now][i].t;
struct queIn qTmp;
qTmp.now = v[que.now][i].b;
qTmp.time = bai * v[que.now][i].k + v[que.now][i].t;
q.push(qTmp);
}
}
}
// rep (i, n) {
// cout << i << ": " << fast[i] << endl;
// }
if (fast[y] == LINF) {
P(-1);
} else {
P(fast[y]);
}
return 0;
} |
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using namespace std::chrono;
#define ll long long
#define ld long double
#define sz(c) ((ll)c.size())
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int ll
const ll inf = 1e18;
const int mod = 1e9 + 7;
const int mod2 = 998244353;
high_resolution_clock::time_point curTime() { return high_resolution_clock::now(); }
#define rep(i,n) for(int i=0;i<n;i++)
signed main()
{
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
auto startTime = curTime();
int n;
cin>>n;
if(n==0)
{
cout<<"Yes"<<endl;
return 0;
}
while(n%10==0) n/=10;
vector<char> s;
while(n)
{
s.push_back(n%10);
n/=10;
}
reverse(s.begin(), s.end());
bool f =true;
for(int i=0;i<sz(s)/2;i++)
{
if(s[i]!=s[sz(s)-i-1]) f=false;
}
cout<<(f? "Yes" : "No")<<endl;
auto stopTime = curTime();
auto duration = duration_cast<microseconds>(stopTime - startTime);
//cout<<"Program ran for "<<(ld)duration.count()/1e6<<" "<<"seconds"<<endl;
} | #include<bits/stdc++.h>
using namespace std;
long long dp[15][205];
int L;
int main(){
scanf("%d",&L);
dp[0][0] = 1;
for(int i=1;i<=12;i++)
for(int j=1;j<=L;j++){
for(int k=L;k>=max(j,i);k--){
dp[i][k] += dp[i-1][k-j];
}
}
printf("%lld\n",dp[12][L]);
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll p=998244353;
ll n,m,ans,jc[500010],ny[500010];
ll ksm(ll x,ll y){
ll xlh=1;
while(y){
if(y&1)xlh=xlh*x%p;
x=x*x%p;
y/=2;
}
return xlh;
}
ll C(ll x,ll y){
return jc[x]*ny[y]%p*ny[x-y]%p;
}
int main(){
ll i,j,x,y,xlh;
scanf("%lld%lld",&n,&m);
jc[0]=ny[0]=1;
for(i=1;i<=500000;i++)jc[i]=jc[i-1]*i%p,ny[i]=ksm(jc[i],p-2);
for(i=1;i<=m;i++){
x=i;
xlh=1;
for(j=2;j*j<=x;j++)if(x%j==0){
//printf("%lld %lld\n",i,j);
y=0;
while(x%j==0)x/=j,y++;
xlh=xlh*C(y-1+n,n-1)%p;
}
if(x>1)xlh=xlh*n%p;
ans=(ans+xlh)%p;
}
printf("%lld",ans);
} | #include<string>
#include<iostream>
#include <algorithm>
#include<vector>
#include<functional>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <numeric>
#include <math.h>
#define ll long long
#define PI 3.14159265358979323846
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
#define rep1(x,n) for(ll x=1;x<(ll)n+1;x++)
using namespace std;
int g1(ll g1) {
vector<ll> a;
bool sw = true;
ll l1 = 1;
while (sw) {
if (g1 > 9) {
a.push_back(g1 % 10);
g1 /= 10;
l1++;
}
else {
a.push_back(g1);
sw = false;
}
}
sort(a.begin(), a.end());
ll gg1 = 0;
ll b = 1;
rep(j, l1) {
gg1 += a.at(j) * b;
b *= 10;
}
return gg1;
}
int g2(ll g2) {
vector<ll> a;
bool sw = true;
ll l1 = 1;
while (sw) {
if (g2 > 9) {
a.push_back(g2 % 10);
g2 /= 10;
l1++;
}
else {
a.push_back(g2);
sw = false;
}
}
sort(a.begin(), a.end(),greater<ll>());
ll gg2 = 0;
ll b = 1;
rep(j, l1) {
gg2 += a.at(j) * b;
b *= 10;
}
return gg2;
}
int main() {
float A, B;
cin >> A >> B;
float ans = ((A - B)*100 / A);
cout << ans << endl;
}
|
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
//#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
char cn[1200010];
char* ci = cn, * di = cn, ct;
char tc[200002], *d1 = tc + 100000, *d2 = tc + 200000;
const char d[2] = "0";
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
rep(i, 100000) tc[i] = '0';
rep(i, 100000) d1[i] = '1';
d2[0] = '0';
d2[1] = '\n';
fread(cn, 1, 1200010, stdin);
int T = 0;
while ((ct = *ci++) >= '0') T = T * 10 + ct - '0';
rep(t, T) {
int N = 0;
while ((ct = *ci++) >= '0') N = N * 10 + ct - '0';
memcpy(di, d1 - N, N);
di += N;
memcpy(di, d2 - N, N + 2);
di += N + 2;
ci += 6 * N + 3;
}
fwrite(cn, 1, di - cn, stdout);
Would you please return 0;
} | #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,n) repi(i,0,n)
#define repi(i,a,n) for(ll i=a;i<(ll)n;++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define Show(val) cout<<(val)<<" "
#define Showln(val) cout<<(val)<<endl
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
// using P = pair<int, int>;
void YN(bool a) { cout << (a ? "Yes" : "No"); }
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; }
void solve()
{
ll t;
cin >> t;
rep(tt,t){
ll n;
string s;
cin >> n >> s>>s>>s;
s = string(n, '0')+string(n,'1')+'0';
Showln(s);
}
}
int main()
{
fastio;
solve();
return 0;
} |
// Problem: E - Train
// Contest: AtCoder - AtCoder Beginner Contest 192
// URL: https://atcoder.jp/contests/abc192/tasks/abc192_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define mod 1000000007
// templates
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define po(x, y) fixed << setprecision(y) << x
#define ss(s) scanf(" %[^\n]%*c", s)
#define sc(n) scanf("%d", &n)
#define sl(n) scanf("%lld", &n)
#define ps(s) printf("%s\n", s)
#define pr(n) printf("%d\n", n)
#define pl(n) printf("%lld\n", n)
#define prs(n) printf("%d ", n)
#define pls(n) printf("%lld ", n)
using namespace std;
const ll inf = (ll)1e16 + 10;
const int N = (int)1e5 + 10;
int a[N], b[N];
vector<array<ll, 3>> adj[N]; // u->v,t,k
vector<ll> dist(N);
ll n, m, x, y;
void bfs() {
set<array<ll, 2>> s; // time,node
s.insert({0, x});
for (int i = 0; i <= n; i++) {
dist[i] = inf;
}
dist[x] = 0;
while (sz(s)) {
auto temp = *s.begin();
s.erase(s.begin());
ll node = temp[1], kete = temp[0];
for (auto c : adj[node]) {
ll add = 0;
if (kete < c[2] && kete != 0) {
add = (c[2] - kete);
} else if (kete != 0) {
if (kete % c[2]) {
add = c[2] - (kete % c[2]);
}
}
ll curr = c[1] + add + kete;
// printf("node-> %lld curr-> %lld\n", node, curr);
if (dist[c[0]] > curr) {
s.erase({dist[c[0]], c[0]});
dist[c[0]] = curr;
s.insert({curr, c[0]});
}
}
}
if (dist[y] == inf) {
dist[y] = -1;
}
pl(dist[y]);
}
void solve() {
sl(n), sl(m), sl(x), sl(y);
for (int i = 1; i <= m; i++) {
ll u, v, t, k;
sl(u), sl(v), sl(t), sl(k);
adj[u].push_back({v, t, k});
adj[v].push_back({u, t, k});
}
bfs();
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t = 1;
// sc(t);
while (t--) {
solve();
}
// cerr << (float)clock() / CLOCKS_PER_SEC * 1000 << " ms" << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define All(a) a.rbegin(), a.rend()
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.precision(10), cout << fixed
const int N = 2e5 + 5;
ll cost[N], n, m;
ll inf = 1e18;
struct dt {
int u, cost, k;
dt(int u, int cost, int k) {
this->u = u;
this->cost = cost;
this->k = k;
}
};
vector<dt> g[N];
void dijk(ll u) {
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
for (int i = 0; i <= n; ++i)
cost[i] = inf;
cost[u] = 0;
q.push({0, u});
while (!q.empty()) {
ll src = q.top().ss, cst = q.top().ff;
q.pop();
for (int i = 0; i < g[src].size(); ++i) {
ll curr = g[src][i].u;
ll tmp = (ll)ceil(cst / (double)g[src][i].k) * g[src][i].k + g[src][i].cost;
if (tmp < cost[curr]) {
cost[curr] = tmp;
q.push({cost[curr], curr});
}
}
}
}
int main() {
ios;
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int x, y;
cin >> n >> m >> x >> y;
x--;
y--;
for (int i = 0, a, b, c, d; i < m; ++i) {
cin >> a >> b >> c >> d;
a--;
b--;
g[a].push_back(dt(b, c, d));
g[b].push_back(dt(a, c, d));
}
dijk(x);
cout << (cost[y] == inf ? -1 : cost[y]) << '\n';
return 0;
}
|
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,k,mod,buf[2000005],buf2[2000005],*f=buf+1000000,*g=buf2+1000000;
int val=500000,tans[100005];
inline int addmod(int x)
{
return x>=mod?x-mod:x;
}
inline int submod(int x)
{
return x<0?x+mod:x;
}
int fpow(int x,int y)
{
int ans=1;
while(y)
{
if(y&1) ans=1ll*ans*x%mod;
x=1ll*x*x%mod;
y/=2;
}
return ans;
}
void ins(int x)
{
for(int i=-val;i<=val;i++)
{
g[i]=addmod(g[i-x]+f[i]);
f[i]=addmod(f[i]+submod(g[i-x]-g[i-(k+1)*x]));
}
}
void ins2(int x)
{
for(int i=val;i>=-val;i--)
{
g[i]=addmod(g[i-x]+f[i]);
f[i]=addmod(f[i]+submod(g[i-x]-g[i-(k+1)*x]));
}
}
void del(int x)
{
for(int i=-val;i<=val;i++)
{
f[i]=submod(f[i]-submod(g[i-x]-g[i-(k+1)*x]));
g[i]=addmod(g[i-x]+f[i]);
}
}
int main()
{
scanf("%d%d%d",&n,&k,&mod);
f[0]=1;
for(int i=1;i<n;i++)
ins(i);
tans[1]=submod(1ll*f[0]*(k+1)%mod-1);
for(int i=2;i<=n;i++)
{
del(n-i+1);
/* for(int j=-val;j<=val;j++)
if(f[j])
{
printf("j=%d,f=%d\n",j,f[j]);
}*/
ins2(-i+1);
/* printf("i=%d\n",i);
for(int j=-val;j<=val;j++)
if(f[j])
{
printf("j=%d,f=%d\n",j,f[j]);
}*/
tans[i]=submod(1ll*f[0]*(k+1)%mod-1);
}
for(int i=1;i<=n;i++)
printf("%d\n",tans[i]);
return 0;
} | #include<iostream>
#include<cstdio>
using namespace std;
long long n,k,sum,f[101][1000001],mod,ans;
int main(){
scanf("%lld%lld%lld",&n,&k,&mod);
sum=((n*(n-1ll))/2ll)*k;
f[0ll][0ll]=1ll;
for(long long i=1ll;i<=n;i++){
for(long long j=0ll;j<=sum;j++){
f[i][j]=f[i-1ll][j];
if(j>=i)f[i][j]=(f[i][j]+f[i][j-i])%mod;
if(j>=(k+1ll)*i)f[i][j]=(f[i][j]-f[i-1][j-(k+1)*i]+mod)%mod;
}
}
for(long long i=1ll;i<=n;i++){
ans=0ll;
for(long long j=0ll;j<=sum;j++)ans=(ans+f[i-1ll][j]*f[n-i][j])%mod;
ans=ans*(k+1ll)%mod;
ans--;
printf("%lld\n",ans);
}
} |
/*
may God of speed-forces be with us !!
--problem isnt, life is empty;
--problem is, life is full of emptiness;
a non-strugling pupil coder !!
*/
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define F first
#define S second
//#define biGinf 9e+18
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define inpv(v) for(auto &x:v) cin>>x
#define otpv(v) for(auto &x:v) cout<<x<<nwl
#define otpv2(v) for(auto &x:v) cout<<x<<" "
#define CASES int tt;cin>>tt; while(tt--)
#define for0(i,n) for(int i=0; i<n; i++)
#define for1(i,n) for(int i=1; i<=n; i++)
#define forr(i,r,n) for(int i=r; i<=n; i++)
#define forj(j,n) for(int j=n; j>=0; j--)
#define forx(x,v) for(auto &x:v)
#define sz(v) (int)(v).size()
#define rz(n) resize(n)
#define vec(x) vector < x >
#define vvim(n,m,v) vvi v(n, vi(m))
using int64 = long long;
using vi = vector < int >;
using vi64 = vector < int64 >;
using mii = map <int , int >;
using vvi = vector < vi >;
using pii = pair < int , int >;
using vpii = vector < pii >;
const int inf = 2e9 + 7;
const char nwl = '\n';
const int mxx = 1e6 + 3;
int64 sumV(vi &v) {
int64 s = 0;
for (int x : v) {
s += (int64)x;
}
return s;
}
int gcd(int a, int b) {
if (!b) return a;
return gcd(b, a % b);
}
bool powerOf2(int n) {
if (n & (n - 1)) return 0;
return 1;
}
//const int64 biGinf = 9e+18;
//const string yo = "Yes\n", no = "No\n";
int64 ceil2(int64 a, int64 b) {
int64 d = a / b;
if (a % b ) d += 1;
return d;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ur code
int64 n, m;
cin >> n >> m;
vi64 v(m);
inpv(v);
sort(all(v));
int64 ans = 0;
if (n == m) {
ans = 0;
} else if (!m) {
ans = 1;
} else {
int64 c = 0, p = n;
for0(i, m) {
int64 z = v[i] - c - 1ll;
if (z > 0)
p = min(p, z);
c = v[i];
}
int64 z = n - v[m - 1];
if (z > 0) {
p = min(p, z);
}
c = 0;
for0(i, m) {
int64 x = ceil2((v[i] - c - 1ll) , p);
c = v[i];
ans += x;
}
int64 x = ceil2((n - c), p);
ans += x;
}
cout << ans << nwl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < (n); ++i)
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int main(){
int n, m; cin >> n >> m;
vector<int> a(m+2);
rep(i,m) cin >> a[i+1];
a.back() = n+1;
sort(a.begin(), a.end());
int k = n;
rep(i,m+1){
int x = a[i+1] - a[i] - 1;
if(x > 0) chmin(k, x);
}
int ans = 0;
rep(i,m+1){
int x = a[i+1] - a[i] - 1;
if(x > 0) ans += (x + k-1)/k;
}
cout << ans << endl;
} |
/*Created By:---- Dibas Behera*/
#include<bits/stdc++.h>
using namespace std;
#define db(x) cout<<x<<'\n'
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define repi(i,n) for(int i=0;i<(n);++i)
//#define repA(i,a,n) for(int i=a;i<=(n);++i)
//#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define ll long long int
#define endl '\n'
#define lld long long double
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int> >
#define err(t) cout<<"Error : "<<t<<endl;
#define all(v) v.begin(),v.end()
#define mp make_pair
#define M 100000
#define MD 1000000007 //998244353
#define pb push_back
#define rep(i,a,b) for(ll i = a; i <= (b); i++)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vec(a) vector<a >
#define se second
#define fi first
#define inf 0xffffffff
#define inchar getchar_unlocked
#define outchar(x) putchar_unlocked(x)
#define goo(x) cout<<"Case #"<<(x)<<": ";
//template <typename T>
//using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll binpow(ll a,ll b){ if(a==0) return 0; if(b==0) return 1;ll ans=1;while(b>0){if(b&1) ans=ans*a; a=a*a; b=b>>1;} return ans;}
ll binpowM(ll a,ll b,ll m=MD){ if(a==0) return 0; if(b==0) return 1;ll ans=1;while(b>0){if(b&1) ans=ans*a;ans%=m; a=a*a;a%=m; b=b>>1;} return ans;}
template <typename T>
T Min(T a,T b){ return a<b?a:b; }
template <typename T>
T Max(T a,T b){ return a>b?a:b; }
//-------------------------------------------
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
// int n;cin>>n;
// string s;cin>>s;
ll n;cin>>n; vl a(n); repi(i,n) cin>>a[i];
ll ans=0;
repi(i,n){
if(a[i]>10) ans+=a[i]-10;
}
db(ans);
/*
int test1;cin>>test1;while(test1--){
}
//*/
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// clang-format off
#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 debug(x) cerr << "(" << __LINE__ << ") " << #x << ": " << (x) << endl;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << ' ' << p.second; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &v) { for ( pair<T1, T2> x : v ) { os << x << (x.first == v.rbegin()->first && x.second == v.rbegin()->second ? "" : "\n"); } return os; }
template <typename T> ostream &operator<<(ostream &os, queue<T> v) { if(!v.empty()) { os << v.front(); v.pop(); } while (!v.empty()) { os << " " << v.front(); v.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, stack<T> v) { if(!v.empty()) { os << v.top(); v.pop(); } while (!v.empty()) { os << " " << v.top(); v.pop(); } return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { bool is_f = true; for ( T x : v ) { os << (is_f ? "" : " ") << x; is_f = false; } return os; }
using ull = unsigned long long;
using ll = long long;
using Pll = pair<ll, ll>;
using P = pair<int, int>;
constexpr ll INF64 = INT64_MAX / 2;
constexpr int INF32 = INT32_MAX / 2;
constexpr int dy[] = { 0, -1, 1, 0, -1, 1, -1, 1 };
constexpr int dx[] = { -1, 0, 0, 1, -1, -1, 1, 1 };
constexpr int mod998244353 = 998244353;
constexpr int mod1000000007 = (int)1e9 + 7;
constexpr char newl = '\n';
// clang-format on
template <typename T>
struct CumulativeSum {
vector<T> sum;
CumulativeSum(const vector<T> &a) {
int n = a.size();
sum.assign(n + 1, 0);
for ( int i = 0; i < n; i++ )
sum[i + 1] = a[i] + sum[i];
}
T query(int l, int r) { return sum[r] - sum[l]; }
};
template <typename T1, typename T2>
T1 Binomial(T1 n, T2 r) {
T1 res = 1;
for ( T1 i = 0; i < r; i++ ) {
res *= n - i;
res /= i + 1;
}
return res;
}
int main() {
int n;
cin >> n;
vector<ll> a(n);
for ( auto &A : a )
cin >> A;
range(i, 0, n) {
if ( i % 2 == 0 ) a[i] *= -1;
}
CumulativeSum<ll> sum(a);
map<ll, ll> cntmp;
for ( auto x : sum.sum ) {
cntmp[x]++;
}
ll ans = 0;
for ( auto [val, cnt] : cntmp ) {
ans += Binomial(cnt, 2LL);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int read()
{
int x , f = 1;
char ch;
while( ( ch = getchar() ) < '0' || ch > '9' )
if( ch == '-' ) f = -1;
x = ch ^ 48;
while( ( ch = getchar() ) >= '0' && ch <= '9' )
x = ( x << 3 ) + ( x << 1 ) + ( ch ^ 48 );
return x * f;
}
int a , b , c;
int main()
{
a = read() , b = read() , c = read();
if( ( c & 1 ) == 0 )
a = abs( a ) , b = abs( b );
putchar( a == b ? '=' : ( a < b ? '<' : '>' ) );
return 0;
} | #include <bits/stdc++.h>
#include <unordered_map>
#define fast_io \
ios::sync_with_stdio(false); \
cin.tie(0)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int mod = 998244353;
string helper(string s) {
if (s.size() % 2 == 1) s += s;
string ret;
for (int i = 0; i < s.size(); i += 2) {
if (s[i] == s[i + 1]) { ret += s[i]; }
else {
if ((s[i] == 'R'&&s[i + 1] == 'S') || (s[i + 1] == 'R'&&s[i] == 'S')) { ret += 'R'; }
if ((s[i] == 'R'&&s[i + 1] == 'P') || (s[i + 1] == 'R'&&s[i] == 'P')) { ret += 'P'; }
if ((s[i] == 'P'&&s[i + 1] == 'S') || (s[i + 1] == 'P'&&s[i] == 'S')) { ret += 'S'; }
}
}
return ret;
}
int main() {
int n,k;
string s;
cin >> n>> k;
cin >> s;
char ans;
for (int i = 0; i < k; i++) {
s = helper(s);
}
ans = s[0];
cout << ans << endl;
return 0;
} |
/*
_______ _______ _______
/ _____ \ / _____ \ / _____ \
/ / \_\ _ __ _ / / \ \ _ __ _ / / \_\
| | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | __ | | | | | | | | | |
| | __ \ \ | | / / | | \ \| | \ \ | | / / | | __
\ \_____/ / \ \/ /\ \/ / \ \_____\ / \ \/ /\/ / \ \_____/ /
\_______/ \___/ \___/ \______/\__\ \___/ \___/ \_______/
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include<queue>
using namespace std;
template <class T>
void Read(T &x)
{
x = 0; int p = 0; char st = getchar();
while (st < '0' || st > '9') p = (st == '-'), st = getchar();
while (st >= '0' && st <= '9') x = (x << 1) + (x << 3) + st - '0', st = getchar();
x = p ? -x : x;
return;
}
template <class T>
void Print(T x)
{
if (x > 9) Print(x / 10);
putchar(x % 10 + '0');
return;
}
const int N=5e5;
priority_queue<int>qod,qev;
int a[N],b[N],c[N],n;
int main()
{
scanf("%d",&n);long long ans=0;
for(int i=1;i<=n;i++)scanf("%d",a+i),ans+=a[i];
for(int i=1;i<=n;i++)scanf("%d",b+i),c[i]=b[i]-a[i];
long long s=ans;
for(int i=1;i<=n;i+=2)qod.push(c[i]);
for(int i=2;i<=n;i+=2)qev.push(c[i]);
for(int i=1;i<=(n>>1);i++)
{
s+=qod.top()+qev.top();qod.pop(),qev.pop();
ans=max(ans,s);
}
cout<<ans<<endl;
return 0;
}
| /*---------changeable---------*/
const int big=1000000007;
//const int big=998244353;
const double EPS=1e-8; //適宜変える
//#include <atcoder/all>
//using namespace atcoder;
#pragma region template
#ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)
/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
constexpr operator int(){return -int(1e9)-10;}
constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
constexpr operator int(){return int(1e9)+10;}
constexpr operator ll(){return ll(1e18)+10;}
constexpr auto operator -(){return neginf;}
}inf;
/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif
/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}
#pragma endregion template
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.precision(10);
/*------------------------------------*/
int n;
cin >> n;
vector<int> a(n),b(n);
rep(i,n)cin >> a[i];
rep(i,n)cin >> b[i];
cout << max(0,min(b)-max(a)+1) << newl;
} |
/* Sat-Chitta-Ananda */
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define inf (int)1e9
#define f first
#define s second
int readInt() { int x; cin >> x; return x; }
char readChar() { char x; cin >> x; return x; }
int readDouble() { double x; cin >> x; return x; }
string readStr() { string x; cin >> x; return x; }
int Dijkstra(const vector<vector<pair<int,int>>> &g, const int &n, int sv) {
vector<int> d(n, inf);
d[sv] = 0;
int ans = inf;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> Queue;
Queue.push({d[sv], sv});
while(!Queue.empty()) {
int node = Queue.top().s;
int weight = Queue.top().f;
Queue.pop();
if(weight > d[node]) continue;
for(auto itr : g[node]) {
int distance = itr.f;
int neighbor = itr.s;
// itself
if(neighbor == sv) {
ans = min(ans, d[node] + distance);
}
else {
if(d[neighbor] > d[node] + distance) {
d[neighbor] = d[node] + distance;
Queue.push({d[neighbor], neighbor});
}
}
}
}
if(ans >= inf) ans = -1;
return ans;
}
int32_t main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n = readInt();
int m = readInt();
vector<vector<pair<int,int>>> g(n);
for(int i = 0 ; i < m ; ++i) {
int a = readInt();
int b = readInt();
int c = readInt();
g[a-1].push_back({c, b-1});
}
for(int i = 0 ; i < n ; ++i) {
cout << Dijkstra(g, n, i) << '\n';
}
return 0;
} | #include <bits/stdc++.h>
//#include <atcoder/modint>
//#include <atcoder/math>
//#include <boost/multiprecision/cpp_int.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPP(i, n) for(int i = 1; i <= n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
//const double PI = acos(-1);
//const double EPS = 1e-15;
//long long INF=(long long)1E17;
#define i_7 (long long)(1e9+7)
//#define i_7 998'244'353
long mod(long a){
long long c = a % i_7;
if(c >= 0) return c;
return c + i_7;
}
long long po(long a, long b){
if(b == 0) return 1;
long long z = po(a , b / 2);
z = mod(z * z);
if(b % 2 != 0) z = mod(a * z);
return z;
}
bool prime_(int n){
if(n == 1){
return false;
}else if(n == 2){
return true;
}else{
for(int i = 2; i <= std::sqrt(n); i++) if(n % i == 0) return false;
return true;
}
}
long long gcd_(long long a, long long b){
if(a < b) std::swap(a,b);
if(a % b == 0) return b;
else return gcd_(b, a % b);
}
long long lcm_(long long x, long long y){
return (x / gcd_(x,y)) * y;
}
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//using mint = modint998244353;
//using namespace boost::multiprecision;
//using namespace __gnu_pbds;
typedef pair<int, int> P;
unordered_map<int, int> G[2010];
int main(){
//using namespace std;
int n, m;
cin>>n>>m;
int a[m], b[m], c[m];
REP(i, m){
cin>>a[i]>>b[i]>>c[i];
a[i]--; b[i]--;
}
REP(i, m){
if(G[a[i]].find(b[i]) == G[a[i]].end()){
G[a[i]][b[i]] = c[i];
continue;
}
G[a[i]][b[i]] = min(G[a[i]][b[i]], c[i]);
}
vector<int> ans(n, -1);
REP(st, n){
//cout << "st: " << st << endl;
vector<int> dist(n, -1);
dist[st] = 0;
priority_queue<P, vector<P>, greater<P>> pq;
pq.emplace(0, st);
while(!pq.empty()){
int now = pq.top().second;
int d = pq.top().first;
//cout << "now: " << now << "d: " << d << endl;
pq.pop();
if(dist[now] > 0 && dist[now] < d) continue;
if(now == st && d > 0) continue;
//if(now == st && d > 0) break;
for(auto& [nxt, cost]: G[now]){
if(nxt != st && nxt == now) continue;
if(dist[nxt] > 0 && dist[nxt] <= d + cost) continue;
dist[nxt] = d + cost;
pq.emplace(dist[nxt], nxt);
}
}
ans[st] = dist[st] == 0 ? -1 : dist[st];
}
REP(i, n) cout << ans[i] << endl;
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
bool v_cmp(pair<int, vector<int>> &p, pair<int, vector<int>> &q) {
return p.first < q.first;
}
// sort(v.begin(), v.end(), v_cmp); // small to large
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
A[i] %= 200;
}
vector<pair<int, vector<int>>> pairs;
for (int bits = 0; bits < (1 << min(N, 8)); bits++) {
bitset<8> bs(bits);
int tmp = 0;
vector<int> s;
for (int i = 0; i < min(N, 8); i++) {
if (bs[i]) {
tmp += A[i];
tmp %= 200;
s.push_back(i + 1);
}
}
if (s.size() != 0) pairs.push_back(make_pair(tmp, s));
}
sort(pairs.begin(), pairs.end(), v_cmp);
for (int i = 0; i < pairs.size() - 1; i++) {
if (pairs[i].first == pairs[i + 1].first) {
cout << "Yes" << endl;
cout << pairs[i].second.size();
for (int o : pairs[i].second) cout << " " << o;
cout << endl;
cout << pairs[i + 1].second.size();
for (int o : pairs[i + 1].second) cout << " " << o;
cout << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;
ll mod = 1000000007;
int main() {
ll N; cin >> N;
ll dp[N+1][200]; ll A[N];
rep(i,200){
rep(j,N+1) dp[j][i] = 0;
}
dp[0][0] = 1; ll k,l; bool found = false;
rep(i,N){
ll a; cin >> a;
A[i] = a%200;
rep(j,200){
if(!found&&j!=0&&dp[i][j]>=2){
k = i; l = j; found = true;
}
dp[i+1][j] += dp[i][j];
dp[i+1][(j+A[i])%200] += dp[i][j];
}
}
rep(j,200){
if(!found&&j!=0&&dp[N][j]>=2){
k = N; l = j; found = true;
}
}
if(!found){
if(dp[N][0]>=3){
cout << "Yes" << endl;
ll s=0,t=0;
rep(i,N){
if(s==0&&A[i]%200==0){
s = i+1;
}
else if(A[i]%200==0) t = i+1;
}
cout << 1 << " " << s << endl;
cout << 1 << " " << t << endl;
return 0;
}
cout << "No" << endl;
}
else{
cout << "Yes" << endl;
vector<int> aa,ab;
ll res = l;
for(int i=k;i>=1;i--){
if(dp[i-1][(res+200-A[i-1])%200]>=1){
res = (res+200-A[i-1])%200; aa.push_back(i);
}
}
res = l;
for(int i=k;i>=1;i--){
if(dp[i-1][res]>=1){
continue;
}
else{
res = (res+200-A[i-1])%200; ab.push_back(i);
}
}
reverse(aa.begin(),aa.end()); reverse(ab.begin(),ab.end());
cout << aa.size() << " ";
for(int x:aa) cout << x << " ";
cout << endl;
cout << ab.size() << " ";
for(int x:ab) cout << x << " ";
cout << endl;
}
//cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
const int N=101;
const ll mod=1e9+7;
int n;
bool a[N][N];
int main(){
ios::sync_with_stdio(false);
cin >> n;
for(int i=1; i<=n ;i++){
for(int j=1; j<=n ;j++){
char c;cin >> c;
if(c=='1') a[i][j]=1;
}
a[i][i]=1;
}
for(int k=1; k<=n ;k++){
for(int i=1; i<=n ;i++){
for(int j=1; j<=n ;j++){
a[i][j]|=a[i][k]&a[k][j];
}
}
}
double ans=0;
for(int i=1; i<=n ;i++){
int cnt=0;
for(int j=1; j<=n ;j++) cnt+=a[j][i];
ans+=1.0/(cnt);
}
cout << fixed << setprecision(15) << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main(){
ll s,d,m,n,x;
s=d=0;
m=0;
cin >> n;
while(n--)
{
cin >> x;
s+=abs(x);
d+=x*x;
m=max(m,abs(x));
}
cout << s << '\n';
cout << setprecision(15) << fixed << (double)sqrt(d) << '\n';
cout << m;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
int charToInt(char c){
char zero_num = '0';
return (int)c - (int)zero_num;
}
signed main(){
int M, H; cin>>M>>H;
if(H%M==0){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
return 0;
} | #include <bits/stdc++.h>
#include <iomanip>
#define ll long long
#define lli long long int
#define ms0(x) memset(x, 0, sizeof(x))
using namespace std;
long long MAXN = 1e9;
long double pi = 3.1415926536;
long long mod = 1e9;
bool secgreat(pair<pair<lli, lli>, lli> a, pair<pair<lli, lli>, lli> b)
{
pair<lli, lli> x, y;
x = a.first;
y = b.first;
return (x.first > y.first) || (x.first == y.first && x.second > y.second);
}
bool secgreat2(pair<pair<lli, lli>, lli> a, pair<pair<lli, lli>, lli> b)
{
pair<lli, lli> x, y;
x = a.first;
y = b.first;
return (x.first > y.second) || (x.first == y.second && x.second > y.first);
}
/*
long long fact(long long n){
n%=mod;
if(n==0){
return 1;
}
else if(n == 1){
return 1;
}
else{
return ((n%mod)*(fact(n-1)%mod))%mod;
}
}
long long power(long long n, long long pow){
n%=mod;
long long res = 1;
while(pow){
if(pow%2){
res = ((res%mod)*(n%mod))%mod;
}
pow/=2;
n= ((n%mod)*(n%mod))%mod;
}
res%=mod;
return res;
}
long long inversemod(long long n){
return power(n, mod-2)%mod;
}
long long ncr(long long a){
long long z = ((fact(a)%mod)(((inversemod(fact((a/2)%mod)%mod)%mod)(inversemod(fact((a/2)%mod)%mod)%mod)%mod)%mod)%mod)%mod;
return z;
}
*/
/*
struct node{
long long key;
node* left;
node* right;
};
struct node* newnode(long long item){
struct node* temp = new struct node;
temp->key = item;
temp->left = NULL;
temp->right = NULL;
return temp;
};
struct node* insert(struct node* root, long long key){
if(root == NULL){
return newnode(key);
}
if (key<root->key){
root->left = insert(root->left, key);
}
else if(key>root->key){
root->right = insert(root->right, key);
}
};
*/
/*
string binary(int a){
string s;
int x = 9;
while(x>0){
s.push_back(48+a%2);
a/=2;
x--;
}
reverse(s.begin(), s.end());
return s;
}
int strtoi(string s){
int n = s.length();
int i;
int sum = 0;
for(i = 0; i<n; i++){
sum+=pow(2, i)*s[i];
}
return sum;
}
*/
/*
long long nCrModp(long long n, long long r, long long p)
{
if (r > n - r)
r = n - r;
long long C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (long long i = 1; i <= n; i++) {
for (int j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
*/
// int dfs_far(vector<bool> visited, int v, vector<int> node[])
// {
// visited[v - 1] = true;
// if (int(node[v - 1].size()) == 1)
// {
// return v;
// }
// int x;
// for (auto i : node[v - 1])
// {
// if (visited[i - 1] == false)
// {
// x = dfs_far(visited, i, node);
// break;
// }
// }
// return x;
// }
// int dfs_dia(vector<bool> visited, int v, vector<int> node[], int dist)
// {
// visited[v - 1] = true;
// //cout << "Correct answer should be " << node[v].size() << endl;
// if (node[v - 1].size() == 1 && dist != 0)
// {
// return dist;
// }
// int x;
// for (auto i : node[v - 1])
// {
// if (visited[i - 1] == false)
// {
// dist++;
// //cout << i << " dist -> " << dist << endl;
// x = dfs_dia(visited, i, node, dist);
// break;
// }
// }
// return x;
// }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
lli n;
long long int x, y, t;
cin >> n >> t;
t *= 100;
lli i;
lli sum = 0;
for (i = 0; i < n; i++)
{
cin >> x >> y;
sum += x * y;
if (sum > t)
{
cout << i + 1;
return 0;
}
}
cout << "-1";
}
|
#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 r, c;
cin >> r >> c;
vector<vector<int>> a(r, vector<int>(c-1));
vector<vector<int>> b(r-1, vector<int>(c));
rep(i,r) rep(j,c-1) cin >> a[i][j];
rep(i,r-1) rep(j,c) cin >> b[i][j];
priority_queue<P, vector<P>, greater<P>> q;
vector<int> dist(r*c*2, INF);
auto push = [&](int i, int j, int k, int x) {
int v = i*c*2 + j*2 + k;
if (dist[v] <= x) return;
dist[v] = x;
q.emplace(x, v);
};
push(0,0,0,0);
while (!q.empty()) {
auto [x, v] = q.top(); q.pop();
if (dist[v] != x) continue;
int i = v/(c*2);
int j = v/2%c;
int k = v%2;
if (k==0) {
if (j+1 < c) push(i, j+1, k, x+a[i][j]);
if (j-1 >=0) push(i, j-1, k, x+a[i][j-1]);
if (i+1 < r) push(i+1, j, k, x+b[i][j]);
push(i, j, 1, x+1);
} else {
if (i-1 >=0) push(i-1, j, k, x+1);
push(i, j, 0, x);
}
}
int ans = dist[(r*c-1)*2];
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
struct PQItem{
int d,x,y;
bool jump;
bool operator<(const PQItem& other)const{
return d>other.d;
}
};
int main() {
int r,c;
cin>>r>>c;
vector<vector<int>> a(r,vector<int>(c-1)),b(r-1,vector<int>(c));
for(auto &row:a)
for(int &x:row)
cin>>x;
for(auto &row:b)
for(int &x:row)
cin>>x;
vector<vector<array<int,2>>> best(r,vector<array<int,2>>(c,{INT_MAX,INT_MAX}));
priority_queue<PQItem> pq;
pq.push({0,0,0,false});
while(true){
PQItem it=pq.top();
pq.pop();
if(best[it.x][it.y][it.jump]<it.d)continue;
if(it.x==r-1&&it.y==c-1){
cout<<it.d<<endl;
break;
}
if(it.y<c-1&&best[it.x][it.y+1][false]>it.d+a[it.x][it.y]){
best[it.x][it.y+1][false]=it.d+a[it.x][it.y];
pq.push({it.d+a[it.x][it.y],it.x,it.y+1,false});
}
if(it.y>0&&best[it.x][it.y-1][false]>it.d+a[it.x][it.y-1]){
best[it.x][it.y-1][false]=it.d+a[it.x][it.y-1];
pq.push({it.d+a[it.x][it.y-1],it.x,it.y-1,false});
}
if(it.x<r-1&&best[it.x+1][it.y][false]>it.d+b[it.x][it.y]){
best[it.x+1][it.y][false]=it.d+b[it.x][it.y];
pq.push({it.d+b[it.x][it.y],it.x+1,it.y,false});
}
if(!it.jump)
for(int i=0;i<it.x;i++)
if(best[i][it.y][true]>it.d+it.x-i+1){
best[i][it.y][true]=it.d+it.x-i+1;
pq.push({it.d+it.x-i+1,i,it.y,true});
}
}
} |
#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;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
#define lpri(n) printf("%lld",n)
#define pri(n) printf("%d",n)
#define pln() printf("\n")
#define priln(n) printf("%d\n",n)
#define lpriln(n) printf("%lld\n",n)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
#define inf INT_MAX
#define linf LLONG_MAX
#define lninf LLONG_MIN
const ll mod = 1e9 + 7;
const int N = 1e6 + 4;
int solve()
{
int n, k; scn(n); scn(k);
if(k == 0)
{
int temp = 1;
while(n--)
{
printf("%d %d\n", temp + 1, temp + 2);
temp += 2;
}
return 0;
}
if(k < 0 or k >= n - 1)
{
pri(-1); return 0;
}
k++;
int l = 1, r = (int)1e8;
printf("%d %d\n", l, r);
n--;
while(k--)
{
n--;
printf("%d %d\n", l + 1, l + 2); l += 2;
}
while(n > 0)
{
n--;
printf("%d %d\n", r + 1, r + 2); r += 2;
}
return 0;
}
//be careful whether to take the input for testcases or not
int main()
{
int t = 1; //scn(t);
while(t--)
{
solve();
}
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
//const ll MAX = 4e5;
//const int MOD = 3;
void fast_io() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
}
//int bf[MAX + 1], bg[MAX + 1];
char colorOnTop(char c0, char c1) {
if (c0 == c1) return c0;
if (c0 == 'W' && c1 == 'B') return 'R';
if (c0 == 'W' && c1 == 'R') return 'B';
if (c0 == 'B' && c1 == 'W') return 'R';
if (c0 == 'B' && c1 == 'R') return 'W';
if (c0 == 'R' && c1 == 'W') return 'B';
if (c0 == 'R' && c1 == 'B') return 'W';
}
char getTopColor(int n, string s) {
if (n == 1) return s[0];
int k = 1;
while (3 * k < n) k *= 3;
string ns = "";
for (int i = 0; i + k < n; i++) ns += colorOnTop(s[i], s[i + k]);
return getTopColor(n - k, ns);
}
int main() {
fast_io();
int n;
string s;
cin >> n >> s;
cout << getTopColor(n, s);
} |
/* *****fireice**** */
#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define vi vector<int>
#define vc vector<char>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define el '\n'
#define N 1000000007
#define M 2147483647
#define MM 1e18;
#define re return
#define F first
#define S second
#define L length
#define deb(x) cout<<#x<<" "<<x<<el;
#define mp make_pair
#define con continue
#define PI 3.14159265
#define all(a) (a).begin(),(a).end()
#define forup(i,n) for(int i=0;i<n;i++)
#define forf(i,x,n) for(int i=x;i<n;i++)
#define ford(i,n) for(int (i)=(n)-1;i>=0;(i)--)
#define take(a,n) forup(i,n) cin>>a[i];
using namespace std;
int gcdExtended(int a, int b, int *x, int *y)
{
// Base Case
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b%a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
ll modInverse(int a, int m)
{
int x, y;
int g = gcdExtended(a, m, &x, &y);
// m is added to handle negative x
ll res = ((ll)x%m + (ll)m) % m;
return res;
}
ll poww(ll x,ll n)
{
if(n==0)
return 1;
else if(n==1) return x;
else if(n%2 == 0) //n is even
return poww(x*x,n/2);
else //n is odd
return x*poww(x*x,(n-1)/2);
}
ll modularExponentiation(ll x,ll n,ll Z)
{
ll result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%Z;
x=(x*x)%Z;
n=n/2;
}
return result;
}
template<typename T>
T maxx(T a,T b)
{
if(a<b) re b;
re a;
}
template<typename T, typename... Args>
T maxx(T a,T b, Args... args)
{
re(maxx(maxx(a,b),args...));
}
template<typename T>
T minn(T a,T b)
{
if(a<b) re a;
re b;
}
template<typename T, typename... Args>
T minn(T a,T b, Args... args)
{
re(minn(minn(a,b),args...));
}
template<typename T>
void read(T& a)
{
cin>>a;
}
template<typename T, typename... Args>
void read(T& a,Args&... args)
{
cin>>a; read(args...);
}
template<typename T>
void write(T a)
{
cout<<a<<' ';
}
template<typename T, typename... Args>
void write(T a,Args... args)
{
cout<<a<<' '; write(args...);
}
inline ll min(ll a,ll b)
{
if(a<b) re a;
else re b;
}
inline ll max(ll a,ll b)
{
if(a<b) re b;
else re a;
}
ll gcd(ll a, ll b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
else return gcd(a, b-a);
}
ll ncr(int n,int r)
{
ll ans=1;
r=min(r,n-r);
forup(i,r)
{
ans=((ans*((n-i)%N))%N*modInverse(i+1,N))%N;
}
re ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
//cin>>t;
while(t--)
{
ll n,m,k;
// cin>>n;
string s;
cin>>s;
string y="ZONe";
m=0;
forup(i,s.L()-3)
{
if(s.substr(i,4)==y) m++;
}
cout<<m<<el;
}
return 0;
}
| #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll mod9 = 998244353;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
void prvec(vector<ll> vec){
ll n = vec.size();
rep(i,n) cout << i << " " << vec.at(i) << "\n";
}
void prvec_won(vector<ll> vec){
ll n = vec.size();
rep(i,n) cout << vec.at(i) << " ";
cout << endl;
}
void pr2d(vector<vector<ll>> vvec){
ll h = vvec.size();
ll w = vvec.at(0).size();
rep(i,h){
rep(j,w){
cout << vvec.at(i).at(j) << " ";
}
cout << "\n";
}
}
void deb_mk(ll n){
cout << "debug " << n << endl;
}
struct SegmentTree {
int n;
vector<ll> node;
//int nとvector<int> nodeの2つを変数に持つ
//初期化処理
SegmentTree(vector<ll> v) {
int sz = v.size(); //szは扱う配列の長さ
n = 1; while(n < sz) n *= 2; //nはvの配列を入れるために必要なseg treeの葉の数
node.resize(2*n-1, 0); //segtreeでは全部で2n-1の配列が必要
for(int i=0; i<sz; i++) node[i+n-1] = v[i]; //segtreeの葉の部分に扱う配列を埋め込む
for(int i=n-2; i>=0; i--) node[i] = node[2*i+1]^node[2*i+2]; //演算によって変える場所 segtreeを作る(注目しているnode iの子ノードを見て二項演算をして、node iの値を決定する)
}
void update(int x, int val) {
x += (n - 1); //配列のx番目(1-indexed)の要素を更新しようとするとき、segtreeで対応する要素はx+n-1(0-indexed)番目である
node[x] = val; //segtree上で値を更新
while(x > 0) { //葉から根に向かってsegtreeの要素を更新していく
x = (x - 1) / 2;
node[x] = node[2*x+1]^node[2*x+2]; //演算によって変える
}
}
int getxor(int a, int b, int k=0, int l=0, int r=-1) {
//求めたいのは区間[a,b)の中で演算しつづけた結果(たとえばこの区間の最小値)
//自分がいるノードのindex(何階にいるか?)
//ノードkは元の配列でいうとどの区間をカバーしているか?
if(r < 0) r = n; //最初に呼び出す時はノードk=0はsegtreeの根なので区間は[0,n)を指す
if(r <= a || b <= l) return 0; //今のノードが求める区間と無関係の場合は、最終的な答えに影響しない単位元(最小値を求めたい場合は単位元はINF)を返す 演算によって変える
if(a <= l && r <= b) return node[k]; //今見ている区間[l,r)が答えを求めたい区間[a,b)に完全に含まれるなら、この区間について演算し続けた結果(=node[k];例えば最小値)を返す
//上記に当てはまらない場合
//kの子ノードを見に行って、再帰的に処理を行う k=0の場合はノード1と2を見る
int vl = getxor(a, b, 2*k+1, l, (l+r)/2); //kの子ノード(left)の子孫のうち、求めたい区間に含まれる要素について演算し続けた結果を返す
int vr = getxor(a, b, 2*k+2, (l+r)/2, r); //kの子ノード(right)の子孫のうち、求めたい区間に含まれる要素について演算し続けた結果を返す
return vl^vr; //演算によって変える場所 子ノードの子孫のうち、演算しつづけた結果を返す
}
};
int main(){
ll nn , q; cin >> nn >> q;
vector<ll> a(nn);
rep(i,nn) cin >> a.at(i);
SegmentTree tree(a);
rep(i,q){
ll t,x,y; cin >> t >> x >> y;
if(t==1){
tree.update(x-1,(tree.node.at(x-1+ (tree.n) -1))^y);
}else{
cout << tree.getxor(x-1,y) << endl;
}
//prvec_won(tree.node);
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); }
int n, m;
int main(int argc, char** argv) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
cin >> n >> m;
vector<int> a(n+1);
FOR1(i, n) cin >> a[i];
vector<vector<int>> edges(n+1);
FOR(i, m){
int u, v;
cin >> u >> v;
edges[v].push_back(u);
}
const ll inf = 1e9+1;
vector<ll> dp(n+1, inf);
ll sol = -1e9;
FOR1(v, n){
for(int u : edges[v])
umin(dp[v], dp[u]);
if (edges[v].size())
umax(sol, (ll)a[v] - dp[v]);
umin(dp[v], a[v]);
}
cout << sol << endl;
if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
return 0;
}
| #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <math.h>
#include <random>
#include <chrono>
#include <assert.h>
using namespace std ;
using ll = long long ;
using ld = long double ;
template<class T> using V = vector<T> ;
template<class T> using VV = V<V<T>> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
ll mod = 1000000007 ;
long double pie = acos(-1) ;
ll INF = 1e18 ;
void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
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 ;
}
void fix_cout(){cout << fixed << setprecision(20) ;}
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 ;}
int main(){
int n,m ;
cin >> n >> m ;
V<ll> c(n) ;
for(auto &i:c) cin >> i ;
VV<int> g(n) ;
for(int i=0;i<m;i++){
int a,b ;
cin >> a >> b ;
g[a-1].push_back(b-1) ;
}
V<ll> dp(n,INF) ;
ll ans = -INF ;
for(int i=0;i<n;i++){
ans = max(ans,c[i]-dp[i]) ;
c[i] = min(c[i],dp[i]) ;
for(int j=0;j<g[i].size();j++){
dp[g[i][j]] = min(dp[g[i][j]],c[i]) ;
}
}
// for(auto i:dp) cout << i << " " ; cout << endl ;
cout << ans << endl ;
}
|
#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define N 200005
using namespace std;
int read() {
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int n,m,a[N],vst[17],tot;
char s[N];
ll f[N][17];
int main() {
scanf("%s",s+1);n = strlen(s+1);
for(int i=1; i<=n; ++i)if(s[i] <= '9')a[i] = s[i]-'0';else a[i] = s[i]-'A'+10;
m = read();
for(int i=1; i<=n; ++i) {
for(int j=1; j<=m; ++j)
f[i][j] = f[i-1][j]*j+f[i-1][j-1]*(16-j+1);
if(a[i] >= 1) {
int sum = 0;
for(int j=0; j<a[i]; ++j)sum += vst[j];
f[i][tot] += sum;
int tmp = a[i]-sum;
if(i==1)tmp--;
f[i][tot+1] += max(tmp,0);
}
if(i > 1)f[i][1] += 15;
for(int j=1; j<=m; ++j)f[i][j] %= mod;
if(!vst[a[i]])vst[a[i]] = 1,tot++;
}
cout << (f[n][m]+(tot==m));
}
| #include <iostream>
#include <algorithm>
#include <string>
using namespace std;
constexpr int Z = 1e9+7;
void add(int cnt[], int &ccnt, int x) {
cnt[x]++;
if (cnt[x] == 1) {
ccnt++;
}
}
void sub(int cnt[], int &ccnt, int x) {
cnt[x]--;
if (cnt[x] == 0) {
ccnt--;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string t;
cin >> t;
int k;
cin >> k;
int n = t.length();
int a[n];
for (int i = 0; i < n; i++) {
a[i] = t[i] <= '9' ? t[i] - '0' : t[i] - 'A' + 10;
}
int dp[n][k+1];
fill(dp[0], dp[n], 0);
dp[0][0] = 1;
for (int i = 1; i < n; i++) {
for (int j = 0; j <= k; j++) {
dp[i][j] = (1ll * dp[i-1][j] * (k - j) + 1ll * (j == 0 ? 0 : dp[i-1][j-1]) * (16 - k + j)) % Z;
}
}
int ans = 0;
for (int i = 1; i < n; i++) {
ans = (0ll + ans + dp[i][k] - dp[i-1][k-1] + Z) % Z;
}
int cnt[16];
fill(cnt, cnt + 16, 0);
int ccnt = 0;
for (int i = 0; i < n; i++) {
for (int j = (i == 0 ? 1 : 0); j < a[i]; j++) {
add(cnt, ccnt, j);
if (ccnt <= k) {
(ans += dp[n-1-i][k-ccnt]) %= Z;
}
sub(cnt, ccnt, j);
}
add(cnt, ccnt, a[i]);
}
(ans += (ccnt == k)) %= Z;
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 102;
double x[N], y[N], n;
double dist(int i, int j) {
return hypot(x[i] - x[j], y[i] - y[j]);
}
bool ok(double m) {
vi p(n + 2);
int s = n, t = n + 1;
iota(ALL(p), 0);
function<int(int)> find = [&](int u) {
return p[u] == u ? u : p[u] = find(p[u]);
};
auto unite = [&](int x, int y) {
int rx = find(x), ry = find(y);
p[rx] = ry;
};
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++) {
if(dist(i, j) <= m)
unite(i, j);
}
}
for(int i = 0; i < n; i++) {
if(100 - y[i] <= m)
unite(s, i);
if(100 + y[i] <= m)
unite(t, i);
}
return find(s) != find(t);
}
signed main()
{
IO_OP;
cin >> n;
for(int i = 0; i < n; i++) cin >> x[i] >> y[i];
double lb = 0, rb = 300;
for(int i = 0; i < 100; i++) {
double mb = (lb + rb) / 2;
if(ok(mb)) lb = mb;
else rb = mb;
}
cout << fixed << setprecision(10) << lb / 2 << endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int n,m;
int vdnsddk;
cin>>n>>m;
vector<pair<int,int>>a(m);
for(int i=0;i<m;i++)
{
cin>>a[i].first>>a[i].second;
}
int k;
cin>>k;
vector<pair<int,int>>b(k);
for(int j=0;j<k;j++)
{
cin>>b[j].first>>b[j].second;
}
ll l=0;
ll tot=1<<k;
for(ll mask=0;mask<tot;mask++)
{
vector<int>v(n+1);
ll ans=0;
for(int i=0;i<k;i++)
{
if(mask & (1<<i))
v[b[i].second]=1;
else
v[b[i].first]=1;
}
for(int i=0;i<m;i++)
{
if(v[a[i].first] && v[a[i].second])
ans++;
}
l=max(l,ans);
}
cout<<l<<endl;
return 0;
} |
#include<algorithm>
#include<iostream>
#include<cstdio>
#define int long long
int t;
std::string st,goal="atcoder";
bool can(int len){
for(int i=0;i<st.length()-len;i++){
std::swap(st[i],st[len+i]);
if(st>goal)return true;
std::swap(st[i],st[len+i]);
}
return false;
}
signed main(){
scanf("%lld",&t);
for(int wc=1;wc<=t;wc++){
std::cin>>st;
if(st>goal){
puts("0");
continue;
}
bool flag=false;
for(int i=1;i<=st.length()-1;i++)if(can(i)){
printf("%lld\n",i);
flag=true;
break;
}
if(!flag)puts("-1");
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
int main() {
auto solve = [&] () {
static char s[1005];
scanf("%s", s);
int n = strlen(s);
const char *t = "atcoder";
int m = strlen(t);
int ans = 0x3f3f3f3f, cur = 0;
bool full = 1;
rep(i, min(n, m)) {
int mneq = n, mngeq = n;
for (int j = i; j < n; ++ j) {
if (s[j] == t[i]) mneq = min(mneq, j);
if (s[j] > t[i]) mngeq = min(mngeq, j);
}
if (mngeq < n) ans = min(ans, cur + mngeq - i);
if (mneq < n) cur += mneq - i;
else { full = 0; break; }
}
if (full && n > m) ans = min(ans, cur);
if (ans >= 1e9) printf("-1\n");
else printf("%d\n", ans);
};
int T; scanf("%d", &T);
while (T --) solve();
return 0;
}
|
//ABC_199_
//
#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)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int mod = 1e9 + 7;
class mint
{
public:
long long x;
mint(long long x = 0) :x((x% mod + mod) % mod) {}
mint operator -() const { return mint(-x); }
mint& operator +=(const mint a)
{
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator -=(const mint a)
{
if ((x += -a.x + mod) >= mod) x -= mod;
return *this;
}
mint& operator *=(const mint a)
{
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a)
{
mint res(*this);
return res += a;
}
mint operator-(const mint a)
{
mint res(*this);
return res -= a;
}
mint operator*(const mint a)
{
mint res(*this);
return res *= a;
}
mint pow(long long t) const
{
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
//for only prime number
//Fermat's little theorem
mint inv() const
{
return pow(mod - 2);
}
mint& operator/=(const mint a)
{
return (*this) *= a.inv();
}
mint operator/(const mint a)
{
mint res(*this);
return res /= a;
}
};
struct Matrix
{
int s;
mint A[100][100];
};
Matrix cal_matrix(Matrix X, Matrix Y)
{
int s = X.s;
Matrix Z = {s};
rep(i,s)rep(j,s) Z.A[i][j] = 0;
//xとyのサイズが同じとする
rep(i,s)rep(j,s)rep(k,s)
{
Z.A[i][j] += X.A[i][k]*Y.A[k][j];
}
return Z;
}
Matrix matrix_pow(Matrix X,ll n)
{
Matrix table[64];
Matrix An = X;
//A^(2n)を計算
for(int i = 0;i<64;++i)
{
table[i] = An;
An = cal_matrix(An,An);
}
//基本行列
Matrix I = {X.s};
rep(i,X.s)rep(j,X.s)
{
if(i==j) I.A[i][j] = 1;
else I.A[i][j] = 0;
}
if(n == 0) return I;
//繰り返し2乗法
for(int i = 0;i < 64;++i)
{
if(n & (1LL<<i)) I = cal_matrix(I,table[i]);
}
return I;
}
//繰り返し2乗法
mint pow_mod(mint x,int n)
{
if(n == 0) return 1;
mint table[64];
mint t = x;
for(int i=0;i<60;++i)
{
table[i] = x;
x = x*x;
}
mint res = 1;
for(int i = 0;i < 64;++i)
{
if(n & (1LL<<i)) res *= table[i];
}
return res;
}
bool g[100][100];
int main()
{
int n,m,k;
cin>> n >> m >> k;
vector<int> A(n);
rep(i,n) cin >> A[i];
//頂点の次数
vector<int> D(n);
rep(i,m)
{
int a,b;cin >> a >> b;
--a,--b;
g[a][b] = true;
g[b][a] = true;
++D[a],++D[b];
}
Matrix F;
F.s = n;
//行列の作成
//分子だけ計算する
rep(i,n)rep(j,n)
{
if(i==j)
{
F.A[i][j] = 2*(m-D[i])+D[i];
}
else
{
if(g[i][j])
{
F.A[i][j] = 1;
}
else
{
F.A[i][j] = 0;
}
}
}
//繰り返し2情報で行列累乗
F = matrix_pow(F,k);
//分母の計算
mint x = 2*m;
x = pow_mod(x,k);
//累乗した行列に対して計算する
mint Y[100];
rep(i,n)
{
rep(j,n)
{
Y[i] += F.A[i][j]*A[j];
}
}
rep(i,n)
{
cout << (x.inv()*Y[i]).x << endl;
}
return 0;
} | #include<bits/stdc++.h>
#define int long long
#define N 100
#define P 1000000007
#define inf 999999999999999999
using namespace std;
int n,m,K,d[N+1],dq[N+1],g[N+1][N+1],ny2,ans[N+1],p,q;
int ksm(int x,int y){int nans=1;for(;y;y>>=1,x=x*x%P)if(y&1)nans=nans*x%P;return nans;}
int qny(int x){return ksm(x,P-2);}
struct mat{
int a[N+1][N+1];
const mat operator *(mat y)const{
mat nans;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
nans.a[i][j]=0;
for(int k=1;k<=n;k++)nans.a[i][j]+=this->a[i][k]*y.a[k][j],nans.a[i][j]%=P;
}
}return nans;
}
}I,A;
void ycl(){
ny2=qny(2);
for(int i=1;i<=n;i++)I.a[i][i]=1;
for(int i=1;i<=n;i++){
A.a[i][i]=ny2+(m-d[i])*qny(2*m%P)%P;
for(int j=1;j<=n;j++){
if(g[i][j])A.a[i][j]=qny(2*m%P);
}
}
}
mat ksm(mat x,int y){mat nans=I;for(;y;y>>=1,x=x*x)if(y&1)nans=nans*x;return nans;}
signed main(){
cin>>n>>m>>K;
for(int i=1;i<=n;i++)cin>>dq[i];
for(int i=1;i<=m;i++)cin>>p>>q,g[p][q]=g[q][p]=1,d[p]++,d[q]++;
ycl();
A=ksm(A,K);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)ans[i]+=dq[j]*A.a[i][j],ans[i]%=P;
for(int i=1;i<=n;i++)printf("%lld\n",ans[i]);
return 0;
} |
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
bool flag=1;
int x=0;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag=0;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return (flag?x:~(x-1));
}
set<int> s;
int n,a,ans=1;
const int mod=1e9+7;
signed main()
{
n=read();
while(n--)
s.insert(read());
s.insert(0);
for(auto a=++s.begin(),b=s.begin();a!=s.end();a++,b++)
{
(ans*=(1+*a-*b))%=mod;
}
cout<<ans;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
const int N = 2e5 + 5;
const int INF = 2e9;
const ll LINF = 9e18;
const int MOD = 1e9 + 7;
int n;
int a[N], dp[N];
int add(int x, int y) { return (x + 0ll + y) % MOD; }
int mul(int x, int y) { return x * 1ll * y % MOD; }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
int ans = 1;
for(int i = 1; i <= n; i++) {
ans = mul(ans, a[i] - a[i - 1] + 1);
}
cout << ans << "\n";
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if(n%2==0) cout << "White" << endl;
else cout << "Black" << endl;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
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
// -----------------------------------------------------------------------------------
#define sz(s) s.size()
#define ll long long int
#define ull unsigned ll
#define ld long double
#define print(v) for(ll i=0;i<v.size();i++)cout<<v[i]<<" "
#define printpair(v) for(ll i=0;i<v.size();i++)cout<<v[i].first<<" "<<v[i].second<<"\n"
#define bmd 1000000007
#define bmd1 998244353
#define umll unordered_map<ll,ll>
#define mll map<ll,ll>
#define mcl map<char,ll>
#define pll pair<ll,ll>
#define F first
#define S second
#define setp(x) setprecision(x)
#define pb push_back
#define all(v) v.begin(),v.end()
#define tr(a) for(auto it=a.begin();it!=a.end();it++)
#define trr(a) for(auto it1=a.begin();it1!=a.end();it1++)
#define vll vector<long long int>
#define sorty(v) sort(v.begin(),v.end())
#define rsort(v) sort(v.rbegin(),v.rend())
#define unik(v) v.erase(unique(all(v)),v.end())
#define PI 3.1415926535897932384626
#define db() cout<<"\n"<<"here"<<"\n"
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//---------------------------------------------------------------------------
const int MOD = 1e9+7;
const int N=2e5+100;
inline ll sqr(ll x){return x*x;}
inline void normal(ll &a) { a = (a+MOD)%MOD; }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1;b=b%MOD; while(p) { if(p&1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInv(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a,ll b) { return modMul(a, modInv(b)); }
//---------------------------------------------------------------------------
void solve()
{
ll n;
cin >> n;
if(n%2)
cout << "Black";
else cout << "White";
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w" , stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define dd double
#define ll long long int
#define light ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{
#ifndef ONLINE_JUDGE
freopen("input1.txt","r",stdin);
freopen("output1.txt","w",stdout);
#endif
light;
dd x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
cout<<setprecision(15)<<x1+((x2-x1)*y1)/(y2+y1)<<"\n";
} | // Problem: D - Opposite
// Contest: AtCoder - AtCoder Beginner Contest 197(Sponsored by Panasonic)
// URL: https://atcoder.jp/contests/abc197/tasks/abc197_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ld long double
#define inf 1000000000
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mod 1000000007
#define mod2 998244353
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define w(x) int x; cin>>x; while(x--)
#define in(a) for(auto &x: a)cin>>x;
void print(bool n){if(n)cout<<"YES";else cout<<"NO";}
#define pi 3.14159
signed main(){
ld n,a,b,c,d;
cin>>n;
cin>>a>>b>>c>>d;
ld mx=(a+c)/2.0;
ld my=(b+d)/2.0;
a-=mx;
b-=my;
ld aa=a*cos(pi/(n/2))-b*sin(pi/(n/2));
ld bb=a*sin(pi/(n/2))+b*cos(pi/(n/2));
//cout<<mx<<' '<<my<<'\n';
cout<<fixed<<setprecision(6)<<aa+mx<<' '<<bb+my;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
const int N=1e3+5;
const int inf=0x3f3f3f3f;
template<class T>inline void read(T &x) {
T f=1;x=0;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;
}
vector<int> to[26][N];
int e[N][N];
int dis[N][N],n,m,a,b,ad,cnt[N];
char s[5];
queue<pii> q;
pii tp;
int main() {
read(n); read(m);
rep(i,1,m) {
read(a); read(b);
scanf("%s",s+1);
ad=s[1]-'a';
e[a][b]=1; e[b][a]=1;
to[ad][a].emplace_back(b);
to[ad][b].emplace_back(a);
}
memset(dis,0x3f,sizeof(dis));
dis[1][n]=0;
q.push(mp(1,n));
int Ans=inf;
while(!q.empty()) {
tp=q.front(); q.pop();
rep(i,0,25) {
for(int v1:to[i][tp.fi]) for(int v2:to[i][tp.se]) {
if(dis[v1][v2]>(dis[tp.fi][tp.se]+2)) {
dis[v1][v2]=dis[tp.fi][tp.se]+2;
q.push(mp(v1,v2));
}
}
}
}
rep(i,1,n) Ans=std::min(Ans,dis[i][i]);
rep(i,1,n) rep(j,1,n) if(e[i][j]) Ans=std::min(Ans,dis[i][j]+1);
if(Ans==inf) puts("-1");
else printf("%d\n",Ans);
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define LOL cout << '\n';
#define kill(x) {cout << (x) << '\n'; return;}
#define sz(x) ((int)x.size())
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
#define deb(x) cout << "[" << (#x) << "=" << x << "]" << '\n'
#define deb2(x,y) cout << "[" << (#x) << "=" << x << "] [" << (#y) << "=" << y << "]" << '\n'
#define deb3(x,y,z) cout << "[" << (#x) << "=" << x << "] [" << (#y) << "=" << y << "] [" << (#z) << "=" << z << "]" << '\n'
void solve(){
int a,b,c,d; cin>>a>>b>>c>>d;
int x=max(a,b),y=min(c,d);
kill(x-y);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int _ = 1; // cin >> _;
for(int i = 1 ; i <= _ ; i++){
// cout << "\nCase #" << i << ": \n";
solve();
}
return 0;
}
// 1.77245 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, A, B;
cin >> N >> A >> B;
cout << N - A + B;
}
| #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 100001
#define i197 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<int,ll>Pil;
typedef pair<P1,ll> P2;
typedef pair<string,string> Ps;
const ll INF=1000000000000000001;
struct edge{int to,cost;};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};
struct pos{
// 1 変数を入れる;
int a;
};
int main(){
int n,a,b;
cin>>n>>a>>b;
cout<<n-a+b<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std ;
using ll = long long ;
int T ;
int main(){
cin >> T ;
vector<ll> val ;
for(int i = 0 ; i < T ; i++){
ll R , L ;
cin >> L >> R ;
ll tmp = 2 * L ;
if(R < tmp){
val.push_back(0) ;
continue ;
}
ll res = R - tmp + 1 ;
ll ans = res * (res + 1) / 2 ;
val.push_back(ans) ;
}
for(ll u : val) cout << u << endl ;
} | // Jai Shree Ram
#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define max2(a,b) (a>b)?a:b
#define mi2(a,b) (a<=b)?a:b
#define fori(i,n) for(ll i=0;i<(ll)n;i++)
#define ford(i,n) for(ll i=n;i>=0;i--)
#define pll pair<ll,ll>
#define MOD 998244353
#define ff first
#define ss second
using namespace std;
#define MAX_LIMIT 1000001
// ll power(ll n, ll pow)
// {
// n %= MOD;
// long long int res = 1;
// while (pow)
// {
// if (pow % 2)
// {
// res = ((res % MOD) * (n % MOD)) % MOD;
// }
// pow /= 2;
// n = ((n % MOD) * (n % MOD)) % MOD;
// }
// res %= MOD;
// return res;
// }
void solve()
{
ll l,r;
cin>>l>>r;
ll ptr1=l,ptr2=r;
ll n=(r-2*l)+1;
if(r-l<l)
{
cout<<0<<"\n";
return;
}
ll cn=(n*(n+1))/2;
// while(true)
// {
// if(ptr2-ptr1>=ptr1)
// {
// cn+=(ptr2-2*ptr1+1);
// ptr2--;
// }
// else
// {
// break;
// }
// }
cout<<cn<<"\n";
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll T;
T=1;
cin>>T;
//sievefun(1000004);
fori(_,T)
{
solve();
}
}
|
#include <cstdio>
#include <cstring>
using namespace std;
#define rei register int
#define rep(i, l, r) for(rei i = l, i##end = r; i <= i##end; ++i)
#define per(i, r, l) for(rei i = r, i##end = l; i >= i##end; --i)
inline void chmax(int &x, const int y) { if(x < y) x = y; }
const int N = 105;
inline int read() {
int res = 0; char ch = getchar(); bool f = true;
for(; ch < '0' || ch > '9'; ch = getchar())
if(ch == '-') f = false;
for(; ch >= '0' && ch <= '9'; ch = getchar())
res = res * 10 + (ch ^ 48);
return f ? res : -res;
}
int n, m, ans, sum, k, a[N], b[N], c[N][2];
bool buc[N];
signed main() {
n = read(); m = read();
rep(i, 1, m) a[i] = read(), b[i] = read();
k = read();
rep(i, 1, k) c[i][0] = read(), c[i][1] = read();
rep(s, 0, (1 << k) - 1) {
sum = 0;
memset(buc, 0, sizeof(buc));
rep(i, 1, k) buc[c[i][(s >> i - 1) & 1]] = 1;
rep(i, 1, m) if(buc[a[i]] && buc[b[i]]) sum ++;
chmax(ans, sum);
}
printf("%d\n", ans);
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define pb push_back
#define vi vector<int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int long long int
#define mod 1000000007
#define MAXN 1000005
typedef tree<int, null_type,
less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
Ordered_set;
// s.find_by_order(idx) find using index..!
// s.order_of_key(val) Gives iterator to the key..!
/* First solve then try Writing Code.. And be cool ^_^ and calm. */
int n, q, a[MAXN], BIT[MAXN];
void update(int idx, int val)
{
while(idx <= n)
{
BIT[idx] ^= val;
idx += (idx&(-idx));
}
}
int getAns(int idx)
{
int ans = 0;
while(idx > 0)
{
ans ^= BIT[idx];
idx -= (idx & (-idx));
}
return ans;
}
void solve(){
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
{
update(i, a[i]);
}
while(q--)
{
int ch;cin>>ch;
if(ch == 1)
{
int l,r;cin>>l>>r;
update(l, r);
}
else
{
int l, r; cin>>l>>r;
cout<<(getAns(r)^getAns(l-1))<<endl;
}
}
}
// 12 - 12 + 11 c
int32_t main()
{
fast;
int t=1;
// cin>>t;
while(t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long L = 10000000000;
int main(){
long long N;
string T;
cin >> N >> T;
if(N==1){
if(T=="0") cout << L << endl;
else cout << L*2 << endl;
}
else if(N==2){
if(T=="00") cout << 0 << endl;
else if(T=="01") cout << L-1 << endl;
else cout << L << endl;
}
else{
int s=-1;
if(T[0]=='0') s = 0;
else if(T[1]=='0') s = 1;
else if(T[2]=='0') s = 2;
else{
cout << 0 << endl;
return 0;
}
long long count=0;
for(int i=s+1;i<N;i++){
if(T[i]=='0') count++;
if((i-s)%3==0 && T[i]!='0'){
cout << 0 << endl;
return 0;
}
if((i-s)%3!=0 && T[i]!='1'){
cout << 0 << endl;
return 0;
}
}
long long ans = L - count;
if(T[N-1]=='1') ans--;
cout << ans << endl;
}
return 0;
}
| #include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
#define ll long long
#define lson (rt<< 1)
#define rson (rt<< 1 | 1)
#define gmid ((l+r)>> 1 )
using namespace std;
const int maxn=20000050;
string s;
char a[4];
int main()
{
ll k=10000000000;
int n;
cin>>n;
a[0]='1';a[1]='1';a[2]='0';
cin>>s;
//cout<<s[0]<<endl;
int f=-1;
for(int i=0;i<=2;++i)
{
f=i;
for(int j=0;j<n;++j)
if(s[j]!=a[(i+j)%3])
{
f=-1;
// cout<<j<<" "<<(i+j)%3<<endl;
//cout<<s[j]<<" "<<a[(i+j)%3]<<endl;
break;
}
//cout<<endl;
if(f>=0) break;
}
//cout<<f<<endl;
if(f==-1) cout<<0<<endl;
else
if(n==1)
{
if(s[0]=='1') cout<<k*2<<endl;
else cout<<k<<endl;
}
else if(n==2)
{
if(s[0]=='1')
{
cout<<k<<endl;
}
else
{
if(s[1]=='1')
cout<<k-1<<endl;
}
}
else
{
n=n-(3-f);
ll t=n/3;
ll l=0;
if(n%3>0) t++;
if(n>0) cout<<k-t<<endl;
else cout<<k<<endl;
}
}
|
/* _____________________
|Author : canhnam357|
|___________________|
*/
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <string> vs;
typedef vector <vector <int>> vvi;
typedef vector <vll> vvll;
typedef map<int, int> mi;
typedef map<string, int> ms;
typedef map<char, int> mc;
typedef map <int, bool> mb;
typedef map<ll, ll> mll;
typedef unordered_map<int, int> umi;
typedef unordered_map<string, int> ums;
typedef unordered_map<char, int> umc;
typedef unordered_map <int, bool> umb;
typedef unordered_map<ll, ll> umll;
typedef vector <ld> vld;
typedef vector <bool> vb;
typedef pair <int, int> pii;
typedef pair<ll, ll> pll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
int CASE = 1;
const int mxn = 4e3 + 1;
const ll INF = 1e10;
#define FOR(i,N) for(ll i = 0 ; i < N;i++)
#define eFOR(i,a,b) for(ll i = a; i <=b;i++)
#define dFOR(i,N) for(ll i = N - 1; i>=0;i--)
#define edFOR(i,b,a) for(ll i = b ; i >=a;i--)
#define all(x) x.begin(),x.end()
#define SORT(x) sort(all(x))
#define RSORT(x) sort(x.rbegin(),x.rend())
#define UNQ(x) unique(all(x))
#define mine(x) min_element(all(x))
#define maxe(x) max_element(all(x))
#define lb(v, x) lower_bound(all(v) , x)
#define ub(v , x) upper_bound(all(v) , x)
#define pb push_back
#define PI 3.141592653589793
#define FIX cout << fixed << setprecision(15)
#define g(i , a) get<i>(a)
const ll mod = 2e5;
bool prime(ll n)
{
if (n <= 1)
return false;
eFOR(i, 2, sqrt(n))
if (!(n % i))
return false;
return true;
}
ll __gcd(ll a, ll b)
{
return !b ? a : __gcd(b, a % b);
}
void solve()
{
int n;
string t;
cin >> n >> t;
string s = "";
if (t.length() == 1)
{
if (t == "1")
cout << 2LL * INF;
else
cout << INF;
return;
}
while (s.length() < mod)
s += "110";
if (s.find(t) == -1)
{
cout << 0;
return;
}
bool ok = true;
int cnt = 0;
for (char c : t)
{
if (ok)
{
ok = false;
cnt++;
}
if (c == '0')
ok = true;
}
cout << INF - cnt + 1;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
while (T--)
{
solve();
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define me(a,x) memset(a,x,sizeof(a))
#define sc scanf
#define pr printf
#define IN freopen("in",stdin);
#define OUT freopen("out","w",stdout);
typedef long long ll;
typedef unsigned long long ull;
const int N=1e7+6;
const int mod=1e9+7;
int O(){putchar('\n');return 0;}template<typename T,typename... Ty>
int O(const T& a,const Ty&... b){cout<<a<<' ';return O(b...);}
void I(){}template<typename T,typename... Ty>void I(T& a,Ty&... b){cin>>a;I(b...);}
template<typename T>void db(T *bg,T *ed){while(bg!=ed)cout<<*bg++<<' ';pr("\n");}
inline ll mul_64(ll x,ll y,ll c){return (x*y-(ll)((long double)x/c*y)*c+c)%c;}
inline ll ksm(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;}
int n;
string s;
//110
//101
//011
string p[]={"110","101","011"};
ll m=1e10;
int check(string x){
for(int i=0;i<3;i++){
if(p[i]==x){
return i;
}
}
return -1;
}
bool istrue(int pos){
int n=s.size();
for(int i=0;i<n;i+=3){
if(n-i<3){
if(p[pos].substr(0,n-i)!=s.substr(i,n-i))return false;
return true;
}
if(p[pos]!=s.substr(i,3))return false;
}
return true;
}
int main(){
I(n,s);
if(n==1){
if(s[0]=='0')return O(m);
else return O(m*2);
}
if(n==2){
if(s=="00")return O(0);
if(s=="11"||s=="10")return O(m);
return O(m-1);
}
int pos=check(s.substr(0,3));
if(pos==-1)return O(0);
bool f=istrue(pos);
if(!f)return O(0);
ll ans=m*3-n-pos;
ll k=ans/3;
while(3LL*k+n+pos<=m*3)k++;
O(k);
}
|
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
//#include <unistd.h>
void solve(){
int n;
cin>>n;
V<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
a[i]--;
}
int t=0;
V<int> ans;
V<bool> ok(n,false);
while(1){
bool ok=true;
for(int j=0;j<n;j++){
if(a[j]<j){
ok=false;
if(j%2==t){
if(t==0){
swap(a[0],a[1]);
ans.emplace_back(1);
}else{
swap(a[1],a[2]);
ans.emplace_back(2);
}
t^=1;
}
while(a[j]<j){
swap(a[j-1],a[j]);
ans.emplace_back(j);
j--;
t^=1;
}
break;
}
}
if(ok)break;
}
// print(a);
cout<<ans.size()<<"\n";
print(ans);
}
int main(){
int t;
cin>>t;
while(t--)solve();
} | #include<bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
using ll = long long;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a) (a).begin(),(a).end()
auto canMove = [](int cnt, int idx){return (cnt + idx) % 2 == 0;};
void Cout(vector<int> &ans){
cout << ans.size() << endl;
for(auto i : ans) cout << i + 1 << " ";
cout << endl;
}
void Check(vector<int> &p, vector<int> &ans){
int n = p.size(), m = ans.size();
if(m > n * n) throw runtime_error("over the limit");
For(i,0,n){
if(p[i] != i){
throw runtime_error("vector is not sorted");
}
}
For(i,0,m){
if(!canMove(i, ans[i])){
throw runtime_error("invalid swap");
}
}
}
void In(int &n, vector<int> &p){
cin >> n;
p.resize(n);
For(i,0,n){
cin >> p[i];
p[i]--;
}
return;
}
void Rand(int &n, vector<int> &p){
random_device seed_gen;
mt19937 engine(seed_gen());
n = engine() % 500 + 1;
p.resize(n);
iota(ALL(p), 0);
shuffle(ALL(p), engine);
}
void solve(){
int n;
vector<int> p, ans;
In(n, p);
// Rand(n, p);
For(cnt,0,n*n){
int target = -1;
for(int i=n-1; i>-1; i--){
if(p[i] != i){
target = i;
break;
}
}
if(target == -1){
Check(p, ans);
Cout(ans);
return;
}
For(i,0,n-1){
if(p[i] == target){
if(canMove(cnt, i)){
ans.push_back(i);
swap(p[i], p[i+1]);
}
else{
ans.push_back(cnt % 2);
swap(p[cnt % 2], p[cnt % 2 + 1]);
}
break;
}
}
}
}
void Main(){
int t;
cin >> t;
For(_,0,t){
solve();
}
}
int main(){
Main();
/*
東方風神録は神が出てくるので当然神ゲー
*/
return 0;
} |
//clear adj and visited vector declared globally after each test case
//check for long long overflow
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Incase of close mle change language to c++17 or c++14
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007ll //998244353ll
#define lld long double
#define mii map<int, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
int g=__gcd(a, b);
return a/g*b;
}
int power(int a, int b, int p)
{
if(a==0)
return 0;
int res=1;
a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
int32_t main()
{
IOS;
int n;
cin>>n;
int a[n+1], b[n+1], c[n+1];
rep(i,1,n+1)
cin>>a[i];
rep(i,1,n+1)
cin>>b[i];
int co[n+1];
fill(co, 0);
rep(i,1,n+1)
{
cin>>c[i];
c[i]=b[c[i]];
co[c[i]]++;
}
int ans=0;
rep(i,1,n+1)
ans+=co[a[i]];
cout<<ans;
} | // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(a) a.begin(), a.end()
#define sz(x) x.size()
#define yn(p) cout << (p?"Yes":"No") << endl;
using namespace std;
using ll = long long;
using P = pair<int, int>;
// #include <atcoder/all>
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace atcoder;
// using lll = boost::multiprecision::cpp_int;
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;
const int INF = 1000000000;
const int MOD = 1000000007;
const ll llINF = 1000000000000000000;
void solve() {
int n;
cin >> n;
vector<ll> a(n), b(n), c(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
vector<ll> x(100001, 0), y(100001, 0);
for (int i = 0; i < n; ++i) {
++x[a[i]];
++y[b[c[i]-1]];
}
ll ans = 0;
for (int i = 0; i < 100001; ++i) {
ans += x[i] * y[i];
}
cout << ans << endl;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;
int main() {
int a,b,c,d;
cin >> a >> b >> c >> d;
int n=0;
if (a <= n*d*c) {
cout << n << endl;
return 0;
}
n++;
if (d*c-b <= 0) {
cout << -1 << endl;
return 0;
}
while ( a > n*(d*c-b)) {
n++;
}
cout << n++ << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define endl '\n'
typedef pair<int, int> P;
typedef long long ll;
class Combination
{
vector<long long> fact;
vector<long long> factinv;
long long MOD = 1e9 + 7;
long long modpow(long long a, long long n)
{
if (n == 0)
return 1;
if (n % 2 == 0)
{
long long t = modpow(a, n / 2);
return t * t % MOD;
}
return a * modpow(a, n - 1) % MOD;
}
long long modinv(long long a)
{
// mod is prime
return modpow(a, MOD - 2);
}
public:
Combination(int n) : fact(n), factinv(n)
{
fact[0] = 1;
for (int i = 1; i < n; i++)
{
fact[i] = fact[i - 1] * i % MOD;
}
factinv[n - 1] = modinv(fact[n - 1]);
for (int i = n - 2; i >= 0; i--)
{
factinv[i] = factinv[i + 1] * (i + 1) % MOD;
}
}
long long ncr(long long n, long long r)
{
if (r < 0 || r > n)
return 0;
return fact[n] * factinv[r] % MOD * factinv[n - r] % MOD;
}
long long npr(long long n, long long r)
{
if (r < 0 || r > n)
return 0;
return fact[n] * factinv[n - r] % MOD;
}
long long nhr(long long n, long long r)
{
if (n == 0 && r == 0)
return 1;
return ncr(n + r - 1, r);
}
};
ll solve1(int n)
{
ll ans = 0;
ll mod = 1e9 + 7;
Combination C(100000);
for (int i = 1; i <= n / 2; i++)
{
ans += C.nhr(i, n - 2 * i);
ans %= mod;
}
return ans;
}
ll solve2(int n)
{
if (n <= 2)
{
return 1;
}
ll ans = 1;
ll mod = 1e9 + 7;
for (int i = 0; i < n - 3; i++)
{
ans *= 2;
ans %= mod;
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
ll ans = 0;
char caa, cab, cba, cbb;
cin >> caa >> cab >> cba >> cbb;
if (cab == 'A')
{
if (caa == 'A')
{
ans = 1;
}
else
{
if (cba == 'A')
{
ans = solve1(n);
}
else
{
ans = solve2(n);
}
}
}
else
{
if (cbb == 'B')
{
ans = 1;
}
else
{
if (cba == 'B')
{
ans = solve1(n);
}
else
{
ans = solve2(n);
}
}
}
cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int res=0;
bool zf=0;
char c;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c=='-')zf=1;
else res=c-'0';
while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0';
if(zf)return -res;
return res;
}
signed main(){
int n=read();
string s;cin>>s;s=' '+s;
int Q=read(),f=0;
while(Q--){
int t=read(),x=read(),y=read();
if(t==1){
if(f){
if(x<=n){
x+=n;
}
else{
x-=n;
}
if(y<=n){
y+=n;
}
else{
y-=n;
}
}
swap(s[x],s[y]);
}
else{
f^=1;
}
}
if(!f){
for(register int i=1;i<=n+n;++i){
putchar(s[i]);
}
puts("");
}
else{
for(register int i=n+1;i<=n+n;++i){
putchar(s[i]);
}
for(register int i=1;i<=n;++i){
putchar(s[i]);
}
puts("");
}
return 0;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
int n,a,minv=1<<30,maxv,T,b,q,num;
string s,t,tt;
char tmp1,tmp2;
signed main()
{
cin>>n>>s>>q;
for(int i=1;i<=q;i++)
{
scanf("%lld%lld%lld",&T,&a,&b);
if(T==1)
{
if(num==1)
{
if(a>n)
a-=n;
else
a+=n;
if(b>n)
b-=n;
else
b+=n;
}
swap(s[a-1],s[b-1]);
}
else
num=1-num;
}
if(num==1)
{
t=s.substr(0,n);
tt=s.substr(n,n);
s=tt+t;
}
cout<<s;
return 0;
} |
#include <bits/stdc++.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include <numeric>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define Rep(i,n) for(int i = 1 ; i <= n ; i++)
const int maxn=2e5+5;
const ll mod = 1e9 + 7;
const bool DEBUG = 0;
int main(){
int a,b,ans;
cin >> a >> b;
int sum = 500500;
Rep(i,a){
if( i < a ){
cout << i << ' ';
sum = sum - i;
}else{
cout << sum << ' ';
}
}
sum = 500500;
Rep(i,b){
if( i < b){
cout << -i << ' ';
sum = sum - i;
}else{
cout << -sum << ' ';
}
}
return 0;
} | #include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")
#define sync ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
#define unq(a) sort(all(a));a.resize(unique(all(a)) - a.begin())
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define endl '\n'
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//mt19937 rng(0);
using pii = pair<int , int>;
int inline max(const int x, const int y){return (x > y ? x : y);}
int inline min(const int x, const int y){return (x < y ? x : y);}
int inline abs(const int x){return (x < 0 ? -x : x);}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
sync
int t = 1;
//cin >> t;
while(t--){
int n, m;
cin >> n >> m;
bool f = 0;
if (n < m){
swap(n , m);
f = 1;
}
vector<int> a(n), b(m);
ll sm = 0;
for (int i = 0; i < n; i++){
a[i] = i + 1;
sm += a[i];
}
for (int i = 0; i < m - 1; i++){
b[i] = -(i + 1);
sm += b[i];
}
b[m - 1] = -sm;
for (int i = 0; i < n + m; i++){
if (i < n){
cout << a[i] * (f ? -1 : 1) << " ";
}
else{
cout << b[i - n] * (f ? -1 : 1) << " ";
}
} cout << endl;
}
cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s ";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
mt19937 rand_mt(42);
const int N = 20;
class Genome{
public:
Genome(const vector<string>& S): sequences(S){
// auto dna = init_random();
auto dna = init_bf();
for(int i=0; i<N; i++){
dna_w[i] = dna[i];
for(int j=0; j<N; j++){
dna_h[j] += dna[i][j];
}
}
for(int i=0; i<N; i++){
dna_w[i] += dna_w[i];
dna_h[i] += dna_h[i];
}
}
array<string, N> init_random(){
auto dna = array<string, N>();
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
dna[i] += 'A' + rand_mt() % 8;
}
}
return dna;
}
array<string, N> init_bf(){
auto dna = array<string, N>();
int i = 0;
for(auto s : sequences){
for(int i=0; i<N; i++){
if(N - dna[i].size() < s.size()) continue;
dna[i] += s;
break;
}
}
return dna;
}
int evaluate(){
const auto M = sequences.size();
int c = 0;
for(auto s : sequences){
bool hasSeq = false;
for(auto dw : dna_w){
if(dw.find(s) != string::npos){ hasSeq = true; break;}
}
if (hasSeq){ c++; continue;}
for(auto dw : dna_h){
if(dw.find(s) != string::npos){ hasSeq = true; break;}
}
if (hasSeq){ c++; continue;}
}
int d = 0;
auto dna = getDNA();
for(auto s : dna){
d += count(s.begin(), s.end(), '.');
}
if(c < M){
return int(1e+8 * (c / (double) M));
}else{
return int(1e+8 * ((2 * N * N) / (double) (2 * N * N - d)));
}
}
array<string, N> getDNA(){
auto dna = array<string, N>();
for(int i=0; i<N; i++){
dna[i] = dna_w[i].substr(0, 20);
}
return dna;
}
void print(){
auto dna = getDNA();
for(int i=0; i<N; i++){
cout << dna[i] << endl;
}
}
public:
array<string, N> dna_w;
array<string, N> dna_h;
const vector<string> sequences;
};
int main()
{
int M;
cin >> M >> M;
vector<string> S(M, "");
for(int i=0; i<M; i++) cin >> S[i];
sort(S.begin(), S.end(), [](const string& s1, const string& s2){return s1.size() > s2.size();});
auto genome = Genome(S);
genome.print();
// cerr << "score = "<< genome.evaluate() << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
class Timer {
public:
Timer() { start_time_ = std::chrono::steady_clock::now(); }
int64_t elapsedMilliSeconds() const {
auto elapsed = std::chrono::steady_clock::now() - start_time_;
return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
}
private:
std::chrono::steady_clock::time_point start_time_;
};
unsigned int randxor() {
static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
unsigned int t;
t = (x ^ (x << 11));
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
int main() {
int32_t N, M;
cin >> N >> M;
vector<string> s(M);
for (string &i : s) {
cin >> i;
}
Timer timer;
// 操作
// (1)まだ載せてないものからランダムに選んで載せる
// (2)すでに載っているものを取り除く
vector<string> ans(N, string(N, '.'));
// 各マスが何個の文字列へ影響しているか
vector<vector<int32_t>> dependence_num(N, vector<int32_t>(N, 0));
// まだ載せてないもの
vector<int32_t> remainder(M);
iota(remainder.begin(), remainder.end(), 0);
constexpr int64_t TIME_LIMIT = 1800;
while (true) {
int64_t curr_time = timer.elapsedMilliSeconds();
if (curr_time >= TIME_LIMIT) {
break;
}
int64_t rand = randxor();
if (!remainder.empty() && true) {
// まだ載せてないもんをランダムに選んで載せる
int32_t index = randxor() % remainder.size();
int32_t x = randxor() % N;
int32_t y = randxor() % N;
int32_t h_or_v = randxor() % 2;
const string &curr_str = s[remainder[index]];
bool ok = true;
for (int32_t i = 0; i < curr_str.size(); i++) {
int32_t curr_x = (h_or_v == 0 ? (x + i) % N : x);
int32_t curr_y = (h_or_v == 0 ? y : (y + i) % N);
if (ans[curr_y][curr_x] != '.' && ans[curr_y][curr_x] != curr_str[i]) {
ok = false;
break;
}
}
if (ok) {
// 書き込む
for (int32_t i = 0; i < curr_str.size(); i++) {
int32_t curr_x = (h_or_v == 0 ? (x + i) % N : x);
int32_t curr_y = (h_or_v == 0 ? y : (y + i) % N);
ans[curr_y][curr_x] = curr_str[i];
dependence_num[curr_y][curr_x]++;
}
remainder[index] = remainder.back();
remainder.pop_back();
}
} else {
}
}
for (int32_t i = 0; i < N; i++) {
cout << ans[i] << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n;
cin>>n;
vector<long long> a(n),b(n),v(1000,0);
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++) cin>>b[i];
for(int i=0;i<n;i++)
{
for(int j=0;j<a[i]-1;j++)v[j]=1;
for(int j=b[i];j<1000;j++)v[j]=1;
}
int count=0;
for(int i=0;i<1000;i++)
{
if(v[i]==0){count++;}
}
cout<<count;
} | #include<bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define ll long long
#define mod 1000000007
#define mod2 998244353
void fast(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
}
int main() {
fast();
int n;
cin>>n;
int a[n];
int b[n];
int maxim=0;
int minim=1001;
for(int i=0;i<n;i++){
cin>>a[i];
maxim=max(a[i],maxim);
}
for(int i=0;i<n;i++){
cin>>b[i];
minim=min(b[i],minim);
}
if(minim>=maxim){
cout<<minim-maxim+1<<"\n";
}else{
cout<<0<<"\n";
}
} |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long int
#define fast ios_base::sync_with_stdio(false)
#define fast_input cin.tie(NULL)
#define fast_output cout.tie(NULL)
#define vi vector<long long int>
#define vii vector<vi>
#define vp vector<pair<ll,ll>>
#define vpp vector<pair<ll,pair<ll,ll>>>
#define pb push_back
#define pu push
#define po pop
#define up(v,x) upper_bound(v.begin(),v.end(),x)
#define lo(v,x) lower_bound(v.begin(),v.end(),x)
#define ups(v,x) v.upper_bound(x)
#define los(v,x) v.loer_bound(x)
#define pa pair<long long int ,long long int>
#define f(a,x,b) for(int a=x;a<b;a++)
#define fr(a,x,b) for(int a=x;a>=b;a--)
#define EACH(a,x) for(auto a:x)
#define sort(x) sort(x.begin(),x.end())
#define rev(x) reverse(x.begin(),x.end())
#define sz(a) (int)a.size()
#define mod 998244353
#define F first
#define S second
#define um unordered_map<ll,ll>
#define ordered_set tree<pa, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
vi v(1009),v1(1009);
vii dp(1009,vi(1009,-1));
ll sol(ll pos1,ll pos2)
{
if(pos2==0)
{
return pos1;
}
if(pos1==0)
{
return pos2;
}
if(dp[pos1][pos2]!=-1)
{
return dp[pos1][pos2];
}
ll x=mod;
if(v[pos1-1]==v1[pos2-1])
{
x=min(x,sol(pos1-1,pos2-1));
}
else
{
x=min({x,sol(pos1-1,pos2)+1,sol(pos1,pos2-1)+1,sol(pos1-1,pos2-1)+1});
}
return dp[pos1][pos2]=x;
}
void solve(){
ll n,m;
cin>>n>>m;
f(i,0,n)
{
cin>>v[i];
}
f(i,0,m)
{
cin>>v1[i];
}
cout<<sol(n,m);
}
int main() {
fast;
fast_input;
fast_output;
// ll t;
// cin>>t;
// f(i,0,t)
// {
// cout<<"Case #"<<i+1<<":"<<" ";
solve();
// }
return 0;
} | #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <queue>
#include <stack>
#include <cstdlib>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <stdio.h>
#include <ctype.h>
#include <random>
#include <string.h>
#include <unordered_map>
#include <cstdio>
#include <climits>
using namespace std;
#define all(vec) vec.begin(),vec.end()
typedef long long ll;
ll gcd(ll x, ll y) {
if (y == 0)return x;
return gcd(y, x%y);
}
ll lcm(ll x, ll y) {
return x / gcd(x, y)*y;
}
ll kai(ll x, ll y, ll m) {
ll res = 1;
for (ll i = x - y + 1; i <= x; i++) {
res *= i; res %= m;
}
return res;
}
ll mod_pow(ll x, ll y, ll m) {
ll res = 1;
while (y > 0) {
if (y & 1) {
res = res * x % m;
}
x = x * x % m;
y >>= 1;
}
return res;
}
ll comb(ll x, ll y, ll m) {
if (y > x)return 0;
return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m;
}
int n;
string s, t;
queue<int> que;
ll ans;
signed main() {
std::random_device rnd;
std::mt19937_64 mt(rnd());
cin >> n >> s >> t;
for (int i = 0; i < n; i++)if (s[i] == '1')que.push(i);
for (int i = 0; i < n; i++) {
if (!que.empty() && que.front() == i)que.pop();
if (s[i] == t[i])continue;
if (que.empty()) {
cout << -1 << endl;
return 0;
}
int p = que.front();
ans += p - i;
que.pop();
if (s[i] == '0')s[i] = '1', s[i + 1] = '0', s[p] = '0';
else s[i] = '0', s[i + 1] = '0', s[p] = '0';
}
cout << ans << endl;
} |
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
#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
const int mod = 1e9 + 7;
vector<pair<int, ll> > E[200001];
ll W[200001];
int kazu[60];
void dfs(int u, int mae) {
for (auto v : E[u]) {
if (v.first != mae) {
W[v.first] = W[u] ^ v.second;
dfs(v.first, u);
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep(i, N - 1) {
int u, v;
ll w;
cin >> u >> v >> w;
E[u].pb({ v,w });
E[v].pb({ u,w });
}
dfs(1, 0);
ll kotae = 0;
rep1(i, N) {
kotae += W[i] % mod * (N - 1) % mod;
}
kotae %= mod;
rep1(i, N) {
rep(j, 60) if (W[i] >> j & 1) {
kazu[j]++;
}
}
rep(j, 60) {
kotae -= ((1ll << j) % mod) * kazu[j] % mod * (kazu[j] - 1) % mod;
}
co((kotae% mod + mod) % mod);
Would you please return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template<class T> using VE = vector<T>;
template<class T> using P = pair<T, T>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define REP(i,k,n) for(int i=(k);i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define output(x,y) cout << fixed << setprecision(y) << x << endl;
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; }
ll Gcd(ll a, ll b) { return b ? Gcd(b, a % b) : a; }
ll Lcm(ll a, ll b) { return a / Gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
int main(){
int n, x;
cin >> n >> x;
string s;
cin >> s;
rep(i, n) {
if (s[i] == 'o') x++;
else if (s[i] == 'x' && x > 0) x--;
}
cout << x << endl;
return 0;
} |
/*
/> フ
| _ _|
/`ミ _x 彡
/ |
/ ヽ ?
/ ̄| | | |
| ( ̄ヽ__ヽ_)_)
\二つ
*/
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pf push_front
#define F first
#define S second
#define SS stringstream
#define sqr(x) ((x)*(x))
#define m0(x) memset(x,0,sizeof(x))
#define m1(x) memset(x,63,sizeof(x))
#define CC(x) cout << (x) << endl
#define AL(x) x.begin(),x.end()
#define pw(x) (1ull<<(x))
#define NMSL cout << "NMSL" << endl;
#define debug(x) cout << #x << ": " << x << endl;
#define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl;
#define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl;
#define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl;
#define fio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define in128 __int128_t
using namespace std;
const int N = 2e5+10;
const int INF = numeric_limits<int>::max() / 4;
const double EPS = 1e-3;
const long double PI = 3.14159265358979323846;
int save[30];
signed main()
{
fio
string s;
cin>>s;
int ans=0;
int n=s.length();
for(int i=n-1;i>0;--i)
{
if(s[i]==s[i-1])
{
ans+=n-1-i-save[s[i]-'a'];
memset(save,0,sizeof(save));
save[s[i]-'a']=n-i;
}else save[s[i]-'a']++;
// debug4(i,ans,s[i],save[0])
}
cout<<ans;
return 0;
}
| //#define MULTICASES
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(i=(a);i<=(b);++i)
#define per(i,a,b) for(i=(a);i>=(b);--i)
#define REP(i,a,b) for(i=(a);i< (b);++i)
#define PER(i,a,b) for(i=(a);i> (b);--i)
#define ERR(...) fprintf(stderr,__VA_ARGS__)
template<class T>inline void cmn(T &x,const T &a){if(a<x)x=a;}
template<class T>inline void cmx(T &x,const T &a){if(x<a)x=a;}
template<class T>inline bool bmn(T &x,const T &a){if(a<x){x=a;return true;}else return false;}
template<class T>inline bool bmx(T &x,const T &a){if(x<a){x=a;return true;}else return false;}
const ll p=998244353ll,pp=p*p;const int ps2=p-2ll;
inline void add(ll &x){if(x>= p)x-=p ;}
inline void sub(ll &x){if(x<0ll)x+=p ;}
inline void adp(ll &x){if(x>=pp)x-=pp;}
inline void sup(ll &x){if(x<0ll)x+=pp;}
inline ll expo(ll a,int b=ps2,ll m=p){
ll s=1ll;
while(b){
if(b&1)s=s*a%p;
b>>=1; a=a*a%p;
}
return s;
}
inline void ac();
//inline void in();int main(){in();int t;scanf("%d",&t);while(t--)ac();return 0;}
//int main(){int t;scanf("%d",&t);while(t--)ac();return 0;}
int main(){ac();return 0;}
map<ll,int>M;
inline void ac(){
int n,a;ll S=0ll,Z=0ll;
scanf("%d",&n);
M[0ll]=1;
while(n--){
scanf("%d",&a);
if(n&1)S+=a;else S-=a;
Z+=(M[S]++);
}
printf("%lld\n",Z);
}
/*
0. int overflow, array bounds
1. special cases (n=1? n=0?)
2. do smth instead of nothing and stay organized
3. WRITE STUFF DOWN
4. DON'T GET STUCK ON ONE APPROACH
5. STAY CALM AND DON'T GET EXPLODED
*/ |
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
#define eps 1e-9
#define eq(x,y) (fabs((x)-(y)) < eps)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int>pii;
const ll mod = 1e9+7;
long double PI = acosl(-1);
const ll infl = 1e18;
//const int inf = 2e9;
const int nmax = 1e6+5;
const int MAXLG = log2(nmax)+1;
//mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//typedef tree< int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ost;
int main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int tc;
cin>>tc;
while(tc--){
int n;
cin>>n;
vector<int>v(n);
for(int &x : v) cin>>x;
if(n%2) cout<<"Second\n";
else{
map<int, int>cnt;
for(int x : v) cnt[x]++;
bool mara = false;
for(auto z : cnt){
if(z.second%2) mara=true;
}
if(mara) cout<<"First\n";
else cout<<"Second\n";
}
}
}
/*
*/
| #include <bits/stdc++.h>
#include <fstream>
#include <bitset>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF=2147483647;
// 9,223,372,036,854,775,807
const ll INFL = 9223372036854775807;
int main(){
ll n;
cin >> n;
vector<pair<string,int>> s(n);
for(int i=0;i<n;i++){
string tmp;
cin >> tmp;
if(tmp[0] == '!'){
s[i] = {tmp,1};
}
else{
tmp.insert(0,"!");
s[i] = {tmp,0};
}
}
sort(s.begin(),s.end());
string ans;
for(int i=0;i<n-1;i++){
if(s[i].first==s[i+1].first&&s[i].second!=s[i+1].second){
s[i].first.erase(s[i].first.begin());
ans = s[i].first;
cout << ans << endl;
return 0;
}
}
cout << "satisfiable " << endl;
return 0;
} |
#line 1 "main_b.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
/* template start */
#line 2 "/home/imperi/cp-library/lib/utility/type_alias.hpp"
#line 4 "/home/imperi/cp-library/lib/utility/type_alias.hpp"
using u64 = std::uint64_t;
using u32 = std::uint32_t;
using u16 = std::uint16_t;
using u8 = std::uint8_t;
using i64 = std::int64_t;
using i32 = std::int32_t;
using i16 = std::int16_t;
using i8 = std::int8_t;
using usize = std::size_t;
using isize = std::ptrdiff_t;
constexpr i64 operator"" _i64(unsigned long long num) { return i64(num); }
constexpr u64 operator"" _u64(unsigned long long num) { return u64(num); }
#line 33 "main_b.cpp"
#define rep(i, a, b) for (i64 i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()
#ifdef LOCAL
#define debug(...) \
std::cerr << "LINE: " << __LINE__ << " [" << #__VA_ARGS__ << "]:", \
debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
std::cerr << " " << h;
if (sizeof...(t) > 0) std::cerr << " :";
debug_out(t...);
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
return os << pa.first << " " << pa.second;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
for (std::size_t i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
return a > b && (a = b, true);
}
template <typename Num, typename Func = std::multiplies<Num>>
constexpr Num mypow(Num a, u64 b, Num id = 1, Func mul = Func()) {
if (b == 0) return id;
Num x = id;
while (b > 0) {
if (b & 1) x = mul(x, a);
a = mul(a, a);
b >>= 1;
}
return x;
}
/* template end */
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
i64 n;
std::cin>>n;
std::vector<i64> a(n);
for(auto&& e:a)std::cin>>e;
std::sort(all(a));
long double ans = 1e18;
long double sum = 0;
i64 coef = n;
chmin(ans,(long double)n*a[n-1]/2);
for(i64 i = n-2;i>=0;i--){
sum += a[i+1];
coef-=2;
chmin(ans,sum + (long double)coef*a[i]/2);
}
std::cout<<std::setprecision(10)<<ans/n<<"\n";
return 0;
}
| #include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<math.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<vector>
#define IOS ios::sync_with_stdio(false);cin.tie();cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pll;
const int N=1e5+10;
const int mod=1e9+7;
double a[N],s[N];
int main()
{
IOS;
int n;
cin>>n;
double n1=n*1.0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
s[i]=s[i-1]+a[i];
double sum=1e18;
for(int i=1;i<=n;i++)
{
double w=a[i]/2;
double w1=(s[n]-s[i])-(n-i-i)*w;
sum=min(sum,w1);
//cout<<sum/n1<<endl;
}
printf("%.6lf",sum/n1);
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
const int P = 998244353;
const int N = 200000;
const int IV2 = (P + 1) >> 1;
inline int norm(int x) {if( x >= P ) x -= P; return x;}
int pw2[N + 5], ipw2[N + 5];
int a[N + 5], n;
int main() {
scanf("%d", &n);
for(int i=1;i<=n;i++) scanf("%d", &a[i]); std::sort(a + 1, a + n + 1);
pw2[0] = ipw2[0] = 1;
for(int i=1;i<=n;i++) {
pw2[i] = norm(pw2[i - 1] << 1);
ipw2[i] = (ll)ipw2[i - 1] * IV2 % P;
}
int ans = 0, t = 0;
for(int i=n;i>=1;i--) {
ans = (ans + (ll)a[i] * a[i]) % P;
ans = (ans + (ll)t * a[i] % P * ipw2[i]) % P;
t = (t + (ll)a[i] * pw2[i - 1]) % P;
}
printf("%d\n", ans);
} | #include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a, 0, sizeof(a))
#define full(a) memset(a, 0x3f, sizeof(a))
#define mset(a, b) memset(a, b, sizeof(a))
#define cpy(a,b) memcpy(a, b, sizeof(a))
#define fornext(x, i) for(signed i = hd[x], y = ver[i]; i; i = nxt[i], y = ver[i])
#define Rep(i, a, b) for(signed i = (signed)(a); i <= (signed)(b); ++i)
#define dRep(i, a, b) for(signed i = (signed)(a); i >= (signed)(b); --i)
#define IOset(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout);
#define pb push_back
#define It iterator
#define endl '\n'
#define un unsigned
#define int ll
#define ll long long
#define db double
#define rept cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
#define dbg cout<<"c "
#define dbug(x) cerr << #x " = " << (x) << endl
template<typename _T> inline void _prt(_T _d) { cerr << _d << ' '; }
template <typename _T, typename ...Args> inline void _prt(_T _d, Args ..._a) { _prt(_d), _prt(_a...); }
template <typename _T, typename ...Args> inline void prt(_T _d, Args ..._a) { _prt(_d), _prt(_a...), cerr<<endl; }
template <typename _T, typename ...Args> inline void prt(_T _d) { cerr << _d <<endl; }
template<typename _T> inline void rd(_T &_d) {
signed _f; char _c; _f = 1,_d = 0; while (_c = getchar(), !isdigit(_c)) if(_c=='-') _f=-1;
while (isdigit(_c)) _d = _d * 10 + _c - '0', _c = getchar();
_d=_f*_d;
}
template <typename _T, typename ...Args> inline void rd(_T &_d, Args &..._a) { rd(_d); rd(_a...); }
const int N=2e5+5,M=998244353;
int n,a[N];
inline void ml(int &x,int v) {
x=1LL*x*v%M;
}
inline int md(int x) {
return x>=M?x-M:x;
}
inline void add(int &x,int v) {
x+=v,x=x>=M?x-M:x,x=x<0?x+M:x;
}
signed main() {
rd(n);
Rep(i,1,n) rd(a[i]);
sort(a+1,a+n+1);
int ans=0,sum=0;
Rep(i,1,n) {
add(sum,a[i-1]);
add(ans,a[i]*(sum+a[i])%M);
sum=md(sum+sum);
}
cout<<ans<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int a[65540];
template<class T>inline void read(T &x){
x=0;register char c=getchar();register bool f=0;
while(!isdigit(c))f^=c=='-',c=getchar();
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
if(f)x=-x;
}
int main(){
int n,t;
cin>>n;
int min1=-INT_MAX,min2=-INT_MAX;
int ans1=0,ans2=0;
int l=pow(2,n)/2,r=pow(2,n);
for(int i=1;i<=l;i++){
read(t);
if(min1<t)
ans1=i,min1=t;
}
for(int i=l+1;i<=r;i++){
read(t);
if(min2<t)
ans2=i,min2=t;
}
if(min1>min2)cout<<ans2<<endl;
else cout<<ans1<<endl;
}
| #include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a),i##END=(b);i<=i##END;i++)
#define Rof(i,b,a) for(int i=(b),i##END=(a);i>=i##END;i--)
#define go(u) for(int i=head[u];i;i=nxt[i])
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
const int N=1e2+10,M=1e4+10;
char s[N];int a[N],b[M][N];
signed main(){
int n=read()+1;
scanf("%s",s+1);
For(i,1,n)a[i]=read();
int k=1e5;
For(i,2,n)k=min(k,abs(a[i]-a[i-1]));
printf("%d\n",k);
For(i,1,k){
For(j,1,n){
printf("%d ",(a[j]+i-1)/k);
}
puts("");
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int N=3005,mod=998244353;
inline void add(int &a,int b){a+=b;if(a>=mod)a-=mod;}
int dp[N][N];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
dp[0][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=n;j>=1;j--)
{
if(j) add(dp[i][j],dp[i-1][j-1]);
if(j*2<=n) add(dp[i][j],dp[i][j*2]);
}
}
printf("%d\n",dp[n][k]);
return 0;
} | #include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
int popcount(ll bits) {
bits = (bits & 0x5555555555555555LL) + (bits >> 1 & 0x5555555555555555LL);
bits = (bits & 0x3333333333333333LL) + (bits >> 2 & 0x3333333333333333LL);
bits = (bits & 0x0f0f0f0f0f0f0f0fLL) + (bits >> 4 & 0x0f0f0f0f0f0f0f0fLL);
bits = (bits & 0x00ff00ff00ff00ffLL) + (bits >> 8 & 0x00ff00ff00ff00ffLL);
bits = (bits & 0x0000ffff0000ffffLL) + (bits >> 16 & 0x0000ffff0000ffffLL);
return (bits & 0x00000000ffffffffLL) + (bits >> 32 & 0x00000000ffffffffLL);
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
input(n, m);
vector<vector<P>> lm(n);
rep(i, 0, m) {
int x, y, z;
input(x, y, z);
x--;
lm[x].emplace_back(y, z);
}
int bits = 1 << n;
vector<ll> dp(bits, 0);
dp[0] = 1;
rep(bit, 0, bits) {
vector<int> cnt(n + 1, 0);
rep(i, 0, n) {
if ((bit >> i) & 1) cnt[i + 1]++;
}
rep(i, 0, n) {
cnt[i + 1] += cnt[i];
}
rep(i, 0, n) {
if ((bit >> i) & 1) continue;
bool ok = true;
for (auto [y, z] : lm[popcount(bit)]) {
int c = cnt[y] + (i + 1 <= y);
if (c > z) {
ok = false;
break;
}
}
if (ok) {
dp[bit | 1 << i] += dp[bit];
}
}
}
print(dp[bits - 1]);
return 0;
}
|
// Author: old_school
// Created: 20.02.2021 17:30:29
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
using namespace std;
#define lld long long int
#define ld long double
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define F first
#define S second
#define nl '\n'
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define sz(c) (c).size()
#define tr(x,a) for(auto &a : x)
#define psnt(x,a) (x).find(a)!=(x).end()
#define vpsnt(x,a) find(all(x),a)!=(x).end()
#define MOD 1000000007
#define tod 100005
#define pi 3.1415926536
#define itr(i,a,b) for(lld i=a;i<=b;i++)
#define itrn(i,a,b) for(lld i=a;i>=b;i--)
#define iot(n) for(lld i=0;i<n;i++)
#define pls(n,arr) lld arr[n]; iot(n) cin>>arr[i];
#define bye fflush(stdout)
typedef pair<lld,lld> pii;
typedef pair<string,lld> psi;
template <typename T>
bool mycomp(T x,T y){
return (x==y); //give your condition here
}
bool paircomp(const pair<lld,lld> &x,const pair<lld,lld> &y){
return x.second<y.second;
}
vector<pair<lld,pair<lld,lld> > > adj[tod];
void dijkstra(lld V,lld source,lld dist[]){
priority_queue<pair<lld,lld> ,vector<pair<lld,lld> >,greater<pair<lld,lld> > > pq; //minheap
for(lld i=0;i<=V;i++)
dist[i]=-1;
dist[source]=0;
pq.push(make_pair(dist[source],source));
while(!pq.empty()){
lld u=pq.top().second;
pq.pop();
for(auto &p:adj[u]){
lld v=p.first,weight=p.second.first,lx=p.second.second;
lld z=dist[u]/lx;
lld ex=0;
if(dist[u]%lx){
z++;
}
z*=lx;
ex=z-dist[u];
if (dist[v]==-1)
{
dist[v] = dist[u] + ex+weight;
pq.push(make_pair(dist[v], v));
}
else if(dist[v]>dist[u]+ex+weight){
dist[v]=dist[u]+ex+weight;
pq.push(make_pair(dist[v], v));
}
}
}
}
void solve(){
lld n,m,x,y; cin>>n>>m>>x>>y;
iot(m){
lld a,b,t,k; cin>>a>>b>>t>>k;
adj[a].pb({b,{t,k}});
adj[b].pb({a,{t,k}});
}
lld dist[n+1];
dijkstra(n,x,dist);
cout<<dist[y];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
lld t=1;// cin>>t;
while(t--){
solve();
cout<<endl;
}
}
| #pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
using pos = pair<int, int>;
bool canMoveWith1Move(const pos& from, const pos& to){
return (
(from.first + from.second == to.first + to.second) ||
(from.first - from.second == to.first - to.second) ||
(abs(from.first - to.first) + abs(from.second - to.second) <= 3)
);
}
bool canMoveWith2Moves(const pos& from, const pos& to){
auto color = [](const pos& p) -> int {
return ((p.first + p.second) % 2 + 2) % 2;
};
bool flg = false;
for (int dx = -3; dx <= 3; ++dx){
for (int dy = -3; dy <= 3; ++dy){
pos tmp = {from.first + dx, from.second + dy};
flg |= (canMoveWith1Move(from, tmp) && canMoveWith1Move(tmp, to));
}
}
return (
(color(from) == color(to)) ||
(abs(from.first + from.second - to.first - to.second) <= 4) ||
(abs(from.first - from.second - to.first + to.second) <= 4) ||
(flg)
);
}
int solve(const pos& from, const pos& to){
if (from == to) return 0;
if (canMoveWith1Move(from, to)) return 1;
if (canMoveWith2Moves(from, to)) return 2;
return 3;
}
int main(void){
int x, y;
cin >> x >> y;
pos from = {x, y};
cin >> x >> y;
pos to = {x, y};
cout << solve(from, to) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define test int t;cin>>t;while(t--)
#define REP(i,n) for (int i = 1; i <= n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define ll long long
#define ld long double
#define int ll
#define pb push_back
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define ff first
#define ss second
#define INF 1000000000
#define HINF 1000000000000000
#define mem(a,b) memset(a,b,sizeof(a))
#define arrin(x,n) int x[n]; for(int o=0;o<n;o++) cin>>x[o]
#define arrout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define vecin(x,n); vi x;int hool; for(int o=0;o<n;o++) {cin>>hool; x.pb(hool);}
#define vecout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define all(x) x.begin(),x.end()
#define deb(x) cout<<#x<<'='<<x<<endl
#define deb2(x,y) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<endl
#define deb3(x,y,z) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<" "<<#z<<'='<<z<<endl
#define debarr(arr,n) for(int o=0;o<n;o++) deb2(o,arr[o])
#define debarrall(arr) debarr(arr,arr.size())
#define IO freopen("input.txt", "r", stdin); freopen("output.txt", "w+", stdout)
const double pi = 3.14159265358979323846;
int powersimple(int a, int b){//a^b
int res=1;
while(b>0){
if(b&1)
{res=(res*a);
b--;}
a=(a*a);
b>>=1;
}
return res;
}
int lcm(int a,int b){
return a*b/__gcd(a,b);
}
int ncr(int n,int k)
{
int ans=1;
if(k>n-k)
k=n-k;
for(int i=1;i<=k;i++)
ans*=(n-i+1),ans/=i;
return ans;
}
int power(int x,int y,int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
int ncrModPFermat(int n,int r, int p)
{
if (r == 0)
return 1;
int fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
///////////////END OF TEMPLATE//////////////////
void solve()
{
int i=0,j=0,k=0,m,n,p,sum=0,cnt=0;
cin>>n>>k;
int arr[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>arr[i][j];
vi po;
for(i=1;i<n;i++)
{
po.pb(i);
}
do{
sum=0;
sum+=arr[0][po.front()]+arr[po.back()][0];
for(i=0;i+1<po.size();i++)
{
sum+=arr[po[i]][po[i+1]];
}
cnt+=(sum==k);
}
while(next_permutation(all(po)));
cout<<cnt<<endl;
}
int32_t main() {
fast
//IO;
//cout<<fixed<<setprecision(15);
//test
{
solve();
}
return 0;
}
| #include<iostream>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<unordered_set>
#include<set>
#include<unordered_map>
#include<map>
#include<algorithm>
#include<climits>
#include<iomanip>
#include<cstdio>
using namespace std;
#define ll long long int
#define ull unsigned long long int
/********************************/
void dfs(int index, vector<vector<int>>& distance, int n, int k, bool visited[], ll& ans, ll currentSum) {
visited[index] = true;
bool isEmpty = true;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
isEmpty = false;
dfs(i, distance, n, k, visited, ans, currentSum + distance[index][i]);
}
}
if (isEmpty) {
currentSum += distance[index][0];
if (currentSum == k) ans++;
}
visited[index] = false;
}
void solve(vector<vector<int>>& distance, int n, int k) {
ll ans = 0;
bool visited[n];
for (int i = 0; i < n; i++) {
visited[i] = false;
}
dfs(0, distance, n, k, visited, ans, 0LL);
cout << ans << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t = 1;
// cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<vector<int>> distance;
for (int i = 0; i < n; i++) {
vector<int> row;
for (int j = 0; j < n; j++) {
int a;
cin >> a;
row.push_back(a);
}
distance.push_back(row);
}
solve(distance, n, k);
}
}
|
#include <bits/stdc++.h>
using namespace std;
//A.at(i) = iの約数の個数 = Π(素因数の乗数+1)
//しかし1から10^5まで全て因数分解するのはたぶん間に合わない
//☆違う
//例えば6の約数の個数は4だが、A.at(2) = A.at(3) = 2なのでA.at(6) = 3
//つまり「A.at(n) = A.at(nの約数iのうち、A.at(i)がもっとも大きいもの) + 1
//DP?
int main() {
int N;
cin >> N;
vector<int> A(N + 1, 2);
A.at(1) = 1;
for (int i = 4; i <= N; i++){
int root = sqrt(i);
for (int j = 2; j <= root; j++){
if(i % j == 0 && A.at(i / j) + 1 > A.at(i)){
A.at(i) = A.at(i / j) + 1;
}
}
}
for (int i = 1; i <= N; i++){
cout << A.at(i);
if(i != N){
cout << " ";
}else{
cout << endl;
}
}
} | #include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define sqr(x) ((x) * (x))
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;
const int MOD = 1e9 + 7;
const LL MX = 30;
const LL INF = 2e9;
int n, a[MX], ans;
void rec(int i, int xr)
{
if(i == n)
{
ans = min(ans, xr);
return;
}
int vor = 0;
loop(j,i,n)
{
vor |= a[j];
rec(j+1,vor ^ xr);
}
}
int main()
{
//ofstream out("output.txt");
//ifstream in("input.txt");
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
loop(i,0,n) cin>>a[i];
ans = INF;
rec(0,0);
cout<<ans<<'\n';
return 0;
}
|
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long int ll;
#define vec vector<int>
#define pb push_back
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 i,j,k,l,m,t;
{
ll a[2][2];
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<(a[0][0]*a[1][1])-(a[1][0]*a[0][1]);
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << a * d - b * c << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
cin.sync_with_stdio(false);
ll n,k;
cin>>n>>k;
ll cnt[n];
memset(cnt,0,sizeof(cnt));
ll i,j;
for(i=0;i<n;i++) {
ll x;
cin>>x;
cnt[x]+=1;
}
ll boxes[k];
memset(boxes,-1,sizeof(boxes));
i=0;
j=0;
while(i<n) {
ll flag=0;
while(cnt[i]>0) {
if(boxes[j]+1==i || boxes[j]==i) {
boxes[j]=i;
j=(j+1)%k;
cnt[i]-=1;
}
else {
j=0;
if(boxes[j]+1==i || boxes[j]==i) {
boxes[j]=i;
j=(j+1)%k;
cnt[i]-=1;
}
else {
flag=1;
break;
}
}
}
i+=1;
j=0;
if(flag==1)
break;
}
ll ans=0;
for(i=0;i<k;i++)
ans=ans+boxes[i]+1;
cout<<ans;
}
| #include<bits/stdc++.h>
#define N 300005
#define ll long long
#define rep(i, l, r) for(int i=l; i<=r; ++i)
#define per(i, r, l) for(int i=r; i>=l; --i)
using namespace std;
int n, k;
int a[N], c[N];
int main(){
scanf("%d%d", &n, &k);
rep(i, 1, n){
scanf("%d", &a[i]);
++c[a[i]];
}
c[0]=min(c[0], k);
int ans=c[0];
rep(i, 1, n){
c[i]=min(c[i], c[i-1]);
ans+=c[i];
}
printf("%d", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin>>s;
bool ok=true;
for ( int i=0; i<s.size(); i++ ) {
if ( isupper(s[i]) ) {
if ( i%2==0 ) ok=false;
} else {
if ( i%2 ) ok=false;
}
}
if ( ok ) cout<<"Yes\n";
else cout<<"No\n";
return 0;
} | #include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false), cin.tie(0);
#define et cout<<endl;
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
const int N = 2e5+5;
const int M = 2e5+5;
const int mod = 1e9+7;
int t, n, k;
int g[N];
char s[N];
void solve()
{
cin >> s + 1;
int len = strlen(s+1);
bool even = true, odd = true;
for(int i = 1; i <= len; i ++)
{
if(i & 1)
{
if(s[i] >= 'a' && s[i] <= 'z') ;
else even = false;
}
if(!(i & 1))
{
if(s[i] >= 'A' && s[i] <= 'Z') ;
else odd = false;
}
}
if(even && odd) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main()
{
ios
bool multi = 0; // 0 for single and 1 for multi
if(multi) cin >> t;
else t = 1;
while(t--)
{
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
// rep
#define rep(i,n) for(decltype(n) i = 0; i < (n) ; ++i)
#define orep(i,n) for(decltype(n) i = 1; i <= (n); ++i) // one rep
#define drep(i,n) for(auto i = (n)-1; i >= static_cast<decltype(i)>(0); --i) // down rep
#define srep(i,s,t) for(auto i = (s); i < (t); ++i) // set rep
// func
#define rng(x) (x).begin(),(x).end() // range
#define rrng(a) (a).rbegin(),(a).rend() // reverse range
#define isin(x,l,r) ((l) <= (x) && (x) <= (r))
#define ru(x,y) (((x)+(y)-1)/(y)) // round up
#define sz(x) int((x).size()) // size
// replace
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)
#define LL_MIN std::numeric_limits<long long>::min()
#define LL_MAX std::numeric_limits<long long>::max()
// type
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vvp = vector<vp>;
using vl = vector<ll>;
using rpq = priority_queue<int,vi,less<int>>; // 大きい順
using pq = priority_queue<int,vi,greater<int>>;// 小さい順
const int INF = 1001001001;
const ll LINF = 1010010011001001001;
template<typename T> void Yes(T flag) {
cout << (flag ? "Yes" : "No") << endl;
}
int main() {
float d; int t,s,v;cin >> v >> t >> s >> d;
Yes(not isin(d / v, t, s));
return 0;
}
| #include <iostream>
using namespace std;
int main(void) {
int V, T, S, D;
cin >> V >> T >> S >> D;
if (D > V*S || T*V > D)
{
cout << "Yes";
}
else
{
cout << "No";
}
} |
Subsets and Splits