code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);
#define l_ength size
void mul_mod(ll& a, ll b){
a *= b;
a %= MOD;
}
void add_mod(ll& a, ll b){
a = (a<MOD)?a:(a-MOD);
b = (b<MOD)?b:(b-MOD);
a += b;
a = (a<MOD)?a:(a-MOD);
}
ll dp[1111][1111],a[1111],b[1111];
int main(void){
int i,j,n,m;
std::fill(dp[0],dp[1111],INFLL);
dp[0][0] = 0ll;
std::cin >> n >> m;
for(i=0; i<n; ++i){
std::cin >> a[i];
}
for(i=0; i<m; ++i){
std::cin >> b[i];
}
for(i=0; i<=n; ++i){
for(j=0; j<=m; ++j){
if(i<n){
dp[i+1][j] = std::min(dp[i+1][j],dp[i][j]+1);
}
if(j<m){
dp[i][j+1] = std::min(dp[i][j+1],dp[i][j]+1);
}
if(i<n && j<m){
dp[i+1][j+1] = std::min(dp[i+1][j+1],dp[i][j]+((a[i]==b[j])?0:1));
}
// std::cout << i << " " << j << " " << dp[i][j] << std::endl;
}
}
std::cout << dp[n][m] << std::endl;
return 0;
}
| // Problem: E - Sequence Matching
// Contest: AtCoder - AtCoder Beginner Contest 185
// URL: https://atcoder.jp/contests/abc185/tasks/abc185_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define ll long long int
#define ii int
#define du double
#define jmp "\n"
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rp(i, s, e) for (ll i = s; i <= e; i++)
#define vl vector<ll>
#define vvl vector<vl>
#define pb push_back
#define pq_max priority_queue<ll>
#define pq_min priority_queue<ll, vi, greater<ll>>
#define printv(v) \
for (auto x : v) \
cout << x << " "; \
cout << jmp;
#define vi vector<int>
#define vb vector<bool>
#define ump unordered_map
#define SORT(v) sort(v.begin(), v.end())
#define REV(x) reverse(x.begin(), x.end())
#define all(x) x.begin(), x.end()
#define SET(x, a) memset(x, a, sizeof(x))
#define si(x) (ll) x.size()
#define ff first
#define ss second
#define I(a) \
for (auto &x : a) \
cin >> x;
#define iin insert
#define log(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
#define logarr(arr, a, b) \
for (int z = (a); z <= (b); z++) \
cout << (arr[z]) << " "; \
cout << endl; \
using namespace std;
using namespace std;
// vs tokenizer(string str,char ch) {std::istringstream var((str)); vs v; string t; while(getline((var), t, (ch))) {v.pb(t);} return v;}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
cout << *it << " = " << a << endl;
err(++it, args...);
}
const ll nax = 1e3 + 5;
ll dp[nax][nax], a[nax], b[nax];
ll n, m;
ll r(ll i, ll j)
{
if (i == -1 && j == -1)
return 0;
if (i == -1)
return j + 1;
if (j == -1)
return i + 1;
ll &mem = dp[i][j];
if (mem != -1)
return mem;
ll aa, bb, c;
aa = (a[i] != b[j]) + r(i - 1, j - 1);
bb = 1 + r(i, j - 1);
c = 1 + r(i - 1, j);
return mem = min({aa, bb, c});
}
void solve()
{
cin >> n >> m;
rep(i, n) cin >> a[i];
rep(i, m) cin >> b[i];
SET(dp, -1);
cout << r(n - 1, m - 1);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t = 1;
// cin >> t;
while (t--)
solve();
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define lb lower_bound
#define ub upper_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a,b)))
#define inf (int)(1e9)
#define endl '\n'
#define mp make_pair
#define time(s) (double(clock()-s)/double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
template<typename T> void _F(const char *name, T arg1){ cerr << name << " = " << arg1 << endl;}
template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args)
{ const char *name = strchr(names, ',');cerr.write(names, name-names) << " = " << arg1 << endl;_F(name+2, args...);}
template< typename T1, typename T2 > istream& operator>>(istream& in, pair<T1, T2> &q){ in >> q.F >> q.S; return in;}
template< typename T1, typename T2 > ostream& operator<<(ostream& out, pair<T1, T2> &q){ out << q.F << " " << q.S; return out;}
template< typename T1, typename T2 > pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F+p2.F, p1.S+p2.S};}
template< typename T1, typename T2 > pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F-p2.F, p1.S-p2.S};}
template< typename T1, typename T2 > bool operator<(pair<T1, T2> p1, pair<T1, T2> p2){ return p1 < p2 ;}
template<typename T> void Unique(vector<T> &v) {
sort(all(v)), v.resize(distance(v.begin(), unique(all(v))));
}
int sum(int n) {
if(n <= 0) return 0;
return n*(n+1)/2;
}
void solve() {
int l, r;
cin >> l >> r;
int k = l*2-1;
int s = max(k, l);
int ans = 0;
if(r >= s)ans = sum(r)-sum(s-1)-k*(r-s+1);
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
start = clock();
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int test = 1;
cin >> test;
cout << fixed << setprecision(12);
for(int i = 1; i <= test; ++i){
solve();
}
cerr << time(start);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve()
{
int l, r;
cin >> l >> r;
int x = l + l;
int y = r + 1;
ll n = y - x;
if (n < 0)
cout << "0\n";
else
{
ll ans = (n * (n + 1)) / 2;
cout << ans << "\n";
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define dump(v) \
cerr << #v ":[ "; \
for (auto macro_vi : v) { \
cerr << macro_vi << " "; \
} \
cerr << "]" << endl;
#define ddump(v) \
cerr << #v ":" << endl; \
for (auto macro_row : v) { \
cerr << "["; \
for (auto macro__vi : macro_row) { \
cerr << macro__vi << " "; \
} \
cerr << "]" << endl; \
}
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const double EPS = 1e-10;
int main() {
lint N, M;
cin >> N >> M;
lint P = (N + 1) / 2;
vector<lint> H(N), W(M);
rep(i, N) { cin >> H[i]; }
rep(i, M) { cin >> W[i]; }
sort(all(H));
sort(all(W));
lint odd = 0, even = 0;
vector<lint> L_odd(N + 100, 0), L_even(N + 100, 0);
rep(i, N) {
if (i % 2 == 0) {
L_even[i + 1] = L_even[i] + H[i];
L_even[i + 2] = L_even[i] + H[i];
} else {
L_odd[i + 1] = L_odd[i] + H[i];
L_odd[i + 2] = L_odd[i] + H[i];
}
}
rep(i, N) {
if (i % 2 == 0) {
even += H[i];
} else {
odd += H[i];
}
}
lint mi = LINF;
for (int k = 0; k < M; k++) {
lint add = W[k];
int i = upper_bound(all(H), add) - H.begin();
// dd(add, i);
// dd(L_odd[i], L_even[i]);
// dd((even - L_even[i]), (odd - L_odd[i]));
lint tmp;
if (i % 2 == 1) {
tmp = (L_odd[i] - L_even[i]) + add;
// d(tmp);
tmp += (even - L_even[i]) - ((odd - L_odd[i]));
} else {
tmp = (L_odd[i] - L_even[i]) - add +
((even - L_even[i]) - (odd - L_odd[i]));
}
// d(tmp);
if (tmp <= mi) { mi = tmp; }
}
cout << mi << endl;
// dump(H);
// dump(W);
// dump(L_odd);
// dump(L_even);
return 0;
} | #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int N,M;
long H[2<<17];
long L[2<<17],R[2<<17];
main()
{
cin>>N>>M;
for(int i=0;i<N;i++)cin>>H[i];
H[N]=H[N+1]=-2e9;
H[N+2]=H[N+3]=2e9;
N+=4;
sort(H,H+N);
for(int i=2;i<N;i+=2)
{
L[i]=L[i-2]+H[i-1]-H[i-2];
}
for(int i=N-2;i>0;i-=2)
{
R[i]=R[i+2]+H[i+1]-H[i];
}
long ans=1e18;
for(int i=0;i<M;i++)
{
int W;cin>>W;
int id=lower_bound(H,H+N,W)-H;
if(id%2==0)
{
ans=min(ans,L[id]+H[id]-W+R[id+1]);
}
else
{
ans=min(ans,L[id-1]+W-H[id-1]+R[id]);
}
}
cout<<ans<<endl;
}
|
//
// blank.cpp
//
//
// Created by Sagar Singh on 17/12/20.
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define bug1( x ) { cerr << (#x) <<"="<< x << endl; }
#define bug2( x , y ) { cerr << (#x) <<"="<< (x) << " " << (#y) << "="<< (y) << endl; }
#define bug3( x , y , z ) { cerr << (#x) <<"="<<(x) << " " << (#y) <<"="<< (y) << " " << (#z) <<"="<< (z) << endl; }
#define bug4( x , y , z , w) { cerr << (#x) <<"="<<(x) << " " << (#y) <<"="<< (y) << " " << (#z) <<"="<< (z) << " " << (#w) <<"="<< w << endl; }
#define bug5( x , y , z , w ,p) { cerr << (#x) <<"="<<(x) << " " << (#y) <<"="<< (y) << " " << (#z) <<"="<< (z) << " " << (#w) <<"="<< w << " " << (#p) <<"="<< p << endl; }
#define bug6( x , y , z , w ,p , q) { cerr << (#x) <<"="<<(x) << " " << (#y) <<"="<< (y) << " " << (#z) <<"="<< (z) << " " << (#w) <<"="<< w << " " << (#p) <<"="<< p << " " << (#q) <<"="<< q << endl; }
#define bugn( x , n ) { cerr<<(#x)<<":";for(int i=0;i<n;i++)cout<<(#x)<<"["<<i<<"]:"<< x[i] << " "; cout<<endl; }
#define bugnm( x , n , m ) { cerr<<(#x)<<endl;for(int i=0;i<n;i++){ cout<<"Row #"<<i<<":"; for(int j=0;j<m;j++) cout<<x[i][j]<<" "; cout << endl;} }
typedef unsigned long long ul;
typedef long double ld;
typedef long long ll;
using namespace std;
template<typename T, typename K>
inline bool smax(T &x,K y){ return x < y ? x = y, true : false; }
template<typename T, typename K>
inline bool smin(T &x,K y){ return x > y ? x = y, true : false; }
int32_t main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int a, b; cin >> a >> b;
if( a == b ){
cout << a << endl;
}else{
for(int i=0;i<3;++i){
if( i != a && i != b ){
cout << i << endl;
return 0;
}
}
}
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int x,y,ans;
cin >> x >> y;
if((x == 0 && y == 0) || (x == 1 && y == 2) || (x == 2 && y == 1) )
ans = 0;
else if((x == 2 && y == 2) || (x == 0 && y == 1) || (x == 1 && y == 0))
ans = 2;
else if((x == 1 && y == 1) || (x == 0 && y == 2) || (x == 2 && y == 0))
ans = 1;
cout << ans << "\n";
return 0;
}
|
#include <iostream>
#include <string>
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <bitset>
#include <functional>
#include <limits>
#include <map>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rrep(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define fore(i,a) for(auto &i:a)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll n, k;
ll a[805][805];
bool check(ll lim){
ll thanlim[805][805];
ll median = k*k/2+1;
rep(i,0,n){
rep(j,0,n){
if(a[i][j] >= lim) thanlim[i][j] = 1;
else thanlim[i][j] = 0;
}
}
vector<vector<int> > s(n+1, vector<int>(n+1, 0));
rep(i,0,n){
rep(j,0,n){
s[i+1][j+1]=s[i][j+1]+s[i+1][j]-s[i][j]+thanlim[i][j];
}
}
bool all_thanlim = true;
rep(i,0,n-k+1){
rep(j,0,n-k+1){
ll cnt = s[i+k][j+k]+s[i][j]-s[i+k][j]-s[i][j+k];
if(cnt < median) all_thanlim = false;
}
}
return all_thanlim;
}
int main(){
cin >> n >> k;
rep(i, 0, n) rep(j, 0, n) cin >> a[i][j];
ll inf = 1000000001;
ll ok = 0, ng = inf;
while (ok + 1 != ng) {
int md = (ok + ng) / 2;
if (check(md)) ok = md;
else ng = md;
}
printf("%lld\n", ok);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int N,M,K=0,P=0,i=1;cin>>N>>M;
int A[M+2],B[M+1];
A[0]=0;
for(;i<=M;i++)cin>>A[i];
A[M+1]=N+1;
sort(A,A+M+2);
for(i=0;i<M+1;i++)B[i]=A[i+1]-A[i]-1;
sort(B,B+M+1);
for(i=0;i<M+1;i++){
if(!K)K=B[i];
if(K)P+=(B[i]+K-1)/K;
}
cout<<P;
} |
#include<bits/stdc++.h>
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define int long long
#define sz(x) (ll)(x.size())
using namespace std;
const int mod = 1e9+7;
int expo_pow(int x,int y){
if(y == 0) return 1;
y=y%(mod-1);
x%=mod;
if(y==0) y=mod-1;
int res=1;
while(y){
if(y&1) res=(res*x)%mod;
x=(x*x)%mod;
y>>=1;
}
return res;
}
ll add()
{
return 0;
}
template <typename T, typename... Types>
T add(T var1, Types... var2){
return (((((ll)(var1)) % mod + (ll)(add(var2...))) % mod) + mod) % mod;
}
ll mul(){
return 1;
}
template <typename T, typename... Types>
T mul(T var1, Types... var2){
return (((ll)(var1)) % mod * (ll)(mul(var2...))) % mod;
}
const int inf = 1e18;
void solve(){
int n,x;
cin >> n >> x;
int a[n];
for (int i = 0; i < n; ++i) cin >> a[i];
vector<vector<int>> dp_ways(n,vector<int>(2));
vector<vector<int>> dp_val(n,vector<int>(2,-inf));
int v1 = (x + a[n-1] - 1)/a[n-1];
v1 *= a[n-1];
v1 -= x;
int v2 = v1 - a[n-1];
dp_val[n-1][0] = v1;
dp_ways[n-1][0] = 1;
if (abs(v2) < a[n-1]) {
dp_val[n-1][1] = v2;
dp_ways[n-1][1] = 1;
}
for (int i = n-2; i >= 0; --i) {
int high = a[i+1]/a[i];
int low = -high;
v1 = (x+a[i] - 1)/a[i];
v1 *= a[i];
v1 -= x;
v2 = v1 - a[i];
dp_val[i][0] = v1;
// 0-0
int req = (dp_val[i][0] - dp_val[i+1][0])/a[i];
if (req > low and req < high) {
dp_ways[i][0] += dp_ways[i+1][0];
}
// 1-1
if (dp_val[i][1] != inf) {
int req = (dp_val[i][0] - dp_val[i+1][1])/a[i];
if (req > low and req < high) {
dp_ways[i][0] += dp_ways[i+1][1];
}
}
if (abs(v2) < a[i]) {
dp_val[i][1] = v2;
int req = (dp_val[i][1] - dp_val[i+1][0])/a[i];
if (req > low and req < high) {
dp_ways[i][1] += dp_ways[i+1][0];
}
if (dp_val[i][1] != inf) {
int req = (dp_val[i][1] - dp_val[i+1][1])/a[i];
if (req > low and req < high) {
dp_ways[i][1] += dp_ways[i+1][1];
}
}
}
}
cout << dp_ways[0][1] + dp_ways[0][0] << "\n";
}
signed main(){
fast;
int test = 1;
int i=1;
while(test--){
solve();
}
}
| #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 <int mod>
struct ModInt {
int64_t x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) { return *this *= p.inverse(); }
ModInt operator-() const { return ModInt() - *this; }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b) t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
return ModInt(u);
}
ModInt pow(int64_t e) const {
ModInt ret(1);
for (ModInt b = *this; e; e >>= 1, b *= b)
if (e & 1) ret *= b;
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
static int modulo() { return mod; }
int get() const { return x; }
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
using Mint = ModInt<int(1e9 + 7)>;
int H, W;
cin >> H >> W;
string S[H];
for (int i = 0; i < H; i++) cin >> S[i];
Mint ans = 0;
int u[H][W], l[H][W];
int K = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
u[i][j] = 0;
l[i][j] = 0;
if (S[i][j] == '#') continue;
u[i][j] = 1;
l[i][j] = 1;
K++;
if (i > 0) u[i][j] += u[i - 1][j];
if (j > 0) l[i][j] += l[i][j - 1];
}
}
int d[H][W], r[H][W];
for (int i = H - 1; i >= 0; i--) {
for (int j = W - 1; j >= 0; j--) {
d[i][j] = 0;
r[i][j] = 0;
if (S[i][j] == '#') continue;
d[i][j] = 1;
r[i][j] = 1;
if (i + 1 < H) d[i][j] += d[i + 1][j];
if (j + 1 < W) r[i][j] += r[i][j + 1];
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (S[i][j] == '#') continue;
int k = u[i][j] - 1 + d[i][j] - 1 + l[i][j] - 1 + r[i][j] - 1 + 1;
ans += (Mint(2).pow(k) - 1) * Mint(2).pow(K - k);
}
}
cout << ans << '\n';
return 0;
}
|
#include <iostream>
using namespace std;
string s;
int main() {
ios::sync_with_stdio(0);
cin>>s;
bool flag = true;
for(int i = 0;i<s.length();i++){
if(i&1){
if(!isupper(s[i])){
flag = false;
break;
}
}
else{
if(!islower(s[i])){
flag = false;
break;
}
}
}
if(flag){
cout<<"Yes"<<endl;
}
else 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 base = 1,cnt = 0;
ll ans = 2e18;
rep(i,62){
ans = min(ans,N/base+N%base+cnt);
base *= 2; cnt++;
}
cout << ans << endl;
} |
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair< ll, ll > Pi;
using vl = vector<ll>;
using vs = vector<string>;
using vvl = vector<vector<ll>>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
bool compare(Pi a, Pi b) {
if(a.first != b.first){
return a.first < b.first;
}else{
return a.second < b.second;
}
}
bool In_map(ll y,ll x,ll h,ll w){
if(y<0 || x<0 || y>=h || x>=w){
return 0;
}else{
return 1;
}
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};
int main() {
ll H,W,N,M;
cin >>H>>W>>N>>M;
vector<ll>A(N),B(N),C(M),D(M);
//vector<ll>A2(H),B2(N),C2(M),D2(M);
vvl G(H,vl(W));
vvl G1(H);
vvl G2(W);
vvl G3(H);
vvl G4(W);
rep(i,N){
cin>>A[i]>>B[i];
A[i]--,B[i]--;
G1[A[i]].pb(B[i]);
G2[B[i]].pb(A[i]);
G[A[i]][B[i]]=1;
}
rep(i,M){
cin>>C[i]>>D[i];
C[i]--,D[i]--;
G3[C[i]].pb(D[i]);
G4[D[i]].pb(C[i]);
G[C[i]][D[i]]=2;
}
rep(i,H){
sort(all(G1[i]));
sort(all(G3[i]));
}
rep(i,W){
sort(all(G2[i]));
sort(all(G4[i]));
}
ll ans=0;
rep(i,H){
rep(j,W){
if(G[i][j]==2)continue;
if(G[i][j]==1){
ans++;
continue;
}
bool flag=0;
ll idx1 = lower_bound(all(G1[i]),j)-G1[i].begin();
ll idx2 = lower_bound(all(G3[i]),j)-G3[i].begin();
ll w1=G1[i].size();
ll w2=G3[i].size();
if(idx1!=w1 && idx2==w2){
flag=1;
}else if(idx1!=w1){
if(G1[i][idx1]<G3[i][idx2]){
flag=1;
}
}
if(idx1!=0&&idx2==0){
flag=1;
}else if(idx1!=0){
if(G1[i][idx1-1]>G3[i][idx2-1]){
flag=1;
}
}
ll idx3 = lower_bound(all(G2[j]),i)-G2[j].begin();
ll idx4 = lower_bound(all(G4[j]),i)-G4[j].begin();
ll h1=G2[j].size();
ll h2=G4[j].size();
if(idx3!=h1 && idx4==h2){
flag=1;
}else if(idx3!=h1){
if(G2[j][idx3]<G4[j][idx4]){
flag=1;
}
}
if(idx3!=0&&idx4==0){
flag=1;
}else if(idx3!=0){
if(G2[j][idx3-1]>G4[j][idx4-1]){
flag=1;
}
}
ans+=flag;
}
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
#define fst first
#define snd second
#define ll long long
#define ld long double
#define pb push_back
#define emp emplace_back
#define pii pair<int, int>
#define usg unsigned
#define sg signed
#define mp make_pair
using namespace std;
void setIO(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
}
const ld PI = 4*atan((ld)1);
const int INF = 1e9+7;
const ll _INF = 1e18;
const int N = 1507;
int a[N][N];
bool vld[N][N];
int main(){
setIO();
int h, w, n, m;
cin >> h >> w >> n >> m;
for (int i = 0; i < n; i++){
int r, c;
cin >> r >> c;
a[r][c] = 1;
}
for (int i = 0; i < m; i++){
int r, c;
cin >> r >> c;
a[r][c] = -1;
}
// right
for (int i = 1; i <= h; i++){
bool ok = 0;
for (int j = 1; j <= w; j++){
if (a[i][j] == 1) ok = 1;
else if (a[i][j] == -1) ok = 0;
vld[i][j] |= ok;
}
}
// left
for (int i = 1; i <= h; i++){
bool ok = 0;
for (int j = w; j >= 1; j--){
if (a[i][j] == 1) ok = 1;
else if (a[i][j] == -1) ok = 0;
vld[i][j] |= ok;
}
}
// down
for (int i = 1; i <= w; i++){
bool ok = 0;
for (int j = 1; j <= h; j++){
if (a[j][i] == 1) ok = 1;
else if (a[j][i] == -1) ok = 0;
vld[j][i] |= ok;
}
}
// up
for (int i = 1; i <= w; i++){
bool ok = 0;
for (int j = h; j >= 1; j--){
if (a[j][i] == 1) ok = 1;
else if (a[j][i] == -1) ok = 0;
vld[j][i] |= ok;
}
}
int ans = 0;
for (int i = 1; i <= h; i++){
for (int j = 1; j <= w; j++){
if (vld[i][j]) ans++;
}
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define mem(ar,x) memset(ar,x,sizeof(ar))
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define vi vector<int>
#define pi pair<int,int>
#define mod 1000000007
#define rt return 0;
#define ct continue;
#define pb push_back
#define int long long
#define ff first
#define ss second
#define maxn 200005
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
void __print(int x) {cerr << x;}
void __print(unsigned 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
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fastio;
int a,b;
cin>>a>>b;
int s1=0,s2=0;
while(a){
s1+=a%10;a/=10;
}
while(b){
s2+=b%10;b/=10;
}
if(s1>=s2)cout<<s1;
else cout<<s2;
} | #include <bits/stdc++.h>
using namespace std;
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
#define setp(x) fixed << setprecision(x)
#define ordered_set tree<pair<long double, int> , null_type,less<pair<long double, int> >, rb_tree_tag,tree_order_statistics_node_update>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
const int inf = int(1e9);
const ll INF = ll(1e16);
const ll mod = 1e9 + 7;
const double PI = acos(-1.0);
ll bp(ll a, ll n, ll cm){
ll r = 1;
while(n){
if(n & 1) r = r * a % cm;
a = a * a % cm;
n >>= 1;
}
return r;
}
template<typename T_vector> // neal
void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {
if (start < 0) start = 0;
if (end < 0) end = int(v.size());
for (int i = start; i < end; i++)
cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}
void solve(){
string s, t;
cin >> s >> t;
if(s[0] + s[1] + s[2] >= t[0] + t[1] + t[2]){
cout << s[0] + s[1] + s[2] - '0' * 3;
}else
cout << t[0] + t[1] + t[2] - '0' * 3;
}
int main()
{
// freopen("input.txt", "r", stdin)
// freopen("output.txt", "w", stdout);
Red;
int T = 1;
// cin >> T;
for(int i = 1; i <= T; ++i){
solve();
}
return 0;
} |
//g++ t.cpp -o t && t < p.txt
//d>p.txt&&t<p.txt&&t2<p.txt
#include <iostream> //
#include <vector> //
#include <algorithm> //
#include <queue> //
#include <string> //
#include <functional> //
#include <set> //
#include <unordered_map> //
#include <random> //
#include <ctime> //
#include <bitset> //
#include <cassert>
// #include <atcoder/all>
// using namespace atcoder;
// g++ t.cpp -o t -I .
#define DB cerr<<"D"<<endl
struct _zo{_zo(){std::cin.tie(nullptr);std::cout.tie(nullptr);std::ios::sync_with_stdio(false);}}__zo;
using namespace std; using ll=long long; using ld=long double; const int INF=1e9; const ll LINF=1e18; const double dINF = 1e18; const ld ldINF = 1e18; const double EPS = 1e-6;
using P=pair<ll,ll>;
using Q=pair<pair<ll,ll>,ll>;
const ll M = 998244353;
ll mod_pow(ll x, ll a) { ll an = 1; while(a > 0) { if (a&1) an = an * x % M; x = x * x % M; a >>= 1;} return an;}
ll mod_pow(ll x, ll a, ll m) { ll an = 1; while(a > 0) { if (a&1) an = an * x % m; x = x * x % m; a >>= 1;} return an;}
void add(ll& x, ll y) {x+=y; x%=M;}; void mul(ll& x, ll y) {x*=y; x%=M;}; template<typename T, typename U> void chmax(T& x, U y) {if (x<y) x=y;}; template<typename T, typename U> void chmin(T& x, U y) {if (x>y) x=y;} bool vaild(int x, int y, int hh, int ww){return 0<=x&&x<hh&&0<=y&&y<ww;}
ll gcd(ll a, ll b) {if (b==0) return a; else return gcd(b, a%b);}
int keta(ll a) {int res=0; while(a>0) res+=a%10, a/=10; return res;}
int ketasu(ll a) {int res=0; while(a>0) res++, a/=10; return res;}
const int up[]={1,-1,0,0}, lf[]={0,0,1,-1};
int main() {
int n;cin>>n;
ll a[n];
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n);
ll an=0, sm=0;
for(int i=0;i<n;i++) {
add(an, (sm + a[i]) * a[i] % M);
sm = sm * 2 + a[i];
sm %= M;
}
cout<<an<<endl;
}
//
// ・配列の大きさok? ・priority_queueはgreater? ・debug消した?
// ・落ち着いて。提出まで4分待ってね……WJ……1/10……2/10……
// Thank you for making problems and running the contest
| #include<iostream>
#include<bits/stdc++.h>
#include<math.h>
#include<vector>
#include<string>
#include<cstring>
#include<ctype.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
new_data_set;
// <<implemented_sorted_vector>> new_data_set p;
// <<insert_like_set>> insert(key):log(n)
//<<accessing_iterator_by_index>> find_by_order(idx):log(n)
// <<accessing_by_value>> order_of_key(key):log(n)
// << replace less<int> by greater<int> for descending order>>
// find(key), erase(find(key)) works the same!
#ifndef ONLINE_JUDGE // setting up print debugging
template<class K, class V>ostream& operator<<(ostream&s,const pair<K,V>&p){s<<'<'<<p.x<<", "<<p.y<<'>';return s;}
template<class T, class=typename T::value_type, class=typename enable_if<!is_same<T,string>::value>::type>
ostream& operator<<(ostream&s,const T&v){s<<'[';for(auto&x:v){s<<x<<", ";}if(!v.empty()){s<<"\b\b";}s<<']';return s;}
void __prnt(){cerr<<endl;} template<class T, class...Ts>void __prnt(T&&a,Ts&&...etc){cerr<<a<<' ';__prnt(etc...);}
#define print(...) __prnt(__VA_ARGS__)
#else
#define print(...)
#endif
typedef long long ll;
typedef long l;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ERR(x) cout<<#x<<"="<<x<<endl
#define ERR2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl;
#define show(v) for(auto e: v) cout<<e<<" ";
#define showArr(A,n) for(int i=0;i<n;i++) cout<<A[i]<<" ";
#define PI 3.1415926535897932384626433832795
#define MOD 998244353
#define mp make_pair
#define ff first
#define ss second
#define umpit unordered_map<ll,ll>::iterator
#define mpit map<ll,ll>::iterator
#define setit set<ll>::iterator
#define all(v) v.begin(),v.end()
ll binexpy(ll x,ll n)
{
if(n==0)
{
return 1;
}
else if(n%2==0)
{
return binexpy(x*x,n/2);
}
else
{
return x*binexpy(x*x,(n-1)/2);
}
}
ll binexpymod(ll x,ll n,ll mod)
{
ll prod = 1;
if(n==0)
{
return 1;
}
else if(n%2==0)
{
prod = ((x%mod)*(x%mod))%mod;
return binexpymod(prod,n/2,mod);
}
else{
prod = ((x%mod)*(x%mod))%mod;
return ((x%mod)*binexpymod(prod,(n-1)/2,mod))%mod;
}
}
//cout<<binexpy(2,60);
//cout<<endl;
//cout<<binexpymod(2,60,2);
ll modInverse(ll n, ll p)
{
return binexpymod(n, p-2, p);
}
ll nCrmod(ll n, ll r, ll p)
{
if (r==0)
return 1;
ll fac[n+1];
fac[0] = 1;
for (ll 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 main()
{
int t;
t = 1;
int Case = 0;
while(t--)
{
ll n;
cin >> n;
vector<ll>A(n);
ll ans = 0;
for(int i=0;i<n;i++){
cin >> A[i];
ans = ( ans+(binexpymod(A[i], 2, MOD)) )%MOD;
}
sort(all(A));
ll post = 0;
for(int i=n-1;i>=1;i--){
post = ((post*2)%MOD + A[i])%MOD;
ll temp = (A[i-1]*post)%MOD;
ans = (ans+temp)%MOD;
}
cout << ans << endl;
}
return 0;
}
|
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cinttypes>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<stack>
#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vpii=vector<pii>;
#define PI 3.1415926535897932384626433832795
template<typename X>
bool max_u(X&m, X v)
{
if(m<v)
{
m=v;
return true;
}
return false;
}
template<typename X>
bool min_u(X&m, X v)
{
if(m>v)
{
m=v;
return true;
}
return false;
}
int cmp(int a, int b)
{
if((3+b-a)%3==1) return b;
return a;
}
struct solve
{
solve(){}
char calc(int k, vi&a)
{
if(k==0) return "RPS"[a[0]];
int n=a.size();
int m=n;
if(m&1) m*=2;
vi b(m);
for(int i=0;i<m;i++)
{
b[i]=cmp(a[(i*2)%n], a[(i*2+1)%n]);
}
return calc(k-1, b);
}
char operator()(int k, vi&a) {
return calc(k, a);
}
};
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
for(;;)
{
int n, k;
cin >> n >> k;
if(cin.fail()) break;
string s;
cin >> s;
vi a(n);
for(int i=0;i<n;i++)
{
if(s[i]=='R') a[i]=0;
if(s[i]=='P') a[i]=1;
if(s[i]=='S') a[i]=2;
}
cout << solve()(k, a) << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
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;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
char win(char c,char d){
if(c==d)return c;
if(c>d)swap(c,d);
if(c=='R'&&d=='S')return 'R';
if(c=='P'&&d=='R')return 'P';
return 'S';
}
int main(){
ll n,k;cin>>n>>k;
string s;cin>>s;
rep(i,k){
s+=s;
string ns;
rep(j,s.size()/2)ns+=win(s[j*2],s[j*2+1]);
s=ns;
}
out(s[0]);
} |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <class T1, class T2>
bool chmax(T1 &a, const T2 b){if(a < b){a = b; return true;} return false;}
template <class T1, class T2>
bool chmin(T1 &a, const T2 b){if(a > b){a = b; return true;} return false;}
ll pow2(const int n){return (1LL << n);}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &i : v) os << i << ' ';
return os;
}
void co() { cout << '\n'; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
cout << head << ' ';
co(forward<Tail>(tail)...);
}
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
cerr << head << ' ';
ce(forward<Tail>(tail)...);
}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const int n){cout << fixed << setprecision(n);}
constexpr int INF = 1000000001;
constexpr ll LINF = 1000000000000000001;
constexpr ll MOD = 1000000007;
constexpr ll MOD_N = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);
struct union_find {
vector<int64_t> par;
vector<int64_t> _size;
vector<ll> sa, sb;
union_find(int64_t _n, vector<ll> a, vector<ll> b) {
par.resize(_n);
_size.assign(_n, 1);
for(int64_t i = 0; i < _n; ++i) par[i] = i;
sa = a, sb = b;
}
int64_t root(int64_t x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int64_t x, int64_t y) {
x = root(x), y = root(y);
if (x != y) {
if (_size[x] > _size[y]) swap(x, y);
par[y] = x;
_size[x] += _size[y];
sa[x] += sa[y];
sb[x] += sb[y];
}
}
int64_t size(int64_t x) {
return _size[root(x)];
}
bool ok(int64_t x) {
return sa[root(x)] == sb[root(x)];
}
bool same(int64_t x, int64_t y) {
return root(x) == root(y);
}
};
int main(void) {
ll n, m;
cin >> n >> m;
Vec a(n), b(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
union_find uf(n, a, b);
rep(i, m) {
ll c, d;
cin >> c >> d;
--c, --d;
uf.unite(c, d);
}
bool flg = true;
rep(i, n) flg &= uf.ok(i);
if (flg) puts("Yes");
else
puts("No");
return 0;
} | #include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<utility>
#include<map>
#include<tuple>
#include<deque>
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
const long long LINF = 1001002003004005006;
const double PI = acos(-1);
int dir4row[] = { 1,0,-1,0 };
int dir4col[] = { 0,1,0,-1 };
int dir8row[] = { 1,1,0,-1,-1,-1,0,1 };
int dir8col[] = { 0,1,1,1,0,-1,-1,-1 };
/*-----------------------------------------------*/
vector<long long> a, b;
struct UnionFind {
vector<int> r;
vector<long long> sum1, sum2;
int N;
UnionFind(int n) {
r = vector<int>(n, -1);
N = n;
sum1 = vector<long long>(n);
sum2 = vector<long long>(n);
for (int i = 0; i < n; ++i) {
sum1[i] = a[i];
sum2[i] = b[i];
}
}
int root(int x) {
if (r[x] < 0) {
return x;
}
return r[x] = root(r[x]);
}
bool unite(int a, int b) {
a = root(a);
b = root(b);
if (a == b) {
return false;
}
if (r[a] > r[b]) {
swap(a, b);
}
sum1[a] += sum1[b];
sum1[b] = 0;
sum2[a] += sum2[b];
sum2[b] = 0;
r[a] += r[b];
r[b] = a;
return true;
}
bool solve() {
for (int i = 0; i < N; ++i) {
if (sum1[i] != sum2[i]) {
return false;
}
}
return true;
}
};
int main() {
int n, m; cin >> n >> m;
a = vector<long long>(n);
b = vector<long long>(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int c, d; cin >> c >> d;
c--;
d--;
uf.unite(c, d);
}
if (uf.solve()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
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;
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;
ll ans = 0;
for(ll i=1;i<=n;i++){
ll x,y;
cin >> x >> y;
ll ss = y*(y+1)/2;
ll gg = x*(x-1)/2;
ans+=(ss-gg);
}
cout << ans <<"\n";
}
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;
int main()
{
long long int n,sum=0;
cin>>n;
for(int i=1;i<=n;i++)
{
long long int x,y;
cin>>x>>y;
sum+=(x+y)*(y-x+1)/2;
}
cout<<sum<<endl;
return 0;
} |
/*
求 k*k 矩阵最小中位数值
*/
#include <bits/stdc++.h>
using namespace std;
const int maxx=805;
long long mp[maxx][maxx];
long long sum[maxx][maxx];
int n,k,inx;
long long l=1e9+5,r=-1;
int check(int x){//每个小矩阵中 比x大的数字要超过 inx
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){//sum[i][j]存的是小矩阵内 >= mid 的数量
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(mp[i][j]>=x);
}
}
for(int i=k;i<=n;i++){
for(int j=k;j<=n;j++){//如果有小矩阵中
int tmp=sum[i][j]-sum[i-k][j]-sum[i][j-k]+sum[i-k][j-k];
if(tmp < inx) return false;
}
}
return true;
}
int main(){
scanf("%d%d",&n,&k);
inx= k * k/2 + 1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%lld",&mp[i][j]);
l=min(mp[i][j],l);
r=max(mp[i][j],r);
}
}
while(l<r){
int mid=(l+r+1)>>1;
if(check(mid))//猜 mid 值,然后去验证每个小矩阵中>=mid的数字个数是否合法
l=mid;
else
r=mid-1;
}
printf("%lld",l);//最小中位数值
// while(l<=r){
// int mid=(l+r)/2;
// if(check(mid)) ans=mid,l=mid+1;
// else r=mid-1;
// }cout<<ans;
} | #define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define rep2(i, a)for(auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
#define INF (1ll << 60)
ll mod = 1000000007;
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p & 1)a *= x; x *= x; p >>= 1; }return a; }
ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p & 1)a = a * x % mod; x = x * x % mod; p >>= 1; }return a; }
ll co(ll n, ll k) { ll a = 1; rep(i, 1ll, k + 1) { a *= n - i + 1; a /= i; }return a; }
ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; }
ll modinv(ll a) { ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); }u %= mod; if (u < 0) u += mod; return u; }
int main(void) {
int n, k;
cin >> n >> k;
vector<vector<int>> a(n, vector<int>(n));
int r = 0, l = 0;
rep(i, 0, n)rep(j, 0, n)cin >> a[i][j], r = max(r, a[i][j]);
while (r - l > 1) {
int m = (r + l) / 2;
vector<vector<int>> s(n + 1, vector<int>(n + 1));
rep(i, 0, n) {
rep(j, 0, n) {
if (a[i][j] > m)s[i + 1][j + 1] = 1;
s[i + 1][j + 1] += s[i][j + 1] + s[i + 1][j] - s[i][j];
}
}
bool ok = false;
rep(x, 0, n - k + 1) {
rep(y, 0, n - k + 1) {
if (s[x + k][y + k] - s[x][y + k] - s[x + k][y] + s[x][y] < k * k / 2 + 1) {
ok = true;
break;
}
}
if (ok)break;
}
if (ok)r = m;
else l = m;
}
printf("%d\n", r);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
cin >> n >> m;
vector<char> str;
for(int i=0; i<m; i++) {
string s;
cin >> s;
for(int j=0; j<s.size(); j++) str.push_back(s.at(j));
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cout << str.at(j*20 + i);
}
cout << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n;
mt19937 mt;
const int TRIES = 750;
const int SHUFFLES = 0;
bool check(string s, vector<vector<char>>& a, int i, int j) {
bool isOk = true;
for (int k = 0; k < s.size() && isOk; ++k) {
if (s[k] != a[i][(j + k) % n]) {
isOk = false;
}
}
if (isOk) {
return true;
}
for (int k = 0; k < s.size() && isOk; ++k) {
if (s[k] != a[(i + k) % n][j]) {
isOk = false;
}
}
return isOk;
}
int calc(vector<string>& v, vector<vector<char>>& a) {
int cnt = 0;
for (string& s : v) {
bool found = false;
for (int i = 0; i < n && !found; ++i) {
for (int j = 0; j < n && !found; ++j) {
if (check(s, a, i, j)) {
found = true;
}
}
}
cnt += found;
}
return cnt;
}
pair<int, vector<vector<char>>> gen(vector<string>& v) {
shuffle(v.begin(), v.end(), mt);
vector<vector<char>> a(n, vector<char>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
a[i][j] = char(mt() % 8 + int('A'));
}
}
int i = 0, j = 0;
for (auto s : v) {
if (j + s.size() >= n) {
j = 0;
++i;
}
if (i >= n) {
break;
}
for (int k = 0; k < s.size(); ++k, ++j) {
a[i][j] = s[k];
}
}
pair<int, vector<vector<char>>> res = {calc(v, a), a};
for (int i = 0; i < SHUFFLES; ++i) {
auto curr = res;
shuffle(curr.second.begin(), curr.second.end(), mt);
curr.first = calc(v, curr.second);
if (curr.first > res.first) {
res = curr;
}
}
return res;
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
mt.seed(time(nullptr));
int m;
cin >> n >> m;
vector<string> v(m);
for (auto& i : v) {
cin >> i;
}
pair<int, vector<vector<char>>> res = gen(v);
for (int i = 0; i < TRIES; ++i) {
auto curr = gen(v);
if (curr.first > res.first) {
res = curr;
}
}
for (auto& i : res.second) {
for (auto& j : i) {
cout << j;
}
cout << '\n';
}
return 0;
}
|
#define ll long long
#define dn double
#define mp make_pair
#define pb push_back
#define se second
#define fi first
#define mod 1000000007
#define sob(v) v.begin(),v.end()
#define sobr(v) v.rbegin(),v.rend()
#define fr(i,a,b) for(int i=a;i<=b;++i)
#define frr(i,a,b) for(int i=a;i>=b;--i)
#define pi acos(-1.00)
#define inf 1e9
#define check cout<<"*"<<endl
#define ffix(x) cout<<fixed<<setprecision(x)
#define fast ios_base::sync_with_stdio(false);cill n.tie(NULL);cout.tie(NULL)
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
typedef vector<int> vi;
typedef map<int,int> mii;
typedef pair<ll,int> pii;
typedef pair<int,vi> pp;
typedef pair<pair<int,int>,int> pain;
int dr[8]= {-1,0,+1,0,-1,-1,+1,+1};
int dc[8]= {0,+1,0,-1,-1,+1,-1,+1};
int kx[8]= {-1,+1,+1,-1,+2,+2,-2,-2};
int ky[8]= {+2,+2,-2,-2,-1,+1,+1,-1};
const int N=100005;
int main()
{
ffix(12);
dn a,b,c,d;
cin>>a>>b>>c>>d;
dn ans=a+((c-a)*b)/(b+d);
cout<<ans<<endl;
}
| #include <iostream>
#include <vector>
std::vector<int> StringsToInt(std::string str, std::string delimit) {
std::vector<int> vec;
if (str.find(delimit) == std::string::npos) {
vec.emplace_back(std::atoi(str.c_str()));
return vec;
}
for (std::size_t pos = 0;
str.find(delimit) != std::string::npos;) {
pos = str.find(delimit);
vec.emplace_back(std::atoi(str.substr(0, pos).c_str()));
str = str.substr(pos + delimit.size(), str.size());
}
vec.emplace_back(std::atoi(str.c_str()));
return vec;
}
int main() {
std::string dish, orange;
if (std::getline(std::cin, dish) && std::getline(std::cin, orange)) {
int sumDish = std::atoi(dish.c_str());
std::vector<int> vOrange = StringsToInt(orange, " ");
int max = 0;
int left=0, right=sumDish, min=0, sum=0;
while(true) {
for (int k=left, count=1; k < right; ++k, ++count) {
if (k == left) {
min = vOrange.at(k);
}
int n = vOrange.at(k);
if (min > n) {
min = n;
}
sum = min * count;
if (sum > max) {
max = sum;
}
}
for (int k=right-1, count=1; k > left-1; --k, ++count) {
if (k == (right-1)) {
min = vOrange.at(k);
}
int n = vOrange.at(k);
if (min > n) {
min = n;
}
sum = min * count;
if (sum > max) {
max = sum;
}
}
if (left > right) {
break;
}
++left;
--right;
}
std::cout << max << std::endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int N=5e5;
int dp[N+10][2];
char a[N+10],b[N+10];
bool xora[N+10],xorb[N+10];
int main(){
int n;
scanf("%d%s%s",&n,a+1,b+1);
for(int i=1;i<=n;i++){
xora[i]=xora[i-1]^(a[i]-'0');
xorb[i]=xorb[i-1]^(b[i]-'0');
}
int l=1;
long long ans=0;
for(int i=1;i<=n;i++){
l=max(l,i);
if(xora[l]==xorb[i])continue;
while(l<=n&&xora[l]!=xorb[i])l++;
if(l>n){
puts("-1");
return 0;
}
ans+=l-i;
}
cout<<ans;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for(ll i = (ll)a; i <= (ll)b; i++)
#define DEC(i, a, b) for(ll i = (ll)a; i >= (ll)b; i--)
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v) sort(all(v)); v.resize(unique(all(v)) - v.begin());
#define INF (ll) 1e9 + 100
#define LLINF (ll) 1e18
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define sandybridge __attribute__((optimize("Ofast"), target("arch=sandybridge")))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
ll n, K, p[105], sz[105], arr[55][55], fac[55], ans = 1, mod = 998244353;
ll fs(ll x) {
if (x == p[x]) return x;
return p[x] = fs(p[x]);
}
int main() {
fastio; cin >> n >> K;
FOR(i, 1, n) FOR(j, 1, n) cin >> arr[i][j];
FOR(i, 1, 2*n) p[i] = i, sz[i] = 1;
fac[0] = 1; FOR(i, 1, n) fac[i] = (fac[i-1] * i) % mod;
FOR(i, 1, n) FOR(j, 1, n) if (fs(i) != fs(j)) {
bool ok = 1;
FOR(k, 1, n) if (arr[i][k] + arr[j][k] > K) ok = 0;
if (ok) {
sz[fs(j)] += sz[fs(i)];
p[fs(i)] = fs(j);
}
}
FOR(i, 1, n) FOR(j, 1, n) if (fs(n+i) != fs(n+j)) {
bool ok = 1;
FOR(k, 1, n) if (arr[k][i] + arr[k][j] > K) ok = 0;
if (ok) {
sz[fs(n+j)] += sz[fs(n+i)];
p[fs(n+i)] = fs(n+j);
}
}
FOR(i, 1, 2*n) if (fs(i) == i) ans = (ans * fac[sz[i]]) % mod;
cout << ans;
}
|
#include "bits/stdc++.h"
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
long long a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, m = 0, n = 0, p = 0, q = 0, mod =
998244353
, pi = 3.1415926535;
string s1, s2;
char moji;
cin >> a;
vector<long long> v(a,0);
vector<vector<vector<long long>>> dp(a+1,vector<vector<long long>>(5001,vector<long long>(51,0)));
for(int i=0;i<a;i++)
{
cin >> v.at(i);
}
p=accumulate(v.begin(),v.end(),0);
if(p%2)
{
cout << 0 << endl;
return 0;
}
for(int i=0;i<a+1;i++)
dp.at(i).at(0).at(0)=1;
for(int j=0;j<a;j++)
{
for(int k=0;k<5001;k++)
{
for(int l=0;l<50;l++)
{
if(v.at(j)>k)
{
dp.at(j+1).at(k).at(l+1)=dp.at(j).at(k).at(l+1);
}
else
{
dp.at(j+1).at(k).at(l+1)=dp.at(j).at(k-v.at(j)).at(l)+dp.at(j).at(k).at(l+1);
}
dp.at(j+1).at(k).at(l+1)%=mod;
}
}
}
for(int i=1;i<a;i++)
{
n=dp.at(a).at(p/2).at(min((long long)i,a-i));
for(int j=1;j<=i;j++)
{
n*=j;
n%=mod;
}
for(int j=1;j<=a-i;j++)
{
n*=j;
n%=mod;
}
m+=n;
m%=mod;
}
cout << m << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int Mod = 998244353;
int N, a[105], f[105][20010], fac[105];
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
cin >> N;
for (int i = 1; i <= N; i++) cin >> a[i];
for (int i = fac[0] = 1; i <= N; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
int zero = 1e4 + 3;
f[0][zero] = 1;
for (int i = 1, sum = 0; i <= N; i++) {
sum += a[i];
for (int j = i; ~j; j--)
for (int k = -sum + zero; k <= sum + zero; k++)
if (j) f[j][k] = (f[j - 1][k - a[i]] + f[j][k + a[i]]) % Mod;
else f[j][k] = f[j][k + a[i]];
}
int ans = 0;
for (int i = 1; i <= N; i++)
ans = (ans + 1ll * f[i][zero] * fac[i] % Mod * fac[N - i]) % Mod;
cout << ans << endl;
return 0;
} |
#include<iostream>
main(){long N,X,a=0,b=1,A,h,c;std::cin>>N>>X>>A;h=X,N--;while(N){std::cin>>A,c=a+b;if(h%A) a=c;if(X%A) b=c;X=(X/A)*A,h=((h+A-1)/A)*A,N--;}std::cout<<a+b;} | #include<bits/stdc++.h>
using namespace std;
#define FOR(i,s,t) for(int i=(s),_t=(t); i<=_t; ++i)
#define DOR(i,s,t) for(int i=(s),_t=(t); i>=_t; --i)
#define EOR(i,x) for(int i=Head[x]; ~i; i=Nxt[i])
typedef long double db;
typedef long long ll;
namespace Eddd {
inline char sc() {
return getchar();
static const int LEN=100000;
static char Buf[LEN],*OP=Buf,*ED=Buf;
if(OP==ED) ED=(OP=Buf)+fread(Buf,1,LEN,stdin);
return *OP++;
}
template<class T> void rd(T &x) {
static int c,f;x=f=0;
while(c=sc(),c<48) if(c=='-') f=1;
do x=(x<<3)+(x<<1)+(c^48);
while(c=sc(),c>47);if(f) x=-x;
}
template<class T> void pt(T x) {
if(x<0) putchar('-'),x=-x;
else if(!x) putchar('0');
static int Stk[30],tp=0;
for(; x; x/=10) Stk[tp++]=x%10;
while(tp) putchar(Stk[--tp]^48);
}
template<class T> void ptk(const T &x) {
pt(x);putchar(' ');
}
template<class T> void ptn(const T &x) {
pt(x);putchar('\n');
}
template<class T> bool chkmi(T &x,const T &y) {
return x>y?x=y,true:false;
}
template<class T> bool chkmx(T &x,const T &y) {
return x<y?x=y,true:false;
}
}
using namespace Eddd;
ll A[55];
ll C[55];
ll f[55][2];
int main() {
int n;
rd(n);
ll num;
rd(num);
FOR(i,1,n) rd(A[i]);
DOR(i,n,1) {
C[i]=num/A[i];
num%=A[i];
}
FOR(i,1,n-1) A[i]=A[i+1]/A[i];
// FOR(i,1,n-1) {
// cerr << C[i] << " " << A[i] << endl;
// }
f[0][0]=1;
FOR(i,1,n-1) {
f[i][0]+=f[i-1][0];
if(C[i]) f[i][1]+=f[i-1][0];
if(C[i]+1==A[i]) {
f[i][1]+=f[i-1][1];
} else {
f[i][0]+=f[i-1][1];
f[i][1]+=f[i-1][1];
}
}
ptn(f[n-1][0]+f[n-1][1]);
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MV 200001
#define LMV 21
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define emp emplace
#define mp make_pair
#define ins insert
#define sz(x) (int)x.size()
#define whoami(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); whi(_it, args); }
void whi(istream_iterator<string> it) { cerr<<"\n"; }
template<typename T, typename... Args>
void whi(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<" "<<a<<" "; whi(++it, args...); }
void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);}
void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);}
typedef long long ll;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<PII, int> PPII;
typedef pair<PLL, ll> PPLL;
typedef map<int, int> MII;
const int MOD = 1000000007;
const ll INF = 4e18;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct h_llint {
static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
struct h_pair{
size_t operator()(const PLL&x)const{
return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32));
}
};
typedef map<ll, ll> MLL;
typedef map<PII, int> MPII;
typedef map<PLL, ll> MPLL;
typedef set<int> SI;
typedef set<ll> SL;
//ordered_set = order_of_key(.)
//ordered_set = find_by_order(.)
typedef ordered_set<int> OSI;
typedef ordered_set<ll> OSL;
typedef ordered_multiset<int> MOSI;
typedef ordered_multiset<ll> MOSL;
typedef unordered_map<ll, int, h_llint> UMLI;
typedef unordered_map<ll, ll, h_llint> UMLL;
typedef unordered_map<PLL, int, h_pair> UMPI;
typedef unordered_map<PLL, ll, h_pair> UMPL;
int ar[MV];
ll arr[MV];
VI G[MV], rz;
void dfs_visit(int s, int p)
{
if(!arr[ar[s]])
rz.pb(s);
arr[ar[s]]++;
for(auto u : G[s])
{
if(u != p)
dfs_visit(u, s);
}
arr[ar[s]]--;
return;
}
void solve(int T)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>ar[i];
for(int i=0;i<n-1;i++)
{
int s,d;
cin>>s>>d;
G[s].pb(d);
G[d].pb(s);
}
dfs_visit(1, -1);
sort(begin(rz), end(rz));
for(auto &&u : rz)
cout<<u<<"\n";
return;
}
int main(void)
{
FLASH();
//freopen("cowjog.in", "r", stdin);
//freopen("cowjog.out", "w", stdout);
int T;
T = 1;
#ifndef ONLINE_JUDGE
time_t time_t1, time_t2;
time_t1 = clock();
#endif
//cin>>T;
while(T--)
solve(T);
#ifndef ONLINE_JUDGE
time_t2 = clock();
SETF();
cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n";
UNSETF();
#endif
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
ll N;
ll C[110000];
vector<ll> G[110000];
ll cnt[110000];
vector<ll> res;
void init() {
cin >> N;
repp(i,1,N+1) cin >> C[i];
rep(i,N-1) {
ll a, b; cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
}
void dfs(ll now, ll from) {
for (ll to : G[now]) {
if (to==from) continue;
if (cnt[C[to]]==0) res.push_back(to);
cnt[C[to]]++;
dfs(to,now);
cnt[C[to]]--;
}
}
int main() {
init();
cnt[C[1]]++;
res.push_back(1);
dfs(1,0);
sort(all(res));
for (ll ans : res) cout << ans << endl;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define cell(a,b) int((a+(b-1))/b)
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define nl '\n'
#define INF 10e9
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
bool sortbyPair(const pair<int,int>&a,const pair<int,int>&b)
{
return (a.second<b.second);
}
int main()
{
int n,i;
ll c=0;
cin>>n;
vector<int> a(n);
map<int,int> mp;
for(int &it:a)
{
cin>>it;
mp[it]++;
}
for(int &it:a)
{
c+=(n-mp[it]);
n--;
mp[it]--;
}
cout<<c<<nl;
}
| #include <bits/stdc++.h>
using namespace std;
int solve(int a) {
return a;
}
int main() {
int N; cin >> N;
int A[110];
int B[110];
for (int i = 0; i < N; i++) {
cin >> A[i];
}
for (int i = 0; i < N; i++) {
cin >> B[i];
}
int ans = 0;
for (int i = A[0]; i <= B[0]; i++) {
bool is = true;
for (int j = 1; j < N; j++) {
if(i < A[j] || B[j] < i) is = false;
}
if (is == true) ans++;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<60)-1;
#define rep(i, n) for (int i = 0; i < n; i++)
#define sz(a) (int)(a.size())
template<class T>
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}
template<class T>
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}
//コーナーケースに気をつけろ!
int main() {
int a, b, c; cin >> a >> b >> c;
cout << 21 - a - b - c << endl;
return 0;
}
//小数点精度
//cout << fixed << std::setprecision(15) << y << endl; | #include<iostream>
#include<algorithm>
using namespace std;
int main(){
int a;
int b;
int w;
int Max = 0;
int Min = 1000000000;
cin >> a >> b >> w;
for(int i = 1; i <= 1000000; i++){
if(a*i <= w*1000 && w*1000 <= b*i){
Min = min(Min,i);
Max = max(Max,i);
}
}
if(Max == 0) {
cout << "UNSATISFIABLE" << endl;
}else{
cout << Min << " " << Max << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define eps 1e-8
#define maxm 600005
#define maxn 200005
#define ls (tot << 1)
#define rs (tot << 1 | 1)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const double pi = acos(-1);
const ll mod = 1e9 + 7;
inline ll read(){
ll x = 0, f = 1;char ch = getchar();
while(ch > '9' || ch < '0'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = x * 10 + ch -'0';ch = getchar();}
return x * f;
}
int t, l, r;
ll ans;
int main(){
int i, j, x;
t = read();
while(t--){
l = read(), r = read();
ans = 0;
ans += 1ll * (r + 1) * (r + 2) / 2;
if(l){
ans -= 1ll * l * (r + 1 + r - l + 2);
ans += 1ll * l * l;
if(r < 2 * l - 2) ans -= 1ll * (l - 1 - (r - l + 2) + 1 + 1) * (l - 1 - (r - l + 2) + 1) / 2;
}
printf("%lld\n", ans);
}
return 0;
} |
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
long long ans=0, first, last, num;
int l, r, n;
int main() {
cin>>n;
while(n--) {
ans=0;
cin>>l>>r;
if(l==r && l==0) cout<<1<<endl;
else if(l+l>r) cout<<0<<endl;
else {
first=l+l;
last=r;
num=last-first+1;
ans=1LL*(1+num)*num/2;
cout<<ans<<endl;
}
}
return 0;
}
/*
5
2 6
0 0
1000000 1000000
12345 67890
0 1000000
6
1
0
933184801
500001500001
*/ |
// ACPC ISA.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ld long double
#define ll long long
#define ull unsigned long long
#define pb push_back
#define eb emplace_back
#define endl '\n'
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename A, typename B>
string to_string(pair<A, B> p);
template<typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template<typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first)res += ", ";
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template<size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++)res += static_cast<char>('0' + v[i]);
return res;
}
template<typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first)res += ", ";
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template<typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template<typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template<typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " +
to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifndef ONLINE_JUDGE
#define err(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define err(...) 42
#endif
ll const N = 1e5 * 2, NN = 1e5 * 2 + 2, mod = 1e9 + 7;
void task_a() {
string s;
cin >> s;
deque<char> dq;
int cnt = 0;
for (int i = 0; i < s.length(); ++i) {
if(s[i]=='R') {
cnt++;
continue;
}
if (cnt % 2 == 0) {
if (dq.empty())dq.pb(s[i]);
else {
if (dq.back() == s[i]) {
dq.pop_back();
} else {
dq.pb(s[i]);
}
}
} else {
if (dq.empty())dq.push_front(s[i]);
else {
if (dq.front() == s[i]) {
dq.pop_front();
} else {
dq.push_front(s[i]);
}
}
}
}
if (cnt % 2 == 0) {
while (!dq.empty()) {
cout << dq.front();
dq.pop_front();
}
} else {
while (!dq.empty()) {
cout << dq.back();
dq.pop_back();
}
}
}
void task_b() {
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
// cin >> t;
for (int i = 1; i <= t; ++i) {
task_a();
// task_b();
}
return 0;
} | #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
const ll mod = 1000000007;
const int INF = 1001001001;
ll N;
vector<pair<ll, ll> > prime;
void prime_fact(ll x){
for (ll i = 2; i * i <= x; i++){
if(x % i == 0){
pair<ll, ll> p = make_pair(i, 0);
while (x % i == 0){
x /= i;
p.second++;
}
prime.push_back(p);
}
}
if(x > 1){
pair<ll, ll> p = make_pair(x, 1);
prime.push_back(p);
}
}
int ans = 0;
void f(ll n, int num){
if(num == prime.size()){
// cout << n << endl;
ll x = (2 * N) / n;
if((x - ( n - 1)) % 2 == 0) ans++;
return;
}
ll a = 1;
rep(i, prime[num].second+1){
f(n*a, num+1);
a *= prime[num].first;
}
}
int main(){
cin >> N;
prime_fact(2*N);
f(1, 0);
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
using namespace std;
typedef long long ll;
char v[65][5];
int n;
ll dp[65][2];
bool mark[65][2];
ll f(int i, int j) {
if(i == n) return j;
if(mark[i][j]) return dp[i][j];
mark[i][j] = true;
ll ans = 0;
if(v[i][0] == 'A') {
if(!j) ans = 2*f(i+1, 0);
else ans = f(i+1, 1) + f(i+1, 0);
}
else {
if(!j) ans = f(i+1, 0) + f(i+1, 1);
else ans = 2*f(i+1, 1);
}
return dp[i][j] = ans;
}
int main () {
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf(" %s", v[i]);
}
printf("%lld\n", f(0, true) + f(0, false));
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define asll unsigned long long
#define ULL unsigned long long
#define ll long long
#define LL long long
#define ld long double
#define imt int
#define pii pair<int,int>
#define st first
#define fi first
#define nd second
#define se second
#define psbk(x) push_back(x);
#define pb push_back
#define For(x,a,b) if(a!=b){icdc=(b-a)/abs(b-a))}else{icdc=1};for(int x=(a);x!=(b+icdc);x+=icdc)
#define forp(x,a,b) if(b-a!=0){icdc=(b-a)/abs(b-a))}else{icdc=1};for(int x=(a);x!=(b);x+=icdc)
#define FOR(x,n) for(int x=0;x<(n);x++)
#define m_p make_pair
int icdc;
#define kj <<" "<<
#define kjb <<" ";
#define dg <<","<<
#define ml <<'\n';
#define co cout<<
#define ter cout<<endl;
#define sibi cout<<"TES"<<endl;
#define tes(x) cout<<"TES"<<x<<endl;
#define kpr(x) cout<<"("<<x.st dg x.nd <<")" ml
ULL M=1e9+7;
LL b,m,k,tc;
LL p,q,r,t;
string s[105];
LL bemer[105];
LL salah[105];
LL fals(int p);
LL tru(int p){
//sibi
//co "t" ml
LL ret=0;
if(p==0){
bemer[p]=1;
return 1;
}
if(bemer[p]!=-1){
return bemer[p];
}
if(s[p-1]=="AND"){
ret=tru(p-1);
bemer[p]=ret;
return bemer[p];
}
else{
ret+=2*tru(p-1);
//sibi
ret+=fals(p-1);
bemer[p]=ret;
return bemer[p];
}
}
LL fals(int p){
//co "f" kj p ml
LL ret=0;
if(p==0){
salah[p]=1;
return 1;
}
if(salah[p]!=-1){
return salah[p];
}
if(s[p-1]=="OR"){
salah[p]=fals(p-1);
//co "kh" kj p kj fals(p-1) ml
return salah[p];
}
else{
LL ret=0;
ret+=2*fals(p-1);
ret+=tru(p-1);
salah[p]=ret;
return salah[p];
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
//srand(time(0));
cin>>b;
//cin.ignore();
//sibi
FOR(i,b){
//sibi
cin>>s[i];
//operator ke i untuk ngoperasike i sama i+1
}
//sibi
memset(bemer,-1,sizeof(bemer));
memset(salah,-1,sizeof(salah));
//cout<<b<<endl;
r=tru(b);
FOR(i,b+1){
//co bemer[i] kj salah[i] ml
}
cout<<r<<endl;
}
/*
*/
|
#pragma GCC optimize("O3")
#pragma GCC target("avx")
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define out(x) cout << x << endl
#define out2(x, y) cout << x << " " << y << " " << endl
const ll INF = 1LL << 60;
const int BIG = 1e9;
const int LN = 1000000000;
vector<char> alp = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int main() {
cout << fixed << setprecision(15);
int n, d, h;
cin >> n >> d >> h;
double maxh = -1;
rep(i, n) {
int di, hi;
double tmp;
cin >> di >> hi;
tmp = h - (double)d * (h - hi) / (d - di);
if (tmp > maxh) maxh = tmp;
}
if (maxh < 0) maxh = 0;
cout << maxh << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define int long double
void Malena(){
int n,d,h;cin>>n>>d>>h;
vector<vector<int>>bars(n,vector<int>(2));
for(int i=0;i<n;++i){
cin>>bars[i][0]>>bars[i][1];
}
int low=0,hi=1001;
int eps=1e-6;
while(abs(hi-low)>eps){
int mi=low+(hi-low)/2;
int found=0;
for(int i=0;i<n;++i){
if(bars[i][1] - ( ((h-mi)/d)*bars[i][0])-mi > 0){
found++;
}
}
(found?low:hi)=mi;
}
cout<<setprecision(12)<<low<<'\n';
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t;
t=1;
//cin>>t;
while(t--){
Malena();
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<(n);++i)
using ll = long long;
using p = pair<int,int>;
int main() {
int a1,a2,a3,a4;
cin >> a1 >> a2 >> a3 >> a4;
int ans = a1;
if(ans > a2) ans = a2;
if(ans > a3) ans = a3;
if(ans > a4) ans = a4;
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define f(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define ff first
#define ss second
#define ll long long
#define mod 1000000007
#define ps(x,y) fixed<<setprecision(y)<<x
#define inp(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
FIO;
int t=1;
// cin>>t;
while(t--)
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<min(min(a,b),min(c,d))<<endl;
}
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
int main(){
int64_t n, a = 0; cin >> n;
if (n >= 1000) a += n - 999;
if (n >= 1000000) a += n - 999999;
if (n >= 1000000000) a += n - 999999999;
if (n >= 1000000000000) a += n - 999999999999;
if (n >= 1000000000000000) a += n - 999999999999999;
cout << a << endl;
} | #pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
// warning: pragmas don't work on USACO and just set to O0
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //gp_hash_table<T, U, chash>;
#include <ext/pb_ds/tree_policy.hpp> //tree<T, U, cmp, rb_tree_tag,tree_order_statistics_node_update>;
using namespace std; using namespace __gnu_pbds;
// shorter template (CF specialization, because compile time better)
inline int RAND(int l, int r) {
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> unifd(l, r); return unifd(mt);
}
template <class T> using pq = priority_queue<T, vector<T>, greater<T>>;
using ll = long long; using db = double; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>;
using vll = vector<ll>; const int xd[4] = {0, 1, 0, -1}, yd[4] = {1, 0, -1, 0};
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define eb emplace_back
#define pb push_back
#define pf push_front
//#define TC
const ll INF = 1e18; const db EPS = 1e-8;
const int MOD = 1e9 + 7, //998244353
_ = 2e5 + 5;
void solve() {
ll N;
cin>>N;
ll x=1;
ll ans=0;
ll y=1;
while(true){
x*=1000;
if(N>=x) {
ll res = (min(x*1000,N+1)-x)*y;
ans+=res;
}
else {
break;
}
y++;
}
cout<<ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
cin.tie(0)->sync_with_stdio(0); //cout << setprecision(3) << fixed << showpoint;
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
void solve() {
using ll = long long;
int n, m; cin >> n >> m;
vector<int> a(n), b(n);
for(auto &x : a) cin >> x;
for(auto &x : b) cin >> x;
vector<vector<int>> g(n);
while(m--) {
int c, d; cin >> c >> d; --c, --d;
g[c].emplace_back(d);
g[d].emplace_back(c);
}
vector<bool> seen(n, false);
auto dfs = [&](auto dfs, int v) -> complex<ll> {
complex<ll> ret(a[v], b[v]);
seen[v] = true;
for(auto &nv : g[v]) {
if(!seen[nv]) {
ret += dfs(dfs, nv);
}
}
return ret;
};
for(int i=0; i<n; ++i) {
if(!seen[i]) {
auto ret = dfs(dfs, i);
if(real(ret) != imag(ret)) {
cout << "No\n";
return;
}
}
}
cout << "Yes\n";
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vpii vector<pair<int, int>>
#define mems(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define forn(i, s, e) for (int i = s; i < (e); ++i)
#define FASTIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define FILEIO \
freopen("./input.txt", "r", stdin); \
freopen("./output.txt", "w", stdout);
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define time__(d) \
for ( \
auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); \
blockTime.second; \
debug("%s : %lld ms\n ", d, chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false)
const int M = 1e9 + 7;
const ll OO = (ll)1e18;
template <class T>
T ABS(const T &x) { return x > 0 ? x : -x; }
ll gcd(ll n1, ll n2) { return n2 == 0 ? ABS(n1) : gcd(n2, n1 % n2); }
ll lcm(ll n1, ll n2) { return n1 == 0 && n2 == 0 ? 0 : ABS(n1 * n2) / gcd(n1, n2); }
vi a, b;
ll sum1, sum2;
void dfs(int curr, vector<vi> &adj, vi &vis)
{
vis[curr] = 1;
sum1 += a[curr];
sum2 += b[curr];
for (int i = 0; i < (int)adj[curr].size(); ++i)
{
int next = adj[curr][i];
if (vis[next])
continue;
dfs(next, adj, vis);
}
}
int main()
{
FASTIO
#ifdef LOCAL
FILEIO
#endif
int n, m;
cin >> n >> m;
int in;
forn(i, 0, n)
{
cin >> in;
a.push_back(in);
}
forn(i, 0, n)
{
cin >> in;
b.push_back(in);
}
int c, d;
vector<vi> adj(n + 1);
forn(i, 0, m)
{
cin >> c >> d;
--c, --d;
adj[c].pb(d);
adj[d].pb(c);
}
vi vis(n, 0);
forn(i, 0, n)
{
if (vis[i])
continue;
sum1 = 0, sum2 = 0;
dfs(i, adj, vis);
if (sum1 != sum2)
{
cout << "No";
return 0;
}
}
cout << "Yes";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
//typedef
typedef long long ll;
typedef pair<ll,ll> PLL;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<string> VS;
//template
template<class T> inline bool chmin(T &a, T b) {if (a > b){a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) {if (a < b){a = b; return true;} return false;}
//repetition
#define FOR(i,a,b) for(int i=int(a); i<int(b); ++i)
#define FORD(i,a,b) for(int i=int(a); i<int(b); --i)
#define REP(i,N) for(int i=0; i<int(N); ++i)
#define REPS(i,N) for(int i=0; i<=int(N); ++i)
#define REPD(i,N) for(int i=0; i<int(N); --i)
//container util
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//constant
#define INF 1000000000000 //10^12:∞
#define MOD 1000000007 //10^9+7
#define MAXR 100000 //配列の最大のrenge
//debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
int main() {
ll N; cin >> N;
ll ans=0;
REP(i,N) {
ll tmp_1,tmp_2; cin>>tmp_1>>tmp_2;
if ((tmp_2-tmp_1)%2==0) ans += ((tmp_1 + tmp_2) * ((tmp_2 - tmp_1)/2) + (tmp_2+tmp_1)/2);
else ans += ((tmp_1 + tmp_2) * ((tmp_2 - tmp_1 + 1)/2));
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define ll unsigned long long
#define pi 3.141592653
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define MAX 100005
using namespace std;
ll count(ll n)
{
ll c=0;
while(n!=0)
{
c++;
n=n/10;
}
return c;
}
int isPrime(ll n)
{
// Corner case
if (n <= 1)
return 0;
// Check from 2 to n-1
for (ll i = 2; i < n; i++)
if (n % i == 0)
return 0;
return 1;
}
ll modFact(ll n, ll p)
{
if (n >= p)
return 0;
ll result = 1;
for (ll i = 1; i <= n; i++)
result = (result * i) % p;
return result;
}
ll divisor(ll n)
{ ll h=1;
for(ll i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
h=i;
break;
}
}
return h;
}
bool sortbysec(const pair<ll,ll> &a,
const pair<ll,ll> &b)
{
return (a.second < b.second);
}
const int logN = 20;
int f[MAX][logN],mval[MAX][logN],depth[MAX];
vector<pair<int,int> >v[100005];
void dfs(int u)
{
for(int i=1;i<logN;i++)
{
f[u][i]=f[f[u][i-1]][i-1];
mval[u][i]=max(mval[u][i-1],mval[f[u][i-1]][i-1]);
}
for(auto i:v[u])
{
int ver=i.first,w=i.second;
if(!depth[ver])
{
f[ver][0]=u;
mval[ver][0]=w;
depth[ver]=depth[u]+1;
dfs(ver);
}
}
}
ll sum(ll x)
{
ll s = 0;
while (x != 0) {
s = s + x % 10;
x = x / 10;
}
return s;
}
ll fact(ll n);
ll nCr(ll n, ll r)
{
return fact(n) / (fact(r) * fact(n - r));
}
// Returns factorial of n
ll fact(ll n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
ll modularExponentiation(ll x,ll n,ll M)
{
if(n==0)
return 1;
else if(n%2 == 0) //n is even
return modularExponentiation((x*x)%M,n/2,M);
else //n is odd
return (x*modularExponentiation((x*x)%M,(n-1)/2,M))%M;
}
ll xorOfArray(ll arr[], ll n)
{
// Resultant variable
ll xor_arr = 0;
// Iterating through every element in
// the array
for (ll i = 1; i <=n; i++) {
// Find XOR with the result
xor_arr = xor_arr ^ arr[i];
}
// Return the XOR
return xor_arr;
}
// Returns (a * b) % mod
ll moduloMultiplication(ll a,
ll b,
ll mod)
{
ll res = 0; // Initialize result
// Update a if it is more than
// or equal to mod
a %= mod;
while (b)
{
// If b is odd, add a with result
if (b & 1)
res = (res + a) % mod;
// Here we assume that doing 2*a
// doesn't cause overflow
a = (2 * a) % mod;
b >>= 1; // b = b / 2
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll m=998244353;
ll a,b,c;
cin>>a>>b>>c;
ll x=a*(a+1)/2;//moduloMultiplication(a,a+1,998244353);
ll y=b*(b+1)/2;//moduloMultiplication(b,b+1,998244353);
ll z=c*(c+1)/2;//moduloMultiplication(c,c+1,998244353);
ll s=moduloMultiplication(x,y,998244353);
ll ans=moduloMultiplication(s,z,998244353);
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
ll x, y;
map<ll, vector<ll>> mp;
set<ll> s;
ll in[200005];
ll out[200005];
int cnt1, cnt2;
int main()
{
cin>>n>>m;
for (int i=1; i<=m; ++i)
{
cin>>x>>y;
mp[x].push_back(y);
}
s.insert(n);
for (auto t : mp)
{
cnt1 = 0;
cnt2 = 0;
for (auto it : t.second)
{
if (s.count(it)) out[++cnt2] = it;
if (it-1>=0 && s.count(it-1)) in[++cnt1] = it;
if (it+1<=2*n-1 && s.count(it+1)) in[++cnt1] = it;
}
for (int i=1; i<=cnt2; ++i) s.erase(out[i]);
for (int i=1; i<=cnt1; ++i) s.insert(in[i]);
}
cout<<s.size();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " is " << x << endl
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
int n, m;
vector<P> p;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
int x, y;
for (int i = 0; i < m; ++i) {
cin >> x >> y;
p.push_back({x, y});
}
sort(p.begin(), p.end());
set<int> s;
s.insert(n);
int l = 0;
while (l < m) {
vector<int> a, b;
int r = l;
while (r < m) {
if (p[r].first == p[l].first) r++;
else break;
}
for (int i = l; i < r; ++i) {
int y = p[i].second;
if ((s.find(y - 1) != s.end() || s.find(y + 1) != s.end()) && s.find(y) == s.end()) a.push_back(y);
if ((s.find(y - 1) == s.end() && s.find(y + 1) == s.end()) && s.find(y) != s.end()) b.push_back(y);
}
for (int e : a) s.insert(e);
for (int e : b) s.erase(e);
l = r;
}
cout << (int)s.size() << '\n';
return 0;
} |
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#define rep(i,n) for(int i=0;i<n;i++)
#define repn(i,n) for(int i=1;i<=n;i++)
#define repr(e,x) for(auto& e:x)
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef pair<int,int> P;
double const PI=3.141592653589793;
int const INF=1001001001;
ll const LINF=1001001001001001001;
ll const MOD=1000000007;
ll N;
ll A[200000];
int main(){
cin>>N;
rep(i,N) cin>>A[i];
vector<ll> sum(N,0);
vector<ll> t(N,0);
rep(i,N) sum[i]=A[i];
rep(i,N-1) sum[i+1]+=sum[i];
//rep(i,N) cout<<sum[i]<<' ';cout<<endl;
rep(i,N) t[i]=sum[i];
rep(i,N-1) sum[i+1]=max(sum[i+1],sum[i]);
rep(i,N-1) t[i+1]+=t[i];
//rep(i,N) cout<<sum[i]<<' ';cout<<endl;
ll ans=0;
rep(i,N-1){
ans=max(ans,t[i]+sum[i+1]);
}
ans=max(ans,t[0]);
// ll maxi=-INF;
// ll idx=0;
// rep(i,N){
// if(t[i]>maxi){
// maxi=t[i];
// idx=i;
// }
// }
cout<<ans<<endl;
return 0;
} | #include <string>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <climits>
#include <bitset>
#include <functional>
#include <numeric>
#include <ctime>
#include <cassert>
#include <cstring>
#include <fstream>
#include <iomanip>
using namespace std;
#define FOR(i, a, b) for(int (i)=(a);(i)<(b);(i)++)
#define IFOR(i, a, b) for(int (i)=(a);(i)<=(b);(i)++)
#define RFOR(i, a, b) for(int (i)=(a);(i)>=(b);(i)--)
#define REP(i, n) FOR((i), 0, (n))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int main() {
vector<string> s(3);
REP(i, 3)
cin >> s[i];
set<char> chars;
REP(i, 3){
for(auto c: s[i]) chars.insert(c);
}
if(chars.size() > 10){
cout << "UNSOLVABLE" << endl;
return 0;
}
vector<int> nums(10);
iota(nums.begin(), nums.end(), 0);
vector<int> nums_orig(nums);
vector<ll> ans = {-1, -1, -1};
do{
next_permutation(nums.begin(), nums.end());
map<char, int> assoc;
int i = 0;
for(auto c: chars){
assoc[c] = nums[i];
i++;
}
vector<string> new_str(s);
vector<ll> new_num(3);
REP(i, 3){
REP(j, new_str[i].size()){
new_str[i][j] = assoc[new_str[i][j]] + '0';
}
new_num[i] = stoll(new_str[i]);
}
if(
(new_str[0][0] == '0')
|| (new_str[1][0] == '0')
|| (new_str[2][0] == '0')) continue;
if(new_num[0] + new_num[1] == new_num[2]){
ans = new_num;
break;
}
} while(nums != nums_orig);
if(ans[0] != -1){
for(auto t: ans) cout << t << endl;
}
else{
cout << "UNSOLVABLE" << endl;
}
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <unordered_map>
#include <set>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define ls rt<<1
#define rs rt<<1|1
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define Inf 0x3f3f3f3f
#define debug(a) cout<<#a<<"="<<a<<"\n"
using namespace std;
inline void ckmin(int& a,int b){if(a>b) a=b;}
inline void ckmax(int& a,int b){if(a<b) a=b;}
const int mod=998244353;
const int maxn=2e5+5;
vector<int>g[maxn];
int n,k;
int d[maxn],ans[maxn],num[maxn];
vector<pii>qu[maxn];
void dfs(int x,int fa){
d[x]=d[fa]+1;
for(pii i:qu[x]){
ans[i.se]=-num[i.fi];
}
++num[d[x]];
for(int i:g[x]){
dfs(i,x);
}
for(pii i:qu[x]){
ans[i.se]+=num[i.fi];
}
}
int main()
{
scanf("%d",&n);
for(int x,i=2;i<=n;i++){
scanf("%d",&x);
g[x].push_back(i);
}
d[0]=-1;
int q,u,c;
scanf("%d",&q);
for(int i=1;i<=q;i++){
scanf("%d%d",&u,&c);
qu[u].push_back({c,i});
}
dfs(1,0);
for(int i=1;i<=q;i++){
printf("%d\n",ans[i]);
}
return 0;
}
| #include <bits/stdc++.h>
const int N = 200005;
int T, n; char A[N], B[N], C[N];
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n); n *= 2;
scanf("%s%s%s", A, B, C);
int cnt1 = (A[0] == '1') + (B[0] == '1') + (C[0] == '1');
int cnt2 = (A[n - 1] == '1') + (B[n - 1] == '1') + (C[n - 1] == '1');
if (cnt1 == 3 || cnt2 == 3) {
if (cnt1 == 3) printf("1");
for (int i = 1; i <= n; i++) printf("0");
if (cnt1 != 3) printf("1");
}
else if (cnt1 == 0 || cnt2 == 0) {
if (cnt1 == 0) printf("0");
for (int i = 1; i <= n; i++) printf("1");
if (cnt1 != 0) printf("0");
}
else if (cnt1 != cnt2) {
int ok = cnt1 > 1;
printf("%d", ok);
for (int i = 1; i <= n/2; i++) printf("%d", ok ^ 1);
for (int i = 1; i <= n/2; i++) printf("%d", ok);
} else {
int ok = cnt1 > 1;
for (int i = 1; i <= n/2; i++) printf("%d", ok);
for (int i = 1; i <= n/2; i++) printf("%d", ok ^ 1);
printf("%d", ok);
}
puts("");
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
// your code goes here
ll a,b,c;cin>>a>>b>>c;
if(a==b)cout<<c;
else if(b==c)cout<<a;
else if(a==c)cout<<b;
else if(a==b&&b==c)cout<<a;
else cout<<"0";
return 0;
} | // “Alhamdulillah for Everything”
#include<bits/stdc++.h>
using namespace std;
//---------DEFINES------------
#define pai acos(-1)
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>
#define sz(a) (ll)a.size()
#define endl "\n"
#define sline(s) getline(cin,s);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a*b)/gcd(a,b))
#define all(sob) sob.begin(),sob.end()
#define ms(a, b) memset(a,b,sizeof(a))
#define rep(i,b,n) for(ll i = b;i <= n; i++)
#define rev(i,b,n) for(ll i = b;i >= n; i--)
#define setp(x) cout<<fixed<<setprecision(x)
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define TEST_CASE(t) for(ll zz=1;zz<=t;zz++)
#define PRINT_CASE cout<<"Case "<<zz<<": ";
#define fii freopen("input.txt","r",stdin);
#define foo freopen("output.txt","w",stdout);
#define ActioN ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
//-------CONSTANTS------------
const ll mod = 1e9+7;
const ll mxn = 1e5+7;
const ll inf = 1e18;
//------MOD-FUNCTIONS---------
inline void normal(ll &a) { a %= mod; (a < 0) && (a += 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; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, mod - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
int main()
{
//Light_Camera:)
ActioN;
ll i,j,k,a,b,c,d,n,m,t,u,v,w,x,y,z;
vector<ll>vec(3);
for(i=0;i<3;i++)
{
cin>>vec[i];
}
sort(all(vec));
a=vec[0];
b=vec[1];
c=vec[2];
if(a==b)
{
cout<<c<<endl;
}
else if(b==c)
{
cout<<a<<endl;
}
else
{
cout<<0<<endl;
}
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<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long int
#define endl "\n"
#define MOD 1000000007
#define mod 1000000007
#define M 1000000007
#define pb push_back
#define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mx 1000005
#define fiint(a,b) memset(a,b,sizeof(a))
#define bitcount __builtin_popcount
#define fori(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define debug(x) cout<<x<<endl;
#define cases(t) int t; cin>>t; while(t--)
#define inf1 INT_MAX
#define all(a) a.begin(),a.end()
#define vec vector<int>
#define pii pair<int,int>
#define plii pair<int,int>
#define pint pair<int,int>
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mp make_pair
#define sz(x) (int)x.size()
#define PI 3.14159265359
const long long INF=1e18;
bool chmin(int& a, int b){ if(a > b){ a = b; return 1; } return 0; }
int factorial[10000001]={0};
int gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
int powerFunction(int x,int y){
int res = 1;
int p = mod;
x = x;
while (y > 0){
if (y & 1)
res = (res*x)%p ;
y = y>>1;
x = (x*x)%p ;
}
return res;
}
void factorialFunction(int maxLimit){
factorial[0]=1;
for(int i=1 ; i <= maxLimit ; i++)
factorial[i]=(factorial[i-1]*i)%MOD;
return;
}
int nCr(int n, int r)
{
if(r < 0 || r > n){
return 0;
}
int temp = factorial[n];
temp *= (powerFunction(factorial[r], MOD-2)%MOD);
temp %= MOD;
temp *= (powerFunction(factorial[n-r], MOD-2)%MOD);
temp %= MOD;
return temp;
}
map<int,int> dp;
int find(int x ,int y )
{
if(x >= y)
return x - y;
if(dp.find(y) != dp.end())
return dp[y];
if(y % 2 == 0 )
{
int a = find(x , y/2 ) + 1;
dp[y] = min(y - x , a);
return dp[y];
}
else
{
int a = find( x , (y+1)/2 ) + 2;
int b = find(x , (y-1)/2) + 2;
dp[y] = min(min(a,b) , y - x );
return dp[y];
}
}
signed main()
{
int x,y;
cin>>x>>y;
cout<< find(x,y)<<endl;
}
| #include<cstdio>
#include<cctype>
#include<algorithm>
#define LL long long
using namespace std;
template <class I>
inline void read(I &z)
{
z=0;
char c=getchar();int base=1;
while (!isdigit(c) && c!='-') c=getchar();
if (c=='-') c=getchar(),base=-1;
while (isdigit(c)) z=z*10+c-'0',c=getchar();
z*=base;
}
int x,y,z,n,ans=2147483647;
int main(int argc, char const *argv[])
{
read(n);
for (int i=1;i<=n;++i)
{
read(x),read(y),read(z);
if (x<z) ans=min(ans,y);
}
if (ans==2147483647) printf("-1"); else printf("%d",ans);
return 0;
}
|
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define f0(i,n) for(i=0;i<n;i++)
#define rf0(i,n) for(i=n-1;i>=0;i--)
#define f2(i,n) for(i=1;i<n;i++)
#define f1(i,n) for(i=1;i<=n;i++)
#define fab(i,a,b) for(i=a;i<=b;i++)
#define shr ll t;cin>>t; while(t--)
#define fi first
#define sc second
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpll vector<pll>
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define dev(a) for(auto it:a)cout<<it<<" "
#define prec(nm,prc) cout<<fixed<<setprecision(nm)<<prc<<" "
#define print(a) cout<<a<<"\n";
#define print2(a,b) cout<<a<<" "<<b<<"\n";
#pragma GCC optimization ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define nl cout<<"\n"
#define ye cout<<"Yes\n"
#define yee cout<<"YES\n"
#define no cout<<"No\n"
#define noo cout<<"NO\n"
#define ln length()
#define ms(cnt,r) memset(cnt, r, sizeof(cnt))
using namespace std;
bool comp(pll &a,pll &b)
{
return (a.sc<b.sc);
}
ll power(ll n,ll p)
{
if(p==0)
return 1;
else
{
ll ans=power(n,p/2)%mod;
ans=(ans*ans)%mod;
if(p%2==0)
return ans;
else
return ans=(ans*n)%mod;
}
}
bool isPrime(ll n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
/********************HAR HAR MAHADEV***********JAI BAJRANG BALI************************/
int main()
{
FAST;
/* ifstream cin;
cin.open("input.txt");
ofstream cout;
cout.open("output.txt");
*/
//shr
{
ll i,j,n,a,b,c,d,m,sum=0,ans=0,cnt=0,r=0,e=0;
string str,s1,om,s;
unordered_map<ll,ll>mp,mp1;
set<ll>st;
cin>>n>>a>>b;
f0(i,n)
{
cin>>r>>e;
if(r<a&&e>b)cnt=1;
}
if(cnt)ye;
else no;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define vll vector<ll>
#define pll pair<ll,ll>
#define forA(i,a,n,x) for(ll i=a;i<n;i+=x)
#define forD(i,a,n,x) for(ll i=n-1;i>=a;i-=x)
#define ranit(it,n) for(auto it:n)
#define testCases ll t;cin>>t;while(t--)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define init(a,x) memset(a,x,sizeof(a))
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll n,s,d;
cin>>n>>s>>d;
ll tdamage=0;
while(n--)
{
ll x,y;
cin>>x>>y;
if(x<s && y>d)tdamage|=1;
}
if(tdamage)cout<<"Yes";
else cout<<"No";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
char s[200010];
char d1[]={'1', '1', '0'};
char d2[]={'1', '0', '1'};
char d3[]={'0', '1', '1'};
int main()
{
ll i, j, n, m;
scanf("%lld %s", &n, s);
if(n==1)
{
if(s[0]=='1') printf("20000000000\n");
else printf("10000000000\n");
return 0;
}
ll ex=0;
ll in=0;
for(i=0; i<n; i++) if(s[i]!=d1[i%3]) in=1;
if(in==0)
{
n=(n+2)/3;
n=1e10-n +1;
printf("%lld\n", n);
return 0;
}
in=0;
for(i=0; i<n; i++)
{
if(s[i]!=d2[i%3]) in=1;
//cout<<s[i]<<" "<<d2[i%3]<<endl;
}
if(in==0)
{
n+=1;
n=(n+2)/3;
n=1e10-n +1;
printf("%lld\n", n);
return 0;
}
in=0;
for(i=0; i<n; i++) if(s[i]!=d3[i%3]) in=1;
if(in==0)
{
n+=2;
n=(n+2)/3;
n=1e10-n +1;
printf("%lld\n", n);
return 0;
}
printf("0\n");
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using LL = long long;
using P = pair<int,int>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;
const int query = 1000;
const int grid_size = 30;
const int grid_total = grid_size * grid_size;
const int initial_length = 5000;
string path_ex(int si, int sj, int ti, int tj){
int di = ti - si, dj = tj - sj;
string s;
if(di >= 0) rep(k,di) s.push_back('D');
else rep(k,-di) s.push_back('U');
if(dj >= 0) rep(k,dj) s.push_back('R');
else rep(k,-dj) s.push_back('L');
return s;
}
vector<vector<int>> G;
int cost[grid_total][grid_total];
string dijkstra(int st, int ed){
int N = G.size();
vector<int> dist(N, INF), par(N, -1);
dist[st] = 0;
priority_queue<P,vector<P>,greater<P>> q;
q.push({dist[st], st});
while(!q.empty()){
P p = q.top(); q.pop();
int d = p.first, n = p.second;
if(dist[n] < d) continue;
for(auto v : G[n]){
int c = cost[n][v];
if(dist[v] <= d + c) continue;
dist[v] = d + c;
q.push({dist[v], v});
par[v] = n;
}
}
string path;
int now = ed;
while(now != st){
char c;
if(par[now] + 1 == now) c = 'R';
else if(par[now] - 1 == now) c = 'L';
else if(par[now] + grid_size == now) c = 'D';
else c = 'U';
path.push_back(c);
now = par[now];
}
reverse(path.begin(), path.end());
return path;
}
int main(){
//FILE *outputfile;
//outputfile = freopen("test.txt", "w", stdout);
G.resize(grid_total);
rep(i,grid_size){
rep(j,grid_size) cost[i][j] = INF;
}
rep(i,grid_size){
rep(j,grid_size-1){
int l = i * grid_size + j, r = i * grid_size + j + 1;
assert(l < grid_total and r < grid_total);
G[l].emplace_back(r);
G[r].emplace_back(l);
cost[l][r] = initial_length, cost[r][l] = initial_length;
}
}
rep(j,grid_size){
rep(i,grid_size-1){
int u = i * grid_size + j, d = (i + 1) * grid_size + j;
assert(u < grid_total and d < grid_total);
G[u].emplace_back(d);
G[d].emplace_back(u);
cost[u][d] = initial_length, cost[d][u] = initial_length;
}
}
rep(q,query){
int si, sj, ti, tj;
cin >> si >> sj >> ti >> tj;
int st = si * grid_size + sj, ed = ti * grid_size + tj;
assert(st < grid_total and ed < grid_total);
string path = dijkstra(st, ed);
cout << path << endl;
int dist_result;
cin >> dist_result;
int now = st, pre = -1;
int path_size = path.size();
rep(k,path_size){
pre = now;
if(path[k] == 'R') now++;
else if(path[k] == 'L') now--;
else if(path[k] == 'D') now += grid_size;
else now -= grid_size;
int now_length = cost[pre][now];
int ave_length = dist_result / path_size;
cost[pre][now] = (now_length * (query - q - 1) + ave_length * (q + 1)) / query;
}
}
return 0;
}
|
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define pb push_back
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin >> s;
string res = "";
for(char c : s){
if(c == '.') break;
else res +=c;
}
cout << res;
return 0;
} | #include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
cout << 100-(n%100) << "\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pl pair<ll,ll>
#define pll pair<ll,pl>
#define pi pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
ll mod = 1e9+7;
const int IMAX = 2000500;
const int V = 2100;
const ll LMAX = 99999999999999999;
const ll p = 1000000000;
int R[] = {-1,0,1,0};
int C[] = {0,1,0,-1};
void solve(int test)
{
string s,s1;
cin>>s;
int n = s.length(),cnt=0;
for(int i=0;i<=n-4;i++)
{
s1 = s.substr(i,4);
if(s1 == "ZONe")
cnt++;
}
cout<<cnt<<"\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
// cin>>t;
t = 1;
// preprocess();
for(int i=1;i<=t;i++)
solve(i);
}
/////////////////////////////////// UNION - FIND ////////////////////////
/*void initialize()
{
for(int i=0;i<MAXM;i++)
{
root[i] = i;
size[i] = 1;
}
}
int find(int a)
{
while(a!=root[a])
{
root[a] = root[root[a]];
a = root[a];
}
return a;
}
void merge(int a,int b)
{
int roota = find(a);
int rootb = find(b);
if(size[roota] < size[rootb])
{
size[rootb] += size[roota];
root[roota] = root[rootb];
}
else
{
size[roota] += size[rootb];
root[rootb] = root[roota];
}
}*/ | #include <bits/stdc++.h>
using namespace std;
long long n, ans;
int main (){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
long long lo=0, hi=1500000007;
while (lo<hi){
long long mid=(lo+hi+1)/2;
long long cur = mid*(mid+1)/2;
if (cur <= (n+1)) lo=mid;
else hi=mid-1;
}
cout << (n-lo+1);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define M 100005
LL A[M],sum[M],K;
int main(){
int n,q;
cin>>n>>q;
A[0]=0;
for(int i=1;i<=n;i++){
scanf("%lld",&A[i]);
sum[i]=sum[i-1]+A[i]-A[i-1]-1;
}
sum[n+1]=2e18-A[n];
cout<<endl;
while(q--){
scanf("%lld",&K);
int L=1,R=n+1,res;
while(L<=R){
int mid=(L+R)>>1;
if(sum[mid]>=K){
R=mid-1;res=mid;
}else L=mid+1;
}
printf("%lld\n",A[res-1]+K-sum[res-1]);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define vt vector
#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
// 🚩
// 20210306 21:02:45~
//
int main()
{
ll A, B;
cin >> A >> B;
ll C = A + B;
ll ans = 0;
if (C >= 15 && B >= 8)
{
ans = 1;
}
else if (C >= 10 && B >= 3)
{
ans = 2;
}
else if (C >= 3)
{
ans = 3;
}
else
{
ans = 4;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define int long long
typedef unsigned long long lu;
typedef vector<ll> v;
typedef vector<vector<ll> > vv;
typedef vector<string> vs;
typedef vector<pair<ll,ll>> vpr;
typedef vector<bool>vb;
typedef vector<double>vd;
typedef long double ld;
#define f(i,n) for(ll i = 0; i < n; i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define endl "\n"
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define amax(x,y) if(y>x) x=y;
#define amin(x,y) if(y<x)x=y;
#define bg(x) x.begin()
#define sz(x) (ll)x.size()
#define in(x,n) for(ll i=0;i<n;i++)cin>>x[i]
#define out(x,n) for(ll i=0;i<n;i++)cout<<x[i]<<" "
#define mxt(a) *(max_element(a.begin(),a.end()))
#define mnt(a) *(min_element(a.begin(),a.end())
#define tc ll t;cin>>t;while(t--)
typedef pair<ll,ll> pi;
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define yesno(f) if(f) yes else no
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
const ld PI = 2 * acos(0.0);
ll cel(ll x1,ll y1){if((x1%y1)==0)return x1/y1;else return x1/y1+1;}
ll power(ll a,ll b,ll m)
{
if(b==0)
return 1;
ll d=power(a,b/2,m);
d=(d*d)%m;
if(b&1)
d=(d*a)%m;
return d;
}
const ll mod=1e9+7;
int MOD(int a)
{
if(a<0)
a+=mod;
if(a>=mod)
a%=mod;
return a;
}
// set_name.find_by_order(k) It returns to an iterator to the kth element (counting from zero) in the set in O(logn) time
// set_name.order_of_key(k) It returns to the number of items that are strictly smaller than our item k in O(logn) time.
/*string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
str.substr (x) : returns a substring str[x],... end of string
str.find(qtr) : returns the first occurenece of qtr in str */
int32_t main()
{
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n;cin>>n;
v a(n);in(a,n);
int g=0;
f(i,n)
g=__gcd(g,a[i]);
cout<<g<<endl;
return 0;
} | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
# define M_PI 3.14159265358979323846
const int M=1e9+7;
long long mod(long long x){
return ((x%M + M)%M);
}
long long add(long long a, long long b){
return mod(mod(a)+mod(b));
}
long long mul(long long a, long long b){
return mod(mod(a)*mod(b));
}
ll modPow(ll a, ll b){
if(b==0)
return 1LL;
if(b==1)
return a%M;
ll res=1;
while(b){
if(b%2==1)
res=mul(res,a);
a=mul(a,a);
b=b/2;
}
return res;
}
ll lcm(ll a, ll b){
ll ans=(a*b)/(ll)(__gcd(a,b));
return ans;
}
void solve(){
ll n;
cin>>n;
ll ans=1;
for(ll i=2;i<=n;i++){
ans=lcm(ans,i);
}
cout<<ans+1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout<<fixed;
cout<<setprecision(10);
// freopen("timber_input.txt", "r", stdin);
// freopen("timber_output.txt", "w", stdout);
int t=1;
// cin>>t;
for(int i=1;i<=t;i++){
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
map<string, bool> mp;
string ans = "satisfiable";
while(N--) {
string s;
cin >> s;
if(ans != "satisfiable") continue;
bool exist = (s[0] == '!' ? true : false);
if(exist) s.erase(s.begin());
if(mp.count(s) && mp[s] != exist) ans = s;
mp[s] = exist;
}
cout << ans << '\n';
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define FAST cin.tie(nullptr)->sync_with_stdio(false);
#define PRECISE cout << fixed << setprecision(19);
#define ONE int tc = 1;
#define GO for (int i = 0; i < tc; ++i) { solve(); }
using vi = vector<int>;
#define all(x) (x).begin(), (x).end()
#define ice(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
void solve() {
int n; cin >> n;
vi a(n), b(n);
ice(i, 0, n) {
cin >> a[i];
}
ice(i, 0, n) {
cin >> b[i];
}
cout << max(0, *min_element(all(b)) - *max_element(all(a)) + 1);
}
int main() {
FAST PRECISE ONE GO
} |
#include <bits/stdc++.h>
using namespace std;
//solve関数。falseなら7が入っている。trueなら7が入っていない。
bool solve7(int n){
while(n!=0){
if(n%8==7){
return false;
}
//ケタを下ろしていく(1の桁以外を調べる)
n /= 8;
}
return true;
}
bool solve10(int n){
while(n!=0){
if(n%10==7){
return false;
}
//ケタを下ろしていく(1の桁以外を調べる)
n /= 10;
}
return true;
}
int main(void)
{
int n;
cin >> n;
int cnt = 0;
for( int i=1 ; i<=n ; i++ ){
//どっちの基準でも7が入っていなかったら、true。
//数字をカウントする
if( solve7(i) && solve10(i) ){
cnt++;
}
}
cout << cnt << endl;
return 0;
} | #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define fi first
#define se second
long long mo = 1000000007;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
string zero_padding(int val, int nf){ ostringstream sout;sout << setfill('0') << setw(nf) << val; return sout.str();};
ld PI=3.14159265358979323846;
//using namespace atcoder;
string toeight(ll a){
string ret = "";
while(a>0){
ll r = a % 8;
ret = ret + char(r +'0');
a /= 8;
}
return ret;
}
int main(){
ll N;
cin >> N;
ll ans = 0;
REP(i,1,N+1){
string S = to_string(i);
string T = toeight(i);
rep(j,S.size()){
if(S[j] == '7') goto gt;
}
rep(j,T.size()){
if(T[j] == '7') goto gt;
}
ans++;
gt:
continue;
}
cout << ans << endl;
} |
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
//#include<ctime>
//#include<algorithm>//next_permutation
#define rep(i,n) for (int i = 0;i < (n);i++)
#define all(v) v.begin(),v.end()
#define dec(n) cout << fixed << setprecision(n);
#define large "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define small "abcdefghijklmnopqrstuvwxyz"
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
ll gcd(ll a,ll b){
if(b == 0) return a;
return gcd(b , a % b);
}
const ll MOD = 1000000007;
const ll MAX = 2000001;
ll mod(ll a){
return a % MOD;
}
ll lcm(ll a,ll b){
return (a*b)/gcd(a,b);
}
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(){
ll n; cin >> n;
vvl cost(n,vl(n));
vl x(n); vl y(n); vl z(n);
rep(i,n) cin >> x[i] >> y[i] >> z[i];
rep(i,n){
rep(j,n){
//各都市から各都市へのコストを計算
cost[i][j] = abs(x[j] - x[i]) + abs(y[j] - y[i]) + max(0LL,z[j] - z[i]);
}
}
ll inf = 1e18;
vvl dp((1<<n),vl(n,inf)); //dp[訪れた都市の集合][最後に訪れた都市]
dp[1][0] = 0; //最初は都市1(0-indexedでは0)にいるのでコスト0
for(ll i=1; i < (1<<n); i++){ //都市の集合のループ
if(i % 2 == 0) continue; //都市1には行ったことがあるので,必ず奇数になる
for(ll j=1; j < n; j++){ //これから向かう都市のループ
if(i & (1<<j)) continue; //訪れたことのある都市に向かおうとしていたらcontinue
for(ll k=0; k < n; k++){ //最後に来た都市のループ
if(!(i&(1<<k))) continue; //まだ訪れていない都市からやってこようとしたらcontinue
//もともとの行き方よりも,訪れた都市集合がi,最後に来た都市kの状態で,
//これから都市jに向かった方がコストがかからない場合,更新
if(dp[i|(1<<j)][j] > dp[i][k] + cost[k][j]){
dp[i|(1<<j)][j] = dp[i][k] + cost[k][j];
}
}
}
}
ll ans = 1e18;
rep(i,n){
//全ての都市を回った状態で,都市1に戻ってくる
ans = min(dp[(1<<n)-1][i] + cost[i][0],ans);
}
cout << ans << endl;
} | #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int n; cin >> n;
vector<int> l(n + 1, mod);
vector<int> r(n + 1, -mod);
rep(i, n) {
int x, c; cin >> x >> c;
l[c] = min(l[c], x);
r[c] = max(r[c], x);
}
vector<LP> dp;
dp.push_back({ 0,0 });
rep1(c, n) {
if (l[c] == mod)continue;
ll lval = INF, rval = INF;
for (LP p : dp) {
ll loc = p.first;
ll val = p.second;
lval = min(lval, val + abs(r[c] - loc) + r[c] - l[c]);
rval = min(rval, val + abs(l[c] - loc) + r[c] - l[c]);
}
dp.clear();
dp.push_back({ l[c],lval });
dp.push_back({ r[c],rval });
}
ll ans = INF;
for (LP p : dp) {
ll loc = p.first;
ll val = p.second;
ans = min(ans, abs(loc) + val);
}
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//int t; cin >> t; rep(i, t)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF (int)1e9
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
//
#define FOR(i, a, b, in) for (int i=a ; i<(b) ; i+=in)
#define RFOR(i, a, b, in) for (int i=a-1 ; i>=(b) ; i-=in)
#define REP(i, a, b) FOR(i, a, b, 1)
#define RREP(i, a, b) RFOR(i, a, b, 1)
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define all(cont) cont.begin(), cont.end()
void solve () {
int ans = 1e9;
bool ok = 0;
int n;
cin >> n;
REP (i, 0, n) {
int a, p, x;
cin >> a >> p >>x;
if (a < x) {
ans = min(ans, p);
ok = true;
}
}
if (!ok) ans = -1;
cout << ans << "\n";
}
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
//$ sudo g++ -o name name.cpp
//$ ./name
| #include<bits/stdc++.h>
using namespace std;
int main(){
int n,x=1e8,y=1e8,x1,y1,i,s,p,q,a,b,c=1e8,d=1e8;
cin>>n;
int arr[n][2];
for(i=0;i<n;i++){
cin>>arr[i][0]>>arr[i][1];
if(arr[i][0]<x){
x=arr[i][0];
x1=i;}
if(arr[i][1]<y){
y=arr[i][1];
y1=i;}}
if(x1==y1){
p = x+y;
for(i=0;i<n;i++){
if(i!=x1){
c = min(c,arr[i][0]);}}
for(i=0;i<n;i++){
if(i!=y1){
d=min(d,arr[i][1]);}}
c=max(c,y);
d=max(d,x);
c=min(c,d);
c = min(c,p);
cout<<c;}
else{
p = max(x,y);
cout<<p;}
return 0;} |
#include <bits/stdc++.h>
using namespace std;
// template {{{
#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
constexpr i32 mod = 1e9 + 7;
// constexpr i32 mod = 998244353;
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;
constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};
struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;
template <typename T = i64> T input() { T x; cin >> x; return x; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}
void solve() {
i64 n = input(), k = input();
auto dp = make_vector(3, 4 * n, 0ll);
range(a, 1, n + 1) dp[0][a] = 1;
range(i, 0, 2) {
range(a, 1, dp[i].size() - n - 1) {
dp[i + 1][a + 1] += dp[i][a];
dp[i + 1][a + n + 1] -= dp[i][a];
}
range(b, 1, dp[i].size()) dp[i + 1][b] += dp[i + 1][b - 1];
}
i64 s = 3;
while (s < 3 * n) {
// i64 p = (s - 1) * (s - 2) / 2;
i64 p = dp[2][s];
// debug(p);
if (k <= p) break;
k -= p;
s++;
}
i64 a = 1;
while (a < n) {
i64 d = s - a;
i64 l = d - n;
i64 r = n;
i64 p = max(r - l + 1, i64(0));
p = dp[1][d];
if (k <= p) break;
k -= p;
a++;
}
range(b, 1, s - a + 1) {
int c = (s - a) - b;
if (c > n) continue;
k--;
if (k) continue;
cout << a << " " << b << " " << c << endl;
return;
}
}
signed main() {
solve();
}
| #include<bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define put(i) cout<<i<<endl
#define rep(i,s,n) for(long long i=s;i<(long long)(n);i++)
using namespace std;
using ll = long long;
int main(){
ll a, b, c;
cin >> a >> b >> c;
if(a*a + b*b < c*c) put("Yes");
else put("No");
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;};
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;
int main(){
int n; cin >> n;
VI a(n), b(n), c(n), d(n);
REP(i,n) cin >> a[i] >> b[i], a[i]*=n, b[i]*=n;
REP(i,n) cin >> c[i] >> d[i], c[i]*=n, d[i]*=n;
if(n==1){
cout << "Yes" << endl;
return 0;
}
double csx=0, csy=0, ctx=0, cty=0;
REP(i,n){
csx+=a[i];
csy+=b[i];
ctx+=c[i];
cty+=d[i];
}
csx/=n, csy/=n, ctx/=n, cty/=n;
REP(i,n){
a[i]-=csx;
b[i]-=csy;
c[i]-=ctx;
d[i]-=cty;
}
if(a[0]==0&&b[0]==0){
swap(a[0],a[1]);
swap(b[0],b[1]);
}
REP(i,n){
if(c[i]==0&&d[i]==0)
continue;
double ang=atan2(d[i],c[i])-atan2(b[0],a[0]);
bool ok=1;
REP(j,n){
double x=a[j]*cos(ang)-b[j]*sin(ang);
double y=a[j]*sin(ang)+b[j]*cos(ang);
bool f=0;
REP(k,n){
if(abs(x-c[k])<=1e-6&&abs(y-d[k])<=1e-6)
f=1;
}
if(!f){
ok=0;
break;
}
}
if(ok){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
} | #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0'||c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c&&c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
const int MaxN = 200005;
const int Mod = 998244353;
vector<int> v[MaxN];
map<int,int> mp;
int c[MaxN]; // BIT
int main(){
int n = readint();
for(int i=1,t=0; i<=n; ++i){
int &x = mp[readint()+i];
if(!x) x = ++ t; // new id
v[x].push_back(i);
}
for(int i=1; i<=n; ++i){
int len = v[i].size();
rep(j,0,len-1-j)
swap(v[i][j],v[i][len-1-j]);
}
int_ ans = 0;
for(int i=1; i<=n; ++i){
int &x = mp[readint()+i];
if(!x || v[x].empty()){
puts("-1"); return 0;
}
int id = n+1-v[x].back();
v[x].pop_back(); // match
for(int j=id; j; j-=(j&-j))
ans += c[j]; // inversion
for(int j=id; j<=n; j+=(j&-j))
++ c[j]; // count
}
printf("%lld\n",ans);
return 0;
}
|
#include<iostream>
#include<vector>
#include<string>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}
template<typename T, long long MOD_VALUE>
class ModInt {
static constexpr long long MOD = MOD_VALUE;
private:
T value_;
public:
ModInt() {}
ModInt(const T& value) {if (value >= 0) {value_ = value % MOD;} else {T k = (MOD - 1 - value) / MOD; value_ = (value + k * MOD) % MOD;}}
ModInt& operator+=(const ModInt& x) {value_ += x.value_; if (value_ >= MOD) value_ -= MOD; return *this;}
friend ModInt& operator+=(const T& x, const ModInt& y) {ModInt res(x); res.value_ += x.value_; if (res.value_ >= MOD) res.value_ -= MOD; return res;}
ModInt& operator-=(const ModInt& x) {if (value_ < x.value_) value_ += MOD; value_ -= x.value_; return *this;}
friend ModInt& operator-=(const T& x, const ModInt& y) {ModInt res(x); if (res.value_ < y.value_) res.value_ += MOD; res.value_ -= y.value_; return res;}
ModInt& operator*=(const ModInt& x) {value_ = (value_ * x.value_) % MOD; return *this;}
friend ModInt& operator*=(const T& x, const ModInt& y) {ModInt res(x); res.value_ = (res.value_ * y.value_) % MOD; return res;}
const ModInt operator+(const ModInt& x) const {return ModInt(*this) += x;}
friend const ModInt operator+(const T& x, const ModInt& y) {return ModInt(x) += y;}
const ModInt operator-(const ModInt& x) const {return ModInt(*this) -= x;}
friend const ModInt operator-(const T& x, const ModInt& y) {return ModInt(x) -= y;}
const ModInt operator*(const ModInt& x) const {return ModInt(*this) *= x;}
friend const ModInt operator*(const T& x, const ModInt& y) {return ModInt(x) *= y;}
static ModInt modpow(ModInt x, long long y) {ModInt z = 1; while (y > 0) {if (y & 1) {z *= x;}x *= x; y /= 2;} return z;}
ModInt& operator/=(const ModInt& x) {return *this *= modpow(x, MOD - 2);}
const ModInt operator/(const ModInt& x) const {return ModInt(*this) /= x;}
friend const ModInt operator/(const T& x, const ModInt& y) {return ModInt(x) /= y;}
ModInt operator++(int) {ModInt tmp(*this); value_ = (value_ + 1 == MOD ? 0 : value_ + 1); return tmp;}
ModInt operator--(int) {ModInt tmp(*this); value_ = (value_ - 1 < 0 ? MOD - 1 : value_ - 1); return tmp;}
friend istream& operator>>(istream& stream, ModInt& x) {stream >> x.value_; x.value_ %= MOD; return stream;}
friend ostream& operator<<(ostream& stream, const ModInt& x) {stream << x.value_; return stream;}
};
using mint = ModInt<ll, 1000000007>;
void solve() {
ll N, A, B;
cin >> N >> A >> B;
mint K = max(N - A - B + 1, 0LL);
mint cnt = K * (K + 1); // 片方が接しない通り数
mint ans = cnt * mint(N - A + 1) * mint(N - B + 1) * 2;
ans -= cnt * cnt;
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int T;
cin >> T;
rep(_, 0, T) {
solve();
}
return 0;
} | #pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline ll read()
{
ll x=0,w=1; char c=getchar();
while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
return w==1?x:-x;
}
const ll mod=1000000007;
inline ll C(ll x){return (x*(x+1ll)/2ll)%mod;}
inline ll Mul(ll a,ll b){return (a%mod*b%mod)%mod;}
int main()
{
int T=read();
while(T--)
{
ll n=read(),a=read(),b=read(),ans=0;
if(a+b>n) {puts("0"); continue;}
ans=Mul(4,C(n-a-b+1));
ans=Mul(ans,Mul(n-a+1,n-b+1));
ll res=Mul(C(n-a-b+1),C(n-a-b+1)); res=Mul(4,res);
ans=(ans-res+mod)%mod;
printf("%lld\n",ans);
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
/*#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
*/typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
#define int ll
#define LOCAL 0
#define dbg(x) cout << #x << " is " << x << "\n"
#define gll(x) scanf("%d",&x)
#define gll2(x,y) scanf("%d%d",&x,&y)
#define gll3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define gllarr(arr,n) f(i,n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define rs(v) { s(v) ; r(v) ; }
#define r(v) {reverse(all(v));}
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,n) for(int i=n-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e16;
const ld eps = 1e-12;
const ll N = (int)1e5 + 5;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
inline ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
inline ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
inline ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = mul(x, x, m); if(b % 2) x = mul(x, a, m); return x;}
vector< pair<int, pair<int,int> > > edges;
map<pair<int, int>, int> mp;
int par[N], sz[N];
int n, m;
vector<pair<int, int> > adj[N];
int ans[N];
struct DSU{
void init()
{
f(i, N) {
par[i] = i;
sz[i] = 1;
}
}
int find(int x){
if(x == par[x]) return x;
return par[x] = find(par[x]);
}
void merge(int x, int y) {
int u = find(x);
int v = find(y);
if (sz[u] <= sz[v])
{
par[u] = par[v];
sz[v] += sz[u];
}
else
{
par[v] = par[u];
sz[u] += sz[v];
}
}
};
ll kruskal()
{
ll ret = 0ll;
DSU dsu;
dsu.init();
for(auto it : edges)
{
int w = it.first, u = it.second.first, v = it.second.second;
int par_u = dsu.find(u), par_v = dsu.find(v);
if(par_u == par_v) continue;
adj[u].pb({v, mp[{u, v}]});
adj[v].pb({u, mp[{u, v}]});
dsu.merge(u,v);
}
return ret;
}
void dfs(int src = 0, int par = -1) {
for(auto it: adj[src]) {
int v = it.first, c = it.second;
if (v == par) continue;
if (c == ans[src]) ans[v] = (c + 1) % n;
else ans[v] = c;
dfs(v, src);
}
}
void solve() {
cin>>n>>m;
f(i, m) {
int u, v, c;
cin>>u>>v>>c;
u--, v--, c--;
if(mp.find({u, v}) != mp.end()) continue;
edges.push_back({i, {u, v}});
mp[{u, v}] = c;
mp[{v, u}] = c;
}
kruskal();
ans[0] = 0;
dfs();
f(i, n) cout<<ans[i] + 1<<"\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (LOCAL) {
freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\input.txt", "r", stdin);
freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\output.txt", "w", stdout);
}
int t = 1;
//cin>>t;
for(int test = 1; test <= t; test++) {
//cout<<"Case #"<<test<<": ";
solve();
}
return 0;
} | #include<cstdio>
#include<algorithm>
#include<vector>
#define For(i,A,B) for(i=(A);i<=(B);++i)
#define pb push_back
using namespace std;
const int N=100050;
int G[N],to[N*2],w[N*2],nxt[N*2],sz,f[N],a[N];
inline void adde(int u,int v,int c){
to[++sz]=v;w[sz]=c;nxt[sz]=G[u];G[u]=sz;
to[++sz]=u;w[sz]=c;nxt[sz]=G[v];G[v]=sz;
}
int gf(int x){return x==f[x]?x:f[x]=gf(f[x]);}
void dfs(int u,int fa,int c){
int i,v;
vector<int> t;
if(c>0)a[u]=c;
else if(c<0)t.pb(-c);
for(i=G[u];i;i=nxt[i])if((v=to[i])!=fa){
dfs(v,u,w[i]==c?-c:w[i]);
t.pb(w[i]);
}
if(c<=0){
sort(t.begin(),t.end());
t.erase(unique(t.begin(),t.end()),t.end());
For(i,0,t.size()-1)if(t[i]!=i+1){
a[u]=i+1;
break;
}
if(!a[u])a[u]=t.size()+1;
}
}
int main(){
int n,m,i,u,v,c,x,y;
scanf("%d%d",&n,&m);
For(i,1,n)f[i]=i;
while(m--){
scanf("%d%d%d",&u,&v,&c);
if((x=gf(u))!=(y=gf(v))){
adde(u,v,c);
f[x]=y;
}
}
dfs(1,0,0);
For(i,1,n)printf("%d\n",a[i]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int,int> pii;
typedef long long ll;
const long long MOD=1000000007;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep2(i,m,n) for(ll i=(m);i<(n);i++)
#define ALL(v) v.begin(), v.end()
#define pb push_back
int main(){
ll a,b,c,d;
cin>>a>>b>>c>>d;
int ans=0;
if (a==c and b==d){
ans=0;
}
else if ((a+b)==(c+d) or (a-b)==(c-d) or (abs(a-c)+abs(b-d))<=3){
ans=1;
}
else if ((a+b+c+d)%2==0){
ans=2;
}
else {
bool flag=false;
for (int i=-2;i<=2;i++){
for (int j=-2;j<=2;j++){
int x=a+i;
int y=b+j;
if ((x+y)==(c+d) or (x-y)==(c-d) or (abs(x-c)+abs(y-d))<=3){
flag=true;
}
}
}
int x=a-3;
int y=b;
if ((x+y)==(c+d) or (x-y)==(c-d) or (abs(x-c)+abs(y-d))<=3){
flag=true;
}
x=a+3;
if ((x+y)==(c+d) or (x-y)==(c-d) or (abs(x-c)+abs(y-d))<=3){
flag=true;
}
x=a;
y=b-3;
if ((x+y)==(c+d) or (x-y)==(c-d) or (abs(x-c)+abs(y-d))<=3){
flag=true;
}
y=b+3;
if ((x+y)==(c+d) or (x-y)==(c-d) or (abs(x-c)+abs(y-d))<=3){
flag=true;
}
if (flag){
ans=2;
}
else {
ans=3;
}
}
cout<<ans<<endl;
} | #include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main(void){
int r1, c1, r2, c2;
cin >> r1 >> c1;
cin >> r2 >> c2;
if((r1==r2)&&(c1==c2)){
cout << 0 << endl;
}else if((r1+c1 == r2+c2)||(r1-c1 == r2-c2)){
cout << 1 << endl;
}else if(abs(r1-r2)+abs(c1-c2)<=3){
cout << 1 << endl;
}else if((r1+c1)%2 == (r2+c2)%2){
cout << 2 << endl;
}else if(min(abs(r1+c1-r2-c2), abs(r1-c1-r2+c2))<=3){
cout << 2 << endl;
}else if(abs(r1-r2)+abs(c1-c2)<=6){
cout << 2 << endl;
}else{
cout << 3 << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#include <math.h>
#include <cmath>
using namespace std;
using ll =long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vvs = vector<vs>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vp =vector<pair<ll,ll>>;
using vvp =vector<vp>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const long long INF = 1LL << 60;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
#define all(a) a.begin(),a.end()
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define fr(i,a,b) for(int i=a;i<b;i++)
#define pri(a) printf("%.14lf\n",a);
#define MOD 1000000007
#define dup(x,y) (((x)+(y)-1)/(y))
#define fi first
#define se second
#define sz(x) (ll)(x).size()
bool is_int_lround(double x){
return std::lround(x)==x;
}
ll keta(ll x){
ll n=0;
while(x>0){
x /=10 ;
n ++;
}
return n;
}
ll conbi(int n,int m){
cin>>n>>m;
vector<ll> a(100);
a[0] =1;
for(int i=0;i<14;i++){
a[i+1]=a[i]*(i+1);
}
return a[n] /(a[m] *a[n-m]);
}
long long fac[510000], finv[510000], inv[510000];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < 510000; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
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;
}
ll kaijo(ll x){
ll z=1;
if(x==0){
return 1;
}
while(x>0){
z *=x;
z %=MOD;
x--;
}
return z;
}
ll yakusu_num(ll n){
vl yakusu(n+1,1);
for(ll i=2;i*i<=n;i++){
while(n%i==0){
n /=i;
yakusu[i]++;
}
}
if(n!=1)yakusu[n]++;
ll num=1;
for(ll i=0;i <=n;i++){
num*=yakusu[i];
}
return num;
}
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
for(int i = 0; i < N; i++) par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = root(x); //xの根をrx
int ry = root(y); //yの根をry
if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
//cout<<""<<endl;
// ./a.out
// g++ 201110.cpp
// ll N;cin>>N;
int main(){
ll a,b,c,d ;
cin >>a>>b>>c>>d ;
ll x=max((a-c),(a-d));
ll y=max((b-c),(b-d)) ;
cout<<max(x,y) <<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int main(){
IOS;
int a,b,c,d;
cin>>a>>b>>c>>d;
int ans=b-c;
cout<<ans;
}
|
#include <bits/stdc++.h>
const int N = 200000;
std::vector<int>G[N + 5];
void adde(int u, int v) {
G[u].push_back(v), G[v].push_back(u);
}
int n, k, d, t;
int a[N + 5], b[N + 5];
void dfs1(int x, int f) {
a[x] = b[x] = -1;
int p = -1, q = 0;
for(auto to : G[x]) if( to != f ) {
dfs1(to, x);
p = std::max(p, a[to] - 1);
q = std::max(q, b[to] + 1);
}
if( p >= q ) q = -1;
if( q == d ) p = d, q = -1, t++;
a[x] = p, b[x] = q;
}
bool check(int _d) {
d = _d, t = 0, dfs1(1, 0);
if( b[1] != -1 ) t++;
return t <= k;
}
int main() {
scanf("%d%d", &n, &k);
for(int i=1,u,v;i<n;i++) scanf("%d%d", &u, &v), adde(u, v);
int l = 1, r = n - 1;
while( l < r ) {
int m = (l + r) >> 1;
if( check(m) ) r = m;
else l = m + 1;
}
printf("%d\n", l);
} | #include<bits/stdc++.h>
#define fgx cerr<<"-----------------------"<<endl
#define LL long long
#define DB double
#define pb push_back
using namespace std;
inline int read(){
int nm=0,fh=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') fh=-1;
for(;isdigit(c);c=getchar()) nm=nm*10+c-'0';
return nm*fh;
}
#define M 200020
#define pii pair<int,int>
#define mp make_pair
#define ds second
int n,K,ans,tot,to[M<<1],nt[M<<1],fs[M],dep[M],fa[M][21],lim;
bool vis[M]; priority_queue<pii >que;
inline void link(int u,int v){to[++tot]=v,nt[tot]=fs[u],fs[u]=tot;}
inline int Jump(int u,int k){for(int i=20;~i;i--) if(k&(1<<i)) u=fa[u][i]; return u;}
inline void dfs(int u,int last=0){
dep[u]=dep[last]+1,fa[u][0]=last;
for(int i=1;i<=20;i++) fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=fs[u];i;i=nt[i]) if(to[i]^last) dfs(to[i],u);
}
inline void dfst(int u,int dep,int last=0){
if(dep>lim) return; vis[u]=true;
for(int i=fs[u];i;i=nt[i]) if(to[i]^last) dfst(to[i],dep+1,u);
}
inline bool Jud(int td){
lim=td;
for(int i=1;i<=n;i++) que.push(mp(dep[i],i)),vis[i]=false;
int ret=0;
while(!que.empty()){
if(ret>=K) return 0;
int u=que.top().ds,pos; que.pop();
if(vis[u]) continue;
if(dep[u]<=td) pos=1; else pos=Jump(u,td);
dfst(pos,0),ret++;
while(!que.empty()&&vis[que.top().ds]) que.pop();
} return ret<=K;
}
int main(){
n=read(),ans=n; K=read();
for(int i=1;i<n;i++){int u=read(),v=read(); link(u,v),link(v,u);}
dfs(1);
for(int k=20;~k;k--){int td=ans-(1<<k); if(td>=0&&Jud(td)) ans=td;}
printf("%d\n",ans);
return 0;
} |
/*
P_r_A_d_Y ☹
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define mod 1000000007
#define inf 1e18
#define pb push_back
#define all(x) x.begin(),x.end()
#define arr_max(x) *max_element(x.begin(),x.end())
#define arr_min(x) *min_element(x.begin(),x.end())
#define pii pair<int,int>
#define ff first
#define ss second
#define vec vector<int>
const int N = 200005;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
bool cmp(vector<int>a , vector<int>b) {
return a[0] < b[0];
}
int poww(int x, int n)
{
if (n == 0 ) return 1 ;
int u = poww(x, n / 2);
u = (u * u) ;
if (n % 2 == 1)u = (u * x) ;
return u;
}
//(x**n)%m
int mod_pow(int x, int n, int m)
{
if (n == 0 ) return 1 % m;
int u = mod_pow(x, n / 2, m);
u = (u * u) % m;
if (n % 2 == 1)u = (u * x) % m;
return u;
}
int query(int l, int r)
{
int x;
cout << "? " << l << " " << r << endl;
cout.flush();
cin >> x;
cout.flush();
return x;
}
int xd[4] = {1, 0, -1, 0};
int yd[4] = {0, -1, 0, 1};
int ceil(int a, int d)
{
return (a % d ? a / d + 1 : a / d);
}
int32_t main()
{
fastIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test;
test = 1;
//cin >> test;
int ert = 1;
while (test--)
{
int n; cin >> n;
int d = 0;
int dup = n;
while (dup > 0)
{
d++;
dup /= 10;
}
int ans = 0;
for (int i = 4; i < d; i++)
{
int g = (i - 1) / 3;
ans += g * (9 * poww(10, i - 1));
}
int f = (d - 1) / 3;
ans += f * (n - poww(10, d - 1) + 1);
cout << ans << endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll mod=1000000007;
int main(){
ll N,ans=0,K=1000;
cin >> N;
while(N-K+1>=0){
ans+=N-K+1;
K*=1000;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<62) - 1;
int main(){
int n;
cin >> n;
ll res1 = 0;
ll res2 = 0;
ll res3 = 0;
for (int i = 0; i < n; ++i) {
ll x;
cin >> x;
res1 += abs(x);
res2 += x * x;
chmax(res3, abs(x));
}
cout << res1 << endl;
cout << fixed << setprecision(10) << sqrt(res2) << endl;
cout << res3 << endl;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;
int main() {
int n;
cin >> n;
vector<tuple<int, int, int>> lr;
int ans = 0;
rep(i, n) {
int t, l, r;
cin >> t >> l >> r;
for(auto [pt, pl, pr] : lr) {
if(pr < l || r < pl) continue;
if(pr == l) {
if((pt == 1 || pt == 3) && (t == 1 || t == 2)) {
} else {
continue;
}
}
if(r == pl) {
if((pt == 1 || pt == 2) && (t == 1 || t == 3)) {
} else {
continue;
}
}
ans++;
}
lr.push_back({t, l, r});
}
cout << ans << endl;
return 0;
} |
#include <iostream>
using namespace std;
int main() {
long long a, b, c, d;
//long long n, x, y;
long long ans = 0;
double n = 0;
cin >> a >> b >> c >> d;
if (b >= d*c) {
ans = -1;
} else {
// n = 0;
// x = 0;
// y = 0;
// while (a + x > d*y) {
// x += b;
// y += c;
// n++;
// }
n = (double)a/(double)(c*d - b);
ans = (long long)n;
if (n - ans != 0) {
ans++;
}
}
cout << ans << '\n';
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define MP make_pair
#define PB push_back
#define REP(i, L, R) for (int i = L; i < (R); ++i)
#define SZ(x) (int)x.size()
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template <typename T>
bool ckmin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
}
template <typename T>
bool ckmax(T &a, const T &b) {
return a < b ? a = b, 1 : 0;
}
// O(log max(a,b))
template <typename T = int>
T gcd(T a, T b) {
if (b == 0) {
return a;
}
return gcd<T>(b, a % b);
}
// Finds gcd(a,b) and x, y s.t. ax + by = gcd(a,b).
template <typename T = int>
T ext_gcd(T a, T b, T &x, T &y) {
T d = a;
if (b != 0) {
d = ext_gcd<T>(b, a % b, y, x);
y -= (a / b) * x;
} else {
// b = 0 then gcd(a,b) = a = a*1 + b*0
x = 1;
y = 0;
}
return d;
}
// Finds x s.t. ax = 1 (mod m) given (a,m)=1.
// (a,m)=1=ax+my.
template <typename T = int>
T mod_inverse(T a, T m) {
T x, y;
ext_gcd<T>(a, m, x, y);
return (m + x % m) % m;
}
// Computes C(n,k) % m.
template <typename T = int>
T comb(T n, T k, T m) {
if (n < k || k < 0 || n < 0) return 0;
T ret = 1;
for (T i = 0; i < k; ++i) {
ret = ret * (n - i) % m;
ret = ret * mod_inverse<T>(i + 1, m) % m;
}
return ret;
}
// Solves for x in ax = b (mod m). The solution when exists is of the form x
// (mod n). Returns false if no solution exists, in which case x and n are
// undefined.
template <typename T = int>
bool solve_lineq(T a, T b, T m, T &x, T &n) {
T d = gcd<T>(a, m);
if (b % d != 0) {
return false;
}
// ax - b = tm -> a/d x - b/d = t m/d -> a' x = b' (mod m') with (a',m') = 1.
// The solution is x = inverse(a') b' (mod m').
a /= d;
b /= d;
m /= d;
x = mod_inverse(a, m) * b % m;
n = m;
return true;
}
// Solves a linear system: A_i x = B_i (mod M_i) for i=1,...n (n >= 1).
// Returns a pair (b, m) means x=b(mod m) is the solution. The pair (0, -1)
// means no solution existed.
template <typename T = int>
pair<T, T> solve_linsys(const vector<T> &A, const vector<T> &B,
const vector<T> &M) {
// Consider the first equation x = 0 (mod 1).
T x = 0, m = 1;
for (int i = 0; i < A.size(); ++i) {
// x = b (mod m) -> x = b+tm
// A[i] x = B[i] (mod M[i]) -> (A[i]*m) * t = (B[i]-A[i]*b) (mod M[i])
T a = A[i] * m;
T b = B[i] - A[i] * x;
T d = gcd(M[i], a);
if (b % d != 0) {
// no solution
return make_pair(0, -1);
}
T t = b / d * mod_inverse(a / d, M[i] / d) % (M[i] / d);
x = x + m * t;
m *= M[i] / d;
}
return make_pair((x + m) % m, m);
}
void solve() {
int t;
cin >> t;
REP(ti, 0, t) {
ll X, Y, P, Q;
cin >> X >> Y >> P >> Q;
ll ans = -1;
REP(q, 0, Q) {
REP(y, 0, Y) {
// solve: x = P+q (mod P+Q) and x = X+y (mod 2X+2Y)
pl sol = solve_linsys<ll>(vector<ll>{1, 1}, vector<ll>{P + q, X + y},
vector<ll>{P + Q, 2 * X + 2 * Y});
if (sol.second != -1) {
if (ans == -1) {
ans = sol.first;
} else {
ans = min(ans, sol.first);
}
}
}
}
if (ans == -1) {
cout << "infinity";
} else {
cout << ans;
}
cout << endl;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
while (T--) {
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf=1000000000000000000,mod=1000000007;
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 h,w,n,m;cin>>h>>w>>n>>m;
ll vis[h][w],dp[h][w],ans=0,a,b;
for(ll i=0;i<h;i++)for(ll j=0;j<w;j++)vis[i][j]=0;
for(ll i=0;i<n;i++)cin>>a>>b,vis[a-1][b-1]=1;
for(ll i=0;i<m;i++)cin>>a>>b,vis[a-1][b-1]=2;
ll flg;
// for(ll i=0;i<h;i++)
for(ll i=0;i<h;i++){
flg=1;
for(ll j=0;j<w;j++){
dp[i][j]=0;
if(vis[i][j]==1)flg=0;
else if(vis[i][j]==2)flg=1;
if(!flg)
dp[i][j]=1;
}
}
for(ll i=0;i<h;i++){
flg=1;
for(ll j=w-1;j>=0;j--){
if(vis[i][j]==1)flg=0;
else if(vis[i][j]==2)flg=1;
if(!flg)
dp[i][j]=1;
}
}
for(ll j=0;j<w;j++){
flg=1;
for(ll i=0;i<h;i++){
if(vis[i][j]==1)flg=0;
else if(vis[i][j]==2)flg=1;
if(!flg)
dp[i][j]=1;
}
}
for(ll j=0;j<w;j++){
flg=1;
for(ll i=h-1;i>=0;i--){
if(vis[i][j]==1)flg=0;
else if(vis[i][j]==2)flg=1;
if(!flg)
dp[i][j]=1;
}
}
for(ll i=0;i<h;i++)for(ll j=0;j<w;j++)if(dp[i][j])ans++;
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
//---------------- 定数 ----------------
// [X_MIN, X_MAX], [Y_MIN, Y_MAX]
#define X_MIN 0
#define X_MAX 29
#define Y_MIN 0
#define Y_MAX 29
#define QUERY 1000
// -------- util function --------
int rand_int(){
static unsigned long long x=123456789, y=362436069, z=521288629, w=88675123;
unsigned long long t = (x^(x << 11));
x=y; y=z; z=w;
return (w=(w^(w>>19))^(t^(t>>8)));
}
// -------------------------------
// 座標(y, x)を扱うクラス
struct Point{
int x, y;
//Point(int y, int x): x(x), y(y);
};
struct Query{
Point start, end;
string ans;
// クエリを開始する処理
void init(){
cin >> start.y >> start.x >> end.y >> end.x;
}
// クエリの答えを出力する処理
void submit(){
int point;
cout << ans << endl;
cin >> point;
return;
}
void solve(){
Point now = start;
while(now.y != end.y || now.x != end.x){
int flag = -1;
if(now.y == end.y) flag = 1;
if(now.x == end.x) flag = 0;
if(flag == -1) flag = rand_int() % 2;
if(flag == 0){
if(now.y < end.y){
ans.push_back('D');
now.y++;
}else{
ans.push_back('U');
now.y--;
}
}else{
if(now.x < end.x){
ans.push_back('R');
now.x++;
}else{
ans.push_back('L');
now.x--;
}
}
}
}
};
int main(){
for(int q = 0;q < QUERY;q++){
Query query;
query.init();
query.solve();
query.submit();
}
return 0;
}
|
#include <bits/stdc++.h>
namespace IO {
#define iL (1 << 20)
char ibuf[iL], *iS = ibuf + iL, *iT = ibuf + iL;
#define gc() ((iS == iT) ? (iT = (iS = ibuf) + fread(ibuf, 1, iL, stdin), iS == iT ? EOF : *iS ++) : *iS ++)
template<class T> inline void read(T &x) {
x = 0;int f = 0;char ch = gc();
for (; !isdigit(ch); f |= ch == '-', ch = gc());
for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc());
x = (f == 1 ? ~ x + 1 : x);
}
char Out[iL], *iter = Out;
#define flush() fwrite(Out, 1, iter - Out, stdout), iter = Out
template<class T> inline void write(T x, char ch = '\n') {
T l, c[35];
if (x < 0) *iter ++ = '-', x = ~ x + 1;
for (l = 0; !l || x; c[l] = x % 10, l++, x /= 10);
for (; l; -- l, *iter ++ = c[l] + '0');*iter ++ = ch;
flush();
}
}
using namespace IO;
#define N 200005
#define ll long long
#define MOD 998244353
#define DEBUG
using namespace std;
int n, fa[N];
int Find(int x) {
return (fa[x] == x) ? x : fa[x] = Find(fa[x]);
}
int Qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % MOD;
a = 1ll * a * a % MOD, b >>= 1;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
read(n);
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1, f; i <= n; i++) {
read(f);
int a = Find(i), b = Find(f);
if (a != b) fa[a] = b;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (fa[i] == i) ans = (ans + 1) % MOD;
}
// write(((ans - 1) * ans % MOD + 1) % MOD);
write((Qpow(2, ans) % MOD - 1) % MOD);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
void check(long long &a, vector<int> &v, vector<int> &c, int i, int &lc){
if(c.at(i) == lc){
a++;
return;
}
if(c.at(i) != -1){
return;
}
c.at(i) = lc;
check(a, v, c, v.at(i), lc);
}
int main(){
int N;
long long a = 0;
cin >> N;
vector<int> v(N);
rep(i, N){
cin >> v.at(i);
v.at(i)--;
}
vector<int> c(N, -1);
for(int i = 0; i < N; i++){
if(c.at(i) == -1){
check(a, v, c, i, i);
}
}
long long ans = 1;
rep(i, a){
ans *= 2;
ans %= 998244353;
}
cout << ans - 1 << endl;
} |
#include <bits/stdc++.h>
using namespace std;
// clang-format off
#define forn(i, x, y) for (int i = x; i < y; i++)
#define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(NULL)
#define all(v) v.begin(), v.end()
// clang-format on
using VI = vector<int>;
template <int n>
class UF
{
public:
vector<int> par, rank, cnt;
UF()
{
par = rank = vector<int>(n, 0);
cnt = vector<int>(n, 1);
forn(i, 0, n) par[i] = i;
}
void reset()
{
forn(i, 0, n) rank[i] = 0, cnt[i] = 1, par[i] = i;
}
int operator[](int x) { return (par[x] == x) ? (x) : (par[x] = operator[](par[x])); }
int count(int x) { return cnt[operator[](x)]; }
int operator()(int x, int y)
{
if ((x = operator[](x)) == (y = operator[](y)))
return x;
cnt[y] = cnt[x] = cnt[x] + cnt[y];
if (rank[x] > rank[y])
return par[x] = y;
rank[x] += rank[x] == rank[y];
return par[y] = x;
}
};
int N;
int X[100], Y[100];
struct edge
{
int u, v;
double w;
};
int main()
{
IOS;
cin >> N;
forn(i, 0, N) cin >> X[i] >> Y[i];
vector<edge> edges;
forn(i, 0, N)
{
edges.push_back({i, 100, 100.0 - Y[i]});
edges.push_back({i, 101, Y[i] + 100.0});
forn(j, 0, N)
edges.push_back({i, j, hypot(X[i] - X[j], Y[i] - Y[j])});
}
sort(all(edges), [](edge a, edge b) {
return a.w < b.w;
});
UF<102> uf;
int n = edges.size();
double res = 0.0;
forn(i, 0, n)
{
edge &e = edges[i];
uf(e.u, e.v);
if (uf[100] == uf[101])
{
res = e.w / 2;
break;
}
}
cout << fixed << setprecision(10) << res << endl;
return 0;
}
| #pragma GCC optimize("-Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
// #include <atcoder/all>
// #include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
// using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<p64, p64> pp64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef pair<ll, p64> tp;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln '\n'
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define zero ll(0)
#define set_bits(x) __builtin_popcountll(x)
// #define mint modint998244353
ll mpow(ll a, ll b)
{
if (a == 0)
return 0;
if (b == 0)
return 1;
ll t1 = mpow(a, b / 2);
t1 *= t1;
t1 %= MOD;
if (b % 2)
t1 *= a;
t1 %= MOD;
return t1;
}
ll mpow(ll a, ll b, ll p)
{
if (a == 0)
return 0;
if (b == 0)
return 1;
ll t1 = mpow(a, b / 2, p);
t1 *= t1;
t1 %= p;
if (b % 2)
t1 *= a;
t1 %= p;
return t1;
}
ll modinverse(ll a, ll m)
{
ll m0 = m;
ll y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
{
ll q = a / m;
ll t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
ll range(ll l, ll r)
{
return l + mt() % (r - l + 1);
}
ll rev(ll v)
{
return mpow(v, MOD - 2);
}
ll nc2(ll n)
{
return (n * (n - 1)) / 2;
}
ll ceil(ll p, ll q)
{
return (p + q - 1) / q;
}
void solve()
{
int k, n, m;
cin >> k >> n >> m;
v64 arr(k + 1);
v64 ans(k + 1, 0);
for (int i = 1; i <= k; i++)
{
cin >> arr[i];
ans[i] = arr[i] * m / n;
}
m = m % n;
// v64 f(k + 1);
// v64 c(k + 1);
vp64 temp(k + 1);
ll sum = 0;
for (int i = 1; i <= k; i++)
{
// f[i] = arr[i] * m / n;
// c[i] = f[i] + 1;
temp[i] = {(arr[i] * m) % n,
i};
sum += temp[i].fi;
}
int cnt = sum / n;
sort(temp.begin() + 1, temp.end(), greater<p64>());
for (int i = 1; i <= cnt; i++)
{
ans[temp[i].se]++;
}
for (int i = 1; i <= k; i++)
{
cout << ans[i] << " ";
}
}
int main()
{
fast_cin();
ll t = 1;
// cin >> t;
// mint a = 3;
// cout << (1 - a).val() << endl;
forn(i, t)
{
// cout << "Case #" << i+1 << ": ";
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
template <class T>
class Matrix {
public:
int h, w;
V<V<T>> data;
Matrix(int h_, int w_ = 0, T v = 0) {
h = h_;
w = (w != 0) ? w_ : h;
data.resize(h);
rep(i, h) data[i].resize(w, v);
}
Matrix(const V<V<T>>& data_)
: h(data_.size()), w(data_[0].size), data(data_) {}
Matrix(initializer_list<initializer_list<T>> list) {
data.resize(list.size());
int cnt = 0;
for (auto row : list) data[cnt++] = V<T>(row);
h = data.size(), w = data[0].size();
}
Matrix(const Matrix& a) : h(a.h), w(a.w), data(a.data) {}
Matrix& operator=(const Matrix& a) {
h = a.h, w = a.w, data = a.data;
return *this;
}
V<T>& operator[](int n) { return data[n]; }
Matrix& operator*=(Matrix& a) {
V<V<T>> res(w, V<T>(a.w));
rep(i, w) rep(j, a.w) {
rep(k, w) res[i][j] += data[i][k] * a[k][j];
}
swap(data, res);
h = w, w = a.w;
return *this;
}
Matrix operator*(Matrix& a) { return Matrix(*this) *= a; }
Matrix& operator+=(Matrix& a) {
rep(i, h) rep(j, w) data[i][j] += a[i][j];
return *this;
}
string str() {
ostringstream oss;
rep(i, h) {
rep(j, w) oss << data[i][j] << " ";
if (i != h - 1) oss << "\n";
}
return oss.str();
}
};
int main() {
int n, m;
cin >> n;
V<P> xy(n);
rep(i, n) cin >> xy[i].first >> xy[i].second;
V<Matrix<ll>> ops;
Matrix<ll> now = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
ops.push_back(now);
Matrix<ll> clk = {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}};
Matrix<ll> clw = {{0, -1, 0}, {1, 0, 0}, {0, 0, 1}};
cin >> m;
rep(i, m) {
int op;
ll p;
cin >> op;
if (op > 2) cin >> p;
if (op == 1) {
now = clk * now;
} else if (op == 2) {
now = clw * now;
} else if (op == 3) {
Matrix<ll> f = {{-1, 0, 2 * p}, {0, 1, 0}, {0, 0, 1}};
now = f * now;
} else if (op == 4) {
Matrix<ll> f = {{1, 0, 0}, {0, -1, 2 * p}, {0, 0, 1}};
now = f * now;
}
ops.push_back(now);
}
int Q;
cin >> Q;
rep(i, Q) {
int a, b;
cin >> a >> b;
b--;
Matrix<ll> pos = {{xy[b].first}, {xy[b].second}, {1}};
auto ans = ops[a] * pos;
ll x = ans[0][0], y = ans[1][0];
printf("%ld %ld\n", x, y);
}
}
| #include<bits/stdc++.h>
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define fast ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pop pop_back
#define mii map<int,int>
#define in(a,n) for(int i=0;i<n;i++)cin>>a[i];
#define out(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define test int t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define endl "\n"
#define MOD 1000000007
using namespace std;
int power(int x, int y)
{
float temp;
if(y == 0) return 1;
temp = power(x, y / 2);
if (y % 2 == 0) return temp * temp;
else
{
if(y > 0) return x * temp * temp;
else return (temp * temp) / x;
}
}
signed main()
{
fast
int x,y,a,b;
cin>>x>>y>>a>>b;
if(a>=b || x>=b) cout<<(y-x-1)/b<<endl;
else
{
int s=log((b)/x)/(log(a));
int c=x*power(a,s);
// cout<<s<<" "<<c<<endl;
if(c>=y)
{
int r=(log(y/x)/log(a));
if(x*power(a,r)==y) cout<<r-1<<endl;
}
else if(c+((y-c)/b)*b==y) cout<<s+(y-c)/b-1<<endl;
else cout<<s+(y-c)/b<<endl;
}
}
|
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int N, M;
cin>>N>>M;
long long MOD = 998244353;
vector<vector<long long>> C(N+1, vector<long long>(N+1));
for (int i=0; i<=N; i++)
{
C[i][0] = 1;
for (int j=1; j<i; j++)
C[i][j] = (C[i-1][j-1]+C[i-1][j])%MOD;
C[i][i] = 1;
}
//for (int i=0; i<=N; i++)
//{
// for (int j=0; j<=i; j++)
// cout<<" "<<C[i][j];
// cout<<endl;
//}
vector<long long> T(N+1);
T[0] = 1;
for (int i=0; i<16; i++)
{
vector<long long> P(N+1);
P.swap(T);
for (int j=0; j<=N; j++)
for (int k=0; k<=N; k+=2)
if (j%2==(M>>i&1))
(T[(j>>1)+(k>>1)] += C[N][k]*P[j]%MOD) %= MOD;
}
cout<<T[0]<<endl;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using LL = long long;
using P = pair<int,int>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;
long long const mod = 1000000007;
struct mint{
long long val;
mint(long long val = 0): val(val % mod) {}
mint& operator += (const mint n){
val += n.val;
if(val >= mod) val -= mod;
return *this;
}
mint& operator -= (const mint n){
val -= n.val;
if(val < 0) val += mod;
return *this;
}
mint& operator *= (const mint n){
val *= n.val;
val %= mod;
if(val < 0) val += mod;
return *this;
}
mint operator + (const mint n) const{
mint res(*this);
return res += n;
}
mint operator - (const mint n) const{
mint res(*this);
return res -= n;
}
mint operator * (const mint n) const{
mint res(*this);
return res *= n;
}
mint pow(long long n) const{
if(n == 0) return 1;
mint m = pow(n >> 1);
m *= m;
if(n & 1) m *= *this;
return m;
}
// mint division for prime mod
mint inv() const{
return pow(mod - 2);
}
mint& operator /= (const mint n){
return (*this) *= n.inv();
}
mint operator / (const mint n) const{
mint res(*this);
return res /= n;
}
};
template<typename T>
struct matrix{
int row, column;
vector<vector<T>> mat;
matrix(int r = 0, int c = 0, T val = 0) : row(r), column(c) {
mat = vector<vector<T>>(r, vector<T>(c, val));
}
vector<T>& operator [] (const int num) {
return mat[num];
}
matrix& operator += (const matrix A) {
assert(row == A.row && column == A.column);
for(int i = 0; i < row; i++) {
for(int j = 0; j < column; j++) {
mat[i][j] += A.mat[i][j];
}
}
return *this;
}
matrix& operator -= (const matrix A) {
assert(row == A.row && column == A.column);
for(int i = 0; i < row; i++) {
for(int j = 0; j < column; j++) {
mat[i][j] -= A.mat[i][j];
}
}
return *this;
}
matrix& operator *= (const matrix A) {
assert(column == A.row);
matrix B(row, A.column);
for(int i = 0; i < row; i++) {
for(int k = 0; k < column; k++) {
for(int j = 0; j < A.column; j++) {
B.mat[i][j] += mat[i][k] * A.mat[k][j];
}
}
}
return *this = B;
}
matrix& operator *= (long long k) {
for(int i = 0; i < row; i++) {
for(int j = 0; j < column; j++) {
mat[i][j] *= k;
}
}
return *this;
}
matrix operator + (const matrix A) const {
assert(row == A.row && column == A.column);
matrix B = *this;
return B += A;
}
matrix operator - (const matrix A) const {
assert(row == A.row && column == A.column);
matrix B = *this;
return B -= A;
}
matrix operator * (const matrix A) const {
assert(column == A.row);
matrix B = *this;
return B *= A;
}
matrix operator * (long long k) const {
matrix B = *this;
return B *= k;
}
vector<T> operator * (const vector<int> A) const {
int vector_size = A.size();
assert(column == vector_size);
vector<T> B(row);
for(int i = 0; i < row; i++) {
for(int j = 0; j < column; j++) {
B[i] += mat[i][j] * A[j];
}
}
return B;
}
matrix pow(long long n) const {
assert(row == column);
if(n == 0){
matrix E(row, column);
for(int i = 0; i < row; i++) {
E.mat[i][i] = 1;
}
return E;
}
matrix A = pow(n >> 1);
A *= A;
if(n & 1) A *= *this;
return A;
}
};
int main(){
int N, M, K;
cin >> N >> M >> K;
vector<int> A(N);
rep(i,N) cin >> A[i];
vv G(N);
rep(i,M){
int x, y;
cin >> x >> y;
x--; y--;
G[x].emplace_back(y);
G[y].emplace_back(x);
}
matrix<mint> mat(N, N), F(N, N);
rep(i,N){
int s = G[i].size();
mint coef = M * 2 - s, tot = M * 2;
coef *= tot.inv();
mat[i][i] = coef;
for(auto j : G[i]) mat[i][j] = tot.inv();
}
F = mat.pow(K);
vector<mint> ans = F * A;
rep(i,N) cout << ans[i].val << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9+7;
template<uint_fast64_t Modulus = MOD>
struct Modint {
using u64 = uint_fast64_t;
u64 a;
constexpr Modint(const u64 x = 0) noexcept : a(x % Modulus) {}
constexpr Modint operator+(const Modint rhs) const noexcept {
return Modint(*this) += rhs;
}
constexpr Modint operator-(const Modint rhs) const noexcept {
return Modint(*this) -= rhs;
}
constexpr Modint operator*(const Modint rhs) const noexcept {
return Modint(*this) *= rhs;
}
constexpr Modint operator/(const Modint rhs) const noexcept {
return Modint(*this) /= rhs;
}
constexpr Modint &operator+=(const Modint rhs) noexcept {
a += rhs.a;
if (a >= Modulus) a -= Modulus;
return *this;
}
constexpr Modint &operator-=(const Modint rhs) noexcept {
if (a < rhs.a) a += Modulus;
a -= rhs.a;
return *this;
}
constexpr Modint &operator*=(const Modint rhs) noexcept {
a = a * rhs.a % Modulus;
return *this;
}
constexpr Modint &operator/=(Modint rhs) noexcept {
u64 exp = Modulus - 2;
while (exp) {
if (exp & 1) *this *= rhs;
rhs *= rhs;
exp >>= 1;
}
return *this;
}
Modint pow(u64 t) const {
if (!t) return 1;
Modint x = pow(t>>1);
x *= x;
if (t&1) x *= *this;
return x;
}
explicit operator bool() const {
return a;
}
friend ostream &operator<<(ostream &os, const Modint &m) {
return os << m.a;
}
};
using mint = Modint<>;
void solve() {
int h, w; cin >> h >> w;
vector<string> g(h);
for(auto &s : g) cin >> s;
vector<vector<bool>> b(h, vector<bool>(w, false));
int sum = 0;
for(int i=0; i<h; ++i) {
for(int j=0; j<w; ++j) {
if(g[i][j] == '.') {
++sum;
} else {
b[i][j] = true;
}
}
}
vector<vector<int>> cnt(h, vector<int>(w, -1));
for(int i=0; i<h; ++i) {
int prev = 0;
for(int j=0; j<=w; ++j) {
if(j == w || b[i][j]) {
for(int k=prev; k<j; ++k) {
cnt[i][k] += j - prev;
}
prev = j+1;
}
}
}
for(int j=0; j<w; ++j) {
int prev = 0;
for(int i=0; i<=h; ++i) {
if(i == h || b[i][j]) {
for(int k=prev; k<i; ++k) {
cnt[k][j] += i - prev;
}
prev = i+1;
}
}
}
vector<mint> two(sum+1);
two[0] = 1;
for(int i=0; i<sum; ++i) {
two[i+1] = two[i] * 2;
}
mint ans = two[sum] * sum;
for(int i=0; i<h; ++i) {
for(int j=0; j<w; ++j) {
if(cnt[i][j] > 0) {
ans -= two[sum - cnt[i][j]];
}
}
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
solve();
return 0;
}
| #include<bits/stdc++.h>
typedef int64_t i64;
typedef long double f128;
using namespace std;
template<typename T>
void scan(T& n)
{
cin>>n;
return;
}
void scan()
{
return;
}
template<typename T,class... Args>
void scan(T& n,Args&... args)
{
scan(n);
scan(args...);
return;
}
template<typename T>
void scan_array(T start,T end)
{
T now=start;
for(;now!=end;++now)
{
scan(*now);
}
return;
}
template<typename T>
void print(T n)
{
cout<<n;
return;
}
template<typename T>
void println(T n)
{
print(n);
print('\n');
return;
}
template<typename T,class... Args>
void println(T n,Args... args)
{
print(n);
print(' ');
println(args...);
return;
}
template<typename T>
void print_array(T start,T end)
{
T now=start;
print(*now);
++now;
for(;now!=end;++now)
{
print(' ');
print(*now);
}
print('\n');
return;
}
i64 pow_mod(i64 a,i64 n,i64 mod)
{
i64 res=1,now=a;
while(n)
{
if(n&1)
{
res=res*now%mod;
}
now=now*now%mod;
n>>=1;
}
return res;
}
i64 inv_mod(i64 a,i64 mod)
{
return pow_mod(a,mod-2,mod);
}
int main()
{
i64 constexpr mod=1000000007;
i64 H,W;
scan(H,W);
vector<string> S(H);
scan_array(S.begin(),S.end());
i64 K=0;
for(i64 i=0;i<H;++i)
{
for(i64 j=0;j<W;++j)
{
if(S[i][j]=='.')
{
K+=1;
}
}
}
vector<vector<i64>> sum1(H,vector<i64> (W)),sum2(H,vector<i64> (W));
for(int i=0;i<H;++i)
{
int start=-1,len=0;
for(int j=0;j<W;++j)
{
if(S[i][j]=='#')
{
if(start==-1)
{
continue;
}
for(int k=start;k<j;++k)
{
sum1[i][k]=len;
}
start=-1;
len=0;
continue;
}
if(start==-1)
{
start=j;
}
len+=1;
}
if(start==-1)
{
continue;
}
for(int k=start;k<W;++k)
{
sum1[i][k]=len;
}
}
for(int j=0;j<W;++j)
{
int start=-1,len=0;
for(int i=0;i<H;++i)
{
if(S[i][j]=='#')
{
if(start==-1)
{
continue;
}
for(int k=start;k<i;++k)
{
sum2[k][j]=len;
}
start=-1;
len=0;
continue;
}
if(start==-1)
{
start=i;
}
len+=1;
}
if(start==-1)
{
continue;
}
for(int k=start;k<H;++k)
{
sum2[k][j]=len;
}
}
vector<vector<i64>> vec(H,vector<i64> (W));
for(int i=0;i<H;++i)
{
for(int j=0;j<W;++j)
{
vec[i][j]=K-(sum1[i][j]+sum2[i][j]-1);
}
}
i64 sum=0;
for(int i=0;i<H;++i)
{
for(int j=0;j<W;++j)
{
if(vec[i][j]==K+1)
{
continue;
}
sum+=pow_mod(2,vec[i][j],mod);
}
}
sum%=mod;
i64 ans=pow_mod(2,K,mod)*K%mod-sum;
ans=(ans%mod+mod)%mod;
println(ans);
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll,ll> pii;
typedef vector<pii> vpii;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define FOR0(i,n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define all(c) (c).begin(),(c).end()
#define fast ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int main(){
fast
ll h,w,n,m;
cin>>h>>w>>n>>m;
vi br[w+1],bc[h+1],dr[w+1],dc[h+1];
map<pii,ll> mp;
FOR0(i,n){
ll x,y;
cin>>x>>y;
mp[{x,y}]=1;
br[y].pb(x);
bc[x].pb(y);
}
FOR0(i,m){
ll x,y;
cin>>x>>y;
mp[{x,y}]=-1;
dr[y].pb(x);
dc[x].pb(y);
}
FOR0(i,h+1){
sort(all(bc[i]));
sort(all(dc[i]));
}
FOR0(i,w+1){
sort(all(br[i]));
sort(all(dr[i]));
}
ll ans=n;
for(ll i=1;i<=h;i++){
for(ll j=1;j<=w;j++){
if(mp[{i,j}] !=0){
continue;
}
auto it1=lower_bound(all(bc[i]),j);
auto it2=lower_bound(all(dc[i]),j);
ll d1=0,d2=w+1;
if(it2 != dc[i].end()){
d2 = *it2;
}
if(it2 != dc[i].begin()){
it2--;
d1 = *it2;
}
ll b1=-1,b2=-1;
if(it1 != bc[i].end()){
b2 = *it1;
}
if(it1 != bc[i].begin()){
it1--;
b1 = *it1;
}
if(b1>d1 && b1<d2){
ans++;
continue;
}
if(b2>d1 && b2<d2){
ans++;
continue;
}
// **************************************
it1=lower_bound(all(br[j]),i);
it2=lower_bound(all(dr[j]),i);
d1=0,d2=h+1;
if(it2 != dr[j].end()){
d2 = *it2;
}
if(it2 != dr[j].begin()){
it2--;
d1 = *it2;
}
b1=-1,b2=-1;
if(it1 != br[j].end()){
b2 = *it1;
}
if(it1 != br[j].begin()){
it1--;
b1 = *it1;
}
if(b1>d1 && b1<d2){
ans++;
continue;
}
if(b2>d1 && b2<d2){
ans++;
continue;
}
}
}
cout<<ans<<endl;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define vr vector
#define endl "\n"
#define all(x) x.begin(),x.end()
template<class T> void read(vr<T> &arr)
{
for (auto &x : arr)
cin >> x;
}
template<class T> void read(T &x)
{
cin >> x;
}
template<class T> void write(vr<T> &arr)
{
for (auto x : arr)
cout << x << " ";
cout << endl;
}
template<class T> void write(vr<vr<T>> &arr)
{
for (auto &x : arr)
{
for (auto &y : x)
cout << y << " ";
cout << endl;
}
}
template<class T> void write(T &x)
{
cout << x;
}
void solve() {
cout<<setprecision(9);
cout<<fixed;
int a,b,c,d;
cin>>a>>b>>c>>d;
b = -b;
if(a == c)
{
cout<<a;
return;
}
double slop = 1.0 * (d - b) / (c - a);
double newX = (slop * a - b) / slop;
cout<<newX;
}
int32_t main() {
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin>>t;
while (t--)
{
solve();
}
return 0;
}
|
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
long long n;
cin>>n;
int count=0;
while(n%2==0)
{
n/=2;
count++;
}
if(count==0)
{
cout<<"Odd"<<endl;
}
else if(count==1)
{
cout<<"Same"<<endl;
}
else
{
cout<<"Even"<<endl;
}
}
} | #include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
void _main(){
ll T;
cin >> T;
ll A[T];
rep(i, 0, T)cin >> A[i];
rep(i, 0, T){
if(A[i]%4==0) cout << "Even" << endl;
else if(A[i]%2==0) cout << "Same" << endl;
else cout << "Odd" << endl;
}
return;
} |
// C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
int main(){
// 1. 入力情報.
int N;
scanf("%d", &N);
set<string> st;
rep(i, N){
char c[22];
scanf("%s", c);
string s(c);
st.insert(s);
}
// 2. 不満な文字列があるか?
string ans = "satisfiable";
for(auto &p : st){
if(p[0] != '!'){
string q = '!' + p;
if(st.count(q)){
ans = p;
break;
}
}
}
// 3. 出力.
printf("%s\n", ans.c_str());
return 0;
} | #include<iostream>
#include<map>
using namespace std;
const int MAXN=2e5+10;
int n;
string a[MAXN];
map<string,int> mp;
int main() {
cin>>n;
for(int i=1;i<=n;i++) {
cin>>a[i];
if(a[i][0]=='!') {
a[i]=a[i].substr(1,a[i].length()-1);
if(mp[a[i]]==0) mp[a[i]]=2;
else if(mp[a[i]]==1) {
cout<<a[i]<<endl;
return 0;
}
}
else {
if(mp[a[i]]==0) mp[a[i]]=1;
else if(mp[a[i]]==2) {
cout<<a[i]<<endl;
return 0;
}
}
}
cout<<"satisfiable\n";
return 0;
} |
#ifdef LOCAL
//#define _GLIBCXX_DEBUG
#endif
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<char> Vc;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
typedef vector<vector<char>> VVc;
typedef vector<vector<vector<ll>>> VVV;
typedef vector<vector<vector<vector<ll>>>> VVVV;
#define endl '\n'
#define REP(i, a, b) for(ll i=(a); i<(b); i++)
#define PER(i, a, b) for(ll i=(a); i>=(b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF=1e18+18;
const ll MOD=1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a,b)
#define Each(a,b) for(auto &a :b)
#define rEach(i, mp) for (auto i = mp.rbegin(); i != mp.rend(); ++i)
#ifdef LOCAL
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) cerr << #mp << ":"<<endl; for (auto i = mp.begin(); i != mp.end(); ++i) { cerr << i->first <<":"<<i->second << endl;}
#define dbgset(st) cerr << #st << ":"<<endl; for (auto i = st.begin(); i != st.end(); ++i) { cerr << *i <<" ";}cerr<<endl;
#define dbgarr(n,m,arr) rep(i,n){rep(j,m){cerr<<arr[i][j]<<" ";}cerr<<endl;}
#define dbgdp(n,arr) rep(i,n){cerr<<arr[i]<<" ";}cerr<<endl;
#else
#define dbg(...)
#define dbgmap(...)
#define dbgset(...)
#define dbgarr(...)
#define dbgdp(...)
#endif
#define out(a) cout<<a<<endl
#define out2(a,b) cout<<a<<" "<<b<<endl
#define vout(v) rep(i,v.size()){cout<<v[i]<<" ";}cout<<endl
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s<<"("<<p.first<<", "<<p.second<<")"; }
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,v.size())i>>v[j];return i;}
// vector
template<typename T>
ostream &operator<<(ostream &s, const vector<T> &v) {
int len=v.size();
for(int i=0; i<len; ++i) {
s<<v[i];
if(i<len-1) s<<" ";
}
return s;
}
// 2 dimentional vector
template<typename T>
ostream &operator<<(ostream &s, const vector<vector<T> > &vv) {
int len=vv.size();
for(int i=0; i<len; ++i) {
s<<vv[i]<<endl;
}
return s;
}
int solve(){
ll n;
cin>>n;
Vec a(n);
Vec b(n);
cin>>a>>b;
Vec v(n);
ll ans = 0;
rep(i,n){
ans += a[i];
v[i] = b[i]-a[i];
}
dbg(ans);
dbg(v);
Vec x;
Vec y;
rep(i,n){
if(i%2==0){
x.pb(v[i]);
}else{
y.pb(v[i]);
}
}
sort(rALL(x));
sort(rALL(y));
dbg(x)
dbg(y)
ll now = ans;
rep(i,n/2){
now += x[i] + y[i];
chmax(ans,now);
}
out(ans);
return 0;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout<<std::setprecision(10);
// ll T;
// cin>>T;
// while(T--)
solve();
}
| #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
FAST/**/
ll n;
cin>>n;
ll sum = 0;
ll arr[n], arr1[n];
rep(n)
cin>>arr[i];
rep(n)
cin>>arr1[i];
rep(n)
sum+=arr[i];
set<pii,greater<pii>> s1,s2;
rep(n)
if(i%2 == 0)
s2.insert(mp(arr1[i]-arr[i],i));
else s1.insert(mp(arr1[i]-arr[i],i));
while(s1.size()>0 && (*s1.begin()).ff + (*s2.begin()).ff > 0)
{
auto curr = *s1.begin();
auto curr1 = *s2.begin();
s1.erase(s1.begin());
s2.erase(s2.begin());
sum+=(curr.ff+curr1.ff);
}
cout<<sum;
return 0;
}
|
#include <algorithm>
// #include <atcoder/all>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>
/*
cd $dir && g++ -std=c++17 -Wall -Wextra -O2 -DATCODERDEBUG
-I/mnt/d/MyProjects/atcoder/lib/ac-library $fileName && echo 'compilation ok!'
&& ./a.out
*/
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
#define dame(a) \
{ \
cout << a << endl; \
return 0; \
}
typedef long long ll;
class dstream : public ostream {};
template <typename T>
dstream &operator<<(dstream &os, T v) {
#ifdef ATCODERDEBUG
cout << "\033[1;31m" << v << "\033[0m";
#endif
return os;
}
ostream &endd(std::ostream &out) {
#ifdef ATCODERDEBUG
out << std::endl;
#endif
return out;
}
dstream dout;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[ ";
for (auto const &x : v) {
os << x;
if (&x != &v.back()) {
os << " : ";
}
}
os << " ]" << endl;
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
os << "[" << endl;
for (auto const &x : v) {
os << " ";
os << x;
}
os << "]" << endl;
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
os << "[ " << v.first << " " << v.second << " ]" << endl;
return os;
}
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
ll op(ll a, ll b) { return min(a, b); }
ll e() { return 1e17; }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, x, c;
cin >> N;
vector<vector<ll>> c2xs(N, vector<ll>());
REP(i, N) {
cin >> x;
cin >> c;
c2xs[c - 1].emplace_back(x);
}
REP(i, N) { sort(c2xs[i].begin(), c2xs[i].end()); }
ll left = 0;
ll right = 0;
ll left_v = 0;
ll right_v = 0;
REP(c, N) {
ll siz = c2xs[c].size();
if (siz == 0) continue;
ll minX = c2xs[c][0];
ll maxX = c2xs[c][siz - 1];
ll n_right_v = min(left_v + abs(left - minX), right_v + abs(right - minX)) +
(maxX - minX);
ll n_left_v = min(right_v + abs(right - maxX), left_v + abs(left - maxX)) +
(maxX - minX);
right_v = n_right_v;
left_v = n_left_v;
right = maxX;
left = minX;
dout << left << " " << left_v << " " << right << " " << right_v << endd;
}
ll ans = 1e17;
chmin(ans, left_v + abs(left));
chmin(ans, right_v + abs(right));
cout << ans << endl;
// cout << std::fixed << std::setprecision(15) << sqrt(ans) << endl;
} | #pragma GCC optimize("Ofast")
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <cmath>
#include <iomanip>
#include <string>
#include <deque>
#include <assert.h>
#include <random>
#include <ctime>
#include <unordered_map>
#include <complex>
#include <chrono>
#define pnode node*
using namespace std;
typedef long long ll;
typedef long double ld;
const ll mod = (ll)1e9 + 7;
ll pw(ll a, ll p) {
if (p == 0) {
return 1;
}
ll r1 = pw(a, p / 2);
if (p % 2 == 0) {
return (r1 * r1) % mod;
} else {
return (pw(a, p - 1) * a) % mod;
}
}
ll rev(ll x) {
return pw(x, mod - 2);
}
ll f[(ll)2e7];
ll c(ll n, ll k) {
if (k > n) {
return 0;
}
ll ans = 1;
// cerr << n << " " << k << "\n";
for (ll i = n; i >= n - k + 1; --i) {
ans *= i;
ans %= mod;
}
// cerr << ans << "\n";
ans = (ans * rev(f[k])) % mod;
return ans;
}
void solve(){
f[0] = 1;
for (ll i = 1; i < 2e7; ++i) {
f[i] = (f[i - 1] * i) % mod;
}
ll n, m;
vector < ll > a;
cin >> n >> m;
a.resize(n);
ll sm = 0;
for (ll i = 0; i < n; ++i) {
cin >> a[i];
sm += a[i];
}
sm += n;
m += n;
cout << c(m, sm) << "\n";
}
signed main(){
ll t = 1;
ios_base::sync_with_stdio(0);
srand(time(0));
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(20);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#endif
while(t--){
solve();
}
}
|
// abc183_f
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define REP(i, n) for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n) for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define REP_REV(i, n) for (int (i) = (int)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (int (i) = (int)(n) - 1 ; (i) >= m ; --(i))
#define ALL(x) x.begin(), x.end()
#define INF (ll)(1e9)
#define MOD (1000000007)
#define print2D(h, w, arr) REP(i, h) { REP(j, w) cout << arr[i][j] << " "; cout << endl; }
#define print_line(vec, n) {for(int idx=0;idx<(n-1);idx++) cout << (vec)[idx] << " "; cout << (vec)[(n)-1] << endl;}
template<class T> void print(const T& x){cout << x << "\n";}
template<class T, class... A> void print(const T& first, const A&... rest) { cout << first << " "; print(rest...); }
struct PreMain {PreMain(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} premain;
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
vector<int> rank;
vector<int> size; // size[i]: iが親なら連結成分の数
vector<map<int, int>> info;
UnionFind(int N, vector<int>& C) : par(N), rank(N), size(N), info(N) { //最初は全てが根であるとして初期化
for(int i = 0; i < N; i++) {
par[i] = i; rank[i] = 0; size[i] = 1;
info[i][C[i]]++;
}
}
int find(int x) { // データxが属する木の根を再帰で得る:find(x) = {xの木の根}
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) { // xとyの木を併合
int rx = find(x); //xの根をrx
int ry = find(y); //yの根をry
if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
// rank[rx] >= rank[ry] となるように rx と ry を swap
if(rank[rx] < rank[ry]) swap(rx, ry);
// 同じ高さだったならばまとめたことによって高さ +1
if(rank[rx] == rank[ry]) ++rank[rx];
par[ry] = rx; // root rx に ry の木をつなぐ
size[rx] += size[ry];
size[ry] = 0;
for (auto e: info[ry]){
info[rx][e.first] += e.second;
}
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = find(x);
int ry = find(y);
return rx == ry;
}
int get_size(int x){
int rx = find(x);
return size[rx];
}
};
int main() {
#ifdef LOCAL
ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif
int N, Q;
cin >> N >> Q;
vector<int> C(N);
REP(i, N){
cin >> C[i]; C[i]--;
}
UnionFind uf(N, C);
REP(i, Q){
int t;
cin >> t;
if (t == 1){
int a, b;
cin >> a >> b; a--; b--;
if (not uf.same(a, b)){
uf.unite(a, b);
}
} else {
int x, y;
cin >> x >> y; x--; y--;
int ans = uf.info[uf.find(x)][y];
print(ans);
}
}
return 0;
}
| #define _GLIBCXX_DEBUG
// #include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <unordered_set>
#include <utility>
#include <tuple>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const ll INF = 1ll << 60; // 十分大きい値とする (ここでは 2^60)
const ll MOD = 1000000009;
template<class T> void chmin(T &a, T b) {
if (a > b) a = b;
}
template<class T> void chmax(T &a, T b) {
if (a < b) a = b;
}
int main() {
ll N;
cin >> N;
vector< tuple<ll, ll, ll> > p(N);
rep(i, N) {
ll A, B;
cin >> A >> B;
p[i] = make_tuple(2*A + B, A, B);
}
sort(p.begin(), p.end());
reverse(p.begin(), p.end());
/*
cout << endl;
rep(i, N) {
cout << get<1>(p[i]) << " " << get<2>(p[i]) << endl;
}
cout << endl;
*/
ll score_T = 0, score_A = 0;
rep(i, N) score_A += get<1>(p[i]);
// cout << score_A << " " << score_T << endl;
rep(i, N) {
ll A, B;
A = get<1>(p[i]);
B = get<2>(p[i]);
score_A -= A;
score_T += A + B;
// cout << score_A << " " << score_T << endl;
if (score_T > score_A) {
cout << i+1 << endl;
break;
}
}
}
|
#include<bits/stdc++.h>
using namespace std;
#define IOS { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); }
typedef unsigned long long ull;
typedef long long int ll;
typedef long double ld;
#define REP(i,a,b) for(ll i=a;i<=b;++i)
#define REV(i,a,b) for(ll i=a;i>=b;i--)
#define pll pair<ll,ll>
#define vll vector<ll>
#define sll set<ll>
#define vpll vector<pll>
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define min_pq priority_queue <ll, vector<ll>, greater<ll> >
#define max_pq priority_queue <ll>
#define deci(n) fixed << setprecision(n)
#define INF 1e18
ll mod=1000000007;
////__RANGE__UPDATE___///
vector<ll> initializeDiffArray(vector<ll>& A);
void update(vector<ll>& D, ll l, ll r, ll x);
void getArray(vector<ll>& A, vector<ll>& D);
////__RANGE__UPDATE___///
ll min(ll a,ll b);
ll max(ll a,ll b);
ll gcd(ll a, ll b);
void swap(ll *a,ll *b);
ll lcm(ll a, ll b);
ll modpower(ll x, ll y, ll p);
//Only for integer y's
ll power(ll x, ll y);
ll modulo( ll value, ll m);
ll myXOR(ll x, ll y) ;
ll diff(ll a,ll b);
int main()
{
IOS;
/* #ifdef DEBUG
freopen("std.in", "r", stdin);
freopen("std.out", "w", stdout);
#else
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif */
ll rep=1;
//cin>>rep;
while(rep--)
{
ll n,k,m;cin>>n>>k>>m;
ll s=0;
ll a[n];REP(i,0,n-2){ll t;cin>>t;s+=t;}
if((s+k)/n<m)cout<<-1<<'\n';
else
{
cout<<max(0,m*(n)-s)<<'\n';
}
}
return 0;
}
ll myXOR(ll x, ll y)
{
return (x | y) & (~x | ~y);
}
ll min(ll a,ll b)
{
if(a>b)
return b;
return a;
}
ll max(ll a,ll b)
{
if(a<b)
return b;
return a;
}
ll gcd(ll a, ll b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
ll diff(ll a,ll b)
{
if(a>b)
return a-b;
return b-a;
}
void swap(ll *a,ll *b)
{
ll t=*a;
*a=*b;
*b=t;
}
ll lcm(ll a, ll b)
{
if(a==0)
return b;
if(b==0)
return a;
return (a*b)/gcd(a, b);
}
ll modpower(ll x, ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
ll power(ll x, ll y)
{
ll temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
ll modulo( ll value, ll m)
{
ll mod = value % m;
if (value < 0) {
mod += m;
}
return mod;
}
vector<ll> initializeDiffArray(vector<ll>& A)
{
ll n = A.size();
// We use one extra space because
// update(l, r, x) updates D[r+1]
vector<ll> D(n + 1);
D[0] = A[0], D[n] = 0;
for (ll i = 1; i < n; i++)
D[i] = A[i] - A[i - 1];
return D;
}
// Does range update
void update(vector<ll>& D, ll l, ll r, ll x)
{
D[l] += x;
D[r + 1] -= x;
}
// Prints updated Array
void getArray(vector<ll>& A, vector<ll>& D)
{
for (ll i = 0; i < A.size(); i++) {
if (i == 0)
A[i] = D[i];
// Note that A[0] or D[0] decides
// values of rest of the elements.
else
A[i] = D[i] + A[i - 1];
}
}
| #include <bits/stdc++.h>
using namespace std;
//actual solution is at bottom//
////<pbds>////
/*
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T> using Tree = tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
// change null_type for map
// change less to less_equal for multiset
#define ook order_of_key
#define fbo find_by_order*/
///</pbds>///
#define int long long int
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define google(i) cout<<"Case #"<<i<<" :";
///<constants>///
const int MOD = 1e9+7; // 998244353; // = (119<<23)+1
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = 4*atan((ld)1);
const int xd[4] = {0,1,0,-1}, yd[4] = {1,0,-1,0};
///</constants>///
///<IO>///
namespace io {
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
// cin.exceptions(cin.failbit); // ex. throws exception when you try to read letter into int
if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}
}
using namespace io;
///</IO>///
///<execution-time>///
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
/*example for clock usage
clock_t z = clock();
debug("Total Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
*/
///</execution-time>///
signed main(){
setIO("");
int a,b,c,d;cin>>a>>b>>c>>d;
cout<<(a*d-b*c);
return 0;
}
/* stuff you should look for
* check whether you have read the problem correctly
* int overflow, array bounds
* special cases (n=1?), slow multiset operations
* do smth instead of nothing and stay organized
*/
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<bool> consumed;
vector<set<int>> v;
void dfs(int i) {
consumed[i] = true;
for(auto e : v[i]) {
if(!consumed[e])
dfs(e);
}
}
#ifndef _LOCAL
int main() {
int n, m;
cin >> n >> m;
vector<ll> a(m), b(m);
for(int i = 0; i < m; i++)
cin >> a[i] >> b[i];
for(int i = 0; i < m; i++) {
a[i]--;
b[i]--;
}
v = vector<set<int>>(n, set<int>());
for(int i = 0; i < m; i++) {
v[a[i]].insert(b[i]);
}
ll ans = 0ll;
for(int i = 0; i < n; i++) {
consumed = vector<bool>(n, false);
dfs(i);
for(int j = 0; j < n; j++)
if(consumed[j])
ans++;
/*
cout << i << " -> ";
for(int j = 0; j < n; j++)
if(consumed[j])
cout << j << " ";
cout << endl;
*/
}
cout << ans << endl;
return 0;
}
#endif | /* これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました */
#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()
#define ld long double
#define mem(x,y) memset(x,y,sizeof x)
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_set;
const int mod = 1e9+7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
bool vis[30];
vector<int> ord;
int adj[30][30];
int choice[30];
void dfs(int v)
{
vis[v]=1;
ord.pb(v);
for(int i=1;i<=20;i++)
{
if(adj[v][i]==1 && !vis[i]) dfs(i);
}
}
ll f(int ind, int c)
{
if(ind == S(ord)-1) return 1;
choice[ord[ind]]=c;
set<int> nxt={1,2,3};
for(int i=0;i<=ind;i++)
if(adj[ord[i]][ord[ind+1]]==1) nxt.erase(choice[ord[i]]);
ll ret=0;
for(auto u:nxt) ret += f(ind+1,u);
return ret;
}
int main()
{
IOS
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
adj[a][b]=adj[b][a]=1;
}
ll ans=1;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
dfs(i);
ans *= (f(0,1)+f(0,2)+f(0,3));
ord.clear();
}
}
cout<<ans<<'\n';
}
|
#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<tuple>
#define rep(i,n) for(int i = 0;i < (n);i++)
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
// {g,x y} : ax * by = g
tuple<ll,ll,ll> extgcd(ll a,ll b){
if(b == 0) return {a,1,0};
ll g,x,y;
tie(g,x,y) = extgcd(b, a % b);
return {g,y,x - a / b * y};
}
void solve(){
int n,s,k;
cin >> n >> s >> k;
ll g,x,y;
tie(g,x,y) = extgcd(k,n);
if(s % g != 0){
cout << -1 <<endl;
return;
}
n /= g;
s /= g;
k /= g;
ll ans = ((x* - s) % n + n) % n;
cout << ans << endl;
}
int main(){
int t;
cin >> t;
rep(i,t){
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
int dp[101][505001];
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,k,mod; cin >> n >> k >> mod;
dp[0][0]=1;
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
vector<int> ps(i);
for(int j=0;j<=sum*k;j++){
int x=j%i;
(ps[x]+=dp[i-1][j])%=mod;
if(j-i*(k+1)>=0) (ps[x]-=dp[i-1][j-i*(k+1)])%=mod;
dp[i][j]=ps[x];
if(dp[i][j]<0)dp[i][j]+=mod;
}
}
sum=0;
for(int i=1;i<=n;i++){
sum+=i;
ll res=0;
for(int j=0;j<=sum*k;j++){
(res+=(ll)dp[i-1][j]*(ll)dp[n-i][j]%mod)%=mod;
}
res=res*(k+1)-1;
res%=mod;
if(res<0)res+=mod;
cout << res << "\n";
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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; }
#define all(x) (x).begin(),(x).end()
#define pll pair<ll,ll>
#define pii pair<int,int>
#define rep(i,n) for(ll i=0;i<n;i++)
#define For(i,a,b) for(int i=a;i<b;i++)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define mp make_pair
#define bit(n) (1LL<<(n))
#define F first
#define S second
#define debug(x) cerr << x << endl
#define Complex complex<double>
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
const ll INF = 1LL<<60;
const ll mod = (int)1e9 + 7;
class UnionFind {
public:
vector <ll> par; // 各元の親を表す配列
vector <ll> siz; // 素集合のサイズを表す配列(1 で初期化)
// Constructor
UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
void init(ll sz_) {
siz.assign(sz_, 1LL); // assign: 再代入
par.resize(sz_); // resize: 再確保
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
};
int main() {
int n,m;
cin >> n >> m;
ll a[n];
rep(i,n)cin >> a[i];
ll b[n];
rep(i,n)cin >> b[i];
UnionFind tree(n);
rep(i,m){
int c,d;
cin >> c >> d;
c--;
d--;
tree.merge(c,d);
}
rep(i,n){
if(tree.root(i)!=i){
a[tree.root(i)]+=a[i];
b[tree.root(i)]+=b[i];
}
}
rep(i,n){
if(tree.root(i)==i){
if(a[i]!=b[i]){
cout << "No";
return 0;
}
}
}
cout << "Yes";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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, m; cin >> n >> m;
vector<int> a(n), b(n);
for(int i=0; i<n; ++i){cin >> a[i];}
for(int i=0; i<n; ++i){cin >> b[i];}
UnionFind uf(n);
vector<vector<int>> g(n);
for(int i=0; i<m; ++i){
int c, d; cin >> c >> d; c--; d--;
g[c].push_back(d); g[d].push_back(c);
uf.unite(c, d);
}
vector<ll> sa(n, 0), sb(n, 0);
for(int v=0; v<n; ++v){
int r = uf.root(v);
sa[r] += a[v]; sb[r] += b[v];
}
bool res = true;
for(int v=0; v<n; ++v){
int r = uf.root(v);
if(sa[r]!=sb[r]){res = false;}
}
if(res) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int a[1111],b[1111];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
int ans = 111111111;
for(int i =1 ; i <= n ; i++){
scanf("%d %d",&a[i],&b[i]);
}
for(int i = 1; i <= n ; i++){
for(int j = 1 ; j <= n ; j++)
if(i == j){
ans = min (ans, a[i]+b[j]);
}else ans = min (ans, max(a[i],b[j]));
}
printf("%d\n",ans);
}
return 0;
} | #include <math.h>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main()
{
int n;
cin >> n;
int a[n], b[n];
rep(i, n)
{
cin >> a[i] >> b[i];
}
int time[2] = {a[0], b[0]};
int ans;
rep(i, n)
{
rep(j, n)
{
if (i == j)
{
if (a[i] + b[i] <= max(time[0], time[1]))
{
time[0] = a[i];
time[1] = b[j];
ans = time[0] + time[1];
}
}
else
{
if (max(a[i], b[j]) <= max(time[0], time[1]))
{
time[0] = a[i];
time[1] = b[j];
ans = max(time[0], time[1]);
}
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count());
#define printArr(arr, n) cerr << #arr << " = ["; for(int i=0; i<(n); i++){if (i) cerr << ", "; cerr << arr[i];} cerr << "]\n"
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
template<int modd> struct modint {
static const int mod = modd;
int v; explicit operator int() const { return v;}
modint() : v(0) {}
modint(ll x) : v(int(x%mod)) {if (v < 0) v += mod;}
friend bool operator==(const modint& a, const modint& b) {return a.v == b.v;}
friend bool operator!=(const modint& a, const modint& b) {return a.v != b.v;}
friend bool operator<(const modint& a, const modint& b) {return a.v < b.v;}
modint& operator+=(const modint& a) {if ((v += a.v) >= mod) v -= mod; return *this;}
modint& operator-=(const modint& a) {if ((v -= a.v) < 0) v += mod; return *this;}
modint& operator*=(const modint& a) {v = int((ll)v*a.v%mod); return *this;}
modint& operator/=(const modint& a) {return (*this) *= inv(a);}
friend modint pow(modint a, ll p) {modint ret = 1; for (; p; p /= 2, a *= a){if (p&1) ret *= a;} return ret;}
friend modint inv(const modint& a) {return pow(a,mod-2);}
modint operator-() const {return modint(-v);}
modint& operator++() {return *this += 1;}
modint& operator--() {return *this -= 1;}
friend modint operator+(modint a, const modint& b) {return a += b;}
friend modint operator-(modint a, const modint& b) {return a -= b;}
friend modint operator*(modint a, const modint& b) {return a *= b;}
friend modint operator/(modint a, const modint& b) {return a /= b;}
friend ostream& operator<<(ostream& out, modint a) {return out << a.v;}
friend istream& operator>>(istream& in, modint& a) {ll b; in >> b; a.v = b; return in;}
};
using mint = modint<1000000007>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n; char aa,ab,ba,bb;
cin >> n >> aa >> ab >> ba >> bb;
if (n == 2) {
cout << "1\n";
return 0;
}
if (ab == 'B') {
if (bb == 'B') {
cout << "1\n";
return 0;
} else {
if (ba == 'A') {
mint ans = pow(mint(2),n-3);
cout << ans << "\n";
return 0;
} else {
vector<vector<mint>> dp(n,vector<mint>(2));
dp[1][1] = 1;
for (int q = 2; q < n-1; q++) {
dp[q][1] = dp[q-1][0]+dp[q-1][1];
dp[q][0] = dp[q-1][1];
}
cout << dp[n-2][0]+dp[n-2][1] << "\n";
return 0;
}
}
}
if (ab == 'A') {
if (aa == 'A') {
cout << "1\n";
return 0;
} else {
if (ba == 'B') {
mint ans = pow(mint(2),n-3);
cout << ans << "\n";
return 0;
} else {
vector<vector<mint>> dp(n,vector<mint>(2));
dp[1][1] = 1;
for (int q = 2; q < n-1; q++) {
dp[q][1] = dp[q-1][0]+dp[q-1][1];
dp[q][0] = dp[q-1][1];
}
cout << dp[n-2][0]+dp[n-2][1] << "\n";
return 0;
}
}
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
std::fill( (T*)array, (T*)(array+N), val );
}
template<class T> inline bool chmax(T &a, T b){
if(a<b){
a = b;
return true;
}
return false;
}
template<class T> inline bool chmin(T &a, T b){
if(a>b){
a = b;
return true;
}
return false;
}
int main(){
int n,L;
cin >> n >> L;
vector<int> a(n+1);
a[0] = 0;
rep(i,n) cin >> a[i+1];
a.push_back(L+1);
vector<int> b(n+1);
b[0] = 0;
rep(i,n) cin >> b[i+1];
b.push_back(L+1);
n+=2;
vector<int> p;
rep(i,n){
if(a[i]>b[i]){
p.push_back(-1);
}else if(a[i]==b[i]){
p.push_back(0);
}else{
p.push_back(1);
}
}
ll res = 0;
int id = 0;
int left = 0;
int dir = 0;
while(id!=n+1){
if(dir!=p[id]){
if(dir==1){
if(p[id]==-1){
cout << -1 << endl;
return 0;
}
while(p[left]==0)left++;
int right = id;
// cerr <<"test2 " << left << " " << right << endl;
ll c = 1;
int L = b[left];
for(int i = left+1;i<=right;i++){
int now = a[i];
if(now - c == L){
res += c;
left++;
c--;
while(left<=right&&now-c==b[left]){
c--;
left++;
}
L = b[left];
}
c++;
}
if(c!=0){
cout << -1 << endl;
return 0;
}
}
if(dir==-1){
int right = id-1;
// cerr <<"test:" << left << " " << right << endl;
ll c = 1;
int R = b[right];
for(int i = right-1;i>=left;i--){
int now = a[i];
// cerr << now << " " << c << " " << R << endl;
if(now + c == R){
res += c;
right--;
c--;
while(right>i&&now+c==b[right]){
c--;
right--;
}
R = b[right];
}
c++;
}
// cerr << c << endl;
if(c!=1){
cout << -1 << endl;
return 0;
}
left = id;
}
dir = p[id];
}
if(dir==0&&p[id]==0){
left = id;
}
// cerr << id << " " << res << endl;
id++;
}
cout << res << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fr(i,n) for(int i = 0; i<n; i++)
#define sz(v) (int)(v.size())
#define prin(a) cout << #a << " = " << a << endl
#define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl
#define all(v) (v).begin(),(v).end()
typedef long long ll;
#define rmin(a,b) a = min<ll>(a,b)
#define rmax(a,b) a = max<ll>(a,b)
#define fi first
#define se second
const int N = 19;
vector<pair<int,int>> ct[N];
ll dp[N][(1<<N)+10];
ll n, m;
bool ok(int mask, int i){
for(auto &[tam,ns] : ct[i]){
if(bitset<32>( ((1<<tam)-1)&(mask)).count()<ns) return 0;
}
return 1;
}
ll rec(int id, int mask){
if(id==n) return 1;
ll &ans = dp[id][mask];
if(~ans) return ans;
ans = 0;
fr(i,n){
if((mask&(1<<i))==0){
int nmask = mask|(1<<i);
if(ok(nmask,id)) ans+=rec(id+1,nmask);
}
}
return ans;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
fr(mm,m){
ll xi, yi, zi; cin >> xi >> yi >> zi;
yi = (n+1)-yi;
yi--;
yi--;
ll need_smaller = max(0ll,xi-zi);
if(need_smaller) ct[yi].emplace_back(xi,need_smaller);
}
memset(dp,-1,sizeof(dp));
cout << rec(0,0) << "\n";
} | #include <iostream>
#include <cstdio>
#define N 5005
using namespace std;
int n, c, c1[N], c2[N];
string a;
int main()
{
int i, j;
cin >> n >> a;
a = '.' + a;
for (i = 1; i <= n; i++) {
c1[i] = c1[i - 1];
c2[i] = c2[i - 1];
if (a[i] == 'A') c1[i]++;
else if (a[i] == 'C') c2[i]++;
else if (a[i] == 'G') c2[i]--;
else c1[i]--;
}
for (i = 1; i <= n; i++) {
for (j = 0; j < i; j++) {
if (c1[i] - c1[j] == 0 && c2[i] - c2[j] == 0) {
c++;
}
}
}
cout << c << endl;
return 0;
} |
/**
* author: FromDihPout
* created: 2021-06-22
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s, x;
cin >> n >> s >> x;
reverse(s.begin(), s.end());
reverse(x.begin(), x.end());
vector<int> a(n);
for (int i = 0, p = 1; i < n; i++, p = (p * 10) % 7) {
a[i] = ((s[i] - '0') * p) % 7;
}
vector<bool> prev(7);
if (x[0] == 'A' && a[0] == 0) {
prev[0] = true;
} else if (x[0] == 'T') {
prev[0] = prev[7 - a[0]] = true;
}
for (int i = 1; i < n - 1; i++) {
vector<bool> curr(7);
for (int j = 0; j < 7; j++) {
if (x[i] == 'A') {
curr[j] = prev[(j + a[i]) % 7] && prev[j];
} else {
curr[j] = prev[(j + a[i]) % 7] || prev[j];
}
}
prev = curr;
}
if (x[n - 1] == 'A') {
cout << (prev[a[n - 1]] && prev[0] ? "Takahashi" : "Aoki") << '\n';
} else {
cout << (prev[a[n - 1]] || prev[0] ? "Takahashi" : "Aoki") << '\n';
}
return 0;
} | // 問題の URL を書いておく
//
#include <bits/stdc++.h>
using namespace std;
//#define ENABLE_PRINT
#if defined(ENABLE_PRINT)
#define Print(v) \
do {\
cout << #v << ": " << v << endl; \
}while(0)
#define PrintVec(v) \
do {\
for(int __i = 0; __i < v.size(); ++__i) \
{ \
cout << #v << "[" << __i << "]: " << v[__i] << endl; \
}\
}while(0)
#else
#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)
#endif
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
using ll = long long;
// 0 なら高橋勝ち
// 1 なら青木勝ち
int calc(int round, int m, string& S, string& T, vector<vector<int>>& state)
{
Print(round);
Print(m);
if(state[round][m] != -1) return state[round][m];
// 最終ラウンド
auto n = S[round] - '0';
if(round == (int)state.size() - 1)
{
if(T[round] == 'A')
{
if(m != 0 || (m + n) % 7 != 0)
{
state[round][m] = 1;
}
else state[round][m] = 0;
}
else
{
if(m == 0 || (m + n) % 7 == 0)
{
state[round][m] = 0;
}
else state[round][m] = 1;
}
Print(state[round][m]);
return state[round][m];
}
if(T[round] == 'A')
{
if(calc(round + 1, (m * 10) % 7, S, T, state) == 1 || calc(round + 1, (m + n) * 10 % 7, S, T, state) == 1)
{
state[round][m] = 1;
return 1;
}
state[round][m] = 0;
return 0;
}
else
{
if(calc(round + 1, (m * 10) % 7, S, T, state) == 0 || calc(round + 1, (m + n) * 10 % 7, S, T, state) == 0)
{
state[round][m] = 0;
return 0;
}
state[round][m] = 1;
return 1;
}
}
int main(int, const char**)
{
int N;
cin >> N;
string S, T;
cin >> S >> T;
vector<vector<int>> state(N, vector<int>(7, -1));
auto ans = calc(0, 0, S, T, state);
Print(ans);
cout << (ans == 0 ? "Takahashi" : "Aoki") << endl;
return 0;
}
|
//#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> p;
const int INF = 1e9;
const double eps = 1e-7;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const long double pi = 4 * atan(1);
#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 < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) \
{ \
cout << x << ' '; \
} \
cout << endl;
template <class T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
if (b < a)
{
a = b;
return 1;
}
return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v)
{
for (T &x : v)
is >> x;
return is;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
vector<int> a(n);
cin >> a;
map<int, int> m;
rep(i, n)
{
m[a[i]]++;
}
int ans = 0;
int i = 1;
int box = min(m[0],k);
while(box){
int h = max(box - m[i],0);
ans += h*i;
chmin(box,m[i]);
i++;
}
cout<<ans<<"\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define all(c) (c).begin(), (c).end()
// !!READ THE PROBLEM STATEMENT CORRECTLY YOU STUPID, WORTHLESS PIECE OF SHIT!!
//Analyze testcases thoroughly before starting implementation
//Don't forget to read the stuff at the bottom
void solve() {
int N, M;
cin >> N >> M;
vector <int> A(N), B(M);
for (auto &a : A) cin >> a;
for (auto &b : B) cin >> b;
// this is equivalent to an edit distance problem
vector <vector <int> > dp(N + 1, vector <int> (M + 1));
for (int i = 1; i <= N; i++) {
dp[i][0] = i;
for (int j = 1; j <= M; j++) {
dp[0][j] = j;
dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (A[i - 1] != B[j - 1])});
}
}
cout << dp[N][M];
}
int main() {
fastIO;
solve();
cout << '\n';
return 0;
}
/*
check for edge cases : n = 0, 1 etc
check for overflows and out of bounds
*/ |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x) \
for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << "(" << p.first << " " << p.second << ")";
return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
rep(i, v.size()) {
if (i) os << " ";
os << v[i];
}
return os;
}
template <class T, class U>
istream& operator>>(istream& is, pair<T, U>& p) {
is >> p.first >> p.second;
return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
rep(i, v.size()) { is >> v[i]; }
return is;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
#ifdef LOCAL
#define dbg(x) cerr << #x << ": " << (x) << '\n'
#define say(x) cerr << (x) << '\n'
#else
#define dbg(x)
#define say(x)
#endif
string solve(bool a) { return ((a) ? "Yes" : "No"); }
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
// int up = 1e9;
// int down = 1;
// bool can = false;
cin >> n >> m;
int margin = m + 1;
dbg(margin);
VPi ans(n);
if (m == 0) {
rep(i, n) { ans[i] = mp(i * 2 + 1, i * 2 + 2); }
} else if (m < 0 || m > n - 2) {
cout << -1 << endl;
return 0;
} else {
for (int i = 1; i <= margin; i++) {
ans[i - 1] = mp(2 * i, 2 * i + 1);
dbg(ans[i - 1]);
}
ans[margin] = mp(1, 2 * margin + 2);
dbg(ans[margin]);
for (int i = margin + 1; i < n; i++) {
ans[i] = mp(2 * margin + 2 + 2 * i, 2 * margin + 3 + 2 * i);
}
}
rep(i, n) { cout << ans[i].F << " " << ans[i].S << endl; }
}
/*
1 9, 2 3 , 4 5 ,6 7 (2:2)
1 5, 6 9, 2 3 , 7 8 (0:0/0)
1 2 ,3 9, 4 5 ,6 7 (1 : 0/1)
2 3, 4 5, 6 7, (2n: 2n+1;n=1...M) +(1:2(M+1))
*/ | //vs solve(int n){
// vs ans;
// per(bit,n){
// rep(cases,1<<(n-1-bit)){
// ll mask = 1<<bit;
// rep(i,n-1-bit)
// if(cases&(1<<i))
// mask ^= 1<<(bit+i+1);
// string cur;
// rep(i,1<<n)
// if(__builtin_popcountll(i&mask)%2 == 1)
// cur += "A";
// else
// cur += "B";
//
// ans.pb(cur);
// }
// }
// return ans;
//}
//void __(){
// _(ll,k);
// vector<string> ans;
// rep(i,k){
// string cur;
// rep(j,(1<<k))
// if(j&(1<<i))
// cur += "A";
// else
// cur += "B";
// ans.pb(cur);
// }
// ans = solve(k);
// print sz(ans);
// print_vn ans;
// //if(_[i] != _[j])
//}
//
#include <iomanip>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define pb push_back
#define REP_ZERO_INT(i,r) for(int i = 0; i < (r); ++i)
#define GET_REP_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) GET_REP_MACRO(__VA_ARGS__,REP_ANY,REP_INT,REP_ZERO_INT)(__VA_ARGS__)
#define PER_ZERO_INT(i,r) for(int i = (r)-1; i >= 0; --i)
#define GET_PER_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define per(...) GET_PER_MACRO(__VA_ARGS__,PER_ANY,PER_INT,PER_ZERO_INT)(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<string> vs;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
T1 istream& read(T, T, istream& = cin);
struct _print {
string sep,end;
bool space;
ostream &stream;
_print(string _sep=" ",string _end="\n",
ostream &_stream = cout)
: sep(_sep),end(_end),space(false),
stream(_stream) {}
~_print() { stream << end; }
template <typename T>
_print &operator , (const T &t) {
if (space) stream << sep;
space = true;
stream << t;
return *this;
}
};
struct _print2{
string s1,s2,end;
bool space;
ostream &stream;
_print2(string _s1 = " ", string _s2 = " ", string _end="\n", ostream &_stream = cout)
:s1(_s1),s2(_s2),end(_end),space(false),
stream(_stream) {}
~_print2() { stream << end; }
template <typename T>
_print2 &operator , (const T &t) {
for(auto _ : t){
if (space) stream << s1;
space = true;
_print p(s2,"",stream);
p,_;
}
return *this;
}
};
#define print _print(),
#define print_vn _print2("\n"," ","\n"),
#define INPUT_WITHOUT_INIT(type,name) type name; cin >> name
#define GET_INPUT_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define _(...) GET_INPUT_MACRO(__VA_ARGS__,_IWI,_IWI,_IWI,_IWI,_IWI,_IWI,INPUT_WITHOUT_INIT)(__VA_ARGS__)
vs solve(int n){
vs ans;
per(bit,n){
rep(cases,1<<(n-1-bit)){
ll mask = 1<<bit;
rep(i,n-1-bit)
if(cases&(1<<i))
mask ^= 1<<(bit+i+1);
string cur;
rep(i,1<<n)
if(__builtin_popcountll(i&mask)%2 == 1)
cur += "A";
else
cur += "B";
ans.pb(cur);
}
}
return ans;
}
void __(){
_(ll,k);
vector<string> ans;
rep(i,k){
string cur;
rep(j,(1<<k))
if(j&(1<<i))
cur += "A";
else
cur += "B";
ans.pb(cur);
}
ans = solve(k);
print sz(ans);
print_vn ans;
//if(_[i] != _[j])
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
__();
}
|
#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define int ll
#define each(i, a) for (auto &&i : (a))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using vp = vector<P>;
using int128 = __int128_t;//cin coutはできない
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a / gcd(a, b) * b;};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
template <class T>
void CVEC(const T &v) {
int c = v.size() - 1;
for (int i = 0; i < c; i++) cout << v[i] << ' ';
if (c > -1) cout << v[c];
cout << '\n';
}
#pragma endregion header
signed main(){
int n, m; cin >> n >> m;
set<int> a, b;
rep(i,n){
int t; cin >> t;
a.insert(t);
}
rep(i,m){
int t; cin >> t;
b.insert(t);
}
for(int i = 1; i <= 1000; i++){
bool isA = a.count(i);
bool isB = b.count(i);
if(isA && isB) continue;
if(!isA && !isB) continue;
cout << i << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, m;
cin >> n >> k >> m;
int s = 0;
int A;
for(int i = 0; i < n - 1; i++){
cin >> A;
s += A;
}
int B = n*m - s;
if(B > k){
cout << -1;
}
else if(B < 0){
cout << 0;
}
else{
cout << B;
}
} |
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <ctime>
#include <complex>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-10;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
if(a>b){
a = b; return true;
}
else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
if(a<b){
a = b; return true;
}
else return false;
}
template<typename T> inline void print(T &a){
for(auto itr = a.begin(); itr != a.end(); itr++){
cout << *itr << " ";
}
cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
cout << "debug: " << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
cout << "debug: " << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;
template <std::uint_fast64_t Modulus> class modint {
using u64 = std::uint_fast64_t;
public:
u64 a;
constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
constexpr u64 &value() noexcept { return a; }
constexpr const u64 &value() const noexcept { return a; }
constexpr modint operator+(const modint rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint &operator+=(const modint rhs) noexcept {
a += rhs.a;
if (a >= Modulus) {
a -= Modulus;
}
return *this;
}
constexpr modint &operator-=(const modint rhs) noexcept {
if (a < rhs.a) {
a += Modulus;
}
a -= rhs.a;
return *this;
}
constexpr modint &operator*=(const modint rhs) noexcept {
a = a * rhs.a % Modulus;
return *this;
}
constexpr modint &operator/=(modint rhs) noexcept {
u64 exp = Modulus - 2;
while (exp) {
if (exp % 2) {
*this *= rhs;
}
rhs *= rhs;
exp /= 2;
}
return *this;
}
};
using mint = modint<mod>;
ll modpow(ll x, ll n, ll mod){
ll res = 1;
while(n > 0){
if(n & 1) res = res * x % mod;
x = x * x % mod;
n >>= 1;
}
return res;
}
int main(){
int t; cin >> t;
while(t--){
ll n,a,b; cin >> n >> a >> b;
if(n < a+b){
cout << 0 << "\n";
continue;
}
if(a > b) swap(a,b);
mint ans = n * 2 - b * 2 - a + 3;
ans *= a;
mint t = (n-b+1) * (b-a-1) + mod;
ans += t;
ans *= ans;
mint as = (n-a+1) * (n-b+1);
as *= as;
ans = as - ans;
cout << ans.value() << "\n";
}
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
std::fstream myfile("D:\\CodeBlocks\\c++\\input.txt.c", std::ios_base::in);
ll n,sum=0;
cin>>n;
while(n--){
ll a,b;
cin>>a>>b;
sum+=((b*(b+1)/2)-(a*(a-1)/2)) ;
}
cout<<sum;
return 0;
}
|
//@formatter:off
#include<bits/stdc++.h>
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(i,n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i,s,n) for (ll i = ll(s); i < ll(n); ++i)
#define rep3(i,s,n,d) for(ll i = ll(s); i < ll(n); i+=d)
#define rep(...) overload4(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep(i,n) for (ll i = ll(n)-1; i >= 0; i--)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
#ifdef __LOCAL
#define debug(...) { cout << #__VA_ARGS__; cout << ": "; print(__VA_ARGS__); }
#else
#define debug(...) void(0)
#endif
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> void fin(T a) { cout << a << '\n'; exit(0); }
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 scan(){}
template <class Head, class... Tail> void scan(Head& head, Tail&... tail){ cin >> head; scan(tail...); }
template<class T> void print(const T& t){ cout << t << '\n'; }
template <class Head, class... Tail> void print(const Head& head, const Tail&... tail){ cout<<head<<' '; print(tail...); }
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
INT(n, m);
vvp G(n);
vi a(m), b(m);
rep(i, m) {
scan(a[i], b[i]);
a[i]--;
b[i]--;
}
vi c(n);
cin >> c;
vi ans(m, -1);
int cnt = 0;
auto direct = [&](int id, int from, int to) {
if (a[id] == from) ans[id] = 0;
else ans[id] = 1;
};
rep(i, m) {
if (c[a[i]] > c[b[i]]) direct(i, a[i], b[i]);
else if (c[a[i]] < c[b[i]]) direct(i, b[i], a[i]);
else {
G[a[i]].eb(b[i], i);
G[b[i]].eb(a[i], i);
cnt++;
}
}
vb seen(n);
auto dfs = [&](auto &self, int u, int p) -> void {
seen[u] = true;
for (auto[v, id] : G[u]) {
if (v == p) continue;
if (ans[id] < 0) direct(id, u, v);
if (!seen[v]) {
self(self, v, u);
}
}
};
rep(i, n) {
if (seen[i]) continue;
dfs(dfs, i, -1);
}
rep(i, m) {
assert(ans[i] >= 0);
cout << (ans[i] ? "<-" : "->") << endl;
}
} | #include<bits/stdc++.h>
using namespace std;
long long n,m,a[5005],b[5005],c[5005];
vector<int> grafo[105];
map<pair<int,int>,bool> f;
bool vis[101];
void dfs(int nodo){
if(vis[nodo])return;
vis[nodo]=true;
for(auto i : grafo[nodo]){
if(c[i]<c[nodo] || (c[i]==c[nodo] && f[{i,nodo}]==false && f[{nodo,i}]==false)){
f[{nodo,i}]=true;
dfs(i);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>a[i]>>b[i];
grafo[a[i]].push_back(b[i]);
grafo[b[i]].push_back(a[i]);
}
for(int i=1;i<=n;i++)cin>>c[i];
for(int i=1;i<=n;i++){
dfs(i);
}
for(int i=0;i<m;i++){
if(f[{a[i],b[i]}])cout<<"->";
else cout<<"<-";
cout<<"\n";
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\n'
int main (){
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n;cin>>n;
vector<ll>a(n),b(n);
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n;i++) cin>>b[i];
// for(int i=n-1;i>=0;i--){
// suff_b[i]=max(suff_b[i+1],b[i]);
// }
ll mx_a = 1, mx_b = 1,pr=1;
for(int i=0;i<n;i++){
mx_a=max(mx_a,a[i]);
cout<<max(pr,mx_a*b[i])<<endl;
pr = max(pr,mx_a*b[i]);
}
return 0;
}
| #include <iostream>
#include <algorithm>
#include <string>
#include <complex>
#include <vector>
#include <set>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <numeric> //lcm
#include <iomanip> //double精度 setprecision
#include <chrono>
#include <random>
#include <cassert>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = (n)-1; i >= 0; --i)
#define rep1(i,n) for(int i = 1; i <= (n); ++i)
#define rrep1(i,n) for(int i = (n); i >= 1; --i)
#define REP(i,n,m) for(int i = (n); i < (m); ++i)
#define all(vec) (vec).begin(),(vec).end()
#define debug(a) { cerr << #a << " = " << a << endl;}
#define debugV(vec) { cerr << #vec << " : "; for(auto v : vec) cerr << v << " "; cerr << endl; }
#define debug2D(vec2D, w) { cerr << #vec2D << endl; for(auto vec : vec2D) { for (auto v : vec) cerr << setw(w) << v << " "; cerr << endl; } }
#define debugVP(vec) { cerr << #vec << " : "; for(auto v : vec) cerr << "(" << v.first << "," << v.second << ") "; cerr << endl;}
#define debug2DP(vec2D) { cerr << #vec2D << endl; for(auto vec : vec2D) { for (auto v : vec) cerr << "(" << v.first << "," << v.second << ") "; cerr << endl; } }
typedef long long ll;
constexpr ll INF = 1e14; //32 ~2*10^10 //64 ~9*10^19
//constexpr ll MOD = 998244353;
constexpr ll MOD = 1000000007;
const double EPS = round(1e-6);
const char en = '\n';
/*
struct Edge {
Edge(int _s, int _t, ll _cost = 1) : from(_s), to(_t), cost(_cost) {};
int from, to;
ll cost;
//ll fee;
};
*/
//typedef ll Edge;7
struct Edge {
int id;
int from, to;
Edge() { id = from = to = -1; };
Edge(int a, int b, int c) : id(a), from(b), to(c) {};
};
typedef vector<vector<Edge>> Graph;
/*
void add_edge(Graph& g, int from, int to, ll cost = 1) {
g[from].push_back(Edge(from, to, cost));
}
*/
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } else return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } else return false; }
void solve() {
ll n; cin >> n;
vector<ll> a(n), b(n);
vector<ll> ma(n), mb(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
rep(i, n) { ma[i] = a[i]; if (i-1 >= 0) chmax(ma[i], ma[i-1]);}
ll ans = 0;
rep(i, n) {
chmax(ans,a[i]*b[i]);
if (i - 1 >= 0) chmax(ans,ma[i-1LL]*b[i]);
cout << ans << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
//rep(i,3)
solve();
} |
#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#define ll long long
#include <iomanip>
void solve()
{
ll int n;
cin>>n;
ll int arr[n];
ll int count=0;
unordered_map<ll int,ll int> m;
for(ll int i=0;i<n;i++)
{
cin>>arr[i];
if(arr[i]>=200)
{
m[arr[i]%200]++;
}
else
{
m[arr[i]]++;
}
}
for(auto i:m)
{
ll int x=i.second;
if(x>1)
{
count+=(x*(x-1)/2);
}
}
cout<<count<<endl;
}
int main()
{
// ll int t;
// cin>>t;
// while(t--)
// {
// solve();
// }
solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;//10の18乗
#define yn {puts("Yes");}else{puts("No");}
#define dame { puts("-1"); return 0;}
int main() {
ll n;
cin >> n;
ll ans=0;
Vl v(n);
rep(i,n) cin >> v[i];
Vl cnt(250);
rep(i,n) cnt[(v[i])%200]++;
rep(i,250){
ans+=cnt[i]*(cnt[i]-1)/2;
}
cout << ans << endl;
}
|
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <iomanip>
#include <algorithm>
#include <iterator>
#include <stack>
#include <queue>
#include <unordered_set>
#include <stdio.h>
#include <math.h>
#include <bitset>
using namespace std;
#define pb push_back
#define mp make_pair
#define ifor(a, b) for(int i = a; i < b; ++i)
#define jfor(a, b) for(int j = a; j < b; ++j)
#define kfor(a, b) for(int k = a; k < b; ++k)
#define irof(a, b) for(int i = b - 1; i >= a; --i)
#define jrof(a, b) for(int j = b - 1; j >= a; --j)
#define krof(a, b) for(int k = b - 1; k >= a; --k)
#define all(a) a.begin(), a.end()
#define safe_ind(ind, mass) ((ind) >= 0 ? mass[ind] : 0)
typedef long long ll;
typedef unsigned long long ull;
ll mmod(ll x, ll y) {
if (x >= 0LL)
return x % y;
else
return (y - (-x) % y) % y;
}
ll ddiv(ll x, ll y) {
x -= mmod(x, y);
return x / y;
}
struct edge {
int u;
int v;
double weight;
edge(int u, int v, double weight) :u(u), v(v), weight(weight) {}
};
double sosize(pair<int, int>& u, pair<int, int>& v) {
return sqrt((u.first - v.first) * (u.first - v.first) + (u.second - v.second) * (u.second - v.second));
}
vector<int> dsu_par;
vector<int> dsu_size;
void dsu_init(int n) {
dsu_par.resize(n);
ifor(0, n) dsu_par[i] = i;
dsu_size.resize(n, 1);
}
int dsu_root(int u) {
if (dsu_par[u] == u) return u;
return dsu_par[u] = dsu_root(dsu_par[u]);
}
void dsu_merge(int u, int v) {
u = dsu_root(u);
v = dsu_root(v);
if (u != v) {
if (dsu_size[v] > dsu_size[u]) {
swap(u, v);
}
dsu_par[v] = u;
dsu_size[u] += dsu_size[v];
}
}
void solve() {
int n; cin >> n;
vector<pair<int, int>> dots;
ifor(0, n) {
int x, y;
cin >> x >> y;
dots.emplace_back(x, y);
}
vector<edge> edges;
ifor(0, n) {
edges.emplace_back(i, n, 100 - dots[i].second);
edges.emplace_back(i, n + 1, dots[i].second + 100);
}
ifor(0, n - 1) {
jfor(i + 1, n) {
edges.emplace_back(i, j, sosize(dots[i], dots[j]));
}
}
sort(all(edges), [](edge& f, edge& s) {
return f.weight < s.weight;
});
dsu_init(n + 2);
for (auto& it : edges) {
dsu_merge(it.u, it.v);
if (dsu_root(n) == dsu_root(n + 1)) {
cout << fixed;
cout << setprecision(8) << it.weight / 2 << "\n";
return;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
kfor(0, t)
solve();
//system("pause");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INCANT cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12)
const int INF = 1e9 + 7;
const ll LINF = 1e18 + 7;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
void debug_out();
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T);
#ifdef MY_DEBUG
#define dbg(...) debug_out(__VA_ARGS__)
#define dbgs(x) cout << x << " "
#define dbge cout << endl
#else
#define dbg(...)
#define dbgs(x)
#define dbge
#endif
template <class T> bool chmax(T &, const T &);
template <class T> bool chmin(T &, const T &);
template <class T> T pwr(T, ll);
template <class T> T squ(T);
ll fact(ll);
ll comb(ll, ll);
int main() {
INCANT;
int n;
cin >> n;
vector<pair<int, int>> x(n), y(n);
vector<pair<int, pair<int, int>>> ls(4);
for (int i = 0; i < n; i++) {
cin >> x[i].first >> y[i].first;
x[i].second = i, y[i].second = i;
}
sort(x.begin(), x.end());
reverse(x.begin(), x.end());
sort(y.begin(), y.end());
reverse(y.begin(), y.end());
ls[0].first = x[0].first - x[n-1].first;
ls[1].first = max(x[1].first - x[n-1].first, x[0].first - x[n-2].first);
ls[2].first = y[0].first - y[n-1].first;
ls[3].first = max(y[1].first - y[n-1].first, y[0].first - y[n-2].first);
set<int> s1, s2;
s1.insert(x[0].second), s1.insert(x[n-1].second);
s2.insert(y[0].second), s2.insert(y[n-1].second);
if (s1 == s2) {
cout << max(ls[1].first, ls[3].first) << endl;
}
else {
sort(ls.begin(), ls.end());
reverse(ls.begin(), ls.end());
cout << ls[1].first << endl;
}
}
/*
-----------------------------------以下関数宣言------------------------------------ */
void debug_out() {
cout << endl;
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <class T> T pwr(T x, ll n) {
T ans = 1;
for (int i = 0; i < n; i++) {
ans *= x;
}
return ans;
}
template <class T> T squ(T x) {
return x * x;
}
ll fact(ll n) {
ll res = 1;
for (ll i = 1; i <= n; i++) res *= i;
return res;
}
ll comb(ll n, ll r) { // comb(60, 30)までオーバーフローなし
ll res = 1;
for (int i = 1; i <= r; i++) {
res *= n--;
res /= i;
}
return res;
}
|
#include <bits/stdc++.h>
using namespace std;
int _ = (cout << fixed << setprecision(9), cin.tie(0), ios::sync_with_stdio(0));
using Int = long long;
int main() {
int N; cin >> N;
vector<int> A(N); for (auto &a : A) cin >> a;
for (int i = 1; i < N; i += 2) {
A[i] *= -1;
}
vector<Int> S(N + 1);
for (int i = 0; i < N; i++) {
S[i + 1] = S[i] + A[i];
}
map<Int, int> M;
for (int i = 0; i < N + 1; i++) {
M[S[i]]++;
}
Int ans = 0;
for (auto [p, q] : M) {
ans += (Int)q * (q - 1) / 2;
}
cout << ans << '\n';
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mk make_pair
#define MOD 1000000007
#define pi 3.1415926536
/* BAAP BAAP HOTA HAI
AUTHOR- Devansh Nandasana*/
void adde(vector<ll> adj[],ll u,ll v)
{
adj[u].pb(v);
adj[v].pb(u);
}
ll modInverse(ll a, ll m)
{
ll m0=m;
ll y=0,x=1;
if(m==1)
return 0;
while(a>1)
{
ll q=a/m;
ll t=m;
m=a%m,a=t;
t=y;
y=x-q*y;
x=t;
}
if(x<0)
x+=m0;
return x;
}
void BFS(vector<ll> adj[],ll bfs[],ll s,ll n)
{
bool *visited = new bool[n+1];
for(ll i=0;i<n+1;i++)
visited[i] = false;
list<ll> queue;
visited[s] = true;
bfs[s]=0;
queue.push_back(s);
vector<ll>::iterator i;
while(!queue.empty())
{
s=queue.front();
queue.pop_front();
for(i=adj[s].begin();i!=adj[s].end();++i)
{
if(!visited[*i])
{
visited[*i] = true;
bfs[*i]=bfs[s]+1;
queue.push_back(*i);
}
}
}
}
ll nCrModp(ll n,ll r,ll p)
{
if (r > n - r)
r = n - r;
ll C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (ll i = 1; i <= n; i++) {
for (ll j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
ll find_center(ll n,vector<ll> adj[])
{
queue<ll> q;
bool vis[n+1];
ll cnt[n+1];
for(ll i=1;i<=n;i++)
{
vis[i]=false;
cnt[i]=adj[i].size();
if(cnt[i]==1)
q.push(i);
}
while(q.size()>1)
{
ll cur=q.front();
vis[cur]=true;
q.pop();
for(ll i=0;i<adj[cur].size();i++)
{
if(!vis[adj[cur][i]])
{
cnt[adj[cur][i]]--;
if(cnt[adj[cur][i]]==1)
q.push(adj[cur][i]);
}
}
}
return q.front();
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/devansh/Desktop/temp/input.txt", "r", stdin);
freopen("/home/devansh/Desktop/temp/output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin>>n;
ll a[n+1];
for(ll i=1;i<=n;i++)
cin>>a[i];
ll dp[n+1];
dp[n]=1e18;
for(ll i=n-1;i>=0;i--)
{
dp[i]=a[i+1]-dp[i+1];
}
map<ll,ll> m;
ll ans=0;
for(ll i=0;i<=n;i++)
m[dp[i]]++;
for(auto [i,val]:m)
{
ans+=(val*(val-1)/2);
if(i!=0 && i>0)
ans+=(m[i]*m[-i]);
// cout<<ans<<"\n";
}
cout<<ans<<"\n";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) rep2(i,0,n)
#define rep2(i,l,r) for(ll i=(l),i##_len=(r);i<i##_len;i++)
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template <typename T>class segtree{
typedef function<T(T,T)> F;
const T _e;
F fun1,fun2;
public:
ll _n;
vector<T> node;
//Constructor
segtree():_n(0){}
segtree(F resultFunction,ll n,T e):segtree(resultFunction,vector<T>(n,e),e,NULL){}
segtree(F resultFunction,vector<T> &v,T e):segtree(resultFunction,v,e,NULL){}
segtree(F resultFunction,vector<T> &v,T e,F changeFunction):fun1(resultFunction),_e(e),fun2(changeFunction){
ll m=v.size();
_n=1;
while(_n<m) _n*=2;
node.resize(2*_n-1);
for(ll i=0;i<2*_n-1;i++){
if(_n-1<=i&&i<_n+m-1) node[i]=v[i-_n+1];
else node[i]=e;
}
}
//Build//O(N)
void build(){
for (ll i = _n - 2; i>= 0; i--){
node[i] = fun1(node[2 * i + 1], node[2 * i + 2]);
}
}
//Update//O(logN)
void update(ll num,T x,bool mode){
num+=_n-1;
if(mode) node[num]=fun2(node[num],x);
else node[num]=x;
while(num>0){
num=(num-1)/2;//parent
node[num]=fun1(node[num*2+1],node[num*2+2]);
}
}
//Query//O(logN)
T query(ll a,ll b){return query_sub(a,b,0,0,_n);}
T query_sub(ll a,ll b,ll k,ll l,ll r){
if(r<=a||b<=l) return _e;
else if(a<=l&&r<=b) return node[k];
else{
T vl=query_sub(a,b,2*k+1,l,(l+r)/2);
T vr=query_sub(a,b,2*k+2,(l+r)/2,r);
return fun1(vl,vr);
}
}
};
int f(int a,int b){return a xor b;}
int main(){
ll n,q;cin>>n>>q;
vector<int> a(n);rep(i,n) cin>>a[i];
segtree<int> seg(f,a,0,f);
seg.build();
rep(i,q){
int t,x,y;cin>>t>>x>>y;
x--;
if(t==1) seg.update(x,y,1);
else cout<<seg.query(x,y)<<endl;
}
} |
// Problem : F - Range Xor Query
// Contest : AtCoder - AtCoder Beginner Contest 185
// URL : https://atcoder.jp/contests/abc185/tasks/abc185_f
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)
#include <bits/stdc++.h>
using namespace std;
#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long
#define endl '\n'
#define vi vector<ll>
#define pii pair<ll, ll>
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define fill(a,b) memset(a, b, sizeof(a))
#define setbits(x) __builtin_popcountll(x)
const ll inf=0x3f3f3f3f3f3f3f3f, N = 3e5 + 5;
ll a[N], st[4 * N], n, q;
void build(ll node, ll L, ll R) {
if(L == R) {
st[node] = a[L];
return;
}
ll M = (L + R)>>1;
build(2 * node, L, M);
build(2 * node + 1, M + 1, R);
st[node] = st[2 * node] ^ st[2 * node + 1];
}
void update(ll node, ll L, ll R, ll pos, ll y) {
if(pos < L || pos > R)return;
if(L == R) {
a[L] ^= y;
st[node] ^= y;
return;
}
ll M = (L + R)>>1;
update(2 * node, L, M, pos, y);
update(2 * node + 1, M + 1, R, pos, y);
st[node] = st[2 * node] ^ st[2 * node + 1];
}
ll query(ll node, ll L, ll R, ll l, ll r) {
if(L > r || l > R)return 0;
if(l <= L && r >= R)return st[node];
ll M = (L + R)>>1;
return (query(2 * node, L, M, l, r) ^ query(2 * node + 1, M + 1, R, l, r));
}
int main() {
SPEED;
cin>>n>>q;
for(ll i = 1; i <= n; i++)cin>>a[i];
build(1, 1, n);
while(q--) {
ll t, x, y;
cin>>t>>x>>y;
if(t == 1)update(1, 1, n, x, y);
else cout<<query(1, 1, n, x, y)<<endl;
}
return 0;
} |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
int main(){
int n,m;
cin >> n >> m;
vl h(n),w(m),a(n),s(n);
rep(i,n)cin >> h[i];
rep(i,m)cin >> w[i];
sort(h.begin(),h.end());
sort(w.begin(),w.end());
for(int i=0;i<n-1;i++){
a[i]=h[i+1]-h[i];
if(i==0 or i==1) s[i]=a[i];
else s[i]=s[i-2]+a[i];
}
ll ans=1e14;
if(n==1){
rep(i,m) ans=min(ans,abs(h[0]-w[i]));
}else rep(i,m){
//cout << ans << "\n";
int insert=lower_bound(h.begin(),h.end(),w[i])-h.begin();
int counter;
if(insert%2==0){
if(insert==0) ans=min(ans,(h[0]-w[i])+s[n-2]);
else if(insert==n-1) ans=min(ans,s[n-3]+(h[n-1]-w[i]));
else ans=min(ans,s[insert-2]+(h[insert]-w[i])+(s[n-2]-s[insert-1]));
}else{
if(insert==n) ans=min(ans,(w[i]-h[n-1])+s[n-3]);
else if(insert==1) ans=min(ans,(w[i]-h[0])+s[n-2]);
else ans=min(ans,s[insert-3]+(w[i]-h[insert-1])+(s[n-2]-s[insert-2]));
}
}
cout << ans;
return 0;
} | //
#include<bits/stdc++.h>
using namespace std;
#define PB push_back
#define f fiCrst
#define s second
#define what_is(x) cerr << #x << " is " << x << endl;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const ll MOD = 1000000007;
void solve_test()
{
int n, m;
cin >> n >> m;
ll h[n+1];
h[0] = -INF;
ll tot = 0;
for(int i=1; i<=n; i++)
{
cin >> h[i];
tot += h[i];
}
sort(h+1, h+n+1);
ll sum[n+1][2];
memset(sum, 0, sizeof(sum));
sum[1][1] = h[1];
for(int i=2; i<=n; i++)
{
for(int j=0; j<2; j++)
{
sum[i][j] = sum[i-1][j];
}
sum[i][i%2] += h[i];
}
ll ans = 1e18;
for(int i=0; i<m; i++)
{
ll x;
cin >> x;
int ind = upper_bound(h, h+n+1, x) - h;
ll sum1 = sum[ind-1][ind%2];
sum1 += sum[n][(ind+1)%2] - sum[ind-1][(ind+1)%2];
ll sum2 = tot - sum1;
sum1 += x;
// what_is(x);
// what_is(sum1);
// what_is(sum2);
ans = min(ans, abs(sum1-sum2));
}
cout << ans;
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int tests;
tests = 1;
//cin >> tests;
while(tests--)
{
solve_test();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string ans = "No";
string N;
cin >> N;
for (int i = 0; i <= 9; i++) {
string Nt = string(i, '0') + N;
if (Nt.size() % 2 == 1) Nt.erase(Nt.begin() + Nt.size() / 2);
string s1 = Nt.substr(0, Nt.size() / 2);
string s2 = Nt.substr(Nt.size() / 2);
reverse(s2.begin(), s2.end());
if (s1.compare(s2) == 0) ans = "Yes";
}
cout << ans << endl;
} | #include "bits/stdc++.h"
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
long long a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, m = 0, n = 0, p = 0, q = 0, mod = 998244353, pi = 3.1415926535;
string s1,s2;
char moji;
cin >> s1;
while(s1.size())
{
if(s1.back()=='0')
s1.pop_back();
else
break;
}
s2=s1;
reverse(s1.begin(),s1.end());
cout << (s1==s2?"Yes":"No") << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
int main() {
int n;
int a[1001], b[1001];
std::cin >> n;
for(int i = 0; i < n; ++i) {
std::cin >> a[i] >> b[i];
}
int min = 1000000;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int sum = std::max(a[i], b[j]);
if (sum < min) min = sum;
sum = std::max(b[i], a[j]);
if (sum < min) min = sum;
}
}
for (int i = 0; i < n; ++i) {
int sum = a[i] + b[i];
if (sum < min) min = sum;
}
std::cout << min << std::endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
struct mount{string name; int height;};
int main()
{
int n;
cin >> n;
mount m[n];
rep(i, n) cin >> m[i].name >> m[i].height;
int a1 = 0, a2 = -1, m1 = 0, m2 = 0;
rep(i, n){
if(m[i].height > m1){
m2 = m1;
m1 = m[i].height;
a2 = a1;
a1 = i;
}
else if(m[i].height > m2){
m2 = m[i].height;
a2 = i;
}
}
cout << m[a2].name << endl;
return 0;
} |
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <utility>
#include <tuple>
#include <cctype>
#include <bitset>
#include <complex>
#include <cmath>
#include <array>
#include <numeric>
#include <cassert>
#include <memory>
#include <random>
#include <chrono>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3fLL
//#define MOD 998244353
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pint;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> tint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<pint> vpint;
typedef vector<tint> vtint;
typedef vector<vint> vvint;
// int dx[8]={0,0,-1,1,1,1,-1,-1};
// int dy[8]={-1,1,0,0,1,-1,1,-1};
const int SIZE=50;
//↑templete
int si, sj;
int t[SIZE][SIZE] = {};
int p[SIZE][SIZE] = {};
string nowStr = "";
string bestStr = "";
bool usedTile[2500] = {};
int score = 0;
int bestScore = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
string com[4] = {"L", "D", "R", "U"};
std::random_device seed_gen;
std::mt19937 engine(seed_gen());
chrono::system_clock::time_point start;
bool isEnd = 0;
void DFS(int x, int y){
if (isEnd)
return;
int nowTime = chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now() - start).count();
if (nowTime > 100)
{
isEnd = 1;
// cout << bestStr << endl;
return;
}
//現在位置が入力される
//マスを埋めて、スコアを加算する
int tile = t[x][y];
if (usedTile[tile])
return;
score += p[x][y];
usedTile[tile] = 1;
if (bestScore < score)
{
bestScore = score;
bestStr = nowStr;
// cout<<bestScore <<" "<< bestStr<< " " <<nowTime<<endl;
}
//探索順をランダムに決めてみる
vint ran = {0, 1, 2, 3};
shuffle(ran.begin(), ran.end(), engine);
for (int a=0;a<4;a++)
{
int k = ran[a];
//コマンドを作って次のマスに
int X = x + dx[k], Y = y + dy[k];
// cout << X <<" "<< Y <<endl;
if (X < 0 || SIZE <= X || Y < 0 || SIZE <= Y)
continue;
nowStr += com[k];
DFS(X, Y);
nowStr.pop_back();
}
//全方向見たら、マスを開放してスコアを減算する
score -= p[x][y];
usedTile[tile] = 0;
return;
}
signed main(){
start = chrono::system_clock::now();
cin>>si>>sj;
for (int i=0;i<SIZE;i++)
for (int j=0;j<SIZE;j++)
cin>>t[i][j];
for (int i=0;i<SIZE;i++)
for (int j=0;j<SIZE;j++)
cin>>p[i][j];
int ret = 0;
string retStr = "";
for (int i=0;i<19;i++)
{
start = chrono::system_clock::now();
bestScore = 0;
score = 0;
bestStr = "";
nowStr = "";
usedTile[2500] = {};
isEnd = 0;
DFS(si, sj);
// cout << bestScore << endl;
if (ret < bestScore)
{
ret = bestScore;
retStr = bestStr;
}
}
cout << retStr << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using st = string;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
const ll MOD = 1000000007;
const ll INF_I = 0x3fffffff;
const ll INF_L = 0x7fffffffffffffff;
const ld PI = 3.141592653589793;
const st Y = "Yes", N = "No";
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Mod(x, mod) \
if (x < 0) \
x = (x + mod * (abs(x) / mod + 1)) % mod; \
else \
x %= mod;
#define ExactMinority(degits) \
cout << fixed << setprecision(degits)
#define UniqueVector(vec) \
sort(vec.begin(), vec.end()); \
vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define all(x) x.begin(), x.end()
#define deb(a) cout << "//" << a << endl;
ll Permutation(ll x, ll y)
{
ll ret = 1;
for (ll i = x - y + 1; i <= x; i++)
{
ret *= i;
}
return ret;
}
ll Combination(ll x, ll y)
{
return Permutation(x, y) / Permutation(y, y);
}
ll Gcd(ll x, ll y)
{
if (x < y)
{
swap(x, y);
}
if (x % y != 0)
{
return Gcd(y, x % y);
}
else
{
return y;
}
}
ll Lcm(ll x, ll y)
{
return x / Gcd(x, y) * y;
}
/////////////////////////////////////////////////////////bebebe
int main()
{
int n, k;
cin >> n >> k;
vi a(n);
vector<vector<int>> t(n, vector<int>(n));
rep(i, n)
{
a[i] = i;
rep(j, n)
{
cin >> t[i][j];
}
}
ll time;
ll count = 0;
do
{
if (a[0] == 0)
{
time = 0;
//cout << "[";
rep(i, n - 1)
{
time += t[a[i]][a[i + 1]];
//cout << t[i][a[i + 1]] << " ";
}
time += t[a[n-1]][0];
//cout << t[n - 1][0] << "]" << endl;
if (time == k)
{
count++;
}
/*rep(i, n)
{
cout << a[i] << " ";
}
cout << endl
<< "//" << time << endl;*/
}
} while (next_permutation(all(a)));
cout << count << endl;
} |
//------------ヘッダーインクルード------------
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//------------型マクロ定義------------
typedef long l;
typedef long long ll;
#define ALL(v) (v).begin(), (v).end()
//------------forマクロ定義------------
//------------main------------
int main(){
long long int N;
cin >> N;
vector<long long int> vec;
for(long long int i = 1; i*i <= N; i++){
if((N % i) == 0LL){
vec.push_back(i);
if(i*i != N)vec.push_back((long long int)(N / i));
}
}
sort(vec.begin(), vec.end());
for(auto v: vec)cout << v << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a) (a).begin(),(a).end()
ll ans = 0;
int ReLu(int x){
if(x >= 0) return x;
else return 0;
}
void Main(){
int x;
cin >> x;
cout << ReLu(x) << endl;
}
int main(){
Main();
/*
東方風神録は神が出てくるので当然神ゲー
*/
return 0;
} |
#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout << fixed << setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
// clang-format on
ll fibrest(ll x0, ll y0, ll n, bool isx) {
ll iter = 0;
vector<ll> fib = {x0, y0};
ll x = x0;
ll y = y0;
ll op = 0;
while (true) {
if (isx) {
x = x + y;
} else {
y = y + x;
}
if (x >= n) break;
op++;
isx = !isx;
}
return op;
}
void op(vector<ll> &ops, ll &x, ll &y, ll t) {
if (t == 1) {
x = x + 1;
}
if (t == 2) {
y = y + 1;
}
if (t == 3) {
x = x + y;
}
if (t == 4) {
y = y + x;
}
ops.push_back(t);
}
void output(vector<ll> &ops) {
ll k = ops.size();
print(ops.size());
for (auto ope : ops) {
print(ope);
}
}
int main() {
ioinit();
ll n;
cin >> n;
// while (fib.back() < n) {
// fib.push_back(fib[fib.size() - 1] + fib[fib.size() - 2]);
// }
// ll m = fib.size();
ll x = 0;
ll y = 0;
vector<ll> ops;
if (n < 120) {
while (x < n) {
op(ops, x, y, 1);
}
output(ops);
return 0;
}
op(ops, x, y, 1);
bool isx = false;
while (true) {
if (x + y > n) {
while (x < n) {
op(ops, x, y, 1);
}
break;
}
ll curop = fibrest(x, y, n, isx);
{
ll k = 0;
while (true) {
ll r = fibrest(x + k + 1, y, n, isx);
if (r != curop) break;
//perr(curop, r);
op(ops, x, y, 1);
k = k + 1;
}
}
if (isx) {
op(ops, x, y, 3);
} else {
op(ops, x, y, 4);
}
isx = !isx;
if (x >= n) break;
}
output(ops);
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
int n; cin >> n;
vector<int> p(n), pos(n), res;
REP(i, n) cin >> p[i], pos[--p[i]] = i;
vector<bool> used(n-1, false);
REP(i, n) {
int now = pos[i];
while (i < now) {
int before = now - 1;
if (used[before]) { cout << -1 << '\n'; return 0; }
used[before] = true;
pos[p[now]] = before;
pos[p[before]] = now;
swap(p[before], p[now]);
res.emplace_back(before);
now--;
}
}
if ((int)res.size() != n-1) cout << -1 << '\n';
else REP(i, n-1) cout << res[i] + 1 << '\n';
return 0;
} |
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) a.begin(),a.end()
using namespace std;
int main() {
int H,W;cin>>H>>W;
vector<vector<bool> > Map(H+10,vector<bool>(W+10,false));//黒ならTRUE
int sy=-1,sx=-1;
for(int h=1;h<=H;h++){
string s;cin>>s;
for(int w=1;w<=W;w++){
Map[h][w]=(s[w-1]=='#');
if(Map[h][w]&&sy==-1){
sy=h;sx=w;
}
}
}
if(sy==-1){
cout<<0<<endl;
return 0;
}
int ny=sy,nx=sx;
int p=0;
int r=2;//下から来た
int i=0;
while(true){
//i++;
r=-r;
bool a1=Map[ny][nx];
bool a2=Map[ny-1][nx];
bool a3=Map[ny][nx-1];
bool a4=Map[ny-1][nx-1];
int k1,k2;
if((a1?1:0)+(a2?1:0)+(a3?1:0)+(a4?1:0)==3){
a1=!a1;a2=!a2;a3=!a3;a4=!a4;
}
if(a1&&!a2&&!a3&&!a4){k1=-2;k2=1;}
if(!a1&&a2&&!a3&&!a4){k1=1;k2=2;}
if(!a1&&!a2&&a3&&!a4){k1=-2;k2=-1;}
if(!a1&&!a2&&!a3&&a4){k1=-1;k2=2;}
if(a1&&a2&&!a3&&!a4){k1=-2;k2=2;}
if(!a1&&!a2&&a3&&a4){k1=-2;k2=2;}
if(!a1&&a2&&!a3&&a4){k1=-1;k2=1;}
if(a1&&!a2&&a3&&!a4){k1=-1;k2=1;}
int n=99;
//cout<<a1<<a2<<a3<<a4<<endl;
//cout<<k1<<" "<<k2<<endl;
if(r==k1)n=k2;
else if(r==k2)n=k1;
if(-n!=r)p++;
if(n==1)nx++;
if(n==-1)nx--;
if(n==2)ny--;
if(n==-2)ny++;
//cout<<p<<" "<<ny<<" "<<nx<<endl;
r=n;
if(ny==sy&&nx==sx)break;
if(i>13)break;
}
cout<<p<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define pb push_back
#define fi first
#define se second
#define ll long long
#define tp top()
#define fr front()
#define vi vector<int>
#define sz size()
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define mem(a, b) memset(a, (b), sizeof(a))
#define clr(a) memset(a, 0, sizeof(a))
#define sqr(x) ( (x) * (x) )
#define all(v) v.begin(), v.end()
typedef pair<int, int> pii;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
void base(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
vector<pair<double, double>> ps;
#define N 120
int par[N];
int fpar(int n){
return par[n] = (par[n] == n)? n : fpar(par[n]);
}
double dist(pair<double, double> p1, pair<double, double> p2){
return hypot(p1.fi-p2.fi, p1.se-p2.se);
}
void solve(){
int n; cin>>n;
rep(i,0,n){
double x,y;cin>>x>>y;
ps.pb({x,y});
par[i]=i;
}
par[n]=n;
par[n+1]=n+1;
priority_queue<pair<double, pii>, vector<pair<double, pii>>, greater<pair<double, pii> > > pq;
rep(i,0,n){
pq.push({100-ps[i].se,{i,n}});
pq.push({ps[i].se-(-100),{i,n+1}});
rep(j,i+1,n){
pq.push({dist(ps[i],ps[j]),{i,j}});
}
}
double ans = 0;
while(pq.sz){
double range = pq.tp.fi;
int x = pq.tp.se.fi;
int y = pq.tp.se.se;
pq.pop();
int p1 = fpar(x);
int p2 = fpar(y);
if (p1 != p2) {
par[p1] = p2;
int pn = fpar(n);
int pm = fpar(n+1);
ans = range;
if (pn == pm) break;
}
}
cout<<fixed<<setprecision(9)<<ans/2.0<<"\n";
}
int main()
{
base();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
|
#include<iostream>
#include<bits/stdc++.h>
#include<climits>
#include<cmath>
#include<set>
using namespace std;
long long binpow(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int main(){
int n;
cin>>n;
if(floor(1.08*n)<206) cout<<"Yay!"<<endl;
else if(floor(1.08*n)>206) cout<<":("<<endl;
else cout<<"so-so"<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(long long int i=(long long int)j;i<(long long int)k;i++)
#define MOD 1000000007
#define pb push_back
#define fi first
#define se second
typedef long long lld;
typedef long ld;
#define inf LLONG_MAX
const lld MAXN = (int)3e5 + 5;
typedef pair<lld, lld> pii;
ld gcd(ld a, ld b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
lld power(lld x, lld y)
{
lld res = 1;
while (y > 0) {
if (y & 1)
res = (res * x) % MOD;
y = y >> 1;
x = (x * x) % MOD;
}
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lld n;
cin>>n;
lld ans=n;
map<lld,lld> m;
rep(i,2,sqrt(n)+1)
{
lld j=i*i;
while(j<=n)
{
m[j]++;
if(m[j]==1)
ans--;
//cout<<j<<endl;
j*=i;
}
}
cout<<ans<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
// #define ll long long
#define f(i,n) for(int i=0;i<n;i++)
#define fi(i,x,n) for(int i=x;i<n;i++)
#define pii pair<int,int>
#define vi vector<int>
#define pb push_back
#define rof(i,a,b) for(int i=(a);i>=(b);i--)
#define S second
#define F first
#define is insert
#define INF 1000000000
#define MOD 1000000007
void solve() {
int n; cin >> n;
vi a(n); f(i, n) cin >> a[i];
vi cnt(100000, 0);
int res = 0;
for (int x : a)
{
int k = x % 200;
res += cnt[k];
cnt[k]++;
}
cout << res;
}
int32_t main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
int t = 1;
//cin >> t;
while (t--)
{
solve();
cout << "\n";
}
} | #include <bits/stdc++.h>
#define M 1000000007
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define d(x) cout << #x << " " << x << "\n";
#define min(x1, x2) (x1 > x2 ? x2 : x1)
#define max(x1, x2) (x1 < x2 ? x2 : x1)
#define min3(x1, x2, x3) (x3 > min(x1, x2) ? min(x2, x1) : x3)
#define max3(x1, x2, x3) (x3 < max(x1, x2) ? max(x1, x2) : x3)
#define ll long long int
#define ul unsigned long long int
#define p pair<ll, ll>
#define ld long double
#define dv(v) \
cerr << #v << " "; \
for (int i = 0; i < (v).size(); i++) \
cerr << v[i] << " "; \
cerr << "\n";
#define inf INT_MAX
#define mp(x, y) make_pair(x, y)
using namespace std;
int main()
{
fio;
ll t, n, x, y, z, k;
string s;
cin >> s;
n=0;
for (int i = 0; i < s.size(); ++i)
{
string a;
for (int j = i; j < min(s.size(), i + 4); ++j)
{
a.push_back(s[j]);
}
if (a == "ZONe")
{
++n;
}
}
cout<<n;
return 0;
} |
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,avx512f,avx512bw,avx512bitalg")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(int x=start;x!=end;x++)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int B,m;
bool bad[25][25];
int ind[1050000];
int ahat[1050000][21];
int bhat[1050000][21];
ll hat[1050000][21];
void conv(){
for (int x=0;x<1048576;x++) {
ahat[x][__builtin_popcount(x)]=ind[x];
bhat[x][__builtin_popcount(x)]=ind[x];
}
rep(bit,0,20){
for (int x=0;x<1048576;x++) if (x&(1<<bit)){
rep(cnt,0,21){
ahat[x][cnt]+=ahat[x^(1<<bit)][cnt];
bhat[x][cnt]+=bhat[x^(1<<bit)][cnt];
}
}
}
for (int x=0;x<1048576;x++){
rep(cnt,0,21){
rep(cnt2,0,cnt+1){
hat[x][cnt]+=(ll)ahat[x][cnt2]*bhat[x][cnt-cnt2];
}
}
}
rep(bit,0,20){
for (int x=0;x<1048576;x++) if (x&(1<<bit)){
rep(cnt,0,21){
hat[x][cnt]-=hat[x^(1<<bit)][cnt];
}
}
}
memset(ahat,0,sizeof(ahat));
memset(bhat,0,sizeof(bhat));
for (int x=0;x<1048576;x++){
ahat[x][__builtin_popcount(x)]=ind[x];
bhat[x][__builtin_popcount(x)]=hat[x][__builtin_popcount(x)];
}
memset(hat,0,sizeof(hat));
rep(bit,0,20){
for (int x=0;x<1048576;x++) if (x&(1<<bit)){
rep(cnt,0,21){
ahat[x][cnt]+=ahat[x^(1<<bit)][cnt];
bhat[x][cnt]+=bhat[x^(1<<bit)][cnt];
}
}
}
for (int x=0;x<1048576;x++){
rep(cnt,0,21){
rep(cnt2,0,cnt+1){
hat[x][cnt]+=(ll)ahat[x][cnt2]*bhat[x][cnt-cnt2];
}
}
}
rep(bit,0,20){
for (int x=0;x<1048576;x++) if (x&(1<<bit)){
rep(cnt,0,21){
hat[x][cnt]-=hat[x^(1<<bit)][cnt];
}
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>B>>m;
int a,b;
rep(x,0,m){
cin>>a>>b;
a--,b--;
bad[a][b]=bad[b][a]=true;
}
ind[0]=1;
rep(x,1,1<<B){
int temp=__builtin_ctz(x);
int nx=x^(1<<temp);
ind[x]=ind[nx];
rep(bit,0,B) if (nx&(1<<bit) && bad[temp][bit]){
ind[x]=0;
}
}
conv();
cout<<hat[(1<<B)-1][B]<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T, class U>
void chmin(T& t, const U& u) {
if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
if (t < u) t = u;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "{";
rep(i, v.size()) {
if (i) os << ",";
os << v[i];
}
os << "}";
return os;
}
#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
const ll INF = TEN(18);
int main() {
int N, M;
cin >> N >> M;
V<ll> w(N);
rep(i, N) cin >> w[i];
V<int> pm(N);
iota(ALL(pm), 0);
V<pii> br(M);
ll mx = 0;
for (auto v : w) chmax(mx, v);
rep(i, M) cin >> br[i].se >> br[i].fi;
rep(i, M) if (mx > br[i].fi) {
puts("-1");
return 0;
}
map<int, int> D;
int now = 0;
V<int> xs;
rep(i, 1 << N) {
int s = 0;
rep(j, N) if (i >> j & 1) { s += w[j]; }
xs.pb(s);
}
sort(ALL(xs));
sort(ALL(br));
int md = 0;
for (auto v : xs) {
while (now < M && br[now].fi < v) {
chmax(md, br[now++].se);
}
D[v] = md;
}
ll ans = INF;
do {
V<ll> a(N);
rep(i, N) a[i] = w[pm[i]];
V<ll> dp(N);
rep(i, N) {
int s = 0;
if (i) chmax(dp[i], dp[i - 1]);
for (int j = i; j < N; ++j) {
s += a[j];
if (j > i) chmax(dp[j], dp[i] + D[s]);
}
}
chmin(ans, dp.back());
} while (next_permutation(ALL(pm)));
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define init(a,i) for(int k=0;k<(i);k++)(a)[k]=0
#define in(a,i) for(int k=0;k<(i);k++)cin>>(a)[k]
#define all(a) (a).begin(),(a).end()
#define mod 998244353
#define inf 2147483647
#define range(x,a,b) (a)<=x&&x<=(b)
int main(){
int n;
string s;
cin>>n>>s;
int pair1=0,pair2=0,count=0;
rep(i,n){
pair1=0;
pair2=0;
for(int j=i;j<n;j++){
switch(s[j]){
case 'A':
pair1++;
break;
case 'T':
pair1--;
break;
case 'C':
pair2++;
break;
case 'G':
pair2--;
break;
}
if(pair1==0&&pair2==0)count++;
}
}
cout<<count;
return 0;
} | #include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T>
bool chmax(T &val, T nv) { return val < nv ? (val = nv, true) : false; }
template<class T>
bool chmin(T &val, T nv) { return nv < val ? (val = nv, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T1, class ...T2>
void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 2010;
char w[MAX_N][MAX_N];
bool used[MAX_N];
int dis[MAX_N][MAX_N];
int n, m;
vector<pair<int,int>> ap[ MAX_N ];
int solve() {
memset(dis, -1, sizeof(dis));
queue<pair<int,int>> q;
int x, y;
for (int i = 1;i <= n;++i)
for (int j = 1;j <= m;++j) {
if (w[i][j] == 'S')
q.emplace(i, j), x = i, y = j;
if (w[i][j] <= 'z' && w[i][j] >= 'a')
ap[ w[i][j] ].pb(i, j);
}
dis[x][y] = 0;
while (q.size()) {
auto [x, y] = q.front(); q.pop();
//DE(x, y, dis[x][y]);
int d = dis[x][y] + 1;
auto update = [&](int x, int y) {
if (x < 1 || y < 1 || x > n || y > m || dis[x][y] != -1 || w[x][y] == '#') return;
dis[x][y] = d;
q.emplace(x, y);
};
update(x+1, y), update(x-1, y), update(x, y-1), update(x, y+1);
if (w[x][y] <= 'z' && w[x][y] >= 'a' && !used[ w[x][y] ]) {
used[ w[x][y] ] = true;
for (auto [a, b] : ap[w[x][y]])
update(a, b);
}
}
for (int i = 1;i <= n;++i)
for (int j = 1;j <= m;++j)
if (w[i][j] == 'G')
return dis[i][j];
assert(false);
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for (int i = 1;i <= n;++i)
cin >> w[i] + 1;
cout << solve() << '\n';
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll fp[3][250005];
struct node
{
ll op, po;
}p[250005];
struct qu
{
ll A, B, id;
}q[250005];
int cmp(qu z1, qu z2)
{
return z1.A<z2.A;
}
struct nn
{
ll rx, ry;
}res[250005];
int main()
{
int n;
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
scanf("%lld %lld", &fp[1][i], &fp[2][i]);
}
ll m;
scanf("%lld", &m);
for(int i=1; i<=m; i++)
{
scanf("%lld", &p[i].op);
if(p[i].op>2)
{
scanf("%lld", &p[i].po);
}
}
ll Q;
scanf("%lld", &Q);
for(int i=1; i<=Q; i++)
{
scanf("%lld %lld", &q[i].A, &q[i].B);
q[i].id=i;
}
sort(q+1,q+1+Q, cmp);
ll ra[3];
ra[1]=1LL;ra[2]=2LL;
ll f1[3], f2[3], cnt=1;
f1[1]=f1[2]=0LL;
f2[1]=f2[2]=1LL;
for(int i=1; i<=Q; i++)
{
for(int j=cnt; j<=q[i].A; j++)
{
if(p[j].op==2)
{
swap(ra[1], ra[2]);
swap(f1[1], f1[2]);
swap(f2[1], f2[2]);
f1[1]*=-1;
f2[1]*=-1;
}
else if(p[j].op==1)
{
swap(ra[1], ra[2]);
swap(f1[1], f1[2]);
swap(f2[1], f2[2]);
f1[2]*=-1;
f2[2]*=-1;
}
else if(p[j].op==4)
{
f1[2]=2*p[j].po-f1[2];
f2[2]*=-1;
}
else
{
f1[1]=2*p[j].po-f1[1];
f2[1]*=-1;
}
}
//printf("%d %lld %lld %lld %lld\n",i, f1[1], f1[2], f2[1], f2[2]);
res[q[i].id].rx=f1[1]+f2[1]*fp[ra[1]][q[i].B];
res[q[i].id].ry=f1[2]+f2[2]*fp[ra[2]][q[i].B];
cnt=q[i].A+1;
}
for(int i=1; i<=Q; i++)
{
printf("%lld %lld\n", res[i].rx, res[i].ry);
}
}
| #include <bits/stdc++.h>
#define endl '\n'
#define fr first
#define sc second
#define all(v) v.begin(),v.end()
#define unq(v) sort( all(v) ); v.erase( unique( all(v) ), v.end() )
#define bout(x) cout << bitset<sizeof(x)*8>(x) << endl
#define mkp(a,b) make_pair(a,b)
#define gcd(a,b) __gcd(a,b)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using cpl = complex<ld>;
const ld pi = 3.14159265358979323846264338327950288;
const ld tau = 2 * pi;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, ddy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int nx[8] = {1, 2, 2, 1, -1, -2, -2, -1}, ny[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
//#define DEBUG
typedef pair<int, bool> pib;
int arr[400020];
pii srt[400020];
bool add[400020];
stack<pib> st;
vector<int> v1, v2;
void Main(){
int n; cin >> n;
for (int i = 1; i <= 2*n; i++){
cin >> arr[i];
srt[i] = {i, arr[i]};
}
sort(srt+1, srt+2*n+1, [](pii a, pii b){
return a.sc > b.sc;
});
for (int i = 1; i <= n; i++){
add[ srt[i].fr ] = 1;
//cout << srt[i].fr << ' ';
}
for (int i = 1; i <= 2*n; i++){
if (st.empty()){ st.push({i, add[i]}); }
else{
if (st.top().sc != add[i]){
v1.push_back(st.top().fr); v2.push_back(i);
st.pop();
}
else{
st.push({i, add[i]});
}
}
}
sort(all(v1)); sort(all(v2));
int p1 = 0, p2 = 0;
string s = "";
for (int i = 1; i <= 2*n; i++){
if (p1 == n){ s += ')'; p2 += 1; }
else if (p2 == n){ s += '('; p1 += 1; }
else if (v1[p1] == i){ s += '('; p1 += 1; }
else{ s += ')'; p2 += 1; }
}
cout << s;
}
int main(){
#ifndef DEBUG
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#endif
cout.setf(ios::fixed);
cout.precision(0);
Main();
}
|
//
// Created by zhujunchao on 2021/4/18.
//
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
int a, b;
scanf("%d%d", &a, &b);
for(int i = 1; i < min(a, b); i++){
printf("%d %d ",i, -i);
}
int u = 0;
if(a > b){
for(int i = b; i <= a; i++){
printf("%d ", i);
u += i;
}
printf("%d ", -u);
}
else{
for(int i = a; i <= b; i++){
printf("%d ", -i);
u += i;
}
printf("%d ", u);
}
printf("\n");
return 0;
} | /*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip> //setprecision
#include <map> // map
#include <unordered_map> //unordered_map
#include <queue> // queue, priority_queue
#include <set> // set,multiset
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#include <numeric> //accumulate,,,
#include <sstream>
#define endl "\n";
using namespace std;
using PLL = pair<long long,long long>;
typedef tuple<int,int,int> TUP;
using P = pair<int,int>;
const long long INF = 4000000000000000001;
const int inf = 1001001001;
const long long MOD = 1000000007;
//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M, long long mod) {
if (M == 0) return 1;
long long res = mod_pow((N * N) % mod, M / 2,mod);
//When end-of-a bit is 1, times simple N.
if (M & 1) res = (res * N) % mod;
return res %= mod;
}
long long gcd(long long a, long long b) {
if (b == 0) return a; else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
return a / gcd(a, b) * b ;
}
long long get_mod(long long res){
if(res < 0) res += MOD;
return res % MOD;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
long long N,M;cin >> N >> M;
cout << mod_pow(10,N,M*M)/M << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t, n;
cin >> t >> n;
int l = 1, r = 1e12;
int ans = 0;
while (l <= r) {
int mid = (l + r) / 2;
int price = mid * (100 + t) / 100;
int count = price - mid;
// cout << mid << ' ' << count << endl;
if (count >= n)
ans = mid, r = mid - 1;
else
l = mid + 1;
}
int price = ans * (100 + t) / 100;
int count = price - ans;
cout << price - 1 - (count - n) << '\n';
return 0;
}
| #include "bits/stdc++.h"
#include <chrono>
#include <random>
#define lli long long int
using namespace std;
#define mod 1000000007
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define INF 1000000000
#define common cout << "Case #" << w+1 << ": "
#define maxn 10010
void setIO(string name) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
template< typename T>
void PVecPrint(vector<T>&v)
{
for(int i=0;i<(int)v.size();i++)
cout << v[i].first << "," << v[i].second << ' ';
cout << '\n';
}
template<class T>
void VecPrint(vector<T>&v)
{
for(int i=0;i<v.size();i++)
cout << v[i] << ' ';
cout << '\n';
}
/*-------------------------------------------------------------------Code-----------------------------------------------------------------*/
int main()
{
int t,n;
cin >> t >> n;
set<int>s;
int p=0,cnt=0,req;
for(int i=1;i<1000;i++)
{
int r=(i*t)%100,q=(i*t)/100;
if(s.find(r)!=s.end())
{
req=i-1;
break;
}
s.insert(r);
if(p!=q)
{
++cnt;
p=q;
}
}
// cout << cnt << ' ' << req << '\n';
if(cnt>=n)
{
// cout << "enter\n";
int r=n,p=0;
for(int i=1;i<1000;i++)
{
int q=(i*t)/100;
if(p!=q)
{
if(r==1)
{
cout << i+q-1 << '\n';
break;
}
--r;
p=q;
}
}
}
else
{
int q=n/cnt,r=n%cnt;
if(r==0)
{
q-=1;
r=cnt;
}
lli ev=req*1LL*q;
// cout << ev << ' ' << r << '\n';
lli prev=(ev*t)/100;
for(lli i=ev+1;i<ev+1000;i++)
{
lli val=i+(i*t)/100,a=(i*t)/100;
if(a!=prev)
{
if(r==1)
{
cout << val-1 << '\n';
break;
}
// cout << val-1 << '\n';
--r;
prev=a;
}
}
}
} |
#include <bits/stdc++.h>
using namespace std;
template <int MOD_> struct modnum {
static constexpr int MOD = MOD_;
static_assert(MOD_ > 0, "MOD must be positive");
private:
using ll = long long;
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modnum() : v(0) {}
modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }
friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
modnum inv() const {
modnum res;
res.v = minv(v, MOD);
return res;
}
friend modnum inv(const modnum& m) { return m.inv(); }
modnum neg() const {
modnum res;
res.v = v ? MOD-v : 0;
return res;
}
friend modnum neg(const modnum& m) { return m.neg(); }
modnum operator- () const {
return neg();
}
modnum operator+ () const {
return modnum(*this);
}
modnum& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modnum& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modnum& operator += (const modnum& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modnum& operator -= (const modnum& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modnum& operator *= (const modnum& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modnum& operator /= (const modnum& o) {
return *this *= o.inv();
}
friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
using num = modnum<998244353>;
vector<num> fact, ifact;
void init(){
int N = 1100000;
fact = {1};
for(int i = 1; i < N; i++) fact.push_back(i * fact[i-1]);
ifact.resize(N);
ifact.back() = 1 / fact.back();
for(int i = N - 1; i > 0; i--) ifact[i-1] = i * ifact[i];
}
num ncr(int n, int k){
if(k < 0 || k > n) return 0;
return fact[n] * ifact[k] * ifact[n-k];
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
init();
int n, m;
cin >> n >> m;
num ans = pow(num(m), n) * n;
vector<num> r(m, 1);
for(int len = 1; len < n; len++){
num mult = pow(num(m), n-1-len) * (n-len);
num v = 0;
for(int a = 0; a < m; a++){
v += r[a];
r[a] *= a;
}
ans -= mult * v;
}
cout << ans << '\n';
}
| #include<bits/stdc++.h>
using namespace std;
const int N=5005,mod=998244353;
int n,m,ans,f[N][2],g[N][2];
inline int add(int a,int b){
a+=b;
return a<mod?a:a-mod;
}
inline int dec(int a,int b){
a-=b;
return a<0?a+mod:a;
}
inline int mul(int a,int b){
return 1LL*a*b%mod;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
memset(f,0,sizeof(f));
memset(g,0,sizeof(f));
f[0][0]=1;
g[0][0]=0;
for(int j=1;j<=n;j++){
f[j][0]=add(mul(f[j-1][0],m-1),mul(f[j-1][1],i-1));
g[j][0]=add(mul(g[j-1][0],m-1),mul(g[j-1][1],i-1));
f[j][1]=add(f[j-1][0],mul(f[j-1][1],m-i+1));
if(j==n){
j++;
j--;
}
g[j][1]=add(g[j-1][0],f[j-1][0]);
g[j][1]=add(g[j][1],mul(g[j-1][1],m-i+1));
}
ans=add(ans,add(g[n][0],g[n][1]));
}
printf("%d\n",ans);
return 0;
} |
Subsets and Splits