code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
#define trav(a, x) for(auto& a : x)
#define vi vector<int>
#define fo(i, k, n) for(int i = k; i < n; ++i)
typedef long long ll;
void setIO(string name = "") {
ios_base::sync_with_stdio(0); cin.tie(0);
if(name.size()){
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
const int mod = 1e9 + 7;
int main()
{
setIO();
int n;
cin >> n;
map<int, ll> count;
ll res = 0;
for(int i = 0; i < n; ++i) {
int k;
cin >> k;
res += (i - count[k]);
count[k]++;
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N;
cin >> N;
vector<int> A(N);
for(int i = 0; i < N; ++i){
cin >> A[i];
}
const int MIN_ELEM = *min_element(A.begin(), A.end());
unordered_map<int, int> memo;
memo[MIN_ELEM] = MIN_ELEM;
for(int i = 0; i < N; ++i){
for(int d = 1; d * d <= A[i]; ++d){
if(A[i] % d == 0){
for(int divisor: set<int>{d, A[i] / d}){
memo[divisor] = (memo.count(divisor) ? __gcd(memo[divisor], A[i]) : A[i]);
}
}
}
}
int answer = 0;
for(const pair<int, int>& P: memo){
answer += (P.first == P.second && P.first <= MIN_ELEM);
}
cout << answer;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define w(x) int x; cin>>x; while(x--)
#define endl ("\n")
#define f(i, a, n) for(int i=a; i<n; i++)
#define cc(r) cout<<r<<" "
#define ce(r) cout<<r<<endl
void inout()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
int nCrModpDP(int n, int r, int p)
{
int C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
int nCrModpLucas(int n, int r, int p)
{
if (r == 0)
return 1;
int ni = n % p, ri = r % p;
return (nCrModpLucas(n / p, r / p, p) * nCrModpDP(ni, ri, p)) % p;
}
int32_t main()
{
inout();
int n;
cin >> n;
int r[n], a[n], ans = 0;
f(i, 0, n) {
r[i] = nCrModpLucas(n - 1, i, 3);
}
char k;
f(i, 0, n) {
cin >> k;
if (k == 'B')
a[i] = 0;
else if (k == 'W')
a[i] = 1;
else
a[i] = 2;
}
f(i, 0, n) {
ans = (ans + (a[i] * r[i]) % 3) % 3;
}
if ((n & 1) == 0) {
ans = (-ans + 3) % 3;
}
if (ans == 0)
ce('B');
else if (ans == 1)
ce('W');
else
ce('R');
}
| #include <bits/stdc++.h>
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define repr(i,a,b) for(int i = a ; i >= b; --i)
#define NAME "NAME"
#define pb push_back
#define mp make_pair
#define EL cout << '\n'
#define cqtshadow int main
#define fillchar(a,x) memset(a, x, sizeof (a))
#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
typedef int64_t ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef pair<ll, ll> iil;
const ll oo = 1e18;
const int inf = 1e9;
const int mod = 3;
const int N = 4e5 + 7;
const int M = 1e6 + 7;
void file() {freopen ( NAME ".inp", "r", stdin ); freopen ( NAME ".out", "w", stdout ); freopen ( NAME ".err", "w", stderr);}
int n, c[N], C_[5][5];
string s;
void cal() {
rep(i, 0, 3)
rep(j, 0, i)
if(i == 0 || j == 0) C_[i][j] = 1;
else C_[i][j] = (C_[i - 1][j]%mod + C_[i - 1][j - 1]%mod)%mod;
}
int C(int k, int n) {
if(k < mod && n < mod) return C_[n][k];
return (C(k/mod, n/mod)%mod * C_[n%mod][k%mod])%mod;
}
cqtshadow () {
faster
cin >> n >> s;
rep(i, 1, n) {
if(s[i - 1] == 'W') c[i] = 0;
if(s[i - 1] == 'R') c[i] = 1;
if(s[i - 1] == 'B') c[i] = 2;
}
cal();
int res = 0;
rep(i, 1, n) {
res = (res + (c[i] * C(i - 1, n - 1)%mod)%mod)%mod;
}
if(n%2 == 0) res *= -1;
res = (res%mod + mod*1000000)%mod;
if(res == 0) cout << "W";
if(res == 1) cout << 'R';
if(res == 2) cout << "B";
}
|
#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;
/*---------------------DEBUGGING--------------------------------------------*/
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 pb push_back
#define ll long long
#define pii pair<int,int>
#define pcc pair<char,char>
#define F first
#define S second
#define int long long
#define pi 3.141592653589793238462643383279502
#define M 1000000007 //998244353
#define rep(i,a,n) for(int i=a;i<n;i++)
#define INF 1000000000000005
#define N 500005
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define endl "\n"
bool comp(pii &p1,pii &p2)
{
if(p1.F<p2.F)return false;
else if(p1.F == p2.F)
{
return p1.F<p2.F;
}
else return true;
}
void solve()
{
int n,m,q;
cin>>n>>m>>q;
vector<pii>v(n);
rep(i,0,n)
{
cin>>v[i].S>>v[i].F;
}
sort(all(v),comp);
vi bags(m);
rep(i,0,m)cin>>bags[i];
while(q--)
{
int l,r;
cin>>l>>r;
l--,r--;
multiset<int> a;
for(int i=0;i<l;i++)
{
a.insert(bags[i]);
}
for(int i=r+1;i<m;i++)
{
a.insert(bags[i]);
}
int ans=0;
for(auto x:v)
{
int val = x.S;
auto it = a.lower_bound(val);
if(it == a.end())continue;
a.erase(it);ans+=x.F;
}
cout<<ans<<endl;
}
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int tests;
//cin>>tests;
tests=1;
for(int i=0;i<tests;i++)
{
solve();
}
}
| #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007
int main(){
int n,m,Q;
cin >> n >> m >> Q;
vector<P> v(n);
rep(i,n)cin >> v[i].first >> v[i].second;
sort(ALL(v));
vector<P> x(m);
rep(i,m)cin >> x[i].first,x[i].second = i;
sort(ALL(x));
while(Q--){
int l,r;cin >> l >> r;
l--;r--;
vector<ll> dp(m+1);
rep(i,n){
vector<ll> DP(m+1);
for(int j = 1;j <= m;j++){
if(l <= x[j-1].second && x[j-1].second <= r)continue;
DP[j] = max(DP[j],dp[j]);
if(v[i].first > x[j-1].first)continue;
rep(k,j){
DP[j] = max(DP[j],dp[k] + v[i].second);
}
}
swap(DP,dp);
}
cout << *max_element(ALL(dp)) << "\n";
}
return 0;
} |
#include <iostream>
#include <stdio.h>
#include <vector>
#include <map>
#include <stack>
#include <cstring>
#include <utility>
#include <iostream>
#include <iomanip>
#include <list>
#include <queue>
#include <algorithm>
#include <cmath>
#include <set>
#include <unordered_set>
#include <bitset>
#define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define SUM(v) accumulate(ALL(v),0ll)
using ll = long long;
const ll INF=1000000;
using namespace std;
const ll mod=1000000007;
int main()
{
ll n;
cin>>n;
ll x=1000;
ll ans=0;
while(true)
{
if(n<x)
{
break;
}
ans+=n-x+1;
x*=1000;
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
long long gcd_vec(vector<long long> const &A) { // N個の要素に対する最大公約数
int size = (int)A.size();
long long ret = A[0];
for (int i = 1; i < size; i++) {
ret = gcd(ret, A[i]);
}
return ret;
}
long long lcm2(long long a, long long b) {
long long d = gcd(a, b);
return a / d * b;
}
long long lcm(const vector<long long> &vec) {
long long l = vec[0];
for (int i = 0; i < vec.size() - 1; i++) {
l = lcm2(l, vec[i + 1]);
}
return l;
}
long long int mypow(long long int a,long long int b){
long long int i=0,ans;
for(i=0;i<b;i++)
{
if(i==0)
{
ans=a;
}
else
{
ans*=a;
}
}
return ans;
}
struct UnionFind{
int n;
vector<int> par; // 親の番号
vector<int> siz; // サイズを表している。
// 初期化する。parは自身を指す(iotaは 0 ~ nまでの数字を順に入れていく)
// size(siz) は最初はすべて1
UnionFind(int n) : n(n){
par.resize(n);
iota(par.begin(),par.end(),0);
siz.assign(n,1);
}
// 頂点の親を求める。
// 再帰関数を使っており、経路圧縮もしている。
int root(int x){
// ここは親の処理。
if (par[x] == x){
return x;
}
//経路圧縮をしながら値を返す。
// par[x] = root(par[x]);
// return par[x];
// と同じ意味。
return par[x] = root(par[x]);
}
// unite :: a と bを同じグループに入れる。
// もし初めから同じなら何もしない。
void unite(int a,int b){
int ra = root(a);
int rb = root(b);
if(ra == rb){
return;
}
// サイズの大きい方にサイズの小さいほうを合成するので、swapしている。
if (siz[ra] < siz[rb]){
swap(ra,rb);
}
// サイズが大きい方に小さいのを合体
// 小さいほうの親は大きい方の親になる。
siz[ra] += siz[rb];
par[rb] = ra;
}
// 同じグループかどうか判定するには、親が一緒かどうかをみたらよい
bool same(int x,int y){
return root(x) == root(y);
}
// 頂点が含まれるグループの頂点を求める。
int get_size(int x){
return siz[root(x)];
}
};
long long int facctorialMethod(int k){
long long int sum = 1;
for (int i = 1; i <= k; ++i)
{
sum *= i;
}
return sum;
}
#define PI 3.14159265359
int main()
{
[[maybe_unused]]long long int i=0,j=0,a,b,w,g,c=0,c1=0,c2,c3,c4,n;
[[maybe_unused]]double qw;
string s,s1;
cin >> n;
c+=1000000-1000;
c1=c;
c+=2*(1000000000-1000000);
c2=c;
c+=3*(1000000000000-1000000000);
c3=c;
c+=4*(1000000000000000-1000000000000);
c4=c;
if(n<1000)
{
cout << "0";
}
else if(n<1000000)
{
cout << n-999;
}
else if(n<1000000000)
{
cout << c1+(n-999999)*2;
}
else if(n<1000000000000)
{
cout << c2+(n-999999999)*3;
}
else if(n<1000000000000000)
{
cout << c3+(n-999999999999)*4;
}
else
{
cout << c3+(n-999999999999-1)*4+5;
}
return 0;
} |
#include "bits/stdc++.h"
#include <chrono>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>
#define INF 1000000007
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i < b; i++)
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,fma,mmx,avx,tune=native")
using namespace __gnu_pbds;
using namespace std;
typedef tree< int,null_type, less<int> ,rb_tree_tag,tree_order_statistics_node_update > indexed_set;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef unordered_map<int,int> umi ;
typedef unordered_set<int> usi ;
typedef pair<int,int> pi;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
bool ldequal(ld a ,ld b){
return abs(a-b) < 1e-9 ;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(permutation.begin(), permutation.end(), rng); for permute
// uniform_int_distribution<int>(0, i)(rng) for rand
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
const int N =2e5+1 ;
ll a[N] ,b[N] ;
indexed_set s1 ;
set<pair<ll,ll>> s2 ;
bool tests=false ;
void gen(){} ;
void solve(){
int n ;
cin>>n ;
REP(i,0,n){
cin>>a[i] ;
s1.insert(i) ;
s2.insert({a[i]+i,i}) ;
}
REP(i,0,n) cin>>b[i] ;
ll res=0 ;
REP(i,0,n){
ll val =b[i]+i ;
auto l = s2.lower_bound({val,0}) ;
if(l==s2.end()){
cout<<-1 ;
return ;
}
//pair<ll,ll> temp = MP((*l).S ,(*l).F) ;
auto r =s1.order_of_key((*l).S) ;
debug(val,*l ,r ) ;
res+=r;
s1.erase((*l).S) ;
s2.erase(l) ;
}
cout<<res ;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
gen() ;
long long test =1 ;
if(tests) cin>>test ;
REP(i,1,test+1){
debug(i) ;
//cout<<"Case #"<<i<<": " ;
solve() ;
cout<<"\n" ;
}
return 0 ;
} | #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
#define rep(i, s, n) for (int i = (int)s; i < (int)n; i++)
#define repr(i, n, s) for (int i = (int)n; (int)s < i; i--)
#define len(a) (int)a.size()
#define all(a) a.begin(), a.end()
/* 1次元ベクトルをスペースでつなぎ、改行をつけて出力 vi, vl, list<int>, list<ll>に対応 */
template<class T> void print_elements(T a) {string str = ""; for (auto itr = a.begin(); itr != a.end(); ++itr) \
{str += to_string(*itr) + ' ';} str.pop_back(); cout << str << endl;}
const int INF32 = 1 << 30;
const long long INF64 = 1LL << 60;
const long long MOD = 1000000009;
const double PI = acos(-1);
int main() {
ll N;
cin >> N;
set<ll> ans;
for (ll i = 1; i <= (ll)sqrt(N); i++) {
if (N % i == 0) {
ans.insert(i);
ans.insert(N / i);
}
}
for (auto x: ans) {
cout << x << '\n';
}
}
|
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
int main(){
double n, D, H;
cin >> n >> D >> H;
vector<double> d(n), h(n);
for(int i = 0; i < n; i++) cin >> d[i] >> h[i];
vector<double> x(n);
double ans = 0.0;
for(int i = 0; i < n; i++){
x[i] = H - D * (H - h[i]) / (D - d[i]);
ans = max(ans, x[i]);
}
if(ans == 0) cout << "0.0" << endl;
else cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
const ll MAX = 1001001;
const ll MOD = 1000000007;
const double pi = 2.0 * asin(1.0);
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
int main()
{
double n, d, h;
cin >> n >> d >> h;
vector<double> a(n), b(n);
rep(i, n)
{
cin >> a[i] >> b[i];
}
double ans = 0;
rep(i, n)
{
double k = h - d / (d - a[i]) * (h - b[i]);
ans = max(ans, k);
}
cout << fixed << setprecision(10) << ans << endl;
} |
//@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>
#define li long int
#define lli long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define si size()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define lb lower_bound
#define ub upper_bound
#define BS binary_search
#define pll pair < lli, lli >
#define pii pair < int, int>
#define vi vector < int >
#define vvi vector < vector < int > >
#define vlli vector < lli >
#define all(x) x.begin(), x.end()
#define vvlli vector < vector < lli > >
#define vp vector < pll >
#define vvp vector< vector< pll> >
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<lli , null_type, less<lli>, rb_tree_tag,tree_order_statistics_node_update>
#define fill(a,n) for(lli iiii=0;iiii<n;iiii++) cin>>a[iiii];
#define mpii map<int, int>
# define precision(x) cout << fixed << setprecision(x)
#define REP(i, a, b) for (int i=a; i<=b; i++)
using namespace __gnu_pbds;
lli mod=1e9+7;
using namespace std;
int main()
{
fastio;
string s;
cin>>s;
int i,n,f=0;
for(i=0;i<s.length();i++)
{
if(i%2==0)
{
if(s[i]>='a' && s[i]<='z') continue;
else {f=1; break;}
}
else
{
if(s[i]>='A' && s[i]<='Z') continue;
else {f=1; break;}
}
}
if(f==0) cout<<"Yes\n";
else cout<<"No\n";
} |
//GOD PLEASE HELP ME ,GOD PLEASE HELP ME , GOD PLEASE HELP ME
//Name Rishiraj Kalita
// sc id : 1912041
#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y <<","<< #z << "=" << z << endl
#define debv(v) for(auto x:v) cout<<x<<" "
#define debg(g,n) for(int i=1;i<=n;i++){cout<<i<<"--->";for(auto x:g[i]){cout<<x<<" ";}cout<<"\n";}
#define clrg(g,n) for(int i=0;i<=n;i++) g[i].clear();
#define pb push_back
#define mp make_pair
#define MinHeap priority_queue <ll, vector<ll>, greater<ll> >
#define e endl
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define inf 9000000000000000000
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0,lim-1);
return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi g[N];
int a[N];
void solve() {
ll i, j, n,k,data,m,f=1;
ll a,b,c,d;
cin>>a>>b>>c>>d;
if(d*c-b<=0)
{
cout<<"-1\n";
return;
}
data=d*c-b;
if(a%(data)==0)
{
cout<<a/data;
}
else{
cout<<a/data+1;
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
//cin >> t;
while(t--) {
solve();
}
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m){
int i, u, v;
while(m--){
cin>>u>>v;
// u--, v--;
g[u].pb(v);
g[v].pb(u);
}
}
void dfs(int u, int par){
for(int v:g[u]){
if (v == par) continue;
dfs(v, u);
}
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve(){
ll a,b,c,d;
cin>>a>>b>>c>>d;
ll temp=(a);
ll temp1=(d*c-b);
if(temp1<=0)
cout<<-1<<endl;
else{
if(temp%temp1==0)
cout<<temp/temp1<<endl;
else
cout<<temp/temp1+1<<endl;
}
}
int main(){
ll t=1;
//cin>>t;
while(t--){
solve();
}
}
|
#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<algorithm>
#include<map>
#include<set>
#include<cstdlib>
#include<cmath>
#include<numeric>
#include<iomanip>
#include<functional>
#include<cstdlib>
#include<queue>
#include<deque>
#include<cassert>
#include <iterator> // std::back_inserter
const double PI = acos(-1);
using namespace std;
using ll =long long;
#define rep(i,n)for(ll i=0;i<(n);i++)
const int mod = 1000000007;
const ll inf = 1e18 + 1;
int ctoi(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
return 0;
}
ll gcd(ll a, ll b) {
if (a % b == 0) {
return b;
}
else {
return gcd(b, a % b);
}
}
ll lcm(ll a, ll b) {
return a * b / gcd(a, b);
};
//fixed << setprecision(2)
///*�R���r�l�[�V����*/
//
///*Union Find*/
//#define MAX_N 200000
//#define MAX_M 200000
//vector<ll>par(MAX_N);
//vector<ll>ran(MAX_N);
//void init(ll N) {
// rep(i, N) {
// par[i] = i;
// ran[i] = 0;
// }
//}
//ll find(ll x) {
// if (par[x] == x) {
// return x;
// }
// else {
// return par[x] = find(par[x]);
// }
//}
////x��y�̑�����W����
//void unite(ll x,ll y) {
// x = find(x);
// y = find(y);
// if (x == y)return;
// if (ran[x] < ran[y]) {
// par[x] = y;
//
// }
// else {
// par[y] = x;
// if (ran[x] == ran[y])ran[x]++;
// }
//}
//bool same(ll x, ll y) {
// return find(x) == find(y);
//}
///*Union Find*
bool isPrime(int x) {
int i;
if (x < 2)return 0;
else if (x == 2) return 1;
if (x % 2 == 0) return 0;
for (i = 3; i * i <= x; i += 2) if (x % i == 0) return 0;
return 1;
}
int digsum(int n) {
int res = 0;
while (n > 0) {
res += n % 10;
n /= 10;
}
return res;
}
ll p(int x, int y) {
ll v = 1;
rep(i, y) {
v *= x;
}
return v;
}
long long MOD = 1e9 + 7;
long long modPow(long long x, long long a) {
if (a == 1) return x;
if (a % 2) return (x * modPow(x, a - 1));
long long t = modPow(x, a / 2);
return (t * t);
}
long long modInv(long long x) {
return modPow(x, MOD - 2);
}
long long modPerm(long long n, long long k) {
long long ret = 1;
for (long long i = 0; i < k; i++) {
ret = (ret * (n - i));
}
return ret;
}
long long modComb(long long n, long long k) {
long long a, b;
a = modPerm(n, k);
b = modPerm(k, k);
return (a * modInv(b));
}
struct memo{
ll time;
ll money;
};
using vv =vector<vector<ll>>;
int N, X, cnt, a[10009];
int main() {
int N;
ll C;
cin >> N >> C;
vector<pair<ll,ll>>a(N), b(N);
vector<pair<ll,ll>>A(2*N);
vector<ll>c(N);
map<ll,ll>m;
rep(i, N) {
cin >> a[i].first >> b[i].first >> c[i];
b[i].first++;
a[i].second = c[i];
b[i].second = -c[i];
}
for (ll i = 0; i < N;i++) {
A[2*i]=a[i];
A[2 * i + 1] = b[i];
}
ll time = 0;
sort(A.begin(), A.end());
sort(a.begin(), a.end());
sort(b.begin(), b.end());
ll money = 0;
ll ans=0;
m[0] = 0;
rep(i,2*N){
if (money <= C) {
ans += (A[i].first-time) * money;
}
else {
ans += (A[i].first - time) * C;
}
time = A[i].first;
money += A[i].second;
}
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int main() {
int n, d;
cin >> n >> d;
map<int, ll> mp;
rep(i, n) {
int a, b, c;
cin >> a >> b >> c;
mp[a] += c;
mp[b + 1] -= c;
}
ll ans = 0;
ll s = 0;
int pre = 0;
for (auto p : mp) {
ans += min<ll>(d, s) * (p.first - pre);
s += p.second;
pre = p.first;
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
typedef long long ll;
using namespace std;
int main()
{
ll H, W;
ll start_x, start_y, goal_x, goal_y;
cin >> H >> W;
vector<vector<char>> map(H, vector<char>(W));
vector<int> dh = {1, 0, -1, 0};
vector<int> dw = {0, 1, 0, -1};
vector<vector<bool>> ready(H, vector<bool>(W, false));
vector<vector<ll>> answer(H, vector<ll>(W, 0));
vector<vector<pair<ll, ll>>> warp(30);
vector<bool> alpha(30, false);
queue<pair<ll, ll>> Q;
rep(i, H)
{
rep(j, W)
{
char input;
cin >> input;
if ('a' <= input && 'z' >= input)
{
warp[input - 'a'].push_back(make_pair(i, j));
}
else if (input == 'S')
{
start_y = i;
start_x = j;
}
else if (input == 'G')
{
goal_y = i;
goal_x = j;
}
map[i][j] = input;
}
}
Q.push({start_y, start_x});
ready[start_y][start_x] = true;
while (!Q.empty())
{
ll x, y;
y = Q.front().first;
x = Q.front().second;
Q.pop();
//ワープ判定
if (map[y][x] <= 'z' && 'a' <= map[y][x])
{
int f = map[y][x] - 'a';
if (!alpha[f])
{
alpha[f] = true;
rep(i, warp[f].size())
{
auto k = warp[f][i];
if (!ready[k.first][k.second])
{
ready[k.first][k.second] = true;
answer[k.first][k.second] = answer[y][x] + 1;
Q.push(k);
}
}
}
}
//幅探索
rep(k, 4)
{
int th, tw;
th = y + dh[k];
tw = x + dw[k];
if (th < 0 || th >= H || tw < 0 || tw >= W)
continue;
if (map[th][tw] == '#' || ready[th][tw])
continue;
ready[th][tw] = true;
answer[th][tw] = answer[y][x] + 1;
Q.push({th, tw});
}
//debug
// rep(i, H)
// {
// rep(j, W)
// {
// cout << answer[i][j] << ',';
// }
// cout << endl;
// }
// cout << endl;
}
//debug
// cout << "answer" << endl;
// rep(i, H)
// {
// rep(j, W)
// {
// cout << answer[i][j] << ',';
// }
// cout << endl;
// }
if (answer[goal_y][goal_x] == 0)
{
cout << -1 << endl;
}
else
{
cout << answer[goal_y][goal_x] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int H, W;
cin >> H >> W;
vector<vector<int> > road(H*W);
vector<vector<int> > port(26);
vector<vector<char> > grid(H, vector<char>(W,'#'));
int S, G;
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
cin >> grid.at(i).at(j);
if (grid.at(i).at(j) == 'S')
S = i*W + j;
if (grid.at(i).at(j) == 'G')
G = i*W + j;
if (grid.at(i).at(j) - 'a' >= 0 && grid.at(i).at(j) - 'a' < 26)
port.at(grid.at(i).at(j) - 'a').push_back(i*W+j);
}
}
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
if (grid.at(i).at(j) == '#') continue;
if (i != 0 && grid.at(i-1).at(j) != '#') road.at(i*W+j).push_back((i-1)*W+j);
if (i != H-1 && grid.at(i+1).at(j) != '#') road.at(i*W+j).push_back((i+1)*W+j);
if (j != 0 && grid.at(i).at(j-1) != '#') road.at(i*W+j).push_back(i*W+j-1);
if (j != W-1 && grid.at(i).at(j+1) != '#') road.at(i*W+j).push_back(i*W+j+1);
}
}
queue<int> que;
vector<vector<int> > city(H, vector<int>(W,-1));
vector<int> port_check(26, 0);
que.push(S);
city.at(S/W).at(S%W) = 0;
int now;
while(!que.empty())
{
now = que.front();
que.pop();
for (auto x : road.at(now))
{
if (city.at(x/W).at(x%W) >= 0) continue;
city.at(x/W).at(x%W) = city.at(now/W).at(now%W) + 1;
que.push(x);
}
if (grid.at(now/W).at(now%W) - 'a' >= 0 && grid.at(now/W).at(now%W) - 'a' < 26)
{
if (port_check.at(grid.at(now/W).at(now%W) - 'a') == 0)
{
for (auto y : port.at(grid.at(now/W).at(now%W) - 'a'))
{
if (city.at(y/W).at(y%W) >= 0) continue;
city.at(y/W).at(y%W) = city.at(now/W).at(now%W) + 1;
que.push(y);
}
port_check.at(grid.at(now/W).at(now%W) - 'a') = 1;
}
}
}
cout << city.at(G/W).at(G%W) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<vector<int>> children(n);
vector<int> par(n);
par[0] = -1;
for (int i = 1; i < n; i++){
int p;
cin >> p;
p--;
par[i] = p;
children[p].push_back(i);
}
auto dfs = [&](auto v, auto dfs) -> array<int, 2>{
vector<array<int, 2>> A;
for (auto &c : children[v]){
A.push_back(dfs(c, dfs));
}
sort(A.begin(), A.end());
array<int, 2> res = {1, 0};
for (const auto &[s, p]: A){
if (s <= 0 and p == 0)
res[0] += s;
}
int b = 0;
for (const auto &[s, p]: A){
if (p == 1){
if (b == 0)
res[0] += s;
else
res[0] -= s;
b ^= 1;
}
}
for (const auto &[s, p]: A){
if (s > 0 and p == 0){
if (b == 0)
res[0] += s;
else
res[0] -= s;
}
}
b ^= 1;
res[1] = b;
return res;
};
auto res = dfs(0, dfs);
cout << (res[0] + n) / 2 << endl;
} | /*input
20
2 8 4 7 5 3 1 2 4 1 2 5 4 3 3 8 1 7 8 2
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("O2","unroll-loops","no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<ld,ld>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=1e18+1;
const int INF=0x3f3f3f3f;
const ll MOD=998244353;
const ld PI=acos(-1);
const ld eps=1e-3;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
inline ll mult(ll a,ll b){
if(a>=MOD) a%=MOD;
if(b>=MOD) b%=MOD;
return (a*b)%MOD;
}
inline ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b>>=1;
}
return res;
}
const int maxn=1e4+5;
const int maxlg=__lg(maxn)+2;
int dp[105][maxn];
ll fac[maxn];
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0);
int n;
cin>>n;
int s=0;
fac[0]=1;
REP1(i,n) fac[i]=mult(fac[i-1],i);
dp[0][0]=1;
REP(i,n){
int w;
cin>>w;
s+=w;
for(int z=n;z>=1;z--){
for(int j=maxn-1;j>=w;j--){
dp[z][j]+=dp[z-1][j-w];
dp[z][j]%=MOD;
}
}
}
if(s%2) cout<<0;
else{
ll ans=0;
REP1(i,n){
ans+=mult(mult(fac[i],fac[n-i]),dp[i][s/2]);
ans%=MOD;
}
cout<<ans;
}
} |
#line 1 "/Users/haruki/kyopro/library/template.cpp"
// code by lynmisakura. wish to be accepted!
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
#define rep(i,N) for(int i = 0;i < N;i++)
#define rng(i,a,b) for(int i = a;i < b;i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
cout << "{\n"; for (auto& vec : arr)cout << " " << vec << ",\n"; cout << "}";
return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
cout << "{" << p.first << "," << p.second << "}" << '\n';
return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
cin >> p.first >> p.second; return in;
}/////////////////////////////////////////////////////////////////////
///////////////// DON'T TOUCH BEFORE THIS LINE //////////////////////
/////////////////////////////////////////////////////////////////////
#line 2 "combined.cpp"
ll INF = (1LL << 55);
ll low = -INF,high = INF;
ll add = 0;
ll val = 0;
bool fix = false;
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
//cout << fixed << setprecision(20);
int n;cin >> n;
rep(i,n){
ll a;cin >> a;
int t;cin >> t;
if(t == 1){
add += a;
}else if(t == 2){
a -= add;
if(fix){
val = max(val,a);
}else{
if(a >= high){
fix = 1;
val = a;
}else{
low = max(low , a);
}
}
}else{
a -= add;
if(fix){
val = min(val,a);
}else{
if(a <= low){
fix = 1;
val = a;
}else{
high = min(high , a);
}
}
}
}
low += add;
high += add;
int q;cin >> q;
rep(_,q){
ll x;cin >> x;
if(fix){
cout << val + add << '\n';
}else{
x += add;
if(x <= low){
cout << low << '\n';
}else if(x >= high){
cout << high << '\n';
}else{
cout << x << '\n';
}
}
}
}
| #include <bits/stdc++.h>
using namespace std;
long long f(vector<long long>& a, vector<long long>& t, const long long& X0){
long long res = X0;
for(int i = 1; i < (int)a.size(); ++i){
if(t[i] == 1){
res += a[i];
}else if(t[i] == 2){
res = max(a[i], res);
}else{
res = min(a[i], res);
}
}
return res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
const long long MIN_ELEM = -1e9;
const long long MAX_ELEM = 1e9;
long long N;
cin >> N;
vector<long long> a(N + 1);
vector<long long> t(N + 1);
for(int i = 1; i <= N; ++i){
cin >> a[i] >> t[i];
}
// X0: identity element for 'f(..)'
// X0 = 0 for '+' operation and X0 = a[1] for 'min' and 'max' operations
const long long X0 = (t[1] == 1 ? 0 : a[1]);
long long fInitialValue = f(a, t, X0);
long long fMinValue = f(a, t, MIN_ELEM);
long long fMaxValue = f(a, t, MAX_ELEM);
// Compute left displacement
long long l = 0;
long long r = 2 * MAX_ELEM + 5;
while(l != r){
long long mid = (l + r) / 2;
if(f(a, t, X0 - mid) == fInitialValue){
l = mid + 1;
}else{
r = mid;
}
}
long long leftDispl = r - 1;
// Compute right displacement
l = 0;
r = 2 * MAX_ELEM + 5;
while(l != r){
long long mid = (l + r) / 2;
if(f(a, t, X0 + mid) == fInitialValue){
l = mid + 1;
}else{
r = mid;
}
}
long long rightDispl = r - 1;
// Answer each query
long long Q;
cin >> Q;
vector<long long> x(Q + 1);
for(int i = 1; i <= Q; ++i){
cin >> x[i];
}
for(int i = 1; i <= Q; ++i){
long long answer = fInitialValue;
if(x[i] < X0 && X0 - x[i] >= leftDispl){
answer = max(fInitialValue - (X0 - x[i]) + leftDispl, fMinValue);
}else if(x[i] > X0 && x[i] - X0 >= rightDispl){
answer = min(fInitialValue + (x[i] - X0) - rightDispl, fMaxValue);
}
cout << answer << "\n";
}
return 0;
} |
#include <bits/stdc++.h>
#define debug_input freopen("stdin.in", "r", stdin)
#define debug_output freopen("stdout.out", "w", stdout)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORALL(i, n) for(int i = 0; i < n; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
#define SORT(a) sort(a.begin(), a.end())
#define REV(a) reverse(a.begin(), a.end())
#define NP(a) next_permutation(a.begin(), a.end())
#define ALL(a) a.begin, a.end()
#define mp make_pair
#define pb push_back
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5;
const int mod = 1e9 + 7;
const ll inf = 1e18;
const double pi = 3.141592653589793;
const int dr[4] = {-1,0,1,0}, dc[4] = {0,1,0,-1};
void solve() {
int a, b;
cin >> a >> b;
cout << a / b << endl;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int N,W;
int re;
cin >> N >> W;
re=N/W;
cout << re << endl;
} |
//@sakshjha
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define JALDI ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define pi 3.14159265358979
const int mod = 1e9 +7;
const int N = 1e6 +5 ;
int a[1005],b[1005];
int dp[1005][1005];
int solve(int x, int y)
{
if(x<0)
return y+1;
if(y<0)
return x+1;
if(dp[x][y]==-1)
{
dp[x][y]=min(solve(x-1,y),solve(x,y-1))+1;
int tmp=0;
if(a[x]!=b[y])
tmp=1;
dp[x][y]=min(dp[x][y],solve(x-1,y-1)+tmp);
}
return dp[x][y];
}
int32_t main()
{
JALDI
int i,j,tc=1;
//cin>>tc;
while(tc--)
{
int n,m; cin>>n>>m;
for(i=0; i<n; i++)
cin>>a[i];
for(i=0; i<m; i++)
cin>>b[i];
memset(dp,-1,sizeof(dp));
cout<<solve(n-1,m-1);
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) cout<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define pw(b,p) pow(b,p) + 0.1
#define endl "\n"
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int32_t 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
char myHeap[200 * 1024 * 1024];
int sz = 0;
void assert_tle(bool q) { while (!q); }
void* operator new ( std::size_t count ) {
sz += count;
assert_tle(sz <= 200 * 1024 * 1024);
return myHeap + sz - count;
}
void operator delete (void *ptr) { }
void fastIO()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
void solve()
{
int n,m;
cin>>n>>m;
int ar[n],br[m];
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
for(int i=0;i<m;i++)
{
cin>>br[i];
}
int memo[n+1][m+1]={0};
for(int i=1;i<=n;i++)
{
memo[i][0]=i;
}
for(int j=1;j<=m;j++)
{
memo[0][j]=j;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int mn=1+min(memo[i-1][j],memo[i][j-1]);
memo[i][j]=mn;
if(ar[i-1]==br[j-1])
{
memo[i][j]=min(memo[i][j],memo[i-1][j-1]);
}
else
{
memo[i][j]=min(memo[i][j],1+memo[i-1][j-1]);
}
}
}
cout<<memo[n][m];
}
int32_t main()
{
fastIO();
//w(t)
{
solve();
cout<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rng(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define per(i, b) gnr(i, 0, b)
#define all(obj) begin(obj), end(obj)
#define allr(obj) rbegin(obj), rend(obj)
#define cinv(a) rep(i, (int)a.size()) cin >> a[i]
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<Pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
const int MOD = 1e9 + 7;
const int INF = 1e9 + 9;
const ll LINF = 1e18;
const long double eps = 1e-10;
const char nl = '\n';
ll power(ll a, ll b) { return b ? power(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1; }
ll nCk(int n, int k)
{
ll x = 1, y = 1;
for (int i = 1; i <= k; ++i)
{
x = x * (n - i + 1) % MOD;
y = y * i % MOD;
}
return x * power(y, MOD - 2) % MOD;
}
struct UF
{
vi d;
UF(int n) : d(n, -1) {}
int root(int x)
{
if (d[x] < 0)
return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y)
{
x = root(x); y = root(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -d[root(x)]; }
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout<<fixed<<setprecision(20);
int n, m; cin >> n >> m;
map<int, vi> mp;
rep(i, m)
{
int x, y; cin >> x >> y;
mp[x].pb(y);
}
set<int> s{n};
for (auto p : mp)
{
vi add;
for (auto y : p.se)
if (s.count(y-1) || s.count(y+1))
add.pb(y);
for (auto y : p.se) s.erase(y);
for (auto y : add) s.insert(y);
}
cout << sz(s) << nl;
}
| #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;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define fi first
#define se second
#define pi 3.141592653589793
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
#define sleep for (int i = 1, a;i < 100000000;i++, a = a * a)
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map;
int main() {
fio;
int n, m;
cin >> n >> m;
map<int, vi> a;
for (int i = 0;i < m;i++) {
int x, y;
cin >> x >> y;
a[x].pb(y);
}
set<int> s;
s.insert(n);
for (auto &p : a) {
vi &b = p.se;
vi rem, add;
for (auto &x : b)
if (s.count(x))
rem.pb(x);
for (auto &x : b)
if (s.count(x - 1) || s.count(x + 1))
add.pb(x);
for (auto &x : rem)
s.erase(x);
for (auto &x : add)
s.insert(x);
}
cout << s.size() << '\n';
}
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>
// AtCoder
// #include <atcoder/all>
// using namespace atcoder;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif
#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b-1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;
constexpr int MOD = 1000000007;
// + - * / ^
int mod(int a)
{
int res = a % MOD;
if(res < 0) {
return res + MOD;
}
return res;
}
int mul_mod(int a, int b)
{
ll res = ((ll)a * (ll)b) % MOD;
return mod((int)res);
}
int pow_mod(int a, int b)
{
ll res = 1;
while (b > 0) {
if(b & 1) {
res = res * (ll)a % (ll)MOD;
}
a = mul_mod(a, a);
b >>= 1;
}
return (int)res;
}
int inv_mod(int a)
{
return pow_mod(a, MOD - 2);
}
int div_mod(int a, int b)
{
return mul_mod(a, inv_mod(b));
}
// ! C
constexpr int FAC_MAX = 3000001;
ll fac[FAC_MAX], finv[FAC_MAX], inv[FAC_MAX];
void com_init()
{
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < FAC_MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll com_mod(int a, int b){
if (a < b) return 0;
if (a < 0 || b < 0) return 0;
return fac[a] * (finv[b] * finv[a - b] % MOD) % MOD;
}
int main (void)
{
cin.tie(0);
ios::sync_with_stdio(false);
ll t; cin >> t;
ll inv2 = inv_mod(2);
rep (_, t) {
ll n, a, b, p;
cin >> n >> a >> b;
if (a + b > n) {
cout << "0\n";
continue;
}
p = n - a - b + 2;
ll x = mul_mod(inv2, mul_mod(p, p - 1)), y = mul_mod(n - a + 1, n - b + 1);
eprintf("%lld %lld %lld\n", p, x, y);
cout << mod(mul_mod(4, mul_mod(x, y) - mul_mod(x, x))) << "\n";
}
/***************************************************/
/* Use "submit code (with ACL v1)" when using ACL! */
/***************************************************/
return 0;
} | #include<iostream>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define int long long
using namespace std;
int c[502][502],a[502],b[502];
signed main(){
ios::sync_with_stdio(false);
int n; cin>>n;
rep(i,1,n)rep(j,1,n)cin>>c[i][j];
bool flag=true;
rep(i,1,n){rep(j,2,n){
if(i==1)a[j]=c[i][j]-c[i][j-1];
else if(a[j]!=c[i][j]-c[i][j-1]){flag=false;break;}
}if(!flag)break;}
if(!flag){cout<<"No\n";return 0;}
rep(j,1,n){rep(i,2,n){
if(j==1)b[i]=c[i][j]-c[i-1][j];
else if(b[i]!=c[i][j]-c[i-1][j]){flag=false;break;}
}if(!flag)break;}
if(!flag){cout<<"No\n";return 0;}
int mna=0,mnb=0;
rep(i,1,n){
a[i]+=a[i-1],b[i]+=b[i-1];
mna=min(mna,a[i]),mnb=min(mnb,b[i]);
}
//cout<<mna<<" "<<mnb<<endl;
if(c[1][1]+mna+mnb<0){cout<<"No\n";return 0;}
int ada=-mna,adb=c[1][1]-ada;
cout<<"Yes\n";
rep(i,1,n)cout<<b[i]+adb<<" ";
cout<<endl;
rep(i,1,n)cout<<a[i]+ada<<" ";
cout<<endl;
return 0;
} |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\n'
#define debug(x) std::cerr << #x << " = " << (x) << endl;
using ll = long long;
using P = std::pair<ll, ll>;
constexpr ll INF = 1ll<<60;
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 S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }
template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }
ll N;
ll X[200010], C[200010];
std::vector<ll> vs[200010];
std::map<ll, ll> dp[2];
void chminMp( std::map<ll, ll> &mp, ll idx, ll x ) {
if( !mp.count(idx) )
mp[idx] = x;
else
mp[idx] = std::min( mp[idx], x );
return;
}
int main() {
std::cin >> N;
rep( i, N ) {
std::cin >> X[i] >> C[i];
--C[i];
vs[C[i]].emplace_back(X[i]);
}
rep( i, N )
std::sort(all(vs[i]));
ll x = 0;
dp[0][0] = 0;
ll p = 0;
auto prv = dp[0], nxt = dp[1];
rep( i, N ) {
if( vs[i].empty() )
continue;
nxt.clear();
itr( it, prv ) {
ll x = it->first;
ll idx = std::lower_bound(all(vs[i]), x)-vs[i].begin();
if( idx && vs[i].size()-idx ) {
ll d1 = llabs(vs[i][0]-x);
ll d2 = llabs(vs[i][vs[i].size()-1]-x);
chminMp(nxt, vs[i][vs[i].size()-1], prv[x]+d1*2+d2);
chminMp(nxt, vs[i][0], prv[x]+d1+d2*2);
} else if( idx ) {
ll d1 = llabs(vs[i][0]-x);
chminMp(nxt, vs[i][0], prv[x]+d1);
} else if( vs[i].size()-idx ) {
ll d2 = llabs(vs[i][vs[i].size()-1]-x);
chminMp(nxt, vs[i][vs[i].size()-1], prv[x]+d2);
}
}
std::swap(prv, nxt);
}
ll ans = INF;
itr( it, prv ) {
chmin( ans, it->second+llabs(it->first) );
}
std::cout << ans << endl;
return 0;
} | //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <x86intrin.h> // __builtin_popcount(int x)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define sz(x) int(x.size())
template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;}
template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;}
template <typename T> void print(T a) {cout << a << ' ';}
template <typename T> void printe(T a) {cout << a << endl;}
template <typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;}
template <typename T> void printp(pair<T, T> a) {print(a.first); cout<<a.second<<endl;}
template <typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);}
V<int> dx = {-1, 1, 0, 0, -1, -1, 1, 1};
V<int> dy = { 0, 0, -1, 1, -1, 1, -1, 1};
int main () {
int n; cin >> n;
V<P> a(n); // {l, r}
const ll INF = 1e18;
rep(i, n) a[i] = mp(INF, -INF);
rep(i, n) {
ll x, c; cin >> x >> c; c--;
chmin(a[c].first, x), chmax(a[c].second, x);
}
V<ll> dp(2, 0); // 0:左 1:右
ll l = 0, r = 0;
rep(i, n) {
if (a[i].first == INF) continue;
V<ll> ndp(2);
ll nl = a[i].first, nr = a[i].second;
// l->nr->nl or r->nr->nl
ndp[0] = min(dp[0] + abs(l-nr) + abs(nr-nl), dp[1] + abs(r-nr) + abs(nr-nl));
// l->nl->nr or r->nl->nr
ndp[1] = min(dp[0] + abs(l-nl) + abs(nl-nr), dp[1] + abs(r-nl) + abs(nl-nr));
l = nl, r = nr;
swap(dp, ndp);
}
ll ans = min(dp[0] + abs(l), dp[1] + abs(r));
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
string s;
cin >> s;
if(n == 1){
if(s == "0"){
cout << 10000000000LL << endl;
}else{
cout << 20000000000LL << endl;
}
return 0;
}
if(n == 2){
if(s == "11") cout << 10000000000LL << endl;
else if(s == "10") cout << 10000000000LL << endl;
else if(s == "01") cout << 9999999999LL << endl;
else cout << 0 << endl;
return 0;
}
int cas;
if(s[0] == '0') cas = 0;
else if(s[1] == '0') cas = 1;
else cas = 2;
if(cas == 0){
s = s.substr(1, n - 1);
n--;
string t = "110";
bool ok = true;
for(int i=0;i<n;i++){
if(s[i] != t[i%3]) ok = false;
}
n = (n + 2) / 3;
cout << (ok ? 10000000000LL - n : 0) << endl;
return 0;
}else if(cas == 1){
s = s.substr(2, n - 2);
n -= 2;
string t = "110";
bool ok = true;
for(int i=0;i<n;i++){
if(s[i] != t[i%3]) ok = false;
}
n = (n + 2) / 3;
cout << (ok ? 10000000000LL - n : 0) << endl;
return 0;
}else{
if(s[2] == '1'){
cout << 0 << endl;
return 0;
}
s = s.substr(3, n - 3);
n -= 3;
string t = "110";
bool ok = true;
for(int i=0;i<n;i++){
if(s[i] != t[i%3]) ok = false;
}
n = (n + 2) / 3;
cout << (ok ? 10000000000LL - n : 0) << endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) cout<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define pw(b,p) pow(b,p) + 0.1
#define endl "\n"
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int32_t 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
char myHeap[200 * 1024 * 1024];
int sz = 0;
void assert_tle(bool q) { while (!q); }
void* operator new ( std::size_t count ) {
sz += count;
assert_tle(sz <= 200 * 1024 * 1024);
return myHeap + sz - count;
}
void operator delete (void *ptr) { }
void fastIO()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
void solve()
{
int n;
cin>>n;
string str;
cin>>str;
string s="110";
int i=0;
bool started=false;
string comp;
while(comp.length()<=n+5)
{
comp.pb('1');
comp.pb('1');
comp.pb('0');
}
int lim=pow(10ll,10);
lim*=3;
lim--;
int endpt=lim+3;
// if(str=="0")
// {
// cout<<pow(10ll,10);
// return;
// }
if(str=="1")
{
int ret=2*pow(10ll,10);
cout<<ret;
return;
}
if(comp.substr(0,n)==str)
{
endpt=n-1;
}
if(comp.substr(1,n)==str)
{
endpt=n;
}
if(comp.substr(2,n)==str)
{
endpt=n+1;
}
int ans=1;
ans=(lim-endpt)/3;
ans++;
cout<<ans;
}
int32_t main()
{
fastIO();
//w(t)
{
solve();
cout<<endl;
}
return 0;
} |
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <functional>
#include <queue>
#include <unordered_set>
#include <climits>
#include <set>
#include <list>
#include <cmath>
#include <map>
//#include <boost/multiprecision/cpp_int.hpp>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
//typedef boost::multiprecision::cpp_int ll;
typedef long long ll;
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
int main() {
#ifdef DEBUG
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
ll N, P;
cin >> N >> P;
mint ans = P - 1;
mint m = P - 2;
m = m.pow(N-1);
ans = ans * m;
cout << ans.x << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
long S,P;
cin >> S >> P;
for(long i=1; i<S;i++){
if(i*(S-i) == P){
cout << "Yes" << endl;
//cout << i << S << P << endl;
return 0;
}
else if (i*(S-i) > P){
cout << "No" << endl;
//cout << i << S << P << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
} |
#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);
}
ll a[55];
int n;
map<pair<int,ll>, ll> memo;
ll rek(int x, ll j){
if (memo.find({x,j}) != memo.end()) return memo[{x,j}];
ll cnt = 0;
if (j % a[x]==0){
cnt = 1;
} else {
ll y = j / a[x];
if (x == n-1 || y * a[x] < a[x+1]) cnt += rek(x-1, abs(y * a[x] - j));
if (x == n-1 || (y+1) * a[x] < a[x+1]) cnt += rek(x-1, abs((y+1) * a[x] - j));
}
return memo[{x,j}] = cnt;
}
void solve(){
ll x; cin>>n>>x;
rep(i,0,n) cin>>a[i];
cout<<rek(n-1,x)<<"\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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vbl;
typedef vector<vector<bool>> vvbl;
void INX(){}
template<typename Head, typename... Tail>
void INX(Head&& head, Tail&&... tail)
{
cin >> head;
INX(forward<Tail>(tail)...);
}
void OUTX(){}
template<typename Head, typename... Tail>
void OUTX(Head&& head, Tail&&... tail)
{
cout << head << endl;
OUTX(forward<Tail>(tail)...);
}
void OUTX2(){cout << endl;}
template<typename Head, typename... Tail>
void OUTX2(Head&& head, Tail&&... tail)
{
cout << head << ' ';
OUTX2(forward<Tail>(tail)...);
}
#define ADD emplace_back
#define MP make_pair
#define __LONG_LONG_MIN__ (-__LONG_LONG_MAX__ - 1)
//#define MOD 1000000007 // 10^9 + 7
//setprecision(10)
// 公式解説
ll N;
vll ballcnt; // 色ごとのボールの数
vll lpos; // 色ごとにボールの左端(最小)の位置を記録した配列
vll rpos; // 色ごとにボールの右端(最大)の位置を記録した配列
vvpll dp; // 色cまで回収した時点でのコスト(.first)と位置(.second)
pll f(ll c, ll lr)
{
if(c < 0) return MP(0, 0);
else if(dp[c][lr].first >= 0) return dp[c][lr];
else if(ballcnt[c] == 0) return f(c - 1, lr);
else
{
auto f2 = [](pll &pre, ll &pass, ll &goal)
{
return MP(pre.first + abs(pass - pre.second) + abs(goal - pass), goal);
};
pll prel = f(c - 1, 0);
pll prer = f(c - 1, 1);
pll can1, can2;
if(lr == 0)
{
can1 = f2(prel, rpos[c], lpos[c]);
can2 = f2(prer, rpos[c], lpos[c]);
}
else
{
can1 = f2(prel, lpos[c], rpos[c]);
can2 = f2(prer, lpos[c], rpos[c]);
}
pll &result = (can1.first < can2.first ? can1 : can2);
dp[c][lr] = result;
return result;
}
}
int main()
{
INX(N);
ballcnt.resize(N, 0);
lpos.resize(N, __LONG_LONG_MAX__);
rpos.resize(N, __LONG_LONG_MIN__);
dp.resize(N, vpll(2, MP(-1, -1)));
for (ll i = 0; i < N; i++)
{
ll X, C;
INX(X, C);
C--;
lpos[C] = min(lpos[C], X);
rpos[C] = max(rpos[C], X);
ballcnt[C]++;
}
pll can1 = f(N - 1, 0);
pll can2 = f(N - 1, 1);
ll can1c = can1.first + abs(can1.second);
ll can2c = can2.first + abs(can2.second);
OUTX(min(can1c, can2c));
return 0;
}
|
#include <bits/stdc++.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimize("O3,unroll-loops")
using ll = long long;
using ull = unsigned long long;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) for (int(i) = 0; (i) < int(n); (i)++)
#define repi(i, a, b) for (int(i) = (a); (i) < int(b); (i)++)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep)(__VA_ARGS__)
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;
}
constexpr int dy[4] = {-1, 0, 1, 0};
constexpr int dx[4] = {0, -1, 0, 1};
constexpr ll MOD = /*1'000'000'007LL;*/ 998'244'353LL;
template <unsigned int Mod>
struct mint {
unsigned int x;
mint(long long x = 0) : x((x % Mod + Mod) % Mod) {}
mint operator-() const { return mint(0) - *this; }
mint& operator+=(const mint a) {
if ((x += a.x) >= Mod) x -= Mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += Mod - a.x) >= Mod) x -= Mod;
return *this;
}
mint& operator*=(const mint a) {
x = (unsigned long long)x * (unsigned long long)a.x % Mod;
return *this;
}
mint operator^(long long t) const {
mint ret(1);
mint a = *this;
while (t) {
if (t & 1) {
ret *= a;
}
a *= a;
t >>= 1;
}
return ret;
}
mint inv() const { return *this ^ (Mod - 2); }
mint& operator/=(const mint a) { return *this *= a.inv(); }
friend mint operator+(const mint& lhs, const mint& rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint& lhs, const mint& rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint& lhs, const mint& rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint& lhs, const mint& rhs) {
return mint(lhs) /= rhs;
}
friend istream& operator>>(istream& is, mint& a) { return is >> a.x; }
friend ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
};
int H, W, K;
string S[5000];
mint<MOD> dp[5001][5001];
int sumX[5001][5001], sumY[5001][5001];
mint<MOD> prod[5001];
signed main() {
cin >> H >> W >> K;
rep(i, H) S[i] = string(W, '?');
rep(i, K) {
int h, w;
char c;
cin >> h >> w >> c;
S[h - 1][w - 1] = c;
}
rep(i, H) {
rep(j, W) {
sumY[j][i + 1] = sumY[j][i] + (S[i][j] == '?');
sumX[i][j + 1] = sumX[i][j] + (S[i][j] == '?');
}
}
prod[0] = 1;
rep(i, 1, 5001) prod[i] = prod[i - 1] * 3;
dp[0][0] = 1;
rep(i, H) {
rep(j, W) {
if (S[i][j] == '?') {
dp[i + 1][j] += dp[i][j] * prod[sumX[i + 1][j]] * 2;
dp[i][j + 1] += dp[i][j] * prod[sumY[j + 1][i]] * 2;
}
if (S[i][j] == 'R') {
dp[i][j + 1] += dp[i][j] * prod[sumY[j + 1][i]];
}
if (S[i][j] == 'D') {
dp[i + 1][j] += dp[i][j] * prod[sumX[i + 1][j]];
}
if (S[i][j] == 'X') {
dp[i + 1][j] += dp[i][j] * prod[sumX[i + 1][j]];
dp[i][j + 1] += dp[i][j] * prod[sumY[j + 1][i]];
}
// cout << dp[i][j] << " ";
}
// cout << endl;
}
cout << dp[H - 1][W - 1] * (S[H - 1][W - 1] == '?' ? 3 : 1) << endl;
} | #pragma GCC optimize(2)
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 5010
using namespace std;
ll ksm(ll x,ll y)
{
if(y==0)
{
return 1;
}
ll ret=ksm(x,y>>1);
ret=(ret*ret)%mod;
if(y&1)
{
ret=(ret*x)%mod;
}
return ret;
}
ll n,m,k,dp[N][N],sum[N][N],pw3[2],inv[5],ans=0;
char grid[N][N];
ll dfs(ll x,ll y)
{
if(x==n-1&&y==m-1)
{
return dp[x][y]=pw3[0];
}
if(x>=n||y>=m)
{
return dp[x][y]=0;
}
if(dp[x][y]!=-1)
{
return dp[x][y];
}
dp[x][y]=0;
if(grid[x][y]=='D')
{
return dp[x][y]=dfs(x+1,y);
}
if(grid[x][y]=='R')
{
return dp[x][y]=dfs(x,y+1);
}
if(grid[x][y]=='X')
{
return dp[x][y]=(dfs(x,y+1)+dfs(x+1,y))%mod;
}
return dp[x][y]=(2*(dfs(x,y+1)+dfs(x+1,y))*inv[1])%mod;
}
int main(){
ll i,j,x,y;
char c;
scanf("%lld%lld%lld",&n,&m,&k);
pw3[0]=ksm(3,n*m-k);
inv[1]=ksm(3,mod-2);
for(i=0;i<k;i++)
{
scanf("%lld%lld %c",&x,&y,&c);
x--;
y--;
grid[x][y]=c;
}
memset(dp,-1,sizeof(dp));
for(i=n-1;i>=0;i--)
{
for(j=m-1;j>=0;j--)
{
sum[i][j]=(sum[i+1][j]+sum[i][j+1]-sum[i+1][j+1]+(grid[i][j]!='D'&&grid[i][j]!='R'&&grid[i][j]!='X'));
}
}
dfs(0,0);
printf("%lld\n",dp[0][0]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
char a, b;
cin>>a>>b;
if(a=='Y') {b=b-32; cout << b << endl;}
else cout << b << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
int k=(2*a)+100;
cout<<k-b<<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)
#define MOD 1000000007
#define MOD2 1000000009
#define endl "\n"
#define pii pair <int, int>
#define pll pair <ll, ll>
#define F first
#define S second
map<ll, ll> dp, vis;
ll ans = 1e18;
ll solve(ll x, ll y) {
if(vis[y]) {
return dp[y];
}
vis[y] = 1;
dp[y] = 1e18;
dp[y] = min(dp[y], abs(x - y));
if(x == y)
return dp[y];
if(y % 2 == 0)
return dp[y] = min(dp[y], 1 + solve(x, y / 2));
return dp[y] = min(dp[y], 1 + min(solve(x, y - 1), solve(x, y + 1)));
return dp[y];
}
int main()
{
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
/* int t;
cin >> t;
while(t--) {
}*/
ll x, y;
cin >> x >> y;
cout << solve(x, y);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
constexpr int inf=1000001000;
constexpr ll INF=2e18;
constexpr ll mod=1000000007;
// ll MOD=998244353;
const ll MOD=998244353;
void Add(int &a, int b) {
a += b;
while (a >= MOD) a -= MOD;
while (a < 0) a += MOD;
}
void Mul(int &a, int b) {
a = 1LL * a * b % MOD;
}
int Pow(int b, int e) {
if (e < 0) e += MOD - 1;
int ret = 1;
while (e) {
if (e & 1) ret = 1LL * ret * b % MOD;
b = 1LL * b * b % MOD;
e >>= 1;
}
return ret;
}
int main() {
int n,k;
cin >> n >> k;
vector<ll> a(n);
rep(i,n) cin >> a[i];
// vector<vector<ll>> b(n,vector<ll>(k+1,1));
vector<ll> b(n,1);
vector<ll> e(k+5,0);
rep(i,k+1) e[i]=Pow(i,MOD-2);
vector<ll> c(k+1,0);
rep(i,n){
rep(j,k+1){
(c[j]+=b[i])%=MOD;
b[i]=(b[i]*a[i]%MOD*e[j+1])%MOD;
}
}
// rep(i,n){
// rep(j,k+1) (c[j]+=b[i][j])%=MOD;
// }
vector<ll> ans(k+1);
ans[1]=1;
rep(i,k){
ans[i+2]=(ans[i+1]*(i+2))%MOD;
}
vector<ll> o(k,0);
rep(i,k){
rep(j,k+1){
if (i+1-j<0) break;
(o[i]+=c[j]*c[i+1-j]%MOD)%=MOD;
}
}
vector<ll> p(k+2,0);
ll cc;
rep(i,n){
cc=a[i]*2;
rep(j,k) {
(p[j]+=cc)%=MOD;
(cc*=a[i]*2)%=MOD;
}
}
rep(i,k) cout << ((ans[i+1]*o[i]%MOD+MOD-p[i])%MOD)*Pow(2,MOD-2)%MOD << endl;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
typedef vector<vector<bool> > vvb;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define mes(a) cout << (a) << endl
#define dmes(a, b) cout << (a) << " " << (b) << endl
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#define ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define Rev(a) reverse(a.begin(), a.end())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
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; }
struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } };
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
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; }
ll modpow(ll x, ll y) {
if(y == 1) {
return x;
}
ll ans;
if(y % 2 == 1) {
ll r = modpow(x,(y-1)/2);
ans = r * r % MOD;
ans = ans * x % MOD;
}
else {
ll r = modpow(x,y/2);
ans = r * r % MOD;
}
return ans;
}
ll modncr(ll N, ll K) {
ll res = 1;
ll p=1;
for (ll n = 0; n < K; ++n) {
res = (res*(N - n))%MOD;
p = (p*(n + 1))%MOD;
}
return (res*modpow(p,MOD-2))%MOD;
}
signed main() {
ll n;
cin >> n;
ll r = 1;
vll flg(31, 0);
for (ll i = n; i >= 2; i--) {
r = lcm (r, i);
}
mes(r+1);
}
| #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG fprintf(stderr,"Running on Line %d in Function %s\n",__LINE__,__FUNCTION__)
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define int long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read()
{
char ch=getchar(); int nega=1; while(!isdigit(ch)) {if(ch=='-') nega=-1; ch=getchar();}
int ans=0; while(isdigit(ch)) {ans=ans*10+ch-48;ch=getchar();}
if(nega==-1) return -ans;
return ans;
}
typedef pair<int,int> pii;
void print(vector<int> x){for(int i=0;i<(int)x.size();i++) printf("%d%c",x[i]," \n"[i==(int)x.size()-1]);}
#define N 100005
int f[N],g[N],n;
vector<int> ans;
int now,x,y;
void push(int v)
{
if(v==1) x++;
else if(v==2) y++;
else if(v==3) x+=y;
else y+=x;
ans.pb(v);
}
signed main()
{
cin>>n;
f[0]=f[1]=1;
for(int i=2;i<=100;i++) f[i]=f[i-1]+f[i-2];
int maxn=-1;
for(int i=87;i>=1;i--)
{
while(n>=f[i])
{
if(maxn==-1) maxn=i;
n-=f[i],g[maxn-i]++;
}
}
now=maxn&1;
for(int i=0;i<=maxn;i++)
{
while(g[i]) push(now+1),g[i]--;
push(now+3),now^=1;
}
cout<<ans.size()<<endl;
for(int i=0;i<(int)ans.size();i++) printf("%lld\n",ans[i]);
return 0;
}
|
//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define shandom_ruffle random_shuffle
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;
const int nax=1000*1007;
int n;
int fix(int v)
{
while(v>n)
v-=n;
return v;
}
int main()
{
scanf("%d", &n);
for (int i=1; i<=n; i++)
{
int a=2*i;
int b=2*i+1;
a=fix(a);
b=fix(b);
printf("%d %d\n", a, b);
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
if(n==2){
cout<<1<<" "<<2<<endl;
cout<<1<<" "<<2<<endl;
}else{
for(int i=1;i<=n;i++){
if(i==1){
cout<<1<<" "<<n/2+1<<endl;
}else if(i==n){
cout<<n/2+1<<" "<<n<<endl;
}else{
if(i!=2){
cout<<(i+1)/2<<" "<<i+(n+1-i)/2<<endl;
}else{
cout<<1<<" "<<i+(n+1-i)/2<<endl;
}
}
}
}
} |
#include<bits/stdc++.h>
//#include <atcoder/all>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define fi first
#define se second
#define sz(x) ((int)x.size())
using namespace std;
//using namespace atcoder;
using ld = long double;
using ll = long long;
using P = pair<ll, ll>;
template<typename T> bool chmin(T& a, const T& b) { if(a >= b){ a = b; return 1;} return 0; }
template<typename T> bool chmax(T& a, const T& b) { if(a <= b){ a = b; return 1;} return 0; }
const ll MOD = 998244353;
const ll INF = 1e9;
//BIT
template <typename T>
struct BIT{
int n; //配列の要素数
vector<T> bit; //データの格納先
BIT(int n_) : n(n_ + 1), bit(n, 0){}
void add(int i, T x){
for(int idx = i; idx < n; idx += (idx & -idx)){
bit[idx] += x;
}
}
T sum(int i){
T s(0);
for(int idx = i; idx > 0; idx -= (idx & -idx)){
s += bit[idx];
}
return s;
}
//sum(a_i) >= w となるような最小のxを求める
int lower_bound(T w) { // a_1 + a_2 + ... + a_x >= w となるような最小の x を求める(ただし a_i >= 0)
if (w <= 0) return 0;
else {
int x = 0, r = 1;
while (r < n) r = r << 1;
for (int len = r; len > 0; len = len >> 1) { // 長さlenは1段下るごとに半分に
if (x + len < n && bit[x + len] < w) { // 採用するとき
w -= bit[x + len];
x += len;
}
}
return x + 1;
}
}
T num(int i){
T res = sum(i);
if(i != 1)res -= sum(i - 1);
return res;
}
};
vector<vector<int>> G;
vector<int> eul, dep;
void dfs(int v, int p){
eul.pb(v);
for(auto nv : G[v]){
if(nv == p)continue;
dep[nv] = dep[v] + 1;
dfs(nv, v);
}
eul.pb(v);
}
int main(){
int n;
cin >> n;
G.resize(n);
dep.resize(n);
vector<int> p(n - 1);
rep(i, n - 1)cin >> p[i];
rep(i, n - 1){
p[i]--;
G[i + 1].pb(p[i]);
G[p[i]].pb(i + 1);
}
int q;
cin >> q;
vector<int> U(q), D(q);
rep(i, q)cin >> U[i] >> D[i];
//Dごとに管理する
vector<vector<P>> qry(n + 1);
rep(i, q){
U[i]--;
qry[D[i]].pb(P(U[i], i));
}
dfs(0, -1);
//dep[v] -> v
vector<vector<int>> rdep(n + 1);
rep(i, n){
rdep[dep[i]].pb(i);
}
//vを根とする部分木に対応する区間を持つ
vector<int> L(n, -1), R(n);
rep(i, sz(eul)){
if(L[eul[i]] == -1)L[eul[i]] = i;
R[eul[i]] = i;
}
BIT<int> bit(sz(eul));
vector<int> res(q);
REP(i, 0, n){
//bitに深さiの点を加える
for(auto v : rdep[i]){
bit.add(L[v] + 1, 1);
}
//クエリ処理
for(auto qi : qry[i]){
auto [ui, id] = qi;
res[id] = bit.sum(R[ui] + 1);
if(L[ui] > 0)res[id] -= bit.sum(L[ui]);
}
//足した分引く
for(auto v : rdep[i]){
bit.add(L[v] + 1, -1);
}
}
for(auto ri : res)cout << ri << endl;
} | #include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define endl '\n'
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1000000007;
const int dx[] = { 0, 0, -1, 1, 1, -1, 1, -1};
const int dy[] = {-1, 1, 0, 0, 1, -1, -1, 1};
void merge(map<int, int> &from, map<int, int> &to){
if(from.size() > to.size())
swap(from, to);
for(auto &[a, b]: from)
to[a] += b;
from.clear();
}
const int MAXN = 200010;
int ans[MAXN];
vector<int> adj[MAXN];
vector<pii> queries[MAXN];
void dfs(int u, int dt, map<int, int> &mp){
for(int to: adj[u]){
map<int, int> aux;
dfs(to, dt + 1, aux);
merge(aux, mp);
}
mp[dt]++;
for(auto [d, id]: queries[u])
ans[id] = mp[d];
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n;
cin >> n;
for(int i=2; i<=n; i++){
int x;
cin >> x;
adj[x].push_back(i);
}
int q;
cin >> q;
for(int i=0; i<q; i++){
int u, d;
cin >> u >> d;
queries[u].emplace_back(d, i);
}
map<int, int> mp;
dfs(1, 0, mp);
for(int i=0; i<q; i++)
cout << ans[i] << endl;
return 0;
}
|
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define DB double
#define LD long double
#define ST string
#define BS bitset
#define PA pair<LL,LL>
#define VE vector
#define VL VE<LL>
#define VP VE<PA>
#define VVL VE<VL>
#define VVVL VE<VVL>
#define PQ priority_queue
#define PQS priority_queue<LL,vector<LL>,greater<LL>>
#define FI first
#define SE second
#define PB push_back
#define POB pop_back
#define PF push_front
#define POF pop_front
#define MP make_pair
#define TS to_string
#define TU to_ullong
#define BPL __builtin_popcountll
#define FOR(i,a,n) for(i=a;i<n;++i)
#define FORR(i,a,n) for(i=n-1;i>=a;--i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) FORR(i,0,n)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define UB(a,n) *upper_bound(ALL(a),n)
#define UBn(a,n) upper_bound(ALL(a),n)-a.begin()
#define LB(a,n) *lower_bound(ALL(a),n)
#define LBn(a,n) lower_bound(ALL(a),n)-a.begin()
#define INF 1000000000000000003
#define PI 3.14159265358979323846264338327950288
//#define MOD 1000000007
#define MOD 998244353
#define ERR 1e-10
#define coutl cout<<fixed<<setprecision(15)
#define FAST cin.tie(0);ios::sync_with_stdio(false)
void Yn(LL a){if(a)cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void YN(LL a){if(a)cout<<"YES"<<endl;else cout<<"NO"<<endl;}
LL pwmn(LL a,LL n){LL ans=1;while(ans<a)ans*=n;return ans;}
LL dig(LL n){LL ret=0;while(n)n/=10,++ret;return ret;}
LL GCD(LL a,LL b){LL c=1,tmp=max(a,b);b=min(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;}
LL LCM(LL a,LL b){return a*b/GCD(a,b);}
LL cmod(LL a,LL m){if(a%m<0)return a%m+abs(m);else return a%m;}
LL DIV(LL a,LL d,LL m){LL l=m,x=1,y=0,k;while(l){k=d/l;d-=k*l;swap(l,d);x-=k*y;swap(x,y);}return cmod(a*cmod(x,m),m);}
LL POW(LL a,LL n,LL m){LL ans=1;while(n>0){if(n&1)ans=ans*a%m;a=a*a%m;n>>=1;}return ans;}
VL fact,finv,inv;
void comi(LL n){LL i;fact.resize(max(2LL,n+1));finv.resize(max(2LL,n+1));inv.resize(max(2LL,n+1));fact[0]=fact[1]=1;finv[0]=finv[1]=1;inv[0]=inv[1]=1;FOR(i,2,n+1){fact[i]=fact[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}}
LL com(LL n,LL k){if(n<k||n<0||k<0)return 0;return fact[n]*(finv[k]*finv[n-k]%MOD)%MOD;}
bool cmps(PA a,PA b){if(a.SE!=b.SE)return a.SE<b.SE;return a.FI<b.FI;}
template<typename T>bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>void vout(VE<T> &v){LL i;rep(i,v.size()){cout<<v[i];if(i<v.size()-1)cout<<" ";}cout<<endl;}
template<typename T>void v2out(VE<VE<T>> &v){for(auto a:v)vout(a);}
int main(){
FAST;
LL i,ans=0,V,T,S,D;
cin>>V>>T>>S>>D;
Yn(!(V*T<=D&&V*S>=D));
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//int a[200000];
int main()
{
int n, m, t, k, s = 0;
int a,b,c,d;
cin>>a>>b>>c>>d;
int t1=b*a;
int t2=c*a;
if(d>=t1&&d<=t2)
{
cout<<"No";
}
else
{
cout<<"Yes";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
// Defines
#define all(x) (x).begin(),(x).end()
#define sortall(x) sort(x.begin(),x.end())
#define maxin(x) *max_element(x.begin(),x.end())
#define minin(x) *min_element(x.begin(),x.end())
#define lint long long int
#define nl '\n'
#define INF (lint)1e18
#define EPS (long double)1/(INF)
//#define MOD (lint)998244353
#define MOD (lint)(1e9+7)
//Debugs
#ifdef LOCAL
#define debugvar(x) cout<<#x<<"\t:"<<(x)<<"\n"
#define debugvec(x) cout<<#x<<"\t:"; for(auto it = (x).begin();it!=(x).end();it++) cout<<*it<<" "; cout<<nl
#else
#define debugvar(x) 0
#define debugvec(x) 0
#endif
//Templates
template <typename T> inline T gcd(T a, T b) { a=(a>0?a:-a); b=(b>0?b:-b); while (b != 0) swap(b, a %= b); return a;}
template <typename T> inline T extended_gcd(T a, T& x, T b, T& y){ // returns gcd(a,b) && a*x + b*y = gcd(a,b)
T l_a = 1, l_b = 0;
T r_a = 0, r_b = 1;
T q = a/b, r = a%b;
while(r>0){
T temp;
temp = r_a, r_a = l_a - q*r_a ,l_a = temp;
temp = r_b, r_b = l_b - q*r_b ,l_b = temp;
a = b;
b = r;
q = a/b;
r = a%b;
}
x = r_a;
y = r_b;
return b;
}
template <typename T> inline T MOD_inv (T num){
lint inv,temp;
assert(extended_gcd(MOD,temp,num,inv)==1);
return (inv%MOD+MOD)%MOD;
}
template <typename T> inline T lcm(T a, T b) { if(a*b==0) return (T)0; return ((a/gcd(a,b))*b);}
template <typename T> inline T getpow(T base, T exp){
if(exp==0) return (T)1;
T half = getpow(base,exp/2);
T ans = half*half;
if(exp&1) ans = ans*base;
return ans;
}
template <typename T> inline T getpowMOD(T base, T exp){
if(exp==0) return (T)1;
base = base%MOD;
T half = getpowMOD(base,exp/2);
T ans = (half*half)%MOD;
if(exp&1) ans = (ans*base)%MOD;
return ans;
}
lint ncrMOD(lint n, lint r){
if(r>n) return 0;
r = min(r,n-r);
lint fact = 1;
for(lint i=1;i<=r;i++){
fact=(fact*(n+1-i))%MOD;
fact=(fact*MOD_inv(i))%MOD;
}
return fact;
}
void solve(){
lint n,m,k;
cin>>n>>m>>k;
if(m-n<-k){cout<<0;return;}
lint ans = ncrMOD(m+n,m)-ncrMOD(m+n,m+k+1);
ans = (ans+MOD)%MOD;
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);lint testnum=1,totalnum=1;
//cin>>totalnum;
for(testnum=1;testnum<=totalnum;++testnum){
//cout<<"Case #"<<testnum<<": ";
//cout<<ncrMOD(28,14);
solve();
cout<<nl;
}
bool time = 0;
//time=true;
if(time){
std::chrono::high_resolution_clock::time_point end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time_taken = std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time);
cout << "\nTime Taken: " <<setprecision(10)<<time_taken.count() << " s";
}
return 0;
}
/*
created on: 27.06.2021
*/
| /*
JAI JAGANNATH!
*/
//@Author : zanj0
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ff first
#define ss second
#define pb push_back
#define MOD 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define w(x) int x; cin>>x; while(x--)
#define endl "\n"
#define timetaken cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n"
void zanj0()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
const int MAX = 3e6 + 5;
int fac[MAX], finv[MAX];
int inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
int 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;
}
int add(int a, int b) {
return ((a % MOD) + (b % MOD)) % MOD;
}
int mul(int a, int b) {
return ((a % MOD) * (b % MOD)) % MOD;
}
int fastPow(int base, int p) {
int ret = 1;
while (p) {
if (p & 1) ret = mul(ret, base);
base = mul(base, base);
p >>= 1;
}
return ret;
}
int sub(int a, int b) {
return (((a % MOD) - (b % MOD)) + MOD) % MOD;
}
void solve() {
int n, m, k;
cin >> n >> m >> k;
COMinit();
if (n > m + k) {
cout << 0 << endl;
} else if (k >= n) {
cout << COM(n + m, n) << endl;
} else {
cout << sub(COM(n + m, n), COM(n + m, n - k - 1)) << endl;
}
}
int32_t main()
{
zanj0();
solve();
timetaken;
return 0;
} |
//
// main.cpp
// Atcoder1
//
// Created by Hamske on 2020/09/17.
// Copyright © 2020 Author. All rights reserved.
//
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
//#define pb push_back
//#define mp make_pair
#define fi first
#define se second
typedef long long ll;
//#define rep(_i,iv,n) for(int _i=(int)(iv);_i<(int)(n);++_i)
#define rep0(_i,n) for(int _i=0;_i<(int)(n);++_i)
//#define REP(_i,iv,_l,n) for(int _i=(int)(iv),_l=(int)(n);_i<_l;++_i)
#define REP0(_i,_l,n) for(int _i=0,_l=(int)(n);_i<_l;++_i)
//#define all(x) (x).begin(),(x).end()
#define REPV(v,_itr,_end) for(auto _itr=(v).begin(),_end=(v).end();_itr!=_end;++_itr)
#define REPVC(v,_itr,_end) for(auto _itr=(v).cbegin(),_end=(v).cend();_itr!=_end;++_itr)
#define td typedef
//#define tds(stl,type,name) typedef stl<type > name;
#define tdv(type,name) typedef vector<type > name;
typedef vector<bool> vb;
typedef vector<vb > vvb;
typedef vector<int> vi;
typedef vector<vi > vvi;
typedef vector<ll> vll;
typedef vector<vll > vvll;
#define INF 1<<30
td pair<int, int > pii;
td priority_queue<pii,vector<pii>,greater<pii> > pq;
#define mod7 1000000007
void init(int S, int *tree, int i) {
if (i >= S) {
return;
}
init(S, tree, i * 2);
init(S, tree, i * 2 + 1);
tree[i] = tree[i * 2] ^ tree[i * 2 + 1];
}
void update(int *tree, int i) {
if (i == 1) {
return;
}
int pi = i / 2;
tree[pi] = tree[pi * 2] ^ tree[pi * 2 + 1];
update(tree, pi);
}
int show(int *tree, int l) {
int ans = tree[l];
for (int i = l; i != 1; i /= 2) {
if (i % 2) {
ans ^= tree[i - 1];
}
}
return ans;
}
int getS(int N) {
for (int i = 1; ; ++i) {
if (N <= (1 << i)) {
return 1 << i;
}
}
}
int main(int argc, const char * argv[]) {
int N;
cin >> N;
int A[N], B[N];
rep0(i, N) {
cin>>A[i]>>B[i];
}
int tMin = INT32_MAX;
rep0(i, N) {
rep0(j, N) {
int t;
if (i == j) {
t = A[i]+B[j];
} else {
t = max(A[i], B[j]);
}
if (t <tMin) {
tMin = t;
}
}
}
cout << tMin <<endl;
return 0;
}
| /* _____________________
|Author : canhnam357|
|___________________|
*/
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <string> vs;
typedef vector <vector <int>> vvi;
typedef vector <vll> vvll;
typedef map<int, int> mi;
typedef map<string, int> ms;
typedef map<char, int> mc;
typedef map <int, bool> mb;
typedef map<ll, ll> mll;
typedef unordered_map<int, int> umi;
typedef unordered_map<string, int> ums;
typedef unordered_map<char, int> umc;
typedef unordered_map <int, bool> umb;
typedef unordered_map<ll, ll> umll;
typedef vector <ld> vld;
typedef vector <bool> vb;
typedef pair <int, int> pii;
typedef pair<ll, ll> pll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
int CASE = 1;
const int mxn = 4e3 + 1;
const ll INF = 1e18;
#define FOR(i,N) for(ll i = 0 ; i < N;i++)
#define eFOR(i,a,b) for(ll i = a; i <=b;i++)
#define dFOR(i,N) for(ll i = N - 1; i>=0;i--)
#define edFOR(i,b,a) for(ll i = b ; i >=a;i--)
#define all(x) x.begin(),x.end()
#define SORT(x) sort(all(x))
#define RSORT(x) sort(x.rbegin(),x.rend())
#define UNQ(x) unique(all(x))
#define mine(x) min_element(all(x))
#define maxe(x) max_element(all(x))
#define lb(v, x) lower_bound(all(v) , x)
#define ub(v , x) upper_bound(all(v) , x)
#define pb push_back
#define PI 3.141592653589793
#define FIX cout << fixed << setprecision(15)
#define g(i , a) get<i>(a)
const ll mod = 1e5;
bool prime(ll n)
{
if (n <= 1)
return false;
eFOR(i, 2, sqrt(n))
if (!(n % i))
return false;
return true;
}
ll __gcd(ll a, ll b)
{
return !b ? a : __gcd(b, a % b);
}
void solve()
{
int a, b, x, y;
cin >> a >> b >> x >> y;
if (b >= a)
{
cout << x + min(2 * x, y) * (b - a);
}
else
{
int k = a - b;
cout << min(x + (k - 1) * y, (2 * k - 1) * x);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
while (T--)
{
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9+7;
void experiment(vector<char> V){
for(int i=0; i<4; i++){
cout << V[i];
}
cout << " ";
set<string> S;
S.insert("AB");
queue<string> Q;
Q.push("AB");
for(int i=3; i<=10; i++){
cout << S.size() << " ";
S.clear();
int q = Q.size();
for(int a=0; a<q; a++){
string T = Q.front();
Q.pop();
for(int i=0; i<T.size()-1; i++){
string U = "";
U += T.substr(0,i+1);
if(T[i] == 'A' && T[i+1] == 'A'){
U += V[0];
}
if(T[i] == 'A' && T[i+1] == 'B'){
U += V[1];
}
if(T[i] == 'B' && T[i+1] == 'A'){
U += V[2];
}
if(T[i] == 'B' && T[i+1] == 'B'){
U += V[3];
}
U += T.substr(i+1);
S.insert(U);
Q.push(U);
}
}
}
cout << endl;
}
int main(){
int N;
cin >> N;
vector<char> vec(4);
string ans = "";
for(int i=0; i<4; i++){
cin >> vec[i];
ans += vec[i];
}
/*for(int bit = 0; bit<(1<<4); bit++){
bitset<4> X(bit);
for(int j=0; j<4; j++){
if(X.test(j)){
vec[j] = 'B';
}
else{
vec[j] = 'A';
}
}
experiment(vec);
}*/
vector<int64_t> calc(N+1,1);
if(ans == "ABAA" || ans == "BBAA" || ans == "BABA" || ans == "BABB"){
for(int i=3; i<N; i++){
calc[i+1] = calc[i]*2;
calc[i+1] %= MOD;
}
}
else if(ans == "BAAA" || ans == "ABBA" || ans == "BBBA" || ans == "BAAB"){
for(int i=3; i<N; i++){
calc[i+1] = calc[i] + calc[i-1];
calc[i+1] %= MOD;
}
}
cout << calc[N] << endl;
} | #include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define mp make_pair
#define pb push_back
#define pll pair<LL,LL>
#define pii pair<int,int>
#define y second
#define x first
#define LL long long
#define ULL unsigned long long
#define sqr(x) ((x)*(x))
#define pi acos(-1)
#define pdd pair<double,double>
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
using namespace std;
LL mod=1e9+7;
LL f_pow(LL a,LL b){
LL res=1,temp=a;
while(b){
if(b&1)res=res*temp%mod;
temp=temp*temp%mod;
b>>=1;
}
return res;
}
void solve(){
int n;
scanf("%d",&n);
char c[2][2];
for(int i = 0;i<2;i++){
for(int j =0;j<2;j++){
scanf(" %c",&c[i][j]);
}
}
if(n<=3){
printf("1\n");
return;
}
if(c[0][1]=='A'){
if(c[0][0]=='A')printf("1\n");
else{
if(c[1][0]=='B'){
printf("%d\n",f_pow(2,n-3));
}
else{
int f[1005];
f[0]=1;
f[1]=1;
for(int i=2;i<=n-2;i++){
f[i]=(f[i-1]+f[i-2])%mod;
}
printf("%lld\n",(f[n-3]+f[n-4])%mod);
}
}
}
else{
if(c[1][1]=='B')printf("1\n");
else{
if(c[1][0]=='A'){
printf("%d\n",f_pow(2,n-3));
}
else{
int f[1005];
f[0]=1;
f[1]=1;
for(int i=2;i<=n-2;i++){
f[i]=(f[i-1]+f[i-2])%mod;
}
printf("%lld\n",(f[n-3]+f[n-4])%mod);
}
}
}
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--){
solve();
}
} |
#define ll long long int
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#include <bits/stdc++.h>
using namespace std;
void solve()
{
string s;
cin >> s;
string t = "";
for(int i=0; i<s.size(); i++){
if(s[i] == '.'){break;}
else{t += s[i];}
}
cout << t << "\n";
}
int main()
{
fast;
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std ;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef long long ll ;
typedef unsigned long long ull ;
typedef long double ld ;
const ll MOD = 1E9 + 7, INF = 2E18 + 5 ;
const double PI = 2 * acos(0.0) ;
const long double EPS = 1.0E-14 ;
vector <ll> vec ;
int main()
{
ll n, lim ;
cin >> n ;
lim = sqrt(n) ;
for (int i = 1; i <= lim; i++) {
if (n % i == 0) {
if (n / i != i) {
vec.push_back(i) ;
vec.push_back(n / i) ;
}
else {
vec.push_back(n / i) ;
}
}
}
sort(vec.begin(), vec.end()) ;
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << "\n" ;
}
return 0 ;
}
|
#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for(T &x : v) is >> x;
return is;
}
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
/*-------------------------------------------*/
int isp[1000009];
int main() {
int l, r;
cin >> l >> r;
for(int i = 2; i < 1000009; i++)
if(isp[i] == 0)
for(int j = i; j < 1000009; j += i) isp[j] = i;
ll ans = 0;
FOR(i, 2, r + 1) {
int g = i;
int mn = l / g;
if(l % g != 0) mn++;
if(mn * g > r) continue;
int mx = r / g;
vector<int> ps;
bool flag = true;
while(g != 1) {
if(!ps.empty() && ps.back() == isp[g]) flag = false;
ps.push_back(isp[g]);
g /= isp[g];
}
if(flag)
ans += (ps.size() % 2 ? 1LL : -1LL) * (mx - mn + 1) * (mx - mn + 1);
if(mn == 1) ans -= 2 * mx - 1;
// cout << g << " " << ans << " " << mn << " " << mx << endl;
}
cout << ans << endl;
} | #include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define ull unsigned ll
#define ld long double
#define mod 1000000007
#define pb push_back
#define pf push_front
#define dll deque<ll>
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define dpll deque<pll>
#define mapll map<ll,ll>
#define umapll unordered_map<ll,ll,custom_hash>
#define nl "\n"
#define all(v) v.begin(),v.end()
#define fr for(ll i=0;i<n;i++)
#define frr for(ll i=1;i<=n;i++)
#define ms(a,x) memset(a,x,sizeof(a))
//#define fr(aa,bb) for(ll i=aa;i<=bb;i++)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace __gnu_pbds;
vector<pair<ll,ll>>v[100005];
vector<string> split(const string& s, char c) {
vector<string> v; stringstream ss(s); string x;
while (getline(ss, x, c)) v.push_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
stringstream s; s << "[";
for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
s << arr[n - 1] << "]";
return s.str();
}
#define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __evars_out_var(T val) { cerr << val; }
inline void __evars(vector<string>::iterator it) { cerr << endl; }
template<typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
__evars_out_var(a);
cerr << "; ";
__evars(++it, args...);
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
//template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
//template<class T>using uset=unordered_set<T,custom_hash>;
ll dmod(ll x){
return ((x+mod)%mod);
}
ll modular_power(ll x,ll y){
ll ans=1;
while(y){
if(y&1)ans=dmod(ans*x);
y/=2;
x=dmod(x*x);
}
return ans;
}
ll inv(ll x){
return modular_power(dmod(x),mod-2);
}
int main()
{
//clock_t clk = clock();
fio
//cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
int t=1;
//cin>>t;
while(t--)
{
ll n,var;cin>>n;string s;
vector<ll>v;
unordered_map<ll,string,custom_hash>mp;
fr
{
cin>>s>>var;
mp[var]=s;
v.pb(var);
}
sort(v.begin(),v.end());
cout<<mp[v[v.size()-2]];
}
return 0;
} |
#include<bits/stdc++.h>
#define for0(n) for(lli i=0;i<n;i++)
#define in(x) lli x; cin>>x
using namespace std;
typedef long long int lli;
void buildTree(lli s,lli e,lli *arr, vector<lli> &tree,lli ind){
// base
if(s==e){
// at the leaf node
tree[ind] = arr[s];
return;
}
// recurse
lli mid = (s+e)/2;
buildTree(s,mid,arr,tree,2*ind);
buildTree(mid+1,e,arr,tree,2*ind+1);
tree[ind] = (tree[2*ind] ^ tree[2*ind+1]);
return;
}
lli queryTree(lli l,lli r ,lli s,lli e,vector<lli> &tree,lli ind){
//base
if(s > r || e < l)
return 0; // as x xor 0 is just x
if(s>=l && e<=r){
return tree[ind];
}
// recurse
lli mid = (s+e)/2;
lli left = queryTree(l,r,s,mid,tree,2*ind);
lli right = queryTree(l,r,mid+1,e,tree,2*ind+1);
return (left^right);
}
// let's just not take lazy and try it
void updateTree(lli s,lli e,lli i,lli val, vector<lli> &tree,lli ind){
//base
// function signature ->
// val : it is the value with which
// you wanna replace a[i].
if(s == e && i==s){
tree[ind] = val;
return;
}
if(i < s || i > e)
return;
//reurse
lli mid = (s+e)/2;
updateTree(s,mid,i,val,tree,2*ind);
updateTree(mid+1,e,i,val,tree,2*ind+1);
tree[ind] = tree[2*ind] ^ tree[2*ind+1];
return;
}
int main(){
ios::sync_with_stdio(0);
in(n);
in(q);
lli t,x,y;
lli arr[n];
for0(n)
cin>>arr[i];
vector<lli> tree(4*300005);
buildTree(0,n-1,arr,tree,1LL);
while(q--){
cin>>t>>x>>y;
if(t==1){
updateTree(0,n-1,x-1,(arr[x-1] ^ y),tree,1LL);
arr[x-1] ^= y;
}
else{
cout<<queryTree(x-1,y-1,0,n-1,tree,1LL)<<endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define LL long long
#define int long long
namespace IO {
const int N = 2e6;
char buf[N], *p1 = buf, *p2 = buf;
inline char gc() {
if(p1 == p2) p2 = (p1 = buf) + fread(buf, 1, N, stdin);
return p1 == p2 ? EOF : *(p1 ++);
}
template <typename T>
inline void read(T &x) {
x = 0; bool f = 0; char a = gc();
for(; ! isdigit(a); a = gc()) if(a == '-') f = 1;
for(; isdigit(a); a = gc()) x = x * 10 + (a ^ 48);
if(f) x = -x;
}
inline int read() { int x; read(x); return x; }
inline LL readll() { LL x; read(x); return x; }
inline char readchar() {
char a = gc();
while(a == ' ' || a == '\n') a = gc(); return a;
}
inline int reads(char *s) {
char *O = s, a = gc();
while(! isalpha(a) && ! isdigit(a)) a = gc();
while(isalpha(a) || isdigit(a)) *O = a, O ++, a = gc();
return O - s;
}
inline void outs(int len, char *s, char ch = '\n') {
char *O = s;
while(len --) putchar(*O), O ++; putchar(ch);
}
char Of[105], *O1 = Of, *O2 = Of;
template <typename T>
inline void print(T n, char ch = '\n') {
if(n < 0) putchar('-'), n = -n;
if(n == 0) putchar('0');
while(n) *(O1 ++) = (n % 10) ^ 48, n /= 10;
while(O1 != O2) putchar(*(-- O1));
putchar(ch);
}
}
using IO :: read;
using IO :: print;
using IO :: reads;
using IO :: outs;
using IO :: readchar;
const int N = 2e5 + 10;
int A, B, C;
vector<int> find(int x, int p) {
x %= p;
map<int, int> mp;
vector<int> res;
int t = 1;
while(1) {
t = (LL) t * x % p;
if(mp[t]) return res;
res.push_back(t);
mp[t] = 1;
}
}
int power(int x, int k, int P) {
int res = 1;
while(k) {
if(k & 1) res = (LL) res * x % P;
x = (LL) x * x % P; k >>= 1;
} return res;
}
signed main() {
#ifdef IN
freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
#endif
A = read(); B = read(); C = read();
vector<int> p1 = find(A, 10);
B = power(B, C, p1.size());
B --;
if(B < 0) B += p1.size();
print(p1[B]);
/*vector<int> p2 = find(B, p1.size());
C %= p2.size();
C --;
if(C < 0) C += p2.size();
int v = p2[C];
v %= p1.size();
v --;
if(v < 0) v += p1.size();
v = p1[v];
print(v % 10);*/
return 0;
} |
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double Sx, Sy, Gx, Gy;
cin >> Sx >> Sy >> Gx >> Gy;
cout << fixed << setprecision(7) << Sx + Sy * (Gx - Sx) / (Gy + Sy) << endl;
} | /*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
//*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int ,int > pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn = 3e6;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n;
char aa , ab , ba , bb;
ll F[maxn];
ll f(int x){
if(x <= 1)return(F[x] = 1);
if(F[x])return(F[x]);
return( F[x] = (f(x - 1) + f(x - 2))%mod );
}
int32_t main(){
migmig;
cin >> n >> aa >> ab >> ba >> bb;
if(n <= 3)dokme(1);
if(aa == ab && ab == ba && ba == bb)dokme(1);
if(ab == 'B'){
if(bb == 'B')dokme(1);
if(ba == 'A')dokme(pw(2 , n - 3));
dokme(f(n - 2));
}
if(aa == 'A')dokme(1);
if(ba == 'B')dokme(pw(2 , n - 3));
dokme((f(n - 2)));
return(0);
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 10e16;
int main() {
int n, m;
cin >> n >> m;
vector<ll> a(m), b(m);
vector<char> c(m);
rep(i, m) {
cin >> a[i] >> b[i] >> c[i];
a[i]--, b[i]--;
}
vector<vector<vector<P>>> G(n, vector<vector<P>>(n));
rep(i, m) rep(j, m) {
if (i == j) continue;
if (c[i] != c[j]) continue;
G[a[i]][a[j]].push_back({b[i], b[j]});
G[b[i]][b[j]].push_back({a[i], a[j]});
G[a[i]][b[j]].push_back({b[i], a[j]});
G[b[i]][a[j]].push_back({a[i], b[j]});
}
vector<vector<ll>> dist(n, vector<ll>(n, INF));
queue<P> que;
dist[0][n - 1] = 0;
que.push({0, n - 1});
while (!que.empty()) {
P p = que.front();
que.pop();
for (P np : G[p.first][p.second]) {
if (dist[np.first][np.second] != INF) continue;
dist[np.first][np.second] = dist[p.first][p.second] + 1;
que.push(np);
}
}
ll ans = INF;
rep(i, n) ans = min(ans, dist[i][i] * 2);
rep(i, m) {
ans = min(ans, dist[a[i]][b[i]] * 2 + 1);
ans = min(ans, dist[b[i]][a[i]] * 2 + 1);
}
if (ans == INF) ans = -1;
cout << ans << endl;
} | #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <math.h>
#include <random>
#include <chrono>
#include <assert.h>
using namespace std ;
using ll = long long ;
using ld = long double ;
template<class T> using V = vector<T> ;
template<class T> using VV = V<V<T>> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
ll mod = 1000000007 ;
long double pie = acos(-1) ;
ll INF = 1e18 ;
void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
ll extGCD(ll a,ll b,ll &x,ll &y){
if(b==0){
x = 1 ;
y = 0 ;
return a ;
}
ll d = extGCD(b,a%b,y,x) ;
y -= a/b*x ;
return d ;
}
void fix_cout(){cout << fixed << setprecision(20) ;}
template<class T> void chmax(T &a,T &b){if(a<b) a = b ;}
template<class T> void chmin(T &a,T &b){if(a>b) a = b ;}
int main(){
int n,m ;
cin >> n >> m ;
VV<pair<int,char>> g(n) ;
VV<V<ll>> edge(n,VV<ll>(26)) ;
for(int i=0;i<m;i++){
int a,b ;
char c ;
cin >> a >> b >> c ;
a-- ; b-- ;
g[a].emplace_back(b,c) ;
g[b].emplace_back(a,c) ;
edge[a][c-'a'].push_back(b) ;
edge[b][c-'a'].push_back(a) ;
}
VV<ll> dp(n,V<ll>(n,INF)) ;
dp[0][n-1] = 0 ;
queue<pair<int,int>> q ;
q.emplace(0,n-1) ;
while(!q.empty()){
int start = q.front().first ;
int goal = q.front().second ;
q.pop() ;
for(auto p:g[start]){
int nx = p.first ;
char c = p.second;
for(auto &nx2:edge[goal][c-'a']){
if(dp[nx][nx2]!=INF) continue ;
dp[nx][nx2] = dp[start][goal]+2 ;
q.emplace(nx,nx2) ;
}
}
}
ll ans = INF ;
for(int i=0;i<n;i++) ans = min(ans,dp[i][i]) ;
for(int i=0;i<n;i++) for(auto &p:g[i]) ans = min(ans,dp[i][p.first]+1) ;
if(ans==INF) ans = -1 ;
cout << ans << endl ;
} |
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define int long long
#define ld long double
#define f(i,x,n) for(int i=x;i<n;i++)
using namespace std;
const int mod=1e9+7;
const int inf=1e18;
int n,x[20],y[20],z[20];
int dp[200000][20];
int dfs(int mask,int pos) {
int &an=dp[mask][pos];
// cout<<mask<<endl;
if(an!=-1)
return an;
if(mask==((1ll<<n)-1)) {
return abs(x[0]-x[pos])+abs(y[0]-y[pos])+max(0ll,z[0]-z[pos]);
}
an=inf;
f(i,0,n) {
if((mask&(1ll<<i)) ==0 ) {
// cout<<i<<endl;
an=min(an,abs(x[pos]-x[i])+abs(y[pos]-y[i])+max(0ll,z[i]-z[pos])+dfs(mask|(1ll<<i),i) );
}
}
return an;
}
void solve() {
f(i,0,200000)
f(j,0,18)
dp[i][j]=-1;
cin>>n;
f(i,0,n) {
cin>>x[i]>>y[i]>>z[i];
}
cout<<dfs(1,0)<<'\n';
}
int32_t main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t=1;
// cin>>t;
while(t--) {
solve();
}
return 0;
} | #include<bits/stdc++.h>
#define int long long
#define ld long double
#define fi first
#define se second
#define vll vector<int>
#define pii pair<int,int>
#define pb push_back
#define sz(v) (int)(v).size()
#define inf (int)(1e18)
#define md (int)(998244353)
#define all(v) (v).begin(),(v).end()
#define rep(i,a,b) for(int i=a;i<b;++i)
using namespace std;
const int M = 3e5 + 10;
int ex(int a,int b=md-2){
int ans=1;
while(b) {
if(b&1) ans=(ans*a)%md;
a=(a*a)%md,b/=2;
}
return ans;
}
inline int add(int a,int b){
a+=b;
if(a>=md) a-=md;
return a;
}
inline int sub(int a,int b){
a-=b;
if(a<0) a+=md;
return a;
}
inline int mul(int a,int b){
return (a*b)%md;
}
int f[M],iv[M];
int C(int n,int r){
if(n<r) return 0;
return mul(f[n],mul(iv[r],iv[n-r]));
}
void init(){
f[0]=1;
for(int i=1;i<M;++i) {
f[i]=mul(i,f[i-1]);
}
iv[M-1]=ex(f[M-1]);
for(int i=M-2;i>=0;--i) {
iv[i]=mul(i+1,iv[i+1]);
}
}
int spf[M];
int get(int n,int x) {
int ans=1;
while(x>1) {
int p=spf[x],cnt=0;
while(x%p==0) x/=p,++cnt;
ans=mul(ans,C(cnt+n-1,n-1));
}
return ans;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
init();
spf[1]=1;
rep(i,2,M) if(!spf[i])
for(int j=i;j<M;j+=i) if(!spf[j]) spf[j]=i;
int n,m;
cin>>n>>m;
int ans=0;
rep(i,1,m+1) ans=add(ans,get(n,i));
cout<<ans;
return 0;
} |
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
vector<vector<int>> cnt(200, vector<int>(0));
for(int i = 0; i < N; i++) cin >> A[i];
int tg = min(N, 8);
for(int i = 0; i < (1 << tg); i++) {
int sum = 0;
vector<int> s;
for(int j = 0; j < tg; j++) {
if(i & (1 << j)) {
s.push_back(j + 1);
sum = (sum + A[j]) % 200;
}
}
if(cnt[sum].size() != 0) {
cout << "Yes" << endl;
cout << s.size();
for(auto e : s) cout << " " << e;
cout << endl;
cout << cnt[sum].size() << " ";
for(auto e : cnt[sum]) cout << e << " ";
cout << endl;
return 0;
} else {
cnt[sum] = s;
}
}
cout << "No" << endl;
return 0;
} | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
//#define ACL
#ifdef ACL
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#if __has_include("acl-all.h")
#include "acl-all.h" //on Wandbox
using namespace atcoder;
#endif
#endif //#ifdef ACL
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<ll, ll> > vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
typedef vector<vector<bool> > vvb;
typedef pair<ll, ll> pll;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = 1; i <= int(n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = int(n)-1; i >= 0; --i)
#define MES(a) MES2 a
#define MES2(a0,a1,a2,a3,a4,x,...) x
#define mes_1(x1) cout<<x1<<endl
#define mes_2(x1,x2) cout<<x1<<" "<<x2<<endl
#define mes_3(x1,x2,x3) cout<<x1<<" "<<x2<<" "<<x3<<endl
#define mes_4(x1,x2,x3,x4) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<endl
#define mes_5(x1,x2,x3,x4,x5) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<" "<<x5<<endl
#define mes(...) CHOOSE((__VA_ARGS__,mes_5,mes_4,mes_3,mes_2,mes_1,~))(__VA_ARGS__)
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#define Ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define Rev(a) reverse(a.begin(), a.end())
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
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; }
struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } };
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
ll fact_mod(ll n, ll mod) {
ll f = 1; for (ll i = 2; i <= n; i++) f = f * (i % mod) % mod;
return f;
}
ll modpow(ll x, ll n, ll mod) {
if(n == 0) return 1;
ll res = modpow((x * x) % mod, n / 2 , mod);
if(n & 1) res = (res * x) % mod;
return res;
}
ll modncr(ll n, ll r, ll mod) {
if(r > n-r) r = n-r;
if(r == 0) return 1;
ll a = 1;
rep(i, r) a = a * ((n-i) % mod) % mod;
ll b = modpow(fact_mod(r, mod), mod-2, mod);
return (a % mod) * (b % mod) % mod;
}
signed main() {
ll n;
cin >> n;
ll nn = (ll)sqrt(n)+1;
ll w = 10000 / nn;
ll cnt = 0;
rep(i, nn) {
rep(j, nn) {
mes(j*w, i*w, (j+1)*w-1, (i+1)*w-1);
cnt++;
if (cnt == n) {
re0;
}
}
}
}
|
/* Author: rrrr_wys
**/
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
typedef double db;
typedef long long ll;
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
const int N = 200100;
const int P = 1e9 + 7;
ll n, f[1223][1222];
int nn[222], vis[N];
vector<int> op;
void op1() { op.pb(1); }
void op2() { op.pb(2); }
void op3() { op.pb(3); }
void op4() { op.pb(4); }
vector<vector<int>> ops;
int mn = 200;
vector<pii> vv, v;
void dfs(ll n, int num, int I) {
if (n == 0) {
if (mn > num) {
mn = num;
v = vv;
}
return;
}
if (num > mn)
return;
rep(k, 1, 1) {
per(i, min(nn[k], I), 2) {
if (f[k][i] <= n) {
vv.pb({k, i});
dfs(n - f[k][i], num + k, i);
vv.pop_back();
break;
}
}
}
}
signed main() {
cin >> n;
ll tn = n;
rep(k, 1, 130) {
f[k][1] = 0;
f[k][2] = k;
for (int i = 3;; ++i) {
f[k][i] = f[k][i - 1] + f[k][i - 2];
if (f[k][i] >= n) {
nn[k] = i;
break;
}
}
}
// rep(k, 1, 130) {
// per(i, nn[k], 2) if (f[k][i] <= n) {
// vv.pb({k, i});
// dfs(n - f[k][i], k + i - 2, i);
// vv.pop_back();
// }
//}
// cout << mn << "\n";
// for (auto vx : v) {
// cout << vx.fi << "," << vx.se << "\n";
//}
int ff = 0;
per(i, nn[1], 2) if (f[1][i] <= n) {
if (!ff) {
ff = 1;
if (i & 1) {
op2();
rep(k, 3, i) {
if (k & 1)
op3();
else
op4();
}
} else {
op1();
rep(k, 3, i) {
if (k & 1)
op4();
else
op3();
}
}
} else {
int pp = op.size() - i + 1;
if (i & 1)
vis[pp] = 2;
else
vis[pp] = 1;
}
n -= f[1][i];
}
vector<int> ans;
rep(i, 0, (int)op.size() - 1) {
ans.pb(op[i]);
if (vis[i])
ans.pb(vis[i]);
}
assert(ans.size() <= 130);
ll x = 0, y = 0;
cout << ans.size() << "\n";
for (int w : ans) {
cout << w << "\n";
switch (w) {
case 1:
x++;
break;
case 2:
y++;
break;
case 3:
x += y;
break;
case 4:
y += x;
break;
}
}
assert(x == tn);
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();
}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline bool read(int& a){
int s=0,w=1;
char ch=getchar();
if(ch==EOF){
return false;
}
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();
}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
a=s*w;
return true;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
puts("");
}
int n;
int ans;
signed main(){
n=read();
for(int i=1;i*i<=n;i++){
if(n%i==0&&i*i!=n){
int j=n/i;
if(i%2==1) ans++;
if(j%2==1) ans++;
}
else if(i*i==n){
if(i%2==1) ans++;
}
}
n*=2;
for(int i=1;i*i<=n;i++){
if(n%i==0&&i*i!=n){
int j=n/i;
if(i%2==0&&j%2==1) ans++;
if(j%2==0&&i%2==1) ans++;
}
else if(i*i==n){
continue;
}
}
writeln(ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define M 1000000007
#define mod 998244353
#define pll pair<ll, ll>
#define f first
#define se second
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define pb push_back
#define rb pop_back
#define prdb(x) cout << fixed << setprecision(10) << x;
#define speed1 ios_base ::sync_with_stdio(false);
#define speed2 cin.tie(NULL);
#define endl "\n"
/************
* fill the array with desired value
fill(all(vec), 1);
*fills the dsu parent array consecutively starting from desired value
iota(all(parent), 0);
* sort the vector and then make all elements unique
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
************/
ll power(ll x, ll y)
{
if (y == 0)
return 1;
ll z = power(x, y / 2);
z = z * z;
if (y & 1)
return (z * x);
return z;
}
ll solve(vector<ll> &v, ll ind)
{
if (ind == -1)
return 1;
if (v[ind])
return solve(v, ind - 1);
return solve(v, ind - 1) + power(2, ind + 1);
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//#endif
speed1;
speed2;
ll t = 1;
// cin >> t;
while (t--)
{
ll n, i, j, k;
cin >> n;
vector<ll> v(n);
for (i = 0; i < n; i++)
{
string s;
cin >> s;
if (s == "AND")
v[i] = 1;
else
v[i] = 0;
}
cout << solve(v, n - 1) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define closeSync ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 400005;
const int INF = 0x3f3f3f3f;
char s[MAXN];
inline void solve()
{
int n;
cin >> n;
string str; cin >> str;
str = " " + str;
for (int i=1;i<=(n<<1);i++)
s[i] = str[i];
int q;
cin >> q;
bool sp = false;
while (q--)
{
int t,a,b;
cin >> t >> a >> b;
if (a > b) swap(a,b);
if (t == 1)
{
// || ()
if (sp)
{
if ((a <= n && b <=n) )
{
swap(s[n+a],s[n+b]);
}
else if (a>n && b>n)
{
swap(s[a-n],s[b-n]);
}
else
{
swap(s[n+a],s[b-n]);
}
}
else
swap(s[a],s[b]);
}
else
sp = !sp;
}
if (!sp)
{
for (int i=1;i<=(n<<1);i++)
{
cout << s[i];
}
cout << endl;
}
else
{
for (int i=n+1;i<=(n<<1);i++)
cout << s[i];
for (int i=1;i<=n;i++)
cout << s[i];
cout << endl;
}
return ;
}
int main()
{closeSync;
// int T;
// cin >> T;
// while (T--)
solve();
return 0;
}
/*
3 4 4
GPLT
PATA
OMSA
*/ |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N; cin >> N;
vector<int> data(N);
vector<long long> r(200, 0);
for (int i = 0; i < N; ++i) {
cin >> data.at(i);
r[data.at(i) % 200]++;
}
long long ans = 0;
//for (auto n : r) {
// cout << n.first <<"," << n.second << endl;
//}
for (int i = 0; i < 200; ++i) {
ans += (r[i] * (r[i] - 1) / 2);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
/*
#include <atcoder/all>
using namespace atcoder;
*/
#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
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;
}
//グラフ関連
struct Edge {
ll to, cap, rev;
Edge(ll _to, ll _cap, ll _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
G[from].push_back(Edge(to, cap, (ll)G[to].size()));
if(revFlag)
G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}
void solve() {
ll n;
cin >> n;
vec<ll> a(n), b(n);
REP(i, n) {
cin >> a[i];
}
REP(i, n) {
cin >> b[i];
}
vec<ll> odds, evens;
ll tmp = 0;
REP(i, n) {
tmp += a[i];
if(i % 2)
odds.push_back(b[i] - a[i]);
else
evens.push_back(b[i] - a[i]);
}
sort(all(odds), greater<ll>());
sort(all(evens), greater<ll>());
ll ans = tmp;
REP(i, n / 2) {
tmp += odds[i] + evens[i];
chmax(ans, tmp);
}
cout << ans << en;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
/*
ll t;
cin >> t;
while(t--)*/
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int NN = 2e5 + 5;
const int mo = 1e9 + 7;
const ld eps = 1e-9;
ll n;
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
ll x = n % 100;
x = 100 - x;
cout << x << "\n";
}
| #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <numeric>
using namespace std;
using ll = long long;
//ループ
#define REP(i, n) for(int i = 0; i < n; ++i)
#define REPR(i, n) for(int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for(int i = m; i < n; ++i)
//コンテナ
#define all(x) (x).begin(),(x).end()
//関数
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; }
//-----global variable------------
//-----function definition--------
//-----main-----------------------
int main() {
//----code here------------
int X;
cin >> X;
cout << ( 100 - ( X % 100 ) ) << endl;
//----return---------------
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long
#define t() int t;cin>>t;while(t--)
#define lt() long long int t;cin>>t;while(t--)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define all(x) begin(x),end(x)
#define nik(i,k,n) for ( long long int i = k; i < (n); ++i)
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
ll gcd(ll A , ll B)
{
if(B == 0)return A;
return gcd(B , A%B);
}
ll fact(ll n){
ll ans = 1;
for(ll i=1;i<=n;i++){
ans*=i%mod;
}
return ans%mod;
}
bool checkperfectsquare(int n)
{
if (ceil((double)sqrt(n)) == floor((double)sqrt(n))) {
return true;
}
return false;
}
bool isPowerOfTwo(ll n)
{
if (n == 0)
return 0;
while (n != 1)
{
if (n%2 != 0)
return 0;
n = n/2;
}
return 1;
}
void fast()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
char s[500005];
void solve()
{
int n;
cin >> n;
vector<pair<int, string > > v;
vector<int> t;
while(n--){
string s;
int h;
cin >> s >> h;
t.pb(h);
v.push_back({h , s});
}
sort(t.rbegin() , t.rend());
int x = t[1];
for(auto p : v){
if(p.first == x){
cout << p.second << endl;
}
}
}
int32_t main()
{
IOS;
//lt(){
solve();
//}
return 0;
}
| #include<cstdio>
#include<algorithm>
using namespace std;
int Read()
{
int Output=0;
char Input=getchar();
while(Input<'0'||Input>'9')
Input=getchar();
while(Input>='0'&&Input<='9')
{
Output=Output*10+Input-'0';
Input=getchar();
}
return Output;
}
int N;
char Name[1001][16];
int T[1001];
int Max,Sec;
int main()
{
N=Read();
for(int i=1;i<=N;++i)
{
scanf("%s",Name[i]);
T[i]=Read();
if(T[i]>T[Max])
{
Sec=Max;
Max=i;
}
else if(T[i]>T[Sec])
Sec=i;
}
printf("%s",Name[Sec]);
} |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int read()
{
char c;
int w=1;
while((c=getchar())>'9'||c<'0')if(c=='-')w=-1;
int ans=c-'0';
while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0';
return ans*w;
}
int n;
ll X;
int a[105];
int b[105];
int f[105][105][105];
int vis[105][105][105];
int mod;
int red(int x)
{
return x>=mod?x-mod:x;
}
void check(int a,int b,int c)
{
if(vis[a][b][c]!=mod)
{
vis[a][b][c]=mod;
f[a][b][c]=-1e9;
}
}
int main(){
// freopen("a.in","r",stdin);
cin>>n>>X;
for(int i=1;i<=n;i++)a[i]=read();
ll ans=1e18;
for(mod=1;mod<=n;mod++)
{
for(int i=1;i<=n;i++)b[i]=a[i]%mod;
vis[0][0][0]=mod;
for(int x=0;x<n;x++)
{
for(int i=max(0,mod-n+x);i<=x;i++)
{
for(int j=0;j<mod;j++)
{
if(vis[x][i][j]!=mod)continue;
// cout<<x<<" "<<i<<" "<<j<<" "<<f[x][i][j]<<endl;
int o=red(j+b[x+1]);
check(x+1,i,j);
check(x+1,i+1,o);
f[x+1][i][j]=max(f[x+1][i][j],f[x][i][j]);
f[x+1][i+1][o]=max(f[x+1][i+1][o],f[x][i][j]+a[x+1]);
}
}
}
int s=X%mod;
if(vis[n][mod][s]!=mod)continue;
// cout<<mod<<" "<<f[n][mod][s]<<endl;
ans=min(ans,(X-f[n][mod][s])/mod);
}
cout<<ans<<endl;
return 0;
} | #include<bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
typedef std::vector<ll> vi;
typedef std::vector<std::vector<ll> > vv;
typedef std::vector<std::pair<ll,ll> > pii;
#define mod 1000000007
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fo(i,a,b) for(i=a;i<b;i++)
#define forr(i,a,b) for(i=a;i>=b;i--)
#define mp make_pair
#define pb(x) push_back(x)
#define fi first
#define se second
#define print(vec,a,b) for(ll i=a;i<b;i++) cout<<vec[i]<<" ";cout<<endl;
#define all(a) a.begin(),a.end()
#define input(vec,a,b) for(ll i = a;i<b;i++) cin>>vec[i];
#define ms(a,val) memset(a,val,sizeof(a))
using namespace std;
const int N = 2e5+ 5;
ll expo_pow(ll x, ll y) {
if (y == 0)
return 1;
y = y % (mod - 1);
x %= mod;
if (y == 0)
y = mod - 1;
ll res = 1;
while (y) {
if (y & 1)
res = (res * x) % mod;
x = (x * x) % mod;
y >>= 1;
}
return res;
}
ll modInverse(ll a, ll m = mod) {
return expo_pow(a, m - 2);
}
using matrix = vector<vector<ll>>;
matrix matMul(matrix A, matrix B) {
//assert((int)A[0].size() == (int)B.size());
matrix res((ll)A.size(), vector<ll>((ll)B[0].size(), 0));
for (int i = 0; i < (int)A.size(); ++i) {
for (int j = 0; j < (int)B[0].size(); ++j) {
for (int k = 0; k < (int)A[0].size(); ++k) {
res[i][j] += (A[i][k] * B[k][j]) % mod;
res[i][j] %= mod;
}
}
}
return res;
}
matrix matPow(matrix M, int p) {
if (p == 1) return M;
if (p % 2) return matMul(M, matPow(M, p - 1));
matrix X = matPow(M, p / 2);
return matMul(X, X);
}
void solve(){
ll i,j,n,m,k;
cin>>n>>m>>k;
vv a(1,vi(n,0));
input(a[0],0,n);
vv adj(n,vector<ll>(n,0));
vi deg(n,0);
fo(i,0,m)
{
ll x,y;
cin>>x>>y;
x--,y--;
adj[x][y] = 1;
adj[y][x] = 1;
deg[x]++;
deg[y]++;
}
ll inv = modInverse(2*m,mod);
vv M(n,vi(n,0));
if(k == 0)
{
print(a[0],0,n);
return ;
}
fo(i,0,n)
{
fo(j,0,n)
{
if(i == j)
{
M[i][j] = (1 - (deg[i]*inv)%mod + mod)%mod;
}
else
{
M[i][j] = inv*adj[i][j];
}
}
}
M = matPow(M, k);
a = matMul(a,M);
fo(i,0,n)
{
cout<<a[0][i]<<endl;
}
}
int main()
{
IO;
ll t=1,i;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
|
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
//vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#ifdef DEBUG
#else
#define cerr if (false) cerr
#endif
template <typename T> istream& operator>>(istream& in, vector <T>& a) {for (auto& i : a) in >> i; return in;}
template <typename T> ostream& operator<<(ostream& out, vector <T>& a) {for (auto& i : a) out << i << " "; return out;}
template <typename T, typename U> void chkmin(T& a, U b) {if (a > b) a = b;}
template <typename T, typename U> void chkmax(T& a, U b) {if (a < b) a = b;}
const int MOD = 998244353;
int add(int x, int y) {
x += y;
if (x >= MOD)
return x - MOD;
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0)
return x + MOD;
return x;
}
int mul(int x, int y) {
return (1LL * x * y) % MOD;
}
int bin_pow(int a, int p) {
if (!p)
return 1;
if (p % 2)
return (1LL * bin_pow(a, p - 1) * a) % MOD;
int t = bin_pow(a, p / 2);
return (1LL * t * t) % MOD;
}
int inv(int x) {
return bin_pow(x, MOD - 2);
}
class Dsu {
private:
int n;
vector <int> p, sz;
public:
Dsu(int _n) {
n = _n;
p.resize(n, -1);
sz.resize(n, 1);
}
int get(int v) {
if (p[v] == -1)
return v;
return p[v] = get(p[v]);
}
void unite(int v, int u) {
v = get(v), u = get(u);
if (v == u)
return;
if (sz[v] > sz[u])
swap(v, u);
p[v] = u;
sz[u] += sz[v];
}
int get_size(int comp) const {
return sz[comp];
}
};
vector <int> f;
int n, limit;
int solve(vector <vector <int>> a) {
Dsu gr(sz(a));
forn (i, 0, n) {
forn (j, i + 1, n) {
bool ok = true;
forn (k, 0, n) {
if (a[i][k] + a[j][k] > limit)
ok = false;
}
if (ok)
gr.unite(i, j);
}
}
set <int> same;
forn (i, 0, n)
same.insert(gr.get(i));
int ans = 1;
for (int i : same) {
ans = mul(ans, f[gr.get_size(i)]);
cerr << i << " ";
}
cerr << "\n";
return ans;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> limit;
vector <vector <int>> a(n, vector <int> (n));
cin >> a;
f.resize(1e5);
f[0] = 1;
forn (i, 1, sz(f))
f[i] = mul(f[i - 1], i);
int t = solve(a);
forn (i, 0, n)
forn (j, i + 1, n)
swap(a[i][j], a[j][i]);
cerr << t << "\n";
cout << mul(t, solve(a));
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// using mint = long double;
// using mint = modint998244353;
// using mint = modint1000000007;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> T;
typedef pair<ll, vector<ll>> Pd;
const ll INF = 2e18;
const ll fact_table = 1200008;
priority_queue<ll> pql;
priority_queue<P> pqp;
// big priority queue
// priority_queue <ll, vector<ll>, greater<ll> > pqls;
priority_queue<P, vector<P>, greater<P>> pqps;
// small priority queue
// top pop
ll dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
ll dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
//↓,→,↑,←
/*
#define endl "\n"
#ifdef ENJAPMA
#undef endl
#endif
*/
#define p(x) cout << x << endl;
#define el cout << endl;
#define pe(x) cout << x << " ";
#define ps(x) cout << fixed << setprecision(25) << x << endl;
#define pu(x) cout << (x);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pc(x) cout << x << ",";
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, a, b) for (ll i = a; i <= (b); i++)
#define rep3(i, a, b) for (ll i = a; i >= (b); i--)
#define all(c) begin(c), end(c)
typedef vector<ll> vec;
typedef vector<vector<ll>> mat;
// vec v(n) -> 長さnのベクトルを宣言
// mat dp(h, vec(w)) -> h * w の行列を宣言
const ll mod = 998244353ll;
// const ll mod = 1000000007ll;
ll mypow(ll a, ll b, ll m = mod) {
ll x = 1;
while (b) {
while (!(b & 1)) {
(a *= a) %= m;
b >>= 1;
}
(x *= a) %= m;
b--;
}
return x;
}
vec rv(ll read) {
vec res(read);
for (int i = 0; i < read; i++) {
cin >> res[i];
}
return res;
}
/*
ll fact[fact_table + 5], rfact[fact_table + 5];
void c3_init() {
fact[0] = rfact[0] = 1;
for (ll i = 1; i <= fact_table; i++) {
fact[i] = (fact[i - 1] * i) % mod;
}
rfact[fact_table] = mypow(fact[fact_table], mod - 2, mod);
for (ll i = fact_table; i >= 1; i--) {
rfact[i - 1] = rfact[i] * i;
rfact[i - 1] %= mod;
}
return;
}
ll c3(ll n, ll r) {
return (((fact[n] * rfact[r]) % mod) * rfact[n - r]) % mod;
}
*/
bool icpc = false;
bool multicase = false;
ll n, k;
ll dist = 0, cnt = 0;
vec G[500005];
P dfs(int v, int p) {
// first : 一番近い黒までの距離
// second : 一番遠い白までの距離
ll black = INF, white = 0;
for (auto nv : G[v]) {
if (nv == p) continue;
auto [b, w] = dfs(nv, v);
b++;
w++;
black = min(black, b);
white = max(white, w);
}
if (white + black <= dist) {
white = -1;
}
if (white == dist) {
cnt++;
white = -1, black = 0;
}
if (p == -1 && white >= 0) {
cnt++;
}
return P(black, white);
}
int calc(ll mid) {
dist = mid;
cnt = 0;
P res = dfs(1, -1);
return cnt;
}
bool solve() {
cin >> n >> k;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
G[a].pb(b);
G[b].pb(a);
}
ll ok = n, ng = -1;
while (abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if (calc(mid) <= k) {
ok = mid;
} else {
ng = mid;
}
}
p(ok);
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
if (icpc) {
while (solve())
;
return 0;
}
ll q, testcase = 1;
if (multicase) {
cin >> q;
} else {
q = 1;
}
while (q--) {
solve();
testcase++;
}
// solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=1000010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;
//dijkstra(O(ElogV))
//0-indexed
void dijkstra(ll n,ll s,vector<vector<pair<ll,pll> > >&G,vector<ll> &d){
priority_queue<pll,vector<pll>,greater<pll> >pq;
rep(i,n){
d[i]=inf;
}
d[s]=0;
pq.push(mp(0,s));
while(!pq.empty()){
pll k=pq.top();pq.pop();
ll v=k.second;
if(d[v]<k.first)continue;
for(auto e:G[v]){
ll x=e.se.fi,y=e.se.se;
ll nxt=x+y/(d[v]+1);
if(y>0){
ll z=sqrt(y);
if(z>=d[v]){
nxt=min(nxt,x+min(y/(z+1)+z-d[v],y/(z+2)+1+z-d[v]));
}
if(z-1>=d[v]){
nxt=min(nxt,x+y/z+z-1-d[v]);
}
}
if(d[e.first]>d[v]+nxt){
d[e.first]=d[v]+nxt;
pq.push(mp(d[e.first],e.first));
}
}
}
}
signed main(){
ll n,m;cin>>n>>m;
vector<vector<pair<ll,pll> > >G(n);
rep(i,m){
ll a,b,c,d;cin>>a>>b>>c>>d;
a--;b--;
G[a].pb(mp(b,mp(c,d)));
G[b].pb(mp(a,mp(c,d)));
}
vector<ll>d(n);
dijkstra(n,0,G,d);
ll ans=d[n-1];
if(ans==inf)ans=-1;
cout<<ans<<endl;
} | #include<bits/stdc++.h>
using namespace std;
const int N = 1e5+100;
typedef long long ll;
const int inf = 0x3f3f3f3f;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)
int n,l;
int a[N],b[N];
int pos[N];
set<pii> s1;
int main(){
scanf("%d%d",&n,&l);
fr(i,1,n)scanf("%d",a+i);
fr(i,1,n)scanf("%d",b+i);
fr(i,1,n){
s1.insert(pii(a[i]-i,i));
}
a[n+1] = l+1;
s1.insert(pii(0,0)); s1.insert(pii(a[n+1]-n-1,n+1));
ll ans = 0;
bool flag = true;
fr(i,1,n){
if(b[i] <= a[i]){
pii cj = *(--s1.upper_bound(pii(b[i]-i,i)));
if(cj.first != b[i]-i){
flag = false;
} else pos[i] = cj.second;
} else {
pii cj = *s1.lower_bound(pii(b[i]-i,i));
if(cj.first != b[i]-i){
flag = false;
} else pos[i] = cj.second;
}
}
if(!flag){
puts("-1"); return 0;
}
pos[n+1] = pos[0] = 1145141919;
fr(i,1,n){
if(pos[i] == i)continue ;
if(pos[i] < i){
if(pos[i] == pos[i+1])continue ;
else ans += i-pos[i];
} else {
if(pos[i] == pos[i-1])continue ;
else ans += pos[i]-i;
}
}
printf("%lld\n",ans);
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
using namespace std;
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; }
long long mod= 1000000007;
vector<pair<int, int> > prime_factorize(int N) {
vector<pair<int, int> > res;
for (int a = 2; a * a <= N; ++a) {
if (N % a != 0) continue;
int ex = 0; // 指数
// 割れる限り割り続ける
while (N % a == 0) {
++ex;
N /= a;
}
// その結果を push
res.push_back({a, ex});
}
// 最後に残った数について
if (N != 1) res.push_back({N, 1});
return res;
}
int main(){
int N;
cin >> N;
unsigned long long ans = 1;
int prime[] = {2,3,5,7,11,13,17,19,23,29};
unordered_map<int, int> um;
for(int i=2; i <=N; ++i){
vector<pair<int, int> > v = prime_factorize(i);
for(int j=0; j<v.size(); ++j){
pair<int, int> p = v[j];
int prime = p.first;
int num = p.second;
if(um.find(prime) == um.end()){
um[prime] = num;
}
else{
if(um[prime] < num){
um[prime] = num;
}
}
}
}
for(auto a:um){
for(int i=0; i<a.second; ++i){
ans *= a.first;
}
}
ans +=1;
cout << ans << endl;
return 0;
} | #pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define bg begin()
#define en end()
#define endl "\n"
#define vvl(n,m) vector<vector<ll> > a( n , vector<ll> (m))
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mod 1000000007
using namespace std;
ll power(ll x, ll y, ll m)
{
if (y == 0)
return 1;
ll p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
ll modInverse(ll a, ll m)
{
return power(a, m - 2, m);
}
ll knapsack(vector<vector<ll>>&dp,ll x,ll i,ll n,ll s[],ll h[]){
ll r,l;
if(i>=n || x==0)
return 0;
if(dp[i][x]!=-1)
return dp[i][x];
if(x>=h[i]){
l=s[i]+knapsack(dp,x-h[i],i+1,n,s,h);
r=knapsack(dp,x,i+1,n,s,h);
return dp[i][x]=max(l,r);
}
else
return dp[i][x]=knapsack(dp,x,i+1,n,s,h);
}
void solve()
{
ll r=1;
r*=16*27*25*7*11*13;
r*=(17*19*23*29);
r++;
cout<<r;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast
solve();
#ifndef ONLINE_JUDGE
cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<LD> VLD;
typedef vector<string> VS;
#define rep(i, max) for (LL i = 0; i < (LL)(max); i++)
#define repm(i, min, max) for (LL i = (LL)(min); i < (LL)(max); i++)
#define rev(i, max) for (LL i = (LL)(max) - 1; i >= 0; i--)
#define revm(i, min, max) for (LL i = (LL)(max) - 1; i >= (LL)(min); i--)
int main()
{
LL N;
cin>>N;
LL a = 3;
int p = 1;
while(a < N){
LL b = 5;
int q = 1;
while(b < N){
if(a + b == N){
cout<<p<<" "<<q<<endl;
return 0;
}
q++;
b *= 5;
}
p++;
a *= 3;
}
cout<<-1<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define rep(i,n) for (int i=0; i<(n);++i)
ll n,p,q,mod=pow(10,9)+7;
int main(){
cin >> n;
p = n%5;
if(p==0){
cout << -1;
return 0;
}
if(p==1) q=4;
else if(p==2) q=3;
else if(p==3) q=1;
else q=2;
ll t=1,ans=q,ans2=0;
set<ll> s;
while(true){
if(t<pow(10,18)){
t*=5;
s.insert(t);
}
else break;
}
q = pow(3,q);
rep(i,20){
if(n>q){
if(s.count(n-q)){
ll r=n-q;
while(true){
if(r%5==0){
ans2++;
r/=5;
}
else break;
}
cout << ans << ' ' << ans2 << endl;
return 0;
}
}
else{
cout << -1;
return 0;
}
q*=81;
ans+=4;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<=b;i++)
using LL = long long;
const int Nmax=2e5+9;
int N,M,A[109],B[109],C[19],D[19],K,ans,Ball[109],tmp;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>N>>M;
REP(i,1,M) cin>>A[i]>>B[i];
cin>>K;
REP(i,1,K) cin>>C[i]>>D[i];
REP(j,0,(1<<K)-1) {
fill(Ball+1,Ball+N+1,0);
REP(k,1,K) {
if ((j>>(k-1))&1) Ball[C[k]]=1;
else Ball[D[k]]=1;
}
tmp=0;
REP(i,1,M) {
if (Ball[A[i]] && Ball[B[i]]) ++tmp;
}
ans=max(ans,tmp);
}
cout<<ans;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define int long long
#define vi std::vector<int>
#define setbits(x) __builtin_popcountll(x)
#define pb push_back
#define fn 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 ret(x) {cout << #x << endl; return;}
#define PI 3.14159265358979323846
#define pii pair<ll,ll>
#define fr first
#define sc second
const int mod = 1e+7;
void getio();
ll ceill(ll a, ll b) { return (a + b - 1) / b; }
ll fastpow(ll x, ll n)
{
if (n == 0)
return 1;
ll res = 1;
while (n)
{
if (n % 2)
res = res * x;
x = x * x;
n = n >> 1;
}
return res;
}
// Function to generate all binary strings
vector<vector<int>> store;
void generateAllBinaryStrings(int n, vi &arr, int i)
{
if (i == n) {
// printTheArray(arr, n);
store.pb(arr);
return;
}
// First assign "0" at ith position
// and try for all other permutations
// for remaining positions
arr[i] = 0;
generateAllBinaryStrings(n, arr, i + 1);
// And then assign "1" at ith position
// and try for all other permutations
// for remaining positions
arr[i] = 1;
generateAllBinaryStrings(n, arr, i + 1);
}
void solve()
{
int n, m;
cin >> n >> m;
vector<pair<int, int>> use, chos;
vector<pair<int, int>> ar;
rep(i,0,m)
{
int a, b;
cin >> a >> b;
ar.push_back({a, b});
}
int k;
cin >> k;
rep(i,0,k)
{
int c, d;
cin >> c >> d;
chos.pb({c, d});
}
vi per(k);
generateAllBinaryStrings(k, per, 0);
// cout << store.size();
int ans = 0;
int cnt = 0;
for(int i=0; i<store.size(); i++)
{
cnt=0;
vector<int> tem(n+1,0);
for(int j=0; j<k; j++)
{
if(store[i][j] == 0)
tem[chos[j].first]++;
else tem[chos[j].second]++;
}
for(int i=0; i<m; i++)
{
if(tem[ar[i].first]>=1 and tem[ar[i].second]>=1)
{
// cout << ar[i].first << ' ' << ar[i].second << endl;
cnt++;
}
}
ans = max(ans, cnt);
}
cout << ans << endl;
}
int32_t main()
{
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
getio();
// int t; cin >> t; while (t--)
solve();
}
void getio()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rep1(i,n) for(int i = 1; i <= (n); ++i)
// #define MOD 998'244'353
// #define MOD 1'000'000'007
// #define INF 1'000'000'000'000'000'000
using namespace std;
// using namespace atcoder;
typedef long long ll;
int main(){
ll N;
cin >> N;
int M;
cin >> M;
if (M == 0){
cout << 1 << endl;
return 0;
}
vector<ll> A(M+1);
A[0] = 0;
rep1(i, M) cin >> A[i];
if (N == M){
cout << 0 << endl;
return 0;
}
sort(A.begin(), A.end());
vector<ll> dif(M+1);
rep(i, M){
dif[i] = A[i+1] - A[i] - 1;
}
dif[M] = N - A[M];
sort(dif.begin(), dif.end());
int pos = 0;
while(dif[pos] == 0){
pos++;
}
ll k = dif[pos];
// cout << k << endl;
ll ans = 0;
for(int i = pos; i <= M; i++){
if (dif[i] % k == 0){
ans += dif[i] / k;
}else{
ans += (dif[i] / k + 1);
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <initializer_list>
#include <cmath>
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPS(i, a, b) for(int i = (a); i <= (b); i++)
#define REPR(i, n) for(int i = (n) - 1; i >= 0; i--)
#define REPRS(i, b, a) for(int i = (b) ; i >= (a); i--)
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
constexpr static int MOD = 1e8 + 7;
inline void scan() {}
template <typename T, typename... U>
inline void scan(T&& t, U&&... u) {
std::cin >> t;
scan(std::forward<U>(u)...);
}
template <typename T>
inline void scana(T t, int n) {
for (int i = 0; i < n; i++) {
std::cin >> t[i];
}
}
template <typename T>
inline void scana(T t, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
std::cin >> t[i][j];
}
}
}
inline void print() {
std::cout << "\n";
}
template <typename T, typename... U>
inline void print(T&& t, U&&... u) {
std::cout << t;
print(std::forward<U>(u)...);
}
using graph = std::vector<std::vector<int>>;
using ll = long long;
using ull = unsigned long long;
ll pow(ll x, ll n) {
if (n == 0) return 1;
ll val = pow(x, n / 2);
val *= val;
if (n % 2 == 1) val *= x;
return val;
}
int main() {
int n;
scan(n);
int ans = static_cast<int>(std::sqrt(2 * n));
while (true) {
if ((ans * (ans + 1)) >= 2 * n) {
break;
} else {
ans++;
}
}
print(ans);
return 0;
} |
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T> void chmin(T &a, const T &b) {
if (a > b) a = b;
}
template <class T> void chmax(T &a, const T &b) {
if (a < b) a = b;
}
void add(ll &a, ll b) {
a = (a + b) % MOD;
}
void mul(ll &a, ll b) {
a %= MOD;
b %= MOD;
a = a * b % MOD;
}
void solve() {
int N;
cin >> N;
vector<ll> A(N);
rep(i, N) cin >> A[i];
vector<ll> num_dp(2, 0);
num_dp[0] = 1;
vector<ll> sum_dp(2, 0);
sum_dp[0] = A[0];
for (int i = 1; i < N; i++) {
vector<ll> nex_num_dp(2, 0);
vector<ll> nex_sum_dp(2, 0);
{
add(nex_num_dp[1], num_dp[0]);
add(nex_num_dp[0], num_dp[0]);
add(nex_num_dp[0], num_dp[1]);
}
{
add(nex_sum_dp[1], ((sum_dp[0] - A[i] * num_dp[0] % MOD) % MOD + MOD) % MOD);
add(nex_sum_dp[0], ((sum_dp[0] + A[i] * num_dp[0] % MOD) % MOD + MOD) % MOD);
add(nex_sum_dp[0], ((sum_dp[1] + A[i] * num_dp[1] % MOD) % MOD + MOD) % MOD);
}
swap(nex_num_dp, num_dp);
swap(nex_sum_dp, sum_dp);
}
ll ans = 0;
add(ans, sum_dp[0]);
add(ans, sum_dp[1]);
cout << ans << endl;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
solve();
return 0;
} | #pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int mod = 998244353;
ll binpow(ll a, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
(res *= a) %= mod;
}
p >>= 1;
(a *= a) %= mod;
}
return res;
}
void solve() {
ll n;
cin >> n;
ll g = 1;
for (ll i = 2; i <= n; i++) {
g = g * i / __gcd(i, g);
}
cout << g + 1 << '\n';
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case " << tc << ": ";
solve();
}
return 0;
} |
/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
* author: Daredevil666
* institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll unsigned long long int
#define REP(i,a,n) for(i=a;i<=n;i++)
#define F(i,a,b) for(i=a;i<b;i++)
#define RF(i,b,a) for(i=b;i>=a;i--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const ll M=1e9+7;
const ll MAXN=100200;
ll i,j,mask;
ll power(ll p,ll x)
{
ll res=1;
while(x>0)
{
if(x%2==1)
{
res*=p;
}
x/=2;
p*=p;
}
return res;
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE*/
ll n;
cin>>n;
for(i=1;power(3,i)<=n;i++)
{
for(j=1;power(5,j)<=n;j++)
{
if(power(3,i)+power(5,j)==n)
{
cout<<i<<" "<<j;
return 0;
}
}
}
cout<<-1;
return 0;
}
| // Author : ps41
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll M = 1e9 + 7, N = 1e6 + 5;
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr);
string s;
cin >> s;
int n = s.size();
int ch = 1;
for (int i = 0; i < n; i++)
{
if (i % 2)
{
if (s[i] < 'A' or s[i] > 'Z')
{
ch = 0;
break;
}
}
else
{
if (s[i] < 'a' or s[i] > 'z')
{
ch = 0;
break;
}
}
}
if (ch)
cout << "Yes\n";
else
cout << "No\n";
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
// typedef long long int;
int const N = 4e6 + 10, mod = 1e9 + 7;
int fact[N], infact[N];
int n, m, k;
int qmi(int a, int k, int p) {
int res = 1;
while (k) {
if (k & 1) res = res * a % p;
k >>= 1;
a = a * a % p;
}
return res;
}
// 预处理
void init() {
fact[0] = infact[0] = 1;
for (int i = 1; i < N; ++i) {
fact[i] = (int)fact[i - 1] * i % mod;
infact[i] = (int)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
}
}
int cnm(int a, int b) {
return (int)fact[a] * infact[b] % mod * infact[a - b] % mod;
}
signed main() {
init(); // 预处理
cin >> n >> m >> k; // c(n + m, n) - c(n + m, n - k - 1)
if (n >= m + k + 1) cout << 0;
else cout << ((cnm(n + m, n) % mod - cnm(n + m, m + k + 1) % mod) % mod + mod) % mod ;
return 0;
} | // title
#include <cstdint>
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;
}
};
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
#define prime 1000000007
modint<prime> path(int x, int y){
if(x<0 || y<0){
return 0;
}
modint<prime> ans = 1;
rep(i,x+y){
ans *=i+1;
}
rep(i,x){
ans /=i+1;
}
rep(i,y){
ans /=i+1;
}
return ans;
}
int main() {
int N,M,K;
cin >> N >> M >> K;
if(N > M+K){
cout << "0\n";
return 0;
}
int ans = (path(N,M) - path(N-(K+1), M+(K+1))).value();
cout << ans << endl;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;
int main(){
int n;
cin >> n;
cout << n-1 << endl;
} | #include <iostream>
using namespace std;
int main() {
int n;
while(cin>>n){
cout<<n-1<<endl;
}
} |
#include<iostream>
#include<algorithm>
#include<ctime>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#include<numeric>
#include<queue>
#include<deque>
#include<cfloat>
#include<functional>
#include<tuple>
#include<math.h>
#include<bitset>
#include<stack>
#include<set>
#include<random>
#include<stdlib.h>
#define rip(i,n) for(int i=0;i<n;i++)
#define Rip(i,n) for(int i=1;i<=n;i++)
#define RIP(i,a,b) for(int i=a;i<b;i++)
#define all(V) V.begin(),V.end()
#define sec setprecision;
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx")
#pragma optimize("O3")
#pragma optimize("unroll-loops");
constexpr double eps = 1e-9;
constexpr double pi = 3.141592653589793238462643383279;
using namespace std;
using ll = long long;
using ld = long double;
using Pa = pair<ll, ll>;
const ll MOD = 1000000007;
ll gcd(ll a, ll b)
{
if (b == 0) return a;
else return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return a * b / gcd(a, b);
}
ll pow(ll a, ll b) {
ll ans = 1;
if (b <= 0)return 1;
else if (b % 2 == 0)return(pow(a * a, b / 2));
else return (a * pow(a, b - 1));
}
ll modpow(ll x, ll n, ll mod) {
x %= mod;
if (n == 0)
return 1;
if (x == 1)return 1;
if (n % 2 == 0)
return (modpow((x * x) % mod, n / 2, mod) % mod);
else
return (x * modpow(x, n - 1, mod)) % mod;
}
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;
}
};
void solveA() {
int x, y, z; cin >> x >> y >> z;
int num = 0;
for (int i = 1000000; i >= 0; i--)if (y * z > x* i) {
num = i; break;
}
cout << num << endl;
}
void solveB() {
int n, m; cin >> n >> m;
vector<int>a(1005, 0);
vector<int>b(1005, 0);
vector<int>ab(1005, 0);
rip(i, n) {
int d; cin >> d;
a[d]++, ab[d]++;
}
rip(i, m) {
int e; cin >> e;
b[e]++, ab[e]++;
}
for (int i = 1; i <= 1000; i++) {
if (ab[i] != 0 && (ab[i] == a[i] || ab[i] == b[i]))cout << i << endl;
}
}
void solveC() {
int a, b; cin >> a >> b;
if (2 * a <= b)cout << b / 2 << endl;
else {
int c = b - a;
for (int i = c; i >= 1; i--) {
if (a % i == 0 || b % i == 0) {
cout << i << endl;
break;
}
else if (a/i + 1 <b/i - 1) {
cout << i << endl;
break;
}
}
}
}
void solveD() {
int N,P; cin >> N>>P;
ll ans = modpow(P - 2, N-1, MOD)*(P-1);
cout << ans % MOD << endl;
}
void solveE() {
}
void solveF() {
}
int main() {
solveD();
}
| #include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define X first
#define Y second
#define nl '\n'
#define AC return 0
#define pb(a) push_back(a)
#define mst(a,b) memset(a, b, sizeof a)
#define rep(i,n) for(int i = 0; (i)<(n); i++)
#define rep1(i,n) for(int i = 1; (i)<=(n); i++)
#define scd(a) scanf("%lld", &a)
#define scdd(a,b) scanf("%lld%lld", &a, &b)
#define scs(s) scanf("%s", s)
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const ll INF = (ll)0x3f3f3f3f3f3f3f, MAX = 9e18, MIN = -9e18;
const int N = 1e6+10, M = 2e6+10, mod = 1e9+7, inf = 0x3f3f3f;
ll a[N];
int main()
{
IOS;
string s, t;
cin>>s>>t;
if(s[0] == 'Y')
rep(i, t.size())
cout<<char(t[i] >= 'a'&&t[i] <= 'z' ? t[i]-32 : t[i]);
else
rep(i, t.size())
cout<<t[i];
AC;
} |
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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; }
struct State{
int at;
ll cost;
State(int a, ll c) : at(a), cost(c){}
bool operator>(const State& s) const{
return cost > s.cost;
}
};
struct Edge{
int to;
ll cost;
Edge(int t, ll c) : to(t), cost(c){}
};
using Graph = vector<vector<Edge> >;
const ll INF = 1e15;
void dijkstra(int s, const Graph& G, vector<ll>& minc){
minc.assign(G.size(), INF);
priority_queue<State, vector<State>, greater<State> > pq;
pq.emplace(s, 0);
minc[s] = 0;
while(!pq.empty()){
State cur = pq.top();
pq.pop();
if(minc[cur.at] < cur.cost) continue;
for(const Edge& e : G[cur.at]){
ll cost = cur.cost + e.cost;
if(minc[e.to] <= cost) continue;
minc[e.to] = cost;
pq.emplace(e.to, cost);
}
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<int> a(m), b(m);
Graph g1(n);
rep(i,m){
cin >> a[i] >> b[i];
--a[i];
--b[i];
g1[a[i]].emplace_back(b[i], 1);
g1[b[i]].emplace_back(a[i], 1);
}
int k;
cin >> k;
vector<int> c(k);
vector<vector<ll>> minc(k);
rep(i,k){
cin >> c[i];
--c[i];
dijkstra(c[i], g1, minc[i]);
}
ll ans = INF;
vector<vector<ll>> dp((1<<k), vector<ll>(k, INF));
rep(i,k) dp[0][i] = 1;
rep(S,(1<<k)){
rep(i,k){
rep(j,k){
if(S>>j & 1) continue;
int nextS = S + (1<<j);
ll cost = dp[S][i] + minc[i][c[j]];
chmin(dp[nextS][j], cost);
}
}
}
rep(j,k) chmin(ans, dp[(1<<k)-1][j]);
if(ans == INF) ans = -1;
cout << ans << endl;
return 0;
} | #include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INF = (1u<<30) - 1;
struct BFS
{
int V;
const vector<vector<int>> &g;
int s;
vector<int> dist;
vector<int> previous;
BFS(const vector<vector<int>> &g) : V(g.size()), g(g) {}
void calc(int start) {
s = start;
dist.assign(V, INF);
previous.assign(V, -1);
dist[s] = 0;
queue<int> que;
que.push(s);
while (!que.empty()) {
int v = que.front(); que.pop();
for (int nv : g[v]) {
if (dist[nv] != INF) continue;
dist[nv] = dist[v] + 1;
previous[nv] = v;
que.push(nv);
}
}
}
};
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--; b--;
g[a].push_back(b);
g[b].push_back(a);
}
int k;
cin >> k;
vector<int> C(k);
for (int i = 0; i < k; i++) {
cin >> C[i];
C[i]--;
}
vector<vector<int>> dmatrix(k, vector<int>(k, INF));
BFS bfs(g);
for (int i = 0; i < k; i++) {
bfs.calc(C[i]);
for (int j = 0; j < k; j++) {
dmatrix[i][j] = bfs.dist[C[j]];
}
}
int p = 1 << k;
vector<vector<int>> dp(p, vector<int>(k, INF));
for (int v = 0; v < k; v++) dp[0][v] = 0;
for (int i = 0; i < p; i++) {
for (int v = 0; v < k; v++) {
for (int u = 0; u < k; u++) {
dp[i][v] = min(dp[i][v], dp[i & (~(1<<v))][u] + dmatrix[v][u]);
}
}
}
int ans = INF;
for (int i = 0; i < k; i++) {
ans = min(ans, dp[p-1][i]);
}
ans++;
if (ans >= INF) ans = -1;
cout << ans << endl;
return 0;
} |
#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;
if(n == 3) {
cout << "6 10 15" << endl;
return 0;
}
set<int> se;
int now = 6;
while(now <= 100000) {
se.insert(now);
now += 6;
}
now = 10;
while(now <= 100000) {
se.insert(now);
now += 10;
}
now = 15;
while(now <= 100000) {
se.insert(now);
now += 15;
}
auto itr = se.begin();
bool f = false;
vector<int> v;
while(n--) {
if(f) cout << " ";
cout << *itr;
itr++;
f = true;
v.push_back(*itr);
}
cout << endl;
// rep(i, v.size()) {
// rep2(j, i+1, v.size()) {
// int g = gcd(v.at(i), v.at(j));
// if(g == 1) {
// cout << "!!!!" << v.at(i) << " " << v.at(j) << endl;
// return 1;
// }
// }
// }
// int g = v.front();
// rep2(i, 1, v.size()) g = gcd(g, v.at(i));
// cerr << g << 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>
using namespace std;
int main() {
int n;
cin >> n;
int m = 1;
for (int i = 0; i < n; i++) {
m *= 2;
}
vector<int> a(m);
for (int i = 0; i < m; i++) {
cin >> a[i];
}
int x = 0, y = m/2;
for (int i = 0; i*2 < m; i++) {
if (a[i] > a[x]) x = i;
if (a[i + m / 2] > a[y]) y = i + m / 2;
}
if (a[x] > a[y])
cout << y+1 << endl;
else
cout << x+1 << endl;
return 0;
} |
#include<iostream>
#include<vector>
#include<algorithm>
int main(void){
int n, t;
std::cin >> n >> t;
std::vector<long long> vec1, vec2;
vec1 = vec2 = {0};
for(int i = 0; i < n; i++){
int a;
std::cin >> a;
for(int j = vec1.size()-1; j >= 0; --j){
vec1.push_back(a+vec1[j]);
}
std::swap(vec1, vec2);
}
std::sort(vec1.begin(), vec1.end());
long long ans = 0;
for(int i = 0; i < vec2.size(); i++){
if(vec2[i] > t) continue;
int vec1_i = std::distance(vec1.begin(), std::upper_bound(vec1.begin(), vec1.end(), t - vec2[i]));
ans = std::max(ans, vec1[vec1_i-1]+vec2[i]);
}
std::cout << ans << std::endl;
return 0;
} | #include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define abs(x) ((x) > 0 ? (x) : -(x))
#define FOREACH(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)
typedef long long LL;
const int MAXN = 45;
LL n, T;
LL a[MAXN];
set<LL> get_sum(vector<int> &num) {
set<LL> ret;
for (int i = 0; i < (1 << num.size()); i++) {
LL sum = 0;
for (int j = 0; j < num.size(); j++) {
if (i & (1 << j)) {
sum += num[j];
}
}
ret.insert(sum);
}
return ret;
}
void solve() {
cin >> n >> T;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> A, B;
for (int i = 0; i < n / 2; i++) {
A.push_back(a[i]);
}
for (int i = n / 2; i < n; i++) {
B.push_back(a[i]);
}
set<LL> X = get_sum(A), Y = get_sum(B);
LL res = 0;
for (auto x : X) {
auto it = Y.upper_bound(T - x);
if (it == Y.begin()) continue;
it--;
res = max(res, x + *it);
}
cout << res << endl;
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
ll countFull(ll n) {
string n_str = to_string(n);
ll dig_n_2 = n_str.length() - 2;
if (dig_n_2 == 0) return 0;
//ll dig_n_2_m1 = dig_n_2 - 1;
ll dig_n_2_m1 = (dig_n_2 - 1)/2;
ll ret = 0;
ll ten = 1;
//rep(i, dig_n_2_m1 - 1) ten *= 10;
rep(i, dig_n_2_m1 ) ten *= 10;
ret = 9 * ten;
return ret + countFull(n / 100);
}
int main() {
ll n;
cin >> n;
string ns =to_string(n);
if (ns.size() % 2 != 0){
n=9;
if (ns.size() != 1) rep(i, ns.size() - 2) {
n *= 10;
n += 9;
}
else {
cout << 0 <<endl;
return 0;
}
}
ll cnt_fra;
string n_str = to_string(n);
string bef, aft;
rep(i, n_str.length()) {
if (i < n_str.length() / 2) bef.push_back(n_str[i]);
else aft.push_back(n_str[i]);
}
ll be = stoll(bef);
ll af = stoll(aft);
ll ten = 1;
rep(i, (n_str.length() / 2) - 1) ten *= 10;
cnt_fra = min(be, af) - (ten - 1);
if (to_string(min(be, af)).length() < n_str.length() / 2) {
cnt_fra = 0;
}
if (be > af) {
ten =1;
rep(i,bef.size()-1) ten *= 10;
cnt_fra = be - 1 - (ten-1) ;
}
cout << cnt_fra + countFull(n) << endl;
} | /*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
using vll = vector<ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
ostream& operator<<(ostream& os, lpair& h){ os << "(" << h.first << ", " << h.second << ")"; return os;}
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
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 solve(){
ll a[3];
rep(i,0,3) cin >> a[i];
sort(a, a+3);
if(a[1] - a[0] == a[2] - a[1]){
print("Yes");
}else{
print("No");
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
solve();
} |
#include<iostream>
#include<string>
using namespace std;
int main(){
string n;
cin >> n;
int a;
a=n.length();
a--;
int i=a,j;
while(i>=0 && n[i]=='0'){
if(n[i]=='0') a--;
i--;
}
j=a;
int k=0,flag=0;
while(k<=j){
if(n[k]!=n[j]) flag=1;
k++;
j--;
}
if(flag==0) cout << "Yes";
else cout << "No";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
int gcd(int a, int b) {
if(a == 0) return b;
return gcd(b % a, a);
}
int lcm(int a, int b) {
return ((a * b) / gcd(a, b));
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int x; cin >> x;
int ans = 1;
for(int i = 1; i <= x; i ++) {
ans = lcm(i, ans);
}
ans ++;
cout << ans << endl;
return 0;
} |
// ahc002_a
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../../debug_util/cxx-prettyprint/prettyprint.hpp"
#include "../../debug_util/rng.hpp"
#endif
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#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)(1e15)
#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;
#include <chrono>
struct Timer {
std::chrono::time_point<std::chrono::system_clock> begin;
void init() {
begin = std::chrono::system_clock::now();
}
int get_ms() {
auto cur = std::chrono::system_clock::now();
return std::chrono::duration_cast<std::chrono::milliseconds>(cur - begin).count();
}
};
struct Grid {
const int H, W;
const vector<vector<int>> S, T;
Timer timer;
const int di[8]={ 1, 0, -1, 0, -1, 1, -1, 1};
const int dj[8]={ 0, 1, 0, -1, -1, -1, 1, 1};
bool bound_check(int i, int j) const {return (0 <= i) && (i < H) && (0 <= j) && (j < W);}
const char order[4] = {'D', 'R', 'U', 'L'};
[[nodiscard]] int hash_func(int i, int j) const {
return i * W + j;
}
[[nodiscard]] tuple<int, int> rev_hash(int h) const{
int i = h / W;
int j = h % W;
return tie(i, j);
}
Grid(int H, int W, vector<vector<int>>& S, vector<vector<int>>& T): H(H), W(W), S(S), T(T) {}
vector<bool> used;
int score = 0;
int max_score = 0;
string sequence;
string ans;
void dfs(int i, int j){
used[S[i][j]] = true;
score += T[i][j];
if (score > max_score){
max_score = score;
ans = sequence;
}
if (timer.get_ms() > 1900){
throw 1;
}
REP(k, 4){
int ni = i + di[k];
int nj = j + dj[k];
if (not bound_check(ni, nj)) continue;
if (used[S[ni][nj]]) continue;
sequence += order[k];
dfs(ni, nj);
sequence.pop_back();
}
score -= T[i][j];
used[S[i][j]] = false;
}
string dfs_wrap(int si, int sj){
// init
int mx = 0;
REP(i, H) REP(j, W) mx = max(mx, S[i][j]);
used.assign(mx+1, false);
// exec
timer.init();
try{
dfs(si, sj);
} catch(int& n){
;
}
return ans;
}
};
int main() {
#ifdef LOCAL
ifstream in("../arg.txt"); cin.rdbuf(in.rdbuf());
#endif
int si, sj;
cin >> si >> sj;
vector<vector<int>> S(50, vector<int>(50)), T(50, vector<int>(50));
REP(i, 50) REP(j, 50) cin >> S[i][j];
REP(i, 50) REP(j, 50) cin >> T[i][j];
Grid g(50, 50, S, T);
string ans = g.dfs_wrap(si, sj);
print(ans);
#ifdef LOCAL
print(g.max_score);
#endif
return 0;
}
| #include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
using ll = long long;
#define MOD 1000000007
#define SWAP(a,b) ((a != b)? (a += b,b = a - b,a -= b) : 0 ) //数値のみ
#define SWAP(type,a,b) { type temp = a; a = b; b = temp; } //ポインタ, 構造体, 文字列
#define rng(i, a, b) for(int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define pb push_back
#define eb emplace_back
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
ll jo(ll a, ll n){
ll tmp = 1;
rep(i, n) {
tmp *= a;
if(tmp <= 0) return -1;
}
return tmp;
}
ll jom(ll a, ll n){
ll tmp = 1;
rep(i, n) {
tmp *= a;
tmp %= MOD;
}
return tmp;
}
unsigned digit(unsigned num){
return std::to_string(num).length();
}
void yn(bool Yes){
cout << ((Yes)? "Yes" : "No") << endl;
}
void ny(bool No){
cout << ((No)? "No" : "Yes") << endl;
}
void YN(bool YES){
cout << ((YES)? "YES" : "NO") << endl;
}
void NY(bool NO){
cout << ((NO)? "NO" : "YES") << endl;
}
void Yay(bool y){
cout << ((y)? "Yay!" : ":(") << endl;
}
string move(int &si, int &sj, vector<vector<int>> &t, vector<vector<int>> &p){
int mi = -1, mj = -1, i = 0;
if(si < 25) mi = 1;
if(sj < 25) mj = 1;
string ans = "";
while(si+mi >= 0 && si+mi < 50 && sj+mj >= 0 && sj+mj < 50 && i != 50){
i++;
t[si][sj] += 10000;
if(t[si+mi][sj] % 10000 != t[si][sj] % 10000 && t[si+mi][sj] <= 10000){
if(mi == 1) ans += 'D';
else ans += 'U';
si += mi;
}
else if(t[si][sj+mj] % 10000 != t[si][sj] % 10000 && t[si][sj+mj] <= 10000){
if(mj == 1) ans += 'R';
else ans += 'L';
sj += mj;
}
else {
break;
cout << si << " " << sj << endl;
}
}
return ans;
}
int main() {
int n, si, sj, b, ans = 0, tmp = 0;
string s = "", s2 = "";
vector<vector<int>> t(50, vector<int>(50, 0)), p(50, vector<int>(50, 0));
cin >> si >> sj;
rep(i, 50) rep(j, 50) cin >> t[i][j];
rep(i, 50) rep(j, 50) cin >> p[i][j];
s = move(si, sj, t, p);
//s2 = move(si, sj, t, p);
cout << s << endl;
//printf("%.10lf\n", ans);
} |
#pragma warning(disable : 4996)
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef tuple<ll,ll,ll> tl3;
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FORN(i, 1, n)
#define tc(t) while (t--)
// https://atcoder.jp/contests/hhkb2020/tasks/hhkb2020_c
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
vll v(n);
map<ll,ll> isUsed;
ll min = 0;
rep(i,n){
cin >> v[i];
isUsed[v[i]]++;
for(;;min++){
if(isUsed[min] == 0){
break;
}
}
cout << min << "\n";
}
return 0;
} | #include <stdio.h>
#define MOD 3
int fact[400010],count[400010];
char x[400010];
int comb(int a, int b)
{
if(count[a]>count[b]+count[a-b]) return 0;
else return (fact[a]*fact[b]*fact[a-b])%MOD;
}
int main()
{
fact[0] = 1;
count[0] = 0;
for(int i=1;i<=400000;i++)
{
int c = i;
count[i] = count[i-1];
while(c%MOD==0)
{
c/=MOD;
count[i]++;
}
fact[i] = (fact[i-1]*c)%MOD;
}
int a;
scanf("%d",&a);
scanf("%s",x+1);
int sum = 0;
for(int i=1;i<=a;i++)
{
int c;
if(x[i]=='W') c = 0;
if(x[i]=='B') c = 1;
if(x[i]=='R') c = 2;
sum += comb(a-1,i-1)*c;
sum %= MOD;
}
if(a%2==1)
{
if(sum==0) printf("W");
if(sum==1) printf("B");
if(sum==2) printf("R");
}
else
{
if(sum==0) printf("W");
if(sum==1) printf("R");
if(sum==2) printf("B");
}
} |
#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 }; // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };
const ll mod = 998244353;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);
struct UnionFind {
vector< int > data;
UnionFind(int sz) {
data.assign(sz, -1);
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return (false);
if(data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if(data[k] < 0) return (k);
return (data[k] = find(data[k]));
}
bool findSet(int x,int y){
return find(x) == find(y);
}
int size(int k) {
return (-data[find(k)]);
}
};
ll f(ll x){
ll ret = 1;
for(int i=0;i<x;i++){
ret *= i+1;
ret %= mod;
}
return ret % mod;
}
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n,m;
cin >> n >> m;
vector<vector<int>> a(n,vector<int>(n));
rep(i,n)rep(j,n)cin >> a[i][j];
ll ans = 1;
// まずは行についてスワップできる組み合わせを見つけていく
UnionFind unitate(n),uniyoko(n);
set<int> tate,yoko;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
bool ok = true;
for(int k=0;k<n;k++){
if(a[i][k] + a[j][k] > m)ok = false;
}
if(ok){
unitate.unite(i,j);
}
}
}
for(int i=0;i<n;i++){
int size = unitate.size(i);
int now = unitate.find(i);
if(tate.count(now))continue;
else{
tate.insert(now);
ans *= f(size);
ans %= mod;
}
}
// 次は列についてスワップできる組み合わせを見つける
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
bool ok = true;
for(int k=0;k<n;k++){
if(a[k][i] + a[k][j] > m)ok = false;
}
if(ok){
uniyoko.unite(i,j);
}
}
}
for(int i=0;i<n;i++){
int size = uniyoko.size(i);
int now = uniyoko.find(i);
if(yoko.count(now))continue;
else{
yoko.insert(now);
ans *= f(size);
ans %= mod;
}
}
cout << ans << endl;
} | #include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int M=1e6+5;
const int N=1e3+5;
#define int long long
int Max(int x,int y){return x>y?x:y;}
int Min(int x,int y){return x>y?y:x;}
int Abs(int x){return x>0?x:-x;}
int read(){
int k=1,num=0;
char s=getchar();
while(s>'9'||s<'0'){
if(s=='-') k=-1;
s=getchar();
}
while(s>='0'&&s<='9'){
num=(num<<1)+(num<<3)+(s-'0');
s=getchar();
}
return num*k;
}
int cnt[M];
signed main(){
int L=read(),R=read();
for(int i=2;i<=R;i++){
if(cnt[i]==0){
for(int j=i;j<=R;j+=i) cnt[j]++;
for(int j=i*i;j<=R;j+=i*i) cnt[j]=-(1ll<<60);
}
}
int ans=0;
for(int k=2;k<=R;k++){
if(cnt[k]<0) continue;
int val=R/k-(L-1)/k;
val=val*(val-1)/2;
if(cnt[k]%2) ans+=val;
else ans-=val;
}
for(int i=Max(2ll,L);i<=R;i++){
ans-=R/i-1;
}
printf("%lld",ans*2);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define all(v) v.begin(),v.end()
const int mod = 1e9 + 7;
int main (){
fast_io;
int x, y, z;
cin >> x >> y >> z;
int ans = (y * z)/ x + ((y * z) % x > 0);
cout << ans - 1 << endl;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
cout<<a+b+c-min(a,min(b,c));
return 0;
} |
#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size()-1)
#define ms(a,b) memset(a,b,sizeof a)
#define F(i,a,b) for (int i=(a);i<=(b);++i)
#define DF(i,a,b) for (int i=(a);i>=(b);--i)
using namespace std;
inline int read(){
char ch=getchar(); int w=1,c=0;
for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
return w*c;
}
int main(){
int n=read();
cout<<(1<<n)-1<<"\n";
F(i,1,(1<<n)-1){
F(j,0,(1<<n)-1){
cout<<((__builtin_popcount(i&j)&1)&1 ? 'A' : 'B');
}
cout<<"\n";
}
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
| #include <bits/stdc++.h>
#define div /
/*
ID: farukde1
TASK: beads
LANG: C++
*/
using namespace std;
int main() {
ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
/*
freopen("input.txt","r",stdin);
freopen("beads.out","w",stdout);
*/
string s;
cin >> s;
int ans=0;
for(int i=0; i<=9999; i++){
bool flag[10]={false};
int x=i;
for(int j=0; j<4; j++){
flag[x%10] = true;
x=x div 10;
}
bool flag2 = true;
for(int j=0; j<10; j++){
if(s[j]=='o' && !flag[j]) flag2 = false;
if(s[j]=='x' && flag[j]) flag2 = false;
}
ans+=flag2;
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,ans=1e18;
int a[100001];
signed main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=0;i<1<<(n-1);i++)
{
int x=0,o=0;
for(int j=1;j<=n;j++)
{
o|=a[j];
if(1<<(j-1)&(i))x^=o,o=0;
}
x^=o;
ans=min(ans,x);
}
cout<<ans;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 998244353;
int N, a[200010], vis[200010];
int in[200010];
int main()
{
scanf("%d",&N);
for (int i=1; i<=N; i++)
{
scanf("%d",&a[i]);
in[a[i]]++;
}
queue<int>q;
for (int i=1; i<=N; i++)
if (!in[i])
q.push(i);
while(!q.empty())
{
int now = q.front();
q.pop();
vis[now] = true;
if (--in[a[now]] == 0)
q.push(a[now]);
}
int cnt = 0;
for (int i=1; i<=N; i++)
{
if (!vis[i])
{
cnt++;
for (int j=i; !vis[j]; j=a[j])
{
vis[j] = true;
}
}
}
LL ans = 1;
for (int i=1; i<=cnt; i++)
ans = ans * 2LL % MOD;
ans = (ans + MOD - 1) % MOD;
printf("%lld\n",ans);
} |
#include <iostream>
using namespace std;
int a, b, c, d;
int main() {
cin >> a >> b >> c >> d;
cout << b - c;
return 0;
} | // coded by- mrNOBODY_
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define in(x) cin>>x;
#define print(x) cout<<x;
#define println(x) cout<<x<<"\n";
#define vec(x) vector<x>
#define endl "\n"
#define all(x) x.begin(),x.end()
#define inf 1e15
#define sqr(x) ((x) * (x))
#define fr(i,a,b) for(ll i=a;i<b;i++)
void OJ();
ll const M=1e9 +7;
using namespace std;
/*ll binpow(ll a,ll n){
ll res=1;
while (n>0)
{
if(n&1)
res =res*a;
a=sqr(a);n>>=1;
}
return res;
}
ll binpow(ll a,ll n,ll m){
ll res=1;
while (n>0)
{
if(n&1)
res=((res%m)*(a%m))%m;
a=(a*a)%m;n>>=1;
}
return res;
}
bool comp(const pair<int,int> &a , const pair<int,int> &b){
return a.first < b.second;
}*/
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin.sync();
ll a,b,c,d;
in(a>>b>>c>>d)
print(max(a,b)-min(c,d))
return 0;
}
void OJ()
{
#ifndef ONLINE_JUDGE
freopen("inputf.in", "r", stdin);
freopen("outputf1.in", "w", stdout);
#endif
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
// --------------------------------------------------------
template<class T> bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; }
#define FOR(i,l,r) for (ll i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define COUNT(c,v) count(ALL(c),(v))
#define SZ(c) ((ll)(c).size())
#define BIT(b,i) (((b)>>(i)) & 1)
#define PCNT(b) __builtin_popcountll(b)
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n'
ll llceil(ll a, ll b) { return (a + b - 1) / b; }
ll bitlen(ll b) { if (b <= 0) { return 0; } return (64LL - __builtin_clzll(b)); }
string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; }
string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; }
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VVS = vector<VS>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VVVLL = vector<VVLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VVVB = vector<VVB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VLD = vector<ld>;
using VVLD = vector<VLD>;
using VVVLD = vector<VVLD>;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
ll N; cin >> N;
VVLL C(N,VLL(N)); REP(i,N) REP(j,N) cin >> C[i][j];
VLL A(N),B(N);
A[0] = 0;
REP(i,N) B[i] = C[0][i];
FOR(i,1,N) A[i] = C[i][0] - B[0];
FOR(i,1,N) FOR(j,1,N) {
if (A[i] + B[j] != C[i][j]) {
COUT("No"); return 0;
}
}
ll min_a = MIN(A);
ll min_b = MIN(B);
if (min_a + min_b < 0) {
COUT("No"); return 0;
} else if (min_a < 0) {
REP(i,N) A[i] += (-min_a);
REP(i,N) B[i] += min_a;
} else if (min_b < 0) {
REP(i,N) B[i] += (-min_b);
REP(i,N) A[i] += min_b;
}
COUT("Yes");
REP(i,N) {
if (i) cout << " ";
cout << A[i];
}
cout << endl;
REP(i,N) {
if (i) cout << " ";
cout << B[i];
}
cout << endl;
return 0;
}
| #pragma GCC optimize ("Ofast")
#pragma GCC optimization ("unroll-loops, no-stack-protector")
#pragma GCC target ("avx")
#pragma GCC target ("avx2")
#pragma GCC target ("fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#define ordered_set tree <ll, null_type, less <ll>, rb_tree_tag, tree_order_statistics_node_update>
#pragma 03
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll a[505], b[505];
ll c[505][505];
int main(){
fastio;
ll n; cin >> n;
for (ll i = 1; i <= n; i++){
for (ll j = 1; j <= n; j++) cin >> c[i][j];
}
for (ll i = 1; i <= n; i++){
a[i] = 1e9;
for (ll j = 1; j <= n; j++) a[i] = min(a[i], c[i][j]);
}
for (ll j = 1; j <= n; j++) b[j] = c[1][j] - a[1];
for (ll i = 2; i <= n; i++){
for (ll j = 1; j <= n; j++){
if (c[i][j] != a[i] + b[j]){
cout << "No\n"; return 0;
}
}
}
cout << "Yes\n";
for (ll i = 1; i <= n; i++) cout << a[i] << " ";
cout << "\n";
for (ll i = 1; i <= n; i++) cout << b[i] << " ";
cout << "\n";
} |
#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";}
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;
vector<ll> pickdiv(ll n){
vector<ll> res;
for(ll i=1; i*i <=n;i++){
if(n%i)continue;
res.emplace_back(i);
ll q = n/i;
if(q != i){
res.emplace_back(q);
}
}
return res;
}
int main(){
ll S,P;
cin >> S >> P;
auto v = pickdiv(P);
bool f = 0;
for(auto& e:v){
ll q = P/e;
if(q+e == S){
f = 1;
}
}
cout << (f?"Yes":"No") << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define eb emplace_back
#define pb push_back
#define INF LLONG_MAX
#define FLT_INF pow(10.0, 16)
using ll = long long;
using vll = vector<ll>;
using ull = unsigned long long;
using int2 = pair<ll, ll>;
using int3 = tuple<ll, ll, ll>;
template <typename A>
string to_string(const A &v);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(const pair<A, B> &p)
{
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(const tuple<A, B, C> &p)
{
return "(" + to_string(std::get<0>(p)) + ", " + to_string(std::get<1>(p)) + ", " + to_string(std::get<2>(p)) + ")";
}
template <typename A>
string to_string(const 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 T>
void sort(vector<T> &val)
{
sort(val.begin(), val.end());
}
template <typename T>
void reverse(vector<T> &val)
{
reverse(val.begin(), val.end());
}
template <typename T>
void sort_b(vector<T> &val)
{
sort(val.begin(), val.end(), greater<T>());
}
template <typename T>
T pair_sum(pair<T, T> &val)
{
return val.first + val.second;
}
template <typename T>
T min(T a, T b, T c)
{
return min(min(a, b), c);
}
template <typename T>
T max(T a, T b, T c)
{
return max(max(a, b), c);
}
template <typename T>
T mul_mod(T a, T b, T mod)
{
T res = 0;
a %= mod;
while (b)
{
if (b & 1)
{
res = (res + a) % mod;
}
a = (2 * a) % mod;
b >>= 1;
}
return res;
}
template <typename T>
T fac_mod(T v, T mod)
{
ll res = 1;
for (ll i = 1; i <= v; i++)
{
res *= i;
res = res % mod;
}
return res;
}
template <typename T>
T pow_mod(T v, T p, T mod)
{
if (p == 1)
return v;
if (p % 2 == 0)
{
T t = pow_mod(v, p / 2, mod);
return (t * t) % mod;
}
else
{
return (v * pow_mod(v, p - 1, mod)) % mod;
}
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T)
{
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) ;
#endif
#define COMMON_MOD static_cast<ll>(pow(10, 9) + 7)
#define PI 3.14159265
// sort(a.begin(), a.end(), less<ll>()); greater<ll>()
// cout<<fixed<<setprecision(20)<<(s/kl)*2;
/* printf("%lld %lld \n", cur_n, m / 2 + 1);
sort(a.begin(), a.end(), [ ](ll& lhs, ll& rhs) {return abs(lhs) < abs(rhs);});
*/
class mytask
{
public:
mytask() {}
int solve()
{
ll n;
cin >> n;
string s;
cin >> s;
n = s.size();
vll stack;
ll res = n;
for (ll i = 0; i < n; i++)
{
if (s[i] == 'f')
stack.pb(1);
else if (s[i] == 'o' && stack.size() && stack.back() == 1)
{
stack[stack.size() - 1] = 2;
}
else if (s[i] == 'x' && stack.size() && stack.back() == 2)
{
stack.pop_back();
res -= 3;
}
else
{
stack.clear();
}
}
cout << res;
return 0;
}
};
int main()
{
auto task = mytask();
task.solve();
return 0;
}
|
#include <bits/stdc++.h>
#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))
#ifdef LOCAL
#define log(val) std::cerr << std::setw(3) << __LINE__ << "| " << #val << " = " << (val) << std::endl
#define dbg(proc) proc
#else
#define log(val) 0
#define dbg(proc) 0
#endif
template <typename T>
std::ostream& operator<<(std::ostream& os,const std::vector<T>& v){os << "{";if(!v.empty()){os << v[0];for(std::size_t i=1;i<v.size();++i){os << ", " << v[i];}}return os << "}";}
using ll = std::int64_t;
using P = std::tuple<int,int>;
int N, M;
P YX[200100];
std::set<int> se;
int main(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> N >> M;
for(int i=0;i<M;++i){
int X, Y;
std::cin >> X >> Y;
YX[i] = std::make_tuple(X, Y);
}
std::sort(YX, YX + M);
se.emplace(N);
for(int i=0,j=0;i<M;){
while(j < M && fst(YX[j]) == fst(YX[i])){
j += 1;
}
std::set<int> ok, ng;
while(i < j){
auto [Y, X] = YX[i];
if(se.count(X) > 0){
ng.emplace(X);
}
if(se.count(X - 1) > 0 || se.count(X + 1) > 0){
ok.emplace(X);
}
i += 1;
}
for(int x : ng){
auto it = se.find(x);
if(it != se.end()){
se.erase(it);
}
}
for(int x : ok){
se.emplace(x);
}
}
int res = se.size();
std::cout << res << std::endl;
}
| #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 = 4e5+10;
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int npos, npawns; cin >> npos >> npawns;
map<int,vector<int>> mp;
fr(i,npawns){
int x, y; cin >> x >> y;
mp[x].push_back(y);
}
set<int> at;
at.insert(npos);
for(auto &[x,v] : mp){
vector<int> por, tirar;
for(auto &i : v){
tirar.push_back(i);
if(at.count(i-1) or at.count(i+1)) por.push_back(i);
}
for(auto &i : tirar) at.erase(i);
for(auto &i : por) at.insert(i);
}
cout << sz(at) << "\n";
} |
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int n;
int re[10005],a[2505];
int main()
{
int len=0;
for(int i=1;i<=5000;i++)
if(i%3==0 || i%5==0 || i%7==0 || i%11==0)
re[++len]=i;
n=read();
a[n]=3*5*7*11;
for(int i=1;i<=n-1;i++) a[i]=2*re[i];
for(int i=1;i<=n;i++) printf("%d ",a[i]);
printf("\n");
return 0;
} | //#include <boost/multiprecision/cpp_int.hpp>
#include "bits/stdc++.h"
//using namespace boost::multiprecision;
using namespace std;
#define MODULO 1000000007
#define PI 3.14159265359
typedef long long int ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
cout<<"6 10 15 ";
int x=n-3;
int xd=0;
if(x==0)
return 0;
for(int i=16;i<=10000;i++)
{
if(i%6==0 || i%10==0 || i%15==0)
{
cout<<i<<" ";
xd++;
}
if(xd==x)
break;
}
return 0;
} |
#include <bits/stdc++.h>
#define all(nums) nums.begin(), nums.end()
#define allr(nums) nums.rbegin(), nums.rend()
using namespace std;
typedef long long int ll;
typedef vector<vector<int>>graph;
const int imax = INT32_MAX;
const int imin = INT32_MIN;
const int mx=1e5+1;
const ll inf=1e18;
template<class T>void print(vector<T>vec){for(T i:vec)cout<<i<<" ";cout<<"\n";}
template<class T>void alloc(vector<vector<T>*>vec, T n){for(auto v:vec)v->resize(n+1);}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll t = 1;
// cin>>t;
while(t--) {
int n;
cin>>n;
cout<<(n&1?"Black":"White");
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin >> T;
if (T % 2 == 1)
cout << "Black";
else
cout << "White";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
long long n, k, m, sum=0;
int main(){
cin >> n >> k >> m;
for (int i=0;i<n-1;i++){
long long a;
cin >> a;
sum+=a;
}
m*=n;
cout << (m-sum<=k ? max((long long)0,m-sum) : -1) << "\n";
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <unordered_map>
using namespace std;
using ll = long long;
void _cin(){} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail){ cin >> head; _cin(forward<Tail>(tail)...); }
void _cout(){ cout << "\n"; } template <class Head, class... Tail> void _cout(Head&& head, Tail&&... tail){ cout << head; _cout(forward<Tail>(tail)...); }
int gcd(int a, int b){ return (b == 0) ? a : gcd(b, a % b); }
template<typename S, typename T> ostream& operator<<(ostream &os, const pair<S, T> &p){ cout << "[" << p.first << ", " << p.second << "]"; return os; }
#define Sq(x) (x)*(x)
#define For(i, n) for(int i = 0; i < (n); i ++)
#define Rep(n) For(_, n)
#define Range(c) c.begin(), c.end()
#define RevRange(c) c.rbegin(), c.rend()
#define Contains(c, x) (find(Range(c), x) != c.end())
#define Search(rb, re, x) distance(rb, find(rb, re, x))
#define Sort(a) sort(Range(a))
#define DeSort(a) sort(RevRange(a))
#define Reverse(c) reverse(Range(c))
#define Unique(a) a.erase(unique(Range(a)), a.end())
#define Cusum(T, xs, sxs) vector<T> sxs(xs.size()+1); For(i, (int)xs.size()) sxs[i+1] = sxs[i] + xs[i]
#define Cin(T, ...) T __VA_ARGS__; _cin(__VA_ARGS__)
#define Cins(T, n, xs) vector<T> xs(n); For(i, n) cin >> xs[i]
#define Cins2(T, n, xs, ys) vector<T> xs(n), ys(n); For(i, n) cin >> xs[i] >> ys[i]
#define Cins3(T, n, xs, ys, zs) vector<T> xs(n), ys(n), zs(n); For(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define Cinss(T, n, m, xs) Vec2(T, n, m, xs); For(i, n) For(j, m) cin >> xs[i][j]
#define Cinm(T, n, map) unordered_map<T, int> map; Rep(n){ Cin(T, x); map[x] ++; }
#define Cout(...) _cout(__VA_ARGS__)
#define Couts(xs) { for(const auto &e : xs) cout << e << " "; cout << "\n"; }
#define Coutyn(cond) Cout((cond) ? "yes" : "no")
#define CoutYn(cond) Cout((cond) ? "Yes" : "No")
#define CoutYN(cond) Cout((cond) ? "YES" : "NO")
#define Return(expr) { Cout(expr); return 0; }
#define vc vector
#define Mini(a, x) a = min(a, x)
#define Maxi(a, x) a = max(a, x)
// constexpr int MOD = 1e9+7;
int main(void){
Cin(int, n, k, m);
int need = m*n;
Rep(n-1){
Cin(int, x);
need -= x;
}
Cout(need < 0 ? 0 : need <= k ? need : -1);
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
int gc = 0;
void br() {
cout << "(" << gc++ << ") "
<< "======" << endl;
}
template <typename T>
void print(vector<T> &a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i];
if (i + 1 < a.size()) {
cout << " ";
}
}
cout << endl;
}
void print(int n) { cout << n << endl; }
void print(ll n) { cout << n << endl; }
void print(ull n) { cout << n << endl; }
void print(double n) { cout << n << endl; }
void print(long double n) { cout << n << endl; }
void print(char n) { cout << n << endl; }
void print(string n) { cout << n << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
double toRadian(double deg) { return deg * M_PI / 180; }
ll gcd(ll a, ll b) {
if (a < b) {
a ^= b;
b ^= a;
a ^= b;
}
return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int main() {
ll n;
cin >> n;
vector<P> v(n);
for (int i = 0; i < n; i++) {
ll a, b;
cin >> a >> b;
v[i].first = a;
v[i].second = b;
}
ll ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double dx = v[j].first - v[i].first;
double dy = v[j].second - v[i].second;
if (dx != 0) {
double k = dy / dx;
if (-1 <= k && k <= 1) {
ans++;
}
}
}
}
print(ans);
return 0;
}
| #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
ll c=a%i_7;
if(c>=0)return c;
return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int main(){fastio
ll n;cin>>n;
if(n==1){
cout<<0<<endl;
return 0;
}
ll x[n],y[n];
rep(i,0,n-1){
cin>>x[i]>>y[i];
}
ll ans=0;
rep(i,0,n-2){
rep(j,i+1,n-1){
//abs(y[j]-y[i])/abs(x[j]-x[i])が1以下
if(abs(y[j]-y[i])<=abs(x[j]-x[i])){
ans++;
}
}
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)
typedef long long ll;
const int mxN = 5e3+5;
const int mxM = 5e3+5;
const int mod = 998244353;
int N, M;
int powtbl[mxM][mxN], powsum[mxN];
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// observation 1: X will contribute if min(prv[X], pos[X], thing) < X
// <=> X will not contribute if min(prv[X], pos[x], thing) > X
// consider prv[X] = 0 (DNE), 1, 2...pos[X]-1;
// (M-X) ** (pos[X]-1-prv[X])
//
// rest: M ** (prv[X]-1) * M ** (N - pos[X]);
//
// for each i < j:
// for each X:
// (M-X) ** (j-1-i) * M ** (i-1) * M ** (N-j);
// -------
//
// (M-X)**(j-1-i) * M**(N-j+i-1)
// precompute 0...M ** 0..N
// precompute add (0 to M-1) ** 0...N
cin >> N >> M;
FOR(i,0,M){
powtbl[i][0] = 1;
if (i<M) powsum[0] += powtbl[i][0];
FOR(j,1,N){
powtbl[i][j] = (ll)powtbl[i][j-1]*i % mod;
if (i<M) powsum[j] = (powsum[j]+powtbl[i][j]) % mod;
}
}
int ans = 1;
FOR(i,1,N) ans = (ll)ans*M % mod;
ans = (ll)ans*N % mod;
FOR(i,1,N){
FOR(j,i+1,N){
int x = (ll)powsum[j-i-1] * powtbl[M][N-j+i-1] % mod;
ans = ((ll)ans-x+mod) % mod;
}
}
cout << ans << '\n';
}
| // Problem: C - IPFL
// Contest: AtCoder - AtCoder Beginner Contest 199(Sponsored by Panasonic)
// URL: https://atcoder.jp/contests/abc199/tasks/abc199_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
#include<bits/stdc++.h>
// #define int long long
// #define maxn
// #define mod
using namespace std;
// template<class T>T ABS(T x) {return x<0?-x:x;}
//int ksm(int x,int y) {int r=1;while(y) y&1?r=1ll*r*x%mod:0,x=1ll*x*x%mod,y>>=1;return r;}
int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
char S[2][1000010];int n,Q;
// struct Edge{int to,next;}a[maxn];
// int h[maxn],cnt;
// void add(int x,int y) {a[++cnt]=(Edge){y,h[x]},h[x]=cnt,a[++cnt]=(Edge){x,h[y]},h[y]=cnt;}
signed main()
{
n=read();
scanf("%s",S[0]+1);
for(int i=1;i<=n;i++) S[1][i]=S[0][i+n];
int fr=0;Q=read();
for(int i=1;i<=Q;i++)
{
if(read()==2) fr^=1,read(),read();
else {
int x=read(),y=read();
swap(S[fr^(x>n)][(x>n?x-n:x)],S[fr^(y>n)][(y>n?y-n:y)]);
}
}
for(int i=1;i<=n;i++) cout<<S[fr][i];
for(int i=1;i<=n;i++) cout<<S[fr^1][i];
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mit map<int,int>::iterator
#define sit set<int>::iterator
#define itrm(g,x) for(mit g=x.begin();g!=x.end();g++)
#define itrs(g,x) for(sit g=x.begin();g!=x.end();g++)
#define ltype int
#define rep(i,j,k) for(ltype(i)=(j);(i)<=(k);(i)++)
#define rap(i,j,k) for(ltype(i)=(j);(i)<(k);(i)++)
#define per(i,j,k) for(ltype(i)=(j);(i)>=(k);(i)--)
#define pii pair<int,int>
#define fi first
#define se second
#define mpr make_pair
#define pb push_back
#define fastio ios::sync_with_stdio(false)
#define check(x) if(x>=mod) x-=mod
const int inf=0x3f3f3f3f,mod=998244353;
const double pi=3.1415926535897932,eps=1e-6;
void chmax(int &x,int y){if(x < y) x = y;}
void chmin(int &x,int y){if(x > y) x = y;}
int qpow(int x,int y){
int ret = 1;
while(y) {
if(y & 1) ret = (ll)ret * x % mod;
x = (ll)x * x % mod;
y >>= 1;
}
return ret;
}
int n,p[105],fac[105];
int dp[105][5005][105];
int main()
{
scanf("%d",&n);
fac[0] = 1;rep(i,1,n) fac[i] = (ll)fac[i - 1] * i % mod;
int sum = 0;
rep(i,1,n) scanf("%d",p+i), sum += p[i];
if(sum & 1) {
puts("0");
return 0;
}
dp[0][0][0] = 1;
rep(i,1,n) {
rep(j,0,sum/2) {
rep(k,0,i) {
dp[i][j][k] = dp[i - 1][j][k];
if(p[i] <= j && k) dp[i][j][k] += dp[i - 1][j - p[i]][k - 1];
check(dp[i][j][k]);
}
}
}
int ans = 0;
rep(i,1,n) {
int num = dp[n][sum/2][i];
ans += (ll)num * fac[i] % mod * fac[n - i] % mod;
check(ans);
}
printf("%d\n",ans);
return 0;
} | #include <bits/stdc++.h>
const int mod=998244353;
using namespace std;
int T,n,m,fac[110],a[110],f[110][5010];
void init(int N){
fac[0]=1;
for(int i=1;i<=N;i++) fac[i]=1ll*fac[i-1]*i%mod;
}
void qmo(int &x){
x+=(x>>31)&mod;
}
int main(){
scanf("%d",&n),init(n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),m+=a[i];
if(m&1) return puts("0"),0;
m/=2,f[0][0]=1;
for(int i=1;i<=n;i++) for(int j=i;j>=1;j--) for(int k=m;k>=a[i];k--){
qmo(f[j][k]+=f[j-1][k-a[i]]-mod);
}
int res=0;
for(int i=0;i<=n;i++) res=(res+1ll*fac[i]*fac[n-i]%mod*f[i][m])%mod;
printf("%d\n",res);
}
|
#include <iostream>
#include <vector>
#include <string>
#include <stdint.h>
#include <math.h>
#include <algorithm>
#include <cmath>
#define rep(i, j) for (int i = 0; i < (j); ++i)
#define fep(i, j) for (i = 0; i < (j); ++i)
#define pb push_back
#define ll long long
#define dp vector<pair<ll, int> >
#define dn vector<vector<int> >
#define ds vector<int>
//template <class RandomAccessIterator>
//void sort (RandomAccessIterator first, RandomAccessIterator last);
using namespace std;
int sol()
{
int N;
int64_t S, D, X, Y;
cin>>N>>S>>D;
vector<vector<int> > p;
string ans = "No";
rep(i, N)
{
vector<int> spells;
rep(i, 1)
{
cin >> X >> Y;
spells.pb(X);
spells.pb(Y);
}
p.pb(spells);
}
rep(i, N)
{
for(unsigned int j = 0; j < p[i].size(); ++j)
{
if(p[i][0] < S && p[i][1] > D)
{
ans = "Yes";
}
}
}
cout << ans;
return 0;
}
int main()
{
return sol();
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
int n,s,d;
int main(){
scanf("%d%d%d",&n,&s,&d);
bool flag=false;
for(int i=1;i<=n;i++){
int x,y;
scanf("%d%d",&x,&y);
if(x<s&&y>d) flag=true;
}
if(flag) puts("Yes");
else puts("No");
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];
void solve(int T)
{
int n;
cin>>n;
VI ax(n), bx(n), cx(n), cc(n+1);
for(auto &&u : ax)
{
cin>>u;
cc[u]++;
}
for(auto &&u : bx)
cin>>u;
for(auto &&u : cx)
cin>>u;
ll tt = 0;
for(auto &&u : cx)
tt += cc[bx[u-1]];
cout<<tt<<"\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;
#define ll long long
#define pb push_back
#define sz(x) int(x).size()
typedef pair<int,int>ii;
typedef vector<int> vi;
const int mxn=2e5+5;
int n,a[mxn],me=1e9+10;
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
me=min(me,a[i]);
}
unordered_map<int,int>mp;
for(int i=0;i<n;i++){
for(int j=1;j*j<=a[i];j++){
if(a[i]%j==0){
if(j<me){
if(mp.find(j)==mp.end())mp[j]=a[i];
mp[j]=__gcd(mp[j],a[i]);
}
if(a[i]/j<me){
if(mp.find(a[i]/j)==mp.end())mp[a[i]/j]=a[i];
mp[a[i]/j]=__gcd(mp[a[i]/j],a[i]);
}
}
}
}
int ans=0;
for(auto it=mp.begin();it!=mp.end();it++){
if(it->first==it->second)ans++;
}
cout<<ans+1<<"\n";
return 0;
} |
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define lowbit(x) (x & (-x))
const int maxn = 2e5+8, inf = 1e18+9, mod = 1e9+7;
int n, m, a[maxn], b[maxn], bit[maxn], pos[maxn];
map<int, set<int>> mp;
void update(int x, int v) {
while (x < maxn) bit[x] += v, x += lowbit(x);
}
int ask(int x) {
int ans = 0;
while (x) ans += bit[x], x -= lowbit(x);
return ans;
}
void solve() {
int i, j, ans = 0;
cin >> n;
for (i = 1; i <= n; i++) cin >> a[i], a[i] += i;
for (i = 1; i <= n; i++) cin >> b[i], b[i] += i;
for (i = 1; i <= n; i++) mp[a[i]].insert(i);
for (i = 1; i <= n; i++) {
if (mp[b[i]].empty()) return (void)(cout << -1 << endl);
auto it = mp[b[i]].begin();
pos[i] = *it;
mp[b[i]].erase(it);
}
for (i = 1; i <= n; i++) {
ans += ask(n) - ask(pos[i]);
update(pos[i], 1);
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); //cout << fixed << setprecision(15);
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define scanf(...) scanf(__VA_ARGS__)?:0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair <PLL, int> PLLI;
typedef pair <PII, PII> PP;
typedef pair <PII, int> PPI;
typedef pair <int, PII> PIP;
typedef pair <ll, int> PLI;
typedef unsigned int ui;
const int inf = 1e9+9;
const ll MOD = 1e9+696969;
#ifdef DEBUG
template<class T> int size(T &&x) {
return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
out << '{';
for(auto it = x.begin(); it != x.end(); ++it)
out << *it << (it == prev(x.end()) ? "" : ", ");
return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
#endif
#ifdef DEBUG
struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
const LL INF = 1e18;
const int maxn = 300100;
const int mod = 998244353;
int dr[maxn];
void add(int p, int v) {
for (; p < maxn; p += p & (-p)) dr[p] += v;
}
int get(int p) {
int res = 0;
for (; p > 0; p -= p & (-p)) res += dr[p];
return res;
}
ll inv(vector <int> &perm) {
int n = perm.size();
ll res = 0;
for (int i=n-1; i>=0; --i) {
res += get(perm[i]);
add(perm[i], 1);
}
return res;
}
void solve() {
int n;
cin >> n;
set <PII> q;
FOR(i, 0, n-1) {
int a;
cin >> a;
q.insert({a + i, i + 1});
}
vector <int> perm;
vector <int> B(n + 1);
FOR(i, 1, n) {
cin >> B[i];
}
FOR(i, 1, n) {
int a = B[i];
int co = a + i - 1;
set <PII> :: iterator szukane = q.lower_bound(make_pair(co, -1));
if (szukane == q.end() || szukane->first != co) {
cout << "-1\n";
return;
}
perm.push_back(szukane -> e2);
q.erase(szukane);
}
debug(perm);
cout << inv(perm) << "\n";
}
int main() {
boost;
int tests;
tests = 1;
//cin >> tests;
while (tests--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int par[110];
int depth[110];
int siz[110];
void init(){
for(int i = 0; i < 110; i++){
par[i] = i; depth[i] = 0; siz[i] = 1;
}
}
int find(int x){
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
else if(depth[x] < depth[y]){
par[x] = y;
siz[y] += siz[x];
}else{
par[y] = x;
siz[x] += siz[y];
if(depth[x] == depth[y]) depth[x] ++;
}
}
bool same(int x, int y){
return find(x) == find(y);
}
int sz(int x){
return siz[find(x)];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int N; cin >> N;
int X[110], Y[110];
for(int i = 1; i <= N; i++){
cin >> X[i] >> Y[i];
}
double ok = 0; double ng = 114.514;
int T = 64;
while(T--){
init();
double mid = (ok + ng) * 0.5;
init();
for(int i = 1; i <= N; i++){
if(Y[i] + mid * 2 >= 100) unite(0, i);
if(Y[i] - mid * 2 <= -100) unite(N+1, i);
}
if(mid >= 100) unite(0, N+1);
for(int i = 1; i <= N; i++){
for(int j = i+1; j <= N; j++){
double d = pow((X[j] - X[i]) * (X[j] - X[i]) + (Y[j] - Y[i]) * (Y[j] - Y[i]), 0.5);
if(d <= mid * 2) unite(i, j);
}
}
if(same(0, N+1)) ng = mid;
else ok = mid;
}
cout.precision(12);
cout << ok << "\n";
} | #include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
#define vall(x) (x).begin(), (x).end()
using namespace std;
namespace atcoder {}
using namespace atcoder;
using lli = long long int;
void YESNO(bool), YesNo(bool);
template <class T1, class T2> bool chmin(T1 &l, const T2 &r);
template <class T1, class T2> bool chmax(T1 &l, const T2 &r);
#define int long long int
int help(int st, int dst, int last) { return abs(st - dst) + abs(last - dst); }
signed main() {
int n;
cin >> n;
map<int, vector<int>> m;
rep(i, n) {
int x, c;
cin >> x >> c;
m[c].push_back(x);
}
vector<vector<int>> dp(1e6, vector<int>(2, 1e18));
vector<vector<int>> mem;
for (auto &e : m) {
sort(e.second.begin(), e.second.end());
int f = e.second.front();
int en = e.second.back();
mem.push_back({f, en});
}
rep(i, mem.size()) {
if (i) {
chmin(dp[i][1], dp[i - 1][0] + help(mem[i - 1][0], mem[i][0], mem[i][1]));
chmin(dp[i][0], dp[i - 1][0] + help(mem[i - 1][0], mem[i][1], mem[i][0]));
chmin(dp[i][1], dp[i - 1][1] + help(mem[i - 1][1], mem[i][0], mem[i][1]));
chmin(dp[i][0], dp[i - 1][1] + help(mem[i - 1][1], mem[i][1], mem[i][0]));
} else {
dp[i][0] = help(0, mem[i][1], mem[i][0]);
dp[i][1] = help(0, mem[i][0], mem[i][1]);
}
}
cout << min(dp[mem.size() - 1][0] + abs(mem.back()[0]),
dp[mem.size() - 1][1] + abs(mem.back()[1]))
<< endl;
return 0;
}
// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
template <class T1, class T2> bool chmin(T1 &l, const T2 &r) {
return (l > r) ? (l = r, true) : false;
}
template <class T1, class T2> bool chmax(T1 &l, const T2 &r) {
return (l < r) ? (l = r, true) : false;
}
template <class T1, class T2> void vadd(vector<T1> &v, T2 x) {
for (auto &s : v)
s += T2(x);
}
|
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
// {{{ModInt
template <int modulo>
class ModInt {
public:
ModInt() : val_(0) {}
ModInt(int v) {
val_ = v % modulo;
if (val_ < 0) val_ += modulo;
}
ModInt(long long v) {
val_ = v % modulo;
if (val_ < 0) val_ += modulo;
}
inline int val() const { return val_; }
ModInt& operator++() {
++val_;
if (val_ == modulo) val_ = 0;
return *this;
}
ModInt& operator--() {
if (val_ == 0) val_ = modulo;
--val_;
return *this;
}
ModInt operator++(int) {
auto ret = *this;
++*this;
return ret;
}
ModInt operator--(int) {
auto ret = *this;
--*this;
return ret;
}
ModInt& operator+=(const ModInt& o) {
val_ += o.val_;
if (val_ >= modulo) val_ -= modulo;
return *this;
}
ModInt& operator-=(const ModInt& o) {
val_ -= o.val_;
if (val_ < 0) val_ += modulo;
return *this;
}
ModInt& operator*=(const ModInt& o) {
val_ = (1ll * val_ * o.val_) % modulo;
return *this;
}
ModInt& operator/=(const ModInt& o) { return *this *= o.Recip(); }
ModInt Pow(long long n) const {
ModInt a = *this;
ModInt res(1);
while (n > 0) {
if (n & 1) res *= a;
a *= a;
n >>= 1;
}
return res;
}
ModInt Recip() const { return Pow(modulo - 2); }
friend ModInt operator+(const ModInt& lhs, const ModInt& rhs) {
return ModInt(lhs) += rhs;
}
friend ModInt operator-(const ModInt& lhs, const ModInt& rhs) {
return ModInt(lhs) -= rhs;
}
friend ModInt operator*(const ModInt& lhs, const ModInt& rhs) {
return ModInt(lhs) *= rhs;
}
friend ModInt operator/(const ModInt& lhs, const ModInt& rhs) {
return ModInt(lhs) /= rhs;
}
friend bool operator==(const ModInt& lhs, const ModInt& rhs) {
return lhs.val_ == rhs.val_;
}
friend bool operator!=(const ModInt& lhs, const ModInt& rhs) {
return lhs.val_ != rhs.val_;
}
private:
int val_;
};
// }}}
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
using i64 = long long;
using mint = ModInt<(int)1e9 + 7>;
vector<vector<pair<int, i64>>> g;
int n;
vector<i64> cumXor;
void dfs(int v, int p = -1, i64 xr = 0) {
cumXor[v] = xr;
for (auto [u, w] : g[v]) {
if (u != p) {
dfs(u, v, xr ^ w);
}
}
}
void solve() {
scanf("%d", &n);
g.assign(n, {});
cumXor.resize(n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
i64 w;
scanf("%d%d%lld", &u, &v, &w);
--u; --v;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
dfs(0);
mint ans = 0;
i64 p2 = 1;
for (int l = 0; l < 60; ++l) {
int ones = 0, zeros = 0;
for (i64 x : cumXor) {
if ((x >> l) & 1) {
ones++;
} else {
zeros++;
}
}
for (i64 x : cumXor) {
if ((x >> l) & 1) {
ans += mint(zeros) * mint(p2);
} else {
ans += mint(ones) * mint(p2);
}
}
p2 *= 2;
}
cout << (ans / 2).val() << endl;
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long int
#define fast ios_base::sync_with_stdio(false)
#define fast_input cin.tie(NULL)
#define fast_output cout.tie(NULL)
#define vi vector<long long int>
#define vp vector<pair<long long int ,long long int>>
#define pb push_back
#define mp make_pair
#define pp pop_back
#define iter vector<int>::iterator
#define pa pair<long long int ,long long int>
#define f(a,b) for(int a=0;a<b;a++)
#define mod 1000000007
#define F first
#define S second
#define sett set<long long int>
#define um unordered_map<ll,ll>
#define ordered_set tree<pa, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
vector<pa>g[1000001];
ll dp[1000001];
void dfs(ll v,ll par=-1)
{
for(pa d:g[v])
{
if(d.first-par)
{
dp[d.first]=dp[v]^d.second;
dfs(d.first,v);
}
}
}
void solve()
{
ll n;
cin>>n;
f(i,n-1)
{
ll x,y,w;
cin>>x>>y>>w;
x--,y--;
g[x].pb({y,w});
g[y].pb({x,w});
}
dfs(0);
ll ans=0;
f(j,61)
{
ll ones,zeros;
ones=zeros=0;
f(i,n)
{
if(dp[i]&(1ll<<j))
{
ones++;
}
else
{
zeros++;
}
}
zeros*=ones;
zeros%=mod;
ans+=((1ll<<j)%mod*zeros%mod);
ans%=mod;
}
cout<<ans<<endl;
}
int main() {
fast;
fast_input;
fast_output;
// ll t;
// cin>>t;
// while(t--)
// {
solve();
// }
return 0;
} |
#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
map<pll, ll> mp;
ll arr[300020];
ll pre[300020];
void Main(){
int n; cin >> n;
for (int i = 1; i <= n; i++){ cin >> arr[i]; }
ll ans = 0;
mp[mkp(0, 0)] = 1;
for (int i = 1; i <= n; i++){
pre[i] = arr[i] - pre[i-1];
pll p = {pre[i], i&1};
if (mp.count(p) == 0){ mp[p] = 1; }
else{ mp[p] = mp[p] + 1; }
pll p1 = {pre[i], i&1}, p2 = {-pre[i], ~i&1};
ll x1 = mp[p1], x2 = mp[p2];
ans += x1-1 + x2;
}
cout << ans;
}
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();
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
long long MOD =1e9+7;
vector<vector<long long> > m(3e3+10,vector<long long>(3e3+10,-1));
vector<long long> v;
vector<vector<int> > tps;
long long bourrin(int i, int from) {
if(from == -1) return 0;
if(m[i][from]!= -1) {
return m[i][from];
}
if(from >= v.size()) {
return 1;
}
long long res = bourrin(i,tps[from][i]);
if(tps[from][i+1] != -1) {
res += bourrin(i+1,tps[from][i+1]);
}
res%=MOD;
m[i][from] = res;
return res;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
cin>>N;
v=vector<long long>(N);
for(int c=0;c<N;c++) cin>>v[c];
vector<vector<int> > modulos(N+10,vector<int>(N+10,-2));
for(int c=0;c<N+10;c++) modulos[c][0]=-1;
vector<long long> current(N+10,0);
tps=vector<vector<int> > (N+10,vector<int> (N+10,-1));
reverse(v.begin(),v.end());
for(int c=0;c<N;c++) {
for(int c2=1;c2<=N;c2++){
current[c2] += v[c];
current[c2] %= c2;
if(modulos[c2][current[c2]] != -2) {
tps[N-1-c][c2] = N-1-modulos[c2][current[c2]];
}
modulos[c2][current[c2]]=c;
}
}
// return 0;
reverse(v.begin(),v.end());
cout<<bourrin(1,1);
}
|
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pll;
#define x first
#define y second
#define mp make_pair
const int MAXN = (int)2e5;
int n, q;
ll a[MAXN + 5], t[MAXN + 5], x[MAXN + 5], sol[2 * MAXN + 5];
vector <int> ms[2 * MAXN + 5];
set <pll> s;
void dfs(int x, int p){
for(vector <int>::iterator i = ms[x].begin() ; i != ms[x].end() ; ++i){
int o = *i;
if(o == p)
continue;
sol[o] = sol[x];
dfs(o, x);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1 ; i <= n ; ++i){
cin >> a[i] >> t[i];
}
cin >> q;
for(int i = 1 ; i <= q ; ++i){
cin >> x[i];
s.insert(mp(x[i], i));
}
ll sad = 0;
for(int i = 1 ; i <= n ; ++i){
if(t[i] == 1){
sad += a[i];
}
else if(t[i] == 2){
while(!s.empty() && (s.begin())->x <= a[i] - sad){
int o = s.begin()->y;
s.erase(s.begin());
ms[i + q].push_back(o);
}
s.insert(mp(a[i] - sad, i + q));
}
else{
while(!s.empty() && (--s.end())->x >= a[i] - sad){
int o = (--s.end())->y;
s.erase((--s.end()));
ms[i + q].push_back(o);
}
s.insert(mp(a[i] - sad, i + q));
}
}
for(set <pll>::iterator i = s.begin() ; i != s.end() ; ++i){
pll o = *i;
sol[o.y] = o.x + sad;
dfs(o.y, -1);
}
for(int i = 1 ; i <= q ; ++i){
cout << sol[i] << "\n";
}
return 0;
}
| #ifdef _DEBUG
#define _GLIBCXX_DEBUG
#endif
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, ll>;
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i--)
const int MOD = 1000000007;
const int MOD2 = 998244353;
const ll INF = 1e18;
const ld PI = acos(-1);
class UnionFind {
public:
vector<int> par;
vector<int> rnk;
vector<ll> siz;
vector<int> isequal;
UnionFind(int N): par(N), rnk(N), siz(N), isequal(N){
for(int i = 0; i < N; i++){
par[i] = i, rnk[i] = 0, siz[i] = 1;
}
}
//根
int find(int x){
if(par[x] ==x )return x;
return par[x] = find(par[x]);
}
void unite(int x, int y){
int rx = find(x);
int ry = find(y);
if (rx == ry) return;
if(rnk[rx] < rnk[ry]){
par[rx] = ry;
siz[ry] += siz[rx];
siz[rx] = 0;
}
else{
par[ry] = rx;
siz[rx] += siz[ry];
siz[ry] = 0;
if(rnk[rx] == rnk[ry]){
rnk[rx]++;
}
}
}
//集合が同じか
bool same(int x, int y){
return find(x) == find(y);
}
//集合の大きさ
ll size(int x){
return siz[find(x)];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N,Q;
cin >> N;
vector<ll> A(N),T(N);
ll delta=0;
rep(i,0,N-1){
cin >> A[i] >> T[i];
if(T[i]==1) delta+=A[i];
}
cin >> Q;
vector<int> X(Q);
rep(i,0,Q-1){
cin >> X[i];
}
ll mx=INF,mn=-INF;
rep(i,0,N-1){
if(T[i]==1){
mx+=A[i];
mn+=A[i];
}
else if(T[i]==2){
mx=max(A[i],mx);
mn=max(A[i],mn);
}
else if(T[i]==3){
mx=min(A[i],mx);
mn=min(A[i],mn);
}
}
/*cout << delta << endl;
cout << mx << ' ' << mn << endl;*/
rep(i,0,Q-1){
if(X[i]+delta>=mx){
cout << mx << endl;
}
else if(X[i]+delta<=mn){
cout << mn << endl;
}
else{
cout << X[i]+delta << endl;
}
}
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define int long long
#define ll long long
#define all(v) v.begin(), v.end()
#define intvect vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define fo(i, n) for (int i = 0; i < n; ++i)
#define Fo(i, k, n) for (int i = k; i < n; ++i)
#define ld long double
#define deb(x) cout << #x << " " << x << endl;
#define pb push_back
#define pob pop_back
#define lcm(a, b) (a / __gcd(a, b) * b)
#define F first
#define S second
#define ull unsigned long long
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
const ll mod = 10000007;
const ll N = (ll)5e5 + 4;
template <typename... T>
void read(T &... args)
{
((cin >> args), ...);
}
template <typename... T>
void write(T &&... args)
{
((cout << args), ...);
}
void vjudge()
{
#ifndef RTE
if (fopen("input.txt", "r"))
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
bool comp(pair<int, int> x, pair<int, int> y)
{
if (x.first != y.first)
return x.first < y.first;
return x.second < y.second;
}
void solve()
{
int n, w;
cin >> n >> w;
vector<pair<int, int>> e;
for (int i = 0; i < n; ++i)
{
int a, b, p;
cin >> a >> b >> p;
e.emplace_back(a, -p);
e.emplace_back(b - 1, p);
}
sort(e.begin(), e.end());
pair<int, int> ans = {0, -1};
int cur = 0;
for (pair<int, int> p : e)
{
cur += -p.second;
if (cur > ans.first)
{
ans = {cur, p.first};
}
}
if (ans.first > w)
cout << "No";
else
cout << "Yes";
}
int32_t main()
{
clock_t start, end;
start = clock();
vjudge();
int t = 1;
// cin >> t;
while (t--)
solve();
end = clock();
double waste = double(end - start) / double(CLOCKS_PER_SEC);
cerr << "Time wasted by program is : " << fixed
<< waste << setprecision(15);
cerr << " sec " << endl;
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 998244353
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define vpii vector<pair<int,int> >
#define all(x) x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y) for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x) (int)x.size()
#define db(x) cout<<x<<"\n";
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//insert,find_by_order,order_of_key,lower_bound,upper_bound;
#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif
//////////////////////////////code//////////////////////////////
const int N = 1e3 + 5;
vector<pair<int,char> > adj[N];
int is[N][N];
int dp[N][N];
void solve()
{
int n,m;
cin >> n >> m;
for(int i = 0;i < m; i++)
{
int u,v;
char w;
cin >> u >> v >> w;
adj[u].pb({v,w});
adj[v].pb({u,w});
is[u][v] = is[v][u] = 1;
//deb(v,u);
//deb(sz(adj[u]),sz(adj[v]));
}
queue<pii>q;
// int dp[n + 1][n + 1];
for(int i = 1;i <= n; i++)
for(int j = 1;j <= m; j++)
dp[i][j] = inf;
dp[1][n] = 0;
q.push({1,n});
int ans = inf;
// deb(sz(adj[n]));
while(!q.empty())
{
pii p = q.front();
int node1 = p.ff, node2 = p.ss;
q.pop();
if(is[node1][node2])
{
ans = min(ans,dp[node1][node2] + 1);
//deb(node1,node2,"1");
continue;
}
//deb(node1,node2);
for(auto &c1 : adj[node1])
{
//deb("OK");
for(auto &c2 : adj[node2])
{
//deb("OK");
int child1 = c1.ff , child2 = c2.ff;
int col1 = c1.ss , col2 = c2.ss;
// deb("Ok");
if(col1 != col2 || dp[child1][child2] != inf)
continue;
dp[child1][child2] = 2 + dp[node1][node2];
if(child1 != child2)
{
q.push({child1,child2});
// deb(child1,child2,"Push");
}
else
{
ans = min(ans,dp[child1][child2]);
// deb(child1,child2,"2");
}
}
}
}
if(ans == inf)
db(-1)
else
db(ans)
}
int32_t main()
{
LETS_GET_SCHWIFTY;
#ifndef ONLINE_JUDGE
freopen("INP.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
//cin >> t;
while (t--)
solve();
}
// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ans = 0;
template<typename T>
struct HamiltonGraph {
int n;
T M;
vector<vector<T>> DP, dt, rt;
const T INF = numeric_limits<T>::max();
HamiltonGraph (const int n_) : n(n_), M(1ll<<n_) {
dt.assign(n_, vector<T>(n_, INF));
}
T Calc_MinimumCost (int s, int g = -1){
DP.assign(M, vector<T>(n, INF));
rt.assign(M, vector<T>(n, -1));
DP[1<<s][s] = 0;
for(ll i = 0; i < M; i++){
for(int j = 0; j < n; j++){
if(i & (1ll<<j)){
for(int k = 0; k < n; k++){
if(DP[i][j] != INF && DP[i|(1ll<<k)][k] > DP[i][j]+dt[j][k]){
DP[i|(1ll<<k)][k] = DP[i][j]+dt[j][k];
rt[i|(1ll<<k)][k] = j;
}
}
}
}
}
if(g < 0) g = min_element(DP[M-1].begin(), DP[M-1].end())-DP[M-1].begin();
return DP[M-1][g];
}
vector<int> Build_ShortestPath (int s = 0, int g = -1){
vector<int> path;
T m = M-1;
if(g < 0) g = min_element(DP[M-1].begin(), DP[M-1].end())-DP[M-1].begin();
int now, nxt; now = nxt = g;
if(s == g){
path.push_back(g);
now = nxt = rt[m][g];
ans += dt[now][g];
}
while(m != (1<<s) || now != s){
path.push_back(now);
now = rt[m][now];
ans += dt[now][nxt];
m ^= (1<<nxt);
nxt = now;
}
path.push_back(now);
reverse(path.begin(), path.end());
return path;
}
};
int main()
{
int n;
cin >> n;
vector<ll> x(n),y(n),z(n);
for (ll i = 0; i < n; ++i) {
cin >> x[i] >> y[i] >> z[i];
}
HamiltonGraph<ll> g(n);
for (ll i = 0; i < n; i++){
for (ll j = 0; j < n; j++){
if(z[j]-z[i] > 0) g.dt[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]) + z[j]-z[i];
else g.dt[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]);
}
}
g.Calc_MinimumCost(0,0);
g.Build_ShortestPath(0,0);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
const int MAX_N = 18;
int dp[(1<<MAX_N)][MAX_N];
void init(){
rep(i,(1<<MAX_N)){
rep(j,MAX_N){
dp[i][j] = INF;
}
}
}
int main() {
int n; cin >> n;
vector<vector<int> > dist(n,vector<int>(n,-INF));
vi x(n), y(n), z(n);
rep(i,n){
cin >> x[i] >> y[i] >> z[i];
}
rep(i,n){
rep(j,n){
if(i == j){
dist[i][j] = 0;
continue;
}
dist[i][j] = abs(x[j]-x[i]) + abs(y[j]-y[i]) + max(0,z[j]-z[i]);
}
}
init();
rep(i,n){
if(i == 0) continue;
dp[1<<i][i] = dist[0][i];
}
rep(b,(1<<n)){
if(b == 0) continue;
rep(j,n){
if(!((b>>j)&1)) continue;
rep(k,n){
if((b>>k)&1) continue;
dp[b|(1<<k)][k] = min(dp[b|(1<<k)][k], dp[b][j]+dist[j][k]);
}
}
}
cout << dp[(1<<n)-1][0] << 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<998244353>;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,k;
cin >> n >> m >> k;
if (n == 1) {
if (m == 1) {
cout << k << "\n";
} else {
mint x = k;
cout << pow(x,m) << "\n";
}
} else {
if (m == 1) {
mint x = k;
cout << pow(x,n) << "\n";
} else {
mint ans = 0;
for (int q = 1; q <= k; q++) {
mint x = pow(mint(q),n) - pow(mint(q-1),n);
x *= pow(mint(k-q+1),m);
ans += x;
}
cout << ans << "\n";
}
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
const int N = 40, M = 1<<(N / 2);
int n, t, a[N];
vector<ll> v1(M), v2(M);
int main() {
cin >> n >> t;
rep(i, n) cin >> a[i];
rep(bit, M) {
ll now = 0;
rep(i, N / 2) {
if (bit & 1<<i) {
now += a[i];
}
}
v1[bit] = now;
}
rep(bit, M) {
ll now = 0;
rep(i, N / 2) {
if (bit & 1<<i) {
now += a[N / 2 + i];
}
}
v2[bit] = -now;
}
sort(v2.begin(), v2.end());
ll ans = 0;
rep(i1, M) {
ll now = v1[i1];
if (now > t) continue;
int i2 = lower_bound(v2.begin(), v2.end(), now - t) - v2.begin();
now -= v2[i2];
ans = max(ans, now);
}
cout << ans << endl;
return 0;
}
|
#include <cstdio>
#include <cstdlib>
using namespace std;
#define ll long long
const ll MAXN = 400011;
struct fhqTreap {
ll tot, rt;
ll val[MAXN], sum[MAXN], sz[MAXN], ls[MAXN], rs[MAXN], rd[MAXN];
void pushup(ll u) {sz[u] = sz[ls[u]] + sz[rs[u]] + 1; sum[u] = sum[ls[u]] + sum[rs[u]] + val[u];}
void split(ll u, ll k, ll &x, ll &y) {
if(!u) {x = y = 0; return;}
if(val[u] <= k) x = u, split(rs[u], k, rs[u], y);
else y = u, split(ls[u], k, x, ls[u]);
pushup(u);
}
ll merge(ll u, ll v) {
if(!u || !v) return u | v;
if(rd[u] < rd[v]) {rs[u] = merge(rs[u], v); pushup(u); return u;}
else {ls[v] = merge(u, ls[v]); pushup(v); return v;}
}
ll add(ll k) {
rd[++tot] = rand();
sz[tot] = 1;
val[tot] = sum[tot] = k;
return tot;
}
void insert(ll k) {
ll x, y;
split(rt, k, x, y);
rt = merge(merge(x, add(k)), y);
}
ll cb(ll k) {
ll x, y;
split(rt, k, x, y);
ll ans = sz[x] * k + sum[y];
rt = merge(x, y);
return ans;
}
void delet(ll k) {
ll x, y, z;
split(rt, k, x, z);
split(x, k - 1, x, y);
y = merge(ls[y], rs[y]);
rt = merge(merge(x, y), z);
}
} t[2];
ll q;
ll len[2];
ll w[2][MAXN];
ll read() {
ll X = 0, F = 1; char C = getchar();
while(C < '0' || C > '9') {if(C == '-')F=-1; C = getchar();}
while(C >= '0' && C <= '9') {X = X*10+C-'0'; C = getchar();}
return X * F;
}
int main() {
ll ans = 0;
len[1] = read(); len[0] = read(); q = read();
for(ll i = 0; i < 2; ++i)
for(ll j = 1; j <= len[i]; ++j)
t[i].rt = t[i].merge(t[i].rt, t[i].add(0));
for(ll i = 1, opt, x, y; i <= q; ++i) {
opt = (read() & 1); x = read(); y = read();
t[opt].delet(w[opt][x]);
t[opt].insert(y);
ans -= t[opt^1].cb(w[opt][x]);
w[opt][x] = y;
ans += t[opt^1].cb(w[opt][x]);
printf("%lld\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <typename T>
struct binary_indexed_tree{
int N;
vector<T> BIT;
binary_indexed_tree(int n){
N = 1;
while (N < n){
N *= 2;
}
BIT = vector<T>(N + 1, 0);
}
void add(int i, T x){
i++;
while (i <= N){
BIT[i] += x;
i += i & -i;
}
}
T sum(int i){
T ans = 0;
while (i > 0){
ans += BIT[i];
i -= i & -i;
}
return ans;
}
T sum(int L, int R){
return sum(R) - sum(L);
}
T all(){
return BIT[0];
}
};
int main(){
int N, M, Q;
cin >> N >> M >> Q;
vector<int> T(Q), X(Q), Y(Q);
for (int i = 0; i < Q; i++){
cin >> T[i] >> X[i] >> Y[i];
X[i]--;
}
vector<int> c = Y;
c.push_back(0);
sort(c.begin(), c.end());
c.erase(unique(c.begin(), c.end()), c.end());
int cnt = c.size();
binary_indexed_tree<long long> Acnt(cnt), Bcnt(cnt);
binary_indexed_tree<long long> Asum(cnt), Bsum(cnt);
Acnt.add(0, N);
Bcnt.add(0, M);
vector<int> a(N, 0), b(M, 0);
long long ans = 0;
for (int i = 0; i < Q; i++){
if (T[i] == 1){
int curr_id = lower_bound(c.begin(), c.end(), a[X[i]]) - c.begin();
int nxt_id = lower_bound(c.begin(), c.end(), Y[i]) - c.begin();
ans -= Bsum.sum(curr_id, nxt_id);
ans -= Bcnt.sum(0, curr_id) * a[X[i]];
ans += Bcnt.sum(0, nxt_id) * Y[i];
Acnt.add(curr_id, -1);
Asum.add(curr_id, -a[X[i]]);
Acnt.add(nxt_id, 1);
Asum.add(nxt_id, Y[i]);
a[X[i]] = Y[i];
}
if (T[i] == 2){
int curr_id = lower_bound(c.begin(), c.end(), b[X[i]]) - c.begin();
int nxt_id = lower_bound(c.begin(), c.end(), Y[i]) - c.begin();
ans -= Asum.sum(curr_id + 1, nxt_id + 1);
ans -= Acnt.sum(0, curr_id + 1) * b[X[i]];
ans += Acnt.sum(0, nxt_id + 1) * Y[i];
Bcnt.add(curr_id, -1);
Bsum.add(curr_id, -b[X[i]]);
Bcnt.add(nxt_id, 1);
Bsum.add(nxt_id, Y[i]);
b[X[i]] = Y[i];
}
cout << ans << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define fi first
#define se second
#define UP(a,b,c) for(ll (a)=(b);(a)<(c);(a)++)
#define UU(a,b,c) for(ll (a)=(b);(a)<=(c);(a)++)
#define DN(a,b,c) for(ll (a)=(b);(a)>(c);(a)--)
#define DU(a,b,c) for(ll (a)=(b);(a)>=(c);(a)--)
int fen[300005], n, q, a[300005], t, x, y;
int get(int idx)
{
int res = 0;
while(idx > 0)
{
res ^= fen[idx];
idx -= (idx & -idx);
}
return res;
}
void update(int idx, int val)
{
while(idx <= n)
{
fen[idx] ^= val;
idx += (idx & -idx);
}
}
void reset()
{
}
void input()
{
cin >> n >> q;
UU(i, 1, n)
{
cin >> a[i];
update(i, a[i]);
}
}
void solve()
{
while(q--)
{
cin >> t >> x >> y;
if(t == 1)
{
update(x, y);
}
else
{
int a = get(x - 1);
int b = get(y);
cout << (a ^ b) << endl;
}
}
}
void LetsRock()
{
solve();
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
input();
reset();
LetsRock();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, M, T;
int R = 0;
cin >> N >> M >> T;
int L = N;
for (int i = 0; i < M; i++){
int A = 0, B = 0;
cin >> A >> B;
L -= (A - R);
if (L <= 0){
break;
}
else{
L += (B - A);
if(L > N) {
L = N;
}
R = B;
}
}
if (L > 0) {
L -= (T - R);
}
else{
}
if (L > 0) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#ifdef Dhiraj
#include "D:/dhiraj/Programming/debug.h"
#else
#define d(...) 11
#define cerr if(0) cerr
#endif
#define ll long long int
#define endl '\n'
template <typename T, typename U>
inline istream& operator >> (istream& in, pair<T, U>& p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
inline istream& operator >> (istream& in, vector<T>& v) {
for(T& x : v) in >> x;
return in;
}
const ll MX = 4e5 + 5;
ll n, m;
vector<vector<ll>> g;
vector<ll> vis;
void clearAll()
{
vis.assign(MX, 0);
g.assign(MX, vector<ll>());
}
ll cc, edges;
void dfs(ll u)
{
if(vis[u] || g[u].empty()) return;
cc++;
vis[u] = 1;
for(ll v : g[u]) {
edges++;
dfs(v);
}
}
void solve(int &tc)
{
cin >> n;
clearAll();
for(ll i = 0; i < n; i++)
{
ll x, y;
cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
ll ans = 0;
for(ll i = 0; i < MX; i++)
{
cc = edges = 0;
dfs(i);
edges /= 2;
ans += min(cc, edges);
}
cout << ans << endl;
}
int main()
{
#ifdef Dhiraj
freopen("D:/dhiraj/Programming/i1.txt", "r", stdin);
freopen("D:/dhiraj/Programming/o1.txt", "w", stdout);
// freopen("D:/dhiraj/Programming/e1.txt", "w", stderr);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int tc = 1;
for(int i = 1; i <= tc; i++) {
cerr << "Case #" << i << "\n";
solve(i);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int sum=0;
int dd=0;
int vg=0;
int d[400000];
vector<int> ed[400000];
int vis[400000];
void dfs(int v,int p)
{
//cout<<v<<endl;
sum+=ed[v].size();
vis[v]=1;
vg++;
if(d[v]!=0) dd++;
if(ed[v].size()==0) return;
for(int i=0;i<ed[v].size();i++)
if(vis[ed[v][i]]==0) dfs(ed[v][i],v);
}
int main() {
int n;
cin>>n;
for(int i=0;i<400000;i++) {vis[i]=0;d[i]=0;}
while(n--)
{
int x,y;
cin>>x>>y;
x--;y--;
if(x!=y)
{
ed[x].push_back(y);
ed[y].push_back(x);
}
else d[x]++;
}
int ans=0;//cout<<d[110]<<endl;
for(int i=0;i<400000;i++)
{
if(vis[i]==0)
{
dfs(i,-1);
//if(i==110) cout<<dd<<endl;
if(sum/2==vg-1 && dd==0) ans+=vg-1;
else ans+=vg;
sum=0;
dd=0;
vg=0;
}
}
cout<<ans<<endl;
return 0;
} |
#include <iostream>
using namespace std;
int main(){
int n,sum=0;
int a[n];
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
if (a[i]>10){
sum+=(a[i]-10);
}
}
cout<<sum;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <stack>
#include <queue>
#include <vector>
static const int IINF = 1 << 30;
static const long long LINF = 1LL << 60;
static const long long MOD = 1.0e+9 + 7;
template <typename T> std::vector<T> vectors(std::size_t n, T val) {
return std::vector<T>(n, val);
}
template <typename T, typename... Args>
auto vectors(std::size_t n, Args... args) {
return std::vector<decltype(vectors<T>(args...))>(n, vectors<T>(args...));
}
template <class T> inline bool chmin(T &a, const T &b) {
return (a > b) ? a = b, true : false;
}
template <class T> inline bool chmax(T &a, const T &b) {
return (a < b) ? a = b, true : false;
}
template <class T> inline void chadd(T &a, const T &b) {
a += b, a %= MOD;
// TODO minus case
}
template <class T>
std::ostream &operator<<(std::ostream &stream, const std::vector<T> &vec) {
for (auto val : vec) {
if (std::is_fundamental<T>::value)
stream << " " << val;
else
stream << std::endl << val;
}
return stream;
}
int main() {
int N;
std::cin >> N;
std::vector<int> A(N);
for (int i = 0; i < N; ++i)
std::cin >> A[i];
int res = 0;
for (int i = 0; i < N; ++i) {
if (A[i] >= 10) {
res += A[i] - 10;
}
}
std::cout << res << std::endl;
}
|
#include <bits/stdc++.h>
#define fi first
#define se second
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define V vector
#define TP template <class o>
#define TPP template <typename t1, typename t2>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = b; i >= a; i--)
#define FOR(i, n) rep(i, 1, n)
#define debug(x) cerr << #x << ' ' << '=' << ' ' << x << endl
using namespace std;
typedef unsigned ui;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
// char buf[1 << 20],*p1=buf,*p2=buf;
TP void qr(o& x) {
char c = gc;
x = 0;
int f = 1;
while (!isdigit(c)) {
if (c == '-')
f = -1;
c = gc;
}
while (isdigit(c))
x = x * 10 + c - '0', c = gc;
x *= f;
}
template <class o, class... O> void qr(o& x, O&... y) {
qr(x);
qr(y...);
}
TP void qw(o x) {
if (x / 10)
qw(x / 10);
putchar(x % 10 + '0');
}
TP void pr1(o x) {
if (x < 0)
x = -x, putchar('-');
qw(x);
putchar(' ');
}
template <class o, class... O> void pr1(o x, O... y) {
pr1(x);
pr1(y...);
}
TP void pr2(o x) {
if (x < 0)
x = -x, putchar('-');
qw(x);
putchar(10);
}
template <class o, class... O> void pr2(o x, O... y) {
pr2(x);
pr2(y...);
}
TP void cmax(o& x, o y) {
if (x < y)
x = y;
}
TP void cmin(o& x, o y) {
if (x > y)
x = y;
}
const int mod = 998244353;
TPP void ad(t1& x, t2 y) {
x += y;
if (x >= mod)
x -= mod;
}
TPP void dl(t1& x, t2 y) {
x -= y;
if (x < 0)
x += mod;
}
const int N = 5010, inf = 2e9;
const ll INF = 1e15;
int n, m, f[N], g[N], C[N][N];
void solve() {
qr(n, m);
f[m] = 1;
rep(i, 0, n) {
C[i][0] = 1;
FOR(j, i) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod;
}
if (m & 1) {
return pr2(0);
}
while (m) {
memcpy(g, f, sizeof g);
memset(f, 0, sizeof f);
for (int i = 0; i <= m; i += 2)
if (g[i]) {
for (int j = 0; j <= i; j += 2)
ad(f[(i - j) / 2], (ll)g[i] * C[n][j] % mod);
}
m /= 2;
}
pr2(f[0]);
}
int main() {
#ifndef ONLINE_JUDGE
clock_t start_time = clock();
#endif
int T = 1;
// qr(T);
while (T--)
solve();
#ifndef ONLINE_JUDGE
cerr << 1.0 * (clock() - start_time) / CLOCKS_PER_SEC << ' ' << 's' << endl;
#endif
return 0;
} | #include<bits/stdc++.h>
#define inf 1e18
#define endl "\n"
#define mp make_pair
#define pb push_back
#define loop(i,x,y) for(int i = x; i < y ; i++ )
#define all(x) (x).begin(),(x).end()
#define in(n) int n; cin>>n;
#define inarr(arr,n) vector<int>arr(n); for(int i = 0; i < n ; i++){cin>>arr[i];}
#define maploop(x) for(auto itr = x.begin(); itr != x.end();itr++)
#define int long long
using namespace std;
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
const int N = 50001;
const int mod = 998244353;
int fac[N];
int mul(int a, int b)
{
return (a * b) % mod;
}
void pre()
{
fac[0] = 1;
loop(i, 1, N)
{
fac[i] = mul(fac[i - 1], i);
}
}
int pow(int a, int b)
{
int z = 1;
while (b > 0 )
{
if (b % 2)
{
z = mul(a, z);
}
a = mul(a, a);
b = b / 2;
}
return z;
}
int div(int a)
{
int ans;
return ans = pow(a, mod - 2);
}
int comb(int n, int k)
{
if (n >= k)
{
return mul(fac[n], div(mul(fac[k], fac[n - k])));
}
else
return 0;
}
void Main()
{
in(n)
in(m)
vector<int>vis(m + 1, 0);
vis[0] = 1;
loop(i, 0, 18)
{
vector<int>temp(m + 1, 0);
//temp = vis;
int inc = 0;
for (int j = 0 ; j <= n; j += 2)
{
loop(k, 0, m + 1)
{
if (inc + k > m)
break;
//cout << inc + k << endl;
temp[inc + k] += (vis[k] * comb(n, j)) % mod;
if (temp[inc + k] >= mod)
{
temp[inc + k] -= mod;
}
}
inc += 2 * (1 << i);
}
vis = temp;
}
cout << vis[m] << endl;
}
int32_t main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
pre();
// cin >> t;
while (t--)
{
Main();
}
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
typedef long long ll;
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const vector<T>&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.first>>v.second;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.first<<","<<v.second;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const vector<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
#ifdef _DEBUG
inline void dump() { cerr << endl; }
template <typename Head> void dump(Head &&head) { cerr << head; dump(); }
template <typename Head, typename... Tail> void dump(Head &&head, Tail &&... tail) { cerr << head << ", "; dump(forward<Tail>(tail)...); }
#define debug(...) do { cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; dump(__VA_ARGS__); } while (false)
#else
#define dump(...)
#define debug(...)
#endif
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
const ll LINF = 1LL << 60;
const int INF = 1001001001;
/////////////////////////////////////////////////////////////////////
int main()
{
int n,k; cin>>n>>k;
vector<vector<int>> t(n, vector<int>(n));
rep(i, n) rep(j, n) cin>>t[i][j];
vector<int> a(n-1);
rep(i, n-1) a[i] = i+1;
int ans = 0;
do {
int sum = t[0][a[0]] + t[a[n-2]][0];
rep(i, n-2) {
sum += t[a[i]][a[i+1]];
}
if (sum == k) ans++;
} while (next_permutation(a.begin(), a.end()));
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using iPair = pair<int,int>;
using lPair = pair<ll, ll>;
using ivector = vector<int>;
using lvector = vector<ll>;
using istack = stack<int>;
using iqueue = queue<int>;
using ivv = vector<vector<int>>;
using lvv = vector<vector<ll>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};
#define dump(x) cout << #x << " = " << (x) << endl
#define ALL(x) begin(x),end(x)
#define rep(i,s,e) for(ll i=(s), i_stop=(e); i<i_stop; ++i)
#define repRev(i,s,e) for(ll i=(e)-1, i_stop=(s); i>=i_stop; --i)
#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)
#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)
#define foreach(x,container) for(auto &&x:container)
template<typename T> bool chmax(T& a, const T b) {if(a<b) {a=b;return true;} return false;}
template<typename T> bool chmin(T& a, const T b) {if(a>b) {a=b;return true;} return false;}
template<typename T> void printArr(vector<T> &arr){
for(auto &x:arr) {cout << x << " ";} cout << endl;
}
/*
## 關鍵詞:
## 題意:
## 思路:
## 雜多:
*/
void solve() {
int n,m; cin>>n>>m;
vector<vector<tuple<int,ll,ll>>> edge(n+1);
range(i, 1, m) {
int a,b; ll c,d; cin>>a>>b>>c>>d;
edge[a].emplace_back(b,c,d);
edge[b].emplace_back(a,c,d);
}
using P = pair<ll,int>;
priority_queue<P, vector<P>, greater<P>> pq;
lvector dist(n+1, LINF);
dist[1] = 0; pq.emplace(0, 1);
while(pq.size()) {
auto [t, u] = pq.top(); pq.pop();
if(dist[u] < t) continue;
if(u == n) {
cout << t << endl;
return;
}
// 到達頂點u所需的最短時刻爲t
for(auto [v,c,d]: edge[u]) {
// 對每個鄰點v求最短時刻v_t
// time of u->v:
// = t + c + floor(d/t+1)
// = floor(t + c + d/(t+1))
// = floor(t + 1 + d/(t+1) + (c-1))
// 所以答案大致在滿足 (t+1)^2 = d 的t附近
double _x = sqrt((double)d); _x -= 1;
int x = (int)_x;
int bestx = 0; ll ans = LINF;
for(int i = max(0, x-2); i <= x+2; ++i) {
if(chmin(ans, i+c+d/(i+1))) bestx = i;
}
// 當 t = bestx 的時候這條路的經過時間取到最小值
if(t >= bestx) { // 立即出發
if(chmin(dist[v], t+c+d/(t+1)))
pq.emplace(dist[v], v);
}else{ // 待機到bestx
if(chmin(dist[v], bestx+c+d/(bestx+1)))
pq.emplace(dist[v], v);
}
}
}
cout << "-1" << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
} |
/**Bismillahir Rahmanir Rahim.**/
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
#define Faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi acos(-1);
#define test int t; cin>>t; while(t--)
#define dot(x) fixed<<setprecision(x)
const double EPS = 1e-12;
bool isEqual(ld a, ld b) {
return abs(a - b) <= EPS;
}
bool isGreater(ld a, ld b) {
return a > b + EPS;
}
bool isSmaller(ld a, ld b) {
return a + EPS < b;
}
// cin.ignore();
//scanf("%[^\n]%*c", str);
//sort(s,s+n,greater<int>());
//cin.getline(array,size);
//s.erase(s.begin()+s.size()-1,s.end()); last index
int main()
{
#ifndef ONLINE_JUDGE
clock_t tStart = clock();
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
Faster
ll n,m,t,a[10001],b[10001],s=0,p=0,q=0,i;
cin>>n>>m>>t;
s=n;
for(i=0;i<m;i++)
{
cin>>a[i]>>b[i];
}
for(i=0;i<m;i++)
{
if(i==0)
{
if(s-a[i]<=0)
{
s=0;
p=1;
}
else
s=s-a[i];
s+=(b[i]-a[i]);
if(s>n)
s=n;
}
else
{
s-=(a[i]-b[i-1]);
if(s<=0)
{
s=0;
p=1;
}
s+=(b[i]-a[i]);
if(s>n)
s=n;
}
}
if(t-b[m-1]>=s)
p=1;
if(p==1)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
#ifndef ONLINE_JUDGE
fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
#endif
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
int x;
cin >> n >> x;
int curr = 0;
for(int i = 0; i < n; i++){
int a, b;
cin >> a >> b;
curr += a*b;
if(curr > x*100){
cout << i+1 << endl;
return 0;
}
}
cout << "-1" << endl;
}
|
#include <bits/stdc++.h>
typedef long long ll;
#define rep(i,a,n) for (int i=a;i<n;i++)
using namespace std;
int main()
{
int n,w;
cin>>n>>w;
cout<<n/w;
return 0;
} | #include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
int n,w;
scanf("%d%d",&n,&w);
printf("%d\n",n/w);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std ;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin() , (x).end()
#define sz(x) (int)((x).size())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define ppc __builtin_popcount
#define mp make_pair
void solve(){
float a,b,c,d;
cin>>a>>b>>c>>d;
//int i=0;
if(d*c <= b)
{
cout<< "-1";
return;
}
else{
cout<<(int)ceil((a/(c*d - b))) ;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int k =1;
//cin>>k;
while(k--){
// cout<<n;
solve();
}
}
| #include<iostream>
#include<bitset>
#include<string>
#include<vector>
#include<queue>
#include<random>
#include<map>
#include<set>
#include<stack>
#include<functional>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<ctime>
using namespace std;
using Graph = vector<vector<int>>;
const long long INF = 1LL << 60;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(), v.end()
//ll の最大値 2147483647≒10^9
//long longの最大値 9,223,372,036,854,775,807≒10^18
ll gcd(ll a, ll b) {
if (a % b == 0) {
return b;
}
else {
return gcd(b, a % b);
}
}
ll digit(ll x) {
ll d = 0;
while (x) {
d++;
x /= 10;
}
return d;
}
ll modpow(ll a, ll n, ll mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
struct UnionFind {
vector<int> par, siz;
UnionFind(int n) :par(n, -2), siz(n, 1) {}
void red(int x) {
par[x] = -1;
}
int root(int x) {
if (par[x] == -1) {
return x;
}
else {
return par[x] = root(par[x]);
}
}
bool issame(int x, int y) {
return root(x) == root(y);
}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return false;
}
if (siz[x] < siz[y]) {
swap(x, y);
}
par[y] = x;
siz[x] += siz[y];
return true;
}
int size(int x) {
return siz[root(x)];
}
};
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() {
double A, B, C, D;
cin >> A >> B >> C >> D;
if (C * D - B <= 0) {
cout << -1 << endl;
}
else {
cout << ceil(A / (C * D - B)) << endl;
}
}
|
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(long long i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
ll g = a;
x = 1;
y = 0;
if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
return g;
}
ll crt(ll div1,ll mod1,ll div2,ll mod2){
ll x,y;
ll d=extgcd(div1,div2,x,y);
if(d-1)assert(0);
ll ndiv=div1*div2;
// ll nmod=(x*(mod2-mod1)*div1+mod1)%ndiv;
ll nmod=(mod1*div2%ndiv*(ndiv+y)%ndiv+mod2*div1%ndiv*(ndiv+x)%ndiv)%ndiv;
return nmod;
}
typedef int SegT;
const SegT defvalue=0;
class SegTree{
private:
vector<SegT>val;
int n;
SegT combine(SegT a,SegT b){return a+b;}
public:
SegTree(int size){
n=1;
while(n<size)n<<=1;
val=vector<SegT>(2*n,defvalue);
}
SegTree(const vector<SegT> &in){
n=1;
while(n<in.size())n<<=1;
val=vector<SegT>(2*n,defvalue);
for(int i=n-1+in.size()-1;i>=0;i--){
if(n-1<=i)val[i]=in[i-(n-1)];
else val[i]=combine(val[i*2+1],val[i*2+2]);
}
}
void update(int i,SegT a){
i+=n-1;
val[i]=a;
while(i>0){
i=(i-1)/2;
val[i]=combine(val[i*2+1],val[i*2+2]);
}
}
SegT query(int a,int b,int k=0,int l=0,int r=-1){//[a,b)
if(r==-1)r=n;
if(r<=a||b<=l)return defvalue;
if(a<=l&&r<=b)return val[k];
else return combine(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));
}
void tmp(){
rep(i,val.size())cout<<" "<<val[i];cout<<endl;
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,x;
cin>>n>>m>>x;
vvi in(x,vi(2));
rep(i,x)rep(j,2)cin>>in[i][j],in[i][j]--;
vi h(n,m),w(m,n);
rep(i,x){
cmin(h[in[i][0]],in[i][1]);
cmin(w[in[i][1]],in[i][0]);
}
// show1d(h);
// show1d(w);
int out=0;
rep(i,w[0])out+=h[i];
vp t;
rep(i,w[0])t.pb({h[i],i});
if(w[0]-n){
t.pb({0,w[0]});
loop(i,w[0]+1,n)t.pb({0,i});
}
sort(all(t));
SegTree st(n);
int it=0;
rep(i,h[0]){
//cout<<w[i]<<" "<<st.query(0,w[i])<<endl;
out+=st.query(0,w[i]);
while(it!=t.size()&&t[it].first==i){
// cout<<t[it].first<<" "<<t[it].second<<endl;
st.update(t[it].second,1);
it++;
}
}
cout<<out<<endl;
}
/*
....
.#..
#...
...#
.#..
*/
/*
int q;
cin>>q;
while(q--){
int n,a,b;
cin>>n>>a>>b;//b*x mod n == n-a
int g=gcd(n,b);
if(a%g){cout<<-1<<endl;continue;}
a/=g;b/=g;n/=g;
// cout<<n<<" "<<a<<" "<<b<<endl;
cout<<crt(n,n-a,b,0)/b<<endl;
}
}
// b*x mod n == n-a
// 3x mod 10 == 6
*/
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 400005;
vector<int> BIT(N, 0);
vector<bool> seen(N, false);
void update(int x, int delta) {
for (; x <= N; x += x & -x)
BIT[x] += delta;
}
int query(int x) {
int sum = 0;
for (; x > 0; x -= x & -x)
sum += BIT[x];
return sum;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int h, w, m;
cin >> h >> w >> m;
vector<int> row(h + 1, w + 1), col(w + 1, h + 1);
vector<vector<int>> pos(N);
for (int i = 1; i <= m; ++i) {
int x, y;
cin >> x >> y;
pos[y].push_back(x);
row[x] = min(row[x], y);
col[y] = min(col[y], x);
}
ll res = 0;
for (int i = 1; i < col[1]; ++i) res += row[i] - 1;
for (int i = col[1] + 1; i <= h; ++i) {
update(i, 1);
seen[i] = 1;
}
for (int i = 1; i < row[1]; ++i) {
res += query(col[i] - 1);
for (int &v : pos[i]) {
if (!seen[v]) update(v, 1);
seen[v] = 1;
}
}
cout << res << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
ll x, y;
map<ll, ll> vis, cache;
ll solve(ll val);
int main() {
//freopen("input.txt", "r", stdin);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> x >> y;
cout << solve(y) << endl;
return 0;
}
ll solve(ll val) {
if (vis[val]) return cache[val];
vis[val] = 1;
cache[val] = 1e18;
ll &ans = cache[val];
ans = min(ans, abs(val - x));
if (val == 1) return ans;
if (!(val & 1)) ans = min(ans, 1 + solve(val / 2));
else ans = min(ans, 1 + min(solve(val + 1), solve(val - 1)));
return ans;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
int x,y;
cin>>x>>y;
if(x>=y){
cout<<x-y;
return 0;
}
int ans=y-x;
queue<pair<int,int>>q;
q.push({0,y});
set<int>s;
while(!q.empty()){
pair<int,int>f=q.front();
q.pop();
if(s.find(f.second)!=s.end())
continue;
s.insert(f.second);
ans=min(ans,f.first+abs(x-f.second));
if(f.second<x)
continue;
if(f.second&1){
q.push({f.first+1,f.second-1});
q.push({f.first+1,f.second+1});
}
else
q.push({f.first+1,f.second>>1});
}
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db long double
#define fi first
#define se second
#define mem(x) memset(x,0,sizeof x)
db x0,y00,r;
bool isv(int x,int y){
db dx=1.0*x-x0;
db dy=1.0*y-y00;
dx*=dx;dy*=dy;
dx+=dy;
return dx<=(r)+1e-10;
}
int main(){
cin>>x0>>y00>>r;
int xl=ceil(x0-r);
int xr=floor(x0+r);ll ans=0;
//cout<<xl<<" "<<xr<<endl;
if(xl>xr) {
cout<<0<<endl;
return 0;
}
r*=r;
for(int x=xl;x<=xr;x++){
db dx=x-x0;
dx*=dx;
db dr=r-dx;
if(dr<-1e-10) break;
dr=abs(dr);
dr=sqrt(dr);
//cout<<dr<<endl;
db yl=y00-dr;
db yr=y00+dr;
// cout<<yyl<<" "<<yyr<<endl;
int yyl=ceil(yl);
int yyr=floor(yr);
//cout<<yyl<<" "<<yyr<<endl;
if(yyr-yyl<=20){
for(int j=yyl-1;j<=yyr+1;j++){
ans+=isv(x,j);
}
continue;
}
//if(yyl>yyr) break
for(int j=yyl-5;j<=yyl+5;j++) ans+=isv(x,j);
for(int j=yyr-5;j<=yyr+5;j++) ans+=isv(x,j);
// cout<<yyl<<" "<<yyr<<endl;
ans+=(yyr-yyl-11);
//cout<<"?"<<x<<" "<<yyr-yyl+1<<endl;
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<char, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll INF = 1LL << 60;
struct FastIO {
FastIO() {
cin.tie(0);
ios::sync_with_stdio(0);
}
} fastiofastio;
const ll MOD = 998244353;
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; }
// BEGIN CUT
ll modpow(ll x, ll y, ll m) {
ll a = 1, p = x;
while (y > 0) {
if (y % 2 == 0) {
p = (p * p) % m;
y /= 2;
} else {
a = (a * p) % m;
y--;
}
}
return a;
}
// END CUT
int main() {
int N, M, K;
cin >> N >> M >> K;
if (N == 1) {
cout << modpow(K, M, MOD) << endl;
return 0;
}
if (M == 1) {
cout << modpow(K, N, MOD) << endl;
return 0;
}
ll ans = 0;
for (int x = 1; x <= K; x++) {
ll a = modpow(x, N, MOD);
(a += MOD - modpow(x - 1, N, MOD)) %= MOD;
ll b = modpow(K - x + 1, M, MOD);
(ans += a * b % MOD) %= MOD;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<long long>;
using vll = vector<vector<long long>>;
#define _GLIBCXX_DEBUG
#define rep(i, s, n) for (int i = (int)s; i < (int)n; i++)
#define repr(i, n, s) for (int i = (int)n; (int)s < i; i--)
#define len(a) (int)a.size()
#define all(a) a.begin(), a.end()
const int INF32 = 1 << 30;
const long long INF64 = 1LL << 60;
const double PI = acos(-1);
/* 1次元ベクトルをスペースでつなぎ、改行をつけて出力
vi, vl, list<int>, list<ll>に対応 */
template<class T> string print_elements(T a) {
string str = "";
for (auto itr = a.begin(); itr != a.end(); ++itr) {
str += to_string(*itr) + ' ';
}
str.pop_back(); // 最後の空白を削除
str += '\n';
return str;
}
int main() {
int t;
ll N;
cin >> t >> N;
double tax = (100.0 + t) / 100.0;
double d = ceil(1.0 * N * 100.0 / (1.0 * t));
ll ans = (ll)(d * (100.0 + t) / 100) - 1LL;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define fo(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
ll a[n];
ll ma=0LL;
fo(i,n){
ll x;
cin>>x;
ma=max(ma,x);
a[i]=ma;
}
ll b[n];
fo(i,n){
cin>>b[i];
}
ma=0LL;
ll prefb[n];
for(int i=n-1;i>=0;i--){
ma=max(ma,b[i]);
prefb[i]=ma;
}
ma=0;
for(int i=0;i<n;i++){
ma=max(a[i]*b[i],ma);
cout<<ma<<"\n";
}
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
const int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
const int m = 20;
ll dp[1<<m];
int main() {
ll a,b;
cin >> a >> b;
int n = b-a+1;
vector<int> v(n);
for(ll i=a;i<=b;++i)rep(j,m) if(i%p[j]==0) v[i-a] |= 1<<j;
dp[0] = 1;
rep(i,n)rep(x,1<<m){
if(x&v[i]) continue;
dp[x|v[i]] += dp[x];
}
ll ans = 0;
rep(x,1<<m) ans += dp[x];
cout << ans << endl;
}
| #include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri();
int x = ri();
int a[n];
for (auto &i : a) i = ri();
bool beginning = true;
for (int i = 0; i < n; i++) {
if (a[i] == x) continue;
if (beginning) beginning = false;
else printf(" ");
printf("%d", a[i]);
}
puts("");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define all(n) begin(n),end(n)
typedef vector<int> vi;
typedef vector<string> vs;
const int mod = 1000000007;
const int inf = 1061109567;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
signed main()
{
int n;
cin >> n;
if(!(n%2)) cout << "White" << endl;
else cout << "Black" << endl;
return 0;
} | #include <iostream>
#include <set>
#include <cmath>
using namespace std;
int main(){
long long int n,k; cin>>n;
set<long long int> s;
for(int a=2;a<=100000;a++){
for(int b=2;b<=34;b++){
k=pow(a,b);
if(k>n) break;
else s.insert(k);
}
}
cout<<n-s.size()<<endl;
return 0;
} |
Subsets and Splits