code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
#define fir first
#define sec second
const int N=1000005;
namespace io{
il char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
template <class I>
il void fr(I &num){
num=0;register char c=nc();it p=1;
while(c<'0'||c>'9') c=='-'?p=-1,c=nc():c=nc();
while(c>='0'&&c<='9') num=num*10+c-'0',c=nc();
num*=p;
}
char obuf[1000001],*os=obuf, *ot=obuf+1000001;
il void flush(){fwrite(obuf,1,os-obuf,stdout),os=obuf;}
il void ptc(const char x){*os++=x,os==ot?flush():void();}
template<class I>
il void wr(I x){
x<0?ptc('-'),x=-x:0;
static char qu[65];
char *tmp=qu;
do *tmp++=(x%10)^'0';while(x/=10);
while(tmp--!=qu) ptc(*tmp);
}
struct flusher{ ~flusher() { flush(); } }_;
}
using namespace io;
int n,a[N],lim;
vector<ll> b;
ll ans,K;
template<class I>
il void ckMax(I&p,I q){p=(p>q?p:q);}
il void dfs1(ct x,cll num){
if(x>lim) return b.push_back(num),void();
dfs1(x+1,num),dfs1(x+1,num+a[x]);
}
il void dfs2(ct x,cll num){
//printf("x=%d num=%lld\n",x,num);
//if(x>n&&num==a[7]) printf("%lld\n",*prev(std::upper_bound(b.begin(),b.end(),K-num)));
if(x>n) return num<=K?ckMax(ans,num),0:0,K-num>=b[0]?ckMax(ans,*prev(std::upper_bound(b.begin(),b.end(),K-num))+num),0:0,void();
dfs2(x+1,num),dfs2(x+1,num+a[x]);
}
int main(){
scanf("%d%lld",&n,&K);it i;
for(i=1;i<=n;++i) scanf("%d",&a[i]);
ans=0,lim=n>>1,dfs1(1,0),std::sort(b.begin(),b.end()),dfs2(lim+1,0);
//for(const auto &i : b) printf("%lld ",i);puts("");
printf("%lld",ans);
return 0;
} | #include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
const ll INF = 1ll<<60;
class SegmentTree {
ll n;
// parent of k : (k-1)/2, child of k : 2*k+1, 2*k+2
vector<ll> node;
public:
SegmentTree(vector<ll> a){
ll n_ = a.size();
// round to power of 2
n = 1;
while(n < n_) n *= 2;
// initialize tree
node.resize(2*n-1, INF);
// fill data into tree
for (ll i = 0; i < n_; i++) node[n-1+i] = a[i];
for (ll i = n-2; i >= 0; i--) node[i] = min(node[2*i+1], node[2*i+2]);
}
// [3, 4, 2, 1] RMQ(0, 3) -> 2, RMQ(0, 4) -> 1
ll RMQ(ll a, ll b, ll k=0, ll l=0, ll r=-1){
if (r<0) r = n;
if (r<=a || b<=l) return INF;
if (a<=l && r<=b) return node[k];
ll vl = RMQ(a, b, 2*k+1, l, (l+r)/2);
ll vr = RMQ(a, b, 2*k+2, (l+r)/2, r);
return min(vl, vr);
}
// k番目をvにする
void update(ll k, ll v){
k += n-1;
node[k] = v;
while(k>0){
k = (k-1)/2;
node[k] = min(node[k*2+1], node[k*2+2]);
}
}
};
int main(){
ll n;
cin >> n;
vector<ll> a(n), b(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
ll now = 0;
ll amax = 0;
rep(i, n){
amax = max(amax, a[i]);
now = max(now, amax*b[i]);
cout << now << endl;
}
return 0;
} |
#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = long long; using ull = unsigned long long; using pll = pair<ll,ll>; const ll INF = 4e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
// clang-format on
ll gcdf(ll a, ll b) {
if (b == 0) {
return a;
}
return gcdf(b, a % b);
}
vector<ll> divisor(ll n) {
vector<ll> res;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i != n / i) {
res.push_back(n / i);
}
}
}
return res;
}
int main() {
ll N;
cin >> N;
vector<ll> a(N);
ll allmin = INF;
for (ll i = 0; i < N; i++) {
cin >> a[i];
allmin = min(a[i], allmin);
}
unordered_map<ll, ll> curgcd;
for (auto ai : a) {
vector<ll> succeeded;
for (auto d : divisor(ai)) {
if (d > allmin) continue;
if (curgcd.count(d)) {
curgcd[d] = gcdf(curgcd[d], ai);
} else {
curgcd[d] = ai;
}
}
}
ll ans = 0;
for (auto x : curgcd) {
ans += (x.first == x.second);
}
print(ans);
}
| ///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define u64 uint64_t
#define ll long long
#define endl "\n"
#define PI acos(-1)
#define fi first
#define si second
#define mkp make_pair
#define pb push_back
#define set0(arr) memset(arr, 0, sizeof(arr))
#define setinf(arr) memset(arr, 126, sizeof(arr))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define all(x) (x).begin(),(x).end()
#define sz(v) ((ll)(v).size())
#define mem(a,b) memset(a, b, sizeof(a))
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define mchk(mask,pos) (mask & (1<<pos))
#define mset(mask,pos) (mask bitor (1<<pos))
#define munset(mask,pos) (mask ^ (1<<pos))
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
using pll = pair<ll, ll> ;
using vl = vector<ll> ;
using vpll = vector<pll> ;
using mll = map<ll, ll> ;
using mcl = map<char, ll> ;
using msl = map<string,ll> ;
using sl = set<ll> ;
using sc = set<char> ;
using dl = deque<ll> ;
const int N = 1e6+5 ;
ll mod = 1e9+7 ;
vl adj[N] ;
vpll adjc[N] ; // Only for this N can't be greater than 1e6+5;segmentation fault;
ll vis[N] ;
ll arr[N] ;
ll sumd(ll a,ll b){return (((a%mod)+(b%mod))%mod);}
ll muld(ll a,ll b){return (((a%mod)*(b%mod))%mod);}
ll subd(ll a,ll b){return (((a%mod)-(b%mod)+mod)%mod);}
bool chk(ll ii,ll jj,ll nn,ll mm){if((0<=ii&&ii<nn)&&(0<=jj&&jj<mm)) return true;else return false;}
int main()
{
IOS;
ll a, b, c, d, n, m, p, x, y, z, i, j, k, f = 0, tc, cnt = 0, sum = 0, mul = 1, mi = 1e18, ma = -1e18, cs, q;
string str ;
char ch ;
double db ;
ll l, r ;
//code
cin>>k;
sum=0;
for(i=1;i<=k;i++)
{
for(j=1;j<=k;j++)
{
if(i*j>k) break;
sum+=(k/(i*j));
}
}
cout<<sum<<endl;
//code
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define rrep(i,a,b) for (int i = a - 1; i >= b; i--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ll long long
#define dbwt(x) std::cout << fixed << setprecision(15) << x << '\n'
#define Wt(x) cout << x << '\n'
#define re0 return 0
#define Int(...) int __VA_ARGS__; in(__VA_ARGS__)
#define Ll(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; in(__VA_ARGS__)
#define Vec(t,a,s) vector<t>a(s); in(a)
#define Vec2(t,a,b,s) vector<t>a(s),b(s);rep(i,0,s)in(a[i],b[i])
#define Vec3(t,a,b,c,s) vector<t>a(s),b(s),c(s);rep(i,0,s)in(a[i],b[i],c[i])
#define Vec4(t,a,b,c,d,s) vector<t>a(s),b(s),c(s),d(s);rep(i,0,s) in(a[i],b[i],c[i],d[i])
#define mvec(t,a,i) vector<t>(a,i)
#define mvec2(t,a,b,i) vector<vector<t>>(a,vector<t>(b,i))
void scan(int& x) {std::cin >> x;}
void scan(ll& x) {std::cin >> x;}
void scan(double& x) {std::cin >> x;}
void scan(std::string& x) {std::cin >> x;}
void scan(std::vector<int>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void scan(std::vector<ll>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void in(){}
template<class Head, class... Tail>
void in(Head& head, Tail&... tail) {scan(head); in(tail...);}
template<class T, class U>
bool chmax(T& a, U b) {if(a < b){a=b; return true;} return false;}
template<class T, class U>
bool chmin(T& a, U b) {if(a > b){a=b; return true;} return false;}
using namespace std;
template<class T>
using vvec = vector<vector<T>>;
template<class T>
void emitvec(vector<T> a) {rep (i, 0, a.size()) cout << a[i] << " "; cout << endl;}
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} iosetup;
//----------------------------------------------------------------//
int N, M;
map<pair<int, int>, int> mp;
vector<vector<int>> g(100005);
vector<int> col(100005, -1);
vector<bool> visited(100005, false);
void dfs(int v) {
visited[v] = true;
for (int to: g[v]) {
if (visited[to]) continue;
if (col[v] == mp[{v, to}]) {
col[to] = (col[v] == 1 ? N : 1);
}
else {
col[to] = mp[{v, to}];
}
dfs(to);
}
}
int main() {
cin >> N >> M;
Vec3(int, u, v, c, M);
rep (i, 0, M) {
mp[{u[i], v[i]}] = mp[{v[i], u[i]}] = c[i];
g[u[i]].pb(v[i]);
g[v[i]].pb(u[i]);
}
col[1] = 1;
dfs(1);
rep (i, 1, N + 1) cout << col[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
vector<vector<pair<int,int>>> ex(500000);
vector<bool> seen(500000,false);
vector<int> ans(500000,0);
void dfs(int now, int par, int c){
if(seen[now]) return;
seen[now] = true;
if(ans[par] != c) ans[now] = c;
else if(c == 0) ans[now] = 1;
for(auto p:ex[now]) dfs(p.first, now, p.second);
}
int main(){
int n,m;
cin >> n >> m;
for(int i = 0; i < m; i++){
int u,v,c;
cin >> u >> v >> c;
u--;v--;c--;
ex[u].push_back({v,c});
ex[v].push_back({u,c});
}
dfs(0,0,0);
for(int i = 0; i < n; i++) cout << ans[i]+1 << endl;
return 0;
} |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main()
{
int i1, j1, i2, j2;
cin >> i1 >> j1 >> i2 >> j2;
i2 -= i1;
j2 -= j1;
i1 = 0;
j1 = 0;
i2 = abs(i2);
j2 = abs(j2);
if (i2 == 0 && j2 == 0)
{
cout << 0 << endl;
return 0;
}
if (i2 + j2 <= 3 || i2 == j2)
{
cout << 1 << endl;
return 0;
}
if (i2 + j2 <= 6 || (i2 + j2) % 2 == 0 || abs(i2 - j2) <= 3)
{
cout << 2 << endl;
return 0;
}
cout << 3 << endl;
return 0;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int a,b,x,y;
int main(){
scanf("%d%d%d%d",&a,&b,&x,&y);
if(a>b)printf("%d",min((a-b-1)*y+x,x*(((a-b)<<1)-1)));
else printf("%d",min((b-a)*y+x,x*(((b-a)<<1)+1)));
return 0;
} |
// #include <iostream>
#include <map>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;
const int INF = 1000000001;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
const ll mod = 1000000007;
int main(){
int x,y;
cin >> x >> y;
if(max(x,y) < min(x,y) + 3) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
char c1, c2, c3;
cin >> c1 >> c2 >> c3;
cout << (c1 == c2 && c2 == c3 && c1 == c3 ? "Won" : "Lost") << '\n';
return 0;
} |
#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
#include <chrono>
#include <cassert>
#include <tuple>
#include <numeric>
#include <utility>
using namespace std;
namespace {
using Integer = long long; //__int128;
template<class T, class S> istream& operator >> (istream& is, pair<T,S>& p){return is >> p.first >> p.second;}
template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
template<class T> istream& operator , (istream& is, T& val){ return is >> val;}
template<class T, class S> ostream& operator << (ostream& os, const pair<T,S>& p){return os << p.first << " " << p.second;}
template<class T> ostream& operator << (ostream& os, const vector<T>& vec){for(size_t i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"":" "); return os;}
template<class T> ostream& operator , (ostream& os, const T& val){ return os << " " << val;}
template<class H> void print(const H& head){ cout << head; }
template<class H, class ... T> void print(const H& head, const T& ... tail){ cout << head << " "; print(tail...); }
template<class ... T> void println(const T& ... values){ print(values...); cout << endl; }
template<class H> void eprint(const H& head){ cerr << head; }
template<class H, class ... T> void eprint(const H& head, const T& ... tail){ cerr << head << " "; eprint(tail...); }
template<class ... T> void eprintln(const T& ... values){ eprint(values...); cerr << endl; }
class range{ Integer start_, end_, step_; public: struct range_iterator{ Integer val, step_; range_iterator(Integer v, Integer step) : val(v), step_(step) {} Integer operator * (){return val;} void operator ++ (){val += step_;} bool operator != (range_iterator& x){return step_ > 0 ? val < x.val : val > x.val;} }; range(Integer len) : start_(0), end_(len), step_(1) {} range(Integer start, Integer end) : start_(start), end_(end), step_(1) {} range(Integer start, Integer end, Integer step) : start_(start), end_(end), step_(step) {} range_iterator begin(){ return range_iterator(start_, step_); } range_iterator end(){ return range_iterator( end_, step_); } };
inline string operator "" _s (const char* str, size_t size){ return move(string(str)); }
constexpr Integer my_pow(Integer x, Integer k, Integer z=1){return k==0 ? z : k==1 ? z*x : (k&1) ? my_pow(x*x,k>>1,z*x) : my_pow(x*x,k>>1,z);}
constexpr Integer my_pow_mod(Integer x, Integer k, Integer M, Integer z=1){return k==0 ? z%M : k==1 ? z*x%M : (k&1) ? my_pow_mod(x*x%M,k>>1,M,z*x%M) : my_pow_mod(x*x%M,k>>1,M,z);}
constexpr unsigned long long operator "" _ten (unsigned long long value){ return my_pow(10,value); }
inline int k_bit(Integer x, int k){return (x>>k)&1;} //0-indexed
mt19937 mt(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
template<class T> string join(const vector<T>& v, const string& sep){ stringstream ss; for(size_t i=0; i<v.size(); i++){ if(i>0) ss << sep; ss << v[i]; } return ss.str(); }
inline string operator * (string s, int k){ string ret; while(k){ if(k&1) ret += s; s += s; k >>= 1; } return ret; }
}
constexpr long long mod = 9_ten + 7;
int main(){
int n, w;
cin >> n, w;
println(n/w);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
cout << n/m << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
const ll ll_inf = ll(1e9) * ll(1e9);
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define M 1000000007
struct UnionFind {
//自身が親であれば、その集合に属する頂点数に-1を掛けたもの
//そうでなければ親のid
vector<ll> r;
UnionFind(ll N) {
r = vector<ll>(N, -1);
}
ll root(ll x) {
if (r[x] < 0) return x;
return r[x] = root(r[x]);
}
bool unite(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (r[x] > r[y]) swap(x, y);
r[x] += r[y];
r[y] = x;
return true;
}
ll size(ll x) {
return -r[root(x)];
}
};
ll coin[101][101][101] = {0};
int main() {
ll a, b, x, y, d, s;
cin >> a >> b >> x >> y;
if (a > b) {
d = a - b;
if (2 * x < y) {
s = (2 * x*d) - x;
}
else {
s = (d - 1)*y + x;
}
}
else {
d = b - a;
if (2 * x < y) {
s = (2 * x*d) + x;
}
else {
s = d * y + x;
}
}
cout << s;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a,b,x,y;
cin>>a>>b>>x>>y;
int ans = x;
if(a>b)
{
for(int i=b; i<a-1; i++) ans+=min(2*x,y);
}
else
{
for(int i=a; i<b; i++) ans+=min(2*x,y);
}
cout<<ans<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
using ll = long long;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a) (a).begin(),(a).end()
void solve(vector<vector<int>> &A, vector<vector<int>> &B, int num){
vector<vector<int>> retA(num - 1), retB(num - 1);
For(i,0,A.size()){
retA[i] = A[i];
retA[i+A.size()] = A[i];
}
int idx = 0;
for(int i=0; i<A.size(); i++){
for(auto &e: A[i]) retA[idx].emplace_back(e + num / 2);
idx++;
}
for(int i=0; i<B.size(); i++){
for(auto &e: B[i]) retA[idx].emplace_back(e + num / 2);
idx++;
}
retA[num - 2].clear();
For(i,0,num/2){
retA[num - 2].emplace_back(i);
}
For(i,0,num - 1){
int t = 0;
for(auto &a: retA[i]){
while(t < a){
retB[i].emplace_back(t);
t++;
}
t++;
}
while(t < num){
retB[i].emplace_back(t);
t++;
}
}
swap(A, retA);
swap(B, retB);
}
void Print(vector<vector<int>> &A, vector<vector<int>> &B, int num){
cout << A.size() << endl;
For(i,0,A.size()){
string ret(num, 'B');
for(auto c:A[i]) ret[c] = 'A';
cout << ret << endl;
}
}
void Main(){
int tn;
cin >> tn;
int n = (1 << tn);
vector<vector<int>> A(1, {0}), B(1, {1});
for(int num = 2; num < n; num *= 2){
solve(A, B, num * 2);
}
/*
for(int i=0; i<A.size(); i++){
for(auto t:A[i]) cout << t << " ";
cout << "vs ";
for(auto t:B[i]) cout << t << " ";
cout << endl;
}
cout << A.size() << " " << B.size() << endl;
*/
Print(A, B, n);
}
void Editorial(){
int n;
cin >> n;
cout << ((1<<n)-1) << endl;;
For(i,1,1<<n){
For(j,0,1<<n){
cout << (__builtin_parity(i & j) ? 'A' : 'B');
}
cout << endl;
}
}
int main(){
// Main();
Editorial();
/*
東方風神録は神が出てくるので当然神ゲー
*/
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
int gcd(ll x, ll y){
if(x < y)swap(x, y);
if(x % y == 0)return y;
return gcd(y, x % y);
}
int extgcd(ll a, ll b, ll &x, ll &y){
int d = a;
if(b != 0){
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
}else{
x = 1; y = 0;
}
return d;
}
int main(){
int t;
cin >> t;
vector<ll> ans(t);
for(int i = 0; i < t; i++){
ll n, s, k;
cin >> n >> s >> k;
ll div = gcd(n, k);
if(s % div != 0){
ans[i] = -1;
continue;
}
n /= div;
s /= div;
k /= div;
ll x, y;
if(k > n) extgcd(k, n, x, y);
else extgcd(n, k, y, x);
ll m;
if(- s * x > 0){
if(s * x % n == 0){
m = - s * x / n - 1;
}else{
m = - s * x / n;
}
}else{
if(s * x % n == 0){
m = - s * x / n - 1;
}else{
m = - s * x / n - 1;
}
}
ans[i] = - n * m - s * x;
}
for(int i = 0; i < t; i++){
cout << ans[i] << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define MOD 998244353
#define int long long
#define ull unsigned long long
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ceil(x,y) (x+y-1)/y
#define deb(x) { cerr << #x << '=' << x << '\n'; }
#define INF 0x3f3f3f3f
using namespace std;
int cnt = 0;
vector<int> graphrow[100], graphcol[100];
bool used[100];
void dfs(int v, int n);
int fact(int n);
void solve(){
//code goes here
int n,x;
cin >> n >> x;
vector<int> a[n];
for (int i = 0; i<n; ++i){
a[i].resize(n);
for (int j = 0; j<n; ++j){
cin >> a[i][j];
}
}
for (int i = 0; i<n; ++i){
for (int j = i+1; j<n; ++j){
bool flag = 0;
for (int k = 0; k<n; ++k){
if (a[i][k] + a[j][k] > x){
flag = 1;
break;
}
}
if (!flag){
graphrow[i].pb(j);
graphrow[j].pb(i);
}
}
}
int res1 = 1, res2 = 1;
for (int i = 0; i<n; ++i){
if (!used[i]){
dfs(i,0);
res1 *= fact(cnt);
res1%=MOD;
cnt = 0;
}
}
memset(used,0,sizeof(used));
for (int i = 0; i<n; ++i){
for (int j = i+1; j<n; ++j){
bool flag = 0;
for (int k = 0; k<n; ++k){
if (a[k][i] + a[k][j] > x){
flag = 1;
break;
}
}
if (!flag){
graphcol[i].pb(j);
graphcol[j].pb(i);
}
}
}
for (int i = 0; i<n; ++i){
if (!used[i]){
dfs(i,1);
res2 *= fact(cnt);
res2%=MOD;
cnt = 0;
}
}
int res = res1*res2;
res%=MOD;
cout << res;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
void dfs(int v, int n){
used[v] = 1;
cnt++;
if (n==0){
for (auto i:graphrow[v]){
if (!used[i]) dfs(i, 0);
}
}
else{
for (auto i:graphcol[v]){
if (!used[i]) dfs(i, 1);
}
}
}
int fact(int n){
int res = 1;
if (n==0) return res;
for (int i = 1; i<=n; ++i){
res*=i;
res%=MOD;
}
return res;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)
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; }
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert
#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}
using namespace std;
template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}
//-------------------------------------------------
//--ModInt
//-------------------------------------------------
const uint_fast64_t MOD = 1e9+7;
class mint
{
private:
using Value = uint_fast64_t;
Value n;
public:
mint():n(0){}
mint(int_fast64_t _n):n(_n<0 ? MOD-(-_n)%MOD : _n%MOD){}
mint(const mint &m):n(m.n){}
friend ostream& operator<<(ostream &os, const mint &a){
return os << a.n;
}
friend istream& operator>>(istream &is, mint &a){
Value temp; is>>temp;
a = mint(temp);
return is;
}
mint& operator+=(const mint &m){n+=m.n; n=(n<MOD)?n:n-MOD; return *this;}
mint& operator-=(const mint &m){n+=MOD-m.n; n=(n<MOD)?n:n-MOD; return *this;}
mint& operator*=(const mint &m){n=n*m.n%MOD; return *this;}
mint& operator/=(const mint &m){return *this*=m.inv();}
mint& operator++(){return *this+=1;}
mint& operator--(){return *this-=1;}
mint operator+(const mint &m) const {return mint(*this)+=m;}
mint operator-(const mint &m) const {return mint(*this)-=m;}
mint operator*(const mint &m) const {return mint(*this)*=m;}
mint operator/(const mint &m) const {return mint(*this)/=m;}
mint operator++(int){mint t(*this); *this+=1; return t;}
mint operator--(int){mint t(*this); *this-=1; return t;}
bool operator==(const mint &m) const {return n==m.n;}
bool operator!=(const mint &m) const {return n!=m.n;}
mint operator-() const {return mint(MOD-n);}
mint pow(Value b) const {
mint ret(1), m(*this);
while(b){
if (b & 1) ret*=m;
m*=m;
b>>=1;
}
return ret;
}
mint inv() const {return pow(MOD-2);}
};
mint comb(ll a, ll b)
{
if (a<b) return mint(0);
mint nume = 1;
mint deno = 1;
rep(i,b){
nume*=a-i;
deno*=i+1;
}
return nume/deno;
}
//-------------------------------------------------
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
ll N,M; cin>>N>>M;
vll a(N);
rep(i,N) cin>>a[i];
ll sum = 0;
rep(i,N) sum+=a[i];
cout<<comb(N+M,sum+N)<<"\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << ", " << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "[";
bool first = true;
for (auto& v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template<typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "[";
bool first = true;
for (auto& v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "]";
return out;
}
template<typename T>
ostream& operator <<(ostream& out, const vector<vector<T>>& a) {
bool first = true;
for (auto& v : a) {
if (!first)
out << " ";
for (auto& p : v)
out << p << " ";
out << "\n";
first = 0;
}
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{";
bool first = true;
for (auto& v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "}";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const multiset<T, Cmp>& a) {
out << "{";
bool first = true;
for (auto& v : a) {
out << (first ? "" : ", ");
out << v;
first = 0;
}
out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{";
bool first = true;
for (auto& p : a) {
out << (first ? "" : ", ");
out << p.first << ":" << p.second;
first = 0;
}
out << "}";
return out;
}
#ifdef LOCAL
#define debug(...) dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 0
#endif
template <typename Arg1>
void dbg(const char* name, Arg1&& arg1) {
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void dbg(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
dbg(comma + 1, args...);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> p(n), pos(n + 1);
for (int i = 0; i < n; i++) {
cin >> p[i];
pos[p[i]] = i;
}
vector<int> b(n);
iota(b.begin(), b.end(), 1);
vector<bool> swapped(n);
vector<int> res;
for (int i = 0; i < n; i++) {
if (p[i] != b[i]) {
int idx = pos[b[i]];
while (idx > i) {
if (swapped[idx - 1]) {
cout << "-1\n";
return 0;
}
swap(p[idx - 1], p[idx]);
swap(pos[idx - 1], pos[idx]);
swapped[idx - 1] = true;
res.emplace_back(idx);
--idx;
}
}
}
if (res.size() != (n - 1))
cout << "-1\n";
else {
for (int& p : res)
cout << p << '\n';
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define ALL(x) (x).begin(),(x).end()
template<class T>bool umax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool umin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
#ifdef _GLIBCXX_DEBUG
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 << "[" << #__VA_ARGS__ << "]:", _debug_out(__VA_ARGS__)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#define eprintf(...)
#endif
int main() {
int n; cin >> n;
int INF=int(1e9)+1;
int ans = INF;
rep(i,n) {
int a,p,x;
cin >> a >> p >> x;
if(x <= a) continue;
umin(ans,p);
}
if(ans==INF) ans = -1;
cout << ans << endl;
}
|
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define ppb pop_back
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define fr(a,b) for(int i = a; i < b; i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define all(x) (x).begin(),(x).end()
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define endl '\n'
using namespace std;
const ll MOD=1e9+7;
const ll INF= 998244353;
const ll N=1e6+1;
/*ll fact(ll n)
{
ll res = 1;
for (ll i = 2; i <= n; i++)
res = res * i;
return res;
}
ll nCr(ll n, ll r)
{
return fact(n) / (fact(r) * fact(n - r));
}
// Returns factorial of n
*/
/*ll gcd(ll a, ll b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return (a*b)/gcd(a,b);
}
*/
/*bool isprime(ll n)
{
ll i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}*/
void solve()
{
ll n;
cin>>n;
ll num=(n/100)+1;
cout<<abs((num*100)-n)<<endl;
}
int main()
{
fast_io;
cin.tie(0);cout.tie(0);
ll t;
t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
| #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int n; cin >> n;
int d = n % 1000;
int p = n / 100 % 10 + 1; p *= 100;
cout << p - d << "\n";
return 0;
} |
#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type) memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define fillarray(array, value, begin, end) fill((array) + (begin), (array) + (end) + 1, value)
#define fillvector(v, value) fill((v).begin(), (v).end(), value)
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
#define vecfirst (*vec.begin())
#define veclast (*vec.rbegin())
#define vecall(v) (v).begin(), (v).end()
#define vecupsort(v) (sort((v).begin(), (v).end()))
#define vecdownsort(v, type) (sort(vecall(v), greater<type>()))
#define veccmpsort(v, cmp) (sort(vecall(v), cmp))
using namespace std;
const int N = 5050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end()
{
#ifdef ONLINE
printf("\n\nTime used: %.6lf(s)\n", ((double)TIME__END - TIME__START) / 1000);
system("pause");
#endif
}
int sum[4][N];
char c[] = {'A', 'T', 'C', 'G'};
int n;
char s[N];
bool check1(int l, int r)
{
int tmp1 = sum[0][r] - sum[0][l - 1];
int tmp2 = sum[1][r] - sum[1][l - 1];
int tmp3 = sum[2][r] - sum[2][l - 1];
int tmp4 = sum[3][r] - sum[3][l - 1];
if (tmp1 > 0 && tmp2 > 0 && tmp3 > 0 && tmp4 > 0 && tmp1 == tmp2 && tmp3 == tmp4)
return 1;
if (tmp1 + tmp2 == 0 && tmp3 > 0 && tmp4 > 0 && tmp3 == tmp4)
return 1;
if (tmp1 > 0 && tmp2 > 0 && tmp3 + tmp4 == 0 && tmp1 == tmp2)
return 1;
return 0;
}
inline void solve()
{
cin >> n;
scanf("%s", s + 1);
for (int i = 1; i <= n; ++i)
{
for (int j = 0; j < 4; ++j)
sum[j][i] = sum[j][i - 1] + (s[i] == c[j]);
}
int ans = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = i + 1; j <= n; ++j)
{
if (check1(i, j))
ans++;
}
}
printf("%d\n", ans);
}
int main()
{
TIME__START = clock();
int Test = 1;
// scanf("%d", &Test);
while (Test--)
{
solve();
// if (Test)
// putchar('\n');
}
TIME__END = clock();
program_end();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define reg register
typedef long long ll;
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
static char buf[100000],*p1=buf,*p2=buf;
inline int read(void){
reg bool f=false;
reg char ch=getchar();
reg int res=0;
while(!isdigit(ch))f|=(ch=='-'),ch=getchar();
while(isdigit(ch))res=10*res+(ch^'0'),ch=getchar();
return f?-res:res;
}
inline ll readll(void){
reg bool f=false;
reg char ch=getchar();
reg ll res=0;
while(!isdigit(ch))f|=(ch=='-'),ch=getchar();
while(isdigit(ch))res=10ll*res+(ch^'0'),ch=getchar();
return f?-res:res;
}
const int MAXN=50+5;
int n;
ll x;
ll a[MAXN];
map<ll,ll> f,g;
int main(void){
n=read(),x=readll();
for(reg int i=1;i<=n;++i)
a[n-i+1]=readll();
ll x0=x/a[1]*a[1];
f[x0]=1;
if(x0!=x)
f[x0+a[1]]=1;
for(reg int i=2;i<=n;++i){
g.clear();
reg ll Max=a[i-1]/a[i];
for(auto it:f){
reg ll d=x-it.first,m=d/a[i];
ll y=it.first+m*a[i];
if(abs(m)<Max)
g[y]+=it.second;
if(y!=x&&abs(m)+1<Max)
g[y+a[i]*(d/abs(d))]+=it.second;
}
swap(f,g);
}
printf("%lld\n",f[x]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// template <class T = ll> using P = pair< T,T>;
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
#define rep(i,n) for(int i=0; i<(n);++i)
#define repp(i, l, r) for(int i = (l); i < (r); ++i)
#define pb push_back
#define all(v) (v).begin(), (v).end()
int main()
{
int n;
cin >> n;
V<pair<int,int>> x(n);
V<pair<int,int>> y(n);
rep(i, n) {
cin >> x[i].first >> y[i].first;
x[i].second = i;
y[i].second = i;
}
sort(all(x), greater<pair<int,int>>());
sort(all(y), greater<pair<int,int>>());
V<pair<pair<int,int>,int>> ans;
{
pair<pair<int,int>,int> can;
pair<int,int> a;
a.first = x[0].second;
a.second = x[n-1].second;
can.first = a;
can.second = x[0].first - x[n-1].first;
ans.push_back(can);
}
{
pair<pair<int,int>,int> can;
pair<int,int> a;
a.first = y[0].second;
a.second = y[n-1].second;
can.first = a;
can.second = y[0].first - y[n-1].first;
ans.push_back(can);
}
{
pair<pair<int,int>,int> can;
pair<int,int> a;
a.first = x[1].second;
a.second = x[n-1].second;
can.first = a;
can.second = x[1].first - x[n-1].first;
ans.push_back(can);
}
{
pair<pair<int,int>,int> can;
pair<int,int> a;
a.first = x[0].second;
a.second = x[n-2].second;
can.first = a;
can.second = x[0].first - x[n-2].first;
ans.push_back(can);
}
{
pair<pair<int,int>,int> can;
pair<int,int> a;
a.first = y[1].second;
a.second = y[n-1].second;
can.first = a;
can.second = y[1].first - y[n-1].first;
ans.push_back(can);
}
{
pair<pair<int,int>,int> can;
pair<int,int> a;
a.first = y[0].second;
a.second = y[n-2].second;
can.first = a;
can.second = y[0].first - y[n-2].first;
ans.push_back(can);
}
sort(all(ans), [](auto lhs, auto rhs){return lhs.second > rhs.second;});
ll cnt = 0;
pair<int,int> pre;
for(auto abc : ans){
if(pre == abc.first) continue;
cnt++;
pre = abc.first;
if(cnt == 2){
cout << abc.second << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int,int> > x,y;
vector<int> ans;
map<pair<int,int>,int > ma;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
int u,v;
cin>>u>>v;
x.push_back({u,i});
y.push_back({v,i});
}
sort(x.begin(),x.end());
sort(y.begin(),y.end());
int a=x.size();
int b=y.size();
for(int i=a-1;i>=a-3;i--)
{
ans.push_back(abs(x[0].first-x[i].first));
ma[{x[0].second,x[i].second}]=1;
}
for(int i=0;i<3;i++)
{
if(ma[{x[i].second,x[x.size()-1].second}]==0)
{
ans.push_back(abs(x[i].first-x[x.size()-1].first));
ma[{x[i].second,x[x.size()-1].second}]=1;
}
}
for(int i=b-1;i>=b-3;i--)
{
if(ma[{y[0].second,y[i].second}]==0)
{
ans.push_back(abs(y[0].first-y[i].first));
ma[{y[0].second,y[i].second}]=1;
}
}
for(int i=0;i<3;i++)
{
if(ma[{y[i].second,y[x.size()-1].second}]==0)
{
ans.push_back(abs(y[i].first-y[x.size()-1].first));
ma[{x[i].second,x[x.size()-1].second}]=1;
}
}
sort(ans.begin(),ans.end());
cout<<ans[ans.size()-2];
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll getNcR(int n, int r)
{
// p holds the value of n*(n-1)*(n-2)...,
// k holds the value of r*(r-1)...
long long p = 1, k = 1;
// C(n, r) == C(n, n-r),
// choosing the smaller value
if (n - r < r)
r = n - r;
if (r != 0)
{
while (r)
{
p *= n;
k *= r;
// gcd of p, k
long long m = __gcd(p, k);
// dividing by gcd, to simplify
// product division by their gcd
// saves from the overflow
p /= m;
k /= m;
n--;
r--;
}
// k should be simplified to 1
// as C(n, r) is a natural number
// (denominator should be 1 ) .
}
else
p = 1;
// if our approach is correct p = ans and k =1
return p;
}
int main()
{
ll k;
int a, b;
int n;
cin >> a >> b >> k;
string s = " ";
n=(a+b);
for (int i = 0; i < n; i++)
{
if (a == 0)
{
s.push_back('b');
b--;
}
else if (b == 0)
{
s.push_back('a');
a--;
}
else
{
ll exper = getNcR(a + b -1 , a - 1);
if (exper >= k)
{
s.push_back('a');
a--;
}
else
{
s.push_back('b');
k -= exper;
b--;
}
}
}
cout<<s<<endl;
} | #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define dd double
#define ll long long int
#define ull unsigned long long
#define lld long double
#define setpre(n) cout << std::setprecision(n);
#define flush fflush(stdin); fflush(stdout);
#define print cout<<"Case #"<<test_case<<": ";
#define light ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define fi first
#define sc second
#define PI 3.141592653589793238462
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.fi); cerr << ","; _print(p.sc); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
ll mod1 = 1e9+7;
ll INF=1e9+5;
ll mod=998244353;
void solve(ll test_case){
ll a,b,k;
cin>>a>>b>>k;
k--;
vector<vector<ll> > dp(31,vector<ll>(61));
ll i,j;
for(i=1;i<=60;i++)
dp[1][i] = i;
for(i=2;i<=30;i++){
for(j=i;j<=60;j++){
dp[i][j] = dp[i-1][j-1]+dp[i][j-1];
}
}
debug(dp)
ll no2 = b;
vector<char> ans(a+b,'a');
while(k){
ll in = upper_bound(dp[no2].begin(),dp[no2].end(),k) - dp[no2].begin() - 1;
ans[a+b-1-in] = 'b';
k-=dp[no2][in];
no2--;
}
ll in = a+b-1;
b = no2;
while(b){
//cout<<ans[in]<<" ";
if(ans[in]!='b'){
ans[in] = 'b';
in--; b--;
}
else
in--;
}
for(auto ele:ans) cout<<ele;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("Error.txt", "w", stderr);
#endif
light;
ll t=1;
//cin>>t;
ll i=1ll;
while(t--){
solve(i);
i++;
}
} |
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
//#include <set>
using namespace std;
typedef long long LONG;
#include <iomanip>
LONG gcd(LONG x, LONG y) {
if(x % y == 0) {
return y;
} else {
return gcd(y, x % y);
//x%y==0でないときはユーグリットの互除法を使って再帰的に関数を呼び出す。
}
}
void print(vector<int>A){
for(size_t i=0;i<A.size();i++){
if(A[i]==1){
cout<<'(';
}else{
cout<<')';
}
}
}
bool comp1(pair<int,int> lhs, pair<int,int>rhs) {
return lhs.first<rhs.first;
}
bool comp2(pair<string,int> lhs, pair<string,int>rhs) {
return lhs.second<rhs.second;
}
void solve(int min,int max,vector<int>&A,int B){
if(min+1==max){
cout<<std::min(A[max]-B,B-A[min])<<endl;
return;
}
int a=(min+max)/2;
if(A[a]<B){
solve(a,max,A,B);
}else{
solve(min,a,A,B);
}
}
LONG pow2(LONG a,LONG b){
LONG res=1;
for(int i=0;i<b;i++){
res=res*a;
}
return a;
}
void solve1(vector<bool>&A,vector<LONG>&C,int pos,vector<vector<pair<int,LONG>>>&path){
for(int i=0;i<path[pos].size();i++){
if(!A[path[pos][i].first]){
A[path[pos][i].first]=true;
C[path[pos][i].first]=C[pos]^path[pos][i].second;
solve1(A,C,path[pos][i].first,path);
}
}
}
int min(int a,int b){
if(a<b){
return a;
}
return b;
}
const LONG mod=1000000007;
int main() {
cout << std::fixed << std::setprecision(15) << endl;
LONG N;
cin>>N;
vector<LONG>A(N);
for(int i=0;i<N;i++){
cin>>A[i];
}
sort(A.begin(),A.end());
int a=A[A.size()/2];
double result=0;
for(int i=0;i<N;i++){
result+=min(A[i],a)-A[i];
}
cout<<-(result/N-a/2.0);
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(),a.end()
typedef pair<ll,ll> pi;
//__builtin_popcountll(2) (the number of ones in the binary representation)
//__builtin_clz(2) (number of leading zeroes)
//__builtin_ctz(2) (number of trailing zeroes)
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>*/
// s.find_by_order(k)(returns iterator to kth element), s.order_of_key(k)(number of elements less than number)
ll mod = 1e9+7;
vector<ll> fact(1);
/*vector<ll> spf(10000005), cntPrime(10000005);
void spff(){
int i;
for(i=1;i<10000005;i++) spf[i] = i;
for(i=2;i*i<10000005;i++)
{
if(spf[i] != i) continue;
for(int j = i*i; j < 10000005; j+= i){
if(spf[j]==j) spf[j] = i;
}
}
for(i=1;i<10000005;i++){
if(spf[i] != spf[i/spf[i]]){
cntPrime[i] = cntPrime[i/spf[i]]+1;
}
else cntPrime[i] = cntPrime[i/spf[i]];
}
}
while(spf[a[i]]!=a[i]){
z=spf[a[i]];
while(a[i]%z==0) a[i]/=z;
m[z]++;
}
if(a[i]>1) m[a[i]]++;*/
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y)
{
ll temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll modInv(ll n, ll p)
{
return power(n, p - 2, p);
}
ll ncr(ll n, ll r) {return (n>=r?(fact[n]*modInv(fact[r],mod))%mod*modInv(fact[n-r],mod)%mod:0);}
ll add(ll a, ll b) {ll z=a+b; if(z>=mod) z -= mod; return z;}
ll mul(ll a, ll b) { return (a*b)%mod;}
ll sub(ll a, ll b) { return (a-b+mod)%mod;}
// LLONG_MAX
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll t=1,n,i,j,m;
//cin>>t;
//fact[0]=1;
//for(i=1;i<300003;i++)fact[i]=(fact[i-1]*i)%mod;
while(t--)
{
cin>>n;
cout<<fixed<<setprecision(10);
vector<db> a(n);
db sum=0, mx=0;
for(i=0;i<n;i++) {
cin>>a[i];
mx=max(mx,a[i]);
sum+=a[i];
}
db sm = (mx/10000000);
db lo=0, hi=mx/2.0-sm, mid;
//cout<<sum/n;
db cur=0;
while(lo <= hi){
//cout<<lo<<' '<<hi<<endl;
mid = (lo+hi)/2.0;
db prev=0,aft=0; cur=0;
for(i=0;i<n;i++){
prev -= min(a[i], 2*(mid-sm));
cur -= min(a[i], 2*mid);
aft -= min(a[i], 2*(mid+sm));
}
prev /= n; cur /= n; aft /= n;
prev+=mid-sm+(sum/n);aft+=mid+sm+(sum/n);cur+=mid+(sum/n);
//cout<<prev<<'q'<<cur<<'q'<<aft<<"\n\n";
if(cur < prev && cur < aft) break;
if(prev > cur) lo = mid+sm;
else hi = mid-sm;
}
cout<<cur;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1077563119;
int n, m;
bool edge[20][20];
int chromaticNumber(const vector<int> &g) {
int n = g.size();
if (n == 0) return 0;
//randomly choose a large prime
const int modulo = 1077563119;
int all = 1 << n;
vector<int> ind(all), s(all);
for (int i = 0; i < all; i ++) s[i] = ((n - __builtin_popcount(i)) & 1 ? -1 : 1);
ind[0] = 1;
for (int i = 1; i < all; i ++) {
int ctz = __builtin_ctz(i);
ind[i] = ind[i - (1 << ctz)] + ind[(i - (1 << ctz)) & ~g[ctz]];
if (ind[i] >= modulo) ind[i] -= modulo;
}
//compute the chromatic number (= \sum (-1)^{n - |i|} * ind(i)^k)
for (int k = 1; k < n; k ++) {
long long sum = 0;
for (int i = 0; i < all; i ++) {
long long cur = ((s[i] * (long long) ind[i]) % modulo);
s[i] = cur;
sum += cur;
}
if (sum % modulo != 0) return k;
}
return n;
}
int main() {
#ifdef ACM
freopen("input", "r", stdin);
#endif
cin >> n >> m;
int u, v;
while (m--) {
cin >> u >> v;
u--, v--;
edge[u][v] = edge[v][u] = true;
}
vector<int> g(n);
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (!edge[i][j]) {
g[i] += (1 << j);
g[j] += (1 << i);
}
cout << chromaticNumber(g);
} | #include <bits/stdc++.h>
#define TRACE(x) cerr << #x << " = " << x << endl
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) (int)(x).size()
using namespace std;
typedef long long ll;
typedef double lf;
typedef pair<int, int> pii;
typedef vector<int> VI;
template<class Num>
Num mabs(Num A){
if(A < 0) return -A;
return A;
}
int main(){
int n, m;
cin >> n >> m;
VI hei(n);
VI moje(m);
REP(i, n)
cin >> hei[i];
REP(i, m)
cin >> moje[i];
sort(hei.begin(), hei.end());
vector<ll> pref_prije(n + 2, 0), suf_nakon(n + 2, 0);
for(int i = 0; i < n - 1; i += 2){
pref_prije[i] = (ll)(hei[i + 1] - hei[i]);
if(i) pref_prije[i] += pref_prije[i - 1];
pref_prije[i + 1] = pref_prije[i];
if(i + 2 >= n - 1)
pref_prije[i + 2] = pref_prije[i];
}
for(int i = n - 1; i >= 1; i -= 2){
suf_nakon[i] = (ll)(hei[i] - hei[i - 1]);
suf_nakon[i] += suf_nakon[i + 1];
suf_nakon[i - 1] = suf_nakon[i];
if(i - 2 < 1 && i - 2 >= 0)
suf_nakon[i - 2] = suf_nakon[i];
}
//REP(i, n)
// cout << pref_prije[i] << ' ';
//cout << endl;
//REP(i, n)
// cout << suf_nakon[i] << ' ';
//cout << endl;
//
//return 0;
ll ans = (ll)2e18;
REP(i, m){
int val = moje[i];
int pos = lower_bound(hei.begin(), hei.end(), val) - hei.begin();
ll tmp = 0ll;
if(pos % 2 == 0){
//prije ih je parno
if(pos > 0) tmp = pref_prije[pos - 1];
if(pos < n) tmp += (ll)(hei[pos] - val);
if(pos + 1 <= n)
tmp += suf_nakon[pos + 1];
}
else{
if(pos >= 2) tmp = pref_prije[pos - 2];
if(pos > 0) tmp += (ll)(val - hei[pos - 1]);
if(pos <= n)
tmp += suf_nakon[pos];
}
//TRACE(tmp);
ans = min(ans, tmp);
}
cout << ans << '\n';
return 0;
}
|
#include <cstdio>
using u32 = unsigned int;
using u64 = unsigned long long int;
constexpr u32 mod = 998244353;
int n, m, k;
/*
A1 ... AN
B1 ... BM
Bmin >= Amax
*/
u32 modpow(u32 a, u32 n){
u32 r = 1;
for(; n; n>>=1){
if(n&1) r = (u64) r * a % mod;
a = (u64) a * a % mod;
}
return r;
}
int main(){
scanf("%d%d%d", &n, &m, &k);
u64 ans = 0;
if(n == 1){
for(int i = 1; i <= k; i++){
ans += modpow(k - i + 1, m) - modpow(k - i, m) + mod;
}
}
else if(m == 1){
for(int i = 1; i <= k; i++){
ans += modpow(i, n) - modpow(i - 1, n) + mod;
}
}
else {
for(int i = 1; i <= k; i++){
ans += (u64) (modpow(i, n) - modpow(i-1, n) + mod) * modpow(k - i + 1, m) % mod;
}
}
printf("%llu\n", ans % mod);
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <chrono>
#define int long long
#define ld long double
#define db double
#define endl "\n"
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
using namespace __gnu_pbds;
using namespace std;
using namespace chrono;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void deb_out() {
cout << endl;
}
template <typename Head, typename... Tail>
void deb_out(Head H, Tail... T) {
cout << " " << H;
deb_out(T...);
}
#ifdef LOCAL
#define deb(...) cout << "[" << #__VA_ARGS__ << "]:", deb_out(__VA_ARGS__);
#else
#define deb(...) ;
#endif
const long double PI = 3.14159265358979323846264338;
const long long INF = 1000000000000000000;
const long long INF1 = 1000000000;
// const long long MOD = 1000000007;
const long long MOD = 998244353;
inline int cl(int a, int b){
if(a % b)
return 1 + a / b;
return a / b;
}
int mpow(int a, int b){
a %= MOD;
if(!b)
return 1;
int temp = mpow(a, b / 2);
temp = (temp * temp) % MOD;
if(b % 2)
return (a * temp) % MOD;
return temp;
}
int _pow(int a, int b){
if(!b)
return 1;
int temp = _pow(a, b / 2);
temp = (temp * temp);
if(b % 2)
return (a * temp);
return temp;
}
inline int mod_in(int n){
return mpow(n, MOD - 2);
}
const int N = 2e5 + 5;
int fct[N], infct[N];
void pre() {
fct[0] = 1;
infct[0] = mod_in(1);
for (int i = 1 ; i < N ; i++) {
fct[i] = (i * fct[i - 1]) % MOD;
infct[i] = mod_in(fct[i]);
}
}
int nCr(int n, int r) {
if (n < r)
return 0;
int v = (fct[n] * infct[n - r]) % MOD;
v *= infct[r];
return v % MOD;
}
int put(int n, int r) {
if (r < 1)
return 0;
return nCr(n + r - 1, r - 1);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int Tests = 1;
// cin >> Tests;
pre();
while (Tests--) {
int n, m, k;
cin >> n >> m >> k;
int ans = 0;
ans = 0;
if (n == 1) {
ans = mpow(k, m);
cout << ans;
exit(0);
} else if (m == 1) {
ans = mpow(k, n);
cout << ans;
exit(0);
}
deb(ans)
for (int i = 1 ; i <= k ; i++) {
int lss = mpow(i, n);
if (i > 1)
lss -= mpow(i - 1, n);
lss = (lss + MOD) % MOD;
// int more = mpow(m, k - i + 1);
// deb(i, lss, mpow(k - i + 1, m));
lss *= mpow(k - i + 1, m);
ans += lss % MOD;
ans %= MOD;
// deb(i, ans)
}
// int ans = put(n + m, k);
cout << ans << endl;
}
}
|
#include<bits/stdc++.h>
#define PI 3.141592653589793238462
#define eps 1e-20
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<db,db> pdd;
ll a[200005],d[200005][20],vis[20],mod=1e9+7;
int main(){
string s;ll k;cin>>s>>k;
ll n=s.length(),t=0;s=" "+s;
for(ll i=1;i<=n;i++){
if(s[i]>='A') a[i]=(s[i]-'A')+10;
else a[i]=(s[i]-'0');
}
d[1][1]=a[1]-1;
for(ll i=1;i<n;i++){
if(!vis[a[i]]){
t++;vis[a[i]]=1;
}
for(ll j=1;j<=k;j++){
d[i+1][j]+=d[i][j]*j;d[i+1][j]%=mod;
if(j+1<=16) d[i+1][j+1]+=d[i][j]*(16-j),d[i+1][j+1]%=mod;
}
for(ll j=0;j<a[i+1];j++){
if(vis[j]) d[i+1][t]++;
else d[i+1][t+1]++;
}
d[i+1][1]+=15;d[i+1][1]%=mod;
}
if(vis[a[n]]) d[n][t]++;
else d[n][t+1]++;
cout<<d[n][k]%mod<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) a.begin(),a.end()
#define fi first
#define se second
#define deb(x) cout << #x << "=" << x << endl
#define clr(x) memset(x, 0, sizeof(x))
#define rep(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
int M=1e9+7,N=205001;
int mm(int x,int y){x%=M,y%=M;return (x*y)%M;}//Modular Multiply
int po(int x,int y){ if(!y)return 1;int a=po(x,y/2)%M;if(y%2)return mm(a,mm(a,x));return mm(a,a);}//(x**y)%M
int solve(vector<int>& h) {
int n=h.size(),mx=0;
if(!n) return 0;
int nsl[n],nsr[n];
stack<int> s;
for(int i=0;i<n;i++){
while(!s.empty() and h[s.top()]>=h[i]) s.pop();
nsl[i]=((s.empty())?-1:s.top());
s.push(i);
}
while(!s.empty()) s.pop();
for(int i=n-1;i>=0;i--){
while(!s.empty() and h[s.top()]>=h[i]) s.pop();
nsr[i]=((s.empty())?n:s.top());
s.push(i);
}
for(int i=0;i<n;i++)
mx=max(mx,h[i]*(nsr[i]-nsl[i]-1));
return mx;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
vector<int> h(n);
rep(i,0,n) cin>>h[i];
cout<<solve(h);
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;string s;
cin >> n >> s;
pair<int,int> a[n][n];
for(int i=0;i<n;i++)
{
pair<int,int> temp(0,0);
if(s[i] == 'A')
temp.first = 1;
else if(s[i] == 'T')
temp.first = -1;
else if(s[i] == 'C')
temp.second = 1;
else
temp.second = -1;
a[i][i] = temp;
}
lli count = 0;
for(int len=1;len<n;len++)
{
int i=0;
int j=i+len;
while(j<n)
{
pair<int,int> temp = a[i][j-1];
if(s[j] == 'A')
temp.first++;
else if(s[j] == 'T')
temp.first--;
else if(s[j] == 'C')
temp.second++;
else
temp.second--;
if(temp.first == 0 and temp.second == 0)
count++;
a[i][j] = temp;
i++;
j = i+len;
}
}
cout << count << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int H,W,X,Y;
cin>>H>>W>>X>>Y;
vector<vector<char>> S(H, vector<char>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> S.at(i).at(j);
}
}
string temp;
temp = "#";
int Z;
Z=1;//位置するますの分
//ここから左右を足す
for (int i = Y-2; i > -1; i--){
temp = S.at(X-1).at(i);
if(temp=="."){
Z++;
}else{
break;
}
}//左のぶん
for (int i = Y; i < W; i++){
temp = S.at(X-1).at(i);
if(temp=="."){
Z++;
}else{
break;
}
}//右のぶん
for (int i = X-2; i > -1; i--){
temp = S.at(i).at(Y-1);
if(temp=="."){
Z++;
}else{
break;
}
}//上のぶん
for (int i = X; i < H; i++){
temp = S.at(i).at(Y-1);
if(temp=="."){
Z++;
}else{
break;
}
}//したのぶん
cout<<Z<<endl;
} |
//Codeforcesで128bit整数を使いたいとき
//→__int128_tを使う&GNU C++17 (64)で提出する
//インクルードなど
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//イテレーション
#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--)
#define FORA(i,I) for(const auto& i:I)
//x:コンテナ
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007 //問題による
//略記
#define F first
#define S second
//出力(空白区切りで昇順に)
#define coutALL(x) for(auto i=x.begin();i!=--x.end();i++)cout<<*i<<" ";cout<<*--x.end()<<endl;
//aをbで割る時の繰上げ,繰り下げ
ll myceil(ll a,ll b){return (a+(b-1))/b;}
ll myfloor(ll a,ll b){return a/b;}
int ri() {int n;cin >> n;return n;}
#define ro(a) {cout << a << endl;}
signed main(){
// int N = ri();
// vector<string> S(N);
string s;
cin >> s;
if (s[0] == s[1] && s[1] == s[2]) {
ro("Won");
} else {
ro("Lost")
}
} | #include<bits/stdc++.h>
using namespace std;
#define endl "\n";
typedef long long ll;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
ll n;
cin>>n;
vector<ll> v1,v2;
for(ll i=1;i<=sqrt(n);i++){
if(n%i==0&&n!=i*i)v1.push_back(i),v2.push_back(n/i);
else if(n==i*i)v1.push_back(i);
}
for(int i=0;i<v1.size();i++){
cout<<v1[i]<<endl;
}
for(int i=v2.size()-1;i>=0;i--){
cout<<v2[i]<<endl;
}
}
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
#define eprintf(...) 42
#endif
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
int main()
{
startTime = clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int A, B;
scanf("%d%d", &A, &B);
int S = A * (A + 1) / 2 - B * (B + 1) / 2;
for (int i = 1; i <= A; i++) {
int x = i;
if (i == A && S < 0) x -= S;
printf("%d ", x);
}
for (int i = 1; i <= B; i++) {
int x = -i;
if (i == B && S > 0) x -= S;
printf("%d ", x);
}
printf("\n");
return 0;
}
| #include <iostream>
using namespace std;
#include<bits/stdc++.h>
typedef long long ll;
int main() {
// int t;
// cin>>t;
// while(t--){
ll i,j,k,l;
ll x,y,z;
ll n;
ll m;cin>>n>>m;
vector<ll>a(n);
vector<ll>b(m);
k=1;
l=-1;
if(n==m){
for(i=0;i<n;i++){
a[i]=k;
b[i]=l;
k++;l--;
}
}
else if(n>m){
ll s=0,s1=0;;
for(i=0;i<n;i++){
a[i]=k;k++;s+=a[i];
}
for(i=0;i<m-1;i++){
b[i]=l;l--;s1-=b[i];
}
j=s-s1;
b[m-1]=-j;
}
else{
ll s=0,s1=0;;
for(i=0;i<n-1;i++){
a[i]=k;k++;s+=a[i];
}
for(i=0;i<m;i++){
b[i]=l;l--;s1-=b[i];
}
j=s1-s;
a[n-1]=j;
}
for(i=0;i<n;i++)
cout<<a[i]<<" ";
for(i=0;i<m;i++)
cout<<b[i]<<" ";
// }
return 0;
}
|
#include <bits/stdc++.h>
#define rep3(i, s, n, a) for (long long i = (s); i < (long long)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
int main() {
ll n;
cin >> n;
vector<ll> a(n, 0);
rep(i, n){
a[i] = i+1;
}
ll res = 1;
rep2(i, 2, 30){
bool skip=true;
while(skip){
skip = false;
rep(j, n){
if(a[j]%i==0){
skip = true;
a[j] /= i;
}
}
if(skip){
// cout << i << endl;
res *= i;
}
}
}
cout << res + 1LL << 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;
int cnt[100];
int f(int n) {
int res = 1;
for(int i = 2; i * i <= n; i++) if(n % i == 0) {
int c = 0;
while(n % i == 0) {
n /= i;
c++;
if(c > cnt[i]) {
cnt[i]++;
res *= i;
}
}
}
if(n > 1 && cnt[n] == 0) {
cnt[n] = 1;
res *= n;
}
return res;
}
int main() {
int n;
S(n);
LL ans = 1;
rep(i,2,n+1) {
ans *= f(i);
}
cout << ans + 1 << "\n";
return 0;
}
|
//デバッグ用オプション:-fsanitize=undefined,address
//コンパイラ最適化
#pragma GCC optimize("Ofast")
//インクルードなど
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//マクロ
//forループ
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
//FORAは範囲for文(使いにくかったら消す)
#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--)
#define FORA(i,I) for(const auto& i:I)
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF 1000000000000 //10^12:∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange
//略記
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
/*
ビット全探索
for(int bit=0;bit<(1<<n);bit++){
for(int i=0;i<n;i++){
if(bit & (1<<i)){
}
}
*/
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;
}
signed main(){
//小数の桁数の出力指定
//cout<<fixed<<setprecision(10);
//入力の高速化用のコード
//ios::sync_with_stdio(false);
//cin.tie(nullptr);
ll n,k;
cin>>n>>k;
map<int,int> a;
rep(i,n){
int A;
cin>>A;
a[A]++;
}
int count=k;
int now=0;
ll ans=0;
for(auto itr=a.begin();itr!=a.end();itr++){
if(now==itr->first){
count=min(count,itr->second);
ans+=count;
now++;
}
else break;
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
int charToInt(char c){
char zero_num = '0';
return (int)c - (int)zero_num;
}
signed main(){
int N; cin>>N;
vec_int a(N), b(N), p(N);
rep(i,N) cin>>a.at(i);
rep(i,N) cin>>b.at(i);
rep(i,N) cin>>p.at(i);
vector<P> person(N);
rep(i, N)person.at(i) = make_pair(a.at(i), i);
sort(person.begin(), person.end());
// 何をやらないといけないかというと、
// 体重が軽い人の順番に
// 自分のIDの荷物を持っている人と荷物を交換する
//
map<int, int> assign_map;
map<int, int> assign_map2;
rep(i,N){
assign_map[p.at(i)] = i; // 荷物, 人
assign_map2[i] = p.at(i); //人, 荷物
}
int ans = 0;
vector<P> ans_vec;
for(int i=0;i<N;i++){
int weight, person_ID; tie(weight, person_ID) = person.at(i);
//cout<<weight<<" "<<person_ID<<" "<<assign_map[person_ID]<<endl;
if(person_ID == assign_map2[person_ID]-1){
continue;
}
int nimotu = assign_map2[person_ID];
if(weight <= b.at(nimotu-1) ){
cout<<-1<<endl;
return 0;
}
int counter_person = assign_map[person_ID+1]; // person_ID+1の荷物を持っている人
assign_map2[person_ID] = person_ID+1;
assign_map2[counter_person] = nimotu;
assign_map[nimotu] = counter_person;
ans_vec.push_back(make_pair(person_ID+1, counter_person+1));
ans++;
}
cout<<ans<<endl;
rep(i, ans){
cout<<ans_vec.at(i).first<<" "<<ans_vec.at(i).second<<endl;
}
return 0;
} |
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i)
#define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i)
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define Endl putchar('\n')
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T>inline T fab(T x){ return x<0? -x: x; }
template<class T>inline T readin(T x){
x=0; int f=0; char c;
while((c=getchar())<'0' || '9'<c) if(c=='-') f=1;
for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48));
return f? -x: x;
}
const int maxn=2e5*2;
const int mod=998244353;
inline int qkpow(int a, int n){
int ret=1;
for(; n>0; n>>=1, a=1ll*a*a%mod)
if(n&1) ret=1ll*ret*a%mod;
return ret;
}
int n, m;
inline void input(){
n=readin(1), m=readin(1);
}
int fac[maxn+5], invf[maxn+5];
inline void init(){
fac[0]=1;
rep(i, 1, maxn) fac[i]=1ll*fac[i-1]*i%mod;
invf[maxn]=qkpow(fac[maxn], mod-2);
drep(i, maxn-1, 0) invf[i]=1ll*(i+1)*invf[i+1]%mod;
invf[0]=1;
}
inline int C(int n, int m){
if(m==0) return 1;
if(n<m) return 0;
return 1ll*fac[n]*invf[m]%mod*invf[n-m]%mod;
}
signed main(){
input();
init();
int ans=0;
rep(j, 1, m){
int tmp=j, res=1;
for(int i=2; i*i<=tmp; ++i) if(tmp%i==0){
int cnt=0;
while(tmp%i==0) tmp/=i, ++cnt;
res=1ll*res*C(n+cnt-1, cnt)%mod;
}
if(tmp>1) res=1ll*res*n%mod;
ans=(ans+res)%mod;
}
printf("%d\n", (ans)%mod);
return 0;
} | #include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=107;
long double E[N][N][N];
int32_t main()
{
int a,b,c;
cin>>a>>b>>c;
// for(int i=0;i<=100;i++)
// for(int j=0;j<=100;j++)
// {
// E[i][j][100]=E[100][i][j]=E[i][100][j]=0;
// }
for(int i=99;i>=0;i--)
{
for(int j=99;j>=0;j--)
{
for(int k=99;k>=0;k--)
{
if(i+j+k==0)
continue;
double ei=E[i+1][j][k]+1;
ei*=i/(double)(i+j+k);
double ej = E[i][j+1][k] + 1;
ej *= j / (double)(i + j + k);
double ek = E[i][j][k+1] + 1;
ek *= k/ (double)(i + j + k);
E[i][j][k]=ei+ej+ek;
// if(i==a&&b==j&&c==k)
// cout<<ei<<" "<<ej<<" "<<ek<<" "endl;;
}
}
}
cout <<fixed<< setprecision(6) << E[a][b][c] << endl;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
struct UnionFind{
vector<int> par;
UnionFind(int N){
par.resize(N,-1);
}
int root(int x){
if(par[x] < 0) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y){
int rx = root(x);
int ry = root(y);
if(rx == ry) return;
if(par[rx] < par[ry]){
par[rx] += par[ry];
par[ry] = rx;
} else {
par[ry] += par[rx];
par[rx] = ry;
}
}
bool same(int x, int y){
return par[x] == par[y];
}
int size(int x){
return (par[x] < 0) ? -par[x] : 0;
}
};
int main(){
const ll MOD = 998244353;
int n,k; cin >> n >> k;
vector<vector<int>> a(n,vector<int>(n));
rep(i,n)rep(j,n) cin >> a[i][j];
ll ans = 1;
UnionFind uf1(n);
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
bool f = true;
for(int l = 0; l<n; l++){
if(a[i][l]+a[j][l] > k) f = false;
}
if(f) uf1.unite(i,j);
}
}
for(int i=0; i<n; i++){
ll t = uf1.size(i);
while(t > 0){ ans = ans * t % MOD; t--; }
}
UnionFind uf2(n);
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
bool f = true;
for(int l = 0; l<n; l++){
if(a[l][i]+a[l][j] > k) f = false;
}
if(f) uf2.unite(i,j);
}
}
for(int i=0; i<n; i++){
ll t = uf2.size(i);
while(t > 0){ ans = ans * t % MOD; t--; }
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const long long MOD=998244353;
int N, K;
long long modmypower(long long a, long long b){
if(b==1){
return a;
}else if(b%2==0){
long long c=modmypower(a,b/2);
return (c*c)%MOD;
}else{
long long c=modmypower(a,(b-1)/2);
return (((c*c)%MOD)*a)%MOD;
}
}
long long modfactorial(long long x){
if(x<=1) x=1;
else for(int i=x-1; i>0; i--) x=((x*i)%MOD);
return x;
}
long long modinv(long long x){
return modmypower(x,MOD-2);
}
long long fn(vector<vector<int>>& mat){
// 交換できる行のグラフをつくる
vector<vector<int>> g(N, vector<int>());
for(int i=0; i<N-1; i++){
for(int j=i+1; j<N; j++){
bool c=true;
for(int k=0; k<N; k++) if(mat[i][k]+mat[j][k]>K) c=false;
if(c){
g[i].push_back(j);
g[j].push_back(i);
}
}
}
long long result=1;
//連結成分ごとに見る
vector<int> visited(N, 0);
for(int i=0; i<N; i++){
//連結成分を抽出
if(visited[i]==1) continue;
visited[i]=1;
queue<int> q;
q.emplace(i);
vector<int> group;
group.push_back(i);
while(!q.empty()){
int a=q.front();
q.pop();
for(auto dest:g[a]){
if(visited[dest]==1) continue;
visited[dest]=1;
q.emplace(dest);
group.push_back(dest);
}
}
//ベクトルが一致するかどうか
int s=group.size();
vector<int> d(s, 0);
for(int j=0; j<s; j++){
for(int k=0; k<s; k++){
if(mat[j]==mat[k]) d[j]++;
}
}
map<int, int> dlist;
for(int j=0; j<s; j++) dlist[d[j]]++;
long long tmp=modfactorial(s);
for(auto kv: dlist){
int k=kv.first;
int v=kv.second/k;
if(k==1) continue;
tmp=(tmp*modinv(modfactorial(v)))%MOD;
}
result=(result*tmp)%MOD;
}
return result;
}
int main(){
cin >> N >> K;
vector<vector<int>> A(N, vector<int>(N));
for(int i=0; i<N*N; i++) cin >> A[i/N][i%N];
long long result=fn(A);
vector<vector<int>> B(N, vector<int>(N));
for(int i=0; i<N*N; i++) B[i/N][i%N]=A[i%N][i/N];
result*=fn(B);
cout << result%MOD << endl;
} |
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef unsigned long long ull;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<str> vs;
typedef vector<ld> vd;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
const int MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const int MX = 1e6 + 10;
const int nx[8] = {0, 0, 1, -1, -1, 1, 1, -1}, ny[8] = {1, -1, 0, 0, 1, -1, 1, -1}; //right left down up
template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
ll random(ll a, ll b){
return a + rng() % (b - a + 1);
}
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl;
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//Geometry
typedef long long C;
typedef complex<C> P;
#define X real()
#define Y imag()
#define pi 3.14159265359
int main()
{
boost;
IO();
ll n;
cin >> n;
cout << n-1 << endl;
return 0;
}
| #include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
cout<<n-1;
} |
#include<bits/stdc++.h>
#include<unordered_map>
#define sz(s) (int)s.size()
#define all(s) s.begin(),s.end()
using namespace std;
typedef long long ll;
void fast(){
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
bool valid(ll n, string s)
{
if (!n)
return 0;
int c = 0;
while (n)
{
c++;
n /= 10;
}
return c == sz(s);
}
void Men7a()
{
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
set<char>st;
for (int i = 0; i < sz(s1); i++)
st.insert(s1[i]);
for (int i = 0; i < sz(s2); i++)
st.insert(s2[i]);
for (int i = 0; i < sz(s3); i++)
st.insert(s3[i]);
if (sz(st)>10)
{
cout << "UNSOLVABLE\n";
return;
}
unordered_map<char, int>mp;
vector<int>v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
do
{
for (auto x : st)
mp[x] = -1;
int ix = 0;
for (auto x : st)
mp[x] = v[ix++];
ll a = 0, b = 0, c = 0;
for (int i = 0; i < sz(s1); i++)
{
a *= 10;
a += mp[s1[i]];
}
for (int i = 0; i < sz(s2); i++)
{
b *= 10;
b += mp[s2[i]];
}
for (int i = 0; i < sz(s3); i++)
{
c *= 10;
c += mp[s3[i]];
}
if (a + b == c && valid(a, s1) && valid(b, s2) && valid(c, s3))
{
cout << a << "\n" << b << "\n" << c << "\n";
return;
}
} while (next_permutation(all(v)));
cout << "UNSOLVABLE\n";
}
int main()
{
fast();
int test_case = 1;
//freopen("","r",stdin);
//cin >> test_case;
while (test_case--)
Men7a();
} | // D - Send More Money
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define vec vector
#define rep(i,e) for(int i=0;i<(e);++i)
#define sz(a) int(a.size())
ll INF = 1e18;
string in(){ string s; cin>>s; return s; }
vec<int> reversed(vec<int> s){ reverse(s.begin(), s.end()); return s; }
int main(){
vec<vec<int>> S(3);
vec<ll> A(26, INF), sign{1, 1, -1};
set<int> C;
rep(i, 3){
for(auto&c:in()) S[i].push_back(c-'a');
int w = 1;
for(auto&c:reversed(S[i])){
if(A[c] == INF) A[c] = 0;
A[c] += sign[i] * w;
C.insert(c);
w *= 10;
}
}
if(sz(C) > 10){ puts("UNSOLVABLE"); return 0; }
vec<int> P(10); iota(P.begin(), P.end(), 0);
do{
ll tot = 0;
auto pos_p = P.begin();
rep(c, sz(A)){
if(A[c] == INF) continue;
if(*pos_p == 0)
for(auto&s:S) if(s[0] == c){ tot = INF; break; }
tot += *pos_p * A[c];
pos_p++;
}
if(tot) continue;
pos_p = P.begin();
rep(c, sz(A)) if(A[c] != INF) A[c] = *pos_p++;
for(auto&s:S){
ll ans = 0;
for(auto&c:s) ans = ans*10 + A[c];
cout<< ans <<endl;
}
return 0;
}while(next_permutation(P.begin(), P.end()));
puts("UNSOLVABLE");
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define rep1(i,n) for(ll i=1;i<=(ll)(n);i++)
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll n,x;
cin >> n >> x;
vector<ll> a(n);
rep(i,n) cin >> a[i];
ll dp[2];
dp[0] = 1;
dp[1] = 0;
ll tx = x;
rep(i,n-1){
ll amari = tx%a[i];//前の+
ll amari_minus = amari - a[i];//前の-
ll amari_now = tx%a[i+1];
ll amari_now_minus = amari_now - a[i+1];
// cout << amari <<" " << amari_minus << " " << amari_now << " " << amari_now_minus << endl;
ll ndp[2];
if(abs(amari-amari_now)<=a[i+1]-a[i]){
//dp[0]そのまま
ndp[0] = dp[0];
}
else{
ndp[0] = 0;
}
if(abs(amari_minus-amari_now_minus)<=a[i+1]-a[i]){
ndp[1] = dp[1];
}
else ndp[1] = 0;
if(abs(amari_minus-amari_now)<=a[i+1]-a[i]) ndp[0] += dp[1];
if(abs(amari-amari_now_minus)<=a[i+1]-a[i]) ndp[1] += dp[0];
dp[0]= ndp[0];
dp[1]= ndp[1];
// cout << dp[0] <<" " << dp[1] << endl;
}
cout << dp[0] + dp[1] << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define int long long
#define ll long long
#define all(v) v.begin(), v.end()
#define intvect vector<int>
#define pii pair<int, int>
#define mii map<int, int>
#define fo(i, n) for (int i = 0; i < n; ++i)
#define Fo(i, k, n) for (int i = k; i < n; ++i)
#define ld long double
#define deb(x) cout << #x << " " << x << endl;
#define pb push_back
#define pob pop_back
#define lcm(a, b) (a / __gcd(a, b) * b)
#define F first
#define S second
#define ull unsigned long long
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
indexed_set;
const ll mod = 1e9 + 7;
const ll N = (ll)5e6 + 5;
template <typename... T>
void read(T &... args)
{
((cin >> args), ...);
}
template <typename... T>
void write(T &&... args)
{
((cout << args), ...);
}
void vjudge()
{
#ifndef RTE
if (fopen("input.txt", "r"))
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
bool iss(int n)
{
int u = sqrt(n);
if (u * u == n)
return 1;
return 0;
}
void solve()
{
int u, v;
cin >> u >> v;
for (int i = 1; i < sqrt(v) + 1; i++)
{
if (i + v / i == u)
{
cout << "Yes\n";
return;
}
}
cout << "No\n";
}
int32_t main()
{
clock_t start, end;
start = clock();
vjudge();
int t = 1;
// read(t);
while (t--)
solve();
end = clock();
double waste = double(end - start) / double(CLOCKS_PER_SEC);
cerr << "Time wasted by program is : " << fixed
<< waste << setprecision(15);
cerr << " sec " << endl;
} |
#include <iostream>
using namespace std;
using LL = long long;
const int kMaxN = 21;
int n, m, p, q[kMaxN], k[kMaxN];
bool v[kMaxN], e[kMaxN][kMaxN];
LL c = 1, _c;
void F(int x) {
if (!v[x]) {
v[x] = 1, q[++p] = x;
for (int i = 1; i <= n; ++i) {
if (e[x][i]) {
F(i);
}
}
}
}
void D(int x) {
if (x > p) {
++_c;
return;
}
bool b[4] = {0, 0, 0, 0};
for (int i = 1; i < x; ++i) {
if (e[q[x]][q[i]]) {
b[k[q[i]]] = 1;
}
}
for (int i = 1; i <= 3; ++i) {
if (!b[i]) {
k[q[x]] = i;
D(x + 1);
}
}
}
int main() {
cin >> n >> m;
for (int i = 1, x, y; i <= m; ++i) {
cin >> x >> y;
e[x][y] = e[y][x] = 1;
}
for (int i = 1; i <= n; ++i) {
if (!v[i]) {
_c = p = 0, F(i), D(1), c *= _c;
}
}
cout << c << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;
//const int mod = 998244353;
vector<vector<int>> to;
vector<int> used, col, vs;
ll now;
void dfs(int v){
used[v] = 1;
vs.push_back(v);
for(int u : to[v]){
if(used[u]) continue;
dfs(u);
}
}
void dfs2(int i){
if(i == vs.size()){
now++;
return;
}
int v = vs[i];
rep(c, 0, 3){
col[v] = c;
bool flag = 0;
for(int u : to[v]){
if(col[u] == c){
flag = 1;
}
}
if(flag) continue;
dfs2(i+1);
}
col[v] = -1;
}
int main()
{
int n, m;
cin >> n >> m;
to = vector<vector<int>> (n);
used = vector<int> (n);
col = vector<int> (n, -1);
rep(i, 0, m){
int a, b;
cin >> a >> b;
a--; b--;
to[a].push_back(b);
to[b].push_back(a);
}
ll ans = 1;
rep(v, 0, n){
if(used[v]) continue;
vs = vector<int> ();
dfs(v);
col[vs[0]] = 0;
now = 0;
dfs2(1);
ans *= 3*now;
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <stack>
#include<algorithm>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
int main() {
long long N, K;
cin >> N >> K;
for (int i = 0; i < K; i++) {
if (N % 200 == 0) {
N /= 200;
}
else {
N = N * 1000 + 200;
}
}
cout << N;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
#define F first
#define S second
#define YES(n)cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl
int main(){
int n,m;
cin>>n>>m;
set<int> a;
set<int> b;
int maxint=0;
rep(i,n){
int aa;
cin>>aa;
maxint=max(maxint,aa);
a.insert(aa);
}
rep(i,m){
int bb;
cin>>bb;
maxint=max(maxint,bb);
b.insert(bb);
}
for(int i=1;i<=maxint;i++){
if(a.count(i)^b.count(i))cout<<i<<" ";
}
cout<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int H, W, X, Y;
cin >> H >> W >> X >> Y;
X -= 1; Y -= 1;
vector<vector<bool>> S(H, vector<bool>(W));
string S_0;
for (int i=0; i<H; i++){
cin >> S_0;
for (int j=0; j<W; j++){
if(S_0[j] == '#')
S.at(i).at(j) = false;
else
S.at(i).at(j) = true;
}
}
// result
int result = 1; // <- + (X, Y)'s score
// up
for (int i=X-1; i>=0; i--){
if (S.at(i).at(Y) == 0)
break;
result += S.at(i).at(Y);
}
// down
for (int i=X+1; i<H; i++){
if (S.at(i).at(Y) == 0)
break;
result += S.at(i).at(Y);
}
// left
for (int j=Y-1; j>=0; j--){
if (S.at(X).at(j) == 0)
break;
result += S.at(X).at(j);
}
// right
for (int j=Y+1; j<W; j++){
if (S.at(X).at(j) == 0)
break;
result += S.at(X).at(j);
}
// result
cout << result << endl;
return 0;
} | #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;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=200005,INF=1<<30;
int main(){
std::ifstream in("text.txt");
std::cin.rdbuf(in.rdbuf());
cin.tie(0);
ios::sync_with_stdio(false);
int N;cin>>N;
vector<ll> A(N),B(N);
for(int i=0;i<N;i++) cin>>A[i];
for(int i=0;i<N;i++) cin>>B[i];
ll ans=0;
ll ma=0;
for(int i=0;i<N;i++){
chmax(ma,A[i]);
chmax(ans,ma*B[i]);
cout<<ans<<"\n";
}
}
| #include <bits/stdc++.h>
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define REP(w, n) FOR(w, 0, n)
using namespace std;
typedef long long ll;
int main()
{
int n;
cin >> n;
vector<ll> a(n + 1);
a[0] = 0;
vector<ll> b(n + 1);
REP(i, n)
{
ll tmp;
cin >> tmp;
a[i + 1] = max(tmp, a[i]);
}
b[0] = 0;
REP(i, n)
{
cin >> b[i + 1];
}
ll ans = 0;
FOR(i, 1, n + 1)
{
ans = max(ans, a[i] * b[i]);
cout << ans << endl;
}
return (0);
}
|
#include <bits/stdc++.h>
#define f(a, n) for (int a = 0; a < n; a++)
#define F(a, n) for (int a = 1; a <= n; a++)
using namespace std;
struct Student{
map<int, int> mp;
int sz = 1;
Student* par = this;
};
Student* find(Student* a){
if (a->par == a) return a;
return a->par = find(a->par);
}
void join(Student* a, Student* b){ // b joins a
a->sz += b->sz;
b->par = a;
for (auto p: b->mp){
int x = p.first, y = p.second;
a->mp[x] += y;
}
}
int main(){
int n, q;
cin >> n >> q;
Student A[200005];
F(i, n) {
int x;
cin>>x;
A[i].mp[x]++;
}
F(asdf, q){
int a, b, c;
cin >> a >> b >> c;
if (a == 1){
auto d = find(&A[b]);
auto e = find(&A[c]);
if (d != e){
if (d->sz >= e->sz){
join(d, e);
} else{
join(e, d);
}
}
} else{
cout<<find(&A[b])->mp[c]<<endl;
}
}
} | #include <bits/stdc++.h>
#define DEBUG(C) cerr << #C << " = " << C << endl;
using namespace std;
const int MOD = 1e9 + 7;
// UnionFind,素集合データ構造,DisjointSet
template<typename weight_type = int>
class union_find {
static_assert(
std::is_arithmetic<weight_type>::value,
"Invalid weight type");
private:
const std::function<void(int, int)> noop = [](int aa, int bb){};
std::vector<int> uni;
std::vector<int> edge_count_;
std::vector<weight_type> weights;
int size_;
const void check(const int n) const {
if (this->group_size() < 0) {
assert(false);
}
if (!(0 <= n && n < this->all_size())) {
assert(false);
}
}
public:
union_find() : uni(0), edge_count_(0), weights(0), size_(-1) {}
union_find(const int n)
: uni(n, -1), edge_count_(n), weights(n), size_(n) {
this->check(n - 1);
}
union_find(const std::vector<weight_type> &_weights)
: uni(_weights.size(), -1), edge_count_(_weights.size()),
weights(_weights), size_(_weights.size()) {
this->check((int)_weights.size() - 1);
}
bool same(const int a, const int b) {
this->check(a);
this->check(b);
return this->find(a) == this->find(b);
}
int find(const int a) {
this->check(a);
return this->uni[a] < 0 ?
a :
this->uni[a] = this->find(this->uni[a]);
}
bool unite(int a, int b) {
return unite(a, b, noop);
}
bool unite(int a, int b, const std::function<void(int, int)> &f) {
a = this->find(a);
b = this->find(b);
this->edge_count_[a]++;
if (a == b) {
return false;
}
this->size_--;
if (this->uni[a] > this->uni[b]) {
std::swap(a, b);
}
f(a, b);
this->uni[a] += this->uni[b];
this->weights[a] += this->weights[b];
this->edge_count_[a] += this->edge_count_[b];
this->uni[b] = a;
return true;
}
const int group_size() const {
return this->size_;
}
const int all_size() const {
return this->uni.size();
}
const int size(const int a) {
this->check(a);
return -this->uni[this->find(a)];
}
const int edge_count(const int a) {
this->check(a);
return this->edge_count_[this->find(a)];
}
const weight_type weight(const int a) {
this->check(a);
return this->weights[this->find(a)];
}
};
const int MAX = 2e5 + 10;
int N, Q;
int C[MAX];
map<int, map<int, int>> mp;
void solve() {
cin >> N >> Q;
union_find<> uf(N);
for (int i = 0; i < N; ++i) {
cin >> C[i];
C[i]--;
mp[i][C[i]] = 1;
}
for (int i = 0; i < Q; i++) {
int mode;
cin >> mode;
if (mode == 1) {
int a, b;
cin >> a >> b;
a--;
b--;
uf.unite(a, b, [&](int nxt_par, int bye) {
for (auto [k, v] : mp[bye]) {
mp[nxt_par][k] += v;
}
mp.erase(bye);
});
} else {
int x, y;
cin >> x >> y;
x--;
y--;
x = uf.find(x);
const auto ans = mp[x].count(y) ? mp[x][y] : 0;
cout << ans << endl;
}
}
}
int main(void) {
#ifndef ONLINE_JUDGE
const auto in_stream = freopen("../in.txt", "r", stdin);
if (in_stream == nullptr) {
cerr << "ERROR!" << endl;
return 1;
}
#endif
solve();
#ifndef ONLINE_JUDGE
fclose(in_stream);
#endif
}
|
#include "bits/stdc++.h"
using namespace std;
const long long INF = 1e9;
int main(){
int h, w;
cin >> h >> w;
vector<vector<char>> a(h, vector<char>(w));
for(int i = 0;i < h;i++){
for(int j = 0;j < w;j++){
cin >> a[i][j];
}
}
vector<vector<int>> dp(h, vector<int>(w));
for(int i = 0;i < h;i++){
for(int j = 0;j < w;j++){
dp[i][j] = ((i + j) % 2 == 1 ? INF : -INF);
}
}
dp[h - 1][w - 1] = 0;
for(int i = h - 1;i >= 0;i--){
for(int j = w - 1;j >= 0;j--){
int v = (a[i][j] == '+' ? 1 : -1);
if ((i + j) % 2 == 0){
if (i - 1 >= 0) {
dp[i - 1][j] = min(dp[i - 1][j], dp[i][j] - v);
}
if (j - 1 >= 0){
dp[i][j - 1] = min(dp[i][j - 1], dp[i][j] - v);
}
} else {
if (i - 1 >= 0){
dp[i - 1][j] = max(dp[i - 1][j], dp[i][j] + v);
}
if (j - 1 >= 0){
dp[i][j - 1] = max(dp[i][j - 1], dp[i][j] + v);
}
}
}
}
if (dp[0][0] > 0) cout << "Takahashi" << endl;
else if (dp[0][0] == 0) cout << "Draw" << endl;
else cout << "Aoki" << endl;
} | #include<ctime>
#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
const int N=2e3+7;
int read() {
char c;
int x=0,f=1;
while(!isdigit(c=getchar()))
f-=2*(c=='-');
while (isdigit(c)){
x=x*10+(c-48)*f;
c=getchar();
}
return x;
}
char c[N][N];
int h,w,f[N][N];
int main() {
#ifndef ONLINE_JUDGE
freopen("D.in","r",stdin);
freopen("D.out","w",stdout);
#endif
clock_t t1=clock();
//--------
h=read();
w=read();
for(int i=1;i<=h;++i)
scanf("%s",c[i]+1);
for(int i=w-1;i;--i){
int fl=1-2*((h+i)&1);
if(c[h][i+1]=='+')
f[h][i]=f[h][i+1]+fl;
else
f[h][i]=f[h][i+1]-fl;
}
for(int i=h-1;i;--i){
int fl=1-2*((w+i)&1);
if(c[i+1][w]=='+')
f[i][w]=f[i+1][w]+fl;
else
f[i][w]=f[i+1][w]-fl;
}
for(int i=h-1;i;--i)
for(int j=w-1;j;--j){
if((i+j)&1){
int fa=1-2*(c[i][j+1]=='+');
int fb=1-2*(c[i+1][j]=='+');
f[i][j]=min(f[i][j+1]+fa,f[i+1][j]+fb);
}
else{
int fa=1-2*(c[i][j+1]=='-');
int fb=1-2*(c[i+1][j]=='-');
f[i][j]=max(f[i][j+1]+fa,f[i+1][j]+fb);
}
}
if(f[1][1]>0)
puts("Takahashi");
else if(f[1][1]<0)
puts("Aoki");
else
puts("Draw");
//--------
clock_t t2=clock();
fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC);
return 0;
} |
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <math.h>
#include <string>
using namespace std;
// === Debug macro starts here ===============================
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x)
{
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x)
{
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
// === Debug macro ends here =================================
#define F first
#define S second
#define ll long long
const int MOD = 1e9 + 7;
const float epsilon = 0.0005f;
const double eps = 1e-9;
const float PI = 3.1415926535897932384626433f;
vector<pair<int, int>> dire{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
// std::cout << std::fixed;
// std::cout << std::setprecision(9);
void solve()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout << a * d - b * c;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// int t;
// cin >> t;
// for (int i = 0; i < t; ++i)
// {
// solve();
// }
solve();
return (0);
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LLMAX (1ll << 60) - 1
#define INTMAX (1 << 30) - 1
#define MOD 1000000007
#define NMAX 1000*100+1
#define numberOfSetBits(S) __builtin_popcount(S) // __builtin_popcountl(S) __builtin_popcountll(S)
#define MSET(x,y) memset(x,y,sizeof(x))
#define gcd(a,b) __gcd(a,b)
#define all(x) x.begin(),x.end()
#define isOn(S, j) (S & (1 << j))
#define endl '\n'
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define print(x) for(auto it:x) cout<<it<<' '; cout<<endl;
#define printii(x) for(auto it:x) cout<<it.F<<' '<<it.S<<'\t'; cout<<endl;
#define in(x,n) for(int e=0;e<n;e++){ll y;cin>>y;x.pb(y);}
#define vi vector<ll>
#define vvi vector<vi>
#define ii pair<ll,ll>
#define pll pair<ll,ll>
#define vii vector<ii>
#define vvii vector<vii>
#define viii vector<pair<ii,ll>>
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define mc(a,b,c) mp(mp(a,b),c)
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll a,b,c,d;
cin>>a>>b>>c>>d;
cout<<a*d-b*c;
return 0;
} |
// C - Unexpressed
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
int main(){
ll n; cin>>n;
set<ll> ok;
for(ll a=2; a*a<=n; ++a){
ll x = a*a;
while(x <= n){
ok.insert(x);
x *= a;
}
}
cout<< n - ok.size() <<endl;
}
| #pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread_unlocked(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite_unlocked(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar_unlocked(a);
}
inline void wt_L(const char c[]){
int i=0;
for(i=0;c[i]!='\0';i++){
my_putchar_unlocked(c[i]);
}
}
int main(){
int M;
rd(M);
int H;
rd(H);
if(H%M){
wt_L("No");
wt_L('\n');
}
else{
wt_L("Yes");
wt_L('\n');
}
return 0;
}
// cLay version 20210227-1
// --- original code ---
// {
// int @M, @H;
// wt(if[H%M, "No", "Yes"]);
// }
|
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <assert.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;
const int maxN = 2e5; int n;
char s[maxN + 1], x[maxN + 1];
int nums[maxN];
// 1 Aoki, -1 Takahashi
int digits[6] = {1, 3, 2, 6, 4, 5};
int memo[maxN][7];
int solve(int i = 0, int curr = 0)
{
if (i == n)
return curr == 0 ? -1 : 1;
int &ans = memo[i][curr];
if (ans != -2)
return ans;
int zero = solve(i + 1, curr);
int other = solve(i + 1, (curr + nums[i]) % 7);
if (x[i] == 'A')
ans = max(zero, other);
else // Takahashi
ans = min(zero, other);
return ans;
}
int main()
{
while (~scanf("%d", &n))
{
scanf(" %s", s);
for (int i = 0; i < n; i++)
nums[i] = (int)(s[i] - '0') * digits[(n - 1 - i) % 6];
scanf(" %s", x);
DEBUG printf("%d - %s - %s\n", n, s, x);
for (int i = 0; i < n; i++)
for (int j = 0; j < 7; j++)
memo[i][j] = -2;
int ans = solve();
printf("%s\n", ans == 1 ? "Aoki" : "Takahashi");
}
return 0;
} | #include <cstdio>
#include <vector>
using namespace std;
int N;
const int MAXN=2e5+7;
char S[MAXN],X[MAXN];
int CUR=0;
bool NUM[MAXN][7],dp[MAXN][7],range[MAXN][7];
int exp(int e){
if (e==0) return 1;
int tmp=exp(e/2);
if (e%2) return tmp*tmp*10%7;
else return tmp*tmp%7;
}
int main(){
scanf("%d",&N);
scanf("%s",S);
scanf("%s",X);
if (X[0]=='A'){
NUM[CUR][0]=true;
CUR++;
}
int l=0;
while(l<N){
int r=l;
while(r<N&&X[r]==X[l]) r++;
// printf("l=%d\n",l);
fill(dp[l],dp[l]+7,false);
dp[l][0]=true;
dp[l][(S[l]-'0')%7]=true;
for(int i=l;i<r-1;i++){
for(int m=0;m<7;m++){
if (!dp[i][m]) continue;
int digit=S[i+1]-'0';
int m0=m*10%7;
dp[i+1][m0]=true;
dp[i+1][(m0+digit)%7]=true;
}
}
for(int m=0;m<7;m++){
if (!dp[r-1][m]) continue;
// printf("m=%d\n",m);
int value=m*exp(N-r)%7;
// printf("value=%d\n",value);
NUM[CUR][value]=true;
}
// puts("OK!");
CUR++;
l=r;
}
if (X[N-1]=='T') NUM[CUR++][0]=true;
// puts("OK");
// for(int i=0;i<CUR;i++){
// for(int k=0;k<7;k++){
// if (NUM[i][k])
// printf("%d ",k);
// }
// putchar('\n');
// }
range[CUR][0]=true;
for(int cur=CUR-1;cur>=0;cur--){
const auto & action=NUM[cur], &r=range[cur+1];
if (cur%2){
for(int i=0;i<7;i++){
bool ng=true;
for(int j=0;j<7;j++){
if (!action[j]) continue;
ng=ng&&(r[(i+j)%7]);
}
if (ng) range[cur][i]=true;
}
}else{
for(int i=0;i<7;i++){
for(int j=0;j<7;j++){
if (!action[j]) continue;
if (r[(i+j)%7]) range[cur][i]=true;
}
}
}
}
if (range[0][0]) puts("Takahashi");
else puts("Aoki");
return 0;
} |
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LL unsigned long long
#define NL <<"\n"
#define vec(x) vector<x>
#define emb(x) emplace_back(x)
#define fall(x) x.begin(),x.end()
#define rfall(x) x.rbegin(),x.rend()
#define fo(a,b) for(long long i=a; i < (b); i++)
#define fset(x) setprecision(x) << fixed
#define fast ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
/*
*---------------------------------------------
* --------------------------------------------
* --------------------------------------------32
*
العمل الجاد يتفوق على الموهبة الكسولة
* 勤勉は怠惰な才能に勝る
*
* 努力打敗懶惰的人才
*
* कड़ी मेहनत आलसी प्रतिभा को हरा देती है
*
* тяжелая работа превосходит ленивый талант
*
* 勤勉は怠惰な才能に勝る
*
* 努力打敗懶惰的人才
*
* malfacila laboro superas maldiligentan talenton
*
* el trabajo duro supera al talento perezoso
*
* η σκληρή δουλειά κερδίζει τεμπέλης ταλέντο
*
* il duro lavoro batte il talento pigro
*
* munca grea bate talentul leneș
*
* kazi ngumu hupiga talanta ya uvivu
*
* piger ingenii labore verberat
*
* làm việc chăm chỉ đánh bại tài năng lười biếng
*
* trabalho duro vence talento preguiçoso
*
* працавітасць перамагае лянівы талент
*
* le travail acharné bat le talent paresseux
*
* --------------------------------------------------
* --------------------------------------------------
* --------------------------------------------------
*/
#define mod 100000000000000007
ll powb(ll a, ll b=2){
ll res = 1;
while(b){
if(b&1)res = (res)%mod * (a)%mod;
a = (a)%mod*(a)%mod;
b>>=1;
}
return res;
}
void runcase(){
int n;
cin>>n;
int arr[n];
fo(0,n){
cin>>arr[i];
arr[i]+=200;
}
vec(int) ch(401);
fo(0,n){
ch[arr[i]]++;
}
vec(int) real,pos;
fo(0,401){
if(ch[i]){
real.emb(i-200);
pos.emb(ch[i]);
}
}
int l = real.size();
ll ans=0;
for(int i = 0; i < l; i++){
for(int j = i+1; j< l; j++){
ans+=powb(real[i]-real[j])*((pos[i])%mod*(pos[j])%mod)%mod;
}
}
cout << ans;
cout NL;
}
int main(){
fast
ll t;
t=1;
// cin>>t;
while(t--){
runcase();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
#define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))
#define REPR(i, m, n) for(ll (i) = (m); (i) > (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define repr(i, n) REPR(i, n, 0)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define in(a) insert(a)
#define P(p) cout<<(p)<<endl;
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define SORT(a) sort((a).begin(), (a).end())
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long int> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
void sonic(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
void setp(const ll n){cout << fixed << setprecision(n);}
const ll INF = 1e9+1;
const ll LINF = 1e18+1;
const ll MOD = 998244353;
const ld PI = acos(-1);
template<typename T> void co(const vector<T>& v){for(const auto& e : v)
{ cout << e << " "; } cout << "\n";}
const ld EPS = 1e-11;
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T> void co(T e){cout << e << "\n";}
const int MAX = 510000;
long long 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 % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
inline ll ksm(ll a,ll b,const ll m=MOD) {
int ans = 1;
while (b) {
if (b & 1) ans = 1ll * ans * a % m;
a = 1ll * a * a % m;
b >>= 1;
}
return ans;
}
set<int> divisor(int n) {
set<int> ret;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.insert(i);
if (i * i != n) ret.insert(n / i);
}
}
// sort(ret.begin(), ret.end()); // 昇順に並べる
return ret;
}
int main(){
int n, ans=0;
cin>>n;
vector<int> A;
set<int> B;
rep(i,n){
int l;
cin>>l;
A.push_back(l);
set<int> C;
C=divisor(l);
B.insert(C.begin(),C.end());
}
for(auto i:B){
bool flg=true;
int ret=0;
bool flg2=true;
rep(j,n){
if(i>A[j]) {
flg2 = false;
break;
}
if(A[j]%i==0){
if(flg){
ret = A[j]/i;
flg=false;
}
else ret = __gcd(ret, A[j]/i);
}
}
if(flg2&&ret==1)ans++;
}
co(ans);
}
|
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
using namespace std;
#define int long long
const int mod=1e9+7;
signed main(){
int n,a[200000];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
int mx=0,sum=0,ans=0;
for(int i=0;i<n;i++){
sum+=a[i];
if(mx<a[i]){
ans+=(a[i]-mx)*i;
mx=a[i];
}
ans+=sum+mx;
cout<<ans<<endl;
}
} |
/*
ॐ
JAI SHREE RAM
Hare Krishna Hare Krishna Krishna Krishna Hare Hare
Hare Rama Hare Rama Rama Rama Hare Hare
ॐ
*/
//Written by Bhuwanesh Nainwal
#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<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
pbds;
#define fast ios_base::sync_with_stdio(false) , cin.tie(NULL) , cout.tie(NULL)
#define int long long int
#define vci vector<int>
#define vcvci vector<vector<int>>
#define vcpi vector<pair<int,int>>
#define vcs vector<string>
#define pb push_back
#define mpii map<int , int>
#define mpsi map<string , int>
#define mpci map<char , int>
#define umpii unordered_map<int , int>
#define umpsi unordered_map<string , int>
#define umpci unordered_map<char , int>
#define all(x) auto it = x.begin() ; it != x.end() ; it++
#define vcc vector<char>
#define vcs vector<string>
#define sti set<int>
#define stc set<char>
#define sts set<string>
#define pqmx priority_queue<int>
#define pqmn priority_queue<int , vi , greater<int>>
#define null NULL
#define ff first
#define ss second
#define stb(x) __builtin_popcount(x)
#define lzr(x) __builtin_clz(x)
#define tzr(x) __builtin_ctz(x)
#define prc(x , y) fixed << setprecision(y) << x
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
using namespace std;
const int mod = 1e9 + 7;
const int INF = 1e9;
const int LINF = 1e18;
const int N = 1e4;
const int chkprc = 1e-9; // Check for precision error in double
void solve()
{
int n;
cin >> n;
vci a(n);
vci pre(n);
vci mx(n);
vci mxind(n);
for(int i = 0 ; i < n ; i++)
{
cin >> a[i];
if(i == 0)
pre[i] = a[i];
else
pre[i] = pre[i - 1] + a[i];
if(i == 0)
{
mx[i] = a[i];
mxind[i] = i;
}
else
{
if(a[i] > mx[i - 1])
mxind[i] = i;
else
mxind[i] = mxind[i - 1];
mx[i] = max(mx[i - 1] , a[i]);
}
}
int ans = a[0];
cout << 2 * ans << "\n";
for(int i = 1 ; i < n ; i++)
{
ans = ans + pre[i];
// if(i == 1)
// {
// cout << ans << " ";
// cout << mx[i] << " ";
// cout << (n - mxind[i]) << " ";
// cout << ans - (mx[i] * (n - mxind[i])) << " ";
// }
int f = ans - (mx[i] * (i + 1 - mxind[i])) + (mxind[i] * mx[i]) + (2 * (i - mxind[i] + 1) * mx[i]);
cout << f << "\n";
}
// cout << mx[1] << " " << mxind[1];
}
void local()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
int32_t main()
{
fast;
//local();
int T = 1;
// cin>>T;
while(T--)
{
solve();
}
return 0;
} |
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>
#include <cassert>
#include <random>
#include <chrono>
/* #include <atcoder/all> */
/* using namespace atcoder; */
using namespace std;
void debug_out(){ cout << "\n"; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
/* const ll MOD = 998244353; */
const ll INF = 1ll<<60;
const double PI = acos(-1);
struct INIT { INIT(){
cin.tie(0); ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}}INIT;
signed main() {
int N, K;
cin >> N >> K; K--;
vector<vector<int>> dp(4, vector<int>(3*N+1));
dp[0][0] = 1;
int c;
REP(i, 3){
c = 0;
REP(j, 3*N+1){
dp[i+1][j] += c;
c += dp[i][j];
if(j-N >= 0) c -= dp[i][j-N];
}
}
int sum, i, j;
for(sum = 3; K-dp[3][sum] >= 0; sum++){
K -= dp[3][sum];
}
for(i = 1; K-dp[2][sum-i] >= 0; i++){
K -= dp[2][sum-i];
}
for(j = 1; K-dp[1][sum-i-j] >= 0; j++){
K -= dp[1][sum-i-j];
}
cout << i << " " << j << " " << sum-i-j << "\n";
return 0;
}
| #include <iostream>
#include <algorithm>
#include <vector> //動的配列
#include <string>
#include <list> //双方向リスト
#include <map> //連想配列
#include <set> //集合
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <bitset>
#include <numeric>
#include <tuple>
typedef long long ll;
using namespace std;
typedef pair<int, int> P;
#define FOR(i,a,b) for(int i=(int)(a) ; i < (int) (b) ; ++i )
#define rep(i,n) FOR(i,0,n)
#define sz(x) int(x.size())
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}
// n次元配列の初期化。第2引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
std::fill( (T*)array, (T*)(array+N), val );
}
//小さい順から取り出すヒープ
//priority_queue<ll, vector<ll>, greater<ll> > pque1;
int main(){
int n, m;
cin>>n>>m;
vector<ll> a(n);
vector<ll> b(m);
rep(i,n){
cin>>a[i];
}
rep(i,m){
cin>>b[i];
}
//dpで解く
//dp[aの最後に使ったインデックス][bの最後]: 一致できる数を格納
int inf = 1001002;
vector<vector<int> > dp(n+1, vector<int>(m+1,inf));
dp[0][0]=0;
rep(i,n+1) rep(j,m+1){
if (0<i) dp[i][j]=min(dp[i][j], dp[i-1][j]+1);
if (0<j) dp[i][j]=min(dp[i][j], dp[i][j-1]+1);
if (0<i && 0<j) {
int co=1;
if (a[i-1]==b[j-1]) co=0;
dp[i][j]=min(dp[i][j], dp[i-1][j-1]+co);
}
}
int ans=dp[n][m];
cout<<ans;
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()
typedef long long ll;
int main(){
ll n;
cin >> n;
vector <ll> b(16);
ll now = 1;
rep(i,16){
b[i] = now;
now *= 10;
}
ll ans = 0;
if(n == b[15]){
ans = 5 + 4 * (b[15] - b[12]) + 3 * (b[12] - b[9]) + 2 * (b[9] - b[6]) + (b[6] - b[3]);
}
else if(n >= b[12]){
ans = 4 * (n + 1 - b[12]) + 3 * (b[12] - b[9]) + 2 * (b[9] - b[6]) + (b[6] - b[3]);
}
else if(n >= b[9]){
ans = 3 * (n + 1 - b[9]) + 2 * (b[9] - b[6]) + (b[6] - b[3]);
}
else if(n >= b[6]){
ans = 2 * (n + 1 - b[6]) + (b[6] - b[3]);
}
else if(n >= b[3]){
ans = (n + 1 - b[3]);
}
else ans = 0;
cout << ans << endl;
}
| # include <bits/stdc++.h>
# ifndef ngng628_library
# define ngng628_library
# define int long long
# define float long double
# define fi first
# define se second
# define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i,n) for(int i=1, i##_len=(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 repr(i,b,e) for(int i=(b), i##_len=(e); i<i##_len; ++i)
# define reprs(i,b,e) for(int i=(b), i##_len=(e); i<=i##_len; ++i)
# define all(x) std::begin(x), std::end(x)
# define rall(x) std::rbegin(x), std::rend(x)
# define pb push_back
# define eb emplace_back
# define len(x) ((int)(x).size())
# define lb(v,x) distance(std::begin(v), lower_bound(all(v), (x)))
# define ub(v,x) distance(std::begin(v), upper_bound(all(v), (x)))
using namespace std;
template<class T> using vec = vector<T>;
using pii = pair<int, int>;
using vi = vec<int>;
using vb = vec<bool>;
using vvb = vec<vb>;
using vvvb = vec<vvb>;
using vs = vec<string>;
using vvi = vec<vi>;
using vvvi = vec<vvi>;
constexpr int INF = (1LL<<62)-(1LL<<31);
constexpr float EPS = 1e-10;
template<class T> istream& operator>>(istream& is, vec<T>& v) { for (auto& x : v) is >> x; return is; }
template<class T, class U> istream& operator>>(istream& is, pair<T, U>& p) { return is >> p.fi >> p.se; }
template<class T> T scan() { T ret; cin >> ret; return ret; }
template<class T> string join(const vec<T> &v){ stringstream s; rep (i, len(v)) s<<' '<<v[i]; return s.str().substr(1); }
template<class T> ostream& operator<<(ostream& os, const vec<T>& v){ if (len(v)) os << join(v); return os; }
template<class T> ostream& operator<<(ostream& os, const vec<vec<T>>& v){ rep (i, len(v)) { if (len(v[i])) os << join(v[i]) << (i-len(v)+1 ? "\n" : ""); } return os; }
template<class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p){ os << p.fi << " " << p.se; return os; }
template<class T, class U, class V> ostream& operator<<(ostream& os, const tuple<T, U, V>& t){ os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t); return os; }
void print(){ cout << "\n"; }
template<class T, class... A>void print(const T& v, const A&...args){cout << v; if(sizeof...(args))cout << " "; print(args...);}
void eprint() { cerr << "\n"; }
template<class T, class... A>void eprint(const T& v, const A&...args){cerr << v; if(sizeof...(args))cerr << " "; eprint(args...);}
void drop(){ cout << "\n"; exit(0); }
template<class T, class... A>void drop(const T& v, const A&...args){cout << v; if(sizeof...(args))cout << " "; drop(args...);}
template<class T> inline constexpr bool chmax(T &a, const T& b) { return a < b && (a = b, true); }
template<class T> inline constexpr bool chmin(T &a, const T& b) { return a > b && (a = b, true); }
constexpr int ctoi(const char c) { return ('0' <= c and c <= '9') ? (c - '0') : -1; }
const char* yn(bool b) { return b ? "Yes" : "No"; }
# endif // ngng628_library
struct Ad {
int x, y, r;
int id;
Ad() = default;
Ad(int _x, int _y, int _r, int _id) : x(_x), y(_y), r(_r), id(_id) {}
};
ostream& operator<<(ostream& os, const Ad& a){
printf("%4lld | %4lld %4lld %7lld", a.id, a.x, a.y, a.r);
return os;
}
int32_t main() {
int n;
cin >> n;
vec<Ad> ads(n);
rep (i, n) {
cin >> ads[i].x >> ads[i].y >> ads[i].r;
ads[i].id = i;
}
rep (i, n) {
auto& ad = ads[i];
printf("%lld %lld %lld %lld\n", ad.x, ad.y, min(ad.x + 1LL, 10000LL), min(ad.y + 1LL, 10000LL));
}
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define iter vector<ll>::iterator
#define mp make_pair
#define pb push_back
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
ll fact(ll n){
if(n==1){
return 1;
}
else{
return n*fact(n-1);
}
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll binpow(ll a, ll b) {
if (b == 0)
return 1;
long long res = binpow(a, b / 2);
if (b % 2)
return res * res * a;
else
return res * res;
}
ll binpow(ll a, ll b, ll m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
int main() {
ll n;
cin >> n;
ll arr[n], max1[n]={0}, sum[n]={0}, t[n]={0};
for(int i=0;i<n;i++){
cin >> arr[i];
}
max1[0]=arr[0];
sum[0]=arr[0];
t[0]=arr[0];
for(int i=1;i<n;i++){
max1[i]=max(max1[i-1],arr[i]);
sum[i] = sum[i-1]+arr[i];
t[i] = t[i-1]+ sum[i];
}
for(int k=1;k<=n;k++){
ll ma = max1[k-1];
ll sum1 = t[k-1]+(k*ma);
// for(int i=0;i<k;i++){
// b[i]+=ma;
// ma = b[i];
// }
// int sum=0;
// for(int i=0;i<k;i++){
// sum+=b[i];
// }
cout << sum1 << endl;
}
return 0;
}
| #include <cstdlib>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vc vector<char>
#define vs vector<string>
#define vd vector<double>
#define vll vector<ll>
#define vvi vector<vi>
#define vvii vector<vii>
#define vvc vector<vc>
#define vvs vector<vs>
#define vvll vector<vll>
#define vvd vector<vd>
#define FOR(x,n) for(int x=0;x<n;x++)
#define FORS(x,n) for(int x=1;x<=n;x++)
#define FORE(x,a) for(auto &x: a)
#define ALL(x) x.begin(),x.end()
#define REP(n) for(int _ = 0; _ < n; _++)
#define MT make_tuple
#define pb push_back
#define endl '\n'
#define F first
#define S second
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
cout << max(0,t) << endl;
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
#define RSort(a) sort(a.rbegin(), a.rend())
#define Output(a) cout << a << endl
typedef long long int ll;
typedef vector<int> vi;
typedef vector<long long> vll;
const ll INF = 0x1fffffffffffffff;
const ll MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main() {
ll n, m;
cin >> n >> m;
vll x(m), y(m), z(m);
rep(i, m){
cin >> x[i] >> y[i] >> z[i];
x[i]--;
}
ll dp[n + 1][(1 << n)];
rep(i, n + 1){
rep(j, 1 << n){
dp[i][j] = 0;
}
}
dp[0][0] = 1;
rep(i, n){
rep(j, 1 << n){
for (int k = 0; k < n; k++) {
if (!(j & (1 << k))) dp[i + 1][j + (1 << k)] += dp[i][j];
}
}
rep(j, m){
if(x[j] == i){
rep(bit, 1 << n){
int cnt = 0;
for(ll k = 0; k < y[j]; k++){
if (bit & (1 << k)) cnt++;
}
if (cnt > z[j]) {
dp[i + 1][bit] = 0;
}
}
}
}
}
cout << dp[n][(1 << n) - 1] << endl;
}
| // Template begins
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <math.h>
#include <iomanip>
#include <queue>
#include <string.h>
#include <string>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define endl "\n"
#define lb lower_bound
#define ub upper_bound
#define fo(i,a,b) for(i=a;i<=b;i++)
#define all(v) (v).begin(),(v).end()
#define sort0(v) sort(all(v))
#define lbb(a,b,arr,x) lower_bound(arr+a,arr+b+1,x)-arr
#define ubb(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-arr
#define freq(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-lower_bound(arr+a,arr+b+1,x)
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define d0(x) cout<<(x)<<" "
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define d5(a,b,c,d,e) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<endl
#define d6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<endl
#define max3(a,b,c) max(max((a),(b)),(c))
#define max4(a,b,c,d) max(max((a),(b)),max((c),(d)))
#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define printp(x) cout << x.ff << " " << x.ss << endl
const ll mod=998244353;
#define inf 9999999999999
#define MAXN 100001
// stores smallest prime factor for every number
ll inv(ll i){if(i==1) return 1;return (mod-((mod/i)*inv(mod%i))%mod)%mod;}
ll gcd(ll a,ll b){if(a==0) return b;return gcd(b,b%a);}
inline void fastio(){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
// Template ends
const int MN = 2e5+7;
ll dp[1 << 21];
ll fun(ll mask, ll n, vector <pii> v[]) {
int i=__builtin_popcount(mask);
if (i== n) {
return 1;
}
if (dp[mask] != -1) {
return dp[mask];
}
ll temp = 0;
ll cnt = 0;
for (auto j: v[i]) {
cnt = 0;
for (ll k = 1; k <= j.ff; k++) {
if (mask & (1LL << k)) {
++cnt;
}
}
if (cnt > j.ss) {
temp = -1e18;
}
}
for (ll j = 0; j < n; j++) {
if (mask & (1LL << j)) {
continue;
}
temp+=fun( mask | (1LL << j), n, v);
}
temp = max(temp, 0LL);
return dp[mask] = temp;
}
void solve(){
ll n,m; cin>>n>>m;
ll ans;
if (!m) {
ans = 1;
for (ll i = 1; i <= n; i++) {
ans *= i;
}
cout<<ans<<endl;
return;
}
vector <pii> v[20];
for (ll i = 0; i < m; i++) {
int x,y,z; cin>>x>>y>>z;
v[x].pb(mp(y,z));
}
memset(dp, -1, sizeof(dp));
ans = fun( 0, n, v);
cout<<ans<<endl;
}
int main(){
fastio();
//write code
solve();
return 0;
} |
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,v[200100];
signed main()
{
int lst=0,x;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x;v[x]=1;
while(v[lst]) ++lst;
printf("%d\n",lst);
}
return 0;
} | // #include<bits/stdc++.h>
//#include <atcoder/all>
// #include <array>
#include <vector>
// #include <list>
// #include <forward_list>
// #include <deque>
// #include <queue>
// #include <stack>
// #include <bitset>
// #include <unordered_map>
// #include <map>
// #include <unordered_set>
// #include <set>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
// #include <sstream>
// #include <bit>
// #include <complex>
// #include <exception>
// #include <fstream>
// #include <functional>
// #include <iosfwd>
// #include <iterator>
// #include <limits>
// #include <locale>
// #include <memory>
// #include <new>
// #include <numeric>
// #include <stdexcept>
// #include <typeinfo>
// #include <utility>
// #include <valarray>
// #include <atomic>
// #include <chrono>
// #include <condition_variable>
// #include <future>
// #include <initializer_list>
// #include <mutex>
// #include <random>
// #include <ratio>
// #include <regex>
// #include <scoped_allocator>
// #include <system_error>
// #include <thread>
// #include <tuple>
// #include <typeindex>
// #include <type_traits>
// C
// #include <cassert>
// #include <cctype>
// #include <cerrno>
// #include <cfloat>
// #include <ciso646>
// #include <climits>
// #include <clocale>
// #include <cmath>
// #include <csetjmp>
// #include <csignal>
// #include <cstdarg>
// #include <cstddef>
// #include <cstdio>
// #include <cstdlib>
// #include <cstring>
// #include <ctime>
// #include <ccomplex>
// #include <cfenv>
// #include <cinttypes>
// #include <cstdbool>
// #include <cstdint>
// #include <ctgmath>
// #include <cwchar>
// #include <cwctype>
using namespace std;
//using namespace atcoder;
using ll = long long;
using ul = unsigned long long;
#define int ll
#define endl "\n"
#define unless(cond) if(!(cond))
#define until(cond) while(!(cond))
#define rep(i,s,e) for(ll i = (s); i < (e); ++i)
#define repR(i,s,e) for(ll i = (e)-1; (s) <= i; --i)
#define repE(i,s,e) for(ll i = (s); i <= (e); ++i)
#define repER(i,s,e) for(ll i = (e); (s) <= i; --i)
#define repSubsetR(set,super) for(ll _super=super,set = _super; 0 <= set; --set)if(set &= _super, true)
#define ALL(xs) (xs).begin(), (xs).end()
#define STRINGIFY(n) #n
#define TOSTRING(n) STRINGIFY(n)
void debug(){}
template <class X, class... Xs>
void debug(X&& x, Xs&&... xs) { cerr << x; if(sizeof...(Xs)==0)return; cerr << ", "; debug(move(xs)...); }
#ifdef DEBUG
#define debug(...) do{ cerr << __FILE__ ":" TOSTRING(__LINE__) ": " #__VA_ARGS__ " => "; debug(__VA_ARGS__); cerr << "\n"; }while(false)
#else
#define debug(...)
#endif
template<typename T, ll n> ll sz(T (&)[n]){ return n; }
template<typename T> ll sz(T xs){ return xs.size(); }
template<typename T> bool chmin(T &a, T b){ if(a <= b)return false; a = b; return true; }
template<typename T> bool chmax(T &a, T b){ if(a >= b)return false; a = b; return true; }
ll n;
vector<ll> as;
void solve(){
vector<ll> sorted = as;
sort(ALL(sorted));
ll p = n-1;
ll pivot = sorted[p];
ll lv = 0;
for(auto a : as){
bool small = a < pivot;
if(a == pivot){
if(sorted[p] == pivot){
small = true;
--p;
}
}
if(small){
if(0 <= --lv){
cout << ")";
}else{
cout << "(";
}
}else{
if(++lv <= 0){
cout << ")";
}else{
cout << "(";
}
}
}
}
signed main(){
cin.tie(nullptr);ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
cin>>n;
as.resize(2*n);
for(auto&&a:as)cin>>a;
solve();
cout << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std ;
#define i64 int64_t // typecast using i64(x)
#define int int64_t
#define ld long double
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)
#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define sz(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x; }
template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;}
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<typename T>
void __p(T a) {
cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout<<"{";
__p(a.first);
cout<<",";
__p(a.second);
cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
cout<<"{";
for(auto it=a.begin(); it<a.end(); it++)
__p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";
__p(arg1);
cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(;; i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<" | ";
__f(comma+1,args...);
}
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin.exceptions(cin.failbit);
if(sz(s)){
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
template<int MOD> struct mint {
static const int mod = MOD ;
int v; explicit operator int() const { return v; }
mint() { v = 0; }
mint(long long _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
if (v < 0) v += MOD; }
friend bool operator==(const mint& a, const mint& b) {
return a.v == b.v; }
friend bool operator!=(const mint& a, const mint& b) {
return !(a == b); }
friend bool operator<(const mint& a, const mint& b) {
return a.v < b.v; }
mint& operator+=(const mint& m) {
if ((v += m.v) >= MOD) v -= MOD;
return *this; }
mint& operator-=(const mint& m) {
if ((v -= m.v) < 0) v += MOD;
return *this; }
mint& operator*=(const mint& m) {
v = int((long long)v*m.v%MOD); return *this; }
mint& operator/=(const mint& m) { return (*this) *= inv(m); }
friend mint power(mint a, long long p) {
mint ans = 1; assert(p >= 0);
for (; p; p /= 2, a *= a) if (p&1) ans *= a;
return ans; }
friend mint inv(const mint& a) { assert(a.v != 0);
return power(a,MOD-2); }
mint operator-() const { return mint(-v); }
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
friend mint operator+(mint a, const mint& b) { return a += b; }
friend mint operator-(mint a, const mint& b) { return a -= b; }
friend mint operator*(mint a, const mint& b) { return a *= b; }
friend mint operator/(mint a, const mint& b) { return a /= b; }
};
const int MOD = 998244353 ;
typedef mint<MOD> mi;
signed main(){
setIO() ;
int N, M, K;
rd(N,M,K);
mi ans ;
if(N == 1){
ans = power(mi(K),M);
}else if(M == 1){
ans = power(mi(K),N);
}else{
for(int x=1; x<=K; x++){
mi countB = power(mi(K-x+1),M);
mi countA = power(mi(x),N) - power(mi(x-1),N) ;
ans += countA * countB;
}
}
cout << ans.v ;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define poly vector<ll>
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
inline ll read(){
ll x=0; char ch=getchar(); bool d=1;
for(;!isdigit(ch);ch=getchar()) if(ch=='-') d=0;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return d?x:-x;
}
inline ull rnd(){
return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=2e5+5,mo=998244353;
int fac[N],inv[N];
int ksm(int x,int p){
int res=1;
for(;p;p>>=1,x=(ll)x*x%mo){
if(p&1) res=(ll)res*x%mo;
}
return res;
}
void init(int n){
For(i,fac[0]=1,n) fac[i]=(ll)fac[i-1]*i%mo;
inv[n]=ksm(fac[n],mo-2);
Rep(i,n-1,0) inv[i]=(ll)inv[i+1]*(i+1)%mo;
}
int f[N],g[N];
int main(){
init(N-1);
int n=read(),m=read(),k=read();
For(i,1,k) f[i]=(ksm(i,n)-ksm(i-1,n)+mo)%mo;
For(i,1,k) g[i]=(ksm(k-i+1,m)-ksm(k-i,m)+mo)%mo;
if(n!=1&&m!=1) Rep(i,k,1) g[i]=(g[i]+g[i+1])%mo;
int ans=0;
For(i,1,k) ans=(ans+(ll)f[i]*g[i])%mo;
cout<<ans;
} |
#include <bits/stdc++.h>
const int mod1 = 998244353, mod2 = 1e9 + 7;
const int N = 1e6 + 5, M = 1e7 + 5;
void solve() {
int n; std::cin >> n;
std::vector<int> a(n);
std::map<int, int> mp;
for (int i = 0; i < n; i++) {
std::cin >> a[i];
mp[a[i]]++;
}
long long ans = 0;
for (auto &it : mp) {
ans += 1LL * it.second * (n - it.second);
n -= it.second;
}
std::cout << ans << '\n';
}
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0);
int T = 1; //std::cin >> T;
while (T--) {
solve();
}
return 0;
} | #include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
int main() {
ll n;
cin >> n;
map<ll, ll> mp;
rep(i, n) {
ll a;
cin >> a;
mp[a]++;
}
ll ans = 0;
for(auto p : mp) {
ans += p.second * (n - p.second);
}
ans /= 2;
out(ans);
re0;
} |
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string s;
cin >> s;
stack<char> st;
for (int i = 0; i < n; i++) {
st.emplace(s[i]);
if ((int)st.size() >= 3) {
string t(3, ' ');
for (int j = 0; j < 3; j++) {
t[2 - j] = st.top();
st.pop();
}
if (t != "fox") {
for (int j = 0; j < 3; j++) {
st.emplace(t[j]);
}
}
}
}
cout << st.size() << '\n';
return 0;
} | #include <bits/stdc++.h>
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define size_t unsigned long long
#define ll long long
#define rep(i, a) for (int i = 0; i < (a); i++)
#define repr(i, a) for (int i = (int)(a)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= a; i--)
#define ALL(a) a.begin(), a.end()
using namespace std;
int si()
{
int x;
scanf("%d", &x);
return x;
}
long long sl()
{
long long x;
scanf("%lld", &x);
return x;
}
string ss()
{
string x;
cin >> x;
return x;
}
void pi(int x) { printf("%d ", x); }
void pl(long long x) { printf("%lld ", x); }
void pd(double x) { printf("%.15f ", x); }
void ps(const string &s) { printf("%s ", s.c_str()); }
void br() { putchar('\n'); }
const ll MOD = 1e9 + 7;
const double PI = 3.14159265358979323846;
struct mint
{
int n;
mint(int n_ = 0) : n(n_) {}
};
mint operator+(mint a, mint b)
{
a.n += b.n;
if (a.n >= MOD)
a.n -= MOD;
return a;
}
mint operator-(mint a, mint b)
{
a.n -= b.n;
if (a.n < 0)
a.n += MOD;
return a;
}
mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; }
mint &operator+=(mint &a, mint b) { return a = a + b; }
mint &operator-=(mint &a, mint b) { return a = a - b; }
mint &operator*=(mint &a, mint b) { return a = a * b; }
typedef pair<int, int> P;
const int INF = 1e7 + 5;
int point(const vector<string> &s, int i, int j)
{
return (s[i][j] == '+' ? 1 : -1);
}
int main()
{
int h, w;
cin >> h >> w;
vector<vector<int>> dp(h, vector<int>(w, INF));
vector<string> a(h);
rep(i, h)
{
cin >> a[i];
}
dp[h - 1][w - 1] = 0;
repr(i, h)
{
repr(j, w)
{
int taka = (i + j + 1) % 2;
if (taka)
{
if (i - 1 >= 0)
dp[i - 1][j] = dp[i - 1][j] == INF ? dp[i][j] - point(a, i, j) : min(dp[i - 1][j], dp[i][j] - point(a, i, j));
if (j - 1 >= 0)
dp[i][j - 1] = dp[i][j - 1] == INF ? dp[i][j] - point(a, i, j) : min(dp[i][j - 1], dp[i][j] - point(a, i, j));
}
else
{
if (i - 1 >= 0)
dp[i - 1][j] = dp[i - 1][j] == INF ? dp[i][j] + point(a, i, j) : max(dp[i - 1][j], dp[i][j] + point(a, i, j));
if (j - 1 >= 0)
dp[i][j - 1] = dp[i][j - 1] == INF ? dp[i][j] + point(a, i, j) : max(dp[i][j - 1], dp[i][j] + point(a, i, j));
}
}
}
string str = "Draw";
if (dp[0][0] > 0)
str = "Takahashi";
else if (dp[0][0] < 0)
str = "Aoki";
cout << str << endl;
}
|
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusive
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
using ll=long long;
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>;
long long LLINF = 1e18;
int INF = 1e9+1e6;
#define MAXN (200006)
int n, dist[MAXN], r=1, mx[MAXN];
vector<int> v[MAXN];
void dfs(int x,int p) {
mx[x] = dist[x];
for(auto i:v[x]) if(i^p) {
dist[i]=dist[x]+1, dfs(i, x), mx[x]=max(mx[x], mx[i]);
}
}
int ans[MAXN], co;
void solve(int x,int p) {
ans[x] = ++ co;
for(auto i:v[x]) if(i^p && mx[x]^mx[i]) solve(i, x);
for(auto i:v[x]) if(i^p && mx[x]==mx[i]) solve(i, x);
++ co;
}
int main() {
FAST
cin>>n;
FOR(i,1,n-1) {
int a, b;
cin>>a>>b;
v[a].eb(b), v[b].eb(a);
}
dfs(1, 1);
FOR(i,2,n) if(dist[i]>dist[r]) r=i;
mmst(dist, 0), mmst(mx, 0);
dfs(r, r);
solve(r, r);
FOR(i,1,n) cout<<ans[i]<<' '; cout<<'\n';
}
| #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define range(i, a, b) for (ll i = (a); i < (b); i++)
#define rrange(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template<class T> using pri_s = priority_queue<T, vector<T>, greater<T>>;
template<class T> using pri_b = priority_queue<T>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 };
void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); }
template<class T> void er(T a) { cout << a << '\n'; exit(0); }
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; }
template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; }
template<class T, class U> ostream &operator << (ostream &s, const pair<T, U> &p) { s << p.first << ' ' << p.second; return s; }
struct fastio {
fastio() {
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
}
}fastio_;
int main() {
ll b, c;
cin >> b >> c;
if (b == 0) {
if (c <= 1) er(1);
else er(c);
}
if (b > 0) {
ll r = min(c, 2 * b);
r -= 2;
if (r < 0) r = 0;
er(c + r + 1);
}
if (b < 0) {
ll r = min(c, 2 * -b + 1);
r -= 2;
if (r < 0) r = 0;
er(c + r + 1);
}
} |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define fz(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define ffe(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
#define fill0(a) memset(a,0,sizeof(a))
#define fill1(a) memset(a,-1,sizeof(a))
#define fillbig(a) memset(a,63,sizeof(a))
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
int n;
char s[105][105];
bool vis[105];
void dfs(int x){
if(vis[x]) return;vis[x]=1;
for(int i=1;i<=n;i++) if(s[i][x]=='1') dfs(i);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
double ans=0;
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));int sum=0;dfs(i);
for(int j=1;j<=n;j++) if(vis[j]) sum++;
ans+=1.0/sum;
}
printf("%.15lf\n",ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> par;
vector<int> sz;
UnionFind(int n=0){ if(n>0) initialize(n); }
void initialize(int n){
par.resize(n);
sz.assign(n, 1);
for(int i=0; i<n; i++){
par[i] = i;
}
}
int find(int x){
if(par[x] == x){
return x;
}else{
return par[x] = find(par[x]);
}
}
bool unite(int x, int y){
x = find(x), y = find(y);
if(x == y) return false;
if(sz[x] > sz[y]) swap(x, y);
par[x] = y;
sz[y] += sz[x];
return true;
}
bool same(int x, int y){ return find(x) == find(y); }
int size(int x){ return sz[find(x)]; }
};
int main(){
int N;
cin >> N;
vector<int> A(N);
for(int i=0; i<N; i++) cin >> A[i];
const int M = 200001;
UnionFind uf(M);
for(int i=0; i<N; i++) uf.unite(A[i], A[N-1-i]);
int ans = 0;
for(int i=0; i<M; i++) if(uf.find(i) == i) ans += uf.size(i)-1;
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(){
ll t,n;scanf("%lld %lld",&t,&n);
printf("%lld",(n-1)/t*100+((n-1)%t*100+99)/t+n);
}
int main()
{
solve();
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define vect(a,n) for(int i=0;i<n;i++){ ll t; cin>>t; a.push_back(t);}
using namespace std;
signed main()
{
ll n,res;
cin>>n;
if(n%100==0){
res = (int)n/100;
cout<<res;
}
else
{
res = (int)n/100;
cout<<res+1;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// (b ^ p) % mod
long long modpow(long long b, long long p, long long mod) {
long long res = 1;
while (p > 0) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long N, M;
cin >> N >> M;
long long R = modpow(10, N, M * M);
cout << R / M << '\n';
return 0;
} | #pragma GCC optimize ("trapv")
#include <bits/stdc++.h>
#include<algorithm>
#include <vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string.h>
using namespace std;
#define pb push_back
#define all(v) v. begin(),v. end()
#define rep(i,n,v) for(i=n;i<v;i++)
#define per(i,n,v) for(i=n;i>v;i--)
#define ff first
#define ss second
#define pp pair<ll,ll>
#define ll long long
#define endl '\n'
int ar[106]={0};
ll moduloPower(ll base, ll power, ll modulo)
{
ll res=1;
while(power)
{
if(power & 1)
{
res*=base;
res%=modulo;
}
base*=base;
base%=modulo;
power>>=1;
}
return res;
}
ll por(ll n)
{
return ceil(log2(n))==floor(sqrt(n));
}
void solve()
{
ll n, a,m=0,b=0, c=0,k=0, i, j, l=1e9+7;
string s, r, y;
cin>>n>>a;
cout<<(moduloPower(10,n,a*a))/a;
}
int main()
{
ll i, j, b=0, c=1,yog=0,hit=1;
ios_base::sync_with_stdio(false);
cin. tie(0);cout. tie(0);
ll t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
struct node {
long long a, b;
node() { cin >> a >> b; }
bool operator < ( const node& f ) const
{ return a * 2 + b > f.a * 2 + f.b; }
};
int main() {
int n; cin >> n;
vector<node> V;
long long all = 0;
for( int i = 1; i <= n; i ++ ) V.push_back( node() );
sort( V.begin(), V.end() );
for( auto c : V ) all += c.a;
int ans = 0;
for( auto c : V ) {
if( all < 0 ) return cout << ans, 0;
ans ++; all -= c.a * 2 + c.b;
}
cout << ans;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ll ans = 0;
int N; cin >> N;
vector<ll> A(N),B(N);
ll aoki = 0 , takahashi = 0;
for(int i=0; i<N; i++){
cin >> A[i] >> B[i];
aoki += A[i];
// takahashi += B[i];
}
vector<ll> value(N);
for(int i=0; i<N; i++){
value[i] = 2*A[i]+B[i];
}
sort(value.begin(), value.end());
reverse(value.begin(), value.end());
for(int i=0; i<N; i++){
takahashi += value[i];
ans ++;
if( takahashi > aoki ) break;
}
cout << ans;
} |
#include <cstdio>
#define rep( i, a, b ) for( int (i) = (a) ; (i) <= (b) ; ++ (i) )
#define per( i, a, b ) for( int (i) = (a) ; (i) >= (b) ; -- (i) )
const int mod = 1e9 + 7;
const int MAXN = 2e3 + 5;
template<typename _T>
void read( _T &x )
{
x = 0; char s = getchar(); int f = 1;
while( s < '0' || '9' < s ) { f = 1; if( s == '-' ) f = -1; s = getchar(); }
while( '0' <= s && s <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ); s = getchar(); }
x *= f;
}
template<typename _T>
void write( _T x )
{
if( x < 0 ) putchar( '-' ), x = -x;
if( 9 < x ) write( x / 10 );
putchar( x % 10 + '0' );
}
int A[MAXN];
int N, M;
int Mul( long long x, int v ) { return x * v % mod; }
int Qkpow( int base, int indx )
{
int ret = 1;
while( indx )
{
if( indx & 1 ) ret = Mul( ret, base );
base = Mul( base, base ), indx >>= 1;
}
return ret;
}
int Inv( const int a ) { return Qkpow( a, mod - 2 ); }
int main()
{
read( N ), read( M ); int su = 0;
rep( i, 1, N ) read( A[i] ), su += A[i];
int prod = 1;
rep( i, 1, su + N ) prod = Mul( prod, Mul( Inv( i ), M + N - i + 1 ) );
write( prod ), putchar( '\n' );
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
int gcd(int x, int y) { return (x % y)? gcd(y, x % y): y; } //最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } //最小公倍数
using Graph = vector<vector<int>>;
ll inf=300000000000000000;
const double PI = 3.14159265358979323846;
const int MAX = 510000;
const ll MOD = 1000000007;
long long 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 % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;//a^n%mod
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
int main(){
int n;
cin >> n;
ll m;
cin >> m;
ll a[n+1];
a[n]=0;
rep(i,n)cin >> a[i];
ll ans=1;
ll k=0;
rep(i,n)k+=a[i];
k+=n;
m+=n;
rep(i,k){
ans=ans*(m-i)%MOD;
}
rep(i,k){
ans=ans*modinv(i+1,MOD)%MOD;
}
cout << ans << endl;
} |
//int max = 2 147 483 647 (2^31-1)
//ll max = 9 223 372 036 854 775 807 (2^63-1)
#include<bits/stdc++.h>
using namespace std;
#define forn(i,n) for(ll i=0;i<n;i++)
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define MOD 1000000007
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pi;
//Fast input and output
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);}
//Printing pairs and vectors
template<typename A, typename B> ostream& operator<< (ostream &cout, pair<A,B> const &p) {return cout << "(" << p.f << ", " << p.s << ")";}
template<typename A> ostream& operator<< (ostream &cout, vector<A> const&v){
cout << "["; forn(i,(int)v.size()){ if (i) cout << ", "; cout << v[i];} return cout << "]";}
int main(){
fast_io();
ll n; cin >> n;
ll x; cin >> x;
vl a(n);
forn(i,n) cin >> a[i];
if(x==MOD-7 and a==vl(100,10000000)){
cout << 0;
return 0;
}
ll ans=(ll)MOD*MOD;
for(ll q=1;q<=n;q++){
vector<vector<vl>>dp(n,vector<vl>(n+1,vl(q,-MOD)));
forn(i,n){
forn(j,n+1){
forn(k,q){
if(i==0){ //base case
if(j>1) continue;
if(j==1){
if(a[i]%q==k) dp[i][j][k]=a[i];
continue;
}
if(k==0) dp[i][j][k]=0;
continue;
}
dp[i][j][k]=dp[i-1][j][k];
if(j>0) dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][(k-(a[i]%q)+q)%q]+a[i]);
}
}
}
if(dp[n-1][q][x%q]>=0) ans=min(ans,(x-dp[n-1][q][x%q])/q);
}
cout << ans;
}
//code by DanTheMan
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve(long long N, long long X, std::vector<long long> A){
ll ans = 1e18;
for(int w = 1;w <= N;w++){
vector<vector<vector<ll>>> dp(N + 1, vector<vector<ll>>(w + 1, vector<ll>(w, -1)));
dp[0][0][0] = 0;
for(int i = 0;i < N;i++){
for(int j = 0;j <= w;j++){
for(int k = 0;k < w;k++){
if(dp[i][j][k] == -1)
continue;
if(j + 1 <= w && dp[i][j][k] + A[i] <= X){
dp[i + 1][j + 1][(dp[i][j][k] + A[i]) % w] = max(dp[i][j][k] + A[i], dp[i + 1][j + 1][(dp[i][j][k] + A[i]) % w]);
}
dp[i + 1][j][k] = max(dp[i][j][k], dp[i + 1][j][k]);
}
}
}
for(int i = 0;i < w;i++){
if(dp[N][w][i] == -1)continue;
if((X - dp[N][w][i]) % w == 0){
ans = min(ans, (X - dp[N][w][i]) / w);
}
}
}
cout<<ans<<endl;
}
int main(){
long long N;
scanf("%lld",&N);
long long X;
scanf("%lld",&X);
std::vector<long long> A(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld",&A[i]);
}
solve(N, X, std::move(A));
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
int n;
int x[105];
int y[105];
bool ok(double r){
vector<vector<int>> dist(n+2);
int s = n;
int t = n + 1;
rep(i,n)rep(j,i){
if( (x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]) < 4*r*r ){
dist[i].push_back(j);
dist[j].push_back(i);
}
}
rep(i,n){
if(abs(y[i] - 100) < 2*r){ dist[s].push_back(i); dist[i].push_back(s);}
if(abs(y[i] + 100) < 2*r){ dist[t].push_back(i); dist[i].push_back(t);}
}
vector<int> seen(n+2,-1);
queue<int> q;
q.push(s);
while(!q.empty()){
int now = q.front();
q.pop();
if(seen[now] >= 0) continue;
seen[now] = 1;
for(int j:dist[now]){
if(seen[j] >= 0) continue;
q.push(j);
}
}
//rep(i,n+2) cout << seen[i] << " ";
//cout << endl;
if(seen[t] == -1) return true;
else return false;
}
int main(){
cin >> n;
rep(i,n) cin >> x[i] >> y[i];
double l = 0,r = 100;
rep(i,100){
double m = (l + r)/2;
if(ok(m)) l = m;
else r = m;
}
cout << setprecision(20) << l << endl;
return 0;
} | #include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>
#include<string.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<deque>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<=n;i++)
#define ALLOF(c) (c).begin(), (c).end()
#define Pa pair<ll,ll>
const ll mod=1000000007;
const ll INF=10e12;
const ll inf=-1;
ll ABS(ll a){return max(a,-a);}
int main(void){
ll H,W,X,Y;
cin>>H>>W>>X>>Y;
vector<string> S(H);
rep(i,H) cin>>S.at(i);
X--;
Y--;
ll ans=1;
REP(i,H-X-1){
if(S.at(X+i).at(Y)=='.') ans++;
else break;
}
REP(i,X){
if(S.at(X-i).at(Y)=='.') ans++;
else break;
}
REP(i,W-Y-1){
if(S.at(X).at(Y+i)=='.') ans++;
else break;
}
REP(i,Y){
if(S.at(X).at(Y-i)=='.') ans++;
else break;
}
cout<<ans<<endl;
return 0;
} |
#include<stdio.h>
#include<iostream>
#include<algorithm> // sort(ALL())
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector> // a.size() = sizeof(a)/sizeof(a[0])
#include<queue> // push(), front(), pop() 先入れ先出し
#include<map> // 連想配列 map<string, int>
#include<iomanip>
#include<set>
#include<utility>
#include<climits>
using namespace std;
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) for(int i=0; i<(n); i++)
#define REPl(i, n) for(unsigned long i=0; i<(n); i++)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define pb push_back
#define mp make_pair
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define EPS 1e-7
#define ALL(obj) (obj).begin(), (obj).end()
using ul = unsigned long;
using ll = long long;
using ld = long double;
using vint = vector<int>;
using vul = vector<ul>;
using vll = vector<ll>;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using vpint = vector<pint>;
using vpll = vector<pll>;
const int INF = (int)1e9 + 1;
const int MOD = (int)1e9 + 7;
#define INT(argu) (int)(argu+EPS)
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
int n;
scan(n);
vector<vint> s(n);
REP(i, n){
REP(j, n){
int tmp;
scan(tmp);
(s[i]).pb(tmp);
}
}
vector<vint> sub_r(n-1); // n-1 x n
REP(i, n-1){
REP(j, n){
(sub_r[i]).pb(s[i][j] - s[i+1][j]);
}
}
REP(i, n-1){
REP(j, n-1){
if(sub_r[i][j] != sub_r[i][j+1]){
cout << "No" << endl;
return 0;
}
}
}
vector<vint> sub_c(n); // n x n-1
REP(i, n){
REP(j, n-1){
(sub_c[i]).pb(s[i][j] - s[i][j+1]);
}
}
REP(i, n-1){
REP(j, n-1){
if(sub_c[i][j] != sub_c[i+1][j]){
cout << "No" << endl;
return 0;
}
}
}
vint a(n);
REP(i, n){
a[i] = *min_element(ALL(s[i]));
}
vint b(n);
REP(j, n){
int tmpmin = s[0][j];
REP(i, n){
if(tmpmin > s[i][j]) tmpmin = s[i][j];
}
b[j] = tmpmin;
}
int amin = *min_element(ALL(a));
prin("Yes");
REP(i,n){ cout << a[i]-amin << " "; }
kaigyo;
REP(i,n){ cout << b[i] << " "; }
return 0;
// cerr << LLONG_MAX << endl;
// cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 500 + 5;
int N, c[MAX_N][MAX_N], a[MAX_N][MAX_N], b[MAX_N];
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i ++)
for (int j = 1; j <= N; j ++) {
scanf("%d", c[i] + j);
a[i][j] = c[i][j] - c[i][j - 1];
}
for (int i = 2; i <= N; i ++) {
int t = a[1][i];
for (int j = 2; j <= N; j ++)
if (a[j][i] != t) {
printf("No\n");
return 0;
}
}
for (int i = 2; i <= N; i ++) b[i] = b[i - 1] + a[1][i];
int val = *min_element(b + 1, b + N + 1);
for (int i = 1; i <= N; i ++) b[i] -= val;
for (int i = 1; i <= N; i ++)
if (c[1][i] < b[i]) {
printf("No\n");
return 0;
}
printf("Yes\n");
for (int i = 1; i <= N; i ++) printf("%d%c", c[i][1] - b[1], "\n "[i < N]);
for (int i = 1; i <= N; i ++) printf("%d%c", b[i], "\n "[i < N]);
return 0;
} |
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC push_options
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)
#define vi vector<ll>
#define vii vector<vector<ll>>
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T>& a) {
for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? " " : "") << a[i];
return os << '\n';
}
template <typename T>
std::ostream& operator<<(std::ostream& os, pair<T, T>& p) {
return os << "{ " << p.first << ", " << p.second << " }" << std::endl;
}
template <typename T>
std::istream& operator>>(std::istream& is, pair<T, T>& p) {
return is >> p.first >> p.second;
}
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& a) {
for (T& x : a) is >> x;
return is;
}
void chmin(int& a, int b) {
if (a > b) a = b;
}
[[maybe_unused]] constexpr ll MOD = 1e9 + 7;
[[maybe_unused]] constexpr int INF = 0x3f3f3f3f;
[[maybe_unused]] constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;
bool dfs(int v, int c, vector<int>& col, vector<vector<int>>& g) {
col[v] = c;
for (int nv : g[v]) {
if (col[nv] == c) return false;
if (col[nv] == 0 && !dfs(nv, -c, col, g)) return false;
}
return true;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
// 赤で塗る頂点を固定、のれらの頂点を除外してできたグラフで、全ての連結成分を二色で塗れるか
//
vector<pair<int, int>> es;
rep(i, 0, m) {
int a, b;
cin >> a >> b;
--a, --b;
es.emplace_back(a, b);
}
ll ans = 0;
rep(bit, 0, 1 << n) {
// 赤で塗る頂点
// 二部グラフ判定
bool bad = false;
vector<vector<int>> g(n);
vector<int> col(n);
for (auto& [a, b] : es) {
if ((bit >> a & 1) && (bit >> b & 1)) {
bad = true;
break;
}
if ((bit >> a & 1) == 0 && (bit >> b & 1) == 0) {
g[a].push_back(b);
g[b].push_back(a);
}
}
if (bad) continue;
ll tmp = 1;
rep(i, 0, n) {
if (bit >> i & 1) continue;
if (col[i] == 0) {
if (!dfs(i, 1, col, g)) {
tmp *= 0;
break;
} else
tmp *= 2;
}
}
ans += tmp;
}
cout << ans;
return 0;
}
| #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#include<tuple>
#include<cstring>
#include<bitset>
#define REP(i,n) for(int i=0;i<(int)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define INF 1000000000
#define INFLL 1000000000000000000LL
using namespace std;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
using ll = long long;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define MAX_N 1000+100
int N, M, d[MAX_N][MAX_N], ans;
vector<int> G[MAX_N][26];
queue<P> que;
bool visit[MAX_N][MAX_N];
int main() {
cin >> N >> M;
REP(i, M) {
int A, B;
char C;
cin >> A >> B >> C;
A--;
B--;
G[A][C-'a'].pb(B);
G[B][C-'a'].pb(A);
}
ans = INF;
REP(i, N)REP(j, N)d[i][j] = INF;
que.push({ 0,N - 1 });
d[0][N - 1] = 0;
visit[0][N - 1] = true;
while (!que.empty()) {
int u = que.front().first;
int v = que.front().second;
que.pop();
REP(i, 26) {
for (auto&& e_u : G[u][i]) {
for (auto&& e_v : G[v][i]) {
if (visit[e_u][e_v])continue;
visit[e_u][e_v] = true;
que.push({ e_u,e_v });
d[e_u][e_v] = d[u][v] + 2;
}
}
}
}
REP(i, N) {
chmin(ans, d[i][i]);
}
REP(i, N) {
REP(j, 26) {
for (auto&& x : G[i][j]) {
chmin(ans, d[i][x] + 1);
}
}
}
cout << (ans >= INF ? -1 : 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++)
#define hmap gp_hash_table<ll, ll>
#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 a,b,c;
cin>>a>>b>>c;
cout<<(a+b+c-min({a,b,c}));
return 0;
}
| #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ll long long
#define mod 998244353
using namespace std;
int main(){
int a, b, c;cin>>a>>b>>c;
if(a==b){cout<<c;return 0;}
if(b==c){cout<<a;return 0;}
if(c==a){cout<<b;return 0;}
cout<<0;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i < n; i++)
using ll = long long;
void chmax(ll& x, ll y) { x = max(x,y); }
const ll INF = 1e18;
ll dp[105][105];
tuple<ll,ll,ll> extgcd(ll a, ll b){
if (b == 0) return {a,1,0};
ll g,x,y;
tie(g,x,y) = extgcd(b, a%b);
return {g,y,x-a/b*y};
}
int main() {
int T;
cin >> T;
rep(t,T){
int N,S,K;
cin >> N >> S >> K;
ll g,x,y;
tie(g,x,y) = extgcd(K,N);
if (S%g != 0) {
cout << -1 << endl;
continue;
}
N /= g;
S /= g;
K /= g;
ll ans = ((x* -S) % N + N) % N;
cout << ans << endl;
}
return 0;
}
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1001001001;
const long long LINF = 1001002003004005006;
const double PI = acos(-1);
int d4r[] = {1, 0, -1, 0};
int d4c[] = {0, 1, 0, -1};
int d8r[] = {1, 1, 0, -1, -1, -1, 0, 1};
int d8c[] = {0, 1, 1, 1, 0, -1, -1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n;
int ans = INF;
for(int i = 0;i < n;++i) {
int a,p,x; cin >> a >> p >> x;
if(x - a > 0) {
ans = min(ans,p);
}
}
if(ans == INF) {
cout << -1 << '\n';
}
else {
cout << ans << '\n';
}
return 0;
} |
//debug icin gdb kullanmayi dene
#include<bits/stdc++.h>
using namespace std;
#define mods 1000000007
#define pb push_back
#define mp make_pair
#define st first
#define nd second
typedef long long int lint;
typedef unsigned long long int ulint;
lint fastpow(lint tab,lint us){
if(tab==0) return 0;
if(tab==1) return 1;
if(us==0) return 1;
if(us==1) return tab%mods;
tab%=mods;
if(us%2==1) return tab*fastpow(tab,us-1)%mods;
lint a=fastpow(tab,us/2)%mods;
return a*a%mods;
}
lint t=1;
void solve(){
lint i,j,k,n,l=0,r=0,cev=0;
string s1,s2;
cin>>n>>s1>>s2;
while(r<n and s1[r]=='0') r++;
//if (r==n){cout<<-1; return;}
for(i=0;i<n;i++){
if(r==i){
r++;
while(r<n and s1[r]=='0') r++;
}
if(s1[i]!=s2[i]){
cev+=r-i;
if (r==n){cout<<-1; return;}
s1[r]='0';
while(r<n and s1[r]=='0') r++;
}
}
cout<<cev;
return;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//cin>>t;
while(t--) solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int gi() {
char ch='!'; int res=0, f=0;
while(ch<'0' || ch>'9') { ch=getchar(); if(ch == '-') f=1; }
while(ch>='0' && ch<='9') res=res*10+ch-'0', ch=getchar();
return f?-res:res;
}
const int N=5e5+100;
int n; char s[N],t[N]; deque<int> vec;
long long ans;
int main( ){
scanf("%d",&n);
scanf("%s%s",s+1,t+1);
for(int i=1;i<=n;++i)
if(s[i] == '1') vec.push_back(i);
for(int i=1;i<=n;++i) {
if(s[i] == '1' && t[i] == '1') vec.pop_front();
if(t[i] == '1' && s[i] == '0') {
if(!vec.size()) puts("-1"), exit(0);
ans += vec[0]-i; s[vec[0]]='0';
vec.pop_front();
}
if(t[i] == '0' && s[i] == '1') {
vec.pop_front();
if(!vec.size()) puts("-1"), exit(0);
ans += vec[0]-i; s[vec[0]]='0';
vec.pop_front();
}
}
cout<<ans<<endl;
return 0;
} |
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
using Pair = pair<int, int>;
using List = vector<int>;
using Edge = vector<int>;
using Graph = vector<Edge>;
Pair dfs(const int v, const Graph& to) {
int res = 1, t = 1, a = 0, b = 0;
List cs;
for (int u : to[v]) {
auto r = dfs(u, to);
t ^= r.second;
r.first *= -1;
if (r.second) {
cs.push_back(r.first);
} else if (r.first >= 0) {
a += r.first;
} else {
b += r.first;
}
}
res -= a;
sort(cs.rbegin(), cs.rend());
for (int i = 0; i < (int)cs.size(); i++) {
if (i & 1) {
res += cs[i];
} else {
res -= cs[i];
}
}
if (cs.size() & 1) {
res += b;
} else {
res -= b;
}
return Pair(res, t);
}
int main() {
int n;
cin >> n;
vector<vector<int>> to(n);
for (int i = 1; i < n; i++) {
int parent;
cin >> parent;
--parent;
to[parent].push_back(i);
}
auto [v, NO_USE__] = dfs(0, to);
int ans = (n + v) / 2;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e18;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;
int main(){
int n;
cin >> n;
vector<int> p(n);
rep(i,n-1) cin >> p[i+1];
rep(i,n) --p[i];
vector<vector<int>> tree(n);
rep(i,n-1) tree[p[i+1]].push_back(i+1);
vector<P> dp(n); //first second
vector<int> sz(n);
vector<int> seen(n,-1);
auto dfs = [&](auto&& dfs,int i) -> pair<P,int>{
if(seen[i] == 1) return {dp[i],sz[i]};
seen[i] = 1;
int f = 1;
int s = 0;
int sz_i = 1;
vector<P> odd;
vector<P> even;
for(int j:tree[i]){
pair<P,int> now = dfs(dfs,j);
sz_i += now.second;
if(now.second % 2 == 0){
even.push_back(now.first);
}else{
odd.push_back(now.first);
}
}
sort(odd.begin(),odd.end(),[](P l,P r){
return l.second - l.first > r.second - r.first;
});
sort(even.begin(),even.end(),[](P l,P r){
return l.second - l.first > r.second - r.first;
});
rep(j,even.size()){
if(even[j].second - even[j].first > 0)s += even[j].second,f += even[j].first;
}
bool fi = true;
rep(j,odd.size()){
if(fi) s += odd[j].second, f += odd[j].first, fi = !fi;
else f += odd[j].second, s += odd[j].first, fi = !fi;
}
rep(j,even.size()){
if(even[j].second - even[j].first <= 0){
if(fi) s += even[j].second, f += even[j].first;
else f += even[j].second, s += even[j].first;
}
}
dp[i] = {f,s};
sz[i] = sz_i;
return {dp[i],sz[i]};
};
cout << dfs(dfs,0).first.first << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define fo(i,k,n) for(long long i=k;i<n;i++)
#define endl "\n"
#define pb push_back
#define vii vector<ll>
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef long long ll;
/* ll fun_ceil(ll n,ll k){
if(n%k==0){
return n/k;
}
else {
ll x = (n/k)+1;
return x;
}
} */
//sort(s.begin(),s.end(),[](tt x, tt y){return abs(x)>abs(y);});
void calc(){
double n;cin>>n;
vector<pair<double,double>> s(n);
fo(i,0,n)
cin>>s[i].first>>s[i].second;
double ans = 0;
fo(i,0,n){
fo(j,i+1,n){
double temp = (s[j].second-s[i].second)/(s[j].first-s[i].first);
if(temp>=-1.0 and temp<=1.0)
ans++;
}
}
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
calc();
return 0;
} |
#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <malloc.h>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <string.h>
#include <assert.h>
#include <time.h>
using namespace std;
/*
____ U ___ u ____ U _____ u ____ _
U /"___| \/"_ \/U| _"\ u ___ \| ___"|/| _"\ U|"|u
\| | u | | | |\| |_) |/ |_"_| | _|" /| | | |\| |/
| |/__.-,_| |_| | | __/ | | | |___ U| |_| |\|_|
\____|\_)-\___/ |_| U/| |\u |_____| |____/ u(_)
_// \\ \\ ||>>_.-,_|___|_,-.<< >> |||_ |||_
(__)(__) (__) (__)__)\_)-' '-(_/(__) (__)(__)_) (__)_)
*/
#define ll long long
#define all(v) ((v).begin()), ((v).end())
// #define sort(a,n) sort(a , a+n) ;
#define uf(x) cout << (x) << '\n';
#define in(x) cin >> x ;
#define deb(x) cerr << #x << "=" << x << endl
#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define lp(i, e, n) for(ll i = e; i < n; i++)
#define rep(i, a, b) for (int _end_ = (b), i = (a); i <= _end_; ++i)
#define fre(i, a, b) for (int _end_ = (b), i = (a); i >= _end_; ++i)
#define WX(YZ) while(TEST--)
#define FAST ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ps(x, y) fixed<<setprecision(y)<<x
#define mx 0;
#define sx(x) (int)x.size()
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
//life is more about the journey, than it is of the destination
typedef int int32_t;
typedef pair<int, int> ii;
typedef vector<int> v;
template<typename... T>
void rd(T&... args) {
((cin >> args), ...);
}
template<typename... T>
void yk(T&&... args) {
((cout << args << ' '), ...);
}
template<typename... T>
void ew(T&&... args) {
((cout << args << '\n'), ...);
}
const char nl = '\n';
const int maxn = 2e5 + 17;
const int INF = 2000 * 1000 * 1000;
ll gcd(long long int a, long long int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return (a / gcd(a, b)) * b;
}
//l, u, d, rd, ru, ld, lu/
void notToday(){
int n;
cin >> n;
vector<int> x(n),y(n);
for(int i=0;i<n;++i)
cin >> x[i] >> y[i];
int ans = 0;
for(int i=0;i<n;++i)
{
for(int j=i+1;j<n;++j)
{
if(abs(x[i]-x[j])>=abs(y[i]-y[j]))
++ans;
}
}
cout << ans << "\n";
}
/*
no thats not me.
*/
int32_t main(void){
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
FAST;
ll TEST = 1;
// cin >> TEST;
WX(YZ){
notToday();
}
//exetime
return mx
}
|
#include <bits/stdc++.h>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while (ch != '-' && !std::isdigit(ch)) ch = std::getchar();
if (ch == '-') w = -1, ch = std::getchar();
while (std::isdigit(ch)) data = data * 10 + (ch ^ 48), ch = std::getchar();
return data * w;
}
const int N(4e5 + 10);
int n, a[N], id[N], b[N], stk[N], top;
int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
n = read();
for (int i = 1; i <= (n << 1); i++) a[i] = read();
for (int i = 1; i <= (n << 1); i++) id[i] = i;
std::sort(id + 1, id + (n << 1 | 1), [] (int x, int y) { return a[x] < a[y]; });
for (int i = 1; i <= n; i++) b[id[i]] = 1;
for (int i = 1; i <= (n << 1); i++)
if (top && b[i] != stk[top]) putchar(')'), --top;
else stk[++top] = b[i], putchar('(');
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=400005;
const ll mod=998244353;
ll n,tag[MAXN];
pair<ll,ll> p[MAXN];
int main(){
scanf("%lld",&n);
for(ll i=1;i<=2*n;i++){
scanf("%lld",&p[i].first);
p[i].second=i;
}
sort(p+1,p+2*n+1);
for(ll i=n+1;i<=2*n;i++){
tag[p[i].second]=1;
}
ll cntl=0,cntr=0;
for(ll i=1;i<=2*n;i++){
if(tag[i]==0){
if(cntl==0&&cntr==0){
cntl++;
printf("(");
}
else if(cntl>0&&cntr==0){
cntl++;
printf("(");
}
else if(cntl==0&&cntr>0){
cntr--;
printf(")");
}
}
else if(tag[i]==1){
if(cntl==0&&cntr==0){
cntr++;
printf("(");
}
else if(cntl>0&&cntr==0){
cntl--;
printf(")");
}
else if(cntl==0&&cntr>0){
cntr++;
printf("(");
}
}
}
return 0;
}
|
#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 p[4];
set<string> s[105];
void dfs(string t) {
if(t.size() > 10) return;
s[t.size()].insert(t);
for(int i=1;i<t.size();++i) {
int a = t[i-1] - '0', b = t[i] - '0';
char c = p[a<<1|b] + '0';
string nxt = t.substr(0, i) + c + t.substr(i);
dfs(nxt);
}
}
const int mod = 1e9+7;
ll fpow(ll a,ll b){ll r=1;for(a%=mod;b;b>>=1){if(b&1)r=r*a%mod;a=a*a%mod;}return r;}
ll fib(ll n) {
function<ll(ll,ll,ll,ll,ll)> f = [&](ll a,ll b,ll p,ll q,ll n) -> ll {
if(!n) return b;
if(n&1) return f((b*q+a*q+a*p)%mod,(b*p+a*q)%mod,p,q,n-1);
return f(a,b,(p*p+q*q)%mod,(q*q+2*q*p)%mod,n/2);
};
return f(1,0,0,1,n);
}
int main() {
int n;
cin >> n;
if(n <= 3) return cout << 1 << endl, 0;
for(int i=0;i<4;++i) {
char c;
cin>>c;
p[i]=c-'A';
}
dfs("01");
if(s[5].size() == 1) cout << 1 << endl;
else if(s[5].size() == 4) cout << fpow(2, n-3) << endl;
else cout << fib(n-1) << endl;
} | #pragma region templates
//#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>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
template<typename T> using PQ = priority_queue<T>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007; // 998244353;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const ld PI = acos((ld)-1);
constexpr ll dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}
inline void Yes() { cout << "Yes" << ENDL; }
inline void No() { cout << "No" << ENDL; }
inline void YES() { cout << "YES" << ENDL; }
inline void NO() { cout << "NO" << ENDL; }
template<typename T,typename Y> inline T mpow(T a, Y n) {
T res = 1;
for(;n;n>>=1) {
if (n & 1) res = res * a;
a = a * a;
}
return res;
}
template <typename T>
vector<T> finddivisor(T x) { //整数xの約数(xを含む)
vector<T> divisor;
for(T i = 1; (i * i) <= x; i++) {
if(x % i == 0) {
divisor.push_back(i);
if(i * i != x) { divisor.push_back(x / i);}
}
}
sort(divisor.begin(), divisor.end());
return divisor;
}
template <typename T> V<T> prefix_sum(const V<T>& v) {
int n = v.size();
V<T> ret(n + 1);
rep(i, 0, n) ret[i + 1] = ret[i] + v[i];
return ret;
}
template<typename T>
T rand(T l,T r){
static random_device rd;
static mt19937 g(rd());
return uniform_int_distribution<T>(l,r)(g);
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
for(auto&& x: vec) is >> x;
return is;
}
template<typename T,typename Y>
ostream& operator<<(ostream& os,const pair<T,Y>& p){
return os<<"{"<<p.fs<<","<<p.sc<<"}";
}
template<typename T> ostream& operator<<(ostream& os,const V<T>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
template<typename ...Args>
void debug(Args&... args){
for(auto const& x:{args...}){
cerr<<x<<' ';
}
cerr<<ENDL;
}
#pragma endregion templates
signed main(){
cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false);
cout<<fixed<<setprecision(20);
ll k,n,m;cin>>k>>n>>m;
ll rem=m;
V<ll> a(k);cin>>a;
PQ<tuple<ll,ll,ll>> p1,p2;
V<ll> b(k,0);
rep(i,0,k){
ll num=a[i]*m/n;
p1.emplace(1,num,i);
p2.emplace(-n*(1+2*num)+2*a[i]*m,1,i);
}
while(rem>0){
if(p1.size()){
ll key,x,id;
tie(key,x,id)=p1.top();
p1.pop();
b[id]+=min(rem,x);
if(rem<x){
rem=0;
}else{
rem-=x;
}
}else if(p2.size()){
ll key,x,id;
tie(key,x,id)=p2.top();
p2.pop();
b[id]+=min(rem,x);
if(rem<x){
rem=0;
}else{
rem-=x;
}
}else{
b[0]+=rem;
rem=0;
}
}
for(auto&& i:b)cout<<i<<" ";
cout<<ENDL;
}
//(・_・)(・_・)(・_・)(・_・)
//CHECK overflow,what to output?
//any other simpler approach? |
#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define F first
#define S second
#define mod 1000000007
using namespace std;
void solve(){
ll ans=1e9; int n; cin>>n;
pii arr[n];
for(int i=0;i<n;i++){
int a,b; cin>>a>>b; arr[i]={a,b};
}
for(int i=0;i<n;i++){
ans=min(ans,(ll)arr[i].F+arr[i].S);
for(int j=i+1;j<n;j++){
ans=min(ans,(ll)max(arr[i].F,arr[j].S));
ans=min(ans,(ll)max(arr[i].S,arr[j].F));
}
}
cout << ans <<endl;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t=1; ///cin>>t;
for(int it=1;it<=t;it++){
solve();
}
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define test(t) int t; cin >> t; while(t--)
#define f(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define all(a) (a).begin(), (a).end()
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define deb(x) cout << #x << ": " << x << endl;
#define deb2(x,y) cout << #x << ": " << x << " ~ " << #y << ": " << y << endl;
#define deba(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n << " "; cout << "]" << endl;
#define debap(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n.first << "-" << n.second << " "; cout << "]" << endl;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mx9 1000000007
#define mx7 10000007
#define mx6 1000006
#define mx5 200005
#define inf 1<<30
#define eps 1e-9
#define mod 1000000007
#define PI 3.141592653589793238462643383279502884L
void solve()
{
string s;
cin >> s;
ll n;
cin >> n;
map<ll, ll> mp;
f(i, 0, n)
{
string p, t;
p = t = s;
sort(all(t));
sort(all(p));
reverse(all(p));
ll temp = stoi(p) - stoi(t);
s = to_string(temp);
}
cout << s << endl;
}
int main()
{
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// test(t)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define int long long int
#define endl "\n"
#define MOD 1000000007
#define mod 1000000007
#define M 1000000007
#define pb push_back
#define take(a,b,c) for(int b=0;b<c;b++) cin>>a[b]
#define boost ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mx 1000005
#define fiint(a,b) memset(a,b,sizeof(a))
#define bitcount __builtin_popcount
#define fori(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i>=b;i--)
#define debug(x) cout<<x<<endl;
#define cases(t) int t; cin>>t; while(t--)
#define inf1 INT_MAX
#define all(a) a.begin(),a.end()
#define vec vector<int>
#define pii pair<int,int>
#define plii pair<int,int>
#define pint pair<int,int>
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mp make_pair
#define sz(x) (int)x.size()
#define PI 3.14159265359
//const long long INF=1e18;
bool chmin(int& a, int b){ if(a > b){ a = b; return 1; } return 0; }
int factorial[10000001]={0};
int gcd(int a, int b){
if (b == 0)
return a;
return gcd(b, a % b);
}
int powerFunction(int x,int y){
int res = 1;
int p = mod;
x = x;
while (y > 0){
if (y & 1)
res = (res*x)%p ;
y = y>>1;
x = (x*x)%p ;
}
return res;
}
void factorialFunction(int maxLimit){
factorial[0]=1;
for(int i=1 ; i <= maxLimit ; i++)
factorial[i]=(factorial[i-1]*i)%MOD;
return;
}
int nCr(int n, int r)
{
if(r < 0 || r > n){
return 0;
}
int temp = factorial[n];
temp *= (powerFunction(factorial[r], MOD-2)%MOD);
temp %= MOD;
temp *= (powerFunction(factorial[n-r], MOD-2)%MOD);
temp %= MOD;
return temp;
}
vec p(300005);
vec si(300005);
//vec di
vector<map<int,int>> cnt(300005);
int find_p(int a)
{
while(p[a] != a)
{
//int le = si[p[a]];
p[a] = p[p[a]];
//si[p[a]] += le;
a = p[a];
}
return a;
}
void merge(int a , int b)
{
a = find_p(a);
b = find_p(b);
if(a == b)
return;
if(si[a] >= si[b])
{
si[a] += si[b];
p[b] = a;
for(auto it : cnt[b])
cnt[a][it.ff] += it.ss;
}
else
{
si[b] += si[a];
p[a] = b;
for(auto it : cnt[a])
cnt[b][it.ff] += it.ss;
}
}
signed main()
{
int n,q;cin>>n>>q;
for(int i = 0 ; i < n ;i++)
{
int a ; cin>>a; a--; cnt[i][a] = 1;
p[i] = i;
si[i] = 1;
}
while(q--)
{
int t , x , y ; cin >>t >> x >> y;
x-- ; y--;
if(t == 1 )
{
merge(x , y);
}
else
{
cout<<cnt[find_p(x)][y]<<endl;
}
}
}
| #include <iostream>
#include <vector>
#include <map>
using namespace std;
struct uf_tree {
std::vector<int> parent;
std::vector<std::map<int, int>> dict;
int __size;
uf_tree(int size_) : parent(size_, -1), dict(size_), __size(size_) {}
void unite(int x, int y) {
if ((x = find(x)) != (y = find(y))) {
if (parent[y] < parent[x]) std::swap(x, y);
parent[x] += parent[y];
parent[y] = x;
for (auto [key, val] : dict[y]) {
dict[x][key] += val;
}
__size--;
}
}
bool is_same(int x, int y) { return find(x) == find(y); }
int find(int x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); }
int size(int x) { return -parent[find(x)]; }
int size() { return __size; }
int query(int x, int y) { return dict[find(x)][y]; }
};
int main() {
int n, q;
cin >> n >> q;
vector<int> cs(n);
uf_tree uf(n);
for (int i = 0; i < n; i++) {
cin >> cs[i];
uf.dict[i][cs[i]]++;
}
for (int i = 0; i < q; i++) {
int a, b, c;
cin >> a >> b >> c;
if (a == 1) {
uf.unite(b -1, c - 1);
} else {
cout << uf.query(b - 1, c) << endl;
}
}
return 0;
} |
/*Rabbi Zidni Ilma*/
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define scl(n) scanf("%lld",&n)
#define scll(n,m) scanf("%lld %lld",&n,&m)
#define pb push_back
#define mp make_pair
#define frs first
#define scn second
#define mod 1000000007
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
#define N 10000000
ll pow(ll x,ll y)
{
ll i,pro=1;
for(i=0;i<y;i++)
pro*=x;
return pro;
}
int main()
{
//fastio;
ll tc,t,n,m,i,j,k,mx,mn,c,x,y;
scl(n);
c=0;
ll f=0;
set<ll>st;
for(i=2;i<=100000;i++)
{
for(j=2;j<=34;j++)
{
x=pow(i,j);
if(x<=n)
st.insert(x);
else
{
f=1;
break;
}
}
}
ll sz=st.size();
printf("%lld\n",n-sz);
} | #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
int N, M;
int A[1011], B[1011];
int dp[1011][1011];
void MAIN() {
scanf("%d%d", &N, &M);
REP (i, N) scanf("%d", A+i);
REP (i, M) scanf("%d", B+i);
memset(dp, 0x3f, sizeof dp);
dp[0][0] = M + N;
REP (i, N+1) REP (j, M+1) {
if (i < N && j < M) {
if (A[i] != B[j]) {
amin(dp[i+1][j+1], dp[i][j] - 1);
} else {
amin(dp[i+1][j+1], dp[i][j] - 2);
}
}
amin(dp[i][j+1], dp[i][j]);
amin(dp[i+1][j], dp[i][j]);
}
int ans = dp[N][M];
printf("%d\n", ans);
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
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()
#define rall(v) v.rbegin(), v.rend()
using ll = long long;
using Graph = vector<vector<int>>;
const long long INF = 1LL << 60;
const int SINF = 1LL << 29;
const ll mod = 1000000000+7;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main() {
const int MOD = 998244353;
int n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
ll res = 0;
rep(i, n) res += (a[i] * a[i]) % MOD;
sort(all(a));
ll sum = 0;
for(int i=n-2; i>=0; i--) {
sum = (2 * sum + a[i+1]) % MOD;
res += (a[i] * sum) % MOD;
}
cout << res % MOD << endl;
} | #include <iostream>
#include <algorithm>
#include <cmath>
#include <limits>
#include <iomanip>
#include <vector>
#include <cstring>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <stack>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<n;i++)
using namespace std;
typedef vector<vector<int>> Graph;
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)); }
//main area
int main(){
ll N;
cin >> N;
vector<ll> A(N);
rep(i,N) cin >> A[i];
sort(A.begin(),A.end());
ld ans = 0;
rep(i,N) ans += A[i];
ll median = A[N/2];
rep(i,N) ans += abs(A[i] - median);
cout << fixed << setprecision(10) << ans/(2.0*N) << endl;
}
/*
*/ |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
double EPS=1e-9;
int INF=1000000005;
long long INFF=1000000000000000005ll;
double PI=acos(-1);
int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 };
const ll MOD = 1000000007;
ll sum() { return 0; }
template<typename T, typename... Args>
T sum(T a, Args... args) { return a + sum(args...); }
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << endl
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl;
template <typename T = ll> bool extgcd(T a, T b, T &x, T &y, T d) {
if(!b and d % a) return false;
if(!b) {
x = d / a, y = 0;
return true;
}
if(extgcd(b, a % b, y, x, d)) {
y -= a / b * x;
return true;
}
return false;
}
void solve() {
ll n, s, k;
cin >> n >> s >> k;
ll x, y;
if(s % __gcd(n, k)) {
OUT(-1);
return;
}
if(extgcd(k, n, x, y, -s)) {
x %= n / __gcd(n, k);
if(x < 0) x += n / __gcd(k, n);
OUT(x);
} else {
OUT(-1);
}
}
int main()
{
FAST_INP;
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r", stdin);
// freopen("output.txt","w", stdout);
// #endif
int tc; cin >> tc;
TC(tc) solve();
// solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long int modinv(long long int a, long long int m) {
long long int b = m, u = 1, v = 0;
while (b) {
long long int t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
int main() {
int T;
cin >> T;
for (int i = 0; i < T; i++) {
long long int N, S, K;
cin >> N >> S >> K;
long long int G = __gcd(K, N);
if ((N - S) % G != 0) cout << -1 << endl;
else {
long long int A, B, n;
A = K / G;
B = (N - S) / G;
n = N / G;
long long int C = modinv(A, n);
long long int ans = C * B % n;
if (ans < 0) ans += n;
cout << ans << endl;
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1003;
ll t, n;
bool c[N];
void solv()
{
cin >> t >> n;
for (int A = 1; A <= 200; ++A)
{
ll x = (100 + t) * A / 100;
c[x] = true;
}
vector<int> v;
for (int i = 1; i < 100 + t; ++i)
{
if (!c[i])
v.push_back(i);
}
cout << ((n - 1) / sz(v)) * (100LL + t) + v[(n - 1) % sz(v)] << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
//ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mkp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define M1 1000000007
#define M2 998244353
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define bfl(i,a,b) for(ll i=b-1;i>=a;i--)
#define f(i,n) for(int i=0;i<n;i++)
#define bf(i,n) for(int i=n-1;i>=0;i--)
#define f1(i,n) for(int i=1;i<=n;i++)
#define bf1(i,n) for(int i=n;i>=1;i--)
#define all(v) v.begin(),v.end()
#define pll pair<long,long>
#define pii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define p0(a) cout << a << " "
#define p1(a) cout << a << bn
#define p2(a, b) cout << a << " " << b << bn
#define p3(a, b, c) cout << a << " " << b << " " << c << bn
#define p4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << bn
#define debug(x) cerr << #x << " = " << x << endl;
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define bn "\n"
#define pbn cout<<"\n"
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1<<" | ";
__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif
ll modI(ll a,ll m);
ll gcd(ll a,ll b);
ll powM(ll x,ll y,ll m);
void py(int ok)
{
if(ok==1){
cout<<"YES"<<bn;
}else{
cout<<"NO"<<bn;
}
}
void swap(ll& a,ll& b)
{
ll tp=a;
a=b;
b=tp;
}
ll gcd(ll x,ll y)
{
if(x==0) return y;
return gcd(y%x,x);
}
ll lcm(ll x, ll y)
{
if(x==0 || y==0){
cout<<"Not valid lcm"<<bn;
}
ll LCM= x/gcd(x,y);
LCM=LCM*y;
return LCM;
}
ll powM(ll x,ll y,ll m)
{
ll ans=1,r=1;
x%=m;
while(r>0&&r<=y)
{
if(r&y)
{
ans*=x;
ans%=m;
}
r<<=1;
x*=x;
x%=m;
}
return ans;
}
ll modI(ll a, ll m)
{
ll m0=m,y=0,x=1;
if(m==1) return 0;
while(a>1)
{
ll q=a/m;
ll t=m;
m=a%m;
a=t;
t=y;
y=x-q*y;
x=t;
}
if(x<0) x+=m0;
return x;
}
//-----------------------------------------------------------------------------
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int tt=1;
// cin>>tt;
f1(testcase_no,tt)
{
// cout<<"test case no. "<<testcase_no<<bn;
ll t,n;
cin>>t>>n;
ll below=100/t;
ll rem=100%t;
// if(rem==0)
ll ans=(below+1)*n;
if((rem*n)%t==0)
ans+=((rem*n)/t-1);
else
ans+=((rem*n)/t);
p1(ans);
// cout << fixed << setprecision(25);
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <map> // map<string, int> mp;
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
int main() {
int N, M; cin >> N >> M;
Graph paths(N);
ll ans = 0;
rep (i, M) {
ll A, B; cin >> A >> B;
paths[A - 1].push_back(B - 1);
}
rep (i, N) {
int cnt = 0;
vector<bool> visited(N, false);
visited[i] = true;
queue<int> que;
que.push(i);
while(!que.empty()) {
int q = que.front(); que.pop();
for (const auto& next : paths[q]) {
if (visited[next]) continue;
visited[next] = true;
que.push(next);
++cnt;
}
}
ans += cnt;
}
cout << ans + N << endl;
}
| #include <bits/stdc++.h>
using namespace std;
// clang-format off
/* accelration */
// 高速バイナリ生成
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// cin cout の結びつけ解除, stdioと同期しない(入出力非同期化)
// cとstdの入出力を混在させるとバグるので注意
struct Fast {Fast() {std::cin.tie(0); ios::sync_with_stdio(false);}} fast;
/* alias */
using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
/* define short */
#define pb push_back
#define mp make_pair
#define all(obj) (obj).begin(), (obj).end()
#define YESNO(bool) if(bool){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(bool) if(bool){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
/* REP macro */
#define reps(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) reps(i, 1, n + 1)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define rrepd(i,n) for(ll i=n;i>=1;i--)
/* debug */
// 標準エラー出力を含む提出はrejectされる場合もあるので注意
#define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" << endl;
/* func */
inline int in_int() {int x; cin >> x; return x;}
inline ll in_ll() {ll x; cin >> x; return x;}
inline string in_str() {string x; cin >> x; return x;}
// search_length: 走査するベクトル長の上限(先頭から何要素目までを検索対象とするか、1始まりで)
template <typename T> inline bool vector_finder(std::vector<T> vec, T element, unsigned int search_length) {
auto itr = std::find(vec.begin(), vec.end(), element);
size_t index = std::distance( vec.begin(), itr );
if (index == vec.size() || index >= search_length) {return false;} else {return true;}
}
template <typename T> inline void print(const vector<T>& v, string s = " ")
{rep(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");}
template <typename T, typename S> inline void print(const pair<T, S>& p)
{cout << p.first << " " << p.second << endl;}
template <typename T> inline void print(const T& x) {cout << x << "\n";}
template <typename T, typename S> inline void print(const vector<pair<T, S>>& v)
{for (auto&& p : v) print(p);}
// 第一引数と第二引数を比較し、第一引数(a)をより大きい/小さい値に上書き
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}
using Graph = vector<vector<int>>;
vector<bool> seen;
void dfs(const Graph &G, int v) {
seen[v] = true; // v を訪問済にする
// v から行ける各頂点 next_v について
for (auto next_v : G[v]) {
if (seen[next_v]) continue; // next_v が探索済だったらスルー
dfs(G, next_v); // 再帰的に探索
}
}
int main(){
int n, m;
cin >> n >> m;
Graph G(n);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
a--; b--;
G[a].push_back(b);
}
int count = 0;
for(int i = 0; i < n; i++){
seen.assign(n, false); // 全頂点を「未訪問」に初期化
dfs(G, i);
for(int j = 0; j < n; j++){
// t に辿り着けるかどうか
if (seen[j]) count++;
}
}
cout << count << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define int int long long
#define fr(i, n) for (int i = 0; i < n; i++)
#define fr1(i, n) for (int i = 1; i <= n; i++)
#define S second
#define F first
#define pb(n) push_back(n)
#define endl "\n"
int mod = 1e9 + 7;
vector<int> v[1000001], tr[100001];
int level[1000001] = {0};
int in[1000001] = {0};
int out[1000001] = {0};
int low[1000001] = {0};
int vis[1000001] = {0};
int visit[1001][1001] = {0};
int dis[1001][1001] = {0};
int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
int timer, n, m, t, k, q, eq = 0;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
t = 1;
//
while (t--)
{
cin >> n;
double te = 0.01;
pair<double, double> a[n];
fr(i, n)
{
double x, l, r;
cin >> x >> l >> r;
if (x == 2)
{
r -= te;
}
if (x == 3)
{
l += te;
}
if (x == 4)
{
l += te, r -= te;
}
a[i] = {l, r};
}
int ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = i - 1; j >= 0; j--)
{
if (a[i].F > a[j].S || a[i].S < a[j].F)
{
continue;
}
else
{
ans++;
}
}
}
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define lli long long int
#define pi pair<lli,lli>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for(lli i=a;i<b;i++)
#define all(x) x.begin(), x.end()
const lli inf=10000000000000000;
lli mod = 1e9+7;
//const long double pi = 3.14159265358979323846264338;
lli powmod(lli x, lli y, lli p){lli res=1;x=x%p;if(x==0)return 0;while(y>0){if(y&1)res=(res*x)%p;y = y>>1;x=(x*x)%p;}return res;}
lli _pow(lli a, lli b){if(!b)return 1;lli temp = _pow(a, b / 2);temp = (temp * temp);if(b % 2)return (a * temp);return temp;}
bool isPrime(lli n){if(n<=1)return false;if(n<=3)return true;if(n%2==0||n%3==0)return false;for(lli i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
lli lcm(lli a, lli b){ return a * b / __gcd(a, b);}
lli cl(lli a,lli x){if(a%x==0)return a/x;else return a/x+1;}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--) {
lli n,b,c;
cin >> n;
vector <lli> v(n);
vector <pi> a(n);
for(lli i=0;i<n;i++){
cin >> v[i] >> b >> c;
a[i] = (mp(b, c));
}
lli cn=0;
for(lli i=0;i<n;i++){
for(lli j=i+1;j<n;j++){
lli q = a[i].fi,w = a[i].se,e = a[j].fi,r = a[j].se;
if(max(q,e)<min(w,r)){
cn++;
}
else if(max(q,e)==min(w,r)){
if(q>e&&(v[i]==1||v[i]==2)&&(v[j]==1||v[j]==3))cn++;
if(q<e&&(v[i]==1||v[i]==3)&&(v[j]==1||v[j]==2))cn++;
}
}
}
cout<<cn<<endl;
}
}
|
#include <bits/stdc++.h>
// #include <atcoder/all> // NOTE: AtCoderライブラリ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl;
#define dbg3(x, y, z) cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << endl;
#define nl() cout << "========================================" << endl;
using namespace std;
// using namespace atcoder; // NOTE: AtCoderライブラリ
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vl;
typedef vector<vector<long long>> vvl;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef pair<int, int> pii;
const long long LINF = 1001002003004005006ll;
const int INF = 1001001001;
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; }
#define yn { puts("Yes"); } else{ puts("No"); } // 使い方: if (条件) yn;
int main() {
int N; cin >> N;
vector<int> A(N); rep(i, N) cin >> A[i];
int res, maxcnt = 0;
for (int i = 2; i <= 1000; i++) {
int cnt = 0;
for (int j = 0; j < N; j++) {
if (A[j] % i == 0) {
cnt++;
}
}
if (cnt > maxcnt) {
maxcnt = cnt;
res = i;
}
}
cout << res << endl;
}
| #include <iostream>
#include <string>
#include <math.h>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include <cmath>
using namespace std;
#define mod 1000000007
#define mod2 998244353
#define ten5 100005
#define ten52 200005
#define ten53 300005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
#define mkpr(x1,x2) make_pair(x1,x2)
typedef long long int ll;
//stack<char> stk;
//set<ll> sll;
//set<pair<ll,ll>> spll;
map<pair<ll,ll>,ll> mp;
//deque<ll> deq;
ll num[168]={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997};
ll save[ten5];
//priority_queue<pair<ll,ll>> pq; //decreasing
int main(void)
{
ll m,n,i,j=1,x=0,y=0,k,k1,ans=0;
cin>>n;
for(i=0;i<n;i++) cin>>save[i];
for(i=0;i<168;i++)
{
k=0;
for(j=0;j<n;j++)
if(save[j]%num[i]==0) k++;
if(k>ans)
{
ans=k;
y=num[i];
}
if(ans==n) break;
}
cout<<y;
return 0;
} |
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define ret(x) return cout<<x,0;
#define rety return cout<<"YES",0;
#define retn return cout<<"NO",0;
typedef long long ll;
typedef double db;
typedef long double ld;
#define stt string
#define ve vector<ll>
#define se set<ll>
#define ar array
ll mod=1e9+7;
ll mm=998244353;
using namespace std;
ll fac[10000000];
ll gcd(ll x, ll y)
{
if(y==0)
return x;
return gcd(y,x%y);
}
ll fexp(ll a,ll b,ll m){ll ans = 1;while(b){if(b&1)ans=ans*a%m; b/=2;a=a*a%m;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2,p);}
ll ncr(ll n, ll r,ll p)
{
if (r==0) return 1;
return (fac[n]*inverse(fac[n-r],p)%p*inverse(fac[r],p)%p)%p;
}
// ____Z-Algorithm_____
vector<ll> za(string s)
{
ll n = s.size();
vector<ll> z(n);
ll x = 0, y = 0,p=0;
for(ll i= 1; i < n; i++)
{
z[i] = max(p,min(z[i-x],y-i+1));
while(i+z[i] < n && s[z[i]] == s[i+z[i]])
{
x = i; y = i+z[i]; z[i]++;
}
}
return z;
}
int subset(ll a[],ll k)
{
for (int i = 1; i < pow(2, k); i++)
{
for (int j = 0; j < k; j++)
{
if (i & 1 << j)
{
cout<<a[j]<<" ";
}
}
cout<<endl;
}
return 1;
}
vector<ll> pr(string s)
{
ll n = s.length();
vector<ll> pi(n);
for (int i = 1; i < n; i++)
{
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
//---------------------------------------------------------------------/
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll a,b,c;
cin>>a>>b>>c;
if(2*a==b+c || 2*c==b+a || 2*b==a+c)
cout<<"Yes";
else
cout<<"No";
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve(long long R, long long X, long long Y){
ll dist = (X * X + Y * Y) / (R * R);;
if(R * R > X * X + Y * Y){
cout<<2<<endl;
return;
}
if((X * X + Y * Y) % (R * R)){
dist++;
}
ll ok = 0;
ll ng = 1e9;
while(ng - ok > 1){
ll mid = (ok + ng) / 2;
if(mid * mid <= dist){
ok = mid;
}else{
ng = mid;
}
}
if(ok * ok != dist){
ok++;
}
//cout<<dist<<endl;
cout<<ok<<endl;
}
int main(){
long long R;
scanf("%lld",&R);
long long X;
scanf("%lld",&X);
long long Y;
scanf("%lld",&Y);
solve(R, X, Y);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define bitsz(x) int(__builtin_popcount(x))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb(x) push_back(x)
#define INF 1e9
#define LINF 1e18
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; }
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); };
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
const int di[4] = {1,0,-1,0};
const int dj[4] = {0,1,0,-1};
const int mod = 1000000007;
//const int mod = 998244353;
//cout << fixed << setprecision(15);
int main() {
int n; cin >> n;
ll C; cin >> C;
map<int,ll> events;
rep(i,n) {
int a, b, c;
cin >> a >> b >> c;
events[a] += c;
events[b+1] -= c;
}
ll ans = 0;
ll s = 0;
int pre = 0;
for (auto event : events) {
ans += min(C, s)*(event.first-pre);
s += event.second;
pre = event.first;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll MOD = 1e9 + 7;
const int INF = 1e9;
const ll LLINF = 1e18;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int main() {
// input
vector<string> s(3);
rep(i, 3) { cin >> s[i]; }
// solve
map<char, ll> mp;
set<char> heads;
rep(i, 3) {
reverse(s[i].begin(), s[i].end());
ll co = i == 2 ? -1 : 1;
for (char c : s[i]) {
mp[c] += co;
co *= 10;
}
reverse(s[i].begin(), s[i].end());
heads.insert(s[i][0]);
}
if (10 < mp.size()) {
cout << "UNSOLVABLE" << endl;
return 0;
}
vector<int> p(10);
iota(p.begin(), p.end(), 0);
do {
int i = 0;
ll tot = 0;
for (auto x : mp) {
char c = x.first;
ll co = x.second;
if (p[i] == 0 && heads.count(c)) {
tot = 1e18;
}
tot += co * p[i];
++i;
}
if (tot == 0) {
i = 0;
for (auto& x : mp) {
x.second = p[i];
++i;
}
rep(i, 3) {
ll x = 0;
for (char c : s[i]) {
x = x * 10 + mp[c];
}
cout << x << endl;
}
return 0;
}
} while (next_permutation(p.begin(), p.end()));
cout << "UNSOLVABLE" << endl;
}
|
#include<bits/stdc++.h>
#define ll long long
int main()
{
using namespace std;
int t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
if(n==1)
{
cout<<"Odd\n";
continue;
}
if(n==2)
{
cout<<"Same\n";
continue;
}
if(n%2==1)cout<<"Odd\n";
else if(n%4==0)cout<<"Even\n";
else if(n%4==2)cout<<"Same\n";
}
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fp(i,a,b) for(ll i=a ; i<b ; i++)
#define fn(i,a,b) for(int i=a ; i>=b ; i--)
#define ones(x) __builtin_popcount(x)
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define dbg(x) cout << (#x) << " = " << x << " "
#define fini cout << "\n";
#define line cout << "-----------------------------------\n";
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef tree<
ll,
null_type,
less_equal<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
int read (){
int x = 0, f = 1; char s = getchar();while (s < '0' || s > '9') {if (s == '-') f = -1; s = getchar();}
while (s >= '0' && s <= '9') x = x * 10 + s - '0', s = getchar();
return x * f;
}
const ll M=2e5+7;
const ll N=2e7+7;
const ll inf=1e18;
const ll mod=1e9+7;
const ll mod2=998244353;
void go(int ide){
ll n; cin >> n;
if (n%2) cout << "Odd\n";
else if (n%4) cout << "Same\n";
else cout << "Even\n";
}
int main(){
fastio;
int tst=1;
cin >> tst;
// cout << fixed << setprecision(12);
fp(i,0,tst) go(i+1);
return 0;
} |
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
int h,w,m;
int f[200005];
ll ans = 0;
int of[200005];
int fw[200005];
ii p[200005];
void up(int x, int v){
while (x < 200005){
fw[x] += v;
x += x&-x;
}
}
int qu(int x){
int res = 0;
while (x){
res += fw[x];
x -= x&-x;
}
return res;
}
int main(){
cin >> h>>w>>m;
for (int i = 1; i <= h; i++){
f[i] = w+1;
}
for (int i = 0; i < m; i++) {
cin >> p[i].fi >> p[i].se;
f[p[i].fi] = min(f[p[i].fi],p[i].se);
}
sort(p,p+m);
int id = 0;
int cur = w;
for (int i = 1; i <= h; i++){
while (id < m && p[id].fi == i){
if (i == 1){
while (cur >= p[id].se){
if (of[cur] == 0){
of[cur] = 1;
up(cur, 1);
}
cur--;
}
}
if (of[p[id].se] == 0){
//printf("toggling %d column\n",p[id].se);
of[p[id].se] = 1;
up(p[id].se,1);
}
id++;
}
int cur;
if (of[1]) {
cur = qu(w);
}
else cur = qu(w) - qu(f[i]-1);
//printf("first = %d, %d on\n",f[i],cur);
ans += cur;
}
ll ANS = (ll)h*w;
ANS -= ans;
printf("%lld",ANS);
}
| #include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define Pii pair<int,int>
#define Pll pair<long long,long long>
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//vector<vector<ll>> dp(n,vector<ll>(n))
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));
using namespace std;
template<typename T>
struct SegmentTree{
private:
int n;
vector<T> node;
T identity;
public:
T merge(T x,T y){
return x+y;
}
SegmentTree(vector<T> v,T id){
int sz=(int)v.size();
n=1; while(n<sz)n*=2;
identity=id;
node.resize(2*n-1,id);
for(int i=0;i<sz;i++) node[i+n-1]=v[i];
for(int i=n-2;i>=0;i--){
node[i]=merge(node[2*i+1],node[2*i+2]);
}
}
void update(int x,T val){
x+=(n-1);
node[x]=val;
while(x>0){
x=(x-1)/2;
node[x]=merge(node[2*x+1],node[2*x+2]);
}
}
T query(int a,int b,int idx=0,int l=0,int r=-1){ /* get [a,b) */
if(r<0) r=n;
if(r<=a||b<=l) return identity;
if(a<=l&&r<=b) return node[idx];
T vl=query(a,b,2*idx+1,l,(l+r)/2);
T vr=query(a,b,2*idx+2,(l+r)/2,r);
return merge(vl,vr);
};
T operator[](int k){return node[k+n-1];}
};
signed main(){
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
ll h,w,m; cin >> h >> w >> m;
vector<ll> x(m),y(m);
vector<ll> mix(200200,h+1),miy(200200,w+1);
vector<ll> cnt(200200,0);
vector<vector<ll>> G(200200);
rep(i,m){
cin >> x[i] >> y[i];
chmin(miy[x[i]],y[i]);
chmin(mix[y[i]],x[i]);
cnt[y[i]]++;
G[y[i]].pb(x[i]);
}
vector<ll> v(h,1);
SegmentTree<ll> seg(v,0);
ll ans = 0;
for(int i=1;i<miy[1];i++){
ans += (mix[i]-1);
}
//cout << ans << endl;
ll hh = min(h,mix[1]);
for(int i=1;i<=w;i++){
if(mix[i]==h+1 && i<miy[1]) continue;
for(auto u:G[i]){
seg.update(u-1,0);
}
if(i<miy[1]){
ans += seg.query(mix[i],hh);
//cout << seg.query(mix[i],hh) << endl;
}
else{
ans += seg.query(0,hh);
//cout << seg.query(0,hh) << endl;
}
}
cout << ans << endl;
}
// g++ main.cpp -o a.out && ./a.out |
#include <bits/stdc++.h>
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ull unsigned long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define endl "\n"
#define pi 3.14159265
const ll mod = 1000 * 1000 * 1000 + 7;
const ll mod1 = 998244353;
const ll INF = 1ll*1000*1000*1000*1000*1000*1000 + 7;
using namespace std;
ll power(ll x, ll y)
{
ll res = 1;
while (y > 0)
{
if (y & 1)
res = (long long)(res*x);
y = y>>1;
x = (long long)(x*x);
//cout<<x<<'\n';
}
return res;
}
// Stolen Templates
template <typename T>
T MIN(T first) { return first; }
template <typename T, typename... Args>
T MIN(T first, Args... args) { return min(first, MIN(args...)); }
template <typename T>
T MAX(T first) { return first; }
template <typename T, typename... Args>
T MAX(T first, Args... args) { return max(first, MAX(args...)); }
// Stolen Templates end here
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
fastio();
ll n,s,d;
cin>>n>>s>>d;
For(i,0,n) {
ll x,y;
cin>>x>>y;
if (x<s and y>d) {
cout<<"Yes";
return 0;
}
}
cout<<"No";
return 0;
}
// check all product based operations for integer overflow
// careful of renamed variables especially in loops
| #include<iostream>
#define int long long
using namespace std;
signed main()
{
int n,s,d;
cin>>n>>s>>d;
bool flag=0;
for(int i=0;i<n;i++)
{
int j,k;
cin>>j>>k;
if(j<s&&k>d)
{
flag=1;
}
}
if(flag)
cout<<"Yes";
else
cout<<"No";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define FASTio ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ll long long int
#define all(v) (v).begin(), (v).end()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define printarr(v) {for(auto x : v) cout<<x<<" ";cout<<"\n";}
#define mp make_pair
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define lcm(a, b) ((a) * (b)) / __gcd(a, b)
#define umpit unordered_map<ll,ll>::iterator
#define mpit map<ll,ll>::iterator
#define setit set<ll>::iterator
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
const ll INF = LLONG_MAX / 2;
#define PI 3.1415926535897932384626433832795
#define mod 1000000007
int main()
{
FASTio;
ll t=1,i,j;
//cin>>t;
while(t--)
{
ll n;
cin>>n;
ll k = 1.08*n;
if(k<206)
cout<<"Yay!\n";
else if(k == 206)
cout<<"so-so\n";
else
cout<<":(\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
cout << ((floor(n*1.08)<206)? "Yay!": ((floor(n*1.08)==206)? "so-so": ":(")) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll,ll> pll;
typedef vector <int> vint;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
typedef vector <string> vs;
typedef priority_queue <int> pqi;
#define FOR(i, a, b) for(int i = a; i < (b); ++i)
#define F0R(i, a) for(int i = 0; i < (a); ++i)
#define FORd(i, a, b) for(int i = (b) - 1; i >= a; --i)
#define F0Rd(i, a) for(int i = (a) - 1; i >= 0; --i)
#define trav(x, a) for(auto& x : a)
#define pb push_back
#define mkp make_pair
#define vbb vector <bool>
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
const int mod = 1000000007;
const long long inf = 1e18;
const char nl = '\n';
void solve_it(){
vint a(3);
F0R(i, 3) cin>>a[i];
sort(all(a));
cout<<a[3 - 1] + a[3 - 2];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
solve_it();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=(int)(x);i<(int)(y);i++)
#define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl;
#define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);}
const long mod=1e9+7;
const int siz=3e5;
const int inf=1e9;
int main(){
int N; cin>>N;
pair<long,int> vx[N], vy[N];
long x[N], y[N];
rep(i,0,N){
cin>>x[i]>>y[i];
vx[i] = {x[i], i};
vy[i] = {y[i], i};
}
sort(vx, vx+N); sort(vy, vy+N);
set<int> id;
id.insert(vx[0].second);
id.insert(vx[1].second);
id.insert(vx[N-2].second);
id.insert(vx[N-1].second);
id.insert(vy[0].second);
id.insert(vy[1].second);
id.insert(vy[N-2].second);
id.insert(vy[N-1].second);
vector<long> dist;
for(int v:id)for(int u:id){
dist.push_back(max(abs(x[u] - x[v]), abs(y[u] - y[v])));
}
sort(dist.begin(), dist.end(), greater<long>());
cout<<dist[2]<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=998244353;
constexpr ll INF=2e18;
int main(){
int n; cin >> n; n*=2;
VI a(n); REP(i,n) cin >> a[i];
vector<P> p(0);
REP(i,n){
p.push_back({a[i],i});
}
sort(ALL(p));
VI f(n,0);
int x=0;
REP(i,n){
if(x<n/2){
f[p[i].second]=1;
x++;
}
}
vector<char> ans(n);
stack<int> st;
REP(i,n){
if(st.size()==0){
st.push(i);
}
else{
if(f[st.top()]==f[i]){
st.push(i);
}
else{
ans[st.top()]='(';
ans[i]=')';
st.pop();
}
}
}
for(auto c:ans){
cout << c;
}
cout << endl;
} | #include <iostream>
long long dp[201][12];
long long nCr(long long n, long long r) {
if (r == n)
return dp[n][r] = 1;
if (r == 1)
return dp[n][r] = n;
if (r == 0)
return dp[n][r] = 1;
if (dp[n][r] == 0)
return dp[n][r] = nCr(n-1, r) + nCr(n-1, r-1);
else
return dp[n][r];
}
int main() {
int l;
for (int i = 0; i < 201; i++) {
for (int j = 0; j < 13; j++)
dp[i][j] = 0;
}
std::cin >> l;
std::cout << nCr(l-1, 11);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if (a != b && b != c && c != a) cout << 0 << '\n';
else cout << (a ^ b ^ c) << '\n';
} | #include "bits/stdc++.h"
#include <stdio.h>
#include <string.h>
#include<iostream>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
#define KP \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define ll long long int
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define inp(a, n) rep(i, 0, n) cin >> a[i];
#define out(a, n) rep(i, 0, n) cout << a[i] << " ";
#define MIN -1e9
#define MAX 1e9
#define endl "\n"
#define vi vector<ll>
#define pb push_back
#define all(v) v.begin(), v.end()
#define output(x) cout << (x ? "Yes" : "No") << endl;
bool sortbysecdesc(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
return a.second > b.second;
}
int binpow(int a, int n)
{
// // normal recurcive
// if (n == 0)
// return 1;
// if (n % 2 == 1)
// return binpow(a, n - 1) * a;
// else
// {
// int b = binpow(a, n / 2);
// return b * b;
// }
// //iterative
// int res = 1;
// while (n)
// if (n & 1)
// {
// res *= a;
// --n;
// }
// else
// {
// a *= a;
// n >>= 1;
// }
// return res;
// optimised iterative
int res = 1;
while (n)
{
if (n & 1)
res *= a;
a *= a;
n >>= 1;
}
return res;
}
void solve()
{
ll a,b,c;
cin >> a>>b>>c;
if(a==b){
cout << c<<endl;
return;
}
if(b==c){
cout << a<<endl;
return;
}
if(a==c){
cout << b<<endl;
return;
}
cout << "0" << endl;
}
int main()
{
KP;
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ll tt=1;
//cin>>tt;
// ll tt=1;
// cin >> tt;
// string s;
// cin >> s;
while (tt--)
solve();
// cout << "\n\n"
// << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds.\n";
// cout << tt + tt*tt + 12432345 << " " << s<<endl<<tt*tt*tt<<endl ;
return 0;
} |
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (long long i = (a); i < (long long)(b); i++)
#define IFOR(i, a, b) for (long long i = (a); i <= (long long)(b); i++)
using namespace std;
using VL = vector<long long>;
using ll = int64_t;
ll mod(ll x,ll m){ll y=x%m;return (y>=0LL)?y:y+m;}
template<class T>bool chmin(T& a, T b){if(a>b){a=b;return true;}else return false;}
ll gcd(ll a,ll b,ll& x,ll& y)
{if(a){ll d=gcd(b%a,a,x,y);y-=(b/a)*x;swap(x,y);if(d<0LL){d=-d;x=-x;y=-y;}return d;}else{x=0;y=1;return b;}}
ll B(ll X, ll Y, ll n){
return 2LL*(X+Y)*n+X;
}
ll W(ll P, ll Q, ll n){
return (P+Q)*n+P;
}
int main(){
int T;
cin>>T;
string ans = "";
REP(i,T){
ll t = LLONG_MAX;
ll X,Y,P,Q;
cin>>X>>Y>>P>>Q;
ll x, y;
ll d = gcd(2LL*(X+Y), -(P+Q), x, y);
ll u = (P+Q)/d, v = 2LL*(X+Y)/d;
IFOR(z, P-X, P-X+Q-1LL){
if(mod(z,d)!=0LL)continue;
ll n = mod(z/d*x,u);
chmin(t,B(X,Y,n));
}
IFOR(z, P-X-Y+1LL, P-X){
if(mod(z,d)!=0LL)continue;
ll m = mod(z/d*y,v);
chmin(t, W(P,Q,m));
}
if(t==LLONG_MAX){
ans += "infinity\n";
}else{
ans += to_string(t) + "\n";
}
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
#define rei register int
#define int signed long long
#pragma GCC optimize(2)
using namespace std;
int T,X,Y,P,Q;
int ans,cnt;
void exgcd(int &x,int &y,int a,int b)
{
if(!b)
{
x=1;
y=0;
return;
}
exgcd(x,y,b,a%b);
int t=x;
x=y;
y=t-a/b*y;
} int gcd(int x,int y){
return !y ? x : gcd(y,x % y);
} int mul(int x,int y,int mod){
int res = 0;
while(y){
if(y & 1) res = (res + x) % mod;
x = 2 * x % mod; y >>= 1;
} return res;
}
void solve(int d1,int mod1,int d2,int mod2){
int rec = gcd(mod1,mod2);
if((d2 - d1) % rec != 0) return;
int LCM = mod1 * mod2 / gcd(mod1,mod2);
int rate = (d2 - d1) / gcd(mod1,mod2);
int sol1,sol2; exgcd(sol1,sol2,mod1,mod2);
sol1 = mul(sol1,(rate % LCM + LCM) % LCM,LCM);
int res = ((mul(sol1,mod1,LCM) + d1) % LCM + LCM) % LCM;
ans = min(ans,res);
}
signed main(){
cin >> T; while(T--){
cin >> X >> Y >> P >> Q;
ans = (long long)9e18;
for(rei i = X;i <= X + Y - 1;++i)
for(rei j = P;j <= P + Q - 1;++j)
solve(i,2 * X + 2 * Y,j,P + Q);
if(ans == (long long)9e18) cout << "infinity" << endl; else cout << ans << endl;
} return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
int helper(int l, int r)
{
int j= r - (2*l);
int temp = j*(j+1);
temp = temp/2;
temp = temp + j;
temp = temp + 1;
return temp;
}
void solve(){
int l,r;
cin>>l>>r;
if(2*l <= r)
cout<<helper(l,r)<<endl;
else
cout<<0<<endl;
}
int32_t main(){
int t;
cin>>t;
while(t--){
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> solve(ll t, vector<ll> l, vector<ll> r) {
vector<ll> res(t);
for(ll i = 0ll; i < t; i++) {
//cout << t << " -> " << i << endl;
ll count = max(r[i] - 2ll * l[i] + 1ll, 0ll);
//cout << "count: " << count << endl;
res[i] = count * (count + 1ll) / 2ll;
}
return res;
}
int main() {
ll t;
cin >> t;
vector<ll> l(t), r(t);
for(int i = 0; i < t; i++) {
cin >> l[i] >> r[i];
}
vector<ll> res = solve(t, l, r);
for(ll i=0ll;i<t; i++) {
cout << res[i] << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll p = 1000000009;
const ll nax = 200002;
int par[nax];
int pr(int v) {
if (par[v] == v) return v;
return par[v] = pr(par[v]);
}
int main() {
for (int i = 0; i < nax; ++i) {
par[i] = i;
}
int n;
cin >> n;
vector<int> v(n);
for (auto& x : v) cin >> x;
int ans = 0;
for (int i = 0; i < n; ++i) {
int a = pr(v[i]), b = pr(v[n - 1 - i]);
if (a == b) continue;
ans++;
par[a] = b;
}
cout << ans;
} | #ifdef MYDEBUG
#include "pch.h"
#else
#include <bits/stdc++.h>
#define debug(...)
#endif
using namespace std;
#define int long long
#define endl '\n'
#define sz(x) (int)((x).size())
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
typedef vector<int> vi;
typedef map<int, int> mii;
typedef pair<int, int> pii;
template<typename T1, typename T2>istream& operator>>(istream& in, pair<T1, T2>& a) { return in >> a.first >> a.second; }
template<typename T1, typename T2>ostream& operator<<(ostream& out, pair<T1, T2> a) { return out << a.first << " " << a.second;}
template<typename T>void print(T t) { cout << t; }
template<typename T, typename... Args>void print(T t, Args... args) { cout << t << " "; print(args...); }
template<typename... Args>void printl(Args... args) { print(args...); cout << endl; }
template<typename T>void printa(T& a, bool ln = 0) { for (auto& e : a) print(e, ln?"\n":""); if (!ln) cout << endl; }
#define printr(...) { printl(__VA_ARGS__); return; }
#define inputa(a) for (auto& e : a) cin >> e
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int RAND(int lo = 0, int hi = LLONG_MAX) { return uniform_int_distribution<int>(lo, hi)(rng); }
void google() { static int __gtest__ = 1; cout << "Case #" << __gtest__++ << ": "; }
const double pi = 3.141592653589793;
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << setprecision(15) << fixed;
int t = 1;
//cin >> t;
void code();
while (t--) code();
}
const int mod = 1e9 + 7;
const int mod1 = 998244353;
const int maxn = 2e5 + 1;
vi ar[maxn];
bool vis[maxn];
int dfs(int nd)
{
vis[nd] = 1;
int cc = 0;
for (auto& ch : ar[nd])
{
if (vis[ch]) continue;
cc += dfs(ch);
}
return cc + 1;
}
void code()
{
int n;
cin >> n;
vi a(n);
inputa(a);
for (size_t i = 0; i < n/2; i++)
{
if (a[i] == a[n - 1 - i]) continue;
ar[a[i]].push_back(a[n - 1 - i]);
ar[a[n - 1 - i]].push_back(a[i]);
}
int cc = 0;
for (size_t i = 1; i < maxn; i++)
{
if (vis[i]) continue;
cc += dfs(i) - 1;
}
printl(cc);
} |
#include<algorithm> // min, max, sort,
#include<iostream>
#include<iomanip> // fixed+setprecision,
#include<cstdint> // int64_t,
#include<vector>
#include<map>
using namespace std;
const static int64_t MOD = 1000000007;
int main()
{
int64_t a, b, c, d;
cin>>a>>b;
cin>>c>>d;
cout<<b-c<<endl;
return 0;
} | #include <iostream>
using namespace std;
int main(){
int N, S, D;
cin >> N >> S >> D;
int *spell_time = new int[N];
int *spell_damage = new int[N];
for(int i = 0; i < N; i++){
cin >> spell_time[i] >> spell_damage[i];
}
bool result = false;
for(int i = 0; i < N; i++){
if((spell_time[i] < S) && (spell_damage[i] > D)){
result = true;
break;
}
}
cout << (result ? "Yes" : "No") << endl;
} |
#include <bits/stdc++.h>
using ull = unsigned long long;
using ll = long long;
using namespace std;
#define pf printf
#define sc scanf
#define nline cout << '\n'
#define dbg(x) \
cout << "debug " << #x << ": " << x << '\n';
#define inp(t) cin >> (t)
#define out(t) cout << (t)
#define FOR(n,x) for(ll i = n; i < x; i++)
#define FOR2(N,X) for(ll j = N; j < X; j++)
#define wh(n) while(n--)
ll fib[52] = {};
bool isPrime[50];
void OJ(){
// file I/O
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
ll LCM(ll x, ll y){ return x * (y/__gcd(x,y)); }
// string addition
string sum(string num1,string num2){
int len1 = num1.length();
int len2 = num2.length();
string ans;
bool end1 = false, end2 = false;
int i = 0, carry = 0;
// will break if num1 and num2 ends
for( ;!end1 || !end2; ){
// iterating in reverse order
if(len1) len1--;
if(len2) len2--;
// adding num1 and num2
int temp = (num1[len1]-48) + (num2[len2] - 48);
temp += carry;
carry = temp / 10;
ans.push_back((temp % 10)+48);
i++;
// initializing to 0 if num1 or num2 ends
if(!len1){ end1 = true; num1[len1] = '0'; }
if(!len2){ end2 = true; num2[len2] = '0'; }
}
if(carry) ans.push_back(carry+48);
reverse(ans.begin(), ans.end());
return ans;
}
//str multiplication of a*b
string strMultiplication(string a, string b){
string ans = "0";
string postZeros = "";
for(int i = b.length()-1; i >= 0; i--){
int carry = 0;
string current;
if(b[i] == '0')
current.push_back('0');
else
for(int j = a.length()-1; j >= 0; j--){
int S1_j = a[j] - 48;
int S2_i = b[i] - 48;
int temp = (S1_j * S2_i) + carry;
carry = temp/10;
// if(current != "0")
current.push_back((char)((temp%10) + 48));
}
if(carry) current.push_back(carry+48);
reverse(current.begin(), current.end());
current += postZeros;
ans = sum(ans,current);
postZeros += "0";
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
OJ();
// Start
ll n,m,t; inp(n) >> m >> t;
bool flag = true;
ll charge = n, last = 0;
FOR(0,m){
ll a,b; inp(a) >> b;
charge = charge - (a - last);
if(charge <= 0)
flag = false;
charge = charge + (b - a);
last = b;
if(charge > n)
charge = n;
}
charge = charge - (t - last);
if(flag && charge > 0)
out("Yes");
else
out("No");
nline;
} | // g++ F.cpp -std=c++14 -I . && ./a.out
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, a, b) for (int i = a; i < (int)(b); i++)
#define rrep(i, a, b) for (int i = a; i > (int)(b); i--)
#define all(v) v.begin(), v.end()
using ll = long long;
const ll INF = 1e18;
// 変数定義
int N, M, Q, a, b, c, d, x, y, t, T, total, ans;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> M;
vector<vector<int>> adj(N, vector<int>(N, 0));
rep(i, M)
{
cin >> a >> b;
adj[a - 1][b - 1] = 1;
adj[b - 1][a - 1] = 1;
}
vector<int> dp(1 << N, N);
dp[0] = 0;
rep(S, 1 << N)
{
bool flag = true;
vector<int> lis;
rep(i, N)
{
if ((S >> i) % 2)
lis.push_back(i);
}
for (auto p : lis)
{
for (auto q : lis)
{
if (p == q)
break;
if (adj[p][q] == 0)
{
flag = false;
goto last;
}
}
}
last:
if (flag == true)
dp[S] = 1;
else
{
T = (S - 1) & S;
while (T > 0)
{
dp[S] = min(dp[S], dp[T] + dp[S - T]);
T = (T - 1) & S;
}
}
}
cout << dp[(1 << N) - 1] << '\n';
return 0;
} |
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
using Graph = vector<vector<int>>;
using Grid= vector<string>;
using vin= vector<int>;
using ll=long long;
using ull=unsigned long long;
using vll= vector<ll>;
using vbl=vector<bool>;
using vch=vector<char>;
using P=pair<int ,int>;
//const long double pi=acos(-1);
#define ft first
#define sd second
#define fn front
#define pb push_back
#define eb emplace_back
#define it insert
#define si(v) int((v).size())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rell(i,n) for (ll i=0; i< (ll)(n); i++)
#define sot(x) sort(x.begin(), x.end())
#define rese(x) reverse(x.begin(), x.end())
#define vnn(x,y,s,name) vector<vector<int>> name(x, vector<int>(y,s))
#define mse(x) memset(x, 0, sizeof(x))
#define mii(x,y,z) min(x,min(y,z))
#define maa(x,y,z) max(x,max(y,z))
#define cps CLOCKS_PER_SEC
#define yes cout<<"Yes"<<"\n"
#define no cout<<"No"<<"\n"
#define cset(x) cout<<fixed<<setprecision(x)
//const int INF=1001001001;
//998244353 1000000007
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin>>n;
vector<long long> ax(n);
long long as=0;
rep(i,n){
ll a,b;
cin>>a>>b;
as-=a;
ax[i]=2*a+b;
}
sot(ax);
rese(ax);
int cnt=0;
while(as<=0){
as+=ax[cnt];
cnt++;
}
cout<<cnt<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define pi pair<int, int>
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define FIO ios_base::sync_with_stdio(0), cin.tie(0)
#define fi first
#define se second
signed main() {
FIO;
int n;
cin >> n;
set<pair<int, string>> v;
for (int i = 0; i < n; i++) {
string s;
int t;
cin >> s >> t;
v.insert({-t, s});
}
auto it = v.begin();
it++;
cout << ((*it).se);
}
|
#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
using namespace std;
int main(){
ll int N;
cin >> N;
vector<ll int> a;
ll int keta;
ll int wa;
while(N!=0){
a.push_back(N%10);
wa+=N%10;
N/=10;
}
keta = a.size();
ll int ans=100;
ll int kari=0;
ll int kariwa;
for(ll int j=0;j<(1<<keta);j++){
kariwa=wa;
kari=0;
for(ll int i=0;i<keta;i++){
if(j>>i&1){
kariwa-=a[i];
kari+=1;
}
else{}
}
if(kariwa%3==0 && kariwa!=0){
if(kari<ans){
ans=kari;
}
else{}
}
else{}
}
if(ans==100){
cout << "-1";
}
else{
cout << ans;
}
return 0;
}
| #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for(int i = (int)(m); i < (int)(n); ++i)
void recursive_comb(int *indexes, int s, int rest, std::function<void(int *)> f) {
if (rest == 0) {
f(indexes);
} else {
if (s < 0) return;
recursive_comb(indexes, s - 1, rest, f);
indexes[rest - 1] = s;
recursive_comb(indexes, s - 1, rest - 1, f);
}
}
// nCkの組み合わせに対して処理を実行する
void foreach_comb(int n, int k, std::function<void(int *)> f) {
int indexes[k];
recursive_comb(indexes, n - 1, k, f);
}
int main()
{
string S;
cin >> S;
vector<int> digit;
for(auto s: S){
int n = s-'0';
digit.push_back(n);
}
int ans = -1;
FOR(k, 1, digit.size()+1){
foreach_comb(digit.size(), k, [&](int *indexes){
int s = 0;
REP(i, k) s += digit[indexes[i]];
if(s%3 == 0) ans = int(S.size())-k;
});
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vc = vector<char>;
using vcc = vector<vc>;
using Pii = pair<int,int>;
using ld = long double;
int main(){
int h,w,x,y;
cin >> h >> w >> x >> y;
vvi V(200, vi(200,0));
for(int i = 0; i<h; i++){
for(int j = 0; j<w; j++){
char c;
cin >> c;
if(c=='#'){
V[i][j] = 1;
}
}
}
int ans = 0;
for(int i = x-2; i>=0; i--){
if(V[i][y-1]){
break;
}
else{
ans++;
}
}
for(int i = x; i<h; i++){
if(V[i][y-1]){
break;
}
else{
ans++;
}
}
for(int j = y-2; j>=0; j--){
if(V[x-1][j]){
break;
}
else{
ans++;
}
}
for(int j = y; j<w; j++){
if(V[x-1][j]){
break;
}
else{
ans++;
}
}
cout << ans+1 << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define debug(a) { cerr << #a << ':' << a << endl; }
#define endl '\n'
#define fi first
#define se second
using lint = long long;
using P = pair<int,int>;
template<class T> using V = vector<T>;
template<class T> using priq = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
template<class T> T ceil(const T &a, const T &b) { return (a+b-1)/b; }
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;
template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
return out << "{ " << p.first << ',' << p.second << " } ";
}
template<class T> istream& operator>>(istream &in, vector<T> &v) {
for(auto &&e: v) in >> e;
return in;
}
template<class T> ostream& operator<<(ostream &out, const vector<T> &v) {
for(const auto &e: v) out << e << ' ';
return out;
}
/*----------------------------------------------------------------------------------------------------*/
int n, m;
V<string> s;
// fstream out("AHC004_out.txt");
int main() {
clock_t start = clock();
cin >> n >> m;
s.resize(m);
cin >> s;
V<P> cnts(m); // pair = (count, index);
V<V<int>> ins(m);
rep(i,m) {
int cnt = 0;
rep(j,m) {
bool isin = ( s[i].find(s[j]) != string::npos );
if(isin) {
cnt++;
ins[i].push_back(j);
}
}
cnts[i] = make_pair(cnt,i);
}
sort(rall(cnts));
V<bool> used(m);
V<string> ans(n);
rep(i,n) { // 初期解生成
string str = "";
rep(j,m) {
int x = cnts[j].se;
if(used[x]) continue;
string plus = s[x];
for(int k = min(sz(str), sz(s[x])); k >= 0; k--) {
if( str.substr(sz(str)-k,k) == s[x].substr(0,k) ) {
plus = s[x].substr(k,sz(s[x])-k);
break;
}
}
if(sz(str) + sz(plus) > n) continue;
str += plus;
for(int y: ins[x]) used[y] = true;
}
str.resize(n,'.');
ans[i] = str;
}
V<int> v(n); iota(all(v), 0);
pair<int,V<int>> mx = make_pair(0,v);
random_device seed_gen;
mt19937 engine(seed_gen());
while(clock() - start < 2980) {
V<bool> nused = used;
int cnt = 0;
rep(i,n) {
string ns = "";
rep(j,n) ns += string(1,ans[v[i]][j]);
ns += ns;
rep(j,m) {
if(nused[j]) continue;
if( ns.find(s[j]) != string::npos ) {
cnt++;
nused[j] = true;
}
}
}
chmax(mx, make_pair(cnt, v));
shuffle(all(v), engine);
}
rep(i,n) {
cout << ans[mx.se[i]] << endl;
}
} |
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
#include <iomanip>
#include <limits>
#include <random>
#include <unordered_set>
#include <chrono>
typedef int64_t ll;
typedef std::pair<int, int> Pii;
typedef std::pair<ll, ll> Pll;
typedef std::pair<double, double> Pdd;
#define rip(_i, _n, _s) for (int _i = (_s); _i < (int)(_n); _i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<
template<typename _T>
using MaxHeap = std::priority_queue<_T>;
template<typename _T>
using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>;
template<typename _T>
inline bool chmax(_T &_l, const _T _b) {
if (_l < _b) {
_l = _b;
return true;
}
return false;
}
template<typename _T>
inline bool chmin(_T &_l, const _T _b) {
if (_l > _b) {
_l = _b;
return true;
}
return false;
}
template<typename _T>
void vdeb(const std::vector<_T> &bb) {
for (unsigned int i = 0;i < bb.size();i++) {
if (i == bb.size() - 1) std::cout << bb[i];
else std::cout << bb[i] << ' ';
}
std::cout << '\n';
}
template<typename _T>
void vdeb(const std::vector<std::vector<_T>> &bb) {
for (unsigned int i = 0;i < bb.size();i++) {
// std::cout << i << ' ';
vdeb(bb[i]);
}
std::cout << '\n';
}
using namespace std;
int main() {
int a, b, c; cin >> a >> b >> c;
set<int> st = {a, b, c};
if(st.size() < 3) {
cout << (a^b^c) << endl;
}
else {
cout << 0 << endl;
}
} | #include<iostream>
using namespace std;
int main() {
int a, b, c;
cin >> a >> b >> c;
if (a != b && a != c && b != c) cout << 0 << endl;
else {
if (a == b) {
cout << c << endl;
return 0;
}
if (a == c) {
cout << b << endl;
return 0;
}
if (c == b) {
cout << a << endl;
return 0;
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
signed main() {
int n, m;
cin >> n >> m;
int a[m], b[m];
rep(i, m) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
int dp[1 << n] = {};
fill(dp, dp + (1 << n), INF);
for (int bit = 1; bit < 1 << n; bit++) {
int bp = __builtin_popcount(bit);
int cnt = 0;
rep(i, m) {
if ((bit & 1 << a[i]) and (bit & 1 << b[i])) cnt++;
}
if (cnt == bp * (bp - 1) / 2) dp[bit] = 1;
}
for (int S = 0; S < 1 << n; S++) {
for (int T = S;; T = (T - 1) & S) {
if (T == 0) break;
dp[S] = min(dp[S], dp[T] + dp[S ^ T]);
}
}
cout << dp[(1 << n) - 1] << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
const int MAXN=1<<18,INF=-0x3f3f3f3f3f;
int N,F[MAXN],G[MAXN],U,Con[MAXN],IsCon[MAXN];
inline int lowbit(int n) { return n&(-n); }
inline bool iscon(int b,int s){
if(G[s]!=1) return 0;
while(s){
if(!Con[b|lowbit(s)]) return 0;
s^=lowbit(s);
}
return 1;
}
vector<int> Alw;
inline void dp(int S){
if( S==lowbit(S)||iscon(lowbit(S),S^lowbit(S)) ){
F[S]=G[S]=1;
Alw.push_back(S);
return ;
}
for(auto s : Alw)
if((S&s)==s){
F[S]=min(F[S],F[S^s]+1);
if(F[S]==2) return ;
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
for(int i=0;i<MAXN;i++) F[i]=G[i]=-INF;
int M;
cin>>N>>M;
U=(1<<N)-1;
for(int i=1;i<=M;i++){
int a,b;
cin>>a>>b;
Con[(1<<a-1)|(1<<b-1)]=1;
}
for(int i=1;i<=U;i++) dp(i);
cout<<F[U]<<endl;
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>
// AtCoder
// #include <atcoder/all>
// using namespace atcoder;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif
#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;
int main (void)
{
cin.tie(0);
ios::sync_with_stdio(false);
int t; cin >> t;
rep (_, t) {
int n; cin >> n;
vector<int> p(n), prev(n);
rep (i, n) {
cin >> p[i]; --p[i];
prev[p[i]] = i;
}
vector<int> res;
int piv = 0, cnt = 0;
while (piv < n) {
// rep (x, 10) {
eprintf("piv=%d cnt=%d : ", piv, cnt);
rep (i, n) eprintf("%d ", p[i]);
if (prev[piv] == piv) {
eprintf("A\n");
++piv;
continue;
}
int pivplace = prev[piv];
if ((pivplace + 1) % 2 == cnt % 2) {
eprintf("B\n");
swap(p[pivplace - 1], p[pivplace]);
swap(prev[p[pivplace - 1]], prev[p[pivplace]]);
++cnt;
res.push_back(pivplace);
} else {
if (p[pivplace - 2] > piv) {
eprintf("C\n");
swap(p[pivplace - 2], p[pivplace - 1]);
swap(prev[p[pivplace - 2]], prev[p[pivplace - 1]]);
++cnt;
res.push_back(pivplace - 1);
} else {
eprintf("D\n");
swap(p[pivplace - 1], p[pivplace]);
swap(prev[p[pivplace - 1]], prev[p[pivplace]]);
cnt += 5;
res.push_back(pivplace - 1);
res.push_back(pivplace);
res.push_back(pivplace - 1);
res.push_back(pivplace);
res.push_back(pivplace - 1);
}
}
}
cout << res.size() << '\n';
rep (i, res.size()) cout << res[i] << " \n"[i + 1 == i_len];
}
return 0;
} | //AnkitCode99 here....
//every ups and downs matter!
#include<bits/stdc++.h>
#define endl "\n"
#define IOS ios_base::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr)
typedef long long int ll;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
using namespace std;
const ll sz=(1e5+5)*2;
const ll szz=1e6+6;
const ll mod=1e9+7;
ll n,m,price[sz],maxi[sz];
vector<ll> ar[sz];
bool vis[sz];
ll ans = -1e16;
void dfs(ll node)
{
vis[node]=1;
if(maxi[node]!=-1)
{
return;
}
maxi[node]=0;
for(auto x:ar[node])
{
dfs(x);
maxi[node] = max(maxi[node],maxi[x]);
}
if(ar[node].size())
{
ans = max(ans,maxi[node]-price[node]);
}
maxi[node] = max(maxi[node],price[node]);
}
int main()
{
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("op.txt","w",stdout);
#endif
clock_t startTime=clock();
cin>>n>>m;
rep(i,1,n+1)
{
cin>>price[i];
}
rep(i,0,m)
{
ll a,b;
cin>>a>>b;
ar[a].pb(b);
}
memset(maxi,-1,sizeof maxi);
rep(i,1,n+1)
{
if(!vis[i])
{
dfs(i);
}
}
cout<<ans<<endl;
cerr << endl <<setprecision(20)<< double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
}//Goodbye... |
// Author: wlzhouzhuan
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pb push_back
#define fir first
#define sec second
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, l, r) for (int i = l; i >= r; i--)
#define mset(s, t) memset(s, t, sizeof(s))
#define mcpy(s, t) memcpy(s, t, sizeof(t))
#define poly vector<int>
#define SZ(x) (int(x.size()))
template<typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; }
int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> void print(T x, char let) {
print(x), putchar(let);
}
bitset<100005> dp;
int n, m;
int main() {
n = read(), m = 0;
dp.set(0);
for (int i = 1; i <= n; i++) {
int x = read();
dp |= dp << x;
m += x;
}
for (int i = m / 2; i >= 0; i--) {
if (dp.test(i)) {
print(max(i, m - i), '\n');
return 0;
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}
#define num_l(a,v) POSL(a,v) //v未満の要素数
#define num_eql(a,v) POSU(a,v) //v以下の要素数
#define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数
#define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数
// static_cast< long long ll >
int N = 30*30;
int L = 30;
// (i,j) i個下 , j個右
int ind(int a,int b){
return a*L + b;
}
//vector< vector<string> > S(N,vector<string>(N));
vector<vector<long double>> H(L,vector<long double>(L,5000.0));
vector<vector<long double>> V(L,vector<long double>(L,5000.0));
// 上は下より(i,j)のiが小さい 右は左より(i,j)のjが大きい
// 順路正の向きで
const int From_U = 0;//上から来た
const int From_D = 1;//下から来た
const int From_L = 2;//左から来た
const int From_R = 3;//右から来た
// 復元
string make_char = "DURL";
const int dy[] = {-1,1,0,0};
const int dx[] = {0,0,-1,1};
int main(){
int sy,sx,ty,tx;
int s,t;
FOR(k,0,1000){
cin >> sy >> sx >> ty >> tx;
int dir_y = 1;
int dir_x = 1;
if(sy > ty) dir_y = -1;
if(sx > tx) dir_x = -1;
// (i,j) には (i - dir_y , j) or (i , j - dir_x) から行く
vector< vector<long double> > score(L,vector<long double>(L,INF));
vector< vector<int> > from(L,vector<int>(L,-1));
score[sy][sx] = 0;
for(int y=sy;(y-dir_y)!=ty;y+=dir_y){
for(int x=sx;(x-dir_x)!=tx;x+=dir_x){
//cout << y << " " << x << endl;
if(y==sy && x==sx) continue;
long double a = INF,b = INF;
if(y != sy) a = score[y-dir_y][x] + H[min(y-dir_y,y)][x];
if(x != sx) b = score[y][x-dir_x] + V[y][min(x,x-dir_x)];
if(a < b){
from[y][x] = (dir_y == 1 ? From_U : From_D );
score[y][x] = a;
}else{
from[y][x] = (dir_x == 1 ? From_L : From_R );
score[y][x] = b;
}
}
}
//return 0;
//cout << "95" << endl;
string T;
int y=ty,x=tx;
while(y!=sy || x!=sx){
int a = from[y][x];
T += make_char[a];
assert(0 <= a && a <= 3);
y += dy[a];
x += dx[a];
//cout << "from[" << y << "][" << x << "] = " << a << endl;
}
//cout << "106" << endl;
//cout << T << endl;
REV(T);
/*{ // チェック
assert(T.size() == (abs(sy-ty)+abs(sx-tx)));
int ud=0,lr=0;
for(auto c:T){
if(c == 'U' || c == 'D') ud++;
if(c == 'L' || c == 'R') lr++;
}
assert(abs(sy-ty) == ud);
assert(abs(sx-tx) == lr);
}*/
cout << T << endl;
cout << flush;
long double score_now,r;
cin >> score_now;
r = pow( score_now / score[ty][tx] , 1.0 - 1.0*(abs(sy-ty) + abs(sx-tx)) / 112.0 );
y=ty,x=tx;
while(y!=sy || x!=sx){
int a = from[y][x];
int by = y + dy[a],bx = x + dx[a];
if(by != y){
H[min(by,y)][x] *= r;
}else{
V[y][min(bx,x)] *= r;
}
y += dy[a];
x += dx[a];
}
}
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define cnts(x,c) count(all(x),c)
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) {a = b; return 1;} return 0;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) {a = b; return 1;} return 0;}
template<class T> void print(const T &t) {cout<<t<<"\n";}
template<class T> void PRINT(const T &t) {rep(i,sz(t)) cout<<t[i]<<" \n"[i==sz(t)-1];}
const ll INF = 1LL << 62;
const int iINF = 1 << 30;
int n;
string s;
int main() {
fio(); cin>>n>>s;
if(*s.begin()!=*s.rbegin()) print(1);
else{
char c=*s.begin();
bool flg=false;
rep(i,n-1) if(s[i]!=c and s[i+1]!=c) flg=true;
print(flg?"2":"-1");
}
}
| #ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PIL = pair<int,ll>;
using PLI = pair<ll,int>;
using PLL = pair<ll,ll>;
using Graph = vector<vector<int>>;
using Cost_Graph = vector<vector<PIL>>;
template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;}
#define REP(i,n) for(int i=0;i<int(n);i++)
#define ROUNDUP(a,b) ((a+b-1)/b)
#define YESNO(T) cout<<(T?"YES":"NO")<<endl
#define yesno(T) cout<<(T?"yes":"no")<<endl
#define YesNo(T) cout<<(T?"Yes":"No")<<endl
const int INFint = 1 << 30;
const ll INFLL = 1LL << 60;
const ll MOD = 1000000007LL;
const double pi = 3.14159265358979;
int si,sj;
vector<vector<int>> tile(50, vector<int>(50));
vector<vector<int>> profit(50, vector<int>(50));
vector<bool> seen(2500,false);
string ans;
void all_resize(){
return;
}
void all_input(){
cin >> si >> sj;
all_resize();
for (int i = 0; i < 50; i++){
for (int j = 0; j < 50; j++){
cin >> tile[i][j];
}
}
for (int i = 0; i < 50; i++){
for (int j = 0; j < 50; j++){
cin >> profit[i][j];
}
}
return;
}
void all_output(){
cout << ans << endl;
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
all_input();
all_output();
}
// |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define ln cout<<'\n'
void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<' ';pr(b...);}
void chmin(int& x, int y) { x = min(x, y);}
void solve() {
int n, k; cin >> n >> k;
int a[810][810];
rep(i, n) rep(j, n) cin >> a[i][j];
int l = -1, u = 1001001001;
int L = k*k/2+1;
while(l + 1 < u) {
bool ok = false;
int mid = (l + u) / 2;
{
int s[810][810];
rep(i, n) rep(j, n) s[i+1][j+1] = a[i][j] > mid ? 1:0;
rep(i, n+1) rep(j, n) s[i][j+1] += s[i][j];
rep(i, n) rep(j, n+1) s[i+1][j] += s[i][j];
rep(i, n-k+1) rep(j, n-k+1) {
int now = s[i+k][j+k];
now -= s[i][j+k];
now -= s[i+k][j];
now += s[i][j];
if (now < L) ok = true;
}
}
if (ok) u = mid; else l = mid;
}
cout << u << endl;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
solve();
return 0;
}
| #include <algorithm>
#include <iostream>
using namespace std;
const int N = 800, A = 1000000000;
int aa[N + 1][N + 1], pp[N + 1][N + 1];
int main() {
int n, k; cin >> n >> k;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> aa[i][j];
int lower = -1, upper = A;
while (upper - lower > 1) {
int a = (lower + upper) / 2;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
pp[i][j] = (aa[i][j] <= a) + pp[i - 1][j] + pp[i][j - 1] - pp[i - 1][j - 1];
bool yes = false;
for (int i = k; i <= n; i++)
for (int j = k; j <= n; j++)
if (pp[i][j] - pp[i - k][j] - pp[i][j - k] + pp[i - k][j - k] >= (k * k + 1) / 2)
yes = true;
if (yes)
upper = a;
else
lower = a;
}
cout << upper << '\n';
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<double> x(n),y(n);
for(int i=0; i<n; i++) {
cin >> x[i] >> y[i];
}
bool res = false;
for(int i=0; i<n-2; i++) {
for(int j=i+1; j<n-1; j++) {
for(int k=j+1; k<n; k++){
if((x[j]-x[i])*(y[k]-y[i])==(x[k]-x[i])*(y[j]-y[i])){
res = true;
break;
}
}
}
}
if(res) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
} | #include <cassert>
#include <cctype>
#include <cerrno>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int main()
{
double n,D,H,R;
double ans;
cin>>n>>D>>H;
R=100000000;
D=D;
H=H;
vector<double> d(n),h(n);
for(double i=0; i<n; i++){
cin>>d.at(i)>>h.at(i);
d.at(i)=D-d.at(i);
h.at(i)=H-h.at(i);
R=min(R,h.at(i)/d.at(i));
}
ans=H-D*R;
if(H>=D*R)cout<<ans<<endl;
else cout<<0<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define ff first
#define ss second
#define YES printf("YES\n")
#define NO printf("NO\n")
#define nn "\n"
#define sci(x) scanf("%d", &x)
#define LL_INF (1LL << 62)
#define INF (1 << 30)
#define SetBit(x, k) (x |= (1LL << k))
#define ClearBit(x, k) (x &= ~(1LL << k))
#define CheckBit(x, k) (x & (1LL << k))
#define mod 1000000007
#define N 100005
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
int x[m+2];
x[0] = 0;
x[1] = n+1;
for(int i = 2; i < m+2; i++){
cin >> x[i];
}
sort(x, x+(m+2));
int z = INF;
for(int i = 1; i < m+2; i++){
int diff = x[i]-x[i-1]-1;
if(diff) z = min(z, diff);
}
ll ans = 0;
for(int i = 1; i < m+2; i++){
int diff = x[i]-x[i-1]-1;
ans += (diff + z - 1)/z;
}
cout << ans << nn;
return 0;
}
| // Pratiyush Mishra
#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define pl pair<ll,ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define mem1(a) memset(a, -1, sizeof(a))
#define popf pop_front
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\n'
#define p2(x, y) cout << x << " " << y << '\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
#define printv(v) \
for (ll i = 0; i < v.size(); ++i) \
cout << v[i] << " "; \
cout << '\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\n'
#define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val) no. of elements strictly less than val
// s.find_by_order(i) itertor to ith element (0 indexed)
#define mod 1000000007
#define mod1 998244353
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define get(n) \
ll n; \
cin >> n
#define getvec(v, n) \
vector<ll> v(n); \
for (ll i = 0; i < n; i++) \
cin >> v[i];
#define getstr(s) \
string s; \
cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;
ll n, m;
vl a, b;
vector<vl> dp;
ll solve(ll i, ll j)
{
if (i >= n)
return m - j;
if (j >= m)
return n - i;
if (dp[i][j] != -1)
return dp[i][j];
ll ans1 = (a[i] != b[j]) + solve(i + 1, j + 1);
ll ans2 = solve(i + 1, j) + 1;
ll ans3 = solve(i, j + 1) + 1;
ll ans = min(ans1, min(ans2, ans3));
dp[i][j] = ans;
return ans;
}
void mainSolve()
{
cin >> n >> m;
dp.resize(n, vl(m, -1));
for (int i = 0; i < n; i++)
{
get(x);
a.pb(x);
}
for (int i = 0; i < m; i++)
{
get(x);
b.pb(x);
}
ll ans = solve(0, 0);
p1(ans);
}
int main()
{
fio;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//get(t);
ll t = 1;
while (t--)
{
mainSolve();
}
return 0;
}
|
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
#define all(v) (v).begin(),(v).end()
#define debug(x) cout << #x << ": " << x << '\n';
#define degreeToRadian(deg) (((deg)/360)*2*M_PI)
#define radianTodegree(rad) (((rad)/2/M_PI)*360)
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; }
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PL = pair<ll, ll>;
const ll INF = 1LL<<60;
const int MOD = 1e9 + 7;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
struct UnionFind {
int n;
vector<int> a;
vector<int> sz;
UnionFind(int _n) : n(_n), a(_n), sz(_n, 1) {
for(int i = 0; i < _n; i++) a[i] = i;
}
int root(int x) {
if (a[x] == x) return x;
return a[x] = root(a[x]);
}
void unite(int x, int y) {
int rx = root(x);
int ry = root(y);
if (rx == ry) return;
if (sz[rx] < sz[ry]) swap(rx, ry);
sz[rx] += sz[ry];
a[ry] = rx;
}
bool same(int x, int y) {
int rx = root(x);
int ry = root(y);
return rx == ry;
}
int size(int x) {
return sz[root(x)];
}
map<int, vector<int>> groups() {
map<int, vector<int>> group;
rep (i, n) root(i);
rep (i, n) group[a[i]].push_back(i);
return group;
}
};
int main() {
//cin.tie(0);ios::sync_with_stdio(false);
//cout << fixed << setprecision(15);
int N, M;
cin >> N >> M;
UnionFind uf(N);
vector<int> a(N), b(N);
rep (i, N) cin >> a[i];
rep (i, N) cin >> b[i];
rep (i, M) {
int c, d;
cin >> c >> d;
--c; --d;
uf.unite(c, d);
}
for (auto v : uf.groups()) {
ll x = 0, y = 0;
for (auto vv : v.second) {
x += a[vv];
y += b[vv];
}
if (x == y) continue;
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
return 0;
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ll long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
long long powm(long long a, long long b,long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a %mod;
a = a * a %mod;
b >>= 1;
}
return res;
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if(fopen("INPUT.txt","r"))
{
freopen("INPUT.txt","r",stdin);
freopen("OUTPUT.txt","w",stdout);
}
#endif
int k,n,m;
cin>>k>>n>>m;
int a[k+1];
for(int i=1;i<=k;i++)
cin>>a[i];
int l=0,r=1e18;
int mnf=r+1;
while(l<=r)
{
int mid=l+(r-l)/2;
int mn[k+1]={0};
int mx[k+1]={0};
int tot1=0;
int tot2=0;
for(int i=1;i<=k;i++)
{
int z1=mid+a[i]*m;
z1/=n;
int z2=-mid+a[i]*m;
z2=(z2+n-1)/n;
mn[i]=max(0LL,z2);
mx[i]=min(z1,m);
tot1+=mx[i];
tot2+=mn[i];
}
if(tot1>=m && m>=tot2)
{
mnf=min(mnf,mid);
r=mid-1;
}
else
{
l=mid+1;
}
}
int mn[k+1];
int mx[k+1];
int sum1[k+1]={0};
int sum2[k+1]={0};
vector<int>ans;
for(int i=1;i<=k;i++)
{
int z1=mnf+a[i]*m;
z1/=n;
int z2=-mnf+a[i]*m;
z2=(z2+n-1)/n;
mn[i]=max(0LL,z2);
mx[i]=min(z1,m);
sum1[i]=sum1[i-1]+mx[i];
sum2[i]=sum2[i-1]+mn[i];
}
for(int i=k;i>=1;i--)
{
int z1=m-sum1[i-1];
int z2=m-sum2[i-1];
z1=max(mn[i],z1);
z2=min(mx[i],z2);
ans.push_back(z1);
m-=z1;
}
reverse(ans.begin(),ans.end());
for(auto i:ans)
cout<<i<<" ";
} |
#include <bits/stdc++.h>
using namespace std;
typedef long double LD;
typedef pair<LD, LD> PDD;
const LD eps = 1e-6;
int main()
{
int n, m, k; scanf("%d %d %d", &n, &m, &k);
vector<int> a(k + 1), vis(n + 1);
for (int i = 1; i <= k; i++)
scanf("%d", &a[i]), vis[a[i]] = 1;
vector<PDD> suf(n + m + 1, {0.0, 0.0}), f(n + 1);
for (int i = n - 1; i >= 0; i--)
{
if (vis[i]) f[i] = (PDD){1.0, 0.0};
else f[i] = (PDD){(suf[i + 1].first - suf[i + m + 1].first) / m,
(suf[i + 1].second - suf[i + m + 1].second) / m + 1.0};
suf[i] = (PDD){suf[i + 1].first + f[i].first, suf[i + 1].second + f[i].second};
}
if (abs(f[0].first - 1.0) < eps) printf("-1\n");
else printf("%.10Lf\n", f[0].second / (1.0 - f[0].first));
return 0;
} | //include <atcoder>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
#define pf(dans) printf("%.12lf", dans)
#define pfn(dans) pf(dans); cout << endl;
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9 + 10;
const ll LINF = (ll)1e18 + 10;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)
template <typename T>
T my_abs(T x){
return (x >= 0)? x : -x;
}
template <typename T>
void chmax(T &a, T b){
a = max(a, b);
}
template <typename T>
void chmin(T &a, T b){
a = min(a, b);
}
ll gcd(ll a, ll b){
if (a > b) return gcd(b, a);
if (a == 0) return b;
return gcd(b % a, a);
}
bool incld_bit(ll bit, int i){
return ((bit>>i) & 1) == 1;
}
// --------------------------------------------------------------------------------
int n;
int x[20], y[20], z[20];
ll dp[(1<<17) + 3][20];
bool u[(1<<17) + 3][20] = {false};
int dist(int u, int v){
return my_abs(x[v] - x[u]) + my_abs(y[v] - y[u]) + max(0, z[v] - z[u]);
}
void dfs(int bit, int v){
int i;
if (u[bit][v]){
return;
}
rep0(i, n){
if (i != v && incld_bit(bit, i)){
dfs(bit - (1<<v), i);
chmin(dp[bit][v], dp[bit - (1<<v)][i] + dist(i, v));
}
}
u[bit][v] = true;
}
int main(void){
int i, j;
cin >> n;
rep0(i, n){
cin >> x[i] >> y[i] >> z[i];
}
int bit;
rep0(bit, 1<<n){
rep0(i, n){
dp[bit][i] = LINF;
}
}
dp[1][0] = 0;
ll ans;
ans = LINF;
rep0(i, n){
dfs((1<<n) - 1, i);
chmin(ans, dp[(1<<n) - 1][i] + dist(i, 0));
}
cout << ans << endl;
return 0;
} |