code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
#define int long long
template<typename T>
void chmin(T &a,T b){
if(a>b)a=b;
}
template<typename T>
void chmax(T &a,T b){
if(a<b)a=b;
}
const int INF=1e9+1;
signed main(){
int n;cin>>n;
vector<int> l(n,INF),r(n,-INF);
REP(i,n){
int x,c;cin>>x>>c;c--;
chmin(l[c],x);
chmax(r[c],x);
}
int tmpl=0,tmpr=0,nowl=0,nowr=0;
REP(i,n){
if(l[i]>r[i])continue;
int kaka;
kaka=min(abs(nowl-r[i])+r[i]-l[i]+tmpl,abs(nowr-r[i])+r[i]-l[i]+tmpr);
tmpr=min(abs(nowl-l[i])+r[i]-l[i]+tmpl,abs(nowr-l[i])+r[i]-l[i]+tmpr);
tmpl=kaka;
nowl=l[i],nowr=r[i];
}
cout<<min(tmpl+abs(nowl),tmpr+abs(nowr))<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
long long dis(long long x,long long y){
return abs(x-y);
}
int main()
{
int n;
scanf("%d",&n);
vector<int> lft(n+1,1e9),rit(n+1,-1e9),C;
for(int i=0;i<n;++i){
int x,c;
scanf("%d%d",&x,&c);
lft[c]=min(lft[c],x);
rit[c]=max(rit[c],x);
C.push_back(c);
}
lft[0]=rit[0]=0;
sort(C.begin(),C.end());
C.erase(unique(C.begin(),C.end()),C.end());
C.push_back(0);
vector<vector<long long> >dp(2,vector<long long>(n+1,0));
dp[0][C[0]]=dis(0,rit[C[0]])+dis(rit[C[0]],lft[C[0]]);
dp[1][C[0]]=dis(0,lft[C[0]])+dis(lft[C[0]],rit[C[0]]);
for(int i=1;i<C.size();++i){
dp[0][C[i]]=min(dp[0][C[i-1]]+dis(lft[C[i-1]],rit[C[i]])+dis(rit[C[i]],lft[C[i]])
,dp[1][C[i-1]]+dis(rit[C[i-1]],rit[C[i]])+dis(rit[C[i]],lft[C[i]]));
dp[1][C[i]]=min(dp[0][C[i-1]]+dis(lft[C[i-1]],lft[C[i]])+dis(rit[C[i]],lft[C[i]])
,dp[1][C[i-1]]+dis(rit[C[i-1]],lft[C[i]])+dis(rit[C[i]],lft[C[i]]));
}
printf("%lld\n",min(dp[0][0],dp[1][0]));
return 0;
}
|
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int MOD=1000000007;
int n,m;
string grid[2005];
ll p2[4000005];
int l[2005][2005];
int r[2005][2005];
int up[2005][2005];
int down[2005][2005];
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n>>m;
rep(x,0,n) cin>>grid[x];
p2[0]=1;
rep(x,1,4000005) p2[x]=p2[x-1]*2%MOD;
ll tot=0;
rep(x,0,n) rep(y,0,m) if (grid[x][y]=='.') tot++;
ll ans=tot*p2[tot]%MOD;
rep(x,0,n) rep(y,0,m){
if (grid[x][y]=='#') l[x][y]=0;
else if (y==0) l[x][y]=1;
else l[x][y]=l[x][y-1]+1;
}
rep(x,0,n) rep(y,m,0){
if (grid[x][y]=='#') r[x][y]=0;
else if (y==m-1) r[x][y]=1;
else r[x][y]=r[x][y+1]+1;
}
rep(x,0,n) rep(y,0,m){
if (grid[x][y]=='#') down[x][y]=0;
else if (x==0) down[x][y]=1;
else down[x][y]=down[x-1][y]+1;
}
rep(x,n,0) rep(y,0,m){
if (grid[x][y]=='#') up[x][y]=0;
else if (x==n-1) up[x][y]=1;
else up[x][y]=up[x+1][y]+1;
}
rep(x,0,n) rep(y,0,m) if (grid[x][y]=='.'){
int curr=l[x][y]+r[x][y]+up[x][y]+down[x][y]-3;
ans=(ans-p2[tot-curr]+MOD)%MOD;
//cout<<curr<<endl;
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n) \
cerr << __LINE__ << ": " << #x << " = {"; \
for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
cerr << ((hoge) ? "," : "") << x[hoge]; \
cerr << "}" << '\n'
#define debugMatrix(x, h, w) \
cerr << __LINE__ << ": " << #x << " =\n"; \
for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) { \
cerr << ((hoge ? " {" : "{{")); \
for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga)) \
cerr << ((fuga ? ", " : "")) << x[hoge][fuga]; \
cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \
}
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif
template <int mod>
struct ModInt {
int64_t x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod)) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) { return *this *= p.inverse(); }
ModInt operator-() const { return ModInt() - *this; }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b) t = a / b, swap(a -= t * b, b), swap(u -= t * v, v);
return ModInt(u);
}
ModInt pow(int64_t e) const {
ModInt ret(1);
for (ModInt b = *this; e; e >>= 1, b *= b)
if (e & 1) ret *= b;
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
static int modulo() { return mod; }
int get() const { return x; }
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
using Mint = ModInt<int(1e9 + 7)>;
int H, W;
cin >> H >> W;
string S[H];
for (int i = 0; i < H; i++) cin >> S[i];
Mint ans = 0;
int u[H][W], l[H][W];
int K = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
u[i][j] = 0;
l[i][j] = 0;
if (S[i][j] == '#') continue;
u[i][j] = 1;
l[i][j] = 1;
K++;
if (i > 0) u[i][j] += u[i - 1][j];
if (j > 0) l[i][j] += l[i][j - 1];
}
}
int d[H][W], r[H][W];
for (int i = H - 1; i >= 0; i--) {
for (int j = W - 1; j >= 0; j--) {
d[i][j] = 0;
r[i][j] = 0;
if (S[i][j] == '#') continue;
d[i][j] = 1;
r[i][j] = 1;
if (i + 1 < H) d[i][j] += d[i + 1][j];
if (j + 1 < W) r[i][j] += r[i][j + 1];
}
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (S[i][j] == '#') continue;
int k = u[i][j] - 1 + d[i][j] - 1 + l[i][j] - 1 + r[i][j] - 1 + 1;
ans += (Mint(2).pow(k) - 1) * Mint(2).pow(K - k);
}
}
cout << ans << '\n';
return 0;
}
|
// C++ program to implement Best First Search using priority
// queue
#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, int> pi;
struct node {
int next, c, d;
node(int next, int c, int d) {
this->next = next;
this->c = c;
this->d = d;
}
};
vector<vector<node> > graph;
// Function for adding edges to graph
void addedge(int a, int b, int c, int d)
{
graph[a-1].push_back(node(b-1, c, d));
graph[b-1].push_back(node(a-1, c, d));
}
// Function For Implementing Best First Search
// Gives output path having lowest cost
long long dijkstra(int source, int target, int n)
{
vector<bool> visited(n, false);
// MIN HEAP priority queue
priority_queue<pi, vector<pi>, greater<pi> > pq;
// sorting in pq gets done by first value of pair
pq.push(make_pair(0, source));
while (!pq.empty()) {
int x = pq.top().second;
long long t = pq.top().first;
// Displaying the path having lowest cost
// cout << x << "," << t << ' ';
pq.pop();
if (visited[x])
continue;
visited[x] = true;
if (x == target)
return t;
for (node v: graph[x]) {
if (!visited[v.next]) {
long long t_best = ceil(sqrt((double)v.d))-1;
if (t_best < t)
t_best = t;
long long t_next = t_best + (long long)v.c + floor((double)v.d / (t_best+1));
pq.push(make_pair(t_next, v.next));
}
}
}
return -1;
}
// Driver code to test above methods
int main()
{
int n, m;
cin >> n >> m;
graph.resize(n);
int a, b, c, d;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c >> d;
addedge(a, b, c, d);
}
// Function call
cout << dijkstra(0, n-1, n);
return 0;
}
| #include <cstdio>
#include <algorithm>
#include <ctype.h>
const int bufSize = 1e6;
#define int long long
inline char nc()
{
#ifdef DEBUG
return getchar();
#endif
static char buf[bufSize], *p1 = buf, *p2 = buf;
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2) ? EOF : *p1++;
}
inline void read(char *s)
{
static char c;
for (; !isalpha(c); c = nc());
for (; isalpha(c); c = nc()) *s++ = c;
*s = '\0';
}
template<typename T>
inline T read(T &r)
{
static char c;
static int flag;
flag = 1, r = 0;
for (c = nc(); !isdigit(c); c = nc()) if (c == '-') flag = -1;
for (; isdigit(c); c = nc()) r = r * 10 + c - 48;
return r *= flag;
}
const int maxn = 2e5 + 100;
int n, a[maxn], b[maxn],res;
signed main()
{
read(n);
long long now = 0;
for (int i = 1; i <= n; ++i) read(a[i]),read(b[i]),now -= a[i],a[i] = b[i] + 2 * a[i];
std::sort(a + 1,a + 1 + n);
for(int i = n;i;--i)
{
if (now > 0) { printf("%lld\n", res); return 0; }
now += a[i], ++res;
}
printf("%lld\n", res);
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update> oset;
#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__) << "] "
using ll = long long;
#define ar array
clock_t time_req = clock()*1.0/CLOCKS_PER_SEC;
map<int,pair<int,int>> read(int n , int L){
vector<int> v{0};
for(int i = 1 ; i <= n + 1; i++){
int ai = L+1;
if(i <= n){
cin >> ai;
}
v.push_back(ai-i);
}
map<int,pair<int,int>> interval;
for(int i = 0 ; i < (int)v.size() ; i++){
if( i == 0 || v[i] != v[i-1]){
interval[v[i]].first = i;
}
interval[v[i]].second = i;
}
return interval;
}
void test_case() {
int n , L;
cin >> n >> L;
auto a = read(n,L);
auto b = read(n,L);
debug() << imie(a);
debug() << imie(b);
ll answer = 0;
for(auto pp : b){
int value = pp.first;
if(!a.count(value)){
puts("-1");
return;
}
answer += max(0,a[value].first - b[value].first) + max(0,b[value].second - a[value].second);
}
cout<<answer<<"\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int T = 1;
//cin >> T;
while(T--){
test_case();
}
}
| #include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 mrand(42);
#define ii for(int i=1;i<=n;++i)
#define ji for(int j=1;j<=n;++j)
#define jj for(int j=1;j<=m;++j)
#define ij for(int i=1;i<=m;++i)
#define sz(x) ((ll)x.size())
#define all(x) x.begin(),x.end()
#define al(x) x+1,x+1+n
#define asd cout<<"ok"<<endl;
#define asdd cout<<"okok"<<endl;
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define pr(v) for(auto i:v) cout<<i<<" ";cout<<endl;
#define prt(a, l, r) for(auto i=l;i<=r;++i) cout<<a[i]<<" ";cout<<endl;
#define pc(x) __builtin_popcount(x)
#define pb push_back
#define PS string qqwwee;cin>>qqwwee;
typedef pair<int,int> pii;
int a[100005], b[100005];
ll solve(int l, int r, int L, int R) {
if(l > r) return 0;
int left = 0, right = 0;
for(int i=l;i<=r;++i) {
if(b[i] < a[i]) left = i;
else if(a[i] < b[i]) {
if(!right) right = i;
} else assert(0);
}
if(left && right) {
if(left > right) return -1;
assert(left + 1 == right);
ll tmp1 = solve(l, left, L, a[left]+1);
ll tmp2 = solve(right, r, a[left], R);
if(tmp1 == -1 || tmp2 == -1) return -1;
return tmp1 + tmp2;
}
if(right) {
for(int i=l;i<=r;++i) a[i]=-a[i],b[i]=-b[i];
reverse(a+l,a+r+1);
reverse(b+l,b+r+1);
L=-R;
}
unordered_map<int,int> mp;
ll ret=0;
mp[L-(l-1)]=l-1;
for(int i=l;i<=r;++i) {
if(!mp.count(b[i]-i)) return -1;
ret+=i-mp[b[i]-i];
mp[b[i]-i]=mp[a[i]-i]=i;
}
return ret;
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, l;
cin >> n >> l;
for(int i=1;i<=n;++i) cin >> a[i];
for(int i=1;i<=n;++i) cin >> b[i];
ll ans = 0, lst = 1, lstl = 0;
for(int i=1;i<=n;++i) {
if(a[i] == b[i]) {
ll tmp = solve(lst, i-1, lstl, b[i]);
if(tmp < 0) {
ans = -1;
break;
}
ans += tmp;
lst = i+1;
lstl = a[i];
}
}
if(ans != -1) {
ll tmp = solve(lst, n, lstl, l+1);
if(tmp < 0) ans = -1;
else ans += tmp;
}
cout << ans << endl;
} |
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#include <ctime>
#pragma warning(disable:4996)
#include <iostream>
#include <iomanip>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
template<typename T1, typename T2> bool chmin(T1 &a, T2 b) { if (a <= b)return 0; a = b; return 1; }
template<typename T1, typename T2> bool chmax(T1 &a, T2 b) { if (a >= b)return 0; a = b; return 1; }
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
long double eps = 1e-9;
long double pi = acos(-1);
template< int mod = 1000000007 >
struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt< mod >(t);
return (is);
}
static int get_mod() { return mod; }
};
// const int mod = 1000000007;
const int mod = 998244353;
using mint = ModInt< mod >;
const int MAX = 200005;
mint fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i;
}
finv[MAX - 1] = fac[MAX - 1].inverse();
for (int i = MAX - 2; i >= 1; i--) {
finv[i] = finv[i + 1] * (i + 1);
inv[i + 1] = fac[i] * finv[i + 1];
}
}
// 二項係数計算
mint 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]);
}
mint ans = 0;
ll n, m;
ll mp[200020][20];
mint dfs(ll now, ll cnt) {
if (mp[now][cnt]>=0) {
return mp[now][cnt];
}
mint ret = COM(n - 1, cnt - 1);
for (ll i = 2; i <= m; i++) {
if (now*i <= m)ret += dfs(now*i, cnt + 1);
else break;
}
mp[now][cnt] = ret.x;
return ret;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
cin >> n >> m;
fill(mp[0], mp[200005], -1);
COMinit();
for (int i = 1; i <= m; i++) {
ans += dfs(i, 1);
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const int M = 998244353;
int n,m;
vector<bool> num(210000,true);
vector<int> prime(210000,-1);
vector<ll> fac(210000); //n!(mod M)
vector<ll> ifac(210000); //k!^{M-2} (mod M)
inline ll mpow(ll x, ll n){ //x^n(mod M) ←普通にpow(x,n)では溢れてしまうため,随時mod計算
ll ans = 1;
while(n != 0){
if(n&1) ans = ans*x % M;
x = x*x % M;
n = n >> 1;
}
return ans;
}
inline ll comb(int a, int b){ //aCbをmod計算
if(a == 0 && b == 0)return 1;
if(b == 0)return 1;
if(a < b || a < 0)return 0;
ll tmp = ifac[a-b]* ifac[b] % M;
return tmp * fac[a] % M;
}
void Eratos(){
num[0] = num[1] = false;
int itr = 0;
for(int i = 2;i < 210000;i++){
if(num[i]){
for(int j = 2*i;j <= 210000;j += i){
num[j] = false;
}
prime[itr] = i;
itr++;
}
}
}
ll solve(int n,int m){
ll res = 0;
ll add = 1;
for(int j = 1;j <= m;j++){
add = 1;
int num = j;
for(int i = 0;prime[i] != -1 && prime[i] <= num;i++){
int cnt = 0;
while(num % prime[i] == 0){
cnt++;
num /= prime[i];
}
// cout << j << sp << prime[i] << sp << cnt << sp << endl;
// cout << "comb" << n-2+cnt << sp << cnt << sp << comb(n-2+cnt,cnt) << endl;
add *= comb(n-2+cnt,cnt);
add %= M;
}
//cout << add << endl;
res += add*(m/j);
res %= M;
}
return res;
}
int main(){
cin >> n >> m;
Eratos();
fac[0] = 1;
ifac[0] = 1;
for(int i = 0;i <= 210000;i++){
fac[i+1] = fac[i]*(i+1) % M; // n!(mod M)
ifac[i+1] = ifac[i]*mpow(i+1, M-2) % M; // k!^{M-2} (mod M) ←累乗にmpowを採用
}
cout << solve(n,m) << endl;
} |
// Problem: E - Unique Color
// Contest: AtCoder - AtCoder Beginner Contest 198
// URL: https://atcoder.jp/contests/abc198/tasks/abc198_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a,b,sizeof(a))
#define vi vector<int>
#define vii vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define w(x) int x; cin>>x; while(x--)
#define in(a) for(auto &x: a)cin>>x;
#define out(a) for(auto x: a){cout<<x<<'\n';}
#define inf 1000000000
//#define mm 1000000007 998244353
void print(bool n){if(n)cout<<"YES";else cout<<"NO";}
vector<vi> adj;
vi c;
vi ans;
multiset <int, greater <int>> col;
void dfs(int v,int par){
if(col.empty() || col.find(c[v])==col.end())ans.pb(v+1);
col.insert(c[v]);
for(auto u: adj[v]){
if(u==par)continue;
dfs(u,v);
}
col.erase(col.find(c[v]));
}
signed main(){
fastio
int n;
cin>>n;
c.resize(n);
in(c);
adj.resize(n);
rep(i,0,n-1){
int x,y;
cin>>x>>y;
--x;--y;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(0,-1);
sort(all(ans));
out(ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int mod=998244353;
int n;
int a[200005];
int b[200005];
int vis[200005];
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[a[i]]++;
}
int tmp=1;
for (int i=1;i<=n;i++)
{
if (vis[i]==0 && b[i]>0)
{
vis[i]=i;
int x=a[i];
while (vis[x]==0)
{
vis[x]=i;
x=a[x];
}
if (vis[x]==i) tmp=tmp*2%mod;
}
}
cout<<tmp-1<<endl;
return 0;
}
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long tnum;
#define loop(i, num) for(int i = 0; i < num; i++)
tnum* tnumAlloc(int num);
tnum getNumStdin();
tnum* getNumsStdin(int);
char* getCharsStdin(int maxlength);
string getStringStdin();
void printlnNums(int count, tnum* nums);
void printlnNum(tnum num);
void printString(string str);
#define min(a,b) (a <= b ? a : b)
#define max(a,b) (a >= n ? a : b)
/*void sort(T* start, T* end) (in <algorithm>)
example:
int a[5] = {2, 1, 3, 5, 4};
sort(a, a + 5);
-> a: {1, 2, 3, 4, 5}
*/
int main() {
loop(i, 101) {
char c = (char) getchar();
if (c == '\n' || c == '\r' || c == '.' || c == ' ') {
break;
}
printf("%c", c);
}
return 0;
}
//have to be free()
tnum* tnumAlloc(int num) {
return (tnum*)calloc(num,sizeof(tnum));
}
tnum getNumStdin() {
tnum ret;
cin >> ret;
return ret;
}
//have to be free()
tnum* getNumsStdin(int num) {
tnum* buf;
tnum* tmp;
buf = tnumAlloc(num);
for (int i = 0; i < num; i++) {
cin >> buf[i];
}
return buf;
}
//have to be free()
char* getCharsStdin(int maxlength) {
char* ret = (char*)malloc(maxlength+1);
scanf("%s", ret);
return ret;
}
//have to be free()
string getStringStdin() {
string ret;
cin >> ret;
return ret;
}
void printlnNum(tnum num) {
cout << num << endl;
}
// can be used for debugging
void printlnNums(int count, tnum *nums) {
loop(i, count) {
cout << nums[i];
if (i != count - 1) printf(" ");
}
printf("\n");
}
void printString(string str) {
cout << str;
} | /*
* @Author: Krishnakant
* @Date: 2021-03-19 23:18:20
* @Last Modified by: Krishnakant
* @Last Modified time: 2021-03-20 01:23:11
*/
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
typedef long long ll;
#define _cin \
ios_base::sync_with_stdio(0); \
cin.tie(0);
#define endl "\n"
#define pb push_back
#define rp(i, b) for (ll i = 0; i < b; ++i)
#define rpd(i, a) for (ll i = a; i >= 0; --i)
#define mp make_pair
#define hell 1000000007
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define mll map<ll, ll>
#define sz(x) (ll) x.size()
#define sll set<ll>
#define pll pair<ll, ll>
#define F first
#define S second
#define eb emplace_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#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 deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define precise(x) cout << fixed << setprecision(x)
const ll MAX = 1000004;
const ll INF = 1e18L + 5;
template <class T, class U>
void chmin(T &t, const U &u)
{
if (t > u)
t = u;
}
template <class T, class U>
void chmax(T &t, const U &u)
{
if (t < u)
t = u;
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T)
{
cerr << " " << H;
debug_out(T...);
}
template <typename Arg1>
void pn(Arg1 &&arg1)
{
cout << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void pn(Arg1 &&arg1, Args &&...args)
{
cout << arg1 << "\n";
pn(args...);
}
template <typename Arg1>
void ps(Arg1 &&arg1)
{
cout << arg1 << " ";
}
template <typename Arg1, typename... Args>
void ps(Arg1 &&arg1, Args &&...args)
{
cout << arg1 << " ";
ps(args...);
}
template <typename Arg1>
void read(Arg1 &&arg1)
{
cin >> arg1;
}
template <typename Arg1, typename... Args>
void read(Arg1 &&arg1, Args &&...args)
{
cin >> arg1;
read(args...);
}
void solve()
{
string s;
read(s);
string ans = "";
for (auto i : s)
{
if (i == '.')
break;
ans += i;
}
pn(ans);
}
int main()
{
_cin
ll tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
int main()
{
int n;
cin >> n;
vector<int> a(n);
int m = 0;
for (int i = 0; i < n; i++)
{
cin >> a[i];
m += a[i];
}
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 0; i <= n; i++)
{
dp[i][0] = 1;
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
if (j - a[i - 1] >= 0 && dp[i - 1][j - a[i - 1]] == 1)
{
dp[i][j] = 1;
}
if (j + a[i - 1] <= m && dp[i - 1][j] == 1)
{
dp[i][j + a[i - 1]] = 1;
}
if (dp[i][j] == 0)
{
dp[i][j] = dp[i - 1][j];
}
}
}
int ans = INF;
// for (int i = 0; i <= n; i++)
// {
// for (int j = 0; j <= m; j++)
// {
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
for (int i = 0; i <= m; i++)
{
if (dp[n][i] == 1)
{
ans = min(ans, max(m - i, i));
}
}
cout << ans << endl;
return 0;
}
| #include<iostream>
#include<algorithm>
#include<numeric>
#include<queue>
#include<vector>
using namespace std;
const int BUF = 400005;
int nVal;
int val[BUF];
void read() {
cin >> nVal;
nVal *= 2;
for (int i = 0; i < nVal; ++i) {
cin >> val[i];
}
}
void work() {
long long sum = accumulate(val, val + nVal, 0LL);
priority_queue<int, vector<int>, greater<int> > Q;
for (int i = 0, L = nVal / 2 - 1, R = nVal / 2; i < nVal; i += 2, --L, ++R) {
Q.push(val[L]);
Q.push(val[R]);
sum -= Q.top();
Q.pop();
}
cout << sum << endl;
}
int main() {
read();
work();
return 0;
}
|
#include <bits/stdc++.h>
#define fst first
#define snd second
#define ll long long
#define ld long double
#define pb push_back
#define emp emplace_back
#define pii pair<int, int>
#define usg unsigned
#define sg signed
#define mp make_pair
using namespace std;
void setIO(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
}
const ld PI = 4*atan((ld)1);
const int INF = 1e9+7;
class SegmentTree
{
private:
int n;
vector<ll>a;
vector<ll>t;
private:
void build(int i, int l, int r){
if (l == r){
t[i] = a[l];
return;
}
int m = (l+r)>>1;
build(i<<1, l, m);
build(i<<1|1, m+1, r);
t[i] = (t[i<<1]^t[i<<1|1]);
}
void updatePoint(int i, int l, int r, int pos, int v){
if (l == r){
t[i] ^= v;
return;
}
int m = (l+r)>>1;
if (pos <= m) updatePoint(i<<1, l, m, pos, v);
else updatePoint(i<<1|1, m+1, r, pos, v);
t[i] = (t[i<<1]^t[i<<1|1]);
}
int XORsum(int i, int l, int r, int pl, int pr){
if (l >= pl && r <= pr)
return t[i];
if (pl > r || pr < l)
return 0;
int m = (l+r)>>1;
return (XORsum(i<<1, l, m, pl, pr)^XORsum(i<<1|1, m+1, r, pl, pr));
}
public:
SegmentTree(const vector<ll> &_A){
a = _A; n = a.size();
t.assign(n<<2, 0);
build(1, 0, n-1);
}
void PointUpdate(int pos, int val){
updatePoint(1, 0, n-1, pos-1, val);
}
int XORsum(int l, int r){
return XORsum(1, 0, n-1, l-1, r-1);
}
};
vector<ll>v;
int main(){
setIO();
int n, q;
cin >> n >> q;
v.resize(n);
for (auto &x : v)
cin >> x;
SegmentTree St(v);
for (int i = 0; i < q; i++){
int t, l, r;
cin >> t >> l >> r;
if (t&1) St.PointUpdate(l, r);
else cout << St.XORsum(l, r) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll> ;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define inf 1LL<<60;
ll min(ll a,ll b){return a>=b ? b:a;}
ll max(ll a,ll b){return a>=b ? a:b;}
ll dist[1<<18];
typedef struct{
ll to;
ll c;
ll d;
}town;
vector<town> G[1LL<<18];
ll func(ll t,ll d){
ll rd=sqrt(d);
ll ret=d/(t+1);
for (ll i=-10;i<=10;++i){
if (rd+i-t-1<0) continue;
ll k=rd-t+i-1+d/(rd+i);
ret=min(ret,k);
}
return ret;
}
void dijkstra(ll st){
priority_queue<P,vector<P>,greater<>> q;
dist[st]=0;
q.push(P(0,st));
while (!q.empty()){
ll D=q.top().first;
ll pos=q.top().second;q.pop();
if (dist[pos]!=D) continue;
rep(i,(ll)G[pos].size()){
ll to=G[pos][i].to;
ll cost=G[pos][i].c;
ll di=G[pos][i].d;
ll ct=func(dist[pos],di);
if (dist[to]>dist[pos]+ct+cost){
dist[to]=ct+cost+dist[pos];
q.push(make_pair(dist[to],to));
}
}
}
}
int main(){
ll n,m;
cin >> n >> m;
rep(i,m){
ll a,b,c,d;
cin >> a >> b >> c >>d;
a--;b--;
G[a].push_back(town({b,c,d}));
G[b].push_back(town({a,c,d}));
}
rep(i,n+1) dist[i]=1LL<<60;
dijkstra(0);
if (dist[n-1]==1LL<<60){
cout << -1 << endl;
}
else cout << dist[n-1] << endl;
}
|
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
FAST/**/
ll n;
cin>>n;
ll arr[n], arr1[n];
rep(n)
cin>>arr[i];
rep(n)
cin>>arr1[i];
ll ans = 0;
rep(n)
ans+=(arr[i]*arr1[i]);
if(ans == 0)
cout<<"Yes"<<'\n';
else cout<<"No\n";
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
const int N=500010;
char s[N],t[N];
int sta[N],top;
int n;
void fail(){printf("-1\n");exit(0);}
int main(){
scanf("%d",&n);
scanf("%s",s+1);
scanf("%s",t+1);
int las=0;
for(int i=n;i>=1;i--) if(t[i]=='1') sta[++top]=i;
long long ans=0;
for(int i=1;i<=n;i++)
if(s[i]=='1'){
if(las) ans+=i-las,las=0;
else if(top && sta[top]<=i) ans+=i-sta[top],top--;
else las=i;
}
if(las || top) fail();
printf("%lld\n",ans);
} |
//Author: RingweEH
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define db double
using namespace std;
const int L=1<<20;
char buf[L],*p1,*p2;
#define getchar() (p1==p2?(p2=buf+fread(p1=buf,1,L,stdin),p1==p2?EOF:*p1++):*p1++)
int read()
{
int x=0,w=1; char ch=getchar();
while ( ch>'9' || ch<'0' ) { if ( ch=='-' ) w=-1; ch=getchar(); }
while ( ch<='9' && ch>='0' ) x=x*10+ch-'0',ch=getchar();
return x*w;
}
const int N=510;
int n,a[N],c[N][N],d[N][N];
int main()
{
//freopen( "exam.in","r",stdin );
memset( a,0x7f,sizeof(a) );
n=read();
for ( int i=1; i<=n; i++ )
for ( int j=1; j<=n; j++ )
{
c[i][j]=read(); a[i]=min(a[i],c[i][j]);
d[i][j]=c[i][j]-c[i][1];
}
for ( int i=1; i<=n; i++ )
for ( int j=1; j<=n; j++ )
if ( d[i][j]^d[1][j] ) { puts("No"); return 0; }
puts("Yes");
for ( int i=1; i<=n; i++ ) printf("%d ",a[i] );
puts("");
for ( int i=1; i<=n; i++ ) printf("%d ",c[1][i]-a[1] );
puts("");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
const int beamwidth = 300;
const double TL=1.9;
double start;
int n=50;
int sx, sy;
VVI t(n,VI(n));
VVI p(n,VI(n));
struct status{
int x;
int y;
int score;
int eval;
VI path;
VI used;
bool operator<(const status &another) const {return eval < another.eval;};
};
vector<status> search(const status &base){
vector<status> res;
REP(i,4){
int tox=base.x+dx[i];
int toy=base.y+dy[i];
if(tox<0||tox>=n||toy<0||toy>=n)
continue;
if(base.used[t[tox][toy]])
continue;
int anotp=p[tox][toy];
REP(j,4){
int toox=tox+dx[j];
int tooy=toy+dy[j];
if(toox<0||toox>=n||tooy<0||tooy>=n)
continue;
if(t[tox][toy]==t[toox][tooy]){
anotp=p[toox][tooy];
break;
}
}
int newscore=base.score+p[tox][toy]-max(0,anotp-p[tox][toy]);
int dlx=min(tox,49-tox), dly=min(toy,49-toy);
int neweval=newscore-(3*(dlx+dly)+min(dlx,dly))*100;
res.push_back((status){tox,toy,newscore,neweval,base.path,base.used});
res[res.size()-1].used[t[tox][toy]]=1;
res[res.size()-1].path.push_back(i);
}
return res;
}
int main(){
start=clock();
cin >> sx >> sy;
int maxt=0;
REP(i,n)REP(j,n){
cin >> t[i][j];
maxt=max(maxt,t[i][j]);
}
REP(i,n)REP(j,n) cin >> p[i][j];
priority_queue<status> que, backup;
VI initused(maxt+1,0); initused[t[sx][sy]]=1;
que.push({sx,sy,p[sx][sy],0,VI(0),initused});
int maxscore=0; VI ans;
int cnt=0;
while(1){
priority_queue<status> nxtque;
VVI mxs(n,VI(n,0));
while(nxtque.size()<beamwidth){
if(que.empty())
break;
if(que.top().score>maxscore){
maxscore=que.top().score;
ans=que.top().path;
}
vector<status> res=search(que.top());
for(auto j:res){
if(mxs[j.x][j.y]<10){
mxs[j.x][j.y]++;
nxtque.push(j);
}
}
que.pop();
}
if(cnt%10==0&&que.size()>=5){
REP(j,5){
vector<status> res=search(que.top());
for(auto j:res){
backup.push(j);
}
que.pop();
}
}
que=nxtque;
double tim=(double)(clock()-start)/CLOCKS_PER_SEC/TL;
if(tim>=1.0)
break;
if(que.empty())
que=backup;
cnt++;
}
string dir="DRUL";
for(auto i:ans)
cout << dir[i];
cout << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int X, Y, Z;
cin >> X >> Y >> Z;
if(Z * Y % X == 0) cout << Z * Y / X - 1 << endl;
else cout << Z * Y / X << endl;
} | #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ALL(obj) obj.begin(),obj.end()
#define SORT(obj) sort(obj.begin(),obj.end())
#define pb(obj) push_back(obj)
#define rep(i,n) for(int i=0; i<n; i++)
typedef long long ll;
ll const MOD=1e9+7;
const ll INF = 9000000000000000000;
vector<bool> seen;//グラフ用の配列
long long pow(long long x, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret = ret * x % MOD; // n の最下位bitが 1 ならば x^(2^i) をかける
x = x * x % MOD;
n >>= 1; // n を1bit 左にずらす
}
return ret;
}
int ctoi(const char c){
switch(c){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default : return -1;
}
}
int kakuketa(int n){
int ans = 0;
while(n>0){
ans += n%10;
n /= 10;
}
return ans;
}
void dfs2(int y,int x,vector<vector<bool>> &flags,Graph &G){//二次元dfs
if(G[y][x] == 0) return;
if(flags[y][x] == true) return;
else{
flags[y][x] = true;
}
dfs2(y+1,x,flags,G);
dfs2(y+1,x+1,flags,G);
dfs2(y+1,x-1,flags,G);
dfs2(y,x+1,flags,G);
dfs2(y,x-1,flags,G);
dfs2(y-1,x+1,flags,G);
dfs2(y-1,x,flags,G);
dfs2(y-1,x-1,flags,G);
}
int main(){
int x,y,z;
cin>>x>>y>>z;
double a = (double)y*z/x;
//cout<<a<<endl;
if(ceil(a) == floor(a)){
cout<<(int)a-1<<endl;
return 0;
}
else{
cout<<floor(a)<<endl;
return 0;
}
} |
#include<bits/stdc++.h>
using namespace std;
int x,y,z;
int b;
int main()
{
cin>>x>>y>>z;
if((y/x)*x==y)cout<<y/x*z-1;
else
{
b=y/x*z;
while(b*x<y*z)b++;
cout<<b-1;
}
return 0;
} | #pragma region my_template
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
#define range(i, l, r) for(int i = (int)(l); i < (int)(r); i++)
#define rrange(i, l, r) for(int i = (int)(r)-1; i >= (int)(l); i--)
#define rep(i, n) range(i, 0, n)
#define rrep(i, n) rrange(i, 0, n)
#define len(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define sum(a) accumulate(all(a), 0)
#define uni(a) (a).erase(unique(all(a)), (a).end());
#define append push_back
#define mp make_pair
#define elif else if
constexpr int MOD = 1e9+7;
//constexpr int MOD = 998244353;
constexpr int INF = 1e9;
constexpr ll LINF = 1e18;
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
#pragma endregion
constexpr int K = 1000;
void solve() {
rep(i, K) {
int sy, sx, ty, tx;
cin >> sy >> sx >> ty >> tx;
string ans;
if (sy > ty) {
rep(_, sy-ty) ans.append('U');
} else {
rep(_, ty-sy) ans.append('D');
}
if (sx > tx) {
rep(_, sx-tx) ans.append('L');
} else {
rep(_, tx-sx) ans.append('R');
}
cout << ans << endl;
int t; cin >> t;
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
//cout << fixed << setprecision(15);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int N,M; cin >> N >> M;
if(N == 1 && M == 0){
cout << 1 << " " << 2 << endl;
return 0;
}
if(M < 0 || M == N || M == N-1){
cout << -1 << endl;
return 0;
}
rep(i,N-(M+1)){
cout << i+1 << " " << i+1 + TEN(8) << endl;
}
rep(i,M+1){
cout << TEN(7) + i*2 << " " << TEN(7) + i*2+1 << endl;
}
}
| #include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int64_t> A(N), B(N);
vector<int> P(N), inv(N);
for (auto &a : A)
cin >> a;
for (auto &b : B)
cin >> b;
for (auto &p : P) {
cin >> p;
p--;
}
for (int i = 0; i < N; i++)
inv[P[i]] = i;
for (int i = 0; i < N; i++)
if (i != P[i] && A[i] <= B[P[i]]) {
cout << -1 << '\n';
return 0;
}
vector<int> B_order(N);
iota(B_order.begin(), B_order.end(), 0);
sort(B_order.begin(), B_order.end(), [&](int x, int y) {
return B[x] < B[y];
});
vector<array<int, 2>> ops;
for (int b : B_order) {
while (inv[b] != b) {
int x = inv[b];
int i = inv[x];
ops.push_back({i, x});
swap(inv[x], inv[b]);
swap(P[i], P[x]);
}
}
cout << ops.size() << '\n';
for (auto &op : ops)
cout << op[0] + 1 << ' ' << op[1] + 1 << '\n';
}
|
// C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using vi = vector<int>;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
LL a[22];
int main(){
// 1. 入力情報.
int N;
scanf("%d", &N);
rep(i, N) scanf("%lld", &a[i]);
// 2. 2 の 冪乗 を 保存.
vi cPow2;
int count = 0;
cPow2.pb(1);
while(count < N){
// 最大値を2倍.
int p = 2 * cPow2.back();
// 保存.
cPow2.pb(p);
// インクリメント.
count++;
}
// 3. 区間を生成し, 最小値を探索.
LL ans = 202020202020202020;
rep(i, 1 << N){
// 3-1. 区間を生成.
int bef = -1, cur;
vi v;
rep(j, N){
// 今回分更新.
cur = (cPow2[N - 1 - j] & i) ? 1 : 0;
// 前回と異なる場合は, 区間開始地点と見て保存.
if(bef != cur) v.pb(j);
// 前回分更新.
bef = cur;
}
// 3-2. 番兵.
v.pb(N);
// 3-3. XOR生成.
LL cXOR = 0;
rep(j, v.size() - 1){
// 区間開始地点, 区間終了地点.
LL cOR = 0;
int s = v[j];
int e = v[j + 1];
// 区間内の数のビット単位OR を計算.
repx(k, s, e) cOR |= a[k];
// 全ての値のビット単位XOR を計算.
cXOR ^= cOR;
}
// 3-4. 最小値更新.
ans = min(ans, cXOR);
}
// 4. 出力.
printf("%lld\n", ans);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(){
ll n;cin >> n;
cout << (1<<n)-1 <<endl;
repl(bit,1,1<<n){
rep(cit,1<<n){
if(__builtin_popcount(bit&cit)%2){
cout << "A";
}
else cout << "B";
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define init(a,i) for(int k=0;k<(i);k++)(a)[k]=0
#define in(a,i) for(int k=0;k<(i);k++)cin>>(a)[k]
#define all(a) (a).begin(),(a).end()
#define inf 2147483647
#define range(x,a,b) (a)<=x&&x<=(b)
#define mod 323
int top(int *unionfind,int a){
int c=unionfind[a];
if(c==a)return a;
unionfind[a]=top(unionfind,c);
return unionfind[a];
}
int bind(int *unionfind,int a,int b){
int c=unionfind[a];
if(c==a){
unionfind[a]=b;
return b;
}
unionfind[a]=bind(unionfind,unionfind[a],b);
return unionfind[a];
}
int add(int *unionfind,int a,int number,ll *num){
int c=unionfind[a];
if(c==a){
num[a]+=number;
return a;
}
unionfind[a]=add(unionfind,unionfind[a],number,num);
return unionfind[a];
}
int main(){
int n,m;
cin>>n>>m;
int a[n],b[n];
in(a,n);in(b,n);
int ab[n];
rep(i,n)ab[i]=a[i]-b[i];
if(m==0){
int flag=1;
rep(i,n){
if(ab[i]!=0)flag=0;
}
if(flag==1) cout<<"Yes";
else cout<<"No";
return 0;
}
int c[m],d[m],tmp;
rep(i,m){
cin>>tmp;
c[i]=tmp-1;
cin>>tmp;
d[i]=tmp-1;
}
int unionfind[n];
rep(i,n)unionfind[i]=i;
rep(i,m) bind(unionfind,c[i],top(unionfind,d[i]));
ll num[n];
rep(i,n)num[i]=0;
rep(i,n){
add(unionfind,i,ab[i],num);
}
int flag=1;
rep(i,n)if(num[i]!=0)flag=0;
if(flag==1) cout<<"Yes";
else cout<<"No";
return 0;
} | /* union_find_tree_size.cpp
素集合系を union-find tree で管理するライブラリ
併合時の工夫:union by size
verified: AtCoder ABC157 D - Friend Suggestions
https://atcoder.jp/contests/abc157/tasks/abc157_d
*/
#include <bits/stdc++.h>
using namespace std;
/* UnionFind:素集合系管理の構造体(union by size)
isSame(x, y): x と y が同じ集合にいるか。 計算量はならし O(α(n))
unite(x, y): x と y を同じ集合にする。計算量はならし O(α(n))
treeSize(x): x を含む集合の要素数。
*/
struct UnionFind {
vector<int> size, parents;
UnionFind() {}
UnionFind(int n) { // make n trees.
size.resize(n, 0);
parents.resize(n, 0);
for (int i = 0; i < n; i++) {
makeTree(i);
}
}
void makeTree(int x) {
parents[x] = x; // the parent of x is x
size[x] = 1;
}
bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
bool unite(int x, int y) {
x = findRoot(x);
y = findRoot(y);
if (x == y) return false;
if (size[x] > size[y]) {
parents[y] = x;
size[x] += size[y];
} else {
parents[x] = y;
size[y] += size[x];
}
return true;
}
int findRoot(int x) {
if (x != parents[x]) {
parents[x] = findRoot(parents[x]);
}
return parents[x];
}
int treeSize(int x) { return size[findRoot(x)]; }
};
int main() {
int N, M;
cin >> N >> M;
UnionFind uf(N); // Union-Find Tree で考える
vector<long long> a(N), b(N);
for (int i = 0; i < N; i++) {
cin >> a[i];
}
for (int i = 0; i < N; i++) {
cin >> b[i];
}
for (int i = 0; i < M; i++) {
int c, d;
cin >> c >> d;
c--, d--;
uf.unite(c, d);
}
map<int, long long> m;
for (int i = 0; i < N; i++) {
m[uf.findRoot(i)] += (a[i] - b[i]);
}
for (auto p : m) {
if (p.second != 0) {
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
ll a,b,c;
cin >> a>> b >> c;
if (a*a + b*b < c*c)cout << "Yes";
else cout << "No";
} | //https://atcoder.jp/contests/abc190/tasks/abc190_b
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef long double ld;
const ll mod = 1e9 + 7;
int main ()
{
ios_base::sync_with_stdio (0); cin.tie (0); cout.tie (0);
int n, s, d;
cin >> n >> s >> d;
int chk = 0;
while (n--)
{
int x, y;
cin >> x >> y;
if (x < s && y > d) chk = 1;
}
cout << (chk ? "Yes" : "No");
}
|
#include <bits/stdc++.h>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
class rep {
struct Iter {
usize itr;
constexpr Iter(const usize pos) noexcept: itr(pos) { }
constexpr void operator ++ () noexcept { ++itr; }
constexpr bool operator != (const Iter& other) const noexcept { return itr != other.itr; }
constexpr usize operator * () const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr rep(const usize first, const usize last) noexcept: first(first), last(std::max(first, last)) { }
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
template <class T>
using Vec = std::vector<T>;
using Point = std::complex<double>;
constexpr double PI = std::acos(-1.0);
void D_main() {
usize N;
std::cin >> N;
Point p, q;
double px, py, qx, qy;
std::cin >> px >> py >> qx >> qy;
p.real(px);
p.imag(py);
q.real(qx);
q.imag(qy);
const auto c = (p + q) / 2.0;
const auto v = (p - c) * std::polar(1.0, 2.0 * PI / N);
const auto ans = c + v;
std::cout << ans.real() << ' ' << ans.imag() << '\n';
return;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(10);
D_main();
return 0;
}
| #include <iostream>
using namespace std;
const int N = 35;
int prims[N], idx;
bool st[N];
int main() {
for (int i = 2; i <= 30; i ++) {
if (!st[i]) prims[idx ++] = i;
for (int j = 0; prims[j] <= 30 / i; j ++) {
st[prims[j] * i] = true;
if (i % prims[j] == 0) break;
}
}
// for (int i = 0; i < idx; i ++) cout << prims[i] << ' ';
// cout << endl;
// cout << idx << endl;
long long t = 1;
for (int i = 0; i < idx; i ++) t *= prims[i];
t *= 8;
t *= 9;
t *= 5;
cout << t + 1;
return 0;
}
|
#include <bits/stdc++.h>
#include <math.h>
#define REP(i,a,b) for (int i=a; i<b; i++)
#define ALL(v) (v).begin(), (v).end()
#define FORE(i,a) for(auto &i:a)
using namespace std;
typedef long long ll;
const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (a>b) { a=b; return 1; } return 0; }
//#pragma GCC optimize ("-O3")
void _main(); int main() {cin.tie(0); ios::sync_with_stdio(false); _main(); }
int findSumOfDigits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
void _main() {
ll N;
cin >> N;
ll extra;
ll ans = 0;
ll base = 1;
ll ct = 0;
while (N >= base*1000) {
ans += (base*1000 - base)*ct;
ct++;
base *= 1000;
}
extra = N-base+1;
ans += extra*ct;
cout << ans << endl;
}
| // Problem: B - Palindrome with leading zeros
// Contest: AtCoder - AtCoder Beginner Contest 198
// URL: https://atcoder.jp/contests/abc198/tasks/abc198_b
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define it insert
#define pob pop_back
#define pub push_back
#define emb emplace_back
#define all(v) (v).begin(), (v).end()
#define mkp(a, b) make_pair(a, b)
using LL = long long;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLL = pair<LL, LL>;
const double EPS = 1e-6;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 7;
int main() {
int x;
cin >> x;
int t = x;
int ans = 0;
while (t) {
ans = ans * 10 + t % 10;
t /= 10;
}
while (x && x % 10 == 0) x /= 10;
if (x == ans)
cout << "Yes";
else
cout << "No";
return 0;
} |
#include<cstdio>
#define fo(x,a,b) for(int x=(a),_e=(b);x<=_e;x++)
#define fd(x,a,b) for(int x=(a),_e=(b);x>=_e;x--)
typedef long long ll;
const int N=5005;
int a[N][4],pd[N],n,ans;
char c[N];
int main(){
scanf("%d",&n);scanf("%s",c+1);
pd['A']=0, pd['T']=1, pd['C']=2, pd['G']=3;
fo(i,1,n) {
fo(j,0,3) a[i][j]=a[i-1][j];
a[i][pd[c[i]]]++;
}
fo(i,1,n) {
fo(j,i+1,n) {
if(a[j][0]-a[i-1][0]==a[j][1]-a[i-1][1] && a[j][2]-a[i-1][2]==a[j][3]-a[i-1][3])
++ans;
}
}
printf("%d",ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll (i) = 0 ; (i) < (ll)(n) ; ++(i))
#define REPN(i, m, n) for (ll (i) = m ; (i) < (ll)(n) ; ++(i))
#define REP_REV(i, n) for (ll (i) = (ll)(n) - 1 ; (i) >= 0 ; --(i))
#define REPN_REV(i, m, n) for (ll (i) = (ll)(n) - 1 ; (i) >= m ; --(i))
#define INF 2e9
#define INF_LL 1LL<<60
#define ll long long
#define M_PI 3.14159265358979323846
typedef pair<int, int> P;
int main(){
string s[3];
int num = 0;
vector<int> kaisu(26, 0);
vector<int> jun(26, 0);
REP(i, 3) {
cin >> s[i];
REP(j, s[i].size()) kaisu[s[i][j] - 'a']++;
}
REP(i, kaisu.size()) {
if(0 < kaisu[i]) {
jun[i] = num;
num++;
}
if(num > 10) {
cout << "UNSOLVABLE" << endl;
return 0;
}
}
vector<int> v(10);
REP(i, v.size()) v[i] = i;
do{
bool b = false;
vector<string> ss(3);
REP(i, ss.size()) ss[i] = s[i];
REP(i, 3) {
REP(j, s[i].size()) {
int n = v[jun[s[i][j] - 'a']];
ss[i][j] = '0' + n;
}
if(ss[i][0] == '0') b = true;
}
if(b) continue;
vector<ll> ans(3);
REP(i, ans.size()) ans[i] = stol(ss[i]);
if(ans[0] + ans[1] == ans[2]) {
cout << ans[0] << endl;
cout << ans[1] << endl;
cout << ans[2] << endl;
return 0;
}
} while(next_permutation(v.begin(), v.end()));
cout << "UNSOLVABLE" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fcout cout << fixed << setprecision(18)
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll N; cin >> N;
double M; cin >> M;
ll K; cin >> K;
vector<ll> A(N + 10, 0);
for (ll i = 0; i < K; i++) {
ll X; cin >> X;
A[X] = 1;
}
ll check = 0;
for (ll i = 0; i < N + 1; i++) {
if (A[i] == 1) check++;
else check = 0;
if (check >= M) {
cout << -1 << '\n';
return 0;
}
}
double l = 0, r = 1e12;
while (l + 0.00001 < r) {
double m = (l + r) / 2.0;
vector<double> E(N + M + 10, 0);
double sum = 0;
for (ll i = N - 1; i >= 0; i--) {
sum += (E[i + 1] - E[i + M + 1]);
if (A[i] == 1) {
E[i] = m;
} else {
E[i] = 1.0 + sum / M;
}
}
if (E[0] > m) {
l = m;
} else {
r = m;
}
}
fcout << l << '\n';
return 0;
} | #include<bits/stdc++.h>
using namespace std;
struct vals{
double val1;
double val2;
vals():val1(0.f),val2(0.f){};
vals operator +(const vals & b){
vals tmp;
tmp.val1 = b.val1 + val1;
tmp.val2 = b.val2 + val2;
return tmp;
}
vals operator -(const vals & b){
vals tmp;
tmp.val1 = val1 - b.val1;
tmp.val2 = val2 - b.val2;
return tmp;
}
void operator =(const vals & b){
this -> val1 = b.val1;
this -> val2 = b.val2;
}
};
int main(){
cout << setprecision(15);
cout << fixed;
int n,m,k;
cin >> n >> m >> k;
set<int>badpos;
for(int i = 0; i < k; ++i){
int tmp;
cin >> tmp;
badpos.insert(tmp);
}
vector<vals>dp(n+m+1);
vals orig;
for(int i = n; i >= 0; --i){
if(badpos.count(i)){
dp[i].val2 = 1;
} else {
dp[i] = orig;
dp[i].val1 /= double(m);
dp[i].val2 /= double(m);
if(i != n)dp[i].val1 += 1.f;
}
orig = orig - dp[i + m];
orig = orig + dp[i];
//cerr << dp[i].val1 << " , "<< dp[i].val2 << " is the expected number of moves \n";
}
long double den = 1.0 - dp[0].val2;
if(fabs(den) < 1e-9){
cout <<"-1\n";
} else {
cout << dp[0].val1/den << "\n";
}
return 0;
} |
#include<bits/stdc++.h>
#define pb push_back
#define pf push_front
using namespace std;
typedef long long int lli;
lli n,k,m,t,a,b,l,r,h,u,x,y,temp1,temp2,temp,q,d,mod=1e9+7,w,p,c,z;
lli gcd(lli a,lli b)
{
if(b == 0)return a;
return gcd(b, a%b);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
vector<lli>v(n+2);
lli mn = 1e9+7;
for(int i=0;i<n;i++)
{
cin>>v[i];
mn = min(mn,v[i]);
}
unordered_map<lli,lli>mp;
for(auto x:v)
{
lli sq = sqrt(x);
for(int i=1;i<=sq;i++)
{
if( x%i == 0 )
{
if(mp.find(i) == mp.end())
{
mp[i] = x;
}
else
{
mp[i] = gcd(mp[i], x);
}
if(mp.find(x/i) == mp.end())
{
mp[x/i] = x;
}
else
{
mp[x/i] = gcd(mp[x/i], x);
}
}
}
}
lli ans = 0;
for(auto x:mp)
{
if(x.first <= mn)
{
if(x.second == x.first) ans++;
}
}
cout<<ans<<endl;
}
| #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define reps(i,s,n) for(int i=s;i<n;++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int M = 1e5;
int main()
{
int n;
cin >> n;
vector<int> t(n);
rep(i, n) cin >> t[i];
int sum = 0;
rep(i, n) sum += t[i];
vector<vector<int>> dp(n+5, vector<int>(2*M,-1));
dp[0][0] = 1;
rep(i, n)
{
rep(j, M + 2)
{
dp[i + 1][j] = max(dp[i][j],dp[i+1][j]);
dp[i + 1][j + t[i]] = max(dp[i][j], dp[i + 1][j+t[i]]);
}
}
int ans = M;
rep(i, M)
{
if (dp[n][i] == 1)
{
int a = i;
int b = sum - i;
ans = min(ans, max(a, b));
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int n, s, d;
cin >> n >> s >> d;
string ans = "No";
for(int i = 0; i < n; i++){
int x, y;
cin >> x >> y;
if(x < s && y > d){
ans = "Yes";
break;
}
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
//kaizen_philosopher
//Al Waris
/*-----------------------------------------------------------------------*/
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define ll long long int
//loops
#define lp0(i,a) for(i=0;i<a;i++)
#define lp1(i,a) for(i=1;i<=a;i++)
//file input output(T.N.T.I)
#define f_input freopen("input.txt","r",stdin)
#define f_output freopen("output.txt","w",stdout)
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vc vector<char>
#define si stack<int>
#define qi queue<int>
#define sq(a) ((a)*(a))
#define sf scanf
#define pf printf
#define endl "\n"
const double pi=acos(-1);
/*---------------------------------------------------------------------*/
void solve(){
ll a,x,y,i,j;
cin>>a>>x>>y;
ll ar[a][2];
lp0(i,a){
cin>>ar[i][0];
cin>>ar[i][1];
}
bool flag=false;
lp0(j,a){
if(ar[j][0]<x && ar[j][1]>y) flag=true;
}
if(flag==true)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
int main(){
fastio
int t=1;
while(t--)solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
main() {
int N; scanf("%d", &N);
int A[N + 5], B[N + 5];
for (int k = 0; k < 2; k++) {
for (int i = 0; i < N; i++) {
if (k == 1) scanf("%d", &B[i]);
else scanf("%d", &A[i]);
}
}
int ans = 0;
for (int i = 0; i < N; i++) ans += A[i] * B[i];
if (ans == 0) printf("Yes");
else printf("No");
}
| #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <deque>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cassert>
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;
typedef long long ll;
#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=b-1LL;i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define MOD 1000000007
ll d[100100];
int main(){
clr(d,0);
ll n;
cin>>n;
vector<ll> v;
ll sum = 0;
rep(i,0,n){
ll a;
cin>>a;
v.pb(a);
}
sort(all(v));
rep(i,0,v.sz){
d[i] += v[i];
d[i+1] += d[i];
}
double mn = 1000000000000000000.;
rep(i,0,v.sz){
double e = 1.*v[i]/2+(1.*d[n]-1.*d[i]-1.*v[i]*(n-1-i))/n;
mn = min(mn,e);
}
printf("%20.20f\n", mn);
} |
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <bitset>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define MOD 1000000007
#define rep(i, n) for( ll int i = 0; i < (n); i++ )
int main(){
ll int n;
cin >> n;
vector<ll int> a(n);
rep(i, n) cin >> a.at(i);
map<ll int, ll int> mp;
rep(i, n){
if( mp.count( a.at(i) ) ){
mp[ a.at(i) ] += 1;
}
else{
mp[ a.at(i) ] = 1;
}
}
ll int ans = n * (n-1) / 2;
for( auto k : mp ){
if( k.second >= 2 ){
ans -= k.second * (k.second-1) / 2;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
struct io_setup{
io_setup(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
}
} io_setup;
template<int mod>
struct Mod_Int{
int x;
Mod_Int() : x(0) {}
Mod_Int(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
Mod_Int &operator += (const Mod_Int &p){
if((x += p.x) >= mod) x -= mod;
return *this;
}
Mod_Int &operator -= (const Mod_Int &p){
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
Mod_Int &operator *= (const Mod_Int &p){
x = (int) (1LL * x * p.x % mod);
return *this;
}
Mod_Int &operator /= (const Mod_Int &p){
*this *= p.inverse();
return *this;
}
Mod_Int &operator ++ () {return *this += Mod_Int(1);}
Mod_Int operator ++ (int){
Mod_Int tmp = *this;
++*this;
return tmp;
}
Mod_Int &operator -- () {return *this -= Mod_Int(1);}
Mod_Int operator -- (int){
Mod_Int tmp = *this;
--*this;
return tmp;
}
Mod_Int operator - () const {return Mod_Int(-x);}
Mod_Int operator + (const Mod_Int &p) const {return Mod_Int(*this) += p;}
Mod_Int operator - (const Mod_Int &p) const {return Mod_Int(*this) -= p;}
Mod_Int operator * (const Mod_Int &p) const {return Mod_Int(*this) *= p;}
Mod_Int operator / (const Mod_Int &p) const {return Mod_Int(*this) /= p;}
bool operator == (const Mod_Int &p) const {return x == p.x;}
bool operator != (const Mod_Int &p) const {return x != p.x;}
Mod_Int inverse() const{
assert(*this != Mod_Int(0));
return pow(mod-2);
}
Mod_Int pow(long long k) const{
Mod_Int now = *this, ret = 1;
for(; k > 0; k >>= 1, now *= now){
if(k&1) ret *= now;
}
return ret;
}
friend ostream &operator << (ostream &os, const Mod_Int &p){
return os << p.x;
}
friend istream &operator >> (istream &is, Mod_Int &p){
long long a;
is >> a;
p = Mod_Int<mod>(a);
return is;
}
};
using mint = Mod_Int<MOD>;
int main(){
int N, P; cin >> N >> P;
mint ans = P-1;
ans *= mint(P-2).pow(N-1);
cout << ans << '\n';
} |
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll =long long;
using vi = vector<ll>;
using vc = vector<char>;
using vs = vector<string>;
using P = pair<ll , ll>;
using vp = vector<P>;
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define rep2(i, s, n) for (int i = (s); i < n; i++)
#define sort(A) sort(A.begin(),A.end());
#define reverse(A) reverse(A.begin(),A.end());
#define vecint2(A, n, m) vector<vector<long long int>> A(n, vector<long long int>(m));
#define vecchar2(A, n, m) vector<vector<char>> A(n, vector<char>(m));
#define k(s) cout << fixed << setprecision(s);
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
const long double pi=3.14159265358979323846;
int main() {
int N;
cin>>N;
vi X(N),Y(N);
rep(i,N) cin>>X[i]>>Y[i];
int M;
cin>>M;
vi sita(M+1);
vi xc(M+1),xd(M+1);
vi yc(M+1),yd(M+1);
vi seijou(M+1,0);
seijou[0]=1;
rep(i,M){
int t;
cin>>t;
if(t==1){
xd[i+1]=yd[i];
yd[i+1]=-xd[i];
if(seijou[i]==1){
seijou[i+1]=0;
xc[i+1]=xc[i]+1;
yc[i+1]=yc[i];
}else if(seijou[i]==0){
seijou[i+1]=1;
xc[i+1]=xc[i];
yc[i+1]=yc[i]+1;
}
}else if(t==2){
sita[i+1]=sita[i]+1;
xc[i+1]=xc[i];
xd[i+1]=-yd[i];
yc[i+1]=yc[i];
yd[i+1]=xd[i];
if(seijou[i]==1){
seijou[i+1]=0;
xc[i+1]=xc[i];
yc[i+1]=yc[i]+1;
}else if(seijou[i]==0){
seijou[i+1]=1;
xc[i+1]=xc[i]+1;
yc[i+1]=yc[i];
}
}else if(t==3){
ll p;
cin>>p;
if(seijou[i]==1){
xc[i+1]=xc[i]+1;
yc[i+1]=yc[i];
}else{
xc[i+1]=xc[i];
yc[i+1]=yc[i]+1;
}
xd[i+1]=2*p-xd[i];
yd[i+1]=yd[i];
seijou[i+1]=seijou[i];
}else{
ll p;
cin>>p;
xd[i+1]=xd[i];
if(seijou[i]==1){
yc[i+1]=yc[i]+1;
xc[i+1]=xc[i];
}else{
xc[i+1]=xc[i]+1;
yc[i+1]=yc[i];
}
yd[i+1]=2*p-yd[i];
seijou[i+1]=seijou[i];
}
}
int Q;
cin>>Q;
rep(i,Q){
int A,B;
cin>>A>>B;
ll tx=X[B-1],ty=Y[B-1];
ll atx,aty;
if(seijou[A]==1){
if(xc[A]%2==0) atx=xd[A]+tx;
else atx=xd[A]-tx;
if(yc[A]%2==0) aty=yd[A]+ty;
else aty=yd[A]-ty;
}else{
if(yc[A]%2==0) atx=xd[A]+ty;
else atx=xd[A]-ty;
if(xc[A]%2==0) aty=yd[A]+tx;
else aty=yd[A]-tx;
}
cout<<atx<<" "<<aty<<endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
template <typename T>
inline void read(T &x)
{
T data = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
{
data = (data << 3) + (data << 1) + ch - '0';
ch = getchar();
}
x = f * data;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args)
{
read(t);
read(args...);
}
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5 + 9;
const ll mod = 998244353;
struct point
{
ll x, y;
point() {}
point(int a, int b) : x(a), y(b) {}
};
point p[maxn];
ll mat[maxn][4][4];
signed main()
{
int n, m, q;
read(n);
for (int i = 1; i <= n; ++i)
read(p[i].x, p[i].y);
read(m);
mat[0][1][1] = mat[0][2][2] = mat[0][3][3] = 1;
for (int i = 1, op, d; i <= m; ++i)
{
read(op);
if (op <= 2)
{
if (op == 1)
{
for (int j = 1; j <= 3; ++j)
{
mat[i][1][j] = mat[i - 1][2][j];
mat[i][2][j] = -mat[i - 1][1][j];
mat[i][3][j] = mat[i - 1][3][j];
}
}
else
{
for (int j = 1; j <= 3; ++j)
{
mat[i][1][j] = -mat[i - 1][2][j];
mat[i][2][j] = mat[i - 1][1][j];
mat[i][3][j] = mat[i - 1][3][j];
}
}
}
else
{
read(d);
if (op == 3)
{
for (int j = 1; j <= 3; ++j)
{
mat[i][1][j] = -mat[i - 1][1][j] + 2LL * d * mat[i - 1][3][j];
mat[i][2][j] = mat[i - 1][2][j];
mat[i][3][j] = mat[i - 1][3][j];
}
}
else
{
for (int j = 1; j <= 3; ++j)
{
mat[i][2][j] = -mat[i - 1][2][j] + 2LL * d * mat[i - 1][3][j];
mat[i][1][j] = mat[i - 1][1][j];
mat[i][3][j] = mat[i - 1][3][j];
}
}
}
}
read(q);
while (q--)
{
int a, b;
read(a, b);
ll x = p[b].x * mat[a][1][1] + p[b].y * mat[a][1][2] + mat[a][1][3];
ll y = p[b].x * mat[a][2][1] + p[b].y * mat[a][2][2] + mat[a][2][3];
printf("%lld %lld\n", x, y);
}
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
template <class T>
using idx_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ull unsigned long long
#define arr array
#define vt vector
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define all(v) (v).begin(), (v).end()
#define pi acosl(-1)
//ofstream out("debug.txt");
const int MxN = 1e5, md = 1e9 + 7;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
bool sat = 0;
int n;
cin >> n;
set<string> s;
string t, r;
for (int i = 0; i < n; ++i)
{
cin >> t;
if (t[0] == '!')
{
if (s.count(t.substr(1)))
{
sat = 1;
r = t.substr(1);
}
}
else
{
if (s.count('!'+t))
{
sat = 1;
r = t;
}
}
s.insert(t);
}
if (sat)
cout << r << '\n';
else
cout << "satisfiable\n";
}
| #include <cstdio>
const int M = 1005;
int read()
{
int x=0,f=1;char c;
while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;}
while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();}
return x*f;
}
char s[M];int ans;
signed main()
{
scanf("%s",s+1);
for(int i=1;i<=12;i++)
if(s[i]=='Z' && s[i+1]=='O' && s[i+2]=='N' && s[i+3]=='e')
ans++;
printf("%d\n",ans);
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int ll
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
template<typename T, typename U>
ostream& operator<<(ostream& o, const pair<T,U>& p) {
return o << "(" << p.first << "," << p.second << ")";
}
template<typename T, typename U>
ostream& operator<<(ostream& o, const map<T,U>& m) {
o << "{";
for (const pair<T,U>& p : m)
o << p.first << ": " << p.second << ",";
return o << "}";
}
template<typename T>
ostream& operator<<(ostream& o, const set<T>& s) {
o << "{";
for (const T& e : s)
o << s << ",";
return o << "}";
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
o << "[";
for (const T& x : v)
o << x << ",";
return o << "]";
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T>>& v) {
o << "[";
for (const vector<T>& x : v)
o << x << ",\n";
return o << "]";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
string s;
cin >> n >> s;
if (s[0] != s[n-1]) {
cout << "1\n";
return 0;
}
for (int i=1; i<n-2; ++i) {
if (s[0] != s[i] && s[i+1] != s[n-1]) {
cout << "2\n";
return 0;
}
}
cout << "-1\n";
}
| // chrono::system_clock::now().time_since_epoch().count()
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#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 debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MAXN = (int)1e5 + 5;
const int INF = (int)1e9;
int dp[MAXN], mem[26];
int n;
string s;
void solve() {
cin >> n;
cin >> s;
s = '#' + s;
fill(dp, dp + n + 1, INF);
fill(mem, mem + 26, INF);
dp[0] = 0;
mem[s[1] - 'a'] = 0;
rep (i, 1, n + 1) {
rep (j, 0, 26) {
if (s[i] != j + 'a') {
dp[i] = min(dp[i], mem[j] + 1);
}
}
if (i < n) {
mem[s[i + 1] - 'a'] = min(mem[s[i + 1] - 'a'], dp[i]);
}
}
if (dp[n] == INF) {
dp[n] = -1;
}
cout << dp[n] << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
for (int i = 1; i <= tt; ++i) {
solve();
}
return 0;
} |
/*
( _
) /=>
( +____________________/\/\___ / /|
.''._____________'._____ / /|/\
: () : :\ ----\| \ )
'..'______________.'0|----| \
0_0/____/ \
|---- /----\
|| -\\ --| \
|| || ||\ \
\\____// '| \
Bang! Bang! .'/ |
.:/ |
:/_________|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BALJOT SINGH
EVERYONE HAS THEIR OWN TIME
DEBUG->COMMENT FAST
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define loop(i,a,b) for(int i = a; i<=b ; i++)
#define rep(i,a,b) for(int i = a; i>=b ; i--)
#define pb push_back
#define mp make_pair
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL) ;cout.tie(NULL);
using namespace std;
using namespace __gnu_pbds;
using ll =long long ;
template<class T> using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll M = 1e9 + 7;
const ll iM = 1e5 + 5;
const ll inf = 2e18;
//modular arithmetic worst part-----------------------------------
ll mod(ll x){ return (x%M+M)%M;}
ll mod_min(ll a, ll b){ ll ans= (mod(a)-mod(b)); if(ans<0) ans=mod(ans+M); return ans;}
ll mod_mul(ll a,ll b){ return mod(mod(a)*mod(b));}
ll mod_add(ll a,ll b){ return mod(mod(a)+mod(b));}
ll power(ll x,ll y) { ll res = 1;if (x == 0) return 0;while (y > 0){if (y & 1) res=mod_mul(res,x); y = y>>1; x=mod_mul(x,x);} return res;}
ll gcd_extend(ll a, ll b, ll& x, ll& y) {if (b == 0) {x = 1;y = 0;return a;}ll x1, y1;ll d = gcd_extend(b, a % b, x1, y1);x = y1;y = x1 - y1 * (a / b);return d;}
ll modinverse(ll value) {ll x, y;ll res=gcd_extend(value,M,x,y);if(res!=1)return -1;return mod(x);}
//----------------------------------------------------------------------------------------------------------------------------------
int main()
{
FAST;
ll n;
cin>>n;
if(n<=7)
{
cout<<"-1";
return 0;
}
for(ll i=1;i<log(inf)/log(5);i++)
for(ll j=1;j<log(inf)/log(3);j++)
if(((ll)powl(5,i)+(ll) powl(3,j))==n)
{
cout<<j<<" "<<i;
return 0;
}
cout<<"-1";
}
| //
// main.cpp
// atcoder
//
// Created by 荣恒嬉 on 2020/10/24.
//
#include <bits/stdc++.h>
#define ll unsigned long long
using namespace std;
int main() {
ll a;
cin>>a;
ll x=1,y=1;
for(int i=1;i<38;i++){
x*=3;
ll y=1;
for(int j=1;j<28;j++){
y*=5;
if(x+y==a)
{
cout<<i<<" "<<j;
return 0;
}
}
}
cout<<-1;
return 0;
}
|
#include<bits/stdc++.h>
#define ar long long int
using namespace std;
int main() {
ar n;
cin>>n;
ar a[n];
for(ar i=0; i<n; i++){
cin>>a[i];
}
ar sum = 0;
for(ar i=0; i<n; i++){
if(a[i] <= 10){
continue;
} else {
sum = sum + (a[i] - 10);
}
}
cout<<sum<<endl;
return 0;
} | #include <bits/stdc++.h>
#define gc() getchar()
using namespace std;
typedef long long ll;
template <typename T> void rd(T &x){
ll f=1;x=0;char c=gc();
for(;!isdigit(c);c=gc())if(c=='-')f=-1;
for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
x*=f;
}
int main(){
int n,a,ans=0;
rd(n);
for(;n;--n)rd(a),ans+=max(0,a-10);
printf("%d\n",ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;cin>>N;
for(int i=1;i<=N;i++)
cout<<(i*2%N==0? N:i*2%N)<<" "<<((i*2+1)%N==0? N:(i*2+1)%N)<<endl;
} | #pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using namespace std;
void IO(){
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif
}
const int N = 1e6 + 7, K = 26, M = 5e3 + 5, mod = 1e9 + 7, dx[] = {0,1,-1,0}, MOD = 998244353,
dy[] = {1,0,0,-1};
int tc = 1, n, m, a[M][M], b[N], ans, k, cnt[N], grid[M][M], L[N], R[N], pre[N];
char s[N];
void test_case(){
scanf("%d", &n);
for(int i = 1 ; i <= n ; i++){
bool ok = true;
int j = i;
while(j){
ok &= (j % 10) != 7;
j /= 10;
}
j = i;
while(j){
ok &= (j % 8) != 7;
j /= 8;
}
ans += ok;
}
printf("%d\n", ans);
}
int main(){
//IO();
//scanf("%d", &tc);
while(tc--){
test_case();
}
}
|
#include <bits/stdc++.h>
const long long INF = 1ll << 61;
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<vector<pair<int, int>>> edges(26);
for (int e = 0; e < M; e++) {
int A, B;
char C;
cin >> A >> B >> C;
edges[C - 'a'].push_back({--A, --B});
}
vector<vector<int>> graph(N * N);
auto index = [&](int u, int v) {
return min(u, v) * N + max(u, v);
};
for (int ch = 0; ch < 26; ch++) {
for (auto [u, v] : edges[ch]) {
for (auto [w, x] : edges[ch]) {
graph[index(u, w)].push_back(index(v, x));
graph[index(u, x)].push_back(index(v, w));
graph[index(v, w)].push_back(index(u, x));
graph[index(v, x)].push_back(index(u, w));
}
}
}
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
vector<long long> dist(N * N, INF);
pq.push({0ll, index(0, N - 1)});
dist[index(0, N - 1)] = 0;
while (!pq.empty()) {
auto [cost, node] = pq.top();
pq.pop();
if (dist[node] < cost) continue;
for (int next : graph[node]) {
if (dist[next] <= cost + 1) continue;
dist[next] = cost + 1;
pq.push({dist[next], next});
}
}
long long ans = INF;
for (int i = 0; i < N; i++) {
ans = min(dist[index(i, i)] * 2, ans);
}
for (int ch = 0; ch < 26; ch++) {
for (auto [u, v] : edges[ch]) {
ans = min(dist[index(u, v)] * 2 + 1, ans);
}
}
cout << (ans < INF ? ans : -1) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
struct dijkstra {
ll N;
struct edge { ll to, d; };
vector<vector<edge>> adj;
vector<ll> minlen;
dijkstra(ll n) { N = n; adj.resize(N); minlen.assign(N,LLINF); }
ll operator[](const ll &x) const { return minlen[x]; }
void add_edge(ll from, ll to, ll d){
adj[from].push_back({to,d});
}
vector<ll> calc(ll n=0) {
auto c = [](const p_ll &x, const p_ll &y){return x.second>y.second;};
priority_queue<p_ll, vector<p_ll>, decltype(c)> q(c);
minlen[n] = 0; q.push(make_pair(n,0));
while(q.size()) {
p_ll now = q.top(); q.pop();
ll np = now.first, nd = now.second;
if (nd>minlen[np]) continue;
for (auto x: adj[np]) {
if (minlen[x.to]<=minlen[np]+x.d) continue;
q.push(make_pair(x.to, minlen[np]+x.d));
minlen[x.to] = minlen[np]+x.d;
}
}
return minlen;
}
};
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
ll R, C;
ll gid(ll i, ll j, bool ura) { return i*C + j + ura*(R*C); }
int main() {
cin >> R >> C;
ll A[R][C-1]; rep(i,R) rep(j,C-1) cin >> A[i][j];
ll B[R-1][C]; rep(i,R-1) rep(j,C) cin >> B[i][j];
dijkstra ds(R*C*2);
rep(i,R) rep(j,C-1) {
ds.add_edge(gid(i,j,false), gid(i,j+1,false), A[i][j]);
ds.add_edge(gid(i,j+1,false), gid(i,j,false), A[i][j]);
}
rep(i,R-1) rep(j,C) {
ds.add_edge(gid(i,j,false), gid(i+1,j,false), B[i][j]);
ds.add_edge(gid(i+1,j,true), gid(i,j,true), 1);
}
rep(i,R) rep(j,C) {
ds.add_edge(gid(i,j,false), gid(i,j,true), 1);
ds.add_edge(gid(i,j,true), gid(i,j,false), 0);
}
vector<ll> d = ds.calc();
ll result = ds[gid(R-1,C-1,false)];
cout << result << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const long nPrime = 998244353;
int main() {
long n,m;
cin >> n >> m;
vector<long> viPowSum(n,0);
vector<long> viPow(m,1);
for(long i = 0; i < n; i++){
for(long j = 0; j < m; j++){
viPowSum[i] += viPow[j];
viPow[j] *= j;
viPow[j] %= nPrime;
}
viPowSum[i] %= nPrime;
}
vector<long> viPowM(n+1,1);
for(long i = 1; i < n+1; i++){
viPowM[i] = viPowM[i-1] * m;
viPowM[i] %= nPrime;
}
vector<long> viDP(n+1,0);
for(long i = 1; i < n+1; i++){
viDP[i] = viDP[i-1] * m;
long nCount0 = 0;
for(long j = 0; j < i-1; j++){
nCount0 += viPowSum[j] * viPowM[i-j-2];
nCount0 %= nPrime;
}
viDP[i] += (viPowM[i] - nCount0);
viDP[i] %= nPrime;
if(viDP[i] < 0){
viDP[i] += nPrime;
}
}
cout << viDP[n] << endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)
template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}
const char newl = '\n';
// const ll mod = 1000000007;
const ll mod = 998244353;
const int MAX_N = 100010;
ll fact[MAX_N],fact_inv[MAX_N],inv[MAX_N];
ll pow_mod(ll a,ll b) {
ll ret;
if (b < 0) ret = pow_mod(a,mod+b-1);
else if (b == 0) ret = 1;
else if (b == 1) ret = a;
else {
ll c = pow_mod(a,b/2);
if (b%2) ret = (c*c)%mod*a%mod;
else ret = c*c%mod;
}
return ret;
}
void init(int x) {
fact[0] = 1;fact[1] = 1;
for (int i = 2;i <= x;++i) fact[i] = fact[i-1]*i%mod;
fact_inv[x] = pow_mod(fact[x],-1);
for (int i = x;i > 0;--i) fact_inv[i-1] = fact_inv[i]*i%mod;
for (int i = 1;i <= x;++i) inv[i] = fact_inv[i]*fact[i-1]%mod;
}
ll combi(ll a, ll b) {
return fact[a]*fact_inv[b]%mod*fact_inv[a-b]%mod;
}
ll permu(ll a, ll b) {
return fact[a]*fact_inv[a-b]%mod;
}
int main() {
int n,m;
cin >> n >> m;
vector<vector<ll>> a(m+1,vector<ll>(n+1,1));
for (int i = 0;i < m+1;++i) for (int j = 0;j < n;++j) a[i][j+1] = a[i][j]*i%mod;
ll ans = n*a[m][n]%mod;
for (int i = 1;i < n;++i) for (int j = 0;j < m;++j) {
(ans -= (n-i)*a[m-j-1][i-1]%mod*a[m][n-i-1]%mod) %= mod;
}
cout << (ans+mod)%mod << newl;
} |
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
#include<bitset>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
if ((tmp & ma0) ^ ma0) {
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += (72 - dig >> 3);
}
else {
tmp = tmp & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 8;
if ((ct = *ci++) >= '0') {
tmp = tmp * 10 + ct - '0';
if ((ct = *ci++) >= '0') {
tmp = tmp * 10 + ct - '0';
ci++;
}
}
}
return tmp;
}
ll tmp[200001];
void pakuri_sort(int N, ll A[]) {
const int b = 8;
rep(k, 4) {
int kazu[1 << b] = {}, kazu2[1 << b] = {};
rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
k++;
rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
}
}
bitset<400010> ST;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N = getint(), M = getint();
ll XY[200000];
int k = 0;
int nm1 = N - M, nm2 = N + M;
rep(i, M) {
int x = getint();
int y = getint();
if (y < nm1 | y > nm2) continue;
XY[k++] = x | (ll)(y - N + 200001) << 32;
}
M = k;
pakuri_sort(M, XY);
ST[200001] = 1;
const ll ma = (1ll << 32) - 1;
int m = 0;
while (m < M) {
int m0 = m;
int x = (XY[m] & ma);
while (m < M && (XY[m] & ma) == x) {
int y = (XY[m] >> 32);
if (ST[y - 1] | ST[y + 1]) XY[m] |= 1ll;
else if (ST[y]) XY[m] &= (~1ll);
else XY[m] = 0;
m++;
}
for (int p = m0; p < m; p++) {
int y = (XY[p] >> 32);
if (XY[p] & 1) ST[y] = 1;
else if (XY[p]) ST[y] = 0;
}
}
int kotae = ST.count();
printf("%d\n", kotae);
Would you please return 0;
} | #include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main()
{
int N;
int M;
vector<string> S(M, "");
int L = 0;
cin >> N >> M;
for (int i = 0; i < M; i++)
{
cin >> S.at(i);
L = max(L, (int)S.at(i).size());
}
/*
todo 親和性の高い部分列同士をあらかじめ結合しておく
//結合度3以上の二つを結合する。
for(int ketsugou = L; L > 2; L--){
for(int i = 0; i)
}
*/
//ランダムにあてはめる。
vector<vector<char>> board(20, vector<char>(20, '.'));
//todo:Sを並べ替える
//mt19937 get_rand_mt;
//shuffle( S.begin(), S.end(), get_rand_mt );
//sort(S.begin(), S.end(), [](string &a, string &b){return a.size() >= b.size();});
for (string s : S)
{
int best_score = -1;
int best_pos = -1;
//横0縦1
bool best_muki_is_tate = true;
for (int i = 0; i < 400; i++)
{
int pos_i = i / 20;
int pos_j = i % 20;
int goodness = 0;
for (int s_index = 0; s_index < (int)s.size(); s_index++)
{
if (board.at((pos_i + s_index) % 20).at(pos_j) == '.')
{
continue;
}
else if (board.at((pos_i + s_index) % 20).at(pos_j) == s.at(s_index))
{
goodness += 1;
}
else
{
goodness = -1;
break;
}
}
if (goodness > best_score)
{
best_score = goodness;
best_pos = i;
best_muki_is_tate = false;
}
goodness = 0;
for (int s_index = 0; s_index < (int)s.size(); s_index++)
{
if (board.at(pos_i).at((pos_j + s_index) % 20) == '.')
{
continue;
}
else if (board.at(pos_i).at((pos_j + s_index) % 20) == s.at(s_index))
{
goodness += 1;
}
else
{
goodness = -1;
break;
}
}
if (goodness > best_score)
{
best_score = goodness;
best_pos = i;
best_muki_is_tate = true;
}
}
//boardを更新する。
if (best_score == -1)
{
continue;
}
int pos_i = best_pos / 20;
int pos_j = best_pos % 20;
for (int s_index = 0; s_index < (int)s.size(); s_index++)
{
board.at((pos_i + !best_muki_is_tate * s_index)%20).at((pos_j + best_muki_is_tate * s_index)%20) = s.at(s_index);
}
}
for(int i = 0; i < 20; i++){
for(int j = 0; j < 20; j++){
cout << board.at(i).at(j);
}
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
int pw(int n,int k){
assert(k>=0);
int res=1;
while(k){
if(k&1)res*=n;
n*=n;
k>>=1;
}
return res;
}
signed main(){
string s;cin>>s;
int ans=0;
REP(i,10000){
bool ok=1;
set<int> se;
REP(j,4)se.insert((i/pw(10,j))%10);
REP(j,10){
if(s[j]=='o'&&!se.count(j))ok=0;
if(s[j]=='x'&&se.count(j))ok=0;
}
ans+=ok;
}
cout<<ans<<endl;
}
|
#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;
ll INF = 1e16;
int inf = 1e9;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = ((n) - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
// inputs
#define VLLIN(x, n) vll x(n); REP(i, n) cin >> x[i];
#define VIIN(x, n) vin x(n); REP(i, n) cin >> x[i];
#define VLDIN(x, n) vld x(n); REP(i, n) cin >> x[i];
#define SIN(s, n) string s; cin >> s;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() {
cerr << endl;
}
template <typename H, typename... T>
void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T>
void print(T x) {
cout << x << "\n";
}
template <typename T>
void print(vector<T>& x) {
int N = x.size();
REP(i, N) {
if (i > 0) cout << " ";
cout << x[i];
}
cout << "\n";
}
ll fact(ll n) {
ll ans = 1;
REP(i, n) ans *= i + 1;
return ans;
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
string s;
cin >> s;
ll ans = 0;
REP(x, 10000) {
int cur = x;
set<int> ss;
while(cur > 0) {
ss.insert(cur % 10);
cur /= 10;
}
if (x < 1000) ss.insert(0);
bool f = true;
REP(j, s.size()) {
if (s[j] == 'x') {
if (ss.count(j) > 0) {
f = false;
break;
}
} else if (s[j] == 'o') {
if (ss.count(j) == 0) {
f = false;
break;
}
}
}
if (f) ans++;
}
print(ans);
}
|
#include<bits/stdc++.h>
using namespace std;
#define A(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define int long long
void _read();
int32_t main () { _read();
int n;
cin >> n;
vector<char> vec;
for( int i = 0; i < n; i++) {
char c; cin >> c;
if( c == 'x' && sz(vec) >= 2 && vec[sz(vec)-2] == 'f' && vec.back() == 'o') {
vec.pop_back();
vec.pop_back();
continue;
}
vec.push_back(c);
}
cout << sz(vec) << '\n';
return 0;
};
void _read() {
ios_base :: sync_with_stdio(false);
cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt","r",stdin);
#endif
}
| #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define __ ios_base::sync_with_stdio(false);cin.tie(NULL);
const int maxn = 200007;
const int mod = 998244353;
int mod_pow(int a,int b){
int x = 1;
while(b){
if(b&1)(x*=a)%=mod;
(a*=a)%=mod;
b>>=1;
}
return x;
}
vector<int> g[maxn],gr[maxn],graph[maxn];
vector<bool> used(maxn),vis(maxn);
vector<int> order, component;
void dfs1(int u) {
used[u] = true;
for(auto v:g[u]){
if(!used[v])
dfs1(v);
}
order.push_back(u);
}
void dfs2(int u) {
used[u] = true;
component.push_back(u);
for(auto v:gr[u]){
if(!used[v])
dfs2(v);
}
}
void dfs(int u){
vis[u] = true;
for(auto v:graph[u]){
if(!vis[v])
dfs(v);
}
}
signed main(){__
int n;
cin>>n;
vector<int> nums(n);
for(auto &c:nums)cin>>c;
vector<int> z[n+1];
for(int i = 0;i<n;i++){
g[i].push_back(nums[i]-1);
gr[nums[i]-1].push_back(i);
z[nums[i]].push_back(i);
}
for(int i= 0;i<=n;i++){
for(int j = 1;j<z[i].size();j++){
g[z[i][j-1]].push_back(z[i][j]);
g[z[i][j]].push_back(z[i][j-1]);
gr[z[i][j]].push_back(z[i][j-1]);
gr[z[i][j-1]].push_back(z[i][j]);
}
}
int c = 0;
used.assign(n,false);
for(int i=0;i<n;i++)
if(!used[i])
dfs1(i);
used.assign(n,false);
vector<int> p(n);
for(int i=0;i<n;i++){
int v = order[n-1-i];
if(!used[v]){
dfs2(v);
for(auto d:component)
p[d] = c;
c++;
component.clear();
}
}
// for(auto d:p)cout<<d<<" ";
// cout<<endl;
for(int i = 0;i<n;i++){
graph[p[nums[i]-1]].push_back(p[i]);
graph[p[i]].push_back(p[nums[i]-1]);
}
int cont = 0;
for(int i = 0;i<c;i++){
if(!vis[i]){
dfs(i);
cont++;
}
}
int ans = mod_pow(2,cont)-1;
if(ans<0)ans+=mod;
cout<<ans<<endl;
return 0;
}
|
#define G 3
#include<map>
#define AK goto
#include<cmath>
#include<queue>
#include<bitset>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define md ((l+r)>>1)
#define cht 1000000007
#define inf 1145141919810
#define mem(i,j)memset(i,j,sizeof(i))
#define F(i,j,n)for(int i=j;i<=n;i++)
#define D(i,j,n)for(int i=j;i>=n;i--)
using namespace std;
namespace sky_chen{
// char buf[1<<23],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
// #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
inline ll read(){ll ans=0,f=1;char a=getchar();while(a>'9'||a<'0'){if(a=='-')f=-1;a=getchar();}while(a>='0'&&a<='9')ans=ans*10+(a^48),a=getchar();return ans*f;}
inline char red(){char a=getchar();while(a>'z'||a<'a')a=getchar();return a;}
// inline void print(ll x){if(x>9)print(x/10);*O++=x%10+'0';}
inline void cadd(ll &x,ll y){x=(x+y)%cht;}
inline void cmin(ll &x,ll y){x=min(x,y);}
inline void cmax(ll &x,ll y){x=max(x,y);}
ll qpow(ll n,ll base=cht-2){ll ans=1;while(base){if(base&1)ans=ans*n%cht;n=n*n%cht;base>>=1;}return ans;}
const int maxn = 1e6+5;
const int maxc = 3e5+5;
ll n,x[maxn],y[maxn];
struct node{
ll a=0,b=0,c=0;
}p1,p2,t,f[maxn],g[maxn];
int yyds(){
n=read();
F(i,1,n)x[i]=read(),y[i]=read();
ll m=read();
p1.a=1;p2.b=1;
f[0]=p1;g[0]=p2;
F(i,1,m){
ll opt=read();
if(!(opt-1)){
t=p1;p1=p2;p2.a=-t.a;p2.b=-t.b;p2.c=-t.c;
f[i]=p1;g[i]=p2;
}
else if(!(opt-2)){
t=p2;p2=p1;p1.a=-t.a;p1.b=-t.b;p1.c=-t.c;
f[i]=p1;g[i]=p2;
}
else if(!(opt-3)){
p1.c=2*read()-p1.c;p1.b=-p1.b;p1.a=-p1.a;
f[i]=p1;g[i]=p2;
}
else{
p2.c=2*read()-p2.c;p2.b=-p2.b;p2.a=-p2.a;
f[i]=p1;g[i]=p2;
}
}
// F(i,0,m)cout<<f[i].a<<' '<<f[i].b<<' '<<f[i].c<<' '<<g[i].a<<' '<<g[i].b<<' '<<g[i].c<<endl;
ll q=read();
F(i,1,q){
ll sb1=read(),sb2=read();
ll za=x[sb2],zb=y[sb2];
ll nza=f[sb1].a*za+f[sb1].b*zb+f[sb1].c,nzb=g[sb1].a*za+g[sb1].b*zb+g[sb1].c;
printf("%lld %lld\n",nza,nzb);
}
return 0;
}
}
int main(){return sky_chen::yyds();}
//sky_chen txdy! | #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={0,1,-1,0,1,1,-1,-1,0};
const ll dx[9]={1,0,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(){
ll n;cin >> n;
cout << (1<<n)-1 <<endl;
repl(bit,1,1<<n){
rep(cit,1<<n){
if(__builtin_popcount(bit&cit)%2){
cout << "A";
}
else cout << "B";
}
cout << endl;
}
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define vl vector<ll>
#define vs vector<string>
#define mll map<ll,ll>
#define pll pair<ll,ll>
#define sl set<ll>
#define pql priority_queue<ll>
#define rep(i,n) for(i=0;i<n;i++)
#define repr(i,n) for(i=n-1;i>=0;i--)
#define REP(i,a,b) for(i=a;i<b;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(), a.rend()
#define vsort(a) sort(all(a))
#define rvsort(a) sort(rall(a))
#define F first
#define S second
#define pi acos(-1)
#define inf 10000000000000000
#define mod 1000000007
#define MA LLONG_MAX
#define MI LLONG_MIN
long long power(long long x,ll y)
{
long long res = 1;
x = x % mod;
while (y > 0)
{
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
long modInverse(long long n)
{
return power(n, mod - 2);
}
long long ncr(long long n, ll r)
{
if (n < r)
return 0;
if (r == 0)
return 1;
long long fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % mod;
return (fac[n] * modInverse(fac[r]) % mod*modInverse(fac[n - r])% mod)% mod;
}
void solve()
{
ll n,m,i,j,ans=0,fans=0;
cin>>n>>m;
vl a(m),b(m);
rep(i,m)
cin>>a[i]>>b[i];
ll k;
cin>>k;
vl c(k),d(k);
rep(i,k)
{
cin>>c[i]>>d[i];
}
ll temp=pow(2,k)-1;
for(i=0;i<=temp;i++)
{
bitset<20> bb(i);
vl count(n+1,0);
ans=0;
for(j=0;j<k;j++)
{
if(bb[j]==0)
count[c[j]]++;
else
count[d[j]]++;
}
for(j=0;j<m;j++)
{
if(count[a[j]]>0&&count[b[j]]>0)
ans++;
}
//cout<<ans<<" ";
fans=max(fans,ans);
}
cout<<fans<<endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// #ifdef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t=1;
//cin>>t;
while (t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
#include <vector>
#include<math.h>
#include<string.h>
using namespace std;
#define MAX 300005
#define MOD 1000000007
#define GMAX 19
#define INF 20000000000000000
#define EPS 0.000000001
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
int dishes[MAX];
pair<int,int> conditions[MAX];
int add[20][2];
int ans;
void backtrack(int index,int k,int m)
{
int i;
if(index==k)
{
int curr=0;
for(i=0;i<m;i++)
{
curr=curr+(dishes[conditions[i].first]>0&&dishes[conditions[i].second]>0);
}
ans=max(ans,curr);
return;
}
for(i=0;i<2;i++)
{
dishes[add[index][i]]++;
backtrack(index+1,k,m);
dishes[add[index][i]]--;
}
}
int main()
{
int n,m,k,i,j;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d %d",&conditions[i].first,&conditions[i].second);
}
scanf("%d",&k);
for(i=0;i<k;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&add[i][j]);
}
}
backtrack(0,k,m);
printf("%d",ans);
return 0;
}
|
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
#include<cstring>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvl;
typedef vector<pll> vpll;
#define endl '\n'
#define fast std::ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define mp make_pair
void solve(){
ll n;
cin>>n;
pair<ll,ll> p[n];
ll t[n];
for(int i=0;i<n;i++){
cin>>t[i]>>p[i].first>>p[i].second;
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(p[i].first>p[j].second || p[i].second<p[j].first)continue;
if(p[i].first==p[j].second){
if(!((t[i]==1 || t[i]==2)&&(t[j]==1 || t[j]==3)))continue;
}
if(p[i].second==p[j].first){
//cout<<"*"<<endl;
if(!((t[j]==1 || t[j]==2)&&(t[i]==1 || t[i]==3)))continue;
}
ans++;
// cout<<i<<" "<<j<<endl;
}
}
cout<<ans<<endl;
}
int main(){
fast;
//clock_t begin = clock();
ll tc=1;//cin>>tc;
for(ll i=1;i<=tc;i++){
//cout<<"Case #"<<i<<": ";
solve();
}
//clock_t end = clock();double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;cerr << elapsed_secs;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define INF 1e9
typedef long long ll;
typedef long double ld;
int main(){
ll n;
cin >> n;
vector<pair<double,double>> v(n);
REP(i,n){
ll t;
double l, r;
cin >> t >> l >> r;
if(t == 2)r-=0.1;
else if(t==3)l+=0.1;
else if(t==4){
l+=0.1;
r-=0.1;
}
v[i] = {l,r};
}
ll cnt = 0;
REP(i,n-1){
FOR(j,i+1,n){
if(v[j].first <= v[i].second && v[i].first <= v[j].second)cnt++;
}
}
cout << cnt << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define int LL
const int N = 300;
int c[N][N];
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
c[0][0] = 1;
for (int i = 1; i < N; i++) {
c[i][0] = 1;
for (int j = 1; j <= i; j++) {
c[i][j] = c[i - 1][j]+ c[i - 1][j - 1];
}
}
int n;
cin >> n;
cout << c[n - 1][11] << endl;
return 0;
}
| #include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
using ll = long long;
void solve(ll n, ll r)
{
long long p = 1, k = 1;
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
long long m = __gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else
p = 1;
cout << p << endl;
}
signed main()
{
ll n;
cin >> n;
n--;
solve(n, 11);
return 0;
}
// ll fact(ll n);
// ll nCr(ll n, ll r)
// {
// cout << fact(n) << " " << fact(r) << " ";
// return fact(n) / (fact(r) * fact(n - r));
// }
// ll fact(ll n)
// {
// ll res = 1;
// for (ll i = 2; i <= n; i++)
// res = res * i;
// return res;
// }
// ll main()
// {
// ll n,r;
// cin >> n;
// n--;
// cout << nCr(n,11);
// return 0;
// } |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define inf 2000000000
#define infLL 2000000000000000000
#define MAX5 100005
#define MAX6 1000006
#define MAX7 10000007
#define sf(a) scanf("%d", &a)
#define sfl(a) scanf("%lld", &a)
#define sfs(a) scanf("%s", a)
#define sline(a) scanf("%[^\n]%*c", a);
#define pf(a) printf("%d\n", a)
#define pfl(a) printf("%lld\n", a)
#define pfs(a) printf("%s\n", a)
#define Case(t) printf("Case %d: ", t)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mod 1000000007
#define Mod 998244353
#define PI acos(-1.0)
#define eps 1e-9
#define mem(a, b) memset(a, b, sizeof(a))
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = MAX5;
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
//#endif
FASTIO;
ll a, b, c, d;
cin>>a>>b>>c>>d;
if(a==0) cout<<0<<endl;
else {
ll mx = a, cnt = 5000000, cc = 0, ans = -1;
for(int i = 1; i <= cnt; i++) {
a += b;
cc += c;
if(a <= (cc * d)) {
ans = i;
break;
}
ll dif = a - (cc * d);
if(dif > mx) {
break;
}
mx = dif;
}
cout<<ans<<endl;
}
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define pb(a) push_back(a)
#define vll vector<ll>
#define loop(i, n) for(ll i=1;i<=n;i++)
#define loop0(i, n) for(ll i=0;i<n;i++)
#define in(i) scanf("%lld", &i);
#define out(i) printf("%lld", i)
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define mp(i, j) make_pair(i, j)
#define google cout<<"Case #"<<tc-t<<": ";
const ll mod = 1000000007;
const ll big = 2999999999999999999;
const ll small = -2999999999999999999;
void in2(ll& a, ll& b) { in(a); in(b); }
void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); }
void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; }
void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); }
ll n, k;
vpll a;
void solve()
{
in2(n, k);
a.resize(n);
loop0(i, n)
in2(a[i].first, a[i].second);
sort(a.begin(), a.end());
ll rem = k;
ll cv = 0;
loop0(i, n)
{
if(cv + rem < a[i].first)
{
cv += rem;
rem = 0;
break;
}
else
{
rem -= (a[i].first-cv);
rem += a[i].second;
cv = a[i].first;
}
}
cv += rem;
cout<<cv<<"\n";
}
int main()
{
solve();
}
|
#include <bits/stdc++.h>
#include <fstream>
#include <cstdio>
using namespace std;
// vector<pair<int,int> > v;
// cin.ignore();//twice getline(cin,s);
// g++ iterator.cpp -std=c++17
// cout<<(A + B == C ? "YES" : "NO")<<endl;
// __gcd(a,b)
// string s=to_string(n);
// string p=s.substr(0,len);
// string q=s.insert(0,str);
// sort(arr, arr+n, greater<int>());
// cout<<fixed<<setprecision(9)<<pi<<endl
// const double pi = acos(-1.0);
// sort(v.rbegin(),v.rend());
#define ff first
#define ss second
#define pb push_back
#define mk make_pair
#define vll vector<ll>
#define mll map<ll,ll >
#define mlli map<ll,ll>::iterator
#define size size()
#define endl "\n"
#define ll long long int
#define ld long double
void fast(){ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);}
bool comp(int a,int b)
{
return (a<b);
// cout<<min({a,b,c,d},comp)<<endl;
}
int main(void)
{
fast();
ll n;
cin>>n;
ll x;
map<ll,ll> m;
for(ll i=0;i<n;i++)
{
cin>>x;
m[x%200]++;
}
ll ans = 0;
for(auto it=m.begin();it!=m.end();it++)
{
ans += ((it->second)*(it->second-1))/2;
}
cout<<ans<<endl;
} | /** I can do this all day **/
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define Mp make_pair
#define SZ(x) (int)x.size()
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;
ll pw(ll a , ll b, ll M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }
int n, A[N];
bitset < N > bt;
int main()
{
cin >> n;
bt[0] = 1;
int sum = 0, tot = 1e9;
for(int i = 1; i <= n; i ++)
{
cin >> A[i];
bt |= bt << A[i];
sum += A[i];
}
for(int i = 0; i < N ;i ++)
{
if(bt[i])
{
tot = min(tot, max(i, sum - i));
}
}
cout << tot << endl;
return 0;
}
/** test corner cases(n = 1?) watch for overflow or minus indices **/
|
#include <bits/stdc++.h>
#include <cmath>
#include <limits>
using namespace std;
typedef long long LL;
static long long INF = (1LL<<62);
int isPrime(LL p){
if(p<=1) return 0;
if(p==2 || p==3 || p==5 || p==7) return 1;
if(p==4 || p==6 || p==8 || p==9) return 0;
LL i;
int ret = 1;
for(i=2; i*i<=p; i++){
if(p%i==0 && i!=p){
ret = 0;
break;
}
}
return ret;
}
void _factorize(long long N, map<long long, int> &mp){
if(N == 1){
return;
}
if(isPrime(N)){
mp[N] += 1;
return;
}
long long i = 2, n = N;
for(i = 2; n != 1 && i < (N/2) + 1; i++){
if((n % i)==0){
if(isPrime(i)){
int e = 0;
long long u = n;
while((u % i)==0){
e++;
u = u / i;
n = u;
}
mp[i] = e;
if(isPrime(n)){
mp[n] = 1;
break;
}
}
}
}
return;
}
vector<LL> A;
void dfs(map<long long, int> mp, vector<int> v){
if(v.size() == mp.size()){
LL d = 1;
int i= 0;
for(auto itr = mp.begin(); itr != mp.end(); itr++){
LL p = itr->first;
int e = v[i];
for(int j=0; j<e; j++){
d *= p;
}
i++;
}
A.push_back(d);
return;
}
int s = v.size();
int i = 0, e = -1;
for(auto itr = mp.begin(); itr != mp.end(); itr++){
if(i == s){
e = itr->second;
break;
}
i++;
}
for(int j=0; j<=e; j++){
v.push_back(j);
dfs(mp, v);
v.pop_back();
}
return;
}
int main(int argc, char* argv[]){
cin.tie(0);
ios::sync_with_stdio(false);
LL N; cin >> N;
map<long long, int> mp;
_factorize(N, mp);
vector<int> v;
dfs(mp, v);
sort(A.begin(), A.end());
for(int i=0; i<A.size(); i++){
printf("%lld\n", A[i]);
}
return 0;
}
| #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<long long> enum_divisors(long long N) {
vector<long long> res;
for (long long i = 1; i * i <= N; ++i) {
if (N % i == 0) {
res.push_back(i);
// 重複しないならば i の相方である N/i も push
if (N/i != i) res.push_back(N/i);
}
}
// 小さい順に並び替える
sort(res.begin(), res.end());
return res;
}
int main() {
long long N;
cin >> N;
const auto &res = enum_divisors(N);
for (int i = 0; i < res.size(); ++i) cout << res[i] << " ";
cout << endl;
} |
#include <bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
using namespace std;
ll dp[105][100005];
ll t[105];
ll n;
ll totalsum=0;
ll solve(ll pos,ll sum)
{
if(sum>totalsum/2)
return INT_MIN;
if(pos>=n&&sum<=totalsum/2)
return sum;
ll &ans=dp[pos][sum];
if(ans!=-1)
return ans;
ans=0;
ans=max(solve(pos+1,sum),solve(pos+1,sum+t[pos]));
return ans;
}
int main() {
fast();
// ll t;
// cin>>t;
// while(t--)
// {
// }
memset(dp,-1,sizeof dp);
cin>>n;
for(int i=0;i<n;i++)
{
cin>>t[i];
totalsum+=t[i];
}
// cout<<totalsum<<" ";
cout<<totalsum-solve(0,0);
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxl=2e5+10;
const int mod=1e9+7;
int n,k;
int a[maxl];
ll dp[maxl][17][2][2];
char s[maxl];
inline ll dfs(int pos,int st,bool up,bool lead)
{
int cnt=__builtin_popcount(st);
if(cnt>k) return 0;
if(pos>n) return cnt==k;
ll &ret=dp[pos][cnt][up][lead];
if(ret!=-1)
return ret;
ret=0;
int r=up?a[pos]:15;
for(int i=0;i<=r;i++)
ret=(ret+dfs(pos+1,(!i&&!lead)?st:st|(1<<i),up&&(i==r),lead|i))%mod;
return ret;
}
int main()
{
scanf("%s",s+1);scanf("%d",&k);
n=strlen(s+1);
for(int i=1;i<=n;i++)
if(s[i]>='A' && s[i]<='Z')
a[i]=s[i]-'A'+10;
else
a[i]=s[i]-'0';
memset(dp,-1,sizeof(dp));
printf("%lld\n",dfs(1,0,1,0));
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define SZ(x) (int)x.size()
#define F first
#define S second
#define ALL(x) x.begin(), x.end()
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(i, a, b) for(int i = a; i < b; ++i)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e18;
const int MOD = 1e9 + 7;
const ld PI = acosl(-1.0);
const ld EPS = 1e-9;
#define int long long
const int N = 1000 + 5;
int dp[N][N];
std::vector<int> a(N), b(N);
int f(int i, int j) {
if(i == -1 && j == -1) return 0;
if(i == -1) return j+1;
if(j == -1) return i+1;
if(dp[i][j] != -1) return dp[i][j];
int &res = dp[i][j];
res = INF;
res = min(res, f(i-1, j) + 1);
res = min(res, f(i, j-1) + 1);
res = min(res, f(i-1, j-1) + (a[i] != b[j]));
return res;
}
void solve() {
memset(dp, -1, sizeof(dp));
int n, m;
cin >> n >> m;
FOR(i, 0, n) {
cin >> a[i];
}
FOR(j, 0, m) {
cin >> b[j];
}
cout << f(n-1, m-1) << endl;
}
signed main() {
fast;
int T = 1;
// cin >> T;
while(T--) {
solve();
}
}
| #include <bits/stdc++.h>
using namespace std;
#define fo(i, x, y) for (int i = (x); i <= (y); ++i)
#define fd(i, x, y) for (int i = (x); i >= (y); --i)
typedef long long ll;
const int p[10][4] = {{0, 0, 0, 0}
, {0, 0, 0, 0}
, {6, 2, 4, 8}
, {1, 3, 9, 7}
, {6, 4, 0, 0}
, {0, 0, 0, 0}
, {0, 0, 0, 0}
, {1, 7, 9, 3}
, {6, 8, 4, 2}
, {1, 9, 0, 0}};
int getint()
{
char ch;
int res = 0, p;
while (!isdigit(ch = getchar()) && (ch ^ '-'));
p = ch == '-'? ch = getchar(), -1 : 1;
while (isdigit(ch))
res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return res * p;
}
ll pw(ll a, ll b, ll mod)
{
ll res = 1;
while (b)
{
if (b & 1) (res *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return res;
}
int main()
{
ll a, b, c;
cin >> a >> b >> c;
a %= 10;
int s2 = pw(b, c, 2), s4 = pw(b, c, 4);
switch(a)
{
case 0: printf("0\n"); break;
case 1: printf("1\n"); break;
case 2: printf("%d\n", p[2][s4]); break;
case 3: printf("%d\n", p[3][s4]); break;
case 4: printf("%d\n", p[4][s2]); break;
case 5: printf("5\n"); break;
case 6: printf("6\n"); break;
case 7: printf("%d\n", p[7][s4]); break;
case 8: printf("%d\n", p[8][s4]); break;
case 9: printf("%d\n", p[9][s2]); break;
}
return 0;
} |
/*
मनस्वी म्रियते कामं कार्पण्यं न तु गच्छति ।
अपि निर्वाणमायाति नानलो याति शीतताम् ॥
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define mod 1000000007
#define pb push_back
#define ll long long int
#define ull unsigned ll
#define ld long double
#define vi vector<int>
#define vl vector<ll>
#define v2i vector<vector<int>>
#define v2l vector<vector<ll>>
#define ppi pair<int,int>
#define ppl pair<ll,ll>
#define vpi vector<ppi>
#define vpl vector<ppl>
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define forn(i,a,b) for(int i=a;i<b;i++)
#define forr(i,a,b) for(int i=a;i>=b;i--)
#define forv(i,m) for(auto i : m)
#define p2d(v) for(auto a:v){for(auto b:a)cout<<b<<" ";cout<<endl;}
#define p1d(v) for(auto a:v)cout<<a<<" ";cout<<endl;
#define ppd(v) for(auto a:v)cout<<a.ff<<" "<<a.ss<<endl;
#define imx INT_MAX
#define imn INT_MIN
#define inf 9000000000000000000
#define minf -inf
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define sze size()
#define rvs reverse
#define itr iterator
#define pre cout<<fixed<<setprecision(10);
#define umap unordered_map
#define uset unordered_set
#define pi 3.141592653589793
#define MAXN 100005
#define test1(x) cout << #x " = " << x << endl;
#define test2(x,y) cout << #x " = " << x << " " << #y " = " << y << endl;
#define test3(x,y,z) cout << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << endl;
#define test4(w,x,y,z) cout << #w " = " << w << " " << #x " = " << x << " " << #y " = " << y << " " << #z " = " << z << endl;
#define oset tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
/*
const ll infinity = 9e18;
bool compare(ll a,ll b) {return a > b;}
bool compare1(ppl a,ppl b) {return a.ff > b.ff;}
bool compare2(ppl a,ppl b) {return a.ss < b.ss;}
bool isprime(ll n){if(n < 2) return 0; ll i = 2; while(i*i <= n){if(n%i == 0) return 0; i++;} return 1;}
ll Npower(ll a,ll b) {ll product = 1; while(b){if(b&1){product = product*a;}a = a*a;b = b>>1;} return product;}
ll power(ll a,ll b,ll md = mod) {ll product = 1; while(b){if(b&1){product = (product*a)%md;}a = (a*a)%md;b = b>>1;} return product%md;}
ll GCD(ll a,ll b) {return b==0 ? a:GCD(b,a%b);}
ll LCM(ll a,ll b) {return (a/GCD(a,b))*b; }
*/
int main()
{
fast
/*
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//*/
ll n,m,t;
cin>>n>>m>>t;
vpl vp(m);
cin>>vp[0].ff>>vp[0].ss;
ll total = n-vp[0].ff;
bool val = 1;
if(total <= 0)
{
val = 0;
}
forn(i,1,m)
{
if(total <= 0)
{
val = 0;
}
cin>>vp[i].ff>>vp[i].ss;
total = total+(vp[i-1].ss-vp[i-1].ff);
total = min(total,n);
if(total <= 0)
{
val = 0;
}
total -= (vp[i].ff - vp[i-1].ss);
if(total <= 0)
{
val = 0;
}
}
total += (vp[m-1].ss-vp[m-1].ff);
total = min(total,n);
total -= (t-vp[m-1].ss);
if(total <= 0)
{
val = 0;
}
if(val)
{
cout<<"Yes";
}
else
{
cout<<"No";
}
return 0;
}
| /*
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
*/
#include <bits/stdc++.h>
#include <set>
#include <queue>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename Key, typename T> using omap =
tree<Key, T, less<Key>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define rb pop_back
#define pf push_front
#define rf pop_front
#define heap priority_queue
#define mpr make_pair
#define fi first
#define se second
#define endl "\n"
#define pii pair<int, int>
#define int long long
#define float long double
#define multiTest cin >> t; for(test = 0; test < t; test++)
#define toFile freopen("a.inp", "r", stdin); freopen("a.out", "w", stdout);
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int inf = 9e18;
const int mod = 1e9 + 7;
int t;
int test;
void solve() {
int n, m, t;
cin >> n >> m >> t;
int bat = n;
int tim = 0;
bool failed = false;
int a, b;
for(int i = 0; i < m; i++) {
cin >> a >> b;
bat -= (a - tim);
if(bat <= 0) {
failed = true;
break;
}
bat += b - a;
if(bat > n) bat = n;
tim = b;
}
bat -= (t - tim);
if(bat <= 0) {
failed = true;
}
if(failed) cout << "No";
else cout << "Yes";
}
signed main() {
speed;
//toFile
//multiTest
solve();
return 0;
} |
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){
a = b; return true;
}
return false;
}
template<unsigned long long m> class comb_perm{
private:
int n;
std::vector<long long> fact, fact_inv;
public:
comb_perm(): n(0), fact(1), fact_inv(1){
fact[0] = fact_inv[0] = 1;
}
comb_perm(int N):n(N), fact(N+1), fact_inv(N+1){
fact[0] = fact[1] = fact_inv[0] = fact_inv[1] = 1;
for(int i(2); i <= n; ++i) fact[i] = fact[i - 1] * i % m;
for(int i(2); i <= n; ++i) fact_inv[i] = inv(fact[i]);
}
bool resize(int N){
if(N <= n) return false;
fact.resize(N+1);
fact_inv.resize(N+1);
if(!n && N){
fact[1] = fact_inv[1] = 1;
}
for(int i(std::max(2, n+1)); i <= N; ++i) fact[i] = fact[i - 1] * i % m;
for(int i(std::max(2, n+1)); i <= N; ++i) fact_inv[i] = inv(fact[i]);
n = N;
return true;
}
long long power(long long a, unsigned long long b){
long long ret(1);
while(b){
if(b & 1) ret = (ret * a) % m;
a = (a * a) % m;
b /= 2;
if(ret < 0) ret += m;
if(a < 0) a += m;
}
return ret;
}
long long inv(long long a){ // GCD(a, m) = 1
return power(a, m-2);
}
long long permutation(int n, int r){
if(n < r) return 0;
if(n < 0 || r < 0) return 0;
long long ret(fact[n]);
ret = ret * fact_inv[n - r] % m;
return ret;
}
long long combination(int n, int r){
if(n < r) return 0;
if(n < 0 || r < 0) return 0;
long long ret(permutation(n, r));
return ret * fact_inv[r] % m;
}
long long multi_combination(int n, int r){
return combination(n + r - 1, r);
}
long long factorial(int a){
return fact[a];
}
long long inv_factorial(int a){
return fact_inv[a];
}
};
std::unordered_map<long long, int> primeFactorisation_Map(long long n){
std::unordered_map<long long, int> mp;
for(long long i(2); i * i <= n; ++i)
while(!(n % i)){
++mp[i]; n /= i;
}
if(n > 1) ++mp[n];
return mp;
}
int main(){
int N, M; std::cin >> N >> M;
if(N == 1){
std::cout << M << std::endl;
return 0;
}
// int mx{};
comb_perm<MOD2> cp(N + 30);
long long ans{};
/* std::vector<long long> hs(21);
hs[0] = 1;
for(int i{1}; i < 21; ++i){
hs[i] = hs[i-1] + cp.multi_combination(N - 1, i);
hs[i] %= MOD2;
}*/
for(int i{1}; i <= M; ++i){
auto pmp(primeFactorisation_Map(i));
long long cm{1};
for(auto [p, e]: pmp){
cm *= cp.combination(N+e-1, e);
cm %= MOD2;
// chmax(mx, e);
}
(ans += cm) %= MOD2;
}
if(ans < 0) ans += MOD2;
std::cout << ans << std::endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, b) for (ll i = a; i < b; ++i)
#define rep_down(i, a, b) for (ll i = a; i > b; --i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
#define vvvvll vector<vector<vector<vector<ll>>>>
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define vvvvdo vector<vector<vector<vector<double>>>>
#define vvvdo vector<vector<vector<double>>>
#define vvdo vector<vector<double>>
#define vdo vector<double>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>
constexpr ll INF = (1ll << 60);
// constexpr ll mod = 998244353;
// constexpr ll mod = 67280421310721;
constexpr ll mod = 1000000007;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
void pt_vll(vector<T> &v) {
ll vs = v.size();
rep(i, vs) {
cout << v[i];
if (i == vs - 1)
cout << "\n";
else
cout << " ";
}
}
template <typename T>
void pt_vvll(vector<vector<T>> &v) {
ll vs = v.size();
rep(i, vs) pt_vll(v[i]);
}
template <typename T>
void pt(T val) {
cout << val << "\n";
}
ll gcd(ll a, ll b) {
if (a % b == 0) return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
ll modpow(ll x, ll n) {
ll ret = 1;
while (n > 0) {
if (n & 1) ret = ret * x % mod;
x = x * x % mod;
n >>= 1;
}
return ret;
}
ll minv(ll a, ll m) { return modpow(a, m - 2); }
struct UnionFind {
vector<int> size, parents;
UnionFind() {}
UnionFind(int n) { // make n trees.
size.resize(n, 0);
parents.resize(n, 0);
for (int i = 0; i < n; i++) {
makeTree(i);
}
}
void makeTree(int x) {
parents[x] = x; // the parent of x is x
size[x] = 1;
}
bool isSame(int x, int y) { return findRoot(x) == findRoot(y); }
bool unite(int x, int y) {
x = findRoot(x);
y = findRoot(y);
if (x == y) return false;
if (size[x] > size[y]) {
parents[y] = x;
size[x] += size[y];
} else {
parents[x] = y;
size[y] += size[x];
}
return true;
}
int findRoot(int x) {
if (x != parents[x]) {
parents[x] = findRoot(parents[x]);
}
return parents[x];
}
int treeSize(int x) { return size[findRoot(x)]; }
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
vll A(N), B(N);
UnionFind uf(4e5 + 10);
vector<bool> lp(4e5 + 10);
rep(i, N) {
ll a, b;
cin >> a >> b;
A[i] = a, B[i] = b;
if (uf.isSame(a, b))
lp[uf.findRoot(a)] = true;
else
uf.unite(a, b);
}
rep(i, 4e5 + 10) if (lp[i]) lp[uf.findRoot(i)] = true;
vector<bool> seen(4e5 + 10);
ll ans = 0;
rep(i, N) {
ll r = uf.findRoot(A[i]);
if (seen[r]) continue;
if (lp[r]) {
ans += uf.treeSize(r);
seen[r] = true;
} else {
ans += uf.treeSize(r) - 1;
seen[r] = true;
}
}
pt(ans);
return 0;
} |
#include <bits/stdc++.h>
#define int long long
#define double long double
#define ff first
#define ss second
#define endl '\n'
#define ii pair<int, int>
#define mp make_pair
#define mt make_tuple
#define DESYNC \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define vi vector<int>
#define vii vector<ii>
#define all(x) x.begin(), x.end()
#define EPS 1e-9
#define INF 1e18
#define ROOT 1
#define curtime chrono::steady_clock::now().time_since_epoch().count
#define rep(i, beg, n, s) for (int i = beg; i < n; i += s)
using ll = long long;
using namespace std;
const double PI = acos(-1);
const int M = 1000000007;
// const int M = 998244353;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int mod(int n, int m = M) {
int ret = n % m;
if (ret < 0) ret += m;
return ret;
}
int exp(int n, int k, int m = M) {
if (k == 0) return 1;
if (k == 1) return n;
int ax = exp(n, k / 2, m);
ax = mod(ax * ax, m);
if (k % 2) ax = mod(ax * n, m);
return ax;
}
int gcd(int a, int b) {
if (a == 0)
return b;
else
return gcd(b % a, a);
}
int solve(const vector<int> &v, const vector<ii> &b, const vector<int> &calc) {
int n = v.size();
int g[n][n];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int id = upper_bound(all(b), ii(v[i] + v[j] - 1, INF)) - b.begin();
id--;
if (id == -1)
g[i][j] = 0;
else
g[i][j] = calc[id];
}
}
vector<int> d(n, 0);
d[0] = 0;
for (int i = 1; i < n; i++) {
int sum = v[i];
for (int j = i - 1; j >= 0; j--) {
sum += v[j];
int id = upper_bound(all(b), ii(sum - 1, INF)) - b.begin();
id--;
int l = 0;
if (id != -1) l = calc[id];
d[i] = max(d[i], d[j] + l);
}
}
return d[n - 1];
}
//#define MULTIPLE_TEST_CASE
void solution() {
int n, m;
cin >> n >> m;
vector<ii> b(m);
vector<int> calc;
vi v(n);
int mx = -INF, mn = INF;
for (int &x : v) cin >> x, mx = max(mx, x);
for (ii &x : b) cin >> x.ss >> x.ff, mn = min(mn, x.ff);
int ans = INF;
if (mx > mn) {
cout << -1 << endl;
return;
}
sort(all(v));
sort(all(b));
for (ii x : b) {
if (calc.size() == 0)
calc.pb(x.ss);
else
calc.pb(max(calc.back(), x.ss));
}
do {
ans = min(ans, solve(v, b, calc));
} while (next_permutation(all(v)));
cout << ans << endl;
}
int32_t main() {
DESYNC;
int t = 1;
#ifdef MULTIPLE_TEST_CASE
cin >> t;
#endif
while (t--) solution();
}
| #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
template<class T>
struct graph{
struct edge{
int to;
T cost;
edge(int to, T cost) : to(to), cost(cost){}
bool operator<(const edge& r)const { return cost < r.cost; }
};
int V;
vector< vector< edge > > adj;
vector< T > dist;
vector< int > prev;
graph(){}
graph(int n = 0){
init(n);
}
void init(int size){
V = size;
adj.assign(size, vector<edge>());
}
void add_edge(int from, int to, T cost){
adj[from].push_back(edge(to, cost));
}
void Dijkstra(int from, T inf){
using P = pair<T, int>;
priority_queue< P, vector<P>, greater<P> > q;
dist.assign(V, inf);
prev.assign(V, -2);
dist[from] = 0;
prev[from] = -1;
q.push(P(0, from));
while(!q.empty()){
P p = q.top();
q.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(edge e: adj[v]){
if(dist[v] + e.cost < dist[e.to]){
dist[e.to] = dist[v] + e.cost;
prev[e.to] = v;
q.push(P(dist[e.to], e.to));
}
}
}
}
void get_path(int to, vector<int>& path){
path.clear();
if(prev[to] == -2) return;
while(prev[to] != -1){
path.push_back(to);
to = prev[to];
}
path.push_back(to);
reverse(path.begin(), path.end());
}
};
int main(void){
using P = pair<int, int>;
int n, m;
cin >> n >> m;
vector< int > w(n);
vector< P > vl;
for(int i=0;i<n;i++) cin >> w[i];
for(int i=0;i<m;i++){
int l, v;
cin >> l >> v;
vl.push_back(P(v, l));
}
{
int wmx = 0;
for(int i=0;i<n;i++) wmx = max(wmx, w[i]);
for(int i=0;i<m;i++){
if(vl[i].first < wmx){
puts("-1");
return 0;
}
}
}
vl.push_back(P(1e9, 0));
sort(vl.begin(), vl.end());
vector< int > lb(m+1, 0);
lb[0] = 0;
for(int i=1;i<=m;i++){
lb[i] = max(lb[i-1], vl[i-1].second);
}
auto f = [&](int v){
int ng = -1, ok = m;
while(ng + 1 < ok){
int mid = (ng + ok) / 2;
if(v <= vl[mid].first) ok = mid;
else ng = mid;
}
return lb[ok];
};
int ans = 1e9;
sort(w.begin(), w.end());
do{
graph<int> g(n);
for(int i=0;i<n;i++){
int c = w[i];
for(int j=i+1;j<n;j++){
c += w[j];
g.add_edge(i, j, -f(c));
}
}
g.Dijkstra(0, 1e9);
ans = min(ans, -g.dist[n-1]);
}while(next_permutation(w.begin(), w.end()));
cout << ans << endl;
}
|
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <unordered_map>
//#include"testlib.h"
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(s) s.rbegin(),s.rend()
#define RT(s) return cout<<s,0
#define sz(s) (int)(s.size())
//#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x)<<" = "<<x<<endl
#define mk(x, y) make_pair(x, y)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
void file() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#else
//freopen("gcd.in", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
}
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
}
const int N = 2e5 + 9;
vi adjL[N];
ll mem[N][10], cost[N];
int n;
ll solve(int u, int type) {
if (!sz(adjL[u]) && !type) return -1e10;
ll& ret = mem[u][type];
if (ret != -1) return ret;
ret = -1e10;
if (!type) {
for (auto& it : adjL[u]) {
ret = max(ret, solve(it, 1) - cost[u]);
ret = max(ret, solve(it, 0));
}
}
else {
ret = cost[u];
for (auto& it : adjL[u])
ret = max(ret, solve(it, 1));
}
return ret;
}
int main() {
file();
fast();
memset(mem, -1, sizeof mem);
int m; cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> cost[i];
while (m--) {
int x, y; cin >> x >> y;
adjL[x].push_back(y);
}
ll ans = LLONG_MIN;
for (int i = 1; i <= n; i++)
ans = max(ans, solve(i, 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;
vi to[MX];
vi a;
vi dp(MX,-1);
int dfs(int u){
if(dp[u]>-1) return max(a[u], dp[u]);
int res = -1;
for(int v: to[u]){
res = max(res,dfs(v));
}
dp[u] = res;
return max(a[u],res);
}
int main() {
int n,m; cin >> n >> m;
rep(i,n){
int ai; cin >> ai;
a.push_back(ai);
}
rep(i,m){
int x,y; cin >> x >> y;
--x,--y;
to[x].push_back(y);
}
rep(i,n) dfs(i);
int ans = -INF;
rep(i,n){
int tos = to[i].size();
if(tos == 0) continue;
if(dp[i] == -1) continue;
// printf("size: %d\n",(int)to[i].size());
// printf("ans: %d\n",dp[i]-a[i]);
ans = max(dp[i]-a[i],ans);
}
// cout << endl;
cout << ans << endl;
/*
rep(i,n){
cout << dp[i] << endl;
}
*/
return 0;
}
|
/*
Over Again
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()),((v).end())
#define allr(v) ((v).rbegin()),((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v,d) memset( v, d ,sizeof(v))
#define lcm(a,b) ((a*b)/(__gcd(a,b)))
#define PI acos(-1)
#define vec(a,b) ((b)-(a))
#define length(a) hypot( (a.imag()),(a.real()) )
#define normalize(a) (a)/(length(a))
#define point complex<double>
#define dp(a,b) (((conj(a))*(b)).real())
#define same(p1,p2) ( dp( vec(p1,p2),vec(p1,p2)) < eps )
#define rotate0( a,ang) ((a)*exp( point(0,ang) ))
#define rotateA(about,p,ang) (rotate0(vec(about,p),ang)+about)
typedef long long ll ;
typedef unsigned long long ull;
const double eps= (1e-8);
const double EPS = 1e-6;
using namespace std;
using namespace __gnu_pbds;
int dcmp(double a,double b){ return fabs(a-b)<=eps ? 0: (a>b)? 1:-1 ;}
int getBit(ll num, int idx) {return ((num >> idx) & 1ll) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(ll num, int idx) {return num ^ (1ll<<idx);}
void GO(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask) {mask &= (mask-1);++ret; }return ret;}
const ll N=2000+5,mod=1000000007,sh=201,inf=2000000000;
typedef tree<
pair<ll,ll> ,
null_type,
less<pair<ll,ll> >,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
double sx[N],sy[N],tx[N],ty[N];
double get_angle(int x,int y){
return atan2(y,x);
}
int get_dist(int x,int y){
return x*x+y*y;
}
double torad(double ang){
return (ang*PI/180.0);
}
int main(){
GO();
int T=1;
//cin>>T;
while(T--){
int n;
cin>>n;
vector< pair<double,pair<int,int> > > v2,v1;
int sumx=0,sumy=0;
for(int i=0;i<n;i++){
cin>>sx[i]>>sy[i];
sumx+=sx[i];
sumy+=sy[i];
sx[i]*=n;
sy[i]*=n;
}
for(int i=0;i<n;i++){
sx[i]-=sumx;
sy[i]-=sumy;
}
sumx=0;
sumy=0;
for(int i=0;i<n;i++){
cin>>tx[i]>>ty[i];
sumx+=tx[i];
sumy+=ty[i];
tx[i]*=n;
ty[i]*=n;
}
for(int i=0;i<n;i++){
tx[i]-=sumx;
ty[i]-=sumy;
}
for(int i=0;i<n;i++){
if(sx[i]!=0||sy[i]!=0){
swap(sx[i],sx[0]);
swap(sy[i],sy[0]);
}
}
bool ans=0;
for(int i=0;i<n;i++){
double d1= get_angle(sx[0],sy[0]);
double d2= get_angle(tx[i],ty[i]);
double dd= d2-d1;
map< pair<double,double> ,int> mb;
bool fok=1;
for(int j=0;j<n;j++){
double tsx= sx[j]*cos(dd)-sy[j]*sin(dd);
double tsy= sx[j]*sin(dd)+sy[j]*cos(dd);
bool ok=0;
for(int j=0;j<n&&ok==0;j++){
if(dcmp(tx[j],tsx)==0&&dcmp(ty[j],tsy)==0){
ok=1;
}
}
if(ok==0){
fok=0;
break;
}
// if(i==2) cout<<tsx<<" "<<tsy<<" "<<mb[mp(tsx,tsy)]<<endl;
}
if(fok){
ans=1;
break;
}
}
if(ans){
cout<<"Yes\n";
}
else{
cout<<"No\n";
}
}
}
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Erep(i,n) for(int i = 0; i <= (n); ++i)
#define repS(i,s,n) for(int i = s; i < (n); ++i)
#define ErepS(i,s,n) for(int i = s; i <= (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
#define RSort(a) sort(a.rbegin(), a.rend())
#define Output(a) printf("%lld %s", a, "\n")
typedef long long int ll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<string> vst;
typedef vector<double> vd;
const ll INF = 0x1fffffffffffffff;
const ll MOD = 1000000007;
const double PI = acos(-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; }
ll n;
vd a, b, c, d;
void input(){
cin >> n;
a = vd(n);
b = vd(n);
c = vd(n);
d = vd(n);
rep(i, n) cin >> a[i] >> b[i];
rep(i, n) cin >> c[i] >> d[i];
}
void solve(){
string ans = "No";
vd x(n), y(n);
for(int k = 0; k < 360; k++){
rep(i, n){
double tx = a[i], ty = b[i];
x[i] = tx * cos((k * PI ) / 180) - ty * sin((k * PI ) / 180);
y[i] = tx * sin((k * PI ) / 180) + ty * cos((k * PI ) / 180);
}
double ave_s_x = 0, ave_s_y = 0, ave_t_x = 0, ave_t_y = 0;
rep(i, n){
ave_s_x += x[i];
ave_s_y += y[i];
ave_t_x += c[i];
ave_t_y += d[i];
}
double diff_x = (ave_t_x - ave_s_x) / n;
double diff_y = (ave_t_y - ave_s_y) / n;
ll check = 1;
rep(i, n){
ll count = 0;
rep(j, n){
double ex = x[i] + diff_x, ey = y[i] + diff_y;
//if(k == 90)
//cout << ex << " " << c[j] << " " << ey << " " << d[j] << "\n";
if(abs(ex - c[j]) <= (double) 0.05 && abs(ey - d[j]) <= (double) 0.05){
//cout << k << endl;
//cout << x[i] << " " << c[j] << " " << y[i] << " " << d[j] << "\n";
count++;
}
}
if(count == 0){
check = 0;
}
}
if(check){
ans = "Yes";
break;
}
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
input();
solve();
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAX_N=1e6+10;
ll n,k;
ll dp[4][MAX_N*3];
ll sum[MAX_N*3];
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++) dp[1][i]=1;
for(int i=2;i<=3;i++){
sum[0]=0;
for(int j=1;j<=i*n;j++) sum[j]=sum[j-1]+dp[i-1][j];
for(int j=1;j<=i*n;j++) dp[i][j]=sum[j-1]-sum[max(0ll,j-n-1)];
}
int tmp=1;
while(k-dp[3][tmp]>0) k-=dp[3][tmp++];
ll ans1=1;
while(k-dp[2][tmp-ans1]>0) k-=dp[2][tmp-ans1++];
tmp-=ans1;
ll mn=max(1ll,tmp-n),ans2=mn+k-1,ans3=tmp-ans2;
printf("%lld %lld %lld\n",ans1,ans2,ans3);
return 0;
} | #line 1 "/Users/lynmisakura/kyopro/library_cp/template.cpp"
#include<bits/stdc++.h>
#line 3 "/Users/lynmisakura/kyopro/library_cp/template.cpp"
using namespace std;
#define REP(i,n) for(int i = 0;i < n;i++)
#define RNG(i,s,n) for(int i = s;i <= n;i++)
#define _RNG(i,e,s) for(int i = e;i >= s;i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define dup(x,y) (x + y - 1) / (y)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define lb(x,key) lower_bound(all(x) , (key))
#define ub(x,key) upper_bound(all(x) , (key))
#define _size(v) int((v).size())
template<class T> bool chmax(T& a,T b){ if(a < b){ a = b; return true; }else return false; }
template<class T> bool chmin(T& a,T b){ if(a > b){ a = b; return true; }else return false; }
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 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;
}
#define F first
#define S second
/////////////////////////////////////////////////////////////////////////
namespace cp{
void iob(){ cin.tie(0); ios::sync_with_stdio(false); }
}
#line 2 "main.cpp"
// =========================== DIJKSTRA's SHORTEST PATH ===============================
namespace cp{
using std::vector;
using std::pair;
template<class T> struct Dijkstra{
using P = pair<T,int>;
int N;
std::vector<std::vector<P>> graph;
Dijkstra(){}
Dijkstra(int N):N(N){graph.resize(N);}
void add_edge(int a,int b){ add_edge(a,b,1); }
void add_edge(int a,int b,T c){ graph[a].emplace_back(c,b); }
const T INF=std::numeric_limits<T>::max()/2;
vector<T> dist;
vector<int> prev,vis;
void solve(int s){
dist.assign(N,INF);
prev.assign(N,-1);
vis.assign(N,0);
dist[s]=0,vis[s]=1;
std::priority_queue<P,vector<P>,std::greater<P>> Q;
Q.push({0,s});
while(!Q.empty()){
auto[d,v]=Q.top();Q.pop();
vis[v]=1;
if(dist[v]<d)continue;
for(auto& [c,to] : graph[v]){
if(dist[to]>dist[v]+c){
dist[to]=dist[v]+c;
prev[to]=v;
Q.push({dist[to],to});
}
}
}
}
vector<int> get_path(int to){
vector<int> res;
while(to!=-1){ res.push_back(to); to=prev[to]; }
reverse(res.begin(),res.end());
return res;
}
bool reachable(int x){ return vis[x]; }
};
};
ll mindist[2020][2020];
int main(){
int n,m;cin >> n >> m;
REP(i,2020)REP(j,2020) mindist[i][j] = (1LL << 55);
cp::Dijkstra<ll> graph(2 * n);
REP(i,m){
int a,b;cin >> a >> b;
a--,b--;
ll c;cin >> c;
chmin(mindist[a][b],c);
graph.add_edge(a,b,c);
}
for(int i = n;i < 2 * n;i++){
for(int j = 0;j < n;j++){
if(mindist[i - n][j] == (1LL << 55))continue;
graph.add_edge(i,j,mindist[i - n][j]);
}
}
for(int i = n;i < 2 * n;i++){
graph.solve(i);
if(graph.reachable(i - n)){
cout << graph.dist[i-n] << '\n';
}else{
cout << -1 << '\n';
}
}
}
|
/*
皮卡丘冲鸭!
へ /|
/\7 ∠_/
/ │ / /
│ Z _,< / /`ヽ
│ ヽ / 〉
Y ` / /
イ● 、 ● ⊂⊃〈 /
() へ | \〈
>ー 、_ ィ │ //
/ へ / ノ<| \\
ヽ_ノ (_/ │//
7 |/
>―r ̄ ̄`ー―_
*/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define mk make_pair
#define pi pair<int, int>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i,a,b) for(int i=a;i>=(b);--i)
using namespace std;
int dt[][2]= {{1,0},{-1,0},{0,1},{0,-1},{0,0}};
//typedef pair<int, int> P;
//priority_queue<int, vector<int>, greater<int> > q;
int a[110][110];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
IOS;
int h,w;
cin >> h >> w;
int mi = INF;
rep(i,1,h)
{
rep(j,1,w)
{
cin >> a[i][j];
mi = min(mi,a[i][j]);
}
}
ll ans = 0;
rep(i,1,h)
{
rep(j,1,w)
{
ans += a[i][j] - mi;
}
}
cout << ans;
}
| /**
* author: shu8Cream
* created: 13.05.2021 00:15:07
**/
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using ll = long long;
using P = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll t,n;
cin >> t >> n;
cout << ((100+t)*n-1)/t << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define inf 1e15
#define ll long long int
void solve(ll n, ll k){
vector<ll>a(n);
vector<ll>mp(n,0);
for(int i=0;i<n;++i){
cin>>a[i];
++mp[a[i]];
}
ll i=0;
ll res = 0;
while(i<n){
if(mp[i]==0){
break;
}
if(mp[i]<k){
res += (k-mp[i])*i;
k = mp[i];
}
++i;
}
res += k*i;
cout<<res;
}
int main() {
int n,k;
cin>>n>>k;
solve(n,k);
} | #include<bits/stdc++.h>
using namespace std;
typedef vector <int> vi;
typedef pair< int ,int > pii;
#define endl "\n"
#define sd(val) scanf("%d",&val)
#define ss(val) scanf("%s",&val)
#define sl(val) scanf("%lld",&val)
#define debug(val) printf("check%d\n",val)
#define all(v) v.begin(),v.end()
#define PB push_back
#define MP make_pair
#define FF first
#define SS second
#define ll long long
#define MOD 1000000007
#define clr(val) memset(val,0,sizeof(val))
#define what_is(x) cerr << #x << " is " << x << endl;
#define OJ \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int main()
{
int N;
int x;
cin >> N;
int a[N], b[N];
for (int i = 0; i < N; i++){
cin >> a[i];
}
for (int i = 0; i < N; i++){
cin >> b[i];
}
int* a_max = max_element(a, a + N);
int* b_min = min_element(b, b + N);
x = *b_min - *a_max;
if (x < 0){
cout << 0;
}else{
cout << x + 1<< endl;
}
return 0;
} |
/* Aditya0412 */
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define vii vector < int >
#define pii pair < int , int >
#define fi first
#define all(a) (a).begin(),(a).end()
#define si(x) (int)((x).size())
#define debug(x) cout<<#x<<" is "<<(x)<<endl;cout.flush();
#define se second
#define endl '\n'
#define f(i,a,b) for(int i=a;i<b;i++)
#define mem(a,x) memset(a,x,sizeof(a))
#define fastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=998244353;
// const int mod = 1000000000+7;
const int N=1000000+6;
#define M_PI 3.14159265358979323846
// In C++, comparator should return false if its arguments are equal.
// === Debug macro starts here ===
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
#else
#define dbg(x)
#endif
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value, Ostream&>::type operator<<(Ostream& os, const Cont& v){
os<<"[";
for(auto& x:v){os<<x<<", ";}
return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os, const pair<Ts...>& p){
return os<<"{"<<p.first<<", "<<p.second<<"}";
}
// === Debug macro ends here ===
int inv[3000000+5],fac_inv[3000000+5],fac[3000000+5];
void initialize()
{
int i;
inv[1]=1;
for(i=2;i<=3000000;i++)
inv[i]=(mod-mod/i)*inv[mod%i]%mod;
fac[0]=fac[1]=1;
for(i=2;i<=3000000;i++)
fac[i]=i*fac[i-1]%mod;
fac_inv[0]=fac_inv[1]=1;
for(i=2;i<=3000000;i++)
fac_inv[i]=inv[i]*fac_inv[i-1]%mod;
}
int ncr(int n,int r)
{
if(n<r) return 0;
return (fac[n]*fac_inv[r]%mod)*fac_inv[n-r]%mod;
}
int spf[N];
void smallest_prime()
{
mem(spf,0);
for(int i=2;i<N;i++)
{
if(spf[i]==0)
{
spf[i]=i;
for(int j=i*i;j<N;j+=i)
if(spf[j]==0)
spf[j]=i;
}
}
}
int n,m;
int32_t main()
{
// _ _| | _ _ _
fastIO;smallest_prime();initialize();
cin>>n>>m;int ans=0;
for(int i=1;i<=m;i++){
int t=1;
int x=i;
while(x!=1)
{
int y=spf[x],z=0;
while(x%y==0)
{
x/=y;z++;
}
t*=ncr(z+n-1,n-1);
t%=mod;
}
ans+=t;ans%=mod;
}
cout<<ans;
} | #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<functional>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 10000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
bool isprime(int p) {
if (p == 1) return false;
for (int i = 2; i < p; i++) {
if (p % i == 0) return false;
}
return true;
}
ll gcd(ll a, ll b) {
if (a < b)swap(a, b);
if (a % b == 0)return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll keta(ll n) {
ll res = 0;
while (n >= 1) {
res += n % 10; n /= 10;
}
return res;
}
ll modpow(ll x, ll y) {
ll res = 1;
while (y) {
if (y % 2) { res *= x; res %= mod; }
x = x * x % mod; y /= 2;
}
return res;
}
ll nCk(ll n, ll k) {
ll a = 1, b = 1;
for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
return a * modpow(b, mod - 2) % mod;
}
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
struct edge { ll to, cost; };
struct status {
ll y;
ll high;
ll cost;
bool operator<(const status& rhs) const { return cost < rhs.cost; };
bool operator>(const status& rhs) const { return cost > rhs.cost; };
};
ll a[123456], w1[1234567], w2[1234567];
signed main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
//cout << fixed << setprecision(15);
ll n, x; cin >> n >> x;
rep(i, n) {
cin >> a[i];
}
for (int i = 0; i < (1 << (n / 2)); i++) {
ll sum = 0;
for (int j = 0; j < (n / 2); j++) {
if ((i / (1 << j)) % 2 == 0) sum += a[j];
}
w1[i] = sum;
}
// 後半 N - N/2 個を bit 全探索で全列挙する
for (int i = 0; i < (1 << (n - (n / 2))); i++) {
ll sum = 0;
for (int j = 0; j < n - (n / 2); j++) {
if ((i / (1 << j)) % 2 == 0) sum += a[j + (n / 2)];
}
w2[i] = sum;
}
// ソートしたうえで半分全列挙をする
ll ans = 0;
sort(w1, w1 + (1 << (n / 2)));
sort(w2, w2 + (1 << (n - (n / 2))));
for (int i = 0; i < (1 << (n / 2)); i++) {
ll rem = x - w1[i];
ll pos1 = upper_bound(w2, w2 + (1 << (n - (n / 2))), rem) - w2;
if (pos1 == 0) { continue; }
pos1--;
ans = max(ans, w1[i] + w2[pos1]);
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int n;
cin>>n;
string s;
cin>>s;
vector<int> min_rem(n+1, n);
min_rem[n]=0;
vector<int> ending_with_char(26, -1);
for(int i=n-1;i>=0;i--){
if(i==n-1){
min_rem[i]=-1;
ending_with_char[(int)(s.at(i)-'a')]=1;
}
else{
min_rem[i]=n;
for(int ch=0;ch<26;ch++){
if(ch!=(int)(s.at(i)-'a')){
if(ending_with_char[ch]!=-1){
min_rem[i]=min(min_rem[i], ending_with_char[ch]);
}
}
}
min_rem[i]=(min_rem[i]==n?-1:min_rem[i]);
if(min_rem[i+1]!=-1){
ending_with_char[(int)(s.at(i)-'a')]=min(ending_with_char[(int)(s.at(i)-'a')]==-1?n:ending_with_char[(int)(s.at(i)-'a')], min_rem[i+1]+1);
}
}
}
cout<<min_rem[0]<<"\n";
}
| #include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define F first
#define S second
#define pss pair<string,string>
#define pcc pair<char,char>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define piii pair<int,pii>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define vs vector<string>
#define vl vector<ll>
#define vs vector<string>
#define vll vector<pll>
#define vss vector<pss>
#define vcc vector<pcc>
#define MP make_pair
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=n-1;i>=0;i--)
#define Very_fast std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const int N = 1e5+5;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
inline ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll lowbit(ll x){return (x&-x);}
inline ll kissme(ll x,ll y)
{
ll res=1;
if(x==0)return 1;
if(x==1)return y;
res=kissme(x/2,y);
res=res*res%mod;
if(x%2==1)res=res*y%mod;
return res;
}
int T,n,d[N],p[N];
string s;
signed main()
{
Very_fast;
cin>>n;
cin>>s;
if(s[0]!=s[s.size()-1])cout<<"1\n";
else{
for(int i=0;i<s.size()-1;i++)
if(s[i]!=s[0]&&s[i+1]!=s[s.size()-1])
{
cout<<"2\n";
return 0;
}
cout<<"-1\n";
}
return 0;
} |
#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
preprocess() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} ____;
#pragma endregion head
#pragma region library
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;
}
bool operator==(const mint a) const {
return x == a.x;
}
bool operator!=(const mint a) const {
return x != a.x;
}
friend ostream& operator<<(ostream& os, const mint& value) {
os << value.x;
return os;
}
friend istream& operator>>(istream& is, mint& value) {
ll t;
is >> t;
value = mint(t);
return is;
}
};
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
#pragma endregion library
int main() {
int n;
cin >> n;
vector<char> c(4);
rep(i, 4) cin >> c[i];
if (n <= 3 || (c[0] == 'A' && c[1] == 'A') || (c[1] == 'B' && c[3] == 'B')) {
cout << 1 << '\n';
return 0;
} else {
if (c[1] != c[2]) {
mint two = 2;
cout << two.pow(n - 3) << '\n';
} else {
mint ans = 0;
combination cb(2000);
rep(i, n) {
int cnta = n - 3 - i;
if (cnta < 0) break;
ans += cb(cnta + 1, i);
}
cout << ans << '\n';
}
}
}
| #include <bits/stdc++.h>
using namespace std;
const int MAX_LEN = 50;
string DIR[5] = {"U", "D", "L", "R", ""};
int dir2int(char c) {
if ( c == 'U' ) { return 0; }
if ( c == 'D' ) { return 1; }
if ( c == 'L' ) { return 2; }
if ( c == 'R' ) { return 3; }
return 4;
}
void move(int* i, int* j, int dir) {
if ( dir == 0 && *i > 0 ) { *i -= 1; }
if ( dir == 1 && *i < MAX_LEN-1 ) { *i += 1; }
if ( dir == 2 && *j > 0 ) { *j -= 1; }
if ( dir == 3 && *j < MAX_LEN-1 ) { *j += 1; }
}
int si, sj;
int t[MAX_LEN][MAX_LEN], p[MAX_LEN][MAX_LEN];
const double TIME_LIMIT = 1.3;
double now_time(clock_t start, clock_t now) {
return ((double)(now - start) / CLOCKS_PER_SEC);
}
int next_dir(int i, int j, bool visited[]) {
int ret = 4;
int next_p = -1;
if ( i > 0 && (! visited[t[i-1][j]]) && p[i-1][j] > next_p ) { ret = 0; next_p = p[i-1][j]; }
if ( i < MAX_LEN-1 && (! visited[t[i+1][j]]) && p[i+1][j] > next_p ) { ret = 1; next_p = p[i+1][j]; }
if ( j > 0 && (! visited[t[i][j-1]]) && p[i][j-1] > next_p ) { ret = 2; next_p = p[i][j-1]; }
if ( j < MAX_LEN-1 && (! visited[t[i][j+1]]) && p[i][j+1] > next_p ) { ret = 3; next_p = p[i][j+1]; }
return ret;
}
int next_dir_2(int i, int j, bool visited[]) {
int ret = 4;
int next_p = -1;
if ( i > 0 && (! visited[t[i-1][j]]) && p[i-1][j] > next_p ) { ret = 0; next_p = p[i-1][j]; }
if ( i < MAX_LEN-1 && (! visited[t[i+1][j]]) && p[i+1][j] > next_p ) { ret = 1; next_p = p[i+1][j]; }
if ( j > 0 && (! visited[t[i][j-1]]) && p[i][j-1] > next_p ) { ret = 2; next_p = p[i][j-1]; }
if ( j < MAX_LEN-1 && (! visited[t[i][j+1]]) && p[i][j+1] > next_p ) { ret = 3; next_p = p[i][j+1]; }
return ret;
}
struct STATE {
string route;
};
void init(STATE& state) {
state.route = "";
int i = si, j = sj;
bool visited[2500] = {};
visited[t[i][j]] = true;
// 貪欲
while ( true ) {
int next = next_dir(i, j, visited);
if ( next == 4 ) { break; }
state.route += DIR[next];
move(&i, &j, next);
visited[t[i][j]] = true;
}
}
void modify(STATE& state) {
string mod_route = "";
int i = si;
int j = sj;
bool visited[2500] = {};
visited[t[i][j]] = true;
int use_len = rand() % state.route.size();
for ( int k = 0; k < use_len; k++ ) {
mod_route += state.route[k];
move(&i, &j, dir2int(state.route[k]));
visited[t[i][j]] = true;
}
// randに変更
int r = rand() % 4;
mod_route += DIR[r];
move(&i, &j, r);
visited[t[i][j]] = true;
// それ以降貪欲
while ( true ) {
int next = next_dir(i, j, visited);
if ( next == 4 ) { break; }
mod_route += DIR[next];
move(&i, &j, next);
visited[t[i][j]] = true;
}
state.route = mod_route;
}
int calc_score(STATE& state) {
int point = p[si][sj];
int i = si;
int j = sj;
bool visited[2500] = {};
visited[t[i][j]] = true;
for ( int k = 0; k < state.route.size(); k++ ) {
move(&i, &j, dir2int(state.route[k]));
point += p[i][j];
if ( visited[t[i][j]] ) { return 0; }
visited[t[i][j]] = true;
}
return point;
}
void mountain(STATE& state) {
init(state);
clock_t start = clock();
while ( true ) {
clock_t now = clock();
if ( now_time(start, now) > TIME_LIMIT ) { break; }
STATE new_state = state;
modify(new_state);
int new_score = calc_score(new_state);
int pre_score = calc_score(state);
if ( new_score > pre_score ) {
state = new_state;
}
}
}
int main() {
srand(time(NULL));
cin >> si >> sj;
for ( int i = 0; i < MAX_LEN; i++ ) {
for ( int j = 0; j < MAX_LEN; j++ ) {
cin >> t[i][j];
}
}
for ( int i = 0; i < MAX_LEN; i++ ) {
for ( int j = 0; j < MAX_LEN; j++ ) {
cin >> p[i][j];
}
}
STATE ans;
mountain(ans);
cout << ans.route << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
#define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)),end(v))
#define CHMIN(x,y) x = min(x, y)
#define CHMAX(x,y) x = max(x, y)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl
const int MAX = 20;
int N, M;
vii X[MAX];
ll dp[1<<MAX];
int main() {
cout << fixed << setprecision(15);
cin >> N >> M;
REP (i, M) {
int x, y, z;
cin >> x >> y >> z;
X[x].push_back(make_pair(y, z));
}
vector<bool> visit(1<<N, false);
queue<int> q;
q.push(0);
dp[0] = 1;
while (q.size()) {
int state = q.front(); q.pop();
int b = __builtin_popcount(state) + 1;
REP (i, N) if ((state & (1<<i)) == 0) {
int next = state | (1<<i);
bool check = true;
FORE (x, X[b]) {
if (__builtin_popcount(next&((1<<(x.first))-1)) > x.second) check = false;
}
if (check) {
dp[next] += dp[state];
if (!visit[next]) {
q.push(next);
visit[next] = true;
}
}
}
}
cout << dp[(1<<N)-1] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
ll n, m;
vi x(100), y(100), z(100);
ll cntmask(ll n) {
ll ans = 0;
while (n) {
ans += n%2;
n /= 2;
}
return ans;
}
bool checkrule(ll mask, ll rule) {
ll cnt = 0;
for (ll i = 0; i < y[rule]; ++i)
if (mask & (1<<i))
++cnt;
return cnt <= z[rule];
}
bool check(ll mask) {
ll elems = cntmask(mask);
for (ll i = 0; i < m; ++i) {
if (x[i] != elems) continue;
if (not checkrule(mask, i)) return false;
}
return true;
}
ll solve() {
vi memo(1<<n);
memo[0] = 1;
queue<ll> q;
q.push(0);
while (not q.empty()) {
ll mask = q.front();
q.pop();
for (ll i = 0; i < n; ++i) {
if (mask & (1<<i)) continue;
if (check(mask + (1<<i))) {
memo[mask + (1<<i)] += memo[mask];
if (memo[mask + (1<<i)] == memo[mask])
q.push(mask + (1<<i));
}
}
}
return memo[(1<<n) - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
for (ll i = 0; i < m; ++i)
cin >> x[i] >> y[i] >> z[i];
cout << solve() << '\n';
return 0;
} |
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define st first
#define nd second
#define pii pair<int,int>
#define mp make_pair
#define pll pair<long long,long long>
using namespace std;
const int nax = 205;
vector<pii> parki;
int dp[nax];
int n;
void solve(){
cin >> n;
for(int i=1;i<=n;i++){
int x, y; cin>>x>>y;
parki.pb(mp(x, y));
}
dp[0] = 0;
for(int i=1;i<=2*n;i++){
dp[i] = 1e9;
if(i & 1) continue;
for(int lo=1;lo<i;lo+=2){
int hi = i;
bool ok = true;
set<int> xd;
int len = (hi - lo + 1);
for(pii cur : parki){
if(cur.st != - 1){
if(cur.st >= lo && cur.st < lo + len / 2){
if(xd.count(cur.st)) ok = false;
xd.insert(cur.st);
if(cur.nd != -1){
if(cur.nd != cur.st + len / 2) ok = false;
}
}
}
else if(cur.nd != -1){
// cur.st = -1
if(cur.nd >= lo + len / 2 && cur.nd <= hi){
if(xd.count(cur.nd - len / 2)) ok = false;
xd.insert(cur.nd - len / 2);
}
}
}
if(ok){
dp[i] = min(dp[i], dp[lo - 1] + (int)(len / 2 - xd.size()));
}
}
}
int cnt = 0;
for(pii cur : parki){
if(cur == mp(-1, -1)) cnt++;
}
if(dp[n * 2] == cnt) cout << "Yes" << "\n";
else cout << "No" << "\n";
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int tt = 1;
// cin >> tt;
while(tt--) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define per(i, b) per2(i, 0, b)
#define per2(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define ALL(c) (c).begin(), (c).end()
#define SZ(x) ((int)(x).size())
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T, class U>
void chmin(T& t, const U& u) {
if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
if (t < u) t = u;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
os << "{";
rep(i, v.size()) {
if (i) os << ",";
os << v[i];
}
os << "}";
return os;
}
#ifdef LOCAL
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
int main() {
int N;
cin >> N;
V<int> A(N), B(N);
int sz = N * 2;
V<int> tp(sz), com(sz, -1);
bool ng = false;
rep(i, N) {
cin >> A[i] >> B[i];
if (A[i] != -1) {
--A[i];
if (tp[A[i]]) ng = true;
tp[A[i]] = i + 1;
}
if (B[i] != -1) {
--B[i];
if (tp[B[i]]) ng = true;
tp[B[i]] = -(i + 1);
}
if (A[i] != -1 && B[i] != -1) {
com[A[i]] = i;
com[B[i]] = i;
}
}
if (ng) {
puts("No");
return 0;
}
V<bool> dp(sz + 1);
dp[0] = true;
rep(i, sz) {
if (!dp[i]) continue;
for (int j = i + 1; j < sz; ++j) {
int w = j - i + 1;
if (w & 1) continue;
w /= 2;
bool ok = true;
rep(k, w) {
int p = i + k, q = i + k + w;
if(com[p] != com[q])ok = false;
if (tp[p] != 0 && tp[q] != 0) {
if (tp[p] < 0 || tp[p] + tp[q] != 0) {
ok = false;
}
}
if (tp[p] < 0 || tp[q] > 0) {
ok = false;
}
}
if (ok) {
dp[j + 1] = true;
}
}
}
puts(dp[sz] ? "Yes" : "No");
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define f(i,a,n) for(ll i=a;i<n;i++)
#define w(x) ll _t;cin>>_t;f(x,1,_t+1)
#define br cout<<"\n"
#define N 10000000
ll md=1e9+7,n;
ll h,w,m;
//l,d,r,u
ll x[]={0,1,0,-1};
ll y[]={1,0,-1,0};
ll mat[1501][1501];
bool isSafe(ll i,ll j){
return i>=0 && i<h && j>=0 && j<w;
}
map<pair<ll,ll>,vector<ll>> mp;
void dfs(ll i,ll j,ll cur_dir){
if(!isSafe(i,j)) return ;
if(mat[i][j]==2) return ;
if(mat[i][j]==-1){
if(cur_dir ==0 || cur_dir ==2) mp[{i,j}][0]=mp[{i,j}][2]=0;
if(cur_dir ==1 || cur_dir ==3) mp[{i,j}][1]=mp[{i,j}][3]=0;
}
else
mat[i][j]=1;
dfs(i+x[cur_dir],j+y[cur_dir],cur_dir);
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);
//w(_x)
{
ll ans=0;
cin>>h>>w>>n>>m;
vector<pair<ll,ll>> bulb;
ll bi,bj;
f(i,0,h) f(j,0,w) mat[i][j]=0;
f(ti,0,n){
cin>>bi>>bj;
mat[bi-1][bj-1]=-1;
bulb.push_back({bi-1,bj-1});
mp[{bi-1,bj-1}].push_back(1);
mp[{bi-1,bj-1}].push_back(1);
mp[{bi-1,bj-1}].push_back(1);
mp[{bi-1,bj-1}].push_back(1);
}
f(i,0,m){
cin>>bi>>bj;
mat[bi-1][bj-1]=2;
}
f(i,0,n){
bi=bulb[i].first;
bj=bulb[i].second;
if(mp[{bi,bj}][0]) dfs(bulb[i].first+x[0],bulb[i].second+y[0],0);
if(mp[{bi,bj}][1]) dfs(bulb[i].first+x[1],bulb[i].second+y[1],1);
if(mp[{bi,bj}][2]) dfs(bulb[i].first+x[2],bulb[i].second+y[2],2);
if(mp[{bi,bj}][3]) dfs(bulb[i].first+x[3],bulb[i].second+y[3],3);
}
f(i,0,h) f(j,0,w) if(mat[i][j]==1 || mat[i][j]==-1) ans++;
cout<<ans;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)((n)-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}
template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
int l = vv.size();
if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n " ) << vv[i] << (i==l-1 ? " ]" : ""); }
return o;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& p){
return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) {
freopen(filename.c_str(), "r", stdin);
}
int main(){
ios_base::sync_with_stdio(0);
// finput("./input");
int t; cin >> t;
while(t--){
string s; cin >> s;
int ans = -1;
if("atcoder" < s){
ans = 0;
}else{
rep(j,s.size()){
if('a' < s[j]){
if('t' < s[j]){
ans = j-1;
}else{
ans = j;
}
break;
}
}
}
cout << ans << endl;
}
return 0;
} |
#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair
typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;
const int N = 505;
const int mod = 998244353;
string s, t;
int main() {
int n,m;
S2(n,m);
int ways = 1;
rep(i,0,n+m-1) t += ".";
rep(i,0,n) {
cin >> s;
rep(j,0,m) {
if(s[j] == '.') continue;
if(s[j] == 'B') {
if(t[i + j] == 'R') ways = 0;
t[i + j] = 'B';
}
if(s[j] == 'R') {
if(t[i + j] == 'B') ways = 0;
t[i + j] = 'R';
}
}
}
rep(i,0,n+m-1) if(t[i] == '.') {
ways = ways * 2;
if(ways >= mod) {
ways -= mod;
}
}
P(ways);
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define all(c) (c).begin(),(c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(),(c).rend()
#define fr(x,in,n,r) for(ll x=in;x<n;x+=r)
#define ifr(x,n) for(ll x=0;x<n;x++)
#define dfr(x,n) for(ll x=n-1;x>=0;x--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define pof(a) pop_front(a)
#define pob(a) pop_back(a)
#define eb(a) emplace_back(a)
#define ef(a) emplace_front(a)
#define fstm(m,n,r) m.reserve(n);m.max_load_factor(r)
#define os tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef map<ll,ll> mll;
typedef map<string,ll> msll;
typedef unordered_map<ll,ll> umap;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef long double ld;
#define mod 1000000007
#define N 200001
vector<pll> v[N];ll a[N];
bool vis[N];
void dfs(ll u){
vis[u]=1;
ifr(i,v[u].size()){
if(!vis[v[u][i].ff]){
a[v[u][i].ff]=a[u]^v[u][i].ss;
dfs(v[u][i].ff);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,x,y,w;
cin>>n;
ifr(i,n-1){
cin>>x>>y>>w;
v[x].pb(pll(y,w));
v[y].pb(pll(x,w));
}
a[1]=0;
dfs(1);
ll ans=0;
ifr(i,60){
ll cnt =0,c=0,r=1ll<<i;
fr(j,1,n+1,1){
if(r&(a[j]))cnt++;
else c++;
}
ans =(ans + ((r)%mod*(cnt*c)%mod)%mod )%mod;
}
cout<<ans<<endl;
return 0;
} |
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <cassert>
#include <memory>
#include <random>
#include <functional>
#include <complex>
#include <immintrin.h>
#ifdef DEBUG
#include "./Lib/debug_VC.hpp"
#include "./Lib/Timer.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif
/* macro */
#define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x,c) for(const auto& x:(c))
#define REPI2(it,b,e) for(auto it = (b); it != (e); ++it)
#define REPI(it,c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(),(x).end()
#define cauto const auto&
/* macro func */
template<class T>
inline auto sort(T& t) { std::sort(ALL(t)); }
template<class T>
inline auto rsort(T& t) { std::sort((t).rbegin(), (t).rend()); }
template<class T>
inline auto unique(T& t) { (t).erase(unique((t).begin(), (t).end()), (t).end()); }
template<class T, class S>
inline auto chmax(T& t, const S& s) { if (s > t) { t = s; return true; } return false; }
template<class T, class S>
inline auto chmin(T& t, const S& s) { if (s < t) { t = s; return true; } return false; }
inline auto BR() { std::cout << "\n"; }
/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template<class T>
using V = std::vector<T>;
/* using std */
using std::cout;
constexpr char endl = '\n';
using std::cin;
using std::pair;
using std::string;
using std::stack;
using std::queue;
using std::vector;
using std::list;
using std::map;
using std::unordered_map;
using std::multimap;
using std::unordered_multimap;
using std::set;
using std::unordered_set;
using std::unordered_multiset;
using std::multiset;
using std::bitset;
using std::priority_queue;
/* Initial processing */
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;
/* Remove the source of the bug */
signed pow(signed, signed) { assert(false); return -1; }
/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> { public: size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } };
}
/* input */
template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for (T& x : vec) is >> x; return is; }
/* constant value */
//constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
//=============================================================================================;
auto d(ll n, ll p) {
while (n > 0) {
ll k = n % p;
if (k == 7) { return false; }
n /= p;
}
return true;
}
signed main() {
ll n;
cin >> n;
ll ans = 0;
FOR(i, 1, n + 1) {
ans += (d(i, 10) && d(i, 8));
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
#define MOD(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forr(i,a,b) for (int i = a; i <= b; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
vvi res;
vvi comb (vvi &a, vvi &b) {
vvi res;
forn (i, a.size()) {
vi v;
for (int x : a[i]) v.pb(x);
for (int x : b[i]) v.pb(x);
res.pb(v);
}
return res;
}
void go (int u) {
if (!u) return;
go(u - 1);
vvi a = res, b = res;
res = {{}};
forn (i, (1 << u - 1)) res[0].pb(1);
forn (i, (1 << u - 1)) res[0].pb(0);
for (vi v : comb(a, b))
res.pb(v);
for (vi &v : b)
for (int &x : v)
x ^= 1;
for (vi v : comb(a, b))
res.pb(v);
}
int main () {
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
go(n);
cout << res.size() << endl;
for (vi &v : res) {
for (int x : v)
cout << "AB"[x];
cout << endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
int t = s.size();
int flag=0;
for(int i=0;i<t;i++){
if(i%2==0){
if((s[i]-'Z')<=0){
flag++;
}
}
else{
if((s[i]-'Z')>0){
flag++;
}
}
}
if(flag>0){
cout << "No" << endl;
}
else{
cout << "Yes" << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
for (unsigned int i{};i<s.length()/2;i++){
char temp = s.at(i);
s.at(i) = s.at(s.length() - i - 1);
s.at(s.length() - i - 1) = temp;
}
for (unsigned int i{};i<s.length();i++){
if (s.at(i) == '9'){
s.at(i) = '6';
}else if(s.at(i) == '6'){
s.at(i) = '9';
}
}
cout<<s<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin>>N;
if(N%2==1){
cout<<"Black"<<endl;
}else{
cout<<"White"<<endl;
}
} | #include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 20000;
int Max[MAXN<<2];
int S[MAXN];
void build(int l,int r,int rt) {
if(l == r) {
Max[rt] = S[l];
return ;
}
int m = (l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
Max[rt] = max(Max[rt<<1],Max[rt<<1|1]);
}
int query(int L,int R,int l,int r,int rt) {
if(l>=L && r<=R) {
return Max[rt];
}
int m = (l+r)>>1;
int Max = -1;
if(L<=m) Max = max(Max,query(L,R,l,m,rt<<1));
if(R > m) Max = max(Max,query(L,R,m+1,r,rt<<1|1));
return Max;
}
int main() {
int n;scanf("%d",&n);
if(n%2==0) {
cout<<"White"<<endl;
}else {
cout<<"Black"<<endl;
}
return 0;
}
|
// Qg3
// //g++ -std=c++17 -O2 -Wall a.cpp -o test
// _()_
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vll vector<ll>
#define vi vector<int>
#define vb vector<bool>
#define pi pair<int,int>
#define pll pair<ll,ll>
#define vp vector<pi>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define For(i,a,b) for(ll i=a;i<b;i++)
//#define endl "\n"
#define debug2(x,y) cout<<"This side ----> "<<#x<<" -> "<<x<<" | "<<#y<<" -> "<<y<<endl;
#define debug(x) cout<<"This side ----> "<<#x<<" -> "<<x<<endl
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define mint map<int,int>
#define mall map<ll,ll>
#define ciN cin
#define gu(a,s) get<a>(s)
#define tin tuple<ll,ll,ll>
#define ter(x,y,z) ((x)?y:z)
#define ul ll
/////////////
const ll N = 1e6 + 3;
const ll maxn = 2e5 + 5;
const ll max_val = 5e4 + 10;
ll mod = 1e9 + 7;
const ll bits = 20;
ll caseNumber = 1;
////////////////////////////////////////////////////////////////
vi adj[maxn];
vb vis(maxn, 0);
ll cnt = 0;
void dfs(int st) {
vis[st] = 1;
cnt++;
for (auto x : adj[st]) {
if (!vis[x])dfs(x);
}
}
void jabru() {
ll n; cin >> n;
vll v(n);
for (int i = 0; i < n; i++)cin >> v[i];
for (int i = 0; i < n / 2; i++) {
if (v[i] != v[n - i - 1]) {
adj[v[i]].pb(v[n - i - 1]);
adj[v[n - i - 1]].pb(v[i]);
}
}
ll ans = 0;
for (int i = 1; i < maxn; i++) {
if (!vis[i]) {
cnt = 0;
dfs(i);
ans += cnt - 1;
}
}
cout << ans << endl;
}
bool TestCase1 = 0;
bool isGoogles = 0;
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
ll t;
t = 1;
if (TestCase1) {
cin >> t;
}
while (t--) {
if (isGoogles) {cout << "Case #" << caseNumber << ": ";} caseNumber++;
jabru();
}
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>
using namespace std;
int N, A[200000], ufds[200000], rnk[200000];
map<int,int> m;
int findSet(int i) {
return ufds[i] == i ? i : ufds[i] = findSet(ufds[i]);
}
void unionSet(int i, int j) {
int a = findSet(i), b = findSet(j);
if (a == b) return;
if (rnk[a] < rnk[b]) ufds[a] = b;
if (rnk[a] > rnk[b]) ufds[b] = a;
if (rnk[a] == rnk[b]) ufds[a] = b, rnk[b]++;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i) scanf("%d", &A[i]);
for (int i = 0; i < N; ++i) ufds[i] = i, rnk[i] = 1;
for (int i = 0; i < N; ++i) {
if (m[A[i]] != 0) unionSet(i,m[A[i]]-1);
m[A[i]] = i+1;
}
int ans = 0;
for (int i = 0; i*2 < N; ++i) if (findSet(i) != findSet(N-i-1)) {
ans++;
unionSet(i,N-i-1);
}
printf("%d", ans);
return 0;
}
|
#include<bits/stdc++.h>
#ifdef BIZON
#define rr(...) [](const auto&...x){ char c='='; cerr << boolalpha << "\e[1;38;5;68m" << #__VA_ARGS__ << " "; ((cerr<<"\e[0;38;5;61m"<<exchange(c,',')<<"\e[0m "<<x),...); cerr<<endl; }(__VA_ARGS__);
#else
#define rr(...) 0;
#define endl '\n'
#endif
#define ALL(c) begin(c), end(c)
#define II(...) __VA_ARGS__; [](auto&...x){(cin>>...>>x);}(__VA_ARGS__);
#define ii(...) int II(__VA_ARGS__)
inline bool umin(auto&x, const auto&y){ return y<x ? x=y, 1 : 0; }
inline bool umax(auto&x, const auto&y){ return y>x ? x=y, 1 : 0; }
using namespace std;
using ll = long long;
using ld = long double;
ll f(int t, ll n) {
ld m = (ld(100)+t) / 100;
ll l = 1, r = 1e16;
while(l<r) {
ll d = (l+r) / 2;
ll a = (t+100) * d / 100;
assert(a>=d);
if(a-d < n) l = d+1; else r = d;
}
ll ans = (t+100) * r / 100;
return ans - 1;
}
int main(){
if(auto f="in.txt"; fopen(f,"r") && freopen(f,"r",stdin));
ios::sync_with_stdio(0);cin.tie(0);
rr(f(3,1))
rr(f(3,2))
rr(f(3,3))
rr(f(3,4))
rr(f(3,5))
ii(t,n)
cout<<f(t,n)<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
struct UnionFind {
private:
vector<int> par;
vector<int> count;
vector<int> rank;
public:
UnionFind(int N) {
count.assign(N, 1);
rank.assign(N, 0);
par.assign(N, 0);
rep(i, N) par[i] = i;
}
int root(int x) {
if (par[x] == x)
return x;
else {
par[x] = root(par[x]);
return par[x];
}
}
void unite(int x, int y) {
x = root(x), y = root(y);
if (x == y) return;
if (rank[x] < rank[y])
swap(x, y);
else if (rank[x] == rank[y])
rank[y]++;
par[y] = x;
count[x] += count[y];
return;
}
int size(int x) {
return count[root(x)];
}
bool issame(int x, int y) {
return root(x) == root(y);
}
};
using UF = struct UnionFind;
int main() {
ll N;
cin >> N;
vl X(N), Y(N);
rep(i, N) cin >> X[i] >> Y[i];
using tp = tuple<ld, ll, ll>;
vc<tp> P;
rep(i, N) repe(j, i + 1, N) P.eb(hypotl(X[i] - X[j], Y[i] - Y[j]), i, j);
rep(i, N) P.eb(100 - Y[i], i, N), P.eb(Y[i] + 100, i, N + 1);
sort(all(P));
UF uf(N + 2);
for(auto[d, i, j]: P){
uf.unite(i, j);
if(uf.issame(N, N + 1)){
cout << d / 2 << '\n';
return 0;
}
}
} |
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
ll N;
ll par[105];
ll rank_[105];
void init_uf(ll n_){
for(ll i=0;i<n_;i++){
par[i]=i;
rank_[i]=0;
}
}
ll find(ll x){
if(par[x]==x)
return x;
return par[x]=find(par[x]);
}
void unite(ll x,ll y){
x=find(x); y=find(y);
if(x==y)
return ;
if(rank_[x]<rank_[y])
par[x]=y;
else{
par[y]=x;if(rank_[x]==rank_[y])rank_[x]++;
}
}
bool same(ll x,ll y){
x=find(x); y=find(y);
return x==y;
}
int main(){
cin>>N;
vector<ld>X(N);
vector<ld>Y(N);
for(ll i=0;i<N;i++){
cin>>X[i];cin>>Y[i];
}ld left=0;ld right=100;
while(0.000001<right-left){
ld mid=left+(right-left)/2;
init_uf(N+2);
for(ll i=0;i<N-1;i++)
for(ll j=i+1;j<N;j++)
if(pow(pow(X[i]-X[j],2)+pow(Y[i]-Y[j],2),0.5)<=2*mid)
unite(i+1,j+1);
for(ll i=0;i<N;i++){
if(abs(Y[i]-100)<=2*mid)unite(i+1,0);
if(abs(Y[i]+100)<=2*mid)unite(i+1,N+1);
}
if(same(0,N+1))
right=mid;
else
left=mid;
}cout<<fixed<<setprecision(7)<<left<<endl;
return 0;
} | // C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using P = pair<double, pair<int, int>>;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define a first
#define b second
double x[111], y[111];
// https://github.com/atcoder/live_library/blob/master/uf.cpp
// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
// -> 一部改変.
struct UnionFind{
vector<int> d;
UnionFind(int n = 0): d(n, -1) {}
int find(int x){
return (d[x] < 0) ? x : (d[x] = find(d[x]));
}
bool unite(int x, int y){
x = find(x);
y = find(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 find(x) == find(y);
}
int size(int x){
return -d[find(x)];
}
};
int main(){
// 1. 入力情報.
int N;
scanf("%d", &N);
rep(i, N) scanf("%lf %lf", &x[i], &y[i]);
// 2. 2点間の距離, 頂点のペア を 保存.
// -> 頂点 1 … 直線 y = -100, 頂点 N + 2 … 直線 y = 100 とする.
priority_queue<P, vector<P>, greater<P>> pq;
rep(i, N){
P p0, p1;
// 頂点 1 と 他の頂点との辺 を 追加.
p0.a = abs(-100.0 - y[i]);
p0.b.a = 0;
p0.b.b = i + 1;
pq.push(p0);
// 頂点 N + 2 と 他の頂点との辺 を 追加.
p1.a = abs(100.0 - y[i]);
p1.b.a = N + 1;
p1.b.b = i + 1;
pq.push(p1);
}
rep(i, N){
repx(j, i + 1, N){
double d = hypot(x[i] - x[j], y[i] - y[j]);
P p;
p.a = d;
p.b.a = i + 1;
p.b.b = j + 1;
pq.push(p);
}
}
// 3. 距離の短い方から順に, 辺を追加.
double ans = 0.0;
UnionFind uf(N + 2);
while(!pq.empty()){
// 3-1. 辺を取り出す.
P p = pq.top();
pq.pop();
// 3-2. 円の直径を更新.
ans = p.a;
// 3-3. グラフに辺を追加.
uf.unite(p.b.a, p.b.b);
// 3-4. 辺を追加した結果, 二直線が, 同じ連結成分に含まれてしまう場合は終了.
if(uf.same(0, N + 1)) break;
}
// 4. 出力.
printf("%.12lf\n", ans / 2.0);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
inline void mod(ll &x){
if(x>=MOD)x-=MOD;
}
vector<ll> fact;
vector<ll> ifact;
ll add(ll a,ll b)
{
a+=b;
while(a>=MOD) a-=MOD;
return a;
}
ll mult(ll a,ll b)
{
return (a*1LL*b)%MOD;
}
ll binpow(ll a, ll b)
{
ll r=1;
while(b)
{
if(b&1) r=mult(r,a);
a=mult(a,a);
b>>=1;
}
return r;
}
ll choose(ll a, ll b)
{
if(a<b) return 0;
if(b==0) return 1;
if(a==b) return 1;
ll r = mult(ifact[b],ifact[a-b]);
r = mult(r,fact[a]);
return r;
}
ll permute(ll a,ll b){
ll res = mult(fact[a],ifact[a-b]);
return res;
}
ll inv(int a){return binpow(a,MOD-2);}
void init(ll n)
{
fact.clear(); ifact.clear();
fact.resize(n+1);
ifact.resize(n+1);
ifact[0]=1;
fact[0]=1;
for(int i=1;i<=n;i++){fact[i]=mult(fact[i-1],i);}
ifact[n] = inv(fact[n]);
for(int i=n-1;i>=1;i--)
{
ifact[i] = mult(ifact[i + 1], i + 1);
}
}
int main()
{
owo
int n;
cin>>n;
init(1000000);
int sum = 0;
vector<int>a(n);
for(int i=0;i<n;i++){cin>>a[i];sum+=a[i];}
if(sum%2){
cout<<0;
return 0;
}
sum/=2;
VII dp(n+1,vector<ll>(sum+1));
dp[0][0] = 1;
for(int i=0;i<n;i++){
for(int j=n;j>=1;j--){
for(int k=a[i];k<=sum;k++){
dp[j][k]+=dp[j-1][k-a[i]];
mod(dp[j][k]);
}
}
}
ll ans = 0;
for(int i=1;i<=n;i++){
ll res = dp[i][sum]*fact[i]%MOD;
res*=fact[n-i];
res%=MOD;
//cout<<res<<'\n';
ans+=res;
ans%=MOD;
}
//cout<<"TES"<<'\n';
cout<<ans;
}
| #ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
template<typename T> bool chmax(T &a,T b) {if(a<b) {a=b; return true;} return false;}
template<typename T> bool chmin(T &a,T b) {if(a>b) {a=b; return true;} return false;}
#define itn int
#define fi first
#define se second
#define intmax numeric_limits<int>::max()
#define llmax numeric_limits<ll>::max()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>=1;i--)
#define all(vec) vec.begin(),vec.end()
#define sortt(vec) sort((vec).begin(),(vec).end())
#define rsort(vec) sort((vec).rbegin(), (vec).rend())
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef tuple<ll,ll,ll> tlll;
typedef tuple<int,int,int> tiii;
const ll mod=1e9+7;
const int inf=1<<30;
const ll lnf=1ll<<60;
ll rui(ll a,ll n){
ll ans=1;
while(n>0){
if(n&1) ans=ans*a%mod;
a=a*a%mod;
n/=2;
}
return ans;
}
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
mint dp[1010];
int main(){
dp[1]=2;
dp[2]=3;
rep1(i,1e3) dp[i+2]=dp[i+1]+dp[i];
int n; cin >> n;
char a,b,c,d; cin >> a >> b >> c >> d;
if(n==2||n==3){
cout << 1 << endl;
return 0;
}
if(b=='B'){
if(d=='B'){
cout << 1 << endl;
}
else{
if(c=='A'){
cout << rui(2,n-3) << endl;
}
else{
cout << dp[n-3] << endl;
}
}
}
else{
if(a=='A'){
cout << 1 << endl;
}
else{
if(c=='B'){
cout << rui(2,n-3) << endl;
}
else{
cout << dp[n-3] << endl;
}
}
}
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (int i = l; i < r; i++)
#define ForE(i, l, r) for (int i = l; i <= r; i++)
#define FordE(i, l, r) for (int i = l; i >= r; i--)
#define Fora(v, a) for (auto v: a)
#define bend(a) a.begin(), a.end()
#define isz(a) ((signed)a.size())
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpii;
typedef vector <vi> vvi;
const int N = 1e2 + 5;
int n, m, x, y;
char a[N][N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen(".inp", "r", stdin);
// freopen(".out", "w", stdout);
cin >> n >> m >> x >> y;
ForE(i, 1, n){
ForE(j, 1, m){
cin >> a[i][j];
}
}
int ans = 1;
For(k, 0, 4){
int tx = x, ty = y;
while (1){
tx += dx[k]; ty += dy[k];
if (a[tx][ty] == '.'){
ans++;
}
else{
break;
}
}
}
cout << ans << endl;
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/ | #include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int h = ri();
int w = ri();
int x = ri() - 1;
int y = ri() - 1;
std::string s[h];
for (auto &i : s) std::cin >> i;
int cnt = -3;
for (int i = x; i < h && s[i][y] != '#'; i++) cnt++;
for (int i = x; i >= 0 && s[i][y] != '#'; i--) cnt++;
for (int j = y; j < w && s[x][j] != '#'; j++) cnt++;
for (int j = y; j >= 0 && s[x][j] != '#'; j--) cnt++;
printf("%d\n", cnt);
return 0;
}
|
#define skies dashabi
#include <bits/stdc++.h>
using namespace std;
const int N= 200+10;
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define PII pair<int,int>
inline int inc(int x,int v,int mod){x+=v;return x>=mod?x-mod:x;}
inline int dec(int x,int v,int mod){x-=v;return x<0?x+mod:x;}
inline int rd(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline void write(int x){if(x==0){putchar(48);return;}int len=0,dg[20];while(x>0){dg[++len]=x%10;x/=10;}for(register int i=len;i>=1;i--)putchar(dg[i]+48);}
int a[N],n;
vector<int> b[N];
int dp[N];
bool fl;
void out(vector<int>va)
{
cout<<va.size()<<" ";
for(int i=0;i<va.size();i++)
{
printf("%d ",va[i]);
}
puts("");
}
void dfs(int x,vector<int>v,int sum)
{
if(fl||x>n)return ;
sum%=200;
if(!b[sum].empty())
{
fl=1;
puts("Yes");
out(v);
out(b[sum]);
return ;
}
cout<<x<<" "<<sum<<endl;
b[sum]=v;
dfs(x+1,v,sum);
v.push_back(x);
dfs(x+1,v,sum+a[x]);
}
int g(int x)
{
return ((x%200)+200)%200;
}
signed main()
{
//freopen("未命名1.in","r",stdin);
//freopen("未命名1.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++)
{
a[i]=rd();
}
for(int i=1;i<=n;i++)
{
a[i]%=200;
}
bool op=0;
for(int i=1;i<=n;i++)
{
if(op)break;
for(int j=0;j<=199;j++)
{
if(dp[g(j-a[i])]==1||j==a[i])
{
if(dp[j]==1)
{
puts("Yes");
b[g(j-a[i])].push_back(i);
out(b[g(j-a[i])]);out(b[j]);
op=1;
break;
}
}
}
for(int j=199;j>=0;j--)
{
if(dp[g(j-a[i])]==1||j==a[i])
{
dp[j]=2;
if(j!=a[i])
b[j]=b[g(j-a[i])];
b[j].push_back(i);
}
}
for(int j=199;j>=0;j--)
{
if(dp[j]==2)dp[j]=1;
}
}
if(!op)
{
puts("No");
}
return 0;
}
| #line 1 "/workspaces/compro/lib/template.hpp"
#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>
#ifndef IO_VECTOR
#define IO_VECTOR
template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
int size = v.size();
for (int i = 0; i < size; i++) {
std::cout << v[i];
if (i != size - 1)
std::cout << " ";
}
return out;
}
template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
for (auto &el : v) {
std::cin >> el;
}
return in;
}
#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<
using namespace std;
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;
}
// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
std::sort(v.begin(), v.end());
v.erase(std::unique(v.begin(), v.end()), v.end());
}
#line 2 "main.cpp"
std::string solve(int N, const std::vector<std::string> &S) {
map<string, bool> yes, no;
REP(i, N) {
if (S[i][0] == '!') {
auto sub = S[i].substr(1, S[i].length() - 1);
no[sub] = true;
} else {
yes[S[i]] = true;
}
}
for (const auto el : yes) {
auto [str, val] = el;
if (no.find(str) != no.end()) {
return str;
}
}
for (const auto &el : no) {
auto [str, val] = el;
if (yes[str]) {
cout << str << endl;
return str;
}
}
return "satisfiable";
}
// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
int N;
std::cin >> N;
std::vector<std::string> S(N);
for (int i = 0; i < N; ++i) {
std::cin >> S[i];
}
auto ans = solve(N, S);
std::cout << ans << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
struct io_setup{
io_setup(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
}
} io_setup;
int main(){
int A, B; cin >> A >> B; A--;
rep3(i, B, 1){
if((B/i)-(A/i) >= 2) {cout << i << '\n'; return 0;}
}
} | #include<bits/stdc++.h>
using namespace std;
#define Re register int
typedef long long ll;
const int N = 5005;
const int p = 998244353;
int n, m, k, t, fac[N], inv[N], f[15][5005];
inline int read()
{
char c = getchar();
int ans = 0;
bool f = 1;
while (c < 48 || c > 57)
{
if (c == '-') f = 0;
c = getchar();
}
while (c >= 48 && c <= 57) ans = (ans << 3) + (ans << 1) + (c ^ 48), c = getchar();
return f ? ans : -ans;
}
inline ll fpow(ll x, int y)
{
ll z = 1;
while (y)
{
if (y & 1) z = z * x % p;
x = x * x % p, y >>= 1;
}
return z;
}
inline ll C(int x, int y)
{
return 1ll * fac[x] * inv[y] % p * inv[x - y] % p;
}
int main()
{
n = read(), m = k = read(), fac[0] = fac[1] = 1;
if (m & 1)
{
putchar(48);
return 0;
}
for (Re i = 2; i <= n; ++i) fac[i] = 1ll * fac[i - 1] * i % p;
inv[n] = fpow(fac[n], p - 2);
for (Re i = n; i; --i) inv[i - 1] = 1ll * inv[i] * i % p;
while (k) k >>= 1, ++t;
f[t][0] = 1;
for (Re i = t - 1; i; --i)
for (Re j = 0; j <= m; j += (1 << i))
for (Re k = 0; k * (1 << i) <= j && (k << 1) <= n; ++k) f[i][j] = (C(n, k << 1) * f[i + 1][j - k * (1 << i)] + f[i][j]) % p;
printf("%d", f[1][m]);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main () {
int N;
cin >> N;
int gr[101][101];
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N; j ++) {
char c;
cin >> c;
gr[i][j] = (c == '1');
}
}
for (int i = 0; i < N; i ++) gr[i][i] = 1;
for (int p = 0; p < 10; p ++) {
int gr_[101][101];
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N; j ++) {
int kj = 0;
for (int k = 0; k < N; k ++) {
kj += gr[i][k] * gr[k][j];
}
gr_[i][j] = (kj > 0);
}
}
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N; j ++) {
gr[i][j] = gr_[i][j];
//cout << gr[i][j];
}
//cout << endl;
}
//cout << endl;
}
double ans = 0;
for (int i = 0; i < N; i ++) {
double kj = 0;
for (int j = 0; j < N; j ++) {
kj += (double)gr[j][i];
}
ans += 1 / kj;
}
cout << setprecision(15) << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
const int maxn=100000;
int f[maxn+5],l[maxn+5];
bool d[maxn+5];
vector<int>g[maxn+5];
bool cmp(int i,int j)
{
return (f[i]-l[i])>(f[j]-l[j]);
}
void dfs(int x)
{
//cout<<x<<endl;
if(g[x].size()==0)
{
l[x]=1;
f[x]=0;
d[x]=1;
return;
}
vector<int>v;
d[x]=0;
f[x]=0;
l[x]=1;
int tmp1=0,tmp2=0;
for(int i=0; i<g[x].size(); i++)
{
int id=g[x][i];
dfs(id);
d[x]^=d[id];
if(d[id])
v.push_back(id);
else
{
if(f[id]>=l[id])
{
f[x]+=f[id];
l[x]+=l[id];
}
else
{
tmp1+=f[id];
tmp2+=l[id];
}
}
}
sort(v.begin(),v.end(),cmp);
for(int i=0; i<v.size(); i++)
{
int id=v[i];
if(i%2==0)
{
l[x]+=l[id];
f[x]+=f[id];
}
else
{
f[x]+=l[id];
l[x]+=f[id];
}
}
//cout<<f[x]<<" "<<l[x]<<endl;
if(d[x])
{
f[x]+=tmp2;
l[x]+=tmp1;
}
else
{
f[x]+=tmp1;
l[x]+=tmp2;
}
d[x]^=1;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=2; i<=n; i++)
{
int x;
scanf("%d",&x);
g[x].push_back(i);
}
dfs(1);
//for(int i=1;i<=n;i++)
//cout<<d[i]<<" "<<l[i]<<" "<<f[i]<<endl;
printf("%d\n",l[1]);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> getIntArray(int num) {
vector<int> result;
int tmp = num;
while(tmp > 0) {
int digit = tmp % 10;
result.push_back(digit);
tmp /= 10;
}
return result;
}
int calc(vector<int> v) {
sort(v.begin(), v.end());
int greater = 0;
int less = 0;
for(int i = 0; i < (int)v.size(); i++) {
int num = v.at(i);
greater+=(num * pow(10, i));
if(num == 0) {
less*=10;
} else {
less+=(num * pow(10, (v.size() - i - 1)));
}
}
return greater - less;
}
int main() {
int N, K;
cin >> N >> K;
int result = N;
for(int i = 0; i < K; i++) {
vector<int> intArr = getIntArray(result);
result = calc(intArr);
}
cout << result << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 1e6 + 5;
int g2(int x) {
string s = to_string(x);
sort(s.begin(),s.end());
return stoi(s);
}
int g1(int x) {
string s = to_string(x);
sort(s.rbegin(),s.rend());
return stoi(s);
}
int f(int x) {
return g1(x)-g2(x);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, k;
cin>>n>>k;
for (int i=0; i<k; i++) {
n = f(n);
}
cout<<n<<endl;
return 0;
}
|
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<strings.h>
#include<sstream>
using namespace std;
int fan(int x)
{
int maxn=0,mixn=0;
int a[10]= {0},b[10]= {0};
string str;
stringstream ss;
ss << x;
ss >> str;
if(x==0)
return 0;
else
{
int len = str.length();
for(int i = 0; i < len; i ++)
{
a[str[i]-'0']++;
b[str[i]-'0']++;
}
//最大值
for(int i = 9; i >= 0; i--)
{
while(a[i]!=0)
{
maxn = maxn *10 + i;
a[i]--;
}
}
//最小值
for(int i = 1; i <= 9; i ++)
{
while(b[i]!=0)
{
mixn = mixn *10 + i;
b[i]--;
}
}
return maxn - mixn;
}
}
int main()
{
int x,n,pre=0,now=0;
scanf("%d%d",&x,&n);
if(n==0)
printf("%d\n",x);
else
{
for(int i = 1; i <= n ; i ++)
{
pre = now;
if(i==1)
now = fan(x);
else
now = fan(now);
if(now==pre)
break;
}
printf("%d\n",now);
}
return 0;
}
| #include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<string>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
//#define double long double
using namespace std;
typedef long long ll;
const int maxn=100005;
const int inf=0x3f3f3f3f;
int n,m,k;
int gfun1(int x){
vector<int>v;
while(x){
v.push_back(x%10);
x/=10;
}
sort(v.begin(),v.end());
int re=0;
for(auto i:v){
re*=10;
re+=i;
}
return re;
}
int gfun2(int x){
vector<int>v;
while(x){
v.push_back(x%10);
x/=10;
}
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
int re=0;
for(auto i:v){
re*=10;
re+=i;
}
return re;
}
int fun(int x){
return gfun2(x)-gfun1(x);
}
signed main(){
IOS
#ifndef ONLINE_JUDGE
freopen("D:\\_ACM_code\\IO\\in.txt","r",stdin);
freopen("D:\\_ACM_code\\IO\\out.txt","w",stdout);
#endif
cin>>n>>m;
int ans=n;
while(m--){
int t=fun(ans);
if(t==ans)
break;
ans=t;
}
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void fun()
{
ll n,k;
cin>>n>>k;
// ll a[n];
map<ll,ll>mp;
for(ll i=0;i<n;i++)
{
ll a,b;
cin>>a>>b;
mp[a]+=b;
}
ll curr=0;
for(auto i:mp)
{
if(curr+k>=i.first)
{
curr=curr+k;
k=i.second;
}
else
{
curr=curr+k;
k=0;
break;
}
}
if(k>0)
curr=curr+k;
cout<<curr;
}
int main() {
ll t=1;
// cin>>t;
while(t--)
fun();
return 0;
}
| /*jai_ganeshdeva*/
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pu push // adds the value to the last of the queue
#define lld long long int
#define ldo long double
#define ins insert /// used in set to insert the values
#define adv advance /// used to increment the iterator
#define mp make_pair
#define fi first
#define se second
#define all(c) c.begin(),c.end()
#define PI 3.1415926
#define INF (lld)1e16
#define vl vector<long long int >
#define vpll vector< pair<lld ,lld> >
#define vvl vector<vector<lld> >
#define pll pair <lld,lld>
lld p1=1e9 + 7,p2=998244353;
void judge(){
#ifndef ONLINE_JUDGE
// for getting input from input.
freopen("input.txt", "r", stdin);
// for writing output to output.txt
// freopen("output.txt", "w", stdout);
#endif
}
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
long long int modp(lld a,lld p,lld n)
{
// n=n%(p-1);
if(n==0) return 1;
if(n==1) return a%p;
lld x=modp(a,p,n/2);
x*=x;
x%=p;
return (n%2?(x*a)%p:x);
}
lld modInverse(lld n, lld p)
{
return modp(n, p, p-2);
}
// lld fac[500005];
// void getfac(lld p)
// {
// fac[0] = 1;
// for (lld i=1 ; i<=500005; i++)
// fac[i] = fac[i-1]*i%p;
// }
// long long int nCrmodp(lld n, lld r,lld p)
// {
// // Base case
// if (r==0)
// return 1;
// return (fac[n]* modInverse(fac[r], p) % p *
// modInverse(fac[n-r], p) % p) % p;
// }
struct segtree{
lld n,n2;
vector<lld> v;
void init(lld x)
{
lld a=1;
while(a<=x) a*=2;
n2=2*a;
n=x;
v.assign(n2,0);
}
void build(vector<lld> &w,lld curr,lld l,lld r)
{
if(l==r)
{
if(l<n)
{
v[curr]=w[l];
}
return;
}
lld mid=l+(r-l)/2;
build(w,2*curr+1,l,mid);
build(w,2*curr+2,mid+1,r);
v[curr]=v[2*curr+1]+v[2*curr+2];
}
void build(vector<lld>&w)
{
build(w,0,0,n-1);
}
void update(lld l, lld r,lld pos,lld curr)
{
if(l==r)
{
v[curr]-=1;
return;
}
lld mid=l+(r-l)/2;
if(pos<=mid)
{
update(l,mid,pos,2*curr+1);
}
else update(mid+1,r,pos,2*curr+2);
v[curr]=v[2*curr+1]+v[2*curr+2];
}
void update(lld pos)
{
update(0,n-1,pos,0);
}
pll getVal(lld l,lld r,lld curr,lld k)
{
if(l==r)
{
return {l,k};
}
lld mid=l+(r-l)/2;
if(v[2*curr+1]>=k)
{
return getVal(l,mid,2*curr+1,k);
}
return getVal(mid+1,r,2*curr+2,k-v[2*curr+1]);
}
pll getVal(lld k)
{
return getVal(0,n-1,0,k);
}
};
lld l,r,mid,ans=0;
lld n,i,j,k,g,m;
lld x,y,n1,n2,h,z,c;
bool compare(pair<pll,lld> p1,pair<pll,lld> p2)
{
if(p1.fi.fi!=p2.fi.fi) return p1.fi.fi<p2.fi.fi;
return p1.fi.se>p2.fi.se;
}
void solve(){
cin>>n>>k;
vector<pll> v;
for(i=0;i<n;i++)
{
cin>>x>>y;
v.pb({x,y});
}
sort(all(v));
for(i=0;i<n;i++)
{
if(k>=v[i].fi)
{
k+=v[i].se;
}
else break;
}
cout<<k;
}
int main()
{
// judge();
fastio();
lld t=1;
// lld n;
// cin>>t;
// cout<<t<<" \n";
// lld p1 = 1e9 + 7;
// getfac(p1);
for(int i=0;i<t;i++)
{
// cout<<"Case #"<<i+1<<": ";
// cout<<" ----------------\n";
solve();
cout<<" \n";
}
// cerr << "Time : " << ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
// exit(0);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, M;
vector<vector<int>> G(N);
bool visited[100];
vector<pair<int,int>> E1;
set<pair<int,int>> E2;
void f(int p) {
visited[p] = true;
for (int x : G[p]) {
if (visited[x]) {
E2.insert({min(p,x),max(x,p)});
} else {
E1.push_back({p,x});
f(x);
}
}
}
int main() {
cin >> N >> M;
G.resize(N);
for (int i = 0; i < M; ++ i) {
int u, v;
cin >> u >> v;
-- u; -- v;
G[u].push_back(v);
G[v].push_back(u);
}
ll res = 1;
vector<int> color(N);
for (int i = 0; i < N; ++ i) if (!visited[i]) {
E1.clear();
E2.clear();
f(i);
for (auto [u,v] : E1) E2.erase({min(u,v),max(u,v)});
int rr = 0;
for (int p = 0; p < (1<<E1.size()); ++ p) {
for (unsigned i = 0; i < E1.size(); ++ i) {
auto [u, v] = E1[i];
int cc[2];
for (int k = 0, j = 0; k < 3; ++ k) if (k != color[u]) cc[j++] = k;
color[v] = cc[(p>>i)&1];
}
for (auto [u, v] : E2) {
if (color[u] == color[v]) goto next;
}
++ rr;
next:;
}
res *= rr*3;
}
cout << res << endl;
}
| #include<algorithm>
#include<bitset>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << '\n'
constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-10;
constexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
constexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
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(b < a){
a = b;
return true;
}
return false;
}
bool dfs(int v, int c, vector<int>& col, const vector< vector<int> >& g){
col[v] = c;
bool ret = true;
for(auto to : g[v]){
if(col[to] == -1){
ret &= dfs(to, c ^ 1, col, g);
}else if(col[v] == col[to]){
return false;
}
}
return ret;
}
template<class T>
T power(T x, unsigned long long n){
T res = T(1);
while(n){
if(n & 1ull){
res *= x;
}
x *= x;
n >>= 1;
}
return res;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
int n, m;
cin >> n >> m;
vector< P > es;
rep(i, m){
int a, b;
cin >> a >> b;
es.emplace_back(a - 1, b - 1);
}
lint ans = 0;
rep(bit, 1 << n){
bool flg = true;
vector< vector<int> > g(n);
rep(i, m){
int a = es[i].first;
int b = es[i].second;
if((bit >> a & 1) && (bit >> b & 1)){
flg = false;
break;
}else if(!(bit >> a & 1) && !(bit >> b & 1)){
g[a].emplace_back(b);
g[b].emplace_back(a);
}
}
if(!flg){
continue;
}
vector<int> col(n, -1);
lint tmp = 1;
rep(i, n){
if((bit >> i & 1) || col[i] != -1){
continue;
}
if(!dfs(i, 0, col, g)){
tmp = 0;
flg = false;
break;
}
tmp *= 2;
}
ans += tmp;
}
cout << ans << endl;
cout << flush;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define N 20
#define INF 1000000005
#define INFLL (ll)1000000000000000005
#define PI 3.1415926535897
typedef long long ll;
#define ALL(v) (v).begin(),(v).end()
#define SZ(x) int(x.size())
#define IN(a) cin>>(a)
#define OUT(a) cout<<(a)<<endl
#define per(a,mod) ((a)%(mod)+(mod))%(mod)
typedef pair<int,int> P;
const int MAX = (1<<20);
const int MOD = 998244353;
ll dp[MAX][3][3];
void solve(){
int n;
cin>>n;
vector<int>x(n),y(n);
REP(i,n)cin>>x[i]>>y[i];
int m;
cin>>m;
REP(i,3)dp[0][i][i]=1;
REP(t,m){
int c;
cin>>c;
if(c==1){
ll w[3][3]={{0,1,0},{-1,0,0},{0,0,1}};
REP(i,3){
REP(j,3){
REP(k,3){
dp[t+1][i][j]+=w[i][k]*dp[t][k][j];
}
}
}
}else if(c==2){
ll w[3][3]={{0,-1,0},{1,0,0},{0,0,1}};
REP(i,3){
REP(j,3){
REP(k,3){
dp[t+1][i][j]+=w[i][k]*dp[t][k][j];
}
}
}
}else if(c==3){
ll p;
cin>>p;
ll w[3][3]={{-1,0,2*p},{0,1,0},{0,0,1}};
REP(i,3){
REP(j,3){
REP(k,3){
dp[t+1][i][j]+=w[i][k]*dp[t][k][j];
}
}
}
}else{
ll p;
cin>>p;
ll w[3][3]={{1,0,0},{0,-1,2*p},{0,0,1}};
REP(i,3){
REP(j,3){
REP(k,3){
dp[t+1][i][j]+=w[i][k]*dp[t][k][j];
}
}
}
}
}
int Q;
cin>>Q;
vector<int>ra(Q),rb(Q);
REP(i,Q){
cin>>ra[i]>>rb[i];
rb[i]--;
}
REP(i,Q){
int a=ra[i],b=rb[i];
ll ax=dp[a][0][0]*x[b]+dp[a][0][1]*y[b]+dp[a][0][2];
ll ay=dp[a][1][0]*x[b]+dp[a][1][1]*y[b]+dp[a][1][2];
cout<<ax<<" "<<ay<<endl;
}
}
int main(){
solve();
return 0;
}
| #include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
using LL = long long;
const int M = 1e9 + 7;
int main() {
// freopen("in", "r", stdin);
std::cin.tie(nullptr)->sync_with_stdio(false);
int n;
std::cin >> n;
std::vector<std::vector<std::pair<int, LL>>> e(n + 1);
for (int i = 1, u, v; i < n; ++i) {
LL w;
std::cin >> u >> v >> w;
e[u].emplace_back(v, w);
e[v].emplace_back(u, w);
}
std::vector<LL> val(n + 1);
std::function<void(int, int)> dfs = [&](int u, int fa) {
for (auto &[v, w] : e[u]) if (v != fa) {
val[v] = val[u] ^ w;
dfs(v, u);
}
};
dfs(1, 0);
const int N = 60;
std::vector<int> cnt(N);
for (auto x : val) {
for (int i = 0; x; x >>= 1, ++i) if (x & 1) ++cnt[i];
}
int ans = 0;
for (int i = 0; i < N; ++i) {
ans = (ans + (1LL << i) % M * cnt[i] % M * (n - cnt[i])) % M;
}
std::cout << ans << '\n';
return 0;
} |
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
using pii = pair<int, int>;
bool nBase(string s, ll n, ll m){
__int128 res = 0;
int len = s.length();
for (int i = 0; i < len;i++){
res = res * n + s[i] - '0';
if(res > m)
return false;
}
return true;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string x;
cin >> x;
ll m;
cin >> m;
if(x.length()==1){
cout << (atoi(x.c_str()) <= m ? 1 : 0);
return 0;
}
int d = *max_element(x.begin(), x.end()) - '0';
ll l = d + 1, r = 1e18;
while(l <= r){
ll mid = (l + r) / 2;
if(nBase(x, mid, m)){
l = mid + 1;
}else{
r = mid - 1;
}
}
cout << max(0ll, l - d - 1) << '\n';
return 0;
}
| // YATIN KWATRA
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ar array
#define sz(v) (int)(v.size())
#define inf 1e18
#define int ll
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ld long double
#define ull unsigned long long
#define endl "\n"
#define fo(i,a,b) for(int i = a; i<=b ; i++)
#define rfo(i,a,b) for(int i = a; i>=b ; i--)
#define vii vector<int>
#define pq priority_queue
#define uomii unordered_map<int,int,best_hash>
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define pii pair<int,int>
#define mii map<int,int>
#define vvii vector<vii>
#define mod 1000000007
#define MIN -1e9
#define pi 3.1415926535897932384626433832795
#define cz(x) 63 - __builtin_clzll(x)
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct best_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
void INPUT()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/*
----------------------------------------------------------------
-> Check For Overflows
-> Out of Bounds
-> Initialisations of global arrays and variables
----------------------------------------------------------------
*/
const int N = 805;
int v[N][N];
int a[N][N];
int dp[N][N];
int n, k;
bool possible(int med) {
// so we can do like this in order to check if there is any matrix of size
// K * K, such that median is <= med.
// we can convert all values greater than med into 1 and other to 0
// so if sum of a k * k matrix is <= (k*k)/2 , so median can be <= med
fo(i, 0, n - 1) {
fo(j, 0, n - 1) {
if (v[i][j] > med) a[i][j] = 1;
else a[i][j] = 0;
}
}
dp[0][0] = a[0][0];
fo(i, 1, n - 1) {
dp[0][i] = dp[0][i - 1] + a[0][i];
dp[i][0] = dp[i - 1][0] + a[i][0];
}
fo(i, 1, n - 1) {
int sum = a[i][0];
fo(j, 1, n - 1) {
dp[i][j] = dp[i - 1][j] + a[i][j] + sum;
sum += a[i][j];
}
}
fo(i, k - 1, n - 1) {
fo(j, k - 1, n - 1) {
int sum = dp[i][j];
if ((j - k) >= 0) sum -= dp[i][j - k];
if ((i - k) >= 0) sum -= dp[i - k][j];
if ((i - k) >= 0 and (j - k) >= 0) sum += dp[i - k][j - k];
if (sum <= ((k * k) / 2)) return 1;
}
}
return 0;
}
void solve()
{
cin >> n >> k;
fo(i, 0, n - 1) {
fo(j, 0, n - 1) cin >> v[i][j];
}
int low = 0, high = 1e9, ans = 1e9, mid;
while (low <= high) {
mid = (low + high) / 2;
if (possible(mid)) {
ans = mid;
high = mid - 1;
}
else low = mid + 1;
}
cout << ans;
}
signed main() {
FIO
INPUT();
int t;
t = 1;
//cin >> t;
fo(i, 0, t - 1) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template<typename T> vector<T> vin(int n) {
vector<T> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
return v;
}
vector<int> vi(int n) {
vector<int> v(n);
for (int i = 0; i < n; ++i) v[i] = i;
return v;
}
void in() {
}
template<typename Head, typename... Tail>
void in(Head& h, Tail&... tails) {
cin >> h;
in(forward<Tail&>(tails)...);
}
void init_cin(const std::string& s) {
cin.rdbuf((new istringstream(s))->rdbuf());
}
void out() {
cout << endl;
}
template<typename T, typename... Args>
void out(T&& head, Args&&... args) {
cout << head << " ";
out(forward<Args>(args)...);
}
template<template <typename T, typename A = allocator<T>> class Cont, typename T>
ostream& operator<<(ostream& os, const Cont<T>& c) {
for (auto it = c.begin(); it != c.end(); ++it) {
if (it != c.begin()) os << " ";
os << *it;
}
return os;
}
int main() {
auto f = [](int n, int k, const vector<vector<int>>& tt) {
vector<int> cities = vi(n);
int ans = 0;
do {
int time = 0;
for (size_t i = 0; i < cities.size(); ++i) {
time += tt[cities[i]][cities[(i+1)%n]];
}
if (time == k) ans++;
// out(cities, "/", time, ans);
} while (next_permutation(cities.begin()+1, cities.end()));
out(ans);
};
int N, K;
cin >> N >> K;
vector<vector<int>> tt(N, vector<int>(N));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> tt[i][j];
}
}
f(N, K, tt);
// f(4, 330,
// {
// {0, 1, 10, 100},
// {1, 0, 20, 200},
// {10, 20, 0, 300},
// {100, 200, 300, 0}
// });
}
| #include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
typedef pair<int, int> PII;
const int N = 10;
const int M = 110;
ll h[N], ne[M], e[M], w[M], idx;
ll ans;
int n;
ll k;
bool vis[N];
bool vis1;
int cnt = 1;
void add(int a, int b, int c)
{
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx++;
}
void dfs(int x, ll dis)
{
if(cnt == n && x == 1)
if(dis == k)
{
ans++;
return;
}
vis[x] = true;
cnt++;
for(int i = h[x]; ~i; i = ne[i])
{
int j = e[i];
if(vis[j])
continue;
ll tmp = dis + w[i];
if(tmp > k)
continue;
dfs(j, tmp);
}
vis[x] = false;
cnt--;
}
int main()
{
IOS;
memset(h, -1, sizeof h);
cin >> n >> k;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
int tmp;
cin >> tmp;
if(j < i)
continue;
add(i, j, tmp);
add(j, i, tmp);
}
for(int i = h[1]; ~i; i = ne[i])
{
int t = e[i];
dfs(t, w[i]);
}
cout << ans << endl;
} |
//templete
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0;i<n;i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int main(){
int N;
vector<int>A;
vector<int>B;
int tmpa,tmpb;
cin>>N;
rep(i,N){
cin>>tmpa>>tmpb;
A.push_back(tmpa);
B.push_back(tmpb);
}
int time=200009;
rep(i,N){
rep(j,N){
if(i==j)continue;
if(time>max(A[i],B[j])){
time=max(A[i],B[j]);
}
}
}
rep(i,N){
if(A[i]+B[i]<time){
time=A[i]+B[i];
}
}
cout<<time;
return 0;
} | #include <bits/stdc++.h>
#define ALL(a) a.begin(), a.end()
#define ALLR(a) a.rbegin(), a.rend()
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a); i > (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, n) for (int i = (n)-1; i >= 0; i--)
#define SI(a) (int)a.size()
using namespace std;
using ll = long long;
template <class T>
bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll llceil(ll a, ll b) { return (a + b - 1) / b; }
ll shift(ll a) { return 1LL << a; }
const int INF = shift(30);
const ll LINF = shift(60);
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
REP(i, n) { cin >> a[i] >> b[i]; }
int ma, mb, ia, ib;
ma = mb = INF;
ia = ib = -1;
REP(i, n) {
if (chmin(ma, a[i])) {
ia = i;
}
if (chmin(mb, b[i])) {
ib = i;
}
}
if (ia != ib) {
cout << max(a[ia], b[ib]) << endl;
} else {
int ia2, ib2;
ia2 = ib2 = -1;
ma = mb = INF;
REP(i, n) {
if (i == ia) continue;
if (chmin(ma, a[i])) {
ia2 = i;
}
if (chmin(mb, b[i])) {
ib2 = i;
}
}
cout << min({a[ia] + b[ib], max(a[ia], b[ib2]), max(a[ia2], b[ib])})
<< endl;
}
} |
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#define MOD 1000000007
int dp[1001];
int main() {
int N;
char aa,ab,ba,bb;
cin >> N >> aa >> ab >> ba >> bb;
if (N == 2) {
cout << 1 << endl;
return 0;
}
if (ab == 'B') {
if (bb == 'B') {
cout << 1 << endl;
return 0;
}
else if (ba == 'A') {
int i,p = 1;
for (i = 0; i < N-3; i++) p *= 2,p %= MOD;
cout << p << endl;
}
else {
int i;
dp[0] = dp[1] = 1;
for (i = 2; i <= N; i++) {
dp[i] = dp[i-2]+dp[i-1];
dp[i] %= MOD;
}
cout << dp[N-2] << endl;
}
}
else {
if (aa == 'A') {
cout << 1 << endl;
return 0;
}
else if (ba == 'B') {
int i,p = 1;
for (i = 0; i < N-3; i++) p *= 2,p %= MOD;
cout << p << endl;
}
else {
int i;
dp[0] = dp[1] = 1;
for (i = 2; i <= N; i++) {
dp[i] = dp[i-2]+dp[i-1];
dp[i] %= MOD;
}
cout << dp[N-2] << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define M1 1000000007
#define M2 998244353
#define INF 1e18
#define ll long long
#define pll pair<ll,ll>
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define REPR(i,a,b) for(ll i=b-1;i>=a;i--)
#define forr(i,n) for(ll i=0;i<n;i++)
#define F first
#define S second
#define pb push_back
#define DB pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<int>
#define vlli vector <long long>
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'
#define CASE(t) ll t; cin>>t; while(t--)
/********************************************************************/
const double pi = 3.1415926535;
/********************************************************************/
//FAST IO//
void FAST() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
/********************************************************************/
int main()
{
FAST();
CASE(t)
{
ll int n; cin>>n;
int ct=0;
while (n%2==0)
{
ct++; n/=2;
}
if (ct==1){cout<<"Same"<<endl; }
else if (ct==0){cout<<"Odd"<<endl; }
else {cout<<"Even"<<endl; }
}
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005
ll a[MAX_N] = {};
int main() {
ll n, w;
cin >> n >> w;
ll s[n], t[n], p[n];
rep(i,n){
cin >> s[i] >> t[i] >> p[i];
a[s[i]] += p[i];
a[t[i]] -= p[i];
}
int ok = 1;
ll now = 0;
rep(i,MAX_N){
now += a[i];
if(now > w) ok = 0;
}
if(ok) yn;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vector<T>>;
int main() {
int n, w; cin >> n >> w;
vec<int64_t> imos(2 * 1e5 + 1, 0);
for (int i = 0; i < n; ++i) {
int s, t, p; cin >> s >> t >> p;
imos[s] += p;
imos[t] -= p;
}
for (int i = 0; i < 2 * 1e5; ++i) {
imos[i + 1] += imos[i];
}
for (auto e : imos) {
if (w < e) {
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
}
|
#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template<class T>bool chmax(T &a, const T &b) {if(a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a) {a=b; return 1;} return 0;}
static const ll INF=1e18;
static const ll MAX=101010;
//static const ll MOD=1e9+7;
static const ll MOD=998244353;
//for(i=0;i<N;i++) cin>>a[i];
class sumSegTree {
public:
ll n, height;
vector<ll> data;
sumSegTree(ll _n) {
n=1;
height=1;
while(n<_n) {
n*=2;
height++;
}
data=vector<ll>(2*n-1);
}
void add(ll i, ll x) {
i+=n-1;
data[i]+=x;
while(i>0) {
i=(i-1)/2;
data[i]+=x;
}
}
//[a, b):要求区間,[l, r):対象区間
ll _sum(ll a, ll b, ll k, ll l, ll r) {
if(r<=a || b<=l) return 0;
if(a<=l && r<=b) return data[k];
else {
ll s=_sum(a, b, 2*k+1, l, (l+r)/2);
ll t=_sum(a, b, 2*k+2, (l+r)/2, r);
return s+t;
}
}
ll sum(ll a, ll b) {
return _sum(a, b, 0, 0, n);
}
};
int main(void) {
ll i, j, k;
ll H,W,M;
cin>>H>>W>>M;
vector<vector<ll>> yoko(H,vector<ll>{W}),tate(W,vector<ll>({H}));
for(i=0;i<M;i++){
ll x,y;
cin>>x>>y;x--;y--;
yoko[x].push_back(y);
tate[y].push_back(x);
}
for(i=0;i<H;i++) sort(yoko[i].begin(), yoko[i].end());
for(j=0;j<W;j++) sort(tate[j].begin(), tate[j].end());
ll ans=0;
ll f=0;
for(j=0;j<yoko[0][0];j++) ans+=tate[j][0];
sumSegTree sst(W+1);
for(j=yoko[0][0];j<W;j++) sst.add(j,1);
for(i=0;i<tate[0][0];i++){
ans+=sst.sum(0, yoko[i][0]);
for(auto u:yoko[i]){
if(sst.sum(u, u+1)==0)
sst.add(u, 1);
}
}
pt(ans);
}
| #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <utility>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <stack>
#include <functional>
#include <iterator>
#include <random>
#include <fstream>
#include <chrono>
#include <cctype>
using namespace std;
#define forn(i, n) for (int i = 0; i < n; ++i)
#define rforn(i, n) for (int i = n; i >= 0; --i)
#define int long long
const int N = 201337;
int ts=N * 4;
vector<vector<int>>d(ts);
vector<int> a;
void build(int v, int l, int r) {
if (l == r - 1) {
d[v].push_back(a[l]);
return;
}
int m = (l+r)/2;
build(2*v+1,l,m);
build(2*v+2,m,r);
merge(d[2*v+1].begin(),d[2*v+1].end(),d[2*v+2].begin(),d[2*v+2].end(),back_inserter(d[v]));
}
int get_res(int v,int l,int r, int L, int R, int k){
if (r <= L || R <= l) {
return 0;
}
if (L <= l && r <= R) {
return d[v].end() - upper_bound(d[v].begin(),d[v].end(),k);
}
int m = (l+r)/2;
int left = get_res(2*v+1,l,m,L,R,k);
int right = get_res(2*v+2,m,r,L,R,k);
return left+right;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h, w, m;
cin >> h >> w >> m;
// храним для row минимальный
// храним для column min
vector<int>row(h + 3, w + 1); // сколько для строки я могу пройти вниз
vector<int>col(w + 3, h + 1); // сколько для столбца я могу пройти вправо
forn(i, m){
int x, y;
cin >> x >> y;
x--;
y--;
row[x] = min(row[x], y + 1);
col[y] = min(col[y], x + 1);
}
a.assign(w, 0);
int ans = 0;
forn(i, row[0] - 1){ //
//cout << "at col = " << i << "limit = " << max(0ll,col[i] - 1) << "\n";
a[i] = max(0ll,col[i] - 1);
ans += max(0ll,col[i] - 1);
}
//cout << ans << "\n";
// forn(i, w){
// if(col[i] == w)
// col[i]++;
// }
build(0,0,w);
forn(i, a[0]){
int R = row[i] - 1;
//cout << "at row = " << i + 1 << " we can take up to" << R << "\n";
//R--;
int q = get_res(0,0,w,0,R,i);
//cout << "q = " << q << "\n";
ans += R - q;
}
cout << ans << "\n";
return 0;
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ud unsigned int
#define ll long long
#define ull unsigned long long
#define MAX_INF 0x3f
#define MAX_INF_VAL 0x3f3f3f3f
#define MAX_INF_VAL_LL 0x3f3f3f3f3f3f3f3f
//#define pi 3.141592653589
#define eps 1e-9
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
//#define p 2173412051LL
//#define sz 2
using namespace std;
template< typename T > void read( T &x ) {
x = 0;
char ch = getchar();
ll f = 1;
while( !isdigit( ch ) ) {
if( ch == '-' )
f *= -1;
ch = getchar();
}
while( isdigit( ch ) ) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
}
struct custom_hash {
static uint64_t splitmix64( uint64_t x ) {
x += 0x9e3779b97f4a7c15;
x = ( x ^ ( x >> 30 ) ) * 0xbf58476d1ce4e5b9;
x = ( x ^ ( x >> 27 ) ) * 0x94d049bb133111eb;
return x ^ ( x >> 31 );
}
size_t operator() ( uint64_t x ) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64( x + FIXED_RANDOM );
}
};
int n;
string str;
pair< ll, ll > a[ 200010 ];
int cnt[ 3 ];
ll get( int, int );
ll getAns( int, int );
int main() {
ios::sync_with_stdio( false );
cin.tie( 0 ), cout.tie( 0 );
cin >> n;
n <<= 1;
for( int i = 1; i <= n; ++i ) {
cin >> a[ i ].first >> str;
if( str[ 0 ] == 'R' )
a[ i ].second = 0;
else if( str[ 0 ] == 'G' )
a[ i ].second = 1;
else
a[ i ].second = 2;
++cnt[ a[ i ].second ];
}
if( cnt[ 0 ] % 2 == 0 && cnt[ 1 ] % 2 == 0 && cnt[ 2 ] % 2 == 0 ) {
cout << 0;
return 0;
}
sort( a + 1, a + 1 + n );
if( cnt[ 0 ] % 2 == 0 )
cout << getAns( 1, 2 );
else if( cnt[ 1 ] % 2 == 0 )
cout << getAns( 0, 2 );
else
cout << getAns( 0, 1 );
return 0;
}
ll get( int x, int y ) {
ll ans = MAX_INF_VAL_LL;
ll lst = -MAX_INF_VAL_LL;
for( int i = 1; i <= n; ++i ) {
if( a[ i ].second == y )
ans = min( ans, a[ i ].first - lst );
else if( a[ i ].second == x )
lst = a[ i ].first;
}
return ans;
}
ll getAns( int x, int y ) {
int z = 3 ^ x ^ y;
ll ans = min( get( x, y ), get( y, x ) );
if( cnt[ z ] ) {
ll t1 = min( get( x, z ), get( z, x ) );
ll t2 = min( get( y, z ), get( z, y ) );
ans = min( ans, t1 + t2 );
}
return ans;
} |
/**
* Dont raise your voice, improve your argument.
* --Desmond Tutu
*/
#include <bits/stdc++.h>
using namespace std;
const bool ready = [](){
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
return true;
}();
using ld=long double;
const ld PI = acos((ld)-1);
using ll= long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for(int i=0; i<int(n); i++)
#define cini(i) int i; cin>>i;
#define cins(s) string s; cin>>s;
#define cind(d) ld d; cin>>d;
#define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; }
using pii= pair<int, int>;
using pdd= pair<ld, ld>;
using vd= vector<ld>;
using vb= vector<bool>;
using vi= vector<int>;
using vvi= vector<vi>;
using vs= vector<string>;
#define endl "\n"
void solve() {
cini(x);
x%=100;
cout<<100-x<<endl;
}
signed main() {
solve();
} |
#include <bits/stdc++.h>
using namespace std;
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define FOR(i,a) for(int i=0;i<a;i++)
#define FOR1(i,j,a) for(int i=j;i<a;i++)
#define EB emplace_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define max1 101
#define MOD 1000000007
#define read(type) readInt<type>()
#define out(x) cout<<x<<'\n'
#define out1(x) cout<<x<<" "
#define END cout<<'\n'
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
void fast(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
signed main(){
int n;
cin>>n;
int a[n];
FOR(i,n){
cin>>a[i];
if(a[i]<0){a[i]*=-1;}
}
int x=0;
FOR(i,n){
x+=a[i];}
out(x);
x=0;
FOR(i,n){
x+=a[i]*a[i];
}
double ans=(double)sqrt(x);
cout<<fixed<<setprecision(15)<<ans<<endl;
x=0;
FOR(i,n){
x=max(x,a[i]);}
out(x);
}
| #include "bits/stdc++.h"
using namespace std;
#define int long long
#define forn(i,n) for(int i = 0; i < n; i++)
int32_t main() {
int n, q, k; cin >> n >> q;
vector<int> a(n);
forn(i, n) cin >> a[i];
vector<int> ps(n);
ps[0] = a[0] - 1;
forn(i, n - 1) ps[i + 1] = ps[i] + a[i + 1] - a[i] - 1;
while (q--) {
cin >> k;
int x = --lower_bound(ps.begin(), ps.end(), k) - ps.begin();
if (x == -1) cout << k;
else cout << a[x] + k - ps[x];
cout << "\n";
}
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define drep(i,b,a) for(int i = (b)-1; i >= (a); i--)
#define bit(n) (1LL << (n))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(v) sort(v.begin(),v.end());
#define RSORT(v) sort(v.rbegin(),v.rend());
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vd> vvd;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
const int inf = 1 << 30; const ll infl = 1LL << 60; const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool odd(T x) { return x & 1; }
template<class T> inline bool even(T x) { return !(x & 1); }
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill( (T*)array, (T*)(array+N), val );
}
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << endl; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; }
//----------------------------------------------------------------------------------------------
struct edge {
int to;
int c;
edge(int to, int c):to(to),c(c){}
};
vector<edge> G[100010];
int res[100010];
void dfs(int v, int p = -1, int ok = 1) {
if(ok) {
if(p != -1) {
if(res[p] != 0) res[v] = 0;
else res[v] = 1;
} else {
res[v] = 0;
}
for(auto e : G[v]) {
int nv = e.to;
int co = e.c;
if(res[nv] != -1) continue;
if(co == res[v]) dfs(nv, v, ok);
else {
res[nv] = co;
dfs(nv, v, 1-ok);
}
}
}
else {
for(auto e : G[v]) {
int nv = e.to;
int co = e.c;
if(res[nv] != -1) continue;
if(co == res[v]) dfs(nv, v, 1-ok);
else {
res[nv] = co;
dfs(nv, v, ok);
}
}
}
}
int main()
{
int n,m;
cin >> n >> m;
rep(i,0,m) {
int u,v,c;
cin >> u >> v >> c;
u--; v--; c--;
G[u].emplace_back(v,c);
G[v].emplace_back(u,c);
}
rep(i,0,n) res[i] = -1;
dfs(0);
rep(i,0,n) cout << res[i]+1 << endl;
return 0;
} | // #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define _rep(i, x, y) for(int i = (int)x; i < (int)y; ++i)
#define _dep(i,x,y) for(int i = (int)x; i > (int)y; i--)
#define PII pair<int,int>
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define PQ priority_queue
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<ll> VL;
typedef vector<vector<ll>> VLL;
constexpr int mod = 1e9 + 7;
constexpr int KINF = 0x3f3f3f3f;
constexpr int INF = 0x7f7f7f7f;
constexpr double eps = 1e-7;
int main(){
ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
int r, x, y;
cin >> r >> x >> y;
ll dist = (1ll * x * x) + (1ll * y * y);
//dist = ceil(sqrt(dist));
if(dist < 1ll * r * r) {
cout << 2 << endl;
return 0;
}
ll k = 1;
while(1ll * k * k * r * r < dist) k ++;
cout << k << endl;
return 0;
}
// 小心左移运算符导致的int溢出
// dp记得处理非法状态
// resize并不会初始化数组
// 全局函数一定要记得inline
// 带空格字符串不能直接cin,要用getline
// 最好用用"\n"代替endl
|
#include <bits/stdc++.h>
#include <immintrin.h>
using namespace std;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
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, const vector<T> &a) {
for (auto &i : a) {
out << i << " ";
}
return out;
}
template<typename T, typename D>
istream &operator>>(istream &in, pair<T, D> &a) {
in >> a.first >> a.second;
return in;
}
template<typename T, typename D>
ostream &operator<<(ostream &out, const pair<T, D> &a) {
out << a.first << " " << a.second;
return out;
}
struct LogOutput {
template<typename T>
LogOutput &operator<<(const T &x) {
#ifdef DIVAN
cout << x;
#endif
return *this;
}
} dout, fout;
typedef long long ll;
typedef unsigned long long ull;
typedef double dl;
typedef complex<double> cd;
#define nl '\n'
#define elif else if
#define all(_v) _v.begin(), _v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) (int)(v.size())
#define sqr(_v) ((_v) * (_v))
#define vpi vector<pair<int, int>>
#define eb emplace_back
#define pb push_back
#define mod(x, m) ((x) >= 0 ? ((x) % m) : ((((x) % m) + m) % m))
#define vi vector<int>
#define pi pair<int, int>
#define ti tuple<int, int, int>
#define minq(x, y) x = min((x), (y))
#define maxq(x, y) x = max(x, (y))
#define forn(i, n) for (int i = 0; i < (n); ++i)
const ll INFL = 9187201950435737471;
const ll nINFL = -9187201950435737472;
const int INF = 2139062143;
const int nINF = -2139062144;
const ull ULINF = numeric_limits<ull>::max();
const long double PI = acos(-1);
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(seed);
inline void IO() {
#ifdef DIVAN
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
}
//#define int ll
const int MAXN = 2000 + 15;
vpi adj[MAXN];
int dist[MAXN];
int g[MAXN][MAXN];
void Func(int from) {
memset(dist, 127, sizeof(dist));
dist[from] = 0;
set <pi> q;
q.insert({0, from});
while (!q.empty()) {
auto [d, v] = *q.begin();
q.erase(q.begin());
for (auto &[to, cost] : adj[v]) {
if (dist[to] > d + cost) {
q.erase({dist[to], to});
dist[to] = d + cost;
q.insert({dist[to], to});
}
}
}
}
void Solve() {
memset(g, -1, sizeof(g));
int n, m;
cin >> n >> m;
forn(i, m) {
int a, b, c;
cin >> a >> b >> c;
if (g[a][b] == -1 || g[a][b] > c) {
g[a][b] = c;
}
adj[a].eb(b, c);
}
for (int i = 1; i <= n; ++i) {
Func(i);
int now = INF;
for (int j = 1; j <= n; ++j) {
if (g[j][i] != -1 && dist[j] != INF) {
minq(now, g[j][i] + dist[j]);
}
}
if (now == INF) {
cout << "-1\n";
} else {
cout << now << '\n';
}
}
}
signed main() {
IO();
int t = 1;
// cin >> t;
int startTime = clock();
for (int i = 1; i <= t; ++i) {
// cout << "Case #" << i << ": ";
Solve();
}
int endTime = clock();
fout << '\n' << "Time: " << (endTime - startTime + 999) / 1000;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
#define lin(x) ll x; cin>>x;
#define stin(x) string x; cin>>x;
#define yn(x) if(x) { cout<<"Yes"<<endl; } else { cout<<"No"<<endl; }
#define YN(x) if(x) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; }
#define co(x) cout<<x<<endl;
#define kiri(x,y) (x+y-1)/y;
#define shisha(x,y) (x+(y/2))/y;
#define gomi(x) x.erase(unique(all(x)), x.end());
using ll=long long;
using ld=long double;
using vl=vector<ll>;
using vs=vector<string>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll mod=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
return ((a > b) ? (a = b, true) : (false));
}
ll powmod(ll n, ll k, ll m) {
ll r=1;
for(; k>0; k >>=1) {
if(k&1) r=(r*n)%m;
n=(n*n)%m;
}
return r;
}
ll fact(ll n) {
ll a=1;
rep(i,n) {
a=a*(n-i);
}
return a;
}
ll pow(ll a, ll b) {
ll x=1;
rep(i,b) {
x=x*a;
}
return x;
}
int main() {
lin(a);
lin(b);
lin(c);
co(21-a-b-c);
} |
#include <bits/stdc++.h>
#define forloop(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ll long long int
#define ld long double
#define pb emplace_back
#define ravi ios_base::sync_with_stdio(false);
#define parmar2002 cin.tie(NULL); cout.tie(NULL);
#define nl "\n"
#define sclock clock_t tStart = clock();
#define eclock printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
#define finout freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define whole(x) x.begin(), x.end()
#define wholer(x) x.rbegin(), x.rend()
#define len(s) s.size()
#define endline cout << "\n";
#define T(T) ll tt; cin >> tt; forloop(T, 1, tt + 1)
#define retf(flag, x) if(flag) {cout<<x<<nl; return;}
#define vint vector<int>
#define forv(arr) for(auto i:arr) cout<<i<<" "; endline;
#define forin(arr) for(auto &i : arr) cin>>i;
#define ret(x) {cout<<x<<nl; return;}
using namespace std;
class Solution {
public:
};
void solve();
int main()
{
ravi parmar2002
// finout //In case of File input output
// sclock
// T(T)
solve();
// eclock
return 0;
}
void solve()
{
ll n, k;
cin>>n>>k;
vector<pair<ll, ll>>arr(n);
for(auto &i:arr) cin>>i.first>>i.second;
arr.pb(make_pair(0, 0));
sort(whole(arr));
ll prev = 0;
forloop(i, 1, len(arr))
{
if(arr[i].first - arr[i-1].first <= k)
k -= (arr[i].first - arr[i-1].first);
else ret(arr[i-1].first + k);
k += arr[i].second;
}
cout<<arr.back().first + k;
} | #include<bits/stdc++.h>
#define god dimasi5eks
#pragma GCC optimize("O3")
#define fi first
#define se second
#define pb push_back
#define pf push_front
// #define fisier 1
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const double dancila = 3.14159265359; // PI
const double eps = 1e-9;
int n;
ll k;
vector<pair<ll, ll> > v;
int main()
{
#ifdef fisier
ifstream cin("input.in");
ofstream cout("output.out");
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for(int i = 1; i <= n; ++i)
{
ll a, b;
cin >> a >> b;
v.push_back({a, b});
}
sort(v.begin(), v.end());
v.push_back({(1LL<<62), 0});
ll current_place = 0;
for(int i = 0; i < v.size(); ++i)
{
if(v[i].first - current_place > k)
{
current_place += k;
break;
}
k -= (v[i].first - current_place);
k += v[i].second;
current_place = v[i].first;
}
cout << current_place;
return 0;
}
|
/*
Alexander Rango
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
#define pii pair <ll , ll>
#define F first
#define S second
const ll max_n = 1e3 + 20, inf = 1e18 + 20;
ll dist[max_n][max_n], n, m ,s, t, T, u, v, w, ans;
char c;
bitset <max_n> visited[max_n];
vector <pii> g[max_n][26];
void solve(ll x, ll y) {
for (ll i = 1; i <= n; i++) {
fill(dist[i] + 1, dist[i] + n + 1, inf);
visited[i] = 0;
}
dist[x][y] = 0;
priority_queue < pair <ll , pii > > pq;
pq.push({0, {x, y}});
while (!pq.empty()) {
ll x = pq.top().S.F;
ll y = pq.top().S.S;
pq.pop();
if (!visited[x][y]) {
visited[x][y] = 1;
for (ll i = 0; i < 26; i++) {
for (pii u : g[x][i]) {
for (pii v : g[y][i]) {
if (dist[x][y] + u.S + v.S < dist[u.F][v.F]) {
dist[u.F][v.F] = dist[x][y] + u.S + v.S;
pq.push({-dist[u.F][v.F], {u.F, v.F}});
}
}
}
}
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// cin >> T;
T = 1;
while (T--) {
cin >> n >> m/* >> s >> t*/;
for(ll i = 1; i <= n; i++) {
for (ll j = 0; j < 26; j++) {
g[i][j].clear();
}
}
for(ll i = 0; i < m; i++) {
cin >> u >> v /*>> w*/ >> c;
g[u][int(c) - 97].push_back({v, 1});
g[v][int(c) - 97].push_back({u, 1});
}
ans = inf;
solve(1, n);
for (ll i = 1; i <= n; i++) {
ans = min(ans, dist[i][i]);
}
for (ll u = 1; u <= n; u++) {
for (ll j = 0; j < 26; j++) {
for (pii v : g[u][j]) {
ans = min(ans, dist[u][v.F] + v.S);
}
}
}
if(ans == inf)ans = -1;
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
void get(ll i, ll arr[], ll n, ll xo, ll o, ll &ans)
{
if(i == n)
{
ans = min(ans, xo^o);
return;
}
get(i+1, arr, n, xo, o|arr[i], ans);
get(i+1, arr, n, xo^o, arr[i], ans);
}
const ld PI = acos(-1);
ll n;
ll get(ll i, ll j)
{
return i*n + j;
}
void dijk(vector<ll> v[], ll n, ll x, vector<ll> &dis)
{
dis[x] = 0;
queue<pii> q;
q.push(mp(0,x));
dis[x] = 0;
while(q.size() > 0)
{
ll temp = q.front().second;
q.pop();
for(auto it = v[temp].begin();it!=v[temp].end();it++)
{
ll v1 = *it;
ll wt = 1;
if(dis[v1]>dis[temp] + wt)
{
dis[v1] = dis[temp] + wt;
q.push({dis[v1],v1});
}
}
}
}
int main()
{
FAST/**/
ll m;
cin>>n>>m;
vector<pii> gr[n];
bool ter = 0;
rep(m)
{
ll a,b;
char c;
cin>>a>>b>>c;
a--, b--;
if(a>b)
swap(a,b);
if(a == 0 && b == n-1 && ter == false)
{
cout<<1<<'\n';
ter = true;
}
gr[a].pb({b,c});
gr[b].pb({a,c});
}
if(ter)
return 0;
ll lim = n*n;
vector<ll> tot[lim];
for(ll i=0;i<n;i++)
{
for(ll j=0;j<n;j++)
{
autoit(gr[i],it)
autoit(gr[j],it1)
{
if(it1->ss == it->ss)
tot[get(i,j)].pb(get(it->ff, it1->ff));
}
}
}
vector<ll> dis(lim,inf);
dijk(tot,lim,get(0,n-1),dis);
ll ans = inf;
rep(n)
ans = min(ans, 2*dis[get(i,i)]);
rep(n)
autoit(gr[i],it)
{
ans = min(ans, 1 + 2*dis[get(i,it->ff)]);
}
if(ans == inf)
ans = -1;
cout<<ans<<"\n";
return 0;
}
|
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
#include<string>
#define rep(i,p) for(long long int i=0;i<p;i++)
#define reep(i,p) for(long long int i=1;i<=p;i++)
#define ll long long
#define mod 1000000007
using namespace std;
//---------------------約数の列挙
vector<long long int> divisor(long long int N){
vector<long long int> p;
for(long long int q=1;q*q<=N;q++){
if(N%q == 0){
p.push_back(q);
if(N/q != q){
p.push_back(N/q);
}
else{}
}
else{}
}
sort(p.begin(),p.end());
return p;
}
int main(){
ll int S,P;
cin >> S >> P;
if(1+P==S){
cout << "Yes";
return 0;
}
vector<ll int > a;
a = divisor(P);
ll int i=0;
ll int j=0;
ll int N ;
N = a.size();
while(i < sqrt(P) || j!=N){
i=a[j];
if(i + (P/i) == S){
cout << "Yes";
return 0;
}
else{}
j+=1;
}
cout << "No";
return 0;
}
| /*
{
######################
# Author #
# Gary #
# 2021 #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
// int x=0;
// char ch=getchar();
// while(ch<'0'||ch>'9'){
// ch=getchar();
// }
// while(ch>='0'&&ch<='9'){
// x=(x<<1)+(x<<3)+(ch^48);
// ch=getchar();
// }
// return x;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
const int MAXN=2e5+233;
int k,n;
vector<int> g[MAXN];
struct inf{
int cnt,dis,ty;
inf(){cnt=dis=ty=0;}
inf(int A,int B,int C){cnt=A,dis=B,ty=C;}
};
int D;
inf operator + (inf A,inf B){
if(A.ty&&B.ty) return inf(A.cnt+B.cnt,max(A.dis,B.dis),1);
if(!A.ty&&!B.ty) return inf(A.cnt+B.cnt,min(A.dis,B.dis),0);
if(A.ty>B.ty) swap(A,B);
if(A.dis+B.dis<=D) return inf(A.cnt+B.cnt,A.dis,0);
return inf(A.cnt+B.cnt,B.dis,1);
}
inf get(int now,int fa){
inf ret;
if(g[now].size()==1&&fa){
return inf(0,0,1);
}
bool ok=0;
for(auto it:g[now]){
if(it!=fa){
auto Tmp=get(it,now);
Tmp.dis++;
if(!ok) ret=Tmp;
else ret=ret+Tmp;
ok=1;
}
}
if(II(ret.dis,ret.ty)==II(D,1)){
ret=inf(ret.cnt+1,0,0);
}
if(II(ret.dis,ret.ty)==II(D+1,0)){
ret=inf(ret.cnt,0,1);
}
// cout<<now<<" "<<ret.cnt<<' '<<ret.dis<<' '<<ret.ty<<endl;
return ret;
}
bool check(int d){
D=d;
auto it=get(1,0);
return it.cnt+it.ty<=k;
}
int main(){
scanf("%d%d",&n,&k);
rb(i,1,n-1){
int u,v;
scanf("%d%d",&u,&v);
g[u].PB(v);
g[v].PB(u);
}
int l=1,r=n;
while(l<r){
int mid=(l+r)>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<l<<endl;
return 0;
} |
#include <iostream>
using namespace std;
int main(void) {
int N, A, B;
cin >> N >> A >> B;
N -= A;
if (N < 0) {
N = 0;
}
N += B;
cout << N << endl;
return 0;
} | #include <iostream>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b>>c;
d=a-b+c;
cout<<d;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 100;
char s[N][N];
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
int solve() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%s", s[i]);
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
int cnt = 0;
int orient = 0;
for (int k = 0; k < 4; ++k) {
int nx = i + dx[k], ny = j + dy[k];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (s[i][j] != s[nx][ny]) {
orient |= 1 << (k & 1);
++cnt;
}
}
if (cnt == 2 && orient == 3) ++ans;
else if (cnt == 3) ans += 2;
else if (cnt == 4) ans += 4;
}
}
printf("%d\n", ans);
return 0;
}
int main() {
int t = 1;
// scanf("%d", &t);
for (int tc = 0; tc < t; ++tc) {
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
//UnionFindテンプレ
class UnionFind {
// まとめる 判定 サイズを知る
public:
// Aから見た親の番号を格納する。rootだったら-1*その集合のサイズ。
vector<int> Parent;
// 初期化
UnionFind(int N) {
Parent = vector<int>(N, -1);
}
// Aのrootを調べる
int root(int A) {
if (Parent[A] < 0) return A; // マイナスならそれはroot
return Parent[A] = root(Parent[A]);
}
// rootの値をプラスに戻して返す(サイズ)
int size(int A) {
return -Parent[root(A)];
}
// くっつける関数
bool connect(int A, int B) {
// AとBのroot同士をくっつける
A = root(A); // ここのAは、"rootの場所"の意味
B = root(B);
if (A == B) return false; // 既にくっついている
if (size(A) < size(B))
swap(A, B); // 大きい方にくっつけるために中身交換
Parent[A] += Parent[B]; // 中身更新
Parent[B] = A;
return true;
}
//連結か調べる関数
bool issame(int A, int B) {
return root(A) == root(B);
}
};
//chmin,chmax関数(DP用)
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;
}
//aのZ/mZでの逆元
int64_t modinv(int64_t a, int64_t m) {
int64_t b = m, u = 1, v = 0;
while (b) {
int64_t t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
//最大公約数
int64_t gcd(int64_t a, int64_t b) {
if (a % b == 0) {
return b;
}
return gcd(b, a%b);
}
int64_t lcm(int64_t a, int64_t b) {
return a*b/gcd(a,b);
}
//エラストテレスのふるい
vector<bool> make_is_prime(int N) {
vector<bool> prime(N + 1, true);
if (N >= 0) prime[0] = false;
if (N >= 1) prime[1] = false;
for (int i = 2; i * i <= N; i++) {
if (!prime[i]) continue;
for (int j = i * i; j <= N; j += i) {
prime[j] = false;
}
}
return prime;
}
//累乗a^b
int64_t pow(int a, int b) {
int64_t ans = (int64_t)1;
for (int i = 0; i < b; i++) {
ans *= (int64_t)a;
}
return ans;
}
//ここから
int main() {
int H,W; cin>>H>>W;
vector<vector<char>> map(H,vector<char>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin>>map.at(i).at(j);
}
}
int ans = 0;
for (int i = 0; i < H-1; i++) {
for (int j = 0; j < W-1; j++) {
int cnt = 0;
if (map.at(i).at(j) == '#') cnt++;
if (map.at(i+1).at(j) == '#') cnt++;
if (map.at(i).at(j+1) == '#') cnt++;
if (map.at(i+1).at(j+1) == '#') cnt++;
if (cnt == 1 || cnt == 3) ans++;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll m;
int A[62],n;
bool judge(ll x){
ll ret = 0,bs = 1;
for(int i(0);i <= n;++i){
ll sum = bs*A[i];
if(sum > m) return 0;
if(ret+sum > m) return 0;
ret += sum;
if(i != n){
if(m/x < bs) return 0;
bs = bs*x;
if(bs > m) return 0;
}
}
return 1;
}
int main(){
#ifdef LZX_CAI
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
string S; cin >> S;
n = S.size()-1;
int mx = 0;
for(int i(0);i <= n;++i){
A[i] = S[n-i]-'0';
mx = max(mx,S[n-i]-'0');
}
cin >> m;
if(n == 0){
int x = A[0];
if(m < x) return puts("0"),0;
else return puts("1"),0;
}
ll R = m,Ans = mx,L = mx+1;
while(L <= R){
ll mid = (L+R)/2;
if(judge(mid)) Ans = mid,L = mid+1;
else R = mid-1;
}
cout << Ans-mx << endl;
return 0;
} | /*
このコード、と~おれ!
Be accepted!
∧_∧
(。・ω・。)つ━☆・*。
⊂ ノ ・゜+.
しーJ °。+ *´¨)
.· ´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·'* ☆
*/
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
////多倍長整数, cpp_intで宣言
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
//
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a) //少数出力
#define printLdb(a) printf("%.50Lf\n", a) //少数出力
#define printdbd(a) printf("%.16lf\n", a) //少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str()) //string出力
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d)) /*(a, b) から (c, d) のマンハッタン距離 */
#define inf numeric_limits<double>::infinity();
#define linf numeric_limits<long double>::infinity()
using ll = long long;
using ull = unsigned long long;
constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);
using namespace std;
void scans(string& str) {
char c;
str = "";
scanf("%c", &c);
if (c == '\n')scanf("%c", &c);
while (c != '\n' && c != -1 && c != ' ') {
str += c;
scanf("%c", &c);
}
}
void scanc(char& str) {
char c;
scanf("%c", &c);
if (c == -1)return;
while (c == '\n') {
scanf("%c", &c);
}
str = c;
}
double acot(double x) {
return PI / 2 - atan(x);
}
ll LSB(ll n) { return (n & (-n)); }
template<typename T>
inline T chmin(T& a, const T& b) {
if (a > b)a = b;
return a;
}
template<typename T>
inline T chmax(T& a, const T& b) {
if (a < b)a = b;
return a;
}
////atcoder library
//#include <atcoder/all>
//using namespace atcoder;
/*-----------------------------------------ここからコード-----------------------------------------*/
int main() {
int n, a, b;
scanf("%d%d%d", &n, &a, &b);
printf("%d\n", n - a + b);
Please AC;
}
|
// {{{ by unolight
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i,a,b) for (int i = a; i <= (int)(b); i++)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
namespace { namespace unolight {
#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif
template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template <class T, class F = less<T>>
void sort_uniq(vector<T> &v, F f = F()) {
sort(begin(v), end(v), f);
v.resize(unique(begin(v), end(v)) - begin(v));
}
// }}}
// {{{ ModInt
template<int _MOD>
struct ModInt {
static const auto MOD = _MOD;
template<class T> using integral_only = typename enable_if<is_integral<T>::value>::type;
int x;
constexpr ModInt() : x() {}
template<class T, integral_only<T>* = nullptr>
ModInt(T _x=0) {
x = _x % MOD;
if(x<0) x+=MOD;
}
ModInt operator-() const { return {x == 0? 0 : MOD-x}; }
ModInt& operator+=(const ModInt a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
ModInt& operator-=(const ModInt a) {
if ((x += MOD-a.x) >= MOD) x -= MOD;
return *this;
}
ModInt& operator*=(const ModInt a) {
x = (long long)x * a.x % MOD;
return *this;
}
ModInt& operator/=(const ModInt a) {
return (*this) *= a.inv();
}
ModInt operator+(const ModInt a) const { return ModInt(*this) += a; }
ModInt operator-(const ModInt a) const { return ModInt(*this) -= a; }
ModInt operator*(const ModInt a) const { return ModInt(*this) *= a; }
ModInt operator/(const ModInt a) const { return ModInt(*this) /= a; }
ModInt inv() const {
// for prime MOD
return pow(MOD-2);
}
ModInt inv2() const {
// work for non-prime MOD if gcd(x,MOD) = 1
int a = x, b = MOD, u = 1, v = 0;
while(b != 0) {
int t = a / b;
a -= t * b;
u -= t * v;
swap(a, b);
swap(u, v);
}
return u;
}
template<class T, integral_only<T>* = nullptr>
ModInt pow(T t) const {
ModInt r = 1, p = *this;
while(t) {
if(t & 1) r *= p;
p *= p;
t >>= 1;
}
return r;
}
bool operator==(ModInt rhs) const { return x == rhs.x; }
bool operator!=(ModInt rhs) const { return x != rhs.x; }
bool operator<(ModInt rhs) const { return x < rhs.x; }
bool operator<=(ModInt rhs) const { return x <= rhs.x; }
bool operator>(ModInt rhs) const { return x > rhs.x; }
bool operator>=(ModInt rhs) const { return x >= rhs.x; }
friend string to_string(ModInt i) { return to_string(i.x); }
friend istream& operator>>(istream&i,ModInt&a) { return i>>a.x; }
friend ostream& operator<<(ostream&o,const ModInt&a) { return o<<a.x; }
};
const int MOD=998244353;
using mint=ModInt<MOD>;
// }}}
void main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
REP(i,n) cin >> a[i];
vector<mint> s(k+1); // sum(ai^k)
vector<vector<mint>> c(k+1, vector<mint>(k+1));
c[0][0]=1;
REP(i,k) {
REP(j,i+1) {
c[i+1][j]+=c[i][j];
c[i+1][j+1]+=c[i][j];
}
}
REP(i,n) {
mint x=1;
REP(j,k+1) {
s[j]+=x;
x*=a[i];
}
}
REP1(i,1,k) {
mint ans=0;
REP(j,i+1) {
ans+=c[i][j]*s[j]*s[i-j];
ans-=c[i][j]*s[i];
}
cout << ans/2 << '\n';
}
}
// {{{ main
}}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
unolight::main();
return 0;
}
// }}} | #include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int K=1e5;
long long w[K+10];
priority_queue<pair<long long,int>> pq;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int k;
long long n,m;
cin>>k>>n>>m;
int lft=m;
for(int i=1;i<=k;i++)
{
long long a;
cin>>a;
w[i]=(a*m)/n;
lft-=w[i];
pq.push({abs(w[i]*n-a*m)-abs((w[i]+1)*n-a*m),i});
}
for(;lft>0;lft--)
{
w[pq.top().se]++;
pq.pop();
}
for(int i=1;i<=k;i++)
cout<<w[i]<<" ";
cout<<"\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll z = 0, mod = 1000000007;
int main(){
ll N;
cin >> N;
if (N == 1){
cout << 1 << endl;
cout << 1 << endl;
exit(0);
}
vector<ll> fib(2);
fib[0] = 1;
fib[1] = 1;
ll now = 2;
while (true){
ll next = fib[now - 1] + fib[now - 2];
if (next > N && now % 2 == 0) break;
now++;
fib.push_back(next);
}
vector<ll> num(fib.size(), 0);
ll ope = (ll)(fib.size()) - 1;
for (ll i = (ll)(fib.size()) - 1; i >= 0; i--){
num[i] = N / fib[i];
N -= num[i] * fib[i];
ope += num[i];
//cout << fib[i] << ' ' << num[i] << endl;
}
cout << ope << endl;
ll chnum = (ll)(num.size()) - 1;
for (ll i = 0; i < chnum; i++){
for (ll k = 0; k < num[chnum - 1 - 2 * (i / 2)]; k++){
cout << 1 << endl;
num[chnum - 1 - 2 * (i / 2)] = 0;
}
for (ll k = 0; k < num[chnum - 2 * ((i + 1) / 2)]; k++){
cout << 2 << endl;
num[chnum - 2 * ((i + 1) / 2)] = 0;
}
if (i % 2 == 0){
cout << 3 << endl;
}
else{
cout << 4 << endl;
}
}
} |
#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;
ll INF = 1e16;
int inf = 1e9;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = ((n) - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
// inputs
#define VLLIN(x, n) vll x(n); REP(i, n) cin >> x[i];
#define VIIN(x, n) vi x(n); REP(i, n) cin >> x[i];
#define VLDIN(x, n) vld x(n); REP(i, n) cin >> x[i];
#define SIN(s, n) string s; cin >> s;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() {
cerr << endl;
}
template <typename H, typename... T>
void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T>
void print(T x) {
cout << x << "\n";
}
template <typename T>
void print(vector<T>& x) {
int N = x.size();
REP(i, N) {
if (i > 0) cout << " ";
cout << x[i];
}
cout << "\n";
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll a, b, c; cin >> a >> b >> c;
if (a == b) {
print("=");
return 0;
}
if (abs(a) == abs(b) && ~c & 1) {
print("=");
return 0;
}
if (a > 0 && b > 0) {
if (a > b) {
print(">");
} else {
print("<");
}
} else if (a < 0 && b < 0) {
a = abs(a), b = abs(b);
if (c & 1) {
// negative
if (a > b) {
print("<");
} else {
print(">");
}
} else {
// positive
if (a > b) {
print(">");
} else {
print("<");
}
}
} else {
if (~c & 1) {
a = abs(a), b = abs(b);
}
if (a > b) {
print(">");
} else {
print("<");
}
}
}
|
/*
P_r_A_d_Y ☹
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
#define mod 1000000007
#define inf 1e18
#define pb push_back
#define all(x) x.begin(),x.end()
#define arr_max(x) *max_element(x.begin(),x.end())
#define arr_min(x) *min_element(x.begin(),x.end())
#define pii pair<int,int>
#define ff first
#define ss second
#define vec vector<int>
const int N = 300005;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
bool cmp(vector<int>a , vector<int>b) {
return a[0] < b[0];
}
int poww(int x, int n)
{
if (n == 0 ) return 1 ;
int u = poww(x, n / 2);
u = (u * u) ;
if (n % 2 == 1)u = (u * x) ;
return u;
}
//(x**n)%m
int mod_pow(int x, int n, int m)
{
if (n == 0 ) return 1 % m;
int u = mod_pow(x, n / 2, m);
u = (u * u) % m;
if (n % 2 == 1)u = (u * x) % m;
return u;
}
int query(int l, int r)
{
int x;
cout << "? " << l << " " << r << endl;
cin >> x;
return x;
}
// int xd[4] = {1, 0, -1, 0};
// int yd[4] = {0, -1, 0, 1};
int help(int n)
{
int count_pairs = 0;
for (int i = 1; i <= sqrt(n - 1); ++i) {
for (int j = i; j * i < n; ++j)
++count_pairs;
}
count_pairs *= 2;
count_pairs -= (int)(sqrt(n - 1));
return count_pairs;
}
int32_t main()
{
fastIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
t = 1;
//cin >> t;
int ert = 1;
while (t--)
{
int k; cin >> k;
int ans = 0;
for (int i = 1; i <= k; i++)
{
int r = k / i;
if (r == 0)
break;
int g = help(r + 1);
//cout << r << " " << g << endl;
ans += g;
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i, a, b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
int main() {
string S;
cin >> S;
bool is_reversed = false;
deque<char> q;
for(auto s: S) {
if (s == 'R') is_reversed = !is_reversed;
else {
if (is_reversed) {
// 反転されているときは先頭に追加する
q.push_front(s);
}
else {
// そうでないときは末尾に追加する
q.push_back(s);
}
}
}
if (is_reversed) {
reverse(q.begin(), q.end());
}
string T;
for(char c : q) {
if (T.size() && T.back() == c) T.pop_back();
else T.push_back(c);
}
cout << T << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long int
#define pb push_back
#define mp make_pair
#define FIO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(in) in.begin(), in.end()
typedef pair<ll, ll> pll;
void solve(){
ll n; cin >> n;
ll ans = 0;
vector<ll> a(n), b(n), even, odd;
for(int i = 0 ; i < n ; i++){
cin >> a[i]; ans += a[i];
}
for(int i = 0 ; i < n ; i++){
cin >> b[i];
}
for(int i = 0 ; i < n ; i++){
ll x = b[i] - a[i];
if(i&1){
even.pb(x);
}else{
odd.pb(x);
}
}
sort(all(even));
sort(all(odd));
for(int i = even.size()-1 ; i >= 0 ; i--){
ll x = even[i] + odd[i];
if(x < 0){
break;
}
ans += x;
}
cout << ans << endl;
}
int main(){
FIO;
solve();
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#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("")
#define v(T) vector<T>
#define vv(T) v(v(T))
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;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const v(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.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
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 v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;}
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;
int main() {
int n;
scanf("%d",&n);
vi a(n), b(n);
cin>>a>>b;
ll ans = 0;
rep(i,n) {
if (i%2) ans += b[i], a[i] = a[i]-b[i];
else ans += a[i], a[i] = b[i]-a[i];
}
sort(rrng(a));
rep(i,n/2) ans += a[i];
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define ll long long
using namespace std;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
bool vis[200];
int main(){
ll t=read(),n=read()-1;
rep(i,1,100)vis[i*(100+t)/100]=1;
int c=0;ll ans=0;
rep(i,1,100+t){
if(!vis[i]){
if(c==n%t){
ans=i;
}
c++;
}
}
ans+=1ll*(100+t)*(n/t);
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define MP make_pair
#define PB push_back
#define X first
#define Y second
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define ALL(a) a.begin(), a.end()
#define SZ(a) (int)((a).size())
#define FILL(a, value) memset(a, value, sizeof(a))
#define debug(a) cerr << #a << " = " << a << endl;
template<typename T> void setmax(T& x, T y) {x = max(x, y);}
template<typename T> void setmin(T& x, T y) {x = min(x, y);}
template<typename T> void print(const T& a, ostream& out){
for(auto i: a) out << i << ' ';
out << endl;
}
const double PI = acos(-1.0);
const LL INF = 1e9 + 47;
const LL LINF = INF * INF;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
namespace IntModulo
{
const int mod = 998244353;
inline int add(int x, int y, int m = mod)
{
if (x + y < m)
return x + y;
return x + y - m;
}
inline int sub(int x, int y, int m = mod)
{
if (x >= y)
return x - y;
return x - y + m;
}
inline int mult(int x, int y, int m = mod)
{
return x * (LL) y % m;
}
inline int power(int x, LL y, int m = mod)
{
int r = 1;
while(y)
{
if (y & 1)
r = mult(r, x, m);
x = mult(x, x, m);
y >>= 1;
}
return r;
}
inline int inverse(int x, int m = mod)
{
return power(x, m - 2, m);
}
inline void ADD(int& x, int y, int m = mod){
x += y;
if (x >= m) x -= m;
}
inline void SUB(int& x, int y, int m = mod){
x -= y;
if (x < 0) x += m;
}
inline void MULT(int& x, int y, int m = mod){
x = (x * (LL) y) % m;
}
};
using namespace IntModulo;
const int N = 64;
int n, k, a[N][N], f[N];
int solve(vector<vector<int>>& g){
int C = 0;
vector<int> used(n, 0);
function<void(int)> dfs = [&](int v){
C++;
used[v] = 1;
for(auto i: g[v]){
if (!used[i]){
dfs(i);
}
}
};
vector<int> res;
FOR(i, 0, n) if (!used[i]){
dfs(i);
res.PB(C);
C = 0;
}
int ans = 1;
for(auto i: res){
MULT(ans, f[i]);
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
f[0] = 1;
FOR(i, 1, N) f[i] = mult(f[i - 1], i);
cin >> n >> k;
FOR(i, 0, n) FOR(j, 0, n) cin >> a[i][j];
vector<vector<int>> row(n), column(n);
FOR(i, 0, n){
FOR(j, 0, i){
bool ok = true;
FOR(x, 0, n){
ok &= a[i][x] + a[j][x] <= k;
}
if (ok){
row[i].PB(j);
row[j].PB(i);
}
}
}
FOR(i, 0, n){
FOR(j, 0, i){
bool ok = true;
FOR(x, 0, n){
ok &= a[x][i] + a[x][j] <= k;
}
if (ok){
column[i].PB(j);
column[j].PB(i);
}
}
}
int R = solve(row);
int C = solve(column);
cout << mult(R, C) << endl;
cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
return 0;
}
|
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define FILL(i,n) memset(i,n,sizeof i)
#define X first
#define Y second
#define ET cout << "\n"
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef bbq
#define debug(...) {\
fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
_do(__VA_ARGS__);\
}
#define DB(a,s,e) {for(int _i=s;_i<e;++_i) cerr << a[_i] << " ";cerr << "\n";}
template<typename T>void _do(T &&x){cerr<<x<<endl;}
template<typename T,typename ...S> void _do(T &&x,S &&...t){cerr<<x<<", ";_do(t...);}
template<typename a,typename b> ostream& operator << (ostream &s,const pair<a,b> &p){return s<<"("<<p.X<<","<<p.Y<<")";}
#else
#define debug(...)
#define DB(a,s,e)
#endif
pii edge[20000];
int c[20000], dir[20000], boss[20000], vis[20000], dft;
vector<int> G[20000];
void dfs(int u, int f) {
vis[u] = ++dft;
for (int i : G[u]) {
if (i == f) continue;
int type, v;
if (edge[i].X == u)
type = 1, v = edge[i].Y;
else
type = 2, v = edge[i].X;
if (!vis[v]) {
dir[i] = type;
dfs(v, i);
}
else if (vis[u] > vis[v])
dir[i] = type;
}
}
int finds(int x) {
if (x == boss[x])
return x;
return boss[x] = finds(boss[x]);
}
int main() {
IOS();
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i)
cin >> edge[i].X >> edge[i].Y;
for (int i = 1; i <= n; ++i)
cin >> c[i], boss[i] = i;
for (int i = 1; i <= m; ++i)
if (c[edge[i].X] == c[edge[i].Y]) {
boss[finds(edge[i].X)] = finds(edge[i].Y);
G[edge[i].X].pb(i);
G[edge[i].Y].pb(i);
}
else if (c[edge[i].X] > c[edge[i].Y])
dir[i] = 1;
else
dir[i] = 2;
for (int i = 1; i <= n; ++i)
if (!vis[finds(i)])
dfs(finds(i), 0);
for (int i = 1; i <= m; ++i)
if (dir[i] == 1)
cout << "->\n";
else if (dir[i] == 2)
cout << "<-\n";
else
assert(0);
}
| #include "bits/stdc++.h"
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define mp make_pair
#define FI first
#define SE second
#define maxn 10000
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef double db;
vi to[maxn+5];
pii e[maxn+5];
int c[maxn+5];
bool ans[maxn+5];
int dep[maxn+5];
void dfs(int now,int fa)
{
for(auto i: to[now])
{
int v=now^e[i].FI^e[i].SE;
if(c[v]!=c[now] || v==fa) continue;
if(dep[v]==0)
{
if(e[i].FI==now) ans[i]=1;
dep[v]=dep[now]+1;
dfs(v,now);
}
else if(dep[v]<dep[now])
{
if(e[i].FI==now) ans[i]=1;
}
}
}
int main()
{
int n,m; scanf("%d%d",&n,&m);
rep(i,1,m) scanf("%d%d",&e[i].FI,&e[i].SE),to[e[i].FI].pb(i),to[e[i].SE].pb(i);
rep(i,1,n) scanf("%d",&c[i]);
rep(i,1,m) if(c[e[i].FI]>c[e[i].SE]) ans[i]=1;
rep(i,1,n) if(dep[i]==0) dep[i]=1,dfs(i,0);
rep(i,1,m) if(ans[i]) puts("->"); else puts("<-");
return 0;
} |
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <bitset>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>
#include <algorithm>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <ctime>
#include <chrono>
/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
*/
#ifdef LOCAL
#define dbg(x) cout << #x << " : " << x << endl;
#else
#define dbg(x)
#endif
#define int long long
#define pb push_back
#define ppb pop_back()
#define mp make_pair
#define fi(a, b) for (int i = a; i < b; i++)
#define fj(a, b) for (int j = a; j < b; j++)
#define fk(a, b) for (int k = a; k < b; k++)
#define fi1(a, b) for (int i = a - 1; i >= b; i--)
#define fj1(a, b) for (int j = a - 1; j >= b; j--)
#define fk1(a, b) for (int k = a - 1; k >= b; k--)
#define fx(x, a) for (auto& x : a)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rep1(i, a, b) for (int i = a - 1; i >= b; --i)
#define siz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2>inline void mine(T1 &x, const T2 &y) { if (y < x) x = y; }
template<typename T1, typename T2>inline void maxe(T1 &x, const T2 &y) { if (x < y) x = y; }
ostream& operator << (ostream &out, const vector<int> &b) {
for (auto k : b) out << k << ' ';
return out;
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef char ch;
typedef string str;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<ch> vch;
typedef vector<vch> vvch;
typedef vector<str> vs;
const int MOD = 1000000007;
const int INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const int MX = 200010;
const double EPS = 1e-9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vi g[MX];
int turn[MX];
int c[MX];
vpii edges;
int used[MX];
int dp[MX];
void make(int id, int a, int b) {
if (edges[id].first == a)
turn[id] = 0;
else
turn[id] = 1;
}
void dfs(int v, int prid =-1) {
used[v] = 1;
fx(id, g[v]) {
if (id == prid) continue;
int to = edges[id].first ^ edges[id].second ^ v;
if (c[to] != c[v]) continue;
if (used[to]) {
if (dp[to] < dp[v]) {
make(id, v, to);
}
} else {
dp[to] = dp[v] + 1;
make(id, v, to);
dfs(to, id);
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
fi(0, m) {
int a, b;
cin >> a >> b;
--a, --b;
g[a].pb(i);
g[b].pb(i);
edges.pb({a, b});
turn[i] = -1;
}
vi p(n);
fi(0, n) {
cin >> c[i];
p[i] = i;
}
sort(all(p), [&](int i, int j) {
return c[i] < c[j];
});
for (int col = 2; col <= n; ++col) {
fi(0, n) {
if (!used[i] && c[i] == col) {
dfs(i);
}
}
}
fx(v, p) {
fx(id, g[v]) {
if (turn[id] == -1) {
int to = edges[id].first ^ edges[id].second ^ v;
assert(c[to] > c[v]);
make(id, to, v);
}
}
}
fi(0, m) {
assert(turn[i] != -1);
if (turn[i] == 0) {
cout << "->\n";
} else {
cout << "<-\n";
}
}
}
| #include<bits/stdc++.h>
using namespace std;
#define ma 2147483647
#define ll long long
#define ld long double
#define pii pair<int,int>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define um unordered_map
#define vi vector<int>
#define mii map<int,int>
#define si set<int>
#define ft first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) a*(b/gcd(a,b))
#define mmst(a,b) memset(a,b,sizeof(a))
int n,m;
int a[10005],b[10004],c[10005],ans[10005];
int tu[200][200];
void dfs(int x)
{
for (int i=1;i<=n;i++)
{
if (tu[x][i]!=0)
{
ans[ abs(tu[x][i]) ] = tu[x][i]/abs(tu[x][i]);
tu[x][i]=0;
tu[i][x]=0;
dfs(i);
}
}
}
int main()
{
//freopen("d.in","r",stdin);
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[i]=x,b[i]=y;
}
for (int i=1;i<=n;i++) scanf("%d",&c[i]);
for (int i=1;i<=m;i++)
{
if (c[a[i]]<c[b[i]]) ans[i]=-1;
if (c[a[i]]>c[b[i]]) ans[i]=1;
}
for (int i=1;i<=m;i++)
{
if ( c[a[i]] == c[b[i]] )
{
tu[a[i]][b[i]]=i;
tu[b[i]][a[i]]=-i;
}
}
for (int i=1;i<=n;i++)
{
dfs(i);
}
for (int i=1;i<=m;i++)
{
if (ans[i]<0) printf("<-");
if (ans[i]>0) printf("->");
printf("\n");
}
} |
#include <iostream>
#include <cmath>
#include <utility>
using namespace std;
#define mp make_pair
#define x first
#define y second
typedef pair<int,int> pii;
typedef long long ll;
pii tocke[1003];
int main(){
ios::sync_with_stdio(0);
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>tocke[i].x>>tocke[i].y;
}
int br=0;
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(abs(tocke[i].y-tocke[j].y)/abs(tocke[i].x-tocke[j].x)==0 or abs(tocke[i].y-tocke[j].y)==abs(tocke[i].x-tocke[j].x)) br++;
}
}
cout<<br;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
ll n;
cin>>n;
vector<pair<ll,ll>>v;
ll two=INT_MAX;
for(int i=0;i<n;i++){
ll a,b;
cin>>a>>b;
v.push_back(make_pair(a,b));
two=min(two,a+b);
}
ll first=INT_MAX;
ll second=INT_MAX;
ll f2=INT_MAX;
ll s2=INT_MAX;
ll i=0,index=0;
for(auto itr:v){
if(itr.first<first){
first=min(itr.first,first);
index=i;
}
i++;
}
i=0;
for(auto itr:v){
if(itr.second<second&&index!=i ){
second=min(itr.second,second);
}
i++;
}
i=0;
index=0;
for(auto itr:v){
if(itr.first<f2){
f2=min(itr.first,f2);
index=i;
}
i++;
}
i=0;
for(auto itr:v){
if(itr.second<s2&&index!=i ){
s2=min(itr.second,s2);
}
i++;
}
//cout<<first<<" "<<second<<" "<<f2<<" "<<s2<<" ";
ll tmp=min(max(first,second),max(f2,s2));
ll ans=min(two,tmp);
cout<<ans<<"\n";
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.